xref: /netbsd-src/sys/arch/mac68k/dev/ncr5380.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: ncr5380.c,v 1.62 2006/02/25 00:58:35 wiz 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.62 2006/02/25 00:58:35 wiz 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 u_char	ncr_will_link = 0x00;
58 #ifdef	TRY_SCSI_LINKED_COMMANDS
59 u_char	ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
60 #else
61 u_char	ncr_test_link = 0x7f;
62 #endif
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 *);
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 *);
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  * -- A sleeping Fujitsu M2512 is even worse; try 2.5 sec     -hf 20 Jun
105  */
106 extern inline int wait_req_true(void)
107 {
108 	int	timeout = 2500000;
109 
110 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
111 		delay(1);
112 	return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
113 }
114 
115 /*
116  * Wait for request-line to become inactive. When it doesn't return 0.
117  * Otherwise return != 0.
118  */
119 extern inline int wait_req_false(void)
120 {
121 	int	timeout = 2500000;
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 extern inline void ack_message(void)
129 {
130 	SET_5380_REG(NCR5380_ICOM, 0);
131 }
132 
133 extern inline void nack_message(SC_REQ *reqp, u_char msg)
134 {
135 	SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
136 	reqp->msgout = msg;
137 }
138 
139 extern inline void finish_req(SC_REQ *reqp)
140 {
141 	int			sps;
142 	struct scsipi_xfer	*xs = reqp->xs;
143 
144 #ifdef REAL_DMA
145 	/*
146 	 * If we bounced, free the bounce buffer
147 	 */
148 	if (reqp->dr_flag & DRIVER_BOUNCING)
149 		free_bounceb(reqp->bounceb);
150 #endif /* REAL_DMA */
151 #ifdef DBG_REQ
152 	if (dbg_target_mask & (1 << reqp->targ_id))
153 		show_request(reqp, "DONE");
154 #endif
155 #ifdef DBG_ERR_RET
156 	if (reqp->xs->error != 0)
157 		show_request(reqp, "ERR_RET");
158 #endif
159 	/*
160 	 * Return request to free-q
161 	 */
162 	sps = splbio();
163 	reqp->next = free_head;
164 	free_head  = reqp;
165 	splx(sps);
166 
167 	xs->xs_status |= XS_STS_DONE;
168 	if (!(reqp->dr_flag & DRIVER_LINKCHK))
169 		scsipi_done(xs);
170 }
171 
172 /*
173  * Auto config stuff....
174  */
175 void	ncr_attach(struct device *, struct device *, void *);
176 int	ncr_match(struct device *, struct cfdata *, void *);
177 
178 /*
179  * Tricks to make driver-name configurable
180  */
181 #define CFNAME(n)	__CONCAT(n,_cd)
182 #define CANAME(n)	__CONCAT(n,_ca)
183 #define CFSTRING(n)	__STRING(n)
184 #define	CFDRNAME(n)	n
185 
186 CFATTACH_DECL(CFDRNAME(DRNAME), sizeof(struct ncr_softc),
187     ncr_match, ncr_attach, NULL, NULL);
188 
189 extern struct cfdriver CFNAME(DRNAME);
190 
191 int
192 ncr_match(struct device *parent, struct cfdata *cf, void *aux)
193 {
194 	return (machine_match(parent, cf, aux, &CFNAME(DRNAME)));
195 }
196 
197 void
198 ncr_attach(struct device *pdp, struct device *dp, void *auxp)
199 {
200 	struct ncr_softc	*sc;
201 	int			i;
202 
203 	sc = (struct ncr_softc *)dp;
204 
205 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
206 	sc->sc_adapter.adapt_openings = 7;
207 	sc->sc_adapter.adapt_max_periph = 1;
208 	sc->sc_adapter.adapt_ioctl = NULL;
209 	sc->sc_adapter.adapt_minphys = ncr5380_minphys;
210 	sc->sc_adapter.adapt_request = ncr5380_scsi_request;
211 
212 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
213 	sc->sc_channel.chan_bustype = &scsi_bustype;
214 	sc->sc_channel.chan_channel = 0;
215 	sc->sc_channel.chan_ntargets = 8;
216 	sc->sc_channel.chan_nluns = 8;
217 	sc->sc_channel.chan_id = 7;
218 
219 	/*
220 	 * bitmasks
221 	 */
222 	sc->sc_noselatn = 0;
223 	sc->sc_selected = 0;
224 
225 	/*
226 	 * Initialize machine-type specific things...
227 	 */
228 	scsi_mach_init(sc);
229 	printf("\n");
230 
231 	/*
232 	 * Initialize request queue freelist.
233 	 */
234 	for (i = 0; i < NREQ; i++) {
235 		req_queue[i].next = free_head;
236 		free_head = &req_queue[i];
237 	}
238 
239 	/*
240 	 * Initialize the host adapter
241 	 */
242 	scsi_idisable();
243 	ENABLE_NCR5380(sc);
244 	SET_5380_REG(NCR5380_ICOM, 0);
245 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
246 	SET_5380_REG(NCR5380_TCOM, 0);
247 	SET_5380_REG(NCR5380_IDSTAT, 0);
248 	scsi_ienable();
249 
250 	/*
251 	 * attach all scsi units on us
252 	 */
253 	config_found(dp, &sc->sc_channel, scsiprint);
254 }
255 
256 /*
257  * End of auto config stuff....
258  */
259 
260 /*
261  * Carry out a request from the high level driver.
262  */
263 static void
264 ncr5380_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
265     void *arg)
266 {
267 	struct scsipi_xfer *xs;
268 	struct scsipi_periph *periph;
269 	struct ncr_softc *sc = (void *)chan->chan_adapter->adapt_dev;
270 	int	sps, flags;
271 	SC_REQ	*reqp, *link, *tmp;
272 
273 	switch (req) {
274 	case ADAPTER_REQ_RUN_XFER:
275 		xs = arg;
276 		flags = xs->xs_control;
277 		periph = xs->xs_periph;
278 
279 		/*
280 		 * We do not queue RESET commands
281 		 */
282 		if (flags & XS_CTL_RESET) {
283 			scsi_reset_verbose(sc, "Got reset-command");
284 			scsipi_done(xs);
285 			return;
286 		}
287 
288 		/*
289 		 * Get a request block
290 		 */
291 		sps = splbio();
292 		if ((reqp = free_head) == 0) {
293 			xs->error = XS_RESOURCE_SHORTAGE;
294 			scsipi_done(xs);
295 			return;
296 		}
297 		free_head  = reqp->next;
298 		reqp->next = NULL;
299 		splx(sps);
300 
301 		/*
302 		 * Initialize our private fields
303 		 */
304 		reqp->dr_flag   = (flags & XS_CTL_POLL) ? DRIVER_NOINT : 0;
305 		reqp->phase     = NR_PHASE;
306 		reqp->msgout    = MSG_NOOP;
307 		reqp->status    = SCSGOOD;
308 		reqp->message   = 0xff;
309 		reqp->link      = NULL;
310 		reqp->xs        = xs;
311 		reqp->targ_id   = xs->xs_periph->periph_target;
312 		reqp->targ_lun  = xs->xs_periph->periph_lun;
313 		reqp->xdata_ptr = (u_char*)xs->data;
314 		reqp->xdata_len = xs->datalen;
315 		memcpy(&reqp->xcmd, xs->cmd, xs->cmdlen);
316 		reqp->xcmd_len = xs->cmdlen;
317 		reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
318 
319 #ifdef REAL_DMA
320 		/*
321 		 * Check if DMA can be used on this request
322 		 */
323 		if (scsi_dmaok(reqp))
324 			reqp->dr_flag |= DRIVER_DMAOK;
325 #endif /* REAL_DMA */
326 
327 		/*
328 		 * Insert the command into the issue queue. Note that
329 		 * 'REQUEST SENSE' commands are inserted at the head of the
330 		 * queue since any command will clear the existing contingent
331 		 * allegience condition and the sense data is only valid while
332 		 * the condition exists.
333 		 * When possible, link the command to a previous command to
334 		 * the same target. This is not very sensible when AUTO_SENSE
335 		 * is not defined!  Interrupts are disabled while we are
336 		 * fiddling with the issue-queue.
337 		 */
338 		sps = splbio();
339 		link = NULL;
340 		if ((issue_q == NULL) ||
341 		    (reqp->xcmd.opcode == SCSI_REQUEST_SENSE)) {
342 			reqp->next = issue_q;
343 			issue_q    = reqp;
344 		} else {
345 			tmp  = issue_q;
346 			do {
347 				if (!link && (tmp->targ_id == reqp->targ_id) &&
348 				    !tmp->link)
349 					link = tmp;
350 			} while (tmp->next && (tmp = tmp->next));
351 			tmp->next = reqp;
352 #ifdef AUTO_SENSE
353 			if (link && (ncr_will_link & (1<<reqp->targ_id))) {
354 				link->link = reqp;
355 				link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
356 			}
357 #endif
358 		}
359 #ifdef AUTO_SENSE
360 		/*
361 		 * If we haven't already, check the target for link support.
362 		 * Do this by prefixing the current command with a dummy
363 		 * Request_Sense command, link the dummy to the current
364 		 * command, and insert the dummy command at the head of the
365 		 * issue queue.  Set the DRIVER_LINKCHK flag so that we'll
366 		 * ignore the results of the dummy command, since we only
367 		 * care about whether it was accepted or not.
368 		 */
369 		if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
370 		    (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
371 			free_head = tmp->next;
372 			tmp->dr_flag =
373 			    (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
374 			tmp->phase = NR_PHASE;
375 			tmp->msgout = MSG_NOOP;
376 			tmp->status = SCSGOOD;
377 			tmp->xs = reqp->xs;
378 			tmp->targ_id = reqp->targ_id;
379 			tmp->targ_lun = reqp->targ_lun;
380 			memcpy(&tmp->xcmd, sense_cmd, sizeof(sense_cmd));
381 			tmp->xcmd_len = sizeof(sense_cmd);
382 			tmp->xdata_ptr = (u_char *)&tmp->xs->sense.scsi_sense;
383 			tmp->xdata_len = sizeof(tmp->xs->sense.scsi_sense);
384 			ncr_test_link |= 1<<tmp->targ_id;
385 			tmp->link = reqp;
386 			tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
387 			tmp->next = issue_q;
388 			issue_q = tmp;
389 #ifdef DBG_REQ
390 			if (dbg_target_mask & (1 << tmp->targ_id))
391 				show_request(tmp, "LINKCHK");
392 #endif
393 		}
394 #endif
395 		splx(sps);
396 
397 #ifdef DBG_REQ
398 		if (dbg_target_mask & (1 << reqp->targ_id))
399 			show_request(reqp,
400 			    (reqp->xcmd.opcode == SCSI_REQUEST_SENSE) ?
401 			    "HEAD":"TAIL");
402 #endif
403 
404 		run_main(sc);
405 		return;
406 
407 	case ADAPTER_REQ_GROW_RESOURCES:
408 		/* XXX Not supported. */
409 		return;
410 
411 	case ADAPTER_REQ_SET_XFER_MODE:
412 		/* XXX Not supported. */
413 		return;
414 	}
415 }
416 
417 static void
418 ncr5380_minphys(struct buf *bp)
419 {
420     if (bp->b_bcount > MIN_PHYS)
421 	bp->b_bcount = MIN_PHYS;
422     minphys(bp);
423 }
424 #undef MIN_PHYS
425 
426 static void
427 ncr5380_show_scsi_cmd(struct scsipi_xfer *xs)
428 {
429 	u_char	*b = (u_char *) xs->cmd;
430 	int	i  = 0;
431 
432 	scsipi_printaddr(xs->xs_periph);
433 	if (!(xs->xs_control & XS_CTL_RESET)) {
434 		while (i < xs->cmdlen) {
435 			if (i)
436 				printf(",");
437 			printf("%x",b[i++]);
438 		}
439 		printf("-\n");
440 	}
441 	else {
442 		printf("-RESET-\n");
443 	}
444 }
445 
446 /*
447  * The body of the driver.
448  */
449 static void
450 scsi_main(struct ncr_softc *sc)
451 {
452 	SC_REQ	*req, *prev;
453 	int	itype;
454 	int	sps;
455 
456 	/*
457 	 * While running in the driver SCSI-interrupts are disabled.
458 	 */
459 	scsi_idisable();
460 	ENABLE_NCR5380(sc);
461 
462 	PID("scsi_main1");
463 	for (;;) {
464 	    sps = splbio();
465 	    if (!connected) {
466 		/*
467 		 * Check if it is fair keep any exclusive access to DMA
468 		 * claimed. If not, stop queueing new jobs so the discon_q
469 		 * will be eventually drained and DMA can be given up.
470 		 */
471 		if (!fair_to_keep_dma())
472 			goto main_exit;
473 
474 		/*
475 		 * Search through the issue-queue for a command
476 		 * destined for a target that isn't busy.
477 		 */
478 		prev = NULL;
479 		for (req=issue_q; req != NULL; prev = req, req = req->next) {
480 			if (!(busy & (1 << req->targ_id))) {
481 				/*
482 				 * Found one, remove it from the issue queue
483 				 */
484 				if (prev == NULL)
485 					issue_q = req->next;
486 				else prev->next = req->next;
487 				req->next = NULL;
488 				break;
489 			}
490 		}
491 
492 		/*
493 		 * When a request has just ended, we get here before an other
494 		 * device detects that the bus is free and that it can
495 		 * reconnect. The problem is that when this happens, we always
496 		 * baffle the device because our (initiator) id is higher. This
497 		 * can cause a sort of starvation on slow devices. So we check
498 		 * for a pending reselection here.
499 		 * Note that 'connected' will be non-null if the reselection
500 		 * succeeds.
501 		 */
502 		if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
503 						== (SC_S_SEL|SC_S_IO)){
504 			if (req != NULL) {
505 				req->next = issue_q;
506 				issue_q = req;
507 			}
508 			splx(sps);
509 
510 			reselect(sc);
511 			scsi_clr_ipend();
512 			goto connected;
513 		}
514 
515 		/*
516 		 * The host is not connected and there is no request
517 		 * pending, exit.
518 		 */
519 		if (req == NULL) {
520 			PID("scsi_main2");
521 			goto main_exit;
522 		}
523 
524 		/*
525 		 * Re-enable interrupts before handling the request.
526 		 */
527 		splx(sps);
528 
529 #ifdef DBG_REQ
530 		if (dbg_target_mask & (1 << req->targ_id))
531 			show_request(req, "TARGET");
532 #endif
533 		/*
534 		 * We found a request. Try to connect to the target. If the
535 		 * initiator fails arbitration, the command is put back in the
536 		 * issue queue.
537 		 */
538 		if (scsi_select(req, 0)) {
539 			sps = splbio();
540 			req->next = issue_q;
541 			issue_q = req;
542 			splx(sps);
543 #ifdef DBG_REQ
544 			if (dbg_target_mask & (1 << req->targ_id))
545 				ncr_tprint(req, "Select failed\n");
546 #endif
547 		}
548 	    }
549 	    else splx(sps);
550 connected:
551 	    if (connected) {
552 		/*
553 		 * If the host is currently connected but a 'real-DMA' transfer
554 		 * is in progress, the 'end-of-DMA' interrupt restarts main.
555 		 * So quit.
556 		 */
557 		sps = splbio();
558 		if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
559 			PID("scsi_main3");
560 			goto main_exit;
561 		}
562 		splx(sps);
563 
564 		/*
565 		 * Let the target guide us through the bus-phases
566 		 */
567 		while (information_transfer(sc) == -1)
568 			;
569 	    }
570 	}
571 	/* NEVER TO REACH HERE */
572 	panic("ncr5380-SCSI: not designed to come here");
573 
574 main_exit:
575 	/*
576 	 * We enter here with interrupts disabled. We are about to exit main
577 	 * so interrupts should be re-enabled. Because interrupts are edge
578 	 * triggered, we could already have missed the interrupt. Therefore
579 	 * we check the IRQ-line here and re-enter when we really missed a
580 	 * valid interrupt.
581 	 */
582 	PID("scsi_main4");
583 	scsi_ienable();
584 
585 	/*
586 	 * If we're not currently connected, enable reselection
587 	 * interrupts.
588 	 */
589 	if (!connected)
590 		SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
591 
592 	if (scsi_ipending()) {
593 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
594 			scsi_idisable();
595 			splx(sps);
596 
597 			if (itype == INTR_RESEL)
598 				reselect(sc);
599 #ifdef REAL_DMA
600 			else dma_ready();
601 #else
602 			else {
603 				if (pdma_ready())
604 					goto connected;
605 				panic("Got DMA interrupt without DMA");
606 			}
607 #endif
608 			scsi_clr_ipend();
609 			goto connected;
610 		}
611 	}
612 	reconsider_dma();
613 
614 	main_running = 0;
615 	splx(sps);
616 	PID("scsi_main5");
617 }
618 
619 #ifdef REAL_DMA
620 /*
621  * The SCSI-DMA interrupt.
622  * This interrupt can only be triggered when running in non-polled DMA
623  * mode. When DMA is not active, it will be silently ignored, it is usually
624  * to late because the EOP interrupt of the controller happens just a tiny
625  * bit earlier. It might become usefull when scatter/gather is implemented,
626  * because in that case only part of the DATAIN/DATAOUT transfer is taken
627  * out of a single buffer.
628  */
629 static void
630 ncr_dma_intr(struct ncr_softc *sc)
631 {
632 	SC_REQ	*reqp;
633 	int	dma_done;
634 
635 	PID("ncr_dma_intr");
636 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
637 		scsi_idisable();
638 		if (!(dma_done = dma_ready())) {
639 			transfer_dma(reqp, reqp->phase, 0);
640 			return;
641 		}
642 		run_main(sc);
643 	}
644 }
645 #endif /* REAL_DMA */
646 
647 /*
648  * The SCSI-controller interrupt. This interrupt occurs on reselections and
649  * at the end of non-polled DMA-interrupts. It is assumed to be called from
650  * the machine-dependent hardware interrupt.
651  */
652 static void
653 ncr_ctrl_intr(struct ncr_softc *sc)
654 {
655 	int	itype;
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	tphase = PH_MSGOUT;
912 			u_char	msg   = MSG_ABORT;
913 
914 			transfer_pio(&tphase, &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 		DBG_INFPRINT(show_phase, reqp, phase);
989 	}
990 	else {
991 		/*
992 		 * Same data-phase. If same error give up
993 		 */
994 		if ((reqp->msgout == MSG_ABORT)
995 		     && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
996 			busy     &= ~(1 << reqp->targ_id);
997 			connected = NULL;
998 			finish_req(reqp);
999 			scsi_reset_verbose(sc, "Failure to abort command");
1000 			return (0);
1001 		}
1002 	}
1003 
1004 	switch (phase) {
1005 	    case PH_DATAOUT:
1006 #ifdef DBG_NOWRITE
1007 		ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
1008 		reqp->msgout = MSG_ABORT;
1009 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1010 		return (-1);
1011 #endif /* DBG_NOWRITE */
1012 		/*
1013 		 * If this is the first write using DMA, fill
1014 		 * the bounce buffer.
1015 		 */
1016 		if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
1017 		    if (reqp->dr_flag & DRIVER_BOUNCING)
1018 			memcpy(reqp->bounceb, reqp->xdata_ptr, reqp->xdata_len);
1019 		}
1020 
1021 	   case PH_DATAIN:
1022 		if (reqp->xdata_len <= 0) {
1023 			/*
1024 			 * Target keeps requesting data. Try to get into
1025 			 * message-out phase by feeding/taking 100 byte.
1026 			 */
1027 			ncr_tprint(reqp, "Target requests too much data\n");
1028 			reqp->msgout = MSG_ABORT;
1029 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1030 			reach_msg_out(sc, 100);
1031 			return (-1);
1032 		}
1033 #ifdef REAL_DMA
1034 		if (reqp->dr_flag & DRIVER_DMAOK) {
1035 			int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1036 			transfer_dma(reqp, phase, poll);
1037 			if (!poll)
1038 				return (0);
1039 		}
1040 		else
1041 #endif
1042 		{
1043 			PID("info_transf3");
1044 			len = reqp->xdata_len;
1045 #ifdef USE_PDMA
1046 			if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1047 				return (0);
1048 #else
1049 			transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1050 #endif
1051 			reqp->xdata_ptr += reqp->xdata_len - len;
1052 			reqp->xdata_len  = len;
1053 		}
1054 		return (-1);
1055 	   case PH_MSGIN:
1056 		/*
1057 		 * We only expect single byte messages here.
1058 		 */
1059 		len = 1;
1060 		transfer_pio(&phase, &tmp, &len, 1);
1061 		reqp->message = tmp;
1062 		return (handle_message(reqp, tmp));
1063 	   case PH_MSGOUT:
1064 		len = 1;
1065 		transfer_pio(&phase, &reqp->msgout, &len, 0);
1066 		if (reqp->msgout == MSG_ABORT) {
1067 			busy     &= ~(1 << reqp->targ_id);
1068 			connected = NULL;
1069 			if (!reqp->xs->error)
1070 				reqp->xs->error = XS_DRIVER_STUFFUP;
1071 			finish_req(reqp);
1072 			PID("info_transf4");
1073 			return (0);
1074 		}
1075 		reqp->msgout = MSG_NOOP;
1076 		return (-1);
1077 	   case PH_CMD :
1078 		len = reqp->xcmd_len;
1079 		transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1080 		PID("info_transf5");
1081 		return (-1);
1082 	   case PH_STATUS:
1083 		len = 1;
1084 		transfer_pio(&phase, &tmp, &len, 0);
1085 		reqp->status = tmp;
1086 		PID("info_transf6");
1087 		return (-1);
1088 	   default :
1089 		ncr_tprint(reqp, "Unknown phase\n");
1090 	}
1091 	PID("info_transf7");
1092 	return (-1);
1093 }
1094 
1095 /*
1096  * Handle the message 'msg' send to us by the target.
1097  * Return values:
1098  *	 0 : The current command has completed.
1099  *	-1 : Get on to the next phase.
1100  */
1101 static int
1102 handle_message(SC_REQ *reqp, u_int msg)
1103 {
1104 	int	sps;
1105 	SC_REQ	*prev, *req;
1106 
1107 	PID("hmessage1");
1108 	switch (msg) {
1109 		/*
1110 		 * Linking lets us reduce the time required to get
1111 		 * the next command to the device, skipping the arbitration
1112 		 * and selection time. In the current implementation,
1113 		 * we merely have to start the next command pointed
1114 		 * to by 'next_link'.
1115 		 */
1116 		case MSG_LINK_CMD_COMPLETE:
1117 		case MSG_LINK_CMD_COMPLETEF:
1118 			if (reqp->link == NULL) {
1119 				ncr_tprint(reqp, "No link for linked command");
1120 				nack_message(reqp, MSG_ABORT);
1121 				PID("hmessage2");
1122 				return (-1);
1123 			}
1124 			ack_message();
1125 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1126 				reqp->xs->resid = reqp->xdata_len;
1127 				reqp->xs->error = 0;
1128 			}
1129 
1130 #ifdef AUTO_SENSE
1131 			if (check_autosense(reqp, 1) == -1)
1132 				return (-1);
1133 #endif /* AUTO_SENSE */
1134 
1135 #ifdef DBG_REQ
1136 			if (dbg_target_mask & (1 << reqp->targ_id))
1137 				show_request(reqp->link, "LINK");
1138 #endif
1139 			connected = reqp->link;
1140 
1141 			/*
1142 			 * Unlink the 'linked' request from the issue_q
1143 			 */
1144 			sps  = splbio();
1145 			prev = NULL;
1146 			req  = issue_q;
1147 			for (; req != NULL; prev = req, req = req->next) {
1148 				if (req == connected)
1149 					break;
1150 			}
1151 			if (req == NULL)
1152 				panic("Inconsistent issue_q");
1153 			if (prev == NULL)
1154 				issue_q = req->next;
1155 			else prev->next = req->next;
1156 			req->next = NULL;
1157 			splx(sps);
1158 
1159 			finish_req(reqp);
1160 			PID("hmessage3");
1161 			return (-1);
1162 		case MSG_ABORT:
1163 		case MSG_CMDCOMPLETE:
1164 			ack_message();
1165 			connected = NULL;
1166 			busy     &= ~(1 << reqp->targ_id);
1167 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1168 				reqp->xs->resid = reqp->xdata_len;
1169 				reqp->xs->error = 0;
1170 			}
1171 
1172 #ifdef AUTO_SENSE
1173 			if (check_autosense(reqp, 0) == -1) {
1174 				PID("hmessage4");
1175 				return (0);
1176 			}
1177 #endif /* AUTO_SENSE */
1178 
1179 			finish_req(reqp);
1180 			PID("hmessage5");
1181 			return (0);
1182 		case MSG_MESSAGE_REJECT:
1183 			ack_message();
1184 			PID("hmessage6");
1185 			return (-1);
1186 		case MSG_DISCONNECT:
1187 			ack_message();
1188 #ifdef DBG_REQ
1189 			if (dbg_target_mask & (1 << reqp->targ_id))
1190 				show_request(reqp, "DISCON");
1191 #endif
1192 			sps = splbio();
1193 			connected  = NULL;
1194 			reqp->next = discon_q;
1195 			discon_q   = reqp;
1196 			splx(sps);
1197 			PID("hmessage7");
1198 			return (0);
1199 		case MSG_SAVEDATAPOINTER:
1200 		case MSG_RESTOREPOINTERS:
1201 			/*
1202 			 * We save pointers implicitely at disconnect.
1203 			 * So we can ignore these messages.
1204 			 */
1205 			ack_message();
1206 			PID("hmessage8");
1207 			return (-1);
1208 		case MSG_EXTENDED:
1209 			nack_message(reqp, MSG_MESSAGE_REJECT);
1210 			PID("hmessage9");
1211 			return (-1);
1212 		default:
1213 			if ((msg & 0x80) && !(msg & 0x18)) {	/* IDENTIFY */
1214 				PID("hmessage10");
1215 				ack_message();
1216 				return (0);
1217 			} else {
1218 				ncr_tprint(reqp,
1219 					   "Unknown message %x.  Rejecting.\n",
1220 					   msg);
1221 				nack_message(reqp, MSG_MESSAGE_REJECT);
1222 			}
1223 			return (-1);
1224 	}
1225 	PID("hmessage11");
1226 	return (-1);
1227 }
1228 
1229 /*
1230  * Handle reselection. If a valid reconnection occurs, connected
1231  * points at the reconnected command. The command is removed from the
1232  * disconnected queue.
1233  */
1234 static void
1235 reselect(struct ncr_softc *sc)
1236 {
1237 	u_char	phase;
1238 	u_long	len;
1239 	u_char	msg;
1240 	u_char	target_mask;
1241 	int	abort = 0;
1242 	SC_REQ	*tmp, *prev;
1243 
1244 	PID("reselect1");
1245 	target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1246 
1247 	/*
1248 	 * At this point, we have detected that our SCSI-id is on the bus,
1249 	 * SEL is true and BSY was false for at least one bus settle
1250 	 * delay (400 ns.).
1251 	 * We must assert BSY ourselves, until the target drops the SEL signal.
1252 	 * The SCSI-spec specifies no maximum time for this, so we have to
1253 	 * choose something long enough to suit all targets.
1254 	 */
1255 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1256 	len = 250000;
1257 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1258 		delay(1);
1259 		len--;
1260 	}
1261 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1262 		/* Damn SEL isn't dropping */
1263 		scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
1264 		return;
1265 	}
1266 
1267 	SET_5380_REG(NCR5380_ICOM, 0);
1268 
1269 	/*
1270 	 * Check if the reselection is still valid. Check twice because
1271 	 * of possible line glitches - cheaper than delay(1) and we need
1272 	 * only a few nanoseconds.
1273 	 */
1274 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1275 	    if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1276 		ncr_aprint(sc, "Stepped into the reselection timeout\n");
1277 		return;
1278 	    }
1279 	}
1280 
1281 	/*
1282 	 * Get the expected identify message.
1283 	 */
1284 	phase = PH_MSGIN;
1285 	len   = 1;
1286 	transfer_pio(&phase, &msg, &len, 0);
1287 	if (len || !MSG_ISIDENTIFY(msg)) {
1288 		ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1289 		abort = 1;
1290 		tmp = NULL;
1291 	}
1292 	else {
1293 	    /*
1294 	     * Find the command reconnecting
1295 	     */
1296 	    for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1297 		if (target_mask == (1 << tmp->targ_id)) {
1298 			if (prev)
1299 				prev->next = tmp->next;
1300 			else discon_q = tmp->next;
1301 			tmp->next = NULL;
1302 			break;
1303 		}
1304 	    }
1305 	    if (tmp == NULL) {
1306 		ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1307 								target_mask);
1308 		abort = 1;
1309 	    }
1310 	}
1311 	if (abort) {
1312 		msg   = MSG_ABORT;
1313 		len   = 1;
1314 		phase = PH_MSGOUT;
1315 
1316 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1317 		if (transfer_pio(&phase, &msg, &len, 0) || len)
1318 			scsi_reset_verbose(sc, "Failure to abort reselection");
1319 	}
1320 	else {
1321 		connected = tmp;
1322 #ifdef DBG_REQ
1323 		if (dbg_target_mask & (1 << tmp->targ_id))
1324 			show_request(tmp, "RECON");
1325 #endif
1326 	}
1327 	PID("reselect2");
1328 }
1329 
1330 /*
1331  * Transfer data in a given phase using programmed I/O.
1332  * Returns -1 when a different phase is entered without transferring the
1333  * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1334  * phase.
1335  */
1336 static int
1337 transfer_pio(u_char *phase, u_char *data, u_long *len, int dont_drop_ack)
1338 {
1339 	u_int	cnt = *len;
1340 	u_char	ph  = *phase;
1341 	u_char	tmp, new_icom;
1342 
1343 	DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1344 	PID("tpio1");
1345 	SET_5380_REG(NCR5380_TCOM, ph);
1346 	do {
1347 	    if (!wait_req_true()) {
1348 		DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1349 		break;
1350 	    }
1351 	    if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1352 		DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1353 		break;
1354 	    }
1355 	    if (PH_IN(ph)) {
1356 		*data++ = GET_5380_REG(NCR5380_DATA);
1357 		SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1358 		if ((cnt == 1) && dont_drop_ack)
1359 			new_icom = SC_A_ACK;
1360 		else new_icom = 0;
1361 	    }
1362 	    else {
1363 		SET_5380_REG(NCR5380_DATA, *data++);
1364 
1365 		/*
1366 		 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1367 		 * the initiator should drop ATN on the last byte of the
1368 		 * message phase after REQ has been asserted for the handshake
1369 		 * but before the initiator raises ACK.
1370 		 */
1371 		if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1372 			SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1373 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1374 			new_icom = 0;
1375 		}
1376 		else {
1377 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1378 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1379 			new_icom = SC_A_ATN;
1380 		}
1381 	    }
1382 	    if (!wait_req_false()) {
1383 		DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1384 		break;
1385 	    }
1386 	    SET_5380_REG(NCR5380_ICOM, new_icom);
1387 
1388 	} while (--cnt);
1389 
1390 	if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1391 		*phase = (tmp >> 2) & 7;
1392 	else *phase = NR_PHASE;
1393 	*len = cnt;
1394 	DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1395 								*phase, cnt);
1396 	PID("tpio2");
1397 	if (!cnt || (*phase == ph))
1398 		return (0);
1399 	return (-1);
1400 }
1401 
1402 #ifdef REAL_DMA
1403 /*
1404  * Start a DMA-transfer on the device using the current pointers.
1405  * If 'poll' is true, the function busy-waits until DMA has completed.
1406  */
1407 static void
1408 transfer_dma(SC_REQ *reqp, u_int phase, int poll)
1409 {
1410 	int	dma_done;
1411 	u_char	mbase = 0;
1412 	int	sps;
1413 
1414 again:
1415 	PID("tdma1");
1416 
1417 	/*
1418 	 * We should be in phase, otherwise we are not allowed to
1419 	 * drive the bus.
1420 	 */
1421 	SET_5380_REG(NCR5380_TCOM, phase);
1422 
1423 	/*
1424 	 * Defer interrupts until DMA is fully running.
1425 	 */
1426 	sps = splbio();
1427 
1428 	/*
1429 	 * Clear pending interrupts and parity errors.
1430 	 */
1431 	scsi_clr_ipend();
1432 
1433 	if (!poll) {
1434 		/*
1435 		 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1436 		 * to the interrupts we want enabled.
1437 		 */
1438 		scsi_ienable();
1439 		reqp->dr_flag |= DRIVER_IN_DMA;
1440 		mbase = SC_E_EOPI | SC_MON_BSY;
1441 	}
1442 	else scsi_idisable();
1443 	mbase |=  IMODE_BASE | SC_M_DMA;
1444 	scsi_dma_setup(reqp, phase, mbase);
1445 
1446 	splx(sps);
1447 
1448 	if (poll) {
1449 		/*
1450 		 * On polled-DMA transfers, we wait here until the
1451 		 * 'end-of-DMA' condition occurs.
1452 		 */
1453 		poll_edma(reqp);
1454 		if (!(dma_done = dma_ready()))
1455 			goto again;
1456 	}
1457 	PID("tdma2");
1458 }
1459 
1460 /*
1461  * Check results of a DMA data-transfer.
1462  */
1463 static int
1464 dma_ready(void)
1465 {
1466 	SC_REQ	*reqp = connected;
1467 	int	dmstat, is_edma;
1468 	long	bytes_left, bytes_done;
1469 
1470 	is_edma = get_dma_result(reqp, &bytes_left);
1471 	dmstat  = GET_5380_REG(NCR5380_DMSTAT);
1472 
1473 	/*
1474 	 * Check if the call is sensible and not caused by any spurious
1475 	 * interrupt.
1476 	 */
1477 	if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1478 		     && (dmstat & SC_PHS_MTCH) ) {
1479 		ncr_tprint(reqp, "dma_ready: spurious call "
1480 				 "(dm:%x,last_hit: %s)\n",
1481 #ifdef DBG_PID
1482 					dmstat, last_hit[DBG_PID-1]);
1483 #else
1484 					dmstat, "unknown");
1485 #endif
1486 		return (0);
1487 	}
1488 
1489 	/*
1490 	 * Clear all (pending) interrupts.
1491 	 */
1492 	scsi_clr_ipend();
1493 
1494 	/*
1495 	 * Update various transfer-pointers/lengths
1496 	 */
1497 	bytes_done = reqp->dm_cur->dm_count - bytes_left;
1498 
1499 	if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1500 		/*
1501 		 * Copy the bytes read until now from the bounce buffer
1502 		 * to the 'real' destination. Flush the data-cache
1503 		 * before copying.
1504 		 */
1505 		PCIA();
1506 		memcpy(reqp->xdata_ptr, reqp->bouncerp, bytes_done);
1507 		reqp->bouncerp += bytes_done;
1508 	}
1509 
1510 	reqp->xdata_ptr  = &reqp->xdata_ptr[bytes_done];	/* XXX */
1511 	reqp->xdata_len -= bytes_done;				/* XXX */
1512 	if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1513 		reqp->dm_cur++;
1514 	else reqp->dm_cur->dm_addr += bytes_done;
1515 
1516 	if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1517 		if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
1518 			ncr_tprint(reqp, "parity error in data-phase\n");
1519 			reqp->xs->error = XS_TIMEOUT;
1520 		}
1521 	}
1522 
1523 	/*
1524 	 * DMA mode should always be reset even when we will continue with the
1525 	 * next chain. It is also essential to clear the MON_BUSY because
1526 	 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1527 	 * the bus....
1528 	 */
1529 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1530 
1531 
1532 	if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1533 		 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1534 
1535 		/*
1536 		 * Tell interrupt functions DMA mode has ended.
1537 		 */
1538 		reqp->dr_flag &= ~DRIVER_IN_DMA;
1539 
1540 		/*
1541 		 * Clear mode and icom
1542 		 */
1543 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1544 		SET_5380_REG(NCR5380_ICOM, 0);
1545 
1546 		if (dmstat & SC_BSY_ERR) {
1547 			if (!reqp->xs->error)
1548 				reqp->xs->error = XS_TIMEOUT;
1549 			finish_req(reqp);
1550 			PID("dma_ready1");
1551 			return (1);
1552 		}
1553 
1554 		if (reqp->xs->error != 0) {
1555 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1556 			reqp->msgout = MSG_ABORT;
1557 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1558 		}
1559 		PID("dma_ready2");
1560 		return (1);
1561 	}
1562 	return (0);
1563 }
1564 #endif /* REAL_DMA */
1565 
1566 static int
1567 check_autosense(SC_REQ *reqp, int linked)
1568 {
1569 	int	sps;
1570 
1571 	/*
1572 	 * If this is the driver's Link Check for this target, ignore
1573 	 * the results of the command.  All we care about is whether we
1574 	 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
1575 	 */
1576 	PID("linkcheck");
1577 	if (reqp->dr_flag & DRIVER_LINKCHK) {
1578 		if (linked)
1579 			ncr_will_link |= 1<<reqp->targ_id;
1580 		else ncr_tprint(reqp, "Does not support linked commands\n");
1581 		return (0);
1582 	}
1583 	/*
1584 	 * If we not executing an auto-sense and the status code
1585 	 * is request-sense, we automatically issue a request
1586 	 * sense command.
1587 	 */
1588 	PID("cautos1");
1589 	if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1590 		switch (reqp->status & SCSMASK) {
1591 		    case SCSCHKC:
1592 			memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1593 			reqp->xcmd_len = sizeof(sense_cmd);
1594 			reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense;
1595 			reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense);
1596 			reqp->dr_flag  |= DRIVER_AUTOSEN;
1597 			reqp->dr_flag  &= ~DRIVER_DMAOK;
1598 			if (!linked) {
1599 				sps = splbio();
1600 				reqp->next = issue_q;
1601 				issue_q    = reqp;
1602 				splx(sps);
1603 			}
1604 			else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
1605 
1606 #ifdef DBG_REQ
1607 			memset(reqp->xdata_ptr, 0, reqp->xdata_len);
1608 			if (dbg_target_mask & (1 << reqp->targ_id))
1609 				show_request(reqp, "AUTO-SENSE");
1610 #endif
1611 			PID("cautos2");
1612 			return (-1);
1613 		    case SCSBUSY:
1614 			reqp->xs->error = XS_BUSY;
1615 			return (0);
1616 		}
1617 	}
1618 	else {
1619 		/*
1620 		 * An auto-sense has finished
1621 		 */
1622 		if ((reqp->status & SCSMASK) != SCSGOOD)
1623 			reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1624 		else reqp->xs->error = XS_SENSE;
1625 		reqp->status = SCSCHKC;
1626 	}
1627 	PID("cautos3");
1628 	return (0);
1629 }
1630 
1631 static int
1632 reach_msg_out(struct ncr_softc *sc, u_long len)
1633 {
1634 	u_char	phase;
1635 	u_char	data;
1636 	u_long	n = len;
1637 
1638 	ncr_aprint(sc, "Trying to reach Message-out phase\n");
1639 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1640 		phase = (phase >> 2) & 7;
1641 	else return (-1);
1642 	ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1643 	if (phase == PH_MSGOUT)
1644 		return (0);
1645 
1646 	SET_5380_REG(NCR5380_TCOM, phase);
1647 
1648 	do {
1649 		if (!wait_req_true())
1650 			break;
1651 		if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1652 			break;
1653 		if (PH_IN(phase)) {
1654 			data = GET_5380_REG(NCR5380_DATA);
1655 			SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1656 		}
1657 		else {
1658 			SET_5380_REG(NCR5380_DATA, 0);
1659 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1660 		}
1661 		if (!wait_req_false())
1662 			break;
1663 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1664 	} while (--n);
1665 
1666 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1667 		phase = (phase >> 2) & 7;
1668 		if (phase == PH_MSGOUT) {
1669 			ncr_aprint(sc, "Message-out phase reached after "
1670 					"%ld bytes.\n", len - n);
1671 			return (0);
1672 		}
1673 	}
1674 	return (-1);
1675 }
1676 
1677 void
1678 scsi_reset(void)
1679 {
1680 	SC_REQ	*tmp, *next;
1681 	int	sps;
1682 
1683 	PID("scsi_reset1");
1684 	sps = splbio();
1685 	SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1686 	delay(100);
1687 	SET_5380_REG(NCR5380_ICOM, 0);
1688 	scsi_clr_ipend();
1689 
1690 	/*
1691 	 * None of the jobs in the discon_q will ever be reconnected,
1692 	 * notify this to the higher level code.
1693 	 */
1694 	for (tmp = discon_q; tmp ;) {
1695 		next = tmp->next;
1696 		tmp->next = NULL;
1697 		tmp->xs->error = XS_TIMEOUT;
1698 		busy &= ~(1 << tmp->targ_id);
1699 		finish_req(tmp);
1700 		tmp = next;
1701 	}
1702 	discon_q = NULL;
1703 
1704 	/*
1705 	 * The current job will never finish either.
1706 	 * The problem is that we can't finish the job because an instance
1707 	 * of main is running on it. Our best guess is that the job is currently
1708 	 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1709 	 * the job because it detects BSY-loss.
1710 	 */
1711 	if ((tmp = connected) != NULL) {
1712 		if (tmp->dr_flag & DRIVER_IN_DMA) {
1713 			tmp->xs->error = XS_DRIVER_STUFFUP;
1714 #ifdef REAL_DMA
1715 			dma_ready();
1716 #endif
1717 		}
1718 	}
1719 	splx(sps);
1720 	PID("scsi_reset2");
1721 
1722 	/*
1723 	 * Give the attached devices some time to handle the reset. This
1724 	 * value is arbitrary but should be relatively long.
1725 	 */
1726 	delay(100000);
1727 }
1728 
1729 static void
1730 scsi_reset_verbose(struct ncr_softc *sc, const char *why)
1731 {
1732 	ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
1733 
1734 	scsi_reset();
1735 }
1736 
1737 /*
1738  * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1739  * the appropriate action is carried out (reselection or DMA ready) and
1740  * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1741  * and INTR_SPURIOUS is returned.
1742  */
1743 static int
1744 check_intr(struct ncr_softc *sc)
1745 {
1746 	SC_REQ	*reqp;
1747 
1748 	if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1749 							==(SC_S_SEL|SC_S_IO))
1750 		return (INTR_RESEL);
1751 	else {
1752 		if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1753 			reqp->dr_flag &= ~DRIVER_IN_DMA;
1754 			return (INTR_DMA);
1755 		}
1756 	}
1757 	scsi_clr_ipend();
1758 	printf("-->");
1759 	scsi_show();
1760 	ncr_aprint(sc, "Spurious interrupt.\n");
1761 	return (INTR_SPURIOUS);
1762 }
1763 
1764 #ifdef REAL_DMA
1765 /*
1766  * Check if DMA can be used for this request. This function also builds
1767  * the DMA-chain.
1768  */
1769 static int
1770 scsi_dmaok(SC_REQ *reqp)
1771 {
1772 	u_long			phy_buf;
1773 	u_long			phy_len;
1774 	void			*req_addr;
1775 	u_long			req_len;
1776 	struct dma_chain	*dm;
1777 
1778 	/*
1779 	 * Initialize locals and requests' DMA-chain.
1780 	 */
1781 	req_len        = reqp->xdata_len;
1782 	req_addr       = (void*)reqp->xdata_ptr;
1783 	dm             = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1784 	dm->dm_count   = dm->dm_addr = 0;
1785 	reqp->dr_flag &= ~DRIVER_BOUNCING;
1786 
1787 	/*
1788 	 * Do not accept zero length DMA.
1789 	 */
1790 	if (req_len == 0)
1791 		return (0);
1792 
1793 	/*
1794 	 * LWP: I think that this restriction is not strictly nessecary.
1795 	 */
1796 	if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1797 		return (0);
1798 
1799 	/*
1800 	 * Build the DMA-chain.
1801 	 */
1802 	dm->dm_addr = phy_buf = kvtop(req_addr);
1803 	while (req_len) {
1804 		if (req_len <
1805 		    (phy_len = PAGE_SIZE - m68k_page_offset(req_addr)))
1806 			phy_len = req_len;
1807 
1808 		req_addr     += phy_len;
1809 		req_len      -= phy_len;
1810 		dm->dm_count += phy_len;
1811 
1812 		if (req_len) {
1813 			u_long	tmp = kvtop(req_addr);
1814 
1815 			if ((phy_buf + phy_len) != tmp) {
1816 			    if (wrong_dma_range(reqp, dm)) {
1817 				if (reqp->dr_flag & DRIVER_BOUNCING)
1818 					goto bounceit;
1819 				return (0);
1820 			    }
1821 
1822 			    if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1823 				ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1824 				return (0);
1825 			    }
1826 			    dm->dm_count = 0;
1827 			    dm->dm_addr  = tmp;
1828 			}
1829 			phy_buf = tmp;
1830 		}
1831 	}
1832         if (wrong_dma_range(reqp, dm)) {
1833 		if (reqp->dr_flag & DRIVER_BOUNCING)
1834 			goto bounceit;
1835 		return (0);
1836 	}
1837 	reqp->dm_last = dm;
1838 	return (1);
1839 
1840 bounceit:
1841 	if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1842 		/*
1843 		 * If we can't get a bounce buffer, forget DMA
1844 		 */
1845 		reqp->dr_flag &= ~DRIVER_BOUNCING;
1846 		return(0);
1847 	}
1848 	/*
1849 	 * Initialize a single DMA-range containing the bounced request
1850 	 */
1851 	dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1852 	dm->dm_addr    = kvtop(reqp->bounceb);
1853 	dm->dm_count   = reqp->xdata_len;
1854 	reqp->bouncerp = reqp->bounceb;
1855 
1856 	return (1);
1857 }
1858 #endif /* REAL_DMA */
1859 
1860 static void
1861 run_main(struct ncr_softc *sc)
1862 {
1863 	int	sps = splbio();
1864 
1865 	if (!main_running) {
1866 		/*
1867 		 * If shared resources are required, claim them
1868 		 * before entering 'scsi_main'. If we can't get them
1869 		 * now, assume 'run_main' will be called when the resource
1870 		 * becomes available.
1871 		 */
1872 		if (!claimed_dma()) {
1873 			splx(sps);
1874 			return;
1875 		}
1876 		main_running = 1;
1877 		splx(sps);
1878 		scsi_main(sc);
1879 	}
1880 	else splx(sps);
1881 }
1882 
1883 /*
1884  * Prefix message with full target info.
1885  */
1886 static void
1887 ncr_tprint(SC_REQ *reqp, const char *fmt, ...)
1888 {
1889 	va_list	ap;
1890 
1891 	va_start(ap, fmt);
1892 	scsipi_printaddr(reqp->xs->xs_periph);
1893 	vprintf(fmt, ap);
1894 	va_end(ap);
1895 }
1896 
1897 /*
1898  * Prefix message with adapter info.
1899  */
1900 static void
1901 ncr_aprint(struct ncr_softc *sc, const char *fmt, ...)
1902 {
1903 	va_list	ap;
1904 
1905 	va_start(ap, fmt);
1906 	printf("%s: ", sc->sc_dev.dv_xname);
1907 	vprintf(fmt, ap);
1908 	va_end(ap);
1909 }
1910 /****************************************************************************
1911  *		Start Debugging Functions				    *
1912  ****************************************************************************/
1913 static void
1914 show_data_sense(struct scsipi_xfer *xs)
1915 {
1916 	u_char	*p1, *p2;
1917 	int	i;
1918 
1919 	p1 = (u_char *) xs->cmd;
1920 	p2 = (u_char *)&xs->sense.scsi_sense;
1921 	if(*p2 == 0)
1922 		return;	/* No(n)sense */
1923 	printf("cmd[%d]: ", xs->cmdlen);
1924 	for (i = 0; i < xs->cmdlen; i++)
1925 		printf("%x ", p1[i]);
1926 	printf("\nsense: ");
1927 	for (i = 0; i < sizeof(xs->sense.scsi_sense); i++)
1928 		printf("%x ", p2[i]);
1929 	printf("\n");
1930 }
1931 
1932 static void
1933 show_request(SC_REQ *reqp, const char *qtxt)
1934 {
1935 	printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
1936 			qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1937 			reqp->xcmd.opcode, reqp->status, reqp->message,
1938 			reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
1939 			reqp->link ? "L":"");
1940 	if (reqp->status == SCSCHKC)
1941 		show_data_sense(reqp->xs);
1942 }
1943 
1944 static const char *sig_names[] = {
1945 	"PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
1946 	"ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
1947 };
1948 
1949 static void
1950 show_signals(u_char dmstat, u_char idstat)
1951 {
1952 	u_short	tmp, mask;
1953 	int	j, need_pipe;
1954 
1955 	tmp = idstat | ((dmstat & 3) << 8);
1956 	printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
1957 	for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
1958 		if (tmp & mask)
1959 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1960 	}
1961 	printf("\nDma status (%02x): ", dmstat);
1962 	for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
1963 		if (dmstat & mask)
1964 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1965 	}
1966 	printf("\n");
1967 }
1968 
1969 void
1970 scsi_show(void)
1971 {
1972 	SC_REQ	*tmp;
1973 	int	sps = splhigh();
1974 	u_char	idstat, dmstat;
1975 #ifdef	DBG_PID
1976 	int	i;
1977 #endif
1978 
1979 	printf("scsi_show: scsi_main is%s running\n",
1980 		main_running ? "" : " not");
1981 	for (tmp = issue_q; tmp; tmp = tmp->next)
1982 		show_request(tmp, "ISSUED");
1983 	for (tmp = discon_q; tmp; tmp = tmp->next)
1984 		show_request(tmp, "DISCONNECTED");
1985 	if (connected)
1986 		show_request(connected, "CONNECTED");
1987 	idstat = GET_5380_REG(NCR5380_IDSTAT);
1988 	dmstat = GET_5380_REG(NCR5380_DMSTAT);
1989 	show_signals(dmstat, idstat);
1990 	if (connected)
1991 		printf("phase = %d, ", connected->phase);
1992 	printf("busy:%x, spl:%04x\n", busy, sps);
1993 #ifdef	DBG_PID
1994 	for (i=0; i<DBG_PID; i++)
1995 		printf("\t%d\t%s\n", i, last_hit[i]);
1996 #endif
1997 
1998 	splx(sps);
1999 }
2000