2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
6 * Note that these routines also work with NR53c400 family chips.
8 * Copyright 1993, Drew Eckhardt
10 * (Unix and Linux consulting and custom programming)
14 * DISTRIBUTION RELEASE 6.
16 * For more information, please consult
19 * SCSI Protocol Controller
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
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)
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
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
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>
96 #define NDEBUG_ABORT 0
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); }
104 #define REMOVE(w,x,y,z)
114 #define READ_OVERRUNS
117 #ifdef BOARD_REQUIRES_NO_DELAY
118 #define io_recovery_delay(x)
120 #define io_recovery_delay(x) udelay(x)
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
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
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
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
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
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
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
;
232 cmd
->SCp
.buffer
= NULL
;
233 cmd
->SCp
.buffers_residual
= 0;
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
;
260 NCR5380_setup(instance
);
264 r
= NCR5380_read(reg
);
271 while(time_before(jiffies
, end
))
273 r
= NCR5380_read(reg
);
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"}
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"},
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"},
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
);
379 * NCR5380_print_phase - show SCSI phase
380 * @instance: adapter to dump
382 * Print the current SCSI phase for debugging purposes
387 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
389 NCR5380_local_declare();
390 unsigned char status
;
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
);
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
);
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.
415 /* 20 ms (reasonable hard disk speed) */
416 #define USLEEP_SLEEP (20*HZ/1000)
418 /* 300 RPM (floppy speed) */
420 #define USLEEP_POLL (200*HZ/1000)
422 #ifndef USLEEP_WAITLONG
423 /* RvC: (reasonable time to wait on select error) */
424 #define USLEEP_WAITLONG USLEEP_SLEEP
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
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
)
458 return DISCONNECT_TIME_TO_DATA
;
463 return DISCONNECT_LONG
;
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
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
)
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
,
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))
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
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
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
)
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
560 static void __init __maybe_unused
561 NCR5380_print_options(struct Scsi_Host
*instance
)
563 printk(" generic options"
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
627 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
629 char *lprint_Scsi_Cmnd(Scsi_Cmnd
* cmd
, char *pos
, char *buffer
, int length
);
631 char *lprint_command(unsigned char *cmd
, char *pos
, char *buffer
, int len
);
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
)
639 struct NCR5380_hostdata
*hostdata
;
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;
648 #ifdef PAS16_PUBLIC_RELEASE
649 pas_wmaxi
= pas_maxi
= 0;
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
);
659 #ifdef T128_PUBLIC_RELEASE
660 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE
);
662 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
663 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE
);
665 #ifdef PAS16_PUBLIC_RELEASE
666 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE
);
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");
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
);
679 #ifdef PAS16_PUBLIC_RELEASE
680 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi
, pas_maxi
);
682 spin_lock_irq(instance
->host_lock
);
683 if (!hostdata
->connected
)
684 SPRINTF("scsi%d: no currently connected command\n", instance
->host_no
);
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
);
697 if (pos
- buffer
< offset
)
699 else if (pos
- buffer
- offset
< length
)
700 return pos
- buffer
- offset
;
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
);
712 static char *lprint_command(unsigned char *command
, char *pos
, char *buffer
, int length
)
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
]);
722 static char *lprint_opcode(int opcode
, char *pos
, char *buffer
, int length
)
724 SPRINTF("%2d (0x%02x)", opcode
, opcode
);
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();
749 unsigned long timeout
;
750 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*) instance
->hostdata
;
753 printk(KERN_ERR
"NCR5380_init called with interrupts off!\n");
755 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
760 if (flags
& FLAG_NCR53C400
)
761 instance
->NCR5380_instance_name
+= NCR53C400_address_adjust
;
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;
774 hostdata
->dmalen
= 0;
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
);
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;
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
;
799 hostdata
->flags
= FLAG_CHECK_LAST_BYTE_SENT
| flags
;
801 hostdata
->host
= instance
;
802 hostdata
->time_expires
= 0;
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);
816 if (hostdata
->flags
& FLAG_NCR53C400
) {
817 NCR5380_write(C400_CONTROL_STATUS_REG
, CSR_BASE
);
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
) {
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
);
843 printk(KERN_WARNING
"scsi%d: bus busy, attempting abort\n", instance
->host_no
);
847 printk(KERN_WARNING
"scsi%d: bus busy, attempting reset\n", instance
->host_no
);
851 printk(KERN_ERR
"scsi%d: bus locked solid or invalid override\n", instance
->host_no
);
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
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
;
889 #if (NDEBUG & NDEBUG_NO_WRITE)
890 switch (cmd
->cmnd
[0]) {
893 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance
->host_no
);
894 cmd
->result
= (DID_ERROR
<< 16);
898 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
901 switch (cmd
->cmnd
[0]) {
905 hostdata
->time_write
[cmd
->device
->id
] -= (jiffies
- hostdata
->timebase
);
906 hostdata
->bytes_write
[cmd
->device
->id
] += scsi_bufflen(cmd
);
907 hostdata
->pendingw
++;
912 hostdata
->time_read
[cmd
->device
->id
] -= (jiffies
- hostdata
->timebase
);
913 hostdata
->bytes_read
[cmd
->device
->id
] += scsi_bufflen(cmd
);
914 hostdata
->pendingr
++;
920 * We use the host_scribble field as a pointer to the next command
924 cmd
->host_scribble
= NULL
;
925 cmd
->scsi_done
= done
;
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
;
940 for (tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
; tmp
->host_scribble
; tmp
= (Scsi_Cmnd
*) tmp
->host_scribble
);
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);
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
;
973 spin_lock_irq(instance
->host_lock
);
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
)
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
))) {
990 REMOVE(prev
, prev
->host_scribble
, tmp
, tmp
->host_scribble
);
991 prev
->host_scribble
= tmp
->host_scribble
;
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
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
1026 (tmp
->cmnd
[0] == REQUEST_SENSE
) ? TAG_NONE
: TAG_NEXT
)) {
1029 LIST(tmp
, hostdata
->issue_queue
);
1030 tmp
->host_scribble
= (unsigned char *) hostdata
->issue_queue
;
1031 hostdata
->issue_queue
= tmp
;
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 */
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
)) {
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
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
1059 && !hostdata
->dmalen
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
));
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()
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
;
1095 unsigned long flags
;
1097 dprintk(NDEBUG_INTR
, ("scsi : NCR5380 irq %d triggered\n",
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
)) {
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
);
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
))) {
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
);
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
);
1151 spin_unlock_irqrestore(instance
->host_lock
, flags
);
1153 schedule_delayed_work(&hostdata
->coroutine
, 0);
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]) {
1175 hostdata
->time_write
[scmd_id(cmd
)] += (jiffies
- hostdata
->timebase
);
1176 hostdata
->pendingw
--;
1181 hostdata
->time_read
[scmd_id(cmd
)] += (jiffies
- hostdata
->timebase
);
1182 hostdata
->pendingr
--;
1190 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
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
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
;
1229 unsigned long timeout
;
1230 unsigned char value
;
1232 NCR5380_setup(instance
);
1234 if (hostdata
->selecting
)
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
);
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
);
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().
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
));
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
));
1300 * Again, bus clear + bus settle time is 1.2us, however, this is
1301 * a minimum so we'll udelay ceil(1.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
1334 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
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
1352 * wingel suggests that this could be due to failing to wait
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
1365 timeout
= jiffies
+ (250 * HZ
/ 1000);
1368 hostdata
->select_time
= 0; /* we count the clock ticks at which we polled */
1369 hostdata
->selecting
= cmd
;
1372 /* RvC: here we enter after a sleeping period, or immediately after
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,
1385 hostdata
->selecting
= NULL
;/* clear this pointer, because we passed the
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
);
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
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
);
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
);
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
);
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
);
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
);
1443 /* Send message(s) */
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
);
1455 /* Selection failed */
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
;
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
);
1481 dprintk(NDEBUG_PIO
, ("scsi%d : pio write %d bytes\n", instance
->host_no
, c
));
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
)) {
1501 * Wait for assertion of REQ, after which the phase bits will be
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
);
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
);
1525 /* Do actual transfer from SCSI bus to / from memory */
1527 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1529 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
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.
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
);
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
);
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
);
1573 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1577 dprintk(NDEBUG_PIO
, ("scsi%d : residual %d\n", instance
->host_no
, c
));
1581 tmp
= NCR5380_read(STATUS_REG
);
1583 *phase
= tmp
& PHASE_MASK
;
1585 *phase
= PHASE_UNKNOWN
;
1587 if (!c
|| (*phase
== p
))
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
);
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
;
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
);
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
);
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
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
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
;
1694 #if defined(REAL_DMA_POLL)
1696 unsigned char saved_data
= 0, overrun
= 0, residue
;
1699 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*) instance
->hostdata
;
1701 NCR5380_setup(instance
);
1703 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1707 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1708 #ifdef READ_OVERRUNS
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
);
1717 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
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
);
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
);
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
);
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.
1750 io_recovery_delay(1);
1751 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
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)
1762 tmp
= NCR5380_read(BUS_AND_STATUS_REG
);
1763 } while ((tmp
& BASR_PHASE_MATCH
) && !(tmp
& (BASR_BUSY_ERROR
| BASR_END_DMA_TRANSFER
)));
1767 #ifdef READ_OVERRUNS
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
);
1776 while (((tmp
= NCR5380_read(BUS_AND_STATUS_REG
)) & BASR_ACK
) || (NCR5380_read(STATUS_REG
) & SR_REQ
)) {
1777 if (!(tmp
& BASR_PHASE_MATCH
))
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
);
1793 *phase
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1795 #ifdef READ_OVERRUNS
1796 if (*phase
== p
&& (p
& SR_IO
) && residue
== 0) {
1798 dprintk(NDEBUG_DMA
, ("Got an input overrun, using saved byte\n"));
1799 **data
= saved_data
;
1804 printk("No overrun??\n");
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
;
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
)));
1816 #elif defined(REAL_DMA)
1818 #else /* defined(REAL_DMA_POLL) */
1820 #ifdef DMA_WORKS_RIGHT
1821 foo
= NCR5380_pread(instance
, d
, c
);
1824 if (hostdata
->flags
& FLAG_NCR53C400
) {
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
);
1838 #ifdef DMA_WORKS_RIGHT
1839 foo
= NCR5380_pwrite(instance
, d
, c
);
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
)) {
1850 while (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_DRQ
) && (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
));
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
));
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"));
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
);
1879 printk("53C400w: IRQ NOT THERE!\n");
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) */
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
;
1900 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
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
);
1912 tmp
= NCR5380_read(STATUS_REG
);
1913 /* We only have a valid SCSI phase when REQ is asserted */
1915 phase
= (tmp
& PHASE_MASK
);
1916 if (phase
!= old_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
);
1932 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1933 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance
->host_no
);
1936 cmd
->result
= DID_ERROR
<< 16;
1937 cmd
->scsi_done(cmd
);
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
) {
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)
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) {
1973 transfersize
= cmd
->transfersize
;
1975 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
1976 if (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;
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
);
1998 cmd
->result
= DID_ERROR
<< 16;
1999 cmd
->scsi_done(cmd
);
2001 cmd
->SCp
.this_residual
-= transfersize
- len
;
2003 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2004 NCR5380_transfer_pio(instance
, &phase
, (int *) &cmd
->SCp
.this_residual
, (unsigned char **)
2010 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2011 cmd
->SCp
.Message
= 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.
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
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
);
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
;
2050 #endif /* def LINKED */
2052 case COMMAND_COMPLETE
:
2053 /* Accept message by clearing ACK */
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
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);
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
));
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
)
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
);
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
)
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
2162 case RESTORE_POINTERS
:
2163 /* Accept message by clearing ACK */
2164 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2166 case EXTENDED_MESSAGE
:
2168 * Extended messages are sent in the following format :
2170 * 0 EXTENDED_MESSAGE == 1
2171 * 1 length (includes one byte for code, doesn't
2172 * include first two bytes)
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
));
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]) {
2204 case EXTENDED_MODIFY_DATA_POINTER
:
2205 case EXTENDED_EXTENDED_IDENTIFY
:
2209 printk("scsi%d: error receiving extended message\n", instance
->host_no
);
2212 printk("scsi%d: extended message code %02x length %d is too long\n", instance
->host_no
, extended_msg
[2], extended_msg
[1]);
2215 /* Fall through to reject message */
2218 * If we get something weird that we aren't expecting,
2223 printk("scsi%d: rejecting message ", instance
->host_no
);
2224 spi_print_msg(extended_msg
);
2226 } else if (tmp
!= EXTENDED_MESSAGE
)
2227 scmd_printk(KERN_INFO
, cmd
,
2228 "rejecting unknown message %02x\n",tmp
);
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
);
2236 } /* switch (tmp) */
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
);
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
));
2267 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2268 cmd
->SCp
.Status
= tmp
;
2271 printk("scsi%d : unknown phase\n", instance
->host_no
);
2272 NCR5380_dprint(NDEBUG_ALL
, instance
);
2273 } /* switch(phase) */
2274 } /* if (tmp * SR_REQ) */
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
));
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
*)
2303 unsigned char target_mask
;
2304 unsigned char lun
, phase
;
2306 unsigned char msg
[3];
2307 unsigned char *data
;
2308 Scsi_Cmnd
*tmp
= NULL
, *prev
;
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
2328 * We must assert BSY ourselves, until the target drops the SEL
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)
2337 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2340 if(NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, 2*HZ
))
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
);
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
)
2373 REMOVE(prev
, prev
->host_scribble
, tmp
, tmp
->host_scribble
);
2374 prev
->host_scribble
= tmp
->host_scribble
;
2376 REMOVE(-1, hostdata
->disconnected_queue
, tmp
, tmp
->host_scribble
);
2377 hostdata
->disconnected_queue
= (Scsi_Cmnd
*) tmp
->host_scribble
;
2379 tmp
->host_scribble
= NULL
;
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,
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.
2413 static void NCR5380_dma_complete(NCR5380_instance
* instance
) {
2414 NCR5380_local_declare();
2415 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*) instance
->hostdata
;
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
)
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
);
2473 #if (NDEBUG & NDEBUG_ABORT)
2475 else if (prev
== tmp
)
2476 printk(KERN_ERR
"scsi%d : LOOP\n", instance
->host_no
);
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
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,
2490 if (hostdata
->connected
) {
2491 dprintk(NDEBUG_ABORT
, ("scsi%d : abort failed, command connected.\n", instance
->host_no
));
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
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
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
)
2521 dprintk(NDEBUG_ABORT
, ("scsi%d : aborting disconnected command.\n", instance
->host_no
));
2523 if (NCR5380_select(instance
, cmd
, (int) cmd
->tag
))
2525 dprintk(NDEBUG_ABORT
, ("scsi%d : nexus reestablished.\n", instance
->host_no
));
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
)
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
);
2540 * Case 5 : If we reached this point, the command was not found in any of
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
2548 printk(KERN_WARNING
"scsi%d : warning : SCSI command probably completed successfully\n"
2549 " before abortion\n", instance
->host_no
);
2555 * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
2557 * Purpose : reset the SCSI bus.
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
);
2574 spin_unlock_irq(instance
->host_lock
);