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