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