2 #define NDEBUG (NDEBUG_RESTART_SELECT | NDEBUG_ABORT)
5 * NCR 5380 generic driver routines. These should make it *trivial*
6 * to implement 5380 SCSI drivers under Linux with a non-trantor
9 * Note that these routines also work with NR53c400 family chips.
11 * Copyright 1993, Drew Eckhardt
13 * (Unix and Linux consulting and custom programming)
17 * DISTRIBUTION RELEASE 6.
19 * For more information, please consult
22 * SCSI Protocol Controller
25 * NCR Microelectronics
26 * 1635 Aeroplaza Drive
27 * Colorado Springs, CO 80916
35 * Revision 1.10 1998/9/2 Alan Cox
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)
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
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 * 4. Test SCSI-II tagged queueing (I have no devices which support
89 * 5. Test linked command handling code after Eric is ready with
90 * the high level code.
93 #if (NDEBUG & NDEBUG_LISTS)
94 #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
95 #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 #define REMOVE(w,x,y,z)
108 #define READ_OVERRUNS
111 #ifdef BOARD_REQUIRES_NO_DELAY
112 #define io_recovery_delay(x)
114 #define io_recovery_delay(x) udelay(x)
121 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
122 * and specifically for each board rather than each chip. This makes their
123 * adaptation to platforms like the Mac (Some of which use NCR5380's)
124 * more difficult than it has to be.
126 * Also, many of the SCSI drivers were written before the command queuing
127 * routines were implemented, meaning their implementations of queued
128 * commands were hacked on rather than designed in from the start.
130 * When I designed the Linux SCSI drivers I figured that
131 * while having two different SCSI boards in a system might be useful
132 * for debugging things, two of the same type wouldn't be used.
133 * Well, I was wrong and a number of users have mailed me about running
134 * multiple high-performance SCSI boards in a server.
136 * Finally, when I get questions from users, I have no idea what
137 * revision of my driver they are running.
139 * This driver attempts to address these problems :
140 * This is a generic 5380 driver. To use it on a different platform,
141 * one simply writes appropriate system specific macros (ie, data
142 * transfer - some PC's will use the I/O bus, 68K's must use
143 * memory mapped) and drops this file in their 'C' wrapper.
145 * As far as command queueing, two queues are maintained for
146 * each 5380 in the system - commands that haven't been issued yet,
147 * and commands that are currently executing. This means that an
148 * unlimited number of commands may be queued, letting
149 * more commands propagate from the higher driver levels giving higher
150 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
151 * allowing multiple commands to propagate all the way to a SCSI-II device
152 * while a command is already executing.
154 * To solve the multiple-boards-in-the-same-system problem,
155 * there is a separate instance structure for each instance
156 * of a 5380 in the system. So, multiple NCR5380 drivers will
157 * be able to coexist with appropriate changes to the high level
160 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
161 * number (updated for each public release) printed by the
162 * NCR5380_print_options command, which should be called from the
163 * wrapper detect function, so that I know what release of the driver
166 * Issues specific to the NCR5380 :
168 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
169 * piece of hardware that requires you to sit in a loop polling for
170 * the REQ signal as long as you are connected. Some devices are
171 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
172 * while doing long seek operations.
174 * The workaround for this is to keep track of devices that have
175 * disconnected. If the device hasn't disconnected, for commands that
176 * should disconnect, we do something like
178 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
180 * Some tweaking of N and M needs to be done. An algorithm based
181 * on "time to data" would give the best results as long as short time
182 * to datas (ie, on the same track) were considered, however these
183 * broken devices are the exception rather than the rule and I'd rather
184 * spend my time optimizing for the normal case.
188 * At the heart of the design is a coroutine, NCR5380_main,
189 * which is started when not running by the interrupt handler,
190 * timer, and queue command function. It attempts to establish
191 * I_T_L or I_T_L_Q nexuses by removing the commands from the
192 * issue queue and calling NCR5380_select() if a nexus
193 * is not established.
195 * Once a nexus is established, the NCR5380_information_transfer()
196 * phase goes through the various phases as instructed by the target.
197 * if the target goes into MSG IN and sends a DISCONNECT message,
198 * the command structure is placed into the per instance disconnected
199 * queue, and NCR5380_main tries to find more work. If the target is
200 * idle for too long, the system will try to sleep.
202 * If a command has disconnected, eventually an interrupt will trigger,
203 * calling NCR5380_intr() which will in turn call NCR5380_reselect
204 * to reestablish a nexus. This will run main if necessary.
206 * On command termination, the done function will be called as
209 * SCSI pointers are maintained in the SCp field of SCSI command
210 * structures, being initialized after the command is connected
211 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
212 * Note that in violation of the standard, an implicit SAVE POINTERS operation
213 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
218 * This file a skeleton Linux SCSI driver for the NCR 5380 series
219 * of chips. To use it, you write an architecture specific functions
220 * and macros and include this file in your driver.
222 * These macros control options :
223 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
226 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
227 * for commands that return with a CHECK CONDITION status.
229 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
232 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
233 * override-configure an IRQ.
235 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
236 * bytes at a time. Since interrupts are disabled by default during
237 * these transfers, we might need this to give reasonable interrupt
238 * service time if the transfer size gets too large.
240 * LINKED - if defined, linked commands are supported.
242 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
244 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
246 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
247 * rely on phase mismatch and EOP interrupts to determine end
250 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
251 * only really want to use this if you're having a problem with
252 * dropped characters during high speed communications, and even
253 * then, you're going to be better off twiddling with transfersize
254 * in the high level code.
256 * Defaults for these will be provided although the user may want to adjust
257 * these to allocate CPU resources to the SCSI driver or "real" code.
259 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
261 * USLEEP_POLL - amount of time, in jiffies, to poll
263 * These macros MUST be defined :
264 * NCR5380_local_declare() - declare any local variables needed for your
267 * NCR5380_setup(instance) - initialize any local variables needed from a given
268 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
270 * NCR5380_read(register) - read from the specified register
272 * NCR5380_write(register, value) - write to the specific register
274 * NCR5380_implementation_fields - additional fields needed for this
275 * specific implementation of the NCR5380
277 * Either real DMA *or* pseudo DMA may be implemented
279 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
280 * Note that the DMA setup functions should return the number of bytes
281 * that they were able to program the controller for.
283 * Also note that generic i386/PC versions of these macros are
284 * available as NCR5380_i386_dma_write_setup,
285 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
287 * NCR5380_dma_write_setup(instance, src, count) - initialize
288 * NCR5380_dma_read_setup(instance, dst, count) - initialize
289 * NCR5380_dma_residual(instance); - residual count
292 * NCR5380_pwrite(instance, src, count)
293 * NCR5380_pread(instance, dst, count);
295 * If nothing specific to this implementation needs doing (ie, with external
296 * hardware), you must also define
298 * NCR5380_queue_command
303 * to be the global entry points into the specific driver, ie
304 * #define NCR5380_queue_command t128_queue_command.
306 * If this is not done, the routines will be defined as static functions
307 * with the NCR5380* names and the user must provide a globally
308 * accessible wrapper function.
310 * The generic driver is initialized by calling NCR5380_init(instance),
311 * after setting the appropriate host specific fields and ID. If the
312 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
313 * possible) function may be used. Before the specific driver initialization
314 * code finishes, NCR5380_print_options should be called.
317 static int do_abort(struct Scsi_Host *host);
318 static void do_reset(struct Scsi_Host *host);
319 static struct Scsi_Host *first_instance = NULL;
320 static Scsi_Host_Template *the_template = NULL;
321 static struct timer_list usleep_timer;
324 * initialize_SCp - init the scsi pointer field
325 * @cmd: command block to set up
327 * Set up the internal fields in the SCSI command.
330 static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
333 * Initialize the Scsi Pointer field so that all of the commands in the
334 * various queues are valid.
338 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
339 cmd->SCp.buffers_residual = cmd->use_sg - 1;
340 cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
341 cmd->SCp.this_residual = cmd->SCp.buffer->length;
343 cmd->SCp.buffer = NULL;
344 cmd->SCp.buffers_residual = 0;
345 cmd->SCp.ptr = (char *) cmd->request_buffer;
346 cmd->SCp.this_residual = cmd->request_bufflen;
350 #include <linux/delay.h>
373 {ICR_ASSERT_RST, "ASSERT RST"},
374 {ICR_ASSERT_ACK, "ASSERT ACK"},
375 {ICR_ASSERT_BSY, "ASSERT BSY"},
376 {ICR_ASSERT_SEL, "ASSERT SEL"},
377 {ICR_ASSERT_ATN, "ASSERT ATN"},
378 {ICR_ASSERT_DATA, "ASSERT DATA"},
382 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
383 {MR_TARGET, "MODE TARGET"},
384 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
385 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
386 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
387 {MR_DMA_MODE, "MODE DMA"},
388 {MR_ARBITRATE, "MODE ARBITRATION"},
393 * NCR5380_print - print scsi bus signals
394 * @instance: adapter state to dump
396 * Print the SCSI bus signals for debugging purposes
401 static void NCR5380_print(struct Scsi_Host *instance)
403 NCR5380_local_declare();
405 unsigned char status, data, basr, mr, icr, i;
406 NCR5380_setup(instance);
407 /* FIXME - this needs proper locking */
410 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
411 status = NCR5380_read(STATUS_REG);
412 mr = NCR5380_read(MODE_REG);
413 icr = NCR5380_read(INITIATOR_COMMAND_REG);
414 basr = NCR5380_read(BUS_AND_STATUS_REG);
415 restore_flags(flags);
416 printk("STATUS_REG: %02x ", status);
417 for (i = 0; signals[i].mask; ++i)
418 if (status & signals[i].mask)
419 printk(",%s", signals[i].name);
420 printk("\nBASR: %02x ", basr);
421 for (i = 0; basrs[i].mask; ++i)
422 if (basr & basrs[i].mask)
423 printk(",%s", basrs[i].name);
424 printk("\nICR: %02x ", icr);
425 for (i = 0; icrs[i].mask; ++i)
426 if (icr & icrs[i].mask)
427 printk(",%s", icrs[i].name);
428 printk("\nMODE: %02x ", mr);
429 for (i = 0; mrs[i].mask; ++i)
430 if (mr & mrs[i].mask)
431 printk(",%s", mrs[i].name);
439 {PHASE_DATAOUT, "DATAOUT"},
440 {PHASE_DATAIN, "DATAIN"},
441 {PHASE_CMDOUT, "CMDOUT"},
442 {PHASE_STATIN, "STATIN"},
443 {PHASE_MSGOUT, "MSGOUT"},
444 {PHASE_MSGIN, "MSGIN"},
445 {PHASE_UNKNOWN, "UNKNOWN"}
449 * NCR5380_print_phase - show SCSI phase
450 * @instance: adapter to dump
452 * Print the current SCSI phase for debugging purposes
457 static void NCR5380_print_phase(struct Scsi_Host *instance)
459 NCR5380_local_declare();
460 unsigned char status;
462 NCR5380_setup(instance);
464 status = NCR5380_read(STATUS_REG);
465 if (!(status & SR_REQ))
466 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
468 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
469 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
475 * We need to have our coroutine active given these constraints :
476 * 1. The mutex flag, main_running, can only be set when the main
477 * routine can actually process data, otherwise SCSI commands
478 * will never get issued.
480 * 2. NCR5380_main() shouldn't be called before it has exited, because
481 * other drivers have had kernel stack overflows in similar
484 * 3. We don't want to inline NCR5380_main() because of space concerns,
485 * even though it is only called in two places.
487 * So, the solution is to set the mutex in an inline wrapper for the
488 * main coroutine, and have the main coroutine exit with interrupts
489 * disabled after the final search through the queues so that no race
490 * conditions are possible.
493 static unsigned long main_running = 0;
496 * Function : run_main(void)
498 * Purpose : insure that the coroutine is running and will process our
499 * request. main_running is checked/set here (in an inline function)
500 * rather than in NCR5380_main itself to reduce the chances of stack
505 static __inline__ void run_main(void)
507 if (!test_and_set_bit(0, &main_running))
512 * These need tweaking, and would probably work best as per-device
513 * flags initialized differently for disk, tape, cd, etc devices.
514 * People with broken devices are free to experiment as to what gives
515 * the best results for them.
517 * USLEEP_SLEEP should be a minimum seek time.
519 * USLEEP_POLL should be a maximum rotational latency.
522 /* 20 ms (reasonable hard disk speed) */
523 #define USLEEP_SLEEP (20*HZ/1000)
525 /* 300 RPM (floppy speed) */
527 #define USLEEP_POLL (200*HZ/1000)
529 #ifndef USLEEP_WAITLONG
530 /* RvC: (reasonable time to wait on select error) */
531 #define USLEEP_WAITLONG USLEEP_SLEEP
534 static struct Scsi_Host *expires_first = NULL;
537 * Function : int should_disconnect (unsigned char cmd)
539 * Purpose : decide weather a command would normally disconnect or
540 * not, since if it won't disconnect we should go to sleep.
542 * Input : cmd - opcode of SCSI command
544 * Returns : DISCONNECT_LONG if we should disconnect for a really long
545 * time (ie always, sleep, look for REQ active, sleep),
546 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
547 * time-to-data delay, DISCONNECT_NONE if this command would return
550 * Future sleep algorithms based on time to data can exploit
551 * something like this so they can differentiate between "normal"
552 * (ie, read, write, seek) and unusual commands (ie, * format).
554 * Note : We don't deal with commands that handle an immediate disconnect,
558 static int should_disconnect(unsigned char cmd)
567 return DISCONNECT_TIME_TO_DATA;
572 return DISCONNECT_LONG;
574 return DISCONNECT_NONE;
579 * Assumes instance->time_expires has been set in higher level code.
581 * Locks: Caller must hold io_request_lock
584 static int NCR5380_set_timer(struct Scsi_Host *instance)
586 struct Scsi_Host *tmp, **prev;
588 if (((struct NCR5380_hostdata *) (instance->hostdata))->next_timer) {
591 for (prev = &expires_first, tmp = expires_first; tmp; prev = &(((struct NCR5380_hostdata *) tmp->hostdata)->next_timer), tmp = ((struct NCR5380_hostdata *) tmp->hostdata)->next_timer)
592 if (((struct NCR5380_hostdata *) instance->hostdata)->time_expires < ((struct NCR5380_hostdata *) tmp->hostdata)->time_expires)
595 ((struct NCR5380_hostdata *) instance->hostdata)->next_timer = tmp;
598 mod_timer(&usleep_timer, ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires);
603 * NCR5380_timer_fn - handle polled timeouts
606 * Walk the list of controllers, find which controllers have exceeded
607 * their expiry timeout and then schedule the processing co-routine to
610 * Doing something about unwanted reentrancy here might be useful
612 * Locks: disables irqs, takes and frees io_request_lock
615 static void NCR5380_timer_fn(unsigned long unused)
617 struct Scsi_Host *instance;
619 spin_lock_irq(&io_request_lock);
621 for (; expires_first && time_before_eq(((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires, jiffies);) {
622 instance = ((struct NCR5380_hostdata *) expires_first->hostdata)->next_timer;
623 ((struct NCR5380_hostdata *) expires_first->hostdata)->next_timer = NULL;
624 ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires = 0;
625 expires_first = instance;
628 del_timer(&usleep_timer);
630 usleep_timer.expires = ((struct NCR5380_hostdata *) expires_first->hostdata)->time_expires;
631 add_timer(&usleep_timer);
634 spin_unlock_irq(&io_request_lock);
638 * NCR5380_all_init - global setup
640 * Set up the global values and timers needed by the NCR5380 driver
643 static inline void NCR5380_all_init(void)
647 dprintk(NDEBUG_INIT, ("scsi : NCR5380_all_init()\n"));
649 init_timer(&usleep_timer);
650 usleep_timer.function = NCR5380_timer_fn;
655 static int probe_irq __initdata = 0;
658 * probe_intr - helper for IRQ autoprobe
659 * @irq: interrupt number
663 * Set a flag to indicate the IRQ in question was received. This is
664 * used by the IRQ probe code.
667 static void __init probe_intr(int irq, void *dev_id, struct pt_regs *regs)
673 * NCR5380_probe_irq - find the IRQ of an NCR5380
674 * @instance: NCR5380 controller
675 * @possible: bitmask of ISA IRQ lines
677 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
678 * and then looking to see what interrupt actually turned up.
680 * Locks: none, irqs must be enabled on entry
683 static int __init NCR5380_probe_irq(struct Scsi_Host *instance, int possible)
685 NCR5380_local_declare();
686 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
687 unsigned long timeout;
688 int trying_irqs, i, mask;
689 NCR5380_setup(instance);
691 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
692 if ((mask & possible) && (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL) == 0))
695 timeout = jiffies + (250 * HZ / 1000);
696 probe_irq = SCSI_IRQ_NONE;
699 * A interrupt is triggered whenever BSY = false, SEL = true
700 * and a bit set in the SELECT_ENABLE_REG is asserted on the
703 * Note that the bus is only driven when the phase control signals
704 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
708 NCR5380_write(TARGET_COMMAND_REG, 0);
709 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
710 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
711 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
713 while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
716 NCR5380_write(SELECT_ENABLE_REG, 0);
717 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
719 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
720 if (trying_irqs & mask)
727 * NCR58380_print_options - show options
728 * @instance: unused for now
730 * Called by probe code indicating the NCR5380 driver options that
731 * were selected. At some point this will switch to runtime options
732 * read from the adapter in question
737 static void __init NCR5380_print_options(struct Scsi_Host *instance)
739 printk(" generic options"
765 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
766 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
767 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
768 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
773 * NCR5380_print_status - dump controller info
774 * @instance: controller to dump
776 * Print commands in the various queues, called from NCR5380_abort
777 * and NCR5380_debug to aid debugging.
779 * Locks: called functions disable irqs, missing queue lock in proc call
782 static void NCR5380_print_status(struct Scsi_Host *instance)
784 static char pr_bfr[512];
788 printk("NCR5380 : coroutine is%s running.\n", main_running ? "" : "n't");
790 NCR5380_dprint(NDEBUG_ANY, instance);
791 NCR5380_dprint_phase(NDEBUG_ANY, instance);
793 len = NCR5380_proc_info(pr_bfr, &start, 0, sizeof(pr_bfr), instance->host_no, 0);
795 printk("\n%s\n", pr_bfr);
798 /******************************************/
800 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
802 * *buffer: I/O buffer
803 * **start: if inout == FALSE pointer into buffer where user read should start
804 * offset: current offset
805 * length: length of buffer
806 * hostno: Scsi_Host host_no
807 * inout: TRUE - user is writing; FALSE - user is reading
809 * Return the number of bytes read from or written
813 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
815 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
817 char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
819 char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
821 #ifndef NCR5380_proc_info
824 int NCR5380_proc_info(char *buffer, char **start, off_t offset, int length, int hostno, int inout)
827 struct Scsi_Host *instance;
828 struct NCR5380_hostdata *hostdata;
831 for (instance = first_instance; instance && instance->host_no != hostno; instance = instance->next);
834 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
836 if (inout) { /* Has data been written to the file ? */
837 #ifdef DTC_PUBLIC_RELEASE
838 dtc_wmaxi = dtc_maxi = 0;
840 #ifdef PAS16_PUBLIC_RELEASE
841 pas_wmaxi = pas_maxi = 0;
843 return (-ENOSYS); /* Currently this is a no-op */
845 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
846 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
847 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
848 #ifdef DTC_PUBLIC_RELEASE
849 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
851 #ifdef T128_PUBLIC_RELEASE
852 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
854 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
855 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
857 #ifdef PAS16_PUBLIC_RELEASE
858 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
861 SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
862 SPRINTF("io_port: %04x ", (int) instance->io_port);
863 if (instance->irq == SCSI_IRQ_NONE)
864 SPRINTF("IRQ: None.\n");
866 SPRINTF("IRQ: %d.\n", instance->irq);
868 #ifdef DTC_PUBLIC_RELEASE
869 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi);
871 #ifdef PAS16_PUBLIC_RELEASE
872 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi);
874 spin_lock_irq(&io_request_lock);
875 SPRINTF("NCR5380 : coroutine is%s running.\n", main_running ? "" : "n't");
876 if (!hostdata->connected)
877 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
879 pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, pos, buffer, length);
880 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
881 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
882 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
884 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
885 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
886 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
888 spin_unlock_irq(&io_request_lock);
891 if (pos - buffer < offset)
893 else if (pos - buffer - offset < length)
894 return pos - buffer - offset;
899 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
901 SPRINTF("scsi%d : destination target %d, lun %d\n", cmd->host->host_no, cmd->target, cmd->lun);
902 SPRINTF(" command = ");
903 pos = lprint_command(cmd->cmnd, pos, buffer, length);
908 char *lprint_command(unsigned char *command, char *pos, char *buffer, int length)
911 pos = lprint_opcode(command[0], pos, buffer, length);
912 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
913 SPRINTF("%02x ", command[i]);
919 char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
921 SPRINTF("%2d (0x%02x)", opcode, opcode);
927 * NCR5380_init - initialise an NCR5380
928 * @instance: adapter to configure
929 * @flags: control flags
931 * Initializes *instance and corresponding 5380 chip,
932 * with flags OR'd into the initial flags value.
934 * Notes : I assume that the host, hostno, and id bits have been
935 * set correctly. I don't care about the irq and other fields.
937 * Locks: interrupts must be enabled when we are called
940 static void __init NCR5380_init(struct Scsi_Host *instance, int flags)
942 NCR5380_local_declare();
944 unsigned long timeout;
945 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
948 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
950 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
955 if (flags & FLAG_NCR53C400)
956 instance->NCR5380_instance_name += NCR53C400_address_adjust;
959 NCR5380_setup(instance);
962 hostdata->aborted = 0;
963 hostdata->id_mask = 1 << instance->this_id;
964 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
965 if (i > hostdata->id_mask)
966 hostdata->id_higher_mask |= i;
967 for (i = 0; i < 8; ++i)
968 hostdata->busy[i] = 0;
970 hostdata->dmalen = 0;
972 hostdata->targets_present = 0;
973 hostdata->connected = NULL;
974 hostdata->issue_queue = NULL;
975 hostdata->disconnected_queue = NULL;
977 for (i = 0; i < 8; ++i) {
978 hostdata->time_read[i] = 0;
979 hostdata->time_write[i] = 0;
980 hostdata->bytes_read[i] = 0;
981 hostdata->bytes_write[i] = 0;
983 hostdata->timebase = 0;
984 hostdata->pendingw = 0;
985 hostdata->pendingr = 0;
988 /* The CHECK code seems to break the 53C400. Will check it later maybe */
989 if (flags & FLAG_NCR53C400)
990 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
992 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
995 the_template = instance->hostt;
996 first_instance = instance;
998 hostdata->time_expires = 0;
999 hostdata->next_timer = NULL;
1002 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
1003 printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" " without AUTOSENSE option, contingent allegiance conditions may\n"
1004 " be incorrectly cleared.\n", instance->host_no);
1005 #endif /* def AUTOSENSE */
1007 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1008 NCR5380_write(MODE_REG, MR_BASE);
1009 NCR5380_write(TARGET_COMMAND_REG, 0);
1010 NCR5380_write(SELECT_ENABLE_REG, 0);
1013 if (hostdata->flags & FLAG_NCR53C400) {
1014 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
1019 * Detect and correct bus wedge problems.
1021 * If the system crashed, it may have crashed in a state
1022 * where a SCSI command was still executing, and the
1023 * SCSI bus is not in a BUS FREE STATE.
1025 * If this is the case, we'll try to abort the currently
1026 * established nexus which we know nothing about, and that
1027 * failing, do a hard reset of the SCSI bus
1030 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
1035 printk("scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
1036 timeout = jiffies + 5 * HZ;
1037 while (time_before(jiffies, timeout) && (NCR5380_read(STATUS_REG) & SR_BSY));
1040 printk("scsi%d: bus busy, attempting abort\n", instance->host_no);
1044 printk("scsi%d: bus busy, attempting reset\n", instance->host_no);
1048 printk("scsi%d: bus locked solid or invalid override\n", instance->host_no);
1054 * NCR5380_queue_command - queue a command
1055 * @cmd: SCSI command
1056 * @done: completion handler
1058 * cmd is added to the per instance issue_queue, with minor
1059 * twiddling done to the host specific fields of cmd. If the
1060 * main coroutine is not running, it is restarted.
1062 * Locks: io_request lock held by caller. Called functions drop and
1063 * retake this lock. Called functions take dma lock.
1066 /* Only make static if a wrapper function is used */
1067 #ifndef NCR5380_queue_command
1070 int NCR5380_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *)) {
1071 struct Scsi_Host *instance = cmd->host;
1072 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1075 #if (NDEBUG & NDEBUG_NO_WRITE)
1076 switch (cmd->cmnd[0]) {
1079 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
1080 cmd->result = (DID_ERROR << 16);
1084 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1086 #ifdef NCR5380_STATS
1087 switch (cmd->cmnd[0]) {
1091 hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
1092 hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
1093 hostdata->pendingw++;
1098 hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
1099 hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
1100 hostdata->pendingr++;
1106 * We use the host_scribble field as a pointer to the next command
1110 cmd->host_scribble = NULL;
1111 cmd->scsi_done = done;
1115 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1116 * commands are added to the head of the queue since any command will
1117 * clear the contingent allegiance condition that exists and the
1118 * sense data is only guaranteed to be valid while the condition exists.
1121 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1122 LIST(cmd, hostdata->issue_queue);
1123 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1124 hostdata->issue_queue = cmd;
1126 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
1128 tmp->host_scribble = (unsigned char *) cmd;
1130 dprintk(NDEBUG_QUEUES, ("scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
1132 /* Run the coroutine if it isn't already running. */
1138 * NCR5380_main - NCR state machines
1140 * NCR5380_main is a coroutine that runs as long as more work can
1141 * be done on the NCR5380 host adapters in a system. Both
1142 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1143 * in case it is not running.
1145 * Locks; The caller must hold the io_request_lock. The lock will still be
1146 * held on return but may be dropped while running. Called functions take
1150 static void NCR5380_main(void) {
1151 Scsi_Cmnd *tmp, *prev;
1152 struct Scsi_Host *instance;
1153 struct NCR5380_hostdata *hostdata;
1157 * We run (with interrupts disabled) until we're sure that none of
1158 * the host adapters have anything that can be done, at which point
1159 * we set main_running to 0 and exit.
1161 * Interrupts are enabled before doing various other internal
1162 * instructions, after we've decided that we need to run through
1165 * this should prevent any race conditions.
1169 /* Lock held here */
1171 for (instance = first_instance; instance && instance->hostt == the_template; instance = instance->next) {
1172 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1173 /* Lock held here */
1174 if (!hostdata->connected && !hostdata->selecting) {
1175 dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no));
1177 * Search through the issue_queue for a command destined
1178 * for a target that's not busy.
1180 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
1183 dprintk(NDEBUG_LISTS, ("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun));
1184 /* When we find one, remove it from the issue queue. */
1185 if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
1187 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
1188 prev->host_scribble = tmp->host_scribble;
1190 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
1191 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1193 tmp->host_scribble = NULL;
1197 * Attempt to establish an I_T_L nexus here.
1198 * On success, instance->hostdata->connected is set.
1199 * On failure, we must add the command back to the
1200 * issue queue so we can keep trying.
1202 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main() : command for target %d lun %d removed from issue_queue\n", instance->host_no, tmp->target, tmp->lun));
1205 * A successful selection is defined as one that
1206 * leaves us with the command connected and
1207 * in hostdata->connected, OR has terminated the
1210 * With successful commands, we fall through
1211 * and see if we can do an information transfer,
1212 * with failures we will restart.
1214 hostdata->selecting = 0;
1215 /* RvC: have to preset this to indicate a new command is being performed */
1217 if (!NCR5380_select(instance, tmp,
1219 * REQUEST SENSE commands are issued without tagged
1220 * queueing, even on SCSI-II devices because the
1221 * contingent allegiance condition exists for the
1224 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
1227 LIST(tmp, hostdata->issue_queue);
1228 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1229 hostdata->issue_queue = tmp;
1231 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no));
1233 /* lock held here still */
1234 } /* if target/lun is not busy */
1237 } /* if (!hostdata->connected) */
1238 if (hostdata->selecting) {
1239 tmp = (Scsi_Cmnd *) hostdata->selecting;
1240 /* Selection will drop and retake the lock */
1241 if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
1244 /* RvC: device failed, so we wait a long time
1245 this is needed for Mustek scanners, that
1246 do not respond to commands immediately
1248 printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->target);
1249 //spin_lock_irq(&io_request_lock);
1250 LIST(tmp, hostdata->issue_queue);
1251 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1252 hostdata->issue_queue = tmp;
1253 //spin_unlock_irq(&io_request_lock);
1255 hostdata->time_expires = jiffies + USLEEP_WAITLONG;
1256 NCR5380_set_timer(instance);
1258 } /* if hostdata->selecting */
1259 if (hostdata->connected
1261 && !hostdata->dmalen
1263 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1265 dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no));
1266 NCR5380_information_transfer(instance);
1267 dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no));
1271 } /* for instance */
1273 /* Exit lock held */
1274 clear_bit(0, &main_running);
1277 #ifndef DONT_USE_INTR
1278 #include <linux/blk.h>
1279 #include <linux/spinlock.h>
1282 * NCR5380_intr - generic NCR5380 irq handler
1284 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1285 * from the disconnected queue, and restarting NCR5380_main()
1288 * Locks: caller must hold the io_request lock.
1291 static void NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs) {
1292 NCR5380_local_declare();
1293 struct Scsi_Host *instance;
1297 dprintk(NDEBUG_INTR, ("scsi : NCR5380 irq %d triggered\n", irq));
1301 for (instance = first_instance; instance && (instance->hostt == the_template); instance = instance->next)
1302 if (instance->irq == irq) {
1304 /* Look for pending interrupts */
1305 NCR5380_setup(instance);
1306 basr = NCR5380_read(BUS_AND_STATUS_REG);
1307 /* XXX dispatch to appropriate routine if found and done=0 */
1308 if (basr & BASR_IRQ) {
1309 NCR5380_dprint(NDEBUG_INTR, instance);
1310 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1312 dprintk(NDEBUG_INTR, ("scsi%d : SEL interrupt\n", instance->host_no));
1313 NCR5380_reselect(instance);
1314 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1315 } else if (basr & BASR_PARITY_ERROR) {
1316 dprintk(NDEBUG_INTR, ("scsi%d : PARITY interrupt\n", instance->host_no));
1317 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1318 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1319 dprintk(NDEBUG_INTR, ("scsi%d : RESET interrupt\n", instance->host_no));
1320 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1322 #if defined(REAL_DMA)
1324 * We should only get PHASE MISMATCH and EOP interrupts
1325 * if we have DMA enabled, so do a sanity check based on
1326 * the current setting of the MODE register.
1329 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
1332 if (!hostdata->connected)
1333 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1335 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1336 hostdata->connected->SCp.this_residual -= transferred;
1337 hostdata->connected->SCp.ptr += transferred;
1338 hostdata->dmalen = 0;
1340 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1343 unsigned long timeout = jiffies + NCR_TIMEOUT;
1345 spin_unlock_irq(&io_request_lock);
1346 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK && time_before(jiffies, timeout));
1347 spin_lock_irq(&io_request_lock);
1349 if (time_after_eq(jiffies, timeout))
1350 printk("scsi%d: timeout at NCR5380.c:%d\n", host->host_no, __LINE__);
1352 #else /* NCR_TIMEOUT */
1353 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
1356 NCR5380_write(MODE_REG, MR_BASE);
1357 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1360 dprintk(NDEBUG_INTR, ("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG)));
1361 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1367 } /* if (instance->irq == irq) */
1373 * @irq: interrupt number
1374 * @dev_id: device info
1375 * @regs: registers (unused)
1377 * Takes the io_request_lock and invokes the generic NCR5380 interrupt
1380 * Locks: takes and releases the io_request lock
1383 static void do_NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs) {
1384 unsigned long flags;
1386 spin_lock_irqsave(&io_request_lock, flags);
1387 NCR5380_intr(irq, dev_id, regs);
1388 spin_unlock_irqrestore(&io_request_lock, flags);
1394 * collect_stats - collect stats on a scsi command
1395 * @hostdata: adapter
1396 * @cmd: command being issued
1398 * Update the statistical data by parsing the command in question
1401 static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
1403 #ifdef NCR5380_STATS
1404 switch (cmd->cmnd[0]) {
1408 hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1409 hostdata->pendingw--;
1414 hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1415 hostdata->pendingr--;
1423 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1426 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1427 * including ARBITRATION, SELECTION, and initial message out for
1428 * IDENTIFY and queue messages.
1430 * Inputs : instance - instantiation of the 5380 driver on which this
1431 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1432 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1433 * the command that is presently connected.
1435 * Returns : -1 if selection could not execute for some reason,
1436 * 0 if selection succeeded or failed because the target
1440 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1441 * with registers as they should have been on entry - ie
1442 * SELECT_ENABLE will be set appropriately, the NCR5380
1443 * will cease to drive any SCSI bus signals.
1445 * If successful : I_T_L or I_T_L_Q nexus will be established,
1446 * instance->connected will be set to cmd.
1447 * SELECT interrupt will be disabled.
1449 * If failed (no target) : cmd->scsi_done() will be called, and the
1450 * cmd->result host byte set to DID_BAD_TARGET.
1452 * Locks: caller holds io_request_lock
1455 static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
1457 NCR5380_local_declare();
1458 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1459 unsigned char tmp[3], phase;
1460 unsigned char *data;
1462 unsigned long timeout;
1463 unsigned char value;
1464 NCR5380_setup(instance);
1466 if (hostdata->selecting) {
1467 goto part2; /* RvC: sorry prof. Dijkstra, but it keeps the
1468 rest of the code nearly the same */
1471 hostdata->restart_select = 0;
1473 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
1474 dprintk(NDEBUG_ARBITRATION, ("scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id));
1477 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1478 * data bus during SELECTION.
1481 NCR5380_write(TARGET_COMMAND_REG, 0);
1484 * Start arbitration.
1487 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1488 NCR5380_write(MODE_REG, MR_ARBITRATE);
1490 /* Wait for arbitration logic to complete */
1493 unsigned long timeout = jiffies + 2 * NCR_TIMEOUT;
1495 spin_unlock_irq(&io_request_lock);
1497 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1498 && time_before(jiffies, timeout));
1500 spin_lock_irq(&io_request_lock);
1502 if (time_after_eq(jiffies, timeout)) {
1503 printk("scsi: arbitration timeout at %d\n", __LINE__);
1504 NCR5380_write(MODE_REG, MR_BASE);
1505 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1509 #else /* NCR_TIMEOUT */
1510 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
1513 dprintk(NDEBUG_ARBITRATION, ("scsi%d : arbitration complete\n", instance->host_no));
1516 * The arbitration delay is 2.2us, but this is a minimum and there is
1517 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1518 * the integral nature of udelay().
1524 /* Check for lost arbitration */
1525 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1526 NCR5380_write(MODE_REG, MR_BASE);
1527 dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no));
1530 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1532 if (!(hostdata->flags & FLAG_DTC3181E) &&
1533 /* RvC: DTC3181E has some trouble with this
1534 * so we simply removed it. Seems to work with
1535 * only Mustek scanner attached
1537 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1538 NCR5380_write(MODE_REG, MR_BASE);
1539 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1540 dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no));
1544 * Again, bus clear + bus settle time is 1.2us, however, this is
1545 * a minimum so we'll udelay ceil(1.2)
1550 dprintk(NDEBUG_ARBITRATION, ("scsi%d : won arbitration\n", instance->host_no));
1553 * Now that we have won arbitration, start Selection process, asserting
1554 * the host and target ID's on the SCSI bus.
1557 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1560 * Raise ATN while SEL is true before BSY goes false from arbitration,
1561 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1562 * phase immediately after selection.
1565 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1566 NCR5380_write(MODE_REG, MR_BASE);
1569 * Reselect interrupts must be turned off prior to the dropping of BSY,
1570 * otherwise we will trigger an interrupt.
1572 NCR5380_write(SELECT_ENABLE_REG, 0);
1575 * The initiator shall then wait at least two deskew delays and release
1578 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1581 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1584 * Something weird happens when we cease to drive BSY - looks
1585 * like the board/chip is letting us do another read before the
1586 * appropriate propagation delay has expired, and we're confusing
1587 * a BSY signal from ourselves as the target's response to SELECTION.
1589 * A small delay (the 'C++' frontend breaks the pipeline with an
1590 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1591 * tighter 'C' code breaks and requires this) solves the problem -
1592 * the 1 us delay is arbitrary, and only used because this delay will
1593 * be the same on other platforms and since it works here, it should
1596 * wingel suggests that this could be due to failing to wait
1602 dprintk(NDEBUG_SELECTION, ("scsi%d : selecting target %d\n", instance->host_no, cmd->target));
1605 * The SCSI specification calls for a 250 ms timeout for the actual
1609 timeout = jiffies + (250 * HZ / 1000);
1612 * XXX very interesting - we're seeing a bounce where the BSY we
1613 * asserted is being reflected / still asserted (propagation delay?)
1614 * and it's detecting as true. Sigh.
1617 hostdata->select_time = 0; /* we count the clock ticks at which we polled */
1618 hostdata->selecting = cmd;
1621 /* RvC: here we enter after a sleeping period, or immediately after
1623 we poll only once ech clock tick */
1624 value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
1626 if (!value && (hostdata->select_time < 25)) {
1627 /* RvC: we still must wait for a device response */
1628 hostdata->select_time++; /* after 25 ticks the device has failed */
1629 hostdata->time_expires = jiffies + 1;
1630 NCR5380_set_timer(instance);
1631 return 0; /* RvC: we return here with hostdata->selecting set,
1635 hostdata->selecting = 0; /* clear this pointer, because we passed the
1637 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1638 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1639 NCR5380_reselect(instance);
1640 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1641 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1645 * No less than two deskew delays after the initiator detects the
1646 * BSY signal is true, it shall release the SEL signal and may
1647 * change the DATA BUS. -wingel
1652 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1654 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1655 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1656 if (hostdata->targets_present & (1 << cmd->target)) {
1657 printk("scsi%d : weirdness\n", instance->host_no);
1658 if (hostdata->restart_select)
1659 printk("\trestart select\n");
1660 NCR5380_dprint(NDEBUG_SELECTION, instance);
1661 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1664 cmd->result = DID_BAD_TARGET << 16;
1665 collect_stats(hostdata, cmd);
1666 cmd->scsi_done(cmd);
1667 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1668 dprintk(NDEBUG_SELECTION, ("scsi%d : target did not respond within 250ms\n", instance->host_no));
1669 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1672 hostdata->targets_present |= (1 << cmd->target);
1675 * Since we followed the SCSI spec, and raised ATN while SEL
1676 * was true but before BSY was false during selection, the information
1677 * transfer phase should be a MESSAGE OUT phase so that we can send the
1680 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1681 * message (2 bytes) with a tag ID that we increment with every command
1682 * until it wraps back to 0.
1684 * XXX - it turns out that there are some broken SCSI-II devices,
1685 * which claim to support tagged queuing but fail when more than
1686 * some number of commands are issued at once.
1689 /* Wait for start of REQ/ACK handshake */
1692 unsigned long timeout = jiffies + NCR_TIMEOUT;
1694 spin_unlock_irq(&io_request_lock);
1695 while (!(NCR5380_read(STATUS_REG) & SR_REQ) && time_before(jiffies, timeout));
1696 spin_lock_irq(&io_request_lock);
1698 if (time_after_eq(jiffies, timeout)) {
1699 printk("scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1700 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1704 #else /* NCR_TIMEOUT */
1705 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1706 #endif /* def NCR_TIMEOUT */
1708 dprintk(NDEBUG_SELECTION, ("scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->target));
1709 tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->lun);
1714 /* Send message(s) */
1716 phase = PHASE_MSGOUT;
1717 NCR5380_transfer_pio(instance, &phase, &len, &data);
1718 dprintk(NDEBUG_SELECTION, ("scsi%d : nexus established.\n", instance->host_no));
1719 /* XXX need to handle errors here */
1720 hostdata->connected = cmd;
1721 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1723 initialize_SCp(cmd);
1730 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1731 * unsigned char *phase, int *count, unsigned char **data)
1733 * Purpose : transfers data in given phase using polled I/O
1735 * Inputs : instance - instance of driver, *phase - pointer to
1736 * what phase is expected, *count - pointer to number of
1737 * bytes to transfer, **data - pointer to data pointer.
1739 * Returns : -1 when different phase is entered without transferring
1740 * maximum number of bytes, 0 if all bytes or transfered or exit
1743 * Also, *phase, *count, *data are modified in place.
1745 * XXX Note : handling for bus free may be useful.
1749 * Note : this code is not as quick as it could be, however it
1750 * IS 100% reliable, and for the actual data transfer where speed
1751 * counts, we will always do a pseudo DMA or DMA transfer.
1754 static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1755 NCR5380_local_declare();
1756 unsigned char p = *phase, tmp;
1758 unsigned char *d = *data;
1760 * RvC: some administrative data to process polling time
1762 int break_allowed = 0;
1763 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1764 NCR5380_setup(instance);
1767 dprintk(NDEBUG_PIO, ("scsi%d : pio write %d bytes\n", instance->host_no, c));
1769 dprintk(NDEBUG_PIO, ("scsi%d : pio read %d bytes\n", instance->host_no, c));
1772 * The NCR5380 chip will only drive the SCSI bus when the
1773 * phase specified in the appropriate bits of the TARGET COMMAND
1774 * REGISTER match the STATUS REGISTER
1777 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1779 /* RvC: don't know if this is necessary, but other SCSI I/O is short
1780 * so breaks are not necessary there
1782 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
1787 * Wait for assertion of REQ, after which the phase bits will be
1791 /* RvC: we simply poll once, after that we stop temporarily
1792 * and let the device buffer fill up
1793 * if breaking is not allowed, we keep polling as long as needed
1796 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
1797 if (!(tmp & SR_REQ)) {
1798 /* timeout condition */
1799 hostdata->time_expires = jiffies + USLEEP_SLEEP;
1800 NCR5380_set_timer(instance);
1804 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : REQ detected\n", instance->host_no));
1806 /* Check for phase mismatch */
1807 if ((tmp & PHASE_MASK) != p) {
1808 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : phase mismatch\n", instance->host_no));
1809 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1812 /* Do actual transfer from SCSI bus to / from memory */
1814 NCR5380_write(OUTPUT_DATA_REG, *d);
1816 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1821 * The SCSI standard suggests that in MSGOUT phase, the initiator
1822 * should drop ATN on the last byte of the message phase
1823 * after REQ has been asserted for the handshake but before
1824 * the initiator raises ACK.
1828 if (!((p & SR_MSG) && c > 1)) {
1829 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1830 NCR5380_dprint(NDEBUG_PIO, instance);
1831 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1833 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1834 NCR5380_dprint(NDEBUG_PIO, instance);
1835 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1838 NCR5380_dprint(NDEBUG_PIO, instance);
1839 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1842 while (NCR5380_read(STATUS_REG) & SR_REQ);
1844 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : req false, handshake complete\n", instance->host_no));
1847 * We have several special cases to consider during REQ/ACK handshaking :
1848 * 1. We were in MSGOUT phase, and we are on the last byte of the
1849 * message. ATN must be dropped as ACK is dropped.
1851 * 2. We are in a MSGIN phase, and we are on the last byte of the
1852 * message. We must exit with ACK asserted, so that the calling
1853 * code may raise ATN before dropping ACK to reject the message.
1855 * 3. ACK and ATN are clear and the target may proceed as normal.
1857 if (!(p == PHASE_MSGIN && c == 1)) {
1858 if (p == PHASE_MSGOUT && c > 1)
1859 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1861 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1865 dprintk(NDEBUG_PIO, ("scsi%d : residual %d\n", instance->host_no, c));
1869 tmp = NCR5380_read(STATUS_REG);
1871 *phase = tmp & PHASE_MASK;
1873 *phase = PHASE_UNKNOWN;
1875 if (!c || (*phase == p))
1882 * do_reset - issue a reset command
1883 * @host: adapter to reset
1885 * Issue a reset sequence to the NCR5380 and try and get the bus
1886 * back into sane shape.
1888 * Locks: caller holds io_request lock
1891 static void do_reset(struct Scsi_Host *host) {
1892 NCR5380_local_declare();
1893 NCR5380_setup(host);
1895 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1896 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1898 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1902 * Function : do_abort (Scsi_Host *host)
1904 * Purpose : abort the currently established nexus. Should only be
1905 * called from a routine which can drop into a
1907 * Returns : 0 on success, -1 on failure.
1909 * Locks: io_request lock held by caller
1912 static int do_abort(struct Scsi_Host *host) {
1913 NCR5380_local_declare();
1914 unsigned char tmp, *msgptr, phase;
1916 NCR5380_setup(host);
1919 /* Request message out phase */
1920 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1923 * Wait for the target to indicate a valid phase by asserting
1924 * REQ. Once this happens, we'll have either a MSGOUT phase
1925 * and can immediately send the ABORT message, or we'll have some
1926 * other phase and will have to source/sink data.
1928 * We really don't care what value was on the bus or what value
1929 * the target sees, so we just handshake.
1932 while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1934 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1936 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1937 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1938 while (NCR5380_read(STATUS_REG) & SR_REQ);
1939 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1944 phase = PHASE_MSGOUT;
1945 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1948 * If we got here, and the command completed successfully,
1949 * we're about to go into bus free state.
1952 return len ? -1 : 0;
1955 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1957 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1958 * unsigned char *phase, int *count, unsigned char **data)
1960 * Purpose : transfers data in given phase using either real
1963 * Inputs : instance - instance of driver, *phase - pointer to
1964 * what phase is expected, *count - pointer to number of
1965 * bytes to transfer, **data - pointer to data pointer.
1967 * Returns : -1 when different phase is entered without transferring
1968 * maximum number of bytes, 0 if all bytes or transfered or exit
1971 * Also, *phase, *count, *data are modified in place.
1973 * Locks: io_request lock held by caller
1977 static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1978 NCR5380_local_declare();
1979 register int c = *count;
1980 register unsigned char p = *phase;
1981 register unsigned char *d = *data;
1984 #if defined(REAL_DMA_POLL)
1986 unsigned char saved_data = 0, overrun = 0, residue;
1989 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1991 NCR5380_setup(instance);
1993 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1997 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1998 #ifdef READ_OVERRUNS
2003 dprintk(NDEBUG_DMA, ("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d));
2004 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
2007 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
2010 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
2011 #elif defined(REAL_DMA_POLL)
2012 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2015 * Note : on my sample board, watch-dog timeouts occurred when interrupts
2016 * were not disabled for the duration of a single DMA transfer, from
2017 * before the setting of DMA mode to after transfer of the last byte.
2020 #if defined(PSEUDO_DMA) && defined(UNSAFE)
2021 spin_unlock_irq(&io_request_lock);
2023 /* KLL May need eop and parity in 53c400 */
2024 if (hostdata->flags & FLAG_NCR53C400)
2025 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE | MR_MONITOR_BSY);
2027 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
2028 #endif /* def REAL_DMA */
2030 dprintk(NDEBUG_DMA, ("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG)));
2033 * On the PAS16 at least I/O recovery delays are not needed here.
2034 * Everyone else seems to want them.
2038 io_recovery_delay(1);
2039 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
2041 io_recovery_delay(1);
2042 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
2043 io_recovery_delay(1);
2044 NCR5380_write(START_DMA_SEND_REG, 0);
2045 io_recovery_delay(1);
2048 #if defined(REAL_DMA_POLL)
2050 tmp = NCR5380_read(BUS_AND_STATUS_REG);
2051 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
2054 At this point, either we've completed DMA, or we have a phase mismatch,
2055 or we've unexpectedly lost BUSY (which is a real error).
2057 For write DMAs, we want to wait until the last byte has been
2058 transferred out over the bus before we turn off DMA mode. Alas, there
2059 seems to be no terribly good way of doing this on a 5380 under all
2060 conditions. For non-scatter-gather operations, we can wait until REQ
2061 and ACK both go false, or until a phase mismatch occurs. Gather-writes
2062 are nastier, since the device will be expecting more data than we
2063 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
2064 could test LAST BIT SENT to assure transfer (I imagine this is precisely
2065 why this signal was added to the newer chips) but on the older 538[01]
2066 this signal does not exist. The workaround for this lack is a watchdog;
2067 we bail out of the wait-loop after a modest amount of wait-time if
2068 the usual exit conditions are not met. Not a terribly clean or
2069 correct solution :-%
2071 Reads are equally tricky due to a nasty characteristic of the NCR5380.
2072 If the chip is in DMA mode for an READ, it will respond to a target's
2073 REQ by latching the SCSI data into the INPUT DATA register and asserting
2074 ACK, even if it has _already_ been notified by the DMA controller that
2075 the current DMA transfer has completed! If the NCR5380 is then taken
2076 out of DMA mode, this already-acknowledged byte is lost.
2078 This is not a problem for "one DMA transfer per command" reads, because
2079 the situation will never arise... either all of the data is DMA'ed
2080 properly, or the target switches to MESSAGE IN phase to signal a
2081 disconnection (either operation bringing the DMA to a clean halt).
2082 However, in order to handle scatter-reads, we must work around the
2083 problem. The chosen fix is to DMA N-2 bytes, then check for the
2084 condition before taking the NCR5380 out of DMA mode. One or two extra
2085 bytes are transferred via PIO as necessary to fill out the original
2090 #ifdef READ_OVERRUNS
2092 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
2093 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
2099 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
2100 if (!(tmp & BASR_PHASE_MATCH))
2107 dprintk(NDEBUG_DMA, ("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG)));
2109 NCR5380_write(MODE_REG, MR_BASE);
2110 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2112 residue = NCR5380_dma_residual(instance);
2116 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2118 #ifdef READ_OVERRUNS
2119 if (*phase == p && (p & SR_IO) && residue == 0) {
2121 dprintk(NDEBUG_DMA, ("Got an input overrun, using saved byte\n"));
2122 **data = saved_data;
2127 printk("No overrun??\n");
2130 dprintk(NDEBUG_DMA, ("Doing %d-byte PIO to 0x%X\n", cnt, *data));
2131 NCR5380_transfer_pio(instance, phase, &cnt, data);
2132 *count -= toPIO - cnt;
2136 dprintk(NDEBUG_DMA, ("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count)));
2139 #elif defined(REAL_DMA)
2141 #else /* defined(REAL_DMA_POLL) */
2143 #ifdef DMA_WORKS_RIGHT
2144 foo = NCR5380_pread(instance, d, c);
2147 if (hostdata->flags & FLAG_NCR53C400) {
2150 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2152 * We can't disable DMA mode after successfully transferring
2153 * what we plan to be the last byte, since that would open up
2154 * a race condition where if the target asserted REQ before
2155 * we got the DMA mode reset, the NCR5380 would have latched
2156 * an additional byte into the INPUT DATA register and we'd
2159 * The workaround was to transfer one fewer bytes than we
2160 * intended to with the pseudo-DMA read function, wait for
2161 * the chip to latch the last byte, read it, and then disable
2164 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2165 * REQ is deasserted when ACK is asserted, and not reasserted
2166 * until ACK goes false. Since the NCR5380 won't lower ACK
2167 * until DACK is asserted, which won't happen unless we twiddle
2168 * the DMA port or we take the NCR5380 out of DMA mode, we
2169 * can guarantee that we won't handshake another extra
2173 if (!(hostdata->flags & FLAG_NCR53C400)) {
2174 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2175 /* Wait for clean handshake */
2176 while (NCR5380_read(STATUS_REG) & SR_REQ);
2177 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2182 #ifdef DMA_WORKS_RIGHT
2183 foo = NCR5380_pwrite(instance, d, c);
2186 dprintk(NDEBUG_C400_PWRITE, ("About to pwrite %d bytes\n", c));
2187 if (!(foo = NCR5380_pwrite(instance, d, c))) {
2189 * Wait for the last byte to be sent. If REQ is being asserted for
2190 * the byte we're interested, we'll ACK it and it will go false.
2192 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2194 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
2197 dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : timed out on last byte\n", instance->host_no));
2199 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2200 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2201 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2202 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2203 dprintk(NDEBUG_LAST_WRITE_SENT, ("scsi%d : last bit sent works\n", instance->host_no));
2207 dprintk(NDEBUG_C400_PWRITE, ("Waiting for LASTBYTE\n"));
2208 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2209 dprintk(NDEBUG_C400_PWRITE, ("Got LASTBYTE\n"));
2214 NCR5380_write(MODE_REG, MR_BASE);
2215 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2217 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2218 dprintk(NDEBUG_C400_PWRITE, ("53C400w: Checking for IRQ\n"));
2219 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2220 dprintk(NDEBUG_C400_PWRITE, ("53C400w: got it, reading reset interrupt reg\n"));
2221 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2223 printk("53C400w: IRQ NOT THERE!\n");
2228 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2229 #if defined(PSEUDO_DMA) && defined(UNSAFE)
2230 spin_lock_irq(&io_request_lock);
2231 #endif /* defined(REAL_DMA_POLL) */
2233 #endif /* def REAL_DMA */
2235 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2238 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2240 * Purpose : run through the various SCSI phases and do as the target
2241 * directs us to. Operates on the currently connected command,
2242 * instance->connected.
2244 * Inputs : instance, instance for which we are doing commands
2246 * Side effects : SCSI things happen, the disconnected queue will be
2247 * modified if a command disconnects, *instance->connected will
2250 * XXX Note : we need to watch for bus free or a reset condition here
2251 * to recover from an unexpected bus free condition.
2253 * Locks: io_request_lock held by caller
2256 static void NCR5380_information_transfer(struct Scsi_Host *instance) {
2257 NCR5380_local_declare();
2258 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
2259 unsigned char msgout = NOP;
2262 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2265 unsigned char *data;
2266 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
2267 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2268 /* RvC: we need to set the end of the polling time */
2269 unsigned long poll_time = jiffies + USLEEP_POLL;
2271 NCR5380_setup(instance);
2274 tmp = NCR5380_read(STATUS_REG);
2275 /* We only have a valid SCSI phase when REQ is asserted */
2277 phase = (tmp & PHASE_MASK);
2278 if (phase != old_phase) {
2280 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
2282 if (sink && (phase != PHASE_MSGOUT)) {
2283 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2285 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
2286 while (NCR5380_read(STATUS_REG) & SR_REQ);
2287 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2294 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2295 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
2298 cmd->result = DID_ERROR << 16;
2303 * If there is no room left in the current buffer in the
2304 * scatter-gather list, move onto the next one.
2307 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2309 --cmd->SCp.buffers_residual;
2310 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2311 cmd->SCp.ptr = cmd->SCp.buffer->address;
2312 dprintk(NDEBUG_INFORMATION, ("scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
2315 * The preferred transfer method is going to be
2316 * PSEUDO-DMA for systems that are strictly PIO,
2317 * since we can let the hardware do the handshaking.
2319 * For this to work, we need to know the transfersize
2320 * ahead of time, since the pseudo-DMA code will sit
2321 * in an unconditional loop.
2324 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2326 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2327 * driver then it will always be defined, so the
2328 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2329 * NCR5380 case. I think this is a fairly clean solution.
2330 * We supplement these 2 if's with the flag.
2332 #ifdef NCR5380_dma_xfer_len
2333 if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2335 transfersize = cmd->transfersize;
2337 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2338 if (transfersize > 512)
2340 #endif /* LIMIT_TRANSFERSIZE */
2342 if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
2343 /* Limit transfers to 32K, for xx400 & xx406
2344 * pseudoDMA that transfers in 128 bytes blocks. */
2345 if (transfersize > 32 * 1024)
2346 transfersize = 32 * 1024;
2349 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
2351 * If the watchdog timer fires, all future accesses to this
2352 * device will use the polled-IO.
2354 printk("scsi%d : switching target %d lun %d to slow handshake\n", instance->host_no, cmd->target, cmd->lun);
2355 cmd->device->borken = 1;
2356 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2359 cmd->result = DID_ERROR << 16;
2361 /* XXX - need to source or sink data here, as appropriate */
2363 cmd->SCp.this_residual -= transfersize - len;
2365 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2366 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
2372 NCR5380_transfer_pio(instance, &phase, &len, &data);
2373 cmd->SCp.Message = tmp;
2377 * Linking lets us reduce the time required to get the
2378 * next command out to the device, hopefully this will
2379 * mean we don't waste another revolution due to the delays
2380 * required by ARBITRATION and another SELECTION.
2382 * In the current implementation proposal, low level drivers
2383 * merely have to start the next command, pointed to by
2384 * next_link, done() is called as with unlinked commands.
2387 case LINKED_CMD_COMPLETE:
2388 case LINKED_FLG_CMD_COMPLETE:
2389 /* Accept message by clearing ACK */
2390 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2391 dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->target, cmd->lun));
2393 * Sanity check : A linked command should only terminate with
2394 * one of these messages if there are more linked commands
2397 if (!cmd->next_link) {
2398 printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->target, cmd->lun);
2403 initialize_SCp(cmd->next_link);
2404 /* The next command is still part of this process */
2405 cmd->next_link->tag = cmd->tag;
2406 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2407 dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked request done, calling scsi_done().\n", instance->host_no, cmd->target, cmd->lun));
2408 collect_stats(hostdata, cmd);
2409 cmd->scsi_done(cmd);
2410 cmd = hostdata->connected;
2412 #endif /* def LINKED */
2414 case COMMAND_COMPLETE:
2415 /* Accept message by clearing ACK */
2417 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2418 hostdata->connected = NULL;
2419 dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->target, cmd->lun));
2420 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2423 * I'm not sure what the correct thing to do here is :
2425 * If the command that just executed is NOT a request
2426 * sense, the obvious thing to do is to set the result
2427 * code to the values of the stored parameters.
2429 * If it was a REQUEST SENSE command, we need some way
2430 * to differentiate between the failure code of the original
2431 * and the failure code of the REQUEST sense - the obvious
2432 * case is success, where we fall through and leave the result
2435 * The non-obvious place is where the REQUEST SENSE failed
2438 if (cmd->cmnd[0] != REQUEST_SENSE)
2439 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2440 else if (cmd->SCp.Status != GOOD)
2441 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2444 if ((cmd->cmnd[0] != REQUEST_SENSE) && (cmd->SCp.Status == CHECK_CONDITION)) {
2445 dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no));
2446 cmd->cmnd[0] = REQUEST_SENSE;
2447 cmd->cmnd[1] &= 0xe0;
2450 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2453 cmd->SCp.buffer = NULL;
2454 cmd->SCp.buffers_residual = 0;
2455 cmd->SCp.ptr = (char *) cmd->sense_buffer;
2456 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2458 LIST(cmd, hostdata->issue_queue);
2459 cmd->host_scribble = (unsigned char *)
2460 hostdata->issue_queue;
2461 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2462 dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
2464 #endif /* def AUTOSENSE */
2466 collect_stats(hostdata, cmd);
2467 cmd->scsi_done(cmd);
2470 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2472 * Restore phase bits to 0 so an interrupted selection,
2473 * arbitration can resume.
2475 NCR5380_write(TARGET_COMMAND_REG, 0);
2477 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2480 case MESSAGE_REJECT:
2481 /* Accept message by clearing ACK */
2482 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2483 switch (hostdata->last_message) {
2484 case HEAD_OF_QUEUE_TAG:
2485 case ORDERED_QUEUE_TAG:
2486 case SIMPLE_QUEUE_TAG:
2487 cmd->device->tagged_queue = 0;
2488 hostdata->busy[cmd->target] |= (1 << cmd->lun);
2494 /* Accept message by clearing ACK */
2495 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2496 cmd->device->disconnect = 1;
2497 LIST(cmd, hostdata->disconnected_queue);
2498 cmd->host_scribble = (unsigned char *)
2499 hostdata->disconnected_queue;
2500 hostdata->connected = NULL;
2501 hostdata->disconnected_queue = cmd;
2502 dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d lun %d was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->target, cmd->lun));
2504 * Restore phase bits to 0 so an interrupted selection,
2505 * arbitration can resume.
2507 NCR5380_write(TARGET_COMMAND_REG, 0);
2509 /* Enable reselect interrupts */
2510 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2511 /* Wait for bus free to avoid nasty timeouts */
2512 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2517 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2518 * operation, in violation of the SCSI spec so we can safely
2519 * ignore SAVE/RESTORE pointers calls.
2521 * Unfortunately, some disks violate the SCSI spec and
2522 * don't issue the required SAVE_POINTERS message before
2523 * disconnecting, and we have to break spec to remain
2527 case RESTORE_POINTERS:
2528 /* Accept message by clearing ACK */
2529 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2531 case EXTENDED_MESSAGE:
2533 * Extended messages are sent in the following format :
2535 * 0 EXTENDED_MESSAGE == 1
2536 * 1 length (includes one byte for code, doesn't
2537 * include first two bytes)
2539 * 3..length+1 arguments
2541 * Start the extended message buffer with the EXTENDED_MESSAGE
2542 * byte, since print_msg() wants the whole thing.
2544 extended_msg[0] = EXTENDED_MESSAGE;
2545 /* Accept first byte by clearing ACK */
2546 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2547 dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no));
2550 data = extended_msg + 1;
2551 phase = PHASE_MSGIN;
2552 NCR5380_transfer_pio(instance, &phase, &len, &data);
2554 dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]));
2556 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2557 /* Accept third byte by clearing ACK */
2558 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2559 len = extended_msg[1] - 1;
2560 data = extended_msg + 3;
2561 phase = PHASE_MSGIN;
2563 NCR5380_transfer_pio(instance, &phase, &len, &data);
2564 dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len));
2566 switch (extended_msg[2]) {
2569 case EXTENDED_MODIFY_DATA_POINTER:
2570 case EXTENDED_EXTENDED_IDENTIFY:
2574 printk("scsi%d: error receiving extended message\n", instance->host_no);
2577 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2580 /* Fall through to reject message */
2583 * If we get something weird that we aren't expecting,
2588 printk("scsi%d: rejecting message ", instance->host_no);
2589 print_msg(extended_msg);
2591 } else if (tmp != EXTENDED_MESSAGE)
2592 printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n", instance->host_no, tmp, cmd->target, cmd->lun);
2594 printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n", instance->host_no, extended_msg[1], extended_msg[0], cmd->target, cmd->lun);
2596 msgout = MESSAGE_REJECT;
2597 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2599 } /* switch (tmp) */
2604 hostdata->last_message = msgout;
2605 NCR5380_transfer_pio(instance, &phase, &len, &data);
2606 if (msgout == ABORT) {
2607 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2608 hostdata->connected = NULL;
2609 cmd->result = DID_ERROR << 16;
2610 collect_stats(hostdata, cmd);
2611 cmd->scsi_done(cmd);
2612 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2621 * XXX for performance reasons, on machines with a
2622 * PSEUDO-DMA architecture we should probably
2623 * use the dma transfer function.
2625 NCR5380_transfer_pio(instance, &phase, &len, &data);
2626 if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
2627 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2628 dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
2629 NCR5380_set_timer(instance);
2636 NCR5380_transfer_pio(instance, &phase, &len, &data);
2637 cmd->SCp.Status = tmp;
2640 printk("scsi%d : unknown phase\n", instance->host_no);
2641 NCR5380_dprint(NDEBUG_ALL, instance);
2642 } /* switch(phase) */
2643 } /* if (tmp * SR_REQ) */
2645 /* RvC: go to sleep if polling time expired
2647 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
2648 hostdata->time_expires = jiffies + USLEEP_SLEEP;
2649 dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
2650 NCR5380_set_timer(instance);
2658 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2660 * Purpose : does reselection, initializing the instance->connected
2661 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2662 * nexus has been reestablished,
2664 * Inputs : instance - this instance of the NCR5380.
2666 * Locks: io_request_lock held by caller
2669 static void NCR5380_reselect(struct Scsi_Host *instance) {
2670 NCR5380_local_declare();
2671 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2673 unsigned char target_mask;
2674 unsigned char lun, phase;
2676 unsigned char msg[3];
2677 unsigned char *data;
2678 Scsi_Cmnd *tmp = NULL, *prev;
2680 NCR5380_setup(instance);
2683 * Disable arbitration, etc. since the host adapter obviously
2684 * lost, and tell an interrupted NCR5380_select() to restart.
2687 NCR5380_write(MODE_REG, MR_BASE);
2688 hostdata->restart_select = 1;
2690 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2691 dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no));
2694 * At this point, we have detected that our SCSI ID is on the bus,
2695 * SEL is true and BSY was false for at least one bus settle delay
2698 * We must assert BSY ourselves, until the target drops the SEL
2702 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2704 while (NCR5380_read(STATUS_REG) & SR_SEL);
2705 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2708 * Wait for target to go into MSGIN.
2711 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2715 phase = PHASE_MSGIN;
2716 NCR5380_transfer_pio(instance, &phase, &len, &data);
2718 if (!msg[0] & 0x80) {
2719 printk("scsi%d : expecting IDENTIFY message, got ", instance->host_no);
2723 /* Accept message by clearing ACK */
2724 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2725 lun = (msg[0] & 0x07);
2728 * We need to add code for SCSI-II to track which devices have
2729 * I_T_L_Q nexuses established, and which have simple I_T_L
2730 * nexuses so we can chose to do additional data transfer.
2734 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2735 * just reestablished, and remove it from the disconnected queue.
2739 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
2740 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2743 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2744 prev->host_scribble = tmp->host_scribble;
2746 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2747 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2749 tmp->host_scribble = NULL;
2753 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2755 * Since we have an established nexus that we can't do anything with,
2765 hostdata->connected = tmp;
2766 dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->target, tmp->lun, tmp->tag));
2771 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2773 * Purpose : called by interrupt handler when DMA finishes or a phase
2774 * mismatch occurs (which would finish the DMA transfer).
2776 * Inputs : instance - this instance of the NCR5380.
2778 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2779 * nexus has been reestablished, on failure NULL is returned.
2783 static void NCR5380_dma_complete(NCR5380_instance * instance) {
2784 NCR5380_local_declare();
2785 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata * instance->hostdata);
2787 NCR5380_setup(instance);
2790 * XXX this might not be right.
2792 * Wait for final byte to transfer, ie wait for ACK to go false.
2794 * We should use the Last Byte Sent bit, unfortunately this is
2795 * not available on the 5380/5381 (only the various CMOS chips)
2798 while (NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2800 NCR5380_write(MODE_REG, MR_BASE);
2801 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2804 * The only places we should see a phase mismatch and have to send
2805 * data from the same set of pointers will be the data transfer
2806 * phases. So, residual, requested length are only important here.
2809 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2810 transferred = instance->dmalen - NCR5380_dma_residual();
2811 hostdata->connected->SCp.this_residual -= transferred;
2812 hostdata->connected->SCp.ptr += transferred;
2815 #endif /* def REAL_DMA */
2818 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2820 * Purpose : abort a command
2822 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2823 * host byte of the result field to, if zero DID_ABORTED is
2826 * Returns : 0 - success, -1 on failure.
2828 * XXX - there is no way to abort the command that is currently
2829 * connected, you have to wait for it to complete. If this is
2830 * a problem, we could implement longjmp() / setjmp(), setjmp()
2831 * called where the loop started in NCR5380_main().
2833 * Locks: io_request_lock held by caller
2836 #ifndef NCR5380_abort
2839 int NCR5380_abort(Scsi_Cmnd * cmd) {
2840 NCR5380_local_declare();
2841 struct Scsi_Host *instance = cmd->host;
2842 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2843 Scsi_Cmnd *tmp, **prev;
2845 printk("scsi%d : aborting command\n", instance->host_no);
2846 print_Scsi_Cmnd(cmd);
2848 NCR5380_print_status(instance);
2850 printk("scsi%d : aborting command\n", instance->host_no);
2851 print_Scsi_Cmnd(cmd);
2853 NCR5380_print_status(instance);
2855 NCR5380_setup(instance);
2857 dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no));
2858 dprintk(NDEBUG_ABORT, (" basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)));
2862 * Case 1 : If the command is the currently executing command,
2863 * we'll set the aborted flag and return control so that
2864 * information transfer routine can exit cleanly.
2867 if (hostdata->connected == cmd) {
2868 dprintk(NDEBUG_ABORT, ("scsi%d : aborting connected command\n", instance->host_no));
2869 hostdata->aborted = 1;
2871 * We should perform BSY checking, and make sure we haven't slipped
2875 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2877 * Since we can't change phases until we've completed the current
2878 * handshake, we have to source or sink a byte of data if the current
2879 * phase is not MSGOUT.
2883 * Return control to the executing NCR drive so we can clear the
2884 * aborted flag and get back into our main loop.
2892 * Case 2 : If the command hasn't been issued yet, we simply remove it
2893 * from the issue queue.
2896 dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no));
2897 for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue), tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
2899 REMOVE(5, *prev, tmp, tmp->host_scribble);
2900 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2901 tmp->host_scribble = NULL;
2902 tmp->result = DID_ABORT << 16;
2903 dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no));
2905 return SCSI_ABORT_SUCCESS;
2907 #if (NDEBUG & NDEBUG_ABORT)
2909 else if (prev == tmp)
2910 printk("scsi%d : LOOP\n", instance->host_no);
2914 * Case 3 : If any commands are connected, we're going to fail the abort
2915 * and let the high level SCSI driver retry at a later time or
2918 * Timeouts, and therefore aborted commands, will be highly unlikely
2919 * and handling them cleanly in this situation would make the common
2920 * case of noresets less efficient, and would pollute our code. So,
2924 if (hostdata->connected) {
2925 dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no));
2926 return SCSI_ABORT_NOT_RUNNING;
2929 * Case 4: If the command is currently disconnected from the bus, and
2930 * there are no connected commands, we reconnect the I_T_L or
2931 * I_T_L_Q nexus associated with it, go into message out, and send
2934 * This case is especially ugly. In order to reestablish the nexus, we
2935 * need to call NCR5380_select(). The easiest way to implement this
2936 * function was to abort if the bus was busy, and let the interrupt
2937 * handler triggered on the SEL for reselect take care of lost arbitrations
2938 * where necessary, meaning interrupts need to be enabled.
2940 * When interrupts are enabled, the queues may change - so we
2941 * can't remove it from the disconnected queue before selecting it
2942 * because that could cause a failure in hashing the nexus if that
2943 * device reselected.
2945 * Since the queues may change, we can't use the pointers from when we
2948 * So, we must first locate the command, and if NCR5380_select()
2949 * succeeds, then issue the abort, relocate the command and remove
2950 * it from the disconnected queue.
2953 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
2955 dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no));
2957 if (NCR5380_select(instance, cmd, (int) cmd->tag))
2958 return SCSI_ABORT_BUSY;
2959 dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no));
2963 for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue), tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
2965 REMOVE(5, *prev, tmp, tmp->host_scribble);
2966 *prev = (Scsi_Cmnd *) tmp->host_scribble;
2967 tmp->host_scribble = NULL;
2968 tmp->result = DID_ABORT << 16;
2970 return SCSI_ABORT_SUCCESS;
2974 * Case 5 : If we reached this point, the command was not found in any of
2977 * We probably reached this point because of an unlikely race condition
2978 * between the command completing successfully and the abortion code,
2979 * so we won't panic, but we will notify the user in case something really
2982 printk("scsi%d : warning : SCSI command probably completed successfully\n" " before abortion\n", instance->host_no);
2983 return SCSI_ABORT_NOT_RUNNING;
2988 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
2990 * Purpose : reset the SCSI bus.
2992 * Returns : SCSI_RESET_WAKEUP
2994 * Locks: io_request_lock held by caller
2997 #ifndef NCR5380_reset
3000 int NCR5380_reset(Scsi_Cmnd * cmd, unsigned int dummy) {
3001 NCR5380_local_declare();
3002 NCR5380_setup(cmd->host);
3004 NCR5380_print_status(cmd->host);
3005 do_reset(cmd->host);
3007 return SCSI_RESET_WAKEUP;