OSDN Git Service

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