Import 2.1.81
[davej-history.git] / drivers / scsi / NCR5380.c
blob6eccb24c75dbc081a88f22a1d0a6cd12749f838d
1 #ifndef NDEBUG
2 #define NDEBUG (NDEBUG_RESTART_SELECT | NDEBUG_ABORT)
3 #endif
4 /*
5 * NCR 5380 generic driver routines. These should make it *trivial*
6 * to implement 5380 SCSI drivers under Linux with a non-trantor
7 * architecture.
9 * Note that these routines also work with NR53c400 family chips.
11 * Copyright 1993, Drew Eckhardt
12 * Visionary Computing
13 * (Unix and Linux consulting and custom programming)
14 * drew@colorado.edu
15 * +1 (303) 666-5836
17 * DISTRIBUTION RELEASE 6.
19 * For more information, please consult
21 * NCR 5380 Family
22 * SCSI Protocol Controller
23 * Databook
25 * NCR Microelectronics
26 * 1635 Aeroplaza Drive
27 * Colorado Springs, CO 80916
28 * 1+ (719) 578-3400
29 * 1+ (800) 334-5454
33 * $Log: NCR5380.c,v $
34 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
35 * added proc_info
36 * added support needed for DTC 3180/3280
37 * fixed a couple of bugs
40 * Revision 1.5 1994/01/19 09:14:57 drew
41 * Fixed udelay() hack that was being used on DATAOUT phases
42 * instead of a proper wait for the final handshake.
44 * Revision 1.4 1994/01/19 06:44:25 drew
45 * *** empty log message ***
47 * Revision 1.3 1994/01/19 05:24:40 drew
48 * Added support for TCR LAST_BYTE_SENT bit.
50 * Revision 1.2 1994/01/15 06:14:11 drew
51 * REAL DMA support, bug fixes.
53 * Revision 1.1 1994/01/15 06:00:54 drew
54 * Initial revision
59 * Further development / testing that should be done :
60 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
61 * code so that everything does the same thing that's done at the
62 * end of a pseudo-DMA read operation.
64 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
65 * basically, transfer size needs to be reduced by one
66 * and the last byte read as is done with PSEUDO_DMA.
68 * 3. Test USLEEP code
70 * 4. Test SCSI-II tagged queueing (I have no devices which support
71 * tagged queueing)
73 * 5. Test linked command handling code after Eric is ready with
74 * the high level code.
77 #if (NDEBUG & NDEBUG_LISTS)
78 #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
79 #define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
80 #else
81 #define LIST(x,y)
82 #define REMOVE(w,x,y,z)
83 #endif
85 #ifndef notyet
86 #undef LINKED
87 #undef USLEEP
88 #undef REAL_DMA
89 #endif
91 #ifdef REAL_DMA_POLL
92 #undef READ_OVERRUNS
93 #define READ_OVERRUNS
94 #endif
97 * Design
98 * Issues :
100 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
101 * and specifically for each board rather than each chip. This makes their
102 * adaptation to platforms like the Mac (Some of which use NCR5380's)
103 * more difficult than it has to be.
105 * Also, many of the SCSI drivers were written before the command queuing
106 * routines were implemented, meaning their implementations of queued
107 * commands were hacked on rather than designed in from the start.
109 * When I designed the Linux SCSI drivers I figured that
110 * while having two different SCSI boards in a system might be useful
111 * for debugging things, two of the same type wouldn't be used.
112 * Well, I was wrong and a number of users have mailed me about running
113 * multiple high-performance SCSI boards in a server.
115 * Finally, when I get questions from users, I have no idea what
116 * revision of my driver they are running.
118 * This driver attempts to address these problems :
119 * This is a generic 5380 driver. To use it on a different platform,
120 * one simply writes appropriate system specific macros (ie, data
121 * transfer - some PC's will use the I/O bus, 68K's must use
122 * memory mapped) and drops this file in their 'C' wrapper.
124 * As far as command queueing, two queues are maintained for
125 * each 5380 in the system - commands that haven't been issued yet,
126 * and commands that are currently executing. This means that an
127 * unlimited number of commands may be queued, letting
128 * more commands propagate from the higher driver levels giving higher
129 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
130 * allowing multiple commands to propagate all the way to a SCSI-II device
131 * while a command is already executing.
133 * To solve the multiple-boards-in-the-same-system problem,
134 * there is a separate instance structure for each instance
135 * of a 5380 in the system. So, multiple NCR5380 drivers will
136 * be able to coexist with appropriate changes to the high level
137 * SCSI code.
139 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
140 * number (updated for each public release) printed by the
141 * NCR5380_print_options command, which should be called from the
142 * wrapper detect function, so that I know what release of the driver
143 * users are using.
145 * Issues specific to the NCR5380 :
147 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
148 * piece of hardware that requires you to sit in a loop polling for
149 * the REQ signal as long as you are connected. Some devices are
150 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
151 * while doing long seek operations.
153 * The workaround for this is to keep track of devices that have
154 * disconnected. If the device hasn't disconnected, for commands that
155 * should disconnect, we do something like
157 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
159 * Some tweaking of N and M needs to be done. An algorithm based
160 * on "time to data" would give the best results as long as short time
161 * to datas (ie, on the same track) were considered, however these
162 * broken devices are the exception rather than the rule and I'd rather
163 * spend my time optimizing for the normal case.
165 * Architecture :
167 * At the heart of the design is a coroutine, NCR5380_main,
168 * which is started when not running by the interrupt handler,
169 * timer, and queue command function. It attempts to establish
170 * I_T_L or I_T_L_Q nexuses by removing the commands from the
171 * issue queue and calling NCR5380_select() if a nexus
172 * is not established.
174 * Once a nexus is established, the NCR5380_information_transfer()
175 * phase goes through the various phases as instructed by the target.
176 * if the target goes into MSG IN and sends a DISCONNECT message,
177 * the command structure is placed into the per instance disconnected
178 * queue, and NCR5380_main tries to find more work. If USLEEP
179 * was defined, and the target is idle for too long, the system
180 * will try to sleep.
182 * If a command has disconnected, eventually an interrupt will trigger,
183 * calling NCR5380_intr() which will in turn call NCR5380_reselect
184 * to reestablish a nexus. This will run main if necessary.
186 * On command termination, the done function will be called as
187 * appropriate.
189 * SCSI pointers are maintained in the SCp field of SCSI command
190 * structures, being initialized after the command is connected
191 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
192 * Note that in violation of the standard, an implicit SAVE POINTERS operation
193 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
197 * Using this file :
198 * This file a skeleton Linux SCSI driver for the NCR 5380 series
199 * of chips. To use it, you write an architecture specific functions
200 * and macros and include this file in your driver.
202 * These macros control options :
203 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
204 * defined.
206 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
207 * for commands that return with a CHECK CONDITION status.
209 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
210 * transceivers.
212 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
213 * override-configure an IRQ.
215 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
216 * bytes at a time. Since interrupts are disabled by default during
217 * these transfers, we might need this to give reasonable interrupt
218 * service time if the transfer size gets too large.
220 * LINKED - if defined, linked commands are supported.
222 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
224 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
226 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
227 * rely on phase mismatch and EOP interrupts to determine end
228 * of phase.
230 * SCSI2 - if defined, SCSI-2 tagged queuing is used where possible
232 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
233 * only really want to use this if you're having a problem with
234 * dropped characters during high speed communications, and even
235 * then, you're going to be better off twiddling with transfersize
236 * in the high level code.
238 * USLEEP - if defined, on devices that aren't disconnecting from the
239 * bus, we will go to sleep so that the CPU can get real work done
240 * when we run a command that won't complete immediately.
242 * Note that if USLEEP is defined, NCR5380_TIMER *must* also be
243 * defined.
245 * Defaults for these will be provided if USLEEP is defined, although
246 * the user may want to adjust these to allocate CPU resources to
247 * the SCSI driver or "real" code.
249 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
251 * USLEEP_POLL - amount of time, in jiffies, to poll
253 * These macros MUST be defined :
254 * NCR5380_local_declare() - declare any local variables needed for your
255 * transfer routines.
257 * NCR5380_setup(instance) - initialize any local variables needed from a given
258 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
260 * NCR5380_read(register) - read from the specified register
262 * NCR5380_write(register, value) - write to the specific register
264 * NCR5380_implementation_fields - additional fields needed for this
265 * specific implementation of the NCR5380
267 * Either real DMA *or* pseudo DMA may be implemented
268 * REAL functions :
269 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
270 * Note that the DMA setup functions should return the number of bytes
271 * that they were able to program the controller for.
273 * Also note that generic i386/PC versions of these macros are
274 * available as NCR5380_i386_dma_write_setup,
275 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
277 * NCR5380_dma_write_setup(instance, src, count) - initialize
278 * NCR5380_dma_read_setup(instance, dst, count) - initialize
279 * NCR5380_dma_residual(instance); - residual count
281 * PSEUDO functions :
282 * NCR5380_pwrite(instance, src, count)
283 * NCR5380_pread(instance, dst, count);
285 * If nothing specific to this implementation needs doing (ie, with external
286 * hardware), you must also define
288 * NCR5380_queue_command
289 * NCR5380_reset
290 * NCR5380_abort
291 * NCR5380_proc_info
293 * to be the global entry points into the specific driver, ie
294 * #define NCR5380_queue_command t128_queue_command.
296 * If this is not done, the routines will be defined as static functions
297 * with the NCR5380* names and the user must provide a globally
298 * accessible wrapper function.
300 * The generic driver is initialized by calling NCR5380_init(instance),
301 * after setting the appropriate host specific fields and ID. If the
302 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
303 * possible) function may be used. Before the specific driver initialization
304 * code finishes, NCR5380_print_options should be called.
307 static int do_abort (struct Scsi_Host *host);
308 static void do_reset (struct Scsi_Host *host);
309 static struct Scsi_Host *first_instance = NULL;
310 static Scsi_Host_Template *the_template = NULL;
313 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
315 * Purpose : initialize the saved data pointers for cmd to point to the
316 * start of the buffer.
318 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
321 static __inline__ void initialize_SCp(Scsi_Cmnd *cmd) {
323 * Initialize the Scsi Pointer field so that all of the commands in the
324 * various queues are valid.
327 if (cmd->use_sg) {
328 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
329 cmd->SCp.buffers_residual = cmd->use_sg - 1;
330 cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
331 cmd->SCp.this_residual = cmd->SCp.buffer->length;
332 } else {
333 cmd->SCp.buffer = NULL;
334 cmd->SCp.buffers_residual = 0;
335 cmd->SCp.ptr = (char *) cmd->request_buffer;
336 cmd->SCp.this_residual = cmd->request_bufflen;
340 #include <linux/delay.h>
342 #ifdef NDEBUG
343 static struct {
344 unsigned char mask;
345 const char * name;}
346 signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
347 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
348 { SR_SEL, "SEL" }, {0, NULL}},
349 basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
350 icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
351 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
352 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
353 {0, NULL}},
354 mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
355 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
356 "MODE PARITY INTR"}, {MR_MONITOR_BSY, "MODE MONITOR BSY"},
357 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
358 {0, NULL}};
361 * Function : void NCR5380_print(struct Scsi_Host *instance)
363 * Purpose : print the SCSI bus signals for debugging purposes
365 * Input : instance - which NCR5380
368 static void NCR5380_print(struct Scsi_Host *instance) {
369 NCR5380_local_declare();
370 unsigned char status, data, basr, mr, icr, i;
371 NCR5380_setup(instance);
372 cli();
373 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
374 status = NCR5380_read(STATUS_REG);
375 mr = NCR5380_read(MODE_REG);
376 icr = NCR5380_read(INITIATOR_COMMAND_REG);
377 basr = NCR5380_read(BUS_AND_STATUS_REG);
378 sti();
379 printk("STATUS_REG: %02x ", status);
380 for (i = 0; signals[i].mask ; ++i)
381 if (status & signals[i].mask)
382 printk(",%s", signals[i].name);
383 printk("\nBASR: %02x ", basr);
384 for (i = 0; basrs[i].mask ; ++i)
385 if (basr & basrs[i].mask)
386 printk(",%s", basrs[i].name);
387 printk("\nICR: %02x ", icr);
388 for (i = 0; icrs[i].mask; ++i)
389 if (icr & icrs[i].mask)
390 printk(",%s", icrs[i].name);
391 printk("\nMODE: %02x ", mr);
392 for (i = 0; mrs[i].mask; ++i)
393 if (mr & mrs[i].mask)
394 printk(",%s", mrs[i].name);
395 printk("\n");
398 static struct {
399 unsigned char value;
400 const char *name;
401 } phases[] = {
402 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
403 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
404 {PHASE_UNKNOWN, "UNKNOWN"}};
407 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
409 * Purpose : print the current SCSI phase for debugging purposes
411 * Input : instance - which NCR5380
414 static void NCR5380_print_phase(struct Scsi_Host *instance) {
415 NCR5380_local_declare();
416 unsigned char status;
417 int i;
418 NCR5380_setup(instance);
420 status = NCR5380_read(STATUS_REG);
421 if (!(status & SR_REQ))
422 printk("scsi%d : REQ not asserted, phase unknown.\n",
423 instance->host_no);
424 else {
425 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
426 (phases[i].value != (status & PHASE_MASK)); ++i);
427 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
430 #endif
433 * We need to have our coroutine active given these constraints :
434 * 1. The mutex flag, main_running, can only be set when the main
435 * routine can actually process data, otherwise SCSI commands
436 * will never get issued.
438 * 2. NCR5380_main() shouldn't be called before it has exited, because
439 * other drivers have had kernel stack overflows in similar
440 * situations.
442 * 3. We don't want to inline NCR5380_main() because of space concerns,
443 * even though it is only called in two places.
445 * So, the solution is to set the mutex in an inline wrapper for the
446 * main coroutine, and have the main coroutine exit with interrupts
447 * disabled after the final search through the queues so that no race
448 * conditions are possible.
451 static volatile int main_running = 0;
454 * Function : run_main(void)
456 * Purpose : insure that the coroutine is running and will process our
457 * request. main_running is checked/set here (in an inline function)
458 * rather than in NCR5380_main itself to reduce the chances of stack
459 * overflow.
463 static __inline__ void run_main(void) {
464 cli();
465 if (!main_running) {
466 main_running = 1;
467 NCR5380_main();
469 * main_running is cleared in NCR5380_main once it can't do
470 * more work, and NCR5380_main exits with interrupts disabled.
472 sti();
473 } else
474 sti();
477 #ifdef USLEEP
478 #ifndef NCR5380_TIMER
479 #error "NCR5380_TIMER must be defined so that this type of NCR5380 driver gets a unique timer."
480 #endif
483 * These need tweaking, and would probably work best as per-device
484 * flags initialized differently for disk, tape, cd, etc devices.
485 * People with broken devices are free to experiment as to what gives
486 * the best results for them.
488 * USLEEP_SLEEP should be a minimum seek time.
490 * USLEEP_POLL should be a maximum rotational latency.
492 #ifndef USLEEP_SLEEP
493 /* 20 ms (reasonable hard disk speed) */
494 #define USLEEP_SLEEP 2
495 #endif
496 /* 300 RPM (floppy speed) */
497 #ifndef USLEEP_POLL
498 #define USLEEP_POLL 20
499 #endif
501 static struct Scsi_Host * expires_first = NULL;
504 * Function : int should_disconnect (unsigned char cmd)
506 * Purpose : decide weather a command would normally disconnect or
507 * not, since if it won't disconnect we should go to sleep.
509 * Input : cmd - opcode of SCSI command
511 * Returns : DISCONNECT_LONG if we should disconnect for a really long
512 * time (ie always, sleep, look for REQ active, sleep),
513 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
514 * time-to-data delay, DISCONNECT_NONE if this command would return
515 * immediately.
517 * Future sleep algorithms based on time to data can exploit
518 * something like this so they can differentiate between "normal"
519 * (ie, read, write, seek) and unusual commands (ie, * format).
521 * Note : We don't deal with commands that handle an immediate disconnect,
525 static int should_disconnect (unsigned char cmd) {
526 switch (cmd) {
527 case READ_6:
528 case WRITE_6:
529 case SEEK_6:
530 case READ_10:
531 case WRITE_10:
532 case SEEK_10:
533 return DISCONNECT_TIME_TO_DATA;
534 case FORMAT_UNIT:
535 case SEARCH_HIGH:
536 case SEARCH_LOW:
537 case SEARCH_EQUAL:
538 return DISCONNECT_LONG;
539 default:
540 return DISCONNECT_NONE;
545 * Assumes instance->time_expires has been set in higher level code.
548 static int NCR5380_set_timer (struct Scsi_Host *instance) {
549 struct Scsi_Host *tmp, **prev;
551 cli();
552 if (((struct NCR5380_hostdata *) (instance->host_data))->next_timer) {
553 sti();
554 return -1;
557 for (prev = &expires_first, tmp = expires_first; tmp;
558 prev = &(((struct NCR5380_hostdata *) tmp->host_data)->next_timer),
559 tmp = ((struct NCR5380_hostdata *) tmp->host_data)->next_timer)
560 if (instance->time_expires < tmp->time_expires)
561 break;
563 instance->next_timer = tmp;
564 *prev = instance;
565 timer_table[NCR5380_TIMER].expires = expires_first->time_expires;
566 timer_active |= 1 << NCR5380_TIMER;
567 sti();
568 return 0;
571 /* Doing something about unwanted reentrancy here might be useful */
572 void NCR5380_timer_fn(void) {
573 struct Scsi_Host *instance;
574 cli();
575 for (; expires_first && expires_first->time_expires >= jiffies; ) {
576 instance = ((NCR5380_hostdata *) expires_first->host_data)->
577 expires_next;
578 ((NCR5380_hostdata *) expires_first->host_data)->expires_next =
579 NULL;
580 ((NCR5380_hostdata *) expires_first->host_data)->time_expires =
582 expires_first = instance;
585 if (expires_first) {
586 timer_table[NCR5380_TIMER].expires = ((NCR5380_hostdata *)
587 expires_first->host_data)->time_expires;
588 timer_active |= (1 << NCR5380_TIMER);
589 } else {
590 timer_table[NCR5380_TIMER].expires = 0;
591 timer_active &= ~(1 << MCR5380_TIMER);
593 sti();
595 run_main();
597 #endif /* def USLEEP */
599 static inline void NCR5380_all_init (void) {
600 static int done = 0;
601 if (!done) {
602 #if (NDEBUG & NDEBUG_INIT)
603 printk("scsi : NCR5380_all_init()\n");
604 #endif
605 done = 1;
606 #ifdef USLEEP
607 timer_table[NCR5380_TIMER].expires = 0;
608 timer_table[NCR5380_TIMER].fn = NCR5380_timer_fn;
609 #endif
613 #ifdef AUTOPROBE_IRQ
615 * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
617 * Purpose : autoprobe for the IRQ line used by the NCR5380.
619 * Inputs : instance - pointer to this instance of the NCR5380 driver,
620 * possible - bitmask of permissible interrupts.
622 * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired.
624 * XXX no effort is made to deal with spurious interrupts.
628 static int probe_irq __initdata ;
629 __initfunc(static void probe_intr (int irq, void *dev_id, struct pt_regs * regs)) {
630 probe_irq = irq;
633 __initfunc(static int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)) {
634 NCR5380_local_declare();
635 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
636 instance->hostdata;
637 unsigned long timeout;
638 int trying_irqs, i, mask;
639 NCR5380_setup(instance);
641 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
642 if ((mask & possible) && (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL)
643 == 0))
644 trying_irqs |= mask;
646 timeout = jiffies + 25;
647 probe_irq = IRQ_NONE;
650 * A interrupt is triggered whenever BSY = false, SEL = true
651 * and a bit set in the SELECT_ENABLE_REG is asserted on the
652 * SCSI bus.
654 * Note that the bus is only driven when the phase control signals
655 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
656 * to zero.
659 NCR5380_write(TARGET_COMMAND_REG, 0);
660 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
661 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
662 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
663 ICR_ASSERT_SEL);
665 while (probe_irq == IRQ_NONE && jiffies < timeout)
666 barrier();
668 NCR5380_write(SELECT_ENABLE_REG, 0);
669 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
671 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
672 if (trying_irqs & mask)
673 free_irq(i, NULL);
675 return probe_irq;
677 #endif /* AUTOPROBE_IRQ */
680 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
682 * Purpose : called by probe code indicating the NCR5380 driver
683 * options that were selected.
685 * Inputs : instance, pointer to this instance. Unused.
688 __initfunc(static void NCR5380_print_options (struct Scsi_Host *instance)) {
689 printk(" generic options"
690 #ifdef AUTOPROBE_IRQ
691 " AUTOPROBE_IRQ"
692 #endif
693 #ifdef AUTOSENSE
694 " AUTOSENSE"
695 #endif
696 #ifdef DIFFERENTIAL
697 " DIFFERENTIAL"
698 #endif
699 #ifdef REAL_DMA
700 " REAL DMA"
701 #endif
702 #ifdef REAL_DMA_POLL
703 " REAL DMA POLL"
704 #endif
705 #ifdef PARITY
706 " PARITY"
707 #endif
708 #ifdef PSEUDO_DMA
709 " PSEUDO DMA"
710 #endif
711 #ifdef SCSI2
712 " SCSI-2"
713 #endif
714 #ifdef UNSAFE
715 " UNSAFE "
716 #endif
718 #ifdef USLEEP
719 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
720 #endif
721 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
722 if (((struct NCR5380_hostdata *)instance->hostdata)->flags & FLAG_NCR53C400) {
723 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
728 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
730 * Purpose : print commands in the various queues, called from
731 * NCR5380_abort and NCR5380_debug to aid debugging.
733 * Inputs : instance, pointer to this instance.
736 static void NCR5380_print_status (struct Scsi_Host *instance) {
737 static char pr_bfr[512];
738 char *start;
739 int len;
741 printk("NCR5380 : coroutine is%s running.\n",
742 main_running ? "" : "n't");
744 #ifdef NDEBUG
745 NCR5380_print (instance);
746 NCR5380_print_phase (instance);
747 #endif
749 len = NCR5380_proc_info(pr_bfr, &start, 0, sizeof(pr_bfr),
750 instance->host_no, 0);
751 pr_bfr[len] = 0;
752 printk("\n%s\n", pr_bfr);
755 /******************************************/
757 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
759 * *buffer: I/O buffer
760 * **start: if inout == FALSE pointer into buffer where user read should start
761 * offset: current offset
762 * length: length of buffer
763 * hostno: Scsi_Host host_no
764 * inout: TRUE - user is writing; FALSE - user is reading
766 * Return the number of bytes read from or written
769 #undef SPRINTF
770 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
771 static
772 char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length);
773 static
774 char *lprint_command (unsigned char *cmd, char *pos, char *buffer, int len);
775 static
776 char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
778 #ifndef NCR5380_proc_info
779 static
780 #endif
781 int NCR5380_proc_info (
782 char *buffer, char **start,off_t offset,
783 int length,int hostno,int inout)
785 char *pos = buffer;
786 struct Scsi_Host *instance;
787 struct NCR5380_hostdata *hostdata;
788 Scsi_Cmnd *ptr;
790 for (instance = first_instance; instance &&
791 instance->host_no != hostno; instance=instance->next)
793 if (!instance)
794 return(-ESRCH);
795 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
797 if (inout) { /* Has data been written to the file ? */
798 #ifdef DTC_PUBLIC_RELEASE
799 dtc_wmaxi = dtc_maxi = 0;
800 #endif
801 #ifdef PAS16_PUBLIC_RELEASE
802 pas_wmaxi = pas_maxi = 0;
803 #endif
804 return(-ENOSYS); /* Currently this is a no-op */
806 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
807 if (((struct NCR5380_hostdata *)instance->hostdata)->flags & FLAG_NCR53C400)
808 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
809 #ifdef DTC_PUBLIC_RELEASE
810 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
811 #endif
812 #ifdef T128_PUBLIC_RELEASE
813 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
814 #endif
815 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
816 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
817 #endif
818 #ifdef PAS16_PUBLIC_RELEASE
819 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
820 #endif
822 SPRINTF("\nBase Addr: 0x%05X ", (int)instance->base);
823 SPRINTF("io_port: %04x ", (int)instance->io_port);
824 if (instance->irq == IRQ_NONE)
825 SPRINTF("IRQ: None.\n");
826 else
827 SPRINTF("IRQ: %d.\n", instance->irq);
829 #ifdef DTC_PUBLIC_RELEASE
830 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
831 dtc_wmaxi, dtc_maxi);
832 #endif
833 #ifdef PAS16_PUBLIC_RELEASE
834 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
835 pas_wmaxi, pas_maxi);
836 #endif
837 cli();
838 SPRINTF("NCR5380 : coroutine is%s running.\n", main_running ? "" : "n't");
839 if (!hostdata->connected)
840 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
841 else
842 pos = lprint_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected,
843 pos, buffer, length);
844 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
845 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr;
846 ptr = (Scsi_Cmnd *) ptr->host_scribble)
847 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
849 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
850 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
851 ptr = (Scsi_Cmnd *) ptr->host_scribble)
852 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
854 sti();
855 *start=buffer;
856 if (pos - buffer < offset)
857 return 0;
858 else if (pos - buffer - offset < length)
859 return pos - buffer - offset;
860 return length;
863 static
864 char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length) {
865 SPRINTF("scsi%d : destination target %d, lun %d\n",
866 cmd->host->host_no, cmd->target, cmd->lun);
867 SPRINTF(" command = ");
868 pos = lprint_command (cmd->cmnd, pos, buffer, length);
869 return (pos);
872 static
873 char *lprint_command (unsigned char *command,
874 char *pos, char *buffer, int length) {
875 int i, s;
876 pos = lprint_opcode(command[0], pos, buffer, length);
877 for ( i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
878 SPRINTF("%02x ", command[i]);
879 SPRINTF("\n");
880 return(pos);
883 static
884 char *lprint_opcode(int opcode, char *pos, char *buffer, int length) {
885 SPRINTF("%2d (0x%02x)", opcode, opcode);
886 return(pos);
891 * Function : void NCR5380_init (struct Scsi_Host *instance, flags)
893 * Purpose : initializes *instance and corresponding 5380 chip,
894 * with flags OR'd into the initial flags value.
896 * Inputs : instance - instantiation of the 5380 driver.
898 * Notes : I assume that the host, hostno, and id bits have been
899 * set correctly. I don't care about the irq and other fields.
903 __initfunc(static void NCR5380_init (struct Scsi_Host *instance, int flags)) {
904 NCR5380_local_declare();
905 int i, pass;
906 unsigned long timeout;
907 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
908 instance->hostdata;
911 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
912 * the base address.
915 #ifdef NCR53C400
916 if (flags & FLAG_NCR53C400)
917 instance->NCR5380_instance_name += NCR53C400_address_adjust;
918 #endif
920 NCR5380_setup(instance);
922 NCR5380_all_init();
924 hostdata->aborted = 0;
925 hostdata->id_mask = 1 << instance->this_id;
926 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
927 if (i > hostdata->id_mask)
928 hostdata->id_higher_mask |= i;
929 for (i = 0; i < 8; ++i)
930 hostdata->busy[i] = 0;
931 #ifdef REAL_DMA
932 hostdata->dmalen = 0;
933 #endif
934 hostdata->targets_present = 0;
935 hostdata->connected = NULL;
936 hostdata->issue_queue = NULL;
937 hostdata->disconnected_queue = NULL;
938 #ifdef NCR5380_STATS
939 for (i = 0; i < 8; ++i) {
940 hostdata->time_read[i] = 0;
941 hostdata->time_write[i] = 0;
942 hostdata->bytes_read[i] = 0;
943 hostdata->bytes_write[i] = 0;
945 hostdata->timebase = 0;
946 hostdata->pendingw = 0;
947 hostdata->pendingr = 0;
948 #endif
950 /* The CHECK code seems to break the 53C400. Will check it later maybe */
951 if (flags & FLAG_NCR53C400)
952 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
953 else
954 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
956 if (!the_template) {
957 the_template = instance->hostt;
958 first_instance = instance;
962 #ifdef USLEEP
963 hostdata->time_expires = 0;
964 hostdata->next_timer = NULL;
965 #endif
967 #ifndef AUTOSENSE
968 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1))
969 printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
970 " without AUTOSENSE option, contingent allegiance conditions may\n"
971 " be incorrectly cleared.\n", instance->host_no);
972 #endif /* def AUTOSENSE */
974 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
975 NCR5380_write(MODE_REG, MR_BASE);
976 NCR5380_write(TARGET_COMMAND_REG, 0);
977 NCR5380_write(SELECT_ENABLE_REG, 0);
979 #ifdef NCR53C400
980 if (hostdata->flags & FLAG_NCR53C400) {
981 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
983 #endif
986 * Detect and correct bus wedge problems.
988 * If the system crashed, it may have crashed in a state
989 * where a SCSI command was still executing, and the
990 * SCSI bus is not in a BUS FREE STATE.
992 * If this is the case, we'll try to abort the currently
993 * established nexus which we know nothing about, and that
994 * failing, do a hard reset of the SCSI bus
997 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) &&
998 pass <= 6 ; ++pass) {
999 switch (pass) {
1000 case 1:
1001 case 3:
1002 case 5:
1003 printk("scsi%d: SCSI bus busy, waiting up to five seconds\n",
1004 instance->host_no);
1005 timeout = jiffies + 500;
1006 while (jiffies < timeout && (NCR5380_read(STATUS_REG) & SR_BSY));
1007 break;
1008 case 2:
1009 printk("scsi%d: bus busy, attempting abort\n",
1010 instance->host_no);
1011 do_abort (instance);
1012 break;
1013 case 4:
1014 printk("scsi%d: bus busy, attempting reset\n",
1015 instance->host_no);
1016 do_reset (instance);
1017 break;
1018 case 6:
1019 printk("scsi%d: bus locked solid or invalid override\n",
1020 instance->host_no);
1026 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
1027 * void (*done)(Scsi_Cmnd *))
1029 * Purpose : enqueues a SCSI command
1031 * Inputs : cmd - SCSI command, done - function called on completion, with
1032 * a pointer to the command descriptor.
1034 * Returns : 0
1036 * Side effects :
1037 * cmd is added to the per instance issue_queue, with minor
1038 * twiddling done to the host specific fields of cmd. If the
1039 * main coroutine is not running, it is restarted.
1043 /* Only make static if a wrapper function is used */
1044 #ifndef NCR5380_queue_command
1045 static
1046 #endif
1047 int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *)) {
1048 struct Scsi_Host *instance = cmd->host;
1049 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
1050 instance->hostdata;
1051 Scsi_Cmnd *tmp;
1053 #if (NDEBUG & NDEBUG_NO_WRITE)
1054 switch (cmd->cmnd[0]) {
1055 case WRITE_6:
1056 case WRITE_10:
1057 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
1058 instance->host_no);
1059 cmd->result = (DID_ERROR << 16);
1060 done(cmd);
1061 return 0;
1063 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1065 #ifdef NCR5380_STATS
1066 # if 0
1067 if (!hostdata->connected && !hostdata->issue_queue &&
1068 !hostdata->disconnected_queue) {
1069 hostdata->timebase = jiffies;
1071 # endif
1072 # ifdef NCR5380_STAT_LIMIT
1073 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1074 # endif
1075 switch (cmd->cmnd[0])
1077 case WRITE:
1078 case WRITE_6:
1079 case WRITE_10:
1080 hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
1081 hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
1082 hostdata->pendingw++;
1083 break;
1084 case READ:
1085 case READ_6:
1086 case READ_10:
1087 hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
1088 hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
1089 hostdata->pendingr++;
1090 break;
1092 #endif
1095 * We use the host_scribble field as a pointer to the next command
1096 * in a queue
1099 cmd->host_scribble = NULL;
1100 cmd->scsi_done = done;
1102 cmd->result = 0;
1106 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1107 * commands are added to the head of the queue since any command will
1108 * clear the contingent allegiance condition that exists and the
1109 * sense data is only guaranteed to be valid while the condition exists.
1112 cli();
1113 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1114 LIST(cmd, hostdata->issue_queue);
1115 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1116 hostdata->issue_queue = cmd;
1117 } else {
1118 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
1119 tmp = (Scsi_Cmnd *) tmp->host_scribble);
1120 LIST(cmd, tmp);
1121 tmp->host_scribble = (unsigned char *) cmd;
1123 #if (NDEBUG & NDEBUG_QUEUES)
1124 printk("scsi%d : command added to %s of queue\n", instance->host_no,
1125 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1126 #endif
1128 /* Run the coroutine if it isn't already running. */
1129 run_main();
1130 return 0;
1134 * Function : NCR5380_main (void)
1136 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1137 * be done on the NCR5380 host adapters in a system. Both
1138 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1139 * in case it is not running.
1141 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1142 * reenable them. This prevents reentrancy and kernel stack overflow.
1145 static void NCR5380_main (void) {
1146 Scsi_Cmnd *tmp, *prev;
1147 struct Scsi_Host *instance;
1148 struct NCR5380_hostdata *hostdata;
1149 int done;
1152 * We run (with interrupts disabled) until we're sure that none of
1153 * the host adapters have anything that can be done, at which point
1154 * we set main_running to 0 and exit.
1156 * Interrupts are enabled before doing various other internal
1157 * instructions, after we've decided that we need to run through
1158 * the loop again.
1160 * this should prevent any race conditions.
1163 do {
1164 cli(); /* Freeze request queues */
1165 done = 1;
1166 for (instance = first_instance; instance &&
1167 instance->hostt == the_template; instance=instance->next) {
1168 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1169 cli();
1170 if (!hostdata->connected) {
1171 #if (NDEBUG & NDEBUG_MAIN)
1172 printk("scsi%d : not connected\n", instance->host_no);
1173 #endif
1175 * Search through the issue_queue for a command destined
1176 * for a target that's not busy.
1178 #if (NDEBUG & NDEBUG_LISTS)
1179 for (tmp= (Scsi_Cmnd *) hostdata->issue_queue, prev=NULL; tmp && (tmp != prev); prev=tmp, tmp=(Scsi_Cmnd*)tmp->host_scribble)
1181 /*printk("%p ", tmp);*/
1182 if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1183 #endif
1184 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1185 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
1186 tmp->host_scribble) {
1188 #if (NDEBUG & NDEBUG_LISTS)
1189 if (prev != tmp)
1190 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun);
1191 #endif
1192 /* When we find one, remove it from the issue queue. */
1193 if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
1194 if (prev) {
1195 REMOVE(prev,prev->host_scribble,tmp,tmp->host_scribble);
1196 prev->host_scribble = tmp->host_scribble;
1197 } else {
1198 REMOVE(-1,hostdata->issue_queue,tmp,tmp->host_scribble);
1199 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1201 tmp->host_scribble = NULL;
1203 /* reenable interrupts after finding one */
1204 sti();
1207 * Attempt to establish an I_T_L nexus here.
1208 * On success, instance->hostdata->connected is set.
1209 * On failure, we must add the command back to the
1210 * issue queue so we can keep trying.
1212 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1213 printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
1214 instance->host_no, tmp->target, tmp->lun);
1215 #endif
1218 * A successful selection is defined as one that
1219 * leaves us with the command connected and
1220 * in hostdata->connected, OR has terminated the
1221 * command.
1223 * With successful commands, we fall through
1224 * and see if we can do an information transfer,
1225 * with failures we will restart.
1228 if (!NCR5380_select(instance, tmp,
1230 * REQUEST SENSE commands are issued without tagged
1231 * queueing, even on SCSI-II devices because the
1232 * contingent allegiance condition exists for the
1233 * entire unit.
1235 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1236 TAG_NEXT)) {
1237 break;
1238 } else {
1239 cli();
1240 LIST(tmp, hostdata->issue_queue);
1241 tmp->host_scribble = (unsigned char *)
1242 hostdata->issue_queue;
1243 hostdata->issue_queue = tmp;
1244 done = 0;
1245 sti();
1246 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1247 printk("scsi%d : main(): select() failed, returned to issue_queue\n",
1248 instance->host_no);
1249 #endif
1251 } /* if target/lun is not busy */
1252 } /* for */
1253 } /* if (!hostdata->connected) */
1255 if (hostdata->connected
1256 #ifdef REAL_DMA
1257 && !hostdata->dmalen
1258 #endif
1259 #ifdef USLEEP
1260 && (!hostdata->time_expires || hostdata->time_expires >= jiffies)
1261 #endif
1263 sti();
1264 #if (NDEBUG & NDEBUG_MAIN)
1265 printk("scsi%d : main() : performing information transfer\n",
1266 instance->host_no);
1267 #endif
1268 NCR5380_information_transfer(instance);
1269 #if (NDEBUG & NDEBUG_MAIN)
1270 printk("scsi%d : main() : done set false\n", instance->host_no);
1271 #endif
1272 done = 0;
1273 } else
1274 break;
1275 } /* for instance */
1276 } while (!done);
1277 main_running = 0;
1280 #ifndef DONT_USE_INTR
1282 * Function : void NCR5380_intr (int irq)
1284 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1285 * from the disconnected queue, and restarting NCR5380_main()
1286 * as required.
1288 * Inputs : int irq, irq that caused this interrupt.
1292 static void NCR5380_intr (int irq, void *dev_id, struct pt_regs * regs) {
1293 NCR5380_local_declare();
1294 struct Scsi_Host *instance;
1295 int done;
1296 unsigned char basr;
1297 #if (NDEBUG & NDEBUG_INTR)
1298 printk("scsi : NCR5380 irq %d triggered\n", irq);
1299 #endif
1300 do {
1301 done = 1;
1302 for (instance = first_instance; instance && (instance->hostt ==
1303 the_template); instance = instance->next)
1304 if (instance->irq == irq) {
1306 /* Look for pending interrupts */
1307 NCR5380_setup(instance);
1308 basr = NCR5380_read(BUS_AND_STATUS_REG);
1309 /* XXX dispatch to appropriate routine if found and done=0 */
1310 if (basr & BASR_IRQ) {
1311 #if (NDEBUG & NDEBUG_INTR)
1312 NCR5380_print(instance);
1313 #endif
1314 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1315 (SR_SEL | SR_IO)) {
1316 done = 0;
1317 sti();
1318 #if (NDEBUG & NDEBUG_INTR)
1319 printk("scsi%d : SEL interrupt\n", instance->host_no);
1320 #endif
1321 NCR5380_reselect(instance);
1322 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1323 } else if (basr & BASR_PARITY_ERROR) {
1324 #if (NDEBUG & NDEBUG_INTR)
1325 printk("scsi%d : PARITY interrupt\n", instance->host_no);
1326 #endif
1327 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1328 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1329 #if (NDEBUG & NDEBUG_INTR)
1330 printk("scsi%d : RESET interrupt\n", instance->host_no);
1331 #endif
1332 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1333 } else {
1335 * XXX the rest of the interrupt conditions should *only* occur during a
1336 * DMA transfer, which I haven't gotten around to fixing yet.
1339 #if defined(REAL_DMA)
1341 * We should only get PHASE MISMATCH and EOP interrupts
1342 * if we have DMA enabled, so do a sanity check based on
1343 * the current setting of the MODE register.
1346 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr &
1347 BASR_END_DMA_TRANSFER) ||
1348 !(basr & BASR_PHASE_MATCH))) {
1349 int transfered;
1351 if (!hostdata->connected)
1352 panic("scsi%d : received end of DMA interrupt with no connected cmd\n",
1353 instance->hostno);
1355 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1356 hostdata->connected->SCp.this_residual -= transferred;
1357 hostdata->connected->SCp.ptr += transferred;
1358 hostdata->dmalen = 0;
1360 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1361 #if NCR_TIMEOUT
1363 unsigned long timeout = jiffies + NCR_TIMEOUT;
1365 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK
1366 && jiffies < timeout)
1368 if (jiffies >= timeout)
1369 printk("scsi%d: timeout at NCR5380.c:%d\n",
1370 host->host_no, __LINE__);
1372 #else /* NCR_TIMEOUT */
1373 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1374 #endif
1376 NCR5380_write(MODE_REG, MR_BASE);
1377 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1379 #else
1380 #if (NDEBUG & NDEBUG_INTR)
1381 printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1382 #endif
1383 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1384 #endif
1386 } /* if BASR_IRQ */
1387 if (!done)
1388 run_main();
1389 } /* if (instance->irq == irq) */
1390 } while (!done);
1392 #endif
1394 #ifdef NCR5380_STATS
1395 static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
1397 # ifdef NCR5380_STAT_LIMIT
1398 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1399 # endif
1400 switch (cmd->cmnd[0])
1402 case WRITE:
1403 case WRITE_6:
1404 case WRITE_10:
1405 hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1406 /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen;*/
1407 hostdata->pendingw--;
1408 break;
1409 case READ:
1410 case READ_6:
1411 case READ_10:
1412 hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1413 /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen;*/
1414 hostdata->pendingr--;
1415 break;
1418 #endif
1421 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1422 * int tag);
1424 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1425 * including ARBITRATION, SELECTION, and initial message out for
1426 * IDENTIFY and queue messages.
1428 * Inputs : instance - instantiation of the 5380 driver on which this
1429 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1430 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1431 * the command that is presently connected.
1433 * Returns : -1 if selection could not execute for some reason,
1434 * 0 if selection succeeded or failed because the target
1435 * did not respond.
1437 * Side effects :
1438 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1439 * with registers as they should have been on entry - ie
1440 * SELECT_ENABLE will be set appropriately, the NCR5380
1441 * will cease to drive any SCSI bus signals.
1443 * If successful : I_T_L or I_T_L_Q nexus will be established,
1444 * instance->connected will be set to cmd.
1445 * SELECT interrupt will be disabled.
1447 * If failed (no target) : cmd->scsi_done() will be called, and the
1448 * cmd->result host byte set to DID_BAD_TARGET.
1451 static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1452 int tag) {
1453 NCR5380_local_declare();
1454 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata*)
1455 instance->hostdata;
1456 unsigned char tmp[3], phase;
1457 unsigned char *data;
1458 int len;
1459 unsigned long timeout;
1460 NCR5380_setup(instance);
1462 hostdata->restart_select = 0;
1463 #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION)
1464 NCR5380_print(instance);
1465 printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1466 instance->this_id);
1467 #endif
1468 cli();
1471 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1472 * data bus during SELECTION.
1475 NCR5380_write(TARGET_COMMAND_REG, 0);
1479 * Start arbitration.
1482 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1483 NCR5380_write(MODE_REG, MR_ARBITRATE);
1485 sti();
1487 /* Wait for arbitration logic to complete */
1488 #if NCR_TIMEOUT
1490 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1492 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1493 && jiffies < timeout)
1495 if (jiffies >= timeout)
1497 printk("scsi: arbitration timeout at %d\n", __LINE__);
1498 NCR5380_write(MODE_REG, MR_BASE);
1499 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1500 return -1;
1503 #else /* NCR_TIMEOUT */
1504 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1505 #endif
1507 #if (NDEBUG & NDEBUG_ARBITRATION)
1508 printk("scsi%d : arbitration complete\n", instance->host_no);
1509 /* Avoid GCC 2.4.5 asm needs to many reloads error */
1510 __asm__("nop");
1511 #endif
1514 * The arbitration delay is 2.2us, but this is a minimum and there is
1515 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1516 * the integral nature of udelay().
1520 udelay(3);
1522 /* Check for lost arbitration */
1523 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1524 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1525 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1526 NCR5380_write(MODE_REG, MR_BASE);
1527 #if (NDEBUG & NDEBUG_ARBITRATION)
1528 printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n",
1529 instance->host_no);
1530 #endif
1531 return -1;
1536 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1538 if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) {
1539 NCR5380_write(MODE_REG, MR_BASE);
1540 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1541 #if (NDEBUG & NDEBUG_ARBITRATION)
1542 printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n",
1543 instance->host_no);
1544 #endif
1545 return -1;
1549 * Again, bus clear + bus settle time is 1.2us, however, this is
1550 * a minimum so we'll udelay ceil(1.2)
1553 udelay(2);
1555 #if (NDEBUG & NDEBUG_ARBITRATION)
1556 printk("scsi%d : won arbitration\n", instance->host_no);
1557 #endif
1561 * Now that we have won arbitration, start Selection process, asserting
1562 * the host and target ID's on the SCSI bus.
1565 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1568 * Raise ATN while SEL is true before BSY goes false from arbitration,
1569 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1570 * phase immediately after selection.
1573 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1574 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1575 NCR5380_write(MODE_REG, MR_BASE);
1578 * Reselect interrupts must be turned off prior to the dropping of BSY,
1579 * otherwise we will trigger an interrupt.
1581 NCR5380_write(SELECT_ENABLE_REG, 0);
1584 * The initiator shall then wait at least two deskew delays and release
1585 * the BSY signal.
1587 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1589 /* Reset BSY */
1590 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1591 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1594 * Something weird happens when we cease to drive BSY - looks
1595 * like the board/chip is letting us do another read before the
1596 * appropriate propagation delay has expired, and we're confusing
1597 * a BSY signal from ourselves as the target's response to SELECTION.
1599 * A small delay (the 'C++' frontend breaks the pipeline with an
1600 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1601 * tighter 'C' code breaks and requires this) solves the problem -
1602 * the 1 us delay is arbitrary, and only used because this delay will
1603 * be the same on other platforms and since it works here, it should
1604 * work there.
1606 * wingel suggests that this could be due to failing to wait
1607 * one deskew delay.
1610 udelay(1);
1612 #if (NDEBUG & NDEBUG_SELECTION)
1613 printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1614 #endif
1617 * The SCSI specification calls for a 250 ms timeout for the actual
1618 * selection.
1621 timeout = jiffies + 25;
1624 * XXX very interesting - we're seeing a bounce where the BSY we
1625 * asserted is being reflected / still asserted (propagation delay?)
1626 * and it's detecting as true. Sigh.
1629 while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) &
1630 (SR_BSY | SR_IO)));
1632 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1633 (SR_SEL | SR_IO)) {
1634 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1635 NCR5380_reselect(instance);
1636 printk ("scsi%d : reselection after won arbitration?\n",
1637 instance->host_no);
1638 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1639 return -1;
1643 * No less than two deskew delays after the initiator detects the
1644 * BSY signal is true, it shall release the SEL signal and may
1645 * change the DATA BUS. -wingel
1648 udelay(1);
1650 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1652 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1653 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1654 if (hostdata->targets_present & (1 << cmd->target)) {
1655 printk("scsi%d : weirdness\n", instance->host_no);
1656 if (hostdata->restart_select)
1657 printk("\trestart select\n");
1658 #ifdef NDEBUG
1659 NCR5380_print (instance);
1660 #endif
1661 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1662 return -1;
1664 cmd->result = DID_BAD_TARGET << 16;
1665 #ifdef NCR5380_STATS
1666 collect_stats(hostdata, cmd);
1667 #endif
1668 cmd->scsi_done(cmd);
1669 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1670 #if (NDEBUG & NDEBUG_SELECTION)
1671 printk("scsi%d : target did not respond within 250ms\n",
1672 instance->host_no);
1673 #endif
1674 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1675 return 0;
1678 hostdata->targets_present |= (1 << cmd->target);
1681 * Since we followed the SCSI spec, and raised ATN while SEL
1682 * was true but before BSY was false during selection, the information
1683 * transfer phase should be a MESSAGE OUT phase so that we can send the
1684 * IDENTIFY message.
1686 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1687 * message (2 bytes) with a tag ID that we increment with every command
1688 * until it wraps back to 0.
1690 * XXX - it turns out that there are some broken SCSI-II devices,
1691 * which claim to support tagged queuing but fail when more than
1692 * some number of commands are issued at once.
1695 /* Wait for start of REQ/ACK handshake */
1696 #ifdef NCR_TIMEOUT
1698 unsigned long timeout = jiffies + NCR_TIMEOUT;
1700 while (!(NCR5380_read(STATUS_REG) & SR_REQ) && jiffies < timeout);
1702 if (jiffies >= timeout) {
1703 printk("scsi%d: timeout at NCR5380.c:%d\n", __LINE__);
1704 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1705 return -1;
1708 #else /* NCR_TIMEOUT */
1709 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1710 #endif /* def NCR_TIMEOUT */
1712 #if (NDEBUG & NDEBUG_SELECTION)
1713 printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1714 instance->host_no, cmd->target);
1715 #endif
1716 tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1717 #ifdef SCSI2
1718 if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
1719 tmp[1] = SIMPLE_QUEUE_TAG;
1720 if (tag == TAG_NEXT) {
1721 /* 0 is TAG_NONE, used to imply no tag for this command */
1722 if (cmd->device->current_tag == 0)
1723 cmd->device->current_tag = 1;
1725 cmd->tag = cmd->device->current_tag;
1726 cmd->device->current_tag++;
1727 } else
1728 cmd->tag = (unsigned char) tag;
1730 tmp[2] = cmd->tag;
1731 hostdata->last_message = SIMPLE_QUEUE_TAG;
1732 len = 3;
1733 } else
1734 #endif /* def SCSI2 */
1736 len = 1;
1737 cmd->tag=0;
1740 /* Send message(s) */
1741 data = tmp;
1742 phase = PHASE_MSGOUT;
1743 NCR5380_transfer_pio(instance, &phase, &len, &data);
1744 #if (NDEBUG & NDEBUG_SELECTION)
1745 printk("scsi%d : nexus established.\n", instance->host_no);
1746 #endif
1747 /* XXX need to handle errors here */
1748 hostdata->connected = cmd;
1749 #ifdef SCSI2
1750 if (!cmd->device->tagged_queue)
1751 #endif
1752 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1754 initialize_SCp(cmd);
1757 return 0;
1761 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1762 * unsigned char *phase, int *count, unsigned char **data)
1764 * Purpose : transfers data in given phase using polled I/O
1766 * Inputs : instance - instance of driver, *phase - pointer to
1767 * what phase is expected, *count - pointer to number of
1768 * bytes to transfer, **data - pointer to data pointer.
1770 * Returns : -1 when different phase is entered without transferring
1771 * maximum number of bytes, 0 if all bytes or transfered or exit
1772 * is in same phase.
1774 * Also, *phase, *count, *data are modified in place.
1776 * XXX Note : handling for bus free may be useful.
1780 * Note : this code is not as quick as it could be, however it
1781 * IS 100% reliable, and for the actual data transfer where speed
1782 * counts, we will always do a pseudo DMA or DMA transfer.
1785 static int NCR5380_transfer_pio (struct Scsi_Host *instance,
1786 unsigned char *phase, int *count, unsigned char **data) {
1787 NCR5380_local_declare();
1788 register unsigned char p = *phase, tmp;
1789 register int c = *count;
1790 register unsigned char *d = *data;
1791 NCR5380_setup(instance);
1793 #if (NDEBUG & NDEBUG_PIO)
1794 if (!(p & SR_IO))
1795 printk("scsi%d : pio write %d bytes\n", instance->host_no, c);
1796 else
1797 printk("scsi%d : pio read %d bytes\n", instance->host_no, c);
1798 #endif
1801 * The NCR5380 chip will only drive the SCSI bus when the
1802 * phase specified in the appropriate bits of the TARGET COMMAND
1803 * REGISTER match the STATUS REGISTER
1806 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1808 do {
1810 * Wait for assertion of REQ, after which the phase bits will be
1811 * valid
1813 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1815 #if (NDEBUG & NDEBUG_HANDSHAKE)
1816 printk("scsi%d : REQ detected\n", instance->host_no);
1817 #endif
1819 /* Check for phase mismatch */
1820 if ((tmp & PHASE_MASK) != p) {
1821 #if (NDEBUG & NDEBUG_PIO)
1822 printk("scsi%d : phase mismatch\n", instance->host_no);
1823 NCR5380_print_phase(instance);
1824 #endif
1825 break;
1828 /* Do actual transfer from SCSI bus to / from memory */
1829 if (!(p & SR_IO))
1830 NCR5380_write(OUTPUT_DATA_REG, *d);
1831 else
1832 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1834 ++d;
1837 * The SCSI standard suggests that in MSGOUT phase, the initiator
1838 * should drop ATN on the last byte of the message phase
1839 * after REQ has been asserted for the handshake but before
1840 * the initiator raises ACK.
1843 if (!(p & SR_IO)) {
1844 if (!((p & SR_MSG) && c > 1)) {
1845 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1846 ICR_ASSERT_DATA);
1847 #if (NDEBUG & NDEBUG_PIO)
1848 NCR5380_print(instance);
1849 #endif
1850 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1851 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1852 } else {
1853 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1854 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1855 #if (NDEBUG & NDEBUG_PIO)
1856 NCR5380_print(instance);
1857 #endif
1858 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1859 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1861 } else {
1862 #if (NDEBUG & NDEBUG_PIO)
1863 NCR5380_print(instance);
1864 #endif
1865 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1868 while (NCR5380_read(STATUS_REG) & SR_REQ);
1870 #if (NDEBUG & NDEBUG_HANDSHAKE)
1871 printk("scsi%d : req false, handshake complete\n", instance->host_no);
1872 #endif
1875 * We have several special cases to consider during REQ/ACK handshaking :
1876 * 1. We were in MSGOUT phase, and we are on the last byte of the
1877 * message. ATN must be dropped as ACK is dropped.
1879 * 2. We are in a MSGIN phase, and we are on the last byte of the
1880 * message. We must exit with ACK asserted, so that the calling
1881 * code may raise ATN before dropping ACK to reject the message.
1883 * 3. ACK and ATN are clear and the target may proceed as normal.
1885 if (!(p == PHASE_MSGIN && c == 1)) {
1886 if (p == PHASE_MSGOUT && c > 1)
1887 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1888 else
1889 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1891 } while (--c);
1893 #if (NDEBUG & NDEBUG_PIO)
1894 printk("scsi%d : residual %d\n", instance->host_no, c);
1895 #endif
1897 *count = c;
1898 *data = d;
1899 tmp = NCR5380_read(STATUS_REG);
1900 if (tmp & SR_REQ)
1901 *phase = tmp & PHASE_MASK;
1902 else
1903 *phase = PHASE_UNKNOWN;
1905 if (!c || (*phase == p))
1906 return 0;
1907 else
1908 return -1;
1911 static void do_reset (struct Scsi_Host *host) {
1912 NCR5380_local_declare();
1913 NCR5380_setup(host);
1915 cli();
1916 NCR5380_write(TARGET_COMMAND_REG,
1917 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1918 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1919 udelay(25);
1920 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1921 sti();
1925 * Function : do_abort (Scsi_Host *host)
1927 * Purpose : abort the currently established nexus. Should only be
1928 * called from a routine which can drop into a
1930 * Returns : 0 on success, -1 on failure.
1933 static int do_abort (struct Scsi_Host *host) {
1934 NCR5380_local_declare();
1935 unsigned char tmp, *msgptr, phase;
1936 int len;
1937 NCR5380_setup(host);
1940 /* Request message out phase */
1941 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1944 * Wait for the target to indicate a valid phase by asserting
1945 * REQ. Once this happens, we'll have either a MSGOUT phase
1946 * and can immediately send the ABORT message, or we'll have some
1947 * other phase and will have to source/sink data.
1949 * We really don't care what value was on the bus or what value
1950 * the target sees, so we just handshake.
1953 while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1955 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1957 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1958 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1959 ICR_ASSERT_ACK);
1960 while (NCR5380_read(STATUS_REG) & SR_REQ);
1961 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1964 tmp = ABORT;
1965 msgptr = &tmp;
1966 len = 1;
1967 phase = PHASE_MSGOUT;
1968 NCR5380_transfer_pio (host, &phase, &len, &msgptr);
1971 * If we got here, and the command completed successfully,
1972 * we're about to go into bus free state.
1975 return len ? -1 : 0;
1978 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1980 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1981 * unsigned char *phase, int *count, unsigned char **data)
1983 * Purpose : transfers data in given phase using either real
1984 * or pseudo DMA.
1986 * Inputs : instance - instance of driver, *phase - pointer to
1987 * what phase is expected, *count - pointer to number of
1988 * bytes to transfer, **data - pointer to data pointer.
1990 * Returns : -1 when different phase is entered without transferring
1991 * maximum number of bytes, 0 if all bytes or transfered or exit
1992 * is in same phase.
1994 * Also, *phase, *count, *data are modified in place.
1999 static int NCR5380_transfer_dma (struct Scsi_Host *instance,
2000 unsigned char *phase, int *count, unsigned char **data) {
2001 NCR5380_local_declare();
2002 register int c = *count;
2003 register unsigned char p = *phase;
2004 register unsigned char *d = *data;
2005 unsigned char tmp;
2006 int foo;
2007 #if defined(REAL_DMA_POLL)
2008 int cnt, toPIO;
2009 unsigned char saved_data = 0, overrun = 0, residue;
2010 #endif
2012 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2013 instance->hostdata;
2015 NCR5380_setup(instance);
2017 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
2018 *phase = tmp;
2019 return -1;
2021 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
2022 #ifdef READ_OVERRUNS
2023 if (p & SR_IO) {
2024 c -= 2;
2026 #endif
2027 #if (NDEBUG & NDEBUG_DMA)
2028 printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
2029 instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
2030 "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
2031 #endif
2032 hostdata->dma_len = (p & SR_IO) ?
2033 NCR5380_dma_read_setup(instance, d, c) :
2034 NCR5380_dma_write_setup(instance, d, c);
2035 #endif
2037 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
2039 #ifdef REAL_DMA
2040 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
2041 #elif defined(REAL_DMA_POLL)
2042 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2043 #else
2045 * Note : on my sample board, watch-dog timeouts occurred when interrupts
2046 * were not disabled for the duration of a single DMA transfer, from
2047 * before the setting of DMA mode to after transfer of the last byte.
2050 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2051 cli();
2052 #endif
2053 /* KLL May need eop and parity in 53c400 */
2054 if (hostdata->flags & FLAG_NCR53C400)
2055 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK
2056 | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE
2057 | MR_MONITOR_BSY);
2058 else
2059 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2060 #endif /* def REAL_DMA */
2062 #if (NDEBUG & NDEBUG_DMA) & 0
2063 printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
2064 #endif
2067 * FOO stuff. For some UNAPPARENT reason, I'm getting
2068 * watchdog timers fired on bootup for NO APPARENT REASON, meaning it's
2069 * probably a timing problem.
2071 * Since this is the only place I have back-to-back writes, perhaps this
2072 * is the problem?
2075 if (p & SR_IO) {
2076 #ifndef FOO
2077 udelay(1);
2078 #endif
2079 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2080 } else {
2081 #ifndef FOO
2082 udelay(1);
2083 #endif
2084 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2085 #ifndef FOO
2086 udelay(1);
2087 #endif
2088 NCR5380_write(START_DMA_SEND_REG, 0);
2089 #ifndef FOO
2090 udelay(1);
2091 #endif
2094 #if defined(REAL_DMA_POLL)
2095 do {
2096 tmp = NCR5380_read(BUS_AND_STATUS_REG);
2097 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR |
2098 BASR_END_DMA_TRANSFER)));
2101 At this point, either we've completed DMA, or we have a phase mismatch,
2102 or we've unexpectedly lost BUSY (which is a real error).
2104 For write DMAs, we want to wait until the last byte has been
2105 transferred out over the bus before we turn off DMA mode. Alas, there
2106 seems to be no terribly good way of doing this on a 5380 under all
2107 conditions. For non-scatter-gather operations, we can wait until REQ
2108 and ACK both go false, or until a phase mismatch occurs. Gather-writes
2109 are nastier, since the device will be expecting more data than we
2110 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
2111 could test LAST BIT SENT to assure transfer (I imagine this is precisely
2112 why this signal was added to the newer chips) but on the older 538[01]
2113 this signal does not exist. The workaround for this lack is a watchdog;
2114 we bail out of the wait-loop after a modest amount of wait-time if
2115 the usual exit conditions are not met. Not a terribly clean or
2116 correct solution :-%
2118 Reads are equally tricky due to a nasty characteristic of the NCR5380.
2119 If the chip is in DMA mode for an READ, it will respond to a target's
2120 REQ by latching the SCSI data into the INPUT DATA register and asserting
2121 ACK, even if it has _already_ been notified by the DMA controller that
2122 the current DMA transfer has completed! If the NCR5380 is then taken
2123 out of DMA mode, this already-acknowledged byte is lost.
2125 This is not a problem for "one DMA transfer per command" reads, because
2126 the situation will never arise... either all of the data is DMA'ed
2127 properly, or the target switches to MESSAGE IN phase to signal a
2128 disconnection (either operation bringing the DMA to a clean halt).
2129 However, in order to handle scatter-reads, we must work around the
2130 problem. The chosen fix is to DMA N-2 bytes, then check for the
2131 condition before taking the NCR5380 out of DMA mode. One or two extra
2132 bytes are transferred via PIO as necessary to fill out the original
2133 request.
2136 if (p & SR_IO) {
2137 #ifdef READ_OVERRUNS
2138 udelay(10);
2139 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH|BASR_ACK)) ==
2140 (BASR_PHASE_MATCH | BASR_ACK))) {
2141 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
2142 overrun = 1;
2144 #endif
2145 } else {
2146 int limit = 100;
2147 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
2148 (NCR5380_read(STATUS_REG) & SR_REQ)) {
2149 if (!(tmp & BASR_PHASE_MATCH)) break;
2150 if (--limit < 0) break;
2155 #if (NDEBUG & NDEBUG_DMA)
2156 printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
2157 instance->host_no, tmp, NCR5380_read(STATUS_REG));
2158 #endif
2160 NCR5380_write(MODE_REG, MR_BASE);
2161 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2163 residue = NCR5380_dma_residual(instance);
2164 c -= residue;
2165 *count -= c;
2166 *data += c;
2167 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2169 #ifdef READ_OVERRUNS
2170 if (*phase == p && (p & SR_IO) && residue == 0) {
2171 if (overrun) {
2172 #if (NDEBUG & NDEBUG_DMA)
2173 printk("Got an input overrun, using saved byte\n");
2174 #endif
2175 **data = saved_data;
2176 *data += 1;
2177 *count -= 1;
2178 cnt = toPIO = 1;
2179 } else {
2180 printk("No overrun??\n");
2181 cnt = toPIO = 2;
2183 #if (NDEBUG & NDEBUG_DMA)
2184 printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
2185 #endif
2186 NCR5380_transfer_pio(instance, phase, &cnt, data);
2187 *count -= toPIO - cnt;
2189 #endif
2191 #if (NDEBUG & NDEBUG_DMA)
2192 printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
2193 *data, *count, *(*data+*count-1), *(*data+*count));
2194 #endif
2195 return 0;
2197 #elif defined(REAL_DMA)
2198 return 0;
2199 #else /* defined(REAL_DMA_POLL) */
2200 if (p & SR_IO) {
2201 #ifdef DMA_WORKS_RIGHT
2202 foo = NCR5380_pread(instance, d, c);
2203 #else
2204 int diff = 1;
2205 if (hostdata->flags & FLAG_NCR53C400) {
2206 diff=0;
2209 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2211 * We can't disable DMA mode after successfully transferring
2212 * what we plan to be the last byte, since that would open up
2213 * a race condition where if the target asserted REQ before
2214 * we got the DMA mode reset, the NCR5380 would have latched
2215 * an additional byte into the INPUT DATA register and we'd
2216 * have dropped it.
2218 * The workaround was to transfer one fewer bytes than we
2219 * intended to with the pseudo-DMA read function, wait for
2220 * the chip to latch the last byte, read it, and then disable
2221 * pseudo-DMA mode.
2223 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2224 * REQ is deasserted when ACK is asserted, and not reasserted
2225 * until ACK goes false. Since the NCR5380 won't lower ACK
2226 * until DACK is asserted, which won't happen unless we twiddle
2227 * the DMA port or we take the NCR5380 out of DMA mode, we
2228 * can guarantee that we won't handshake another extra
2229 * byte.
2232 if (!(hostdata->flags & FLAG_NCR53C400)) {
2233 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2234 /* Wait for clean handshake */
2235 while (NCR5380_read(STATUS_REG) & SR_REQ);
2236 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2239 #endif
2240 } else {
2241 #ifdef DMA_WORKS_RIGHT
2242 foo = NCR5380_pwrite(instance, d, c);
2243 #else
2244 int timeout;
2245 #if (NDEBUG & NDEBUG_C400_PWRITE)
2246 printk("About to pwrite %d bytes\n", c);
2247 #endif
2248 if (!(foo = NCR5380_pwrite(instance, d, c))) {
2250 * Wait for the last byte to be sent. If REQ is being asserted for
2251 * the byte we're interested, we'll ACK it and it will go false.
2253 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2254 timeout = 20000;
2255 #if 1
2256 #if 1
2257 while (!(NCR5380_read(BUS_AND_STATUS_REG) &
2258 BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
2259 BASR_PHASE_MATCH));
2260 #else
2261 if (NCR5380_read(STATUS_REG) & SR_REQ) {
2262 for (; timeout &&
2263 !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2264 --timeout);
2265 for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
2266 --timeout);
2268 #endif
2271 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2272 if (!timeout)
2273 printk("scsi%d : timed out on last byte\n",
2274 instance->host_no);
2275 #endif
2278 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2279 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2280 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2281 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2282 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2283 printk("scsi%d : last bit sent works\n",
2284 instance->host_no);
2285 #endif
2288 } else {
2289 #if (NDEBUG & NDEBUG_C400_PWRITE)
2290 printk("Waiting for LASTBYTE\n");
2291 #endif
2292 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2293 #if (NDEBUG & NDEBUG_C400_PWRITE)
2294 printk("Got LASTBYTE\n");
2295 #endif
2297 #else
2298 udelay (5);
2299 #endif
2301 #endif
2304 NCR5380_write(MODE_REG, MR_BASE);
2305 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2307 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2308 #if (NDEBUG & NDEBUG_C400_PWRITE)
2309 printk("53C400w: Checking for IRQ\n");
2310 #endif
2311 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2312 #if (NDEBUG & NDEBUG_C400_PWRITE)
2313 printk("53C400w: got it, reading reset interrupt reg\n");
2314 #endif
2315 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2316 } else {
2317 printk("53C400w: IRQ NOT THERE!\n");
2321 *data = d + c;
2322 *count = 0;
2323 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2324 #if 0
2325 NCR5380_print_phase(instance);
2326 #endif
2327 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2328 sti();
2329 #endif /* defined(REAL_DMA_POLL) */
2330 return foo;
2331 #endif /* def REAL_DMA */
2333 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2336 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2338 * Purpose : run through the various SCSI phases and do as the target
2339 * directs us to. Operates on the currently connected command,
2340 * instance->connected.
2342 * Inputs : instance, instance for which we are doing commands
2344 * Side effects : SCSI things happen, the disconnected queue will be
2345 * modified if a command disconnects, *instance->connected will
2346 * change.
2348 * XXX Note : we need to watch for bus free or a reset condition here
2349 * to recover from an unexpected bus free condition.
2352 static void NCR5380_information_transfer (struct Scsi_Host *instance) {
2353 NCR5380_local_declare();
2354 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2355 instance->hostdata;
2356 unsigned char msgout = NOP;
2357 int sink = 0;
2358 int len;
2359 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2360 int transfersize;
2361 #endif
2362 unsigned char *data;
2363 unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
2364 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2365 NCR5380_setup(instance);
2367 while (1) {
2368 tmp = NCR5380_read(STATUS_REG);
2369 /* We only have a valid SCSI phase when REQ is asserted */
2370 if (tmp & SR_REQ) {
2371 phase = (tmp & PHASE_MASK);
2372 if (phase != old_phase) {
2373 old_phase = phase;
2374 #if (NDEBUG & NDEBUG_INFORMATION)
2375 NCR5380_print_phase(instance);
2376 #endif
2379 if (sink && (phase != PHASE_MSGOUT)) {
2380 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2382 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2383 ICR_ASSERT_ACK);
2384 while (NCR5380_read(STATUS_REG) & SR_REQ);
2385 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2386 ICR_ASSERT_ATN);
2387 sink = 0;
2388 continue;
2391 switch (phase) {
2392 case PHASE_DATAIN:
2393 case PHASE_DATAOUT:
2394 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2395 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
2396 instance->host_no);
2397 sink = 1;
2398 do_abort(instance);
2399 cmd->result = DID_ERROR << 16;
2400 cmd->done(cmd);
2401 return;
2402 #endif
2404 * If there is no room left in the current buffer in the
2405 * scatter-gather list, move onto the next one.
2408 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2409 ++cmd->SCp.buffer;
2410 --cmd->SCp.buffers_residual;
2411 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2412 cmd->SCp.ptr = cmd->SCp.buffer->address;
2413 #if (NDEBUG & NDEBUG_INFORMATION)
2414 printk("scsi%d : %d bytes and %d buffers left\n",
2415 instance->host_no, cmd->SCp.this_residual,
2416 cmd->SCp.buffers_residual);
2417 #endif
2421 * The preferred transfer method is going to be
2422 * PSEUDO-DMA for systems that are strictly PIO,
2423 * since we can let the hardware do the handshaking.
2425 * For this to work, we need to know the transfersize
2426 * ahead of time, since the pseudo-DMA code will sit
2427 * in an unconditional loop.
2430 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2431 /* KLL
2432 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2433 * driver then it will always be defined, so the
2434 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2435 * NCR5380 case. I think this is a fairly clean solution.
2436 * We supplement these 2 if's with the flag.
2438 #ifdef NCR5380_dma_xfer_len
2439 if (!cmd->device->borken &&
2440 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2441 (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2442 #else
2443 transfersize = cmd->transfersize;
2445 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2446 if( transfersize > 512 )
2447 transfersize = 512;
2448 #endif /* LIMIT_TRANSFERSIZE */
2450 if (!cmd->device->borken && transfersize &&
2451 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2452 cmd->SCp.this_residual && !(cmd->SCp.this_residual %
2453 transfersize)) {
2454 /* Limit transfers to 32K, for xx400 & xx406
2455 * pseudoDMA that transfers in 128 bytes blocks. */
2456 if (transfersize > 32*1024)
2457 transfersize = 32*1024;
2458 #endif
2459 len = transfersize;
2460 if (NCR5380_transfer_dma(instance, &phase,
2461 &len, (unsigned char **) &cmd->SCp.ptr)) {
2463 * If the watchdog timer fires, all future accesses to this
2464 * device will use the polled-IO.
2466 printk("scsi%d : switching target %d lun %d to slow handshake\n",
2467 instance->host_no, cmd->target, cmd->lun);
2468 cmd->device->borken = 1;
2469 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2470 ICR_ASSERT_ATN);
2471 sink = 1;
2472 do_abort(instance);
2473 cmd->result = DID_ERROR << 16;
2474 cmd->done(cmd);
2475 /* XXX - need to source or sink data here, as appropriate */
2476 } else
2477 cmd->SCp.this_residual -= transfersize - len;
2478 } else
2479 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2480 NCR5380_transfer_pio(instance, &phase,
2481 (int *) &cmd->SCp.this_residual, (unsigned char **)
2482 &cmd->SCp.ptr);
2483 break;
2484 case PHASE_MSGIN:
2485 len = 1;
2486 data = &tmp;
2487 NCR5380_transfer_pio(instance, &phase, &len, &data);
2488 cmd->SCp.Message = tmp;
2490 switch (tmp) {
2492 * Linking lets us reduce the time required to get the
2493 * next command out to the device, hopefully this will
2494 * mean we don't waste another revolution due to the delays
2495 * required by ARBITRATION and another SELECTION.
2497 * In the current implementation proposal, low level drivers
2498 * merely have to start the next command, pointed to by
2499 * next_link, done() is called as with unlinked commands.
2501 #ifdef LINKED
2502 case LINKED_CMD_COMPLETE:
2503 case LINKED_FLG_CMD_COMPLETE:
2504 /* Accept message by clearing ACK */
2505 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2507 #if (NDEBUG & NDEBUG_LINKED)
2508 printk("scsi%d : target %d lun %d linked command complete.\n",
2509 instance->host_no, cmd->target, cmd->lun);
2510 #endif
2512 * Sanity check : A linked command should only terminate with
2513 * one of these messages if there are more linked commands
2514 * available.
2517 if (!cmd->next_link) {
2518 printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
2519 instance->host_no, cmd->target, cmd->lun);
2520 sink = 1;
2521 do_abort (instance);
2522 return;
2525 initialize_SCp(cmd->next_link);
2526 /* The next command is still part of this process */
2527 cmd->next_link->tag = cmd->tag;
2528 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2529 #if (NDEBUG & NDEBUG_LINKED)
2530 printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
2531 instance->host_no, cmd->target, cmd->lun);
2532 #endif
2533 #ifdef NCR5380_STATS
2534 collect_stats(hostdata, cmd);
2535 #endif
2536 cmd->scsi_done(cmd);
2537 cmd = hostdata->connected;
2538 break;
2539 #endif /* def LINKED */
2540 case ABORT:
2541 case COMMAND_COMPLETE:
2542 /* Accept message by clearing ACK */
2543 sink = 1;
2544 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2545 hostdata->connected = NULL;
2546 #if (NDEBUG & NDEBUG_QUEUES)
2547 printk("scsi%d : command for target %d, lun %d completed\n",
2548 instance->host_no, cmd->target, cmd->lun);
2549 #endif
2550 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2553 * I'm not sure what the correct thing to do here is :
2555 * If the command that just executed is NOT a request
2556 * sense, the obvious thing to do is to set the result
2557 * code to the values of the stored parameters.
2559 * If it was a REQUEST SENSE command, we need some way
2560 * to differentiate between the failure code of the original
2561 * and the failure code of the REQUEST sense - the obvious
2562 * case is success, where we fall through and leave the result
2563 * code unchanged.
2565 * The non-obvious place is where the REQUEST SENSE failed
2568 if (cmd->cmnd[0] != REQUEST_SENSE)
2569 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2570 else if (cmd->SCp.Status != GOOD)
2571 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2573 #ifdef AUTOSENSE
2574 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2575 (cmd->SCp.Status == CHECK_CONDITION)) {
2576 #if (NDEBUG & NDEBUG_AUTOSENSE)
2577 printk("scsi%d : performing request sense\n",
2578 instance->host_no);
2579 #endif
2580 cmd->cmnd[0] = REQUEST_SENSE;
2581 cmd->cmnd[1] &= 0xe0;
2582 cmd->cmnd[2] = 0;
2583 cmd->cmnd[3] = 0;
2584 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2585 cmd->cmnd[5] = 0;
2587 cmd->SCp.buffer = NULL;
2588 cmd->SCp.buffers_residual = 0;
2589 cmd->SCp.ptr = (char *) cmd->sense_buffer;
2590 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2592 cli();
2593 LIST(cmd,hostdata->issue_queue);
2594 cmd->host_scribble = (unsigned char *)
2595 hostdata->issue_queue;
2596 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2597 sti();
2598 #if (NDEBUG & NDEBUG_QUEUES)
2599 printk("scsi%d : REQUEST SENSE added to head of issue queue\n",instance->host_no);
2600 #endif
2601 } else {
2602 #endif /* def AUTOSENSE */
2603 #ifdef NCR5380_STATS
2604 collect_stats(hostdata, cmd);
2605 #endif
2606 cmd->scsi_done(cmd);
2609 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2611 * Restore phase bits to 0 so an interrupted selection,
2612 * arbitration can resume.
2614 NCR5380_write(TARGET_COMMAND_REG, 0);
2616 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2617 barrier();
2618 return;
2619 case MESSAGE_REJECT:
2620 /* Accept message by clearing ACK */
2621 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2622 switch (hostdata->last_message) {
2623 case HEAD_OF_QUEUE_TAG:
2624 case ORDERED_QUEUE_TAG:
2625 case SIMPLE_QUEUE_TAG:
2626 cmd->device->tagged_queue = 0;
2627 hostdata->busy[cmd->target] |= (1 << cmd->lun);
2628 break;
2629 default:
2630 break;
2632 case DISCONNECT:
2633 /* Accept message by clearing ACK */
2634 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2635 cmd->device->disconnect = 1;
2636 cli();
2637 LIST(cmd,hostdata->disconnected_queue);
2638 cmd->host_scribble = (unsigned char *)
2639 hostdata->disconnected_queue;
2640 hostdata->connected = NULL;
2641 hostdata->disconnected_queue = cmd;
2642 sti();
2643 #if (NDEBUG & NDEBUG_QUEUES)
2644 printk("scsi%d : command for target %d lun %d was moved from connected to"
2645 " the disconnected_queue\n", instance->host_no,
2646 cmd->target, cmd->lun);
2647 #endif
2649 * Restore phase bits to 0 so an interrupted selection,
2650 * arbitration can resume.
2652 NCR5380_write(TARGET_COMMAND_REG, 0);
2654 /* Enable reselect interrupts */
2655 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2656 /* Wait for bus free to avoid nasty timeouts */
2657 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2658 barrier();
2659 #if 0
2660 NCR5380_print_status(instance);
2661 #endif
2662 return;
2664 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2665 * operation, in violation of the SCSI spec so we can safely
2666 * ignore SAVE/RESTORE pointers calls.
2668 * Unfortunately, some disks violate the SCSI spec and
2669 * don't issue the required SAVE_POINTERS message before
2670 * disconnecting, and we have to break spec to remain
2671 * compatible.
2673 case SAVE_POINTERS:
2674 case RESTORE_POINTERS:
2675 /* Accept message by clearing ACK */
2676 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2677 break;
2678 case EXTENDED_MESSAGE:
2680 * Extended messages are sent in the following format :
2681 * Byte
2682 * 0 EXTENDED_MESSAGE == 1
2683 * 1 length (includes one byte for code, doesn't
2684 * include first two bytes)
2685 * 2 code
2686 * 3..length+1 arguments
2688 * Start the extended message buffer with the EXTENDED_MESSAGE
2689 * byte, since print_msg() wants the whole thing.
2691 extended_msg[0] = EXTENDED_MESSAGE;
2692 /* Accept first byte by clearing ACK */
2693 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2695 #if (NDEBUG & NDEBUG_EXTENDED)
2696 printk("scsi%d : receiving extended message\n",
2697 instance->host_no);
2698 #endif
2700 len = 2;
2701 data = extended_msg + 1;
2702 phase = PHASE_MSGIN;
2703 NCR5380_transfer_pio(instance, &phase, &len, &data);
2705 #if (NDEBUG & NDEBUG_EXTENDED)
2706 printk("scsi%d : length=%d, code=0x%02x\n",
2707 instance->host_no, (int) extended_msg[1],
2708 (int) extended_msg[2]);
2709 #endif
2711 if (!len && extended_msg[1] <=
2712 (sizeof (extended_msg) - 1)) {
2713 /* Accept third byte by clearing ACK */
2714 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2715 len = extended_msg[1] - 1;
2716 data = extended_msg + 3;
2717 phase = PHASE_MSGIN;
2719 NCR5380_transfer_pio(instance, &phase, &len, &data);
2721 #if (NDEBUG & NDEBUG_EXTENDED)
2722 printk("scsi%d : message received, residual %d\n",
2723 instance->host_no, len);
2724 #endif
2726 switch (extended_msg[2]) {
2727 case EXTENDED_SDTR:
2728 case EXTENDED_WDTR:
2729 case EXTENDED_MODIFY_DATA_POINTER:
2730 case EXTENDED_EXTENDED_IDENTIFY:
2731 tmp = 0;
2733 } else if (len) {
2734 printk("scsi%d: error receiving extended message\n",
2735 instance->host_no);
2736 tmp = 0;
2737 } else {
2738 printk("scsi%d: extended message code %02x length %d is too long\n",
2739 instance->host_no, extended_msg[2], extended_msg[1]);
2740 tmp = 0;
2742 /* Fall through to reject message */
2745 * If we get something weird that we aren't expecting,
2746 * reject it.
2748 default:
2749 if (!tmp) {
2750 printk("scsi%d: rejecting message ", instance->host_no);
2751 print_msg (extended_msg);
2752 printk("\n");
2753 } else if (tmp != EXTENDED_MESSAGE)
2754 printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n",
2755 instance->host_no, tmp, cmd->target, cmd->lun);
2756 else
2757 printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n",
2758 instance->host_no, extended_msg[1], extended_msg[0], cmd->target, cmd->lun);
2760 msgout = MESSAGE_REJECT;
2761 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2762 ICR_ASSERT_ATN);
2763 break;
2764 } /* switch (tmp) */
2765 break;
2766 case PHASE_MSGOUT:
2767 len = 1;
2768 data = &msgout;
2769 hostdata->last_message = msgout;
2770 NCR5380_transfer_pio(instance, &phase, &len, &data);
2771 if (msgout == ABORT) {
2772 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2773 hostdata->connected = NULL;
2774 cmd->result = DID_ERROR << 16;
2775 #ifdef NCR5380_STATS
2776 collect_stats(hostdata, cmd);
2777 #endif
2778 cmd->scsi_done(cmd);
2779 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2780 return;
2782 msgout = NOP;
2783 break;
2784 case PHASE_CMDOUT:
2785 len = cmd->cmd_len;
2786 data = cmd->cmnd;
2788 * XXX for performance reasons, on machines with a
2789 * PSEUDO-DMA architecture we should probably
2790 * use the dma transfer function.
2792 NCR5380_transfer_pio(instance, &phase, &len,
2793 &data);
2794 #ifdef USLEEP
2795 if (!disconnect && should_disconnect(cmd->cmnd[0])) {
2796 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2797 #if (NDEBUG & NDEBUG_USLEEP)
2798 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
2799 hostdata->time_expires);
2800 #endif
2801 NCR5380_set_timer (instance);
2802 return;
2804 #endif /* def USLEEP */
2805 break;
2806 case PHASE_STATIN:
2807 len = 1;
2808 data = &tmp;
2809 NCR5380_transfer_pio(instance, &phase, &len, &data);
2810 cmd->SCp.Status = tmp;
2811 break;
2812 default:
2813 printk("scsi%d : unknown phase\n", instance->host_no);
2814 #ifdef NDEBUG
2815 NCR5380_print(instance);
2816 #endif
2817 } /* switch(phase) */
2818 } /* if (tmp * SR_REQ) */
2819 #ifdef USLEEP
2820 else {
2821 if (!disconnect && hostdata->time_expires && jiffies >
2822 hostdata->time_expires) {
2823 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2824 #if (NDEBUG & NDEBUG_USLEEP)
2825 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
2826 hostdata->time_expires);
2827 #endif
2828 NCR5380_set_timer (instance);
2829 return;
2832 #endif
2833 } /* while (1) */
2837 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2839 * Purpose : does reselection, initializing the instance->connected
2840 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2841 * nexus has been reestablished,
2843 * Inputs : instance - this instance of the NCR5380.
2848 static void NCR5380_reselect (struct Scsi_Host *instance) {
2849 NCR5380_local_declare();
2850 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2851 instance->hostdata;
2852 unsigned char target_mask;
2853 unsigned char lun, phase;
2854 int len;
2855 #ifdef SCSI2
2856 unsigned char tag;
2857 #endif
2858 unsigned char msg[3];
2859 unsigned char *data;
2860 Scsi_Cmnd *tmp = NULL, *prev;
2861 int abort = 0;
2862 NCR5380_setup(instance);
2865 * Disable arbitration, etc. since the host adapter obviously
2866 * lost, and tell an interrupted NCR5380_select() to restart.
2869 NCR5380_write(MODE_REG, MR_BASE);
2870 hostdata->restart_select = 1;
2872 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2874 #if (NDEBUG & NDEBUG_RESELECTION)
2875 printk("scsi%d : reselect\n", instance->host_no);
2876 #endif
2879 * At this point, we have detected that our SCSI ID is on the bus,
2880 * SEL is true and BSY was false for at least one bus settle delay
2881 * (400 ns).
2883 * We must assert BSY ourselves, until the target drops the SEL
2884 * signal.
2887 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2889 while (NCR5380_read(STATUS_REG) & SR_SEL);
2890 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2893 * Wait for target to go into MSGIN.
2896 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2898 len = 1;
2899 data = msg;
2900 phase = PHASE_MSGIN;
2901 NCR5380_transfer_pio(instance, &phase, &len, &data);
2904 if (!msg[0] & 0x80) {
2905 printk("scsi%d : expecting IDENTIFY message, got ",
2906 instance->host_no);
2907 print_msg(msg);
2908 abort = 1;
2909 } else {
2910 /* Accept message by clearing ACK */
2911 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2912 lun = (msg[0] & 0x07);
2915 * We need to add code for SCSI-II to track which devices have
2916 * I_T_L_Q nexuses established, and which have simple I_T_L
2917 * nexuses so we can chose to do additional data transfer.
2920 #ifdef SCSI2
2921 #error "SCSI-II tagged queueing is not supported yet"
2922 #endif
2925 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2926 * just reestablished, and remove it from the disconnected queue.
2930 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2931 tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
2932 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2933 #ifdef SCSI2
2934 && (tag == tmp->tag)
2935 #endif
2937 if (prev) {
2938 REMOVE(prev,prev->host_scribble,tmp,tmp->host_scribble);
2939 prev->host_scribble = tmp->host_scribble;
2940 } else {
2941 REMOVE(-1,hostdata->disconnected_queue,tmp,tmp->host_scribble);
2942 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2944 tmp->host_scribble = NULL;
2945 break;
2948 if (!tmp) {
2949 #ifdef SCSI2
2950 printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
2951 instance->host_no, target_mask, lun, tag);
2952 #else
2953 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
2954 instance->host_no, target_mask, lun);
2955 #endif
2957 * Since we have an established nexus that we can't do anything with,
2958 * we must abort it.
2960 abort = 1;
2964 if (abort) {
2965 do_abort (instance);
2966 } else {
2967 hostdata->connected = tmp;
2968 #if (NDEBUG & NDEBUG_RESELECTION)
2969 printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
2970 instance->host_no, tmp->target, tmp->lun, tmp->tag);
2971 #endif
2976 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2978 * Purpose : called by interrupt handler when DMA finishes or a phase
2979 * mismatch occurs (which would finish the DMA transfer).
2981 * Inputs : instance - this instance of the NCR5380.
2983 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2984 * nexus has been reestablished, on failure NULL is returned.
2987 #ifdef REAL_DMA
2988 static void NCR5380_dma_complete (NCR5380_instance *instance) {
2989 NCR5380_local_declare();
2990 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *
2991 instance->hostdata);
2992 int transferred;
2993 NCR5380_setup(instance);
2996 * XXX this might not be right.
2998 * Wait for final byte to transfer, ie wait for ACK to go false.
3000 * We should use the Last Byte Sent bit, unfortunately this is
3001 * not available on the 5380/5381 (only the various CMOS chips)
3004 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
3006 NCR5380_write(MODE_REG, MR_BASE);
3007 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3010 * The only places we should see a phase mismatch and have to send
3011 * data from the same set of pointers will be the data transfer
3012 * phases. So, residual, requested length are only important here.
3015 if (!(hostdata->connected->SCp.phase & SR_CD)) {
3016 transferred = instance->dmalen - NCR5380_dma_residual();
3017 hostdata->connected->SCp.this_residual -= transferred;
3018 hostdata->connected->SCp.ptr += transferred;
3021 #endif /* def REAL_DMA */
3024 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
3026 * Purpose : abort a command
3028 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
3029 * host byte of the result field to, if zero DID_ABORTED is
3030 * used.
3032 * Returns : 0 - success, -1 on failure.
3034 * XXX - there is no way to abort the command that is currently
3035 * connected, you have to wait for it to complete. If this is
3036 * a problem, we could implement longjmp() / setjmp(), setjmp()
3037 * called where the loop started in NCR5380_main().
3040 #ifndef NCR5380_abort
3041 static
3042 #endif
3043 int NCR5380_abort (Scsi_Cmnd *cmd) {
3044 NCR5380_local_declare();
3045 struct Scsi_Host *instance = cmd->host;
3046 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
3047 instance->hostdata;
3048 Scsi_Cmnd *tmp, **prev;
3050 printk("scsi%d : aborting command\n", instance->host_no);
3051 print_Scsi_Cmnd (cmd);
3053 NCR5380_print_status (instance);
3055 printk("scsi%d : aborting command\n", instance->host_no);
3056 print_Scsi_Cmnd (cmd);
3058 NCR5380_print_status (instance);
3060 cli();
3061 NCR5380_setup(instance);
3063 #if (NDEBUG & NDEBUG_ABORT)
3064 printk("scsi%d : abort called\n", instance->host_no);
3065 printk(" basr 0x%X, sr 0x%X\n",
3066 NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
3067 #endif
3069 #if 0
3071 * Case 1 : If the command is the currently executing command,
3072 * we'll set the aborted flag and return control so that
3073 * information transfer routine can exit cleanly.
3076 if (hostdata->connected == cmd) {
3077 #if (NDEBUG & NDEBUG_ABORT)
3078 printk("scsi%d : aborting connected command\n", instance->host_no);
3079 #endif
3080 hostdata->aborted = 1;
3082 * We should perform BSY checking, and make sure we haven't slipped
3083 * into BUS FREE.
3086 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
3088 * Since we can't change phases until we've completed the current
3089 * handshake, we have to source or sink a byte of data if the current
3090 * phase is not MSGOUT.
3094 * Return control to the executing NCR drive so we can clear the
3095 * aborted flag and get back into our main loop.
3098 return 0;
3100 #endif
3103 * Case 2 : If the command hasn't been issued yet, we simply remove it
3104 * from the issue queue.
3106 #if (NDEBUG & NDEBUG_ABORT)
3107 /* KLL */
3108 printk("scsi%d : abort going into loop.\n", instance->host_no);
3109 #endif
3110 for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue),
3111 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
3112 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp =
3113 (Scsi_Cmnd *) tmp->host_scribble)
3114 if (cmd == tmp) {
3115 REMOVE(5,*prev,tmp,tmp->host_scribble);
3116 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
3117 tmp->host_scribble = NULL;
3118 tmp->result = DID_ABORT << 16;
3119 sti();
3120 #if (NDEBUG & NDEBUG_ABORT)
3121 printk("scsi%d : abort removed command from issue queue.\n",
3122 instance->host_no);
3123 #endif
3124 tmp->done(tmp);
3125 return SCSI_ABORT_SUCCESS;
3127 #if (NDEBUG & NDEBUG_ABORT)
3128 /* KLL */
3129 else if (prev == tmp) printk("scsi%d : LOOP\n", instance->host_no);
3130 #endif
3133 * Case 3 : If any commands are connected, we're going to fail the abort
3134 * and let the high level SCSI driver retry at a later time or
3135 * issue a reset.
3137 * Timeouts, and therefore aborted commands, will be highly unlikely
3138 * and handling them cleanly in this situation would make the common
3139 * case of noresets less efficient, and would pollute our code. So,
3140 * we fail.
3143 if (hostdata->connected) {
3144 sti();
3145 #if (NDEBUG & NDEBUG_ABORT)
3146 printk("scsi%d : abort failed, command connected.\n", instance->host_no);
3147 #endif
3148 return SCSI_ABORT_NOT_RUNNING;
3152 * Case 4: If the command is currently disconnected from the bus, and
3153 * there are no connected commands, we reconnect the I_T_L or
3154 * I_T_L_Q nexus associated with it, go into message out, and send
3155 * an abort message.
3157 * This case is especially ugly. In order to reestablish the nexus, we
3158 * need to call NCR5380_select(). The easiest way to implement this
3159 * function was to abort if the bus was busy, and let the interrupt
3160 * handler triggered on the SEL for reselect take care of lost arbitrations
3161 * where necessary, meaning interrupts need to be enabled.
3163 * When interrupts are enabled, the queues may change - so we
3164 * can't remove it from the disconnected queue before selecting it
3165 * because that could cause a failure in hashing the nexus if that
3166 * device reselected.
3168 * Since the queues may change, we can't use the pointers from when we
3169 * first locate it.
3171 * So, we must first locate the command, and if NCR5380_select()
3172 * succeeds, then issue the abort, relocate the command and remove
3173 * it from the disconnected queue.
3176 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
3177 tmp = (Scsi_Cmnd *) tmp->host_scribble)
3178 if (cmd == tmp) {
3179 sti();
3180 #if (NDEBUG & NDEBUG_ABORT)
3181 printk("scsi%d : aborting disconnected command.\n", instance->host_no);
3182 #endif
3184 if (NCR5380_select (instance, cmd, (int) cmd->tag))
3185 return SCSI_ABORT_BUSY;
3187 #if (NDEBUG & NDEBUG_ABORT)
3188 printk("scsi%d : nexus reestablished.\n", instance->host_no);
3189 #endif
3191 do_abort (instance);
3193 cli();
3194 for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue),
3195 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
3196 tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp =
3197 (Scsi_Cmnd *) tmp->host_scribble)
3198 if (cmd == tmp) {
3199 REMOVE(5,*prev,tmp,tmp->host_scribble);
3200 *prev = (Scsi_Cmnd *) tmp->host_scribble;
3201 tmp->host_scribble = NULL;
3202 tmp->result = DID_ABORT << 16;
3203 sti();
3204 tmp->done(tmp);
3205 return SCSI_ABORT_SUCCESS;
3210 * Case 5 : If we reached this point, the command was not found in any of
3211 * the queues.
3213 * We probably reached this point because of an unlikely race condition
3214 * between the command completing successfully and the abortion code,
3215 * so we won't panic, but we will notify the user in case something really
3216 * broke.
3219 sti();
3220 printk("scsi%d : warning : SCSI command probably completed successfully\n"
3221 " before abortion\n", instance->host_no);
3222 return SCSI_ABORT_NOT_RUNNING;
3227 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
3229 * Purpose : reset the SCSI bus.
3231 * Returns : SCSI_RESET_WAKEUP
3235 #ifndef NCR5380_reset
3236 static
3237 #endif
3238 int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int dummy) {
3239 NCR5380_local_declare();
3240 NCR5380_setup(cmd->host);
3242 NCR5380_print_status (cmd->host);
3243 do_reset (cmd->host);
3245 return SCSI_RESET_WAKEUP;