2 * Driver for Adaptec AHA-1542 SCSI host adapters
4 * Copyright (C) 1992 Tommy Thorn
5 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
6 * Copyright (C) 2015 Ondrej Zary
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/ioport.h>
15 #include <linux/delay.h>
16 #include <linux/proc_fs.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/isa.h>
20 #include <linux/pnp.h>
21 #include <linux/blkdev.h>
22 #include <linux/slab.h>
28 #include <scsi/scsi_host.h>
30 #include <linux/stat.h>
39 static bool isapnp = 1;
40 module_param(isapnp, bool, 0);
41 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
43 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
44 module_param_array(io, int, NULL, 0);
45 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
47 /* time AHA spends on the AT-bus during data transfer */
48 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
49 module_param_array(bus_on, int, NULL, 0);
50 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
52 /* time AHA spends off the bus (not to monopolize it) during data transfer */
53 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
54 module_param_array(bus_off, int, NULL, 0);
55 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
57 /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
58 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
59 module_param_array(dma_speed, int, NULL, 0);
60 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
62 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
63 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
65 struct aha1542_hostdata {
66 /* This will effectively start both of them at the first mailbox */
67 int bios_translation; /* Mapping bios uses - for compatibility */
68 int aha1542_last_mbi_used;
69 int aha1542_last_mbo_used;
70 struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
71 struct mailbox mb[2 * AHA1542_MAILBOXES];
72 struct ccb ccb[AHA1542_MAILBOXES];
75 static DEFINE_SPINLOCK(aha1542_lock);
77 static inline void aha1542_intr_reset(u16 base)
79 outb(IRST, CONTROL(base));
82 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
92 u8 bits = inb(port) & mask;
93 if ((bits & allof) == allof && ((bits & noneof) == 0))
104 /* This is a bit complicated, but we need to make sure that an interrupt
105 routine does not send something out while we are in the middle of this.
106 Fortunately, it is only at boot time that multi-byte messages
108 static int aha1542_outb(unsigned int base, u8 val)
113 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
114 printk(KERN_ERR "aha1542_outb failed");
117 spin_lock_irqsave(&aha1542_lock, flags);
118 if (inb(STATUS(base)) & CDF) {
119 spin_unlock_irqrestore(&aha1542_lock, flags);
122 outb(val, DATA(base));
123 spin_unlock_irqrestore(&aha1542_lock, flags);
128 static int aha1542_out(unsigned int base, u8 *buf, int len)
132 spin_lock_irqsave(&aha1542_lock, flags);
134 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
135 spin_unlock_irqrestore(&aha1542_lock, flags);
136 printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
139 outb(*buf++, DATA(base));
141 spin_unlock_irqrestore(&aha1542_lock, flags);
142 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
148 /* Only used at boot time, so we do not need to worry about latency as much
151 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
155 spin_lock_irqsave(&aha1542_lock, flags);
157 if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
158 spin_unlock_irqrestore(&aha1542_lock, flags);
160 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
163 *buf++ = inb(DATA(base));
165 spin_unlock_irqrestore(&aha1542_lock, flags);
169 static int makecode(unsigned hosterr, unsigned scsierr)
173 case 0xa: /* Linked command complete without error and linked normally */
174 case 0xb: /* Linked command complete without error, interrupt generated */
178 case 0x11: /* Selection time out-The initiator selection or target
179 reselection was not complete within the SCSI Time out period */
180 hosterr = DID_TIME_OUT;
183 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
184 than was allocated by the Data Length field or the sum of the
185 Scatter / Gather Data Length fields. */
187 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
189 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
190 invalid. This usually indicates a software failure. */
192 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
193 This usually indicates a software failure. */
195 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
196 of linked CCB's does not specify the same logical unit number as
198 case 0x18: /* Invalid Target Direction received from Host-The direction of a
199 Target Mode CCB was invalid. */
201 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
202 received to service data transfer between the same target LUN
203 and initiator SCSI ID in the same direction. */
205 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
206 length segment or invalid segment list boundaries was received.
207 A CCB parameter was invalid. */
208 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
209 hosterr = DID_ERROR; /* Couldn't find any better */
212 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
213 phase sequence was requested by the target. The host adapter
214 will generate a SCSI Reset Condition, notifying the host with
219 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
222 return scsierr | (hosterr << 16);
225 static int aha1542_test_port(int bse, struct Scsi_Host *sh)
227 u8 inquiry_result[4];
230 /* Quick and dirty test for presence of the card. */
231 if (inb(STATUS(bse)) == 0xff)
234 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
236 /* In case some other card was probing here, reset interrupts */
237 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
239 outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
241 mdelay(20); /* Wait a little bit for things to settle down. */
243 /* Expect INIT and IDLE, any of the others are bad */
244 if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
247 /* Shouldn't have generated any interrupts during reset */
248 if (inb(INTRFLAGS(bse)) & INTRMASK)
251 /* Perform a host adapter inquiry instead so we do not need to set
252 up the mailboxes ahead of time */
254 aha1542_outb(bse, CMD_INQUIRY);
256 for (i = 0; i < 4; i++) {
257 if (!wait_mask(STATUS(bse), DF, DF, 0, 0))
259 inquiry_result[i] = inb(DATA(bse));
262 /* Reading port should reset DF */
263 if (inb(STATUS(bse)) & DF)
266 /* When HACC, command is completed, and we're though testing */
267 if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0))
270 /* Clear interrupts */
271 outb(IRST, CONTROL(bse));
276 /* A "high" level interrupt handler */
277 static void aha1542_intr_handle(struct Scsi_Host *sh)
279 struct aha1542_hostdata *aha1542 = shost_priv(sh);
280 void (*my_done)(struct scsi_cmnd *) = NULL;
281 int errstatus, mbi, mbo, mbistatus;
284 struct scsi_cmnd *tmp_cmd;
286 struct mailbox *mb = aha1542->mb;
287 struct ccb *ccb = aha1542->ccb;
291 flag = inb(INTRFLAGS(sh->io_port));
292 printk(KERN_DEBUG "aha1542_intr_handle: ");
293 if (!(flag & ANYINTR))
294 printk("no interrupt?");
303 printk("status %02x\n", inb(STATUS(sh->io_port)));
309 flag = inb(INTRFLAGS(sh->io_port));
311 /* Check for unusual interrupts. If any of these happen, we should
312 probably do something special, but for now just printing a message
313 is sufficient. A SCSI reset detected is something that we really
314 need to deal with in some way. */
323 aha1542_intr_reset(sh->io_port);
325 spin_lock_irqsave(&aha1542_lock, flags);
326 mbi = aha1542->aha1542_last_mbi_used + 1;
327 if (mbi >= 2 * AHA1542_MAILBOXES)
328 mbi = AHA1542_MAILBOXES;
331 if (mb[mbi].status != 0)
334 if (mbi >= 2 * AHA1542_MAILBOXES)
335 mbi = AHA1542_MAILBOXES;
336 } while (mbi != aha1542->aha1542_last_mbi_used);
338 if (mb[mbi].status == 0) {
339 spin_unlock_irqrestore(&aha1542_lock, flags);
340 /* Hmm, no mail. Must have read it the last time around */
341 if (!number_serviced)
342 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
346 mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
347 mbistatus = mb[mbi].status;
349 aha1542->aha1542_last_mbi_used = mbi;
350 spin_unlock_irqrestore(&aha1542_lock, flags);
354 if (ccb[mbo].tarstat | ccb[mbo].hastat)
355 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
356 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
361 continue; /* Aborted command not found */
364 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
367 tmp_cmd = aha1542->int_cmds[mbo];
369 if (!tmp_cmd || !tmp_cmd->scsi_done) {
370 printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
371 printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
372 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
375 my_done = tmp_cmd->scsi_done;
376 kfree(tmp_cmd->host_scribble);
377 tmp_cmd->host_scribble = NULL;
378 /* Fetch the sense data, and tuck it away, in the required slot. The
379 Adaptec automatically fetches it, and there is no guarantee that
380 we will still have it in the cdb when we come back */
381 if (ccb[mbo].tarstat == 2)
382 memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
383 SCSI_SENSE_BUFFERSIZE);
386 /* is there mail :-) */
388 /* more error checking left out here */
390 /* This is surely wrong, but I don't know what's right */
391 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
397 printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
398 ccb[mbo].hastat, ccb[mbo].tarstat);
401 if (ccb[mbo].tarstat == 2) {
405 DEB(printk("aha1542_intr_handle: sense:"));
407 for (i = 0; i < 12; i++)
408 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
412 DEB(printk("aha1542_intr_handle: buf:"));
413 for (i = 0; i < bufflen; i++)
414 printk("%02x ", ((unchar *)buff)[i]);
418 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
419 tmp_cmd->result = errstatus;
420 aha1542->int_cmds[mbo] = NULL; /* This effectively frees up the mailbox slot, as
421 far as queuecommand is concerned */
427 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
428 static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
431 struct Scsi_Host *sh = dev_id;
433 spin_lock_irqsave(sh->host_lock, flags);
434 aha1542_intr_handle(sh);
435 spin_unlock_irqrestore(sh->host_lock, flags);
439 static int aha1542_queuecommand_lck(struct scsi_cmnd *cmd, void (*done) (struct scsi_cmnd *))
441 struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
443 u8 target = cmd->device->id;
444 u8 lun = cmd->device->lun;
446 int bufflen = scsi_bufflen(cmd);
448 struct mailbox *mb = aha1542->mb;
449 struct ccb *ccb = aha1542->ccb;
453 DEB(if (target > 1) {
454 cmd->result = DID_TIME_OUT << 16;
459 if (*cmd->cmnd == REQUEST_SENSE) {
460 /* Don't do the command - we have the sense data already */
466 if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
467 i = xscsi2int(cmd->cmnd + 2);
468 else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
469 i = scsi2int(cmd->cmnd + 2);
473 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
475 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
476 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
477 for (i = 0; i < cmd->cmd_len; i++)
478 printk("%02x ", cmd->cmnd[i]);
480 if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
481 return 0; /* we are still testing, so *don't* write */
483 /* Use the outgoing mailboxes in a round-robin fashion, because this
484 is how the host adapter will scan for them */
486 spin_lock_irqsave(&aha1542_lock, flags);
487 mbo = aha1542->aha1542_last_mbo_used + 1;
488 if (mbo >= AHA1542_MAILBOXES)
492 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
495 if (mbo >= AHA1542_MAILBOXES)
497 } while (mbo != aha1542->aha1542_last_mbo_used);
499 if (mb[mbo].status || aha1542->int_cmds[mbo])
500 panic("Unable to find empty mailbox for aha1542.\n");
502 aha1542->int_cmds[mbo] = cmd; /* This will effectively prevent someone else from
503 screwing with this cdb. */
505 aha1542->aha1542_last_mbo_used = mbo;
506 spin_unlock_irqrestore(&aha1542_lock, flags);
509 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
512 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
514 memset(&ccb[mbo], 0, sizeof(struct ccb));
516 ccb[mbo].cdblen = cmd->cmd_len;
519 if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
521 else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
524 memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
527 struct scatterlist *sg;
532 int i, sg_count = scsi_sg_count(cmd);
533 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
534 cmd->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
535 GFP_KERNEL | GFP_DMA);
536 cptr = (struct chain *) cmd->host_scribble;
538 /* free the claimed mailbox slot */
539 aha1542->int_cmds[mbo] = NULL;
540 return SCSI_MLQUEUE_HOST_BUSY;
542 scsi_for_each_sg(cmd, sg, sg_count, i) {
543 any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
545 any2scsi(cptr[i].datalen, sg->length);
547 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
548 any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
550 printk("cptr %x: ", cptr);
551 ptr = (unsigned char *) cptr;
552 for (i = 0; i < 18; i++)
553 printk("%02x ", ptr[i]);
556 ccb[mbo].op = 0; /* SCSI Initiator Command */
557 cmd->host_scribble = NULL;
558 any2scsi(ccb[mbo].datalen, 0);
559 any2scsi(ccb[mbo].dataptr, 0);
561 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
562 ccb[mbo].rsalen = 16;
563 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
564 ccb[mbo].commlinkid = 0;
569 printk(KERN_DEBUG "aha1542_command: sending.. ");
570 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
571 printk("%02x ", ((u8 *) &ccb[mbo])[i]);
576 DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
577 cmd->scsi_done = done;
579 aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
581 printk("aha1542_queuecommand: done can't be NULL\n");
586 static DEF_SCSI_QCMD(aha1542_queuecommand)
588 /* Initialize mailboxes */
589 static void setup_mailboxes(int bse, struct Scsi_Host *sh)
591 struct aha1542_hostdata *aha1542 = shost_priv(sh);
593 struct mailbox *mb = aha1542->mb;
594 struct ccb *ccb = aha1542->ccb;
596 u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
598 for (i = 0; i < AHA1542_MAILBOXES; i++) {
599 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
600 any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
602 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
603 any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
604 if (aha1542_out(bse, mb_cmd, 5))
605 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
606 aha1542_intr_reset(bse);
609 static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id)
611 u8 inquiry_result[3];
613 i = inb(STATUS(base_io));
615 i = inb(DATA(base_io));
617 aha1542_outb(base_io, CMD_RETCONF);
618 aha1542_in(base_io, inquiry_result, 3, 0);
619 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
620 printk(KERN_ERR "aha1542_detect: query board settings\n");
621 aha1542_intr_reset(base_io);
622 switch (inquiry_result[0]) {
636 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
637 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
641 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
644 switch (inquiry_result[1]) {
664 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
667 *scsi_id = inquiry_result[2] & 7;
671 /* This function should only be called for 1542C boards - we can detect
672 the special firmware settings and unlock the board */
674 static int aha1542_mbenable(int base)
676 static u8 mbenable_cmd[3];
677 static u8 mbenable_result[2];
680 retval = BIOS_TRANSLATION_6432;
682 aha1542_outb(base, CMD_EXTBIOS);
683 if (aha1542_in(base, mbenable_result, 2, 100))
685 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
687 aha1542_intr_reset(base);
689 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
690 mbenable_cmd[0] = CMD_MBENABLE;
692 mbenable_cmd[2] = mbenable_result[1];
694 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
695 retval = BIOS_TRANSLATION_25563;
697 if (aha1542_out(base, mbenable_cmd, 3))
702 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
704 aha1542_intr_reset(base);
708 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
709 static int aha1542_query(int base_io, int *transl)
711 u8 inquiry_result[4];
713 i = inb(STATUS(base_io));
715 i = inb(DATA(base_io));
717 aha1542_outb(base_io, CMD_INQUIRY);
718 aha1542_in(base_io, inquiry_result, 4, 0);
719 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
720 printk(KERN_ERR "aha1542_detect: query card type\n");
721 aha1542_intr_reset(base_io);
723 *transl = BIOS_TRANSLATION_6432; /* Default case */
725 /* For an AHA1740 series board, we ignore the board since there is a
726 hardware bug which can lead to wrong blocks being returned if the board
727 is operating in the 1542 emulation mode. Since there is an extended mode
728 driver, we simply ignore the board and let the 1740 driver pick it up.
731 if (inquiry_result[0] == 0x43) {
732 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
736 /* Always call this - boards that do not support extended bios translation
737 will ignore the command, and we will set the proper default */
739 *transl = aha1542_mbenable(base_io);
744 static u8 dma_speed_hw(int dma_speed)
759 return 0xff; /* invalid */
762 /* Set the Bus on/off-times as not to ruin floppy performance */
763 static void aha1542_set_bus_times(int indx)
765 unsigned int base_io = io[indx];
767 if (bus_on[indx] > 0) {
768 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on[indx], 2, 15) };
770 aha1542_intr_reset(base_io);
771 if (aha1542_out(base_io, oncmd, 2))
775 if (bus_off[indx] > 0) {
776 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off[indx], 1, 64) };
778 aha1542_intr_reset(base_io);
779 if (aha1542_out(base_io, offcmd, 2))
783 if (dma_speed_hw(dma_speed[indx]) != 0xff) {
784 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed[indx]) };
786 aha1542_intr_reset(base_io);
787 if (aha1542_out(base_io, dmacmd, 2))
790 aha1542_intr_reset(base_io);
793 printk(KERN_ERR "setting bus on/off-time failed\n");
794 aha1542_intr_reset(base_io);
797 /* return non-zero on detection */
798 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
800 unsigned int base_io = io[indx];
801 struct Scsi_Host *sh;
802 struct aha1542_hostdata *aha1542;
807 if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
810 sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
813 aha1542 = shost_priv(sh);
815 if (!aha1542_test_port(base_io, sh))
818 aha1542_set_bus_times(indx);
819 if (aha1542_query(base_io, &aha1542->bios_translation))
821 if (aha1542_getconfig(base_io, &sh->irq, &sh->dma_channel, &sh->this_id) == -1)
824 printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", sh->this_id, base_io, sh->irq);
825 if (sh->dma_channel != 0xFF)
826 printk(", DMA %d", sh->dma_channel);
828 if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
829 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
831 setup_mailboxes(base_io, sh);
833 if (request_irq(sh->irq, do_aha1542_intr_handle, 0,
835 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
838 if (sh->dma_channel != 0xFF) {
839 if (request_dma(sh->dma_channel, "aha1542")) {
840 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
843 if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
844 set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
845 enable_dma(sh->dma_channel);
849 sh->unique_id = base_io;
850 sh->io_port = base_io;
851 sh->n_io_port = AHA1542_REGION_SIZE;
852 aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
853 aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
855 if (scsi_add_host(sh, pdev))
862 if (sh->dma_channel != 0xff)
863 free_dma(sh->dma_channel);
865 free_irq(sh->irq, sh);
869 release_region(base_io, AHA1542_REGION_SIZE);
874 static int aha1542_release(struct Scsi_Host *sh)
876 scsi_remove_host(sh);
877 if (sh->dma_channel != 0xff)
878 free_dma(sh->dma_channel);
880 free_irq(sh->irq, sh);
881 if (sh->io_port && sh->n_io_port)
882 release_region(sh->io_port, sh->n_io_port);
889 * This is a device reset. This is handled by sending a special command
892 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
894 struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
896 struct mailbox *mb = aha1542->mb;
897 u8 target = cmd->device->id;
898 u8 lun = cmd->device->lun;
900 struct ccb *ccb = aha1542->ccb;
902 spin_lock_irqsave(&aha1542_lock, flags);
903 mbo = aha1542->aha1542_last_mbo_used + 1;
904 if (mbo >= AHA1542_MAILBOXES)
908 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
911 if (mbo >= AHA1542_MAILBOXES)
913 } while (mbo != aha1542->aha1542_last_mbo_used);
915 if (mb[mbo].status || aha1542->int_cmds[mbo])
916 panic("Unable to find empty mailbox for aha1542.\n");
918 aha1542->int_cmds[mbo] = cmd; /* This will effectively
919 prevent someone else from
920 screwing with this cdb. */
922 aha1542->aha1542_last_mbo_used = mbo;
923 spin_unlock_irqrestore(&aha1542_lock, flags);
925 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
927 memset(&ccb[mbo], 0, sizeof(struct ccb));
929 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
931 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
933 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
934 ccb[mbo].commlinkid = 0;
937 * Now tell the 1542 to flush all pending commands for this
940 aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
942 scmd_printk(KERN_WARNING, cmd,
943 "Trying device reset for target\n");
948 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
950 struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
954 * This does a scsi reset for all devices on the bus.
955 * In principle, we could also reset the 1542 - should
956 * we do this? Try this first, and we can add that later
957 * if it turns out to be useful.
959 outb(reset_cmd, CONTROL(cmd->device->host->io_port));
962 * Wait for the thing to settle down a bit. Unfortunately
963 * this is going to basically lock up the machine while we
964 * wait for this to complete. To be 100% correct, we need to
965 * check for timeout, and if we are doing something like this
966 * we are pretty desperate anyways.
969 spin_lock_irq(cmd->device->host->host_lock);
971 if (!wait_mask(STATUS(cmd->device->host->io_port),
972 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
973 spin_unlock_irq(cmd->device->host->host_lock);
977 * We need to do this too before the 1542 can interact with
978 * us again after host reset.
980 if (reset_cmd & HRST)
981 setup_mailboxes(cmd->device->host->io_port, cmd->device->host);
983 * Now try to pick up the pieces. For all pending commands,
984 * free any internal data structures, and basically clear things
985 * out. We do not try and restart any commands or anything -
986 * the strategy handler takes care of that crap.
988 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
990 for (i = 0; i < AHA1542_MAILBOXES; i++) {
991 if (aha1542->int_cmds[i] != NULL) {
992 struct scsi_cmnd *tmp_cmd;
993 tmp_cmd = aha1542->int_cmds[i];
995 if (tmp_cmd->device->soft_reset) {
997 * If this device implements the soft reset option,
998 * then it is still holding onto the command, and
999 * may yet complete it. In this case, we don't
1004 kfree(tmp_cmd->host_scribble);
1005 tmp_cmd->host_scribble = NULL;
1006 aha1542->int_cmds[i] = NULL;
1007 aha1542->mb[i].status = 0;
1011 spin_unlock_irq(cmd->device->host->host_lock);
1015 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
1017 return aha1542_reset(cmd, SCRST);
1020 static int aha1542_host_reset(struct scsi_cmnd *cmd)
1022 return aha1542_reset(cmd, HRST | SCRST);
1025 static int aha1542_biosparam(struct scsi_device *sdev,
1026 struct block_device *bdev, sector_t capacity, int geom[])
1028 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1030 if (capacity >= 0x200000 &&
1031 aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1032 /* Please verify that this is the same as what DOS returns */
1033 geom[0] = 255; /* heads */
1034 geom[1] = 63; /* sectors */
1036 geom[0] = 64; /* heads */
1037 geom[1] = 32; /* sectors */
1039 geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
1043 MODULE_LICENSE("GPL");
1045 static struct scsi_host_template driver_template = {
1046 .module = THIS_MODULE,
1047 .proc_name = "aha1542",
1048 .name = "Adaptec 1542",
1049 .queuecommand = aha1542_queuecommand,
1050 .eh_device_reset_handler= aha1542_dev_reset,
1051 .eh_bus_reset_handler = aha1542_bus_reset,
1052 .eh_host_reset_handler = aha1542_host_reset,
1053 .bios_param = aha1542_biosparam,
1054 .can_queue = AHA1542_MAILBOXES,
1058 .unchecked_isa_dma = 1,
1059 .use_clustering = ENABLE_CLUSTERING,
1062 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1064 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1069 dev_set_drvdata(pdev, sh);
1073 static int aha1542_isa_remove(struct device *pdev,
1076 aha1542_release(dev_get_drvdata(pdev));
1077 dev_set_drvdata(pdev, NULL);
1081 static struct isa_driver aha1542_isa_driver = {
1082 .match = aha1542_isa_match,
1083 .remove = aha1542_isa_remove,
1088 static int isa_registered;
1091 static struct pnp_device_id aha1542_pnp_ids[] = {
1092 { .id = "ADP1542" },
1095 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1097 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1100 struct Scsi_Host *sh;
1102 for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1106 if (pnp_activate_dev(pdev) < 0)
1109 io[indx] = pnp_port_start(pdev, 0);
1111 /* The card can be queried for its DMA, we have
1112 the DMA set up that is enough */
1114 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", io[indx]);
1117 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1121 pnp_set_drvdata(pdev, sh);
1125 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1127 aha1542_release(pnp_get_drvdata(pdev));
1128 pnp_set_drvdata(pdev, NULL);
1131 static struct pnp_driver aha1542_pnp_driver = {
1133 .id_table = aha1542_pnp_ids,
1134 .probe = aha1542_pnp_probe,
1135 .remove = aha1542_pnp_remove,
1137 static int pnp_registered;
1138 #endif /* CONFIG_PNP */
1140 static int __init aha1542_init(void)
1146 ret = pnp_register_driver(&aha1542_pnp_driver);
1151 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1165 static void __exit aha1542_exit(void)
1169 pnp_unregister_driver(&aha1542_pnp_driver);
1172 isa_unregister_driver(&aha1542_isa_driver);
1175 module_init(aha1542_init);
1176 module_exit(aha1542_exit);