root/drivers/scsi/eata_dma.c

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

DEFINITIONS

This source file includes following definitions.
  1. eata_scsi_done
  2. eata_fake_int_handler
  3. eata_release
  4. eata_latency_in
  5. eata_latency_out
  6. eata_int_handler
  7. eata_send_command
  8. eata_send_immediate
  9. eata_queue
  10. eata_abort
  11. eata_reset
  12. eata_select_queue_depths
  13. check_blink_state
  14. get_board_data
  15. get_conf_PIO
  16. print_config
  17. register_HBA
  18. find_EISA
  19. find_ISA
  20. find_PCI
  21. eata_detect

   1 /************************************************************
   2  *                                                          *
   3  *                  Linux EATA SCSI driver                  *
   4  *                                                          *
   5  *  based on the CAM document CAM/89-004 rev. 2.0c,         *
   6  *  DPT's driver kit, some internal documents and source,   *
   7  *  and several other Linux scsi drivers and kernel docs.   *
   8  *                                                          *
   9  *  The driver currently:                                   *
  10  *      -supports all ISA based EATA-DMA boards             *
  11  *       like PM2011, PM2021, PM2041, PM3021                *
  12  *      -supports all EISA based EATA-DMA boards            *
  13  *       like PM2012B, PM2022, PM2122, PM2322, PM2042,      *
  14  *            PM3122, PM3222, PM3332                        *
  15  *      -supports all PCI based EATA-DMA boards             *
  16  *       like PM2024, PM2124, PM2044, PM2144, PM3224,       *
  17  *            PM3334                                        *
  18  *      -supports the Wide, Ultra Wide and Differential     *
  19  *       versions of the boards                             *
  20  *      -supports multiple HBAs with & without IRQ sharing  *
  21  *      -supports all SCSI channels on multi channel boards *
  22  *      -needs identical IDs on all channels of a HBA       * 
  23  *      -can be loaded as module                            *
  24  *      -displays statistical and hardware information      *
  25  *       in /proc/scsi/eata_dma                             *
  26  *      -provides rudimentary latency measurement           * 
  27  *       possibilities via /proc/scsi/eata_dma/<hostnum>    *
  28  *                                                          *
  29  *  (c)1993-96 Michael Neuffer                              *
  30  *             mike@i-Connect.Net                           *
  31  *             neuffer@mail.uni-mainz.de                    *
  32  *                                                          *
  33  *  This program is free software; you can redistribute it  *
  34  *  and/or modify it under the terms of the GNU General     *
  35  *  Public License as published by the Free Software        *
  36  *  Foundation; either version 2 of the License, or         *
  37  *  (at your option) any later version.                     *
  38  *                                                          *
  39  *  This program is distributed in the hope that it will be *
  40  *  useful, but WITHOUT ANY WARRANTY; without even the      *
  41  *  implied warranty of MERCHANTABILITY or FITNESS FOR A    *
  42  *  PARTICULAR PURPOSE.  See the GNU General Public License *
  43  *  for more details.                                       *
  44  *                                                          *
  45  *  You should have received a copy of the GNU General      *
  46  *  Public License along with this kernel; if not, write to *
  47  *  the Free Software Foundation, Inc., 675 Mass Ave,       *
  48  *  Cambridge, MA 02139, USA.                               *
  49  *                                                          *
  50  * I have to thank DPT for their excellent support. I took  *
  51  * me almost a year and a stopover at their HQ, on my first *
  52  * trip to the USA, to get it, but since then they've been  *
  53  * very helpful and tried to give me all the infos and      *
  54  * support I need.                                          *
  55  *                                                          *
  56  * Thanks also to Simon Shapiro, Greg Hosler and Mike       *
  57  * Jagdis who did a lot of testing and found quite a number *
  58  * of bugs during the development.                          *
  59  ************************************************************
  60  *  last change: 95/05/05                 OS: Linux 1.3.98  *
  61  ************************************************************/
  62 
  63 /* Look in eata_dma.h for configuration and revision information */
  64 
  65 #include <linux/module.h>
  66 #include <linux/kernel.h>
  67 #include <linux/sched.h>
  68 #include <linux/string.h>
  69 #include <linux/ioport.h>
  70 #include <linux/malloc.h>
  71 #include <linux/in.h>
  72 #include <linux/bios32.h>
  73 #include <linux/pci.h>
  74 #include <linux/proc_fs.h>
  75 #include <linux/delay.h>
  76 #include <asm/byteorder.h>
  77 #include <asm/types.h>
  78 #include <asm/io.h>
  79 #include <asm/dma.h>
  80 #include <linux/blk.h>
  81 #include "scsi.h"
  82 #include "sd.h"
  83 #include "hosts.h"
  84 #include "eata_dma.h"
  85 #include "eata_dma_proc.h" 
  86 
  87 #include <linux/stat.h>
  88 #include <linux/config.h>       /* for CONFIG_PCI */
  89 
  90 struct proc_dir_entry proc_scsi_eata_dma = {
  91     PROC_SCSI_EATA, 8, "eata_dma",
  92     S_IFDIR | S_IRUGO | S_IXUGO, 2
  93 };
  94 
  95 static u32 ISAbases[] =
  96 {0x1F0, 0x170, 0x330, 0x230};
  97 static unchar EISAbases[] =
  98 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  99 static uint registered_HBAs = 0;
 100 static struct Scsi_Host *last_HBA = NULL;
 101 static struct Scsi_Host *first_HBA = NULL;
 102 static unchar reg_IRQ[] =
 103 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 104 static unchar reg_IRQL[] =
 105 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 106 static struct eata_sp *status = 0;   /* Statuspacket array   */
 107 static void *dma_scratch = 0;
 108 
 109 static struct eata_register *fake_int_base;
 110 static int fake_int_result;
 111 static int fake_int_happened;
 112 
 113 static ulong int_counter = 0;
 114 static ulong queue_counter = 0;
 115 
 116 void eata_scsi_done (Scsi_Cmnd * scmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118     scmd->request.rq_status = RQ_SCSI_DONE;
 119 
 120     if (scmd->request.sem != NULL)
 121         up(scmd->request.sem);
 122     
 123     return;
 124 }   
 125 
 126 void eata_fake_int_handler(s32 irq, void *dev_id, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128     fake_int_result = inb((ulong)fake_int_base + HA_RSTATUS);
 129     fake_int_happened = TRUE;
 130     DBG(DBG_INTR3, printk("eata_fake_int_handler called irq%d base %p"
 131                           " res %#x\n", irq, fake_int_base, fake_int_result));
 132     return;
 133 }
 134 
 135 #include "eata_dma_proc.c"
 136 
 137 #ifdef MODULE
 138 int eata_release(struct Scsi_Host *sh)
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140     uint i;
 141     if (sh->irq && reg_IRQ[sh->irq] == 1) free_irq(sh->irq, NULL);
 142     else reg_IRQ[sh->irq]--;
 143     
 144     scsi_init_free((void *)status, 512);
 145     scsi_init_free((void *)dma_scratch - 4, 1024);
 146     for (i = 0; i < sh->can_queue; i++){ /* Free all SG arrays */
 147         if(SD(sh)->ccb[i].sg_list != NULL)
 148             scsi_init_free((void *) SD(sh)->ccb[i].sg_list, 
 149                            sh->sg_tablesize * sizeof(struct eata_sg_list));
 150     }
 151     
 152     if (SD(sh)->channel == 0) {
 153         if (sh->dma_channel != BUSMASTER) free_dma(sh->dma_channel);
 154         if (sh->io_port && sh->n_io_port)
 155             release_region(sh->io_port, sh->n_io_port);
 156     }
 157     return(TRUE);
 158 }
 159 #endif
 160 
 161 
 162 inline void eata_latency_in(struct eata_ccb *cp, hostdata *hd)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164     uint time;
 165     time = jiffies - cp->timestamp;
 166     if(hd->all_lat[1] > time)
 167         hd->all_lat[1] = time;
 168     if(hd->all_lat[2] < time)
 169         hd->all_lat[2] = time;
 170     hd->all_lat[3] += time;
 171     hd->all_lat[0]++;
 172     if((cp->rw_latency) == WRITE) { /* was WRITE */
 173         if(hd->writes_lat[cp->sizeindex][1] > time)
 174             hd->writes_lat[cp->sizeindex][1] = time;
 175         if(hd->writes_lat[cp->sizeindex][2] < time)
 176             hd->writes_lat[cp->sizeindex][2] = time;
 177         hd->writes_lat[cp->sizeindex][3] += time;
 178         hd->writes_lat[cp->sizeindex][0]++;
 179     } else if((cp->rw_latency) == READ) {
 180         if(hd->reads_lat[cp->sizeindex][1] > time)
 181             hd->reads_lat[cp->sizeindex][1] = time;
 182         if(hd->reads_lat[cp->sizeindex][2] < time)
 183             hd->reads_lat[cp->sizeindex][2] = time;
 184         hd->reads_lat[cp->sizeindex][3] += time;
 185         hd->reads_lat[cp->sizeindex][0]++;
 186     }
 187 } 
 188 
 189 inline void eata_latency_out(struct eata_ccb *cp, Scsi_Cmnd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191     int x, z;
 192     short *sho;
 193     long *lon;
 194     x = 0;                              /* just to keep GCC quiet */ 
 195     cp->timestamp = jiffies;            /* For latency measurements */
 196     switch(cmd->cmnd[0]) {
 197     case WRITE_6:   
 198         x = cmd->cmnd[4]/2; 
 199         cp->rw_latency = WRITE;
 200         break;
 201     case READ_6:    
 202         x = cmd->cmnd[4]/2; 
 203         cp->rw_latency = READ;
 204         break;
 205     case WRITE_10:   
 206         sho = (short *) &cmd->cmnd[7];
 207         x = ntohs(*sho)/2;            
 208         cp->rw_latency = WRITE;
 209         break;
 210     case READ_10:
 211         sho = (short *) &cmd->cmnd[7];
 212         x = ntohs(*sho)/2;            
 213         cp->rw_latency = READ;
 214         break;
 215     case WRITE_12:   
 216         lon = (long *) &cmd->cmnd[6];
 217         x = ntohl(*lon)/2;            
 218         cp->rw_latency = WRITE;
 219         break;
 220     case READ_12:
 221         lon = (long *) &cmd->cmnd[6];
 222         x = ntohl(*lon)/2;            
 223         cp->rw_latency = READ;
 224         break;
 225     default:
 226         cp->rw_latency = OTHER;
 227         break;
 228     }
 229     if (cmd->cmnd[0] == WRITE_6 || cmd->cmnd[0] == WRITE_10 || 
 230         cmd->cmnd[0] == WRITE_12 || cmd->cmnd[0] == READ_6 || 
 231         cmd->cmnd[0] == READ_10 || cmd->cmnd[0] == READ_12) {
 232         for(z = 0; (x > (1 << z)) && (z <= 11); z++) 
 233             /* nothing */;
 234         cp->sizeindex = z;
 235     } 
 236 }
 237 
 238 
 239 void eata_int_handler(int irq, void *dev_id, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241     uint i, result = 0;
 242     uint hba_stat, scsi_stat, eata_stat;
 243     Scsi_Cmnd *cmd;
 244     struct eata_ccb *ccb;
 245     struct eata_sp *sp;
 246     uint base;
 247     ulong flags;
 248     uint x;
 249     struct Scsi_Host *sh;
 250 
 251     save_flags(flags);
 252     cli();
 253 
 254     for (x = 1, sh = first_HBA; x <= registered_HBAs; x++, sh = SD(sh)->next) {
 255         if (sh->irq != irq)
 256             continue;
 257         
 258         while(inb((uint)sh->base + HA_RAUXSTAT) & HA_AIRQ) {
 259             
 260             int_counter++;
 261             
 262             sp = &SD(sh)->sp;
 263             ccb = sp->ccb;
 264             
 265             if(ccb == NULL) {
 266                 eata_stat = inb((uint)sh->base + HA_RSTATUS);
 267                 printk("eata_dma: int_handler, Spurious IRQ %d "
 268                        "received. CCB pointer not set.\n", irq);
 269                 break;
 270             }
 271 
 272             cmd = ccb->cmd;
 273             base = (uint) cmd->host->base;
 274             hba_stat = sp->hba_stat;
 275             
 276             scsi_stat = (sp->scsi_stat >> 1) & 0x1f; 
 277             
 278             if (sp->EOC == FALSE) {
 279                 eata_stat = inb(base + HA_RSTATUS);
 280                 printk(KERN_WARNING "eata_dma: int_handler, board: %x cmd %lx "
 281                        "returned unfinished.\n"
 282                        "EATA: %x HBA: %x SCSI: %x spadr %lx spadrirq %lx, "
 283                        "irq%d\n", base, (long)ccb, eata_stat, hba_stat, 
 284                        scsi_stat,(long)&status, (long)&status[irq], irq);
 285                 cmd->result = DID_ERROR << 16;
 286                 ccb->status = FREE;
 287                 cmd->scsi_done(cmd);
 288                 break;
 289             } 
 290             
 291             if (ccb->status == LOCKED) {
 292                 ccb->status = FREE;
 293                 eata_stat = inb(base + HA_RSTATUS);
 294                 printk("eata_dma: int_handler, freeing locked queueslot\n");
 295                 DBG(DBG_INTR && DBG_DELAY, DELAY(1));
 296                 break;
 297             }
 298             
 299             sp->EOC = FALSE; /* Clean out this flag */
 300 
 301             eata_stat = inb(base + HA_RSTATUS); 
 302             DBG(DBG_INTR, printk("IRQ %d received, base %#.4x, pid %ld, "
 303                                  "target: %x, lun: %x, ea_s: %#.2x, hba_s: "
 304                                  "%#.2x \n", irq, base, cmd->pid, cmd->target,
 305                                  cmd->lun, eata_stat, hba_stat));
 306             
 307             switch (hba_stat) {
 308             case HA_NO_ERROR:   /* NO Error */
 309                 HD(cmd)->t_state[ccb->cp_channel][ccb->cp_id] = OK;
 310                 if(HD(cmd)->do_latency == TRUE && ccb->timestamp) 
 311                     eata_latency_in(ccb, HD(cmd));
 312                 result = DID_OK << 16;
 313                 HD(cmd)->t_timeout[ccb->cp_channel][ccb->cp_id] = OK;
 314                 break;
 315             case HA_ERR_SEL_TO:         /* Selection Timeout */
 316             case HA_ERR_CMD_TO:         /* Command Timeout   */
 317                 result = DID_TIME_OUT << 16;
 318                 break;
 319             case HA_BUS_RESET:          /* SCSI Bus Reset Received */
 320                 result = DID_RESET << 16;
 321                 DBG(DBG_STATUS, printk(KERN_WARNING "scsi%d: BUS RESET "
 322                                        "received on cmd %ld\n", 
 323                                        HD(cmd)->HBA_number, cmd->pid));
 324                 break;
 325             case HA_INIT_POWERUP:       /* Initial Controller Power-up */
 326                 if (cmd->device->type != TYPE_TAPE)
 327                     result = DID_BUS_BUSY << 16;
 328                 else
 329                     result = DID_ERROR << 16;
 330                 
 331                 for (i = 0; i < MAXTARGET; i++)
 332                     HD(cmd)->t_state[ccb->cp_channel][i] = RESET;
 333                 DBG(DBG_STATUS, printk(KERN_DEBUG "scsi%d: cmd pid %ld "
 334                                        "returned with INIT_POWERUP\n", 
 335                                        HD(cmd)->HBA_number, cmd->pid));
 336                 break;
 337             case HA_CP_ABORT_NA:
 338             case HA_CP_ABORTED:
 339                 result = DID_ABORT << 16;
 340                 DBG(DBG_STATUS, printk(KERN_WARNING "scsi%d: aborted cmd "
 341                                        "returned\n", HD(cmd)->HBA_number));
 342                 break;
 343             case HA_CP_RESET_NA:
 344             case HA_CP_RESET:
 345                 HD(cmd)->t_state[cmd->channel][cmd->target] = OK; 
 346                 HD(cmd)->resetlevel[cmd->channel] = 0; 
 347                 result = DID_RESET << 16;
 348                 DBG(DBG_STATUS, printk(KERN_WARNING "scsi%d: reseted cmd "
 349                                        "pid %ldreturned\n", 
 350                                        HD(cmd)->HBA_number, cmd->pid));
 351             case HA_SCSI_HUNG:          /* SCSI Hung                 */
 352                 printk(KERN_ERR "scsi%d: SCSI hung\n", HD(cmd)->HBA_number);
 353                 result = DID_ERROR << 16;
 354                 break;
 355             case HA_RSENSE_FAIL:        /* Auto Request-Sense Failed */
 356                 DBG(DBG_STATUS, printk(KERN_ERR "scsi%d: Auto Request Sense "
 357                                        "Failed\n", HD(cmd)->HBA_number));
 358                 result = DID_ERROR << 16;
 359                 break;
 360             case HA_UNX_BUSPHASE:       /* Unexpected Bus Phase */
 361             case HA_UNX_BUS_FREE:       /* Unexpected Bus Free */
 362             case HA_BUS_PARITY:         /* Bus Parity Error */
 363             case HA_UNX_MSGRJCT:        /* Unexpected Message Reject */
 364             case HA_RESET_STUCK:        /* SCSI Bus Reset Stuck */
 365             case HA_PARITY_ERR:         /* Controller Ram Parity */
 366             default:
 367                 result = DID_ERROR << 16;
 368                 break;
 369             }
 370             cmd->result = result | (scsi_stat << 1); 
 371             
 372 #if DBG_INTR2
 373             if (scsi_stat || result || hba_stat || eata_stat != 0x50 
 374                 || cmd->scsi_done == NULL || cmd->device->id == 7) 
 375                 printk("HBA: %d, channel %d, id: %d, lun %d, pid %ld:\n" 
 376                        "eata_stat %#x, hba_stat %#.2x, scsi_stat %#.2x, "
 377                        "sense_key: %#x, result: %#.8x\n", x, 
 378                        cmd->device->channel, cmd->device->id, cmd->device->lun,
 379                        cmd->pid, eata_stat, hba_stat, scsi_stat, 
 380                        cmd->sense_buffer[2] & 0xf, cmd->result); 
 381             DBG(DBG_INTR&&DBG_DELAY,DELAY(1));
 382 #endif
 383             
 384             ccb->status = FREE;     /* now we can release the slot  */
 385             cmd->scsi_done(cmd);
 386         }
 387     }
 388     restore_flags(flags);
 389     
 390     return;
 391 }
 392 
 393 inline int eata_send_command(u32 addr, u32 base, u8 command)
     /* [previous][next][first][last][top][bottom][index][help] */
 394 {
 395     long loop = R_LIMIT;
 396     
 397     while (inb(base + HA_RAUXSTAT) & HA_ABUSY)
 398         if (--loop == 0)
 399             return(FALSE);
 400 
 401     /* And now the address in nice little byte chunks */
 402 #ifdef __LITTLE_ENDIAN
 403     outb( addr & 0x000000ff,        base + HA_WDMAADDR);
 404     outb((addr & 0x0000ff00) >> 8,  base + HA_WDMAADDR + 1);
 405     outb((addr & 0x00ff0000) >> 16, base + HA_WDMAADDR + 2);
 406     outb((addr & 0xff000000) >> 24, base + HA_WDMAADDR + 3);
 407 #else
 408     outb((addr & 0xff000000) >> 24, base + HA_WDMAADDR);
 409     outb((addr & 0x00ff0000) >> 16, base + HA_WDMAADDR + 1);
 410     outb((addr & 0x0000ff00) >> 8,  base + HA_WDMAADDR + 2);
 411     outb((addr & 0x000000ff),       base + HA_WDMAADDR + 3);
 412 #endif
 413     outb(command, base + HA_WCOMMAND);
 414     return(TRUE);
 415 }
 416 
 417 inline int eata_send_immediate(u32 base, u32 addr, u8 ifc, u8 code, u8 code2)
     /* [previous][next][first][last][top][bottom][index][help] */
 418 {
 419     outb(0x0, base + HA_WDMAADDR - 1);
 420     if(addr){
 421 #ifdef __LITTLE_ENDIAN
 422         outb( addr & 0x000000ff,        base + HA_WDMAADDR);
 423         outb((addr & 0x0000ff00) >> 8,  base + HA_WDMAADDR + 1);
 424         outb((addr & 0x00ff0000) >> 16, base + HA_WDMAADDR + 2);
 425         outb((addr & 0xff000000) >> 24, base + HA_WDMAADDR + 3);
 426 #else
 427         outb((addr & 0xff000000) >> 24, base + HA_WDMAADDR);
 428         outb((addr & 0x00ff0000) >> 16, base + HA_WDMAADDR + 1);
 429         outb((addr & 0x0000ff00) >> 8,  base + HA_WDMAADDR + 2);
 430         outb((addr & 0x000000ff),       base + HA_WDMAADDR + 3);
 431 #endif
 432     } else {
 433         outb(0x0, base + HA_WDMAADDR);
 434         outb(0x0, base + HA_WDMAADDR + 1);
 435         outb(code2, base + HA_WCODE2);
 436         outb(code,  base + HA_WCODE);
 437     }
 438     
 439     outb(ifc, base + HA_WIFC);
 440     outb(EATA_CMD_IMMEDIATE, base + HA_WCOMMAND);
 441     return(TRUE);
 442 }
 443 
 444 int eata_queue(Scsi_Cmnd * cmd, void (* done) (Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 445 {
 446     unsigned int i, x, y;
 447     ulong flags;
 448     hostdata *hd;
 449     struct Scsi_Host *sh;
 450     struct eata_ccb *ccb;
 451     struct scatterlist *sl;
 452     
 453     save_flags(flags);
 454     cli();
 455     
 456     queue_counter++;
 457 
 458     hd = HD(cmd);
 459     sh = cmd->host;
 460     
 461 #if 1
 462     if (cmd->cmnd[0] == REQUEST_SENSE && cmd->sense_buffer[0] != 0) {
 463         DBG(DBG_REQSENSE, printk(KERN_DEBUG "Tried to REQUEST SENSE\n"));
 464         cmd->result = DID_OK << 16;
 465         done(cmd);
 466         return(0);
 467     }
 468 #endif
 469 
 470     /* check for free slot */
 471     for (y = hd->last_ccb + 1, x = 0; x < sh->can_queue; x++, y++) { 
 472         if (y >= sh->can_queue)
 473             y = 0;
 474         if (hd->ccb[y].status == FREE)
 475             break;
 476     }
 477     
 478     hd->last_ccb = y;
 479 
 480     if (x >= sh->can_queue) { 
 481         cmd->result = DID_BUS_BUSY << 16;
 482         DBG(DBG_QUEUE && DBG_ABNORM, 
 483             printk(KERN_CRIT "eata_queue pid %ld, HBA QUEUE FULL..., "
 484                    "returning DID_BUS_BUSY\n", cmd->pid));
 485         done(cmd);
 486         restore_flags(flags);
 487         return(0);
 488     }
 489     ccb = &hd->ccb[y];
 490     
 491     memset(ccb, 0, sizeof(struct eata_ccb) - sizeof(struct eata_sg_list *));
 492     
 493     ccb->status = USED;                 /* claim free slot */
 494     
 495     DBG(DBG_QUEUE, printk("eata_queue pid %ld, target: %x, lun: %x, y %d\n",
 496                           cmd->pid, cmd->target, cmd->lun, y));
 497     DBG(DBG_QUEUE && DBG_DELAY, DELAY(1));
 498     
 499     if(hd->do_latency == TRUE) 
 500         eata_latency_out(ccb, cmd);
 501 
 502     cmd->scsi_done = (void *)done;
 503     
 504     switch (cmd->cmnd[0]) {
 505     case CHANGE_DEFINITION: case COMPARE:         case COPY:
 506     case COPY_VERIFY:       case LOG_SELECT:      case MODE_SELECT:
 507     case MODE_SELECT_10:    case SEND_DIAGNOSTIC: case WRITE_BUFFER:
 508     case FORMAT_UNIT:       case REASSIGN_BLOCKS: case RESERVE:
 509     case SEARCH_EQUAL:      case SEARCH_HIGH:     case SEARCH_LOW:
 510     case WRITE_6:           case WRITE_10:        case WRITE_VERIFY:
 511     case UPDATE_BLOCK:      case WRITE_LONG:      case WRITE_SAME:      
 512     case SEARCH_HIGH_12:    case SEARCH_EQUAL_12: case SEARCH_LOW_12:
 513     case WRITE_12:          case WRITE_VERIFY_12: case SET_WINDOW: 
 514     case MEDIUM_SCAN:       case SEND_VOLUME_TAG:            
 515     case 0xea:      /* alternate number for WRITE LONG */
 516         ccb->DataOut = TRUE;    /* Output mode */
 517         break;
 518     case TEST_UNIT_READY:
 519     default:
 520         ccb->DataIn = TRUE;     /* Input mode  */
 521     }
 522 
 523     /* FIXME: This will will have to be changed once the midlevel driver 
 524      *        allows different HBA IDs on every channel.
 525      */
 526     if (cmd->target == sh->this_id) 
 527         ccb->Interpret = TRUE;  /* Interpret command */
 528 
 529     if (cmd->use_sg) {
 530         ccb->scatter = TRUE;    /* SG mode     */
 531         if (ccb->sg_list == NULL) {
 532             ccb->sg_list = kmalloc(sh->sg_tablesize * sizeof(struct eata_sg_list),
 533                                   GFP_ATOMIC | GFP_DMA);
 534         }
 535         if (ccb->sg_list == NULL)
 536             panic("eata_dma: Run out of DMA memory for SG lists !\n");
 537         ccb->cp_dataDMA = htonl(virt_to_bus(ccb->sg_list)); 
 538         
 539         ccb->cp_datalen = htonl(cmd->use_sg * sizeof(struct eata_sg_list));
 540         sl=(struct scatterlist *)cmd->request_buffer;
 541         for(i = 0; i < cmd->use_sg; i++, sl++){
 542             ccb->sg_list[i].data = htonl(virt_to_bus(sl->address));
 543             ccb->sg_list[i].len = htonl((u32) sl->length);
 544         }
 545     } else {
 546         ccb->scatter = FALSE;
 547         ccb->cp_datalen = htonl(cmd->request_bufflen);
 548         ccb->cp_dataDMA = htonl(virt_to_bus(cmd->request_buffer));
 549     }
 550     
 551     ccb->Auto_Req_Sen = TRUE;
 552     ccb->cp_reqDMA = htonl(virt_to_bus(cmd->sense_buffer));
 553     ccb->reqlen = sizeof(cmd->sense_buffer);
 554     
 555     ccb->cp_id = cmd->target;
 556     ccb->cp_channel = cmd->channel;
 557     ccb->cp_lun = cmd->lun;
 558     ccb->cp_dispri = TRUE;
 559     ccb->cp_identify = TRUE;
 560     memcpy(ccb->cp_cdb, cmd->cmnd, cmd->cmd_len);
 561     
 562     ccb->cp_statDMA = htonl(virt_to_bus(&(hd->sp)));
 563     
 564     ccb->cp_viraddr = ccb; /* This will be passed thru, so we don't need to 
 565                           * convert it */
 566     ccb->cmd = cmd;
 567     cmd->host_scribble = (char *)&hd->ccb[y];   
 568     
 569     if(eata_send_command((u32) ccb, (u32) sh->base, EATA_CMD_DMA_SEND_CP) == FALSE) {
 570         cmd->result = DID_BUS_BUSY << 16;
 571         DBG(DBG_QUEUE && DBG_ABNORM, 
 572             printk("eata_queue target %d, pid %ld, HBA busy, "
 573                    "returning DID_BUS_BUSY\n",cmd->target, cmd->pid));
 574         done(cmd);
 575         ccb->status = FREE;    
 576         restore_flags(flags);
 577         return(0);
 578     }
 579     DBG(DBG_QUEUE, printk("Queued base %#.4x pid: %ld target: %x lun: %x "
 580                          "slot %d irq %d\n", (s32)sh->base, cmd->pid, 
 581                          cmd->target, cmd->lun, y, sh->irq));
 582     DBG(DBG_QUEUE && DBG_DELAY, DELAY(1));
 583     restore_flags(flags);
 584     return(0);
 585 }
 586 
 587 
 588 int eata_abort(Scsi_Cmnd * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 589 {
 590     ulong loop = R_LIMIT;
 591     ulong flags;
 592 
 593     save_flags(flags);
 594     cli();
 595 
 596     DBG(DBG_ABNORM, printk("eata_abort called pid: %ld target: %x lun: %x"
 597                            " reason %x\n", cmd->pid, cmd->target, cmd->lun, 
 598                            cmd->abort_reason));
 599     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 600 
 601     while (inb((u32)(cmd->host->base) + HA_RAUXSTAT) & HA_ABUSY) {
 602         if (--loop == 0) {
 603             printk("eata_dma: abort, timeout error.\n");
 604             DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 605             restore_flags(flags);
 606             return (SCSI_ABORT_ERROR);
 607         }
 608     }
 609     if (CD(cmd)->status == RESET) {
 610         printk("eata_dma: abort, command reset error.\n");
 611         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 612         restore_flags(flags);
 613         return (SCSI_ABORT_ERROR);
 614     }
 615     if (CD(cmd)->status == LOCKED) {
 616         DBG(DBG_ABNORM, printk("eata_dma: abort, queue slot locked.\n"));
 617         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 618         restore_flags(flags);
 619         return (SCSI_ABORT_NOT_RUNNING);
 620     }
 621     if (CD(cmd)->status == USED) {
 622         DBG(DBG_ABNORM, printk("Returning: SCSI_ABORT_BUSY\n"));
 623         restore_flags(flags);
 624         return (SCSI_ABORT_BUSY);  /* SNOOZE */ 
 625     }
 626     if (CD(cmd)->status == FREE) {
 627         DBG(DBG_ABNORM, printk("Returning: SCSI_ABORT_NOT_RUNNING\n")); 
 628         restore_flags(flags);
 629         return (SCSI_ABORT_NOT_RUNNING);
 630     }
 631     restore_flags(flags);
 632     panic("eata_dma: abort: invalid slot status\n");
 633 }
 634 
 635 int eata_reset(Scsi_Cmnd * cmd, unsigned int resetflags)
     /* [previous][next][first][last][top][bottom][index][help] */
 636 {
 637     ushort x, z; 
 638     ulong limit = 0;
 639     ulong loop = loops_per_sec / 3;
 640     ulong flags;
 641     unchar success = FALSE;
 642     Scsi_Cmnd *sp; 
 643     
 644     save_flags(flags);
 645     cli();
 646     
 647     DBG(DBG_ABNORM, printk("eata_reset called pid:%ld target: %x lun: %x"
 648                            " reason %x\n", cmd->pid, cmd->target, cmd->lun, 
 649                            cmd->abort_reason));
 650         
 651     if (HD(cmd)->state == RESET) {
 652         printk("eata_reset: exit, already in reset.\n");
 653         restore_flags(flags);
 654         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 655         return (SCSI_RESET_ERROR);
 656     }
 657     
 658     while (inb((u32)(cmd->host->base) + HA_RAUXSTAT) & HA_ABUSY)
 659         if (--loop == 0) {
 660             printk("eata_reset: exit, timeout error.\n");
 661             restore_flags(flags);
 662             DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 663             return (SCSI_RESET_ERROR);
 664         }
 665  
 666     for (x = 0; x < MAXCHANNEL; x++) {
 667         for (z = 0; z < MAXTARGET; z++) {
 668             HD(cmd)->t_state[x][z] = RESET;
 669             HD(cmd)->t_timeout[x][z] = NO_TIMEOUT;
 670         }
 671     }
 672 
 673     for (x = 0; x < cmd->host->can_queue; x++) {
 674         if (HD(cmd)->ccb[x].status == FREE)
 675             continue;
 676         
 677         if (HD(cmd)->ccb[x].status == LOCKED) {
 678             HD(cmd)->ccb[x].status = FREE;
 679             printk("eata_reset: locked slot %d forced free.\n", x);
 680             DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 681             continue;
 682         }
 683         sp = HD(cmd)->ccb[x].cmd;
 684         HD(cmd)->ccb[x].status = RESET;
 685         printk("eata_reset: slot %d in reset, pid %ld.\n", x, sp->pid);
 686         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 687         
 688         if (sp == NULL)
 689             panic("eata_reset: slot %d, sp==NULL.\n", x);
 690         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 691         
 692         if (sp == cmd)
 693             success = TRUE;
 694     }
 695     
 696     /* hard reset the HBA  */
 697     inb((u32) (cmd->host->base) + HA_RSTATUS);  /* This might cause trouble */
 698     eata_send_command(0, (u32) cmd->host->base, EATA_CMD_RESET);
 699     
 700     DBG(DBG_ABNORM, printk("eata_reset: board reset done, enabling interrupts.\n"));
 701     HD(cmd)->state = RESET;
 702 
 703     DELAY(1); 
 704     
 705     DBG(DBG_ABNORM, printk("eata_reset: interrupts disabled, loops %ld.\n", 
 706                            limit));
 707     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 708     
 709     for (x = 0; x < cmd->host->can_queue; x++) {
 710         
 711         /* Skip slots already set free by interrupt */
 712         if (HD(cmd)->ccb[x].status != RESET)
 713             continue;
 714         
 715         sp = HD(cmd)->ccb[x].cmd;
 716         sp->result = DID_RESET << 16;
 717         
 718         /* This mailbox is still waiting for its interrupt */
 719         HD(cmd)->ccb[x].status = LOCKED;
 720         
 721         printk("eata_reset: slot %d locked, DID_RESET, pid %ld done.\n",
 722                x, sp->pid);
 723         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 724 
 725         sp->scsi_done(sp);
 726     }
 727     
 728     HD(cmd)->state = FALSE;
 729     restore_flags(flags);
 730     
 731     if (success) {
 732         DBG(DBG_ABNORM, printk("eata_reset: exit, success.\n"));
 733         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 734         return (SCSI_RESET_SUCCESS);
 735     } else {
 736         DBG(DBG_ABNORM, printk("eata_reset: exit, wakeup.\n"));
 737         DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
 738         return (SCSI_RESET_PUNT);
 739     }
 740 }
 741 
 742 /* Here we try to determine the optimum queue depth for
 743  * each attached device.
 744  *
 745  * At the moment the algorithm is rather simple
 746  */
 747 static void eata_select_queue_depths(struct Scsi_Host *host, 
     /* [previous][next][first][last][top][bottom][index][help] */
 748                                      Scsi_Device *devicelist)
 749 {
 750     Scsi_Device *device;
 751     int devcount = 0; 
 752     int factor = 0;
 753     
 754     
 755     /* First we do a sample run go find out what we have */
 756     for(device = devicelist; device != NULL; device = device->next) {
 757         if (device->host == host) {
 758             devcount++;
 759             switch(device->type) {
 760             case TYPE_DISK:
 761             case TYPE_MOD:
 762                 factor += TYPE_DISK_QUEUE;
 763                 break;
 764             case TYPE_TAPE:
 765                 factor += TYPE_TAPE_QUEUE;
 766                 break;
 767             case TYPE_WORM:
 768             case TYPE_ROM:
 769                 factor += TYPE_ROM_QUEUE;
 770                 break;
 771             case TYPE_PROCESSOR:
 772             case TYPE_SCANNER:
 773             default:
 774                 factor += TYPE_OTHER_QUEUE;
 775                 break;
 776             }
 777         }
 778     }
 779 
 780     DBG(DBG_REGISTER, printk(KERN_DEBUG "scsi%d: needed queueslots %d\n", 
 781                              host->host_no, factor));
 782 
 783     if(factor == 0)    /* We don't want to get a DIV BY ZERO error */
 784         factor = 1;
 785 
 786     factor = (SD(host)->queuesize * 10) / factor;
 787 
 788     DBG(DBG_REGISTER, printk(KERN_DEBUG "scsi%d: using factor %dE-1\n", 
 789                              host->host_no, factor));
 790 
 791     /* Now that have the factor we can set the individual queuesizes */
 792     for(device = devicelist; device != NULL; device = device->next) {
 793         if(device->host == host) {
 794             if(SD(device->host)->bustype != IS_ISA){
 795                 switch(device->type) {
 796                 case TYPE_DISK:
 797                 case TYPE_MOD:
 798                     device->queue_depth = (TYPE_DISK_QUEUE * factor) / 10;
 799                     break;
 800                 case TYPE_TAPE:
 801                     device->queue_depth = (TYPE_TAPE_QUEUE * factor) / 10;
 802                     break;
 803                 case TYPE_WORM:
 804                 case TYPE_ROM:
 805                     device->queue_depth = (TYPE_ROM_QUEUE * factor) / 10;
 806                     break;
 807                 case TYPE_PROCESSOR:
 808                 case TYPE_SCANNER:
 809                 default:
 810                     device->queue_depth = (TYPE_OTHER_QUEUE * factor) / 10;
 811                     break;
 812                 }
 813             } else /* ISA forces us to limit the QS because of bounce buffers*/
 814                 device->queue_depth = 2; /* I know this is cruel */
 815 
 816             printk(KERN_INFO "scsi%d: queue depth for target %d on channel %d "
 817                    "set to %d\n", host->host_no, device->id, device->channel,
 818                    device->queue_depth);
 819         }
 820     }
 821 }
 822 
 823 int check_blink_state(long base)
     /* [previous][next][first][last][top][bottom][index][help] */
 824 {
 825     ushort loops = 10;
 826     u32 blinkindicator;
 827     u32 state = 0x12345678;
 828     u32 oldstate = 0;
 829 
 830     blinkindicator = htonl(0x54504442);
 831     while ((loops--) && (state != oldstate)) {
 832         oldstate = state;
 833         state = inl((uint) base + 1);
 834     }
 835 
 836     DBG(DBG_BLINK, printk("Did Blink check. Status: %d\n",
 837               (state == oldstate) && (state == blinkindicator)));
 838 
 839     if ((state == oldstate) && (state == blinkindicator))
 840         return(TRUE);
 841     else
 842         return (FALSE);
 843 }
 844 
 845 char * get_board_data(u32 base, u32 irq, u32 id)
     /* [previous][next][first][last][top][bottom][index][help] */
 846 {
 847     struct eata_ccb *cp;
 848     struct eata_sp  *sp;
 849     static char *buff;
 850     ulong i;
 851     ulong limit = 0;
 852 
 853     cp = (struct eata_ccb *) scsi_init_malloc(sizeof(struct eata_ccb),
 854                                               GFP_ATOMIC | GFP_DMA);
 855     sp = (struct eata_sp *) scsi_init_malloc(sizeof(struct eata_sp), 
 856                                              GFP_ATOMIC | GFP_DMA);
 857 
 858     buff = dma_scratch;
 859  
 860     memset(cp, 0, sizeof(struct eata_ccb));
 861     memset(sp, 0, sizeof(struct eata_sp));
 862     memset(buff, 0, 256);
 863 
 864     cp->DataIn = TRUE;     
 865     cp->Interpret = TRUE;   /* Interpret command */
 866     cp->cp_dispri = TRUE;
 867     cp->cp_identify = TRUE;
 868  
 869     cp->cp_datalen = htonl(56);  
 870     cp->cp_dataDMA = htonl(virt_to_bus(buff));
 871     cp->cp_statDMA = htonl(virt_to_bus(sp));
 872     cp->cp_viraddr = cp;
 873     
 874     cp->cp_id = id;
 875     cp->cp_lun = 0;
 876 
 877     cp->cp_cdb[0] = INQUIRY;
 878     cp->cp_cdb[1] = 0;
 879     cp->cp_cdb[2] = 0;
 880     cp->cp_cdb[3] = 0;
 881     cp->cp_cdb[4] = 56;
 882     cp->cp_cdb[5] = 0;
 883 
 884     fake_int_base = (struct eata_register *) base;
 885     fake_int_result = FALSE;
 886     fake_int_happened = FALSE;
 887 
 888     eata_send_command((u32) cp, (u32) base, EATA_CMD_DMA_SEND_CP);
 889     
 890     i = jiffies + (3 * HZ);
 891     while (fake_int_happened == FALSE && jiffies <= i) 
 892         barrier();
 893     
 894     DBG(DBG_INTR3, printk(KERN_DEBUG "fake_int_result: %#x hbastat %#x "
 895                           "scsistat %#x, buff %p sp %p\n",
 896                           fake_int_result, (u32) (sp->hba_stat /*& 0x7f*/), 
 897                           (u32) sp->scsi_stat, buff, sp));
 898 
 899     scsi_init_free((void *)cp, sizeof(struct eata_ccb));
 900     scsi_init_free((void *)sp, sizeof(struct eata_sp));
 901     
 902     if ((fake_int_result & HA_SERROR) || jiffies > i){
 903         printk(KERN_WARNING "eata_dma: trying to reset HBA at %x to clear "
 904                "possible blink state\n", base); 
 905         /* hard reset the HBA  */
 906         inb((u32) (base) + HA_RSTATUS);
 907         eata_send_command(0, base, EATA_CMD_RESET);
 908         DELAY(1);
 909         return (NULL);
 910     } else
 911         return (buff);
 912 }
 913 
 914 
 915 int get_conf_PIO(u32 base, struct get_conf *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 916 {
 917     ulong loop = R_LIMIT;
 918     u16 *p;
 919 
 920     if(check_region(base, 9)) 
 921         return (FALSE);
 922      
 923     memset(buf, 0, sizeof(struct get_conf));
 924 
 925     while (inb(base + HA_RSTATUS) & HA_SBUSY)
 926         if (--loop == 0) 
 927             return (FALSE);
 928 
 929     fake_int_base = (struct eata_register *) base;
 930     fake_int_result = FALSE;
 931     fake_int_happened = FALSE;
 932        
 933     DBG(DBG_PIO && DBG_PROBE,
 934         printk("Issuing PIO READ CONFIG to HBA at %#x\n", base));
 935     eata_send_command(0, base, EATA_CMD_PIO_READ_CONFIG);
 936 
 937     loop = R_LIMIT;
 938     for (p = (u16 *) buf; 
 939          (long)p <= ((long)buf + (sizeof(struct get_conf) / 2)); p++) {
 940         while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
 941             if (--loop == 0)
 942                 return (FALSE);
 943 
 944         loop = R_LIMIT;
 945         *p = inw(base + HA_RDATA);
 946     }
 947 
 948     if (!(inb(base + HA_RSTATUS) & HA_SERROR)) {            /* Error ? */
 949         if (htonl(EATA_SIGNATURE) == buf->signature) {
 950             DBG(DBG_PIO&&DBG_PROBE, printk("EATA Controller found at %x "
 951                                            "EATA Level: %x\n", (uint) base, 
 952                                            (uint) (buf->version)));
 953             
 954             while (inb(base + HA_RSTATUS) & HA_SDRQ) 
 955                 inw(base + HA_RDATA);
 956             return (TRUE);
 957         } 
 958     } else {
 959         DBG(DBG_PROBE, printk("eata_dma: get_conf_PIO, error during transfer "
 960                   "for HBA at %lx\n", (long)base));
 961     }
 962     return (FALSE);
 963 }
 964 
 965 
 966 void print_config(struct get_conf *gc)
     /* [previous][next][first][last][top][bottom][index][help] */
 967 {
 968     printk("LEN: %d ver:%d OCS:%d TAR:%d TRNXFR:%d MORES:%d DMAS:%d\n",
 969            (u32) ntohl(gc->len), gc->version,
 970            gc->OCS_enabled, gc->TAR_support, gc->TRNXFR, gc->MORE_support,
 971            gc->DMA_support);
 972     printk("DMAV:%d HAAV:%d SCSIID0:%d ID1:%d ID2:%d QUEUE:%d SG:%d SEC:%d\n",
 973            gc->DMA_valid, gc->HAA_valid, gc->scsi_id[3], gc->scsi_id[2],
 974            gc->scsi_id[1], ntohs(gc->queuesiz), ntohs(gc->SGsiz), gc->SECOND);
 975     printk("IRQ:%d IRQT:%d DMAC:%d FORCADR:%d SG_64K:%d SG_UAE:%d MID:%d "
 976            "MCH:%d MLUN:%d\n",
 977            gc->IRQ, gc->IRQ_TR, (8 - gc->DMA_channel) & 7, gc->FORCADR, 
 978            gc->SG_64K, gc->SG_UAE, gc->MAX_ID, gc->MAX_CHAN, gc->MAX_LUN); 
 979     printk("RIDQ:%d PCI:%d EISA:%d\n",
 980            gc->ID_qest, gc->is_PCI, gc->is_EISA);
 981     DBG(DPT_DEBUG, DELAY(14));
 982 }
 983 
 984 short register_HBA(u32 base, struct get_conf *gc, Scsi_Host_Template * tpnt, 
     /* [previous][next][first][last][top][bottom][index][help] */
 985                    u8 bustype)
 986 {
 987     ulong size = 0;
 988     unchar dma_channel = 0;
 989     char *buff = 0;
 990     unchar bugs = 0;
 991     struct Scsi_Host *sh;
 992     hostdata *hd;
 993     int x;
 994     
 995     
 996     DBG(DBG_REGISTER, print_config(gc));
 997 
 998     if (gc->DMA_support == FALSE) {
 999         printk("The EATA HBA at %#.4x does not support DMA.\n" 
1000                "Please use the EATA-PIO driver.\n", base);
1001         return (FALSE);
1002     }
1003     if(gc->HAA_valid == FALSE || ntohl(gc->len) < 0x22) 
1004         gc->MAX_CHAN = 0;
1005 
1006     if (reg_IRQ[gc->IRQ] == FALSE) {    /* Interrupt already registered ? */
1007         if (!request_irq(gc->IRQ, (void *) eata_fake_int_handler, SA_INTERRUPT,
1008                          "eata_dma", NULL)){
1009             reg_IRQ[gc->IRQ]++;
1010             if (!gc->IRQ_TR)
1011                 reg_IRQL[gc->IRQ] = TRUE;   /* IRQ is edge triggered */
1012         } else {
1013             printk("Couldn't allocate IRQ %d, Sorry.", gc->IRQ);
1014             return (FALSE);
1015         }
1016     } else {            /* More than one HBA on this IRQ */
1017         if (reg_IRQL[gc->IRQ] == TRUE) {
1018             printk("Can't support more than one HBA on this IRQ,\n"
1019                    "  if the IRQ is edge triggered. Sorry.\n");
1020             return (FALSE);
1021         } else
1022             reg_IRQ[gc->IRQ]++;
1023     }
1024 
1025  
1026     /* If DMA is supported but DMA_valid isn't set to indicate that
1027      * the channel number is given we must have pre 2.0 firmware (1.7?)
1028      * which leaves us to guess since the "newer ones" also don't set the 
1029      * DMA_valid bit.
1030      */
1031     if (gc->DMA_support && !gc->DMA_valid && gc->DMA_channel) {
1032       printk(KERN_WARNING "eata_dma: If you are using a pre 2.0 firmware "
1033              "please update it !\n"
1034              "          You can get new firmware releases from ftp.dpt.com\n");
1035         gc->DMA_channel = (base == 0x1f0 ? 3 /* DMA=5 */ : 2 /* DMA=6 */);
1036         gc->DMA_valid = TRUE;
1037     }
1038 
1039     /* if gc->DMA_valid it must be an ISA HBA and we have to register it */
1040     dma_channel = BUSMASTER;
1041     if (gc->DMA_valid) {
1042         if (request_dma(dma_channel = (8 - gc->DMA_channel) & 7, "eata_dma")) {
1043             printk(KERN_WARNING "Unable to allocate DMA channel %d for ISA HBA"
1044                    " at %#.4x.\n", dma_channel, base);
1045             reg_IRQ[gc->IRQ]--;
1046             if (reg_IRQ[gc->IRQ] == 0)
1047                 free_irq(gc->IRQ, NULL);
1048             if (gc->IRQ_TR == FALSE)
1049                 reg_IRQL[gc->IRQ] = FALSE; 
1050             return (FALSE);
1051         }
1052     }
1053 
1054     if (dma_channel != BUSMASTER) {
1055         disable_dma(dma_channel);
1056         clear_dma_ff(dma_channel);
1057         set_dma_mode(dma_channel, DMA_MODE_CASCADE);
1058         enable_dma(dma_channel);
1059     }
1060 
1061     if (bustype != IS_EISA && bustype != IS_ISA)
1062         buff = get_board_data(base, gc->IRQ, gc->scsi_id[3]);
1063 
1064     if (buff == NULL) {
1065         if (bustype == IS_EISA || bustype == IS_ISA) {
1066             bugs = bugs || BROKEN_INQUIRY;
1067         } else {
1068             if (gc->DMA_support == FALSE)
1069                 printk(KERN_WARNING "HBA at %#.4x doesn't support DMA. "
1070                        "Sorry\n", base);
1071             else
1072                 printk(KERN_WARNING "HBA at %#.4x does not react on INQUIRY. "
1073                        "Sorry.\n", base);
1074             if (gc->DMA_valid) 
1075                 free_dma(dma_channel);
1076             reg_IRQ[gc->IRQ]--;
1077             if (reg_IRQ[gc->IRQ] == 0)
1078                 free_irq(gc->IRQ, NULL);
1079             if (gc->IRQ_TR == FALSE)
1080                 reg_IRQL[gc->IRQ] = FALSE; 
1081             return (FALSE);
1082         }
1083     }
1084  
1085     if (gc->DMA_support == FALSE && buff != NULL)  
1086         printk(KERN_WARNING "HBA %.12sat %#.4x doesn't set the DMA_support "
1087                "flag correctly.\n", &buff[16], base);
1088     
1089     request_region(base, 9, "eata_dma"); /* We already checked the 
1090                                           * availability, so this
1091                                           * should not fail.
1092                                           */
1093     
1094     if(ntohs(gc->queuesiz) == 0) {
1095         gc->queuesiz = ntohs(64);
1096         printk(KERN_WARNING "Warning: Queue size has to be corrected. Assuming"
1097                " 64 queueslots\n"
1098                "         This might be a PM2012B with a defective Firmware\n"
1099                "         Contact DPT support@dpt.com for an upgrade\n");
1100     }
1101 
1102     size = sizeof(hostdata) + ((sizeof(struct eata_ccb) + sizeof(long)) 
1103                                * ntohs(gc->queuesiz));
1104 
1105     DBG(DBG_REGISTER, printk("scsi_register size: %ld\n", size));
1106 
1107     sh = scsi_register(tpnt, size);
1108     
1109     if(sh != NULL) {
1110 
1111         hd = SD(sh);               
1112 
1113         memset(hd->reads, 0, sizeof(u32) * 26); 
1114         
1115         sh->select_queue_depths = eata_select_queue_depths;
1116         
1117         hd->bustype = bustype;
1118 
1119         /*
1120          * If we are using a ISA board, we can't use extended SG,
1121          * because we would need exessive amounts of memory for
1122          * bounce buffers.
1123          */
1124         if (gc->SG_64K==TRUE && ntohs(gc->SGsiz)==64 && hd->bustype!=IS_ISA){
1125             sh->sg_tablesize = SG_SIZE_BIG;
1126         } else {
1127             sh->sg_tablesize = ntohs(gc->SGsiz);
1128             if (sh->sg_tablesize > SG_SIZE || sh->sg_tablesize == 0) {
1129                 if (sh->sg_tablesize == 0)
1130                     printk(KERN_WARNING "Warning: SG size had to be fixed.\n"
1131                            "This might be a PM2012 with a defective Firmware"
1132                            "\nContact DPT support@dpt.com for an upgrade\n");
1133                 sh->sg_tablesize = SG_SIZE;
1134             }
1135         }
1136         hd->sgsize = sh->sg_tablesize;
1137     }
1138 
1139     if(sh != NULL) {
1140         sh->can_queue = hd->queuesize = ntohs(gc->queuesiz);
1141         sh->cmd_per_lun = 0;
1142     }
1143 
1144     if(sh == NULL) { 
1145         DBG(DBG_REGISTER, printk(KERN_NOTICE "eata_dma: couldn't register HBA"
1146                                  " at%x \n", base));
1147         scsi_unregister(sh);
1148         if (gc->DMA_valid) 
1149             free_dma(dma_channel);
1150         
1151         reg_IRQ[gc->IRQ]--;
1152         if (reg_IRQ[gc->IRQ] == 0)
1153             free_irq(gc->IRQ, NULL);
1154         if (gc->IRQ_TR == FALSE)
1155             reg_IRQL[gc->IRQ] = FALSE; 
1156         return (FALSE);
1157     }
1158 
1159     
1160     hd->broken_INQUIRY = (bugs & BROKEN_INQUIRY);
1161 
1162     if(hd->broken_INQUIRY == TRUE) {
1163         strcpy(hd->vendor, "DPT");
1164         strcpy(hd->name, "??????????");
1165         strcpy(hd->revision, "???.?");
1166         hd->firmware_revision = 0;
1167     } else {    
1168         strncpy(hd->vendor, &buff[8], 8);
1169         hd->vendor[8] = 0;
1170         strncpy(hd->name, &buff[16], 17);
1171         hd->name[17] = 0;
1172         hd->revision[0] = buff[32];
1173         hd->revision[1] = buff[33];
1174         hd->revision[2] = buff[34];
1175         hd->revision[3] = '.';
1176         hd->revision[4] = buff[35];
1177         hd->revision[5] = 0;
1178         hd->firmware_revision = (buff[32] << 24) + (buff[33] << 16) 
1179                                     + (buff[34] << 8) + buff[35]; 
1180     }
1181 
1182     if (hd->firmware_revision >= (('0'<<24) + ('7'<<16) + ('G'<< 8) + '0'))
1183         hd->immediate_support = 1;
1184     else 
1185         hd->immediate_support = 0;
1186 
1187     switch (ntohl(gc->len)) {
1188     case 0x1c:
1189         hd->EATA_revision = 'a';
1190         break;
1191     case 0x1e:
1192         hd->EATA_revision = 'b';
1193         break;
1194     case 0x22:
1195         hd->EATA_revision = 'c';
1196         break;
1197     case 0x24:
1198         hd->EATA_revision = 'z';                
1199     default:
1200         hd->EATA_revision = '?';
1201     }
1202 
1203 
1204     if(ntohl(gc->len) >= 0x22) {
1205         sh->max_id = gc->MAX_ID + 1;
1206         sh->max_lun = gc->MAX_LUN + 1;
1207     } else {
1208         sh->max_id = 8;
1209         sh->max_lun = 8;
1210     }
1211 
1212     hd->HBA_number = sh->host_no;
1213     hd->channel = gc->MAX_CHAN;     
1214     sh->max_channel = gc->MAX_CHAN; 
1215     sh->unique_id = base;
1216     sh->base = (char *) base;
1217     sh->io_port = base;
1218     sh->n_io_port = 9;
1219     sh->irq = gc->IRQ;
1220     sh->dma_channel = dma_channel;
1221     
1222     /* FIXME:
1223      * SCSI midlevel code should support different HBA ids on every channel
1224      */
1225     sh->this_id = gc->scsi_id[3];
1226     
1227     if (gc->SECOND)
1228         hd->primary = FALSE;
1229     else
1230         hd->primary = TRUE;
1231     
1232     sh->wish_block = FALSE;        
1233     
1234     if (hd->bustype != IS_ISA) {
1235         sh->unchecked_isa_dma = FALSE;
1236     } else {
1237         sh->unchecked_isa_dma = TRUE;   /* We're doing ISA DMA */
1238     }
1239     
1240     for(x = 0; x <= 11; x++){            /* Initialize min. latency */
1241         hd->writes_lat[x][1] = 0xffffffff;
1242         hd->reads_lat[x][1] = 0xffffffff;
1243     }
1244     hd->all_lat[1] = 0xffffffff;
1245 
1246     hd->next = NULL;    /* build a linked list of all HBAs */
1247     hd->prev = last_HBA;
1248     if(hd->prev != NULL)
1249         SD(hd->prev)->next = sh;
1250     last_HBA = sh;
1251     if (first_HBA == NULL)
1252         first_HBA = sh;
1253     registered_HBAs++;
1254     
1255     return (TRUE);
1256 }
1257 
1258 
1259 
1260 void find_EISA(struct get_conf *buf, Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1261 {
1262     u32 base;
1263     int i;
1264     
1265 #if CHECKPAL
1266     u8 pal1, pal2, pal3;
1267 #endif
1268     
1269     for (i = 0; i < MAXEISA; i++) {
1270         if (EISAbases[i] == TRUE) { /* Still a possibility ?          */
1271             
1272             base = 0x1c88 + (i * 0x1000);
1273 #if CHECKPAL
1274             pal1 = inb((u16)base - 8);
1275             pal2 = inb((u16)base - 7);
1276             pal3 = inb((u16)base - 6);
1277             
1278             if (((pal1 == DPT_ID1) && (pal2 == DPT_ID2)) ||
1279                 ((pal1 == NEC_ID1) && (pal2 == NEC_ID2) && (pal3 == NEC_ID3))||
1280                 ((pal1 == ATT_ID1) && (pal2 == ATT_ID2) && (pal3 == ATT_ID3))){
1281                 DBG(DBG_PROBE, printk("EISA EATA id tags found: %x %x %x \n",
1282                                       (int)pal1, (int)pal2, (int)pal3));
1283 #endif
1284                 if (get_conf_PIO(base, buf) == TRUE) {
1285                     if (buf->IRQ) {  
1286                         DBG(DBG_EISA, printk("Registering EISA HBA\n"));
1287                         register_HBA(base, buf, tpnt, IS_EISA);
1288                     } else
1289                         printk("eata_dma: No valid IRQ. HBA removed from list\n");
1290                 } else {
1291                     if (check_blink_state(base)) 
1292                         printk("HBA is in BLINK state. Consult your HBAs "
1293                                "Manual to correct this.\n");
1294                 } 
1295                 /* Nothing found here so we take it from the list */
1296                 EISAbases[i] = 0;  
1297 #if CHECKPAL
1298             } 
1299 #endif
1300         }
1301     }
1302     return; 
1303 }
1304 
1305 void find_ISA(struct get_conf *buf, Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1306 {
1307     int i;
1308     
1309     for (i = 0; i < MAXISA; i++) {  
1310         if (ISAbases[i]) {  
1311             if (get_conf_PIO(ISAbases[i],buf) == TRUE){
1312                 DBG(DBG_ISA, printk("Registering ISA HBA\n"));
1313                 register_HBA(ISAbases[i], buf, tpnt, IS_ISA);
1314             } else {
1315                 if (check_blink_state(ISAbases[i])) 
1316                     printk("HBA is in BLINK state. Consult your HBAs "
1317                            "Manual to correct this.\n");
1318             }
1319             ISAbases[i] = 0;
1320         }
1321     }
1322     return;
1323 }
1324 
1325 void find_PCI(struct get_conf *buf, Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1326 {
1327 
1328 #ifndef CONFIG_PCI
1329     printk("eata_dma: kernel PCI support not enabled. Skipping scan for PCI HBAs.\n");
1330 #else
1331     
1332     u8 pci_bus, pci_device_fn;
1333     static s16 pci_index = 0;   /* Device index to PCI BIOS calls */
1334     u32 base = 0;
1335     u16 com_adr;
1336     u16 rev_device;
1337     u32 error, i, x;
1338     u8 pal1, pal2, pal3;
1339 
1340     if (pcibios_present()) {
1341         for (i = 0; i <= MAXPCI; ++i, ++pci_index) {
1342             if (pcibios_find_device(PCI_VENDOR_ID_DPT, PCI_DEVICE_ID_DPT, 
1343                                     pci_index, &pci_bus, &pci_device_fn))
1344                 break;
1345             DBG(DBG_PROBE && DBG_PCI, 
1346                 printk("eata_dma: find_PCI, HBA at bus %d, device %d,"
1347                        " function %d, index %d\n", (s32)pci_bus, 
1348                        (s32)((pci_device_fn & 0xf8) >> 3),
1349                        (s32)(pci_device_fn & 7), pci_index));
1350             
1351             if (!(error = pcibios_read_config_word(pci_bus, pci_device_fn, 
1352                                        PCI_CLASS_DEVICE, &rev_device))) {
1353                 if (rev_device == PCI_CLASS_STORAGE_SCSI) {
1354                     if (!(error = pcibios_read_config_word(pci_bus, 
1355                                                pci_device_fn, PCI_COMMAND, 
1356                                                (u16 *) & com_adr))) {
1357                         if (!((com_adr & PCI_COMMAND_IO) && 
1358                               (com_adr & PCI_COMMAND_MASTER))) {
1359                             printk("eata_dma: find_PCI, HBA has IO or BUSMASTER mode disabled\n");
1360                             continue;
1361                         }
1362                     } else
1363                         printk("eata_dma: find_PCI, error %x while reading "
1364                                "PCI_COMMAND\n", error);
1365                 } else
1366                     printk("eata_dma: find_PCI, DEVICECLASSID %x didn't match\n", 
1367                            rev_device);
1368             } else {
1369                 printk("eata_dma: find_PCI, error %x while reading PCI_CLASS_BASE\n", 
1370                        error);
1371                 continue;
1372             }
1373             
1374             if (!(error = pcibios_read_config_dword(pci_bus, pci_device_fn,
1375                                        PCI_BASE_ADDRESS_0, (int *) &base))){
1376                 
1377                 /* Check if the address is valid */
1378                 if (base & 0x01) {
1379                     base &= 0xfffffffe;
1380                     /* EISA tag there ? */
1381                     pal1 = inb(base);
1382                     pal2 = inb(base + 1);
1383                     pal3 = inb(base + 2);
1384                     if (((pal1 == DPT_ID1) && (pal2 == DPT_ID2)) ||
1385                         ((pal1 == NEC_ID1) && (pal2 == NEC_ID2) && 
1386                          (pal3 == NEC_ID3)) ||
1387                         ((pal1 == ATT_ID1) && (pal2 == ATT_ID2) && 
1388                          (pal3 == ATT_ID3)))
1389                         base += 0x08;
1390                     else
1391                         base += 0x10;   /* Now, THIS is the real address */
1392 
1393                     if (base != 0x1f8) {
1394                         /* We didn't find it in the primary search */
1395                         if (get_conf_PIO(base, buf) == TRUE) {
1396 
1397                             /* OK. We made it till here, so we can go now  
1398                              * and register it. We  only have to check and 
1399                              * eventually remove it from the EISA and ISA list 
1400                              */
1401                             DBG(DBG_PCI, printk("Registering PCI HBA\n"));
1402                             register_HBA(base, buf, tpnt, IS_PCI);
1403                             
1404                             if (base < 0x1000) {
1405                                 for (x = 0; x < MAXISA; ++x) {
1406                                     if (ISAbases[x] == base) {
1407                                         ISAbases[x] = 0;
1408                                         break;
1409                                     }
1410                                 }
1411                             } else if ((base & 0x0fff) == 0x0c88) 
1412                                 EISAbases[(base >> 12) & 0x0f] = 0;
1413                             continue;  /* break; */
1414                         } else if (check_blink_state(base) == TRUE) {
1415                             printk("eata_dma: HBA is in BLINK state.\n"
1416                                    "Consult your HBAs manual to correct this.\n");
1417                         }
1418                     }
1419                 }
1420             } else {
1421                 printk("eata_dma: error %x while reading "
1422                        "PCI_BASE_ADDRESS_0\n", error);
1423             }
1424         }
1425     } else {
1426         printk("eata_dma: No BIOS32 extensions present. This driver release "
1427                "still depends on it.\n"
1428                "          Skipping scan for PCI HBAs. \n");
1429     }
1430 #endif /* #ifndef CONFIG_PCI */
1431     return;
1432 }
1433 
1434 int eata_detect(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1435 {
1436     struct Scsi_Host *HBA_ptr;
1437     struct get_conf gc;
1438     int i;
1439     
1440     DBG((DBG_PROBE && DBG_DELAY) || DPT_DEBUG,
1441         printk("Using lots of delays to let you read the debugging output\n"));
1442 
1443     tpnt->proc_dir = &proc_scsi_eata_dma;
1444 
1445     status = scsi_init_malloc(512, GFP_ATOMIC | GFP_DMA);
1446     dma_scratch = scsi_init_malloc(1024, GFP_ATOMIC | GFP_DMA);
1447 
1448     if(status == NULL || dma_scratch == NULL) {
1449         printk("eata_dma: can't allocate enough memory to probe for hosts !\n");
1450         return(0);
1451     }
1452 
1453     dma_scratch += 4;
1454 
1455     find_PCI(&gc, tpnt);
1456     
1457     find_EISA(&gc, tpnt);
1458     
1459     find_ISA(&gc, tpnt);
1460     
1461     for (i = 0; i <= MAXIRQ; i++) { /* Now that we know what we have, we     */
1462         if (reg_IRQ[i] >= 1){       /* exchange the interrupt handler which  */
1463             free_irq(i, NULL);      /* we used for probing with the real one */
1464             request_irq(i, (void *)(eata_int_handler), SA_INTERRUPT, 
1465                         "eata_dma", NULL);
1466         }
1467     }
1468 
1469     HBA_ptr = first_HBA;
1470 
1471     if (registered_HBAs != 0) {
1472         printk("EATA (Extended Attachment) driver version: %d.%d%s"
1473                "\ndeveloped in co-operation with DPT\n"
1474                "(c) 1993-96 Michael Neuffer, mike@i-Connect.Net\n",
1475                VER_MAJOR, VER_MINOR, VER_SUB);
1476         printk("Registered HBAs:");
1477         printk("\nHBA no. Boardtype    Revis  EATA Bus  BaseIO IRQ"
1478                " DMA Ch ID Pr QS  S/G IS\n");
1479         for (i = 1; i <= registered_HBAs; i++) {
1480             printk("scsi%-2d: %.12s v%s 2.0%c %s %#.4x  %2d",
1481                    HBA_ptr->host_no, SD(HBA_ptr)->name, SD(HBA_ptr)->revision,
1482                    SD(HBA_ptr)->EATA_revision, (SD(HBA_ptr)->bustype == 'P')? 
1483                    "PCI ":(SD(HBA_ptr)->bustype == 'E')?"EISA":"ISA ",
1484                    (u32) HBA_ptr->base, HBA_ptr->irq);
1485             if(HBA_ptr->dma_channel != BUSMASTER)
1486                 printk("  %2x ", HBA_ptr->dma_channel);
1487             else
1488                 printk(" %s", "BMST");
1489             printk(" %d  %d  %c %3d %3d %c\n", 
1490                    SD(HBA_ptr)->channel+1, HBA_ptr->this_id, 
1491                    (SD(HBA_ptr)->primary == TRUE)?'Y':'N', 
1492                    HBA_ptr->can_queue, HBA_ptr->sg_tablesize, 
1493                    (SD(HBA_ptr)->immediate_support == TRUE)?'Y':'N'); 
1494             HBA_ptr = SD(HBA_ptr)->next;
1495         }
1496     } else {
1497         scsi_init_free((void *)status, 512);
1498     }
1499 
1500     scsi_init_free((void *)dma_scratch - 4, 1024);
1501 
1502     DBG(DPT_DEBUG, DELAY(12));
1503 
1504     return(registered_HBAs);
1505 }
1506 
1507 #ifdef MODULE
1508 /* Eventually this will go into an include file, but this will be later */
1509 Scsi_Host_Template driver_template = EATA_DMA;
1510 #include "scsi_module.c"
1511 #endif
1512 
1513 /*
1514  * Overrides for Emacs so that we almost follow Linus's tabbing style.
1515  * Emacs will notice this stuff at the end of the file and automatically
1516  * adjust the settings for this buffer only.  This must remain at the end
1517  * of the file.
1518  * ---------------------------------------------------------------------------
1519  * Local variables:
1520  * c-indent-level: 4
1521  * c-brace-imaginary-offset: 0
1522  * c-brace-offset: -4
1523  * c-argdecl-indent: 4
1524  * c-label-offset: -4
1525  * c-continued-statement-offset: 4
1526  * c-continued-brace-offset: 0
1527  * tab-width: 8
1528  * End:
1529  */

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