GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / atari_NCR5380.c
bloba2d7ab6f96d104e90dfa8a40c547378d68792f06
1 /*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
6 * Note that these routines also work with NR53c400 family chips.
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
14 * DISTRIBUTION RELEASE 6.
16 * For more information, please consult
18 * NCR 5380 Family
19 * SCSI Protocol Controller
20 * Databook
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
25 * 1+ (719) 578-3400
26 * 1+ (800) 334-5454
30 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
31 * this file, too:
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
48 * calls.
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
63 * interrupt.
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)
80 #define LIST(x, y) \
81 do { \
82 printk("LINE:%d Adding %p to %p\n", \
83 __LINE__, (void*)(x), (void*)(y)); \
84 if ((x) == (y)) \
85 udelay(5); \
86 } while (0)
87 #define REMOVE(w, x, y, z) \
88 do { \
89 printk("LINE:%d Removing: %p->%p %p->%p \n", \
90 __LINE__, (void*)(w), (void*)(x), \
91 (void*)(y), (void*)(z)); \
92 if ((x) == (y)) \
93 udelay(5); \
94 } while (0)
95 #else
96 #define LIST(x,y)
97 #define REMOVE(w,x,y,z)
98 #endif
100 #ifndef notyet
101 #undef LINKED
102 #endif
106 * Using this file :
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
128 * REAL functions :
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
141 * PSEUDO functions :
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
149 * NCR5380_reset
150 * NCR5380_abort
151 * NCR5380_proc_info
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
183 #ifdef SUPPORT_TAGS
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
202 * correctly.
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 */
221 #undef TAG_NONE
222 #define TAG_NONE 0xff
224 typedef struct {
225 DECLARE_BITMAP(allocated, MAX_TAGS);
226 int nr_allocated;
227 int queue_size;
228 } TAG_ALLOC;
230 static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
233 static void __init init_tags(void)
235 int target, lun;
236 TAG_ALLOC *ta;
238 if (!setup_use_tagged_queuing)
239 return;
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
260 * conditions.
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))
268 return 1;
269 if (!should_be_tagged ||
270 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
271 return 0;
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);
276 return 1;
278 return 0;
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
284 * untagged.
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) {
296 cmd->tag = TAG_NONE;
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);
300 } else {
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);
305 ta->nr_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,
315 * unlock the LUN.
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);
329 } else {
330 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
331 clear_bit(cmd->tag, ta->allocated);
332 ta->nr_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)
341 int target, lun;
342 TAG_ALLOC *ta;
344 if (!setup_use_tagged_queuing)
345 return;
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;
376 int cnt = 1;
377 #endif
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)
385 ++cnt;
386 #endif
387 ++cmd->SCp.buffer;
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);
396 #endif
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);
424 } else {
425 cmd->SCp.buffer = NULL;
426 cmd->SCp.buffers_residual = 0;
427 cmd->SCp.ptr = NULL;
428 cmd->SCp.this_residual = 0;
432 #include <linux/delay.h>
434 #if NDEBUG
435 static struct {
436 unsigned char mask;
437 const char *name;
438 } signals[] = {
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}
442 }, basrs[] = {
443 {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}
444 }, icrs[] = {
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"},
448 {0, NULL}
449 }, mrs[] = {
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"},
455 {0, NULL}
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;
469 unsigned long flags;
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);
494 printk("\n");
497 static struct {
498 unsigned char value;
499 const char *name;
500 } phases[] = {
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;
517 int i;
519 status = NCR5380_read(STATUS_REG);
520 if (!(status & SR_REQ))
521 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
522 else {
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);
530 #else /* !NDEBUG */
532 /* dummies... */
533 static inline void NCR5380_print(struct Scsi_Host *instance)
536 static inline void NCR5380_print_phase(struct Scsi_Host *instance)
540 #endif
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)
564 if (!main_running) {
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
568 finished. */
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)
578 static int done = 0;
579 if (!done) {
580 INI_PRINTK("scsi : NCR5380_all_init()\n");
581 done = 1;
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"
598 #ifdef AUTOSENSE
599 " AUTOSENSE"
600 #endif
601 #ifdef REAL_DMA
602 " REAL DMA"
603 #endif
604 #ifdef PARITY
605 " PARITY"
606 #endif
607 #ifdef SUPPORT_TAGS
608 " SCSI-2 TAGGED QUEUING"
609 #endif
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)
625 char *pr_bfr;
626 char *start;
627 int len;
629 NCR_PRINT(NDEBUG_ANY);
630 NCR_PRINT_PHASE(NDEBUG_ANY);
632 pr_bfr = (char *)__get_free_page(GFP_ATOMIC);
633 if (!pr_bfr) {
634 printk("NCR5380_print_status: no memory for print buffer\n");
635 return;
637 len = NCR5380_proc_info(instance, pr_bfr, &start, 0, PAGE_SIZE, 0);
638 pr_bfr[len] = 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
658 #undef SPRINTF
659 #define SPRINTF(fmt,args...) \
660 do { \
661 if (pos + strlen(fmt) + 20 /* slop */ < buffer + length) \
662 pos += sprintf(pos, fmt , ## args); \
663 } while(0)
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)
669 char *pos = buffer;
670 struct NCR5380_hostdata *hostdata;
671 Scsi_Cmnd *ptr;
672 unsigned long flags;
673 off_t begin = 0;
674 #define check_offset() \
675 do { \
676 if (pos - buffer < offset - begin) { \
677 begin += pos - buffer; \
678 pos = buffer; \
680 } while (0)
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);
687 check_offset();
688 local_irq_save(flags);
689 SPRINTF("NCR5380: coroutine is%s running.\n",
690 main_running ? "" : "n't");
691 check_offset();
692 if (!hostdata->connected)
693 SPRINTF("scsi%d: no currently connected command\n", HOSTNO);
694 else
695 pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected,
696 pos, buffer, length);
697 SPRINTF("scsi%d: issue_queue\n", HOSTNO);
698 check_offset();
699 for (ptr = (Scsi_Cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr)) {
700 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
701 check_offset();
704 SPRINTF("scsi%d: disconnected_queue\n", HOSTNO);
705 check_offset();
706 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
707 ptr = NEXT(ptr)) {
708 pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
709 check_offset();
712 local_irq_restore(flags);
713 *start = buffer + (offset - begin);
714 if (pos - buffer < offset - begin)
715 return 0;
716 else if (pos - buffer - (offset - begin) < length)
717 return pos - buffer - (offset - begin);
718 return length;
721 static char *lprint_Scsi_Cmnd(Scsi_Cmnd *cmd, char *pos, char *buffer, int length)
723 int i, s;
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 = ");
728 command = cmd->cmnd;
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]);
732 SPRINTF("\n");
733 return pos;
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)
751 int i;
752 SETUP_HOSTDATA(instance);
754 NCR5380_all_init();
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;
764 #ifdef SUPPORT_TAGS
765 init_tags();
766 #endif
767 #if defined(REAL_DMA)
768 hostdata->dma_len = 0;
769 #endif
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;
776 if (!the_template) {
777 the_template = instance->hostt;
778 first_instance = instance;
781 #ifndef AUTOSENSE
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);
793 return 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.
805 * Returns : 0
807 * Side effects :
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);
817 Scsi_Cmnd *tmp;
818 int oldto;
819 unsigned long flags;
821 #if (NDEBUG & NDEBUG_NO_WRITE)
822 switch (cmd->cmnd[0]) {
823 case WRITE_6:
824 case WRITE_10:
825 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
826 H_NO(cmd));
827 cmd->result = (DID_ERROR << 16);
828 done(cmd);
829 return 0;
831 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
833 #ifdef NCR5380_STATS
834 # ifdef NCR5380_STAT_LIMIT
835 if (scsi_bufflen(cmd) > NCR5380_STAT_LIMIT)
836 # endif
837 switch (cmd->cmnd[0]) {
838 case WRITE:
839 case WRITE_6:
840 case WRITE_10:
841 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
842 hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
843 hostdata->pendingw++;
844 break;
845 case READ:
846 case READ_6:
847 case READ_10:
848 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
849 hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
850 hostdata->pendingr++;
851 break;
853 #endif
856 * We use the host_scribble field as a pointer to the next command
857 * in a queue
860 SET_NEXT(cmd, NULL);
861 cmd->scsi_done = done;
863 cmd->result = 0;
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.
885 if (!IS_A_TT()) {
886 /* perhaps stop command timer here */
887 falcon_get_lock();
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;
894 } else {
895 for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
896 NEXT(tmp); tmp = NEXT(tmp))
898 LIST(cmd, tmp);
899 SET_NEXT(tmp, cmd);
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)
914 queue_main();
915 else
916 NCR5380_main(NULL);
917 return 0;
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);
937 int done;
938 unsigned long flags;
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
947 * the loop again.
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. */
961 if (main_running)
962 return;
963 main_running = 1;
965 local_save_flags(flags);
966 do {
967 local_irq_disable(); /* Freeze request queues */
968 done = 1;
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)
982 printk(" LOOP\n");
983 /* else printk("\n"); */
984 #endif
985 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
986 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) {
988 #if (NDEBUG & NDEBUG_LISTS)
989 if (prev != tmp)
990 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n",
991 tmp, tmp->device->id, hostdata->busy[tmp->device->id],
992 tmp->device->lun);
993 #endif
994 /* When we find one, remove it from the issue queue. */
995 /* ++guenther: possible race with Falcon locking */
996 if (
997 #ifdef SUPPORT_TAGS
998 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
999 #else
1000 !(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))
1001 #endif
1003 /* ++guenther: just to be sure, this must be atomic */
1004 local_irq_disable();
1005 if (prev) {
1006 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
1007 SET_NEXT(prev, NEXT(tmp));
1008 } else {
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
1031 * entire unit.
1033 /* ++roman: ...and the standard also requires that
1034 * REQUEST SENSE command are untagged.
1037 #ifdef SUPPORT_TAGS
1038 cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE);
1039 #endif
1040 if (!NCR5380_select(instance, tmp,
1041 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1042 TAG_NEXT)) {
1043 falcon_dont_release--;
1044 /* release if target did not response! */
1045 falcon_release_lock_if_possible(hostdata);
1046 break;
1047 } else {
1048 local_irq_disable();
1049 LIST(tmp, hostdata->issue_queue);
1050 SET_NEXT(tmp, hostdata->issue_queue);
1051 hostdata->issue_queue = tmp;
1052 #ifdef SUPPORT_TAGS
1053 cmd_free_tag(tmp);
1054 #endif
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)
1060 break;
1062 } /* if target/lun/target queue is not busy */
1063 } /* for issue_queue */
1064 } /* if (!hostdata->connected) */
1066 if (hostdata->connected
1067 #ifdef REAL_DMA
1068 && !hostdata->dma_len
1069 #endif
1071 local_irq_restore(flags);
1072 MAIN_PRINTK("scsi%d: main: performing information transfer\n",
1073 HOSTNO);
1074 NCR5380_information_transfer(instance);
1075 MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO);
1076 done = 0;
1078 } while (!done);
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... */
1083 main_running = 0;
1084 local_irq_restore(flags);
1088 #ifdef REAL_DMA
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);
1109 return;
1112 if (atari_read_overruns) {
1113 p = hostdata->connected->SCp.phase;
1114 if (p & SR_IO) {
1115 udelay(10);
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);
1120 overrun = 1;
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;
1145 if (overrun) {
1146 DMA_PRINTK("Got an input overrun, using saved byte\n");
1147 *(*data)++ = saved_data;
1148 (*count)--;
1149 cnt--;
1150 toPIO--;
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()
1166 * as required.
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;
1176 unsigned char basr;
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)) {
1187 done = 0;
1188 ENABLE_IRQ();
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);
1198 } else {
1200 * The rest of the interrupt conditions can occur only during a
1201 * DMA transfer
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 );
1217 done = 0;
1218 ENABLE_IRQ();
1219 } else
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) */
1231 handled = 1;
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);
1239 if (!done) {
1240 INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO);
1241 /* Put a call to NCR5380_main() on the queue... */
1242 queue_main();
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)
1252 # endif
1253 switch (cmd->cmnd[0]) {
1254 case WRITE:
1255 case WRITE_6:
1256 case WRITE_10:
1257 hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
1258 /*hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);*/
1259 hostdata->pendingw--;
1260 break;
1261 case READ:
1262 case READ_6:
1263 case READ_10:
1264 hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
1265 /*hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);*/
1266 hostdata->pendingr--;
1267 break;
1270 #endif
1273 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1274 * int tag);
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
1287 * did not respond.
1289 * Side effects :
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;
1308 int len;
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,
1315 instance->this_id);
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);
1325 return -1;
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);
1350 return -1;
1353 #else /* NCR_TIMEOUT */
1354 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) &&
1355 !hostdata->connected)
1357 #endif
1359 ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO);
1361 if (hostdata->connected) {
1362 NCR5380_write(MODE_REG, MR_BASE);
1363 return -1;
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().
1372 udelay(3);
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",
1381 HOSTNO);
1382 return -1;
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",
1396 HOSTNO);
1397 return -1;
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... */
1407 udelay(15);
1408 #else
1409 udelay(2);
1410 #endif
1412 if (hostdata->connected) {
1413 NCR5380_write(MODE_REG, MR_BASE);
1414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1415 return -1;
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);
1444 return -1;
1447 NCR5380_write(SELECT_ENABLE_REG, 0);
1450 * The initiator shall then wait at least two deskew delays and release
1451 * the BSY signal.
1453 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1455 /* Reset BSY */
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
1470 * work there.
1472 * wingel suggests that this could be due to failing to wait
1473 * one deskew delay.
1476 udelay(1);
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
1482 * selection.
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
1497 udelay(1);
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);
1509 return -1;
1511 cmd->result = DID_BAD_TARGET << 16;
1512 #ifdef NCR5380_STATS
1513 collect_stats(hostdata, cmd);
1514 #endif
1515 #ifdef SUPPORT_TAGS
1516 cmd_free_tag(cmd);
1517 #endif
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);
1522 return 0;
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);
1536 #ifdef SUPPORT_TAGS
1537 if (cmd->tag != TAG_NONE) {
1538 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1539 tmp[2] = cmd->tag;
1540 len = 3;
1541 } else
1542 len = 1;
1543 #else
1544 len = 1;
1545 cmd->tag = 0;
1546 #endif /* SUPPORT_TAGS */
1548 /* Send message(s) */
1549 data = tmp;
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);
1556 #endif
1558 initialize_SCp(cmd);
1560 return 0;
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));
1586 do {
1588 * Wait for assertion of REQ, after which the phase bits will be
1589 * valid
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);
1600 break;
1603 /* Do actual transfer from SCSI bus to / from memory */
1604 if (!(p & SR_IO))
1605 NCR5380_write(OUTPUT_DATA_REG, *d);
1606 else
1607 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1609 ++d;
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.
1618 if (!(p & SR_IO)) {
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);
1624 } else {
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);
1631 } else {
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);
1655 else
1656 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1658 } while (--c);
1660 PIO_PRINTK("scsi%d: residual %d\n", HOSTNO, c);
1662 *count = c;
1663 *data = d;
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;
1671 else
1672 *phase = PHASE_UNKNOWN;
1674 if (!c || (*phase == p))
1675 return 0;
1676 else
1677 return -1;
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;
1692 int len;
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 |
1714 ICR_ASSERT_ACK);
1715 while (NCR5380_read(STATUS_REG) & SR_REQ)
1717 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1720 tmp = ABORT;
1721 msgptr = &tmp;
1722 len = 1;
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
1740 * or pseudo DMA.
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
1748 * is in same phase.
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;
1763 unsigned char tmp;
1764 unsigned long flags;
1766 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1767 *phase = tmp;
1768 return -1;
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));
1780 #ifdef REAL_DMA
1781 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1782 #endif /* def REAL_DMA */
1784 if (IS_A_TT()) {
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);
1795 if (p & SR_IO)
1796 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1797 else {
1798 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1799 NCR5380_write(START_DMA_SEND_REG, 0);
1802 if (!IS_A_TT()) {
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);
1812 return 0;
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;
1822 int sink = 0;
1823 int len;
1824 #if defined(REAL_DMA)
1825 int transfersize;
1826 #endif
1827 unsigned char *data;
1828 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1829 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
1831 while (1) {
1832 tmp = NCR5380_read(STATUS_REG);
1833 /* We only have a valid SCSI phase when REQ is asserted */
1834 if (tmp & SR_REQ) {
1835 phase = (tmp & PHASE_MASK);
1836 if (phase != old_phase) {
1837 old_phase = 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 |
1845 ICR_ASSERT_ACK);
1846 while (NCR5380_read(STATUS_REG) & SR_REQ)
1848 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1849 ICR_ASSERT_ATN);
1850 sink = 0;
1851 continue;
1854 switch (phase) {
1855 case PHASE_DATAOUT:
1856 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1857 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
1858 "aborted\n", HOSTNO);
1859 sink = 1;
1860 do_abort(instance);
1861 cmd->result = DID_ERROR << 16;
1862 cmd->scsi_done(cmd);
1863 return;
1864 #endif
1865 case PHASE_DATAIN:
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) {
1872 ++cmd->SCp.buffer;
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
1896 * #if def(REAL_DMA)
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) {
1903 len = transfersize;
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
1910 * polled-IO. */
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 |
1916 ICR_ASSERT_ATN);
1917 sink = 1;
1918 do_abort(instance);
1919 cmd->result = DID_ERROR << 16;
1920 cmd->scsi_done(cmd);
1921 } else {
1922 #ifdef REAL_DMA
1923 /* ++roman: When using real DMA,
1924 * information_transfer() should return after
1925 * starting DMA since it has nothing more to
1926 * do.
1928 return;
1929 #else
1930 cmd->SCp.this_residual -= transfersize - len;
1931 #endif
1933 } else
1934 #endif /* defined(REAL_DMA) */
1935 NCR5380_transfer_pio(instance, &phase,
1936 (int *)&cmd->SCp.this_residual,
1937 (unsigned char **)&cmd->SCp.ptr);
1938 break;
1939 case PHASE_MSGIN:
1940 len = 1;
1941 data = &tmp;
1942 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
1943 NCR5380_transfer_pio(instance, &phase, &len, &data);
1944 cmd->SCp.Message = tmp;
1946 switch (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.
1957 #ifdef LINKED
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);
1978 sink = 1;
1979 do_abort(instance);
1980 return;
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);
1993 #endif
1994 cmd->scsi_done(cmd);
1995 cmd = hostdata->connected;
1996 break;
1997 #endif /* def LINKED */
1998 case ABORT:
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);
2007 #ifdef SUPPORT_TAGS
2008 cmd_free_tag(cmd);
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
2014 * commands now.
2016 /* ++Andreas: the mid level code knows about
2017 QUEUE_FULL now. */
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,
2022 ta->nr_allocated);
2023 if (ta->queue_size > ta->nr_allocated)
2024 ta->nr_allocated = ta->queue_size;
2026 #else
2027 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2028 #endif
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);
2053 #ifdef AUTOSENSE
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));
2073 } else
2074 #endif /* def AUTOSENSE */
2076 #ifdef NCR5380_STATS
2077 collect_stats(hostdata, cmd);
2078 #endif
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)
2090 barrier();
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);
2098 return;
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 "
2119 "disabled\n",
2120 HOSTNO, cmd->device->id, cmd->device->lun);
2121 break;
2123 break;
2124 case DISCONNECT:
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)
2148 barrier();
2149 return;
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
2158 * compatible.
2160 case SAVE_POINTERS:
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);
2166 break;
2167 case EXTENDED_MESSAGE:
2169 * Extended messages are sent in the following format :
2170 * Byte
2171 * 0 EXTENDED_MESSAGE == 1
2172 * 1 length (includes one byte for code, doesn't
2173 * include first two bytes)
2174 * 2 code
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);
2186 len = 2;
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",
2203 HOSTNO, len);
2205 switch (extended_msg[2]) {
2206 case EXTENDED_SDTR:
2207 case EXTENDED_WDTR:
2208 case EXTENDED_MODIFY_DATA_POINTER:
2209 case EXTENDED_EXTENDED_IDENTIFY:
2210 tmp = 0;
2212 } else if (len) {
2213 printk(KERN_NOTICE "scsi%d: error receiving "
2214 "extended message\n", HOSTNO);
2215 tmp = 0;
2216 } else {
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]);
2220 tmp = 0;
2222 /* Fall through to reject message */
2225 * If we get something weird that we aren't expecting,
2226 * reject it.
2228 default:
2229 if (!tmp) {
2230 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2231 spi_print_msg(extended_msg);
2232 printk("\n");
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);
2237 else
2238 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2239 "extended message "
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);
2247 break;
2248 } /* switch (tmp) */
2249 break;
2250 case PHASE_MSGOUT:
2251 len = 1;
2252 data = &msgout;
2253 hostdata->last_message = msgout;
2254 NCR5380_transfer_pio(instance, &phase, &len, &data);
2255 if (msgout == ABORT) {
2256 #ifdef SUPPORT_TAGS
2257 cmd_free_tag(cmd);
2258 #else
2259 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2260 #endif
2261 hostdata->connected = NULL;
2262 cmd->result = DID_ERROR << 16;
2263 #ifdef NCR5380_STATS
2264 collect_stats(hostdata, cmd);
2265 #endif
2266 cmd->scsi_done(cmd);
2267 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2268 falcon_release_lock_if_possible(hostdata);
2269 return;
2271 msgout = NOP;
2272 break;
2273 case PHASE_CMDOUT:
2274 len = cmd->cmd_len;
2275 data = cmd->cmnd;
2276 NCR5380_transfer_pio(instance, &phase, &len, &data);
2277 break;
2278 case PHASE_STATIN:
2279 len = 1;
2280 data = &tmp;
2281 NCR5380_transfer_pio(instance, &phase, &len, &data);
2282 cmd->SCp.Status = tmp;
2283 break;
2284 default:
2285 printk("scsi%d: unknown phase\n", HOSTNO);
2286 NCR_PRINT(NDEBUG_ANY);
2287 } /* switch(phase) */
2288 } /* if (tmp * SR_REQ) */
2289 } /* while (1) */
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;
2309 int len;
2310 #ifdef SUPPORT_TAGS
2311 unsigned char tag;
2312 #endif
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
2333 * (400 ns).
2335 * We must assert BSY ourselves, until the target drops the SEL
2336 * signal.
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))
2352 len = 1;
2353 data = msg;
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);
2359 spi_print_msg(msg);
2360 do_abort(instance);
2361 return;
2363 lun = (msg[0] & 0x07);
2365 #ifdef SUPPORT_TAGS
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.
2370 tag = TAG_NONE;
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);
2374 len = 2;
2375 data = msg + 1;
2376 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2377 msg[1] == SIMPLE_QUEUE_TAG)
2378 tag = msg[2];
2379 TAG_PRINTK("scsi%d: target mask %02x, lun %d sent tag %d at "
2380 "reselection\n", HOSTNO, target_mask, lun, tag);
2382 #endif
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)
2392 #ifdef SUPPORT_TAGS
2393 && (tag == tmp->tag)
2394 #endif
2396 /* ++guenther: prevent race with falcon_release_lock */
2397 falcon_dont_release++;
2398 if (prev) {
2399 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
2400 SET_NEXT(prev, NEXT(tmp));
2401 } else {
2402 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2403 hostdata->disconnected_queue = NEXT(tmp);
2405 SET_NEXT(tmp, NULL);
2406 break;
2410 if (!tmp) {
2411 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2412 #ifdef SUPPORT_TAGS
2413 "tag %d "
2414 #endif
2415 "not in disconnected_queue.\n",
2416 HOSTNO, target_mask, lun
2417 #ifdef SUPPORT_TAGS
2418 , tag
2419 #endif
2422 * Since we have an established nexus that we can't do anything
2423 * with, we must abort it.
2425 do_abort(instance);
2426 return;
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--;
2440 static
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",
2457 HOSTNO);
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
2474 * into BUS FREE.
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;
2493 #ifdef SUPPORT_TAGS
2494 cmd_free_tag(cmd);
2495 #else
2496 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2497 #endif
2498 local_irq_restore(flags);
2499 cmd->scsi_done(cmd);
2500 falcon_release_lock_if_possible(hostdata);
2501 return SCSI_ABORT_SUCCESS;
2502 } else {
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)) {
2516 if (cmd == 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",
2523 HOSTNO);
2524 /* Tagged queuing note: no tag to free here, hasn't been assigned
2525 * yet... */
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
2535 * issue a reset.
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,
2540 * we fail.
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
2553 * an abort message.
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
2567 * first locate it.
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;
2575 tmp = NEXT(tmp)) {
2576 if (cmd == 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);
2585 do_abort(instance);
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)) {
2591 if (cmd == tmp) {
2592 REMOVE(5, *prev, tmp, NEXT(tmp));
2593 *prev = 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)
2600 #ifdef SUPPORT_TAGS
2601 cmd_free_tag(tmp);
2602 #else
2603 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2604 #endif
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
2616 * the queues.
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
2621 * broke.
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);
2649 int i;
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",
2655 H_NO(cmd));
2657 NCR5380_print_status(cmd->device->host);
2659 /* get in phase */
2660 NCR5380_write(TARGET_COMMAND_REG,
2661 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
2662 /* assert RST */
2663 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
2664 udelay(40);
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;
2687 #ifdef SUPPORT_TAGS
2688 free_all_tags();
2689 #endif
2690 for (i = 0; i < 8; ++i)
2691 hostdata->busy[i] = 0;
2692 #ifdef REAL_DMA
2693 hostdata->dma_len = 0;
2694 #endif
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);
2714 if (i > 0)
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;