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