1 // SPDX-License-Identifier: GPL-2.0
3 * NCR 5380 generic driver routines. These should make it *trivial*
4 * to implement 5380 SCSI drivers under Linux with a non-trantor
7 * Note that these routines also work with NR53c400 family chips.
9 * Copyright 1993, Drew Eckhardt
11 * (Unix and Linux consulting and custom programming)
15 * For more information, please consult
18 * SCSI Protocol Controller
21 * NCR Microelectronics
22 * 1635 Aeroplaza Drive
23 * Colorado Springs, CO 80916
29 * With contributions from Ray Van Tassle, Ingmar Baumgart,
30 * Ronald van Cuijlenborg, Alan Cox and others.
33 /* Ported to Atari by Roman Hodek and others. */
35 /* Adapted for the Sun 3 by Sam Creasey. */
40 * This is a generic 5380 driver. To use it on a different platform,
41 * one simply writes appropriate system specific macros (ie, data
42 * transfer - some PC's will use the I/O bus, 68K's must use
43 * memory mapped) and drops this file in their 'C' wrapper.
45 * As far as command queueing, two queues are maintained for
46 * each 5380 in the system - commands that haven't been issued yet,
47 * and commands that are currently executing. This means that an
48 * unlimited number of commands may be queued, letting
49 * more commands propagate from the higher driver levels giving higher
50 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
51 * allowing multiple commands to propagate all the way to a SCSI-II device
52 * while a command is already executing.
55 * Issues specific to the NCR5380 :
57 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
58 * piece of hardware that requires you to sit in a loop polling for
59 * the REQ signal as long as you are connected. Some devices are
60 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
61 * while doing long seek operations. [...] These
62 * broken devices are the exception rather than the rule and I'd rather
63 * spend my time optimizing for the normal case.
67 * At the heart of the design is a coroutine, NCR5380_main,
68 * which is started from a workqueue for each NCR5380 host in the
69 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
70 * removing the commands from the issue queue and calling
71 * NCR5380_select() if a nexus is not established.
73 * Once a nexus is established, the NCR5380_information_transfer()
74 * phase goes through the various phases as instructed by the target.
75 * if the target goes into MSG IN and sends a DISCONNECT message,
76 * the command structure is placed into the per instance disconnected
77 * queue, and NCR5380_main tries to find more work. If the target is
78 * idle for too long, the system will try to sleep.
80 * If a command has disconnected, eventually an interrupt will trigger,
81 * calling NCR5380_intr() which will in turn call NCR5380_reselect
82 * to reestablish a nexus. This will run main if necessary.
84 * On command termination, the done function will be called as
87 * The command data pointer is initialized after the command is connected
88 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
89 * Note that in violation of the standard, an implicit SAVE POINTERS operation
90 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
95 * This file a skeleton Linux SCSI driver for the NCR 5380 series
96 * of chips. To use it, you write an architecture specific functions
97 * and macros and include this file in your driver.
99 * These macros MUST be defined :
101 * NCR5380_read(register) - read from the specified register
103 * NCR5380_write(register, value) - write to the specific register
105 * NCR5380_implementation_fields - additional fields needed for this
106 * specific implementation of the NCR5380
108 * Either real DMA *or* pseudo DMA may be implemented
110 * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer
111 * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
112 * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
113 * NCR5380_dma_residual - residual byte count
115 * The generic driver is initialized by calling NCR5380_init(instance),
116 * after setting the appropriate host specific fields and ID.
119 #ifndef NCR5380_io_delay
120 #define NCR5380_io_delay(x)
123 #ifndef NCR5380_acquire_dma_irq
124 #define NCR5380_acquire_dma_irq(x) (1)
127 #ifndef NCR5380_release_dma_irq
128 #define NCR5380_release_dma_irq(x)
131 static unsigned int disconnect_mask
= ~0;
132 module_param(disconnect_mask
, int, 0444);
134 static int do_abort(struct Scsi_Host
*, unsigned int);
135 static void do_reset(struct Scsi_Host
*);
136 static void bus_reset_cleanup(struct Scsi_Host
*);
139 * initialize_SCp - init the scsi pointer field
140 * @cmd: command block to set up
142 * Set up the internal fields in the SCSI command.
145 static inline void initialize_SCp(struct scsi_cmnd
*cmd
)
147 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(cmd
);
149 if (scsi_bufflen(cmd
)) {
150 ncmd
->buffer
= scsi_sglist(cmd
);
151 ncmd
->ptr
= sg_virt(ncmd
->buffer
);
152 ncmd
->this_residual
= ncmd
->buffer
->length
;
156 ncmd
->this_residual
= 0;
162 static inline void advance_sg_buffer(struct NCR5380_cmd
*ncmd
)
164 struct scatterlist
*s
= ncmd
->buffer
;
166 if (!ncmd
->this_residual
&& s
&& !sg_is_last(s
)) {
167 ncmd
->buffer
= sg_next(s
);
168 ncmd
->ptr
= sg_virt(ncmd
->buffer
);
169 ncmd
->this_residual
= ncmd
->buffer
->length
;
173 static inline void set_resid_from_SCp(struct scsi_cmnd
*cmd
)
175 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(cmd
);
176 int resid
= ncmd
->this_residual
;
177 struct scatterlist
*s
= ncmd
->buffer
;
180 while (!sg_is_last(s
)) {
184 scsi_set_resid(cmd
, resid
);
188 * NCR5380_poll_politely2 - wait for two chip register values
189 * @hostdata: host private data
190 * @reg1: 5380 register to poll
191 * @bit1: Bitmask to check
192 * @val1: Expected value
193 * @reg2: Second 5380 register to poll
194 * @bit2: Second bitmask to check
195 * @val2: Second expected value
196 * @wait: Time-out in jiffies, 0 if sleeping is not allowed
198 * Polls the chip in a reasonably efficient manner waiting for an
199 * event to occur. After a short quick poll we begin to yield the CPU
200 * (if possible). In irq contexts the time-out is arbitrarily limited.
202 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
205 static int NCR5380_poll_politely2(struct NCR5380_hostdata
*hostdata
,
206 unsigned int reg1
, u8 bit1
, u8 val1
,
207 unsigned int reg2
, u8 bit2
, u8 val2
,
210 unsigned long n
= hostdata
->poll_loops
;
211 unsigned long deadline
= jiffies
+ wait
;
214 if ((NCR5380_read(reg1
) & bit1
) == val1
)
216 if ((NCR5380_read(reg2
) & bit2
) == val2
)
224 /* Repeatedly sleep for 1 ms until deadline */
225 while (time_is_after_jiffies(deadline
)) {
226 schedule_timeout_uninterruptible(1);
227 if ((NCR5380_read(reg1
) & bit1
) == val1
)
229 if ((NCR5380_read(reg2
) & bit2
) == val2
)
252 {BASR_END_DMA_TRANSFER
, "END OF DMA"},
254 {BASR_PARITY_ERROR
, "PARITY ERROR"},
256 {BASR_PHASE_MATCH
, "PHASE MATCH"},
257 {BASR_BUSY_ERROR
, "BUSY ERROR"},
263 {ICR_ASSERT_RST
, "ASSERT RST"},
264 {ICR_ARBITRATION_PROGRESS
, "ARB. IN PROGRESS"},
265 {ICR_ARBITRATION_LOST
, "LOST ARB."},
266 {ICR_ASSERT_ACK
, "ASSERT ACK"},
267 {ICR_ASSERT_BSY
, "ASSERT BSY"},
268 {ICR_ASSERT_SEL
, "ASSERT SEL"},
269 {ICR_ASSERT_ATN
, "ASSERT ATN"},
270 {ICR_ASSERT_DATA
, "ASSERT DATA"},
274 {MR_BLOCK_DMA_MODE
, "BLOCK DMA MODE"},
275 {MR_TARGET
, "TARGET"},
276 {MR_ENABLE_PAR_CHECK
, "PARITY CHECK"},
277 {MR_ENABLE_PAR_INTR
, "PARITY INTR"},
278 {MR_ENABLE_EOP_INTR
, "EOP INTR"},
279 {MR_MONITOR_BSY
, "MONITOR BSY"},
280 {MR_DMA_MODE
, "DMA MODE"},
281 {MR_ARBITRATE
, "ARBITRATE"},
286 * NCR5380_print - print scsi bus signals
287 * @instance: adapter state to dump
289 * Print the SCSI bus signals for debugging purposes
292 static void NCR5380_print(struct Scsi_Host
*instance
)
294 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
295 unsigned char status
, basr
, mr
, icr
, i
;
297 status
= NCR5380_read(STATUS_REG
);
298 mr
= NCR5380_read(MODE_REG
);
299 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
300 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
302 printk(KERN_DEBUG
"SR = 0x%02x : ", status
);
303 for (i
= 0; signals
[i
].mask
; ++i
)
304 if (status
& signals
[i
].mask
)
305 printk(KERN_CONT
"%s, ", signals
[i
].name
);
306 printk(KERN_CONT
"\nBASR = 0x%02x : ", basr
);
307 for (i
= 0; basrs
[i
].mask
; ++i
)
308 if (basr
& basrs
[i
].mask
)
309 printk(KERN_CONT
"%s, ", basrs
[i
].name
);
310 printk(KERN_CONT
"\nICR = 0x%02x : ", icr
);
311 for (i
= 0; icrs
[i
].mask
; ++i
)
312 if (icr
& icrs
[i
].mask
)
313 printk(KERN_CONT
"%s, ", icrs
[i
].name
);
314 printk(KERN_CONT
"\nMR = 0x%02x : ", mr
);
315 for (i
= 0; mrs
[i
].mask
; ++i
)
316 if (mr
& mrs
[i
].mask
)
317 printk(KERN_CONT
"%s, ", mrs
[i
].name
);
318 printk(KERN_CONT
"\n");
325 {PHASE_DATAOUT
, "DATAOUT"},
326 {PHASE_DATAIN
, "DATAIN"},
327 {PHASE_CMDOUT
, "CMDOUT"},
328 {PHASE_STATIN
, "STATIN"},
329 {PHASE_MSGOUT
, "MSGOUT"},
330 {PHASE_MSGIN
, "MSGIN"},
331 {PHASE_UNKNOWN
, "UNKNOWN"}
335 * NCR5380_print_phase - show SCSI phase
336 * @instance: adapter to dump
338 * Print the current SCSI phase for debugging purposes
341 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
343 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
344 unsigned char status
;
347 status
= NCR5380_read(STATUS_REG
);
348 if (!(status
& SR_REQ
))
349 shost_printk(KERN_DEBUG
, instance
, "REQ not asserted, phase unknown.\n");
351 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
352 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
354 shost_printk(KERN_DEBUG
, instance
, "phase %s\n", phases
[i
].name
);
360 * NCR5380_info - report driver and host information
361 * @instance: relevant scsi host instance
363 * For use as the host template info() handler.
366 static const char *NCR5380_info(struct Scsi_Host
*instance
)
368 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
370 return hostdata
->info
;
374 * NCR5380_init - initialise an NCR5380
375 * @instance: adapter to configure
376 * @flags: control flags
378 * Initializes *instance and corresponding 5380 chip,
379 * with flags OR'd into the initial flags value.
381 * Notes : I assume that the host, hostno, and id bits have been
382 * set correctly. I don't care about the irq and other fields.
384 * Returns 0 for success
387 static int NCR5380_init(struct Scsi_Host
*instance
, int flags
)
389 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
391 unsigned long deadline
;
392 unsigned long accesses_per_ms
;
394 instance
->max_lun
= 7;
396 hostdata
->host
= instance
;
397 hostdata
->id_mask
= 1 << instance
->this_id
;
398 hostdata
->id_higher_mask
= 0;
399 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
400 if (i
> hostdata
->id_mask
)
401 hostdata
->id_higher_mask
|= i
;
402 for (i
= 0; i
< 8; ++i
)
403 hostdata
->busy
[i
] = 0;
404 hostdata
->dma_len
= 0;
406 spin_lock_init(&hostdata
->lock
);
407 hostdata
->connected
= NULL
;
408 hostdata
->sensing
= NULL
;
409 INIT_LIST_HEAD(&hostdata
->autosense
);
410 INIT_LIST_HEAD(&hostdata
->unissued
);
411 INIT_LIST_HEAD(&hostdata
->disconnected
);
413 hostdata
->flags
= flags
;
415 INIT_WORK(&hostdata
->main_task
, NCR5380_main
);
416 hostdata
->work_q
= alloc_workqueue("ncr5380_%d",
417 WQ_UNBOUND
| WQ_MEM_RECLAIM
,
418 0, instance
->host_no
);
419 if (!hostdata
->work_q
)
422 snprintf(hostdata
->info
, sizeof(hostdata
->info
),
423 "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}",
424 instance
->hostt
->name
, instance
->irq
, hostdata
->io_port
,
425 hostdata
->base
, instance
->can_queue
, instance
->cmd_per_lun
,
426 instance
->sg_tablesize
, instance
->this_id
,
427 hostdata
->flags
& FLAG_DMA_FIXUP
? "DMA_FIXUP " : "",
428 hostdata
->flags
& FLAG_NO_PSEUDO_DMA
? "NO_PSEUDO_DMA " : "",
429 hostdata
->flags
& FLAG_TOSHIBA_DELAY
? "TOSHIBA_DELAY " : "");
431 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
432 NCR5380_write(MODE_REG
, MR_BASE
);
433 NCR5380_write(TARGET_COMMAND_REG
, 0);
434 NCR5380_write(SELECT_ENABLE_REG
, 0);
436 /* Calibrate register polling loop */
438 deadline
= jiffies
+ 1;
441 } while (time_is_after_jiffies(deadline
));
442 deadline
+= msecs_to_jiffies(256);
444 NCR5380_read(STATUS_REG
);
447 } while (time_is_after_jiffies(deadline
));
448 accesses_per_ms
= i
/ 256;
449 hostdata
->poll_loops
= NCR5380_REG_POLL_TIME
* accesses_per_ms
/ 2;
455 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
456 * @instance: adapter to check
458 * If the system crashed, it may have crashed with a connected target and
459 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
460 * currently established nexus, which we know nothing about. Failing that
463 * Note that a bus reset will cause the chip to assert IRQ.
465 * Returns 0 if successful, otherwise -ENXIO.
468 static int NCR5380_maybe_reset_bus(struct Scsi_Host
*instance
)
470 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
473 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) && pass
<= 6; ++pass
) {
478 shost_printk(KERN_ERR
, instance
, "SCSI bus busy, waiting up to five seconds\n");
479 NCR5380_poll_politely(hostdata
,
480 STATUS_REG
, SR_BSY
, 0, 5 * HZ
);
483 shost_printk(KERN_ERR
, instance
, "bus busy, attempting abort\n");
484 do_abort(instance
, 1);
487 shost_printk(KERN_ERR
, instance
, "bus busy, attempting reset\n");
489 /* Wait after a reset; the SCSI standard calls for
490 * 250ms, we wait 500ms to be on the safe side.
491 * But some Toshiba CD-ROMs need ten times that.
493 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
499 shost_printk(KERN_ERR
, instance
, "bus locked solid\n");
507 * NCR5380_exit - remove an NCR5380
508 * @instance: adapter to remove
510 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
513 static void NCR5380_exit(struct Scsi_Host
*instance
)
515 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
517 cancel_work_sync(&hostdata
->main_task
);
518 destroy_workqueue(hostdata
->work_q
);
522 * complete_cmd - finish processing a command and return it to the SCSI ML
523 * @instance: the host instance
524 * @cmd: command to complete
527 static void complete_cmd(struct Scsi_Host
*instance
,
528 struct scsi_cmnd
*cmd
)
530 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
532 dsprintk(NDEBUG_QUEUES
, instance
, "complete_cmd: cmd %p\n", cmd
);
534 if (hostdata
->sensing
== cmd
) {
535 /* Autosense processing ends here */
536 if (get_status_byte(cmd
) != SAM_STAT_GOOD
) {
537 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
539 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
540 set_status_byte(cmd
, SAM_STAT_CHECK_CONDITION
);
542 hostdata
->sensing
= NULL
;
549 * NCR5380_queue_command - queue a command
550 * @instance: the relevant SCSI adapter
553 * cmd is added to the per-instance issue queue, with minor
554 * twiddling done to the host specific fields of cmd. If the
555 * main coroutine is not running, it is restarted.
558 static int NCR5380_queue_command(struct Scsi_Host
*instance
,
559 struct scsi_cmnd
*cmd
)
561 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
562 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(cmd
);
565 #if (NDEBUG & NDEBUG_NO_WRITE)
566 switch (cmd
->cmnd
[0]) {
569 shost_printk(KERN_DEBUG
, instance
, "WRITE attempted with NDEBUG_NO_WRITE set\n");
570 cmd
->result
= (DID_ERROR
<< 16);
574 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
578 spin_lock_irqsave(&hostdata
->lock
, flags
);
580 if (!NCR5380_acquire_dma_irq(instance
)) {
581 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
583 return SCSI_MLQUEUE_HOST_BUSY
;
587 * Insert the cmd into the issue queue. Note that REQUEST SENSE
588 * commands are added to the head of the queue since any command will
589 * clear the contingent allegiance condition that exists and the
590 * sense data is only guaranteed to be valid while the condition exists.
593 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
594 list_add(&ncmd
->list
, &hostdata
->unissued
);
596 list_add_tail(&ncmd
->list
, &hostdata
->unissued
);
598 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
600 dsprintk(NDEBUG_QUEUES
, instance
, "command %p added to %s of queue\n",
601 cmd
, (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
603 /* Kick off command processing */
604 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
608 static inline void maybe_release_dma_irq(struct Scsi_Host
*instance
)
610 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
612 /* Caller does the locking needed to set & test these data atomically */
613 if (list_empty(&hostdata
->disconnected
) &&
614 list_empty(&hostdata
->unissued
) &&
615 list_empty(&hostdata
->autosense
) &&
616 !hostdata
->connected
&&
617 !hostdata
->selecting
) {
618 NCR5380_release_dma_irq(instance
);
623 * dequeue_next_cmd - dequeue a command for processing
624 * @instance: the scsi host instance
626 * Priority is given to commands on the autosense queue. These commands
627 * need autosense because of a CHECK CONDITION result.
629 * Returns a command pointer if a command is found for a target that is
630 * not already busy. Otherwise returns NULL.
633 static struct scsi_cmnd
*dequeue_next_cmd(struct Scsi_Host
*instance
)
635 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
636 struct NCR5380_cmd
*ncmd
;
637 struct scsi_cmnd
*cmd
;
639 if (hostdata
->sensing
|| list_empty(&hostdata
->autosense
)) {
640 list_for_each_entry(ncmd
, &hostdata
->unissued
, list
) {
641 cmd
= NCR5380_to_scmd(ncmd
);
642 dsprintk(NDEBUG_QUEUES
, instance
, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
643 cmd
, scmd_id(cmd
), hostdata
->busy
[scmd_id(cmd
)], cmd
->device
->lun
);
645 if (!(hostdata
->busy
[scmd_id(cmd
)] & (1 << cmd
->device
->lun
))) {
646 list_del(&ncmd
->list
);
647 dsprintk(NDEBUG_QUEUES
, instance
,
648 "dequeue: removed %p from issue queue\n", cmd
);
653 /* Autosense processing begins here */
654 ncmd
= list_first_entry(&hostdata
->autosense
,
655 struct NCR5380_cmd
, list
);
656 list_del(&ncmd
->list
);
657 cmd
= NCR5380_to_scmd(ncmd
);
658 dsprintk(NDEBUG_QUEUES
, instance
,
659 "dequeue: removed %p from autosense queue\n", cmd
);
660 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
661 hostdata
->sensing
= cmd
;
667 static void requeue_cmd(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
669 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
670 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(cmd
);
672 if (hostdata
->sensing
== cmd
) {
673 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
674 list_add(&ncmd
->list
, &hostdata
->autosense
);
675 hostdata
->sensing
= NULL
;
677 list_add(&ncmd
->list
, &hostdata
->unissued
);
681 * NCR5380_main - NCR state machines
683 * NCR5380_main is a coroutine that runs as long as more work can
684 * be done on the NCR5380 host adapters in a system. Both
685 * NCR5380_queue_command() and NCR5380_intr() will try to start it
686 * in case it is not running.
689 static void NCR5380_main(struct work_struct
*work
)
691 struct NCR5380_hostdata
*hostdata
=
692 container_of(work
, struct NCR5380_hostdata
, main_task
);
693 struct Scsi_Host
*instance
= hostdata
->host
;
699 spin_lock_irq(&hostdata
->lock
);
700 while (!hostdata
->connected
&& !hostdata
->selecting
) {
701 struct scsi_cmnd
*cmd
= dequeue_next_cmd(instance
);
706 dsprintk(NDEBUG_MAIN
, instance
, "main: dequeued %p\n", cmd
);
709 * Attempt to establish an I_T_L nexus here.
710 * On success, instance->hostdata->connected is set.
711 * On failure, we must add the command back to the
712 * issue queue so we can keep trying.
715 * REQUEST SENSE commands are issued without tagged
716 * queueing, even on SCSI-II devices because the
717 * contingent allegiance condition exists for the
721 if (!NCR5380_select(instance
, cmd
)) {
722 dsprintk(NDEBUG_MAIN
, instance
, "main: select complete\n");
724 dsprintk(NDEBUG_MAIN
| NDEBUG_QUEUES
, instance
,
725 "main: select failed, returning %p to queue\n", cmd
);
726 requeue_cmd(instance
, cmd
);
729 if (hostdata
->connected
&& !hostdata
->dma_len
) {
730 dsprintk(NDEBUG_MAIN
, instance
, "main: performing information transfer\n");
731 NCR5380_information_transfer(instance
);
734 if (!hostdata
->connected
) {
735 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
736 maybe_release_dma_irq(instance
);
738 spin_unlock_irq(&hostdata
->lock
);
745 * NCR5380_dma_complete - finish DMA transfer
746 * @instance: the scsi host instance
748 * Called by the interrupt handler when DMA finishes or a phase
749 * mismatch occurs (which would end the DMA transfer).
752 static void NCR5380_dma_complete(struct Scsi_Host
*instance
)
754 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
755 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(hostdata
->connected
);
757 unsigned char **data
;
759 int saved_data
= 0, overrun
= 0;
762 if (hostdata
->read_overruns
) {
766 if ((NCR5380_read(BUS_AND_STATUS_REG
) &
767 (BASR_PHASE_MATCH
| BASR_ACK
)) ==
768 (BASR_PHASE_MATCH
| BASR_ACK
)) {
769 saved_data
= NCR5380_read(INPUT_DATA_REG
);
771 dsprintk(NDEBUG_DMA
, instance
, "read overrun handled\n");
777 if (sun3scsi_dma_finish(hostdata
->connected
->sc_data_direction
)) {
778 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
783 if ((NCR5380_read(BUS_AND_STATUS_REG
) & (BASR_PHASE_MATCH
| BASR_ACK
)) ==
784 (BASR_PHASE_MATCH
| BASR_ACK
)) {
785 pr_err("scsi%d: BASR %02x\n", instance
->host_no
,
786 NCR5380_read(BUS_AND_STATUS_REG
));
787 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
793 NCR5380_write(MODE_REG
, MR_BASE
);
794 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
795 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
797 transferred
= hostdata
->dma_len
- NCR5380_dma_residual(hostdata
);
798 hostdata
->dma_len
= 0;
800 data
= (unsigned char **)&ncmd
->ptr
;
801 count
= &ncmd
->this_residual
;
802 *data
+= transferred
;
803 *count
-= transferred
;
805 if (hostdata
->read_overruns
) {
808 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) == p
&& (p
& SR_IO
)) {
809 cnt
= toPIO
= hostdata
->read_overruns
;
811 dsprintk(NDEBUG_DMA
, instance
,
812 "Got an input overrun, using saved byte\n");
813 *(*data
)++ = saved_data
;
819 dsprintk(NDEBUG_DMA
, instance
,
820 "Doing %d byte PIO to 0x%p\n", cnt
, *data
);
821 NCR5380_transfer_pio(instance
, &p
, &cnt
, data
, 0);
822 *count
-= toPIO
- cnt
;
829 * NCR5380_intr - generic NCR5380 irq handler
830 * @irq: interrupt number
831 * @dev_id: device info
833 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
834 * from the disconnected queue, and restarting NCR5380_main()
837 * The chip can assert IRQ in any of six different conditions. The IRQ flag
838 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
839 * Three of these six conditions are latched in the Bus and Status Register:
840 * - End of DMA (cleared by ending DMA Mode)
841 * - Parity error (cleared by reading RPIR)
842 * - Loss of BSY (cleared by reading RPIR)
843 * Two conditions have flag bits that are not latched:
844 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
845 * - Bus reset (non-maskable)
846 * The remaining condition has no flag bit at all:
847 * - Selection/reselection
849 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
850 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
851 * claimed that "the design of the [DP8490] interrupt logic ensures
852 * interrupts will not be lost (they can be on the DP5380)."
853 * The L5380/53C80 datasheet from LOGIC Devices has more details.
855 * Checking for bus reset by reading RST is futile because of interrupt
856 * latency, but a bus reset will reset chip logic. Checking for parity error
857 * is unnecessary because that interrupt is never enabled. A Loss of BSY
858 * condition will clear DMA Mode. We can tell when this occurs because the
859 * Busy Monitor interrupt is enabled together with DMA Mode.
862 static irqreturn_t __maybe_unused
NCR5380_intr(int irq
, void *dev_id
)
864 struct Scsi_Host
*instance
= dev_id
;
865 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
870 spin_lock_irqsave(&hostdata
->lock
, flags
);
872 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
873 if (basr
& BASR_IRQ
) {
874 unsigned char mr
= NCR5380_read(MODE_REG
);
875 unsigned char sr
= NCR5380_read(STATUS_REG
);
877 dsprintk(NDEBUG_INTR
, instance
, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
880 if ((mr
& MR_DMA_MODE
) || (mr
& MR_MONITOR_BSY
)) {
881 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
882 * We ack IRQ after clearing Mode Register. Workarounds
883 * for End of DMA errata need to happen in DMA Mode.
886 dsprintk(NDEBUG_INTR
, instance
, "interrupt in DMA mode\n");
888 if (hostdata
->connected
) {
889 NCR5380_dma_complete(instance
);
890 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
892 NCR5380_write(MODE_REG
, MR_BASE
);
893 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
895 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_mask
) &&
896 (sr
& (SR_SEL
| SR_IO
| SR_BSY
| SR_RST
)) == (SR_SEL
| SR_IO
)) {
897 /* Probably reselected */
898 NCR5380_write(SELECT_ENABLE_REG
, 0);
899 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
901 dsprintk(NDEBUG_INTR
, instance
, "interrupt with SEL and IO\n");
903 if (!hostdata
->connected
) {
904 NCR5380_reselect(instance
);
905 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
907 if (!hostdata
->connected
)
908 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
910 /* Probably Bus Reset */
911 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
914 /* Certainly Bus Reset */
915 shost_printk(KERN_WARNING
, instance
,
916 "bus reset interrupt\n");
917 bus_reset_cleanup(instance
);
919 dsprintk(NDEBUG_INTR
, instance
, "unknown interrupt\n");
922 dregs
->csr
|= CSR_DMA_ENABLE
;
927 dsprintk(NDEBUG_INTR
, instance
, "interrupt without IRQ bit\n");
929 dregs
->csr
|= CSR_DMA_ENABLE
;
933 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
935 return IRQ_RETVAL(handled
);
939 * NCR5380_select - attempt arbitration and selection for a given command
940 * @instance: the Scsi_Host instance
941 * @cmd: the scsi_cmnd to execute
943 * This routine establishes an I_T_L nexus for a SCSI command. This involves
944 * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
946 * Returns true if the operation should be retried.
947 * Returns false if it should not be retried.
950 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
951 * with registers as they should have been on entry - ie
952 * SELECT_ENABLE will be set appropriately, the NCR5380
953 * will cease to drive any SCSI bus signals.
955 * If successful : the I_T_L nexus will be established, and
956 * hostdata->connected will be set to cmd.
957 * SELECT interrupt will be disabled.
959 * If failed (no target) : scsi_done() will be called, and the
960 * cmd->result host byte set to DID_BAD_TARGET.
963 static bool NCR5380_select(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
964 __releases(&hostdata
->lock
) __acquires(&hostdata
->lock
)
966 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
967 unsigned char tmp
[3], phase
;
972 bool can_disconnect
= instance
->irq
!= NO_IRQ
&&
973 cmd
->cmnd
[0] != REQUEST_SENSE
&&
974 (disconnect_mask
& BIT(scmd_id(cmd
)));
976 NCR5380_dprint(NDEBUG_ARBITRATION
, instance
);
977 dsprintk(NDEBUG_ARBITRATION
, instance
, "starting arbitration, id = %d\n",
981 * Arbitration and selection phases are slow and involve dropping the
982 * lock, so we have to watch out for EH. An exception handler may
983 * change 'selecting' to NULL. This function will then return false
984 * so that the caller will forget about 'cmd'. (During information
985 * transfer phases, EH may change 'connected' to NULL.)
987 hostdata
->selecting
= cmd
;
990 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
991 * data bus during SELECTION.
994 NCR5380_write(TARGET_COMMAND_REG
, 0);
1000 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
1001 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
1003 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1004 * Bus Free Delay, arbitration will begin.
1007 spin_unlock_irq(&hostdata
->lock
);
1008 err
= NCR5380_poll_politely2(hostdata
, MODE_REG
, MR_ARBITRATE
, 0,
1009 INITIATOR_COMMAND_REG
, ICR_ARBITRATION_PROGRESS
,
1010 ICR_ARBITRATION_PROGRESS
, HZ
);
1011 spin_lock_irq(&hostdata
->lock
);
1012 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
)) {
1013 /* Reselection interrupt */
1016 if (!hostdata
->selecting
) {
1017 /* Command was aborted */
1018 NCR5380_write(MODE_REG
, MR_BASE
);
1022 NCR5380_write(MODE_REG
, MR_BASE
);
1023 shost_printk(KERN_ERR
, instance
,
1024 "select: arbitration timeout\n");
1027 spin_unlock_irq(&hostdata
->lock
);
1029 /* The SCSI-2 arbitration delay is 2.4 us */
1032 /* Check for lost arbitration */
1033 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1034 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1035 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
)) {
1036 NCR5380_write(MODE_REG
, MR_BASE
);
1037 dsprintk(NDEBUG_ARBITRATION
, instance
, "lost arbitration, deasserting MR_ARBITRATE\n");
1038 spin_lock_irq(&hostdata
->lock
);
1042 /* After/during arbitration, BSY should be asserted.
1043 * IBM DPES-31080 Version S31Q works now
1044 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1046 NCR5380_write(INITIATOR_COMMAND_REG
,
1047 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1050 * Again, bus clear + bus settle time is 1.2us, however, this is
1051 * a minimum so we'll udelay ceil(1.2)
1054 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
1059 spin_lock_irq(&hostdata
->lock
);
1061 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1062 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
))
1065 if (!hostdata
->selecting
) {
1066 NCR5380_write(MODE_REG
, MR_BASE
);
1067 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1071 dsprintk(NDEBUG_ARBITRATION
, instance
, "won arbitration\n");
1074 * Now that we have won arbitration, start Selection process, asserting
1075 * the host and target ID's on the SCSI bus.
1078 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
| (1 << scmd_id(cmd
)));
1081 * Raise ATN while SEL is true before BSY goes false from arbitration,
1082 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1083 * phase immediately after selection.
1086 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
|
1087 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1088 NCR5380_write(MODE_REG
, MR_BASE
);
1091 * Reselect interrupts must be turned off prior to the dropping of BSY,
1092 * otherwise we will trigger an interrupt.
1094 NCR5380_write(SELECT_ENABLE_REG
, 0);
1096 spin_unlock_irq(&hostdata
->lock
);
1099 * The initiator shall then wait at least two deskew delays and release
1102 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1105 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
|
1106 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1109 * Something weird happens when we cease to drive BSY - looks
1110 * like the board/chip is letting us do another read before the
1111 * appropriate propagation delay has expired, and we're confusing
1112 * a BSY signal from ourselves as the target's response to SELECTION.
1114 * A small delay (the 'C++' frontend breaks the pipeline with an
1115 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1116 * tighter 'C' code breaks and requires this) solves the problem -
1117 * the 1 us delay is arbitrary, and only used because this delay will
1118 * be the same on other platforms and since it works here, it should
1121 * wingel suggests that this could be due to failing to wait
1127 dsprintk(NDEBUG_SELECTION
, instance
, "selecting target %d\n", scmd_id(cmd
));
1130 * The SCSI specification calls for a 250 ms timeout for the actual
1134 err
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_BSY
, SR_BSY
,
1135 msecs_to_jiffies(250));
1137 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) == (SR_SEL
| SR_IO
)) {
1138 spin_lock_irq(&hostdata
->lock
);
1139 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1140 NCR5380_reselect(instance
);
1141 shost_printk(KERN_ERR
, instance
, "reselection after won arbitration?\n");
1146 spin_lock_irq(&hostdata
->lock
);
1147 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1149 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1150 if (!hostdata
->selecting
)
1153 cmd
->result
= DID_BAD_TARGET
<< 16;
1154 complete_cmd(instance
, cmd
);
1155 dsprintk(NDEBUG_SELECTION
, instance
,
1156 "target did not respond within 250ms\n");
1162 * No less than two deskew delays after the initiator detects the
1163 * BSY signal is true, it shall release the SEL signal and may
1164 * change the DATA BUS. -wingel
1169 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1172 * Since we followed the SCSI spec, and raised ATN while SEL
1173 * was true but before BSY was false during selection, the information
1174 * transfer phase should be a MESSAGE OUT phase so that we can send the
1178 /* Wait for start of REQ/ACK handshake */
1180 err
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1181 spin_lock_irq(&hostdata
->lock
);
1183 shost_printk(KERN_ERR
, instance
, "select: REQ timeout\n");
1184 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1187 if (!hostdata
->selecting
) {
1188 do_abort(instance
, 0);
1192 dsprintk(NDEBUG_SELECTION
, instance
, "target %d selected, going into MESSAGE OUT phase.\n",
1194 tmp
[0] = IDENTIFY(can_disconnect
, cmd
->device
->lun
);
1198 phase
= PHASE_MSGOUT
;
1199 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 0);
1201 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1202 cmd
->result
= DID_ERROR
<< 16;
1203 complete_cmd(instance
, cmd
);
1204 dsprintk(NDEBUG_SELECTION
, instance
, "IDENTIFY message transfer failed\n");
1209 dsprintk(NDEBUG_SELECTION
, instance
, "nexus established.\n");
1211 hostdata
->connected
= cmd
;
1212 hostdata
->busy
[cmd
->device
->id
] |= 1 << cmd
->device
->lun
;
1214 #ifdef SUN3_SCSI_VME
1215 dregs
->csr
|= CSR_INTR
;
1218 initialize_SCp(cmd
);
1223 if (!hostdata
->selecting
)
1225 hostdata
->selecting
= NULL
;
1230 * NCR5380_transfer_pio() - transfers data in given phase using polled I/O
1231 * @instance: instance of driver
1232 * @phase: pointer to what phase is expected
1233 * @count: pointer to number of bytes to transfer
1234 * @data: pointer to data pointer
1235 * @can_sleep: 1 or 0 when sleeping is permitted or not, respectively
1237 * Returns: void. *phase, *count, *data are modified in place.
1241 * Note : this code is not as quick as it could be, however it
1242 * IS 100% reliable, and for the actual data transfer where speed
1243 * counts, we will always do a pseudo DMA or DMA transfer.
1246 static void NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1247 unsigned char *phase
, int *count
,
1248 unsigned char **data
, unsigned int can_sleep
)
1250 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1251 unsigned char p
= *phase
, tmp
;
1253 unsigned char *d
= *data
;
1256 * The NCR5380 chip will only drive the SCSI bus when the
1257 * phase specified in the appropriate bits of the TARGET COMMAND
1258 * REGISTER match the STATUS REGISTER
1261 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1265 * Wait for assertion of REQ, after which the phase bits will be
1269 if (NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
| SR_BSY
,
1270 SR_REQ
| SR_BSY
, HZ
* can_sleep
) < 0)
1273 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ asserted\n");
1275 /* Check for phase mismatch */
1276 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) != p
) {
1277 dsprintk(NDEBUG_PIO
, instance
, "phase mismatch\n");
1278 NCR5380_dprint_phase(NDEBUG_PIO
, instance
);
1282 /* Do actual transfer from SCSI bus to / from memory */
1284 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1286 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1291 * The SCSI standard suggests that in MSGOUT phase, the initiator
1292 * should drop ATN on the last byte of the message phase
1293 * after REQ has been asserted for the handshake but before
1294 * the initiator raises ACK.
1298 if (!((p
& SR_MSG
) && c
> 1)) {
1299 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1300 NCR5380_dprint(NDEBUG_PIO
, instance
);
1301 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1302 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1304 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1305 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1306 NCR5380_dprint(NDEBUG_PIO
, instance
);
1307 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1308 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1311 NCR5380_dprint(NDEBUG_PIO
, instance
);
1312 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1315 if (NCR5380_poll_politely(hostdata
,
1316 STATUS_REG
, SR_REQ
, 0, 5 * HZ
* can_sleep
) < 0)
1319 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ negated, handshake complete\n");
1322 * We have several special cases to consider during REQ/ACK
1325 * 1. We were in MSGOUT phase, and we are on the last byte of
1326 * the message. ATN must be dropped as ACK is dropped.
1328 * 2. We are in MSGIN phase, and we are on the last byte of the
1329 * message. We must exit with ACK asserted, so that the calling
1330 * code may raise ATN before dropping ACK to reject the message.
1332 * 3. ACK and ATN are clear & the target may proceed as normal.
1334 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1335 if (p
== PHASE_MSGOUT
&& c
> 1)
1336 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1338 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1342 dsprintk(NDEBUG_PIO
, instance
, "residual %d\n", c
);
1346 tmp
= NCR5380_read(STATUS_REG
);
1347 /* The phase read from the bus is valid if either REQ is (already)
1348 * asserted or if ACK hasn't been released yet. The latter applies if
1349 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1351 if ((tmp
& SR_REQ
) || ((tmp
& SR_IO
) && c
== 0))
1352 *phase
= tmp
& PHASE_MASK
;
1354 *phase
= PHASE_UNKNOWN
;
1358 * do_reset - issue a reset command
1359 * @instance: adapter to reset
1361 * Issue a reset sequence to the NCR5380 and try and get the bus
1362 * back into sane shape.
1364 * This clears the reset interrupt flag because there may be no handler for
1365 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1366 * been installed. And when in EH we may have released the ST DMA interrupt.
1369 static void do_reset(struct Scsi_Host
*instance
)
1371 struct NCR5380_hostdata __maybe_unused
*hostdata
= shost_priv(instance
);
1372 unsigned long flags
;
1374 local_irq_save(flags
);
1375 NCR5380_write(TARGET_COMMAND_REG
,
1376 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1377 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1379 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1380 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1381 local_irq_restore(flags
);
1385 * do_abort - abort the currently established nexus by going to
1386 * MESSAGE OUT phase and sending an ABORT message.
1387 * @instance: relevant scsi host instance
1388 * @can_sleep: 1 or 0 when sleeping is permitted or not, respectively
1390 * Returns 0 on success, negative error code on failure.
1393 static int do_abort(struct Scsi_Host
*instance
, unsigned int can_sleep
)
1395 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1396 unsigned char *msgptr
, phase
, tmp
;
1400 /* Request message out phase */
1401 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1404 * Wait for the target to indicate a valid phase by asserting
1405 * REQ. Once this happens, we'll have either a MSGOUT phase
1406 * and can immediately send the ABORT message, or we'll have some
1407 * other phase and will have to source/sink data.
1409 * We really don't care what value was on the bus or what value
1410 * the target sees, so we just handshake.
1413 rc
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
,
1414 10 * HZ
* can_sleep
);
1418 tmp
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1420 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1422 if (tmp
!= PHASE_MSGOUT
) {
1423 NCR5380_write(INITIATOR_COMMAND_REG
,
1424 ICR_BASE
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1425 rc
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, 0,
1426 3 * HZ
* can_sleep
);
1429 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1435 phase
= PHASE_MSGOUT
;
1436 NCR5380_transfer_pio(instance
, &phase
, &len
, &msgptr
, can_sleep
);
1441 * If we got here, and the command completed successfully,
1442 * we're about to go into bus free state.
1446 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1451 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1452 * unsigned char *phase, int *count, unsigned char **data)
1454 * Purpose : transfers data in given phase using either real
1457 * Inputs : instance - instance of driver, *phase - pointer to
1458 * what phase is expected, *count - pointer to number of
1459 * bytes to transfer, **data - pointer to data pointer.
1461 * Returns : -1 when different phase is entered without transferring
1462 * maximum number of bytes, 0 if all bytes or transferred or exit
1465 * Also, *phase, *count, *data are modified in place.
1469 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1470 unsigned char *phase
, int *count
,
1471 unsigned char **data
)
1473 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1474 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(hostdata
->connected
);
1476 unsigned char p
= *phase
;
1477 unsigned char *d
= *data
;
1481 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1489 if (hostdata
->read_overruns
)
1490 c
-= hostdata
->read_overruns
;
1491 else if (hostdata
->flags
& FLAG_DMA_FIXUP
)
1495 dsprintk(NDEBUG_DMA
, instance
, "initializing DMA %s: length %d, address %p\n",
1496 (p
& SR_IO
) ? "receive" : "send", c
, d
);
1499 /* send start chain */
1500 sun3scsi_dma_start(c
, *data
);
1503 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1504 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_MONITOR_BSY
|
1505 MR_ENABLE_EOP_INTR
);
1507 if (!(hostdata
->flags
& FLAG_LATE_DMA_SETUP
)) {
1508 /* On the Medusa, it is a must to initialize the DMA before
1509 * starting the NCR. This is also the cleaner way for the TT.
1512 result
= NCR5380_dma_recv_setup(hostdata
, d
, c
);
1514 result
= NCR5380_dma_send_setup(hostdata
, d
, c
);
1518 * On the PAS16 at least I/O recovery delays are not needed here.
1519 * Everyone else seems to want them.
1523 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1524 NCR5380_io_delay(1);
1525 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1527 NCR5380_io_delay(1);
1528 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1529 NCR5380_io_delay(1);
1530 NCR5380_write(START_DMA_SEND_REG
, 0);
1531 NCR5380_io_delay(1);
1535 #ifdef SUN3_SCSI_VME
1536 dregs
->csr
|= CSR_DMA_ENABLE
;
1538 sun3_dma_active
= 1;
1541 if (hostdata
->flags
& FLAG_LATE_DMA_SETUP
) {
1542 /* On the Falcon, the DMA setup must be done after the last
1543 * NCR access, else the DMA setup gets trashed!
1546 result
= NCR5380_dma_recv_setup(hostdata
, d
, c
);
1548 result
= NCR5380_dma_send_setup(hostdata
, d
, c
);
1551 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1555 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1557 hostdata
->dma_len
= result
;
1561 /* The result is zero iff pseudo DMA send/receive was completed. */
1562 hostdata
->dma_len
= c
;
1565 * A note regarding the DMA errata workarounds for early NMOS silicon.
1567 * For DMA sends, we want to wait until the last byte has been
1568 * transferred out over the bus before we turn off DMA mode. Alas, there
1569 * seems to be no terribly good way of doing this on a 5380 under all
1570 * conditions. For non-scatter-gather operations, we can wait until REQ
1571 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1572 * are nastier, since the device will be expecting more data than we
1573 * are prepared to send it, and REQ will remain asserted. On a 53C8[01]
1574 * we could test Last Byte Sent to assure transfer (I imagine this is
1575 * precisely why this signal was added to the newer chips) but on the
1576 * older 538[01] this signal does not exist. The workaround for this
1577 * lack is a watchdog; we bail out of the wait-loop after a modest
1578 * amount of wait-time if the usual exit conditions are not met.
1579 * Not a terribly clean or correct solution :-%
1581 * DMA receive is equally tricky due to a nasty characteristic of the
1582 * NCR5380. If the chip is in DMA receive mode, it will respond to a
1583 * target's REQ by latching the SCSI data into the INPUT DATA register
1584 * and asserting ACK, even if it has _already_ been notified by the
1585 * DMA controller that the current DMA transfer has completed! If the
1586 * NCR5380 is then taken out of DMA mode, this already-acknowledged
1589 * This is not a problem for "one DMA transfer per READ
1590 * command", because the situation will never arise... either all of
1591 * the data is DMA'ed properly, or the target switches to MESSAGE IN
1592 * phase to signal a disconnection (either operation bringing the DMA
1593 * to a clean halt). However, in order to handle scatter-receive, we
1594 * must work around the problem. The chosen fix is to DMA fewer bytes,
1595 * then check for the condition before taking the NCR5380 out of DMA
1596 * mode. One or two extra bytes are transferred via PIO as necessary
1597 * to fill out the original request.
1600 if ((hostdata
->flags
& FLAG_DMA_FIXUP
) &&
1601 (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
)) {
1603 * The workaround was to transfer fewer bytes than we
1604 * intended to with the pseudo-DMA receive function, wait for
1605 * the chip to latch the last byte, read it, and then disable
1608 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1609 * REQ is deasserted when ACK is asserted, and not reasserted
1610 * until ACK goes false. Since the NCR5380 won't lower ACK
1611 * until DACK is asserted, which won't happen unless we twiddle
1612 * the DMA port or we take the NCR5380 out of DMA mode, we
1613 * can guarantee that we won't handshake another extra
1616 * If sending, wait for the last byte to be sent. If REQ is
1617 * being asserted for the byte we're interested, we'll ACK it
1618 * and it will go false.
1620 if (!NCR5380_poll_politely(hostdata
, BUS_AND_STATUS_REG
,
1621 BASR_DRQ
, BASR_DRQ
, 0)) {
1623 (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
)) {
1624 if (!NCR5380_poll_politely(hostdata
, STATUS_REG
,
1626 d
[c
] = NCR5380_read(INPUT_DATA_REG
);
1627 --ncmd
->this_residual
;
1630 scmd_printk(KERN_ERR
, hostdata
->connected
,
1631 "PDMA fixup: !REQ timeout\n");
1634 } else if (NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
) {
1636 scmd_printk(KERN_ERR
, hostdata
->connected
,
1637 "PDMA fixup: DRQ timeout\n");
1641 NCR5380_dma_complete(instance
);
1646 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1648 * Purpose : run through the various SCSI phases and do as the target
1649 * directs us to. Operates on the currently connected command,
1650 * instance->connected.
1652 * Inputs : instance, instance for which we are doing commands
1654 * Side effects : SCSI things happen, the disconnected queue will be
1655 * modified if a command disconnects, *instance->connected will
1659 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
1660 __releases(&hostdata
->lock
) __acquires(&hostdata
->lock
)
1662 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1663 unsigned char msgout
= NOP
;
1667 unsigned char *data
;
1668 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
1669 struct scsi_cmnd
*cmd
;
1671 #ifdef SUN3_SCSI_VME
1672 dregs
->csr
|= CSR_INTR
;
1675 while ((cmd
= hostdata
->connected
)) {
1676 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(cmd
);
1678 tmp
= NCR5380_read(STATUS_REG
);
1679 /* We only have a valid SCSI phase when REQ is asserted */
1681 phase
= (tmp
& PHASE_MASK
);
1682 if (phase
!= old_phase
) {
1684 NCR5380_dprint_phase(NDEBUG_INFORMATION
, instance
);
1687 if (phase
== PHASE_CMDOUT
&&
1688 sun3_dma_setup_done
!= cmd
) {
1691 advance_sg_buffer(ncmd
);
1693 count
= sun3scsi_dma_xfer_len(hostdata
, cmd
);
1696 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
1697 sun3scsi_dma_send_setup(hostdata
,
1700 sun3scsi_dma_recv_setup(hostdata
,
1702 sun3_dma_setup_done
= cmd
;
1704 #ifdef SUN3_SCSI_VME
1705 dregs
->csr
|= CSR_INTR
;
1708 #endif /* CONFIG_SUN3 */
1710 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
1711 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1713 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1715 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1717 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1725 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1726 shost_printk(KERN_DEBUG
, instance
, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1728 do_abort(instance
, 0);
1729 cmd
->result
= DID_ERROR
<< 16;
1730 complete_cmd(instance
, cmd
);
1731 hostdata
->connected
= NULL
;
1732 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
1737 * If there is no room left in the current buffer in the
1738 * scatter-gather list, move onto the next one.
1741 advance_sg_buffer(ncmd
);
1742 dsprintk(NDEBUG_INFORMATION
, instance
,
1743 "this residual %d, sg ents %d\n",
1744 ncmd
->this_residual
,
1745 sg_nents(ncmd
->buffer
));
1748 * The preferred transfer method is going to be
1749 * PSEUDO-DMA for systems that are strictly PIO,
1750 * since we can let the hardware do the handshaking.
1752 * For this to work, we need to know the transfersize
1753 * ahead of time, since the pseudo-DMA code will sit
1754 * in an unconditional loop.
1758 if (!cmd
->device
->borken
)
1759 transfersize
= NCR5380_dma_xfer_len(hostdata
, cmd
);
1761 if (transfersize
> 0) {
1763 if (NCR5380_transfer_dma(instance
, &phase
,
1764 &len
, (unsigned char **)&ncmd
->ptr
)) {
1766 * If the watchdog timer fires, all future
1767 * accesses to this device will use the
1770 scmd_printk(KERN_INFO
, cmd
,
1771 "switching to slow handshake\n");
1772 cmd
->device
->borken
= 1;
1774 bus_reset_cleanup(instance
);
1777 /* Transfer a small chunk so that the
1778 * irq mode lock is not held too long.
1780 transfersize
= min(ncmd
->this_residual
,
1781 NCR5380_PIO_CHUNK_SIZE
);
1783 NCR5380_transfer_pio(instance
, &phase
, &len
,
1784 (unsigned char **)&ncmd
->ptr
,
1786 ncmd
->this_residual
-= transfersize
- len
;
1789 if (sun3_dma_setup_done
== cmd
)
1790 sun3_dma_setup_done
= NULL
;
1797 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 0);
1803 set_host_byte(cmd
, DID_ABORT
);
1805 case COMMAND_COMPLETE
:
1806 /* Accept message by clearing ACK */
1808 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1809 dsprintk(NDEBUG_QUEUES
, instance
,
1810 "COMMAND COMPLETE %p target %d lun %llu\n",
1811 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1813 hostdata
->connected
= NULL
;
1814 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
1816 set_status_byte(cmd
, ncmd
->status
);
1818 set_resid_from_SCp(cmd
);
1820 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
1821 complete_cmd(instance
, cmd
);
1823 if (ncmd
->status
== SAM_STAT_CHECK_CONDITION
||
1824 ncmd
->status
== SAM_STAT_COMMAND_TERMINATED
) {
1825 dsprintk(NDEBUG_QUEUES
, instance
, "autosense: adding cmd %p to tail of autosense queue\n",
1827 list_add_tail(&ncmd
->list
,
1828 &hostdata
->autosense
);
1830 complete_cmd(instance
, cmd
);
1834 * Restore phase bits to 0 so an interrupted selection,
1835 * arbitration can resume.
1837 NCR5380_write(TARGET_COMMAND_REG
, 0);
1840 case MESSAGE_REJECT
:
1841 /* Accept message by clearing ACK */
1842 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1843 switch (hostdata
->last_message
) {
1844 case HEAD_OF_QUEUE_TAG
:
1845 case ORDERED_QUEUE_TAG
:
1846 case SIMPLE_QUEUE_TAG
:
1847 cmd
->device
->simple_tags
= 0;
1848 hostdata
->busy
[cmd
->device
->id
] |= (1 << (cmd
->device
->lun
& 0xFF));
1855 /* Accept message by clearing ACK */
1856 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1857 hostdata
->connected
= NULL
;
1858 list_add(&ncmd
->list
, &hostdata
->disconnected
);
1859 dsprintk(NDEBUG_INFORMATION
| NDEBUG_QUEUES
,
1860 instance
, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1861 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1864 * Restore phase bits to 0 so an interrupted selection,
1865 * arbitration can resume.
1867 NCR5380_write(TARGET_COMMAND_REG
, 0);
1869 #ifdef SUN3_SCSI_VME
1870 dregs
->csr
|= CSR_DMA_ENABLE
;
1874 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1875 * operation, in violation of the SCSI spec so we can safely
1876 * ignore SAVE/RESTORE pointers calls.
1878 * Unfortunately, some disks violate the SCSI spec and
1879 * don't issue the required SAVE_POINTERS message before
1880 * disconnecting, and we have to break spec to remain
1884 case RESTORE_POINTERS
:
1885 /* Accept message by clearing ACK */
1886 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1888 case EXTENDED_MESSAGE
:
1890 * Start the message buffer with the EXTENDED_MESSAGE
1891 * byte, since spi_print_msg() wants the whole thing.
1893 extended_msg
[0] = EXTENDED_MESSAGE
;
1894 /* Accept first byte by clearing ACK */
1895 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1897 spin_unlock_irq(&hostdata
->lock
);
1899 dsprintk(NDEBUG_EXTENDED
, instance
, "receiving extended message\n");
1902 data
= extended_msg
+ 1;
1903 phase
= PHASE_MSGIN
;
1904 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 1);
1905 dsprintk(NDEBUG_EXTENDED
, instance
, "length %d, code 0x%02x\n",
1906 (int)extended_msg
[1],
1907 (int)extended_msg
[2]);
1909 if (!len
&& extended_msg
[1] > 0 &&
1910 extended_msg
[1] <= sizeof(extended_msg
) - 2) {
1911 /* Accept third byte by clearing ACK */
1912 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1913 len
= extended_msg
[1] - 1;
1914 data
= extended_msg
+ 3;
1915 phase
= PHASE_MSGIN
;
1917 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 1);
1918 dsprintk(NDEBUG_EXTENDED
, instance
, "message received, residual %d\n",
1921 switch (extended_msg
[2]) {
1927 shost_printk(KERN_ERR
, instance
, "error receiving extended message\n");
1930 shost_printk(KERN_NOTICE
, instance
, "extended message code %02x length %d is too long\n",
1931 extended_msg
[2], extended_msg
[1]);
1935 spin_lock_irq(&hostdata
->lock
);
1936 if (!hostdata
->connected
)
1939 /* Reject message */
1943 * If we get something weird that we aren't expecting,
1946 if (tmp
== EXTENDED_MESSAGE
)
1947 scmd_printk(KERN_INFO
, cmd
,
1948 "rejecting unknown extended message code %02x, length %d\n",
1949 extended_msg
[2], extended_msg
[1]);
1951 scmd_printk(KERN_INFO
, cmd
,
1952 "rejecting unknown message code %02x\n",
1955 msgout
= MESSAGE_REJECT
;
1956 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1958 } /* switch (tmp) */
1963 hostdata
->last_message
= msgout
;
1964 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 0);
1965 if (msgout
== ABORT
) {
1966 hostdata
->connected
= NULL
;
1967 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
1968 cmd
->result
= DID_ERROR
<< 16;
1969 complete_cmd(instance
, cmd
);
1978 * XXX for performance reasons, on machines with a
1979 * PSEUDO-DMA architecture we should probably
1980 * use the dma transfer function.
1982 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 0);
1988 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 0);
1992 shost_printk(KERN_ERR
, instance
, "unknown phase\n");
1993 NCR5380_dprint(NDEBUG_ANY
, instance
);
1994 } /* switch(phase) */
1998 spin_unlock_irq(&hostdata
->lock
);
1999 err
= NCR5380_poll_politely(hostdata
, STATUS_REG
,
2000 SR_REQ
, SR_REQ
, HZ
);
2001 spin_lock_irq(&hostdata
->lock
);
2003 if (err
< 0 && hostdata
->connected
&&
2004 !(NCR5380_read(STATUS_REG
) & SR_BSY
)) {
2005 scmd_printk(KERN_ERR
, hostdata
->connected
,
2006 "BSY signal lost\n");
2008 bus_reset_cleanup(instance
);
2015 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2017 * Purpose : does reselection, initializing the instance->connected
2018 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2019 * nexus has been reestablished,
2021 * Inputs : instance - this instance of the NCR5380.
2024 static void NCR5380_reselect(struct Scsi_Host
*instance
)
2026 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2027 unsigned char target_mask
;
2029 unsigned char msg
[3];
2030 struct NCR5380_cmd
*ncmd
;
2031 struct scsi_cmnd
*tmp
;
2034 * Disable arbitration, etc. since the host adapter obviously
2035 * lost, and tell an interrupted NCR5380_select() to restart.
2038 NCR5380_write(MODE_REG
, MR_BASE
);
2040 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2041 if (!target_mask
|| target_mask
& (target_mask
- 1)) {
2042 shost_printk(KERN_WARNING
, instance
,
2043 "reselect: bad target_mask 0x%02x\n", target_mask
);
2048 * At this point, we have detected that our SCSI ID is on the bus,
2049 * SEL is true and BSY was false for at least one bus settle delay
2052 * We must assert BSY ourselves, until the target drops the SEL
2056 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2057 if (NCR5380_poll_politely(hostdata
,
2058 STATUS_REG
, SR_SEL
, 0, 0) < 0) {
2059 shost_printk(KERN_ERR
, instance
, "reselect: !SEL timeout\n");
2060 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2063 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2066 * Wait for target to go into MSGIN.
2069 if (NCR5380_poll_politely(hostdata
,
2070 STATUS_REG
, SR_REQ
, SR_REQ
, 0) < 0) {
2071 if ((NCR5380_read(STATUS_REG
) & (SR_BSY
| SR_SEL
)) == 0)
2072 /* BUS FREE phase */
2074 shost_printk(KERN_ERR
, instance
, "reselect: REQ timeout\n");
2075 do_abort(instance
, 0);
2080 /* acknowledge toggle to MSGIN */
2081 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(PHASE_MSGIN
));
2083 /* peek at the byte without really hitting the bus */
2084 msg
[0] = NCR5380_read(CURRENT_SCSI_DATA_REG
);
2088 unsigned char *data
= msg
;
2089 unsigned char phase
= PHASE_MSGIN
;
2091 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
, 0);
2094 do_abort(instance
, 0);
2098 #endif /* CONFIG_SUN3 */
2100 if (!(msg
[0] & 0x80)) {
2101 shost_printk(KERN_ERR
, instance
, "expecting IDENTIFY message, got ");
2104 do_abort(instance
, 0);
2107 lun
= msg
[0] & 0x07;
2110 * We need to add code for SCSI-II to track which devices have
2111 * I_T_L_Q nexuses established, and which have simple I_T_L
2112 * nexuses so we can chose to do additional data transfer.
2116 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2117 * just reestablished, and remove it from the disconnected queue.
2121 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
2122 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2124 if (target_mask
== (1 << scmd_id(cmd
)) &&
2125 lun
== (u8
)cmd
->device
->lun
) {
2126 list_del(&ncmd
->list
);
2133 dsprintk(NDEBUG_RESELECTION
| NDEBUG_QUEUES
, instance
,
2134 "reselect: removed %p from disconnected queue\n", tmp
);
2136 int target
= ffs(target_mask
) - 1;
2138 shost_printk(KERN_ERR
, instance
, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2141 * Since we have an established nexus that we can't do anything
2142 * with, we must abort it.
2144 if (do_abort(instance
, 0) == 0)
2145 hostdata
->busy
[target
] &= ~(1 << lun
);
2150 if (sun3_dma_setup_done
!= tmp
) {
2153 advance_sg_buffer(ncmd
);
2155 count
= sun3scsi_dma_xfer_len(hostdata
, tmp
);
2158 if (tmp
->sc_data_direction
== DMA_TO_DEVICE
)
2159 sun3scsi_dma_send_setup(hostdata
,
2162 sun3scsi_dma_recv_setup(hostdata
,
2164 sun3_dma_setup_done
= tmp
;
2168 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
2169 #endif /* CONFIG_SUN3 */
2171 /* Accept message by clearing ACK */
2172 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2174 hostdata
->connected
= tmp
;
2175 dsprintk(NDEBUG_RESELECTION
, instance
, "nexus established, target %d, lun %llu\n",
2176 scmd_id(tmp
), tmp
->device
->lun
);
2180 * list_find_cmd - test for presence of a command in a linked list
2181 * @haystack: list of commands
2182 * @needle: command to search for
2185 static bool list_find_cmd(struct list_head
*haystack
,
2186 struct scsi_cmnd
*needle
)
2188 struct NCR5380_cmd
*ncmd
;
2190 list_for_each_entry(ncmd
, haystack
, list
)
2191 if (NCR5380_to_scmd(ncmd
) == needle
)
2197 * list_remove_cmd - remove a command from linked list
2198 * @haystack: list of commands
2199 * @needle: command to remove
2202 static bool list_del_cmd(struct list_head
*haystack
,
2203 struct scsi_cmnd
*needle
)
2205 if (list_find_cmd(haystack
, needle
)) {
2206 struct NCR5380_cmd
*ncmd
= NCR5380_to_ncmd(needle
);
2208 list_del(&ncmd
->list
);
2215 * NCR5380_abort - scsi host eh_abort_handler() method
2216 * @cmd: the command to be aborted
2218 * Try to abort a given command by removing it from queues and/or sending
2219 * the target an abort message. This may not succeed in causing a target
2220 * to abort the command. Nonetheless, the low-level driver must forget about
2221 * the command because the mid-layer reclaims it and it may be re-issued.
2223 * The normal path taken by a command is as follows. For EH we trace this
2224 * same path to locate and abort the command.
2226 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2227 * [disconnected -> connected ->]...
2228 * [autosense -> connected ->] done
2230 * If cmd was not found at all then presumably it has already been completed,
2231 * in which case return SUCCESS to try to avoid further EH measures.
2233 * If the command has not completed yet, we must not fail to find it.
2234 * We have no option but to forget the aborted command (even if it still
2235 * lacks sense data). The mid-layer may re-issue a command that is in error
2236 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2237 * this driver are such that a command can appear on one queue only.
2239 * The lock protects driver data structures, but EH handlers also use it
2240 * to serialize their own execution and prevent their own re-entry.
2243 static int NCR5380_abort(struct scsi_cmnd
*cmd
)
2245 struct Scsi_Host
*instance
= cmd
->device
->host
;
2246 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2247 unsigned long flags
;
2248 int result
= SUCCESS
;
2250 spin_lock_irqsave(&hostdata
->lock
, flags
);
2252 #if (NDEBUG & NDEBUG_ANY)
2253 scmd_printk(KERN_INFO
, cmd
, __func__
);
2255 NCR5380_dprint(NDEBUG_ANY
, instance
);
2256 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2258 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2259 dsprintk(NDEBUG_ABORT
, instance
,
2260 "abort: removed %p from issue queue\n", cmd
);
2261 cmd
->result
= DID_ABORT
<< 16;
2262 scsi_done(cmd
); /* No tag or busy flag to worry about */
2266 if (hostdata
->selecting
== cmd
) {
2267 dsprintk(NDEBUG_ABORT
, instance
,
2268 "abort: cmd %p == selecting\n", cmd
);
2269 hostdata
->selecting
= NULL
;
2270 cmd
->result
= DID_ABORT
<< 16;
2271 complete_cmd(instance
, cmd
);
2275 if (list_del_cmd(&hostdata
->disconnected
, cmd
)) {
2276 dsprintk(NDEBUG_ABORT
, instance
,
2277 "abort: removed %p from disconnected list\n", cmd
);
2278 /* Can't call NCR5380_select() and send ABORT because that
2279 * means releasing the lock. Need a bus reset.
2281 set_host_byte(cmd
, DID_ERROR
);
2282 complete_cmd(instance
, cmd
);
2287 if (hostdata
->connected
== cmd
) {
2288 dsprintk(NDEBUG_ABORT
, instance
, "abort: cmd %p is connected\n", cmd
);
2289 hostdata
->connected
= NULL
;
2290 hostdata
->dma_len
= 0;
2291 if (do_abort(instance
, 0) < 0) {
2292 set_host_byte(cmd
, DID_ERROR
);
2293 complete_cmd(instance
, cmd
);
2297 set_host_byte(cmd
, DID_ABORT
);
2298 complete_cmd(instance
, cmd
);
2302 if (list_del_cmd(&hostdata
->autosense
, cmd
)) {
2303 dsprintk(NDEBUG_ABORT
, instance
,
2304 "abort: removed %p from sense queue\n", cmd
);
2305 complete_cmd(instance
, cmd
);
2309 if (result
== FAILED
)
2310 dsprintk(NDEBUG_ABORT
, instance
, "abort: failed to abort %p\n", cmd
);
2312 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
2313 dsprintk(NDEBUG_ABORT
, instance
, "abort: successfully aborted %p\n", cmd
);
2316 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2317 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
2323 static void bus_reset_cleanup(struct Scsi_Host
*instance
)
2325 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2327 struct NCR5380_cmd
*ncmd
;
2329 /* reset NCR registers */
2330 NCR5380_write(MODE_REG
, MR_BASE
);
2331 NCR5380_write(TARGET_COMMAND_REG
, 0);
2332 NCR5380_write(SELECT_ENABLE_REG
, 0);
2334 /* After the reset, there are no more connected or disconnected commands
2335 * and no busy units; so clear the low-level status here to avoid
2336 * conflicts when the mid-level code tries to wake up the affected
2340 if (hostdata
->selecting
) {
2341 hostdata
->selecting
->result
= DID_RESET
<< 16;
2342 complete_cmd(instance
, hostdata
->selecting
);
2343 hostdata
->selecting
= NULL
;
2346 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
2347 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2349 set_host_byte(cmd
, DID_RESET
);
2350 complete_cmd(instance
, cmd
);
2352 INIT_LIST_HEAD(&hostdata
->disconnected
);
2354 list_for_each_entry(ncmd
, &hostdata
->autosense
, list
) {
2355 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2359 INIT_LIST_HEAD(&hostdata
->autosense
);
2361 if (hostdata
->connected
) {
2362 set_host_byte(hostdata
->connected
, DID_RESET
);
2363 complete_cmd(instance
, hostdata
->connected
);
2364 hostdata
->connected
= NULL
;
2367 for (i
= 0; i
< 8; ++i
)
2368 hostdata
->busy
[i
] = 0;
2369 hostdata
->dma_len
= 0;
2371 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2375 * NCR5380_host_reset - reset the SCSI host
2376 * @cmd: SCSI command undergoing EH
2381 static int NCR5380_host_reset(struct scsi_cmnd
*cmd
)
2383 struct Scsi_Host
*instance
= cmd
->device
->host
;
2384 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2385 unsigned long flags
;
2386 struct NCR5380_cmd
*ncmd
;
2388 spin_lock_irqsave(&hostdata
->lock
, flags
);
2390 #if (NDEBUG & NDEBUG_ANY)
2391 shost_printk(KERN_INFO
, instance
, __func__
);
2393 NCR5380_dprint(NDEBUG_ANY
, instance
);
2394 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2396 list_for_each_entry(ncmd
, &hostdata
->unissued
, list
) {
2397 struct scsi_cmnd
*scmd
= NCR5380_to_scmd(ncmd
);
2399 scmd
->result
= DID_RESET
<< 16;
2402 INIT_LIST_HEAD(&hostdata
->unissued
);
2405 bus_reset_cleanup(instance
);
2407 spin_unlock_irqrestore(&hostdata
->lock
, flags
);