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