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