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