linux/drivers/scsi/aha1542.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  Driver for Adaptec AHA-1542 SCSI host adapters
   4 *
   5 *  Copyright (C) 1992  Tommy Thorn
   6 *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
   7 *  Copyright (C) 2015 Ondrej Zary
   8 */
   9
  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>
  21#include <linux/io.h>
  22#include <asm/dma.h>
  23#include <scsi/scsi_cmnd.h>
  24#include <scsi/scsi_device.h>
  25#include <scsi/scsi_host.h>
  26#include "aha1542.h"
  27
  28#define MAXBOARDS 4
  29
  30static bool isapnp = 1;
  31module_param(isapnp, bool, 0);
  32MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
  33
  34static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
  35module_param_hw_array(io, int, ioport, NULL, 0);
  36MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
  37
  38/* time AHA spends on the AT-bus during data transfer */
  39static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
  40module_param_array(bus_on, int, NULL, 0);
  41MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
  42
  43/* time AHA spends off the bus (not to monopolize it) during data transfer  */
  44static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
  45module_param_array(bus_off, int, NULL, 0);
  46MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
  47
  48/* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
  49static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
  50module_param_array(dma_speed, int, NULL, 0);
  51MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
  52
  53#define BIOS_TRANSLATION_6432 1 /* Default case these days */
  54#define BIOS_TRANSLATION_25563 2        /* Big disk case */
  55
  56struct 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];
  62        struct mailbox *mb;
  63        dma_addr_t mb_handle;
  64        struct ccb *ccb;
  65        dma_addr_t ccb_handle;
  66};
  67
  68#define AHA1542_MAX_SECTORS       16
  69
  70struct aha1542_cmd {
  71        /* bounce buffer */
  72        void *data_buffer;
  73        dma_addr_t data_buffer_handle;
  74};
  75
  76static inline void aha1542_intr_reset(u16 base)
  77{
  78        outb(IRST, CONTROL(base));
  79}
  80
  81static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
  82{
  83        bool delayed = true;
  84
  85        if (timeout == 0) {
  86                timeout = 3000000;
  87                delayed = false;
  88        }
  89
  90        while (1) {
  91                u8 bits = inb(port) & mask;
  92                if ((bits & allof) == allof && ((bits & noneof) == 0))
  93                        break;
  94                if (delayed)
  95                        mdelay(1);
  96                if (--timeout == 0)
  97                        return false;
  98        }
  99
 100        return true;
 101}
 102
 103static int aha1542_outb(unsigned int base, u8 val)
 104{
 105        if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
 106                return 1;
 107        outb(val, DATA(base));
 108
 109        return 0;
 110}
 111
 112static int aha1542_out(unsigned int base, u8 *buf, int len)
 113{
 114        while (len--) {
 115                if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
 116                        return 1;
 117                outb(*buf++, DATA(base));
 118        }
 119        if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
 120                return 1;
 121
 122        return 0;
 123}
 124
 125/*
 126 * Only used at boot time, so we do not need to worry about latency as much
 127 * here
 128 */
 129
 130static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
 131{
 132        while (len--) {
 133                if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
 134                        return 1;
 135                *buf++ = inb(DATA(base));
 136        }
 137        return 0;
 138}
 139
 140static int makecode(unsigned hosterr, unsigned scsierr)
 141{
 142        switch (hosterr) {
 143        case 0x0:
 144        case 0xa:               /* Linked command complete without error and linked normally */
 145        case 0xb:               /* Linked command complete without error, interrupt generated */
 146                hosterr = 0;
 147                break;
 148
 149        case 0x11:              /* Selection time out-The initiator selection or target
 150                                 * reselection was not complete within the SCSI Time out period
 151                                 */
 152                hosterr = DID_TIME_OUT;
 153                break;
 154
 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.
 158                                 */
 159
 160        case 0x13:              /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
 161
 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.
 164                                 */
 165
 166        case 0x16:              /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
 167                                 * This usually indicates a software failure.
 168                                 */
 169
 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
 172                                 * the first.
 173                                 */
 174        case 0x18:              /* Invalid Target Direction received from Host-The direction of a
 175                                 * Target Mode CCB was invalid.
 176                                 */
 177
 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.
 181                                 */
 182
 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.
 186                                 */
 187#ifdef DEBUG
 188                printk("Aha1542: %x %x\n", hosterr, scsierr);
 189#endif
 190                hosterr = DID_ERROR;    /* Couldn't find any better */
 191                break;
 192
 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
 196                                 * a SCRD interrupt
 197                                 */
 198                hosterr = DID_RESET;
 199                break;
 200        default:
 201                printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
 202                break;
 203        }
 204        return scsierr | (hosterr << 16);
 205}
 206
 207static int aha1542_test_port(struct Scsi_Host *sh)
 208{
 209        u8 inquiry_result[4];
 210        int i;
 211
 212        /* Quick and dirty test for presence of the card. */
 213        if (inb(STATUS(sh->io_port)) == 0xff)
 214                return 0;
 215
 216        /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
 217
 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 */
 220
 221        outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
 222
 223        mdelay(20);             /* Wait a little bit for things to settle down. */
 224
 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))
 227                return 0;
 228
 229        /* Shouldn't have generated any interrupts during reset */
 230        if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
 231                return 0;
 232
 233        /*
 234         * Perform a host adapter inquiry instead so we do not need to set
 235         * up the mailboxes ahead of time
 236         */
 237
 238        aha1542_outb(sh->io_port, CMD_INQUIRY);
 239
 240        for (i = 0; i < 4; i++) {
 241                if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
 242                        return 0;
 243                inquiry_result[i] = inb(DATA(sh->io_port));
 244        }
 245
 246        /* Reading port should reset DF */
 247        if (inb(STATUS(sh->io_port)) & DF)
 248                return 0;
 249
 250        /* When HACC, command is completed, and we're though testing */
 251        if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
 252                return 0;
 253
 254        /* Clear interrupts */
 255        outb(IRST, CONTROL(sh->io_port));
 256
 257        return 1;
 258}
 259
 260static void aha1542_free_cmd(struct scsi_cmnd *cmd)
 261{
 262        struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
 263
 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;
 268                struct bio_vec bv;
 269
 270                rq_for_each_segment(bv, rq, iter) {
 271                        memcpy_to_page(bv.bv_page, bv.bv_offset, buf,
 272                                       bv.bv_len);
 273                        buf += bv.bv_len;
 274                }
 275        }
 276
 277        scsi_dma_unmap(cmd);
 278}
 279
 280static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
 281{
 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;
 286        int number_serviced;
 287        unsigned long flags;
 288        struct scsi_cmnd *tmp_cmd;
 289        int flag;
 290        struct mailbox *mb = aha1542->mb;
 291        struct ccb *ccb = aha1542->ccb;
 292
 293#ifdef DEBUG
 294        {
 295                flag = inb(INTRFLAGS(sh->io_port));
 296                shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
 297                if (!(flag & ANYINTR))
 298                        printk("no interrupt?");
 299                if (flag & MBIF)
 300                        printk("MBIF ");
 301                if (flag & MBOA)
 302                        printk("MBOF ");
 303                if (flag & HACC)
 304                        printk("HACC ");
 305                if (flag & SCRD)
 306                        printk("SCRD ");
 307                printk("status %02x\n", inb(STATUS(sh->io_port)));
 308        };
 309#endif
 310        number_serviced = 0;
 311
 312        spin_lock_irqsave(sh->host_lock, flags);
 313        while (1) {
 314                flag = inb(INTRFLAGS(sh->io_port));
 315
 316                /*
 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.
 321                 */
 322                if (flag & ~MBIF) {
 323                        if (flag & MBOA)
 324                                printk("MBOF ");
 325                        if (flag & HACC)
 326                                printk("HACC ");
 327                        if (flag & SCRD)
 328                                printk("SCRD ");
 329                }
 330                aha1542_intr_reset(sh->io_port);
 331
 332                mbi = aha1542->aha1542_last_mbi_used + 1;
 333                if (mbi >= 2 * AHA1542_MAILBOXES)
 334                        mbi = AHA1542_MAILBOXES;
 335
 336                do {
 337                        if (mb[mbi].status != 0)
 338                                break;
 339                        mbi++;
 340                        if (mbi >= 2 * AHA1542_MAILBOXES)
 341                                mbi = AHA1542_MAILBOXES;
 342                } while (mbi != aha1542->aha1542_last_mbi_used);
 343
 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");
 349                        return IRQ_HANDLED;
 350                };
 351
 352                mbo = (scsi2int(mb[mbi].ccbptr) - (unsigned long)aha1542->ccb_handle) / sizeof(struct ccb);
 353                mbistatus = mb[mbi].status;
 354                mb[mbi].status = 0;
 355                aha1542->aha1542_last_mbi_used = mbi;
 356
 357#ifdef DEBUG
 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);
 361#endif
 362
 363                if (mbistatus == 3)
 364                        continue;       /* Aborted command not found */
 365
 366#ifdef DEBUG
 367                shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
 368#endif
 369
 370                tmp_cmd = aha1542->int_cmds[mbo];
 371
 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);
 377                        return IRQ_HANDLED;
 378                }
 379                my_done = tmp_cmd->scsi_done;
 380                aha1542_free_cmd(tmp_cmd);
 381                /*
 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
 385                 */
 386                if (ccb[mbo].tarstat == 2)
 387                        memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
 388                               SCSI_SENSE_BUFFERSIZE);
 389
 390
 391                /* is there mail :-) */
 392
 393                /* more error checking left out here */
 394                if (mbistatus != 1)
 395                        /* This is surely wrong, but I don't know what's right */
 396                        errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
 397                else
 398                        errstatus = 0;
 399
 400#ifdef DEBUG
 401                if (errstatus)
 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);
 406                if (errstatus)
 407                        printk("aha1542_intr_handle: returning %6x\n", errstatus);
 408#endif
 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
 412                                                 */
 413                my_done(tmp_cmd);
 414                number_serviced++;
 415        };
 416}
 417
 418static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
 419{
 420        struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
 421        struct aha1542_hostdata *aha1542 = shost_priv(sh);
 422        u8 direction;
 423        u8 target = cmd->device->id;
 424        u8 lun = cmd->device->lun;
 425        unsigned long flags;
 426        int bufflen = scsi_bufflen(cmd);
 427        int mbo;
 428        struct mailbox *mb = aha1542->mb;
 429        struct ccb *ccb = aha1542->ccb;
 430
 431        if (*cmd->cmnd == REQUEST_SENSE) {
 432                /* Don't do the command - we have the sense data already */
 433                cmd->result = 0;
 434                cmd->scsi_done(cmd);
 435                return 0;
 436        }
 437#ifdef DEBUG
 438        {
 439                int i = -1;
 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);
 447        }
 448#endif
 449
 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;
 454                struct bio_vec bv;
 455
 456                rq_for_each_segment(bv, rq, iter) {
 457                        memcpy_from_page(buf, bv.bv_page, bv.bv_offset,
 458                                         bv.bv_len);
 459                        buf += bv.bv_len;
 460                }
 461        }
 462
 463        /*
 464         * Use the outgoing mailboxes in a round-robin fashion, because this
 465         * is how the host adapter will scan for them
 466         */
 467
 468        spin_lock_irqsave(sh->host_lock, flags);
 469        mbo = aha1542->aha1542_last_mbo_used + 1;
 470        if (mbo >= AHA1542_MAILBOXES)
 471                mbo = 0;
 472
 473        do {
 474                if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
 475                        break;
 476                mbo++;
 477                if (mbo >= AHA1542_MAILBOXES)
 478                        mbo = 0;
 479        } while (mbo != aha1542->aha1542_last_mbo_used);
 480
 481        if (mb[mbo].status || aha1542->int_cmds[mbo])
 482                panic("Unable to find empty mailbox for aha1542.\n");
 483
 484        aha1542->int_cmds[mbo] = cmd;   /* This will effectively prevent someone else from
 485                                         * screwing with this cdb.
 486                                         */
 487
 488        aha1542->aha1542_last_mbo_used = mbo;
 489
 490#ifdef DEBUG
 491        shost_printk(KERN_DEBUG, sh, "Sending command (%d %p)...", mbo, cmd->scsi_done);
 492#endif
 493
 494        /* This gets trashed for some reason */
 495        any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
 496
 497        memset(&ccb[mbo], 0, sizeof(struct ccb));
 498
 499        ccb[mbo].cdblen = cmd->cmd_len;
 500
 501        direction = 0;
 502        if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
 503                direction = 8;
 504        else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
 505                direction = 16;
 506
 507        memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
 508        ccb[mbo].op = 0;        /* SCSI Initiator Command */
 509        any2scsi(ccb[mbo].datalen, bufflen);
 510        if (bufflen)
 511                any2scsi(ccb[mbo].dataptr, acmd->data_buffer_handle);
 512        else
 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;
 518
 519#ifdef DEBUG
 520        print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
 521        printk("aha1542_queuecommand: now waiting for interrupt ");
 522#endif
 523        mb[mbo].status = 1;
 524        aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
 525        spin_unlock_irqrestore(sh->host_lock, flags);
 526
 527        return 0;
 528}
 529
 530/* Initialize mailboxes */
 531static void setup_mailboxes(struct Scsi_Host *sh)
 532{
 533        struct aha1542_hostdata *aha1542 = shost_priv(sh);
 534        u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
 535        int i;
 536
 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;
 542        };
 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);
 548}
 549
 550static int aha1542_getconfig(struct Scsi_Host *sh)
 551{
 552        u8 inquiry_result[3];
 553        int i;
 554        i = inb(STATUS(sh->io_port));
 555        if (i & DF) {
 556                i = inb(DATA(sh->io_port));
 557        };
 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]) {
 564        case 0x80:
 565                sh->dma_channel = 7;
 566                break;
 567        case 0x40:
 568                sh->dma_channel = 6;
 569                break;
 570        case 0x20:
 571                sh->dma_channel = 5;
 572                break;
 573        case 0x01:
 574                sh->dma_channel = 0;
 575                break;
 576        case 0:
 577                /*
 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.
 580                 */
 581                sh->dma_channel = 0xFF;
 582                break;
 583        default:
 584                shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
 585                return -1;
 586        };
 587        switch (inquiry_result[1]) {
 588        case 0x40:
 589                sh->irq = 15;
 590                break;
 591        case 0x20:
 592                sh->irq = 14;
 593                break;
 594        case 0x8:
 595                sh->irq = 12;
 596                break;
 597        case 0x4:
 598                sh->irq = 11;
 599                break;
 600        case 0x2:
 601                sh->irq = 10;
 602                break;
 603        case 0x1:
 604                sh->irq = 9;
 605                break;
 606        default:
 607                shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
 608                return -1;
 609        };
 610        sh->this_id = inquiry_result[2] & 7;
 611        return 0;
 612}
 613
 614/*
 615 * This function should only be called for 1542C boards - we can detect
 616 * the special firmware settings and unlock the board
 617 */
 618
 619static int aha1542_mbenable(struct Scsi_Host *sh)
 620{
 621        static u8 mbenable_cmd[3];
 622        static u8 mbenable_result[2];
 623        int retval;
 624
 625        retval = BIOS_TRANSLATION_6432;
 626
 627        aha1542_outb(sh->io_port, CMD_EXTBIOS);
 628        if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
 629                return retval;
 630        if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
 631                goto fail;
 632        aha1542_intr_reset(sh->io_port);
 633
 634        if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
 635                mbenable_cmd[0] = CMD_MBENABLE;
 636                mbenable_cmd[1] = 0;
 637                mbenable_cmd[2] = mbenable_result[1];
 638
 639                if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
 640                        retval = BIOS_TRANSLATION_25563;
 641
 642                if (aha1542_out(sh->io_port, mbenable_cmd, 3))
 643                        goto fail;
 644        };
 645        while (0) {
 646fail:
 647                shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
 648        }
 649        aha1542_intr_reset(sh->io_port);
 650        return retval;
 651}
 652
 653/* Query the board to find out if it is a 1542 or a 1740, or whatever. */
 654static int aha1542_query(struct Scsi_Host *sh)
 655{
 656        struct aha1542_hostdata *aha1542 = shost_priv(sh);
 657        u8 inquiry_result[4];
 658        int i;
 659        i = inb(STATUS(sh->io_port));
 660        if (i & DF) {
 661                i = inb(DATA(sh->io_port));
 662        };
 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);
 668
 669        aha1542->bios_translation = BIOS_TRANSLATION_6432;      /* Default case */
 670
 671        /*
 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.
 676         */
 677
 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");
 680                return 1;
 681        };
 682
 683        /*
 684         * Always call this - boards that do not support extended bios translation
 685         * will ignore the command, and we will set the proper default
 686         */
 687
 688        aha1542->bios_translation = aha1542_mbenable(sh);
 689
 690        return 0;
 691}
 692
 693static u8 dma_speed_hw(int dma_speed)
 694{
 695        switch (dma_speed) {
 696        case 5:
 697                return 0x00;
 698        case 6:
 699                return 0x04;
 700        case 7:
 701                return 0x01;
 702        case 8:
 703                return 0x02;
 704        case 10:
 705                return 0x03;
 706        }
 707
 708        return 0xff;    /* invalid */
 709}
 710
 711/* Set the Bus on/off-times as not to ruin floppy performance */
 712static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
 713{
 714        if (bus_on > 0) {
 715                u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
 716
 717                aha1542_intr_reset(sh->io_port);
 718                if (aha1542_out(sh->io_port, oncmd, 2))
 719                        goto fail;
 720        }
 721
 722        if (bus_off > 0) {
 723                u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
 724
 725                aha1542_intr_reset(sh->io_port);
 726                if (aha1542_out(sh->io_port, offcmd, 2))
 727                        goto fail;
 728        }
 729
 730        if (dma_speed_hw(dma_speed) != 0xff) {
 731                u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
 732
 733                aha1542_intr_reset(sh->io_port);
 734                if (aha1542_out(sh->io_port, dmacmd, 2))
 735                        goto fail;
 736        }
 737        aha1542_intr_reset(sh->io_port);
 738        return;
 739fail:
 740        shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
 741        aha1542_intr_reset(sh->io_port);
 742}
 743
 744/* return non-zero on detection */
 745static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
 746{
 747        unsigned int base_io = io[indx];
 748        struct Scsi_Host *sh;
 749        struct aha1542_hostdata *aha1542;
 750        char dma_info[] = "no DMA";
 751
 752        if (base_io == 0)
 753                return NULL;
 754
 755        if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
 756                return NULL;
 757
 758        sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
 759        if (!sh)
 760                goto release;
 761        aha1542 = shost_priv(sh);
 762
 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;
 768
 769        if (!aha1542_test_port(sh))
 770                goto unregister;
 771
 772        aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
 773        if (aha1542_query(sh))
 774                goto unregister;
 775        if (aha1542_getconfig(sh) == -1)
 776                goto unregister;
 777
 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");
 784
 785        if (dma_set_mask_and_coherent(pdev, DMA_BIT_MASK(24)) < 0)
 786                goto unregister;
 787
 788        aha1542->mb = dma_alloc_coherent(pdev,
 789                        AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
 790                        &aha1542->mb_handle, GFP_KERNEL);
 791        if (!aha1542->mb)
 792                goto unregister;
 793
 794        aha1542->ccb = dma_alloc_coherent(pdev,
 795                        AHA1542_MAILBOXES * sizeof(struct ccb),
 796                        &aha1542->ccb_handle, GFP_KERNEL);
 797        if (!aha1542->ccb)
 798                goto free_mb;
 799
 800        setup_mailboxes(sh);
 801
 802        if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
 803                shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
 804                goto free_ccb;
 805        }
 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");
 809                        goto free_irq;
 810                }
 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);
 814                }
 815        }
 816
 817        if (scsi_add_host(sh, pdev))
 818                goto free_dma;
 819
 820        scsi_scan_host(sh);
 821
 822        return sh;
 823
 824free_dma:
 825        if (sh->dma_channel != 0xff)
 826                free_dma(sh->dma_channel);
 827free_irq:
 828        free_irq(sh->irq, sh);
 829free_ccb:
 830        dma_free_coherent(pdev, AHA1542_MAILBOXES * sizeof(struct ccb),
 831                          aha1542->ccb, aha1542->ccb_handle);
 832free_mb:
 833        dma_free_coherent(pdev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
 834                          aha1542->mb, aha1542->mb_handle);
 835unregister:
 836        scsi_host_put(sh);
 837release:
 838        release_region(base_io, AHA1542_REGION_SIZE);
 839
 840        return NULL;
 841}
 842
 843static int aha1542_release(struct Scsi_Host *sh)
 844{
 845        struct aha1542_hostdata *aha1542 = shost_priv(sh);
 846        struct device *dev = sh->dma_dev;
 847
 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);
 855        if (sh->irq)
 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);
 859        scsi_host_put(sh);
 860        return 0;
 861}
 862
 863
 864/*
 865 * This is a device reset.  This is handled by sending a special command
 866 * to the device.
 867 */
 868static int aha1542_dev_reset(struct scsi_cmnd *cmd)
 869{
 870        struct Scsi_Host *sh = cmd->device->host;
 871        struct aha1542_hostdata *aha1542 = shost_priv(sh);
 872        unsigned long flags;
 873        struct mailbox *mb = aha1542->mb;
 874        u8 target = cmd->device->id;
 875        u8 lun = cmd->device->lun;
 876        int mbo;
 877        struct ccb *ccb = aha1542->ccb;
 878
 879        spin_lock_irqsave(sh->host_lock, flags);
 880        mbo = aha1542->aha1542_last_mbo_used + 1;
 881        if (mbo >= AHA1542_MAILBOXES)
 882                mbo = 0;
 883
 884        do {
 885                if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
 886                        break;
 887                mbo++;
 888                if (mbo >= AHA1542_MAILBOXES)
 889                        mbo = 0;
 890        } while (mbo != aha1542->aha1542_last_mbo_used);
 891
 892        if (mb[mbo].status || aha1542->int_cmds[mbo])
 893                panic("Unable to find empty mailbox for aha1542.\n");
 894
 895        aha1542->int_cmds[mbo] = cmd;   /* This will effectively
 896                                         * prevent someone else from
 897                                         * screwing with this cdb.
 898                                         */
 899
 900        aha1542->aha1542_last_mbo_used = mbo;
 901
 902        /* This gets trashed for some reason */
 903        any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
 904
 905        memset(&ccb[mbo], 0, sizeof(struct ccb));
 906
 907        ccb[mbo].op = 0x81;     /* BUS DEVICE RESET */
 908
 909        ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);         /*SCSI Target Id */
 910
 911        ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
 912        ccb[mbo].commlinkid = 0;
 913
 914        /*
 915         * Now tell the 1542 to flush all pending commands for this
 916         * target
 917         */
 918        aha1542_outb(sh->io_port, CMD_START_SCSI);
 919        spin_unlock_irqrestore(sh->host_lock, flags);
 920
 921        scmd_printk(KERN_WARNING, cmd,
 922                "Trying device reset for target\n");
 923
 924        return SUCCESS;
 925}
 926
 927static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
 928{
 929        struct Scsi_Host *sh = cmd->device->host;
 930        struct aha1542_hostdata *aha1542 = shost_priv(sh);
 931        unsigned long flags;
 932        int i;
 933
 934        spin_lock_irqsave(sh->host_lock, flags);
 935        /*
 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.
 940         */
 941        outb(reset_cmd, CONTROL(cmd->device->host->io_port));
 942
 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);
 946                return FAILED;
 947        }
 948
 949        /*
 950         * We need to do this too before the 1542 can interact with
 951         * us again after host reset.
 952         */
 953        if (reset_cmd & HRST)
 954                setup_mailboxes(cmd->device->host);
 955
 956        /*
 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.
 961         */
 962        shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
 963
 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];
 968
 969                        if (tmp_cmd->device->soft_reset) {
 970                                /*
 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
 974                                 * flush the data.
 975                                 */
 976                                continue;
 977                        }
 978                        aha1542_free_cmd(tmp_cmd);
 979                        aha1542->int_cmds[i] = NULL;
 980                        aha1542->mb[i].status = 0;
 981                }
 982        }
 983
 984        spin_unlock_irqrestore(sh->host_lock, flags);
 985        return SUCCESS;
 986}
 987
 988static int aha1542_bus_reset(struct scsi_cmnd *cmd)
 989{
 990        return aha1542_reset(cmd, SCRST);
 991}
 992
 993static int aha1542_host_reset(struct scsi_cmnd *cmd)
 994{
 995        return aha1542_reset(cmd, HRST | SCRST);
 996}
 997
 998static int aha1542_biosparam(struct scsi_device *sdev,
 999                struct block_device *bdev, sector_t capacity, int geom[])
