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