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