1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Adaptec AHA-1542 SCSI host adapters
5 * Copyright (C) 1992 Tommy Thorn
6 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
7 * Copyright (C) 2015 Ondrej Zary
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/isa.h>
19 #include <linux/pnp.h>
20 #include <linux/slab.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_device.h>
25 #include <scsi/scsi_host.h>
30 static bool isapnp = 1;
31 module_param(isapnp, bool, 0);
32 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
34 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
35 module_param_hw_array(io, int, ioport, NULL, 0);
36 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
38 /* time AHA spends on the AT-bus during data transfer */
39 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
40 module_param_array(bus_on, int, NULL, 0);
41 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
43 /* time AHA spends off the bus (not to monopolize it) during data transfer */
44 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
45 module_param_array(bus_off, int, NULL, 0);
46 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
48 /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
49 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
50 module_param_array(dma_speed, int, NULL, 0);
51 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
53 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
54 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
56 struct aha1542_hostdata {
57 /* This will effectively start both of them at the first mailbox */
58 int bios_translation; /* Mapping bios uses - for compatibility */
59 int aha1542_last_mbi_used;
60 int aha1542_last_mbo_used;
61 struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
65 dma_addr_t ccb_handle;
70 dma_addr_t chain_handle;
73 static inline void aha1542_intr_reset(u16 base)
75 outb(IRST, CONTROL(base));
78 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
88 u8 bits = inb(port) & mask;
89 if ((bits & allof) == allof && ((bits & noneof) == 0))
100 static int aha1542_outb(unsigned int base, u8 val)
102 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
104 outb(val, DATA(base));
109 static int aha1542_out(unsigned int base, u8 *buf, int len)
112 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
114 outb(*buf++, DATA(base));
116 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
123 * Only used at boot time, so we do not need to worry about latency as much
127 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
130 if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
132 *buf++ = inb(DATA(base));
137 static int makecode(unsigned hosterr, unsigned scsierr)
141 case 0xa: /* Linked command complete without error and linked normally */
142 case 0xb: /* Linked command complete without error, interrupt generated */
146 case 0x11: /* Selection time out-The initiator selection or target
147 * reselection was not complete within the SCSI Time out period
149 hosterr = DID_TIME_OUT;
152 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
153 * than was allocated by the Data Length field or the sum of the
154 * Scatter / Gather Data Length fields.
157 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
159 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
160 * invalid. This usually indicates a software failure.
163 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
164 * This usually indicates a software failure.
167 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
168 * of linked CCB's does not specify the same logical unit number as
171 case 0x18: /* Invalid Target Direction received from Host-The direction of a
172 * Target Mode CCB was invalid.
175 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
176 * received to service data transfer between the same target LUN
177 * and initiator SCSI ID in the same direction.
180 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
181 * length segment or invalid segment list boundaries was received.
182 * A CCB parameter was invalid.
185 printk("Aha1542: %x %x\n", hosterr, scsierr);
187 hosterr = DID_ERROR; /* Couldn't find any better */
190 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
191 * phase sequence was requested by the target. The host adapter
192 * will generate a SCSI Reset Condition, notifying the host with
198 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
201 return scsierr | (hosterr << 16);
204 static int aha1542_test_port(struct Scsi_Host *sh)
206 u8 inquiry_result[4];
209 /* Quick and dirty test for presence of the card. */
210 if (inb(STATUS(sh->io_port)) == 0xff)
213 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
215 /* In case some other card was probing here, reset interrupts */
216 aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
218 outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
220 mdelay(20); /* Wait a little bit for things to settle down. */
222 /* Expect INIT and IDLE, any of the others are bad */
223 if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
226 /* Shouldn't have generated any interrupts during reset */
227 if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
231 * Perform a host adapter inquiry instead so we do not need to set
232 * up the mailboxes ahead of time
235 aha1542_outb(sh->io_port, CMD_INQUIRY);
237 for (i = 0; i < 4; i++) {
238 if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
240 inquiry_result[i] = inb(DATA(sh->io_port));
243 /* Reading port should reset DF */
244 if (inb(STATUS(sh->io_port)) & DF)
247 /* When HACC, command is completed, and we're though testing */
248 if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
251 /* Clear interrupts */
252 outb(IRST, CONTROL(sh->io_port));
257 static void aha1542_free_cmd(struct scsi_cmnd *cmd)
259 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
260 struct device *dev = cmd->device->host->dma_dev;
261 size_t len = scsi_sg_count(cmd) * sizeof(struct chain);
264 dma_unmap_single(dev, acmd->chain_handle, len, DMA_TO_DEVICE);
272 static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
274 struct Scsi_Host *sh = dev_id;
275 struct aha1542_hostdata *aha1542 = shost_priv(sh);
276 void (*my_done)(struct scsi_cmnd *) = NULL;
277 int errstatus, mbi, mbo, mbistatus;
280 struct scsi_cmnd *tmp_cmd;
282 struct mailbox *mb = aha1542->mb;
283 struct ccb *ccb = aha1542->ccb;
287 flag = inb(INTRFLAGS(sh->io_port));
288 shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
289 if (!(flag & ANYINTR))
290 printk("no interrupt?");
299 printk("status %02x\n", inb(STATUS(sh->io_port)));
304 spin_lock_irqsave(sh->host_lock, flags);
306 flag = inb(INTRFLAGS(sh->io_port));
309 * Check for unusual interrupts. If any of these happen, we should
310 * probably do something special, but for now just printing a message
311 * is sufficient. A SCSI reset detected is something that we really
312 * need to deal with in some way.
322 aha1542_intr_reset(sh->io_port);
324 mbi = aha1542->aha1542_last_mbi_used + 1;
325 if (mbi >= 2 * AHA1542_MAILBOXES)
326 mbi = AHA1542_MAILBOXES;
329 if (mb[mbi].status != 0)
332 if (mbi >= 2 * AHA1542_MAILBOXES)
333 mbi = AHA1542_MAILBOXES;
334 } while (mbi != aha1542->aha1542_last_mbi_used);
336 if (mb[mbi].status == 0) {
337 spin_unlock_irqrestore(sh->host_lock, flags);
338 /* Hmm, no mail. Must have read it the last time around */
339 if (!number_serviced)
340 shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
344 mbo = (scsi2int(mb[mbi].ccbptr) - (unsigned long)aha1542->ccb_handle) / sizeof(struct ccb);
345 mbistatus = mb[mbi].status;
347 aha1542->aha1542_last_mbi_used = mbi;
350 if (ccb[mbo].tarstat | ccb[mbo].hastat)
351 shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
352 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
356 continue; /* Aborted command not found */
359 shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
362 tmp_cmd = aha1542->int_cmds[mbo];
364 if (!tmp_cmd || !tmp_cmd->scsi_done) {
365 spin_unlock_irqrestore(sh->host_lock, flags);
366 shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
367 shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
368 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
371 my_done = tmp_cmd->scsi_done;
372 aha1542_free_cmd(tmp_cmd);
374 * Fetch the sense data, and tuck it away, in the required slot. The
375 * Adaptec automatically fetches it, and there is no guarantee that
376 * we will still have it in the cdb when we come back
378 if (ccb[mbo].tarstat == 2)
379 memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
380 SCSI_SENSE_BUFFERSIZE);
383 /* is there mail :-) */
385 /* more error checking left out here */
387 /* This is surely wrong, but I don't know what's right */
388 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
394 shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
395 ccb[mbo].hastat, ccb[mbo].tarstat);
396 if (ccb[mbo].tarstat == 2)
397 print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
399 printk("aha1542_intr_handle: returning %6x\n", errstatus);
401 tmp_cmd->result = errstatus;
402 aha1542->int_cmds[mbo] = NULL; /* This effectively frees up the mailbox slot, as
403 * far as queuecommand is concerned
410 static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
412 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
413 struct aha1542_hostdata *aha1542 = shost_priv(sh);
415 u8 target = cmd->device->id;
416 u8 lun = cmd->device->lun;
418 int bufflen = scsi_bufflen(cmd);
420 struct mailbox *mb = aha1542->mb;
421 struct ccb *ccb = aha1542->ccb;
423 if (*cmd->cmnd == REQUEST_SENSE) {
424 /* Don't do the command - we have the sense data already */
432 if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
433 i = xscsi2int(cmd->cmnd + 2);
434 else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
435 i = scsi2int(cmd->cmnd + 2);
436 shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
437 target, *cmd->cmnd, i, bufflen);
438 print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
441 sg_count = scsi_dma_map(cmd);
443 size_t len = sg_count * sizeof(struct chain);
445 acmd->chain = kmalloc(len, GFP_DMA);
448 acmd->chain_handle = dma_map_single(sh->dma_dev, acmd->chain,
450 if (dma_mapping_error(sh->dma_dev, acmd->chain_handle))
455 * Use the outgoing mailboxes in a round-robin fashion, because this
456 * is how the host adapter will scan for them
459 spin_lock_irqsave(sh->host_lock, flags);
460 mbo = aha1542->aha1542_last_mbo_used + 1;
461 if (mbo >= AHA1542_MAILBOXES)
465 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
468 if (mbo >= AHA1542_MAILBOXES)
470 } while (mbo != aha1542->aha1542_last_mbo_used);
472 if (mb[mbo].status || aha1542->int_cmds[mbo])
473 panic("Unable to find empty mailbox for aha1542.\n");
475 aha1542->int_cmds[mbo] = cmd; /* This will effectively prevent someone else from
476 * screwing with this cdb.
479 aha1542->aha1542_last_mbo_used = mbo;
482 shost_printk(KERN_DEBUG, sh, "Sending command (%d %p)...", mbo, cmd->scsi_done);
485 /* This gets trashed for some reason */
486 any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
488 memset(&ccb[mbo], 0, sizeof(struct ccb));
490 ccb[mbo].cdblen = cmd->cmd_len;
493 if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
495 else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
498 memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
501 struct scatterlist *sg;
504 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
505 scsi_for_each_sg(cmd, sg, sg_count, i) {
506 any2scsi(acmd->chain[i].dataptr, sg_dma_address(sg));
507 any2scsi(acmd->chain[i].datalen, sg_dma_len(sg));
509 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
510 any2scsi(ccb[mbo].dataptr, acmd->chain_handle);
512 shost_printk(KERN_DEBUG, sh, "cptr %p: ", acmd->chain);
513 print_hex_dump_bytes("cptr: ", DUMP_PREFIX_NONE, acmd->chain, 18);
516 ccb[mbo].op = 0; /* SCSI Initiator Command */
517 any2scsi(ccb[mbo].datalen, 0);
518 any2scsi(ccb[mbo].dataptr, 0);
520 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
521 ccb[mbo].rsalen = 16;
522 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
523 ccb[mbo].commlinkid = 0;
526 print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
527 printk("aha1542_queuecommand: now waiting for interrupt ");
530 aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
531 spin_unlock_irqrestore(sh->host_lock, flags);
539 return SCSI_MLQUEUE_HOST_BUSY;
542 /* Initialize mailboxes */
543 static void setup_mailboxes(struct Scsi_Host *sh)
545 struct aha1542_hostdata *aha1542 = shost_priv(sh);
546 u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
549 for (i = 0; i < AHA1542_MAILBOXES; i++) {
550 aha1542->mb[i].status = 0;
551 any2scsi(aha1542->mb[i].ccbptr,
552 aha1542->ccb_handle + i * sizeof(struct ccb));
553 aha1542->mb[AHA1542_MAILBOXES + i].status = 0;
555 aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
556 any2scsi(mb_cmd + 2, aha1542->mb_handle);
557 if (aha1542_out(sh->io_port, mb_cmd, 5))
558 shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
559 aha1542_intr_reset(sh->io_port);
562 static int aha1542_getconfig(struct Scsi_Host *sh)
564 u8 inquiry_result[3];
566 i = inb(STATUS(sh->io_port));
568 i = inb(DATA(sh->io_port));
570 aha1542_outb(sh->io_port, CMD_RETCONF);
571 aha1542_in(sh->io_port, inquiry_result, 3, 0);
572 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
573 shost_printk(KERN_ERR, sh, "error querying board settings\n");
574 aha1542_intr_reset(sh->io_port);
575 switch (inquiry_result[0]) {
590 * This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
591 * Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this.
593 sh->dma_channel = 0xFF;
596 shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
599 switch (inquiry_result[1]) {
619 shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
622 sh->this_id = inquiry_result[2] & 7;
627 * This function should only be called for 1542C boards - we can detect
628 * the special firmware settings and unlock the board
631 static int aha1542_mbenable(struct Scsi_Host *sh)
633 static u8 mbenable_cmd[3];
634 static u8 mbenable_result[2];
637 retval = BIOS_TRANSLATION_6432;
639 aha1542_outb(sh->io_port, CMD_EXTBIOS);
640 if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
642 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
644 aha1542_intr_reset(sh->io_port);
646 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
647 mbenable_cmd[0] = CMD_MBENABLE;
649 mbenable_cmd[2] = mbenable_result[1];
651 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
652 retval = BIOS_TRANSLATION_25563;
654 if (aha1542_out(sh->io_port, mbenable_cmd, 3))
659 shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
661 aha1542_intr_reset(sh->io_port);
665 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
666 static int aha1542_query(struct Scsi_Host *sh)
668 struct aha1542_hostdata *aha1542 = shost_priv(sh);
669 u8 inquiry_result[4];
671 i = inb(STATUS(sh->io_port));
673 i = inb(DATA(sh->io_port));
675 aha1542_outb(sh->io_port, CMD_INQUIRY);
676 aha1542_in(sh->io_port, inquiry_result, 4, 0);
677 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
678 shost_printk(KERN_ERR, sh, "error querying card type\n");
679 aha1542_intr_reset(sh->io_port);
681 aha1542->bios_translation = BIOS_TRANSLATION_6432; /* Default case */
684 * For an AHA1740 series board, we ignore the board since there is a
685 * hardware bug which can lead to wrong blocks being returned if the board
686 * is operating in the 1542 emulation mode. Since there is an extended mode
687 * driver, we simply ignore the board and let the 1740 driver pick it up.
690 if (inquiry_result[0] == 0x43) {
691 shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
696 * Always call this - boards that do not support extended bios translation
697 * will ignore the command, and we will set the proper default
700 aha1542->bios_translation = aha1542_mbenable(sh);
705 static u8 dma_speed_hw(int dma_speed)
720 return 0xff; /* invalid */
723 /* Set the Bus on/off-times as not to ruin floppy performance */
724 static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
727 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
729 aha1542_intr_reset(sh->io_port);
730 if (aha1542_out(sh->io_port, oncmd, 2))
735 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
737 aha1542_intr_reset(sh->io_port);
738 if (aha1542_out(sh->io_port, offcmd, 2))
742 if (dma_speed_hw(dma_speed) != 0xff) {
743 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
745 aha1542_intr_reset(sh->io_port);
746 if (aha1542_out(sh->io_port, dmacmd, 2))
749 aha1542_intr_reset(sh->io_port);
752 shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
753 aha1542_intr_reset(sh->io_port);
756 /* return non-zero on detection */
757 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
759 unsigned int base_io = io[indx];
760 struct Scsi_Host *sh;
761 struct aha1542_hostdata *aha1542;
762 char dma_info[] = "no DMA";
767 if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
770 sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
773 aha1542 = shost_priv(sh);
775 sh->unique_id = base_io;
776 sh->io_port = base_io;
777 sh->n_io_port = AHA1542_REGION_SIZE;
778 aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
779 aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
781 if (!aha1542_test_port(sh))
784 aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
785 if (aha1542_query(sh))
787 if (aha1542_getconfig(sh) == -1)
790 if (sh->dma_channel != 0xFF)
791 snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
792 shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
793 sh->this_id, base_io, sh->irq, dma_info);
794 if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
795 shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
797 if (dma_set_mask_and_coherent(pdev, DMA_BIT_MASK(24)) < 0)
800 aha1542->mb = dma_alloc_coherent(pdev,
801 AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
802 &aha1542->mb_handle, GFP_KERNEL);
806 aha1542->ccb = dma_alloc_coherent(pdev,
807 AHA1542_MAILBOXES * sizeof(struct ccb),
808 &aha1542->ccb_handle, GFP_KERNEL);
814 if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
815 shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
818 if (sh->dma_channel != 0xFF) {
819 if (request_dma(sh->dma_channel, "aha1542")) {
820 shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
823 if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
824 set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
825 enable_dma(sh->dma_channel);
829 if (scsi_add_host(sh, pdev))
837 if (sh->dma_channel != 0xff)
838 free_dma(sh->dma_channel);
840 free_irq(sh->irq, sh);
842 dma_free_coherent(pdev, AHA1542_MAILBOXES * sizeof(struct ccb),
843 aha1542->ccb, aha1542->ccb_handle);
845 dma_free_coherent(pdev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
846 aha1542->mb, aha1542->mb_handle);
850 release_region(base_io, AHA1542_REGION_SIZE);
855 static int aha1542_release(struct Scsi_Host *sh)
857 struct aha1542_hostdata *aha1542 = shost_priv(sh);
858 struct device *dev = sh->dma_dev;
860 scsi_remove_host(sh);
861 if (sh->dma_channel != 0xff)
862 free_dma(sh->dma_channel);
863 dma_free_coherent(dev, AHA1542_MAILBOXES * sizeof(struct ccb),
864 aha1542->ccb, aha1542->ccb_handle);
865 dma_free_coherent(dev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
866 aha1542->mb, aha1542->mb_handle);
868 free_irq(sh->irq, sh);
869 if (sh->io_port && sh->n_io_port)
870 release_region(sh->io_port, sh->n_io_port);
877 * This is a device reset. This is handled by sending a special command
880 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
882 struct Scsi_Host *sh = cmd->device->host;
883 struct aha1542_hostdata *aha1542 = shost_priv(sh);
885 struct mailbox *mb = aha1542->mb;
886 u8 target = cmd->device->id;
887 u8 lun = cmd->device->lun;
889 struct ccb *ccb = aha1542->ccb;
891 spin_lock_irqsave(sh->host_lock, flags);
892 mbo = aha1542->aha1542_last_mbo_used + 1;
893 if (mbo >= AHA1542_MAILBOXES)
897 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
900 if (mbo >= AHA1542_MAILBOXES)
902 } while (mbo != aha1542->aha1542_last_mbo_used);
904 if (mb[mbo].status || aha1542->int_cmds[mbo])
905 panic("Unable to find empty mailbox for aha1542.\n");
907 aha1542->int_cmds[mbo] = cmd; /* This will effectively
908 * prevent someone else from
909 * screwing with this cdb.
912 aha1542->aha1542_last_mbo_used = mbo;
914 /* This gets trashed for some reason */
915 any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
917 memset(&ccb[mbo], 0, sizeof(struct ccb));
919 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
921 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
923 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
924 ccb[mbo].commlinkid = 0;
927 * Now tell the 1542 to flush all pending commands for this
930 aha1542_outb(sh->io_port, CMD_START_SCSI);
931 spin_unlock_irqrestore(sh->host_lock, flags);
933 scmd_printk(KERN_WARNING, cmd,
934 "Trying device reset for target\n");
939 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
941 struct Scsi_Host *sh = cmd->device->host;
942 struct aha1542_hostdata *aha1542 = shost_priv(sh);
946 spin_lock_irqsave(sh->host_lock, flags);
948 * This does a scsi reset for all devices on the bus.
949 * In principle, we could also reset the 1542 - should
950 * we do this? Try this first, and we can add that later
951 * if it turns out to be useful.
953 outb(reset_cmd, CONTROL(cmd->device->host->io_port));
955 if (!wait_mask(STATUS(cmd->device->host->io_port),
956 STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
957 spin_unlock_irqrestore(sh->host_lock, flags);
962 * We need to do this too before the 1542 can interact with
963 * us again after host reset.
965 if (reset_cmd & HRST)
966 setup_mailboxes(cmd->device->host);
969 * Now try to pick up the pieces. For all pending commands,
970 * free any internal data structures, and basically clear things
971 * out. We do not try and restart any commands or anything -
972 * the strategy handler takes care of that crap.
974 shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
976 for (i = 0; i < AHA1542_MAILBOXES; i++) {
977 if (aha1542->int_cmds[i] != NULL) {
978 struct scsi_cmnd *tmp_cmd;
979 tmp_cmd = aha1542->int_cmds[i];
981 if (tmp_cmd->device->soft_reset) {
983 * If this device implements the soft reset option,
984 * then it is still holding onto the command, and
985 * may yet complete it. In this case, we don't
990 aha1542_free_cmd(tmp_cmd);
991 aha1542->int_cmds[i] = NULL;
992 aha1542->mb[i].status = 0;
996 spin_unlock_irqrestore(sh->host_lock, flags);
1000 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
1002 return aha1542_reset(cmd, SCRST);
1005 static int aha1542_host_reset(struct scsi_cmnd *cmd)
1007 return aha1542_reset(cmd, HRST | SCRST);
1010 static int aha1542_biosparam(struct scsi_device *sdev,
1011 struct block_device *bdev, sector_t capacity, int geom[])
1013 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1015 if (capacity >= 0x200000 &&
1016 aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1017 /* Please verify that this is the same as what DOS returns */
1018 geom[0] = 255; /* heads */
1019 geom[1] = 63; /* sectors */
1021 geom[0] = 64; /* heads */
1022 geom[1] = 32; /* sectors */
1024 geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
1028 MODULE_LICENSE("GPL");
1030 static struct scsi_host_template driver_template = {
1031 .module = THIS_MODULE,
1032 .proc_name = "aha1542",
1033 .name = "Adaptec 1542",
1034 .cmd_size = sizeof(struct aha1542_cmd),
1035 .queuecommand = aha1542_queuecommand,
1036 .eh_device_reset_handler= aha1542_dev_reset,
1037 .eh_bus_reset_handler = aha1542_bus_reset,
1038 .eh_host_reset_handler = aha1542_host_reset,
1039 .bios_param = aha1542_biosparam,
1040 .can_queue = AHA1542_MAILBOXES,
1043 .unchecked_isa_dma = 1,
1046 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1048 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1053 dev_set_drvdata(pdev, sh);
1057 static void aha1542_isa_remove(struct device *pdev,
1060 aha1542_release(dev_get_drvdata(pdev));
1061 dev_set_drvdata(pdev, NULL);
1064 static struct isa_driver aha1542_isa_driver = {
1065 .match = aha1542_isa_match,
1066 .remove = aha1542_isa_remove,
1071 static int isa_registered;
1074 static const struct pnp_device_id aha1542_pnp_ids[] = {
1075 { .id = "ADP1542" },
1078 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1080 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1083 struct Scsi_Host *sh;
1085 for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1089 if (pnp_activate_dev(pdev) < 0)
1092 io[indx] = pnp_port_start(pdev, 0);
1095 * The card can be queried for its DMA, we have
1096 * the DMA set up that is enough
1099 dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1102 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1106 pnp_set_drvdata(pdev, sh);
1110 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1112 aha1542_release(pnp_get_drvdata(pdev));
1113 pnp_set_drvdata(pdev, NULL);
1116 static struct pnp_driver aha1542_pnp_driver = {
1118 .id_table = aha1542_pnp_ids,
1119 .probe = aha1542_pnp_probe,
1120 .remove = aha1542_pnp_remove,
1122 static int pnp_registered;
1123 #endif /* CONFIG_PNP */
1125 static int __init aha1542_init(void)
1131 ret = pnp_register_driver(&aha1542_pnp_driver);
1136 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1150 static void __exit aha1542_exit(void)
1154 pnp_unregister_driver(&aha1542_pnp_driver);
1157 isa_unregister_driver(&aha1542_isa_driver);
1160 module_init(aha1542_init);
1161 module_exit(aha1542_exit);