xref: /freebsd/sys/dev/mlx/mlx_pci.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
11ac4b82bSMike Smith /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
41ac4b82bSMike Smith  * Copyright (c) 1999 Michael Smith
51ac4b82bSMike Smith  * All rights reserved.
61ac4b82bSMike Smith  *
71ac4b82bSMike Smith  * Redistribution and use in source and binary forms, with or without
81ac4b82bSMike Smith  * modification, are permitted provided that the following conditions
91ac4b82bSMike Smith  * are met:
101ac4b82bSMike Smith  * 1. Redistributions of source code must retain the above copyright
111ac4b82bSMike Smith  *    notice, this list of conditions and the following disclaimer.
121ac4b82bSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
131ac4b82bSMike Smith  *    notice, this list of conditions and the following disclaimer in the
141ac4b82bSMike Smith  *    documentation and/or other materials provided with the distribution.
151ac4b82bSMike Smith  *
161ac4b82bSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171ac4b82bSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181ac4b82bSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191ac4b82bSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201ac4b82bSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211ac4b82bSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221ac4b82bSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231ac4b82bSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241ac4b82bSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251ac4b82bSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261ac4b82bSMike Smith  * SUCH DAMAGE.
271ac4b82bSMike Smith  */
281ac4b82bSMike Smith 
291ac4b82bSMike Smith #include <sys/param.h>
301ac4b82bSMike Smith #include <sys/systm.h>
31b04e4c12SJohn Baldwin #include <sys/bio.h>
321ac4b82bSMike Smith #include <sys/kernel.h>
330fca6f8bSJohn Baldwin #include <sys/lock.h>
34fe12f24bSPoul-Henning Kamp #include <sys/module.h>
350fca6f8bSJohn Baldwin #include <sys/mutex.h>
360fca6f8bSJohn Baldwin #include <sys/sx.h>
371ac4b82bSMike Smith 
381ac4b82bSMike Smith #include <sys/bus.h>
391ac4b82bSMike Smith #include <sys/conf.h>
401ac4b82bSMike Smith 
411ac4b82bSMike Smith #include <machine/bus.h>
421ac4b82bSMike Smith #include <machine/resource.h>
431ac4b82bSMike Smith #include <sys/rman.h>
441ac4b82bSMike Smith 
45891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
46891619a6SPoul-Henning Kamp 
4738d8c994SWarner Losh #include <dev/pci/pcireg.h>
4838d8c994SWarner Losh #include <dev/pci/pcivar.h>
491ac4b82bSMike Smith 
501ac4b82bSMike Smith #include <dev/mlx/mlxio.h>
511ac4b82bSMike Smith #include <dev/mlx/mlxvar.h>
524b006d7bSMike Smith #include <dev/mlx/mlxreg.h>
531ac4b82bSMike Smith 
541ac4b82bSMike Smith static int			mlx_pci_probe(device_t dev);
551ac4b82bSMike Smith static int			mlx_pci_attach(device_t dev);
561ac4b82bSMike Smith 
571ac4b82bSMike Smith static device_method_t mlx_methods[] = {
581ac4b82bSMike Smith     /* Device interface */
591ac4b82bSMike Smith     DEVMETHOD(device_probe,	mlx_pci_probe),
601ac4b82bSMike Smith     DEVMETHOD(device_attach,	mlx_pci_attach),
611ac4b82bSMike Smith     DEVMETHOD(device_detach,	mlx_detach),
621ac4b82bSMike Smith     DEVMETHOD(device_shutdown,	mlx_shutdown),
631ac4b82bSMike Smith     DEVMETHOD(device_suspend,	mlx_suspend),
641ac4b82bSMike Smith     DEVMETHOD(device_resume,	mlx_resume),
651ac4b82bSMike Smith 
664b7ec270SMarius Strobl     DEVMETHOD_END
671ac4b82bSMike Smith };
681ac4b82bSMike Smith 
691ac4b82bSMike Smith static driver_t mlx_pci_driver = {
701ac4b82bSMike Smith 	"mlx",
711ac4b82bSMike Smith 	mlx_methods,
721ac4b82bSMike Smith 	sizeof(struct mlx_softc)
731ac4b82bSMike Smith };
741ac4b82bSMike Smith 
759cd54db5SJohn Baldwin DRIVER_MODULE(mlx, pci, mlx_pci_driver, 0, 0);
761ac4b82bSMike Smith 
775792b7feSMike Smith struct mlx_ident
781ac4b82bSMike Smith {
791ac4b82bSMike Smith     u_int16_t	vendor;
801ac4b82bSMike Smith     u_int16_t	device;
815792b7feSMike Smith     u_int16_t	subvendor;
825792b7feSMike Smith     u_int16_t	subdevice;
831ac4b82bSMike Smith     int		iftype;
841ac4b82bSMike Smith     char	*desc;
851ac4b82bSMike Smith } mlx_identifiers[] = {
86da8bb3a3SMike Smith     {0x1069, 0x0001, 0x0000, 0x0000, MLX_IFTYPE_2, "Mylex version 2 RAID interface"},
875792b7feSMike Smith     {0x1069, 0x0002, 0x0000, 0x0000, MLX_IFTYPE_3, "Mylex version 3 RAID interface"},
885792b7feSMike Smith     {0x1069, 0x0010, 0x0000, 0x0000, MLX_IFTYPE_4, "Mylex version 4 RAID interface"},
895792b7feSMike Smith     {0x1011, 0x1065, 0x1069, 0x0020, MLX_IFTYPE_5, "Mylex version 5 RAID interface"},
905792b7feSMike Smith     {0, 0, 0, 0, 0, 0}
911ac4b82bSMike Smith };
921ac4b82bSMike Smith 
930fca6f8bSJohn Baldwin static struct mlx_ident *
mlx_pci_match(device_t dev)940fca6f8bSJohn Baldwin mlx_pci_match(device_t dev)
950fca6f8bSJohn Baldwin {
960fca6f8bSJohn Baldwin     struct mlx_ident *m;
970fca6f8bSJohn Baldwin 
980fca6f8bSJohn Baldwin     for (m = mlx_identifiers; m->vendor != 0; m++) {
990fca6f8bSJohn Baldwin 	if ((m->vendor == pci_get_vendor(dev)) &&
1000fca6f8bSJohn Baldwin 	    (m->device == pci_get_device(dev)) &&
1010fca6f8bSJohn Baldwin 	    ((m->subvendor == 0) || ((m->subvendor == pci_get_subvendor(dev)) &&
1020fca6f8bSJohn Baldwin 				     (m->subdevice == pci_get_subdevice(dev)))))
1030fca6f8bSJohn Baldwin 	    return (m);
1040fca6f8bSJohn Baldwin     }
1050fca6f8bSJohn Baldwin     return (NULL);
1060fca6f8bSJohn Baldwin }
1070fca6f8bSJohn Baldwin 
1081ac4b82bSMike Smith static int
mlx_pci_probe(device_t dev)1091ac4b82bSMike Smith mlx_pci_probe(device_t dev)
1101ac4b82bSMike Smith {
1115792b7feSMike Smith     struct mlx_ident	*m;
1121ac4b82bSMike Smith 
113da8bb3a3SMike Smith     debug_called(1);
1141ac4b82bSMike Smith 
1150fca6f8bSJohn Baldwin     m = mlx_pci_match(dev);
1160fca6f8bSJohn Baldwin     if (m != NULL) {
1175792b7feSMike Smith 	device_set_desc(dev, m->desc);
1186b9907e7SWarner Losh 	return(BUS_PROBE_DEFAULT);
1191ac4b82bSMike Smith     }
1201ac4b82bSMike Smith     return(ENXIO);
1211ac4b82bSMike Smith }
1221ac4b82bSMike Smith 
1231ac4b82bSMike Smith static int
mlx_pci_attach(device_t dev)1241ac4b82bSMike Smith mlx_pci_attach(device_t dev)
1251ac4b82bSMike Smith {
1261ac4b82bSMike Smith     struct mlx_softc	*sc;
1270fca6f8bSJohn Baldwin     struct mlx_ident	*m;
1280fca6f8bSJohn Baldwin     int			error;
1291ac4b82bSMike Smith 
130da8bb3a3SMike Smith     debug_called(1);
1311ac4b82bSMike Smith 
1320fca6f8bSJohn Baldwin     pci_enable_busmaster(dev);
1331ac4b82bSMike Smith 
1341ac4b82bSMike Smith     sc = device_get_softc(dev);
1351ac4b82bSMike Smith     sc->mlx_dev = dev;
1361ac4b82bSMike Smith 
1371ac4b82bSMike Smith     /*
1381ac4b82bSMike Smith      * Work out what sort of adapter this is (we need to know this in order
1391ac4b82bSMike Smith      * to map the appropriate interface resources).
1401ac4b82bSMike Smith      */
1410fca6f8bSJohn Baldwin     m = mlx_pci_match(dev);
1420fca6f8bSJohn Baldwin     if (m == NULL)		/* shouldn't happen */
1431ac4b82bSMike Smith 	return(ENXIO);
1440fca6f8bSJohn Baldwin     sc->mlx_iftype = m->iftype;
1450fca6f8bSJohn Baldwin 
1460fca6f8bSJohn Baldwin     mtx_init(&sc->mlx_io_lock, "mlx I/O", NULL, MTX_DEF);
1470fca6f8bSJohn Baldwin     sx_init(&sc->mlx_config_lock, "mlx config");
1480fca6f8bSJohn Baldwin     callout_init_mtx(&sc->mlx_timeout, &sc->mlx_io_lock, 0);
1491ac4b82bSMike Smith 
1501ac4b82bSMike Smith     /*
1511ac4b82bSMike Smith      * Allocate the PCI register window.
1521ac4b82bSMike Smith      */
1531ac4b82bSMike Smith 
154786cd128SMike Smith     /* type 2/3 adapters have an I/O region we don't prefer at base 0 */
155da8bb3a3SMike Smith     switch(sc->mlx_iftype) {
156da8bb3a3SMike Smith     case MLX_IFTYPE_2:
157da8bb3a3SMike Smith     case MLX_IFTYPE_3:
1589b11c7baSMatthew N. Dodd 	sc->mlx_mem_type = SYS_RES_MEMORY;
1599b11c7baSMatthew N. Dodd 	sc->mlx_mem_rid = MLX_CFG_BASE1;
1605f96beb9SNate Lawson 	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
1615f96beb9SNate Lawson 		&sc->mlx_mem_rid, RF_ACTIVE);
162786cd128SMike Smith 	if (sc->mlx_mem == NULL) {
1639b11c7baSMatthew N. Dodd 	    sc->mlx_mem_type = SYS_RES_IOPORT;
1649b11c7baSMatthew N. Dodd 	    sc->mlx_mem_rid = MLX_CFG_BASE0;
1655f96beb9SNate Lawson 	    sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
1665f96beb9SNate Lawson 		&sc->mlx_mem_rid, RF_ACTIVE);
167786cd128SMike Smith 	}
168da8bb3a3SMike Smith 	break;
169da8bb3a3SMike Smith     case MLX_IFTYPE_4:
170da8bb3a3SMike Smith     case MLX_IFTYPE_5:
1719b11c7baSMatthew N. Dodd 	sc->mlx_mem_type = SYS_RES_MEMORY;
1729b11c7baSMatthew N. Dodd 	sc->mlx_mem_rid = MLX_CFG_BASE0;
1735f96beb9SNate Lawson 	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
1745f96beb9SNate Lawson 		&sc->mlx_mem_rid, RF_ACTIVE);
175da8bb3a3SMike Smith 	break;
176da8bb3a3SMike Smith     }
1771ac4b82bSMike Smith     if (sc->mlx_mem == NULL) {
1781ac4b82bSMike Smith 	device_printf(sc->mlx_dev, "couldn't allocate mailbox window\n");
1791ac4b82bSMike Smith 	mlx_free(sc);
1801ac4b82bSMike Smith 	return(ENXIO);
1811ac4b82bSMike Smith     }
1821ac4b82bSMike Smith 
1831ac4b82bSMike Smith     /*
1841ac4b82bSMike Smith      * Allocate the parent bus DMA tag appropriate for PCI.
1851ac4b82bSMike Smith      */
186b6f97155SScott Long     error = bus_dma_tag_create(bus_get_dma_tag(dev),	/* PCI parent */
1871ac4b82bSMike Smith 			       1, 0, 			/* alignment, boundary */
1881ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1891ac4b82bSMike Smith 			       BUS_SPACE_MAXADDR, 	/* highaddr */
1901ac4b82bSMike Smith 			       NULL, NULL, 		/* filter, filterarg */
1916f954fb3SAlexander Motin 			       BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1926f954fb3SAlexander Motin 			       BUS_SPACE_UNRESTRICTED,	/* nsegments */
1931ac4b82bSMike Smith 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1941ac4b82bSMike Smith 			       BUS_DMA_ALLOCNOW,	/* flags */
195f6b1c44dSScott Long 			       NULL,			/* lockfunc */
196f6b1c44dSScott Long 			       NULL,			/* lockarg */
1971ac4b82bSMike Smith 			       &sc->mlx_parent_dmat);
1981ac4b82bSMike Smith     if (error != 0) {
1991ac4b82bSMike Smith 	device_printf(dev, "can't allocate parent DMA tag\n");
2001ac4b82bSMike Smith 	mlx_free(sc);
2011ac4b82bSMike Smith 	return(ENOMEM);
2021ac4b82bSMike Smith     }
2031ac4b82bSMike Smith 
2041ac4b82bSMike Smith     /*
2059ae6b386SGordon Bergling      * Do bus-independent initialisation.
2061ac4b82bSMike Smith      */
2071ac4b82bSMike Smith     error = mlx_attach(sc);
208648fd2bdSChristian S.J. Peron     if (error != 0) {
20923db907cSPoul-Henning Kamp 	mlx_free(sc);
2101ac4b82bSMike Smith 	return(error);
211648fd2bdSChristian S.J. Peron     }
2121ac4b82bSMike Smith 
2131ac4b82bSMike Smith     /*
2141ac4b82bSMike Smith      * Start the controller.
2151ac4b82bSMike Smith      */
2161ac4b82bSMike Smith     mlx_startup(sc);
2171ac4b82bSMike Smith     return(0);
2181ac4b82bSMike Smith }
219