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