sym53c500_cs.c 23.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/*
*  sym53c500_cs.c	Bob Tracy (rct@frus.com)
*
*  A rewrite of the pcmcia-cs add-on driver for newer (circa 1997)
*  New Media Bus Toaster PCMCIA SCSI cards using the Symbios Logic
*  53c500 controller: intended for use with 2.6 and later kernels.
*  The pcmcia-cs add-on version of this driver is not supported
*  beyond 2.4.  It consisted of three files with history/copyright
*  information as follows:
*
*  SYM53C500.h
*	Bob Tracy (rct@frus.com)
*	Original by Tom Corner (tcorner@via.at).
*	Adapted from NCR53c406a.h which is Copyrighted (C) 1994
*	Normunds Saumanis (normunds@rx.tech.swh.lv)
*
*  SYM53C500.c
*	Bob Tracy (rct@frus.com)
*	Original driver by Tom Corner (tcorner@via.at) was adapted
*	from NCR53c406a.c which is Copyrighted (C) 1994, 1995, 1996 
*	Normunds Saumanis (normunds@fi.ibm.com)
*
*  sym53c500.c
*	Bob Tracy (rct@frus.com)
*	Original by Tom Corner (tcorner@via.at) was adapted from a
*	driver for the Qlogic SCSI card written by
*	David Hinds (dhinds@allegro.stanford.edu).
* 
*  This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by the
*  Free Software Foundation; either version 2, or (at your option) any
*  later version.
*
*  This program is distributed in the hope that it will be useful, but
*  WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  General Public License for more details.
*/

#define SYM53C500_DEBUG 0
#define VERBOSE_SYM53C500_DEBUG 0

/*
*  Set this to 0 if you encounter kernel lockups while transferring 
*  data in PIO mode.  Note this can be changed via "sysfs".
*/
#define USE_FAST_PIO 1

/* =============== End of user configurable parameters ============== */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
#include <linux/spinlock.h>
#include <linux/bitops.h>

#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.h>

#include <scsi/scsi_ioctl.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>

#include <pcmcia/cistpl.h>
#include <pcmcia/ds.h>
#include <pcmcia/ciscode.h>


/* ================================================================== */

#define SYNC_MODE 0 		/* Synchronous transfer mode */

/* Default configuration */
#define C1_IMG   0x07		/* ID=7 */
#define C2_IMG   0x48		/* FE SCSI2 */
#define C3_IMG   0x20		/* CDB */
#define C4_IMG   0x04		/* ANE */
#define C5_IMG   0xa4		/* ? changed from b6= AA PI SIE POL */
#define C7_IMG   0x80		/* added for SYM53C500 t. corner */

/* Hardware Registers: offsets from io_port (base) */

/* Control Register Set 0 */
#define TC_LSB		0x00		/* transfer counter lsb */
#define TC_MSB		0x01		/* transfer counter msb */
#define SCSI_FIFO	0x02		/* scsi fifo register */
#define CMD_REG		0x03		/* command register */
#define STAT_REG	0x04		/* status register */
#define DEST_ID		0x04		/* selection/reselection bus id */
#define INT_REG		0x05		/* interrupt status register */
#define SRTIMOUT	0x05		/* select/reselect timeout reg */
#define SEQ_REG		0x06		/* sequence step register */
#define SYNCPRD		0x06		/* synchronous transfer period */
#define FIFO_FLAGS	0x07		/* indicates # of bytes in fifo */
#define SYNCOFF		0x07		/* synchronous offset register */
#define CONFIG1		0x08		/* configuration register */
#define CLKCONV		0x09		/* clock conversion register */
/* #define TESTREG	0x0A */		/* test mode register */
#define CONFIG2		0x0B		/* configuration 2 register */
#define CONFIG3		0x0C		/* configuration 3 register */
#define CONFIG4		0x0D		/* configuration 4 register */
#define TC_HIGH		0x0E		/* transfer counter high */
/* #define FIFO_BOTTOM	0x0F */		/* reserve FIFO byte register */

