xref: /freebsd/sys/dev/mlx/mlx_pci.c (revision b6f97155ccab302d0c3920d20604b183a036f0fb)
11ac4b82bSMike Smith /*-
21ac4b82bSMike Smith  * Copyright (c) 1999 Michael Smith
31ac4b82bSMike Smith  * All rights reserved.
41ac4b82bSMike Smith  *
51ac4b82bSMike Smith  * Redistribution and use in source and binary forms, with or without
61ac4b82bSMike Smith  * modification, are permitted provided that the following conditions
71ac4b82bSMike Smith  * are met:
81ac4b82bSMike Smith  * 1. Redistributions of source code must retain the above copyright
91ac4b82bSMike Smith  *    notice, this list of conditions and the following disclaimer.
101ac4b82bSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
111ac4b82bSMike Smith  *    notice, this list of conditions and the following disclaimer in the
121ac4b82bSMike Smith  *    documentation and/or other materials provided with the distribution.
131ac4b82bSMike Smith  *
141ac4b82bSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151ac4b82bSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161ac4b82bSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171ac4b82bSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181ac4b82bSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191ac4b82bSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201ac4b82bSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211ac4b82bSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221ac4b82bSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231ac4b82bSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241ac4b82bSMike Smith  * SUCH DAMAGE.
251ac4b82bSMike Smith  */
261ac4b82bSMike Smith 
27aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
28aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
29aad970f1SDavid E. O'Brien 
301ac4b82bSMike Smith #include <sys/param.h>
311ac4b82bSMike Smith #include <sys/systm.h>
321ac4b82bSMike Smith #include <sys/kernel.h>
33fe12f24bSPoul-Henning Kamp #include <sys/module.h>
341ac4b82bSMike Smith 
351ac4b82bSMike Smith #include <sys/bus.h>
361ac4b82bSMike Smith #include <sys/conf.h>
371ac4b82bSMike Smith 
381ac4b82bSMike Smith #include <machine/bus.h>
391ac4b82bSMike Smith #include <machine/resource.h>
401ac4b82bSMike Smith #include <sys/rman.h>
411ac4b82bSMike Smith 
42891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
43891619a6SPoul-Henning Kamp 
4438d8c994SWarner Losh #include <dev/pci/pcireg.h>
4538d8c994SWarner Losh #include <dev/pci/pcivar.h>
461ac4b82bSMike Smith 
4715fd5d22SMike Smith #include <dev/mlx/mlx_compat.h>
481ac4b82bSMike Smith #include <dev/mlx/mlxio.h>
491ac4b82bSMike Smith #include <dev/mlx/mlxvar.h>
504b006d7bSMike Smith #include <dev/mlx/mlxreg.h>
511ac4b82bSMike Smith 
521ac4b82bSMike Smith static int			mlx_pci_probe(device_t dev);
531ac4b82bSMike Smith static int			mlx_pci_attach(device_t dev);
541ac4b82bSMike Smith 
551ac4b82bSMike Smith static device_method_t mlx_methods[] = {
561ac4b82bSMike Smith     /* Device interface */
571ac4b82bSMike Smith     DEVMETHOD(device_probe,	mlx_pci_probe),
581ac4b82bSMike Smith     DEVMETHOD(device_attach,	mlx_pci_attach),
591ac4b82bSMike Smith     DEVMETHOD(device_detach,	mlx_detach),
601ac4b82bSMike Smith     DEVMETHOD(device_shutdown,	mlx_shutdown),
611ac4b82bSMike Smith     DEVMETHOD(device_suspend,	mlx_suspend),
621ac4b82bSMike Smith     DEVMETHOD(device_resume,	mlx_resume),
631ac4b82bSMike Smith 
644b7ec270SMarius Strobl     DEVMETHOD_END
651ac4b82bSMike Smith };
661ac4b82bSMike Smith 
671ac4b82bSMike Smith static driver_t mlx_pci_driver = {
681ac4b82bSMike Smith 	"mlx",
691ac4b82bSMike Smith 	mlx_methods,
701ac4b82bSMike Smith 	sizeof(struct mlx_softc)
711ac4b82bSMike Smith };
721ac4b82bSMike Smith 
731ac4b82bSMike Smith DRIVER_MODULE(mlx, pci, mlx_pci_driver, mlx_devclass, 0, 0);
741ac4b82bSMike Smith 
755792b7feSMike Smith struct mlx_ident
761ac4b82bSMike Smith {
771ac4b82bSMike Smith     u_int16_t	vendor;
781ac4b82bSMike Smith     u_int16_t	device;
795792b7feSMike Smith     u_int16_t	subvendor;
805792b7feSMike Smith     u_int16_t	subdevice;
811ac4b82bSMike Smith     int		iftype;
821ac4b82bSMike Smith     char	*desc;
831ac4b82bSMike Smith } mlx_identifiers[] = {
84da8bb3a3SMike Smith     {0x1069, 0x0001, 0x0000, 0x0000, MLX_IFTYPE_2, "Mylex version 2 RAID interface"},
855792b7feSMike Smith     {0x1069, 0x0002, 0x0000, 0x0000, MLX_IFTYPE_3, "Mylex version 3 RAID interface"},
865792b7feSMike Smith     {0x1069, 0x0010, 0x0000, 0x0000, MLX_IFTYPE_4, "Mylex version 4 RAID interface"},
875792b7feSMike Smith     {0x1011, 0x1065, 0x1069, 0x0020, MLX_IFTYPE_5, "Mylex version 5 RAID interface"},
885792b7feSMike Smith     {0, 0, 0, 0, 0, 0}
891ac4b82bSMike Smith };
901ac4b82bSMike Smith 
911ac4b82bSMike Smith static int
921ac4b82bSMike Smith mlx_pci_probe(device_t dev)
931ac4b82bSMike Smith {
945792b7feSMike Smith     struct mlx_ident	*m;
951ac4b82bSMike Smith 
96da8bb3a3SMike Smith     debug_called(1);
971ac4b82bSMike Smith 
985792b7feSMike Smith     for (m = mlx_identifiers; m->vendor != 0; m++) {
995792b7feSMike Smith 	if ((m->vendor == pci_get_vendor(dev)) &&
1005792b7feSMike Smith 	    (m->device == pci_get_device(dev)) &&
1015792b7feSMike Smith 	    ((m->subvendor == 0) || ((m->subvendor == pci_get_subvendor(dev)) &&
1025792b7feSMike Smith 				     (m->subdevice == pci_get_subdevice(dev))))) {
1031ac4b82bSMike Smith 
1045792b7feSMike Smith 	    device_set_desc(dev, m->desc);
1056b9907e7SWarner Losh 	    return(BUS_PROBE_DEFAULT);
1061ac4b82bSMike Smith 	}
1071ac4b82bSMike Smith     }
1081ac4b82bSMike Smith     return(ENXIO);
1091ac4b82bSMike Smith }
1101ac4b82bSMike Smith 
1111ac4b82bSMike Smith static int
1121ac4b82bSMike Smith mlx_pci_attach(device_t dev)
1131ac4b82bSMike Smith {
1141ac4b82bSMike Smith     struct mlx_softc	*sc;
1159b11c7baSMatthew N. Dodd     int			i, error;
1161ac4b82bSMike Smith     u_int32_t		command;
1171ac4b82bSMike Smith 
118da8bb3a3SMike Smith     debug_called(1);
1191ac4b82bSMike Smith 
1201ac4b82bSMike Smith     /*
1211ac4b82bSMike Smith      * Make sure we are going to be able to talk to this board.
1221ac4b82bSMike Smith      */
123da8bb3a3SMike Smith     command = pci_read_config(dev, PCIR_COMMAND, 2);
1241ac4b82bSMike Smith     if ((command & PCIM_CMD_MEMEN) == 0) {
1251ac4b82bSMike Smith 	device_printf(dev, "memory window not available\n");
1261ac4b82bSMike Smith 	return(ENXIO);
1271ac4b82bSMike Smith     }
128da8bb3a3SMike Smith     /* force the busmaster enable bit on */
129da8bb3a3SMike Smith     command |= PCIM_CMD_BUSMASTEREN;
130da8bb3a3SMike Smith     pci_write_config(dev, PCIR_COMMAND, command, 2);
1311ac4b82bSMike Smith 
1321ac4b82bSMike Smith     /*
1331ac4b82bSMike Smith      * Initialise softc.
1341ac4b82bSMike Smith      */
1351ac4b82bSMike Smith     sc = device_get_softc(dev);
1361ac4b82bSMike Smith     bzero(sc, sizeof(*sc));
1371ac4b82bSMike Smith     sc->mlx_dev = dev;
1381ac4b82bSMike Smith 
1391ac4b82bSMike Smith     /*
1401ac4b82bSMike Smith      * Work out what sort of adapter this is (we need to know this in order
1411ac4b82bSMike Smith      * to map the appropriate interface resources).
1421ac4b82bSMike Smith      */
1431ac4b82bSMike Smith     sc->mlx_iftype = 0;
1441ac4b82bSMike Smith     for (i = 0; mlx_identifiers[i].vendor != 0; i++) {
1451ac4b82bSMike Smith 	if ((mlx_identifiers[i].vendor == pci_get_vendor(dev)) &&
1461ac4b82bSMike Smith 	    (mlx_identifiers[i].device == pci_get_device(dev))) {
1471ac4b82bSMike Smith 	    sc->mlx_iftype = mlx_identifiers[i].iftype;
1481ac4b82bSMike Smith 	    break;
1491ac4b82bSMike Smith 	}
1501ac4b82bSMike Smith     }
1511ac4b82bSMike Smith     if (sc->mlx_iftype == 0)		/* shouldn't happen */
1521ac4b82bSMike Smith 	return(ENXIO);
1531ac4b82bSMike Smith 
1541ac4b82bSMike Smith     /*
1551ac4b82bSMike Smith      * Allocate the PCI register window.
1561ac4b82bSMike Smith      */
1571ac4b82bSMike Smith 
158786cd128SMike Smith     /* type 2/3 adapters have an I/O region we don't prefer at base 0 */
159da8bb3a3SMike Smith     switch(sc->mlx_iftype) {
160da8bb3a3SMike Smith     case MLX_IFTYPE_2:
161da8bb3a3SMike Smith     case MLX_IFTYPE_3:
1629b11c7baSMatthew N. Dodd 	sc->mlx_mem_type = SYS_RES_MEMORY;
1639b11c7baSMatthew N. Dodd 	sc->mlx_mem_rid = MLX_CFG_BASE1;
1645f96beb9SNate Lawson 	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
1655f96beb9SNate Lawson 		&sc->mlx_mem_rid, RF_ACTIVE);
166786cd128SMike Smith 	if (sc->mlx_mem == NULL) {
1679b11c7baSMatthew N. Dodd 	    sc->mlx_mem_type = SYS_RES_IOPORT;
1689b11c7baSMatthew N. Dodd 	    sc->mlx_mem_rid = MLX_CFG_BASE0;
1695f96beb9SNate Lawson 	    sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
1705f96beb9SNate Lawson 		&sc->mlx_mem_rid, RF_ACTIVE);
171786cd128SMike Smith 	}
172da8bb3a3SMike Smith 	break;
173da8bb3a3SMike Smith     case MLX_IFTYPE_4:
174da8bb3a3SMike Smith     case MLX_IFTYPE_5:
1759b11c7baSMatthew N. Dodd 	sc->mlx_mem_type = SYS_RES_MEMORY;
1769b11c7baSMatthew N. Dodd 	sc->mlx_mem_rid = MLX_CFG_BASE0;
1775f96beb9SNate Lawson 	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
1785f96beb9SNate Lawson 		&sc->mlx_mem_rid, RF_ACTIVE);
179da8bb3a3SMike Smith 	break;
180da8bb3a3SMike Smith     }
1811ac4b82bSMike Smith     if (sc->mlx_mem == NULL) {
1821ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "couldn't allocate mailbox window\n");
1831ac4b82bSMike Smith 	mlx_free(sc);
1841ac4b82bSMike Smith 	return(ENXIO);
1851ac4b82bSMike Smith     }
1861ac4b82bSMike Smith     sc->mlx_btag = rman_get_bustag(sc->mlx_mem);
1871ac4b82bSMike Smith     sc->mlx_bhandle = rman_get_bushandle(sc->mlx_mem);
1881ac4b82bSMike Smith 
1891ac4b82bSMike Smith     /*
1901ac4b82bSMike Smith      * Allocate the parent bus DMA tag appropriate for PCI.
1911ac4b82bSMike Smith      */
192*b6f97155SScott Long     error = bus_dma_tag_create(bus_get_dma_tag(dev),	/* PCI parent */
1931ac4b82bSMike Smith 			       1, 0, 			/* alignment, boundary */
1941ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1951ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR, 	/* highaddr */
1961ac4b82bSMike Smith 			       NULL, NULL, 		/* filter, filterarg */
197baff09dbSMike Smith 			       MAXBSIZE, MLX_NSEG,	/* maxsize, nsegments */
1981ac4b82bSMike Smith 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1991ac4b82bSMike Smith 			       BUS_DMA_ALLOCNOW,	/* flags */
200f6b1c44dSScott Long 			       NULL,			/* lockfunc */
201f6b1c44dSScott Long 			       NULL,			/* lockarg */
2021ac4b82bSMike Smith 			       &sc->mlx_parent_dmat);
2031ac4b82bSMike Smith     if (error != 0) {
2041ac4b82bSMike Smith 	device_printf(dev, "can't allocate parent DMA tag\n");
2051ac4b82bSMike Smith 	mlx_free(sc);
2061ac4b82bSMike Smith 	return(ENOMEM);
2071ac4b82bSMike Smith     }
2081ac4b82bSMike Smith 
2091ac4b82bSMike Smith     /*
2101ac4b82bSMike Smith      * Do bus-independant initialisation.
2111ac4b82bSMike Smith      */
2121ac4b82bSMike Smith     error = mlx_attach(sc);
213648fd2bdSChristian S.J. Peron     if (error != 0) {
21423db907cSPoul-Henning Kamp 	mlx_free(sc);
2151ac4b82bSMike Smith 	return(error);
216648fd2bdSChristian S.J. Peron     }
2171ac4b82bSMike Smith 
2181ac4b82bSMike Smith     /*
2191ac4b82bSMike Smith      * Start the controller.
2201ac4b82bSMike Smith      */
2211ac4b82bSMike Smith     mlx_startup(sc);
2221ac4b82bSMike Smith     return(0);
2231ac4b82bSMike Smith }
224