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