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