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