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