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