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