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