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