Home | History | Annotate | Line # | Download | only in ic
ninjascsi32.c revision 1.6
      1 /*	$NetBSD: ninjascsi32.c,v 1.6 2006/01/14 07:14:45 itohy Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004, 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by ITOH Yasufumi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ninjascsi32.c,v 1.6 2006/01/14 07:14:45 itohy Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/callout.h>
     45 #include <sys/device.h>
     46 #include <sys/kernel.h>
     47 #include <sys/buf.h>
     48 #include <sys/scsiio.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/intr.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <dev/scsipi/scsi_all.h>
     56 #include <dev/scsipi/scsipi_all.h>
     57 #include <dev/scsipi/scsiconf.h>
     58 #include <dev/scsipi/scsi_message.h>
     59 
     60 /*
     61  * DualEdge transfer support
     62  */
     63 /* #define NJSC32_DUALEDGE */	/* XXX untested */
     64 
     65 /*
     66  * Auto param loading does not work properly (it partially works (works on
     67  * start, doesn't on restart) on rev 0x54, it doesn't work at all on rev 0x51),
     68  * and it doesn't improve the performance so much,
     69  * forget about it.
     70  */
     71 #undef NJSC32_AUTOPARAM
     72 
     73 #include <dev/ic/ninjascsi32reg.h>
     74 #include <dev/ic/ninjascsi32var.h>
     75 
     76 /* #define NJSC32_DEBUG */
     77 /* #define NJSC32_TRACE */
     78 
     79 #ifdef NJSC32_DEBUG
     80 #define DPRINTF(x)	printf x
     81 #define DPRINTC(cmd, x)	PRINTC(cmd, x)
     82 #else
     83 #define DPRINTF(x)
     84 #define DPRINTC(cmd, x)
     85 #endif
     86 #ifdef NJSC32_TRACE
     87 #define TPRINTF(x)	printf x
     88 #define TPRINTC(cmd, x)	PRINTC(cmd, x)
     89 #else
     90 #define TPRINTF(x)
     91 #define TPRINTC(cmd, x)
     92 #endif
     93 
     94 #define PRINTC(cmd, x)	do {					\
     95 		scsi_print_addr((cmd)->c_xs->xs_periph);	\
     96 		printf x;					\
     97 	} while (/* CONSTCOND */ 0)
     98 
     99 static void	njsc32_scsipi_request(struct scsipi_channel *,
    100 		    scsipi_adapter_req_t, void *);
    101 static void	njsc32_scsipi_minphys(struct buf *);
    102 static int	njsc32_scsipi_ioctl(struct scsipi_channel *, u_long, caddr_t,
    103 		    int, struct proc *);
    104 
    105 static void	njsc32_init(struct njsc32_softc *, int nosleep);
    106 static int	njsc32_init_cmds(struct njsc32_softc *);
    107 static void	njsc32_target_async(struct njsc32_softc *,
    108 		    struct njsc32_target *);
    109 static void	njsc32_init_targets(struct njsc32_softc *);
    110 static void	njsc32_add_msgout(struct njsc32_softc *, int);
    111 static u_int32_t njsc32_get_auto_msgout(struct njsc32_softc *);
    112 #ifdef NJSC32_DUALEDGE
    113 static void	njsc32_msgout_wdtr(struct njsc32_softc *, int);
    114 #endif
    115 static void	njsc32_msgout_sdtr(struct njsc32_softc *, int period,
    116 		    int offset);
    117 static void	njsc32_negotiate_xfer(struct njsc32_softc *,
    118 		    struct njsc32_target *);
    119 static void	njsc32_arbitration_failed(struct njsc32_softc *);
    120 static void	njsc32_start(struct njsc32_softc *);
    121 static void	njsc32_run_xfer(struct njsc32_softc *, struct scsipi_xfer *);
    122 static void	njsc32_end_cmd(struct njsc32_softc *, struct njsc32_cmd *,
    123 		    scsipi_xfer_result_t);
    124 static void	njsc32_reset_bus(struct njsc32_softc *);
    125 static void	njsc32_clear_cmds(struct njsc32_softc *,
    126 		    scsipi_xfer_result_t);
    127 static void	njsc32_reset_detected(struct njsc32_softc *);
    128 static void	njsc32_set_ptr(struct njsc32_softc *, struct njsc32_cmd *,
    129 		    u_int32_t);
    130 static void	njsc32_assert_ack(struct njsc32_softc *);
    131 static void	njsc32_negate_ack(struct njsc32_softc *);
    132 static void	njsc32_wait_req_negate(struct njsc32_softc *);
    133 static void	njsc32_reconnect(struct njsc32_softc *, struct njsc32_cmd *);
    134 enum njsc32_reselstat {
    135 	NJSC32_RESEL_ERROR,		/* to be rejected */
    136 	NJSC32_RESEL_COMPLETE,		/* reselection is just complete */
    137 	NJSC32_RESEL_THROUGH		/* this message is OK (no reply) */
    138 };
    139 static enum njsc32_reselstat njsc32_resel_identify(struct njsc32_softc *,
    140 		    int lun, struct njsc32_cmd **);
    141 static enum njsc32_reselstat njsc32_resel_tag(struct njsc32_softc *,
    142 		    int tag, struct njsc32_cmd **);
    143 static void	njsc32_cmd_reload(struct njsc32_softc *, struct njsc32_cmd *,
    144 		    int);
    145 static void	njsc32_update_xfer_mode(struct njsc32_softc *,
    146 		    struct njsc32_target *);
    147 static void	njsc32_msgin(struct njsc32_softc *);
    148 static void	njsc32_msgout(struct njsc32_softc *);
    149 static void	njsc32_cmdtimeout(void *);
    150 static void	njsc32_reseltimeout(void *);
    151 
    152 static inline unsigned
    153 njsc32_read_1(struct njsc32_softc *sc, int no)
    154 {
    155 
    156 	return bus_space_read_1(sc->sc_regt, sc->sc_regh, no);
    157 }
    158 
    159 static inline unsigned
    160 njsc32_read_2(struct njsc32_softc *sc, int no)
    161 {
    162 
    163 	return bus_space_read_2(sc->sc_regt, sc->sc_regh, no);
    164 }
    165 
    166 static inline u_int32_t
    167 njsc32_read_4(struct njsc32_softc *sc, int no)
    168 {
    169 
    170 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, no);
    171 }
    172 
    173 static inline void
    174 njsc32_write_1(struct njsc32_softc *sc, int no, int val)
    175 {
    176 
    177 	bus_space_write_1(sc->sc_regt, sc->sc_regh, no, val);
    178 }
    179 
    180 static inline void
    181 njsc32_write_2(struct njsc32_softc *sc, int no, int val)
    182 {
    183 
    184 	bus_space_write_2(sc->sc_regt, sc->sc_regh, no, val);
    185 }
    186 
    187 static inline void
    188 njsc32_write_4(struct njsc32_softc *sc, int no, u_int32_t val)
    189 {
    190 
    191 	bus_space_write_4(sc->sc_regt, sc->sc_regh, no, val);
    192 }
    193 
    194 static inline unsigned
    195 njsc32_ireg_read_1(struct njsc32_softc *sc, int no)
    196 {
    197 
    198 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no);
    199 	return bus_space_read_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW);
    200 }
    201 
    202 static inline unsigned
    203 njsc32_ireg_read_2(struct njsc32_softc *sc, int no)
    204 {
    205 
    206 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no);
    207 	return bus_space_read_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW);
    208 }
    209 
    210 static inline u_int32_t
    211 njsc32_ireg_read_4(struct njsc32_softc *sc, int no)
    212 {
    213 	u_int32_t val;
    214 
    215 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no);
    216 	val = (u_int16_t)bus_space_read_2(sc->sc_regt, sc->sc_regh,
    217 	    NJSC32_REG_DATA_LOW);
    218 	return val | (bus_space_read_2(sc->sc_regt, sc->sc_regh,
    219 	    NJSC32_REG_DATA_HIGH) << 16);
    220 }
    221 
    222 static inline void
    223 njsc32_ireg_write_1(struct njsc32_softc *sc, int no, int val)
    224 {
    225 
    226 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no);
    227 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW, val);
    228 }
    229 
    230 static inline void
    231 njsc32_ireg_write_2(struct njsc32_softc *sc, int no, int val)
    232 {
    233 
    234 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no);
    235 	bus_space_write_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW, val);
    236 }
    237 
    238 static inline void
    239 njsc32_ireg_write_4(struct njsc32_softc *sc, int no, u_int32_t val)
    240 {
    241 
    242 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NJSC32_REG_INDEX, no);
    243 	bus_space_write_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_LOW, val);
    244 	bus_space_write_2(sc->sc_regt, sc->sc_regh, NJSC32_REG_DATA_HIGH,
    245 	    val >> 16);
    246 }
    247 
    248 #define NS(ns)	((ns) / 4)	/* nanosecond (>= 50) -> sync value */
    249 #ifdef __STDC__
    250 # define ACKW(n)	NJSC32_ACK_WIDTH_ ## n ## CLK
    251 # define SMPL(n)	(NJSC32_SREQ_SAMPLING_ ## n ## CLK |	\
    252 			 NJSC32_SREQ_SAMPLING_ENABLE)
    253 #else
    254 # define ACKW(n)	NJSC32_ACK_WIDTH_/**/n/**/CLK
    255 # define SMPL(n)	(NJSC32_SREQ_SAMPLING_/**/n/**/CLK |	\
    256 			 NJSC32_SREQ_SAMPLING_ENABLE)
    257 #endif
    258 
    259 #define NJSC32_NSYNCT_MAXSYNC	1
    260 #define NJSC32_NSYNCT		16
    261 
    262 /* 40MHz (25ns) */
    263 static const struct njsc32_sync_param njsc32_synct_40M[NJSC32_NSYNCT] = {
    264 	{ 0, 0, 0 },			/* dummy for async */
    265 	{ NS( 50), ACKW(1), 0       },	/* 20.0 :  50ns,  25ns */
    266 	{ NS( 75), ACKW(1), SMPL(1) },	/* 13.3 :  75ns,  25ns */
    267 	{ NS(100), ACKW(2), SMPL(1) },	/* 10.0 : 100ns,  50ns */
    268 	{ NS(125), ACKW(2), SMPL(2) },	/*  8.0 : 125ns,  50ns */
    269 	{ NS(150), ACKW(3), SMPL(2) },	/*  6.7 : 150ns,  75ns */
    270 	{ NS(175), ACKW(3), SMPL(2) },	/*  5.7 : 175ns,  75ns */
    271 	{ NS(200), ACKW(4), SMPL(2) },	/*  5.0 : 200ns, 100ns */
    272 	{ NS(225), ACKW(4), SMPL(4) },	/*  4.4 : 225ns, 100ns */
    273 	{ NS(250), ACKW(4), SMPL(4) },	/*  4.0 : 250ns, 100ns */
    274 	{ NS(275), ACKW(4), SMPL(4) },	/*  3.64: 275ns, 100ns */
    275 	{ NS(300), ACKW(4), SMPL(4) },	/*  3.33: 300ns, 100ns */
    276 	{ NS(325), ACKW(4), SMPL(4) },	/*  3.01: 325ns, 100ns */
    277 	{ NS(350), ACKW(4), SMPL(4) },	/*  2.86: 350ns, 100ns */
    278 	{ NS(375), ACKW(4), SMPL(4) },	/*  2.67: 375ns, 100ns */
    279 	{ NS(400), ACKW(4), SMPL(4) }	/*  2.50: 400ns, 100ns */
    280 };
    281 
    282 #ifdef NJSC32_SUPPORT_OTHER_CLOCKS
    283 /* 20MHz (50ns) */
    284 static const struct njsc32_sync_param njsc32_synct_20M[NJSC32_NSYNCT] = {
    285 	{ 0, 0, 0 },			/* dummy for async */
    286 	{ NS(100), ACKW(1), 0       },	/* 10.0 : 100ns,  50ns */
    287 	{ NS(150), ACKW(1), SMPL(2) },	/*  6.7 : 150ns,  50ns */
    288 	{ NS(200), ACKW(2), SMPL(2) },	/*  5.0 : 200ns, 100ns */
    289 	{ NS(250), ACKW(2), SMPL(4) },	/*  4.0 : 250ns, 100ns */
    290 	{ NS(300), ACKW(3), SMPL(4) },	/*  3.3 : 300ns, 150ns */
    291 	{ NS(350), ACKW(3), SMPL(4) },	/*  2.8 : 350ns, 150ns */
    292 	{ NS(400), ACKW(4), SMPL(4) },	/*  2.5 : 400ns, 200ns */
    293 	{ NS(450), ACKW(4), SMPL(4) },	/*  2.2 : 450ns, 200ns */
    294 	{ NS(500), ACKW(4), SMPL(4) },	/*  2.0 : 500ns, 200ns */
    295 	{ NS(550), ACKW(4), SMPL(4) },	/*  1.82: 550ns, 200ns */
    296 	{ NS(600), ACKW(4), SMPL(4) },	/*  1.67: 600ns, 200ns */
    297 	{ NS(650), ACKW(4), SMPL(4) },	/*  1.54: 650ns, 200ns */
    298 	{ NS(700), ACKW(4), SMPL(4) },	/*  1.43: 700ns, 200ns */
    299 	{ NS(750), ACKW(4), SMPL(4) },	/*  1.33: 750ns, 200ns */
    300 	{ NS(800), ACKW(4), SMPL(4) }	/*  1.25: 800ns, 200ns */
    301 };
    302 
    303 /* 33.3MHz (30ns) */
    304 static const struct njsc32_sync_param njsc32_synct_pci[NJSC32_NSYNCT] = {
    305 	{ 0, 0, 0 },			/* dummy for async */
    306 	{ NS( 60), ACKW(1), 0       },	/* 16.6 :  60ns,  30ns */
    307 	{ NS( 90), ACKW(1), SMPL(1) },	/* 11.1 :  90ns,  30ns */
    308 	{ NS(120), ACKW(2), SMPL(2) },	/*  8.3 : 120ns,  60ns */
    309 	{ NS(150), ACKW(2), SMPL(2) },	/*  6.7 : 150ns,  60ns */
    310 	{ NS(180), ACKW(3), SMPL(2) },	/*  5.6 : 180ns,  90ns */
    311 	{ NS(210), ACKW(3), SMPL(4) },	/*  4.8 : 210ns,  90ns */
    312 	{ NS(240), ACKW(4), SMPL(4) },	/*  4.2 : 240ns, 120ns */
    313 	{ NS(270), ACKW(4), SMPL(4) },	/*  3.7 : 270ns, 120ns */
    314 	{ NS(300), ACKW(4), SMPL(4) },	/*  3.3 : 300ns, 120ns */
    315 	{ NS(330), ACKW(4), SMPL(4) },	/*  3.0 : 330ns, 120ns */
    316 	{ NS(360), ACKW(4), SMPL(4) },	/*  2.8 : 360ns, 120ns */
    317 	{ NS(390), ACKW(4), SMPL(4) },	/*  2.6 : 390ns, 120ns */
    318 	{ NS(420), ACKW(4), SMPL(4) },	/*  2.4 : 420ns, 120ns */
    319 	{ NS(450), ACKW(4), SMPL(4) },	/*  2.2 : 450ns, 120ns */
    320 	{ NS(480), ACKW(4), SMPL(4) }	/*  2.1 : 480ns, 120ns */
    321 };
    322 #endif	/* NJSC32_SUPPORT_OTHER_CLOCKS */
    323 
    324 #undef NS
    325 #undef ACKW
    326 #undef SMPL
    327 
    328 /* initialize device */
    329 static void
    330 njsc32_init(struct njsc32_softc *sc, int nosleep)
    331 {
    332 	u_int16_t intstat;
    333 
    334 	/* block all interrupts */
    335 	njsc32_write_2(sc, NJSC32_REG_IRQ, NJSC32_IRQ_MASK_ALL);
    336 
    337 	/* clear transfer */
    338 	njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0);
    339 	njsc32_write_4(sc, NJSC32_REG_BM_CNT, 0);
    340 
    341 	/* make sure interrupts are cleared */
    342 	/* XXX loop forever? */
    343 	while ((intstat = njsc32_read_2(sc, NJSC32_REG_IRQ)) &
    344 	    NJSC32_IRQ_INTR_PENDING) {
    345 		DPRINTF(("%s: njsc32_init: intr pending: %#x\n",
    346 		    sc->sc_dev.dv_xname, intstat));
    347 	}
    348 
    349 	/* FIFO threshold */
    350 	njsc32_ireg_write_1(sc, NJSC32_IREG_FIFO_THRESHOLD_FULL,
    351 	    NJSC32_FIFO_FULL_BUSMASTER);
    352 	njsc32_ireg_write_1(sc, NJSC32_IREG_FIFO_THRESHOLD_EMPTY,
    353 	    NJSC32_FIFO_EMPTY_BUSMASTER);
    354 
    355 	/* clock source */
    356 	njsc32_ireg_write_1(sc, NJSC32_IREG_CLOCK, sc->sc_clk);
    357 
    358 	/* memory read multiple */
    359 	njsc32_ireg_write_1(sc, NJSC32_IREG_BM,
    360 	    NJSC32_BM_MEMRD_CMD1 | NJSC32_BM_SGT_AUTO_PARA_MEMRD_CMD);
    361 
    362 	/* clear parity error and enable parity detection */
    363 	njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL,
    364 	    NJSC32_PARITYCTL_CHECK_ENABLE | NJSC32_PARITYCTL_CLEAR_ERROR);
    365 
    366 	/* misc configuration */
    367 	njsc32_ireg_write_2(sc, NJSC32_IREG_MISC,
    368 	    NJSC32_MISC_SCSI_DIRECTION_DETECTOR_SELECT |
    369 	    NJSC32_MISC_DELAYED_BMSTART |
    370 	    NJSC32_MISC_MASTER_TERMINATION_SELECT |
    371 	    NJSC32_MISC_BMREQ_NEGATE_TIMING_SEL |
    372 	    NJSC32_MISC_AUTOSEL_TIMING_SEL |
    373 	    NJSC32_MISC_BMSTOP_CHANGE2_NONDATA_PHASE);
    374 
    375 	/*
    376 	 * Check for termination power (32Bi only?).
    377 	 */
    378 	if (!nosleep || cold) {
    379 		DPRINTF(("%s: njsc32_init: checking TERMPWR\n",
    380 		    sc->sc_dev.dv_xname));
    381 
    382 		/* First, turn termination power off */
    383 		njsc32_ireg_write_1(sc, NJSC32_IREG_TERM_PWR, 0);
    384 
    385 		/* give 0.5s to settle */
    386 		if (nosleep)
    387 			delay(500000);
    388 		else
    389 			tsleep(sc, PWAIT, "njs_t1", hz / 2);
    390 	}
    391 
    392 	/* supply termination power if not supplied by other devices */
    393 	if ((njsc32_ireg_read_1(sc, NJSC32_IREG_TERM_PWR) &
    394 	    NJSC32_TERMPWR_SENSE) == 0) {
    395 		/* termination power is not present on the bus */
    396 		if (sc->sc_flags & NJSC32_CANNOT_SUPPLY_TERMPWR) {
    397 			/*
    398 			 * CardBus device must not supply termination power
    399 			 * to avoid excessive power consumption.
    400 			 */
    401 			printf("%s: no termination power present\n",
    402 			    sc->sc_dev.dv_xname);
    403 		} else {
    404 			/* supply termination power */
    405 			njsc32_ireg_write_1(sc, NJSC32_IREG_TERM_PWR,
    406 			    NJSC32_TERMPWR_BPWR);
    407 
    408 			DPRINTF(("%s: supplying termination power\n",
    409 			    sc->sc_dev.dv_xname));
    410 
    411 			/* give 0.5s to settle */
    412 			if (!nosleep)
    413 				tsleep(sc, PWAIT, "njs_t2", hz / 2);
    414 		}
    415 	}
    416 
    417 	/* stop timer */
    418 	njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP);
    419 	njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP);
    420 
    421 	/* default transfer parameter */
    422 	njsc32_write_1(sc, NJSC32_REG_SYNC, 0);
    423 	njsc32_write_1(sc, NJSC32_REG_ACK_WIDTH, NJSC32_ACK_WIDTH_1CLK);
    424 	njsc32_write_2(sc, NJSC32_REG_SEL_TIMEOUT,
    425 	    NJSC32_SEL_TIMEOUT_TIME);
    426 
    427 	/* select interrupt source */
    428 	njsc32_ireg_write_2(sc, NJSC32_IREG_IRQ_SELECT,
    429 	    NJSC32_IRQSEL_RESELECT |
    430 	    NJSC32_IRQSEL_PHASE_CHANGE |
    431 	    NJSC32_IRQSEL_SCSIRESET |
    432 	    NJSC32_IRQSEL_TIMER |
    433 	    NJSC32_IRQSEL_FIFO_THRESHOLD |
    434 	    NJSC32_IRQSEL_TARGET_ABORT |
    435 	    NJSC32_IRQSEL_MASTER_ABORT |
    436 	/* XXX not yet
    437 	    NJSC32_IRQSEL_SERR |
    438 	    NJSC32_IRQSEL_PERR |
    439 	    NJSC32_IRQSEL_BMCNTERR |
    440 	*/
    441 	    NJSC32_IRQSEL_AUTO_SCSI_SEQ);
    442 
    443 	/* unblock interrupts */
    444 	njsc32_write_2(sc, NJSC32_REG_IRQ, 0);
    445 
    446 	/* turn LED off */
    447 	njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT_DDR,
    448 	    NJSC32_EXTPORT_LED_OFF);
    449 	njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT,
    450 	    NJSC32_EXTPORT_LED_OFF);
    451 
    452 	/* reset SCSI bus so the targets become known state */
    453 	njsc32_reset_bus(sc);
    454 }
    455 
    456 static int
    457 njsc32_init_cmds(struct njsc32_softc *sc)
    458 {
    459 	struct njsc32_cmd *cmd;
    460 	bus_addr_t dmaaddr;
    461 	int i, error;
    462 
    463 	/*
    464 	 * allocate DMA area for command
    465 	 */
    466 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    467 	    sizeof(struct njsc32_dma_page), PAGE_SIZE, 0,
    468 	    &sc->sc_cmdpg_seg, 1, &sc->sc_cmdpg_nsegs, BUS_DMA_NOWAIT)) != 0) {
    469 		printf("%s: unable to allocate cmd page, error = %d\n",
    470 		    sc->sc_dev.dv_xname, error);
    471 		return 0;
    472 	}
    473 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cmdpg_seg,
    474 	    sc->sc_cmdpg_nsegs, sizeof(struct njsc32_dma_page),
    475 	    (caddr_t *)&sc->sc_cmdpg,
    476 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    477 		printf("%s: unable to map cmd page, error = %d\n",
    478 		    sc->sc_dev.dv_xname, error);
    479 		goto fail1;
    480 	}
    481 	if ((error = bus_dmamap_create(sc->sc_dmat,
    482 	    sizeof(struct njsc32_dma_page), 1,
    483 	    sizeof(struct njsc32_dma_page), 0, BUS_DMA_NOWAIT,
    484 	    &sc->sc_dmamap_cmdpg)) != 0) {
    485 		printf("%s: unable to create cmd DMA map, error = %d\n",
    486 		    sc->sc_dev.dv_xname, error);
    487 		goto fail2;
    488 	}
    489 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_cmdpg,
    490 	    sc->sc_cmdpg, sizeof(struct njsc32_dma_page),
    491 	    NULL, BUS_DMA_NOWAIT)) != 0) {
    492 		printf("%s: unable to load cmd DMA map, error = %d\n",
    493 		    sc->sc_dev.dv_xname, error);
    494 		goto fail3;
    495 	}
    496 
    497 	memset(sc->sc_cmdpg, 0, sizeof(struct njsc32_dma_page));
    498 	dmaaddr = sc->sc_dmamap_cmdpg->dm_segs[0].ds_addr;
    499 
    500 #ifdef NJSC32_AUTOPARAM
    501 	sc->sc_ap_dma = dmaaddr + offsetof(struct njsc32_dma_page, dp_ap);
    502 #endif
    503 
    504 	for (i = 0; i < NJSC32_NUM_CMD; i++) {
    505 		cmd = &sc->sc_cmds[i];
    506 		cmd->c_sc = sc;
    507 		cmd->c_sgt = sc->sc_cmdpg->dp_sg[i];
    508 		cmd->c_sgt_dma = dmaaddr +
    509 		    offsetof(struct njsc32_dma_page, dp_sg[i]);
    510 		cmd->c_flags = 0;
    511 
    512 		error = bus_dmamap_create(sc->sc_dmat,
    513 		    NJSC32_MAX_XFER,		/* max total map size */
    514 		    NJSC32_NUM_SG,		/* max number of segments */
    515 		    NJSC32_SGT_MAXSEGLEN,	/* max size of a segment */
    516 		    0,				/* boundary */
    517 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &cmd->c_dmamap_xfer);
    518 		if (error) {
    519 			printf("%s: only %d cmd descs available (error = %d)\n",
    520 			    sc->sc_dev.dv_xname, i, error);
    521 			break;
    522 		}
    523 		TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmd, c_q);
    524 	}
    525 
    526 	if (i > 0)
    527 		return i;
    528 
    529 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_cmdpg);
    530 fail3:	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_cmdpg);
    531 fail2:	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_cmdpg,
    532 	    sizeof(struct njsc32_dma_page));
    533 fail1:	bus_dmamem_free(sc->sc_dmat, &sc->sc_cmdpg_seg, sc->sc_cmdpg_nsegs);
    534 
    535 	return 0;
    536 }
    537 
    538 static void
    539 njsc32_target_async(struct njsc32_softc *sc, struct njsc32_target *target)
    540 {
    541 
    542 	target->t_sync =
    543 	    NJSC32_SYNC_VAL(sc->sc_sync_max, NJSC32_SYNCOFFSET_ASYNC);
    544 	target->t_ackwidth = NJSC32_ACK_WIDTH_1CLK;
    545 	target->t_sample = 0;		/* disable */
    546 	target->t_syncoffset = NJSC32_SYNCOFFSET_ASYNC;
    547 	target->t_syncperiod = NJSC32_SYNCPERIOD_ASYNC;
    548 }
    549 
    550 static void
    551 njsc32_init_targets(struct njsc32_softc *sc)
    552 {
    553 	int id, lun;
    554 	struct njsc32_lu *lu;
    555 
    556 	for (id = 0; id <= NJSC32_MAX_TARGET_ID; id++) {
    557 		/* cancel negotiation status */
    558 		sc->sc_targets[id].t_state = NJSC32_TARST_INIT;
    559 
    560 		/* default to async mode */
    561 		njsc32_target_async(sc, &sc->sc_targets[id]);
    562 
    563 #ifdef NJSC32_DUALEDGE
    564 		sc->sc_targets[id].t_xferctl = 0;
    565 #endif
    566 
    567 		sc->sc_targets[id].t_targetid =
    568 		    (1 << id) | (1 << NJSC32_INITIATOR_ID);
    569 
    570 		/* init logical units */
    571 		for (lun = 0; lun < NJSC32_NLU; lun++) {
    572 			lu = &sc->sc_targets[id].t_lus[lun];
    573 			lu->lu_cmd = NULL;
    574 			TAILQ_INIT(&lu->lu_q);
    575 		}
    576 	}
    577 }
    578 
    579 void
    580 njsc32_attach(struct njsc32_softc *sc)
    581 {
    582 	const char *str;
    583 #if 1	/* test */
    584 	int reg;
    585 	njsc32_model_t detected_model;
    586 #endif
    587 
    588 	/* init */
    589 	TAILQ_INIT(&sc->sc_freecmd);
    590 	TAILQ_INIT(&sc->sc_reqcmd);
    591 
    592 #if 1	/* test */
    593 	/*
    594 	 * try to distinguish 32Bi and 32UDE
    595 	 */
    596 	/* try to set DualEdge bit (exists on 32UDE only) and read it back */
    597 	njsc32_write_2(sc, NJSC32_REG_TRANSFER, NJSC32_XFR_DUALEDGE_ENABLE);
    598 	if ((reg = njsc32_read_2(sc, NJSC32_REG_TRANSFER)) == 0xffff) {
    599 		/* device was removed? */
    600 		aprint_error("%s: attach failed\n", sc->sc_dev.dv_xname);
    601 		return;
    602 	} else if (reg & NJSC32_XFR_DUALEDGE_ENABLE) {
    603 		detected_model = NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE;
    604 	} else {
    605 		detected_model = NJSC32_MODEL_32BI;
    606 	}
    607 	njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0);	/* restore */
    608 
    609 #if 1/*def DIAGNOSTIC*/
    610 	/* compare what is configured with what is detected */
    611 	if ((sc->sc_model & NJSC32_MODEL_MASK) !=
    612 	    (detected_model & NJSC32_MODEL_MASK)) {
    613 		/*
    614 		 * Please report this error if it happens.
    615 		 */
    616 		aprint_error("%s: model mismatch: %#x vs %#x\n",
    617 		    sc->sc_dev.dv_xname, sc->sc_model, detected_model);
    618 		return;
    619 	}
    620 #endif
    621 #endif
    622 
    623 	/* check model */
    624 	switch (sc->sc_model & NJSC32_MODEL_MASK) {
    625 	case NJSC32_MODEL_32BI:
    626 		str = "Bi";
    627 		/* 32Bi doesn't support DualEdge transfer */
    628 		KASSERT((sc->sc_model & NJSC32_FLAG_DUALEDGE) == 0);
    629 		break;
    630 	case NJSC32_MODEL_32UDE:
    631 		str = "UDE";
    632 		break;
    633 	default:
    634 		aprint_error("%s: unknown model!\n", sc->sc_dev.dv_xname);
    635 		return;
    636 	}
    637 	aprint_normal("%s: NJSC-32%s", sc->sc_dev.dv_xname, str);
    638 
    639 	switch (sc->sc_clk) {
    640 	default:
    641 #ifdef DIAGNOSTIC
    642 		panic("njsc32_attach: unknown clk %d", sc->sc_clk);
    643 #endif
    644 	case NJSC32_CLOCK_DIV_4:
    645 		sc->sc_synct = njsc32_synct_40M;
    646 		str = "40MHz";
    647 		break;
    648 #ifdef NJSC32_SUPPORT_OTHER_CLOCKS
    649 	case NJSC32_CLOCK_DIV_2:
    650 		sc->sc_synct = njsc32_synct_20M;
    651 		str = "20MHz";
    652 		break;
    653 	case NJSC32_CLOCK_PCICLK:
    654 		sc->sc_synct = njsc32_synct_pci;
    655 		str = "PCI";
    656 		break;
    657 #endif
    658 	}
    659 	aprint_normal(", G/A rev %#x, clk %s%s\n",
    660 	    NJSC32_INDEX_GAREV(njsc32_read_2(sc, NJSC32_REG_INDEX)), str,
    661 	    (sc->sc_model & NJSC32_FLAG_DUALEDGE) ?
    662 #ifdef NJSC32_DUALEDGE
    663 		", DualEdge"
    664 #else
    665 		", DualEdge (no driver support)"
    666 #endif
    667 	    : "");
    668 
    669 	/* allocate DMA resource */
    670 	if ((sc->sc_ncmd = njsc32_init_cmds(sc)) == 0) {
    671 		printf("%s: no usable DMA map\n", sc->sc_dev.dv_xname);
    672 		return;
    673 	}
    674 	sc->sc_flags |= NJSC32_CMDPG_MAPPED;
    675 
    676 	sc->sc_curcmd = NULL;
    677 	sc->sc_nusedcmds = 0;
    678 	sc->sc_stat = NJSC32_STAT_IDLE;
    679 
    680 	sc->sc_sync_max = 1;	/* XXX look up EEPROM configuration? */
    681 
    682 	/* initialize target structure */
    683 	njsc32_init_targets(sc);
    684 
    685 	/* initialize hardware */
    686 	njsc32_init(sc, cold);
    687 
    688 	/* setup adapter */
    689 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    690 	sc->sc_adapter.adapt_nchannels = 1;
    691 	sc->sc_adapter.adapt_request = njsc32_scsipi_request;
    692 	sc->sc_adapter.adapt_minphys = njsc32_scsipi_minphys;
    693 	sc->sc_adapter.adapt_ioctl = njsc32_scsipi_ioctl;
    694 
    695 	sc->sc_adapter.adapt_max_periph = sc->sc_adapter.adapt_openings =
    696 	    sc->sc_ncmd;
    697 
    698 	/* setup channel */
    699 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    700 	sc->sc_channel.chan_bustype = &scsi_bustype;
    701 	sc->sc_channel.chan_channel = 0;
    702 	sc->sc_channel.chan_ntargets = NJSC32_NTARGET;
    703 	sc->sc_channel.chan_nluns = NJSC32_NLU;
    704 	sc->sc_channel.chan_id = NJSC32_INITIATOR_ID;
    705 
    706 	sc->sc_scsi = config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    707 }
    708 
    709 int
    710 njsc32_detach(struct njsc32_softc *sc, int flags)
    711 {
    712 	int rv = 0;
    713 	int i, s;
    714 	struct njsc32_cmd *cmd;
    715 
    716 	s = splbio();
    717 
    718 	/* clear running/disconnected commands */
    719 	njsc32_clear_cmds(sc, XS_DRIVER_STUFFUP);
    720 
    721 	sc->sc_stat = NJSC32_STAT_DETACH;
    722 
    723 	/* clear pending commands */
    724 	while ((cmd = TAILQ_FIRST(&sc->sc_reqcmd)) != NULL) {
    725 		TAILQ_REMOVE(&sc->sc_reqcmd, cmd, c_q);
    726 		njsc32_end_cmd(sc, cmd, XS_RESET);
    727 	}
    728 
    729 	if (sc->sc_scsi != NULL)
    730 		rv = config_detach(sc->sc_scsi, flags);
    731 
    732 	splx(s);
    733 
    734 	/* free DMA resource */
    735 	if (sc->sc_flags & NJSC32_CMDPG_MAPPED) {
    736 		for (i = 0; i < sc->sc_ncmd; i++) {
    737 			cmd = &sc->sc_cmds[i];
    738 			if (cmd->c_flags & NJSC32_CMD_DMA_MAPPED)
    739 				bus_dmamap_unload(sc->sc_dmat,
    740 				    cmd->c_dmamap_xfer);
    741 			bus_dmamap_destroy(sc->sc_dmat, cmd->c_dmamap_xfer);
    742 		}
    743 
    744 		bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_cmdpg);
    745 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_cmdpg);
    746 		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_cmdpg,
    747 		    sizeof(struct njsc32_dma_page));
    748 		bus_dmamem_free(sc->sc_dmat, &sc->sc_cmdpg_seg,
    749 		    sc->sc_cmdpg_nsegs);
    750 	}
    751 
    752 	return 0;
    753 }
    754 
    755 static inline void
    756 njsc32_cmd_init(struct njsc32_cmd *cmd)
    757 {
    758 
    759 	cmd->c_flags = 0;
    760 
    761 	/* scatter/gather table */
    762 	cmd->c_sgtdmaaddr = NJSC32_CMD_DMAADDR_SGT(cmd, 0);
    763 	cmd->c_sgoffset = 0;
    764 	cmd->c_sgfixcnt = 0;
    765 
    766 	/* data pointer */
    767 	cmd->c_dp_cur = cmd->c_dp_saved = cmd->c_dp_max = 0;
    768 }
    769 
    770 static inline void
    771 njsc32_init_msgout(struct njsc32_softc *sc)
    772 {
    773 
    774 	sc->sc_msgoutlen = 0;
    775 	sc->sc_msgoutidx = 0;
    776 }
    777 
    778 static void
    779 njsc32_add_msgout(struct njsc32_softc *sc, int byte)
    780 {
    781 
    782 	if (sc->sc_msgoutlen >= NJSC32_MSGOUT_LEN) {
    783 		printf("njsc32_add_msgout: too many\n");
    784 		return;
    785 	}
    786 	sc->sc_msgout[sc->sc_msgoutlen++] = byte;
    787 }
    788 
    789 static u_int32_t
    790 njsc32_get_auto_msgout(struct njsc32_softc *sc)
    791 {
    792 	u_int32_t val;
    793 	u_int8_t *p;
    794 
    795 	val = 0;
    796 	p = sc->sc_msgout;
    797 	switch (sc->sc_msgoutlen) {
    798 		/* 31-24 23-16 15-8 7 ... 1 0 */
    799 	case 3:	/* MSG3  MSG2  MSG1 V --- cnt */
    800 		val |= *p++ << NJSC32_MSGOUT_MSG1_SHIFT;
    801 		/* FALLTHROUGH */
    802 
    803 	case 2:	/* MSG2  MSG1  ---  V --- cnt */
    804 		val |= *p++ << NJSC32_MSGOUT_MSG2_SHIFT;
    805 		/* FALLTHROUGH */
    806 
    807 	case 1:	/* MSG1  ---   ---  V --- cnt */
    808 		val |= *p++ << NJSC32_MSGOUT_MSG3_SHIFT;
    809 		val |= NJSC32_MSGOUT_VALID | sc->sc_msgoutlen;
    810 		break;
    811 
    812 	default:
    813 		break;
    814 	}
    815 	return val;
    816 }
    817 
    818 #ifdef NJSC32_DUALEDGE
    819 /* add Wide Data Transfer Request to the next Message Out */
    820 static void
    821 njsc32_msgout_wdtr(struct njsc32_softc *sc, int width)
    822 {
    823 
    824 	njsc32_add_msgout(sc, MSG_EXTENDED);
    825 	njsc32_add_msgout(sc, MSG_EXT_WDTR_LEN);
    826 	njsc32_add_msgout(sc, MSG_EXT_WDTR);
    827 	njsc32_add_msgout(sc, width);
    828 }
    829 #endif
    830 
    831 /* add Synchronous Data Transfer Request to the next Message Out */
    832 static void
    833 njsc32_msgout_sdtr(struct njsc32_softc *sc, int period, int offset)
    834 {
    835 
    836 	njsc32_add_msgout(sc, MSG_EXTENDED);
    837 	njsc32_add_msgout(sc, MSG_EXT_SDTR_LEN);
    838 	njsc32_add_msgout(sc, MSG_EXT_SDTR);
    839 	njsc32_add_msgout(sc, period);
    840 	njsc32_add_msgout(sc, offset);
    841 }
    842 
    843 static void
    844 njsc32_negotiate_xfer(struct njsc32_softc *sc, struct njsc32_target *target)
    845 {
    846 
    847 	/* initial negotiation state */
    848 	if (target->t_state == NJSC32_TARST_INIT) {
    849 #ifdef NJSC32_DUALEDGE
    850 		if (target->t_flags & NJSC32_TARF_DE)
    851 			target->t_state = NJSC32_TARST_DE;
    852 		else
    853 #endif
    854 		if (target->t_flags & NJSC32_TARF_SYNC)
    855 			target->t_state = NJSC32_TARST_SDTR;
    856 		else
    857 			target->t_state = NJSC32_TARST_DONE;
    858 	}
    859 
    860 	switch (target->t_state) {
    861 	default:
    862 	case NJSC32_TARST_INIT:
    863 #ifdef DIAGNOSTIC
    864 		panic("njsc32_negotiate_xfer");
    865 		/* NOTREACHED */
    866 #endif
    867 		/* FALLTHROUGH */
    868 	case NJSC32_TARST_DONE:
    869 		/* no more work */
    870 		break;
    871 
    872 #ifdef NJSC32_DUALEDGE
    873 	case NJSC32_TARST_DE:
    874 		njsc32_msgout_wdtr(sc, 0xde /* XXX? */);
    875 		break;
    876 
    877 	case NJSC32_TARST_WDTR:
    878 		njsc32_msgout_wdtr(sc, MSG_EXT_WDTR_BUS_8_BIT);
    879 		break;
    880 #endif
    881 
    882 	case NJSC32_TARST_SDTR:
    883 		njsc32_msgout_sdtr(sc, sc->sc_synct[sc->sc_sync_max].sp_period,
    884 		    NJSC32_SYNCOFFSET_MAX);
    885 		break;
    886 
    887 	case NJSC32_TARST_ASYNC:
    888 		njsc32_msgout_sdtr(sc, NJSC32_SYNCPERIOD_ASYNC,
    889 		    NJSC32_SYNCOFFSET_ASYNC);
    890 		break;
    891 	}
    892 }
    893 
    894 /* turn LED on */
    895 static inline void
    896 njsc32_led_on(struct njsc32_softc *sc)
    897 {
    898 
    899 	njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT, NJSC32_EXTPORT_LED_ON);
    900 }
    901 
    902 /* turn LED off */
    903 static inline void
    904 njsc32_led_off(struct njsc32_softc *sc)
    905 {
    906 
    907 	njsc32_ireg_write_1(sc, NJSC32_IREG_EXT_PORT, NJSC32_EXTPORT_LED_OFF);
    908 }
    909 
    910 static void
    911 njsc32_arbitration_failed(struct njsc32_softc *sc)
    912 {
    913 	struct njsc32_cmd *cmd;
    914 
    915 	if ((cmd = sc->sc_curcmd) == NULL || sc->sc_stat != NJSC32_STAT_ARBIT)
    916 		return;
    917 
    918 	if ((cmd->c_xs->xs_control & XS_CTL_POLL) == 0)
    919 		callout_stop(&cmd->c_xs->xs_callout);
    920 
    921 	sc->sc_stat = NJSC32_STAT_IDLE;
    922 	sc->sc_curcmd = NULL;
    923 
    924 	/* the command is no longer active */
    925 	if (--sc->sc_nusedcmds == 0)
    926 		njsc32_led_off(sc);
    927 }
    928 
    929 static inline void
    930 njsc32_cmd_load(struct njsc32_softc *sc, struct njsc32_cmd *cmd)
    931 {
    932 	struct njsc32_target *target;
    933 	struct scsipi_xfer *xs;
    934 	int i, control, lun;
    935 	u_int32_t msgoutreg;
    936 #ifdef NJSC32_AUTOPARAM
    937 	struct njsc32_autoparam *ap;
    938 #endif
    939 
    940 	xs = cmd->c_xs;
    941 #ifdef NJSC32_AUTOPARAM
    942 	ap = &sc->sc_cmdpg->dp_ap;
    943 #else
    944 	/* reset CDB pointer */
    945 	njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, NJSC32_CMD_CLEAR_CDB_FIFO_PTR);
    946 #endif
    947 
    948 	/* CDB */
    949 	TPRINTC(cmd, ("njsc32_cmd_load: CDB"));
    950 	for (i = 0; i < xs->cmdlen; i++) {
    951 #ifdef NJSC32_AUTOPARAM
    952 		ap->ap_cdb[i].cdb_data = ((u_int8_t *)xs->cmd)[i];
    953 #else
    954 		njsc32_write_1(sc, NJSC32_REG_COMMAND_DATA,
    955 		    ((u_int8_t *)xs->cmd)[i]);
    956 #endif
    957 		TPRINTF((" %02x", ((u_int8_t *)cmd->c_xs->cmd)[i]));
    958 	}
    959 #ifdef NJSC32_AUTOPARAM	/* XXX needed? */
    960 	for ( ; i < NJSC32_AUTOPARAM_CDBLEN; i++)
    961 		ap->ap_cdb[i].cdb_data = 0;
    962 #endif
    963 
    964 	control = xs->xs_control;
    965 
    966 	/*
    967 	 * Message Out
    968 	 */
    969 	njsc32_init_msgout(sc);
    970 
    971 	/* Identify */
    972 	lun = xs->xs_periph->periph_lun;
    973 	njsc32_add_msgout(sc, (control & XS_CTL_REQSENSE) ?
    974 	    MSG_IDENTIFY(lun, 0) : MSG_IDENTIFY(lun, 1));
    975 
    976 	/* tagged queueing */
    977 	if (control & XS_CTL_TAGMASK) {
    978 		njsc32_add_msgout(sc, xs->xs_tag_type);
    979 		njsc32_add_msgout(sc, xs->xs_tag_id);
    980 		TPRINTF((" (tag %#x %#x)\n", xs->xs_tag_type, xs->xs_tag_id));
    981 	}
    982 	TPRINTF(("\n"));
    983 
    984 	target = cmd->c_target;
    985 
    986 	/* transfer negotiation */
    987 	if (control & XS_CTL_REQSENSE)
    988 		target->t_state = NJSC32_TARST_INIT;
    989 	njsc32_negotiate_xfer(sc, target);
    990 
    991 	msgoutreg = njsc32_get_auto_msgout(sc);
    992 
    993 #ifdef NJSC32_AUTOPARAM
    994 	ap->ap_msgout = htole32(msgoutreg);
    995 
    996 	ap->ap_sync	= target->t_sync;
    997 	ap->ap_ackwidth	= target->t_ackwidth;
    998 	ap->ap_targetid	= target->t_targetid;
    999 	ap->ap_sample	= target->t_sample;
   1000 
   1001 	ap->ap_cmdctl = htole16(NJSC32_CMD_CLEAR_CDB_FIFO_PTR |
   1002 	    NJSC32_CMD_AUTO_COMMAND_PHASE |
   1003 	    NJSC32_CMD_AUTO_SCSI_START | NJSC32_CMD_AUTO_ATN |
   1004 	    NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02);
   1005 #ifdef NJSC32_DUALEDGE
   1006 	ap->ap_xferctl = htole16(cmd->c_xferctl | target->t_xferctl);
   1007 #else
   1008 	ap->ap_xferctl = htole16(cmd->c_xferctl);
   1009 #endif
   1010 	ap->ap_sgtdmaaddr = htole32(cmd->c_sgtdmaaddr);
   1011 
   1012 	/* sync njsc32_autoparam */
   1013 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg,
   1014 	    offsetof(struct njsc32_dma_page, dp_ap),	/* offset */
   1015 	    sizeof(struct njsc32_autoparam),
   1016 	    BUS_DMASYNC_PREWRITE);
   1017 
   1018 	/* autoparam DMA address */
   1019 	njsc32_write_4(sc, NJSC32_REG_SGT_ADR, sc->sc_ap_dma);
   1020 
   1021 	/* start command (autoparam) */
   1022 	njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL,
   1023 	    NJSC32_CMD_CLEAR_CDB_FIFO_PTR | NJSC32_CMD_AUTO_PARAMETER);
   1024 
   1025 #else	/* not NJSC32_AUTOPARAM */
   1026 
   1027 	njsc32_write_4(sc, NJSC32_REG_SCSI_MSG_OUT, msgoutreg);
   1028 
   1029 	/* load parameters */
   1030 	njsc32_write_1(sc, NJSC32_REG_TARGET_ID, target->t_targetid);
   1031 	njsc32_write_1(sc, NJSC32_REG_SYNC, target->t_sync);
   1032 	njsc32_write_1(sc, NJSC32_REG_ACK_WIDTH, target->t_ackwidth);
   1033 	njsc32_write_1(sc, NJSC32_REG_SREQ_SAMPLING, target->t_sample);
   1034 	njsc32_write_4(sc, NJSC32_REG_SGT_ADR, cmd->c_sgtdmaaddr);
   1035 #ifdef NJSC32_DUALEDGE
   1036 	njsc32_write_2(sc, NJSC32_REG_TRANSFER,
   1037 	    cmd->c_xferctl | target->t_xferctl);
   1038 #else
   1039 	njsc32_write_2(sc, NJSC32_REG_TRANSFER, cmd->c_xferctl);
   1040 #endif
   1041 	/* start AutoSCSI */
   1042 	njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL,
   1043 	    NJSC32_CMD_CLEAR_CDB_FIFO_PTR | NJSC32_CMD_AUTO_COMMAND_PHASE |
   1044 	    NJSC32_CMD_AUTO_SCSI_START | NJSC32_CMD_AUTO_ATN |
   1045 	    NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02);
   1046 #endif	/* not NJSC32_AUTOPARAM */
   1047 }
   1048 
   1049 /* Note: must be called at splbio() */
   1050 static void
   1051 njsc32_start(struct njsc32_softc *sc)
   1052 {
   1053 	struct njsc32_cmd *cmd;
   1054 
   1055 	/* get a command to issue */
   1056 	TAILQ_FOREACH(cmd, &sc->sc_reqcmd, c_q) {
   1057 		if (cmd->c_lu->lu_cmd == NULL &&
   1058 		    ((cmd->c_flags & NJSC32_CMD_TAGGED) ||
   1059 		     TAILQ_EMPTY(&cmd->c_lu->lu_q)))
   1060 			break;	/* OK, the logical unit is free */
   1061 	}
   1062 	if (!cmd)
   1063 		goto out;	/* no work to do */
   1064 
   1065 	/* request will always fail if not in bus free phase */
   1066 	if (njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) !=
   1067 	    NJSC32_BUSMON_BUSFREE)
   1068 		goto busy;
   1069 
   1070 	/* clear parity error and enable parity detection */
   1071 	njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL,
   1072 	    NJSC32_PARITYCTL_CHECK_ENABLE | NJSC32_PARITYCTL_CLEAR_ERROR);
   1073 
   1074 	njsc32_cmd_load(sc, cmd);
   1075 
   1076 	if (sc->sc_nusedcmds++ == 0)
   1077 		njsc32_led_on(sc);
   1078 
   1079 	sc->sc_curcmd = cmd;
   1080 	sc->sc_stat = NJSC32_STAT_ARBIT;
   1081 
   1082 	if ((cmd->c_xs->xs_control & XS_CTL_POLL) == 0) {
   1083 		callout_reset(&cmd->c_xs->xs_callout,
   1084 		    mstohz(cmd->c_xs->timeout),
   1085 		    njsc32_cmdtimeout, cmd);
   1086 	}
   1087 
   1088 	return;
   1089 
   1090 busy:	/* XXX retry counter */
   1091 	TPRINTF(("%s: njsc32_start: busy\n", sc->sc_dev.dv_xname));
   1092 	njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_ARBITRATION_RETRY_TIME);
   1093 out:	njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0);
   1094 }
   1095 
   1096 static void
   1097 njsc32_run_xfer(struct njsc32_softc *sc, struct scsipi_xfer *xs)
   1098 {
   1099 	struct scsipi_periph *periph;
   1100 	int control;
   1101 	int lun;
   1102 	struct njsc32_cmd *cmd;
   1103 	int s, i, error;
   1104 
   1105 	periph = xs->xs_periph;
   1106 	KASSERT((unsigned)periph->periph_target <= NJSC32_MAX_TARGET_ID);
   1107 
   1108 	control = xs->xs_control;
   1109 	lun = periph->periph_lun;
   1110 
   1111 	/*
   1112 	 * get a free cmd
   1113 	 * (scsipi layer knows the number of cmds, so this shall never fail)
   1114 	 */
   1115 	s = splbio();
   1116 	cmd = TAILQ_FIRST(&sc->sc_freecmd);
   1117 	KASSERT(cmd);
   1118 	TAILQ_REMOVE(&sc->sc_freecmd, cmd, c_q);
   1119 	splx(s);
   1120 
   1121 	/*
   1122 	 * build a request
   1123 	 */
   1124 	njsc32_cmd_init(cmd);
   1125 	cmd->c_xs = xs;
   1126 	cmd->c_target = &sc->sc_targets[periph->periph_target];
   1127 	cmd->c_lu = &cmd->c_target->t_lus[lun];
   1128 
   1129 	/* tagged queueing */
   1130 	if (control & XS_CTL_TAGMASK) {
   1131 		cmd->c_flags |= NJSC32_CMD_TAGGED;
   1132 		if (control & XS_CTL_HEAD_TAG)
   1133 			cmd->c_flags |= NJSC32_CMD_TAGGED_HEAD;
   1134 	}
   1135 
   1136 	/* map DMA buffer */
   1137 	cmd->c_datacnt = xs->datalen;
   1138 	if (xs->datalen) {
   1139 		/* Is XS_CTL_DATA_UIO ever used anywhere? */
   1140 		KASSERT((control & XS_CTL_DATA_UIO) == 0);
   1141 
   1142 		error = bus_dmamap_load(sc->sc_dmat, cmd->c_dmamap_xfer,
   1143 		    xs->data, xs->datalen, NULL,
   1144 		    ((control & XS_CTL_NOSLEEP) ?
   1145 			BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
   1146 		    BUS_DMA_STREAMING |
   1147 		    ((control & XS_CTL_DATA_IN) ?
   1148 			BUS_DMA_READ : BUS_DMA_WRITE));
   1149 
   1150 		switch (error) {
   1151 		case 0:
   1152 			break;
   1153 		case ENOMEM:
   1154 		case EAGAIN:
   1155 			xs->error = XS_RESOURCE_SHORTAGE;
   1156 			goto map_failed;
   1157 		default:
   1158 			xs->error = XS_DRIVER_STUFFUP;
   1159 		map_failed:
   1160 			printf("%s: njsc32_run_xfer: map failed, error %d\n",
   1161 			    sc->sc_dev.dv_xname, error);
   1162 			/* put it back to free command list */
   1163 			s = splbio();
   1164 			TAILQ_INSERT_HEAD(&sc->sc_freecmd, cmd, c_q);
   1165 			splx(s);
   1166 			/* abort this transfer */
   1167 			scsipi_done(xs);
   1168 			return;
   1169 		}
   1170 
   1171 		bus_dmamap_sync(sc->sc_dmat, cmd->c_dmamap_xfer,
   1172 		    0, cmd->c_dmamap_xfer->dm_mapsize,
   1173 		    (control & XS_CTL_DATA_IN) ?
   1174 			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1175 
   1176 		for (i = 0; i < cmd->c_dmamap_xfer->dm_nsegs; i++) {
   1177 			cmd->c_sgt[i].sg_addr =
   1178 			    htole32(cmd->c_dmamap_xfer->dm_segs[i].ds_addr);
   1179 			cmd->c_sgt[i].sg_len =
   1180 			    htole32(cmd->c_dmamap_xfer->dm_segs[i].ds_len);
   1181 		}
   1182 		/* end mark */
   1183 		cmd->c_sgt[i - 1].sg_len |= htole32(NJSC32_SGT_ENDMARK);
   1184 
   1185 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg,
   1186 		    (char *)cmd->c_sgt - (char *)sc->sc_cmdpg, /* offset */
   1187 		    NJSC32_SIZE_SGT,
   1188 		    BUS_DMASYNC_PREWRITE);
   1189 
   1190 		cmd->c_flags |= NJSC32_CMD_DMA_MAPPED;
   1191 
   1192 		/* enable transfer */
   1193 		cmd->c_xferctl =
   1194 		    NJSC32_XFR_TRANSFER_GO | NJSC32_XFR_BM_START |
   1195 		    NJSC32_XFR_ALL_COUNT_CLR;
   1196 
   1197 		/* XXX How can we specify the DMA direction? */
   1198 
   1199 #if 0	/* faster write mode? (doesn't work) */
   1200 		if ((control & XS_CTL_DATA_IN) == 0)
   1201 			cmd->c_xferctl |= NJSC32_XFR_ADVANCED_BM_WRITE;
   1202 #endif
   1203 	} else {
   1204 		/* no data transfer */
   1205 		cmd->c_xferctl = 0;
   1206 	}
   1207 
   1208 	/* queue request */
   1209 	s = splbio();
   1210 	TAILQ_INSERT_TAIL(&sc->sc_reqcmd, cmd, c_q);
   1211 
   1212 	/* start the controller if idle */
   1213 	if (sc->sc_stat == NJSC32_STAT_IDLE)
   1214 		njsc32_start(sc);
   1215 
   1216 	splx(s);
   1217 
   1218 	if (control & XS_CTL_POLL) {
   1219 		/* wait for completion */
   1220 		/* XXX should handle timeout? */
   1221 		while ((xs->xs_status & XS_STS_DONE) == 0) {
   1222 			delay(1000);
   1223 			njsc32_intr(sc);
   1224 		}
   1225 	}
   1226 }
   1227 
   1228 static void
   1229 njsc32_end_cmd(struct njsc32_softc *sc, struct njsc32_cmd *cmd,
   1230     scsipi_xfer_result_t result)
   1231 {
   1232 	struct scsipi_xfer *xs;
   1233 	int s;
   1234 #ifdef DIAGNOSTIC
   1235 	struct njsc32_cmd *c;
   1236 #endif
   1237 
   1238 	KASSERT(cmd);
   1239 
   1240 #ifdef DIAGNOSTIC
   1241 	s = splbio();
   1242 	TAILQ_FOREACH(c, &sc->sc_freecmd, c_q) {
   1243 		if (cmd == c)
   1244 			panic("njsc32_end_cmd: already in free list");
   1245 	}
   1246 	splx(s);
   1247 #endif
   1248 	xs = cmd->c_xs;
   1249 
   1250 	if (cmd->c_flags & NJSC32_CMD_DMA_MAPPED) {
   1251 		if (cmd->c_datacnt) {
   1252 			bus_dmamap_sync(sc->sc_dmat, cmd->c_dmamap_xfer,
   1253 			    0, cmd->c_dmamap_xfer->dm_mapsize,
   1254 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   1255 				BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1256 
   1257 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg,
   1258 			    (char *)cmd->c_sgt - (char *)sc->sc_cmdpg,
   1259 			    NJSC32_SIZE_SGT, BUS_DMASYNC_POSTWRITE);
   1260 		}
   1261 
   1262 		bus_dmamap_unload(sc->sc_dmat, cmd->c_dmamap_xfer);
   1263 		cmd->c_flags &= ~NJSC32_CMD_DMA_MAPPED;
   1264 	}
   1265 
   1266 	s = splbio();
   1267 	if ((xs->xs_control & XS_CTL_POLL) == 0)
   1268 		callout_stop(&xs->xs_callout);
   1269 
   1270 	TAILQ_INSERT_HEAD(&sc->sc_freecmd, cmd, c_q);
   1271 	splx(s);
   1272 
   1273 	xs->error = result;
   1274 	scsipi_done(xs);
   1275 
   1276 	if (--sc->sc_nusedcmds == 0)
   1277 		njsc32_led_off(sc);
   1278 }
   1279 
   1280 /*
   1281  * request from scsipi layer
   1282  */
   1283 static void
   1284 njsc32_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
   1285     void *arg)
   1286 {
   1287 	struct njsc32_softc *sc;
   1288 	struct scsipi_xfer_mode *xm;
   1289 	struct njsc32_target *target;
   1290 
   1291 	sc = (void *)chan->chan_adapter->adapt_dev;
   1292 
   1293 	switch (req) {
   1294 	case ADAPTER_REQ_RUN_XFER:
   1295 		njsc32_run_xfer(sc, arg);
   1296 		break;
   1297 
   1298 	case ADAPTER_REQ_GROW_RESOURCES:
   1299 		/* not supported */
   1300 		break;
   1301 
   1302 	case ADAPTER_REQ_SET_XFER_MODE:
   1303 		xm = arg;
   1304 		target = &sc->sc_targets[xm->xm_target];
   1305 
   1306 		target->t_flags = 0;
   1307 		if (xm->xm_mode & PERIPH_CAP_TQING)
   1308 			target->t_flags |= NJSC32_TARF_TAG;
   1309 		if (xm->xm_mode & PERIPH_CAP_SYNC) {
   1310 			target->t_flags |= NJSC32_TARF_SYNC;
   1311 #ifdef NJSC32_DUALEDGE
   1312 			if (sc->sc_model & NJSC32_FLAG_DUALEDGE)
   1313 				target->t_flags |= NJSC32_TARF_DE;
   1314 #endif
   1315 		}
   1316 #ifdef NJSC32_DUALEDGE
   1317 		target->t_xferctl = 0;
   1318 #endif
   1319 		target->t_state = NJSC32_TARST_INIT;
   1320 		njsc32_target_async(sc, target);
   1321 
   1322 		break;
   1323 	default:
   1324 		break;
   1325 	}
   1326 }
   1327 
   1328 static void
   1329 njsc32_scsipi_minphys(struct buf *bp)
   1330 {
   1331 
   1332 	if (bp->b_bcount > NJSC32_MAX_XFER)
   1333 		bp->b_bcount = NJSC32_MAX_XFER;
   1334 	minphys(bp);
   1335 }
   1336 
   1337 static void
   1338 njsc32_reset_bus(struct njsc32_softc *sc)
   1339 {
   1340 	int s;
   1341 
   1342 	DPRINTF(("%s: njsc32_reset_bus:\n", sc->sc_dev.dv_xname));
   1343 
   1344 	/* SCSI bus reset */
   1345 	njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, NJSC32_SBCTL_RST);
   1346 	delay(NJSC32_RESET_HOLD_TIME);
   1347 	njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, 0);
   1348 
   1349 	/* clear transfer */
   1350 	s = splbio();
   1351 	njsc32_reset_detected(sc);
   1352 	splx(s);
   1353 }
   1354 
   1355 /*
   1356  * clear running/disconnected commands
   1357  */
   1358 static void
   1359 njsc32_clear_cmds(struct njsc32_softc *sc, scsipi_xfer_result_t cmdresult)
   1360 {
   1361 	struct njsc32_cmd *cmd;
   1362 	int id, lun;
   1363 	struct njsc32_lu *lu;
   1364 
   1365 	njsc32_arbitration_failed(sc);
   1366 
   1367 	/* clear current transfer */
   1368 	if ((cmd = sc->sc_curcmd) != NULL) {
   1369 		sc->sc_curcmd = NULL;
   1370 		njsc32_end_cmd(sc, cmd, cmdresult);
   1371 	}
   1372 
   1373 	/* clear disconnected transfers */
   1374 	for (id = 0; id <= NJSC32_MAX_TARGET_ID; id++) {
   1375 		for (lun = 0; lun < NJSC32_NLU; lun++) {
   1376 			lu = &sc->sc_targets[id].t_lus[lun];
   1377 
   1378 			if ((cmd = lu->lu_cmd) != NULL) {
   1379 				lu->lu_cmd = NULL;
   1380 				njsc32_end_cmd(sc, cmd, cmdresult);
   1381 			}
   1382 			while ((cmd = TAILQ_FIRST(&lu->lu_q)) != NULL) {
   1383 				TAILQ_REMOVE(&lu->lu_q, cmd, c_q);
   1384 				njsc32_end_cmd(sc, cmd, cmdresult);
   1385 			}
   1386 		}
   1387 	}
   1388 }
   1389 
   1390 static void
   1391 njsc32_reset_detected(struct njsc32_softc *sc)
   1392 {
   1393 
   1394 	njsc32_clear_cmds(sc, XS_RESET);
   1395 	njsc32_init_targets(sc);
   1396 	sc->sc_stat = NJSC32_STAT_IDLE;
   1397 	KASSERT(sc->sc_nusedcmds == 0);
   1398 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_RESET, NULL);
   1399 }
   1400 
   1401 static int
   1402 njsc32_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, caddr_t addr,
   1403     int flag, struct proc *p)
   1404 {
   1405 	struct njsc32_softc *sc = (void *)chan->chan_adapter->adapt_dev;
   1406 
   1407 	switch (cmd) {
   1408 	case SCBUSIORESET:
   1409 		njsc32_init(sc, 0);
   1410 		return 0;
   1411 	default:
   1412 		break;
   1413 	}
   1414 
   1415 	return ENOTTY;
   1416 }
   1417 
   1418 /*
   1419  * set current data pointer
   1420  */
   1421 static inline void
   1422 njsc32_set_cur_ptr(struct njsc32_cmd *cmd, u_int32_t pos)
   1423 {
   1424 
   1425 	/* new current data pointer */
   1426 	cmd->c_dp_cur = pos;
   1427 
   1428 	/* update number of bytes transferred */
   1429 	if (pos > cmd->c_dp_max)
   1430 		cmd->c_dp_max = pos;
   1431 }
   1432 
   1433 /*
   1434  * set data pointer for the next transfer
   1435  */
   1436 static void
   1437 njsc32_set_ptr(struct njsc32_softc *sc, struct njsc32_cmd *cmd, u_int32_t pos)
   1438 {
   1439 	struct njsc32_sgtable *sg;
   1440 	unsigned sgte;
   1441 	u_int32_t len;
   1442 
   1443 	/* set current pointer */
   1444 	njsc32_set_cur_ptr(cmd, pos);
   1445 
   1446 	/* undo previous fix if any */
   1447 	if (cmd->c_sgfixcnt != 0) {
   1448 		sg = &cmd->c_sgt[cmd->c_sgoffset];
   1449 		sg->sg_addr = htole32(le32toh(sg->sg_addr) - cmd->c_sgfixcnt);
   1450 		sg->sg_len = htole32(le32toh(sg->sg_len) + cmd->c_sgfixcnt);
   1451 		cmd->c_sgfixcnt = 0;
   1452 	}
   1453 
   1454 	if (pos >= cmd->c_datacnt) {
   1455 		/* transfer done */
   1456 #if 1 /*def DIAGNOSTIC*/
   1457 		if (pos > cmd->c_datacnt)
   1458 			printf("%s: pos %u too large\n",
   1459 			    sc->sc_dev.dv_xname, pos - cmd->c_datacnt);
   1460 #endif
   1461 		cmd->c_xferctl = 0;	/* XXX correct? */
   1462 
   1463 		return;
   1464 	}
   1465 
   1466 	for (sgte = 0, sg = cmd->c_sgt;
   1467 	    sgte < NJSC32_NUM_SG && pos > 0; sgte++, sg++) {
   1468 		len = le32toh(sg->sg_len) & ~NJSC32_SGT_ENDMARK;
   1469 		if (pos < len) {
   1470 			sg->sg_addr = htole32(le32toh(sg->sg_addr) + pos);
   1471 			sg->sg_len = htole32(le32toh(sg->sg_len) - pos);
   1472 			cmd->c_sgfixcnt = pos;
   1473 			break;
   1474 		}
   1475 		pos -= len;
   1476 #ifdef DIAGNOSTIC
   1477 		if (sg->sg_len & htole32(NJSC32_SGT_ENDMARK)) {
   1478 			panic("njsc32_set_ptr: bad pos");
   1479 		}
   1480 #endif
   1481 	}
   1482 #ifdef DIAGNOSTIC
   1483 	if (sgte >= NJSC32_NUM_SG)
   1484 		panic("njsc32_set_ptr: bad sg");
   1485 #endif
   1486 	if (cmd->c_sgoffset != sgte) {
   1487 		cmd->c_sgoffset = sgte;
   1488 		cmd->c_sgtdmaaddr = NJSC32_CMD_DMAADDR_SGT(cmd, sgte);
   1489 	}
   1490 
   1491 	/* XXX overkill */
   1492 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_cmdpg,
   1493 	    (char *)cmd->c_sgt - (char *)sc->sc_cmdpg,	/* offset */
   1494 	    NJSC32_SIZE_SGT,
   1495 	    BUS_DMASYNC_PREWRITE);
   1496 }
   1497 
   1498 /*
   1499  * save data pointer
   1500  */
   1501 static inline void
   1502 njsc32_save_ptr(struct njsc32_cmd *cmd)
   1503 {
   1504 
   1505 	cmd->c_dp_saved = cmd->c_dp_cur;
   1506 }
   1507 
   1508 static void
   1509 njsc32_assert_ack(struct njsc32_softc *sc)
   1510 {
   1511 	u_int8_t reg;
   1512 
   1513 	reg = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_CONTROL);
   1514 	reg |= NJSC32_SBCTL_ACK | NJSC32_SBCTL_ACK_ENABLE;
   1515 #if 0	/* needed? */
   1516 	reg |= NJSC32_SBCTL_AUTODIRECTION;
   1517 #endif
   1518 	njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, reg);
   1519 }
   1520 
   1521 static void
   1522 njsc32_negate_ack(struct njsc32_softc *sc)
   1523 {
   1524 	u_int8_t reg;
   1525 
   1526 	reg = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_CONTROL);
   1527 #if 0	/* needed? */
   1528 	reg |= NJSC32_SBCTL_ACK_ENABLE;
   1529 	reg |= NJSC32_SBCTL_AUTODIRECTION;
   1530 #endif
   1531 	reg &= ~NJSC32_SBCTL_ACK;
   1532 	njsc32_write_1(sc, NJSC32_REG_SCSI_BUS_CONTROL, reg);
   1533 }
   1534 
   1535 static void
   1536 njsc32_wait_req_negate(struct njsc32_softc *sc)
   1537 {
   1538 	int cnt;
   1539 
   1540 	for (cnt = 0; cnt < NJSC32_REQ_TIMEOUT; cnt++) {
   1541 		if ((njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) &
   1542 		    NJSC32_BUSMON_REQ) == 0)
   1543 			return;
   1544 		delay(1);
   1545 	}
   1546 	printf("%s: njsc32_wait_req_negate: timed out\n", sc->sc_dev.dv_xname);
   1547 }
   1548 
   1549 static void
   1550 njsc32_reconnect(struct njsc32_softc *sc, struct njsc32_cmd *cmd)
   1551 {
   1552 	struct scsipi_xfer *xs;
   1553 
   1554 	xs = cmd->c_xs;
   1555 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
   1556 		callout_stop(&xs->xs_callout);
   1557 		callout_reset(&xs->xs_callout,
   1558 		    mstohz(xs->timeout),
   1559 		    njsc32_cmdtimeout, cmd);
   1560 	}
   1561 
   1562 	/* Reconnection implies Restore Pointers */
   1563 	njsc32_set_ptr(sc, cmd, cmd->c_dp_saved);
   1564 }
   1565 
   1566 static enum njsc32_reselstat
   1567 njsc32_resel_identify(struct njsc32_softc *sc, int lun,
   1568     struct njsc32_cmd **pcmd)
   1569 {
   1570 	int targetid;
   1571 	struct njsc32_lu *plu;
   1572 	struct njsc32_cmd *cmd;
   1573 
   1574 	switch (sc->sc_stat) {
   1575 	case NJSC32_STAT_RESEL:
   1576 		break;	/* OK */
   1577 
   1578 	case NJSC32_STAT_RESEL_LUN:
   1579 	case NJSC32_STAT_RECONNECT:
   1580 		/*
   1581 		 * accept and ignore if the LUN is the same as the current one,
   1582 		 * reject otherwise.
   1583 		 */
   1584 		return sc->sc_resellun == lun ?
   1585 		    NJSC32_RESEL_THROUGH : NJSC32_RESEL_ERROR;
   1586 
   1587 	default:
   1588 		printf("%s: njsc32_resel_identify: not in reselection\n",
   1589 		    sc->sc_dev.dv_xname);
   1590 		return NJSC32_RESEL_ERROR;
   1591 	}
   1592 
   1593 	targetid = sc->sc_reselid;
   1594 	TPRINTF(("%s: njsc32_resel_identify: reselection lun %d\n",
   1595 	    sc->sc_dev.dv_xname, lun));
   1596 
   1597 	if (targetid > NJSC32_MAX_TARGET_ID || lun >= NJSC32_NLU)
   1598 		return NJSC32_RESEL_ERROR;
   1599 
   1600 	sc->sc_resellun = lun;
   1601 	plu = &sc->sc_targets[targetid].t_lus[lun];
   1602 
   1603 	if ((cmd = plu->lu_cmd) != NULL) {
   1604 		sc->sc_stat = NJSC32_STAT_RECONNECT;
   1605 		plu->lu_cmd = NULL;
   1606 		*pcmd = cmd;
   1607 		TPRINTC(cmd, ("njsc32_resel_identify: I_T_L nexus\n"));
   1608 		njsc32_reconnect(sc, cmd);
   1609 		return NJSC32_RESEL_COMPLETE;
   1610 	} else if (!TAILQ_EMPTY(&plu->lu_q)) {
   1611 		/* wait for tag */
   1612 		sc->sc_stat = NJSC32_STAT_RESEL_LUN;
   1613 		return NJSC32_RESEL_THROUGH;
   1614 	}
   1615 
   1616 	/* no disconnected commands */
   1617 	return NJSC32_RESEL_ERROR;
   1618 }
   1619 
   1620 static enum njsc32_reselstat
   1621 njsc32_resel_tag(struct njsc32_softc *sc, int tag, struct njsc32_cmd **pcmd)
   1622 {
   1623 	struct njsc32_cmd_head *head;
   1624 	struct njsc32_cmd *cmd;
   1625 
   1626 	TPRINTF(("%s: njsc32_resel_tag: reselection tag %d\n",
   1627 	    sc->sc_dev.dv_xname, tag));
   1628 	if (sc->sc_stat != NJSC32_STAT_RESEL_LUN)
   1629 		return NJSC32_RESEL_ERROR;
   1630 
   1631 	head = &sc->sc_targets[sc->sc_reselid].t_lus[sc->sc_resellun].lu_q;
   1632 
   1633 	/* XXX slow? */
   1634 	/* search for the command of the tag */
   1635 	TAILQ_FOREACH(cmd, head, c_q) {
   1636 		if (cmd->c_xs->xs_tag_id == tag) {
   1637 			sc->sc_stat = NJSC32_STAT_RECONNECT;
   1638 			TAILQ_REMOVE(head, cmd, c_q);
   1639 			*pcmd = cmd;
   1640 			TPRINTC(cmd, ("njsc32_resel_tag: I_T_L_Q nexus\n"));
   1641 			njsc32_reconnect(sc, cmd);
   1642 			return NJSC32_RESEL_COMPLETE;
   1643 		}
   1644 	}
   1645 
   1646 	/* no disconnected commands */
   1647 	return NJSC32_RESEL_ERROR;
   1648 }
   1649 
   1650 /*
   1651  * Reload parameters and restart AutoSCSI.
   1652  *
   1653  * XXX autoparam doesn't work as expected and we can't use it here.
   1654  */
   1655 static void
   1656 njsc32_cmd_reload(struct njsc32_softc *sc, struct njsc32_cmd *cmd, int cctl)
   1657 {
   1658 	struct njsc32_target *target;
   1659 
   1660 	target = cmd->c_target;
   1661 
   1662 	/* clear parity error and enable parity detection */
   1663 	njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL,
   1664 	    NJSC32_PARITYCTL_CHECK_ENABLE | NJSC32_PARITYCTL_CLEAR_ERROR);
   1665 
   1666 	/* load parameters */
   1667 	njsc32_write_1(sc, NJSC32_REG_SYNC, target->t_sync);
   1668 	njsc32_write_1(sc, NJSC32_REG_ACK_WIDTH, target->t_ackwidth);
   1669 	njsc32_write_1(sc, NJSC32_REG_SREQ_SAMPLING, target->t_sample);
   1670 	njsc32_write_4(sc, NJSC32_REG_SGT_ADR, cmd->c_sgtdmaaddr);
   1671 #ifdef NJSC32_DUALEDGE
   1672 	njsc32_write_2(sc, NJSC32_REG_TRANSFER,
   1673 	    cmd->c_xferctl | target->t_xferctl);
   1674 #else
   1675 	njsc32_write_2(sc, NJSC32_REG_TRANSFER, cmd->c_xferctl);
   1676 #endif
   1677 	/* start AutoSCSI */
   1678 	njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl);
   1679 
   1680 	sc->sc_curcmd = cmd;
   1681 }
   1682 
   1683 static void
   1684 njsc32_update_xfer_mode(struct njsc32_softc *sc, struct njsc32_target *target)
   1685 {
   1686 	struct scsipi_xfer_mode xm;
   1687 
   1688 	xm.xm_target = target - sc->sc_targets;	/* target ID */
   1689 	xm.xm_mode = 0;
   1690 	xm.xm_period = target->t_syncperiod;
   1691 	xm.xm_offset = target->t_syncoffset;
   1692 	if (xm.xm_offset != 0)
   1693 		xm.xm_mode |= PERIPH_CAP_SYNC;
   1694 	if (target->t_flags & NJSC32_TARF_TAG)
   1695 		xm.xm_mode |= PERIPH_CAP_TQING;
   1696 
   1697 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
   1698 }
   1699 
   1700 static void
   1701 njsc32_msgin(struct njsc32_softc *sc)
   1702 {
   1703 	u_int8_t msg0, msg;
   1704 	int msgcnt;
   1705 	struct njsc32_cmd *cmd;
   1706 	enum njsc32_reselstat rstat;
   1707 	int cctl = 0;
   1708 	u_int32_t ptr;	/* unsigned type ensures 2-complement calculation */
   1709 	u_int32_t msgout = 0;
   1710 	boolean_t reload_params = FALSE;
   1711 	struct njsc32_target *target;
   1712 	int idx, period, offset;
   1713 
   1714 	/*
   1715 	 * we are in Message In, so the previous Message Out should have
   1716 	 * been done.
   1717 	 */
   1718 	njsc32_init_msgout(sc);
   1719 
   1720 	/* get a byte of Message In */
   1721 	msg = njsc32_read_1(sc, NJSC32_REG_DATA_IN);
   1722 	TPRINTF(("%s: njsc32_msgin: got %#x\n", sc->sc_dev.dv_xname, msg));
   1723 	if ((msgcnt = sc->sc_msgincnt) < NJSC32_MSGIN_LEN)
   1724 		sc->sc_msginbuf[sc->sc_msgincnt] = msg;
   1725 
   1726 	njsc32_assert_ack(sc);
   1727 
   1728 	msg0 = sc->sc_msginbuf[0];
   1729 	cmd = sc->sc_curcmd;
   1730 
   1731 	/* check for parity error */
   1732 	if (njsc32_read_1(sc, NJSC32_REG_PARITY_STATUS) &
   1733 	    NJSC32_PARITYSTATUS_ERROR_LSB) {
   1734 
   1735 		printf("%s: msgin: parity error\n", sc->sc_dev.dv_xname);
   1736 
   1737 		/* clear parity error */
   1738 		njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL,
   1739 		    NJSC32_PARITYCTL_CHECK_ENABLE |
   1740 		    NJSC32_PARITYCTL_CLEAR_ERROR);
   1741 
   1742 		/* respond as Message Parity Error */
   1743 		njsc32_add_msgout(sc, MSG_PARITY_ERROR);
   1744 
   1745 		/* clear Message In */
   1746 		sc->sc_msgincnt = 0;
   1747 		goto reply;
   1748 	}
   1749 
   1750 #define WAITNEXTMSG	do { sc->sc_msgincnt++; goto restart; } while (0)
   1751 #define MSGCOMPLETE	do { sc->sc_msgincnt = 0; goto restart; } while (0)
   1752 	if (MSG_ISIDENTIFY(msg0)) {
   1753 		/*
   1754 		 * Got Identify message from target.
   1755 		 */
   1756 		if ((msg0 & ~MSG_IDENTIFY_LUNMASK) != MSG_IDENTIFYFLAG ||
   1757 		    (rstat = njsc32_resel_identify(sc, msg0 &
   1758 			MSG_IDENTIFY_LUNMASK, &cmd)) == NJSC32_RESEL_ERROR) {
   1759 			/*
   1760 			 * invalid Identify -> Reject
   1761 			 */
   1762 			goto reject;
   1763 		}
   1764 		if (rstat == NJSC32_RESEL_COMPLETE)
   1765 			reload_params = TRUE;
   1766 		MSGCOMPLETE;
   1767 	}
   1768 
   1769 	if (msg0 == MSG_SIMPLE_Q_TAG) {
   1770 		if (msgcnt == 0)
   1771 			WAITNEXTMSG;
   1772 
   1773 		/* got whole message */
   1774 		sc->sc_msgincnt = 0;
   1775 
   1776 		if ((rstat = njsc32_resel_tag(sc, sc->sc_msginbuf[1], &cmd))
   1777 		    == NJSC32_RESEL_ERROR) {
   1778 			/*
   1779 			 * invalid Simple Queue Tag -> Abort Tag
   1780 			 */
   1781 			printf("%s: msgin: invalid tag\n", sc->sc_dev.dv_xname);
   1782 			njsc32_add_msgout(sc, MSG_ABORT_TAG);
   1783 			goto reply;
   1784 		}
   1785 		if (rstat == NJSC32_RESEL_COMPLETE)
   1786 			reload_params = TRUE;
   1787 		MSGCOMPLETE;
   1788 	}
   1789 
   1790 	/* I_T_L or I_T_L_Q nexus should be established now */
   1791 	if (cmd == NULL) {
   1792 		printf("%s: msgin %#x without nexus -- sending abort\n",
   1793 		    sc->sc_dev.dv_xname, msg0);
   1794 		njsc32_add_msgout(sc, MSG_ABORT);
   1795 		goto reply;
   1796 	}
   1797 
   1798 	/*
   1799 	 * extended message
   1800 	 * 0x01 <length (0 stands for 256)> <length bytes>
   1801 	 *                                 (<code> [<parameter> ...])
   1802 	 */
   1803 #define EXTLENOFF	1
   1804 #define EXTCODEOFF	2
   1805 	if (msg0 == MSG_EXTENDED) {
   1806 		if (msgcnt < EXTLENOFF ||
   1807 		    msgcnt < EXTLENOFF + 1 +
   1808 		    (u_int8_t)(sc->sc_msginbuf[EXTLENOFF] - 1))
   1809 			WAITNEXTMSG;
   1810 
   1811 		/* got whole message */
   1812 		sc->sc_msgincnt = 0;
   1813 
   1814 		switch (sc->sc_msginbuf[EXTCODEOFF]) {
   1815 		case 0:	/* Modify Data Pointer */
   1816 			if (msgcnt != 5 + EXTCODEOFF - 1)
   1817 				break;
   1818 			/*
   1819 			 * parameter is 32bit big-endian signed (2-complement)
   1820 			 * value
   1821 			 */
   1822 			ptr = (sc->sc_msginbuf[EXTCODEOFF + 1] << 24) |
   1823 			      (sc->sc_msginbuf[EXTCODEOFF + 2] << 16) |
   1824 			      (sc->sc_msginbuf[EXTCODEOFF + 3] << 8) |
   1825 			      sc->sc_msginbuf[EXTCODEOFF + 4];
   1826 
   1827 			/* new pointer */
   1828 			ptr += cmd->c_dp_cur;	/* ignore overflow */
   1829 
   1830 			/* reject if ptr is not in data buffer */
   1831 			if (ptr > cmd->c_datacnt)
   1832 				break;
   1833 
   1834 			njsc32_set_ptr(sc, cmd, ptr);
   1835 			goto restart;
   1836 
   1837 		case MSG_EXT_SDTR:	/* Synchronous Data Transfer Request */
   1838 			DPRINTC(cmd, ("SDTR %#x %#x\n",
   1839 			    sc->sc_msginbuf[EXTCODEOFF + 1],
   1840 			    sc->sc_msginbuf[EXTCODEOFF + 2]));
   1841 			if (msgcnt != MSG_EXT_SDTR_LEN + EXTCODEOFF-1)
   1842 				break;	/* reject */
   1843 
   1844 			target = cmd->c_target;
   1845 
   1846 			/* lookup sync period parameters */
   1847 			period = sc->sc_msginbuf[EXTCODEOFF + 1];
   1848 			for (idx = sc->sc_sync_max; idx < NJSC32_NSYNCT; idx++)
   1849 				if (sc->sc_synct[idx].sp_period >= period) {
   1850 					period = sc->sc_synct[idx].sp_period;
   1851 					break;
   1852 				}
   1853 			if (idx >= NJSC32_NSYNCT) {
   1854 				/*
   1855 				 * We can't meet the timing condition that
   1856 				 * the target requests -- use async.
   1857 				 */
   1858 				njsc32_target_async(sc, target);
   1859 				njsc32_update_xfer_mode(sc, target);
   1860 				if (target->t_state == NJSC32_TARST_SDTR) {
   1861 					/*
   1862 					 * We started SDTR exchange -- start
   1863 					 * negotiation again and request async.
   1864 					 */
   1865 					target->t_state = NJSC32_TARST_ASYNC;
   1866 					njsc32_negotiate_xfer(sc, target);
   1867 					goto reply;
   1868 				} else {
   1869 					/*
   1870 					 * The target started SDTR exchange
   1871 					 * -- just reject and fallback
   1872 					 * to async.
   1873 					 */
   1874 					goto reject;
   1875 				}
   1876 			}
   1877 
   1878 			/* check sync offset */
   1879 			offset = sc->sc_msginbuf[EXTCODEOFF + 2];
   1880 			if (offset > NJSC32_SYNCOFFSET_MAX) {
   1881 				if (target->t_state == NJSC32_TARST_SDTR) {
   1882 					printf("%s: wrong sync offset: %d\n",
   1883 					    cmd->c_xs->xs_periph->periph_dev->dv_xname,
   1884 					    offset);
   1885 					/* XXX what to do? */
   1886 				}
   1887 				offset = NJSC32_SYNCOFFSET_MAX;
   1888 			}
   1889 
   1890 			target->t_ackwidth = sc->sc_synct[idx].sp_ackw;
   1891 			target->t_sample   = sc->sc_synct[idx].sp_sample;
   1892 			target->t_syncperiod = period;
   1893 			target->t_syncoffset = offset;
   1894 			target->t_sync = NJSC32_SYNC_VAL(idx, offset);
   1895 			njsc32_update_xfer_mode(sc, target);
   1896 
   1897 			if (target->t_state == NJSC32_TARST_SDTR) {
   1898 				target->t_state = NJSC32_TARST_DONE;
   1899 			} else {
   1900 				njsc32_msgout_sdtr(sc, period, offset);
   1901 				goto reply;
   1902 			}
   1903 			goto restart;
   1904 
   1905 		case MSG_EXT_WDTR:	/* Wide Data Transfer Request */
   1906 			DPRINTC(cmd,
   1907 			    ("WDTR %#x\n", sc->sc_msginbuf[EXTCODEOFF + 1]));
   1908 #ifdef NJSC32_DUALEDGE
   1909 			if (msgcnt != MSG_EXT_WDTR_LEN + EXTCODEOFF-1)
   1910 				break;	/* reject */
   1911 
   1912 			/*
   1913 			 * T->I of this message is not used for
   1914 			 * DualEdge negotiation, so the device
   1915 			 * must not be a DualEdge device.
   1916 			 *
   1917 			 * XXX correct?
   1918 			 */
   1919 			target = cmd->c_target;
   1920 			target->t_xferctl = 0;
   1921 
   1922 			switch (target->t_state) {
   1923 			case NJSC32_TARST_DE:
   1924 				if (sc->sc_msginbuf[EXTCODEOFF + 1] !=
   1925 				    MSG_EXT_WDTR_BUS_8_BIT) {
   1926 					/*
   1927 					 * Oops, we got unexpected WDTR.
   1928 					 * Negotiate for 8bit.
   1929 					 */
   1930 					target->t_state = NJSC32_TARST_WDTR;
   1931 				} else {
   1932 					target->t_state = NJSC32_TARST_SDTR;
   1933 				}
   1934 				njsc32_negotiate_xfer(sc, target);
   1935 				goto reply;
   1936 
   1937 			case NJSC32_TARST_WDTR:
   1938 				if (sc->sc_msginbuf[EXTCODEOFF + 1] !=
   1939 				    MSG_EXT_WDTR_BUS_8_BIT) {
   1940 					printf("%s: unexpected transfer width: %#x\n",
   1941 					    cmd->c_xs->xs_periph->periph_dev->dv_xname,
   1942 					    sc->sc_msginbuf[EXTCODEOFF + 1]);
   1943 					/* XXX what to do? */
   1944 				}
   1945 				target->t_state = NJSC32_TARST_SDTR;
   1946 				njsc32_negotiate_xfer(sc, target);
   1947 				goto reply;
   1948 
   1949 			default:
   1950 				/* the target started WDTR exchange */
   1951 				DPRINTC(cmd, ("WDTR from target\n"));
   1952 
   1953 				target->t_state = NJSC32_TARST_SDTR;
   1954 				njsc32_target_async(sc, target);
   1955 
   1956 				break;	/* reject the WDTR (8bit transfer) */
   1957 			}
   1958 #endif	/* NJSC32_DUALEDGE */
   1959 			break;	/* reject */
   1960 		}
   1961 		DPRINTC(cmd, ("njsc32_msgin: reject ext msg %#x msgincnt %d\n",
   1962 		    sc->sc_msginbuf[EXTCODEOFF], msgcnt));
   1963 		goto reject;
   1964 	}
   1965 
   1966 	/* 2byte messages */
   1967 	if (MSG_IS2BYTE(msg0)) {
   1968 		if (msgcnt == 0)
   1969 			WAITNEXTMSG;
   1970 
   1971 		/* got whole message */
   1972 		sc->sc_msgincnt = 0;
   1973 	}
   1974 
   1975 	switch (msg0) {
   1976 	case MSG_CMDCOMPLETE:		/* 0x00 */
   1977 	case MSG_SAVEDATAPOINTER:	/* 0x02 */
   1978 	case MSG_DISCONNECT:		/* 0x04 */
   1979 		/* handled by AutoSCSI */
   1980 		PRINTC(cmd, ("msgin: unexpected msg: %#x\n", msg0));
   1981 		break;
   1982 
   1983 	case MSG_RESTOREPOINTERS:	/* 0x03 */
   1984 		/* restore data pointer to what was saved */
   1985 		DPRINTC(cmd, ("njsc32_msgin: Restore Pointers\n"));
   1986 		njsc32_set_ptr(sc, cmd, cmd->c_dp_saved);
   1987 		reload_params = TRUE;
   1988 		MSGCOMPLETE;
   1989 		/* NOTREACHED */
   1990 		break;
   1991 
   1992 #if 0	/* handled above */
   1993 	case MSG_EXTENDED:		/* 0x01 */
   1994 #endif
   1995 	case MSG_MESSAGE_REJECT:	/* 0x07 */
   1996 		target = cmd->c_target;
   1997 		DPRINTC(cmd, ("Reject tarst %d\n", target->t_state));
   1998 		switch (target->t_state) {
   1999 #ifdef NJSC32_DUALEDGE
   2000 		case NJSC32_TARST_WDTR:
   2001 		case NJSC32_TARST_DE:
   2002 			target->t_xferctl = 0;
   2003 			target->t_state = NJSC32_TARST_SDTR;
   2004 			njsc32_negotiate_xfer(sc, target);
   2005 			goto reply;
   2006 #endif
   2007 		case NJSC32_TARST_SDTR:
   2008 		case NJSC32_TARST_ASYNC:
   2009 			njsc32_target_async(sc, target);
   2010 			target->t_state = NJSC32_TARST_DONE;
   2011 			njsc32_update_xfer_mode(sc, target);
   2012 			break;
   2013 		default:
   2014 			break;
   2015 		}
   2016 		goto restart;
   2017 
   2018 	case MSG_NOOP:			/* 0x08 */
   2019 #ifdef NJSC32_DUALEDGE
   2020 		target = cmd->c_target;
   2021 		if (target->t_state == NJSC32_TARST_DE) {
   2022 			aprint_normal("%s: DualEdge transfer\n",
   2023 			    cmd->c_xs->xs_periph->periph_dev->dv_xname);
   2024 			target->t_xferctl = NJSC32_XFR_DUALEDGE_ENABLE;
   2025 			/* go to next negotiation */
   2026 			target->t_state = NJSC32_TARST_SDTR;
   2027 			njsc32_negotiate_xfer(sc, target);
   2028 			goto reply;
   2029 		}
   2030 #endif
   2031 		goto restart;
   2032 
   2033 	case MSG_INITIATOR_DET_ERR:	/* 0x05 I->T only */
   2034 	case MSG_ABORT:			/* 0x06 I->T only */
   2035 	case MSG_PARITY_ERROR:		/* 0x09 I->T only */
   2036 	case MSG_LINK_CMD_COMPLETE:	/* 0x0a */
   2037 	case MSG_LINK_CMD_COMPLETEF:	/* 0x0b */
   2038 	case MSG_BUS_DEV_RESET:		/* 0x0c I->T only */
   2039 	case MSG_ABORT_TAG:		/* 0x0d I->T only */
   2040 	case MSG_CLEAR_QUEUE:		/* 0x0e I->T only */
   2041 
   2042 #if 0	/* handled above */
   2043 	case MSG_SIMPLE_Q_TAG:		/* 0x20 */
   2044 #endif
   2045 	case MSG_HEAD_OF_Q_TAG:		/* 0x21 I->T only */
   2046 	case MSG_ORDERED_Q_TAG:		/* 0x22 I->T only */
   2047 	case MSG_IGN_WIDE_RESIDUE:	/* 0x23 */
   2048 
   2049 	default:
   2050 #ifdef NJSC32_DEBUG
   2051 		PRINTC(cmd, ("msgin: unsupported msg: %#x", msg0));
   2052 		if (MSG_IS2BYTE(msg0))
   2053 			printf(" %#x", msg);
   2054 		printf("\n");
   2055 #endif
   2056 		break;
   2057 	}
   2058 
   2059 reject:
   2060 	njsc32_add_msgout(sc, MSG_MESSAGE_REJECT);
   2061 
   2062 reply:
   2063 	msgout = njsc32_get_auto_msgout(sc);
   2064 
   2065 restart:
   2066 	cctl = NJSC32_CMD_CLEAR_CDB_FIFO_PTR |
   2067 	    NJSC32_CMD_AUTO_COMMAND_PHASE |
   2068 	    NJSC32_CMD_AUTO_SCSI_RESTART;
   2069 
   2070 	/*
   2071 	 * Be careful the second and latter bytes of Message In
   2072 	 * shall not be absorbed by AutoSCSI.
   2073 	 */
   2074 	if (sc->sc_msgincnt == 0)
   2075 		cctl |= NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02;
   2076 
   2077 	if (sc->sc_msgoutlen != 0)
   2078 		cctl |= NJSC32_CMD_AUTO_ATN;
   2079 
   2080 	njsc32_write_4(sc, NJSC32_REG_SCSI_MSG_OUT, msgout);
   2081 
   2082 	/* (re)start AutoSCSI (may assert ATN) */
   2083 	if (reload_params) {
   2084 		njsc32_cmd_reload(sc, cmd, cctl);
   2085 	} else {
   2086 		njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl);
   2087 	}
   2088 
   2089 	/* +ATN -> -REQ: need 90ns delay? */
   2090 
   2091 	njsc32_wait_req_negate(sc);	/* wait for REQ negation */
   2092 
   2093 	njsc32_negate_ack(sc);
   2094 
   2095 	return;
   2096 }
   2097 
   2098 static void
   2099 njsc32_msgout(struct njsc32_softc *sc)
   2100 {
   2101 	int cctl;
   2102 	u_int8_t bus;
   2103 	unsigned n;
   2104 
   2105 	if (sc->sc_msgoutlen == 0) {
   2106 		/* target entered to Message Out on unexpected timing */
   2107 		njsc32_add_msgout(sc, MSG_NOOP);
   2108 	}
   2109 
   2110 	cctl = NJSC32_CMD_CLEAR_CDB_FIFO_PTR |
   2111 	    NJSC32_CMD_AUTO_COMMAND_PHASE | NJSC32_CMD_AUTO_SCSI_RESTART |
   2112 	    NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02;
   2113 
   2114 	/* make sure target is in Message Out phase */
   2115 	bus = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR);
   2116 	if ((bus & NJSC32_BUSMON_PHASE_MASK) != NJSC32_PHASE_MESSAGE_OUT) {
   2117 		/*
   2118 		 * Message Out is aborted by target.
   2119 		 */
   2120 		printf("%s: njsc32_msgout: phase change %#x\n",
   2121 		    sc->sc_dev.dv_xname, bus);
   2122 
   2123 		/* XXX what to do? */
   2124 
   2125 		/* restart AutoSCSI (negate ATN) */
   2126 		njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl);
   2127 
   2128 		sc->sc_msgoutidx = 0;
   2129 		return;
   2130 	}
   2131 
   2132 	n = sc->sc_msgoutidx;
   2133 	if (n == sc->sc_msgoutlen - 1) {
   2134 		/*
   2135 		 * negate ATN before sending ACK
   2136 		 */
   2137 		njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, 0);
   2138 
   2139 		sc->sc_msgoutidx = 0;	/* target may retry Message Out */
   2140 	} else {
   2141 		cctl |= NJSC32_CMD_AUTO_ATN;
   2142 		sc->sc_msgoutidx++;
   2143 	}
   2144 
   2145 	/* Send Message Out */
   2146 	njsc32_write_1(sc, NJSC32_REG_SCSI_OUT_LATCH, sc->sc_msgout[n]);
   2147 
   2148 	/* DBn -> +ACK: need 55ns delay? */
   2149 
   2150 	njsc32_assert_ack(sc);
   2151 	njsc32_wait_req_negate(sc);	/* wait for REQ negation */
   2152 
   2153 	/* restart AutoSCSI */
   2154 	njsc32_write_2(sc, NJSC32_REG_COMMAND_CONTROL, cctl);
   2155 
   2156 	njsc32_negate_ack(sc);
   2157 
   2158 	/*
   2159 	 * do not reset sc->sc_msgoutlen so the target
   2160 	 * can retry Message Out phase
   2161 	 */
   2162 }
   2163 
   2164 static void
   2165 njsc32_cmdtimeout(void *arg)
   2166 {
   2167 	struct njsc32_cmd *cmd = arg;
   2168 	struct njsc32_softc *sc;
   2169 	int s;
   2170 
   2171 	PRINTC(cmd, ("command timeout\n"));
   2172 
   2173 	sc = cmd->c_sc;
   2174 
   2175 	s = splbio();
   2176 
   2177 	if (sc->sc_stat == NJSC32_STAT_ARBIT)
   2178 		njsc32_arbitration_failed(sc);
   2179 	else {
   2180 		sc->sc_curcmd = NULL;
   2181 		sc->sc_stat = NJSC32_STAT_IDLE;
   2182 		njsc32_end_cmd(sc, cmd, XS_TIMEOUT);
   2183 	}
   2184 
   2185 	/* XXX? */
   2186 	njsc32_init(sc, 1);	/* bus reset */
   2187 
   2188 	splx(s);
   2189 }
   2190 
   2191 static void
   2192 njsc32_reseltimeout(void *arg)
   2193 {
   2194 	struct njsc32_cmd *cmd = arg;
   2195 	struct njsc32_softc *sc;
   2196 	int s;
   2197 
   2198 	PRINTC(cmd, ("reselection timeout\n"));
   2199 
   2200 	sc = cmd->c_sc;
   2201 
   2202 	s = splbio();
   2203 
   2204 	/* remove from disconnected list */
   2205 	if (cmd->c_flags & NJSC32_CMD_TAGGED) {
   2206 		/* I_T_L_Q */
   2207 		KASSERT(cmd->c_lu->lu_cmd == NULL);
   2208 		TAILQ_REMOVE(&cmd->c_lu->lu_q, cmd, c_q);
   2209 	} else {
   2210 		/* I_T_L */
   2211 		KASSERT(cmd->c_lu->lu_cmd == cmd);
   2212 		cmd->c_lu->lu_cmd = NULL;
   2213 	}
   2214 
   2215 	njsc32_end_cmd(sc, cmd, XS_TIMEOUT);
   2216 
   2217 	/* XXX? */
   2218 	njsc32_init(sc, 1);	/* bus reset */
   2219 
   2220 	splx(s);
   2221 }
   2222 
   2223 static inline void
   2224 njsc32_end_auto(struct njsc32_softc *sc, struct njsc32_cmd *cmd, int auto_phase)
   2225 {
   2226 	struct scsipi_xfer *xs;
   2227 
   2228 	if (auto_phase & NJSC32_XPHASE_MSGIN_02) {
   2229 		/* Message In: 0x02 Save Data Pointer */
   2230 
   2231 		/*
   2232 		 * Adjust saved data pointer
   2233 		 * if the command is not completed yet.
   2234 		 */
   2235 		if ((auto_phase & NJSC32_XPHASE_MSGIN_00) == 0 &&
   2236 		    (auto_phase &
   2237 		     (NJSC32_XPHASE_DATA_IN | NJSC32_XPHASE_DATA_OUT)) != 0) {
   2238 			njsc32_save_ptr(cmd);
   2239 		}
   2240 		TPRINTF(("BM %u, SGT %u, SACK %u, SAVED_ACK %u\n",
   2241 		    njsc32_read_4(sc, NJSC32_REG_BM_CNT),
   2242 		    njsc32_read_4(sc, NJSC32_REG_SGT_ADR),
   2243 		    njsc32_read_4(sc, NJSC32_REG_SACK_CNT),
   2244 		    njsc32_read_4(sc, NJSC32_REG_SAVED_ACK_CNT)));
   2245 	}
   2246 
   2247 	xs = cmd->c_xs;
   2248 
   2249 	if (auto_phase & NJSC32_XPHASE_MSGIN_00) {
   2250 		/* Command Complete */
   2251 		TPRINTC(cmd, ("njsc32_intr: Command Complete\n"));
   2252 		switch (xs->status) {
   2253 		case SCSI_CHECK: case SCSI_QUEUE_FULL: case SCSI_BUSY:
   2254 			/*
   2255 			 * scsipi layer will automatically handle the error
   2256 			 */
   2257 			njsc32_end_cmd(sc, cmd, XS_BUSY);
   2258 			break;
   2259 		default:
   2260 			xs->resid -= cmd->c_dp_max;
   2261 			njsc32_end_cmd(sc, cmd, XS_NOERROR);
   2262 			break;
   2263 		}
   2264 	} else if (auto_phase & NJSC32_XPHASE_MSGIN_04) {
   2265 		/* Disconnect */
   2266 		TPRINTC(cmd, ("njsc32_intr: Disconnect\n"));
   2267 
   2268 		/* for ill-designed devices */
   2269 		if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) != 0)
   2270 			njsc32_save_ptr(cmd);
   2271 
   2272 		/*
   2273 		 * move current cmd to disconnected list
   2274 		 */
   2275 		if (cmd->c_flags & NJSC32_CMD_TAGGED) {
   2276 			/* I_T_L_Q */
   2277 			if (cmd->c_flags & NJSC32_CMD_TAGGED_HEAD)
   2278 				TAILQ_INSERT_HEAD(&cmd->c_lu->lu_q, cmd, c_q);
   2279 			else
   2280 				TAILQ_INSERT_TAIL(&cmd->c_lu->lu_q, cmd, c_q);
   2281 		} else {
   2282 			/* I_T_L */
   2283 			cmd->c_lu->lu_cmd = cmd;
   2284 		}
   2285 
   2286 		/*
   2287 		 * schedule timeout -- avoid being
   2288 		 * disconnected forever
   2289 		 */
   2290 		if ((xs->xs_control & XS_CTL_POLL) == 0) {
   2291 			callout_stop(&xs->xs_callout);
   2292 			callout_reset(&xs->xs_callout, mstohz(xs->timeout),
   2293 			    njsc32_reseltimeout, cmd);
   2294 		}
   2295 
   2296 	} else {
   2297 		/*
   2298 		 * target has come to Bus Free phase
   2299 		 * probably to notify an error
   2300 		 */
   2301 		PRINTC(cmd, ("njsc32_intr: unexpected bus free\n"));
   2302 		/* try Request Sense */
   2303 		xs->status = SCSI_CHECK;
   2304 		njsc32_end_cmd(sc, cmd, XS_BUSY);
   2305 	}
   2306 }
   2307 
   2308 int
   2309 njsc32_intr(void *arg)
   2310 {
   2311 	struct njsc32_softc *sc = arg;
   2312 	u_int16_t intr;
   2313 	u_int8_t arbstat, bus_phase;
   2314 	int auto_phase;
   2315 	int idbit;
   2316 	struct njsc32_cmd *cmd;
   2317 
   2318 	intr = njsc32_read_2(sc, NJSC32_REG_IRQ);
   2319 	if ((intr & NJSC32_IRQ_INTR_PENDING) == 0)
   2320 		return 0;	/* not mine */
   2321 
   2322 	TPRINTF(("%s: njsc32_intr: %#x\n", sc->sc_dev.dv_xname, intr));
   2323 
   2324 #if 0	/* I don't think this is required */
   2325 	/* mask interrupts */
   2326 	njsc32_write_2(sc, NJSC32_REG_IRQ, NJSC32_IRQ_MASK_ALL);
   2327 #endif
   2328 
   2329 	/* we got an interrupt, so stop the timer */
   2330 	njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP);
   2331 
   2332 	if (intr & NJSC32_IRQ_SCSIRESET) {
   2333 		printf("%s: detected bus reset\n", sc->sc_dev.dv_xname);
   2334 		/* clear current request */
   2335 		njsc32_reset_detected(sc);
   2336 		goto out;
   2337 	}
   2338 
   2339 	if (sc->sc_stat == NJSC32_STAT_ARBIT) {
   2340 		cmd = sc->sc_curcmd;
   2341 		KASSERT(cmd);
   2342 		arbstat = njsc32_read_1(sc, NJSC32_REG_ARBITRATION_STAT);
   2343 		if (arbstat & (NJSC32_ARBSTAT_WIN | NJSC32_ARBSTAT_FAIL)) {
   2344 			/*
   2345 			 * arbitration done
   2346 			 */
   2347 			/* clear arbitration status */
   2348 			njsc32_write_1(sc, NJSC32_REG_SET_ARBITRATION,
   2349 			    NJSC32_SETARB_CLEAR);
   2350 
   2351 			if (arbstat & NJSC32_ARBSTAT_WIN) {
   2352 				TPRINTC(cmd,
   2353 				    ("njsc32_intr: arbitration won\n"));
   2354 
   2355 				TAILQ_REMOVE(&sc->sc_reqcmd, cmd, c_q);
   2356 
   2357 				sc->sc_stat = NJSC32_STAT_CONNECT;
   2358 			} else {
   2359 				TPRINTC(cmd,
   2360 				    ("njsc32_intr: arbitration failed\n"));
   2361 
   2362 				njsc32_arbitration_failed(sc);
   2363 
   2364 				/* XXX delay */
   2365 				/* XXX retry counter */
   2366 			}
   2367 		}
   2368 	}
   2369 
   2370 	if (intr & NJSC32_IRQ_TIMER) {
   2371 		TPRINTF(("%s: njsc32_intr: timer interrupt\n",
   2372 		    sc->sc_dev.dv_xname));
   2373 	}
   2374 
   2375 	if (intr & NJSC32_IRQ_RESELECT) {
   2376 		/* Reselection from a target */
   2377 		njsc32_arbitration_failed(sc);	/* just in case */
   2378 		if ((cmd = sc->sc_curcmd) != NULL) {
   2379 			/* ? */
   2380 			printf("%s: unexpected reselection\n",
   2381 			    sc->sc_dev.dv_xname);
   2382 			sc->sc_curcmd = NULL;
   2383 			sc->sc_stat = NJSC32_STAT_IDLE;
   2384 			njsc32_end_cmd(sc, cmd, XS_DRIVER_STUFFUP);
   2385 		}
   2386 
   2387 		idbit = njsc32_read_1(sc, NJSC32_REG_RESELECT_ID);
   2388 		if ((idbit & (1 << NJSC32_INITIATOR_ID)) == 0 ||
   2389 		    (sc->sc_reselid = ffs(idbit & ~NJSC32_INITIATOR_ID) -1)
   2390 		    < 0) {
   2391 			printf("%s: invalid reselection (id: %#x)\n",
   2392 			    sc->sc_dev.dv_xname, idbit);
   2393 			sc->sc_stat = NJSC32_STAT_IDLE;	/* XXX ? */
   2394 		} else {
   2395 			sc->sc_stat = NJSC32_STAT_RESEL;
   2396 			TPRINTF(("%s: njsc32_intr: reselection from %d\n",
   2397 			    sc->sc_dev.dv_xname, sc->sc_reselid));
   2398 		}
   2399 	}
   2400 
   2401 	if (intr & NJSC32_IRQ_PHASE_CHANGE) {
   2402 #if 1	/* XXX probably not needed */
   2403 		if (sc->sc_stat == NJSC32_STAT_ARBIT)
   2404 			PRINTC(sc->sc_curcmd,
   2405 			    ("njsc32_intr: cancel arbitration phase\n"));
   2406 		njsc32_arbitration_failed(sc);
   2407 #endif
   2408 		/* current bus phase */
   2409 		bus_phase = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) &
   2410 		    NJSC32_BUSMON_PHASE_MASK;
   2411 
   2412 		switch (bus_phase) {
   2413 		case NJSC32_PHASE_MESSAGE_IN:
   2414 			njsc32_msgin(sc);
   2415 			break;
   2416 
   2417 		/*
   2418 		 * target may suddenly become Status / Bus Free phase
   2419 		 * to notify an error condition
   2420 		 */
   2421 		case NJSC32_PHASE_STATUS:
   2422 			printf("%s: unexpected bus phase: Status\n",
   2423 			    sc->sc_dev.dv_xname);
   2424 			if ((cmd = sc->sc_curcmd) != NULL) {
   2425 				cmd->c_xs->status =
   2426 				    njsc32_read_1(sc, NJSC32_REG_SCSI_CSB_IN);
   2427 				TPRINTC(cmd, ("njsc32_intr: Status %d\n",
   2428 				    cmd->c_xs->status));
   2429 			}
   2430 			break;
   2431 		case NJSC32_PHASE_BUSFREE:
   2432 			printf("%s: unexpected bus phase: Bus Free\n",
   2433 			    sc->sc_dev.dv_xname);
   2434 			if ((cmd = sc->sc_curcmd) != NULL) {
   2435 				sc->sc_curcmd = NULL;
   2436 				sc->sc_stat = NJSC32_STAT_IDLE;
   2437 				if (cmd->c_xs->status != SCSI_QUEUE_FULL &&
   2438 				    cmd->c_xs->status != SCSI_BUSY)
   2439 					cmd->c_xs->status = SCSI_CHECK;/* XXX */
   2440 				njsc32_end_cmd(sc, cmd, XS_BUSY);
   2441 			}
   2442 			goto out;
   2443 		default:
   2444 #ifdef NJSC32_DEBUG
   2445 			printf("%s: unexpected bus phase: ",
   2446 			    sc->sc_dev.dv_xname);
   2447 			switch (bus_phase) {
   2448 			case NJSC32_PHASE_COMMAND:
   2449 				printf("Command\n");	break;
   2450 			case NJSC32_PHASE_MESSAGE_OUT:
   2451 				printf("Message Out\n");break;
   2452 			case NJSC32_PHASE_DATA_IN:
   2453 				printf("Data In\n");	break;
   2454 			case NJSC32_PHASE_DATA_OUT:
   2455 				printf("Data Out\n");	break;
   2456 			case NJSC32_PHASE_RESELECT:
   2457 				printf("Reselect\n");break;
   2458 			default: printf("%#x\n", bus_phase);	break;
   2459 			}
   2460 #else
   2461 			printf("%s: unexpected bus phase: %#x",
   2462 			    sc->sc_dev.dv_xname, bus_phase);
   2463 #endif
   2464 			break;
   2465 		}
   2466 	}
   2467 
   2468 	if (intr & NJSC32_IRQ_AUTOSCSI) {
   2469 		/*
   2470 		 * AutoSCSI interrupt
   2471 		 */
   2472 		auto_phase = njsc32_read_2(sc, NJSC32_REG_EXECUTE_PHASE);
   2473 		TPRINTF(("%s: njsc32_intr: AutoSCSI: %#x\n",
   2474 		    sc->sc_dev.dv_xname, auto_phase));
   2475 		njsc32_write_2(sc, NJSC32_REG_EXECUTE_PHASE, 0);
   2476 
   2477 		if (auto_phase & NJSC32_XPHASE_SEL_TIMEOUT) {
   2478 			cmd = sc->sc_curcmd;
   2479 			if (cmd == NULL) {
   2480 				printf("%s: sel no cmd\n",
   2481 				    sc->sc_dev.dv_xname);
   2482 				goto out;
   2483 			}
   2484 			DPRINTC(cmd, ("njsc32_intr: selection timeout\n"));
   2485 
   2486 			sc->sc_curcmd = NULL;
   2487 			sc->sc_stat = NJSC32_STAT_IDLE;
   2488 			njsc32_end_cmd(sc, cmd, XS_SELTIMEOUT);
   2489 
   2490 			goto out;
   2491 		}
   2492 
   2493 #ifdef NJSC32_TRACE
   2494 		if (auto_phase & NJSC32_XPHASE_COMMAND) {
   2495 			/* Command phase has been automatically processed */
   2496 			TPRINTF(("%s: njsc32_intr: Command\n",
   2497 			    sc->sc_dev.dv_xname));
   2498 		}
   2499 #endif
   2500 #ifdef NJSC32_DEBUG
   2501 		if (auto_phase & NJSC32_XPHASE_ILLEGAL) {
   2502 			printf("%s: njsc32_intr: Illegal phase\n",
   2503 			    sc->sc_dev.dv_xname);
   2504 		}
   2505 #endif
   2506 
   2507 		if (auto_phase & NJSC32_XPHASE_PAUSED_MSG_IN) {
   2508 			TPRINTF(("%s: njsc32_intr: Process Message In\n",
   2509 			    sc->sc_dev.dv_xname));
   2510 			njsc32_msgin(sc);
   2511 		}
   2512 
   2513 		if (auto_phase & NJSC32_XPHASE_PAUSED_MSG_OUT) {
   2514 			TPRINTF(("%s: njsc32_intr: Process Message Out\n",
   2515 			    sc->sc_dev.dv_xname));
   2516 			njsc32_msgout(sc);
   2517 		}
   2518 
   2519 		cmd = sc->sc_curcmd;
   2520 		if (cmd == NULL) {
   2521 			TPRINTF(("%s: njsc32_intr: no cmd\n",
   2522 			    sc->sc_dev.dv_xname));
   2523 			goto out;
   2524 		}
   2525 
   2526 		if (auto_phase &
   2527 		    (NJSC32_XPHASE_DATA_IN | NJSC32_XPHASE_DATA_OUT)) {
   2528 			u_int32_t sackcnt, cntoffset;
   2529 
   2530 #ifdef NJSC32_TRACE
   2531 			if (auto_phase & NJSC32_XPHASE_DATA_IN)
   2532 				PRINTC(cmd, ("njsc32_intr: data in done\n"));
   2533 			if (auto_phase & NJSC32_XPHASE_DATA_OUT)
   2534 				PRINTC(cmd, ("njsc32_intr: data out done\n"));
   2535 			printf("BM %u, SGT %u, SACK %u, SAVED_ACK %u\n",
   2536 				njsc32_read_4(sc, NJSC32_REG_BM_CNT),
   2537 				njsc32_read_4(sc, NJSC32_REG_SGT_ADR),
   2538 				njsc32_read_4(sc, NJSC32_REG_SACK_CNT),
   2539 				njsc32_read_4(sc, NJSC32_REG_SAVED_ACK_CNT));
   2540 #endif
   2541 
   2542 			/*
   2543 			 * detected parity error on data transfer?
   2544 			 */
   2545 			if (njsc32_read_1(sc, NJSC32_REG_PARITY_STATUS) &
   2546 			    (NJSC32_PARITYSTATUS_ERROR_LSB|
   2547 			     NJSC32_PARITYSTATUS_ERROR_MSB)) {
   2548 
   2549 				PRINTC(cmd, ("datain: parity error\n"));
   2550 
   2551 				/* clear parity error */
   2552 				njsc32_write_1(sc, NJSC32_REG_PARITY_CONTROL,
   2553 				    NJSC32_PARITYCTL_CHECK_ENABLE |
   2554 				    NJSC32_PARITYCTL_CLEAR_ERROR);
   2555 
   2556 				if (auto_phase & NJSC32_XPHASE_BUS_FREE) {
   2557 					/*
   2558 					 * XXX command has already finished
   2559 					 * -- what can we do?
   2560 					 *
   2561 					 * It is not clear current command
   2562 					 * caused the error -- reset everything.
   2563 					 */
   2564 					njsc32_init(sc, 1);	/* XXX */
   2565 				} else {
   2566 					/* XXX does this case occur? */
   2567 #if 1
   2568 					printf("%s: datain: parity error\n",
   2569 					    sc->sc_dev.dv_xname);
   2570 #endif
   2571 					/*
   2572 					 * Make attention condition and try
   2573 					 * to send Initiator Detected Error
   2574 					 * message.
   2575 					 */
   2576 					njsc32_init_msgout(sc);
   2577 					njsc32_add_msgout(sc,
   2578 					    MSG_INITIATOR_DET_ERR);
   2579 					njsc32_write_4(sc,
   2580 					    NJSC32_REG_SCSI_MSG_OUT,
   2581 					    njsc32_get_auto_msgout(sc));
   2582 					/* restart autoscsi with ATN */
   2583 					njsc32_write_2(sc,
   2584 					    NJSC32_REG_COMMAND_CONTROL,
   2585 					    NJSC32_CMD_CLEAR_CDB_FIFO_PTR |
   2586 					    NJSC32_CMD_AUTO_COMMAND_PHASE |
   2587 					    NJSC32_CMD_AUTO_SCSI_RESTART |
   2588 					    NJSC32_CMD_AUTO_MSGIN_00_04 |
   2589 					    NJSC32_CMD_AUTO_MSGIN_02 |
   2590 					    NJSC32_CMD_AUTO_ATN);
   2591 				}
   2592 				goto out;
   2593 			}
   2594 
   2595 			/*
   2596 			 * data has been transferred, and current pointer
   2597 			 * is changed
   2598 			 */
   2599 			sackcnt = njsc32_read_4(sc, NJSC32_REG_SACK_CNT);
   2600 
   2601 			/*
   2602 			 * The controller returns extra ACK count
   2603 			 * if the DMA buffer is not 4byte aligned.
   2604 			 */
   2605 			cntoffset = le32toh(cmd->c_sgt[0].sg_addr) & 3;
   2606 #ifdef NJSC32_DEBUG
   2607 			if (cntoffset != 0) {
   2608 				printf("sackcnt %u, cntoffset %u\n",
   2609 				    sackcnt, cntoffset);
   2610 			}
   2611 #endif
   2612 			/* advance SCSI pointer */
   2613 			njsc32_set_cur_ptr(cmd,
   2614 			    cmd->c_dp_cur + sackcnt - cntoffset);
   2615 		}
   2616 
   2617 		if (auto_phase & NJSC32_XPHASE_MSGOUT) {
   2618 			/* Message Out phase has been automatically processed */
   2619 			TPRINTC(cmd, ("njsc32_intr: Message Out\n"));
   2620 			if ((auto_phase & NJSC32_XPHASE_PAUSED_MSG_IN) == 0 &&
   2621 			    sc->sc_msgoutlen <= NJSC32_MSGOUT_MAX_AUTO) {
   2622 				njsc32_init_msgout(sc);
   2623 			}
   2624 		}
   2625 
   2626 		if (auto_phase & NJSC32_XPHASE_STATUS) {
   2627 			/* Status phase has been automatically processed */
   2628 			cmd->c_xs->status =
   2629 			    njsc32_read_1(sc, NJSC32_REG_SCSI_CSB_IN);
   2630 			TPRINTC(cmd, ("njsc32_intr: Status %#x\n",
   2631 			    cmd->c_xs->status));
   2632 		}
   2633 
   2634 		if (auto_phase & NJSC32_XPHASE_BUS_FREE) {
   2635 			/* AutoSCSI is finished */
   2636 
   2637 			TPRINTC(cmd, ("njsc32_intr: Bus Free\n"));
   2638 
   2639 			sc->sc_stat = NJSC32_STAT_IDLE;
   2640 			sc->sc_curcmd = NULL;
   2641 
   2642 			njsc32_end_auto(sc, cmd, auto_phase);
   2643 		}
   2644 		goto out;
   2645 	}
   2646 
   2647 	if (intr & NJSC32_IRQ_FIFO_THRESHOLD) {
   2648 		/* XXX We use DMA, and this shouldn't happen */
   2649 		printf("%s: njsc32_intr: FIFO\n", sc->sc_dev.dv_xname);
   2650 		njsc32_init(sc, 1);
   2651 		goto out;
   2652 	}
   2653 	if (intr & NJSC32_IRQ_PCI) {
   2654 		/* XXX? */
   2655 		printf("%s: njsc32_intr: PCI\n", sc->sc_dev.dv_xname);
   2656 	}
   2657 	if (intr & NJSC32_IRQ_BMCNTERR) {
   2658 		/* XXX? */
   2659 		printf("%s: njsc32_intr: BM\n", sc->sc_dev.dv_xname);
   2660 	}
   2661 
   2662 out:
   2663 	/* go next command if controller is idle */
   2664 	if (sc->sc_stat == NJSC32_STAT_IDLE)
   2665 		njsc32_start(sc);
   2666 
   2667 #if 0
   2668 	/* enable interrupts */
   2669 	njsc32_write_2(sc, NJSC32_REG_IRQ, 0);
   2670 #endif
   2671 
   2672 	return 1;	/* processed */
   2673 }
   2674