OSDN Git Service

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