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.
70 * Further development / testing that should be done :
71 * 1. Test linked command handling code after Eric is ready with
72 * the high level code.
75 /* Adapted for the sun3 by Sam Creasey. */
77 #include <scsi/scsi_dbg.h>
78 #include <scsi/scsi_transport_spi.h>
80 #if (NDEBUG & NDEBUG_LISTS)
83 printk("LINE:%d Adding %p to %p\n", \
84 __LINE__, (void*)(x), (void*)(y)); \
88 #define REMOVE(w, x, y, z) \
90 printk("LINE:%d Removing: %p->%p %p->%p \n", \
91 __LINE__, (void*)(w), (void*)(x), \
92 (void*)(y), (void*)(z)); \
98 #define REMOVE(w,x,y,z)
108 * This is a generic 5380 driver. To use it on a different platform,
109 * one simply writes appropriate system specific macros (ie, data
110 * transfer - some PC's will use the I/O bus, 68K's must use
111 * memory mapped) and drops this file in their 'C' wrapper.
113 * As far as command queueing, two queues are maintained for
114 * each 5380 in the system - commands that haven't been issued yet,
115 * and commands that are currently executing. This means that an
116 * unlimited number of commands may be queued, letting
117 * more commands propagate from the higher driver levels giving higher
118 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
119 * allowing multiple commands to propagate all the way to a SCSI-II device
120 * while a command is already executing.
123 * Issues specific to the NCR5380 :
125 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
126 * piece of hardware that requires you to sit in a loop polling for
127 * the REQ signal as long as you are connected. Some devices are
128 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
129 * while doing long seek operations.
131 * The workaround for this is to keep track of devices that have
132 * disconnected. If the device hasn't disconnected, for commands that
133 * should disconnect, we do something like
135 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
137 * Some tweaking of N and M needs to be done. An algorithm based
138 * on "time to data" would give the best results as long as short time
139 * to datas (ie, on the same track) were considered, however these
140 * broken devices are the exception rather than the rule and I'd rather
141 * spend my time optimizing for the normal case.
145 * At the heart of the design is a coroutine, NCR5380_main,
146 * which is started from a workqueue for each NCR5380 host in the
147 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
148 * removing the commands from the issue queue and calling
149 * NCR5380_select() if a nexus is not established.
151 * Once a nexus is established, the NCR5380_information_transfer()
152 * phase goes through the various phases as instructed by the target.
153 * if the target goes into MSG IN and sends a DISCONNECT message,
154 * the command structure is placed into the per instance disconnected
155 * queue, and NCR5380_main tries to find more work. If the target is
156 * idle for too long, the system will try to sleep.
158 * If a command has disconnected, eventually an interrupt will trigger,
159 * calling NCR5380_intr() which will in turn call NCR5380_reselect
160 * to reestablish a nexus. This will run main if necessary.
162 * On command termination, the done function will be called as
165 * SCSI pointers are maintained in the SCp field of SCSI command
166 * structures, being initialized after the command is connected
167 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
168 * Note that in violation of the standard, an implicit SAVE POINTERS operation
169 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
174 * This file a skeleton Linux SCSI driver for the NCR 5380 series
175 * of chips. To use it, you write an architecture specific functions
176 * and macros and include this file in your driver.
178 * These macros control options :
179 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
180 * for commands that return with a CHECK CONDITION status.
182 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
185 * LINKED - if defined, linked commands are supported.
187 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
189 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
191 * These macros MUST be defined :
193 * NCR5380_read(register) - read from the specified register
195 * NCR5380_write(register, value) - write to the specific register
197 * NCR5380_implementation_fields - additional fields needed for this
198 * specific implementation of the NCR5380
200 * Either real DMA *or* pseudo DMA may be implemented
202 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
203 * Note that the DMA setup functions should return the number of bytes
204 * that they were able to program the controller for.
206 * Also note that generic i386/PC versions of these macros are
207 * available as NCR5380_i386_dma_write_setup,
208 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
210 * NCR5380_dma_write_setup(instance, src, count) - initialize
211 * NCR5380_dma_read_setup(instance, dst, count) - initialize
212 * NCR5380_dma_residual(instance); - residual count
215 * NCR5380_pwrite(instance, src, count)
216 * NCR5380_pread(instance, dst, count);
218 * The generic driver is initialized by calling NCR5380_init(instance),
219 * after setting the appropriate host specific fields and ID. If the
220 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
221 * possible) function may be used.
224 /* Macros ease life... :-) */
225 #define SETUP_HOSTDATA(in) \
226 struct NCR5380_hostdata *hostdata = \
227 (struct NCR5380_hostdata *)(in)->hostdata
228 #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
230 #define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble)
231 #define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
232 #define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble)
234 #define HOSTNO instance->host_no
235 #define H_NO(cmd) (cmd)->device->host->host_no
237 static int do_abort(struct Scsi_Host
*);
238 static void do_reset(struct Scsi_Host
*);
243 * Functions for handling tagged queuing
244 * =====================================
246 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
248 * Using consecutive numbers for the tags is no good idea in my eyes. There
249 * could be wrong re-usings if the counter (8 bit!) wraps and some early
250 * command has been preempted for a long time. My solution: a bitfield for
251 * remembering used tags.
253 * There's also the problem that each target has a certain queue size, but we
254 * cannot know it in advance :-( We just see a QUEUE_FULL status being
255 * returned. So, in this case, the driver internal queue size assumption is
256 * reduced to the number of active tags if QUEUE_FULL is returned by the
257 * target. The command is returned to the mid-level, but with status changed
258 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
261 * We're also not allowed running tagged commands as long as an untagged
262 * command is active. And REQUEST SENSE commands after a contingent allegiance
263 * condition _must_ be untagged. To keep track whether an untagged command has
264 * been issued, the host->busy array is still employed, as it is without
265 * support for tagged queuing.
267 * One could suspect that there are possible race conditions between
268 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
269 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
270 * which already guaranteed to be running at most once. It is also the only
271 * place where tags/LUNs are allocated. So no other allocation can slip
272 * between that pair, there could only happen a reselection, which can free a
273 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
274 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
277 static void __init
init_tags(struct NCR5380_hostdata
*hostdata
)
280 struct tag_alloc
*ta
;
282 if (!(hostdata
->flags
& FLAG_TAGGED_QUEUING
))
285 for (target
= 0; target
< 8; ++target
) {
286 for (lun
= 0; lun
< 8; ++lun
) {
287 ta
= &hostdata
->TagAlloc
[target
][lun
];
288 bitmap_zero(ta
->allocated
, MAX_TAGS
);
289 ta
->nr_allocated
= 0;
290 /* At the beginning, assume the maximum queue size we could
291 * support (MAX_TAGS). This value will be decreased if the target
292 * returns QUEUE_FULL status.
294 ta
->queue_size
= MAX_TAGS
;
300 /* Check if we can issue a command to this LUN: First see if the LUN is marked
301 * busy by an untagged command. If the command should use tagged queuing, also
302 * check that there is a free tag and the target's queue won't overflow. This
303 * function should be called with interrupts disabled to avoid race
307 static int is_lun_busy(struct scsi_cmnd
*cmd
, int should_be_tagged
)
309 u8 lun
= cmd
->device
->lun
;
310 SETUP_HOSTDATA(cmd
->device
->host
);
312 if (hostdata
->busy
[cmd
->device
->id
] & (1 << lun
))
314 if (!should_be_tagged
||
315 !(hostdata
->flags
& FLAG_TAGGED_QUEUING
) ||
316 !cmd
->device
->tagged_supported
)
318 if (hostdata
->TagAlloc
[scmd_id(cmd
)][lun
].nr_allocated
>=
319 hostdata
->TagAlloc
[scmd_id(cmd
)][lun
].queue_size
) {
320 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %d: no free tags\n",
321 H_NO(cmd
), cmd
->device
->id
, lun
);
328 /* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
329 * must be called before!), or reserve the LUN in 'busy' if the command is
333 static void cmd_get_tag(struct scsi_cmnd
*cmd
, int should_be_tagged
)
335 u8 lun
= cmd
->device
->lun
;
336 SETUP_HOSTDATA(cmd
->device
->host
);
338 /* If we or the target don't support tagged queuing, allocate the LUN for
339 * an untagged command.
341 if (!should_be_tagged
||
342 !(hostdata
->flags
& FLAG_TAGGED_QUEUING
) ||
343 !cmd
->device
->tagged_supported
) {
345 hostdata
->busy
[cmd
->device
->id
] |= (1 << lun
);
346 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %d now allocated by untagged "
347 "command\n", H_NO(cmd
), cmd
->device
->id
, lun
);
349 struct tag_alloc
*ta
= &hostdata
->TagAlloc
[scmd_id(cmd
)][lun
];
351 cmd
->tag
= find_first_zero_bit(ta
->allocated
, MAX_TAGS
);
352 set_bit(cmd
->tag
, ta
->allocated
);
354 dprintk(NDEBUG_TAGS
, "scsi%d: using tag %d for target %d lun %d "
355 "(now %d tags in use)\n",
356 H_NO(cmd
), cmd
->tag
, cmd
->device
->id
,
357 lun
, ta
->nr_allocated
);
362 /* Mark the tag of command 'cmd' as free, or in case of an untagged command,
366 static void cmd_free_tag(struct scsi_cmnd
*cmd
)
368 u8 lun
= cmd
->device
->lun
;
369 SETUP_HOSTDATA(cmd
->device
->host
);
371 if (cmd
->tag
== TAG_NONE
) {
372 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << lun
);
373 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %d untagged cmd finished\n",
374 H_NO(cmd
), cmd
->device
->id
, lun
);
375 } else if (cmd
->tag
>= MAX_TAGS
) {
376 printk(KERN_NOTICE
"scsi%d: trying to free bad tag %d!\n",
377 H_NO(cmd
), cmd
->tag
);
379 struct tag_alloc
*ta
= &hostdata
->TagAlloc
[scmd_id(cmd
)][lun
];
380 clear_bit(cmd
->tag
, ta
->allocated
);
382 dprintk(NDEBUG_TAGS
, "scsi%d: freed tag %d for target %d lun %d\n",
383 H_NO(cmd
), cmd
->tag
, cmd
->device
->id
, lun
);
388 static void free_all_tags(struct NCR5380_hostdata
*hostdata
)
391 struct tag_alloc
*ta
;
393 if (!(hostdata
->flags
& FLAG_TAGGED_QUEUING
))
396 for (target
= 0; target
< 8; ++target
) {
397 for (lun
= 0; lun
< 8; ++lun
) {
398 ta
= &hostdata
->TagAlloc
[target
][lun
];
399 bitmap_zero(ta
->allocated
, MAX_TAGS
);
400 ta
->nr_allocated
= 0;
405 #endif /* SUPPORT_TAGS */
409 * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd )
411 * Purpose: Try to merge several scatter-gather requests into one DMA
412 * transfer. This is possible if the scatter buffers lie on
413 * physical contiguous addresses.
415 * Parameters: struct scsi_cmnd *cmd
416 * The command to work on. The first scatter buffer's data are
417 * assumed to be already transferred into ptr/this_residual.
420 static void merge_contiguous_buffers(struct scsi_cmnd
*cmd
)
422 #if !defined(CONFIG_SUN3)
423 unsigned long endaddr
;
424 #if (NDEBUG & NDEBUG_MERGING)
425 unsigned long oldlen
= cmd
->SCp
.this_residual
;
429 for (endaddr
= virt_to_phys(cmd
->SCp
.ptr
+ cmd
->SCp
.this_residual
- 1) + 1;
430 cmd
->SCp
.buffers_residual
&&
431 virt_to_phys(sg_virt(&cmd
->SCp
.buffer
[1])) == endaddr
;) {
432 dprintk(NDEBUG_MERGING
, "VTOP(%p) == %08lx -> merging\n",
433 page_address(sg_page(&cmd
->SCp
.buffer
[1])), endaddr
);
434 #if (NDEBUG & NDEBUG_MERGING)
438 --cmd
->SCp
.buffers_residual
;
439 cmd
->SCp
.this_residual
+= cmd
->SCp
.buffer
->length
;
440 endaddr
+= cmd
->SCp
.buffer
->length
;
442 #if (NDEBUG & NDEBUG_MERGING)
443 if (oldlen
!= cmd
->SCp
.this_residual
)
444 dprintk(NDEBUG_MERGING
, "merged %d buffers from %p, new length %08x\n",
445 cnt
, cmd
->SCp
.ptr
, cmd
->SCp
.this_residual
);
447 #endif /* !defined(CONFIG_SUN3) */
451 * initialize_SCp - init the scsi pointer field
452 * @cmd: command block to set up
454 * Set up the internal fields in the SCSI command.
457 static inline void initialize_SCp(struct scsi_cmnd
*cmd
)
460 * Initialize the Scsi Pointer field so that all of the commands in the
461 * various queues are valid.
464 if (scsi_bufflen(cmd
)) {
465 cmd
->SCp
.buffer
= scsi_sglist(cmd
);
466 cmd
->SCp
.buffers_residual
= scsi_sg_count(cmd
) - 1;
467 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
468 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
469 /* ++roman: Try to merge some scatter-buffers if they are at
470 * contiguous physical addresses.
472 merge_contiguous_buffers(cmd
);
474 cmd
->SCp
.buffer
= NULL
;
475 cmd
->SCp
.buffers_residual
= 0;
477 cmd
->SCp
.this_residual
= 0;
482 * NCR5380_poll_politely - wait for NCR5380 status bits
483 * @instance: controller to poll
484 * @reg: 5380 register to poll
485 * @bit: Bitmask to check
486 * @val: Value required to exit
488 * Polls the NCR5380 in a reasonably efficient manner waiting for
489 * an event to occur, after a short quick poll we begin giving the
490 * CPU back in non IRQ contexts
492 * Returns the value of the register or a negative error code.
495 static int NCR5380_poll_politely(struct Scsi_Host
*instance
,
496 int reg
, int bit
, int val
, int t
)
499 unsigned long end
= jiffies
+ t
;
503 r
= NCR5380_read(reg
);
504 if ((r
& bit
) == val
)
510 while (time_before(jiffies
, end
)) {
511 r
= NCR5380_read(reg
);
512 if ((r
& bit
) == val
)
522 #include <linux/delay.h>
529 { SR_DBP
, "PARITY"}, { SR_RST
, "RST" }, { SR_BSY
, "BSY" },
530 { SR_REQ
, "REQ" }, { SR_MSG
, "MSG" }, { SR_CD
, "CD" }, { SR_IO
, "IO" },
531 { SR_SEL
, "SEL" }, {0, NULL
}
533 {BASR_ATN
, "ATN"}, {BASR_ACK
, "ACK"}, {0, NULL
}
535 {ICR_ASSERT_RST
, "ASSERT RST"},{ICR_ASSERT_ACK
, "ASSERT ACK"},
536 {ICR_ASSERT_BSY
, "ASSERT BSY"}, {ICR_ASSERT_SEL
, "ASSERT SEL"},
537 {ICR_ASSERT_ATN
, "ASSERT ATN"}, {ICR_ASSERT_DATA
, "ASSERT DATA"},
540 {MR_BLOCK_DMA_MODE
, "MODE BLOCK DMA"}, {MR_TARGET
, "MODE TARGET"},
541 {MR_ENABLE_PAR_CHECK
, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR
,
542 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR
,"MODE EOP INTR"},
543 {MR_MONITOR_BSY
, "MODE MONITOR BSY"},
544 {MR_DMA_MODE
, "MODE DMA"}, {MR_ARBITRATE
, "MODE ARBITRATION"},
549 * NCR5380_print - print scsi bus signals
550 * @instance: adapter state to dump
552 * Print the SCSI bus signals for debugging purposes
555 static void NCR5380_print(struct Scsi_Host
*instance
)
557 unsigned char status
, data
, basr
, mr
, icr
, i
;
560 local_irq_save(flags
);
561 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
562 status
= NCR5380_read(STATUS_REG
);
563 mr
= NCR5380_read(MODE_REG
);
564 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
565 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
566 local_irq_restore(flags
);
567 printk("STATUS_REG: %02x ", status
);
568 for (i
= 0; signals
[i
].mask
; ++i
)
569 if (status
& signals
[i
].mask
)
570 printk(",%s", signals
[i
].name
);
571 printk("\nBASR: %02x ", basr
);
572 for (i
= 0; basrs
[i
].mask
; ++i
)
573 if (basr
& basrs
[i
].mask
)
574 printk(",%s", basrs
[i
].name
);
575 printk("\nICR: %02x ", icr
);
576 for (i
= 0; icrs
[i
].mask
; ++i
)
577 if (icr
& icrs
[i
].mask
)
578 printk(",%s", icrs
[i
].name
);
579 printk("\nMODE: %02x ", mr
);
580 for (i
= 0; mrs
[i
].mask
; ++i
)
581 if (mr
& mrs
[i
].mask
)
582 printk(",%s", mrs
[i
].name
);
590 {PHASE_DATAOUT
, "DATAOUT"}, {PHASE_DATAIN
, "DATAIN"}, {PHASE_CMDOUT
, "CMDOUT"},
591 {PHASE_STATIN
, "STATIN"}, {PHASE_MSGOUT
, "MSGOUT"}, {PHASE_MSGIN
, "MSGIN"},
592 {PHASE_UNKNOWN
, "UNKNOWN"}
596 * NCR5380_print_phase - show SCSI phase
597 * @instance: adapter to dump
599 * Print the current SCSI phase for debugging purposes
604 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
606 unsigned char status
;
609 status
= NCR5380_read(STATUS_REG
);
610 if (!(status
& SR_REQ
))
611 printk(KERN_DEBUG
"scsi%d: REQ not asserted, phase unknown.\n", HOSTNO
);
613 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
614 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
616 printk(KERN_DEBUG
"scsi%d: phase %s\n", HOSTNO
, phases
[i
].name
);
623 * ++roman: New scheme of calling NCR5380_main()
625 * If we're not in an interrupt, we can call our main directly, it cannot be
626 * already running. Else, we queue it on a task queue, if not 'main_running'
627 * tells us that a lower level is already executing it. This way,
628 * 'main_running' needs not be protected in a special way.
630 * queue_main() is a utility function for putting our main onto the task
631 * queue, if main_running is false. It should be called only from a
632 * interrupt or bottom half.
635 #include <linux/gfp.h>
636 #include <linux/workqueue.h>
637 #include <linux/interrupt.h>
639 static inline void queue_main(struct NCR5380_hostdata
*hostdata
)
641 if (!hostdata
->main_running
) {
642 /* If in interrupt and NCR5380_main() not already running,
643 queue it on the 'immediate' task queue, to be processed
644 immediately after the current interrupt processing has
646 schedule_work(&hostdata
->main_task
);
648 /* else: nothing to do: the running NCR5380_main() will pick up
649 any newly queued command. */
653 * NCR58380_info - report driver and host information
654 * @instance: relevant scsi host instance
656 * For use as the host template info() handler.
661 static const char *NCR5380_info(struct Scsi_Host
*instance
)
663 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
665 return hostdata
->info
;
668 static void prepare_info(struct Scsi_Host
*instance
)
670 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
672 snprintf(hostdata
->info
, sizeof(hostdata
->info
),
673 "%s, io_port 0x%lx, n_io_port %d, "
674 "base 0x%lx, irq %d, "
675 "can_queue %d, cmd_per_lun %d, "
676 "sg_tablesize %d, this_id %d, "
679 instance
->hostt
->name
, instance
->io_port
, instance
->n_io_port
,
680 instance
->base
, instance
->irq
,
681 instance
->can_queue
, instance
->cmd_per_lun
,
682 instance
->sg_tablesize
, instance
->this_id
,
683 hostdata
->flags
& FLAG_TAGGED_QUEUING
? "TAGGED_QUEUING " : "",
684 hostdata
->flags
& FLAG_TOSHIBA_DELAY
? "TOSHIBA_DELAY " : "",
701 * NCR5380_print_status - dump controller info
702 * @instance: controller to dump
704 * Print commands in the various queues, called from NCR5380_abort
708 static void lprint_Scsi_Cmnd(struct scsi_cmnd
*cmd
)
711 unsigned char *command
;
712 printk("scsi%d: destination target %d, lun %llu\n",
713 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
714 printk(KERN_CONT
" command = ");
716 printk(KERN_CONT
"%2d (0x%02x)", command
[0], command
[0]);
717 for (i
= 1, s
= COMMAND_SIZE(command
[0]); i
< s
; ++i
)
718 printk(KERN_CONT
" %02x", command
[i
]);
722 static void __maybe_unused
NCR5380_print_status(struct Scsi_Host
*instance
)
724 struct NCR5380_hostdata
*hostdata
;
725 struct scsi_cmnd
*ptr
;
728 NCR5380_dprint(NDEBUG_ANY
, instance
);
729 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
731 hostdata
= (struct NCR5380_hostdata
*)instance
->hostdata
;
733 local_irq_save(flags
);
734 printk("NCR5380: coroutine is%s running.\n",
735 hostdata
->main_running
? "" : "n't");
736 if (!hostdata
->connected
)
737 printk("scsi%d: no currently connected command\n", HOSTNO
);
739 lprint_Scsi_Cmnd((struct scsi_cmnd
*) hostdata
->connected
);
740 printk("scsi%d: issue_queue\n", HOSTNO
);
741 for (ptr
= (struct scsi_cmnd
*)hostdata
->issue_queue
; ptr
; ptr
= NEXT(ptr
))
742 lprint_Scsi_Cmnd(ptr
);
744 printk("scsi%d: disconnected_queue\n", HOSTNO
);
745 for (ptr
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
; ptr
;
747 lprint_Scsi_Cmnd(ptr
);
749 local_irq_restore(flags
);
753 static void show_Scsi_Cmnd(struct scsi_cmnd
*cmd
, struct seq_file
*m
)
756 unsigned char *command
;
757 seq_printf(m
, "scsi%d: destination target %d, lun %llu\n",
758 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
759 seq_puts(m
, " command = ");
761 seq_printf(m
, "%2d (0x%02x)", command
[0], command
[0]);
762 for (i
= 1, s
= COMMAND_SIZE(command
[0]); i
< s
; ++i
)
763 seq_printf(m
, " %02x", command
[i
]);
767 static int __maybe_unused
NCR5380_show_info(struct seq_file
*m
,
768 struct Scsi_Host
*instance
)
770 struct NCR5380_hostdata
*hostdata
;
771 struct scsi_cmnd
*ptr
;
774 hostdata
= (struct NCR5380_hostdata
*)instance
->hostdata
;
776 local_irq_save(flags
);
777 seq_printf(m
, "NCR5380: coroutine is%s running.\n",
778 hostdata
->main_running
? "" : "n't");
779 if (!hostdata
->connected
)
780 seq_printf(m
, "scsi%d: no currently connected command\n", HOSTNO
);
782 show_Scsi_Cmnd((struct scsi_cmnd
*) hostdata
->connected
, m
);
783 seq_printf(m
, "scsi%d: issue_queue\n", HOSTNO
);
784 for (ptr
= (struct scsi_cmnd
*)hostdata
->issue_queue
; ptr
; ptr
= NEXT(ptr
))
785 show_Scsi_Cmnd(ptr
, m
);
787 seq_printf(m
, "scsi%d: disconnected_queue\n", HOSTNO
);
788 for (ptr
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
; ptr
;
790 show_Scsi_Cmnd(ptr
, m
);
792 local_irq_restore(flags
);
797 * NCR5380_init - initialise an NCR5380
798 * @instance: adapter to configure
799 * @flags: control flags
801 * Initializes *instance and corresponding 5380 chip,
802 * with flags OR'd into the initial flags value.
804 * Notes : I assume that the host, hostno, and id bits have been
805 * set correctly. I don't care about the irq and other fields.
807 * Returns 0 for success
810 static int __init
NCR5380_init(struct Scsi_Host
*instance
, int flags
)
813 SETUP_HOSTDATA(instance
);
815 hostdata
->host
= instance
;
816 hostdata
->id_mask
= 1 << instance
->this_id
;
817 hostdata
->id_higher_mask
= 0;
818 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
819 if (i
> hostdata
->id_mask
)
820 hostdata
->id_higher_mask
|= i
;
821 for (i
= 0; i
< 8; ++i
)
822 hostdata
->busy
[i
] = 0;
826 #if defined (REAL_DMA)
827 hostdata
->dma_len
= 0;
829 hostdata
->targets_present
= 0;
830 hostdata
->connected
= NULL
;
831 hostdata
->issue_queue
= NULL
;
832 hostdata
->disconnected_queue
= NULL
;
833 hostdata
->flags
= flags
;
835 INIT_WORK(&hostdata
->main_task
, NCR5380_main
);
837 prepare_info(instance
);
839 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
840 NCR5380_write(MODE_REG
, MR_BASE
);
841 NCR5380_write(TARGET_COMMAND_REG
, 0);
842 NCR5380_write(SELECT_ENABLE_REG
, 0);
848 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
849 * @instance: adapter to check
851 * If the system crashed, it may have crashed with a connected target and
852 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
853 * currently established nexus, which we know nothing about. Failing that
856 * Note that a bus reset will cause the chip to assert IRQ.
858 * Returns 0 if successful, otherwise -ENXIO.
861 static int NCR5380_maybe_reset_bus(struct Scsi_Host
*instance
)
863 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
866 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) && pass
<= 6; ++pass
) {
871 shost_printk(KERN_ERR
, instance
, "SCSI bus busy, waiting up to five seconds\n");
872 NCR5380_poll_politely(instance
,
873 STATUS_REG
, SR_BSY
, 0, 5 * HZ
);
876 shost_printk(KERN_ERR
, instance
, "bus busy, attempting abort\n");
880 shost_printk(KERN_ERR
, instance
, "bus busy, attempting reset\n");
882 /* Wait after a reset; the SCSI standard calls for
883 * 250ms, we wait 500ms to be on the safe side.
884 * But some Toshiba CD-ROMs need ten times that.
886 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
892 shost_printk(KERN_ERR
, instance
, "bus locked solid\n");
900 * NCR5380_exit - remove an NCR5380
901 * @instance: adapter to remove
903 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
906 static void NCR5380_exit(struct Scsi_Host
*instance
)
908 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
910 cancel_work_sync(&hostdata
->main_task
);
914 * NCR5380_queue_command - queue a command
915 * @instance: the relevant SCSI adapter
918 * cmd is added to the per instance issue_queue, with minor
919 * twiddling done to the host specific fields of cmd. If the
920 * main coroutine is not running, it is restarted.
923 static int NCR5380_queue_command(struct Scsi_Host
*instance
,
924 struct scsi_cmnd
*cmd
)
926 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
927 struct scsi_cmnd
*tmp
;
930 #if (NDEBUG & NDEBUG_NO_WRITE)
931 switch (cmd
->cmnd
[0]) {
934 printk(KERN_NOTICE
"scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
936 cmd
->result
= (DID_ERROR
<< 16);
940 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
943 * We use the host_scribble field as a pointer to the next command
951 * Insert the cmd into the issue queue. Note that REQUEST SENSE
952 * commands are added to the head of the queue since any command will
953 * clear the contingent allegiance condition that exists and the
954 * sense data is only guaranteed to be valid while the condition exists.
957 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
958 * Otherwise a running NCR5380_main may steal the lock.
959 * Lock before actually inserting due to fairness reasons explained in
960 * atari_scsi.c. If we insert first, then it's impossible for this driver
961 * to release the lock.
962 * Stop timer for this command while waiting for the lock, or timeouts
963 * may happen (and they really do), and it's no good if the command doesn't
964 * appear in any of the queues.
965 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
966 * because also a timer int can trigger an abort or reset, which would
967 * alter queues and touch the lock.
969 if (!NCR5380_acquire_dma_irq(instance
))
970 return SCSI_MLQUEUE_HOST_BUSY
;
972 local_irq_save(flags
);
975 * Insert the cmd into the issue queue. Note that REQUEST SENSE
976 * commands are added to the head of the queue since any command will
977 * clear the contingent allegiance condition that exists and the
978 * sense data is only guaranteed to be valid while the condition exists.
981 if (!(hostdata
->issue_queue
) || (cmd
->cmnd
[0] == REQUEST_SENSE
)) {
982 LIST(cmd
, hostdata
->issue_queue
);
983 SET_NEXT(cmd
, hostdata
->issue_queue
);
984 hostdata
->issue_queue
= cmd
;
986 for (tmp
= (struct scsi_cmnd
*)hostdata
->issue_queue
;
987 NEXT(tmp
); tmp
= NEXT(tmp
))
992 local_irq_restore(flags
);
994 dprintk(NDEBUG_QUEUES
, "scsi%d: command added to %s of queue\n", H_NO(cmd
),
995 (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
997 /* If queue_command() is called from an interrupt (real one or bottom
998 * half), we let queue_main() do the job of taking care about main. If it
999 * is already running, this is a no-op, else main will be queued.
1001 * If we're not in an interrupt, we can call NCR5380_main()
1002 * unconditionally, because it cannot be already running.
1004 if (in_interrupt() || irqs_disabled())
1005 queue_main(hostdata
);
1007 NCR5380_main(&hostdata
->main_task
);
1011 static inline void maybe_release_dma_irq(struct Scsi_Host
*instance
)
1013 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1015 /* Caller does the locking needed to set & test these data atomically */
1016 if (!hostdata
->disconnected_queue
&&
1017 !hostdata
->issue_queue
&&
1018 !hostdata
->connected
&&
1019 !hostdata
->retain_dma_intr
)
1020 NCR5380_release_dma_irq(instance
);
1024 * NCR5380_main - NCR state machines
1026 * NCR5380_main is a coroutine that runs as long as more work can
1027 * be done on the NCR5380 host adapters in a system. Both
1028 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1029 * in case it is not running.
1031 * Locks: called as its own thread with no locks held.
1034 static void NCR5380_main(struct work_struct
*work
)
1036 struct NCR5380_hostdata
*hostdata
=
1037 container_of(work
, struct NCR5380_hostdata
, main_task
);
1038 struct Scsi_Host
*instance
= hostdata
->host
;
1039 struct scsi_cmnd
*tmp
, *prev
;
1041 unsigned long flags
;
1044 * We run (with interrupts disabled) until we're sure that none of
1045 * the host adapters have anything that can be done, at which point
1046 * we set main_running to 0 and exit.
1048 * Interrupts are enabled before doing various other internal
1049 * instructions, after we've decided that we need to run through
1052 * this should prevent any race conditions.
1054 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1055 * because also a timer int can trigger an abort or reset, which can
1056 * alter queues and touch the Falcon lock.
1059 /* Tell int handlers main() is now already executing. Note that
1060 no races are possible here. If an int comes in before
1061 'main_running' is set here, and queues/executes main via the
1062 task queue, it doesn't do any harm, just this instance of main
1063 won't find any work left to do. */
1064 if (hostdata
->main_running
)
1066 hostdata
->main_running
= 1;
1068 local_save_flags(flags
);
1070 local_irq_disable(); /* Freeze request queues */
1073 if (!hostdata
->connected
) {
1074 dprintk(NDEBUG_MAIN
, "scsi%d: not connected\n", HOSTNO
);
1076 * Search through the issue_queue for a command destined
1077 * for a target that's not busy.
1079 #if (NDEBUG & NDEBUG_LISTS)
1080 for (tmp
= (struct scsi_cmnd
*) hostdata
->issue_queue
, prev
= NULL
;
1081 tmp
&& (tmp
!= prev
); prev
= tmp
, tmp
= NEXT(tmp
))
1083 /*printk("%p ", tmp);*/
1084 if ((tmp
== prev
) && tmp
)
1086 /* else printk("\n"); */
1088 for (tmp
= (struct scsi_cmnd
*) hostdata
->issue_queue
,
1089 prev
= NULL
; tmp
; prev
= tmp
, tmp
= NEXT(tmp
)) {
1090 u8 lun
= tmp
->device
->lun
;
1092 dprintk(NDEBUG_LISTS
,
1093 "MAIN tmp=%p target=%d busy=%d lun=%d\n",
1094 tmp
, scmd_id(tmp
), hostdata
->busy
[scmd_id(tmp
)],
1096 /* When we find one, remove it from the issue queue. */
1097 /* ++guenther: possible race with Falcon locking */
1100 !is_lun_busy( tmp
, tmp
->cmnd
[0] != REQUEST_SENSE
)
1102 !(hostdata
->busy
[tmp
->device
->id
] & (1 << lun
))
1105 /* ++guenther: just to be sure, this must be atomic */
1106 local_irq_disable();
1108 REMOVE(prev
, NEXT(prev
), tmp
, NEXT(tmp
));
1109 SET_NEXT(prev
, NEXT(tmp
));
1111 REMOVE(-1, hostdata
->issue_queue
, tmp
, NEXT(tmp
));
1112 hostdata
->issue_queue
= NEXT(tmp
);
1114 SET_NEXT(tmp
, NULL
);
1115 hostdata
->retain_dma_intr
++;
1117 /* reenable interrupts after finding one */
1118 local_irq_restore(flags
);
1121 * Attempt to establish an I_T_L nexus here.
1122 * On success, instance->hostdata->connected is set.
1123 * On failure, we must add the command back to the
1124 * issue queue so we can keep trying.
1126 dprintk(NDEBUG_MAIN
, "scsi%d: main(): command for target %d "
1127 "lun %d removed from issue_queue\n",
1128 HOSTNO
, tmp
->device
->id
, lun
);
1130 * REQUEST SENSE commands are issued without tagged
1131 * queueing, even on SCSI-II devices because the
1132 * contingent allegiance condition exists for the
1135 /* ++roman: ...and the standard also requires that
1136 * REQUEST SENSE command are untagged.
1140 cmd_get_tag(tmp
, tmp
->cmnd
[0] != REQUEST_SENSE
);
1142 if (!NCR5380_select(instance
, tmp
)) {
1143 local_irq_disable();
1144 hostdata
->retain_dma_intr
--;
1145 /* release if target did not response! */
1146 maybe_release_dma_irq(instance
);
1147 local_irq_restore(flags
);
1150 local_irq_disable();
1151 LIST(tmp
, hostdata
->issue_queue
);
1152 SET_NEXT(tmp
, hostdata
->issue_queue
);
1153 hostdata
->issue_queue
= tmp
;
1157 hostdata
->retain_dma_intr
--;
1158 local_irq_restore(flags
);
1159 dprintk(NDEBUG_MAIN
, "scsi%d: main(): select() failed, "
1160 "returned to issue_queue\n", HOSTNO
);
1161 if (hostdata
->connected
)
1164 } /* if target/lun/target queue is not busy */
1165 } /* for issue_queue */
1166 } /* if (!hostdata->connected) */
1168 if (hostdata
->connected
1170 && !hostdata
->dma_len
1173 local_irq_restore(flags
);
1174 dprintk(NDEBUG_MAIN
, "scsi%d: main: performing information transfer\n",
1176 NCR5380_information_transfer(instance
);
1177 dprintk(NDEBUG_MAIN
, "scsi%d: main: done set false\n", HOSTNO
);
1182 /* Better allow ints _after_ 'main_running' has been cleared, else
1183 an interrupt could believe we'll pick up the work it left for
1184 us, but we won't see it anymore here... */
1185 hostdata
->main_running
= 0;
1186 local_irq_restore(flags
);
1192 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1194 * Purpose : Called by interrupt handler when DMA finishes or a phase
1195 * mismatch occurs (which would finish the DMA transfer).
1197 * Inputs : instance - this instance of the NCR5380.
1201 static void NCR5380_dma_complete(struct Scsi_Host
*instance
)
1203 SETUP_HOSTDATA(instance
);
1205 unsigned char **data
;
1206 volatile int *count
;
1207 int saved_data
= 0, overrun
= 0;
1210 if (!hostdata
->connected
) {
1211 printk(KERN_WARNING
"scsi%d: received end of DMA interrupt with "
1212 "no connected cmd\n", HOSTNO
);
1216 if (hostdata
->read_overruns
) {
1217 p
= hostdata
->connected
->SCp
.phase
;
1220 if ((NCR5380_read(BUS_AND_STATUS_REG
) &
1221 (BASR_PHASE_MATCH
|BASR_ACK
)) ==
1222 (BASR_PHASE_MATCH
|BASR_ACK
)) {
1223 saved_data
= NCR5380_read(INPUT_DATA_REG
);
1225 dprintk(NDEBUG_DMA
, "scsi%d: read overrun handled\n", HOSTNO
);
1230 dprintk(NDEBUG_DMA
, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1231 HOSTNO
, NCR5380_read(BUS_AND_STATUS_REG
),
1232 NCR5380_read(STATUS_REG
));
1234 #if defined(CONFIG_SUN3)
1235 if ((sun3scsi_dma_finish(rq_data_dir(hostdata
->connected
->request
)))) {
1236 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1241 /* make sure we're not stuck in a data phase */
1242 if ((NCR5380_read(BUS_AND_STATUS_REG
) & (BASR_PHASE_MATCH
| BASR_ACK
)) ==
1243 (BASR_PHASE_MATCH
| BASR_ACK
)) {
1244 pr_err("scsi%d: BASR %02x\n", instance
->host_no
,
1245 NCR5380_read(BUS_AND_STATUS_REG
));
1246 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1252 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1253 NCR5380_write(MODE_REG
, MR_BASE
);
1254 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1256 transferred
= hostdata
->dma_len
- NCR5380_dma_residual(instance
);
1257 hostdata
->dma_len
= 0;
1259 data
= (unsigned char **)&hostdata
->connected
->SCp
.ptr
;
1260 count
= &hostdata
->connected
->SCp
.this_residual
;
1261 *data
+= transferred
;
1262 *count
-= transferred
;
1264 if (hostdata
->read_overruns
) {
1267 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) == p
&& (p
& SR_IO
)) {
1268 cnt
= toPIO
= hostdata
->read_overruns
;
1270 dprintk(NDEBUG_DMA
, "Got an input overrun, using saved byte\n");
1271 *(*data
)++ = saved_data
;
1276 dprintk(NDEBUG_DMA
, "Doing %d-byte PIO to 0x%08lx\n", cnt
, (long)*data
);
1277 NCR5380_transfer_pio(instance
, &p
, &cnt
, data
);
1278 *count
-= toPIO
- cnt
;
1282 #endif /* REAL_DMA */
1286 * NCR5380_intr - generic NCR5380 irq handler
1287 * @irq: interrupt number
1288 * @dev_id: device info
1290 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1291 * from the disconnected queue, and restarting NCR5380_main()
1295 static irqreturn_t
NCR5380_intr(int irq
, void *dev_id
)
1297 struct Scsi_Host
*instance
= dev_id
;
1298 int done
= 1, handled
= 0;
1301 dprintk(NDEBUG_INTR
, "scsi%d: NCR5380 irq triggered\n", HOSTNO
);
1303 /* Look for pending interrupts */
1304 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
1305 dprintk(NDEBUG_INTR
, "scsi%d: BASR=%02x\n", HOSTNO
, basr
);
1306 /* dispatch to appropriate routine if found and done=0 */
1307 if (basr
& BASR_IRQ
) {
1308 NCR5380_dprint(NDEBUG_INTR
, instance
);
1309 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
|SR_IO
)) == (SR_SEL
|SR_IO
)) {
1311 dprintk(NDEBUG_INTR
, "scsi%d: SEL interrupt\n", HOSTNO
);
1312 NCR5380_reselect(instance
);
1313 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1314 } else if (basr
& BASR_PARITY_ERROR
) {
1315 dprintk(NDEBUG_INTR
, "scsi%d: PARITY interrupt\n", HOSTNO
);
1316 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1317 } else if ((NCR5380_read(STATUS_REG
) & SR_RST
) == SR_RST
) {
1318 dprintk(NDEBUG_INTR
, "scsi%d: RESET interrupt\n", HOSTNO
);
1319 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1322 * The rest of the interrupt conditions can occur only during a
1326 #if defined(REAL_DMA)
1328 * We should only get PHASE MISMATCH and EOP interrupts if we have
1329 * DMA enabled, so do a sanity check based on the current setting
1330 * of the MODE register.
1333 if ((NCR5380_read(MODE_REG
) & MR_DMA_MODE
) &&
1334 ((basr
& BASR_END_DMA_TRANSFER
) ||
1335 !(basr
& BASR_PHASE_MATCH
))) {
1337 dprintk(NDEBUG_INTR
, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO
);
1338 NCR5380_dma_complete( instance
);
1341 #endif /* REAL_DMA */
1343 /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1344 if (basr
& BASR_PHASE_MATCH
)
1345 dprintk(NDEBUG_INTR
, "scsi%d: unknown interrupt, "
1346 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1347 HOSTNO
, basr
, NCR5380_read(MODE_REG
),
1348 NCR5380_read(STATUS_REG
));
1349 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1350 #ifdef SUN3_SCSI_VME
1351 dregs
->csr
|= CSR_DMA_ENABLE
;
1354 } /* if !(SELECTION || PARITY) */
1356 } /* BASR & IRQ */ else {
1357 printk(KERN_NOTICE
"scsi%d: interrupt without IRQ bit set in BASR, "
1358 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO
, basr
,
1359 NCR5380_read(MODE_REG
), NCR5380_read(STATUS_REG
));
1360 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1361 #ifdef SUN3_SCSI_VME
1362 dregs
->csr
|= CSR_DMA_ENABLE
;
1367 dprintk(NDEBUG_INTR
, "scsi%d: in int routine, calling main\n", HOSTNO
);
1368 /* Put a call to NCR5380_main() on the queue... */
1369 queue_main(shost_priv(instance
));
1371 return IRQ_RETVAL(handled
);
1375 * Function : int NCR5380_select(struct Scsi_Host *instance,
1376 * struct scsi_cmnd *cmd)
1378 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1379 * including ARBITRATION, SELECTION, and initial message out for
1380 * IDENTIFY and queue messages.
1382 * Inputs : instance - instantiation of the 5380 driver on which this
1383 * target lives, cmd - SCSI command to execute.
1385 * Returns : -1 if selection could not execute for some reason,
1386 * 0 if selection succeeded or failed because the target
1390 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1391 * with registers as they should have been on entry - ie
1392 * SELECT_ENABLE will be set appropriately, the NCR5380
1393 * will cease to drive any SCSI bus signals.
1395 * If successful : I_T_L or I_T_L_Q nexus will be established,
1396 * instance->connected will be set to cmd.
1397 * SELECT interrupt will be disabled.
1399 * If failed (no target) : cmd->scsi_done() will be called, and the
1400 * cmd->result host byte set to DID_BAD_TARGET.
1403 static int NCR5380_select(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
1405 SETUP_HOSTDATA(instance
);
1406 unsigned char tmp
[3], phase
;
1407 unsigned char *data
;
1409 unsigned long timeout
;
1410 unsigned long flags
;
1412 hostdata
->restart_select
= 0;
1413 NCR5380_dprint(NDEBUG_ARBITRATION
, instance
);
1414 dprintk(NDEBUG_ARBITRATION
, "scsi%d: starting arbitration, id = %d\n", HOSTNO
,
1418 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1419 * data bus during SELECTION.
1422 local_irq_save(flags
);
1423 if (hostdata
->connected
) {
1424 local_irq_restore(flags
);
1427 NCR5380_write(TARGET_COMMAND_REG
, 0);
1430 * Start arbitration.
1433 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
1434 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
1436 local_irq_restore(flags
);
1438 /* Wait for arbitration logic to complete */
1439 #if defined(NCR_TIMEOUT)
1441 unsigned long timeout
= jiffies
+ 2*NCR_TIMEOUT
;
1443 while (!(NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
) &&
1444 time_before(jiffies
, timeout
) && !hostdata
->connected
)
1446 if (time_after_eq(jiffies
, timeout
)) {
1447 printk("scsi : arbitration timeout at %d\n", __LINE__
);
1448 NCR5380_write(MODE_REG
, MR_BASE
);
1449 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1453 #else /* NCR_TIMEOUT */
1454 while (!(NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
) &&
1455 !hostdata
->connected
)
1459 dprintk(NDEBUG_ARBITRATION
, "scsi%d: arbitration complete\n", HOSTNO
);
1461 if (hostdata
->connected
) {
1462 NCR5380_write(MODE_REG
, MR_BASE
);
1466 * The arbitration delay is 2.2us, but this is a minimum and there is
1467 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1468 * the integral nature of udelay().
1474 /* Check for lost arbitration */
1475 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1476 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1477 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1478 hostdata
->connected
) {
1479 NCR5380_write(MODE_REG
, MR_BASE
);
1480 dprintk(NDEBUG_ARBITRATION
, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1485 /* after/during arbitration, BSY should be asserted.
1486 IBM DPES-31080 Version S31Q works now */
1487 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1488 NCR5380_write(INITIATOR_COMMAND_REG
,
1489 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1491 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1492 hostdata
->connected
) {
1493 NCR5380_write(MODE_REG
, MR_BASE
);
1494 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1495 dprintk(NDEBUG_ARBITRATION
, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1501 * Again, bus clear + bus settle time is 1.2us, however, this is
1502 * a minimum so we'll udelay ceil(1.2)
1505 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
1510 if (hostdata
->connected
) {
1511 NCR5380_write(MODE_REG
, MR_BASE
);
1512 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1516 dprintk(NDEBUG_ARBITRATION
, "scsi%d: won arbitration\n", HOSTNO
);
1519 * Now that we have won arbitration, start Selection process, asserting
1520 * the host and target ID's on the SCSI bus.
1523 NCR5380_write(OUTPUT_DATA_REG
, (hostdata
->id_mask
| (1 << cmd
->device
->id
)));
1526 * Raise ATN while SEL is true before BSY goes false from arbitration,
1527 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1528 * phase immediately after selection.
1531 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_BSY
|
1532 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1533 NCR5380_write(MODE_REG
, MR_BASE
);
1536 * Reselect interrupts must be turned off prior to the dropping of BSY,
1537 * otherwise we will trigger an interrupt.
1540 if (hostdata
->connected
) {
1541 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1545 NCR5380_write(SELECT_ENABLE_REG
, 0);
1548 * The initiator shall then wait at least two deskew delays and release
1551 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1554 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_DATA
|
1555 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1558 * Something weird happens when we cease to drive BSY - looks
1559 * like the board/chip is letting us do another read before the
1560 * appropriate propagation delay has expired, and we're confusing
1561 * a BSY signal from ourselves as the target's response to SELECTION.
1563 * A small delay (the 'C++' frontend breaks the pipeline with an
1564 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1565 * tighter 'C' code breaks and requires this) solves the problem -
1566 * the 1 us delay is arbitrary, and only used because this delay will
1567 * be the same on other platforms and since it works here, it should
1570 * wingel suggests that this could be due to failing to wait
1576 dprintk(NDEBUG_SELECTION
, "scsi%d: selecting target %d\n", HOSTNO
, cmd
->device
->id
);
1579 * The SCSI specification calls for a 250 ms timeout for the actual
1583 timeout
= jiffies
+ msecs_to_jiffies(250);
1586 * XXX very interesting - we're seeing a bounce where the BSY we
1587 * asserted is being reflected / still asserted (propagation delay?)
1588 * and it's detecting as true. Sigh.
1592 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1593 * IO while SEL is true. But again, there are some disks out the in the
1594 * world that do that nevertheless. (Somebody claimed that this announces
1595 * reselection capability of the target.) So we better skip that test and
1596 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1599 while (time_before(jiffies
, timeout
) &&
1600 !(NCR5380_read(STATUS_REG
) & (SR_BSY
| SR_IO
)))
1603 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) == (SR_SEL
| SR_IO
)) {
1604 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1605 NCR5380_reselect(instance
);
1606 printk(KERN_ERR
"scsi%d: reselection after won arbitration?\n",
1608 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1612 while (time_before(jiffies
, timeout
) && !(NCR5380_read(STATUS_REG
) & SR_BSY
))
1617 * No less than two deskew delays after the initiator detects the
1618 * BSY signal is true, it shall release the SEL signal and may
1619 * change the DATA BUS. -wingel
1624 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1626 if (!(NCR5380_read(STATUS_REG
) & SR_BSY
)) {
1627 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1628 if (hostdata
->targets_present
& (1 << cmd
->device
->id
)) {
1629 printk(KERN_ERR
"scsi%d: weirdness\n", HOSTNO
);
1630 if (hostdata
->restart_select
)
1631 printk(KERN_NOTICE
"\trestart select\n");
1632 NCR5380_dprint(NDEBUG_ANY
, instance
);
1633 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1636 cmd
->result
= DID_BAD_TARGET
<< 16;
1640 cmd
->scsi_done(cmd
);
1641 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1642 dprintk(NDEBUG_SELECTION
, "scsi%d: target did not respond within 250ms\n", HOSTNO
);
1646 hostdata
->targets_present
|= (1 << cmd
->device
->id
);
1649 * Since we followed the SCSI spec, and raised ATN while SEL
1650 * was true but before BSY was false during selection, the information
1651 * transfer phase should be a MESSAGE OUT phase so that we can send the
1654 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1655 * message (2 bytes) with a tag ID that we increment with every command
1656 * until it wraps back to 0.
1658 * XXX - it turns out that there are some broken SCSI-II devices,
1659 * which claim to support tagged queuing but fail when more than
1660 * some number of commands are issued at once.
1663 /* Wait for start of REQ/ACK handshake */
1664 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
))
1667 dprintk(NDEBUG_SELECTION
, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
1668 HOSTNO
, cmd
->device
->id
);
1669 tmp
[0] = IDENTIFY(1, cmd
->device
->lun
);
1672 if (cmd
->tag
!= TAG_NONE
) {
1673 tmp
[1] = hostdata
->last_message
= SIMPLE_QUEUE_TAG
;
1681 #endif /* SUPPORT_TAGS */
1683 /* Send message(s) */
1685 phase
= PHASE_MSGOUT
;
1686 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1687 dprintk(NDEBUG_SELECTION
, "scsi%d: nexus established.\n", HOSTNO
);
1688 /* XXX need to handle errors here */
1689 hostdata
->connected
= cmd
;
1690 #ifndef SUPPORT_TAGS
1691 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
1693 #ifdef SUN3_SCSI_VME
1694 dregs
->csr
|= CSR_INTR
;
1697 initialize_SCp(cmd
);
1703 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1704 * unsigned char *phase, int *count, unsigned char **data)
1706 * Purpose : transfers data in given phase using polled I/O
1708 * Inputs : instance - instance of driver, *phase - pointer to
1709 * what phase is expected, *count - pointer to number of
1710 * bytes to transfer, **data - pointer to data pointer.
1712 * Returns : -1 when different phase is entered without transferring
1713 * maximum number of bytes, 0 if all bytes are transferred or exit
1716 * Also, *phase, *count, *data are modified in place.
1718 * XXX Note : handling for bus free may be useful.
1722 * Note : this code is not as quick as it could be, however it
1723 * IS 100% reliable, and for the actual data transfer where speed
1724 * counts, we will always do a pseudo DMA or DMA transfer.
1727 static int NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1728 unsigned char *phase
, int *count
,
1729 unsigned char **data
)
1731 register unsigned char p
= *phase
, tmp
;
1732 register int c
= *count
;
1733 register unsigned char *d
= *data
;
1736 * The NCR5380 chip will only drive the SCSI bus when the
1737 * phase specified in the appropriate bits of the TARGET COMMAND
1738 * REGISTER match the STATUS REGISTER
1741 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1745 * Wait for assertion of REQ, after which the phase bits will be
1748 while (!((tmp
= NCR5380_read(STATUS_REG
)) & SR_REQ
))
1751 dprintk(NDEBUG_HANDSHAKE
, "scsi%d: REQ detected\n", HOSTNO
);
1753 /* Check for phase mismatch */
1754 if ((tmp
& PHASE_MASK
) != p
) {
1755 dprintk(NDEBUG_PIO
, "scsi%d: phase mismatch\n", HOSTNO
);
1756 NCR5380_dprint_phase(NDEBUG_PIO
, instance
);
1760 /* Do actual transfer from SCSI bus to / from memory */
1762 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1764 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1769 * The SCSI standard suggests that in MSGOUT phase, the initiator
1770 * should drop ATN on the last byte of the message phase
1771 * after REQ has been asserted for the handshake but before
1772 * the initiator raises ACK.
1776 if (!((p
& SR_MSG
) && c
> 1)) {
1777 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1778 NCR5380_dprint(NDEBUG_PIO
, instance
);
1779 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1780 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1782 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1783 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1784 NCR5380_dprint(NDEBUG_PIO
, instance
);
1785 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1786 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1789 NCR5380_dprint(NDEBUG_PIO
, instance
);
1790 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1793 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1796 dprintk(NDEBUG_HANDSHAKE
, "scsi%d: req false, handshake complete\n", HOSTNO
);
1799 * We have several special cases to consider during REQ/ACK handshaking :
1800 * 1. We were in MSGOUT phase, and we are on the last byte of the
1801 * message. ATN must be dropped as ACK is dropped.
1803 * 2. We are in a MSGIN phase, and we are on the last byte of the
1804 * message. We must exit with ACK asserted, so that the calling
1805 * code may raise ATN before dropping ACK to reject the message.
1807 * 3. ACK and ATN are clear and the target may proceed as normal.
1809 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1810 if (p
== PHASE_MSGOUT
&& c
> 1)
1811 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1813 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1817 dprintk(NDEBUG_PIO
, "scsi%d: residual %d\n", HOSTNO
, c
);
1821 tmp
= NCR5380_read(STATUS_REG
);
1822 /* The phase read from the bus is valid if either REQ is (already)
1823 * asserted or if ACK hasn't been released yet. The latter is the case if
1824 * we're in MSGIN and all wanted bytes have been received.
1826 if ((tmp
& SR_REQ
) || (p
== PHASE_MSGIN
&& c
== 0))
1827 *phase
= tmp
& PHASE_MASK
;
1829 *phase
= PHASE_UNKNOWN
;
1831 if (!c
|| (*phase
== p
))
1838 * do_reset - issue a reset command
1839 * @instance: adapter to reset
1841 * Issue a reset sequence to the NCR5380 and try and get the bus
1842 * back into sane shape.
1844 * This clears the reset interrupt flag because there may be no handler for
1845 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1846 * been installed. And when in EH we may have released the ST DMA interrupt.
1849 static void do_reset(struct Scsi_Host
*instance
)
1851 unsigned long flags
;
1853 local_irq_save(flags
);
1854 NCR5380_write(TARGET_COMMAND_REG
,
1855 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1856 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1858 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1859 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1860 local_irq_restore(flags
);
1864 * Function : do_abort (Scsi_Host *host)
1866 * Purpose : abort the currently established nexus. Should only be
1867 * called from a routine which can drop into a
1869 * Returns : 0 on success, -1 on failure.
1872 static int do_abort(struct Scsi_Host
*instance
)
1874 unsigned char tmp
, *msgptr
, phase
;
1877 /* Request message out phase */
1878 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1881 * Wait for the target to indicate a valid phase by asserting
1882 * REQ. Once this happens, we'll have either a MSGOUT phase
1883 * and can immediately send the ABORT message, or we'll have some
1884 * other phase and will have to source/sink data.
1886 * We really don't care what value was on the bus or what value
1887 * the target sees, so we just handshake.
1890 while (!((tmp
= NCR5380_read(STATUS_REG
)) & SR_REQ
))
1893 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1895 if ((tmp
& PHASE_MASK
) != PHASE_MSGOUT
) {
1896 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1898 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1900 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1906 phase
= PHASE_MSGOUT
;
1907 NCR5380_transfer_pio(instance
, &phase
, &len
, &msgptr
);
1910 * If we got here, and the command completed successfully,
1911 * we're about to go into bus free state.
1914 return len
? -1 : 0;
1917 #if defined(REAL_DMA)
1919 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1920 * unsigned char *phase, int *count, unsigned char **data)
1922 * Purpose : transfers data in given phase using either real
1925 * Inputs : instance - instance of driver, *phase - pointer to
1926 * what phase is expected, *count - pointer to number of
1927 * bytes to transfer, **data - pointer to data pointer.
1929 * Returns : -1 when different phase is entered without transferring
1930 * maximum number of bytes, 0 if all bytes or transferred or exit
1933 * Also, *phase, *count, *data are modified in place.
1938 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1939 unsigned char *phase
, int *count
,
1940 unsigned char **data
)
1942 SETUP_HOSTDATA(instance
);
1943 register int c
= *count
;
1944 register unsigned char p
= *phase
;
1945 unsigned long flags
;
1947 #if defined(CONFIG_SUN3)
1949 if (!sun3_dma_setup_done
) {
1950 pr_err("scsi%d: transfer_dma without setup!\n",
1954 hostdata
->dma_len
= c
;
1956 dprintk(NDEBUG_DMA
, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1957 instance
->host_no
, (p
& SR_IO
) ? "reading" : "writing",
1958 c
, (p
& SR_IO
) ? "to" : "from", *data
);
1960 /* netbsd turns off ints here, why not be safe and do it too */
1961 local_irq_save(flags
);
1963 /* send start chain */
1964 sun3scsi_dma_start(c
, *data
);
1967 NCR5380_write(TARGET_COMMAND_REG
, 1);
1968 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1969 NCR5380_write(INITIATOR_COMMAND_REG
, 0);
1970 NCR5380_write(MODE_REG
,
1971 (NCR5380_read(MODE_REG
) | MR_DMA_MODE
| MR_ENABLE_EOP_INTR
));
1972 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1974 NCR5380_write(TARGET_COMMAND_REG
, 0);
1975 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1976 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_ASSERT_DATA
);
1977 NCR5380_write(MODE_REG
,
1978 (NCR5380_read(MODE_REG
) | MR_DMA_MODE
| MR_ENABLE_EOP_INTR
));
1979 NCR5380_write(START_DMA_SEND_REG
, 0);
1982 #ifdef SUN3_SCSI_VME
1983 dregs
->csr
|= CSR_DMA_ENABLE
;
1986 local_irq_restore(flags
);
1988 sun3_dma_active
= 1;
1990 #else /* !defined(CONFIG_SUN3) */
1991 register unsigned char *d
= *data
;
1994 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1999 if (hostdata
->read_overruns
&& (p
& SR_IO
))
2000 c
-= hostdata
->read_overruns
;
2002 dprintk(NDEBUG_DMA
, "scsi%d: initializing DMA for %s, %d bytes %s %p\n",
2003 HOSTNO
, (p
& SR_IO
) ? "reading" : "writing",
2004 c
, (p
& SR_IO
) ? "to" : "from", d
);
2006 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
2009 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_ENABLE_EOP_INTR
| MR_MONITOR_BSY
);
2010 #endif /* def REAL_DMA */
2012 if (!(hostdata
->flags
& FLAG_LATE_DMA_SETUP
)) {
2013 /* On the Medusa, it is a must to initialize the DMA before
2014 * starting the NCR. This is also the cleaner way for the TT.
2016 local_irq_save(flags
);
2017 hostdata
->dma_len
= (p
& SR_IO
) ?
2018 NCR5380_dma_read_setup(instance
, d
, c
) :
2019 NCR5380_dma_write_setup(instance
, d
, c
);
2020 local_irq_restore(flags
);
2024 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
2026 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
2027 NCR5380_write(START_DMA_SEND_REG
, 0);
2030 if (hostdata
->flags
& FLAG_LATE_DMA_SETUP
) {
2031 /* On the Falcon, the DMA setup must be done after the last */
2032 /* NCR access, else the DMA setup gets trashed!
2034 local_irq_save(flags
);
2035 hostdata
->dma_len
= (p
& SR_IO
) ?
2036 NCR5380_dma_read_setup(instance
, d
, c
) :
2037 NCR5380_dma_write_setup(instance
, d
, c
);
2038 local_irq_restore(flags
);
2040 #endif /* !defined(CONFIG_SUN3) */
2044 #endif /* defined(REAL_DMA) */
2047 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2049 * Purpose : run through the various SCSI phases and do as the target
2050 * directs us to. Operates on the currently connected command,
2051 * instance->connected.
2053 * Inputs : instance, instance for which we are doing commands
2055 * Side effects : SCSI things happen, the disconnected queue will be
2056 * modified if a command disconnects, *instance->connected will
2059 * XXX Note : we need to watch for bus free or a reset condition here
2060 * to recover from an unexpected bus free condition.
2063 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
2065 SETUP_HOSTDATA(instance
);
2066 unsigned long flags
;
2067 unsigned char msgout
= NOP
;
2070 #if defined(REAL_DMA)
2073 unsigned char *data
;
2074 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
2075 struct scsi_cmnd
*cmd
= (struct scsi_cmnd
*) hostdata
->connected
;
2077 #ifdef SUN3_SCSI_VME
2078 dregs
->csr
|= CSR_INTR
;
2082 tmp
= NCR5380_read(STATUS_REG
);
2083 /* We only have a valid SCSI phase when REQ is asserted */
2085 phase
= (tmp
& PHASE_MASK
);
2086 if (phase
!= old_phase
) {
2088 NCR5380_dprint_phase(NDEBUG_INFORMATION
, instance
);
2090 #if defined(CONFIG_SUN3)
2091 if (phase
== PHASE_CMDOUT
) {
2092 #if defined(REAL_DMA)
2094 unsigned long count
;
2096 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
2097 count
= cmd
->SCp
.buffer
->length
;
2098 d
= sg_virt(cmd
->SCp
.buffer
);
2100 count
= cmd
->SCp
.this_residual
;
2103 /* this command setup for dma yet? */
2104 if ((count
>= DMA_MIN_SIZE
) && (sun3_dma_setup_done
!= cmd
)) {
2105 if (cmd
->request
->cmd_type
== REQ_TYPE_FS
) {
2106 sun3scsi_dma_setup(d
, count
,
2107 rq_data_dir(cmd
->request
));
2108 sun3_dma_setup_done
= cmd
;
2112 #ifdef SUN3_SCSI_VME
2113 dregs
->csr
|= CSR_INTR
;
2116 #endif /* CONFIG_SUN3 */
2118 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
2119 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
2121 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
2123 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
2125 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
2133 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2134 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2135 "aborted\n", HOSTNO
);
2138 cmd
->result
= DID_ERROR
<< 16;
2139 cmd
->scsi_done(cmd
);
2144 * If there is no room left in the current buffer in the
2145 * scatter-gather list, move onto the next one.
2148 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
2150 --cmd
->SCp
.buffers_residual
;
2151 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
2152 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
2153 /* ++roman: Try to merge some scatter-buffers if
2154 * they are at contiguous physical addresses.
2156 merge_contiguous_buffers(cmd
);
2157 dprintk(NDEBUG_INFORMATION
, "scsi%d: %d bytes and %d buffers left\n",
2158 HOSTNO
, cmd
->SCp
.this_residual
,
2159 cmd
->SCp
.buffers_residual
);
2163 * The preferred transfer method is going to be
2164 * PSEUDO-DMA for systems that are strictly PIO,
2165 * since we can let the hardware do the handshaking.
2167 * For this to work, we need to know the transfersize
2168 * ahead of time, since the pseudo-DMA code will sit
2169 * in an unconditional loop.
2172 /* ++roman: I suggest, this should be
2174 * instead of leaving REAL_DMA out.
2177 #if defined(REAL_DMA)
2179 #if !defined(CONFIG_SUN3)
2180 !cmd
->device
->borken
&&
2182 (transfersize
= NCR5380_dma_xfer_len(instance
, cmd
, phase
)) >= DMA_MIN_SIZE
) {
2184 cmd
->SCp
.phase
= phase
;
2185 if (NCR5380_transfer_dma(instance
, &phase
,
2186 &len
, (unsigned char **)&cmd
->SCp
.ptr
)) {
2188 * If the watchdog timer fires, all future
2189 * accesses to this device will use the
2191 scmd_printk(KERN_INFO
, cmd
,
2192 "switching to slow handshake\n");
2193 cmd
->device
->borken
= 1;
2196 cmd
->result
= DID_ERROR
<< 16;
2197 cmd
->scsi_done(cmd
);
2198 /* XXX - need to source or sink data here, as appropriate */
2201 /* ++roman: When using real DMA,
2202 * information_transfer() should return after
2203 * starting DMA since it has nothing more to
2208 cmd
->SCp
.this_residual
-= transfersize
- len
;
2212 #endif /* defined(REAL_DMA) */
2213 NCR5380_transfer_pio(instance
, &phase
,
2214 (int *)&cmd
->SCp
.this_residual
,
2215 (unsigned char **)&cmd
->SCp
.ptr
);
2216 #if defined(CONFIG_SUN3) && defined(REAL_DMA)
2217 /* if we had intended to dma that command clear it */
2218 if (sun3_dma_setup_done
== cmd
)
2219 sun3_dma_setup_done
= NULL
;
2225 NCR5380_write(SELECT_ENABLE_REG
, 0); /* disable reselects */
2226 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2227 cmd
->SCp
.Message
= tmp
;
2231 * Linking lets us reduce the time required to get the
2232 * next command out to the device, hopefully this will
2233 * mean we don't waste another revolution due to the delays
2234 * required by ARBITRATION and another SELECTION.
2236 * In the current implementation proposal, low level drivers
2237 * merely have to start the next command, pointed to by
2238 * next_link, done() is called as with unlinked commands.
2241 case LINKED_CMD_COMPLETE
:
2242 case LINKED_FLG_CMD_COMPLETE
:
2243 /* Accept message by clearing ACK */
2244 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2246 dprintk(NDEBUG_LINKED
, "scsi%d: target %d lun %llu linked command "
2247 "complete.\n", HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2249 /* Enable reselect interrupts */
2250 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2252 * Sanity check : A linked command should only terminate
2253 * with one of these messages if there are more linked
2254 * commands available.
2257 if (!cmd
->next_link
) {
2258 printk(KERN_NOTICE
"scsi%d: target %d lun %llu "
2259 "linked command complete, no next_link\n",
2260 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2266 initialize_SCp(cmd
->next_link
);
2267 /* The next command is still part of this process; copy it
2268 * and don't free it! */
2269 cmd
->next_link
->tag
= cmd
->tag
;
2270 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
2271 dprintk(NDEBUG_LINKED
, "scsi%d: target %d lun %llu linked request "
2272 "done, calling scsi_done().\n",
2273 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2274 cmd
->scsi_done(cmd
);
2275 cmd
= hostdata
->connected
;
2277 #endif /* def LINKED */
2279 case COMMAND_COMPLETE
:
2280 /* Accept message by clearing ACK */
2281 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2282 dprintk(NDEBUG_QUEUES
, "scsi%d: command for target %d, lun %llu "
2283 "completed\n", HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2285 local_irq_save(flags
);
2286 hostdata
->retain_dma_intr
++;
2287 hostdata
->connected
= NULL
;
2290 if (status_byte(cmd
->SCp
.Status
) == QUEUE_FULL
) {
2291 /* Turn a QUEUE FULL status into BUSY, I think the
2292 * mid level cannot handle QUEUE FULL :-( (The
2293 * command is retried after BUSY). Also update our
2294 * queue size to the number of currently issued
2297 /* ++Andreas: the mid level code knows about
2299 struct tag_alloc
*ta
= &hostdata
->TagAlloc
[scmd_id(cmd
)][cmd
->device
->lun
];
2300 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %llu returned "
2301 "QUEUE_FULL after %d commands\n",
2302 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
,
2304 if (ta
->queue_size
> ta
->nr_allocated
)
2305 ta
->nr_allocated
= ta
->queue_size
;
2308 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2310 /* Enable reselect interrupts */
2311 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2314 * I'm not sure what the correct thing to do here is :
2316 * If the command that just executed is NOT a request
2317 * sense, the obvious thing to do is to set the result
2318 * code to the values of the stored parameters.
2320 * If it was a REQUEST SENSE command, we need some way to
2321 * differentiate between the failure code of the original
2322 * and the failure code of the REQUEST sense - the obvious
2323 * case is success, where we fall through and leave the
2324 * result code unchanged.
2326 * The non-obvious place is where the REQUEST SENSE failed
2329 if (cmd
->cmnd
[0] != REQUEST_SENSE
)
2330 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
2331 else if (status_byte(cmd
->SCp
.Status
) != GOOD
)
2332 cmd
->result
= (cmd
->result
& 0x00ffff) | (DID_ERROR
<< 16);
2334 if ((cmd
->cmnd
[0] == REQUEST_SENSE
) &&
2335 hostdata
->ses
.cmd_len
) {
2336 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
2337 hostdata
->ses
.cmd_len
= 0 ;
2340 if ((cmd
->cmnd
[0] != REQUEST_SENSE
) &&
2341 (status_byte(cmd
->SCp
.Status
) == CHECK_CONDITION
)) {
2342 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
2344 dprintk(NDEBUG_AUTOSENSE
, "scsi%d: performing request sense\n", HOSTNO
);
2346 LIST(cmd
,hostdata
->issue_queue
);
2347 SET_NEXT(cmd
, hostdata
->issue_queue
);
2348 hostdata
->issue_queue
= (struct scsi_cmnd
*) cmd
;
2349 dprintk(NDEBUG_QUEUES
, "scsi%d: REQUEST SENSE added to head of "
2350 "issue queue\n", H_NO(cmd
));
2352 cmd
->scsi_done(cmd
);
2355 local_irq_restore(flags
);
2357 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2359 * Restore phase bits to 0 so an interrupted selection,
2360 * arbitration can resume.
2362 NCR5380_write(TARGET_COMMAND_REG
, 0);
2364 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2367 local_irq_save(flags
);
2368 hostdata
->retain_dma_intr
--;
2369 /* ++roman: For Falcon SCSI, release the lock on the
2370 * ST-DMA here if no other commands are waiting on the
2371 * disconnected queue.
2373 maybe_release_dma_irq(instance
);
2374 local_irq_restore(flags
);
2376 case MESSAGE_REJECT
:
2377 /* Accept message by clearing ACK */
2378 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2379 /* Enable reselect interrupts */
2380 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2381 switch (hostdata
->last_message
) {
2382 case HEAD_OF_QUEUE_TAG
:
2383 case ORDERED_QUEUE_TAG
:
2384 case SIMPLE_QUEUE_TAG
:
2385 /* The target obviously doesn't support tagged
2386 * queuing, even though it announced this ability in
2387 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2388 * clear 'tagged_supported' and lock the LUN, since
2389 * the command is treated as untagged further on.
2391 cmd
->device
->tagged_supported
= 0;
2392 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
2393 cmd
->tag
= TAG_NONE
;
2394 dprintk(NDEBUG_TAGS
, "scsi%d: target %d lun %llu rejected "
2395 "QUEUE_TAG message; tagged queuing "
2397 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2402 /* Accept message by clearing ACK */
2403 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2404 local_irq_save(flags
);
2405 cmd
->device
->disconnect
= 1;
2406 LIST(cmd
,hostdata
->disconnected_queue
);
2407 SET_NEXT(cmd
, hostdata
->disconnected_queue
);
2408 hostdata
->connected
= NULL
;
2409 hostdata
->disconnected_queue
= cmd
;
2410 local_irq_restore(flags
);
2411 dprintk(NDEBUG_QUEUES
, "scsi%d: command for target %d lun %llu was "
2412 "moved from connected to the "
2413 "disconnected_queue\n", HOSTNO
,
2414 cmd
->device
->id
, cmd
->device
->lun
);
2416 * Restore phase bits to 0 so an interrupted selection,
2417 * arbitration can resume.
2419 NCR5380_write(TARGET_COMMAND_REG
, 0);
2421 /* Enable reselect interrupts */
2422 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2423 /* Wait for bus free to avoid nasty timeouts */
2424 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2426 #ifdef SUN3_SCSI_VME
2427 dregs
->csr
|= CSR_DMA_ENABLE
;
2431 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2432 * operation, in violation of the SCSI spec so we can safely
2433 * ignore SAVE/RESTORE pointers calls.
2435 * Unfortunately, some disks violate the SCSI spec and
2436 * don't issue the required SAVE_POINTERS message before
2437 * disconnecting, and we have to break spec to remain
2441 case RESTORE_POINTERS
:
2442 /* Accept message by clearing ACK */
2443 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2444 /* Enable reselect interrupts */
2445 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2447 case EXTENDED_MESSAGE
:
2449 * Extended messages are sent in the following format :
2451 * 0 EXTENDED_MESSAGE == 1
2452 * 1 length (includes one byte for code, doesn't
2453 * include first two bytes)
2455 * 3..length+1 arguments
2457 * Start the extended message buffer with the EXTENDED_MESSAGE
2458 * byte, since spi_print_msg() wants the whole thing.
2460 extended_msg
[0] = EXTENDED_MESSAGE
;
2461 /* Accept first byte by clearing ACK */
2462 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2464 dprintk(NDEBUG_EXTENDED
, "scsi%d: receiving extended message\n", HOSTNO
);
2467 data
= extended_msg
+ 1;
2468 phase
= PHASE_MSGIN
;
2469 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2470 dprintk(NDEBUG_EXTENDED
, "scsi%d: length=%d, code=0x%02x\n", HOSTNO
,
2471 (int)extended_msg
[1], (int)extended_msg
[2]);
2473 if (!len
&& extended_msg
[1] <=
2474 (sizeof(extended_msg
) - 1)) {
2475 /* Accept third byte by clearing ACK */
2476 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2477 len
= extended_msg
[1] - 1;
2478 data
= extended_msg
+ 3;
2479 phase
= PHASE_MSGIN
;
2481 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2482 dprintk(NDEBUG_EXTENDED
, "scsi%d: message received, residual %d\n",
2485 switch (extended_msg
[2]) {
2488 case EXTENDED_MODIFY_DATA_POINTER
:
2489 case EXTENDED_EXTENDED_IDENTIFY
:
2493 printk(KERN_NOTICE
"scsi%d: error receiving "
2494 "extended message\n", HOSTNO
);
2497 printk(KERN_NOTICE
"scsi%d: extended message "
2498 "code %02x length %d is too long\n",
2499 HOSTNO
, extended_msg
[2], extended_msg
[1]);
2502 /* Fall through to reject message */
2505 * If we get something weird that we aren't expecting,
2510 printk(KERN_INFO
"scsi%d: rejecting message ",
2512 spi_print_msg(extended_msg
);
2514 } else if (tmp
!= EXTENDED_MESSAGE
)
2515 scmd_printk(KERN_INFO
, cmd
,
2516 "rejecting unknown message %02x\n",
2519 scmd_printk(KERN_INFO
, cmd
,
2520 "rejecting unknown extended message code %02x, length %d\n",
2521 extended_msg
[1], extended_msg
[0]);
2523 msgout
= MESSAGE_REJECT
;
2524 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
2526 } /* switch (tmp) */
2531 hostdata
->last_message
= msgout
;
2532 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2533 if (msgout
== ABORT
) {
2534 local_irq_save(flags
);
2538 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2540 hostdata
->connected
= NULL
;
2541 cmd
->result
= DID_ERROR
<< 16;
2542 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2543 maybe_release_dma_irq(instance
);
2544 local_irq_restore(flags
);
2545 cmd
->scsi_done(cmd
);
2554 * XXX for performance reasons, on machines with a
2555 * PSEUDO-DMA architecture we should probably
2556 * use the dma transfer function.
2558 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2563 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2564 cmd
->SCp
.Status
= tmp
;
2567 printk("scsi%d: unknown phase\n", HOSTNO
);
2568 NCR5380_dprint(NDEBUG_ANY
, instance
);
2569 } /* switch(phase) */
2570 } /* if (tmp * SR_REQ) */
2575 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2577 * Purpose : does reselection, initializing the instance->connected
2578 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2579 * nexus has been reestablished,
2581 * Inputs : instance - this instance of the NCR5380.
2586 /* it might eventually prove necessary to do a dma setup on
2587 reselection, but it doesn't seem to be needed now -- sam */
2589 static void NCR5380_reselect(struct Scsi_Host
*instance
)
2591 SETUP_HOSTDATA(instance
);
2592 unsigned char target_mask
;
2597 unsigned char msg
[3];
2598 int __maybe_unused len
;
2599 unsigned char __maybe_unused
*data
, __maybe_unused phase
;
2600 struct scsi_cmnd
*tmp
= NULL
, *prev
;
2603 * Disable arbitration, etc. since the host adapter obviously
2604 * lost, and tell an interrupted NCR5380_select() to restart.
2607 NCR5380_write(MODE_REG
, MR_BASE
);
2608 hostdata
->restart_select
= 1;
2610 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2612 dprintk(NDEBUG_RESELECTION
, "scsi%d: reselect\n", HOSTNO
);
2615 * At this point, we have detected that our SCSI ID is on the bus,
2616 * SEL is true and BSY was false for at least one bus settle delay
2619 * We must assert BSY ourselves, until the target drops the SEL
2623 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2625 while (NCR5380_read(STATUS_REG
) & SR_SEL
)
2627 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2630 * Wait for target to go into MSGIN.
2633 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
))
2636 #if defined(CONFIG_SUN3) && defined(REAL_DMA)
2637 /* acknowledge toggle to MSGIN */
2638 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(PHASE_MSGIN
));
2640 /* peek at the byte without really hitting the bus */
2641 msg
[0] = NCR5380_read(CURRENT_SCSI_DATA_REG
);
2645 phase
= PHASE_MSGIN
;
2646 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2649 if (!(msg
[0] & 0x80)) {
2650 printk(KERN_DEBUG
"scsi%d: expecting IDENTIFY message, got ", HOSTNO
);
2655 lun
= (msg
[0] & 0x07);
2657 #if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
2658 /* If the phase is still MSGIN, the target wants to send some more
2659 * messages. In case it supports tagged queuing, this is probably a
2660 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2663 if (phase
== PHASE_MSGIN
&& (hostdata
->flags
& FLAG_TAGGED_QUEUING
)) {
2664 /* Accept previous IDENTIFY message by clearing ACK */
2665 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2668 if (!NCR5380_transfer_pio(instance
, &phase
, &len
, &data
) &&
2669 msg
[1] == SIMPLE_QUEUE_TAG
)
2671 dprintk(NDEBUG_TAGS
, "scsi%d: target mask %02x, lun %d sent tag %d at "
2672 "reselection\n", HOSTNO
, target_mask
, lun
, tag
);
2677 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2678 * just reestablished, and remove it from the disconnected queue.
2681 for (tmp
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
, prev
= NULL
;
2682 tmp
; prev
= tmp
, tmp
= NEXT(tmp
)) {
2683 if ((target_mask
== (1 << tmp
->device
->id
)) && (lun
== tmp
->device
->lun
)
2685 && (tag
== tmp
->tag
)
2689 REMOVE(prev
, NEXT(prev
), tmp
, NEXT(tmp
));
2690 SET_NEXT(prev
, NEXT(tmp
));
2692 REMOVE(-1, hostdata
->disconnected_queue
, tmp
, NEXT(tmp
));
2693 hostdata
->disconnected_queue
= NEXT(tmp
);
2695 SET_NEXT(tmp
, NULL
);
2701 printk(KERN_WARNING
"scsi%d: warning: target bitmask %02x lun %d "
2705 "not in disconnected_queue.\n",
2706 HOSTNO
, target_mask
, lun
2712 * Since we have an established nexus that we can't do anything
2713 * with, we must abort it.
2719 #if defined(CONFIG_SUN3) && defined(REAL_DMA)
2720 /* engage dma setup for the command we just saw */
2723 unsigned long count
;
2725 if (!tmp
->SCp
.this_residual
&& tmp
->SCp
.buffers_residual
) {
2726 count
= tmp
->SCp
.buffer
->length
;
2727 d
= sg_virt(tmp
->SCp
.buffer
);
2729 count
= tmp
->SCp
.this_residual
;
2732 /* setup this command for dma if not already */
2733 if ((count
>= DMA_MIN_SIZE
) && (sun3_dma_setup_done
!= tmp
)) {
2734 sun3scsi_dma_setup(d
, count
, rq_data_dir(tmp
->request
));
2735 sun3_dma_setup_done
= tmp
;
2739 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
2742 /* Accept message by clearing ACK */
2743 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2745 #if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2746 /* If the phase is still MSGIN, the target wants to send some more
2747 * messages. In case it supports tagged queuing, this is probably a
2748 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2751 if (phase
== PHASE_MSGIN
&& setup_use_tagged_queuing
) {
2752 /* Accept previous IDENTIFY message by clearing ACK */
2753 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2756 if (!NCR5380_transfer_pio(instance
, &phase
, &len
, &data
) &&
2757 msg
[1] == SIMPLE_QUEUE_TAG
)
2759 dprintk(NDEBUG_TAGS
, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n"
2760 HOSTNO
, target_mask
, lun
, tag
);
2764 hostdata
->connected
= tmp
;
2765 dprintk(NDEBUG_RESELECTION
, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n",
2766 HOSTNO
, tmp
->device
->id
, tmp
->device
->lun
, tmp
->tag
);
2771 * Function : int NCR5380_abort (struct scsi_cmnd *cmd)
2773 * Purpose : abort a command
2775 * Inputs : cmd - the scsi_cmnd to abort, code - code to set the
2776 * host byte of the result field to, if zero DID_ABORTED is
2779 * Returns : SUCCESS - success, FAILED on failure.
2781 * XXX - there is no way to abort the command that is currently
2782 * connected, you have to wait for it to complete. If this is
2783 * a problem, we could implement longjmp() / setjmp(), setjmp()
2784 * called where the loop started in NCR5380_main().
2788 int NCR5380_abort(struct scsi_cmnd
*cmd
)
2790 struct Scsi_Host
*instance
= cmd
->device
->host
;
2791 SETUP_HOSTDATA(instance
);
2792 struct scsi_cmnd
*tmp
, **prev
;
2793 unsigned long flags
;
2795 scmd_printk(KERN_NOTICE
, cmd
, "aborting command\n");
2797 NCR5380_print_status(instance
);
2799 local_irq_save(flags
);
2801 dprintk(NDEBUG_ABORT
, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO
,
2802 NCR5380_read(BUS_AND_STATUS_REG
),
2803 NCR5380_read(STATUS_REG
));
2807 * Case 1 : If the command is the currently executing command,
2808 * we'll set the aborted flag and return control so that
2809 * information transfer routine can exit cleanly.
2812 if (hostdata
->connected
== cmd
) {
2814 dprintk(NDEBUG_ABORT
, "scsi%d: aborting connected command\n", HOSTNO
);
2816 * We should perform BSY checking, and make sure we haven't slipped
2820 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2822 * Since we can't change phases until we've completed the current
2823 * handshake, we have to source or sink a byte of data if the current
2824 * phase is not MSGOUT.
2828 * Return control to the executing NCR drive so we can clear the
2829 * aborted flag and get back into our main loop.
2832 if (do_abort(instance
) == 0) {
2833 hostdata
->connected
= NULL
;
2834 cmd
->result
= DID_ABORT
<< 16;
2838 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2840 maybe_release_dma_irq(instance
);
2841 local_irq_restore(flags
);
2842 cmd
->scsi_done(cmd
);
2845 local_irq_restore(flags
);
2846 printk("scsi%d: abort of connected command failed!\n", HOSTNO
);
2853 * Case 2 : If the command hasn't been issued yet, we simply remove it
2854 * from the issue queue.
2856 for (prev
= (struct scsi_cmnd
**)&(hostdata
->issue_queue
),
2857 tmp
= (struct scsi_cmnd
*)hostdata
->issue_queue
;
2858 tmp
; prev
= NEXTADDR(tmp
), tmp
= NEXT(tmp
)) {
2860 REMOVE(5, *prev
, tmp
, NEXT(tmp
));
2861 (*prev
) = NEXT(tmp
);
2862 SET_NEXT(tmp
, NULL
);
2863 tmp
->result
= DID_ABORT
<< 16;
2864 maybe_release_dma_irq(instance
);
2865 local_irq_restore(flags
);
2866 dprintk(NDEBUG_ABORT
, "scsi%d: abort removed command from issue queue.\n",
2868 /* Tagged queuing note: no tag to free here, hasn't been assigned
2870 tmp
->scsi_done(tmp
);
2876 * Case 3 : If any commands are connected, we're going to fail the abort
2877 * and let the high level SCSI driver retry at a later time or
2880 * Timeouts, and therefore aborted commands, will be highly unlikely
2881 * and handling them cleanly in this situation would make the common
2882 * case of noresets less efficient, and would pollute our code. So,
2886 if (hostdata
->connected
) {
2887 local_irq_restore(flags
);
2888 dprintk(NDEBUG_ABORT
, "scsi%d: abort failed, command connected.\n", HOSTNO
);
2893 * Case 4: If the command is currently disconnected from the bus, and
2894 * there are no connected commands, we reconnect the I_T_L or
2895 * I_T_L_Q nexus associated with it, go into message out, and send
2898 * This case is especially ugly. In order to reestablish the nexus, we
2899 * need to call NCR5380_select(). The easiest way to implement this
2900 * function was to abort if the bus was busy, and let the interrupt
2901 * handler triggered on the SEL for reselect take care of lost arbitrations
2902 * where necessary, meaning interrupts need to be enabled.
2904 * When interrupts are enabled, the queues may change - so we
2905 * can't remove it from the disconnected queue before selecting it
2906 * because that could cause a failure in hashing the nexus if that
2907 * device reselected.
2909 * Since the queues may change, we can't use the pointers from when we
2912 * So, we must first locate the command, and if NCR5380_select()
2913 * succeeds, then issue the abort, relocate the command and remove
2914 * it from the disconnected queue.
2917 for (tmp
= (struct scsi_cmnd
*) hostdata
->disconnected_queue
; tmp
;
2920 local_irq_restore(flags
);
2921 dprintk(NDEBUG_ABORT
, "scsi%d: aborting disconnected command.\n", HOSTNO
);
2923 if (NCR5380_select(instance
, cmd
))
2926 dprintk(NDEBUG_ABORT
, "scsi%d: nexus reestablished.\n", HOSTNO
);
2930 local_irq_save(flags
);
2931 for (prev
= (struct scsi_cmnd
**)&(hostdata
->disconnected_queue
),
2932 tmp
= (struct scsi_cmnd
*)hostdata
->disconnected_queue
;
2933 tmp
; prev
= NEXTADDR(tmp
), tmp
= NEXT(tmp
)) {
2935 REMOVE(5, *prev
, tmp
, NEXT(tmp
));
2937 SET_NEXT(tmp
, NULL
);
2938 tmp
->result
= DID_ABORT
<< 16;
2939 /* We must unlock the tag/LUN immediately here, since the
2940 * target goes to BUS FREE and doesn't send us another
2941 * message (COMMAND_COMPLETE or the like)
2946 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2948 maybe_release_dma_irq(instance
);
2949 local_irq_restore(flags
);
2950 tmp
->scsi_done(tmp
);
2957 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2958 * possible at all) At least, we should check if the lock could be
2959 * released after the abort, in case it is kept due to some bug.
2961 maybe_release_dma_irq(instance
);
2962 local_irq_restore(flags
);
2965 * Case 5 : If we reached this point, the command was not found in any of
2968 * We probably reached this point because of an unlikely race condition
2969 * between the command completing successfully and the abortion code,
2970 * so we won't panic, but we will notify the user in case something really
2974 printk(KERN_INFO
"scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO
);
2981 * NCR5380_bus_reset - reset the SCSI bus
2982 * @cmd: SCSI command undergoing EH
2987 static int NCR5380_bus_reset(struct scsi_cmnd
*cmd
)
2989 struct Scsi_Host
*instance
= cmd
->device
->host
;
2990 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2992 unsigned long flags
;
2994 local_irq_save(flags
);
2996 #if (NDEBUG & NDEBUG_ANY)
2997 scmd_printk(KERN_INFO
, cmd
, "performing bus reset\n");
2998 NCR5380_print_status(instance
);
3003 /* reset NCR registers */
3004 NCR5380_write(MODE_REG
, MR_BASE
);
3005 NCR5380_write(TARGET_COMMAND_REG
, 0);
3006 NCR5380_write(SELECT_ENABLE_REG
, 0);
3008 /* After the reset, there are no more connected or disconnected commands
3009 * and no busy units; so clear the low-level status here to avoid
3010 * conflicts when the mid-level code tries to wake up the affected
3014 if (hostdata
->issue_queue
)
3015 dprintk(NDEBUG_ABORT
, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd
));
3016 if (hostdata
->connected
)
3017 dprintk(NDEBUG_ABORT
, "scsi%d: reset aborted a connected command\n", H_NO(cmd
));
3018 if (hostdata
->disconnected_queue
)
3019 dprintk(NDEBUG_ABORT
, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd
));
3021 hostdata
->issue_queue
= NULL
;
3022 hostdata
->connected
= NULL
;
3023 hostdata
->disconnected_queue
= NULL
;
3025 free_all_tags(hostdata
);
3027 for (i
= 0; i
< 8; ++i
)
3028 hostdata
->busy
[i
] = 0;
3030 hostdata
->dma_len
= 0;
3033 maybe_release_dma_irq(instance
);
3034 local_irq_restore(flags
);