/* Control Register Set 1 */
/* #define JUMPER_SENSE	0x00 */		/* jumper sense port reg (r/w) */
/* #define SRAM_PTR	0x01 */		/* SRAM address pointer reg (r/w) */
/* #define SRAM_DATA	0x02 */		/* SRAM data register (r/w) */
#define PIO_FIFO	0x04		/* PIO FIFO registers (r/w) */
/* #define PIO_FIFO1	0x05 */		/*  */
/* #define PIO_FIFO2	0x06 */		/*  */
/* #define PIO_FIFO3	0x07 */		/*  */
#define PIO_STATUS	0x08		/* PIO status (r/w) */
/* #define ATA_CMD	0x09 */		/* ATA command/status reg (r/w) */
/* #define ATA_ERR	0x0A */		/* ATA features/error reg (r/w) */
#define PIO_FLAG	0x0B		/* PIO flag interrupt enable (r/w) */
#define CONFIG5		0x09		/* configuration 5 register */
/* #define SIGNATURE	0x0E */		/* signature register (r) */
/* #define CONFIG6	0x0F */		/* configuration 6 register (r) */
#define CONFIG7		0x0d

/* select register set 0 */
#define REG0(x)		(outb(C4_IMG, (x) + CONFIG4))
/* select register set 1 */
#define REG1(x)		outb(C7_IMG, (x) + CONFIG7); outb(C5_IMG, (x) + CONFIG5)

#if SYM53C500_DEBUG
#define DEB(x) x
#else
#define DEB(x)
#endif

#if VERBOSE_SYM53C500_DEBUG
#define VDEB(x) x
#else
#define VDEB(x)
#endif

#define LOAD_DMA_COUNT(x, count) \
  outb(count & 0xff, (x) + TC_LSB); \
  outb((count >> 8) & 0xff, (x) + TC_MSB); \
  outb((count >> 16) & 0xff, (x) + TC_HIGH);

/* Chip commands */
#define DMA_OP               0x80

#define SCSI_NOP             0x00
#define FLUSH_FIFO           0x01
#define CHIP_RESET           0x02
#define SCSI_RESET           0x03
#define RESELECT             0x40
#define SELECT_NO_ATN        0x41
#define SELECT_ATN           0x42
#define SELECT_ATN_STOP      0x43
#define ENABLE_SEL           0x44
#define DISABLE_SEL          0x45
#define SELECT_ATN3          0x46
#define RESELECT3            0x47
#define TRANSFER_INFO        0x10
#define INIT_CMD_COMPLETE    0x11
#define MSG_ACCEPT           0x12
#define TRANSFER_PAD         0x18
#define SET_ATN              0x1a
#define RESET_ATN            0x1b
#define SEND_MSG             0x20
#define SEND_STATUS          0x21
#define SEND_DATA            0x22
#define DISCONN_SEQ          0x23
#define TERMINATE_SEQ        0x24
#define TARG_CMD_COMPLETE    0x25
#define DISCONN              0x27
#define RECV_MSG             0x28
#define RECV_CMD             0x29
#define RECV_DATA            0x2a
#define RECV_CMD_SEQ         0x2b
#define TARGET_ABORT_DMA     0x04

/* ================================================================== */

struct scsi_info_t {
191
	struct pcmcia_device	*p_dev;
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	struct Scsi_Host *host;
	unsigned short manf_id;
};

/*
*  Repository for per-instance host data.
*/
struct sym53c500_data {
	struct scsi_cmnd *current_SC;
	int fast_pio;
};

enum Phase {
    idle,
    data_out,
    data_in,
    command_ph,
    status_ph,
    message_out,
    message_in
};

/* ================================================================== */

