- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / NCR5380.c
blob7e9c3f9b58c8ba367a0b381de6dd90bc356ecd25
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 if (!main_running) {
567 main_running = 1;
568 NCR5380_main();
572 #ifdef USLEEP
575 * These need tweaking, and would probably work best as per-device
576 * flags initialized differently for disk, tape, cd, etc devices.
577 * People with broken devices are free to experiment as to what gives
578 * the best results for them.
580 * USLEEP_SLEEP should be a minimum seek time.
582 * USLEEP_POLL should be a maximum rotational latency.
584 #ifndef USLEEP_SLEEP
585 /* 20 ms (reasonable hard disk speed) */
586 #define USLEEP_SLEEP (20*HZ/1000)
587 #endif
588 /* 300 RPM (floppy speed) */
589 #ifndef USLEEP_POLL
590 #define USLEEP_POLL (200*HZ/1000)
591 #endif
592 #ifndef USLEEP_WAITLONG
593 /* RvC: (reasonable time to wait on select error) */
594 #define USLEEP_WAITLONG USLEEP_SLEEP
595 #endif
597 static struct Scsi_Host *expires_first = NULL;
600 * Function : int should_disconnect (unsigned char cmd)
602 * Purpose : decide weather a command would normally disconnect or
603 * not, since if it won't disconnect we should go to sleep.
605 * Input : cmd - opcode of SCSI command
607 * Returns : DISCONNECT_LONG if we should disconnect for a really long
608 * time (ie always, sleep, look for REQ active, sleep),
609 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
610 * time-to-data delay, DISCONNECT_NONE if this command would return
611 * immediately.
613 * Future sleep algorithms based on time to data can exploit
614 * something like this so they can differentiate between "normal"
615 * (ie, read, write, seek) and unusual commands (ie, * format).
617 * Note : We don't deal with commands that handle an immediate disconnect,
621 static int should_disconnect(unsigned char cmd)
623 switch (cmd) {
624 case READ_6:
625 case WRITE_6:
626 case SEEK_6:
627 case READ_10:
628 case WRITE_10:
629 case SEEK_10:
630 return DISCONNECT_TIME_TO_DATA;
631 case FORMAT_UNIT:
632 case SEARCH_HIGH:
633 case SEARCH_LOW:
634 case SEARCH_EQUAL:
635 return DISCONNECT_LONG;
636 default:
637 return DISCONNECT_NONE;
642 * Assumes instance->time_expires has been set in higher level code.
645 static int NCR5380_set_timer(struct Scsi_Host *instance)
647 unsigned long flags;
648 struct Scsi_Host *tmp, **prev;
650 save_flags(flags);
651 cli();
652 if (((struct NCR5380_hostdata *) (instance->hostdata))->next_timer) {
653 restore_flags(flags);
654 return -1;
656 for (prev = &expires_first, tmp = expires_first; tmp;
657 prev = &(((struct NCR5380_hostdata *) tmp->hostdata)->next_timer),
658 tmp = ((struct NCR5380_hostdata *) tmp->hostdata)->next_timer)
659 if (((struct NCR5380_hostdata *)instance->hostdata)->time_expires <
660 ((struct NCR5380_hostdata *)tmp->hostdata)->time_expires)
661 break;
663 ((struct NCR5380_hostdata *) instance->hostdata)->next_timer = tmp;
664 *prev = instance;
666 mod_timer(&usleep_timer, ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires);
667 restore_flags(flags);
668 return 0;
671 /* Doing something about unwanted reentrancy here might be useful */
672 void NCR5380_timer_fn(unsigned long surplus_to_requirements)
674 unsigned long flags;
675 struct Scsi_Host *instance;
676 save_flags(flags);
677 cli();
678 for (; expires_first &&
679 time_before_eq(((struct NCR5380_hostdata *)expires_first->hostdata)->time_expires, jiffies); )
681 instance = ((struct NCR5380_hostdata *) expires_first->hostdata)->next_timer;
682 ((struct NCR5380_hostdata *) expires_first->hostdata)->next_timer = NULL;
683 ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires = 0;
684 expires_first = instance;
687 del_timer(&usleep_timer);
688 if (expires_first)
690 usleep_timer.expires = ((struct NCR5380_hostdata *)expires_first->hostdata)->time_expires;
691 add_timer(&usleep_timer);
693 restore_flags(flags);
695 spin_lock_irqsave(&io_request_lock, flags);
696 run_main();
697 spin_unlock_irqrestore(&io_request_lock, flags);
699 #endif /* def USLEEP */
701 static inline void NCR5380_all_init(void)
703 static int done = 0;
704 if (!done) {
705 #if (NDEBUG & NDEBUG_INIT)
706 printk("scsi : NCR5380_all_init()\n");
707 #endif
708 done = 1;
709 #ifdef USLEEP
710 init_timer(&usleep_timer);
711 usleep_timer.function = NCR5380_timer_fn;
712 #endif
716 #ifdef AUTOPROBE_IRQ
718 * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
720 * Purpose : autoprobe for the IRQ line used by the NCR5380.
722 * Inputs : instance - pointer to this instance of the NCR5380 driver,
723 * possible - bitmask of permissible interrupts.
725 * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired.
727 * XXX no effort is made to deal with spurious interrupts.
731 static int probe_irq __initdata = 0;
733 static void __init probe_intr(int irq, void *dev_id, struct pt_regs *regs)
735 probe_irq = irq;
738 static int __init NCR5380_probe_irq(struct Scsi_Host *instance, int possible)
740 NCR5380_local_declare();
741 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
742 instance->hostdata;
743 unsigned long timeout;
744 int trying_irqs, i, mask;
745 NCR5380_setup(instance);
747 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
748 if ((mask & possible) && (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL)
749 == 0))
750 trying_irqs |= mask;
752 timeout = jiffies + (250 * HZ / 1000);
753 probe_irq = IRQ_NONE;
756 * A interrupt is triggered whenever BSY = false, SEL = true
757 * and a bit set in the SELECT_ENABLE_REG is asserted on the
758 * SCSI bus.
760 * Note that the bus is only driven when the phase control signals
761 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
762 * to zero.
765 NCR5380_write(TARGET_COMMAND_REG, 0);
766 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
767 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
768 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
769 ICR_ASSERT_SEL);
771 while (probe_irq == IRQ_NONE && time_before(jiffies,timeout))
772 barrier();
774 NCR5380_write(SELECT_ENABLE_REG, 0);
775 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
777 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
778 if (trying_irqs & mask)
779 free_irq(i, NULL);
781 return probe_irq;
783 #endif /* AUTOPROBE_IRQ */
786 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
788 * Purpose : called by probe code indicating the NCR5380 driver
789 * options that were selected.
791 * Inputs : instance, pointer to this instance. Unused.
794 static void __init NCR5380_print_options(struct Scsi_Host *instance)
796 printk(" generic options"
797 #ifdef AUTOPROBE_IRQ
798 " AUTOPROBE_IRQ"
799 #endif
800 #ifdef AUTOSENSE
801 " AUTOSENSE"
802 #endif
803 #ifdef DIFFERENTIAL
804 " DIFFERENTIAL"
805 #endif
806 #ifdef REAL_DMA
807 " REAL DMA"
808 #endif
809 #ifdef REAL_DMA_POLL
810 " REAL DMA POLL"
811 #endif
812 #ifdef PARITY
813 " PARITY"
814 #endif
815 #ifdef PSEUDO_DMA
816 " PSEUDO DMA"
817 #endif
818 #ifdef SCSI2
819 " SCSI-2"
820 #endif
821 #ifdef UNSAFE
822 " UNSAFE "
823 #endif
825 #ifdef USLEEP
826 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
827 #endif
828 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
829 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
830 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
835 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
837 * Purpose : print commands in the various queues, called from
838 * NCR5380_abort and NCR5380_debug to aid debugging.
840 * Inputs : instance, pointer to this instance.
843 static void NCR5380_print_status(struct Scsi_Host *instance)
845 static char pr_bfr[512];
846 char *start;
847 int len;
849 printk("NCR5380 : coroutine is%s running.\n",
850 main_running ? "" : "n't");
852 #ifdef NDEBUG
853 NCR5380_print(instance);
854 NCR5380_print_phase(instance);
855 #endif
857 len = NCR5380_proc_info(pr_bfr, &start, 0, sizeof(pr_bfr),
858 instance->host_no, 0);
859 pr_bfr[len] = 0;
860 printk("\n%s\n", pr_bfr);
863 /******************************************/
865 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
867 * *buffer: I/O buffer
868 * **start: if inout == FALSE pointer into buffer where user read should start
869 * offset: current offset
870 * length: length of buffer
871 * hostno: Scsi_Host host_no
872 * inout: TRUE - user is writing; FALSE - user is reading
874 * Return the number of bytes read from or written
877 #undef SPRINTF
878 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
879 static
880 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
881 static
882 char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
883 static
884 char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
886 #ifndef NCR5380_proc_info
887 static
888 #endif
889 int NCR5380_proc_info(
890 char *buffer, char **start, off_t offset,
891 int length, int hostno, int inout)
893 unsigned long flags;
894 char *pos = buffer;
895 struct Scsi_Host *instance;
896 struct NCR5380_hostdata *hostdata;
897 Scsi_Cmnd *ptr;
899 for (instance = first_instance; instance &&
900 instance->host_no != hostno; instance = instance->next);
901 if (!instance)
902 return (-ESRCH);
903 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
905 if (inout) { /* Has data been written to the file ? */
906 #ifdef DTC_PUBLIC_RELEASE
907 dtc_wmaxi = dtc_maxi = 0;
908 #endif
909 #ifdef PAS16_PUBLIC_RELEASE
910 pas_wmaxi = pas_maxi = 0;
911 #endif
912 return (-ENOSYS); /* Currently this is a no-op */
914 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
915 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
916 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
917 #ifdef DTC_PUBLIC_RELEASE
918 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
919 #endif
920 #ifdef T128_PUBLIC_RELEASE
921 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
922 #endif
923 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
924 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
925 #endif
926 #ifdef PAS16_PUBLIC_RELEASE
927 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
928 #endif
930 SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
931 SPRINTF("io_port: %04x ", (int) instance->io_port);
932 if (instance->irq == IRQ_NONE)
933 SPRINTF("IRQ: None.\n");
934 else
935 SPRINTF("IRQ: %d.\n", instance->irq);
937 #ifdef DTC_PUBLIC_RELEASE
938 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
939 dtc_wmaxi, dtc_maxi);
940 #endif
941 #ifdef PAS16_PUBLIC_RELEASE
942 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
943 pas_wmaxi, pas_maxi);
944 #endif
945 save_flags(flags);
946 cli();
947 SPRINTF("NCR5380 : coroutine is%s running.\n", main_running ? "" : "n't");
948 if (!hostdata->connected)
949 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
950 else
951 pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected,
952 pos, buffer, length);
953 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
954 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr;
955 ptr = (Scsi_Cmnd *) ptr->host_scribble)
956 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
958 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
959 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
960 ptr = (Scsi_Cmnd *) ptr->host_scribble)
961 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
963 restore_flags(flags);
964 *start = buffer;
965 if (pos - buffer < offset)
966 return 0;
967 else if (pos - buffer - offset < length)
968 return pos - buffer - offset;
969 return length;
972 static
973 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
975 SPRINTF("scsi%d : destination target %d, lun %d\n",
976 cmd->host->host_no, cmd->target, cmd->lun);
977 SPRINTF(" command = ");
978 pos = lprint_command(cmd->cmnd, pos, buffer, length);
979 return (pos);
982 static
983 char *lprint_command(unsigned char *command,
984 char *pos, char *buffer, int length)
986 int i, s;
987 pos = lprint_opcode(command[0], pos, buffer, length);
988 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
989 SPRINTF("%02x ", command[i]);
990 SPRINTF("\n");
991 return (pos);
994 static
995 char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
997 SPRINTF("%2d (0x%02x)", opcode, opcode);
998 return (pos);
1003 * Function : void NCR5380_init (struct Scsi_Host *instance, flags)
1005 * Purpose : initializes *instance and corresponding 5380 chip,
1006 * with flags OR'd into the initial flags value.
1008 * Inputs : instance - instantiation of the 5380 driver.
1010 * Notes : I assume that the host, hostno, and id bits have been
1011 * set correctly. I don't care about the irq and other fields.
1015 static void __init NCR5380_init(struct Scsi_Host *instance, int flags)
1017 NCR5380_local_declare();
1018 int i, pass;
1019 unsigned long timeout;
1020 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
1021 instance->hostdata;
1024 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
1025 * the base address.
1028 #ifdef NCR53C400
1029 if (flags & FLAG_NCR53C400)
1030 instance->NCR5380_instance_name += NCR53C400_address_adjust;
1031 #endif
1033 NCR5380_setup(instance);
1035 NCR5380_all_init();
1037 hostdata->aborted = 0;
1038 hostdata->id_mask = 1 << instance->this_id;
1039 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
1040 if (i > hostdata->id_mask)
1041 hostdata->id_higher_mask |= i;
1042 for (i = 0; i < 8; ++i)
1043 hostdata->busy[i] = 0;
1044 #ifdef REAL_DMA
1045 hostdata->dmalen = 0;
1046 #endif
1047 hostdata->targets_present = 0;
1048 hostdata->connected = NULL;
1049 hostdata->issue_queue = NULL;
1050 hostdata->disconnected_queue = NULL;
1051 #ifdef NCR5380_STATS
1052 for (i = 0; i < 8; ++i) {
1053 hostdata->time_read[i] = 0;
1054 hostdata->time_write[i] = 0;
1055 hostdata->bytes_read[i] = 0;
1056 hostdata->bytes_write[i] = 0;
1058 hostdata->timebase = 0;
1059 hostdata->pendingw = 0;
1060 hostdata->pendingr = 0;
1061 #endif
1063 /* The CHECK code seems to break the 53C400. Will check it later maybe */
1064 if (flags & FLAG_NCR53C400)
1065 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
1066 else
1067 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
1069 if (!the_template) {
1070 the_template = instance->hostt;
1071 first_instance = instance;
1073 #ifdef USLEEP
1074 hostdata->time_expires = 0;
1075 hostdata->next_timer = NULL;
1076 #endif
1078 #ifndef AUTOSENSE
1079 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
1081 printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
1082 " without AUTOSENSE option, contingent allegiance conditions may\n"
1083 " be incorrectly cleared.\n", instance->host_no);
1084 #endif /* def AUTOSENSE */
1086 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1087 NCR5380_write(MODE_REG, MR_BASE);
1088 NCR5380_write(TARGET_COMMAND_REG, 0);
1089 NCR5380_write(SELECT_ENABLE_REG, 0);
1091 #ifdef NCR53C400
1092 if (hostdata->flags & FLAG_NCR53C400) {
1093 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
1095 #endif
1098 * Detect and correct bus wedge problems.
1100 * If the system crashed, it may have crashed in a state
1101 * where a SCSI command was still executing, and the
1102 * SCSI bus is not in a BUS FREE STATE.
1104 * If this is the case, we'll try to abort the currently
1105 * established nexus which we know nothing about, and that
1106 * failing, do a hard reset of the SCSI bus
1109 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) &&
1110 pass <= 6; ++pass) {
1111 switch (pass) {
1112 case 1:
1113 case 3:
1114 case 5:
1115 printk("scsi%d: SCSI bus busy, waiting up to five seconds\n",
1116 instance->host_no);
1117 timeout = jiffies + 5 * HZ;
1118 while (time_before(jiffies,timeout) && (NCR5380_read(STATUS_REG) & SR_BSY));
1119 break;
1120 case 2:
1121 printk("scsi%d: bus busy, attempting abort\n",
1122 instance->host_no);
1123 do_abort(instance);
1124 break;
1125 case 4:
1126 printk("scsi%d: bus busy, attempting reset\n",
1127 instance->host_no);
1128 do_reset(instance);
1129 break;
1130 case 6:
1131 printk("scsi%d: bus locked solid or invalid override\n",
1132 instance->host_no);
1138 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
1139 * void (*done)(Scsi_Cmnd *))
1141 * Purpose : enqueues a SCSI command
1143 * Inputs : cmd - SCSI command, done - function called on completion, with
1144 * a pointer to the command descriptor.
1146 * Returns : 0
1148 * Side effects :
1149 * cmd is added to the per instance issue_queue, with minor
1150 * twiddling done to the host specific fields of cmd. If the
1151 * main coroutine is not running, it is restarted.
1155 /* Only make static if a wrapper function is used */
1156 #ifndef NCR5380_queue_command
1157 static
1158 #endif
1159 int NCR5380_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *)) {
1160 struct Scsi_Host *instance = cmd->host;
1161 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
1162 instance->hostdata;
1163 Scsi_Cmnd *tmp;
1165 #if (NDEBUG & NDEBUG_NO_WRITE)
1166 switch (cmd->cmnd[0]) {
1167 case WRITE_6:
1168 case WRITE_10:
1169 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
1170 instance->host_no);
1171 cmd->result = (DID_ERROR << 16);
1172 done(cmd);
1173 return 0;
1175 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1177 #ifdef NCR5380_STATS
1178 #if 0
1179 if (!hostdata->connected && !hostdata->issue_queue &&
1180 !hostdata->disconnected_queue) {
1181 hostdata->timebase = jiffies;
1183 #endif
1184 #ifdef NCR5380_STAT_LIMIT
1185 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1186 #endif
1187 switch (cmd->cmnd[0]) {
1188 case WRITE:
1189 case WRITE_6:
1190 case WRITE_10:
1191 hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
1192 hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
1193 hostdata->pendingw++;
1194 break;
1195 case READ:
1196 case READ_6:
1197 case READ_10:
1198 hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
1199 hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
1200 hostdata->pendingr++;
1201 break;
1203 #endif
1206 * We use the host_scribble field as a pointer to the next command
1207 * in a queue
1210 cmd->host_scribble = NULL;
1211 cmd->scsi_done = done;
1213 cmd->result = 0;
1217 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1218 * commands are added to the head of the queue since any command will
1219 * clear the contingent allegiance condition that exists and the
1220 * sense data is only guaranteed to be valid while the condition exists.
1223 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1224 LIST(cmd, hostdata->issue_queue);
1225 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1226 hostdata->issue_queue = cmd;
1227 } else {
1228 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
1229 tmp = (Scsi_Cmnd *) tmp->host_scribble);
1230 LIST(cmd, tmp);
1231 tmp->host_scribble = (unsigned char *) cmd;
1233 #if (NDEBUG & NDEBUG_QUEUES)
1234 printk("scsi%d : command added to %s of queue\n", instance->host_no,
1235 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1236 #endif
1238 /* Run the coroutine if it isn't already running. */
1239 run_main();
1240 return 0;
1244 * Function : NCR5380_main (void)
1246 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1247 * be done on the NCR5380 host adapters in a system. Both
1248 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1249 * in case it is not running.
1251 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1252 * reenable them. This prevents reentrancy and kernel stack overflow.
1255 static void NCR5380_main(void) {
1256 Scsi_Cmnd *tmp, *prev;
1257 struct Scsi_Host *instance;
1258 struct NCR5380_hostdata *hostdata;
1259 int done;
1260 unsigned long flags;
1263 * We run (with interrupts disabled) until we're sure that none of
1264 * the host adapters have anything that can be done, at which point
1265 * we set main_running to 0 and exit.
1267 * Interrupts are enabled before doing various other internal
1268 * instructions, after we've decided that we need to run through
1269 * the loop again.
1271 * this should prevent any race conditions.
1274 spin_unlock_irq(&io_request_lock);
1276 save_flags(flags);
1278 do {
1279 cli(); /* Freeze request queues */
1280 done = 1;
1281 for (instance = first_instance; instance &&
1282 instance->hostt == the_template; instance = instance->next) {
1283 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1284 cli();
1285 #ifdef USLEEP
1286 if (!hostdata->connected && !hostdata->selecting) {
1287 #else
1288 if (!hostdata->connected) {
1289 #endif
1290 #if (NDEBUG & NDEBUG_MAIN)
1291 printk("scsi%d : not connected\n", instance->host_no);
1292 #endif
1294 * Search through the issue_queue for a command destined
1295 * for a target that's not busy.
1297 #if (NDEBUG & NDEBUG_LISTS)
1298 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp && (tmp != prev); prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble);
1299 /*printk("%p ", tmp); */
1300 if ((tmp == prev) && tmp)
1301 printk(" LOOP\n"); /* else printk("\n"); */
1302 #endif
1303 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1304 prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
1305 tmp->host_scribble) {
1307 #if (NDEBUG & NDEBUG_LISTS)
1308 if (prev != tmp)
1309 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun);
1310 #endif
1311 /* When we find one, remove it from the issue queue. */
1312 if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
1313 if (prev) {
1314 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
1315 prev->host_scribble = tmp->host_scribble;
1316 } else {
1317 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
1318 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1320 tmp->host_scribble = NULL;
1322 /* reenable interrupts after finding one */
1323 restore_flags(flags);
1326 * Attempt to establish an I_T_L nexus here.
1327 * On success, instance->hostdata->connected is set.
1328 * On failure, we must add the command back to the
1329 * issue queue so we can keep trying.
1331 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1332 printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
1333 instance->host_no, tmp->target, tmp->lun);
1334 #endif
1337 * A successful selection is defined as one that
1338 * leaves us with the command connected and
1339 * in hostdata->connected, OR has terminated the
1340 * command.
1342 * With successful commands, we fall through
1343 * and see if we can do an information transfer,
1344 * with failures we will restart.
1346 #ifdef USLEEP
1347 hostdata->selecting = 0; /* RvC: have to preset this
1348 to indicate a new command is being performed */
1349 #endif
1351 if (!NCR5380_select(instance, tmp,
1353 * REQUEST SENSE commands are issued without tagged
1354 * queueing, even on SCSI-II devices because the
1355 * contingent allegiance condition exists for the
1356 * entire unit.
1358 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1359 TAG_NEXT)) {
1360 break;
1361 } else {
1362 cli();
1363 LIST(tmp, hostdata->issue_queue);
1364 tmp->host_scribble = (unsigned char *)
1365 hostdata->issue_queue;
1366 hostdata->issue_queue = tmp;
1367 done = 0;
1368 restore_flags(flags);
1369 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1370 printk("scsi%d : main(): select() failed, returned to issue_queue\n",
1371 instance->host_no);
1372 #endif
1374 } /* if target/lun is not busy */
1375 } /* for */
1376 } /* if (!hostdata->connected) */
1377 #ifdef USLEEP
1378 if (hostdata->selecting)
1380 tmp = (Scsi_Cmnd *)hostdata->selecting;
1381 if (!NCR5380_select(instance, tmp,
1382 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT))
1384 /* Ok ?? */
1386 else
1388 /* RvC: device failed, so we wait a long time
1389 this is needed for Mustek scanners, that
1390 do not respond to commands immediately
1391 after a scan */
1392 printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n",
1393 instance->host_no, tmp->target);
1394 cli();
1395 LIST(tmp, hostdata->issue_queue);
1396 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1397 hostdata->issue_queue = tmp;
1398 restore_flags(flags);
1400 hostdata->time_expires = jiffies + USLEEP_WAITLONG;
1401 NCR5380_set_timer (instance);
1403 } /* if hostdata->selecting */
1404 #endif
1405 if (hostdata->connected
1406 #ifdef REAL_DMA
1407 && !hostdata->dmalen
1408 #endif
1409 #ifdef USLEEP
1410 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1411 #endif
1413 restore_flags(flags);
1414 #if (NDEBUG & NDEBUG_MAIN)
1415 printk("scsi%d : main() : performing information transfer\n",
1416 instance->host_no);
1417 #endif
1418 NCR5380_information_transfer(instance);
1419 #if (NDEBUG & NDEBUG_MAIN)
1420 printk("scsi%d : main() : done set false\n", instance->host_no);
1421 #endif
1422 done = 0;
1423 } else
1424 break;
1425 } /* for instance */
1426 } while (!done);
1427 spin_lock_irq(&io_request_lock);
1428 /* cli();*/
1429 main_running = 0;
1432 #ifndef DONT_USE_INTR
1433 #include <linux/blk.h>
1434 #include <linux/spinlock.h>
1437 * Function : void NCR5380_intr (int irq)
1439 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1440 * from the disconnected queue, and restarting NCR5380_main()
1441 * as required.
1443 * Inputs : int irq, irq that caused this interrupt.
1447 static void NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs) {
1448 NCR5380_local_declare();
1449 struct Scsi_Host *instance;
1450 int done;
1451 unsigned char basr;
1452 unsigned long flags;
1454 save_flags(flags);
1455 cli();
1456 #if (NDEBUG & NDEBUG_INTR)
1457 printk("scsi : NCR5380 irq %d triggered\n", irq);
1458 #endif
1459 do {
1460 done = 1;
1461 for (instance = first_instance; instance && (instance->hostt ==
1462 the_template); instance = instance->next)
1463 if (instance->irq == irq) {
1465 /* Look for pending interrupts */
1466 NCR5380_setup(instance);
1467 basr = NCR5380_read(BUS_AND_STATUS_REG);
1468 /* XXX dispatch to appropriate routine if found and done=0 */
1469 if (basr & BASR_IRQ) {
1470 #if (NDEBUG & NDEBUG_INTR)
1471 NCR5380_print(instance);
1472 #endif
1473 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1474 (SR_SEL | SR_IO)) {
1475 done = 0;
1476 restore_flags(flags);
1477 #if (NDEBUG & NDEBUG_INTR)
1478 printk("scsi%d : SEL interrupt\n", instance->host_no);
1479 #endif
1480 NCR5380_reselect(instance);
1481 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1482 } else if (basr & BASR_PARITY_ERROR) {
1483 #if (NDEBUG & NDEBUG_INTR)
1484 printk("scsi%d : PARITY interrupt\n", instance->host_no);
1485 #endif
1486 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1487 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1488 #if (NDEBUG & NDEBUG_INTR)
1489 printk("scsi%d : RESET interrupt\n", instance->host_no);
1490 #endif
1491 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1492 } else {
1494 * XXX the rest of the interrupt conditions should *only* occur during a
1495 * DMA transfer, which I haven't gotten around to fixing yet.
1498 #if defined(REAL_DMA)
1500 * We should only get PHASE MISMATCH and EOP interrupts
1501 * if we have DMA enabled, so do a sanity check based on
1502 * the current setting of the MODE register.
1505 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr &
1506 BASR_END_DMA_TRANSFER) ||
1507 !(basr & BASR_PHASE_MATCH))) {
1508 int transfered;
1510 if (!hostdata->connected)
1511 panic("scsi%d : received end of DMA interrupt with no connected cmd\n",
1512 instance->hostno);
1514 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1515 hostdata->connected->SCp.this_residual -= transferred;
1516 hostdata->connected->SCp.ptr += transferred;
1517 hostdata->dmalen = 0;
1519 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1520 #if NCR_TIMEOUT
1522 unsigned long timeout = jiffies + NCR_TIMEOUT;
1524 spin_unlock_irq(&io_request_lock);
1525 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK
1526 && time_before(jiffies, timeout));
1527 spin_lock_irq(&io_request_lock);
1529 if (time_after_eq(jiffies, timeout) )
1530 printk("scsi%d: timeout at NCR5380.c:%d\n",
1531 host->host_no, __LINE__);
1533 #else /* NCR_TIMEOUT */
1534 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1535 #endif
1537 NCR5380_write(MODE_REG, MR_BASE);
1538 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1540 #else
1541 #if (NDEBUG & NDEBUG_INTR)
1542 printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1543 #endif
1544 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1545 #endif
1547 } /* if BASR_IRQ */
1548 if (!done)
1549 run_main();
1550 } /* if (instance->irq == irq) */
1551 } while (!done);
1555 static void do_NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs) {
1556 unsigned long flags;
1558 spin_lock_irqsave(&io_request_lock, flags);
1559 NCR5380_intr(irq, dev_id, regs);
1560 spin_unlock_irqrestore(&io_request_lock, flags);
1563 #endif
1565 #ifdef NCR5380_STATS
1566 static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd) {
1567 #ifdef NCR5380_STAT_LIMIT
1568 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1569 #endif
1570 switch (cmd->cmnd[0]) {
1571 case WRITE:
1572 case WRITE_6:
1573 case WRITE_10:
1574 hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1575 /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen; */
1576 hostdata->pendingw--;
1577 break;
1578 case READ:
1579 case READ_6:
1580 case READ_10:
1581 hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1582 /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen; */
1583 hostdata->pendingr--;
1584 break;
1587 #endif
1589 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1590 * int tag);
1592 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1593 * including ARBITRATION, SELECTION, and initial message out for
1594 * IDENTIFY and queue messages.
1596 * Inputs : instance - instantiation of the 5380 driver on which this
1597 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1598 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1599 * the command that is presently connected.
1601 * Returns : -1 if selection could not execute for some reason,
1602 * 0 if selection succeeded or failed because the target
1603 * did not respond.
1605 * Side effects :
1606 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1607 * with registers as they should have been on entry - ie
1608 * SELECT_ENABLE will be set appropriately, the NCR5380
1609 * will cease to drive any SCSI bus signals.
1611 * If successful : I_T_L or I_T_L_Q nexus will be established,
1612 * instance->connected will be set to cmd.
1613 * SELECT interrupt will be disabled.
1615 * If failed (no target) : cmd->scsi_done() will be called, and the
1616 * cmd->result host byte set to DID_BAD_TARGET.
1618 static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag) {
1619 NCR5380_local_declare();
1620 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1621 unsigned char tmp[3], phase;
1622 unsigned char *data;
1623 int len;
1624 unsigned long timeout;
1625 unsigned long flags;
1626 #ifdef USLEEP
1627 unsigned char value;
1628 #endif
1630 NCR5380_setup(instance);
1632 #ifdef USLEEP
1634 if (hostdata->selecting)
1636 goto part2; /* RvC: sorry prof. Dijkstra, but it keeps the
1637 rest of the code nearly the same */
1639 #endif
1641 hostdata->restart_select = 0;
1642 #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION)
1643 NCR5380_print(instance);
1644 printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
1645 instance->this_id);
1646 #endif
1647 save_flags(flags);
1648 cli();
1651 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1652 * data bus during SELECTION.
1655 NCR5380_write(TARGET_COMMAND_REG, 0);
1659 * Start arbitration.
1662 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1663 NCR5380_write(MODE_REG, MR_ARBITRATE);
1665 restore_flags(flags);
1667 /* Wait for arbitration logic to complete */
1668 #if NCR_TIMEOUT
1670 unsigned long timeout = jiffies + 2 * NCR_TIMEOUT;
1672 spin_unlock_irq(&io_request_lock);
1674 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1675 && time_before(jiffies,timeout));
1677 spin_lock_irq(&io_request_lock);
1679 if (time_after_eq(jiffies,timeout)) {
1680 printk("scsi: arbitration timeout at %d\n", __LINE__);
1681 NCR5380_write(MODE_REG, MR_BASE);
1682 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1683 return -1;
1686 #else /* NCR_TIMEOUT */
1687 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1688 #endif
1690 #if (NDEBUG & NDEBUG_ARBITRATION)
1691 printk("scsi%d : arbitration complete\n", instance->host_no);
1692 /* Avoid GCC 2.4.5 asm needs to many reloads error */
1693 __asm__("nop");
1694 #endif
1697 * The arbitration delay is 2.2us, but this is a minimum and there is
1698 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1699 * the integral nature of udelay().
1703 udelay(3);
1705 /* Check for lost arbitration */
1706 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1707 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1708 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1709 NCR5380_write(MODE_REG, MR_BASE);
1710 #if (NDEBUG & NDEBUG_ARBITRATION)
1711 printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n",
1712 instance->host_no);
1713 #endif
1714 return -1;
1716 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1718 if (!(hostdata->flags & FLAG_DTC3181E) &&
1719 /* RvC: DTC3181E has some trouble with this
1720 * so we simply removed it. Seems to work with
1721 * only Mustek scanner attached
1723 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST))
1725 NCR5380_write(MODE_REG, MR_BASE);
1726 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1727 #if (NDEBUG & NDEBUG_ARBITRATION)
1728 printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n",
1729 instance->host_no);
1730 #endif
1731 return -1;
1734 * Again, bus clear + bus settle time is 1.2us, however, this is
1735 * a minimum so we'll udelay ceil(1.2)
1738 udelay(2);
1740 #if (NDEBUG & NDEBUG_ARBITRATION)
1741 printk("scsi%d : won arbitration\n", instance->host_no);
1742 #endif
1746 * Now that we have won arbitration, start Selection process, asserting
1747 * the host and target ID's on the SCSI bus.
1750 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1753 * Raise ATN while SEL is true before BSY goes false from arbitration,
1754 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1755 * phase immediately after selection.
1758 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1759 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1760 NCR5380_write(MODE_REG, MR_BASE);
1763 * Reselect interrupts must be turned off prior to the dropping of BSY,
1764 * otherwise we will trigger an interrupt.
1766 NCR5380_write(SELECT_ENABLE_REG, 0);
1769 * The initiator shall then wait at least two deskew delays and release
1770 * the BSY signal.
1772 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1774 /* Reset BSY */
1775 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1776 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1779 * Something weird happens when we cease to drive BSY - looks
1780 * like the board/chip is letting us do another read before the
1781 * appropriate propagation delay has expired, and we're confusing
1782 * a BSY signal from ourselves as the target's response to SELECTION.
1784 * A small delay (the 'C++' frontend breaks the pipeline with an
1785 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1786 * tighter 'C' code breaks and requires this) solves the problem -
1787 * the 1 us delay is arbitrary, and only used because this delay will
1788 * be the same on other platforms and since it works here, it should
1789 * work there.
1791 * wingel suggests that this could be due to failing to wait
1792 * one deskew delay.
1795 udelay(1);
1797 #if (NDEBUG & NDEBUG_SELECTION)
1798 printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
1799 #endif
1802 * The SCSI specification calls for a 250 ms timeout for the actual
1803 * selection.
1806 timeout = jiffies + (250 * HZ / 1000);
1809 * XXX very interesting - we're seeing a bounce where the BSY we
1810 * asserted is being reflected / still asserted (propagation delay?)
1811 * and it's detecting as true. Sigh.
1814 #ifdef USLEEP
1815 hostdata->select_time = 0; /* we count the clock ticks at which we polled */
1816 hostdata->selecting = cmd;
1818 part2:
1819 /* RvC: here we enter after a sleeping period, or immediately after
1820 execution of part 1
1821 we poll only once ech clock tick */
1822 value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
1824 if (!value && (hostdata->select_time < 25))
1826 /* RvC: we still must wait for a device response */
1827 hostdata->select_time++; /* after 25 ticks the device has failed */
1828 hostdata->time_expires = jiffies + 1;
1829 NCR5380_set_timer(instance);
1830 return 0; /* RvC: we return here with hostdata->selecting set,
1831 to go to sleep */
1834 hostdata->selecting = 0; /* clear this pointer, because we passed the
1835 waiting period */
1836 #else
1837 spin_unlock_irq(&io_request_lock);
1838 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) &
1839 (SR_BSY | SR_IO)));
1840 spin_lock_irq(&io_request_lock);
1841 #endif
1842 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1843 (SR_SEL | SR_IO)) {
1844 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1845 NCR5380_reselect(instance);
1846 printk("scsi%d : reselection after won arbitration?\n",
1847 instance->host_no);
1848 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1849 return -1;
1852 * No less than two deskew delays after the initiator detects the
1853 * BSY signal is true, it shall release the SEL signal and may
1854 * change the DATA BUS. -wingel
1857 udelay(1);
1859 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1861 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1862 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1863 if (hostdata->targets_present & (1 << cmd->target)) {
1864 printk("scsi%d : weirdness\n", instance->host_no);
1865 if (hostdata->restart_select)
1866 printk("\trestart select\n");
1867 #if (NDEBUG & NDEBUG_SELECTION)
1868 NCR5380_print(instance);
1869 #endif
1870 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1871 return -1;
1873 cmd->result = DID_BAD_TARGET << 16;
1874 #ifdef NCR5380_STATS
1875 collect_stats(hostdata, cmd);
1876 #endif
1877 cmd->scsi_done(cmd);
1878 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1879 #if (NDEBUG & NDEBUG_SELECTION)
1880 printk("scsi%d : target did not respond within 250ms\n",
1881 instance->host_no);
1882 #endif
1883 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1884 return 0;
1886 hostdata->targets_present |= (1 << cmd->target);
1889 * Since we followed the SCSI spec, and raised ATN while SEL
1890 * was true but before BSY was false during selection, the information
1891 * transfer phase should be a MESSAGE OUT phase so that we can send the
1892 * IDENTIFY message.
1894 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1895 * message (2 bytes) with a tag ID that we increment with every command
1896 * until it wraps back to 0.
1898 * XXX - it turns out that there are some broken SCSI-II devices,
1899 * which claim to support tagged queuing but fail when more than
1900 * some number of commands are issued at once.
1903 /* Wait for start of REQ/ACK handshake */
1904 #ifdef NCR_TIMEOUT
1906 unsigned long timeout = jiffies + NCR_TIMEOUT;
1908 spin_unlock_irq(&io_request_lock);
1909 while (!(NCR5380_read(STATUS_REG) & SR_REQ) && time_before(jiffies, timeout));
1910 spin_lock_irq(&io_request_lock);
1912 if (time_after_eq(jiffies, timeout)) {
1913 printk("scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1914 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1915 return -1;
1918 #else /* NCR_TIMEOUT */
1919 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1920 #endif /* def NCR_TIMEOUT */
1922 #if (NDEBUG & NDEBUG_SELECTION)
1923 printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1924 instance->host_no, cmd->target);
1925 #endif
1926 tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
1927 #ifdef SCSI2
1928 if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
1929 tmp[1] = SIMPLE_QUEUE_TAG;
1930 if (tag == TAG_NEXT) {
1931 /* 0 is TAG_NONE, used to imply no tag for this command */
1932 if (cmd->device->current_tag == 0)
1933 cmd->device->current_tag = 1;
1935 cmd->tag = cmd->device->current_tag;
1936 cmd->device->current_tag++;
1937 } else
1938 cmd->tag = (unsigned char) tag;
1940 tmp[2] = cmd->tag;
1941 hostdata->last_message = SIMPLE_QUEUE_TAG;
1942 len = 3;
1943 } else
1944 #endif /* def SCSI2 */
1946 len = 1;
1947 cmd->tag = 0;
1950 /* Send message(s) */
1951 data = tmp;
1952 phase = PHASE_MSGOUT;
1953 NCR5380_transfer_pio(instance, &phase, &len, &data);
1954 #if (NDEBUG & NDEBUG_SELECTION)
1955 printk("scsi%d : nexus established.\n", instance->host_no);
1956 #endif
1957 /* XXX need to handle errors here */
1958 hostdata->connected = cmd;
1959 #ifdef SCSI2
1960 if (!cmd->device->tagged_queue)
1961 #endif
1962 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1964 initialize_SCp(cmd);
1967 return 0;
1971 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1972 * unsigned char *phase, int *count, unsigned char **data)
1974 * Purpose : transfers data in given phase using polled I/O
1976 * Inputs : instance - instance of driver, *phase - pointer to
1977 * what phase is expected, *count - pointer to number of
1978 * bytes to transfer, **data - pointer to data pointer.
1980 * Returns : -1 when different phase is entered without transferring
1981 * maximum number of bytes, 0 if all bytes or transfered or exit
1982 * is in same phase.
1984 * Also, *phase, *count, *data are modified in place.
1986 * XXX Note : handling for bus free may be useful.
1990 * Note : this code is not as quick as it could be, however it
1991 * IS 100% reliable, and for the actual data transfer where speed
1992 * counts, we will always do a pseudo DMA or DMA transfer.
1995 static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1996 unsigned char *phase, int *count, unsigned char **data) {
1997 NCR5380_local_declare();
1998 register unsigned char p = *phase, tmp;
1999 register int c = *count;
2000 register unsigned char *d = *data;
2001 #ifdef USLEEP
2003 * RvC: some administrative data to process polling time
2005 int break_allowed = 0;
2006 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2007 #endif
2008 NCR5380_setup(instance);
2010 #if (NDEBUG & NDEBUG_PIO)
2011 if (!(p & SR_IO))
2012 printk("scsi%d : pio write %d bytes\n", instance->host_no, c);
2013 else
2014 printk("scsi%d : pio read %d bytes\n", instance->host_no, c);
2015 #endif
2018 * The NCR5380 chip will only drive the SCSI bus when the
2019 * phase specified in the appropriate bits of the TARGET COMMAND
2020 * REGISTER match the STATUS REGISTER
2023 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
2025 #ifdef USLEEP
2026 /* RvC: don't know if this is necessary, but other SCSI I/O is short
2027 * so breaks are not necessary there
2029 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT))
2031 break_allowed = 1;
2033 #endif
2036 do {
2038 * Wait for assertion of REQ, after which the phase bits will be
2039 * valid
2042 #ifdef USLEEP
2043 /* RvC: we simply poll once, after that we stop temporarily
2044 * and let the device buffer fill up
2045 * if breaking is not allowed, we keep polling as long as needed
2048 while ( !((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) &&
2049 !break_allowed );
2050 if (!(tmp & SR_REQ))
2052 /* timeout condition */
2053 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2054 NCR5380_set_timer (instance);
2055 break;
2057 #else
2058 while ( !((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) );
2059 #endif
2061 #if (NDEBUG & NDEBUG_HANDSHAKE)
2062 printk("scsi%d : REQ detected\n", instance->host_no);
2063 #endif
2065 /* Check for phase mismatch */
2066 if ((tmp & PHASE_MASK) != p) {
2067 #if (NDEBUG & NDEBUG_PIO)
2068 printk("scsi%d : phase mismatch\n", instance->host_no);
2069 NCR5380_print_phase(instance);
2070 #endif
2071 break;
2073 /* Do actual transfer from SCSI bus to / from memory */ if (!(p & SR_IO))
2074 NCR5380_write(OUTPUT_DATA_REG, *d);
2075 else
2076 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
2078 ++d;
2081 * The SCSI standard suggests that in MSGOUT phase, the initiator
2082 * should drop ATN on the last byte of the message phase
2083 * after REQ has been asserted for the handshake but before
2084 * the initiator raises ACK.
2087 if (!(p & SR_IO)) {
2088 if (!((p & SR_MSG) && c > 1)) {
2089 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2090 ICR_ASSERT_DATA);
2091 #if (NDEBUG & NDEBUG_PIO)
2092 NCR5380_print(instance);
2093 #endif
2094 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2095 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
2096 } else {
2097 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2098 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
2099 #if (NDEBUG & NDEBUG_PIO)
2100 NCR5380_print(instance);
2101 #endif
2102 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2103 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
2105 } else {
2106 #if (NDEBUG & NDEBUG_PIO)
2107 NCR5380_print(instance);
2108 #endif
2109 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2112 while (NCR5380_read(STATUS_REG) & SR_REQ);
2114 #if (NDEBUG & NDEBUG_HANDSHAKE)
2115 printk("scsi%d : req false, handshake complete\n", instance->host_no);
2116 #endif
2119 * We have several special cases to consider during REQ/ACK handshaking :
2120 * 1. We were in MSGOUT phase, and we are on the last byte of the
2121 * message. ATN must be dropped as ACK is dropped.
2123 * 2. We are in a MSGIN phase, and we are on the last byte of the
2124 * message. We must exit with ACK asserted, so that the calling
2125 * code may raise ATN before dropping ACK to reject the message.
2127 * 3. ACK and ATN are clear and the target may proceed as normal.
2129 if (!(p == PHASE_MSGIN && c == 1)) {
2130 if (p == PHASE_MSGOUT && c > 1)
2131 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2132 else
2133 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2135 } while (--c);
2137 #if (NDEBUG & NDEBUG_PIO)
2138 printk("scsi%d : residual %d\n", instance->host_no, c);
2139 #endif
2141 *count = c;
2142 *data = d;
2143 tmp = NCR5380_read(STATUS_REG);
2144 if (tmp & SR_REQ)
2145 *phase = tmp & PHASE_MASK;
2146 else
2147 *phase = PHASE_UNKNOWN;
2149 if (!c || (*phase == p))
2150 return 0;
2151 else
2152 return -1;
2155 static void do_reset(struct Scsi_Host *host) {
2156 unsigned long flags;
2157 NCR5380_local_declare();
2158 NCR5380_setup(host);
2160 save_flags(flags);
2161 cli();
2162 NCR5380_write(TARGET_COMMAND_REG,
2163 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
2164 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2165 udelay(25);
2166 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2167 restore_flags(flags);
2168 } /*
2170 * Function : do_abort (Scsi_Host *host)
2172 * Purpose : abort the currently established nexus. Should only be
2173 * called from a routine which can drop into a
2175 * Returns : 0 on success, -1 on failure.
2176 */ static int do_abort(struct Scsi_Host *host) {
2177 NCR5380_local_declare();
2178 unsigned char tmp, *msgptr, phase;
2179 int len;
2180 NCR5380_setup(host);
2183 /* Request message out phase */
2184 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2187 * Wait for the target to indicate a valid phase by asserting
2188 * REQ. Once this happens, we'll have either a MSGOUT phase
2189 * and can immediately send the ABORT message, or we'll have some
2190 * other phase and will have to source/sink data.
2192 * We really don't care what value was on the bus or what value
2193 * the target sees, so we just handshake.
2196 while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
2198 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2200 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
2201 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2202 ICR_ASSERT_ACK);
2203 while (NCR5380_read(STATUS_REG) & SR_REQ);
2204 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2206 tmp = ABORT;
2207 msgptr = &tmp;
2208 len = 1;
2209 phase = PHASE_MSGOUT;
2210 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
2213 * If we got here, and the command completed successfully,
2214 * we're about to go into bus free state.
2217 return len ? -1 : 0;
2220 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
2222 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
2223 * unsigned char *phase, int *count, unsigned char **data)
2225 * Purpose : transfers data in given phase using either real
2226 * or pseudo DMA.
2228 * Inputs : instance - instance of driver, *phase - pointer to
2229 * what phase is expected, *count - pointer to number of
2230 * bytes to transfer, **data - pointer to data pointer.
2232 * Returns : -1 when different phase is entered without transferring
2233 * maximum number of bytes, 0 if all bytes or transfered or exit
2234 * is in same phase.
2236 * Also, *phase, *count, *data are modified in place.
2241 static int NCR5380_transfer_dma(struct Scsi_Host *instance,
2242 unsigned char *phase, int *count, unsigned char **data) {
2243 NCR5380_local_declare();
2244 register int c = *count;
2245 register unsigned char p = *phase;
2246 register unsigned char *d = *data;
2247 unsigned char tmp;
2248 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2249 unsigned long flags;
2250 #endif
2251 int foo;
2252 #if defined(REAL_DMA_POLL)
2253 int cnt, toPIO;
2254 unsigned char saved_data = 0, overrun = 0, residue;
2255 #endif
2257 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2258 instance->hostdata;
2260 NCR5380_setup(instance);
2262 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
2263 *phase = tmp;
2264 return -1;
2266 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
2267 #ifdef READ_OVERRUNS if (p & SR_IO) { c -= 2;
2269 #endif
2270 #if (NDEBUG & NDEBUG_DMA)
2271 printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
2272 instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
2273 "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
2274 #endif
2275 hostdata->dma_len = (p & SR_IO) ?
2276 NCR5380_dma_read_setup(instance, d, c) :
2277 NCR5380_dma_write_setup(instance, d, c);
2278 #endif
2280 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
2282 #ifdef REAL_DMA
2283 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
2284 #elif defined(REAL_DMA_POLL)
2285 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2286 #else
2288 * Note : on my sample board, watch-dog timeouts occurred when interrupts
2289 * were not disabled for the duration of a single DMA transfer, from
2290 * before the setting of DMA mode to after transfer of the last byte.
2293 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2294 save_flags(flags);
2295 cli();
2296 #endif
2297 /* KLL May need eop and parity in 53c400 */
2298 if (hostdata->flags & FLAG_NCR53C400)
2299 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK
2300 | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE
2301 | MR_MONITOR_BSY);
2302 else
2303 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2304 #endif /* def REAL_DMA */
2306 #if (NDEBUG & NDEBUG_DMA) & 0
2307 printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
2308 #endif
2311 * FOO stuff. For some UNAPPARENT reason, I'm getting
2312 * watchdog timers fired on bootup for NO APPARENT REASON, meaning it's
2313 * probably a timing problem.
2315 * Since this is the only place I have back-to-back writes, perhaps this
2316 * is the problem?
2319 if (p & SR_IO)
2321 #ifndef FOO
2322 udelay(1);
2323 #endif
2324 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2325 } else {
2326 #ifndef FOO
2327 udelay(1);
2328 #endif
2329 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2330 #ifndef FOO
2331 udelay(1);
2332 #endif
2333 NCR5380_write(START_DMA_SEND_REG, 0);
2334 #ifndef FOO
2335 udelay(1);
2336 #endif
2339 #if defined(REAL_DMA_POLL)
2340 do {
2341 tmp = NCR5380_read(BUS_AND_STATUS_REG);
2342 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR |
2344 BASR_END_DMA_TRANSFER)));
2347 At this point, either we've completed DMA, or we have a phase mismatch,
2348 or we've unexpectedly lost BUSY (which is a real error).
2350 For write DMAs, we want to wait until the last byte has been
2351 transferred out over the bus before we turn off DMA mode. Alas, there
2352 seems to be no terribly good way of doing this on a 5380 under all
2353 conditions. For non-scatter-gather operations, we can wait until REQ
2354 and ACK both go false, or until a phase mismatch occurs. Gather-writes
2355 are nastier, since the device will be expecting more data than we
2356 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
2357 could test LAST BIT SENT to assure transfer (I imagine this is precisely
2358 why this signal was added to the newer chips) but on the older 538[01]
2359 this signal does not exist. The workaround for this lack is a watchdog;
2360 we bail out of the wait-loop after a modest amount of wait-time if
2361 the usual exit conditions are not met. Not a terribly clean or
2362 correct solution :-%
2364 Reads are equally tricky due to a nasty characteristic of the NCR5380.
2365 If the chip is in DMA mode for an READ, it will respond to a target's
2366 REQ by latching the SCSI data into the INPUT DATA register and asserting
2367 ACK, even if it has _already_ been notified by the DMA controller that
2368 the current DMA transfer has completed! If the NCR5380 is then taken
2369 out of DMA mode, this already-acknowledged byte is lost.
2371 This is not a problem for "one DMA transfer per command" reads, because
2372 the situation will never arise... either all of the data is DMA'ed
2373 properly, or the target switches to MESSAGE IN phase to signal a
2374 disconnection (either operation bringing the DMA to a clean halt).
2375 However, in order to handle scatter-reads, we must work around the
2376 problem. The chosen fix is to DMA N-2 bytes, then check for the
2377 condition before taking the NCR5380 out of DMA mode. One or two extra
2378 bytes are transferred via PIO as necessary to fill out the original
2379 request.
2382 if (p & SR_IO) {
2383 #ifdef READ_OVERRUNS
2384 udelay(10);
2385 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
2386 (BASR_PHASE_MATCH | BASR_ACK))) {
2387 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
2388 overrun = 1;
2390 #endif
2391 } else {
2392 int limit = 100;
2393 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
2394 (NCR5380_read(STATUS_REG) & SR_REQ)) {
2395 if (!(tmp & BASR_PHASE_MATCH))
2396 break;
2397 if (--limit < 0)
2398 break;
2403 #if (NDEBUG & NDEBUG_DMA)
2404 printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
2405 instance->host_no, tmp, NCR5380_read(STATUS_REG));
2406 #endif
2408 NCR5380_write(MODE_REG, MR_BASE);
2409 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2411 residue = NCR5380_dma_residual(instance);
2412 c -= residue;
2413 *count -= c;
2414 *data += c;
2415 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2417 #ifdef READ_OVERRUNS
2418 if (*phase == p && (p & SR_IO) && residue == 0)
2420 if (overrun) {
2421 #if (NDEBUG & NDEBUG_DMA)
2422 printk("Got an input overrun, using saved byte\n");
2423 #endif
2424 **data = saved_data;
2425 *data += 1;
2426 *count -= 1;
2427 cnt = toPIO = 1;
2428 } else {
2429 printk("No overrun??\n");
2430 cnt = toPIO = 2;
2432 #if (NDEBUG & NDEBUG_DMA)
2433 printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
2434 #endif
2435 NCR5380_transfer_pio(instance, phase, &cnt, data);
2436 *count -= toPIO - cnt;
2438 #endif
2440 #if (NDEBUG & NDEBUG_DMA)
2441 printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
2442 *data, *count, *(*data + *count - 1), *(*data + *count));
2443 #endif
2444 return 0;
2446 #elif defined(REAL_DMA)
2447 return 0;
2448 #else /* defined(REAL_DMA_POLL) */
2449 if (p & SR_IO) {
2450 #ifdef DMA_WORKS_RIGHT
2451 foo = NCR5380_pread(instance, d, c);
2452 #else
2453 int diff = 1;
2454 if (hostdata->flags & FLAG_NCR53C400) {
2455 diff = 0;
2457 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2459 * We can't disable DMA mode after successfully transferring
2460 * what we plan to be the last byte, since that would open up
2461 * a race condition where if the target asserted REQ before
2462 * we got the DMA mode reset, the NCR5380 would have latched
2463 * an additional byte into the INPUT DATA register and we'd
2464 * have dropped it.
2466 * The workaround was to transfer one fewer bytes than we
2467 * intended to with the pseudo-DMA read function, wait for
2468 * the chip to latch the last byte, read it, and then disable
2469 * pseudo-DMA mode.
2471 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2472 * REQ is deasserted when ACK is asserted, and not reasserted
2473 * until ACK goes false. Since the NCR5380 won't lower ACK
2474 * until DACK is asserted, which won't happen unless we twiddle
2475 * the DMA port or we take the NCR5380 out of DMA mode, we
2476 * can guarantee that we won't handshake another extra
2477 * byte.
2480 if (!(hostdata->flags & FLAG_NCR53C400)) {
2481 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2482 /* Wait for clean handshake */
2483 while (NCR5380_read(STATUS_REG) & SR_REQ);
2484 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2487 #endif
2488 } else {
2489 #ifdef DMA_WORKS_RIGHT
2490 foo = NCR5380_pwrite(instance, d, c);
2491 #else
2492 int timeout;
2493 #if (NDEBUG & NDEBUG_C400_PWRITE)
2494 printk("About to pwrite %d bytes\n", c);
2495 #endif
2496 if (!(foo = NCR5380_pwrite(instance, d, c))) {
2498 * Wait for the last byte to be sent. If REQ is being asserted for
2499 * the byte we're interested, we'll ACK it and it will go false.
2501 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2502 timeout = 20000;
2503 #if 1
2504 #if 1
2505 while (!(NCR5380_read(BUS_AND_STATUS_REG) &
2506 BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
2507 BASR_PHASE_MATCH));
2508 #else
2509 if (NCR5380_read(STATUS_REG) & SR_REQ) {
2510 for (; timeout &&
2511 !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2512 --timeout);
2513 for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
2514 --timeout);
2516 #endif
2519 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2520 if (!timeout)
2521 printk("scsi%d : timed out on last byte\n",
2522 instance->host_no);
2523 #endif
2526 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2527 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2528 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2529 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2530 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2531 printk("scsi%d : last bit sent works\n",
2532 instance->host_no);
2533 #endif
2536 } else {
2537 #if (NDEBUG & NDEBUG_C400_PWRITE)
2538 printk("Waiting for LASTBYTE\n");
2539 #endif
2540 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2541 #if (NDEBUG & NDEBUG_C400_PWRITE)
2542 printk("Got LASTBYTE\n");
2543 #endif
2545 #else
2546 udelay(5);
2547 #endif
2549 #endif
2551 NCR5380_write(MODE_REG, MR_BASE);
2552 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2554 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2555 #if (NDEBUG & NDEBUG_C400_PWRITE)
2556 printk("53C400w: Checking for IRQ\n");
2557 #endif
2558 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2559 #if (NDEBUG & NDEBUG_C400_PWRITE)
2560 printk("53C400w: got it, reading reset interrupt reg\n");
2561 #endif
2562 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2563 } else {
2564 printk("53C400w: IRQ NOT THERE!\n");
2567 *data = d + c;
2568 *count = 0;
2569 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2570 #if 0
2571 NCR5380_print_phase(instance);
2572 #endif
2573 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2574 restore_flags(flags);
2575 #endif /* defined(REAL_DMA_POLL) */
2576 return foo;
2577 #endif /* def REAL_DMA */
2579 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2582 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2584 * Purpose : run through the various SCSI phases and do as the target
2585 * directs us to. Operates on the currently connected command,
2586 * instance->connected.
2588 * Inputs : instance, instance for which we are doing commands
2590 * Side effects : SCSI things happen, the disconnected queue will be
2591 * modified if a command disconnects, *instance->connected will
2592 * change.
2594 * XXX Note : we need to watch for bus free or a reset condition here
2595 * to recover from an unexpected bus free condition.
2598 static void NCR5380_information_transfer(struct Scsi_Host *instance) {
2599 NCR5380_local_declare();
2600 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2601 instance->hostdata;
2602 unsigned char msgout = NOP;
2603 int sink = 0;
2604 int len;
2605 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2606 int transfersize;
2607 #endif
2608 unsigned char *data;
2609 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
2610 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2611 #ifdef USLEEP
2612 /* RvC: we need to set the end of the polling time */
2613 unsigned long poll_time = jiffies + USLEEP_POLL;
2614 #endif
2616 NCR5380_setup(instance);
2618 while (1) {
2619 tmp = NCR5380_read(STATUS_REG);
2620 /* We only have a valid SCSI phase when REQ is asserted */
2621 if (tmp & SR_REQ) {
2622 phase = (tmp & PHASE_MASK);
2623 if (phase != old_phase) {
2624 old_phase = phase;
2625 #if (NDEBUG & NDEBUG_INFORMATION)
2626 NCR5380_print_phase(instance);
2627 #endif
2629 if (sink && (phase != PHASE_MSGOUT)) {
2630 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2632 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2633 ICR_ASSERT_ACK);
2634 while (NCR5380_read(STATUS_REG) & SR_REQ);
2635 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2636 ICR_ASSERT_ATN);
2637 sink = 0;
2638 continue;
2640 switch (phase) {
2641 case PHASE_DATAIN:
2642 case PHASE_DATAOUT:
2643 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2644 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
2645 instance->host_no);
2646 sink = 1;
2647 do_abort(instance);
2648 cmd->result = DID_ERROR << 16;
2649 cmd->done(cmd);
2650 return;
2651 #endif
2653 * If there is no room left in the current buffer in the
2654 * scatter-gather list, move onto the next one.
2657 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2658 ++cmd->SCp.buffer;
2659 --cmd->SCp.buffers_residual;
2660 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2661 cmd->SCp.ptr = cmd->SCp.buffer->address;
2662 #if (NDEBUG & NDEBUG_INFORMATION)
2663 printk("scsi%d : %d bytes and %d buffers left\n",
2664 instance->host_no, cmd->SCp.this_residual,
2665 cmd->SCp.buffers_residual);
2666 #endif
2669 * The preferred transfer method is going to be
2670 * PSEUDO-DMA for systems that are strictly PIO,
2671 * since we can let the hardware do the handshaking.
2673 * For this to work, we need to know the transfersize
2674 * ahead of time, since the pseudo-DMA code will sit
2675 * in an unconditional loop.
2678 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2679 /* KLL
2680 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2681 * driver then it will always be defined, so the
2682 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2683 * NCR5380 case. I think this is a fairly clean solution.
2684 * We supplement these 2 if's with the flag.
2686 #ifdef NCR5380_dma_xfer_len
2687 if (!cmd->device->borken &&
2688 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2689 (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2690 #else
2691 transfersize = cmd->transfersize;
2693 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2694 if (transfersize > 512)
2695 transfersize = 512;
2696 #endif /* LIMIT_TRANSFERSIZE */
2698 if (!cmd->device->borken && transfersize &&
2699 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2700 cmd->SCp.this_residual && !(cmd->SCp.this_residual %
2701 transfersize)) {
2702 /* Limit transfers to 32K, for xx400 & xx406
2703 * pseudoDMA that transfers in 128 bytes blocks. */
2704 if (transfersize > 32 * 1024)
2705 transfersize = 32 * 1024;
2706 #endif
2707 len = transfersize;
2708 if (NCR5380_transfer_dma(instance, &phase,
2709 &len, (unsigned char **) &cmd->SCp.ptr)) {
2711 * If the watchdog timer fires, all future accesses to this
2712 * device will use the polled-IO.
2714 printk("scsi%d : switching target %d lun %d to slow handshake\n",
2715 instance->host_no, cmd->target, cmd->lun);
2716 cmd->device->borken = 1;
2717 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2718 ICR_ASSERT_ATN);
2719 sink = 1;
2720 do_abort(instance);
2721 cmd->result = DID_ERROR << 16;
2722 cmd->done(cmd);
2723 /* XXX - need to source or sink data here, as appropriate */
2724 } else
2725 cmd->SCp.this_residual -= transfersize - len;
2726 } else
2727 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2728 NCR5380_transfer_pio(instance, &phase,
2729 (int *) &cmd->SCp.this_residual, (unsigned char **)
2730 &cmd->SCp.ptr);
2731 break;
2732 case PHASE_MSGIN:
2733 len = 1;
2734 data = &tmp;
2735 NCR5380_transfer_pio(instance, &phase, &len, &data);
2736 cmd->SCp.Message = tmp;
2738 switch (tmp) {
2740 * Linking lets us reduce the time required to get the
2741 * next command out to the device, hopefully this will
2742 * mean we don't waste another revolution due to the delays
2743 * required by ARBITRATION and another SELECTION.
2745 * In the current implementation proposal, low level drivers
2746 * merely have to start the next command, pointed to by
2747 * next_link, done() is called as with unlinked commands.
2749 #ifdef LINKED
2750 case LINKED_CMD_COMPLETE:
2751 case LINKED_FLG_CMD_COMPLETE:
2752 /* Accept message by clearing ACK */
2753 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2755 #if (NDEBUG & NDEBUG_LINKED)
2756 printk("scsi%d : target %d lun %d linked command complete.\n",
2757 instance->host_no, cmd->target, cmd->lun);
2758 #endif
2760 * Sanity check : A linked command should only terminate with
2761 * one of these messages if there are more linked commands
2762 * available.
2765 if (!cmd->next_link) {
2766 printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
2767 instance->host_no, cmd->target, cmd->lun);
2768 sink = 1;
2769 do_abort(instance);
2770 return;
2772 initialize_SCp(cmd->next_link);
2773 /* The next command is still part of this process */
2774 cmd->next_link->tag = cmd->tag;
2775 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2776 #if (NDEBUG & NDEBUG_LINKED)
2777 printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
2778 instance->host_no, cmd->target, cmd->lun);
2779 #endif
2780 #ifdef NCR5380_STATS
2781 collect_stats(hostdata, cmd);
2782 #endif
2783 cmd->scsi_done(cmd);
2784 cmd = hostdata->connected;
2785 break;
2786 #endif /* def LINKED */
2787 case ABORT:
2788 case COMMAND_COMPLETE:
2789 /* Accept message by clearing ACK */
2790 sink = 1;
2791 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2792 hostdata->connected = NULL;
2793 #if (NDEBUG & NDEBUG_QUEUES)
2794 printk("scsi%d : command for target %d, lun %d completed\n",
2795 instance->host_no, cmd->target, cmd->lun);
2796 #endif
2797 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2800 * I'm not sure what the correct thing to do here is :
2802 * If the command that just executed is NOT a request
2803 * sense, the obvious thing to do is to set the result
2804 * code to the values of the stored parameters.
2806 * If it was a REQUEST SENSE command, we need some way
2807 * to differentiate between the failure code of the original
2808 * and the failure code of the REQUEST sense - the obvious
2809 * case is success, where we fall through and leave the result
2810 * code unchanged.
2812 * The non-obvious place is where the REQUEST SENSE failed
2815 if (cmd->cmnd[0] != REQUEST_SENSE)
2816 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2817 else if (cmd->SCp.Status != GOOD)
2818 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2820 #ifdef AUTOSENSE
2821 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2822 (cmd->SCp.Status == CHECK_CONDITION)) {
2823 unsigned long flags;
2824 #if (NDEBUG & NDEBUG_AUTOSENSE)
2825 printk("scsi%d : performing request sense\n",
2826 instance->host_no);
2827 #endif
2828 cmd->cmnd[0] = REQUEST_SENSE;
2829 cmd->cmnd[1] &= 0xe0;
2830 cmd->cmnd[2] = 0;
2831 cmd->cmnd[3] = 0;
2832 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2833 cmd->cmnd[5] = 0;
2835 cmd->SCp.buffer = NULL;
2836 cmd->SCp.buffers_residual = 0;
2837 cmd->SCp.ptr = (char *) cmd->sense_buffer;
2838 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2840 save_flags(flags);
2841 cli();
2842 LIST(cmd, hostdata->issue_queue);
2843 cmd->host_scribble = (unsigned char *)
2844 hostdata->issue_queue;
2845 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2846 restore_flags(flags);
2847 #if (NDEBUG & NDEBUG_QUEUES)
2848 printk("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no);
2849 #endif
2850 } else {
2851 #endif /* def AUTOSENSE */
2852 #ifdef NCR5380_STATS
2853 collect_stats(hostdata, cmd);
2854 #endif
2855 cmd->scsi_done(cmd);
2858 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2860 * Restore phase bits to 0 so an interrupted selection,
2861 * arbitration can resume.
2863 NCR5380_write(TARGET_COMMAND_REG, 0);
2865 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2866 barrier();
2867 return;
2868 case MESSAGE_REJECT:
2869 /* Accept message by clearing ACK */
2870 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2871 switch (hostdata->last_message) {
2872 case HEAD_OF_QUEUE_TAG:
2873 case ORDERED_QUEUE_TAG:
2874 case SIMPLE_QUEUE_TAG:
2875 cmd->device->tagged_queue = 0;
2876 hostdata->busy[cmd->target] |= (1 << cmd->lun);
2877 break;
2878 default:
2879 break;
2881 case DISCONNECT: {
2882 unsigned long flags;
2883 /* Accept message by clearing ACK */
2884 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2885 cmd->device->disconnect = 1;
2886 save_flags(flags);
2887 cli();
2888 LIST(cmd, hostdata->disconnected_queue);
2889 cmd->host_scribble = (unsigned char *)
2890 hostdata->disconnected_queue;
2891 hostdata->connected = NULL;
2892 hostdata->disconnected_queue = cmd;
2893 restore_flags(flags);
2894 #if (NDEBUG & NDEBUG_QUEUES)
2895 printk("scsi%d : command for target %d lun %d was moved from connected to"
2896 " the disconnected_queue\n", instance->host_no,
2897 cmd->target, cmd->lun);
2898 #endif
2900 * Restore phase bits to 0 so an interrupted selection,
2901 * arbitration can resume.
2903 NCR5380_write(TARGET_COMMAND_REG, 0);
2905 /* Enable reselect interrupts */
2906 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2907 /* Wait for bus free to avoid nasty timeouts */
2908 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2909 barrier();
2910 #if 0
2911 NCR5380_print_status(instance);
2912 #endif
2913 return;
2916 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2917 * operation, in violation of the SCSI spec so we can safely
2918 * ignore SAVE/RESTORE pointers calls.
2920 * Unfortunately, some disks violate the SCSI spec and
2921 * don't issue the required SAVE_POINTERS message before
2922 * disconnecting, and we have to break spec to remain
2923 * compatible.
2925 case SAVE_POINTERS:
2926 case RESTORE_POINTERS:
2927 /* Accept message by clearing ACK */
2928 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2929 break;
2930 case EXTENDED_MESSAGE:
2932 * Extended messages are sent in the following format :
2933 * Byte
2934 * 0 EXTENDED_MESSAGE == 1
2935 * 1 length (includes one byte for code, doesn't
2936 * include first two bytes)
2937 * 2 code
2938 * 3..length+1 arguments
2940 * Start the extended message buffer with the EXTENDED_MESSAGE
2941 * byte, since print_msg() wants the whole thing.
2943 extended_msg[0] = EXTENDED_MESSAGE;
2944 /* Accept first byte by clearing ACK */
2945 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2947 #if (NDEBUG & NDEBUG_EXTENDED)
2948 printk("scsi%d : receiving extended message\n",
2949 instance->host_no);
2950 #endif
2952 len = 2;
2953 data = extended_msg + 1;
2954 phase = PHASE_MSGIN;
2955 NCR5380_transfer_pio(instance, &phase, &len, &data);
2957 #if (NDEBUG & NDEBUG_EXTENDED)
2958 printk("scsi%d : length=%d, code=0x%02x\n",
2959 instance->host_no, (int) extended_msg[1],
2960 (int) extended_msg[2]);
2961 #endif
2963 if (!len && extended_msg[1] <=
2964 (sizeof(extended_msg) - 1)) {
2965 /* Accept third byte by clearing ACK */
2966 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2967 len = extended_msg[1] - 1;
2968 data = extended_msg + 3;
2969 phase = PHASE_MSGIN;
2971 NCR5380_transfer_pio(instance, &phase, &len, &data);
2973 #if (NDEBUG & NDEBUG_EXTENDED)
2974 printk("scsi%d : message received, residual %d\n",
2975 instance->host_no, len);
2976 #endif
2978 switch (extended_msg[2]) {
2979 case EXTENDED_SDTR:
2980 case EXTENDED_WDTR:
2981 case EXTENDED_MODIFY_DATA_POINTER:
2982 case EXTENDED_EXTENDED_IDENTIFY:
2983 tmp = 0;
2985 } else if (len) {
2986 printk("scsi%d: error receiving extended message\n",
2987 instance->host_no);
2988 tmp = 0;
2989 } else {
2990 printk("scsi%d: extended message code %02x length %d is too long\n",
2991 instance->host_no, extended_msg[2], extended_msg[1]);
2992 tmp = 0;
2994 /* Fall through to reject message */
2997 * If we get something weird that we aren't expecting,
2998 * reject it.
3000 default:
3001 if (!tmp) {
3002 printk("scsi%d: rejecting message ", instance->host_no);
3003 print_msg(extended_msg);
3004 printk("\n");
3005 } else if (tmp != EXTENDED_MESSAGE)
3006 printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n",
3007 instance->host_no, tmp, cmd->target, cmd->lun);
3008 else
3009 printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n",
3010 instance->host_no, extended_msg[1], extended_msg[0], cmd->target, cmd->lun);
3012 msgout = MESSAGE_REJECT;
3013 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
3014 ICR_ASSERT_ATN);
3015 break;
3016 } /* switch (tmp) */
3017 break;
3018 case PHASE_MSGOUT:
3019 len = 1;
3020 data = &msgout;
3021 hostdata->last_message = msgout;
3022 NCR5380_transfer_pio(instance, &phase, &len, &data);
3023 if (msgout == ABORT) {
3024 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
3025 hostdata->connected = NULL;
3026 cmd->result = DID_ERROR << 16;
3027 #ifdef NCR5380_STATS
3028 collect_stats(hostdata, cmd);
3029 #endif
3030 cmd->scsi_done(cmd);
3031 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
3032 return;
3034 msgout = NOP;
3035 break;
3036 case PHASE_CMDOUT:
3037 len = cmd->cmd_len;
3038 data = cmd->cmnd;
3040 * XXX for performance reasons, on machines with a
3041 * PSEUDO-DMA architecture we should probably
3042 * use the dma transfer function.
3044 NCR5380_transfer_pio(instance, &phase, &len,
3045 &data);
3046 #ifdef USLEEP
3047 if (!cmd->device->disconnect &&
3048 should_disconnect(cmd->cmnd[0]))
3050 hostdata->time_expires = jiffies + USLEEP_SLEEP;
3051 #if (NDEBUG & NDEBUG_USLEEP)
3052 printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
3053 hostdata->time_expires);
3054 #endif
3055 NCR5380_set_timer(instance);
3056 return;
3058 #endif /* def USLEEP */
3059 break;
3060 case PHASE_STATIN:
3061 len = 1;
3062 data = &tmp;
3063 NCR5380_transfer_pio(instance, &phase, &len, &data);
3064 cmd->SCp.Status = tmp;
3065 break;
3066 default:
3067 printk("scsi%d : unknown phase\n", instance->host_no);
3068 #ifdef NDEBUG
3069 NCR5380_print(instance);
3070 #endif
3071 } /* switch(phase) */
3072 } /* if (tmp * SR_REQ) */
3073 #ifdef USLEEP
3074 else
3076 /* RvC: go to sleep if polling time expired
3078 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time))
3080 hostdata->time_expires = jiffies + USLEEP_SLEEP;
3081 #if (NDEBUG & NDEBUG_USLEEP)
3082 printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
3083 hostdata->time_expires);
3084 #endif
3085 NCR5380_set_timer(instance);
3086 return;
3089 #endif
3090 } /* while (1) */
3094 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
3096 * Purpose : does reselection, initializing the instance->connected
3097 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
3098 * nexus has been reestablished,
3100 * Inputs : instance - this instance of the NCR5380.
3105 static void NCR5380_reselect(struct Scsi_Host *instance) {
3106 NCR5380_local_declare();
3107 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
3108 instance->hostdata;
3109 unsigned char target_mask;
3110 unsigned char lun, phase;
3111 int len;
3112 #ifdef SCSI2
3113 unsigned char tag;
3114 #endif
3115 unsigned char msg[3];
3116 unsigned char *data;
3117 Scsi_Cmnd *tmp = NULL, *prev;
3118 int abort = 0;
3119 NCR5380_setup(instance);
3122 * Disable arbitration, etc. since the host adapter obviously
3123 * lost, and tell an interrupted NCR5380_select() to restart.
3126 NCR5380_write(MODE_REG, MR_BASE);
3127 hostdata->restart_select = 1;
3129 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
3131 #if (NDEBUG & NDEBUG_RESELECTION)
3132 printk("scsi%d : reselect\n", instance->host_no);
3133 #endif
3136 * At this point, we have detected that our SCSI ID is on the bus,
3137 * SEL is true and BSY was false for at least one bus settle delay
3138 * (400 ns).
3140 * We must assert BSY ourselves, until the target drops the SEL
3141 * signal.
3144 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
3146 while (NCR5380_read(STATUS_REG) & SR_SEL);
3147 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3150 * Wait for target to go into MSGIN.
3153 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
3155 len = 1;
3156 data = msg;
3157 phase = PHASE_MSGIN;
3158 NCR5380_transfer_pio(instance, &phase, &len, &data);
3161 if (!msg[0] & 0x80) {
3162 printk("scsi%d : expecting IDENTIFY message, got ",
3163 instance->host_no);
3164 print_msg(msg);
3165 abort = 1;
3166 } else {
3167 /* Accept message by clearing ACK */
3168 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3169 lun = (msg[0] & 0x07);
3172 * We need to add code for SCSI-II to track which devices have
3173 * I_T_L_Q nexuses established, and which have simple I_T_L
3174 * nexuses so we can chose to do additional data transfer.
3177 #ifdef SCSI2
3178 #error "SCSI-II tagged queueing is not supported yet"
3179 #endif
3182 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
3183 * just reestablished, and remove it from the disconnected queue.
3187 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
3188 tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
3189 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
3190 #ifdef SCSI2
3191 && (tag == tmp->tag)
3192 #endif
3194 if (prev) {
3195 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
3196 prev->host_scribble = tmp->host_scribble;
3197 } else {
3198 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
3199 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
3201 tmp->host_scribble = NULL;
3202 break;
3204 if (!tmp) {
3205 #ifdef SCSI2
3206 printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
3207 instance->host_no, target_mask, lun, tag);
3208 #else
3209 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
3210 instance->host_no, target_mask, lun);
3211 #endif
3213 * Since we have an established nexus that we can't do anything with,
3214 * we must abort it.
3216 abort = 1;
3220 if (abort) {
3221 do_abort(instance);
3222 } else {
3223 hostdata->connected = tmp;
3224 #if (NDEBUG & NDEBUG_RESELECTION)
3225 printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
3226 instance->host_no, tmp->target, tmp->lun, tmp->tag);
3227 #endif
3232 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
3234 * Purpose : called by interrupt handler when DMA finishes or a phase
3235 * mismatch occurs (which would finish the DMA transfer).
3237 * Inputs : instance - this instance of the NCR5380.
3239 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
3240 * nexus has been reestablished, on failure NULL is returned.
3243 #ifdef REAL_DMA
3244 static void NCR5380_dma_complete(NCR5380_instance * instance) {
3245 NCR5380_local_declare();
3246 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *
3247 instance->hostdata);
3248 int transferred;
3249 NCR5380_setup(instance);
3252 * XXX this might not be right.
3254 * Wait for final byte to transfer, ie wait for ACK to go false.
3256 * We should use the Last Byte Sent bit, unfortunately this is
3257 * not available on the 5380/5381 (only the various CMOS chips)
3260 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
3262 NCR5380_write(MODE_REG, MR_BASE);
3263 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
3266 * The only places we should see a phase mismatch and have to send
3267 * data from the same set of pointers will be the data transfer
3268 * phases. So, residual, requested length are only important here.
3271 if (!(hostdata->connected->SCp.phase & SR_CD)) {
3272 transferred = instance->dmalen - NCR5380_dma_residual();
3273 hostdata->connected->SCp.this_residual -= transferred;
3274 hostdata->connected->SCp.ptr += transferred;
3277 #endif /* def REAL_DMA */
3280 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
3282 * Purpose : abort a command
3284 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
3285 * host byte of the result field to, if zero DID_ABORTED is
3286 * used.
3288 * Returns : 0 - success, -1 on failure.
3290 * XXX - there is no way to abort the command that is currently
3291 * connected, you have to wait for it to complete. If this is
3292 * a problem, we could implement longjmp() / setjmp(), setjmp()
3293 * called where the loop started in NCR5380_main().
3296 #ifndef NCR5380_abort
3297 static
3298 #endif
3299 int NCR5380_abort(Scsi_Cmnd * cmd) {
3300 NCR5380_local_declare();
3301 unsigned long flags;
3302 struct Scsi_Host *instance = cmd->host;
3303 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
3304 instance->hostdata;
3305 Scsi_Cmnd *tmp, **prev;
3307 printk("scsi%d : aborting command\n", instance->host_no);
3308 print_Scsi_Cmnd(cmd);
3310 NCR5380_print_status(instance);
3312 printk("scsi%d : aborting command\n", instance->host_no);
3313 print_Scsi_Cmnd(cmd);
3315 NCR5380_print_status(instance);
3317 save_flags(flags);
3318 cli();
3319 NCR5380_setup(instance);
3321 #if (NDEBUG & NDEBUG_ABORT)
3322 printk("scsi%d : abort called\n", instance->host_no);
3323 printk(" basr 0x%X, sr 0x%X\n",
3324 NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
3325 #endif
3327 #if 0
3329 * Case 1 : If the command is the currently executing command,
3330 * we'll set the aborted flag and return control so that
3331 * information transfer routine can exit cleanly.
3334 if (hostdata->connected == cmd) {
3335 #if (NDEBUG & NDEBUG_ABORT)
3336 printk("scsi%d : aborting connected command\n", instance->host_no);
3337 #endif
3338 hostdata->aborted = 1;
3340 * We should perform BSY checking, and make sure we haven't slipped
3341 * into BUS FREE.
3344 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
3346 * Since we can't change phases until we've completed the current
3347 * handshake, we have to source or sink a byte of data if the current
3348 * phase is not MSGOUT.
3352 * Return control to the executing NCR drive so we can clear the
3353 * aborted flag and get back into our main loop.
3356 return 0;
3358 #endif
3361 * Case 2 : If the command hasn't been issued yet, we simply remove it
3362 * from the issue queue.
3364 #if (NDEBUG & NDEBUG_ABORT)
3365 /* KLL */
3366 printk("scsi%d : abort going into loop.\n", instance->host_no);
3367 #endif
3368 for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue),
3369 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
3370 tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp =
3371 (Scsi_Cmnd *) tmp->host_scribble)
3372 if (cmd == tmp) {
3373 REMOVE(5, *prev, tmp, tmp->host_scribble);
3374 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
3375 tmp->host_scribble = NULL;
3376 tmp->result = DID_ABORT << 16;
3377 restore_flags(flags);
3378 #if (NDEBUG & NDEBUG_ABORT)
3379 printk("scsi%d : abort removed command from issue queue.\n",
3380 instance->host_no);
3381 #endif
3382 tmp->done(tmp);
3383 return SCSI_ABORT_SUCCESS;
3385 #if (NDEBUG & NDEBUG_ABORT)
3386 /* KLL */
3387 else if (prev == tmp)
3388 printk("scsi%d : LOOP\n", instance->host_no);
3389 #endif
3392 * Case 3 : If any commands are connected, we're going to fail the abort
3393 * and let the high level SCSI driver retry at a later time or
3394 * issue a reset.
3396 * Timeouts, and therefore aborted commands, will be highly unlikely
3397 * and handling them cleanly in this situation would make the common
3398 * case of noresets less efficient, and would pollute our code. So,
3399 * we fail.
3402 if (hostdata->connected) {
3403 restore_flags(flags);
3404 #if (NDEBUG & NDEBUG_ABORT)
3405 printk("scsi%d : abort failed, command connected.\n", instance->host_no);
3406 #endif
3407 return SCSI_ABORT_NOT_RUNNING;
3410 * Case 4: If the command is currently disconnected from the bus, and
3411 * there are no connected commands, we reconnect the I_T_L or
3412 * I_T_L_Q nexus associated with it, go into message out, and send
3413 * an abort message.
3415 * This case is especially ugly. In order to reestablish the nexus, we
3416 * need to call NCR5380_select(). The easiest way to implement this
3417 * function was to abort if the bus was busy, and let the interrupt
3418 * handler triggered on the SEL for reselect take care of lost arbitrations
3419 * where necessary, meaning interrupts need to be enabled.
3421 * When interrupts are enabled, the queues may change - so we
3422 * can't remove it from the disconnected queue before selecting it
3423 * because that could cause a failure in hashing the nexus if that
3424 * device reselected.
3426 * Since the queues may change, we can't use the pointers from when we
3427 * first locate it.
3429 * So, we must first locate the command, and if NCR5380_select()
3430 * succeeds, then issue the abort, relocate the command and remove
3431 * it from the disconnected queue.
3434 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
3435 tmp = (Scsi_Cmnd *) tmp->host_scribble)
3436 if (cmd == tmp) {
3437 restore_flags(flags);
3438 #if (NDEBUG & NDEBUG_ABORT)
3439 printk("scsi%d : aborting disconnected command.\n", instance->host_no);
3440 #endif
3442 if (NCR5380_select(instance, cmd, (int) cmd->tag))
3443 return SCSI_ABORT_BUSY;
3445 #if (NDEBUG & NDEBUG_ABORT)
3446 printk("scsi%d : nexus reestablished.\n", instance->host_no);
3447 #endif
3449 do_abort(instance);
3451 cli();
3452 for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue),
3453 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
3454 tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp =
3455 (Scsi_Cmnd *) tmp->host_scribble)
3456 if (cmd == tmp) {
3457 REMOVE(5, *prev, tmp, tmp->host_scribble);
3458 *prev = (Scsi_Cmnd *) tmp->host_scribble;
3459 tmp->host_scribble = NULL;
3460 tmp->result = DID_ABORT << 16;
3461 restore_flags(flags);
3462 tmp->done(tmp);
3463 return SCSI_ABORT_SUCCESS;
3467 * Case 5 : If we reached this point, the command was not found in any of
3468 * the queues.
3470 * We probably reached this point because of an unlikely race condition
3471 * between the command completing successfully and the abortion code,
3472 * so we won't panic, but we will notify the user in case something really
3473 * broke.
3476 restore_flags(flags);
3477 printk("scsi%d : warning : SCSI command probably completed successfully\n"
3478 " before abortion\n", instance->host_no);
3479 return SCSI_ABORT_NOT_RUNNING;
3484 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
3486 * Purpose : reset the SCSI bus.
3488 * Returns : SCSI_RESET_WAKEUP
3492 #ifndef NCR5380_reset
3493 static
3494 #endif
3495 int NCR5380_reset(Scsi_Cmnd * cmd, unsigned int dummy) {
3496 NCR5380_local_declare();
3497 NCR5380_setup(cmd->host);
3499 NCR5380_print_status(cmd->host);
3500 do_reset(cmd->host);
3502 return SCSI_RESET_WAKEUP;