xref: /freebsd/sys/dev/mlx/mlx_pci.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Michael Smith
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bio.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/sx.h>
37 
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/rman.h>
44 
45 #include <geom/geom_disk.h>
46 
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 
50 #include <dev/mlx/mlxio.h>
51 #include <dev/mlx/mlxvar.h>
52 #include <dev/mlx/mlxreg.h>
53 
54 static int			mlx_pci_probe(device_t dev);
55 static int			mlx_pci_attach(device_t dev);
56 
57 static device_method_t mlx_methods[] = {
58     /* Device interface */
59     DEVMETHOD(device_probe,	mlx_pci_probe),
60     DEVMETHOD(device_attach,	mlx_pci_attach),
61     DEVMETHOD(device_detach,	mlx_detach),
62     DEVMETHOD(device_shutdown,	mlx_shutdown),
63     DEVMETHOD(device_suspend,	mlx_suspend),
64     DEVMETHOD(device_resume,	mlx_resume),
65 
66     DEVMETHOD_END
67 };
68 
69 static driver_t mlx_pci_driver = {
70 	"mlx",
71 	mlx_methods,
72 	sizeof(struct mlx_softc)
73 };
74 
75 DRIVER_MODULE(mlx, pci, mlx_pci_driver, 0, 0);
76 
77 struct mlx_ident
78 {
79     u_int16_t	vendor;
80     u_int16_t	device;
81     u_int16_t	subvendor;
82     u_int16_t	subdevice;
83     int		iftype;
84     char	*desc;
85 } mlx_identifiers[] = {
86     {0x1069, 0x0001, 0x0000, 0x0000, MLX_IFTYPE_2, "Mylex version 2 RAID interface"},
87     {0x1069, 0x0002, 0x0000, 0x0000, MLX_IFTYPE_3, "Mylex version 3 RAID interface"},
88     {0x1069, 0x0010, 0x0000, 0x0000, MLX_IFTYPE_4, "Mylex version 4 RAID interface"},
89     {0x1011, 0x1065, 0x1069, 0x0020, MLX_IFTYPE_5, "Mylex version 5 RAID interface"},
90     {0, 0, 0, 0, 0, 0}
91 };
92 
93 static struct mlx_ident *
94 mlx_pci_match(device_t dev)
95 {
96     struct mlx_ident *m;
97 
98     for (m = mlx_identifiers; m->vendor != 0; m++) {
99 	if ((m->vendor == pci_get_vendor(dev)) &&
100 	    (m->device == pci_get_device(dev)) &&
101 	    ((m->subvendor == 0) || ((m->subvendor == pci_get_subvendor(dev)) &&
102 				     (m->subdevice == pci_get_subdevice(dev)))))
103 	    return (m);
104     }
105     return (NULL);
106 }
107 
108 static int
109 mlx_pci_probe(device_t dev)
110 {
111     struct mlx_ident	*m;
112 
113     debug_called(1);
114 
115     m = mlx_pci_match(dev);
116     if (m != NULL) {
117 	device_set_desc(dev, m->desc);
118 	return(BUS_PROBE_DEFAULT);
119     }
120     return(ENXIO);
121 }
122 
123 static int
124 mlx_pci_attach(device_t dev)
125 {
126     struct mlx_softc	*sc;
127     struct mlx_ident	*m;
128     int			error;
129 
130     debug_called(1);
131 
132     pci_enable_busmaster(dev);
133 
134     sc = device_get_softc(dev);
135     sc->mlx_dev = dev;
136 
137     /*
138      * Work out what sort of adapter this is (we need to know this in order
139      * to map the appropriate interface resources).
140      */
141     m = mlx_pci_match(dev);
142     if (m == NULL)		/* shouldn't happen */
143 	return(ENXIO);
144     sc->mlx_iftype = m->iftype;
145 
146     mtx_init(&sc->mlx_io_lock, "mlx I/O", NULL, MTX_DEF);
147     sx_init(&sc->mlx_config_lock, "mlx config");
148     callout_init_mtx(&sc->mlx_timeout, &sc->mlx_io_lock, 0);
149 
150     /*
151      * Allocate the PCI register window.
152      */
153 
154     /* type 2/3 adapters have an I/O region we don't prefer at base 0 */
155     switch(sc->mlx_iftype) {
156     case MLX_IFTYPE_2:
157     case MLX_IFTYPE_3:
158 	sc->mlx_mem_type = SYS_RES_MEMORY;
159 	sc->mlx_mem_rid = MLX_CFG_BASE1;
160 	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
161 		&sc->mlx_mem_rid, RF_ACTIVE);
162 	if (sc->mlx_mem == NULL) {
163 	    sc->mlx_mem_type = SYS_RES_IOPORT;
164 	    sc->mlx_mem_rid = MLX_CFG_BASE0;
165 	    sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
166 		&sc->mlx_mem_rid, RF_ACTIVE);
167 	}
168 	break;
169     case MLX_IFTYPE_4:
170     case MLX_IFTYPE_5:
171 	sc->mlx_mem_type = SYS_RES_MEMORY;
172 	sc->mlx_mem_rid = MLX_CFG_BASE0;
173 	sc->mlx_mem = bus_alloc_resource_any(dev, sc->mlx_mem_type,
174 		&sc->mlx_mem_rid, RF_ACTIVE);
175 	break;
176     }
177     if (sc->mlx_mem == NULL) {
178 	device_printf(sc->mlx_dev, "couldn't allocate mailbox window\n");
179 	mlx_free(sc);
180 	return(ENXIO);
181     }
182 
183     /*
184      * Allocate the parent bus DMA tag appropriate for PCI.
185      */
186     error = bus_dma_tag_create(bus_get_dma_tag(dev),	/* PCI parent */
187 			       1, 0, 			/* alignment, boundary */
188 			       BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
189 			       BUS_SPACE_MAXADDR, 	/* highaddr */
190 			       NULL, NULL, 		/* filter, filterarg */
191 			       BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
192 			       BUS_SPACE_UNRESTRICTED,	/* nsegments */
193 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
194 			       BUS_DMA_ALLOCNOW,	/* flags */
195 			       NULL,			/* lockfunc */
196 			       NULL,			/* lockarg */
197 			       &sc->mlx_parent_dmat);
198     if (error != 0) {
199 	device_printf(dev, "can't allocate parent DMA tag\n");
200 	mlx_free(sc);
201 	return(ENOMEM);
202     }
203 
204     /*
205      * Do bus-independent initialisation.
206      */
207     error = mlx_attach(sc);
208     if (error != 0) {
209 	mlx_free(sc);
210 	return(error);
211     }
212 
213     /*
214      * Start the controller.
215      */
216     mlx_startup(sc);
217     return(0);
218 }
219