static void
chip_init(int io_port)
{
	REG1(io_port);
	outb(0x01, io_port + PIO_STATUS);
	outb(0x00, io_port + PIO_FLAG);

	outb(C4_IMG, io_port + CONFIG4);	/* REG0(io_port); */
	outb(C3_IMG, io_port + CONFIG3);
	outb(C2_IMG, io_port + CONFIG2);
	outb(C1_IMG, io_port + CONFIG1);

	outb(0x05, io_port + CLKCONV);	/* clock conversion factor */
	outb(0x9C, io_port + SRTIMOUT);	/* Selection timeout */
	outb(0x05, io_port + SYNCPRD);	/* Synchronous transfer period */
	outb(SYNC_MODE, io_port + SYNCOFF);	/* synchronous mode */  
}

static void
SYM53C500_int_host_reset(int io_port)
{
	outb(C4_IMG, io_port + CONFIG4);	/* REG0(io_port); */
	outb(CHIP_RESET, io_port + CMD_REG);
	outb(SCSI_NOP, io_port + CMD_REG);	/* required after reset */
	outb(SCSI_RESET, io_port + CMD_REG);
	chip_init(io_port);
}

static __inline__ int
SYM53C500_pio_read(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
{
	int i;
	int len;	/* current scsi fifo size */

	REG1(base);
	while (reqlen) {
		i = inb(base + PIO_STATUS);
		/* VDEB(printk("pio_status=%x\n", i)); */
		if (i & 0x80) 
			return 0;

		switch (i & 0x1e) {
		default:
		case 0x10:	/* fifo empty */
			len = 0;
			break;
		case 0x0:
			len = 1;
			break; 
		case 0x8:	/* fifo 1/3 full */
			len = 42;
			break;
		case 0xc:	/* fifo 2/3 full */
			len = 84;
			break;
		case 0xe:	/* fifo full */
			len = 128;
			break;
		}

		if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
			return 0;
		}

		if (len) {
			if (len > reqlen) 
				len = reqlen;

			if (fast_pio && len > 3) {
				insl(base + PIO_FIFO, request, len >> 2);
				request += len & 0xfc; 
				reqlen -= len & 0xfc; 
			} else {
				while (len--) {
					*request++ = inb(base + PIO_FIFO);
					reqlen--;
				}
			} 
		}
	}
	return 0;
}

