OSDN Git Service

214b9f488869b1387f057739b27824c0c18223d1
[uclinux-h8/linux.git] / drivers / scsi / aha1542.c
1 /*
2  *  Driver for Adaptec AHA-1542 SCSI host adapters
3  *
4  *  Copyright (C) 1992  Tommy Thorn
5  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
6  *  Copyright (C) 2015 Ondrej Zary
7  */
8
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/ioport.h>
15 #include <linux/delay.h>
16 #include <linux/proc_fs.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/isa.h>
20 #include <linux/pnp.h>
21 #include <linux/blkdev.h>
22 #include <linux/slab.h>
23
24 #include <asm/dma.h>
25 #include <asm/io.h>
26
27 #include "scsi.h"
28 #include <scsi/scsi_host.h>
29 #include "aha1542.h"
30 #include <linux/stat.h>
31
32 #ifdef DEBUG
33 #define DEB(x) x
34 #else
35 #define DEB(x)
36 #endif
37 #define MAXBOARDS 4
38
39 static bool isapnp = 1;
40 module_param(isapnp, bool, 0);
41 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
42
43 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
44 module_param_array(io, int, NULL, 0);
45 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
46
47 /* time AHA spends on the AT-bus during data transfer */
48 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
49 module_param_array(bus_on, int, NULL, 0);
50 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
51
52 /* time AHA spends off the bus (not to monopolize it) during data transfer  */
53 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
54 module_param_array(bus_off, int, NULL, 0);
55 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
56
57 /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
58 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
59 module_param_array(dma_speed, int, NULL, 0);
60 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
61
62 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
63 #define BIOS_TRANSLATION_25563 2        /* Big disk case */
64
65 struct aha1542_hostdata {
66         /* This will effectively start both of them at the first mailbox */
67         int bios_translation;   /* Mapping bios uses - for compatibility */
68         int aha1542_last_mbi_used;
69         int aha1542_last_mbo_used;
70         struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
71         struct mailbox mb[2 * AHA1542_MAILBOXES];
72         struct ccb ccb[AHA1542_MAILBOXES];
73 };
74
75 static DEFINE_SPINLOCK(aha1542_lock);
76
77 static inline void aha1542_intr_reset(u16 base)
78 {
79         outb(IRST, CONTROL(base));
80 }
81
82 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
83 {
84         bool delayed = true;
85
86         if (timeout == 0) {
87                 timeout = 3000000;
88                 delayed = false;
89         }
90
91         while (1) {
92                 u8 bits = inb(port) & mask;
93                 if ((bits & allof) == allof && ((bits & noneof) == 0))
94                         break;
95                 if (delayed)
96                         mdelay(1);
97                 if (--timeout == 0)
98                         return false;
99         }
100
101         return true;
102 }
103
104 /* This is a bit complicated, but we need to make sure that an interrupt
105    routine does not send something out while we are in the middle of this.
106    Fortunately, it is only at boot time that multi-byte messages
107    are ever sent. */
108 static int aha1542_outb(unsigned int base, u8 val)
109 {
110         unsigned long flags;
111
112         while (1) {
113                 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
114                         printk(KERN_ERR "aha1542_outb failed");
115                         return 1;
116                 }
117                 spin_lock_irqsave(&aha1542_lock, flags);
118                 if (inb(STATUS(base)) & CDF) {
119                         spin_unlock_irqrestore(&aha1542_lock, flags);
120                         continue;
121                 }
122                 outb(val, DATA(base));
123                 spin_unlock_irqrestore(&aha1542_lock, flags);
124                 return 0;
125         }
126 }
127
128 static int aha1542_out(unsigned int base, u8 *buf, int len)
129 {
130         unsigned long flags;
131
132         spin_lock_irqsave(&aha1542_lock, flags);
133         while (len--) {
134                 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
135                         spin_unlock_irqrestore(&aha1542_lock, flags);
136                         printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
137                         return 1;
138                 }
139                 outb(*buf++, DATA(base));
140         }
141         spin_unlock_irqrestore(&aha1542_lock, flags);
142         if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
143                 return 1;
144
145         return 0;
146 }
147
148 /* Only used at boot time, so we do not need to worry about latency as much
149    here */
150
151 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
152 {
153         unsigned long flags;
154
155         spin_lock_irqsave(&aha1542_lock, flags);
156         while (len--) {
157                 if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
158                         spin_unlock_irqrestore(&aha1542_lock, flags);
159                         if (timeout == 0)
160                                 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
161                         return 1;
162                 }
163                 *buf++ = inb(DATA(base));
164         }
165         spin_unlock_irqrestore(&aha1542_lock, flags);
166         return 0;
167 }
168
169 static int makecode(unsigned hosterr, unsigned scsierr)
170 {
171         switch (hosterr) {
172         case 0x0:
173         case 0xa:               /* Linked command complete without error and linked normally */
174         case 0xb:               /* Linked command complete without error, interrupt generated */
175                 hosterr = 0;
176                 break;
177
178         case 0x11:              /* Selection time out-The initiator selection or target
179                                    reselection was not complete within the SCSI Time out period */
180                 hosterr = DID_TIME_OUT;
181                 break;
182
183         case 0x12:              /* Data overrun/underrun-The target attempted to transfer more data
184                                    than was allocated by the Data Length field or the sum of the
185                                    Scatter / Gather Data Length fields. */
186
187         case 0x13:              /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
188
189         case 0x15:              /* MBO command was not 00, 01 or 02-The first byte of the CB was
190                                    invalid. This usually indicates a software failure. */
191
192         case 0x16:              /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
193                                    This usually indicates a software failure. */
194
195         case 0x17:              /* Linked CCB does not have the same LUN-A subsequent CCB of a set
196                                    of linked CCB's does not specify the same logical unit number as
197                                    the first. */
198         case 0x18:              /* Invalid Target Direction received from Host-The direction of a
199                                    Target Mode CCB was invalid. */
200
201         case 0x19:              /* Duplicate CCB Received in Target Mode-More than once CCB was
202                                    received to service data transfer between the same target LUN
203                                    and initiator SCSI ID in the same direction. */
204
205         case 0x1a:              /* Invalid CCB or Segment List Parameter-A segment list with a zero
206                                    length segment or invalid segment list boundaries was received.
207                                    A CCB parameter was invalid. */
208                 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
209                 hosterr = DID_ERROR;    /* Couldn't find any better */
210                 break;
211
212         case 0x14:              /* Target bus phase sequence failure-An invalid bus phase or bus
213                                    phase sequence was requested by the target. The host adapter
214                                    will generate a SCSI Reset Condition, notifying the host with
215                                    a SCRD interrupt */
216                 hosterr = DID_RESET;
217                 break;
218         default:
219                 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
220                 break;
221         }
222         return scsierr | (hosterr << 16);
223 }
224
225 static int aha1542_test_port(int bse, struct Scsi_Host *sh)
226 {
227         u8 inquiry_result[4];
228         int i;
229
230         /* Quick and dirty test for presence of the card. */
231         if (inb(STATUS(bse)) == 0xff)
232                 return 0;
233
234         /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
235
236         /* In case some other card was probing here, reset interrupts */
237         aha1542_intr_reset(bse);        /* reset interrupts, so they don't block */
238
239         outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
240
241         mdelay(20);             /* Wait a little bit for things to settle down. */
242
243         /* Expect INIT and IDLE, any of the others are bad */
244         if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
245                 return 0;
246
247         /* Shouldn't have generated any interrupts during reset */
248         if (inb(INTRFLAGS(bse)) & INTRMASK)
249                 return 0;
250
251         /* Perform a host adapter inquiry instead so we do not need to set
252            up the mailboxes ahead of time */
253
254         aha1542_outb(bse, CMD_INQUIRY);
255
256         for (i = 0; i < 4; i++) {
257                 if (!wait_mask(STATUS(bse), DF, DF, 0, 0))
258                         return 0;
259                 inquiry_result[i] = inb(DATA(bse));
260         }
261
262         /* Reading port should reset DF */
263         if (inb(STATUS(bse)) & DF)
264                 return 0;
265
266         /* When HACC, command is completed, and we're though testing */
267         if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0))
268                 return 0;
269
270         /* Clear interrupts */
271         outb(IRST, CONTROL(bse));
272
273         return 1;
274 }
275
276 /* A "high" level interrupt handler */
277 static void aha1542_intr_handle(struct Scsi_Host *sh)
278 {
279         struct aha1542_hostdata *aha1542 = shost_priv(sh);
280         void (*my_done)(struct scsi_cmnd *) = NULL;
281         int errstatus, mbi, mbo, mbistatus;
282         int number_serviced;
283         unsigned long flags;
284         struct scsi_cmnd *tmp_cmd;
285         int flag;
286         struct mailbox *mb = aha1542->mb;
287         struct ccb *ccb = aha1542->ccb;
288
289 #ifdef DEBUG
290         {
291                 flag = inb(INTRFLAGS(sh->io_port));
292                 printk(KERN_DEBUG "aha1542_intr_handle: ");
293                 if (!(flag & ANYINTR))
294                         printk("no interrupt?");
295                 if (flag & MBIF)
296                         printk("MBIF ");
297                 if (flag & MBOA)
298                         printk("MBOF ");
299                 if (flag & HACC)
300                         printk("HACC ");
301                 if (flag & SCRD)
302                         printk("SCRD ");
303                 printk("status %02x\n", inb(STATUS(sh->io_port)));
304         };
305 #endif
306         number_serviced = 0;
307
308         while (1 == 1) {
309                 flag = inb(INTRFLAGS(sh->io_port));
310
311                 /* Check for unusual interrupts.  If any of these happen, we should
312                    probably do something special, but for now just printing a message
313                    is sufficient.  A SCSI reset detected is something that we really
314                    need to deal with in some way. */
315                 if (flag & ~MBIF) {
316                         if (flag & MBOA)
317                                 printk("MBOF ");
318                         if (flag & HACC)
319                                 printk("HACC ");
320                         if (flag & SCRD)
321                                 printk("SCRD ");
322                 }
323                 aha1542_intr_reset(sh->io_port);
324
325                 spin_lock_irqsave(&aha1542_lock, flags);
326                 mbi = aha1542->aha1542_last_mbi_used + 1;
327                 if (mbi >= 2 * AHA1542_MAILBOXES)
328                         mbi = AHA1542_MAILBOXES;
329
330                 do {
331                         if (mb[mbi].status != 0)
332                                 break;
333                         mbi++;
334                         if (mbi >= 2 * AHA1542_MAILBOXES)
335                                 mbi = AHA1542_MAILBOXES;
336                 } while (mbi != aha1542->aha1542_last_mbi_used);
337
338                 if (mb[mbi].status == 0) {
339                         spin_unlock_irqrestore(&aha1542_lock, flags);
340                         /* Hmm, no mail.  Must have read it the last time around */
341                         if (!number_serviced)
342                                 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
343                         return;
344                 };
345
346                 mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
347                 mbistatus = mb[mbi].status;
348                 mb[mbi].status = 0;
349                 aha1542->aha1542_last_mbi_used = mbi;
350                 spin_unlock_irqrestore(&aha1542_lock, flags);
351
352 #ifdef DEBUG
353                 {
354                         if (ccb[mbo].tarstat | ccb[mbo].hastat)
355                                 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
356                                        ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
357                 };
358 #endif
359
360                 if (mbistatus == 3)
361                         continue;       /* Aborted command not found */
362
363 #ifdef DEBUG
364                 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
365 #endif
366
367                 tmp_cmd = aha1542->int_cmds[mbo];
368
369                 if (!tmp_cmd || !tmp_cmd->scsi_done) {
370                         printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
371                         printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
372                                ccb[mbo].hastat, ccb[mbo].idlun, mbo);
373                         return;
374                 }
375                 my_done = tmp_cmd->scsi_done;
376                 kfree(tmp_cmd->host_scribble);
377                 tmp_cmd->host_scribble = NULL;
378                 /* Fetch the sense data, and tuck it away, in the required slot.  The
379                    Adaptec automatically fetches it, and there is no guarantee that
380                    we will still have it in the cdb when we come back */
381                 if (ccb[mbo].tarstat == 2)
382                         memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
383                                SCSI_SENSE_BUFFERSIZE);
384
385
386                 /* is there mail :-) */
387
388                 /* more error checking left out here */
389                 if (mbistatus != 1)
390                         /* This is surely wrong, but I don't know what's right */
391                         errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
392                 else
393                         errstatus = 0;
394
395 #ifdef DEBUG
396                 if (errstatus)
397                         printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
398                                ccb[mbo].hastat, ccb[mbo].tarstat);
399 #endif
400
401                 if (ccb[mbo].tarstat == 2) {
402 #ifdef DEBUG
403                         int i;
404 #endif
405                         DEB(printk("aha1542_intr_handle: sense:"));
406 #ifdef DEBUG
407                         for (i = 0; i < 12; i++)
408                                 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
409                         printk("\n");
410 #endif
411                         /*
412                            DEB(printk("aha1542_intr_handle: buf:"));
413                            for (i = 0; i < bufflen; i++)
414                            printk("%02x ", ((unchar *)buff)[i]);
415                            printk("\n");
416                          */
417                 }
418                 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
419                 tmp_cmd->result = errstatus;
420                 aha1542->int_cmds[mbo] = NULL;  /* This effectively frees up the mailbox slot, as
421                                                    far as queuecommand is concerned */
422                 my_done(tmp_cmd);
423                 number_serviced++;
424         };
425 }
426
427 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
428 static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
429 {
430         unsigned long flags;
431         struct Scsi_Host *sh = dev_id;
432
433         spin_lock_irqsave(sh->host_lock, flags);
434         aha1542_intr_handle(sh);
435         spin_unlock_irqrestore(sh->host_lock, flags);
436         return IRQ_HANDLED;
437 }
438
439 static int aha1542_queuecommand_lck(struct scsi_cmnd *cmd, void (*done) (struct scsi_cmnd *))
440 {
441         struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
442         u8 direction;
443         u8 target = cmd->device->id;
444         u8 lun = cmd->device->lun;
445         unsigned long flags;
446         int bufflen = scsi_bufflen(cmd);
447         int mbo;
448         struct mailbox *mb = aha1542->mb;
449         struct ccb *ccb = aha1542->ccb;
450
451         DEB(int i);
452
453         DEB(if (target > 1) {
454             cmd->result = DID_TIME_OUT << 16;
455             done(cmd); return 0;
456             }
457         );
458
459         if (*cmd->cmnd == REQUEST_SENSE) {
460                 /* Don't do the command - we have the sense data already */
461                 cmd->result = 0;
462                 done(cmd);
463                 return 0;
464         }
465 #ifdef DEBUG
466         if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
467                 i = xscsi2int(cmd->cmnd + 2);
468         else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
469                 i = scsi2int(cmd->cmnd + 2);
470         else
471                 i = -1;
472         if (done)
473                 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
474         else
475                 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
476         printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
477         for (i = 0; i < cmd->cmd_len; i++)
478                 printk("%02x ", cmd->cmnd[i]);
479         printk("\n");
480         if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
481                 return 0;       /* we are still testing, so *don't* write */
482 #endif
483         /* Use the outgoing mailboxes in a round-robin fashion, because this
484            is how the host adapter will scan for them */
485
486         spin_lock_irqsave(&aha1542_lock, flags);
487         mbo = aha1542->aha1542_last_mbo_used + 1;
488         if (mbo >= AHA1542_MAILBOXES)
489                 mbo = 0;
490
491         do {
492                 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
493                         break;
494                 mbo++;
495                 if (mbo >= AHA1542_MAILBOXES)
496                         mbo = 0;
497         } while (mbo != aha1542->aha1542_last_mbo_used);
498
499         if (mb[mbo].status || aha1542->int_cmds[mbo])
500                 panic("Unable to find empty mailbox for aha1542.\n");
501
502         aha1542->int_cmds[mbo] = cmd;   /* This will effectively prevent someone else from
503                                            screwing with this cdb. */
504
505         aha1542->aha1542_last_mbo_used = mbo;
506         spin_unlock_irqrestore(&aha1542_lock, flags);
507
508 #ifdef DEBUG
509         printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
510 #endif
511
512         any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));   /* This gets trashed for some reason */
513
514         memset(&ccb[mbo], 0, sizeof(struct ccb));
515
516         ccb[mbo].cdblen = cmd->cmd_len;
517
518         direction = 0;
519         if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
520                 direction = 8;
521         else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
522                 direction = 16;
523
524         memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
525
526         if (bufflen) {
527                 struct scatterlist *sg;
528                 struct chain *cptr;
529 #ifdef DEBUG
530                 unsigned char *ptr;
531 #endif
532                 int i, sg_count = scsi_sg_count(cmd);
533                 ccb[mbo].op = 2;        /* SCSI Initiator Command  w/scatter-gather */
534                 cmd->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
535                                                          GFP_KERNEL | GFP_DMA);
536                 cptr = (struct chain *) cmd->host_scribble;
537                 if (cptr == NULL) {
538                         /* free the claimed mailbox slot */
539                         aha1542->int_cmds[mbo] = NULL;
540                         return SCSI_MLQUEUE_HOST_BUSY;
541                 }
542                 scsi_for_each_sg(cmd, sg, sg_count, i) {
543                         any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
544                                                                 + sg->offset);
545                         any2scsi(cptr[i].datalen, sg->length);
546                 };
547                 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
548                 any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
549 #ifdef DEBUG
550                 printk("cptr %x: ", cptr);
551                 ptr = (unsigned char *) cptr;
552                 for (i = 0; i < 18; i++)
553                         printk("%02x ", ptr[i]);
554 #endif
555         } else {
556                 ccb[mbo].op = 0;        /* SCSI Initiator Command */
557                 cmd->host_scribble = NULL;
558                 any2scsi(ccb[mbo].datalen, 0);
559                 any2scsi(ccb[mbo].dataptr, 0);
560         };
561         ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);     /*SCSI Target Id */
562         ccb[mbo].rsalen = 16;
563         ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
564         ccb[mbo].commlinkid = 0;
565
566 #ifdef DEBUG
567         {
568                 int i;
569                 printk(KERN_DEBUG "aha1542_command: sending.. ");
570                 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
571                         printk("%02x ", ((u8 *) &ccb[mbo])[i]);
572         };
573 #endif
574
575         if (done) {
576                 DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
577                 cmd->scsi_done = done;
578                 mb[mbo].status = 1;
579                 aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
580         } else
581                 printk("aha1542_queuecommand: done can't be NULL\n");
582
583         return 0;
584 }
585
586 static DEF_SCSI_QCMD(aha1542_queuecommand)
587
588 /* Initialize mailboxes */
589 static void setup_mailboxes(int bse, struct Scsi_Host *sh)
590 {
591         struct aha1542_hostdata *aha1542 = shost_priv(sh);
592         int i;
593         struct mailbox *mb = aha1542->mb;
594         struct ccb *ccb = aha1542->ccb;
595
596         u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
597
598         for (i = 0; i < AHA1542_MAILBOXES; i++) {
599                 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
600                 any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
601         };
602         aha1542_intr_reset(bse);        /* reset interrupts, so they don't block */
603         any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
604         if (aha1542_out(bse, mb_cmd, 5))
605                 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
606         aha1542_intr_reset(bse);
607 }
608
609 static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id)
610 {
611         u8 inquiry_result[3];
612         int i;
613         i = inb(STATUS(base_io));
614         if (i & DF) {
615                 i = inb(DATA(base_io));
616         };
617         aha1542_outb(base_io, CMD_RETCONF);
618         aha1542_in(base_io, inquiry_result, 3, 0);
619         if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
620                 printk(KERN_ERR "aha1542_detect: query board settings\n");
621         aha1542_intr_reset(base_io);
622         switch (inquiry_result[0]) {
623         case 0x80:
624                 *dma_chan = 7;
625                 break;
626         case 0x40:
627                 *dma_chan = 6;
628                 break;
629         case 0x20:
630                 *dma_chan = 5;
631                 break;
632         case 0x01:
633                 *dma_chan = 0;
634                 break;
635         case 0:
636                 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
637                    Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
638                 *dma_chan = 0xFF;
639                 break;
640         default:
641                 printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
642                 return -1;
643         };
644         switch (inquiry_result[1]) {
645         case 0x40:
646                 *irq_level = 15;
647                 break;
648         case 0x20:
649                 *irq_level = 14;
650                 break;
651         case 0x8:
652                 *irq_level = 12;
653                 break;
654         case 0x4:
655                 *irq_level = 11;
656                 break;
657         case 0x2:
658                 *irq_level = 10;
659                 break;
660         case 0x1:
661                 *irq_level = 9;
662                 break;
663         default:
664                 printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
665                 return -1;
666         };
667         *scsi_id = inquiry_result[2] & 7;
668         return 0;
669 }
670
671 /* This function should only be called for 1542C boards - we can detect
672    the special firmware settings and unlock the board */
673
674 static int aha1542_mbenable(int base)
675 {
676         static u8 mbenable_cmd[3];
677         static u8 mbenable_result[2];
678         int retval;
679
680         retval = BIOS_TRANSLATION_6432;
681
682         aha1542_outb(base, CMD_EXTBIOS);
683         if (aha1542_in(base, mbenable_result, 2, 100))
684                 return retval;
685         if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
686                 goto fail;
687         aha1542_intr_reset(base);
688
689         if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
690                 mbenable_cmd[0] = CMD_MBENABLE;
691                 mbenable_cmd[1] = 0;
692                 mbenable_cmd[2] = mbenable_result[1];
693
694                 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
695                         retval = BIOS_TRANSLATION_25563;
696
697                 if (aha1542_out(base, mbenable_cmd, 3))
698                         goto fail;
699         };
700         while (0) {
701 fail:
702                 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
703         }
704         aha1542_intr_reset(base);
705         return retval;
706 }
707
708 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
709 static int aha1542_query(int base_io, int *transl)
710 {
711         u8 inquiry_result[4];
712         int i;
713         i = inb(STATUS(base_io));
714         if (i & DF) {
715                 i = inb(DATA(base_io));
716         };
717         aha1542_outb(base_io, CMD_INQUIRY);
718         aha1542_in(base_io, inquiry_result, 4, 0);
719         if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
720                 printk(KERN_ERR "aha1542_detect: query card type\n");
721         aha1542_intr_reset(base_io);
722
723         *transl = BIOS_TRANSLATION_6432;        /* Default case */
724
725         /* For an AHA1740 series board, we ignore the board since there is a
726            hardware bug which can lead to wrong blocks being returned if the board
727            is operating in the 1542 emulation mode.  Since there is an extended mode
728            driver, we simply ignore the board and let the 1740 driver pick it up.
729          */
730
731         if (inquiry_result[0] == 0x43) {
732                 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
733                 return 1;
734         };
735
736         /* Always call this - boards that do not support extended bios translation
737            will ignore the command, and we will set the proper default */
738
739         *transl = aha1542_mbenable(base_io);
740
741         return 0;
742 }
743
744 static u8 dma_speed_hw(int dma_speed)
745 {
746         switch (dma_speed) {
747         case 5:
748                 return 0x00;
749         case 6:
750                 return 0x04;
751         case 7:
752                 return 0x01;
753         case 8:
754                 return 0x02;
755         case 10:
756                 return 0x03;
757         }
758
759         return 0xff;    /* invalid */
760 }
761
762 /* Set the Bus on/off-times as not to ruin floppy performance */
763 static void aha1542_set_bus_times(int indx)
764 {
765         unsigned int base_io = io[indx];
766
767         if (bus_on[indx] > 0) {
768                 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on[indx], 2, 15) };
769
770                 aha1542_intr_reset(base_io);
771                 if (aha1542_out(base_io, oncmd, 2))
772                         goto fail;
773         }
774
775         if (bus_off[indx] > 0) {
776                 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off[indx], 1, 64) };
777
778                 aha1542_intr_reset(base_io);
779                 if (aha1542_out(base_io, offcmd, 2))
780                         goto fail;
781         }
782
783         if (dma_speed_hw(dma_speed[indx]) != 0xff) {
784                 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed[indx]) };
785
786                 aha1542_intr_reset(base_io);
787                 if (aha1542_out(base_io, dmacmd, 2))
788                         goto fail;
789         }
790         aha1542_intr_reset(base_io);
791         return;
792 fail:
793         printk(KERN_ERR "setting bus on/off-time failed\n");
794         aha1542_intr_reset(base_io);
795 }
796
797 /* return non-zero on detection */
798 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
799 {
800         unsigned int base_io = io[indx];
801         struct Scsi_Host *sh;
802         struct aha1542_hostdata *aha1542;
803
804         if (base_io == 0)
805                 return NULL;
806
807         if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
808                 return NULL;
809
810         sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
811         if (!sh)
812                 goto release;
813         aha1542 = shost_priv(sh);
814
815         if (!aha1542_test_port(base_io, sh))
816                 goto unregister;
817
818         aha1542_set_bus_times(indx);
819         if (aha1542_query(base_io, &aha1542->bios_translation))
820                 goto unregister;
821         if (aha1542_getconfig(base_io, &sh->irq, &sh->dma_channel, &sh->this_id) == -1)
822                 goto unregister;
823
824         printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", sh->this_id, base_io, sh->irq);
825         if (sh->dma_channel != 0xFF)
826                 printk(", DMA %d", sh->dma_channel);
827         printk("\n");
828         if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
829                 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
830
831         setup_mailboxes(base_io, sh);
832
833         if (request_irq(sh->irq, do_aha1542_intr_handle, 0,
834                                         "aha1542", sh)) {
835                 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
836                 goto unregister;
837         }
838         if (sh->dma_channel != 0xFF) {
839                 if (request_dma(sh->dma_channel, "aha1542")) {
840                         printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
841                         goto free_irq;
842                 }
843                 if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
844                         set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
845                         enable_dma(sh->dma_channel);
846                 }
847         }
848
849         sh->unique_id = base_io;
850         sh->io_port = base_io;
851         sh->n_io_port = AHA1542_REGION_SIZE;
852         aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
853         aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
854
855         if (scsi_add_host(sh, pdev))
856                 goto free_dma;
857
858         scsi_scan_host(sh);
859
860         return sh;
861 free_dma:
862         if (sh->dma_channel != 0xff)
863                 free_dma(sh->dma_channel);
864 free_irq:
865         free_irq(sh->irq, sh);
866 unregister:
867         scsi_host_put(sh);
868 release:
869         release_region(base_io, AHA1542_REGION_SIZE);
870
871         return NULL;
872 }
873
874 static int aha1542_release(struct Scsi_Host *sh)
875 {
876         scsi_remove_host(sh);
877         if (sh->dma_channel != 0xff)
878                 free_dma(sh->dma_channel);
879         if (sh->irq)
880                 free_irq(sh->irq, sh);
881         if (sh->io_port && sh->n_io_port)
882                 release_region(sh->io_port, sh->n_io_port);
883         scsi_host_put(sh);
884         return 0;
885 }
886
887
888 /*
889  * This is a device reset.  This is handled by sending a special command
890  * to the device.
891  */
892 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
893 {
894         struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
895         unsigned long flags;
896         struct mailbox *mb = aha1542->mb;
897         u8 target = cmd->device->id;
898         u8 lun = cmd->device->lun;
899         int mbo;
900         struct ccb *ccb = aha1542->ccb;
901
902         spin_lock_irqsave(&aha1542_lock, flags);
903         mbo = aha1542->aha1542_last_mbo_used + 1;
904         if (mbo >= AHA1542_MAILBOXES)
905                 mbo = 0;
906
907         do {
908                 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
909                         break;
910                 mbo++;
911                 if (mbo >= AHA1542_MAILBOXES)
912                         mbo = 0;
913         } while (mbo != aha1542->aha1542_last_mbo_used);
914
915         if (mb[mbo].status || aha1542->int_cmds[mbo])
916                 panic("Unable to find empty mailbox for aha1542.\n");
917
918         aha1542->int_cmds[mbo] = cmd;   /* This will effectively
919                                            prevent someone else from
920                                            screwing with this cdb. */
921
922         aha1542->aha1542_last_mbo_used = mbo;
923         spin_unlock_irqrestore(&aha1542_lock, flags);
924
925         any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));   /* This gets trashed for some reason */
926
927         memset(&ccb[mbo], 0, sizeof(struct ccb));
928
929         ccb[mbo].op = 0x81;     /* BUS DEVICE RESET */
930
931         ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);         /*SCSI Target Id */
932
933         ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
934         ccb[mbo].commlinkid = 0;
935
936         /* 
937          * Now tell the 1542 to flush all pending commands for this 
938          * target 
939          */
940         aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
941
942         scmd_printk(KERN_WARNING, cmd,
943                 "Trying device reset for target\n");
944
945         return SUCCESS;
946 }
947
948 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
949 {
950         struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
951         int i;
952
953         /* 
954          * This does a scsi reset for all devices on the bus.
955          * In principle, we could also reset the 1542 - should
956          * we do this?  Try this first, and we can add that later
957          * if it turns out to be useful.
958          */
959         outb(reset_cmd, CONTROL(cmd->device->host->io_port));
960
961         /*
962          * Wait for the thing to settle down a bit.  Unfortunately
963          * this is going to basically lock up the machine while we
964          * wait for this to complete.  To be 100% correct, we need to
965          * check for timeout, and if we are doing something like this
966          * we are pretty desperate anyways.
967          */
968         ssleep(4);
969         spin_lock_irq(cmd->device->host->host_lock);
970
971         if (!wait_mask(STATUS(cmd->device->host->io_port),
972              STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
973                 spin_unlock_irq(cmd->device->host->host_lock);
974                 return FAILED;
975         }
976         /*
977          * We need to do this too before the 1542 can interact with
978          * us again after host reset.
979          */
980         if (reset_cmd & HRST)
981                 setup_mailboxes(cmd->device->host->io_port, cmd->device->host);
982         /*
983          * Now try to pick up the pieces.  For all pending commands,
984          * free any internal data structures, and basically clear things
985          * out.  We do not try and restart any commands or anything - 
986          * the strategy handler takes care of that crap.
987          */
988         printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
989
990         for (i = 0; i < AHA1542_MAILBOXES; i++) {
991                 if (aha1542->int_cmds[i] != NULL) {
992                         struct scsi_cmnd *tmp_cmd;
993                         tmp_cmd = aha1542->int_cmds[i];
994
995                         if (tmp_cmd->device->soft_reset) {
996                                 /*
997                                  * If this device implements the soft reset option,
998                                  * then it is still holding onto the command, and
999                                  * may yet complete it.  In this case, we don't
1000                                  * flush the data.
1001                                  */
1002                                 continue;
1003                         }
1004                         kfree(tmp_cmd->host_scribble);
1005                         tmp_cmd->host_scribble = NULL;
1006                         aha1542->int_cmds[i] = NULL;
1007                         aha1542->mb[i].status = 0;
1008                 }
1009         }
1010
1011         spin_unlock_irq(cmd->device->host->host_lock);
1012         return SUCCESS;
1013 }
1014
1015 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
1016 {
1017         return aha1542_reset(cmd, SCRST);
1018 }
1019
1020 static int aha1542_host_reset(struct scsi_cmnd *cmd)
1021 {
1022         return aha1542_reset(cmd, HRST | SCRST);
1023 }
1024
1025 static int aha1542_biosparam(struct scsi_device *sdev,
1026                 struct block_device *bdev, sector_t capacity, int geom[])
1027 {
1028         struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1029
1030         if (capacity >= 0x200000 &&
1031                         aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1032                 /* Please verify that this is the same as what DOS returns */
1033                 geom[0] = 255;  /* heads */
1034                 geom[1] = 63;   /* sectors */
1035         } else {
1036                 geom[0] = 64;   /* heads */
1037                 geom[1] = 32;   /* sectors */
1038         }
1039         geom[2] = sector_div(capacity, geom[0] * geom[1]);      /* cylinders */
1040
1041         return 0;
1042 }
1043 MODULE_LICENSE("GPL");
1044
1045 static struct scsi_host_template driver_template = {
1046         .module                 = THIS_MODULE,
1047         .proc_name              = "aha1542",
1048         .name                   = "Adaptec 1542",
1049         .queuecommand           = aha1542_queuecommand,
1050         .eh_device_reset_handler= aha1542_dev_reset,
1051         .eh_bus_reset_handler   = aha1542_bus_reset,
1052         .eh_host_reset_handler  = aha1542_host_reset,
1053         .bios_param             = aha1542_biosparam,
1054         .can_queue              = AHA1542_MAILBOXES, 
1055         .this_id                = 7,
1056         .sg_tablesize           = 16,
1057         .cmd_per_lun            = 1,
1058         .unchecked_isa_dma      = 1, 
1059         .use_clustering         = ENABLE_CLUSTERING,
1060 };
1061
1062 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1063 {
1064         struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1065
1066         if (!sh)
1067                 return 0;
1068
1069         dev_set_drvdata(pdev, sh);
1070         return 1;
1071 }
1072
1073 static int aha1542_isa_remove(struct device *pdev,
1074                                     unsigned int ndev)
1075 {
1076         aha1542_release(dev_get_drvdata(pdev));
1077         dev_set_drvdata(pdev, NULL);
1078         return 0;
1079 }
1080
1081 static struct isa_driver aha1542_isa_driver = {
1082         .match          = aha1542_isa_match,
1083         .remove         = aha1542_isa_remove,
1084         .driver         = {
1085                 .name   = "aha1542"
1086         },
1087 };
1088 static int isa_registered;
1089
1090 #ifdef CONFIG_PNP
1091 static struct pnp_device_id aha1542_pnp_ids[] = {
1092         { .id = "ADP1542" },
1093         { .id = "" }
1094 };
1095 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1096
1097 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1098 {
1099         int indx;
1100         struct Scsi_Host *sh;
1101
1102         for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1103                 if (io[indx])
1104                         continue;
1105
1106                 if (pnp_activate_dev(pdev) < 0)
1107                         continue;
1108
1109                 io[indx] = pnp_port_start(pdev, 0);
1110
1111                 /* The card can be queried for its DMA, we have
1112                    the DMA set up that is enough */
1113
1114                 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", io[indx]);
1115         }
1116
1117         sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1118         if (!sh)
1119                 return -ENODEV;
1120
1121         pnp_set_drvdata(pdev, sh);
1122         return 0;
1123 }
1124
1125 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1126 {
1127         aha1542_release(pnp_get_drvdata(pdev));
1128         pnp_set_drvdata(pdev, NULL);
1129 }
1130
1131 static struct pnp_driver aha1542_pnp_driver = {
1132         .name           = "aha1542",
1133         .id_table       = aha1542_pnp_ids,
1134         .probe          = aha1542_pnp_probe,
1135         .remove         = aha1542_pnp_remove,
1136 };
1137 static int pnp_registered;
1138 #endif /* CONFIG_PNP */
1139
1140 static int __init aha1542_init(void)
1141 {
1142         int ret = 0;
1143
1144 #ifdef CONFIG_PNP
1145         if (isapnp) {
1146                 ret = pnp_register_driver(&aha1542_pnp_driver);
1147                 if (!ret)
1148                         pnp_registered = 1;
1149         }
1150 #endif
1151         ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1152         if (!ret)
1153                 isa_registered = 1;
1154
1155 #ifdef CONFIG_PNP
1156         if (pnp_registered)
1157                 ret = 0;
1158 #endif
1159         if (isa_registered)
1160                 ret = 0;
1161
1162         return ret;
1163 }
1164
1165 static void __exit aha1542_exit(void)
1166 {
1167 #ifdef CONFIG_PNP
1168         if (pnp_registered)
1169                 pnp_unregister_driver(&aha1542_pnp_driver);
1170 #endif
1171         if (isa_registered)
1172                 isa_unregister_driver(&aha1542_isa_driver);
1173 }
1174
1175 module_init(aha1542_init);
1176 module_exit(aha1542_exit);