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 * For more information, please consult
17 * SCSI Protocol Controller
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
28 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
31 * - Some of the debug statements were incorrect (undefined variables and the
32 * like). I fixed that.
34 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
35 * possible DMA transfer size should also happen for REAL_DMA. I added this
36 * in the #if statement.
38 * - When using real DMA, information_transfer() should return in a DATAOUT
39 * phase after starting the DMA. It has nothing more to do.
41 * - The interrupt service routine should run main after end of DMA, too (not
42 * only after RESELECTION interrupts). Additionally, it should _not_ test
43 * for more interrupts after running main, since a DMA process may have
44 * been started and interrupts are turned on now. The new int could happen
45 * inside the execution of NCR5380_intr(), leading to recursive
48 * - I've added a function merge_contiguous_buffers() that tries to
49 * merge scatter-gather buffers that are located at contiguous
50 * physical addresses and can be processed with the same DMA setup.
51 * Since most scatter-gather operations work on a page (4K) of
52 * 4 buffers (1K), in more than 90% of all cases three interrupts and
53 * DMA setup actions are saved.
55 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
56 * and USLEEP, because these were messing up readability and will never be
57 * needed for Atari SCSI.
59 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
60 * stuff), and 'main' is executed in a bottom half if awoken by an
63 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
64 * constructs. In my eyes, this made the source rather unreadable, so I
65 * finally replaced that by the *_PRINTK() macros.
69 /* Adapted for the sun3 by Sam Creasey. */
71 #include <scsi/scsi_dbg.h>
72 #include <scsi/scsi_transport_spi.h>
74 #if (NDEBUG & NDEBUG_LISTS)
77 printk("LINE:%d Adding %p to %p\n", \
78 __LINE__, (void*)(x), (void*)(y)); \
82 #define REMOVE(w, x, y, z) \
84 printk("LINE:%d Removing: %p->%p %p->%p \n", \
85 __LINE__, (void*)(w), (void*)(x), \
86 (void*)(y), (void*)(z)); \
92 #define REMOVE(w,x,y,z)
98 * This is a generic 5380 driver. To use it on a different platform,
99 * one simply writes appropriate system specific macros (ie, data
100 * transfer - some PC's will use the I/O bus, 68K's must use
101 * memory mapped) and drops this file in their 'C' wrapper.
103 * As far as command queueing, two queues are maintained for
104 * each 5380 in the system - commands that haven't been issued yet,
105 * and commands that are currently executing. This means that an
106 * unlimited number of commands may be queued, letting
107 * more commands propagate from the higher driver levels giving higher
108 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
109 * allowing multiple commands to propagate all the way to a SCSI-II device
110 * while a command is already executing.
113 * Issues specific to the NCR5380 :
115 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
116 * piece of hardware that requires you to sit in a loop polling for
117 * the REQ signal as long as you are connected. Some devices are
118 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
119 * while doing long seek operations. [...] These
120 * broken devices are the exception rather than the rule and I'd rather
121 * spend my time optimizing for the normal case.
125 * At the heart of the design is a coroutine, NCR5380_main,
126 * which is started from a workqueue for each NCR5380 host in the
127 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
128 * removing the commands from the issue queue and calling
129 * NCR5380_select() if a nexus is not established.
131 * Once a nexus is established, the NCR5380_information_transfer()
132 * phase goes through the various phases as instructed by the target.
133 * if the target goes into MSG IN and sends a DISCONNECT message,
134 * the command structure is placed into the per instance disconnected
135 * queue, and NCR5380_main tries to find more work. If the target is
136 * idle for too long, the system will try to sleep.
138 * If a command has disconnected, eventually an interrupt will trigger,
139 * calling NCR5380_intr() which will in turn call NCR5380_reselect
140 * to reestablish a nexus. This will run main if necessary.
142 * On command termination, the done function will be called as
145 * SCSI pointers are maintained in the SCp field of SCSI command
146 * structures, being initialized after the command is connected
147 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
148 * Note that in violation of the standard, an implicit SAVE POINTERS operation
149 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
154 * This file a skeleton Linux SCSI driver for the NCR 5380 series
155 * of chips. To use it, you write an architecture specific functions
156 * and macros and include this file in your driver.
158 * These macros control options :
159 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
160 * for commands that return with a CHECK CONDITION status.
162 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
165 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
167 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
169 * These macros MUST be defined :
171 * NCR5380_read(register) - read from the specified register
173 * NCR5380_write(register, value) - write to the specific register
175 * NCR5380_implementation_fields - additional fields needed for this
176 * specific implementation of the NCR5380
178 * Either real DMA *or* pseudo DMA may be implemented
180 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
181 * Note that the DMA setup functions should return the number of bytes
182 * that they were able to program the controller for.
184 * Also note that generic i386/PC versions of these macros are
185 * available as NCR5380_i386_dma_write_setup,
186 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
188 * NCR5380_dma_write_setup(instance, src, count) - initialize
189 * NCR5380_dma_read_setup(instance, dst, count) - initialize
190 * NCR5380_dma_residual(instance); - residual count
193 * NCR5380_pwrite(instance, src, count)
194 * NCR5380_pread(instance, dst, count);
196 * The generic driver is initialized by calling NCR5380_init(instance),
197 * after setting the appropriate host specific fields and ID. If the
198 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
199 * possible) function may be used.
202 /* Macros ease life... :-) */
203 #define SETUP_HOSTDATA(in) \
204 struct NCR5380_hostdata *hostdata = \
205 (struct NCR5380_hostdata *)(in)->hostdata
206 #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
208 #define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
209 #define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
210 #define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
212 #define HOSTNO instance->host_no
213 #define H_NO(cmd) (cmd)->device->host->host_no
215 static int do_abort(struct Scsi_Host
*);
216 static void do_reset(struct Scsi_Host
*);
221 * Functions for handling tagged queuing
222 * =====================================
224 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
226 * Using consecutive numbers for the tags is no good idea in my eyes. There
227 * could be wrong re-usings if the counter (8 bit!) wraps and some early
228 * command has been preempted for a long time. My solution: a bitfield for
229 * remembering used tags.
231 * There's also the problem that each target has a certain queue size, but we
232 * cannot know it in advance :-( We just see a QUEUE_FULL status being
233 * returned. So, in this case, the driver internal queue size assumption is
234 * reduced to the number of active tags if QUEUE_FULL is returned by the
235 * target. The command is returned to the mid-level, but with status changed
236 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
239 * We're also not allowed running tagged commands as long as an untagged
240 * command is active. And REQUEST SENSE commands after a contingent allegiance
241 * condition _must_ be untagged. To keep track whether an untagged command has
242 * been issued, the host->busy array is still employed, as it is without
243 * support for tagged queuing.
245 * One could suspect that there are possible race conditions between
246 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
247 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
248 * which already guaranteed to be running at most once. It is also the only
249 * place where tags/LUNs are allocated. So no other allocation can slip
250 * between that pair, there could only happen a reselection, which can free a
251 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
252 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
255 static void __init
init_tags(struct NCR5380_hostdata
*hostdata
)
258 struct tag_alloc
*ta
;
260 if (!(hostdata
->flags
& FLAG_TAGGED_QUEUING
))
263 for (target
= 0; target
< 8; ++target
) {
264 for (lun
= 0; lun
< 8; ++lun
) {
265 ta
= &hostdata
->TagAlloc
[target
][lun
];
266 bitmap_zero(ta
->allocated
, MAX_TAGS
);
267 ta
->nr_allocated
= 0;
268 /* At the beginning, assume the maximum queue size we could
269 * support (MAX_TAGS). This value will be decreased if the target
270 * returns QUEUE_FULL status.
272 ta
->queue_size
= MAX_TAGS
;
278 /* Check if we can issue a command to this LUN: First see if the LUN is marked
279 * busy by an untagged command. If the command should use tagged queuing, also
280 * check that there is a free tag and the target's queue won't overflow. This
281 * function should be called with interrupts disabled to avoid race
285 static int is_lun_busy(struct scsi_cmnd
*cmd
, int should_be_tagged
)
287 u8 lun
= cmd
->device
->lun
;
288 SETUP_HOSTDATA(cmd
->device
->host
);
290 if (hostdata
->busy
[cmd
->device
->id
] & (1 << lun
))
292 if (!should_be_tagged
||
293 !(hostdata
->flags
& FLAG_TAGGED_QUEUING
) ||
294 !cmd
->device
->tagged_supported
)
296 if (hostdata
->TagAlloc
[scmd_id(cmd
)][lun
].nr_allocated
>=
297 hostdata
->TagAlloc
[scmd_id(cmd
)][lun
].queue_size
) {
298 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %d: no free tags\n",
299 H_NO(cmd
), cmd
->device
->id
, lun
);
306 /* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
307 * must be called before!), or reserve the LUN in 'busy' if the command is
311 static void cmd_get_tag(struct scsi_cmnd
*cmd
, int should_be_tagged
)
313 u8 lun
= cmd
->device
->lun
;
314 SETUP_HOSTDATA(cmd
->device
->host
);
316 /* If we or the target don't support tagged queuing, allocate the LUN for
317 * an untagged command.
319 if (!should_be_tagged
||
320 !(hostdata
->flags
& FLAG_TAGGED_QUEUING
) ||
321 !cmd
->device
->tagged_supported
) {
323 hostdata
->busy
[cmd
->device
->id
] |= (1 << lun
);
324 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %d now allocated by untagged "
325 "command\n", H_NO(cmd
), cmd
->device
->id
, lun
);
327 struct tag_alloc
*ta
= &hostdata
->TagAlloc
[scmd_id(cmd
)][lun
];
329 cmd
->tag
= find_first_zero_bit(ta
->allocated
, MAX_TAGS
);
330 set_bit(cmd
->tag
, ta
->allocated
);
332 dprintk(NDEBUG_TAGS
, "scsi%d: using tag %d for target %d lun %d "
333 "(now %d tags in use)\n",
334 H_NO(cmd
), cmd
->tag
, cmd
->device
->id
,
335 lun
, ta
->nr_allocated
);
340 /* Mark the tag of command 'cmd' as free, or in case of an untagged command,
344 static void cmd_free_tag(struct scsi_cmnd
*cmd
)
346 u8 lun
= cmd
->device
->lun
;
347 SETUP_HOSTDATA(cmd
->device
->host
);
349 if (cmd
->tag
== TAG_NONE
) {
350 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << lun
);
351 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %d untagged cmd finished\n",
352 H_NO(cmd
), cmd
->device
->id
, lun
);
353 } else if (cmd
->tag
>= MAX_TAGS
) {
354 printk(KERN_NOTICE
"scsi%d: trying to free bad tag %d!\n",
355 H_NO(cmd
), cmd
->tag
);
357 struct tag_alloc
*ta
= &hostdata
->TagAlloc
[scmd_id(cmd
)][lun
];
358 clear_bit(cmd
->tag
, ta
->allocated
);
360 dprintk(NDEBUG_TAGS
, "scsi%d: freed tag %d for target %d lun %d\n",
361 H_NO(cmd
), cmd
->tag
, cmd
->device
->id
, lun
);
366 static void free_all_tags(struct NCR5380_hostdata
*hostdata
)
369 struct tag_alloc
*ta
;
371 if (!(hostdata
->flags
& FLAG_TAGGED_QUEUING
))
374 for (target
= 0; target
< 8; ++target
) {
375 for (lun
= 0; lun
< 8; ++lun
) {
376 ta
= &hostdata
->TagAlloc
[target
][lun
];
377 bitmap_zero(ta
->allocated
, MAX_TAGS
);
378 ta
->nr_allocated
= 0;
383 #endif /* SUPPORT_TAGS */
387 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
389 * Purpose: Try to merge several scatter-gather requests into one DMA
390 * transfer. This is possible if the scatter buffers lie on
391 * physical contiguous addresses.
393 * Parameters: struct scsi_cmnd *cmd
394 * The command to work on. The first scatter buffer's data are
395 * assumed to be already transferred into ptr/this_residual.
398 static void merge_contiguous_buffers(struct scsi_cmnd
*cmd
)
400 #if !defined(CONFIG_SUN3)
401 unsigned long endaddr
;
402 #if (NDEBUG & NDEBUG_MERGING)
403 unsigned long oldlen
= cmd
->SCp
.this_residual
;
407 for (endaddr
= virt_to_phys(cmd
->SCp
.ptr
+ cmd
->SCp
.this_residual
- 1) + 1;
408 cmd
->SCp
.buffers_residual
&&
409 virt_to_phys(sg_virt(&cmd
->SCp
.buffer
[1])) == endaddr
;) {
410 dprintk(NDEBUG_MERGING
, "VTOP(%p) == %08lx -> merging\n",
411 page_address(sg_page(&cmd
->SCp
.buffer
[1])), endaddr
);
412 #if (NDEBUG & NDEBUG_MERGING)
416 --cmd
->SCp
.buffers_residual
;
417 cmd
->SCp
.this_residual
+= cmd
->SCp
.buffer
->length
;
418 endaddr
+= cmd
->SCp
.buffer
->length
;
420 #if (NDEBUG & NDEBUG_MERGING)
421 if (oldlen
!= cmd
->SCp
.this_residual
)
422 dprintk(NDEBUG_MERGING
, "merged %d buffers from %p, new length %08x\n",
423 cnt
, cmd
->SCp
.ptr
, cmd
->SCp
.this_residual
);
425 #endif /* !defined(CONFIG_SUN3) */
429 * initialize_SCp - init the scsi pointer field
430 * @cmd: command block to set up
432 * Set up the internal fields in the SCSI command.
435 static inline void initialize_SCp(struct scsi_cmnd
*cmd
)
438 * Initialize the Scsi Pointer field so that all of the commands in the
439 * various queues are valid.
442 if (scsi_bufflen(cmd
)) {
443 cmd
->SCp
.buffer
= scsi_sglist(cmd
);
444 cmd
->SCp
.buffers_residual
= scsi_sg_count(cmd
) - 1;
445 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
446 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
447 /* ++roman: Try to merge some scatter-buffers if they are at
448 * contiguous physical addresses.
450 merge_contiguous_buffers(cmd
);
452 cmd
->SCp
.buffer
= NULL
;
453 cmd
->SCp
.buffers_residual
= 0;
455 cmd
->SCp
.this_residual
= 0;
460 * NCR5380_poll_politely - wait for chip register value
461 * @instance: controller to poll
462 * @reg: 5380 register to poll
463 * @bit: Bitmask to check
464 * @val: Value required to exit
465 * @wait: Time-out in jiffies
467 * Polls the chip in a reasonably efficient manner waiting for an
468 * event to occur. After a short quick poll we begin to yield the CPU
469 * (if possible). In irq contexts the time-out is arbitrarily limited.
470 * Callers may hold locks as long as they are held in irq mode.
472 * Returns 0 if event occurred otherwise -ETIMEDOUT.
475 static int NCR5380_poll_politely(struct Scsi_Host
*instance
,
476 int reg
, int bit
, int val
, int wait
)
478 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
479 unsigned long deadline
= jiffies
+ wait
;
482 /* Busy-wait for up to 10 ms */
483 n
= min(10000U, jiffies_to_usecs(wait
));
484 n
*= hostdata
->accesses_per_ms
;
487 if ((NCR5380_read(reg
) & bit
) == val
)
492 if (irqs_disabled() || in_interrupt())
495 /* Repeatedly sleep for 1 ms until deadline */
496 while (time_is_after_jiffies(deadline
)) {
497 schedule_timeout_uninterruptible(1);
498 if ((NCR5380_read(reg
) & bit
) == val
)
505 #include <linux/delay.h>
512 { SR_DBP
, "PARITY"}, { SR_RST
, "RST" }, { SR_BSY
, "BSY" },
513 { SR_REQ
, "REQ" }, { SR_MSG
, "MSG" }, { SR_CD
, "CD" }, { SR_IO
, "IO" },
514 { SR_SEL
, "SEL" }, {0, NULL
}
516 {BASR_ATN
, "ATN"}, {BASR_ACK
, "ACK"}, {0, NULL
}
518 {ICR_ASSERT_RST
, "ASSERT RST"},{ICR_ASSERT_ACK
, "ASSERT ACK"},
519 {ICR_ASSERT_BSY
, "ASSERT BSY"}, {ICR_ASSERT_SEL
, "ASSERT SEL"},
520 {ICR_ASSERT_ATN
, "ASSERT ATN"}, {ICR_ASSERT_DATA
, "ASSERT DATA"},
523 {MR_BLOCK_DMA_MODE
, "MODE BLOCK DMA"}, {MR_TARGET
, "MODE TARGET"},
524 {MR_ENABLE_PAR_CHECK
, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR
,
525 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR
,"MODE EOP INTR"},
526 {MR_MONITOR_BSY
, "MODE MONITOR BSY"},
527 {MR_DMA_MODE
, "MODE DMA"}, {MR_ARBITRATE
, "MODE ARBITRATION"},
532 * NCR5380_print - print scsi bus signals
533 * @instance: adapter state to dump
535 * Print the SCSI bus signals for debugging purposes
538 static void NCR5380_print(struct Scsi_Host
*instance
)
540 unsigned char status
, data
, basr
, mr
, icr
, i
;
543 local_irq_save(flags
);
544 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
545 status
= NCR5380_read(STATUS_REG
);
546 mr
= NCR5380_read(MODE_REG
);
547 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
548 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
549 local_irq_restore(flags
);
550 printk("STATUS_REG: %02x ", status
);
551 for (i
= 0; signals
[i
].mask
; ++i
)
552 if (status
& signals
[i
].mask
)
553 printk(",%s", signals
[i
].name
);
554 printk("\nBASR: %02x ", basr
);
555 for (i
= 0; basrs
[i
].mask
; ++i
)
556 if (basr
& basrs
[i
].mask
)
557 printk(",%s", basrs
[i
].name
);
558 printk("\nICR: %02x ", icr
);
559 for (i
= 0; icrs
[i
].mask
; ++i
)
560 if (icr
& icrs
[i
].mask
)
561 printk(",%s", icrs
[i
].name
);
562 printk("\nMODE: %02x ", mr
);
563 for (i
= 0; mrs
[i
].mask
; ++i
)
564 if (mr
& mrs
[i
].mask
)
565 printk(",%s", mrs
[i
].name
);
573 {PHASE_DATAOUT
, "DATAOUT"}, {PHASE_DATAIN
, "DATAIN"}, {PHASE_CMDOUT
, "CMDOUT"},
574 {PHASE_STATIN
, "STATIN"}, {PHASE_MSGOUT
, "MSGOUT"}, {PHASE_MSGIN
, "MSGIN"},
575 {PHASE_UNKNOWN
, "UNKNOWN"}
579 * NCR5380_print_phase - show SCSI phase
580 * @instance: adapter to dump
582 * Print the current SCSI phase for debugging purposes
587 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
589 unsigned char status
;
592 status
= NCR5380_read(STATUS_REG
);
593 if (!(status
& SR_REQ
))
594 printk(KERN_DEBUG
"scsi%d: REQ not asserted, phase unknown.\n", HOSTNO
);
596 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
597 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
599 printk(KERN_DEBUG
"scsi%d: phase %s\n", HOSTNO
, phases
[i
].name
);
606 * ++roman: New scheme of calling NCR5380_main()
608 * If we're not in an interrupt, we can call our main directly, it cannot be
609 * already running. Else, we queue it on a task queue, if not 'main_running'
610 * tells us that a lower level is already executing it. This way,
611 * 'main_running' needs not be protected in a special way.
613 * queue_main() is a utility function for putting our main onto the task
614 * queue, if main_running is false. It should be called only from a
615 * interrupt or bottom half.
618 #include <linux/gfp.h>
619 #include <linux/workqueue.h>
620 #include <linux/interrupt.h>
622 static inline void queue_main(struct NCR5380_hostdata
*hostdata
)
624 if (!hostdata
->main_running
) {
625 /* If in interrupt and NCR5380_main() not already running,
626 queue it on the 'immediate' task queue, to be processed
627 immediately after the current interrupt processing has
629 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
631 /* else: nothing to do: the running NCR5380_main() will pick up
632 any newly queued command. */
636 * NCR58380_info - report driver and host information
637 * @instance: relevant scsi host instance
639 * For use as the host template info() handler.
644 static const char *NCR5380_info(struct Scsi_Host
*instance
)
646 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
648 return hostdata
->info
;
651 static void prepare_info(struct Scsi_Host
*instance
)
653 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
655 snprintf(hostdata
->info
, sizeof(hostdata
->info
),
656 "%s, io_port 0x%lx, n_io_port %d, "
657 "base 0x%lx, irq %d, "
658 "can_queue %d, cmd_per_lun %d, "
659 "sg_tablesize %d, this_id %d, "
662 instance
->hostt
->name
, instance
->io_port
, instance
->n_io_port
,
663 instance
->base
, instance
->irq
,
664 instance
->can_queue
, instance
->cmd_per_lun
,
665 instance
->sg_tablesize
, instance
->this_id
,
666 hostdata
->flags
& FLAG_TAGGED_QUEUING
? "TAGGED_QUEUING " : "",
667 hostdata
->flags
& FLAG_TOSHIBA_DELAY
? "TOSHIBA_DELAY " : "",
684 * NCR5380_print_status - dump controller info
685 * @instance: controller to dump
687 * Print commands in the various queues, called from NCR5380_abort
691 static void lprint_Scsi_Cmnd(struct scsi_cmnd
*cmd
)
694 unsigned char *command
;
695 printk("scsi%d: destination target %d, lun %llu\n",
696 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
697 printk(KERN_CONT
" command = ");
699 printk(KERN_CONT
"%2d (0x%02x)", command
[0], command
[0]);
700 for (i
= 1, s
= COMMAND_SIZE(command
[0]); i
< s
; ++i
)
701 printk(KERN_CONT
" %02x", command
[i
]);
705 static void __maybe_unused
NCR5380_print_status(struct Scsi_Host
*instance
)
707 struct NCR5380_hostdata
*hostdata
;
708 struct scsi_cmnd
*ptr
;
711 NCR5380_dprint(NDEBUG_ANY
, instance
);
712 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
714 hostdata
= (struct NCR5380_hostdata
*)instance
->hostdata
;
716 local_irq_save(flags
);
717 printk("NCR5380: coroutine is%s running.\n",
718 hostdata
->main_running
? "" : "n't");
719 if (!hostdata
->connected
)
720 printk("scsi%d: no currently connected command\n", HOSTNO
);
722 lprint_Scsi_Cmnd((struct scsi_cmnd
*) hostdata
->connected
);
723 printk("scsi%d: issue_queue\n", HOSTNO
);
724 for (ptr
= (struct scsi_cmnd
*)hostdata
->issue_queue
; ptr
; ptr
= NEXT(ptr
))
725 lprint_Scsi_Cmnd(ptr
);
727 printk("scsi%d: disconnected_queue\n", HOSTNO
);
728 for (ptr
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
; ptr
;
730 lprint_Scsi_Cmnd(ptr
);
732 local_irq_restore(flags
);
736 static void show_Scsi_Cmnd(struct scsi_cmnd
*cmd
, struct seq_file
*m
)
739 unsigned char *command
;
740 seq_printf(m
, "scsi%d: destination target %d, lun %llu\n",
741 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
742 seq_puts(m
, " command = ");
744 seq_printf(m
, "%2d (0x%02x)", command
[0], command
[0]);
745 for (i
= 1, s
= COMMAND_SIZE(command
[0]); i
< s
; ++i
)
746 seq_printf(m
, " %02x", command
[i
]);
750 static int __maybe_unused
NCR5380_show_info(struct seq_file
*m
,
751 struct Scsi_Host
*instance
)
753 struct NCR5380_hostdata
*hostdata
;
754 struct scsi_cmnd
*ptr
;
757 hostdata
= (struct NCR5380_hostdata
*)instance
->hostdata
;
759 local_irq_save(flags
);
760 seq_printf(m
, "NCR5380: coroutine is%s running.\n",
761 hostdata
->main_running
? "" : "n't");
762 if (!hostdata
->connected
)
763 seq_printf(m
, "scsi%d: no currently connected command\n", HOSTNO
);
765 show_Scsi_Cmnd((struct scsi_cmnd
*) hostdata
->connected
, m
);
766 seq_printf(m
, "scsi%d: issue_queue\n", HOSTNO
);
767 for (ptr
= (struct scsi_cmnd
*)hostdata
->issue_queue
; ptr
; ptr
= NEXT(ptr
))
768 show_Scsi_Cmnd(ptr
, m
);
770 seq_printf(m
, "scsi%d: disconnected_queue\n", HOSTNO
);
771 for (ptr
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
; ptr
;
773 show_Scsi_Cmnd(ptr
, m
);
775 local_irq_restore(flags
);
780 * NCR5380_init - initialise an NCR5380
781 * @instance: adapter to configure
782 * @flags: control flags
784 * Initializes *instance and corresponding 5380 chip,
785 * with flags OR'd into the initial flags value.
787 * Notes : I assume that the host, hostno, and id bits have been
788 * set correctly. I don't care about the irq and other fields.
790 * Returns 0 for success
793 static int __init
NCR5380_init(struct Scsi_Host
*instance
, int flags
)
796 SETUP_HOSTDATA(instance
);
797 unsigned long deadline
;
799 hostdata
->host
= instance
;
800 hostdata
->id_mask
= 1 << instance
->this_id
;
801 hostdata
->id_higher_mask
= 0;
802 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
803 if (i
> hostdata
->id_mask
)
804 hostdata
->id_higher_mask
|= i
;
805 for (i
= 0; i
< 8; ++i
)
806 hostdata
->busy
[i
] = 0;
810 #if defined (REAL_DMA)
811 hostdata
->dma_len
= 0;
813 hostdata
->connected
= NULL
;
814 hostdata
->issue_queue
= NULL
;
815 hostdata
->disconnected_queue
= NULL
;
816 hostdata
->flags
= flags
;
818 INIT_WORK(&hostdata
->main_task
, NCR5380_main
);
819 hostdata
->work_q
= alloc_workqueue("ncr5380_%d",
820 WQ_UNBOUND
| WQ_MEM_RECLAIM
,
821 1, instance
->host_no
);
822 if (!hostdata
->work_q
)
825 prepare_info(instance
);
827 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
828 NCR5380_write(MODE_REG
, MR_BASE
);
829 NCR5380_write(TARGET_COMMAND_REG
, 0);
830 NCR5380_write(SELECT_ENABLE_REG
, 0);
832 /* Calibrate register polling loop */
834 deadline
= jiffies
+ 1;
837 } while (time_is_after_jiffies(deadline
));
838 deadline
+= msecs_to_jiffies(256);
840 NCR5380_read(STATUS_REG
);
843 } while (time_is_after_jiffies(deadline
));
844 hostdata
->accesses_per_ms
= i
/ 256;
850 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
851 * @instance: adapter to check
853 * If the system crashed, it may have crashed with a connected target and
854 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
855 * currently established nexus, which we know nothing about. Failing that
858 * Note that a bus reset will cause the chip to assert IRQ.
860 * Returns 0 if successful, otherwise -ENXIO.
863 static int NCR5380_maybe_reset_bus(struct Scsi_Host
*instance
)
865 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
868 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) && pass
<= 6; ++pass
) {
873 shost_printk(KERN_ERR
, instance
, "SCSI bus busy, waiting up to five seconds\n");
874 NCR5380_poll_politely(instance
,
875 STATUS_REG
, SR_BSY
, 0, 5 * HZ
);
878 shost_printk(KERN_ERR
, instance
, "bus busy, attempting abort\n");
882 shost_printk(KERN_ERR
, instance
, "bus busy, attempting reset\n");
884 /* Wait after a reset; the SCSI standard calls for
885 * 250ms, we wait 500ms to be on the safe side.
886 * But some Toshiba CD-ROMs need ten times that.
888 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
894 shost_printk(KERN_ERR
, instance
, "bus locked solid\n");
902 * NCR5380_exit - remove an NCR5380
903 * @instance: adapter to remove
905 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
908 static void NCR5380_exit(struct Scsi_Host
*instance
)
910 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
912 cancel_work_sync(&hostdata
->main_task
);
913 destroy_workqueue(hostdata
->work_q
);
917 * NCR5380_queue_command - queue a command
918 * @instance: the relevant SCSI adapter
921 * cmd is added to the per-instance issue queue, with minor
922 * twiddling done to the host specific fields of cmd. If the
923 * main coroutine is not running, it is restarted.
926 static int NCR5380_queue_command(struct Scsi_Host
*instance
,
927 struct scsi_cmnd
*cmd
)
929 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
930 struct scsi_cmnd
*tmp
;
933 #if (NDEBUG & NDEBUG_NO_WRITE)
934 switch (cmd
->cmnd
[0]) {
937 printk(KERN_NOTICE
"scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
939 cmd
->result
= (DID_ERROR
<< 16);
943 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
946 * We use the host_scribble field as a pointer to the next command
954 * Insert the cmd into the issue queue. Note that REQUEST SENSE
955 * commands are added to the head of the queue since any command will
956 * clear the contingent allegiance condition that exists and the
957 * sense data is only guaranteed to be valid while the condition exists.
960 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
961 * Otherwise a running NCR5380_main may steal the lock.
962 * Lock before actually inserting due to fairness reasons explained in
963 * atari_scsi.c. If we insert first, then it's impossible for this driver
964 * to release the lock.
965 * Stop timer for this command while waiting for the lock, or timeouts
966 * may happen (and they really do), and it's no good if the command doesn't
967 * appear in any of the queues.
968 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
969 * because also a timer int can trigger an abort or reset, which would
970 * alter queues and touch the lock.
972 if (!NCR5380_acquire_dma_irq(instance
))
973 return SCSI_MLQUEUE_HOST_BUSY
;
975 local_irq_save(flags
);
978 * Insert the cmd into the issue queue. Note that REQUEST SENSE
979 * commands are added to the head of the queue since any command will
980 * clear the contingent allegiance condition that exists and the
981 * sense data is only guaranteed to be valid while the condition exists.
984 if (!(hostdata
->issue_queue
) || (cmd
->cmnd
[0] == REQUEST_SENSE
)) {
985 LIST(cmd
, hostdata
->issue_queue
);
986 SET_NEXT(cmd
, hostdata
->issue_queue
);
987 hostdata
->issue_queue
= cmd
;
989 for (tmp
= (struct scsi_cmnd
*)hostdata
->issue_queue
;
990 NEXT(tmp
); tmp
= NEXT(tmp
))
995 local_irq_restore(flags
);
997 dprintk(NDEBUG_QUEUES
, "scsi%d: command added to %s of queue\n", H_NO(cmd
),
998 (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
1000 /* If queue_command() is called from an interrupt (real one or bottom
1001 * half), we let queue_main() do the job of taking care about main. If it
1002 * is already running, this is a no-op, else main will be queued.
1004 * If we're not in an interrupt, we can call NCR5380_main()
1005 * unconditionally, because it cannot be already running.
1007 if (in_interrupt() || irqs_disabled())
1008 queue_main(hostdata
);
1010 NCR5380_main(&hostdata
->main_task
);
1014 static inline void maybe_release_dma_irq(struct Scsi_Host
*instance
)
1016 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1018 /* Caller does the locking needed to set & test these data atomically */
1019 if (!hostdata
->disconnected_queue
&&
1020 !hostdata
->issue_queue
&&
1021 !hostdata
->connected
&&
1022 !hostdata
->retain_dma_intr
)
1023 NCR5380_release_dma_irq(instance
);
1027 * NCR5380_main - NCR state machines
1029 * NCR5380_main is a coroutine that runs as long as more work can
1030 * be done on the NCR5380 host adapters in a system. Both
1031 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1032 * in case it is not running.
1034 * Locks: called as its own thread with no locks held.
1037 static void NCR5380_main(struct work_struct
*work
)
1039 struct NCR5380_hostdata
*hostdata
=
1040 container_of(work
, struct NCR5380_hostdata
, main_task
);
1041 struct Scsi_Host
*instance
= hostdata
->host
;
1042 struct scsi_cmnd
*tmp
, *prev
;
1044 unsigned long flags
;
1047 * We run (with interrupts disabled) until we're sure that none of
1048 * the host adapters have anything that can be done, at which point
1049 * we set main_running to 0 and exit.
1051 * Interrupts are enabled before doing various other internal
1052 * instructions, after we've decided that we need to run through
1055 * this should prevent any race conditions.
1057 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1058 * because also a timer int can trigger an abort or reset, which can
1059 * alter queues and touch the Falcon lock.
1062 /* Tell int handlers main() is now already executing. Note that
1063 no races are possible here. If an int comes in before
1064 'main_running' is set here, and queues/executes main via the
1065 task queue, it doesn't do any harm, just this instance of main
1066 won't find any work left to do. */
1067 if (hostdata
->main_running
)
1069 hostdata
->main_running
= 1;
1071 local_save_flags(flags
);
1073 local_irq_disable(); /* Freeze request queues */
1076 if (!hostdata
->connected
) {
1077 dprintk(NDEBUG_MAIN
, "scsi%d: not connected\n", HOSTNO
);
1079 * Search through the issue_queue for a command destined
1080 * for a target that's not busy.
1082 #if (NDEBUG & NDEBUG_LISTS)
1083 for (tmp
= (struct scsi_cmnd
*) hostdata
->issue_queue
, prev
= NULL
;
1084 tmp
&& (tmp
!= prev
); prev
= tmp
, tmp
= NEXT(tmp
))
1086 /*printk("%p ", tmp);*/
1087 if ((tmp
== prev
) && tmp
)
1089 /* else printk("\n"); */
1091 for (tmp
= (struct scsi_cmnd
*) hostdata
->issue_queue
,
1092 prev
= NULL
; tmp
; prev
= tmp
, tmp
= NEXT(tmp
)) {
1093 u8 lun
= tmp
->device
->lun
;
1095 dprintk(NDEBUG_LISTS
,
1096 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1097 tmp
, scmd_id(tmp
), hostdata
->busy
[scmd_id(tmp
)],
1099 /* When we find one, remove it from the issue queue. */
1100 /* ++guenther: possible race with Falcon locking */
1103 !is_lun_busy( tmp
, tmp
->cmnd
[0] != REQUEST_SENSE
)
1105 !(hostdata
->busy
[tmp
->device
->id
] & (1 << lun
))
1108 /* ++guenther: just to be sure, this must be atomic */
1109 local_irq_disable();
1111 REMOVE(prev
, NEXT(prev
), tmp
, NEXT(tmp
));
1112 SET_NEXT(prev
, NEXT(tmp
));
1114 REMOVE(-1, hostdata
->issue_queue
, tmp
, NEXT(tmp
));
1115 hostdata
->issue_queue
= NEXT(tmp
);
1117 SET_NEXT(tmp
, NULL
);
1118 hostdata
->retain_dma_intr
++;
1120 /* reenable interrupts after finding one */
1121 local_irq_restore(flags
);
1124 * Attempt to establish an I_T_L nexus here.
1125 * On success, instance->hostdata->connected is set.
1126 * On failure, we must add the command back to the
1127 * issue queue so we can keep trying.
1129 dprintk(NDEBUG_MAIN
, "scsi%d: main(): command for target %d "
1130 "lun %d removed from issue_queue\n",
1131 HOSTNO
, tmp
->device
->id
, lun
);
1133 * REQUEST SENSE commands are issued without tagged
1134 * queueing, even on SCSI-II devices because the
1135 * contingent allegiance condition exists for the
1138 /* ++roman: ...and the standard also requires that
1139 * REQUEST SENSE command are untagged.
1143 cmd_get_tag(tmp
, tmp
->cmnd
[0] != REQUEST_SENSE
);
1145 if (!NCR5380_select(instance
, tmp
)) {
1146 /* OK or bad target */
1147 local_irq_disable();
1148 hostdata
->retain_dma_intr
--;
1149 maybe_release_dma_irq(instance
);
1150 local_irq_restore(flags
);
1153 local_irq_disable();
1154 LIST(tmp
, hostdata
->issue_queue
);
1155 SET_NEXT(tmp
, hostdata
->issue_queue
);
1156 hostdata
->issue_queue
= tmp
;
1160 hostdata
->retain_dma_intr
--;
1161 local_irq_restore(flags
);
1163 dprintk(NDEBUG_MAIN
, "scsi%d: main(): select() failed, "
1164 "returned to issue_queue\n", HOSTNO
);
1166 if (hostdata
->connected
)
1168 } /* if target/lun/target queue is not busy */
1169 } /* for issue_queue */
1170 } /* if (!hostdata->connected) */
1172 if (hostdata
->connected
1174 && !hostdata
->dma_len
1177 local_irq_restore(flags
);
1178 dprintk(NDEBUG_MAIN
, "scsi%d: main: performing information transfer\n",
1180 NCR5380_information_transfer(instance
);
1181 dprintk(NDEBUG_MAIN
, "scsi%d: main: done set false\n", HOSTNO
);
1186 /* Better allow ints _after_ 'main_running' has been cleared, else
1187 an interrupt could believe we'll pick up the work it left for
1188 us, but we won't see it anymore here... */
1189 hostdata
->main_running
= 0;
1190 local_irq_restore(flags
);
1196 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1198 * Purpose : Called by interrupt handler when DMA finishes or a phase
1199 * mismatch occurs (which would finish the DMA transfer).
1201 * Inputs : instance - this instance of the NCR5380.
1205 static void NCR5380_dma_complete(struct Scsi_Host
*instance
)
1207 SETUP_HOSTDATA(instance
);
1209 unsigned char **data
;
1210 volatile int *count
;
1211 int saved_data
= 0, overrun
= 0;
1214 if (!hostdata
->connected
) {
1215 printk(KERN_WARNING
"scsi%d: received end of DMA interrupt with "
1216 "no connected cmd\n", HOSTNO
);
1220 if (hostdata
->read_overruns
) {
1221 p
= hostdata
->connected
->SCp
.phase
;
1224 if ((NCR5380_read(BUS_AND_STATUS_REG
) &
1225 (BASR_PHASE_MATCH
|BASR_ACK
)) ==
1226 (BASR_PHASE_MATCH
|BASR_ACK
)) {
1227 saved_data
= NCR5380_read(INPUT_DATA_REG
);
1229 dprintk(NDEBUG_DMA
, "scsi%d: read overrun handled\n", HOSTNO
);
1234 dprintk(NDEBUG_DMA
, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1235 HOSTNO
, NCR5380_read(BUS_AND_STATUS_REG
),
1236 NCR5380_read(STATUS_REG
));
1238 #if defined(CONFIG_SUN3)
1239 if ((sun3scsi_dma_finish(rq_data_dir(hostdata
->connected
->request
)))) {
1240 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1245 /* make sure we're not stuck in a data phase */
1246 if ((NCR5380_read(BUS_AND_STATUS_REG
) & (BASR_PHASE_MATCH
| BASR_ACK
)) ==
1247 (BASR_PHASE_MATCH
| BASR_ACK
)) {
1248 pr_err("scsi%d: BASR %02x\n", instance
->host_no
,
1249 NCR5380_read(BUS_AND_STATUS_REG
));
1250 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1256 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1257 NCR5380_write(MODE_REG
, MR_BASE
);
1258 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1260 transferred
= hostdata
->dma_len
- NCR5380_dma_residual(instance
);
1261 hostdata
->dma_len
= 0;
1263 data
= (unsigned char **)&hostdata
->connected
->SCp
.ptr
;
1264 count
= &hostdata
->connected
->SCp
.this_residual
;
1265 *data
+= transferred
;
1266 *count
-= transferred
;
1268 if (hostdata
->read_overruns
) {
1271 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) == p
&& (p
& SR_IO
)) {
1272 cnt
= toPIO
= hostdata
->read_overruns
;
1274 dprintk(NDEBUG_DMA
, "Got an input overrun, using saved byte\n");
1275 *(*data
)++ = saved_data
;
1280 dprintk(NDEBUG_DMA
, "Doing %d-byte PIO to 0x%08lx\n", cnt
, (long)*data
);
1281 NCR5380_transfer_pio(instance
, &p
, &cnt
, data
);
1282 *count
-= toPIO
- cnt
;
1286 #endif /* REAL_DMA */
1290 * NCR5380_intr - generic NCR5380 irq handler
1291 * @irq: interrupt number
1292 * @dev_id: device info
1294 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1295 * from the disconnected queue, and restarting NCR5380_main()
1299 static irqreturn_t
NCR5380_intr(int irq
, void *dev_id
)
1301 struct Scsi_Host
*instance
= dev_id
;
1302 int done
= 1, handled
= 0;
1305 dprintk(NDEBUG_INTR
, "scsi%d: NCR5380 irq triggered\n", HOSTNO
);
1307 /* Look for pending interrupts */
1308 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
1309 dprintk(NDEBUG_INTR
, "scsi%d: BASR=%02x\n", HOSTNO
, basr
);
1310 /* dispatch to appropriate routine if found and done=0 */
1311 if (basr
& BASR_IRQ
) {
1312 NCR5380_dprint(NDEBUG_INTR
, instance
);
1313 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
|SR_IO
)) == (SR_SEL
|SR_IO
)) {
1315 dprintk(NDEBUG_INTR
, "scsi%d: SEL interrupt\n", HOSTNO
);
1316 NCR5380_reselect(instance
);
1317 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1318 } else if (basr
& BASR_PARITY_ERROR
) {
1319 dprintk(NDEBUG_INTR
, "scsi%d: PARITY interrupt\n", HOSTNO
);
1320 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1321 } else if ((NCR5380_read(STATUS_REG
) & SR_RST
) == SR_RST
) {
1322 dprintk(NDEBUG_INTR
, "scsi%d: RESET interrupt\n", HOSTNO
);
1323 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1326 * The rest of the interrupt conditions can occur only during a
1330 #if defined(REAL_DMA)
1332 * We should only get PHASE MISMATCH and EOP interrupts if we have
1333 * DMA enabled, so do a sanity check based on the current setting
1334 * of the MODE register.
1337 if ((NCR5380_read(MODE_REG
) & MR_DMA_MODE
) &&
1338 ((basr
& BASR_END_DMA_TRANSFER
) ||
1339 !(basr
& BASR_PHASE_MATCH
))) {
1341 dprintk(NDEBUG_INTR
, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO
);
1342 NCR5380_dma_complete( instance
);
1345 #endif /* REAL_DMA */
1347 /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1348 if (basr
& BASR_PHASE_MATCH
)
1349 dprintk(NDEBUG_INTR
, "scsi%d: unknown interrupt, "
1350 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1351 HOSTNO
, basr
, NCR5380_read(MODE_REG
),
1352 NCR5380_read(STATUS_REG
));
1353 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1354 #ifdef SUN3_SCSI_VME
1355 dregs
->csr
|= CSR_DMA_ENABLE
;
1358 } /* if !(SELECTION || PARITY) */
1360 } /* BASR & IRQ */ else {
1361 printk(KERN_NOTICE
"scsi%d: interrupt without IRQ bit set in BASR, "
1362 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO
, basr
,
1363 NCR5380_read(MODE_REG
), NCR5380_read(STATUS_REG
));
1364 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1365 #ifdef SUN3_SCSI_VME
1366 dregs
->csr
|= CSR_DMA_ENABLE
;
1371 dprintk(NDEBUG_INTR
, "scsi%d: in int routine, calling main\n", HOSTNO
);
1372 /* Put a call to NCR5380_main() on the queue... */
1373 queue_main(shost_priv(instance
));
1375 return IRQ_RETVAL(handled
);
1379 * Function : int NCR5380_select(struct Scsi_Host *instance,
1380 * struct scsi_cmnd *cmd)
1382 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1383 * including ARBITRATION, SELECTION, and initial message out for
1384 * IDENTIFY and queue messages.
1386 * Inputs : instance - instantiation of the 5380 driver on which this
1387 * target lives, cmd - SCSI command to execute.
1389 * Returns : -1 if selection failed but should be retried.
1390 * 0 if selection failed and should not be retried.
1391 * 0 if selection succeeded completely (hostdata->connected == cmd).
1394 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1395 * with registers as they should have been on entry - ie
1396 * SELECT_ENABLE will be set appropriately, the NCR5380
1397 * will cease to drive any SCSI bus signals.
1399 * If successful : I_T_L or I_T_L_Q nexus will be established,
1400 * instance->connected will be set to cmd.
1401 * SELECT interrupt will be disabled.
1403 * If failed (no target) : cmd->scsi_done() will be called, and the
1404 * cmd->result host byte set to DID_BAD_TARGET.
1407 static int NCR5380_select(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
1409 SETUP_HOSTDATA(instance
);
1410 unsigned char tmp
[3], phase
;
1411 unsigned char *data
;
1414 unsigned long flags
;
1415 unsigned long timeout
;
1417 NCR5380_dprint(NDEBUG_ARBITRATION
, instance
);
1418 dprintk(NDEBUG_ARBITRATION
, "scsi%d: starting arbitration, id = %d\n", HOSTNO
,
1422 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1423 * data bus during SELECTION.
1426 local_irq_save(flags
);
1427 if (hostdata
->connected
) {
1428 local_irq_restore(flags
);
1431 NCR5380_write(TARGET_COMMAND_REG
, 0);
1434 * Start arbitration.
1437 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
1438 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
1440 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1441 * Bus Free Delay, arbitration will begin.
1444 local_irq_restore(flags
);
1445 timeout
= jiffies
+ HZ
;
1447 if (time_is_before_jiffies(timeout
)) {
1448 NCR5380_write(MODE_REG
, MR_BASE
);
1449 shost_printk(KERN_ERR
, instance
,
1450 "select: arbitration timeout\n");
1453 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
)) {
1454 /* Reselection interrupt */
1457 if (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
)
1461 /* The SCSI-2 arbitration delay is 2.4 us */
1464 /* Check for lost arbitration */
1465 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1466 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1467 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1468 hostdata
->connected
) {
1469 NCR5380_write(MODE_REG
, MR_BASE
);
1470 dprintk(NDEBUG_ARBITRATION
, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1475 /* After/during arbitration, BSY should be asserted.
1476 * IBM DPES-31080 Version S31Q works now
1477 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1479 NCR5380_write(INITIATOR_COMMAND_REG
,
1480 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1482 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1483 hostdata
->connected
) {
1484 NCR5380_write(MODE_REG
, MR_BASE
);
1485 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1486 dprintk(NDEBUG_ARBITRATION
, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1492 * Again, bus clear + bus settle time is 1.2us, however, this is
1493 * a minimum so we'll udelay ceil(1.2)
1496 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
1501 if (hostdata
->connected
) {
1502 NCR5380_write(MODE_REG
, MR_BASE
);
1503 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1507 dprintk(NDEBUG_ARBITRATION
, "scsi%d: won arbitration\n", HOSTNO
);
1510 * Now that we have won arbitration, start Selection process, asserting
1511 * the host and target ID's on the SCSI bus.
1514 NCR5380_write(OUTPUT_DATA_REG
, (hostdata
->id_mask
| (1 << cmd
->device
->id
)));
1517 * Raise ATN while SEL is true before BSY goes false from arbitration,
1518 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1519 * phase immediately after selection.
1522 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_BSY
|
1523 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1524 NCR5380_write(MODE_REG
, MR_BASE
);
1527 * Reselect interrupts must be turned off prior to the dropping of BSY,
1528 * otherwise we will trigger an interrupt.
1531 if (hostdata
->connected
) {
1532 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1536 NCR5380_write(SELECT_ENABLE_REG
, 0);
1539 * The initiator shall then wait at least two deskew delays and release
1542 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1545 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_DATA
|
1546 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1549 * Something weird happens when we cease to drive BSY - looks
1550 * like the board/chip is letting us do another read before the
1551 * appropriate propagation delay has expired, and we're confusing
1552 * a BSY signal from ourselves as the target's response to SELECTION.
1554 * A small delay (the 'C++' frontend breaks the pipeline with an
1555 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1556 * tighter 'C' code breaks and requires this) solves the problem -
1557 * the 1 us delay is arbitrary, and only used because this delay will
1558 * be the same on other platforms and since it works here, it should
1561 * wingel suggests that this could be due to failing to wait
1567 dprintk(NDEBUG_SELECTION
, "scsi%d: selecting target %d\n", HOSTNO
, cmd
->device
->id
);
1570 * The SCSI specification calls for a 250 ms timeout for the actual
1574 err
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_BSY
, SR_BSY
,
1575 msecs_to_jiffies(250));
1577 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) == (SR_SEL
| SR_IO
)) {
1578 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1579 NCR5380_reselect(instance
);
1580 printk(KERN_ERR
"scsi%d: reselection after won arbitration?\n",
1582 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1587 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1588 cmd
->result
= DID_BAD_TARGET
<< 16;
1592 cmd
->scsi_done(cmd
);
1593 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1594 dprintk(NDEBUG_SELECTION
, "scsi%d: target did not respond within 250ms\n", HOSTNO
);
1599 * No less than two deskew delays after the initiator detects the
1600 * BSY signal is true, it shall release the SEL signal and may
1601 * change the DATA BUS. -wingel
1606 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1609 * Since we followed the SCSI spec, and raised ATN while SEL
1610 * was true but before BSY was false during selection, the information
1611 * transfer phase should be a MESSAGE OUT phase so that we can send the
1614 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1615 * message (2 bytes) with a tag ID that we increment with every command
1616 * until it wraps back to 0.
1618 * XXX - it turns out that there are some broken SCSI-II devices,
1619 * which claim to support tagged queuing but fail when more than
1620 * some number of commands are issued at once.
1623 /* Wait for start of REQ/ACK handshake */
1625 err
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1627 shost_printk(KERN_ERR
, instance
, "select: REQ timeout\n");
1628 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1629 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1633 dprintk(NDEBUG_SELECTION
, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
1634 HOSTNO
, cmd
->device
->id
);
1635 tmp
[0] = IDENTIFY(1, cmd
->device
->lun
);
1638 if (cmd
->tag
!= TAG_NONE
) {
1639 tmp
[1] = hostdata
->last_message
= SIMPLE_QUEUE_TAG
;
1647 #endif /* SUPPORT_TAGS */
1649 /* Send message(s) */
1651 phase
= PHASE_MSGOUT
;
1652 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1653 dprintk(NDEBUG_SELECTION
, "scsi%d: nexus established.\n", HOSTNO
);
1654 /* XXX need to handle errors here */
1655 hostdata
->connected
= cmd
;
1656 #ifndef SUPPORT_TAGS
1657 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
1659 #ifdef SUN3_SCSI_VME
1660 dregs
->csr
|= CSR_INTR
;
1663 initialize_SCp(cmd
);
1669 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1670 * unsigned char *phase, int *count, unsigned char **data)
1672 * Purpose : transfers data in given phase using polled I/O
1674 * Inputs : instance - instance of driver, *phase - pointer to
1675 * what phase is expected, *count - pointer to number of
1676 * bytes to transfer, **data - pointer to data pointer.
1678 * Returns : -1 when different phase is entered without transferring
1679 * maximum number of bytes, 0 if all bytes are transferred or exit
1682 * Also, *phase, *count, *data are modified in place.
1684 * XXX Note : handling for bus free may be useful.
1688 * Note : this code is not as quick as it could be, however it
1689 * IS 100% reliable, and for the actual data transfer where speed
1690 * counts, we will always do a pseudo DMA or DMA transfer.
1693 static int NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1694 unsigned char *phase
, int *count
,
1695 unsigned char **data
)
1697 register unsigned char p
= *phase
, tmp
;
1698 register int c
= *count
;
1699 register unsigned char *d
= *data
;
1702 * The NCR5380 chip will only drive the SCSI bus when the
1703 * phase specified in the appropriate bits of the TARGET COMMAND
1704 * REGISTER match the STATUS REGISTER
1707 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1711 * Wait for assertion of REQ, after which the phase bits will be
1715 if (NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
) < 0)
1718 dprintk(NDEBUG_HANDSHAKE
, "scsi%d: REQ detected\n", HOSTNO
);
1720 /* Check for phase mismatch */
1721 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) != p
) {
1722 dprintk(NDEBUG_PIO
, "scsi%d: phase mismatch\n", HOSTNO
);
1723 NCR5380_dprint_phase(NDEBUG_PIO
, instance
);
1727 /* Do actual transfer from SCSI bus to / from memory */
1729 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1731 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1736 * The SCSI standard suggests that in MSGOUT phase, the initiator
1737 * should drop ATN on the last byte of the message phase
1738 * after REQ has been asserted for the handshake but before
1739 * the initiator raises ACK.
1743 if (!((p
& SR_MSG
) && c
> 1)) {
1744 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1745 NCR5380_dprint(NDEBUG_PIO
, instance
);
1746 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1747 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1749 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1750 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1751 NCR5380_dprint(NDEBUG_PIO
, instance
);
1752 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1753 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1756 NCR5380_dprint(NDEBUG_PIO
, instance
);
1757 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1760 if (NCR5380_poll_politely(instance
,
1761 STATUS_REG
, SR_REQ
, 0, 5 * HZ
) < 0)
1764 dprintk(NDEBUG_HANDSHAKE
, "scsi%d: req false, handshake complete\n", HOSTNO
);
1767 * We have several special cases to consider during REQ/ACK handshaking :
1768 * 1. We were in MSGOUT phase, and we are on the last byte of the
1769 * message. ATN must be dropped as ACK is dropped.
1771 * 2. We are in a MSGIN phase, and we are on the last byte of the
1772 * message. We must exit with ACK asserted, so that the calling
1773 * code may raise ATN before dropping ACK to reject the message.
1775 * 3. ACK and ATN are clear and the target may proceed as normal.
1777 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1778 if (p
== PHASE_MSGOUT
&& c
> 1)
1779 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1781 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1785 dprintk(NDEBUG_PIO
, "scsi%d: residual %d\n", HOSTNO
, c
);
1789 tmp
= NCR5380_read(STATUS_REG
);
1790 /* The phase read from the bus is valid if either REQ is (already)
1791 * asserted or if ACK hasn't been released yet. The latter applies if
1792 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1794 if ((tmp
& SR_REQ
) || ((tmp
& SR_IO
) && c
== 0))
1795 *phase
= tmp
& PHASE_MASK
;
1797 *phase
= PHASE_UNKNOWN
;
1799 if (!c
|| (*phase
== p
))
1806 * do_reset - issue a reset command
1807 * @instance: adapter to reset
1809 * Issue a reset sequence to the NCR5380 and try and get the bus
1810 * back into sane shape.
1812 * This clears the reset interrupt flag because there may be no handler for
1813 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1814 * been installed. And when in EH we may have released the ST DMA interrupt.
1817 static void do_reset(struct Scsi_Host
*instance
)
1819 unsigned long flags
;
1821 local_irq_save(flags
);
1822 NCR5380_write(TARGET_COMMAND_REG
,
1823 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1824 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1826 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1827 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1828 local_irq_restore(flags
);
1832 * do_abort - abort the currently established nexus by going to
1833 * MESSAGE OUT phase and sending an ABORT message.
1834 * @instance: relevant scsi host instance
1836 * Returns 0 on success, -1 on failure.
1839 static int do_abort(struct Scsi_Host
*instance
)
1841 unsigned char tmp
, *msgptr
, phase
;
1845 /* Request message out phase */
1846 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1849 * Wait for the target to indicate a valid phase by asserting
1850 * REQ. Once this happens, we'll have either a MSGOUT phase
1851 * and can immediately send the ABORT message, or we'll have some
1852 * other phase and will have to source/sink data.
1854 * We really don't care what value was on the bus or what value
1855 * the target sees, so we just handshake.
1858 rc
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, 10 * HZ
);
1862 tmp
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1864 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1866 if (tmp
!= PHASE_MSGOUT
) {
1867 NCR5380_write(INITIATOR_COMMAND_REG
,
1868 ICR_BASE
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1869 rc
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, 0, 3 * HZ
);
1872 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1878 phase
= PHASE_MSGOUT
;
1879 NCR5380_transfer_pio(instance
, &phase
, &len
, &msgptr
);
1882 * If we got here, and the command completed successfully,
1883 * we're about to go into bus free state.
1886 return len
? -1 : 0;
1889 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1893 #if defined(REAL_DMA)
1895 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1896 * unsigned char *phase, int *count, unsigned char **data)
1898 * Purpose : transfers data in given phase using either real
1901 * Inputs : instance - instance of driver, *phase - pointer to
1902 * what phase is expected, *count - pointer to number of
1903 * bytes to transfer, **data - pointer to data pointer.
1905 * Returns : -1 when different phase is entered without transferring
1906 * maximum number of bytes, 0 if all bytes or transferred or exit
1909 * Also, *phase, *count, *data are modified in place.
1914 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1915 unsigned char *phase
, int *count
,
1916 unsigned char **data
)
1918 SETUP_HOSTDATA(instance
);
1919 register int c
= *count
;
1920 register unsigned char p
= *phase
;
1921 unsigned long flags
;
1923 #if defined(CONFIG_SUN3)
1925 if (!sun3_dma_setup_done
) {
1926 pr_err("scsi%d: transfer_dma without setup!\n",
1930 hostdata
->dma_len
= c
;
1932 dprintk(NDEBUG_DMA
, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1933 instance
->host_no
, (p
& SR_IO
) ? "reading" : "writing",
1934 c
, (p
& SR_IO
) ? "to" : "from", *data
);
1936 /* netbsd turns off ints here, why not be safe and do it too */
1937 local_irq_save(flags
);
1939 /* send start chain */
1940 sun3scsi_dma_start(c
, *data
);
1943 NCR5380_write(TARGET_COMMAND_REG
, 1);
1944 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1945 NCR5380_write(INITIATOR_COMMAND_REG
, 0);
1946 NCR5380_write(MODE_REG
,
1947 (NCR5380_read(MODE_REG
) | MR_DMA_MODE
| MR_ENABLE_EOP_INTR
));
1948 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1950 NCR5380_write(TARGET_COMMAND_REG
, 0);
1951 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1952 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_ASSERT_DATA
);
1953 NCR5380_write(MODE_REG
,
1954 (NCR5380_read(MODE_REG
) | MR_DMA_MODE
| MR_ENABLE_EOP_INTR
));
1955 NCR5380_write(START_DMA_SEND_REG
, 0);
1958 #ifdef SUN3_SCSI_VME
1959 dregs
->csr
|= CSR_DMA_ENABLE
;
1962 local_irq_restore(flags
);
1964 sun3_dma_active
= 1;
1966 #else /* !defined(CONFIG_SUN3) */
1967 register unsigned char *d
= *data
;
1970 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1975 if (hostdata
->read_overruns
&& (p
& SR_IO
))
1976 c
-= hostdata
->read_overruns
;
1978 dprintk(NDEBUG_DMA
, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1979 HOSTNO
, (p
& SR_IO
) ? "reading" : "writing",
1980 c
, (p
& SR_IO
) ? "to" : "from", d
);
1982 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1985 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_ENABLE_EOP_INTR
| MR_MONITOR_BSY
);
1986 #endif /* def REAL_DMA */
1988 if (!(hostdata
->flags
& FLAG_LATE_DMA_SETUP
)) {
1989 /* On the Medusa, it is a must to initialize the DMA before
1990 * starting the NCR. This is also the cleaner way for the TT.
1992 local_irq_save(flags
);
1993 hostdata
->dma_len
= (p
& SR_IO
) ?
1994 NCR5380_dma_read_setup(instance
, d
, c
) :
1995 NCR5380_dma_write_setup(instance
, d
, c
);
1996 local_irq_restore(flags
);
2000 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
2002 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
2003 NCR5380_write(START_DMA_SEND_REG
, 0);
2006 if (hostdata
->flags
& FLAG_LATE_DMA_SETUP
) {
2007 /* On the Falcon, the DMA setup must be done after the last */
2008 /* NCR access, else the DMA setup gets trashed!
2010 local_irq_save(flags
);
2011 hostdata
->dma_len
= (p
& SR_IO
) ?
2012 NCR5380_dma_read_setup(instance
, d
, c
) :
2013 NCR5380_dma_write_setup(instance
, d
, c
);
2014 local_irq_restore(flags
);
2016 #endif /* !defined(CONFIG_SUN3) */
2020 #endif /* defined(REAL_DMA) */
2023 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2025 * Purpose : run through the various SCSI phases and do as the target
2026 * directs us to. Operates on the currently connected command,
2027 * instance->connected.
2029 * Inputs : instance, instance for which we are doing commands
2031 * Side effects : SCSI things happen, the disconnected queue will be
2032 * modified if a command disconnects, *instance->connected will
2035 * XXX Note : we need to watch for bus free or a reset condition here
2036 * to recover from an unexpected bus free condition.
2039 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
2041 SETUP_HOSTDATA(instance
);
2042 unsigned long flags
;
2043 unsigned char msgout
= NOP
;
2046 #if defined(REAL_DMA)
2049 unsigned char *data
;
2050 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
2051 struct scsi_cmnd
*cmd
= (struct scsi_cmnd
*) hostdata
->connected
;
2053 #ifdef SUN3_SCSI_VME
2054 dregs
->csr
|= CSR_INTR
;
2058 tmp
= NCR5380_read(STATUS_REG
);
2059 /* We only have a valid SCSI phase when REQ is asserted */
2061 phase
= (tmp
& PHASE_MASK
);
2062 if (phase
!= old_phase
) {
2064 NCR5380_dprint_phase(NDEBUG_INFORMATION
, instance
);
2066 #if defined(CONFIG_SUN3)
2067 if (phase
== PHASE_CMDOUT
) {
2068 #if defined(REAL_DMA)
2070 unsigned long count
;
2072 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
2073 count
= cmd
->SCp
.buffer
->length
;
2074 d
= sg_virt(cmd
->SCp
.buffer
);
2076 count
= cmd
->SCp
.this_residual
;
2079 /* this command setup for dma yet? */
2080 if ((count
>= DMA_MIN_SIZE
) && (sun3_dma_setup_done
!= cmd
)) {
2081 if (cmd
->request
->cmd_type
== REQ_TYPE_FS
) {
2082 sun3scsi_dma_setup(d
, count
,
2083 rq_data_dir(cmd
->request
));
2084 sun3_dma_setup_done
= cmd
;
2088 #ifdef SUN3_SCSI_VME
2089 dregs
->csr
|= CSR_INTR
;
2092 #endif /* CONFIG_SUN3 */
2094 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
2095 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
2097 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
2099 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
2101 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
2109 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2110 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2111 "aborted\n", HOSTNO
);
2114 cmd
->result
= DID_ERROR
<< 16;
2115 cmd
->scsi_done(cmd
);
2120 * If there is no room left in the current buffer in the
2121 * scatter-gather list, move onto the next one.
2124 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
2126 --cmd
->SCp
.buffers_residual
;
2127 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
2128 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
2129 /* ++roman: Try to merge some scatter-buffers if
2130 * they are at contiguous physical addresses.
2132 merge_contiguous_buffers(cmd
);
2133 dprintk(NDEBUG_INFORMATION
, "scsi%d: %d bytes and %d buffers left\n",
2134 HOSTNO
, cmd
->SCp
.this_residual
,
2135 cmd
->SCp
.buffers_residual
);
2139 * The preferred transfer method is going to be
2140 * PSEUDO-DMA for systems that are strictly PIO,
2141 * since we can let the hardware do the handshaking.
2143 * For this to work, we need to know the transfersize
2144 * ahead of time, since the pseudo-DMA code will sit
2145 * in an unconditional loop.
2148 /* ++roman: I suggest, this should be
2150 * instead of leaving REAL_DMA out.
2153 #if defined(REAL_DMA)
2154 #if !defined(CONFIG_SUN3)
2156 if (!cmd
->device
->borken
)
2158 transfersize
= NCR5380_dma_xfer_len(instance
, cmd
, phase
);
2160 if (transfersize
>= DMA_MIN_SIZE
) {
2162 cmd
->SCp
.phase
= phase
;
2163 if (NCR5380_transfer_dma(instance
, &phase
,
2164 &len
, (unsigned char **)&cmd
->SCp
.ptr
)) {
2166 * If the watchdog timer fires, all future
2167 * accesses to this device will use the
2169 scmd_printk(KERN_INFO
, cmd
,
2170 "switching to slow handshake\n");
2171 cmd
->device
->borken
= 1;
2174 cmd
->result
= DID_ERROR
<< 16;
2175 cmd
->scsi_done(cmd
);
2176 /* XXX - need to source or sink data here, as appropriate */
2179 /* ++roman: When using real DMA,
2180 * information_transfer() should return after
2181 * starting DMA since it has nothing more to
2186 cmd
->SCp
.this_residual
-= transfersize
- len
;
2190 #endif /* defined(REAL_DMA) */
2191 NCR5380_transfer_pio(instance
, &phase
,
2192 (int *)&cmd
->SCp
.this_residual
,
2193 (unsigned char **)&cmd
->SCp
.ptr
);
2194 #if defined(CONFIG_SUN3) && defined(REAL_DMA)
2195 /* if we had intended to dma that command clear it */
2196 if (sun3_dma_setup_done
== cmd
)
2197 sun3_dma_setup_done
= NULL
;
2203 NCR5380_write(SELECT_ENABLE_REG
, 0); /* disable reselects */
2204 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2205 cmd
->SCp
.Message
= tmp
;
2209 case COMMAND_COMPLETE
:
2210 /* Accept message by clearing ACK */
2211 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2212 dprintk(NDEBUG_QUEUES
, "scsi%d: command for target %d, lun %llu "
2213 "completed\n", HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2215 local_irq_save(flags
);
2216 hostdata
->retain_dma_intr
++;
2217 hostdata
->connected
= NULL
;
2220 if (status_byte(cmd
->SCp
.Status
) == QUEUE_FULL
) {
2221 /* Turn a QUEUE FULL status into BUSY, I think the
2222 * mid level cannot handle QUEUE FULL :-( (The
2223 * command is retried after BUSY). Also update our
2224 * queue size to the number of currently issued
2227 /* ++Andreas: the mid level code knows about
2229 struct tag_alloc
*ta
= &hostdata
->TagAlloc
[scmd_id(cmd
)][cmd
->device
->lun
];
2230 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %llu returned "
2231 "QUEUE_FULL after %d commands\n",
2232 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
,
2234 if (ta
->queue_size
> ta
->nr_allocated
)
2235 ta
->nr_allocated
= ta
->queue_size
;
2238 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2240 /* Enable reselect interrupts */
2241 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2244 * I'm not sure what the correct thing to do here is :
2246 * If the command that just executed is NOT a request
2247 * sense, the obvious thing to do is to set the result
2248 * code to the values of the stored parameters.
2250 * If it was a REQUEST SENSE command, we need some way to
2251 * differentiate between the failure code of the original
2252 * and the failure code of the REQUEST sense - the obvious
2253 * case is success, where we fall through and leave the
2254 * result code unchanged.
2256 * The non-obvious place is where the REQUEST SENSE failed
2259 if (cmd
->cmnd
[0] != REQUEST_SENSE
)
2260 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
2261 else if (status_byte(cmd
->SCp
.Status
) != GOOD
)
2262 cmd
->result
= (cmd
->result
& 0x00ffff) | (DID_ERROR
<< 16);
2264 if ((cmd
->cmnd
[0] == REQUEST_SENSE
) &&
2265 hostdata
->ses
.cmd_len
) {
2266 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
2267 hostdata
->ses
.cmd_len
= 0 ;
2270 if ((cmd
->cmnd
[0] != REQUEST_SENSE
) &&
2271 (status_byte(cmd
->SCp
.Status
) == CHECK_CONDITION
)) {
2272 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
2274 dprintk(NDEBUG_AUTOSENSE
, "scsi%d: performing request sense\n", HOSTNO
);
2276 LIST(cmd
,hostdata
->issue_queue
);
2277 SET_NEXT(cmd
, hostdata
->issue_queue
);
2278 hostdata
->issue_queue
= (struct scsi_cmnd
*) cmd
;
2279 dprintk(NDEBUG_QUEUES
, "scsi%d: REQUEST SENSE added to head of "
2280 "issue queue\n", H_NO(cmd
));
2282 cmd
->scsi_done(cmd
);
2285 local_irq_restore(flags
);
2287 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2289 * Restore phase bits to 0 so an interrupted selection,
2290 * arbitration can resume.
2292 NCR5380_write(TARGET_COMMAND_REG
, 0);
2294 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2297 local_irq_save(flags
);
2298 hostdata
->retain_dma_intr
--;
2299 /* ++roman: For Falcon SCSI, release the lock on the
2300 * ST-DMA here if no other commands are waiting on the
2301 * disconnected queue.
2303 maybe_release_dma_irq(instance
);
2304 local_irq_restore(flags
);
2306 case MESSAGE_REJECT
:
2307 /* Accept message by clearing ACK */
2308 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2309 /* Enable reselect interrupts */
2310 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2311 switch (hostdata
->last_message
) {
2312 case HEAD_OF_QUEUE_TAG
:
2313 case ORDERED_QUEUE_TAG
:
2314 case SIMPLE_QUEUE_TAG
:
2315 /* The target obviously doesn't support tagged
2316 * queuing, even though it announced this ability in
2317 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2318 * clear 'tagged_supported' and lock the LUN, since
2319 * the command is treated as untagged further on.
2321 cmd
->device
->tagged_supported
= 0;
2322 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
2323 cmd
->tag
= TAG_NONE
;
2324 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %llu rejected "
2325 "QUEUE_TAG message; tagged queuing "
2327 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2332 /* Accept message by clearing ACK */
2333 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2334 local_irq_save(flags
);
2335 LIST(cmd
,hostdata
->disconnected_queue
);
2336 SET_NEXT(cmd
, hostdata
->disconnected_queue
);
2337 hostdata
->connected
= NULL
;
2338 hostdata
->disconnected_queue
= cmd
;
2339 local_irq_restore(flags
);
2340 dprintk(NDEBUG_QUEUES
, "scsi%d: command for target %d lun %llu was "
2341 "moved from connected to the "
2342 "disconnected_queue\n", HOSTNO
,
2343 cmd
->device
->id
, cmd
->device
->lun
);
2345 * Restore phase bits to 0 so an interrupted selection,
2346 * arbitration can resume.
2348 NCR5380_write(TARGET_COMMAND_REG
, 0);
2350 /* Enable reselect interrupts */
2351 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2352 /* Wait for bus free to avoid nasty timeouts */
2353 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2355 #ifdef SUN3_SCSI_VME
2356 dregs
->csr
|= CSR_DMA_ENABLE
;
2360 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2361 * operation, in violation of the SCSI spec so we can safely
2362 * ignore SAVE/RESTORE pointers calls.
2364 * Unfortunately, some disks violate the SCSI spec and
2365 * don't issue the required SAVE_POINTERS message before
2366 * disconnecting, and we have to break spec to remain
2370 case RESTORE_POINTERS
:
2371 /* Accept message by clearing ACK */
2372 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2373 /* Enable reselect interrupts */
2374 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2376 case EXTENDED_MESSAGE
:
2378 * Extended messages are sent in the following format :
2380 * 0 EXTENDED_MESSAGE == 1
2381 * 1 length (includes one byte for code, doesn't
2382 * include first two bytes)
2384 * 3..length+1 arguments
2386 * Start the extended message buffer with the EXTENDED_MESSAGE
2387 * byte, since spi_print_msg() wants the whole thing.
2389 extended_msg
[0] = EXTENDED_MESSAGE
;
2390 /* Accept first byte by clearing ACK */
2391 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2393 dprintk(NDEBUG_EXTENDED
, "scsi%d: receiving extended message\n", HOSTNO
);
2396 data
= extended_msg
+ 1;
2397 phase
= PHASE_MSGIN
;
2398 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2399 dprintk(NDEBUG_EXTENDED
, "scsi%d: length=%d, code=0x%02x\n", HOSTNO
,
2400 (int)extended_msg
[1], (int)extended_msg
[2]);
2402 if (!len
&& extended_msg
[1] <=
2403 (sizeof(extended_msg
) - 1)) {
2404 /* Accept third byte by clearing ACK */
2405 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2406 len
= extended_msg
[1] - 1;
2407 data
= extended_msg
+ 3;
2408 phase
= PHASE_MSGIN
;
2410 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2411 dprintk(NDEBUG_EXTENDED
, "scsi%d: message received, residual %d\n",
2414 switch (extended_msg
[2]) {
2417 case EXTENDED_MODIFY_DATA_POINTER
:
2418 case EXTENDED_EXTENDED_IDENTIFY
:
2422 printk(KERN_NOTICE
"scsi%d: error receiving "
2423 "extended message\n", HOSTNO
);
2426 printk(KERN_NOTICE
"scsi%d: extended message "
2427 "code %02x length %d is too long\n",
2428 HOSTNO
, extended_msg
[2], extended_msg
[1]);
2431 /* Fall through to reject message */
2434 * If we get something weird that we aren't expecting,
2439 printk(KERN_INFO
"scsi%d: rejecting message ",
2441 spi_print_msg(extended_msg
);
2443 } else if (tmp
!= EXTENDED_MESSAGE
)
2444 scmd_printk(KERN_INFO
, cmd
,
2445 "rejecting unknown message %02x\n",
2448 scmd_printk(KERN_INFO
, cmd
,
2449 "rejecting unknown extended message code %02x, length %d\n",
2450 extended_msg
[1], extended_msg
[0]);
2452 msgout
= MESSAGE_REJECT
;
2453 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
2455 } /* switch (tmp) */
2460 hostdata
->last_message
= msgout
;
2461 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2462 if (msgout
== ABORT
) {
2463 local_irq_save(flags
);
2467 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2469 hostdata
->connected
= NULL
;
2470 cmd
->result
= DID_ERROR
<< 16;
2471 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2472 maybe_release_dma_irq(instance
);
2473 local_irq_restore(flags
);
2474 cmd
->scsi_done(cmd
);
2483 * XXX for performance reasons, on machines with a
2484 * PSEUDO-DMA architecture we should probably
2485 * use the dma transfer function.
2487 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2492 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2493 cmd
->SCp
.Status
= tmp
;
2496 printk("scsi%d: unknown phase\n", HOSTNO
);
2497 NCR5380_dprint(NDEBUG_ANY
, instance
);
2498 } /* switch(phase) */
2500 NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
2506 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2508 * Purpose : does reselection, initializing the instance->connected
2509 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2510 * nexus has been reestablished,
2512 * Inputs : instance - this instance of the NCR5380.
2517 /* it might eventually prove necessary to do a dma setup on
2518 reselection, but it doesn't seem to be needed now -- sam */
2520 static void NCR5380_reselect(struct Scsi_Host
*instance
)
2522 SETUP_HOSTDATA(instance
);
2523 unsigned char target_mask
;
2528 unsigned char msg
[3];
2529 int __maybe_unused len
;
2530 unsigned char __maybe_unused
*data
, __maybe_unused phase
;
2531 struct scsi_cmnd
*tmp
= NULL
, *prev
;
2534 * Disable arbitration, etc. since the host adapter obviously
2535 * lost, and tell an interrupted NCR5380_select() to restart.
2538 NCR5380_write(MODE_REG
, MR_BASE
);
2540 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2542 dprintk(NDEBUG_RESELECTION
, "scsi%d: reselect\n", HOSTNO
);
2545 * At this point, we have detected that our SCSI ID is on the bus,
2546 * SEL is true and BSY was false for at least one bus settle delay
2549 * We must assert BSY ourselves, until the target drops the SEL
2553 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2555 while (NCR5380_read(STATUS_REG
) & SR_SEL
)
2557 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2560 * Wait for target to go into MSGIN.
2563 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
))
2566 #if defined(CONFIG_SUN3) && defined(REAL_DMA)
2567 /* acknowledge toggle to MSGIN */
2568 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(PHASE_MSGIN
));
2570 /* peek at the byte without really hitting the bus */
2571 msg
[0] = NCR5380_read(CURRENT_SCSI_DATA_REG
);
2575 phase
= PHASE_MSGIN
;
2576 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2579 if (!(msg
[0] & 0x80)) {
2580 printk(KERN_DEBUG
"scsi%d: expecting IDENTIFY message, got ", HOSTNO
);
2585 lun
= (msg
[0] & 0x07);
2587 #if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
2588 /* If the phase is still MSGIN, the target wants to send some more
2589 * messages. In case it supports tagged queuing, this is probably a
2590 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2593 if (phase
== PHASE_MSGIN
&& (hostdata
->flags
& FLAG_TAGGED_QUEUING
)) {
2594 /* Accept previous IDENTIFY message by clearing ACK */
2595 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2598 if (!NCR5380_transfer_pio(instance
, &phase
, &len
, &data
) &&
2599 msg
[1] == SIMPLE_QUEUE_TAG
)
2601 dprintk(NDEBUG_TAGS
, "scsi%d: target mask %02x, lun %d sent tag %d at "
2602 "reselection\n", HOSTNO
, target_mask
, lun
, tag
);
2607 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2608 * just reestablished, and remove it from the disconnected queue.
2611 for (tmp
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
, prev
= NULL
;
2612 tmp
; prev
= tmp
, tmp
= NEXT(tmp
)) {
2613 if ((target_mask
== (1 << tmp
->device
->id
)) && (lun
== tmp
->device
->lun
)
2615 && (tag
== tmp
->tag
)
2619 REMOVE(prev
, NEXT(prev
), tmp
, NEXT(tmp
));
2620 SET_NEXT(prev
, NEXT(tmp
));
2622 REMOVE(-1, hostdata
->disconnected_queue
, tmp
, NEXT(tmp
));
2623 hostdata
->disconnected_queue
= NEXT(tmp
);
2625 SET_NEXT(tmp
, NULL
);
2631 printk(KERN_WARNING
"scsi%d: warning: target bitmask %02x lun %d "
2635 "not in disconnected_queue.\n",
2636 HOSTNO
, target_mask
, lun
2642 * Since we have an established nexus that we can't do anything
2643 * with, we must abort it.
2649 #if defined(CONFIG_SUN3) && defined(REAL_DMA)
2650 /* engage dma setup for the command we just saw */
2653 unsigned long count
;
2655 if (!tmp
->SCp
.this_residual
&& tmp
->SCp
.buffers_residual
) {
2656 count
= tmp
->SCp
.buffer
->length
;
2657 d
= sg_virt(tmp
->SCp
.buffer
);
2659 count
= tmp
->SCp
.this_residual
;
2662 /* setup this command for dma if not already */
2663 if ((count
>= DMA_MIN_SIZE
) && (sun3_dma_setup_done
!= tmp
)) {
2664 sun3scsi_dma_setup(d
, count
, rq_data_dir(tmp
->request
));
2665 sun3_dma_setup_done
= tmp
;
2669 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
2672 /* Accept message by clearing ACK */
2673 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2675 #if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2676 /* If the phase is still MSGIN, the target wants to send some more
2677 * messages. In case it supports tagged queuing, this is probably a
2678 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2681 if (phase
== PHASE_MSGIN
&& setup_use_tagged_queuing
) {
2682 /* Accept previous IDENTIFY message by clearing ACK */
2683 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2686 if (!NCR5380_transfer_pio(instance
, &phase
, &len
, &data
) &&
2687 msg
[1] == SIMPLE_QUEUE_TAG
)
2689 dprintk(NDEBUG_TAGS
, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2690 HOSTNO
, target_mask
, lun
, tag
);
2694 hostdata
->connected
= tmp
;
2695 dprintk(NDEBUG_RESELECTION
, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
2696 HOSTNO
, tmp
->device
->id
, tmp
->device
->lun
, tmp
->tag
);
2701 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
2703 * Purpose : abort a command
2705 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
2706 * host byte of the result field to, if zero DID_ABORTED is
2709 * Returns : SUCCESS - success, FAILED on failure.
2711 * XXX - there is no way to abort the command that is currently
2712 * connected, you have to wait for it to complete. If this is
2713 * a problem, we could implement longjmp() / setjmp(), setjmp()
2714 * called where the loop started in NCR5380_main().
2718 int NCR5380_abort(struct scsi_cmnd
*cmd
)
2720 struct Scsi_Host
*instance
= cmd
->device
->host
;
2721 SETUP_HOSTDATA(instance
);
2722 struct scsi_cmnd
*tmp
, **prev
;
2723 unsigned long flags
;
2725 scmd_printk(KERN_NOTICE
, cmd
, "aborting command\n");
2727 NCR5380_print_status(instance
);
2729 local_irq_save(flags
);
2731 dprintk(NDEBUG_ABORT
, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO
,
2732 NCR5380_read(BUS_AND_STATUS_REG
),
2733 NCR5380_read(STATUS_REG
));
2737 * Case 1 : If the command is the currently executing command,
2738 * we'll set the aborted flag and return control so that
2739 * information transfer routine can exit cleanly.
2742 if (hostdata
->connected
== cmd
) {
2744 dprintk(NDEBUG_ABORT
, "scsi%d: aborting connected command\n", HOSTNO
);
2746 * We should perform BSY checking, and make sure we haven't slipped
2750 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2752 * Since we can't change phases until we've completed the current
2753 * handshake, we have to source or sink a byte of data if the current
2754 * phase is not MSGOUT.
2758 * Return control to the executing NCR drive so we can clear the
2759 * aborted flag and get back into our main loop.
2762 if (do_abort(instance
) == 0) {
2763 hostdata
->connected
= NULL
;
2764 cmd
->result
= DID_ABORT
<< 16;
2768 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2770 maybe_release_dma_irq(instance
);
2771 local_irq_restore(flags
);
2772 cmd
->scsi_done(cmd
);
2775 local_irq_restore(flags
);
2776 printk("scsi%d: abort of connected command failed!\n", HOSTNO
);
2783 * Case 2 : If the command hasn't been issued yet, we simply remove it
2784 * from the issue queue.
2786 for (prev
= (struct scsi_cmnd
**)&(hostdata
->issue_queue
),
2787 tmp
= (struct scsi_cmnd
*)hostdata
->issue_queue
;
2788 tmp
; prev
= NEXTADDR(tmp
), tmp
= NEXT(tmp
)) {
2790 REMOVE(5, *prev
, tmp
, NEXT(tmp
));
2791 (*prev
) = NEXT(tmp
);
2792 SET_NEXT(tmp
, NULL
);
2793 tmp
->result
= DID_ABORT
<< 16;
2794 maybe_release_dma_irq(instance
);
2795 local_irq_restore(flags
);
2796 dprintk(NDEBUG_ABORT
, "scsi%d: abort removed command from issue queue.\n",
2798 /* Tagged queuing note: no tag to free here, hasn't been assigned
2800 tmp
->scsi_done(tmp
);
2806 * Case 3 : If any commands are connected, we're going to fail the abort
2807 * and let the high level SCSI driver retry at a later time or
2810 * Timeouts, and therefore aborted commands, will be highly unlikely
2811 * and handling them cleanly in this situation would make the common
2812 * case of noresets less efficient, and would pollute our code. So,
2816 if (hostdata
->connected
) {
2817 local_irq_restore(flags
);
2818 dprintk(NDEBUG_ABORT
, "scsi%d: abort failed, command connected.\n", HOSTNO
);
2823 * Case 4: If the command is currently disconnected from the bus, and
2824 * there are no connected commands, we reconnect the I_T_L or
2825 * I_T_L_Q nexus associated with it, go into message out, and send
2828 * This case is especially ugly. In order to reestablish the nexus, we
2829 * need to call NCR5380_select(). The easiest way to implement this
2830 * function was to abort if the bus was busy, and let the interrupt
2831 * handler triggered on the SEL for reselect take care of lost arbitrations
2832 * where necessary, meaning interrupts need to be enabled.
2834 * When interrupts are enabled, the queues may change - so we
2835 * can't remove it from the disconnected queue before selecting it
2836 * because that could cause a failure in hashing the nexus if that
2837 * device reselected.
2839 * Since the queues may change, we can't use the pointers from when we
2842 * So, we must first locate the command, and if NCR5380_select()
2843 * succeeds, then issue the abort, relocate the command and remove
2844 * it from the disconnected queue.
2847 for (tmp
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
; tmp
;
2850 local_irq_restore(flags
);
2851 dprintk(NDEBUG_ABORT
, "scsi%d: aborting disconnected command.\n", HOSTNO
);
2853 if (NCR5380_select(instance
, cmd
))
2856 dprintk(NDEBUG_ABORT
, "scsi%d: nexus reestablished.\n", HOSTNO
);
2860 local_irq_save(flags
);
2861 for (prev
= (struct scsi_cmnd
**)&(hostdata
->disconnected_queue
),
2862 tmp
= (struct scsi_cmnd
*)hostdata
->disconnected_queue
;
2863 tmp
; prev
= NEXTADDR(tmp
), tmp
= NEXT(tmp
)) {
2865 REMOVE(5, *prev
, tmp
, NEXT(tmp
));
2867 SET_NEXT(tmp
, NULL
);
2868 tmp
->result
= DID_ABORT
<< 16;
2869 /* We must unlock the tag/LUN immediately here, since the
2870 * target goes to BUS FREE and doesn't send us another
2871 * message (COMMAND_COMPLETE or the like)
2876 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2878 maybe_release_dma_irq(instance
);
2879 local_irq_restore(flags
);
2880 tmp
->scsi_done(tmp
);
2887 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2888 * possible at all) At least, we should check if the lock could be
2889 * released after the abort, in case it is kept due to some bug.
2891 maybe_release_dma_irq(instance
);
2892 local_irq_restore(flags
);
2895 * Case 5 : If we reached this point, the command was not found in any of
2898 * We probably reached this point because of an unlikely race condition
2899 * between the command completing successfully and the abortion code,
2900 * so we won't panic, but we will notify the user in case something really
2904 printk(KERN_INFO
"scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO
);
2911 * NCR5380_bus_reset - reset the SCSI bus
2912 * @cmd: SCSI command undergoing EH
2917 static int NCR5380_bus_reset(struct scsi_cmnd
*cmd
)
2919 struct Scsi_Host
*instance
= cmd
->device
->host
;
2920 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2922 unsigned long flags
;
2924 local_irq_save(flags
);
2926 #if (NDEBUG & NDEBUG_ANY)
2927 scmd_printk(KERN_INFO
, cmd
, "performing bus reset\n");
2928 NCR5380_print_status(instance
);
2933 /* reset NCR registers */
2934 NCR5380_write(MODE_REG
, MR_BASE
);
2935 NCR5380_write(TARGET_COMMAND_REG
, 0);
2936 NCR5380_write(SELECT_ENABLE_REG
, 0);
2938 /* After the reset, there are no more connected or disconnected commands
2939 * and no busy units; so clear the low-level status here to avoid
2940 * conflicts when the mid-level code tries to wake up the affected
2944 if (hostdata
->issue_queue
)
2945 dprintk(NDEBUG_ABORT
, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd
));
2946 if (hostdata
->connected
)
2947 dprintk(NDEBUG_ABORT
, "scsi%d: reset aborted a connected command\n", H_NO(cmd
));
2948 if (hostdata
->disconnected_queue
)
2949 dprintk(NDEBUG_ABORT
, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd
));
2951 hostdata
->issue_queue
= NULL
;
2952 hostdata
->connected
= NULL
;
2953 hostdata
->disconnected_queue
= NULL
;
2955 free_all_tags(hostdata
);
2957 for (i
= 0; i
< 8; ++i
)
2958 hostdata
->busy
[i
] = 0;
2960 hostdata
->dma_len
= 0;
2963 maybe_release_dma_irq(instance
);
2964 local_irq_restore(flags
);