This source file includes following definitions.
- sbic_arm_write
- sbic_arm_read
- dmac_address
- acornscsi_dumpdma
- acornscsi_sbic_xfcount
- acornscsi_sbic_wait
- acornscsi_sbic_issuecmd
- acornscsi_csdelay
- acornscsi_resetcard
- print_scsi_status
- print_sbic_status
- acornscsi_dumplogline
- acornscsi_dumplog
- acornscsi_target
- acornscsi_cmdtype
- acornscsi_datadirection
- acornscsi_getperiod
- round_period
- calc_sync_xfer
- acornscsi_kick
- acornscsi_done
- acornscsi_data_updateptr
- acornscsi_data_read
- acornscsi_data_write
- acornscsi_dma_stop
- acornscsi_dma_setup
- acornscsi_dma_cleanup
- acornscsi_dma_intr
- acornscsi_dma_xfer
- acornscsi_dma_adjust
- acornscsi_write_pio
- acornscsi_sendcommand
- acornscsi_sendmessage
- acornscsi_readstatusbyte
- acornscsi_readmessagebyte
- acornscsi_message
- acornscsi_buildmessages
- acornscsi_starttransfer
- acornscsi_reconnect
- acornscsi_reconnect_finish
- acornscsi_disconnect_unexpected
- acornscsi_abortcmd
- acornscsi_sbicintr
- acornscsi_intr
- acornscsi_queuecmd_lck
- DEF_SCSI_QCMD
- acornscsi_do_abort
- acornscsi_abort
- acornscsi_host_reset
- acornscsi_info
- acornscsi_show_info
- acornscsi_probe
- acornscsi_remove
- acornscsi_init
- acornscsi_exit
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 #define DEBUG_NO_WRITE  1
  29 #define DEBUG_QUEUES    2
  30 #define DEBUG_DMA       4
  31 #define DEBUG_ABORT     8
  32 #define DEBUG_DISCON    16
  33 #define DEBUG_CONNECT   32
  34 #define DEBUG_PHASES    64
  35 #define DEBUG_WRITE     128
  36 #define DEBUG_LINK      256
  37 #define DEBUG_MESSAGES  512
  38 #define DEBUG_RESET     1024
  39 #define DEBUG_ALL       (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
  40                          DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
  41                          DEBUG_DMA|DEBUG_QUEUES)
  42 
  43 
  44 
  45 
  46 
  47 
  48 
  49 
  50 
  51 
  52 
  53 
  54 
  55 
  56 
  57 
  58 
  59 
  60 #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  61 
  62 
  63 
  64 
  65 
  66 
  67 
  68 
  69 
  70 #define SDTR_SIZE       12
  71 #define SDTR_PERIOD     125
  72 #define DEFAULT_PERIOD  500
  73 
  74 
  75 
  76 
  77 
  78 
  79 
  80 
  81 #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
  82 
  83 #define NO_WRITE 0xFE
  84 
  85 
  86 
  87 
  88 
  89 
  90 
  91 #define TIMEOUT_TIME 10
  92 
  93 
  94 
  95 
  96 #undef CONFIG_ACORNSCSI_CONSTANTS
  97 
  98 
  99 
 100 
 101 #define USE_DMAC
 102 
 103 
 104 
 105 
 106 
 107 #ifdef DEBUG_TARGET
 108 #define DBG(cmd,xxx...) \
 109   if (cmd->device->id == DEBUG_TARGET) { \
 110     xxx; \
 111   }
 112 #else
 113 #define DBG(cmd,xxx...) xxx
 114 #endif
 115 
 116 #include <linux/module.h>
 117 #include <linux/kernel.h>
 118 #include <linux/string.h>
 119 #include <linux/signal.h>
 120 #include <linux/errno.h>
 121 #include <linux/proc_fs.h>
 122 #include <linux/ioport.h>
 123 #include <linux/blkdev.h>
 124 #include <linux/delay.h>
 125 #include <linux/interrupt.h>
 126 #include <linux/init.h>
 127 #include <linux/bitops.h>
 128 #include <linux/stringify.h>
 129 #include <linux/io.h>
 130 
 131 #include <asm/ecard.h>
 132 
 133 #include "../scsi.h"
 134 #include <scsi/scsi_dbg.h>
 135 #include <scsi/scsi_host.h>
 136 #include <scsi/scsi_transport_spi.h>
 137 #include "acornscsi.h"
 138 #include "msgqueue.h"
 139 #include "scsi.h"
 140 
 141 #include <scsi/scsicam.h>
 142 
 143 #define VER_MAJOR 2
 144 #define VER_MINOR 0
 145 #define VER_PATCH 6
 146 
 147 #ifndef ABORT_TAG
 148 #define ABORT_TAG 0xd
 149 #else
 150 #error "Yippee!  ABORT TAG is now defined!  Remove this error!"
 151 #endif
 152 
 153 #ifdef USE_DMAC
 154 
 155 
 156  
 157 #define INIT_DEVCON0    (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
 158 #define INIT_DEVCON1    (DEVCON1_BHLD)
 159 #define DMAC_READ       (MODECON_READ)
 160 #define DMAC_WRITE      (MODECON_WRITE)
 161 #define INIT_SBICDMA    (CTRL_DMABURST)
 162 
 163 #define scsi_xferred    have_data_in
 164 
 165 
 166 
 167 
 168 #define DMAC_BUFFER_SIZE        65536
 169 #endif
 170 
 171 #define STATUS_BUFFER_TO_PRINT  24
 172 
 173 unsigned int sdtr_period = SDTR_PERIOD;
 174 unsigned int sdtr_size   = SDTR_SIZE;
 175 
 176 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
 177                            unsigned int result);
 178 static int acornscsi_reconnect_finish(AS_Host *host);
 179 static void acornscsi_dma_cleanup(AS_Host *host);
 180 static void acornscsi_abortcmd(AS_Host *host, unsigned char tag);
 181 
 182 
 183 
 184 
 185 
 186 
 187 #define SBIC_REGIDX     0x2000
 188 #define SBIC_REGVAL     0x2004
 189 #define DMAC_OFFSET     0x3000
 190 
 191 
 192 #define INT_REG         0x2000
 193 #define PAGE_REG        0x3000
 194 
 195 static inline void sbic_arm_write(AS_Host *host, unsigned int reg, unsigned int value)
 196 {
 197     writeb(reg, host->base + SBIC_REGIDX);
 198     writeb(value, host->base + SBIC_REGVAL);
 199 }
 200 
 201 static inline int sbic_arm_read(AS_Host *host, unsigned int reg)
 202 {
 203     if(reg == SBIC_ASR)
 204            return readl(host->base + SBIC_REGIDX) & 255;
 205     writeb(reg, host->base + SBIC_REGIDX);
 206     return readl(host->base + SBIC_REGVAL) & 255;
 207 }
 208 
 209 #define sbic_arm_writenext(host, val)   writeb((val), (host)->base + SBIC_REGVAL)
 210 #define sbic_arm_readnext(host)         readb((host)->base + SBIC_REGVAL)
 211 
 212 #ifdef USE_DMAC
 213 #define dmac_read(host,reg) \
 214         readb((host)->base + DMAC_OFFSET + ((reg) << 2))
 215 
 216 #define dmac_write(host,reg,value) \
 217         ({ writeb((value), (host)->base + DMAC_OFFSET + ((reg) << 2)); })
 218 
 219 #define dmac_clearintr(host)    writeb(0, (host)->fast + INT_REG)
 220 
 221 static inline unsigned int dmac_address(AS_Host *host)
 222 {
 223     return dmac_read(host, DMAC_TXADRHI) << 16 |
 224            dmac_read(host, DMAC_TXADRMD) << 8 |
 225            dmac_read(host, DMAC_TXADRLO);
 226 }
 227 
 228 static
 229 void acornscsi_dumpdma(AS_Host *host, char *where)
 230 {
 231         unsigned int mode, addr, len;
 232 
 233         mode = dmac_read(host, DMAC_MODECON);
 234         addr = dmac_address(host);
 235         len  = dmac_read(host, DMAC_TXCNTHI) << 8 |
 236                dmac_read(host, DMAC_TXCNTLO);
 237 
 238         printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
 239                 host->host->host_no, where,
 240                 mode, addr, (len + 1) & 0xffff,
 241                 dmac_read(host, DMAC_MASKREG));
 242 
 243         printk("DMA @%06x, ", host->dma.start_addr);
 244         printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
 245                 host->scsi.SCp.this_residual);
 246         printk("DT @+%04x ST @+%04x", host->dma.transferred,
 247                 host->scsi.SCp.scsi_xferred);
 248         printk("\n");
 249 }
 250 #endif
 251 
 252 static
 253 unsigned long acornscsi_sbic_xfcount(AS_Host *host)
 254 {
 255     unsigned long length;
 256 
 257     length = sbic_arm_read(host, SBIC_TRANSCNTH) << 16;
 258     length |= sbic_arm_readnext(host) << 8;
 259     length |= sbic_arm_readnext(host);
 260 
 261     return length;
 262 }
 263 
 264 static int
 265 acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
 266 {
 267         int asr;
 268 
 269         do {
 270                 asr = sbic_arm_read(host, SBIC_ASR);
 271 
 272                 if ((asr & stat_mask) == stat)
 273                         return 0;
 274 
 275                 udelay(1);
 276         } while (--timeout);
 277 
 278         printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
 279 
 280         return -1;
 281 }
 282 
 283 static
 284 int acornscsi_sbic_issuecmd(AS_Host *host, int command)
 285 {
 286     if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
 287         return -1;
 288 
 289     sbic_arm_write(host, SBIC_CMND, command);
 290 
 291     return 0;
 292 }
 293 
 294 static void
 295 acornscsi_csdelay(unsigned int cs)
 296 {
 297     unsigned long target_jiffies, flags;
 298 
 299     target_jiffies = jiffies + 1 + cs * HZ / 100;
 300 
 301     local_save_flags(flags);
 302     local_irq_enable();
 303 
 304     while (time_before(jiffies, target_jiffies)) barrier();
 305 
 306     local_irq_restore(flags);
 307 }
 308 
 309 static
 310 void acornscsi_resetcard(AS_Host *host)
 311 {
 312     unsigned int i, timeout;
 313 
 314     
 315     host->card.page_reg = 0x80;
 316     writeb(host->card.page_reg, host->fast + PAGE_REG);
 317 
 318     
 319     acornscsi_csdelay(3);
 320 
 321     host->card.page_reg = 0;
 322     writeb(host->card.page_reg, host->fast + PAGE_REG);
 323 
 324     
 325 
 326 
 327     timeout = 1000;
 328     do {
 329         if (readb(host->fast + INT_REG) & 8)
 330             break;
 331         udelay(1);
 332     } while (--timeout);
 333 
 334     if (timeout == 0)
 335         printk("scsi%d: timeout while resetting card\n",
 336                 host->host->host_no);
 337 
 338     sbic_arm_read(host, SBIC_ASR);
 339     sbic_arm_read(host, SBIC_SSR);
 340 
 341     
 342     sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
 343     sbic_arm_write(host, SBIC_CMND, CMND_RESET);
 344 
 345     
 346 
 347 
 348     timeout = 1000;
 349     do {
 350         if (readb(host->fast + INT_REG) & 8)
 351             break;
 352         udelay(1);
 353     } while (--timeout);
 354 
 355     if (timeout == 0)
 356         printk("scsi%d: timeout while resetting card\n",
 357                 host->host->host_no);
 358 
 359     sbic_arm_read(host, SBIC_ASR);
 360     if (sbic_arm_read(host, SBIC_SSR) != 0x01)
 361         printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
 362                 host->host->host_no);
 363 
 364     sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
 365     sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
 366     sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
 367     sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
 368 
 369     host->card.page_reg = 0x40;
 370     writeb(host->card.page_reg, host->fast + PAGE_REG);
 371 
 372     
 373     dmac_write(host, DMAC_INIT, 0);
 374 #ifdef USE_DMAC
 375     dmac_write(host, DMAC_INIT, INIT_8BIT);
 376     dmac_write(host, DMAC_CHANNEL, CHANNEL_0);
 377     dmac_write(host, DMAC_DEVCON0, INIT_DEVCON0);
 378     dmac_write(host, DMAC_DEVCON1, INIT_DEVCON1);
 379 #endif
 380 
 381     host->SCpnt = NULL;
 382     host->scsi.phase = PHASE_IDLE;
 383     host->scsi.disconnectable = 0;
 384 
 385     memset(host->busyluns, 0, sizeof(host->busyluns));
 386 
 387     for (i = 0; i < 8; i++) {
 388         host->device[i].sync_state = SYNC_NEGOCIATE;
 389         host->device[i].disconnect_ok = 1;
 390     }
 391 
 392     
 393     acornscsi_csdelay(25);
 394 }
 395 
 396 
 397 
 398 
 399 #ifdef CONFIG_ACORNSCSI_CONSTANTS
 400 static char *acornscsi_interrupttype[] = {
 401   "rst",  "suc",  "p/a",  "3",
 402   "term", "5",    "6",    "7",
 403   "serv", "9",    "a",    "b",
 404   "c",    "d",    "e",    "f"
 405 };
 406 
 407 static signed char acornscsi_map[] = {
 408   0,  1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 409  -1,  2, -1, -1,  -1, -1,  3, -1,   4,  5,  6,  7,   8,  9, 10, 11,
 410  12, 13, 14, -1,  -1, -1, -1, -1,   4,  5,  6,  7,   8,  9, 10, 11,
 411  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 412  15, 16, 17, 18,  19, -1, -1, 20,   4,  5,  6,  7,   8,  9, 10, 11,
 413  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 414  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 415  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 416  21, 22, -1, -1,  -1, 23, -1, -1,   4,  5,  6,  7,   8,  9, 10, 11,
 417  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 418  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 419  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 420  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 421  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 422  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
 423  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1
 424 };      
 425 
 426 static char *acornscsi_interruptcode[] = {
 427     
 428     "reset - normal mode",      
 429     "reset - advanced mode",    
 430 
 431     
 432     "sel",                      
 433     "sel+xfer",                 
 434     "data-out",                 
 435     "data-in",                  
 436     "cmd",                      
 437     "stat",                     
 438     "??-out",                   
 439     "??-in",                    
 440     "msg-out",                  
 441     "msg-in",                   
 442 
 443     
 444     "/ACK asserted",            
 445     "save-data-ptr",            
 446     "{re}sel",                  
 447 
 448     
 449     "inv cmd",                  
 450     "unexpected disconnect",    
 451     "sel timeout",              
 452     "P err",                    
 453     "P err+ATN",                
 454     "bad status byte",          
 455 
 456     
 457     "resel, no id",             
 458     "resel",                    
 459     "discon",                   
 460 };
 461 
 462 static
 463 void print_scsi_status(unsigned int ssr)
 464 {
 465     if (acornscsi_map[ssr] != -1)
 466         printk("%s:%s",
 467                 acornscsi_interrupttype[(ssr >> 4)],
 468                 acornscsi_interruptcode[acornscsi_map[ssr]]);
 469     else
 470         printk("%X:%X", ssr >> 4, ssr & 0x0f);    
 471 }    
 472 #endif
 473 
 474 static
 475 void print_sbic_status(int asr, int ssr, int cmdphase)
 476 {
 477 #ifdef CONFIG_ACORNSCSI_CONSTANTS
 478     printk("sbic: %c%c%c%c%c%c ",
 479             asr & ASR_INT ? 'I' : 'i',
 480             asr & ASR_LCI ? 'L' : 'l',
 481             asr & ASR_BSY ? 'B' : 'b',
 482             asr & ASR_CIP ? 'C' : 'c',
 483             asr & ASR_PE  ? 'P' : 'p',
 484             asr & ASR_DBR ? 'D' : 'd');
 485     printk("scsi: ");
 486     print_scsi_status(ssr);
 487     printk(" ph %02X\n", cmdphase);
 488 #else
 489     printk("sbic: %02X scsi: %X:%X ph: %02X\n",
 490             asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
 491 #endif
 492 }
 493 
 494 static void
 495 acornscsi_dumplogline(AS_Host *host, int target, int line)
 496 {
 497         unsigned long prev;
 498         signed int ptr;
 499 
 500         ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
 501         if (ptr < 0)
 502                 ptr += STATUS_BUFFER_SIZE;
 503 
 504         printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
 505                 line == 0 ? "ph" : line == 1 ? "ssr" : "int");
 506 
 507         prev = host->status[target][ptr].when;
 508 
 509         for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
 510                 unsigned long time_diff;
 511 
 512                 if (!host->status[target][ptr].when)
 513                         continue;
 514 
 515                 switch (line) {
 516                 case 0:
 517                         printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
 518                                          host->status[target][ptr].ph);
 519                         break;
 520 
 521                 case 1:
 522                         printk(" %02X", host->status[target][ptr].ssr);
 523                         break;
 524 
 525                 case 2:
 526                         time_diff = host->status[target][ptr].when - prev;
 527                         prev = host->status[target][ptr].when;
 528                         if (time_diff == 0)
 529                                 printk("==^");
 530                         else if (time_diff >= 100)
 531                                 printk("   ");
 532                         else
 533                                 printk(" %02ld", time_diff);
 534                         break;
 535                 }
 536         }
 537 
 538         printk("\n");
 539 }
 540 
 541 static
 542 void acornscsi_dumplog(AS_Host *host, int target)
 543 {
 544     do {
 545         acornscsi_dumplogline(host, target, 0);
 546         acornscsi_dumplogline(host, target, 1);
 547         acornscsi_dumplogline(host, target, 2);
 548 
 549         if (target == 8)
 550             break;
 551 
 552         target = 8;
 553     } while (1);
 554 }
 555 
 556 static
 557 char acornscsi_target(AS_Host *host)
 558 {
 559         if (host->SCpnt)
 560                 return '0' + host->SCpnt->device->id;
 561         return 'H';
 562 }
 563 
 564 
 565 
 566 
 567 
 568 
 569 
 570 
 571 
 572 static inline
 573 cmdtype_t acornscsi_cmdtype(int command)
 574 {
 575     switch (command) {
 576     case WRITE_6:  case WRITE_10:  case WRITE_12:
 577         return CMD_WRITE;
 578     case READ_6:   case READ_10:   case READ_12:
 579         return CMD_READ;
 580     default:
 581         return CMD_MISC;
 582     }
 583 }
 584 
 585 
 586 
 587 
 588 
 589 
 590 
 591 
 592 
 593 static
 594 datadir_t acornscsi_datadirection(int command)
 595 {
 596     switch (command) {
 597     case CHANGE_DEFINITION:     case COMPARE:           case COPY:
 598     case COPY_VERIFY:           case LOG_SELECT:        case MODE_SELECT:
 599     case MODE_SELECT_10:        case SEND_DIAGNOSTIC:   case WRITE_BUFFER:
 600     case FORMAT_UNIT:           case REASSIGN_BLOCKS:   case RESERVE:
 601     case SEARCH_EQUAL:          case SEARCH_HIGH:       case SEARCH_LOW:
 602     case WRITE_6:               case WRITE_10:          case WRITE_VERIFY:
 603     case UPDATE_BLOCK:          case WRITE_LONG:        case WRITE_SAME:
 604     case SEARCH_HIGH_12:        case SEARCH_EQUAL_12:   case SEARCH_LOW_12:
 605     case WRITE_12:              case WRITE_VERIFY_12:   case SET_WINDOW:
 606     case MEDIUM_SCAN:           case SEND_VOLUME_TAG:   case 0xea:
 607         return DATADIR_OUT;
 608     default:
 609         return DATADIR_IN;
 610     }
 611 }
 612 
 613 
 614 
 615 
 616 
 617 
 618 static struct sync_xfer_tbl {
 619     unsigned int period_ns;
 620     unsigned char reg_value;
 621 } sync_xfer_table[] = {
 622     {   1, 0x20 },    { 249, 0x20 },    { 374, 0x30 },
 623     { 499, 0x40 },    { 624, 0x50 },    { 749, 0x60 },
 624     { 874, 0x70 },    { 999, 0x00 },    {   0,    0 }
 625 };
 626 
 627 
 628 
 629 
 630 
 631 
 632 
 633 static
 634 int acornscsi_getperiod(unsigned char syncxfer)
 635 {
 636     int i;
 637 
 638     syncxfer &= 0xf0;
 639     if (syncxfer == 0x10)
 640         syncxfer = 0;
 641 
 642     for (i = 1; sync_xfer_table[i].period_ns; i++)
 643         if (syncxfer == sync_xfer_table[i].reg_value)
 644             return sync_xfer_table[i].period_ns;
 645     return 0;
 646 }
 647 
 648 
 649 
 650 
 651 
 652 
 653 
 654 
 655 static inline
 656 int round_period(unsigned int period)
 657 {
 658     int i;
 659 
 660     for (i = 1; sync_xfer_table[i].period_ns; i++) {
 661         if ((period <= sync_xfer_table[i].period_ns) &&
 662             (period > sync_xfer_table[i - 1].period_ns))
 663             return i;
 664     }
 665     return 7;
 666 }
 667 
 668 
 669 
 670 
 671 
 672 
 673 
 674 
 675 
 676 static
 677 unsigned char __maybe_unused calc_sync_xfer(unsigned int period,
 678                                             unsigned int offset)
 679 {
 680     return sync_xfer_table[round_period(period)].reg_value |
 681                 ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
 682 }
 683 
 684 
 685 
 686 
 687 
 688 
 689 
 690 
 691 
 692 
 693 
 694 static
 695 intr_ret_t acornscsi_kick(AS_Host *host)
 696 {
 697     int from_queue = 0;
 698     struct scsi_cmnd *SCpnt;
 699 
 700     
 701     SCpnt = host->origSCpnt;
 702     host->origSCpnt = NULL;
 703 
 704     
 705     if (!SCpnt) {
 706         SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
 707         if (!SCpnt)
 708             return INTR_IDLE;
 709 
 710         from_queue = 1;
 711     }
 712 
 713     if (host->scsi.disconnectable && host->SCpnt) {
 714         queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
 715         host->scsi.disconnectable = 0;
 716 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
 717         DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
 718                 host->host->host_no, acornscsi_target(host)));
 719 #endif
 720         host->SCpnt = NULL;
 721     }
 722 
 723     
 724 
 725 
 726 
 727     if (!(sbic_arm_read(host, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
 728         sbic_arm_write(host, SBIC_DESTID, SCpnt->device->id);
 729         sbic_arm_write(host, SBIC_CMND, CMND_SELWITHATN);
 730     }
 731 
 732     
 733 
 734 
 735 
 736     host->scsi.phase = PHASE_CONNECTING;
 737     host->SCpnt = SCpnt;
 738     host->scsi.SCp = SCpnt->SCp;
 739     host->dma.xfer_setup = 0;
 740     host->dma.xfer_required = 0;
 741     host->dma.xfer_done = 0;
 742 
 743 #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
 744     DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
 745             host->host->host_no, '0' + SCpnt->device->id,
 746             SCpnt->cmnd[0]));
 747 #endif
 748 
 749     if (from_queue) {
 750 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
 751         
 752 
 753 
 754         if (SCpnt->device->simple_tags) {
 755             SCpnt->device->current_tag += 1;
 756             if (SCpnt->device->current_tag == 0)
 757                 SCpnt->device->current_tag = 1;
 758             SCpnt->tag = SCpnt->device->current_tag;
 759         } else
 760 #endif
 761             set_bit(SCpnt->device->id * 8 +
 762                     (u8)(SCpnt->device->lun & 0x07), host->busyluns);
 763 
 764         host->stats.removes += 1;
 765 
 766         switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
 767         case CMD_WRITE:
 768             host->stats.writes += 1;
 769             break;
 770         case CMD_READ:
 771             host->stats.reads += 1;
 772             break;
 773         case CMD_MISC:
 774             host->stats.miscs += 1;
 775             break;
 776         }
 777     }
 778 
 779     return INTR_PROCESSING;
 780 }    
 781 
 782 
 783 
 784 
 785 
 786 
 787 
 788 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
 789                            unsigned int result)
 790 {
 791         struct scsi_cmnd *SCpnt = *SCpntp;
 792 
 793     
 794     sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
 795 
 796     host->stats.fins += 1;
 797 
 798     if (SCpnt) {
 799         *SCpntp = NULL;
 800 
 801         acornscsi_dma_cleanup(host);
 802 
 803         SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
 804 
 805         
 806 
 807 
 808 
 809 
 810 
 811 
 812 
 813         if (result == DID_OK) {
 814                 int xfer_warn = 0;
 815 
 816                 if (SCpnt->underflow == 0) {
 817                         if (host->scsi.SCp.ptr &&
 818                             acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
 819                                 xfer_warn = 1;
 820                 } else {
 821                         if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
 822                             host->scsi.SCp.scsi_xferred != host->dma.transferred)
 823                                 xfer_warn = 1;
 824                 }
 825 
 826                 
 827 
 828 
 829 
 830 
 831 
 832 
 833 
 834 
 835 
 836 
 837 
 838                 if (host->dma.xfer_done)
 839                         xfer_warn = 0;
 840 
 841                 if (xfer_warn) {
 842                     switch (status_byte(SCpnt->result)) {
 843                     case CHECK_CONDITION:
 844                     case COMMAND_TERMINATED:
 845                     case BUSY:
 846                     case QUEUE_FULL:
 847                     case RESERVATION_CONFLICT:
 848                         break;
 849 
 850                     default:
 851                         scmd_printk(KERN_ERR, SCpnt,
 852                                     "incomplete data transfer detected: "
 853                                     "result=%08X", SCpnt->result);
 854                         scsi_print_command(SCpnt);
 855                         acornscsi_dumpdma(host, "done");
 856                         acornscsi_dumplog(host, SCpnt->device->id);
 857                         set_host_byte(SCpnt, DID_ERROR);
 858                     }
 859                 }
 860         }
 861 
 862         if (!SCpnt->scsi_done)
 863             panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
 864 
 865         clear_bit(SCpnt->device->id * 8 +
 866                   (u8)(SCpnt->device->lun & 0x7), host->busyluns);
 867 
 868         SCpnt->scsi_done(SCpnt);
 869     } else
 870         printk("scsi%d: null command in acornscsi_done", host->host->host_no);
 871 
 872     host->scsi.phase = PHASE_IDLE;
 873 }
 874 
 875 
 876 
 877 
 878 
 879 
 880 
 881 
 882 static
 883 void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
 884 {
 885     SCp->ptr += length;
 886     SCp->this_residual -= length;
 887 
 888     if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
 889         host->dma.xfer_done = 1;
 890 }
 891 
 892 
 893 
 894 
 895 
 896 
 897 
 898 
 899 
 900 
 901 
 902 static
 903 void acornscsi_data_read(AS_Host *host, char *ptr,
 904                                  unsigned int start_addr, unsigned int length)
 905 {
 906     extern void __acornscsi_in(void __iomem *, char *buf, int len);
 907     unsigned int page, offset, len = length;
 908 
 909     page = (start_addr >> 12);
 910     offset = start_addr & ((1 << 12) - 1);
 911 
 912     writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
 913 
 914     while (len > 0) {
 915         unsigned int this_len;
 916 
 917         if (len + offset > (1 << 12))
 918             this_len = (1 << 12) - offset;
 919         else
 920             this_len = len;
 921 
 922         __acornscsi_in(host->base + (offset << 1), ptr, this_len);
 923 
 924         offset += this_len;
 925         ptr += this_len;
 926         len -= this_len;
 927 
 928         if (offset == (1 << 12)) {
 929             offset = 0;
 930             page ++;
 931             writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
 932         }
 933     }
 934     writeb(host->card.page_reg, host->fast + PAGE_REG);
 935 }
 936 
 937 
 938 
 939 
 940 
 941 
 942 
 943 
 944 
 945 
 946 
 947 static
 948 void acornscsi_data_write(AS_Host *host, char *ptr,
 949                                  unsigned int start_addr, unsigned int length)
 950 {
 951     extern void __acornscsi_out(void __iomem *, char *buf, int len);
 952     unsigned int page, offset, len = length;
 953 
 954     page = (start_addr >> 12);
 955     offset = start_addr & ((1 << 12) - 1);
 956 
 957     writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
 958 
 959     while (len > 0) {
 960         unsigned int this_len;
 961 
 962         if (len + offset > (1 << 12))
 963             this_len = (1 << 12) - offset;
 964         else
 965             this_len = len;
 966 
 967         __acornscsi_out(host->base + (offset << 1), ptr, this_len);
 968 
 969         offset += this_len;
 970         ptr += this_len;
 971         len -= this_len;
 972 
 973         if (offset == (1 << 12)) {
 974             offset = 0;
 975             page ++;
 976             writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
 977         }
 978     }
 979     writeb(host->card.page_reg, host->fast + PAGE_REG);
 980 }
 981 
 982 
 983 
 984 
 985 #ifdef USE_DMAC
 986 
 987 
 988 
 989 
 990 
 991 
 992 
 993 static inline
 994 void acornscsi_dma_stop(AS_Host *host)
 995 {
 996     dmac_write(host, DMAC_MASKREG, MASK_ON);
 997     dmac_clearintr(host);
 998 
 999 #if (DEBUG & DEBUG_DMA)
1000     DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
1001 #endif
1002 }
1003 
1004 
1005 
1006 
1007 
1008 
1009 
1010 
1011 
1012 static
1013 void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1014 {
1015     unsigned int address, length, mode;
1016 
1017     host->dma.direction = direction;
1018 
1019     dmac_write(host, DMAC_MASKREG, MASK_ON);
1020 
1021     if (direction == DMA_OUT) {
1022 #if (DEBUG & DEBUG_NO_WRITE)
1023         if (NO_WRITE & (1 << host->SCpnt->device->id)) {
1024             printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1025                     host->host->host_no, acornscsi_target(host));
1026             return;
1027         }
1028 #endif
1029         mode = DMAC_WRITE;
1030     } else
1031         mode = DMAC_READ;
1032 
1033     
1034 
1035 
1036     length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1037     if (length) {
1038         host->dma.start_addr = address = host->dma.free_addr;
1039         host->dma.free_addr = (host->dma.free_addr + length) &
1040                                 (DMAC_BUFFER_SIZE - 1);
1041 
1042         
1043 
1044 
1045         if (direction == DMA_OUT)
1046             acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1047                                 length);
1048 
1049         length -= 1;
1050         dmac_write(host, DMAC_TXCNTLO, length);
1051         dmac_write(host, DMAC_TXCNTHI, length >> 8);
1052         dmac_write(host, DMAC_TXADRLO, address);
1053         dmac_write(host, DMAC_TXADRMD, address >> 8);
1054         dmac_write(host, DMAC_TXADRHI, 0);
1055         dmac_write(host, DMAC_MODECON, mode);
1056         dmac_write(host, DMAC_MASKREG, MASK_OFF);
1057 
1058 #if (DEBUG & DEBUG_DMA)
1059         DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1060 #endif
1061         host->dma.xfer_setup = 1;
1062     }
1063 }
1064 
1065 
1066 
1067 
1068 
1069 
1070 
1071 
1072 
1073 static
1074 void acornscsi_dma_cleanup(AS_Host *host)
1075 {
1076     dmac_write(host, DMAC_MASKREG, MASK_ON);
1077     dmac_clearintr(host);
1078 
1079     
1080 
1081 
1082     if (host->dma.xfer_required) {
1083         host->dma.xfer_required = 0;
1084         if (host->dma.direction == DMA_IN)
1085             acornscsi_data_read(host, host->dma.xfer_ptr,
1086                                  host->dma.xfer_start, host->dma.xfer_length);
1087     }
1088 
1089     
1090 
1091 
1092     if (host->dma.xfer_setup) {
1093         unsigned int transferred;
1094 
1095         host->dma.xfer_setup = 0;
1096 
1097 #if (DEBUG & DEBUG_DMA)
1098         DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1099 #endif
1100 
1101         
1102 
1103 
1104         transferred = dmac_address(host) - host->dma.start_addr;
1105         host->dma.transferred += transferred;
1106 
1107         if (host->dma.direction == DMA_IN)
1108             acornscsi_data_read(host, host->scsi.SCp.ptr,
1109                                  host->dma.start_addr, transferred);
1110 
1111         
1112 
1113 
1114         acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1115 #if (DEBUG & DEBUG_DMA)
1116         DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1117 #endif
1118     }
1119 }
1120 
1121 
1122 
1123 
1124 
1125 
1126 
1127 
1128 
1129 
1130 
1131 static
1132 void acornscsi_dma_intr(AS_Host *host)
1133 {
1134     unsigned int address, length, transferred;
1135 
1136 #if (DEBUG & DEBUG_DMA)
1137     DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1138 #endif
1139 
1140     dmac_write(host, DMAC_MASKREG, MASK_ON);
1141     dmac_clearintr(host);
1142 
1143     
1144 
1145 
1146     transferred = dmac_address(host) - host->dma.start_addr;
1147     host->dma.transferred += transferred;
1148 
1149     
1150 
1151 
1152     if (host->dma.direction == DMA_IN) {
1153         host->dma.xfer_start = host->dma.start_addr;
1154         host->dma.xfer_length = transferred;
1155         host->dma.xfer_ptr = host->scsi.SCp.ptr;
1156         host->dma.xfer_required = 1;
1157     }
1158 
1159     acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1160 
1161     
1162 
1163 
1164     length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1165     if (length) {
1166         host->dma.start_addr = address = host->dma.free_addr;
1167         host->dma.free_addr = (host->dma.free_addr + length) &
1168                                 (DMAC_BUFFER_SIZE - 1);
1169 
1170         
1171 
1172 
1173         if (host->dma.direction == DMA_OUT)
1174             acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1175                                 length);
1176 
1177         length -= 1;
1178         dmac_write(host, DMAC_TXCNTLO, length);
1179         dmac_write(host, DMAC_TXCNTHI, length >> 8);
1180         dmac_write(host, DMAC_TXADRLO, address);
1181         dmac_write(host, DMAC_TXADRMD, address >> 8);
1182         dmac_write(host, DMAC_TXADRHI, 0);
1183         dmac_write(host, DMAC_MASKREG, MASK_OFF);
1184 
1185 #if (DEBUG & DEBUG_DMA)
1186         DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1187 #endif
1188     } else {
1189         host->dma.xfer_setup = 0;
1190 #if 0
1191         
1192 
1193 
1194 
1195 
1196 
1197         if (dmac_read(host, DMAC_STATUS) & STATUS_RQ0) {
1198             acornscsi_abortcmd(host, host->SCpnt->tag);
1199 
1200             dmac_write(host, DMAC_TXCNTLO, 0);
1201             dmac_write(host, DMAC_TXCNTHI, 0);
1202             dmac_write(host, DMAC_TXADRLO, 0);
1203             dmac_write(host, DMAC_TXADRMD, 0);
1204             dmac_write(host, DMAC_TXADRHI, 0);
1205             dmac_write(host, DMAC_MASKREG, MASK_OFF);
1206         }
1207 #endif
1208     }
1209 }
1210 
1211 
1212 
1213 
1214 
1215 
1216 static
1217 void acornscsi_dma_xfer(AS_Host *host)
1218 {
1219     host->dma.xfer_required = 0;
1220 
1221     if (host->dma.direction == DMA_IN)
1222         acornscsi_data_read(host, host->dma.xfer_ptr,
1223                                 host->dma.xfer_start, host->dma.xfer_length);
1224 }
1225 
1226 
1227 
1228 
1229 
1230 
1231 
1232 static
1233 void acornscsi_dma_adjust(AS_Host *host)
1234 {
1235     if (host->dma.xfer_setup) {
1236         signed long transferred;
1237 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1238         DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1239 #endif
1240         
1241 
1242 
1243 
1244 
1245 
1246 
1247 
1248 
1249 
1250 
1251         transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1252         if (transferred < 0)
1253             printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1254                     host->host->host_no, acornscsi_target(host), transferred);
1255         else if (transferred == 0)
1256             host->dma.xfer_setup = 0;
1257         else {
1258             transferred += host->dma.start_addr;
1259             dmac_write(host, DMAC_TXADRLO, transferred);
1260             dmac_write(host, DMAC_TXADRMD, transferred >> 8);
1261             dmac_write(host, DMAC_TXADRHI, transferred >> 16);
1262 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1263             DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1264 #endif
1265         }
1266     }
1267 }
1268 #endif
1269 
1270 
1271 
1272 
1273 static int
1274 acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1275 {
1276         unsigned int asr, timeout = max_timeout;
1277         int my_ptr = *ptr;
1278 
1279         while (my_ptr < len) {
1280                 asr = sbic_arm_read(host, SBIC_ASR);
1281 
1282                 if (asr & ASR_DBR) {
1283                         timeout = max_timeout;
1284 
1285                         sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
1286                 } else if (asr & ASR_INT)
1287                         break;
1288                 else if (--timeout == 0)
1289                         break;
1290                 udelay(1);
1291         }
1292 
1293         *ptr = my_ptr;
1294 
1295         return (timeout == 0) ? -1 : 0;
1296 }
1297 
1298 
1299 
1300 
1301 
1302 
1303 static void
1304 acornscsi_sendcommand(AS_Host *host)
1305 {
1306         struct scsi_cmnd *SCpnt = host->SCpnt;
1307 
1308     sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1309     sbic_arm_writenext(host, 0);
1310     sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1311 
1312     acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1313 
1314     if (acornscsi_write_pio(host, SCpnt->cmnd,
1315         (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1316         printk("scsi%d: timeout while sending command\n", host->host->host_no);
1317 
1318     host->scsi.phase = PHASE_COMMAND;
1319 }
1320 
1321 static
1322 void acornscsi_sendmessage(AS_Host *host)
1323 {
1324     unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1325     unsigned int msgnr;
1326     struct message *msg;
1327 
1328 #if (DEBUG & DEBUG_MESSAGES)
1329     printk("scsi%d.%c: sending message ",
1330             host->host->host_no, acornscsi_target(host));
1331 #endif
1332 
1333     switch (message_length) {
1334     case 0:
1335         acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1336 
1337         acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1338 
1339         sbic_arm_write(host, SBIC_DATA, NOP);
1340 
1341         host->scsi.last_message = NOP;
1342 #if (DEBUG & DEBUG_MESSAGES)
1343         printk("NOP");
1344 #endif
1345         break;
1346 
1347     case 1:
1348         acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1349         msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1350 
1351         acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1352 
1353         sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
1354 
1355         host->scsi.last_message = msg->msg[0];
1356 #if (DEBUG & DEBUG_MESSAGES)
1357         spi_print_msg(msg->msg);
1358 #endif
1359         break;
1360 
1361     default:
1362         
1363 
1364 
1365 
1366 
1367 
1368 
1369 
1370         sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1371         sbic_arm_writenext(host, 0);
1372         sbic_arm_writenext(host, message_length);
1373         acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1374 
1375         msgnr = 0;
1376         while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1377             unsigned int i;
1378 #if (DEBUG & DEBUG_MESSAGES)
1379             spi_print_msg(msg);
1380 #endif
1381             i = 0;
1382             if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1383                 printk("scsi%d: timeout while sending message\n", host->host->host_no);
1384 
1385             host->scsi.last_message = msg->msg[0];
1386             if (msg->msg[0] == EXTENDED_MESSAGE)
1387                 host->scsi.last_message |= msg->msg[2] << 8;
1388 
1389             if (i != msg->length)
1390                 break;
1391         }
1392         break;
1393     }
1394 #if (DEBUG & DEBUG_MESSAGES)
1395     printk("\n");
1396 #endif
1397 }
1398 
1399 
1400 
1401 
1402 
1403 
1404 static
1405 void acornscsi_readstatusbyte(AS_Host *host)
1406 {
1407     acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1408     acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1409     host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
1410 }
1411 
1412 
1413 
1414 
1415 
1416 
1417 static
1418 unsigned char acornscsi_readmessagebyte(AS_Host *host)
1419 {
1420     unsigned char message;
1421 
1422     acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1423 
1424     acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1425 
1426     message = sbic_arm_read(host, SBIC_DATA);
1427 
1428     
1429     acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1430 
1431     sbic_arm_read(host, SBIC_SSR);
1432 
1433     return message;
1434 }
1435 
1436 
1437 
1438 
1439 
1440 
1441 static
1442 void acornscsi_message(AS_Host *host)
1443 {
1444     unsigned char message[16];
1445     unsigned int msgidx = 0, msglen = 1;
1446 
1447     do {
1448         message[msgidx] = acornscsi_readmessagebyte(host);
1449 
1450         switch (msgidx) {
1451         case 0:
1452             if (message[0] == EXTENDED_MESSAGE ||
1453                 (message[0] >= 0x20 && message[0] <= 0x2f))
1454                 msglen = 2;
1455             break;
1456 
1457         case 1:
1458             if (message[0] == EXTENDED_MESSAGE)
1459                 msglen += message[msgidx];
1460             break;
1461         }
1462         msgidx += 1;
1463         if (msgidx < msglen) {
1464             acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1465 
1466             
1467             acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1468             sbic_arm_read(host, SBIC_SSR);
1469         }
1470     } while (msgidx < msglen);
1471 
1472 #if (DEBUG & DEBUG_MESSAGES)
1473     printk("scsi%d.%c: message in: ",
1474             host->host->host_no, acornscsi_target(host));
1475     spi_print_msg(message);
1476     printk("\n");
1477 #endif
1478 
1479     if (host->scsi.phase == PHASE_RECONNECTED) {
1480         
1481 
1482 
1483 
1484 
1485 
1486         if (message[0] == SIMPLE_QUEUE_TAG)
1487             host->scsi.reconnected.tag = message[1];
1488         if (acornscsi_reconnect_finish(host))
1489             host->scsi.phase = PHASE_MSGIN;
1490     }
1491 
1492     switch (message[0]) {
1493     case ABORT:
1494     case ABORT_TAG:
1495     case COMMAND_COMPLETE:
1496         if (host->scsi.phase != PHASE_STATUSIN) {
1497             printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1498                     host->host->host_no, acornscsi_target(host));
1499             acornscsi_dumplog(host, host->SCpnt->device->id);
1500         }
1501         host->scsi.phase = PHASE_DONE;
1502         host->scsi.SCp.Message = message[0];
1503         break;
1504 
1505     case SAVE_POINTERS:
1506         
1507 
1508 
1509 
1510 
1511 
1512         acornscsi_dma_cleanup(host);
1513         host->SCpnt->SCp = host->scsi.SCp;
1514         host->SCpnt->SCp.sent_command = 0;
1515         host->scsi.phase = PHASE_MSGIN;
1516         break;
1517 
1518     case RESTORE_POINTERS:
1519         
1520 
1521 
1522 
1523 
1524 
1525 
1526 
1527 
1528         acornscsi_dma_cleanup(host);
1529         host->scsi.SCp = host->SCpnt->SCp;
1530         host->scsi.phase = PHASE_MSGIN;
1531         break;
1532 
1533     case DISCONNECT:
1534         
1535 
1536 
1537 
1538 
1539 
1540 
1541 
1542 
1543         acornscsi_dma_cleanup(host);
1544         host->scsi.phase = PHASE_DISCONNECT;
1545         break;
1546 
1547     case MESSAGE_REJECT:
1548 #if 0 
1549         
1550 
1551 
1552 
1553 
1554 
1555         if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
1556             host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
1557 #endif
1558 
1559         
1560 
1561 
1562         if (msgqueue_msglength(&host->scsi.msgs))
1563             acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1564 
1565         switch (host->scsi.last_message) {
1566 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1567         case HEAD_OF_QUEUE_TAG:
1568         case ORDERED_QUEUE_TAG:
1569         case SIMPLE_QUEUE_TAG:
1570             
1571 
1572 
1573 
1574 
1575 
1576             printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
1577                     host->host->host_no, acornscsi_target(host));
1578             host->SCpnt->device->simple_tags = 0;
1579             set_bit(host->SCpnt->device->id * 8 +
1580                     (u8)(host->SCpnt->device->lun & 0x7), host->busyluns);
1581             break;
1582 #endif
1583         case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1584             
1585 
1586 
1587             printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1588                     host->host->host_no, acornscsi_target(host));
1589             host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
1590             host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
1591             sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1592             break;
1593 
1594         default:
1595             break;
1596         }
1597         break;
1598 
1599     case QUEUE_FULL:
1600         
1601         break;
1602 
1603     case SIMPLE_QUEUE_TAG:
1604         
1605         printk("scsi%d.%c: reconnect queue tag %02X\n",
1606                 host->host->host_no, acornscsi_target(host),
1607                 message[1]);
1608         break;
1609 
1610     case EXTENDED_MESSAGE:
1611         switch (message[2]) {
1612 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1613         case EXTENDED_SDTR:
1614             if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
1615                 
1616 
1617 
1618 
1619 
1620 
1621                 host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
1622                 printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1623                         host->host->host_no, acornscsi_target(host),
1624                         message[4], message[3] * 4);
1625                 host->device[host->SCpnt->device->id].sync_xfer =
1626                         calc_sync_xfer(message[3] * 4, message[4]);
1627             } else {
1628                 unsigned char period, length;
1629                 
1630 
1631 
1632 
1633                 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1634                 period = max_t(unsigned int, message[3], sdtr_period / 4);
1635                 length = min_t(unsigned int, message[4], sdtr_size);
1636                 msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1637                                  EXTENDED_SDTR, period, length);
1638                 host->device[host->SCpnt->device->id].sync_xfer =
1639                         calc_sync_xfer(period * 4, length);
1640             }
1641             sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1642             break;
1643 #else
1644             
1645 
1646 
1647 #endif
1648 
1649         case EXTENDED_WDTR:
1650             
1651 
1652 
1653         default:
1654             acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1655             msgqueue_flush(&host->scsi.msgs);
1656             msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1657             break;
1658         }
1659         break;
1660 
1661     default: 
1662         printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1663                 host->host->host_no, acornscsi_target(host),
1664                 message[0]);
1665         acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1666         msgqueue_flush(&host->scsi.msgs);
1667         msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1668         host->scsi.phase = PHASE_MSGIN;
1669         break;
1670     }
1671     acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1672 }
1673 
1674 
1675 
1676 
1677 
1678 
1679 static
1680 void acornscsi_buildmessages(AS_Host *host)
1681 {
1682 #if 0
1683     
1684     if (cmd_reset) {
1685         msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1686         return;
1687     }
1688 #endif
1689 
1690     msgqueue_addmsg(&host->scsi.msgs, 1,
1691                      IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
1692                              host->SCpnt->device->lun));
1693 
1694 #if 0
1695     
1696     if (cmd_aborted) {
1697         acornscsi_abortcmd(host->SCpnt->tag);
1698         return;
1699     }
1700 #endif
1701 
1702 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1703     if (host->SCpnt->tag) {
1704         unsigned int tag_type;
1705 
1706         if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
1707             host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
1708             host->SCpnt->cmnd[0] == INQUIRY)
1709             tag_type = HEAD_OF_QUEUE_TAG;
1710         else
1711             tag_type = SIMPLE_QUEUE_TAG;
1712         msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
1713     }
1714 #endif
1715 
1716 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1717     if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
1718         host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
1719         msgqueue_addmsg(&host->scsi.msgs, 5,
1720                          EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1721                          sdtr_period / 4, sdtr_size);
1722     }
1723 #endif
1724 }
1725 
1726 
1727 
1728 
1729 
1730 
1731 
1732 static
1733 int acornscsi_starttransfer(AS_Host *host)
1734 {
1735     int residual;
1736 
1737     if (!host->scsi.SCp.ptr ) {
1738         printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1739                 host->host->host_no, acornscsi_target(host));
1740         return 0;
1741     }
1742 
1743     residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
1744 
1745     sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1746     sbic_arm_writenext(host, residual >> 16);
1747     sbic_arm_writenext(host, residual >> 8);
1748     sbic_arm_writenext(host, residual);
1749     acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1750     return 1;
1751 }
1752 
1753 
1754 
1755 
1756 
1757 
1758 
1759 
1760 
1761 
1762 
1763 
1764 static
1765 int acornscsi_reconnect(AS_Host *host)
1766 {
1767     unsigned int target, lun, ok = 0;
1768 
1769     target = sbic_arm_read(host, SBIC_SOURCEID);
1770 
1771     if (!(target & 8))
1772         printk(KERN_ERR "scsi%d: invalid source id after reselection "
1773                 "- device fault?\n",
1774                 host->host->host_no);
1775 
1776     target &= 7;
1777 
1778     if (host->SCpnt && !host->scsi.disconnectable) {
1779         printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1780                 "progress to target %d?\n",
1781                 host->host->host_no, target, host->SCpnt->device->id);
1782         host->SCpnt = NULL;
1783     }
1784 
1785     lun = sbic_arm_read(host, SBIC_DATA) & 7;
1786 
1787     host->scsi.reconnected.target = target;
1788     host->scsi.reconnected.lun = lun;
1789     host->scsi.reconnected.tag = 0;
1790 
1791     if (host->scsi.disconnectable && host->SCpnt &&
1792         host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
1793         ok = 1;
1794 
1795     if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1796         ok = 1;
1797 
1798     ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1799 
1800     if (ok) {
1801         host->scsi.phase = PHASE_RECONNECTED;
1802     } else {
1803         
1804         printk(KERN_ERR "scsi%d.%c: reselected with no command "
1805                 "to reconnect with\n",
1806                 host->host->host_no, '0' + target);
1807         acornscsi_dumplog(host, target);
1808         acornscsi_abortcmd(host, 0);
1809         if (host->SCpnt) {
1810             queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1811             host->SCpnt = NULL;
1812         }
1813     }
1814     acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1815     return !ok;
1816 }
1817 
1818 
1819 
1820 
1821 
1822 
1823 
1824 static
1825 int acornscsi_reconnect_finish(AS_Host *host)
1826 {
1827     if (host->scsi.disconnectable && host->SCpnt) {
1828         host->scsi.disconnectable = 0;
1829         if (host->SCpnt->device->id  == host->scsi.reconnected.target &&
1830             host->SCpnt->device->lun == host->scsi.reconnected.lun &&
1831             host->SCpnt->tag         == host->scsi.reconnected.tag) {
1832 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1833             DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1834                     host->host->host_no, acornscsi_target(host)));
1835 #endif
1836         } else {
1837             queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1838 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1839             DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1840                     "to disconnected queue\n",
1841                     host->host->host_no, acornscsi_target(host)));
1842 #endif
1843             host->SCpnt = NULL;
1844         }
1845     }
1846     if (!host->SCpnt) {
1847         host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1848                                 host->scsi.reconnected.target,
1849                                 host->scsi.reconnected.lun,
1850                                 host->scsi.reconnected.tag);
1851 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1852         DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1853                 host->host->host_no, acornscsi_target(host)));
1854 #endif
1855     }
1856 
1857     if (!host->SCpnt)
1858         acornscsi_abortcmd(host, host->scsi.reconnected.tag);
1859     else {
1860         
1861 
1862 
1863         host->scsi.SCp = host->SCpnt->SCp;
1864 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1865         printk(", data pointers: [%p, %X]",
1866                 host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1867 #endif
1868     }
1869 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1870     printk("\n");
1871 #endif
1872 
1873     host->dma.transferred = host->scsi.SCp.scsi_xferred;
1874 
1875     return host->SCpnt != NULL;
1876 }
1877 
1878 
1879 
1880 
1881 
1882 
1883 static
1884 void acornscsi_disconnect_unexpected(AS_Host *host)
1885 {
1886     printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1887             host->host->host_no, acornscsi_target(host));
1888 #if (DEBUG & DEBUG_ABORT)
1889     acornscsi_dumplog(host, 8);
1890 #endif
1891 
1892     acornscsi_done(host, &host->SCpnt, DID_ERROR);
1893 }
1894 
1895 
1896 
1897 
1898 
1899 
1900 
1901 static
1902 void acornscsi_abortcmd(AS_Host *host, unsigned char tag)
1903 {
1904     host->scsi.phase = PHASE_ABORTED;
1905     sbic_arm_write(host, SBIC_CMND, CMND_ASSERTATN);
1906 
1907     msgqueue_flush(&host->scsi.msgs);
1908 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1909     if (tag)
1910         msgqueue_addmsg(&host->scsi.msgs, 2, ABORT_TAG, tag);
1911     else
1912 #endif
1913         msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1914 }
1915 
1916 
1917 
1918 
1919 
1920 
1921 
1922 
1923 
1924 
1925 
1926 
1927 static
1928 intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1929 {
1930     unsigned int asr, ssr;
1931 
1932     asr = sbic_arm_read(host, SBIC_ASR);
1933     if (!(asr & ASR_INT))
1934         return INTR_IDLE;
1935 
1936     ssr = sbic_arm_read(host, SBIC_SSR);
1937 
1938 #if (DEBUG & DEBUG_PHASES)
1939     print_sbic_status(asr, ssr, host->scsi.phase);
1940 #endif
1941 
1942     ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1943 
1944     if (host->SCpnt && !host->scsi.disconnectable)
1945         ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
1946 
1947     switch (ssr) {
1948     case 0x00:                          
1949         printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1950                 host->host->host_no);
1951         
1952         sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
1953         sbic_arm_write(host, SBIC_CMND, CMND_RESET);
1954         return INTR_IDLE;
1955 
1956     case 0x01:                          
1957         sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
1958         sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
1959         sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1960         sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1961         msgqueue_flush(&host->scsi.msgs);
1962         return INTR_IDLE;
1963 
1964     case 0x41:                          
1965         acornscsi_disconnect_unexpected(host);
1966         return INTR_NEXT_COMMAND;
1967     }
1968 
1969     switch (host->scsi.phase) {
1970     case PHASE_CONNECTING:              
1971         switch (ssr) {
1972         case 0x11:                      
1973             
1974             host->scsi.phase = PHASE_CONNECTED;
1975             msgqueue_flush(&host->scsi.msgs);
1976             host->dma.transferred = host->scsi.SCp.scsi_xferred;
1977             
1978             asr = sbic_arm_read(host, SBIC_ASR);
1979             if (!(asr & ASR_INT))
1980                 break;
1981             ssr = sbic_arm_read(host, SBIC_SSR);
1982             ADD_STATUS(8, ssr, host->scsi.phase, 1);
1983             ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
1984             goto connected;
1985             
1986         case 0x42:                      
1987                                         
1988             acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
1989             return INTR_NEXT_COMMAND;
1990 
1991         case 0x81:                      
1992             
1993             host->origSCpnt = host->SCpnt;
1994             host->SCpnt = NULL;
1995             msgqueue_flush(&host->scsi.msgs);
1996             acornscsi_reconnect(host);
1997             break;
1998 
1999         default:
2000             printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
2001                     host->host->host_no, acornscsi_target(host), ssr);
2002             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2003             acornscsi_abortcmd(host, host->SCpnt->tag);
2004         }
2005         return INTR_PROCESSING;
2006 
2007     connected:
2008     case PHASE_CONNECTED:               
2009         switch (ssr) {
2010 #ifdef NONSTANDARD
2011         case 0x8a:                      
2012             
2013             acornscsi_sendcommand(host);
2014             break;
2015 
2016         case 0x8b:                      
2017             
2018             acornscsi_readstatusbyte(host);
2019             host->scsi.phase = PHASE_STATUSIN;
2020             break;
2021 #endif
2022 
2023         case 0x8e:                      
2024             
2025             host->scsi.phase = PHASE_MSGOUT;
2026             acornscsi_buildmessages(host);
2027             acornscsi_sendmessage(host);
2028             break;
2029 
2030         
2031         case 0x85:                      
2032             acornscsi_done(host, &host->SCpnt, DID_ERROR);
2033             break;
2034 
2035         default:
2036             printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
2037                     host->host->host_no, acornscsi_target(host), ssr);
2038             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2039             acornscsi_abortcmd(host, host->SCpnt->tag);
2040         }
2041         return INTR_PROCESSING;
2042 
2043     case PHASE_MSGOUT:                  
2044         
2045 
2046 
2047 
2048         switch (ssr) {
2049         case 0x8a:                      
2050         case 0x1a:                      
2051             
2052             acornscsi_sendcommand(host);
2053             break;
2054 
2055         case 0x8b:                      
2056         case 0x1b:                      
2057             
2058             acornscsi_readstatusbyte(host);
2059             host->scsi.phase = PHASE_STATUSIN;
2060             break;
2061 
2062         case 0x8e:                      
2063             
2064             acornscsi_sendmessage(host);
2065             break;
2066 
2067         case 0x4f:                      
2068         case 0x1f:                      
2069             
2070             acornscsi_message(host);
2071             break;
2072 
2073         default:
2074             printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2075                     host->host->host_no, acornscsi_target(host), ssr);
2076             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2077         }
2078         return INTR_PROCESSING;
2079 
2080     case PHASE_COMMAND:                 
2081         switch (ssr) {
2082         case 0x18:                      
2083             
2084             if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2085                 acornscsi_abortcmd(host, host->SCpnt->tag);
2086             acornscsi_dma_setup(host, DMA_OUT);
2087             if (!acornscsi_starttransfer(host))
2088                 acornscsi_abortcmd(host, host->SCpnt->tag);
2089             host->scsi.phase = PHASE_DATAOUT;
2090             return INTR_IDLE;
2091 
2092         case 0x19:                      
2093             
2094             if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2095                 acornscsi_abortcmd(host, host->SCpnt->tag);
2096             acornscsi_dma_setup(host, DMA_IN);
2097             if (!acornscsi_starttransfer(host))
2098                 acornscsi_abortcmd(host, host->SCpnt->tag);
2099             host->scsi.phase = PHASE_DATAIN;
2100             return INTR_IDLE;
2101 
2102         case 0x1b:                      
2103             
2104             acornscsi_readstatusbyte(host);
2105             host->scsi.phase = PHASE_STATUSIN;
2106             break;
2107 
2108         case 0x1e:                      
2109             
2110             acornscsi_sendmessage(host);
2111             break;
2112 
2113         case 0x1f:                      
2114             
2115             acornscsi_message(host);
2116             break;
2117 
2118         default:
2119             printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2120                     host->host->host_no, acornscsi_target(host), ssr);
2121             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2122         }
2123         return INTR_PROCESSING;
2124 
2125     case PHASE_DISCONNECT:              
2126         if (ssr == 0x85) {              
2127             host->scsi.disconnectable = 1;
2128             host->scsi.reconnected.tag = 0;
2129             host->scsi.phase = PHASE_IDLE;
2130             host->stats.disconnects += 1;
2131         } else {
2132             printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2133                     host->host->host_no, acornscsi_target(host), ssr);
2134             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2135         }
2136         return INTR_NEXT_COMMAND;
2137 
2138     case PHASE_IDLE:                    
2139         if (ssr == 0x81)                
2140             acornscsi_reconnect(host);
2141         else {
2142             printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2143                     host->host->host_no, acornscsi_target(host), ssr);
2144             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2145         }
2146         return INTR_PROCESSING;
2147 
2148     case PHASE_RECONNECTED:             
2149         
2150 
2151 
2152 
2153         
2154 
2155 
2156 
2157         if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2158             return INTR_IDLE;
2159         ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
2160         switch (ssr) {
2161         case 0x88:                      
2162                                         
2163             
2164             acornscsi_dma_setup(host, DMA_OUT);
2165             if (!acornscsi_starttransfer(host))
2166                 acornscsi_abortcmd(host, host->SCpnt->tag);
2167             host->scsi.phase = PHASE_DATAOUT;
2168             return INTR_IDLE;
2169 
2170         case 0x89:                      
2171                                         
2172             
2173             acornscsi_dma_setup(host, DMA_IN);
2174             if (!acornscsi_starttransfer(host))
2175                 acornscsi_abortcmd(host, host->SCpnt->tag);
2176             host->scsi.phase = PHASE_DATAIN;
2177             return INTR_IDLE;
2178 
2179         case 0x8a:                      
2180             
2181             acornscsi_sendcommand(host);
2182             break;
2183 
2184         case 0x8b:                      
2185                                         
2186             
2187             acornscsi_readstatusbyte(host);
2188             host->scsi.phase = PHASE_STATUSIN;
2189             break;
2190 
2191         case 0x8e:                      
2192                                         
2193             
2194             acornscsi_sendmessage(host);
2195             break;
2196 
2197         case 0x8f:                      
2198             acornscsi_message(host);    
2199             break;
2200 
2201         default:
2202             printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2203                     host->host->host_no, acornscsi_target(host), ssr);
2204             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2205         }
2206         return INTR_PROCESSING;
2207 
2208     case PHASE_DATAIN:                  
2209         
2210 
2211 
2212 
2213         switch (ssr) {
2214         case 0x19:                      
2215         case 0x89:                      
2216             acornscsi_abortcmd(host, host->SCpnt->tag);
2217             return INTR_IDLE;
2218 
2219         case 0x1b:                      
2220         case 0x4b:                      
2221         case 0x8b:                      
2222             
2223             host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2224                                           acornscsi_sbic_xfcount(host);
2225             acornscsi_dma_stop(host);
2226             acornscsi_readstatusbyte(host);
2227             host->scsi.phase = PHASE_STATUSIN;
2228             break;
2229 
2230         case 0x1e:                      
2231         case 0x4e:                      
2232         case 0x8e:                      
2233             
2234             host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2235                                           acornscsi_sbic_xfcount(host);
2236             acornscsi_dma_stop(host);
2237             acornscsi_sendmessage(host);
2238             break;
2239 
2240         case 0x1f:                      
2241         case 0x4f:                      
2242         case 0x8f:                      
2243             
2244             host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2245                                           acornscsi_sbic_xfcount(host);
2246             acornscsi_dma_stop(host);
2247             acornscsi_message(host);    
2248             break;
2249 
2250         default:
2251             printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2252                     host->host->host_no, acornscsi_target(host), ssr);
2253             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2254         }
2255         return INTR_PROCESSING;
2256 
2257     case PHASE_DATAOUT:                 
2258         
2259 
2260 
2261 
2262         switch (ssr) {
2263         case 0x18:                      
2264         case 0x88:                      
2265             acornscsi_abortcmd(host, host->SCpnt->tag);
2266             return INTR_IDLE;
2267 
2268         case 0x1b:                      
2269         case 0x4b:                      
2270         case 0x8b:                      
2271             
2272             host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2273                                           acornscsi_sbic_xfcount(host);
2274             acornscsi_dma_stop(host);
2275             acornscsi_dma_adjust(host);
2276             acornscsi_readstatusbyte(host);
2277             host->scsi.phase = PHASE_STATUSIN;
2278             break;
2279 
2280         case 0x1e:                      
2281         case 0x4e:                      
2282         case 0x8e:                      
2283             
2284             host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2285                                           acornscsi_sbic_xfcount(host);
2286             acornscsi_dma_stop(host);
2287             acornscsi_dma_adjust(host);
2288             acornscsi_sendmessage(host);
2289             break;
2290 
2291         case 0x1f:                      
2292         case 0x4f:                      
2293         case 0x8f:                      
2294             
2295             host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2296                                           acornscsi_sbic_xfcount(host);
2297             acornscsi_dma_stop(host);
2298             acornscsi_dma_adjust(host);
2299             acornscsi_message(host);    
2300             break;
2301 
2302         default:
2303             printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2304                     host->host->host_no, acornscsi_target(host), ssr);
2305             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2306         }
2307         return INTR_PROCESSING;
2308 
2309     case PHASE_STATUSIN:                
2310         switch (ssr) {
2311         case 0x1f:                      
2312         case 0x8f:                      
2313             
2314             acornscsi_message(host);
2315             break;
2316 
2317         case 0x1e:                      
2318         case 0x8e:                      
2319             
2320             acornscsi_sendmessage(host);
2321             break;
2322 
2323         default:
2324             printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2325                     host->host->host_no, acornscsi_target(host), ssr);
2326             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2327         }
2328         return INTR_PROCESSING;
2329 
2330     case PHASE_MSGIN:                   
2331         switch (ssr) {
2332         case 0x1e:                      
2333         case 0x4e:                      
2334         case 0x8e:                      
2335             
2336             acornscsi_sendmessage(host);
2337             break;
2338 
2339         case 0x1f:                      
2340         case 0x2f:
2341         case 0x4f:
2342         case 0x8f:
2343             acornscsi_message(host);
2344             break;
2345 
2346         case 0x85:
2347             printk("scsi%d.%c: strange message in disconnection\n",
2348                 host->host->host_no, acornscsi_target(host));
2349             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2350             acornscsi_done(host, &host->SCpnt, DID_ERROR);
2351             break;
2352 
2353         default:
2354             printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2355                     host->host->host_no, acornscsi_target(host), ssr);
2356             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2357         }
2358         return INTR_PROCESSING;
2359 
2360     case PHASE_DONE:                    
2361         switch (ssr) {
2362         case 0x85:                      
2363             acornscsi_done(host, &host->SCpnt, DID_OK);
2364             return INTR_NEXT_COMMAND;
2365 
2366         case 0x1e:
2367         case 0x8e:
2368             acornscsi_sendmessage(host);
2369             break;
2370 
2371         default:
2372             printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2373                     host->host->host_no, acornscsi_target(host), ssr);
2374             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2375         }
2376         return INTR_PROCESSING;
2377 
2378     case PHASE_ABORTED:
2379         switch (ssr) {
2380         case 0x85:
2381             if (host->SCpnt)
2382                 acornscsi_done(host, &host->SCpnt, DID_ABORT);
2383             else {
2384                 clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2385                           host->busyluns);
2386                 host->scsi.phase = PHASE_IDLE;
2387             }
2388             return INTR_NEXT_COMMAND;
2389 
2390         case 0x1e:
2391         case 0x2e:
2392         case 0x4e:
2393         case 0x8e:
2394             acornscsi_sendmessage(host);
2395             break;
2396 
2397         default:
2398             printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2399                     host->host->host_no, acornscsi_target(host), ssr);
2400             acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2401         }
2402         return INTR_PROCESSING;
2403 
2404     default:
2405         printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2406                 host->host->host_no, acornscsi_target(host), ssr);
2407         acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2408     }
2409     return INTR_PROCESSING;
2410 }
2411 
2412 
2413 
2414 
2415 
2416 
2417 
2418 static irqreturn_t
2419 acornscsi_intr(int irq, void *dev_id)
2420 {
2421     AS_Host *host = (AS_Host *)dev_id;
2422     intr_ret_t ret;
2423     int iostatus;
2424     int in_irq = 0;
2425 
2426     do {
2427         ret = INTR_IDLE;
2428 
2429         iostatus = readb(host->fast + INT_REG);
2430 
2431         if (iostatus & 2) {
2432             acornscsi_dma_intr(host);
2433             iostatus = readb(host->fast + INT_REG);
2434         }
2435 
2436         if (iostatus & 8)
2437             ret = acornscsi_sbicintr(host, in_irq);
2438 
2439         
2440 
2441 
2442 
2443 
2444         if (host->dma.xfer_required)
2445             acornscsi_dma_xfer(host);
2446 
2447         if (ret == INTR_NEXT_COMMAND)
2448             ret = acornscsi_kick(host);
2449 
2450         in_irq = 1;
2451     } while (ret != INTR_IDLE);
2452 
2453     return IRQ_HANDLED;
2454 }
2455 
2456 
2457 
2458 
2459 
2460 
2461 
2462 
2463 
2464 
2465 
2466 
2467 static int acornscsi_queuecmd_lck(struct scsi_cmnd *SCpnt,
2468                        void (*done)(struct scsi_cmnd *))
2469 {
2470     AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2471 
2472     if (!done) {
2473         
2474         panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2475                 host->host->host_no, SCpnt);
2476         return -EINVAL;
2477     }
2478 
2479 #if (DEBUG & DEBUG_NO_WRITE)
2480     if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
2481         printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2482             host->host->host_no, '0' + SCpnt->device->id);
2483         SCpnt->result = DID_NO_CONNECT << 16;
2484         done(SCpnt);
2485         return 0;
2486     }
2487 #endif
2488 
2489     SCpnt->scsi_done = done;
2490     SCpnt->host_scribble = NULL;
2491     SCpnt->result = 0;
2492     SCpnt->tag = 0;
2493     SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2494     SCpnt->SCp.sent_command = 0;
2495     SCpnt->SCp.scsi_xferred = 0;
2496 
2497     init_SCp(SCpnt);
2498 
2499     host->stats.queues += 1;
2500 
2501     {
2502         unsigned long flags;
2503 
2504         if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2505             SCpnt->result = DID_ERROR << 16;
2506             done(SCpnt);
2507             return 0;
2508         }
2509         local_irq_save(flags);
2510         if (host->scsi.phase == PHASE_IDLE)
2511             acornscsi_kick(host);
2512         local_irq_restore(flags);
2513     }
2514     return 0;
2515 }
2516 
2517 DEF_SCSI_QCMD(acornscsi_queuecmd)
2518 
2519 
2520 
2521 
2522 
2523 
2524 
2525 
2526 
2527 static inline void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1,
2528                                           struct scsi_cmnd **SCpntp2,
2529                                           int result)
2530 {
2531         struct scsi_cmnd *SCpnt = *SCpntp1;
2532 
2533     if (SCpnt) {
2534         *SCpntp1 = NULL;
2535 
2536         SCpnt->result = result;
2537         SCpnt->scsi_done(SCpnt);
2538     }
2539 
2540     if (SCpnt == *SCpntp2)
2541         *SCpntp2 = NULL;
2542 }
2543 
2544 enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2545 
2546 
2547 
2548 
2549 
2550 
2551 
2552 static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
2553 {
2554         enum res_abort res = res_not_running;
2555 
2556         if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
2557                 
2558 
2559 
2560 
2561 
2562 
2563 
2564                 printk("on issue queue ");
2565 
2566                 res = res_success;
2567         } else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
2568                 
2569 
2570 
2571 
2572 
2573 
2574 
2575 
2576                 printk("on disconnected queue ");
2577 
2578                 res = res_success;
2579         } else if (host->SCpnt == SCpnt) {
2580                 unsigned long flags;
2581 
2582 
2583                 printk("executing ");
2584 
2585 
2586                 local_irq_save(flags);
2587                 switch (host->scsi.phase) {
2588                 
2589 
2590 
2591 
2592 
2593 
2594 
2595 
2596                 case PHASE_IDLE:
2597                         if (host->scsi.disconnectable) {
2598                                 host->scsi.disconnectable = 0;
2599                                 host->SCpnt = NULL;
2600                                 res = res_success;
2601                         }
2602                         break;
2603 
2604                 
2605 
2606 
2607 
2608 
2609                 case PHASE_CONNECTED:
2610                         sbic_arm_write(host, SBIC_CMND, CMND_DISCONNECT);
2611                         host->SCpnt = NULL;
2612                         res = res_success_clear;
2613                         break;
2614 
2615                 default:
2616                         acornscsi_abortcmd(host, host->SCpnt->tag);
2617                         res = res_snooze;
2618                 }
2619                 local_irq_restore(flags);
2620         } else if (host->origSCpnt == SCpnt) {
2621                 
2622 
2623 
2624 
2625 
2626 
2627                 host->origSCpnt = NULL;
2628 
2629                 printk("waiting for execution ");
2630 
2631                 res = res_success_clear;
2632         } else
2633                 printk("unknown ");
2634 
2635         return res;
2636 }
2637 
2638 
2639 
2640 
2641 
2642 
2643 
2644 int acornscsi_abort(struct scsi_cmnd *SCpnt)
2645 {
2646         AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
2647         int result;
2648 
2649         host->stats.aborts += 1;
2650 
2651 #if (DEBUG & DEBUG_ABORT)
2652         {
2653                 int asr, ssr;
2654                 asr = sbic_arm_read(host, SBIC_ASR);
2655                 ssr = sbic_arm_read(host, SBIC_SSR);
2656 
2657                 printk(KERN_WARNING "acornscsi_abort: ");
2658                 print_sbic_status(asr, ssr, host->scsi.phase);
2659                 acornscsi_dumplog(host, SCpnt->device->id);
2660         }
2661 #endif
2662 
2663         printk("scsi%d: ", host->host->host_no);
2664 
2665         switch (acornscsi_do_abort(host, SCpnt)) {
2666         
2667 
2668 
2669 
2670 
2671         case res_success_clear:
2672 
2673                 printk("clear ");
2674 
2675                 clear_bit(SCpnt->device->id * 8 +
2676                           (u8)(SCpnt->device->lun & 0x7), host->busyluns);
2677 
2678         
2679 
2680 
2681 
2682 
2683         case res_success:
2684 
2685                 printk("success\n");
2686 
2687                 result = SUCCESS;
2688                 break;
2689 
2690         
2691 
2692 
2693 
2694 
2695         case res_snooze:
2696 
2697                 printk("snooze\n");
2698 
2699                 result = FAILED;
2700                 break;
2701 
2702         
2703 
2704 
2705 
2706         default:
2707         case res_not_running:
2708                 acornscsi_dumplog(host, SCpnt->device->id);
2709                 result = FAILED;
2710 
2711                 printk("not running\n");
2712 
2713                 break;
2714         }
2715 
2716         return result;
2717 }
2718 
2719 
2720 
2721 
2722 
2723 
2724 
2725 int acornscsi_host_reset(struct scsi_cmnd *SCpnt)
2726 {
2727         AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2728         struct scsi_cmnd *SCptr;
2729     
2730     host->stats.resets += 1;
2731 
2732 #if (DEBUG & DEBUG_RESET)
2733     {
2734         int asr, ssr, devidx;
2735 
2736         asr = sbic_arm_read(host, SBIC_ASR);
2737         ssr = sbic_arm_read(host, SBIC_SSR);
2738 
2739         printk(KERN_WARNING "acornscsi_reset: ");
2740         print_sbic_status(asr, ssr, host->scsi.phase);
2741         for (devidx = 0; devidx < 9; devidx++)
2742             acornscsi_dumplog(host, devidx);
2743     }
2744 #endif
2745 
2746     acornscsi_dma_stop(host);
2747 
2748     
2749 
2750 
2751 
2752     acornscsi_resetcard(host);
2753 
2754     while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2755         ;
2756 
2757     return SUCCESS;
2758 }
2759 
2760 
2761 
2762 
2763 
2764 
2765 
2766 
2767 
2768 
2769 
2770 const
2771 char *acornscsi_info(struct Scsi_Host *host)
2772 {
2773     static char string[100], *p;
2774 
2775     p = string;
2776     
2777     p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2778 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2779     " SYNC"
2780 #endif
2781 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2782     " TAG"
2783 #endif
2784 #if (DEBUG & DEBUG_NO_WRITE)
2785     " NOWRITE (" __stringify(NO_WRITE) ")"
2786 #endif
2787                 , host->hostt->name, host->io_port, host->irq,
2788                 VER_MAJOR, VER_MINOR, VER_PATCH);
2789     return string;
2790 }
2791 
2792 static int acornscsi_show_info(struct seq_file *m, struct Scsi_Host *instance)
2793 {
2794     int devidx;
2795     struct scsi_device *scd;
2796     AS_Host *host;
2797 
2798     host  = (AS_Host *)instance->hostdata;
2799     
2800     seq_printf(m, "AcornSCSI driver v%d.%d.%d"
2801 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2802     " SYNC"
2803 #endif
2804 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2805     " TAG"
2806 #endif
2807 #if (DEBUG & DEBUG_NO_WRITE)
2808     " NOWRITE (" __stringify(NO_WRITE) ")"
2809 #endif
2810                 "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2811 
2812     seq_printf(m,       "SBIC: WD33C93A  Address: %p    IRQ : %d\n",
2813                         host->base + SBIC_REGIDX, host->scsi.irq);
2814 #ifdef USE_DMAC
2815     seq_printf(m,       "DMAC: uPC71071  Address: %p  IRQ : %d\n\n",
2816                         host->base + DMAC_OFFSET, host->scsi.irq);
2817 #endif
2818 
2819     seq_printf(m,       "Statistics:\n"
2820                         "Queued commands: %-10u    Issued commands: %-10u\n"
2821                         "Done commands  : %-10u    Reads          : %-10u\n"
2822                         "Writes         : %-10u    Others         : %-10u\n"
2823                         "Disconnects    : %-10u    Aborts         : %-10u\n"
2824                         "Resets         : %-10u\n\nLast phases:",
2825                         host->stats.queues,             host->stats.removes,
2826                         host->stats.fins,               host->stats.reads,
2827                         host->stats.writes,             host->stats.miscs,
2828                         host->stats.disconnects,        host->stats.aborts,
2829                         host->stats.resets);
2830 
2831     for (devidx = 0; devidx < 9; devidx ++) {
2832         unsigned int statptr, prev;
2833 
2834         seq_printf(m, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
2835         statptr = host->status_ptr[devidx] - 10;
2836 
2837         if ((signed int)statptr < 0)
2838             statptr += STATUS_BUFFER_SIZE;
2839 
2840         prev = host->status[devidx][statptr].when;
2841 
2842         for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
2843             if (host->status[devidx][statptr].when) {
2844                 seq_printf(m, "%c%02X:%02X+%2ld",
2845                         host->status[devidx][statptr].irq ? '-' : ' ',
2846                         host->status[devidx][statptr].ph,
2847                         host->status[devidx][statptr].ssr,
2848                         (host->status[devidx][statptr].when - prev) < 100 ?
2849                                 (host->status[devidx][statptr].when - prev) : 99);
2850                 prev = host->status[devidx][statptr].when;
2851             }
2852         }
2853     }
2854 
2855     seq_printf(m, "\nAttached devices:\n");
2856 
2857     shost_for_each_device(scd, instance) {
2858         seq_printf(m, "Device/Lun TaggedQ      Sync\n");
2859         seq_printf(m, "     %d/%llu   ", scd->id, scd->lun);
2860         if (scd->tagged_supported)
2861                 seq_printf(m, "%3sabled(%3d) ",
2862                              scd->simple_tags ? "en" : "dis",
2863                              scd->current_tag);
2864         else
2865                 seq_printf(m, "unsupported  ");
2866 
2867         if (host->device[scd->id].sync_xfer & 15)
2868                 seq_printf(m, "offset %d, %d ns\n",
2869                              host->device[scd->id].sync_xfer & 15,
2870                              acornscsi_getperiod(host->device[scd->id].sync_xfer));
2871         else
2872                 seq_printf(m, "async\n");
2873 
2874     }
2875     return 0;
2876 }
2877 
2878 static struct scsi_host_template acornscsi_template = {
2879         .module                 = THIS_MODULE,
2880         .show_info              = acornscsi_show_info,
2881         .name                   = "AcornSCSI",
2882         .info                   = acornscsi_info,
2883         .queuecommand           = acornscsi_queuecmd,
2884         .eh_abort_handler       = acornscsi_abort,
2885         .eh_host_reset_handler  = acornscsi_host_reset,
2886         .can_queue              = 16,
2887         .this_id                = 7,
2888         .sg_tablesize           = SG_ALL,
2889         .cmd_per_lun            = 2,
2890         .dma_boundary           = PAGE_SIZE - 1,
2891         .proc_name              = "acornscsi",
2892 };
2893 
2894 static int acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
2895 {
2896         struct Scsi_Host *host;
2897         AS_Host *ashost;
2898         int ret;
2899 
2900         ret = ecard_request_resources(ec);
2901         if (ret)
2902                 goto out;
2903 
2904         host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
2905         if (!host) {
2906                 ret = -ENOMEM;
2907                 goto out_release;
2908         }
2909 
2910         ashost = (AS_Host *)host->hostdata;
2911 
2912         ashost->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
2913         ashost->fast = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
2914         if (!ashost->base || !ashost->fast)
2915                 goto out_put;
2916 
2917         host->irq = ec->irq;
2918         ashost->host = host;
2919         ashost->scsi.irq = host->irq;
2920 
2921         ec->irqaddr     = ashost->fast + INT_REG;
2922         ec->irqmask     = 0x0a;
2923 
2924         ret = request_irq(host->irq, acornscsi_intr, 0, "acornscsi", ashost);
2925         if (ret) {
2926                 printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
2927                         host->host_no, ashost->scsi.irq, ret);
2928                 goto out_put;
2929         }
2930 
2931         memset(&ashost->stats, 0, sizeof (ashost->stats));
2932         queue_initialise(&ashost->queues.issue);
2933         queue_initialise(&ashost->queues.disconnected);
2934         msgqueue_initialise(&ashost->scsi.msgs);
2935 
2936         acornscsi_resetcard(ashost);
2937 
2938         ret = scsi_add_host(host, &ec->dev);
2939         if (ret)
2940                 goto out_irq;
2941 
2942         scsi_scan_host(host);
2943         goto out;
2944 
2945  out_irq:
2946         free_irq(host->irq, ashost);
2947         msgqueue_free(&ashost->scsi.msgs);
2948         queue_free(&ashost->queues.disconnected);
2949         queue_free(&ashost->queues.issue);
2950  out_put:
2951         ecardm_iounmap(ec, ashost->fast);
2952         ecardm_iounmap(ec, ashost->base);
2953         scsi_host_put(host);
2954  out_release:
2955         ecard_release_resources(ec);
2956  out:
2957         return ret;
2958 }
2959 
2960 static void acornscsi_remove(struct expansion_card *ec)
2961 {
2962         struct Scsi_Host *host = ecard_get_drvdata(ec);
2963         AS_Host *ashost = (AS_Host *)host->hostdata;
2964 
2965         ecard_set_drvdata(ec, NULL);
2966         scsi_remove_host(host);
2967 
2968         
2969 
2970 
2971         writeb(0x80, ashost->fast + PAGE_REG);
2972 
2973         free_irq(host->irq, ashost);
2974 
2975         msgqueue_free(&ashost->scsi.msgs);
2976         queue_free(&ashost->queues.disconnected);
2977         queue_free(&ashost->queues.issue);
2978         ecardm_iounmap(ec, ashost->fast);
2979         ecardm_iounmap(ec, ashost->base);
2980         scsi_host_put(host);
2981         ecard_release_resources(ec);
2982 }
2983 
2984 static const struct ecard_id acornscsi_cids[] = {
2985         { MANU_ACORN, PROD_ACORN_SCSI },
2986         { 0xffff, 0xffff },
2987 };
2988 
2989 static struct ecard_driver acornscsi_driver = {
2990         .probe          = acornscsi_probe,
2991         .remove         = acornscsi_remove,
2992         .id_table       = acornscsi_cids,
2993         .drv = {
2994                 .name           = "acornscsi",
2995         },
2996 };
2997 
2998 static int __init acornscsi_init(void)
2999 {
3000         return ecard_register_driver(&acornscsi_driver);
3001 }
3002 
3003 static void __exit acornscsi_exit(void)
3004 {
3005         ecard_remove_driver(&acornscsi_driver);
3006 }
3007 
3008 module_init(acornscsi_init);
3009 module_exit(acornscsi_exit);
3010 
3011 MODULE_AUTHOR("Russell King");
3012 MODULE_DESCRIPTION("AcornSCSI driver");
3013 MODULE_LICENSE("GPL");