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;
68 #define AHA1542_MAX_SECTORS 16
73 dma_addr_t data_buffer_handle;
76 static inline void aha1542_intr_reset(u16 base)
78 outb(IRST, CONTROL(base));
81 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
91 u8 bits = inb(port) & mask;
92 if ((bits & allof) == allof && ((bits & noneof) == 0))
103 static int aha1542_outb(unsigned int base, u8 val)
105 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
107 outb(val, DATA(base));
112 static int aha1542_out(unsigned int base, u8 *buf, int len)
115 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
117 outb(*buf++, DATA(base));
119 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
126 * Only used at boot time, so we do not need to worry about latency as much
130 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
133 if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
135 *buf++ = inb(DATA(base));
140 static int makecode(unsigned hosterr, unsigned scsierr)
144 case 0xa: /* Linked command complete without error and linked normally */
145 case 0xb: /* Linked command complete without error, interrupt generated */
149 case 0x11: /* Selection time out-The initiator selection or target
150 * reselection was not complete within the SCSI Time out period
152 hosterr = DID_TIME_OUT;
155 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
156 * than was allocated by the Data Length field or the sum of the
157 * Scatter / Gather Data Length fields.
160 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
162 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
163 * invalid. This usually indicates a software failure.
166 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
167 * This usually indicates a software failure.
170 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
171 * of linked CCB's does not specify the same logical unit number as
174 case 0x18: /* Invalid Target Direction received from Host-The direction of a
175 * Target Mode CCB was invalid.
178 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
179 * received to service data transfer between the same target LUN
180 * and initiator SCSI ID in the same direction.
183 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
184 * length segment or invalid segment list boundaries was received.
185 * A CCB parameter was invalid.
188 printk("Aha1542: %x %x\n", hosterr, scsierr);
190 hosterr = DID_ERROR; /* Couldn't find any better */
193 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
194 * phase sequence was requested by the target. The host adapter
195 * will generate a SCSI Reset Condition, notifying the host with
201 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
204 return scsierr | (hosterr << 16);
207 static int aha1542_test_port(struct Scsi_Host *sh)
209 u8 inquiry_result[4];
212 /* Quick and dirty test for presence of the card. */
213 if (inb(STATUS(sh->io_port)) == 0xff)
216 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
218 /* In case some other card was probing here, reset interrupts */
219 aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
221 outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
223 mdelay(20); /* Wait a little bit for things to settle down. */
225 /* Expect INIT and IDLE, any of the others are bad */
226 if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
229 /* Shouldn't have generated any interrupts during reset */
230 if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
234 * Perform a host adapter inquiry instead so we do not need to set
235 * up the mailboxes ahead of time
238 aha1542_outb(sh->io_port, CMD_INQUIRY);
240 for (i = 0; i < 4; i++) {
241 if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
243 inquiry_result[i] = inb(DATA(sh->io_port));
246 /* Reading port should reset DF */
247 if (inb(STATUS(sh->io_port)) & DF)
250 /* When HACC, command is completed, and we're though testing */
251 if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
254 /* Clear interrupts */
255 outb(IRST, CONTROL(sh->io_port));
260 static void aha1542_free_cmd(struct scsi_cmnd *cmd)
262 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
264 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
265 struct request *rq = scsi_cmd_to_rq(cmd);
266 void *buf = acmd->data_buffer;
267 struct req_iterator iter;
270 rq_for_each_segment(bv, rq, iter) {
271 memcpy_to_page(bv.bv_page, bv.bv_offset, buf,
280 static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
282 struct Scsi_Host *sh = dev_id;
283 struct aha1542_hostdata *aha1542 = shost_priv(sh);
284 void (*my_done)(struct scsi_cmnd *) = NULL;
285 int errstatus, mbi, mbo, mbistatus;
288 struct scsi_cmnd *tmp_cmd;
290 struct mailbox *mb = aha1542->mb;
291 struct ccb *ccb = aha1542->ccb;
295 flag = inb(INTRFLAGS(sh->io_port));
296 shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
297 if (!(flag & ANYINTR))
298 printk("no interrupt?");
307 printk("status %02x\n", inb(STATUS(sh->io_port)));
312 spin_lock_irqsave(sh->host_lock, flags);
314 flag = inb(INTRFLAGS(sh->io_port));
317 * Check for unusual interrupts. If any of these happen, we should
318 * probably do something special, but for now just printing a message
319 * is sufficient. A SCSI reset detected is something that we really
320 * need to deal with in some way.
330 aha1542_intr_reset(sh->io_port);
332 mbi = aha1542->aha1542_last_mbi_used + 1;
333 if (mbi >= 2 * AHA1542_MAILBOXES)
334 mbi = AHA1542_MAILBOXES;
337 if (mb[mbi].status != 0)
340 if (mbi >= 2 * AHA1542_MAILBOXES)
341 mbi = AHA1542_MAILBOXES;
342 } while (mbi != aha1542->aha1542_last_mbi_used);
344 if (mb[mbi].status == 0) {
345 spin_unlock_irqrestore(sh->host_lock, flags);
346 /* Hmm, no mail. Must have read it the last time around */
347 if (!number_serviced)
348 shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
352 mbo = (scsi2int(mb[mbi].ccbptr) - (unsigned long)aha1542->ccb_handle) / sizeof(struct ccb);
353 mbistatus = mb[mbi].status;
355 aha1542->aha1542_last_mbi_used = mbi;
358 if (ccb[mbo].tarstat | ccb[mbo].hastat)
359 shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
360 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
364 continue; /* Aborted command not found */
367 shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
370 tmp_cmd = aha1542->int_cmds[mbo];
372 if (!tmp_cmd || !tmp_cmd->scsi_done) {
373 spin_unlock_irqrestore(sh->host_lock, flags);
374 shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
375 shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
376 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
379 my_done = tmp_cmd->scsi_done;
380 aha1542_free_cmd(tmp_cmd);
382 * Fetch the sense data, and tuck it away, in the required slot. The
383 * Adaptec automatically fetches it, and there is no guarantee that
384 * we will still have it in the cdb when we come back
386 if (ccb[mbo].tarstat == 2)
387 memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
388 SCSI_SENSE_BUFFERSIZE);
391 /* is there mail :-) */
393 /* more error checking left out here */
395 /* This is surely wrong, but I don't know what's right */
396 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
402 shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
403 ccb[mbo].hastat, ccb[mbo].tarstat);
404 if (ccb[mbo].tarstat == 2)
405 print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
407 printk("aha1542_intr_handle: returning %6x\n", errstatus);
409 tmp_cmd->result = errstatus;
410 aha1542->int_cmds[mbo] = NULL; /* This effectively frees up the mailbox slot, as
411 * far as queuecommand is concerned
418 static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
420 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
421 struct aha1542_hostdata *aha1542 = shost_priv(sh);
423 u8 target = cmd->device->id;
424 u8 lun = cmd->device->lun;
426 int bufflen = scsi_bufflen(cmd);
428 struct mailbox *mb = aha1542->mb;
429 struct ccb *ccb = aha1542->ccb;
431 if (*cmd->cmnd == REQUEST_SENSE) {
432 /* Don't do the command - we have the sense data already */
440 if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
441 i = xscsi2int(cmd->cmnd + 2);
442 else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
443 i = scsi2int(cmd->cmnd + 2);
444 shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
445 target, *cmd->cmnd, i, bufflen);
446 print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
450 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
451 struct request *rq = scsi_cmd_to_rq(cmd);
452 void *buf = acmd->data_buffer;
453 struct req_iterator iter;
456 rq_for_each_segment(bv, rq, iter) {
457 memcpy_from_page(buf, bv.bv_page, bv.bv_offset,
464 * Use the outgoing mailboxes in a round-robin fashion, because this
465 * is how the host adapter will scan for them
468 spin_lock_irqsave(sh->host_lock, flags);
469 mbo = aha1542->aha1542_last_mbo_used + 1;
470 if (mbo >= AHA1542_MAILBOXES)
474 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
477 if (mbo >= AHA1542_MAILBOXES)
479 } while (mbo != aha1542->aha1542_last_mbo_used);
481 if (mb[mbo].status || aha1542->int_cmds[mbo])
482 panic("Unable to find empty mailbox for aha1542.\n");
484 aha1542->int_cmds[mbo] = cmd; /* This will effectively prevent someone else from
485 * screwing with this cdb.
488 aha1542->aha1542_last_mbo_used = mbo;
491 shost_printk(KERN_DEBUG, sh, "Sending command (%d %p)...", mbo, cmd->scsi_done);
494 /* This gets trashed for some reason */
495 any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
497 memset(&ccb[mbo], 0, sizeof(struct ccb));
499 ccb[mbo].cdblen = cmd->cmd_len;
502 if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
504 else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
507 memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
508 ccb[mbo].op = 0; /* SCSI Initiator Command */
509 any2scsi(ccb[mbo].datalen, bufflen);
511 any2scsi(ccb[mbo].dataptr, acmd->data_buffer_handle);
513 any2scsi(ccb[mbo].dataptr, 0);
514 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
515 ccb[mbo].rsalen = 16;
516 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
517 ccb[mbo].commlinkid = 0;
520 print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
521 printk("aha1542_queuecommand: now waiting for interrupt ");
524 aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
525 spin_unlock_irqrestore(sh->host_lock, flags);
530 /* Initialize mailboxes */
531 static void setup_mailboxes(struct Scsi_Host *sh)
533 struct aha1542_hostdata *aha1542 = shost_priv(sh);
534 u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
537 for (i = 0; i < AHA1542_MAILBOXES; i++) {
538 aha1542->mb[i].status = 0;
539 any2scsi(aha1542->mb[i].ccbptr,
540 aha1542->ccb_handle + i * sizeof(struct ccb));
541 aha1542->mb[AHA1542_MAILBOXES + i].status = 0;
543 aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
544 any2scsi(mb_cmd + 2, aha1542->mb_handle);
545 if (aha1542_out(sh->io_port, mb_cmd, 5))
546 shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
547 aha1542_intr_reset(sh->io_port);
550 static int aha1542_getconfig(struct Scsi_Host *sh)
552 u8 inquiry_result[3];
554 i = inb(STATUS(sh->io_port));
556 i = inb(DATA(sh->io_port));
558 aha1542_outb(sh->io_port, CMD_RETCONF);
559 aha1542_in(sh->io_port, inquiry_result, 3, 0);
560 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
561 shost_printk(KERN_ERR, sh, "error querying board settings\n");
562 aha1542_intr_reset(sh->io_port);
563 switch (inquiry_result[0]) {
578 * This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
579 * Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this.
581 sh->dma_channel = 0xFF;
584 shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
587 switch (inquiry_result[1]) {
607 shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
610 sh->this_id = inquiry_result[2] & 7;
615 * This function should only be called for 1542C boards - we can detect
616 * the special firmware settings and unlock the board
619 static int aha1542_mbenable(struct Scsi_Host *sh)
621 static u8 mbenable_cmd[3];
622 static u8 mbenable_result[2];
625 retval = BIOS_TRANSLATION_6432;
627 aha1542_outb(sh->io_port, CMD_EXTBIOS);
628 if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
630 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
632 aha1542_intr_reset(sh->io_port);
634 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
635 mbenable_cmd[0] = CMD_MBENABLE;
637 mbenable_cmd[2] = mbenable_result[1];
639 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
640 retval = BIOS_TRANSLATION_25563;
642 if (aha1542_out(sh->io_port, mbenable_cmd, 3))
647 shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
649 aha1542_intr_reset(sh->io_port);
653 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
654 static int aha1542_query(struct Scsi_Host *sh)
656 struct aha1542_hostdata *aha1542 = shost_priv(sh);
657 u8 inquiry_result[4];
659 i = inb(STATUS(sh->io_port));
661 i = inb(DATA(sh->io_port));
663 aha1542_outb(sh->io_port, CMD_INQUIRY);
664 aha1542_in(sh->io_port, inquiry_result, 4, 0);
665 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
666 shost_printk(KERN_ERR, sh, "error querying card type\n");
667 aha1542_intr_reset(sh->io_port);
669 aha1542->bios_translation = BIOS_TRANSLATION_6432; /* Default case */
672 * For an AHA1740 series board, we ignore the board since there is a
673 * hardware bug which can lead to wrong blocks being returned if the board
674 * is operating in the 1542 emulation mode. Since there is an extended mode
675 * driver, we simply ignore the board and let the 1740 driver pick it up.
678 if (inquiry_result[0] == 0x43) {
679 shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
684 * Always call this - boards that do not support extended bios translation
685 * will ignore the command, and we will set the proper default
688 aha1542->bios_translation = aha1542_mbenable(sh);
693 static u8 dma_speed_hw(int dma_speed)
708 return 0xff; /* invalid */
711 /* Set the Bus on/off-times as not to ruin floppy performance */
712 static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
715 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
717 aha1542_intr_reset(sh->io_port);
718 if (aha1542_out(sh->io_port, oncmd, 2))
723 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
725 aha1542_intr_reset(sh->io_port);
726 if (aha1542_out(sh->io_port, offcmd, 2))
730 if (dma_speed_hw(dma_speed) != 0xff) {
731 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
733 aha1542_intr_reset(sh->io_port);
734 if (aha1542_out(sh->io_port, dmacmd, 2))
737 aha1542_intr_reset(sh->io_port);
740 shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
741 aha1542_intr_reset(sh->io_port);
744 /* return non-zero on detection */
745 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
747 unsigned int base_io = io[indx];
748 struct Scsi_Host *sh;
749 struct aha1542_hostdata *aha1542;
750 char dma_info[] = "no DMA";
755 if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
758 sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
761 aha1542 = shost_priv(sh);
763 sh->unique_id = base_io;
764 sh->io_port = base_io;
765 sh->n_io_port = AHA1542_REGION_SIZE;
766 aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
767 aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
769 if (!aha1542_test_port(sh))
772 aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
773 if (aha1542_query(sh))
775 if (aha1542_getconfig(sh) == -1)
778 if (sh->dma_channel != 0xFF)
779 snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
780 shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
781 sh->this_id, base_io, sh->irq, dma_info);
782 if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
783 shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
785 if (dma_set_mask_and_coherent(pdev, DMA_BIT_MASK(24)) < 0)
788 aha1542->mb = dma_alloc_coherent(pdev,
789 AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
790 &aha1542->mb_handle, GFP_KERNEL);
794 aha1542->ccb = dma_alloc_coherent(pdev,
795 AHA1542_MAILBOXES * sizeof(struct ccb),
796 &aha1542->ccb_handle, GFP_KERNEL);
802 if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
803 shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
806 if (sh->dma_channel != 0xFF) {
807 if (request_dma(sh->dma_channel, "aha1542")) {
808 shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
811 if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
812 set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
813 enable_dma(sh->dma_channel);
817 if (scsi_add_host(sh, pdev))
825 if (sh->dma_channel != 0xff)
826 free_dma(sh->dma_channel);
828 free_irq(sh->irq, sh);
830 dma_free_coherent(pdev, AHA1542_MAILBOXES * sizeof(struct ccb),
831 aha1542->ccb, aha1542->ccb_handle);
833 dma_free_coherent(pdev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
834 aha1542->mb, aha1542->mb_handle);
838 release_region(base_io, AHA1542_REGION_SIZE);
843 static int aha1542_release(struct Scsi_Host *sh)
845 struct aha1542_hostdata *aha1542 = shost_priv(sh);
846 struct device *dev = sh->dma_dev;
848 scsi_remove_host(sh);
849 if (sh->dma_channel != 0xff)
850 free_dma(sh->dma_channel);
851 dma_free_coherent(dev, AHA1542_MAILBOXES * sizeof(struct ccb),
852 aha1542->ccb, aha1542->ccb_handle);
853 dma_free_coherent(dev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
854 aha1542->mb, aha1542->mb_handle);
856 free_irq(sh->irq, sh);
857 if (sh->io_port && sh->n_io_port)
858 release_region(sh->io_port, sh->n_io_port);
865 * This is a device reset. This is handled by sending a special command
868 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
870 struct Scsi_Host *sh = cmd->device->host;
871 struct aha1542_hostdata *aha1542 = shost_priv(sh);
873 struct mailbox *mb = aha1542->mb;
874 u8 target = cmd->device->id;
875 u8 lun = cmd->device->lun;
877 struct ccb *ccb = aha1542->ccb;
879 spin_lock_irqsave(sh->host_lock, flags);
880 mbo = aha1542->aha1542_last_mbo_used + 1;
881 if (mbo >= AHA1542_MAILBOXES)
885 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
888 if (mbo >= AHA1542_MAILBOXES)
890 } while (mbo != aha1542->aha1542_last_mbo_used);
892 if (mb[mbo].status || aha1542->int_cmds[mbo])
893 panic("Unable to find empty mailbox for aha1542.\n");
895 aha1542->int_cmds[mbo] = cmd; /* This will effectively
896 * prevent someone else from
897 * screwing with this cdb.
900 aha1542->aha1542_last_mbo_used = mbo;
902 /* This gets trashed for some reason */
903 any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
905 memset(&ccb[mbo], 0, sizeof(struct ccb));
907 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
909 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
911 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
912 ccb[mbo].commlinkid = 0;
915 * Now tell the 1542 to flush all pending commands for this
918 aha1542_outb(sh->io_port, CMD_START_SCSI);
919 spin_unlock_irqrestore(sh->host_lock, flags);
921 scmd_printk(KERN_WARNING, cmd,
922 "Trying device reset for target\n");
927 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
929 struct Scsi_Host *sh = cmd->device->host;
930 struct aha1542_hostdata *aha1542 = shost_priv(sh);
934 spin_lock_irqsave(sh->host_lock, flags);
936 * This does a scsi reset for all devices on the bus.
937 * In principle, we could also reset the 1542 - should
938 * we do this? Try this first, and we can add that later
939 * if it turns out to be useful.
941 outb(reset_cmd, CONTROL(cmd->device->host->io_port));
943 if (!wait_mask(STATUS(cmd->device->host->io_port),
944 STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
945 spin_unlock_irqrestore(sh->host_lock, flags);
950 * We need to do this too before the 1542 can interact with
951 * us again after host reset.
953 if (reset_cmd & HRST)
954 setup_mailboxes(cmd->device->host);
957 * Now try to pick up the pieces. For all pending commands,
958 * free any internal data structures, and basically clear things
959 * out. We do not try and restart any commands or anything -
960 * the strategy handler takes care of that crap.
962 shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
964 for (i = 0; i < AHA1542_MAILBOXES; i++) {
965 if (aha1542->int_cmds[i] != NULL) {
966 struct scsi_cmnd *tmp_cmd;
967 tmp_cmd = aha1542->int_cmds[i];
969 if (tmp_cmd->device->soft_reset) {
971 * If this device implements the soft reset option,
972 * then it is still holding onto the command, and
973 * may yet complete it. In this case, we don't
978 aha1542_free_cmd(tmp_cmd);
979 aha1542->int_cmds[i] = NULL;
980 aha1542->mb[i].status = 0;
984 spin_unlock_irqrestore(sh->host_lock, flags);
988 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
990 return aha1542_reset(cmd, SCRST);
993 static int aha1542_host_reset(struct scsi_cmnd *cmd)
995 return aha1542_reset(cmd, HRST | SCRST);
998 static int aha1542_biosparam(struct scsi_device *sdev,
999 struct block_device *bdev, sector_t capacity, int geom[])
1001 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1003 if (capacity >= 0x200000 &&
1004 aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1005 /* Please verify that this is the same as what DOS returns */
1006 geom[0] = 255; /* heads */
1007 geom[1] = 63; /* sectors */
1009 geom[0] = 64; /* heads */
1010 geom[1] = 32; /* sectors */
1012 geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
1016 MODULE_LICENSE("GPL");
1018 static int aha1542_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1020 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1022 acmd->data_buffer = dma_alloc_coherent(shost->dma_dev,
1023 SECTOR_SIZE * AHA1542_MAX_SECTORS,
1024 &acmd->data_buffer_handle, GFP_KERNEL);
1025 if (!acmd->data_buffer)
1030 static int aha1542_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1032 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1034 dma_free_coherent(shost->dma_dev, SECTOR_SIZE * AHA1542_MAX_SECTORS,
1035 acmd->data_buffer, acmd->data_buffer_handle);
1039 static struct scsi_host_template driver_template = {
1040 .module = THIS_MODULE,
1041 .proc_name = "aha1542",
1042 .name = "Adaptec 1542",
1043 .cmd_size = sizeof(struct aha1542_cmd),
1044 .queuecommand = aha1542_queuecommand,
1045 .eh_device_reset_handler= aha1542_dev_reset,
1046 .eh_bus_reset_handler = aha1542_bus_reset,
1047 .eh_host_reset_handler = aha1542_host_reset,
1048 .bios_param = aha1542_biosparam,
1049 .init_cmd_priv = aha1542_init_cmd_priv,
1050 .exit_cmd_priv = aha1542_exit_cmd_priv,
1051 .can_queue = AHA1542_MAILBOXES,
1053 .max_sectors = AHA1542_MAX_SECTORS,
1054 .sg_tablesize = SG_ALL,
1057 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1059 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1064 dev_set_drvdata(pdev, sh);
1068 static void aha1542_isa_remove(struct device *pdev,
1071 aha1542_release(dev_get_drvdata(pdev));
1072 dev_set_drvdata(pdev, NULL);
1075 static struct isa_driver aha1542_isa_driver = {
1076 .match = aha1542_isa_match,
1077 .remove = aha1542_isa_remove,
1082 static int isa_registered;
1085 static const struct pnp_device_id aha1542_pnp_ids[] = {
1086 { .id = "ADP1542" },
1089 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1091 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1094 struct Scsi_Host *sh;
1096 for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1100 if (pnp_activate_dev(pdev) < 0)
1103 io[indx] = pnp_port_start(pdev, 0);
1106 * The card can be queried for its DMA, we have
1107 * the DMA set up that is enough
1110 dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1113 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1117 pnp_set_drvdata(pdev, sh);
1121 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1123 aha1542_release(pnp_get_drvdata(pdev));
1124 pnp_set_drvdata(pdev, NULL);
1127 static struct pnp_driver aha1542_pnp_driver = {
1129 .id_table = aha1542_pnp_ids,
1130 .probe = aha1542_pnp_probe,
1131 .remove = aha1542_pnp_remove,
1133 static int pnp_registered;
1134 #endif /* CONFIG_PNP */
1136 static int __init aha1542_init(void)
1142 ret = pnp_register_driver(&aha1542_pnp_driver);
1147 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1161 static void __exit aha1542_exit(void)
1165 pnp_unregister_driver(&aha1542_pnp_driver);
1168 isa_unregister_driver(&aha1542_isa_driver);
1171 module_init(aha1542_init);
1172 module_exit(aha1542_exit);