1 /*
2 *  sym53c500_cs.c	Bob Tracy (rct@frus.com)
3 *
4 *  A rewrite of the pcmcia-cs add-on driver for newer (circa 1997)
5 *  New Media Bus Toaster PCMCIA SCSI cards using the Symbios Logic
6 *  53c500 controller: intended for use with 2.6 and later kernels.
7 *  The pcmcia-cs add-on version of this driver is not supported
8 *  beyond 2.4.  It consisted of three files with history/copyright
9 *  information as follows:
10 *
11 *  SYM53C500.h
12 *	Bob Tracy (rct@frus.com)
13 *	Original by Tom Corner (tcorner@via.at).
14 *	Adapted from NCR53c406a.h which is Copyrighted (C) 1994
15 *	Normunds Saumanis (normunds@rx.tech.swh.lv)
16 *
17 *  SYM53C500.c
18 *	Bob Tracy (rct@frus.com)
19 *	Original driver by Tom Corner (tcorner@via.at) was adapted
20 *	from NCR53c406a.c which is Copyrighted (C) 1994, 1995, 1996
21 *	Normunds Saumanis (normunds@fi.ibm.com)
22 *
23 *  sym53c500.c
24 *	Bob Tracy (rct@frus.com)
25 *	Original by Tom Corner (tcorner@via.at) was adapted from a
26 *	driver for the Qlogic SCSI card written by
27 *	David Hinds (dhinds@allegro.stanford.edu).
28 *
29 *  This program is free software; you can redistribute it and/or modify it
30 *  under the terms of the GNU General Public License as published by the
31 *  Free Software Foundation; either version 2, or (at your option) any
32 *  later version.
33 *
34 *  This program is distributed in the hope that it will be useful, but
35 *  WITHOUT ANY WARRANTY; without even the implied warranty of
36 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37 *  General Public License for more details.
38 */
39 
40 #define SYM53C500_DEBUG 0
41 #define VERBOSE_SYM53C500_DEBUG 0
42 
43 /*
44 *  Set this to 0 if you encounter kernel lockups while transferring
45 *  data in PIO mode.  Note this can be changed via "sysfs".
46 */
47 #define USE_FAST_PIO 1
48 
49 /* =============== End of user configurable parameters ============== */
50 
51 #include <linux/module.h>
52 #include <linux/moduleparam.h>
53 #include <linux/errno.h>
54 #include <linux/init.h>
55 #include <linux/interrupt.h>
56 #include <linux/kernel.h>
57 #include <linux/slab.h>
58 #include <linux/string.h>
59 #include <linux/ioport.h>
60 #include <linux/blkdev.h>
61 #include <linux/spinlock.h>
62 #include <linux/bitops.h>
63 
64 #include <asm/io.h>
65 #include <asm/dma.h>
66 #include <asm/irq.h>
67 
68 #include <scsi/scsi_ioctl.h>
69 #include <scsi/scsi_cmnd.h>
70 #include <scsi/scsi_device.h>
71 #include <scsi/scsi.h>
72 #include <scsi/scsi_host.h>
73 
74 #include <pcmcia/cs_types.h>
75 #include <pcmcia/cs.h>
76 #include <pcmcia/cistpl.h>
77 #include <pcmcia/ds.h>
78 #include <pcmcia/ciscode.h>
79 
80 
81 /* ================================================================== */
82 
83 #define SYNC_MODE 0 		/* Synchronous transfer mode */
84 
85 /* Default configuration */
86 #define C1_IMG   0x07		/* ID=7 */
87 #define C2_IMG   0x48		/* FE SCSI2 */
88 #define C3_IMG   0x20		/* CDB */
89 #define C4_IMG   0x04		/* ANE */
90 #define C5_IMG   0xa4		/* ? changed from b6= AA PI SIE POL */
91 #define C7_IMG   0x80		/* added for SYM53C500 t. corner */
92 
93 /* Hardware Registers: offsets from io_port (base) */
94 
95 /* Control Register Set 0 */
96 #define TC_LSB		0x00		/* transfer counter lsb */
97 #define TC_MSB		0x01		/* transfer counter msb */
98 #define SCSI_FIFO	0x02		/* scsi fifo register */
99 #define CMD_REG		0x03		/* command register */
100 #define STAT_REG	0x04		/* status register */
101 #define DEST_ID		0x04		/* selection/reselection bus id */
102 #define INT_REG		0x05		/* interrupt status register */
103 #define SRTIMOUT	0x05		/* select/reselect timeout reg */
104 #define SEQ_REG		0x06		/* sequence step register */
105 #define SYNCPRD		0x06		/* synchronous transfer period */
106 #define FIFO_FLAGS	0x07		/* indicates # of bytes in fifo */
107 #define SYNCOFF		0x07		/* synchronous offset register */
108 #define CONFIG1		0x08		/* configuration register */
109 #define CLKCONV		0x09		/* clock conversion register */
110 /* #define TESTREG	0x0A */		/* test mode register */
111 #define CONFIG2		0x0B		/* configuration 2 register */
112 #define CONFIG3		0x0C		/* configuration 3 register */
113 #define CONFIG4		0x0D		/* configuration 4 register */
114 #define TC_HIGH		0x0E		/* transfer counter high */
115 /* #define FIFO_BOTTOM	0x0F */		/* reserve FIFO byte register */
116 
117 /* Control Register Set 1 */
118 /* #define JUMPER_SENSE	0x00 */		/* jumper sense port reg (r/w) */
119 /* #define SRAM_PTR	0x01 */		/* SRAM address pointer reg (r/w) */
120 /* #define SRAM_DATA	0x02 */		/* SRAM data register (r/w) */
121 #define PIO_FIFO	0x04		/* PIO FIFO registers (r/w) */
122 /* #define PIO_FIFO1	0x05 */		/*  */
123 /* #define PIO_FIFO2	0x06 */		/*  */
124 /* #define PIO_FIFO3	0x07 */		/*  */
125 #define PIO_STATUS	0x08		/* PIO status (r/w) */
126 /* #define ATA_CMD	0x09 */		/* ATA command/status reg (r/w) */
127 /* #define ATA_ERR	0x0A */		/* ATA features/error reg (r/w) */
128 #define PIO_FLAG	0x0B		/* PIO flag interrupt enable (r/w) */
129 #define CONFIG5		0x09		/* configuration 5 register */
130 /* #define SIGNATURE	0x0E */		/* signature register (r) */
131 /* #define CONFIG6	0x0F */		/* configuration 6 register (r) */
132 #define CONFIG7		0x0d
133 
134 /* select register set 0 */
135 #define REG0(x)		(outb(C4_IMG, (x) + CONFIG4))
136 /* select register set 1 */
137 #define REG1(x)		outb(C7_IMG, (x) + CONFIG7); outb(C5_IMG, (x) + CONFIG5)
138 
139 #if SYM53C500_DEBUG
140 #define DEB(x) x
141 #else
142 #define DEB(x)
143 #endif
144 
145 #if VERBOSE_SYM53C500_DEBUG
146 #define VDEB(x) x
147 #else
148 #define VDEB(x)
149 #endif
150 
151 #define LOAD_DMA_COUNT(x, count) \
152   outb(count & 0xff, (x) + TC_LSB); \
153   outb((count >> 8) & 0xff, (x) + TC_MSB); \
154   outb((count >> 16) & 0xff, (x) + TC_HIGH);
155 
156 /* Chip commands */
157 #define DMA_OP               0x80
158 
159 #define SCSI_NOP             0x00
160 #define FLUSH_FIFO           0x01
161 #define CHIP_RESET           0x02
162 #define SCSI_RESET           0x03
163 #define RESELECT             0x40
164 #define SELECT_NO_ATN        0x41
165 #define SELECT_ATN           0x42
166 #define SELECT_ATN_STOP      0x43
167 #define ENABLE_SEL           0x44
168 #define DISABLE_SEL          0x45
169 #define SELECT_ATN3          0x46
170 #define RESELECT3            0x47
171 #define TRANSFER_INFO        0x10
172 #define INIT_CMD_COMPLETE    0x11
173 #define MSG_ACCEPT           0x12
174 #define TRANSFER_PAD         0x18
175 #define SET_ATN              0x1a
176 #define RESET_ATN            0x1b
177 #define SEND_MSG             0x20
178 #define SEND_STATUS          0x21
179 #define SEND_DATA            0x22
180 #define DISCONN_SEQ          0x23
181 #define TERMINATE_SEQ        0x24
182 #define TARG_CMD_COMPLETE    0x25
183 #define DISCONN              0x27
184 #define RECV_MSG             0x28
185 #define RECV_CMD             0x29
186 #define RECV_DATA            0x2a
187 #define RECV_CMD_SEQ         0x2b
188 #define TARGET_ABORT_DMA     0x04
189 
190 /* ================================================================== */
191 
192 struct scsi_info_t {
193 	struct pcmcia_device	*p_dev;
194 	dev_node_t node;
195 	struct Scsi_Host *host;
196 	unsigned short manf_id;
197 };
198 
199 /*
200 *  Repository for per-instance host data.
201 */
202 struct sym53c500_data {
203 	struct scsi_cmnd *current_SC;
204 	int fast_pio;
205 };
206 
207 enum Phase {
208     idle,
209     data_out,
210     data_in,
211     command_ph,
212     status_ph,
213     message_out,
214     message_in
215 };
216 
217 /* ================================================================== */
218 
219 static void
220 chip_init(int io_port)
221 {
222 	REG1(io_port);
223 	outb(0x01, io_port + PIO_STATUS);
224 	outb(0x00, io_port + PIO_FLAG);
225 
226 	outb(C4_IMG, io_port + CONFIG4);	/* REG0(io_port); */
227 	outb(C3_IMG, io_port + CONFIG3);
228 	outb(C2_IMG, io_port + CONFIG2);
229 	outb(C1_IMG, io_port + CONFIG1);
230 
231 	outb(0x05, io_port + CLKCONV);	/* clock conversion factor */
232 	outb(0x9C, io_port + SRTIMOUT);	/* Selection timeout */
233 	outb(0x05, io_port + SYNCPRD);	/* Synchronous transfer period */
234 	outb(SYNC_MODE, io_port + SYNCOFF);	/* synchronous mode */
235 }
236 
237 static void
238 SYM53C500_int_host_reset(int io_port)
239 {
240 	outb(C4_IMG, io_port + CONFIG4);	/* REG0(io_port); */
241 	outb(CHIP_RESET, io_port + CMD_REG);
242 	outb(SCSI_NOP, io_port + CMD_REG);	/* required after reset */
243 	outb(SCSI_RESET, io_port + CMD_REG);
244 	chip_init(io_port);
245 }
246 
247 static __inline__ int
248 SYM53C500_pio_read(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
249 {
250 	int i;
251 	int len;	/* current scsi fifo size */
252 
253 	REG1(base);
254 	while (reqlen) {
255 		i = inb(base + PIO_STATUS);
256 		/* VDEB(printk("pio_status=%x\n", i)); */
257 		if (i & 0x80)
258 			return 0;
259 
260 		switch (i & 0x1e) {
261 		default:
262 		case 0x10:	/* fifo empty */
263 			len = 0;
264 			break;
265 		case 0x0:
266 			len = 1;
267 			break;
268 		case 0x8:	/* fifo 1/3 full */
269 			len = 42;
270 			break;
271 		case 0xc:	/* fifo 2/3 full */
272 			len = 84;
273 			break;
274 		case 0xe:	/* fifo full */
275 			len = 128;
276 			break;
277 		}
278 
279 		if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
280 			return 0;
281 		}
282 
283 		if (len) {
284 			if (len > reqlen)
285 				len = reqlen;
286 
287 			if (fast_pio && len > 3) {
288 				insl(base + PIO_FIFO, request, len >> 2);
289 				request += len & 0xfc;
290 				reqlen -= len & 0xfc;
291 			} else {
292 				while (len--) {
293 					*request++ = inb(base + PIO_FIFO);
294 					reqlen--;
295 				}
296 			}
297 		}
298 	}
299 	return 0;
300 }
301 
302 static __inline__ int
303 SYM53C500_pio_write(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
304 {
305 	int i = 0;
306 	int len;	/* current scsi fifo size */
307 
308 	REG1(base);
309 	while (reqlen && !(i & 0x40)) {
310 		i = inb(base + PIO_STATUS);
311 		/* VDEB(printk("pio_status=%x\n", i)); */
312 		if (i & 0x80)	/* error */
313 			return 0;
314 
315 		switch (i & 0x1e) {
316 		case 0x10:
317 			len = 128;
318 			break;
319 		case 0x0:
320 			len = 84;
321 			break;
322 		case 0x8:
323 			len = 42;
324 			break;
325 		case 0xc:
326 			len = 1;
327 			break;
328 		default:
329 		case 0xe:
330 			len = 0;
331 			break;
332 		}
333 
334 		if (len) {
335 			if (len > reqlen)
336 				len = reqlen;
337 
338 			if (fast_pio && len > 3) {
339 				outsl(base + PIO_FIFO, request, len >> 2);
340 				request += len & 0xfc;
341 				reqlen -= len & 0xfc;
342 			} else {
343 				while (len--) {
344 					outb(*request++, base + PIO_FIFO);
345 					reqlen--;
346 				}
347 			}
348 		}
349 	}
350 	return 0;
351 }
352 
353 static irqreturn_t
354 SYM53C500_intr(int irq, void *dev_id)
355 {
356 	unsigned long flags;
357 	struct Scsi_Host *dev = dev_id;
358 	DEB(unsigned char fifo_size;)
359 	DEB(unsigned char seq_reg;)
360 	unsigned char status, int_reg;
361 	unsigned char pio_status;
362 	int port_base = dev->io_port;
363 	struct sym53c500_data *data =
364 	    (struct sym53c500_data *)dev->hostdata;
365 	struct scsi_cmnd *curSC = data->current_SC;
366 	int fast_pio = data->fast_pio;
367 
368 	spin_lock_irqsave(dev->host_lock, flags);
369 
370 	VDEB(printk("SYM53C500_intr called\n"));
371 
372 	REG1(port_base);
373 	pio_status = inb(port_base + PIO_STATUS);
374 	REG0(port_base);
375 	status = inb(port_base + STAT_REG);
376 	DEB(seq_reg = inb(port_base + SEQ_REG));
377 	int_reg = inb(port_base + INT_REG);
378 	DEB(fifo_size = inb(port_base + FIFO_FLAGS) & 0x1f);
379 
380 #if SYM53C500_DEBUG
381 	printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x",
382 	    status, seq_reg, int_reg, fifo_size);
383 	printk(", pio=%02x\n", pio_status);
384 #endif /* SYM53C500_DEBUG */
385 
386 	if (int_reg & 0x80) {	/* SCSI reset intr */
387 		DEB(printk("SYM53C500: reset intr received\n"));
388 		curSC->result = DID_RESET << 16;
389 		goto idle_out;
390 	}
391 
392 	if (pio_status & 0x80) {
393 		printk("SYM53C500: Warning: PIO error!\n");
394 		curSC->result = DID_ERROR << 16;
395 		goto idle_out;
396 	}
397 
398 	if (status & 0x20) {		/* Parity error */
399 		printk("SYM53C500: Warning: parity error!\n");
400 		curSC->result = DID_PARITY << 16;
401 		goto idle_out;
402 	}
403 
404 	if (status & 0x40) {		/* Gross error */
405 		printk("SYM53C500: Warning: gross error!\n");
406 		curSC->result = DID_ERROR << 16;
407 		goto idle_out;
408 	}
409 
410 	if (int_reg & 0x20) {		/* Disconnect */
411 		DEB(printk("SYM53C500: disconnect intr received\n"));
412 		if (curSC->SCp.phase != message_in) {	/* Unexpected disconnect */
413 			curSC->result = DID_NO_CONNECT << 16;
414 		} else {	/* Command complete, return status and message */
415 			curSC->result = (curSC->SCp.Status & 0xff)
416 			    | ((curSC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
417 		}
418 		goto idle_out;
419 	}
420 
421 	switch (status & 0x07) {	/* scsi phase */
422 	case 0x00:			/* DATA-OUT */
423 		if (int_reg & 0x10) {	/* Target requesting info transfer */
424 			struct scatterlist *sg;
425 			int i;
426 
427 			curSC->SCp.phase = data_out;
428 			VDEB(printk("SYM53C500: Data-Out phase\n"));
429 			outb(FLUSH_FIFO, port_base + CMD_REG);
430 			LOAD_DMA_COUNT(port_base, scsi_bufflen(curSC));	/* Max transfer size */
431 			outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
432 
433 			scsi_for_each_sg(curSC, sg, scsi_sg_count(curSC), i) {
434 				SYM53C500_pio_write(fast_pio, port_base,
435 				    sg_virt(sg), sg->length);
436 			}
437 			REG0(port_base);
438 		}
439 		break;
440 
441 	case 0x01:		/* DATA-IN */
442 		if (int_reg & 0x10) {	/* Target requesting info transfer */
443 			struct scatterlist *sg;
444 			int i;
445 
446 			curSC->SCp.phase = data_in;
447 			VDEB(printk("SYM53C500: Data-In phase\n"));
448 			outb(FLUSH_FIFO, port_base + CMD_REG);
449 			LOAD_DMA_COUNT(port_base, scsi_bufflen(curSC));	/* Max transfer size */
450 			outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
451 
452 			scsi_for_each_sg(curSC, sg, scsi_sg_count(curSC), i) {
453 				SYM53C500_pio_read(fast_pio, port_base,
454 					sg_virt(sg), sg->length);
455 			}
456 			REG0(port_base);
457 		}
458 		break;
459 
460 	case 0x02:		/* COMMAND */
461 		curSC->SCp.phase = command_ph;
462 		printk("SYM53C500: Warning: Unknown interrupt occurred in command phase!\n");
463 		break;
464 
465 	case 0x03:		/* STATUS */
466 		curSC->SCp.phase = status_ph;
467 		VDEB(printk("SYM53C500: Status phase\n"));
468 		outb(FLUSH_FIFO, port_base + CMD_REG);
469 		outb(INIT_CMD_COMPLETE, port_base + CMD_REG);
470 		break;
471 
472 	case 0x04:		/* Reserved */
473 	case 0x05:		/* Reserved */
474 		printk("SYM53C500: WARNING: Reserved phase!!!\n");
475 		break;
476 
477 	case 0x06:		/* MESSAGE-OUT */
478 		DEB(printk("SYM53C500: Message-Out phase\n"));
479 		curSC->SCp.phase = message_out;
480 		outb(SET_ATN, port_base + CMD_REG);	/* Reject the message */
481 		outb(MSG_ACCEPT, port_base + CMD_REG);
482 		break;
483 
484 	case 0x07:		/* MESSAGE-IN */
485 		VDEB(printk("SYM53C500: Message-In phase\n"));
486 		curSC->SCp.phase = message_in;
487 
488 		curSC->SCp.Status = inb(port_base + SCSI_FIFO);
489 		curSC->SCp.Message = inb(port_base + SCSI_FIFO);
490 
491 		VDEB(printk("SCSI FIFO size=%d\n", inb(port_base + FIFO_FLAGS) & 0x1f));
492 		DEB(printk("Status = %02x  Message = %02x\n", curSC->SCp.Status, curSC->SCp.Message));
493 
494 		if (curSC->SCp.Message == SAVE_POINTERS || curSC->SCp.Message == DISCONNECT) {
495 			outb(SET_ATN, port_base + CMD_REG);	/* Reject message */
496 			DEB(printk("Discarding SAVE_POINTERS message\n"));
497 		}
498 		outb(MSG_ACCEPT, port_base + CMD_REG);
499 		break;
500 	}
501 out:
502 	spin_unlock_irqrestore(dev->host_lock, flags);
503 	return IRQ_HANDLED;
504 
505 idle_out:
506 	curSC->SCp.phase = idle;
507 	curSC->scsi_done(curSC);
508 	goto out;
509 }
510 
511 static void
512 SYM53C500_release(struct pcmcia_device *link)
513 {
514 	struct scsi_info_t *info = link->priv;
515 	struct Scsi_Host *shost = info->host;
516 
517 	dev_dbg(&link->dev, "SYM53C500_release\n");
518 
519 	/*
520 	*  Do this before releasing/freeing resources.
521 	*/
522 	scsi_remove_host(shost);
523 
524 	/*
525 	*  Interrupts getting hosed on card removal.  Try
526 	*  the following code, mostly from qlogicfas.c.
527 	*/
528 	if (shost->irq)
529 		free_irq(shost->irq, shost);
530 	if (shost->io_port && shost->n_io_port)
531 		release_region(shost->io_port, shost->n_io_port);
532 
533 	pcmcia_disable_device(link);
534 
535 	scsi_host_put(shost);
536 } /* SYM53C500_release */
537 
538 static const char*
539 SYM53C500_info(struct Scsi_Host *SChost)
540 {
541 	static char info_msg[256];
542 	struct sym53c500_data *data =
543 	    (struct sym53c500_data *)SChost->hostdata;
544 
545 	DEB(printk("SYM53C500_info called\n"));
546 	(void)snprintf(info_msg, sizeof(info_msg),
547 	    "SYM53C500 at 0x%lx, IRQ %d, %s PIO mode.",
548 	    SChost->io_port, SChost->irq, data->fast_pio ? "fast" : "slow");
549 	return (info_msg);
550 }
551 
552 static int
553 SYM53C500_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
554 {
555 	int i;
556 	int port_base = SCpnt->device->host->io_port;
557 	struct sym53c500_data *data =
558 	    (struct sym53c500_data *)SCpnt->device->host->hostdata;
559 
560 	VDEB(printk("SYM53C500_queue called\n"));
561 
562 	DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n",
563 	    SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->device->id,
564 	    SCpnt->device->lun,  scsi_bufflen(SCpnt)));
565 
566 	VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
567 	    printk("cmd[%d]=%02x  ", i, SCpnt->cmnd[i]));
568 	VDEB(printk("\n"));
569 
570 	data->current_SC = SCpnt;
571 	data->current_SC->scsi_done = done;
572 	data->current_SC->SCp.phase = command_ph;
573 	data->current_SC->SCp.Status = 0;
574 	data->current_SC->SCp.Message = 0;
575 
576 	/* We are locked here already by the mid layer */
577 	REG0(port_base);
578 	outb(scmd_id(SCpnt), port_base + DEST_ID);	/* set destination */
579 	outb(FLUSH_FIFO, port_base + CMD_REG);	/* reset the fifos */
580 
581 	for (i = 0; i < SCpnt->cmd_len; i++) {
582 		outb(SCpnt->cmnd[i], port_base + SCSI_FIFO);
583 	}
584 	outb(SELECT_NO_ATN, port_base + CMD_REG);
585 
586 	return 0;
587 }
588 
589 static int
590 SYM53C500_host_reset(struct scsi_cmnd *SCpnt)
591 {
592 	int port_base = SCpnt->device->host->io_port;
593 
594 	DEB(printk("SYM53C500_host_reset called\n"));
595 	spin_lock_irq(SCpnt->device->host->host_lock);
596 	SYM53C500_int_host_reset(port_base);
597 	spin_unlock_irq(SCpnt->device->host->host_lock);
598 
599 	return SUCCESS;
600 }
601 
602 static int
603 SYM53C500_biosparm(struct scsi_device *disk,
604     struct block_device *dev,
605     sector_t capacity, int *info_array)
606 {
607 	int size;
608 
609 	DEB(printk("SYM53C500_biosparm called\n"));
610 
611 	size = capacity;
612 	info_array[0] = 64;		/* heads */
613 	info_array[1] = 32;		/* sectors */
614 	info_array[2] = size >> 11;	/* cylinders */
615 	if (info_array[2] > 1024) {	/* big disk */
616 		info_array[0] = 255;
617 		info_array[1] = 63;
618 		info_array[2] = size / (255 * 63);
619 	}
620 	return 0;
621 }
622 
623 static ssize_t
624 SYM53C500_show_pio(struct device *dev, struct device_attribute *attr,
625 		   char *buf)
626 {
627 	struct Scsi_Host *SHp = class_to_shost(dev);
628 	struct sym53c500_data *data =
629 	    (struct sym53c500_data *)SHp->hostdata;
630 
631 	return snprintf(buf, 4, "%d\n", data->fast_pio);
632 }
633 
634 static ssize_t
635 SYM53C500_store_pio(struct device *dev, struct device_attribute *attr,
636 		    const char *buf, size_t count)
637 {
638 	int pio;
639 	struct Scsi_Host *SHp = class_to_shost(dev);
640 	struct sym53c500_data *data =
641 	    (struct sym53c500_data *)SHp->hostdata;
642 
643 	pio = simple_strtoul(buf, NULL, 0);
644 	if (pio == 0 || pio == 1) {
645 		data->fast_pio = pio;
646 		return count;
647 	}
648 	else
649 		return -EINVAL;
650 }
651 
652 /*
653 *  SCSI HBA device attributes we want to
654 *  make available via sysfs.
655 */
656 static struct device_attribute SYM53C500_pio_attr = {
657 	.attr = {
658 		.name = "fast_pio",
659 		.mode = (S_IRUGO | S_IWUSR),
660 	},
661 	.show = SYM53C500_show_pio,
662 	.store = SYM53C500_store_pio,
663 };
664 
665 static struct device_attribute *SYM53C500_shost_attrs[] = {
666 	&SYM53C500_pio_attr,
667 	NULL,
668 };
669 
670 /*
671 *  scsi_host_template initializer
672 */
673 static struct scsi_host_template sym53c500_driver_template = {
674      .module			= THIS_MODULE,
675      .name			= "SYM53C500",
676      .info			= SYM53C500_info,
677      .queuecommand		= SYM53C500_queue,
678      .eh_host_reset_handler	= SYM53C500_host_reset,
679      .bios_param		= SYM53C500_biosparm,
680      .proc_name			= "SYM53C500",
681      .can_queue			= 1,
682      .this_id			= 7,
683      .sg_tablesize		= 32,
684      .cmd_per_lun		= 1,
685      .use_clustering		= ENABLE_CLUSTERING,
686      .shost_attrs		= SYM53C500_shost_attrs
687 };
688 
689 static int SYM53C500_config_check(struct pcmcia_device *p_dev,
690 				  cistpl_cftable_entry_t *cfg,
691 				  cistpl_cftable_entry_t *dflt,
692 				  unsigned int vcc,
693 				  void *priv_data)
694 {
695 	p_dev->io.BasePort1 = cfg->io.win[0].base;
696 	p_dev->io.NumPorts1 = cfg->io.win[0].len;
697 
698 	if (p_dev->io.BasePort1 == 0)
699 		return -ENODEV;
700 
701 	return pcmcia_request_io(p_dev, &p_dev->io);
702 }
703 
704 static int
705 SYM53C500_config(struct pcmcia_device *link)
706 {
707 	struct scsi_info_t *info = link->priv;
708 	int ret;
709 	int irq_level, port_base;
710 	struct Scsi_Host *host;
711 	struct scsi_host_template *tpnt = &sym53c500_driver_template;
712 	struct sym53c500_data *data;
713 
714 	dev_dbg(&link->dev, "SYM53C500_config\n");
715 
716 	info->manf_id = link->manf_id;
717 
718 	ret = pcmcia_loop_config(link, SYM53C500_config_check, NULL);
719 	if (ret)
720 		goto failed;
721 
722 	ret = pcmcia_request_irq(link, &link->irq);
723 	if (ret)
724 		goto failed;
725 
726 	ret = pcmcia_request_configuration(link, &link->conf);
727 	if (ret)
728 		goto failed;
729 
730 	/*
731 	*  That's the trouble with copying liberally from another driver.
732 	*  Some things probably aren't relevant, and I suspect this entire
733 	*  section dealing with manufacturer IDs can be scrapped.	--rct
734 	*/
735 	if ((info->manf_id == MANFID_MACNICA) ||
736 	    (info->manf_id == MANFID_PIONEER) ||
737 	    (info->manf_id == 0x0098)) {
738 		/* set ATAcmd */
739 		outb(0xb4, link->io.BasePort1 + 0xd);
740 		outb(0x24, link->io.BasePort1 + 0x9);
741 		outb(0x04, link->io.BasePort1 + 0xd);
742 	}
743 
744 	/*
745 	*  irq_level == 0 implies tpnt->can_queue == 0, which
746 	*  is not supported in 2.6.  Thus, only irq_level > 0
747 	*  will be allowed.
748 	*
749 	*  Possible port_base values are as follows:
750 	*
751 	*	0x130, 0x230, 0x280, 0x290,
752 	*	0x320, 0x330, 0x340, 0x350
753 	*/
754 	port_base = link->io.BasePort1;
755 	irq_level = link->irq.AssignedIRQ;
756 
757 	DEB(printk("SYM53C500: port_base=0x%x, irq=%d, fast_pio=%d\n",
758 	    port_base, irq_level, USE_FAST_PIO);)
759 
760 	chip_init(port_base);
761 
762 	host = scsi_host_alloc(tpnt, sizeof(struct sym53c500_data));
763 	if (!host) {
764 		printk("SYM53C500: Unable to register host, giving up.\n");
765 		goto err_release;
766 	}
767 
768 	data = (struct sym53c500_data *)host->hostdata;
769 
770 	if (irq_level > 0) {
771 		if (request_irq(irq_level, SYM53C500_intr, IRQF_SHARED, "SYM53C500", host)) {
772 			printk("SYM53C500: unable to allocate IRQ %d\n", irq_level);
773 			goto err_free_scsi;
774 		}
775 		DEB(printk("SYM53C500: allocated IRQ %d\n", irq_level));
776 	} else if (irq_level == 0) {
777 		DEB(printk("SYM53C500: No interrupts detected\n"));
778 		goto err_free_scsi;
779 	} else {
780 		DEB(printk("SYM53C500: Shouldn't get here!\n"));
781 		goto err_free_scsi;
782 	}
783 
784 	host->unique_id = port_base;
785 	host->irq = irq_level;
786 	host->io_port = port_base;
787 	host->n_io_port = 0x10;
788 	host->dma_channel = -1;
789 
790 	/*
791 	*  Note fast_pio is set to USE_FAST_PIO by
792 	*  default, but can be changed via "sysfs".
793 	*/
794 	data->fast_pio = USE_FAST_PIO;
795 
796 	sprintf(info->node.dev_name, "scsi%d", host->host_no);
797 	link->dev_node = &info->node;
798 	info->host = host;
799 
800 	if (scsi_add_host(host, NULL))
801 		goto err_free_irq;
802 
803 	scsi_scan_host(host);
804 
805 	return 0;
806 
807 err_free_irq:
808 	free_irq(irq_level, host);
809 err_free_scsi:
810 	scsi_host_put(host);
811 err_release:
812 	release_region(port_base, 0x10);
813 	printk(KERN_INFO "sym53c500_cs: no SCSI devices found\n");
814 	return -ENODEV;
815 
816 failed:
817 	SYM53C500_release(link);
818 	return -ENODEV;
819 } /* SYM53C500_config */
820 
821 static int sym53c500_resume(struct pcmcia_device *link)
822 {
823 	struct scsi_info_t *info = link->priv;
824 
825 	/* See earlier comment about manufacturer IDs. */
826 	if ((info->manf_id == MANFID_MACNICA) ||
827 	    (info->manf_id == MANFID_PIONEER) ||
828 	    (info->manf_id == 0x0098)) {
829 		outb(0x80, link->io.BasePort1 + 0xd);
830 		outb(0x24, link->io.BasePort1 + 0x9);
831 		outb(0x04, link->io.BasePort1 + 0xd);
832 	}
833 	/*
834 	 *  If things don't work after a "resume",
835 	 *  this is a good place to start looking.
836 	 */
837 	SYM53C500_int_host_reset(link->io.BasePort1);
838 
839 	return 0;
840 }
841 
842 static void
843 SYM53C500_detach(struct pcmcia_device *link)
844 {
845 	dev_dbg(&link->dev, "SYM53C500_detach\n");
846 
847 	SYM53C500_release(link);
848 
849 	kfree(link->priv);
850 	link->priv = NULL;
851 } /* SYM53C500_detach */
852 
853 static int
854 SYM53C500_probe(struct pcmcia_device *link)
855 {
856 	struct scsi_info_t *info;
857 
858 	dev_dbg(&link->dev, "SYM53C500_attach()\n");
859 
860 	/* Create new SCSI device */
861 	info = kzalloc(sizeof(*info), GFP_KERNEL);
862 	if (!info)
863 		return -ENOMEM;
864 	info->p_dev = link;
865 	link->priv = info;
866 	link->io.NumPorts1 = 16;
867 	link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
868 	link->io.IOAddrLines = 10;
869 	link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
870 	link->conf.Attributes = CONF_ENABLE_IRQ;
871 	link->conf.IntType = INT_MEMORY_AND_IO;
872 
873 	return SYM53C500_config(link);
874 } /* SYM53C500_attach */
875 
876 MODULE_AUTHOR("Bob Tracy <rct@frus.com>");
877 MODULE_DESCRIPTION("SYM53C500 PCMCIA SCSI driver");
878 MODULE_LICENSE("GPL");
879 
880 static struct pcmcia_device_id sym53c500_ids[] = {
881 	PCMCIA_DEVICE_PROD_ID12("BASICS by New Media Corporation", "SCSI Sym53C500", 0x23c78a9d, 0x0099e7f7),
882 	PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "SCSI Bus Toaster Sym53C500", 0x085a850b, 0x45432eb8),
883 	PCMCIA_DEVICE_PROD_ID2("SCSI9000", 0x21648f44),
884 	PCMCIA_DEVICE_NULL,
885 };
886 MODULE_DEVICE_TABLE(pcmcia, sym53c500_ids);
887 
888 static struct pcmcia_driver sym53c500_cs_driver = {
889 	.owner		= THIS_MODULE,
890 	.drv		= {
891 		.name	= "sym53c500_cs",
892 	},
893 	.probe		= SYM53C500_probe,
894 	.remove		= SYM53C500_detach,
895 	.id_table       = sym53c500_ids,
896 	.resume		= sym53c500_resume,
897 };
898 
899 static int __init
900 init_sym53c500_cs(void)
901 {
902 	return pcmcia_register_driver(&sym53c500_cs_driver);
903 }
904 
905 static void __exit
906 exit_sym53c500_cs(void)
907 {
908 	pcmcia_unregister_driver(&sym53c500_cs_driver);
909 }
910 
911 module_init(init_sym53c500_cs);
912 module_exit(exit_sym53c500_cs);
913