1 /* 2 * NCR 5380 generic driver routines. These should make it *trivial* 3 * to implement 5380 SCSI drivers under Linux with a non-trantor 4 * architecture. 5 * 6 * Copyright 1993, Drew Eckhardt 7 * Visionary Computing 8 * (Unix and Linux consulting and custom programming) 9 * drew@colorado.edu 10 * +1 (303) 440-4894 11 * 12 * DISTRIBUTION REALEASE 3. 13 * 14 * For more information, please consult 15 * 16 * NCR 5380 Family 17 * SCSI Protocol Controller 18 * Databook 19 * 20 * NCR Microelectronics 21 * 1635 Aeroplaza Drive 22 * Colorado Springs, CO 80916 23 * 1+ (719) 578-3400 24 * 1+ (800) 334-5454 25 */ 26
27 /* 28 * $Log: NCR5380.c,v $ 29 */ 30
31 /* 32 * Furthur development / testing that should be done : 33 * 1. Test USLEEP code 34 * 2. Test SCSI-II tagged queueing (I have no devices which support 35 * tagged queueing) 36 * 3. Flesh out REAL_DMA code (my sample board doesn't support this) 37 * Perhaps some of the 680x0 porting crew can help out here? 38 * 4. Test linked command handling code after Eric is ready with 39 * the high level code. 40 * 5. Tie instance handling into Eric's multi-host patches 41 */ 42
43 #ifndefnotyet 44 #undefLINKED 45 #undefUSLEEP 46 #undefREAL_DMA 47 #endif 48
49 /* 50 * Design 51 * Issues : 52 * 53 * The other Linux SCSI drivers were written when Linux was Intel PC-only, 54 * and specifically for each board rather than each chip. This makes their 55 * adaptation to platforms like the Mac (Some of which use NCR5380's) 56 * more difficult than it has to be. 57 * 58 * Also, many of the SCSI drivers were written before the command queing 59 * routines were implemented, meaning their implementations of queued 60 * commands were hacked on rather than designed in from the start. 61 * 62 * When I designed the Linux SCSI drivers I figured that 63 * while having two different SCSI boards in a system might be useful 64 * for debugging things, two of the same type wouldn't be used. 65 * Well, I was wrong and a number of users have mailed me about running 66 * multiple high-performance SCSI boards in a server. 67 * 68 * Finally, when I get questions from users, I have no idea what 69 * revision of my driver they are running. 70 * 71 * This driver attempts to address these problems : 72 * This is a generic 5380 driver. To use it on a different platform, 73 * one simply writes appropriate system specific macros (ie, data 74 * transfer - some PC's will use the I/O bus, 68K's must use 75 * memory mapped) and drops this file in their 'C' wrapper. 76 * 77 * As far as command queueing, two queues are maintained for 78 * each 5380 in the system - commands that haven't been issued yet, 79 * and commands that are currently executing. This means that an 80 * unlimited number of commands may be queued, letting 81 * more commands propogate from the higher driver levels giving higher 82 * througput. Note that both I_T_L and I_T_L_Q nexuses are supported, 83 * allowing multiple commands to propogate all the way to a SCSI-II device 84 * while a command is allready executing. 85 * 86 * To solve the multiple-boards-in-the-same-system problem, 87 * there is a separate instance structure for each instance 88 * of a 5380 in the system. So, mutliple NCR5380 drivers will 89 * be able to coexist with appropriate changes to the high level 90 * SCSI code. 91 * 92 * A NCR5380_PUBLIC_REVISION macro is provided, with the release 93 * number (updated for each public release) printed by the 94 * NCR5380_print_options command, which should be called from the 95 * wrapper detect function, so that I know what release of the driver 96 * users are using. 97 * 98 * Issues specific to the NCR5380 : 99 * 100 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead 101 * piece of hardware that requires you to sit in a loop polling for 102 * the REQ signal as long as you are connected. Some devices are 103 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect 104 * while doing long seek operations. 105 * 106 * The workarround for this is to keep track of devices that have 107 * disconnected. If the device hasn't disconnected, for commands that 108 * should disconnect, we do something like 109 * 110 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs } 111 * 112 * Some tweaking of N and M needs to be done. An algorithm based 113 * on "time to data" would give the best results as long as short time 114 * to datas (ie, on the same track) were considered, however these 115 * broken devices are the exception rather than the rule and I'd rather 116 * spend my time optomizing for the normal case. 117 * 118 * Architecture : 119 * 120 * At the heart of the design is a corroutine, NCR5380_main, 121 * which is started when not running by the interrupt handler, 122 * timer, and queue command function. It attempts to establish 123 * I_T_L or I_T_L_Q nexuses by removing the commands from the 124 * issue queue and calling NCR5380_select() if a nexus 125 * is not established. 126 * 127 * Once a nexus is established, the NCR5380_information_transfer() 128 * phase goes through the various phases as instructed by the target. 129 * if the target goes into MSG IN and sends a DISCONNECT message, 130 * the command structure is placed into the per instance disconnected 131 * queue, and NCR5380_main tries to find more work. If USLEEP 132 * was defined, and the target is idle for too long, the system 133 * will try to sleep. 134 * 135 * If a command has disconnected, eventually an interrupt will trigger, 136 * calling NCR5380_intr() which will inturn call NCR5380_reselect 137 * to restablish a nexus. This will run main if necessary. 138 * 139 * On command termination, the done function will be called as 140 * appropriate. 141 * 142 * SCSI pointers are maintained in the SCp field of SCSI command 143 * structures, being initialized after the command is connected 144 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer. 145 * Note that in violation of the standard, an implicit SAVE POINTERS operation 146 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS. 147 */ 148
149 /* 150 * Using this file : 151 * This file a skeleton Linux SCSI driver for the NCR 5380 series 152 * of chips. To use it, you write a architecture specific functions 153 * and macros and include this file in your driver. 154 * 155 * These macros control options : 156 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be 157 * defined. 158 * 159 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically 160 * for commands that return with a CHECK CONDITION status. 161 * 162 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential 163 * tracievers. 164 * 165 * LINKED - if defined, linked commands are supported. 166 * 167 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases. 168 * 169 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases. 170 * 171 * SCSI2 - if defined, SCSI-2 tagged queing is used where possible 172 * 173 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You 174 * only really want to use this if you're having a problem with 175 * dropped characters during high speed communications, and even 176 * then, you're going to be better off twiddling with transfersize 177 * in the high level code. 178 * 179 * USLEEP - if defined, on devices that aren't disconnecting from the 180 * bus, we will go to sleep so that the CPU can get real work done 181 * when we run a command that won't complete immediately. 182 * 183 * Note that if USLEEP is defined, NCR5380_TIMER *must* also be 184 * defined. 185 * 186 * Defaults for these will be provided if USLEEP is defined, although 187 * the user may want to adjust these to allocate CPU resources to 188 * the SCSI driver or "real" code. 189 * 190 * USLEEP_SLEEP - amount of time, in jiffies, to sleep 191 * 192 * USLEEP_POLL - amount of time, in jiffies, to poll 193 * 194 * These macros MUST be defined : 195 * NCR5380_local_declare() - declare any local variables needed for your transfer 196 * routines. 197 * 198 * NCR5380_setup(instance) - initialize any local variables needed from a given 199 * instance of the host adapter for NCR5380_{read,write,pread,pwrite} 200 * 201 * NCR5380_read(register) - read from the specified register 202 * 203 * NCR5380_write(register, value) - write to the specific register 204 * 205 * NCR5380_implementation_fields - additional fields needed for this 206 * specific implementation of the NCR5380 207 * 208 * Either real DMA *or* pseudo DMA may be implemented 209 * REAL functions : 210 * NCR5380_REAL_DMA should be defined if real DMA is to be used. 211 * NCR5380_dma_write_setup(instance, src, count) - initialize 212 * NCR5380_dma_read_setup(instance, dst, count) - initialize 213 * NCR5380_dma_residual(); - residual count 214 * 215 * PSEUDO functions : 216 * NCR5380_pwrite(instance, src, count) 217 * NCR5380_pread(instance, dst, count); 218 * 219 * If nothing specific to this implementation needs doing (ie, with external 220 * hardware), you must also define 221 * 222 * NCR5380_queue_command 223 * NCR5380_reset 224 * NCR5380_abort 225 * 226 * to be the global entry points into the specific driver, ie 227 * #define NCR5380_queue_command t128_queue_command. 228 * 229 * If this is not done, the routines will be defined as static functions 230 * with the NCR5380* names and the user must provide a globally 231 * accessable wrapper function. 232 * 233 * The generic driver is initialized by calling NCR5380_init(instance), 234 * after setting the appropriate host specific fields and ID. If the 235 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance, 236 * possible) function may be used. Before the specific driver initialization 237 * code finishes, NCR5380_print_options should be called. 238 */ 239
240 staticstructScsi_Host *first_instance = NULL;
241 staticScsi_Host_Template *the_template = NULL;
242
243 /* 244 * Function : void initialize_SCp(Scsi_Cmnd *cmd) 245 * 246 * Purpose : initialize the saved data pointers for cmd to point to the 247 * start of the buffer. 248 * 249 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset. 250 */ 251
252 static__inline__voidinitialize_SCp(Scsi_Cmnd *cmd) {/* */ 253 /* 254 * Initialize the Scsi Pointer field so that all of the commands in the 255 * various queues are valid. 256 */ 257
258 if (cmd->use_sg) { 259 cmd->SCp.buffer = (structscatterlist *) cmd->buffer;
260 cmd->SCp.buffers_residual = cmd->use_sg - 1;
261 cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
262 cmd->SCp.this_residual = cmd->SCp.buffer->length;
263 }else{ 264 cmd->SCp.buffer = NULL;
265 cmd->SCp.buffers_residual = 0;
266 cmd->SCp.ptr = (char *) cmd->request_buffer;
267 cmd->SCp.this_residual = cmd->request_bufflen;
268 } 269 } 270
271 #include <linux/delay.h>
272
273 #ifdefNDEBUG 274 staticstruct{ 275 unsignedcharmask;
276 char * name;} 277 signals[] = {{SR_DBP, "PARITY"}, {SR_RST, "RST" }, {SR_BSY, "BSY" },
278 {SR_REQ, "REQ" }, {SR_MSG, "MSG" }, {SR_CD, "CD" }, {SR_IO, "IO" },
279 {SR_SEL, "SEL" }, {0, NULL}},
280 basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
281 icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
282 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
283 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
284 {0, NULL}},
285 mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
286 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
287 "MODE PARITY INTR"}, {MR_MONITOR_BSY, "MODE MONITOR BSY"},
288 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
289 {0, NULL}};
290
291 /* 292 * Function : void NCR5380_print(struct Scsi_Host *instance) 293 * 294 * Purpose : print the SCSI bus signals for debugging purposes 295 * 296 * Input : instance - which NCR5380 297 */ 298
299 staticvoidNCR5380_print(structScsi_Host *instance) {/* */ 300 NCR5380_local_declare();
301 unsignedcharstatus, data, basr, mr, icr, i;
302 NCR5380_setup(instance);
303 cli();
304 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
305 status = NCR5380_read(STATUS_REG);
306 mr = NCR5380_read(MODE_REG);
307 icr = NCR5380_read(INITIATOR_COMMAND_REG);
308 basr = NCR5380_read(BUS_AND_STATUS_REG);
309 sti();
310 for (i = 0; signals[i].mask ; ++i)
311 if (status & signals[i].mask)
312 printk(" %s", signals[i].name);
313 for (i = 0; basrs[i].mask ; ++i)
314 if (basr & basrs[i].mask)
315 printk(" %s", basrs[i].name);
316 for (i = 0; icrs[i].mask; ++i)
317 if (icr & icrs[i].mask)
318 printk(" %s", icrs[i].name);
319 for (i = 0; mrs[i].mask; ++i)
320 if (mr & mrs[i].mask)
321 printk(" %s", mrs[i].name);
322 printk("\n");
323 } 324
325 staticstruct{ 326 unsignedcharvalue;
327 char *name;
328 }phases[] = { 329 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
330 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
331 {PHASE_UNKNOWN, "UNKNOWN"}};
332
333 /* 334 * Function : void NCR5380_print_phase(struct Scsi_Host *instance) 335 * 336 * Purpose : print the current SCSI phase for debugging purposes 337 * 338 * Input : instance - which NCR5380 339 */ 340
341 staticvoidNCR5380_print_phase(structScsi_Host *instance) {/* */ 342 NCR5380_local_declare();
343 unsignedcharstatus;
344 inti;
345 NCR5380_setup(instance);
346
347 status = NCR5380_read(STATUS_REG);
348 if (!(status & SR_REQ))
349 printk("scsi%d : REQ not asserted, phase unknown.\n",
350 instance->host_no);
351 else{ 352 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
353 (phases[i].value != (status & PHASE_MASK)); ++i);
354 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
355 } 356 } 357 #endif 358
359 /* 360 * We need to have our corroutine active given these constraints : 361 * 1. The mutex flag, main_running, can only be set when the main 362 * routine can actually process data, otherwise SCSI commands 363 * will never get issued. 364 * 365 * 2. NCR5380_main() shouldn't be called before it has exited, because 366 * other drivers have had kernel stack overflows in similar 367 * situations. 368 * 369 * 3. We don't want to inline NCR5380_main() because of space concerns, 370 * even though it is only called in two places. 371 * 372 * So, the solution is to set the mutex in an inline wrapper for the 373 * main corroutine, and have the main corroutine exit with interrupts 374 * disabled after the final search through the queues so that no race 375 * conditions are possible. 376 */ 377
378 staticvolatileintmain_running = 0;
379
380 /* 381 * Function : run_main(void) 382 * 383 * Purpose : insure that the coroutine is running and will process our 384 * request. main_running is checked/set here (in an inline function) 385 * rather than in NCR5380_main itself to reduce the chances of stack 386 * overflow. 387 * 388 */ 389
390 static__inline__voidrun_main(void) {/* */ 391 cli();
392 if (!main_running) { 393 main_running = 1;
394 NCR5380_main();
395 /* 396 * main_running is cleared in NCR5380_main once it can't do 397 * more work, and NCR5380_main exits with interrupts disabled. 398 */ 399 sti();
400 }else 401 sti();
402 } 403
404 #ifdefUSLEEP 405 #ifndefNCR5380_TIMER 406 #error "NCR5380_TIMER must be defined so that this type of NCR5380 driver gets a unique timer."
407 #endif 408
409 /* 410 * These need tweaking, and would probably work best as per-device 411 * flags initialized differently for disk, tape, cd, etc devices. 412 * People with broken devices are free to experiment as to what gives 413 * the best results for them. 414 * 415 * USLEEP_SLEEP should be a minimum seek time. 416 * 417 * USLEEP_POLL should be a maximum rotational latency. 418 */ 419 #ifndefUSLEEP_SLEEP 420 /* 20 ms (reasonable hard disk speed) */ 421 #defineUSLEEP_SLEEP 2
422 #endif 423 /* 300 RPM (floppy speed) */ 424 #ifndefUSLEEP_POLL 425 #defineUSLEEP_POLL 20
426 #endif 427
428 staticstructScsi_Host * expires_first = NULL;
429
430 /* 431 * Function : int should_disconnect (unsigned char cmd) 432 * 433 * Purpose : decide weather a commmand would normally disconnect or 434 * not, since if it won't disconnect we should go to sleep. 435 * 436 * Input : cmd - opcode of SCSI command 437 * 438 * Returns : DISCONNECT_LONG if we should disconnect for a really long 439 * time (ie always, sleep, look for REQ active, sleep), 440 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal 441 * time-to-data dealy, DISCONNECT_NONE if this command would return 442 * immediately. 443 * 444 * Future sleep algorithms based on time to data can exploit 445 * something like this so they can differentiate between "normal" 446 * (ie, read, write, seek) and unusual commands (ie, * format). 447 * 448 * Note : We don't deal with commands that handle an immediate disconnect, 449 * 450 */ 451
452 staticintshould_disconnect (unsignedcharcmd) {/* */ 453 switch (cmd) { 454 caseREAD_6:
455 caseWRITE_6:
456 caseSEEK_6:
457 caseREAD_10:
458 caseWRITE_10:
459 caseSEEK_10:
460 returnDISCONNECT_TIME_TO_DATA;
461 caseFORMAT_UNIT:
462 caseSEARCH_HIGH:
463 caseSEARCH_LOW:
464 caseSEARCH_EQUAL:
465 returnDISCONNECT_LONG;
466 default:
467 returnDISCONNECT_NONE;
468 } 469 } 470
471 /* 472 * Assumes instance->time_expires has been set in higher level code. 473 */ 474
475 staticintNCR5380_set_timer (structScsi_Host *instance) {/* */ 476 structScsi_Host *tmp, **prev;
477
478 cli();
479 if (((structNCR5380_hostdata *) (instance->host_data))->next_timer) { 480 sti();
481 return -1;
482 } 483
484 for (prev = &expires_first, tmp = expires_first; tmp;
485 prev = &(((structNCR5380_hostdata *) tmp->host_data)->next_timer),
486 tmp = ((structNCR5380_hostdata *) tmp->host_data)->next_timer)
487 if (instance->time_expires < tmp->time_expires)
488 break;
489
490 instance->next_timer = tmp;
491 *prev = instance;
492 timer_table[NCR5380_TIMER].expires = expires_first->time_expires;
493 timer_active |= 1 << NCR5380_TIMER;
494 sti;
495 return 0;
496 } 497
498 /* Doing something about unwanted rentrancy here might be useful */ 499 voidNCR5380_timer_fn(void) {/* */ 500 structScsi_Host *instance;
501 cli();
502 for (; expires_first && expires_first->time_expires >= jiffies; ) { 503 instance = ((NCR5380_hostdata *) expires_first->host_data)->
504 expires_next;
505 ((NCR5380_hostdata *) expires_first->host_data)->expires_next =
506 NULL;
507 ((NCR5380_hostdata *) expires_first->host_data)->time_expires =
508 0;
509 expires_first = instance;
510 } 511
512 if (expires_first) { 513 timer_table[NCR5380_TIMER].expires = ((NCR5380_hostdata *)
514 expires_first->host_data)->time_expires;
515 timer_active |= (1 << NCR5380_TIMER);
516 }else{ 517 timer_table[NCR5380_TIMER].expires = 0;
518 timer_active &= ~(1 << MCR5380_TIMER);
519 } 520 sti();
521
522 run_main();
523 } 524 #endif/* def USLEEP */ 525
526 staticvoidNCR5380_all_init (void) {/* */ 527 staticintdone = 0;
528 if (!done) { 529 #if (NDEBUG & NDEBUG_INIT)
530 printk("scsi : NCR5380_all_init()\n");
531 #endif 532 done = 1;
533 #ifdefUSLEEP 534 timer_table[NCR5380_TIMER].expires = 0;
535 timer_table[NCR5380_TIMER].fn = NCR5380_timer_fn;
536 #endif 537 } 538 } 539
540 #ifdefAUTOPROBE_IRQ 541 /* 542 * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible) 543 * 544 * Purpose : autoprobe for the IRQ line used by the NCR5380. 545 * 546 * Inputs : instance - pointer to this instance of the NCR5380 driver, 547 * possible - bitmask of permissable interrupts. 548 * 549 * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired. 550 * 551 * XXX no effort is made to deal with spurious interrupts. 552 */ 553
554
555 staticintprobe_irq;
556 staticvoidprobe_intr (intsig) {/* */ 557 probe_irq = sig;
558 };
559 staticstructsigactionprobe_sigaction = {probe_intr, 0, SA_INTERRUPT,
560 NULL};
561
562 staticintNCR5380_probe_irq (structScsi_Host *instance, intpossible) {/* */ 563 NCR5380_local_declare();
564 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
565 instance->hostdata;
566 unsignedlongtimeout;
567 inttrying_irqs, i, mask;
568 NCR5380_setup(instance);
569
570 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
571 if ((mask & possible) && (irqaction (i, &probe_sigaction)
572 == 0))
573 trying_irqs |= mask;
574
575 timeout = jiffies + 25;
576 probe_irq = IRQ_NONE;
577
578 /* 579 * A interrupt is triggered whenever BSY = false, SEL = true 580 * and a bit set in the SELECT_ENABLE_REG is asserted on the 581 * SCSI bus. 582 * 583 * Note that the bus is only driven when the phase control signals 584 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that 585 * to zero. 586 */ 587
588 NCR5380_write(TARGET_COMMAND_REG, 0);
589 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
590 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
591 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
592 ICR_ASSERT_SEL);
593
594 while (probe_irq == IRQ_NONE && jiffies < timeout);
595
596 NCR5380_write(SELECT_ENABLE_REG, 0);
597 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
598
599 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
600 if (trying_irqs & mask)
601 free_irq(i);
602
603 returnprobe_irq;
604 } 605 #endif/* AUTOPROBE_IRQ */ 606
607 /* 608 * Function : void NCR58380_print_options (struct Scsi_Host *instance) 609 * 610 * Purpose : called by probe code indicating the NCR5380 driver 611 * options that were selected. 612 * 613 * Inputs : instance, pointer to this instance. Unused. 614 */ 615
616 staticvoidNCR5380_print_options (structScsi_Host *instance) {/* */ 617 printk(" generic options"
618 #ifdefAUTOPROBE_IRQ 619 " AUTOPROBE_IRQ"
620 #endif 621 #ifdefAUTOSENSE 622 " AUTOSENSE"
623 #endif 624 #ifdefDIFFERENTIAL 625 " DIFFERENTIAL"
626 #endif 627 #ifdef REALDMA
628 " REAL DMA"
629 #endif 630 #ifdefPARITY 631 " PARITY"
632 #endif 633 #ifdefPSEUDO_DMA 634 " PSEUDO DMA"
635 #endif 636 #ifdefSCSI2 637 " SCSI-2"
638 #endif 639 #ifdefUNSAFE 640 " UNSAFE "
641 #endif 642 );
643 #ifdefUSLEEP 644 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
645 #endif 646 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
647 } 648
649
650 /* 651 * Function : void NCR5380_init (struct Scsi_Host *instance) 652 * 653 * Purpose : initializies *instance and corresponding 5380 chip. 654 * 655 * Inputs : instance - instantiation of the 5380 driver. 656 * 657 * Notes : I assume that the host, hostno, and id bits have been 658 * set correctly. I don't care about the irq and other fields. 659 * 660 */ 661
662 staticvoidNCR5380_init (structScsi_Host *instance) {/* */ 663 NCR5380_local_declare();
664 inti;
665 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
666 instance->hostdata;
667 NCR5380_setup(instance);
668
669 NCR5380_all_init();
670
671 hostdata->id_mask = 1 << instance->this_id;
672 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
673 if (i > hostdata->id_mask)
674 hostdata->id_higher_mask |= i;
675 for (i = 0; i < 8; ++i)
676 hostdata->busy[i] = 0;
677 #ifdefREAL_DMA 678 hostdata->dmalen = 0;
679 #endif 680 hostdata->connected = NULL;
681 hostdata->issue_queue = NULL;
682 hostdata->disconnected_queue = NULL;
683
684 if (!the_template) { 685 the_template = instance->hostt;
686 first_instance = instance;
687 } 688
689
690 #ifdefUSLEEP 691 hostdata->time_expires = 0;
692 hostdata->next_timer = NULL;
693 #endif 694
695 #ifndefAUTOSENSE 696 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1))
697 printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
698 " without AUTOSENSE option, contigent alligence conditions may\n"
699 " be incorrectly cleared.\n", instance->host_no);
700 #endif/* def AUTOSENSE */ 701
702 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
703 NCR5380_write(MODE_REG, MR_BASE);
704 NCR5380_write(TARGET_COMMAND_REG, 0);
705 NCR5380_write(SELECT_ENABLE_REG, 0);
706 } 707
708 /* 709 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd, 710 * void (*done)(Scsi_Cmnd *)) 711 * 712 * Purpose : enqueues a SCSI command 713 * 714 * Inputs : cmd - SCSI command, done - function called on completion, with 715 * a pointer to the command descriptor. 716 * 717 * Returns : 0 718 * 719 * Side effects : 720 * cmd is added to the per instance issue_queue, with minor 721 * twiddling done to the host specific fields of cmd. If the 722 * main coroutine is not running, it is restarted. 723 * 724 */ 725
726 /* Only make static if a wrapper function is used */ 727 #ifndefNCR5380_queue_command 728 static 729 #endif 730 intNCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *)) {/* */ 731 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
732 cmd->host->hostdata;
733 Scsi_Cmnd *tmp;
734
735 #if (NDEBUG & NDEBUG_NO_WRITE)
736 switch (cmd->cmnd[0]) { 737 caseWRITE:
738 caseWRITE_10:
739 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
740 instance->host_no);
741 cmd->result = (DID_ERROR << 16);
742 done(cmd);
743 return 0;
744 } 745 #endif/* (NDEBUG & NDEBUG_NO_WRITE) */ 746
747
748 /* 749 * We use the host_scribble field as a pointer to the next command 750 * in a queue 751 */ 752
753 cmd->host_scribble = NULL;
754 cmd->scsi_done = done;
755
756 cmd->result = 0;
757
758
759 /* 760 * Insert the cmd into the issue queue. Note that REQUEST SENSE 761 * commands are added to the head of the queue since any command will 762 * clear the contingent allegience condition that exists and the 763 * sense data is only guranteed to be valid while the condition exists. 764 */ 765
766 cli();
767 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) { 768 cmd->host_scribble = (unsignedchar *) hostdata->issue_queue;
769 hostdata->issue_queue = cmd;
770 }else{ 771 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
772 tmp = (Scsi_Cmnd *) tmp->host_scribble);
773 tmp->host_scribble = (unsignedchar *) cmd;
774 } 775 #if (NDEBUG & NDEBUG_QUEUES)
776 printk("scsi%d : command added to %s of queue\n", instance->host_no,
777 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
778 #endif 779
780 /* Run the coroutine if it isn't allready running. */ 781 run_main();
782 return 0;
783 } 784
785 /* 786 * Function : NCR5380_main (void) 787 * 788 * Purpose : NCR5380_main is a corroutine that runs as long as more work can 789 * be done on the NCR5380 host adapters in a system. Both 790 * NCR5380_queue_command() and NCR5380_intr() will try to start it 791 * in case it is not running. 792 * 793 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 794 * reenable them. This prevents rentrancy and kernel stack overflow. 795 */ 796
797 staticvoidNCR5380_main (void) {/* */ 798 Scsi_Cmnd *tmp, *prev;
799 structScsi_Host *instance;
800 structNCR5380_hostdata *hostdata;
801 intdone;
802
803 /* 804 * We run (with interrupts disabled) until we're sure that none of 805 * the host adapters have anything that can be done, at which point 806 * we set main_running to 0 and exit. 807 * 808 * Interrupts are enabled before doing various other internal 809 * instructions, after we've decided that we need to run through 810 * the loop again. 811 * 812 * this should prevent any race conditions. 813 */ 814
815 do{ 816 cli(); /* Freeze request queues */ 817 done = 1;
818 for (instance = first_instance; instance && instance->hostt == the_template;
819 instance=instance->next) { 820 hostdata = (structNCR5380_hostdata *) instance->hostdata;
821 cli();
822 if (!hostdata->connected) { 823 #if (NDEBUG & NDEBUG_MAIN)
824 printk("scsi%d : not connected\n", instance->host_no);
825 #endif 826 /* 827 * Search through the issue_queue for a command destined 828 * for a target that's not busy. 829 */ 830 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
831 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
832 tmp->host_scribble)
833
834 /* When we find one, remove it from the issue queue. */ 835 if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) { 836 if (prev)
837 prev->host_scribble = tmp->host_scribble;
838 else 839 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
840 tmp->host_scribble = NULL;
841
842 /* renable interrupts after finding one */ 843 sti();
844
845 /* 846 * Attempt to establish an I_T_L nexus here. 847 * On success, instance->hostdata->connected is set. 848 * On failure, we must add the command back to the 849 * issue queue so we can keep trying. 850 */ 851 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
852 printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
853 instance->host_no, tmp->target, tmp->lun);
854 #endif 855 /* 856 * REQUEST SENSE commands are issued without tagged 857 * queueing, even on SCSI-II devices because the 858 * contingent alligence condition exists for the 859 * entire unit. 860 */ 861
862 if (!NCR5380_select(instance, tmp,
863 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
864 TAG_NEXT)) { 865 break;
866 }else{ 867 cli();
868 tmp->host_scribble = (unsignedchar *)
869 hostdata->issue_queue;
870 hostdata->issue_queue = tmp;
871 sti();
872 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
873 printk("scsi%d : main(): select() failed, returned to issue_queue\n",
874 instance->host_no);
875 #endif 876 } 877 }/* if target/lun is not busy */ 878 }/* if (!hostdata->connected) */ 879
880 if (hostdata->connected 881 #ifdefREAL_DMA 882 && !hostdata->dmalen 883 #endif 884 #ifdefUSLEEP 885 && (!hostdata->time_expires || hostdata->time_expires >= jiffies)
886 #endif 887 ) { 888 sti();
889 #if (NDEBUG & NDEBUG_MAIN)
890 printk("scsi%d : main() : performing information transfer\n",
891 instance->host_no);
892 #endif 893 NCR5380_information_transfer(instance);
894 #if (NDEBUG & NDEBUG_MAIN)
895 printk("scsi%d : main() : done set false\n", instance->host_no);
896 #endif 897 done = 0;
898 }else 899 break;
900 }/* for instance */ 901 }while (!done);
902 main_running = 0;
903 } 904
905 /* 906 * Function : void NCR5380_intr (int irq) 907 * 908 * Purpose : handle interrupts, restablishing I_T_L or I_T_L_Q nexuses 909 * from the disconnected queue, and restarting NCR5380_main() 910 * as required. 911 * 912 * Inputs : int irq, irq that caused this interrupt. 913 * 914 */ 915
916 staticvoidNCR5380_intr (intirq) {/* */ 917 NCR5380_local_declare();
918 structScsi_Host *instance;
919 intdone;
920 #if (NDEBUG & NDEBUG_INTR)
921 printk("scsi : NCR5380 irq %d triggered\n", irq);
922 #endif 923 do{ 924 done = 1;
925 for (instance = first_instance; instance && (instance->hostt ==
926 the_template); instance = instance->next)
927 if (instance->irq == irq) { 928
929 /* Look for pending interrupts */ 930 NCR5380_setup(instance);
931 /* XXX dispatch to appropriate routine if found and done=0 */ 932 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) { 933 #if (NDEBUG & NDEBUG_INTR)
934 NCR5380_print(instance);
935 #endif 936 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
937 (SR_SEL | SR_IO)) { 938 done = 0;
939 sti();
940 #if (NDEBUG & NDEBUG_INTR)
941 printk("scsi%d : SEL interrupt\n", instance->host_no);
942 #endif 943 NCR5380_reselect(instance);
944 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
945 }elseif (NCR5380_read(BUS_AND_STATUS_REG) &
946 BASR_PARITY_ERROR) { 947 #if (NDEBUG & NDEBUG_INTR)
948 printk("scsi%d : PARITY interrupt\n", instance->host_no);
949 #endif 950 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
951 }else{ 952 /* 953 * XXX the rest of the interrupt conditions should *only* occur during a 954 * DMA transfer, which I haven't gotten arround to fixing yet. 955 */ 956
957 #ifdefined(REAL_DMA)
958 #else 959 #if (NDEBUG & NDEBUG_INTR)
960 printk("scsi : unknown interrupt\n");
961 #endif 962 #endif 963 } 964 }/* if BASR_IRQ */ 965 if (!done)
966 run_main();
967 }/* if (instance->irq == irq) */ 968 }while (!done);
969 } 970
971 /* 972 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 973 * int tag); 974 * 975 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command, 976 * including ARBITRATION, SELECTION, and initial message out for 977 * IDENTIFY and queue messages. 978 * 979 * Inputs : instance - instantiation of the 5380 driver on which this 980 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 981 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 982 * the command that is presently connected. 983 * 984 * Returns : -1 if selection could not execute for some reason, 985 * 0 if selection succeeeded or failed because the target 986 * did not respond. 987 * 988 * Side effects : 989 * If bus busy, arbitration failed, etc, NCR5380_select() will exit 990 * with registers as they should have been on entry - ie 991 * SELECT_ENABLE will be set appropriately, the NCR5380 992 * will cease to drive any SCSI bus signals. 993 * 994 * If successful : I_T_L or I_T_L_Q nexus will be established, 995 * instance->connected will be set to cmd. 996 * SELECT interrupt will be disabled. 997 * 998 * If failed (no target) : cmd->scsi_done() will be called, and the 999 * cmd->result host byte set to DID_BAD_TARGET.1000 */1001
1002 staticintNCR5380_select (structScsi_Host *instance, Scsi_Cmnd *cmd,
/* */1003 inttag) {1004 NCR5380_local_declare();
1005 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata*)
1006 instance->hostdata;
1007 unsignedchartmp[3], phase;
1008 unsignedchar *data;
1009 intlen;
1010 unsignedlongtimeout;
1011 NCR5380_setup(instance);
1012
1013 #ifdefined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION)
1014 NCR5380_print(instance);
1015 printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1016 instance->this_id);
1017 #endif1018
1019 /* 1020 * Set the phase bits to 0, otherwise the NCR5380 won't drive the 1021 * data bus during SELECTION.1022 */1023
1024 NCR5380_write(TARGET_COMMAND_REG, 0);
1025
1026 /* Start arbitration */1027 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1028 NCR5380_write(MODE_REG, MR_ARBITRATE);
1029
1030 /* Wait for arbitration logic to complete */1031 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1032
1033 #if (NDEBUG & NDEBUG_ARBITRATION)
1034 printk("scsi%d : arbitration complete\n", instance->host_no);
1035 /* Avoid GCC 2.4.5 asm needs to many reloads error */1036 __asm__("nop");
1037 #endif1038
1039 /* 1040 * The arbitration delay is 2.2us, but this is a minimum and there is 1041 * no maximum so we can safely sleep for ceil(2.2) usecs to accomodate1042 * the integral nature of udelay().1043 *1044 */1045
1046 udelay(3);
1047
1048 /* Check for lost arbitration */1049 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1050 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1051 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {1052 NCR5380_write(MODE_REG, MR_BASE);
1053 #if (NDEBUG & NDEBUG_ARBITRATION)
1054 printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n",
1055 instance->host_no);
1056 #endif1057 return -1;
1058 }1059
1060 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1061
1062 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) {1063 NCR5380_write(MODE_REG, MR_BASE);
1064 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1065 #if (NDEBUG & NDEBUG_ARBITRATION)
1066 printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n",
1067 instance->host_no);
1068 #endif1069 return -1;
1070 }1071
1072 /* 1073 * Again, bus clear + bus settle time is 1.2us, however, this is 1074 * a minimum so we'll udelay ceil(1.2)1075 */1076
1077 udelay(2);
1078
1079 #if (NDEBUG & NDEBUG_ARBITRATION)
1080 printk("scsi%d : won arbitration\n", instance->host_no);
1081 #endif1082
1083 /* 1084 * Now that we have won arbitration, start Selection process, asserting 1085 * the host and target ID's on the SCSI bus.1086 */1087
1088 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1089
1090 /* 1091 * Raise ATN while SEL is true before BSY goes false from arbitration,1092 * since this is the only way to gurantee that we'll get a MESSAGE OUT1093 * phase immediately after selection.1094 */1095
1096 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1097 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1098 NCR5380_write(MODE_REG, MR_BASE);
1099
1100 /* 1101 * Reselect interrupts must be turned off prior to the dropping of BSY,1102 * otherwise we will trigger an interrupt.1103 */1104 NCR5380_write(SELECT_ENABLE_REG, 0);
1105
1106 /* Reset BSY */1107 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1108 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1109
1110 /* 1111 * Something wierd happens when we cease to drive BSY - looks1112 * like the board/chip is letting us do another read before the 1113 * appropriate propogation delay has expired, and we're confusing1114 * a BSY signal from ourselves as the target's response to SELECTION.1115 *1116 * A small delay (the 'C++' frontend breaks the pipeline with an1117 * unecessary jump, making it work on my 386-33/Trantor T128, the1118 * tighter 'C' code breaks and requires this) solves the problem - 1119 * the 1 us delay is arbitrary, and only used because this delay will 1120 * be the same on other platforms and since it works here, it should 1121 * work there.1122 */1123
1124 udelay(1);
1125
1126 #if (NDEBUG & NDEBUG_SELECTION)
1127 printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1128 #endif1129
1130 /* 1131 * The SCSI specification calls for a 250 ms timeout for the actual 1132 * selection.1133 */1134
1135 timeout = jiffies + 25;
1136
1137 /* 1138 * XXX very interesting - we're seeing a bounce where the BSY we 1139 * asserted is being reflected / still asserted (propogation delay?)1140 * and it's detecting as true. Sigh.1141 */1142
1143 while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
1144
1145 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1146
1147 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {1148 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1149 cmd->result = DID_BAD_TARGET << 16;
1150 cmd->scsi_done(cmd);
1151 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1152 #if (NDEBUG & NDEBUG_SELECTION)
1153 printk("scsi%d : target did not respond within 250ms\n",
1154 instance->host_no);
1155 #endif1156 return 0;
1157 }1158
1159 /*1160 * Since we followed the SCSI spec, and raised ATN while SEL 1161 * was true but before BSY was false during selection, the information1162 * transfer phase should be a MESSAGE OUT phase so that we can send the1163 * IDENTIFY message.1164 * 1165 * If SCSI-II tagged queing is enabled, we also send a SIMPLE_QUEUE_TAG1166 * message (2 bytes) with a tag ID that we increment with every command1167 * until it wraps back to 0.1168 *1169 * XXX - it turns out that there are some broken SCSI-II devices,1170 * which claim to support tagged queing but fail when more than1171 * some number of commands are issued at once.1172 */1173
1174 /* Wait for start of REQ/ACK handshake */1175 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1176
1177 #if (NDEBUG & NDEBUG_SELECTION)
1178 printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1179 instance->host_no, cmd->target);
1180 #endif1181 tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1182 #ifdefSCSI21183 if (scsi_devices[cmd->index].tagged_queue && (tag != TAG_NONE)) {1184 tmp[1] = SIMPLE_QUEUE_TAG;
1185 if (tag == TAG_NEXT) {1186 /* 0 is TAG_NONE, used to imply no tag for this command */1187 if (scsi_devices[cmd->index].current_tag == 0)
1188 scsi_devices[cmd->index].current_tag = 1;
1189
1190 cmd->tag = scsi_devices[cmd->index].current_tag;
1191 scsi_devices[cmd->index].current_tag++;
1192 }else1193 cmd->tag = (unsignedchar) tag;
1194
1195 tmp[2] = cmd->tag;
1196 hostdata->last_message = SIMPLE_QUEUE_TAG;
1197 len = 3;
1198 }else1199 #endif/* def SCSI2 */1200 {1201 len = 1;
1202 cmd->tag=0;
1203 }1204
1205 /* Send message(s) */1206 data = tmp;
1207 phase = PHASE_MSGOUT;
1208 NCR5380_transfer_pio(instance, &phase, &len, &data);
1209 #if (NDEBUG & NDEBUG_SELECTION)
1210 printk("scsi%d : nexus established.\n", instance->host_no);
1211 #endif1212 /* XXX need to handle errors here */1213 hostdata->connected = cmd;
1214 #ifdefSCSI21215 if (!scsi_devices[cmd->index].tagged_queue)
1216 #endif1217 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1218
1219 initialize_SCp(cmd);
1220
1221 return 0;
1222 }1223
1224 /* 1225 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 1226 * unsigned char *phase, int *count, unsigned char **data)1227 *1228 * Purpose : transfers data in given phase using polled I/O1229 *1230 * Inputs : instance - instance of driver, *phase - pointer to 1231 * what phase is expected, *count - pointer to number of 1232 * bytes to transfer, **data - pointer to data pointer.1233 * 1234 * Returns : -1 when different phase is enterred without transfering1235 * maximum number of bytes, 0 if all bytes or transfered or exit1236 * is in same phase.1237 *1238 * Also, *phase, *count, *data are modified in place.1239 *1240 * XXX Note : handling for bus free may be useful.1241 */1242
1243 /*1244 * Note : this code is not as quick as it could be, however it 1245 * IS 100% reliable, and for the actual data transfer where speed1246 * counts, we will always do a pseudo DMA or DMA transfer.1247 */1248
1249 staticintNCR5380_transfer_pio (structScsi_Host *instance,
/* */1250 unsignedchar *phase, int *count, unsignedchar **data) {1251 NCR5380_local_declare();
1252 registerunsignedcharp = *phase, tmp;
1253 registerintc = *count;
1254 registerunsignedchar *d = *data;
1255 NCR5380_setup(instance);
1256
1257 /* 1258 * The NCR5380 chip will only drive the SCSI bus when the 1259 * phase specified in the appropriate bits of the TARGET COMMAND1260 * REGISTER match the STATUS REGISTER1261 */1262
1263 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1264
1265 do{1266 /* 1267 * Wait for assertion of REQ, after which the phase bits will be 1268 * valid 1269 */1270 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1271
1272 #if (NDEBUG & NDEBUG_HANDSHAKE)
1273 printk("scsi%d : REQ detected\n", instance->host_no);
1274 #endif1275
1276 /* Check for phase mismatch */1277 if ((tmp & PHASE_MASK) != p) {1278 #if (NDEBUG & NDEBUG_PIO)
1279 printk("scsi%d : phase mismatch\n", instance->host_no);
1280 NCR5380_print_phase(instance);
1281 #endif1282 break;
1283 }1284
1285 /* Do actual transfer from SCSI bus to / from memory */1286 if (!(p & SR_IO))
1287 NCR5380_write(OUTPUT_DATA_REG, *d);
1288 else1289 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1290
1291 ++d;
1292
1293 /* 1294 * The SCSI standard suggests that in MSGOUT phase, the initiator1295 * should drop ATN on the last byte of the message phase1296 * after REQ has been asserted for the handshake but before1297 * the initiator raises ACK.1298 */1299
1300 if (!(p & SR_IO)) {1301 if (!((p & SR_MSG) && c > 1)) {1302 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1303 ICR_ASSERT_DATA);
1304 #if (NDEBUG & NDEBUG_PIO)
1305 NCR5380_print(instance);
1306 #endif1307 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1308 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1309 }else{1310 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1311 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1312 #if (NDEBUG & NDEBUG_PIO)
1313 NCR5380_print(instance);
1314 #endif1315 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1316 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1317 }1318 }else{1319 #if (NDEBUG & NDEBUG_PIO)
1320 NCR5380_print(instance);
1321 #endif1322 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1323 }1324
1325 while (NCR5380_read(STATUS_REG) & SR_REQ);
1326
1327 #if (NDEBUG & NDEBUG_HANDSHAKE)
1328 printk("scsi%d : req false, handshake complete\n", instance->host_no);
1329 #endif1330
1331 if (!(p == PHASE_MSGOUT && c > 1))
1332 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1333 else1334 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1335 }while (--c);
1336
1337 #if (NDEBUG & NDEBUG_PIO)
1338 printk("scsi%d : residual %d\n", instance->host_no, c);
1339 #endif1340
1341 *count = c;
1342 *data = d;
1343 tmp = NCR5380_read(STATUS_REG);
1344 if (tmp & SR_REQ)
1345 *phase = tmp & PHASE_MASK;
1346 else1347 *phase = PHASE_UNKNOWN;
1348
1349 if (!c || (*phase == p))
1350 return 0;
1351 else1352 return -1;
1353 }1354
1355 #ifdefined(REAL_DMA) | defined(PSEUDO_DMA)
1356 /* 1357 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 1358 * unsigned char *phase, int *count, unsigned char **data)1359 *1360 * Purpose : transfers data in given phase using either real1361 * or pseudo DMA.1362 *1363 * Inputs : instance - instance of driver, *phase - pointer to 1364 * what phase is expected, *count - pointer to number of 1365 * bytes to transfer, **data - pointer to data pointer.1366 * 1367 * Returns : -1 when different phase is enterred without transfering1368 * maximum number of bytes, 0 if all bytes or transfered or exit1369 * is in same phase.1370 *1371 * Also, *phase, *count, *data are modified in place.1372 *1373 */1374
1375
1376 staticintNCR5380_transfer_dma (structScsi_Host *instance,
/* */1377 unsignedchar *phase, int *count, unsignedchar **data) {1378 NCR5380_local_declare();
1379 registerintc = *count;
1380 registerunsignedcharp = *phase;
1381 registerunsignedchar *d = *data;
1382 unsignedchartmp;
1383 intfoo;
1384 NCR5380_setup(instance);
1385
1386 #ifdefREAL_DMA1387 instance->dmalen = c;
1388 if (p & SR_IO)
1389 NCR5380_dma_read_setup(d, c);
1390 else1391 NCR5380_dma_write_setup(d, c);
1392 #endif1393
1394
1395 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {1396 *phase = tmp;
1397 return -1;
1398 }1399
1400 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1401
1402 #ifdefREAL_DMA1403 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1404 #else1405 /*1406 * Note : on my sample board, watch-dog timeouts occured when interrupts1407 * were not disabled for the duration of a single DMA transfer, from 1408 * before the setting of DMA mode to after transfer of the last byte.1409 */1410
1411 #ifndefUNSAFE1412 cli();
1413 #endif1414 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1415 #endif1416
1417 if (p & SR_IO)
1418 NCR5380_write(START_DMA_INITIATOR_RECIEVE_REG, 0);
1419 else{1420 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1421 NCR5380_write(START_DMA_SEND_REG, 0);
1422 }1423
1424 #ifdefREAL_DMA1425 return 0;
1426 #else1427 foo = ((p & SR_IO) ? NCR5380_pread(instance, d, c) :
1428 NCR5380_pwrite(instance, d, c));
1429 NCR5380_write(MODE_REG, MR_BASE);
1430 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1431 *data = d + c;
1432 *count = 0;
1433 *phase = (NCR5380_read(STATUS_REG & PHASE_MASK));
1434 #ifndefUNSAFE1435 sti();
1436 #endif1437 returnfoo;
1438 #endif/* def REAL_DMA */1439 }1440 #endif/* defined(REAL_DMA) | defined(PSEUDO_DMA) */1441
1442 /*1443 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)1444 *1445 * Purpose : run through the various SCSI phases and do as the target 1446 * directs us to. Operates on the currently connected command, 1447 * instance->connected.1448 *1449 * Inputs : instance, instance for which we are doing commands1450 *1451 * Side effects : SCSI things happen, the disconnected queue will be 1452 * modified if a command disconnects, *instance->connected will1453 * change.1454 *1455 * XXX Note : we need to watch for bus free or a reset condition here 1456 * to recover from an unexpected bus free condition.1457 */1458
1459 staticvoidNCR5380_information_transfer (structScsi_Host *instance) {/* */1460 NCR5380_local_declare();
1461 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
1462 instance->hostdata;
1463 unsignedcharmsgout = NOP;
1464 intlen;
1465 unsignedchar *data;
1466 unsignedcharphase, tmp, old_phase=0xff;
1467 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
1468 NCR5380_setup(instance);
1469
1470 while (1) {1471 tmp = NCR5380_read(STATUS_REG);
1472 /* We only have a valid SCSI phase when REQ is asserted */1473 if (tmp & SR_REQ) {1474 phase = (tmp & PHASE_MASK);
1475 if (phase != old_phase) {1476 old_phase = phase;
1477 #if (NDEBUG & NDEBUG_INFORMATION)
1478 NCR5380_print_phase(instance);
1479 #endif1480 }1481 switch (phase) {1482 casePHASE_DATAIN:
1483 casePHASE_DATAOUT:
1484 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1485 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
1486 instance->host_no);
1487 msgout = ABORT;
1488 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1489 break;
1490 #endif1491 /* 1492 * If there is no room left in the current buffer in the1493 * scatter-gather list, move onto the next one.1494 */1495
1496 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {1497 ++cmd->SCp.buffer;
1498 --cmd->SCp.buffers_residual;
1499 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1500 cmd->SCp.ptr = cmd->SCp.buffer->address;
1501 #if (NDEBUG & NDEBUG_INFORMATION)
1502 printk("scsi%d : %d bytes and %d buffers left\n",
1503 instance->host_no, cmd->SCp.this_residual,
1504 cmd->SCp.buffers_residual);
1505 #endif1506 }1507
1508 /*1509 * The preffered transfer method is going to be 1510 * PSEUDO-DMA for systems that are strictly PIO,1511 * since we can let the hardware do the handshaking.1512 *1513 * For this to work, we need to know the transfersize1514 * ahead of time, since the pseudo-DMA code will sit1515 * in an unconditional loop.1516 */1517
1518 #if (defined(PSEUDO_DMA))
1519 if (!scsi_devices[cmd->index].borken && cmd->transfersize &&
1520 cmd->SCp.this_residual && !(cmd->SCp.this_residual %
1521 cmd->transfersize)) {1522 len = cmd->transfersize;
1523 if (NCR5380_transfer_dma(instance, &phase,
1524 &len, (unsignedchar **) &cmd->SCp.ptr)) {1525 /*1526 * If the watchdog timer fires, all future accesses to this1527 * device will use the polled-IO.1528 */1529 printk("scsi%d : switching target %d lun %d to slow handshake\n",
1530 instance->host_no, cmd->target, cmd->lun);
1531 scsi_devices[cmd->index].borken = 1;
1532 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1533 ICR_ASSERT_ATN);
1534 msgout = ABORT;
1535 }else1536 cmd->SCp.this_residual -= cmd->transfersize;
1537 }else1538 #endif/* (defined(REAL_DMA) || defined(PSEUDO_DMA)) */1539 NCR5380_transfer_pio(instance, &phase,
1540 (int *) &cmd->SCp.this_residual, (unsignedchar **)
1541 &cmd->SCp.ptr);
1542 break;
1543 casePHASE_MSGIN:
1544 /* 1545 * XXX - we don't handle multi-byte messages here, since we 1546 * shouldn't get them after the I_T_L_Q nexus is established1547 * for tagged queuing, and the host should initiate any 1548 * negotiations for sync. SCSI, etc.1549 */1550
1551 len = 1;
1552 data = &tmp;
1553 NCR5380_transfer_pio(instance, &phase, &len, &data);
1554 cmd->SCp.Message = tmp;
1555
1556 switch (tmp) {1557 /*1558 * Linking lets us reduce the time required to get the 1559 * next command out to the device, hopefully this will1560 * mean we don't waste another revolution due to the delays1561 * required by ARBITRATION and another SELECTION.1562 *1563 * In the current implementation proposal, low level drivers1564 * merely have to start the next command, pointed to by 1565 * next_link, done() is called as with unlinked commands.1566 */1567 #ifdefLINKED1568 caseLINKED_CMD_COMPLETE:
1569 caseLINKED_FLG_CMD_COMPLETE:
1570 #if (NDEBUG & NDEBUG_LINKED)
1571 printk("scsi%d : target %d lun %d linked command complete.\n",
1572 instance->host_no, cmd->target, cmd->lun);
1573 #endif1574 /* 1575 * Sanity check : A linked command should only terminate with1576 * one of these messages if there are more linked commands1577 * available.1578 */1579
1580 if (!cmd->next_link) {1581 printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
1582 instance->host_no, cmd->target, cmd->lun);
1583 msgout = ABORT;
1584 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1585 ICR_ASSERT_ATN);
1586 break;
1587 }1588
1589 initialize_SCp(cmd->next_link);
1590 /* The next command is still part of this process */1591 cmd->next_link->tag = cmd->tag;
1592 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1593 #if (NDEBUG & NDEBUG_LINKED)
1594 printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
1595 instance->host_no, cmd->target, cmd->lun);
1596 #endif1597 cmd->scsi_done(cmd);
1598 cmd = hostdata->connected;
1599 break;
1600 #endif/* def LINKED */1601 caseABORT:
1602 caseCOMMAND_COMPLETE:
1603 hostdata->connected = NULL;
1604 #if (NDEBUG & NDEBUG_QUEUES)
1605 printk("scsi%d : command for target %d, lun %d completed\n",
1606 instance->host_no, cmd->target, cmd->lun);
1607 #endif1608 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1609
1610 /* 1611 * Use the status and message bytes from the original 1612 * command.1613 */1614
1615 if (cmd->cmnd[0] != REQUEST_SENSE)
1616 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1617 elseif (cmd->SCp.Status != GOOD)
1618 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1619 #ifdefAUTOSENSE1620 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
1621 (cmd->SCp.Status == CHECK_CONDITION)) {1622 #if (NDEBUG & NDEBUG_AUTOSENSE)
1623 printk("scsi%d : performing request sense\n",
1624 instance->host_no);
1625 #endif1626 cmd->cmnd[0] = REQUEST_SENSE;
1627 cmd->cmnd[1] &= 0xe0;
1628 cmd->cmnd[2] = 0;
1629 cmd->cmnd[3] = 0;
1630 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
1631 cmd->cmnd[5] = 0;
1632
1633 cmd->SCp.buffer = NULL;
1634 cmd->SCp.buffers_residual = 0;
1635 cmd->SCp.ptr = (char *) cmd->sense_buffer;
1636 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
1637
1638 cli();
1639 cmd->host_scribble = (unsignedchar *)
1640 hostdata->issue_queue;
1641 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
1642 sti();
1643 #if (NDEBUG & NDEBUG_QUEUES)
1644 printk("scsi%d : REQUEST SENSE added to head of issue queue\n");
1645 #endif1646 }else1647 #endif/* def AUTOSENSE */1648 cmd->scsi_done(cmd);
1649
1650 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1651 return;
1652 caseMESSAGE_REJECT:
1653 switch (hostdata->last_message) {1654 caseHEAD_OF_QUEUE_TAG:
1655 caseORDERED_QUEUE_TAG:
1656 caseSIMPLE_QUEUE_TAG:
1657 scsi_devices[cmd->index].tagged_queue = 0;
1658 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1659 break;
1660 default:
1661 break;
1662 }1663 caseDISCONNECT:
1664 scsi_devices[cmd->index].disconnect = 1;
1665 cli();
1666 cmd->host_scribble = (unsignedchar *)
1667 hostdata->disconnected_queue;
1668 hostdata->connected = NULL;
1669 hostdata->disconnected_queue = cmd;
1670 sti();
1671 #if (NDEBUG & NDEBUG_QUEUES)
1672 printk("scsi%d : command for target %d lun %d was moved from connected to"
1673 " the disconnected_queue\n", instance->host_no,
1674 cmd->target, cmd->lun);
1675 #endif1676
1677 /* Enable reselect interupts */1678 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1679 return;
1680 /* 1681 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect1682 * operation, in violation of the SCSI spec so we can safely 1683 * ignore SAVE/RESTORE pointers calls.1684 *1685 * Unfortunately, some disks violate the SCSI spec and 1686 * don't issue the required SAVE_POINTERS message before1687 * disconnecting, and we have to break spec to remain 1688 * compatable.1689 */1690 caseSAVE_POINTERS:
1691 caseRESTORE_POINTERS:
1692 break;
1693 default:
1694 /* 1695 * XXX rejected messages should be handled in the pio data transfer phase,1696 * since ATN should be raised before ACK goes false when we reject a message1697 */1698
1699 #ifdefnotyet1700 /* 1701 * If we get something wierd that we aren't expecting, 1702 * reject it.1703 */1704 msgout = MESSAGE_REJECT;
1705 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1706 ICR_ASSERT_ATN);
1707 #endif1708 break;
1709 }/* switch (tmp) */1710 break;
1711 casePHASE_MSGOUT:
1712 len = 1;
1713 data = &msgout;
1714 hostdata->last_message = msgout;
1715 NCR5380_transfer_pio(instance, &phase, &len, &data);
1716 if (msgout == ABORT) {1717 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1718 hostdata->connected = NULL;
1719 cmd->result = DID_ERROR << 16;
1720 cmd->scsi_done(cmd);
1721 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1722 return;
1723 }1724 msgout = NOP;
1725 break;
1726 casePHASE_CMDOUT:
1727 len = COMMAND_SIZE(cmd->cmnd[0]);
1728 data = cmd->cmnd;
1729 /* 1730 * XXX for performance reasons, on machines with a 1731 * PSEUDO-DMA architecture we should probably 1732 * use the dma transfer function. 1733 */1734 NCR5380_transfer_pio(instance, &phase, &len,
1735 &data);
1736 #ifdefUSLEEP1737 if (!disconnect && should_disconnect(cmd->cmnd[0])) {1738 hostdata->time_expires = jiffies + USLEEP_SLEEP;
1739 #if (NDEBUG & NDEBUG_USLEEP)
1740 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
1741 hostdata->time_expires);
1742 #endif1743 NCR5380_set_timer (instance);
1744 return;
1745 }1746 #endif/* def USLEEP */1747 break;
1748 casePHASE_STATIN:
1749 len = 1;
1750 data = &tmp;
1751 NCR5380_transfer_pio(instance, &phase, &len, &data);
1752 cmd->SCp.Status = tmp;
1753 break;
1754 default:
1755 printk("scsi%d : unknown phase\n", instance->host_no);
1756 #ifdefNDEBUG1757 NCR5380_print(instance);
1758 #endif1759 }/* switch(phase) */1760 }/* if (tmp * SR_REQ) */1761 #ifdefUSLEEP1762 else{1763 if (!disconnect && hostdata->time_expires && jiffies >
1764 hostdata->time_expires) {1765 hostdata->time_expires = jiffies + USLEEP_SLEEP;
1766 #if (NDEBUG & NDEBUG_USLEEP)
1767 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
1768 hostdata->time_expires);
1769 #endif1770 NCR5380_set_timer (instance);
1771 return;
1772 }1773 }1774 #endif1775 }/* while (1) */1776 }1777
1778 /*1779 * Function : void NCR5380_reselect (struct Scsi_Host *instance)1780 *1781 * Purpose : does reselection, initializing the instance->connected 1782 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q 1783 * nexus has been restablished,1784 * 1785 * Inputs : instance - this instance of the NCR5380.1786 *1787 */1788
1789
1790 staticvoidNCR5380_reselect (structScsi_Host *instance) {/* */1791 NCR5380_local_declare();
1792 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
1793 instance->hostdata;
1794 unsignedchartarget_mask;
1795 unsignedcharlun, phase;
1796 intlen;
1797 #ifdefSCSI21798 unsignedchartag;
1799 #endif1800 unsignedcharmsg[3];
1801 unsignedchar *data;
1802 Scsi_Cmnd *tmp = NULL, *prev;
1803 intabort = 0;
1804 NCR5380_setup(instance);
1805
1806 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
1807
1808 #if (NDEBUG & NDEBUG_RESELECTION)
1809 printk("scsi%d : reselect\n", instance->host_no);
1810 #endif1811
1812 /* 1813 * At this point, we have detected that our SCSI ID is on the bus,1814 * SEL is true and BSY was false for at least one bus settle delay1815 * (400 ns).1816 *1817 * We must assert BSY ourselves, until the target drops the SEL1818 * signal.1819 */1820
1821 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
1822 while (NCR5380_read(STATUS_REG) & SR_SEL);
1823 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1824
1825 /*1826 * Wait for target to go into MSGIN.1827 */1828
1829 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1830
1831 len = 3;
1832 data = msg;
1833 phase = PHASE_MSGIN;
1834 NCR5380_transfer_pio(instance, &phase, &len, &data);
1835
1836 #ifdefSCSI21837 /*1838 * If there was no residual from the attempt to transfer three bytes, then1839 * the target sent the one byte IDENTIFY message followed by a two byte1840 * queue message.1841 *1842 * If there were two bytes of residual, we got the IDENTIFY message1843 * only.1844 *1845 * If there was one byte of residual, we got the IDENTIFY message1846 * followed by a RESTORE pointers message (which was ignored - 1847 * see MSGIN phase of the NCR5380_information_transfer() function.1848 */1849
1850 if (!len)
1851 tag = msg[2];
1852 else1853 tag = 0;
1854 #endif1855
1856 if (!msg[0] & 0x80) {1857 printk("scsi%d : expecting IDENTIFY message, got ",
1858 instance->host_no);
1859 print_msg(msg);
1860 abort = 1;
1861 }else{1862
1863 lun = (msg[0] & 0x07);
1864
1865 /* 1866 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we 1867 * just restablished, and remove it from the disconnected queue.1868 */1869
1870 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
1871 tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
1872 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
1873 #ifdefSCSI21874 && (tag == tmp->tag)
1875 #endif1876 ) {1877 if (prev)
1878 prev->host_scribble = tmp->host_scribble;
1879 else1880 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
1881 tmp->host_scribble = NULL;
1882 break;
1883 }1884
1885 if (!tmp) {1886 #ifdefSCSI21887 printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
1888 instance->host_no, target_mask, lun, tag);
1889 #else1890 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
1891 instance->host_no, target_mask, lun);
1892 #endif1893 /* 1894 * Since we have an established nexus that we can't do anything with,1895 * we must abort it. 1896 */1897 abort = 1;
1898 }1899 }1900
1901 if (abort) {1902 msg[0] = ABORT;
1903 len = 1;
1904 data = msg;
1905 phase = PHASE_MSGOUT;
1906 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1907 NCR5380_transfer_pio(instance, &phase, &len, &data);
1908 }else{1909 hostdata->connected = tmp;
1910 #if (NDEBUG & NDEBUG_RESELECTION)
1911 printk"scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
1912 instance->host_no, cmd->target, cmd->lun, cmd->tag);
1913 #endif1914 }1915 }1916
1917 /*1918 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)1919 *1920 * Purpose : called by interrupt handler when DMA finishes or a phase1921 * mismatch occurs (which would finish the DMA transfer). 1922 *1923 * Inputs : instance - this instance of the NCR5380.1924 *1925 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L1926 * nexus has been restablished, on failure NULL is returned.1927 */1928
1929 #ifdefREAL_DMA1930 staticvoid NCR5380_dma_complete (NCR5380_instance *instance) {/* */1931 NCR5380_local_declare();
1932 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *
1933 instance->hostdata);
1934 inttransferred;
1935 NCR5380_setup(instance);
1936
1937 /*1938 * XXX this might not be right.1939 *1940 * Wait for final byte to transfer, ie wait for ACK to go false.1941 *1942 * We should use the Last Byte Sent bit, unfortunately this is 1943 * not available on the 5380/5381 (only the various CMOS chips)1944 */1945
1946 while (NCR5380_read(STATUS_REG) & SR_ACK);
1947
1948 NCR5380_write(MODE_REG, MR_BASE);
1949 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1950
1951 /*1952 * The only places we should see a phase mismatch and have to send1953 * data from the same set of pointers will be the data transfer1954 * phases. So, residual, requested length are only important here.1955 */1956
1957 if (!(hostdata->connected->SCp.phase & SR_CD)) {1958 transferred = instance->dma_len - NCR5380_dma_residual();
1959 hostdata->connected->SCp.this_residual -= transferred;
1960 hostdata->connected->SCp.ptr += transferred;
1961 }1962 }1963 #endif/* def REAL_DMA */1964
1965 /*1966 * Function : int NCR5380_abort (Scsi_Cmnd *cmd, int code)1967 *1968 * Purpose : abort a command1969 *1970 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 1971 * host byte of the result field to, if zero DID_ABORTED is 1972 * used.1973 *1974 * Returns : 0 - success, -1 on failure.1975 *1976 * XXX - there is no way to abort the command that is currently 1977 * connected, you have to wait for it to complete. If this is 1978 * a problem, we could implement longjmp() / setjmp(), setjmp()1979 * called where the loop started in NCR5380_main().1980 */1981
1982 #ifndefNCR5380_abort1983 static1984 #endif1985 intNCR5380_abort (Scsi_Cmnd *cmd, intcode) {/* */1986 NCR5380_local_declare();
1987 structScsi_Host *instance = cmd->host;
1988 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
1989 instance->hostdata;
1990 Scsi_Cmnd *tmp, **prev;
1991 unsignedcharmsg, phase, *msgptr;
1992 intlen;
1993
1994 cli();
1995 NCR5380_setup(instance);
1996
1997 #if (NDEBUG & NDEBUG_ABORT)
1998 printk("scsi%d : abort called\n", instance->host_no);
1999 #endif2000 /* 2001 * Case 1 : If the command hasn't been issued yet, we simply remove it 2002 * from the issue queue.2003 */2004 for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue),
2005 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2006 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp =
2007 (Scsi_Cmnd *) tmp->host_scribble)
2008 if (cmd == tmp) {2009 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2010 tmp->host_scribble = NULL;
2011 tmp->result = (code ? code : DID_ABORT) << 16;
2012 sti();
2013 #if (NDEBUG & NDEBUG_ABORT)
2014 printk("scsi%d : abort removed command from issue queue.\n",
2015 instance->host_no);
2016 #endif2017 tmp->done(tmp);
2018 return 0;
2019 }2020
2021 /* 2022 * Case 2 : If any commands are connected, we're going to fail the abort2023 * and let the high level SCSI driver retry at a later time or 2024 * issue a reset.2025 *2026 * Timeouts, and therefore aborted commands, will be highly unlikely2027 * and handling them cleanly in this situation would make the common2028 * case of noresets less efficient, and would pollute our code. So,2029 * we fail.2030 */2031
2032 if (hostdata->connected) {2033 sti();
2034 #if (NDEBUG & NDEBUG_ABORT)
2035 printk("scsi%d : abort failed, command connected.\n", instance->host_no);
2036 #endif2037 return -1;
2038 }2039
2040 /*2041 * Case 3: If the command is currently disconnected from the bus, and 2042 * there are no connected commands, we reconnect the I_T_L or 2043 * I_T_L_Q nexus associated with it, go into message out, and send 2044 * an abort message.2045 *2046 * This case is especially ugly. In order to resetablish the nexus, we2047 * need to call NCR5380_select(). The easiest way to implement this 2048 * function was to abort if the bus was busy, and let the interrupt2049 * handler triggered on the SEL for reselect take care of lost arbitrations2050 * where necessary, meaning interrupts need to be enabled.2051 *2052 * When interrupts are enabled, the queues may change - so we 2053 * can't remove it from the disconnected queue before selecting it2054 * because that could cause a failure in hashing the nexus if that 2055 * device reselected.2056 * 2057 * Since the queues may change, we can't use the pointers from when we2058 * first locate it.2059 *2060 * So, we must first locate the command, and if NCR5380_select()2061 * succeeds, then issue the abort, relocate the command and remove2062 * it from the disconnected queue.2063 */2064
2065 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2066 tmp = (Scsi_Cmnd *) tmp->host_scribble)
2067 if (cmd == tmp) {2068 sti();
2069 #if (NDEBUG & NDEBUG_ABORT)
2070 printk("scsi%d : aborting disconnected command.\n", instance->host_no);
2071 #endif2072
2073 if (NCR5380_select (instance, cmd, (int) cmd->tag))
2074 return 1;
2075
2076 #if (NDEBUG & NDEBUG_ABORT)
2077 printk("scsi%d : nexus restablished.\n", instance->host_no);
2078 #endif2079
2080 msg = ABORT;
2081 msgptr = &msg;
2082 len = 1;
2083 phase = PHASE_MSGOUT;
2084 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2085 NCR5380_transfer_pio (instance, &phase, &len, &msgptr);
2086
2087 cli();
2088 for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue),
2089 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
2090 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp =
2091 (Scsi_Cmnd *) tmp->host_scribble)
2092 if (cmd == tmp) {2093 *prev = (Scsi_Cmnd *) tmp->host_scribble;
2094 tmp->host_scribble = NULL;
2095 tmp->result = (code ? code : DID_ABORT) << 16;
2096 sti();
2097 tmp->done(tmp);
2098 return 0;
2099 }2100 }2101
2102 /*2103 * Case 4 : If we reached this point, the command was not found in any of 2104 * the queues.2105 *2106 * We probably reached this point because of an unlikely race condition2107 * between the command completing successfully and the abortion code,2108 * so we won't panic, but we will notify the user in case somethign really2109 * broke.2110 */2111
2112 sti();
2113 printk("scsi%d : warning : SCSI command probably completed successfully\n"
2114 " before abortion\n", instance->host_no);
2115 return 0;
2116 }2117
2118
2119 /* 2120 * Function : int NCR5380_reset (struct Scsi_Cmnd *)2121 * 2122 * Purpose : reset the SCSI bus.2123 *2124 * Returns : 02125 */2126
2127 #ifndefNCR5380_reset2128 static2129 #endif2130 intNCR5380_reset (Scsi_Cmnd * SCpnt) {/* */2131 NCR5380_local_declare();
2132 structScsi_Host *instance;
2133 cli();
2134
2135 instance = SCpnt->host;
2136 NCR5380_setup(instance);
2137 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2138 udelay(1);
2139 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2140
2141 sti();
2142 if (SCpnt) SCpnt->flags |= NEEDS_JUMPSTART;
2143 return 0;
2144 }