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