* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / scsi / mac_NCR5380.c
blob331d1c0ac68bdb213929b6aed5a4546758ef2aea
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.
77 /*
78 * Michael: To port Romans driver to the Macintosh, I've left most of the code
79 * unchanged, in order to make later implemantation of REAL_DMA easier.
81 * Alan: In order to make it easier to read and as the 5380 based Mac's never
82 * have DMA I took the real DMA out of mac_scsi.c but not this file.
84 * With luck we can merge this back with the ST folks in time.
86 * Changes:
88 * - all Falcon-specific stuff (ST-DMA locking) was removed
93 #if (NDEBUG & NDEBUG_LISTS)
94 #define LIST(x,y) \
95 { printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); \
96 if ((x)==(y)) udelay(5); }
97 #define REMOVE(w,x,y,z) \
98 { printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, \
99 (void*)(w), (void*)(x), (void*)(y), (void*)(z)); \
100 if ((x)==(y)) udelay(5); }
101 #else
102 #define LIST(x,y)
103 #define REMOVE(w,x,y,z)
104 #endif
106 #ifndef notyet
107 #undef LINKED
108 #endif
111 * Design
112 * Issues :
114 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
115 * and specifically for each board rather than each chip. This makes their
116 * adaptation to platforms like the Mac (Some of which use NCR5380's)
117 * more difficult than it has to be.
119 * Also, many of the SCSI drivers were written before the command queuing
120 * routines were implemented, meaning their implementations of queued
121 * commands were hacked on rather than designed in from the start.
123 * When I designed the Linux SCSI drivers I figured that
124 * while having two different SCSI boards in a system might be useful
125 * for debugging things, two of the same type wouldn't be used.
126 * Well, I was wrong and a number of users have mailed me about running
127 * multiple high-performance SCSI boards in a server.
129 * Finally, when I get questions from users, I have no idea what
130 * revision of my driver they are running.
132 * This driver attempts to address these problems :
133 * This is a generic 5380 driver. To use it on a different platform,
134 * one simply writes appropriate system specific macros (ie, data
135 * transfer - some PC's will use the I/O bus, 68K's must use
136 * memory mapped) and drops this file in their 'C' wrapper.
138 * As far as command queueing, two queues are maintained for
139 * each 5380 in the system - commands that haven't been issued yet,
140 * and commands that are currently executing. This means that an
141 * unlimited number of commands may be queued, letting
142 * more commands propagate from the higher driver levels giving higher
143 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
144 * allowing multiple commands to propagate all the way to a SCSI-II device
145 * while a command is already executing.
147 * To solve the multiple-boards-in-the-same-system problem,
148 * there is a separate instance structure for each instance
149 * of a 5380 in the system. So, multiple NCR5380 drivers will
150 * be able to coexist with appropriate changes to the high level
151 * SCSI code.
153 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
154 * number (updated for each public release) printed by the
155 * NCR5380_print_options command, which should be called from the
156 * wrapper detect function, so that I know what release of the driver
157 * users are using.
159 * Issues specific to the NCR5380 :
161 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
162 * piece of hardware that requires you to sit in a loop polling for
163 * the REQ signal as long as you are connected. Some devices are
164 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
165 * while doing long seek operations.
167 * The workaround for this is to keep track of devices that have
168 * disconnected. If the device hasn't disconnected, for commands that
169 * should disconnect, we do something like
171 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
173 * Some tweaking of N and M needs to be done. An algorithm based
174 * on "time to data" would give the best results as long as short time
175 * to datas (ie, on the same track) were considered, however these
176 * broken devices are the exception rather than the rule and I'd rather
177 * spend my time optimizing for the normal case.
179 * Architecture :
181 * At the heart of the design is a coroutine, NCR5380_main,
182 * which is started when not running by the interrupt handler,
183 * timer, and queue command function. It attempts to establish
184 * I_T_L or I_T_L_Q nexuses by removing the commands from the
185 * issue queue and calling NCR5380_select() if a nexus
186 * is not established.
188 * Once a nexus is established, the NCR5380_information_transfer()
189 * phase goes through the various phases as instructed by the target.
190 * if the target goes into MSG IN and sends a DISCONNECT message,
191 * the command structure is placed into the per instance disconnected
192 * queue, and NCR5380_main tries to find more work. If USLEEP
193 * was defined, and the target is idle for too long, the system
194 * will try to sleep.
196 * If a command has disconnected, eventually an interrupt will trigger,
197 * calling NCR5380_intr() which will in turn call NCR5380_reselect
198 * to reestablish a nexus. This will run main if necessary.
200 * On command termination, the done function will be called as
201 * appropriate.
203 * SCSI pointers are maintained in the SCp field of SCSI command
204 * structures, being initialized after the command is connected
205 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
206 * Note that in violation of the standard, an implicit SAVE POINTERS operation
207 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
211 * Using this file :
212 * This file a skeleton Linux SCSI driver for the NCR 5380 series
213 * of chips. To use it, you write a architecture specific functions
214 * and macros and include this file in your driver.
216 * These macros control options :
217 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
218 * for commands that return with a CHECK CONDITION status.
220 * LINKED - if defined, linked commands are supported.
222 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
224 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
226 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
228 * These macros MUST be defined :
230 * NCR5380_read(register) - read from the specified register
232 * NCR5380_write(register, value) - write to the specific register
234 * Either real DMA *or* pseudo DMA may be implemented
235 * REAL functions :
236 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
237 * Note that the DMA setup functions should return the number of bytes
238 * that they were able to program the controller for.
240 * Also note that generic i386/PC versions of these macros are
241 * available as NCR5380_i386_dma_write_setup,
242 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
244 * NCR5380_dma_write_setup(instance, src, count) - initialize
245 * NCR5380_dma_read_setup(instance, dst, count) - initialize
246 * NCR5380_dma_residual(instance); - residual count
248 * PSEUDO functions :
249 * NCR5380_pwrite(instance, src, count)
250 * NCR5380_pread(instance, dst, count);
252 * If nothing specific to this implementation needs doing (ie, with external
253 * hardware), you must also define
255 * NCR5380_queue_command
256 * NCR5380_reset
257 * NCR5380_abort
258 * NCR5380_proc_info
260 * to be the global entry points into the specific driver, ie
261 * #define NCR5380_queue_command t128_queue_command.
263 * If this is not done, the routines will be defined as static functions
264 * with the NCR5380* names and the user must provide a globally
265 * accessible wrapper function.
267 * The generic driver is initialized by calling NCR5380_init(instance),
268 * after setting the appropriate host specific fields and ID. If the
269 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
270 * possible) function may be used. Before the specific driver initialization
271 * code finishes, NCR5380_print_options should be called.
274 static struct Scsi_Host *first_instance = NULL;
275 static Scsi_Host_Template *the_template = NULL;
277 /* Macros ease life... :-) */
278 #define SETUP_HOSTDATA(in) \
279 struct NCR5380_hostdata *hostdata = \
280 (struct NCR5380_hostdata *)(in)->hostdata
281 #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
283 #define NEXT(cmd) ((Scsi_Cmnd *)((cmd)->host_scribble))
284 #define NEXTADDR(cmd) ((Scsi_Cmnd **)&((cmd)->host_scribble))
286 #define HOSTNO instance->host_no
287 #define H_NO(cmd) (cmd)->host->host_no
289 #ifdef SUPPORT_TAGS
292 * Functions for handling tagged queuing
293 * =====================================
295 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
297 * Using consecutive numbers for the tags is no good idea in my eyes. There
298 * could be wrong re-usings if the counter (8 bit!) wraps and some early
299 * command has been preempted for a long time. My solution: a bitfield for
300 * remembering used tags.
302 * There's also the problem that each target has a certain queue size, but we
303 * cannot know it in advance :-( We just see a QUEUE_FULL status being
304 * returned. So, in this case, the driver internal queue size assumption is
305 * reduced to the number of active tags if QUEUE_FULL is returned by the
306 * target. The command is returned to the mid-level, but with status changed
307 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
308 * correctly.
310 * We're also not allowed running tagged commands as long as an untagged
311 * command is active. And REQUEST SENSE commands after a contingent allegiance
312 * condition _must_ be untagged. To keep track whether an untagged command has
313 * been issued, the host->busy array is still employed, as it is without
314 * support for tagged queuing.
316 * One could suspect that there are possible race conditions between
317 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
318 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
319 * which already guaranteed to be running at most once. It is also the only
320 * place where tags/LUNs are allocated. So no other allocation can slip
321 * between that pair, there could only happen a reselection, which can free a
322 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
323 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
326 /* -1 for TAG_NONE is not possible with unsigned char cmd->tag */
327 #undef TAG_NONE
328 #define TAG_NONE 0xff
330 /* For the m68k, the number of bits in 'allocated' must be a multiple of 32! */
331 #if (MAX_TAGS % 32) != 0
332 #error "MAX_TAGS must be a multiple of 32!"
333 #endif
335 typedef struct {
336 char allocated[MAX_TAGS/8];
337 int nr_allocated;
338 int queue_size;
339 } TAG_ALLOC;
341 static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
344 static void init_tags( void )
346 int target, lun;
347 TAG_ALLOC *ta;
349 if (!setup_use_tagged_queuing)
350 return;
352 for( target = 0; target < 8; ++target ) {
353 for( lun = 0; lun < 8; ++lun ) {
354 ta = &TagAlloc[target][lun];
355 memset( &ta->allocated, 0, MAX_TAGS/8 );
356 ta->nr_allocated = 0;
357 /* At the beginning, assume the maximum queue size we could
358 * support (MAX_TAGS). This value will be decreased if the target
359 * returns QUEUE_FULL status.
361 ta->queue_size = MAX_TAGS;
367 /* Check if we can issue a command to this LUN: First see if the LUN is marked
368 * busy by an untagged command. If the command should use tagged queuing, also
369 * check that there is a free tag and the target's queue won't overflow. This
370 * function should be called with interrupts disabled to avoid race
371 * conditions.
374 static int is_lun_busy( Scsi_Cmnd *cmd, int should_be_tagged )
376 SETUP_HOSTDATA(cmd->host);
378 if (hostdata->busy[cmd->target] & (1 << cmd->lun))
379 return( 1 );
380 if (!should_be_tagged ||
381 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
382 return( 0 );
383 if (TagAlloc[cmd->target][cmd->lun].nr_allocated >=
384 TagAlloc[cmd->target][cmd->lun].queue_size ) {
385 TAG_PRINTK( "scsi%d: target %d lun %d: no free tags\n",
386 H_NO(cmd), cmd->target, cmd->lun );
387 return( 1 );
389 return( 0 );
393 /* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
394 * must be called before!), or reserve the LUN in 'busy' if the command is
395 * untagged.
398 static void cmd_get_tag( Scsi_Cmnd *cmd, int should_be_tagged )
400 SETUP_HOSTDATA(cmd->host);
402 /* If we or the target don't support tagged queuing, allocate the LUN for
403 * an untagged command.
405 if (!should_be_tagged ||
406 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
407 cmd->tag = TAG_NONE;
408 hostdata->busy[cmd->target] |= (1 << cmd->lun);
409 TAG_PRINTK( "scsi%d: target %d lun %d now allocated by untagged "
410 "command\n", H_NO(cmd), cmd->target, cmd->lun );
412 else {
413 TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
415 cmd->tag = find_first_zero_bit( &ta->allocated, MAX_TAGS );
416 set_bit( cmd->tag, &ta->allocated );
417 ta->nr_allocated++;
418 TAG_PRINTK( "scsi%d: using tag %d for target %d lun %d "
419 "(now %d tags in use)\n",
420 H_NO(cmd), cmd->tag, cmd->target, cmd->lun,
421 ta->nr_allocated );
426 /* Mark the tag of command 'cmd' as free, or in case of an untagged command,
427 * unlock the LUN.
430 static void cmd_free_tag( Scsi_Cmnd *cmd )
432 SETUP_HOSTDATA(cmd->host);
434 if (cmd->tag == TAG_NONE) {
435 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
436 TAG_PRINTK( "scsi%d: target %d lun %d untagged cmd finished\n",
437 H_NO(cmd), cmd->target, cmd->lun );
439 else if (cmd->tag >= MAX_TAGS) {
440 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
441 H_NO(cmd), cmd->tag );
443 else {
444 TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
445 clear_bit( cmd->tag, &ta->allocated );
446 ta->nr_allocated--;
447 TAG_PRINTK( "scsi%d: freed tag %d for target %d lun %d\n",
448 H_NO(cmd), cmd->tag, cmd->target, cmd->lun );
453 static void free_all_tags( void )
455 int target, lun;
456 TAG_ALLOC *ta;
458 if (!setup_use_tagged_queuing)
459 return;
461 for( target = 0; target < 8; ++target ) {
462 for( lun = 0; lun < 8; ++lun ) {
463 ta = &TagAlloc[target][lun];
464 memset( &ta->allocated, 0, MAX_TAGS/8 );
465 ta->nr_allocated = 0;
470 #endif /* SUPPORT_TAGS */
474 * Function: void merge_contiguous_buffers( Scsi_Cmnd *cmd )
476 * Purpose: Try to merge several scatter-gather requests into one DMA
477 * transfer. This is possible if the scatter buffers lie on
478 * physical contiguous addresses.
480 * Parameters: Scsi_Cmnd *cmd
481 * The command to work on. The first scatter buffer's data are
482 * assumed to be already transfered into ptr/this_residual.
485 static void merge_contiguous_buffers( Scsi_Cmnd *cmd )
487 unsigned long endaddr;
488 #if (NDEBUG & NDEBUG_MERGING)
489 unsigned long oldlen = cmd->SCp.this_residual;
490 int cnt = 1;
491 #endif
493 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
494 cmd->SCp.buffers_residual &&
495 virt_to_phys(cmd->SCp.buffer[1].address) == endaddr; ) {
497 MER_PRINTK("VTOP(%p) == %08lx -> merging\n",
498 cmd->SCp.buffer[1].address, endaddr);
499 #if (NDEBUG & NDEBUG_MERGING)
500 ++cnt;
501 #endif
502 ++cmd->SCp.buffer;
503 --cmd->SCp.buffers_residual;
504 cmd->SCp.this_residual += cmd->SCp.buffer->length;
505 endaddr += cmd->SCp.buffer->length;
507 #if (NDEBUG & NDEBUG_MERGING)
508 if (oldlen != cmd->SCp.this_residual)
509 MER_PRINTK("merged %d buffers from %p, new length %08x\n",
510 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
511 #endif
515 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
517 * Purpose : initialize the saved data pointers for cmd to point to the
518 * start of the buffer.
520 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
523 static __inline__ void initialize_SCp(Scsi_Cmnd *cmd)
526 * Initialize the Scsi Pointer field so that all of the commands in the
527 * various queues are valid.
530 if (cmd->use_sg) {
531 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
532 cmd->SCp.buffers_residual = cmd->use_sg - 1;
533 cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
534 cmd->SCp.this_residual = cmd->SCp.buffer->length;
535 /* ++roman: Try to merge some scatter-buffers if they are at
536 * contiguous physical addresses.
538 merge_contiguous_buffers( cmd );
539 } else {
540 cmd->SCp.buffer = NULL;
541 cmd->SCp.buffers_residual = 0;
542 cmd->SCp.ptr = (char *) cmd->request_buffer;
543 cmd->SCp.this_residual = cmd->request_bufflen;
547 #include <linux/config.h>
548 #include <linux/delay.h>
550 #if 1
551 static struct {
552 unsigned char mask;
553 const char * name;}
554 signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
555 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
556 { SR_SEL, "SEL" }, {0, NULL}},
557 basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
558 icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
559 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
560 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
561 {0, NULL}},
562 mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
563 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
564 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
565 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
566 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
567 {0, NULL}};
570 * Function : void NCR5380_print(struct Scsi_Host *instance)
572 * Purpose : print the SCSI bus signals for debugging purposes
574 * Input : instance - which NCR5380
577 static void NCR5380_print(struct Scsi_Host *instance) {
578 unsigned char status, data, basr, mr, icr, i;
579 unsigned long flags;
581 save_flags(flags);
582 cli();
583 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
584 status = NCR5380_read(STATUS_REG);
585 mr = NCR5380_read(MODE_REG);
586 icr = NCR5380_read(INITIATOR_COMMAND_REG);
587 basr = NCR5380_read(BUS_AND_STATUS_REG);
588 restore_flags(flags);
589 printk("STATUS_REG: %02x ", status);
590 for (i = 0; signals[i].mask ; ++i)
591 if (status & signals[i].mask)
592 printk(",%s", signals[i].name);
593 printk("\nBASR: %02x ", basr);
594 for (i = 0; basrs[i].mask ; ++i)
595 if (basr & basrs[i].mask)
596 printk(",%s", basrs[i].name);
597 printk("\nICR: %02x ", icr);
598 for (i = 0; icrs[i].mask; ++i)
599 if (icr & icrs[i].mask)
600 printk(",%s", icrs[i].name);
601 printk("\nMODE: %02x ", mr);
602 for (i = 0; mrs[i].mask; ++i)
603 if (mr & mrs[i].mask)
604 printk(",%s", mrs[i].name);
605 printk("\n");
608 static struct {
609 unsigned char value;
610 const char *name;
611 } phases[] = {
612 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
613 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
614 {PHASE_UNKNOWN, "UNKNOWN"}};
617 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
619 * Purpose : print the current SCSI phase for debugging purposes
621 * Input : instance - which NCR5380
624 static void NCR5380_print_phase(struct Scsi_Host *instance)
626 unsigned char status;
627 int i;
629 status = NCR5380_read(STATUS_REG);
630 if (!(status & SR_REQ))
631 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
632 else {
633 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
634 (phases[i].value != (status & PHASE_MASK)); ++i);
635 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
639 #else /* !NDEBUG */
641 /* dummies... */
642 __inline__ void NCR5380_print(struct Scsi_Host *instance) { };
643 __inline__ void NCR5380_print_phase(struct Scsi_Host *instance) { };
645 #endif
648 * ++roman: New scheme of calling NCR5380_main()
650 * If we're not in an interrupt, we can call our main directly, it cannot be
651 * already running. Else, we queue it on a task queue, if not 'main_running'
652 * tells us that a lower level is already executing it. This way,
653 * 'main_running' needs not be protected in a special way.
655 * queue_main() is a utility function for putting our main onto the task
656 * queue, if main_running is false. It should be called only from a
657 * interrupt or bottom half.
660 #include <linux/tqueue.h>
661 #include <linux/interrupt.h>
663 static volatile int main_running = 0;
664 static struct tq_struct NCR5380_tqueue = {
665 NULL, /* next */
666 0, /* sync */
667 (void (*)(void*))NCR5380_main, /* routine, must have (void *) arg... */
668 NULL /* data */
671 static __inline__ void queue_main(void)
673 if (!main_running) {
674 /* If in interrupt and NCR5380_main() not already running,
675 queue it on the 'immediate' task queue, to be processed
676 immediately after the current interrupt processing has
677 finished. */
678 queue_task(&NCR5380_tqueue, &tq_immediate);
679 mark_bh(IMMEDIATE_BH);
681 /* else: nothing to do: the running NCR5380_main() will pick up
682 any newly queued command. */
686 static void NCR5380_all_init (void)
688 static int done = 0;
689 if (!done) {
690 INI_PRINTK("scsi : NCR5380_all_init()\n");
691 done = 1;
697 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
699 * Purpose : called by probe code indicating the NCR5380 driver
700 * options that were selected.
702 * Inputs : instance, pointer to this instance. Unused.
705 static void NCR5380_print_options (struct Scsi_Host *instance)
707 printk(" generic options"
708 #ifdef AUTOSENSE
709 " AUTOSENSE"
710 #endif
711 #ifdef REAL_DMA
712 " REAL DMA"
713 #endif
714 #ifdef PSEUDO_DMA
715 " PSEUDO DMA"
716 #endif
717 #ifdef PARITY
718 " PARITY"
719 #endif
720 #ifdef SUPPORT_TAGS
721 " SCSI-2 TAGGED QUEUING"
722 #endif
724 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
728 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
730 * Purpose : print commands in the various queues, called from
731 * NCR5380_abort and NCR5380_debug to aid debugging.
733 * Inputs : instance, pointer to this instance.
736 static void NCR5380_print_status (struct Scsi_Host *instance)
738 char *pr_bfr;
739 char *start;
740 int len;
742 NCR_PRINT(NDEBUG_ANY);
743 NCR_PRINT_PHASE(NDEBUG_ANY);
745 pr_bfr = (char *) __get_free_page(GFP_ATOMIC);
746 if (!pr_bfr) {
747 printk("NCR5380_print_status: no memory for print buffer\n");
748 return;
750 len = NCR5380_proc_info(pr_bfr, &start, 0, PAGE_SIZE, HOSTNO, 0);
751 pr_bfr[len] = 0;
752 printk("\n%s\n", pr_bfr);
753 free_page((unsigned long) pr_bfr);
757 /******************************************/
759 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
761 * *buffer: I/O buffer
762 * **start: if inout == FALSE pointer into buffer where user read should start
763 * offset: current offset
764 * length: length of buffer
765 * hostno: Scsi_Host host_no
766 * inout: TRUE - user is writing; FALSE - user is reading
768 * Return the number of bytes read from or written
771 #undef SPRINTF
772 #define SPRINTF(fmt,args...) \
773 do { if (pos + strlen(fmt) + 20 /* slop */ < buffer + length) \
774 pos += sprintf(pos, fmt , ## args); } while(0)
775 static
776 char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length);
778 #ifndef NCR5380_proc_info
779 static
780 #endif
781 int NCR5380_proc_info (char *buffer, char **start, off_t offset,
782 int length, int hostno, int inout)
784 char *pos = buffer;
785 struct Scsi_Host *instance;
786 struct NCR5380_hostdata *hostdata;
787 Scsi_Cmnd *ptr;
788 unsigned long flags;
789 off_t begin = 0;
790 #define check_offset() \
791 do { \
792 if (pos - buffer < offset - begin) { \
793 begin += pos - buffer; \
794 pos = buffer; \
796 } while (0)
798 for (instance = first_instance; instance && HOSTNO != hostno;
799 instance = instance->next)
801 if (!instance)
802 return(-ESRCH);
803 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
805 if (inout) { /* Has data been written to the file ? */
806 return(-ENOSYS); /* Currently this is a no-op */
808 SPRINTF("NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
809 check_offset();
810 save_flags(flags);
811 cli();
812 SPRINTF("NCR5380: coroutine is%s running.\n", main_running ? "" : "n't");
813 check_offset();
814 if (!hostdata->connected)
815 SPRINTF("scsi%d: no currently connected command\n", HOSTNO);
816 else
817 pos = lprint_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected,
818 pos, buffer, length);
819 SPRINTF("scsi%d: issue_queue\n", HOSTNO);
820 check_offset();
821 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = NEXT(ptr)) {
822 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
823 check_offset();
826 SPRINTF("scsi%d: disconnected_queue\n", HOSTNO);
827 check_offset();
828 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
829 ptr = NEXT(ptr)) {
830 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
831 check_offset();
834 restore_flags(flags);
835 *start = buffer + (offset - begin);
836 if (pos - buffer < offset - begin)
837 return 0;
838 else if (pos - buffer - (offset - begin) < length)
839 return pos - buffer - (offset - begin);
840 return length;
843 static char *
844 lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length)
846 int i, s;
847 unsigned char *command;
848 SPRINTF("scsi%d: destination target %d, lun %d\n",
849 H_NO(cmd), cmd->target, cmd->lun);
850 SPRINTF(" command = ");
851 command = cmd->cmnd;
852 SPRINTF("%2d (0x%02x)", command[0], command[0]);
853 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
854 SPRINTF(" %02x", command[i]);
855 SPRINTF("\n");
856 return pos;
861 * Function : void NCR5380_init (struct Scsi_Host *instance)
863 * Purpose : initializes *instance and corresponding 5380 chip.
865 * Inputs : instance - instantiation of the 5380 driver.
867 * Notes : I assume that the host, hostno, and id bits have been
868 * set correctly. I don't care about the irq and other fields.
872 static void NCR5380_init (struct Scsi_Host *instance, int flags)
874 int i;
875 SETUP_HOSTDATA(instance);
877 NCR5380_all_init();
879 hostdata->aborted = 0;
880 hostdata->id_mask = 1 << instance->this_id;
881 hostdata->id_higher_mask = 0;
882 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
883 if (i > hostdata->id_mask)
884 hostdata->id_higher_mask |= i;
885 for (i = 0; i < 8; ++i)
886 hostdata->busy[i] = 0;
887 #ifdef SUPPORT_TAGS
888 init_tags();
889 #endif
890 #if defined (REAL_DMA)
891 hostdata->dma_len = 0;
892 #endif
893 hostdata->targets_present = 0;
894 hostdata->connected = NULL;
895 hostdata->issue_queue = NULL;
896 hostdata->disconnected_queue = NULL;
897 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
899 if (!the_template) {
900 the_template = instance->hostt;
901 first_instance = instance;
905 #ifndef AUTOSENSE
906 if ((instance->cmd_per_lun > 1) || (instance->can_queue > 1))
907 printk("scsi%d: WARNING : support for multiple outstanding commands enabled\n"
908 " without AUTOSENSE option, contingent allegiance conditions may\n"
909 " be incorrectly cleared.\n", HOSTNO);
910 #endif /* def AUTOSENSE */
912 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
913 NCR5380_write(MODE_REG, MR_BASE);
914 NCR5380_write(TARGET_COMMAND_REG, 0);
915 NCR5380_write(SELECT_ENABLE_REG, 0);
919 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
920 * void (*done)(Scsi_Cmnd *))
922 * Purpose : enqueues a SCSI command
924 * Inputs : cmd - SCSI command, done - function called on completion, with
925 * a pointer to the command descriptor.
927 * Returns : 0
929 * Side effects :
930 * cmd is added to the per instance issue_queue, with minor
931 * twiddling done to the host specific fields of cmd. If the
932 * main coroutine is not running, it is restarted.
936 /* Only make static if a wrapper function is used */
937 #ifndef NCR5380_queue_command
938 static
939 #endif
940 int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
942 SETUP_HOSTDATA(cmd->host);
943 Scsi_Cmnd *tmp;
944 int oldto;
945 unsigned long flags;
946 extern int update_timeout(Scsi_Cmnd * SCset, int timeout);
948 #if (NDEBUG & NDEBUG_NO_WRITE)
949 switch (cmd->cmnd[0]) {
950 case WRITE_6:
951 case WRITE_10:
952 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
953 H_NO(cmd));
954 cmd->result = (DID_ERROR << 16);
955 done(cmd);
956 return 0;
958 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
961 #ifdef NCR5380_STATS
962 # if 0
963 if (!hostdata->connected && !hostdata->issue_queue &&
964 !hostdata->disconnected_queue) {
965 hostdata->timebase = jiffies;
967 # endif
968 # ifdef NCR5380_STAT_LIMIT
969 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
970 # endif
971 switch (cmd->cmnd[0])
973 case WRITE:
974 case WRITE_6:
975 case WRITE_10:
976 hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
977 hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
978 hostdata->pendingw++;
979 break;
980 case READ:
981 case READ_6:
982 case READ_10:
983 hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
984 hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
985 hostdata->pendingr++;
986 break;
988 #endif
991 * We use the host_scribble field as a pointer to the next command
992 * in a queue
995 NEXT(cmd) = NULL;
996 cmd->scsi_done = done;
998 cmd->result = 0;
1002 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1003 * commands are added to the head of the queue since any command will
1004 * clear the contingent allegiance condition that exists and the
1005 * sense data is only guaranteed to be valid while the condition exists.
1008 save_flags(flags);
1009 cli();
1011 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
1012 LIST(cmd, hostdata->issue_queue);
1013 NEXT(cmd) = hostdata->issue_queue;
1014 hostdata->issue_queue = cmd;
1015 } else {
1016 for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
1017 NEXT(tmp); tmp = NEXT(tmp))
1019 LIST(cmd, tmp);
1020 NEXT(tmp) = cmd;
1022 restore_flags(flags);
1024 QU_PRINTK("scsi%d: command added to %s of queue\n", H_NO(cmd),
1025 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1027 /* If queue_command() is called from an interrupt (real one or bottom
1028 * half), we let queue_main() do the job of taking care about main. If it
1029 * is already running, this is a no-op, else main will be queued.
1031 * If we're not in an interrupt, we can call NCR5380_main()
1032 * unconditionally, because it cannot be already running.
1034 if (in_interrupt() > 0 || ((flags >> 8) & 7) >= 6)
1035 queue_main();
1036 else
1037 NCR5380_main();
1038 return 0;
1042 * Function : NCR5380_main (void)
1044 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1045 * be done on the NCR5380 host adapters in a system. Both
1046 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1047 * in case it is not running.
1049 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1050 * reenable them. This prevents reentrancy and kernel stack overflow.
1053 static void NCR5380_main (void)
1055 Scsi_Cmnd *tmp, *prev;
1056 struct Scsi_Host *instance = first_instance;
1057 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
1058 int done;
1059 unsigned long flags;
1062 * We run (with interrupts disabled) until we're sure that none of
1063 * the host adapters have anything that can be done, at which point
1064 * we set main_running to 0 and exit.
1066 * Interrupts are enabled before doing various other internal
1067 * instructions, after we've decided that we need to run through
1068 * the loop again.
1070 * this should prevent any race conditions.
1072 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1073 * because also a timer int can trigger an abort or reset, which can
1074 * alter queues and touch the Falcon lock.
1077 /* Tell int handlers main() is now already executing. Note that
1078 no races are possible here. If an int comes in before
1079 'main_running' is set here, and queues/executes main via the
1080 task queue, it doesn't do any harm, just this instance of main
1081 won't find any work left to do. */
1082 if (main_running)
1083 return;
1084 main_running = 1;
1086 save_flags(flags);
1087 do {
1088 cli(); /* Freeze request queues */
1089 done = 1;
1091 if (!hostdata->connected) {
1092 MAIN_PRINTK( "scsi%d: not connected\n", HOSTNO );
1094 * Search through the issue_queue for a command destined
1095 * for a target that's not busy.
1097 #if (NDEBUG & NDEBUG_LISTS)
1098 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
1099 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1101 /*printk("%p ", tmp);*/
1102 if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1103 #endif
1104 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1105 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp) ) {
1107 #if (NDEBUG & NDEBUG_LISTS)
1108 if (prev != tmp)
1109 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n",
1110 tmp, tmp->target, hostdata->busy[tmp->target],
1111 tmp->lun);
1112 #endif
1113 /* When we find one, remove it from the issue queue. */
1114 if (
1115 #ifdef SUPPORT_TAGS
1116 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
1117 #else
1118 !(hostdata->busy[tmp->target] & (1 << tmp->lun))
1119 #endif
1121 cli(); /* ++guenther: just to be sure, this must be atomic */
1122 if (prev) {
1123 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
1124 NEXT(prev) = NEXT(tmp);
1125 } else {
1126 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1127 hostdata->issue_queue = NEXT(tmp);
1129 NEXT(tmp) = NULL;
1131 /* reenable interrupts after finding one */
1132 restore_flags(flags);
1135 * Attempt to establish an I_T_L nexus here.
1136 * On success, instance->hostdata->connected is set.
1137 * On failure, we must add the command back to the
1138 * issue queue so we can keep trying.
1140 MAIN_PRINTK("scsi%d: main(): command for target %d "
1141 "lun %d removed from issue_queue\n",
1142 HOSTNO, tmp->target, tmp->lun);
1144 * REQUEST SENSE commands are issued without tagged
1145 * queueing, even on SCSI-II devices because the
1146 * contingent allegiance condition exists for the
1147 * entire unit.
1149 /* ++roman: ...and the standard also requires that
1150 * REQUEST SENSE command are untagged.
1153 #ifdef SUPPORT_TAGS
1154 cmd_get_tag( tmp, tmp->cmnd[0] != REQUEST_SENSE );
1155 #endif
1156 if (!NCR5380_select(instance, tmp,
1157 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1158 TAG_NEXT)) {
1159 break;
1160 } else {
1161 cli();
1162 LIST(tmp, hostdata->issue_queue);
1163 NEXT(tmp) = hostdata->issue_queue;
1164 hostdata->issue_queue = tmp;
1165 #ifdef SUPPORT_TAGS
1166 cmd_free_tag( tmp );
1167 #endif
1168 restore_flags(flags);
1169 MAIN_PRINTK("scsi%d: main(): select() failed, "
1170 "returned to issue_queue\n", HOSTNO);
1171 if (hostdata->connected)
1172 break;
1174 } /* if target/lun/target queue is not busy */
1175 } /* for issue_queue */
1176 } /* if (!hostdata->connected) */
1178 if (hostdata->connected
1179 #ifdef REAL_DMA
1180 && !hostdata->dma_len
1181 #endif
1183 restore_flags(flags);
1184 MAIN_PRINTK("scsi%d: main: performing information transfer\n",
1185 HOSTNO);
1186 NCR5380_information_transfer(instance);
1187 MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO);
1188 done = 0;
1190 } while (!done);
1192 /* Better allow ints _after_ 'main_running' has been cleared, else
1193 an interrupt could believe we'll pick up the work it left for
1194 us, but we won't see it anymore here... */
1195 main_running = 0;
1196 restore_flags(flags);
1200 #ifdef REAL_DMA
1202 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1204 * Purpose : Called by interrupt handler when DMA finishes or a phase
1205 * mismatch occurs (which would finish the DMA transfer).
1207 * Inputs : instance - this instance of the NCR5380.
1211 static void NCR5380_dma_complete( struct Scsi_Host *instance )
1213 SETUP_HOSTDATA(instance);
1214 int transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1215 unsigned char **data, p;
1216 volatile int *count;
1218 if (!hostdata->connected) {
1219 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1220 "no connected cmd\n", HOSTNO);
1221 return;
1224 if (mac_read_overruns) {
1225 p = hostdata->connected->SCp.phase;
1226 if (p & SR_IO) {
1227 udelay(10);
1228 if ((((NCR5380_read(BUS_AND_STATUS_REG)) &
1229 (BASR_PHASE_MATCH|BASR_ACK)) ==
1230 (BASR_PHASE_MATCH|BASR_ACK))) {
1231 saved_data = NCR5380_read(INPUT_DATA_REG);
1232 overrun = 1;
1233 DMA_PRINTK("scsi%d: read overrun handled\n", HOSTNO);
1238 DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1239 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1240 NCR5380_read(STATUS_REG));
1242 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1243 NCR5380_write(MODE_REG, MR_BASE);
1244 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1246 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1247 hostdata->dma_len = 0;
1249 data = (unsigned char **) &(hostdata->connected->SCp.ptr);
1250 count = &(hostdata->connected->SCp.this_residual);
1251 *data += transfered;
1252 *count -= transfered;
1254 if (mac_read_overruns) {
1255 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1256 cnt = toPIO = mac_read_overruns;
1257 if (overrun) {
1258 DMA_PRINTK("Got an input overrun, using saved byte\n");
1259 *(*data)++ = saved_data;
1260 (*count)--;
1261 cnt--;
1262 toPIO--;
1264 DMA_PRINTK("Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
1265 NCR5380_transfer_pio(instance, &p, &cnt, data);
1266 *count -= toPIO - cnt;
1270 #endif /* REAL_DMA */
1274 * Function : void NCR5380_intr (int irq)
1276 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1277 * from the disconnected queue, and restarting NCR5380_main()
1278 * as required.
1280 * Inputs : int irq, irq that caused this interrupt.
1284 static void NCR5380_intr (int irq, void *dev_id, struct pt_regs *regs)
1286 struct Scsi_Host *instance = first_instance;
1287 int done = 1;
1288 unsigned char basr;
1290 INT_PRINTK("scsi%d: NCR5380 irq triggered\n", HOSTNO);
1292 /* Look for pending interrupts */
1293 basr = NCR5380_read(BUS_AND_STATUS_REG);
1294 INT_PRINTK("scsi%d: BASR=%02x\n", HOSTNO, basr);
1295 /* dispatch to appropriate routine if found and done=0 */
1296 if (basr & BASR_IRQ) {
1297 NCR_PRINT(NDEBUG_INTR);
1298 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1299 done = 0;
1300 ENABLE_IRQ();
1301 INT_PRINTK("scsi%d: SEL interrupt\n", HOSTNO);
1302 NCR5380_reselect(instance);
1303 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1305 else if (basr & BASR_PARITY_ERROR) {
1306 INT_PRINTK("scsi%d: PARITY interrupt\n", HOSTNO);
1307 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1309 else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1310 INT_PRINTK("scsi%d: RESET interrupt\n", HOSTNO);
1311 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1313 else {
1315 * The rest of the interrupt conditions can occur only during a
1316 * DMA transfer
1319 #if defined(REAL_DMA)
1321 * We should only get PHASE MISMATCH and EOP interrupts if we have
1322 * DMA enabled, so do a sanity check based on the current setting
1323 * of the MODE register.
1326 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1327 ((basr & BASR_END_DMA_TRANSFER) ||
1328 !(basr & BASR_PHASE_MATCH))) {
1330 INT_PRINTK("scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
1331 NCR5380_dma_complete( instance );
1332 done = 0;
1333 ENABLE_IRQ();
1334 } else
1335 #endif /* REAL_DMA */
1337 /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1338 if (basr & BASR_PHASE_MATCH)
1339 printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1340 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1341 HOSTNO, basr, NCR5380_read(MODE_REG),
1342 NCR5380_read(STATUS_REG));
1343 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1345 } /* if !(SELECTION || PARITY) */
1346 } /* BASR & IRQ */
1347 else {
1348 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1349 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1350 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1351 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1354 if (!done) {
1355 INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO);
1356 /* Put a call to NCR5380_main() on the queue... */
1357 queue_main();
1361 #ifdef NCR5380_STATS
1362 static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
1364 # ifdef NCR5380_STAT_LIMIT
1365 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1366 # endif
1367 switch (cmd->cmnd[0])
1369 case WRITE:
1370 case WRITE_6:
1371 case WRITE_10:
1372 hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
1373 /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen;*/
1374 hostdata->pendingw--;
1375 break;
1376 case READ:
1377 case READ_6:
1378 case READ_10:
1379 hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
1380 /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen;*/
1381 hostdata->pendingr--;
1382 break;
1385 #endif
1388 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1389 * int tag);
1391 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1392 * including ARBITRATION, SELECTION, and initial message out for
1393 * IDENTIFY and queue messages.
1395 * Inputs : instance - instantiation of the 5380 driver on which this
1396 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1397 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1398 * the command that is presently connected.
1400 * Returns : -1 if selection could not execute for some reason,
1401 * 0 if selection succeeded or failed because the target
1402 * did not respond.
1404 * Side effects :
1405 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1406 * with registers as they should have been on entry - ie
1407 * SELECT_ENABLE will be set appropriately, the NCR5380
1408 * will cease to drive any SCSI bus signals.
1410 * If successful : I_T_L or I_T_L_Q nexus will be established,
1411 * instance->connected will be set to cmd.
1412 * SELECT interrupt will be disabled.
1414 * If failed (no target) : cmd->scsi_done() will be called, and the
1415 * cmd->result host byte set to DID_BAD_TARGET.
1418 static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
1420 SETUP_HOSTDATA(instance);
1421 unsigned char tmp[3], phase;
1422 unsigned char *data;
1423 int len;
1424 unsigned long timeout;
1425 unsigned long flags;
1427 hostdata->restart_select = 0;
1428 NCR_PRINT(NDEBUG_ARBITRATION);
1429 ARB_PRINTK("scsi%d: starting arbitration, id = %d\n", HOSTNO,
1430 instance->this_id);
1433 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1434 * data bus during SELECTION.
1437 save_flags(flags);
1438 cli();
1439 if (hostdata->connected) {
1440 restore_flags(flags);
1441 return -1;
1443 NCR5380_write(TARGET_COMMAND_REG, 0);
1447 * Start arbitration.
1450 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1451 NCR5380_write(MODE_REG, MR_ARBITRATE);
1453 restore_flags(flags);
1455 /* Wait for arbitration logic to complete */
1456 #if NCR_TIMEOUT
1458 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1460 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1461 && time_before(jiffies, timeout) && !hostdata->connected)
1463 if (time_after_eq(jiffies, timeout))
1465 printk("scsi : arbitration timeout at %d\n", __LINE__);
1466 NCR5380_write(MODE_REG, MR_BASE);
1467 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1468 return -1;
1471 #else /* NCR_TIMEOUT */
1472 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1473 && !hostdata->connected);
1474 #endif
1476 ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO);
1478 if (hostdata->connected) {
1479 NCR5380_write(MODE_REG, MR_BASE);
1480 return -1;
1483 * The arbitration delay is 2.2us, but this is a minimum and there is
1484 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1485 * the integral nature of udelay().
1489 udelay(3);
1491 /* Check for lost arbitration */
1492 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1493 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1494 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1495 hostdata->connected) {
1496 NCR5380_write(MODE_REG, MR_BASE);
1497 ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1498 HOSTNO);
1499 return -1;
1502 /* after/during arbitration, BSY should be asserted.
1503 IBM DPES-31080 Version S31Q works now */
1504 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1505 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL |
1506 ICR_ASSERT_BSY ) ;
1508 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1509 hostdata->connected) {
1510 NCR5380_write(MODE_REG, MR_BASE);
1511 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1512 ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1513 HOSTNO);
1514 return -1;
1518 * Again, bus clear + bus settle time is 1.2us, however, this is
1519 * a minimum so we'll udelay ceil(1.2)
1522 #ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
1523 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1524 udelay(15);
1525 #else
1526 udelay(2);
1527 #endif
1529 if (hostdata->connected) {
1530 NCR5380_write(MODE_REG, MR_BASE);
1531 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1532 return -1;
1535 ARB_PRINTK("scsi%d: won arbitration\n", HOSTNO);
1538 * Now that we have won arbitration, start Selection process, asserting
1539 * the host and target ID's on the SCSI bus.
1542 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
1545 * Raise ATN while SEL is true before BSY goes false from arbitration,
1546 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1547 * phase immediately after selection.
1550 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1551 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1552 NCR5380_write(MODE_REG, MR_BASE);
1555 * Reselect interrupts must be turned off prior to the dropping of BSY,
1556 * otherwise we will trigger an interrupt.
1559 if (hostdata->connected) {
1560 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1561 return -1;
1564 NCR5380_write(SELECT_ENABLE_REG, 0);
1567 * The initiator shall then wait at least two deskew delays and release
1568 * the BSY signal.
1570 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1572 /* Reset BSY */
1573 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1574 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1577 * Something weird happens when we cease to drive BSY - looks
1578 * like the board/chip is letting us do another read before the
1579 * appropriate propagation delay has expired, and we're confusing
1580 * a BSY signal from ourselves as the target's response to SELECTION.
1582 * A small delay (the 'C++' frontend breaks the pipeline with an
1583 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1584 * tighter 'C' code breaks and requires this) solves the problem -
1585 * the 1 us delay is arbitrary, and only used because this delay will
1586 * be the same on other platforms and since it works here, it should
1587 * work there.
1589 * wingel suggests that this could be due to failing to wait
1590 * one deskew delay.
1593 udelay(1);
1595 SEL_PRINTK("scsi%d: selecting target %d\n", HOSTNO, cmd->target);
1598 * The SCSI specification calls for a 250 ms timeout for the actual
1599 * selection.
1602 timeout = jiffies + 25;
1605 * XXX very interesting - we're seeing a bounce where the BSY we
1606 * asserted is being reflected / still asserted (propagation delay?)
1607 * and it's detecting as true. Sigh.
1610 #if 0
1611 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1612 * IO while SEL is true. But again, there are some disks out the in the
1613 * world that do that nevertheless. (Somebody claimed that this announces
1614 * reselection capability of the target.) So we better skip that test and
1615 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1618 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) &
1619 (SR_BSY | SR_IO)));
1621 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1622 (SR_SEL | SR_IO)) {
1623 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1624 NCR5380_reselect(instance);
1625 printk (KERN_ERR "scsi%d: reselection after won arbitration?\n",
1626 HOSTNO);
1627 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1628 return -1;
1630 #else
1631 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
1632 #endif
1635 * No less than two deskew delays after the initiator detects the
1636 * BSY signal is true, it shall release the SEL signal and may
1637 * change the DATA BUS. -wingel
1640 udelay(1);
1642 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1644 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1645 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1646 if (hostdata->targets_present & (1 << cmd->target)) {
1647 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1648 if (hostdata->restart_select)
1649 printk(KERN_NOTICE "\trestart select\n");
1650 NCR_PRINT(NDEBUG_ANY);
1651 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1652 return -1;
1654 cmd->result = DID_BAD_TARGET << 16;
1655 #ifdef NCR5380_STATS
1656 collect_stats(hostdata, cmd);
1657 #endif
1658 #ifdef SUPPORT_TAGS
1659 cmd_free_tag( cmd );
1660 #endif
1661 cmd->scsi_done(cmd);
1662 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1663 SEL_PRINTK("scsi%d: target did not respond within 250ms\n", HOSTNO);
1664 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1665 return 0;
1668 hostdata->targets_present |= (1 << cmd->target);
1671 * Since we followed the SCSI spec, and raised ATN while SEL
1672 * was true but before BSY was false during selection, the information
1673 * transfer phase should be a MESSAGE OUT phase so that we can send the
1674 * IDENTIFY message.
1676 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1677 * message (2 bytes) with a tag ID that we increment with every command
1678 * until it wraps back to 0.
1680 * XXX - it turns out that there are some broken SCSI-II devices,
1681 * which claim to support tagged queuing but fail when more than
1682 * some number of commands are issued at once.
1685 /* Wait for start of REQ/ACK handshake */
1686 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1688 SEL_PRINTK("scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
1689 HOSTNO, cmd->target);
1690 tmp[0] = IDENTIFY(1, cmd->lun);
1692 #ifdef SUPPORT_TAGS
1693 if (cmd->tag != TAG_NONE) {
1694 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1695 tmp[2] = cmd->tag;
1696 len = 3;
1697 } else
1698 len = 1;
1699 #else
1700 len = 1;
1701 cmd->tag=0;
1702 #endif /* SUPPORT_TAGS */
1704 /* Send message(s) */
1705 data = tmp;
1706 phase = PHASE_MSGOUT;
1707 NCR5380_transfer_pio(instance, &phase, &len, &data);
1708 SEL_PRINTK("scsi%d: nexus established.\n", HOSTNO);
1709 /* XXX need to handle errors here */
1710 hostdata->connected = cmd;
1711 #ifndef SUPPORT_TAGS
1712 hostdata->busy[cmd->target] |= (1 << cmd->lun);
1713 #endif
1715 initialize_SCp(cmd);
1718 return 0;
1722 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1723 * unsigned char *phase, int *count, unsigned char **data)
1725 * Purpose : transfers data in given phase using polled I/O
1727 * Inputs : instance - instance of driver, *phase - pointer to
1728 * what phase is expected, *count - pointer to number of
1729 * bytes to transfer, **data - pointer to data pointer.
1731 * Returns : -1 when different phase is entered without transferring
1732 * maximum number of bytes, 0 if all bytes are transfered or exit
1733 * is in same phase.
1735 * Also, *phase, *count, *data are modified in place.
1737 * XXX Note : handling for bus free may be useful.
1741 * Note : this code is not as quick as it could be, however it
1742 * IS 100% reliable, and for the actual data transfer where speed
1743 * counts, we will always do a pseudo DMA or DMA transfer.
1746 static int NCR5380_transfer_pio( struct Scsi_Host *instance,
1747 unsigned char *phase, int *count,
1748 unsigned char **data)
1750 register unsigned char p = *phase, tmp;
1751 register int c = *count;
1752 register unsigned char *d = *data;
1755 * The NCR5380 chip will only drive the SCSI bus when the
1756 * phase specified in the appropriate bits of the TARGET COMMAND
1757 * REGISTER match the STATUS REGISTER
1760 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1762 do {
1764 * Wait for assertion of REQ, after which the phase bits will be
1765 * valid
1767 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1769 HSH_PRINTK("scsi%d: REQ detected\n", HOSTNO);
1771 /* Check for phase mismatch */
1772 if ((tmp & PHASE_MASK) != p) {
1773 PIO_PRINTK("scsi%d: phase mismatch\n", HOSTNO);
1774 NCR_PRINT_PHASE(NDEBUG_PIO);
1775 break;
1778 /* Do actual transfer from SCSI bus to / from memory */
1779 if (!(p & SR_IO))
1780 NCR5380_write(OUTPUT_DATA_REG, *d);
1781 else
1782 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1784 ++d;
1787 * The SCSI standard suggests that in MSGOUT phase, the initiator
1788 * should drop ATN on the last byte of the message phase
1789 * after REQ has been asserted for the handshake but before
1790 * the initiator raises ACK.
1793 if (!(p & SR_IO)) {
1794 if (!((p & SR_MSG) && c > 1)) {
1795 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1796 ICR_ASSERT_DATA);
1797 NCR_PRINT(NDEBUG_PIO);
1798 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1799 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1800 } else {
1801 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1802 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1803 NCR_PRINT(NDEBUG_PIO);
1804 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1805 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1807 } else {
1808 NCR_PRINT(NDEBUG_PIO);
1809 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1812 while (NCR5380_read(STATUS_REG) & SR_REQ);
1814 HSH_PRINTK("scsi%d: req false, handshake complete\n", HOSTNO);
1817 * We have several special cases to consider during REQ/ACK handshaking :
1818 * 1. We were in MSGOUT phase, and we are on the last byte of the
1819 * message. ATN must be dropped as ACK is dropped.
1821 * 2. We are in a MSGIN phase, and we are on the last byte of the
1822 * message. We must exit with ACK asserted, so that the calling
1823 * code may raise ATN before dropping ACK to reject the message.
1825 * 3. ACK and ATN are clear and the target may proceed as normal.
1827 if (!(p == PHASE_MSGIN && c == 1)) {
1828 if (p == PHASE_MSGOUT && c > 1)
1829 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1830 else
1831 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1833 } while (--c);
1835 PIO_PRINTK("scsi%d: residual %d\n", HOSTNO, c);
1837 *count = c;
1838 *data = d;
1839 tmp = NCR5380_read(STATUS_REG);
1840 /* The phase read from the bus is valid if either REQ is (already)
1841 * asserted or if ACK hasn't been released yet. The latter is the case if
1842 * we're in MSGIN and all wanted bytes have been received. */
1843 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1844 *phase = tmp & PHASE_MASK;
1845 else
1846 *phase = PHASE_UNKNOWN;
1848 if (!c || (*phase == p))
1849 return 0;
1850 else
1851 return -1;
1855 * Function : do_abort (Scsi_Host *host)
1857 * Purpose : abort the currently established nexus. Should only be
1858 * called from a routine which can drop into a
1860 * Returns : 0 on success, -1 on failure.
1863 static int do_abort (struct Scsi_Host *host)
1865 unsigned char tmp, *msgptr, phase;
1866 int len;
1868 /* Request message out phase */
1869 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1872 * Wait for the target to indicate a valid phase by asserting
1873 * REQ. Once this happens, we'll have either a MSGOUT phase
1874 * and can immediately send the ABORT message, or we'll have some
1875 * other phase and will have to source/sink data.
1877 * We really don't care what value was on the bus or what value
1878 * the target sees, so we just handshake.
1881 while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1883 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1885 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1886 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1887 ICR_ASSERT_ACK);
1888 while (NCR5380_read(STATUS_REG) & SR_REQ);
1889 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1892 tmp = ABORT;
1893 msgptr = &tmp;
1894 len = 1;
1895 phase = PHASE_MSGOUT;
1896 NCR5380_transfer_pio (host, &phase, &len, &msgptr);
1899 * If we got here, and the command completed successfully,
1900 * we're about to go into bus free state.
1903 return len ? -1 : 0;
1906 #if defined(REAL_DMA) || defined(PSEUDO_DMA)
1908 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1909 * unsigned char *phase, int *count, unsigned char **data)
1911 * Purpose : transfers data in given phase using either real
1912 * or pseudo DMA.
1914 * Inputs : instance - instance of driver, *phase - pointer to
1915 * what phase is expected, *count - pointer to number of
1916 * bytes to transfer, **data - pointer to data pointer.
1918 * Returns : -1 when different phase is entered without transferring
1919 * maximum number of bytes, 0 if all bytes or transfered or exit
1920 * is in same phase.
1922 * Also, *phase, *count, *data are modified in place.
1927 static int NCR5380_transfer_dma( struct Scsi_Host *instance,
1928 unsigned char *phase, int *count,
1929 unsigned char **data)
1931 SETUP_HOSTDATA(instance);
1932 register int c = *count;
1933 register unsigned char p = *phase;
1934 register unsigned char *d = *data;
1935 register int foo;
1936 unsigned char tmp;
1937 unsigned long flags;
1940 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1941 *phase = tmp;
1942 return -1;
1945 if (mac_read_overruns && (p & SR_IO)) {
1946 c -= mac_read_overruns;
1949 DMA_PRINTK("scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1950 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1951 c, (p & SR_IO) ? "to" : "from", d);
1953 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1955 #ifdef REAL_DMA
1956 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1957 #else /* PSEUDO_DMA! */
1958 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
1959 save_flags(flags);
1960 cli();
1961 #endif
1962 /* KLL May need eop and parity in 53c400 */
1963 if (hostdata->flags & FLAG_NCR53C400)
1964 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_PAR_CHECK
1965 | MR_ENABLE_PAR_INTR | MR_ENABLE_EOP_INTR | MR_DMA_MODE
1966 | MR_MONITOR_BSY);
1967 else
1968 #ifndef EMULATE_PSEUDO_DMA
1969 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
1970 #else
1971 NCR5380_write(MODE_REG, MR_BASE);
1972 #endif
1973 #endif /* def REAL_DMA */
1975 #ifdef REAL_DMA
1976 /* On the Medusa, it is a must to initialize the DMA before
1977 * starting the NCR. This is also the cleaner way for the TT.
1979 save_flags(flags);
1980 cli();
1981 hostdata->dma_len = (p & SR_IO) ?
1982 NCR5380_dma_read_setup(instance, d, c) :
1983 NCR5380_dma_write_setup(instance, d, c);
1984 restore_flags(flags);
1985 #endif /* def REAL_DMA */
1987 #ifndef EMULATE_PSEUDO_DMA
1988 if (p & SR_IO)
1989 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1990 else {
1991 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1992 NCR5380_write(START_DMA_SEND_REG, 0);
1994 #else
1995 hostdata->dma_len = c;
1996 #endif
1998 #if defined(REAL_DMA)
1999 return 0;
2000 #else /* defined(PSEUDO_DMA) */
2001 if (p & SR_IO) {
2002 #ifdef DMA_WORKS_RIGHT
2003 foo = NCR5380_pread(instance, d, c);
2004 #else
2005 int diff = 1;
2006 if (hostdata->flags & FLAG_NCR53C400) {
2007 diff=0;
2010 if (!(foo = NCR5380_pread(instance, d, c - diff))) {
2012 * We can't disable DMA mode after successfully transferring
2013 * what we plan to be the last byte, since that would open up
2014 * a race condition where if the target asserted REQ before
2015 * we got the DMA mode reset, the NCR5380 would have latched
2016 * an additional byte into the INPUT DATA register and we'd
2017 * have dropped it.
2019 * The workaround was to transfer one fewer bytes than we
2020 * intended to with the pseudo-DMA read function, wait for
2021 * the chip to latch the last byte, read it, and then disable
2022 * pseudo-DMA mode.
2024 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
2025 * REQ is deasserted when ACK is asserted, and not reasserted
2026 * until ACK goes false. Since the NCR5380 won't lower ACK
2027 * until DACK is asserted, which won't happen unless we twiddle
2028 * the DMA port or we take the NCR5380 out of DMA mode, we
2029 * can guarantee that we won't handshake another extra
2030 * byte.
2033 if (!(hostdata->flags & FLAG_NCR53C400)) {
2034 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
2035 /* Wait for clean handshake */
2036 while (NCR5380_read(STATUS_REG) & SR_REQ);
2037 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
2040 #endif
2041 } else {
2042 #ifdef DMA_WORKS_RIGHT
2043 foo = NCR5380_pwrite(instance, d, c);
2044 #else
2045 int timeout;
2046 #if (NDEBUG & NDEBUG_C400_PWRITE)
2047 printk("About to pwrite %d bytes\n", c);
2048 #endif
2049 if (!(foo = NCR5380_pwrite(instance, d, c))) {
2051 * Wait for the last byte to be sent. If REQ is being asserted for
2052 * the byte we're interested, we'll ACK it and it will go false.
2054 if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
2055 timeout = 20000;
2056 #if 1
2057 #if 1
2058 while (!(NCR5380_read(BUS_AND_STATUS_REG) &
2059 BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
2060 BASR_PHASE_MATCH));
2061 #else
2062 if (NCR5380_read(STATUS_REG) & SR_REQ) {
2063 for (; timeout &&
2064 !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK);
2065 --timeout);
2066 for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
2067 --timeout);
2069 #endif
2072 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2073 if (!timeout)
2074 printk("scsi%d : timed out on last byte\n",
2075 instance->host_no);
2076 #endif
2079 if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
2080 hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
2081 if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
2082 hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
2083 #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
2084 printk("scsi%d : last bit sent works\n",
2085 instance->host_no);
2086 #endif
2089 } else {
2090 #if (NDEBUG & NDEBUG_C400_PWRITE)
2091 printk("Waiting for LASTBYTE\n");
2092 #endif
2093 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
2094 #if (NDEBUG & NDEBUG_C400_PWRITE)
2095 printk("Got LASTBYTE\n");
2096 #endif
2098 #else
2099 udelay (5);
2100 #endif
2102 #endif
2105 NCR5380_write(MODE_REG, MR_BASE);
2106 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2108 if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
2109 #if (NDEBUG & NDEBUG_C400_PWRITE)
2110 printk("53C400w: Checking for IRQ\n");
2111 #endif
2112 if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
2113 #if (NDEBUG & NDEBUG_C400_PWRITE)
2114 printk("53C400w: got it, reading reset interrupt reg\n");
2115 #endif
2116 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
2117 } else {
2118 printk("53C400w: IRQ NOT THERE!\n");
2122 *data = d + c;
2123 *count = 0;
2124 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
2125 #if 0
2126 NCR5380_print_phase(instance);
2127 #endif
2128 #if defined(PSEUDO_DMA) && !defined(UNSAFE)
2129 restore_flags(flags);
2130 #endif /* defined(REAL_DMA_POLL) */
2131 return foo;
2132 #endif /* def REAL_DMA */
2134 #endif /* defined(REAL_DMA) || defined(PSEUDO_DMA) */
2138 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
2140 * Purpose : run through the various SCSI phases and do as the target
2141 * directs us to. Operates on the currently connected command,
2142 * instance->connected.
2144 * Inputs : instance, instance for which we are doing commands
2146 * Side effects : SCSI things happen, the disconnected queue will be
2147 * modified if a command disconnects, *instance->connected will
2148 * change.
2150 * XXX Note : we need to watch for bus free or a reset condition here
2151 * to recover from an unexpected bus free condition.
2154 static void NCR5380_information_transfer (struct Scsi_Host *instance)
2156 SETUP_HOSTDATA(instance);
2157 unsigned long flags;
2158 unsigned char msgout = NOP;
2159 int sink = 0;
2160 int len;
2161 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
2162 int transfersize;
2163 #endif
2164 unsigned char *data;
2165 unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
2166 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2168 while (1) {
2169 tmp = NCR5380_read(STATUS_REG);
2170 /* We only have a valid SCSI phase when REQ is asserted */
2171 if (tmp & SR_REQ) {
2172 phase = (tmp & PHASE_MASK);
2173 if (phase != old_phase) {
2174 old_phase = phase;
2175 NCR_PRINT_PHASE(NDEBUG_INFORMATION);
2178 if (sink && (phase != PHASE_MSGOUT)) {
2179 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2181 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2182 ICR_ASSERT_ACK);
2183 while (NCR5380_read(STATUS_REG) & SR_REQ);
2184 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2185 ICR_ASSERT_ATN);
2186 sink = 0;
2187 continue;
2190 switch (phase) {
2191 case PHASE_DATAOUT:
2192 #if (NDEBUG & NDEBUG_NO_DATAOUT)
2193 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2194 "aborted\n", HOSTNO);
2195 sink = 1;
2196 do_abort(instance);
2197 cmd->result = DID_ERROR << 16;
2198 cmd->done(cmd);
2199 return;
2200 #endif
2201 case PHASE_DATAIN:
2203 * If there is no room left in the current buffer in the
2204 * scatter-gather list, move onto the next one.
2207 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2208 ++cmd->SCp.buffer;
2209 --cmd->SCp.buffers_residual;
2210 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2211 cmd->SCp.ptr = cmd->SCp.buffer->address;
2212 /* ++roman: Try to merge some scatter-buffers if
2213 * they are at contiguous physical addresses.
2215 merge_contiguous_buffers( cmd );
2216 INF_PRINTK("scsi%d: %d bytes and %d buffers left\n",
2217 HOSTNO, cmd->SCp.this_residual,
2218 cmd->SCp.buffers_residual);
2222 * The preferred transfer method is going to be
2223 * PSEUDO-DMA for systems that are strictly PIO,
2224 * since we can let the hardware do the handshaking.
2226 * For this to work, we need to know the transfersize
2227 * ahead of time, since the pseudo-DMA code will sit
2228 * in an unconditional loop.
2231 /* ++roman: I suggest, this should be
2232 * #if def(REAL_DMA)
2233 * instead of leaving REAL_DMA out.
2236 #if defined(REAL_DMA) || defined(PSEUDO_DMA)
2237 if (!cmd->device->borken &&
2238 !(hostdata->flags & FLAG_NO_PSEUDO_DMA) &&
2239 (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
2241 len = transfersize;
2242 cmd->SCp.phase = phase;
2243 if (NCR5380_transfer_dma(instance, &phase,
2244 &len, (unsigned char **) &cmd->SCp.ptr)) {
2246 * If the watchdog timer fires, all future
2247 * accesses to this device will use the
2248 * polled-IO. */
2249 printk(KERN_NOTICE "scsi%d: switching target %d "
2250 "lun %d to slow handshake\n", HOSTNO,
2251 cmd->target, cmd->lun);
2252 cmd->device->borken = 1;
2253 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2254 ICR_ASSERT_ATN);
2255 sink = 1;
2256 do_abort(instance);
2257 cmd->result = DID_ERROR << 16;
2258 cmd->done(cmd);
2259 /* XXX - need to source or sink data here, as appropriate */
2260 } else {
2261 #ifdef REAL_DMA
2262 /* ++roman: When using real DMA,
2263 * information_transfer() should return after
2264 * starting DMA since it has nothing more to
2265 * do.
2267 return;
2268 #else
2269 /* Michael: When using pseudo-DMA emulation, we must
2270 * take care to take into account the residual from
2271 * the current transfer as determined by either the
2272 * interrupt routine ot the pseudo-transfer functions
2273 * (whichever notices it first).
2275 if (mac_pdma_residual)
2276 len -= mac_pdma_residual;
2277 cmd->SCp.this_residual -= transfersize - len;
2278 #endif
2280 } else
2281 #endif /* defined(REAL_DMA) || defined(PSEUDO_DMA) */
2282 NCR5380_transfer_pio(instance, &phase,
2283 (int *) &cmd->SCp.this_residual, (unsigned char **)
2284 &cmd->SCp.ptr);
2285 break;
2286 case PHASE_MSGIN:
2287 len = 1;
2288 data = &tmp;
2289 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2290 NCR5380_transfer_pio(instance, &phase, &len, &data);
2291 cmd->SCp.Message = tmp;
2293 switch (tmp) {
2295 * Linking lets us reduce the time required to get the
2296 * next command out to the device, hopefully this will
2297 * mean we don't waste another revolution due to the delays
2298 * required by ARBITRATION and another SELECTION.
2300 * In the current implementation proposal, low level drivers
2301 * merely have to start the next command, pointed to by
2302 * next_link, done() is called as with unlinked commands.
2304 #ifdef LINKED
2305 case LINKED_CMD_COMPLETE:
2306 case LINKED_FLG_CMD_COMPLETE:
2307 /* Accept message by clearing ACK */
2308 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2310 LNK_PRINTK("scsi%d: target %d lun %d linked command "
2311 "complete.\n", HOSTNO, cmd->target, cmd->lun);
2313 /* Enable reselect interrupts */
2314 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2316 * Sanity check : A linked command should only terminate
2317 * with one of these messages if there are more linked
2318 * commands available.
2321 if (!cmd->next_link) {
2322 printk(KERN_NOTICE "scsi%d: target %d lun %d "
2323 "linked command complete, no next_link\n",
2324 HOSTNO, cmd->target, cmd->lun);
2325 sink = 1;
2326 do_abort (instance);
2327 return;
2330 initialize_SCp(cmd->next_link);
2331 /* The next command is still part of this process; copy it
2332 * and don't free it! */
2333 cmd->next_link->tag = cmd->tag;
2334 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2335 LNK_PRINTK("scsi%d: target %d lun %d linked request "
2336 "done, calling scsi_done().\n",
2337 HOSTNO, cmd->target, cmd->lun);
2338 #ifdef NCR5380_STATS
2339 collect_stats(hostdata, cmd);
2340 #endif
2341 cmd->scsi_done(cmd);
2342 cmd = hostdata->connected;
2343 break;
2344 #endif /* def LINKED */
2345 case ABORT:
2346 case COMMAND_COMPLETE:
2347 /* Accept message by clearing ACK */
2348 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2349 hostdata->connected = NULL;
2350 QU_PRINTK("scsi%d: command for target %d, lun %d "
2351 "completed\n", HOSTNO, cmd->target, cmd->lun);
2352 #ifdef SUPPORT_TAGS
2353 cmd_free_tag( cmd );
2354 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2355 /* Turn a QUEUE FULL status into BUSY, I think the
2356 * mid level cannot handle QUEUE FULL :-( (The
2357 * command is retried after BUSY). Also update our
2358 * queue size to the number of currently issued
2359 * commands now.
2361 /* ++Andreas: the mid level code knows about
2362 QUEUE_FULL now. */
2363 TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
2364 TAG_PRINTK("scsi%d: target %d lun %d returned "
2365 "QUEUE_FULL after %d commands\n",
2366 HOSTNO, cmd->target, cmd->lun,
2367 ta->nr_allocated);
2368 if (ta->queue_size > ta->nr_allocated)
2369 ta->nr_allocated = ta->queue_size;
2371 #else
2372 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2373 #endif
2374 /* Enable reselect interrupts */
2375 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2378 * I'm not sure what the correct thing to do here is :
2380 * If the command that just executed is NOT a request
2381 * sense, the obvious thing to do is to set the result
2382 * code to the values of the stored parameters.
2384 * If it was a REQUEST SENSE command, we need some way to
2385 * differentiate between the failure code of the original
2386 * and the failure code of the REQUEST sense - the obvious
2387 * case is success, where we fall through and leave the
2388 * result code unchanged.
2390 * The non-obvious place is where the REQUEST SENSE failed
2393 if (cmd->cmnd[0] != REQUEST_SENSE)
2394 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2395 else if (status_byte(cmd->SCp.Status) != GOOD)
2396 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2398 #ifdef AUTOSENSE
2399 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2400 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
2401 ASEN_PRINTK("scsi%d: performing request sense\n",
2402 HOSTNO);
2403 cmd->cmnd[0] = REQUEST_SENSE;
2404 cmd->cmnd[1] &= 0xe0;
2405 cmd->cmnd[2] = 0;
2406 cmd->cmnd[3] = 0;
2407 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2408 cmd->cmnd[5] = 0;
2409 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
2411 cmd->use_sg = 0;
2412 /* this is initialized from initialize_SCp
2413 cmd->SCp.buffer = NULL;
2414 cmd->SCp.buffers_residual = 0;
2416 cmd->request_buffer = (char *) cmd->sense_buffer;
2417 cmd->request_bufflen = sizeof(cmd->sense_buffer);
2419 save_flags(flags);
2420 cli();
2421 LIST(cmd,hostdata->issue_queue);
2422 NEXT(cmd) = hostdata->issue_queue;
2423 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2424 restore_flags(flags);
2425 QU_PRINTK("scsi%d: REQUEST SENSE added to head of "
2426 "issue queue\n", H_NO(cmd));
2427 } else
2428 #endif /* def AUTOSENSE */
2430 #ifdef NCR5380_STATS
2431 collect_stats(hostdata, cmd);
2432 #endif
2433 cmd->scsi_done(cmd);
2436 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2438 * Restore phase bits to 0 so an interrupted selection,
2439 * arbitration can resume.
2441 NCR5380_write(TARGET_COMMAND_REG, 0);
2443 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2444 barrier();
2446 return;
2447 case MESSAGE_REJECT:
2448 /* Accept message by clearing ACK */
2449 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2450 /* Enable reselect interrupts */
2451 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2452 switch (hostdata->last_message) {
2453 case HEAD_OF_QUEUE_TAG:
2454 case ORDERED_QUEUE_TAG:
2455 case SIMPLE_QUEUE_TAG:
2456 /* The target obviously doesn't support tagged
2457 * queuing, even though it announced this ability in
2458 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2459 * clear 'tagged_supported' and lock the LUN, since
2460 * the command is treated as untagged further on.
2462 cmd->device->tagged_supported = 0;
2463 hostdata->busy[cmd->target] |= (1 << cmd->lun);
2464 cmd->tag = TAG_NONE;
2465 TAG_PRINTK("scsi%d: target %d lun %d rejected "
2466 "QUEUE_TAG message; tagged queuing "
2467 "disabled\n",
2468 HOSTNO, cmd->target, cmd->lun);
2469 break;
2471 break;
2472 case DISCONNECT:
2473 /* Accept message by clearing ACK */
2474 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2475 save_flags(flags);
2476 cli();
2477 cmd->device->disconnect = 1;
2478 LIST(cmd,hostdata->disconnected_queue);
2479 NEXT(cmd) = hostdata->disconnected_queue;
2480 hostdata->connected = NULL;
2481 hostdata->disconnected_queue = cmd;
2482 restore_flags(flags);
2483 QU_PRINTK("scsi%d: command for target %d lun %d was "
2484 "moved from connected to the "
2485 "disconnected_queue\n", HOSTNO,
2486 cmd->target, cmd->lun);
2488 * Restore phase bits to 0 so an interrupted selection,
2489 * arbitration can resume.
2491 NCR5380_write(TARGET_COMMAND_REG, 0);
2493 /* Enable reselect interrupts */
2494 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2495 /* Wait for bus free to avoid nasty timeouts */
2496 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2497 barrier();
2498 return;
2500 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2501 * operation, in violation of the SCSI spec so we can safely
2502 * ignore SAVE/RESTORE pointers calls.
2504 * Unfortunately, some disks violate the SCSI spec and
2505 * don't issue the required SAVE_POINTERS message before
2506 * disconnecting, and we have to break spec to remain
2507 * compatible.
2509 case SAVE_POINTERS:
2510 case RESTORE_POINTERS:
2511 /* Accept message by clearing ACK */
2512 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2513 /* Enable reselect interrupts */
2514 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2515 break;
2516 case EXTENDED_MESSAGE:
2518 * Extended messages are sent in the following format :
2519 * Byte
2520 * 0 EXTENDED_MESSAGE == 1
2521 * 1 length (includes one byte for code, doesn't
2522 * include first two bytes)
2523 * 2 code
2524 * 3..length+1 arguments
2526 * Start the extended message buffer with the EXTENDED_MESSAGE
2527 * byte, since print_msg() wants the whole thing.
2529 extended_msg[0] = EXTENDED_MESSAGE;
2530 /* Accept first byte by clearing ACK */
2531 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2533 EXT_PRINTK("scsi%d: receiving extended message\n", HOSTNO);
2535 len = 2;
2536 data = extended_msg + 1;
2537 phase = PHASE_MSGIN;
2538 NCR5380_transfer_pio(instance, &phase, &len, &data);
2539 EXT_PRINTK("scsi%d: length=%d, code=0x%02x\n", HOSTNO,
2540 (int)extended_msg[1], (int)extended_msg[2]);
2542 if (!len && extended_msg[1] <=
2543 (sizeof (extended_msg) - 1)) {
2544 /* Accept third byte by clearing ACK */
2545 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2546 len = extended_msg[1] - 1;
2547 data = extended_msg + 3;
2548 phase = PHASE_MSGIN;
2550 NCR5380_transfer_pio(instance, &phase, &len, &data);
2551 EXT_PRINTK("scsi%d: message received, residual %d\n",
2552 HOSTNO, len);
2554 switch (extended_msg[2]) {
2555 case EXTENDED_SDTR:
2556 case EXTENDED_WDTR:
2557 case EXTENDED_MODIFY_DATA_POINTER:
2558 case EXTENDED_EXTENDED_IDENTIFY:
2559 tmp = 0;
2561 } else if (len) {
2562 printk(KERN_NOTICE "scsi%d: error receiving "
2563 "extended message\n", HOSTNO);
2564 tmp = 0;
2565 } else {
2566 printk(KERN_NOTICE "scsi%d: extended message "
2567 "code %02x length %d is too long\n",
2568 HOSTNO, extended_msg[2], extended_msg[1]);
2569 tmp = 0;
2571 /* Fall through to reject message */
2574 * If we get something weird that we aren't expecting,
2575 * reject it.
2577 default:
2578 if (!tmp) {
2579 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2580 print_msg (extended_msg);
2581 printk("\n");
2582 } else if (tmp != EXTENDED_MESSAGE)
2583 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2584 "message %02x from target %d, lun %d\n",
2585 HOSTNO, tmp, cmd->target, cmd->lun);
2586 else
2587 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2588 "extended message "
2589 "code %02x, length %d from target %d, lun %d\n",
2590 HOSTNO, extended_msg[1], extended_msg[0],
2591 cmd->target, cmd->lun);
2594 msgout = MESSAGE_REJECT;
2595 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2596 ICR_ASSERT_ATN);
2597 break;
2598 } /* switch (tmp) */
2599 break;
2600 case PHASE_MSGOUT:
2601 len = 1;
2602 data = &msgout;
2603 hostdata->last_message = msgout;
2604 NCR5380_transfer_pio(instance, &phase, &len, &data);
2605 if (msgout == ABORT) {
2606 #ifdef SUPPORT_TAGS
2607 cmd_free_tag( cmd );
2608 #else
2609 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2610 #endif
2611 hostdata->connected = NULL;
2612 cmd->result = DID_ERROR << 16;
2613 #ifdef NCR5380_STATS
2614 collect_stats(hostdata, cmd);
2615 #endif
2616 cmd->scsi_done(cmd);
2617 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2618 return;
2620 msgout = NOP;
2621 break;
2622 case PHASE_CMDOUT:
2623 len = cmd->cmd_len;
2624 data = cmd->cmnd;
2626 * XXX for performance reasons, on machines with a
2627 * PSEUDO-DMA architecture we should probably
2628 * use the dma transfer function.
2630 NCR5380_transfer_pio(instance, &phase, &len,
2631 &data);
2632 break;
2633 case PHASE_STATIN:
2634 len = 1;
2635 data = &tmp;
2636 NCR5380_transfer_pio(instance, &phase, &len, &data);
2637 cmd->SCp.Status = tmp;
2638 break;
2639 default:
2640 printk("scsi%d: unknown phase\n", HOSTNO);
2641 NCR_PRINT(NDEBUG_ANY);
2642 } /* switch(phase) */
2643 } /* if (tmp * SR_REQ) */
2644 } /* while (1) */
2648 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2650 * Purpose : does reselection, initializing the instance->connected
2651 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2652 * nexus has been reestablished,
2654 * Inputs : instance - this instance of the NCR5380.
2659 static void NCR5380_reselect (struct Scsi_Host *instance)
2661 SETUP_HOSTDATA(instance);
2662 unsigned char target_mask;
2663 unsigned char lun, phase;
2664 int len;
2665 #ifdef SUPPORT_TAGS
2666 unsigned char tag;
2667 #endif
2668 unsigned char msg[3];
2669 unsigned char *data;
2670 Scsi_Cmnd *tmp = NULL, *prev;
2671 /* unsigned long flags; */
2674 * Disable arbitration, etc. since the host adapter obviously
2675 * lost, and tell an interrupted NCR5380_select() to restart.
2678 NCR5380_write(MODE_REG, MR_BASE);
2679 hostdata->restart_select = 1;
2681 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2683 RSL_PRINTK("scsi%d: reselect\n", HOSTNO);
2686 * At this point, we have detected that our SCSI ID is on the bus,
2687 * SEL is true and BSY was false for at least one bus settle delay
2688 * (400 ns).
2690 * We must assert BSY ourselves, until the target drops the SEL
2691 * signal.
2694 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2696 while (NCR5380_read(STATUS_REG) & SR_SEL);
2697 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2700 * Wait for target to go into MSGIN.
2703 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2705 len = 1;
2706 data = msg;
2707 phase = PHASE_MSGIN;
2708 NCR5380_transfer_pio(instance, &phase, &len, &data);
2710 if (!msg[0] & 0x80) {
2711 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2712 print_msg(msg);
2713 do_abort(instance);
2714 return;
2716 lun = (msg[0] & 0x07);
2718 #ifdef SUPPORT_TAGS
2719 /* If the phase is still MSGIN, the target wants to send some more
2720 * messages. In case it supports tagged queuing, this is probably a
2721 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2723 tag = TAG_NONE;
2724 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2725 /* Accept previous IDENTIFY message by clearing ACK */
2726 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
2727 len = 2;
2728 data = msg+1;
2729 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2730 msg[1] == SIMPLE_QUEUE_TAG)
2731 tag = msg[2];
2732 TAG_PRINTK("scsi%d: target mask %02x, lun %d sent tag %d at "
2733 "reselection\n", HOSTNO, target_mask, lun, tag);
2735 #endif
2738 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2739 * just reestablished, and remove it from the disconnected queue.
2742 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2743 tmp; prev = tmp, tmp = NEXT(tmp) ) {
2744 if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
2745 #ifdef SUPPORT_TAGS
2746 && (tag == tmp->tag)
2747 #endif
2749 if (prev) {
2750 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
2751 NEXT(prev) = NEXT(tmp);
2752 } else {
2753 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2754 hostdata->disconnected_queue = NEXT(tmp);
2756 NEXT(tmp) = NULL;
2757 break;
2761 if (!tmp) {
2762 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2763 #ifdef SUPPORT_TAGS
2764 "tag %d "
2765 #endif
2766 "not in disconnect_queue.\n",
2767 HOSTNO, target_mask, lun
2768 #ifdef SUPPORT_TAGS
2769 , tag
2770 #endif
2773 * Since we have an established nexus that we can't do anything
2774 * with, we must abort it.
2776 do_abort(instance);
2777 return;
2780 /* Accept message by clearing ACK */
2781 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2783 hostdata->connected = tmp;
2784 RSL_PRINTK("scsi%d: nexus established, target = %d, lun = %d, tag = %d\n",
2785 HOSTNO, tmp->target, tmp->lun, tmp->tag);
2790 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2792 * Purpose : abort a command
2794 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2795 * host byte of the result field to, if zero DID_ABORTED is
2796 * used.
2798 * Returns : 0 - success, -1 on failure.
2800 * XXX - there is no way to abort the command that is currently
2801 * connected, you have to wait for it to complete. If this is
2802 * a problem, we could implement longjmp() / setjmp(), setjmp()
2803 * called where the loop started in NCR5380_main().
2806 #ifndef NCR5380_abort
2807 static
2808 #endif
2809 int NCR5380_abort (Scsi_Cmnd *cmd)
2811 struct Scsi_Host *instance = cmd->host;
2812 SETUP_HOSTDATA(instance);
2813 Scsi_Cmnd *tmp, **prev;
2814 unsigned long flags;
2816 printk(KERN_NOTICE "scsi%d: aborting command\n", HOSTNO);
2817 print_Scsi_Cmnd (cmd);
2819 NCR5380_print_status (instance);
2821 save_flags(flags);
2822 cli();
2824 ABRT_PRINTK("scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
2825 NCR5380_read(BUS_AND_STATUS_REG),
2826 NCR5380_read(STATUS_REG));
2828 #if 1
2830 * Case 1 : If the command is the currently executing command,
2831 * we'll set the aborted flag and return control so that
2832 * information transfer routine can exit cleanly.
2835 if (hostdata->connected == cmd) {
2837 ABRT_PRINTK("scsi%d: aborting connected command\n", HOSTNO);
2839 * We should perform BSY checking, and make sure we haven't slipped
2840 * into BUS FREE.
2843 /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2845 * Since we can't change phases until we've completed the current
2846 * handshake, we have to source or sink a byte of data if the current
2847 * phase is not MSGOUT.
2851 * Return control to the executing NCR drive so we can clear the
2852 * aborted flag and get back into our main loop.
2855 if (do_abort(instance) == 0) {
2856 hostdata->aborted = 1;
2857 hostdata->connected = NULL;
2858 cmd->result = DID_ABORT << 16;
2859 #ifdef SUPPORT_TAGS
2860 cmd_free_tag( cmd );
2861 #else
2862 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2863 #endif
2864 restore_flags(flags);
2865 cmd->scsi_done(cmd);
2866 return SCSI_ABORT_SUCCESS;
2867 } else {
2868 /* restore_flags(flags); */
2869 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
2870 return SCSI_ABORT_ERROR;
2873 #endif
2876 * Case 2 : If the command hasn't been issued yet, we simply remove it
2877 * from the issue queue.
2879 for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue),
2880 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2881 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
2882 if (cmd == tmp) {
2883 REMOVE(5, *prev, tmp, NEXT(tmp));
2884 (*prev) = NEXT(tmp);
2885 NEXT(tmp) = NULL;
2886 tmp->result = DID_ABORT << 16;
2887 restore_flags(flags);
2888 ABRT_PRINTK("scsi%d: abort removed command from issue queue.\n",
2889 HOSTNO);
2890 /* Tagged queuing note: no tag to free here, hasn't been assigned
2891 * yet... */
2892 tmp->scsi_done(tmp);
2893 return SCSI_ABORT_SUCCESS;
2897 * Case 3 : If any commands are connected, we're going to fail the abort
2898 * and let the high level SCSI driver retry at a later time or
2899 * issue a reset.
2901 * Timeouts, and therefore aborted commands, will be highly unlikely
2902 * and handling them cleanly in this situation would make the common
2903 * case of noresets less efficient, and would pollute our code. So,
2904 * we fail.
2907 if (hostdata->connected) {
2908 restore_flags(flags);
2909 ABRT_PRINTK("scsi%d: abort failed, command connected.\n", HOSTNO);
2910 return SCSI_ABORT_SNOOZE;
2914 * Case 4: If the command is currently disconnected from the bus, and
2915 * there are no connected commands, we reconnect the I_T_L or
2916 * I_T_L_Q nexus associated with it, go into message out, and send
2917 * an abort message.
2919 * This case is especially ugly. In order to reestablish the nexus, we
2920 * need to call NCR5380_select(). The easiest way to implement this
2921 * function was to abort if the bus was busy, and let the interrupt
2922 * handler triggered on the SEL for reselect take care of lost arbitrations
2923 * where necessary, meaning interrupts need to be enabled.
2925 * When interrupts are enabled, the queues may change - so we
2926 * can't remove it from the disconnected queue before selecting it
2927 * because that could cause a failure in hashing the nexus if that
2928 * device reselected.
2930 * Since the queues may change, we can't use the pointers from when we
2931 * first locate it.
2933 * So, we must first locate the command, and if NCR5380_select()
2934 * succeeds, then issue the abort, relocate the command and remove
2935 * it from the disconnected queue.
2938 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2939 tmp = NEXT(tmp))
2940 if (cmd == tmp) {
2941 restore_flags(flags);
2942 ABRT_PRINTK("scsi%d: aborting disconnected command.\n", HOSTNO);
2944 if (NCR5380_select (instance, cmd, (int) cmd->tag))
2945 return SCSI_ABORT_BUSY;
2947 ABRT_PRINTK("scsi%d: nexus reestablished.\n", HOSTNO);
2949 do_abort (instance);
2951 save_flags(flags);
2952 cli();
2953 for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue),
2954 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
2955 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
2956 if (cmd == tmp) {
2957 REMOVE(5, *prev, tmp, NEXT(tmp));
2958 *prev = NEXT(tmp);
2959 NEXT(tmp) = NULL;
2960 tmp->result = DID_ABORT << 16;
2961 /* We must unlock the tag/LUN immediately here, since the
2962 * target goes to BUS FREE and doesn't send us another
2963 * message (COMMAND_COMPLETE or the like)
2965 #ifdef SUPPORT_TAGS
2966 cmd_free_tag( tmp );
2967 #else
2968 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
2969 #endif
2970 restore_flags(flags);
2971 tmp->scsi_done(tmp);
2972 return SCSI_ABORT_SUCCESS;
2977 * Case 5 : If we reached this point, the command was not found in any of
2978 * the queues.
2980 * We probably reached this point because of an unlikely race condition
2981 * between the command completing successfully and the abortion code,
2982 * so we won't panic, but we will notify the user in case something really
2983 * broke.
2986 restore_flags(flags);
2987 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully\n"
2988 KERN_INFO " before abortion\n", HOSTNO);
2990 /* Maybe it is sufficient just to release the ST-DMA lock... (if
2991 * possible at all) At least, we should check if the lock could be
2992 * released after the abort, in case it is kept due to some bug.
2995 return SCSI_ABORT_NOT_RUNNING;
3000 * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
3002 * Purpose : reset the SCSI bus.
3004 * Returns : SCSI_RESET_WAKEUP
3008 static int NCR5380_reset( Scsi_Cmnd *cmd, unsigned int reset_flags)
3010 SETUP_HOSTDATA(cmd->host);
3011 int i;
3012 unsigned long flags;
3013 #if 1
3014 Scsi_Cmnd *connected, *disconnected_queue;
3015 #endif
3017 NCR5380_print_status (cmd->host);
3019 /* get in phase */
3020 NCR5380_write( TARGET_COMMAND_REG,
3021 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
3022 /* assert RST */
3023 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
3024 udelay (40);
3025 /* reset NCR registers */
3026 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
3027 NCR5380_write( MODE_REG, MR_BASE );
3028 NCR5380_write( TARGET_COMMAND_REG, 0 );
3029 NCR5380_write( SELECT_ENABLE_REG, 0 );
3030 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
3031 * through anymore ... */
3032 (void)NCR5380_read( RESET_PARITY_INTERRUPT_REG );
3034 #if 1 /* XXX Should now be done by midlevel code, but it's broken XXX */
3035 /* XXX see below XXX */
3037 /* MSch: old-style reset: actually abort all command processing here */
3039 /* After the reset, there are no more connected or disconnected commands
3040 * and no busy units; to avoid problems with re-inserting the commands
3041 * into the issue_queue (via scsi_done()), the aborted commands are
3042 * remembered in local variables first.
3044 save_flags(flags);
3045 cli();
3046 connected = (Scsi_Cmnd *)hostdata->connected;
3047 hostdata->connected = NULL;
3048 disconnected_queue = (Scsi_Cmnd *)hostdata->disconnected_queue;
3049 hostdata->disconnected_queue = NULL;
3050 #ifdef SUPPORT_TAGS
3051 free_all_tags();
3052 #endif
3053 for( i = 0; i < 8; ++i )
3054 hostdata->busy[i] = 0;
3055 #ifdef REAL_DMA
3056 hostdata->dma_len = 0;
3057 #endif
3058 restore_flags(flags);
3060 /* In order to tell the mid-level code which commands were aborted,
3061 * set the command status to DID_RESET and call scsi_done() !!!
3062 * This ultimately aborts processing of these commands in the mid-level.
3065 if ((cmd = connected)) {
3066 ABRT_PRINTK("scsi%d: reset aborted a connected command\n", H_NO(cmd));
3067 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
3068 cmd->scsi_done( cmd );
3071 for (i = 0; (cmd = disconnected_queue); ++i) {
3072 disconnected_queue = NEXT(cmd);
3073 NEXT(cmd) = NULL;
3074 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
3075 cmd->scsi_done( cmd );
3077 if (i > 0)
3078 ABRT_PRINTK("scsi: reset aborted %d disconnected command(s)\n", i);
3080 /* since all commands have been explicitly terminated, we need to tell
3081 * the midlevel code that the reset was SUCCESSFUL, and there is no
3082 * need to 'wake up' the commands by a request_sense
3084 return SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
3085 #else /* 1 */
3087 /* MSch: new-style reset handling: let the mid-level do what it can */
3089 /* ++guenther: MID-LEVEL IS STILL BROKEN.
3090 * Mid-level is supposed to requeue all commands that were active on the
3091 * various low-level queues. In fact it does this, but that's not enough
3092 * because all these commands are subject to timeout. And if a timeout
3093 * happens for any removed command, *_abort() is called but all queues
3094 * are now empty. Abort then gives up the falcon lock, which is fatal,
3095 * since the mid-level will queue more commands and must have the lock
3096 * (it's all happening inside timer interrupt handler!!).
3097 * Even worse, abort will return NOT_RUNNING for all those commands not
3098 * on any queue, so they won't be retried ...
3100 * Conclusion: either scsi.c disables timeout for all resetted commands
3101 * immediately, or we loose! As of linux-2.0.20 it doesn't.
3104 /* After the reset, there are no more connected or disconnected commands
3105 * and no busy units; so clear the low-level status here to avoid
3106 * conflicts when the mid-level code tries to wake up the affected
3107 * commands!
3110 if (hostdata->issue_queue)
3111 ABRT_PRINTK("scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
3112 if (hostdata->connected)
3113 ABRT_PRINTK("scsi%d: reset aborted a connected command\n", H_NO(cmd));
3114 if (hostdata->disconnected_queue)
3115 ABRT_PRINTK("scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
3117 save_flags(flags);
3118 cli();
3119 hostdata->issue_queue = NULL;
3120 hostdata->connected = NULL;
3121 hostdata->disconnected_queue = NULL;
3122 #ifdef SUPPORT_TAGS
3123 free_all_tags();
3124 #endif
3125 for( i = 0; i < 8; ++i )
3126 hostdata->busy[i] = 0;
3127 #ifdef REAL_DMA
3128 hostdata->dma_len = 0;
3129 #endif
3130 restore_flags(flags);
3132 /* we did no complete reset of all commands, so a wakeup is required */
3133 return SCSI_RESET_WAKEUP | SCSI_RESET_BUS_RESET;
3134 #endif /* 1 */
3137 /* Local Variables: */
3138 /* tab-width: 8 */
3139 /* End: */