xref: /netbsd-src/sys/arch/x68k/dev/mha.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: mha.c,v 1.51 2008/12/18 05:56:42 isaki Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996-1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum, Masaru Oki, Takumi Nakamura, Masanobu Saitoh and
9  * Minoura Makoto.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 /*-
34  * Copyright (c) 1994 Jarle Greipsland
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. The name of the author may not be used to endorse or promote products
46  *    derived from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
52  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
57  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58  * POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: mha.c,v 1.51 2008/12/18 05:56:42 isaki Exp $");
63 
64 #include "opt_ddb.h"
65 
66 /* Synchronous data transfers? */
67 #define SPC_USE_SYNCHRONOUS	0
68 #define SPC_SYNC_REQ_ACK_OFS 	8
69 
70 /* Default DMA mode? */
71 #define MHA_DMA_LIMIT_XFER	1
72 #define MHA_DMA_BURST_XFER	1
73 #define MHA_DMA_SHORT_BUS_CYCLE	1
74 
75 #define MHA_DMA_DATAIN	(0 | (MHA_DMA_LIMIT_XFER << 1)		\
76 			   | (MHA_DMA_BURST_XFER << 2)		\
77 			   | (MHA_DMA_SHORT_BUS_CYCLE << 3))
78 #define MHA_DMA_DATAOUT	(1 | (MHA_DMA_LIMIT_XFER << 1)		\
79 			   | (MHA_DMA_BURST_XFER << 2)		\
80 			   | (MHA_DMA_SHORT_BUS_CYCLE << 3))
81 
82 /* Include debug functions?  At the end of this file there are a bunch of
83  * functions that will print out various information regarding queued SCSI
84  * commands, driver state and chip contents.  You can call them from the
85  * kernel debugger.  If you set SPC_DEBUG to 0 they are not included (the
86  * kernel uses less memory) but you lose the debugging facilities.
87  */
88 #define SPC_DEBUG		0
89 
90 /* End of customizable parameters */
91 
92 /*
93  * MB86601A SCSI Protocol Controller (SPC) routines for MANKAI Mach-2
94  */
95 
96 #include <sys/types.h>
97 #include <sys/param.h>
98 #include <sys/systm.h>
99 #include <sys/kernel.h>
100 #include <sys/errno.h>
101 #include <sys/ioctl.h>
102 #include <sys/device.h>
103 #include <sys/buf.h>
104 #include <sys/proc.h>
105 #include <sys/user.h>
106 #include <sys/queue.h>
107 
108 #include <machine/bus.h>
109 
110 #include <dev/scsipi/scsi_spc.h>
111 #include <dev/scsipi/scsi_all.h>
112 #include <dev/scsipi/scsipi_all.h>
113 #include <dev/scsipi/scsi_message.h>
114 #include <dev/scsipi/scsiconf.h>
115 
116 #include <x68k/x68k/iodevice.h>
117 #include <x68k/dev/mb86601reg.h>
118 #include <x68k/dev/mhavar.h>
119 #include <x68k/dev/intiovar.h>
120 #include <x68k/dev/scsiromvar.h>
121 
122 #if 0
123 #define WAIT {if (sc->sc_pc[2]) {printf("[W_%d", __LINE__); while (sc->sc_pc[2] & 0x40);printf("]");}}
124 #else
125 #define WAIT {while (sc->sc_pc[2] & 0x40);}
126 #endif
127 
128 #define SSR	(sc->sc_pc[2])
129 #define	SS_IREQUEST	0x80
130 #define	SS_BUSY		0x40
131 #define	SS_DREG_FULL	0x02
132 
133 #define	NSR	(sc->sc_pc[3])
134 
135 #define	SIR	(sc->sc_pc[4])
136 
137 #define	CMR	(sc->sc_pc[5])
138 #define	CMD_SEL_AND_CMD	0x00
139 #define	CMD_SELECT	0x09
140 #define	CMD_SET_ATN	0x0a
141 #define	CMD_RESET_ATN	0x0b
142 #define	CMD_RESET_ACK	0x0d
143 #define	CMD_SEND_FROM_MPU	0x10
144 #define	CMD_SEND_FROM_DMA	0x11
145 #define	CMD_RECEIVE_TO_MPU	0x12
146 #define	CMD_RECEIVE_TO_DMA	0x13
147 #define	CMD_RECEIVE_MSG	0x1a
148 #define	CMD_RECEIVE_STS	0x1c
149 #define	CMD_SOFT_RESET	0x40
150 #define	CMD_SCSI_RESET	0x42
151 #define	CMD_SET_UP_REG	0x43
152 
153 #define	SCR	(sc->sc_pc[11])
154 
155 #define	TMR	(sc->sc_pc[12])
156 #define	TM_SYNC		0x80
157 #define	TM_ASYNC	0x00
158 
159 #define	WAR	(sc->sc_pc[15])
160 #define	WA_MCSBUFWIN	0x00
161 #define	WA_UPMWIN	0x80
162 #define	WA_INITWIN	0xc0
163 
164 #define	MBR	(sc->sc_pc[15])
165 
166 #define ISCSR	(sc->sc_ps[2])
167 
168 #define	CCR	(sc->sc_pcx[0])
169 #define	OIR	(sc->sc_pcx[1])
170 #define	AMR	(sc->sc_pcx[2])
171 #define	SMR	(sc->sc_pcx[3])
172 #define	SRR	(sc->sc_pcx[4])
173 #define	STR	(sc->sc_pcx[5])
174 #define	RTR	(sc->sc_pcx[6])
175 #define	ATR	(sc->sc_pcx[7])
176 #define	PER	(sc->sc_pcx[8])
177 #define	IER	(sc->sc_pcx[9])
178 #define	IE_ALL	0xBF
179 
180 #define	GLR	(sc->sc_pcx[10])
181 #define	DMR	(sc->sc_pcx[11])
182 #define	IMR	(sc->sc_pcx[12])
183 
184 #ifndef DDB
185 #define	Debugger() panic("should call debugger here (mha.c)")
186 #endif /* ! DDB */
187 
188 
189 #if SPC_DEBUG
190 #define SPC_SHOWACBS	0x01
191 #define SPC_SHOWINTS	0x02
192 #define SPC_SHOWCMDS	0x04
193 #define SPC_SHOWMISC	0x08
194 #define SPC_SHOWTRAC	0x10
195 #define SPC_SHOWSTART	0x20
196 #define SPC_SHOWPHASE	0x40
197 #define SPC_SHOWDMA	0x80
198 #define SPC_SHOWCCMDS	0x100
199 #define SPC_SHOWMSGS	0x200
200 #define SPC_DOBREAK	0x400
201 
202 int mha_debug =
203 #if 0
204 0x7FF;
205 #else
206 SPC_SHOWSTART|SPC_SHOWTRAC;
207 #endif
208 
209 
210 #define SPC_ACBS(str)  do {if (mha_debug & SPC_SHOWACBS) printf str;} while (0)
211 #define SPC_MISC(str)  do {if (mha_debug & SPC_SHOWMISC) printf str;} while (0)
212 #define SPC_INTS(str)  do {if (mha_debug & SPC_SHOWINTS) printf str;} while (0)
213 #define SPC_TRACE(str) do {if (mha_debug & SPC_SHOWTRAC) printf str;} while (0)
214 #define SPC_CMDS(str)  do {if (mha_debug & SPC_SHOWCMDS) printf str;} while (0)
215 #define SPC_START(str) do {if (mha_debug & SPC_SHOWSTART) printf str;}while (0)
216 #define SPC_PHASE(str) do {if (mha_debug & SPC_SHOWPHASE) printf str;}while (0)
217 #define SPC_DMA(str)   do {if (mha_debug & SPC_SHOWDMA) printf str;}while (0)
218 #define SPC_MSGS(str)  do {if (mha_debug & SPC_SHOWMSGS) printf str;}while (0)
219 #define	SPC_BREAK()    do {if ((mha_debug & SPC_DOBREAK) != 0) Debugger();} while (0)
220 #define	SPC_ASSERT(x)  do {if (x) {} else {printf("%s at line %d: assertion failed\n", device_xname(sc->sc_dev), __LINE__); Debugger();}} while (0)
221 #else
222 #define SPC_ACBS(str)
223 #define SPC_MISC(str)
224 #define SPC_INTS(str)
225 #define SPC_TRACE(str)
226 #define SPC_CMDS(str)
227 #define SPC_START(str)
228 #define SPC_PHASE(str)
229 #define SPC_DMA(str)
230 #define SPC_MSGS(str)
231 #define	SPC_BREAK()
232 #define	SPC_ASSERT(x)
233 #endif
234 
235 int	mhamatch(device_t, cfdata_t, void *);
236 void	mhaattach(device_t, device_t, void *);
237 void	mhaselect(struct mha_softc *, u_char, u_char, u_char *, u_char);
238 void	mha_scsi_reset(struct mha_softc *);
239 void	mha_reset(struct mha_softc *);
240 void	mha_free_acb(struct mha_softc *, struct acb *, int);
241 void	mha_sense(struct mha_softc *, struct acb *);
242 void	mha_msgin(struct mha_softc *);
243 void	mha_msgout(struct mha_softc *);
244 int	mha_dataout_pio(struct mha_softc *, u_char *, int);
245 int	mha_datain_pio(struct mha_softc *, u_char *, int);
246 int	mha_dataout(struct mha_softc *, u_char *, int);
247 int	mha_datain(struct mha_softc *, u_char *, int);
248 void	mha_abort(struct mha_softc *, struct acb *);
249 void 	mha_init(struct mha_softc *);
250 void	mha_scsi_request(struct scsipi_channel *, scsipi_adapter_req_t, void *);
251 void	mha_poll(struct mha_softc *, struct acb *);
252 void	mha_sched(struct mha_softc *);
253 void	mha_done(struct mha_softc *, struct acb *);
254 int	mhaintr(void *);
255 void	mha_timeout(void *);
256 void	mha_minphys(struct buf *);
257 void	mha_dequeue(struct mha_softc *, struct acb *);
258 inline void	mha_setsync(struct mha_softc *, struct spc_tinfo *);
259 #if SPC_DEBUG
260 void	mha_print_acb(struct acb *);
261 void	mha_show_scsi_cmd(struct acb *);
262 void	mha_print_active_acb(void);
263 void	mha_dump_driver(struct mha_softc *);
264 #endif
265 
266 static int mha_dataio_dma(int, int, struct mha_softc *, u_char *, int);
267 
268 CFATTACH_DECL_NEW(mha, sizeof(struct mha_softc),
269     mhamatch, mhaattach, NULL, NULL);
270 
271 extern struct cfdriver mha_cd;
272 
273 /*
274  * returns non-zero value if a controller is found.
275  */
276 int
277 mhamatch(device_t parent, cfdata_t cf, void *aux)
278 {
279 	struct intio_attach_args *ia = aux;
280 	bus_space_tag_t iot = ia->ia_bst;
281 	bus_space_handle_t ioh;
282 
283 	ia->ia_size=0x20;
284 	if (ia->ia_addr != 0xea0000)
285 		return 0;
286 
287 	if (intio_map_allocate_region(device_parent(parent), ia,
288 				      INTIO_MAP_TESTONLY) < 0) /* FAKE */
289 		return 0;
290 
291 	if (bus_space_map(iot, ia->ia_addr, 0x20, BUS_SPACE_MAP_SHIFTED,
292 			  &ioh) < 0)
293 		return 0;
294 	if (!badaddr((void *)IIOV(ia->ia_addr + 0)))
295 		return 0;
296 	bus_space_unmap(iot, ioh, 0x20);
297 
298 	return 1;
299 }
300 
301 /*
302  */
303 
304 struct mha_softc *tmpsc;
305 
306 void
307 mhaattach(device_t parent, device_t self, void *aux)
308 {
309 	struct mha_softc *sc = device_private(self);
310 	struct intio_attach_args *ia = aux;
311 
312 	tmpsc = sc;	/* XXX */
313 	sc->sc_dev = self;
314 
315 	aprint_normal(": Mankai Mach-2 Fast SCSI Host Adaptor\n");
316 
317 	SPC_TRACE(("mhaattach  "));
318 	sc->sc_state = SPC_INIT;
319 	sc->sc_iobase = (void *)IIOV(ia->ia_addr + 0x80); /* XXX */
320 	intio_map_allocate_region(device_parent(parent), ia, INTIO_MAP_ALLOCATE);
321 				/* XXX: FAKE  */
322 	sc->sc_dmat = ia->ia_dmat;
323 
324 	sc->sc_pc = (volatile u_char *)sc->sc_iobase;
325 	sc->sc_ps = (volatile u_short *)sc->sc_iobase;
326 	sc->sc_pcx = &sc->sc_pc[0x10];
327 
328 	sc->sc_id = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
329 
330 	intio_intr_establish(ia->ia_intr, "mha", mhaintr, sc);
331 
332 	mha_init(sc);	/* Init chip and driver */
333 
334 	mha_scsi_reset(sc);	/* XXX: some devices need this. */
335 
336 	sc->sc_phase  = BUSFREE_PHASE;
337 
338 	/*
339 	 * Fill in the adapter.
340 	 */
341 	sc->sc_adapter.adapt_dev = sc->sc_dev;
342 	sc->sc_adapter.adapt_nchannels = 1;
343 	sc->sc_adapter.adapt_openings = 7;
344 	sc->sc_adapter.adapt_max_periph = 1;
345 	sc->sc_adapter.adapt_ioctl = NULL;
346 	sc->sc_adapter.adapt_minphys = mha_minphys;
347 	sc->sc_adapter.adapt_request = mha_scsi_request;
348 
349 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
350 	sc->sc_channel.chan_bustype = &scsi_bustype;
351 	sc->sc_channel.chan_channel = 0;
352 	sc->sc_channel.chan_ntargets = 8;
353 	sc->sc_channel.chan_nluns = 8;
354 	sc->sc_channel.chan_id = sc->sc_id;
355 
356 	sc->sc_spcinitialized = 0;
357 	WAR = WA_INITWIN;
358 #if 1
359 	CCR = 0x14;
360 	OIR = sc->sc_id;
361 	AMR = 0x00;
362 	SMR = 0x00;
363 	SRR = 0x00;
364 	STR = 0x20;
365 	RTR = 0x40;
366 	ATR = 0x01;
367 	PER = 0xc9;
368 #endif
369 	IER = IE_ALL;	/* $B$9$Y$F$N3d$j9~$_$r5v2D(B */
370 #if 1
371 	GLR = 0x00;
372 	DMR = 0x30;
373 	IMR = 0x00;
374 #endif
375 	WAR = WA_MCSBUFWIN;
376 
377 	/* drop off */
378 	while (SSR & SS_IREQUEST) {
379 		(void) ISCSR;
380 	}
381 
382 	CMR = CMD_SET_UP_REG;	/* setup reg cmd. */
383 
384 	SPC_TRACE(("waiting for intr..."));
385 	while (!(SSR & SS_IREQUEST))
386 		delay(10);
387 	mhaintr(sc);
388 
389 	tmpsc = NULL;
390 
391 	config_found(self, &sc->sc_channel, scsiprint);
392 }
393 
394 #if 0
395 void
396 mha_reset(struct mha_softc *sc)
397 {
398 	u_short	dummy;
399 printf("reset...");
400 	CMR = CMD_SOFT_RESET;
401 	__asm volatile ("nop");	/* XXX wait (4clk in 20 MHz) ??? */
402 	dummy = sc->sc_ps[-1];
403 	dummy = sc->sc_ps[-1];
404 	dummy = sc->sc_ps[-1];
405 	dummy = sc->sc_ps[-1];
406 	__asm volatile ("nop");
407 	CMR = CMD_SOFT_RESET;
408 	sc->sc_spcinitialized = 0;
409 	CMR = CMD_SET_UP_REG;	/* setup reg cmd. */
410 	while(!sc->sc_spcinitialized);
411 
412 	sc->sc_id = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
413 printf("done.\n");
414 }
415 #endif
416 
417 /*
418  * Pull the SCSI RST line for 500us.
419  */
420 void
421 mha_scsi_reset(struct mha_softc *sc)
422 {
423 
424 	CMR = CMD_SCSI_RESET;	/* SCSI RESET */
425 	while (!(SSR&SS_IREQUEST))
426 		delay(10);
427 }
428 
429 /*
430  * Initialize mha SCSI driver.
431  */
432 void
433 mha_init(struct mha_softc *sc)
434 {
435 	struct acb *acb;
436 	int r;
437 
438 	if (sc->sc_state == SPC_INIT) {
439 		/* First time through; initialize. */
440 		TAILQ_INIT(&sc->ready_list);
441 		TAILQ_INIT(&sc->nexus_list);
442 		TAILQ_INIT(&sc->free_list);
443 		sc->sc_nexus = NULL;
444 		acb = sc->sc_acb;
445 		memset(acb, 0, sizeof(sc->sc_acb));
446 		for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
447 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
448 			acb++;
449 		}
450 		memset(&sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
451 
452 		r = bus_dmamem_alloc(sc->sc_dmat, MAXBSIZE, 0, 0,
453 				     sc->sc_dmaseg, 1, &sc->sc_ndmasegs,
454 				     BUS_DMA_NOWAIT);
455 		if (r)
456 			panic("mha_init: cannot allocate DMA memory");
457 		if (sc->sc_ndmasegs != 1)
458 			panic("mha_init: number of segment > 1??");
459 		r = bus_dmamem_map(sc->sc_dmat, sc->sc_dmaseg, sc->sc_ndmasegs,
460 				   MAXBSIZE, &sc->sc_dmabuf, BUS_DMA_NOWAIT);
461 		if (r)
462 			panic("mha_init: cannot map DMA memory");
463 		r = bus_dmamap_create(sc->sc_dmat, MAXBSIZE, 1,
464 				      MAXBSIZE, 0, BUS_DMA_NOWAIT,
465 				      &sc->sc_dmamap);
466 		if (r)
467 			panic("mha_init: cannot create dmamap structure");
468 		r = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
469 				    sc->sc_dmabuf, MAXBSIZE, NULL,
470 				    BUS_DMA_NOWAIT);
471 		if (r)
472 			panic("mha_init: cannot load DMA buffer into dmamap");
473 		sc->sc_p = 0;
474 	} else {
475 		/* Cancel any active commands. */
476 		sc->sc_flags |= SPC_ABORTING;
477 		sc->sc_state = SPC_IDLE;
478 		if ((acb = sc->sc_nexus) != NULL) {
479 			acb->xs->error = XS_DRIVER_STUFFUP;
480 			mha_done(sc, acb);
481 		}
482 		while ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
483 			acb->xs->error = XS_DRIVER_STUFFUP;
484 			mha_done(sc, acb);
485 		}
486 	}
487 
488 	sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
489 	for (r = 0; r < 8; r++) {
490 		struct spc_tinfo *ti = &sc->sc_tinfo[r];
491 
492 		ti->flags = 0;
493 #if SPC_USE_SYNCHRONOUS
494 		ti->flags |= T_SYNCMODE;
495 		ti->period = sc->sc_minsync;
496 		ti->offset = SPC_SYNC_REQ_ACK_OFS;
497 #else
498 		ti->period = ti->offset = 0;
499 #endif
500 		ti->width = 0;
501 	}
502 
503 	sc->sc_state = SPC_IDLE;
504 }
505 
506 void
507 mha_free_acb(struct mha_softc *sc, struct acb *acb, int flags)
508 {
509 	int s;
510 
511 	s = splbio();
512 
513 	acb->flags = 0;
514 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
515 
516 	/*
517 	 * If there were none, wake anybody waiting for one to come free,
518 	 * starting with queued entries.
519 	 */
520 	if (TAILQ_NEXT(acb, chain) == NULL)
521 		wakeup(&sc->free_list);
522 
523 	splx(s);
524 }
525 
526 /*
527  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
528  */
529 
530 /*
531  * Expected sequence:
532  * 1) Command inserted into ready list
533  * 2) Command selected for execution
534  * 3) Command won arbitration and has selected target device
535  * 4) Send message out (identify message, eventually also sync.negotiations)
536  * 5) Send command
537  * 5a) Receive disconnect message, disconnect.
538  * 5b) Reselected by target
539  * 5c) Receive identify message from target.
540  * 6) Send or receive data
541  * 7) Receive status
542  * 8) Receive message (command complete etc.)
543  * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
544  *    Repeat 2-8 (no disconnects please...)
545  */
546 
547 /*
548  * Start a selection.  This is used by mha_sched() to select an idle target,
549  * and by mha_done() to immediately reselect a target to get sense information.
550  */
551 void
552 mhaselect(struct mha_softc *sc, u_char target, u_char lun, u_char *cmd,
553     u_char clen)
554 {
555 	int i;
556 	int s;
557 
558 	s = splbio();	/* XXX */
559 
560 	SPC_TRACE(("[mhaselect(t%d,l%d,cmd:%x)] ", target, lun, *(u_char *)cmd));
561 
562 	/* CDB $B$r(B SPC $B$N(B MCS REG $B$K%;%C%H$9$k(B */
563 	/* Now the command into the FIFO */
564 	WAIT;
565 #if 1
566 	SPC_MISC(("[cmd:"));
567 	for (i = 0; i < clen; i++) {
568 		unsigned c = cmd[i];
569 		if (i == 1)
570 			c |= lun << 5;
571 		SPC_MISC((" %02x", c));
572 		sc->sc_pcx[i] = c;
573 	}
574 	SPC_MISC(("], target=%d\n", target));
575 #else
576 	memcpy(sc->sc_pcx, cmd, clen);
577 #endif
578 	if (NSR & 0x80)
579 		panic("scsistart: already selected...");
580 	sc->sc_phase  = COMMAND_PHASE;
581 
582 	/* new state ASP_SELECTING */
583 	sc->sc_state = SPC_SELECTING;
584 
585 	SIR = target;
586 #if 0
587 	CMR = CMD_SELECT;
588 #else
589 	CMR = CMD_SEL_AND_CMD;	/* select & cmd */
590 #endif
591 	splx(s);
592 }
593 
594 #if 0
595 int
596 mha_reselect(struct mha_softc *sc, u_char message)
597 {
598 	u_char selid, target, lun;
599 	struct acb *acb;
600 	struct scsipi_periph *periph;
601 	struct spc_tinfo *ti;
602 
603 	/*
604 	 * The SCSI chip made a snapshot of the data bus while the reselection
605 	 * was being negotiated.  This enables us to determine which target did
606 	 * the reselect.
607 	 */
608 	selid = sc->sc_selid & ~(1 << sc->sc_id);
609 	if (selid & (selid - 1)) {
610 		printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
611 		    device_xname(sc->sc_dev), selid);
612 		SPC_BREAK();
613 		goto reset;
614 	}
615 
616 	/*
617 	 * Search wait queue for disconnected cmd
618 	 * The list should be short, so I haven't bothered with
619 	 * any more sophisticated structures than a simple
620 	 * singly linked list.
621 	 */
622 	target = ffs(selid) - 1;
623 	lun = message & 0x07;
624 	TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
625 		periph = acb->xs->xs_periph;
626 		if (periph->periph_target == target &&
627 		    periph->periph_lun == lun)
628 			break;
629 	}
630 	if (acb == NULL) {
631 		printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
632 		    device_xname(sc->sc_dev), target, lun);
633 		SPC_BREAK();
634 		goto abort;
635 	}
636 
637 	/* Make this nexus active again. */
638 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
639 	sc->sc_state = SPC_HASNEXUS;
640 	sc->sc_nexus = acb;
641 	ti = &sc->sc_tinfo[target];
642 	ti->lubusy |= (1 << lun);
643 	mha_setsync(sc, ti);
644 
645 	if (acb->flags & ACB_RESET)
646 		mha_sched_msgout(sc, SEND_DEV_RESET);
647 	else if (acb->flags & ACB_ABORTED)
648 		mha_sched_msgout(sc, SEND_ABORT);
649 
650 	/* Do an implicit RESTORE POINTERS. */
651 	sc->sc_dp = acb->daddr;
652 	sc->sc_dleft = acb->dleft;
653 	sc->sc_cp = (u_char *)&acb->cmd;
654 	sc->sc_cleft = acb->clen;
655 
656 	return (0);
657 
658 reset:
659 	mha_sched_msgout(sc, SEND_DEV_RESET);
660 	return (1);
661 
662 abort:
663 	mha_sched_msgout(sc, SEND_ABORT);
664 	return (1);
665 }
666 #endif
667 /*
668  * Start a SCSI-command
669  * This function is called by the higher level SCSI-driver to queue/run
670  * SCSI-commands.
671  */
672 void
673 mha_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
674     void *arg)
675 {
676 	struct scsipi_xfer *xs;
677 	struct scsipi_periph *periph;
678 	struct mha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
679 	struct acb *acb;
680 	int s, flags;
681 
682 	switch (req) {
683 	case ADAPTER_REQ_RUN_XFER:
684 		xs = arg;
685 		periph = xs->xs_periph;
686 
687 		SPC_TRACE(("[mha_scsi_cmd] "));
688 		SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
689 		    periph->periph_target));
690 
691 		flags = xs->xs_control;
692 
693 		/* Get a mha command block */
694 		s = splbio();
695 		acb = TAILQ_FIRST(&sc->free_list);
696 		if (acb) {
697 			TAILQ_REMOVE(&sc->free_list, acb, chain);
698 			ACB_SETQ(acb, ACB_QNONE);
699 		}
700 
701 		if (acb == NULL) {
702 			xs->error = XS_RESOURCE_SHORTAGE;
703 			scsipi_done(xs);
704 			splx(s);
705 			return;
706 		}
707 		splx(s);
708 
709 		/* Initialize acb */
710 		acb->xs = xs;
711 		memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
712 		acb->clen = xs->cmdlen;
713 		acb->daddr = xs->data;
714 		acb->dleft = xs->datalen;
715 		acb->stat = 0;
716 
717 		s = splbio();
718 		ACB_SETQ(acb, ACB_QREADY);
719 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
720 #if 1
721 		callout_reset(&acb->xs->xs_callout,
722 		    mstohz(xs->timeout), mha_timeout, acb);
723 #endif
724 
725 		/*
726 		 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
727 		 */
728 		if (sc->sc_state == SPC_IDLE)
729 			mha_sched(sc);
730 
731 		splx(s);
732 
733 		if (flags & XS_CTL_POLL) {
734 			/* Not allowed to use interrupts, use polling instead */
735 			mha_poll(sc, acb);
736 		}
737 
738 		SPC_MISC(("SUCCESSFULLY_QUEUED"));
739 		return;
740 
741 	case ADAPTER_REQ_GROW_RESOURCES:
742 		/* XXX Not supported. */
743 		return;
744 
745 	case ADAPTER_REQ_SET_XFER_MODE:
746 		/* XXX Not supported. */
747 		return;
748 	}
749 }
750 
751 /*
752  * Adjust transfer size in buffer structure
753  */
754 void
755 mha_minphys(struct buf *bp)
756 {
757 
758 	SPC_TRACE(("mha_minphys  "));
759 	minphys(bp);
760 }
761 
762 /*
763  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
764  */
765 void
766 mha_poll(struct mha_softc *sc, struct acb *acb)
767 {
768 	struct scsipi_xfer *xs = acb->xs;
769 	int count = xs->timeout * 100;
770 	int s;
771 
772 	s = splbio();
773 
774 	SPC_TRACE(("[mha_poll] "));
775 
776 	while (count) {
777 		/*
778 		 * If we had interrupts enabled, would we
779 		 * have got an interrupt?
780 		 */
781 		if (SSR & SS_IREQUEST)
782 			mhaintr(sc);
783 		if ((xs->xs_status & XS_STS_DONE) != 0)
784 			break;
785 		DELAY(10);
786 #if 1
787 		if (sc->sc_state == SPC_IDLE) {
788 			SPC_TRACE(("[mha_poll: rescheduling] "));
789 			mha_sched(sc);
790 		}
791 #endif
792 		count--;
793 	}
794 
795 	if (count == 0) {
796 		SPC_MISC(("mha_poll: timeout"));
797 		mha_timeout((void *)acb);
798 	}
799 	splx(s);
800 	scsipi_done(xs);
801 }
802 
803 /*
804  * LOW LEVEL SCSI UTILITIES
805  */
806 
807 /*
808  * Set synchronous transfer offset and period.
809  */
810 inline void
811 mha_setsync(struct mha_softc *sc, struct spc_tinfo *ti)
812 {
813 }
814 
815 /*
816  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
817  * handler so that we may call it from mha_scsi_cmd and mha_done.  This may
818  * save us an unecessary interrupt just to get things going.  Should only be
819  * called when state == SPC_IDLE and at bio pl.
820  */
821 void
822 mha_sched(struct mha_softc *sc)
823 {
824 	struct scsipi_periph *periph;
825 	struct acb *acb;
826 	int t;
827 
828 	SPC_TRACE(("[mha_sched] "));
829 	if (sc->sc_state != SPC_IDLE)
830 		panic("mha_sched: not IDLE (state=%d)", sc->sc_state);
831 
832 	if (sc->sc_flags & SPC_ABORTING)
833 		return;
834 
835 	/*
836 	 * Find first acb in ready queue that is for a target/lunit
837 	 * combinations that is not busy.
838 	 */
839 	TAILQ_FOREACH(acb, &sc->ready_list, chain) {
840 		struct spc_tinfo *ti;
841 		periph = acb->xs->xs_periph;
842 		t = periph->periph_target;
843 		ti = &sc->sc_tinfo[t];
844 		if (!(ti->lubusy & (1 << periph->periph_lun))) {
845 			if ((acb->flags & ACB_QBITS) != ACB_QREADY)
846 				panic("mha: busy entry on ready list");
847 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
848 			ACB_SETQ(acb, ACB_QNONE);
849 			sc->sc_nexus = acb;
850 			sc->sc_flags = 0;
851 			sc->sc_prevphase = INVALID_PHASE;
852 			sc->sc_dp = acb->daddr;
853 			sc->sc_dleft = acb->dleft;
854 			ti->lubusy |= (1<<periph->periph_lun);
855 			mhaselect(sc, t, periph->periph_lun,
856 				     (u_char *)&acb->cmd, acb->clen);
857 			break;
858 		} else {
859 			SPC_MISC(("%d:%d busy\n",
860 			    periph->periph_target,
861 			    periph->periph_lun));
862 		}
863 	}
864 }
865 
866 /*
867  * POST PROCESSING OF SCSI_CMD (usually current)
868  */
869 void
870 mha_done(struct mha_softc *sc, struct acb *acb)
871 {
872 	struct scsipi_xfer *xs = acb->xs;
873 	struct scsipi_periph *periph = xs->xs_periph;
874 	struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
875 
876 	SPC_TRACE(("[mha_done(error:%x)] ", xs->error));
877 
878 #if 1
879 	callout_stop(&acb->xs->xs_callout);
880 #endif
881 
882 	/*
883 	 * Now, if we've come here with no error code, i.e. we've kept the
884 	 * initial XS_NOERROR, and the status code signals that we should
885 	 * check sense, we'll need to set up a request sense cmd block and
886 	 * push the command back into the ready queue *before* any other
887 	 * commands for this target/lunit, else we lose the sense info.
888 	 * We don't support chk sense conditions for the request sense cmd.
889 	 */
890 	if (xs->error == XS_NOERROR) {
891 		if ((acb->flags & ACB_ABORTED) != 0) {
892 			xs->error = XS_TIMEOUT;
893 		} else if (acb->flags & ACB_CHKSENSE) {
894 			xs->error = XS_SENSE;
895 		} else {
896 			xs->status = acb->stat & ST_MASK;
897 			switch (xs->status) {
898 			case SCSI_CHECK:
899 				xs->resid = acb->dleft;
900 				/* FALLTHROUGH */
901 			case SCSI_BUSY:
902 				xs->error = XS_BUSY;
903 				break;
904 			case SCSI_OK:
905 				xs->resid = acb->dleft;
906 				break;
907 			default:
908 				xs->error = XS_DRIVER_STUFFUP;
909 #if SPC_DEBUG
910 				printf("%s: mha_done: bad stat 0x%x\n",
911 					device_xname(sc->sc_dev), acb->stat);
912 #endif
913 				break;
914 			}
915 		}
916 	}
917 
918 #if SPC_DEBUG
919 	if ((mha_debug & SPC_SHOWMISC) != 0) {
920 		if (xs->resid != 0)
921 			printf("resid=%d ", xs->resid);
922 		if (xs->error == XS_SENSE)
923 			printf("sense=0x%02x\n", xs->sense.scsi_sense.response_code);
924 		else
925 			printf("error=%d\n", xs->error);
926 	}
927 #endif
928 
929 	/*
930 	 * Remove the ACB from whatever queue it's on.
931 	 */
932 	switch (acb->flags & ACB_QBITS) {
933 	case ACB_QNONE:
934 		if (acb != sc->sc_nexus) {
935 			panic("%s: floating acb", device_xname(sc->sc_dev));
936 		}
937 		sc->sc_nexus = NULL;
938 		sc->sc_state = SPC_IDLE;
939 		ti->lubusy &= ~(1<<periph->periph_lun);
940 		mha_sched(sc);
941 		break;
942 	case ACB_QREADY:
943 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
944 		break;
945 	case ACB_QNEXUS:
946 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
947 		ti->lubusy &= ~(1<<periph->periph_lun);
948 		break;
949 	case ACB_QFREE:
950 		panic("%s: dequeue: busy acb on free list",
951 			device_xname(sc->sc_dev));
952 		break;
953 	default:
954 		panic("%s: dequeue: unknown queue %d",
955 			device_xname(sc->sc_dev), acb->flags & ACB_QBITS);
956 	}
957 
958 	/* Put it on the free list, and clear flags. */
959 #if 0
960 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
961 	acb->flags = ACB_QFREE;
962 #else
963 	mha_free_acb(sc, acb, xs->xs_control);
964 #endif
965 
966 	ti->cmds++;
967 	scsipi_done(xs);
968 }
969 
970 void
971 mha_dequeue(struct mha_softc *sc, struct acb *acb)
972 {
973 
974 	if (acb->flags & ACB_QNEXUS) {
975 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
976 	} else {
977 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
978 	}
979 }
980 
981 /*
982  * INTERRUPT/PROTOCOL ENGINE
983  */
984 
985 /*
986  * Schedule an outgoing message by prioritizing it, and asserting
987  * attention on the bus. We can only do this when we are the initiator
988  * else there will be an illegal command interrupt.
989  */
990 #define mha_sched_msgout(m) \
991 	do {				\
992 		SPC_MISC(("mha_sched_msgout %d ", m)); \
993 		CMR = CMD_SET_ATN;	\
994 		sc->sc_msgpriq |= (m);	\
995 	} while (0)
996 
997 /*
998  * Precondition:
999  * The SCSI bus is already in the MSGI phase and there is a message byte
1000  * on the bus, along with an asserted REQ signal.
1001  */
1002 void
1003 mha_msgin(struct mha_softc *sc)
1004 {
1005 	int v;
1006 
1007 	SPC_TRACE(("[mha_msgin(curmsglen:%d)] ", sc->sc_imlen));
1008 
1009 	/*
1010 	 * Prepare for a new message.  A message should (according
1011 	 * to the SCSI standard) be transmitted in one single
1012 	 * MESSAGE_IN_PHASE. If we have been in some other phase,
1013 	 * then this is a new message.
1014 	 */
1015 	if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
1016 		sc->sc_flags &= ~SPC_DROP_MSGI;
1017 		sc->sc_imlen = 0;
1018 	}
1019 
1020 	WAIT;
1021 
1022 	v = MBR;	/* modified byte */
1023 	v = sc->sc_pcx[0];
1024 
1025 	sc->sc_imess[sc->sc_imlen] = v;
1026 
1027 	/*
1028 	 * If we're going to reject the message, don't bother storing
1029 	 * the incoming bytes.  But still, we need to ACK them.
1030 	 */
1031 
1032 	if ((sc->sc_flags & SPC_DROP_MSGI)) {
1033 		CMR = CMD_SET_ATN;
1034 /*		ESPCMD(sc, ESPCMD_MSGOK);*/
1035 		printf("<dropping msg byte %x>",
1036 			sc->sc_imess[sc->sc_imlen]);
1037 		return;
1038 	}
1039 
1040 	if (sc->sc_imlen >= SPC_MAX_MSG_LEN) {
1041 		mha_sched_msgout(SEND_REJECT);
1042 		sc->sc_flags |= SPC_DROP_MSGI;
1043 	} else {
1044 		sc->sc_imlen++;
1045 		/*
1046 		 * This testing is suboptimal, but most
1047 		 * messages will be of the one byte variety, so
1048 		 * it should not effect performance
1049 		 * significantly.
1050 		 */
1051 		if (sc->sc_imlen == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
1052 			goto gotit;
1053 		if (sc->sc_imlen == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
1054 			goto gotit;
1055 		if (sc->sc_imlen >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
1056 		    sc->sc_imlen == sc->sc_imess[1] + 2)
1057 			goto gotit;
1058 	}
1059 #if 0
1060 	/* Ack what we have so far */
1061 	ESPCMD(sc, ESPCMD_MSGOK);
1062 #endif
1063 	return;
1064 
1065 gotit:
1066 	SPC_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
1067 	/*
1068 	 * Now we should have a complete message (1 byte, 2 byte
1069 	 * and moderately long extended messages).  We only handle
1070 	 * extended messages which total length is shorter than
1071 	 * SPC_MAX_MSG_LEN.  Longer messages will be amputated.
1072 	 */
1073 	if (sc->sc_state == SPC_HASNEXUS) {
1074 		struct acb *acb = sc->sc_nexus;
1075 		struct spc_tinfo *ti =
1076 			&sc->sc_tinfo[acb->xs->xs_periph->periph_target];
1077 
1078 		switch (sc->sc_imess[0]) {
1079 		case MSG_CMDCOMPLETE:
1080 			SPC_MSGS(("cmdcomplete "));
1081 			if (sc->sc_dleft < 0) {
1082 				struct scsipi_periph *periph = acb->xs->xs_periph;
1083 				printf("mha: %d extra bytes from %d:%d\n",
1084 					-sc->sc_dleft,
1085 					periph->periph_target,
1086 				        periph->periph_lun);
1087 				sc->sc_dleft = 0;
1088 			}
1089 			acb->xs->resid = acb->dleft = sc->sc_dleft;
1090 			sc->sc_flags |= SPC_BUSFREE_OK;
1091 			break;
1092 
1093 		case MSG_MESSAGE_REJECT:
1094 #if SPC_DEBUG
1095 			if (mha_debug & SPC_SHOWMSGS)
1096 				printf("%s: our msg rejected by target\n",
1097 					device_xname(sc->sc_dev));
1098 #endif
1099 #if 1 /* XXX - must remember last message */
1100 			scsipi_printaddr(acb->xs->xs_periph);
1101 			printf("MSG_MESSAGE_REJECT>>");
1102 #endif
1103 			if (sc->sc_flags & SPC_SYNCHNEGO) {
1104 				ti->period = ti->offset = 0;
1105 				sc->sc_flags &= ~SPC_SYNCHNEGO;
1106 				ti->flags &= ~T_NEGOTIATE;
1107 			}
1108 			/* Not all targets understand INITIATOR_DETECTED_ERR */
1109 			if (sc->sc_msgout == SEND_INIT_DET_ERR)
1110 				mha_sched_msgout(SEND_ABORT);
1111 			break;
1112 		case MSG_NOOP:
1113 			SPC_MSGS(("noop "));
1114 			break;
1115 		case MSG_DISCONNECT:
1116 			SPC_MSGS(("disconnect "));
1117 			ti->dconns++;
1118 			sc->sc_flags |= SPC_DISCON;
1119 			sc->sc_flags |= SPC_BUSFREE_OK;
1120 			if ((acb->xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
1121 				break;
1122 			/*FALLTHROUGH*/
1123 		case MSG_SAVEDATAPOINTER:
1124 			SPC_MSGS(("save datapointer "));
1125 			acb->dleft = sc->sc_dleft;
1126 			acb->daddr = sc->sc_dp;
1127 			break;
1128 		case MSG_RESTOREPOINTERS:
1129 			SPC_MSGS(("restore datapointer "));
1130 			if (!acb) {
1131 				mha_sched_msgout(SEND_ABORT);
1132 				printf("%s: no DATAPOINTERs to restore\n",
1133 				    device_xname(sc->sc_dev));
1134 				break;
1135 			}
1136 			sc->sc_dp = acb->daddr;
1137 			sc->sc_dleft = acb->dleft;
1138 			break;
1139 		case MSG_PARITY_ERROR:
1140 			printf("%s:target%d: MSG_PARITY_ERROR\n",
1141 				device_xname(sc->sc_dev),
1142 				acb->xs->xs_periph->periph_target);
1143 			break;
1144 		case MSG_EXTENDED:
1145 			SPC_MSGS(("extended(%x) ", sc->sc_imess[2]));
1146 			switch (sc->sc_imess[2]) {
1147 			case MSG_EXT_SDTR:
1148 				SPC_MSGS(("SDTR period %d, offset %d ",
1149 					sc->sc_imess[3], sc->sc_imess[4]));
1150 				ti->period = sc->sc_imess[3];
1151 				ti->offset = sc->sc_imess[4];
1152 				if (sc->sc_minsync == 0) {
1153 					/* We won't do synch */
1154 					ti->offset = 0;
1155 					mha_sched_msgout(SEND_SDTR);
1156 				} else if (ti->offset == 0) {
1157 					printf("%s:%d: async\n", "mha",
1158 						acb->xs->xs_periph->periph_target);
1159 					ti->offset = 0;
1160 					sc->sc_flags &= ~SPC_SYNCHNEGO;
1161 				} else if (ti->period > 124) {
1162 					printf("%s:%d: async\n", "mha",
1163 						acb->xs->xs_periph->periph_target);
1164 					ti->offset = 0;
1165 					mha_sched_msgout(SEND_SDTR);
1166 				} else {
1167 #if 0
1168 					int p;
1169 					p =  mha_stp2cpb(sc, ti->period);
1170 					ti->period = mha_cpb2stp(sc, p);
1171 #endif
1172 
1173 #if SPC_DEBUG
1174 					scsipi_printaddr(acb->xs->xs_periph);
1175 #endif
1176 					if ((sc->sc_flags&SPC_SYNCHNEGO) == 0) {
1177 						/* Target initiated negotiation */
1178 						if (ti->flags & T_SYNCMODE) {
1179 						    ti->flags &= ~T_SYNCMODE;
1180 #if SPC_DEBUG
1181 						    printf("renegotiated ");
1182 #endif
1183 						}
1184 						TMR=TM_ASYNC;
1185 						/* Clamp to our maxima */
1186 						if (ti->period < sc->sc_minsync)
1187 							ti->period = sc->sc_minsync;
1188 						if (ti->offset > 15)
1189 							ti->offset = 15;
1190 						mha_sched_msgout(SEND_SDTR);
1191 					} else {
1192 						/* we are sync */
1193 						sc->sc_flags &= ~SPC_SYNCHNEGO;
1194 						TMR = TM_SYNC;
1195 						ti->flags |= T_SYNCMODE;
1196 					}
1197 				}
1198 				ti->flags &= ~T_NEGOTIATE;
1199 				break;
1200 			default: /* Extended messages we don't handle */
1201 				CMR = CMD_SET_ATN; /* XXX? */
1202 				break;
1203 			}
1204 			break;
1205 		default:
1206 			SPC_MSGS(("ident "));
1207 			/* thanks for that ident... */
1208 			if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1209 				SPC_MISC(("unknown "));
1210 printf("%s: unimplemented message: %d\n", device_xname(sc->sc_dev), sc->sc_imess[0]);
1211 				CMR = CMD_SET_ATN; /* XXX? */
1212 			}
1213 			break;
1214 		}
1215 	} else if (sc->sc_state == SPC_RESELECTED) {
1216 		struct scsipi_periph *periph = NULL;
1217 		struct acb *acb;
1218 		struct spc_tinfo *ti;
1219 		u_char lunit;
1220 
1221 		if (MSG_ISIDENTIFY(sc->sc_imess[0])) { 	/* Identify? */
1222 			SPC_MISC(("searching "));
1223 			/*
1224 			 * Search wait queue for disconnected cmd
1225 			 * The list should be short, so I haven't bothered with
1226 			 * any more sophisticated structures than a simple
1227 			 * singly linked list.
1228 			 */
1229 			lunit = sc->sc_imess[0] & 0x07;
1230 			TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
1231 				periph = acb->xs->xs_periph;
1232 				if (periph->periph_lun == lunit &&
1233 				    sc->sc_selid == (1<<periph->periph_target)) {
1234 					TAILQ_REMOVE(&sc->nexus_list, acb,
1235 					    chain);
1236 					ACB_SETQ(acb, ACB_QNONE);
1237 					break;
1238 				}
1239 			}
1240 
1241 			if (!acb) {		/* Invalid reselection! */
1242 				mha_sched_msgout(SEND_ABORT);
1243 				printf("mha: invalid reselect (idbit=0x%2x)\n",
1244 				    sc->sc_selid);
1245 			} else {		/* Reestablish nexus */
1246 				/*
1247 				 * Setup driver data structures and
1248 				 * do an implicit RESTORE POINTERS
1249 				 */
1250 				ti = &sc->sc_tinfo[periph->periph_target];
1251 				sc->sc_nexus = acb;
1252 				sc->sc_dp = acb->daddr;
1253 				sc->sc_dleft = acb->dleft;
1254 				sc->sc_tinfo[periph->periph_target].lubusy
1255 					|= (1<<periph->periph_lun);
1256 				if (ti->flags & T_SYNCMODE) {
1257 					TMR = TM_SYNC;	/* XXX */
1258 				} else {
1259 					TMR = TM_ASYNC;
1260 				}
1261 				SPC_MISC(("... found acb"));
1262 				sc->sc_state = SPC_HASNEXUS;
1263 			}
1264 		} else {
1265 			printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
1266 			    device_xname(sc->sc_dev), sc->sc_selid);
1267 			mha_sched_msgout(SEND_DEV_RESET);
1268 		}
1269 	} else { /* Neither SPC_HASNEXUS nor SPC_RESELECTED! */
1270 		printf("%s: unexpected message in; will send DEV_RESET\n",
1271 		    device_xname(sc->sc_dev));
1272 		mha_sched_msgout(SEND_DEV_RESET);
1273 	}
1274 
1275 	/* Ack last message byte */
1276 #if 0
1277 	ESPCMD(sc, ESPCMD_MSGOK);
1278 #endif
1279 
1280 	/* Done, reset message pointer. */
1281 	sc->sc_flags &= ~SPC_DROP_MSGI;
1282 	sc->sc_imlen = 0;
1283 }
1284 
1285 /*
1286  * Send the highest priority, scheduled message.
1287  */
1288 void
1289 mha_msgout(struct mha_softc *sc)
1290 {
1291 #if (SPC_USE_SYNCHRONOUS || SPC_USE_WIDE)
1292 	struct spc_tinfo *ti;
1293 #endif
1294 	int n;
1295 
1296 	SPC_TRACE(("mha_msgout  "));
1297 
1298 	if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1299 		if (sc->sc_omp == sc->sc_omess) {
1300 			/*
1301 			 * This is a retransmission.
1302 			 *
1303 			 * We get here if the target stayed in MESSAGE OUT
1304 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
1305 			 * that all of the previously transmitted messages must
1306 			 * be sent again, in the same order.  Therefore, we
1307 			 * requeue all the previously transmitted messages, and
1308 			 * start again from the top.  Our simple priority
1309 			 * scheme keeps the messages in the right order.
1310 			 */
1311 			SPC_MISC(("retransmitting  "));
1312 			sc->sc_msgpriq |= sc->sc_msgoutq;
1313 			/*
1314 			 * Set ATN.  If we're just sending a trivial 1-byte
1315 			 * message, we'll clear ATN later on anyway.
1316 			 */
1317 			CMR = CMD_SET_ATN; /* XXX? */
1318 		} else {
1319 			/* This is a continuation of the previous message. */
1320 			n = sc->sc_omp - sc->sc_omess;
1321 			goto nextbyte;
1322 		}
1323 	}
1324 
1325 	/* No messages transmitted so far. */
1326 	sc->sc_msgoutq = 0;
1327 	sc->sc_lastmsg = 0;
1328 
1329 nextmsg:
1330 	/* Pick up highest priority message. */
1331 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1332 	sc->sc_msgpriq &= ~sc->sc_currmsg;
1333 	sc->sc_msgoutq |= sc->sc_currmsg;
1334 
1335 	/* Build the outgoing message data. */
1336 	switch (sc->sc_currmsg) {
1337 	case SEND_IDENTIFY:
1338 		SPC_ASSERT(sc->sc_nexus != NULL);
1339 		sc->sc_omess[0] =
1340 		    MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1);
1341 		n = 1;
1342 		break;
1343 
1344 #if SPC_USE_SYNCHRONOUS
1345 	case SEND_SDTR:
1346 		SPC_ASSERT(sc->sc_nexus != NULL);
1347 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
1348 		sc->sc_omess[4] = MSG_EXTENDED;
1349 		sc->sc_omess[3] = 3;
1350 		sc->sc_omess[2] = MSG_EXT_SDTR;
1351 		sc->sc_omess[1] = ti->period >> 2;
1352 		sc->sc_omess[0] = ti->offset;
1353 		n = 5;
1354 		break;
1355 #endif
1356 
1357 #if SPC_USE_WIDE
1358 	case SEND_WDTR:
1359 		SPC_ASSERT(sc->sc_nexus != NULL);
1360 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
1361 		sc->sc_omess[3] = MSG_EXTENDED;
1362 		sc->sc_omess[2] = 2;
1363 		sc->sc_omess[1] = MSG_EXT_WDTR;
1364 		sc->sc_omess[0] = ti->width;
1365 		n = 4;
1366 		break;
1367 #endif
1368 
1369 	case SEND_DEV_RESET:
1370 		sc->sc_flags |= SPC_ABORTING;
1371 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1372 		n = 1;
1373 		break;
1374 
1375 	case SEND_REJECT:
1376 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1377 		n = 1;
1378 		break;
1379 
1380 	case SEND_PARITY_ERROR:
1381 		sc->sc_omess[0] = MSG_PARITY_ERROR;
1382 		n = 1;
1383 		break;
1384 
1385 	case SEND_INIT_DET_ERR:
1386 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1387 		n = 1;
1388 		break;
1389 
1390 	case SEND_ABORT:
1391 		sc->sc_flags |= SPC_ABORTING;
1392 		sc->sc_omess[0] = MSG_ABORT;
1393 		n = 1;
1394 		break;
1395 
1396 	default:
1397 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1398 		    device_xname(sc->sc_dev));
1399 		SPC_BREAK();
1400 		sc->sc_omess[0] = MSG_NOOP;
1401 		n = 1;
1402 		break;
1403 	}
1404 	sc->sc_omp = &sc->sc_omess[n];
1405 
1406 nextbyte:
1407 	/* Send message bytes. */
1408 	/* send TRANSFER command. */
1409 	sc->sc_ps[3] = 1;
1410 	sc->sc_ps[4] = n >> 8;
1411 	sc->sc_pc[10] = n;
1412 	sc->sc_ps[-1] = 0x000F;	/* burst */
1413 	__asm volatile ("nop");
1414 	CMR = CMD_SEND_FROM_DMA;	/* send from DMA */
1415 	for (;;) {
1416 		if ((SSR & SS_BUSY) != 0)
1417 			break;
1418 		if (SSR & SS_IREQUEST)
1419 			goto out;
1420 	}
1421 	for (;;) {
1422 #if 0
1423 		for (;;) {
1424 			if ((PSNS & PSNS_REQ) != 0)
1425 				break;
1426 			/* Wait for REQINIT.  XXX Need timeout. */
1427 		}
1428 #endif
1429 		if (SSR & SS_IREQUEST) {
1430 			/*
1431 			 * Target left MESSAGE OUT, possibly to reject
1432 			 * our message.
1433 			 *
1434 			 * If this is the last message being sent, then we
1435 			 * deassert ATN, since either the target is going to
1436 			 * ignore this message, or it's going to ask for a
1437 			 * retransmission via MESSAGE PARITY ERROR (in which
1438 			 * case we reassert ATN anyway).
1439 			 */
1440 #if 0
1441 			if (sc->sc_msgpriq == 0)
1442 				CMR = CMD_RESET_ATN;
1443 #endif
1444 			goto out;
1445 		}
1446 
1447 #if 0
1448 		/* Clear ATN before last byte if this is the last message. */
1449 		if (n == 1 && sc->sc_msgpriq == 0)
1450 			CMR = CMD_RESET_ATN;
1451 #endif
1452 
1453 		while ((SSR & SS_DREG_FULL) != 0)
1454 			;
1455 		/* Send message byte. */
1456 		sc->sc_pc[0] = *--sc->sc_omp;
1457 		--n;
1458 		/* Keep track of the last message we've sent any bytes of. */
1459 		sc->sc_lastmsg = sc->sc_currmsg;
1460 
1461 		if (n == 0)
1462 			break;
1463 	}
1464 
1465 	/* We get here only if the entire message has been transmitted. */
1466 	if (sc->sc_msgpriq != 0) {
1467 		/* There are more outgoing messages. */
1468 		goto nextmsg;
1469 	}
1470 
1471 	/*
1472 	 * The last message has been transmitted.  We need to remember the last
1473 	 * message transmitted (in case the target switches to MESSAGE IN phase
1474 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
1475 	 * this time around (in case the target stays in MESSAGE OUT phase to
1476 	 * request a retransmit).
1477 	 */
1478 
1479 out:
1480 	/* Disable REQ/ACK protocol. */
1481 	return;
1482 }
1483 
1484 /***************************************************************
1485  *
1486  *	datain/dataout
1487  *
1488  */
1489 
1490 int
1491 mha_datain_pio(struct mha_softc *sc, u_char *p, int n)
1492 {
1493 	u_short d;
1494 	int a;
1495 	int total_n = n;
1496 
1497 	SPC_TRACE(("[mha_datain_pio(%p,%d)", p, n));
1498 
1499 	WAIT;
1500 	sc->sc_ps[3] = 1;
1501 	sc->sc_ps[4] = n >> 8;
1502 	sc->sc_pc[10] = n;
1503 	/* $BHa$7$-%=%U%HE>Aw(B */
1504 	CMR = CMD_RECEIVE_TO_MPU;
1505 	for (;;) {
1506 		a = SSR;
1507 		if (a & 0x04) {
1508 			d = sc->sc_ps[0];
1509 			*p++ = d >> 8;
1510 			if (--n > 0) {
1511 				*p++ = d;
1512 				--n;
1513 			}
1514 			a = SSR;
1515 		}
1516 		if (a & 0x40)
1517 			continue;
1518 		if (a & 0x80)
1519 			break;
1520 	}
1521 	SPC_TRACE(("...%d resd]", n));
1522 	return total_n - n;
1523 }
1524 
1525 int
1526 mha_dataout_pio(struct mha_softc *sc, u_char *p, int n)
1527 {
1528 	u_short d;
1529 	int a;
1530 	int total_n = n;
1531 
1532 	SPC_TRACE(("[mha_dataout_pio(%p,%d)", p, n));
1533 
1534 	WAIT;
1535 	sc->sc_ps[3] = 1;
1536 	sc->sc_ps[4] = n >> 8;
1537 	sc->sc_pc[10] = n;
1538 	/* $BHa$7$-%=%U%HE>Aw(B */
1539 	CMR = CMD_SEND_FROM_MPU;
1540 	for (;;) {
1541 		a = SSR;
1542 		if (a & 0x04) {
1543 			d = *p++ << 8;
1544 			if (--n > 0) {
1545 				d |= *p++;
1546 				--n;
1547 			}
1548 			sc->sc_ps[0] = d;
1549 			a = SSR;
1550 		}
1551 		if (a & 0x40)
1552 			continue;
1553 		if (a & 0x80)
1554 			break;
1555 	}
1556 	SPC_TRACE(("...%d resd]", n));
1557 	return total_n - n;
1558 }
1559 
1560 /*
1561  * dw: DMA word
1562  * cw: CMR word
1563  */
1564 static int
1565 mha_dataio_dma(int dw, int cw, struct mha_softc *sc, u_char *p, int n)
1566 {
1567 	char *paddr;
1568 
1569 	if (n > MAXBSIZE)
1570 		panic("transfer size exceeds MAXBSIZE");
1571 	if (sc->sc_dmasize > 0)
1572 		panic("DMA request while another DMA transfer is in pregress");
1573 
1574 	if (cw == CMD_SEND_FROM_DMA) {
1575 		memcpy(sc->sc_dmabuf, p, n);
1576 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREWRITE);
1577 	} else {
1578 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREREAD);
1579 	}
1580 	sc->sc_p = p;
1581 	sc->sc_dmasize = n;
1582 
1583 	paddr = (char *)sc->sc_dmaseg[0].ds_addr;
1584 #if MHA_DMA_SHORT_BUS_CYCLE == 1
1585 	if ((*(volatile int *)&IODEVbase->io_sram[0xac]) &
1586 	    (1 << ((paddr_t)paddr >> 19)))
1587 		dw &= ~(1 << 3);
1588 #endif
1589 	sc->sc_pc[0x80 + (((long)paddr >> 16) & 0xFF)] = 0;
1590 	sc->sc_pc[0x180 + (((long)paddr >> 8) & 0xFF)] = 0;
1591 	sc->sc_pc[0x280 + (((long)paddr >> 0) & 0xFF)] = 0;
1592 	WAIT;
1593 	sc->sc_ps[3] = 1;
1594 	sc->sc_ps[4] = n >> 8;
1595 	sc->sc_pc[10] = n;
1596 	/* DMA $BE>Aw@)8f$O0J2<$NDL$j!#(B
1597 	   3 ... short bus cycle
1598 	   2 ... MAXIMUM XFER.
1599 	   1 ... BURST XFER.
1600 	   0 ... R/W */
1601 	sc->sc_ps[-1] = dw;	/* burst */
1602 	__asm volatile ("nop");
1603 	CMR = cw;	/* receive to DMA */
1604 	return n;
1605 }
1606 
1607 int
1608 mha_dataout(struct mha_softc *sc, u_char *p, int n)
1609 {
1610 	if (n == 0)
1611 		return n;
1612 
1613 	if (n & 1)
1614 		return mha_dataout_pio(sc, p, n);
1615 	return mha_dataio_dma(MHA_DMA_DATAOUT, CMD_SEND_FROM_DMA, sc, p, n);
1616 }
1617 
1618 int
1619 mha_datain(struct mha_softc *sc, u_char *p, int n)
1620 {
1621 	 struct acb *acb = sc->sc_nexus;
1622 
1623 	 if (n == 0)
1624 		 return n;
1625 	 if (acb->cmd.opcode == SCSI_REQUEST_SENSE || (n & 1))
1626 		 return mha_datain_pio(sc, p, n);
1627 	 return mha_dataio_dma(MHA_DMA_DATAIN, CMD_RECEIVE_TO_DMA, sc, p, n);
1628 }
1629 
1630 /*
1631  * Catch an interrupt from the adaptor
1632  */
1633 /*
1634  * This is the workhorse routine of the driver.
1635  * Deficiencies (for now):
1636  * 1) always uses programmed I/O
1637  */
1638 int
1639 mhaintr(void *arg)
1640 {
1641 	struct mha_softc *sc = arg;
1642 #if 0
1643 	u_char ints;
1644 #endif
1645 	struct acb *acb;
1646 	u_char ph;
1647 	u_short r;
1648 	int n;
1649 
1650 #if 1	/* XXX called during attach? */
1651 	if (tmpsc != NULL) {
1652 		SPC_MISC(("[%p %p]\n", mha_cd.cd_devs, sc));
1653 		sc = tmpsc;
1654 	} else {
1655 #endif
1656 
1657 #if 1	/* XXX */
1658 	}
1659 #endif
1660 
1661 #if 0
1662 	/*
1663 	 * $B3d$j9~$_6X;_$K$9$k(B
1664 	 */
1665 	SCTL &= ~SCTL_INTR_ENAB;
1666 #endif
1667 
1668 	SPC_TRACE(("[mhaintr]"));
1669 
1670 	/*
1671 	 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
1672 	 */
1673 	/*
1674 	 * First check for abnormal conditions, such as reset.
1675 	 */
1676 #if 0
1677 #if 1 /* XXX? */
1678 	while (((ints = SSR) & SS_IREQUEST) == 0)
1679 		delay(1);
1680 	SPC_MISC(("ints = 0x%x  ", ints));
1681 #else /* usually? */
1682 	ints = SSR;
1683 #endif
1684 #endif
1685 	while (SSR & SS_IREQUEST) {
1686 		acb = sc->sc_nexus;
1687 		r = ISCSR;
1688 		SPC_MISC(("[r=0x%x]", r));
1689 		switch (r >> 8) {
1690 		default:
1691 			printf("[addr=%p\n"
1692 			       "result=0x%x\n"
1693 			       "cmd=0x%x\n"
1694 			       "ph=0x%x(ought to be %d)]\n",
1695 			       &ISCSR,
1696 			       r,
1697 			       acb->xs->cmd->opcode,
1698 			       SCR, sc->sc_phase);
1699 			panic("unexpected result.");
1700 		case 0x82:	/* selection timeout */
1701 			SPC_MISC(("selection timeout  "));
1702 			sc->sc_phase = BUSFREE_PHASE;
1703 			SPC_ASSERT(sc->sc_nexus != NULL);
1704 			acb = sc->sc_nexus;
1705 			delay(250);
1706 			acb->xs->error = XS_SELTIMEOUT;
1707 			mha_done(sc, acb);
1708 			continue;	/* XXX ??? msaitoh */
1709 		case 0x60:	/* command completed */
1710 			sc->sc_spcinitialized++;
1711 			if (sc->sc_phase == BUSFREE_PHASE)
1712 				continue;
1713 			ph = SCR;
1714 			if (ph & PSNS_ACK) {
1715 				int s;
1716 				/* $B$U$D!<$N%3%^%s%I$,=*N;$7$?$i$7$$(B */
1717 				SPC_MISC(("0x60)phase = %x(ought to be %x)\n",
1718 					  ph & PHASE_MASK, sc->sc_phase));
1719 #if 0
1720 /*				switch (sc->sc_phase) {*/
1721 #else
1722 				switch (ph & PHASE_MASK) {
1723 #endif
1724 				case STATUS_PHASE:
1725 					if (sc->sc_state != SPC_HASNEXUS)
1726 						printf("stsin: !SPC_HASNEXUS->(%d)\n",
1727 						       sc->sc_state);
1728 					SPC_ASSERT(sc->sc_nexus != NULL);
1729 					acb = sc->sc_nexus;
1730 					WAIT;
1731 					s = MBR;
1732 					SPC_ASSERT(s == 1);
1733 					acb->stat = sc->sc_pcx[0]; /* XXX */
1734 					SPC_MISC(("stat=0x%02x  ", acb->stat));
1735 					sc->sc_prevphase = STATUS_PHASE;
1736 					break;
1737 				case MESSAGE_IN_PHASE:
1738 					mha_msgin(sc);
1739 					sc->sc_prevphase = MESSAGE_IN_PHASE;
1740 					/* thru */
1741 				case DATA_IN_PHASE:
1742 					if (sc->sc_dmasize == 0)
1743 						break;
1744 					bus_dmamap_sync(sc->sc_dmat,
1745 							sc->sc_dmamap,
1746 							0, sc->sc_dmasize,
1747 							BUS_DMASYNC_POSTREAD);
1748 					memcpy(sc->sc_p, sc->sc_dmabuf,
1749 					       sc->sc_dmasize);
1750 					sc->sc_dmasize = 0;
1751 					break;
1752 				case DATA_OUT_PHASE:
1753 					if (sc->sc_dmasize == 0)
1754 						break;
1755 					bus_dmamap_sync(sc->sc_dmat,
1756 							sc->sc_dmamap,
1757 							0, sc->sc_dmasize,
1758 							BUS_DMASYNC_POSTWRITE);
1759 					sc->sc_dmasize = 0;
1760 					break;
1761 				}
1762 				WAIT;
1763 				CMR = CMD_RESET_ACK;	/* reset ack */
1764 				/*mha_done(sc, acb);	XXX */
1765 				continue;
1766 			} else if (NSR & 0x80) { /* nexus */
1767 #if 1
1768 				if (sc->sc_state == SPC_SELECTING)	/* XXX msaitoh */
1769 					sc->sc_state = SPC_HASNEXUS;
1770 				/* $B%U%'!<%:$N7h$aBG$A$r$9$k(B
1771 				   $B30$l$?$i!"(Binitial-phase error(0x54) $B$,(B
1772 				   $BJV$C$F$/$k$s$GCm0U$7$?$^$(!#(B
1773 				   $B$G$b$J$<$+(B 0x65 $B$,JV$C$F$-$?$j$7$F$M!<$+(B? */
1774 				WAIT;
1775 				if (SSR & SS_IREQUEST)
1776 					continue;
1777 				switch (sc->sc_phase) {
1778 				default:
1779 					panic("$B8+CN$i$L(B phase $B$,Mh$A$^$C$?$@$h(B");
1780 				case MESSAGE_IN_PHASE:
1781 					/* $B2?$b$7$J$$(B */
1782 					continue;
1783 				case STATUS_PHASE:
1784 					sc->sc_phase = MESSAGE_IN_PHASE;
1785 					CMR = CMD_RECEIVE_MSG;	/* receive msg */
1786 					continue;
1787 				case DATA_IN_PHASE:
1788 					sc->sc_prevphase = DATA_IN_PHASE;
1789 					if (sc->sc_dleft == 0) {
1790 						/* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
1791 						   $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
1792 						sc->sc_phase = STATUS_PHASE;
1793 						CMR = CMD_RECEIVE_STS;	/* receive sts */
1794 						continue;
1795 					}
1796 					n = mha_datain(sc, sc->sc_dp,
1797 						       sc->sc_dleft);
1798 					sc->sc_dp += n;
1799 					sc->sc_dleft -= n;
1800 					continue;
1801 				case DATA_OUT_PHASE:
1802 					sc->sc_prevphase = DATA_OUT_PHASE;
1803 					if (sc->sc_dleft == 0) {
1804 						/* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
1805 						   $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
1806 						sc->sc_phase = STATUS_PHASE;
1807 						CMR = CMD_RECEIVE_STS;	/* receive sts */
1808 						continue;
1809 					}
1810 					/* data phase $B$NB3$-$r$d$m$&(B */
1811 					n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1812 					sc->sc_dp += n;
1813 					sc->sc_dleft -= n;
1814 					continue;
1815 				case COMMAND_PHASE:
1816 					/* $B:G=i$O(B CMD PHASE $B$H$$$&$3$H$i$7$$(B */
1817 					if (acb->dleft) {
1818 						/* $B%G!<%?E>Aw$,$"$j$&$k>l9g(B */
1819 						if (acb->xs->xs_control & XS_CTL_DATA_IN) {
1820 							sc->sc_phase = DATA_IN_PHASE;
1821 							n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
1822 							sc->sc_dp += n;
1823 							sc->sc_dleft -= n;
1824 						}
1825 						else if (acb->xs->xs_control & XS_CTL_DATA_OUT) {
1826 							sc->sc_phase = DATA_OUT_PHASE;
1827 							n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1828 							sc->sc_dp += n;
1829 							sc->sc_dleft -= n;
1830 						}
1831 						continue;
1832 					}
1833 					else {
1834 						/* $B%G!<%?E>Aw$O$J$$$i$7$$(B?! */
1835 						WAIT;
1836 						sc->sc_phase = STATUS_PHASE;
1837 						CMR = CMD_RECEIVE_STS;	/* receive sts */
1838 						continue;
1839 					}
1840 				}
1841 #endif
1842 			}
1843 			continue;
1844 		case 0x31:	/* disconnected in xfer progress. */
1845 			SPC_MISC(("[0x31]"));
1846 		case 0x70:	/* disconnected. */
1847 			SPC_ASSERT(sc->sc_flags & SPC_BUSFREE_OK);
1848 			sc->sc_phase = BUSFREE_PHASE;
1849 			sc->sc_state = SPC_IDLE;
1850 #if 1
1851 			acb = sc->sc_nexus;
1852 			SPC_ASSERT(sc->sc_nexus != NULL);
1853 			acb->xs->error = XS_NOERROR;
1854 			mha_done(sc, acb);
1855 #else
1856 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1857 			mha_sched(sc);
1858 #endif
1859 			continue;
1860 		case 0x32:	/* phase error in xfer progress. */
1861 			SPC_MISC(("[0x32]"));
1862 #if 0
1863 		case 0x65:	/* invalid command.
1864 				   $B$J$<$3$s$J$b$N$,=P$k$N$+(B
1865 				   $B26$K$OA4$/M}2r$G$-$J$$(B */
1866 #if 1
1867 			SPC_MISC(("[0x%04x]", r));
1868 #endif
1869 #endif
1870 		case 0x54:	/* initial-phase error. */
1871 			SPC_MISC(("[0x54, ns=%x, ph=%x(ought to be %x)]",
1872 				  NSR,
1873 				  SCR, sc->sc_phase));
1874 			/* thru */
1875 		case 0x71:	/* assert req */
1876 			WAIT;
1877 			if (SSR & 0x40) {
1878 				printf("SPC sts=%2x, r=%04x, ns=%x, ph=%x\n",
1879 				       SSR, r, NSR, SCR);
1880 				WAIT;
1881 			}
1882 			ph = SCR;
1883 			if (sc->sc_state == SPC_SELECTING) {	/* XXX msaitoh */
1884 				sc->sc_state = SPC_HASNEXUS;
1885 			}
1886 			if (ph & 0x80) {
1887 				switch (ph & PHASE_MASK) {
1888 				default:
1889 					printf("phase = %x\n", ph);
1890 					panic("assert req: the phase I don't know!");
1891 				case DATA_IN_PHASE:
1892 					sc->sc_prevphase = DATA_IN_PHASE;
1893 					SPC_MISC(("DATAIN(%d)...", sc->sc_dleft));
1894 					n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
1895 					sc->sc_dp += n;
1896 					sc->sc_dleft -= n;
1897 					SPC_MISC(("done\n"));
1898 					continue;
1899 				case DATA_OUT_PHASE:
1900 					sc->sc_prevphase = DATA_OUT_PHASE;
1901 					SPC_MISC(("DATAOUT\n"));
1902 					n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1903 					sc->sc_dp += n;
1904 					sc->sc_dleft -= n;
1905 					continue;
1906 				case STATUS_PHASE:
1907 					sc->sc_phase = STATUS_PHASE;
1908 					SPC_MISC(("[RECV_STS]"));
1909 					WAIT;
1910 					CMR = CMD_RECEIVE_STS;	/* receive sts */
1911 					continue;
1912 				case MESSAGE_IN_PHASE:
1913 					sc->sc_phase = MESSAGE_IN_PHASE;
1914 					WAIT;
1915 					CMR = CMD_RECEIVE_MSG;
1916 					continue;
1917 				}
1918 			}
1919 			continue;
1920 		}
1921 	}
1922 
1923 	return 1;
1924 }
1925 
1926 void
1927 mha_abort(struct mha_softc *sc, struct acb *acb)
1928 {
1929 	acb->flags |= ACB_ABORTED;
1930 
1931 	if (acb == sc->sc_nexus) {
1932 		/*
1933 		 * If we're still selecting, the message will be scheduled
1934 		 * after selection is complete.
1935 		 */
1936 		if (sc->sc_state == SPC_HASNEXUS) {
1937 			sc->sc_flags |= SPC_ABORTING;
1938 			mha_sched_msgout(SEND_ABORT);
1939 		}
1940 	} else {
1941 		if (sc->sc_state == SPC_IDLE)
1942 			mha_sched(sc);
1943 	}
1944 }
1945 
1946 void
1947 mha_timeout(void *arg)
1948 {
1949 	struct acb *acb = (struct acb *)arg;
1950 	struct scsipi_xfer *xs = acb->xs;
1951 	struct scsipi_periph *periph = xs->xs_periph;
1952 	struct mha_softc *sc =
1953 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
1954 	int s;
1955 
1956 	s = splbio();
1957 
1958 	scsipi_printaddr(periph);
1959 	printf("%s: timed out [acb %p (flags 0x%x, dleft %x, stat %x)], "
1960 	       "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) >",
1961 		device_xname(sc->sc_dev),
1962 		acb, acb->flags, acb->dleft, acb->stat,
1963 		sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
1964 		sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout
1965 		);
1966 	printf("[%04x %02x]\n", sc->sc_ps[1], SCR);
1967 	panic("timeout, ouch!");
1968 
1969 	if (acb->flags & ACB_ABORTED) {
1970 		/* abort timed out */
1971 		printf(" AGAIN\n");
1972 #if 0
1973 		mha_init(sc, 1); /* XXX 1?*/
1974 #endif
1975 	} else {
1976 		/* abort the operation that has timed out */
1977 		printf("\n");
1978 		xs->error = XS_TIMEOUT;
1979 		mha_abort(sc, acb);
1980 	}
1981 
1982 	splx(s);
1983 }
1984 
1985 #if SPC_DEBUG
1986 /*
1987  * The following functions are mostly used for debugging purposes, either
1988  * directly called from the driver or from the kernel debugger.
1989  */
1990 
1991 void
1992 mha_show_scsi_cmd(struct acb *acb)
1993 {
1994 	u_char *b = (u_char *)&acb->cmd;
1995 	struct scsipi_periph *periph = acb->xs->xs_periph;
1996 	int i;
1997 
1998 	scsipi_printaddr(periph);
1999 	if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
2000 		for (i = 0; i < acb->clen; i++) {
2001 			if (i)
2002 				printf(",");
2003 			printf("%x", b[i]);
2004 		}
2005 		printf("\n");
2006 	} else
2007 		printf("RESET\n");
2008 }
2009 
2010 void
2011 mha_print_acb(struct acb *acb)
2012 {
2013 
2014 	printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags);
2015 	printf(" dp=%p dleft=%d stat=%x\n",
2016 	    acb->daddr, acb->dleft, acb->stat);
2017 	mha_show_scsi_cmd(acb);
2018 }
2019 
2020 void
2021 mha_print_active_acb(void)
2022 {
2023 	struct acb *acb;
2024 	struct mha_softc *sc = device_lookup_private(&mha_cd, 0); /* XXX */
2025 
2026 	printf("ready list:\n");
2027 	TAILQ_FOREACH(acb, &sc->ready_list, chain)
2028 		mha_print_acb(acb);
2029 	printf("nexus:\n");
2030 	if (sc->sc_nexus != NULL)
2031 		mha_print_acb(sc->sc_nexus);
2032 	printf("nexus list:\n");
2033 	TAILQ_FOREACH(acb, &sc->nexus_list, chain)
2034 		mha_print_acb(acb);
2035 }
2036 
2037 void
2038 mha_dump_driver(struct mha_softc *sc)
2039 {
2040 	struct spc_tinfo *ti;
2041 	int i;
2042 
2043 	printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2044 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2045 	    sc->sc_state, sc->sc_imess[0],
2046 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2047 	for (i = 0; i < 7; i++) {
2048 		ti = &sc->sc_tinfo[i];
2049 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2050 		    i, ti->cmds, ti->dconns, ti->touts);
2051 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2052 	}
2053 }
2054 #endif
2055