static __inline__ int
SYM53C500_pio_write(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
{
	int i = 0;
	int len;	/* current scsi fifo size */

	REG1(base);
	while (reqlen && !(i & 0x40)) {
		i = inb(base + PIO_STATUS);
		/* VDEB(printk("pio_status=%x\n", i)); */
		if (i & 0x80)	/* error */
			return 0;

		switch (i & 0x1e) {
		case 0x10:
			len = 128;
			break;
		case 0x0:
			len = 84;
			break;
		case 0x8:
			len = 42;
			break;
		case 0xc:
			len = 1;
			break;
		default:
		case 0xe:
			len = 0;
			break;
		}

		if (len) {
			if (len > reqlen)
				len = reqlen;

			if (fast_pio && len > 3) {
				outsl(base + PIO_FIFO, request, len >> 2);
				request += len & 0xfc;
				reqlen -= len & 0xfc;
			} else {
				while (len--) {
					outb(*request++, base + PIO_FIFO);
					reqlen--;
				}
			}
		}
	}
	return 0;
}

static irqreturn_t
351
SYM53C500_intr(int irq, void *dev_id)
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
{
	unsigned long flags;
	struct Scsi_Host *dev = dev_id;
	DEB(unsigned char fifo_size;)
	DEB(unsigned char seq_reg;)
	unsigned char status, int_reg;
	unsigned char pio_status;
	int port_base = dev->io_port;
	struct sym53c500_data *data =
	    (struct sym53c500_data *)dev->hostdata;
	struct scsi_cmnd *curSC = data->current_SC;
	int fast_pio = data->fast_pio;

	spin_lock_irqsave(dev->host_lock, flags);

	VDEB(printk("SYM53C500_intr called\n"));

	REG1(port_base);
	pio_status = inb(port_base + PIO_STATUS);
	REG0(port_base);
	status = inb(port_base + STAT_REG);
	DEB(seq_reg = inb(port_base + SEQ_REG));
	int_reg = inb(port_base + INT_REG);
	DEB(fifo_size = inb(port_base + FIFO_FLAGS) & 0x1f);

#if SYM53C500_DEBUG
	printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x", 
	    status, seq_reg, int_reg, fifo_size);
	printk(", pio=%02x\n", pio_status);
#endif /* SYM53C500_DEBUG */

	if (int_reg & 0x80) {	/* SCSI reset intr */
		DEB(printk("SYM53C500: reset intr received\n"));
		curSC->result = DID_RESET << 16;
		goto idle_out;
	}

	if (pio_status & 0x80) {
		printk("SYM53C500: Warning: PIO error!\n");
		curSC->result = DID_ERROR << 16;
		goto idle_out;
	}

	if (status & 0x20) {		/* Parity error */
		printk("SYM53C500: Warning: parity error!\n");
		curSC->result = DID_PARITY << 16;
		goto idle_out;
	}

	if (status & 0x40) {		/* Gross error */
		printk("SYM53C500: Warning: gross error!\n");
		curSC->result = DID_ERROR << 16;
		goto idle_out;
	}

	if (int_reg & 0x20) {		/* Disconnect */
		DEB(printk("SYM53C500: disconnect intr received\n"));
		if (curSC->SCp.phase != message_in) {	/* Unexpected disconnect */
			curSC->result = DID_NO_CONNECT << 16;
		} else {	/* Command complete, return status and message */
			curSC->result = (curSC->SCp.Status & 0xff)
			    | ((curSC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
		}
		goto idle_out;
	}

	switch (status & 0x07) {	/* scsi phase */
	case 0x00:			/* DATA-OUT */
		if (int_reg & 0x10) {	/* Target requesting info transfer */
421 422 423
			struct scatterlist *sg;
			int i;

L
Linus Torvalds 已提交
424 425 426
			curSC->SCp.phase = data_out;
			VDEB(printk("SYM53C500: Data-Out phase\n"));
			outb(FLUSH_FIFO, port_base + CMD_REG);
427
			LOAD_DMA_COUNT(port_base, scsi_bufflen(curSC));	/* Max transfer size */
L
Linus Torvalds 已提交
428
			outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
429 430 431

			scsi_for_each_sg(curSC, sg, scsi_sg_count(curSC), i) {
				SYM53C500_pio_write(fast_pio, port_base,
J
Jens Axboe 已提交
432
				    sg_virt(sg), sg->length);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439
			}
			REG0(port_base);
		}
		break;

	case 0x01:		/* DATA-IN */
		if (int_reg & 0x10) {	/* Target requesting info transfer */
440 441 442
			struct scatterlist *sg;
			int i;

L
Linus Torvalds 已提交
443 444 445
			curSC->SCp.phase = data_in;
			VDEB(printk("SYM53C500: Data-In phase\n"));
			outb(FLUSH_FIFO, port_base + CMD_REG);
446
			LOAD_DMA_COUNT(port_base, scsi_bufflen(curSC));	/* Max transfer size */
L
Linus Torvalds 已提交
447
			outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
448 449 450

			scsi_for_each_sg(curSC, sg, scsi_sg_count(curSC), i) {
				SYM53C500_pio_read(fast_pio, port_base,
J
Jens Axboe 已提交
451
					sg_virt(sg), sg->length);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
			}
			REG0(port_base);
		}
		break;

	case 0x02:		/* COMMAND */
		curSC->SCp.phase = command_ph;
		printk("SYM53C500: Warning: Unknown interrupt occurred in command phase!\n");
		break;

	case 0x03:		/* STATUS */
		curSC->SCp.phase = status_ph;
		VDEB(printk("SYM53C500: Status phase\n"));
		outb(FLUSH_FIFO, port_base + CMD_REG);
		outb(INIT_CMD_COMPLETE, port_base + CMD_REG);
		break;

	case 0x04:		/* Reserved */
	case 0x05:		/* Reserved */
		printk("SYM53C500: WARNING: Reserved phase!!!\n");
		break;

	case 0x06:		/* MESSAGE-OUT */
		DEB(printk("SYM53C500: Message-Out phase\n"));
		curSC->SCp.phase = message_out;
		outb(SET_ATN, port_base + CMD_REG);	/* Reject the message */
		outb(MSG_ACCEPT, port_base + CMD_REG);
		break;

	case 0x07:		/* MESSAGE-IN */
		VDEB(printk("SYM53C500: Message-In phase\n"));
		curSC->SCp.phase = message_in;

		curSC->SCp.Status = inb(port_base + SCSI_FIFO);
		curSC->SCp.Message = inb(port_base + SCSI_FIFO);

		VDEB(printk("SCSI FIFO size=%d\n", inb(port_base + FIFO_FLAGS) & 0x1f));
		DEB(printk("Status = %02x  Message = %02x\n", curSC->SCp.Status, curSC->SCp.Message));

		if (curSC->SCp.Message == SAVE_POINTERS || curSC->SCp.Message == DISCONNECT) {
			outb(SET_ATN, port_base + CMD_REG);	/* Reject message */
			DEB(printk("Discarding SAVE_POINTERS message\n"));
		}
		outb(MSG_ACCEPT, port_base + CMD_REG);
		break;
	}
out:
	spin_unlock_irqrestore(dev->host_lock, flags);
	return IRQ_HANDLED;

idle_out:
	curSC->SCp.phase = idle;
	curSC->scsi_done(curSC);
	goto out;
}

static void
509
SYM53C500_release(struct pcmcia_device *link)
L
Linus Torvalds 已提交
510 511 512 513
{
	struct scsi_info_t *info = link->priv;
	struct Scsi_Host *shost = info->host;

514
	dev_dbg(&link->dev, "SYM53C500_release\n");
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

	/*
	*  Do this before releasing/freeing resources.
	*/
	scsi_remove_host(shost);

	/*
	*  Interrupts getting hosed on card removal.  Try
	*  the following code, mostly from qlogicfas.c.
	*/
	if (shost->irq)
		free_irq(shost->irq, shost);
	if (shost->io_port && shost->n_io_port)
		release_region(shost->io_port, shost->n_io_port);

530
	pcmcia_disable_device(link);
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

	scsi_host_put(shost);
} /* SYM53C500_release */

static const char*
SYM53C500_info(struct Scsi_Host *SChost)
{
	static char info_msg[256];
	struct sym53c500_data *data =
	    (struct sym53c500_data *)SChost->hostdata;

	DEB(printk("SYM53C500_info called\n"));
	(void)snprintf(info_msg, sizeof(info_msg),
	    "SYM53C500 at 0x%lx, IRQ %d, %s PIO mode.", 
	    SChost->io_port, SChost->irq, data->fast_pio ? "fast" : "slow");
	return (info_msg);
}

static int 
SYM53C500_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
{
	int i;
	int port_base = SCpnt->device->host->io_port;
	struct sym53c500_data *data =
	    (struct sym53c500_data *)SCpnt->device->host->hostdata;

	VDEB(printk("SYM53C500_queue called\n"));

	DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n", 
	    SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->device->id, 
561
	    SCpnt->device->lun,  scsi_bufflen(SCpnt)));
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574

	VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
	    printk("cmd[%d]=%02x  ", i, SCpnt->cmnd[i]));
	VDEB(printk("\n"));

	data->current_SC = SCpnt;
	data->current_SC->scsi_done = done;
	data->current_SC->SCp.phase = command_ph;
	data->current_SC->SCp.Status = 0;
	data->current_SC->SCp.Message = 0;

	/* We are locked here already by the mid layer */
	REG0(port_base);
