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