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