575
	outb(scmd_id(SCpnt), port_base + DEST_ID);	/* set destination */
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	outb(FLUSH_FIFO, port_base + CMD_REG);	/* reset the fifos */

	for (i = 0; i < SCpnt->cmd_len; i++) {
		outb(SCpnt->cmnd[i], port_base + SCSI_FIFO);
	}
	outb(SELECT_NO_ATN, port_base + CMD_REG);

	return 0;
}

static int 
SYM53C500_host_reset(struct scsi_cmnd *SCpnt)
{
	int port_base = SCpnt->device->host->io_port;

	DEB(printk("SYM53C500_host_reset called\n"));
592
	spin_lock_irq(SCpnt->device->host->host_lock);
L
Linus Torvalds 已提交
593
	SYM53C500_int_host_reset(port_base);
594
	spin_unlock_irq(SCpnt->device->host->host_lock);
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

	return SUCCESS;
}

static int 
SYM53C500_biosparm(struct scsi_device *disk,
    struct block_device *dev,
    sector_t capacity, int *info_array)
{
	int size;

	DEB(printk("SYM53C500_biosparm called\n"));

	size = capacity;
	info_array[0] = 64;		/* heads */
	info_array[1] = 32;		/* sectors */
	info_array[2] = size >> 11;	/* cylinders */
	if (info_array[2] > 1024) {	/* big disk */
		info_array[0] = 255;
		info_array[1] = 63;
		info_array[2] = size / (255 * 63);
	}
	return 0;
}

