1 /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
2 * linux/kernel/aha1542.c
4 * Copyright (C) 1992 Tommy Thorn
5 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
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
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
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
24 * Modified by Adam Fritzler
25 * Added proper detection of the AHA-1640 (MCA, now deleted)
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>
47 #include <scsi/scsi_host.h>
49 #include <linux/stat.h>
58 static bool isapnp = 1;
59 module_param(isapnp, bool, 0);
60 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
62 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
63 module_param_array(io, int, NULL, 0);
64 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
66 /* time AHA spends on the AT-bus during data transfer */
67 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
68 module_param_array(bus_on, int, NULL, 0);
69 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
71 /* time AHA spends off the bus (not to monopolize it) during data transfer */
72 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
73 module_param_array(bus_off, int, NULL, 0);
74 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
76 /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
77 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
78 module_param_array(dma_speed, int, NULL, 0);
79 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
81 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
82 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
84 struct aha1542_hostdata {
85 /* This will effectively start both of them at the first mailbox */
86 int bios_translation; /* Mapping bios uses - for compatibility */
87 int aha1542_last_mbi_used;
88 int aha1542_last_mbo_used;
89 Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
90 struct mailbox mb[2 * AHA1542_MAILBOXES];
91 struct ccb ccb[AHA1542_MAILBOXES];
94 static DEFINE_SPINLOCK(aha1542_lock);
96 static inline void aha1542_intr_reset(u16 base)
98 outb(IRST, CONTROL(base));
101 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
111 u8 bits = inb(port) & mask;
112 if ((bits & allof) == allof && ((bits & noneof) == 0))
123 /* This is a bit complicated, but we need to make sure that an interrupt
124 routine does not send something out while we are in the middle of this.
125 Fortunately, it is only at boot time that multi-byte messages
127 static int aha1542_outb(unsigned int base, u8 val)
132 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
133 printk(KERN_ERR "aha1542_outb failed");
136 spin_lock_irqsave(&aha1542_lock, flags);
137 if (inb(STATUS(base)) & CDF) {
138 spin_unlock_irqrestore(&aha1542_lock, flags);
141 outb(val, DATA(base));
142 spin_unlock_irqrestore(&aha1542_lock, flags);
147 static int aha1542_out(unsigned int base, u8 *buf, int len)
151 spin_lock_irqsave(&aha1542_lock, flags);
153 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
154 spin_unlock_irqrestore(&aha1542_lock, flags);
155 printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
158 outb(*buf++, DATA(base));
160 spin_unlock_irqrestore(&aha1542_lock, flags);
161 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
167 /* Only used at boot time, so we do not need to worry about latency as much
170 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
174 spin_lock_irqsave(&aha1542_lock, flags);
176 if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
177 spin_unlock_irqrestore(&aha1542_lock, flags);
179 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
182 *buf++ = inb(DATA(base));
184 spin_unlock_irqrestore(&aha1542_lock, flags);
188 static int makecode(unsigned hosterr, unsigned scsierr)
192 case 0xa: /* Linked command complete without error and linked normally */
193 case 0xb: /* Linked command complete without error, interrupt generated */
197 case 0x11: /* Selection time out-The initiator selection or target
198 reselection was not complete within the SCSI Time out period */
199 hosterr = DID_TIME_OUT;
202 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
203 than was allocated by the Data Length field or the sum of the
204 Scatter / Gather Data Length fields. */
206 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
208 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
209 invalid. This usually indicates a software failure. */
211 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
212 This usually indicates a software failure. */
214 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
215 of linked CCB's does not specify the same logical unit number as
217 case 0x18: /* Invalid Target Direction received from Host-The direction of a
218 Target Mode CCB was invalid. */
220 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
221 received to service data transfer between the same target LUN
222 and initiator SCSI ID in the same direction. */
224 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
225 length segment or invalid segment list boundaries was received.
226 A CCB parameter was invalid. */
227 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
228 hosterr = DID_ERROR; /* Couldn't find any better */
231 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
232 phase sequence was requested by the target. The host adapter
233 will generate a SCSI Reset Condition, notifying the host with
238 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
241 return scsierr | (hosterr << 16);
244 static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
246 u8 inquiry_result[4];
249 /* Quick and dirty test for presence of the card. */
250 if (inb(STATUS(bse)) == 0xff)
253 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
255 /* In case some other card was probing here, reset interrupts */
256 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
258 outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
260 mdelay(20); /* Wait a little bit for things to settle down. */
262 /* Expect INIT and IDLE, any of the others are bad */
263 if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
266 /* Shouldn't have generated any interrupts during reset */
267 if (inb(INTRFLAGS(bse)) & INTRMASK)
270 /* Perform a host adapter inquiry instead so we do not need to set
271 up the mailboxes ahead of time */
273 aha1542_outb(bse, CMD_INQUIRY);
275 for (i = 0; i < 4; i++) {
276 if (!wait_mask(STATUS(bse), DF, DF, 0, 0))
278 inquiry_result[i] = inb(DATA(bse));
281 /* Reading port should reset DF */
282 if (inb(STATUS(bse)) & DF)
285 /* When HACC, command is completed, and we're though testing */
286 if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0))
289 /* Clear interrupts */
290 outb(IRST, CONTROL(bse));
295 /* A "high" level interrupt handler */
296 static void aha1542_intr_handle(struct Scsi_Host *shost)
298 struct aha1542_hostdata *aha1542 = shost_priv(shost);
299 void (*my_done) (Scsi_Cmnd *) = NULL;
300 int errstatus, mbi, mbo, mbistatus;
305 struct mailbox *mb = aha1542->mb;
306 struct ccb *ccb = aha1542->ccb;
310 flag = inb(INTRFLAGS(shost->io_port));
311 printk(KERN_DEBUG "aha1542_intr_handle: ");
312 if (!(flag & ANYINTR))
313 printk("no interrupt?");
322 printk("status %02x\n", inb(STATUS(shost->io_port)));
328 flag = inb(INTRFLAGS(shost->io_port));
330 /* Check for unusual interrupts. If any of these happen, we should
331 probably do something special, but for now just printing a message
332 is sufficient. A SCSI reset detected is something that we really
333 need to deal with in some way. */
342 aha1542_intr_reset(shost->io_port);
344 spin_lock_irqsave(&aha1542_lock, flags);
345 mbi = aha1542->aha1542_last_mbi_used + 1;
346 if (mbi >= 2 * AHA1542_MAILBOXES)
347 mbi = AHA1542_MAILBOXES;
350 if (mb[mbi].status != 0)
353 if (mbi >= 2 * AHA1542_MAILBOXES)
354 mbi = AHA1542_MAILBOXES;
355 } while (mbi != aha1542->aha1542_last_mbi_used);
357 if (mb[mbi].status == 0) {
358 spin_unlock_irqrestore(&aha1542_lock, flags);
359 /* Hmm, no mail. Must have read it the last time around */
360 if (!number_serviced)
361 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
365 mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
366 mbistatus = mb[mbi].status;
368 aha1542->aha1542_last_mbi_used = mbi;
369 spin_unlock_irqrestore(&aha1542_lock, flags);
373 if (ccb[mbo].tarstat | ccb[mbo].hastat)
374 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
375 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
380 continue; /* Aborted command not found */
383 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
386 SCtmp = aha1542->SCint[mbo];
388 if (!SCtmp || !SCtmp->scsi_done) {
389 printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
390 printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
391 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
394 my_done = SCtmp->scsi_done;
395 kfree(SCtmp->host_scribble);
396 SCtmp->host_scribble = NULL;
397 /* Fetch the sense data, and tuck it away, in the required slot. The
398 Adaptec automatically fetches it, and there is no guarantee that
399 we will still have it in the cdb when we come back */
400 if (ccb[mbo].tarstat == 2)
401 memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
402 SCSI_SENSE_BUFFERSIZE);
405 /* is there mail :-) */
407 /* more error checking left out here */
409 /* This is surely wrong, but I don't know what's right */
410 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
416 printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
417 ccb[mbo].hastat, ccb[mbo].tarstat);
420 if (ccb[mbo].tarstat == 2) {
424 DEB(printk("aha1542_intr_handle: sense:"));
426 for (i = 0; i < 12; i++)
427 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
431 DEB(printk("aha1542_intr_handle: buf:"));
432 for (i = 0; i < bufflen; i++)
433 printk("%02x ", ((unchar *)buff)[i]);
437 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
438 SCtmp->result = errstatus;
439 aha1542->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
440 far as queuecommand is concerned */
446 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
447 static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
450 struct Scsi_Host *shost = dev_id;
452 spin_lock_irqsave(shost->host_lock, flags);
453 aha1542_intr_handle(shost);
454 spin_unlock_irqrestore(shost->host_lock, flags);
458 static int aha1542_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
460 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
462 u8 target = SCpnt->device->id;
463 u8 lun = SCpnt->device->lun;
465 int bufflen = scsi_bufflen(SCpnt);
467 struct mailbox *mb = aha1542->mb;
468 struct ccb *ccb = aha1542->ccb;
472 DEB(if (target > 1) {
473 SCpnt->result = DID_TIME_OUT << 16;
474 done(SCpnt); return 0;
478 if (*SCpnt->cmnd == REQUEST_SENSE) {
479 /* Don't do the command - we have the sense data already */
485 if (*SCpnt->cmnd == READ_10 || *SCpnt->cmnd == WRITE_10)
486 i = xscsi2int(SCpnt->cmnd + 2);
487 else if (*SCpnt->cmnd == READ_6 || *SCpnt->cmnd == WRITE_6)
488 i = scsi2int(SCpnt->cmnd + 2);
492 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *SCpnt->cmnd, i, bufflen);
494 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *SCpnt->cmnd, i, bufflen);
495 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
496 for (i = 0; i < SCpnt->cmd_len; i++)
497 printk("%02x ", SCpnt->cmnd[i]);
499 if (*SCpnt->cmnd == WRITE_10 || *SCpnt->cmnd == WRITE_6)
500 return 0; /* we are still testing, so *don't* write */
502 /* Use the outgoing mailboxes in a round-robin fashion, because this
503 is how the host adapter will scan for them */
505 spin_lock_irqsave(&aha1542_lock, flags);
506 mbo = aha1542->aha1542_last_mbo_used + 1;
507 if (mbo >= AHA1542_MAILBOXES)
511 if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
514 if (mbo >= AHA1542_MAILBOXES)
516 } while (mbo != aha1542->aha1542_last_mbo_used);
518 if (mb[mbo].status || aha1542->SCint[mbo])
519 panic("Unable to find empty mailbox for aha1542.\n");
521 aha1542->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
522 screwing with this cdb. */
524 aha1542->aha1542_last_mbo_used = mbo;
525 spin_unlock_irqrestore(&aha1542_lock, flags);
528 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
531 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
533 memset(&ccb[mbo], 0, sizeof(struct ccb));
535 ccb[mbo].cdblen = SCpnt->cmd_len;
538 if (*SCpnt->cmnd == READ_10 || *SCpnt->cmnd == READ_6)
540 else if (*SCpnt->cmnd == WRITE_10 || *SCpnt->cmnd == WRITE_6)
543 memcpy(ccb[mbo].cdb, SCpnt->cmnd, ccb[mbo].cdblen);
546 struct scatterlist *sg;
551 int i, sg_count = scsi_sg_count(SCpnt);
552 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
553 SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
554 GFP_KERNEL | GFP_DMA);
555 cptr = (struct chain *) SCpnt->host_scribble;
557 /* free the claimed mailbox slot */
558 aha1542->SCint[mbo] = NULL;
559 return SCSI_MLQUEUE_HOST_BUSY;
561 scsi_for_each_sg(SCpnt, sg, sg_count, i) {
562 any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
564 any2scsi(cptr[i].datalen, sg->length);
566 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
567 any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
569 printk("cptr %x: ", cptr);
570 ptr = (unsigned char *) cptr;
571 for (i = 0; i < 18; i++)
572 printk("%02x ", ptr[i]);
575 ccb[mbo].op = 0; /* SCSI Initiator Command */
576 SCpnt->host_scribble = NULL;
577 any2scsi(ccb[mbo].datalen, 0);
578 any2scsi(ccb[mbo].dataptr, 0);
580 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
581 ccb[mbo].rsalen = 16;
582 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
583 ccb[mbo].commlinkid = 0;
588 printk(KERN_DEBUG "aha1542_command: sending.. ");
589 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
590 printk("%02x ", ((u8 *) &ccb[mbo])[i]);
595 DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
596 SCpnt->scsi_done = done;
598 aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
600 printk("aha1542_queuecommand: done can't be NULL\n");
605 static DEF_SCSI_QCMD(aha1542_queuecommand)
607 /* Initialize mailboxes */
608 static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
610 struct aha1542_hostdata *aha1542 = shost_priv(shpnt);
612 struct mailbox *mb = aha1542->mb;
613 struct ccb *ccb = aha1542->ccb;
615 u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
617 for (i = 0; i < AHA1542_MAILBOXES; i++) {
618 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
619 any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
621 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
622 any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
623 if (aha1542_out(bse, mb_cmd, 5))
624 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
625 aha1542_intr_reset(bse);
628 static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id)
630 u8 inquiry_result[3];
632 i = inb(STATUS(base_io));
634 i = inb(DATA(base_io));
636 aha1542_outb(base_io, CMD_RETCONF);
637 aha1542_in(base_io, inquiry_result, 3, 0);
638 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
639 printk(KERN_ERR "aha1542_detect: query board settings\n");
640 aha1542_intr_reset(base_io);
641 switch (inquiry_result[0]) {
655 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
656 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
660 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
663 switch (inquiry_result[1]) {
683 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
686 *scsi_id = inquiry_result[2] & 7;
690 /* This function should only be called for 1542C boards - we can detect
691 the special firmware settings and unlock the board */
693 static int aha1542_mbenable(int base)
695 static u8 mbenable_cmd[3];
696 static u8 mbenable_result[2];
699 retval = BIOS_TRANSLATION_6432;
701 aha1542_outb(base, CMD_EXTBIOS);
702 if (aha1542_in(base, mbenable_result, 2, 100))
704 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
706 aha1542_intr_reset(base);
708 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
709 mbenable_cmd[0] = CMD_MBENABLE;
711 mbenable_cmd[2] = mbenable_result[1];
713 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
714 retval = BIOS_TRANSLATION_25563;
716 if (aha1542_out(base, mbenable_cmd, 3))
721 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
723 aha1542_intr_reset(base);
727 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
728 static int aha1542_query(int base_io, int *transl)
730 u8 inquiry_result[4];
732 i = inb(STATUS(base_io));
734 i = inb(DATA(base_io));
736 aha1542_outb(base_io, CMD_INQUIRY);
737 aha1542_in(base_io, inquiry_result, 4, 0);
738 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
739 printk(KERN_ERR "aha1542_detect: query card type\n");
740 aha1542_intr_reset(base_io);
742 *transl = BIOS_TRANSLATION_6432; /* Default case */
744 /* For an AHA1740 series board, we ignore the board since there is a
745 hardware bug which can lead to wrong blocks being returned if the board
746 is operating in the 1542 emulation mode. Since there is an extended mode
747 driver, we simply ignore the board and let the 1740 driver pick it up.
750 if (inquiry_result[0] == 0x43) {
751 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
755 /* Always call this - boards that do not support extended bios translation
756 will ignore the command, and we will set the proper default */
758 *transl = aha1542_mbenable(base_io);
763 static u8 dma_speed_hw(int dma_speed)
778 return 0xff; /* invalid */
781 /* Set the Bus on/off-times as not to ruin floppy performance */
782 static void aha1542_set_bus_times(int indx)
784 unsigned int base_io = io[indx];
786 if (bus_on[indx] > 0) {
787 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on[indx], 2, 15) };
789 aha1542_intr_reset(base_io);
790 if (aha1542_out(base_io, oncmd, 2))
794 if (bus_off[indx] > 0) {
795 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off[indx], 1, 64) };
797 aha1542_intr_reset(base_io);
798 if (aha1542_out(base_io, offcmd, 2))
802 if (dma_speed_hw(dma_speed[indx]) != 0xff) {
803 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed[indx]) };
805 aha1542_intr_reset(base_io);
806 if (aha1542_out(base_io, dmacmd, 2))
809 aha1542_intr_reset(base_io);
812 printk(KERN_ERR "setting bus on/off-time failed\n");
813 aha1542_intr_reset(base_io);
816 /* return non-zero on detection */
817 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
819 unsigned int base_io = io[indx];
820 struct Scsi_Host *shpnt;
821 struct aha1542_hostdata *aha1542;
826 if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
829 shpnt = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
832 aha1542 = shost_priv(shpnt);
834 if (!aha1542_test_port(base_io, shpnt))
837 aha1542_set_bus_times(indx);
838 if (aha1542_query(base_io, &aha1542->bios_translation))
840 if (aha1542_getconfig(base_io, &shpnt->irq, &shpnt->dma_channel, &shpnt->this_id) == -1)
843 printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", shpnt->this_id, base_io, shpnt->irq);
844 if (shpnt->dma_channel != 0xFF)
845 printk(", DMA %d", shpnt->dma_channel);
847 if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
848 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
850 setup_mailboxes(base_io, shpnt);
852 if (request_irq(shpnt->irq, do_aha1542_intr_handle, 0,
854 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
857 if (shpnt->dma_channel != 0xFF) {
858 if (request_dma(shpnt->dma_channel, "aha1542")) {
859 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
862 if (shpnt->dma_channel == 0 || shpnt->dma_channel >= 5) {
863 set_dma_mode(shpnt->dma_channel, DMA_MODE_CASCADE);
864 enable_dma(shpnt->dma_channel);
868 shpnt->unique_id = base_io;
869 shpnt->io_port = base_io;
870 shpnt->n_io_port = AHA1542_REGION_SIZE;
871 aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
872 aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
874 if (scsi_add_host(shpnt, pdev))
877 scsi_scan_host(shpnt);
881 if (shpnt->dma_channel != 0xff)
882 free_dma(shpnt->dma_channel);
884 free_irq(shpnt->irq, shpnt);
886 scsi_host_put(shpnt);
888 release_region(base_io, AHA1542_REGION_SIZE);
893 static int aha1542_release(struct Scsi_Host *shost)
895 scsi_remove_host(shost);
896 if (shost->dma_channel != 0xff)
897 free_dma(shost->dma_channel);
899 free_irq(shost->irq, shost);
900 if (shost->io_port && shost->n_io_port)
901 release_region(shost->io_port, shost->n_io_port);
902 scsi_host_put(shost);
908 * This is a device reset. This is handled by sending a special command
911 static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
913 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
915 struct mailbox *mb = aha1542->mb;
916 u8 target = SCpnt->device->id;
917 u8 lun = SCpnt->device->lun;
919 struct ccb *ccb = aha1542->ccb;
921 spin_lock_irqsave(&aha1542_lock, flags);
922 mbo = aha1542->aha1542_last_mbo_used + 1;
923 if (mbo >= AHA1542_MAILBOXES)
927 if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
930 if (mbo >= AHA1542_MAILBOXES)
932 } while (mbo != aha1542->aha1542_last_mbo_used);
934 if (mb[mbo].status || aha1542->SCint[mbo])
935 panic("Unable to find empty mailbox for aha1542.\n");
937 aha1542->SCint[mbo] = SCpnt; /* This will effectively
938 prevent someone else from
939 screwing with this cdb. */
941 aha1542->aha1542_last_mbo_used = mbo;
942 spin_unlock_irqrestore(&aha1542_lock, flags);
944 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
946 memset(&ccb[mbo], 0, sizeof(struct ccb));
948 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
950 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
952 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
953 ccb[mbo].commlinkid = 0;
956 * Now tell the 1542 to flush all pending commands for this
959 aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
961 scmd_printk(KERN_WARNING, SCpnt,
962 "Trying device reset for target\n");
967 static int aha1542_reset(Scsi_Cmnd *SCpnt, u8 reset_cmd)
969 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
973 * This does a scsi reset for all devices on the bus.
974 * In principle, we could also reset the 1542 - should
975 * we do this? Try this first, and we can add that later
976 * if it turns out to be useful.
978 outb(reset_cmd, CONTROL(SCpnt->device->host->io_port));
981 * Wait for the thing to settle down a bit. Unfortunately
982 * this is going to basically lock up the machine while we
983 * wait for this to complete. To be 100% correct, we need to
984 * check for timeout, and if we are doing something like this
985 * we are pretty desperate anyways.
988 spin_lock_irq(SCpnt->device->host->host_lock);
990 if (!wait_mask(STATUS(SCpnt->device->host->io_port),
991 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
992 spin_unlock_irq(SCpnt->device->host->host_lock);
996 * We need to do this too before the 1542 can interact with
997 * us again after host reset.
999 if (reset_cmd & HRST)
1000 setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
1002 * Now try to pick up the pieces. For all pending commands,
1003 * free any internal data structures, and basically clear things
1004 * out. We do not try and restart any commands or anything -
1005 * the strategy handler takes care of that crap.
1007 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1009 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1010 if (aha1542->SCint[i] != NULL) {
1012 SCtmp = aha1542->SCint[i];
1014 if (SCtmp->device->soft_reset) {
1016 * If this device implements the soft reset option,
1017 * then it is still holding onto the command, and
1018 * may yet complete it. In this case, we don't
1023 kfree(SCtmp->host_scribble);
1024 SCtmp->host_scribble = NULL;
1025 aha1542->SCint[i] = NULL;
1026 aha1542->mb[i].status = 0;
1030 spin_unlock_irq(SCpnt->device->host->host_lock);
1034 static int aha1542_bus_reset(Scsi_Cmnd *SCpnt)
1036 return aha1542_reset(SCpnt, SCRST);
1039 static int aha1542_host_reset(Scsi_Cmnd *SCpnt)
1041 return aha1542_reset(SCpnt, HRST | SCRST);
1044 static int aha1542_biosparam(struct scsi_device *sdev,
1045 struct block_device *bdev, sector_t capacity, int geom[])
1047 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1049 if (capacity >= 0x200000 &&
1050 aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1051 /* Please verify that this is the same as what DOS returns */
1052 geom[0] = 255; /* heads */
1053 geom[1] = 63; /* sectors */
1055 geom[0] = 64; /* heads */
1056 geom[1] = 32; /* sectors */
1058 geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
1062 MODULE_LICENSE("GPL");
1064 static struct scsi_host_template driver_template = {
1065 .module = THIS_MODULE,
1066 .proc_name = "aha1542",
1067 .name = "Adaptec 1542",
1068 .queuecommand = aha1542_queuecommand,
1069 .eh_device_reset_handler= aha1542_dev_reset,
1070 .eh_bus_reset_handler = aha1542_bus_reset,
1071 .eh_host_reset_handler = aha1542_host_reset,
1072 .bios_param = aha1542_biosparam,
1073 .can_queue = AHA1542_MAILBOXES,
1077 .unchecked_isa_dma = 1,
1078 .use_clustering = ENABLE_CLUSTERING,
1081 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1083 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1088 dev_set_drvdata(pdev, sh);
1092 static int aha1542_isa_remove(struct device *pdev,
1095 aha1542_release(dev_get_drvdata(pdev));
1096 dev_set_drvdata(pdev, NULL);
1100 static struct isa_driver aha1542_isa_driver = {
1101 .match = aha1542_isa_match,
1102 .remove = aha1542_isa_remove,
1107 static int isa_registered;
1110 static struct pnp_device_id aha1542_pnp_ids[] = {
1111 { .id = "ADP1542" },
1114 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1116 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1119 struct Scsi_Host *sh;
1121 for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1125 if (pnp_activate_dev(pdev) < 0)
1128 io[indx] = pnp_port_start(pdev, 0);
1130 /* The card can be queried for its DMA, we have
1131 the DMA set up that is enough */
1133 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", io[indx]);
1136 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1140 pnp_set_drvdata(pdev, sh);
1144 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1146 aha1542_release(pnp_get_drvdata(pdev));
1147 pnp_set_drvdata(pdev, NULL);
1150 static struct pnp_driver aha1542_pnp_driver = {
1152 .id_table = aha1542_pnp_ids,
1153 .probe = aha1542_pnp_probe,
1154 .remove = aha1542_pnp_remove,
1156 static int pnp_registered;
1157 #endif /* CONFIG_PNP */
1159 static int __init aha1542_init(void)
1165 ret = pnp_register_driver(&aha1542_pnp_driver);
1170 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1184 static void __exit aha1542_exit(void)
1188 pnp_unregister_driver(&aha1542_pnp_driver);
1191 isa_unregister_driver(&aha1542_isa_driver);
1194 module_init(aha1542_init);
1195 module_exit(aha1542_exit);