GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / NCR5380.c
blobf7df8d56f1c071e24589cc9c7da33d7ccee43534
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@lxorguk.ukuu.org.uk)
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.
89 #include <scsi/scsi_dbg.h>
90 #include <scsi/scsi_transport_spi.h>
92 #ifndef NDEBUG
93 #define NDEBUG 0
94 #endif
95 #ifndef NDEBUG_ABORT
96 #define NDEBUG_ABORT 0
97 #endif
99 #if (NDEBUG & NDEBUG_LISTS)
100 #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
101 #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); }
102 #else
103 #define LIST(x,y)
104 #define REMOVE(w,x,y,z)
105 #endif
107 #ifndef notyet
108 #undef LINKED
109 #undef REAL_DMA
110 #endif
112 #ifdef REAL_DMA_POLL
113 #undef READ_OVERRUNS
114 #define READ_OVERRUNS
115 #endif
117 #ifdef BOARD_REQUIRES_NO_DELAY
118 #define io_recovery_delay(x)
119 #else
120 #define io_recovery_delay(x) udelay(x)
121 #endif
125 * Using this file :
126 * This file a skeleton Linux SCSI driver for the NCR 5380 series
127 * of chips. To use it, you write an architecture specific functions
128 * and macros and include this file in your driver.
130 * These macros control options :
131 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
132 * defined.
134 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
135 * for commands that return with a CHECK CONDITION status.
137 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
138 * transceivers.
140 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
141 * override-configure an IRQ.
143 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
144 * bytes at a time. Since interrupts are disabled by default during
145 * these transfers, we might need this to give reasonable interrupt
146 * service time if the transfer size gets too large.
148 * LINKED - if defined, linked commands are supported.
150 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
152 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
154 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
155 * rely on phase mismatch and EOP interrupts to determine end
156 * of phase.
158 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
159 * only really want to use this if you're having a problem with
160 * dropped characters during high speed communications, and even
161 * then, you're going to be better off twiddling with transfersize
162 * in the high level code.
164 * Defaults for these will be provided although the user may want to adjust
165 * these to allocate CPU resources to the SCSI driver or "real" code.
167 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
169 * USLEEP_POLL - amount of time, in jiffies, to poll
171 * These macros MUST be defined :
172 * NCR5380_local_declare() - declare any local variables needed for your
173 * transfer routines.
175 * NCR5380_setup(instance) - initialize any local variables needed from a given
176 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
178 * NCR5380_read(register) - read from the specified register
180 * NCR5380_write(register, value) - write to the specific register
182 * NCR5380_implementation_fields - additional fields needed for this
183 * specific implementation of the NCR5380
185 * Either real DMA *or* pseudo DMA may be implemented
186 * REAL functions :
187 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
188 * Note that the DMA setup functions should return the number of bytes
189 * that they were able to program the controller for.
191 * Also note that generic i386/PC versions of these macros are
192 * available as NCR5380_i386_dma_write_setup,
193 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
195 * NCR5380_dma_write_setup(instance, src, count) - initialize
196 * NCR5380_dma_read_setup(instance, dst, count) - initialize
197 * NCR5380_dma_residual(instance); - residual count
199 * PSEUDO functions :
200 * NCR5380_pwrite(instance, src, count)
201 * NCR5380_pread(instance, dst, count);
203 * The generic driver is initialized by calling NCR5380_init(instance),
204 * after setting the appropriate host specific fields and ID. If the
205 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
206 * possible) function may be used.
209 static int do_abort(struct Scsi_Host *host);
210 static void do_reset(struct Scsi_Host *host);
213 * initialize_SCp - init the scsi pointer field
214 * @cmd: command block to set up
216 * Set up the internal fields in the SCSI command.
219 static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
222 * Initialize the Scsi Pointer field so that all of the commands in the
223 * various queues are valid.
226 if (scsi_bufflen(cmd)) {
227 cmd->SCp.buffer = scsi_sglist(cmd);
228 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
229 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
230 cmd->SCp.this_residual = cmd->SCp.buffer->length;
231 } else {
232 cmd->SCp.buffer = NULL;
233 cmd->SCp.buffers_residual = 0;
234 cmd->SCp.ptr = NULL;
235 cmd->SCp.this_residual = 0;
240 * NCR5380_poll_politely - wait for NCR5380 status bits
241 * @instance: controller to poll
242 * @reg: 5380 register to poll
243 * @bit: Bitmask to check
244 * @val: Value required to exit
246 * Polls the NCR5380 in a reasonably efficient manner waiting for
247 * an event to occur, after a short quick poll we begin giving the
248 * CPU back in non IRQ contexts
250 * Returns the value of the register or a negative error code.
253 static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
255 NCR5380_local_declare();
256 int n = 500; /* At about 8uS a cycle for the cpu access */
257 unsigned long end = jiffies + t;
258 int r;
260 NCR5380_setup(instance);
262 while( n-- > 0)
264 r = NCR5380_read(reg);
265 if((r & bit) == val)
266 return 0;
267 cpu_relax();
270 /* t time yet ? */
271 while(time_before(jiffies, end))
273 r = NCR5380_read(reg);
274 if((r & bit) == val)
275 return 0;
276 if(!in_interrupt())
277 cond_resched();
278 else
279 cpu_relax();
281 return -ETIMEDOUT;
284 static struct {
285 unsigned char value;
286 const char *name;
287 } phases[] __maybe_unused = {
288 {PHASE_DATAOUT, "DATAOUT"},
289 {PHASE_DATAIN, "DATAIN"},
290 {PHASE_CMDOUT, "CMDOUT"},
291 {PHASE_STATIN, "STATIN"},
292 {PHASE_MSGOUT, "MSGOUT"},
293 {PHASE_MSGIN, "MSGIN"},
294 {PHASE_UNKNOWN, "UNKNOWN"}
297 #if NDEBUG
298 static struct {
299 unsigned char mask;
300 const char *name;
301 } signals[] = {
302 {SR_DBP, "PARITY"},
303 {SR_RST, "RST"},
304 {SR_BSY, "BSY"},
305 {SR_REQ, "REQ"},
306 {SR_MSG, "MSG"},
307 {SR_CD, "CD"},
308 {SR_IO, "IO"},
309 {SR_SEL, "SEL"},
310 {0, NULL}
312 basrs[] = {
313 {BASR_ATN, "ATN"},
314 {BASR_ACK, "ACK"},
315 {0, NULL}
317 icrs[] = {
318 {ICR_ASSERT_RST, "ASSERT RST"},
319 {ICR_ASSERT_ACK, "ASSERT ACK"},
320 {ICR_ASSERT_BSY, "ASSERT BSY"},
321 {ICR_ASSERT_SEL, "ASSERT SEL"},
322 {ICR_ASSERT_ATN, "ASSERT ATN"},
323 {ICR_ASSERT_DATA, "ASSERT DATA"},
324 {0, NULL}
326 mrs[] = {
327 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
328 {MR_TARGET, "MODE TARGET"},
329 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
330 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
331 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
332 {MR_DMA_MODE, "MODE DMA"},
333 {MR_ARBITRATE, "MODE ARBITRATION"},
334 {0, NULL}
338 * NCR5380_print - print scsi bus signals
339 * @instance: adapter state to dump
341 * Print the SCSI bus signals for debugging purposes
343 * Locks: caller holds hostdata lock (not essential)
346 static void NCR5380_print(struct Scsi_Host *instance)
348 NCR5380_local_declare();
349 unsigned char status, data, basr, mr, icr, i;
350 NCR5380_setup(instance);
352 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
353 status = NCR5380_read(STATUS_REG);
354 mr = NCR5380_read(MODE_REG);
355 icr = NCR5380_read(INITIATOR_COMMAND_REG);
356 basr = NCR5380_read(BUS_AND_STATUS_REG);
358 printk("STATUS_REG: %02x ", status);
359 for (i = 0; signals[i].mask; ++i)
360 if (status & signals[i].mask)
361 printk(",%s", signals[i].name);
362 printk("\nBASR: %02x ", basr);
363 for (i = 0; basrs[i].mask; ++i)
364 if (basr & basrs[i].mask)
365 printk(",%s", basrs[i].name);
366 printk("\nICR: %02x ", icr);
367 for (i = 0; icrs[i].mask; ++i)
368 if (icr & icrs[i].mask)
369 printk(",%s", icrs[i].name);
370 printk("\nMODE: %02x ", mr);
371 for (i = 0; mrs[i].mask; ++i)
372 if (mr & mrs[i].mask)
373 printk(",%s", mrs[i].name);
374 printk("\n");
379 * NCR5380_print_phase - show SCSI phase
380 * @instance: adapter to dump
382 * Print the current SCSI phase for debugging purposes
384 * Locks: none
387 static void NCR5380_print_phase(struct Scsi_Host *instance)
389 NCR5380_local_declare();
390 unsigned char status;
391 int i;
392 NCR5380_setup(instance);
394 status = NCR5380_read(STATUS_REG);
395 if (!(status & SR_REQ))
396 printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
397 else {
398 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
399 printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
402 #endif
405 * These need tweaking, and would probably work best as per-device
406 * flags initialized differently for disk, tape, cd, etc devices.
407 * People with broken devices are free to experiment as to what gives
408 * the best results for them.
410 * USLEEP_SLEEP should be a minimum seek time.
412 * USLEEP_POLL should be a maximum rotational latency.
414 #ifndef USLEEP_SLEEP
415 /* 20 ms (reasonable hard disk speed) */
416 #define USLEEP_SLEEP (20*HZ/1000)
417 #endif
418 /* 300 RPM (floppy speed) */
419 #ifndef USLEEP_POLL
420 #define USLEEP_POLL (200*HZ/1000)
421 #endif
422 #ifndef USLEEP_WAITLONG
423 /* RvC: (reasonable time to wait on select error) */
424 #define USLEEP_WAITLONG USLEEP_SLEEP
425 #endif
428 * Function : int should_disconnect (unsigned char cmd)
430 * Purpose : decide whether a command would normally disconnect or
431 * not, since if it won't disconnect we should go to sleep.
433 * Input : cmd - opcode of SCSI command
435 * Returns : DISCONNECT_LONG if we should disconnect for a really long
436 * time (ie always, sleep, look for REQ active, sleep),
437 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
438 * time-to-data delay, DISCONNECT_NONE if this command would return
439 * immediately.
441 * Future sleep algorithms based on time to data can exploit
442 * something like this so they can differentiate between "normal"
443 * (ie, read, write, seek) and unusual commands (ie, * format).
445 * Note : We don't deal with commands that handle an immediate disconnect,
449 static int should_disconnect(unsigned char cmd)
451 switch (cmd) {
452 case READ_6:
453 case WRITE_6:
454 case SEEK_6:
455 case READ_10:
456 case WRITE_10:
457 case SEEK_10:
458 return DISCONNECT_TIME_TO_DATA;
459 case FORMAT_UNIT:
460 case SEARCH_HIGH:
461 case SEARCH_LOW:
462 case SEARCH_EQUAL:
463 return DISCONNECT_LONG;
464 default:
465 return DISCONNECT_NONE;
469 static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
471 hostdata->time_expires = jiffies + timeout;
472 schedule_delayed_work(&hostdata->coroutine, timeout);
476 static int probe_irq __initdata = 0;
479 * probe_intr - helper for IRQ autoprobe
480 * @irq: interrupt number
481 * @dev_id: unused
482 * @regs: unused
484 * Set a flag to indicate the IRQ in question was received. This is
485 * used by the IRQ probe code.
488 static irqreturn_t __init probe_intr(int irq, void *dev_id)
490 probe_irq = irq;
491 return IRQ_HANDLED;
495 * NCR5380_probe_irq - find the IRQ of an NCR5380
496 * @instance: NCR5380 controller
497 * @possible: bitmask of ISA IRQ lines
499 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
500 * and then looking to see what interrupt actually turned up.
502 * Locks: none, irqs must be enabled on entry
505 static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
506 int possible)
508 NCR5380_local_declare();
509 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
510 unsigned long timeout;
511 int trying_irqs, i, mask;
512 NCR5380_setup(instance);
514 for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
515 if ((mask & possible) && (request_irq(i, &probe_intr, IRQF_DISABLED, "NCR-probe", NULL) == 0))
516 trying_irqs |= mask;
518 timeout = jiffies + (250 * HZ / 1000);
519 probe_irq = SCSI_IRQ_NONE;
522 * A interrupt is triggered whenever BSY = false, SEL = true
523 * and a bit set in the SELECT_ENABLE_REG is asserted on the
524 * SCSI bus.
526 * Note that the bus is only driven when the phase control signals
527 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
528 * to zero.
531 NCR5380_write(TARGET_COMMAND_REG, 0);
532 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
533 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
534 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
536 while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
537 schedule_timeout_uninterruptible(1);
539 NCR5380_write(SELECT_ENABLE_REG, 0);
540 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
542 for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
543 if (trying_irqs & mask)
544 free_irq(i, NULL);
546 return probe_irq;
550 * NCR58380_print_options - show options
551 * @instance: unused for now
553 * Called by probe code indicating the NCR5380 driver options that
554 * were selected. At some point this will switch to runtime options
555 * read from the adapter in question
557 * Locks: none
560 static void __init __maybe_unused
561 NCR5380_print_options(struct Scsi_Host *instance)
563 printk(" generic options"
564 #ifdef AUTOPROBE_IRQ
565 " AUTOPROBE_IRQ"
566 #endif
567 #ifdef AUTOSENSE
568 " AUTOSENSE"
569 #endif
570 #ifdef DIFFERENTIAL
571 " DIFFERENTIAL"
572 #endif
573 #ifdef REAL_DMA
574 " REAL DMA"
575 #endif
576 #ifdef REAL_DMA_POLL
577 " REAL DMA POLL"
578 #endif
579 #ifdef PARITY
580 " PARITY"
581 #endif
582 #ifdef PSEUDO_DMA
583 " PSEUDO DMA"
584 #endif
585 #ifdef UNSAFE
586 " UNSAFE "
587 #endif
589 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
590 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
591 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
592 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
597 * NCR5380_print_status - dump controller info
598 * @instance: controller to dump
600 * Print commands in the various queues, called from NCR5380_abort
601 * and NCR5380_debug to aid debugging.
603 * Locks: called functions disable irqs
606 static void NCR5380_print_status(struct Scsi_Host *instance)
608 NCR5380_dprint(NDEBUG_ANY, instance);
609 NCR5380_dprint_phase(NDEBUG_ANY, instance);
612 /******************************************/
614 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
616 * *buffer: I/O buffer
617 * **start: if inout == FALSE pointer into buffer where user read should start
618 * offset: current offset
619 * length: length of buffer
620 * hostno: Scsi_Host host_no
621 * inout: TRUE - user is writing; FALSE - user is reading
623 * Return the number of bytes read from or written
626 #undef SPRINTF
627 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
628 static
629 char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
630 static
631 char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
632 static
633 char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
635 static int __maybe_unused NCR5380_proc_info(struct Scsi_Host *instance,
636 char *buffer, char **start, off_t offset, int length, int inout)
638 char *pos = buffer;
639 struct NCR5380_hostdata *hostdata;
640 Scsi_Cmnd *ptr;
642 hostdata = (struct NCR5380_hostdata *) instance->hostdata;
644 if (inout) { /* Has data been written to the file ? */
645 #ifdef DTC_PUBLIC_RELEASE
646 dtc_wmaxi = dtc_maxi = 0;
647 #endif
648 #ifdef PAS16_PUBLIC_RELEASE
649 pas_wmaxi = pas_maxi = 0;
650 #endif
651 return (-ENOSYS); /* Currently this is a no-op */
653 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
654 if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
655 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
656 #ifdef DTC_PUBLIC_RELEASE
657 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
658 #endif
659 #ifdef T128_PUBLIC_RELEASE
660 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
661 #endif
662 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
663 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
664 #endif
665 #ifdef PAS16_PUBLIC_RELEASE
666 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
667 #endif
669 SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
670 SPRINTF("io_port: %04x ", (int) instance->io_port);
671 if (instance->irq == SCSI_IRQ_NONE)
672 SPRINTF("IRQ: None.\n");
673 else
674 SPRINTF("IRQ: %d.\n", instance->irq);
676 #ifdef DTC_PUBLIC_RELEASE
677 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi);
678 #endif
679 #ifdef PAS16_PUBLIC_RELEASE
680 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi);
681 #endif
682 spin_lock_irq(instance->host_lock);
683 if (!hostdata->connected)
684 SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
685 else
686 pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, pos, buffer, length);
687 SPRINTF("scsi%d: issue_queue\n", instance->host_no);
688 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
689 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
691 SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
692 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
693 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
694 spin_unlock_irq(instance->host_lock);
696 *start = buffer;
697 if (pos - buffer < offset)
698 return 0;
699 else if (pos - buffer - offset < length)
700 return pos - buffer - offset;
701 return length;
704 static char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
706 SPRINTF("scsi%d : destination target %d, lun %d\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
707 SPRINTF(" command = ");
708 pos = lprint_command(cmd->cmnd, pos, buffer, length);
709 return (pos);
712 static char *lprint_command(unsigned char *command, char *pos, char *buffer, int length)
714 int i, s;
715 pos = lprint_opcode(command[0], pos, buffer, length);
716 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
717 SPRINTF("%02x ", command[i]);
718 SPRINTF("\n");
719 return (pos);
722 static char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
724 SPRINTF("%2d (0x%02x)", opcode, opcode);
725 return (pos);
730 * NCR5380_init - initialise an NCR5380
731 * @instance: adapter to configure
732 * @flags: control flags
734 * Initializes *instance and corresponding 5380 chip,
735 * with flags OR'd into the initial flags value.
737 * Notes : I assume that the host, hostno, and id bits have been
738 * set correctly. I don't care about the irq and other fields.
740 * Returns 0 for success
742 * Locks: interrupts must be enabled when we are called
745 static int __devinit NCR5380_init(struct Scsi_Host *instance, int flags)
747 NCR5380_local_declare();
748 int i, pass;
749 unsigned long timeout;
750 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
752 if(in_interrupt())
753 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
755 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
756 * the base address.
759 #ifdef NCR53C400
760 if (flags & FLAG_NCR53C400)
761 instance->NCR5380_instance_name += NCR53C400_address_adjust;
762 #endif
764 NCR5380_setup(instance);
766 hostdata->aborted = 0;
767 hostdata->id_mask = 1 << instance->this_id;
768 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
769 if (i > hostdata->id_mask)
770 hostdata->id_higher_mask |= i;
771 for (i = 0; i < 8; ++i)
772 hostdata->busy[i] = 0;
773 #ifdef REAL_DMA
774 hostdata->dmalen = 0;
775 #endif
776 hostdata->targets_present = 0;
777 hostdata->connected = NULL;
778 hostdata->issue_queue = NULL;
779 hostdata->disconnected_queue = NULL;
781 INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main);
783 #ifdef NCR5380_STATS
784 for (i = 0; i < 8; ++i) {
785 hostdata->time_read[i] = 0;
786 hostdata->time_write[i] = 0;
787 hostdata->bytes_read[i] = 0;
788 hostdata->bytes_write[i] = 0;
790 hostdata->timebase = 0;
791 hostdata->pendingw = 0;
792 hostdata->pendingr = 0;
793 #endif
795 /* The CHECK code seems to break the 53C400. Will check it later maybe */
796 if (flags & FLAG_NCR53C400)
797 hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
798 else
799 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
801 hostdata->host = instance;
802 hostdata->time_expires = 0;
804 #ifndef AUTOSENSE
805 if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
806 printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" " without AUTOSENSE option, contingent allegiance conditions may\n"
807 " be incorrectly cleared.\n", instance->host_no);
808 #endif /* def AUTOSENSE */
810 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
811 NCR5380_write(MODE_REG, MR_BASE);
812 NCR5380_write(TARGET_COMMAND_REG, 0);
813 NCR5380_write(SELECT_ENABLE_REG, 0);
815 #ifdef NCR53C400
816 if (hostdata->flags & FLAG_NCR53C400) {
817 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
819 #endif
822 * Detect and correct bus wedge problems.
824 * If the system crashed, it may have crashed in a state
825 * where a SCSI command was still executing, and the
826 * SCSI bus is not in a BUS FREE STATE.
828 * If this is the case, we'll try to abort the currently
829 * established nexus which we know nothing about, and that
830 * failing, do a hard reset of the SCSI bus
833 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
834 switch (pass) {
835 case 1:
836 case 3:
837 case 5:
838 printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
839 timeout = jiffies + 5 * HZ;
840 NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ);
841 break;
842 case 2:
843 printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no);
844 do_abort(instance);
845 break;
846 case 4:
847 printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no);
848 do_reset(instance);
849 break;
850 case 6:
851 printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no);
852 return -ENXIO;
855 return 0;
859 * NCR5380_exit - remove an NCR5380
860 * @instance: adapter to remove
863 static void NCR5380_exit(struct Scsi_Host *instance)
865 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
867 cancel_delayed_work(&hostdata->coroutine);
868 flush_scheduled_work();
872 * NCR5380_queue_command - queue a command
873 * @cmd: SCSI command
874 * @done: completion handler
876 * cmd is added to the per instance issue_queue, with minor
877 * twiddling done to the host specific fields of cmd. If the
878 * main coroutine is not running, it is restarted.
880 * Locks: host lock taken by caller
883 static int NCR5380_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
885 struct Scsi_Host *instance = cmd->device->host;
886 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
887 Scsi_Cmnd *tmp;
889 #if (NDEBUG & NDEBUG_NO_WRITE)
890 switch (cmd->cmnd[0]) {
891 case WRITE_6:
892 case WRITE_10:
893 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
894 cmd->result = (DID_ERROR << 16);
895 done(cmd);
896 return 0;
898 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
900 #ifdef NCR5380_STATS
901 switch (cmd->cmnd[0]) {
902 case WRITE:
903 case WRITE_6:
904 case WRITE_10:
905 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
906 hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
907 hostdata->pendingw++;
908 break;
909 case READ:
910 case READ_6:
911 case READ_10:
912 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
913 hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
914 hostdata->pendingr++;
915 break;
917 #endif
920 * We use the host_scribble field as a pointer to the next command
921 * in a queue
924 cmd->host_scribble = NULL;
925 cmd->scsi_done = done;
926 cmd->result = 0;
929 * Insert the cmd into the issue queue. Note that REQUEST SENSE
930 * commands are added to the head of the queue since any command will
931 * clear the contingent allegiance condition that exists and the
932 * sense data is only guaranteed to be valid while the condition exists.
935 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
936 LIST(cmd, hostdata->issue_queue);
937 cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
938 hostdata->issue_queue = cmd;
939 } else {
940 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
941 LIST(cmd, tmp);
942 tmp->host_scribble = (unsigned char *) cmd;
944 dprintk(NDEBUG_QUEUES, ("scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
946 /* Run the coroutine if it isn't already running. */
947 /* Kick off command processing */
948 schedule_delayed_work(&hostdata->coroutine, 0);
949 return 0;
954 * NCR5380_main - NCR state machines
956 * NCR5380_main is a coroutine that runs as long as more work can
957 * be done on the NCR5380 host adapters in a system. Both
958 * NCR5380_queue_command() and NCR5380_intr() will try to start it
959 * in case it is not running.
961 * Locks: called as its own thread with no locks held. Takes the
962 * host lock and called routines may take the isa dma lock.
965 static void NCR5380_main(struct work_struct *work)
967 struct NCR5380_hostdata *hostdata =
968 container_of(work, struct NCR5380_hostdata, coroutine.work);
969 struct Scsi_Host *instance = hostdata->host;
970 Scsi_Cmnd *tmp, *prev;
971 int done;
973 spin_lock_irq(instance->host_lock);
974 do {
975 /* Lock held here */
976 done = 1;
977 if (!hostdata->connected && !hostdata->selecting) {
978 dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no));
980 * Search through the issue_queue for a command destined
981 * for a target that's not busy.
983 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
985 if (prev != tmp)
986 dprintk(NDEBUG_LISTS, ("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun));
987 /* When we find one, remove it from the issue queue. */
988 if (!(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))) {
989 if (prev) {
990 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
991 prev->host_scribble = tmp->host_scribble;
992 } else {
993 REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
994 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
996 tmp->host_scribble = NULL;
999 * Attempt to establish an I_T_L nexus here.
1000 * On success, instance->hostdata->connected is set.
1001 * On failure, we must add the command back to the
1002 * issue queue so we can keep trying.
1004 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));
1007 * A successful selection is defined as one that
1008 * leaves us with the command connected and
1009 * in hostdata->connected, OR has terminated the
1010 * command.
1012 * With successful commands, we fall through
1013 * and see if we can do an information transfer,
1014 * with failures we will restart.
1016 hostdata->selecting = NULL;
1017 /* RvC: have to preset this to indicate a new command is being performed */
1019 if (!NCR5380_select(instance, tmp,
1021 * REQUEST SENSE commands are issued without tagged
1022 * queueing, even on SCSI-II devices because the
1023 * contingent allegiance condition exists for the
1024 * entire unit.
1026 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
1027 break;
1028 } else {
1029 LIST(tmp, hostdata->issue_queue);
1030 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1031 hostdata->issue_queue = tmp;
1032 done = 0;
1033 dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no));
1035 /* lock held here still */
1036 } /* if target/lun is not busy */
1037 } /* for */
1038 /* exited locked */
1039 } /* if (!hostdata->connected) */
1040 if (hostdata->selecting) {
1041 tmp = (Scsi_Cmnd *) hostdata->selecting;
1042 /* Selection will drop and retake the lock */
1043 if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
1044 /* Ok ?? */
1045 } else {
1046 /* RvC: device failed, so we wait a long time
1047 this is needed for Mustek scanners, that
1048 do not respond to commands immediately
1049 after a scan */
1050 printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
1051 LIST(tmp, hostdata->issue_queue);
1052 tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
1053 hostdata->issue_queue = tmp;
1054 NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
1056 } /* if hostdata->selecting */
1057 if (hostdata->connected
1058 #ifdef REAL_DMA
1059 && !hostdata->dmalen
1060 #endif
1061 && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
1063 dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no));
1064 NCR5380_information_transfer(instance);
1065 dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no));
1066 done = 0;
1067 } else
1068 break;
1069 } while (!done);
1071 spin_unlock_irq(instance->host_lock);
1074 #ifndef DONT_USE_INTR
1077 * NCR5380_intr - generic NCR5380 irq handler
1078 * @irq: interrupt number
1079 * @dev_id: device info
1081 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1082 * from the disconnected queue, and restarting NCR5380_main()
1083 * as required.
1085 * Locks: takes the needed instance locks
1088 static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
1090 NCR5380_local_declare();
1091 struct Scsi_Host *instance = dev_id;
1092 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1093 int done;
1094 unsigned char basr;
1095 unsigned long flags;
1097 dprintk(NDEBUG_INTR, ("scsi : NCR5380 irq %d triggered\n",
1098 instance->irq));
1100 do {
1101 done = 1;
1102 spin_lock_irqsave(instance->host_lock, flags);
1103 /* Look for pending interrupts */
1104 NCR5380_setup(instance);
1105 basr = NCR5380_read(BUS_AND_STATUS_REG);
1106 if (basr & BASR_IRQ) {
1107 NCR5380_dprint(NDEBUG_INTR, instance);
1108 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1109 done = 0;
1110 dprintk(NDEBUG_INTR, ("scsi%d : SEL interrupt\n", instance->host_no));
1111 NCR5380_reselect(instance);
1112 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1113 } else if (basr & BASR_PARITY_ERROR) {
1114 dprintk(NDEBUG_INTR, ("scsi%d : PARITY interrupt\n", instance->host_no));
1115 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1116 } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1117 dprintk(NDEBUG_INTR, ("scsi%d : RESET interrupt\n", instance->host_no));
1118 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1119 } else {
1120 #if defined(REAL_DMA)
1122 * We should only get PHASE MISMATCH and EOP interrupts
1123 * if we have DMA enabled, so do a sanity check based on
1124 * the current setting of the MODE register.
1127 if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
1128 int transfered;
1130 if (!hostdata->connected)
1131 panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
1133 transfered = (hostdata->dmalen - NCR5380_dma_residual(instance));
1134 hostdata->connected->SCp.this_residual -= transferred;
1135 hostdata->connected->SCp.ptr += transferred;
1136 hostdata->dmalen = 0;
1138 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1140 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
1142 NCR5380_write(MODE_REG, MR_BASE);
1143 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1145 #else
1146 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)));
1147 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1148 #endif
1150 } /* if BASR_IRQ */
1151 spin_unlock_irqrestore(instance->host_lock, flags);
1152 if(!done)
1153 schedule_delayed_work(&hostdata->coroutine, 0);
1154 } while (!done);
1155 return IRQ_HANDLED;
1158 #endif
1161 * collect_stats - collect stats on a scsi command
1162 * @hostdata: adapter
1163 * @cmd: command being issued
1165 * Update the statistical data by parsing the command in question
1168 static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
1170 #ifdef NCR5380_STATS
1171 switch (cmd->cmnd[0]) {
1172 case WRITE:
1173 case WRITE_6:
1174 case WRITE_10:
1175 hostdata->time_write[scmd_id(cmd)] += (jiffies - hostdata->timebase);
1176 hostdata->pendingw--;
1177 break;
1178 case READ:
1179 case READ_6:
1180 case READ_10:
1181 hostdata->time_read[scmd_id(cmd)] += (jiffies - hostdata->timebase);
1182 hostdata->pendingr--;
1183 break;
1185 #endif
1190 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1191 * int tag);
1193 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1194 * including ARBITRATION, SELECTION, and initial message out for
1195 * IDENTIFY and queue messages.
1197 * Inputs : instance - instantiation of the 5380 driver on which this
1198 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1199 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1200 * the command that is presently connected.
1202 * Returns : -1 if selection could not execute for some reason,
1203 * 0 if selection succeeded or failed because the target
1204 * did not respond.
1206 * Side effects :
1207 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1208 * with registers as they should have been on entry - ie
1209 * SELECT_ENABLE will be set appropriately, the NCR5380
1210 * will cease to drive any SCSI bus signals.
1212 * If successful : I_T_L or I_T_L_Q nexus will be established,
1213 * instance->connected will be set to cmd.
1214 * SELECT interrupt will be disabled.
1216 * If failed (no target) : cmd->scsi_done() will be called, and the
1217 * cmd->result host byte set to DID_BAD_TARGET.
1219 * Locks: caller holds hostdata lock in IRQ mode
1222 static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
1224 NCR5380_local_declare();
1225 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1226 unsigned char tmp[3], phase;
1227 unsigned char *data;
1228 int len;
1229 unsigned long timeout;
1230 unsigned char value;
1231 int err;
1232 NCR5380_setup(instance);
1234 if (hostdata->selecting)
1235 goto part2;
1237 hostdata->restart_select = 0;
1239 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
1240 dprintk(NDEBUG_ARBITRATION, ("scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id));
1243 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1244 * data bus during SELECTION.
1247 NCR5380_write(TARGET_COMMAND_REG, 0);
1250 * Start arbitration.
1253 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1254 NCR5380_write(MODE_REG, MR_ARBITRATE);
1257 /* We can be relaxed here, interrupts are on, we are
1258 in workqueue context, the birds are singing in the trees */
1259 spin_unlock_irq(instance->host_lock);
1260 err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
1261 spin_lock_irq(instance->host_lock);
1262 if (err < 0) {
1263 printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
1264 NCR5380_write(MODE_REG, MR_BASE);
1265 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1266 goto failed;
1269 dprintk(NDEBUG_ARBITRATION, ("scsi%d : arbitration complete\n", instance->host_no));
1272 * The arbitration delay is 2.2us, but this is a minimum and there is
1273 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1274 * the integral nature of udelay().
1278 udelay(3);
1280 /* Check for lost arbitration */
1281 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)) {
1282 NCR5380_write(MODE_REG, MR_BASE);
1283 dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no));
1284 goto failed;
1286 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
1288 if (!(hostdata->flags & FLAG_DTC3181E) &&
1289 /* RvC: DTC3181E has some trouble with this
1290 * so we simply removed it. Seems to work with
1291 * only Mustek scanner attached
1293 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1294 NCR5380_write(MODE_REG, MR_BASE);
1295 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1296 dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no));
1297 goto failed;
1300 * Again, bus clear + bus settle time is 1.2us, however, this is
1301 * a minimum so we'll udelay ceil(1.2)
1304 udelay(2);
1306 dprintk(NDEBUG_ARBITRATION, ("scsi%d : won arbitration\n", instance->host_no));
1309 * Now that we have won arbitration, start Selection process, asserting
1310 * the host and target ID's on the SCSI bus.
1313 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
1316 * Raise ATN while SEL is true before BSY goes false from arbitration,
1317 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1318 * phase immediately after selection.
1321 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1322 NCR5380_write(MODE_REG, MR_BASE);
1325 * Reselect interrupts must be turned off prior to the dropping of BSY,
1326 * otherwise we will trigger an interrupt.
1328 NCR5380_write(SELECT_ENABLE_REG, 0);
1331 * The initiator shall then wait at least two deskew delays and release
1332 * the BSY signal.
1334 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1336 /* Reset BSY */
1337 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1340 * Something weird happens when we cease to drive BSY - looks
1341 * like the board/chip is letting us do another read before the
1342 * appropriate propagation delay has expired, and we're confusing
1343 * a BSY signal from ourselves as the target's response to SELECTION.
1345 * A small delay (the 'C++' frontend breaks the pipeline with an
1346 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1347 * tighter 'C' code breaks and requires this) solves the problem -
1348 * the 1 us delay is arbitrary, and only used because this delay will
1349 * be the same on other platforms and since it works here, it should
1350 * work there.
1352 * wingel suggests that this could be due to failing to wait
1353 * one deskew delay.
1356 udelay(1);
1358 dprintk(NDEBUG_SELECTION, ("scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd)));
1361 * The SCSI specification calls for a 250 ms timeout for the actual
1362 * selection.
1365 timeout = jiffies + (250 * HZ / 1000);
1368 hostdata->select_time = 0; /* we count the clock ticks at which we polled */
1369 hostdata->selecting = cmd;
1371 part2:
1372 /* RvC: here we enter after a sleeping period, or immediately after
1373 execution of part 1
1374 we poll only once ech clock tick */
1375 value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
1377 if (!value && (hostdata->select_time < HZ/4)) {
1378 /* RvC: we still must wait for a device response */
1379 hostdata->select_time++; /* after 25 ticks the device has failed */
1380 NCR5380_set_timer(hostdata, 1);
1381 return 0; /* RvC: we return here with hostdata->selecting set,
1382 to go to sleep */
1385 hostdata->selecting = NULL;/* clear this pointer, because we passed the
1386 waiting period */
1387 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1388 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1389 NCR5380_reselect(instance);
1390 printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
1391 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1392 return -1;
1395 * No less than two deskew delays after the initiator detects the
1396 * BSY signal is true, it shall release the SEL signal and may
1397 * change the DATA BUS. -wingel
1400 udelay(1);
1402 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1404 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1405 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1406 if (hostdata->targets_present & (1 << scmd_id(cmd))) {
1407 printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no);
1408 if (hostdata->restart_select)
1409 printk(KERN_DEBUG "\trestart select\n");
1410 NCR5380_dprint(NDEBUG_SELECTION, instance);
1411 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1412 return -1;
1414 cmd->result = DID_BAD_TARGET << 16;
1415 collect_stats(hostdata, cmd);
1416 cmd->scsi_done(cmd);
1417 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1418 dprintk(NDEBUG_SELECTION, ("scsi%d : target did not respond within 250ms\n", instance->host_no));
1419 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1420 return 0;
1422 hostdata->targets_present |= (1 << scmd_id(cmd));
1425 /* Wait for start of REQ/ACK handshake */
1427 spin_unlock_irq(instance->host_lock);
1428 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1429 spin_lock_irq(instance->host_lock);
1431 if(err) {
1432 printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
1433 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1434 goto failed;
1437 dprintk(NDEBUG_SELECTION, ("scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id));
1438 tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun);
1440 len = 1;
1441 cmd->tag = 0;
1443 /* Send message(s) */
1444 data = tmp;
1445 phase = PHASE_MSGOUT;
1446 NCR5380_transfer_pio(instance, &phase, &len, &data);
1447 dprintk(NDEBUG_SELECTION, ("scsi%d : nexus established.\n", instance->host_no));
1448 hostdata->connected = cmd;
1449 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1451 initialize_SCp(cmd);
1453 return 0;
1455 /* Selection failed */
1456 failed:
1457 return -1;
1463 * Note : this code is not as quick as it could be, however it
1464 * IS 100% reliable, and for the actual data transfer where speed
1465 * counts, we will always do a pseudo DMA or DMA transfer.
1468 static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1469 NCR5380_local_declare();
1470 unsigned char p = *phase, tmp;
1471 int c = *count;
1472 unsigned char *d = *data;
1474 * RvC: some administrative data to process polling time
1476 int break_allowed = 0;
1477 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1478 NCR5380_setup(instance);
1480 if (!(p & SR_IO))
1481 dprintk(NDEBUG_PIO, ("scsi%d : pio write %d bytes\n", instance->host_no, c));
1482 else
1483 dprintk(NDEBUG_PIO, ("scsi%d : pio read %d bytes\n", instance->host_no, c));
1486 * The NCR5380 chip will only drive the SCSI bus when the
1487 * phase specified in the appropriate bits of the TARGET COMMAND
1488 * REGISTER match the STATUS REGISTER
1491 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1493 /* RvC: don't know if this is necessary, but other SCSI I/O is short
1494 * so breaks are not necessary there
1496 if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
1497 break_allowed = 1;
1499 do {
1501 * Wait for assertion of REQ, after which the phase bits will be
1502 * valid
1505 /* RvC: we simply poll once, after that we stop temporarily
1506 * and let the device buffer fill up
1507 * if breaking is not allowed, we keep polling as long as needed
1510 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
1511 if (!(tmp & SR_REQ)) {
1512 /* timeout condition */
1513 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
1514 break;
1517 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : REQ detected\n", instance->host_no));
1519 /* Check for phase mismatch */
1520 if ((tmp & PHASE_MASK) != p) {
1521 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : phase mismatch\n", instance->host_no));
1522 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1523 break;
1525 /* Do actual transfer from SCSI bus to / from memory */
1526 if (!(p & SR_IO))
1527 NCR5380_write(OUTPUT_DATA_REG, *d);
1528 else
1529 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1531 ++d;
1534 * The SCSI standard suggests that in MSGOUT phase, the initiator
1535 * should drop ATN on the last byte of the message phase
1536 * after REQ has been asserted for the handshake but before
1537 * the initiator raises ACK.
1540 if (!(p & SR_IO)) {
1541 if (!((p & SR_MSG) && c > 1)) {
1542 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1543 NCR5380_dprint(NDEBUG_PIO, instance);
1544 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1545 } else {
1546 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1547 NCR5380_dprint(NDEBUG_PIO, instance);
1548 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1550 } else {
1551 NCR5380_dprint(NDEBUG_PIO, instance);
1552 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1555 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
1556 dprintk(NDEBUG_HANDSHAKE, ("scsi%d : req false, handshake complete\n", instance->host_no));
1559 * We have several special cases to consider during REQ/ACK handshaking :
1560 * 1. We were in MSGOUT phase, and we are on the last byte of the
1561 * message. ATN must be dropped as ACK is dropped.
1563 * 2. We are in a MSGIN phase, and we are on the last byte of the
1564 * message. We must exit with ACK asserted, so that the calling
1565 * code may raise ATN before dropping ACK to reject the message.
1567 * 3. ACK and ATN are clear and the target may proceed as normal.
1569 if (!(p == PHASE_MSGIN && c == 1)) {
1570 if (p == PHASE_MSGOUT && c > 1)
1571 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1572 else
1573 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1575 } while (--c);
1577 dprintk(NDEBUG_PIO, ("scsi%d : residual %d\n", instance->host_no, c));
1579 *count = c;
1580 *data = d;
1581 tmp = NCR5380_read(STATUS_REG);
1582 if (tmp & SR_REQ)
1583 *phase = tmp & PHASE_MASK;
1584 else
1585 *phase = PHASE_UNKNOWN;
1587 if (!c || (*phase == p))
1588 return 0;
1589 else
1590 return -1;
1594 * do_reset - issue a reset command
1595 * @host: adapter to reset
1597 * Issue a reset sequence to the NCR5380 and try and get the bus
1598 * back into sane shape.
1600 * Locks: caller holds queue lock
1603 static void do_reset(struct Scsi_Host *host) {
1604 NCR5380_local_declare();
1605 NCR5380_setup(host);
1607 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1608 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1609 udelay(25);
1610 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1614 static int do_abort(struct Scsi_Host *host) {
1615 NCR5380_local_declare();
1616 unsigned char *msgptr, phase, tmp;
1617 int len;
1618 int rc;
1619 NCR5380_setup(host);
1622 /* Request message out phase */
1623 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1626 * Wait for the target to indicate a valid phase by asserting
1627 * REQ. Once this happens, we'll have either a MSGOUT phase
1628 * and can immediately send the ABORT message, or we'll have some
1629 * other phase and will have to source/sink data.
1631 * We really don't care what value was on the bus or what value
1632 * the target sees, so we just handshake.
1635 rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
1637 if(rc < 0)
1638 return -1;
1640 tmp = (unsigned char)rc;
1642 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1644 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1645 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1646 rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ);
1647 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1648 if(rc == -1)
1649 return -1;
1651 tmp = ABORT;
1652 msgptr = &tmp;
1653 len = 1;
1654 phase = PHASE_MSGOUT;
1655 NCR5380_transfer_pio(host, &phase, &len, &msgptr);
1658 * If we got here, and the command completed successfully,
1659 * we're about to go into bus free state.
1662 return len ? -1 : 0;
1665 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1667 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1668 * unsigned char *phase, int *count, unsigned char **data)
1670 * Purpose : transfers data in given phase using either real
1671 * or pseudo DMA.
1673 * Inputs : instance - instance of driver, *phase - pointer to
1674 * what phase is expected, *count - pointer to number of
1675 * bytes to transfer, **data - pointer to data pointer.
1677 * Returns : -1 when different phase is entered without transferring
1678 * maximum number of bytes, 0 if all bytes or transfered or exit
1679 * is in same phase.
1681 * Also, *phase, *count, *data are modified in place.
1683 * Locks: io_request lock held by caller
1687 static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1688 NCR5380_local_declare();
1689 register int c = *count;
1690 register unsigned char p = *phase;
1691 register unsigned char *d = *data;
1692 unsigned char tmp;
1693 int foo;
1694 #if defined(REAL_DMA_POLL)
1695 int cnt, toPIO;
1696 unsigned char saved_data = 0, overrun = 0, residue;
1697 #endif
1699 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
1701 NCR5380_setup(instance);
1703 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1704 *phase = tmp;
1705 return -1;
1707 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1708 #ifdef READ_OVERRUNS
1709 if (p & SR_IO) {
1710 c -= 2;
1712 #endif
1713 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));
1714 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1715 #endif
1717 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1719 #ifdef REAL_DMA
1720 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1721 #elif defined(REAL_DMA_POLL)
1722 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1723 #else
1725 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1726 * were not disabled for the duration of a single DMA transfer, from
1727 * before the setting of DMA mode to after transfer of the last byte.
1730 #if defined(PSEUDO_DMA) && defined(UNSAFE)
1731 spin_unlock_irq(instance->host_lock);
1732 #endif
1733 /* KLL May need eop and parity in 53c400 */
1734 if (hostdata->flags & FLAG_NCR53C400)
1735 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
1736 MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
1737 MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1738 else
1739 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1740 #endif /* def REAL_DMA */
1742 dprintk(NDEBUG_DMA, ("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG)));
1745 * On the PAS16 at least I/O recovery delays are not needed here.
1746 * Everyone else seems to want them.
1749 if (p & SR_IO) {
1750 io_recovery_delay(1);
1751 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1752 } else {
1753 io_recovery_delay(1);
1754 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1755 io_recovery_delay(1);
1756 NCR5380_write(START_DMA_SEND_REG, 0);
1757 io_recovery_delay(1);
1760 #if defined(REAL_DMA_POLL)
1761 do {
1762 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1763 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1766 if (p & SR_IO) {
1767 #ifdef READ_OVERRUNS
1768 udelay(10);
1769 if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
1770 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1771 overrun = 1;
1773 #endif
1774 } else {
1775 int limit = 100;
1776 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1777 if (!(tmp & BASR_PHASE_MATCH))
1778 break;
1779 if (--limit < 0)
1780 break;
1784 dprintk(NDEBUG_DMA, ("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG)));
1786 NCR5380_write(MODE_REG, MR_BASE);
1787 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1789 residue = NCR5380_dma_residual(instance);
1790 c -= residue;
1791 *count -= c;
1792 *data += c;
1793 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1795 #ifdef READ_OVERRUNS
1796 if (*phase == p && (p & SR_IO) && residue == 0) {
1797 if (overrun) {
1798 dprintk(NDEBUG_DMA, ("Got an input overrun, using saved byte\n"));
1799 **data = saved_data;
1800 *data += 1;
1801 *count -= 1;
1802 cnt = toPIO = 1;
1803 } else {
1804 printk("No overrun??\n");
1805 cnt = toPIO = 2;
1807 dprintk(NDEBUG_DMA, ("Doing %d-byte PIO to 0x%X\n", cnt, *data));
1808 NCR5380_transfer_pio(instance, phase, &cnt, data);
1809 *count -= toPIO - cnt;
1811 #endif
1813 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)));
1814 return 0;
1816 #elif defined(REAL_DMA)
1817 return 0;
1818 #else /* defined(REAL_DMA_POLL) */
1819 if (p & SR_IO) {
1820 #ifdef DMA_WORKS_RIGHT
1821 foo = NCR5380_pread(instance, d, c);
1822 #else
1823 int diff = 1;
1824 if (hostdata->flags & FLAG_NCR53C400) {
1825 diff = 0;
1827 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
1829 if (!(hostdata->flags & FLAG_NCR53C400)) {
1830 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
1831 /* Wait for clean handshake */
1832 while (NCR5380_read(STATUS_REG) & SR_REQ);
1833 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1836 #endif
1837 } else {
1838 #ifdef DMA_WORKS_RIGHT
1839 foo = NCR5380_pwrite(instance, d, c);
1840 #else
1841 int timeout;
1842 dprintk(NDEBUG_C400_PWRITE, ("About to pwrite %d bytes\n", c));
1843 if (!(foo = NCR5380_pwrite(instance, d, c))) {
1845 * Wait for the last byte to be sent. If REQ is being asserted for
1846 * the byte we're interested, we'll ACK it and it will go false.
1848 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
1849 timeout = 20000;
1850 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
1852 if (!timeout)
1853 dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : timed out on last byte\n", instance->host_no));
1855 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
1856 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
1857 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
1858 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
1859 dprintk(NDEBUG_LAST_WRITE_SENT, ("scsi%d : last bit sent works\n", instance->host_no));
1862 } else {
1863 dprintk(NDEBUG_C400_PWRITE, ("Waiting for LASTBYTE\n"));
1864 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
1865 dprintk(NDEBUG_C400_PWRITE, ("Got LASTBYTE\n"));
1868 #endif
1870 NCR5380_write(MODE_REG, MR_BASE);
1871 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1873 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
1874 dprintk(NDEBUG_C400_PWRITE, ("53C400w: Checking for IRQ\n"));
1875 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
1876 dprintk(NDEBUG_C400_PWRITE, ("53C400w: got it, reading reset interrupt reg\n"));
1877 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1878 } else {
1879 printk("53C400w: IRQ NOT THERE!\n");
1882 *data = d + c;
1883 *count = 0;
1884 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1885 #if defined(PSEUDO_DMA) && defined(UNSAFE)
1886 spin_lock_irq(instance->host_lock);
1887 #endif /* defined(REAL_DMA_POLL) */
1888 return foo;
1889 #endif /* def REAL_DMA */
1891 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1894 static void NCR5380_information_transfer(struct Scsi_Host *instance) {
1895 NCR5380_local_declare();
1896 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
1897 unsigned char msgout = NOP;
1898 int sink = 0;
1899 int len;
1900 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1901 int transfersize;
1902 #endif
1903 unsigned char *data;
1904 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1905 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
1906 /* RvC: we need to set the end of the polling time */
1907 unsigned long poll_time = jiffies + USLEEP_POLL;
1909 NCR5380_setup(instance);
1911 while (1) {
1912 tmp = NCR5380_read(STATUS_REG);
1913 /* We only have a valid SCSI phase when REQ is asserted */
1914 if (tmp & SR_REQ) {
1915 phase = (tmp & PHASE_MASK);
1916 if (phase != old_phase) {
1917 old_phase = phase;
1918 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1920 if (sink && (phase != PHASE_MSGOUT)) {
1921 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1923 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1924 while (NCR5380_read(STATUS_REG) & SR_REQ);
1925 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1926 sink = 0;
1927 continue;
1929 switch (phase) {
1930 case PHASE_DATAIN:
1931 case PHASE_DATAOUT:
1932 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1933 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
1934 sink = 1;
1935 do_abort(instance);
1936 cmd->result = DID_ERROR << 16;
1937 cmd->scsi_done(cmd);
1938 return;
1939 #endif
1941 * If there is no room left in the current buffer in the
1942 * scatter-gather list, move onto the next one.
1945 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1946 ++cmd->SCp.buffer;
1947 --cmd->SCp.buffers_residual;
1948 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1949 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
1950 dprintk(NDEBUG_INFORMATION, ("scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
1953 * The preferred transfer method is going to be
1954 * PSEUDO-DMA for systems that are strictly PIO,
1955 * since we can let the hardware do the handshaking.
1957 * For this to work, we need to know the transfersize
1958 * ahead of time, since the pseudo-DMA code will sit
1959 * in an unconditional loop.
1962 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1963 /* KLL
1964 * PSEUDO_DMA is defined here. If this is the g_NCR5380
1965 * driver then it will always be defined, so the
1966 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
1967 * NCR5380 case. I think this is a fairly clean solution.
1968 * We supplement these 2 if's with the flag.
1970 #ifdef NCR5380_dma_xfer_len
1971 if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
1972 #else
1973 transfersize = cmd->transfersize;
1975 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
1976 if (transfersize > 512)
1977 transfersize = 512;
1978 #endif /* LIMIT_TRANSFERSIZE */
1980 if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
1981 /* Limit transfers to 32K, for xx400 & xx406
1982 * pseudoDMA that transfers in 128 bytes blocks. */
1983 if (transfersize > 32 * 1024)
1984 transfersize = 32 * 1024;
1985 #endif
1986 len = transfersize;
1987 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1989 * If the watchdog timer fires, all future accesses to this
1990 * device will use the polled-IO.
1992 scmd_printk(KERN_INFO, cmd,
1993 "switching to slow handshake\n");
1994 cmd->device->borken = 1;
1995 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1996 sink = 1;
1997 do_abort(instance);
1998 cmd->result = DID_ERROR << 16;
1999 cmd->scsi_done(cmd);
2000 } else
2001 cmd->SCp.this_residual -= transfersize - len;
2002 } else
2003 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2004 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
2005 &cmd->SCp.ptr);
2006 break;
2007 case PHASE_MSGIN:
2008 len = 1;
2009 data = &tmp;
2010 NCR5380_transfer_pio(instance, &phase, &len, &data);
2011 cmd->SCp.Message = tmp;
2013 switch (tmp) {
2015 * Linking lets us reduce the time required to get the
2016 * next command out to the device, hopefully this will
2017 * mean we don't waste another revolution due to the delays
2018 * required by ARBITRATION and another SELECTION.
2020 * In the current implementation proposal, low level drivers
2021 * merely have to start the next command, pointed to by
2022 * next_link, done() is called as with unlinked commands.
2024 #ifdef LINKED
2025 case LINKED_CMD_COMPLETE:
2026 case LINKED_FLG_CMD_COMPLETE:
2027 /* Accept message by clearing ACK */
2028 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2029 dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun));
2031 * Sanity check : A linked command should only terminate with
2032 * one of these messages if there are more linked commands
2033 * available.
2035 if (!cmd->next_link) {
2036 printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
2037 sink = 1;
2038 do_abort(instance);
2039 return;
2041 initialize_SCp(cmd->next_link);
2042 /* The next command is still part of this process */
2043 cmd->next_link->tag = cmd->tag;
2044 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2045 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));
2046 collect_stats(hostdata, cmd);
2047 cmd->scsi_done(cmd);
2048 cmd = hostdata->connected;
2049 break;
2050 #endif /* def LINKED */
2051 case ABORT:
2052 case COMMAND_COMPLETE:
2053 /* Accept message by clearing ACK */
2054 sink = 1;
2055 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2056 hostdata->connected = NULL;
2057 dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->device->id, cmd->device->lun));
2058 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2061 * I'm not sure what the correct thing to do here is :
2063 * If the command that just executed is NOT a request
2064 * sense, the obvious thing to do is to set the result
2065 * code to the values of the stored parameters.
2067 * If it was a REQUEST SENSE command, we need some way
2068 * to differentiate between the failure code of the original
2069 * and the failure code of the REQUEST sense - the obvious
2070 * case is success, where we fall through and leave the result
2071 * code unchanged.
2073 * The non-obvious place is where the REQUEST SENSE failed
2076 if (cmd->cmnd[0] != REQUEST_SENSE)
2077 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2078 else if (status_byte(cmd->SCp.Status) != GOOD)
2079 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2081 #ifdef AUTOSENSE
2082 if ((cmd->cmnd[0] == REQUEST_SENSE) &&
2083 hostdata->ses.cmd_len) {
2084 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
2085 hostdata->ses.cmd_len = 0 ;
2088 if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
2089 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
2091 dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no));
2093 LIST(cmd, hostdata->issue_queue);
2094 cmd->host_scribble = (unsigned char *)
2095 hostdata->issue_queue;
2096 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2097 dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
2098 } else
2099 #endif /* def AUTOSENSE */
2101 collect_stats(hostdata, cmd);
2102 cmd->scsi_done(cmd);
2105 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2107 * Restore phase bits to 0 so an interrupted selection,
2108 * arbitration can resume.
2110 NCR5380_write(TARGET_COMMAND_REG, 0);
2112 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2113 barrier();
2114 return;
2115 case MESSAGE_REJECT:
2116 /* Accept message by clearing ACK */
2117 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2118 switch (hostdata->last_message) {
2119 case HEAD_OF_QUEUE_TAG:
2120 case ORDERED_QUEUE_TAG:
2121 case SIMPLE_QUEUE_TAG:
2122 cmd->device->simple_tags = 0;
2123 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2124 break;
2125 default:
2126 break;
2128 case DISCONNECT:{
2129 /* Accept message by clearing ACK */
2130 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2131 cmd->device->disconnect = 1;
2132 LIST(cmd, hostdata->disconnected_queue);
2133 cmd->host_scribble = (unsigned char *)
2134 hostdata->disconnected_queue;
2135 hostdata->connected = NULL;
2136 hostdata->disconnected_queue = cmd;
2137 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));
2139 * Restore phase bits to 0 so an interrupted selection,
2140 * arbitration can resume.
2142 NCR5380_write(TARGET_COMMAND_REG, 0);
2144 /* Enable reselect interrupts */
2145 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2146 /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
2147 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2148 barrier();
2149 return;
2152 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2153 * operation, in violation of the SCSI spec so we can safely
2154 * ignore SAVE/RESTORE pointers calls.
2156 * Unfortunately, some disks violate the SCSI spec and
2157 * don't issue the required SAVE_POINTERS message before
2158 * disconnecting, and we have to break spec to remain
2159 * compatible.
2161 case SAVE_POINTERS:
2162 case RESTORE_POINTERS:
2163 /* Accept message by clearing ACK */
2164 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2165 break;
2166 case EXTENDED_MESSAGE:
2168 * Extended messages are sent in the following format :
2169 * Byte
2170 * 0 EXTENDED_MESSAGE == 1
2171 * 1 length (includes one byte for code, doesn't
2172 * include first two bytes)
2173 * 2 code
2174 * 3..length+1 arguments
2176 * Start the extended message buffer with the EXTENDED_MESSAGE
2177 * byte, since spi_print_msg() wants the whole thing.
2179 extended_msg[0] = EXTENDED_MESSAGE;
2180 /* Accept first byte by clearing ACK */
2181 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2182 dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no));
2184 len = 2;
2185 data = extended_msg + 1;
2186 phase = PHASE_MSGIN;
2187 NCR5380_transfer_pio(instance, &phase, &len, &data);
2189 dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]));
2191 if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
2192 /* Accept third byte by clearing ACK */
2193 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2194 len = extended_msg[1] - 1;
2195 data = extended_msg + 3;
2196 phase = PHASE_MSGIN;
2198 NCR5380_transfer_pio(instance, &phase, &len, &data);
2199 dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len));
2201 switch (extended_msg[2]) {
2202 case EXTENDED_SDTR:
2203 case EXTENDED_WDTR:
2204 case EXTENDED_MODIFY_DATA_POINTER:
2205 case EXTENDED_EXTENDED_IDENTIFY:
2206 tmp = 0;
2208 } else if (len) {
2209 printk("scsi%d: error receiving extended message\n", instance->host_no);
2210 tmp = 0;
2211 } else {
2212 printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
2213 tmp = 0;
2215 /* Fall through to reject message */
2218 * If we get something weird that we aren't expecting,
2219 * reject it.
2221 default:
2222 if (!tmp) {
2223 printk("scsi%d: rejecting message ", instance->host_no);
2224 spi_print_msg(extended_msg);
2225 printk("\n");
2226 } else if (tmp != EXTENDED_MESSAGE)
2227 scmd_printk(KERN_INFO, cmd,
2228 "rejecting unknown message %02x\n",tmp);
2229 else
2230 scmd_printk(KERN_INFO, cmd,
2231 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
2233 msgout = MESSAGE_REJECT;
2234 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2235 break;
2236 } /* switch (tmp) */
2237 break;
2238 case PHASE_MSGOUT:
2239 len = 1;
2240 data = &msgout;
2241 hostdata->last_message = msgout;
2242 NCR5380_transfer_pio(instance, &phase, &len, &data);
2243 if (msgout == ABORT) {
2244 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2245 hostdata->connected = NULL;
2246 cmd->result = DID_ERROR << 16;
2247 collect_stats(hostdata, cmd);
2248 cmd->scsi_done(cmd);
2249 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2250 return;
2252 msgout = NOP;
2253 break;
2254 case PHASE_CMDOUT:
2255 len = cmd->cmd_len;
2256 data = cmd->cmnd;
2257 NCR5380_transfer_pio(instance, &phase, &len, &data);
2258 if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
2259 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
2260 dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
2261 return;
2263 break;
2264 case PHASE_STATIN:
2265 len = 1;
2266 data = &tmp;
2267 NCR5380_transfer_pio(instance, &phase, &len, &data);
2268 cmd->SCp.Status = tmp;
2269 break;
2270 default:
2271 printk("scsi%d : unknown phase\n", instance->host_no);
2272 NCR5380_dprint(NDEBUG_ALL, instance);
2273 } /* switch(phase) */
2274 } /* if (tmp * SR_REQ) */
2275 else {
2276 /* RvC: go to sleep if polling time expired
2278 if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
2279 NCR5380_set_timer(hostdata, USLEEP_SLEEP);
2280 dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
2281 return;
2284 } /* while (1) */
2288 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2290 * Purpose : does reselection, initializing the instance->connected
2291 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2292 * nexus has been reestablished,
2294 * Inputs : instance - this instance of the NCR5380.
2296 * Locks: io_request_lock held by caller if IRQ driven
2299 static void NCR5380_reselect(struct Scsi_Host *instance) {
2300 NCR5380_local_declare();
2301 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
2302 instance->hostdata;
2303 unsigned char target_mask;
2304 unsigned char lun, phase;
2305 int len;
2306 unsigned char msg[3];
2307 unsigned char *data;
2308 Scsi_Cmnd *tmp = NULL, *prev;
2309 int abort = 0;
2310 NCR5380_setup(instance);
2313 * Disable arbitration, etc. since the host adapter obviously
2314 * lost, and tell an interrupted NCR5380_select() to restart.
2317 NCR5380_write(MODE_REG, MR_BASE);
2318 hostdata->restart_select = 1;
2320 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2321 dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no));
2324 * At this point, we have detected that our SCSI ID is on the bus,
2325 * SEL is true and BSY was false for at least one bus settle delay
2326 * (400 ns).
2328 * We must assert BSY ourselves, until the target drops the SEL
2329 * signal.
2332 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2334 if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
2335 abort = 1;
2337 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2340 if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
2341 abort = 1;
2343 len = 1;
2344 data = msg;
2345 phase = PHASE_MSGIN;
2346 NCR5380_transfer_pio(instance, &phase, &len, &data);
2348 if (!(msg[0] & 0x80)) {
2349 printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
2350 spi_print_msg(msg);
2351 abort = 1;
2352 } else {
2353 /* Accept message by clearing ACK */
2354 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2355 lun = (msg[0] & 0x07);
2358 * We need to add code for SCSI-II to track which devices have
2359 * I_T_L_Q nexuses established, and which have simple I_T_L
2360 * nexuses so we can chose to do additional data transfer.
2364 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2365 * just reestablished, and remove it from the disconnected queue.
2369 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
2370 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2372 if (prev) {
2373 REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
2374 prev->host_scribble = tmp->host_scribble;
2375 } else {
2376 REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
2377 hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
2379 tmp->host_scribble = NULL;
2380 break;
2382 if (!tmp) {
2383 printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
2385 * Since we have an established nexus that we can't do anything with,
2386 * we must abort it.
2388 abort = 1;
2392 if (abort) {
2393 do_abort(instance);
2394 } else {
2395 hostdata->connected = tmp;
2396 dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->target, tmp->lun, tmp->tag));
2401 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2403 * Purpose : called by interrupt handler when DMA finishes or a phase
2404 * mismatch occurs (which would finish the DMA transfer).
2406 * Inputs : instance - this instance of the NCR5380.
2408 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2409 * nexus has been reestablished, on failure NULL is returned.
2412 #ifdef REAL_DMA
2413 static void NCR5380_dma_complete(NCR5380_instance * instance) {
2414 NCR5380_local_declare();
2415 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2416 int transferred;
2417 NCR5380_setup(instance);
2420 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2422 NCR5380_write(MODE_REG, MR_BASE);
2423 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2426 * The only places we should see a phase mismatch and have to send
2427 * data from the same set of pointers will be the data transfer
2428 * phases. So, residual, requested length are only important here.
2431 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2432 transferred = instance->dmalen - NCR5380_dma_residual();
2433 hostdata->connected->SCp.this_residual -= transferred;
2434 hostdata->connected->SCp.ptr += transferred;
2437 #endif /* def REAL_DMA */
2440 static int NCR5380_abort(Scsi_Cmnd * cmd) {
2441 NCR5380_local_declare();
2442 struct Scsi_Host *instance = cmd->device->host;
2443 struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
2444 Scsi_Cmnd *tmp, **prev;
2446 printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
2447 scsi_print_command(cmd);
2449 NCR5380_print_status(instance);
2451 NCR5380_setup(instance);
2453 dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no));
2454 dprintk(NDEBUG_ABORT, (" basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)));
2458 * Case 2 : If the command hasn't been issued yet, we simply remove it
2459 * from the issue queue.
2462 dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no));
2463 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)
2464 if (cmd == tmp) {
2465 REMOVE(5, *prev, tmp, tmp->host_scribble);
2466 (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
2467 tmp->host_scribble = NULL;
2468 tmp->result = DID_ABORT << 16;
2469 dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no));
2470 tmp->scsi_done(tmp);
2471 return SUCCESS;
2473 #if (NDEBUG & NDEBUG_ABORT)
2474 /* KLL */
2475 else if (prev == tmp)
2476 printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
2477 #endif
2480 * Case 3 : If any commands are connected, we're going to fail the abort
2481 * and let the high level SCSI driver retry at a later time or
2482 * issue a reset.
2484 * Timeouts, and therefore aborted commands, will be highly unlikely
2485 * and handling them cleanly in this situation would make the common
2486 * case of noresets less efficient, and would pollute our code. So,
2487 * we fail.
2490 if (hostdata->connected) {
2491 dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no));
2492 return FAILED;
2495 * Case 4: If the command is currently disconnected from the bus, and
2496 * there are no connected commands, we reconnect the I_T_L or
2497 * I_T_L_Q nexus associated with it, go into message out, and send
2498 * an abort message.
2500 * This case is especially ugly. In order to reestablish the nexus, we
2501 * need to call NCR5380_select(). The easiest way to implement this
2502 * function was to abort if the bus was busy, and let the interrupt
2503 * handler triggered on the SEL for reselect take care of lost arbitrations
2504 * where necessary, meaning interrupts need to be enabled.
2506 * When interrupts are enabled, the queues may change - so we
2507 * can't remove it from the disconnected queue before selecting it
2508 * because that could cause a failure in hashing the nexus if that
2509 * device reselected.
2511 * Since the queues may change, we can't use the pointers from when we
2512 * first locate it.
2514 * So, we must first locate the command, and if NCR5380_select()
2515 * succeeds, then issue the abort, relocate the command and remove
2516 * it from the disconnected queue.
2519 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
2520 if (cmd == tmp) {
2521 dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no));
2523 if (NCR5380_select(instance, cmd, (int) cmd->tag))
2524 return FAILED;
2525 dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no));
2527 do_abort(instance);
2529 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)
2530 if (cmd == tmp) {
2531 REMOVE(5, *prev, tmp, tmp->host_scribble);
2532 *prev = (Scsi_Cmnd *) tmp->host_scribble;
2533 tmp->host_scribble = NULL;
2534 tmp->result = DID_ABORT << 16;
2535 tmp->scsi_done(tmp);
2536 return SUCCESS;
2540 * Case 5 : If we reached this point, the command was not found in any of
2541 * the queues.
2543 * We probably reached this point because of an unlikely race condition
2544 * between the command completing successfully and the abortion code,
2545 * so we won't panic, but we will notify the user in case something really
2546 * broke.
2548 printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
2549 " before abortion\n", instance->host_no);
2550 return FAILED;
2555 * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
2557 * Purpose : reset the SCSI bus.
2559 * Returns : SUCCESS
2561 * Locks: host lock taken by caller
2564 static int NCR5380_bus_reset(Scsi_Cmnd * cmd)
2566 struct Scsi_Host *instance = cmd->device->host;
2568 NCR5380_local_declare();
2569 NCR5380_setup(instance);
2570 NCR5380_print_status(instance);
2572 spin_lock_irq(instance->host_lock);
2573 do_reset(instance);
2574 spin_unlock_irq(instance->host_lock);
2576 return SUCCESS;