static ssize_t
621 622
SYM53C500_show_pio(struct device *dev, struct device_attribute *attr,
		   char *buf)
L
Linus Torvalds 已提交
623
{
624
	struct Scsi_Host *SHp = class_to_shost(dev);
L
Linus Torvalds 已提交
625 626 627 628 629 630 631
	struct sym53c500_data *data =
	    (struct sym53c500_data *)SHp->hostdata;

	return snprintf(buf, 4, "%d\n", data->fast_pio);
}

static ssize_t
632 633
SYM53C500_store_pio(struct device *dev, struct device_attribute *attr,
		    const char *buf, size_t count)
L
Linus Torvalds 已提交
634 635
{
	int pio;
636
	struct Scsi_Host *SHp = class_to_shost(dev);
L
Linus Torvalds 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
	struct sym53c500_data *data =
	    (struct sym53c500_data *)SHp->hostdata;

	pio = simple_strtoul(buf, NULL, 0);
	if (pio == 0 || pio == 1) {
		data->fast_pio = pio;
		return count;
	}
	else
		return -EINVAL;
}

/*
*  SCSI HBA device attributes we want to
*  make available via sysfs.
*/
653
static struct device_attribute SYM53C500_pio_attr = {
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661
	.attr = {
		.name = "fast_pio",
		.mode = (S_IRUGO | S_IWUSR),
	},
	.show = SYM53C500_show_pio,
	.store = SYM53C500_store_pio,
};

662
static struct device_attribute *SYM53C500_shost_attrs[] = {
L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	&SYM53C500_pio_attr,
	NULL,
};

/*
*  scsi_host_template initializer
*/
static struct scsi_host_template sym53c500_driver_template = {
     .module			= THIS_MODULE,
     .name			= "SYM53C500",
     .info			= SYM53C500_info,
     .queuecommand		= SYM53C500_queue,
     .eh_host_reset_handler	= SYM53C500_host_reset,
     .bios_param		= SYM53C500_biosparm,
     .proc_name			= "SYM53C500",
     .can_queue			= 1,
     .this_id			= 7,
     .sg_tablesize		= 32,
     .cmd_per_lun		= 1,
     .use_clustering		= ENABLE_CLUSTERING,
     .shost_attrs		= SYM53C500_shost_attrs
};

686
static int SYM53C500_config_check(struct pcmcia_device *p_dev, void *priv_data)
687
{
688
	p_dev->io_lines = 10;
689 690
	p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
	p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
691

692
	if (p_dev->resource[0]->start == 0)
693 694
		return -ENODEV;

695
	return pcmcia_request_io(p_dev);
696 697
}

