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