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