Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Driver for Adaptec AHA-1542 SCSI host adapters
0004  *
0005  *  Copyright (C) 1992  Tommy Thorn
0006  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
0007  *  Copyright (C) 2015 Ondrej Zary
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/kernel.h>
0013 #include <linux/types.h>
0014 #include <linux/string.h>
0015 #include <linux/delay.h>
0016 #include <linux/init.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/isa.h>
0019 #include <linux/pnp.h>
0020 #include <linux/slab.h>
0021 #include <linux/io.h>
0022 #include <asm/dma.h>
0023 #include <scsi/scsi_cmnd.h>
0024 #include <scsi/scsi_device.h>
0025 #include <scsi/scsi_host.h>
0026 #include "aha1542.h"
0027 
0028 #define MAXBOARDS 4
0029 
0030 static bool isapnp = 1;
0031 module_param(isapnp, bool, 0);
0032 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
0033 
0034 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
0035 module_param_hw_array(io, int, ioport, NULL, 0);
0036 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
0037 
0038 /* time AHA spends on the AT-bus during data transfer */
0039 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
0040 module_param_array(bus_on, int, NULL, 0);
0041 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
0042 
0043 /* time AHA spends off the bus (not to monopolize it) during data transfer  */
0044 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
0045 module_param_array(bus_off, int, NULL, 0);
0046 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
0047 
0048 /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
0049 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
0050 module_param_array(dma_speed, int, NULL, 0);
0051 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
0052 
0053 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
0054 #define BIOS_TRANSLATION_25563 2    /* Big disk case */
0055 
0056 struct aha1542_hostdata {
0057     /* This will effectively start both of them at the first mailbox */
0058     int bios_translation;   /* Mapping bios uses - for compatibility */
0059     int aha1542_last_mbi_used;
0060     int aha1542_last_mbo_used;
0061     struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
0062     struct mailbox *mb;
0063     dma_addr_t mb_handle;
0064     struct ccb *ccb;
0065     dma_addr_t ccb_handle;
0066 };
0067 
0068 #define AHA1542_MAX_SECTORS       16
0069 
0070 struct aha1542_cmd {
0071     /* bounce buffer */
0072     void *data_buffer;
0073     dma_addr_t data_buffer_handle;
0074 };
0075 
0076 static inline void aha1542_intr_reset(u16 base)
0077 {
0078     outb(IRST, CONTROL(base));
0079 }
0080 
0081 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
0082 {
0083     bool delayed = true;
0084 
0085     if (timeout == 0) {
0086         timeout = 3000000;
0087         delayed = false;
0088     }
0089 
0090     while (1) {
0091         u8 bits = inb(port) & mask;
0092         if ((bits & allof) == allof && ((bits & noneof) == 0))
0093             break;
0094         if (delayed)
0095             mdelay(1);
0096         if (--timeout == 0)
0097             return false;
0098     }
0099 
0100     return true;
0101 }
0102 
0103 static int aha1542_outb(unsigned int base, u8 val)
0104 {
0105     if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
0106         return 1;
0107     outb(val, DATA(base));
0108 
0109     return 0;
0110 }
0111 
0112 static int aha1542_out(unsigned int base, u8 *buf, int len)
0113 {
0114     while (len--) {
0115         if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
0116             return 1;
0117         outb(*buf++, DATA(base));
0118     }
0119     if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
0120         return 1;
0121 
0122     return 0;
0123 }
0124 
0125 /*
0126  * Only used at boot time, so we do not need to worry about latency as much
0127  * here
0128  */
0129 
0130 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
0131 {
0132     while (len--) {
0133         if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
0134             return 1;
0135         *buf++ = inb(DATA(base));
0136     }
0137     return 0;
0138 }
0139 
0140 static int makecode(unsigned hosterr, unsigned scsierr)
0141 {
0142     switch (hosterr) {
0143     case 0x0:
0144     case 0xa:       /* Linked command complete without error and linked normally */
0145     case 0xb:       /* Linked command complete without error, interrupt generated */
0146         hosterr = 0;
0147         break;
0148 
0149     case 0x11:      /* Selection time out-The initiator selection or target
0150                  * reselection was not complete within the SCSI Time out period
0151                  */
0152         hosterr = DID_TIME_OUT;
0153         break;
0154 
0155     case 0x12:      /* Data overrun/underrun-The target attempted to transfer more data
0156                  * than was allocated by the Data Length field or the sum of the
0157                  * Scatter / Gather Data Length fields.
0158                  */
0159 
0160     case 0x13:      /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
0161 
0162     case 0x15:      /* MBO command was not 00, 01 or 02-The first byte of the CB was
0163                  * invalid. This usually indicates a software failure.
0164                  */
0165 
0166     case 0x16:      /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
0167                  * This usually indicates a software failure.
0168                  */
0169 
0170     case 0x17:      /* Linked CCB does not have the same LUN-A subsequent CCB of a set
0171                  * of linked CCB's does not specify the same logical unit number as
0172                  * the first.
0173                  */
0174     case 0x18:      /* Invalid Target Direction received from Host-The direction of a
0175                  * Target Mode CCB was invalid.
0176                  */
0177 
0178     case 0x19:      /* Duplicate CCB Received in Target Mode-More than once CCB was
0179                  * received to service data transfer between the same target LUN
0180                  * and initiator SCSI ID in the same direction.
0181                  */
0182 
0183     case 0x1a:      /* Invalid CCB or Segment List Parameter-A segment list with a zero
0184                  * length segment or invalid segment list boundaries was received.
0185                  * A CCB parameter was invalid.
0186                  */
0187 #ifdef DEBUG
0188         printk("Aha1542: %x %x\n", hosterr, scsierr);
0189 #endif
0190         hosterr = DID_ERROR;    /* Couldn't find any better */
0191         break;
0192 
0193     case 0x14:      /* Target bus phase sequence failure-An invalid bus phase or bus
0194                  * phase sequence was requested by the target. The host adapter
0195                  * will generate a SCSI Reset Condition, notifying the host with
0196                  * a SCRD interrupt
0197                  */
0198         hosterr = DID_RESET;
0199         break;
0200     default:
0201         printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
0202         break;
0203     }
0204     return scsierr | (hosterr << 16);
0205 }
0206 
0207 static int aha1542_test_port(struct Scsi_Host *sh)
0208 {
0209     int i;
0210 
0211     /* Quick and dirty test for presence of the card. */
0212     if (inb(STATUS(sh->io_port)) == 0xff)
0213         return 0;
0214 
0215     /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
0216 
0217     /* In case some other card was probing here, reset interrupts */
0218     aha1542_intr_reset(sh->io_port);    /* reset interrupts, so they don't block */
0219 
0220     outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
0221 
0222     mdelay(20);     /* Wait a little bit for things to settle down. */
0223 
0224     /* Expect INIT and IDLE, any of the others are bad */
0225     if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
0226         return 0;
0227 
0228     /* Shouldn't have generated any interrupts during reset */
0229     if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
0230         return 0;
0231 
0232     /*
0233      * Perform a host adapter inquiry instead so we do not need to set
0234      * up the mailboxes ahead of time
0235      */
0236 
0237     aha1542_outb(sh->io_port, CMD_INQUIRY);
0238 
0239     for (i = 0; i < 4; i++) {
0240         if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
0241             return 0;
0242         (void)inb(DATA(sh->io_port));
0243     }
0244 
0245     /* Reading port should reset DF */
0246     if (inb(STATUS(sh->io_port)) & DF)
0247         return 0;
0248 
0249     /* When HACC, command is completed, and we're though testing */
0250     if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
0251         return 0;
0252 
0253     /* Clear interrupts */
0254     outb(IRST, CONTROL(sh->io_port));
0255 
0256     return 1;
0257 }
0258 
0259 static void aha1542_free_cmd(struct scsi_cmnd *cmd)
0260 {
0261     struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
0262 
0263     if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
0264         struct request *rq = scsi_cmd_to_rq(cmd);
0265         void *buf = acmd->data_buffer;
0266         struct req_iterator iter;
0267         struct bio_vec bv;
0268 
0269         rq_for_each_segment(bv, rq, iter) {
0270             memcpy_to_bvec(&bv, buf);
0271             buf += bv.bv_len;
0272         }
0273     }
0274 
0275     scsi_dma_unmap(cmd);
0276 }
0277 
0278 static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
0279 {
0280     struct Scsi_Host *sh = dev_id;
0281     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0282     int errstatus, mbi, mbo, mbistatus;
0283     int number_serviced;
0284     unsigned long flags;
0285     struct scsi_cmnd *tmp_cmd;
0286     int flag;
0287     struct mailbox *mb = aha1542->mb;
0288     struct ccb *ccb = aha1542->ccb;
0289 
0290 #ifdef DEBUG
0291     {
0292         flag = inb(INTRFLAGS(sh->io_port));
0293         shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
0294         if (!(flag & ANYINTR))
0295             printk("no interrupt?");
0296         if (flag & MBIF)
0297             printk("MBIF ");
0298         if (flag & MBOA)
0299             printk("MBOF ");
0300         if (flag & HACC)
0301             printk("HACC ");
0302         if (flag & SCRD)
0303             printk("SCRD ");
0304         printk("status %02x\n", inb(STATUS(sh->io_port)));
0305     }
0306 #endif
0307     number_serviced = 0;
0308 
0309     spin_lock_irqsave(sh->host_lock, flags);
0310     while (1) {
0311         flag = inb(INTRFLAGS(sh->io_port));
0312 
0313         /*
0314          * Check for unusual interrupts.  If any of these happen, we should
0315          * probably do something special, but for now just printing a message
0316          * is sufficient.  A SCSI reset detected is something that we really
0317          * need to deal with in some way.
0318          */
0319         if (flag & ~MBIF) {
0320             if (flag & MBOA)
0321                 printk("MBOF ");
0322             if (flag & HACC)
0323                 printk("HACC ");
0324             if (flag & SCRD)
0325                 printk("SCRD ");
0326         }
0327         aha1542_intr_reset(sh->io_port);
0328 
0329         mbi = aha1542->aha1542_last_mbi_used + 1;
0330         if (mbi >= 2 * AHA1542_MAILBOXES)
0331             mbi = AHA1542_MAILBOXES;
0332 
0333         do {
0334             if (mb[mbi].status != 0)
0335                 break;
0336             mbi++;
0337             if (mbi >= 2 * AHA1542_MAILBOXES)
0338                 mbi = AHA1542_MAILBOXES;
0339         } while (mbi != aha1542->aha1542_last_mbi_used);
0340 
0341         if (mb[mbi].status == 0) {
0342             spin_unlock_irqrestore(sh->host_lock, flags);
0343             /* Hmm, no mail.  Must have read it the last time around */
0344             if (!number_serviced)
0345                 shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
0346             return IRQ_HANDLED;
0347         }
0348 
0349         mbo = (scsi2int(mb[mbi].ccbptr) - (unsigned long)aha1542->ccb_handle) / sizeof(struct ccb);
0350         mbistatus = mb[mbi].status;
0351         mb[mbi].status = 0;
0352         aha1542->aha1542_last_mbi_used = mbi;
0353 
0354 #ifdef DEBUG
0355         if (ccb[mbo].tarstat | ccb[mbo].hastat)
0356             shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
0357                    ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
0358 #endif
0359 
0360         if (mbistatus == 3)
0361             continue;   /* Aborted command not found */
0362 
0363 #ifdef DEBUG
0364         shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
0365 #endif
0366 
0367         tmp_cmd = aha1542->int_cmds[mbo];
0368 
0369         if (!tmp_cmd) {
0370             spin_unlock_irqrestore(sh->host_lock, flags);
0371             shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
0372             shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
0373                    ccb[mbo].hastat, ccb[mbo].idlun, mbo);
0374             return IRQ_HANDLED;
0375         }
0376         aha1542_free_cmd(tmp_cmd);
0377         /*
0378          * Fetch the sense data, and tuck it away, in the required slot.  The
0379          * Adaptec automatically fetches it, and there is no guarantee that
0380          * we will still have it in the cdb when we come back
0381          */
0382         if (ccb[mbo].tarstat == 2)
0383             memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
0384                    SCSI_SENSE_BUFFERSIZE);
0385 
0386 
0387         /* is there mail :-) */
0388 
0389         /* more error checking left out here */
0390         if (mbistatus != 1)
0391             /* This is surely wrong, but I don't know what's right */
0392             errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
0393         else
0394             errstatus = 0;
0395 
0396 #ifdef DEBUG
0397         if (errstatus)
0398             shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
0399                    ccb[mbo].hastat, ccb[mbo].tarstat);
0400         if (ccb[mbo].tarstat == 2)
0401             print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
0402         if (errstatus)
0403             printk("aha1542_intr_handle: returning %6x\n", errstatus);
0404 #endif
0405         tmp_cmd->result = errstatus;
0406         aha1542->int_cmds[mbo] = NULL;  /* This effectively frees up the mailbox slot, as
0407                          * far as queuecommand is concerned
0408                          */
0409         scsi_done(tmp_cmd);
0410         number_serviced++;
0411     }
0412 }
0413 
0414 static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
0415 {
0416     struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
0417     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0418     u8 direction;
0419     u8 target = cmd->device->id;
0420     u8 lun = cmd->device->lun;
0421     unsigned long flags;
0422     int bufflen = scsi_bufflen(cmd);
0423     int mbo;
0424     struct mailbox *mb = aha1542->mb;
0425     struct ccb *ccb = aha1542->ccb;
0426 
0427     if (*cmd->cmnd == REQUEST_SENSE) {
0428         /* Don't do the command - we have the sense data already */
0429         cmd->result = 0;
0430         scsi_done(cmd);
0431         return 0;
0432     }
0433 #ifdef DEBUG
0434     {
0435         int i = -1;
0436         if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
0437             i = xscsi2int(cmd->cmnd + 2);
0438         else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
0439             i = scsi2int(cmd->cmnd + 2);
0440         shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
0441                         target, *cmd->cmnd, i, bufflen);
0442         print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
0443     }
0444 #endif
0445 
0446     if (cmd->sc_data_direction == DMA_TO_DEVICE) {
0447         struct request *rq = scsi_cmd_to_rq(cmd);
0448         void *buf = acmd->data_buffer;
0449         struct req_iterator iter;
0450         struct bio_vec bv;
0451 
0452         rq_for_each_segment(bv, rq, iter) {
0453             memcpy_from_bvec(buf, &bv);
0454             buf += bv.bv_len;
0455         }
0456     }
0457 
0458     /*
0459      * Use the outgoing mailboxes in a round-robin fashion, because this
0460      * is how the host adapter will scan for them
0461      */
0462 
0463     spin_lock_irqsave(sh->host_lock, flags);
0464     mbo = aha1542->aha1542_last_mbo_used + 1;
0465     if (mbo >= AHA1542_MAILBOXES)
0466         mbo = 0;
0467 
0468     do {
0469         if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
0470             break;
0471         mbo++;
0472         if (mbo >= AHA1542_MAILBOXES)
0473             mbo = 0;
0474     } while (mbo != aha1542->aha1542_last_mbo_used);
0475 
0476     if (mb[mbo].status || aha1542->int_cmds[mbo])
0477         panic("Unable to find empty mailbox for aha1542.\n");
0478 
0479     aha1542->int_cmds[mbo] = cmd;   /* This will effectively prevent someone else from
0480                      * screwing with this cdb.
0481                      */
0482 
0483     aha1542->aha1542_last_mbo_used = mbo;
0484 
0485 #ifdef DEBUG
0486     shost_printk(KERN_DEBUG, sh, "Sending command (%d)...", mbo);
0487 #endif
0488 
0489     /* This gets trashed for some reason */
0490     any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
0491 
0492     memset(&ccb[mbo], 0, sizeof(struct ccb));
0493 
0494     ccb[mbo].cdblen = cmd->cmd_len;
0495 
0496     direction = 0;
0497     if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
0498         direction = 8;
0499     else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
0500         direction = 16;
0501 
0502     memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
0503     ccb[mbo].op = 0;    /* SCSI Initiator Command */
0504     any2scsi(ccb[mbo].datalen, bufflen);
0505     if (bufflen)
0506         any2scsi(ccb[mbo].dataptr, acmd->data_buffer_handle);
0507     else
0508         any2scsi(ccb[mbo].dataptr, 0);
0509     ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
0510     ccb[mbo].rsalen = 16;
0511     ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
0512     ccb[mbo].commlinkid = 0;
0513 
0514 #ifdef DEBUG
0515     print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
0516     printk("aha1542_queuecommand: now waiting for interrupt ");
0517 #endif
0518     mb[mbo].status = 1;
0519     aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
0520     spin_unlock_irqrestore(sh->host_lock, flags);
0521 
0522     return 0;
0523 }
0524 
0525 /* Initialize mailboxes */
0526 static void setup_mailboxes(struct Scsi_Host *sh)
0527 {
0528     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0529     u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
0530     int i;
0531 
0532     for (i = 0; i < AHA1542_MAILBOXES; i++) {
0533         aha1542->mb[i].status = 0;
0534         any2scsi(aha1542->mb[i].ccbptr,
0535              aha1542->ccb_handle + i * sizeof(struct ccb));
0536         aha1542->mb[AHA1542_MAILBOXES + i].status = 0;
0537     }
0538     aha1542_intr_reset(sh->io_port);    /* reset interrupts, so they don't block */
0539     any2scsi(mb_cmd + 2, aha1542->mb_handle);
0540     if (aha1542_out(sh->io_port, mb_cmd, 5))
0541         shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
0542     aha1542_intr_reset(sh->io_port);
0543 }
0544 
0545 static int aha1542_getconfig(struct Scsi_Host *sh)
0546 {
0547     u8 inquiry_result[3];
0548     int i;
0549     i = inb(STATUS(sh->io_port));
0550     if (i & DF) {
0551         i = inb(DATA(sh->io_port));
0552     }
0553     aha1542_outb(sh->io_port, CMD_RETCONF);
0554     aha1542_in(sh->io_port, inquiry_result, 3, 0);
0555     if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
0556         shost_printk(KERN_ERR, sh, "error querying board settings\n");
0557     aha1542_intr_reset(sh->io_port);
0558     switch (inquiry_result[0]) {
0559     case 0x80:
0560         sh->dma_channel = 7;
0561         break;
0562     case 0x40:
0563         sh->dma_channel = 6;
0564         break;
0565     case 0x20:
0566         sh->dma_channel = 5;
0567         break;
0568     case 0x01:
0569         sh->dma_channel = 0;
0570         break;
0571     case 0:
0572         /*
0573          * This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
0574          * Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this.
0575          */
0576         sh->dma_channel = 0xFF;
0577         break;
0578     default:
0579         shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
0580         return -1;
0581     }
0582     switch (inquiry_result[1]) {
0583     case 0x40:
0584         sh->irq = 15;
0585         break;
0586     case 0x20:
0587         sh->irq = 14;
0588         break;
0589     case 0x8:
0590         sh->irq = 12;
0591         break;
0592     case 0x4:
0593         sh->irq = 11;
0594         break;
0595     case 0x2:
0596         sh->irq = 10;
0597         break;
0598     case 0x1:
0599         sh->irq = 9;
0600         break;
0601     default:
0602         shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
0603         return -1;
0604     }
0605     sh->this_id = inquiry_result[2] & 7;
0606     return 0;
0607 }
0608 
0609 /*
0610  * This function should only be called for 1542C boards - we can detect
0611  * the special firmware settings and unlock the board
0612  */
0613 
0614 static int aha1542_mbenable(struct Scsi_Host *sh)
0615 {
0616     static u8 mbenable_cmd[3];
0617     static u8 mbenable_result[2];
0618     int retval;
0619 
0620     retval = BIOS_TRANSLATION_6432;
0621 
0622     aha1542_outb(sh->io_port, CMD_EXTBIOS);
0623     if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
0624         return retval;
0625     if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
0626         goto fail;
0627     aha1542_intr_reset(sh->io_port);
0628 
0629     if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
0630         mbenable_cmd[0] = CMD_MBENABLE;
0631         mbenable_cmd[1] = 0;
0632         mbenable_cmd[2] = mbenable_result[1];
0633 
0634         if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
0635             retval = BIOS_TRANSLATION_25563;
0636 
0637         if (aha1542_out(sh->io_port, mbenable_cmd, 3))
0638             goto fail;
0639     }
0640     while (0) {
0641 fail:
0642         shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
0643     }
0644     aha1542_intr_reset(sh->io_port);
0645     return retval;
0646 }
0647 
0648 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
0649 static int aha1542_query(struct Scsi_Host *sh)
0650 {
0651     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0652     u8 inquiry_result[4];
0653     int i;
0654     i = inb(STATUS(sh->io_port));
0655     if (i & DF) {
0656         i = inb(DATA(sh->io_port));
0657     }
0658     aha1542_outb(sh->io_port, CMD_INQUIRY);
0659     aha1542_in(sh->io_port, inquiry_result, 4, 0);
0660     if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
0661         shost_printk(KERN_ERR, sh, "error querying card type\n");
0662     aha1542_intr_reset(sh->io_port);
0663 
0664     aha1542->bios_translation = BIOS_TRANSLATION_6432;  /* Default case */
0665 
0666     /*
0667      * For an AHA1740 series board, we ignore the board since there is a
0668      * hardware bug which can lead to wrong blocks being returned if the board
0669      * is operating in the 1542 emulation mode.  Since there is an extended mode
0670      * driver, we simply ignore the board and let the 1740 driver pick it up.
0671      */
0672 
0673     if (inquiry_result[0] == 0x43) {
0674         shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
0675         return 1;
0676     }
0677 
0678     /*
0679      * Always call this - boards that do not support extended bios translation
0680      * will ignore the command, and we will set the proper default
0681      */
0682 
0683     aha1542->bios_translation = aha1542_mbenable(sh);
0684 
0685     return 0;
0686 }
0687 
0688 static u8 dma_speed_hw(int dma_speed)
0689 {
0690     switch (dma_speed) {
0691     case 5:
0692         return 0x00;
0693     case 6:
0694         return 0x04;
0695     case 7:
0696         return 0x01;
0697     case 8:
0698         return 0x02;
0699     case 10:
0700         return 0x03;
0701     }
0702 
0703     return 0xff;    /* invalid */
0704 }
0705 
0706 /* Set the Bus on/off-times as not to ruin floppy performance */
0707 static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
0708 {
0709     if (bus_on > 0) {
0710         u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
0711 
0712         aha1542_intr_reset(sh->io_port);
0713         if (aha1542_out(sh->io_port, oncmd, 2))
0714             goto fail;
0715     }
0716 
0717     if (bus_off > 0) {
0718         u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
0719 
0720         aha1542_intr_reset(sh->io_port);
0721         if (aha1542_out(sh->io_port, offcmd, 2))
0722             goto fail;
0723     }
0724 
0725     if (dma_speed_hw(dma_speed) != 0xff) {
0726         u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
0727 
0728         aha1542_intr_reset(sh->io_port);
0729         if (aha1542_out(sh->io_port, dmacmd, 2))
0730             goto fail;
0731     }
0732     aha1542_intr_reset(sh->io_port);
0733     return;
0734 fail:
0735     shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
0736     aha1542_intr_reset(sh->io_port);
0737 }
0738 
0739 /* return non-zero on detection */
0740 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
0741 {
0742     unsigned int base_io = io[indx];
0743     struct Scsi_Host *sh;
0744     struct aha1542_hostdata *aha1542;
0745     char dma_info[] = "no DMA";
0746 
0747     if (base_io == 0)
0748         return NULL;
0749 
0750     if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
0751         return NULL;
0752 
0753     sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
0754     if (!sh)
0755         goto release;
0756     aha1542 = shost_priv(sh);
0757 
0758     sh->unique_id = base_io;
0759     sh->io_port = base_io;
0760     sh->n_io_port = AHA1542_REGION_SIZE;
0761     aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
0762     aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
0763 
0764     if (!aha1542_test_port(sh))
0765         goto unregister;
0766 
0767     aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
0768     if (aha1542_query(sh))
0769         goto unregister;
0770     if (aha1542_getconfig(sh) == -1)
0771         goto unregister;
0772 
0773     if (sh->dma_channel != 0xFF)
0774         snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
0775     shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
0776                 sh->this_id, base_io, sh->irq, dma_info);
0777     if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
0778         shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
0779 
0780     if (dma_set_mask_and_coherent(pdev, DMA_BIT_MASK(24)) < 0)
0781         goto unregister;
0782 
0783     aha1542->mb = dma_alloc_coherent(pdev,
0784             AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
0785             &aha1542->mb_handle, GFP_KERNEL);
0786     if (!aha1542->mb)
0787         goto unregister;
0788 
0789     aha1542->ccb = dma_alloc_coherent(pdev,
0790             AHA1542_MAILBOXES * sizeof(struct ccb),
0791             &aha1542->ccb_handle, GFP_KERNEL);
0792     if (!aha1542->ccb)
0793         goto free_mb;
0794 
0795     setup_mailboxes(sh);
0796 
0797     if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
0798         shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
0799         goto free_ccb;
0800     }
0801     if (sh->dma_channel != 0xFF) {
0802         if (request_dma(sh->dma_channel, "aha1542")) {
0803             shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
0804             goto free_irq;
0805         }
0806         if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
0807             set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
0808             enable_dma(sh->dma_channel);
0809         }
0810     }
0811 
0812     if (scsi_add_host(sh, pdev))
0813         goto free_dma;
0814 
0815     scsi_scan_host(sh);
0816 
0817     return sh;
0818 
0819 free_dma:
0820     if (sh->dma_channel != 0xff)
0821         free_dma(sh->dma_channel);
0822 free_irq:
0823     free_irq(sh->irq, sh);
0824 free_ccb:
0825     dma_free_coherent(pdev, AHA1542_MAILBOXES * sizeof(struct ccb),
0826               aha1542->ccb, aha1542->ccb_handle);
0827 free_mb:
0828     dma_free_coherent(pdev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
0829               aha1542->mb, aha1542->mb_handle);
0830 unregister:
0831     scsi_host_put(sh);
0832 release:
0833     release_region(base_io, AHA1542_REGION_SIZE);
0834 
0835     return NULL;
0836 }
0837 
0838 static int aha1542_release(struct Scsi_Host *sh)
0839 {
0840     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0841     struct device *dev = sh->dma_dev;
0842 
0843     scsi_remove_host(sh);
0844     if (sh->dma_channel != 0xff)
0845         free_dma(sh->dma_channel);
0846     dma_free_coherent(dev, AHA1542_MAILBOXES * sizeof(struct ccb),
0847               aha1542->ccb, aha1542->ccb_handle);
0848     dma_free_coherent(dev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
0849               aha1542->mb, aha1542->mb_handle);
0850     if (sh->irq)
0851         free_irq(sh->irq, sh);
0852     if (sh->io_port && sh->n_io_port)
0853         release_region(sh->io_port, sh->n_io_port);
0854     scsi_host_put(sh);
0855     return 0;
0856 }
0857 
0858 
0859 /*
0860  * This is a device reset.  This is handled by sending a special command
0861  * to the device.
0862  */
0863 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
0864 {
0865     struct Scsi_Host *sh = cmd->device->host;
0866     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0867     unsigned long flags;
0868     struct mailbox *mb = aha1542->mb;
0869     u8 target = cmd->device->id;
0870     u8 lun = cmd->device->lun;
0871     int mbo;
0872     struct ccb *ccb = aha1542->ccb;
0873 
0874     spin_lock_irqsave(sh->host_lock, flags);
0875     mbo = aha1542->aha1542_last_mbo_used + 1;
0876     if (mbo >= AHA1542_MAILBOXES)
0877         mbo = 0;
0878 
0879     do {
0880         if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
0881             break;
0882         mbo++;
0883         if (mbo >= AHA1542_MAILBOXES)
0884             mbo = 0;
0885     } while (mbo != aha1542->aha1542_last_mbo_used);
0886 
0887     if (mb[mbo].status || aha1542->int_cmds[mbo])
0888         panic("Unable to find empty mailbox for aha1542.\n");
0889 
0890     aha1542->int_cmds[mbo] = cmd;   /* This will effectively
0891                      * prevent someone else from
0892                      * screwing with this cdb.
0893                      */
0894 
0895     aha1542->aha1542_last_mbo_used = mbo;
0896 
0897     /* This gets trashed for some reason */
0898     any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
0899 
0900     memset(&ccb[mbo], 0, sizeof(struct ccb));
0901 
0902     ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
0903 
0904     ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);     /*SCSI Target Id */
0905 
0906     ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
0907     ccb[mbo].commlinkid = 0;
0908 
0909     /*
0910      * Now tell the 1542 to flush all pending commands for this
0911      * target
0912      */
0913     aha1542_outb(sh->io_port, CMD_START_SCSI);
0914     spin_unlock_irqrestore(sh->host_lock, flags);
0915 
0916     scmd_printk(KERN_WARNING, cmd,
0917         "Trying device reset for target\n");
0918 
0919     return SUCCESS;
0920 }
0921 
0922 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
0923 {
0924     struct Scsi_Host *sh = cmd->device->host;
0925     struct aha1542_hostdata *aha1542 = shost_priv(sh);
0926     unsigned long flags;
0927     int i;
0928 
0929     spin_lock_irqsave(sh->host_lock, flags);
0930     /*
0931      * This does a scsi reset for all devices on the bus.
0932      * In principle, we could also reset the 1542 - should
0933      * we do this?  Try this first, and we can add that later
0934      * if it turns out to be useful.
0935      */
0936     outb(reset_cmd, CONTROL(cmd->device->host->io_port));
0937 
0938     if (!wait_mask(STATUS(cmd->device->host->io_port),
0939          STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
0940         spin_unlock_irqrestore(sh->host_lock, flags);
0941         return FAILED;
0942     }
0943 
0944     /*
0945      * We need to do this too before the 1542 can interact with
0946      * us again after host reset.
0947      */
0948     if (reset_cmd & HRST)
0949         setup_mailboxes(cmd->device->host);
0950 
0951     /*
0952      * Now try to pick up the pieces.  For all pending commands,
0953      * free any internal data structures, and basically clear things
0954      * out.  We do not try and restart any commands or anything -
0955      * the strategy handler takes care of that crap.
0956      */
0957     shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
0958 
0959     for (i = 0; i < AHA1542_MAILBOXES; i++) {
0960         if (aha1542->int_cmds[i] != NULL) {
0961             struct scsi_cmnd *tmp_cmd;
0962             tmp_cmd = aha1542->int_cmds[i];
0963 
0964             if (tmp_cmd->device->soft_reset) {
0965                 /*
0966                  * If this device implements the soft reset option,
0967                  * then it is still holding onto the command, and
0968                  * may yet complete it.  In this case, we don't
0969                  * flush the data.
0970                  */
0971                 continue;
0972             }
0973             aha1542_free_cmd(tmp_cmd);
0974             aha1542->int_cmds[i] = NULL;
0975             aha1542->mb[i].status = 0;
0976         }
0977     }
0978 
0979     spin_unlock_irqrestore(sh->host_lock, flags);
0980     return SUCCESS;
0981 }
0982 
0983 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
0984 {
0985     return aha1542_reset(cmd, SCRST);
0986 }
0987 
0988 static int aha1542_host_reset(struct scsi_cmnd *cmd)
0989 {
0990     return aha1542_reset(cmd, HRST | SCRST);
0991 }
0992 
0993 static int aha1542_biosparam(struct scsi_device *sdev,
0994         struct block_device *bdev, sector_t capacity, int geom[])
0995 {
0996     struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
0997 
0998     if (capacity >= 0x200000 &&
0999             aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1000         /* Please verify that this is the same as what DOS returns */
1001         geom[0] = 255;  /* heads */
1002         geom[1] = 63;   /* sectors */
1003     } else {
1004         geom[0] = 64;   /* heads */
1005         geom[1] = 32;   /* sectors */
1006     }
1007     geom[2] = sector_div(capacity, geom[0] * geom[1]);  /* cylinders */
1008 
1009     return 0;
1010 }
1011 MODULE_LICENSE("GPL");
1012 
1013 static int aha1542_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1014 {
1015     struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1016 
1017     acmd->data_buffer = dma_alloc_coherent(shost->dma_dev,
1018             SECTOR_SIZE * AHA1542_MAX_SECTORS,
1019             &acmd->data_buffer_handle, GFP_KERNEL);
1020     if (!acmd->data_buffer)
1021         return -ENOMEM;
1022     return 0;
1023 }
1024 
1025 static int aha1542_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1026 {
1027     struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1028 
1029     dma_free_coherent(shost->dma_dev, SECTOR_SIZE * AHA1542_MAX_SECTORS,
1030             acmd->data_buffer, acmd->data_buffer_handle);
1031     return 0;
1032 }
1033 
1034 static struct scsi_host_template driver_template = {
1035     .module         = THIS_MODULE,
1036     .proc_name      = "aha1542",
1037     .name           = "Adaptec 1542",
1038     .cmd_size       = sizeof(struct aha1542_cmd),
1039     .queuecommand       = aha1542_queuecommand,
1040     .eh_device_reset_handler= aha1542_dev_reset,
1041     .eh_bus_reset_handler   = aha1542_bus_reset,
1042     .eh_host_reset_handler  = aha1542_host_reset,
1043     .bios_param     = aha1542_biosparam,
1044     .init_cmd_priv      = aha1542_init_cmd_priv,
1045     .exit_cmd_priv      = aha1542_exit_cmd_priv,
1046     .can_queue      = AHA1542_MAILBOXES,
1047     .this_id        = 7,
1048     .max_sectors        = AHA1542_MAX_SECTORS,
1049     .sg_tablesize       = SG_ALL,
1050 };
1051 
1052 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1053 {
1054     struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1055 
1056     if (!sh)
1057         return 0;
1058 
1059     dev_set_drvdata(pdev, sh);
1060     return 1;
1061 }
1062 
1063 static void aha1542_isa_remove(struct device *pdev,
1064                     unsigned int ndev)
1065 {
1066     aha1542_release(dev_get_drvdata(pdev));
1067     dev_set_drvdata(pdev, NULL);
1068 }
1069 
1070 static struct isa_driver aha1542_isa_driver = {
1071     .match      = aha1542_isa_match,
1072     .remove     = aha1542_isa_remove,
1073     .driver     = {
1074         .name   = "aha1542"
1075     },
1076 };
1077 static int isa_registered;
1078 
1079 #ifdef CONFIG_PNP
1080 static const struct pnp_device_id aha1542_pnp_ids[] = {
1081     { .id = "ADP1542" },
1082     { .id = "" }
1083 };
1084 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1085 
1086 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1087 {
1088     int indx;
1089     struct Scsi_Host *sh;
1090 
1091     for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1092         if (io[indx])
1093             continue;
1094 
1095         if (pnp_activate_dev(pdev) < 0)
1096             continue;
1097 
1098         io[indx] = pnp_port_start(pdev, 0);
1099 
1100         /*
1101          * The card can be queried for its DMA, we have
1102          * the DMA set up that is enough
1103          */
1104 
1105         dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1106     }
1107 
1108     sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1109     if (!sh)
1110         return -ENODEV;
1111 
1112     pnp_set_drvdata(pdev, sh);
1113     return 0;
1114 }
1115 
1116 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1117 {
1118     aha1542_release(pnp_get_drvdata(pdev));
1119     pnp_set_drvdata(pdev, NULL);
1120 }
1121 
1122 static struct pnp_driver aha1542_pnp_driver = {
1123     .name       = "aha1542",
1124     .id_table   = aha1542_pnp_ids,
1125     .probe      = aha1542_pnp_probe,
1126     .remove     = aha1542_pnp_remove,
1127 };
1128 static int pnp_registered;
1129 #endif /* CONFIG_PNP */
1130 
1131 static int __init aha1542_init(void)
1132 {
1133     int ret = 0;
1134 
1135 #ifdef CONFIG_PNP
1136     if (isapnp) {
1137         ret = pnp_register_driver(&aha1542_pnp_driver);
1138         if (!ret)
1139             pnp_registered = 1;
1140     }
1141 #endif
1142     ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1143     if (!ret)
1144         isa_registered = 1;
1145 
1146 #ifdef CONFIG_PNP
1147     if (pnp_registered)
1148         ret = 0;
1149 #endif
1150     if (isa_registered)
1151         ret = 0;
1152 
1153     return ret;
1154 }
1155 
1156 static void __exit aha1542_exit(void)
1157 {
1158 #ifdef CONFIG_PNP
1159     if (pnp_registered)
1160         pnp_unregister_driver(&aha1542_pnp_driver);
1161 #endif
1162     if (isa_registered)
1163         isa_unregister_driver(&aha1542_isa_driver);
1164 }
1165 
1166 module_init(aha1542_init);
1167 module_exit(aha1542_exit);