atari_NCR5380: Remove DMA_MIN_SIZE macro
[deliverable/linux.git] / drivers / scsi / NCR5380.c
CommitLineData
aff0cf9a 1/*
1da177e4 2 * NCR 5380 generic driver routines. These should make it *trivial*
594d4ba3
FT
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
1da177e4 5 *
594d4ba3 6 * Note that these routines also work with NR53c400 family chips.
1da177e4
LT
7 *
8 * Copyright 1993, Drew Eckhardt
594d4ba3
FT
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
1da177e4 13 *
aff0cf9a 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/*
c16df32e
FT
28 * With contributions from Ray Van Tassle, Ingmar Baumgart,
29 * Ronald van Cuijlenborg, Alan Cox and others.
1da177e4
LT
30 */
31
32/*
aff0cf9a 33 * Further development / testing that should be done :
1da177e4 34 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
594d4ba3
FT
35 * code so that everything does the same thing that's done at the
36 * end of a pseudo-DMA read operation.
1da177e4 37 *
aff0cf9a 38 * 4. Test SCSI-II tagged queueing (I have no devices which support
594d4ba3 39 * tagged queueing)
1da177e4
LT
40 */
41
1da177e4
LT
42#ifdef BOARD_REQUIRES_NO_DELAY
43#define io_recovery_delay(x)
44#else
45#define io_recovery_delay(x) udelay(x)
46#endif
47
48/*
49 * Design
50 *
aff0cf9a 51 * This is a generic 5380 driver. To use it on a different platform,
1da177e4 52 * one simply writes appropriate system specific macros (ie, data
aff0cf9a 53 * transfer - some PC's will use the I/O bus, 68K's must use
1da177e4
LT
54 * memory mapped) and drops this file in their 'C' wrapper.
55 *
aff0cf9a 56 * As far as command queueing, two queues are maintained for
1da177e4 57 * each 5380 in the system - commands that haven't been issued yet,
aff0cf9a
FT
58 * and commands that are currently executing. This means that an
59 * unlimited number of commands may be queued, letting
60 * more commands propagate from the higher driver levels giving higher
61 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
62 * allowing multiple commands to propagate all the way to a SCSI-II device
1da177e4
LT
63 * while a command is already executing.
64 *
65 *
aff0cf9a 66 * Issues specific to the NCR5380 :
1da177e4 67 *
aff0cf9a
FT
68 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
69 * piece of hardware that requires you to sit in a loop polling for
70 * the REQ signal as long as you are connected. Some devices are
71 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
686f3990 72 * while doing long seek operations. [...] These
1da177e4
LT
73 * broken devices are the exception rather than the rule and I'd rather
74 * spend my time optimizing for the normal case.
75 *
76 * Architecture :
77 *
78 * At the heart of the design is a coroutine, NCR5380_main,
79 * which is started from a workqueue for each NCR5380 host in the
80 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
81 * removing the commands from the issue queue and calling
aff0cf9a 82 * NCR5380_select() if a nexus is not established.
1da177e4
LT
83 *
84 * Once a nexus is established, the NCR5380_information_transfer()
85 * phase goes through the various phases as instructed by the target.
86 * if the target goes into MSG IN and sends a DISCONNECT message,
87 * the command structure is placed into the per instance disconnected
aff0cf9a 88 * queue, and NCR5380_main tries to find more work. If the target is
1da177e4
LT
89 * idle for too long, the system will try to sleep.
90 *
91 * If a command has disconnected, eventually an interrupt will trigger,
92 * calling NCR5380_intr() which will in turn call NCR5380_reselect
93 * to reestablish a nexus. This will run main if necessary.
94 *
aff0cf9a 95 * On command termination, the done function will be called as
1da177e4
LT
96 * appropriate.
97 *
aff0cf9a 98 * SCSI pointers are maintained in the SCp field of SCSI command
1da177e4
LT
99 * structures, being initialized after the command is connected
100 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
101 * Note that in violation of the standard, an implicit SAVE POINTERS operation
102 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
103 */
104
105/*
106 * Using this file :
107 * This file a skeleton Linux SCSI driver for the NCR 5380 series
aff0cf9a 108 * of chips. To use it, you write an architecture specific functions
1da177e4
LT
109 * and macros and include this file in your driver.
110 *
aff0cf9a
FT
111 * These macros control options :
112 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
594d4ba3 113 * defined.
aff0cf9a 114 *
1da177e4 115 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
594d4ba3 116 * for commands that return with a CHECK CONDITION status.
1da177e4
LT
117 *
118 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
594d4ba3 119 * transceivers.
1da177e4
LT
120 *
121 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
594d4ba3 122 * override-configure an IRQ.
1da177e4 123 *
1da177e4
LT
124 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
125 *
1da177e4 126 * These macros MUST be defined :
aff0cf9a 127 *
1da177e4
LT
128 * NCR5380_read(register) - read from the specified register
129 *
aff0cf9a 130 * NCR5380_write(register, value) - write to the specific register
1da177e4 131 *
aff0cf9a 132 * NCR5380_implementation_fields - additional fields needed for this
594d4ba3 133 * specific implementation of the NCR5380
1da177e4
LT
134 *
135 * Either real DMA *or* pseudo DMA may be implemented
aff0cf9a 136 * Note that the DMA setup functions should return the number of bytes
594d4ba3 137 * that they were able to program the controller for.
1da177e4 138 *
1da177e4
LT
139 * NCR5380_dma_write_setup(instance, src, count) - initialize
140 * NCR5380_dma_read_setup(instance, dst, count) - initialize
141 * NCR5380_dma_residual(instance); - residual count
142 *
143 * PSEUDO functions :
144 * NCR5380_pwrite(instance, src, count)
145 * NCR5380_pread(instance, dst, count);
146 *
147 * The generic driver is initialized by calling NCR5380_init(instance),
aff0cf9a 148 * after setting the appropriate host specific fields and ID. If the
1da177e4
LT
149 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
150 * possible) function may be used.
151 */
152
54d8fe44
FT
153static int do_abort(struct Scsi_Host *);
154static void do_reset(struct Scsi_Host *);
1da177e4 155
c16df32e 156/**
0d2cf867 157 * initialize_SCp - init the scsi pointer field
594d4ba3 158 * @cmd: command block to set up
1da177e4 159 *
594d4ba3 160 * Set up the internal fields in the SCSI command.
1da177e4
LT
161 */
162
710ddd0d 163static inline void initialize_SCp(struct scsi_cmnd *cmd)
1da177e4 164{
aff0cf9a
FT
165 /*
166 * Initialize the Scsi Pointer field so that all of the commands in the
1da177e4
LT
167 * various queues are valid.
168 */
169
9e0fe44d
BH
170 if (scsi_bufflen(cmd)) {
171 cmd->SCp.buffer = scsi_sglist(cmd);
172 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
45711f1a 173 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
1da177e4
LT
174 cmd->SCp.this_residual = cmd->SCp.buffer->length;
175 } else {
176 cmd->SCp.buffer = NULL;
177 cmd->SCp.buffers_residual = 0;
9e0fe44d
BH
178 cmd->SCp.ptr = NULL;
179 cmd->SCp.this_residual = 0;
1da177e4 180 }
f27db8eb
FT
181
182 cmd->SCp.Status = 0;
183 cmd->SCp.Message = 0;
1da177e4
LT
184}
185
186/**
b32ade12 187 * NCR5380_poll_politely2 - wait for two chip register values
2f854b82 188 * @instance: controller to poll
b32ade12
FT
189 * @reg1: 5380 register to poll
190 * @bit1: Bitmask to check
191 * @val1: Expected value
192 * @reg2: Second 5380 register to poll
193 * @bit2: Second bitmask to check
194 * @val2: Second expected value
2f854b82
FT
195 * @wait: Time-out in jiffies
196 *
197 * Polls the chip in a reasonably efficient manner waiting for an
198 * event to occur. After a short quick poll we begin to yield the CPU
199 * (if possible). In irq contexts the time-out is arbitrarily limited.
200 * Callers may hold locks as long as they are held in irq mode.
201 *
b32ade12 202 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
1da177e4 203 */
2f854b82 204
b32ade12
FT
205static int NCR5380_poll_politely2(struct Scsi_Host *instance,
206 int reg1, int bit1, int val1,
207 int reg2, int bit2, int val2, int wait)
1da177e4 208{
2f854b82
FT
209 struct NCR5380_hostdata *hostdata = shost_priv(instance);
210 unsigned long deadline = jiffies + wait;
211 unsigned long n;
212
213 /* Busy-wait for up to 10 ms */
214 n = min(10000U, jiffies_to_usecs(wait));
215 n *= hostdata->accesses_per_ms;
b32ade12 216 n /= 2000;
2f854b82 217 do {
b32ade12
FT
218 if ((NCR5380_read(reg1) & bit1) == val1)
219 return 0;
220 if ((NCR5380_read(reg2) & bit2) == val2)
1da177e4
LT
221 return 0;
222 cpu_relax();
2f854b82
FT
223 } while (n--);
224
225 if (irqs_disabled() || in_interrupt())
226 return -ETIMEDOUT;
227
228 /* Repeatedly sleep for 1 ms until deadline */
229 while (time_is_after_jiffies(deadline)) {
230 schedule_timeout_uninterruptible(1);
b32ade12
FT
231 if ((NCR5380_read(reg1) & bit1) == val1)
232 return 0;
233 if ((NCR5380_read(reg2) & bit2) == val2)
1da177e4 234 return 0;
1da177e4 235 }
2f854b82 236
1da177e4
LT
237 return -ETIMEDOUT;
238}
239
b32ade12
FT
240static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
241 int reg, int bit, int val, int wait)
242{
243 return NCR5380_poll_politely2(instance, reg, bit, val,
244 reg, bit, val, wait);
245}
246
185a7a1c 247#if NDEBUG
1da177e4
LT
248static struct {
249 unsigned char mask;
250 const char *name;
aff0cf9a
FT
251} signals[] = {
252 {SR_DBP, "PARITY"},
253 {SR_RST, "RST"},
254 {SR_BSY, "BSY"},
255 {SR_REQ, "REQ"},
256 {SR_MSG, "MSG"},
257 {SR_CD, "CD"},
258 {SR_IO, "IO"},
259 {SR_SEL, "SEL"},
1da177e4 260 {0, NULL}
aff0cf9a 261},
1da177e4 262basrs[] = {
aff0cf9a
FT
263 {BASR_ATN, "ATN"},
264 {BASR_ACK, "ACK"},
1da177e4 265 {0, NULL}
aff0cf9a
FT
266},
267icrs[] = {
268 {ICR_ASSERT_RST, "ASSERT RST"},
269 {ICR_ASSERT_ACK, "ASSERT ACK"},
270 {ICR_ASSERT_BSY, "ASSERT BSY"},
271 {ICR_ASSERT_SEL, "ASSERT SEL"},
272 {ICR_ASSERT_ATN, "ASSERT ATN"},
273 {ICR_ASSERT_DATA, "ASSERT DATA"},
1da177e4 274 {0, NULL}
aff0cf9a
FT
275},
276mrs[] = {
277 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
278 {MR_TARGET, "MODE TARGET"},
279 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
280 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
0d2cf867 281 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
aff0cf9a
FT
282 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
283 {MR_DMA_MODE, "MODE DMA"},
284 {MR_ARBITRATE, "MODE ARBITRATION"},
1da177e4
LT
285 {0, NULL}
286};
287
288/**
0d2cf867
FT
289 * NCR5380_print - print scsi bus signals
290 * @instance: adapter state to dump
1da177e4 291 *
594d4ba3 292 * Print the SCSI bus signals for debugging purposes
1da177e4
LT
293 */
294
295static void NCR5380_print(struct Scsi_Host *instance)
296{
1da177e4 297 unsigned char status, data, basr, mr, icr, i;
1da177e4
LT
298
299 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
300 status = NCR5380_read(STATUS_REG);
301 mr = NCR5380_read(MODE_REG);
302 icr = NCR5380_read(INITIATOR_COMMAND_REG);
303 basr = NCR5380_read(BUS_AND_STATUS_REG);
304
305 printk("STATUS_REG: %02x ", status);
306 for (i = 0; signals[i].mask; ++i)
307 if (status & signals[i].mask)
308 printk(",%s", signals[i].name);
309 printk("\nBASR: %02x ", basr);
310 for (i = 0; basrs[i].mask; ++i)
311 if (basr & basrs[i].mask)
312 printk(",%s", basrs[i].name);
313 printk("\nICR: %02x ", icr);
314 for (i = 0; icrs[i].mask; ++i)
315 if (icr & icrs[i].mask)
316 printk(",%s", icrs[i].name);
317 printk("\nMODE: %02x ", mr);
318 for (i = 0; mrs[i].mask; ++i)
319 if (mr & mrs[i].mask)
320 printk(",%s", mrs[i].name);
321 printk("\n");
322}
323
0d2cf867
FT
324static struct {
325 unsigned char value;
326 const char *name;
327} phases[] = {
328 {PHASE_DATAOUT, "DATAOUT"},
329 {PHASE_DATAIN, "DATAIN"},
330 {PHASE_CMDOUT, "CMDOUT"},
331 {PHASE_STATIN, "STATIN"},
332 {PHASE_MSGOUT, "MSGOUT"},
333 {PHASE_MSGIN, "MSGIN"},
334 {PHASE_UNKNOWN, "UNKNOWN"}
335};
1da177e4 336
c16df32e 337/**
0d2cf867 338 * NCR5380_print_phase - show SCSI phase
594d4ba3 339 * @instance: adapter to dump
1da177e4 340 *
594d4ba3 341 * Print the current SCSI phase for debugging purposes
1da177e4
LT
342 */
343
344static void NCR5380_print_phase(struct Scsi_Host *instance)
345{
1da177e4
LT
346 unsigned char status;
347 int i;
1da177e4
LT
348
349 status = NCR5380_read(STATUS_REG);
350 if (!(status & SR_REQ))
6a6ff4ac 351 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
1da177e4 352 else {
0d2cf867
FT
353 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
354 (phases[i].value != (status & PHASE_MASK)); ++i)
355 ;
6a6ff4ac 356 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
1da177e4
LT
357 }
358}
359#endif
360
1da177e4 361
d5f7e65d 362static int probe_irq __initdata;
1da177e4
LT
363
364/**
594d4ba3
FT
365 * probe_intr - helper for IRQ autoprobe
366 * @irq: interrupt number
367 * @dev_id: unused
368 * @regs: unused
1da177e4 369 *
594d4ba3
FT
370 * Set a flag to indicate the IRQ in question was received. This is
371 * used by the IRQ probe code.
1da177e4 372 */
aff0cf9a 373
7d12e780 374static irqreturn_t __init probe_intr(int irq, void *dev_id)
1da177e4
LT
375{
376 probe_irq = irq;
377 return IRQ_HANDLED;
378}
379
380/**
594d4ba3
FT
381 * NCR5380_probe_irq - find the IRQ of an NCR5380
382 * @instance: NCR5380 controller
383 * @possible: bitmask of ISA IRQ lines
1da177e4 384 *
594d4ba3
FT
385 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
386 * and then looking to see what interrupt actually turned up.
1da177e4
LT
387 */
388
702809ce
AM
389static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
390 int possible)
1da177e4 391{
e8a60144 392 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
393 unsigned long timeout;
394 int trying_irqs, i, mask;
1da177e4 395
22f5f10d 396 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
4909cc2b 397 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
1da177e4
LT
398 trying_irqs |= mask;
399
4e5a800c 400 timeout = jiffies + msecs_to_jiffies(250);
22f5f10d 401 probe_irq = NO_IRQ;
1da177e4
LT
402
403 /*
404 * A interrupt is triggered whenever BSY = false, SEL = true
aff0cf9a 405 * and a bit set in the SELECT_ENABLE_REG is asserted on the
1da177e4
LT
406 * SCSI bus.
407 *
408 * Note that the bus is only driven when the phase control signals
409 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
410 * to zero.
411 */
412
413 NCR5380_write(TARGET_COMMAND_REG, 0);
414 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
415 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
416 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
417
22f5f10d 418 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
a9a3047d 419 schedule_timeout_uninterruptible(1);
aff0cf9a 420
1da177e4
LT
421 NCR5380_write(SELECT_ENABLE_REG, 0);
422 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
423
22f5f10d 424 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
1da177e4
LT
425 if (trying_irqs & mask)
426 free_irq(i, NULL);
427
428 return probe_irq;
429}
430
431/**
594d4ba3
FT
432 * NCR58380_info - report driver and host information
433 * @instance: relevant scsi host instance
1da177e4 434 *
594d4ba3 435 * For use as the host template info() handler.
1da177e4
LT
436 */
437
8c32513b 438static const char *NCR5380_info(struct Scsi_Host *instance)
1da177e4 439{
8c32513b
FT
440 struct NCR5380_hostdata *hostdata = shost_priv(instance);
441
442 return hostdata->info;
443}
444
445static void prepare_info(struct Scsi_Host *instance)
446{
447 struct NCR5380_hostdata *hostdata = shost_priv(instance);
448
449 snprintf(hostdata->info, sizeof(hostdata->info),
450 "%s, io_port 0x%lx, n_io_port %d, "
451 "base 0x%lx, irq %d, "
452 "can_queue %d, cmd_per_lun %d, "
453 "sg_tablesize %d, this_id %d, "
be3f4121 454 "flags { %s%s%s}, "
8c32513b
FT
455 "options { %s} ",
456 instance->hostt->name, instance->io_port, instance->n_io_port,
457 instance->base, instance->irq,
458 instance->can_queue, instance->cmd_per_lun,
459 instance->sg_tablesize, instance->this_id,
55181be8 460 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
8c32513b 461 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
9c3f0e2b 462 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
1da177e4 463#ifdef AUTOPROBE_IRQ
8c32513b 464 "AUTOPROBE_IRQ "
1da177e4 465#endif
1da177e4 466#ifdef DIFFERENTIAL
8c32513b 467 "DIFFERENTIAL "
1da177e4 468#endif
1da177e4 469#ifdef PARITY
8c32513b 470 "PARITY "
1da177e4
LT
471#endif
472#ifdef PSEUDO_DMA
8c32513b 473 "PSEUDO_DMA "
8c32513b
FT
474#endif
475 "");
1da177e4
LT
476}
477
a9c2dc43 478#ifdef PSEUDO_DMA
dd7ab71b
AV
479static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
480 char *buffer, int length)
481{
a9c2dc43
FT
482 struct NCR5380_hostdata *hostdata = shost_priv(instance);
483
484 hostdata->spin_max_r = 0;
485 hostdata->spin_max_w = 0;
486 return 0;
dd7ab71b 487}
1da177e4 488
dd7ab71b 489static int __maybe_unused NCR5380_show_info(struct seq_file *m,
0d2cf867 490 struct Scsi_Host *instance)
1da177e4 491{
e8a60144 492 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4 493
0c3de38f 494 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
a9c2dc43 495 hostdata->spin_max_w, hostdata->spin_max_r);
dd7ab71b 496 return 0;
1da177e4 497}
e5c3fddf 498#endif
1da177e4
LT
499
500/**
0d2cf867 501 * NCR5380_init - initialise an NCR5380
594d4ba3
FT
502 * @instance: adapter to configure
503 * @flags: control flags
1da177e4 504 *
594d4ba3
FT
505 * Initializes *instance and corresponding 5380 chip,
506 * with flags OR'd into the initial flags value.
1da177e4 507 *
594d4ba3 508 * Notes : I assume that the host, hostno, and id bits have been
0d2cf867 509 * set correctly. I don't care about the irq and other fields.
1da177e4 510 *
594d4ba3 511 * Returns 0 for success
1da177e4
LT
512 */
513
6f039790 514static int NCR5380_init(struct Scsi_Host *instance, int flags)
1da177e4 515{
e8a60144 516 struct NCR5380_hostdata *hostdata = shost_priv(instance);
b6488f97 517 int i;
2f854b82 518 unsigned long deadline;
1da177e4 519
0d2cf867 520 hostdata->host = instance;
1da177e4 521 hostdata->id_mask = 1 << instance->this_id;
0d2cf867 522 hostdata->id_higher_mask = 0;
1da177e4
LT
523 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
524 if (i > hostdata->id_mask)
525 hostdata->id_higher_mask |= i;
526 for (i = 0; i < 8; ++i)
527 hostdata->busy[i] = 0;
e4dec680
FT
528 hostdata->dma_len = 0;
529
11d2f63b 530 spin_lock_init(&hostdata->lock);
1da177e4 531 hostdata->connected = NULL;
f27db8eb
FT
532 hostdata->sensing = NULL;
533 INIT_LIST_HEAD(&hostdata->autosense);
32b26a10
FT
534 INIT_LIST_HEAD(&hostdata->unissued);
535 INIT_LIST_HEAD(&hostdata->disconnected);
536
55181be8 537 hostdata->flags = flags;
aff0cf9a 538
8d8601a7 539 INIT_WORK(&hostdata->main_task, NCR5380_main);
0ad0eff9
FT
540 hostdata->work_q = alloc_workqueue("ncr5380_%d",
541 WQ_UNBOUND | WQ_MEM_RECLAIM,
542 1, instance->host_no);
543 if (!hostdata->work_q)
544 return -ENOMEM;
545
8c32513b
FT
546 prepare_info(instance);
547
1da177e4
LT
548 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
549 NCR5380_write(MODE_REG, MR_BASE);
550 NCR5380_write(TARGET_COMMAND_REG, 0);
551 NCR5380_write(SELECT_ENABLE_REG, 0);
2f854b82
FT
552
553 /* Calibrate register polling loop */
554 i = 0;
555 deadline = jiffies + 1;
556 do {
557 cpu_relax();
558 } while (time_is_after_jiffies(deadline));
559 deadline += msecs_to_jiffies(256);
560 do {
561 NCR5380_read(STATUS_REG);
562 ++i;
563 cpu_relax();
564 } while (time_is_after_jiffies(deadline));
565 hostdata->accesses_per_ms = i / 256;
566
b6488f97
FT
567 return 0;
568}
1da177e4 569
b6488f97
FT
570/**
571 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
572 * @instance: adapter to check
573 *
574 * If the system crashed, it may have crashed with a connected target and
575 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
576 * currently established nexus, which we know nothing about. Failing that
577 * do a bus reset.
578 *
579 * Note that a bus reset will cause the chip to assert IRQ.
580 *
581 * Returns 0 if successful, otherwise -ENXIO.
582 */
583
584static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
585{
9c3f0e2b 586 struct NCR5380_hostdata *hostdata = shost_priv(instance);
b6488f97 587 int pass;
1da177e4
LT
588
589 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
590 switch (pass) {
591 case 1:
592 case 3:
593 case 5:
636b1ec8
FT
594 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
595 NCR5380_poll_politely(instance,
596 STATUS_REG, SR_BSY, 0, 5 * HZ);
1da177e4
LT
597 break;
598 case 2:
636b1ec8 599 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
1da177e4
LT
600 do_abort(instance);
601 break;
602 case 4:
636b1ec8 603 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
1da177e4 604 do_reset(instance);
9c3f0e2b
FT
605 /* Wait after a reset; the SCSI standard calls for
606 * 250ms, we wait 500ms to be on the safe side.
607 * But some Toshiba CD-ROMs need ten times that.
608 */
609 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
610 msleep(2500);
611 else
612 msleep(500);
1da177e4
LT
613 break;
614 case 6:
636b1ec8 615 shost_printk(KERN_ERR, instance, "bus locked solid\n");
1da177e4
LT
616 return -ENXIO;
617 }
618 }
619 return 0;
620}
621
622/**
0d2cf867 623 * NCR5380_exit - remove an NCR5380
594d4ba3 624 * @instance: adapter to remove
0d2cf867
FT
625 *
626 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
1da177e4
LT
627 */
628
a43cf0f3 629static void NCR5380_exit(struct Scsi_Host *instance)
1da177e4 630{
e8a60144 631 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4 632
8d8601a7 633 cancel_work_sync(&hostdata->main_task);
0ad0eff9 634 destroy_workqueue(hostdata->work_q);
1da177e4
LT
635}
636
677e0194
FT
637/**
638 * complete_cmd - finish processing a command and return it to the SCSI ML
639 * @instance: the host instance
640 * @cmd: command to complete
641 */
642
643static void complete_cmd(struct Scsi_Host *instance,
644 struct scsi_cmnd *cmd)
645{
646 struct NCR5380_hostdata *hostdata = shost_priv(instance);
647
648 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
649
f27db8eb
FT
650 if (hostdata->sensing == cmd) {
651 /* Autosense processing ends here */
652 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
653 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
654 set_host_byte(cmd, DID_ERROR);
655 } else
656 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
657 hostdata->sensing = NULL;
658 }
659
677e0194
FT
660 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
661
662 cmd->scsi_done(cmd);
663}
664
1da177e4 665/**
1bb40589
FT
666 * NCR5380_queue_command - queue a command
667 * @instance: the relevant SCSI adapter
668 * @cmd: SCSI command
1da177e4 669 *
1bb40589
FT
670 * cmd is added to the per-instance issue queue, with minor
671 * twiddling done to the host specific fields of cmd. If the
672 * main coroutine is not running, it is restarted.
1da177e4
LT
673 */
674
1bb40589
FT
675static int NCR5380_queue_command(struct Scsi_Host *instance,
676 struct scsi_cmnd *cmd)
1da177e4 677{
1bb40589 678 struct NCR5380_hostdata *hostdata = shost_priv(instance);
32b26a10 679 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1bb40589 680 unsigned long flags;
1da177e4
LT
681
682#if (NDEBUG & NDEBUG_NO_WRITE)
683 switch (cmd->cmnd[0]) {
684 case WRITE_6:
685 case WRITE_10:
dbb6b350 686 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
1da177e4 687 cmd->result = (DID_ERROR << 16);
1bb40589 688 cmd->scsi_done(cmd);
1da177e4
LT
689 return 0;
690 }
0d2cf867 691#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
1da177e4 692
1da177e4
LT
693 cmd->result = 0;
694
11d2f63b 695 spin_lock_irqsave(&hostdata->lock, flags);
1bb40589 696
aff0cf9a
FT
697 /*
698 * Insert the cmd into the issue queue. Note that REQUEST SENSE
1da177e4 699 * commands are added to the head of the queue since any command will
aff0cf9a 700 * clear the contingent allegiance condition that exists and the
1da177e4
LT
701 * sense data is only guaranteed to be valid while the condition exists.
702 */
703
32b26a10
FT
704 if (cmd->cmnd[0] == REQUEST_SENSE)
705 list_add(&ncmd->list, &hostdata->unissued);
706 else
707 list_add_tail(&ncmd->list, &hostdata->unissued);
708
11d2f63b 709 spin_unlock_irqrestore(&hostdata->lock, flags);
1bb40589 710
dbb6b350
FT
711 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
712 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
1da177e4 713
1da177e4 714 /* Kick off command processing */
8d8601a7 715 queue_work(hostdata->work_q, &hostdata->main_task);
1da177e4
LT
716 return 0;
717}
718
f27db8eb
FT
719/**
720 * dequeue_next_cmd - dequeue a command for processing
721 * @instance: the scsi host instance
722 *
723 * Priority is given to commands on the autosense queue. These commands
724 * need autosense because of a CHECK CONDITION result.
725 *
726 * Returns a command pointer if a command is found for a target that is
727 * not already busy. Otherwise returns NULL.
728 */
729
730static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
731{
732 struct NCR5380_hostdata *hostdata = shost_priv(instance);
733 struct NCR5380_cmd *ncmd;
734 struct scsi_cmnd *cmd;
735
8d5dbec3 736 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
f27db8eb
FT
737 list_for_each_entry(ncmd, &hostdata->unissued, list) {
738 cmd = NCR5380_to_scmd(ncmd);
739 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
740 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
741
742 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
743 list_del(&ncmd->list);
744 dsprintk(NDEBUG_QUEUES, instance,
745 "dequeue: removed %p from issue queue\n", cmd);
746 return cmd;
747 }
748 }
749 } else {
750 /* Autosense processing begins here */
751 ncmd = list_first_entry(&hostdata->autosense,
752 struct NCR5380_cmd, list);
753 list_del(&ncmd->list);
754 cmd = NCR5380_to_scmd(ncmd);
755 dsprintk(NDEBUG_QUEUES, instance,
756 "dequeue: removed %p from autosense queue\n", cmd);
757 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
758 hostdata->sensing = cmd;
759 return cmd;
760 }
761 return NULL;
762}
763
764static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
765{
766 struct NCR5380_hostdata *hostdata = shost_priv(instance);
767 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
768
8d5dbec3 769 if (hostdata->sensing == cmd) {
f27db8eb
FT
770 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
771 list_add(&ncmd->list, &hostdata->autosense);
772 hostdata->sensing = NULL;
773 } else
774 list_add(&ncmd->list, &hostdata->unissued);
775}
776
1da177e4 777/**
0d2cf867 778 * NCR5380_main - NCR state machines
1da177e4 779 *
594d4ba3
FT
780 * NCR5380_main is a coroutine that runs as long as more work can
781 * be done on the NCR5380 host adapters in a system. Both
782 * NCR5380_queue_command() and NCR5380_intr() will try to start it
783 * in case it is not running.
1da177e4
LT
784 */
785
c4028958 786static void NCR5380_main(struct work_struct *work)
1da177e4 787{
c4028958 788 struct NCR5380_hostdata *hostdata =
8d8601a7 789 container_of(work, struct NCR5380_hostdata, main_task);
1da177e4 790 struct Scsi_Host *instance = hostdata->host;
1da177e4 791 int done;
aff0cf9a 792
1da177e4 793 do {
1da177e4 794 done = 1;
11d2f63b 795
0a4e3612 796 spin_lock_irq(&hostdata->lock);
ccf6efd7
FT
797 while (!hostdata->connected && !hostdata->selecting) {
798 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
799
800 if (!cmd)
801 break;
1da177e4 802
f27db8eb 803 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
76f13b93 804
f27db8eb
FT
805 /*
806 * Attempt to establish an I_T_L nexus here.
807 * On success, instance->hostdata->connected is set.
808 * On failure, we must add the command back to the
809 * issue queue so we can keep trying.
810 */
811 /*
812 * REQUEST SENSE commands are issued without tagged
813 * queueing, even on SCSI-II devices because the
814 * contingent allegiance condition exists for the
815 * entire unit.
816 */
11d2f63b 817
ccf6efd7 818 if (!NCR5380_select(instance, cmd)) {
707d62b3 819 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
f27db8eb
FT
820 } else {
821 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
822 "main: select failed, returning %p to queue\n", cmd);
823 requeue_cmd(instance, cmd);
824 }
825 }
e4dec680 826 if (hostdata->connected && !hostdata->dma_len) {
b746545f 827 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
1da177e4 828 NCR5380_information_transfer(instance);
1da177e4 829 done = 0;
1d3db59d 830 }
0a4e3612
FT
831 spin_unlock_irq(&hostdata->lock);
832 if (!done)
833 cond_resched();
1da177e4 834 } while (!done);
1da177e4
LT
835}
836
837#ifndef DONT_USE_INTR
838
839/**
cd400825
FT
840 * NCR5380_intr - generic NCR5380 irq handler
841 * @irq: interrupt number
842 * @dev_id: device info
843 *
844 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
845 * from the disconnected queue, and restarting NCR5380_main()
846 * as required.
847 *
848 * The chip can assert IRQ in any of six different conditions. The IRQ flag
849 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
850 * Three of these six conditions are latched in the Bus and Status Register:
851 * - End of DMA (cleared by ending DMA Mode)
852 * - Parity error (cleared by reading RPIR)
853 * - Loss of BSY (cleared by reading RPIR)
854 * Two conditions have flag bits that are not latched:
855 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
856 * - Bus reset (non-maskable)
857 * The remaining condition has no flag bit at all:
858 * - Selection/reselection
859 *
860 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
861 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
862 * claimed that "the design of the [DP8490] interrupt logic ensures
863 * interrupts will not be lost (they can be on the DP5380)."
864 * The L5380/53C80 datasheet from LOGIC Devices has more details.
865 *
866 * Checking for bus reset by reading RST is futile because of interrupt
867 * latency, but a bus reset will reset chip logic. Checking for parity error
868 * is unnecessary because that interrupt is never enabled. A Loss of BSY
869 * condition will clear DMA Mode. We can tell when this occurs because the
870 * the Busy Monitor interrupt is enabled together with DMA Mode.
1da177e4
LT
871 */
872
cd400825 873static irqreturn_t NCR5380_intr(int irq, void *dev_id)
1da177e4 874{
baa9aac6 875 struct Scsi_Host *instance = dev_id;
cd400825
FT
876 struct NCR5380_hostdata *hostdata = shost_priv(instance);
877 int handled = 0;
1da177e4
LT
878 unsigned char basr;
879 unsigned long flags;
880
11d2f63b 881 spin_lock_irqsave(&hostdata->lock, flags);
cd400825
FT
882
883 basr = NCR5380_read(BUS_AND_STATUS_REG);
884 if (basr & BASR_IRQ) {
885 unsigned char mr = NCR5380_read(MODE_REG);
886 unsigned char sr = NCR5380_read(STATUS_REG);
887
b746545f
FT
888 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
889 irq, basr, sr, mr);
1da177e4 890
cd400825
FT
891 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
892 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
893 /* Probably reselected */
894 NCR5380_write(SELECT_ENABLE_REG, 0);
895 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
896
b746545f 897 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
cd400825
FT
898
899 if (!hostdata->connected) {
900 NCR5380_reselect(instance);
901 queue_work(hostdata->work_q, &hostdata->main_task);
1da177e4 902 }
cd400825
FT
903 if (!hostdata->connected)
904 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
905 } else {
906 /* Probably Bus Reset */
907 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
908
b746545f 909 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
cd400825
FT
910 }
911 handled = 1;
912 } else {
913 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
914 }
915
11d2f63b 916 spin_unlock_irqrestore(&hostdata->lock, flags);
cd400825
FT
917
918 return IRQ_RETVAL(handled);
1da177e4
LT
919}
920
aff0cf9a 921#endif
1da177e4 922
aff0cf9a 923/*
710ddd0d 924 * Function : int NCR5380_select(struct Scsi_Host *instance,
594d4ba3 925 * struct scsi_cmnd *cmd)
1da177e4
LT
926 *
927 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
594d4ba3
FT
928 * including ARBITRATION, SELECTION, and initial message out for
929 * IDENTIFY and queue messages.
1da177e4 930 *
aff0cf9a 931 * Inputs : instance - instantiation of the 5380 driver on which this
594d4ba3 932 * target lives, cmd - SCSI command to execute.
aff0cf9a 933 *
707d62b3
FT
934 * Returns cmd if selection failed but should be retried,
935 * NULL if selection failed and should not be retried, or
936 * NULL if selection succeeded (hostdata->connected == cmd).
1da177e4 937 *
aff0cf9a 938 * Side effects :
594d4ba3
FT
939 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
940 * with registers as they should have been on entry - ie
941 * SELECT_ENABLE will be set appropriately, the NCR5380
942 * will cease to drive any SCSI bus signals.
1da177e4 943 *
594d4ba3
FT
944 * If successful : I_T_L or I_T_L_Q nexus will be established,
945 * instance->connected will be set to cmd.
946 * SELECT interrupt will be disabled.
1da177e4 947 *
594d4ba3
FT
948 * If failed (no target) : cmd->scsi_done() will be called, and the
949 * cmd->result host byte set to DID_BAD_TARGET.
1da177e4 950 */
aff0cf9a 951
707d62b3
FT
952static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
953 struct scsi_cmnd *cmd)
1da177e4 954{
e8a60144 955 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
956 unsigned char tmp[3], phase;
957 unsigned char *data;
958 int len;
1da177e4 959 int err;
1da177e4 960
1da177e4 961 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
b746545f
FT
962 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
963 instance->this_id);
1da177e4 964
707d62b3
FT
965 /*
966 * Arbitration and selection phases are slow and involve dropping the
967 * lock, so we have to watch out for EH. An exception handler may
968 * change 'selecting' to NULL. This function will then return NULL
969 * so that the caller will forget about 'cmd'. (During information
970 * transfer phases, EH may change 'connected' to NULL.)
971 */
972 hostdata->selecting = cmd;
973
aff0cf9a
FT
974 /*
975 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1da177e4
LT
976 * data bus during SELECTION.
977 */
978
979 NCR5380_write(TARGET_COMMAND_REG, 0);
980
aff0cf9a 981 /*
1da177e4
LT
982 * Start arbitration.
983 */
984
985 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
986 NCR5380_write(MODE_REG, MR_ARBITRATE);
987
55500d9b
FT
988 /* The chip now waits for BUS FREE phase. Then after the 800 ns
989 * Bus Free Delay, arbitration will begin.
990 */
1da177e4 991
11d2f63b 992 spin_unlock_irq(&hostdata->lock);
b32ade12
FT
993 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
994 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
995 ICR_ARBITRATION_PROGRESS, HZ);
11d2f63b 996 spin_lock_irq(&hostdata->lock);
b32ade12
FT
997 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
998 /* Reselection interrupt */
707d62b3 999 goto out;
b32ade12 1000 }
ccf6efd7
FT
1001 if (!hostdata->selecting) {
1002 /* Command was aborted */
1003 NCR5380_write(MODE_REG, MR_BASE);
1004 goto out;
1005 }
b32ade12
FT
1006 if (err < 0) {
1007 NCR5380_write(MODE_REG, MR_BASE);
1008 shost_printk(KERN_ERR, instance,
1009 "select: arbitration timeout\n");
707d62b3 1010 goto out;
1da177e4 1011 }
11d2f63b 1012 spin_unlock_irq(&hostdata->lock);
1da177e4 1013
55500d9b 1014 /* The SCSI-2 arbitration delay is 2.4 us */
1da177e4
LT
1015 udelay(3);
1016
1017 /* Check for lost arbitration */
0d2cf867
FT
1018 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1019 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1020 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1da177e4 1021 NCR5380_write(MODE_REG, MR_BASE);
b746545f 1022 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
11d2f63b 1023 spin_lock_irq(&hostdata->lock);
707d62b3 1024 goto out;
1da177e4 1025 }
cf13b083
FT
1026
1027 /* After/during arbitration, BSY should be asserted.
1028 * IBM DPES-31080 Version S31Q works now
1029 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1030 */
1031 NCR5380_write(INITIATOR_COMMAND_REG,
1032 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
1da177e4 1033
aff0cf9a
FT
1034 /*
1035 * Again, bus clear + bus settle time is 1.2us, however, this is
1da177e4
LT
1036 * a minimum so we'll udelay ceil(1.2)
1037 */
1038
9c3f0e2b
FT
1039 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1040 udelay(15);
1041 else
1042 udelay(2);
1da177e4 1043
11d2f63b
FT
1044 spin_lock_irq(&hostdata->lock);
1045
72064a78
FT
1046 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1047 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
707d62b3
FT
1048 goto out;
1049
1050 if (!hostdata->selecting) {
1051 NCR5380_write(MODE_REG, MR_BASE);
1052 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1053 goto out;
1054 }
72064a78 1055
b746545f 1056 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
1da177e4 1057
aff0cf9a
FT
1058 /*
1059 * Now that we have won arbitration, start Selection process, asserting
1da177e4
LT
1060 * the host and target ID's on the SCSI bus.
1061 */
1062
3d07d22b 1063 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
1da177e4 1064
aff0cf9a 1065 /*
1da177e4
LT
1066 * Raise ATN while SEL is true before BSY goes false from arbitration,
1067 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1068 * phase immediately after selection.
1069 */
1070
3d07d22b
FT
1071 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1072 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1da177e4
LT
1073 NCR5380_write(MODE_REG, MR_BASE);
1074
aff0cf9a 1075 /*
1da177e4
LT
1076 * Reselect interrupts must be turned off prior to the dropping of BSY,
1077 * otherwise we will trigger an interrupt.
1078 */
1079 NCR5380_write(SELECT_ENABLE_REG, 0);
1080
11d2f63b
FT
1081 spin_unlock_irq(&hostdata->lock);
1082
1da177e4 1083 /*
aff0cf9a 1084 * The initiator shall then wait at least two deskew delays and release
1da177e4
LT
1085 * the BSY signal.
1086 */
0d2cf867 1087 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1da177e4
LT
1088
1089 /* Reset BSY */
3d07d22b
FT
1090 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1091 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1da177e4 1092
aff0cf9a 1093 /*
1da177e4 1094 * Something weird happens when we cease to drive BSY - looks
aff0cf9a 1095 * like the board/chip is letting us do another read before the
1da177e4
LT
1096 * appropriate propagation delay has expired, and we're confusing
1097 * a BSY signal from ourselves as the target's response to SELECTION.
1098 *
1099 * A small delay (the 'C++' frontend breaks the pipeline with an
1100 * unnecessary jump, making it work on my 386-33/Trantor T128, the
aff0cf9a
FT
1101 * tighter 'C' code breaks and requires this) solves the problem -
1102 * the 1 us delay is arbitrary, and only used because this delay will
1103 * be the same on other platforms and since it works here, it should
1da177e4
LT
1104 * work there.
1105 *
1106 * wingel suggests that this could be due to failing to wait
1107 * one deskew delay.
1108 */
1109
1110 udelay(1);
1111
b746545f 1112 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
1da177e4 1113
aff0cf9a
FT
1114 /*
1115 * The SCSI specification calls for a 250 ms timeout for the actual
1da177e4
LT
1116 * selection.
1117 */
1118
ae753a33
FT
1119 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1120 msecs_to_jiffies(250));
1da177e4 1121
1da177e4 1122 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
11d2f63b 1123 spin_lock_irq(&hostdata->lock);
1da177e4
LT
1124 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1125 NCR5380_reselect(instance);
cd400825
FT
1126 if (!hostdata->connected)
1127 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
6a6ff4ac 1128 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
707d62b3 1129 goto out;
1da177e4 1130 }
ae753a33
FT
1131
1132 if (err < 0) {
11d2f63b 1133 spin_lock_irq(&hostdata->lock);
ae753a33 1134 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
ae753a33 1135 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
707d62b3
FT
1136 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1137 if (hostdata->selecting) {
1138 cmd->result = DID_BAD_TARGET << 16;
1139 complete_cmd(instance, cmd);
1140 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1141 cmd = NULL;
1142 }
1143 goto out;
ae753a33
FT
1144 }
1145
aff0cf9a
FT
1146 /*
1147 * No less than two deskew delays after the initiator detects the
1148 * BSY signal is true, it shall release the SEL signal and may
1da177e4
LT
1149 * change the DATA BUS. -wingel
1150 */
1151
1152 udelay(1);
1153
1154 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1155
1da177e4 1156 /*
aff0cf9a 1157 * Since we followed the SCSI spec, and raised ATN while SEL
1da177e4
LT
1158 * was true but before BSY was false during selection, the information
1159 * transfer phase should be a MESSAGE OUT phase so that we can send the
1160 * IDENTIFY message.
aff0cf9a 1161 *
1da177e4
LT
1162 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1163 * message (2 bytes) with a tag ID that we increment with every command
1164 * until it wraps back to 0.
1165 *
1166 * XXX - it turns out that there are some broken SCSI-II devices,
594d4ba3
FT
1167 * which claim to support tagged queuing but fail when more than
1168 * some number of commands are issued at once.
1da177e4
LT
1169 */
1170
1171 /* Wait for start of REQ/ACK handshake */
1172
1da177e4 1173 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
11d2f63b 1174 spin_lock_irq(&hostdata->lock);
1cc160e1 1175 if (err < 0) {
55500d9b
FT
1176 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1177 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1da177e4 1178 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
707d62b3
FT
1179 goto out;
1180 }
1181 if (!hostdata->selecting) {
1182 do_abort(instance);
1183 goto out;
1da177e4
LT
1184 }
1185
b746545f
FT
1186 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1187 scmd_id(cmd));
22f5f10d 1188 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
1da177e4
LT
1189
1190 len = 1;
1191 cmd->tag = 0;
1192
1193 /* Send message(s) */
1194 data = tmp;
1195 phase = PHASE_MSGOUT;
1196 NCR5380_transfer_pio(instance, &phase, &len, &data);
b746545f 1197 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
1da177e4 1198 /* XXX need to handle errors here */
11d2f63b 1199
1da177e4 1200 hostdata->connected = cmd;
3d07d22b 1201 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
1da177e4 1202
28424d3a 1203 initialize_SCp(cmd);
1da177e4 1204
707d62b3
FT
1205 cmd = NULL;
1206
1207out:
1208 if (!hostdata->selecting)
1209 return NULL;
1210 hostdata->selecting = NULL;
1211 return cmd;
1da177e4
LT
1212}
1213
aff0cf9a
FT
1214/*
1215 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
594d4ba3 1216 * unsigned char *phase, int *count, unsigned char **data)
1da177e4
LT
1217 *
1218 * Purpose : transfers data in given phase using polled I/O
1219 *
aff0cf9a 1220 * Inputs : instance - instance of driver, *phase - pointer to
594d4ba3
FT
1221 * what phase is expected, *count - pointer to number of
1222 * bytes to transfer, **data - pointer to data pointer.
aff0cf9a 1223 *
1da177e4 1224 * Returns : -1 when different phase is entered without transferring
0d2cf867 1225 * maximum number of bytes, 0 if all bytes are transferred or exit
594d4ba3 1226 * is in same phase.
1da177e4 1227 *
594d4ba3 1228 * Also, *phase, *count, *data are modified in place.
1da177e4
LT
1229 *
1230 * XXX Note : handling for bus free may be useful.
1231 */
1232
1233/*
aff0cf9a 1234 * Note : this code is not as quick as it could be, however it
1da177e4
LT
1235 * IS 100% reliable, and for the actual data transfer where speed
1236 * counts, we will always do a pseudo DMA or DMA transfer.
1237 */
1238
0d2cf867
FT
1239static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1240 unsigned char *phase, int *count,
1241 unsigned char **data)
1242{
1da177e4
LT
1243 unsigned char p = *phase, tmp;
1244 int c = *count;
1245 unsigned char *d = *data;
1da177e4 1246
aff0cf9a
FT
1247 /*
1248 * The NCR5380 chip will only drive the SCSI bus when the
1da177e4
LT
1249 * phase specified in the appropriate bits of the TARGET COMMAND
1250 * REGISTER match the STATUS REGISTER
1251 */
1252
0d2cf867 1253 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1da177e4 1254
1da177e4 1255 do {
aff0cf9a
FT
1256 /*
1257 * Wait for assertion of REQ, after which the phase bits will be
1258 * valid
1da177e4
LT
1259 */
1260
686f3990 1261 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1da177e4 1262 break;
1da177e4 1263
b746545f 1264 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
1da177e4
LT
1265
1266 /* Check for phase mismatch */
686f3990 1267 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
b746545f
FT
1268 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1269 NCR5380_dprint_phase(NDEBUG_PIO, instance);
1da177e4
LT
1270 break;
1271 }
0d2cf867 1272
1da177e4
LT
1273 /* Do actual transfer from SCSI bus to / from memory */
1274 if (!(p & SR_IO))
1275 NCR5380_write(OUTPUT_DATA_REG, *d);
1276 else
1277 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1278
1279 ++d;
1280
aff0cf9a 1281 /*
1da177e4
LT
1282 * The SCSI standard suggests that in MSGOUT phase, the initiator
1283 * should drop ATN on the last byte of the message phase
1284 * after REQ has been asserted for the handshake but before
1285 * the initiator raises ACK.
1286 */
1287
1288 if (!(p & SR_IO)) {
1289 if (!((p & SR_MSG) && c > 1)) {
1290 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1291 NCR5380_dprint(NDEBUG_PIO, instance);
3d07d22b
FT
1292 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1293 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1da177e4 1294 } else {
3d07d22b
FT
1295 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1296 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1da177e4 1297 NCR5380_dprint(NDEBUG_PIO, instance);
3d07d22b
FT
1298 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1299 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1da177e4
LT
1300 }
1301 } else {
1302 NCR5380_dprint(NDEBUG_PIO, instance);
1303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1304 }
1305
a2edc4a6
FT
1306 if (NCR5380_poll_politely(instance,
1307 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1308 break;
1309
b746545f 1310 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
1da177e4
LT
1311
1312/*
aff0cf9a
FT
1313 * We have several special cases to consider during REQ/ACK handshaking :
1314 * 1. We were in MSGOUT phase, and we are on the last byte of the
594d4ba3 1315 * message. ATN must be dropped as ACK is dropped.
1da177e4 1316 *
aff0cf9a 1317 * 2. We are in a MSGIN phase, and we are on the last byte of the
594d4ba3
FT
1318 * message. We must exit with ACK asserted, so that the calling
1319 * code may raise ATN before dropping ACK to reject the message.
1da177e4
LT
1320 *
1321 * 3. ACK and ATN are clear and the target may proceed as normal.
1322 */
1323 if (!(p == PHASE_MSGIN && c == 1)) {
1324 if (p == PHASE_MSGOUT && c > 1)
1325 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1326 else
1327 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1328 }
1329 } while (--c);
1330
b746545f 1331 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
1da177e4
LT
1332
1333 *count = c;
1334 *data = d;
1335 tmp = NCR5380_read(STATUS_REG);
a2edc4a6
FT
1336 /* The phase read from the bus is valid if either REQ is (already)
1337 * asserted or if ACK hasn't been released yet. The latter applies if
1338 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1339 */
1340 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
1da177e4
LT
1341 *phase = tmp & PHASE_MASK;
1342 else
1343 *phase = PHASE_UNKNOWN;
1344
1345 if (!c || (*phase == p))
1346 return 0;
1347 else
1348 return -1;
1349}
1350
1351/**
636b1ec8
FT
1352 * do_reset - issue a reset command
1353 * @instance: adapter to reset
1da177e4 1354 *
636b1ec8
FT
1355 * Issue a reset sequence to the NCR5380 and try and get the bus
1356 * back into sane shape.
1da177e4 1357 *
636b1ec8
FT
1358 * This clears the reset interrupt flag because there may be no handler for
1359 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1360 * been installed. And when in EH we may have released the ST DMA interrupt.
1da177e4 1361 */
aff0cf9a 1362
54d8fe44
FT
1363static void do_reset(struct Scsi_Host *instance)
1364{
636b1ec8
FT
1365 unsigned long flags;
1366
1367 local_irq_save(flags);
1368 NCR5380_write(TARGET_COMMAND_REG,
1369 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1da177e4 1370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
636b1ec8 1371 udelay(50);
1da177e4 1372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
636b1ec8
FT
1373 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1374 local_irq_restore(flags);
1da177e4
LT
1375}
1376
80d3eb6d
FT
1377/**
1378 * do_abort - abort the currently established nexus by going to
1379 * MESSAGE OUT phase and sending an ABORT message.
1380 * @instance: relevant scsi host instance
1da177e4 1381 *
80d3eb6d 1382 * Returns 0 on success, -1 on failure.
1da177e4
LT
1383 */
1384
54d8fe44
FT
1385static int do_abort(struct Scsi_Host *instance)
1386{
1da177e4
LT
1387 unsigned char *msgptr, phase, tmp;
1388 int len;
1389 int rc;
1da177e4
LT
1390
1391 /* Request message out phase */
1392 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1393
aff0cf9a
FT
1394 /*
1395 * Wait for the target to indicate a valid phase by asserting
1396 * REQ. Once this happens, we'll have either a MSGOUT phase
1397 * and can immediately send the ABORT message, or we'll have some
1da177e4 1398 * other phase and will have to source/sink data.
aff0cf9a 1399 *
1da177e4
LT
1400 * We really don't care what value was on the bus or what value
1401 * the target sees, so we just handshake.
1402 */
1403
80d3eb6d 1404 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1cc160e1 1405 if (rc < 0)
80d3eb6d 1406 goto timeout;
1da177e4 1407
f35d3474 1408 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
aff0cf9a 1409
1da177e4
LT
1410 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1411
f35d3474 1412 if (tmp != PHASE_MSGOUT) {
0d2cf867
FT
1413 NCR5380_write(INITIATOR_COMMAND_REG,
1414 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
54d8fe44 1415 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1cc160e1 1416 if (rc < 0)
80d3eb6d
FT
1417 goto timeout;
1418 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1da177e4 1419 }
0d2cf867 1420
1da177e4
LT
1421 tmp = ABORT;
1422 msgptr = &tmp;
1423 len = 1;
1424 phase = PHASE_MSGOUT;
54d8fe44 1425 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
1da177e4
LT
1426
1427 /*
1428 * If we got here, and the command completed successfully,
1429 * we're about to go into bus free state.
1430 */
1431
1432 return len ? -1 : 0;
80d3eb6d
FT
1433
1434timeout:
1435 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1436 return -1;
1da177e4
LT
1437}
1438
e4dec680 1439#if defined(PSEUDO_DMA)
aff0cf9a
FT
1440/*
1441 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
594d4ba3 1442 * unsigned char *phase, int *count, unsigned char **data)
1da177e4
LT
1443 *
1444 * Purpose : transfers data in given phase using either real
594d4ba3 1445 * or pseudo DMA.
1da177e4 1446 *
aff0cf9a 1447 * Inputs : instance - instance of driver, *phase - pointer to
594d4ba3
FT
1448 * what phase is expected, *count - pointer to number of
1449 * bytes to transfer, **data - pointer to data pointer.
aff0cf9a 1450 *
1da177e4 1451 * Returns : -1 when different phase is entered without transferring
594d4ba3
FT
1452 * maximum number of bytes, 0 if all bytes or transferred or exit
1453 * is in same phase.
1da177e4 1454 *
594d4ba3 1455 * Also, *phase, *count, *data are modified in place.
1da177e4
LT
1456 */
1457
1458
0d2cf867
FT
1459static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1460 unsigned char *phase, int *count,
1461 unsigned char **data)
1462{
1463 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
1464 register int c = *count;
1465 register unsigned char p = *phase;
1466 register unsigned char *d = *data;
1467 unsigned char tmp;
1468 int foo;
1da177e4 1469
1da177e4
LT
1470 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1471 *phase = tmp;
1472 return -1;
1473 }
1da177e4
LT
1474
1475 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1476
1da177e4
LT
1477 /*
1478 * Note : on my sample board, watch-dog timeouts occurred when interrupts
aff0cf9a 1479 * were not disabled for the duration of a single DMA transfer, from
1da177e4
LT
1480 * before the setting of DMA mode to after transfer of the last byte.
1481 */
1482
55181be8 1483 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
cd400825
FT
1484 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1485 MR_ENABLE_EOP_INTR);
1da177e4 1486 else
cd400825 1487 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
1da177e4 1488
52a6a1cb 1489 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
1da177e4 1490
aff0cf9a 1491 /*
594d4ba3
FT
1492 * On the PAS16 at least I/O recovery delays are not needed here.
1493 * Everyone else seems to want them.
1da177e4
LT
1494 */
1495
1496 if (p & SR_IO) {
1497 io_recovery_delay(1);
1498 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1499 } else {
1500 io_recovery_delay(1);
1501 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1502 io_recovery_delay(1);
1503 NCR5380_write(START_DMA_SEND_REG, 0);
1504 io_recovery_delay(1);
1505 }
1506
1da177e4 1507/*
e4dec680 1508 * A note regarding the DMA errata workarounds for early NMOS silicon.
c16df32e
FT
1509 *
1510 * For DMA sends, we want to wait until the last byte has been
1511 * transferred out over the bus before we turn off DMA mode. Alas, there
1512 * seems to be no terribly good way of doing this on a 5380 under all
1513 * conditions. For non-scatter-gather operations, we can wait until REQ
1514 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1515 * are nastier, since the device will be expecting more data than we
1516 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1517 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1518 * why this signal was added to the newer chips) but on the older 538[01]
1519 * this signal does not exist. The workaround for this lack is a watchdog;
1520 * we bail out of the wait-loop after a modest amount of wait-time if
1521 * the usual exit conditions are not met. Not a terribly clean or
1522 * correct solution :-%
1523 *
1524 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1525 * If the chip is in DMA receive mode, it will respond to a target's
1526 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1527 * ACK, even if it has _already_ been notified by the DMA controller that
1528 * the current DMA transfer has completed! If the NCR5380 is then taken
1529 * out of DMA mode, this already-acknowledged byte is lost. This is
1530 * not a problem for "one DMA transfer per READ command", because
1531 * the situation will never arise... either all of the data is DMA'ed
1532 * properly, or the target switches to MESSAGE IN phase to signal a
1533 * disconnection (either operation bringing the DMA to a clean halt).
1534 * However, in order to handle scatter-receive, we must work around the
e4dec680 1535 * problem. The chosen fix is to DMA fewer bytes, then check for the
c16df32e
FT
1536 * condition before taking the NCR5380 out of DMA mode. One or two extra
1537 * bytes are transferred via PIO as necessary to fill out the original
1538 * request.
1da177e4
LT
1539 */
1540
1da177e4 1541 if (p & SR_IO) {
55181be8
FT
1542 foo = NCR5380_pread(instance, d,
1543 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1544 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
1da177e4 1545 /*
e4dec680 1546 * The workaround was to transfer fewer bytes than we
aff0cf9a 1547 * intended to with the pseudo-DMA read function, wait for
1da177e4
LT
1548 * the chip to latch the last byte, read it, and then disable
1549 * pseudo-DMA mode.
aff0cf9a 1550 *
1da177e4
LT
1551 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1552 * REQ is deasserted when ACK is asserted, and not reasserted
1553 * until ACK goes false. Since the NCR5380 won't lower ACK
1554 * until DACK is asserted, which won't happen unless we twiddle
aff0cf9a
FT
1555 * the DMA port or we take the NCR5380 out of DMA mode, we
1556 * can guarantee that we won't handshake another extra
1da177e4
LT
1557 * byte.
1558 */
1559
55181be8
FT
1560 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1561 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1562 foo = -1;
1563 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
1564 }
1565 if (NCR5380_poll_politely(instance, STATUS_REG,
1566 SR_REQ, 0, HZ) < 0) {
1567 foo = -1;
1568 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1da177e4 1569 }
55181be8 1570 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1da177e4 1571 }
1da177e4 1572 } else {
1da177e4 1573 foo = NCR5380_pwrite(instance, d, c);
55181be8 1574 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
1da177e4 1575 /*
aff0cf9a
FT
1576 * Wait for the last byte to be sent. If REQ is being asserted for
1577 * the byte we're interested, we'll ACK it and it will go false.
1da177e4 1578 */
55181be8
FT
1579 if (NCR5380_poll_politely2(instance,
1580 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1581 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1582 foo = -1;
1583 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
1da177e4
LT
1584 }
1585 }
1da177e4
LT
1586 }
1587 NCR5380_write(MODE_REG, MR_BASE);
1588 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
cd400825 1589 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1da177e4
LT
1590 *data = d + c;
1591 *count = 0;
1592 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1da177e4 1593 return foo;
1da177e4 1594}
e4dec680 1595#endif /* PSEUDO_DMA */
1da177e4
LT
1596
1597/*
1598 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1599 *
aff0cf9a 1600 * Purpose : run through the various SCSI phases and do as the target
594d4ba3
FT
1601 * directs us to. Operates on the currently connected command,
1602 * instance->connected.
1da177e4
LT
1603 *
1604 * Inputs : instance, instance for which we are doing commands
1605 *
aff0cf9a 1606 * Side effects : SCSI things happen, the disconnected queue will be
594d4ba3
FT
1607 * modified if a command disconnects, *instance->connected will
1608 * change.
1da177e4 1609 *
aff0cf9a 1610 * XXX Note : we need to watch for bus free or a reset condition here
594d4ba3 1611 * to recover from an unexpected bus free condition.
1da177e4
LT
1612 */
1613
0d2cf867
FT
1614static void NCR5380_information_transfer(struct Scsi_Host *instance)
1615{
e8a60144 1616 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
1617 unsigned char msgout = NOP;
1618 int sink = 0;
1619 int len;
1da177e4 1620 int transfersize;
1da177e4
LT
1621 unsigned char *data;
1622 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
11d2f63b 1623 struct scsi_cmnd *cmd;
1da177e4 1624
11d2f63b 1625 while ((cmd = hostdata->connected)) {
32b26a10
FT
1626 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1627
1da177e4
LT
1628 tmp = NCR5380_read(STATUS_REG);
1629 /* We only have a valid SCSI phase when REQ is asserted */
1630 if (tmp & SR_REQ) {
1631 phase = (tmp & PHASE_MASK);
1632 if (phase != old_phase) {
1633 old_phase = phase;
1634 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1635 }
1636 if (sink && (phase != PHASE_MSGOUT)) {
1637 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1638
3d07d22b
FT
1639 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1640 ICR_ASSERT_ACK);
0d2cf867
FT
1641 while (NCR5380_read(STATUS_REG) & SR_REQ)
1642 ;
3d07d22b
FT
1643 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1644 ICR_ASSERT_ATN);
1da177e4
LT
1645 sink = 0;
1646 continue;
1647 }
0d2cf867 1648
1da177e4 1649 switch (phase) {
1da177e4
LT
1650 case PHASE_DATAOUT:
1651#if (NDEBUG & NDEBUG_NO_DATAOUT)
6a6ff4ac 1652 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1da177e4
LT
1653 sink = 1;
1654 do_abort(instance);
1655 cmd->result = DID_ERROR << 16;
677e0194 1656 complete_cmd(instance, cmd);
dc183965 1657 hostdata->connected = NULL;
1da177e4
LT
1658 return;
1659#endif
bf1a0c6f 1660 case PHASE_DATAIN:
aff0cf9a 1661 /*
1da177e4
LT
1662 * If there is no room left in the current buffer in the
1663 * scatter-gather list, move onto the next one.
1664 */
1665
1666 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1667 ++cmd->SCp.buffer;
1668 --cmd->SCp.buffers_residual;
1669 cmd->SCp.this_residual = cmd->SCp.buffer->length;
45711f1a 1670 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
b746545f
FT
1671 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1672 cmd->SCp.this_residual,
1673 cmd->SCp.buffers_residual);
1da177e4 1674 }
0d2cf867 1675
1da177e4 1676 /*
aff0cf9a 1677 * The preferred transfer method is going to be
1da177e4
LT
1678 * PSEUDO-DMA for systems that are strictly PIO,
1679 * since we can let the hardware do the handshaking.
1680 *
1681 * For this to work, we need to know the transfersize
1682 * ahead of time, since the pseudo-DMA code will sit
1683 * in an unconditional loop.
1684 */
1685
e4dec680 1686#if defined(PSEUDO_DMA)
ff3d4578 1687 transfersize = 0;
7e9ec8d9 1688 if (!cmd->device->borken)
ff3d4578
FT
1689 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1690
1691 if (transfersize) {
1da177e4 1692 len = transfersize;
0d2cf867
FT
1693 if (NCR5380_transfer_dma(instance, &phase,
1694 &len, (unsigned char **)&cmd->SCp.ptr)) {
1da177e4 1695 /*
0d2cf867
FT
1696 * If the watchdog timer fires, all future
1697 * accesses to this device will use the
1698 * polled-IO.
1da177e4 1699 */
017560fc 1700 scmd_printk(KERN_INFO, cmd,
0d2cf867 1701 "switching to slow handshake\n");
1da177e4 1702 cmd->device->borken = 1;
1da177e4
LT
1703 sink = 1;
1704 do_abort(instance);
1705 cmd->result = DID_ERROR << 16;
1da177e4
LT
1706 /* XXX - need to source or sink data here, as appropriate */
1707 } else
1708 cmd->SCp.this_residual -= transfersize - len;
1709 } else
e4dec680 1710#endif /* PSEUDO_DMA */
11d2f63b 1711 {
1678847e
FT
1712 /* Break up transfer into 3 ms chunks,
1713 * presuming 6 accesses per handshake.
1714 */
1715 transfersize = min((unsigned long)cmd->SCp.this_residual,
1716 hostdata->accesses_per_ms / 2);
1717 len = transfersize;
1718 NCR5380_transfer_pio(instance, &phase, &len,
3d07d22b 1719 (unsigned char **)&cmd->SCp.ptr);
1678847e 1720 cmd->SCp.this_residual -= transfersize - len;
11d2f63b 1721 }
1678847e 1722 return;
1da177e4
LT
1723 case PHASE_MSGIN:
1724 len = 1;
1725 data = &tmp;
1726 NCR5380_transfer_pio(instance, &phase, &len, &data);
1727 cmd->SCp.Message = tmp;
1728
1729 switch (tmp) {
1da177e4
LT
1730 case ABORT:
1731 case COMMAND_COMPLETE:
1732 /* Accept message by clearing ACK */
1733 sink = 1;
1734 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
0d3d9a42
FT
1735 dsprintk(NDEBUG_QUEUES, instance,
1736 "COMMAND COMPLETE %p target %d lun %llu\n",
1737 cmd, scmd_id(cmd), cmd->device->lun);
1738
1da177e4 1739 hostdata->connected = NULL;
1da177e4 1740
f27db8eb
FT
1741 cmd->result &= ~0xffff;
1742 cmd->result |= cmd->SCp.Status;
1743 cmd->result |= cmd->SCp.Message << 8;
28424d3a 1744
f27db8eb 1745 if (cmd->cmnd[0] == REQUEST_SENSE)
677e0194 1746 complete_cmd(instance, cmd);
f27db8eb
FT
1747 else {
1748 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1749 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1750 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1751 cmd);
1752 list_add_tail(&ncmd->list,
1753 &hostdata->autosense);
1754 } else
1755 complete_cmd(instance, cmd);
1da177e4
LT
1756 }
1757
aff0cf9a
FT
1758 /*
1759 * Restore phase bits to 0 so an interrupted selection,
1da177e4
LT
1760 * arbitration can resume.
1761 */
1762 NCR5380_write(TARGET_COMMAND_REG, 0);
72064a78
FT
1763
1764 /* Enable reselect interrupts */
1765 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1da177e4
LT
1766 return;
1767 case MESSAGE_REJECT:
1768 /* Accept message by clearing ACK */
1769 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1770 switch (hostdata->last_message) {
1771 case HEAD_OF_QUEUE_TAG:
1772 case ORDERED_QUEUE_TAG:
1773 case SIMPLE_QUEUE_TAG:
1774 cmd->device->simple_tags = 0;
9cb78c16 1775 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1da177e4
LT
1776 break;
1777 default:
1778 break;
1779 }
340b9612 1780 break;
0d2cf867
FT
1781 case DISCONNECT:
1782 /* Accept message by clearing ACK */
1783 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1784 hostdata->connected = NULL;
1785 list_add(&ncmd->list, &hostdata->disconnected);
1786 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1787 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1788 cmd, scmd_id(cmd), cmd->device->lun);
0d3d9a42 1789
0d2cf867
FT
1790 /*
1791 * Restore phase bits to 0 so an interrupted selection,
1792 * arbitration can resume.
1793 */
1794 NCR5380_write(TARGET_COMMAND_REG, 0);
1da177e4 1795
0d2cf867
FT
1796 /* Enable reselect interrupts */
1797 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1798 return;
aff0cf9a 1799 /*
1da177e4 1800 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
aff0cf9a 1801 * operation, in violation of the SCSI spec so we can safely
1da177e4
LT
1802 * ignore SAVE/RESTORE pointers calls.
1803 *
aff0cf9a 1804 * Unfortunately, some disks violate the SCSI spec and
1da177e4 1805 * don't issue the required SAVE_POINTERS message before
aff0cf9a 1806 * disconnecting, and we have to break spec to remain
1da177e4
LT
1807 * compatible.
1808 */
1809 case SAVE_POINTERS:
1810 case RESTORE_POINTERS:
1811 /* Accept message by clearing ACK */
1812 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1813 break;
1814 case EXTENDED_MESSAGE:
c16df32e
FT
1815 /*
1816 * Start the message buffer with the EXTENDED_MESSAGE
1817 * byte, since spi_print_msg() wants the whole thing.
1818 */
1da177e4
LT
1819 extended_msg[0] = EXTENDED_MESSAGE;
1820 /* Accept first byte by clearing ACK */
1821 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
11d2f63b
FT
1822
1823 spin_unlock_irq(&hostdata->lock);
1824
b746545f 1825 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
1da177e4
LT
1826
1827 len = 2;
1828 data = extended_msg + 1;
1829 phase = PHASE_MSGIN;
1830 NCR5380_transfer_pio(instance, &phase, &len, &data);
b746545f
FT
1831 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1832 (int)extended_msg[1],
1833 (int)extended_msg[2]);
1da177e4 1834
e0783ed3
FT
1835 if (!len && extended_msg[1] > 0 &&
1836 extended_msg[1] <= sizeof(extended_msg) - 2) {
1da177e4
LT
1837 /* Accept third byte by clearing ACK */
1838 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1839 len = extended_msg[1] - 1;
1840 data = extended_msg + 3;
1841 phase = PHASE_MSGIN;
1842
1843 NCR5380_transfer_pio(instance, &phase, &len, &data);
b746545f
FT
1844 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1845 len);
1da177e4
LT
1846
1847 switch (extended_msg[2]) {
1848 case EXTENDED_SDTR:
1849 case EXTENDED_WDTR:
1850 case EXTENDED_MODIFY_DATA_POINTER:
1851 case EXTENDED_EXTENDED_IDENTIFY:
1852 tmp = 0;
1853 }
1854 } else if (len) {
6a6ff4ac 1855 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
1da177e4
LT
1856 tmp = 0;
1857 } else {
6a6ff4ac
FT
1858 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1859 extended_msg[2], extended_msg[1]);
1da177e4
LT
1860 tmp = 0;
1861 }
11d2f63b
FT
1862
1863 spin_lock_irq(&hostdata->lock);
1864 if (!hostdata->connected)
1865 return;
1866
1da177e4
LT
1867 /* Fall through to reject message */
1868
aff0cf9a
FT
1869 /*
1870 * If we get something weird that we aren't expecting,
1da177e4
LT
1871 * reject it.
1872 */
1873 default:
1874 if (!tmp) {
6a6ff4ac 1875 shost_printk(KERN_ERR, instance, "rejecting message ");
1abfd370 1876 spi_print_msg(extended_msg);
1da177e4
LT
1877 printk("\n");
1878 } else if (tmp != EXTENDED_MESSAGE)
017560fc 1879 scmd_printk(KERN_INFO, cmd,
0d2cf867
FT
1880 "rejecting unknown message %02x\n",
1881 tmp);
1da177e4 1882 else
017560fc 1883 scmd_printk(KERN_INFO, cmd,
0d2cf867
FT
1884 "rejecting unknown extended message code %02x, length %d\n",
1885 extended_msg[1], extended_msg[0]);
1da177e4
LT
1886
1887 msgout = MESSAGE_REJECT;
1888 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1889 break;
0d2cf867 1890 } /* switch (tmp) */
1da177e4
LT
1891 break;
1892 case PHASE_MSGOUT:
1893 len = 1;
1894 data = &msgout;
1895 hostdata->last_message = msgout;
1896 NCR5380_transfer_pio(instance, &phase, &len, &data);
1897 if (msgout == ABORT) {
1da177e4
LT
1898 hostdata->connected = NULL;
1899 cmd->result = DID_ERROR << 16;
677e0194 1900 complete_cmd(instance, cmd);
1da177e4
LT
1901 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1902 return;
1903 }
1904 msgout = NOP;
1905 break;
1906 case PHASE_CMDOUT:
1907 len = cmd->cmd_len;
1908 data = cmd->cmnd;
aff0cf9a
FT
1909 /*
1910 * XXX for performance reasons, on machines with a
1911 * PSEUDO-DMA architecture we should probably
1912 * use the dma transfer function.
1da177e4
LT
1913 */
1914 NCR5380_transfer_pio(instance, &phase, &len, &data);
1da177e4
LT
1915 break;
1916 case PHASE_STATIN:
1917 len = 1;
1918 data = &tmp;
1919 NCR5380_transfer_pio(instance, &phase, &len, &data);
1920 cmd->SCp.Status = tmp;
1921 break;
1922 default:
6a6ff4ac 1923 shost_printk(KERN_ERR, instance, "unknown phase\n");
4dde8f7d 1924 NCR5380_dprint(NDEBUG_ANY, instance);
0d2cf867 1925 } /* switch(phase) */
686f3990 1926 } else {
11d2f63b 1927 spin_unlock_irq(&hostdata->lock);
686f3990 1928 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
11d2f63b 1929 spin_lock_irq(&hostdata->lock);
1da177e4 1930 }
11d2f63b 1931 }
1da177e4
LT
1932}
1933
1934/*
1935 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1936 *
aff0cf9a 1937 * Purpose : does reselection, initializing the instance->connected
594d4ba3
FT
1938 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1939 * nexus has been reestablished,
aff0cf9a 1940 *
1da177e4 1941 * Inputs : instance - this instance of the NCR5380.
1da177e4
LT
1942 */
1943
0d2cf867
FT
1944static void NCR5380_reselect(struct Scsi_Host *instance)
1945{
e8a60144 1946 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1da177e4
LT
1947 unsigned char target_mask;
1948 unsigned char lun, phase;
1949 int len;
1950 unsigned char msg[3];
1951 unsigned char *data;
32b26a10
FT
1952 struct NCR5380_cmd *ncmd;
1953 struct scsi_cmnd *tmp;
1da177e4
LT
1954
1955 /*
1956 * Disable arbitration, etc. since the host adapter obviously
1957 * lost, and tell an interrupted NCR5380_select() to restart.
1958 */
1959
1960 NCR5380_write(MODE_REG, MR_BASE);
1da177e4
LT
1961
1962 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
b746545f
FT
1963
1964 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
1da177e4 1965
aff0cf9a 1966 /*
1da177e4
LT
1967 * At this point, we have detected that our SCSI ID is on the bus,
1968 * SEL is true and BSY was false for at least one bus settle delay
1969 * (400 ns).
1970 *
1971 * We must assert BSY ourselves, until the target drops the SEL
1972 * signal.
1973 */
1974
1975 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
72064a78
FT
1976 if (NCR5380_poll_politely(instance,
1977 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
1978 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1979 return;
1980 }
1da177e4
LT
1981 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1982
1983 /*
1984 * Wait for target to go into MSGIN.
1da177e4
LT
1985 */
1986
1cc160e1 1987 if (NCR5380_poll_politely(instance,
72064a78
FT
1988 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
1989 do_abort(instance);
1990 return;
1991 }
1da177e4
LT
1992
1993 len = 1;
1994 data = msg;
1995 phase = PHASE_MSGIN;
1996 NCR5380_transfer_pio(instance, &phase, &len, &data);
1997
72064a78
FT
1998 if (len) {
1999 do_abort(instance);
2000 return;
2001 }
2002
1da177e4 2003 if (!(msg[0] & 0x80)) {
72064a78 2004 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
1abfd370 2005 spi_print_msg(msg);
72064a78
FT
2006 printk("\n");
2007 do_abort(instance);
2008 return;
2009 }
2010 lun = msg[0] & 0x07;
1da177e4 2011
72064a78
FT
2012 /*
2013 * We need to add code for SCSI-II to track which devices have
2014 * I_T_L_Q nexuses established, and which have simple I_T_L
2015 * nexuses so we can chose to do additional data transfer.
2016 */
1da177e4 2017
72064a78
FT
2018 /*
2019 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2020 * just reestablished, and remove it from the disconnected queue.
2021 */
1da177e4 2022
32b26a10
FT
2023 tmp = NULL;
2024 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2025 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2026
2027 if (target_mask == (1 << scmd_id(cmd)) &&
2028 lun == (u8)cmd->device->lun) {
2029 list_del(&ncmd->list);
2030 tmp = cmd;
72064a78 2031 break;
1da177e4
LT
2032 }
2033 }
0d3d9a42
FT
2034
2035 if (tmp) {
2036 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2037 "reselect: removed %p from disconnected queue\n", tmp);
2038 } else {
72064a78
FT
2039 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2040 target_mask, lun);
2041 /*
0d2cf867
FT
2042 * Since we have an established nexus that we can't do anything
2043 * with, we must abort it.
72064a78 2044 */
1da177e4 2045 do_abort(instance);
72064a78 2046 return;
1da177e4 2047 }
72064a78
FT
2048
2049 /* Accept message by clearing ACK */
2050 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2051
2052 hostdata->connected = tmp;
b746545f
FT
2053 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2054 scmd_id(tmp), tmp->device->lun, tmp->tag);
1da177e4
LT
2055}
2056
8b00c3d5
FT
2057/**
2058 * list_find_cmd - test for presence of a command in a linked list
2059 * @haystack: list of commands
2060 * @needle: command to search for
2061 */
2062
2063static bool list_find_cmd(struct list_head *haystack,
2064 struct scsi_cmnd *needle)
2065{
2066 struct NCR5380_cmd *ncmd;
2067
2068 list_for_each_entry(ncmd, haystack, list)
2069 if (NCR5380_to_scmd(ncmd) == needle)
2070 return true;
2071 return false;
2072}
2073
2074/**
2075 * list_remove_cmd - remove a command from linked list
2076 * @haystack: list of commands
2077 * @needle: command to remove
2078 */
2079
2080static bool list_del_cmd(struct list_head *haystack,
2081 struct scsi_cmnd *needle)
2082{
2083 if (list_find_cmd(haystack, needle)) {
2084 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2085
2086 list_del(&ncmd->list);
2087 return true;
2088 }
2089 return false;
2090}
2091
2092/**
2093 * NCR5380_abort - scsi host eh_abort_handler() method
2094 * @cmd: the command to be aborted
2095 *
2096 * Try to abort a given command by removing it from queues and/or sending
2097 * the target an abort message. This may not succeed in causing a target
2098 * to abort the command. Nonetheless, the low-level driver must forget about
2099 * the command because the mid-layer reclaims it and it may be re-issued.
2100 *
2101 * The normal path taken by a command is as follows. For EH we trace this
2102 * same path to locate and abort the command.
2103 *
2104 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2105 * [disconnected -> connected ->]...
2106 * [autosense -> connected ->] done
2107 *
8b00c3d5
FT
2108 * If cmd was not found at all then presumably it has already been completed,
2109 * in which case return SUCCESS to try to avoid further EH measures.
dc183965 2110 *
8b00c3d5 2111 * If the command has not completed yet, we must not fail to find it.
dc183965
FT
2112 * We have no option but to forget the aborted command (even if it still
2113 * lacks sense data). The mid-layer may re-issue a command that is in error
2114 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2115 * this driver are such that a command can appear on one queue only.
71a00593
FT
2116 *
2117 * The lock protects driver data structures, but EH handlers also use it
2118 * to serialize their own execution and prevent their own re-entry.
1da177e4
LT
2119 */
2120
710ddd0d
FT
2121static int NCR5380_abort(struct scsi_cmnd *cmd)
2122{
1da177e4 2123 struct Scsi_Host *instance = cmd->device->host;
e8a60144 2124 struct NCR5380_hostdata *hostdata = shost_priv(instance);
11d2f63b 2125 unsigned long flags;
8b00c3d5 2126 int result = SUCCESS;
1fa6b5fb 2127
11d2f63b
FT
2128 spin_lock_irqsave(&hostdata->lock, flags);
2129
32b26a10 2130#if (NDEBUG & NDEBUG_ANY)
8b00c3d5 2131 scmd_printk(KERN_INFO, cmd, __func__);
32b26a10 2132#endif
e5c3fddf
FT
2133 NCR5380_dprint(NDEBUG_ANY, instance);
2134 NCR5380_dprint_phase(NDEBUG_ANY, instance);
1da177e4 2135
8b00c3d5
FT
2136 if (list_del_cmd(&hostdata->unissued, cmd)) {
2137 dsprintk(NDEBUG_ABORT, instance,
2138 "abort: removed %p from issue queue\n", cmd);
2139 cmd->result = DID_ABORT << 16;
2140 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
dc183965 2141 goto out;
8b00c3d5
FT
2142 }
2143
707d62b3
FT
2144 if (hostdata->selecting == cmd) {
2145 dsprintk(NDEBUG_ABORT, instance,
2146 "abort: cmd %p == selecting\n", cmd);
2147 hostdata->selecting = NULL;
2148 cmd->result = DID_ABORT << 16;
2149 complete_cmd(instance, cmd);
2150 goto out;
2151 }
2152
8b00c3d5
FT
2153 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2154 dsprintk(NDEBUG_ABORT, instance,
2155 "abort: removed %p from disconnected list\n", cmd);
71a00593
FT
2156 /* Can't call NCR5380_select() and send ABORT because that
2157 * means releasing the lock. Need a bus reset.
2158 */
dc183965
FT
2159 set_host_byte(cmd, DID_ERROR);
2160 complete_cmd(instance, cmd);
71a00593
FT
2161 result = FAILED;
2162 goto out;
8b00c3d5
FT
2163 }
2164
2165 if (hostdata->connected == cmd) {
2166 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2167 hostdata->connected = NULL;
8b00c3d5 2168 hostdata->dma_len = 0;
8b00c3d5
FT
2169 if (do_abort(instance)) {
2170 set_host_byte(cmd, DID_ERROR);
2171 complete_cmd(instance, cmd);
2172 result = FAILED;
2173 goto out;
2174 }
2175 set_host_byte(cmd, DID_ABORT);
dc183965
FT
2176 complete_cmd(instance, cmd);
2177 goto out;
2178 }
2179
2180 if (list_del_cmd(&hostdata->autosense, cmd)) {
2181 dsprintk(NDEBUG_ABORT, instance,
2182 "abort: removed %p from sense queue\n", cmd);
2183 set_host_byte(cmd, DID_ERROR);
8b00c3d5
FT
2184 complete_cmd(instance, cmd);
2185 }
2186
2187out:
2188 if (result == FAILED)
2189 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2190 else
2191 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2192
2193 queue_work(hostdata->work_q, &hostdata->main_task);
11d2f63b 2194 spin_unlock_irqrestore(&hostdata->lock, flags);
32b26a10 2195
8b00c3d5 2196 return result;
1da177e4
LT
2197}
2198
2199
3be1b3ea
FT
2200/**
2201 * NCR5380_bus_reset - reset the SCSI bus
2202 * @cmd: SCSI command undergoing EH
1da177e4 2203 *
3be1b3ea 2204 * Returns SUCCESS
1da177e4
LT
2205 */
2206
710ddd0d 2207static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
68b3aa7c
JG
2208{
2209 struct Scsi_Host *instance = cmd->device->host;
11d2f63b 2210 struct NCR5380_hostdata *hostdata = shost_priv(instance);
62717f53 2211 int i;
11d2f63b 2212 unsigned long flags;
62717f53 2213 struct NCR5380_cmd *ncmd;
68b3aa7c 2214
11d2f63b 2215 spin_lock_irqsave(&hostdata->lock, flags);
3be1b3ea
FT
2216
2217#if (NDEBUG & NDEBUG_ANY)
62717f53 2218 scmd_printk(KERN_INFO, cmd, __func__);
3be1b3ea 2219#endif
e5c3fddf
FT
2220 NCR5380_dprint(NDEBUG_ANY, instance);
2221 NCR5380_dprint_phase(NDEBUG_ANY, instance);
68b3aa7c 2222
68b3aa7c 2223 do_reset(instance);
3be1b3ea 2224
62717f53
FT
2225 /* reset NCR registers */
2226 NCR5380_write(MODE_REG, MR_BASE);
2227 NCR5380_write(TARGET_COMMAND_REG, 0);
2228 NCR5380_write(SELECT_ENABLE_REG, 0);
2229
2230 /* After the reset, there are no more connected or disconnected commands
2231 * and no busy units; so clear the low-level status here to avoid
2232 * conflicts when the mid-level code tries to wake up the affected
2233 * commands!
2234 */
2235
1884c283
FT
2236 if (list_del_cmd(&hostdata->unissued, cmd)) {
2237 cmd->result = DID_RESET << 16;
2238 cmd->scsi_done(cmd);
2239 }
2240
2241 if (hostdata->selecting) {
2242 hostdata->selecting->result = DID_RESET << 16;
2243 complete_cmd(instance, hostdata->selecting);
2244 hostdata->selecting = NULL;
2245 }
62717f53
FT
2246
2247 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2248 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2249
2250 set_host_byte(cmd, DID_RESET);
2251 cmd->scsi_done(cmd);
2252 }
1884c283 2253 INIT_LIST_HEAD(&hostdata->disconnected);
62717f53
FT
2254
2255 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2256 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2257
2258 set_host_byte(cmd, DID_RESET);
2259 cmd->scsi_done(cmd);
2260 }
1884c283 2261 INIT_LIST_HEAD(&hostdata->autosense);
62717f53
FT
2262
2263 if (hostdata->connected) {
2264 set_host_byte(hostdata->connected, DID_RESET);
2265 complete_cmd(instance, hostdata->connected);
2266 hostdata->connected = NULL;
2267 }
2268
62717f53
FT
2269 for (i = 0; i < 8; ++i)
2270 hostdata->busy[i] = 0;
62717f53 2271 hostdata->dma_len = 0;
62717f53
FT
2272
2273 queue_work(hostdata->work_q, &hostdata->main_task);
11d2f63b 2274 spin_unlock_irqrestore(&hostdata->lock, flags);
1da177e4 2275
1da177e4
LT
2276 return SUCCESS;
2277}
This page took 1.117202 seconds and 5 git commands to generate.