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 * Note that these routines also work with NR53c400 family chips. 7 * 8 * Copyright 1993, Drew Eckhardt 9 * Visionary Computing 10 * (Unix and Linux consulting and custom programming) 11 * drew@colorado.edu 12 * +1 (303) 666-5836 13 * 14 * DISTRIBUTION REALEASE 4. 15 * 16 * For more information, please consult 17 * 18 * NCR 5380 Family 19 * SCSI Protocol Controller 20 * Databook 21 * 22 * NCR Microelectronics 23 * 1635 Aeroplaza Drive 24 * Colorado Springs, CO 80916 25 * 1+ (719) 578-3400 26 * 1+ (800) 334-5454 27 */ 28
29 /* 30 * $Log: NCR5380.c,v $ 31 * Revision 1.5 1994/01/19 09:14:57 drew 32 * Fixed udelay() hack that was being used on DATAOUT phases 33 * instead of a propper wait for the final handshake. 34 * 35 * Revision 1.4 1994/01/19 06:44:25 drew 36 * *** empty log message *** 37 * 38 * Revision 1.3 1994/01/19 05:24:40 drew 39 * Added support for TCR LAST_BYTE_SENT bit. 40 * 41 * Revision 1.2 1994/01/15 06:14:11 drew 42 * REAL DMA support, bug fixes. 43 * 44 * Revision 1.1 1994/01/15 06:00:54 drew 45 * Initial revision 46 * 47 */ 48
49 /* 50 * Furthur development / testing that should be done : 51 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete 52 * code so that everything does the same thing that's done at the 53 * end of a pseudo-DMA read operation. 54 * 55 * 2. Fix REAL_DMA (interrupt driven, polled works fine) - 56 * basically, transfer size needs to be reduced by one 57 * and the last byte read as is done with PSEUDO_DMA. 58 * 59 * 3. Test USLEEP code 60 * 61 * 4. Test SCSI-II tagged queueing (I have no devices which support 62 * tagged queueing) 63 * 64 * 5. Test linked command handling code after Eric is ready with 65 * the high level code. 66 */ 67
68 #ifndefnotyet 69 #undefLINKED 70 #undefUSLEEP 71 #undefREAL_DMA 72 #endif 73
74 #ifdefREAL_DMA_POLL 75 #undefREAD_OVERRUNS 76 #defineREAD_OVERRUNS 77 #endif 78
79 /* 80 * Design 81 * Issues : 82 * 83 * The other Linux SCSI drivers were written when Linux was Intel PC-only, 84 * and specifically for each board rather than each chip. This makes their 85 * adaptation to platforms like the Mac (Some of which use NCR5380's) 86 * more difficult than it has to be. 87 * 88 * Also, many of the SCSI drivers were written before the command queing 89 * routines were implemented, meaning their implementations of queued 90 * commands were hacked on rather than designed in from the start. 91 * 92 * When I designed the Linux SCSI drivers I figured that 93 * while having two different SCSI boards in a system might be useful 94 * for debugging things, two of the same type wouldn't be used. 95 * Well, I was wrong and a number of users have mailed me about running 96 * multiple high-performance SCSI boards in a server. 97 * 98 * Finally, when I get questions from users, I have no idea what 99 * revision of my driver they are running. 100 * 101 * This driver attempts to address these problems : 102 * This is a generic 5380 driver. To use it on a different platform, 103 * one simply writes appropriate system specific macros (ie, data 104 * transfer - some PC's will use the I/O bus, 68K's must use 105 * memory mapped) and drops this file in their 'C' wrapper. 106 * 107 * As far as command queueing, two queues are maintained for 108 * each 5380 in the system - commands that haven't been issued yet, 109 * and commands that are currently executing. This means that an 110 * unlimited number of commands may be queued, letting 111 * more commands propogate from the higher driver levels giving higher 112 * througput. Note that both I_T_L and I_T_L_Q nexuses are supported, 113 * allowing multiple commands to propogate all the way to a SCSI-II device 114 * while a command is allready executing. 115 * 116 * To solve the multiple-boards-in-the-same-system problem, 117 * there is a separate instance structure for each instance 118 * of a 5380 in the system. So, mutliple NCR5380 drivers will 119 * be able to coexist with appropriate changes to the high level 120 * SCSI code. 121 * 122 * A NCR5380_PUBLIC_REVISION macro is provided, with the release 123 * number (updated for each public release) printed by the 124 * NCR5380_print_options command, which should be called from the 125 * wrapper detect function, so that I know what release of the driver 126 * users are using. 127 * 128 * Issues specific to the NCR5380 : 129 * 130 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead 131 * piece of hardware that requires you to sit in a loop polling for 132 * the REQ signal as long as you are connected. Some devices are 133 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect 134 * while doing long seek operations. 135 * 136 * The workarround for this is to keep track of devices that have 137 * disconnected. If the device hasn't disconnected, for commands that 138 * should disconnect, we do something like 139 * 140 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs } 141 * 142 * Some tweaking of N and M needs to be done. An algorithm based 143 * on "time to data" would give the best results as long as short time 144 * to datas (ie, on the same track) were considered, however these 145 * broken devices are the exception rather than the rule and I'd rather 146 * spend my time optomizing for the normal case. 147 * 148 * Architecture : 149 * 150 * At the heart of the design is a corroutine, NCR5380_main, 151 * which is started when not running by the interrupt handler, 152 * timer, and queue command function. It attempts to establish 153 * I_T_L or I_T_L_Q nexuses by removing the commands from the 154 * issue queue and calling NCR5380_select() if a nexus 155 * is not established. 156 * 157 * Once a nexus is established, the NCR5380_information_transfer() 158 * phase goes through the various phases as instructed by the target. 159 * if the target goes into MSG IN and sends a DISCONNECT message, 160 * the command structure is placed into the per instance disconnected 161 * queue, and NCR5380_main tries to find more work. If USLEEP 162 * was defined, and the target is idle for too long, the system 163 * will try to sleep. 164 * 165 * If a command has disconnected, eventually an interrupt will trigger, 166 * calling NCR5380_intr() which will inturn call NCR5380_reselect 167 * to restablish a nexus. This will run main if necessary. 168 * 169 * On command termination, the done function will be called as 170 * appropriate. 171 * 172 * SCSI pointers are maintained in the SCp field of SCSI command 173 * structures, being initialized after the command is connected 174 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer. 175 * Note that in violation of the standard, an implicit SAVE POINTERS operation 176 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS. 177 */ 178
179 /* 180 * Using this file : 181 * This file a skeleton Linux SCSI driver for the NCR 5380 series 182 * of chips. To use it, you write a architecture specific functions 183 * and macros and include this file in your driver. 184 * 185 * These macros control options : 186 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be 187 * defined. 188 * 189 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically 190 * for commands that return with a CHECK CONDITION status. 191 * 192 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential 193 * tracievers. 194 * 195 * LINKED - if defined, linked commands are supported. 196 * 197 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases. 198 * 199 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases. 200 * 201 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't 202 * rely on phase mismatch and EOP interrupts to determine end 203 * of phase. 204 * 205 * SCSI2 - if defined, SCSI-2 tagged queing is used where possible 206 * 207 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You 208 * only really want to use this if you're having a problem with 209 * dropped characters during high speed communications, and even 210 * then, you're going to be better off twiddling with transfersize 211 * in the high level code. 212 * 213 * USLEEP - if defined, on devices that aren't disconnecting from the 214 * bus, we will go to sleep so that the CPU can get real work done 215 * when we run a command that won't complete immediately. 216 * 217 * Note that if USLEEP is defined, NCR5380_TIMER *must* also be 218 * defined. 219 * 220 * Defaults for these will be provided if USLEEP is defined, although 221 * the user may want to adjust these to allocate CPU resources to 222 * the SCSI driver or "real" code. 223 * 224 * USLEEP_SLEEP - amount of time, in jiffies, to sleep 225 * 226 * USLEEP_POLL - amount of time, in jiffies, to poll 227 * 228 * These macros MUST be defined : 229 * NCR5380_local_declare() - declare any local variables needed for your transfer 230 * routines. 231 * 232 * NCR5380_setup(instance) - initialize any local variables needed from a given 233 * instance of the host adapter for NCR5380_{read,write,pread,pwrite} 234 * 235 * NCR5380_read(register) - read from the specified register 236 * 237 * NCR5380_write(register, value) - write to the specific register 238 * 239 * NCR5380_implementation_fields - additional fields needed for this 240 * specific implementation of the NCR5380 241 * 242 * Either real DMA *or* pseudo DMA may be implemented 243 * REAL functions : 244 * NCR5380_REAL_DMA should be defined if real DMA is to be used. 245 * Note that the DMA setup functions should return the number of bytes 246 * that they were able to program the controller for. 247 * 248 * Also note that generic i386/PC versions of these macros are 249 * available as NCR5380_i386_dma_write_setup, 250 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual. 251 * 252 * NCR5380_dma_write_setup(instance, src, count) - initialize 253 * NCR5380_dma_read_setup(instance, dst, count) - initialize 254 * NCR5380_dma_residual(instance); - residual count 255 * 256 * PSEUDO functions : 257 * NCR5380_pwrite(instance, src, count) 258 * NCR5380_pread(instance, dst, count); 259 * 260 * If nothing specific to this implementation needs doing (ie, with external 261 * hardware), you must also define 262 * 263 * NCR5380_queue_command 264 * NCR5380_reset 265 * NCR5380_abort 266 * 267 * to be the global entry points into the specific driver, ie 268 * #define NCR5380_queue_command t128_queue_command. 269 * 270 * If this is not done, the routines will be defined as static functions 271 * with the NCR5380* names and the user must provide a globally 272 * accessable wrapper function. 273 * 274 * The generic driver is initialized by calling NCR5380_init(instance), 275 * after setting the appropriate host specific fields and ID. If the 276 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance, 277 * possible) function may be used. Before the specific driver initialization 278 * code finishes, NCR5380_print_options should be called. 279 */ 280
281 staticstructScsi_Host *first_instance = NULL;
282 staticScsi_Host_Template *the_template = NULL;
283
284 /* 285 * Function : void initialize_SCp(Scsi_Cmnd *cmd) 286 * 287 * Purpose : initialize the saved data pointers for cmd to point to the 288 * start of the buffer. 289 * 290 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset. 291 */ 292
293 static__inline__voidinitialize_SCp(Scsi_Cmnd *cmd) {/* */ 294 /* 295 * Initialize the Scsi Pointer field so that all of the commands in the 296 * various queues are valid. 297 */ 298
299 if (cmd->use_sg) { 300 cmd->SCp.buffer = (structscatterlist *) cmd->buffer;
301 cmd->SCp.buffers_residual = cmd->use_sg - 1;
302 cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
303 cmd->SCp.this_residual = cmd->SCp.buffer->length;
304 }else{ 305 cmd->SCp.buffer = NULL;
306 cmd->SCp.buffers_residual = 0;
307 cmd->SCp.ptr = (char *) cmd->request_buffer;
308 cmd->SCp.this_residual = cmd->request_bufflen;
309 } 310 } 311
312 #include <linux/delay.h>
313
314 #ifdefNDEBUG 315 staticstruct{ 316 unsignedcharmask;
317 char * name;} 318 signals[] = {{SR_DBP, "PARITY"}, {SR_RST, "RST" }, {SR_BSY, "BSY" },
319 {SR_REQ, "REQ" }, {SR_MSG, "MSG" }, {SR_CD, "CD" }, {SR_IO, "IO" },
320 {SR_SEL, "SEL" }, {0, NULL}},
321 basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
322 icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
323 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
324 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
325 {0, NULL}},
326 mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
327 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
328 "MODE PARITY INTR"}, {MR_MONITOR_BSY, "MODE MONITOR BSY"},
329 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
330 {0, NULL}};
331
332 /* 333 * Function : void NCR5380_print(struct Scsi_Host *instance) 334 * 335 * Purpose : print the SCSI bus signals for debugging purposes 336 * 337 * Input : instance - which NCR5380 338 */ 339
340 staticvoidNCR5380_print(structScsi_Host *instance) {/* */ 341 NCR5380_local_declare();
342 unsignedcharstatus, data, basr, mr, icr, i;
343 NCR5380_setup(instance);
344 cli();
345 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
346 status = NCR5380_read(STATUS_REG);
347 mr = NCR5380_read(MODE_REG);
348 icr = NCR5380_read(INITIATOR_COMMAND_REG);
349 basr = NCR5380_read(BUS_AND_STATUS_REG);
350 sti();
351 for (i = 0; signals[i].mask ; ++i)
352 if (status & signals[i].mask)
353 printk(" %s", signals[i].name);
354 for (i = 0; basrs[i].mask ; ++i)
355 if (basr & basrs[i].mask)
356 printk(" %s", basrs[i].name);
357 for (i = 0; icrs[i].mask; ++i)
358 if (icr & icrs[i].mask)
359 printk(" %s", icrs[i].name);
360 for (i = 0; mrs[i].mask; ++i)
361 if (mr & mrs[i].mask)
362 printk(" %s", mrs[i].name);
363 printk("\n");
364 } 365
366 staticstruct{ 367 unsignedcharvalue;
368 char *name;
369 }phases[] = { 370 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
371 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
372 {PHASE_UNKNOWN, "UNKNOWN"}};
373
374 /* 375 * Function : void NCR5380_print_phase(struct Scsi_Host *instance) 376 * 377 * Purpose : print the current SCSI phase for debugging purposes 378 * 379 * Input : instance - which NCR5380 380 */ 381
382 staticvoidNCR5380_print_phase(structScsi_Host *instance) {/* */ 383 NCR5380_local_declare();
384 unsignedcharstatus;
385 inti;
386 NCR5380_setup(instance);
387
388 status = NCR5380_read(STATUS_REG);
389 if (!(status & SR_REQ))
390 printk("scsi%d : REQ not asserted, phase unknown.\n",
391 instance->host_no);
392 else{ 393 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
394 (phases[i].value != (status & PHASE_MASK)); ++i);
395 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
396 } 397 } 398 #endif 399
400 /* 401 * We need to have our corroutine active given these constraints : 402 * 1. The mutex flag, main_running, can only be set when the main 403 * routine can actually process data, otherwise SCSI commands 404 * will never get issued. 405 * 406 * 2. NCR5380_main() shouldn't be called before it has exited, because 407 * other drivers have had kernel stack overflows in similar 408 * situations. 409 * 410 * 3. We don't want to inline NCR5380_main() because of space concerns, 411 * even though it is only called in two places. 412 * 413 * So, the solution is to set the mutex in an inline wrapper for the 414 * main corroutine, and have the main corroutine exit with interrupts 415 * disabled after the final search through the queues so that no race 416 * conditions are possible. 417 */ 418
419 staticvolatileintmain_running = 0;
420
421 /* 422 * Function : run_main(void) 423 * 424 * Purpose : insure that the coroutine is running and will process our 425 * request. main_running is checked/set here (in an inline function) 426 * rather than in NCR5380_main itself to reduce the chances of stack 427 * overflow. 428 * 429 */ 430
431 static__inline__voidrun_main(void) {/* */ 432 cli();
433 if (!main_running) { 434 main_running = 1;
435 NCR5380_main();
436 /* 437 * main_running is cleared in NCR5380_main once it can't do 438 * more work, and NCR5380_main exits with interrupts disabled. 439 */ 440 sti();
441 }else 442 sti();
443 } 444
445 #ifdefUSLEEP 446 #ifndefNCR5380_TIMER 447 #error "NCR5380_TIMER must be defined so that this type of NCR5380 driver gets a unique timer."
448 #endif 449
450 /* 451 * These need tweaking, and would probably work best as per-device 452 * flags initialized differently for disk, tape, cd, etc devices. 453 * People with broken devices are free to experiment as to what gives 454 * the best results for them. 455 * 456 * USLEEP_SLEEP should be a minimum seek time. 457 * 458 * USLEEP_POLL should be a maximum rotational latency. 459 */ 460 #ifndefUSLEEP_SLEEP 461 /* 20 ms (reasonable hard disk speed) */ 462 #defineUSLEEP_SLEEP 2
463 #endif 464 /* 300 RPM (floppy speed) */ 465 #ifndefUSLEEP_POLL 466 #defineUSLEEP_POLL 20
467 #endif 468
469 staticstructScsi_Host * expires_first = NULL;
470
471 /* 472 * Function : int should_disconnect (unsigned char cmd) 473 * 474 * Purpose : decide weather a commmand would normally disconnect or 475 * not, since if it won't disconnect we should go to sleep. 476 * 477 * Input : cmd - opcode of SCSI command 478 * 479 * Returns : DISCONNECT_LONG if we should disconnect for a really long 480 * time (ie always, sleep, look for REQ active, sleep), 481 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal 482 * time-to-data dealy, DISCONNECT_NONE if this command would return 483 * immediately. 484 * 485 * Future sleep algorithms based on time to data can exploit 486 * something like this so they can differentiate between "normal" 487 * (ie, read, write, seek) and unusual commands (ie, * format). 488 * 489 * Note : We don't deal with commands that handle an immediate disconnect, 490 * 491 */ 492
493 staticintshould_disconnect (unsignedcharcmd) {/* */ 494 switch (cmd) { 495 caseREAD_6:
496 caseWRITE_6:
497 caseSEEK_6:
498 caseREAD_10:
499 caseWRITE_10:
500 caseSEEK_10:
501 returnDISCONNECT_TIME_TO_DATA;
502 caseFORMAT_UNIT:
503 caseSEARCH_HIGH:
504 caseSEARCH_LOW:
505 caseSEARCH_EQUAL:
506 returnDISCONNECT_LONG;
507 default:
508 returnDISCONNECT_NONE;
509 } 510 } 511
512 /* 513 * Assumes instance->time_expires has been set in higher level code. 514 */ 515
516 staticintNCR5380_set_timer (structScsi_Host *instance) {/* */ 517 structScsi_Host *tmp, **prev;
518
519 cli();
520 if (((structNCR5380_hostdata *) (instance->host_data))->next_timer) { 521 sti();
522 return -1;
523 } 524
525 for (prev = &expires_first, tmp = expires_first; tmp;
526 prev = &(((structNCR5380_hostdata *) tmp->host_data)->next_timer),
527 tmp = ((structNCR5380_hostdata *) tmp->host_data)->next_timer)
528 if (instance->time_expires < tmp->time_expires)
529 break;
530
531 instance->next_timer = tmp;
532 *prev = instance;
533 timer_table[NCR5380_TIMER].expires = expires_first->time_expires;
534 timer_active |= 1 << NCR5380_TIMER;
535 sti;
536 return 0;
537 } 538
539 /* Doing something about unwanted rentrancy here might be useful */ 540 voidNCR5380_timer_fn(void) {/* */ 541 structScsi_Host *instance;
542 cli();
543 for (; expires_first && expires_first->time_expires >= jiffies; ) { 544 instance = ((NCR5380_hostdata *) expires_first->host_data)->
545 expires_next;
546 ((NCR5380_hostdata *) expires_first->host_data)->expires_next =
547 NULL;
548 ((NCR5380_hostdata *) expires_first->host_data)->time_expires =
549 0;
550 expires_first = instance;
551 } 552
553 if (expires_first) { 554 timer_table[NCR5380_TIMER].expires = ((NCR5380_hostdata *)
555 expires_first->host_data)->time_expires;
556 timer_active |= (1 << NCR5380_TIMER);
557 }else{ 558 timer_table[NCR5380_TIMER].expires = 0;
559 timer_active &= ~(1 << MCR5380_TIMER);
560 } 561 sti();
562
563 run_main();
564 } 565 #endif/* def USLEEP */ 566
567 staticvoidNCR5380_all_init (void) {/* */ 568 staticintdone = 0;
569 if (!done) { 570 #if (NDEBUG & NDEBUG_INIT)
571 printk("scsi : NCR5380_all_init()\n");
572 #endif 573 done = 1;
574 #ifdefUSLEEP 575 timer_table[NCR5380_TIMER].expires = 0;
576 timer_table[NCR5380_TIMER].fn = NCR5380_timer_fn;
577 #endif 578 } 579 } 580
581 #ifdefAUTOPROBE_IRQ 582 /* 583 * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible) 584 * 585 * Purpose : autoprobe for the IRQ line used by the NCR5380. 586 * 587 * Inputs : instance - pointer to this instance of the NCR5380 driver, 588 * possible - bitmask of permissable interrupts. 589 * 590 * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired. 591 * 592 * XXX no effort is made to deal with spurious interrupts. 593 */ 594
595
596 staticintprobe_irq;
597 staticvoidprobe_intr (intsig) {/* */ 598 probe_irq = sig;
599 };
600 staticstructsigactionprobe_sigaction = {probe_intr, 0, SA_INTERRUPT,
601 NULL};
602
603 staticintNCR5380_probe_irq (structScsi_Host *instance, intpossible) {/* */ 604 NCR5380_local_declare();
605 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
606 instance->hostdata;
607 unsignedlongtimeout;
608 inttrying_irqs, i, mask;
609 NCR5380_setup(instance);
610
611 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
612 if ((mask & possible) && (irqaction (i, &probe_sigaction)
613 == 0))
614 trying_irqs |= mask;
615
616 timeout = jiffies + 25;
617 probe_irq = IRQ_NONE;
618
619 /* 620 * A interrupt is triggered whenever BSY = false, SEL = true 621 * and a bit set in the SELECT_ENABLE_REG is asserted on the 622 * SCSI bus. 623 * 624 * Note that the bus is only driven when the phase control signals 625 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that 626 * to zero. 627 */ 628
629 NCR5380_write(TARGET_COMMAND_REG, 0);
630 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
631 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
632 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
633 ICR_ASSERT_SEL);
634
635 while (probe_irq == IRQ_NONE && jiffies < timeout);
636
637 NCR5380_write(SELECT_ENABLE_REG, 0);
638 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
639
640 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
641 if (trying_irqs & mask)
642 free_irq(i);
643
644 returnprobe_irq;
645 } 646 #endif/* AUTOPROBE_IRQ */ 647
648 /* 649 * Function : void NCR58380_print_options (struct Scsi_Host *instance) 650 * 651 * Purpose : called by probe code indicating the NCR5380 driver 652 * options that were selected. 653 * 654 * Inputs : instance, pointer to this instance. Unused. 655 */ 656
657 staticvoidNCR5380_print_options (structScsi_Host *instance) {/* */ 658 printk(" generic options"
659 #ifdefAUTOPROBE_IRQ 660 " AUTOPROBE_IRQ"
661 #endif 662 #ifdefAUTOSENSE 663 " AUTOSENSE"
664 #endif 665 #ifdefDIFFERENTIAL 666 " DIFFERENTIAL"
667 #endif 668 #ifdefREAL_DMA 669 " REAL DMA"
670 #endif 671 #ifdefREAL_DMA_POLL 672 " REAL DMA POLL"
673 #endif 674 #ifdefPARITY 675 " PARITY"
676 #endif 677 #ifdefPSEUDO_DMA 678 " PSEUDO DMA"
679 #endif 680 #ifdefSCSI2 681 " SCSI-2"
682 #endif 683 #ifdefUNSAFE 684 " UNSAFE "
685 #endif 686 );
687 #ifdefUSLEEP 688 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
689 #endif 690 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
691 } 692
693
694 /* 695 * Function : void NCR5380_init (struct Scsi_Host *instance) 696 * 697 * Purpose : initializies *instance and corresponding 5380 chip. 698 * 699 * Inputs : instance - instantiation of the 5380 driver. 700 * 701 * Notes : I assume that the host, hostno, and id bits have been 702 * set correctly. I don't care about the irq and other fields. 703 * 704 */ 705
706 staticvoidNCR5380_init (structScsi_Host *instance) {/* */ 707 NCR5380_local_declare();
708 inti;
709 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
710 instance->hostdata;
711 NCR5380_setup(instance);
712
713 NCR5380_all_init();
714
715 hostdata->id_mask = 1 << instance->this_id;
716 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
717 if (i > hostdata->id_mask)
718 hostdata->id_higher_mask |= i;
719 for (i = 0; i < 8; ++i)
720 hostdata->busy[i] = 0;
721 #ifdefREAL_DMA 722 hostdata->dmalen = 0;
723 #endif 724 hostdata->connected = NULL;
725 hostdata->issue_queue = NULL;
726 hostdata->disconnected_queue = NULL;
727 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
728
729 if (!the_template) { 730 the_template = instance->hostt;
731 first_instance = instance;
732 } 733
734
735 #ifdefUSLEEP 736 hostdata->time_expires = 0;
737 hostdata->next_timer = NULL;
738 #endif 739
740 #ifndefAUTOSENSE 741 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1))
742 printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
743 " without AUTOSENSE option, contigent alligence conditions may\n"
744 " be incorrectly cleared.\n", instance->host_no);
745 #endif/* def AUTOSENSE */ 746
747 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
748 NCR5380_write(MODE_REG, MR_BASE);
749 NCR5380_write(TARGET_COMMAND_REG, 0);
750 NCR5380_write(SELECT_ENABLE_REG, 0);
751 } 752
753 /* 754 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd, 755 * void (*done)(Scsi_Cmnd *)) 756 * 757 * Purpose : enqueues a SCSI command 758 * 759 * Inputs : cmd - SCSI command, done - function called on completion, with 760 * a pointer to the command descriptor. 761 * 762 * Returns : 0 763 * 764 * Side effects : 765 * cmd is added to the per instance issue_queue, with minor 766 * twiddling done to the host specific fields of cmd. If the 767 * main coroutine is not running, it is restarted. 768 * 769 */ 770
771 /* Only make static if a wrapper function is used */ 772 #ifndefNCR5380_queue_command 773 static 774 #endif 775 intNCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *)) {/* */ 776 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
777 cmd->host->hostdata;
778 Scsi_Cmnd *tmp;
779
780 #if (NDEBUG & NDEBUG_NO_WRITE)
781 switch (cmd->cmnd[0]) { 782 caseWRITE:
783 caseWRITE_10:
784 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
785 instance->host_no);
786 cmd->result = (DID_ERROR << 16);
787 done(cmd);
788 return 0;
789 } 790 #endif/* (NDEBUG & NDEBUG_NO_WRITE) */ 791
792
793 /* 794 * We use the host_scribble field as a pointer to the next command 795 * in a queue 796 */ 797
798 cmd->host_scribble = NULL;
799 cmd->scsi_done = done;
800
801 cmd->result = 0;
802
803
804 /* 805 * Insert the cmd into the issue queue. Note that REQUEST SENSE 806 * commands are added to the head of the queue since any command will 807 * clear the contingent allegience condition that exists and the 808 * sense data is only guranteed to be valid while the condition exists. 809 */ 810
811 cli();
812 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) { 813 cmd->host_scribble = (unsignedchar *) hostdata->issue_queue;
814 hostdata->issue_queue = cmd;
815 }else{ 816 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
817 tmp = (Scsi_Cmnd *) tmp->host_scribble);
818 tmp->host_scribble = (unsignedchar *) cmd;
819 } 820 #if (NDEBUG & NDEBUG_QUEUES)
821 printk("scsi%d : command added to %s of queue\n", instance->host_no,
822 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
823 #endif 824
825 /* Run the coroutine if it isn't allready running. */ 826 run_main();
827 return 0;
828 } 829
830 /* 831 * Function : NCR5380_main (void) 832 * 833 * Purpose : NCR5380_main is a corroutine that runs as long as more work can 834 * be done on the NCR5380 host adapters in a system. Both 835 * NCR5380_queue_command() and NCR5380_intr() will try to start it 836 * in case it is not running. 837 * 838 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 839 * reenable them. This prevents rentrancy and kernel stack overflow. 840 */ 841
842 staticvoidNCR5380_main (void) {/* */ 843 Scsi_Cmnd *tmp, *prev;
844 structScsi_Host *instance;
845 structNCR5380_hostdata *hostdata;
846 intdone;
847
848 /* 849 * We run (with interrupts disabled) until we're sure that none of 850 * the host adapters have anything that can be done, at which point 851 * we set main_running to 0 and exit. 852 * 853 * Interrupts are enabled before doing various other internal 854 * instructions, after we've decided that we need to run through 855 * the loop again. 856 * 857 * this should prevent any race conditions. 858 */ 859
860 do{ 861 cli(); /* Freeze request queues */ 862 done = 1;
863 for (instance = first_instance; instance && instance->hostt == the_template;
864 instance=instance->next) { 865 hostdata = (structNCR5380_hostdata *) instance->hostdata;
866 cli();
867 if (!hostdata->connected) { 868 #if (NDEBUG & NDEBUG_MAIN)
869 printk("scsi%d : not connected\n", instance->host_no);
870 #endif 871 /* 872 * Search through the issue_queue for a command destined 873 * for a target that's not busy. 874 */ 875 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
876 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
877 tmp->host_scribble)
878
879 /* When we find one, remove it from the issue queue. */ 880 if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) { 881 if (prev)
882 prev->host_scribble = tmp->host_scribble;
883 else 884 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
885 tmp->host_scribble = NULL;
886
887 /* renable interrupts after finding one */ 888 sti();
889
890 /* 891 * Attempt to establish an I_T_L nexus here. 892 * On success, instance->hostdata->connected is set. 893 * On failure, we must add the command back to the 894 * issue queue so we can keep trying. 895 */ 896 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
897 printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
898 instance->host_no, tmp->target, tmp->lun);
899 #endif 900 /* 901 * REQUEST SENSE commands are issued without tagged 902 * queueing, even on SCSI-II devices because the 903 * contingent alligence condition exists for the 904 * entire unit. 905 */ 906
907 if (!NCR5380_select(instance, tmp,
908 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
909 TAG_NEXT)) { 910 break;
911 }else{ 912 cli();
913 tmp->host_scribble = (unsignedchar *)
914 hostdata->issue_queue;
915 hostdata->issue_queue = tmp;
916 sti();
917 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
918 printk("scsi%d : main(): select() failed, returned to issue_queue\n",
919 instance->host_no);
920 #endif 921 } 922 }/* if target/lun is not busy */ 923 }/* if (!hostdata->connected) */ 924
925 if (hostdata->connected 926 #ifdefREAL_DMA 927 && !hostdata->dmalen 928 #endif 929 #ifdefUSLEEP 930 && (!hostdata->time_expires || hostdata->time_expires >= jiffies)
931 #endif 932 ) { 933 sti();
934 #if (NDEBUG & NDEBUG_MAIN)
935 printk("scsi%d : main() : performing information transfer\n",
936 instance->host_no);
937 #endif 938 NCR5380_information_transfer(instance);
939 #if (NDEBUG & NDEBUG_MAIN)
940 printk("scsi%d : main() : done set false\n", instance->host_no);
941 #endif 942 done = 0;
943 }else 944 break;
945 }/* for instance */ 946 }while (!done);
947 main_running = 0;
948 } 949
950 /* 951 * Function : void NCR5380_intr (int irq) 952 * 953 * Purpose : handle interrupts, restablishing I_T_L or I_T_L_Q nexuses 954 * from the disconnected queue, and restarting NCR5380_main() 955 * as required. 956 * 957 * Inputs : int irq, irq that caused this interrupt. 958 * 959 */ 960
961 staticvoidNCR5380_intr (intirq) {/* */ 962 NCR5380_local_declare();
963 structScsi_Host *instance;
964 intdone;
965 unsignedcharbasr;
966 #if (NDEBUG & NDEBUG_INTR)
967 printk("scsi : NCR5380 irq %d triggered\n", irq);
968 #endif 969 do{ 970 done = 1;
971 for (instance = first_instance; instance && (instance->hostt ==
972 the_template); instance = instance->next)
973 if (instance->irq == irq) { 974
975 /* Look for pending interrupts */ 976 NCR5380_setup(instance);
977 basr = NCR5380_read(BUS_AND_STATUS_REG);
978 /* XXX dispatch to appropriate routine if found and done=0 */ 979 if (basr & BASR_IRQ) { 980 #if (NDEBUG & NDEBUG_INTR)
981 NCR5380_print(instance);
982 #endif 983 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
984 (SR_SEL | SR_IO)) { 985 done = 0;
986 sti();
987 #if (NDEBUG & NDEBUG_INTR)
988 printk("scsi%d : SEL interrupt\n", instance->host_no);
989 #endif 990 NCR5380_reselect(instance);
991 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
992 }elseif (basr &
993 BASR_PARITY_ERROR) { 994 #if (NDEBUG & NDEBUG_INTR)
995 printk("scsi%d : PARITY interrupt\n", instance->host_no);
996 #endif 997 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
998 }else{ 999 /* 1000 * XXX the rest of the interrupt conditions should *only* occur during a 1001 * DMA transfer, which I haven't gotten arround to fixing yet.1002 */1003
1004 #ifdefined(REAL_DMA)
1005 /*1006 * We should only get PHASE MISMATCH and EOP interrupts1007 * if we have DMA enabled, so do a sanity check based on1008 * the current setting of the MODE register.1009 */1010
1011 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr &
1012 BASR_END_DMA_TRANSFER) ||
1013 !(basr & BASR_PHASE_MATCH))) {1014 inttransfered;
1015
1016 if (!hostdata->connected)
1017 panic("scsi%d : recieved end of DMA interrupt with no connected cmd\n",
1018 instance->hostno);
1019
1020 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1021 hostdata->connected->SCp.this_residual -= transferred;
1022 hostdata->connected->SCp.ptr += transferred;
1023 hostdata->dmalen = 0;
1024
1025 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1026
1027 while (NCR5380_read(BUS_AND_STATUS_REG) &
1028 BASR_ACK));
1029
1030 NCR5380_write(MODE_REG, MR_BASE);
1031 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1032 }1033 #else1034 #if (NDEBUG & NDEBUG_INTR)
1035 printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1036 #endif1037 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1038 #endif1039 }1040 }/* if BASR_IRQ */1041 if (!done)
1042 run_main();
1043 }/* if (instance->irq == irq) */1044 }while (!done);
1045 }1046
1047 /* 1048 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 1049 * int tag);1050 *1051 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,1052 * including ARBITRATION, SELECTION, and initial message out for 1053 * IDENTIFY and queue messages. 1054 *1055 * Inputs : instance - instantiation of the 5380 driver on which this 1056 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 1057 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 1058 * the command that is presently connected.1059 * 1060 * Returns : -1 if selection could not execute for some reason,1061 * 0 if selection succeeeded or failed because the target 1062 * did not respond.1063 *1064 * Side effects : 1065 * If bus busy, arbitration failed, etc, NCR5380_select() will exit 1066 * with registers as they should have been on entry - ie1067 * SELECT_ENABLE will be set appropriately, the NCR53801068 * will cease to drive any SCSI bus signals.1069 *1070 * If successful : I_T_L or I_T_L_Q nexus will be established, 1071 * instance->connected will be set to cmd. 1072 * SELECT interrupt will be disabled.1073 *1074 * If failed (no target) : cmd->scsi_done() will be called, and the 1075 * cmd->result host byte set to DID_BAD_TARGET.1076 */1077
1078 staticintNCR5380_select (structScsi_Host *instance, Scsi_Cmnd *cmd,
/* */1079 inttag) {1080 NCR5380_local_declare();
1081 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata*)
1082 instance->hostdata;
1083 unsignedchartmp[3], phase;
1084 unsignedchar *data;
1085 intlen;
1086 unsignedlongtimeout;
1087 NCR5380_setup(instance);
1088
1089 #ifdefined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION)
1090 NCR5380_print(instance);
1091 printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1092 instance->this_id);
1093 #endif1094
1095 /* 1096 * Set the phase bits to 0, otherwise the NCR5380 won't drive the 1097 * data bus during SELECTION.1098 */1099
1100 NCR5380_write(TARGET_COMMAND_REG, 0);
1101
1102 /* Start arbitration */1103 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1104 NCR5380_write(MODE_REG, MR_ARBITRATE);
1105
1106 /* Wait for arbitration logic to complete */1107 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1108
1109 #if (NDEBUG & NDEBUG_ARBITRATION)
1110 printk("scsi%d : arbitration complete\n", instance->host_no);
1111 /* Avoid GCC 2.4.5 asm needs to many reloads error */1112 __asm__("nop");
1113 #endif1114
1115 /* 1116 * The arbitration delay is 2.2us, but this is a minimum and there is 1117 * no maximum so we can safely sleep for ceil(2.2) usecs to accomodate1118 * the integral nature of udelay().1119 *1120 */1121
1122 udelay(3);
1123
1124 /* Check for lost arbitration */1125 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1126 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1127 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {1128 NCR5380_write(MODE_REG, MR_BASE);
1129 #if (NDEBUG & NDEBUG_ARBITRATION)
1130 printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n",
1131 instance->host_no);
1132 #endif1133 return -1;
1134 }1135
1136 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1137
1138 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) {1139 NCR5380_write(MODE_REG, MR_BASE);
1140 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1141 #if (NDEBUG & NDEBUG_ARBITRATION)
1142 printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n",
1143 instance->host_no);
1144 #endif1145 return -1;
1146 }1147
1148 /* 1149 * Again, bus clear + bus settle time is 1.2us, however, this is 1150 * a minimum so we'll udelay ceil(1.2)1151 */1152
1153 udelay(2);
1154
1155 #if (NDEBUG & NDEBUG_ARBITRATION)
1156 printk("scsi%d : won arbitration\n", instance->host_no);
1157 #endif1158
1159 /* 1160 * Now that we have won arbitration, start Selection process, asserting 1161 * the host and target ID's on the SCSI bus.1162 */1163
1164 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1165
1166 /* 1167 * Raise ATN while SEL is true before BSY goes false from arbitration,1168 * since this is the only way to gurantee that we'll get a MESSAGE OUT1169 * phase immediately after selection.1170 */1171
1172 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1173 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1174 NCR5380_write(MODE_REG, MR_BASE);
1175
1176 /* 1177 * Reselect interrupts must be turned off prior to the dropping of BSY,1178 * otherwise we will trigger an interrupt.1179 */1180 NCR5380_write(SELECT_ENABLE_REG, 0);
1181
1182 /* Reset BSY */1183 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1184 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1185
1186 /* 1187 * Something wierd happens when we cease to drive BSY - looks1188 * like the board/chip is letting us do another read before the 1189 * appropriate propogation delay has expired, and we're confusing1190 * a BSY signal from ourselves as the target's response to SELECTION.1191 *1192 * A small delay (the 'C++' frontend breaks the pipeline with an1193 * unecessary jump, making it work on my 386-33/Trantor T128, the1194 * tighter 'C' code breaks and requires this) solves the problem - 1195 * the 1 us delay is arbitrary, and only used because this delay will 1196 * be the same on other platforms and since it works here, it should 1197 * work there.1198 */1199
1200 udelay(1);
1201
1202 #if (NDEBUG & NDEBUG_SELECTION)
1203 printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1204 #endif1205
1206 /* 1207 * The SCSI specification calls for a 250 ms timeout for the actual 1208 * selection.1209 */1210
1211 timeout = jiffies + 25;
1212
1213 /* 1214 * XXX very interesting - we're seeing a bounce where the BSY we 1215 * asserted is being reflected / still asserted (propogation delay?)1216 * and it's detecting as true. Sigh.1217 */1218
1219 while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
1220
1221 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1222
1223 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {1224 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1225 cmd->result = DID_BAD_TARGET << 16;
1226 cmd->scsi_done(cmd);
1227 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1228 #if (NDEBUG & NDEBUG_SELECTION)
1229 printk("scsi%d : target did not respond within 250ms\n",
1230 instance->host_no);
1231 #endif1232 return 0;
1233 }1234
1235 /*1236 * Since we followed the SCSI spec, and raised ATN while SEL 1237 * was true but before BSY was false during selection, the information1238 * transfer phase should be a MESSAGE OUT phase so that we can send the1239 * IDENTIFY message.1240 * 1241 * If SCSI-II tagged queing is enabled, we also send a SIMPLE_QUEUE_TAG1242 * message (2 bytes) with a tag ID that we increment with every command1243 * until it wraps back to 0.1244 *1245 * XXX - it turns out that there are some broken SCSI-II devices,1246 * which claim to support tagged queing but fail when more than1247 * some number of commands are issued at once.1248 */1249
1250 /* Wait for start of REQ/ACK handshake */1251 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1252
1253 #if (NDEBUG & NDEBUG_SELECTION)
1254 printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1255 instance->host_no, cmd->target);
1256 #endif1257 tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1258 #ifdefSCSI21259 if (scsi_devices[cmd->index].tagged_queue && (tag != TAG_NONE)) {1260 tmp[1] = SIMPLE_QUEUE_TAG;
1261 if (tag == TAG_NEXT) {1262 /* 0 is TAG_NONE, used to imply no tag for this command */1263 if (scsi_devices[cmd->index].current_tag == 0)
1264 scsi_devices[cmd->index].current_tag = 1;
1265
1266 cmd->tag = scsi_devices[cmd->index].current_tag;
1267 scsi_devices[cmd->index].current_tag++;
1268 }else1269 cmd->tag = (unsignedchar) tag;
1270
1271 tmp[2] = cmd->tag;
1272 hostdata->last_message = SIMPLE_QUEUE_TAG;
1273 len = 3;
1274 }else1275 #endif/* def SCSI2 */1276 {1277 len = 1;
1278 cmd->tag=0;
1279 }1280
1281 /* Send message(s) */1282 data = tmp;
1283 phase = PHASE_MSGOUT;
1284 NCR5380_transfer_pio(instance, &phase, &len, &data);
1285 #if (NDEBUG & NDEBUG_SELECTION)
1286 printk("scsi%d : nexus established.\n", instance->host_no);
1287 #endif1288 /* XXX need to handle errors here */1289 hostdata->connected = cmd;
1290 #ifdefSCSI21291 if (!scsi_devices[cmd->index].tagged_queue)
1292 #endif1293 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1294
1295 initialize_SCp(cmd);
1296
1297 return 0;
1298 }1299
1300 /* 1301 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 1302 * unsigned char *phase, int *count, unsigned char **data)1303 *1304 * Purpose : transfers data in given phase using polled I/O1305 *1306 * Inputs : instance - instance of driver, *phase - pointer to 1307 * what phase is expected, *count - pointer to number of 1308 * bytes to transfer, **data - pointer to data pointer.1309 * 1310 * Returns : -1 when different phase is enterred without transfering1311 * maximum number of bytes, 0 if all bytes or transfered or exit1312 * is in same phase.1313 *1314 * Also, *phase, *count, *data are modified in place.1315 *1316 * XXX Note : handling for bus free may be useful.1317 */1318
1319 /*1320 * Note : this code is not as quick as it could be, however it 1321 * IS 100% reliable, and for the actual data transfer where speed1322 * counts, we will always do a pseudo DMA or DMA transfer.1323 */1324
1325 staticintNCR5380_transfer_pio (structScsi_Host *instance,
/* */1326 unsignedchar *phase, int *count, unsignedchar **data) {1327 NCR5380_local_declare();
1328 registerunsignedcharp = *phase, tmp;
1329 registerintc = *count;
1330 registerunsignedchar *d = *data;
1331 NCR5380_setup(instance);
1332
1333 /* 1334 * The NCR5380 chip will only drive the SCSI bus when the 1335 * phase specified in the appropriate bits of the TARGET COMMAND1336 * REGISTER match the STATUS REGISTER1337 */1338
1339 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1340
1341 do{1342 /* 1343 * Wait for assertion of REQ, after which the phase bits will be 1344 * valid 1345 */1346 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1347
1348 #if (NDEBUG & NDEBUG_HANDSHAKE)
1349 printk("scsi%d : REQ detected\n", instance->host_no);
1350 #endif1351
1352 /* Check for phase mismatch */1353 if ((tmp & PHASE_MASK) != p) {1354 #if (NDEBUG & NDEBUG_PIO)
1355 printk("scsi%d : phase mismatch\n", instance->host_no);
1356 NCR5380_print_phase(instance);
1357 #endif1358 break;
1359 }1360
1361 /* Do actual transfer from SCSI bus to / from memory */1362 if (!(p & SR_IO))
1363 NCR5380_write(OUTPUT_DATA_REG, *d);
1364 else1365 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1366
1367 ++d;
1368
1369 /* 1370 * The SCSI standard suggests that in MSGOUT phase, the initiator1371 * should drop ATN on the last byte of the message phase1372 * after REQ has been asserted for the handshake but before1373 * the initiator raises ACK.1374 */1375
1376 if (!(p & SR_IO)) {1377 if (!((p & SR_MSG) && c > 1)) {1378 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1379 ICR_ASSERT_DATA);
1380 #if (NDEBUG & NDEBUG_PIO)
1381 NCR5380_print(instance);
1382 #endif1383 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1384 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1385 }else{1386 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1387 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1388 #if (NDEBUG & NDEBUG_PIO)
1389 NCR5380_print(instance);
1390 #endif1391 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1392 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1393 }1394 }else{1395 #if (NDEBUG & NDEBUG_PIO)
1396 NCR5380_print(instance);
1397 #endif1398 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1399 }1400
1401 while (NCR5380_read(STATUS_REG) & SR_REQ);
1402
1403 #if (NDEBUG & NDEBUG_HANDSHAKE)
1404 printk("scsi%d : req false, handshake complete\n", instance->host_no);
1405 #endif1406
1407 if (!(p == PHASE_MSGOUT && c > 1))
1408 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1409 else1410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1411 }while (--c);
1412
1413 #if (NDEBUG & NDEBUG_PIO)
1414 printk("scsi%d : residual %d\n", instance->host_no, c);
1415 #endif1416
1417 *count = c;
1418 *data = d;
1419 tmp = NCR5380_read(STATUS_REG);
1420 if (tmp & SR_REQ)
1421 *phase = tmp & PHASE_MASK;
1422 else1423 *phase = PHASE_UNKNOWN;
1424
1425 if (!c || (*phase == p))
1426 return 0;
1427 else1428 return -1;
1429 }1430
1431 #ifdefined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1432 /* 1433 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 1434 * unsigned char *phase, int *count, unsigned char **data)1435 *1436 * Purpose : transfers data in given phase using either real1437 * or pseudo DMA.1438 *1439 * Inputs : instance - instance of driver, *phase - pointer to 1440 * what phase is expected, *count - pointer to number of 1441 * bytes to transfer, **data - pointer to data pointer.1442 * 1443 * Returns : -1 when different phase is enterred without transfering1444 * maximum number of bytes, 0 if all bytes or transfered or exit1445 * is in same phase.1446 *1447 * Also, *phase, *count, *data are modified in place.1448 *1449 */1450
1451
1452 staticintNCR5380_transfer_dma (structScsi_Host *instance,
/* */1453 unsignedchar *phase, int *count, unsignedchar **data) {1454 NCR5380_local_declare();
1455 registerintc = *count;
1456 registerunsignedcharp = *phase;
1457 registerunsignedchar *d = *data;
1458 unsignedchartmp;
1459 intfoo;
1460 #ifdefined(REAL_DMA_POLL)
1461 intcnt, toPIO;
1462 unsignedcharsaved_data = 0, overrun = 0, residue;
1463 #endif1464
1465 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
1466 instance->hostdata;
1467
1468 NCR5380_setup(instance);
1469
1470 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {1471 *phase = tmp;
1472 return -1;
1473 }1474 #ifdefined(REAL_DMA) || defined(REAL_DMA_POLL)
1475 #ifdefREAD_OVERRUNS1476 if (p & SR_IO) {1477 c -= 2;
1478 }1479 #endif1480 #if (NDEBUG & NDEBUG_DMA)
1481 printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
1482 instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
1483 "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
1484 #endif1485 hostdata->dma_len = (p & SR_IO) ?
1486 NCR5380_dma_read_setup(instance, d, c) :
1487 NCR5380_dma_write_setup(instance, d, c);
1488 #endif1489
1490 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1491
1492 #ifdefREAL_DMA1493 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1494 #elifdefined(REAL_DMA_POLL)
1495 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1496 #else1497 /*1498 * Note : on my sample board, watch-dog timeouts occured when interrupts1499 * were not disabled for the duration of a single DMA transfer, from 1500 * before the setting of DMA mode to after transfer of the last byte.1501 */1502
1503 #ifdefined(PSEUDO_DMA) && !defined(UNSAFE)
1504 cli();
1505 #endif1506 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1507 #endif/* def REAL_DMA */1508
1509 #if (NDEBUG & NDEBUG_DMA) & 0
1510 printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
1511 #endif1512
1513 if (p & SR_IO)
1514 NCR5380_write(START_DMA_INITIATOR_RECIEVE_REG, 0);
1515 else{1516 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1517 NCR5380_write(START_DMA_SEND_REG, 0);
1518 }1519
1520 #ifdefined(REAL_DMA_POLL)
1521 do{1522 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1523 }while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR |
1524 BASR_END_DMA_TRANSFER)));
1525
1526 /*1527 At this point, either we've completed DMA, or we have a phase mismatch,1528 or we've unexpectedly lost BUSY (which is a real error).1529
1530 For write DMAs, we want to wait until the last byte has been1531 transferred out over the bus before we turn off DMA mode. Alas, there1532 seems to be no terribly good way of doing this on a 5380 under all1533 conditions. For non-scatter-gather operations, we can wait until REQ1534 and ACK both go false, or until a phase mismatch occurs. Gather-writes1535 are nastier, since the device will be expecting more data than we1536 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we1537 could test LAST BIT SENT to assure transfer (I imagine this is precisely1538 why this signal was added to the newer chips) but on the older 538[01]1539 this signal does not exist. The workaround for this lack is a watchdog;1540 we bail out of the wait-loop after a modest amount of wait-time if1541 the usual exit conditions are not met. Not a terribly clean or1542 correct solution :-%1543
1544 Reads are equally tricky due to a nasty characteristic of the NCR5380.1545 If the chip is in DMA mode for an READ, it will respond to a target's1546 REQ by latching the SCSI data into the INPUT DATA register and asserting1547 ACK, even if it has _already_ been notified by the DMA controller that1548 the current DMA transfer has completed! If the NCR5380 is then taken1549 out of DMA mode, this already-acknowledged byte is lost.1550
1551 This is not a problem for "one DMA transfer per command" reads, because1552 the situation will never arise... either all of the data is DMA'ed1553 properly, or the target switches to MESSAGE IN phase to signal a1554 disconnection (either operation bringing the DMA to a clean halt).1555 However, in order to handle scatter-reads, we must work around the1556 problem. The chosen fix is to DMA N-2 bytes, then check for the1557 condition before taking the NCR5380 out of DMA mode. One or two extra1558 bytes are tranferred via PIO as necessary to fill out the original1559 request.1560 */1561
1562 if (p & SR_IO) {1563 #ifdefREAD_OVERRUNS1564 udelay(10);
1565 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH|BASR_ACK)) ==
1566 (BASR_PHASE_MATCH | BASR_ACK))) {1567 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1568 overrun = 1;
1569 }1570 #endif1571 }else{1572 intlimit = 100;
1573 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
1574 (NCR5380_read(STATUS_REG) & SR_REQ)) {1575 if (!(tmp & BASR_PHASE_MATCH)) break;
1576 if (--limit < 0) break;
1577 }1578 }1579
1580
1581 #if (NDEBUG & NDEBUG_DMA)
1582 printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
1583 instance->host_no, tmp, NCR5380_read(STATUS_REG));
1584 #endif1585
1586 NCR5380_write(MODE_REG, MR_BASE);
1587 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1588
1589 residue = NCR5380_dma_residual(instance);
1590 c -= residue;
1591 *count -= c;
1592 *data += c;
1593 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1594
1595 #ifdefREAD_OVERRUNS1596 if (*phase == p && (p & SR_IO) && residue == 0) {1597 if (overrun) {1598 #if (NDEBUG & NDEBUG_DMA)
1599 printk("Got an input overrun, using saved byte\n");
1600 #endif1601 **data = saved_data;
1602 *data += 1;
1603 *count -= 1;
1604 cnt = toPIO = 1;
1605 }else{1606 printk("No overrun??\n");
1607 cnt = toPIO = 2;
1608 }1609 #if (NDEBUG & NDEBUG_DMA)
1610 printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
1611 #endif1612 NCR5380_transfer_pio(instance, phase, &cnt, data);
1613 *count -= toPIO - cnt;
1614 }1615 #endif1616
1617 #if (NDEBUG & NDEBUG_DMA)
1618 printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
1619 *data, *count, *(*data+*count-1), *(*data+*count));
1620 #endif1621 return 0;
1622
1623 #elifdefined(REAL_DMA)
1624 return 0;
1625 #else/* defined(REAL_DMA_POLL) */1626 if (p & SR_IO) {1627 if (!(foo = NCR5380_pread(instance, d, c - 1))) {1628 /*1629 * We can't disable DMA mode after successfully transfering 1630 * what we plan to be the last byte, since that would open up1631 * a race condition where if the target asserted REQ before 1632 * we got the DMA mode reset, the NCR5380 would have latched1633 * an additional byte into the INPUT DATA register and we'd1634 * have dropped it.1635 * 1636 * The workarround was to transfer one fewer bytes than we 1637 * intended to with the pseudo-DMA read function, wait for 1638 * the chip to latch the last byte, read it, and then disable1639 * pseudo-DMA mode.1640 * 1641 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.1642 * REQ is deasserted when ACK is asserted, and not reasserted1643 * until ACK goes false. Since the NCR5380 won't lower ACK1644 * until DACK is asserted, which won't happen unless we twiddle1645 * the DMA port or we take the NCR5380 out of DMA mode, we 1646 * can gurantee that we won't handshake another extra 1647 * byte.1648 */1649
1650 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1651 /* Wait for clean handshake */1652 while (NCR5380_read(STATUS_REG) & SR_REQ);
1653 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1654 }1655 }else{1656 inttimeout;
1657 if (!(foo = NCR5380_pwrite(instance, d, c))) {1658 /*1659 * Wait for the last byte to be sent. If REQ is being asserted for 1660 * the byte we're interested, we'll ACK it and it will go false. 1661 */1662 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {1663 timeout = 20000;
1664 #if 1
1665 #if 1
1666 while (!(NCR5380_read(BUS_AND_STATUS_REG) &
1667 BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
1668 BASR_PHASE_MATCH));
1669 #else1670 if (NCR5380_read(STATUS_REG) & SR_REQ) {1671 for (; timeout &&
1672 !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1673 --timeout);
1674 for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
1675 --timeout);
1676 }1677 #endif1678
1679
1680 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
1681 if (!timeout)
1682 printk("scsi%d : timed out on last byte\n",
1683 instance->host_no);
1684 #endif1685
1686
1687 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {1688 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1689 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {1690 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
1691 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
1692 printk("scsi%d : last bit sent works\n",
1693 instance->host_no);
1694 #endif1695 }1696 }1697 }else1698 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
1699 #else1700 udelay (5);
1701 #endif1702 }1703 }1704
1705 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1706 NCR5380_write(MODE_REG, MR_BASE);
1707
1708 *data = d + c;
1709 *count = 0;
1710 *phase = (NCR5380_read(STATUS_REG & PHASE_MASK));
1711 #ifdefined(PSEUDO_DMA) && !defined(UNSAFE)
1712 sti();
1713 #endif/* defined(REAL_DMA_POLL) */1714 returnfoo;
1715 #endif/* def REAL_DMA */1716 }1717 #endif/* defined(REAL_DMA) | defined(PSEUDO_DMA) */1718
1719 /*1720 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)1721 *1722 * Purpose : run through the various SCSI phases and do as the target 1723 * directs us to. Operates on the currently connected command, 1724 * instance->connected.1725 *1726 * Inputs : instance, instance for which we are doing commands1727 *1728 * Side effects : SCSI things happen, the disconnected queue will be 1729 * modified if a command disconnects, *instance->connected will1730 * change.1731 *1732 * XXX Note : we need to watch for bus free or a reset condition here 1733 * to recover from an unexpected bus free condition.1734 */1735
1736 staticvoidNCR5380_information_transfer (structScsi_Host *instance) {/* */1737 NCR5380_local_declare();
1738 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
1739 instance->hostdata;
1740 unsignedcharmsgout = NOP;
1741 intlen, transfersize;
1742 unsignedchar *data;
1743 unsignedcharphase, tmp, old_phase=0xff;
1744 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
1745 NCR5380_setup(instance);
1746
1747 while (1) {1748 tmp = NCR5380_read(STATUS_REG);
1749 /* We only have a valid SCSI phase when REQ is asserted */1750 if (tmp & SR_REQ) {1751 phase = (tmp & PHASE_MASK);
1752 if (phase != old_phase) {1753 old_phase = phase;
1754 #if (NDEBUG & NDEBUG_INFORMATION)
1755 NCR5380_print_phase(instance);
1756 #endif1757 }1758 switch (phase) {1759 casePHASE_DATAIN:
1760 casePHASE_DATAOUT:
1761 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1762 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
1763 instance->host_no);
1764 msgout = ABORT;
1765 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1766 break;
1767 #endif1768 /* 1769 * If there is no room left in the current buffer in the1770 * scatter-gather list, move onto the next one.1771 */1772
1773 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {1774 ++cmd->SCp.buffer;
1775 --cmd->SCp.buffers_residual;
1776 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1777 cmd->SCp.ptr = cmd->SCp.buffer->address;
1778 #if (NDEBUG & NDEBUG_INFORMATION)
1779 printk("scsi%d : %d bytes and %d buffers left\n",
1780 instance->host_no, cmd->SCp.this_residual,
1781 cmd->SCp.buffers_residual);
1782 #endif1783 }1784
1785 /*1786 * The preffered transfer method is going to be 1787 * PSEUDO-DMA for systems that are strictly PIO,1788 * since we can let the hardware do the handshaking.1789 *1790 * For this to work, we need to know the transfersize1791 * ahead of time, since the pseudo-DMA code will sit1792 * in an unconditional loop.1793 */1794
1795 #ifdefined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1796 #ifdefNCR5380_dma_xfer_len1797 if (!scsi_devices[cmd->index].borken &&
1798 (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {1799 #else1800 if (!scsi_devices[cmd->index].borken &&
1801 (transfersize = cmd->transfersize) &&
1802 cmd->SCp.this_residual && !(cmd->SCp.this_residual %
1803 transfersize)) {1804 #endif1805 len = transfersize;
1806 if (NCR5380_transfer_dma(instance, &phase,
1807 &len, (unsignedchar **) &cmd->SCp.ptr)) {1808 /*1809 * If the watchdog timer fires, all future accesses to this1810 * device will use the polled-IO.1811 */1812 printk("scsi%d : switching target %d lun %d to slow handshake\n",
1813 instance->host_no, cmd->target, cmd->lun);
1814 scsi_devices[cmd->index].borken = 1;
1815 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1816 ICR_ASSERT_ATN);
1817 msgout = ABORT;
1818 }else1819 cmd->SCp.this_residual -= transfersize - len;
1820 }else1821 #endif/* defined(REAL_DMA) || defined(REAL_DMA_POLL) */1822 NCR5380_transfer_pio(instance, &phase,
1823 (int *) &cmd->SCp.this_residual, (unsignedchar **)
1824 &cmd->SCp.ptr);
1825 break;
1826 casePHASE_MSGIN:
1827 /* 1828 * XXX - we don't handle multi-byte messages here, since we 1829 * shouldn't get them after the I_T_L_Q nexus is established1830 * for tagged queuing, and the host should initiate any 1831 * negotiations for sync. SCSI, etc.1832 */1833 len = 1;
1834 data = &tmp;
1835 NCR5380_transfer_pio(instance, &phase, &len, &data);
1836 cmd->SCp.Message = tmp;
1837
1838 switch (tmp) {1839 /*1840 * Linking lets us reduce the time required to get the 1841 * next command out to the device, hopefully this will1842 * mean we don't waste another revolution due to the delays1843 * required by ARBITRATION and another SELECTION.1844 *1845 * In the current implementation proposal, low level drivers1846 * merely have to start the next command, pointed to by 1847 * next_link, done() is called as with unlinked commands.1848 */1849 #ifdefLINKED1850 caseLINKED_CMD_COMPLETE:
1851 caseLINKED_FLG_CMD_COMPLETE:
1852 #if (NDEBUG & NDEBUG_LINKED)
1853 printk("scsi%d : target %d lun %d linked command complete.\n",
1854 instance->host_no, cmd->target, cmd->lun);
1855 #endif1856 /* 1857 * Sanity check : A linked command should only terminate with1858 * one of these messages if there are more linked commands1859 * available.1860 */1861
1862 if (!cmd->next_link) {1863 printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
1864 instance->host_no, cmd->target, cmd->lun);
1865 msgout = ABORT;
1866 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1867 ICR_ASSERT_ATN);
1868 break;
1869 }1870
1871 initialize_SCp(cmd->next_link);
1872 /* The next command is still part of this process */1873 cmd->next_link->tag = cmd->tag;
1874 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1875 #if (NDEBUG & NDEBUG_LINKED)
1876 printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
1877 instance->host_no, cmd->target, cmd->lun);
1878 #endif1879 cmd->scsi_done(cmd);
1880 cmd = hostdata->connected;
1881 break;
1882 #endif/* def LINKED */1883 caseABORT:
1884 caseCOMMAND_COMPLETE:
1885 hostdata->connected = NULL;
1886 #if (NDEBUG & NDEBUG_QUEUES)
1887 printk("scsi%d : command for target %d, lun %d completed\n",
1888 instance->host_no, cmd->target, cmd->lun);
1889 #endif1890 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1891
1892 /* 1893 * I'm not sure what the correct thing to do here is : 1894 * 1895 * If the command that just executed is NOT a request 1896 * sense, the obvious thing to do is to set the result1897 * code to the values of the stored parameters.1898 * 1899 * If it was a REQUEST SENSE command, we need some way 1900 * to differentiate between the failure code of the original1901 * and the failure code of the REQUEST sense - the obvious1902 * case is success, where we fall through and leave the result1903 * code unchanged.1904 * 1905 * The non-obvious place is where the REQUEST SENSE failed 1906 */1907
1908 if (cmd->cmnd[0] != REQUEST_SENSE)
1909 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1910 elseif (cmd->SCp.Status != GOOD)
1911 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1912
1913 #ifdefAUTOSENSE1914 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
1915 (cmd->SCp.Status == CHECK_CONDITION)) {1916 #if (NDEBUG & NDEBUG_AUTOSENSE)
1917 printk("scsi%d : performing request sense\n",
1918 instance->host_no);
1919 #endif1920 cmd->cmnd[0] = REQUEST_SENSE;
1921 cmd->cmnd[1] &= 0xe0;
1922 cmd->cmnd[2] = 0;
1923 cmd->cmnd[3] = 0;
1924 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
1925 cmd->cmnd[5] = 0;
1926
1927 cmd->SCp.buffer = NULL;
1928 cmd->SCp.buffers_residual = 0;
1929 cmd->SCp.ptr = (char *) cmd->sense_buffer;
1930 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
1931
1932 cli();
1933 cmd->host_scribble = (unsignedchar *)
1934 hostdata->issue_queue;
1935 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
1936 sti();
1937 #if (NDEBUG & NDEBUG_QUEUES)
1938 printk("scsi%d : REQUEST SENSE added to head of issue queue\n");
1939 #endif1940 }else1941 #endif/* def AUTOSENSE */1942 cmd->scsi_done(cmd);
1943
1944 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1945
1946 while ((NCR5380_read(STATUS_REG) & SR_BSY) &&
1947 !hostdata->connected);
1948 return;
1949 caseMESSAGE_REJECT:
1950 switch (hostdata->last_message) {1951 caseHEAD_OF_QUEUE_TAG:
1952 caseORDERED_QUEUE_TAG:
1953 caseSIMPLE_QUEUE_TAG:
1954 scsi_devices[cmd->index].tagged_queue = 0;
1955 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1956 break;
1957 default:
1958 break;
1959 }1960 caseDISCONNECT:
1961 scsi_devices[cmd->index].disconnect = 1;
1962 cli();
1963 cmd->host_scribble = (unsignedchar *)
1964 hostdata->disconnected_queue;
1965 hostdata->connected = NULL;
1966 hostdata->disconnected_queue = cmd;
1967 sti();
1968 #if (NDEBUG & NDEBUG_QUEUES)
1969 printk("scsi%d : command for target %d lun %d was moved from connected to"
1970 " the disconnected_queue\n", instance->host_no,
1971 cmd->target, cmd->lun);
1972 #endif1973
1974 /* Enable reselect interupts */1975 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1976 /* Wait for bus free to avoid nasty timeouts */1977 while ((NCR5380_read(STATUS_REG) & SR_BSY) &&
1978 !hostdata->connected);
1979 return;
1980 /* 1981 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect1982 * operation, in violation of the SCSI spec so we can safely 1983 * ignore SAVE/RESTORE pointers calls.1984 *1985 * Unfortunately, some disks violate the SCSI spec and 1986 * don't issue the required SAVE_POINTERS message before1987 * disconnecting, and we have to break spec to remain 1988 * compatable.1989 */1990 caseSAVE_POINTERS:
1991 caseRESTORE_POINTERS:
1992 break;
1993 default:
1994 /* 1995 * XXX rejected messages should be handled in the pio data transfer phase,1996 * since ATN should be raised before ACK goes false when we reject a message1997 */1998
1999 printk("Unknown message!\n");
2000
2001 #ifdefnotyet2002 /* 2003 * If we get something wierd that we aren't expecting, 2004 * reject it.2005 */2006 msgout = MESSAGE_REJECT;
2007 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2008 ICR_ASSERT_ATN);
2009 #endif2010 break;
2011 }/* switch (tmp) */2012 break;
2013 casePHASE_MSGOUT:
2014 len = 1;
2015 data = &msgout;
2016 hostdata->last_message = msgout;
2017 NCR5380_transfer_pio(instance, &phase, &len, &data);
2018 if (msgout == ABORT) {2019 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2020 hostdata->connected = NULL;
2021 cmd->result = DID_ERROR << 16;
2022 cmd->scsi_done(cmd);
2023 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2024 return;
2025 }2026 msgout = NOP;
2027 break;
2028 casePHASE_CMDOUT:
2029 len = COMMAND_SIZE(cmd->cmnd[0]);
2030 data = cmd->cmnd;
2031 /* 2032 * XXX for performance reasons, on machines with a 2033 * PSEUDO-DMA architecture we should probably 2034 * use the dma transfer function. 2035 */2036 NCR5380_transfer_pio(instance, &phase, &len,
2037 &data);
2038 #ifdefUSLEEP2039 if (!disconnect && should_disconnect(cmd->cmnd[0])) {2040 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2041 #if (NDEBUG & NDEBUG_USLEEP)
2042 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
2043 hostdata->time_expires);
2044 #endif2045 NCR5380_set_timer (instance);
2046 return;
2047 }2048 #endif/* def USLEEP */2049 break;
2050 casePHASE_STATIN:
2051 len = 1;
2052 data = &tmp;
2053 NCR5380_transfer_pio(instance, &phase, &len, &data);
2054 cmd->SCp.Status = tmp;
2055 break;
2056 default:
2057 printk("scsi%d : unknown phase\n", instance->host_no);
2058 #ifdefNDEBUG2059 NCR5380_print(instance);
2060 #endif2061 }/* switch(phase) */2062 }/* if (tmp * SR_REQ) */2063 #ifdefUSLEEP2064 else{2065 if (!disconnect && hostdata->time_expires && jiffies >
2066 hostdata->time_expires) {2067 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2068 #if (NDEBUG & NDEBUG_USLEEP)
2069 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
2070 hostdata->time_expires);
2071 #endif2072 NCR5380_set_timer (instance);
2073 return;
2074 }2075 }2076 #endif2077 }/* while (1) */2078 }2079
2080 /*2081 * Function : void NCR5380_reselect (struct Scsi_Host *instance)2082 *2083 * Purpose : does reselection, initializing the instance->connected 2084 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q 2085 * nexus has been restablished,2086 * 2087 * Inputs : instance - this instance of the NCR5380.2088 *2089 */2090
2091
2092 staticvoidNCR5380_reselect (structScsi_Host *instance) {/* */2093 NCR5380_local_declare();
2094 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
2095 instance->hostdata;
2096 unsignedchartarget_mask;
2097 unsignedcharlun, phase;
2098 intlen;
2099 #ifdefSCSI22100 unsignedchartag;
2101 #endif2102 unsignedcharmsg[3];
2103 unsignedchar *data;
2104 Scsi_Cmnd *tmp = NULL, *prev;
2105 intabort = 0;
2106 NCR5380_setup(instance);
2107
2108 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2109
2110 #if (NDEBUG & NDEBUG_RESELECTION)
2111 printk("scsi%d : reselect\n", instance->host_no);
2112 #endif2113
2114 /* 2115 * At this point, we have detected that our SCSI ID is on the bus,2116 * SEL is true and BSY was false for at least one bus settle delay2117 * (400 ns).2118 *2119 * We must assert BSY ourselves, until the target drops the SEL2120 * signal.2121 */2122
2123 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2124
2125 while (NCR5380_read(STATUS_REG) & SR_SEL);
2126 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2127
2128 /*2129 * Wait for target to go into MSGIN.2130 */2131
2132 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2133
2134 len = 3;
2135 data = msg;
2136 phase = PHASE_MSGIN;
2137 NCR5380_transfer_pio(instance, &phase, &len, &data);
2138
2139 #ifdefSCSI22140 /*2141 * If there was no residual from the attempt to transfer three bytes, then2142 * the target sent the one byte IDENTIFY message followed by a two byte2143 * queue message.2144 *2145 * If there were two bytes of residual, we got the IDENTIFY message2146 * only.2147 *2148 * If there was one byte of residual, we got the IDENTIFY message2149 * followed by a RESTORE pointers message (which was ignored - 2150 * see MSGIN phase of the NCR5380_information_transfer() function.2151 */2152
2153 if (!len)
2154 tag = msg[2];
2155 else2156 tag = 0;
2157 #endif2158
2159 if (!msg[0] & 0x80) {2160 printk("scsi%d : expecting IDENTIFY message, got ",
2161 instance->host_no);
2162 print_msg(msg);
2163 abort = 1;
2164 }else{2165
2166 lun = (msg[0] & 0x07);
2167
2168 /* 2169 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we 2170 * just restablished, and remove it from the disconnected queue.2171 */2172
2173 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2174 tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
2175 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2176 #ifdefSCSI22177 && (tag == tmp->tag)
2178 #endif2179 ) {2180 if (prev)
2181 prev->host_scribble = tmp->host_scribble;
2182 else2183 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2184 tmp->host_scribble = NULL;
2185 break;
2186 }2187
2188 if (!tmp) {2189 #ifdefSCSI22190 printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
2191 instance->host_no, target_mask, lun, tag);
2192 #else2193 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
2194 instance->host_no, target_mask, lun);
2195 #endif2196 /* 2197 * Since we have an established nexus that we can't do anything with,2198 * we must abort it. 2199 */2200 abort = 1;
2201 }2202 }2203
2204 if (abort) {2205 msg[0] = ABORT;
2206 len = 1;
2207 data = msg;
2208 phase = PHASE_MSGOUT;
2209 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2210 NCR5380_transfer_pio(instance, &phase, &len, &data);
2211 }else{2212 hostdata->connected = tmp;
2213 #if (NDEBUG & NDEBUG_RESELECTION)
2214 printk"scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
2215 instance->host_no, cmd->target, cmd->lun, cmd->tag);
2216 #endif2217 }2218 }2219
2220 /*2221 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)2222 *2223 * Purpose : called by interrupt handler when DMA finishes or a phase2224 * mismatch occurs (which would finish the DMA transfer). 2225 *2226 * Inputs : instance - this instance of the NCR5380.2227 *2228 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L2229 * nexus has been restablished, on failure NULL is returned.2230 */2231
2232 #ifdefREAL_DMA2233 staticvoid NCR5380_dma_complete (NCR5380_instance *instance) {/* */2234 NCR5380_local_declare();
2235 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *
2236 instance->hostdata);
2237 inttransferred;
2238 NCR5380_setup(instance);
2239
2240 /*2241 * XXX this might not be right.2242 *2243 * Wait for final byte to transfer, ie wait for ACK to go false.2244 *2245 * We should use the Last Byte Sent bit, unfortunately this is 2246 * not available on the 5380/5381 (only the various CMOS chips)2247 */2248
2249 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2250
2251 NCR5380_write(MODE_REG, MR_BASE);
2252 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2253
2254 /*2255 * The only places we should see a phase mismatch and have to send2256 * data from the same set of pointers will be the data transfer2257 * phases. So, residual, requested length are only important here.2258 */2259
2260 if (!(hostdata->connected->SCp.phase & SR_CD)) {2261 transferred = instance->dmalen - NCR5380_dma_residual();
2262 hostdata->connected->SCp.this_residual -= transferred;
2263 hostdata->connected->SCp.ptr += transferred;
2264 }2265 }2266 #endif/* def REAL_DMA */2267
2268 /*2269 * Function : int NCR5380_abort (Scsi_Cmnd *cmd, int code)2270 *2271 * Purpose : abort a command2272 *2273 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 2274 * host byte of the result field to, if zero DID_ABORTED is 2275 * used.2276 *2277 * Returns : 0 - success, -1 on failure.2278 *2279 * XXX - there is no way to abort the command that is currently 2280 * connected, you have to wait for it to complete. If this is 2281 * a problem, we could implement longjmp() / setjmp(), setjmp()2282 * called where the loop started in NCR5380_main().2283 */2284
2285 #ifndefNCR5380_abort2286 static2287 #endif2288 intNCR5380_abort (Scsi_Cmnd *cmd, intcode) {/* */2289 NCR5380_local_declare();
2290 structScsi_Host *instance = cmd->host;
2291 structNCR5380_hostdata *hostdata = (structNCR5380_hostdata *)
2292 instance->hostdata;
2293 Scsi_Cmnd *tmp, **prev;
2294 unsignedcharmsg, phase, *msgptr;
2295 intlen;
2296
2297 cli();
2298 NCR5380_setup(instance);
2299
2300 #if (NDEBUG & NDEBUG_ABORT)
2301 printk("scsi%d : abort called\n", instance->host_no);
2302 printk(" basr 0x%X, sr 0x%X\n",
2303 NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
2304 #endif2305 /* 2306 * Case 1 : If the command hasn't been issued yet, we simply remove it 2307 * from the issue queue.2308 */2309 for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue),
2310 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2311 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp =
2312 (Scsi_Cmnd *) tmp->host_scribble)
2313 if (cmd == tmp) {2314 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2315 tmp->host_scribble = NULL;
2316 tmp->result = (code ? code : DID_ABORT) << 16;
2317 sti();
2318 #if (NDEBUG & NDEBUG_ABORT)
2319 printk("scsi%d : abort removed command from issue queue.\n",
2320 instance->host_no);
2321 #endif2322 tmp->done(tmp);
2323 return 0;
2324 }2325
2326 /* 2327 * Case 2 : If any commands are connected, we're going to fail the abort2328 * and let the high level SCSI driver retry at a later time or 2329 * issue a reset.2330 *2331 * Timeouts, and therefore aborted commands, will be highly unlikely2332 * and handling them cleanly in this situation would make the common2333 * case of noresets less efficient, and would pollute our code. So,2334 * we fail.2335 */2336
2337 if (hostdata->connected) {2338 sti();
2339 #if (NDEBUG & NDEBUG_ABORT)
2340 printk("scsi%d : abort failed, command connected.\n", instance->host_no);
2341 #endif2342 return -1;
2343 }2344
2345 /*2346 * Case 3: If the command is currently disconnected from the bus, and 2347 * there are no connected commands, we reconnect the I_T_L or 2348 * I_T_L_Q nexus associated with it, go into message out, and send 2349 * an abort message.2350 *2351 * This case is especially ugly. In order to resetablish the nexus, we2352 * need to call NCR5380_select(). The easiest way to implement this 2353 * function was to abort if the bus was busy, and let the interrupt2354 * handler triggered on the SEL for reselect take care of lost arbitrations2355 * where necessary, meaning interrupts need to be enabled.2356 *2357 * When interrupts are enabled, the queues may change - so we 2358 * can't remove it from the disconnected queue before selecting it2359 * because that could cause a failure in hashing the nexus if that 2360 * device reselected.2361 * 2362 * Since the queues may change, we can't use the pointers from when we2363 * first locate it.2364 *2365 * So, we must first locate the command, and if NCR5380_select()2366 * succeeds, then issue the abort, relocate the command and remove2367 * it from the disconnected queue.2368 */2369
2370 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2371 tmp = (Scsi_Cmnd *) tmp->host_scribble)
2372 if (cmd == tmp) {2373 sti();
2374 #if (NDEBUG & NDEBUG_ABORT)
2375 printk("scsi%d : aborting disconnected command.\n", instance->host_no);
2376 #endif2377
2378 if (NCR5380_select (instance, cmd, (int) cmd->tag))
2379 return 1;
2380
2381 #if (NDEBUG & NDEBUG_ABORT)
2382 printk("scsi%d : nexus restablished.\n", instance->host_no);
2383 #endif2384
2385 msg = ABORT;
2386 msgptr = &msg;
2387 len = 1;
2388 phase = PHASE_MSGOUT;
2389 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2390 NCR5380_transfer_pio (instance, &phase, &len, &msgptr);
2391
2392 cli();
2393 for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue),
2394 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
2395 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp =
2396 (Scsi_Cmnd *) tmp->host_scribble)
2397 if (cmd == tmp) {2398 *prev = (Scsi_Cmnd *) tmp->host_scribble;
2399 tmp->host_scribble = NULL;
2400 tmp->result = (code ? code : DID_ABORT) << 16;
2401 sti();
2402 tmp->done(tmp);
2403 return 0;
2404 }2405 }2406
2407 /*2408 * Case 4 : If we reached this point, the command was not found in any of 2409 * the queues.2410 *2411 * We probably reached this point because of an unlikely race condition2412 * between the command completing successfully and the abortion code,2413 * so we won't panic, but we will notify the user in case somethign really2414 * broke.2415 */2416
2417 sti();
2418 printk("scsi%d : warning : SCSI command probably completed successfully\n"
2419 " before abortion\n", instance->host_no);
2420 return 0;
2421 }2422
2423
2424 /* 2425 * Function : int NCR5380_reset (Scsi_Cmnd *cmd)2426 * 2427 * Purpose : reset the SCSI bus.2428 *2429 * Returns : 02430 *2431 */2432
2433 #ifndefNCR5380_reset2434 static2435 #endif2436 intNCR5380_reset (Scsi_Cmnd *cmd) {/* */2437 NCR5380_local_declare();
2438 NCR5380_setup(cmd->host);
2439
2440 cli();
2441 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2442 udelay(1);
2443 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2444 sti();
2445
2446 return 0;
2447 }