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

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