698
static int
699
SYM53C500_config(struct pcmcia_device *link)
L
Linus Torvalds 已提交
700 701
{
	struct scsi_info_t *info = link->priv;
702
	int ret;
L
Linus Torvalds 已提交
703 704 705 706 707
	int irq_level, port_base;
	struct Scsi_Host *host;
	struct scsi_host_template *tpnt = &sym53c500_driver_template;
	struct sym53c500_data *data;

708
	dev_dbg(&link->dev, "SYM53C500_config\n");
L
Linus Torvalds 已提交
709

710 711
	info->manf_id = link->manf_id;

712 713 714 715
	ret = pcmcia_loop_config(link, SYM53C500_config_check, NULL);
	if (ret)
		goto failed;

716
	if (!link->irq)
717
		goto failed;
L
Linus Torvalds 已提交
718

719
	ret = pcmcia_enable_device(link);
720 721
	if (ret)
		goto failed;
L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729 730 731

	/*
	*  That's the trouble with copying liberally from another driver.
	*  Some things probably aren't relevant, and I suspect this entire
	*  section dealing with manufacturer IDs can be scrapped.	--rct
	*/
	if ((info->manf_id == MANFID_MACNICA) ||
	    (info->manf_id == MANFID_PIONEER) ||
	    (info->manf_id == 0x0098)) {
		/* set ATAcmd */
732 733 734
		outb(0xb4, link->resource[0]->start + 0xd);
		outb(0x24, link->resource[0]->start + 0x9);
		outb(0x04, link->resource[0]->start + 0xd);
L
Linus Torvalds 已提交
735 736 737 738 739 740 741 742 743 744 745 746
	}

	/*
	*  irq_level == 0 implies tpnt->can_queue == 0, which
	*  is not supported in 2.6.  Thus, only irq_level > 0
	*  will be allowed.
	*
	*  Possible port_base values are as follows:
	*
	*	0x130, 0x230, 0x280, 0x290,
	*	0x320, 0x330, 0x340, 0x350
	*/
747
	port_base = link->resource[0]->start;
748
	irq_level = link->irq;
L
Linus Torvalds 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

	DEB(printk("SYM53C500: port_base=0x%x, irq=%d, fast_pio=%d\n",
	    port_base, irq_level, USE_FAST_PIO);)

	chip_init(port_base);

	host = scsi_host_alloc(tpnt, sizeof(struct sym53c500_data));
	if (!host) {
		printk("SYM53C500: Unable to register host, giving up.\n");
		goto err_release;
	}

	data = (struct sym53c500_data *)host->hostdata;

	if (irq_level > 0) {
764
		if (request_irq(irq_level, SYM53C500_intr, IRQF_SHARED, "SYM53C500", host)) {
L
Linus Torvalds 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
			printk("SYM53C500: unable to allocate IRQ %d\n", irq_level);
			goto err_free_scsi;
		}
		DEB(printk("SYM53C500: allocated IRQ %d\n", irq_level));
	} else if (irq_level == 0) {
		DEB(printk("SYM53C500: No interrupts detected\n"));
		goto err_free_scsi;
	} else {
		DEB(printk("SYM53C500: Shouldn't get here!\n"));
		goto err_free_scsi;
	}

	host->unique_id = port_base;
	host->irq = irq_level;
	host->io_port = port_base;
	host->n_io_port = 0x10;
	host->dma_channel = -1;

	/*
	*  Note fast_pio is set to USE_FAST_PIO by
	*  default, but can be changed via "sysfs".
	*/
	data->fast_pio = USE_FAST_PIO;

	info->host = host;

	if (scsi_add_host(host, NULL))
		goto err_free_irq;

	scsi_scan_host(host);

796
	return 0;
L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804

err_free_irq:
	free_irq(irq_level, host);
err_free_scsi:
	scsi_host_put(host);
err_release:
	release_region(port_base, 0x10);
	printk(KERN_INFO "sym53c500_cs: no SCSI devices found\n");
805
	return -ENODEV;
L
Linus Torvalds 已提交
806

807
failed:
L
Linus Torvalds 已提交
808
	SYM53C500_release(link);
809
	return -ENODEV;
L
Linus Torvalds 已提交
810 811
} /* SYM53C500_config */

