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

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