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