812
static int sym53c500_resume(struct pcmcia_device *link)
813 814 815
{
	struct scsi_info_t *info = link->priv;

816 817 818 819
	/* See earlier comment about manufacturer IDs. */
	if ((info->manf_id == MANFID_MACNICA) ||
	    (info->manf_id == MANFID_PIONEER) ||
	    (info->manf_id == 0x0098)) {
820 821 822
		outb(0x80, link->resource[0]->start + 0xd);
		outb(0x24, link->resource[0]->start + 0x9);
		outb(0x04, link->resource[0]->start + 0xd);
823
	}
824 825 826 827
	/*
	 *  If things don't work after a "resume",
	 *  this is a good place to start looking.
	 */
828
	SYM53C500_int_host_reset(link->resource[0]->start);
829 830 831 832

	return 0;
}

L
Linus Torvalds 已提交
833
static void
834
SYM53C500_detach(struct pcmcia_device *link)
L
Linus Torvalds 已提交
835
{
836
	dev_dbg(&link->dev, "SYM53C500_detach\n");
L
Linus Torvalds 已提交
837

838
	SYM53C500_release(link);
L
Linus Torvalds 已提交
839 840 841 842 843

	kfree(link->priv);
	link->priv = NULL;
} /* SYM53C500_detach */

844
static int
845
SYM53C500_probe(struct pcmcia_device *link)
L
Linus Torvalds 已提交
846 847 848
{
	struct scsi_info_t *info;

849
	dev_dbg(&link->dev, "SYM53C500_attach()\n");
L
Linus Torvalds 已提交
850 851

	/* Create new SCSI device */
852
	info = kzalloc(sizeof(*info), GFP_KERNEL);
L
Linus Torvalds 已提交
853
	if (!info)
854
		return -ENOMEM;
855
	info->p_dev = link;
L
Linus Torvalds 已提交
856
	link->priv = info;
857
	link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
L
Linus Torvalds 已提交
858

859
	return SYM53C500_config(link);
L
Linus Torvalds 已提交
860 861 862 863 864 865
} /* SYM53C500_attach */

MODULE_AUTHOR("Bob Tracy <rct@frus.com>");
MODULE_DESCRIPTION("SYM53C500 PCMCIA SCSI driver");
MODULE_LICENSE("GPL");

866 867 868 869 870 871 872 873
static struct pcmcia_device_id sym53c500_ids[] = {
	PCMCIA_DEVICE_PROD_ID12("BASICS by New Media Corporation", "SCSI Sym53C500", 0x23c78a9d, 0x0099e7f7),
	PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "SCSI Bus Toaster Sym53C500", 0x085a850b, 0x45432eb8),
	PCMCIA_DEVICE_PROD_ID2("SCSI9000", 0x21648f44),
	PCMCIA_DEVICE_NULL,
};
MODULE_DEVICE_TABLE(pcmcia, sym53c500_ids);

L
Linus Torvalds 已提交
874 875 876 877 878
static struct pcmcia_driver sym53c500_cs_driver = {
	.owner		= THIS_MODULE,
	.drv		= {
		.name	= "sym53c500_cs",
	},
879
	.probe		= SYM53C500_probe,
880
	.remove		= SYM53C500_detach,
881
	.id_table       = sym53c500_ids,
882
	.resume		= sym53c500_resume,
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
};

static int __init
init_sym53c500_cs(void)
{
	return pcmcia_register_driver(&sym53c500_cs_driver);
}

static void __exit
exit_sym53c500_cs(void)
{
	pcmcia_unregister_driver(&sym53c500_cs_driver);
}

module_init(init_sym53c500_cs);
module_exit(exit_sym53c500_cs);