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