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

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