root/drivers/scsi/eata.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. wait_on_busy
  2. do_dma
  3. read_pio
  4. port_detect
  5. eata2x_detect
  6. build_sg_list
  7. eata2x_queuecommand
  8. eata2x_abort
  9. eata2x_reset
  10. eata2x_interrupt_handler

   1 /*
   2  *      eata.c - Low-level driver for EATA/DMA SCSI host adapters.
   3  *
   4  *      11 Feb 1995 rev. 1.17 for linux 1.1.91
   5  *          Now DEBUG_RESET is disabled by default.
   6  *          Register a board even if it does not assert DMA protocol support
   7  *          (DPT SK2011B does not report correctly the dmasup bit).
   8  *
   9  *       9 Feb 1995 rev. 1.16 for linux 1.1.90
  10  *          Use host->wish_block instead of host->block.
  11  *          New list of Data Out SCSI commands.
  12  *
  13  *       8 Feb 1995 rev. 1.15 for linux 1.1.89
  14  *          Cleared target_time_out counter while performing a reset.
  15  *          All external symbols renamed to avoid possible name conflicts.
  16  *
  17  *      28 Jan 1995 rev. 1.14 for linux 1.1.86
  18  *          Added module support.
  19  *          Log and do a retry when a disk drive returns a target status 
  20  *          different from zero on a recovered error.
  21  *
  22  *      24 Jan 1995 rev. 1.13 for linux 1.1.85
  23  *          Use optimized board configuration, with a measured performance
  24  *          increase in the range 10%-20% on i/o throughput.
  25  *
  26  *      16 Jan 1995 rev. 1.12 for linux 1.1.81
  27  *          Fix mscp structure comments (no functional change).
  28  *          Display a message if check_region detects a port address
  29  *          already in use.
  30  *
  31  *      17 Dec 1994 rev. 1.11 for linux 1.1.74
  32  *          Use the scsicam_bios_param routine. This allows an easy
  33  *          migration path from disk partition tables created using 
  34  *          different SCSI drivers and non optimal disk geometry.
  35  *
  36  *      15 Dec 1994 rev. 1.10 for linux 1.1.74
  37  *          Added support for ISA EATA boards (DPT PM2011, DPT PM2021).
  38  *          The host->block flag is set for all the detected ISA boards.
  39  *          The detect routine no longer enforces LEVEL triggering
  40  *          for EISA boards, it just prints a warning message.
  41  *
  42  *      30 Nov 1994 rev. 1.09 for linux 1.1.68
  43  *          Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
  44  *          Added optional support for using a single board at a time.
  45  *
  46  *      18 Nov 1994 rev. 1.08 for linux 1.1.64
  47  *          Forces sg_tablesize = 64 and can_queue = 64 if these
  48  *          values are not correctly detected (DPT PM2012).
  49  *
  50  *      14 Nov 1994 rev. 1.07 for linux 1.1.63  Final BETA release.
  51  *      04 Aug 1994 rev. 1.00 for linux 1.1.39  First BETA release.
  52  *
  53  *
  54  *          This driver is based on the CAM (Common Access Method Committee)
  55  *          EATA (Enhanced AT Bus Attachment) rev. 2.0A, using DMA protocol.
  56  *
  57  *      Copyright (C) 1994, 1995 Dario Ballabio (dario@milano.europe.dg.com)
  58  *
  59  */
  60 
  61 /*
  62  *
  63  *  Here is a brief description of the DPT SCSI host adapters.
  64  *  All these boards provide an EATA/DMA compatible programming interface
  65  *  and are fully supported by this driver:
  66  *
  67  *  PM2011B/9X -  Entry Level ISA
  68  *  PM2021A/9X -  High Performance ISA
  69  *  PM2012A       Old EISA
  70  *  PM2012B       Old EISA
  71  *  PM2022A/9X -  Entry Level EISA
  72  *  PM2122A/9X -  High Performance EISA
  73  *  PM2322A/9X -  Extra High Performance EISA
  74  *
  75  *  The DPT PM2001 provides only the EATA/PIO interface and hence is not
  76  *  supported by this driver.
  77  *
  78  *  This code has been tested with up to 3 Distributed Processing Technology 
  79  *  PM2122A/9X (DPT SCSI BIOS v002.D1, firmware v05E.0) eisa controllers,
  80  *  no on board cache and no RAID option. 
  81  *  BIOS must be enabled on the first board and must be disabled for all other 
  82  *  boards. 
  83  *  Support is provided for any number of DPT PM2122 eisa boards.
  84  *  All boards should be configured at the same IRQ level.
  85  *  Multiple IRQ configurations are supported too.
  86  *  Boards can be located in any eisa slot (1-15) and are named EATA0, 
  87  *  EATA1,... in increasing eisa slot number. ISA boards are detected
  88  *  after the eisa slot probes.
  89  *
  90  *  The IRQ for EISA boards should be _level_ triggered (not _edge_ triggered).
  91  *  This is a requirement in order to support multiple boards on the same IRQ.
  92  *
  93  *  Other eisa configuration parameters are:
  94  *
  95  *  COMMAND QUEUING   : ENABLED
  96  *  COMMAND TIMEOUT   : ENABLED
  97  *  CACHE             : DISABLED
  98  *
  99  *  In order to support multiple ISA boards in a reliable way,
 100  *  the driver sets host->wish_block = TRUE for all ISA boards.
 101  */
 102 
 103 #if defined(MODULE)
 104 #include <linux/module.h>
 105 #include <linux/version.h>
 106 #endif
 107 
 108 #include <linux/string.h>
 109 #include <linux/sched.h>
 110 #include <linux/kernel.h>
 111 #include <linux/ioport.h>
 112 #include <asm/io.h>
 113 #include <asm/system.h>
 114 #include "../block/blk.h"
 115 #include "scsi.h"
 116 #include "hosts.h"
 117 #include "sd.h"
 118 #include <asm/dma.h>
 119 #include <asm/irq.h>
 120 #include "linux/in.h"
 121 #include "eata.h"
 122 
 123 /* Subversion values */
 124 #define ISA  0
 125 #define ESA 1
 126 
 127 #undef FORCE_CONFIG
 128 
 129 #undef  DEBUG_DETECT
 130 #undef  DEBUG_INTERRUPT
 131 #undef  DEBUG_STATISTICS
 132 #undef  DEBUG_RESET
 133 
 134 #define MAX_TARGET 8
 135 #define MAX_IRQ 16
 136 #define MAX_BOARDS 18
 137 #define MAX_MAILBOXES 64
 138 #define MAX_SGLIST 64
 139 #define MAX_CMD_PER_LUN 2
 140 
 141 #define FALSE 0
 142 #define TRUE 1
 143 #define FREE 0
 144 #define IN_USE   1
 145 #define LOCKED   2
 146 #define IN_RESET 3
 147 #define IGNORE   4
 148 #define NO_IRQ  0xff
 149 #define NO_DMA  0xff
 150 #define MAXLOOP 200000
 151 
 152 #define REG_CMD         7
 153 #define REG_STATUS      7
 154 #define REG_AUX_STATUS  8
 155 #define REG_DATA        0
 156 #define REG_DATA2       1
 157 #define REG_SEE         6
 158 #define REG_LOW         2
 159 #define REG_LM          3
 160 #define REG_MID         4
 161 #define REG_MSB         5
 162 #define REGION_SIZE     9
 163 #define EISA_RANGE      0xf000
 164 #define BSY_ASSERTED      0x80
 165 #define DRQ_ASSERTED      0x08
 166 #define ABSY_ASSERTED     0x01
 167 #define IRQ_ASSERTED      0x02
 168 #define READ_CONFIG_PIO   0xf0
 169 #define SET_CONFIG_PIO    0xf1
 170 #define SEND_CP_PIO       0xf2
 171 #define RECEIVE_SP_PIO    0xf3
 172 #define TRUNCATE_XFR_PIO  0xf4
 173 #define RESET_PIO         0xf9
 174 #define READ_CONFIG_DMA   0xfd
 175 #define SET_CONFIG_DMA    0xfe
 176 #define SEND_CP_DMA       0xff
 177 #define ASOK              0x00
 178 #define ASST              0x01
 179 
 180 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr)[0])
 181 
 182 /* "EATA", in Big Endian format */
 183 #define EATA_SIGNATURE 0x41544145
 184 
 185 /* Number of valid bytes in the board config structure for EATA 2.0x */
 186 #define EATA_2_0A_SIZE 28
 187 #define EATA_2_0B_SIZE 30
 188 
 189 /* Board info structure */
 190 struct eata_info {
 191    ulong  data_len;     /* Number of valid bytes after this field */
 192    ulong  sign;         /* ASCII "EATA" signature */
 193    unchar        :4,    /* unused low nibble */
 194           version:4;    /* EATA version, should be 0x1 */
 195    unchar  ocsena:1,    /* Overlap Command Support Enabled */
 196            tarsup:1,    /* Target Mode Supported */
 197                  :2,
 198            dmasup:1,    /* DMA Supported */
 199            drqvld:1,    /* DRQ Index (DRQX) is valid */
 200               ata:1,    /* This is an ATA device */
 201            haaval:1;    /* Host Adapter Address Valid */
 202    ushort cp_pad_len;   /* Number of pad bytes after cp_len */
 203    unchar host_addr[3]; /* Host Adapter SCSI ID for channels 2, 1, 0 */
 204    unchar reserved;
 205    ulong  cp_len;       /* Number of valid bytes in cp */
 206    ulong  sp_len;       /* Number of valid bytes in sp */
 207    ushort queue_size;   /* Max number of cp that can be queued */
 208    ushort unused;
 209    ushort scatt_size;   /* Max number of entries in scatter/gather table */
 210    unchar     irq:4,    /* Interrupt Request assigned to this controller */
 211            irq_tr:1,    /* 0 for edge triggered, 1 for level triggered */
 212            second:1,    /* 1 if this is a secondary (not primary) controller */
 213              drqx:2;    /* DRQ Index (0=DMA0, 1=DMA7, 2=DMA6, 3=DMA5) */
 214    unchar  sync;        /* 1 if scsi target id 7...0 is running sync scsi */
 215 
 216    /* Structure extension defined in EATA 2.0B */
 217    unchar  isaena:1,    /* ISA i/o addressing is disabled/enabled */
 218          forcaddr:1,    /* Port address has been forced */
 219                  :6;
 220    unchar  max_id:5,    /* Max number of SCSI target IDs */
 221          max_chan:3;    /* Max SCSI channel number on this board */
 222 
 223    ushort ipad[249];
 224    };
 225 
 226 /* Board config structure */
 227 struct eata_config {
 228    ushort len;          /* Number of bytes following this field */
 229    unchar edis:1,       /* Disable EATA interface after config command */
 230          ocena:1,       /* Overlapped Commands Enabled */
 231         mdpena:1,       /* Transfer all Modified Data Pointer Messages */
 232         tarena:1,       /* Target Mode Enabled for this controller */
 233               :4;
 234    unchar cpad[511];
 235    };
 236 
 237 /* Returned status packet structure */
 238 struct mssp {
 239    unchar adapter_status:7,    /* State related to current command */
 240                      eoc:1;    /* End Of Command (1 = command completed) */
 241    unchar target_status;       /* SCSI status received after data transfer */
 242    unchar unused[2];
 243    ulong inv_res_len;          /* Number of bytes not transferred */
 244    Scsi_Cmnd *SCpnt;           /* Address set in cp */
 245    char mess[12];
 246    };
 247 
 248 /* MailBox SCSI Command Packet */
 249 struct mscp {
 250    unchar  sreset:1,     /* SCSI Bus Reset Signal should be asserted */
 251              init:1,     /* Re-initialize controller and self test */
 252            reqsen:1,     /* Transfer Request Sense Data to addr using DMA */
 253                sg:1,     /* Use Scatter/Gather */
 254                  :1,
 255            interp:1,     /* The controller interprets cp, not the target */ 
 256              dout:1,     /* Direction of Transfer is Out (Host to Target) */
 257               din:1;     /* Direction of Transfer is In (Target to Host) */
 258    unchar sense_len;     /* Request Sense Length */
 259    unchar unused[4];
 260    unchar phsunit:1,     /* Send to Target Physical Unit (bypass RAID) */
 261           notused:7;
 262    unchar target;        /* SCSI Target ID */
 263    unchar     lun:3,     /* LUN */
 264                  :2,
 265            luntar:1,     /* This cp is for Target (not LUN) */
 266            dispri:1,     /* Disconnect Privilege granted */
 267               one:1;     /* 1 */
 268    unchar mess[3];       /* Massage to/from Target */
 269    unchar cdb[12];       /* Command Descriptor Block */
 270    ulong  data_len;      /* If sg=0 Data Length, if sg=1 sglist length */
 271    Scsi_Cmnd *SCpnt;     /* Address to be returned is sp */
 272    ulong  data_address;  /* If sg=0 Data Address, if sg=1 sglist address */
 273    ulong  sp_addr;       /* Address where sp is DMA'ed when cp completes */
 274    ulong  sense_addr;    /* Address where Sense Data is DMA'ed on error */
 275 
 276    struct sg_list {
 277       unsigned int address;     /* Segment Address */
 278       unsigned int num_bytes;   /* Segment Length */
 279       } sglist[MAX_SGLIST];
 280 
 281    unsigned int index;   /* cp index */
 282    };
 283 
 284 struct hostdata {
 285    struct mscp cp[MAX_MAILBOXES];       /* Mailboxes for this board */
 286    unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
 287    unsigned int last_cp_used;           /* Index of last mailbox used */
 288    unsigned int iocount;                /* Total i/o done for this board */
 289    unsigned int multicount;             /* Total ... in second ihdlr loop */
 290    int board_number;                    /* Number of this board */
 291    char board_name[16];                 /* Name of this board */
 292    char board_id[256];                  /* data from INQUIRY on this board */
 293    int in_reset;                        /* True if board is doing a reset */
 294    int target_time_out[MAX_TARGET];     /* N. of timeout errors on target */
 295    int target_reset[MAX_TARGET];        /* If TRUE redo operation on target */
 296    unsigned char subversion;            /* Bus type, either ISA or ESA */
 297    unsigned char protocol_rev;          /* EATA 2.0 rev., 'A' or 'B' or 'C' */
 298    struct mssp sp[MAX_MAILBOXES];       /* Returned status for this board */
 299    };
 300 
 301 static struct Scsi_Host * sh[MAX_BOARDS + 1];
 302 static char* driver_name = "EATA";
 303 static unsigned int irqlist[MAX_IRQ], calls[MAX_IRQ];
 304 
 305 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
 306 #define BN(board) (HD(board)->board_name)
 307 
 308 static void eata2x_interrupt_handler(int, struct pt_regs *);
 309 static int do_trace = FALSE;
 310 
 311 static inline unchar wait_on_busy(ushort iobase) {
     /* [previous][next][first][last][top][bottom][index][help] */
 312    unsigned int loop = MAXLOOP;
 313 
 314    while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED)
 315       if (--loop == 0) return TRUE;
 316 
 317    return FALSE;
 318 }
 319 
 320 static inline unchar do_dma (ushort iobase, unsigned int addr, unchar cmd) {
     /* [previous][next][first][last][top][bottom][index][help] */
 321 
 322    if (wait_on_busy(iobase)) return TRUE;
 323 
 324    if (addr) {
 325       outb((char)  addr,        iobase + REG_LOW);
 326       outb((char) (addr >> 8),  iobase + REG_LM);
 327       outb((char) (addr >> 16), iobase + REG_MID);
 328       outb((char) (addr >> 24), iobase + REG_MSB);
 329       }
 330 
 331    outb(cmd, iobase + REG_CMD);
 332    return FALSE;
 333 }
 334 
 335 static inline unchar read_pio (ushort iobase, ushort *start, ushort *end) {
     /* [previous][next][first][last][top][bottom][index][help] */
 336    unsigned int loop = MAXLOOP;
 337    ushort *p;
 338 
 339    for (p = start; p <= end; p++) {
 340 
 341       while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) 
 342          if (--loop == 0) return TRUE;
 343 
 344       loop = MAXLOOP;
 345       *p = inw(iobase);
 346       }
 347 
 348    return FALSE;
 349 }
 350 
 351 static inline int port_detect(ushort *port_base, unsigned int j, 
     /* [previous][next][first][last][top][bottom][index][help] */
 352                               Scsi_Host_Template * tpnt) {
 353    unsigned char irq, dma_channel, subversion;
 354    unsigned char protocol_rev;
 355    struct eata_info info;
 356    char *board_status;
 357 
 358    /* Allowed DMA channels for ISA (0 indicates reserved) */
 359    unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
 360 
 361    char name[16];
 362 
 363    sprintf(name, "%s%d", driver_name, j);
 364 
 365    if(check_region(*port_base, REGION_SIZE)) {
 366       printk("%s: address 0x%03x in use, skipping probe.\n", 
 367              name, *port_base);
 368       return FALSE;
 369       }
 370 
 371    if (do_dma(*port_base, 0, READ_CONFIG_PIO)) return FALSE;
 372 
 373    /* Read the info structure */
 374    if (read_pio(*port_base, (ushort *)&info, (ushort *)&info.ipad[0])) 
 375       return FALSE;
 376 
 377    /* Check the controller "EATA" signature */
 378    if (info.sign != EATA_SIGNATURE) return FALSE;
 379 
 380    if (ntohl(info.data_len) < EATA_2_0A_SIZE) {
 381       printk("%s: config structure size (%ld bytes) too short, detaching.\n", 
 382              name, ntohl(info.data_len));
 383       return FALSE;
 384       }
 385    else if (ntohl(info.data_len) == EATA_2_0A_SIZE)
 386       protocol_rev = 'A';
 387    else if (ntohl(info.data_len) == EATA_2_0B_SIZE)
 388       protocol_rev = 'B';
 389    else
 390       protocol_rev = 'C';
 391 
 392    if (protocol_rev != 'A' && info.max_chan > 0)
 393       printk("%s: warning, only scsi channel 0 is supported.\n", name);
 394 
 395    irq = info.irq;
 396 
 397    if (*port_base & EISA_RANGE) {
 398 
 399       if (!info.haaval || info.ata || info.drqvld) {
 400          printk("%s: unusable EISA board found (%d%d%d), detaching.\n", 
 401                 name, info.haaval, info.ata, info.drqvld);
 402          return FALSE;
 403          }
 404 
 405       subversion = ESA;
 406       dma_channel = NO_DMA;
 407       }
 408    else {
 409 
 410       if (!info.haaval || info.ata || !info.drqvld) {
 411          printk("%s: unusable ISA board found (%d%d%d), detaching.\n",
 412                 name, info.haaval, info.ata, info.drqvld);
 413          return FALSE;
 414          }
 415 
 416       subversion = ISA;
 417       dma_channel = dma_channel_table[3 - info.drqx];
 418       }
 419 
 420    if (!info.dmasup)
 421       printk("%s: warning, DMA protocol support not asserted.\n", name);
 422 
 423    if (subversion == ESA && !info.irq_tr)
 424       printk("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
 425              name, irq);
 426 
 427    if (info.second)
 428       board_status = "Sec.";
 429    else
 430       board_status = "Prim.";
 431 
 432    /* Board detected, allocate its IRQ if not already done */
 433    if ((irq >= MAX_IRQ) || ((irqlist[irq] == NO_IRQ) && request_irq
 434        (irq, eata2x_interrupt_handler, SA_INTERRUPT, driver_name))) {
 435       printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
 436       return FALSE;
 437       }
 438 
 439    if (subversion == ISA && request_dma(dma_channel, driver_name)) {
 440       printk("%s: unable to allocate DMA channel %u, detaching.\n",
 441              name, dma_channel);
 442       free_irq(irq);
 443       return FALSE;
 444       }
 445 
 446 #if defined (FORCE_CONFIG)
 447    {
 448    struct eata_config config;
 449 
 450    /* Set board configuration */
 451    memset((char *)&config, 0, sizeof(struct eata_config));
 452    config.len = (ushort) htons((ushort)510);
 453    config.ocena = TRUE;
 454 
 455    if (do_dma(*port_base, (unsigned int)&config, SET_CONFIG_DMA)) {
 456       printk("%s: busy timeout sending configuration, detaching.\n", name);
 457       return FALSE;
 458       }
 459    }
 460 #endif
 461 
 462    sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
 463    sh[j]->io_port = *port_base;
 464    sh[j]->n_io_port = REGION_SIZE;
 465    sh[j]->dma_channel = dma_channel;
 466    sh[j]->irq = irq;
 467    sh[j]->sg_tablesize = (ushort) ntohs(info.scatt_size);
 468    sh[j]->this_id = (ushort) info.host_addr[3];
 469    sh[j]->can_queue = (ushort) ntohs(info.queue_size);
 470    sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
 471 
 472    /* Register the I/O space that we use */
 473    request_region(sh[j]->io_port, REGION_SIZE, driver_name);
 474 
 475    memset(HD(j), 0, sizeof(struct hostdata));
 476    HD(j)->subversion = subversion;
 477    HD(j)->protocol_rev = protocol_rev;
 478    HD(j)->board_number = j;
 479    irqlist[irq] = j;
 480 
 481    if (HD(j)->subversion == ESA)
 482       sh[j]->unchecked_isa_dma = FALSE;
 483    else {
 484       sh[j]->wish_block = TRUE;
 485       sh[j]->unchecked_isa_dma = TRUE;
 486       disable_dma(dma_channel);
 487       clear_dma_ff(dma_channel);
 488       set_dma_mode(dma_channel, DMA_MODE_CASCADE);
 489       enable_dma(dma_channel);
 490       }
 491 
 492    strcpy(BN(j), name);
 493 
 494    printk("%s: 2.0%c, %s, ID %d, PORT 0x%03x, IRQ %u, DMA %u, SG %d, "\
 495           "Mbox %d, CmdLun %d.\n", BN(j), HD(j)->protocol_rev, board_status, 
 496            sh[j]->this_id, sh[j]->io_port, sh[j]->irq, sh[j]->dma_channel,
 497            sh[j]->sg_tablesize, sh[j]->can_queue, sh[j]->cmd_per_lun);
 498 
 499    /* DPT PM2012 does not allow to detect sg_tablesize correctly */
 500    if (sh[j]->sg_tablesize > MAX_SGLIST || sh[j]->sg_tablesize < 2) {
 501       printk("%s: detect, forcing to use %d SG lists.\n", BN(j), MAX_SGLIST);
 502       sh[j]->sg_tablesize = MAX_SGLIST;
 503       }
 504 
 505    /* DPT PM2012 does not allow to detect can_queue correctly */
 506    if (sh[j]->can_queue > MAX_MAILBOXES || sh[j]->can_queue  < 2) {
 507       printk("%s: detect, forcing to use %d Mbox.\n", BN(j), MAX_MAILBOXES);
 508       sh[j]->can_queue = MAX_MAILBOXES;
 509       }
 510 
 511 #if defined (DEBUG_DETECT)
 512    if (protocol_rev != 'A')
 513       printk("%s: EATA 2.0%c, isaena %u, forcaddr %u, max_id %u,"\
 514              " max_chan %u.\n", name, protocol_rev, info.isaena, 
 515              info.forcaddr, info.max_id, info.max_chan);
 516 
 517    printk("%s: Version 0x%x, SYNC 0x%x, infol %ld, cpl %ld spl %ld.\n", 
 518           name, info.version, info.sync, ntohl(info.data_len), 
 519           ntohl(info.cp_len), ntohl(info.sp_len));
 520 #endif
 521 
 522    return TRUE;
 523 }
 524 
 525 int eata2x_detect (Scsi_Host_Template * tpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 526    unsigned int j = 0, k, flags;
 527 
 528    ushort io_port[] = { 
 529       0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
 530       0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88, 
 531       0x1f0,  0x170,  0x330,  0x230,  0x0
 532       };
 533 
 534    ushort *port_base = io_port;
 535 
 536    save_flags(flags);
 537    cli();
 538 
 539    for (k = 0; k < MAX_IRQ; k++) {
 540       irqlist[k] = NO_IRQ;
 541       calls[k] = 0;
 542       }
 543 
 544    for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
 545 
 546    while (*port_base) {
 547 
 548       if (j < MAX_BOARDS && port_detect(port_base, j, tpnt)) j++;
 549 
 550       port_base++;
 551       }
 552 
 553    if (j > 0) 
 554       printk("EATA/DMA 2.0x: Copyright (C) 1994, 1995 Dario Ballabio.\n");
 555 
 556    restore_flags(flags);
 557    return j;
 558 }
 559 
 560 static inline void build_sg_list(struct mscp *cpp, Scsi_Cmnd *SCpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 561    unsigned int k;
 562    struct scatterlist * sgpnt;
 563 
 564    sgpnt = (struct scatterlist *) SCpnt->request_buffer;
 565 
 566    for (k = 0; k < SCpnt->use_sg; k++) {
 567       cpp->sglist[k].address = htonl((unsigned int) sgpnt[k].address);
 568       cpp->sglist[k].num_bytes = htonl((unsigned int) sgpnt[k].length);
 569       }
 570 
 571    cpp->data_address = htonl((unsigned int) cpp->sglist);
 572    cpp->data_len = htonl((SCpnt->use_sg * sizeof(struct sg_list)));
 573 }
 574 
 575 int eata2x_queuecommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
     /* [previous][next][first][last][top][bottom][index][help] */
 576    unsigned int i, j, k, flags;
 577    struct mscp *cpp;
 578    struct mssp *spp;
 579 
 580    static const unsigned char data_out_cmds[] = {
 581       0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x0b, 0x10, 0x16, 0x18, 0x1d, 
 582       0x24, 0x2b, 0x2e, 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 
 583       0x3f, 0x40, 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea
 584       };
 585 
 586    save_flags(flags);
 587    cli();
 588    /* j is the board number */
 589    j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
 590 
 591    if (!done) panic("%s: qcomm, pid %ld, null done.\n", BN(j), SCpnt->pid);
 592 
 593    /* i is the mailbox number, look for the first free mailbox 
 594       starting from last_cp_used */
 595    i = HD(j)->last_cp_used + 1;
 596 
 597    for (k = 0; k < sh[j]->can_queue; k++, i++) {
 598 
 599       if (i >= sh[j]->can_queue) i = 0;
 600 
 601       if (HD(j)->cp_stat[i] == FREE) {
 602          HD(j)->last_cp_used = i;
 603          break;
 604          }
 605       }
 606 
 607    if (k == sh[j]->can_queue) {
 608       printk("%s: qcomm, no free mailbox, resetting.\n", BN(j));
 609 
 610       if (HD(j)->in_reset) 
 611          printk("%s: qcomm, already in reset.\n", BN(j));
 612       else if (eata2x_reset(SCpnt) == SCSI_RESET_SUCCESS) 
 613          panic("%s: qcomm, SCSI_RESET_SUCCESS.\n", BN(j));
 614 
 615       SCpnt->result = DID_BUS_BUSY << 16; 
 616       SCpnt->host_scribble = NULL;
 617       printk("%s: qcomm, pid %ld, DID_BUS_BUSY, done.\n", BN(j), SCpnt->pid);
 618       restore_flags(flags);
 619       done(SCpnt);    
 620       return 0;
 621       }
 622 
 623    /* Set pointer to control packet structure */
 624    cpp = &HD(j)->cp[i];
 625 
 626    memset(cpp, 0, sizeof(struct mscp));
 627 
 628    /* Set pointer to status packet structure */
 629    spp = &HD(j)->sp[i];
 630 
 631    memset(spp, 0, sizeof(struct mssp));
 632 
 633    /* The EATA protocol uses Big Endian format, while Intel is Little Endian */
 634    cpp->sp_addr = htonl((unsigned int) spp);
 635 
 636    SCpnt->scsi_done = done;
 637    cpp->index = i;
 638    SCpnt->host_scribble = (unsigned char *) &cpp->index;
 639 
 640    if (do_trace) printk("%s: qcomm, mbox %d, target %d, pid %ld.\n",
 641                         BN(j), i, SCpnt->target, SCpnt->pid);
 642 
 643    for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
 644      if (SCpnt->cmnd[0] == data_out_cmds[k]) {
 645         cpp->dout = TRUE;
 646         break;
 647         }
 648 
 649    cpp->din = !cpp->dout;
 650    cpp->reqsen = TRUE;
 651    cpp->dispri = TRUE;
 652    cpp->one = TRUE;
 653    cpp->target = SCpnt->target;
 654    cpp->lun = SCpnt->lun;  
 655    cpp->SCpnt = SCpnt;
 656    cpp->sense_addr = htonl((unsigned int) SCpnt->sense_buffer); 
 657    cpp->sense_len = sizeof SCpnt->sense_buffer;
 658 
 659    if (SCpnt->use_sg) {
 660       cpp->sg = TRUE;
 661       build_sg_list(cpp, SCpnt);
 662       }
 663    else {
 664       cpp->data_address = htonl((unsigned int) SCpnt->request_buffer);
 665       cpp->data_len = htonl(SCpnt->request_bufflen);
 666       }
 667 
 668    memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
 669 
 670    /* Send control packet to the board */
 671    if (do_dma(sh[j]->io_port, (unsigned int) cpp, SEND_CP_DMA)) {
 672       SCpnt->result = DID_ERROR << 16; 
 673       SCpnt->host_scribble = NULL;
 674       printk("%s: qcomm, target %d, pid %ld, adapter busy, DID_ERROR, done.\n", 
 675              BN(j), SCpnt->target, SCpnt->pid);
 676       restore_flags(flags);
 677       done(SCpnt);    
 678       return 0;
 679       }
 680 
 681    HD(j)->cp_stat[i] = IN_USE;
 682    restore_flags(flags);
 683    return 0;
 684 }
 685 
 686 int eata2x_abort (Scsi_Cmnd *SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 687    unsigned int i, j, flags;
 688 
 689    save_flags(flags);
 690    cli();
 691    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 692 
 693    if (SCarg->host_scribble == NULL) {
 694       printk("%s: abort, target %d, pid %ld inactive.\n",
 695              BN(j), SCarg->target, SCarg->pid);
 696       return SCSI_ABORT_NOT_RUNNING;
 697       }
 698 
 699    i = *(unsigned int *)SCarg->host_scribble;
 700    printk("%s: abort, mbox %d, target %d, pid %ld.\n", 
 701           BN(j), i, SCarg->target, SCarg->pid);
 702 
 703    if (i >= sh[j]->can_queue)
 704       panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
 705 
 706    if (wait_on_busy(sh[j]->io_port)) {
 707       printk("%s: abort, timeout error.\n", BN(j));
 708       restore_flags(flags);
 709       return SCSI_ABORT_ERROR;
 710       }
 711 
 712    if (HD(j)->cp_stat[i] == FREE) {
 713       printk("%s: abort, mbox %d is free.\n", BN(j), i);
 714       restore_flags(flags);
 715       return SCSI_ABORT_NOT_RUNNING;
 716       }
 717 
 718    if (HD(j)->cp_stat[i] == IN_USE) {
 719       printk("%s: abort, mbox %d is in use.\n", BN(j), i);
 720 
 721       if (SCarg != HD(j)->cp[i].SCpnt)
 722          panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
 723                BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
 724 
 725       restore_flags(flags);
 726       return SCSI_ABORT_SNOOZE;
 727       }
 728 
 729    if (HD(j)->cp_stat[i] == IN_RESET) {
 730       printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
 731       restore_flags(flags);
 732       return SCSI_ABORT_ERROR;
 733       }
 734 
 735    if (HD(j)->cp_stat[i] == LOCKED) {
 736       printk("%s: abort, mbox %d is locked.\n", BN(j), i);
 737       restore_flags(flags);
 738       return SCSI_ABORT_NOT_RUNNING;
 739       }
 740    else
 741       panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
 742 }
 743 
 744 int eata2x_reset (Scsi_Cmnd *SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 745    unsigned int i, j, flags, time, k, limit = 0;
 746    int arg_done = FALSE;
 747    Scsi_Cmnd *SCpnt;
 748 
 749    save_flags(flags);
 750    cli();
 751    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 752    printk("%s: reset, enter, target %d, pid %ld.\n", 
 753           BN(j), SCarg->target, SCarg->pid);
 754 
 755    if (SCarg->host_scribble == NULL)
 756       printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
 757 
 758    if (HD(j)->in_reset) {
 759       printk("%s: reset, exit, already in reset.\n", BN(j));
 760       restore_flags(flags);
 761       return SCSI_RESET_ERROR;
 762       }
 763 
 764    if (wait_on_busy(sh[j]->io_port)) {
 765       printk("%s: reset, exit, timeout error.\n", BN(j));
 766       restore_flags(flags);
 767       return SCSI_RESET_ERROR;
 768       }
 769 
 770    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_reset[k] = TRUE;
 771 
 772    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_time_out[k] = 0;
 773 
 774    for (i = 0; i < sh[j]->can_queue; i++) {
 775 
 776       if (HD(j)->cp_stat[i] == FREE) continue;
 777 
 778       if (HD(j)->cp_stat[i] == LOCKED) {
 779          HD(j)->cp_stat[i] = FREE;
 780          printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
 781          continue;
 782          }
 783 
 784       SCpnt = HD(j)->cp[i].SCpnt;
 785       HD(j)->cp_stat[i] = IN_RESET;
 786       printk("%s: reset, mbox %d in reset, pid %ld.\n",
 787              BN(j), i, SCpnt->pid);
 788 
 789       if (SCpnt == NULL)
 790          panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
 791 
 792       if (SCpnt->host_scribble == NULL)
 793          panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
 794 
 795       if (*(unsigned int *)SCpnt->host_scribble != i) 
 796          panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
 797 
 798       if (SCpnt->scsi_done == NULL) 
 799          panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
 800 
 801       if (SCpnt == SCarg) arg_done = TRUE;
 802       }
 803 
 804    if (do_dma(sh[j]->io_port, 0, RESET_PIO)) {
 805       printk("%s: reset, cannot reset, timeout error.\n", BN(j));
 806       restore_flags(flags);
 807       return SCSI_RESET_ERROR;
 808       }
 809 
 810    printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
 811 
 812 #if defined (DEBUG_RESET)
 813    do_trace = TRUE;
 814 #endif
 815 
 816    HD(j)->in_reset = TRUE;
 817    sti();
 818    time = jiffies;
 819    while (jiffies < (time + 100) && limit++ < 100000000);
 820    cli();
 821    printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
 822 
 823    for (i = 0; i < sh[j]->can_queue; i++) {
 824 
 825       /* Skip mailboxes already set free by interrupt */
 826       if (HD(j)->cp_stat[i] != IN_RESET) continue;
 827 
 828       SCpnt = HD(j)->cp[i].SCpnt;
 829       SCpnt->result = DID_RESET << 16;
 830       SCpnt->host_scribble = NULL;
 831 
 832       /* This mailbox is still waiting for its interrupt */
 833       HD(j)->cp_stat[i] = LOCKED;
 834 
 835       printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
 836              BN(j), i, SCpnt->pid);
 837       restore_flags(flags);
 838       SCpnt->scsi_done(SCpnt);
 839       cli();
 840       }
 841 
 842    HD(j)->in_reset = FALSE;
 843    do_trace = FALSE;
 844    restore_flags(flags);
 845 
 846    if (arg_done) {
 847       printk("%s: reset, exit, success.\n", BN(j));
 848       return SCSI_RESET_SUCCESS;
 849       }
 850    else {
 851       printk("%s: reset, exit, wakeup.\n", BN(j));
 852       return SCSI_RESET_PUNT;
 853       }
 854 }
 855 
 856 static void eata2x_interrupt_handler(int irq, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
 857    Scsi_Cmnd *SCpnt;
 858    unsigned int i, j, k, flags, status, tstatus, loops, total_loops = 0;
 859    struct mssp *spp;
 860    struct mscp *cpp;
 861 
 862    save_flags(flags);
 863    cli();
 864 
 865    if (irqlist[irq] == NO_IRQ) {
 866       printk("%s, ihdlr, irq %d, unexpected interrupt.\n", driver_name, irq);
 867       restore_flags(flags);
 868       return;
 869       }
 870 
 871    if (do_trace) printk("%s: ihdlr, enter, irq %d, calls %d.\n", 
 872                         driver_name, irq, calls[irq]);
 873 
 874    /* Service all the boards configured on this irq */
 875    for (j = 0; sh[j] != NULL; j++) {
 876 
 877       if (sh[j]->irq != irq) continue;
 878 
 879       loops = 0;
 880 
 881       /* Loop until all interrupts for a board are serviced */
 882       while (inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED) {
 883          total_loops++;
 884          loops++;
 885 
 886          if (do_trace) printk("%s: ihdlr, start service, count %d.\n",
 887                               BN(j), HD(j)->iocount);
 888    
 889          /* Read the status register to clear the interrupt indication */
 890          inb(sh[j]->io_port + REG_STATUS);
 891    
 892          /* Service all mailboxes of this board */
 893          for (i = 0; i < sh[j]->can_queue; i++) {
 894             spp = &HD(j)->sp[i];
 895    
 896             /* Check if this mailbox has completed the operation */
 897             if (spp->eoc == FALSE) continue;
 898    
 899             spp->eoc = FALSE;
 900    
 901             if (HD(j)->cp_stat[i] == IGNORE) {
 902                HD(j)->cp_stat[i] = FREE;
 903                continue;
 904                }
 905             else if (HD(j)->cp_stat[i] == LOCKED) {
 906                HD(j)->cp_stat[i] = FREE;
 907                printk("%s: ihdlr, mbox %d unlocked, count %d.\n",
 908                       BN(j), i, HD(j)->iocount);
 909                continue;
 910                }
 911             else if (HD(j)->cp_stat[i] == FREE) {
 912                printk("%s: ihdlr, mbox %d is free, count %d.\n", 
 913                       BN(j), i, HD(j)->iocount);
 914                continue;
 915                }
 916             else if (HD(j)->cp_stat[i] == IN_RESET)
 917                printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
 918             else if (HD(j)->cp_stat[i] != IN_USE) 
 919                panic("%s: ihdlr, mbox %d, invalid cp_stat.\n", BN(j), i);
 920    
 921             HD(j)->cp_stat[i] = FREE;
 922             cpp = &HD(j)->cp[i];
 923             SCpnt = spp->SCpnt;
 924    
 925             if (SCpnt == NULL)
 926                panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
 927    
 928             if (SCpnt != cpp->SCpnt)
 929                panic("%s: ihdlr, mbox %d, sp SCpnt %p, cp SCpnt %p.\n",
 930                      BN(j), i, SCpnt, cpp->SCpnt);
 931    
 932             if (SCpnt->host_scribble == NULL)
 933                panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n",
 934                      BN(j), i, SCpnt->pid, SCpnt);
 935    
 936             if (*(unsigned int *)SCpnt->host_scribble != i) 
 937                panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d,"\
 938                      " irq %d.\n", BN(j), i, SCpnt->pid, 
 939                      *(unsigned int *)SCpnt->host_scribble, irq);
 940    
 941             tstatus = status_byte(spp->target_status);
 942    
 943             switch (spp->adapter_status) {
 944                case ASOK:     /* status OK */
 945    
 946                   /* Forces a reset if a disk drive keeps returning BUSY */
 947                   if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE) 
 948                      status = DID_ERROR << 16;
 949    
 950                   /* If there was a bus reset, redo operation on each target */
 951                   else if (tstatus != GOOD
 952                            && SCpnt->device->type == TYPE_DISK
 953                            && HD(j)->target_reset[SCpnt->target])
 954                      status = DID_BUS_BUSY << 16;
 955    
 956                   /* Works around a flaw in scsi.c */
 957                   else if (tstatus == CHECK_CONDITION
 958                            && SCpnt->device->type == TYPE_DISK
 959                            && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
 960                      status = DID_BUS_BUSY << 16;
 961 
 962                   else if (tstatus == CHECK_CONDITION
 963                            && SCpnt->device->type == TYPE_DISK
 964                            && (SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION)
 965                      status = DID_ERROR << 16;
 966    
 967                   else
 968                      status = DID_OK << 16;
 969    
 970                   if (tstatus == GOOD)
 971                      HD(j)->target_reset[SCpnt->target] = FALSE;
 972    
 973                   if (spp->target_status && SCpnt->device->type == TYPE_DISK)
 974                      printk("%s: ihdlr, target %d:%d, pid %ld, target_status "\
 975                             "0x%x, sense key 0x%x.\n", BN(j), 
 976                             SCpnt->target, SCpnt->lun, SCpnt->pid,
 977                             spp->target_status, SCpnt->sense_buffer[2]);
 978    
 979                   HD(j)->target_time_out[SCpnt->target] = 0;
 980    
 981                   break;
 982                case ASST:     /* Selection Time Out */
 983                case 0x02:     /* Command Time Out   */
 984    
 985                   if (HD(j)->target_time_out[SCpnt->target] > 1)
 986                      status = DID_ERROR << 16;
 987                   else {
 988                      status = DID_TIME_OUT << 16;
 989                      HD(j)->target_time_out[SCpnt->target]++;
 990                      }
 991    
 992                   break;
 993                case 0x03:     /* SCSI Bus Reset Received */
 994                case 0x04:     /* Initial Controller Power-up */
 995    
 996                   if (SCpnt->device->type != TYPE_TAPE)
 997                      status = DID_BUS_BUSY << 16;
 998                   else
 999                      status = DID_ERROR << 16;
1000    
1001                   for (k = 0; k < MAX_TARGET; k++) 
1002                      HD(j)->target_reset[k] = TRUE;
1003    
1004                   break;
1005                case 0x07:     /* Bus Parity Error */
1006                case 0x0c:     /* Controller Ram Parity */
1007                case 0x05:     /* Unexpected Bus Phase */
1008                case 0x06:     /* Unexpected Bus Free */
1009                case 0x08:     /* SCSI Hung */
1010                case 0x09:     /* Unexpected Message Reject */
1011                case 0x0a:     /* SCSI Bus Reset Stuck */
1012                case 0x0b:     /* Auto Request-Sense Failed */
1013                default:
1014                   status = DID_ERROR << 16;
1015                   break;
1016                }
1017    
1018             SCpnt->result = status | spp->target_status;
1019             HD(j)->iocount++;
1020 
1021             if (loops > 1) HD(j)->multicount++;
1022 
1023 #if defined (DEBUG_INTERRUPT)
1024             if (SCpnt->result || do_trace)
1025 #else
1026             if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
1027                 (spp->adapter_status != ASOK && 
1028                  spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
1029                 do_trace)
1030 #endif
1031                printk("%s: ihdlr, mbox %d, err 0x%x:%x,"\
1032                       " target %d:%d, pid %ld, count %d.\n",
1033                       BN(j), i, spp->adapter_status, spp->target_status,
1034                       SCpnt->target, SCpnt->lun, SCpnt->pid, HD(j)->iocount);
1035    
1036             /* Set the command state to inactive */
1037             SCpnt->host_scribble = NULL;
1038    
1039             restore_flags(flags);
1040             SCpnt->scsi_done(SCpnt);
1041             cli();
1042 
1043             }   /* Mailbox loop */
1044 
1045          }   /* Multiple command loop */
1046 
1047       }   /* Boards loop */
1048 
1049    calls[irq]++;
1050 
1051    if (total_loops == 0) 
1052      printk("%s: ihdlr, irq %d, no command completed, calls %d.\n",
1053             driver_name, irq, calls[irq]);
1054 
1055    if (do_trace) printk("%s: ihdlr, exit, irq %d, calls %d.\n", 
1056                         driver_name, irq, calls[irq]);
1057 
1058 #if defined (DEBUG_STATISTICS)
1059    if ((calls[irq] % 100000) == 10000)
1060       for (j = 0; sh[j] != NULL; j++)
1061          printk("%s: ihdlr, calls %d, count %d, multi %d.\n", BN(j),
1062                 calls[(sh[j]->irq)], HD(j)->iocount, HD(j)->multicount);
1063 #endif
1064 
1065    restore_flags(flags);
1066    return;
1067 }
1068 
1069 #if defined(MODULE)
1070 Scsi_Host_Template driver_template = EATA;
1071 
1072 #include "scsi_module.c"
1073 #endif

/* [previous][next][first][last][top][bottom][index][help] */