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