aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/atari_NCR5380.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/scsi/atari_NCR5380.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/scsi/atari_NCR5380.c')
-rw-r--r--drivers/scsi/atari_NCR5380.c2986
1 files changed, 2986 insertions, 0 deletions
diff --git a/drivers/scsi/atari_NCR5380.c b/drivers/scsi/atari_NCR5380.c
new file mode 100644
index 000000000000..5d1e78ebed83
--- /dev/null
+++ b/drivers/scsi/atari_NCR5380.c
@@ -0,0 +1,2986 @@
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.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
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
13 *
14 * DISTRIBUTION RELEASE 6.
15 *
16 * For more information, please consult
17 *
18 * NCR 5380 Family
19 * SCSI Protocol Controller
20 * Databook
21 *
22 * NCR Microelectronics
23 * 1635 Aeroplaza Drive
24 * Colorado Springs, CO 80916
25 * 1+ (719) 578-3400
26 * 1+ (800) 334-5454
27 */
28
29/*
30 * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
31 * this file, too:
32 *
33 * - Some of the debug statements were incorrect (undefined variables and the
34 * like). I fixed that.
35 *
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.
39 *
40 * - When using real DMA, information_transfer() should return in a DATAOUT
41 * phase after starting the DMA. It has nothing more to do.
42 *
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.
49 *
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.
56 *
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.
60 *
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.
64 *
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.
68 *
69 */
70
71/*
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.
75 */
76
77#if (NDEBUG & NDEBUG_LISTS)
78#define LIST(x,y) \
79 { printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); \
80 if ((x)==(y)) udelay(5); }
81#define REMOVE(w,x,y,z) \
82 { printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, \
83 (void*)(w), (void*)(x), (void*)(y), (void*)(z)); \
84 if ((x)==(y)) udelay(5); }
85#else
86#define LIST(x,y)
87#define REMOVE(w,x,y,z)
88#endif
89
90#ifndef notyet
91#undef LINKED
92#endif
93
94/*
95 * Design
96 * Issues :
97 *
98 * The other Linux SCSI drivers were written when Linux was Intel PC-only,
99 * and specifically for each board rather than each chip. This makes their
100 * adaptation to platforms like the Mac (Some of which use NCR5380's)
101 * more difficult than it has to be.
102 *
103 * Also, many of the SCSI drivers were written before the command queuing
104 * routines were implemented, meaning their implementations of queued
105 * commands were hacked on rather than designed in from the start.
106 *
107 * When I designed the Linux SCSI drivers I figured that
108 * while having two different SCSI boards in a system might be useful
109 * for debugging things, two of the same type wouldn't be used.
110 * Well, I was wrong and a number of users have mailed me about running
111 * multiple high-performance SCSI boards in a server.
112 *
113 * Finally, when I get questions from users, I have no idea what
114 * revision of my driver they are running.
115 *
116 * This driver attempts to address these problems :
117 * This is a generic 5380 driver. To use it on a different platform,
118 * one simply writes appropriate system specific macros (ie, data
119 * transfer - some PC's will use the I/O bus, 68K's must use
120 * memory mapped) and drops this file in their 'C' wrapper.
121 *
122 * As far as command queueing, two queues are maintained for
123 * each 5380 in the system - commands that haven't been issued yet,
124 * and commands that are currently executing. This means that an
125 * unlimited number of commands may be queued, letting
126 * more commands propagate from the higher driver levels giving higher
127 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
128 * allowing multiple commands to propagate all the way to a SCSI-II device
129 * while a command is already executing.
130 *
131 * To solve the multiple-boards-in-the-same-system problem,
132 * there is a separate instance structure for each instance
133 * of a 5380 in the system. So, multiple NCR5380 drivers will
134 * be able to coexist with appropriate changes to the high level
135 * SCSI code.
136 *
137 * A NCR5380_PUBLIC_REVISION macro is provided, with the release
138 * number (updated for each public release) printed by the
139 * NCR5380_print_options command, which should be called from the
140 * wrapper detect function, so that I know what release of the driver
141 * users are using.
142 *
143 * Issues specific to the NCR5380 :
144 *
145 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
146 * piece of hardware that requires you to sit in a loop polling for
147 * the REQ signal as long as you are connected. Some devices are
148 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
149 * while doing long seek operations.
150 *
151 * The workaround for this is to keep track of devices that have
152 * disconnected. If the device hasn't disconnected, for commands that
153 * should disconnect, we do something like
154 *
155 * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
156 *
157 * Some tweaking of N and M needs to be done. An algorithm based
158 * on "time to data" would give the best results as long as short time
159 * to datas (ie, on the same track) were considered, however these
160 * broken devices are the exception rather than the rule and I'd rather
161 * spend my time optimizing for the normal case.
162 *
163 * Architecture :
164 *
165 * At the heart of the design is a coroutine, NCR5380_main,
166 * which is started when not running by the interrupt handler,
167 * timer, and queue command function. It attempts to establish
168 * I_T_L or I_T_L_Q nexuses by removing the commands from the
169 * issue queue and calling NCR5380_select() if a nexus
170 * is not established.
171 *
172 * Once a nexus is established, the NCR5380_information_transfer()
173 * phase goes through the various phases as instructed by the target.
174 * if the target goes into MSG IN and sends a DISCONNECT message,
175 * the command structure is placed into the per instance disconnected
176 * queue, and NCR5380_main tries to find more work. If USLEEP
177 * was defined, and the target is idle for too long, the system
178 * will try to sleep.
179 *
180 * If a command has disconnected, eventually an interrupt will trigger,
181 * calling NCR5380_intr() which will in turn call NCR5380_reselect
182 * to reestablish a nexus. This will run main if necessary.
183 *
184 * On command termination, the done function will be called as
185 * appropriate.
186 *
187 * SCSI pointers are maintained in the SCp field of SCSI command
188 * structures, being initialized after the command is connected
189 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
190 * Note that in violation of the standard, an implicit SAVE POINTERS operation
191 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
192 */
193
194/*
195 * Using this file :
196 * This file a skeleton Linux SCSI driver for the NCR 5380 series
197 * of chips. To use it, you write an architecture specific functions
198 * and macros and include this file in your driver.
199 *
200 * These macros control options :
201 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
202 * for commands that return with a CHECK CONDITION status.
203 *
204 * LINKED - if defined, linked commands are supported.
205 *
206 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
207 *
208 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
209 *
210 * These macros MUST be defined :
211 *
212 * NCR5380_read(register) - read from the specified register
213 *
214 * NCR5380_write(register, value) - write to the specific register
215 *
216 * Either real DMA *or* pseudo DMA may be implemented
217 * REAL functions :
218 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
219 * Note that the DMA setup functions should return the number of bytes
220 * that they were able to program the controller for.
221 *
222 * Also note that generic i386/PC versions of these macros are
223 * available as NCR5380_i386_dma_write_setup,
224 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
225 *
226 * NCR5380_dma_write_setup(instance, src, count) - initialize
227 * NCR5380_dma_read_setup(instance, dst, count) - initialize
228 * NCR5380_dma_residual(instance); - residual count
229 *
230 * PSEUDO functions :
231 * NCR5380_pwrite(instance, src, count)
232 * NCR5380_pread(instance, dst, count);
233 *
234 * If nothing specific to this implementation needs doing (ie, with external
235 * hardware), you must also define
236 *
237 * NCR5380_queue_command
238 * NCR5380_reset
239 * NCR5380_abort
240 * NCR5380_proc_info
241 *
242 * to be the global entry points into the specific driver, ie
243 * #define NCR5380_queue_command t128_queue_command.
244 *
245 * If this is not done, the routines will be defined as static functions
246 * with the NCR5380* names and the user must provide a globally
247 * accessible wrapper function.
248 *
249 * The generic driver is initialized by calling NCR5380_init(instance),
250 * after setting the appropriate host specific fields and ID. If the
251 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
252 * possible) function may be used. Before the specific driver initialization
253 * code finishes, NCR5380_print_options should be called.
254 */
255
256static struct Scsi_Host *first_instance = NULL;
257static Scsi_Host_Template *the_template = NULL;
258
259/* Macros ease life... :-) */
260#define SETUP_HOSTDATA(in) \
261 struct NCR5380_hostdata *hostdata = \
262 (struct NCR5380_hostdata *)(in)->hostdata
263#define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
264
265#define NEXT(cmd) ((Scsi_Cmnd *)((cmd)->host_scribble))
266#define NEXTADDR(cmd) ((Scsi_Cmnd **)&((cmd)->host_scribble))
267
268#define HOSTNO instance->host_no
269#define H_NO(cmd) (cmd)->device->host->host_no
270
271#ifdef SUPPORT_TAGS
272
273/*
274 * Functions for handling tagged queuing
275 * =====================================
276 *
277 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
278 *
279 * Using consecutive numbers for the tags is no good idea in my eyes. There
280 * could be wrong re-usings if the counter (8 bit!) wraps and some early
281 * command has been preempted for a long time. My solution: a bitfield for
282 * remembering used tags.
283 *
284 * There's also the problem that each target has a certain queue size, but we
285 * cannot know it in advance :-( We just see a QUEUE_FULL status being
286 * returned. So, in this case, the driver internal queue size assumption is
287 * reduced to the number of active tags if QUEUE_FULL is returned by the
288 * target. The command is returned to the mid-level, but with status changed
289 * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
290 * correctly.
291 *
292 * We're also not allowed running tagged commands as long as an untagged
293 * command is active. And REQUEST SENSE commands after a contingent allegiance
294 * condition _must_ be untagged. To keep track whether an untagged command has
295 * been issued, the host->busy array is still employed, as it is without
296 * support for tagged queuing.
297 *
298 * One could suspect that there are possible race conditions between
299 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
300 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
301 * which already guaranteed to be running at most once. It is also the only
302 * place where tags/LUNs are allocated. So no other allocation can slip
303 * between that pair, there could only happen a reselection, which can free a
304 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
305 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
306 */
307
308/* -1 for TAG_NONE is not possible with unsigned char cmd->tag */
309#undef TAG_NONE
310#define TAG_NONE 0xff
311
312typedef struct {
313 DECLARE_BITMAP(allocated, MAX_TAGS);
314 int nr_allocated;
315 int queue_size;
316} TAG_ALLOC;
317
318static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
319
320
321static void __init init_tags( void )
322{
323 int target, lun;
324 TAG_ALLOC *ta;
325
326 if (!setup_use_tagged_queuing)
327 return;
328
329 for( target = 0; target < 8; ++target ) {
330 for( lun = 0; lun < 8; ++lun ) {
331 ta = &TagAlloc[target][lun];
332 bitmap_zero(ta->allocated, MAX_TAGS);
333 ta->nr_allocated = 0;
334 /* At the beginning, assume the maximum queue size we could
335 * support (MAX_TAGS). This value will be decreased if the target
336 * returns QUEUE_FULL status.
337 */
338 ta->queue_size = MAX_TAGS;
339 }
340 }
341}
342
343
344/* Check if we can issue a command to this LUN: First see if the LUN is marked
345 * busy by an untagged command. If the command should use tagged queuing, also
346 * check that there is a free tag and the target's queue won't overflow. This
347 * function should be called with interrupts disabled to avoid race
348 * conditions.
349 */
350
351static int is_lun_busy( Scsi_Cmnd *cmd, int should_be_tagged )
352{
353 SETUP_HOSTDATA(cmd->device->host);
354
355 if (hostdata->busy[cmd->device->id] & (1 << cmd->device->lun))
356 return( 1 );
357 if (!should_be_tagged ||
358 !setup_use_tagged_queuing || !cmd->device->tagged_supported)
359 return( 0 );
360 if (TagAlloc[cmd->device->id][cmd->device->lun].nr_allocated >=
361 TagAlloc[cmd->device->id][cmd->device->lun].queue_size ) {
362 TAG_PRINTK( "scsi%d: target %d lun %d: no free tags\n",
363 H_NO(cmd), cmd->device->id, cmd->device->lun );
364 return( 1 );
365 }
366 return( 0 );
367}
368
369
370/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
371 * must be called before!), or reserve the LUN in 'busy' if the command is
372 * untagged.
373 */
374
375static void cmd_get_tag( Scsi_Cmnd *cmd, int should_be_tagged )
376{
377 SETUP_HOSTDATA(cmd->device->host);
378
379 /* If we or the target don't support tagged queuing, allocate the LUN for
380 * an untagged command.
381 */
382 if (!should_be_tagged ||
383 !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
384 cmd->tag = TAG_NONE;
385 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
386 TAG_PRINTK( "scsi%d: target %d lun %d now allocated by untagged "
387 "command\n", H_NO(cmd), cmd->device->id, cmd->device->lun );
388 }
389 else {
390 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
391
392 cmd->tag = find_first_zero_bit( ta->allocated, MAX_TAGS );
393 set_bit( cmd->tag, ta->allocated );
394 ta->nr_allocated++;
395 TAG_PRINTK( "scsi%d: using tag %d for target %d lun %d "
396 "(now %d tags in use)\n",
397 H_NO(cmd), cmd->tag, cmd->device->id, cmd->device->lun,
398 ta->nr_allocated );
399 }
400}
401
402
403/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
404 * unlock the LUN.
405 */
406
407static void cmd_free_tag( Scsi_Cmnd *cmd )
408{
409 SETUP_HOSTDATA(cmd->device->host);
410
411 if (cmd->tag == TAG_NONE) {
412 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
413 TAG_PRINTK( "scsi%d: target %d lun %d untagged cmd finished\n",
414 H_NO(cmd), cmd->device->id, cmd->device->lun );
415 }
416 else if (cmd->tag >= MAX_TAGS) {
417 printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n",
418 H_NO(cmd), cmd->tag );
419 }
420 else {
421 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
422 clear_bit( cmd->tag, ta->allocated );
423 ta->nr_allocated--;
424 TAG_PRINTK( "scsi%d: freed tag %d for target %d lun %d\n",
425 H_NO(cmd), cmd->tag, cmd->device->id, cmd->device->lun );
426 }
427}
428
429
430static void free_all_tags( void )
431{
432 int target, lun;
433 TAG_ALLOC *ta;
434
435 if (!setup_use_tagged_queuing)
436 return;
437
438 for( target = 0; target < 8; ++target ) {
439 for( lun = 0; lun < 8; ++lun ) {
440 ta = &TagAlloc[target][lun];
441 bitmap_zero(ta->allocated, MAX_TAGS);
442 ta->nr_allocated = 0;
443 }
444 }
445}
446
447#endif /* SUPPORT_TAGS */
448
449
450/*
451 * Function: void merge_contiguous_buffers( Scsi_Cmnd *cmd )
452 *
453 * Purpose: Try to merge several scatter-gather requests into one DMA
454 * transfer. This is possible if the scatter buffers lie on
455 * physical contiguous addresses.
456 *
457 * Parameters: Scsi_Cmnd *cmd
458 * The command to work on. The first scatter buffer's data are
459 * assumed to be already transfered into ptr/this_residual.
460 */
461
462static void merge_contiguous_buffers( Scsi_Cmnd *cmd )
463{
464 unsigned long endaddr;
465#if (NDEBUG & NDEBUG_MERGING)
466 unsigned long oldlen = cmd->SCp.this_residual;
467 int cnt = 1;
468#endif
469
470 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
471 cmd->SCp.buffers_residual &&
472 virt_to_phys(page_address(cmd->SCp.buffer[1].page)+
473 cmd->SCp.buffer[1].offset) == endaddr; ) {
474 MER_PRINTK("VTOP(%p) == %08lx -> merging\n",
475 cmd->SCp.buffer[1].address, endaddr);
476#if (NDEBUG & NDEBUG_MERGING)
477 ++cnt;
478#endif
479 ++cmd->SCp.buffer;
480 --cmd->SCp.buffers_residual;
481 cmd->SCp.this_residual += cmd->SCp.buffer->length;
482 endaddr += cmd->SCp.buffer->length;
483 }
484#if (NDEBUG & NDEBUG_MERGING)
485 if (oldlen != cmd->SCp.this_residual)
486 MER_PRINTK("merged %d buffers from %p, new length %08x\n",
487 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
488#endif
489}
490
491/*
492 * Function : void initialize_SCp(Scsi_Cmnd *cmd)
493 *
494 * Purpose : initialize the saved data pointers for cmd to point to the
495 * start of the buffer.
496 *
497 * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
498 */
499
500static __inline__ void initialize_SCp(Scsi_Cmnd *cmd)
501{
502 /*
503 * Initialize the Scsi Pointer field so that all of the commands in the
504 * various queues are valid.
505 */
506
507 if (cmd->use_sg) {
508 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
509 cmd->SCp.buffers_residual = cmd->use_sg - 1;
510 cmd->SCp.ptr = (char *)page_address(cmd->SCp.buffer->page)+
511 cmd->SCp.buffer->offset;
512 cmd->SCp.this_residual = cmd->SCp.buffer->length;
513 /* ++roman: Try to merge some scatter-buffers if they are at
514 * contiguous physical addresses.
515 */
516 merge_contiguous_buffers( cmd );
517 } else {
518 cmd->SCp.buffer = NULL;
519 cmd->SCp.buffers_residual = 0;
520 cmd->SCp.ptr = (char *) cmd->request_buffer;
521 cmd->SCp.this_residual = cmd->request_bufflen;
522 }
523}
524
525#include <linux/config.h>
526#include <linux/delay.h>
527
528#if NDEBUG
529static struct {
530 unsigned char mask;
531 const char * name;}
532signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" },
533 { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" },
534 { SR_SEL, "SEL" }, {0, NULL}},
535basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
536icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
537 {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"},
538 {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"},
539 {0, NULL}},
540mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"},
541 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR,
542 "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
543 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
544 {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"},
545 {0, NULL}};
546
547/*
548 * Function : void NCR5380_print(struct Scsi_Host *instance)
549 *
550 * Purpose : print the SCSI bus signals for debugging purposes
551 *
552 * Input : instance - which NCR5380
553 */
554
555static void NCR5380_print(struct Scsi_Host *instance) {
556 unsigned char status, data, basr, mr, icr, i;
557 unsigned long flags;
558
559 local_irq_save(flags);
560 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
561 status = NCR5380_read(STATUS_REG);
562 mr = NCR5380_read(MODE_REG);
563 icr = NCR5380_read(INITIATOR_COMMAND_REG);
564 basr = NCR5380_read(BUS_AND_STATUS_REG);
565 local_irq_restore(flags);
566 printk("STATUS_REG: %02x ", status);
567 for (i = 0; signals[i].mask ; ++i)
568 if (status & signals[i].mask)
569 printk(",%s", signals[i].name);
570 printk("\nBASR: %02x ", basr);
571 for (i = 0; basrs[i].mask ; ++i)
572 if (basr & basrs[i].mask)
573 printk(",%s", basrs[i].name);
574 printk("\nICR: %02x ", icr);
575 for (i = 0; icrs[i].mask; ++i)
576 if (icr & icrs[i].mask)
577 printk(",%s", icrs[i].name);
578 printk("\nMODE: %02x ", mr);
579 for (i = 0; mrs[i].mask; ++i)
580 if (mr & mrs[i].mask)
581 printk(",%s", mrs[i].name);
582 printk("\n");
583}
584
585static struct {
586 unsigned char value;
587 const char *name;
588} phases[] = {
589 {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
590 {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
591 {PHASE_UNKNOWN, "UNKNOWN"}};
592
593/*
594 * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
595 *
596 * Purpose : print the current SCSI phase for debugging purposes
597 *
598 * Input : instance - which NCR5380
599 */
600
601static void NCR5380_print_phase(struct Scsi_Host *instance)
602{
603 unsigned char status;
604 int i;
605
606 status = NCR5380_read(STATUS_REG);
607 if (!(status & SR_REQ))
608 printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO);
609 else {
610 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
611 (phases[i].value != (status & PHASE_MASK)); ++i);
612 printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name);
613 }
614}
615
616#else /* !NDEBUG */
617
618/* dummies... */
619__inline__ void NCR5380_print(struct Scsi_Host *instance) { };
620__inline__ void NCR5380_print_phase(struct Scsi_Host *instance) { };
621
622#endif
623
624/*
625 * ++roman: New scheme of calling NCR5380_main()
626 *
627 * If we're not in an interrupt, we can call our main directly, it cannot be
628 * already running. Else, we queue it on a task queue, if not 'main_running'
629 * tells us that a lower level is already executing it. This way,
630 * 'main_running' needs not be protected in a special way.
631 *
632 * queue_main() is a utility function for putting our main onto the task
633 * queue, if main_running is false. It should be called only from a
634 * interrupt or bottom half.
635 */
636
637#include <linux/workqueue.h>
638#include <linux/interrupt.h>
639
640static volatile int main_running = 0;
641static DECLARE_WORK(NCR5380_tqueue, (void (*)(void*))NCR5380_main, NULL);
642
643static __inline__ void queue_main(void)
644{
645 if (!main_running) {
646 /* If in interrupt and NCR5380_main() not already running,
647 queue it on the 'immediate' task queue, to be processed
648 immediately after the current interrupt processing has
649 finished. */
650 schedule_work(&NCR5380_tqueue);
651 }
652 /* else: nothing to do: the running NCR5380_main() will pick up
653 any newly queued command. */
654}
655
656
657static inline void NCR5380_all_init (void)
658{
659 static int done = 0;
660 if (!done) {
661 INI_PRINTK("scsi : NCR5380_all_init()\n");
662 done = 1;
663 }
664}
665
666
667/*
668 * Function : void NCR58380_print_options (struct Scsi_Host *instance)
669 *
670 * Purpose : called by probe code indicating the NCR5380 driver
671 * options that were selected.
672 *
673 * Inputs : instance, pointer to this instance. Unused.
674 */
675
676static void __init NCR5380_print_options (struct Scsi_Host *instance)
677{
678 printk(" generic options"
679#ifdef AUTOSENSE
680 " AUTOSENSE"
681#endif
682#ifdef REAL_DMA
683 " REAL DMA"
684#endif
685#ifdef PARITY
686 " PARITY"
687#endif
688#ifdef SUPPORT_TAGS
689 " SCSI-2 TAGGED QUEUING"
690#endif
691 );
692 printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
693}
694
695/*
696 * Function : void NCR5380_print_status (struct Scsi_Host *instance)
697 *
698 * Purpose : print commands in the various queues, called from
699 * NCR5380_abort and NCR5380_debug to aid debugging.
700 *
701 * Inputs : instance, pointer to this instance.
702 */
703
704static void NCR5380_print_status (struct Scsi_Host *instance)
705{
706 char *pr_bfr;
707 char *start;
708 int len;
709
710 NCR_PRINT(NDEBUG_ANY);
711 NCR_PRINT_PHASE(NDEBUG_ANY);
712
713 pr_bfr = (char *) __get_free_page(GFP_ATOMIC);
714 if (!pr_bfr) {
715 printk("NCR5380_print_status: no memory for print buffer\n");
716 return;
717 }
718 len = NCR5380_proc_info(pr_bfr, &start, 0, PAGE_SIZE, HOSTNO, 0);
719 pr_bfr[len] = 0;
720 printk("\n%s\n", pr_bfr);
721 free_page((unsigned long) pr_bfr);
722}
723
724
725/******************************************/
726/*
727 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
728 *
729 * *buffer: I/O buffer
730 * **start: if inout == FALSE pointer into buffer where user read should start
731 * offset: current offset
732 * length: length of buffer
733 * hostno: Scsi_Host host_no
734 * inout: TRUE - user is writing; FALSE - user is reading
735 *
736 * Return the number of bytes read from or written
737*/
738
739#undef SPRINTF
740#define SPRINTF(fmt,args...) \
741 do { if (pos + strlen(fmt) + 20 /* slop */ < buffer + length) \
742 pos += sprintf(pos, fmt , ## args); } while(0)
743static
744char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length);
745
746static
747int NCR5380_proc_info (struct Scsi_Host *instance, char *buffer, char **start, off_t offset,
748 int length, int inout)
749{
750 char *pos = buffer;
751 struct NCR5380_hostdata *hostdata;
752 Scsi_Cmnd *ptr;
753 unsigned long flags;
754 off_t begin = 0;
755#define check_offset() \
756 do { \
757 if (pos - buffer < offset - begin) { \
758 begin += pos - buffer; \
759 pos = buffer; \
760 } \
761 } while (0)
762
763 hostdata = (struct NCR5380_hostdata *)instance->hostdata;
764
765 if (inout) { /* Has data been written to the file ? */
766 return(-ENOSYS); /* Currently this is a no-op */
767 }
768 SPRINTF("NCR5380 core release=%d.\n", NCR5380_PUBLIC_RELEASE);
769 check_offset();
770 local_irq_save(flags);
771 SPRINTF("NCR5380: coroutine is%s running.\n", main_running ? "" : "n't");
772 check_offset();
773 if (!hostdata->connected)
774 SPRINTF("scsi%d: no currently connected command\n", HOSTNO);
775 else
776 pos = lprint_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected,
777 pos, buffer, length);
778 SPRINTF("scsi%d: issue_queue\n", HOSTNO);
779 check_offset();
780 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = NEXT(ptr)) {
781 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
782 check_offset();
783 }
784
785 SPRINTF("scsi%d: disconnected_queue\n", HOSTNO);
786 check_offset();
787 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
788 ptr = NEXT(ptr)) {
789 pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
790 check_offset();
791 }
792
793 local_irq_restore(flags);
794 *start = buffer + (offset - begin);
795 if (pos - buffer < offset - begin)
796 return 0;
797 else if (pos - buffer - (offset - begin) < length)
798 return pos - buffer - (offset - begin);
799 return length;
800}
801
802static char *
803lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length)
804{
805 int i, s;
806 unsigned char *command;
807 SPRINTF("scsi%d: destination target %d, lun %d\n",
808 H_NO(cmd), cmd->device->id, cmd->device->lun);
809 SPRINTF(" command = ");
810 command = cmd->cmnd;
811 SPRINTF("%2d (0x%02x)", command[0], command[0]);
812 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
813 SPRINTF(" %02x", command[i]);
814 SPRINTF("\n");
815 return pos;
816}
817
818
819/*
820 * Function : void NCR5380_init (struct Scsi_Host *instance)
821 *
822 * Purpose : initializes *instance and corresponding 5380 chip.
823 *
824 * Inputs : instance - instantiation of the 5380 driver.
825 *
826 * Notes : I assume that the host, hostno, and id bits have been
827 * set correctly. I don't care about the irq and other fields.
828 *
829 */
830
831static int NCR5380_init (struct Scsi_Host *instance, int flags)
832{
833 int i;
834 SETUP_HOSTDATA(instance);
835
836 NCR5380_all_init();
837
838 hostdata->aborted = 0;
839 hostdata->id_mask = 1 << instance->this_id;
840 hostdata->id_higher_mask = 0;
841 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
842 if (i > hostdata->id_mask)
843 hostdata->id_higher_mask |= i;
844 for (i = 0; i < 8; ++i)
845 hostdata->busy[i] = 0;
846#ifdef SUPPORT_TAGS
847 init_tags();
848#endif
849#if defined (REAL_DMA)
850 hostdata->dma_len = 0;
851#endif
852 hostdata->targets_present = 0;
853 hostdata->connected = NULL;
854 hostdata->issue_queue = NULL;
855 hostdata->disconnected_queue = NULL;
856 hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
857
858 if (!the_template) {
859 the_template = instance->hostt;
860 first_instance = instance;
861 }
862
863
864#ifndef AUTOSENSE
865 if ((instance->cmd_per_lun > 1) || (instance->can_queue > 1))
866 printk("scsi%d: WARNING : support for multiple outstanding commands enabled\n"
867 " without AUTOSENSE option, contingent allegiance conditions may\n"
868 " be incorrectly cleared.\n", HOSTNO);
869#endif /* def AUTOSENSE */
870
871 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
872 NCR5380_write(MODE_REG, MR_BASE);
873 NCR5380_write(TARGET_COMMAND_REG, 0);
874 NCR5380_write(SELECT_ENABLE_REG, 0);
875
876 return 0;
877}
878
879/*
880 * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd,
881 * void (*done)(Scsi_Cmnd *))
882 *
883 * Purpose : enqueues a SCSI command
884 *
885 * Inputs : cmd - SCSI command, done - function called on completion, with
886 * a pointer to the command descriptor.
887 *
888 * Returns : 0
889 *
890 * Side effects :
891 * cmd is added to the per instance issue_queue, with minor
892 * twiddling done to the host specific fields of cmd. If the
893 * main coroutine is not running, it is restarted.
894 *
895 */
896
897static
898int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
899{
900 SETUP_HOSTDATA(cmd->device->host);
901 Scsi_Cmnd *tmp;
902 int oldto;
903 unsigned long flags;
904 extern int update_timeout(Scsi_Cmnd * SCset, int timeout);
905
906#if (NDEBUG & NDEBUG_NO_WRITE)
907 switch (cmd->cmnd[0]) {
908 case WRITE_6:
909 case WRITE_10:
910 printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n",
911 H_NO(cmd));
912 cmd->result = (DID_ERROR << 16);
913 done(cmd);
914 return 0;
915 }
916#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
917
918
919#ifdef NCR5380_STATS
920# if 0
921 if (!hostdata->connected && !hostdata->issue_queue &&
922 !hostdata->disconnected_queue) {
923 hostdata->timebase = jiffies;
924 }
925# endif
926# ifdef NCR5380_STAT_LIMIT
927 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
928# endif
929 switch (cmd->cmnd[0])
930 {
931 case WRITE:
932 case WRITE_6:
933 case WRITE_10:
934 hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
935 hostdata->bytes_write[cmd->device->id] += cmd->request_bufflen;
936 hostdata->pendingw++;
937 break;
938 case READ:
939 case READ_6:
940 case READ_10:
941 hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
942 hostdata->bytes_read[cmd->device->id] += cmd->request_bufflen;
943 hostdata->pendingr++;
944 break;
945 }
946#endif
947
948 /*
949 * We use the host_scribble field as a pointer to the next command
950 * in a queue
951 */
952
953 NEXT(cmd) = NULL;
954 cmd->scsi_done = done;
955
956 cmd->result = 0;
957
958
959 /*
960 * Insert the cmd into the issue queue. Note that REQUEST SENSE
961 * commands are added to the head of the queue since any command will
962 * clear the contingent allegiance condition that exists and the
963 * sense data is only guaranteed to be valid while the condition exists.
964 */
965
966 local_irq_save(flags);
967 /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
968 * Otherwise a running NCR5380_main may steal the lock.
969 * Lock before actually inserting due to fairness reasons explained in
970 * atari_scsi.c. If we insert first, then it's impossible for this driver
971 * to release the lock.
972 * Stop timer for this command while waiting for the lock, or timeouts
973 * may happen (and they really do), and it's no good if the command doesn't
974 * appear in any of the queues.
975 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
976 * because also a timer int can trigger an abort or reset, which would
977 * alter queues and touch the lock.
978 */
979 if (!IS_A_TT()) {
980 oldto = update_timeout(cmd, 0);
981 falcon_get_lock();
982 update_timeout(cmd, oldto);
983 }
984 if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
985 LIST(cmd, hostdata->issue_queue);
986 NEXT(cmd) = hostdata->issue_queue;
987 hostdata->issue_queue = cmd;
988 } else {
989 for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
990 NEXT(tmp); tmp = NEXT(tmp))
991 ;
992 LIST(cmd, tmp);
993 NEXT(tmp) = cmd;
994 }
995 local_irq_restore(flags);
996
997 QU_PRINTK("scsi%d: command added to %s of queue\n", H_NO(cmd),
998 (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
999
1000 /* If queue_command() is called from an interrupt (real one or bottom
1001 * half), we let queue_main() do the job of taking care about main. If it
1002 * is already running, this is a no-op, else main will be queued.
1003 *
1004 * If we're not in an interrupt, we can call NCR5380_main()
1005 * unconditionally, because it cannot be already running.
1006 */
1007 if (in_interrupt() || ((flags >> 8) & 7) >= 6)
1008 queue_main();
1009 else
1010 NCR5380_main(NULL);
1011 return 0;
1012}
1013
1014/*
1015 * Function : NCR5380_main (void)
1016 *
1017 * Purpose : NCR5380_main is a coroutine that runs as long as more work can
1018 * be done on the NCR5380 host adapters in a system. Both
1019 * NCR5380_queue_command() and NCR5380_intr() will try to start it
1020 * in case it is not running.
1021 *
1022 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should
1023 * reenable them. This prevents reentrancy and kernel stack overflow.
1024 */
1025
1026static void NCR5380_main (void *bl)
1027{
1028 Scsi_Cmnd *tmp, *prev;
1029 struct Scsi_Host *instance = first_instance;
1030 struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
1031 int done;
1032 unsigned long flags;
1033
1034 /*
1035 * We run (with interrupts disabled) until we're sure that none of
1036 * the host adapters have anything that can be done, at which point
1037 * we set main_running to 0 and exit.
1038 *
1039 * Interrupts are enabled before doing various other internal
1040 * instructions, after we've decided that we need to run through
1041 * the loop again.
1042 *
1043 * this should prevent any race conditions.
1044 *
1045 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
1046 * because also a timer int can trigger an abort or reset, which can
1047 * alter queues and touch the Falcon lock.
1048 */
1049
1050 /* Tell int handlers main() is now already executing. Note that
1051 no races are possible here. If an int comes in before
1052 'main_running' is set here, and queues/executes main via the
1053 task queue, it doesn't do any harm, just this instance of main
1054 won't find any work left to do. */
1055 if (main_running)
1056 return;
1057 main_running = 1;
1058
1059 local_save_flags(flags);
1060 do {
1061 local_irq_disable(); /* Freeze request queues */
1062 done = 1;
1063
1064 if (!hostdata->connected) {
1065 MAIN_PRINTK( "scsi%d: not connected\n", HOSTNO );
1066 /*
1067 * Search through the issue_queue for a command destined
1068 * for a target that's not busy.
1069 */
1070#if (NDEBUG & NDEBUG_LISTS)
1071 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
1072 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
1073 ;
1074 /*printk("%p ", tmp);*/
1075 if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
1076#endif
1077 for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
1078 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp) ) {
1079
1080#if (NDEBUG & NDEBUG_LISTS)
1081 if (prev != tmp)
1082 printk("MAIN tmp=%p target=%d busy=%d lun=%d\n",
1083 tmp, tmp->device->id, hostdata->busy[tmp->device->id],
1084 tmp->device->lun);
1085#endif
1086 /* When we find one, remove it from the issue queue. */
1087 /* ++guenther: possible race with Falcon locking */
1088 if (
1089#ifdef SUPPORT_TAGS
1090 !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
1091#else
1092 !(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))
1093#endif
1094 ) {
1095 /* ++guenther: just to be sure, this must be atomic */
1096 local_irq_disable();
1097 if (prev) {
1098 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
1099 NEXT(prev) = NEXT(tmp);
1100 } else {
1101 REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
1102 hostdata->issue_queue = NEXT(tmp);
1103 }
1104 NEXT(tmp) = NULL;
1105 falcon_dont_release++;
1106
1107 /* reenable interrupts after finding one */
1108 local_irq_restore(flags);
1109
1110 /*
1111 * Attempt to establish an I_T_L nexus here.
1112 * On success, instance->hostdata->connected is set.
1113 * On failure, we must add the command back to the
1114 * issue queue so we can keep trying.
1115 */
1116 MAIN_PRINTK("scsi%d: main(): command for target %d "
1117 "lun %d removed from issue_queue\n",
1118 HOSTNO, tmp->device->id, tmp->device->lun);
1119 /*
1120 * REQUEST SENSE commands are issued without tagged
1121 * queueing, even on SCSI-II devices because the
1122 * contingent allegiance condition exists for the
1123 * entire unit.
1124 */
1125 /* ++roman: ...and the standard also requires that
1126 * REQUEST SENSE command are untagged.
1127 */
1128
1129#ifdef SUPPORT_TAGS
1130 cmd_get_tag( tmp, tmp->cmnd[0] != REQUEST_SENSE );
1131#endif
1132 if (!NCR5380_select(instance, tmp,
1133 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE :
1134 TAG_NEXT)) {
1135 falcon_dont_release--;
1136 /* release if target did not response! */
1137 falcon_release_lock_if_possible( hostdata );
1138 break;
1139 } else {
1140 local_irq_disable();
1141 LIST(tmp, hostdata->issue_queue);
1142 NEXT(tmp) = hostdata->issue_queue;
1143 hostdata->issue_queue = tmp;
1144#ifdef SUPPORT_TAGS
1145 cmd_free_tag( tmp );
1146#endif
1147 falcon_dont_release--;
1148 local_irq_restore(flags);
1149 MAIN_PRINTK("scsi%d: main(): select() failed, "
1150 "returned to issue_queue\n", HOSTNO);
1151 if (hostdata->connected)
1152 break;
1153 }
1154 } /* if target/lun/target queue is not busy */
1155 } /* for issue_queue */
1156 } /* if (!hostdata->connected) */
1157
1158 if (hostdata->connected
1159#ifdef REAL_DMA
1160 && !hostdata->dma_len
1161#endif
1162 ) {
1163 local_irq_restore(flags);
1164 MAIN_PRINTK("scsi%d: main: performing information transfer\n",
1165 HOSTNO);
1166 NCR5380_information_transfer(instance);
1167 MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO);
1168 done = 0;
1169 }
1170 } while (!done);
1171
1172 /* Better allow ints _after_ 'main_running' has been cleared, else
1173 an interrupt could believe we'll pick up the work it left for
1174 us, but we won't see it anymore here... */
1175 main_running = 0;
1176 local_irq_restore(flags);
1177}
1178
1179
1180#ifdef REAL_DMA
1181/*
1182 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
1183 *
1184 * Purpose : Called by interrupt handler when DMA finishes or a phase
1185 * mismatch occurs (which would finish the DMA transfer).
1186 *
1187 * Inputs : instance - this instance of the NCR5380.
1188 *
1189 */
1190
1191static void NCR5380_dma_complete( struct Scsi_Host *instance )
1192{
1193 SETUP_HOSTDATA(instance);
1194 int transfered, saved_data = 0, overrun = 0, cnt, toPIO;
1195 unsigned char **data, p;
1196 volatile int *count;
1197
1198 if (!hostdata->connected) {
1199 printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
1200 "no connected cmd\n", HOSTNO);
1201 return;
1202 }
1203
1204 if (atari_read_overruns) {
1205 p = hostdata->connected->SCp.phase;
1206 if (p & SR_IO) {
1207 udelay(10);
1208 if ((((NCR5380_read(BUS_AND_STATUS_REG)) &
1209 (BASR_PHASE_MATCH|BASR_ACK)) ==
1210 (BASR_PHASE_MATCH|BASR_ACK))) {
1211 saved_data = NCR5380_read(INPUT_DATA_REG);
1212 overrun = 1;
1213 DMA_PRINTK("scsi%d: read overrun handled\n", HOSTNO);
1214 }
1215 }
1216 }
1217
1218 DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
1219 HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
1220 NCR5380_read(STATUS_REG));
1221
1222 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1223 NCR5380_write(MODE_REG, MR_BASE);
1224 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1225
1226 transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
1227 hostdata->dma_len = 0;
1228
1229 data = (unsigned char **) &(hostdata->connected->SCp.ptr);
1230 count = &(hostdata->connected->SCp.this_residual);
1231 *data += transfered;
1232 *count -= transfered;
1233
1234 if (atari_read_overruns) {
1235 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1236 cnt = toPIO = atari_read_overruns;
1237 if (overrun) {
1238 DMA_PRINTK("Got an input overrun, using saved byte\n");
1239 *(*data)++ = saved_data;
1240 (*count)--;
1241 cnt--;
1242 toPIO--;
1243 }
1244 DMA_PRINTK("Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
1245 NCR5380_transfer_pio(instance, &p, &cnt, data);
1246 *count -= toPIO - cnt;
1247 }
1248 }
1249}
1250#endif /* REAL_DMA */
1251
1252
1253/*
1254 * Function : void NCR5380_intr (int irq)
1255 *
1256 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1257 * from the disconnected queue, and restarting NCR5380_main()
1258 * as required.
1259 *
1260 * Inputs : int irq, irq that caused this interrupt.
1261 *
1262 */
1263
1264static irqreturn_t NCR5380_intr (int irq, void *dev_id, struct pt_regs *regs)
1265{
1266 struct Scsi_Host *instance = first_instance;
1267 int done = 1, handled = 0;
1268 unsigned char basr;
1269
1270 INT_PRINTK("scsi%d: NCR5380 irq triggered\n", HOSTNO);
1271
1272 /* Look for pending interrupts */
1273 basr = NCR5380_read(BUS_AND_STATUS_REG);
1274 INT_PRINTK("scsi%d: BASR=%02x\n", HOSTNO, basr);
1275 /* dispatch to appropriate routine if found and done=0 */
1276 if (basr & BASR_IRQ) {
1277 NCR_PRINT(NDEBUG_INTR);
1278 if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
1279 done = 0;
1280 ENABLE_IRQ();
1281 INT_PRINTK("scsi%d: SEL interrupt\n", HOSTNO);
1282 NCR5380_reselect(instance);
1283 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1284 }
1285 else if (basr & BASR_PARITY_ERROR) {
1286 INT_PRINTK("scsi%d: PARITY interrupt\n", HOSTNO);
1287 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1288 }
1289 else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
1290 INT_PRINTK("scsi%d: RESET interrupt\n", HOSTNO);
1291 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1292 }
1293 else {
1294 /*
1295 * The rest of the interrupt conditions can occur only during a
1296 * DMA transfer
1297 */
1298
1299#if defined(REAL_DMA)
1300 /*
1301 * We should only get PHASE MISMATCH and EOP interrupts if we have
1302 * DMA enabled, so do a sanity check based on the current setting
1303 * of the MODE register.
1304 */
1305
1306 if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
1307 ((basr & BASR_END_DMA_TRANSFER) ||
1308 !(basr & BASR_PHASE_MATCH))) {
1309
1310 INT_PRINTK("scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
1311 NCR5380_dma_complete( instance );
1312 done = 0;
1313 ENABLE_IRQ();
1314 } else
1315#endif /* REAL_DMA */
1316 {
1317/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
1318 if (basr & BASR_PHASE_MATCH)
1319 printk(KERN_NOTICE "scsi%d: unknown interrupt, "
1320 "BASR 0x%x, MR 0x%x, SR 0x%x\n",
1321 HOSTNO, basr, NCR5380_read(MODE_REG),
1322 NCR5380_read(STATUS_REG));
1323 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1324 }
1325 } /* if !(SELECTION || PARITY) */
1326 handled = 1;
1327 } /* BASR & IRQ */
1328 else {
1329 printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
1330 "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
1331 NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
1332 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1333 }
1334
1335 if (!done) {
1336 INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO);
1337 /* Put a call to NCR5380_main() on the queue... */
1338 queue_main();
1339 }
1340 return IRQ_RETVAL(handled);
1341}
1342
1343#ifdef NCR5380_STATS
1344static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
1345{
1346# ifdef NCR5380_STAT_LIMIT
1347 if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
1348# endif
1349 switch (cmd->cmnd[0])
1350 {
1351 case WRITE:
1352 case WRITE_6:
1353 case WRITE_10:
1354 hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
1355 /*hostdata->bytes_write[cmd->device->id] += cmd->request_bufflen;*/
1356 hostdata->pendingw--;
1357 break;
1358 case READ:
1359 case READ_6:
1360 case READ_10:
1361 hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
1362 /*hostdata->bytes_read[cmd->device->id] += cmd->request_bufflen;*/
1363 hostdata->pendingr--;
1364 break;
1365 }
1366}
1367#endif
1368
1369/*
1370 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
1371 * int tag);
1372 *
1373 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1374 * including ARBITRATION, SELECTION, and initial message out for
1375 * IDENTIFY and queue messages.
1376 *
1377 * Inputs : instance - instantiation of the 5380 driver on which this
1378 * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
1379 * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
1380 * the command that is presently connected.
1381 *
1382 * Returns : -1 if selection could not execute for some reason,
1383 * 0 if selection succeeded or failed because the target
1384 * did not respond.
1385 *
1386 * Side effects :
1387 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1388 * with registers as they should have been on entry - ie
1389 * SELECT_ENABLE will be set appropriately, the NCR5380
1390 * will cease to drive any SCSI bus signals.
1391 *
1392 * If successful : I_T_L or I_T_L_Q nexus will be established,
1393 * instance->connected will be set to cmd.
1394 * SELECT interrupt will be disabled.
1395 *
1396 * If failed (no target) : cmd->scsi_done() will be called, and the
1397 * cmd->result host byte set to DID_BAD_TARGET.
1398 */
1399
1400static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
1401{
1402 SETUP_HOSTDATA(instance);
1403 unsigned char tmp[3], phase;
1404 unsigned char *data;
1405 int len;
1406 unsigned long timeout;
1407 unsigned long flags;
1408
1409 hostdata->restart_select = 0;
1410 NCR_PRINT(NDEBUG_ARBITRATION);
1411 ARB_PRINTK("scsi%d: starting arbitration, id = %d\n", HOSTNO,
1412 instance->this_id);
1413
1414 /*
1415 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1416 * data bus during SELECTION.
1417 */
1418
1419 local_irq_save(flags);
1420 if (hostdata->connected) {
1421 local_irq_restore(flags);
1422 return -1;
1423 }
1424 NCR5380_write(TARGET_COMMAND_REG, 0);
1425
1426
1427 /*
1428 * Start arbitration.
1429 */
1430
1431 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1432 NCR5380_write(MODE_REG, MR_ARBITRATE);
1433
1434 local_irq_restore(flags);
1435
1436 /* Wait for arbitration logic to complete */
1437#if NCR_TIMEOUT
1438 {
1439 unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
1440
1441 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1442 && time_before(jiffies, timeout) && !hostdata->connected)
1443 ;
1444 if (time_after_eq(jiffies, timeout))
1445 {
1446 printk("scsi : arbitration timeout at %d\n", __LINE__);
1447 NCR5380_write(MODE_REG, MR_BASE);
1448 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1449 return -1;
1450 }
1451 }
1452#else /* NCR_TIMEOUT */
1453 while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
1454 && !hostdata->connected);
1455#endif
1456
1457 ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO);
1458
1459 if (hostdata->connected) {
1460 NCR5380_write(MODE_REG, MR_BASE);
1461 return -1;
1462 }
1463 /*
1464 * The arbitration delay is 2.2us, but this is a minimum and there is
1465 * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
1466 * the integral nature of udelay().
1467 *
1468 */
1469
1470 udelay(3);
1471
1472 /* Check for lost arbitration */
1473 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1474 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1475 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1476 hostdata->connected) {
1477 NCR5380_write(MODE_REG, MR_BASE);
1478 ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
1479 HOSTNO);
1480 return -1;
1481 }
1482
1483 /* after/during arbitration, BSY should be asserted.
1484 IBM DPES-31080 Version S31Q works now */
1485 /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
1486 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL |
1487 ICR_ASSERT_BSY ) ;
1488
1489 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1490 hostdata->connected) {
1491 NCR5380_write(MODE_REG, MR_BASE);
1492 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1493 ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
1494 HOSTNO);
1495 return -1;
1496 }
1497
1498 /*
1499 * Again, bus clear + bus settle time is 1.2us, however, this is
1500 * a minimum so we'll udelay ceil(1.2)
1501 */
1502
1503#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
1504 /* ++roman: But some targets (see above :-) seem to need a bit more... */
1505 udelay(15);
1506#else
1507 udelay(2);
1508#endif
1509
1510 if (hostdata->connected) {
1511 NCR5380_write(MODE_REG, MR_BASE);
1512 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1513 return -1;
1514 }
1515
1516 ARB_PRINTK("scsi%d: won arbitration\n", HOSTNO);
1517
1518 /*
1519 * Now that we have won arbitration, start Selection process, asserting
1520 * the host and target ID's on the SCSI bus.
1521 */
1522
1523 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));
1524
1525 /*
1526 * Raise ATN while SEL is true before BSY goes false from arbitration,
1527 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1528 * phase immediately after selection.
1529 */
1530
1531 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY |
1532 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
1533 NCR5380_write(MODE_REG, MR_BASE);
1534
1535 /*
1536 * Reselect interrupts must be turned off prior to the dropping of BSY,
1537 * otherwise we will trigger an interrupt.
1538 */
1539
1540 if (hostdata->connected) {
1541 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1542 return -1;
1543 }
1544
1545 NCR5380_write(SELECT_ENABLE_REG, 0);
1546
1547 /*
1548 * The initiator shall then wait at least two deskew delays and release
1549 * the BSY signal.
1550 */
1551 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1552
1553 /* Reset BSY */
1554 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA |
1555 ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1556
1557 /*
1558 * Something weird happens when we cease to drive BSY - looks
1559 * like the board/chip is letting us do another read before the
1560 * appropriate propagation delay has expired, and we're confusing
1561 * a BSY signal from ourselves as the target's response to SELECTION.
1562 *
1563 * A small delay (the 'C++' frontend breaks the pipeline with an
1564 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1565 * tighter 'C' code breaks and requires this) solves the problem -
1566 * the 1 us delay is arbitrary, and only used because this delay will
1567 * be the same on other platforms and since it works here, it should
1568 * work there.
1569 *
1570 * wingel suggests that this could be due to failing to wait
1571 * one deskew delay.
1572 */
1573
1574 udelay(1);
1575
1576 SEL_PRINTK("scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);
1577
1578 /*
1579 * The SCSI specification calls for a 250 ms timeout for the actual
1580 * selection.
1581 */
1582
1583 timeout = jiffies + 25;
1584
1585 /*
1586 * XXX very interesting - we're seeing a bounce where the BSY we
1587 * asserted is being reflected / still asserted (propagation delay?)
1588 * and it's detecting as true. Sigh.
1589 */
1590
1591#if 0
1592 /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
1593 * IO while SEL is true. But again, there are some disks out the in the
1594 * world that do that nevertheless. (Somebody claimed that this announces
1595 * reselection capability of the target.) So we better skip that test and
1596 * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
1597 */
1598
1599 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) &
1600 (SR_BSY | SR_IO)));
1601
1602 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) ==
1603 (SR_SEL | SR_IO)) {
1604 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1605 NCR5380_reselect(instance);
1606 printk (KERN_ERR "scsi%d: reselection after won arbitration?\n",
1607 HOSTNO);
1608 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1609 return -1;
1610 }
1611#else
1612 while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
1613#endif
1614
1615 /*
1616 * No less than two deskew delays after the initiator detects the
1617 * BSY signal is true, it shall release the SEL signal and may
1618 * change the DATA BUS. -wingel
1619 */
1620
1621 udelay(1);
1622
1623 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1624
1625 if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
1626 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1627 if (hostdata->targets_present & (1 << cmd->device->id)) {
1628 printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
1629 if (hostdata->restart_select)
1630 printk(KERN_NOTICE "\trestart select\n");
1631 NCR_PRINT(NDEBUG_ANY);
1632 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1633 return -1;
1634 }
1635 cmd->result = DID_BAD_TARGET << 16;
1636#ifdef NCR5380_STATS
1637 collect_stats(hostdata, cmd);
1638#endif
1639#ifdef SUPPORT_TAGS
1640 cmd_free_tag( cmd );
1641#endif
1642 cmd->scsi_done(cmd);
1643 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1644 SEL_PRINTK("scsi%d: target did not respond within 250ms\n", HOSTNO);
1645 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1646 return 0;
1647 }
1648
1649 hostdata->targets_present |= (1 << cmd->device->id);
1650
1651 /*
1652 * Since we followed the SCSI spec, and raised ATN while SEL
1653 * was true but before BSY was false during selection, the information
1654 * transfer phase should be a MESSAGE OUT phase so that we can send the
1655 * IDENTIFY message.
1656 *
1657 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1658 * message (2 bytes) with a tag ID that we increment with every command
1659 * until it wraps back to 0.
1660 *
1661 * XXX - it turns out that there are some broken SCSI-II devices,
1662 * which claim to support tagged queuing but fail when more than
1663 * some number of commands are issued at once.
1664 */
1665
1666 /* Wait for start of REQ/ACK handshake */
1667 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
1668
1669 SEL_PRINTK("scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
1670 HOSTNO, cmd->device->id);
1671 tmp[0] = IDENTIFY(1, cmd->device->lun);
1672
1673#ifdef SUPPORT_TAGS
1674 if (cmd->tag != TAG_NONE) {
1675 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1676 tmp[2] = cmd->tag;
1677 len = 3;
1678 } else
1679 len = 1;
1680#else
1681 len = 1;
1682 cmd->tag=0;
1683#endif /* SUPPORT_TAGS */
1684
1685 /* Send message(s) */
1686 data = tmp;
1687 phase = PHASE_MSGOUT;
1688 NCR5380_transfer_pio(instance, &phase, &len, &data);
1689 SEL_PRINTK("scsi%d: nexus established.\n", HOSTNO);
1690 /* XXX need to handle errors here */
1691 hostdata->connected = cmd;
1692#ifndef SUPPORT_TAGS
1693 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
1694#endif
1695
1696 initialize_SCp(cmd);
1697
1698
1699 return 0;
1700}
1701
1702/*
1703 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1704 * unsigned char *phase, int *count, unsigned char **data)
1705 *
1706 * Purpose : transfers data in given phase using polled I/O
1707 *
1708 * Inputs : instance - instance of driver, *phase - pointer to
1709 * what phase is expected, *count - pointer to number of
1710 * bytes to transfer, **data - pointer to data pointer.
1711 *
1712 * Returns : -1 when different phase is entered without transferring
1713 * maximum number of bytes, 0 if all bytes are transfered or exit
1714 * is in same phase.
1715 *
1716 * Also, *phase, *count, *data are modified in place.
1717 *
1718 * XXX Note : handling for bus free may be useful.
1719 */
1720
1721/*
1722 * Note : this code is not as quick as it could be, however it
1723 * IS 100% reliable, and for the actual data transfer where speed
1724 * counts, we will always do a pseudo DMA or DMA transfer.
1725 */
1726
1727static int NCR5380_transfer_pio( struct Scsi_Host *instance,
1728 unsigned char *phase, int *count,
1729 unsigned char **data)
1730{
1731 register unsigned char p = *phase, tmp;
1732 register int c = *count;
1733 register unsigned char *d = *data;
1734
1735 /*
1736 * The NCR5380 chip will only drive the SCSI bus when the
1737 * phase specified in the appropriate bits of the TARGET COMMAND
1738 * REGISTER match the STATUS REGISTER
1739 */
1740
1741 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1742
1743 do {
1744 /*
1745 * Wait for assertion of REQ, after which the phase bits will be
1746 * valid
1747 */
1748 while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
1749
1750 HSH_PRINTK("scsi%d: REQ detected\n", HOSTNO);
1751
1752 /* Check for phase mismatch */
1753 if ((tmp & PHASE_MASK) != p) {
1754 PIO_PRINTK("scsi%d: phase mismatch\n", HOSTNO);
1755 NCR_PRINT_PHASE(NDEBUG_PIO);
1756 break;
1757 }
1758
1759 /* Do actual transfer from SCSI bus to / from memory */
1760 if (!(p & SR_IO))
1761 NCR5380_write(OUTPUT_DATA_REG, *d);
1762 else
1763 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1764
1765 ++d;
1766
1767 /*
1768 * The SCSI standard suggests that in MSGOUT phase, the initiator
1769 * should drop ATN on the last byte of the message phase
1770 * after REQ has been asserted for the handshake but before
1771 * the initiator raises ACK.
1772 */
1773
1774 if (!(p & SR_IO)) {
1775 if (!((p & SR_MSG) && c > 1)) {
1776 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1777 ICR_ASSERT_DATA);
1778 NCR_PRINT(NDEBUG_PIO);
1779 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1780 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1781 } else {
1782 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1783 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1784 NCR_PRINT(NDEBUG_PIO);
1785 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1786 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1787 }
1788 } else {
1789 NCR_PRINT(NDEBUG_PIO);
1790 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1791 }
1792
1793 while (NCR5380_read(STATUS_REG) & SR_REQ);
1794
1795 HSH_PRINTK("scsi%d: req false, handshake complete\n", HOSTNO);
1796
1797/*
1798 * We have several special cases to consider during REQ/ACK handshaking :
1799 * 1. We were in MSGOUT phase, and we are on the last byte of the
1800 * message. ATN must be dropped as ACK is dropped.
1801 *
1802 * 2. We are in a MSGIN phase, and we are on the last byte of the
1803 * message. We must exit with ACK asserted, so that the calling
1804 * code may raise ATN before dropping ACK to reject the message.
1805 *
1806 * 3. ACK and ATN are clear and the target may proceed as normal.
1807 */
1808 if (!(p == PHASE_MSGIN && c == 1)) {
1809 if (p == PHASE_MSGOUT && c > 1)
1810 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1811 else
1812 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1813 }
1814 } while (--c);
1815
1816 PIO_PRINTK("scsi%d: residual %d\n", HOSTNO, c);
1817
1818 *count = c;
1819 *data = d;
1820 tmp = NCR5380_read(STATUS_REG);
1821 /* The phase read from the bus is valid if either REQ is (already)
1822 * asserted or if ACK hasn't been released yet. The latter is the case if
1823 * we're in MSGIN and all wanted bytes have been received. */
1824 if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
1825 *phase = tmp & PHASE_MASK;
1826 else
1827 *phase = PHASE_UNKNOWN;
1828
1829 if (!c || (*phase == p))
1830 return 0;
1831 else
1832 return -1;
1833}
1834
1835/*
1836 * Function : do_abort (Scsi_Host *host)
1837 *
1838 * Purpose : abort the currently established nexus. Should only be
1839 * called from a routine which can drop into a
1840 *
1841 * Returns : 0 on success, -1 on failure.
1842 */
1843
1844static int do_abort (struct Scsi_Host *host)
1845{
1846 unsigned char tmp, *msgptr, phase;
1847 int len;
1848
1849 /* Request message out phase */
1850 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1851
1852 /*
1853 * Wait for the target to indicate a valid phase by asserting
1854 * REQ. Once this happens, we'll have either a MSGOUT phase
1855 * and can immediately send the ABORT message, or we'll have some
1856 * other phase and will have to source/sink data.
1857 *
1858 * We really don't care what value was on the bus or what value
1859 * the target sees, so we just handshake.
1860 */
1861
1862 while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
1863
1864 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1865
1866 if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
1867 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1868 ICR_ASSERT_ACK);
1869 while (NCR5380_read(STATUS_REG) & SR_REQ);
1870 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1871 }
1872
1873 tmp = ABORT;
1874 msgptr = &tmp;
1875 len = 1;
1876 phase = PHASE_MSGOUT;
1877 NCR5380_transfer_pio (host, &phase, &len, &msgptr);
1878
1879 /*
1880 * If we got here, and the command completed successfully,
1881 * we're about to go into bus free state.
1882 */
1883
1884 return len ? -1 : 0;
1885}
1886
1887#if defined(REAL_DMA)
1888/*
1889 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1890 * unsigned char *phase, int *count, unsigned char **data)
1891 *
1892 * Purpose : transfers data in given phase using either real
1893 * or pseudo DMA.
1894 *
1895 * Inputs : instance - instance of driver, *phase - pointer to
1896 * what phase is expected, *count - pointer to number of
1897 * bytes to transfer, **data - pointer to data pointer.
1898 *
1899 * Returns : -1 when different phase is entered without transferring
1900 * maximum number of bytes, 0 if all bytes or transfered or exit
1901 * is in same phase.
1902 *
1903 * Also, *phase, *count, *data are modified in place.
1904 *
1905 */
1906
1907
1908static int NCR5380_transfer_dma( struct Scsi_Host *instance,
1909 unsigned char *phase, int *count,
1910 unsigned char **data)
1911{
1912 SETUP_HOSTDATA(instance);
1913 register int c = *count;
1914 register unsigned char p = *phase;
1915 register unsigned char *d = *data;
1916 unsigned char tmp;
1917 unsigned long flags;
1918
1919 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1920 *phase = tmp;
1921 return -1;
1922 }
1923
1924 if (atari_read_overruns && (p & SR_IO)) {
1925 c -= atari_read_overruns;
1926 }
1927
1928 DMA_PRINTK("scsi%d: initializing DMA for %s, %d bytes %s %p\n",
1929 HOSTNO, (p & SR_IO) ? "reading" : "writing",
1930 c, (p & SR_IO) ? "to" : "from", d);
1931
1932 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1933
1934#ifdef REAL_DMA
1935 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
1936#endif /* def REAL_DMA */
1937
1938 if (IS_A_TT()) {
1939 /* On the Medusa, it is a must to initialize the DMA before
1940 * starting the NCR. This is also the cleaner way for the TT.
1941 */
1942 local_irq_save(flags);
1943 hostdata->dma_len = (p & SR_IO) ?
1944 NCR5380_dma_read_setup(instance, d, c) :
1945 NCR5380_dma_write_setup(instance, d, c);
1946 local_irq_restore(flags);
1947 }
1948
1949 if (p & SR_IO)
1950 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1951 else {
1952 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1953 NCR5380_write(START_DMA_SEND_REG, 0);
1954 }
1955
1956 if (!IS_A_TT()) {
1957 /* On the Falcon, the DMA setup must be done after the last */
1958 /* NCR access, else the DMA setup gets trashed!
1959 */
1960 local_irq_save(flags);
1961 hostdata->dma_len = (p & SR_IO) ?
1962 NCR5380_dma_read_setup(instance, d, c) :
1963 NCR5380_dma_write_setup(instance, d, c);
1964 local_irq_restore(flags);
1965 }
1966 return 0;
1967}
1968#endif /* defined(REAL_DMA) */
1969
1970/*
1971 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1972 *
1973 * Purpose : run through the various SCSI phases and do as the target
1974 * directs us to. Operates on the currently connected command,
1975 * instance->connected.
1976 *
1977 * Inputs : instance, instance for which we are doing commands
1978 *
1979 * Side effects : SCSI things happen, the disconnected queue will be
1980 * modified if a command disconnects, *instance->connected will
1981 * change.
1982 *
1983 * XXX Note : we need to watch for bus free or a reset condition here
1984 * to recover from an unexpected bus free condition.
1985 */
1986
1987static void NCR5380_information_transfer (struct Scsi_Host *instance)
1988{
1989 SETUP_HOSTDATA(instance);
1990 unsigned long flags;
1991 unsigned char msgout = NOP;
1992 int sink = 0;
1993 int len;
1994#if defined(REAL_DMA)
1995 int transfersize;
1996#endif
1997 unsigned char *data;
1998 unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
1999 Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
2000
2001 while (1) {
2002 tmp = NCR5380_read(STATUS_REG);
2003 /* We only have a valid SCSI phase when REQ is asserted */
2004 if (tmp & SR_REQ) {
2005 phase = (tmp & PHASE_MASK);
2006 if (phase != old_phase) {
2007 old_phase = phase;
2008 NCR_PRINT_PHASE(NDEBUG_INFORMATION);
2009 }
2010
2011 if (sink && (phase != PHASE_MSGOUT)) {
2012 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
2013
2014 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
2015 ICR_ASSERT_ACK);
2016 while (NCR5380_read(STATUS_REG) & SR_REQ);
2017 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2018 ICR_ASSERT_ATN);
2019 sink = 0;
2020 continue;
2021 }
2022
2023 switch (phase) {
2024 case PHASE_DATAOUT:
2025#if (NDEBUG & NDEBUG_NO_DATAOUT)
2026 printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
2027 "aborted\n", HOSTNO);
2028 sink = 1;
2029 do_abort(instance);
2030 cmd->result = DID_ERROR << 16;
2031 cmd->done(cmd);
2032 return;
2033#endif
2034 case PHASE_DATAIN:
2035 /*
2036 * If there is no room left in the current buffer in the
2037 * scatter-gather list, move onto the next one.
2038 */
2039
2040 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
2041 ++cmd->SCp.buffer;
2042 --cmd->SCp.buffers_residual;
2043 cmd->SCp.this_residual = cmd->SCp.buffer->length;
2044 cmd->SCp.ptr = page_address(cmd->SCp.buffer->page)+
2045 cmd->SCp.buffer->offset;
2046 /* ++roman: Try to merge some scatter-buffers if
2047 * they are at contiguous physical addresses.
2048 */
2049 merge_contiguous_buffers( cmd );
2050 INF_PRINTK("scsi%d: %d bytes and %d buffers left\n",
2051 HOSTNO, cmd->SCp.this_residual,
2052 cmd->SCp.buffers_residual);
2053 }
2054
2055 /*
2056 * The preferred transfer method is going to be
2057 * PSEUDO-DMA for systems that are strictly PIO,
2058 * since we can let the hardware do the handshaking.
2059 *
2060 * For this to work, we need to know the transfersize
2061 * ahead of time, since the pseudo-DMA code will sit
2062 * in an unconditional loop.
2063 */
2064
2065/* ++roman: I suggest, this should be
2066 * #if def(REAL_DMA)
2067 * instead of leaving REAL_DMA out.
2068 */
2069
2070#if defined(REAL_DMA)
2071 if (!cmd->device->borken &&
2072 (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
2073 len = transfersize;
2074 cmd->SCp.phase = phase;
2075 if (NCR5380_transfer_dma(instance, &phase,
2076 &len, (unsigned char **) &cmd->SCp.ptr)) {
2077 /*
2078 * If the watchdog timer fires, all future
2079 * accesses to this device will use the
2080 * polled-IO. */
2081 printk(KERN_NOTICE "scsi%d: switching target %d "
2082 "lun %d to slow handshake\n", HOSTNO,
2083 cmd->device->id, cmd->device->lun);
2084 cmd->device->borken = 1;
2085 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2086 ICR_ASSERT_ATN);
2087 sink = 1;
2088 do_abort(instance);
2089 cmd->result = DID_ERROR << 16;
2090 cmd->done(cmd);
2091 /* XXX - need to source or sink data here, as appropriate */
2092 } else {
2093#ifdef REAL_DMA
2094 /* ++roman: When using real DMA,
2095 * information_transfer() should return after
2096 * starting DMA since it has nothing more to
2097 * do.
2098 */
2099 return;
2100#else
2101 cmd->SCp.this_residual -= transfersize - len;
2102#endif
2103 }
2104 } else
2105#endif /* defined(REAL_DMA) */
2106 NCR5380_transfer_pio(instance, &phase,
2107 (int *) &cmd->SCp.this_residual, (unsigned char **)
2108 &cmd->SCp.ptr);
2109 break;
2110 case PHASE_MSGIN:
2111 len = 1;
2112 data = &tmp;
2113 NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */
2114 NCR5380_transfer_pio(instance, &phase, &len, &data);
2115 cmd->SCp.Message = tmp;
2116
2117 switch (tmp) {
2118 /*
2119 * Linking lets us reduce the time required to get the
2120 * next command out to the device, hopefully this will
2121 * mean we don't waste another revolution due to the delays
2122 * required by ARBITRATION and another SELECTION.
2123 *
2124 * In the current implementation proposal, low level drivers
2125 * merely have to start the next command, pointed to by
2126 * next_link, done() is called as with unlinked commands.
2127 */
2128#ifdef LINKED
2129 case LINKED_CMD_COMPLETE:
2130 case LINKED_FLG_CMD_COMPLETE:
2131 /* Accept message by clearing ACK */
2132 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2133
2134 LNK_PRINTK("scsi%d: target %d lun %d linked command "
2135 "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun);
2136
2137 /* Enable reselect interrupts */
2138 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2139 /*
2140 * Sanity check : A linked command should only terminate
2141 * with one of these messages if there are more linked
2142 * commands available.
2143 */
2144
2145 if (!cmd->next_link) {
2146 printk(KERN_NOTICE "scsi%d: target %d lun %d "
2147 "linked command complete, no next_link\n",
2148 HOSTNO, cmd->device->id, cmd->device->lun);
2149 sink = 1;
2150 do_abort (instance);
2151 return;
2152 }
2153
2154 initialize_SCp(cmd->next_link);
2155 /* The next command is still part of this process; copy it
2156 * and don't free it! */
2157 cmd->next_link->tag = cmd->tag;
2158 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2159 LNK_PRINTK("scsi%d: target %d lun %d linked request "
2160 "done, calling scsi_done().\n",
2161 HOSTNO, cmd->device->id, cmd->device->lun);
2162#ifdef NCR5380_STATS
2163 collect_stats(hostdata, cmd);
2164#endif
2165 cmd->scsi_done(cmd);
2166 cmd = hostdata->connected;
2167 break;
2168#endif /* def LINKED */
2169 case ABORT:
2170 case COMMAND_COMPLETE:
2171 /* Accept message by clearing ACK */
2172 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2173 /* ++guenther: possible race with Falcon locking */
2174 falcon_dont_release++;
2175 hostdata->connected = NULL;
2176 QU_PRINTK("scsi%d: command for target %d, lun %d "
2177 "completed\n", HOSTNO, cmd->device->id, cmd->device->lun);
2178#ifdef SUPPORT_TAGS
2179 cmd_free_tag( cmd );
2180 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
2181 /* Turn a QUEUE FULL status into BUSY, I think the
2182 * mid level cannot handle QUEUE FULL :-( (The
2183 * command is retried after BUSY). Also update our
2184 * queue size to the number of currently issued
2185 * commands now.
2186 */
2187 /* ++Andreas: the mid level code knows about
2188 QUEUE_FULL now. */
2189 TAG_ALLOC *ta = &TagAlloc[cmd->device->id][cmd->device->lun];
2190 TAG_PRINTK("scsi%d: target %d lun %d returned "
2191 "QUEUE_FULL after %d commands\n",
2192 HOSTNO, cmd->device->id, cmd->device->lun,
2193 ta->nr_allocated);
2194 if (ta->queue_size > ta->nr_allocated)
2195 ta->nr_allocated = ta->queue_size;
2196 }
2197#else
2198 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2199#endif
2200 /* Enable reselect interrupts */
2201 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2202
2203 /*
2204 * I'm not sure what the correct thing to do here is :
2205 *
2206 * If the command that just executed is NOT a request
2207 * sense, the obvious thing to do is to set the result
2208 * code to the values of the stored parameters.
2209 *
2210 * If it was a REQUEST SENSE command, we need some way to
2211 * differentiate between the failure code of the original
2212 * and the failure code of the REQUEST sense - the obvious
2213 * case is success, where we fall through and leave the
2214 * result code unchanged.
2215 *
2216 * The non-obvious place is where the REQUEST SENSE failed
2217 */
2218
2219 if (cmd->cmnd[0] != REQUEST_SENSE)
2220 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
2221 else if (status_byte(cmd->SCp.Status) != GOOD)
2222 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
2223
2224#ifdef AUTOSENSE
2225 if ((cmd->cmnd[0] != REQUEST_SENSE) &&
2226 (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
2227 ASEN_PRINTK("scsi%d: performing request sense\n",
2228 HOSTNO);
2229 cmd->cmnd[0] = REQUEST_SENSE;
2230 cmd->cmnd[1] &= 0xe0;
2231 cmd->cmnd[2] = 0;
2232 cmd->cmnd[3] = 0;
2233 cmd->cmnd[4] = sizeof(cmd->sense_buffer);
2234 cmd->cmnd[5] = 0;
2235 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
2236
2237 cmd->use_sg = 0;
2238 /* this is initialized from initialize_SCp
2239 cmd->SCp.buffer = NULL;
2240 cmd->SCp.buffers_residual = 0;
2241 */
2242 cmd->request_buffer = (char *) cmd->sense_buffer;
2243 cmd->request_bufflen = sizeof(cmd->sense_buffer);
2244
2245 local_irq_save(flags);
2246 LIST(cmd,hostdata->issue_queue);
2247 NEXT(cmd) = hostdata->issue_queue;
2248 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
2249 local_irq_restore(flags);
2250 QU_PRINTK("scsi%d: REQUEST SENSE added to head of "
2251 "issue queue\n", H_NO(cmd));
2252 } else
2253#endif /* def AUTOSENSE */
2254 {
2255#ifdef NCR5380_STATS
2256 collect_stats(hostdata, cmd);
2257#endif
2258 cmd->scsi_done(cmd);
2259 }
2260
2261 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2262 /*
2263 * Restore phase bits to 0 so an interrupted selection,
2264 * arbitration can resume.
2265 */
2266 NCR5380_write(TARGET_COMMAND_REG, 0);
2267
2268 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2269 barrier();
2270
2271 falcon_dont_release--;
2272 /* ++roman: For Falcon SCSI, release the lock on the
2273 * ST-DMA here if no other commands are waiting on the
2274 * disconnected queue.
2275 */
2276 falcon_release_lock_if_possible( hostdata );
2277 return;
2278 case MESSAGE_REJECT:
2279 /* Accept message by clearing ACK */
2280 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2281 /* Enable reselect interrupts */
2282 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2283 switch (hostdata->last_message) {
2284 case HEAD_OF_QUEUE_TAG:
2285 case ORDERED_QUEUE_TAG:
2286 case SIMPLE_QUEUE_TAG:
2287 /* The target obviously doesn't support tagged
2288 * queuing, even though it announced this ability in
2289 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2290 * clear 'tagged_supported' and lock the LUN, since
2291 * the command is treated as untagged further on.
2292 */
2293 cmd->device->tagged_supported = 0;
2294 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2295 cmd->tag = TAG_NONE;
2296 TAG_PRINTK("scsi%d: target %d lun %d rejected "
2297 "QUEUE_TAG message; tagged queuing "
2298 "disabled\n",
2299 HOSTNO, cmd->device->id, cmd->device->lun);
2300 break;
2301 }
2302 break;
2303 case DISCONNECT:
2304 /* Accept message by clearing ACK */
2305 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2306 local_irq_save(flags);
2307 cmd->device->disconnect = 1;
2308 LIST(cmd,hostdata->disconnected_queue);
2309 NEXT(cmd) = hostdata->disconnected_queue;
2310 hostdata->connected = NULL;
2311 hostdata->disconnected_queue = cmd;
2312 local_irq_restore(flags);
2313 QU_PRINTK("scsi%d: command for target %d lun %d was "
2314 "moved from connected to the "
2315 "disconnected_queue\n", HOSTNO,
2316 cmd->device->id, cmd->device->lun);
2317 /*
2318 * Restore phase bits to 0 so an interrupted selection,
2319 * arbitration can resume.
2320 */
2321 NCR5380_write(TARGET_COMMAND_REG, 0);
2322
2323 /* Enable reselect interrupts */
2324 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2325 /* Wait for bus free to avoid nasty timeouts */
2326 while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
2327 barrier();
2328 return;
2329 /*
2330 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2331 * operation, in violation of the SCSI spec so we can safely
2332 * ignore SAVE/RESTORE pointers calls.
2333 *
2334 * Unfortunately, some disks violate the SCSI spec and
2335 * don't issue the required SAVE_POINTERS message before
2336 * disconnecting, and we have to break spec to remain
2337 * compatible.
2338 */
2339 case SAVE_POINTERS:
2340 case RESTORE_POINTERS:
2341 /* Accept message by clearing ACK */
2342 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2343 /* Enable reselect interrupts */
2344 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2345 break;
2346 case EXTENDED_MESSAGE:
2347/*
2348 * Extended messages are sent in the following format :
2349 * Byte
2350 * 0 EXTENDED_MESSAGE == 1
2351 * 1 length (includes one byte for code, doesn't
2352 * include first two bytes)
2353 * 2 code
2354 * 3..length+1 arguments
2355 *
2356 * Start the extended message buffer with the EXTENDED_MESSAGE
2357 * byte, since print_msg() wants the whole thing.
2358 */
2359 extended_msg[0] = EXTENDED_MESSAGE;
2360 /* Accept first byte by clearing ACK */
2361 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2362
2363 EXT_PRINTK("scsi%d: receiving extended message\n", HOSTNO);
2364
2365 len = 2;
2366 data = extended_msg + 1;
2367 phase = PHASE_MSGIN;
2368 NCR5380_transfer_pio(instance, &phase, &len, &data);
2369 EXT_PRINTK("scsi%d: length=%d, code=0x%02x\n", HOSTNO,
2370 (int)extended_msg[1], (int)extended_msg[2]);
2371
2372 if (!len && extended_msg[1] <=
2373 (sizeof (extended_msg) - 1)) {
2374 /* Accept third byte by clearing ACK */
2375 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2376 len = extended_msg[1] - 1;
2377 data = extended_msg + 3;
2378 phase = PHASE_MSGIN;
2379
2380 NCR5380_transfer_pio(instance, &phase, &len, &data);
2381 EXT_PRINTK("scsi%d: message received, residual %d\n",
2382 HOSTNO, len);
2383
2384 switch (extended_msg[2]) {
2385 case EXTENDED_SDTR:
2386 case EXTENDED_WDTR:
2387 case EXTENDED_MODIFY_DATA_POINTER:
2388 case EXTENDED_EXTENDED_IDENTIFY:
2389 tmp = 0;
2390 }
2391 } else if (len) {
2392 printk(KERN_NOTICE "scsi%d: error receiving "
2393 "extended message\n", HOSTNO);
2394 tmp = 0;
2395 } else {
2396 printk(KERN_NOTICE "scsi%d: extended message "
2397 "code %02x length %d is too long\n",
2398 HOSTNO, extended_msg[2], extended_msg[1]);
2399 tmp = 0;
2400 }
2401 /* Fall through to reject message */
2402
2403 /*
2404 * If we get something weird that we aren't expecting,
2405 * reject it.
2406 */
2407 default:
2408 if (!tmp) {
2409 printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
2410 print_msg (extended_msg);
2411 printk("\n");
2412 } else if (tmp != EXTENDED_MESSAGE)
2413 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2414 "message %02x from target %d, lun %d\n",
2415 HOSTNO, tmp, cmd->device->id, cmd->device->lun);
2416 else
2417 printk(KERN_DEBUG "scsi%d: rejecting unknown "
2418 "extended message "
2419 "code %02x, length %d from target %d, lun %d\n",
2420 HOSTNO, extended_msg[1], extended_msg[0],
2421 cmd->device->id, cmd->device->lun);
2422
2423
2424 msgout = MESSAGE_REJECT;
2425 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
2426 ICR_ASSERT_ATN);
2427 break;
2428 } /* switch (tmp) */
2429 break;
2430 case PHASE_MSGOUT:
2431 len = 1;
2432 data = &msgout;
2433 hostdata->last_message = msgout;
2434 NCR5380_transfer_pio(instance, &phase, &len, &data);
2435 if (msgout == ABORT) {
2436#ifdef SUPPORT_TAGS
2437 cmd_free_tag( cmd );
2438#else
2439 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2440#endif
2441 hostdata->connected = NULL;
2442 cmd->result = DID_ERROR << 16;
2443#ifdef NCR5380_STATS
2444 collect_stats(hostdata, cmd);
2445#endif
2446 cmd->scsi_done(cmd);
2447 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2448 falcon_release_lock_if_possible( hostdata );
2449 return;
2450 }
2451 msgout = NOP;
2452 break;
2453 case PHASE_CMDOUT:
2454 len = cmd->cmd_len;
2455 data = cmd->cmnd;
2456 /*
2457 * XXX for performance reasons, on machines with a
2458 * PSEUDO-DMA architecture we should probably
2459 * use the dma transfer function.
2460 */
2461 NCR5380_transfer_pio(instance, &phase, &len,
2462 &data);
2463 break;
2464 case PHASE_STATIN:
2465 len = 1;
2466 data = &tmp;
2467 NCR5380_transfer_pio(instance, &phase, &len, &data);
2468 cmd->SCp.Status = tmp;
2469 break;
2470 default:
2471 printk("scsi%d: unknown phase\n", HOSTNO);
2472 NCR_PRINT(NDEBUG_ANY);
2473 } /* switch(phase) */
2474 } /* if (tmp * SR_REQ) */
2475 } /* while (1) */
2476}
2477
2478/*
2479 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2480 *
2481 * Purpose : does reselection, initializing the instance->connected
2482 * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
2483 * nexus has been reestablished,
2484 *
2485 * Inputs : instance - this instance of the NCR5380.
2486 *
2487 */
2488
2489
2490static void NCR5380_reselect (struct Scsi_Host *instance)
2491{
2492 SETUP_HOSTDATA(instance);
2493 unsigned char target_mask;
2494 unsigned char lun, phase;
2495 int len;
2496#ifdef SUPPORT_TAGS
2497 unsigned char tag;
2498#endif
2499 unsigned char msg[3];
2500 unsigned char *data;
2501 Scsi_Cmnd *tmp = NULL, *prev;
2502/* unsigned long flags; */
2503
2504 /*
2505 * Disable arbitration, etc. since the host adapter obviously
2506 * lost, and tell an interrupted NCR5380_select() to restart.
2507 */
2508
2509 NCR5380_write(MODE_REG, MR_BASE);
2510 hostdata->restart_select = 1;
2511
2512 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2513
2514 RSL_PRINTK("scsi%d: reselect\n", HOSTNO);
2515
2516 /*
2517 * At this point, we have detected that our SCSI ID is on the bus,
2518 * SEL is true and BSY was false for at least one bus settle delay
2519 * (400 ns).
2520 *
2521 * We must assert BSY ourselves, until the target drops the SEL
2522 * signal.
2523 */
2524
2525 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2526
2527 while (NCR5380_read(STATUS_REG) & SR_SEL);
2528 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2529
2530 /*
2531 * Wait for target to go into MSGIN.
2532 */
2533
2534 while (!(NCR5380_read(STATUS_REG) & SR_REQ));
2535
2536 len = 1;
2537 data = msg;
2538 phase = PHASE_MSGIN;
2539 NCR5380_transfer_pio(instance, &phase, &len, &data);
2540
2541 if (!(msg[0] & 0x80)) {
2542 printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
2543 print_msg(msg);
2544 do_abort(instance);
2545 return;
2546 }
2547 lun = (msg[0] & 0x07);
2548
2549#ifdef SUPPORT_TAGS
2550 /* If the phase is still MSGIN, the target wants to send some more
2551 * messages. In case it supports tagged queuing, this is probably a
2552 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2553 */
2554 tag = TAG_NONE;
2555 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2556 /* Accept previous IDENTIFY message by clearing ACK */
2557 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
2558 len = 2;
2559 data = msg+1;
2560 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2561 msg[1] == SIMPLE_QUEUE_TAG)
2562 tag = msg[2];
2563 TAG_PRINTK("scsi%d: target mask %02x, lun %d sent tag %d at "
2564 "reselection\n", HOSTNO, target_mask, lun, tag);
2565 }
2566#endif
2567
2568 /*
2569 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2570 * just reestablished, and remove it from the disconnected queue.
2571 */
2572
2573 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
2574 tmp; prev = tmp, tmp = NEXT(tmp) ) {
2575 if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
2576#ifdef SUPPORT_TAGS
2577 && (tag == tmp->tag)
2578#endif
2579 ) {
2580 /* ++guenther: prevent race with falcon_release_lock */
2581 falcon_dont_release++;
2582 if (prev) {
2583 REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
2584 NEXT(prev) = NEXT(tmp);
2585 } else {
2586 REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
2587 hostdata->disconnected_queue = NEXT(tmp);
2588 }
2589 NEXT(tmp) = NULL;
2590 break;
2591 }
2592 }
2593
2594 if (!tmp) {
2595 printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
2596#ifdef SUPPORT_TAGS
2597 "tag %d "
2598#endif
2599 "not in disconnected_queue.\n",
2600 HOSTNO, target_mask, lun
2601#ifdef SUPPORT_TAGS
2602 , tag
2603#endif
2604 );
2605 /*
2606 * Since we have an established nexus that we can't do anything
2607 * with, we must abort it.
2608 */
2609 do_abort(instance);
2610 return;
2611 }
2612
2613 /* Accept message by clearing ACK */
2614 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2615
2616 hostdata->connected = tmp;
2617 RSL_PRINTK("scsi%d: nexus established, target = %d, lun = %d, tag = %d\n",
2618 HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag);
2619 falcon_dont_release--;
2620}
2621
2622
2623/*
2624 * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
2625 *
2626 * Purpose : abort a command
2627 *
2628 * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
2629 * host byte of the result field to, if zero DID_ABORTED is
2630 * used.
2631 *
2632 * Returns : 0 - success, -1 on failure.
2633 *
2634 * XXX - there is no way to abort the command that is currently
2635 * connected, you have to wait for it to complete. If this is
2636 * a problem, we could implement longjmp() / setjmp(), setjmp()
2637 * called where the loop started in NCR5380_main().
2638 */
2639
2640static
2641int NCR5380_abort (Scsi_Cmnd *cmd)
2642{
2643 struct Scsi_Host *instance = cmd->device->host;
2644 SETUP_HOSTDATA(instance);
2645 Scsi_Cmnd *tmp, **prev;
2646 unsigned long flags;
2647
2648 printk(KERN_NOTICE "scsi%d: aborting command\n", HOSTNO);
2649 print_Scsi_Cmnd (cmd);
2650
2651 NCR5380_print_status (instance);
2652
2653 local_irq_save(flags);
2654
2655 if (!IS_A_TT() && !falcon_got_lock)
2656 printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_abort\n",
2657 HOSTNO);
2658
2659 ABRT_PRINTK("scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO,
2660 NCR5380_read(BUS_AND_STATUS_REG),
2661 NCR5380_read(STATUS_REG));
2662
2663#if 1
2664/*
2665 * Case 1 : If the command is the currently executing command,
2666 * we'll set the aborted flag and return control so that
2667 * information transfer routine can exit cleanly.
2668 */
2669
2670 if (hostdata->connected == cmd) {
2671
2672 ABRT_PRINTK("scsi%d: aborting connected command\n", HOSTNO);
2673/*
2674 * We should perform BSY checking, and make sure we haven't slipped
2675 * into BUS FREE.
2676 */
2677
2678/* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
2679/*
2680 * Since we can't change phases until we've completed the current
2681 * handshake, we have to source or sink a byte of data if the current
2682 * phase is not MSGOUT.
2683 */
2684
2685/*
2686 * Return control to the executing NCR drive so we can clear the
2687 * aborted flag and get back into our main loop.
2688 */
2689
2690 if (do_abort(instance) == 0) {
2691 hostdata->aborted = 1;
2692 hostdata->connected = NULL;
2693 cmd->result = DID_ABORT << 16;
2694#ifdef SUPPORT_TAGS
2695 cmd_free_tag( cmd );
2696#else
2697 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2698#endif
2699 local_irq_restore(flags);
2700 cmd->scsi_done(cmd);
2701 falcon_release_lock_if_possible( hostdata );
2702 return SCSI_ABORT_SUCCESS;
2703 } else {
2704/* local_irq_restore(flags); */
2705 printk("scsi%d: abort of connected command failed!\n", HOSTNO);
2706 return SCSI_ABORT_ERROR;
2707 }
2708 }
2709#endif
2710
2711/*
2712 * Case 2 : If the command hasn't been issued yet, we simply remove it
2713 * from the issue queue.
2714 */
2715 for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue),
2716 tmp = (Scsi_Cmnd *) hostdata->issue_queue;
2717 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
2718 if (cmd == tmp) {
2719 REMOVE(5, *prev, tmp, NEXT(tmp));
2720 (*prev) = NEXT(tmp);
2721 NEXT(tmp) = NULL;
2722 tmp->result = DID_ABORT << 16;
2723 local_irq_restore(flags);
2724 ABRT_PRINTK("scsi%d: abort removed command from issue queue.\n",
2725 HOSTNO);
2726 /* Tagged queuing note: no tag to free here, hasn't been assigned
2727 * yet... */
2728 tmp->scsi_done(tmp);
2729 falcon_release_lock_if_possible( hostdata );
2730 return SCSI_ABORT_SUCCESS;
2731 }
2732
2733/*
2734 * Case 3 : If any commands are connected, we're going to fail the abort
2735 * and let the high level SCSI driver retry at a later time or
2736 * issue a reset.
2737 *
2738 * Timeouts, and therefore aborted commands, will be highly unlikely
2739 * and handling them cleanly in this situation would make the common
2740 * case of noresets less efficient, and would pollute our code. So,
2741 * we fail.
2742 */
2743
2744 if (hostdata->connected) {
2745 local_irq_restore(flags);
2746 ABRT_PRINTK("scsi%d: abort failed, command connected.\n", HOSTNO);
2747 return SCSI_ABORT_SNOOZE;
2748 }
2749
2750/*
2751 * Case 4: If the command is currently disconnected from the bus, and
2752 * there are no connected commands, we reconnect the I_T_L or
2753 * I_T_L_Q nexus associated with it, go into message out, and send
2754 * an abort message.
2755 *
2756 * This case is especially ugly. In order to reestablish the nexus, we
2757 * need to call NCR5380_select(). The easiest way to implement this
2758 * function was to abort if the bus was busy, and let the interrupt
2759 * handler triggered on the SEL for reselect take care of lost arbitrations
2760 * where necessary, meaning interrupts need to be enabled.
2761 *
2762 * When interrupts are enabled, the queues may change - so we
2763 * can't remove it from the disconnected queue before selecting it
2764 * because that could cause a failure in hashing the nexus if that
2765 * device reselected.
2766 *
2767 * Since the queues may change, we can't use the pointers from when we
2768 * first locate it.
2769 *
2770 * So, we must first locate the command, and if NCR5380_select()
2771 * succeeds, then issue the abort, relocate the command and remove
2772 * it from the disconnected queue.
2773 */
2774
2775 for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
2776 tmp = NEXT(tmp))
2777 if (cmd == tmp) {
2778 local_irq_restore(flags);
2779 ABRT_PRINTK("scsi%d: aborting disconnected command.\n", HOSTNO);
2780
2781 if (NCR5380_select (instance, cmd, (int) cmd->tag))
2782 return SCSI_ABORT_BUSY;
2783
2784 ABRT_PRINTK("scsi%d: nexus reestablished.\n", HOSTNO);
2785
2786 do_abort (instance);
2787
2788 local_irq_save(flags);
2789 for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue),
2790 tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
2791 tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
2792 if (cmd == tmp) {
2793 REMOVE(5, *prev, tmp, NEXT(tmp));
2794 *prev = NEXT(tmp);
2795 NEXT(tmp) = NULL;
2796 tmp->result = DID_ABORT << 16;
2797 /* We must unlock the tag/LUN immediately here, since the
2798 * target goes to BUS FREE and doesn't send us another
2799 * message (COMMAND_COMPLETE or the like)
2800 */
2801#ifdef SUPPORT_TAGS
2802 cmd_free_tag( tmp );
2803#else
2804 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
2805#endif
2806 local_irq_restore(flags);
2807 tmp->scsi_done(tmp);
2808 falcon_release_lock_if_possible( hostdata );
2809 return SCSI_ABORT_SUCCESS;
2810 }
2811 }
2812
2813/*
2814 * Case 5 : If we reached this point, the command was not found in any of
2815 * the queues.
2816 *
2817 * We probably reached this point because of an unlikely race condition
2818 * between the command completing successfully and the abortion code,
2819 * so we won't panic, but we will notify the user in case something really
2820 * broke.
2821 */
2822
2823 local_irq_restore(flags);
2824 printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully\n"
2825 KERN_INFO " before abortion\n", HOSTNO);
2826
2827/* Maybe it is sufficient just to release the ST-DMA lock... (if
2828 * possible at all) At least, we should check if the lock could be
2829 * released after the abort, in case it is kept due to some bug.
2830 */
2831 falcon_release_lock_if_possible( hostdata );
2832
2833 return SCSI_ABORT_NOT_RUNNING;
2834}
2835
2836
2837/*
2838 * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
2839 *
2840 * Purpose : reset the SCSI bus.
2841 *
2842 * Returns : SCSI_RESET_WAKEUP
2843 *
2844 */
2845
2846static int NCR5380_bus_reset( Scsi_Cmnd *cmd)
2847{
2848 SETUP_HOSTDATA(cmd->device->host);
2849 int i;
2850 unsigned long flags;
2851#if 1
2852 Scsi_Cmnd *connected, *disconnected_queue;
2853#endif
2854
2855 if (!IS_A_TT() && !falcon_got_lock)
2856 printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_reset\n",
2857 H_NO(cmd) );
2858
2859 NCR5380_print_status (cmd->device->host);
2860
2861 /* get in phase */
2862 NCR5380_write( TARGET_COMMAND_REG,
2863 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
2864 /* assert RST */
2865 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
2866 udelay (40);
2867 /* reset NCR registers */
2868 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
2869 NCR5380_write( MODE_REG, MR_BASE );
2870 NCR5380_write( TARGET_COMMAND_REG, 0 );
2871 NCR5380_write( SELECT_ENABLE_REG, 0 );
2872 /* ++roman: reset interrupt condition! otherwise no interrupts don't get
2873 * through anymore ... */
2874 (void)NCR5380_read( RESET_PARITY_INTERRUPT_REG );
2875
2876#if 1 /* XXX Should now be done by midlevel code, but it's broken XXX */
2877 /* XXX see below XXX */
2878
2879 /* MSch: old-style reset: actually abort all command processing here */
2880
2881 /* After the reset, there are no more connected or disconnected commands
2882 * and no busy units; to avoid problems with re-inserting the commands
2883 * into the issue_queue (via scsi_done()), the aborted commands are
2884 * remembered in local variables first.
2885 */
2886 local_irq_save(flags);
2887 connected = (Scsi_Cmnd *)hostdata->connected;
2888 hostdata->connected = NULL;
2889 disconnected_queue = (Scsi_Cmnd *)hostdata->disconnected_queue;
2890 hostdata->disconnected_queue = NULL;
2891#ifdef SUPPORT_TAGS
2892 free_all_tags();
2893#endif
2894 for( i = 0; i < 8; ++i )
2895 hostdata->busy[i] = 0;
2896#ifdef REAL_DMA
2897 hostdata->dma_len = 0;
2898#endif
2899 local_irq_restore(flags);
2900
2901 /* In order to tell the mid-level code which commands were aborted,
2902 * set the command status to DID_RESET and call scsi_done() !!!
2903 * This ultimately aborts processing of these commands in the mid-level.
2904 */
2905
2906 if ((cmd = connected)) {
2907 ABRT_PRINTK("scsi%d: reset aborted a connected command\n", H_NO(cmd));
2908 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2909 cmd->scsi_done( cmd );
2910 }
2911
2912 for (i = 0; (cmd = disconnected_queue); ++i) {
2913 disconnected_queue = NEXT(cmd);
2914 NEXT(cmd) = NULL;
2915 cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
2916 cmd->scsi_done( cmd );
2917 }
2918 if (i > 0)
2919 ABRT_PRINTK("scsi: reset aborted %d disconnected command(s)\n", i);
2920
2921/* The Falcon lock should be released after a reset...
2922 */
2923/* ++guenther: moved to atari_scsi_reset(), to prevent a race between
2924 * unlocking and enabling dma interrupt.
2925 */
2926/* falcon_release_lock_if_possible( hostdata );*/
2927
2928 /* since all commands have been explicitly terminated, we need to tell
2929 * the midlevel code that the reset was SUCCESSFUL, and there is no
2930 * need to 'wake up' the commands by a request_sense
2931 */
2932 return SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
2933#else /* 1 */
2934
2935 /* MSch: new-style reset handling: let the mid-level do what it can */
2936
2937 /* ++guenther: MID-LEVEL IS STILL BROKEN.
2938 * Mid-level is supposed to requeue all commands that were active on the
2939 * various low-level queues. In fact it does this, but that's not enough
2940 * because all these commands are subject to timeout. And if a timeout
2941 * happens for any removed command, *_abort() is called but all queues
2942 * are now empty. Abort then gives up the falcon lock, which is fatal,
2943 * since the mid-level will queue more commands and must have the lock
2944 * (it's all happening inside timer interrupt handler!!).
2945 * Even worse, abort will return NOT_RUNNING for all those commands not
2946 * on any queue, so they won't be retried ...
2947 *
2948 * Conclusion: either scsi.c disables timeout for all resetted commands
2949 * immediately, or we lose! As of linux-2.0.20 it doesn't.
2950 */
2951
2952 /* After the reset, there are no more connected or disconnected commands
2953 * and no busy units; so clear the low-level status here to avoid
2954 * conflicts when the mid-level code tries to wake up the affected
2955 * commands!
2956 */
2957
2958 if (hostdata->issue_queue)
2959 ABRT_PRINTK("scsi%d: reset aborted issued command(s)\n", H_NO(cmd));
2960 if (hostdata->connected)
2961 ABRT_PRINTK("scsi%d: reset aborted a connected command\n", H_NO(cmd));
2962 if (hostdata->disconnected_queue)
2963 ABRT_PRINTK("scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd));
2964
2965 local_irq_save(flags);
2966 hostdata->issue_queue = NULL;
2967 hostdata->connected = NULL;
2968 hostdata->disconnected_queue = NULL;
2969#ifdef SUPPORT_TAGS
2970 free_all_tags();
2971#endif
2972 for( i = 0; i < 8; ++i )
2973 hostdata->busy[i] = 0;
2974#ifdef REAL_DMA
2975 hostdata->dma_len = 0;
2976#endif
2977 local_irq_restore(flags);
2978
2979 /* we did no complete reset of all commands, so a wakeup is required */
2980 return SCSI_RESET_WAKEUP | SCSI_RESET_BUS_RESET;
2981#endif /* 1 */
2982}
2983
2984/* Local Variables: */
2985/* tab-width: 8 */
2986/* End: */