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