Home | History | Annotate | Line # | Download | only in pci
mlx_pci.c revision 1.1.2.2
      1  1.1.2.2  bouyer /*	$NetBSD: mlx_pci.c,v 1.1.2.2 2001/02/11 19:15:58 bouyer Exp $	*/
      2  1.1.2.2  bouyer 
      3  1.1.2.2  bouyer /*-
      4  1.1.2.2  bouyer  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.1.2.2  bouyer  * All rights reserved.
      6  1.1.2.2  bouyer  *
      7  1.1.2.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.2  bouyer  * by Andrew Doran.
      9  1.1.2.2  bouyer  *
     10  1.1.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.1.2.2  bouyer  * are met:
     13  1.1.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.2  bouyer  *    must display the following acknowledgement:
     20  1.1.2.2  bouyer  *        This product includes software developed by the NetBSD
     21  1.1.2.2  bouyer  *        Foundation, Inc. and its contributors.
     22  1.1.2.2  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.2  bouyer  *    contributors may be used to endorse or promote products derived
     24  1.1.2.2  bouyer  *    from this software without specific prior written permission.
     25  1.1.2.2  bouyer  *
     26  1.1.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.2  bouyer  */
     38  1.1.2.2  bouyer 
     39  1.1.2.2  bouyer /*-
     40  1.1.2.2  bouyer  * Copyright (c) 1999 Michael Smith
     41  1.1.2.2  bouyer  * All rights reserved.
     42  1.1.2.2  bouyer  *
     43  1.1.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     44  1.1.2.2  bouyer  * modification, are permitted provided that the following conditions
     45  1.1.2.2  bouyer  * are met:
     46  1.1.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     47  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     48  1.1.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     50  1.1.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     51  1.1.2.2  bouyer  *
     52  1.1.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  1.1.2.2  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.1.2.2  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.1.2.2  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  1.1.2.2  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.1.2.2  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.1.2.2  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.1.2.2  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.1.2.2  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.1.2.2  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.1.2.2  bouyer  * SUCH DAMAGE.
     63  1.1.2.2  bouyer  *
     64  1.1.2.2  bouyer  * from FreeBSD: mlx_pci.c,v 1.4.2.4 2000/10/28 10:48:09 msmith Exp
     65  1.1.2.2  bouyer  */
     66  1.1.2.2  bouyer 
     67  1.1.2.2  bouyer /*
     68  1.1.2.2  bouyer  * PCI front-end for the mlx(4) driver.
     69  1.1.2.2  bouyer  */
     70  1.1.2.2  bouyer 
     71  1.1.2.2  bouyer #include <sys/param.h>
     72  1.1.2.2  bouyer #include <sys/systm.h>
     73  1.1.2.2  bouyer #include <sys/kernel.h>
     74  1.1.2.2  bouyer #include <sys/device.h>
     75  1.1.2.2  bouyer #include <sys/queue.h>
     76  1.1.2.2  bouyer #include <sys/callout.h>
     77  1.1.2.2  bouyer 
     78  1.1.2.2  bouyer #include <machine/endian.h>
     79  1.1.2.2  bouyer #include <machine/bus.h>
     80  1.1.2.2  bouyer 
     81  1.1.2.2  bouyer #include <dev/ic/mlxreg.h>
     82  1.1.2.2  bouyer #include <dev/ic/mlxio.h>
     83  1.1.2.2  bouyer #include <dev/ic/mlxvar.h>
     84  1.1.2.2  bouyer 
     85  1.1.2.2  bouyer #include <dev/pci/pcireg.h>
     86  1.1.2.2  bouyer #include <dev/pci/pcivar.h>
     87  1.1.2.2  bouyer #include <dev/pci/pcidevs.h>
     88  1.1.2.2  bouyer 
     89  1.1.2.2  bouyer static void	mlx_pci_attach(struct device *, struct device *, void *);
     90  1.1.2.2  bouyer static int	mlx_pci_match(struct device *, struct cfdata *, void *);
     91  1.1.2.2  bouyer static const struct mlx_pci_ident *mlx_pci_findmpi(struct pci_attach_args *);
     92  1.1.2.2  bouyer 
     93  1.1.2.2  bouyer static int	mlx_v3_submit(struct mlx_softc *, struct mlx_ccb *);
     94  1.1.2.2  bouyer static int	mlx_v3_findcomplete(struct mlx_softc *, u_int *, u_int *);
     95  1.1.2.2  bouyer static void	mlx_v3_intaction(struct mlx_softc *, int);
     96  1.1.2.2  bouyer static int	mlx_v3_fw_handshake(struct mlx_softc *, int *, int *, int *);
     97  1.1.2.2  bouyer 
     98  1.1.2.2  bouyer static int	mlx_v4_submit(struct mlx_softc *, struct mlx_ccb *);
     99  1.1.2.2  bouyer static int	mlx_v4_findcomplete(struct mlx_softc *, u_int *, u_int *);
    100  1.1.2.2  bouyer static void	mlx_v4_intaction(struct mlx_softc *, int);
    101  1.1.2.2  bouyer static int	mlx_v4_fw_handshake(struct mlx_softc *, int *, int *, int *);
    102  1.1.2.2  bouyer 
    103  1.1.2.2  bouyer static int	mlx_v5_submit(struct mlx_softc *, struct mlx_ccb *);
    104  1.1.2.2  bouyer static int	mlx_v5_findcomplete(struct mlx_softc *, u_int *, u_int *);
    105  1.1.2.2  bouyer static void	mlx_v5_intaction(struct mlx_softc *, int);
    106  1.1.2.2  bouyer static int	mlx_v5_fw_handshake(struct mlx_softc *, int *, int *, int *);
    107  1.1.2.2  bouyer 
    108  1.1.2.2  bouyer struct mlx_pci_ident {
    109  1.1.2.2  bouyer 	u_short	mpi_vendor;
    110  1.1.2.2  bouyer 	u_short	mpi_product;
    111  1.1.2.2  bouyer 	u_short	mpi_subvendor;
    112  1.1.2.2  bouyer 	u_short	mpi_subproduct;
    113  1.1.2.2  bouyer 	int	mpi_iftype;
    114  1.1.2.2  bouyer } static const mlx_pci_ident[] = {
    115  1.1.2.2  bouyer 	{
    116  1.1.2.2  bouyer 		PCI_VENDOR_MYLEX,
    117  1.1.2.2  bouyer 		PCI_PRODUCT_MYLEX_RAID_V2,
    118  1.1.2.2  bouyer 		0x0000,
    119  1.1.2.2  bouyer 		0x0000,
    120  1.1.2.2  bouyer 		2,
    121  1.1.2.2  bouyer 	},
    122  1.1.2.2  bouyer 	{
    123  1.1.2.2  bouyer 		PCI_VENDOR_MYLEX,
    124  1.1.2.2  bouyer 		PCI_PRODUCT_MYLEX_RAID_V3,
    125  1.1.2.2  bouyer 		0x0000,
    126  1.1.2.2  bouyer 		0x0000,
    127  1.1.2.2  bouyer 		3,
    128  1.1.2.2  bouyer 	},
    129  1.1.2.2  bouyer 	{
    130  1.1.2.2  bouyer 		PCI_VENDOR_MYLEX,
    131  1.1.2.2  bouyer 		PCI_PRODUCT_MYLEX_RAID_V4,
    132  1.1.2.2  bouyer 		0x0000,
    133  1.1.2.2  bouyer 		0x0000,
    134  1.1.2.2  bouyer 		4,
    135  1.1.2.2  bouyer 	},
    136  1.1.2.2  bouyer 	{
    137  1.1.2.2  bouyer 		PCI_VENDOR_DEC,
    138  1.1.2.2  bouyer 		PCI_PRODUCT_DEC_SWXCR,
    139  1.1.2.2  bouyer 		PCI_VENDOR_MYLEX,
    140  1.1.2.2  bouyer 		PCI_PRODUCT_MYLEX_RAID_V5,
    141  1.1.2.2  bouyer 		5,
    142  1.1.2.2  bouyer 	},
    143  1.1.2.2  bouyer };
    144  1.1.2.2  bouyer 
    145  1.1.2.2  bouyer struct cfattach mlx_pci_ca = {
    146  1.1.2.2  bouyer 	sizeof(struct mlx_softc), mlx_pci_match, mlx_pci_attach
    147  1.1.2.2  bouyer };
    148  1.1.2.2  bouyer 
    149  1.1.2.2  bouyer /*
    150  1.1.2.2  bouyer  * Try to find a `mlx_pci_ident' entry corresponding to this board.
    151  1.1.2.2  bouyer  */
    152  1.1.2.2  bouyer static const struct mlx_pci_ident *
    153  1.1.2.2  bouyer mlx_pci_findmpi(struct pci_attach_args *pa)
    154  1.1.2.2  bouyer {
    155  1.1.2.2  bouyer 	const struct mlx_pci_ident *mpi, *maxmpi;
    156  1.1.2.2  bouyer 	pcireg_t reg;
    157  1.1.2.2  bouyer 
    158  1.1.2.2  bouyer 	mpi = mlx_pci_ident;
    159  1.1.2.2  bouyer 	maxmpi = mpi + sizeof(mlx_pci_ident) / sizeof(mlx_pci_ident[0]);
    160  1.1.2.2  bouyer 
    161  1.1.2.2  bouyer 	for (; mpi < maxmpi; mpi++) {
    162  1.1.2.2  bouyer 		if (PCI_VENDOR(pa->pa_id) != mpi->mpi_vendor ||
    163  1.1.2.2  bouyer 		    PCI_PRODUCT(pa->pa_id) != mpi->mpi_product)
    164  1.1.2.2  bouyer 			continue;
    165  1.1.2.2  bouyer 
    166  1.1.2.2  bouyer 		if (mpi->mpi_subvendor == 0x0000)
    167  1.1.2.2  bouyer 			return (mpi);
    168  1.1.2.2  bouyer 
    169  1.1.2.2  bouyer 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    170  1.1.2.2  bouyer 
    171  1.1.2.2  bouyer 		if (PCI_VENDOR(reg) == mpi->mpi_subvendor &&
    172  1.1.2.2  bouyer 		    PCI_PRODUCT(reg) == mpi->mpi_subproduct)
    173  1.1.2.2  bouyer 			return (mpi);
    174  1.1.2.2  bouyer 	}
    175  1.1.2.2  bouyer 
    176  1.1.2.2  bouyer 	return (NULL);
    177  1.1.2.2  bouyer }
    178  1.1.2.2  bouyer 
    179  1.1.2.2  bouyer /*
    180  1.1.2.2  bouyer  * Match a supported board.
    181  1.1.2.2  bouyer  */
    182  1.1.2.2  bouyer static int
    183  1.1.2.2  bouyer mlx_pci_match(struct device *parent, struct cfdata *cfdata, void *aux)
    184  1.1.2.2  bouyer {
    185  1.1.2.2  bouyer 
    186  1.1.2.2  bouyer 	return (mlx_pci_findmpi(aux) != NULL);
    187  1.1.2.2  bouyer }
    188  1.1.2.2  bouyer 
    189  1.1.2.2  bouyer /*
    190  1.1.2.2  bouyer  * Attach a supported board.
    191  1.1.2.2  bouyer  */
    192  1.1.2.2  bouyer static void
    193  1.1.2.2  bouyer mlx_pci_attach(struct device *parent, struct device *self, void *aux)
    194  1.1.2.2  bouyer {
    195  1.1.2.2  bouyer 	struct pci_attach_args *pa;
    196  1.1.2.2  bouyer 	struct mlx_softc *mlx;
    197  1.1.2.2  bouyer 	pci_chipset_tag_t pc;
    198  1.1.2.2  bouyer 	pci_intr_handle_t ih;
    199  1.1.2.2  bouyer 	pcireg_t reg;
    200  1.1.2.2  bouyer 	const char *intrstr;
    201  1.1.2.2  bouyer 	int ior, memr, i;
    202  1.1.2.2  bouyer 	const struct mlx_pci_ident *mpi;
    203  1.1.2.2  bouyer 
    204  1.1.2.2  bouyer 	mlx = (struct mlx_softc *)self;
    205  1.1.2.2  bouyer 	pa = aux;
    206  1.1.2.2  bouyer 	pc = pa->pa_pc;
    207  1.1.2.2  bouyer 	mpi = mlx_pci_findmpi(aux);
    208  1.1.2.2  bouyer 
    209  1.1.2.2  bouyer 	mlx->mlx_dmat = pa->pa_dmat;
    210  1.1.2.2  bouyer 	mlx->mlx_iftype = mpi->mpi_iftype;
    211  1.1.2.2  bouyer 
    212  1.1.2.2  bouyer 	printf(": Mylex RAID (v%d interface)\n", mpi->mpi_iftype);
    213  1.1.2.2  bouyer 
    214  1.1.2.2  bouyer 	/*
    215  1.1.2.2  bouyer 	 * Map the PCI register window.
    216  1.1.2.2  bouyer 	 */
    217  1.1.2.2  bouyer 	memr = -1;
    218  1.1.2.2  bouyer 	ior = -1;
    219  1.1.2.2  bouyer 
    220  1.1.2.2  bouyer 	for (i = 0x10; i <= 0x14; i += 4) {
    221  1.1.2.2  bouyer 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, i);
    222  1.1.2.2  bouyer 
    223  1.1.2.2  bouyer 		if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
    224  1.1.2.2  bouyer 			if (ior == -1 && PCI_MAPREG_IO_SIZE(reg) != 0)
    225  1.1.2.2  bouyer 				ior = i;
    226  1.1.2.2  bouyer 		} else {
    227  1.1.2.2  bouyer 			if (memr == -1 && PCI_MAPREG_MEM_SIZE(reg) != 0)
    228  1.1.2.2  bouyer 				memr = i;
    229  1.1.2.2  bouyer 		}
    230  1.1.2.2  bouyer 	}
    231  1.1.2.2  bouyer 
    232  1.1.2.2  bouyer 	if (memr != -1)
    233  1.1.2.2  bouyer 		if (pci_mapreg_map(pa, memr, PCI_MAPREG_TYPE_MEM, 0,
    234  1.1.2.2  bouyer 		    &mlx->mlx_iot, &mlx->mlx_ioh, NULL, NULL))
    235  1.1.2.2  bouyer 			memr = -1;
    236  1.1.2.2  bouyer 	if (ior != -1)
    237  1.1.2.2  bouyer 		if (pci_mapreg_map(pa, ior, PCI_MAPREG_TYPE_IO, 0,
    238  1.1.2.2  bouyer 		    &mlx->mlx_iot, &mlx->mlx_ioh, NULL, NULL))
    239  1.1.2.2  bouyer 		    	ior = -1;
    240  1.1.2.2  bouyer 	if (memr == -1 && ior == -1) {
    241  1.1.2.2  bouyer 		printf("%s: can't map i/o or memory space\n", self->dv_xname);
    242  1.1.2.2  bouyer 		return;
    243  1.1.2.2  bouyer 	}
    244  1.1.2.2  bouyer 
    245  1.1.2.2  bouyer 	/* Enable the device. */
    246  1.1.2.2  bouyer 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    247  1.1.2.2  bouyer 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    248  1.1.2.2  bouyer 	    reg | PCI_COMMAND_MASTER_ENABLE);
    249  1.1.2.2  bouyer 
    250  1.1.2.2  bouyer 	/* Map and establish the interrupt. */
    251  1.1.2.2  bouyer 	if (pci_intr_map(pa, &ih)) {
    252  1.1.2.2  bouyer 		printf("%s: can't map interrupt\n", self->dv_xname);
    253  1.1.2.2  bouyer 		return;
    254  1.1.2.2  bouyer 	}
    255  1.1.2.2  bouyer 	intrstr = pci_intr_string(pc, ih);
    256  1.1.2.2  bouyer 	mlx->mlx_ih = pci_intr_establish(pc, ih, IPL_BIO, mlx_intr, mlx);
    257  1.1.2.2  bouyer 	if (mlx->mlx_ih == NULL) {
    258  1.1.2.2  bouyer 		printf("%s: can't establish interrupt", self->dv_xname);
    259  1.1.2.2  bouyer 		if (intrstr != NULL)
    260  1.1.2.2  bouyer 			printf(" at %s", intrstr);
    261  1.1.2.2  bouyer 		printf("\n");
    262  1.1.2.2  bouyer 		return;
    263  1.1.2.2  bouyer 	}
    264  1.1.2.2  bouyer 
    265  1.1.2.2  bouyer 	/* Select linkage based on controller interface type. */
    266  1.1.2.2  bouyer 	switch (mlx->mlx_iftype) {
    267  1.1.2.2  bouyer 	case 2:
    268  1.1.2.2  bouyer 	case 3:
    269  1.1.2.2  bouyer 		mlx->mlx_submit = mlx_v3_submit;
    270  1.1.2.2  bouyer 		mlx->mlx_findcomplete = mlx_v3_findcomplete;
    271  1.1.2.2  bouyer 		mlx->mlx_intaction = mlx_v3_intaction;
    272  1.1.2.2  bouyer 		mlx->mlx_fw_handshake = mlx_v3_fw_handshake;
    273  1.1.2.2  bouyer 		break;
    274  1.1.2.2  bouyer 
    275  1.1.2.2  bouyer 	case 4:
    276  1.1.2.2  bouyer 		mlx->mlx_submit = mlx_v4_submit;
    277  1.1.2.2  bouyer 		mlx->mlx_findcomplete = mlx_v4_findcomplete;
    278  1.1.2.2  bouyer 		mlx->mlx_intaction = mlx_v4_intaction;
    279  1.1.2.2  bouyer 		mlx->mlx_fw_handshake = mlx_v4_fw_handshake;
    280  1.1.2.2  bouyer 		break;
    281  1.1.2.2  bouyer 
    282  1.1.2.2  bouyer 	case 5:
    283  1.1.2.2  bouyer 		mlx->mlx_submit = mlx_v5_submit;
    284  1.1.2.2  bouyer 		mlx->mlx_findcomplete = mlx_v5_findcomplete;
    285  1.1.2.2  bouyer 		mlx->mlx_intaction = mlx_v5_intaction;
    286  1.1.2.2  bouyer 		mlx->mlx_fw_handshake = mlx_v5_fw_handshake;
    287  1.1.2.2  bouyer 		break;
    288  1.1.2.2  bouyer 	}
    289  1.1.2.2  bouyer 
    290  1.1.2.2  bouyer 	mlx_init(mlx, intrstr);
    291  1.1.2.2  bouyer }
    292  1.1.2.2  bouyer 
    293  1.1.2.2  bouyer /*
    294  1.1.2.2  bouyer  * ================= V3 interface linkage =================
    295  1.1.2.2  bouyer  */
    296  1.1.2.2  bouyer 
    297  1.1.2.2  bouyer /*
    298  1.1.2.2  bouyer  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on
    299  1.1.2.2  bouyer  * failure (the controller is not ready to take a command).
    300  1.1.2.2  bouyer  *
    301  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    302  1.1.2.2  bouyer  */
    303  1.1.2.2  bouyer static int
    304  1.1.2.2  bouyer mlx_v3_submit(struct mlx_softc *mlx, struct mlx_ccb *mc)
    305  1.1.2.2  bouyer {
    306  1.1.2.2  bouyer 
    307  1.1.2.2  bouyer 	/* Ready for our command? */
    308  1.1.2.2  bouyer 	if ((mlx_inb(mlx, MLX_V3REG_IDB) & MLX_V3_IDB_FULL) == 0) {
    309  1.1.2.2  bouyer 		/* Copy mailbox data to window. */
    310  1.1.2.2  bouyer 		bus_space_write_region_1(mlx->mlx_iot, mlx->mlx_ioh,
    311  1.1.2.2  bouyer 		    MLX_V3REG_MAILBOX, mc->mc_mbox, MLX_V3_MAILBOX_LEN);
    312  1.1.2.2  bouyer 		bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh,
    313  1.1.2.2  bouyer 		    MLX_V3REG_MAILBOX, MLX_V3_MAILBOX_LEN,
    314  1.1.2.2  bouyer 		    BUS_SPACE_BARRIER_WRITE);
    315  1.1.2.2  bouyer 
    316  1.1.2.2  bouyer 		/* Post command. */
    317  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V3REG_IDB, MLX_V3_IDB_FULL);
    318  1.1.2.2  bouyer 		return (1);
    319  1.1.2.2  bouyer 	}
    320  1.1.2.2  bouyer 
    321  1.1.2.2  bouyer 	return (0);
    322  1.1.2.2  bouyer }
    323  1.1.2.2  bouyer 
    324  1.1.2.2  bouyer /*
    325  1.1.2.2  bouyer  * See if a command has been completed, if so acknowledge its completion and
    326  1.1.2.2  bouyer  * recover the slot number and status code.
    327  1.1.2.2  bouyer  *
    328  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    329  1.1.2.2  bouyer  */
    330  1.1.2.2  bouyer static int
    331  1.1.2.2  bouyer mlx_v3_findcomplete(struct mlx_softc *mlx, u_int *slot, u_int *status)
    332  1.1.2.2  bouyer {
    333  1.1.2.2  bouyer 
    334  1.1.2.2  bouyer 	/* Status available? */
    335  1.1.2.2  bouyer 	if ((mlx_inb(mlx, MLX_V3REG_ODB) & MLX_V3_ODB_SAVAIL) != 0) {
    336  1.1.2.2  bouyer 		*slot = mlx_inb(mlx, MLX_V3REG_STATUS_IDENT);
    337  1.1.2.2  bouyer 		*status = mlx_inw(mlx, MLX_V3REG_STATUS);
    338  1.1.2.2  bouyer 
    339  1.1.2.2  bouyer 		/* Acknowledge completion. */
    340  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V3REG_ODB, MLX_V3_ODB_SAVAIL);
    341  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V3REG_IDB, MLX_V3_IDB_SACK);
    342  1.1.2.2  bouyer 		return (1);
    343  1.1.2.2  bouyer 	}
    344  1.1.2.2  bouyer 
    345  1.1.2.2  bouyer 	return (0);
    346  1.1.2.2  bouyer }
    347  1.1.2.2  bouyer 
    348  1.1.2.2  bouyer /*
    349  1.1.2.2  bouyer  * Enable/disable interrupts as requested. (No acknowledge required)
    350  1.1.2.2  bouyer  *
    351  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    352  1.1.2.2  bouyer  */
    353  1.1.2.2  bouyer static void
    354  1.1.2.2  bouyer mlx_v3_intaction(struct mlx_softc *mlx, int action)
    355  1.1.2.2  bouyer {
    356  1.1.2.2  bouyer 
    357  1.1.2.2  bouyer 	mlx_outb(mlx, MLX_V3REG_IE, action != 0);
    358  1.1.2.2  bouyer }
    359  1.1.2.2  bouyer 
    360  1.1.2.2  bouyer /*
    361  1.1.2.2  bouyer  * Poll for firmware error codes during controller initialisation.
    362  1.1.2.2  bouyer  *
    363  1.1.2.2  bouyer  * Returns 0 if initialisation is complete, 1 if still in progress but no
    364  1.1.2.2  bouyer  * error has been fetched, 2 if an error has been retrieved.
    365  1.1.2.2  bouyer  */
    366  1.1.2.2  bouyer static int
    367  1.1.2.2  bouyer mlx_v3_fw_handshake(struct mlx_softc *mlx, int *error, int *param1, int *param2)
    368  1.1.2.2  bouyer {
    369  1.1.2.2  bouyer 	u_int8_t fwerror;
    370  1.1.2.2  bouyer 
    371  1.1.2.2  bouyer 	/* First time around, clear any hardware completion status. */
    372  1.1.2.2  bouyer 	if ((mlx->mlx_flags & MLXF_FW_INITTED) == 0) {
    373  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V3REG_IDB, MLX_V3_IDB_SACK);
    374  1.1.2.2  bouyer 		DELAY(1000);
    375  1.1.2.2  bouyer 		mlx->mlx_flags |= MLXF_FW_INITTED;
    376  1.1.2.2  bouyer 	}
    377  1.1.2.2  bouyer 
    378  1.1.2.2  bouyer 	/* Init in progress? */
    379  1.1.2.2  bouyer 	if ((mlx_inb(mlx, MLX_V3REG_IDB) & MLX_V3_IDB_INIT_BUSY) == 0)
    380  1.1.2.2  bouyer 		return (0);
    381  1.1.2.2  bouyer 
    382  1.1.2.2  bouyer 	/* Test error value. */
    383  1.1.2.2  bouyer 	fwerror = mlx_inb(mlx, MLX_V3REG_FWERROR);
    384  1.1.2.2  bouyer 
    385  1.1.2.2  bouyer 	if ((fwerror & MLX_V3_FWERROR_PEND) == 0)
    386  1.1.2.2  bouyer 		return (1);
    387  1.1.2.2  bouyer 
    388  1.1.2.2  bouyer 	/* Mask status pending bit, fetch status. */
    389  1.1.2.2  bouyer 	*error = fwerror & ~MLX_V3_FWERROR_PEND;
    390  1.1.2.2  bouyer 	*param1 = mlx_inb(mlx, MLX_V3REG_FWERROR_PARAM1);
    391  1.1.2.2  bouyer 	*param2 = mlx_inb(mlx, MLX_V3REG_FWERROR_PARAM2);
    392  1.1.2.2  bouyer 
    393  1.1.2.2  bouyer 	/* Acknowledge. */
    394  1.1.2.2  bouyer 	mlx_outb(mlx, MLX_V3REG_FWERROR, 0);
    395  1.1.2.2  bouyer 
    396  1.1.2.2  bouyer 	return (2);
    397  1.1.2.2  bouyer }
    398  1.1.2.2  bouyer 
    399  1.1.2.2  bouyer /*
    400  1.1.2.2  bouyer  * ================= V4 interface linkage =================
    401  1.1.2.2  bouyer  */
    402  1.1.2.2  bouyer 
    403  1.1.2.2  bouyer /*
    404  1.1.2.2  bouyer  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on
    405  1.1.2.2  bouyer  * failure (the controller is not ready to take a command).
    406  1.1.2.2  bouyer  *
    407  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    408  1.1.2.2  bouyer  */
    409  1.1.2.2  bouyer static int
    410  1.1.2.2  bouyer mlx_v4_submit(struct mlx_softc *mlx, struct mlx_ccb *mc)
    411  1.1.2.2  bouyer {
    412  1.1.2.2  bouyer 
    413  1.1.2.2  bouyer 	/* Ready for our command? */
    414  1.1.2.2  bouyer 	if ((mlx_inl(mlx, MLX_V4REG_IDB) & MLX_V4_IDB_FULL) == 0) {
    415  1.1.2.2  bouyer 		/* Copy mailbox data to window. */
    416  1.1.2.2  bouyer 		bus_space_write_region_4(mlx->mlx_iot, mlx->mlx_ioh,
    417  1.1.2.2  bouyer 		    MLX_V4REG_MAILBOX, mc->mc_mbox, MLX_V4_MAILBOX_LEN >> 2);
    418  1.1.2.2  bouyer 		bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh,
    419  1.1.2.2  bouyer 		    MLX_V4REG_MAILBOX, MLX_V4_MAILBOX_LEN,
    420  1.1.2.2  bouyer 		    BUS_SPACE_BARRIER_WRITE);
    421  1.1.2.2  bouyer 
    422  1.1.2.2  bouyer 		/* Post command. */
    423  1.1.2.2  bouyer 		mlx_outl(mlx, MLX_V4REG_IDB, MLX_V4_IDB_HWMBOX_CMD);
    424  1.1.2.2  bouyer 		return (1);
    425  1.1.2.2  bouyer 	}
    426  1.1.2.2  bouyer 
    427  1.1.2.2  bouyer 	return (0);
    428  1.1.2.2  bouyer }
    429  1.1.2.2  bouyer 
    430  1.1.2.2  bouyer /*
    431  1.1.2.2  bouyer  * See if a command has been completed, if so acknowledge its completion and
    432  1.1.2.2  bouyer  * recover the slot number and status code.
    433  1.1.2.2  bouyer  *
    434  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    435  1.1.2.2  bouyer  */
    436  1.1.2.2  bouyer static int
    437  1.1.2.2  bouyer mlx_v4_findcomplete(struct mlx_softc *mlx, u_int *slot, u_int *status)
    438  1.1.2.2  bouyer {
    439  1.1.2.2  bouyer 
    440  1.1.2.2  bouyer 	/* Status available? */
    441  1.1.2.2  bouyer 	if ((mlx_inl(mlx, MLX_V4REG_ODB) & MLX_V4_ODB_HWSAVAIL) != 0) {
    442  1.1.2.2  bouyer 		*slot = mlx_inb(mlx, MLX_V4REG_STATUS_IDENT);
    443  1.1.2.2  bouyer 		*status = mlx_inw(mlx, MLX_V4REG_STATUS);
    444  1.1.2.2  bouyer 
    445  1.1.2.2  bouyer 		/* Acknowledge completion. */
    446  1.1.2.2  bouyer 		mlx_outl(mlx, MLX_V4REG_ODB, MLX_V4_ODB_HWMBOX_ACK);
    447  1.1.2.2  bouyer 		mlx_outl(mlx, MLX_V4REG_IDB, MLX_V4_IDB_SACK);
    448  1.1.2.2  bouyer 		return (1);
    449  1.1.2.2  bouyer 	}
    450  1.1.2.2  bouyer 
    451  1.1.2.2  bouyer 	return (0);
    452  1.1.2.2  bouyer }
    453  1.1.2.2  bouyer 
    454  1.1.2.2  bouyer /*
    455  1.1.2.2  bouyer  * Enable/disable interrupts as requested.
    456  1.1.2.2  bouyer  *
    457  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    458  1.1.2.2  bouyer  */
    459  1.1.2.2  bouyer static void
    460  1.1.2.2  bouyer mlx_v4_intaction(struct mlx_softc *mlx, int action)
    461  1.1.2.2  bouyer {
    462  1.1.2.2  bouyer 	u_int32_t ier;
    463  1.1.2.2  bouyer 
    464  1.1.2.2  bouyer 	if (!action)
    465  1.1.2.2  bouyer 		ier = MLX_V4_IE_MASK | MLX_V4_IE_DISINT;
    466  1.1.2.2  bouyer 	else
    467  1.1.2.2  bouyer 		ier = MLX_V4_IE_MASK & ~MLX_V4_IE_DISINT;
    468  1.1.2.2  bouyer 
    469  1.1.2.2  bouyer 	mlx_outl(mlx, MLX_V4REG_IE, ier);
    470  1.1.2.2  bouyer }
    471  1.1.2.2  bouyer 
    472  1.1.2.2  bouyer /*
    473  1.1.2.2  bouyer  * Poll for firmware error codes during controller initialisation.
    474  1.1.2.2  bouyer  *
    475  1.1.2.2  bouyer  * Returns 0 if initialisation is complete, 1 if still in progress but no
    476  1.1.2.2  bouyer  * error has been fetched, 2 if an error has been retrieved.
    477  1.1.2.2  bouyer  */
    478  1.1.2.2  bouyer static int
    479  1.1.2.2  bouyer mlx_v4_fw_handshake(struct mlx_softc *mlx, int *error, int *param1, int *param2)
    480  1.1.2.2  bouyer {
    481  1.1.2.2  bouyer 	u_int8_t fwerror;
    482  1.1.2.2  bouyer 
    483  1.1.2.2  bouyer 	/* First time around, clear any hardware completion status. */
    484  1.1.2.2  bouyer 	if ((mlx->mlx_flags & MLXF_FW_INITTED) == 0) {
    485  1.1.2.2  bouyer 		mlx_outl(mlx, MLX_V4REG_IDB, MLX_V4_IDB_SACK);
    486  1.1.2.2  bouyer 		DELAY(1000);
    487  1.1.2.2  bouyer 		mlx->mlx_flags |= MLXF_FW_INITTED;
    488  1.1.2.2  bouyer 	}
    489  1.1.2.2  bouyer 
    490  1.1.2.2  bouyer 	/* Init in progress? */
    491  1.1.2.2  bouyer 	if ((mlx_inl(mlx, MLX_V4REG_IDB) & MLX_V4_IDB_INIT_BUSY) == 0)
    492  1.1.2.2  bouyer 		return (0);
    493  1.1.2.2  bouyer 
    494  1.1.2.2  bouyer 	/* Test error value */
    495  1.1.2.2  bouyer 	fwerror = mlx_inb(mlx, MLX_V4REG_FWERROR);
    496  1.1.2.2  bouyer 	if ((fwerror & MLX_V4_FWERROR_PEND) == 0)
    497  1.1.2.2  bouyer 		return (1);
    498  1.1.2.2  bouyer 
    499  1.1.2.2  bouyer 	/* Mask status pending bit, fetch status. */
    500  1.1.2.2  bouyer 	*error = fwerror & ~MLX_V4_FWERROR_PEND;
    501  1.1.2.2  bouyer 	*param1 = mlx_inb(mlx, MLX_V4REG_FWERROR_PARAM1);
    502  1.1.2.2  bouyer 	*param2 = mlx_inb(mlx, MLX_V4REG_FWERROR_PARAM2);
    503  1.1.2.2  bouyer 
    504  1.1.2.2  bouyer 	/* Acknowledge. */
    505  1.1.2.2  bouyer 	mlx_outb(mlx, MLX_V4REG_FWERROR, 0);
    506  1.1.2.2  bouyer 
    507  1.1.2.2  bouyer 	return (2);
    508  1.1.2.2  bouyer }
    509  1.1.2.2  bouyer 
    510  1.1.2.2  bouyer /*
    511  1.1.2.2  bouyer  * ================= V5 interface linkage =================
    512  1.1.2.2  bouyer  */
    513  1.1.2.2  bouyer 
    514  1.1.2.2  bouyer /*
    515  1.1.2.2  bouyer  * Try to give (mc) to the controller.  Returns 1 if successful, 0 on failure
    516  1.1.2.2  bouyer  * (the controller is not ready to take a command).
    517  1.1.2.2  bouyer  *
    518  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    519  1.1.2.2  bouyer  */
    520  1.1.2.2  bouyer static int
    521  1.1.2.2  bouyer mlx_v5_submit(struct mlx_softc *mlx, struct mlx_ccb *mc)
    522  1.1.2.2  bouyer {
    523  1.1.2.2  bouyer 
    524  1.1.2.2  bouyer 	/* Ready for our command? */
    525  1.1.2.2  bouyer 	if ((mlx_inb(mlx, MLX_V5REG_IDB) & MLX_V5_IDB_EMPTY) != 0) {
    526  1.1.2.2  bouyer 		/* Copy mailbox data to window. */
    527  1.1.2.2  bouyer 		bus_space_write_region_4(mlx->mlx_iot, mlx->mlx_ioh,
    528  1.1.2.2  bouyer 		    MLX_V5REG_MAILBOX, mc->mc_mbox, MLX_V5_MAILBOX_LEN >> 2);
    529  1.1.2.2  bouyer 		bus_space_barrier(mlx->mlx_iot, mlx->mlx_ioh,
    530  1.1.2.2  bouyer 		    MLX_V5REG_MAILBOX, MLX_V5_MAILBOX_LEN,
    531  1.1.2.2  bouyer 		    BUS_SPACE_BARRIER_WRITE);
    532  1.1.2.2  bouyer 
    533  1.1.2.2  bouyer 		/* Post command */
    534  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V5REG_IDB, MLX_V5_IDB_HWMBOX_CMD);
    535  1.1.2.2  bouyer 		return (1);
    536  1.1.2.2  bouyer 	}
    537  1.1.2.2  bouyer 
    538  1.1.2.2  bouyer 	return (0);
    539  1.1.2.2  bouyer }
    540  1.1.2.2  bouyer 
    541  1.1.2.2  bouyer /*
    542  1.1.2.2  bouyer  * See if a command has been completed, if so acknowledge its completion and
    543  1.1.2.2  bouyer  * recover the slot number and status code.
    544  1.1.2.2  bouyer  *
    545  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    546  1.1.2.2  bouyer  */
    547  1.1.2.2  bouyer static int
    548  1.1.2.2  bouyer mlx_v5_findcomplete(struct mlx_softc *mlx, u_int *slot, u_int *status)
    549  1.1.2.2  bouyer {
    550  1.1.2.2  bouyer 
    551  1.1.2.2  bouyer 	/* Status available? */
    552  1.1.2.2  bouyer 	if ((mlx_inb(mlx, MLX_V5REG_ODB) & MLX_V5_ODB_HWSAVAIL) != 0) {
    553  1.1.2.2  bouyer 		*slot = mlx_inb(mlx, MLX_V5REG_STATUS_IDENT);
    554  1.1.2.2  bouyer 		*status = mlx_inw(mlx, MLX_V5REG_STATUS);
    555  1.1.2.2  bouyer 
    556  1.1.2.2  bouyer 		/* Acknowledge completion. */
    557  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V5REG_ODB, MLX_V5_ODB_HWMBOX_ACK);
    558  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V5REG_IDB, MLX_V5_IDB_SACK);
    559  1.1.2.2  bouyer 		return (1);
    560  1.1.2.2  bouyer 	}
    561  1.1.2.2  bouyer 
    562  1.1.2.2  bouyer 	return (0);
    563  1.1.2.2  bouyer }
    564  1.1.2.2  bouyer 
    565  1.1.2.2  bouyer /*
    566  1.1.2.2  bouyer  * Enable/disable interrupts as requested.
    567  1.1.2.2  bouyer  *
    568  1.1.2.2  bouyer  * Must be called at splbio or in a fashion that prevents reentry.
    569  1.1.2.2  bouyer  */
    570  1.1.2.2  bouyer static void
    571  1.1.2.2  bouyer mlx_v5_intaction(struct mlx_softc *mlx, int action)
    572  1.1.2.2  bouyer {
    573  1.1.2.2  bouyer 	u_int8_t ier;
    574  1.1.2.2  bouyer 
    575  1.1.2.2  bouyer 	if (!action)
    576  1.1.2.2  bouyer 		ier = 0xff & MLX_V5_IE_DISINT;
    577  1.1.2.2  bouyer 	else
    578  1.1.2.2  bouyer 		ier = 0xff & ~MLX_V5_IE_DISINT;
    579  1.1.2.2  bouyer 
    580  1.1.2.2  bouyer 	mlx_outb(mlx, MLX_V5REG_IE, ier);
    581  1.1.2.2  bouyer }
    582  1.1.2.2  bouyer 
    583  1.1.2.2  bouyer /*
    584  1.1.2.2  bouyer  * Poll for firmware error codes during controller initialisation.
    585  1.1.2.2  bouyer  *
    586  1.1.2.2  bouyer  * Returns 0 if initialisation is complete, 1 if still in progress but no
    587  1.1.2.2  bouyer  * error has been fetched, 2 if an error has been retrieved.
    588  1.1.2.2  bouyer  */
    589  1.1.2.2  bouyer static int
    590  1.1.2.2  bouyer mlx_v5_fw_handshake(struct mlx_softc *mlx, int *error, int *param1, int *param2)
    591  1.1.2.2  bouyer {
    592  1.1.2.2  bouyer 	u_int8_t fwerror;
    593  1.1.2.2  bouyer 
    594  1.1.2.2  bouyer 	/* First time around, clear any hardware completion status. */
    595  1.1.2.2  bouyer 	if ((mlx->mlx_flags & MLXF_FW_INITTED) == 0) {
    596  1.1.2.2  bouyer 		mlx_outb(mlx, MLX_V5REG_IDB, MLX_V5_IDB_SACK);
    597  1.1.2.2  bouyer 		DELAY(1000);
    598  1.1.2.2  bouyer 		mlx->mlx_flags |= MLXF_FW_INITTED;
    599  1.1.2.2  bouyer 	}
    600  1.1.2.2  bouyer 
    601  1.1.2.2  bouyer 	/* Init in progress? */
    602  1.1.2.2  bouyer 	if ((mlx_inb(mlx, MLX_V5REG_IDB) & MLX_V5_IDB_INIT_DONE) != 0)
    603  1.1.2.2  bouyer 		return (0);
    604  1.1.2.2  bouyer 
    605  1.1.2.2  bouyer 	/* Test for error value. */
    606  1.1.2.2  bouyer 	fwerror = mlx_inb(mlx, MLX_V5REG_FWERROR);
    607  1.1.2.2  bouyer 	if ((fwerror & MLX_V5_FWERROR_PEND) == 0)
    608  1.1.2.2  bouyer 		return (1);
    609  1.1.2.2  bouyer 
    610  1.1.2.2  bouyer 	/* Mask status pending bit, fetch status. */
    611  1.1.2.2  bouyer 	*error = fwerror & ~MLX_V5_FWERROR_PEND;
    612  1.1.2.2  bouyer 	*param1 = mlx_inb(mlx, MLX_V5REG_FWERROR_PARAM1);
    613  1.1.2.2  bouyer 	*param2 = mlx_inb(mlx, MLX_V5REG_FWERROR_PARAM2);
    614  1.1.2.2  bouyer 
    615  1.1.2.2  bouyer 	/* Acknowledge. */
    616  1.1.2.2  bouyer 	mlx_outb(mlx, MLX_V5REG_FWERROR, 0xff);
    617  1.1.2.2  bouyer 
    618  1.1.2.2  bouyer 	return (2);
    619  1.1.2.2  bouyer }
    620