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