xref: /netbsd-src/sys/arch/atari/dev/ncr5380.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: ncr5380.c,v 1.63 2009/03/18 17:06:43 cegger Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Leo Weppelman.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ncr5380.c,v 1.63 2009/03/18 17:06:43 cegger Exp $");
35 
36 /*
37  * Bit mask of targets you want debugging to be shown
38  */
39 u_char	dbg_target_mask = 0x7f;
40 
41 /*
42  * Set bit for target when parity checking must be disabled.
43  * My (LWP) Maxtor 7245S  seems to generate parity errors on about 50%
44  * of all transfers while the data is correct!?
45  */
46 u_char	ncr5380_no_parchk = 0xff;
47 
48 #ifdef	AUTO_SENSE
49 
50 /*
51  * Bit masks of targets that accept linked commands, and those
52  * that we've already checked out.  Some devices will report
53  * that they support linked commands when they have problems with
54  * them.  By default, don't try them on any devices.  Allow an
55  * option to override.
56  */
57 #ifdef	TRY_SCSI_LINKED_COMMANDS
58 u_char	ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
59 #else
60 u_char	ncr_test_link = 0x7f;
61 #endif
62 u_char	ncr_will_link = 0x00;
63 
64 #endif	/* AUTO_SENSE */
65 
66 /*
67  * This is the default sense-command we send.
68  */
69 static	u_char	sense_cmd[] = {
70 		SCSI_REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
71 };
72 
73 /*
74  * True if the main co-routine is running
75  */
76 static volatile int	main_running = 0;
77 
78 /*
79  * Mask of targets selected
80  */
81 static u_char	busy;
82 
83 static void	ncr5380_minphys(struct buf *bp);
84 static void	ncr5380_scsi_request(struct scsipi_channel *,
85 					scsipi_adapter_req_t, void *);
86 static void	ncr5380_show_scsi_cmd(struct scsipi_xfer *xs);
87 
88 static SC_REQ	req_queue[NREQ];
89 static SC_REQ	*free_head = NULL;	/* Free request structures	*/
90 
91 
92 /*
93  * Inline functions:
94  */
95 
96 /*
97  * Wait for request-line to become active. When it doesn't return 0.
98  * Otherwise return != 0.
99  * The timeouts in the 'wait_req_*' functions are arbitrary and rather
100  * large. In 99% of the invocations nearly no timeout is needed but in
101  * some cases (especially when using my tapedrive, a Tandberg 3600) the
102  * device is busy internally and the first SCSI-phase will be delayed.
103  */
104 extern inline int wait_req_true(void)
105 {
106 	int	timeout = 250000;
107 
108 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
109 		delay(1);
110 	return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
111 }
112 
113 /*
114  * Wait for request-line to become inactive. When it doesn't return 0.
115  * Otherwise return != 0.
116  */
117 extern inline int wait_req_false(void)
118 {
119 	int	timeout = 250000;
120 
121 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
122 		delay(1);
123 	return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
124 }
125 
126 extern inline void ack_message(void)
127 {
128 	SET_5380_REG(NCR5380_ICOM, 0);
129 }
130 
131 extern inline void nack_message(SC_REQ *reqp, u_char msg)
132 {
133 	SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
134 	reqp->msgout = msg;
135 }
136 
137 extern inline void finish_req(SC_REQ *reqp)
138 {
139 	int			sps;
140 	struct scsipi_xfer	*xs = reqp->xs;
141 
142 #ifdef REAL_DMA
143 	/*
144 	 * If we bounced, free the bounce buffer
145 	 */
146 	if (reqp->dr_flag & DRIVER_BOUNCING)
147 		free_bounceb(reqp->bounceb);
148 #endif /* REAL_DMA */
149 #ifdef DBG_REQ
150 	if (dbg_target_mask & (1 << reqp->targ_id))
151 		show_request(reqp, "DONE");
152 #endif
153 #ifdef DBG_ERR_RET
154 	if ((dbg_target_mask & (1 << reqp->targ_id)) && (reqp->xs->error != 0))
155 		show_request(reqp, "ERR_RET");
156 #endif
157 	/*
158 	 * Return request to free-q
159 	 */
160 	sps = splbio();
161 	reqp->next = free_head;
162 	free_head  = reqp;
163 	splx(sps);
164 
165 	if (!(reqp->dr_flag & DRIVER_LINKCHK))
166 		scsipi_done(xs);
167 }
168 
169 /*
170  * Auto config stuff....
171  */
172 void	ncr_attach(struct device *, struct device *, void *);
173 int	ncr_match(struct device *, struct cfdata *, void *);
174 
175 /*
176  * Tricks to make driver-name configurable
177  */
178 #define CFNAME(n)	__CONCAT(n,_cd)
179 #define CANAME(n)	__CONCAT(n,_ca)
180 #define CFSTRING(n)	__STRING(n)
181 #define	CFDRNAME(n)	n
182 
183 CFATTACH_DECL(CFDRNAME(DRNAME), sizeof(struct ncr_softc),
184     ncr_match, ncr_attach, NULL, NULL);
185 
186 extern struct cfdriver CFNAME(DRNAME);
187 
188 int
189 ncr_match(struct device *pdp, struct cfdata *cfp, void *auxp)
190 {
191 	return (machine_match(pdp, cfp, auxp, &CFNAME(DRNAME)));
192 }
193 
194 void
195 ncr_attach(struct device *pdp, struct device *dp, void *auxp)
196 {
197 	struct ncr_softc	*sc;
198 	int			i;
199 
200 	sc = (struct ncr_softc *)dp;
201 
202 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
203 	sc->sc_adapter.adapt_openings = 7;
204 	sc->sc_adapter.adapt_max_periph = 1;
205 	sc->sc_adapter.adapt_ioctl = NULL;
206 	sc->sc_adapter.adapt_minphys = ncr5380_minphys;
207 	sc->sc_adapter.adapt_request = ncr5380_scsi_request;
208 
209 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
210 	sc->sc_channel.chan_bustype = &scsi_bustype;
211 	sc->sc_channel.chan_channel = 0;
212 	sc->sc_channel.chan_ntargets = 8;
213 	sc->sc_channel.chan_nluns = 8;
214 	sc->sc_channel.chan_id = 7;
215 
216 	/*
217 	 * bitmasks
218 	 */
219 	sc->sc_noselatn = 0;
220 	sc->sc_selected = 0;
221 
222 	/*
223 	 * Initialize machine-type specific things...
224 	 */
225 	scsi_mach_init(sc);
226 	printf("\n");
227 
228 	/*
229 	 * Initialize request queue freelist.
230 	 */
231 	for (i = 0; i < NREQ; i++) {
232 		req_queue[i].next = free_head;
233 		free_head = &req_queue[i];
234 	}
235 
236 	/*
237 	 * Initialize the host adapter
238 	 */
239 	scsi_idisable();
240 	ENABLE_NCR5380(sc);
241 	SET_5380_REG(NCR5380_ICOM, 0);
242 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
243 	SET_5380_REG(NCR5380_TCOM, 0);
244 	SET_5380_REG(NCR5380_IDSTAT, 0);
245 	scsi_ienable();
246 
247 	/*
248 	 * attach all scsi units on us
249 	 */
250 	config_found(dp, &sc->sc_channel, scsiprint);
251 }
252 
253 /*
254  * End of auto config stuff....
255  */
256 
257 /*
258  * Carry out a request from the high level driver.
259  */
260 static void
261 ncr5380_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
262 {
263 	struct scsipi_xfer *xs;
264 	struct scsipi_periph *periph;
265 	struct ncr_softc *sc = (void *)chan->chan_adapter->adapt_dev;
266 	int	sps;
267 	SC_REQ	*reqp, *link, *tmp;
268 	int	flags;
269 
270 	switch (req) {
271 	case ADAPTER_REQ_RUN_XFER:
272 		xs = arg;
273 		periph = xs->xs_periph;
274 
275 		/*
276 		 * We do not queue RESET commands
277 		 */
278 		flags = xs->xs_control;
279 		if (flags & XS_CTL_RESET) {
280 			scsi_reset_verbose(sc, "Got reset-command");
281 			scsipi_done(xs);
282 			return;
283 		}
284 
285 		/*
286 		 * Get a request block
287 		 */
288 		sps = splbio();
289 		if ((reqp = free_head) == 0) {
290 			xs->error = XS_RESOURCE_SHORTAGE;
291 			scsipi_done(xs);
292 			splx(sps);
293 			return;
294 		}
295 		free_head  = reqp->next;
296 		reqp->next = NULL;
297 		splx(sps);
298 
299 		/*
300 		 * Initialize our private fields
301 		 */
302 		reqp->dr_flag   = (flags & XS_CTL_POLL) ? DRIVER_NOINT : 0;
303 		reqp->phase     = NR_PHASE;
304 		reqp->msgout    = MSG_NOOP;
305 		reqp->status    = SCSGOOD;
306 		reqp->message   = 0xff;
307 		reqp->link      = NULL;
308 		reqp->xs        = xs;
309 		reqp->targ_id   = xs->xs_periph->periph_target;
310 		reqp->targ_lun  = xs->xs_periph->periph_lun;
311 		reqp->xdata_ptr = (u_char*)xs->data;
312 		reqp->xdata_len = xs->datalen;
313 		memcpy(&reqp->xcmd, xs->cmd, xs->cmdlen);
314 		reqp->xcmd_len = xs->cmdlen;
315 		reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
316 
317 #ifdef REAL_DMA
318 		/*
319 		 * Check if DMA can be used on this request
320 		 */
321 		if (scsi_dmaok(reqp))
322 			reqp->dr_flag |= DRIVER_DMAOK;
323 #endif /* REAL_DMA */
324 
325 		/*
326 		 * Insert the command into the issue queue. Note that
327 		 * 'REQUEST SENSE' commands are inserted at the head of the
328 		 * queue since any command will clear the existing contingent
329 		 * allegience condition and the sense data is only valid while
330 		 * the condition exists. When possible, link the command to a
331 		 * previous command to the same target. This is not very
332 		 * sensible when AUTO_SENSE is not defined! Interrupts are
333 		 * disabled while we are fiddling with the issue-queue.
334 		 */
335 		sps = splbio();
336 		link = NULL;
337 		if ((issue_q == NULL) ||
338 		    (reqp->xcmd.opcode == SCSI_REQUEST_SENSE)) {
339 			reqp->next = issue_q;
340 			issue_q    = reqp;
341 		}
342 		else {
343 			tmp  = issue_q;
344 			do {
345 			    if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
346 					link = tmp;
347 			} while (tmp->next && (tmp = tmp->next));
348 			tmp->next = reqp;
349 #ifdef AUTO_SENSE
350 			if (link && (ncr_will_link & (1<<reqp->targ_id))) {
351 				link->link = reqp;
352 				link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
353 			}
354 #endif
355 	}
356 #ifdef AUTO_SENSE
357 		/*
358 		 * If we haven't already, check the target for link support.
359 		 * Do this by prefixing the current command with a dummy
360 		 * Request_Sense command, link the dummy to the current
361 		 * command, and insert the dummy command at the head of the
362 		 * issue queue.  Set the DRIVER_LINKCHK flag so that we'll
363 		 * ignore the results of the dummy command, since we only
364 		 * care about whether it was accepted or not.
365 		 */
366 		if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
367 		    (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
368 			free_head = tmp->next;
369 			tmp->dr_flag = (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
370 			tmp->phase = NR_PHASE;
371 			tmp->msgout = MSG_NOOP;
372 			tmp->status = SCSGOOD;
373 			tmp->xs = reqp->xs;
374 			tmp->targ_id = reqp->targ_id;
375 			tmp->targ_lun = reqp->targ_lun;
376 			memcpy( &tmp->xcmd, sense_cmd, sizeof(sense_cmd));
377 			tmp->xcmd_len = sizeof(sense_cmd);
378 			tmp->xdata_ptr = (u_char *)&tmp->xs->sense.scsi_sense;
379 			tmp->xdata_len = sizeof(tmp->xs->sense.scsi_sense);
380 			ncr_test_link |= 1<<tmp->targ_id;
381 			tmp->link = reqp;
382 			tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
383 			tmp->next = issue_q;
384 			issue_q = tmp;
385 #ifdef DBG_REQ
386 			if (dbg_target_mask & (1 << tmp->targ_id))
387 				show_request(tmp, "LINKCHK");
388 #endif
389 		}
390 #endif
391 		splx(sps);
392 
393 #ifdef DBG_REQ
394 		if (dbg_target_mask & (1 << reqp->targ_id))
395 			show_request(reqp,
396 			    (reqp->xcmd.opcode == SCSI_REQUEST_SENSE) ?
397 			    "HEAD":"TAIL");
398 #endif
399 
400 		run_main(sc);
401 		return;
402 
403 	case ADAPTER_REQ_GROW_RESOURCES:
404 		/* XXX Not supported. */
405 		return;
406 
407 	case ADAPTER_REQ_SET_XFER_MODE:
408 		/* XXX Not supported. */
409 		return;
410 	}
411 }
412 
413 static void
414 ncr5380_minphys(struct buf *bp)
415 {
416     if (bp->b_bcount > MIN_PHYS)
417 	bp->b_bcount = MIN_PHYS;
418     minphys(bp);
419 }
420 #undef MIN_PHYS
421 
422 static void
423 ncr5380_show_scsi_cmd(struct scsipi_xfer *xs)
424 {
425 	u_char	*b = (u_char *) xs->cmd;
426 	int	i  = 0;
427 
428 	scsipi_printaddr(xs->xs_periph);
429 	if (!(xs->xs_control & XS_CTL_RESET)) {
430 		while (i < xs->cmdlen) {
431 			if (i)
432 				printf(",");
433 			printf("%x",b[i++]);
434 		}
435 		printf("-\n");
436 	}
437 	else {
438 
439 		printf("-RESET-\n");
440 	}
441 }
442 
443 /*
444  * The body of the driver.
445  */
446 static void
447 scsi_main(struct ncr_softc *sc)
448 {
449 	SC_REQ	*req, *prev;
450 	int	itype;
451 	int	sps;
452 
453 	/*
454 	 * While running in the driver SCSI-interrupts are disabled.
455 	 */
456 	scsi_idisable();
457 	ENABLE_NCR5380(sc);
458 
459 	PID("scsi_main1");
460 	for (;;) {
461 	    sps = splbio();
462 	    if (!connected) {
463 		/*
464 		 * Check if it is fair keep any exclusive access to DMA
465 		 * claimed. If not, stop queueing new jobs so the discon_q
466 		 * will be eventually drained and DMA can be given up.
467 		 */
468 		if (!fair_to_keep_dma())
469 			goto main_exit;
470 
471 		/*
472 		 * Search through the issue-queue for a command
473 		 * destined for a target that isn't busy.
474 		 */
475 		prev = NULL;
476 		for (req=issue_q; req != NULL; prev = req, req = req->next) {
477 			if (!(busy & (1 << req->targ_id))) {
478 				/*
479 				 * Found one, remove it from the issue queue
480 				 */
481 				if (prev == NULL)
482 					issue_q = req->next;
483 				else prev->next = req->next;
484 				req->next = NULL;
485 				break;
486 			}
487 		}
488 
489 		/*
490 		 * When a request has just ended, we get here before an other
491 		 * device detects that the bus is free and that it can
492 		 * reconnect. The problem is that when this happens, we always
493 		 * baffle the device because our (initiator) id is higher. This
494 		 * can cause a sort of starvation on slow devices. So we check
495 		 * for a pending reselection here.
496 		 * Note that 'connected' will be non-null if the reselection
497 		 * succeeds.
498 		 */
499 		if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
500 						== (SC_S_SEL|SC_S_IO)){
501 			if (req != NULL) {
502 				req->next = issue_q;
503 				issue_q = req;
504 			}
505 			splx(sps);
506 
507 			reselect(sc);
508 			scsi_clr_ipend();
509 			goto connected;
510 		}
511 
512 		/*
513 		 * The host is not connected and there is no request
514 		 * pending, exit.
515 		 */
516 		if (req == NULL) {
517 			PID("scsi_main2");
518 			goto main_exit;
519 		}
520 
521 		/*
522 		 * Re-enable interrupts before handling the request.
523 		 */
524 		splx(sps);
525 
526 #ifdef DBG_REQ
527 		if (dbg_target_mask & (1 << req->targ_id))
528 			show_request(req, "TARGET");
529 #endif
530 		/*
531 		 * We found a request. Try to connect to the target. If the
532 		 * initiator fails arbitration, the command is put back in the
533 		 * issue queue.
534 		 */
535 		if (scsi_select(req, 0)) {
536 			sps = splbio();
537 			req->next = issue_q;
538 			issue_q = req;
539 			splx(sps);
540 #ifdef DBG_REQ
541 			if (dbg_target_mask & (1 << req->targ_id))
542 				ncr_tprint(req, "Select failed\n");
543 #endif
544 		}
545 	    }
546 	    else splx(sps);
547 connected:
548 	    if (connected) {
549 		/*
550 		 * If the host is currently connected but a 'real-DMA' transfer
551 		 * is in progress, the 'end-of-DMA' interrupt restarts main.
552 		 * So quit.
553 		 */
554 		sps = splbio();
555 		if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
556 			PID("scsi_main3");
557 			goto main_exit;
558 		}
559 		splx(sps);
560 
561 		/*
562 		 * Let the target guide us through the bus-phases
563 		 */
564 		while (information_transfer(sc) == -1)
565 			;
566 	    }
567 	}
568 	/* NEVER TO REACH HERE */
569 	show_phase(NULL, 0);	/* XXX: Get rid of not used warning */
570 	panic("ncr5380-SCSI: not designed to come here");
571 
572 main_exit:
573 	/*
574 	 * We enter here with interrupts disabled. We are about to exit main
575 	 * so interrupts should be re-enabled. Because interrupts are edge
576 	 * triggered, we could already have missed the interrupt. Therefore
577 	 * we check the IRQ-line here and re-enter when we really missed a
578 	 * valid interrupt.
579 	 */
580 	PID("scsi_main4");
581 	scsi_ienable();
582 
583 	/*
584 	 * If we're not currently connected, enable reselection
585 	 * interrupts.
586 	 */
587 	if (!connected)
588 		SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
589 
590 	if (scsi_ipending()) {
591 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
592 			scsi_idisable();
593 			scsi_clr_ipend();
594 			splx(sps);
595 
596 			if (itype == INTR_RESEL)
597 				reselect(sc);
598 #ifdef REAL_DMA
599 			else dma_ready();
600 #else
601 			else {
602 				if (pdma_ready())
603 					goto connected;
604 				panic("Got DMA interrupt without DMA");
605 			}
606 #endif
607 			goto connected;
608 		}
609 	}
610 	reconsider_dma();
611 	main_running = 0;
612 	splx(sps);
613 	PID("scsi_main5");
614 }
615 
616 #ifdef REAL_DMA
617 /*
618  * The SCSI-DMA interrupt.
619  * This interrupt can only be triggered when running in non-polled DMA
620  * mode. When DMA is not active, it will be silently ignored, it is usually
621  * to late because the EOP interrupt of the controller happens just a tiny
622  * bit earlier. It might become usefull when scatter/gather is implemented,
623  * because in that case only part of the DATAIN/DATAOUT transfer is taken
624  * out of a single buffer.
625  */
626 static void
627 ncr_dma_intr(struct ncr_softc *sc)
628 {
629 	SC_REQ	*reqp;
630 	int	dma_done;
631 
632 	PID("ncr_dma_intr");
633 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
634 		scsi_idisable();
635 		if (!(dma_done = dma_ready())) {
636 			transfer_dma(reqp, reqp->phase, 0);
637 			return;
638 		}
639 		run_main(sc);
640 	}
641 }
642 #endif /* REAL_DMA */
643 
644 /*
645  * The SCSI-controller interrupt. This interrupt occurs on reselections and
646  * at the end of non-polled DMA-interrupts. It is assumed to be called from
647  * the machine-dependent hardware interrupt.
648  */
649 static void
650 ncr_ctrl_intr(struct ncr_softc *sc)
651 {
652 	int	itype;
653 
654 	if (main_running)
655 		return; /* scsi_main() should handle this one */
656 
657 	while (scsi_ipending()) {
658 		scsi_idisable();
659 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
660 			if (itype == INTR_RESEL)
661 				reselect(sc);
662 			else {
663 #ifdef REAL_DMA
664 			    int	dma_done;
665 			    if (!(dma_done = dma_ready())) {
666 				transfer_dma(connected, connected->phase, 0);
667 				return;
668 			    }
669 #else
670 			    if (pdma_ready())
671 				return;
672 			    panic("Got DMA interrupt without DMA");
673 #endif
674 			}
675 			scsi_clr_ipend();
676 		}
677 		run_main(sc);
678 		return;
679 	}
680 	PID("ncr_ctrl_intr1");
681 }
682 
683 /*
684  * Initiate a connection path between the host and the target. The function
685  * first goes into arbitration for the SCSI-bus. When this succeeds, the target
686  * is selected and an 'IDENTIFY' message is send.
687  * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
688  * the target does not respond (to either selection or 'MESSAGE OUT') the
689  * 'done' function is executed.
690  * The result code given by the driver can be influenced by setting 'code'
691  * to a non-zero value. This is the case when 'select' is called by abort.
692  */
693 static int
694 scsi_select(SC_REQ *reqp, int code)
695 {
696 	u_char			tmp[1];
697 	u_char			phase;
698 	u_long			cnt;
699 	int			sps;
700 	u_int8_t		atn_flag;
701 	u_int8_t		targ_bit;
702 	struct ncr_softc	*sc;
703 
704 	sc = (void*)reqp->xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
705 	DBG_SELPRINT ("Starting arbitration\n", 0);
706 	PID("scsi_select1");
707 
708 	sps = splbio();
709 
710 	/*
711 	 * Prevent a race condition here. If a reslection interrupt occurred
712 	 * between the decision to pick a new request and the call to select,
713 	 * we abort the selection.
714 	 * Interrupts are lowered when the 5380 is setup to arbitrate for the
715 	 * bus.
716 	 */
717 	if (connected) {
718 		splx(sps);
719 		PID("scsi_select2");
720 		return (-1);
721 	}
722 
723 	/*
724 	 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
725 	 * selection.
726 	 */
727 	SET_5380_REG(NCR5380_TCOM, 0);
728 	SET_5380_REG(NCR5380_ICOM, 0);
729 
730 	/*
731 	 * Arbitrate for the bus.
732 	 */
733 	SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
734 	SET_5380_REG(NCR5380_MODE, SC_ARBIT);
735 
736 	splx(sps);
737 
738 	cnt = 10;
739 	while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
740 		delay(1);
741 
742 	if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
743 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
744 		SET_5380_REG(NCR5380_ICOM, 0);
745 		DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
746 		PID("scsi_select3");
747 		return (-1);
748 	}
749 
750 	/* The arbitration delay is 2.2 usecs */
751 	delay(3);
752 
753 	/*
754 	 * Check the result of the arbitration. If we failed, return -1.
755 	 */
756 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
757 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
758 		SET_5380_REG(NCR5380_ICOM, 0);
759 		PID("scsi_select4");
760 		return (-1);
761 	}
762 
763 	/*
764 	 * The spec requires that we should read the data register to
765 	 * check for higher id's and check the SC_LA again.
766 	 */
767 	tmp[0] = GET_5380_REG(NCR5380_DATA);
768 	if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
769 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
770 		SET_5380_REG(NCR5380_ICOM, 0);
771 		DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
772 		PID("scsi_select5");
773 		return (-1);
774 	}
775 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
776 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
777 		SET_5380_REG(NCR5380_ICOM, 0);
778 		DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
779 		PID("scsi_select6");
780 		return (-1);
781 	}
782 	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
783 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
784 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
785 		SET_5380_REG(NCR5380_ICOM, 0);
786 		DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
787 		PID("scsi_select7");
788 		return (-1);
789 	}
790 	/* Bus settle delay + Bus clear delay = 1.2 usecs */
791 	delay(2);
792 	DBG_SELPRINT ("Arbitration complete\n", 0);
793 
794 	/*
795 	 * Now that we won the arbitration, start the selection.
796 	 */
797 	targ_bit = 1 << reqp->targ_id;
798 	SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);
799 
800 	if (sc->sc_noselatn & targ_bit)
801 		atn_flag = 0;
802 	else
803 		atn_flag = SC_A_ATN;
804 
805 	/*
806 	 * Raise ATN while SEL is true before BSY goes false from arbitration,
807 	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
808 	 * phase immediately after the selection.
809 	 */
810 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
811 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
812 
813 	/*
814 	 * Turn off reselection interrupts
815 	 */
816 	SET_5380_REG(NCR5380_IDSTAT, 0);
817 
818 	/*
819 	 * Reset BSY. The delay following it, surpresses a glitch in the
820 	 * 5380 which causes us to see our own BSY signal instead of that of
821 	 * the target.
822 	 */
823 	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
824 	delay(1);
825 
826 	/*
827 	 * Wait for the target to react, the specs call for a timeout of
828 	 * 250 ms.
829 	 */
830 	cnt = 25000;
831 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
832 		delay(10);
833 
834 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
835 		/*
836 		 * There is no reaction from the target, start the selection
837 		 * timeout procedure. We release the databus but keep SEL
838 		 * asserted. After that we wait a 'selection abort time' (200
839 		 * usecs) and 2 deskew delays (90 ns) and check BSY again.
840 		 * When BSY is asserted, we assume the selection succeeded,
841 		 * otherwise we release the bus.
842 		 */
843 		SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
844 		delay(201);
845 		if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
846 			SET_5380_REG(NCR5380_ICOM, 0);
847 			reqp->xs->error      = code ? code : XS_SELTIMEOUT;
848 			DBG_SELPRINT ("Target %d not responding to sel\n",
849 								reqp->targ_id);
850 			if (reqp->dr_flag & DRIVER_LINKCHK)
851 				ncr_test_link &= ~(1<<reqp->targ_id);
852 			finish_req(reqp);
853 			PID("scsi_select8");
854 			return (0);
855 		}
856 	}
857 	SET_5380_REG(NCR5380_ICOM, atn_flag);
858 
859 	DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
860 
861 	/*
862 	 * The SCSI-interrupts are disabled while a request is being handled.
863 	 */
864 	scsi_idisable();
865 
866 	/*
867 	 * If we did not request ATN, then don't try to send IDENTIFY.
868 	 */
869 	if (atn_flag == 0) {
870 		reqp->phase = PH_CMD;
871 		goto identify_failed;
872 	}
873 
874 	/*
875 	 * Here we prepare to send an 'IDENTIFY' message.
876 	 * Allow disconnect only when interrupts are allowed.
877 	 */
878 	tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
879 			(reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
880 	cnt    = 1;
881 	phase  = PH_MSGOUT;
882 
883 	/*
884 	 * Since we followed the SCSI-spec and raised ATN while SEL was true
885 	 * but before BSY was false during the selection, a 'MESSAGE OUT'
886 	 * phase should follow.  Unfortunately, this does not happen on
887 	 * all targets (Asante ethernet devices, for example), so we must
888 	 * check the actual mode if the message transfer fails--if the
889 	 * new phase is PH_CMD and has never been successfully selected
890 	 * w/ATN in the past, then we assume that it is an old device
891 	 * that doesn't support select w/ATN.
892 	 */
893 	if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {
894 
895 		if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
896 			DBG_SELPRINT ("Target %d: not responding to ATN.\n",
897 							reqp->targ_id);
898 			sc->sc_noselatn |= targ_bit;
899 			reqp->phase = PH_CMD;
900 			goto identify_failed;
901 		}
902 
903 		DBG_SELPRINT ("Target %d: failed to send identify\n",
904 							reqp->targ_id);
905 		/*
906 		 * Try to disconnect from the target.  We cannot leave
907 		 * it just hanging here.
908 		 */
909 		if (!reach_msg_out(sc, sizeof(struct scsipi_generic))) {
910 			u_long	len   = 1;
911 			u_char	phse  = PH_MSGOUT;
912 			u_char	msg   = MSG_ABORT;
913 
914 			transfer_pio(&phse, &msg, &len, 0);
915 		}
916 		else scsi_reset_verbose(sc, "Connected to unidentified target");
917 
918 		SET_5380_REG(NCR5380_ICOM, 0);
919 		reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
920 		finish_req(reqp);
921 		PID("scsi_select9");
922 		return (0);
923 	}
924 	reqp->phase = PH_MSGOUT;
925 
926 identify_failed:
927 	sc->sc_selected |= targ_bit;
928 
929 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
930 	/*
931 	 * Command is connected, start timer ticking.
932 	 */
933 	ccb_p->xtimeout = ccb_p->timeout + Lbolt;
934 #endif
935 
936 	connected  = reqp;
937 	busy      |= targ_bit;
938 	PID("scsi_select10");
939 	return (0);
940 }
941 
942 /*
943  * Return codes:
944  *	 0: Job has finished or disconnected, find something else
945  *	-1: keep on calling information_transfer() from scsi_main()
946  */
947 static int
948 information_transfer(struct ncr_softc *sc)
949 {
950 	SC_REQ	*reqp = connected;
951 	u_char	tmp, phase;
952 	u_long	len;
953 
954 	PID("info_transf1");
955 	/*
956 	 * Clear pending interrupts from 5380-chip.
957 	 */
958 	scsi_clr_ipend();
959 
960 	/*
961 	 * The SCSI-spec requires BSY to be true while connected to a target,
962 	 * loosing it means we lost the target...
963 	 * Also REQ needs to be asserted here to indicate that the bus-phase
964 	 * is valid. When the target does not supply REQ within a 'reasonable'
965 	 * amount of time, it's probably lost in it's own maze of twisting
966 	 * passages, we have to reset the bus to free it.
967 	 */
968 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
969 		wait_req_true();
970 	tmp = GET_5380_REG(NCR5380_IDSTAT);
971 
972 
973 	if ((tmp & (SC_S_BSY|SC_S_REQ)) != (SC_S_BSY|SC_S_REQ)) {
974 		busy           &= ~(1 << reqp->targ_id);
975 		connected       = NULL;
976 		reqp->xs->error = XS_TIMEOUT;
977 		finish_req(reqp);
978 		if (!(tmp & SC_S_REQ))
979 			scsi_reset_verbose(sc,
980 					   "Timeout waiting for phase-change");
981 		PID("info_transf2");
982 		return (0);
983 	}
984 
985 	phase = (tmp >> 2) & 7;
986 	if (phase != reqp->phase) {
987 		reqp->phase = phase;
988 #ifdef DBG_INF
989 		if (dbg_target_mask & (1 << reqp->targ_id))
990 			DBG_INFPRINT(show_phase, reqp, phase);
991 #endif
992 	}
993 	else {
994 		/*
995 		 * Same data-phase. If same error give up
996 		 */
997 		if ((reqp->msgout == MSG_ABORT)
998 		     && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
999 			busy     &= ~(1 << reqp->targ_id);
1000 			connected = NULL;
1001 			reqp->xs->error = XS_TIMEOUT;
1002 			finish_req(reqp);
1003 			scsi_reset_verbose(sc, "Failure to abort command");
1004 			return (0);
1005 		}
1006 	}
1007 
1008 	switch (phase) {
1009 	    case PH_DATAOUT:
1010 #ifdef DBG_NOWRITE
1011 		ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
1012 		reqp->msgout = MSG_ABORT;
1013 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1014 		return (-1);
1015 #endif /* DBG_NOWRITE */
1016 		/*
1017 		 * If this is the first write using DMA, fill
1018 		 * the bounce buffer.
1019 		 */
1020 		if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
1021 		    if (reqp->dr_flag & DRIVER_BOUNCING)
1022 			memcpy( reqp->bounceb, reqp->xdata_ptr, reqp->xdata_len);
1023 		}
1024 
1025 	   case PH_DATAIN:
1026 		if (reqp->xdata_len <= 0) {
1027 			/*
1028 			 * Target keeps requesting data. Try to get into
1029 			 * message-out phase by feeding/taking 100 byte.
1030 			 */
1031 			ncr_tprint(reqp, "Target requests too much data\n");
1032 			reqp->msgout = MSG_ABORT;
1033 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1034 			reach_msg_out(sc, 100);
1035 			return (-1);
1036 		}
1037 #ifdef REAL_DMA
1038 		if (reqp->dr_flag & DRIVER_DMAOK) {
1039 			int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1040 			transfer_dma(reqp, phase, poll);
1041 			if (!poll)
1042 				return (0);
1043 		}
1044 		else
1045 #endif
1046 		{
1047 			PID("info_transf3");
1048 			len = reqp->xdata_len;
1049 #ifdef USE_PDMA
1050 			if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1051 				return (0);
1052 #else
1053 			transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1054 #endif
1055 			reqp->xdata_ptr += reqp->xdata_len - len;
1056 			reqp->xdata_len  = len;
1057 		}
1058 		return (-1);
1059 	   case PH_MSGIN:
1060 		/*
1061 		 * We only expect single byte messages here.
1062 		 */
1063 		len = 1;
1064 		transfer_pio(&phase, &tmp, &len, 1);
1065 		reqp->message = tmp;
1066 		return (handle_message(reqp, tmp));
1067 	   case PH_MSGOUT:
1068 		len = 1;
1069 		transfer_pio(&phase, &reqp->msgout, &len, 0);
1070 		if (reqp->msgout == MSG_ABORT) {
1071 			busy     &= ~(1 << reqp->targ_id);
1072 			connected = NULL;
1073 			if (!reqp->xs->error)
1074 				reqp->xs->error = XS_DRIVER_STUFFUP;
1075 			finish_req(reqp);
1076 			PID("info_transf4");
1077 			return (0);
1078 		}
1079 		reqp->msgout = MSG_NOOP;
1080 		return (-1);
1081 	   case PH_CMD :
1082 		len = reqp->xcmd_len;
1083 		transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1084 		PID("info_transf5");
1085 		return (-1);
1086 	   case PH_STATUS:
1087 		len = 1;
1088 		transfer_pio(&phase, &tmp, &len, 0);
1089 		reqp->status = tmp;
1090 		PID("info_transf6");
1091 		return (-1);
1092 	   default :
1093 		ncr_tprint(reqp, "Unknown phase\n");
1094 	}
1095 	PID("info_transf7");
1096 	return (-1);
1097 }
1098 
1099 /*
1100  * Handle the message 'msg' send to us by the target.
1101  * Return values:
1102  *	 0 : The current command has completed.
1103  *	-1 : Get on to the next phase.
1104  */
1105 static int
1106 handle_message(SC_REQ *reqp, u_int msg)
1107 {
1108 	int	sps;
1109 	SC_REQ	*prev, *req;
1110 
1111 	PID("hmessage1");
1112 	switch (msg) {
1113 		/*
1114 		 * Linking lets us reduce the time required to get
1115 		 * the next command to the device, skipping the arbitration
1116 		 * and selection time. In the current implementation,
1117 		 * we merely have to start the next command pointed
1118 		 * to by 'next_link'.
1119 		 */
1120 		case MSG_LINK_CMD_COMPLETE:
1121 		case MSG_LINK_CMD_COMPLETEF:
1122 			if (reqp->link == NULL) {
1123 				ncr_tprint(reqp, "No link for linked command");
1124 				nack_message(reqp, MSG_ABORT);
1125 				PID("hmessage2");
1126 				return (-1);
1127 			}
1128 			ack_message();
1129 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1130 				reqp->xs->resid = reqp->xdata_len;
1131 				reqp->xs->error = 0;
1132 			}
1133 
1134 #ifdef AUTO_SENSE
1135 			if (check_autosense(reqp, 1) == -1)
1136 				return (-1);
1137 #endif /* AUTO_SENSE */
1138 
1139 #ifdef DBG_REQ
1140 			if (dbg_target_mask & (1 << reqp->targ_id))
1141 				show_request(reqp->link, "LINK");
1142 #endif
1143 			connected = reqp->link;
1144 
1145 			/*
1146 			 * Unlink the 'linked' request from the issue_q
1147 			 */
1148 			sps  = splbio();
1149 			prev = NULL;
1150 			req  = issue_q;
1151 			for (; req != NULL; prev = req, req = req->next) {
1152 				if (req == connected)
1153 					break;
1154 			}
1155 			if (req == NULL)
1156 				panic("Inconsistent issue_q");
1157 			if (prev == NULL)
1158 				issue_q = req->next;
1159 			else prev->next = req->next;
1160 			req->next = NULL;
1161 			splx(sps);
1162 
1163 			finish_req(reqp);
1164 			PID("hmessage3");
1165 			return (-1);
1166 		case MSG_ABORT:
1167 		case MSG_CMDCOMPLETE:
1168 			ack_message();
1169 			connected = NULL;
1170 			busy     &= ~(1 << reqp->targ_id);
1171 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1172 				reqp->xs->resid = reqp->xdata_len;
1173 				reqp->xs->error = 0;
1174 			}
1175 
1176 #ifdef AUTO_SENSE
1177 			if (check_autosense(reqp, 0) == -1) {
1178 				PID("hmessage4");
1179 				return (0);
1180 			}
1181 #endif /* AUTO_SENSE */
1182 
1183 			finish_req(reqp);
1184 			PID("hmessage5");
1185 			return (0);
1186 		case MSG_MESSAGE_REJECT:
1187 			ack_message();
1188 			PID("hmessage6");
1189 			return (-1);
1190 		case MSG_DISCONNECT:
1191 			ack_message();
1192 #ifdef DBG_REQ
1193 			if (dbg_target_mask & (1 << reqp->targ_id))
1194 				show_request(reqp, "DISCON");
1195 #endif
1196 			sps = splbio();
1197 			connected  = NULL;
1198 			reqp->next = discon_q;
1199 			discon_q   = reqp;
1200 			splx(sps);
1201 			PID("hmessage7");
1202 			return (0);
1203 		case MSG_SAVEDATAPOINTER:
1204 		case MSG_RESTOREPOINTERS:
1205 			/*
1206 			 * We save pointers implicitely at disconnect.
1207 			 * So we can ignore these messages.
1208 			 */
1209 			ack_message();
1210 			PID("hmessage8");
1211 			return (-1);
1212 		case MSG_EXTENDED:
1213 			nack_message(reqp, MSG_MESSAGE_REJECT);
1214 			PID("hmessage9");
1215 			return (-1);
1216 		default:
1217 			if ((msg & 0x80) && !(msg & 0x18)) {	/* IDENTIFY */
1218 				PID("hmessage10");
1219 				ack_message();
1220 				return (0);
1221 			} else {
1222 				ncr_tprint(reqp,
1223 					   "Unknown message %x.  Rejecting.\n",
1224 					   msg);
1225 				nack_message(reqp, MSG_MESSAGE_REJECT);
1226 			}
1227 			return (-1);
1228 	}
1229 	PID("hmessage11");
1230 	return (-1);
1231 }
1232 
1233 /*
1234  * Handle reselection. If a valid reconnection occurs, connected
1235  * points at the reconnected command. The command is removed from the
1236  * disconnected queue.
1237  */
1238 static void
1239 reselect(struct ncr_softc *sc)
1240 {
1241 	u_char	phase;
1242 	u_long	len;
1243 	u_char	msg;
1244 	u_char	target_mask;
1245 	int	abort = 0;
1246 	SC_REQ	*tmp, *prev;
1247 
1248 	PID("reselect1");
1249 	target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1250 
1251 	/*
1252 	 * At this point, we have detected that our SCSI-id is on the bus,
1253 	 * SEL is true and BSY was false for at least one bus settle
1254 	 * delay (400 ns.).
1255 	 * We must assert BSY ourselves, until the target drops the SEL signal.
1256 	 * The SCSI-spec specifies no maximum time for this, so we have to
1257 	 * choose something long enough to suit all targets.
1258 	 */
1259 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1260 	len = 250000;
1261 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1262 		delay(1);
1263 		len--;
1264 	}
1265 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1266 		/* Damn SEL isn't dropping */
1267 		scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
1268 		return;
1269 	}
1270 
1271 	SET_5380_REG(NCR5380_ICOM, 0);
1272 
1273 	/*
1274 	 * Check if the reselection is still valid. Check twice because
1275 	 * of possible line glitches - cheaper than delay(1) and we need
1276 	 * only a few nanoseconds.
1277 	 */
1278 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1279 	    if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1280 		ncr_aprint(sc, "Stepped into the reselection timeout\n");
1281 		return;
1282 	    }
1283 	}
1284 
1285 	/*
1286 	 * Get the expected identify message.
1287 	 */
1288 	phase = PH_MSGIN;
1289 	len   = 1;
1290 	transfer_pio(&phase, &msg, &len, 0);
1291 	if (len || !MSG_ISIDENTIFY(msg)) {
1292 		ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1293 		abort = 1;
1294 		tmp = NULL;
1295 	}
1296 	else {
1297 	    /*
1298 	     * Find the command reconnecting
1299 	     */
1300 	    for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1301 		if (target_mask == (1 << tmp->targ_id)) {
1302 			if (prev)
1303 				prev->next = tmp->next;
1304 			else discon_q = tmp->next;
1305 			tmp->next = NULL;
1306 			break;
1307 		}
1308 	    }
1309 	    if (tmp == NULL) {
1310 		ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1311 								target_mask);
1312 		abort = 1;
1313 	    }
1314 	}
1315 	if (abort) {
1316 		msg   = MSG_ABORT;
1317 		len   = 1;
1318 		phase = PH_MSGOUT;
1319 
1320 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1321 		if (transfer_pio(&phase, &msg, &len, 0) || len)
1322 			scsi_reset_verbose(sc, "Failure to abort reselection");
1323 	}
1324 	else {
1325 		connected = tmp;
1326 #ifdef DBG_REQ
1327 		if (dbg_target_mask & (1 << tmp->targ_id))
1328 			show_request(tmp, "RECON");
1329 #endif
1330 	}
1331 	PID("reselect2");
1332 }
1333 
1334 /*
1335  * Transfer data in a given phase using programmed I/O.
1336  * Returns -1 when a different phase is entered without transferring the
1337  * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1338  * phase.
1339  */
1340 static int
1341 transfer_pio(u_char *phase, u_char *data, u_long *len, int dont_drop_ack)
1342 {
1343 	u_int	cnt = *len;
1344 	u_char	ph  = *phase;
1345 	u_char	tmp, new_icom;
1346 
1347 	DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1348 	PID("tpio1");
1349 	SET_5380_REG(NCR5380_TCOM, ph);
1350 	do {
1351 	    if (!wait_req_true()) {
1352 	    	DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1353 		break;
1354 	    }
1355 	    if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1356 	    	DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1357 		break;
1358 	    }
1359 	    if (PH_IN(ph)) {
1360 		*data++ = GET_5380_REG(NCR5380_DATA);
1361 		SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1362 		if ((cnt == 1) && dont_drop_ack)
1363 			new_icom = SC_A_ACK;
1364 		else new_icom = 0;
1365 	    }
1366 	    else {
1367 		SET_5380_REG(NCR5380_DATA, *data++);
1368 
1369 		/*
1370 		 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1371 		 * the initiator should drop ATN on the last byte of the
1372 		 * message phase after REQ has been asserted for the handshake
1373 		 * but before the initiator raises ACK.
1374 		 */
1375 		if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1376 			SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1377 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1378 			new_icom = 0;
1379 		}
1380 		else {
1381 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1382 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1383 			new_icom = SC_A_ATN;
1384 		}
1385 	    }
1386 	    if (!wait_req_false()) {
1387 		DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1388 		break;
1389 	    }
1390 	    SET_5380_REG(NCR5380_ICOM, new_icom);
1391 
1392 	} while (--cnt);
1393 
1394 	if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1395 		*phase = (tmp >> 2) & 7;
1396 	else *phase = NR_PHASE;
1397 	*len = cnt;
1398 	DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1399 								*phase, cnt);
1400 	PID("tpio2");
1401 	if (!cnt || (*phase == ph))
1402 		return (0);
1403 	return (-1);
1404 }
1405 
1406 #ifdef REAL_DMA
1407 
1408 /*
1409  * Start a DMA-transfer on the device using the current pointers.
1410  * If 'poll' is true, the function busy-waits until DMA has completed.
1411  */
1412 static void
1413 transfer_dma(SC_REQ *reqp, u_int phase, int poll)
1414 {
1415 	int	dma_done;
1416 	u_char	mbase = 0;
1417 	int	sps;
1418 
1419 again:
1420 	PID("tdma1");
1421 
1422 	/*
1423 	 * We should be in phase, otherwise we are not allowed to
1424 	 * drive the bus.
1425 	 */
1426 	SET_5380_REG(NCR5380_TCOM, phase);
1427 
1428 	/*
1429 	 * Defer interrupts until DMA is fully running.
1430 	 */
1431 	sps = splbio();
1432 
1433 	/*
1434 	 * Clear pending interrupts and parity errors.
1435 	 */
1436 	scsi_clr_ipend();
1437 
1438 	if (!poll) {
1439 		/*
1440 		 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1441 		 * to the interrupts we want enabled.
1442 		 */
1443 		scsi_ienable();
1444 		reqp->dr_flag |= DRIVER_IN_DMA;
1445 		mbase = SC_E_EOPI | SC_MON_BSY;
1446 	}
1447 	else scsi_idisable();
1448 	mbase |=  IMODE_BASE | SC_M_DMA;
1449 	scsi_dma_setup(reqp, phase, mbase);
1450 
1451 	splx(sps);
1452 
1453 	if (poll) {
1454 		/*
1455 		 * On polled-DMA transfers, we wait here until the
1456 		 * 'end-of-DMA' condition occurs.
1457 		 */
1458 		poll_edma(reqp);
1459 		if (!(dma_done = dma_ready()))
1460 			goto again;
1461 	}
1462 	PID("tdma2");
1463 }
1464 
1465 /*
1466  * Check results of a DMA data-transfer.
1467  */
1468 static int
1469 dma_ready(void)
1470 {
1471 	SC_REQ	*reqp = connected;
1472 	int	dmstat, is_edma;
1473 	long	bytes_left, bytes_done;
1474 
1475 	is_edma = get_dma_result(reqp, &bytes_left);
1476 	dmstat  = GET_5380_REG(NCR5380_DMSTAT);
1477 
1478 	/*
1479 	 * Check if the call is sensible and not caused by any spurious
1480 	 * interrupt.
1481 	 */
1482 	if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1483 		     && (dmstat & SC_PHS_MTCH) ) {
1484 		ncr_tprint(reqp, "dma_ready: spurious call "
1485 				 "(dm:%x,last_hit: %s)\n",
1486 #ifdef DBG_PID
1487 					dmstat, last_hit[DBG_PID-1]);
1488 #else
1489 					dmstat, "unknown");
1490 #endif
1491 		return (0);
1492 	}
1493 
1494 	/*
1495 	 * Clear all (pending) interrupts.
1496 	 */
1497 	scsi_clr_ipend();
1498 
1499 	/*
1500 	 * Update various transfer-pointers/lengths
1501 	 */
1502 	bytes_done = reqp->dm_cur->dm_count - bytes_left;
1503 
1504 	if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1505 		/*
1506 		 * Copy the bytes read until now from the bounce buffer
1507 		 * to the 'real' destination. Flush the data-cache
1508 		 * before copying.
1509 		 */
1510 		PCIA();
1511 		memcpy( reqp->xdata_ptr, reqp->bouncerp, bytes_done);
1512 		reqp->bouncerp += bytes_done;
1513 	}
1514 
1515 	reqp->xdata_ptr  = &reqp->xdata_ptr[bytes_done];	/* XXX */
1516 	reqp->xdata_len -= bytes_done;				/* XXX */
1517 	if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1518 		reqp->dm_cur++;
1519 	else reqp->dm_cur->dm_addr += bytes_done;
1520 
1521 	if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1522 		if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
1523 			ncr_tprint(reqp, "parity error in data-phase\n");
1524 			reqp->xs->error = XS_TIMEOUT;
1525 		}
1526 	}
1527 
1528 	/*
1529 	 * DMA mode should always be reset even when we will continue with the
1530 	 * next chain. It is also essential to clear the MON_BUSY because
1531 	 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1532 	 * the bus....
1533 	 */
1534 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1535 
1536 
1537 	if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1538 		 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1539 
1540 		/*
1541 		 * Tell interrupt functions DMA mode has ended.
1542 		 */
1543 		reqp->dr_flag &= ~DRIVER_IN_DMA;
1544 
1545 		/*
1546 		 * Clear mode and icom
1547 		 */
1548 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1549 		SET_5380_REG(NCR5380_ICOM, 0);
1550 
1551 		if (dmstat & SC_BSY_ERR) {
1552 			if (!reqp->xs->error)
1553 				reqp->xs->error = XS_TIMEOUT;
1554 			finish_req(reqp);
1555 			PID("dma_ready1");
1556 			return (1);
1557 		}
1558 
1559 		if (reqp->xs->error != 0) {
1560 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1561 			reqp->msgout = MSG_ABORT;
1562 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1563 		}
1564 		PID("dma_ready2");
1565 		return (1);
1566 	}
1567 	return (0);
1568 }
1569 #endif /* REAL_DMA */
1570 
1571 static int
1572 check_autosense(SC_REQ *reqp, int linked)
1573 {
1574 	int	sps;
1575 
1576 	/*
1577 	 * If this is the driver's Link Check for this target, ignore
1578 	 * the results of the command.  All we care about is whether we
1579 	 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
1580 	 */
1581 	PID("linkcheck");
1582 	if (reqp->dr_flag & DRIVER_LINKCHK) {
1583 		if (linked)
1584 			ncr_will_link |= 1<<reqp->targ_id;
1585 		else ncr_tprint(reqp, "Does not support linked commands\n");
1586 		return (0);
1587 	}
1588 	/*
1589 	 * If we not executing an auto-sense and the status code
1590 	 * is request-sense, we automatically issue a request
1591 	 * sense command.
1592 	 */
1593 	PID("cautos1");
1594 	if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1595 		switch (reqp->status & SCSMASK) {
1596 		    case SCSCHKC:
1597 			memcpy( &reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1598 			reqp->xcmd_len = sizeof(sense_cmd);
1599 			reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense;
1600 			reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense);
1601 			reqp->dr_flag  |= DRIVER_AUTOSEN;
1602 			reqp->dr_flag  &= ~DRIVER_DMAOK;
1603 			if (!linked) {
1604 				sps = splbio();
1605 				reqp->next = issue_q;
1606 				issue_q    = reqp;
1607 				splx(sps);
1608 			}
1609 			else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
1610 
1611 #ifdef DBG_REQ
1612 			memset(reqp->xdata_ptr, 0, reqp->xdata_len);
1613 			if (dbg_target_mask & (1 << reqp->targ_id))
1614 				show_request(reqp, "AUTO-SENSE");
1615 #endif
1616 			PID("cautos2");
1617 			return (-1);
1618 		    case SCSBUSY:
1619 			reqp->xs->error = XS_BUSY;
1620 			return (0);
1621 		}
1622 	}
1623 	else {
1624 		/*
1625 		 * An auto-sense has finished
1626 		 */
1627 		if ((reqp->status & SCSMASK) != SCSGOOD)
1628 			reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1629 		else reqp->xs->error = XS_SENSE;
1630 		reqp->status = SCSCHKC;
1631 	}
1632 	PID("cautos3");
1633 	return (0);
1634 }
1635 
1636 static int
1637 reach_msg_out(struct ncr_softc *sc, u_long len)
1638 {
1639 	u_char	phase;
1640 	u_char	data;
1641 	u_long	n = len;
1642 
1643 	ncr_aprint(sc, "Trying to reach Message-out phase\n");
1644 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1645 		phase = (phase >> 2) & 7;
1646 	else return (-1);
1647 	ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1648 	if (phase == PH_MSGOUT)
1649 		return (0);
1650 
1651 	SET_5380_REG(NCR5380_TCOM, phase);
1652 
1653 	do {
1654 		if (!wait_req_true())
1655 			break;
1656 		if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1657 			break;
1658 		if (PH_IN(phase)) {
1659 			data = GET_5380_REG(NCR5380_DATA);
1660 			SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1661 		}
1662 		else {
1663 			SET_5380_REG(NCR5380_DATA, 0);
1664 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN);
1665 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1666 		}
1667 		if (!wait_req_false())
1668 			break;
1669 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1670 	} while (--n);
1671 
1672 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1673 		phase = (phase >> 2) & 7;
1674 		if (phase == PH_MSGOUT) {
1675 			ncr_aprint(sc, "Message-out phase reached after "
1676 					"%ld bytes.\n", len - n);
1677 			return (0);
1678 		}
1679 		ncr_aprint(sc, "Phase now: %d after %ld bytes.\n", phase,
1680 								   len - n);
1681 
1682 	}
1683 	return (-1);
1684 }
1685 
1686 void
1687 scsi_reset(void)
1688 {
1689 	SC_REQ	*tmp, *next;
1690 	int	sps;
1691 
1692 	PID("scsi_reset1");
1693 	sps = splbio();
1694 	SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1695 	delay(100);
1696 	SET_5380_REG(NCR5380_ICOM, 0);
1697 	scsi_clr_ipend();
1698 
1699 	/*
1700 	 * None of the jobs in the discon_q will ever be reconnected,
1701 	 * notify this to the higher level code.
1702 	 */
1703 	for (tmp = discon_q; tmp ;) {
1704 		next = tmp->next;
1705 		tmp->next = NULL;
1706 		tmp->xs->error = XS_TIMEOUT;
1707 		busy &= ~(1 << tmp->targ_id);
1708 		finish_req(tmp);
1709 		tmp = next;
1710 	}
1711 	discon_q = NULL;
1712 
1713 	/*
1714 	 * The current job will never finish either.
1715 	 * The problem is that we can't finish the job because an instance
1716 	 * of main is running on it. Our best guess is that the job is currently
1717 	 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1718 	 * the job because it detects BSY-loss.
1719 	 */
1720 	if ((tmp = connected) != NULL) {
1721 		if (tmp->dr_flag & DRIVER_IN_DMA) {
1722 			tmp->xs->error = XS_DRIVER_STUFFUP;
1723 #ifdef REAL_DMA
1724 			dma_ready();
1725 #endif
1726 		}
1727 	}
1728 	splx(sps);
1729 	PID("scsi_reset2");
1730 
1731 	/*
1732 	 * Give the attached devices some time to handle the reset. This
1733 	 * value is arbitrary but should be relatively long.
1734 	 */
1735 	delay(100000);
1736 }
1737 
1738 static void
1739 scsi_reset_verbose(struct ncr_softc *sc, const char *why)
1740 {
1741 	ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
1742 
1743 	scsi_reset();
1744 }
1745 
1746 /*
1747  * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1748  * the appropriate action is carried out (reselection or DMA ready) and
1749  * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1750  * and INTR_SPURIOUS is returned.
1751  */
1752 static int
1753 check_intr(struct ncr_softc *sc)
1754 {
1755 	SC_REQ	*reqp;
1756 
1757 	if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1758 							==(SC_S_SEL|SC_S_IO))
1759 		return (INTR_RESEL);
1760 	else {
1761 		if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1762 			reqp->dr_flag &= ~DRIVER_IN_DMA;
1763 			return (INTR_DMA);
1764 		}
1765 	}
1766 	printf("-->");
1767 	scsi_show();
1768 	scsi_clr_ipend();
1769 	ncr_aprint(sc, "Spurious interrupt.\n");
1770 	return (INTR_SPURIOUS);
1771 }
1772 
1773 #ifdef REAL_DMA
1774 /*
1775  * Check if DMA can be used for this request. This function also builds
1776  * the DMA-chain.
1777  */
1778 static int
1779 scsi_dmaok(SC_REQ *reqp)
1780 {
1781 	u_long			phy_buf;
1782 	u_long			phy_len;
1783 	char			*req_addr;
1784 	u_long			req_len;
1785 	struct dma_chain	*dm;
1786 
1787 	/*
1788 	 * To be safe, do not use DMA for Falcon
1789 	 */
1790 	if (machineid & ATARI_FALCON)
1791 		return (0);
1792 
1793 	/*
1794 	 * Initialize locals and requests' DMA-chain.
1795 	 */
1796 	req_len        = reqp->xdata_len;
1797 	req_addr       = (void *)reqp->xdata_ptr;
1798 	dm             = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1799 	dm->dm_count   = dm->dm_addr = 0;
1800 	reqp->dr_flag &= ~DRIVER_BOUNCING;
1801 
1802 	/*
1803 	 * Do not accept zero length DMA.
1804 	 */
1805 	if (req_len == 0)
1806 		return (0);
1807 
1808 	/*
1809 	 * If DMA is emulated in software, we don't have to breakup the
1810 	 * request. Just build a chain with a single element and stash in
1811 	 * the KVA and not the KPA.
1812 	 */
1813 	if (emulated_dma()) {
1814 		dm->dm_addr    = (u_long)req_addr;
1815 		dm->dm_count   = req_len;
1816 		return (1);
1817 	}
1818 
1819 	/*
1820 	 * LWP: I think that this restriction is not strictly nessecary.
1821 	 */
1822 	if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1823 		return (0);
1824 
1825 	/*
1826 	 * Build the DMA-chain.
1827 	 */
1828 	dm->dm_addr = phy_buf = kvtop(req_addr);
1829 	while (req_len) {
1830 		if (req_len <
1831 		    (phy_len = PAGE_SIZE - ((u_long)req_addr & PGOFSET)))
1832 			phy_len = req_len;
1833 
1834 		req_addr     += phy_len;
1835 		req_len      -= phy_len;
1836 		dm->dm_count += phy_len;
1837 
1838 		if (req_len) {
1839 			u_long	tmp = kvtop(req_addr);
1840 
1841 			if ((phy_buf + phy_len) != tmp) {
1842 			    if (wrong_dma_range(reqp, dm)) {
1843 				if (reqp->dr_flag & DRIVER_BOUNCING)
1844 					goto bounceit;
1845 				return (0);
1846 			    }
1847 
1848 			    if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1849 				ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1850 				return (0);
1851 			    }
1852 			    dm->dm_count = 0;
1853 			    dm->dm_addr  = tmp;
1854 			}
1855 			phy_buf = tmp;
1856 		}
1857 	}
1858         if (wrong_dma_range(reqp, dm)) {
1859 		if (reqp->dr_flag & DRIVER_BOUNCING)
1860 			goto bounceit;
1861 		return (0);
1862 	}
1863 	reqp->dm_last = dm;
1864 	return (1);
1865 
1866 bounceit:
1867 	if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1868 		/*
1869 		 * If we can't get a bounce buffer, forget DMA
1870 		 */
1871 		reqp->dr_flag &= ~DRIVER_BOUNCING;
1872 		return(0);
1873 	}
1874 	/*
1875 	 * Initialize a single DMA-range containing the bounced request
1876 	 */
1877 	dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1878 	dm->dm_addr    = kvtop(reqp->bounceb);
1879 	dm->dm_count   = reqp->xdata_len;
1880 	reqp->bouncerp = reqp->bounceb;
1881 
1882 	return (1);
1883 }
1884 #endif /* REAL_DMA */
1885 
1886 static void
1887 run_main(struct ncr_softc *sc)
1888 {
1889 	int	sps = splbio();
1890 
1891 	if (!main_running) {
1892 		/*
1893 		 * If shared resources are required, claim them
1894 		 * before entering 'scsi_main'. If we can't get them
1895 		 * now, assume 'run_main' will be called when the resource
1896 		 * becomes available.
1897 		 */
1898 		if (!claimed_dma()) {
1899 			splx(sps);
1900 			return;
1901 		}
1902 		main_running = 1;
1903 		splx(sps);
1904 		scsi_main(sc);
1905 	}
1906 	else splx(sps);
1907 }
1908 
1909 /*
1910  * Prefix message with full target info.
1911  */
1912 static void
1913 ncr_tprint(SC_REQ *reqp, const char *fmt, ...)
1914 {
1915 	va_list	ap;
1916 
1917 	va_start(ap, fmt);
1918 	scsipi_printaddr(reqp->xs->xs_periph);
1919 	vprintf(fmt, ap);
1920 	va_end(ap);
1921 }
1922 
1923 /*
1924  * Prefix message with adapter info.
1925  */
1926 static void
1927 ncr_aprint(struct ncr_softc *sc, const char *fmt, ...)
1928 {
1929 	va_list	ap;
1930 	char buf[256];
1931 
1932 	va_start(ap, fmt);
1933 	vsnprintf(buf, sizeof(buf), fmt, ap);
1934 	va_end(ap);
1935 
1936 	printf("%s: %s", sc->sc_dev.dv_xname, buf);
1937 }
1938 /****************************************************************************
1939  *		Start Debugging Functions				    *
1940  ****************************************************************************/
1941 static const char *phase_names[] = {
1942 	"DATA_OUT", "DATA_IN", "COMMAND", "STATUS", "NONE", "NONE", "MSG_OUT",
1943 	"MSG_IN"
1944 };
1945 
1946 static void
1947 show_phase(SC_REQ *reqp, int phase)
1948 {
1949 	printf("INFTRANS: %d Phase = %s\n", reqp->targ_id, phase_names[phase]);
1950 }
1951 
1952 static void
1953 show_data_sense(struct scsipi_xfer *xs)
1954 {
1955 	u_char	*p1, *p2;
1956 	int	i;
1957 
1958 	p1 = (u_char *) xs->cmd;
1959 	p2 = (u_char *)&xs->sense.scsi_sense;
1960 	if(*p2 == 0)
1961 		return;	/* No(n)sense */
1962 	printf("cmd[%d]: ", xs->cmdlen);
1963 	for (i = 0; i < xs->cmdlen; i++)
1964 		printf("%x ", p1[i]);
1965 	printf("\nsense: ");
1966 	for (i = 0; i < sizeof(xs->sense.scsi_sense); i++)
1967 		printf("%x ", p2[i]);
1968 	printf("\n");
1969 }
1970 
1971 static void
1972 show_request(SC_REQ *reqp, const char *qtxt)
1973 {
1974 	printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
1975 			qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1976 			reqp->xcmd.opcode, reqp->status, reqp->message,
1977 			reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
1978 			reqp->link ? "L":"");
1979 	if (reqp->status == SCSCHKC)
1980 		show_data_sense(reqp->xs);
1981 }
1982 
1983 static const char *sig_names[] = {
1984 	"PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
1985 	"ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
1986 };
1987 
1988 static void
1989 show_signals(u_char dmstat, u_char idstat)
1990 {
1991 	u_short	tmp, mask;
1992 	int	j, need_pipe;
1993 
1994 	tmp = idstat | ((dmstat & 3) << 8);
1995 	printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
1996 	for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
1997 		if (tmp & mask)
1998 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1999 	}
2000 	printf("\nDma status (%02x): ", dmstat);
2001 	for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
2002 		if (dmstat & mask)
2003 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
2004 	}
2005 	printf("\n");
2006 }
2007 
2008 void
2009 scsi_show(void)
2010 {
2011 	SC_REQ	*tmp;
2012 	int	sps = splhigh();
2013 	u_char	idstat, dmstat;
2014 	int	i;
2015 
2016 	printf("scsi_show: scsi_main is%s running\n",
2017 		main_running ? "" : " not");
2018 	for (tmp = issue_q; tmp; tmp = tmp->next)
2019 		show_request(tmp, "ISSUED");
2020 	for (tmp = discon_q; tmp; tmp = tmp->next)
2021 		show_request(tmp, "DISCONNECTED");
2022 	if (connected)
2023 		show_request(connected, "CONNECTED");
2024 	if (can_access_5380()) {
2025 		idstat = GET_5380_REG(NCR5380_IDSTAT);
2026 		dmstat = GET_5380_REG(NCR5380_DMSTAT);
2027 		show_signals(dmstat, idstat);
2028 	}
2029 
2030 	if (connected)
2031 		printf("phase = %d, ", connected->phase);
2032 	printf("busy:%x, spl:%04x\n", busy, sps);
2033 #ifdef	DBG_PID
2034 	for (i=0; i<DBG_PID; i++)
2035 		printf("\t%d\t%s\n", i, last_hit[i]);
2036 #endif
2037 
2038 	splx(sps);
2039 }
2040