2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
6 * Note that these routines also work with NR53c400 family chips.
8 * Copyright 1993, Drew Eckhardt
10 * (Unix and Linux consulting and custom programming)
14 * DISTRIBUTION RELEASE 6.
16 * For more information, please consult
19 * SCSI Protocol Controller
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
30 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
33 * - Some of the debug statements were incorrect (undefined variables and the
34 * like). I fixed that.
36 * - In information_transfer(), I think a #ifdef was wrong. Looking at the
37 * possible DMA transfer size should also happen for REAL_DMA. I added this
38 * in the #if statement.
40 * - When using real DMA, information_transfer() should return in a DATAOUT
41 * phase after starting the DMA. It has nothing more to do.
43 * - The interrupt service routine should run main after end of DMA, too (not
44 * only after RESELECTION interrupts). Additionally, it should _not_ test
45 * for more interrupts after running main, since a DMA process may have
46 * been started and interrupts are turned on now. The new int could happen
47 * inside the execution of NCR5380_intr(), leading to recursive
50 * - I've added a function merge_contiguous_buffers() that tries to
51 * merge scatter-gather buffers that are located at contiguous
52 * physical addresses and can be processed with the same DMA setup.
53 * Since most scatter-gather operations work on a page (4K) of
54 * 4 buffers (1K), in more than 90% of all cases three interrupts and
55 * DMA setup actions are saved.
57 * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
58 * and USLEEP, because these were messing up readability and will never be
59 * needed for Atari SCSI.
61 * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
62 * stuff), and 'main' is executed in a bottom half if awoken by an
65 * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
66 * constructs. In my eyes, this made the source rather unreadable, so I
67 * finally replaced that by the *_PRINTK() macros.
72 * Further development / testing that should be done :
73 * 1. Test linked command handling code after Eric is ready with
74 * the high level code.
76 #include <scsi/scsi_dbg.h>
77 #include <scsi/scsi_transport_spi.h>
79 #if (NDEBUG & NDEBUG_LISTS)
82 printk("LINE:%d Adding %p to %p\n", \
83 __LINE__, (void*)(x), (void*)(y)); \
87 #define REMOVE(w, x, y, z) \
89 printk("LINE:%d Removing: %p->%p %p->%p \n", \
90 __LINE__, (void*)(w), (void*)(x), \
91 (void*)(y), (void*)(z)); \
97 #define REMOVE(w,x,y,z)
107 * This file a skeleton Linux SCSI driver for the NCR 5380 series
108 * of chips. To use it, you write an architecture specific functions
109 * and macros and include this file in your driver.
111 * These macros control options :
112 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
113 * for commands that return with a CHECK CONDITION status.
115 * LINKED - if defined, linked commands are supported.
117 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
119 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
121 * These macros MUST be defined :
123 * NCR5380_read(register) - read from the specified register
125 * NCR5380_write(register, value) - write to the specific register
127 * Either real DMA *or* pseudo DMA may be implemented
129 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
130 * Note that the DMA setup functions should return the number of bytes
131 * that they were able to program the controller for.
133 * Also note that generic i386/PC versions of these macros are
134 * available as NCR5380_i386_dma_write_setup,
135 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
137 * NCR5380_dma_write_setup(instance, src, count) - initialize
138 * NCR5380_dma_read_setup(instance, dst, count) - initialize
139 * NCR5380_dma_residual(instance); - residual count
142 * NCR5380_pwrite(instance, src, count)
143 * NCR5380_pread(instance, dst, count);
145 * If nothing specific to this implementation needs doing (ie, with external
146 * hardware), you must also define
148 * NCR5380_queue_command
153 * to be the global entry points into the specific driver, ie
154 * #define NCR5380_queue_command t128_queue_command.
156 * If this is not done, the routines will be defined as static functions
157 * with the NCR5380* names and the user must provide a globally
158 * accessible wrapper function.
160 * The generic driver is initialized by calling NCR5380_init(instance),
161 * after setting the appropriate host specific fields and ID. If the
162 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
163 * possible) function may be used. Before the specific driver initialization
164 * code finishes, NCR5380_print_options should be called.
167 static struct Scsi_Host
*first_instance
= NULL
;
168 static struct scsi_host_template
*the_template
= NULL
;
170 /* Macros ease life... :-) */
171 #define SETUP_HOSTDATA(in) \
172 struct NCR5380_hostdata *hostdata = \
173 (struct NCR5380_hostdata *)(in)->hostdata
174 #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
176 #define NEXT(cmd) ((Scsi_Cmnd *)(cmd)->host_scribble)
177 #define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next))
178 #define NEXTADDR(cmd) ((Scsi_Cmnd **)&(cmd)->host_scribble)
180 #define HOSTNO instance->host_no
181 #define H_NO(cmd) (cmd)->device->host->host_no
186 * Functions for handling tagged queuing
187 * =====================================
189 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
191 * Using consecutive numbers for the tags is no good idea in my eyes. There
192 * could be wrong re-usings if the counter (8 bit!) wraps and some early
193 * command has been preempted for a long time. My solution: a bitfield for
194 * remembering used tags.
196 * There's also the problem that each target has a certain queue size, but we
197 * cannot know it in advance :-( We just see a QUEUE_FULL status being
198 * returned. So, in this case, the driver internal queue size assumption is
199 * reduced to the number of active tags if QUEUE_FULL is returned by the
200 * target. The command is returned to the mid-level, but with status changed
201 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
204 * We're also not allowed running tagged commands as long as an untagged
205 * command is active. And REQUEST SENSE commands after a contingent allegiance
206 * condition _must_ be untagged. To keep track whether an untagged command has
207 * been issued, the host->busy array is still employed, as it is without
208 * support for tagged queuing.
210 * One could suspect that there are possible race conditions between
211 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
212 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
213 * which already guaranteed to be running at most once. It is also the only
214 * place where tags/LUNs are allocated. So no other allocation can slip
215 * between that pair, there could only happen a reselection, which can free a
216 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
217 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
220 /* -1 for TAG_NONE is not possible with unsigned char cmd->tag */
222 #define TAG_NONE 0xff
225 DECLARE_BITMAP(allocated
, MAX_TAGS
);
230 static TAG_ALLOC TagAlloc
[8][8]; /* 8 targets and 8 LUNs */
233 static void __init
init_tags(void)
238 if (!setup_use_tagged_queuing
)
241 for (target
= 0; target
< 8; ++target
) {
242 for (lun
= 0; lun
< 8; ++lun
) {
243 ta
= &TagAlloc
[target
][lun
];
244 bitmap_zero(ta
->allocated
, MAX_TAGS
);
245 ta
->nr_allocated
= 0;
246 /* At the beginning, assume the maximum queue size we could
247 * support (MAX_TAGS). This value will be decreased if the target
248 * returns QUEUE_FULL status.
250 ta
->queue_size
= MAX_TAGS
;
256 /* Check if we can issue a command to this LUN: First see if the LUN is marked
257 * busy by an untagged command. If the command should use tagged queuing, also
258 * check that there is a free tag and the target's queue won't overflow. This
259 * function should be called with interrupts disabled to avoid race
263 static int is_lun_busy(Scsi_Cmnd
*cmd
, int should_be_tagged
)
265 SETUP_HOSTDATA(cmd
->device
->host
);
267 if (hostdata
->busy
[cmd
->device
->id
] & (1 << cmd
->device
->lun
))
269 if (!should_be_tagged
||
270 !setup_use_tagged_queuing
|| !cmd
->device
->tagged_supported
)
272 if (TagAlloc
[cmd
->device
->id
][cmd
->device
->lun
].nr_allocated
>=
273 TagAlloc
[cmd
->device
->id
][cmd
->device
->lun
].queue_size
) {
274 TAG_PRINTK("scsi%d: target %d lun %d: no free tags\n",
275 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
282 /* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
283 * must be called before!), or reserve the LUN in 'busy' if the command is
287 static void cmd_get_tag(Scsi_Cmnd
*cmd
, int should_be_tagged
)
289 SETUP_HOSTDATA(cmd
->device
->host
);
291 /* If we or the target don't support tagged queuing, allocate the LUN for
292 * an untagged command.
294 if (!should_be_tagged
||
295 !setup_use_tagged_queuing
|| !cmd
->device
->tagged_supported
) {
297 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
298 TAG_PRINTK("scsi%d: target %d lun %d now allocated by untagged "
299 "command\n", H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
301 TAG_ALLOC
*ta
= &TagAlloc
[cmd
->device
->id
][cmd
->device
->lun
];
303 cmd
->tag
= find_first_zero_bit(ta
->allocated
, MAX_TAGS
);
304 set_bit(cmd
->tag
, ta
->allocated
);
306 TAG_PRINTK("scsi%d: using tag %d for target %d lun %d "
307 "(now %d tags in use)\n",
308 H_NO(cmd
), cmd
->tag
, cmd
->device
->id
,
309 cmd
->device
->lun
, ta
->nr_allocated
);
314 /* Mark the tag of command 'cmd' as free, or in case of an untagged command,
318 static void cmd_free_tag(Scsi_Cmnd
*cmd
)
320 SETUP_HOSTDATA(cmd
->device
->host
);
322 if (cmd
->tag
== TAG_NONE
) {
323 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
324 TAG_PRINTK("scsi%d: target %d lun %d untagged cmd finished\n",
325 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
326 } else if (cmd
->tag
>= MAX_TAGS
) {
327 printk(KERN_NOTICE
"scsi%d: trying to free bad tag %d!\n",
328 H_NO(cmd
), cmd
->tag
);
330 TAG_ALLOC
*ta
= &TagAlloc
[cmd
->device
->id
][cmd
->device
->lun
];
331 clear_bit(cmd
->tag
, ta
->allocated
);
333 TAG_PRINTK("scsi%d: freed tag %d for target %d lun %d\n",
334 H_NO(cmd
), cmd
->tag
, cmd
->device
->id
, cmd
->device
->lun
);
339 static void free_all_tags(void)
344 if (!setup_use_tagged_queuing
)
347 for (target
= 0; target
< 8; ++target
) {
348 for (lun
= 0; lun
< 8; ++lun
) {
349 ta
= &TagAlloc
[target
][lun
];
350 bitmap_zero(ta
->allocated
, MAX_TAGS
);
351 ta
->nr_allocated
= 0;
356 #endif /* SUPPORT_TAGS */
360 * Function: void merge_contiguous_buffers( Scsi_Cmnd *cmd )
362 * Purpose: Try to merge several scatter-gather requests into one DMA
363 * transfer. This is possible if the scatter buffers lie on
364 * physical contiguous addresses.
366 * Parameters: Scsi_Cmnd *cmd
367 * The command to work on. The first scatter buffer's data are
368 * assumed to be already transfered into ptr/this_residual.
371 static void merge_contiguous_buffers(Scsi_Cmnd
*cmd
)
373 unsigned long endaddr
;
374 #if (NDEBUG & NDEBUG_MERGING)
375 unsigned long oldlen
= cmd
->SCp
.this_residual
;
379 for (endaddr
= virt_to_phys(cmd
->SCp
.ptr
+ cmd
->SCp
.this_residual
- 1) + 1;
380 cmd
->SCp
.buffers_residual
&&
381 virt_to_phys(sg_virt(&cmd
->SCp
.buffer
[1])) == endaddr
;) {
382 MER_PRINTK("VTOP(%p) == %08lx -> merging\n",
383 page_address(sg_page(&cmd
->SCp
.buffer
[1])), endaddr
);
384 #if (NDEBUG & NDEBUG_MERGING)
388 --cmd
->SCp
.buffers_residual
;
389 cmd
->SCp
.this_residual
+= cmd
->SCp
.buffer
->length
;
390 endaddr
+= cmd
->SCp
.buffer
->length
;
392 #if (NDEBUG & NDEBUG_MERGING)
393 if (oldlen
!= cmd
->SCp
.this_residual
)
394 MER_PRINTK("merged %d buffers from %p, new length %08x\n",
395 cnt
, cmd
->SCp
.ptr
, cmd
->SCp
.this_residual
);
400 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
402 * Purpose : initialize the saved data pointers for cmd to point to the
403 * start of the buffer.
405 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
408 static inline void initialize_SCp(Scsi_Cmnd
*cmd
)
411 * Initialize the Scsi Pointer field so that all of the commands in the
412 * various queues are valid.
415 if (scsi_bufflen(cmd
)) {
416 cmd
->SCp
.buffer
= scsi_sglist(cmd
);
417 cmd
->SCp
.buffers_residual
= scsi_sg_count(cmd
) - 1;
418 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
419 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
420 /* ++roman: Try to merge some scatter-buffers if they are at
421 * contiguous physical addresses.
423 merge_contiguous_buffers(cmd
);
425 cmd
->SCp
.buffer
= NULL
;
426 cmd
->SCp
.buffers_residual
= 0;
428 cmd
->SCp
.this_residual
= 0;
432 #include <linux/delay.h>
439 { SR_DBP
, "PARITY"}, { SR_RST
, "RST" }, { SR_BSY
, "BSY" },
440 { SR_REQ
, "REQ" }, { SR_MSG
, "MSG" }, { SR_CD
, "CD" }, { SR_IO
, "IO" },
441 { SR_SEL
, "SEL" }, {0, NULL
}
443 {BASR_ATN
, "ATN"}, {BASR_ACK
, "ACK"}, {0, NULL
}
445 {ICR_ASSERT_RST
, "ASSERT RST"},{ICR_ASSERT_ACK
, "ASSERT ACK"},
446 {ICR_ASSERT_BSY
, "ASSERT BSY"}, {ICR_ASSERT_SEL
, "ASSERT SEL"},
447 {ICR_ASSERT_ATN
, "ASSERT ATN"}, {ICR_ASSERT_DATA
, "ASSERT DATA"},
450 {MR_BLOCK_DMA_MODE
, "MODE BLOCK DMA"}, {MR_TARGET
, "MODE TARGET"},
451 {MR_ENABLE_PAR_CHECK
, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR
,
452 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR
,"MODE EOP INTR"},
453 {MR_MONITOR_BSY
, "MODE MONITOR BSY"},
454 {MR_DMA_MODE
, "MODE DMA"}, {MR_ARBITRATE
, "MODE ARBITRATION"},
459 * Function : void NCR5380_print(struct Scsi_Host *instance)
461 * Purpose : print the SCSI bus signals for debugging purposes
463 * Input : instance - which NCR5380
466 static void NCR5380_print(struct Scsi_Host
*instance
)
468 unsigned char status
, data
, basr
, mr
, icr
, i
;
471 local_irq_save(flags
);
472 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
473 status
= NCR5380_read(STATUS_REG
);
474 mr
= NCR5380_read(MODE_REG
);
475 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
476 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
477 local_irq_restore(flags
);
478 printk("STATUS_REG: %02x ", status
);
479 for (i
= 0; signals
[i
].mask
; ++i
)
480 if (status
& signals
[i
].mask
)
481 printk(",%s", signals
[i
].name
);
482 printk("\nBASR: %02x ", basr
);
483 for (i
= 0; basrs
[i
].mask
; ++i
)
484 if (basr
& basrs
[i
].mask
)
485 printk(",%s", basrs
[i
].name
);
486 printk("\nICR: %02x ", icr
);
487 for (i
= 0; icrs
[i
].mask
; ++i
)
488 if (icr
& icrs
[i
].mask
)
489 printk(",%s", icrs
[i
].name
);
490 printk("\nMODE: %02x ", mr
);
491 for (i
= 0; mrs
[i
].mask
; ++i
)
492 if (mr
& mrs
[i
].mask
)
493 printk(",%s", mrs
[i
].name
);
501 {PHASE_DATAOUT
, "DATAOUT"}, {PHASE_DATAIN
, "DATAIN"}, {PHASE_CMDOUT
, "CMDOUT"},
502 {PHASE_STATIN
, "STATIN"}, {PHASE_MSGOUT
, "MSGOUT"}, {PHASE_MSGIN
, "MSGIN"},
503 {PHASE_UNKNOWN
, "UNKNOWN"}
507 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
509 * Purpose : print the current SCSI phase for debugging purposes
511 * Input : instance - which NCR5380
514 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
516 unsigned char status
;
519 status
= NCR5380_read(STATUS_REG
);
520 if (!(status
& SR_REQ
))
521 printk(KERN_DEBUG
"scsi%d: REQ not asserted, phase unknown.\n", HOSTNO
);
523 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
524 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
526 printk(KERN_DEBUG
"scsi%d: phase %s\n", HOSTNO
, phases
[i
].name
);
533 static inline void NCR5380_print(struct Scsi_Host
*instance
)
536 static inline void NCR5380_print_phase(struct Scsi_Host
*instance
)
543 * ++roman: New scheme of calling NCR5380_main()
545 * If we're not in an interrupt, we can call our main directly, it cannot be
546 * already running. Else, we queue it on a task queue, if not 'main_running'
547 * tells us that a lower level is already executing it. This way,
548 * 'main_running' needs not be protected in a special way.
550 * queue_main() is a utility function for putting our main onto the task
551 * queue, if main_running is false. It should be called only from a
552 * interrupt or bottom half.
555 #include <linux/gfp.h>
556 #include <linux/workqueue.h>
557 #include <linux/interrupt.h>
559 static volatile int main_running
;
560 static DECLARE_WORK(NCR5380_tqueue
, NCR5380_main
);
562 static inline void queue_main(void)
565 /* If in interrupt and NCR5380_main() not already running,
566 queue it on the 'immediate' task queue, to be processed
567 immediately after the current interrupt processing has
569 schedule_work(&NCR5380_tqueue
);
571 /* else: nothing to do: the running NCR5380_main() will pick up
572 any newly queued command. */
576 static inline void NCR5380_all_init(void)
580 INI_PRINTK("scsi : NCR5380_all_init()\n");
587 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
589 * Purpose : called by probe code indicating the NCR5380 driver
590 * options that were selected.
592 * Inputs : instance, pointer to this instance. Unused.
595 static void __init
NCR5380_print_options(struct Scsi_Host
*instance
)
597 printk(" generic options"
608 " SCSI-2 TAGGED QUEUING"
611 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE
);
615 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
617 * Purpose : print commands in the various queues, called from
618 * NCR5380_abort and NCR5380_debug to aid debugging.
620 * Inputs : instance, pointer to this instance.
623 static void NCR5380_print_status(struct Scsi_Host
*instance
)
629 NCR_PRINT(NDEBUG_ANY
);
630 NCR_PRINT_PHASE(NDEBUG_ANY
);
632 pr_bfr
= (char *)__get_free_page(GFP_ATOMIC
);
634 printk("NCR5380_print_status: no memory for print buffer\n");
637 len
= NCR5380_proc_info(instance
, pr_bfr
, &start
, 0, PAGE_SIZE
, 0);
639 printk("\n%s\n", pr_bfr
);
640 free_page((unsigned long)pr_bfr
);
644 /******************************************/
646 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
648 * *buffer: I/O buffer
649 * **start: if inout == FALSE pointer into buffer where user read should start
650 * offset: current offset
651 * length: length of buffer
652 * hostno: Scsi_Host host_no
653 * inout: TRUE - user is writing; FALSE - user is reading
655 * Return the number of bytes read from or written
659 #define SPRINTF(fmt,args...) \
661 if (pos + strlen(fmt) + 20 /* slop */ < buffer + length) \
662 pos += sprintf(pos, fmt , ## args); \
664 static char *lprint_Scsi_Cmnd(Scsi_Cmnd
*cmd
, char *pos
, char *buffer
, int length
);
666 static int NCR5380_proc_info(struct Scsi_Host
*instance
, char *buffer
,
667 char **start
, off_t offset
, int length
, int inout
)
670 struct NCR5380_hostdata
*hostdata
;
674 #define check_offset() \
676 if (pos - buffer < offset - begin) { \
677 begin += pos - buffer; \
682 hostdata
= (struct NCR5380_hostdata
*)instance
->hostdata
;
684 if (inout
) /* Has data been written to the file ? */
685 return -ENOSYS
; /* Currently this is a no-op */
686 SPRINTF("NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE
);
688 local_irq_save(flags
);
689 SPRINTF("NCR5380: coroutine is%s running.\n",
690 main_running
? "" : "n't");
692 if (!hostdata
->connected
)
693 SPRINTF("scsi%d: no currently connected command\n", HOSTNO
);
695 pos
= lprint_Scsi_Cmnd((Scsi_Cmnd
*) hostdata
->connected
,
696 pos
, buffer
, length
);
697 SPRINTF("scsi%d: issue_queue\n", HOSTNO
);
699 for (ptr
= (Scsi_Cmnd
*)hostdata
->issue_queue
; ptr
; ptr
= NEXT(ptr
)) {
700 pos
= lprint_Scsi_Cmnd(ptr
, pos
, buffer
, length
);
704 SPRINTF("scsi%d: disconnected_queue\n", HOSTNO
);
706 for (ptr
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
; ptr
;
708 pos
= lprint_Scsi_Cmnd(ptr
, pos
, buffer
, length
);
712 local_irq_restore(flags
);
713 *start
= buffer
+ (offset
- begin
);
714 if (pos
- buffer
< offset
- begin
)
716 else if (pos
- buffer
- (offset
- begin
) < length
)
717 return pos
- buffer
- (offset
- begin
);
721 static char *lprint_Scsi_Cmnd(Scsi_Cmnd
*cmd
, char *pos
, char *buffer
, int length
)
724 unsigned char *command
;
725 SPRINTF("scsi%d: destination target %d, lun %d\n",
726 H_NO(cmd
), cmd
->device
->id
, cmd
->device
->lun
);
727 SPRINTF(" command = ");
729 SPRINTF("%2d (0x%02x)", command
[0], command
[0]);
730 for (i
= 1, s
= COMMAND_SIZE(command
[0]); i
< s
; ++i
)
731 SPRINTF(" %02x", command
[i
]);
738 * Function : void NCR5380_init (struct Scsi_Host *instance)
740 * Purpose : initializes *instance and corresponding 5380 chip.
742 * Inputs : instance - instantiation of the 5380 driver.
744 * Notes : I assume that the host, hostno, and id bits have been
745 * set correctly. I don't care about the irq and other fields.
749 static int __init
NCR5380_init(struct Scsi_Host
*instance
, int flags
)
752 SETUP_HOSTDATA(instance
);
756 hostdata
->aborted
= 0;
757 hostdata
->id_mask
= 1 << instance
->this_id
;
758 hostdata
->id_higher_mask
= 0;
759 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
760 if (i
> hostdata
->id_mask
)
761 hostdata
->id_higher_mask
|= i
;
762 for (i
= 0; i
< 8; ++i
)
763 hostdata
->busy
[i
] = 0;
767 #if defined(REAL_DMA)
768 hostdata
->dma_len
= 0;
770 hostdata
->targets_present
= 0;
771 hostdata
->connected
= NULL
;
772 hostdata
->issue_queue
= NULL
;
773 hostdata
->disconnected_queue
= NULL
;
774 hostdata
->flags
= FLAG_CHECK_LAST_BYTE_SENT
;
777 the_template
= instance
->hostt
;
778 first_instance
= instance
;
782 if ((instance
->cmd_per_lun
> 1) || (instance
->can_queue
> 1))
783 printk("scsi%d: WARNING : support for multiple outstanding commands enabled\n"
784 " without AUTOSENSE option, contingent allegiance conditions may\n"
785 " be incorrectly cleared.\n", HOSTNO
);
786 #endif /* def AUTOSENSE */
788 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
789 NCR5380_write(MODE_REG
, MR_BASE
);
790 NCR5380_write(TARGET_COMMAND_REG
, 0);
791 NCR5380_write(SELECT_ENABLE_REG
, 0);
797 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
798 * void (*done)(Scsi_Cmnd *))
800 * Purpose : enqueues a SCSI command
802 * Inputs : cmd - SCSI command, done - function called on completion, with
803 * a pointer to the command descriptor.
808 * cmd is added to the per instance issue_queue, with minor
809 * twiddling done to the host specific fields of cmd. If the
810 * main coroutine is not running, it is restarted.
814 static int NCR5380_queue_command(Scsi_Cmnd
*cmd
, void (*done
)(Scsi_Cmnd
*))
816 SETUP_HOSTDATA(cmd
->device
->host
);
821 #if (NDEBUG & NDEBUG_NO_WRITE)
822 switch (cmd
->cmnd
[0]) {
825 printk(KERN_NOTICE
"scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
827 cmd
->result
= (DID_ERROR
<< 16);
831 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
834 # ifdef NCR5380_STAT_LIMIT
835 if (scsi_bufflen(cmd
) > NCR5380_STAT_LIMIT
)
837 switch (cmd
->cmnd
[0]) {
841 hostdata
->time_write
[cmd
->device
->id
] -= (jiffies
- hostdata
->timebase
);
842 hostdata
->bytes_write
[cmd
->device
->id
] += scsi_bufflen(cmd
);
843 hostdata
->pendingw
++;
848 hostdata
->time_read
[cmd
->device
->id
] -= (jiffies
- hostdata
->timebase
);
849 hostdata
->bytes_read
[cmd
->device
->id
] += scsi_bufflen(cmd
);
850 hostdata
->pendingr
++;
856 * We use the host_scribble field as a pointer to the next command
861 cmd
->scsi_done
= done
;
866 * Insert the cmd into the issue queue. Note that REQUEST SENSE
867 * commands are added to the head of the queue since any command will
868 * clear the contingent allegiance condition that exists and the
869 * sense data is only guaranteed to be valid while the condition exists.
872 local_irq_save(flags
);
873 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
874 * Otherwise a running NCR5380_main may steal the lock.
875 * Lock before actually inserting due to fairness reasons explained in
876 * atari_scsi.c. If we insert first, then it's impossible for this driver
877 * to release the lock.
878 * Stop timer for this command while waiting for the lock, or timeouts
879 * may happen (and they really do), and it's no good if the command doesn't
880 * appear in any of the queues.
881 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
882 * because also a timer int can trigger an abort or reset, which would
883 * alter queues and touch the lock.
886 /* perhaps stop command timer here */
888 /* perhaps restart command timer here */
890 if (!(hostdata
->issue_queue
) || (cmd
->cmnd
[0] == REQUEST_SENSE
)) {
891 LIST(cmd
, hostdata
->issue_queue
);
892 SET_NEXT(cmd
, hostdata
->issue_queue
);
893 hostdata
->issue_queue
= cmd
;
895 for (tmp
= (Scsi_Cmnd
*)hostdata
->issue_queue
;
896 NEXT(tmp
); tmp
= NEXT(tmp
))
901 local_irq_restore(flags
);
903 QU_PRINTK("scsi%d: command added to %s of queue\n", H_NO(cmd
),
904 (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
906 /* If queue_command() is called from an interrupt (real one or bottom
907 * half), we let queue_main() do the job of taking care about main. If it
908 * is already running, this is a no-op, else main will be queued.
910 * If we're not in an interrupt, we can call NCR5380_main()
911 * unconditionally, because it cannot be already running.
913 if (in_interrupt() || ((flags
>> 8) & 7) >= 6)
921 * Function : NCR5380_main (void)
923 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
924 * be done on the NCR5380 host adapters in a system. Both
925 * NCR5380_queue_command() and NCR5380_intr() will try to start it
926 * in case it is not running.
928 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
929 * reenable them. This prevents reentrancy and kernel stack overflow.
932 static void NCR5380_main(struct work_struct
*work
)
934 Scsi_Cmnd
*tmp
, *prev
;
935 struct Scsi_Host
*instance
= first_instance
;
936 struct NCR5380_hostdata
*hostdata
= HOSTDATA(instance
);
941 * We run (with interrupts disabled) until we're sure that none of
942 * the host adapters have anything that can be done, at which point
943 * we set main_running to 0 and exit.
945 * Interrupts are enabled before doing various other internal
946 * instructions, after we've decided that we need to run through
949 * this should prevent any race conditions.
951 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
952 * because also a timer int can trigger an abort or reset, which can
953 * alter queues and touch the Falcon lock.
956 /* Tell int handlers main() is now already executing. Note that
957 no races are possible here. If an int comes in before
958 'main_running' is set here, and queues/executes main via the
959 task queue, it doesn't do any harm, just this instance of main
960 won't find any work left to do. */
965 local_save_flags(flags
);
967 local_irq_disable(); /* Freeze request queues */
970 if (!hostdata
->connected
) {
971 MAIN_PRINTK("scsi%d: not connected\n", HOSTNO
);
973 * Search through the issue_queue for a command destined
974 * for a target that's not busy.
976 #if (NDEBUG & NDEBUG_LISTS)
977 for (tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
, prev
= NULL
;
978 tmp
&& (tmp
!= prev
); prev
= tmp
, tmp
= NEXT(tmp
))
980 /*printk("%p ", tmp);*/
981 if ((tmp
== prev
) && tmp
)
983 /* else printk("\n"); */
985 for (tmp
= (Scsi_Cmnd
*) hostdata
->issue_queue
,
986 prev
= NULL
; tmp
; prev
= tmp
, tmp
= NEXT(tmp
)) {
988 #if (NDEBUG & NDEBUG_LISTS)
990 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n",
991 tmp
, tmp
->device
->id
, hostdata
->busy
[tmp
->device
->id
],
994 /* When we find one, remove it from the issue queue. */
995 /* ++guenther: possible race with Falcon locking */
998 !is_lun_busy( tmp
, tmp
->cmnd
[0] != REQUEST_SENSE
)
1000 !(hostdata
->busy
[tmp
->device
->id
] & (1 << tmp
->device
->lun
))
1003 /* ++guenther: just to be sure, this must be atomic */
1004 local_irq_disable();
1006 REMOVE(prev
, NEXT(prev
), tmp
, NEXT(tmp
));
1007 SET_NEXT(prev
, NEXT(tmp
));
1009 REMOVE(-1, hostdata
->issue_queue
, tmp
, NEXT(tmp
));
1010 hostdata
->issue_queue
= NEXT(tmp
);
1012 SET_NEXT(tmp
, NULL
);
1013 falcon_dont_release
++;
1015 /* reenable interrupts after finding one */
1016 local_irq_restore(flags
);
1019 * Attempt to establish an I_T_L nexus here.
1020 * On success, instance->hostdata->connected is set.
1021 * On failure, we must add the command back to the
1022 * issue queue so we can keep trying.
1024 MAIN_PRINTK("scsi%d: main(): command for target %d "
1025 "lun %d removed from issue_queue\n",
1026 HOSTNO
, tmp
->device
->id
, tmp
->device
->lun
);
1028 * REQUEST SENSE commands are issued without tagged
1029 * queueing, even on SCSI-II devices because the
1030 * contingent allegiance condition exists for the
1033 /* ++roman: ...and the standard also requires that
1034 * REQUEST SENSE command are untagged.
1038 cmd_get_tag(tmp
, tmp
->cmnd
[0] != REQUEST_SENSE
);
1040 if (!NCR5380_select(instance
, tmp
,
1041 (tmp
->cmnd
[0] == REQUEST_SENSE
) ? TAG_NONE
:
1043 falcon_dont_release
--;
1044 /* release if target did not response! */
1045 falcon_release_lock_if_possible(hostdata
);
1048 local_irq_disable();
1049 LIST(tmp
, hostdata
->issue_queue
);
1050 SET_NEXT(tmp
, hostdata
->issue_queue
);
1051 hostdata
->issue_queue
= tmp
;
1055 falcon_dont_release
--;
1056 local_irq_restore(flags
);
1057 MAIN_PRINTK("scsi%d: main(): select() failed, "
1058 "returned to issue_queue\n", HOSTNO
);
1059 if (hostdata
->connected
)
1062 } /* if target/lun/target queue is not busy */
1063 } /* for issue_queue */
1064 } /* if (!hostdata->connected) */
1066 if (hostdata
->connected
1068 && !hostdata
->dma_len
1071 local_irq_restore(flags
);
1072 MAIN_PRINTK("scsi%d: main: performing information transfer\n",
1074 NCR5380_information_transfer(instance
);
1075 MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO
);
1080 /* Better allow ints _after_ 'main_running' has been cleared, else
1081 an interrupt could believe we'll pick up the work it left for
1082 us, but we won't see it anymore here... */
1084 local_irq_restore(flags
);
1090 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1092 * Purpose : Called by interrupt handler when DMA finishes or a phase
1093 * mismatch occurs (which would finish the DMA transfer).
1095 * Inputs : instance - this instance of the NCR5380.
1099 static void NCR5380_dma_complete(struct Scsi_Host
*instance
)
1101 SETUP_HOSTDATA(instance
);
1102 int transfered
, saved_data
= 0, overrun
= 0, cnt
, toPIO
;
1103 unsigned char **data
, p
;
1104 volatile int *count
;
1106 if (!hostdata
->connected
) {
1107 printk(KERN_WARNING
"scsi%d: received end of DMA interrupt with "
1108 "no connected cmd\n", HOSTNO
);
1112 if (atari_read_overruns
) {
1113 p
= hostdata
->connected
->SCp
.phase
;
1116 if ((NCR5380_read(BUS_AND_STATUS_REG
) &
1117 (BASR_PHASE_MATCH
|BASR_ACK
)) ==
1118 (BASR_PHASE_MATCH
|BASR_ACK
)) {
1119 saved_data
= NCR5380_read(INPUT_DATA_REG
);
1121 DMA_PRINTK("scsi%d: read overrun handled\n", HOSTNO
);
1126 DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1127 HOSTNO
, NCR5380_read(BUS_AND_STATUS_REG
),
1128 NCR5380_read(STATUS_REG
));
1130 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1131 NCR5380_write(MODE_REG
, MR_BASE
);
1132 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1134 transfered
= hostdata
->dma_len
- NCR5380_dma_residual(instance
);
1135 hostdata
->dma_len
= 0;
1137 data
= (unsigned char **)&hostdata
->connected
->SCp
.ptr
;
1138 count
= &hostdata
->connected
->SCp
.this_residual
;
1139 *data
+= transfered
;
1140 *count
-= transfered
;
1142 if (atari_read_overruns
) {
1143 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) == p
&& (p
& SR_IO
)) {
1144 cnt
= toPIO
= atari_read_overruns
;
1146 DMA_PRINTK("Got an input overrun, using saved byte\n");
1147 *(*data
)++ = saved_data
;
1152 DMA_PRINTK("Doing %d-byte PIO to 0x%08lx\n", cnt
, (long)*data
);
1153 NCR5380_transfer_pio(instance
, &p
, &cnt
, data
);
1154 *count
-= toPIO
- cnt
;
1158 #endif /* REAL_DMA */
1162 * Function : void NCR5380_intr (int irq)
1164 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1165 * from the disconnected queue, and restarting NCR5380_main()
1168 * Inputs : int irq, irq that caused this interrupt.
1172 static irqreturn_t
NCR5380_intr(int irq
, void *dev_id
)
1174 struct Scsi_Host
*instance
= first_instance
;
1175 int done
= 1, handled
= 0;
1178 INT_PRINTK("scsi%d: NCR5380 irq triggered\n", HOSTNO
);
1180 /* Look for pending interrupts */
1181 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
1182 INT_PRINTK("scsi%d: BASR=%02x\n", HOSTNO
, basr
);
1183 /* dispatch to appropriate routine if found and done=0 */
1184 if (basr
& BASR_IRQ
) {
1185 NCR_PRINT(NDEBUG_INTR
);
1186 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
|SR_IO
)) == (SR_SEL
|SR_IO
)) {
1189 INT_PRINTK("scsi%d: SEL interrupt\n", HOSTNO
);
1190 NCR5380_reselect(instance
);
1191 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1192 } else if (basr
& BASR_PARITY_ERROR
) {
1193 INT_PRINTK("scsi%d: PARITY interrupt\n", HOSTNO
);
1194 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1195 } else if ((NCR5380_read(STATUS_REG
) & SR_RST
) == SR_RST
) {
1196 INT_PRINTK("scsi%d: RESET interrupt\n", HOSTNO
);
1197 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1200 * The rest of the interrupt conditions can occur only during a
1204 #if defined(REAL_DMA)
1206 * We should only get PHASE MISMATCH and EOP interrupts if we have
1207 * DMA enabled, so do a sanity check based on the current setting
1208 * of the MODE register.
1211 if ((NCR5380_read(MODE_REG
) & MR_DMA_MODE
) &&
1212 ((basr
& BASR_END_DMA_TRANSFER
) ||
1213 !(basr
& BASR_PHASE_MATCH
))) {
1215 INT_PRINTK("scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO
);
1216 NCR5380_dma_complete( instance
);
1220 #endif /* REAL_DMA */
1222 /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1223 if (basr
& BASR_PHASE_MATCH
)
1224 printk(KERN_NOTICE
"scsi%d: unknown interrupt, "
1225 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1226 HOSTNO
, basr
, NCR5380_read(MODE_REG
),
1227 NCR5380_read(STATUS_REG
));
1228 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1230 } /* if !(SELECTION || PARITY) */
1232 } /* BASR & IRQ */ else {
1233 printk(KERN_NOTICE
"scsi%d: interrupt without IRQ bit set in BASR, "
1234 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO
, basr
,
1235 NCR5380_read(MODE_REG
), NCR5380_read(STATUS_REG
));
1236 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1240 INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO
);
1241 /* Put a call to NCR5380_main() on the queue... */
1244 return IRQ_RETVAL(handled
);
1247 #ifdef NCR5380_STATS
1248 static void collect_stats(struct NCR5380_hostdata
* hostdata
, Scsi_Cmnd
*cmd
)
1250 # ifdef NCR5380_STAT_LIMIT
1251 if (scsi_bufflen(cmd
) > NCR5380_STAT_LIMIT
)
1253 switch (cmd
->cmnd
[0]) {
1257 hostdata
->time_write
[cmd
->device
->id
] += (jiffies
- hostdata
->timebase
);
1258 /*hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);*/
1259 hostdata
->pendingw
--;
1264 hostdata
->time_read
[cmd
->device
->id
] += (jiffies
- hostdata
->timebase
);
1265 /*hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);*/
1266 hostdata
->pendingr
--;
1273 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1276 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1277 * including ARBITRATION, SELECTION, and initial message out for
1278 * IDENTIFY and queue messages.
1280 * Inputs : instance - instantiation of the 5380 driver on which this
1281 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1282 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1283 * the command that is presently connected.
1285 * Returns : -1 if selection could not execute for some reason,
1286 * 0 if selection succeeded or failed because the target
1290 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1291 * with registers as they should have been on entry - ie
1292 * SELECT_ENABLE will be set appropriately, the NCR5380
1293 * will cease to drive any SCSI bus signals.
1295 * If successful : I_T_L or I_T_L_Q nexus will be established,
1296 * instance->connected will be set to cmd.
1297 * SELECT interrupt will be disabled.
1299 * If failed (no target) : cmd->scsi_done() will be called, and the
1300 * cmd->result host byte set to DID_BAD_TARGET.
1303 static int NCR5380_select(struct Scsi_Host
*instance
, Scsi_Cmnd
*cmd
, int tag
)
1305 SETUP_HOSTDATA(instance
);
1306 unsigned char tmp
[3], phase
;
1307 unsigned char *data
;
1309 unsigned long timeout
;
1310 unsigned long flags
;
1312 hostdata
->restart_select
= 0;
1313 NCR_PRINT(NDEBUG_ARBITRATION
);
1314 ARB_PRINTK("scsi%d: starting arbitration, id = %d\n", HOSTNO
,
1318 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1319 * data bus during SELECTION.
1322 local_irq_save(flags
);
1323 if (hostdata
->connected
) {
1324 local_irq_restore(flags
);
1327 NCR5380_write(TARGET_COMMAND_REG
, 0);
1330 * Start arbitration.
1333 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
1334 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
1336 local_irq_restore(flags
);
1338 /* Wait for arbitration logic to complete */
1339 #if defined(NCR_TIMEOUT)
1341 unsigned long timeout
= jiffies
+ 2*NCR_TIMEOUT
;
1343 while (!(NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
) &&
1344 time_before(jiffies
, timeout
) && !hostdata
->connected
)
1346 if (time_after_eq(jiffies
, timeout
)) {
1347 printk("scsi : arbitration timeout at %d\n", __LINE__
);
1348 NCR5380_write(MODE_REG
, MR_BASE
);
1349 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1353 #else /* NCR_TIMEOUT */
1354 while (!(NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_PROGRESS
) &&
1355 !hostdata
->connected
)
1359 ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO
);
1361 if (hostdata
->connected
) {
1362 NCR5380_write(MODE_REG
, MR_BASE
);
1366 * The arbitration delay is 2.2us, but this is a minimum and there is
1367 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1368 * the integral nature of udelay().
1374 /* Check for lost arbitration */
1375 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1376 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1377 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1378 hostdata
->connected
) {
1379 NCR5380_write(MODE_REG
, MR_BASE
);
1380 ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1385 /* after/during arbitration, BSY should be asserted.
1386 IBM DPES-31080 Version S31Q works now */
1387 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1388 NCR5380_write(INITIATOR_COMMAND_REG
,
1389 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1391 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1392 hostdata
->connected
) {
1393 NCR5380_write(MODE_REG
, MR_BASE
);
1394 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1395 ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1401 * Again, bus clear + bus settle time is 1.2us, however, this is
1402 * a minimum so we'll udelay ceil(1.2)
1405 #ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
1406 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1412 if (hostdata
->connected
) {
1413 NCR5380_write(MODE_REG
, MR_BASE
);
1414 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1418 ARB_PRINTK("scsi%d: won arbitration\n", HOSTNO
);
1421 * Now that we have won arbitration, start Selection process, asserting
1422 * the host and target ID's on the SCSI bus.
1425 NCR5380_write(OUTPUT_DATA_REG
, (hostdata
->id_mask
| (1 << cmd
->device
->id
)));
1428 * Raise ATN while SEL is true before BSY goes false from arbitration,
1429 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1430 * phase immediately after selection.
1433 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_BSY
|
1434 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1435 NCR5380_write(MODE_REG
, MR_BASE
);
1438 * Reselect interrupts must be turned off prior to the dropping of BSY,
1439 * otherwise we will trigger an interrupt.
1442 if (hostdata
->connected
) {
1443 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1447 NCR5380_write(SELECT_ENABLE_REG
, 0);
1450 * The initiator shall then wait at least two deskew delays and release
1453 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1456 NCR5380_write(INITIATOR_COMMAND_REG
, (ICR_BASE
| ICR_ASSERT_DATA
|
1457 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
));
1460 * Something weird happens when we cease to drive BSY - looks
1461 * like the board/chip is letting us do another read before the
1462 * appropriate propagation delay has expired, and we're confusing
1463 * a BSY signal from ourselves as the target's response to SELECTION.
1465 * A small delay (the 'C++' frontend breaks the pipeline with an
1466 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1467 * tighter 'C' code breaks and requires this) solves the problem -
1468 * the 1 us delay is arbitrary, and only used because this delay will
1469 * be the same on other platforms and since it works here, it should
1472 * wingel suggests that this could be due to failing to wait
1478 SEL_PRINTK("scsi%d: selecting target %d\n", HOSTNO
, cmd
->device
->id
);
1481 * The SCSI specification calls for a 250 ms timeout for the actual
1485 timeout
= jiffies
+ 25;
1488 while (time_before(jiffies
, timeout
) && !(NCR5380_read(STATUS_REG
) & SR_BSY
))
1492 * No less than two deskew delays after the initiator detects the
1493 * BSY signal is true, it shall release the SEL signal and may
1494 * change the DATA BUS. -wingel
1499 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1501 if (!(NCR5380_read(STATUS_REG
) & SR_BSY
)) {
1502 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1503 if (hostdata
->targets_present
& (1 << cmd
->device
->id
)) {
1504 printk(KERN_ERR
"scsi%d: weirdness\n", HOSTNO
);
1505 if (hostdata
->restart_select
)
1506 printk(KERN_NOTICE
"\trestart select\n");
1507 NCR_PRINT(NDEBUG_ANY
);
1508 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1511 cmd
->result
= DID_BAD_TARGET
<< 16;
1512 #ifdef NCR5380_STATS
1513 collect_stats(hostdata
, cmd
);
1518 cmd
->scsi_done(cmd
);
1519 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1520 SEL_PRINTK("scsi%d: target did not respond within 250ms\n", HOSTNO
);
1521 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1525 hostdata
->targets_present
|= (1 << cmd
->device
->id
);
1528 /* Wait for start of REQ/ACK handshake */
1529 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
))
1532 SEL_PRINTK("scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
1533 HOSTNO
, cmd
->device
->id
);
1534 tmp
[0] = IDENTIFY(1, cmd
->device
->lun
);
1537 if (cmd
->tag
!= TAG_NONE
) {
1538 tmp
[1] = hostdata
->last_message
= SIMPLE_QUEUE_TAG
;
1546 #endif /* SUPPORT_TAGS */
1548 /* Send message(s) */
1550 phase
= PHASE_MSGOUT
;
1551 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1552 SEL_PRINTK("scsi%d: nexus established.\n", HOSTNO
);
1553 hostdata
->connected
= cmd
;
1554 #ifndef SUPPORT_TAGS
1555 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
1558 initialize_SCp(cmd
);
1565 * Note : this code is not as quick as it could be, however it
1566 * IS 100% reliable, and for the actual data transfer where speed
1567 * counts, we will always do a pseudo DMA or DMA transfer.
1570 static int NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1571 unsigned char *phase
, int *count
,
1572 unsigned char **data
)
1574 register unsigned char p
= *phase
, tmp
;
1575 register int c
= *count
;
1576 register unsigned char *d
= *data
;
1579 * The NCR5380 chip will only drive the SCSI bus when the
1580 * phase specified in the appropriate bits of the TARGET COMMAND
1581 * REGISTER match the STATUS REGISTER
1584 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1588 * Wait for assertion of REQ, after which the phase bits will be
1591 while (!((tmp
= NCR5380_read(STATUS_REG
)) & SR_REQ
))
1594 HSH_PRINTK("scsi%d: REQ detected\n", HOSTNO
);
1596 /* Check for phase mismatch */
1597 if ((tmp
& PHASE_MASK
) != p
) {
1598 PIO_PRINTK("scsi%d: phase mismatch\n", HOSTNO
);
1599 NCR_PRINT_PHASE(NDEBUG_PIO
);
1603 /* Do actual transfer from SCSI bus to / from memory */
1605 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1607 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1612 * The SCSI standard suggests that in MSGOUT phase, the initiator
1613 * should drop ATN on the last byte of the message phase
1614 * after REQ has been asserted for the handshake but before
1615 * the initiator raises ACK.
1619 if (!((p
& SR_MSG
) && c
> 1)) {
1620 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1621 NCR_PRINT(NDEBUG_PIO
);
1622 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1623 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1625 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1626 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1627 NCR_PRINT(NDEBUG_PIO
);
1628 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1629 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1632 NCR_PRINT(NDEBUG_PIO
);
1633 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1636 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1639 HSH_PRINTK("scsi%d: req false, handshake complete\n", HOSTNO
);
1642 * We have several special cases to consider during REQ/ACK handshaking :
1643 * 1. We were in MSGOUT phase, and we are on the last byte of the
1644 * message. ATN must be dropped as ACK is dropped.
1646 * 2. We are in a MSGIN phase, and we are on the last byte of the
1647 * message. We must exit with ACK asserted, so that the calling
1648 * code may raise ATN before dropping ACK to reject the message.
1650 * 3. ACK and ATN are clear and the target may proceed as normal.
1652 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1653 if (p
== PHASE_MSGOUT
&& c
> 1)
1654 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1656 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1660 PIO_PRINTK("scsi%d: residual %d\n", HOSTNO
, c
);
1664 tmp
= NCR5380_read(STATUS_REG
);
1665 /* The phase read from the bus is valid if either REQ is (already)
1666 * asserted or if ACK hasn't been released yet. The latter is the case if
1667 * we're in MSGIN and all wanted bytes have been received.
1669 if ((tmp
& SR_REQ
) || (p
== PHASE_MSGIN
&& c
== 0))
1670 *phase
= tmp
& PHASE_MASK
;
1672 *phase
= PHASE_UNKNOWN
;
1674 if (!c
|| (*phase
== p
))
1681 * Function : do_abort (Scsi_Host *host)
1683 * Purpose : abort the currently established nexus. Should only be
1684 * called from a routine which can drop into a
1686 * Returns : 0 on success, -1 on failure.
1689 static int do_abort(struct Scsi_Host
*host
)
1691 unsigned char tmp
, *msgptr
, phase
;
1694 /* Request message out phase */
1695 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1698 * Wait for the target to indicate a valid phase by asserting
1699 * REQ. Once this happens, we'll have either a MSGOUT phase
1700 * and can immediately send the ABORT message, or we'll have some
1701 * other phase and will have to source/sink data.
1703 * We really don't care what value was on the bus or what value
1704 * the target sees, so we just handshake.
1707 while (!((tmp
= NCR5380_read(STATUS_REG
)) & SR_REQ
))
1710 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1712 if ((tmp
& PHASE_MASK
) != PHASE_MSGOUT
) {
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
| ICR_ASSERT_ATN
);
1723 phase
= PHASE_MSGOUT
;
1724 NCR5380_transfer_pio(host
, &phase
, &len
, &msgptr
);
1727 * If we got here, and the command completed successfully,
1728 * we're about to go into bus free state.
1731 return len
? -1 : 0;
1734 #if defined(REAL_DMA)
1736 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1737 * unsigned char *phase, int *count, unsigned char **data)
1739 * Purpose : transfers data in given phase using either real
1742 * Inputs : instance - instance of driver, *phase - pointer to
1743 * what phase is expected, *count - pointer to number of
1744 * bytes to transfer, **data - pointer to data pointer.
1746 * Returns : -1 when different phase is entered without transferring
1747 * maximum number of bytes, 0 if all bytes or transfered or exit
1750 * Also, *phase, *count, *data are modified in place.
1755 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1756 unsigned char *phase
, int *count
,
1757 unsigned char **data
)
1759 SETUP_HOSTDATA(instance
);
1760 register int c
= *count
;
1761 register unsigned char p
= *phase
;
1762 register unsigned char *d
= *data
;
1764 unsigned long flags
;
1766 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1771 if (atari_read_overruns
&& (p
& SR_IO
))
1772 c
-= atari_read_overruns
;
1774 DMA_PRINTK("scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1775 HOSTNO
, (p
& SR_IO
) ? "reading" : "writing",
1776 c
, (p
& SR_IO
) ? "to" : "from", d
);
1778 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1781 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_ENABLE_EOP_INTR
| MR_MONITOR_BSY
);
1782 #endif /* def REAL_DMA */
1785 /* On the Medusa, it is a must to initialize the DMA before
1786 * starting the NCR. This is also the cleaner way for the TT.
1788 local_irq_save(flags
);
1789 hostdata
->dma_len
= (p
& SR_IO
) ?
1790 NCR5380_dma_read_setup(instance
, d
, c
) :
1791 NCR5380_dma_write_setup(instance
, d
, c
);
1792 local_irq_restore(flags
);
1796 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1798 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1799 NCR5380_write(START_DMA_SEND_REG
, 0);
1803 /* On the Falcon, the DMA setup must be done after the last */
1804 /* NCR access, else the DMA setup gets trashed!
1806 local_irq_save(flags
);
1807 hostdata
->dma_len
= (p
& SR_IO
) ?
1808 NCR5380_dma_read_setup(instance
, d
, c
) :
1809 NCR5380_dma_write_setup(instance
, d
, c
);
1810 local_irq_restore(flags
);
1814 #endif /* defined(REAL_DMA) */
1817 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
1819 SETUP_HOSTDATA(instance
);
1820 unsigned long flags
;
1821 unsigned char msgout
= NOP
;
1824 #if defined(REAL_DMA)
1827 unsigned char *data
;
1828 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
1829 Scsi_Cmnd
*cmd
= (Scsi_Cmnd
*) hostdata
->connected
;
1832 tmp
= NCR5380_read(STATUS_REG
);
1833 /* We only have a valid SCSI phase when REQ is asserted */
1835 phase
= (tmp
& PHASE_MASK
);
1836 if (phase
!= old_phase
) {
1838 NCR_PRINT_PHASE(NDEBUG_INFORMATION
);
1841 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
1842 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1844 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1846 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1848 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1856 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1857 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
1858 "aborted\n", HOSTNO
);
1861 cmd
->result
= DID_ERROR
<< 16;
1862 cmd
->scsi_done(cmd
);
1867 * If there is no room left in the current buffer in the
1868 * scatter-gather list, move onto the next one.
1871 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
1873 --cmd
->SCp
.buffers_residual
;
1874 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
1875 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
1876 /* ++roman: Try to merge some scatter-buffers if
1877 * they are at contiguous physical addresses.
1879 merge_contiguous_buffers(cmd
);
1880 INF_PRINTK("scsi%d: %d bytes and %d buffers left\n",
1881 HOSTNO
, cmd
->SCp
.this_residual
,
1882 cmd
->SCp
.buffers_residual
);
1886 * The preferred transfer method is going to be
1887 * PSEUDO-DMA for systems that are strictly PIO,
1888 * since we can let the hardware do the handshaking.
1890 * For this to work, we need to know the transfersize
1891 * ahead of time, since the pseudo-DMA code will sit
1892 * in an unconditional loop.
1895 /* ++roman: I suggest, this should be
1897 * instead of leaving REAL_DMA out.
1900 #if defined(REAL_DMA)
1901 if (!cmd
->device
->borken
&&
1902 (transfersize
= NCR5380_dma_xfer_len(instance
,cmd
,phase
)) > 31) {
1904 cmd
->SCp
.phase
= phase
;
1905 if (NCR5380_transfer_dma(instance
, &phase
,
1906 &len
, (unsigned char **)&cmd
->SCp
.ptr
)) {
1908 * If the watchdog timer fires, all future
1909 * accesses to this device will use the
1911 printk(KERN_NOTICE
"scsi%d: switching target %d "
1912 "lun %d to slow handshake\n", HOSTNO
,
1913 cmd
->device
->id
, cmd
->device
->lun
);
1914 cmd
->device
->borken
= 1;
1915 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1919 cmd
->result
= DID_ERROR
<< 16;
1920 cmd
->scsi_done(cmd
);
1923 /* ++roman: When using real DMA,
1924 * information_transfer() should return after
1925 * starting DMA since it has nothing more to
1930 cmd
->SCp
.this_residual
-= transfersize
- len
;
1934 #endif /* defined(REAL_DMA) */
1935 NCR5380_transfer_pio(instance
, &phase
,
1936 (int *)&cmd
->SCp
.this_residual
,
1937 (unsigned char **)&cmd
->SCp
.ptr
);
1942 NCR5380_write(SELECT_ENABLE_REG
, 0); /* disable reselects */
1943 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1944 cmd
->SCp
.Message
= tmp
;
1948 * Linking lets us reduce the time required to get the
1949 * next command out to the device, hopefully this will
1950 * mean we don't waste another revolution due to the delays
1951 * required by ARBITRATION and another SELECTION.
1953 * In the current implementation proposal, low level drivers
1954 * merely have to start the next command, pointed to by
1955 * next_link, done() is called as with unlinked commands.
1958 case LINKED_CMD_COMPLETE
:
1959 case LINKED_FLG_CMD_COMPLETE
:
1960 /* Accept message by clearing ACK */
1961 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1963 LNK_PRINTK("scsi%d: target %d lun %d linked command "
1964 "complete.\n", HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
1966 /* Enable reselect interrupts */
1967 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1969 * Sanity check : A linked command should only terminate
1970 * with one of these messages if there are more linked
1971 * commands available.
1974 if (!cmd
->next_link
) {
1975 printk(KERN_NOTICE
"scsi%d: target %d lun %d "
1976 "linked command complete, no next_link\n",
1977 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
1983 initialize_SCp(cmd
->next_link
);
1984 /* The next command is still part of this process; copy it
1985 * and don't free it! */
1986 cmd
->next_link
->tag
= cmd
->tag
;
1987 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
1988 LNK_PRINTK("scsi%d: target %d lun %d linked request "
1989 "done, calling scsi_done().\n",
1990 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
1991 #ifdef NCR5380_STATS
1992 collect_stats(hostdata
, cmd
);
1994 cmd
->scsi_done(cmd
);
1995 cmd
= hostdata
->connected
;
1997 #endif /* def LINKED */
1999 case COMMAND_COMPLETE
:
2000 /* Accept message by clearing ACK */
2001 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2002 /* ++guenther: possible race with Falcon locking */
2003 falcon_dont_release
++;
2004 hostdata
->connected
= NULL
;
2005 QU_PRINTK("scsi%d: command for target %d, lun %d "
2006 "completed\n", HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2009 if (status_byte(cmd
->SCp
.Status
) == QUEUE_FULL
) {
2010 /* Turn a QUEUE FULL status into BUSY, I think the
2011 * mid level cannot handle QUEUE FULL :-( (The
2012 * command is retried after BUSY). Also update our
2013 * queue size to the number of currently issued
2016 /* ++Andreas: the mid level code knows about
2018 TAG_ALLOC
*ta
= &TagAlloc
[cmd
->device
->id
][cmd
->device
->lun
];
2019 TAG_PRINTK("scsi%d: target %d lun %d returned "
2020 "QUEUE_FULL after %d commands\n",
2021 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
,
2023 if (ta
->queue_size
> ta
->nr_allocated
)
2024 ta
->nr_allocated
= ta
->queue_size
;
2027 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2029 /* Enable reselect interrupts */
2030 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2033 * I'm not sure what the correct thing to do here is :
2035 * If the command that just executed is NOT a request
2036 * sense, the obvious thing to do is to set the result
2037 * code to the values of the stored parameters.
2039 * If it was a REQUEST SENSE command, we need some way to
2040 * differentiate between the failure code of the original
2041 * and the failure code of the REQUEST sense - the obvious
2042 * case is success, where we fall through and leave the
2043 * result code unchanged.
2045 * The non-obvious place is where the REQUEST SENSE failed
2048 if (cmd
->cmnd
[0] != REQUEST_SENSE
)
2049 cmd
->result
= cmd
->SCp
.Status
| (cmd
->SCp
.Message
<< 8);
2050 else if (status_byte(cmd
->SCp
.Status
) != GOOD
)
2051 cmd
->result
= (cmd
->result
& 0x00ffff) | (DID_ERROR
<< 16);
2054 if ((cmd
->cmnd
[0] == REQUEST_SENSE
) &&
2055 hostdata
->ses
.cmd_len
) {
2056 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
2057 hostdata
->ses
.cmd_len
= 0 ;
2060 if ((cmd
->cmnd
[0] != REQUEST_SENSE
) &&
2061 (status_byte(cmd
->SCp
.Status
) == CHECK_CONDITION
)) {
2062 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
2064 ASEN_PRINTK("scsi%d: performing request sense\n", HOSTNO
);
2066 local_irq_save(flags
);
2067 LIST(cmd
,hostdata
->issue_queue
);
2068 SET_NEXT(cmd
, hostdata
->issue_queue
);
2069 hostdata
->issue_queue
= (Scsi_Cmnd
*) cmd
;
2070 local_irq_restore(flags
);
2071 QU_PRINTK("scsi%d: REQUEST SENSE added to head of "
2072 "issue queue\n", H_NO(cmd
));
2074 #endif /* def AUTOSENSE */
2076 #ifdef NCR5380_STATS
2077 collect_stats(hostdata
, cmd
);
2079 cmd
->scsi_done(cmd
);
2082 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2084 * Restore phase bits to 0 so an interrupted selection,
2085 * arbitration can resume.
2087 NCR5380_write(TARGET_COMMAND_REG
, 0);
2089 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2092 falcon_dont_release
--;
2093 /* ++roman: For Falcon SCSI, release the lock on the
2094 * ST-DMA here if no other commands are waiting on the
2095 * disconnected queue.
2097 falcon_release_lock_if_possible(hostdata
);
2099 case MESSAGE_REJECT
:
2100 /* Accept message by clearing ACK */
2101 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2102 /* Enable reselect interrupts */
2103 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2104 switch (hostdata
->last_message
) {
2105 case HEAD_OF_QUEUE_TAG
:
2106 case ORDERED_QUEUE_TAG
:
2107 case SIMPLE_QUEUE_TAG
:
2108 /* The target obviously doesn't support tagged
2109 * queuing, even though it announced this ability in
2110 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2111 * clear 'tagged_supported' and lock the LUN, since
2112 * the command is treated as untagged further on.
2114 cmd
->device
->tagged_supported
= 0;
2115 hostdata
->busy
[cmd
->device
->id
] |= (1 << cmd
->device
->lun
);
2116 cmd
->tag
= TAG_NONE
;
2117 TAG_PRINTK("scsi%d: target %d lun %d rejected "
2118 "QUEUE_TAG message; tagged queuing "
2120 HOSTNO
, cmd
->device
->id
, cmd
->device
->lun
);
2125 /* Accept message by clearing ACK */
2126 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2127 local_irq_save(flags
);
2128 cmd
->device
->disconnect
= 1;
2129 LIST(cmd
,hostdata
->disconnected_queue
);
2130 SET_NEXT(cmd
, hostdata
->disconnected_queue
);
2131 hostdata
->connected
= NULL
;
2132 hostdata
->disconnected_queue
= cmd
;
2133 local_irq_restore(flags
);
2134 QU_PRINTK("scsi%d: command for target %d lun %d was "
2135 "moved from connected to the "
2136 "disconnected_queue\n", HOSTNO
,
2137 cmd
->device
->id
, cmd
->device
->lun
);
2139 * Restore phase bits to 0 so an interrupted selection,
2140 * arbitration can resume.
2142 NCR5380_write(TARGET_COMMAND_REG
, 0);
2144 /* Enable reselect interrupts */
2145 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2146 /* Wait for bus free to avoid nasty timeouts */
2147 while ((NCR5380_read(STATUS_REG
) & SR_BSY
) && !hostdata
->connected
)
2151 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2152 * operation, in violation of the SCSI spec so we can safely
2153 * ignore SAVE/RESTORE pointers calls.
2155 * Unfortunately, some disks violate the SCSI spec and
2156 * don't issue the required SAVE_POINTERS message before
2157 * disconnecting, and we have to break spec to remain
2161 case RESTORE_POINTERS
:
2162 /* Accept message by clearing ACK */
2163 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2164 /* Enable reselect interrupts */
2165 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2167 case EXTENDED_MESSAGE
:
2169 * Extended messages are sent in the following format :
2171 * 0 EXTENDED_MESSAGE == 1
2172 * 1 length (includes one byte for code, doesn't
2173 * include first two bytes)
2175 * 3..length+1 arguments
2177 * Start the extended message buffer with the EXTENDED_MESSAGE
2178 * byte, since spi_print_msg() wants the whole thing.
2180 extended_msg
[0] = EXTENDED_MESSAGE
;
2181 /* Accept first byte by clearing ACK */
2182 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2184 EXT_PRINTK("scsi%d: receiving extended message\n", HOSTNO
);
2187 data
= extended_msg
+ 1;
2188 phase
= PHASE_MSGIN
;
2189 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2190 EXT_PRINTK("scsi%d: length=%d, code=0x%02x\n", HOSTNO
,
2191 (int)extended_msg
[1], (int)extended_msg
[2]);
2193 if (!len
&& extended_msg
[1] <=
2194 (sizeof(extended_msg
) - 1)) {
2195 /* Accept third byte by clearing ACK */
2196 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2197 len
= extended_msg
[1] - 1;
2198 data
= extended_msg
+ 3;
2199 phase
= PHASE_MSGIN
;
2201 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2202 EXT_PRINTK("scsi%d: message received, residual %d\n",
2205 switch (extended_msg
[2]) {
2208 case EXTENDED_MODIFY_DATA_POINTER
:
2209 case EXTENDED_EXTENDED_IDENTIFY
:
2213 printk(KERN_NOTICE
"scsi%d: error receiving "
2214 "extended message\n", HOSTNO
);
2217 printk(KERN_NOTICE
"scsi%d: extended message "
2218 "code %02x length %d is too long\n",
2219 HOSTNO
, extended_msg
[2], extended_msg
[1]);
2222 /* Fall through to reject message */
2225 * If we get something weird that we aren't expecting,
2230 printk(KERN_DEBUG
"scsi%d: rejecting message ", HOSTNO
);
2231 spi_print_msg(extended_msg
);
2233 } else if (tmp
!= EXTENDED_MESSAGE
)
2234 printk(KERN_DEBUG
"scsi%d: rejecting unknown "
2235 "message %02x from target %d, lun %d\n",
2236 HOSTNO
, tmp
, cmd
->device
->id
, cmd
->device
->lun
);
2238 printk(KERN_DEBUG
"scsi%d: rejecting unknown "
2240 "code %02x, length %d from target %d, lun %d\n",
2241 HOSTNO
, extended_msg
[1], extended_msg
[0],
2242 cmd
->device
->id
, cmd
->device
->lun
);
2245 msgout
= MESSAGE_REJECT
;
2246 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
2248 } /* switch (tmp) */
2253 hostdata
->last_message
= msgout
;
2254 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2255 if (msgout
== ABORT
) {
2259 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2261 hostdata
->connected
= NULL
;
2262 cmd
->result
= DID_ERROR
<< 16;
2263 #ifdef NCR5380_STATS
2264 collect_stats(hostdata
, cmd
);
2266 cmd
->scsi_done(cmd
);
2267 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
2268 falcon_release_lock_if_possible(hostdata
);
2276 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2281 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2282 cmd
->SCp
.Status
= tmp
;
2285 printk("scsi%d: unknown phase\n", HOSTNO
);
2286 NCR_PRINT(NDEBUG_ANY
);
2287 } /* switch(phase) */
2288 } /* if (tmp * SR_REQ) */
2293 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2295 * Purpose : does reselection, initializing the instance->connected
2296 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2297 * nexus has been reestablished,
2299 * Inputs : instance - this instance of the NCR5380.
2304 static void NCR5380_reselect(struct Scsi_Host
*instance
)
2306 SETUP_HOSTDATA(instance
);
2307 unsigned char target_mask
;
2308 unsigned char lun
, phase
;
2313 unsigned char msg
[3];
2314 unsigned char *data
;
2315 Scsi_Cmnd
*tmp
= NULL
, *prev
;
2316 /* unsigned long flags; */
2319 * Disable arbitration, etc. since the host adapter obviously
2320 * lost, and tell an interrupted NCR5380_select() to restart.
2323 NCR5380_write(MODE_REG
, MR_BASE
);
2324 hostdata
->restart_select
= 1;
2326 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2328 RSL_PRINTK("scsi%d: reselect\n", HOSTNO
);
2331 * At this point, we have detected that our SCSI ID is on the bus,
2332 * SEL is true and BSY was false for at least one bus settle delay
2335 * We must assert BSY ourselves, until the target drops the SEL
2339 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2341 while (NCR5380_read(STATUS_REG
) & SR_SEL
)
2343 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2346 * Wait for target to go into MSGIN.
2349 while (!(NCR5380_read(STATUS_REG
) & SR_REQ
))
2354 phase
= PHASE_MSGIN
;
2355 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2357 if (!(msg
[0] & 0x80)) {
2358 printk(KERN_DEBUG
"scsi%d: expecting IDENTIFY message, got ", HOSTNO
);
2363 lun
= (msg
[0] & 0x07);
2366 /* If the phase is still MSGIN, the target wants to send some more
2367 * messages. In case it supports tagged queuing, this is probably a
2368 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2371 if (phase
== PHASE_MSGIN
&& setup_use_tagged_queuing
) {
2372 /* Accept previous IDENTIFY message by clearing ACK */
2373 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2376 if (!NCR5380_transfer_pio(instance
, &phase
, &len
, &data
) &&
2377 msg
[1] == SIMPLE_QUEUE_TAG
)
2379 TAG_PRINTK("scsi%d: target mask %02x, lun %d sent tag %d at "
2380 "reselection\n", HOSTNO
, target_mask
, lun
, tag
);
2385 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2386 * just reestablished, and remove it from the disconnected queue.
2389 for (tmp
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
, prev
= NULL
;
2390 tmp
; prev
= tmp
, tmp
= NEXT(tmp
)) {
2391 if ((target_mask
== (1 << tmp
->device
->id
)) && (lun
== tmp
->device
->lun
)
2393 && (tag
== tmp
->tag
)
2396 /* ++guenther: prevent race with falcon_release_lock */
2397 falcon_dont_release
++;
2399 REMOVE(prev
, NEXT(prev
), tmp
, NEXT(tmp
));
2400 SET_NEXT(prev
, NEXT(tmp
));
2402 REMOVE(-1, hostdata
->disconnected_queue
, tmp
, NEXT(tmp
));
2403 hostdata
->disconnected_queue
= NEXT(tmp
);
2405 SET_NEXT(tmp
, NULL
);
2411 printk(KERN_WARNING
"scsi%d: warning: target bitmask %02x lun %d "
2415 "not in disconnected_queue.\n",
2416 HOSTNO
, target_mask
, lun
2422 * Since we have an established nexus that we can't do anything
2423 * with, we must abort it.
2429 /* Accept message by clearing ACK */
2430 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2432 hostdata
->connected
= tmp
;
2433 RSL_PRINTK("scsi%d: nexus established, target = %d, lun = %d, tag = %d\n",
2434 HOSTNO
, tmp
->device
->id
, tmp
->device
->lun
, tmp
->tag
);
2435 falcon_dont_release
--;
2441 int NCR5380_abort(Scsi_Cmnd
*cmd
)
2443 struct Scsi_Host
*instance
= cmd
->device
->host
;
2444 SETUP_HOSTDATA(instance
);
2445 Scsi_Cmnd
*tmp
, **prev
;
2446 unsigned long flags
;
2448 printk(KERN_NOTICE
"scsi%d: aborting command\n", HOSTNO
);
2449 scsi_print_command(cmd
);
2451 NCR5380_print_status(instance
);
2453 local_irq_save(flags
);
2455 if (!IS_A_TT() && !falcon_got_lock
)
2456 printk(KERN_ERR
"scsi%d: !!BINGO!! Falcon has no lock in NCR5380_abort\n",
2459 ABRT_PRINTK("scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO
,
2460 NCR5380_read(BUS_AND_STATUS_REG
),
2461 NCR5380_read(STATUS_REG
));
2464 * Case 1 : If the command is the currently executing command,
2465 * we'll set the aborted flag and return control so that
2466 * information transfer routine can exit cleanly.
2469 if (hostdata
->connected
== cmd
) {
2471 ABRT_PRINTK("scsi%d: aborting connected command\n", HOSTNO
);
2473 * We should perform BSY checking, and make sure we haven't slipped
2477 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2479 * Since we can't change phases until we've completed the current
2480 * handshake, we have to source or sink a byte of data if the current
2481 * phase is not MSGOUT.
2485 * Return control to the executing NCR drive so we can clear the
2486 * aborted flag and get back into our main loop.
2489 if (do_abort(instance
) == 0) {
2490 hostdata
->aborted
= 1;
2491 hostdata
->connected
= NULL
;
2492 cmd
->result
= DID_ABORT
<< 16;
2496 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2498 local_irq_restore(flags
);
2499 cmd
->scsi_done(cmd
);
2500 falcon_release_lock_if_possible(hostdata
);
2501 return SCSI_ABORT_SUCCESS
;
2503 /* local_irq_restore(flags); */
2504 printk("scsi%d: abort of connected command failed!\n", HOSTNO
);
2505 return SCSI_ABORT_ERROR
;
2510 * Case 2 : If the command hasn't been issued yet, we simply remove it
2511 * from the issue queue.
2513 for (prev
= (Scsi_Cmnd
**)&(hostdata
->issue_queue
),
2514 tmp
= (Scsi_Cmnd
*)hostdata
->issue_queue
;
2515 tmp
; prev
= NEXTADDR(tmp
), tmp
= NEXT(tmp
)) {
2517 REMOVE(5, *prev
, tmp
, NEXT(tmp
));
2518 (*prev
) = NEXT(tmp
);
2519 SET_NEXT(tmp
, NULL
);
2520 tmp
->result
= DID_ABORT
<< 16;
2521 local_irq_restore(flags
);
2522 ABRT_PRINTK("scsi%d: abort removed command from issue queue.\n",
2524 /* Tagged queuing note: no tag to free here, hasn't been assigned
2526 tmp
->scsi_done(tmp
);
2527 falcon_release_lock_if_possible(hostdata
);
2528 return SCSI_ABORT_SUCCESS
;
2533 * Case 3 : If any commands are connected, we're going to fail the abort
2534 * and let the high level SCSI driver retry at a later time or
2537 * Timeouts, and therefore aborted commands, will be highly unlikely
2538 * and handling them cleanly in this situation would make the common
2539 * case of noresets less efficient, and would pollute our code. So,
2543 if (hostdata
->connected
) {
2544 local_irq_restore(flags
);
2545 ABRT_PRINTK("scsi%d: abort failed, command connected.\n", HOSTNO
);
2546 return SCSI_ABORT_SNOOZE
;
2550 * Case 4: If the command is currently disconnected from the bus, and
2551 * there are no connected commands, we reconnect the I_T_L or
2552 * I_T_L_Q nexus associated with it, go into message out, and send
2555 * This case is especially ugly. In order to reestablish the nexus, we
2556 * need to call NCR5380_select(). The easiest way to implement this
2557 * function was to abort if the bus was busy, and let the interrupt
2558 * handler triggered on the SEL for reselect take care of lost arbitrations
2559 * where necessary, meaning interrupts need to be enabled.
2561 * When interrupts are enabled, the queues may change - so we
2562 * can't remove it from the disconnected queue before selecting it
2563 * because that could cause a failure in hashing the nexus if that
2564 * device reselected.
2566 * Since the queues may change, we can't use the pointers from when we
2569 * So, we must first locate the command, and if NCR5380_select()
2570 * succeeds, then issue the abort, relocate the command and remove
2571 * it from the disconnected queue.
2574 for (tmp
= (Scsi_Cmnd
*) hostdata
->disconnected_queue
; tmp
;
2577 local_irq_restore(flags
);
2578 ABRT_PRINTK("scsi%d: aborting disconnected command.\n", HOSTNO
);
2580 if (NCR5380_select(instance
, cmd
, (int)cmd
->tag
))
2581 return SCSI_ABORT_BUSY
;
2583 ABRT_PRINTK("scsi%d: nexus reestablished.\n", HOSTNO
);
2587 local_irq_save(flags
);
2588 for (prev
= (Scsi_Cmnd
**)&(hostdata
->disconnected_queue
),
2589 tmp
= (Scsi_Cmnd
*)hostdata
->disconnected_queue
;
2590 tmp
; prev
= NEXTADDR(tmp
), tmp
= NEXT(tmp
)) {
2592 REMOVE(5, *prev
, tmp
, NEXT(tmp
));
2594 SET_NEXT(tmp
, NULL
);
2595 tmp
->result
= DID_ABORT
<< 16;
2596 /* We must unlock the tag/LUN immediately here, since the
2597 * target goes to BUS FREE and doesn't send us another
2598 * message (COMMAND_COMPLETE or the like)
2603 hostdata
->busy
[cmd
->device
->id
] &= ~(1 << cmd
->device
->lun
);
2605 local_irq_restore(flags
);
2606 tmp
->scsi_done(tmp
);
2607 falcon_release_lock_if_possible(hostdata
);
2608 return SCSI_ABORT_SUCCESS
;
2615 * Case 5 : If we reached this point, the command was not found in any of
2618 * We probably reached this point because of an unlikely race condition
2619 * between the command completing successfully and the abortion code,
2620 * so we won't panic, but we will notify the user in case something really
2624 local_irq_restore(flags
);
2625 printk(KERN_INFO
"scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO
);
2627 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2628 * possible at all) At least, we should check if the lock could be
2629 * released after the abort, in case it is kept due to some bug.
2631 falcon_release_lock_if_possible(hostdata
);
2633 return SCSI_ABORT_NOT_RUNNING
;
2638 * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
2640 * Purpose : reset the SCSI bus.
2642 * Returns : SCSI_RESET_WAKEUP
2646 static int NCR5380_bus_reset(Scsi_Cmnd
*cmd
)
2648 SETUP_HOSTDATA(cmd
->device
->host
);
2650 unsigned long flags
;
2651 Scsi_Cmnd
*connected
, *disconnected_queue
;
2653 if (!IS_A_TT() && !falcon_got_lock
)
2654 printk(KERN_ERR
"scsi%d: !!BINGO!! Falcon has no lock in NCR5380_reset\n",
2657 NCR5380_print_status(cmd
->device
->host
);
2660 NCR5380_write(TARGET_COMMAND_REG
,
2661 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
)));
2663 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
2665 /* reset NCR registers */
2666 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2667 NCR5380_write(MODE_REG
, MR_BASE
);
2668 NCR5380_write(TARGET_COMMAND_REG
, 0);
2669 NCR5380_write(SELECT_ENABLE_REG
, 0);
2670 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2671 * through anymore ... */
2672 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
2675 /* MSch: old-style reset: actually abort all command processing here */
2677 /* After the reset, there are no more connected or disconnected commands
2678 * and no busy units; to avoid problems with re-inserting the commands
2679 * into the issue_queue (via scsi_done()), the aborted commands are
2680 * remembered in local variables first.
2682 local_irq_save(flags
);
2683 connected
= (Scsi_Cmnd
*)hostdata
->connected
;
2684 hostdata
->connected
= NULL
;
2685 disconnected_queue
= (Scsi_Cmnd
*)hostdata
->disconnected_queue
;
2686 hostdata
->disconnected_queue
= NULL
;
2690 for (i
= 0; i
< 8; ++i
)
2691 hostdata
->busy
[i
] = 0;
2693 hostdata
->dma_len
= 0;
2695 local_irq_restore(flags
);
2697 /* In order to tell the mid-level code which commands were aborted,
2698 * set the command status to DID_RESET and call scsi_done() !!!
2699 * This ultimately aborts processing of these commands in the mid-level.
2702 if ((cmd
= connected
)) {
2703 ABRT_PRINTK("scsi%d: reset aborted a connected command\n", H_NO(cmd
));
2704 cmd
->result
= (cmd
->result
& 0xffff) | (DID_RESET
<< 16);
2705 cmd
->scsi_done(cmd
);
2708 for (i
= 0; (cmd
= disconnected_queue
); ++i
) {
2709 disconnected_queue
= NEXT(cmd
);
2710 SET_NEXT(cmd
, NULL
);
2711 cmd
->result
= (cmd
->result
& 0xffff) | (DID_RESET
<< 16);
2712 cmd
->scsi_done(cmd
);
2715 ABRT_PRINTK("scsi: reset aborted %d disconnected command(s)\n", i
);
2717 /* The Falcon lock should be released after a reset...
2719 /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2720 * unlocking and enabling dma interrupt.
2722 /* falcon_release_lock_if_possible( hostdata );*/
2724 /* since all commands have been explicitly terminated, we need to tell
2725 * the midlevel code that the reset was SUCCESSFUL, and there is no
2726 * need to 'wake up' the commands by a request_sense
2728 return SCSI_RESET_SUCCESS
| SCSI_RESET_BUS_RESET
;