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