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