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