]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mfi/mfi_pci.c
MFV r338092: ntp 4.2.8p12.
[FreeBSD/FreeBSD.git] / sys / dev / mfi / mfi_pci.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause
3  *
4  * Copyright (c) 2006 IronPort Systems
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  * Copyright (c) 2007 LSI Corp.
30  * Copyright (c) 2007 Rajesh Prabhakaran.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  */
54
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57
58 /* PCI/PCI-X/PCIe bus interface for the LSI MegaSAS controllers */
59
60 #include "opt_mfi.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/selinfo.h>
66 #include <sys/module.h>
67 #include <sys/bus.h>
68 #include <sys/conf.h>
69 #include <sys/bio.h>
70 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
72 #include <sys/uio.h>
73
74 #include <machine/bus.h>
75 #include <machine/resource.h>
76 #include <sys/rman.h>
77
78 #include <dev/pci/pcireg.h>
79 #include <dev/pci/pcivar.h>
80
81 #include <dev/mfi/mfireg.h>
82 #include <dev/mfi/mfi_ioctl.h>
83 #include <dev/mfi/mfivar.h>
84
85 static int      mfi_pci_probe(device_t);
86 static int      mfi_pci_attach(device_t);
87 static int      mfi_pci_detach(device_t);
88 static int      mfi_pci_suspend(device_t);
89 static int      mfi_pci_resume(device_t);
90 static void     mfi_pci_free(struct mfi_softc *);
91
92 static device_method_t mfi_methods[] = {
93         DEVMETHOD(device_probe,         mfi_pci_probe),
94         DEVMETHOD(device_attach,        mfi_pci_attach),
95         DEVMETHOD(device_detach,        mfi_pci_detach),
96         DEVMETHOD(device_suspend,       mfi_pci_suspend),
97         DEVMETHOD(device_resume,        mfi_pci_resume),
98
99         DEVMETHOD_END
100 };
101
102 static driver_t mfi_pci_driver = {
103         "mfi",
104         mfi_methods,
105         sizeof(struct mfi_softc)
106 };
107
108 static devclass_t       mfi_devclass;
109 DRIVER_MODULE(mfi, pci, mfi_pci_driver, mfi_devclass, 0, 0);
110 MODULE_VERSION(mfi, 1);
111
112 static int      mfi_msi = 1;
113 SYSCTL_INT(_hw_mfi, OID_AUTO, msi, CTLFLAG_RDTUN, &mfi_msi, 0,
114     "Enable use of MSI interrupts");
115
116 static int      mfi_mrsas_enable;
117 SYSCTL_INT(_hw_mfi, OID_AUTO, mrsas_enable, CTLFLAG_RDTUN, &mfi_mrsas_enable,
118      0, "Allow mrasas to take newer cards");
119
120 struct mfi_ident {
121         uint16_t        vendor;
122         uint16_t        device;
123         uint16_t        subvendor;
124         uint16_t        subdevice;
125         int             flags;
126         const char      *desc;
127 } mfi_identifiers[] = {
128         {0x1000, 0x005b, 0x1028, 0x1f2d, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H810 Adapter"},
129         {0x1000, 0x005b, 0x1028, 0x1f30, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710 Embedded"},
130         {0x1000, 0x005b, 0x1028, 0x1f31, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710P Adapter"},
131         {0x1000, 0x005b, 0x1028, 0x1f33, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710P Mini (blades)"},
132         {0x1000, 0x005b, 0x1028, 0x1f34, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710P Mini (monolithics)"},
133         {0x1000, 0x005b, 0x1028, 0x1f35, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710 Adapter"},
134         {0x1000, 0x005b, 0x1028, 0x1f37, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710 Mini (blades)"},
135         {0x1000, 0x005b, 0x1028, 0x1f38, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Dell PERC H710 Mini (monolithics)"},
136         {0x1000, 0x005b, 0x8086, 0x9265, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Intel (R) RAID Controller RS25DB080"},
137         {0x1000, 0x005b, 0x8086, 0x9285, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "Intel (R) RAID Controller RS25NB008"},
138         {0x1000, 0x005b, 0xffff, 0xffff, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS, "ThunderBolt"},
139         {0x1000, 0x005d, 0xffff, 0xffff, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS| MFI_FLAGS_INVADER, "Invader"},
140         {0x1000, 0x005f, 0xffff, 0xffff, MFI_FLAGS_SKINNY| MFI_FLAGS_TBOLT| MFI_FLAGS_MRSAS| MFI_FLAGS_FURY, "Fury"},
141         {0x1000, 0x0060, 0x1028, 0xffff, MFI_FLAGS_1078,  "Dell PERC 6"},
142         {0x1000, 0x0060, 0xffff, 0xffff, MFI_FLAGS_1078,  "LSI MegaSAS 1078"},
143         {0x1000, 0x0071, 0xffff, 0xffff, MFI_FLAGS_SKINNY, "Drake Skinny"},
144         {0x1000, 0x0073, 0xffff, 0xffff, MFI_FLAGS_SKINNY, "Drake Skinny"},
145         {0x1000, 0x0078, 0xffff, 0xffff, MFI_FLAGS_GEN2,  "LSI MegaSAS Gen2"},
146         {0x1000, 0x0079, 0x1028, 0x1f15, MFI_FLAGS_GEN2,  "Dell PERC H800 Adapter"},
147         {0x1000, 0x0079, 0x1028, 0x1f16, MFI_FLAGS_GEN2,  "Dell PERC H700 Adapter"},
148         {0x1000, 0x0079, 0x1028, 0x1f17, MFI_FLAGS_GEN2,  "Dell PERC H700 Integrated"},
149         {0x1000, 0x0079, 0x1028, 0x1f18, MFI_FLAGS_GEN2,  "Dell PERC H700 Modular"},
150         {0x1000, 0x0079, 0x1028, 0x1f19, MFI_FLAGS_GEN2,  "Dell PERC H700"},
151         {0x1000, 0x0079, 0x1028, 0x1f1a, MFI_FLAGS_GEN2,  "Dell PERC H800 Proto Adapter"},
152         {0x1000, 0x0079, 0x1028, 0x1f1b, MFI_FLAGS_GEN2,  "Dell PERC H800"},
153         {0x1000, 0x0079, 0x1028, 0xffff, MFI_FLAGS_GEN2,  "Dell PERC Gen2"},
154         {0x1000, 0x0079, 0xffff, 0xffff, MFI_FLAGS_GEN2,  "LSI MegaSAS Gen2"},
155         {0x1000, 0x007c, 0xffff, 0xffff, MFI_FLAGS_1078,  "LSI MegaSAS 1078"},
156         {0x1000, 0x0411, 0xffff, 0xffff, MFI_FLAGS_1064R, "LSI MegaSAS 1064R"}, /* Brocton IOP */
157         {0x1000, 0x0413, 0xffff, 0xffff, MFI_FLAGS_1064R, "LSI MegaSAS 1064R"}, /* Verde ZCR */
158         {0x1028, 0x0015, 0xffff, 0xffff, MFI_FLAGS_1064R, "Dell PERC 5/i"},
159         {0, 0, 0, 0, 0, NULL}
160 };
161
162 static struct mfi_ident *
163 mfi_find_ident(device_t dev)
164 {
165         struct mfi_ident *m;
166
167         for (m = mfi_identifiers; m->vendor != 0; m++) {
168                 if ((m->vendor == pci_get_vendor(dev)) &&
169                     (m->device == pci_get_device(dev)) &&
170                     ((m->subvendor == pci_get_subvendor(dev)) ||
171                     (m->subvendor == 0xffff)) &&
172                     ((m->subdevice == pci_get_subdevice(dev)) ||
173                     (m->subdevice == 0xffff)))
174                         return (m);
175         }
176
177         return (NULL);
178 }
179
180 static int
181 mfi_pci_probe(device_t dev)
182 {
183         struct mfi_ident *id;
184
185         if ((id = mfi_find_ident(dev)) != NULL) {
186                 device_set_desc(dev, id->desc);
187
188                 /* give priority to mrsas if tunable set */
189                 if ((id->flags & MFI_FLAGS_MRSAS) && mfi_mrsas_enable)
190                         return (BUS_PROBE_LOW_PRIORITY);
191                 else
192                         return (BUS_PROBE_DEFAULT);
193         }
194         return (ENXIO);
195 }
196
197 static int
198 mfi_pci_attach(device_t dev)
199 {
200         struct mfi_softc *sc;
201         struct mfi_ident *m;
202         int count, 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         /* Ensure busmastering is enabled */
211         pci_enable_busmaster(dev);
212
213         /* Allocate PCI registers */
214         if ((sc->mfi_flags & MFI_FLAGS_1064R) ||
215             (sc->mfi_flags & MFI_FLAGS_1078)) {
216                 /* 1068/1078: Memory mapped BAR is at offset 0x10 */
217                 sc->mfi_regs_rid = PCIR_BAR(0);
218         }
219         else if ((sc->mfi_flags & MFI_FLAGS_GEN2) ||
220                  (sc->mfi_flags & MFI_FLAGS_SKINNY) ||
221                 (sc->mfi_flags & MFI_FLAGS_TBOLT)) { 
222                 /* Gen2/Skinny: Memory mapped BAR is at offset 0x14 */
223                 sc->mfi_regs_rid = PCIR_BAR(1);
224         }
225         if ((sc->mfi_regs_resource = bus_alloc_resource_any(sc->mfi_dev,
226             SYS_RES_MEMORY, &sc->mfi_regs_rid, RF_ACTIVE)) == NULL) {
227                 device_printf(dev, "Cannot allocate PCI registers\n");
228                 return (ENXIO);
229         }
230         sc->mfi_btag = rman_get_bustag(sc->mfi_regs_resource);
231         sc->mfi_bhandle = rman_get_bushandle(sc->mfi_regs_resource);
232
233         error = ENOMEM;
234
235         /* Allocate parent DMA tag */
236         if (bus_dma_tag_create( bus_get_dma_tag(dev),   /* PCI parent */
237                                 1, 0,                   /* algnmnt, boundary */
238                                 BUS_SPACE_MAXADDR,      /* lowaddr */
239                                 BUS_SPACE_MAXADDR,      /* highaddr */
240                                 NULL, NULL,             /* filter, filterarg */
241                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
242                                 BUS_SPACE_UNRESTRICTED, /* nsegments */
243                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
244                                 0,                      /* flags */
245                                 NULL, NULL,             /* lockfunc, lockarg */
246                                 &sc->mfi_parent_dmat)) {
247                 device_printf(dev, "Cannot allocate parent DMA tag\n");
248                 goto out;
249         }
250
251         /* Allocate IRQ resource. */
252         sc->mfi_irq_rid = 0;
253         count = 1;
254         if (mfi_msi && pci_alloc_msi(sc->mfi_dev, &count) == 0) {
255                 device_printf(sc->mfi_dev, "Using MSI\n");
256                 sc->mfi_irq_rid = 1;
257         }
258         if ((sc->mfi_irq = bus_alloc_resource_any(sc->mfi_dev, SYS_RES_IRQ,
259             &sc->mfi_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
260                 device_printf(sc->mfi_dev, "Cannot allocate interrupt\n");
261                 error = EINVAL;
262                 goto out;
263         }
264
265         error = mfi_attach(sc);
266 out:
267         if (error) {
268                 mfi_free(sc);
269                 mfi_pci_free(sc);
270         }
271
272         return (error);
273 }
274
275 static int
276 mfi_pci_detach(device_t dev)
277 {
278         struct mfi_softc *sc;
279         int error, devcount, i;
280         device_t *devlist;
281
282         sc = device_get_softc(dev);
283
284         sx_xlock(&sc->mfi_config_lock);
285         mtx_lock(&sc->mfi_io_lock);
286         if ((sc->mfi_flags & MFI_FLAGS_OPEN) != 0) {
287                 mtx_unlock(&sc->mfi_io_lock);
288                 sx_xunlock(&sc->mfi_config_lock);
289                 return (EBUSY);
290         }
291         sc->mfi_detaching = 1;
292         mtx_unlock(&sc->mfi_io_lock);
293
294         if ((error = device_get_children(sc->mfi_dev, &devlist, &devcount)) != 0) {
295                 sx_xunlock(&sc->mfi_config_lock);
296                 return error;
297         }
298         for (i = 0; i < devcount; i++)
299                 device_delete_child(sc->mfi_dev, devlist[i]);
300         free(devlist, M_TEMP);
301         sx_xunlock(&sc->mfi_config_lock);
302
303         EVENTHANDLER_DEREGISTER(shutdown_final, sc->mfi_eh);
304
305         mfi_shutdown(sc);
306         mfi_free(sc);
307         mfi_pci_free(sc);
308         return (0);
309 }
310
311 static void
312 mfi_pci_free(struct mfi_softc *sc)
313 {
314
315         if (sc->mfi_regs_resource != NULL) {
316                 bus_release_resource(sc->mfi_dev, SYS_RES_MEMORY,
317                     sc->mfi_regs_rid, sc->mfi_regs_resource);
318         }
319         if (sc->mfi_irq_rid != 0)
320                 pci_release_msi(sc->mfi_dev);
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 }