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