[SCSI] libsas: Warn if ATA device detected but CONFIG_SCSI_SAS_ATA not set
[linux-2.6] / 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 version of AHA-1540)
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/isapnp.h>
39 #include <linux/blkdev.h>
40 #include <linux/mca.h>
41 #include <linux/mca-legacy.h>
42
43 #include <asm/dma.h>
44 #include <asm/system.h>
45 #include <asm/io.h>
46
47 #include "scsi.h"
48 #include <scsi/scsi_host.h>
49 #include "aha1542.h"
50
51 #define SCSI_BUF_PA(address)    isa_virt_to_bus(address)
52 #define SCSI_SG_PA(sgent)       (isa_page_to_bus(sg_page((sgent))) + (sgent)->offset)
53
54 static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
55                        struct scatterlist *sgp,
56                        int nseg,
57                        int badseg)
58 {
59         printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n",
60                badseg, nseg, sg_virt(sgp),
61                (unsigned long long)SCSI_SG_PA(sgp),
62                sgp->length);
63
64         /*
65          * Not safe to continue.
66          */
67         panic("Buffer at physical address > 16Mb used for aha1542");
68 }
69
70 #include<linux/stat.h>
71
72 #ifdef DEBUG
73 #define DEB(x) x
74 #else
75 #define DEB(x)
76 #endif
77
78 /*
79    static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
80  */
81
82 /* The adaptec can be configured for quite a number of addresses, but
83    I generally do not want the card poking around at random.  We allow
84    two addresses - this allows people to use the Adaptec with a Midi
85    card, which also used 0x330 -- can be overridden with LILO! */
86
87 #define MAXBOARDS 4             /* Increase this and the sizes of the
88                                    arrays below, if you need more.. */
89
90 /* Boards 3,4 slots are reserved for ISAPnP/MCA scans */
91
92 static unsigned int bases[MAXBOARDS] __initdata = {0x330, 0x334, 0, 0};
93
94 /* set by aha1542_setup according to the command line; they also may
95    be marked __initdata, but require zero initializers then */
96
97 static int setup_called[MAXBOARDS];
98 static int setup_buson[MAXBOARDS];
99 static int setup_busoff[MAXBOARDS];
100 static int setup_dmaspeed[MAXBOARDS] __initdata = { -1, -1, -1, -1 };
101
102 /*
103  * LILO/Module params:  aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
104  *
105  * Where:  <PORTBASE> is any of the valid AHA addresses:
106  *                      0x130, 0x134, 0x230, 0x234, 0x330, 0x334
107  *         <BUSON>  is the time (in microsecs) that AHA spends on the AT-bus
108  *                  when transferring data.  1542A power-on default is 11us,
109  *                  valid values are in range: 2..15 (decimal)
110  *         <BUSOFF> is the time that AHA spends OFF THE BUS after while
111  *                  it is transferring data (not to monopolize the bus).
112  *                  Power-on default is 4us, valid range: 1..64 microseconds.
113  *         <DMASPEED> Default is jumper selected (1542A: on the J1),
114  *                  but experimenter can alter it with this.
115  *                  Valid values: 5, 6, 7, 8, 10 (MB/s)
116  *                  Factory default is 5 MB/s.
117  */
118
119 #if defined(MODULE)
120 static int isapnp = 0;
121 static int aha1542[] = {0x330, 11, 4, -1};
122 module_param_array(aha1542, int, NULL, 0);
123 module_param(isapnp, bool, 0);
124
125 static struct isapnp_device_id id_table[] __initdata = {
126         {
127                 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
128                 ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1542),
129                 0
130         },
131         {0}
132 };
133
134 MODULE_DEVICE_TABLE(isapnp, id_table);
135
136 #else
137 static int isapnp = 1;
138 #endif
139
140 #define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
141 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
142 #define BIOS_TRANSLATION_25563 2        /* Big disk case */
143
144 struct aha1542_hostdata {
145         /* This will effectively start both of them at the first mailbox */
146         int bios_translation;   /* Mapping bios uses - for compatibility */
147         int aha1542_last_mbi_used;
148         int aha1542_last_mbo_used;
149         Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
150         struct mailbox mb[2 * AHA1542_MAILBOXES];
151         struct ccb ccb[AHA1542_MAILBOXES];
152 };
153
154 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
155
156 static struct Scsi_Host *aha_host[7];   /* One for each IRQ level (9-15) */
157
158 static DEFINE_SPINLOCK(aha1542_lock);
159
160
161
162 #define WAITnexttimeout 3000000
163
164 static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
165 static int aha1542_restart(struct Scsi_Host *shost);
166 static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id);
167 static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id);
168
169 #define aha1542_intr_reset(base)  outb(IRST, CONTROL(base))
170
171 #define WAIT(port, mask, allof, noneof)                                 \
172  { register int WAITbits;                                               \
173    register int WAITtimeout = WAITnexttimeout;                          \
174    while (1) {                                                          \
175      WAITbits = inb(port) & (mask);                                     \
176      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
177        break;                                                           \
178      if (--WAITtimeout == 0) goto fail;                                 \
179    }                                                                    \
180  }
181
182 /* Similar to WAIT, except we use the udelay call to regulate the
183    amount of time we wait.  */
184 #define WAITd(port, mask, allof, noneof, timeout)                       \
185  { register int WAITbits;                                               \
186    register int WAITtimeout = timeout;                                  \
187    while (1) {                                                          \
188      WAITbits = inb(port) & (mask);                                     \
189      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
190        break;                                                           \
191      mdelay(1);                                                 \
192      if (--WAITtimeout == 0) goto fail;                                 \
193    }                                                                    \
194  }
195
196 static void aha1542_stat(void)
197 {
198 /*      int s = inb(STATUS), i = inb(INTRFLAGS);
199         printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
200 }
201
202 /* This is a bit complicated, but we need to make sure that an interrupt
203    routine does not send something out while we are in the middle of this.
204    Fortunately, it is only at boot time that multi-byte messages
205    are ever sent. */
206 static int aha1542_out(unsigned int base, unchar * cmdp, int len)
207 {
208         unsigned long flags = 0;
209         int got_lock;
210
211         if (len == 1) {
212                 got_lock = 0;
213                 while (1 == 1) {
214                         WAIT(STATUS(base), CDF, 0, CDF);
215                         spin_lock_irqsave(&aha1542_lock, flags);
216                         if (inb(STATUS(base)) & CDF) {
217                                 spin_unlock_irqrestore(&aha1542_lock, flags);
218                                 continue;
219                         }
220                         outb(*cmdp, DATA(base));
221                         spin_unlock_irqrestore(&aha1542_lock, flags);
222                         return 0;
223                 }
224         } else {
225                 spin_lock_irqsave(&aha1542_lock, flags);
226                 got_lock = 1;
227                 while (len--) {
228                         WAIT(STATUS(base), CDF, 0, CDF);
229                         outb(*cmdp++, DATA(base));
230                 }
231                 spin_unlock_irqrestore(&aha1542_lock, flags);
232         }
233         return 0;
234 fail:
235         if (got_lock)
236                 spin_unlock_irqrestore(&aha1542_lock, flags);
237         printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
238         aha1542_stat();
239         return 1;
240 }
241
242 /* Only used at boot time, so we do not need to worry about latency as much
243    here */
244
245 static int __init aha1542_in(unsigned int base, unchar * cmdp, int len)
246 {
247         unsigned long flags;
248
249         spin_lock_irqsave(&aha1542_lock, flags);
250         while (len--) {
251                 WAIT(STATUS(base), DF, DF, 0);
252                 *cmdp++ = inb(DATA(base));
253         }
254         spin_unlock_irqrestore(&aha1542_lock, flags);
255         return 0;
256 fail:
257         spin_unlock_irqrestore(&aha1542_lock, flags);
258         printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
259         aha1542_stat();
260         return 1;
261 }
262
263 /* Similar to aha1542_in, except that we wait a very short period of time.
264    We use this if we know the board is alive and awake, but we are not sure
265    if the board will respond to the command we are about to send or not */
266 static int __init aha1542_in1(unsigned int base, unchar * cmdp, int len)
267 {
268         unsigned long flags;
269
270         spin_lock_irqsave(&aha1542_lock, flags);
271         while (len--) {
272                 WAITd(STATUS(base), DF, DF, 0, 100);
273                 *cmdp++ = inb(DATA(base));
274         }
275         spin_unlock_irqrestore(&aha1542_lock, flags);
276         return 0;
277 fail:
278         spin_unlock_irqrestore(&aha1542_lock, flags);
279         return 1;
280 }
281
282 static int makecode(unsigned hosterr, unsigned scsierr)
283 {
284         switch (hosterr) {
285         case 0x0:
286         case 0xa:               /* Linked command complete without error and linked normally */
287         case 0xb:               /* Linked command complete without error, interrupt generated */
288                 hosterr = 0;
289                 break;
290
291         case 0x11:              /* Selection time out-The initiator selection or target
292                                    reselection was not complete within the SCSI Time out period */
293                 hosterr = DID_TIME_OUT;
294                 break;
295
296         case 0x12:              /* Data overrun/underrun-The target attempted to transfer more data
297                                    than was allocated by the Data Length field or the sum of the
298                                    Scatter / Gather Data Length fields. */
299
300         case 0x13:              /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
301
302         case 0x15:              /* MBO command was not 00, 01 or 02-The first byte of the CB was
303                                    invalid. This usually indicates a software failure. */
304
305         case 0x16:              /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
306                                    This usually indicates a software failure. */
307
308         case 0x17:              /* Linked CCB does not have the same LUN-A subsequent CCB of a set
309                                    of linked CCB's does not specify the same logical unit number as
310                                    the first. */
311         case 0x18:              /* Invalid Target Direction received from Host-The direction of a
312                                    Target Mode CCB was invalid. */
313
314         case 0x19:              /* Duplicate CCB Received in Target Mode-More than once CCB was
315                                    received to service data transfer between the same target LUN
316                                    and initiator SCSI ID in the same direction. */
317
318         case 0x1a:              /* Invalid CCB or Segment List Parameter-A segment list with a zero
319                                    length segment or invalid segment list boundaries was received.
320                                    A CCB parameter was invalid. */
321                 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
322                 hosterr = DID_ERROR;    /* Couldn't find any better */
323                 break;
324
325         case 0x14:              /* Target bus phase sequence failure-An invalid bus phase or bus
326                                    phase sequence was requested by the target. The host adapter
327                                    will generate a SCSI Reset Condition, notifying the host with
328                                    a SCRD interrupt */
329                 hosterr = DID_RESET;
330                 break;
331         default:
332                 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
333                 break;
334         }
335         return scsierr | (hosterr << 16);
336 }
337
338 static int __init aha1542_test_port(int bse, struct Scsi_Host *shpnt)
339 {
340         unchar inquiry_cmd[] = {CMD_INQUIRY};
341         unchar inquiry_result[4];
342         unchar *cmdp;
343         int len;
344         volatile int debug = 0;
345
346         /* Quick and dirty test for presence of the card. */
347         if (inb(STATUS(bse)) == 0xff)
348                 return 0;
349
350         /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
351
352         /*  DEB(printk("aha1542_test_port called \n")); */
353
354         /* In case some other card was probing here, reset interrupts */
355         aha1542_intr_reset(bse);        /* reset interrupts, so they don't block */
356
357         outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
358
359         mdelay(20);             /* Wait a little bit for things to settle down. */
360
361         debug = 1;
362         /* Expect INIT and IDLE, any of the others are bad */
363         WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
364
365         debug = 2;
366         /* Shouldn't have generated any interrupts during reset */
367         if (inb(INTRFLAGS(bse)) & INTRMASK)
368                 goto fail;
369
370
371         /* Perform a host adapter inquiry instead so we do not need to set
372            up the mailboxes ahead of time */
373
374         aha1542_out(bse, inquiry_cmd, 1);
375
376         debug = 3;
377         len = 4;
378         cmdp = &inquiry_result[0];
379
380         while (len--) {
381                 WAIT(STATUS(bse), DF, DF, 0);
382                 *cmdp++ = inb(DATA(bse));
383         }
384
385         debug = 8;
386         /* Reading port should reset DF */
387         if (inb(STATUS(bse)) & DF)
388                 goto fail;
389
390         debug = 9;
391         /* When HACC, command is completed, and we're though testing */
392         WAIT(INTRFLAGS(bse), HACC, HACC, 0);
393         /* now initialize adapter */
394
395         debug = 10;
396         /* Clear interrupts */
397         outb(IRST, CONTROL(bse));
398
399         debug = 11;
400
401         return debug;           /* 1 = ok */
402 fail:
403         return 0;               /* 0 = not ok */
404 }
405
406 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
407 static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id)
408 {
409         unsigned long flags;
410         struct Scsi_Host *shost;
411
412         shost = aha_host[irq - 9];
413         if (!shost)
414                 panic("Splunge!");
415
416         spin_lock_irqsave(shost->host_lock, flags);
417         aha1542_intr_handle(shost, dev_id);
418         spin_unlock_irqrestore(shost->host_lock, flags);
419         return IRQ_HANDLED;
420 }
421
422 /* A "high" level interrupt handler */
423 static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id)
424 {
425         void (*my_done) (Scsi_Cmnd *) = NULL;
426         int errstatus, mbi, mbo, mbistatus;
427         int number_serviced;
428         unsigned long flags;
429         Scsi_Cmnd *SCtmp;
430         int flag;
431         int needs_restart;
432         struct mailbox *mb;
433         struct ccb *ccb;
434
435         mb = HOSTDATA(shost)->mb;
436         ccb = HOSTDATA(shost)->ccb;
437
438 #ifdef DEBUG
439         {
440                 flag = inb(INTRFLAGS(shost->io_port));
441                 printk(KERN_DEBUG "aha1542_intr_handle: ");
442                 if (!(flag & ANYINTR))
443                         printk("no interrupt?");
444                 if (flag & MBIF)
445                         printk("MBIF ");
446                 if (flag & MBOA)
447                         printk("MBOF ");
448                 if (flag & HACC)
449                         printk("HACC ");
450                 if (flag & SCRD)
451                         printk("SCRD ");
452                 printk("status %02x\n", inb(STATUS(shost->io_port)));
453         };
454 #endif
455         number_serviced = 0;
456         needs_restart = 0;
457
458         while (1 == 1) {
459                 flag = inb(INTRFLAGS(shost->io_port));
460
461                 /* Check for unusual interrupts.  If any of these happen, we should
462                    probably do something special, but for now just printing a message
463                    is sufficient.  A SCSI reset detected is something that we really
464                    need to deal with in some way. */
465                 if (flag & ~MBIF) {
466                         if (flag & MBOA)
467                                 printk("MBOF ");
468                         if (flag & HACC)
469                                 printk("HACC ");
470                         if (flag & SCRD) {
471                                 needs_restart = 1;
472                                 printk("SCRD ");
473                         }
474                 }
475                 aha1542_intr_reset(shost->io_port);
476
477                 spin_lock_irqsave(&aha1542_lock, flags);
478                 mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
479                 if (mbi >= 2 * AHA1542_MAILBOXES)
480                         mbi = AHA1542_MAILBOXES;
481
482                 do {
483                         if (mb[mbi].status != 0)
484                                 break;
485                         mbi++;
486                         if (mbi >= 2 * AHA1542_MAILBOXES)
487                                 mbi = AHA1542_MAILBOXES;
488                 } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
489
490                 if (mb[mbi].status == 0) {
491                         spin_unlock_irqrestore(&aha1542_lock, flags);
492                         /* Hmm, no mail.  Must have read it the last time around */
493                         if (!number_serviced && !needs_restart)
494                                 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
495                         /* We detected a reset.  Restart all pending commands for
496                            devices that use the hard reset option */
497                         if (needs_restart)
498                                 aha1542_restart(shost);
499                         return;
500                 };
501
502                 mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_BUF_PA(&ccb[0]))) / sizeof(struct ccb);
503                 mbistatus = mb[mbi].status;
504                 mb[mbi].status = 0;
505                 HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
506                 spin_unlock_irqrestore(&aha1542_lock, flags);
507
508 #ifdef DEBUG
509                 {
510                         if (ccb[mbo].tarstat | ccb[mbo].hastat)
511                                 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
512                                        ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
513                 };
514 #endif
515
516                 if (mbistatus == 3)
517                         continue;       /* Aborted command not found */
518
519 #ifdef DEBUG
520                 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
521 #endif
522
523                 SCtmp = HOSTDATA(shost)->SCint[mbo];
524
525                 if (!SCtmp || !SCtmp->scsi_done) {
526                         printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
527                         printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
528                                ccb[mbo].hastat, ccb[mbo].idlun, mbo);
529                         return;
530                 }
531                 my_done = SCtmp->scsi_done;
532                 kfree(SCtmp->host_scribble);
533                 SCtmp->host_scribble = NULL;
534                 /* Fetch the sense data, and tuck it away, in the required slot.  The
535                    Adaptec automatically fetches it, and there is no guarantee that
536                    we will still have it in the cdb when we come back */
537                 if (ccb[mbo].tarstat == 2)
538                         memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
539                                SCSI_SENSE_BUFFERSIZE);
540
541
542                 /* is there mail :-) */
543
544                 /* more error checking left out here */
545                 if (mbistatus != 1)
546                         /* This is surely wrong, but I don't know what's right */
547                         errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
548                 else
549                         errstatus = 0;
550
551 #ifdef DEBUG
552                 if (errstatus)
553                         printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
554                                ccb[mbo].hastat, ccb[mbo].tarstat);
555 #endif
556
557                 if (ccb[mbo].tarstat == 2) {
558 #ifdef DEBUG
559                         int i;
560 #endif
561                         DEB(printk("aha1542_intr_handle: sense:"));
562 #ifdef DEBUG
563                         for (i = 0; i < 12; i++)
564                                 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
565                         printk("\n");
566 #endif
567                         /*
568                            DEB(printk("aha1542_intr_handle: buf:"));
569                            for (i = 0; i < bufflen; i++)
570                            printk("%02x ", ((unchar *)buff)[i]);
571                            printk("\n");
572                          */
573                 }
574                 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
575                 SCtmp->result = errstatus;
576                 HOSTDATA(shost)->SCint[mbo] = NULL;     /* This effectively frees up the mailbox slot, as
577                                                            far as queuecommand is concerned */
578                 my_done(SCtmp);
579                 number_serviced++;
580         };
581 }
582
583 static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
584 {
585         unchar ahacmd = CMD_START_SCSI;
586         unchar direction;
587         unchar *cmd = (unchar *) SCpnt->cmnd;
588         unchar target = SCpnt->device->id;
589         unchar lun = SCpnt->device->lun;
590         unsigned long flags;
591         int bufflen = scsi_bufflen(SCpnt);
592         int mbo;
593         struct mailbox *mb;
594         struct ccb *ccb;
595
596         DEB(int i);
597
598         mb = HOSTDATA(SCpnt->device->host)->mb;
599         ccb = HOSTDATA(SCpnt->device->host)->ccb;
600
601         DEB(if (target > 1) {
602             SCpnt->result = DID_TIME_OUT << 16;
603             done(SCpnt); return 0;
604             }
605         );
606
607         if (*cmd == REQUEST_SENSE) {
608                 /* Don't do the command - we have the sense data already */
609 #if 0
610                 /* scsi_request_sense() provides a buffer of size 256,
611                    so there is no reason to expect equality */
612                 if (bufflen != SCSI_SENSE_BUFFERSIZE)
613                         printk(KERN_CRIT "aha1542: Wrong buffer length supplied "
614                                "for request sense (%d)\n", bufflen);
615 #endif
616                 SCpnt->result = 0;
617                 done(SCpnt);
618                 return 0;
619         }
620 #ifdef DEBUG
621         if (*cmd == READ_10 || *cmd == WRITE_10)
622                 i = xscsi2int(cmd + 2);
623         else if (*cmd == READ_6 || *cmd == WRITE_6)
624                 i = scsi2int(cmd + 2);
625         else
626                 i = -1;
627         if (done)
628                 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
629         else
630                 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
631         aha1542_stat();
632         printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
633         for (i = 0; i < SCpnt->cmd_len; i++)
634                 printk("%02x ", cmd[i]);
635         printk("\n");
636         if (*cmd == WRITE_10 || *cmd == WRITE_6)
637                 return 0;       /* we are still testing, so *don't* write */
638 #endif
639         /* Use the outgoing mailboxes in a round-robin fashion, because this
640            is how the host adapter will scan for them */
641
642         spin_lock_irqsave(&aha1542_lock, flags);
643         mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
644         if (mbo >= AHA1542_MAILBOXES)
645                 mbo = 0;
646
647         do {
648                 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
649                         break;
650                 mbo++;
651                 if (mbo >= AHA1542_MAILBOXES)
652                         mbo = 0;
653         } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
654
655         if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
656                 panic("Unable to find empty mailbox for aha1542.\n");
657
658         HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt;      /* This will effectively prevent someone else from
659                                                            screwing with this cdb. */
660
661         HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
662         spin_unlock_irqrestore(&aha1542_lock, flags);
663
664 #ifdef DEBUG
665         printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
666 #endif
667
668         any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo]));       /* This gets trashed for some reason */
669
670         memset(&ccb[mbo], 0, sizeof(struct ccb));
671
672         ccb[mbo].cdblen = SCpnt->cmd_len;
673
674         direction = 0;
675         if (*cmd == READ_10 || *cmd == READ_6)
676                 direction = 8;
677         else if (*cmd == WRITE_10 || *cmd == WRITE_6)
678                 direction = 16;
679
680         memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
681
682         if (bufflen) {
683                 struct scatterlist *sg;
684                 struct chain *cptr;
685 #ifdef DEBUG
686                 unsigned char *ptr;
687 #endif
688                 int i, sg_count = scsi_sg_count(SCpnt);
689                 ccb[mbo].op = 2;        /* SCSI Initiator Command  w/scatter-gather */
690                 SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
691                                                          GFP_KERNEL | GFP_DMA);
692                 cptr = (struct chain *) SCpnt->host_scribble;
693                 if (cptr == NULL) {
694                         /* free the claimed mailbox slot */
695                         HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL;
696                         return SCSI_MLQUEUE_HOST_BUSY;
697                 }
698                 scsi_for_each_sg(SCpnt, sg, sg_count, i) {
699                         any2scsi(cptr[i].dataptr, SCSI_SG_PA(sg));
700                         if (SCSI_SG_PA(sg) + sg->length - 1 > ISA_DMA_THRESHOLD)
701                                 BAD_SG_DMA(SCpnt, scsi_sglist(SCpnt), sg_count, i);
702                         any2scsi(cptr[i].datalen, sg->length);
703                 };
704                 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
705                 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr));
706 #ifdef DEBUG
707                 printk("cptr %x: ", cptr);
708                 ptr = (unsigned char *) cptr;
709                 for (i = 0; i < 18; i++)
710                         printk("%02x ", ptr[i]);
711 #endif
712         } else {
713                 ccb[mbo].op = 0;        /* SCSI Initiator Command */
714                 SCpnt->host_scribble = NULL;
715                 any2scsi(ccb[mbo].datalen, 0);
716                 any2scsi(ccb[mbo].dataptr, 0);
717         };
718         ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);     /*SCSI Target Id */
719         ccb[mbo].rsalen = 16;
720         ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
721         ccb[mbo].commlinkid = 0;
722
723 #ifdef DEBUG
724         {
725                 int i;
726                 printk(KERN_DEBUG "aha1542_command: sending.. ");
727                 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
728                         printk("%02x ", ((unchar *) & ccb[mbo])[i]);
729         };
730 #endif
731
732         if (done) {
733                 DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
734                     aha1542_stat());
735                 SCpnt->scsi_done = done;
736                 mb[mbo].status = 1;
737                 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);  /* start scsi command */
738                 DEB(aha1542_stat());
739         } else
740                 printk("aha1542_queuecommand: done can't be NULL\n");
741
742         return 0;
743 }
744
745 /* Initialize mailboxes */
746 static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
747 {
748         int i;
749         struct mailbox *mb;
750         struct ccb *ccb;
751
752         unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
753
754         mb = HOSTDATA(shpnt)->mb;
755         ccb = HOSTDATA(shpnt)->ccb;
756
757         for (i = 0; i < AHA1542_MAILBOXES; i++) {
758                 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
759                 any2scsi(mb[i].ccbptr, SCSI_BUF_PA(&ccb[i]));
760         };
761         aha1542_intr_reset(bse);        /* reset interrupts, so they don't block */
762         any2scsi((cmd + 2), SCSI_BUF_PA(mb));
763         aha1542_out(bse, cmd, 5);
764         WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
765         while (0) {
766 fail:
767                 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
768         }
769         aha1542_intr_reset(bse);
770 }
771
772 static int __init aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
773 {
774         unchar inquiry_cmd[] = {CMD_RETCONF};
775         unchar inquiry_result[3];
776         int i;
777         i = inb(STATUS(base_io));
778         if (i & DF) {
779                 i = inb(DATA(base_io));
780         };
781         aha1542_out(base_io, inquiry_cmd, 1);
782         aha1542_in(base_io, inquiry_result, 3);
783         WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
784         while (0) {
785 fail:
786                 printk(KERN_ERR "aha1542_detect: query board settings\n");
787         }
788         aha1542_intr_reset(base_io);
789         switch (inquiry_result[0]) {
790         case 0x80:
791                 *dma_chan = 7;
792                 break;
793         case 0x40:
794                 *dma_chan = 6;
795                 break;
796         case 0x20:
797                 *dma_chan = 5;
798                 break;
799         case 0x01:
800                 *dma_chan = 0;
801                 break;
802         case 0:
803                 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
804                    Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
805                 *dma_chan = 0xFF;
806                 break;
807         default:
808                 printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
809                 return -1;
810         };
811         switch (inquiry_result[1]) {
812         case 0x40:
813                 *irq_level = 15;
814                 break;
815         case 0x20:
816                 *irq_level = 14;
817                 break;
818         case 0x8:
819                 *irq_level = 12;
820                 break;
821         case 0x4:
822                 *irq_level = 11;
823                 break;
824         case 0x2:
825                 *irq_level = 10;
826                 break;
827         case 0x1:
828                 *irq_level = 9;
829                 break;
830         default:
831                 printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
832                 return -1;
833         };
834         *scsi_id = inquiry_result[2] & 7;
835         return 0;
836 }
837
838 /* This function should only be called for 1542C boards - we can detect
839    the special firmware settings and unlock the board */
840
841 static int __init aha1542_mbenable(int base)
842 {
843         static unchar mbenable_cmd[3];
844         static unchar mbenable_result[2];
845         int retval;
846
847         retval = BIOS_TRANSLATION_6432;
848
849         mbenable_cmd[0] = CMD_EXTBIOS;
850         aha1542_out(base, mbenable_cmd, 1);
851         if (aha1542_in1(base, mbenable_result, 2))
852                 return retval;
853         WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
854         aha1542_intr_reset(base);
855
856         if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
857                 mbenable_cmd[0] = CMD_MBENABLE;
858                 mbenable_cmd[1] = 0;
859                 mbenable_cmd[2] = mbenable_result[1];
860
861                 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
862                         retval = BIOS_TRANSLATION_25563;
863
864                 aha1542_out(base, mbenable_cmd, 3);
865                 WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
866         };
867         while (0) {
868 fail:
869                 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
870         }
871         aha1542_intr_reset(base);
872         return retval;
873 }
874
875 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
876 static int __init aha1542_query(int base_io, int *transl)
877 {
878         unchar inquiry_cmd[] = {CMD_INQUIRY};
879         unchar inquiry_result[4];
880         int i;
881         i = inb(STATUS(base_io));
882         if (i & DF) {
883                 i = inb(DATA(base_io));
884         };
885         aha1542_out(base_io, inquiry_cmd, 1);
886         aha1542_in(base_io, inquiry_result, 4);
887         WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
888         while (0) {
889 fail:
890                 printk(KERN_ERR "aha1542_detect: query card type\n");
891         }
892         aha1542_intr_reset(base_io);
893
894         *transl = BIOS_TRANSLATION_6432;        /* Default case */
895
896         /* For an AHA1740 series board, we ignore the board since there is a
897            hardware bug which can lead to wrong blocks being returned if the board
898            is operating in the 1542 emulation mode.  Since there is an extended mode
899            driver, we simply ignore the board and let the 1740 driver pick it up.
900          */
901
902         if (inquiry_result[0] == 0x43) {
903                 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
904                 return 1;
905         };
906
907         /* Always call this - boards that do not support extended bios translation
908            will ignore the command, and we will set the proper default */
909
910         *transl = aha1542_mbenable(base_io);
911
912         return 0;
913 }
914
915 #ifndef MODULE
916 static char *setup_str[MAXBOARDS] __initdata;
917 static int setup_idx = 0;
918
919 static void __init aha1542_setup(char *str, int *ints)
920 {
921         const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
922         int setup_portbase;
923
924         if (setup_idx >= MAXBOARDS) {
925                 printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
926                 printk(KERN_ERR "   Entryline 1: %s\n", setup_str[0]);
927                 printk(KERN_ERR "   Entryline 2: %s\n", setup_str[1]);
928                 printk(KERN_ERR "   This line:   %s\n", str);
929                 return;
930         }
931         if (ints[0] < 1 || ints[0] > 4) {
932                 printk(KERN_ERR "aha1542: %s\n", str);
933                 printk(ahausage);
934                 printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
935         }
936         setup_called[setup_idx] = ints[0];
937         setup_str[setup_idx] = str;
938
939         setup_portbase = ints[0] >= 1 ? ints[1] : 0;    /* Preserve the default value.. */
940         setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
941         setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
942         if (ints[0] >= 4) 
943         {
944                 int atbt = -1;
945                 switch (ints[4]) {
946                 case 5:
947                         atbt = 0x00;
948                         break;
949                 case 6:
950                         atbt = 0x04;
951                         break;
952                 case 7:
953                         atbt = 0x01;
954                         break;
955                 case 8:
956                         atbt = 0x02;
957                         break;
958                 case 10:
959                         atbt = 0x03;
960                         break;
961                 default:
962                         printk(KERN_ERR "aha1542: %s\n", str);
963                         printk(ahausage);
964                         printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s.  Using jumper defaults.\n");
965                         break;
966                 }
967                 setup_dmaspeed[setup_idx] = atbt;
968         }
969         if (setup_portbase != 0)
970                 bases[setup_idx] = setup_portbase;
971
972         ++setup_idx;
973 }
974
975 static int __init do_setup(char *str)
976 {
977         int ints[5];
978
979         int count=setup_idx;
980
981         get_options(str, ARRAY_SIZE(ints), ints);
982         aha1542_setup(str,ints);
983
984         return count<setup_idx;
985 }
986
987 __setup("aha1542=",do_setup);
988 #endif
989
990 /* return non-zero on detection */
991 static int __init aha1542_detect(struct scsi_host_template * tpnt)
992 {
993         unsigned char dma_chan;
994         unsigned char irq_level;
995         unsigned char scsi_id;
996         unsigned long flags;
997         unsigned int base_io;
998         int trans;
999         struct Scsi_Host *shpnt = NULL;
1000         int count = 0;
1001         int indx;
1002
1003         DEB(printk("aha1542_detect: \n"));
1004
1005         tpnt->proc_name = "aha1542";
1006
1007 #ifdef MODULE
1008         bases[0] = aha1542[0];
1009         setup_buson[0] = aha1542[1];
1010         setup_busoff[0] = aha1542[2];
1011         {
1012                 int atbt = -1;
1013                 switch (aha1542[3]) {
1014                 case 5:
1015                         atbt = 0x00;
1016                         break;
1017                 case 6:
1018                         atbt = 0x04;
1019                         break;
1020                 case 7:
1021                         atbt = 0x01;
1022                         break;
1023                 case 8:
1024                         atbt = 0x02;
1025                         break;
1026                 case 10:
1027                         atbt = 0x03;
1028                         break;
1029                 };
1030                 setup_dmaspeed[0] = atbt;
1031         }
1032 #endif
1033
1034         /*
1035          *      Find MicroChannel cards (AHA1640)
1036          */
1037 #ifdef CONFIG_MCA_LEGACY
1038         if(MCA_bus) {
1039                 int slot = 0;
1040                 int pos = 0;
1041
1042                 for (indx = 0; (slot != MCA_NOTFOUND) && (indx < ARRAY_SIZE(bases)); indx++) {
1043
1044                         if (bases[indx])
1045                                 continue;
1046
1047                         /* Detect only AHA-1640 cards -- MCA ID 0F1F */
1048                         slot = mca_find_unused_adapter(0x0f1f, slot);
1049                         if (slot == MCA_NOTFOUND)
1050                                 break;
1051
1052                         /* Found one */
1053                         pos = mca_read_stored_pos(slot, 3);
1054
1055                         /* Decode address */
1056                         if (pos & 0x80) {
1057                                 if (pos & 0x02) {
1058                                         if (pos & 0x01)
1059                                                 bases[indx] = 0x334;
1060                                         else
1061                                                 bases[indx] = 0x234;
1062                                 } else {
1063                                         if (pos & 0x01)
1064                                                 bases[indx] = 0x134;
1065                                 }
1066                         } else {
1067                                 if (pos & 0x02) {
1068                                         if (pos & 0x01)
1069                                                 bases[indx] = 0x330;
1070                                         else
1071                                                 bases[indx] = 0x230;
1072                                 } else {
1073                                         if (pos & 0x01)
1074                                                 bases[indx] = 0x130;
1075                                 }
1076                         }
1077
1078                         /* No need to decode IRQ and Arb level -- those are
1079                          * read off the card later.
1080                          */
1081                         printk(KERN_INFO "Found an AHA-1640 in MCA slot %d, I/O 0x%04x\n", slot, bases[indx]);
1082
1083                         mca_set_adapter_name(slot, "Adapter AHA-1640");
1084                         mca_set_adapter_procfn(slot, NULL, NULL);
1085                         mca_mark_as_used(slot);
1086
1087                         /* Go on */
1088                         slot++;
1089                 }
1090
1091         }
1092 #endif
1093
1094         /*
1095          *      Hunt for ISA Plug'n'Pray Adaptecs (AHA1535)
1096          */
1097
1098         if(isapnp)
1099         {
1100                 struct pnp_dev *pdev = NULL;
1101                 for(indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1102                         if(bases[indx])
1103                                 continue;
1104                         pdev = pnp_find_dev(NULL, ISAPNP_VENDOR('A', 'D', 'P'), 
1105                                 ISAPNP_FUNCTION(0x1542), pdev);
1106                         if(pdev==NULL)
1107                                 break;
1108                         /*
1109                          *      Activate the PnP card
1110                          */
1111
1112                         if(pnp_device_attach(pdev)<0)
1113                                 continue;
1114
1115                         if(pnp_activate_dev(pdev)<0) {
1116                                 pnp_device_detach(pdev);
1117                                 continue;
1118                         }
1119
1120                         if(!pnp_port_valid(pdev, 0)) {
1121                                 pnp_device_detach(pdev);
1122                                 continue;
1123                         }
1124
1125                         bases[indx] = pnp_port_start(pdev, 0);
1126
1127                         /* The card can be queried for its DMA, we have 
1128                            the DMA set up that is enough */
1129
1130                         printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1131                 }
1132         }
1133         for (indx = 0; indx < ARRAY_SIZE(bases); indx++)
1134                 if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
1135                         shpnt = scsi_register(tpnt,
1136                                         sizeof(struct aha1542_hostdata));
1137
1138                         if(shpnt==NULL) {
1139                                 release_region(bases[indx], 4);
1140                                 continue;
1141                         }
1142                         /* For now we do this - until kmalloc is more intelligent
1143                            we are resigned to stupid hacks like this */
1144                         if (SCSI_BUF_PA(shpnt) >= ISA_DMA_THRESHOLD) {
1145                                 printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
1146                                 goto unregister;
1147                         }
1148                         if (!aha1542_test_port(bases[indx], shpnt))
1149                                 goto unregister;
1150
1151
1152                         base_io = bases[indx];
1153
1154                         /* Set the Bus on/off-times as not to ruin floppy performance */
1155                         {
1156                                 unchar oncmd[] = {CMD_BUSON_TIME, 7};
1157                                 unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
1158
1159                                 if (setup_called[indx]) {
1160                                         oncmd[1] = setup_buson[indx];
1161                                         offcmd[1] = setup_busoff[indx];
1162                                 }
1163                                 aha1542_intr_reset(base_io);
1164                                 aha1542_out(base_io, oncmd, 2);
1165                                 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1166                                 aha1542_intr_reset(base_io);
1167                                 aha1542_out(base_io, offcmd, 2);
1168                                 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1169                                 if (setup_dmaspeed[indx] >= 0) {
1170                                         unchar dmacmd[] = {CMD_DMASPEED, 0};
1171                                         dmacmd[1] = setup_dmaspeed[indx];
1172                                         aha1542_intr_reset(base_io);
1173                                         aha1542_out(base_io, dmacmd, 2);
1174                                         WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1175                                 }
1176                                 while (0) {
1177 fail:
1178                                         printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
1179                                 }
1180                                 aha1542_intr_reset(base_io);
1181                         }
1182                         if (aha1542_query(base_io, &trans))
1183                                 goto unregister;
1184
1185                         if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1186                                 goto unregister;
1187
1188                         printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1189                         if (dma_chan != 0xFF)
1190                                 printk(", DMA priority %d", dma_chan);
1191                         printk("\n");
1192
1193                         DEB(aha1542_stat());
1194                         setup_mailboxes(base_io, shpnt);
1195
1196                         DEB(aha1542_stat());
1197
1198                         DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1199                         spin_lock_irqsave(&aha1542_lock, flags);
1200                         if (request_irq(irq_level, do_aha1542_intr_handle, 0, "aha1542", NULL)) {
1201                                 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1202                                 spin_unlock_irqrestore(&aha1542_lock, flags);
1203                                 goto unregister;
1204                         }
1205                         if (dma_chan != 0xFF) {
1206                                 if (request_dma(dma_chan, "aha1542")) {
1207                                         printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
1208                                         free_irq(irq_level, NULL);
1209                                         spin_unlock_irqrestore(&aha1542_lock, flags);
1210                                         goto unregister;
1211                                 }
1212                                 if (dma_chan == 0 || dma_chan >= 5) {
1213                                         set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1214                                         enable_dma(dma_chan);
1215                                 }
1216                         }
1217                         aha_host[irq_level - 9] = shpnt;
1218                         shpnt->this_id = scsi_id;
1219                         shpnt->unique_id = base_io;
1220                         shpnt->io_port = base_io;
1221                         shpnt->n_io_port = 4;   /* Number of bytes of I/O space used */
1222                         shpnt->dma_channel = dma_chan;
1223                         shpnt->irq = irq_level;
1224                         HOSTDATA(shpnt)->bios_translation = trans;
1225                         if (trans == BIOS_TRANSLATION_25563)
1226                                 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
1227                         HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1228                         HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1229                         memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
1230                         spin_unlock_irqrestore(&aha1542_lock, flags);
1231 #if 0
1232                         DEB(printk(" *** READ CAPACITY ***\n"));
1233
1234                         {
1235                                 unchar buf[8];
1236                                 static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1237                                 int i;
1238
1239                                 for (i = 0; i < sizeof(buf); ++i)
1240                                         buf[i] = 0x87;
1241                                 for (i = 0; i < 2; ++i)
1242                                         if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
1243                                                 printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
1244                                                        i, xscsi2int(buf + 4), xscsi2int(buf));
1245                                         }
1246                         }
1247
1248                         DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
1249
1250                         for (i = 0; i < 4; ++i) {
1251                                 unsigned char cmd[10];
1252                                 static buffer[512];
1253
1254                                 cmd[0] = READ_10;
1255                                 cmd[1] = 0;
1256                                 xany2scsi(cmd + 2, i);
1257                                 cmd[6] = 0;
1258                                 cmd[7] = 0;
1259                                 cmd[8] = 1;
1260                                 cmd[9] = 0;
1261                                 aha1542_command(0, cmd, buffer, 512);
1262                         }
1263 #endif
1264                         count++;
1265                         continue;
1266 unregister:
1267                         release_region(bases[indx], 4);
1268                         scsi_unregister(shpnt);
1269                         continue;
1270
1271                 };
1272
1273         return count;
1274 }
1275
1276 static int aha1542_release(struct Scsi_Host *shost)
1277 {
1278         if (shost->irq)
1279                 free_irq(shost->irq, NULL);
1280         if (shost->dma_channel != 0xff)
1281                 free_dma(shost->dma_channel);
1282         if (shost->io_port && shost->n_io_port)
1283                 release_region(shost->io_port, shost->n_io_port);
1284         scsi_unregister(shost);
1285         return 0;
1286 }
1287
1288 static int aha1542_restart(struct Scsi_Host *shost)
1289 {
1290         int i;
1291         int count = 0;
1292 #if 0
1293         unchar ahacmd = CMD_START_SCSI;
1294 #endif
1295
1296         for (i = 0; i < AHA1542_MAILBOXES; i++)
1297                 if (HOSTDATA(shost)->SCint[i] &&
1298                     !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
1299 #if 0
1300                         HOSTDATA(shost)->mb[i].status = 1;      /* Indicate ready to restart... */
1301 #endif
1302                         count++;
1303                 }
1304         printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
1305 #if 0
1306         /* start scsi command */
1307         if (count)
1308                 aha1542_out(shost->io_port, &ahacmd, 1);
1309 #endif
1310         return 0;
1311 }
1312
1313 /*
1314  * This is a device reset.  This is handled by sending a special command
1315  * to the device.
1316  */
1317 static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1318 {
1319         unsigned long flags;
1320         struct mailbox *mb;
1321         unchar target = SCpnt->device->id;
1322         unchar lun = SCpnt->device->lun;
1323         int mbo;
1324         struct ccb *ccb;
1325         unchar ahacmd = CMD_START_SCSI;
1326
1327         ccb = HOSTDATA(SCpnt->device->host)->ccb;
1328         mb = HOSTDATA(SCpnt->device->host)->mb;
1329
1330         spin_lock_irqsave(&aha1542_lock, flags);
1331         mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
1332         if (mbo >= AHA1542_MAILBOXES)
1333                 mbo = 0;
1334
1335         do {
1336                 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
1337                         break;
1338                 mbo++;
1339                 if (mbo >= AHA1542_MAILBOXES)
1340                         mbo = 0;
1341         } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
1342
1343         if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
1344                 panic("Unable to find empty mailbox for aha1542.\n");
1345
1346         HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt;      /* This will effectively
1347                                                            prevent someone else from
1348                                                            screwing with this cdb. */
1349
1350         HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
1351         spin_unlock_irqrestore(&aha1542_lock, flags);
1352
1353         any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo]));       /* This gets trashed for some reason */
1354
1355         memset(&ccb[mbo], 0, sizeof(struct ccb));
1356
1357         ccb[mbo].op = 0x81;     /* BUS DEVICE RESET */
1358
1359         ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);         /*SCSI Target Id */
1360
1361         ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1362         ccb[mbo].commlinkid = 0;
1363
1364         /* 
1365          * Now tell the 1542 to flush all pending commands for this 
1366          * target 
1367          */
1368         aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
1369
1370         scmd_printk(KERN_WARNING, SCpnt,
1371                 "Trying device reset for target\n");
1372
1373         return SUCCESS;
1374
1375
1376 #ifdef ERIC_neverdef
1377         /* 
1378          * With the 1542 we apparently never get an interrupt to
1379          * acknowledge a device reset being sent.  Then again, Leonard
1380          * says we are doing this wrong in the first place...
1381          *
1382          * Take a wait and see attitude.  If we get spurious interrupts,
1383          * then the device reset is doing something sane and useful, and
1384          * we will wait for the interrupt to post completion.
1385          */
1386         printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1387
1388         /*
1389          * Free the command block for all commands running on this 
1390          * target... 
1391          */
1392         for (i = 0; i < AHA1542_MAILBOXES; i++) {
1393                 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1394                     HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1395                         Scsi_Cmnd *SCtmp;
1396                         SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1397                         kfree(SCtmp->host_scribble);
1398                         SCtmp->host_scribble = NULL;
1399                         HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1400                         HOSTDATA(SCpnt->host)->mb[i].status = 0;
1401                 }
1402         }
1403         return SUCCESS;
1404
1405         return FAILED;
1406 #endif                          /* ERIC_neverdef */
1407 }
1408
1409 static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1410 {
1411         int i;
1412
1413         /* 
1414          * This does a scsi reset for all devices on the bus.
1415          * In principle, we could also reset the 1542 - should
1416          * we do this?  Try this first, and we can add that later
1417          * if it turns out to be useful.
1418          */
1419         outb(SCRST, CONTROL(SCpnt->device->host->io_port));
1420
1421         /*
1422          * Wait for the thing to settle down a bit.  Unfortunately
1423          * this is going to basically lock up the machine while we
1424          * wait for this to complete.  To be 100% correct, we need to
1425          * check for timeout, and if we are doing something like this
1426          * we are pretty desperate anyways.
1427          */
1428         ssleep(4);
1429
1430         spin_lock_irq(SCpnt->device->host->host_lock);
1431
1432         WAIT(STATUS(SCpnt->device->host->io_port),
1433              STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1434
1435         /*
1436          * Now try to pick up the pieces.  For all pending commands,
1437          * free any internal data structures, and basically clear things
1438          * out.  We do not try and restart any commands or anything - 
1439          * the strategy handler takes care of that crap.
1440          */
1441         printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1442
1443         for (i = 0; i < AHA1542_MAILBOXES; i++) {
1444                 if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
1445                         Scsi_Cmnd *SCtmp;
1446                         SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
1447
1448
1449                         if (SCtmp->device->soft_reset) {
1450                                 /*
1451                                  * If this device implements the soft reset option,
1452                                  * then it is still holding onto the command, and
1453                                  * may yet complete it.  In this case, we don't
1454                                  * flush the data.
1455                                  */
1456                                 continue;
1457                         }
1458                         kfree(SCtmp->host_scribble);
1459                         SCtmp->host_scribble = NULL;
1460                         HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
1461                         HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
1462                 }
1463         }
1464
1465         spin_unlock_irq(SCpnt->device->host->host_lock);
1466         return SUCCESS;
1467
1468 fail:
1469         spin_unlock_irq(SCpnt->device->host->host_lock);
1470         return FAILED;
1471 }
1472
1473 static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1474 {
1475         int i;
1476
1477         /* 
1478          * This does a scsi reset for all devices on the bus.
1479          * In principle, we could also reset the 1542 - should
1480          * we do this?  Try this first, and we can add that later
1481          * if it turns out to be useful.
1482          */
1483         outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
1484
1485         /*
1486          * Wait for the thing to settle down a bit.  Unfortunately
1487          * this is going to basically lock up the machine while we
1488          * wait for this to complete.  To be 100% correct, we need to
1489          * check for timeout, and if we are doing something like this
1490          * we are pretty desperate anyways.
1491          */
1492         ssleep(4);
1493         spin_lock_irq(SCpnt->device->host->host_lock);
1494
1495         WAIT(STATUS(SCpnt->device->host->io_port),
1496              STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1497
1498         /*
1499          * We need to do this too before the 1542 can interact with
1500          * us again.
1501          */
1502         setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
1503
1504         /*
1505          * Now try to pick up the pieces.  For all pending commands,
1506          * free any internal data structures, and basically clear things
1507          * out.  We do not try and restart any commands or anything - 
1508          * the strategy handler takes care of that crap.
1509          */
1510         printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1511
1512         for (i = 0; i < AHA1542_MAILBOXES; i++) {
1513                 if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
1514                         Scsi_Cmnd *SCtmp;
1515                         SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
1516
1517                         if (SCtmp->device->soft_reset) {
1518                                 /*
1519                                  * If this device implements the soft reset option,
1520                                  * then it is still holding onto the command, and
1521                                  * may yet complete it.  In this case, we don't
1522                                  * flush the data.
1523                                  */
1524                                 continue;
1525                         }
1526                         kfree(SCtmp->host_scribble);
1527                         SCtmp->host_scribble = NULL;
1528                         HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
1529                         HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
1530                 }
1531         }
1532
1533         spin_unlock_irq(SCpnt->device->host->host_lock);
1534         return SUCCESS;
1535
1536 fail:
1537         spin_unlock_irq(SCpnt->device->host->host_lock);
1538         return FAILED;
1539 }
1540
1541 #if 0
1542 /*
1543  * These are the old error handling routines.  They are only temporarily
1544  * here while we play with the new error handling code.
1545  */
1546 static int aha1542_old_abort(Scsi_Cmnd * SCpnt)
1547 {
1548 #if 0
1549         unchar ahacmd = CMD_START_SCSI;
1550         unsigned long flags;
1551         struct mailbox *mb;
1552         int mbi, mbo, i;
1553
1554         printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
1555                inb(STATUS(SCpnt->host->io_port)),
1556                inb(INTRFLAGS(SCpnt->host->io_port)));
1557
1558         spin_lock_irqsave(&aha1542_lock, flags);
1559         mb = HOSTDATA(SCpnt->host)->mb;
1560         mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1561         if (mbi >= 2 * AHA1542_MAILBOXES)
1562                 mbi = AHA1542_MAILBOXES;
1563
1564         do {
1565                 if (mb[mbi].status != 0)
1566                         break;
1567                 mbi++;
1568                 if (mbi >= 2 * AHA1542_MAILBOXES)
1569                         mbi = AHA1542_MAILBOXES;
1570         } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1571         spin_unlock_irqrestore(&aha1542_lock, flags);
1572
1573         if (mb[mbi].status) {
1574                 printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
1575                        SCpnt->host->irq);
1576                 aha1542_intr_handle(SCpnt->host, NULL);
1577                 return 0;
1578         }
1579         /* OK, no lost interrupt.  Try looking to see how many pending commands
1580            we think we have. */
1581
1582         for (i = 0; i < AHA1542_MAILBOXES; i++)
1583                 if (HOSTDATA(SCpnt->host)->SCint[i]) {
1584                         if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1585                                 printk(KERN_ERR "Timed out command pending for %s\n",
1586                                        SCpnt->request->rq_disk ?
1587                                        SCpnt->request->rq_disk->disk_name : "?"
1588                                        );
1589                                 if (HOSTDATA(SCpnt->host)->mb[i].status) {
1590                                         printk(KERN_ERR "OGMB still full - restarting\n");
1591                                         aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1592                                 };
1593                         } else
1594                                 printk(KERN_ERR "Other pending command %s\n",
1595                                        SCpnt->request->rq_disk ?
1596                                        SCpnt->request->rq_disk->disk_name : "?"
1597                                        );
1598                 }
1599 #endif
1600
1601         DEB(printk("aha1542_abort\n"));
1602 #if 0
1603         spin_lock_irqsave(&aha1542_lock, flags);
1604         for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++) {
1605                 if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
1606                         mb[mbo].status = 2;     /* Abort command */
1607                         aha1542_out(SCpnt->host->io_port, &ahacmd, 1);  /* start scsi command */
1608                         spin_unlock_irqrestore(&aha1542_lock, flags);
1609                         break;
1610                 }
1611         }
1612         if (AHA1542_MAILBOXES == mbo)
1613                 spin_unlock_irqrestore(&aha1542_lock, flags);
1614 #endif
1615         return SCSI_ABORT_SNOOZE;
1616 }
1617
1618 /* We do not implement a reset function here, but the upper level code
1619    assumes that it will get some kind of response for the command in
1620    SCpnt.  We must oblige, or the command will hang the scsi system.
1621    For a first go, we assume that the 1542 notifies us with all of the
1622    pending commands (it does implement soft reset, after all). */
1623
1624 static int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1625 {
1626         unchar ahacmd = CMD_START_SCSI;
1627         int i;
1628
1629         /*
1630          * See if a bus reset was suggested.
1631          */
1632         if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
1633                 /* 
1634                  * This does a scsi reset for all devices on the bus.
1635                  * In principle, we could also reset the 1542 - should
1636                  * we do this?  Try this first, and we can add that later
1637                  * if it turns out to be useful.
1638                  */
1639                 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1640
1641                 /*
1642                  * Wait for the thing to settle down a bit.  Unfortunately
1643                  * this is going to basically lock up the machine while we
1644                  * wait for this to complete.  To be 100% correct, we need to
1645                  * check for timeout, and if we are doing something like this
1646                  * we are pretty desperate anyways.
1647                  */
1648                 WAIT(STATUS(SCpnt->host->io_port),
1649                 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1650
1651                 /*
1652                  * We need to do this too before the 1542 can interact with
1653                  * us again.
1654                  */
1655                 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1656
1657                 /*
1658                  * Now try to pick up the pieces.  Restart all commands
1659                  * that are currently active on the bus, and reset all of
1660                  * the datastructures.  We have some time to kill while
1661                  * things settle down, so print a nice message.
1662                  */
1663                 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1664
1665                 for (i = 0; i < AHA1542_MAILBOXES; i++)
1666                         if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1667                                 Scsi_Cmnd *SCtmp;
1668                                 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1669                                 SCtmp->result = DID_RESET << 16;
1670                                 kfree(SCtmp->host_scribble);
1671                                 SCtmp->host_scribble = NULL;
1672                                 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1673                                 SCtmp->scsi_done(SCpnt);
1674
1675                                 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1676                                 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1677                         }
1678                 /*
1679                  * Now tell the mid-level code what we did here.  Since
1680                  * we have restarted all of the outstanding commands,
1681                  * then report SUCCESS.
1682                  */
1683                 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
1684 fail:
1685                 printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
1686                 printk(KERN_CRIT "Power cycle machine to reset\n");
1687                 return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
1688
1689
1690         } else {
1691                 /* This does a selective reset of just the one device */
1692                 /* First locate the ccb for this command */
1693                 for (i = 0; i < AHA1542_MAILBOXES; i++)
1694                         if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1695                                 HOSTDATA(SCpnt->host)->ccb[i].op = 0x81;        /* BUS DEVICE RESET */
1696                                 /* Now tell the 1542 to flush all pending commands for this target */
1697                                 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1698
1699                                 /* Here is the tricky part.  What to do next.  Do we get an interrupt
1700                                    for the commands that we aborted with the specified target, or
1701                                    do we generate this on our own?  Try it without first and see
1702                                    what happens */
1703                                 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1704
1705                                 /* If the first does not work, then try the second.  I think the
1706                                    first option is more likely to be correct. Free the command
1707                                    block for all commands running on this target... */
1708                                 for (i = 0; i < AHA1542_MAILBOXES; i++)
1709                                         if (HOSTDATA(SCpnt->host)->SCint[i] &&
1710                                             HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1711                                                 Scsi_Cmnd *SCtmp;
1712                                                 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1713                                                 SCtmp->result = DID_RESET << 16;
1714                                                 kfree(SCtmp->host_scribble);
1715                                                 SCtmp->host_scribble = NULL;
1716                                                 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1717                                                 SCtmp->scsi_done(SCpnt);
1718
1719                                                 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1720                                                 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1721                                         }
1722                                 return SCSI_RESET_SUCCESS;
1723                         }
1724         }
1725         /* No active command at this time, so this means that each time we got
1726            some kind of response the last time through.  Tell the mid-level code
1727            to request sense information in order to decide what to do next. */
1728         return SCSI_RESET_PUNT;
1729 }
1730 #endif    /* end of big comment block around old_abort + old_reset */
1731
1732 static int aha1542_biosparam(struct scsi_device *sdev,
1733                 struct block_device *bdev, sector_t capacity, int *ip)
1734 {
1735         int translation_algorithm;
1736         int size = capacity;
1737
1738         translation_algorithm = HOSTDATA(sdev->host)->bios_translation;
1739
1740         if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1741                 /* Please verify that this is the same as what DOS returns */
1742                 ip[0] = 255;
1743                 ip[1] = 63;
1744                 ip[2] = size / 255 / 63;
1745         } else {
1746                 ip[0] = 64;
1747                 ip[1] = 32;
1748                 ip[2] = size >> 11;
1749         }
1750
1751         return 0;
1752 }
1753 MODULE_LICENSE("GPL");
1754
1755
1756 static struct scsi_host_template driver_template = {
1757         .proc_name              = "aha1542",
1758         .name                   = "Adaptec 1542",
1759         .detect                 = aha1542_detect,
1760         .release                = aha1542_release,
1761         .queuecommand           = aha1542_queuecommand,
1762         .eh_device_reset_handler= aha1542_dev_reset,
1763         .eh_bus_reset_handler   = aha1542_bus_reset,
1764         .eh_host_reset_handler  = aha1542_host_reset,
1765         .bios_param             = aha1542_biosparam,
1766         .can_queue              = AHA1542_MAILBOXES, 
1767         .this_id                = 7,
1768         .sg_tablesize           = AHA1542_SCATTER,
1769         .cmd_per_lun            = AHA1542_CMDLUN,
1770         .unchecked_isa_dma      = 1, 
1771         .use_clustering         = ENABLE_CLUSTERING,
1772 };
1773 #include "scsi_module.c"