Import 2.3.18pre1
[davej-history.git] / drivers / scsi / NCR5380.c
blobdf412e35bba17d3d4927ab45be2507126e6d24d6
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 $
35 * Revision 1.10 1998/9/2 Alan Cox
36 * (alan@redhat.com)
37 * Fixed up the timer lockups reported so far. Things still suck. Looking
38 * forward to 2.3 and per device request queues. Then it'll be possible to
39 * SMP thread this beast and improve life no end.
41 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
42 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
43 * (hopefully) fixed and enhanced USLEEP
44 * added support for DTC3181E card (for Mustek scanner)
47 * Revision 1.8 Ingmar Baumgart
48 * (ingmar@gonzo.schwaben.de)
49 * added support for NCR53C400a card
52 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
53 * added proc_info
54 * added support needed for DTC 3180/3280
55 * fixed a couple of bugs
58 * Revision 1.5 1994/01/19 09:14:57 drew
59 * Fixed udelay() hack that was being used on DATAOUT phases
60 * instead of a proper wait for the final handshake.
62 * Revision 1.4 1994/01/19 06:44:25 drew
63 * *** empty log message ***
65 * Revision 1.3 1994/01/19 05:24:40 drew
66 * Added support for TCR LAST_BYTE_SENT bit.
68 * Revision 1.2 1994/01/15 06:14:11 drew
69 * REAL DMA support, bug fixes.
71 * Revision 1.1 1994/01/15 06:00:54 drew
72 * Initial revision
77 * Further development / testing that should be done :
78 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
79 * code so that everything does the same thing that's done at the
80 * end of a pseudo-DMA read operation.
82 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
83 * basically, transfer size needs to be reduced by one
84 * and the last byte read as is done with PSEUDO_DMA.
86 * 3. Test USLEEP code
88 * 4. Test SCSI-II tagged queueing (I have no devices which support
89 * tagged queueing)
91 * 5. Test linked command handling code after Eric is ready with
92 * the high level code.
95 #if (NDEBUG & NDEBUG_LISTS)
96 #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
97 #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); }
98 #else
99 #define LIST(x,y)
100 #define REMOVE(w,x,y,z)
101 #endif
103 #ifndef notyet
104 #undef LINKED
105 #undef REAL_DMA
106 #endif
108 #ifdef REAL_DMA_POLL
109 #undef READ_OVERRUNS
110 #define READ_OVERRUNS
111 #endif
114 * Design
115 * Issues :
117 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
118 * and specifically for each board rather than each chip. This makes their
119 * adaptation to platforms like the Mac (Some of which use NCR5380's)
120 * more difficult than it has to be.
122 * Also, many of the SCSI drivers were written before the command queuing
123 * routines were implemented, meaning their implementations of queued
124 * commands were hacked on rather than designed in from the start.
126 * When I designed the Linux SCSI drivers I figured that
127 * while having two different SCSI boards in a system might be useful
128 * for debugging things, two of the same type wouldn't be used.
129 * Well, I was wrong and a number of users have mailed me about running
130 * multiple high-performance SCSI boards in a server.
132 * Finally, when I get questions from users, I have no idea what
133 * revision of my driver they are running.
135 * This driver attempts to address these problems :
136 * This is a generic 5380 driver. To use it on a different platform,
137 * one simply writes appropriate system specific macros (ie, data
138 * transfer - some PC's will use the I/O bus, 68K's must use
139 * memory mapped) and drops this file in their 'C' wrapper.
141 * As far as command queueing, two queues are maintained for
142 * each 5380 in the system - commands that haven't been issued yet,
143 * and commands that are currently executing. This means that an
144 * unlimited number of commands may be queued, letting
145 * more commands propagate from the higher driver levels giving higher
146 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
147 * allowing multiple commands to propagate all the way to a SCSI-II device
148 * while a command is already executing.
150 * To solve the multiple-boards-in-the-same-system problem,
151 * there is a separate instance structure for each instance
152 * of a 5380 in the system. So, multiple NCR5380 drivers will
153 * be able to coexist with appropriate changes to the high level
154 * SCSI code.
156 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
157 * number (updated for each public release) printed by the
158 * NCR5380_print_options command, which should be called from the
159 * wrapper detect function, so that I know what release of the driver
160 * users are using.
162 * Issues specific to the NCR5380 :
164 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
165 * piece of hardware that requires you to sit in a loop polling for
166 * the REQ signal as long as you are connected. Some devices are
167 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
168 * while doing long seek operations.
170 * The workaround for this is to keep track of devices that have
171 * disconnected. If the device hasn't disconnected, for commands that
172 * should disconnect, we do something like
174 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
176 * Some tweaking of N and M needs to be done. An algorithm based
177 * on "time to data" would give the best results as long as short time
178 * to datas (ie, on the same track) were considered, however these
179 * broken devices are the exception rather than the rule and I'd rather
180 * spend my time optimizing for the normal case.
182 * Architecture :
184 * At the heart of the design is a coroutine, NCR5380_main,
185 * which is started when not running by the interrupt handler,
186 * timer, and queue command function. It attempts to establish
187 * I_T_L or I_T_L_Q nexuses by removing the commands from the
188 * issue queue and calling NCR5380_select() if a nexus
189 * is not established.
191 * Once a nexus is established, the NCR5380_information_transfer()
192 * phase goes through the various phases as instructed by the target.
193 * if the target goes into MSG IN and sends a DISCONNECT message,
194 * the command structure is placed into the per instance disconnected
195 * queue, and NCR5380_main tries to find more work. If USLEEP
196 * was defined, and the target is idle for too long, the system
197 * will try to sleep.
199 * If a command has disconnected, eventually an interrupt will trigger,
200 * calling NCR5380_intr() which will in turn call NCR5380_reselect
201 * to reestablish a nexus. This will run main if necessary.
203 * On command termination, the done function will be called as
204 * appropriate.
206 * SCSI pointers are maintained in the SCp field of SCSI command
207 * structures, being initialized after the command is connected
208 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
209 * Note that in violation of the standard, an implicit SAVE POINTERS operation
210 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
214 * Using this file :
215 * This file a skeleton Linux SCSI driver for the NCR 5380 series
216 * of chips. To use it, you write an architecture specific functions
217 * and macros and include this file in your driver.
219 * These macros control options :
220 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
221 * defined.
223 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
224 * for commands that return with a CHECK CONDITION status.
226 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
227 * transceivers.
229 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
230 * override-configure an IRQ.
232 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
233 * bytes at a time. Since interrupts are disabled by default during
234 * these transfers, we might need this to give reasonable interrupt
235 * service time if the transfer size gets too large.
237 * LINKED - if defined, linked commands are supported.
239 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
241 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
243 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
244 * rely on phase mismatch and EOP interrupts to determine end
245 * of phase.
247 * SCSI2 - if defined, SCSI-2 tagged queuing is used where possible
249 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
250 * only really want to use this if you're having a problem with
251 * dropped characters during high speed communications, and even
252 * then, you're going to be better off twiddling with transfersize
253 * in the high level code.
255 * USLEEP - if defined, on devices that aren't disconnecting from the
256 * bus, we will go to sleep so that the CPU can get real work done
257 * when we run a command that won't complete immediately.
259 * Defaults for these will be provided if USLEEP is defined, although
260 * the user may want to adjust these to allocate CPU resources to
261 * the SCSI driver or "real" code.
263 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
265 * USLEEP_POLL - amount of time, in jiffies, to poll
267 * These macros MUST be defined :
268 * NCR5380_local_declare() - declare any local variables needed for your
269 * transfer routines.
271 * NCR5380_setup(instance) - initialize any local variables needed from a given
272 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
274 * NCR5380_read(register) - read from the specified register
276 * NCR5380_write(register, value) - write to the specific register
278 * NCR5380_implementation_fields - additional fields needed for this
279 * specific implementation of the NCR5380
281 * Either real DMA *or* pseudo DMA may be implemented
282 * REAL functions :
283 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
284 * Note that the DMA setup functions should return the number of bytes
285 * that they were able to program the controller for.
287 * Also note that generic i386/PC versions of these macros are
288 * available as NCR5380_i386_dma_write_setup,
289 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
291 * NCR5380_dma_write_setup(instance, src, count) - initialize
292 * NCR5380_dma_read_setup(instance, dst, count) - initialize
293 * NCR5380_dma_residual(instance); - residual count
295 * PSEUDO functions :
296 * NCR5380_pwrite(instance, src, count)
297 * NCR5380_pread(instance, dst, count);
299 * If nothing specific to this implementation needs doing (ie, with external
300 * hardware), you must also define
302 * NCR5380_queue_command
303 * NCR5380_reset
304 * NCR5380_abort
305 * NCR5380_proc_info
307 * to be the global entry points into the specific driver, ie
308 * #define NCR5380_queue_command t128_queue_command.
310 * If this is not done, the routines will be defined as static functions
311 * with the NCR5380* names and the user must provide a globally
312 * accessible wrapper function.
314 * The generic driver is initialized by calling NCR5380_init(instance),
315 * after setting the appropriate host specific fields and ID. If the
316 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
317 * possible) function may be used. Before the specific driver initialization
318 * code finishes, NCR5380_print_options should be called.
321 static int do_abort(struct Scsi_Host *host);
322 static void do_reset(struct Scsi_Host *host);
323 static struct Scsi_Host *first_instance = NULL;
324 static Scsi_Host_Template *the_template = NULL;
326 #ifdef USLEEP
327 struct timer_list usleep_timer;
328 #endif
331 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
333 * Purpose : initialize the saved data pointers for cmd to point to the
334 * start of the buffer.
336 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
339 static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
342 * Initialize the Scsi Pointer field so that all of the commands in the
343 * various queues are valid.
346 if (cmd->use_sg) {
347 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
348 cmd->SCp.buffers_residual = cmd->use_sg - 1;
349 cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
350 cmd->SCp.this_residual = cmd->SCp.buffer->length;
351 } else {
352 cmd->SCp.buffer = NULL;
353 cmd->SCp.buffers_residual = 0;
354 cmd->SCp.ptr = (char *) cmd->request_buffer;
355 cmd->SCp.this_residual = cmd->request_bufflen;
359 #include <linux/delay.h>
361 #ifdef NDEBUG
362 static struct {
363 unsigned char mask;
364 const char *name;
365 } signals[] = { {
367 SR_DBP, "PARITY"
368 }, {
369 SR_RST, "RST"
370 }, {
371 SR_BSY, "BSY"
374 SR_REQ, "REQ"
375 }, {
376 SR_MSG, "MSG"
377 }, {
378 SR_CD, "CD"
379 }, {
380 SR_IO, "IO"
383 SR_SEL, "SEL"
384 }, {
385 0, NULL
389 basrs[] = { {
390 BASR_ATN, "ATN"
391 }, {
392 BASR_ACK, "ACK"
393 }, {
394 0, NULL
398 icrs[] = { {
399 ICR_ASSERT_RST, "ASSERT RST"
400 }, {
401 ICR_ASSERT_ACK, "ASSERT ACK"
404 ICR_ASSERT_BSY, "ASSERT BSY"
405 }, {
406 ICR_ASSERT_SEL, "ASSERT SEL"
409 ICR_ASSERT_ATN, "ASSERT ATN"
410 }, {
411 ICR_ASSERT_DATA, "ASSERT DATA"
414 0, NULL
418 mrs[] = { {
419 MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"
420 }, {
421 MR_TARGET, "MODE TARGET"
424 MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"
425 }, {
426 MR_ENABLE_PAR_INTR,
427 "MODE PARITY INTR"
428 }, {
429 MR_MONITOR_BSY, "MODE MONITOR BSY"
432 MR_DMA_MODE, "MODE DMA"
433 }, {
434 MR_ARBITRATE, "MODE ARBITRATION"
437 0, NULL
442 * Function : void NCR5380_print(struct Scsi_Host *instance)
444 * Purpose : print the SCSI bus signals for debugging purposes
446 * Input : instance - which NCR5380
449 static void NCR5380_print(struct Scsi_Host *instance)
451 NCR5380_local_declare();
452 unsigned long flags;
453 unsigned char status, data, basr, mr, icr, i;
454 NCR5380_setup(instance);
455 save_flags(flags);
456 cli();
457 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
458 status = NCR5380_read(STATUS_REG);
459 mr = NCR5380_read(MODE_REG);
460 icr = NCR5380_read(INITIATOR_COMMAND_REG);
461 basr = NCR5380_read(BUS_AND_STATUS_REG);
462 restore_flags(flags);
463 printk("STATUS_REG: %02x ", status);
464 for (i = 0; signals[i].mask; ++i)
465 if (status & signals[i].mask)
466 printk(",%s", signals[i].name);
467 printk("\nBASR: %02x ", basr);
468 for (i = 0; basrs[i].mask; ++i)
469 if (basr & basrs[i].mask)
470 printk(",%s", basrs[i].name);
471 printk("\nICR: %02x ", icr);
472 for (i = 0; icrs[i].mask; ++i)
473 if (icr & icrs[i].mask)
474 printk(",%s", icrs[i].name);
475 printk("\nMODE: %02x ", mr);
476 for (i = 0; mrs[i].mask; ++i)
477 if (mr & mrs[i].mask)
478 printk(",%s", mrs[i].name);
479 printk("\n");
482 static struct {
483 unsigned char value;
484 const char *name;
485 } phases[] = {
488 PHASE_DATAOUT, "DATAOUT"
489 }, {
490 PHASE_DATAIN, "DATAIN"
491 }, {
492 PHASE_CMDOUT, "CMDOUT"
495 PHASE_STATIN, "STATIN"
496 }, {
497 PHASE_MSGOUT, "MSGOUT"
498 }, {
499 PHASE_MSGIN, "MSGIN"
502 PHASE_UNKNOWN, "UNKNOWN"
507 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
509 * Purpose : print the current SCSI phase for debugging purposes
511 * Input : instance - which NCR5380
514 static void NCR5380_print_phase(struct Scsi_Host *instance)
516 NCR5380_local_declare();
517 unsigned char status;
518 int i;
519 NCR5380_setup(instance);
521 status = NCR5380_read(STATUS_REG);
522 if (!(status & SR_REQ))
523 printk("scsi%d : REQ not asserted, phase unknown.\n",
524 instance->host_no);
525 else {
526 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
527 (phases[i].value != (status & PHASE_MASK)); ++i);
528 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
531 #endif
534 * We need to have our coroutine active given these constraints :
535 * 1. The mutex flag, main_running, can only be set when the main
536 * routine can actually process data, otherwise SCSI commands
537 * will never get issued.
539 * 2. NCR5380_main() shouldn't be called before it has exited, because
540 * other drivers have had kernel stack overflows in similar
541 * situations.
543 * 3. We don't want to inline NCR5380_main() because of space concerns,
544 * even though it is only called in two places.
546 * So, the solution is to set the mutex in an inline wrapper for the
547 * main coroutine, and have the main coroutine exit with interrupts
548 * disabled after the final search through the queues so that no race
549 * conditions are possible.
552 static volatile int main_running = 0;
555 * Function : run_main(void)
557 * Purpose : insure that the coroutine is running and will process our
558 * request. main_running is checked/set here (in an inline function)
559 * rather than in NCR5380_main itself to reduce the chances of stack
560 * overflow.
564 static __inline__ void run_main(void)
566 unsigned long flags;
567 save_flags(flags);
568 cli();
569 if (!main_running) {
570 main_running = 1;
571 NCR5380_main();
573 restore_flags(flags);
576 #ifdef USLEEP
579 * These need tweaking, and would probably work best as per-device
580 * flags initialized differently for disk, tape, cd, etc devices.
581 * People with broken devices are free to experiment as to what gives
582 * the best results for them.
584 * USLEEP_SLEEP should be a minimum seek time.
586 * USLEEP_POLL should be a maximum rotational latency.
588 #ifndef USLEEP_SLEEP
589 /* 20 ms (reasonable hard disk speed) */
590 #define USLEEP_SLEEP (20*HZ/1000)
591 #endif
592 /* 300 RPM (floppy speed) */
593 #ifndef USLEEP_POLL
594 #define USLEEP_POLL (200*HZ/1000)
595 #endif
596 #ifndef USLEEP_WAITLONG
597 /* RvC: (reasonable time to wait on select error) */
598 #define USLEEP_WAITLONG USLEEP_SLEEP
599 #endif
601 static struct Scsi_Host *expires_first = NULL;
604 * Function : int should_disconnect (unsigned char cmd)
606 * Purpose : decide weather a command would normally disconnect or
607 * not, since if it won't disconnect we should go to sleep.
609 * Input : cmd - opcode of SCSI command
611 * Returns : DISCONNECT_LONG if we should disconnect for a really long
612 * time (ie always, sleep, look for REQ active, sleep),
613 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
614 * time-to-data delay, DISCONNECT_NONE if this command would return
615 * immediately.
617 * Future sleep algorithms based on time to data can exploit
618 * something like this so they can differentiate between "normal"
619 * (ie, read, write, seek) and unusual commands (ie, * format).
621 * Note : We don't deal with commands that handle an immediate disconnect,
625 static int should_disconnect(unsigned char cmd)
627 switch (cmd) {
628 case READ_6:
629 case WRITE_6:
630 case SEEK_6:
631 case READ_10:
632 case WRITE_10:
633 case SEEK_10:
634 return DISCONNECT_TIME_TO_DATA;
635 case FORMAT_UNIT:
636 case SEARCH_HIGH:
637 case SEARCH_LOW:
638 case SEARCH_EQUAL:
639 return DISCONNECT_LONG;
640 default:
641 return DISCONNECT_NONE;
646 * Assumes instance->time_expires has been set in higher level code.
649 static int NCR5380_set_timer(struct Scsi_Host *instance)
651 unsigned long flags;
652 struct Scsi_Host *tmp, **prev;
654 save_flags(flags);
655 cli();
656 if (((struct NCR5380_hostdata *) (instance->hostdata))->next_timer) {
657 restore_flags(flags);
658 return -1;
660 for (prev = &expires_first, tmp = expires_first; tmp;
661 prev = &(((struct NCR5380_hostdata *) tmp->hostdata)->next_timer),
662 tmp = ((struct NCR5380_hostdata *) tmp->hostdata)->next_timer)
663 if (((struct NCR5380_hostdata *)instance->hostdata)->time_expires <
664 ((struct NCR5380_hostdata *)tmp->hostdata)->time_expires)
665 break;
667 ((struct NCR5380_hostdata *) instance->hostdata)->next_timer = tmp;
668 *prev = instance;
670 del_timer(&usleep_timer);
671 usleep_timer.expires = ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires;
672 add_timer(&usleep_timer);
673 restore_flags(flags);
674 return 0;
677 /* Doing something about unwanted reentrancy here might be useful */
678 void NCR5380_timer_fn(unsigned long surplus_to_requirements)
680 unsigned long flags;
681 struct Scsi_Host *instance;
682 save_flags(flags);
683 cli();
684 for (; expires_first &&
685 time_before_eq(((struct NCR5380_hostdata *)expires_first->hostdata)->time_expires, jiffies); )
687 instance = ((struct NCR5380_hostdata *) expires_first->hostdata)->next_timer;
688 ((struct NCR5380_hostdata *) expires_first->hostdata)->next_timer = NULL;
689 ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires = 0;
690 expires_first = instance;
693 del_timer(&usleep_timer);
694 if (expires_first)
696 usleep_timer.expires = ((struct NCR5380_hostdata *)expires_first->hostdata)->time_expires;
697 add_timer(&usleep_timer);
699 restore_flags(flags);
701 spin_lock_irqsave(&io_request_lock, flags);
702 run_main();
703 spin_unlock_irqrestore(&io_request_lock, flags);
705 #endif /* def USLEEP */
707 static inline void NCR5380_all_init(void)
709 static int done = 0;
710 if (!done) {
711 #if (NDEBUG & NDEBUG_INIT)
712 printk("scsi : NCR5380_all_init()\n");
713 #endif
714 done = 1;
715 #ifdef USLEEP
716 init_timer(&usleep_timer);
717 usleep_timer.function = NCR5380_timer_fn;
718 #endif
722 #ifdef AUTOPROBE_IRQ
724 * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
726 * Purpose : autoprobe for the IRQ line used by the NCR5380.
728 * Inputs : instance - pointer to this instance of the NCR5380 driver,
729 * possible - bitmask of permissible interrupts.
731 * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired.
733 * XXX no effort is made to deal with spurious interrupts.
737 static int probe_irq __initdata = 0;
739 static void __init probe_intr(int irq, void *dev_id, struct pt_regs *regs)
741 probe_irq = irq;
744 static int __init NCR5380_probe_irq(struct Scsi_Host *instance, int possible)
746 NCR5380_local_declare();
747 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
748 instance->hostdata;
749 unsigned long timeout;
750 int trying_irqs, i, mask;
751 NCR5380_setup(instance);
753 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
754 if ((mask & possible) && (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL)
755 == 0))
756 trying_irqs |= mask;
758 timeout = jiffies + (250 * HZ / 1000);
759 probe_irq = IRQ_NONE;
762 * A interrupt is triggered whenever BSY = false, SEL = true
763 * and a bit set in the SELECT_ENABLE_REG is asserted on the
764 * SCSI bus.
766 * Note that the bus is only driven when the phase control signals
767 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
768 * to zero.
771 NCR5380_write(TARGET_COMMAND_REG, 0);
772 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
773 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
774 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
775 ICR_ASSERT_SEL);
777 while (probe_irq == IRQ_NONE && time_before(jiffies,timeout))
778 barrier();
780 NCR5380_write(SELECT_ENABLE_REG, 0);
781 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
783 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
784 if (trying_irqs & mask)
785 free_irq(i, NULL);
787 return probe_irq;
789 #endif /* AUTOPROBE_IRQ */
792 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
794 * Purpose : called by probe code indicating the NCR5380 driver
795 * options that were selected.
797 * Inputs : instance, pointer to this instance. Unused.
800 static void __init NCR5380_print_options(struct Scsi_Host *instance)
802 printk(" generic options"
803 #ifdef AUTOPROBE_IRQ
804 " AUTOPROBE_IRQ"
805 #endif
806 #ifdef AUTOSENSE
807 " AUTOSENSE"
808 #endif
809 #ifdef DIFFERENTIAL
810 " DIFFERENTIAL"
811 #endif
812 #ifdef REAL_DMA
813 " REAL DMA"
814 #endif
815 #ifdef REAL_DMA_POLL
816 " REAL DMA POLL"
817 #endif
818 #ifdef PARITY
819 " PARITY"
820 #endif
821 #ifdef PSEUDO_DMA
822 " PSEUDO DMA"
823 #endif
824 #ifdef SCSI2
825 " SCSI-2"
826 #endif
827 #ifdef UNSAFE
828 " UNSAFE "
829 #endif
831 #ifdef USLEEP
832 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
833 #endif
834 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
835 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
836 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
841 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
843 * Purpose : print commands in the various queues, called from
844 * NCR5380_abort and NCR5380_debug to aid debugging.
846 * Inputs : instance, pointer to this instance.
849 static void NCR5380_print_status(struct Scsi_Host *instance)
851 static char pr_bfr[512];
852 char *start;
853 int len;
855 printk("NCR5380 : coroutine is%s running.\n",
856 main_running ? "" : "n't");
858 #ifdef NDEBUG
859 NCR5380_print(instance);
860 NCR5380_print_phase(instance);
861 #endif
863 len = NCR5380_proc_info(pr_bfr, &start, 0, sizeof(pr_bfr),
864 instance->host_no, 0);
865 pr_bfr[len] = 0;
866 printk("\n%s\n", pr_bfr);
869 /******************************************/
871 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
873 * *buffer: I/O buffer
874 * **start: if inout == FALSE pointer into buffer where user read should start
875 * offset: current offset
876 * length: length of buffer
877 * hostno: Scsi_Host host_no
878 * inout: TRUE - user is writing; FALSE - user is reading
880 * Return the number of bytes read from or written
883 #undef SPRINTF
884 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
885 static
886 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
887 static
888 char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
889 static
890 char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
892 #ifndef NCR5380_proc_info
893 static
894 #endif
895 int NCR5380_proc_info(
896 char *buffer, char **start, off_t offset,
897 int length, int hostno, int inout)
899 unsigned long flags;
900 char *pos = buffer;
901 struct Scsi_Host *instance;
902 struct NCR5380_hostdata *hostdata;
903 Scsi_Cmnd *ptr;
905 for (instance = first_instance; instance &&
906 instance->host_no != hostno; instance = instance->next);
907 if (!instance)
908 return (-ESRCH);
909 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
911 if (inout) { /* Has data been written to the file ? */
912 #ifdef DTC_PUBLIC_RELEASE
913 dtc_wmaxi = dtc_maxi = 0;
914 #endif
915 #ifdef PAS16_PUBLIC_RELEASE
916 pas_wmaxi = pas_maxi = 0;
917 #endif
918 return (-ENOSYS); /* Currently this is a no-op */
920 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
921 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
922 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
923 #ifdef DTC_PUBLIC_RELEASE
924 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
925 #endif
926 #ifdef T128_PUBLIC_RELEASE
927 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
928 #endif
929 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
930 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
931 #endif
932 #ifdef PAS16_PUBLIC_RELEASE
933 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
934 #endif
936 SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
937 SPRINTF("io_port: %04x ", (int) instance->io_port);
938 if (instance->irq == IRQ_NONE)
939 SPRINTF("IRQ: None.\n");
940 else
941 SPRINTF("IRQ: %d.\n", instance->irq);
943 #ifdef DTC_PUBLIC_RELEASE
944 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
945 dtc_wmaxi, dtc_maxi);
946 #endif
947 #ifdef PAS16_PUBLIC_RELEASE
948 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
949 pas_wmaxi, pas_maxi);
950 #endif
951 save_flags(flags);
952 cli();
953 SPRINTF("NCR5380 : coroutine is%s running.\n", main_running ? "" : "n't");
954 if (!hostdata->connected)
955 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
956 else
957 pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected,
958 pos, buffer, length);
959 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
960 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr;
961 ptr = (Scsi_Cmnd *) ptr->host_scribble)
962 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
964 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
965 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
966 ptr = (Scsi_Cmnd *) ptr->host_scribble)
967 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
969 restore_flags(flags);
970 *start = buffer;
971 if (pos - buffer < offset)
972 return 0;
973 else if (pos - buffer - offset < length)
974 return pos - buffer - offset;
975 return length;
978 static
979 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
981 SPRINTF("scsi%d : destination target %d, lun %d\n",
982 cmd->host->host_no, cmd->target, cmd->lun);
983 SPRINTF(" command = ");
984 pos = lprint_command(cmd->cmnd, pos, buffer, length);
985 return (pos);
988 static
989 char *lprint_command(unsigned char *command,
990 char *pos, char *buffer, int length)
992 int i, s;
993 pos = lprint_opcode(command[0], pos, buffer, length);
994 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
995 SPRINTF("%02x ", command[i]);
996 SPRINTF("\n");
997 return (pos);
1000 static
1001 char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
1003 SPRINTF("%2d (0x%02x)", opcode, opcode);
1004 return (pos);
1009 * Function : void NCR5380_init (struct Scsi_Host *instance, flags)
1011 * Purpose : initializes *instance and corresponding 5380 chip,
1012 * with flags OR'd into the initial flags value.
1014 * Inputs : instance - instantiation of the 5380 driver.
1016 * Notes : I assume that the host, hostno, and id bits have been
1017 * set correctly. I don't care about the irq and other fields.
1021 static void __init NCR5380_init(struct Scsi_Host *instance, int flags)
1023 NCR5380_local_declare();
1024 int i, pass;
1025 unsigned long timeout;
1026 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
1027 instance->hostdata;
1030 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
1031 * the base address.
1034 #ifdef NCR53C400
1035 if (flags & FLAG_NCR53C400)
1036 instance->NCR5380_instance_name += NCR53C400_address_adjust;
1037 #endif
1039 NCR5380_setup(instance);
1041 NCR5380_all_init();
1043 hostdata->aborted = 0;
1044 hostdata->id_mask = 1 << instance->this_id;
1045 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
1046 if (i > hostdata->id_mask)
1047 hostdata->id_higher_mask |= i;
1048 for (i = 0; i < 8; ++i)
1049 hostdata->busy[i] = 0;
1050 #ifdef REAL_DMA
1051 hostdata->dmalen = 0;
1052 #endif
1053 hostdata->targets_present = 0;
1054 hostdata->connected = NULL;
1055 hostdata->issue_queue = NULL;
1056 hostdata->disconnected_queue = NULL;
1057 #ifdef NCR5380_STATS
1058 for (i = 0; i < 8; ++i) {
1059 hostdata->time_read[i] = 0;
1060 hostdata->time_write[i] = 0;
1061 hostdata->bytes_read[i] = 0;
1062 hostdata->bytes_write[i] = 0;
1064 hostdata->timebase = 0;
1065 hostdata->pendingw = 0;
1066 hostdata->pendingr = 0;
1067 #endif
1069 /* The CHECK code seems to break the 53C400. Will check it later maybe */
1070 if (flags & FLAG_NCR53C400)
1071 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
1072 else
1073 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
1075 if (!the_template) {
1076 the_template = instance->hostt;
1077 first_instance = instance;
1079 #ifdef USLEEP
1080 hostdata->time_expires = 0;
1081 hostdata->next_timer = NULL;
1082 #endif
1084 #ifndef AUTOSENSE
1085 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
1087 printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
1088 " without AUTOSENSE option, contingent allegiance conditions may\n"
1089 " be incorrectly cleared.\n", instance->host_no);
1090 #endif /* def AUTOSENSE */
1092 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1093 NCR5380_write(MODE_REG, MR_BASE);
1094 NCR5380_write(TARGET_COMMAND_REG, 0);
1095 NCR5380_write(SELECT_ENABLE_REG, 0);
1097 #ifdef NCR53C400
1098 if (hostdata->flags & FLAG_NCR53C400) {
1099 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
1101 #endif
1104 * Detect and correct bus wedge problems.
1106 * If the system crashed, it may have crashed in a state
1107 * where a SCSI command was still executing, and the
1108 * SCSI bus is not in a BUS FREE STATE.
1110 * If this is the case, we'll try to abort the currently
1111 * established nexus which we know nothing about, and that
1112 * failing, do a hard reset of the SCSI bus
1115 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) &&
1116 pass <= 6; ++pass) {
1117 switch (pass) {
1118 case 1:
1119 case 3:
1120 case 5:
1121 printk("scsi%d: SCSI bus busy, waiting up to five seconds\n",
1122 instance->host_no);
1123 timeout = jiffies + 5 * HZ;
1124 while (time_before(jiffies,timeout) && (NCR5380_read(STATUS_REG) & SR_BSY));
1125 break;
1126 case 2:
1127 printk("scsi%d: bus busy, attempting abort\n",
1128 instance->host_no);
1129 do_abort(instance);
1130 break;
1131 case 4:
1132 printk("scsi%d: bus busy, attempting reset\n",
1133 instance->host_no);
1134 do_reset(instance);
1135 break;
1136 case 6:
1137 printk("scsi%d: bus locked solid or invalid override\n",
1138 instance->host_no);
1144 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
1145 * void (*done)(Scsi_Cmnd *))
1147 * Purpose : enqueues a SCSI command
1149 * Inputs : cmd - SCSI command, done - function called on completion, with
1150 * a pointer to the command descriptor.
1152 * Returns : 0
1154 * Side effects :
1155 * cmd is added to the per instance issue_queue, with minor
1156 * twiddling done to the host specific fields of cmd. If the
1157 * main coroutine is not running, it is restarted.
1161 /* Only make static if a wrapper function is used */
1162 #ifndef NCR5380_queue_command
1163 static
1164 #endif
1165 int NCR5380_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *)) {
1166 struct Scsi_Host *instance = cmd->host;
1167 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
1168 instance->hostdata;
1169 Scsi_Cmnd *tmp;
1171 #if (NDEBUG & NDEBUG_NO_WRITE)
1172 switch (cmd->cmnd[0]) {
1173 case WRITE_6:
1174 case WRITE_10:
1175 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
1176 instance->host_no);
1177 cmd->result = (DID_ERROR << 16);
1178 done(cmd);
1179 return 0;
1181 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1183 #ifdef NCR5380_STATS
1184 #if 0
1185 if (!hostdata->connected && !hostdata->issue_queue &&
1186 !hostdata->disconnected_queue) {
1187 hostdata->timebase = jiffies;
1189 #endif
1190 #ifdef NCR5380_STAT_LIMIT
1191 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1192 #endif
1193 switch (cmd->cmnd[0]) {
1194 case WRITE:
1195 case WRITE_6:
1196 case WRITE_10:
1197 hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
1198 hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
1199 hostdata->pendingw++;
1200 break;
1201 case READ:
1202 case READ_6:
1203 case READ_10:
1204 hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
1205 hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
1206 hostdata->pendingr++;
1207 break;
1209 #endif
1212 * We use the host_scribble field as a pointer to the next command
1213 * in a queue
1216 cmd->host_scribble = NULL;
1217 cmd->scsi_done = done;
1219 cmd->result = 0;
1223 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1224 * commands are added to the head of the queue since any command will
1225 * clear the contingent allegiance condition that exists and the
1226 * sense data is only guaranteed to be valid while the condition exists.
1229 cli();
1230 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1231 LIST(cmd, hostdata->issue_queue);
1232 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1233 hostdata->issue_queue = cmd;
1234 } else {
1235 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
1236 tmp = (Scsi_Cmnd *) tmp->host_scribble);
1237 LIST(cmd, tmp);
1238 tmp->host_scribble = (unsigned char *) cmd;
1240 #if (NDEBUG & NDEBUG_QUEUES)
1241 printk("scsi%d : command added to %s of queue\n", instance->host_no,
1242 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1243 #endif
1245 /* Run the coroutine if it isn't already running. */
1246 run_main();
1247 return 0;
1251 * Function : NCR5380_main (void)
1253 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1254 * be done on the NCR5380 host adapters in a system. Both
1255 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1256 * in case it is not running.
1258 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1259 * reenable them. This prevents reentrancy and kernel stack overflow.
1262 static void NCR5380_main(void) {
1263 Scsi_Cmnd *tmp, *prev;
1264 struct Scsi_Host *instance;
1265 struct NCR5380_hostdata *hostdata;
1266 int done;
1267 unsigned long flags;
1270 * We run (with interrupts disabled) until we're sure that none of
1271 * the host adapters have anything that can be done, at which point
1272 * we set main_running to 0 and exit.
1274 * Interrupts are enabled before doing various other internal
1275 * instructions, after we've decided that we need to run through
1276 * the loop again.
1278 * this should prevent any race conditions.
1281 spin_unlock_irq(&io_request_lock);
1283 save_flags(flags);
1285 do {
1286 cli(); /* Freeze request queues */
1287 done = 1;
1288 for (instance = first_instance; instance &&
1289 instance->hostt == the_template; instance = instance->next) {
1290 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1291 cli();
1292 #ifdef USLEEP
1293 if (!hostdata->connected && !hostdata->selecting) {
1294 #else
1295 if (!hostdata->connected) {
1296 #endif
1297 #if (NDEBUG & NDEBUG_MAIN)
1298 printk("scsi%d : not connected\n", instance->host_no);
1299 #endif
1301 * Search through the issue_queue for a command destined
1302 * for a target that's not busy.
1304 #if (NDEBUG & NDEBUG_LISTS)
1305 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp && (tmp != prev); prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble);
1306 /*printk("%p ", tmp); */
1307 if ((tmp == prev) && tmp)
1308 printk(" LOOP\n"); /* else printk("\n"); */
1309 #endif
1310 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1311 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
1312 tmp->host_scribble) {
1314 #if (NDEBUG & NDEBUG_LISTS)
1315 if (prev != tmp)
1316 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun);
1317 #endif
1318 /* When we find one, remove it from the issue queue. */
1319 if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
1320 if (prev) {
1321 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
1322 prev->host_scribble = tmp->host_scribble;
1323 } else {
1324 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
1325 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1327 tmp->host_scribble = NULL;
1329 /* reenable interrupts after finding one */
1330 restore_flags(flags);
1333 * Attempt to establish an I_T_L nexus here.
1334 * On success, instance->hostdata->connected is set.
1335 * On failure, we must add the command back to the
1336 * issue queue so we can keep trying.
1338 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1339 printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
1340 instance->host_no, tmp->target, tmp->lun);
1341 #endif
1344 * A successful selection is defined as one that
1345 * leaves us with the command connected and
1346 * in hostdata->connected, OR has terminated the
1347 * command.
1349 * With successful commands, we fall through
1350 * and see if we can do an information transfer,
1351 * with failures we will restart.
1353 #ifdef USLEEP
1354 hostdata->selecting = 0; /* RvC: have to preset this
1355 to indicate a new command is being performed */
1356 #endif
1358 if (!NCR5380_select(instance, tmp,
1360 * REQUEST SENSE commands are issued without tagged
1361 * queueing, even on SCSI-II devices because the
1362 * contingent allegiance condition exists for the
1363 * entire unit.
1365 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1366 TAG_NEXT)) {
1367 break;
1368 } else {
1369 cli();
1370 LIST(tmp, hostdata->issue_queue);
1371 tmp->host_scribble = (unsigned char *)
1372 hostdata->issue_queue;
1373 hostdata->issue_queue = tmp;
1374 done = 0;
1375 restore_flags(flags);
1376 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1377 printk("scsi%d : main(): select() failed, returned to issue_queue\n",
1378 instance->host_no);
1379 #endif
1381 } /* if target/lun is not busy */
1382 } /* for */
1383 } /* if (!hostdata->connected) */
1384 #ifdef USLEEP
1385 if (hostdata->selecting)
1387 tmp = (Scsi_Cmnd *)hostdata->selecting;
1388 if (!NCR5380_select(instance, tmp,
1389 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT))
1391 /* Ok ?? */
1393 else
1395 /* RvC: device failed, so we wait a long time
1396 this is needed for Mustek scanners, that
1397 do not respond to commands immediately
1398 after a scan */
1399 printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n",
1400 instance->host_no, tmp->target);
1401 cli();
1402 LIST(tmp, hostdata->issue_queue);
1403 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1404 hostdata->issue_queue = tmp;
1405 restore_flags(flags);
1407 hostdata->time_expires = jiffies + USLEEP_WAITLONG;
1408 NCR5380_set_timer (instance);
1410 } /* if hostdata->selecting */
1411 #endif
1412 if (hostdata->connected
1413 #ifdef REAL_DMA
1414 && !hostdata->dmalen
1415 #endif
1416 #ifdef USLEEP
1417 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1418 #endif
1420 restore_flags(flags);
1421 #if (NDEBUG & NDEBUG_MAIN)
1422 printk("scsi%d : main() : performing information transfer\n",
1423 instance->host_no);
1424 #endif
1425 NCR5380_information_transfer(instance);
1426 #if (NDEBUG & NDEBUG_MAIN)
1427 printk("scsi%d : main() : done set false\n", instance->host_no);
1428 #endif
1429 done = 0;
1430 } else
1431 break;
1432 } /* for instance */
1433 } while (!done);
1434 spin_lock_irq(&io_request_lock);
1435 /* cli();*/
1436 main_running = 0;
1439 #ifndef DONT_USE_INTR
1440 #include <linux/blk.h>
1441 #include <linux/spinlock.h>
1444 * Function : void NCR5380_intr (int irq)
1446 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1447 * from the disconnected queue, and restarting NCR5380_main()
1448 * as required.
1450 * Inputs : int irq, irq that caused this interrupt.
1454 static void NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs) {
1455 NCR5380_local_declare();
1456 struct Scsi_Host *instance;
1457 int done;
1458 unsigned char basr;
1459 unsigned long flags;
1461 save_flags(flags);
1462 cli();
1463 #if (NDEBUG & NDEBUG_INTR)
1464 printk("scsi : NCR5380 irq %d triggered\n", irq);
1465 #endif
1466 do {
1467 done = 1;
1468 for (instance = first_instance; instance && (instance->hostt ==
1469 the_template); instance = instance->next)
1470 if (instance->irq == irq) {
1472 /* Look for pending interrupts */
1473 NCR5380_setup(instance);
1474 basr = NCR5380_read(BUS_AND_STATUS_REG);
1475 /* XXX dispatch to appropriate routine if found and done=0 */
1476 if (basr & BASR_IRQ) {
1477 #if (NDEBUG & NDEBUG_INTR)
1478 NCR5380_print(instance);
1479 #endif
1480 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1481 (SR_SEL | SR_IO)) {
1482 done = 0;
1483 restore_flags(flags);
1484 #if (NDEBUG & NDEBUG_INTR)
1485 printk("scsi%d : SEL interrupt\n", instance->host_no);
1486 #endif
1487 NCR5380_reselect(instance);
1488 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1489 } else if (basr & BASR_PARITY_ERROR) {
1490 #if (NDEBUG & NDEBUG_INTR)
1491 printk("scsi%d : PARITY interrupt\n", instance->host_no);
1492 #endif
1493 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1494 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1495 #if (NDEBUG & NDEBUG_INTR)
1496 printk("scsi%d : RESET interrupt\n", instance->host_no);
1497 #endif
1498 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1499 } else {
1501 * XXX the rest of the interrupt conditions should *only* occur during a
1502 * DMA transfer, which I haven't gotten around to fixing yet.
1505 #if defined(REAL_DMA)
1507 * We should only get PHASE MISMATCH and EOP interrupts
1508 * if we have DMA enabled, so do a sanity check based on
1509 * the current setting of the MODE register.
1512 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr &
1513 BASR_END_DMA_TRANSFER) ||
1514 !(basr & BASR_PHASE_MATCH))) {
1515 int transfered;
1517 if (!hostdata->connected)
1518 panic("scsi%d : received end of DMA interrupt with no connected cmd\n",
1519 instance->hostno);
1521 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1522 hostdata->connected->SCp.this_residual -= transferred;
1523 hostdata->connected->SCp.ptr += transferred;
1524 hostdata->dmalen = 0;
1526 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1527 #if NCR_TIMEOUT
1529 unsigned long timeout = jiffies + NCR_TIMEOUT;
1531 spin_unlock_irq(&io_request_lock);
1532 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK
1533 && time_before(jiffies, timeout));
1534 spin_lock_irq(&io_request_lock);
1536 if (time_after_eq(jiffies, timeout) )
1537 printk("scsi%d: timeout at NCR5380.c:%d\n",
1538 host->host_no, __LINE__);
1540 #else /* NCR_TIMEOUT */
1541 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1542 #endif
1544 NCR5380_write(MODE_REG, MR_BASE);
1545 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1547 #else
1548 #if (NDEBUG & NDEBUG_INTR)
1549 printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1550 #endif
1551 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1552 #endif
1554 } /* if BASR_IRQ */
1555 if (!done)
1556 run_main();
1557 } /* if (instance->irq == irq) */
1558 } while (!done);
1562 static void do_NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs) {
1563 unsigned long flags;
1565 spin_lock_irqsave(&io_request_lock, flags);
1566 NCR5380_intr(irq, dev_id, regs);
1567 spin_unlock_irqrestore(&io_request_lock, flags);
1570 #endif
1572 #ifdef NCR5380_STATS
1573 static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd) {
1574 #ifdef NCR5380_STAT_LIMIT
1575 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1576 #endif
1577 switch (cmd->cmnd[0]) {
1578 case WRITE:
1579 case WRITE_6:
1580 case WRITE_10:
1581 hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1582 /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen; */
1583 hostdata->pendingw--;
1584 break;
1585 case READ:
1586 case READ_6:
1587 case READ_10:
1588 hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1589 /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen; */
1590 hostdata->pendingr--;
1591 break;
1594 #endif
1596 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1597 * int tag);
1599 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1600 * including ARBITRATION, SELECTION, and initial message out for
1601 * IDENTIFY and queue messages.
1603 * Inputs : instance - instantiation of the 5380 driver on which this
1604 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1605 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1606 * the command that is presently connected.
1608 * Returns : -1 if selection could not execute for some reason,
1609 * 0 if selection succeeded or failed because the target
1610 * did not respond.
1612 * Side effects :
1613 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1614 * with registers as they should have been on entry - ie
1615 * SELECT_ENABLE will be set appropriately, the NCR5380
1616 * will cease to drive any SCSI bus signals.
1618 * If successful : I_T_L or I_T_L_Q nexus will be established,
1619 * instance->connected will be set to cmd.
1620 * SELECT interrupt will be disabled.
1622 * If failed (no target) : cmd->scsi_done() will be called, and the
1623 * cmd->result host byte set to DID_BAD_TARGET.
1625 static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag) {
1626 NCR5380_local_declare();
1627 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1628 unsigned char tmp[3], phase;
1629 unsigned char *data;
1630 int len;
1631 unsigned long timeout;
1632 unsigned long flags;
1633 #ifdef USLEEP
1634 unsigned char value;
1635 #endif
1637 NCR5380_setup(instance);
1639 #ifdef USLEEP
1641 if (hostdata->selecting)
1643 goto part2; /* RvC: sorry prof. Dijkstra, but it keeps the
1644 rest of the code nearly the same */
1646 #endif
1648 hostdata->restart_select = 0;
1649 #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION)
1650 NCR5380_print(instance);
1651 printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1652 instance->this_id);
1653 #endif
1654 save_flags(flags);
1655 cli();
1658 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1659 * data bus during SELECTION.
1662 NCR5380_write(TARGET_COMMAND_REG, 0);
1666 * Start arbitration.
1669 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1670 NCR5380_write(MODE_REG, MR_ARBITRATE);
1672 restore_flags(flags);
1674 /* Wait for arbitration logic to complete */
1675 #if NCR_TIMEOUT
1677 unsigned long timeout = jiffies + 2 * NCR_TIMEOUT;
1679 spin_unlock_irq(&io_request_lock);
1681 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1682 && time_before(jiffies,timeout));
1684 spin_lock_irq(&io_request_lock);
1686 if (time_after_eq(jiffies,timeout)) {
1687 printk("scsi: arbitration timeout at %d\n", __LINE__);
1688 NCR5380_write(MODE_REG, MR_BASE);
1689 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1690 return -1;
1693 #else /* NCR_TIMEOUT */
1694 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1695 #endif
1697 #if (NDEBUG & NDEBUG_ARBITRATION)
1698 printk("scsi%d : arbitration complete\n", instance->host_no);
1699 /* Avoid GCC 2.4.5 asm needs to many reloads error */
1700 __asm__("nop");
1701 #endif
1704 * The arbitration delay is 2.2us, but this is a minimum and there is
1705 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1706 * the integral nature of udelay().
1710 udelay(3);
1712 /* Check for lost arbitration */
1713 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1714 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1715 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1716 NCR5380_write(MODE_REG, MR_BASE);
1717 #if (NDEBUG & NDEBUG_ARBITRATION)
1718 printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n",
1719 instance->host_no);
1720 #endif
1721 return -1;
1723 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1725 if (!(hostdata->flags & FLAG_DTC3181E) &&
1726 /* RvC: DTC3181E has some trouble with this
1727 * so we simply removed it. Seems to work with
1728 * only Mustek scanner attached
1730 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST))
1732 NCR5380_write(MODE_REG, MR_BASE);
1733 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1734 #if (NDEBUG & NDEBUG_ARBITRATION)
1735 printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n",
1736 instance->host_no);
1737 #endif
1738 return -1;
1741 * Again, bus clear + bus settle time is 1.2us, however, this is
1742 * a minimum so we'll udelay ceil(1.2)
1745 udelay(2);
1747 #if (NDEBUG & NDEBUG_ARBITRATION)
1748 printk("scsi%d : won arbitration\n", instance->host_no);
1749 #endif
1753 * Now that we have won arbitration, start Selection process, asserting
1754 * the host and target ID's on the SCSI bus.
1757 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1760 * Raise ATN while SEL is true before BSY goes false from arbitration,
1761 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1762 * phase immediately after selection.
1765 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1766 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1767 NCR5380_write(MODE_REG, MR_BASE);
1770 * Reselect interrupts must be turned off prior to the dropping of BSY,
1771 * otherwise we will trigger an interrupt.
1773 NCR5380_write(SELECT_ENABLE_REG, 0);
1776 * The initiator shall then wait at least two deskew delays and release
1777 * the BSY signal.
1779 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1781 /* Reset BSY */
1782 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1783 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1786 * Something weird happens when we cease to drive BSY - looks
1787 * like the board/chip is letting us do another read before the
1788 * appropriate propagation delay has expired, and we're confusing
1789 * a BSY signal from ourselves as the target's response to SELECTION.
1791 * A small delay (the 'C++' frontend breaks the pipeline with an
1792 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1793 * tighter 'C' code breaks and requires this) solves the problem -
1794 * the 1 us delay is arbitrary, and only used because this delay will
1795 * be the same on other platforms and since it works here, it should
1796 * work there.
1798 * wingel suggests that this could be due to failing to wait
1799 * one deskew delay.
1802 udelay(1);
1804 #if (NDEBUG & NDEBUG_SELECTION)
1805 printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1806 #endif
1809 * The SCSI specification calls for a 250 ms timeout for the actual
1810 * selection.
1813 timeout = jiffies + (250 * HZ / 1000);
1816 * XXX very interesting - we're seeing a bounce where the BSY we
1817 * asserted is being reflected / still asserted (propagation delay?)
1818 * and it's detecting as true. Sigh.
1821 #ifdef USLEEP
1822 hostdata->select_time = 0; /* we count the clock ticks at which we polled */
1823 hostdata->selecting = cmd;
1825 part2:
1826 /* RvC: here we enter after a sleeping period, or immediately after
1827 execution of part 1
1828 we poll only once ech clock tick */
1829 value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
1831 if (!value && (hostdata->select_time < 25))
1833 /* RvC: we still must wait for a device response */
1834 hostdata->select_time++; /* after 25 ticks the device has failed */
1835 hostdata->time_expires = jiffies + 1;
1836 NCR5380_set_timer(instance);
1837 return 0; /* RvC: we return here with hostdata->selecting set,
1838 to go to sleep */
1841 hostdata->selecting = 0; /* clear this pointer, because we passed the
1842 waiting period */
1843 #else
1844 spin_unlock_irq(&io_request_lock);
1845 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) &
1846 (SR_BSY | SR_IO)));
1847 spin_lock_irq(&io_request_lock);
1848 #endif
1849 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1850 (SR_SEL | SR_IO)) {
1851 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1852 NCR5380_reselect(instance);
1853 printk("scsi%d : reselection after won arbitration?\n",
1854 instance->host_no);
1855 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1856 return -1;
1859 * No less than two deskew delays after the initiator detects the
1860 * BSY signal is true, it shall release the SEL signal and may
1861 * change the DATA BUS. -wingel
1864 udelay(1);
1866 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1868 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1869 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1870 if (hostdata->targets_present & (1 << cmd->target)) {
1871 printk("scsi%d : weirdness\n", instance->host_no);
1872 if (hostdata->restart_select)
1873 printk("\trestart select\n");
1874 #if (NDEBUG & NDEBUG_SELECTION)
1875 NCR5380_print(instance);
1876 #endif
1877 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1878 return -1;
1880 cmd->result = DID_BAD_TARGET << 16;
1881 #ifdef NCR5380_STATS
1882 collect_stats(hostdata, cmd);
1883 #endif
1884 cmd->scsi_done(cmd);
1885 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1886 #if (NDEBUG & NDEBUG_SELECTION)
1887 printk("scsi%d : target did not respond within 250ms\n",
1888 instance->host_no);
1889 #endif
1890 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1891 return 0;
1893 hostdata->targets_present |= (1 << cmd->target);
1896 * Since we followed the SCSI spec, and raised ATN while SEL
1897 * was true but before BSY was false during selection, the information
1898 * transfer phase should be a MESSAGE OUT phase so that we can send the
1899 * IDENTIFY message.
1901 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1902 * message (2 bytes) with a tag ID that we increment with every command
1903 * until it wraps back to 0.
1905 * XXX - it turns out that there are some broken SCSI-II devices,
1906 * which claim to support tagged queuing but fail when more than
1907 * some number of commands are issued at once.
1910 /* Wait for start of REQ/ACK handshake */
1911 #ifdef NCR_TIMEOUT
1913 unsigned long timeout = jiffies + NCR_TIMEOUT;
1915 spin_unlock_irq(&io_request_lock);
1916 while (!(NCR5380_read(STATUS_REG) & SR_REQ) && time_before(jiffies, timeout));
1917 spin_lock_irq(&io_request_lock);
1919 if (time_after_eq(jiffies, timeout)) {
1920 printk("scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1921 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1922 return -1;
1925 #else /* NCR_TIMEOUT */
1926 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1927 #endif /* def NCR_TIMEOUT */
1929 #if (NDEBUG & NDEBUG_SELECTION)
1930 printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1931 instance->host_no, cmd->target);
1932 #endif
1933 tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1934 #ifdef SCSI2
1935 if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
1936 tmp[1] = SIMPLE_QUEUE_TAG;
1937 if (tag == TAG_NEXT) {
1938 /* 0 is TAG_NONE, used to imply no tag for this command */
1939 if (cmd->device->current_tag == 0)
1940 cmd->device->current_tag = 1;
1942 cmd->tag = cmd->device->current_tag;
1943 cmd->device->current_tag++;
1944 } else
1945 cmd->tag = (unsigned char) tag;
1947 tmp[2] = cmd->tag;
1948 hostdata->last_message = SIMPLE_QUEUE_TAG;
1949 len = 3;
1950 } else
1951 #endif /* def SCSI2 */
1953 len = 1;
1954 cmd->tag = 0;
1957 /* Send message(s) */
1958 data = tmp;
1959 phase = PHASE_MSGOUT;
1960 NCR5380_transfer_pio(instance, &phase, &len, &data);
1961 #if (NDEBUG & NDEBUG_SELECTION)
1962 printk("scsi%d : nexus established.\n", instance->host_no);
1963 #endif
1964 /* XXX need to handle errors here */
1965 hostdata->connected = cmd;
1966 #ifdef SCSI2
1967 if (!cmd->device->tagged_queue)
1968 #endif
1969 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1971 initialize_SCp(cmd);
1974 return 0;
1978 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1979 * unsigned char *phase, int *count, unsigned char **data)
1981 * Purpose : transfers data in given phase using polled I/O
1983 * Inputs : instance - instance of driver, *phase - pointer to
1984 * what phase is expected, *count - pointer to number of
1985 * bytes to transfer, **data - pointer to data pointer.
1987 * Returns : -1 when different phase is entered without transferring
1988 * maximum number of bytes, 0 if all bytes or transfered or exit
1989 * is in same phase.
1991 * Also, *phase, *count, *data are modified in place.
1993 * XXX Note : handling for bus free may be useful.
1997 * Note : this code is not as quick as it could be, however it
1998 * IS 100% reliable, and for the actual data transfer where speed
1999 * counts, we will always do a pseudo DMA or DMA transfer.
2002 static int NCR5380_transfer_pio(struct Scsi_Host *instance,
2003 unsigned char *phase, int *count, unsigned char **data) {
2004 NCR5380_local_declare();
2005 register unsigned char p = *phase, tmp;
2006 register int c = *count;
2007 register unsigned char *d = *data;
2008 #ifdef USLEEP
2010 * RvC: some administrative data to process polling time
2012 int break_allowed = 0;
2013 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2014 #endif
2015 NCR5380_setup(instance);
2017 #if (NDEBUG & NDEBUG_PIO)
2018 if (!(p & SR_IO))
2019 printk("scsi%d : pio write %d bytes\n", instance->host_no, c);
2020 else
2021 printk("scsi%d : pio read %d bytes\n", instance->host_no, c);
2022 #endif
2025 * The NCR5380 chip will only drive the SCSI bus when the
2026 * phase specified in the appropriate bits of the TARGET COMMAND
2027 * REGISTER match the STATUS REGISTER
2030 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
2032 #ifdef USLEEP
2033 /* RvC: don't know if this is necessary, but other SCSI I/O is short
2034 * so breaks are not necessary there
2036 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT))
2038 break_allowed = 1;
2040 #endif
2043 do {
2045 * Wait for assertion of REQ, after which the phase bits will be
2046 * valid
2049 #ifdef USLEEP
2050 /* RvC: we simply poll once, after that we stop temporarily
2051 * and let the device buffer fill up
2052 * if breaking is not allowed, we keep polling as long as needed
2055 while ( !((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) &&
2056 !break_allowed );
2057 if (!(tmp & SR_REQ))
2059 /* timeout condition */
2060 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2061 NCR5380_set_timer (instance);
2062 break;
2064 #else
2065 while ( !((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) );
2066 #endif
2068 #if (NDEBUG & NDEBUG_HANDSHAKE)
2069 printk("scsi%d : REQ detected\n", instance->host_no);
2070 #endif
2072 /* Check for phase mismatch */
2073 if ((tmp & PHASE_MASK) != p) {
2074 #if (NDEBUG & NDEBUG_PIO)
2075 printk("scsi%d : phase mismatch\n", instance->host_no);
2076 NCR5380_print_phase(instance);
2077 #endif
2078 break;
2080 /* Do actual transfer from SCSI bus to / from memory */ if (!(p & SR_IO))
2081 NCR5380_write(OUTPUT_DATA_REG, *d);
2082 else
2083 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
2085 ++d;
2088 * The SCSI standard suggests that in MSGOUT phase, the initiator
2089 * should drop ATN on the last byte of the message phase
2090 * after REQ has been asserted for the handshake but before
2091 * the initiator raises ACK.
2094 if (!(p & SR_IO)) {
2095 if (!((p & SR_MSG) && c > 1)) {
2096 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2097 ICR_ASSERT_DATA);
2098 #if (NDEBUG & NDEBUG_PIO)
2099 NCR5380_print(instance);
2100 #endif
2101 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2102 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
2103 } else {
2104 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2105 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
2106 #if (NDEBUG & NDEBUG_PIO)
2107 NCR5380_print(instance);
2108 #endif
2109 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2110 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
2112 } else {
2113 #if (NDEBUG & NDEBUG_PIO)
2114 NCR5380_print(instance);
2115 #endif
2116 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2119 while (NCR5380_read(STATUS_REG) & SR_REQ);
2121 #if (NDEBUG & NDEBUG_HANDSHAKE)
2122 printk("scsi%d : req false, handshake complete\n", instance->host_no);
2123 #endif
2126 * We have several special cases to consider during REQ/ACK handshaking :
2127 * 1. We were in MSGOUT phase, and we are on the last byte of the
2128 * message. ATN must be dropped as ACK is dropped.
2130 * 2. We are in a MSGIN phase, and we are on the last byte of the
2131 * message. We must exit with ACK asserted, so that the calling
2132 * code may raise ATN before dropping ACK to reject the message.
2134 * 3. ACK and ATN are clear and the target may proceed as normal.
2136 if (!(p == PHASE_MSGIN && c == 1)) {
2137 if (p == PHASE_MSGOUT && c > 1)
2138 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2139 else
2140 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2142 } while (--c);
2144 #if (NDEBUG & NDEBUG_PIO)
2145 printk("scsi%d : residual %d\n", instance->host_no, c);
2146 #endif
2148 *count = c;
2149 *data = d;
2150 tmp = NCR5380_read(STATUS_REG);
2151 if (tmp & SR_REQ)
2152 *phase = tmp & PHASE_MASK;
2153 else
2154 *phase = PHASE_UNKNOWN;
2156 if (!c || (*phase == p))
2157 return 0;
2158 else
2159 return -1;
2162 static void do_reset(struct Scsi_Host *host) {
2163 unsigned long flags;
2164 NCR5380_local_declare();
2165 NCR5380_setup(host);
2167 save_flags(flags);
2168 cli();
2169 NCR5380_write(TARGET_COMMAND_REG,
2170 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
2171 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2172 udelay(25);
2173 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2174 restore_flags(flags);
2175 } /*
2177 * Function : do_abort (Scsi_Host *host)
2179 * Purpose : abort the currently established nexus. Should only be
2180 * called from a routine which can drop into a
2182 * Returns : 0 on success, -1 on failure.
2183 */ static int do_abort(struct Scsi_Host *host) {
2184 NCR5380_local_declare();
2185 unsigned char tmp, *msgptr, phase;
2186 int len;
2187 NCR5380_setup(host);
2190 /* Request message out phase */
2191 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2194 * Wait for the target to indicate a valid phase by asserting
2195 * REQ. Once this happens, we'll have either a MSGOUT phase
2196 * and can immediately send the ABORT message, or we'll have some
2197 * other phase and will have to source/sink data.
2199 * We really don't care what value was on the bus or what value
2200 * the target sees, so we just handshake.
2203 while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
2205 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2207 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
2208 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2209 ICR_ASSERT_ACK);
2210 while (NCR5380_read(STATUS_REG) & SR_REQ);
2211 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2213 tmp = ABORT;
2214 msgptr = &tmp;
2215 len = 1;
2216 phase = PHASE_MSGOUT;
2217 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
2220 * If we got here, and the command completed successfully,
2221 * we're about to go into bus free state.
2224 return len ? -1 : 0;
2227 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
2229 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
2230 * unsigned char *phase, int *count, unsigned char **data)
2232 * Purpose : transfers data in given phase using either real
2233 * or pseudo DMA.
2235 * Inputs : instance - instance of driver, *phase - pointer to
2236 * what phase is expected, *count - pointer to number of
2237 * bytes to transfer, **data - pointer to data pointer.
2239 * Returns : -1 when different phase is entered without transferring
2240 * maximum number of bytes, 0 if all bytes or transfered or exit
2241 * is in same phase.
2243 * Also, *phase, *count, *data are modified in place.
2248 static int NCR5380_transfer_dma(struct Scsi_Host *instance,
2249 unsigned char *phase, int *count, unsigned char **data) {
2250 NCR5380_local_declare();
2251 register int c = *count;
2252 register unsigned char p = *phase;
2253 register unsigned char *d = *data;
2254 unsigned char tmp;
2255 unsigned long flags;
2256 int foo;
2257 #if defined(REAL_DMA_POLL)
2258 int cnt, toPIO;
2259 unsigned char saved_data = 0, overrun = 0, residue;
2260 #endif
2262 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2263 instance->hostdata;
2265 NCR5380_setup(instance);
2267 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
2268 *phase = tmp;
2269 return -1;
2271 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
2272 #ifdef READ_OVERRUNS if (p & SR_IO) { c -= 2;
2274 #endif
2275 #if (NDEBUG & NDEBUG_DMA)
2276 printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
2277 instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
2278 "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
2279 #endif
2280 hostdata->dma_len = (p & SR_IO) ?
2281 NCR5380_dma_read_setup(instance, d, c) :
2282 NCR5380_dma_write_setup(instance, d, c);
2283 #endif
2285 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
2287 #ifdef REAL_DMA
2288 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
2289 #elif defined(REAL_DMA_POLL)
2290 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2291 #else
2293 * Note : on my sample board, watch-dog timeouts occurred when interrupts
2294 * were not disabled for the duration of a single DMA transfer, from
2295 * before the setting of DMA mode to after transfer of the last byte.
2298 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2299 save_flags(flags);
2300 cli();
2301 #endif
2302 /* KLL May need eop and parity in 53c400 */
2303 if (hostdata->flags & FLAG_NCR53C400)
2304 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK
2305 | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE
2306 | MR_MONITOR_BSY);
2307 else
2308 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2309 #endif /* def REAL_DMA */
2311 #if (NDEBUG & NDEBUG_DMA) & 0
2312 printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
2313 #endif
2316 * FOO stuff. For some UNAPPARENT reason, I'm getting
2317 * watchdog timers fired on bootup for NO APPARENT REASON, meaning it's
2318 * probably a timing problem.
2320 * Since this is the only place I have back-to-back writes, perhaps this
2321 * is the problem?
2324 if (p & SR_IO)
2326 #ifndef FOO
2327 udelay(1);
2328 #endif
2329 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2330 } else {
2331 #ifndef FOO
2332 udelay(1);
2333 #endif
2334 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2335 #ifndef FOO
2336 udelay(1);
2337 #endif
2338 NCR5380_write(START_DMA_SEND_REG, 0);
2339 #ifndef FOO
2340 udelay(1);
2341 #endif
2344 #if defined(REAL_DMA_POLL)
2345 do {
2346 tmp = NCR5380_read(BUS_AND_STATUS_REG);
2347 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR |
2349 BASR_END_DMA_TRANSFER)));
2352 At this point, either we've completed DMA, or we have a phase mismatch,
2353 or we've unexpectedly lost BUSY (which is a real error).
2355 For write DMAs, we want to wait until the last byte has been
2356 transferred out over the bus before we turn off DMA mode. Alas, there
2357 seems to be no terribly good way of doing this on a 5380 under all
2358 conditions. For non-scatter-gather operations, we can wait until REQ
2359 and ACK both go false, or until a phase mismatch occurs. Gather-writes
2360 are nastier, since the device will be expecting more data than we
2361 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
2362 could test LAST BIT SENT to assure transfer (I imagine this is precisely
2363 why this signal was added to the newer chips) but on the older 538[01]
2364 this signal does not exist. The workaround for this lack is a watchdog;
2365 we bail out of the wait-loop after a modest amount of wait-time if
2366 the usual exit conditions are not met. Not a terribly clean or
2367 correct solution :-%
2369 Reads are equally tricky due to a nasty characteristic of the NCR5380.
2370 If the chip is in DMA mode for an READ, it will respond to a target's
2371 REQ by latching the SCSI data into the INPUT DATA register and asserting
2372 ACK, even if it has _already_ been notified by the DMA controller that
2373 the current DMA transfer has completed! If the NCR5380 is then taken
2374 out of DMA mode, this already-acknowledged byte is lost.
2376 This is not a problem for "one DMA transfer per command" reads, because
2377 the situation will never arise... either all of the data is DMA'ed
2378 properly, or the target switches to MESSAGE IN phase to signal a
2379 disconnection (either operation bringing the DMA to a clean halt).
2380 However, in order to handle scatter-reads, we must work around the
2381 problem. The chosen fix is to DMA N-2 bytes, then check for the
2382 condition before taking the NCR5380 out of DMA mode. One or two extra
2383 bytes are transferred via PIO as necessary to fill out the original
2384 request.
2387 if (p & SR_IO) {
2388 #ifdef READ_OVERRUNS
2389 udelay(10);
2390 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
2391 (BASR_PHASE_MATCH | BASR_ACK))) {
2392 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
2393 overrun = 1;
2395 #endif
2396 } else {
2397 int limit = 100;
2398 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
2399 (NCR5380_read(STATUS_REG) & SR_REQ)) {
2400 if (!(tmp & BASR_PHASE_MATCH))
2401 break;
2402 if (--limit < 0)
2403 break;
2408 #if (NDEBUG & NDEBUG_DMA)
2409 printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
2410 instance->host_no, tmp, NCR5380_read(STATUS_REG));
2411 #endif
2413 NCR5380_write(MODE_REG, MR_BASE);
2414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2416 residue = NCR5380_dma_residual(instance);
2417 c -= residue;
2418 *count -= c;
2419 *data += c;
2420 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2422 #ifdef READ_OVERRUNS
2423 if (*phase == p && (p & SR_IO) && residue == 0)
2425 if (overrun) {
2426 #if (NDEBUG & NDEBUG_DMA)
2427 printk("Got an input overrun, using saved byte\n");
2428 #endif
2429 **data = saved_data;
2430 *data += 1;
2431 *count -= 1;
2432 cnt = toPIO = 1;
2433 } else {
2434 printk("No overrun??\n");
2435 cnt = toPIO = 2;
2437 #if (NDEBUG & NDEBUG_DMA)
2438 printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
2439 #endif
2440 NCR5380_transfer_pio(instance, phase, &cnt, data);
2441 *count -= toPIO - cnt;
2443 #endif
2445 #if (NDEBUG & NDEBUG_DMA)
2446 printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
2447 *data, *count, *(*data + *count - 1), *(*data + *count));
2448 #endif
2449 return 0;
2451 #elif defined(REAL_DMA)
2452 return 0;
2453 #else /* defined(REAL_DMA_POLL) */
2454 if (p & SR_IO) {
2455 #ifdef DMA_WORKS_RIGHT
2456 foo = NCR5380_pread(instance, d, c);
2457 #else
2458 int diff = 1;
2459 if (hostdata->flags & FLAG_NCR53C400) {
2460 diff = 0;
2462 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2464 * We can't disable DMA mode after successfully transferring
2465 * what we plan to be the last byte, since that would open up
2466 * a race condition where if the target asserted REQ before
2467 * we got the DMA mode reset, the NCR5380 would have latched
2468 * an additional byte into the INPUT DATA register and we'd
2469 * have dropped it.
2471 * The workaround was to transfer one fewer bytes than we
2472 * intended to with the pseudo-DMA read function, wait for
2473 * the chip to latch the last byte, read it, and then disable
2474 * pseudo-DMA mode.
2476 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2477 * REQ is deasserted when ACK is asserted, and not reasserted
2478 * until ACK goes false. Since the NCR5380 won't lower ACK
2479 * until DACK is asserted, which won't happen unless we twiddle
2480 * the DMA port or we take the NCR5380 out of DMA mode, we
2481 * can guarantee that we won't handshake another extra
2482 * byte.
2485 if (!(hostdata->flags & FLAG_NCR53C400)) {
2486 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2487 /* Wait for clean handshake */
2488 while (NCR5380_read(STATUS_REG) & SR_REQ);
2489 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2492 #endif
2493 } else {
2494 #ifdef DMA_WORKS_RIGHT
2495 foo = NCR5380_pwrite(instance, d, c);
2496 #else
2497 int timeout;
2498 #if (NDEBUG & NDEBUG_C400_PWRITE)
2499 printk("About to pwrite %d bytes\n", c);
2500 #endif
2501 if (!(foo = NCR5380_pwrite(instance, d, c))) {
2503 * Wait for the last byte to be sent. If REQ is being asserted for
2504 * the byte we're interested, we'll ACK it and it will go false.
2506 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2507 timeout = 20000;
2508 #if 1
2509 #if 1
2510 while (!(NCR5380_read(BUS_AND_STATUS_REG) &
2511 BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
2512 BASR_PHASE_MATCH));
2513 #else
2514 if (NCR5380_read(STATUS_REG) & SR_REQ) {
2515 for (; timeout &&
2516 !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2517 --timeout);
2518 for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
2519 --timeout);
2521 #endif
2524 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2525 if (!timeout)
2526 printk("scsi%d : timed out on last byte\n",
2527 instance->host_no);
2528 #endif
2531 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2532 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2533 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2534 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2535 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2536 printk("scsi%d : last bit sent works\n",
2537 instance->host_no);
2538 #endif
2541 } else {
2542 #if (NDEBUG & NDEBUG_C400_PWRITE)
2543 printk("Waiting for LASTBYTE\n");
2544 #endif
2545 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2546 #if (NDEBUG & NDEBUG_C400_PWRITE)
2547 printk("Got LASTBYTE\n");
2548 #endif
2550 #else
2551 udelay(5);
2552 #endif
2554 #endif
2556 NCR5380_write(MODE_REG, MR_BASE);
2557 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2559 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2560 #if (NDEBUG & NDEBUG_C400_PWRITE)
2561 printk("53C400w: Checking for IRQ\n");
2562 #endif
2563 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2564 #if (NDEBUG & NDEBUG_C400_PWRITE)
2565 printk("53C400w: got it, reading reset interrupt reg\n");
2566 #endif
2567 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2568 } else {
2569 printk("53C400w: IRQ NOT THERE!\n");
2572 *data = d + c;
2573 *count = 0;
2574 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2575 #if 0
2576 NCR5380_print_phase(instance);
2577 #endif
2578 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2579 restore_flags(flags);
2580 #endif /* defined(REAL_DMA_POLL) */
2581 return foo;
2582 #endif /* def REAL_DMA */
2584 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2587 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2589 * Purpose : run through the various SCSI phases and do as the target
2590 * directs us to. Operates on the currently connected command,
2591 * instance->connected.
2593 * Inputs : instance, instance for which we are doing commands
2595 * Side effects : SCSI things happen, the disconnected queue will be
2596 * modified if a command disconnects, *instance->connected will
2597 * change.
2599 * XXX Note : we need to watch for bus free or a reset condition here
2600 * to recover from an unexpected bus free condition.
2603 static void NCR5380_information_transfer(struct Scsi_Host *instance) {
2604 NCR5380_local_declare();
2605 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2606 instance->hostdata;
2607 unsigned char msgout = NOP;
2608 int sink = 0;
2609 int len;
2610 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2611 int transfersize;
2612 #endif
2613 unsigned char *data;
2614 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
2615 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2616 #ifdef USLEEP
2617 /* RvC: we need to set the end of the polling time */
2618 unsigned long poll_time = jiffies + USLEEP_POLL;
2619 #endif
2621 NCR5380_setup(instance);
2623 while (1) {
2624 tmp = NCR5380_read(STATUS_REG);
2625 /* We only have a valid SCSI phase when REQ is asserted */
2626 if (tmp & SR_REQ) {
2627 phase = (tmp & PHASE_MASK);
2628 if (phase != old_phase) {
2629 old_phase = phase;
2630 #if (NDEBUG & NDEBUG_INFORMATION)
2631 NCR5380_print_phase(instance);
2632 #endif
2634 if (sink && (phase != PHASE_MSGOUT)) {
2635 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2637 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2638 ICR_ASSERT_ACK);
2639 while (NCR5380_read(STATUS_REG) & SR_REQ);
2640 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2641 ICR_ASSERT_ATN);
2642 sink = 0;
2643 continue;
2645 switch (phase) {
2646 case PHASE_DATAIN:
2647 case PHASE_DATAOUT:
2648 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2649 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
2650 instance->host_no);
2651 sink = 1;
2652 do_abort(instance);
2653 cmd->result = DID_ERROR << 16;
2654 cmd->done(cmd);
2655 return;
2656 #endif
2658 * If there is no room left in the current buffer in the
2659 * scatter-gather list, move onto the next one.
2662 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2663 ++cmd->SCp.buffer;
2664 --cmd->SCp.buffers_residual;
2665 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2666 cmd->SCp.ptr = cmd->SCp.buffer->address;
2667 #if (NDEBUG & NDEBUG_INFORMATION)
2668 printk("scsi%d : %d bytes and %d buffers left\n",
2669 instance->host_no, cmd->SCp.this_residual,
2670 cmd->SCp.buffers_residual);
2671 #endif
2674 * The preferred transfer method is going to be
2675 * PSEUDO-DMA for systems that are strictly PIO,
2676 * since we can let the hardware do the handshaking.
2678 * For this to work, we need to know the transfersize
2679 * ahead of time, since the pseudo-DMA code will sit
2680 * in an unconditional loop.
2683 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2684 /* KLL
2685 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2686 * driver then it will always be defined, so the
2687 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2688 * NCR5380 case. I think this is a fairly clean solution.
2689 * We supplement these 2 if's with the flag.
2691 #ifdef NCR5380_dma_xfer_len
2692 if (!cmd->device->borken &&
2693 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2694 (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2695 #else
2696 transfersize = cmd->transfersize;
2698 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2699 if (transfersize > 512)
2700 transfersize = 512;
2701 #endif /* LIMIT_TRANSFERSIZE */
2703 if (!cmd->device->borken && transfersize &&
2704 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2705 cmd->SCp.this_residual && !(cmd->SCp.this_residual %
2706 transfersize)) {
2707 /* Limit transfers to 32K, for xx400 & xx406
2708 * pseudoDMA that transfers in 128 bytes blocks. */
2709 if (transfersize > 32 * 1024)
2710 transfersize = 32 * 1024;
2711 #endif
2712 len = transfersize;
2713 if (NCR5380_transfer_dma(instance, &phase,
2714 &len, (unsigned char **) &cmd->SCp.ptr)) {
2716 * If the watchdog timer fires, all future accesses to this
2717 * device will use the polled-IO.
2719 printk("scsi%d : switching target %d lun %d to slow handshake\n",
2720 instance->host_no, cmd->target, cmd->lun);
2721 cmd->device->borken = 1;
2722 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2723 ICR_ASSERT_ATN);
2724 sink = 1;
2725 do_abort(instance);
2726 cmd->result = DID_ERROR << 16;
2727 cmd->done(cmd);
2728 /* XXX - need to source or sink data here, as appropriate */
2729 } else
2730 cmd->SCp.this_residual -= transfersize - len;
2731 } else
2732 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2733 NCR5380_transfer_pio(instance, &phase,
2734 (int *) &cmd->SCp.this_residual, (unsigned char **)
2735 &cmd->SCp.ptr);
2736 break;
2737 case PHASE_MSGIN:
2738 len = 1;
2739 data = &tmp;
2740 NCR5380_transfer_pio(instance, &phase, &len, &data);
2741 cmd->SCp.Message = tmp;
2743 switch (tmp) {
2745 * Linking lets us reduce the time required to get the
2746 * next command out to the device, hopefully this will
2747 * mean we don't waste another revolution due to the delays
2748 * required by ARBITRATION and another SELECTION.
2750 * In the current implementation proposal, low level drivers
2751 * merely have to start the next command, pointed to by
2752 * next_link, done() is called as with unlinked commands.
2754 #ifdef LINKED
2755 case LINKED_CMD_COMPLETE:
2756 case LINKED_FLG_CMD_COMPLETE:
2757 /* Accept message by clearing ACK */
2758 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2760 #if (NDEBUG & NDEBUG_LINKED)
2761 printk("scsi%d : target %d lun %d linked command complete.\n",
2762 instance->host_no, cmd->target, cmd->lun);
2763 #endif
2765 * Sanity check : A linked command should only terminate with
2766 * one of these messages if there are more linked commands
2767 * available.
2770 if (!cmd->next_link) {
2771 printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
2772 instance->host_no, cmd->target, cmd->lun);
2773 sink = 1;
2774 do_abort(instance);
2775 return;
2777 initialize_SCp(cmd->next_link);
2778 /* The next command is still part of this process */
2779 cmd->next_link->tag = cmd->tag;
2780 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2781 #if (NDEBUG & NDEBUG_LINKED)
2782 printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
2783 instance->host_no, cmd->target, cmd->lun);
2784 #endif
2785 #ifdef NCR5380_STATS
2786 collect_stats(hostdata, cmd);
2787 #endif
2788 cmd->scsi_done(cmd);
2789 cmd = hostdata->connected;
2790 break;
2791 #endif /* def LINKED */
2792 case ABORT:
2793 case COMMAND_COMPLETE:
2794 /* Accept message by clearing ACK */
2795 sink = 1;
2796 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2797 hostdata->connected = NULL;
2798 #if (NDEBUG & NDEBUG_QUEUES)
2799 printk("scsi%d : command for target %d, lun %d completed\n",
2800 instance->host_no, cmd->target, cmd->lun);
2801 #endif
2802 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2805 * I'm not sure what the correct thing to do here is :
2807 * If the command that just executed is NOT a request
2808 * sense, the obvious thing to do is to set the result
2809 * code to the values of the stored parameters.
2811 * If it was a REQUEST SENSE command, we need some way
2812 * to differentiate between the failure code of the original
2813 * and the failure code of the REQUEST sense - the obvious
2814 * case is success, where we fall through and leave the result
2815 * code unchanged.
2817 * The non-obvious place is where the REQUEST SENSE failed
2820 if (cmd->cmnd[0] != REQUEST_SENSE)
2821 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2822 else if (cmd->SCp.Status != GOOD)
2823 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2825 #ifdef AUTOSENSE
2826 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2827 (cmd->SCp.Status == CHECK_CONDITION)) {
2828 unsigned long flags;
2829 #if (NDEBUG & NDEBUG_AUTOSENSE)
2830 printk("scsi%d : performing request sense\n",
2831 instance->host_no);
2832 #endif
2833 cmd->cmnd[0] = REQUEST_SENSE;
2834 cmd->cmnd[1] &= 0xe0;
2835 cmd->cmnd[2] = 0;
2836 cmd->cmnd[3] = 0;
2837 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2838 cmd->cmnd[5] = 0;
2840 cmd->SCp.buffer = NULL;
2841 cmd->SCp.buffers_residual = 0;
2842 cmd->SCp.ptr = (char *) cmd->sense_buffer;
2843 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2845 save_flags(flags);
2846 cli();
2847 LIST(cmd, hostdata->issue_queue);
2848 cmd->host_scribble = (unsigned char *)
2849 hostdata->issue_queue;
2850 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2851 restore_flags(flags);
2852 #if (NDEBUG & NDEBUG_QUEUES)
2853 printk("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
2854 #endif
2855 } else {
2856 #endif /* def AUTOSENSE */
2857 #ifdef NCR5380_STATS
2858 collect_stats(hostdata, cmd);
2859 #endif
2860 cmd->scsi_done(cmd);
2863 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2865 * Restore phase bits to 0 so an interrupted selection,
2866 * arbitration can resume.
2868 NCR5380_write(TARGET_COMMAND_REG, 0);
2870 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2871 barrier();
2872 return;
2873 case MESSAGE_REJECT:
2874 /* Accept message by clearing ACK */
2875 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2876 switch (hostdata->last_message) {
2877 case HEAD_OF_QUEUE_TAG:
2878 case ORDERED_QUEUE_TAG:
2879 case SIMPLE_QUEUE_TAG:
2880 cmd->device->tagged_queue = 0;
2881 hostdata->busy[cmd->target] |= (1 << cmd->lun);
2882 break;
2883 default:
2884 break;
2886 case DISCONNECT: {
2887 unsigned long flags;
2888 /* Accept message by clearing ACK */
2889 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2890 cmd->device->disconnect = 1;
2891 save_flags(flags);
2892 cli();
2893 LIST(cmd, hostdata->disconnected_queue);
2894 cmd->host_scribble = (unsigned char *)
2895 hostdata->disconnected_queue;
2896 hostdata->connected = NULL;
2897 hostdata->disconnected_queue = cmd;
2898 restore_flags(flags);
2899 #if (NDEBUG & NDEBUG_QUEUES)
2900 printk("scsi%d : command for target %d lun %d was moved from connected to"
2901 " the disconnected_queue\n", instance->host_no,
2902 cmd->target, cmd->lun);
2903 #endif
2905 * Restore phase bits to 0 so an interrupted selection,
2906 * arbitration can resume.
2908 NCR5380_write(TARGET_COMMAND_REG, 0);
2910 /* Enable reselect interrupts */
2911 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2912 /* Wait for bus free to avoid nasty timeouts */
2913 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2914 barrier();
2915 #if 0
2916 NCR5380_print_status(instance);
2917 #endif
2918 return;
2921 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2922 * operation, in violation of the SCSI spec so we can safely
2923 * ignore SAVE/RESTORE pointers calls.
2925 * Unfortunately, some disks violate the SCSI spec and
2926 * don't issue the required SAVE_POINTERS message before
2927 * disconnecting, and we have to break spec to remain
2928 * compatible.
2930 case SAVE_POINTERS:
2931 case RESTORE_POINTERS:
2932 /* Accept message by clearing ACK */
2933 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2934 break;
2935 case EXTENDED_MESSAGE:
2937 * Extended messages are sent in the following format :
2938 * Byte
2939 * 0 EXTENDED_MESSAGE == 1
2940 * 1 length (includes one byte for code, doesn't
2941 * include first two bytes)
2942 * 2 code
2943 * 3..length+1 arguments
2945 * Start the extended message buffer with the EXTENDED_MESSAGE
2946 * byte, since print_msg() wants the whole thing.
2948 extended_msg[0] = EXTENDED_MESSAGE;
2949 /* Accept first byte by clearing ACK */
2950 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2952 #if (NDEBUG & NDEBUG_EXTENDED)
2953 printk("scsi%d : receiving extended message\n",
2954 instance->host_no);
2955 #endif
2957 len = 2;
2958 data = extended_msg + 1;
2959 phase = PHASE_MSGIN;
2960 NCR5380_transfer_pio(instance, &phase, &len, &data);
2962 #if (NDEBUG & NDEBUG_EXTENDED)
2963 printk("scsi%d : length=%d, code=0x%02x\n",
2964 instance->host_no, (int) extended_msg[1],
2965 (int) extended_msg[2]);
2966 #endif
2968 if (!len && extended_msg[1] <=
2969 (sizeof(extended_msg) - 1)) {
2970 /* Accept third byte by clearing ACK */
2971 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2972 len = extended_msg[1] - 1;
2973 data = extended_msg + 3;
2974 phase = PHASE_MSGIN;
2976 NCR5380_transfer_pio(instance, &phase, &len, &data);
2978 #if (NDEBUG & NDEBUG_EXTENDED)
2979 printk("scsi%d : message received, residual %d\n",
2980 instance->host_no, len);
2981 #endif
2983 switch (extended_msg[2]) {
2984 case EXTENDED_SDTR:
2985 case EXTENDED_WDTR:
2986 case EXTENDED_MODIFY_DATA_POINTER:
2987 case EXTENDED_EXTENDED_IDENTIFY:
2988 tmp = 0;
2990 } else if (len) {
2991 printk("scsi%d: error receiving extended message\n",
2992 instance->host_no);
2993 tmp = 0;
2994 } else {
2995 printk("scsi%d: extended message code %02x length %d is too long\n",
2996 instance->host_no, extended_msg[2], extended_msg[1]);
2997 tmp = 0;
2999 /* Fall through to reject message */
3002 * If we get something weird that we aren't expecting,
3003 * reject it.
3005 default:
3006 if (!tmp) {
3007 printk("scsi%d: rejecting message ", instance->host_no);
3008 print_msg(extended_msg);
3009 printk("\n");
3010 } else if (tmp != EXTENDED_MESSAGE)
3011 printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n",
3012 instance->host_no, tmp, cmd->target, cmd->lun);
3013 else
3014 printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n",
3015 instance->host_no, extended_msg[1], extended_msg[0], cmd->target, cmd->lun);
3017 msgout = MESSAGE_REJECT;
3018 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
3019 ICR_ASSERT_ATN);
3020 break;
3021 } /* switch (tmp) */
3022 break;
3023 case PHASE_MSGOUT:
3024 len = 1;
3025 data = &msgout;
3026 hostdata->last_message = msgout;
3027 NCR5380_transfer_pio(instance, &phase, &len, &data);
3028 if (msgout == ABORT) {
3029 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
3030 hostdata->connected = NULL;
3031 cmd->result = DID_ERROR << 16;
3032 #ifdef NCR5380_STATS
3033 collect_stats(hostdata, cmd);
3034 #endif
3035 cmd->scsi_done(cmd);
3036 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
3037 return;
3039 msgout = NOP;
3040 break;
3041 case PHASE_CMDOUT:
3042 len = cmd->cmd_len;
3043 data = cmd->cmnd;
3045 * XXX for performance reasons, on machines with a
3046 * PSEUDO-DMA architecture we should probably
3047 * use the dma transfer function.
3049 NCR5380_transfer_pio(instance, &phase, &len,
3050 &data);
3051 #ifdef USLEEP
3052 if (!cmd->device->disconnect &&
3053 should_disconnect(cmd->cmnd[0]))
3055 hostdata->time_expires = jiffies + USLEEP_SLEEP;
3056 #if (NDEBUG & NDEBUG_USLEEP)
3057 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
3058 hostdata->time_expires);
3059 #endif
3060 NCR5380_set_timer(instance);
3061 return;
3063 #endif /* def USLEEP */
3064 break;
3065 case PHASE_STATIN:
3066 len = 1;
3067 data = &tmp;
3068 NCR5380_transfer_pio(instance, &phase, &len, &data);
3069 cmd->SCp.Status = tmp;
3070 break;
3071 default:
3072 printk("scsi%d : unknown phase\n", instance->host_no);
3073 #ifdef NDEBUG
3074 NCR5380_print(instance);
3075 #endif
3076 } /* switch(phase) */
3077 } /* if (tmp * SR_REQ) */
3078 #ifdef USLEEP
3079 else
3081 /* RvC: go to sleep if polling time expired
3083 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time))
3085 hostdata->time_expires = jiffies + USLEEP_SLEEP;
3086 #if (NDEBUG & NDEBUG_USLEEP)
3087 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
3088 hostdata->time_expires);
3089 #endif
3090 NCR5380_set_timer(instance);
3091 return;
3094 #endif
3095 } /* while (1) */
3099 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
3101 * Purpose : does reselection, initializing the instance->connected
3102 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
3103 * nexus has been reestablished,
3105 * Inputs : instance - this instance of the NCR5380.
3110 static void NCR5380_reselect(struct Scsi_Host *instance) {
3111 NCR5380_local_declare();
3112 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
3113 instance->hostdata;
3114 unsigned char target_mask;
3115 unsigned char lun, phase;
3116 int len;
3117 #ifdef SCSI2
3118 unsigned char tag;
3119 #endif
3120 unsigned char msg[3];
3121 unsigned char *data;
3122 Scsi_Cmnd *tmp = NULL, *prev;
3123 int abort = 0;
3124 NCR5380_setup(instance);
3127 * Disable arbitration, etc. since the host adapter obviously
3128 * lost, and tell an interrupted NCR5380_select() to restart.
3131 NCR5380_write(MODE_REG, MR_BASE);
3132 hostdata->restart_select = 1;
3134 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
3136 #if (NDEBUG & NDEBUG_RESELECTION)
3137 printk("scsi%d : reselect\n", instance->host_no);
3138 #endif
3141 * At this point, we have detected that our SCSI ID is on the bus,
3142 * SEL is true and BSY was false for at least one bus settle delay
3143 * (400 ns).
3145 * We must assert BSY ourselves, until the target drops the SEL
3146 * signal.
3149 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
3151 while (NCR5380_read(STATUS_REG) & SR_SEL);
3152 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3155 * Wait for target to go into MSGIN.
3158 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
3160 len = 1;
3161 data = msg;
3162 phase = PHASE_MSGIN;
3163 NCR5380_transfer_pio(instance, &phase, &len, &data);
3166 if (!msg[0] & 0x80) {
3167 printk("scsi%d : expecting IDENTIFY message, got ",
3168 instance->host_no);
3169 print_msg(msg);
3170 abort = 1;
3171 } else {
3172 /* Accept message by clearing ACK */
3173 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3174 lun = (msg[0] & 0x07);
3177 * We need to add code for SCSI-II to track which devices have
3178 * I_T_L_Q nexuses established, and which have simple I_T_L
3179 * nexuses so we can chose to do additional data transfer.
3182 #ifdef SCSI2
3183 #error "SCSI-II tagged queueing is not supported yet"
3184 #endif
3187 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
3188 * just reestablished, and remove it from the disconnected queue.
3192 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
3193 tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
3194 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
3195 #ifdef SCSI2
3196 && (tag == tmp->tag)
3197 #endif
3199 if (prev) {
3200 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
3201 prev->host_scribble = tmp->host_scribble;
3202 } else {
3203 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
3204 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
3206 tmp->host_scribble = NULL;
3207 break;
3209 if (!tmp) {
3210 #ifdef SCSI2
3211 printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
3212 instance->host_no, target_mask, lun, tag);
3213 #else
3214 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
3215 instance->host_no, target_mask, lun);
3216 #endif
3218 * Since we have an established nexus that we can't do anything with,
3219 * we must abort it.
3221 abort = 1;
3225 if (abort) {
3226 do_abort(instance);
3227 } else {
3228 hostdata->connected = tmp;
3229 #if (NDEBUG & NDEBUG_RESELECTION)
3230 printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
3231 instance->host_no, tmp->target, tmp->lun, tmp->tag);
3232 #endif
3237 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
3239 * Purpose : called by interrupt handler when DMA finishes or a phase
3240 * mismatch occurs (which would finish the DMA transfer).
3242 * Inputs : instance - this instance of the NCR5380.
3244 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
3245 * nexus has been reestablished, on failure NULL is returned.
3248 #ifdef REAL_DMA
3249 static void NCR5380_dma_complete(NCR5380_instance * instance) {
3250 NCR5380_local_declare();
3251 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *
3252 instance->hostdata);
3253 int transferred;
3254 NCR5380_setup(instance);
3257 * XXX this might not be right.
3259 * Wait for final byte to transfer, ie wait for ACK to go false.
3261 * We should use the Last Byte Sent bit, unfortunately this is
3262 * not available on the 5380/5381 (only the various CMOS chips)
3265 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
3267 NCR5380_write(MODE_REG, MR_BASE);
3268 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3271 * The only places we should see a phase mismatch and have to send
3272 * data from the same set of pointers will be the data transfer
3273 * phases. So, residual, requested length are only important here.
3276 if (!(hostdata->connected->SCp.phase & SR_CD)) {
3277 transferred = instance->dmalen - NCR5380_dma_residual();
3278 hostdata->connected->SCp.this_residual -= transferred;
3279 hostdata->connected->SCp.ptr += transferred;
3282 #endif /* def REAL_DMA */
3285 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
3287 * Purpose : abort a command
3289 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
3290 * host byte of the result field to, if zero DID_ABORTED is
3291 * used.
3293 * Returns : 0 - success, -1 on failure.
3295 * XXX - there is no way to abort the command that is currently
3296 * connected, you have to wait for it to complete. If this is
3297 * a problem, we could implement longjmp() / setjmp(), setjmp()
3298 * called where the loop started in NCR5380_main().
3301 #ifndef NCR5380_abort
3302 static
3303 #endif
3304 int NCR5380_abort(Scsi_Cmnd * cmd) {
3305 NCR5380_local_declare();
3306 unsigned long flags;
3307 struct Scsi_Host *instance = cmd->host;
3308 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
3309 instance->hostdata;
3310 Scsi_Cmnd *tmp, **prev;
3312 printk("scsi%d : aborting command\n", instance->host_no);
3313 print_Scsi_Cmnd(cmd);
3315 NCR5380_print_status(instance);
3317 printk("scsi%d : aborting command\n", instance->host_no);
3318 print_Scsi_Cmnd(cmd);
3320 NCR5380_print_status(instance);
3322 save_flags(flags);
3323 cli();
3324 NCR5380_setup(instance);
3326 #if (NDEBUG & NDEBUG_ABORT)
3327 printk("scsi%d : abort called\n", instance->host_no);
3328 printk(" basr 0x%X, sr 0x%X\n",
3329 NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
3330 #endif
3332 #if 0
3334 * Case 1 : If the command is the currently executing command,
3335 * we'll set the aborted flag and return control so that
3336 * information transfer routine can exit cleanly.
3339 if (hostdata->connected == cmd) {
3340 #if (NDEBUG & NDEBUG_ABORT)
3341 printk("scsi%d : aborting connected command\n", instance->host_no);
3342 #endif
3343 hostdata->aborted = 1;
3345 * We should perform BSY checking, and make sure we haven't slipped
3346 * into BUS FREE.
3349 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
3351 * Since we can't change phases until we've completed the current
3352 * handshake, we have to source or sink a byte of data if the current
3353 * phase is not MSGOUT.
3357 * Return control to the executing NCR drive so we can clear the
3358 * aborted flag and get back into our main loop.
3361 return 0;
3363 #endif
3366 * Case 2 : If the command hasn't been issued yet, we simply remove it
3367 * from the issue queue.
3369 #if (NDEBUG & NDEBUG_ABORT)
3370 /* KLL */
3371 printk("scsi%d : abort going into loop.\n", instance->host_no);
3372 #endif
3373 for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue),
3374 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
3375 tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp =
3376 (Scsi_Cmnd *) tmp->host_scribble)
3377 if (cmd == tmp) {
3378 REMOVE(5, *prev, tmp, tmp->host_scribble);
3379 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
3380 tmp->host_scribble = NULL;
3381 tmp->result = DID_ABORT << 16;
3382 restore_flags(flags);
3383 #if (NDEBUG & NDEBUG_ABORT)
3384 printk("scsi%d : abort removed command from issue queue.\n",
3385 instance->host_no);
3386 #endif
3387 tmp->done(tmp);
3388 return SCSI_ABORT_SUCCESS;
3390 #if (NDEBUG & NDEBUG_ABORT)
3391 /* KLL */
3392 else if (prev == tmp)
3393 printk("scsi%d : LOOP\n", instance->host_no);
3394 #endif
3397 * Case 3 : If any commands are connected, we're going to fail the abort
3398 * and let the high level SCSI driver retry at a later time or
3399 * issue a reset.
3401 * Timeouts, and therefore aborted commands, will be highly unlikely
3402 * and handling them cleanly in this situation would make the common
3403 * case of noresets less efficient, and would pollute our code. So,
3404 * we fail.
3407 if (hostdata->connected) {
3408 restore_flags(flags);
3409 #if (NDEBUG & NDEBUG_ABORT)
3410 printk("scsi%d : abort failed, command connected.\n", instance->host_no);
3411 #endif
3412 return SCSI_ABORT_NOT_RUNNING;
3415 * Case 4: If the command is currently disconnected from the bus, and
3416 * there are no connected commands, we reconnect the I_T_L or
3417 * I_T_L_Q nexus associated with it, go into message out, and send
3418 * an abort message.
3420 * This case is especially ugly. In order to reestablish the nexus, we
3421 * need to call NCR5380_select(). The easiest way to implement this
3422 * function was to abort if the bus was busy, and let the interrupt
3423 * handler triggered on the SEL for reselect take care of lost arbitrations
3424 * where necessary, meaning interrupts need to be enabled.
3426 * When interrupts are enabled, the queues may change - so we
3427 * can't remove it from the disconnected queue before selecting it
3428 * because that could cause a failure in hashing the nexus if that
3429 * device reselected.
3431 * Since the queues may change, we can't use the pointers from when we
3432 * first locate it.
3434 * So, we must first locate the command, and if NCR5380_select()
3435 * succeeds, then issue the abort, relocate the command and remove
3436 * it from the disconnected queue.
3439 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
3440 tmp = (Scsi_Cmnd *) tmp->host_scribble)
3441 if (cmd == tmp) {
3442 restore_flags(flags);
3443 #if (NDEBUG & NDEBUG_ABORT)
3444 printk("scsi%d : aborting disconnected command.\n", instance->host_no);
3445 #endif
3447 if (NCR5380_select(instance, cmd, (int) cmd->tag))
3448 return SCSI_ABORT_BUSY;
3450 #if (NDEBUG & NDEBUG_ABORT)
3451 printk("scsi%d : nexus reestablished.\n", instance->host_no);
3452 #endif
3454 do_abort(instance);
3456 cli();
3457 for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue),
3458 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
3459 tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp =
3460 (Scsi_Cmnd *) tmp->host_scribble)
3461 if (cmd == tmp) {
3462 REMOVE(5, *prev, tmp, tmp->host_scribble);
3463 *prev = (Scsi_Cmnd *) tmp->host_scribble;
3464 tmp->host_scribble = NULL;
3465 tmp->result = DID_ABORT << 16;
3466 restore_flags(flags);
3467 tmp->done(tmp);
3468 return SCSI_ABORT_SUCCESS;
3472 * Case 5 : If we reached this point, the command was not found in any of
3473 * the queues.
3475 * We probably reached this point because of an unlikely race condition
3476 * between the command completing successfully and the abortion code,
3477 * so we won't panic, but we will notify the user in case something really
3478 * broke.
3481 restore_flags(flags);
3482 printk("scsi%d : warning : SCSI command probably completed successfully\n"
3483 " before abortion\n", instance->host_no);
3484 return SCSI_ABORT_NOT_RUNNING;
3489 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
3491 * Purpose : reset the SCSI bus.
3493 * Returns : SCSI_RESET_WAKEUP
3497 #ifndef NCR5380_reset
3498 static
3499 #endif
3500 int NCR5380_reset(Scsi_Cmnd * cmd, unsigned int dummy) {
3501 NCR5380_local_declare();
3502 NCR5380_setup(cmd->host);
3504 NCR5380_print_status(cmd->host);
3505 do_reset(cmd->host);
3507 return SCSI_RESET_WAKEUP;