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