xref: /dragonfly/sys/dev/raid/mfi/mfi_pci.c (revision ce7a3582)
1 /*-
2  * Copyright (c) 2006 IronPort Systems
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  * Copyright (c) 2007 LSI Corp.
28  * Copyright (c) 2007 Rajesh Prabhakaran.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52 /*-
53  * Redistribution and use in source and binary forms, with or without
54  * modification, are permitted provided that the following conditions
55  * are met:
56  *
57  *            Copyright 1994-2009 The FreeBSD Project.
58  *            All rights reserved.
59  *
60  * 1. Redistributions of source code must retain the above copyright
61  *    notice, this list of conditions and the following disclaimer.
62  * 2. Redistributions in binary form must reproduce the above copyright
63  *    notice, this list of conditions and the following disclaimer in the
64  *    documentation and/or other materials provided with the distribution.
65  *
66  *    THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
68  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FREEBSD PROJECT OR
70  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
71  * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
72  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
73  * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
74  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
75  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
76  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77  *
78  * The views and conclusions contained in the software and documentation
79  * are those of the authors and should not be interpreted as representing
80  * official policies,either expressed or implied, of the FreeBSD Project.
81  *
82  * $FreeBSD: src/sys/dev/mfi/mfi_pci.c,v 1.16 2010/03/02 17:34:11 kib Exp $
83  */
84 
85 /* PCI/PCI-X/PCIe bus interface for the LSI MegaSAS controllers */
86 
87 #include "opt_mfi.h"
88 
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/kernel.h>
92 #include <sys/module.h>
93 #include <sys/bus.h>
94 #include <sys/conf.h>
95 #include <sys/buf2.h>
96 #include <sys/malloc.h>
97 #include <sys/uio.h>
98 #include <sys/eventhandler.h>
99 
100 #include <sys/rman.h>
101 
102 #include <bus/pci/pcireg.h>
103 #include <bus/pci/pcivar.h>
104 
105 #include <dev/raid/mfi/mfireg.h>
106 #include <dev/raid/mfi/mfi_ioctl.h>
107 #include <dev/raid/mfi/mfivar.h>
108 
109 static int	mfi_pci_probe(device_t);
110 static int	mfi_pci_attach(device_t);
111 static int	mfi_pci_detach(device_t);
112 static int	mfi_pci_suspend(device_t);
113 static int	mfi_pci_resume(device_t);
114 static void	mfi_pci_free(struct mfi_softc *);
115 
116 static device_method_t mfi_methods[] = {
117 	DEVMETHOD(device_probe,		mfi_pci_probe),
118 	DEVMETHOD(device_attach,	mfi_pci_attach),
119 	DEVMETHOD(device_detach,	mfi_pci_detach),
120 	DEVMETHOD(device_suspend,	mfi_pci_suspend),
121 	DEVMETHOD(device_resume,	mfi_pci_resume),
122 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
123 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
124 	{ 0, 0 }
125 };
126 
127 static driver_t mfi_pci_driver = {
128 	"mfi",
129 	mfi_methods,
130 	sizeof(struct mfi_softc)
131 };
132 
133 static devclass_t	mfi_devclass;
134 DRIVER_MODULE(mfi, pci, mfi_pci_driver, mfi_devclass, NULL, NULL);
135 MODULE_VERSION(mfi, 1);
136 
137 struct mfi_ident {
138 	uint16_t	vendor;
139 	uint16_t	device;
140 	uint16_t	subvendor;
141 	uint16_t	subdevice;
142 	int		flags;
143 	const char	*desc;
144 } mfi_identifiers[] = {
145 	{0x1000, 0x0060, 0x1028, 0xffff, MFI_FLAGS_1078,  "Dell PERC 6"},
146 	{0x1000, 0x0060, 0xffff, 0xffff, MFI_FLAGS_1078,  "LSI MegaSAS 1078"},
147 	{0x1000, 0x0071, 0xffff, 0xffff, MFI_FLAGS_SKINNY, "Drake Skinny"},
148 	{0x1000, 0x0073, 0xffff, 0xffff, MFI_FLAGS_SKINNY, "Drake Skinny"},
149 	{0x1000, 0x0078, 0xffff, 0xffff, MFI_FLAGS_GEN2,  "LSI MegaSAS Gen2"},
150 	{0x1000, 0x0079, 0x1028, 0x1f15, MFI_FLAGS_GEN2,  "Dell PERC H800 Adapter"},
151 	{0x1000, 0x0079, 0x1028, 0x1f16, MFI_FLAGS_GEN2,  "Dell PERC H700 Adapter"},
152 	{0x1000, 0x0079, 0x1028, 0x1f17, MFI_FLAGS_GEN2,  "Dell PERC H700 Integrated"},
153 	{0x1000, 0x0079, 0x1028, 0x1f18, MFI_FLAGS_GEN2,  "Dell PERC H700 Modular"},
154 	{0x1000, 0x0079, 0x1028, 0x1f19, MFI_FLAGS_GEN2,  "Dell PERC H700"},
155 	{0x1000, 0x0079, 0x1028, 0x1f1a, MFI_FLAGS_GEN2,  "Dell PERC H800 Proto Adapter"},
156 	{0x1000, 0x0079, 0x1028, 0x1f1b, MFI_FLAGS_GEN2,  "Dell PERC H800"},
157 	{0x1000, 0x0079, 0x1028, 0xffff, MFI_FLAGS_GEN2,  "Dell PERC Gen2"},
158 	{0x1000, 0x0079, 0xffff, 0xffff, MFI_FLAGS_GEN2,  "LSI MegaSAS Gen2"},
159 	{0x1000, 0x007c, 0xffff, 0xffff, MFI_FLAGS_1078,  "LSI MegaSAS 1078"},
160 	{0x1000, 0x0411, 0xffff, 0xffff, MFI_FLAGS_1064R, "LSI MegaSAS 1064R"}, /* Brocton IOP */
161 	{0x1000, 0x0413, 0xffff, 0xffff, MFI_FLAGS_1064R, "LSI MegaSAS 1064R"}, /* Verde ZCR */
162 	{0x1028, 0x0015, 0xffff, 0xffff, MFI_FLAGS_1064R, "Dell PERC 5/i"},
163 	{0, 0, 0, 0, 0, NULL}
164 };
165 
166 static struct mfi_ident *
167 mfi_find_ident(device_t dev)
168 {
169 	struct mfi_ident *m;
170 
171 	for (m = mfi_identifiers; m->vendor != 0; m++) {
172 		if ((m->vendor == pci_get_vendor(dev)) &&
173 		    (m->device == pci_get_device(dev)) &&
174 		    ((m->subvendor == pci_get_subvendor(dev)) ||
175 		    (m->subvendor == 0xffff)) &&
176 		    ((m->subdevice == pci_get_subdevice(dev)) ||
177 		    (m->subdevice == 0xffff)))
178 			return (m);
179 	}
180 
181 	return (NULL);
182 }
183 
184 static int
185 mfi_pci_probe(device_t dev)
186 {
187 	struct mfi_ident *id;
188 
189 	if ((id = mfi_find_ident(dev)) != NULL) {
190 		device_set_desc(dev, id->desc);
191 		return (BUS_PROBE_DEFAULT);
192 	}
193 	return (ENXIO);
194 }
195 
196 static int
197 mfi_pci_attach(device_t dev)
198 {
199 	struct mfi_softc *sc;
200 	struct mfi_ident *m;
201 	uint32_t command;
202 	int error;
203 
204 	sc = device_get_softc(dev);
205 	bzero(sc, sizeof(*sc));
206 	sc->mfi_dev = dev;
207 	m = mfi_find_ident(dev);
208 	sc->mfi_flags = m->flags;
209 
210 	/* Verify that the adapter can be set up in PCI space */
211 	command = pci_read_config(dev, PCIR_COMMAND, 2);
212 	command |= PCIM_CMD_BUSMASTEREN;
213 	pci_write_config(dev, PCIR_COMMAND, command, 2);
214 	command = pci_read_config(dev, PCIR_COMMAND, 2);
215 	if ((command & PCIM_CMD_BUSMASTEREN) == 0) {
216 		device_printf(dev, "Can't enable PCI busmaster\n");
217 		return (ENXIO);
218 	}
219 	if ((command & PCIM_CMD_MEMEN) == 0) {
220 		device_printf(dev, "PCI memory window not available\n");
221 		return (ENXIO);
222 	}
223 
224 	/* Allocate PCI registers */
225 	if ((sc->mfi_flags & MFI_FLAGS_1064R) ||
226 	    (sc->mfi_flags & MFI_FLAGS_1078)) {
227 		/* 1068/1078: Memory mapped BAR is at offset 0x10 */
228 		sc->mfi_regs_rid = PCIR_BAR(0);
229 	} else if ((sc->mfi_flags & MFI_FLAGS_GEN2) ||
230 		   (sc->mfi_flags & MFI_FLAGS_SKINNY)) {
231 		/* GEN2/Skinny: Memory mapped BAR is at offset 0x14 */
232 		sc->mfi_regs_rid = PCIR_BAR(1);
233 	}
234 	if ((sc->mfi_regs_resource = bus_alloc_resource_any(sc->mfi_dev,
235 	    SYS_RES_MEMORY, &sc->mfi_regs_rid, RF_ACTIVE)) == NULL) {
236 		device_printf(dev, "Cannot allocate PCI registers\n");
237 		return (ENXIO);
238 	}
239 	sc->mfi_btag = rman_get_bustag(sc->mfi_regs_resource);
240 	sc->mfi_bhandle = rman_get_bushandle(sc->mfi_regs_resource);
241 
242 	error = ENOMEM;
243 
244 	/* Allocate parent DMA tag */
245 	if (bus_dma_tag_create(	NULL,			/* parent */
246 				1, 0,			/* algnmnt, boundary */
247 				BUS_SPACE_MAXADDR,	/* lowaddr */
248 				BUS_SPACE_MAXADDR,	/* highaddr */
249 				NULL, NULL,		/* filter, filterarg */
250 				BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
251 				BUS_SPACE_UNRESTRICTED,	/* nsegments */
252 				BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
253 				0,			/* flags */
254 				&sc->mfi_parent_dmat)) {
255 		device_printf(dev, "Cannot allocate parent DMA tag\n");
256 		goto out;
257 	}
258 
259 	error = mfi_attach(sc);
260 out:
261 	if (error) {
262 		mfi_free(sc);
263 		mfi_pci_free(sc);
264 	}
265 
266 	return (error);
267 }
268 
269 static int
270 mfi_pci_detach(device_t dev)
271 {
272 	struct mfi_softc *sc;
273 	struct mfi_disk *ld;
274 	struct mfi_system_pd *syspd = NULL;
275 	int error;
276 
277 	sc = device_get_softc(dev);
278 
279 	lockmgr(&sc->mfi_config_lock, LK_EXCLUSIVE);
280 	lockmgr(&sc->mfi_io_lock, LK_EXCLUSIVE);
281 	if ((sc->mfi_flags & MFI_FLAGS_OPEN) != 0) {
282 		lockmgr(&sc->mfi_io_lock, LK_RELEASE);
283 		lockmgr(&sc->mfi_config_lock, LK_RELEASE);
284 		return (EBUSY);
285 	}
286 	sc->mfi_detaching = 1;
287 	lockmgr(&sc->mfi_io_lock, LK_RELEASE);
288 
289 	while ((ld = TAILQ_FIRST(&sc->mfi_ld_tqh)) != NULL) {
290 		if ((error = device_delete_child(dev, ld->ld_dev)) != 0) {
291 			sc->mfi_detaching = 0;
292 			lockmgr(&sc->mfi_config_lock, LK_RELEASE);
293 			return (error);
294 		}
295 	}
296 	while ((syspd = TAILQ_FIRST(&sc->mfi_syspd_tqh)) != NULL) {
297 		if ((error = device_delete_child(dev,syspd->pd_dev)) != 0) {
298 			sc->mfi_detaching = 0;
299 			lockmgr(&sc->mfi_config_lock, LK_RELEASE);
300 			return (error);
301 		}
302 	}
303 	lockmgr(&sc->mfi_config_lock, LK_RELEASE);
304 
305 	EVENTHANDLER_DEREGISTER(shutdown_final, sc->mfi_eh);
306 
307 	mfi_shutdown(sc);
308 	mfi_free(sc);
309 	mfi_pci_free(sc);
310 	return (0);
311 }
312 
313 static void
314 mfi_pci_free(struct mfi_softc *sc)
315 {
316 
317 	if (sc->mfi_regs_resource != NULL) {
318 		bus_release_resource(sc->mfi_dev, SYS_RES_MEMORY,
319 		    sc->mfi_regs_rid, sc->mfi_regs_resource);
320 	}
321 
322 	return;
323 }
324 
325 static int
326 mfi_pci_suspend(device_t dev)
327 {
328 
329 	return (EINVAL);
330 }
331 
332 static int
333 mfi_pci_resume(device_t dev)
334 {
335 
336 	return (EINVAL);
337 }
338