MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / scsi / NCR5380.c
blob2032b6230e4f4302e447be9b6904d18d826fa03a
1 /*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
6 * Note that these routines also work with NR53c400 family chips.
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
14 * DISTRIBUTION RELEASE 6.
16 * For more information, please consult
18 * NCR 5380 Family
19 * SCSI Protocol Controller
20 * Databook
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
25 * 1+ (719) 578-3400
26 * 1+ (800) 334-5454
30 * $Log: NCR5380.c,v $
32 * Revision 1.10 1998/9/2 Alan Cox
33 * (alan@redhat.com)
34 * Fixed up the timer lockups reported so far. Things still suck. Looking
35 * forward to 2.3 and per device request queues. Then it'll be possible to
36 * SMP thread this beast and improve life no end.
38 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
39 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
40 * (hopefully) fixed and enhanced USLEEP
41 * added support for DTC3181E card (for Mustek scanner)
44 * Revision 1.8 Ingmar Baumgart
45 * (ingmar@gonzo.schwaben.de)
46 * added support for NCR53C400a card
49 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
50 * added proc_info
51 * added support needed for DTC 3180/3280
52 * fixed a couple of bugs
55 * Revision 1.5 1994/01/19 09:14:57 drew
56 * Fixed udelay() hack that was being used on DATAOUT phases
57 * instead of a proper wait for the final handshake.
59 * Revision 1.4 1994/01/19 06:44:25 drew
60 * *** empty log message ***
62 * Revision 1.3 1994/01/19 05:24:40 drew
63 * Added support for TCR LAST_BYTE_SENT bit.
65 * Revision 1.2 1994/01/15 06:14:11 drew
66 * REAL DMA support, bug fixes.
68 * Revision 1.1 1994/01/15 06:00:54 drew
69 * Initial revision
74 * Further development / testing that should be done :
75 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
76 * code so that everything does the same thing that's done at the
77 * end of a pseudo-DMA read operation.
79 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
80 * basically, transfer size needs to be reduced by one
81 * and the last byte read as is done with PSEUDO_DMA.
83 * 4. Test SCSI-II tagged queueing (I have no devices which support
84 * tagged queueing)
86 * 5. Test linked command handling code after Eric is ready with
87 * the high level code.
90 #if (NDEBUG & NDEBUG_LISTS)
91 #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
92 #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); }
93 #else
94 #define LIST(x,y)
95 #define REMOVE(w,x,y,z)
96 #endif
98 #ifndef notyet
99 #undef LINKED
100 #undef REAL_DMA
101 #endif
103 #ifdef REAL_DMA_POLL
104 #undef READ_OVERRUNS
105 #define READ_OVERRUNS
106 #endif
108 #ifdef BOARD_REQUIRES_NO_DELAY
109 #define io_recovery_delay(x)
110 #else
111 #define io_recovery_delay(x) udelay(x)
112 #endif
115 * Design
116 * Issues :
118 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
119 * and specifically for each board rather than each chip. This makes their
120 * adaptation to platforms like the Mac (Some of which use NCR5380's)
121 * more difficult than it has to be.
123 * Also, many of the SCSI drivers were written before the command queuing
124 * routines were implemented, meaning their implementations of queued
125 * commands were hacked on rather than designed in from the start.
127 * When I designed the Linux SCSI drivers I figured that
128 * while having two different SCSI boards in a system might be useful
129 * for debugging things, two of the same type wouldn't be used.
130 * Well, I was wrong and a number of users have mailed me about running
131 * multiple high-performance SCSI boards in a server.
133 * Finally, when I get questions from users, I have no idea what
134 * revision of my driver they are running.
136 * This driver attempts to address these problems :
137 * This is a generic 5380 driver. To use it on a different platform,
138 * one simply writes appropriate system specific macros (ie, data
139 * transfer - some PC's will use the I/O bus, 68K's must use
140 * memory mapped) and drops this file in their 'C' wrapper.
142 * As far as command queueing, two queues are maintained for
143 * each 5380 in the system - commands that haven't been issued yet,
144 * and commands that are currently executing. This means that an
145 * unlimited number of commands may be queued, letting
146 * more commands propagate from the higher driver levels giving higher
147 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
148 * allowing multiple commands to propagate all the way to a SCSI-II device
149 * while a command is already executing.
151 * To solve the multiple-boards-in-the-same-system problem,
152 * there is a separate instance structure for each instance
153 * of a 5380 in the system. So, multiple NCR5380 drivers will
154 * be able to coexist with appropriate changes to the high level
155 * SCSI code.
157 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
158 * number (updated for each public release) printed by the
159 * NCR5380_print_options command, which should be called from the
160 * wrapper detect function, so that I know what release of the driver
161 * users are using.
163 * Issues specific to the NCR5380 :
165 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
166 * piece of hardware that requires you to sit in a loop polling for
167 * the REQ signal as long as you are connected. Some devices are
168 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
169 * while doing long seek operations.
171 * The workaround for this is to keep track of devices that have
172 * disconnected. If the device hasn't disconnected, for commands that
173 * should disconnect, we do something like
175 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
177 * Some tweaking of N and M needs to be done. An algorithm based
178 * on "time to data" would give the best results as long as short time
179 * to datas (ie, on the same track) were considered, however these
180 * broken devices are the exception rather than the rule and I'd rather
181 * spend my time optimizing for the normal case.
183 * Architecture :
185 * At the heart of the design is a coroutine, NCR5380_main,
186 * which is started when not running by the interrupt handler,
187 * timer, and queue command function. It attempts to establish
188 * I_T_L or I_T_L_Q nexuses by removing the commands from the
189 * issue queue and calling NCR5380_select() if a nexus
190 * is not established.
192 * Once a nexus is established, the NCR5380_information_transfer()
193 * phase goes through the various phases as instructed by the target.
194 * if the target goes into MSG IN and sends a DISCONNECT message,
195 * the command structure is placed into the per instance disconnected
196 * queue, and NCR5380_main tries to find more work. If the target is
197 * idle for too long, the system 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 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
248 * only really want to use this if you're having a problem with
249 * dropped characters during high speed communications, and even
250 * then, you're going to be better off twiddling with transfersize
251 * in the high level code.
253 * Defaults for these will be provided although the user may want to adjust
254 * these to allocate CPU resources to the SCSI driver or "real" code.
256 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
258 * USLEEP_POLL - amount of time, in jiffies, to poll
260 * These macros MUST be defined :
261 * NCR5380_local_declare() - declare any local variables needed for your
262 * transfer routines.
264 * NCR5380_setup(instance) - initialize any local variables needed from a given
265 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
267 * NCR5380_read(register) - read from the specified register
269 * NCR5380_write(register, value) - write to the specific register
271 * NCR5380_implementation_fields - additional fields needed for this
272 * specific implementation of the NCR5380
274 * Either real DMA *or* pseudo DMA may be implemented
275 * REAL functions :
276 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
277 * Note that the DMA setup functions should return the number of bytes
278 * that they were able to program the controller for.
280 * Also note that generic i386/PC versions of these macros are
281 * available as NCR5380_i386_dma_write_setup,
282 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
284 * NCR5380_dma_write_setup(instance, src, count) - initialize
285 * NCR5380_dma_read_setup(instance, dst, count) - initialize
286 * NCR5380_dma_residual(instance); - residual count
288 * PSEUDO functions :
289 * NCR5380_pwrite(instance, src, count)
290 * NCR5380_pread(instance, dst, count);
292 * If nothing specific to this implementation needs doing (ie, with external
293 * hardware), you must also define
295 * NCR5380_queue_command
296 * NCR5380_reset
297 * NCR5380_abort
298 * NCR5380_proc_info
300 * to be the global entry points into the specific driver, ie
301 * #define NCR5380_queue_command t128_queue_command.
303 * If this is not done, the routines will be defined as static functions
304 * with the NCR5380* names and the user must provide a globally
305 * accessible wrapper function.
307 * The generic driver is initialized by calling NCR5380_init(instance),
308 * after setting the appropriate host specific fields and ID. If the
309 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
310 * possible) function may be used. Before the specific driver initialization
311 * code finishes, NCR5380_print_options should be called.
313 static int do_abort(struct Scsi_Host *host);
314 static void do_reset(struct Scsi_Host *host);
317 * initialize_SCp - init the scsi pointer field
318 * @cmd: command block to set up
320 * Set up the internal fields in the SCSI command.
323 static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
326 * Initialize the Scsi Pointer field so that all of the commands in the
327 * various queues are valid.
330 if (cmd->use_sg) {
331 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
332 cmd->SCp.buffers_residual = cmd->use_sg - 1;
333 cmd->SCp.ptr = page_address(cmd->SCp.buffer->page)+
334 cmd->SCp.buffer->offset;
335 cmd->SCp.this_residual = cmd->SCp.buffer->length;
336 } else {
337 cmd->SCp.buffer = NULL;
338 cmd->SCp.buffers_residual = 0;
339 cmd->SCp.ptr = (char *) cmd->request_buffer;
340 cmd->SCp.this_residual = cmd->request_bufflen;
345 * NCR5380_poll_politely - wait for NCR5380 status bits
346 * @instance: controller to poll
347 * @reg: 5380 register to poll
348 * @bit: Bitmask to check
349 * @val: Value required to exit
351 * Polls the NCR5380 in a reasonably efficient manner waiting for
352 * an event to occur, after a short quick poll we begin giving the
353 * CPU back in non IRQ contexts
355 * Returns the value of the register or a negative error code.
358 static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
360 NCR5380_local_declare();
361 int n = 500; /* At about 8uS a cycle for the cpu access */
362 unsigned long end = jiffies + t;
363 int r;
365 NCR5380_setup(instance);
367 while( n-- > 0)
369 r = NCR5380_read(reg);
370 if((r & bit) == val)
371 return 0;
372 cpu_relax();
375 /* t time yet ? */
376 while(time_before(jiffies, end))
378 r = NCR5380_read(reg);
379 if((r & bit) == val)
380 return 0;
381 if(!in_interrupt())
382 yield();
383 else
384 cpu_relax();
386 return -ETIMEDOUT;
389 static struct {
390 unsigned char value;
391 const char *name;
392 } phases[] = {
393 {PHASE_DATAOUT, "DATAOUT"},
394 {PHASE_DATAIN, "DATAIN"},
395 {PHASE_CMDOUT, "CMDOUT"},
396 {PHASE_STATIN, "STATIN"},
397 {PHASE_MSGOUT, "MSGOUT"},
398 {PHASE_MSGIN, "MSGIN"},
399 {PHASE_UNKNOWN, "UNKNOWN"}
402 #ifdef NDEBUG
403 static struct {
404 unsigned char mask;
405 const char *name;
406 } signals[] = {
407 {SR_DBP, "PARITY"},
408 {SR_RST, "RST"},
409 {SR_BSY, "BSY"},
410 {SR_REQ, "REQ"},
411 {SR_MSG, "MSG"},
412 {SR_CD, "CD"},
413 {SR_IO, "IO"},
414 {SR_SEL, "SEL"},
415 {0, NULL}
417 basrs[] = {
418 {BASR_ATN, "ATN"},
419 {BASR_ACK, "ACK"},
420 {0, NULL}
422 icrs[] = {
423 {ICR_ASSERT_RST, "ASSERT RST"},
424 {ICR_ASSERT_ACK, "ASSERT ACK"},
425 {ICR_ASSERT_BSY, "ASSERT BSY"},
426 {ICR_ASSERT_SEL, "ASSERT SEL"},
427 {ICR_ASSERT_ATN, "ASSERT ATN"},
428 {ICR_ASSERT_DATA, "ASSERT DATA"},
429 {0, NULL}
431 mrs[] = {
432 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
433 {MR_TARGET, "MODE TARGET"},
434 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
435 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
436 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
437 {MR_DMA_MODE, "MODE DMA"},
438 {MR_ARBITRATE, "MODE ARBITRATION"},
439 {0, NULL}
443 * NCR5380_print - print scsi bus signals
444 * @instance: adapter state to dump
446 * Print the SCSI bus signals for debugging purposes
448 * Locks: caller holds hostdata lock (not essential)
451 static void NCR5380_print(struct Scsi_Host *instance)
453 NCR5380_local_declare();
454 unsigned char status, data, basr, mr, icr, i;
455 NCR5380_setup(instance);
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);
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");
484 * NCR5380_print_phase - show SCSI phase
485 * @instance: adapter to dump
487 * Print the current SCSI phase for debugging purposes
489 * Locks: none
492 static void NCR5380_print_phase(struct Scsi_Host *instance)
494 NCR5380_local_declare();
495 unsigned char status;
496 int i;
497 NCR5380_setup(instance);
499 status = NCR5380_read(STATUS_REG);
500 if (!(status & SR_REQ))
501 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
502 else {
503 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
504 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
507 #endif
510 * These need tweaking, and would probably work best as per-device
511 * flags initialized differently for disk, tape, cd, etc devices.
512 * People with broken devices are free to experiment as to what gives
513 * the best results for them.
515 * USLEEP_SLEEP should be a minimum seek time.
517 * USLEEP_POLL should be a maximum rotational latency.
519 #ifndef USLEEP_SLEEP
520 /* 20 ms (reasonable hard disk speed) */
521 #define USLEEP_SLEEP (20*HZ/1000)
522 #endif
523 /* 300 RPM (floppy speed) */
524 #ifndef USLEEP_POLL
525 #define USLEEP_POLL (200*HZ/1000)
526 #endif
527 #ifndef USLEEP_WAITLONG
528 /* RvC: (reasonable time to wait on select error) */
529 #define USLEEP_WAITLONG USLEEP_SLEEP
530 #endif
533 * Function : int should_disconnect (unsigned char cmd)
535 * Purpose : decide weather a command would normally disconnect or
536 * not, since if it won't disconnect we should go to sleep.
538 * Input : cmd - opcode of SCSI command
540 * Returns : DISCONNECT_LONG if we should disconnect for a really long
541 * time (ie always, sleep, look for REQ active, sleep),
542 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
543 * time-to-data delay, DISCONNECT_NONE if this command would return
544 * immediately.
546 * Future sleep algorithms based on time to data can exploit
547 * something like this so they can differentiate between "normal"
548 * (ie, read, write, seek) and unusual commands (ie, * format).
550 * Note : We don't deal with commands that handle an immediate disconnect,
554 static int should_disconnect(unsigned char cmd)
556 switch (cmd) {
557 case READ_6:
558 case WRITE_6:
559 case SEEK_6:
560 case READ_10:
561 case WRITE_10:
562 case SEEK_10:
563 return DISCONNECT_TIME_TO_DATA;
564 case FORMAT_UNIT:
565 case SEARCH_HIGH:
566 case SEARCH_LOW:
567 case SEARCH_EQUAL:
568 return DISCONNECT_LONG;
569 default:
570 return DISCONNECT_NONE;
574 static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
576 hostdata->time_expires = jiffies + timeout;
577 schedule_delayed_work(&hostdata->coroutine, hostdata->time_expires);
581 static int probe_irq __initdata = 0;
584 * probe_intr - helper for IRQ autoprobe
585 * @irq: interrupt number
586 * @dev_id: unused
587 * @regs: unused
589 * Set a flag to indicate the IRQ in question was received. This is
590 * used by the IRQ probe code.
593 static irqreturn_t __init probe_intr(int irq, void *dev_id,
594 struct pt_regs *regs)
596 probe_irq = irq;
597 return IRQ_HANDLED;
601 * NCR5380_probe_irq - find the IRQ of an NCR5380
602 * @instance: NCR5380 controller
603 * @possible: bitmask of ISA IRQ lines
605 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
606 * and then looking to see what interrupt actually turned up.
608 * Locks: none, irqs must be enabled on entry
611 static int __init NCR5380_probe_irq(struct Scsi_Host *instance, int possible)
613 NCR5380_local_declare();
614 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
615 unsigned long timeout;
616 int trying_irqs, i, mask;
617 NCR5380_setup(instance);
619 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
620 if ((mask & possible) && (request_irq(i, &probe_intr, SA_INTERRUPT, "NCR-probe", NULL) == 0))
621 trying_irqs |= mask;
623 timeout = jiffies + (250 * HZ / 1000);
624 probe_irq = SCSI_IRQ_NONE;
627 * A interrupt is triggered whenever BSY = false, SEL = true
628 * and a bit set in the SELECT_ENABLE_REG is asserted on the
629 * SCSI bus.
631 * Note that the bus is only driven when the phase control signals
632 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
633 * to zero.
636 NCR5380_write(TARGET_COMMAND_REG, 0);
637 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
638 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
639 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
641 while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
643 set_current_state(TASK_UNINTERRUPTIBLE);
644 schedule_timeout(1);
647 NCR5380_write(SELECT_ENABLE_REG, 0);
648 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
650 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
651 if (trying_irqs & mask)
652 free_irq(i, NULL);
654 return probe_irq;
658 * NCR58380_print_options - show options
659 * @instance: unused for now
661 * Called by probe code indicating the NCR5380 driver options that
662 * were selected. At some point this will switch to runtime options
663 * read from the adapter in question
665 * Locks: none
668 static void __init NCR5380_print_options(struct Scsi_Host *instance)
670 printk(" generic options"
671 #ifdef AUTOPROBE_IRQ
672 " AUTOPROBE_IRQ"
673 #endif
674 #ifdef AUTOSENSE
675 " AUTOSENSE"
676 #endif
677 #ifdef DIFFERENTIAL
678 " DIFFERENTIAL"
679 #endif
680 #ifdef REAL_DMA
681 " REAL DMA"
682 #endif
683 #ifdef REAL_DMA_POLL
684 " REAL DMA POLL"
685 #endif
686 #ifdef PARITY
687 " PARITY"
688 #endif
689 #ifdef PSEUDO_DMA
690 " PSEUDO DMA"
691 #endif
692 #ifdef UNSAFE
693 " UNSAFE "
694 #endif
696 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
697 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
698 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
699 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
704 * NCR5380_print_status - dump controller info
705 * @instance: controller to dump
707 * Print commands in the various queues, called from NCR5380_abort
708 * and NCR5380_debug to aid debugging.
710 * Locks: called functions disable irqs
713 static void NCR5380_print_status(struct Scsi_Host *instance)
715 static char pr_bfr[512];
716 char *start;
717 int len;
719 NCR5380_dprint(NDEBUG_ANY, instance);
720 NCR5380_dprint_phase(NDEBUG_ANY, instance);
722 len = NCR5380_proc_info(instance, pr_bfr, &start, 0, sizeof(pr_bfr), 0);
723 pr_bfr[len] = 0;
724 printk("\n%s\n", pr_bfr);
727 /******************************************/
729 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
731 * *buffer: I/O buffer
732 * **start: if inout == FALSE pointer into buffer where user read should start
733 * offset: current offset
734 * length: length of buffer
735 * hostno: Scsi_Host host_no
736 * inout: TRUE - user is writing; FALSE - user is reading
738 * Return the number of bytes read from or written
741 #undef SPRINTF
742 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
743 static
744 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
745 static
746 char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
747 static
748 char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
750 static
751 int NCR5380_proc_info(struct Scsi_Host *instance, char *buffer, char **start, off_t offset, int length, int inout)
753 char *pos = buffer;
754 struct NCR5380_hostdata *hostdata;
755 Scsi_Cmnd *ptr;
757 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
759 if (inout) { /* Has data been written to the file ? */
760 #ifdef DTC_PUBLIC_RELEASE
761 dtc_wmaxi = dtc_maxi = 0;
762 #endif
763 #ifdef PAS16_PUBLIC_RELEASE
764 pas_wmaxi = pas_maxi = 0;
765 #endif
766 return (-ENOSYS); /* Currently this is a no-op */
768 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
769 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
770 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
771 #ifdef DTC_PUBLIC_RELEASE
772 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
773 #endif
774 #ifdef T128_PUBLIC_RELEASE
775 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
776 #endif
777 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
778 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
779 #endif
780 #ifdef PAS16_PUBLIC_RELEASE
781 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
782 #endif
784 SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
785 SPRINTF("io_port: %04x ", (int) instance->io_port);
786 if (instance->irq == SCSI_IRQ_NONE)
787 SPRINTF("IRQ: None.\n");
788 else
789 SPRINTF("IRQ: %d.\n", instance->irq);
791 #ifdef DTC_PUBLIC_RELEASE
792 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi);
793 #endif
794 #ifdef PAS16_PUBLIC_RELEASE
795 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi);
796 #endif
797 spin_lock_irq(instance->host_lock);
798 if (!hostdata->connected)
799 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
800 else
801 pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, pos, buffer, length);
802 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
803 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
804 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
806 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
807 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
808 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
809 spin_unlock_irq(instance->host_lock);
811 *start = buffer;
812 if (pos - buffer < offset)
813 return 0;
814 else if (pos - buffer - offset < length)
815 return pos - buffer - offset;
816 return length;
819 static char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
821 SPRINTF("scsi%d : destination target %d, lun %d\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
822 SPRINTF(" command = ");
823 pos = lprint_command(cmd->cmnd, pos, buffer, length);
824 return (pos);
827 static char *lprint_command(unsigned char *command, char *pos, char *buffer, int length)
829 int i, s;
830 pos = lprint_opcode(command[0], pos, buffer, length);
831 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
832 SPRINTF("%02x ", command[i]);
833 SPRINTF("\n");
834 return (pos);
837 static char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
839 SPRINTF("%2d (0x%02x)", opcode, opcode);
840 return (pos);
845 * NCR5380_init - initialise an NCR5380
846 * @instance: adapter to configure
847 * @flags: control flags
849 * Initializes *instance and corresponding 5380 chip,
850 * with flags OR'd into the initial flags value.
852 * Notes : I assume that the host, hostno, and id bits have been
853 * set correctly. I don't care about the irq and other fields.
855 * Returns 0 for success
857 * Locks: interrupts must be enabled when we are called
860 static int __devinit NCR5380_init(struct Scsi_Host *instance, int flags)
862 NCR5380_local_declare();
863 int i, pass;
864 unsigned long timeout;
865 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
867 if(in_interrupt())
868 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
870 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
871 * the base address.
874 #ifdef NCR53C400
875 if (flags & FLAG_NCR53C400)
876 instance->NCR5380_instance_name += NCR53C400_address_adjust;
877 #endif
879 NCR5380_setup(instance);
881 hostdata->aborted = 0;
882 hostdata->id_mask = 1 << instance->this_id;
883 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
884 if (i > hostdata->id_mask)
885 hostdata->id_higher_mask |= i;
886 for (i = 0; i < 8; ++i)
887 hostdata->busy[i] = 0;
888 #ifdef REAL_DMA
889 hostdata->dmalen = 0;
890 #endif
891 hostdata->targets_present = 0;
892 hostdata->connected = NULL;
893 hostdata->issue_queue = NULL;
894 hostdata->disconnected_queue = NULL;
896 INIT_WORK(&hostdata->coroutine, NCR5380_main, hostdata);
898 #ifdef NCR5380_STATS
899 for (i = 0; i < 8; ++i) {
900 hostdata->time_read[i] = 0;
901 hostdata->time_write[i] = 0;
902 hostdata->bytes_read[i] = 0;
903 hostdata->bytes_write[i] = 0;
905 hostdata->timebase = 0;
906 hostdata->pendingw = 0;
907 hostdata->pendingr = 0;
908 #endif
910 /* The CHECK code seems to break the 53C400. Will check it later maybe */
911 if (flags & FLAG_NCR53C400)
912 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
913 else
914 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
916 hostdata->host = instance;
917 hostdata->time_expires = 0;
919 #ifndef AUTOSENSE
920 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
921 printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" " without AUTOSENSE option, contingent allegiance conditions may\n"
922 " be incorrectly cleared.\n", instance->host_no);
923 #endif /* def AUTOSENSE */
925 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
926 NCR5380_write(MODE_REG, MR_BASE);
927 NCR5380_write(TARGET_COMMAND_REG, 0);
928 NCR5380_write(SELECT_ENABLE_REG, 0);
930 #ifdef NCR53C400
931 if (hostdata->flags & FLAG_NCR53C400) {
932 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
934 #endif
937 * Detect and correct bus wedge problems.
939 * If the system crashed, it may have crashed in a state
940 * where a SCSI command was still executing, and the
941 * SCSI bus is not in a BUS FREE STATE.
943 * If this is the case, we'll try to abort the currently
944 * established nexus which we know nothing about, and that
945 * failing, do a hard reset of the SCSI bus
948 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
949 switch (pass) {
950 case 1:
951 case 3:
952 case 5:
953 printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
954 timeout = jiffies + 5 * HZ;
955 NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ);
956 break;
957 case 2:
958 printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no);
959 do_abort(instance);
960 break;
961 case 4:
962 printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no);
963 do_reset(instance);
964 break;
965 case 6:
966 printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no);
967 return -ENXIO;
970 return 0;
974 * NCR5380_exit - remove an NCR5380
975 * @instance: adapter to remove
978 static void __devexit NCR5380_exit(struct Scsi_Host *instance)
980 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
982 cancel_delayed_work(&hostdata->coroutine);
983 flush_scheduled_work();
987 * NCR5380_queue_command - queue a command
988 * @cmd: SCSI command
989 * @done: completion handler
991 * cmd is added to the per instance issue_queue, with minor
992 * twiddling done to the host specific fields of cmd. If the
993 * main coroutine is not running, it is restarted.
995 * Locks: host lock taken by caller
998 static int NCR5380_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
1000 struct Scsi_Host *instance = cmd->device->host;
1001 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1002 Scsi_Cmnd *tmp;
1004 #if (NDEBUG & NDEBUG_NO_WRITE)
1005 switch (cmd->cmnd[0]) {
1006 case WRITE_6:
1007 case WRITE_10:
1008 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
1009 cmd->result = (DID_ERROR << 16);
1010 done(cmd);
1011 return 0;
1013 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1015 #ifdef NCR5380_STATS
1016 switch (cmd->cmnd[0]) {
1017 case WRITE:
1018 case WRITE_6:
1019 case WRITE_10:
1020 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
1021 hostdata->bytes_write[cmd->device->id] += cmd->request_bufflen;
1022 hostdata->pendingw++;
1023 break;
1024 case READ:
1025 case READ_6:
1026 case READ_10:
1027 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
1028 hostdata->bytes_read[cmd->device->id] += cmd->request_bufflen;
1029 hostdata->pendingr++;
1030 break;
1032 #endif
1035 * We use the host_scribble field as a pointer to the next command
1036 * in a queue
1039 cmd->host_scribble = NULL;
1040 cmd->scsi_done = done;
1041 cmd->result = 0;
1044 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1045 * commands are added to the head of the queue since any command will
1046 * clear the contingent allegiance condition that exists and the
1047 * sense data is only guaranteed to be valid while the condition exists.
1050 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1051 LIST(cmd, hostdata->issue_queue);
1052 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
1053 hostdata->issue_queue = cmd;
1054 } else {
1055 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
1056 LIST(cmd, tmp);
1057 tmp->host_scribble = (unsigned char *) cmd;
1059 dprintk(NDEBUG_QUEUES, ("scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
1061 /* Run the coroutine if it isn't already running. */
1062 /* Kick off command processing */
1063 schedule_work(&hostdata->coroutine);
1064 return 0;
1069 * NCR5380_main - NCR state machines
1071 * NCR5380_main is a coroutine that runs as long as more work can
1072 * be done on the NCR5380 host adapters in a system. Both
1073 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1074 * in case it is not running.
1076 * Locks: called as its own thread with no locks held. Takes the
1077 * host lock and called routines may take the isa dma lock.
1080 static void NCR5380_main(void *p)
1082 struct NCR5380_hostdata *hostdata = p;
1083 struct Scsi_Host *instance = hostdata->host;
1084 Scsi_Cmnd *tmp, *prev;
1085 int done;
1087 spin_lock_irq(instance->host_lock);
1088 do {
1089 /* Lock held here */
1090 done = 1;
1091 if (!hostdata->connected && !hostdata->selecting) {
1092 dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no));
1094 * Search through the issue_queue for a command destined
1095 * for a target that's not busy.
1097 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
1099 if (prev != tmp)
1100 dprintk(NDEBUG_LISTS, ("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun));
1101 /* When we find one, remove it from the issue queue. */
1102 if (!(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))) {
1103 if (prev) {
1104 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
1105 prev->host_scribble = tmp->host_scribble;
1106 } else {
1107 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
1108 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
1110 tmp->host_scribble = NULL;
1113 * Attempt to establish an I_T_L nexus here.
1114 * On success, instance->hostdata->connected is set.
1115 * On failure, we must add the command back to the
1116 * issue queue so we can keep trying.
1118 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));
1121 * A successful selection is defined as one that
1122 * leaves us with the command connected and
1123 * in hostdata->connected, OR has terminated the
1124 * command.
1126 * With successful commands, we fall through
1127 * and see if we can do an information transfer,
1128 * with failures we will restart.
1130 hostdata->selecting = NULL;
1131 /* RvC: have to preset this to indicate a new command is being performed */
1133 if (!NCR5380_select(instance, tmp,
1135 * REQUEST SENSE commands are issued without tagged
1136 * queueing, even on SCSI-II devices because the
1137 * contingent allegiance condition exists for the
1138 * entire unit.
1140 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
1141 break;
1142 } else {
1143 LIST(tmp, hostdata->issue_queue);
1144 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1145 hostdata->issue_queue = tmp;
1146 done = 0;
1147 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no));
1149 /* lock held here still */
1150 } /* if target/lun is not busy */
1151 } /* for */
1152 /* exited locked */
1153 } /* if (!hostdata->connected) */
1154 if (hostdata->selecting) {
1155 tmp = (Scsi_Cmnd *) hostdata->selecting;
1156 /* Selection will drop and retake the lock */
1157 if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
1158 /* Ok ?? */
1159 } else {
1160 /* RvC: device failed, so we wait a long time
1161 this is needed for Mustek scanners, that
1162 do not respond to commands immediately
1163 after a scan */
1164 printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
1165 LIST(tmp, hostdata->issue_queue);
1166 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1167 hostdata->issue_queue = tmp;
1168 NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
1170 } /* if hostdata->selecting */
1171 if (hostdata->connected
1172 #ifdef REAL_DMA
1173 && !hostdata->dmalen
1174 #endif
1175 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1177 dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no));
1178 NCR5380_information_transfer(instance);
1179 dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no));
1180 done = 0;
1181 } else
1182 break;
1183 } while (!done);
1185 spin_unlock_irq(instance->host_lock);
1188 #ifndef DONT_USE_INTR
1191 * NCR5380_intr - generic NCR5380 irq handler
1192 * @irq: interrupt number
1193 * @dev_id: device info
1194 * @regs: registers (unused)
1196 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1197 * from the disconnected queue, and restarting NCR5380_main()
1198 * as required.
1200 * Locks: takes the needed instance locks
1203 static irqreturn_t NCR5380_intr(int irq, void *dev_id, struct pt_regs *regs)
1205 NCR5380_local_declare();
1206 struct Scsi_Host *instance = (struct Scsi_Host *)dev_id;
1207 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1208 int done;
1209 unsigned char basr;
1210 unsigned long flags;
1212 dprintk(NDEBUG_INTR, ("scsi : NCR5380 irq %d triggered\n", irq));
1214 do {
1215 done = 1;
1216 spin_lock_irqsave(instance->host_lock, flags);
1217 /* Look for pending interrupts */
1218 NCR5380_setup(instance);
1219 basr = NCR5380_read(BUS_AND_STATUS_REG);
1220 /* XXX dispatch to appropriate routine if found and done=0 */
1221 if (basr & BASR_IRQ) {
1222 NCR5380_dprint(NDEBUG_INTR, instance);
1223 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1224 done = 0;
1225 dprintk(NDEBUG_INTR, ("scsi%d : SEL interrupt\n", instance->host_no));
1226 NCR5380_reselect(instance);
1227 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1228 } else if (basr & BASR_PARITY_ERROR) {
1229 dprintk(NDEBUG_INTR, ("scsi%d : PARITY interrupt\n", instance->host_no));
1230 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1231 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1232 dprintk(NDEBUG_INTR, ("scsi%d : RESET interrupt\n", instance->host_no));
1233 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1234 } else {
1235 #if defined(REAL_DMA)
1237 * We should only get PHASE MISMATCH and EOP interrupts
1238 * if we have DMA enabled, so do a sanity check based on
1239 * the current setting of the MODE register.
1242 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
1243 int transfered;
1245 if (!hostdata->connected)
1246 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1248 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1249 hostdata->connected->SCp.this_residual -= transferred;
1250 hostdata->connected->SCp.ptr += transferred;
1251 hostdata->dmalen = 0;
1253 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1255 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1256 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1258 NCR5380_write(MODE_REG, MR_BASE);
1259 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1261 #else
1262 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)));
1263 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1264 #endif
1266 } /* if BASR_IRQ */
1267 spin_unlock_irqrestore(instance->host_lock, flags);
1268 if(!done)
1269 schedule_work(&hostdata->coroutine);
1270 } while (!done);
1271 return IRQ_HANDLED;
1274 #endif
1277 * collect_stats - collect stats on a scsi command
1278 * @hostdata: adapter
1279 * @cmd: command being issued
1281 * Update the statistical data by parsing the command in question
1284 static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
1286 #ifdef NCR5380_STATS
1287 switch (cmd->cmnd[0]) {
1288 case WRITE:
1289 case WRITE_6:
1290 case WRITE_10:
1291 hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
1292 hostdata->pendingw--;
1293 break;
1294 case READ:
1295 case READ_6:
1296 case READ_10:
1297 hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
1298 hostdata->pendingr--;
1299 break;
1301 #endif
1306 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1307 * int tag);
1309 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1310 * including ARBITRATION, SELECTION, and initial message out for
1311 * IDENTIFY and queue messages.
1313 * Inputs : instance - instantiation of the 5380 driver on which this
1314 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1315 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1316 * the command that is presently connected.
1318 * Returns : -1 if selection could not execute for some reason,
1319 * 0 if selection succeeded or failed because the target
1320 * did not respond.
1322 * Side effects :
1323 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1324 * with registers as they should have been on entry - ie
1325 * SELECT_ENABLE will be set appropriately, the NCR5380
1326 * will cease to drive any SCSI bus signals.
1328 * If successful : I_T_L or I_T_L_Q nexus will be established,
1329 * instance->connected will be set to cmd.
1330 * SELECT interrupt will be disabled.
1332 * If failed (no target) : cmd->scsi_done() will be called, and the
1333 * cmd->result host byte set to DID_BAD_TARGET.
1335 * Locks: caller holds hostdata lock in IRQ mode
1338 static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
1340 NCR5380_local_declare();
1341 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1342 unsigned char tmp[3], phase;
1343 unsigned char *data;
1344 int len;
1345 unsigned long timeout;
1346 unsigned char value;
1347 int err;
1348 NCR5380_setup(instance);
1350 if (hostdata->selecting)
1351 goto part2;
1353 hostdata->restart_select = 0;
1355 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
1356 dprintk(NDEBUG_ARBITRATION, ("scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id));
1359 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1360 * data bus during SELECTION.
1363 NCR5380_write(TARGET_COMMAND_REG, 0);
1366 * Start arbitration.
1369 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1370 NCR5380_write(MODE_REG, MR_ARBITRATE);
1373 /* We can be relaxed here, interrupts are on, we are
1374 in workqueue context, the birds are singing in the trees */
1375 spin_unlock_irq(instance->host_lock);
1376 err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
1377 spin_lock_irq(instance->host_lock);
1378 if (err < 0) {
1379 printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
1380 NCR5380_write(MODE_REG, MR_BASE);
1381 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1382 goto failed;
1385 dprintk(NDEBUG_ARBITRATION, ("scsi%d : arbitration complete\n", instance->host_no));
1388 * The arbitration delay is 2.2us, but this is a minimum and there is
1389 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1390 * the integral nature of udelay().
1394 udelay(3);
1396 /* Check for lost arbitration */
1397 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)) {
1398 NCR5380_write(MODE_REG, MR_BASE);
1399 dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no));
1400 goto failed;
1402 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1404 if (!(hostdata->flags & FLAG_DTC3181E) &&
1405 /* RvC: DTC3181E has some trouble with this
1406 * so we simply removed it. Seems to work with
1407 * only Mustek scanner attached
1409 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1410 NCR5380_write(MODE_REG, MR_BASE);
1411 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1412 dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no));
1413 goto failed;
1416 * Again, bus clear + bus settle time is 1.2us, however, this is
1417 * a minimum so we'll udelay ceil(1.2)
1420 udelay(2);
1422 dprintk(NDEBUG_ARBITRATION, ("scsi%d : won arbitration\n", instance->host_no));
1425 * Now that we have won arbitration, start Selection process, asserting
1426 * the host and target ID's on the SCSI bus.
1429 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1432 * Raise ATN while SEL is true before BSY goes false from arbitration,
1433 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1434 * phase immediately after selection.
1437 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1438 NCR5380_write(MODE_REG, MR_BASE);
1441 * Reselect interrupts must be turned off prior to the dropping of BSY,
1442 * otherwise we will trigger an interrupt.
1444 NCR5380_write(SELECT_ENABLE_REG, 0);
1447 * The initiator shall then wait at least two deskew delays and release
1448 * the BSY signal.
1450 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1452 /* Reset BSY */
1453 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1456 * Something weird happens when we cease to drive BSY - looks
1457 * like the board/chip is letting us do another read before the
1458 * appropriate propagation delay has expired, and we're confusing
1459 * a BSY signal from ourselves as the target's response to SELECTION.
1461 * A small delay (the 'C++' frontend breaks the pipeline with an
1462 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1463 * tighter 'C' code breaks and requires this) solves the problem -
1464 * the 1 us delay is arbitrary, and only used because this delay will
1465 * be the same on other platforms and since it works here, it should
1466 * work there.
1468 * wingel suggests that this could be due to failing to wait
1469 * one deskew delay.
1472 udelay(1);
1474 dprintk(NDEBUG_SELECTION, ("scsi%d : selecting target %d\n", instance->host_no, cmd->device->id));
1477 * The SCSI specification calls for a 250 ms timeout for the actual
1478 * selection.
1481 timeout = jiffies + (250 * HZ / 1000);
1484 * XXX very interesting - we're seeing a bounce where the BSY we
1485 * asserted is being reflected / still asserted (propagation delay?)
1486 * and it's detecting as true. Sigh.
1489 hostdata->select_time = 0; /* we count the clock ticks at which we polled */
1490 hostdata->selecting = cmd;
1492 part2:
1493 /* RvC: here we enter after a sleeping period, or immediately after
1494 execution of part 1
1495 we poll only once ech clock tick */
1496 value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
1498 if (!value && (hostdata->select_time < HZ/4)) {
1499 /* RvC: we still must wait for a device response */
1500 hostdata->select_time++; /* after 25 ticks the device has failed */
1501 NCR5380_set_timer(hostdata, 1);
1502 return 0; /* RvC: we return here with hostdata->selecting set,
1503 to go to sleep */
1506 hostdata->selecting = NULL;/* clear this pointer, because we passed the
1507 waiting period */
1508 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1509 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1510 NCR5380_reselect(instance);
1511 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1512 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1513 return -1;
1516 * No less than two deskew delays after the initiator detects the
1517 * BSY signal is true, it shall release the SEL signal and may
1518 * change the DATA BUS. -wingel
1521 udelay(1);
1523 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1525 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1526 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1527 if (hostdata->targets_present & (1 << cmd->device->id)) {
1528 printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no);
1529 if (hostdata->restart_select)
1530 printk(KERN_DEBUG "\trestart select\n");
1531 NCR5380_dprint(NDEBUG_SELECTION, instance);
1532 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1533 return -1;
1535 cmd->result = DID_BAD_TARGET << 16;
1536 collect_stats(hostdata, cmd);
1537 cmd->scsi_done(cmd);
1538 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1539 dprintk(NDEBUG_SELECTION, ("scsi%d : target did not respond within 250ms\n", instance->host_no));
1540 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1541 return 0;
1543 hostdata->targets_present |= (1 << cmd->device->id);
1546 * Since we followed the SCSI spec, and raised ATN while SEL
1547 * was true but before BSY was false during selection, the information
1548 * transfer phase should be a MESSAGE OUT phase so that we can send the
1549 * IDENTIFY message.
1551 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1552 * message (2 bytes) with a tag ID that we increment with every command
1553 * until it wraps back to 0.
1555 * XXX - it turns out that there are some broken SCSI-II devices,
1556 * which claim to support tagged queuing but fail when more than
1557 * some number of commands are issued at once.
1560 /* Wait for start of REQ/ACK handshake */
1562 spin_unlock_irq(instance->host_lock);
1563 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1564 spin_lock_irq(instance->host_lock);
1566 if(err) {
1567 printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1568 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1569 goto failed;
1572 dprintk(NDEBUG_SELECTION, ("scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id));
1573 tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun);
1575 len = 1;
1576 cmd->tag = 0;
1578 /* Send message(s) */
1579 data = tmp;
1580 phase = PHASE_MSGOUT;
1581 NCR5380_transfer_pio(instance, &phase, &len, &data);
1582 dprintk(NDEBUG_SELECTION, ("scsi%d : nexus established.\n", instance->host_no));
1583 /* XXX need to handle errors here */
1584 hostdata->connected = cmd;
1585 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1587 if (cmd->SCp.ptr != (char *)cmd->sense_buffer) {
1588 initialize_SCp(cmd);
1591 return 0;
1593 /* Selection failed */
1594 failed:
1595 return -1;
1600 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1601 * unsigned char *phase, int *count, unsigned char **data)
1603 * Purpose : transfers data in given phase using polled I/O
1605 * Inputs : instance - instance of driver, *phase - pointer to
1606 * what phase is expected, *count - pointer to number of
1607 * bytes to transfer, **data - pointer to data pointer.
1609 * Returns : -1 when different phase is entered without transferring
1610 * maximum number of bytes, 0 if all bytes or transfered or exit
1611 * is in same phase.
1613 * Also, *phase, *count, *data are modified in place.
1615 * XXX Note : handling for bus free may be useful.
1619 * Note : this code is not as quick as it could be, however it
1620 * IS 100% reliable, and for the actual data transfer where speed
1621 * counts, we will always do a pseudo DMA or DMA transfer.
1624 static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1625 NCR5380_local_declare();
1626 unsigned char p = *phase, tmp;
1627 int c = *count;
1628 unsigned char *d = *data;
1630 * RvC: some administrative data to process polling time
1632 int break_allowed = 0;
1633 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1634 NCR5380_setup(instance);
1636 if (!(p & SR_IO))
1637 dprintk(NDEBUG_PIO, ("scsi%d : pio write %d bytes\n", instance->host_no, c));
1638 else
1639 dprintk(NDEBUG_PIO, ("scsi%d : pio read %d bytes\n", instance->host_no, c));
1642 * The NCR5380 chip will only drive the SCSI bus when the
1643 * phase specified in the appropriate bits of the TARGET COMMAND
1644 * REGISTER match the STATUS REGISTER
1647 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1649 /* RvC: don't know if this is necessary, but other SCSI I/O is short
1650 * so breaks are not necessary there
1652 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
1653 break_allowed = 1;
1655 do {
1657 * Wait for assertion of REQ, after which the phase bits will be
1658 * valid
1661 /* RvC: we simply poll once, after that we stop temporarily
1662 * and let the device buffer fill up
1663 * if breaking is not allowed, we keep polling as long as needed
1666 /* FIXME */
1667 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
1668 if (!(tmp & SR_REQ)) {
1669 /* timeout condition */
1670 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
1671 break;
1674 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : REQ detected\n", instance->host_no));
1676 /* Check for phase mismatch */
1677 if ((tmp & PHASE_MASK) != p) {
1678 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : phase mismatch\n", instance->host_no));
1679 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1680 break;
1682 /* Do actual transfer from SCSI bus to / from memory */
1683 if (!(p & SR_IO))
1684 NCR5380_write(OUTPUT_DATA_REG, *d);
1685 else
1686 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1688 ++d;
1691 * The SCSI standard suggests that in MSGOUT phase, the initiator
1692 * should drop ATN on the last byte of the message phase
1693 * after REQ has been asserted for the handshake but before
1694 * the initiator raises ACK.
1697 if (!(p & SR_IO)) {
1698 if (!((p & SR_MSG) && c > 1)) {
1699 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1700 NCR5380_dprint(NDEBUG_PIO, instance);
1701 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1702 } else {
1703 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1704 NCR5380_dprint(NDEBUG_PIO, instance);
1705 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1707 } else {
1708 NCR5380_dprint(NDEBUG_PIO, instance);
1709 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1712 /* FIXME - if this fails bus reset ?? */
1713 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
1714 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : req false, handshake complete\n", instance->host_no));
1717 * We have several special cases to consider during REQ/ACK handshaking :
1718 * 1. We were in MSGOUT phase, and we are on the last byte of the
1719 * message. ATN must be dropped as ACK is dropped.
1721 * 2. We are in a MSGIN phase, and we are on the last byte of the
1722 * message. We must exit with ACK asserted, so that the calling
1723 * code may raise ATN before dropping ACK to reject the message.
1725 * 3. ACK and ATN are clear and the target may proceed as normal.
1727 if (!(p == PHASE_MSGIN && c == 1)) {
1728 if (p == PHASE_MSGOUT && c > 1)
1729 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1730 else
1731 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1733 } while (--c);
1735 dprintk(NDEBUG_PIO, ("scsi%d : residual %d\n", instance->host_no, c));
1737 *count = c;
1738 *data = d;
1739 tmp = NCR5380_read(STATUS_REG);
1740 if (tmp & SR_REQ)
1741 *phase = tmp & PHASE_MASK;
1742 else
1743 *phase = PHASE_UNKNOWN;
1745 if (!c || (*phase == p))
1746 return 0;
1747 else
1748 return -1;
1752 * do_reset - issue a reset command
1753 * @host: adapter to reset
1755 * Issue a reset sequence to the NCR5380 and try and get the bus
1756 * back into sane shape.
1758 * Locks: caller holds queue lock
1761 static void do_reset(struct Scsi_Host *host) {
1762 NCR5380_local_declare();
1763 NCR5380_setup(host);
1765 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1766 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1767 udelay(25);
1768 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1772 * Function : do_abort (Scsi_Host *host)
1774 * Purpose : abort the currently established nexus. Should only be
1775 * called from a routine which can drop into a
1777 * Returns : 0 on success, -1 on failure.
1779 * Locks: queue lock held by caller
1780 * FIXME: sort this out and get new_eh running
1783 static int do_abort(struct Scsi_Host *host) {
1784 NCR5380_local_declare();
1785 unsigned char *msgptr, phase, tmp;
1786 int len;
1787 int rc;
1788 NCR5380_setup(host);
1791 /* Request message out phase */
1792 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1795 * Wait for the target to indicate a valid phase by asserting
1796 * REQ. Once this happens, we'll have either a MSGOUT phase
1797 * and can immediately send the ABORT message, or we'll have some
1798 * other phase and will have to source/sink data.
1800 * We really don't care what value was on the bus or what value
1801 * the target sees, so we just handshake.
1804 rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
1806 if(rc < 0)
1807 return -1;
1809 tmp = (unsigned char)rc;
1811 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1813 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1814 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1815 rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ);
1816 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1817 if(rc == -1)
1818 return -1;
1820 tmp = ABORT;
1821 msgptr = &tmp;
1822 len = 1;
1823 phase = PHASE_MSGOUT;
1824 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1827 * If we got here, and the command completed successfully,
1828 * we're about to go into bus free state.
1831 return len ? -1 : 0;
1834 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1836 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1837 * unsigned char *phase, int *count, unsigned char **data)
1839 * Purpose : transfers data in given phase using either real
1840 * or pseudo DMA.
1842 * Inputs : instance - instance of driver, *phase - pointer to
1843 * what phase is expected, *count - pointer to number of
1844 * bytes to transfer, **data - pointer to data pointer.
1846 * Returns : -1 when different phase is entered without transferring
1847 * maximum number of bytes, 0 if all bytes or transfered or exit
1848 * is in same phase.
1850 * Also, *phase, *count, *data are modified in place.
1852 * Locks: io_request lock held by caller
1856 static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1857 NCR5380_local_declare();
1858 register int c = *count;
1859 register unsigned char p = *phase;
1860 register unsigned char *d = *data;
1861 unsigned char tmp;
1862 int foo;
1863 #if defined(REAL_DMA_POLL)
1864 int cnt, toPIO;
1865 unsigned char saved_data = 0, overrun = 0, residue;
1866 #endif
1868 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1870 NCR5380_setup(instance);
1872 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1873 *phase = tmp;
1874 return -1;
1876 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1877 #ifdef READ_OVERRUNS
1878 if (p & SR_IO) {
1879 c -= 2;
1881 #endif
1882 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));
1883 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1884 #endif
1886 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1888 #ifdef REAL_DMA
1889 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1890 #elif defined(REAL_DMA_POLL)
1891 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1892 #else
1894 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1895 * were not disabled for the duration of a single DMA transfer, from
1896 * before the setting of DMA mode to after transfer of the last byte.
1899 #if defined(PSEUDO_DMA) && defined(UNSAFE)
1900 spin_unlock_irq(instance->host_lock);
1901 #endif
1902 /* KLL May need eop and parity in 53c400 */
1903 if (hostdata->flags & FLAG_NCR53C400)
1904 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);
1905 else
1906 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1907 #endif /* def REAL_DMA */
1909 dprintk(NDEBUG_DMA, ("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG)));
1912 * On the PAS16 at least I/O recovery delays are not needed here.
1913 * Everyone else seems to want them.
1916 if (p & SR_IO) {
1917 io_recovery_delay(1);
1918 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1919 } else {
1920 io_recovery_delay(1);
1921 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1922 io_recovery_delay(1);
1923 NCR5380_write(START_DMA_SEND_REG, 0);
1924 io_recovery_delay(1);
1927 #if defined(REAL_DMA_POLL)
1928 do {
1929 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1930 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1933 At this point, either we've completed DMA, or we have a phase mismatch,
1934 or we've unexpectedly lost BUSY (which is a real error).
1936 For write DMAs, we want to wait until the last byte has been
1937 transferred out over the bus before we turn off DMA mode. Alas, there
1938 seems to be no terribly good way of doing this on a 5380 under all
1939 conditions. For non-scatter-gather operations, we can wait until REQ
1940 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1941 are nastier, since the device will be expecting more data than we
1942 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1943 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1944 why this signal was added to the newer chips) but on the older 538[01]
1945 this signal does not exist. The workaround for this lack is a watchdog;
1946 we bail out of the wait-loop after a modest amount of wait-time if
1947 the usual exit conditions are not met. Not a terribly clean or
1948 correct solution :-%
1950 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1951 If the chip is in DMA mode for an READ, it will respond to a target's
1952 REQ by latching the SCSI data into the INPUT DATA register and asserting
1953 ACK, even if it has _already_ been notified by the DMA controller that
1954 the current DMA transfer has completed! If the NCR5380 is then taken
1955 out of DMA mode, this already-acknowledged byte is lost.
1957 This is not a problem for "one DMA transfer per command" reads, because
1958 the situation will never arise... either all of the data is DMA'ed
1959 properly, or the target switches to MESSAGE IN phase to signal a
1960 disconnection (either operation bringing the DMA to a clean halt).
1961 However, in order to handle scatter-reads, we must work around the
1962 problem. The chosen fix is to DMA N-2 bytes, then check for the
1963 condition before taking the NCR5380 out of DMA mode. One or two extra
1964 bytes are transferred via PIO as necessary to fill out the original
1965 request.
1968 if (p & SR_IO) {
1969 #ifdef READ_OVERRUNS
1970 udelay(10);
1971 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1972 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1973 overrun = 1;
1975 #endif
1976 } else {
1977 int limit = 100;
1978 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1979 if (!(tmp & BASR_PHASE_MATCH))
1980 break;
1981 if (--limit < 0)
1982 break;
1986 dprintk(NDEBUG_DMA, ("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG)));
1988 NCR5380_write(MODE_REG, MR_BASE);
1989 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1991 residue = NCR5380_dma_residual(instance);
1992 c -= residue;
1993 *count -= c;
1994 *data += c;
1995 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1997 #ifdef READ_OVERRUNS
1998 if (*phase == p && (p & SR_IO) && residue == 0) {
1999 if (overrun) {
2000 dprintk(NDEBUG_DMA, ("Got an input overrun, using saved byte\n"));
2001 **data = saved_data;
2002 *data += 1;
2003 *count -= 1;
2004 cnt = toPIO = 1;
2005 } else {
2006 printk("No overrun??\n");
2007 cnt = toPIO = 2;
2009 dprintk(NDEBUG_DMA, ("Doing %d-byte PIO to 0x%X\n", cnt, *data));
2010 NCR5380_transfer_pio(instance, phase, &cnt, data);
2011 *count -= toPIO - cnt;
2013 #endif
2015 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)));
2016 return 0;
2018 #elif defined(REAL_DMA)
2019 return 0;
2020 #else /* defined(REAL_DMA_POLL) */
2021 if (p & SR_IO) {
2022 #ifdef DMA_WORKS_RIGHT
2023 foo = NCR5380_pread(instance, d, c);
2024 #else
2025 int diff = 1;
2026 if (hostdata->flags & FLAG_NCR53C400) {
2027 diff = 0;
2029 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2031 * We can't disable DMA mode after successfully transferring
2032 * what we plan to be the last byte, since that would open up
2033 * a race condition where if the target asserted REQ before
2034 * we got the DMA mode reset, the NCR5380 would have latched
2035 * an additional byte into the INPUT DATA register and we'd
2036 * have dropped it.
2038 * The workaround was to transfer one fewer bytes than we
2039 * intended to with the pseudo-DMA read function, wait for
2040 * the chip to latch the last byte, read it, and then disable
2041 * pseudo-DMA mode.
2043 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2044 * REQ is deasserted when ACK is asserted, and not reasserted
2045 * until ACK goes false. Since the NCR5380 won't lower ACK
2046 * until DACK is asserted, which won't happen unless we twiddle
2047 * the DMA port or we take the NCR5380 out of DMA mode, we
2048 * can guarantee that we won't handshake another extra
2049 * byte.
2052 if (!(hostdata->flags & FLAG_NCR53C400)) {
2053 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2054 /* Wait for clean handshake */
2055 while (NCR5380_read(STATUS_REG) & SR_REQ);
2056 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2059 #endif
2060 } else {
2061 #ifdef DMA_WORKS_RIGHT
2062 foo = NCR5380_pwrite(instance, d, c);
2063 #else
2064 int timeout;
2065 dprintk(NDEBUG_C400_PWRITE, ("About to pwrite %d bytes\n", c));
2066 if (!(foo = NCR5380_pwrite(instance, d, c))) {
2068 * Wait for the last byte to be sent. If REQ is being asserted for
2069 * the byte we're interested, we'll ACK it and it will go false.
2071 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2072 timeout = 20000;
2073 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
2075 if (!timeout)
2076 dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : timed out on last byte\n", instance->host_no));
2078 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2079 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2080 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2081 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2082 dprintk(NDEBUG_LAST_WRITE_SENT, ("scsi%d : last bit sent works\n", instance->host_no));
2085 } else {
2086 dprintk(NDEBUG_C400_PWRITE, ("Waiting for LASTBYTE\n"));
2087 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2088 dprintk(NDEBUG_C400_PWRITE, ("Got LASTBYTE\n"));
2091 #endif
2093 NCR5380_write(MODE_REG, MR_BASE);
2094 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2096 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2097 dprintk(NDEBUG_C400_PWRITE, ("53C400w: Checking for IRQ\n"));
2098 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2099 dprintk(NDEBUG_C400_PWRITE, ("53C400w: got it, reading reset interrupt reg\n"));
2100 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2101 } else {
2102 printk("53C400w: IRQ NOT THERE!\n");
2105 *data = d + c;
2106 *count = 0;
2107 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2108 #if defined(PSEUDO_DMA) && defined(UNSAFE)
2109 spin_lock_irq(instance->host_lock);
2110 #endif /* defined(REAL_DMA_POLL) */
2111 return foo;
2112 #endif /* def REAL_DMA */
2114 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2117 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2119 * Purpose : run through the various SCSI phases and do as the target
2120 * directs us to. Operates on the currently connected command,
2121 * instance->connected.
2123 * Inputs : instance, instance for which we are doing commands
2125 * Side effects : SCSI things happen, the disconnected queue will be
2126 * modified if a command disconnects, *instance->connected will
2127 * change.
2129 * XXX Note : we need to watch for bus free or a reset condition here
2130 * to recover from an unexpected bus free condition.
2132 * Locks: io_request_lock held by caller in IRQ mode
2135 static void NCR5380_information_transfer(struct Scsi_Host *instance) {
2136 NCR5380_local_declare();
2137 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
2138 unsigned char msgout = NOP;
2139 int sink = 0;
2140 int len;
2141 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2142 int transfersize;
2143 #endif
2144 unsigned char *data;
2145 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
2146 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2147 /* RvC: we need to set the end of the polling time */
2148 unsigned long poll_time = jiffies + USLEEP_POLL;
2150 NCR5380_setup(instance);
2152 while (1) {
2153 tmp = NCR5380_read(STATUS_REG);
2154 /* We only have a valid SCSI phase when REQ is asserted */
2155 if (tmp & SR_REQ) {
2156 phase = (tmp & PHASE_MASK);
2157 if (phase != old_phase) {
2158 old_phase = phase;
2159 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
2161 if (sink && (phase != PHASE_MSGOUT)) {
2162 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2164 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
2165 while (NCR5380_read(STATUS_REG) & SR_REQ);
2166 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2167 sink = 0;
2168 continue;
2170 switch (phase) {
2171 case PHASE_DATAIN:
2172 case PHASE_DATAOUT:
2173 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2174 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
2175 sink = 1;
2176 do_abort(instance);
2177 cmd->result = DID_ERROR << 16;
2178 cmd->done(cmd);
2179 return;
2180 #endif
2182 * If there is no room left in the current buffer in the
2183 * scatter-gather list, move onto the next one.
2186 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2187 ++cmd->SCp.buffer;
2188 --cmd->SCp.buffers_residual;
2189 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2190 cmd->SCp.ptr = page_address(cmd->SCp.buffer->page)+
2191 cmd->SCp.buffer->offset;
2192 dprintk(NDEBUG_INFORMATION, ("scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
2195 * The preferred transfer method is going to be
2196 * PSEUDO-DMA for systems that are strictly PIO,
2197 * since we can let the hardware do the handshaking.
2199 * For this to work, we need to know the transfersize
2200 * ahead of time, since the pseudo-DMA code will sit
2201 * in an unconditional loop.
2204 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2205 /* KLL
2206 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2207 * driver then it will always be defined, so the
2208 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2209 * NCR5380 case. I think this is a fairly clean solution.
2210 * We supplement these 2 if's with the flag.
2212 #ifdef NCR5380_dma_xfer_len
2213 if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
2214 #else
2215 transfersize = cmd->transfersize;
2217 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2218 if (transfersize > 512)
2219 transfersize = 512;
2220 #endif /* LIMIT_TRANSFERSIZE */
2222 if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
2223 /* Limit transfers to 32K, for xx400 & xx406
2224 * pseudoDMA that transfers in 128 bytes blocks. */
2225 if (transfersize > 32 * 1024)
2226 transfersize = 32 * 1024;
2227 #endif
2228 len = transfersize;
2229 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
2231 * If the watchdog timer fires, all future accesses to this
2232 * device will use the polled-IO.
2234 printk("scsi%d : switching target %d lun %d to slow handshake\n", instance->host_no, cmd->device->id, cmd->device->lun);
2235 cmd->device->borken = 1;
2236 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2237 sink = 1;
2238 do_abort(instance);
2239 cmd->result = DID_ERROR << 16;
2240 cmd->done(cmd);
2241 /* XXX - need to source or sink data here, as appropriate */
2242 } else
2243 cmd->SCp.this_residual -= transfersize - len;
2244 } else
2245 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2246 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
2247 &cmd->SCp.ptr);
2248 break;
2249 case PHASE_MSGIN:
2250 len = 1;
2251 data = &tmp;
2252 NCR5380_transfer_pio(instance, &phase, &len, &data);
2253 cmd->SCp.Message = tmp;
2255 switch (tmp) {
2257 * Linking lets us reduce the time required to get the
2258 * next command out to the device, hopefully this will
2259 * mean we don't waste another revolution due to the delays
2260 * required by ARBITRATION and another SELECTION.
2262 * In the current implementation proposal, low level drivers
2263 * merely have to start the next command, pointed to by
2264 * next_link, done() is called as with unlinked commands.
2266 #ifdef LINKED
2267 case LINKED_CMD_COMPLETE:
2268 case LINKED_FLG_CMD_COMPLETE:
2269 /* Accept message by clearing ACK */
2270 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2271 dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun));
2273 * Sanity check : A linked command should only terminate with
2274 * one of these messages if there are more linked commands
2275 * available.
2277 if (!cmd->next_link) {
2278 printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
2279 sink = 1;
2280 do_abort(instance);
2281 return;
2283 initialize_SCp(cmd->next_link);
2284 /* The next command is still part of this process */
2285 cmd->next_link->tag = cmd->tag;
2286 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2287 dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked request done, calling scsi_done().\n", instance->host_no, cmd->device->id, cmd->device->lun));
2288 collect_stats(hostdata, cmd);
2289 cmd->scsi_done(cmd);
2290 cmd = hostdata->connected;
2291 break;
2292 #endif /* def LINKED */
2293 case ABORT:
2294 case COMMAND_COMPLETE:
2295 /* Accept message by clearing ACK */
2296 sink = 1;
2297 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2298 hostdata->connected = NULL;
2299 dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->device->id, cmd->device->lun));
2300 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2303 * I'm not sure what the correct thing to do here is :
2305 * If the command that just executed is NOT a request
2306 * sense, the obvious thing to do is to set the result
2307 * code to the values of the stored parameters.
2309 * If it was a REQUEST SENSE command, we need some way
2310 * to differentiate between the failure code of the original
2311 * and the failure code of the REQUEST sense - the obvious
2312 * case is success, where we fall through and leave the result
2313 * code unchanged.
2315 * The non-obvious place is where the REQUEST SENSE failed
2318 if (cmd->cmnd[0] != REQUEST_SENSE)
2319 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2320 else if (status_byte(cmd->SCp.Status) != GOOD)
2321 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2323 #ifdef AUTOSENSE
2324 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
2325 dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no));
2326 cmd->cmnd[0] = REQUEST_SENSE;
2327 cmd->cmnd[1] &= 0xe0;
2328 cmd->cmnd[2] = 0;
2329 cmd->cmnd[3] = 0;
2330 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2331 cmd->cmnd[5] = 0;
2333 cmd->SCp.buffer = NULL;
2334 cmd->SCp.buffers_residual = 0;
2335 cmd->SCp.ptr = (char *) cmd->sense_buffer;
2336 cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
2338 LIST(cmd, hostdata->issue_queue);
2339 cmd->host_scribble = (unsigned char *)
2340 hostdata->issue_queue;
2341 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2342 dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
2343 } else
2344 #endif /* def AUTOSENSE */
2346 collect_stats(hostdata, cmd);
2347 cmd->scsi_done(cmd);
2350 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2352 * Restore phase bits to 0 so an interrupted selection,
2353 * arbitration can resume.
2355 NCR5380_write(TARGET_COMMAND_REG, 0);
2357 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2358 barrier();
2359 return;
2360 case MESSAGE_REJECT:
2361 /* Accept message by clearing ACK */
2362 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2363 switch (hostdata->last_message) {
2364 case HEAD_OF_QUEUE_TAG:
2365 case ORDERED_QUEUE_TAG:
2366 case SIMPLE_QUEUE_TAG:
2367 cmd->device->simple_tags = 0;
2368 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2369 break;
2370 default:
2371 break;
2373 case DISCONNECT:{
2374 /* Accept message by clearing ACK */
2375 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2376 cmd->device->disconnect = 1;
2377 LIST(cmd, hostdata->disconnected_queue);
2378 cmd->host_scribble = (unsigned char *)
2379 hostdata->disconnected_queue;
2380 hostdata->connected = NULL;
2381 hostdata->disconnected_queue = cmd;
2382 dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d lun %d was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun));
2384 * Restore phase bits to 0 so an interrupted selection,
2385 * arbitration can resume.
2387 NCR5380_write(TARGET_COMMAND_REG, 0);
2389 /* Enable reselect interrupts */
2390 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2391 /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
2392 /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
2393 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2394 barrier();
2395 return;
2398 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2399 * operation, in violation of the SCSI spec so we can safely
2400 * ignore SAVE/RESTORE pointers calls.
2402 * Unfortunately, some disks violate the SCSI spec and
2403 * don't issue the required SAVE_POINTERS message before
2404 * disconnecting, and we have to break spec to remain
2405 * compatible.
2407 case SAVE_POINTERS:
2408 case RESTORE_POINTERS:
2409 /* Accept message by clearing ACK */
2410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2411 break;
2412 case EXTENDED_MESSAGE:
2414 * Extended messages are sent in the following format :
2415 * Byte
2416 * 0 EXTENDED_MESSAGE == 1
2417 * 1 length (includes one byte for code, doesn't
2418 * include first two bytes)
2419 * 2 code
2420 * 3..length+1 arguments
2422 * Start the extended message buffer with the EXTENDED_MESSAGE
2423 * byte, since print_msg() wants the whole thing.
2425 extended_msg[0] = EXTENDED_MESSAGE;
2426 /* Accept first byte by clearing ACK */
2427 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2428 dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no));
2430 len = 2;
2431 data = extended_msg + 1;
2432 phase = PHASE_MSGIN;
2433 NCR5380_transfer_pio(instance, &phase, &len, &data);
2435 dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]));
2437 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2438 /* Accept third byte by clearing ACK */
2439 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2440 len = extended_msg[1] - 1;
2441 data = extended_msg + 3;
2442 phase = PHASE_MSGIN;
2444 NCR5380_transfer_pio(instance, &phase, &len, &data);
2445 dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len));
2447 switch (extended_msg[2]) {
2448 case EXTENDED_SDTR:
2449 case EXTENDED_WDTR:
2450 case EXTENDED_MODIFY_DATA_POINTER:
2451 case EXTENDED_EXTENDED_IDENTIFY:
2452 tmp = 0;
2454 } else if (len) {
2455 printk("scsi%d: error receiving extended message\n", instance->host_no);
2456 tmp = 0;
2457 } else {
2458 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2459 tmp = 0;
2461 /* Fall through to reject message */
2464 * If we get something weird that we aren't expecting,
2465 * reject it.
2467 default:
2468 if (!tmp) {
2469 printk("scsi%d: rejecting message ", instance->host_no);
2470 print_msg(extended_msg);
2471 printk("\n");
2472 } else if (tmp != EXTENDED_MESSAGE)
2473 printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n", instance->host_no, tmp, cmd->device->id, cmd->device->lun);
2474 else
2475 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->device->id, cmd->device->lun);
2477 msgout = MESSAGE_REJECT;
2478 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2479 break;
2480 } /* switch (tmp) */
2481 break;
2482 case PHASE_MSGOUT:
2483 len = 1;
2484 data = &msgout;
2485 hostdata->last_message = msgout;
2486 NCR5380_transfer_pio(instance, &phase, &len, &data);
2487 if (msgout == ABORT) {
2488 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2489 hostdata->connected = NULL;
2490 cmd->result = DID_ERROR << 16;
2491 collect_stats(hostdata, cmd);
2492 cmd->scsi_done(cmd);
2493 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2494 return;
2496 msgout = NOP;
2497 break;
2498 case PHASE_CMDOUT:
2499 len = cmd->cmd_len;
2500 data = cmd->cmnd;
2502 * XXX for performance reasons, on machines with a
2503 * PSEUDO-DMA architecture we should probably
2504 * use the dma transfer function.
2506 NCR5380_transfer_pio(instance, &phase, &len, &data);
2507 if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
2508 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
2509 dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
2510 return;
2512 break;
2513 case PHASE_STATIN:
2514 len = 1;
2515 data = &tmp;
2516 NCR5380_transfer_pio(instance, &phase, &len, &data);
2517 cmd->SCp.Status = tmp;
2518 break;
2519 default:
2520 printk("scsi%d : unknown phase\n", instance->host_no);
2521 NCR5380_dprint(NDEBUG_ALL, instance);
2522 } /* switch(phase) */
2523 } /* if (tmp * SR_REQ) */
2524 else {
2525 /* RvC: go to sleep if polling time expired
2527 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
2528 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
2529 dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
2530 return;
2533 } /* while (1) */
2537 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2539 * Purpose : does reselection, initializing the instance->connected
2540 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2541 * nexus has been reestablished,
2543 * Inputs : instance - this instance of the NCR5380.
2545 * Locks: io_request_lock held by caller if IRQ driven
2548 static void NCR5380_reselect(struct Scsi_Host *instance) {
2549 NCR5380_local_declare();
2550 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2551 instance->hostdata;
2552 unsigned char target_mask;
2553 unsigned char lun, phase;
2554 int len;
2555 unsigned char msg[3];
2556 unsigned char *data;
2557 Scsi_Cmnd *tmp = NULL, *prev;
2558 int abort = 0;
2559 NCR5380_setup(instance);
2562 * Disable arbitration, etc. since the host adapter obviously
2563 * lost, and tell an interrupted NCR5380_select() to restart.
2566 NCR5380_write(MODE_REG, MR_BASE);
2567 hostdata->restart_select = 1;
2569 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2570 dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no));
2573 * At this point, we have detected that our SCSI ID is on the bus,
2574 * SEL is true and BSY was false for at least one bus settle delay
2575 * (400 ns).
2577 * We must assert BSY ourselves, until the target drops the SEL
2578 * signal.
2581 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2583 /* FIXME: timeout too long, must fail to workqueue */
2584 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2585 abort = 1;
2587 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2590 * Wait for target to go into MSGIN.
2591 * FIXME: timeout needed and fail to work queeu
2594 if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
2595 abort = 1;
2597 len = 1;
2598 data = msg;
2599 phase = PHASE_MSGIN;
2600 NCR5380_transfer_pio(instance, &phase, &len, &data);
2602 if (!(msg[0] & 0x80)) {
2603 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
2604 print_msg(msg);
2605 abort = 1;
2606 } else {
2607 /* Accept message by clearing ACK */
2608 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2609 lun = (msg[0] & 0x07);
2612 * We need to add code for SCSI-II to track which devices have
2613 * I_T_L_Q nexuses established, and which have simple I_T_L
2614 * nexuses so we can chose to do additional data transfer.
2618 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2619 * just reestablished, and remove it from the disconnected queue.
2623 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
2624 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2626 if (prev) {
2627 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2628 prev->host_scribble = tmp->host_scribble;
2629 } else {
2630 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2631 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2633 tmp->host_scribble = NULL;
2634 break;
2636 if (!tmp) {
2637 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2639 * Since we have an established nexus that we can't do anything with,
2640 * we must abort it.
2642 abort = 1;
2646 if (abort) {
2647 do_abort(instance);
2648 } else {
2649 hostdata->connected = tmp;
2650 dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->target, tmp->lun, tmp->tag));
2655 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2657 * Purpose : called by interrupt handler when DMA finishes or a phase
2658 * mismatch occurs (which would finish the DMA transfer).
2660 * Inputs : instance - this instance of the NCR5380.
2662 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2663 * nexus has been reestablished, on failure NULL is returned.
2666 #ifdef REAL_DMA
2667 static void NCR5380_dma_complete(NCR5380_instance * instance) {
2668 NCR5380_local_declare();
2669 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata * instance->hostdata);
2670 int transferred;
2671 NCR5380_setup(instance);
2674 * XXX this might not be right.
2676 * Wait for final byte to transfer, ie wait for ACK to go false.
2678 * We should use the Last Byte Sent bit, unfortunately this is
2679 * not available on the 5380/5381 (only the various CMOS chips)
2681 * FIXME: timeout, and need to handle long timeout/irq case
2684 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2686 NCR5380_write(MODE_REG, MR_BASE);
2687 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2690 * The only places we should see a phase mismatch and have to send
2691 * data from the same set of pointers will be the data transfer
2692 * phases. So, residual, requested length are only important here.
2695 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2696 transferred = instance->dmalen - NCR5380_dma_residual();
2697 hostdata->connected->SCp.this_residual -= transferred;
2698 hostdata->connected->SCp.ptr += transferred;
2701 #endif /* def REAL_DMA */
2704 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2706 * Purpose : abort a command
2708 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2709 * host byte of the result field to, if zero DID_ABORTED is
2710 * used.
2712 * Returns : 0 - success, -1 on failure.
2714 * XXX - there is no way to abort the command that is currently
2715 * connected, you have to wait for it to complete. If this is
2716 * a problem, we could implement longjmp() / setjmp(), setjmp()
2717 * called where the loop started in NCR5380_main().
2719 * Locks: host lock taken by caller
2722 static int NCR5380_abort(Scsi_Cmnd * cmd) {
2723 NCR5380_local_declare();
2724 struct Scsi_Host *instance = cmd->device->host;
2725 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2726 Scsi_Cmnd *tmp, **prev;
2728 printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
2729 print_Scsi_Cmnd(cmd);
2731 NCR5380_print_status(instance);
2733 printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
2734 print_Scsi_Cmnd(cmd);
2736 NCR5380_print_status(instance);
2738 NCR5380_setup(instance);
2740 dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no));
2741 dprintk(NDEBUG_ABORT, (" basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)));
2743 #if 0
2745 * Case 1 : If the command is the currently executing command,
2746 * we'll set the aborted flag and return control so that
2747 * information transfer routine can exit cleanly.
2750 if (hostdata->connected == cmd) {
2751 dprintk(NDEBUG_ABORT, ("scsi%d : aborting connected command\n", instance->host_no));
2752 hostdata->aborted = 1;
2754 * We should perform BSY checking, and make sure we haven't slipped
2755 * into BUS FREE.
2758 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
2760 * Since we can't change phases until we've completed the current
2761 * handshake, we have to source or sink a byte of data if the current
2762 * phase is not MSGOUT.
2766 * Return control to the executing NCR drive so we can clear the
2767 * aborted flag and get back into our main loop.
2770 return 0;
2772 #endif
2775 * Case 2 : If the command hasn't been issued yet, we simply remove it
2776 * from the issue queue.
2779 dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no));
2780 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)
2781 if (cmd == tmp) {
2782 REMOVE(5, *prev, tmp, tmp->host_scribble);
2783 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2784 tmp->host_scribble = NULL;
2785 tmp->result = DID_ABORT << 16;
2786 dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no));
2787 tmp->done(tmp);
2788 return SUCCESS;
2790 #if (NDEBUG & NDEBUG_ABORT)
2791 /* KLL */
2792 else if (prev == tmp)
2793 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2794 #endif
2797 * Case 3 : If any commands are connected, we're going to fail the abort
2798 * and let the high level SCSI driver retry at a later time or
2799 * issue a reset.
2801 * Timeouts, and therefore aborted commands, will be highly unlikely
2802 * and handling them cleanly in this situation would make the common
2803 * case of noresets less efficient, and would pollute our code. So,
2804 * we fail.
2807 if (hostdata->connected) {
2808 dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no));
2809 return FAILED;
2812 * Case 4: If the command is currently disconnected from the bus, and
2813 * there are no connected commands, we reconnect the I_T_L or
2814 * I_T_L_Q nexus associated with it, go into message out, and send
2815 * an abort message.
2817 * This case is especially ugly. In order to reestablish the nexus, we
2818 * need to call NCR5380_select(). The easiest way to implement this
2819 * function was to abort if the bus was busy, and let the interrupt
2820 * handler triggered on the SEL for reselect take care of lost arbitrations
2821 * where necessary, meaning interrupts need to be enabled.
2823 * When interrupts are enabled, the queues may change - so we
2824 * can't remove it from the disconnected queue before selecting it
2825 * because that could cause a failure in hashing the nexus if that
2826 * device reselected.
2828 * Since the queues may change, we can't use the pointers from when we
2829 * first locate it.
2831 * So, we must first locate the command, and if NCR5380_select()
2832 * succeeds, then issue the abort, relocate the command and remove
2833 * it from the disconnected queue.
2836 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
2837 if (cmd == tmp) {
2838 dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no));
2840 if (NCR5380_select(instance, cmd, (int) cmd->tag))
2841 return FAILED;
2842 dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no));
2844 do_abort(instance);
2846 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)
2847 if (cmd == tmp) {
2848 REMOVE(5, *prev, tmp, tmp->host_scribble);
2849 *prev = (Scsi_Cmnd *) tmp->host_scribble;
2850 tmp->host_scribble = NULL;
2851 tmp->result = DID_ABORT << 16;
2852 tmp->done(tmp);
2853 return SUCCESS;
2857 * Case 5 : If we reached this point, the command was not found in any of
2858 * the queues.
2860 * We probably reached this point because of an unlikely race condition
2861 * between the command completing successfully and the abortion code,
2862 * so we won't panic, but we will notify the user in case something really
2863 * broke.
2865 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2866 " before abortion\n", instance->host_no);
2867 return FAILED;
2872 * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
2874 * Purpose : reset the SCSI bus.
2876 * Returns : SUCCESS
2878 * Locks: host lock taken by caller
2881 static int NCR5380_bus_reset(Scsi_Cmnd * cmd) {
2882 NCR5380_local_declare();
2883 NCR5380_setup(cmd->device->host);
2885 NCR5380_print_status(cmd->device->host);
2886 do_reset(cmd->device->host);
2887 return SUCCESS;
2891 * Function : int NCR5380_device_reset (Scsi_Cmnd *cmd)
2893 * Purpose : reset a SCSI device
2895 * Returns : FAILED
2897 * Locks: io_request_lock held by caller
2900 static int NCR5380_device_reset(Scsi_Cmnd * cmd) {
2901 return FAILED;
2905 * Function : int NCR5380_host_reset (Scsi_Cmnd *cmd)
2907 * Purpose : reset a SCSI device
2909 * Returns : FAILED
2911 * Locks: io_request_lock held by caller
2914 static int NCR5380_host_reset(Scsi_Cmnd * cmd) {
2915 return FAILED;