1000{
1001        struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1002
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 */
1008        } else {
1009                geom[0] = 64;   /* heads */
1010                geom[1] = 32;   /* sectors */
1011        }
1012        geom[2] = sector_div(capacity, geom[0] * geom[1]);      /* cylinders */
1013
1014        return 0;
1015}
1016MODULE_LICENSE("GPL");
1017
1018static int aha1542_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1019{
1020        struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1021
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)
1026                return -ENOMEM;
1027        return 0;
1028}
1029
1030static int aha1542_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1031{
1032        struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1033
1034        dma_free_coherent(shost->dma_dev, SECTOR_SIZE * AHA1542_MAX_SECTORS,
1035                        acmd->data_buffer, acmd->data_buffer_handle);
1036        return 0;
1037}
1038
1039static 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,
1052        .this_id                = 7,
1053        .max_sectors            = AHA1542_MAX_SECTORS,
1054        .sg_tablesize           = SG_ALL,
1055};
1056
1057static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1058{
1059        struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1060
1061        if (!sh)
1062                return 0;
1063
1064        dev_set_drvdata(pdev, sh);
1065        return 1;
1066}
1067
1068static void aha1542_isa_remove(struct device *pdev,
1069                                    unsigned int ndev)
1070{
1071        aha1542_release(dev_get_drvdata(pdev));
1072        dev_set_drvdata(pdev, NULL);
1073}
1074
1075static struct isa_driver aha1542_isa_driver = {
1076        .match          = aha1542_isa_match,
1077        .remove         = aha1542_isa_remove,
1078        .driver         = {
1079                .name   = "aha1542"
1080        },
1081};
1082static int isa_registered;
1083
1084#ifdef CONFIG_PNP
1085static const struct pnp_device_id aha1542_pnp_ids[] = {
1086        { .id = "ADP1542" },
1087        { .id = "" }
1088};
1089MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1090
1091static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1092{
1093        int indx;
1094        struct Scsi_Host *sh;
1095
1096        for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1097                if (io[indx])
1098                        continue;
1099
1100                if (pnp_activate_dev(pdev) < 0)
1101                        continue;
1102
1103                io[indx] = pnp_port_start(pdev, 0);
1104
1105                /*
1106                 * The card can be queried for its DMA, we have
1107                 * the DMA set up that is enough
1108                 */
1109
1110                dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1111        }
1112
1113        sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1114        if (!sh)
1115                return -ENODEV;
1116
1117        pnp_set_drvdata(pdev, sh);
1118        return 0;
1119}
1120
1121static void aha1542_pnp_remove(struct pnp_dev *pdev)
1122{
1123        aha1542_release(pnp_get_drvdata(pdev));
1124        pnp_set_drvdata(pdev, NULL);
1125}
1126
1127static struct pnp_driver aha1542_pnp_driver = {
1128        .name           = "aha1542",
1129        .id_table       = aha1542_pnp_ids,
1130        .probe          = aha1542_pnp_probe,
1131        .remove         = aha1542_pnp_remove,
1132};
1133static int pnp_registered;
1134#endif /* CONFIG_PNP */
1135
1136static int __init aha1542_init(void)
1137{
1138        int ret = 0;
1139
1140#ifdef CONFIG_PNP
1141        if (isapnp) {
1142                ret = pnp_register_driver(&aha1542_pnp_driver);
1143                if (!ret)
1144                        pnp_registered = 1;
1145        }
1146#endif
1147        ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1148        if (!ret)
1149                isa_registered = 1;
1150
1151#ifdef CONFIG_PNP
1152        if (pnp_registered)
1153                ret = 0;
1154#endif
1155        if (isa_registered)
1156                ret = 0;
1157
1158        return ret;
1159}
1160
1161static void __exit aha1542_exit(void)
1162{
1163#ifdef CONFIG_PNP
1164        if (pnp_registered)
1165                pnp_unregister_driver(&aha1542_pnp_driver);
1166#endif
1167        if (isa_registered)
1168                isa_unregister_driver(&aha1542_isa_driver);
1169}
1170
1171module_init(aha1542_init);
1172module_exit(aha1542_exit);
1173