2 #define NDEBUG (NDEBUG_RESTART_SELECT | NDEBUG_ABORT)
5 * NCR 5380 generic driver routines. These should make it *trivial*
6 * to implement 5380 SCSI drivers under Linux with a non-trantor
9 * Note that these routines also work with NR53c400 family chips.
11 * Copyright 1993, Drew Eckhardt
13 * (Unix and Linux consulting and custom programming)
17 * DISTRIBUTION RELEASE 6.
19 * For more information, please consult
22 * SCSI Protocol Controller
25 * NCR Microelectronics
26 * 1635 Aeroplaza Drive
27 * Colorado Springs, CO 80916
34 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
36 * added support needed for DTC 3180/3280
37 * fixed a couple of bugs
40 * Revision 1.5 1994/01/19 09:14:57 drew
41 * Fixed udelay() hack that was being used on DATAOUT phases
42 * instead of a proper wait for the final handshake.
44 * Revision 1.4 1994/01/19 06:44:25 drew
45 * *** empty log message ***
47 * Revision 1.3 1994/01/19 05:24:40 drew
48 * Added support for TCR LAST_BYTE_SENT bit.
50 * Revision 1.2 1994/01/15 06:14:11 drew
51 * REAL DMA support, bug fixes.
53 * Revision 1.1 1994/01/15 06:00:54 drew
59 * Further development / testing that should be done :
60 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
61 * code so that everything does the same thing that's done at the
62 * end of a pseudo-DMA read operation.
64 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
65 * basically, transfer size needs to be reduced by one
66 * and the last byte read as is done with PSEUDO_DMA.
70 * 4. Test SCSI-II tagged queueing (I have no devices which support
73 * 5. Test linked command handling code after Eric is ready with
74 * the high level code.
77 #if (NDEBUG & NDEBUG_LISTS)
78 #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
79 #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); }
82 #define REMOVE(w,x,y,z)
100 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
101 * and specifically for each board rather than each chip. This makes their
102 * adaptation to platforms like the Mac (Some of which use NCR5380's)
103 * more difficult than it has to be.
105 * Also, many of the SCSI drivers were written before the command queuing
106 * routines were implemented, meaning their implementations of queued
107 * commands were hacked on rather than designed in from the start.
109 * When I designed the Linux SCSI drivers I figured that
110 * while having two different SCSI boards in a system might be useful
111 * for debugging things, two of the same type wouldn't be used.
112 * Well, I was wrong and a number of users have mailed me about running
113 * multiple high-performance SCSI boards in a server.
115 * Finally, when I get questions from users, I have no idea what
116 * revision of my driver they are running.
118 * This driver attempts to address these problems :
119 * This is a generic 5380 driver. To use it on a different platform,
120 * one simply writes appropriate system specific macros (ie, data
121 * transfer - some PC's will use the I/O bus, 68K's must use
122 * memory mapped) and drops this file in their 'C' wrapper.
124 * As far as command queueing, two queues are maintained for
125 * each 5380 in the system - commands that haven't been issued yet,
126 * and commands that are currently executing. This means that an
127 * unlimited number of commands may be queued, letting
128 * more commands propagate from the higher driver levels giving higher
129 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
130 * allowing multiple commands to propagate all the way to a SCSI-II device
131 * while a command is already executing.
133 * To solve the multiple-boards-in-the-same-system problem,
134 * there is a separate instance structure for each instance
135 * of a 5380 in the system. So, multiple NCR5380 drivers will
136 * be able to coexist with appropriate changes to the high level
139 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
140 * number (updated for each public release) printed by the
141 * NCR5380_print_options command, which should be called from the
142 * wrapper detect function, so that I know what release of the driver
145 * Issues specific to the NCR5380 :
147 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
148 * piece of hardware that requires you to sit in a loop polling for
149 * the REQ signal as long as you are connected. Some devices are
150 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
151 * while doing long seek operations.
153 * The workaround for this is to keep track of devices that have
154 * disconnected. If the device hasn't disconnected, for commands that
155 * should disconnect, we do something like
157 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
159 * Some tweaking of N and M needs to be done. An algorithm based
160 * on "time to data" would give the best results as long as short time
161 * to datas (ie, on the same track) were considered, however these
162 * broken devices are the exception rather than the rule and I'd rather
163 * spend my time optimizing for the normal case.
167 * At the heart of the design is a coroutine, NCR5380_main,
168 * which is started when not running by the interrupt handler,
169 * timer, and queue command function. It attempts to establish
170 * I_T_L or I_T_L_Q nexuses by removing the commands from the
171 * issue queue and calling NCR5380_select() if a nexus
172 * is not established.
174 * Once a nexus is established, the NCR5380_information_transfer()
175 * phase goes through the various phases as instructed by the target.
176 * if the target goes into MSG IN and sends a DISCONNECT message,
177 * the command structure is placed into the per instance disconnected
178 * queue, and NCR5380_main tries to find more work. If USLEEP
179 * was defined, and the target is idle for too long, the system
182 * If a command has disconnected, eventually an interrupt will trigger,
183 * calling NCR5380_intr() which will in turn call NCR5380_reselect
184 * to reestablish a nexus. This will run main if necessary.
186 * On command termination, the done function will be called as
189 * SCSI pointers are maintained in the SCp field of SCSI command
190 * structures, being initialized after the command is connected
191 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
192 * Note that in violation of the standard, an implicit SAVE POINTERS operation
193 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
198 * This file a skeleton Linux SCSI driver for the NCR 5380 series
199 * of chips. To use it, you write an architecture specific functions
200 * and macros and include this file in your driver.
202 * These macros control options :
203 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
206 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
207 * for commands that return with a CHECK CONDITION status.
209 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
212 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
213 * override-configure an IRQ.
215 * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
216 * bytes at a time. Since interrupts are disabled by default during
217 * these transfers, we might need this to give reasonable interrupt
218 * service time if the transfer size gets too large.
220 * LINKED - if defined, linked commands are supported.
222 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
224 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
226 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
227 * rely on phase mismatch and EOP interrupts to determine end
230 * SCSI2 - if defined, SCSI-2 tagged queuing is used where possible
232 * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
233 * only really want to use this if you're having a problem with
234 * dropped characters during high speed communications, and even
235 * then, you're going to be better off twiddling with transfersize
236 * in the high level code.
238 * USLEEP - if defined, on devices that aren't disconnecting from the
239 * bus, we will go to sleep so that the CPU can get real work done
240 * when we run a command that won't complete immediately.
242 * Note that if USLEEP is defined, NCR5380_TIMER *must* also be
245 * Defaults for these will be provided if USLEEP is defined, although
246 * the user may want to adjust these to allocate CPU resources to
247 * the SCSI driver or "real" code.
249 * USLEEP_SLEEP - amount of time, in jiffies, to sleep
251 * USLEEP_POLL - amount of time, in jiffies, to poll
253 * These macros MUST be defined :
254 * NCR5380_local_declare() - declare any local variables needed for your
257 * NCR5380_setup(instance) - initialize any local variables needed from a given
258 * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
260 * NCR5380_read(register) - read from the specified register
262 * NCR5380_write(register, value) - write to the specific register
264 * NCR5380_implementation_fields - additional fields needed for this
265 * specific implementation of the NCR5380
267 * Either real DMA *or* pseudo DMA may be implemented
269 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
270 * Note that the DMA setup functions should return the number of bytes
271 * that they were able to program the controller for.
273 * Also note that generic i386/PC versions of these macros are
274 * available as NCR5380_i386_dma_write_setup,
275 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
277 * NCR5380_dma_write_setup(instance, src, count) - initialize
278 * NCR5380_dma_read_setup(instance, dst, count) - initialize
279 * NCR5380_dma_residual(instance); - residual count
282 * NCR5380_pwrite(instance, src, count)
283 * NCR5380_pread(instance, dst, count);
285 * If nothing specific to this implementation needs doing (ie, with external
286 * hardware), you must also define
288 * NCR5380_queue_command
293 * to be the global entry points into the specific driver, ie
294 * #define NCR5380_queue_command t128_queue_command.
296 * If this is not done, the routines will be defined as static functions
297 * with the NCR5380* names and the user must provide a globally
298 * accessible wrapper function.
300 * The generic driver is initialized by calling NCR5380_init(instance),
301 * after setting the appropriate host specific fields and ID. If the
302 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
303 * possible) function may be used. Before the specific driver initialization
304 * code finishes, NCR5380_print_options should be called.
307 static int do_abort (struct Scsi_Host
*host
);
308 static void do_reset (struct Scsi_Host
*host
);
309 static struct Scsi_Host
*first_instance
= NULL
;
310 static Scsi_Host_Template
*the_template
= NULL
;
313 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
315 * Purpose : initialize the saved data pointers for cmd to point to the
316 * start of the buffer.
318 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
321 static __inline__
void initialize_SCp(Scsi_Cmnd
*cmd
) {
323 * Initialize the Scsi Pointer field so that all of the commands in the
324 * various queues are valid.
328 cmd
->SCp
.buffer
= (struct scatterlist
*) cmd
->buffer
;
329 cmd
->SCp
.buffers_residual
= cmd
->use_sg
- 1;
330 cmd
->SCp
.ptr
= (char *) cmd
->SCp
.buffer
->address
;
331 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
333 cmd
->SCp
.buffer
= NULL
;
334 cmd
->SCp
.buffers_residual
= 0;
335 cmd
->SCp
.ptr
= (char *) cmd
->request_buffer
;
336 cmd
->SCp
.this_residual
= cmd
->request_bufflen
;
340 #include <linux/delay.h>
346 signals
[] = {{ SR_DBP
, "PARITY"}, { SR_RST
, "RST" }, { SR_BSY
, "BSY" },
347 { SR_REQ
, "REQ" }, { SR_MSG
, "MSG" }, { SR_CD
, "CD" }, { SR_IO
, "IO" },
348 { SR_SEL
, "SEL" }, {0, NULL
}},
349 basrs
[] = {{BASR_ATN
, "ATN"}, {BASR_ACK
, "ACK"}, {0, NULL
}},
350 icrs
[] = {{ICR_ASSERT_RST
, "ASSERT RST"},{ICR_ASSERT_ACK
, "ASSERT ACK"},
351 {ICR_ASSERT_BSY
, "ASSERT BSY"}, {ICR_ASSERT_SEL
, "ASSERT SEL"},
352 {ICR_ASSERT_ATN
, "ASSERT ATN"}, {ICR_ASSERT_DATA
, "ASSERT DATA"},
354 mrs
[] = {{MR_BLOCK_DMA_MODE
, "MODE BLOCK DMA"}, {MR_TARGET
, "MODE TARGET"},
355 {MR_ENABLE_PAR_CHECK
, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR
,
356 "MODE PARITY INTR"}, {MR_MONITOR_BSY
, "MODE MONITOR BSY"},
357 {MR_DMA_MODE
, "MODE DMA"}, {MR_ARBITRATE
, "MODE ARBITRATION"},
361 * Function : void NCR5380_print(struct Scsi_Host *instance)
363 * Purpose : print the SCSI bus signals for debugging purposes
365 * Input : instance - which NCR5380
368 static void NCR5380_print(struct Scsi_Host
*instance
) {
369 NCR5380_local_declare();
370 unsigned char status
, data
, basr
, mr
, icr
, i
;
371 NCR5380_setup(instance
);
373 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
374 status
= NCR5380_read(STATUS_REG
);
375 mr
= NCR5380_read(MODE_REG
);
376 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
377 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
379 printk("STATUS_REG: %02x ", status
);
380 for (i
= 0; signals
[i
].mask
; ++i
)
381 if (status
& signals
[i
].mask
)
382 printk(",%s", signals
[i
].name
);
383 printk("\nBASR: %02x ", basr
);
384 for (i
= 0; basrs
[i
].mask
; ++i
)
385 if (basr
& basrs
[i
].mask
)
386 printk(",%s", basrs
[i
].name
);
387 printk("\nICR: %02x ", icr
);
388 for (i
= 0; icrs
[i
].mask
; ++i
)
389 if (icr
& icrs
[i
].mask
)
390 printk(",%s", icrs
[i
].name
);
391 printk("\nMODE: %02x ", mr
);
392 for (i
= 0; mrs
[i
].mask
; ++i
)
393 if (mr
& mrs
[i
].mask
)
394 printk(",%s", mrs
[i
].name
);
402 {PHASE_DATAOUT
, "DATAOUT"}, {PHASE_DATAIN
, "DATAIN"}, {PHASE_CMDOUT
, "CMDOUT"},
403 {PHASE_STATIN
, "STATIN"}, {PHASE_MSGOUT
, "MSGOUT"}, {PHASE_MSGIN
, "MSGIN"},
404 {PHASE_UNKNOWN
, "UNKNOWN"}};
407 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
409 * Purpose : print the current SCSI phase for debugging purposes
411 * Input : instance - which NCR5380
414 static void NCR5380_print_phase(struct Scsi_Host
*instance
) {
415 NCR5380_local_declare();
416 unsigned char status
;
418 NCR5380_setup(instance
);
420 status
= NCR5380_read(STATUS_REG
);
421 if (!(status
& SR_REQ
))
422 printk("scsi%d : REQ not asserted, phase unknown.\n",
425 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
426 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
);
427 printk("scsi%d : phase %s\n", instance
->host_no
, phases
[i
].name
);
433 * We need to have our coroutine active given these constraints :
434 * 1. The mutex flag, main_running, can only be set when the main
435 * routine can actually process data, otherwise SCSI commands
436 * will never get issued.
438 * 2. NCR5380_main() shouldn't be called before it has exited, because
439 * other drivers have had kernel stack overflows in similar
442 * 3. We don't want to inline NCR5380_main() because of space concerns,
443 * even though it is only called in two places.
445 * So, the solution is to set the mutex in an inline wrapper for the
446 * main coroutine, and have the main coroutine exit with interrupts
447 * disabled after the final search through the queues so that no race
448 * conditions are possible.
451 static volatile int main_running
= 0;
454 * Function : run_main(void)
456 * Purpose : insure that the coroutine is running and will process our
457 * request. main_running is checked/set here (in an inline function)
458 * rather than in NCR5380_main itself to reduce the chances of stack
463 static __inline__
void run_main(void) {
469 * main_running is cleared in NCR5380_main once it can't do
470 * more work, and NCR5380_main exits with interrupts disabled.
478 #ifndef NCR5380_TIMER
479 #error "NCR5380_TIMER must be defined so that this type of NCR5380 driver gets a unique timer."
483 * These need tweaking, and would probably work best as per-device
484 * flags initialized differently for disk, tape, cd, etc devices.
485 * People with broken devices are free to experiment as to what gives
486 * the best results for them.
488 * USLEEP_SLEEP should be a minimum seek time.
490 * USLEEP_POLL should be a maximum rotational latency.
493 /* 20 ms (reasonable hard disk speed) */
494 #define USLEEP_SLEEP 2
496 /* 300 RPM (floppy speed) */
498 #define USLEEP_POLL 20
501 static struct Scsi_Host
* expires_first
= NULL
;
504 * Function : int should_disconnect (unsigned char cmd)
506 * Purpose : decide weather a command would normally disconnect or
507 * not, since if it won't disconnect we should go to sleep.
509 * Input : cmd - opcode of SCSI command
511 * Returns : DISCONNECT_LONG if we should disconnect for a really long
512 * time (ie always, sleep, look for REQ active, sleep),
513 * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
514 * time-to-data delay, DISCONNECT_NONE if this command would return
517 * Future sleep algorithms based on time to data can exploit
518 * something like this so they can differentiate between "normal"
519 * (ie, read, write, seek) and unusual commands (ie, * format).
521 * Note : We don't deal with commands that handle an immediate disconnect,
525 static int should_disconnect (unsigned char cmd
) {
533 return DISCONNECT_TIME_TO_DATA
;
538 return DISCONNECT_LONG
;
540 return DISCONNECT_NONE
;
545 * Assumes instance->time_expires has been set in higher level code.
548 static int NCR5380_set_timer (struct Scsi_Host
*instance
) {
549 struct Scsi_Host
*tmp
, **prev
;
552 if (((struct NCR5380_hostdata
*) (instance
->host_data
))->next_timer
) {
557 for (prev
= &expires_first
, tmp
= expires_first
; tmp
;
558 prev
= &(((struct NCR5380_hostdata
*) tmp
->host_data
)->next_timer
),
559 tmp
= ((struct NCR5380_hostdata
*) tmp
->host_data
)->next_timer
)
560 if (instance
->time_expires
< tmp
->time_expires
)
563 instance
->next_timer
= tmp
;
565 timer_table
[NCR5380_TIMER
].expires
= expires_first
->time_expires
;
566 timer_active
|= 1 << NCR5380_TIMER
;
571 /* Doing something about unwanted reentrancy here might be useful */
572 void NCR5380_timer_fn(void) {
573 struct Scsi_Host
*instance
;
575 for (; expires_first
&& expires_first
->time_expires
>= jiffies
; ) {
576 instance
= ((NCR5380_hostdata
*) expires_first
->host_data
)->
578 ((NCR5380_hostdata
*) expires_first
->host_data
)->expires_next
=
580 ((NCR5380_hostdata
*) expires_first
->host_data
)->time_expires
=
582 expires_first
= instance
;
586 timer_table
[NCR5380_TIMER
].expires
= ((NCR5380_hostdata
*)
587 expires_first
->host_data
)->time_expires
;
588 timer_active
|= (1 << NCR5380_TIMER
);
590 timer_table
[NCR5380_TIMER
].expires
= 0;
591 timer_active
&= ~(1 << MCR5380_TIMER
);
597 #endif /* def USLEEP */
599 static inline void NCR5380_all_init (void) {
602 #if (NDEBUG & NDEBUG_INIT)
603 printk("scsi : NCR5380_all_init()\n");
607 timer_table
[NCR5380_TIMER
].expires
= 0;
608 timer_table
[NCR5380_TIMER
].fn
= NCR5380_timer_fn
;
615 * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
617 * Purpose : autoprobe for the IRQ line used by the NCR5380.
619 * Inputs : instance - pointer to this instance of the NCR5380 driver,
620 * possible - bitmask of permissible interrupts.
622 * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired.
624 * XXX no effort is made to deal with spurious interrupts.
628 static int probe_irq __initdata
;
629 __initfunc(static void probe_intr (int irq
, void *dev_id
, struct pt_regs
* regs
)) {
633 __initfunc(static int NCR5380_probe_irq (struct Scsi_Host
*instance
, int possible
)) {
634 NCR5380_local_declare();
635 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
637 unsigned long timeout
;
638 int trying_irqs
, i
, mask
;
639 NCR5380_setup(instance
);
641 for (trying_irqs
= i
= 0, mask
= 1; i
< 16; ++i
, mask
<<= 1)
642 if ((mask
& possible
) && (request_irq(i
, &probe_intr
, SA_INTERRUPT
, "NCR-probe", NULL
)
646 timeout
= jiffies
+ 25;
647 probe_irq
= IRQ_NONE
;
650 * A interrupt is triggered whenever BSY = false, SEL = true
651 * and a bit set in the SELECT_ENABLE_REG is asserted on the
654 * Note that the bus is only driven when the phase control signals
655 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
659 NCR5380_write(TARGET_COMMAND_REG
, 0);
660 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
661 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
662 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
|
665 while (probe_irq
== IRQ_NONE
&& jiffies
< timeout
)
668 NCR5380_write(SELECT_ENABLE_REG
, 0);
669 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
671 for (i
= 0, mask
= 1; i
< 16; ++i
, mask
<<= 1)
672 if (trying_irqs
& mask
)
677 #endif /* AUTOPROBE_IRQ */
680 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
682 * Purpose : called by probe code indicating the NCR5380 driver
683 * options that were selected.
685 * Inputs : instance, pointer to this instance. Unused.
688 __initfunc(static void NCR5380_print_options (struct Scsi_Host
*instance
)) {
689 printk(" generic options"
719 printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL
, USLEEP_SLEEP
);
721 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE
);
722 if (((struct NCR5380_hostdata
*)instance
->hostdata
)->flags
& FLAG_NCR53C400
) {
723 printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE
);
728 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
730 * Purpose : print commands in the various queues, called from
731 * NCR5380_abort and NCR5380_debug to aid debugging.
733 * Inputs : instance, pointer to this instance.
736 static void NCR5380_print_status (struct Scsi_Host
*instance
) {
737 static char pr_bfr
[512];
741 printk("NCR5380 : coroutine is%s running.\n",
742 main_running
? "" : "n't");
745 NCR5380_print (instance
);
746 NCR5380_print_phase (instance
);
749 len
= NCR5380_proc_info(pr_bfr
, &start
, 0, sizeof(pr_bfr
),
750 instance
->host_no
, 0);
752 printk("\n%s\n", pr_bfr
);
755 /******************************************/
757 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
759 * *buffer: I/O buffer
760 * **start: if inout == FALSE pointer into buffer where user read should start
761 * offset: current offset
762 * length: length of buffer
763 * hostno: Scsi_Host host_no
764 * inout: TRUE - user is writing; FALSE - user is reading
766 * Return the number of bytes read from or written
770 #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
772 char *lprint_Scsi_Cmnd (Scsi_Cmnd
*cmd
, char *pos
, char *buffer
, int length
);
774 char *lprint_command (unsigned char *cmd
, char *pos
, char *buffer
, int len
);
776 char *lprint_opcode(int opcode
, char *pos
, char *buffer
, int length
);
778 #ifndef NCR5380_proc_info
781 int NCR5380_proc_info (
782 char *buffer
, char **start
,off_t offset
,
783 int length
,int hostno
,int inout
)
786 struct Scsi_Host
*instance
;
787 struct NCR5380_hostdata
*hostdata
;
790 for (instance
= first_instance
; instance
&&
791 instance
->host_no
!= hostno
; instance
=instance
->next
)
795 hostdata
= (struct NCR5380_hostdata
*)instance
->hostdata
;
797 if (inout
) { /* Has data been written to the file ? */
798 #ifdef DTC_PUBLIC_RELEASE
799 dtc_wmaxi
= dtc_maxi
= 0;
801 #ifdef PAS16_PUBLIC_RELEASE
802 pas_wmaxi
= pas_maxi
= 0;
804 return(-ENOSYS
); /* Currently this is a no-op */
806 SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE
);
807 if (((struct NCR5380_hostdata
*)instance
->hostdata
)->flags
& FLAG_NCR53C400
)
808 SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE
);
809 #ifdef DTC_PUBLIC_RELEASE
810 SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE
);
812 #ifdef T128_PUBLIC_RELEASE
813 SPRINTF("T128 release %d", T128_PUBLIC_RELEASE
);
815 #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
816 SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE
);
818 #ifdef PAS16_PUBLIC_RELEASE
819 SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE
);
822 SPRINTF("\nBase Addr: 0x%05X ", (int)instance
->base
);
823 SPRINTF("io_port: %04x ", (int)instance
->io_port
);
824 if (instance
->irq
== IRQ_NONE
)
825 SPRINTF("IRQ: None.\n");
827 SPRINTF("IRQ: %d.\n", instance
->irq
);
829 #ifdef DTC_PUBLIC_RELEASE
830 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
831 dtc_wmaxi
, dtc_maxi
);
833 #ifdef PAS16_PUBLIC_RELEASE
834 SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n",
835 pas_wmaxi
, pas_maxi
);
838 SPRINTF("NCR5380 : coroutine is%s running.\n", main_running
? "" : "n't");
839 if (!hostdata
->connected
)
840 SPRINTF("scsi%d: no currently connected command\n", instance
->host_no
);
842 pos
= lprint_Scsi_Cmnd ((Scsi_Cmnd
*) hostdata
->connected
,
843 pos
, buffer
, length
);
844 SPRINTF("scsi%d: issue_queue\n", instance
->host_no
);
845 for (ptr
= (Scsi_Cmnd
*) hostdata
->issue_queue
; ptr
;
846 ptr
= (Scsi_Cmnd
*) ptr
->host_scribble
)
847 pos
= lprint_Scsi_Cmnd (ptr
, pos
, buffer
, length
);
849 SPRINTF("scsi%d: disconnected_queue\n", instance
->host_no
);
850 for (ptr
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
; ptr
;
851 ptr
= (Scsi_Cmnd
*) ptr
->host_scribble
)
852 pos
= lprint_Scsi_Cmnd (ptr
, pos
, buffer
, length
);
856 if (pos
- buffer
< offset
)
858 else if (pos
- buffer
- offset
< length
)
859 return pos
- buffer
- offset
;
864 char *lprint_Scsi_Cmnd (Scsi_Cmnd
*cmd
, char *pos
, char *buffer
, int length
) {
865 SPRINTF("scsi%d : destination target %d, lun %d\n",
866 cmd
->host
->host_no
, cmd
->target
, cmd
->lun
);
867 SPRINTF(" command = ");
868 pos
= lprint_command (cmd
->cmnd
, pos
, buffer
, length
);
873 char *lprint_command (unsigned char *command
,
874 char *pos
, char *buffer
, int length
) {
876 pos
= lprint_opcode(command
[0], pos
, buffer
, length
);
877 for ( i
= 1, s
= COMMAND_SIZE(command
[0]); i
< s
; ++i
)
878 SPRINTF("%02x ", command
[i
]);
884 char *lprint_opcode(int opcode
, char *pos
, char *buffer
, int length
) {
885 SPRINTF("%2d (0x%02x)", opcode
, opcode
);
891 * Function : void NCR5380_init (struct Scsi_Host *instance, flags)
893 * Purpose : initializes *instance and corresponding 5380 chip,
894 * with flags OR'd into the initial flags value.
896 * Inputs : instance - instantiation of the 5380 driver.
898 * Notes : I assume that the host, hostno, and id bits have been
899 * set correctly. I don't care about the irq and other fields.
903 __initfunc(static void NCR5380_init (struct Scsi_Host
*instance
, int flags
)) {
904 NCR5380_local_declare();
906 unsigned long timeout
;
907 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
911 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
916 if (flags
& FLAG_NCR53C400
)
917 instance
->NCR5380_instance_name
+= NCR53C400_address_adjust
;
920 NCR5380_setup(instance
);
924 hostdata
->aborted
= 0;
925 hostdata
->id_mask
= 1 << instance
->this_id
;
926 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
927 if (i
> hostdata
->id_mask
)
928 hostdata
->id_higher_mask
|= i
;
929 for (i
= 0; i
< 8; ++i
)
930 hostdata
->busy
[i
] = 0;
932 hostdata
->dmalen
= 0;
934 hostdata
->targets_present
= 0;
935 hostdata
->connected
= NULL
;
936 hostdata
->issue_queue
= NULL
;
937 hostdata
->disconnected_queue
= NULL
;
939 for (i
= 0; i
< 8; ++i
) {
940 hostdata
->time_read
[i
] = 0;
941 hostdata
->time_write
[i
] = 0;
942 hostdata
->bytes_read
[i
] = 0;
943 hostdata
->bytes_write
[i
] = 0;
945 hostdata
->timebase
= 0;
946 hostdata
->pendingw
= 0;
947 hostdata
->pendingr
= 0;
950 /* The CHECK code seems to break the 53C400. Will check it later maybe */
951 if (flags
& FLAG_NCR53C400
)
952 hostdata
->flags
= FLAG_HAS_LAST_BYTE_SENT
| flags
;
954 hostdata
->flags
= FLAG_CHECK_LAST_BYTE_SENT
| flags
;
957 the_template
= instance
->hostt
;
958 first_instance
= instance
;
963 hostdata
->time_expires
= 0;
964 hostdata
->next_timer
= NULL
;
968 if ((instance
->cmd_per_lun
> 1) || instance
->can_queue
> 1))
969 printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
970 " without AUTOSENSE option, contingent allegiance conditions may\n"
971 " be incorrectly cleared.\n", instance
->host_no
);
972 #endif /* def AUTOSENSE */
974 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
975 NCR5380_write(MODE_REG
, MR_BASE
);
976 NCR5380_write(TARGET_COMMAND_REG
, 0);
977 NCR5380_write(SELECT_ENABLE_REG
, 0);
980 if (hostdata
->flags
& FLAG_NCR53C400
) {
981 NCR5380_write(C400_CONTROL_STATUS_REG
, CSR_BASE
);
986 * Detect and correct bus wedge problems.
988 * If the system crashed, it may have crashed in a state
989 * where a SCSI command was still executing, and the
990 * SCSI bus is not in a BUS FREE STATE.
992 * If this is the case, we'll try to abort the currently
993 * established nexus which we know nothing about, and that
994 * failing, do a hard reset of the SCSI bus
997 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) &&
998 pass
<= 6 ; ++pass
) {
1003 printk("scsi%d: SCSI bus busy, waiting up to five seconds\n",
1005 timeout
= jiffies
+ 500;
1006 while (jiffies
< timeout
&& (NCR5380_read(STATUS_REG
) & SR_BSY
));
1009 printk("scsi%d: bus busy, attempting abort\n",
1011 do_abort (instance
);
1014 printk("scsi%d: bus busy, attempting reset\n",
1016 do_reset (instance
);
1019 printk("scsi%d: bus locked solid or invalid override\n",
1026 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
1027 * void (*done)(Scsi_Cmnd *))
1029 * Purpose : enqueues a SCSI command
1031 * Inputs : cmd - SCSI command, done - function called on completion, with
1032 * a pointer to the command descriptor.
1037 * cmd is added to the per instance issue_queue, with minor
1038 * twiddling done to the host specific fields of cmd. If the
1039 * main coroutine is not running, it is restarted.
1043 /* Only make static if a wrapper function is used */
1044 #ifndef NCR5380_queue_command
1047 int NCR5380_queue_command (Scsi_Cmnd
*cmd
, void (*done
)(Scsi_Cmnd
*)) {
1048 struct Scsi_Host
*instance
= cmd
->host
;
1049 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
1053 #if (NDEBUG & NDEBUG_NO_WRITE)
1054 switch (cmd
->cmnd
[0]) {
1057 printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
1059 cmd
->result
= (DID_ERROR
<< 16);
1063 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1065 #ifdef NCR5380_STATS
1067 if (!hostdata
->connected
&& !hostdata
->issue_queue
&&
1068 !hostdata
->disconnected_queue
) {
1069 hostdata
->timebase
= jiffies
;
1072 # ifdef NCR5380_STAT_LIMIT
1073 if (cmd
->request_bufflen
> NCR5380_STAT_LIMIT
)
1075 switch (cmd
->cmnd
[0])
1080 hostdata
->time_write
[cmd
->target
] -= (jiffies
- hostdata
->timebase
);
1081 hostdata
->bytes_write
[cmd
->target
] += cmd
->request_bufflen
;
1082 hostdata
->pendingw
++;
1087 hostdata
->time_read
[cmd
->target
] -= (jiffies
- hostdata
->timebase
);
1088 hostdata
->bytes_read
[cmd
->target
] += cmd
->request_bufflen
;
1089 hostdata
->pendingr
++;
1095 * We use the host_scribble field as a pointer to the next command
1099 cmd
->host_scribble
= NULL
;
1100 cmd
->scsi_done
= done
;
1106 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1107 * commands are added to the head of the queue since any command will
1108 * clear the contingent allegiance condition that exists and the
1109 * sense data is only guaranteed to be valid while the condition exists.
1113 if (!(hostdata
->issue_queue
) || (cmd
->cmnd
[0] == REQUEST_SENSE
)) {
1114 LIST(cmd
, hostdata
->issue_queue
);
1115 cmd
->host_scribble
= (unsigned char *) hostdata
->issue_queue
;
1116 hostdata
->issue_queue
= cmd
;
1118 for (tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
; tmp
->host_scribble
;
1119 tmp
= (Scsi_Cmnd
*) tmp
->host_scribble
);
1121 tmp
->host_scribble
= (unsigned char *) cmd
;
1123 #if (NDEBUG & NDEBUG_QUEUES)
1124 printk("scsi%d : command added to %s of queue\n", instance
->host_no
,
1125 (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
1128 /* Run the coroutine if it isn't already running. */
1134 * Function : NCR5380_main (void)
1136 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1137 * be done on the NCR5380 host adapters in a system. Both
1138 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1139 * in case it is not running.
1141 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1142 * reenable them. This prevents reentrancy and kernel stack overflow.
1145 static void NCR5380_main (void) {
1146 Scsi_Cmnd
*tmp
, *prev
;
1147 struct Scsi_Host
*instance
;
1148 struct NCR5380_hostdata
*hostdata
;
1152 * We run (with interrupts disabled) until we're sure that none of
1153 * the host adapters have anything that can be done, at which point
1154 * we set main_running to 0 and exit.
1156 * Interrupts are enabled before doing various other internal
1157 * instructions, after we've decided that we need to run through
1160 * this should prevent any race conditions.
1164 cli(); /* Freeze request queues */
1166 for (instance
= first_instance
; instance
&&
1167 instance
->hostt
== the_template
; instance
=instance
->next
) {
1168 hostdata
= (struct NCR5380_hostdata
*) instance
->hostdata
;
1170 if (!hostdata
->connected
) {
1171 #if (NDEBUG & NDEBUG_MAIN)
1172 printk("scsi%d : not connected\n", instance
->host_no
);
1175 * Search through the issue_queue for a command destined
1176 * for a target that's not busy.
1178 #if (NDEBUG & NDEBUG_LISTS)
1179 for (tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
, prev
=NULL
; tmp
&& (tmp
!= prev
); prev
=tmp
, tmp
=(Scsi_Cmnd
*)tmp
->host_scribble
)
1181 /*printk("%p ", tmp);*/
1182 if ((tmp
== prev
) && tmp
) printk(" LOOP\n");/* else printk("\n");*/
1184 for (tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
,
1185 prev
= NULL
; tmp
; prev
= tmp
, tmp
= (Scsi_Cmnd
*)
1186 tmp
->host_scribble
) {
1188 #if (NDEBUG & NDEBUG_LISTS)
1190 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp
, tmp
->target
, hostdata
->busy
[tmp
->target
], tmp
->lun
);
1192 /* When we find one, remove it from the issue queue. */
1193 if (!(hostdata
->busy
[tmp
->target
] & (1 << tmp
->lun
))) {
1195 REMOVE(prev
,prev
->host_scribble
,tmp
,tmp
->host_scribble
);
1196 prev
->host_scribble
= tmp
->host_scribble
;
1198 REMOVE(-1,hostdata
->issue_queue
,tmp
,tmp
->host_scribble
);
1199 hostdata
->issue_queue
= (Scsi_Cmnd
*) tmp
->host_scribble
;
1201 tmp
->host_scribble
= NULL
;
1203 /* reenable interrupts after finding one */
1207 * Attempt to establish an I_T_L nexus here.
1208 * On success, instance->hostdata->connected is set.
1209 * On failure, we must add the command back to the
1210 * issue queue so we can keep trying.
1212 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1213 printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
1214 instance
->host_no
, tmp
->target
, tmp
->lun
);
1218 * A successful selection is defined as one that
1219 * leaves us with the command connected and
1220 * in hostdata->connected, OR has terminated the
1223 * With successful commands, we fall through
1224 * and see if we can do an information transfer,
1225 * with failures we will restart.
1228 if (!NCR5380_select(instance
, tmp
,
1230 * REQUEST SENSE commands are issued without tagged
1231 * queueing, even on SCSI-II devices because the
1232 * contingent allegiance condition exists for the
1235 (tmp
->cmnd
[0] == REQUEST_SENSE
) ? TAG_NONE
:
1240 LIST(tmp
, hostdata
->issue_queue
);
1241 tmp
->host_scribble
= (unsigned char *)
1242 hostdata
->issue_queue
;
1243 hostdata
->issue_queue
= tmp
;
1246 #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
1247 printk("scsi%d : main(): select() failed, returned to issue_queue\n",
1251 } /* if target/lun is not busy */
1253 } /* if (!hostdata->connected) */
1255 if (hostdata
->connected
1257 && !hostdata
->dmalen
1260 && (!hostdata
->time_expires
|| hostdata
->time_expires
>= jiffies
)
1264 #if (NDEBUG & NDEBUG_MAIN)
1265 printk("scsi%d : main() : performing information transfer\n",
1268 NCR5380_information_transfer(instance
);
1269 #if (NDEBUG & NDEBUG_MAIN)
1270 printk("scsi%d : main() : done set false\n", instance
->host_no
);
1275 } /* for instance */
1280 #ifndef DONT_USE_INTR
1282 * Function : void NCR5380_intr (int irq)
1284 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1285 * from the disconnected queue, and restarting NCR5380_main()
1288 * Inputs : int irq, irq that caused this interrupt.
1292 static void NCR5380_intr (int irq
, void *dev_id
, struct pt_regs
* regs
) {
1293 NCR5380_local_declare();
1294 struct Scsi_Host
*instance
;
1297 #if (NDEBUG & NDEBUG_INTR)
1298 printk("scsi : NCR5380 irq %d triggered\n", irq
);
1302 for (instance
= first_instance
; instance
&& (instance
->hostt
==
1303 the_template
); instance
= instance
->next
)
1304 if (instance
->irq
== irq
) {
1306 /* Look for pending interrupts */
1307 NCR5380_setup(instance
);
1308 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
1309 /* XXX dispatch to appropriate routine if found and done=0 */
1310 if (basr
& BASR_IRQ
) {
1311 #if (NDEBUG & NDEBUG_INTR)
1312 NCR5380_print(instance
);
1314 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) ==
1318 #if (NDEBUG & NDEBUG_INTR)
1319 printk("scsi%d : SEL interrupt\n", instance
->host_no
);
1321 NCR5380_reselect(instance
);
1322 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1323 } else if (basr
& BASR_PARITY_ERROR
) {
1324 #if (NDEBUG & NDEBUG_INTR)
1325 printk("scsi%d : PARITY interrupt\n", instance
->host_no
);
1327 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1328 } else if ((NCR5380_read(STATUS_REG
) & SR_RST
) == SR_RST
) {
1329 #if (NDEBUG & NDEBUG_INTR)
1330 printk("scsi%d : RESET interrupt\n", instance
->host_no
);
1332 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1335 * XXX the rest of the interrupt conditions should *only* occur during a
1336 * DMA transfer, which I haven't gotten around to fixing yet.
1339 #if defined(REAL_DMA)
1341 * We should only get PHASE MISMATCH and EOP interrupts
1342 * if we have DMA enabled, so do a sanity check based on
1343 * the current setting of the MODE register.
1346 if ((NCR5380_read(MODE_REG
) & MR_DMA
) && ((basr
&
1347 BASR_END_DMA_TRANSFER
) ||
1348 !(basr
& BASR_PHASE_MATCH
))) {
1351 if (!hostdata
->connected
)
1352 panic("scsi%d : received end of DMA interrupt with no connected cmd\n",
1355 transfered
= (hostdata
->dmalen
- NCR5380_dma_residual(instance
));
1356 hostdata
->connected
->SCp
.this_residual
-= transferred
;
1357 hostdata
->connected
->SCp
.ptr
+= transferred
;
1358 hostdata
->dmalen
= 0;
1360 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1363 unsigned long timeout
= jiffies
+ NCR_TIMEOUT
;
1365 while (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_ACK
1366 && jiffies
< timeout
)
1368 if (jiffies
>= timeout
)
1369 printk("scsi%d: timeout at NCR5380.c:%d\n",
1370 host
->host_no
, __LINE__
);
1372 #else /* NCR_TIMEOUT */
1373 while (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_ACK
);
1376 NCR5380_write(MODE_REG
, MR_BASE
);
1377 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1380 #if (NDEBUG & NDEBUG_INTR)
1381 printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr
, NCR5380_read(MODE_REG
), NCR5380_read(STATUS_REG
));
1383 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1389 } /* if (instance->irq == irq) */
1394 #ifdef NCR5380_STATS
1395 static void collect_stats(struct NCR5380_hostdata
* hostdata
, Scsi_Cmnd
* cmd
)
1397 # ifdef NCR5380_STAT_LIMIT
1398 if (cmd
->request_bufflen
> NCR5380_STAT_LIMIT
)
1400 switch (cmd
->cmnd
[0])
1405 hostdata
->time_write
[cmd
->target
] += (jiffies
- hostdata
->timebase
);
1406 /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen;*/
1407 hostdata
->pendingw
--;
1412 hostdata
->time_read
[cmd
->target
] += (jiffies
- hostdata
->timebase
);
1413 /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen;*/
1414 hostdata
->pendingr
--;
1421 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1424 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1425 * including ARBITRATION, SELECTION, and initial message out for
1426 * IDENTIFY and queue messages.
1428 * Inputs : instance - instantiation of the 5380 driver on which this
1429 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1430 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1431 * the command that is presently connected.
1433 * Returns : -1 if selection could not execute for some reason,
1434 * 0 if selection succeeded or failed because the target
1438 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1439 * with registers as they should have been on entry - ie
1440 * SELECT_ENABLE will be set appropriately, the NCR5380
1441 * will cease to drive any SCSI bus signals.
1443 * If successful : I_T_L or I_T_L_Q nexus will be established,
1444 * instance->connected will be set to cmd.
1445 * SELECT interrupt will be disabled.
1447 * If failed (no target) : cmd->scsi_done() will be called, and the
1448 * cmd->result host byte set to DID_BAD_TARGET.
1451 static int NCR5380_select (struct Scsi_Host
*instance
, Scsi_Cmnd
*cmd
,
1453 NCR5380_local_declare();
1454 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
1456 unsigned char tmp
[3], phase
;
1457 unsigned char *data
;
1459 unsigned long timeout
;
1460 NCR5380_setup(instance
);
1462 hostdata
->restart_select
= 0;
1463 #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION)
1464 NCR5380_print(instance
);
1465 printk("scsi%d : starting arbitration, id = %d\n", instance
->host_no
,
1471 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1472 * data bus during SELECTION.
1475 NCR5380_write(TARGET_COMMAND_REG
, 0);
1479 * Start arbitration.
1482 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
1483 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
1487 /* Wait for arbitration logic to complete */
1490 unsigned long timeout
= jiffies
+ 2*NCR_TIMEOUT
;
1492 while (!(NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
)
1493 && jiffies
< timeout
)
1495 if (jiffies
>= timeout
)
1497 printk("scsi: arbitration timeout at %d\n", __LINE__
);
1498 NCR5380_write(MODE_REG
, MR_BASE
);
1499 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1503 #else /* NCR_TIMEOUT */
1504 while (!(NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
));
1507 #if (NDEBUG & NDEBUG_ARBITRATION)
1508 printk("scsi%d : arbitration complete\n", instance
->host_no
);
1509 /* Avoid GCC 2.4.5 asm needs to many reloads error */
1514 * The arbitration delay is 2.2us, but this is a minimum and there is
1515 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1516 * the integral nature of udelay().
1522 /* Check for lost arbitration */
1523 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1524 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1525 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
)) {
1526 NCR5380_write(MODE_REG
, MR_BASE
);
1527 #if (NDEBUG & NDEBUG_ARBITRATION)
1528 printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n",
1536 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_SEL
);
1538 if (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) {
1539 NCR5380_write(MODE_REG
, MR_BASE
);
1540 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1541 #if (NDEBUG & NDEBUG_ARBITRATION)
1542 printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n",
1549 * Again, bus clear + bus settle time is 1.2us, however, this is
1550 * a minimum so we'll udelay ceil(1.2)
1555 #if (NDEBUG & NDEBUG_ARBITRATION)
1556 printk("scsi%d : won arbitration\n", instance
->host_no
);
1561 * Now that we have won arbitration, start Selection process, asserting
1562 * the host and target ID's on the SCSI bus.
1565 NCR5380_write(OUTPUT_DATA_REG
, (hostdata
->id_mask
| (1 << cmd
->target
)));
1568 * Raise ATN while SEL is true before BSY goes false from arbitration,
1569 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1570 * phase immediately after selection.
1573 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_BSY
|
1574 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1575 NCR5380_write(MODE_REG
, MR_BASE
);
1578 * Reselect interrupts must be turned off prior to the dropping of BSY,
1579 * otherwise we will trigger an interrupt.
1581 NCR5380_write(SELECT_ENABLE_REG
, 0);
1584 * The initiator shall then wait at least two deskew delays and release
1587 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1590 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_DATA
|
1591 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1594 * Something weird happens when we cease to drive BSY - looks
1595 * like the board/chip is letting us do another read before the
1596 * appropriate propagation delay has expired, and we're confusing
1597 * a BSY signal from ourselves as the target's response to SELECTION.
1599 * A small delay (the 'C++' frontend breaks the pipeline with an
1600 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1601 * tighter 'C' code breaks and requires this) solves the problem -
1602 * the 1 us delay is arbitrary, and only used because this delay will
1603 * be the same on other platforms and since it works here, it should
1606 * wingel suggests that this could be due to failing to wait
1612 #if (NDEBUG & NDEBUG_SELECTION)
1613 printk("scsi%d : selecting target %d\n", instance
->host_no
, cmd
->target
);
1617 * The SCSI specification calls for a 250 ms timeout for the actual
1621 timeout
= jiffies
+ 25;
1624 * XXX very interesting - we're seeing a bounce where the BSY we
1625 * asserted is being reflected / still asserted (propagation delay?)
1626 * and it's detecting as true. Sigh.
1629 while ((jiffies
< timeout
) && !(NCR5380_read(STATUS_REG
) &
1632 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) ==
1634 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1635 NCR5380_reselect(instance
);
1636 printk ("scsi%d : reselection after won arbitration?\n",
1638 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1643 * No less than two deskew delays after the initiator detects the
1644 * BSY signal is true, it shall release the SEL signal and may
1645 * change the DATA BUS. -wingel
1650 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1652 if (!(NCR5380_read(STATUS_REG
) & SR_BSY
)) {
1653 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1654 if (hostdata
->targets_present
& (1 << cmd
->target
)) {
1655 printk("scsi%d : weirdness\n", instance
->host_no
);
1656 if (hostdata
->restart_select
)
1657 printk("\trestart select\n");
1659 NCR5380_print (instance
);
1661 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1664 cmd
->result
= DID_BAD_TARGET
<< 16;
1665 #ifdef NCR5380_STATS
1666 collect_stats(hostdata
, cmd
);
1668 cmd
->scsi_done(cmd
);
1669 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1670 #if (NDEBUG & NDEBUG_SELECTION)
1671 printk("scsi%d : target did not respond within 250ms\n",
1674 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1678 hostdata
->targets_present
|= (1 << cmd
->target
);
1681 * Since we followed the SCSI spec, and raised ATN while SEL
1682 * was true but before BSY was false during selection, the information
1683 * transfer phase should be a MESSAGE OUT phase so that we can send the
1686 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1687 * message (2 bytes) with a tag ID that we increment with every command
1688 * until it wraps back to 0.
1690 * XXX - it turns out that there are some broken SCSI-II devices,
1691 * which claim to support tagged queuing but fail when more than
1692 * some number of commands are issued at once.
1695 /* Wait for start of REQ/ACK handshake */
1698 unsigned long timeout
= jiffies
+ NCR_TIMEOUT
;
1700 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
) && jiffies
< timeout
);
1702 if (jiffies
>= timeout
) {
1703 printk("scsi%d: timeout at NCR5380.c:%d\n", __LINE__
);
1704 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1708 #else /* NCR_TIMEOUT */
1709 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
));
1710 #endif /* def NCR_TIMEOUT */
1712 #if (NDEBUG & NDEBUG_SELECTION)
1713 printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
1714 instance
->host_no
, cmd
->target
);
1716 tmp
[0] = IDENTIFY(((instance
->irq
== IRQ_NONE
) ? 0 : 1), cmd
->lun
);
1718 if (cmd
->device
->tagged_queue
&& (tag
!= TAG_NONE
)) {
1719 tmp
[1] = SIMPLE_QUEUE_TAG
;
1720 if (tag
== TAG_NEXT
) {
1721 /* 0 is TAG_NONE, used to imply no tag for this command */
1722 if (cmd
->device
->current_tag
== 0)
1723 cmd
->device
->current_tag
= 1;
1725 cmd
->tag
= cmd
->device
->current_tag
;
1726 cmd
->device
->current_tag
++;
1728 cmd
->tag
= (unsigned char) tag
;
1731 hostdata
->last_message
= SIMPLE_QUEUE_TAG
;
1734 #endif /* def SCSI2 */
1740 /* Send message(s) */
1742 phase
= PHASE_MSGOUT
;
1743 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1744 #if (NDEBUG & NDEBUG_SELECTION)
1745 printk("scsi%d : nexus established.\n", instance
->host_no
);
1747 /* XXX need to handle errors here */
1748 hostdata
->connected
= cmd
;
1750 if (!cmd
->device
->tagged_queue
)
1752 hostdata
->busy
[cmd
->target
] |= (1 << cmd
->lun
);
1754 initialize_SCp(cmd
);
1761 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1762 * unsigned char *phase, int *count, unsigned char **data)
1764 * Purpose : transfers data in given phase using polled I/O
1766 * Inputs : instance - instance of driver, *phase - pointer to
1767 * what phase is expected, *count - pointer to number of
1768 * bytes to transfer, **data - pointer to data pointer.
1770 * Returns : -1 when different phase is entered without transferring
1771 * maximum number of bytes, 0 if all bytes or transfered or exit
1774 * Also, *phase, *count, *data are modified in place.
1776 * XXX Note : handling for bus free may be useful.
1780 * Note : this code is not as quick as it could be, however it
1781 * IS 100% reliable, and for the actual data transfer where speed
1782 * counts, we will always do a pseudo DMA or DMA transfer.
1785 static int NCR5380_transfer_pio (struct Scsi_Host
*instance
,
1786 unsigned char *phase
, int *count
, unsigned char **data
) {
1787 NCR5380_local_declare();
1788 register unsigned char p
= *phase
, tmp
;
1789 register int c
= *count
;
1790 register unsigned char *d
= *data
;
1791 NCR5380_setup(instance
);
1793 #if (NDEBUG & NDEBUG_PIO)
1795 printk("scsi%d : pio write %d bytes\n", instance
->host_no
, c
);
1797 printk("scsi%d : pio read %d bytes\n", instance
->host_no
, c
);
1801 * The NCR5380 chip will only drive the SCSI bus when the
1802 * phase specified in the appropriate bits of the TARGET COMMAND
1803 * REGISTER match the STATUS REGISTER
1806 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1810 * Wait for assertion of REQ, after which the phase bits will be
1813 while (!((tmp
= NCR5380_read(STATUS_REG
)) & SR_REQ
));
1815 #if (NDEBUG & NDEBUG_HANDSHAKE)
1816 printk("scsi%d : REQ detected\n", instance
->host_no
);
1819 /* Check for phase mismatch */
1820 if ((tmp
& PHASE_MASK
) != p
) {
1821 #if (NDEBUG & NDEBUG_PIO)
1822 printk("scsi%d : phase mismatch\n", instance
->host_no
);
1823 NCR5380_print_phase(instance
);
1828 /* Do actual transfer from SCSI bus to / from memory */
1830 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1832 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1837 * The SCSI standard suggests that in MSGOUT phase, the initiator
1838 * should drop ATN on the last byte of the message phase
1839 * after REQ has been asserted for the handshake but before
1840 * the initiator raises ACK.
1844 if (!((p
& SR_MSG
) && c
> 1)) {
1845 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1847 #if (NDEBUG & NDEBUG_PIO)
1848 NCR5380_print(instance
);
1850 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1851 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1853 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1854 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1855 #if (NDEBUG & NDEBUG_PIO)
1856 NCR5380_print(instance
);
1858 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1859 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1862 #if (NDEBUG & NDEBUG_PIO)
1863 NCR5380_print(instance
);
1865 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1868 while (NCR5380_read(STATUS_REG
) & SR_REQ
);
1870 #if (NDEBUG & NDEBUG_HANDSHAKE)
1871 printk("scsi%d : req false, handshake complete\n", instance
->host_no
);
1875 * We have several special cases to consider during REQ/ACK handshaking :
1876 * 1. We were in MSGOUT phase, and we are on the last byte of the
1877 * message. ATN must be dropped as ACK is dropped.
1879 * 2. We are in a MSGIN phase, and we are on the last byte of the
1880 * message. We must exit with ACK asserted, so that the calling
1881 * code may raise ATN before dropping ACK to reject the message.
1883 * 3. ACK and ATN are clear and the target may proceed as normal.
1885 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1886 if (p
== PHASE_MSGOUT
&& c
> 1)
1887 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1889 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1893 #if (NDEBUG & NDEBUG_PIO)
1894 printk("scsi%d : residual %d\n", instance
->host_no
, c
);
1899 tmp
= NCR5380_read(STATUS_REG
);
1901 *phase
= tmp
& PHASE_MASK
;
1903 *phase
= PHASE_UNKNOWN
;
1905 if (!c
|| (*phase
== p
))
1911 static void do_reset (struct Scsi_Host
*host
) {
1912 NCR5380_local_declare();
1913 NCR5380_setup(host
);
1916 NCR5380_write(TARGET_COMMAND_REG
,
1917 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1918 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1920 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1925 * Function : do_abort (Scsi_Host *host)
1927 * Purpose : abort the currently established nexus. Should only be
1928 * called from a routine which can drop into a
1930 * Returns : 0 on success, -1 on failure.
1933 static int do_abort (struct Scsi_Host
*host
) {
1934 NCR5380_local_declare();
1935 unsigned char tmp
, *msgptr
, phase
;
1937 NCR5380_setup(host
);
1940 /* Request message out phase */
1941 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1944 * Wait for the target to indicate a valid phase by asserting
1945 * REQ. Once this happens, we'll have either a MSGOUT phase
1946 * and can immediately send the ABORT message, or we'll have some
1947 * other phase and will have to source/sink data.
1949 * We really don't care what value was on the bus or what value
1950 * the target sees, so we just handshake.
1953 while (!(tmp
= NCR5380_read(STATUS_REG
)) & SR_REQ
);
1955 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1957 if ((tmp
& PHASE_MASK
) != PHASE_MSGOUT
) {
1958 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1960 while (NCR5380_read(STATUS_REG
) & SR_REQ
);
1961 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1967 phase
= PHASE_MSGOUT
;
1968 NCR5380_transfer_pio (host
, &phase
, &len
, &msgptr
);
1971 * If we got here, and the command completed successfully,
1972 * we're about to go into bus free state.
1975 return len
? -1 : 0;
1978 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1980 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1981 * unsigned char *phase, int *count, unsigned char **data)
1983 * Purpose : transfers data in given phase using either real
1986 * Inputs : instance - instance of driver, *phase - pointer to
1987 * what phase is expected, *count - pointer to number of
1988 * bytes to transfer, **data - pointer to data pointer.
1990 * Returns : -1 when different phase is entered without transferring
1991 * maximum number of bytes, 0 if all bytes or transfered or exit
1994 * Also, *phase, *count, *data are modified in place.
1999 static int NCR5380_transfer_dma (struct Scsi_Host
*instance
,
2000 unsigned char *phase
, int *count
, unsigned char **data
) {
2001 NCR5380_local_declare();
2002 register int c
= *count
;
2003 register unsigned char p
= *phase
;
2004 register unsigned char *d
= *data
;
2007 #if defined(REAL_DMA_POLL)
2009 unsigned char saved_data
= 0, overrun
= 0, residue
;
2012 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
2015 NCR5380_setup(instance
);
2017 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
2021 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
2022 #ifdef READ_OVERRUNS
2027 #if (NDEBUG & NDEBUG_DMA)
2028 printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
2029 instance
->host_no
, instance
->dma_channel
, (p
& SR_IO
) ? "reading" :
2030 "writing", c
, (p
& SR_IO
) ? "to" : "from", (unsigned) d
);
2032 hostdata
->dma_len
= (p
& SR_IO
) ?
2033 NCR5380_dma_read_setup(instance
, d
, c
) :
2034 NCR5380_dma_write_setup(instance
, d
, c
);
2037 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
2040 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_ENABLE_EOP_INTR
| MR_MONITOR_BSY
);
2041 #elif defined(REAL_DMA_POLL)
2042 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
);
2045 * Note : on my sample board, watch-dog timeouts occurred when interrupts
2046 * were not disabled for the duration of a single DMA transfer, from
2047 * before the setting of DMA mode to after transfer of the last byte.
2050 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2053 /* KLL May need eop and parity in 53c400 */
2054 if (hostdata
->flags
& FLAG_NCR53C400
)
2055 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_ENABLE_PAR_CHECK
2056 | MR_ENABLE_PAR_INTR
| MR_ENABLE_EOP_INTR
| MR_DMA_MODE
2059 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
);
2060 #endif /* def REAL_DMA */
2062 #if (NDEBUG & NDEBUG_DMA) & 0
2063 printk("scsi%d : mode reg = 0x%X\n", instance
->host_no
, NCR5380_read(MODE_REG
));
2067 * FOO stuff. For some UNAPPARENT reason, I'm getting
2068 * watchdog timers fired on bootup for NO APPARENT REASON, meaning it's
2069 * probably a timing problem.
2071 * Since this is the only place I have back-to-back writes, perhaps this
2079 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
2084 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
2088 NCR5380_write(START_DMA_SEND_REG
, 0);
2094 #if defined(REAL_DMA_POLL)
2096 tmp
= NCR5380_read(BUS_AND_STATUS_REG
);
2097 } while ((tmp
& BASR_PHASE_MATCH
) && !(tmp
& (BASR_BUSY_ERROR
|
2098 BASR_END_DMA_TRANSFER
)));
2101 At this point, either we've completed DMA, or we have a phase mismatch,
2102 or we've unexpectedly lost BUSY (which is a real error).
2104 For write DMAs, we want to wait until the last byte has been
2105 transferred out over the bus before we turn off DMA mode. Alas, there
2106 seems to be no terribly good way of doing this on a 5380 under all
2107 conditions. For non-scatter-gather operations, we can wait until REQ
2108 and ACK both go false, or until a phase mismatch occurs. Gather-writes
2109 are nastier, since the device will be expecting more data than we
2110 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
2111 could test LAST BIT SENT to assure transfer (I imagine this is precisely
2112 why this signal was added to the newer chips) but on the older 538[01]
2113 this signal does not exist. The workaround for this lack is a watchdog;
2114 we bail out of the wait-loop after a modest amount of wait-time if
2115 the usual exit conditions are not met. Not a terribly clean or
2116 correct solution :-%
2118 Reads are equally tricky due to a nasty characteristic of the NCR5380.
2119 If the chip is in DMA mode for an READ, it will respond to a target's
2120 REQ by latching the SCSI data into the INPUT DATA register and asserting
2121 ACK, even if it has _already_ been notified by the DMA controller that
2122 the current DMA transfer has completed! If the NCR5380 is then taken
2123 out of DMA mode, this already-acknowledged byte is lost.
2125 This is not a problem for "one DMA transfer per command" reads, because
2126 the situation will never arise... either all of the data is DMA'ed
2127 properly, or the target switches to MESSAGE IN phase to signal a
2128 disconnection (either operation bringing the DMA to a clean halt).
2129 However, in order to handle scatter-reads, we must work around the
2130 problem. The chosen fix is to DMA N-2 bytes, then check for the
2131 condition before taking the NCR5380 out of DMA mode. One or two extra
2132 bytes are transferred via PIO as necessary to fill out the original
2137 #ifdef READ_OVERRUNS
2139 if (((NCR5380_read(BUS_AND_STATUS_REG
) & (BASR_PHASE_MATCH
|BASR_ACK
)) ==
2140 (BASR_PHASE_MATCH
| BASR_ACK
))) {
2141 saved_data
= NCR5380_read(INPUT_DATA_REGISTER
);
2147 while (((tmp
= NCR5380_read(BUS_AND_STATUS_REG
)) & BASR_ACK
) ||
2148 (NCR5380_read(STATUS_REG
) & SR_REQ
)) {
2149 if (!(tmp
& BASR_PHASE_MATCH
)) break;
2150 if (--limit
< 0) break;
2155 #if (NDEBUG & NDEBUG_DMA)
2156 printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
2157 instance
->host_no
, tmp
, NCR5380_read(STATUS_REG
));
2160 NCR5380_write(MODE_REG
, MR_BASE
);
2161 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2163 residue
= NCR5380_dma_residual(instance
);
2167 *phase
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
2169 #ifdef READ_OVERRUNS
2170 if (*phase
== p
&& (p
& SR_IO
) && residue
== 0) {
2172 #if (NDEBUG & NDEBUG_DMA)
2173 printk("Got an input overrun, using saved byte\n");
2175 **data
= saved_data
;
2180 printk("No overrun??\n");
2183 #if (NDEBUG & NDEBUG_DMA)
2184 printk("Doing %d-byte PIO to 0x%X\n", cnt
, *data
);
2186 NCR5380_transfer_pio(instance
, phase
, &cnt
, data
);
2187 *count
-= toPIO
- cnt
;
2191 #if (NDEBUG & NDEBUG_DMA)
2192 printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
2193 *data
, *count
, *(*data
+*count
-1), *(*data
+*count
));
2197 #elif defined(REAL_DMA)
2199 #else /* defined(REAL_DMA_POLL) */
2201 #ifdef DMA_WORKS_RIGHT
2202 foo
= NCR5380_pread(instance
, d
, c
);
2205 if (hostdata
->flags
& FLAG_NCR53C400
) {
2209 if (!(foo
= NCR5380_pread(instance
, d
, c
- diff
))) {
2211 * We can't disable DMA mode after successfully transferring
2212 * what we plan to be the last byte, since that would open up
2213 * a race condition where if the target asserted REQ before
2214 * we got the DMA mode reset, the NCR5380 would have latched
2215 * an additional byte into the INPUT DATA register and we'd
2218 * The workaround was to transfer one fewer bytes than we
2219 * intended to with the pseudo-DMA read function, wait for
2220 * the chip to latch the last byte, read it, and then disable
2223 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2224 * REQ is deasserted when ACK is asserted, and not reasserted
2225 * until ACK goes false. Since the NCR5380 won't lower ACK
2226 * until DACK is asserted, which won't happen unless we twiddle
2227 * the DMA port or we take the NCR5380 out of DMA mode, we
2228 * can guarantee that we won't handshake another extra
2232 if (!(hostdata
->flags
& FLAG_NCR53C400
)) {
2233 while (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_DRQ
));
2234 /* Wait for clean handshake */
2235 while (NCR5380_read(STATUS_REG
) & SR_REQ
);
2236 d
[c
- 1] = NCR5380_read(INPUT_DATA_REG
);
2241 #ifdef DMA_WORKS_RIGHT
2242 foo
= NCR5380_pwrite(instance
, d
, c
);
2245 #if (NDEBUG & NDEBUG_C400_PWRITE)
2246 printk("About to pwrite %d bytes\n", c
);
2248 if (!(foo
= NCR5380_pwrite(instance
, d
, c
))) {
2250 * Wait for the last byte to be sent. If REQ is being asserted for
2251 * the byte we're interested, we'll ACK it and it will go false.
2253 if (!(hostdata
->flags
& FLAG_HAS_LAST_BYTE_SENT
)) {
2257 while (!(NCR5380_read(BUS_AND_STATUS_REG
) &
2258 BASR_DRQ
) && (NCR5380_read(BUS_AND_STATUS_REG
) &
2261 if (NCR5380_read(STATUS_REG
) & SR_REQ
) {
2263 !(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_ACK
);
2265 for (; timeout
&& (NCR5380_read(STATUS_REG
) & SR_REQ
);
2271 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2273 printk("scsi%d : timed out on last byte\n",
2278 if (hostdata
->flags
& FLAG_CHECK_LAST_BYTE_SENT
) {
2279 hostdata
->flags
&= ~FLAG_CHECK_LAST_BYTE_SENT
;
2280 if (NCR5380_read(TARGET_COMMAND_REG
) & TCR_LAST_BYTE_SENT
) {
2281 hostdata
->flags
|= FLAG_HAS_LAST_BYTE_SENT
;
2282 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2283 printk("scsi%d : last bit sent works\n",
2289 #if (NDEBUG & NDEBUG_C400_PWRITE)
2290 printk("Waiting for LASTBYTE\n");
2292 while (!(NCR5380_read(TARGET_COMMAND_REG
) & TCR_LAST_BYTE_SENT
));
2293 #if (NDEBUG & NDEBUG_C400_PWRITE)
2294 printk("Got LASTBYTE\n");
2304 NCR5380_write(MODE_REG
, MR_BASE
);
2305 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2307 if ((!(p
& SR_IO
)) && (hostdata
->flags
& FLAG_NCR53C400
)) {
2308 #if (NDEBUG & NDEBUG_C400_PWRITE)
2309 printk("53C400w: Checking for IRQ\n");
2311 if (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_IRQ
) {
2312 #if (NDEBUG & NDEBUG_C400_PWRITE)
2313 printk("53C400w: got it, reading reset interrupt reg\n");
2315 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
2317 printk("53C400w: IRQ NOT THERE!\n");
2323 *phase
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
2325 NCR5380_print_phase(instance
);
2327 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2329 #endif /* defined(REAL_DMA_POLL) */
2331 #endif /* def REAL_DMA */
2333 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
2336 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2338 * Purpose : run through the various SCSI phases and do as the target
2339 * directs us to. Operates on the currently connected command,
2340 * instance->connected.
2342 * Inputs : instance, instance for which we are doing commands
2344 * Side effects : SCSI things happen, the disconnected queue will be
2345 * modified if a command disconnects, *instance->connected will
2348 * XXX Note : we need to watch for bus free or a reset condition here
2349 * to recover from an unexpected bus free condition.
2352 static void NCR5380_information_transfer (struct Scsi_Host
*instance
) {
2353 NCR5380_local_declare();
2354 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
2356 unsigned char msgout
= NOP
;
2359 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2362 unsigned char *data
;
2363 unsigned char phase
, tmp
, extended_msg
[10], old_phase
=0xff;
2364 Scsi_Cmnd
*cmd
= (Scsi_Cmnd
*) hostdata
->connected
;
2365 NCR5380_setup(instance
);
2368 tmp
= NCR5380_read(STATUS_REG
);
2369 /* We only have a valid SCSI phase when REQ is asserted */
2371 phase
= (tmp
& PHASE_MASK
);
2372 if (phase
!= old_phase
) {
2374 #if (NDEBUG & NDEBUG_INFORMATION)
2375 NCR5380_print_phase(instance
);
2379 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
2380 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
2382 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
2384 while (NCR5380_read(STATUS_REG
) & SR_REQ
);
2385 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
2394 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2395 printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
2399 cmd
->result
= DID_ERROR
<< 16;
2404 * If there is no room left in the current buffer in the
2405 * scatter-gather list, move onto the next one.
2408 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
2410 --cmd
->SCp
.buffers_residual
;
2411 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
2412 cmd
->SCp
.ptr
= cmd
->SCp
.buffer
->address
;
2413 #if (NDEBUG & NDEBUG_INFORMATION)
2414 printk("scsi%d : %d bytes and %d buffers left\n",
2415 instance
->host_no
, cmd
->SCp
.this_residual
,
2416 cmd
->SCp
.buffers_residual
);
2421 * The preferred transfer method is going to be
2422 * PSEUDO-DMA for systems that are strictly PIO,
2423 * since we can let the hardware do the handshaking.
2425 * For this to work, we need to know the transfersize
2426 * ahead of time, since the pseudo-DMA code will sit
2427 * in an unconditional loop.
2430 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2432 * PSEUDO_DMA is defined here. If this is the g_NCR5380
2433 * driver then it will always be defined, so the
2434 * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
2435 * NCR5380 case. I think this is a fairly clean solution.
2436 * We supplement these 2 if's with the flag.
2438 #ifdef NCR5380_dma_xfer_len
2439 if (!cmd
->device
->borken
&&
2440 !(hostdata
->flags
& FLAG_NO_PSEUDO_DMA
) &&
2441 (transfersize
= NCR5380_dma_xfer_len(instance
, cmd
)) != 0) {
2443 transfersize
= cmd
->transfersize
;
2445 #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
2446 if( transfersize
> 512 )
2448 #endif /* LIMIT_TRANSFERSIZE */
2450 if (!cmd
->device
->borken
&& transfersize
&&
2451 !(hostdata
->flags
& FLAG_NO_PSEUDO_DMA
) &&
2452 cmd
->SCp
.this_residual
&& !(cmd
->SCp
.this_residual
%
2454 /* Limit transfers to 32K, for xx400 & xx406
2455 * pseudoDMA that transfers in 128 bytes blocks. */
2456 if (transfersize
> 32*1024)
2457 transfersize
= 32*1024;
2460 if (NCR5380_transfer_dma(instance
, &phase
,
2461 &len
, (unsigned char **) &cmd
->SCp
.ptr
)) {
2463 * If the watchdog timer fires, all future accesses to this
2464 * device will use the polled-IO.
2466 printk("scsi%d : switching target %d lun %d to slow handshake\n",
2467 instance
->host_no
, cmd
->target
, cmd
->lun
);
2468 cmd
->device
->borken
= 1;
2469 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
2473 cmd
->result
= DID_ERROR
<< 16;
2475 /* XXX - need to source or sink data here, as appropriate */
2477 cmd
->SCp
.this_residual
-= transfersize
- len
;
2479 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
2480 NCR5380_transfer_pio(instance
, &phase
,
2481 (int *) &cmd
->SCp
.this_residual
, (unsigned char **)
2487 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2488 cmd
->SCp
.Message
= tmp
;
2492 * Linking lets us reduce the time required to get the
2493 * next command out to the device, hopefully this will
2494 * mean we don't waste another revolution due to the delays
2495 * required by ARBITRATION and another SELECTION.
2497 * In the current implementation proposal, low level drivers
2498 * merely have to start the next command, pointed to by
2499 * next_link, done() is called as with unlinked commands.
2502 case LINKED_CMD_COMPLETE
:
2503 case LINKED_FLG_CMD_COMPLETE
:
2504 /* Accept message by clearing ACK */
2505 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2507 #if (NDEBUG & NDEBUG_LINKED)
2508 printk("scsi%d : target %d lun %d linked command complete.\n",
2509 instance
->host_no
, cmd
->target
, cmd
->lun
);
2512 * Sanity check : A linked command should only terminate with
2513 * one of these messages if there are more linked commands
2517 if (!cmd
->next_link
) {
2518 printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
2519 instance
->host_no
, cmd
->target
, cmd
->lun
);
2521 do_abort (instance
);
2525 initialize_SCp(cmd
->next_link
);
2526 /* The next command is still part of this process */
2527 cmd
->next_link
->tag
= cmd
->tag
;
2528 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
2529 #if (NDEBUG & NDEBUG_LINKED)
2530 printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
2531 instance
->host_no
, cmd
->target
, cmd
->lun
);
2533 #ifdef NCR5380_STATS
2534 collect_stats(hostdata
, cmd
);
2536 cmd
->scsi_done(cmd
);
2537 cmd
= hostdata
->connected
;
2539 #endif /* def LINKED */
2541 case COMMAND_COMPLETE
:
2542 /* Accept message by clearing ACK */
2544 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2545 hostdata
->connected
= NULL
;
2546 #if (NDEBUG & NDEBUG_QUEUES)
2547 printk("scsi%d : command for target %d, lun %d completed\n",
2548 instance
->host_no
, cmd
->target
, cmd
->lun
);
2550 hostdata
->busy
[cmd
->target
] &= ~(1 << cmd
->lun
);
2553 * I'm not sure what the correct thing to do here is :
2555 * If the command that just executed is NOT a request
2556 * sense, the obvious thing to do is to set the result
2557 * code to the values of the stored parameters.
2559 * If it was a REQUEST SENSE command, we need some way
2560 * to differentiate between the failure code of the original
2561 * and the failure code of the REQUEST sense - the obvious
2562 * case is success, where we fall through and leave the result
2565 * The non-obvious place is where the REQUEST SENSE failed
2568 if (cmd
->cmnd
[0] != REQUEST_SENSE
)
2569 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
2570 else if (cmd
->SCp
.Status
!= GOOD
)
2571 cmd
->result
= (cmd
->result
& 0x00ffff) | (DID_ERROR
<< 16);
2574 if ((cmd
->cmnd
[0] != REQUEST_SENSE
) &&
2575 (cmd
->SCp
.Status
== CHECK_CONDITION
)) {
2576 #if (NDEBUG & NDEBUG_AUTOSENSE)
2577 printk("scsi%d : performing request sense\n",
2580 cmd
->cmnd
[0] = REQUEST_SENSE
;
2581 cmd
->cmnd
[1] &= 0xe0;
2584 cmd
->cmnd
[4] = sizeof(cmd
->sense_buffer
);
2587 cmd
->SCp
.buffer
= NULL
;
2588 cmd
->SCp
.buffers_residual
= 0;
2589 cmd
->SCp
.ptr
= (char *) cmd
->sense_buffer
;
2590 cmd
->SCp
.this_residual
= sizeof(cmd
->sense_buffer
);
2593 LIST(cmd
,hostdata
->issue_queue
);
2594 cmd
->host_scribble
= (unsigned char *)
2595 hostdata
->issue_queue
;
2596 hostdata
->issue_queue
= (Scsi_Cmnd
*) cmd
;
2598 #if (NDEBUG & NDEBUG_QUEUES)
2599 printk("scsi%d : REQUEST SENSE added to head of issue queue\n",instance
->host_no
);
2602 #endif /* def AUTOSENSE */
2603 #ifdef NCR5380_STATS
2604 collect_stats(hostdata
, cmd
);
2606 cmd
->scsi_done(cmd
);
2609 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2611 * Restore phase bits to 0 so an interrupted selection,
2612 * arbitration can resume.
2614 NCR5380_write(TARGET_COMMAND_REG
, 0);
2616 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2619 case MESSAGE_REJECT
:
2620 /* Accept message by clearing ACK */
2621 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2622 switch (hostdata
->last_message
) {
2623 case HEAD_OF_QUEUE_TAG
:
2624 case ORDERED_QUEUE_TAG
:
2625 case SIMPLE_QUEUE_TAG
:
2626 cmd
->device
->tagged_queue
= 0;
2627 hostdata
->busy
[cmd
->target
] |= (1 << cmd
->lun
);
2633 /* Accept message by clearing ACK */
2634 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2635 cmd
->device
->disconnect
= 1;
2637 LIST(cmd
,hostdata
->disconnected_queue
);
2638 cmd
->host_scribble
= (unsigned char *)
2639 hostdata
->disconnected_queue
;
2640 hostdata
->connected
= NULL
;
2641 hostdata
->disconnected_queue
= cmd
;
2643 #if (NDEBUG & NDEBUG_QUEUES)
2644 printk("scsi%d : command for target %d lun %d was moved from connected to"
2645 " the disconnected_queue\n", instance
->host_no
,
2646 cmd
->target
, cmd
->lun
);
2649 * Restore phase bits to 0 so an interrupted selection,
2650 * arbitration can resume.
2652 NCR5380_write(TARGET_COMMAND_REG
, 0);
2654 /* Enable reselect interrupts */
2655 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2656 /* Wait for bus free to avoid nasty timeouts */
2657 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2660 NCR5380_print_status(instance
);
2664 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2665 * operation, in violation of the SCSI spec so we can safely
2666 * ignore SAVE/RESTORE pointers calls.
2668 * Unfortunately, some disks violate the SCSI spec and
2669 * don't issue the required SAVE_POINTERS message before
2670 * disconnecting, and we have to break spec to remain
2674 case RESTORE_POINTERS
:
2675 /* Accept message by clearing ACK */
2676 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2678 case EXTENDED_MESSAGE
:
2680 * Extended messages are sent in the following format :
2682 * 0 EXTENDED_MESSAGE == 1
2683 * 1 length (includes one byte for code, doesn't
2684 * include first two bytes)
2686 * 3..length+1 arguments
2688 * Start the extended message buffer with the EXTENDED_MESSAGE
2689 * byte, since print_msg() wants the whole thing.
2691 extended_msg
[0] = EXTENDED_MESSAGE
;
2692 /* Accept first byte by clearing ACK */
2693 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2695 #if (NDEBUG & NDEBUG_EXTENDED)
2696 printk("scsi%d : receiving extended message\n",
2701 data
= extended_msg
+ 1;
2702 phase
= PHASE_MSGIN
;
2703 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2705 #if (NDEBUG & NDEBUG_EXTENDED)
2706 printk("scsi%d : length=%d, code=0x%02x\n",
2707 instance
->host_no
, (int) extended_msg
[1],
2708 (int) extended_msg
[2]);
2711 if (!len
&& extended_msg
[1] <=
2712 (sizeof (extended_msg
) - 1)) {
2713 /* Accept third byte by clearing ACK */
2714 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2715 len
= extended_msg
[1] - 1;
2716 data
= extended_msg
+ 3;
2717 phase
= PHASE_MSGIN
;
2719 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2721 #if (NDEBUG & NDEBUG_EXTENDED)
2722 printk("scsi%d : message received, residual %d\n",
2723 instance
->host_no
, len
);
2726 switch (extended_msg
[2]) {
2729 case EXTENDED_MODIFY_DATA_POINTER
:
2730 case EXTENDED_EXTENDED_IDENTIFY
:
2734 printk("scsi%d: error receiving extended message\n",
2738 printk("scsi%d: extended message code %02x length %d is too long\n",
2739 instance
->host_no
, extended_msg
[2], extended_msg
[1]);
2742 /* Fall through to reject message */
2745 * If we get something weird that we aren't expecting,
2750 printk("scsi%d: rejecting message ", instance
->host_no
);
2751 print_msg (extended_msg
);
2753 } else if (tmp
!= EXTENDED_MESSAGE
)
2754 printk("scsi%d: rejecting unknown message %02x from target %d, lun %d\n",
2755 instance
->host_no
, tmp
, cmd
->target
, cmd
->lun
);
2757 printk("scsi%d: rejecting unknown extended message code %02x, length %d from target %d, lun %d\n",
2758 instance
->host_no
, extended_msg
[1], extended_msg
[0], cmd
->target
, cmd
->lun
);
2760 msgout
= MESSAGE_REJECT
;
2761 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
2764 } /* switch (tmp) */
2769 hostdata
->last_message
= msgout
;
2770 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2771 if (msgout
== ABORT
) {
2772 hostdata
->busy
[cmd
->target
] &= ~(1 << cmd
->lun
);
2773 hostdata
->connected
= NULL
;
2774 cmd
->result
= DID_ERROR
<< 16;
2775 #ifdef NCR5380_STATS
2776 collect_stats(hostdata
, cmd
);
2778 cmd
->scsi_done(cmd
);
2779 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2788 * XXX for performance reasons, on machines with a
2789 * PSEUDO-DMA architecture we should probably
2790 * use the dma transfer function.
2792 NCR5380_transfer_pio(instance
, &phase
, &len
,
2795 if (!disconnect
&& should_disconnect(cmd
->cmnd
[0])) {
2796 hostdata
->time_expires
= jiffies
+ USLEEP_SLEEP
;
2797 #if (NDEBUG & NDEBUG_USLEEP)
2798 printk("scsi%d : issued command, sleeping until %ul\n", instance
->host_no
,
2799 hostdata
->time_expires
);
2801 NCR5380_set_timer (instance
);
2804 #endif /* def USLEEP */
2809 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2810 cmd
->SCp
.Status
= tmp
;
2813 printk("scsi%d : unknown phase\n", instance
->host_no
);
2815 NCR5380_print(instance
);
2817 } /* switch(phase) */
2818 } /* if (tmp * SR_REQ) */
2821 if (!disconnect
&& hostdata
->time_expires
&& jiffies
>
2822 hostdata
->time_expires
) {
2823 hostdata
->time_expires
= jiffies
+ USLEEP_SLEEP
;
2824 #if (NDEBUG & NDEBUG_USLEEP)
2825 printk("scsi%d : poll timed out, sleeping until %ul\n", instance
->host_no
,
2826 hostdata
->time_expires
);
2828 NCR5380_set_timer (instance
);
2837 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2839 * Purpose : does reselection, initializing the instance->connected
2840 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2841 * nexus has been reestablished,
2843 * Inputs : instance - this instance of the NCR5380.
2848 static void NCR5380_reselect (struct Scsi_Host
*instance
) {
2849 NCR5380_local_declare();
2850 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
2852 unsigned char target_mask
;
2853 unsigned char lun
, phase
;
2858 unsigned char msg
[3];
2859 unsigned char *data
;
2860 Scsi_Cmnd
*tmp
= NULL
, *prev
;
2862 NCR5380_setup(instance
);
2865 * Disable arbitration, etc. since the host adapter obviously
2866 * lost, and tell an interrupted NCR5380_select() to restart.
2869 NCR5380_write(MODE_REG
, MR_BASE
);
2870 hostdata
->restart_select
= 1;
2872 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2874 #if (NDEBUG & NDEBUG_RESELECTION)
2875 printk("scsi%d : reselect\n", instance
->host_no
);
2879 * At this point, we have detected that our SCSI ID is on the bus,
2880 * SEL is true and BSY was false for at least one bus settle delay
2883 * We must assert BSY ourselves, until the target drops the SEL
2887 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2889 while (NCR5380_read(STATUS_REG
) & SR_SEL
);
2890 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2893 * Wait for target to go into MSGIN.
2896 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
));
2900 phase
= PHASE_MSGIN
;
2901 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2904 if (!msg
[0] & 0x80) {
2905 printk("scsi%d : expecting IDENTIFY message, got ",
2910 /* Accept message by clearing ACK */
2911 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2912 lun
= (msg
[0] & 0x07);
2915 * We need to add code for SCSI-II to track which devices have
2916 * I_T_L_Q nexuses established, and which have simple I_T_L
2917 * nexuses so we can chose to do additional data transfer.
2921 #error "SCSI-II tagged queueing is not supported yet"
2925 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2926 * just reestablished, and remove it from the disconnected queue.
2930 for (tmp
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
, prev
= NULL
;
2931 tmp
; prev
= tmp
, tmp
= (Scsi_Cmnd
*) tmp
->host_scribble
)
2932 if ((target_mask
== (1 << tmp
->target
)) && (lun
== tmp
->lun
)
2934 && (tag
== tmp
->tag
)
2938 REMOVE(prev
,prev
->host_scribble
,tmp
,tmp
->host_scribble
);
2939 prev
->host_scribble
= tmp
->host_scribble
;
2941 REMOVE(-1,hostdata
->disconnected_queue
,tmp
,tmp
->host_scribble
);
2942 hostdata
->disconnected_queue
= (Scsi_Cmnd
*) tmp
->host_scribble
;
2944 tmp
->host_scribble
= NULL
;
2950 printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
2951 instance
->host_no
, target_mask
, lun
, tag
);
2953 printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
2954 instance
->host_no
, target_mask
, lun
);
2957 * Since we have an established nexus that we can't do anything with,
2965 do_abort (instance
);
2967 hostdata
->connected
= tmp
;
2968 #if (NDEBUG & NDEBUG_RESELECTION)
2969 printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
2970 instance
->host_no
, tmp
->target
, tmp
->lun
, tmp
->tag
);
2976 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2978 * Purpose : called by interrupt handler when DMA finishes or a phase
2979 * mismatch occurs (which would finish the DMA transfer).
2981 * Inputs : instance - this instance of the NCR5380.
2983 * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
2984 * nexus has been reestablished, on failure NULL is returned.
2988 static void NCR5380_dma_complete (NCR5380_instance
*instance
) {
2989 NCR5380_local_declare();
2990 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*
2991 instance
->hostdata
);
2993 NCR5380_setup(instance
);
2996 * XXX this might not be right.
2998 * Wait for final byte to transfer, ie wait for ACK to go false.
3000 * We should use the Last Byte Sent bit, unfortunately this is
3001 * not available on the 5380/5381 (only the various CMOS chips)
3004 while (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_ACK
);
3006 NCR5380_write(MODE_REG
, MR_BASE
);
3007 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
3010 * The only places we should see a phase mismatch and have to send
3011 * data from the same set of pointers will be the data transfer
3012 * phases. So, residual, requested length are only important here.
3015 if (!(hostdata
->connected
->SCp
.phase
& SR_CD
)) {
3016 transferred
= instance
->dmalen
- NCR5380_dma_residual();
3017 hostdata
->connected
->SCp
.this_residual
-= transferred
;
3018 hostdata
->connected
->SCp
.ptr
+= transferred
;
3021 #endif /* def REAL_DMA */
3024 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
3026 * Purpose : abort a command
3028 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
3029 * host byte of the result field to, if zero DID_ABORTED is
3032 * Returns : 0 - success, -1 on failure.
3034 * XXX - there is no way to abort the command that is currently
3035 * connected, you have to wait for it to complete. If this is
3036 * a problem, we could implement longjmp() / setjmp(), setjmp()
3037 * called where the loop started in NCR5380_main().
3040 #ifndef NCR5380_abort
3043 int NCR5380_abort (Scsi_Cmnd
*cmd
) {
3044 NCR5380_local_declare();
3045 struct Scsi_Host
*instance
= cmd
->host
;
3046 struct NCR5380_hostdata
*hostdata
= (struct NCR5380_hostdata
*)
3048 Scsi_Cmnd
*tmp
, **prev
;
3050 printk("scsi%d : aborting command\n", instance
->host_no
);
3051 print_Scsi_Cmnd (cmd
);
3053 NCR5380_print_status (instance
);
3055 printk("scsi%d : aborting command\n", instance
->host_no
);
3056 print_Scsi_Cmnd (cmd
);
3058 NCR5380_print_status (instance
);
3061 NCR5380_setup(instance
);
3063 #if (NDEBUG & NDEBUG_ABORT)
3064 printk("scsi%d : abort called\n", instance
->host_no
);
3065 printk(" basr 0x%X, sr 0x%X\n",
3066 NCR5380_read(BUS_AND_STATUS_REG
), NCR5380_read(STATUS_REG
));
3071 * Case 1 : If the command is the currently executing command,
3072 * we'll set the aborted flag and return control so that
3073 * information transfer routine can exit cleanly.
3076 if (hostdata
->connected
== cmd
) {
3077 #if (NDEBUG & NDEBUG_ABORT)
3078 printk("scsi%d : aborting connected command\n", instance
->host_no
);
3080 hostdata
->aborted
= 1;
3082 * We should perform BSY checking, and make sure we haven't slipped
3086 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_ASSERT_ATN
);
3088 * Since we can't change phases until we've completed the current
3089 * handshake, we have to source or sink a byte of data if the current
3090 * phase is not MSGOUT.
3094 * Return control to the executing NCR drive so we can clear the
3095 * aborted flag and get back into our main loop.
3103 * Case 2 : If the command hasn't been issued yet, we simply remove it
3104 * from the issue queue.
3106 #if (NDEBUG & NDEBUG_ABORT)
3108 printk("scsi%d : abort going into loop.\n", instance
->host_no
);
3110 for (prev
= (Scsi_Cmnd
**) &(hostdata
->issue_queue
),
3111 tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
;
3112 tmp
; prev
= (Scsi_Cmnd
**) &(tmp
->host_scribble
), tmp
=
3113 (Scsi_Cmnd
*) tmp
->host_scribble
)
3115 REMOVE(5,*prev
,tmp
,tmp
->host_scribble
);
3116 (*prev
) = (Scsi_Cmnd
*) tmp
->host_scribble
;
3117 tmp
->host_scribble
= NULL
;
3118 tmp
->result
= DID_ABORT
<< 16;
3120 #if (NDEBUG & NDEBUG_ABORT)
3121 printk("scsi%d : abort removed command from issue queue.\n",
3125 return SCSI_ABORT_SUCCESS
;
3127 #if (NDEBUG & NDEBUG_ABORT)
3129 else if (prev
== tmp
) printk("scsi%d : LOOP\n", instance
->host_no
);
3133 * Case 3 : If any commands are connected, we're going to fail the abort
3134 * and let the high level SCSI driver retry at a later time or
3137 * Timeouts, and therefore aborted commands, will be highly unlikely
3138 * and handling them cleanly in this situation would make the common
3139 * case of noresets less efficient, and would pollute our code. So,
3143 if (hostdata
->connected
) {
3145 #if (NDEBUG & NDEBUG_ABORT)
3146 printk("scsi%d : abort failed, command connected.\n", instance
->host_no
);
3148 return SCSI_ABORT_NOT_RUNNING
;
3152 * Case 4: If the command is currently disconnected from the bus, and
3153 * there are no connected commands, we reconnect the I_T_L or
3154 * I_T_L_Q nexus associated with it, go into message out, and send
3157 * This case is especially ugly. In order to reestablish the nexus, we
3158 * need to call NCR5380_select(). The easiest way to implement this
3159 * function was to abort if the bus was busy, and let the interrupt
3160 * handler triggered on the SEL for reselect take care of lost arbitrations
3161 * where necessary, meaning interrupts need to be enabled.
3163 * When interrupts are enabled, the queues may change - so we
3164 * can't remove it from the disconnected queue before selecting it
3165 * because that could cause a failure in hashing the nexus if that
3166 * device reselected.
3168 * Since the queues may change, we can't use the pointers from when we
3171 * So, we must first locate the command, and if NCR5380_select()
3172 * succeeds, then issue the abort, relocate the command and remove
3173 * it from the disconnected queue.
3176 for (tmp
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
; tmp
;
3177 tmp
= (Scsi_Cmnd
*) tmp
->host_scribble
)
3180 #if (NDEBUG & NDEBUG_ABORT)
3181 printk("scsi%d : aborting disconnected command.\n", instance
->host_no
);
3184 if (NCR5380_select (instance
, cmd
, (int) cmd
->tag
))
3185 return SCSI_ABORT_BUSY
;
3187 #if (NDEBUG & NDEBUG_ABORT)
3188 printk("scsi%d : nexus reestablished.\n", instance
->host_no
);
3191 do_abort (instance
);
3194 for (prev
= (Scsi_Cmnd
**) &(hostdata
->disconnected_queue
),
3195 tmp
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
;
3196 tmp
; prev
= (Scsi_Cmnd
**) &(tmp
->host_scribble
), tmp
=
3197 (Scsi_Cmnd
*) tmp
->host_scribble
)
3199 REMOVE(5,*prev
,tmp
,tmp
->host_scribble
);
3200 *prev
= (Scsi_Cmnd
*) tmp
->host_scribble
;
3201 tmp
->host_scribble
= NULL
;
3202 tmp
->result
= DID_ABORT
<< 16;
3205 return SCSI_ABORT_SUCCESS
;
3210 * Case 5 : If we reached this point, the command was not found in any of
3213 * We probably reached this point because of an unlikely race condition
3214 * between the command completing successfully and the abortion code,
3215 * so we won't panic, but we will notify the user in case something really
3220 printk("scsi%d : warning : SCSI command probably completed successfully\n"
3221 " before abortion\n", instance
->host_no
);
3222 return SCSI_ABORT_NOT_RUNNING
;
3227 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
3229 * Purpose : reset the SCSI bus.
3231 * Returns : SCSI_RESET_WAKEUP
3235 #ifndef NCR5380_reset
3238 int NCR5380_reset (Scsi_Cmnd
*cmd
, unsigned int dummy
) {
3239 NCR5380_local_declare();
3240 NCR5380_setup(cmd
->host
);
3242 NCR5380_print_status (cmd
->host
);
3243 do_reset (cmd
->host
);
3245 return SCSI_RESET_WAKEUP
;