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