]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mfi/mfi_pci.c
Update to tcsh 6.18.01.
[FreeBSD/FreeBSD.git] / sys / dev / mfi / mfi_pci.c
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 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 /* PCI/PCI-X/PCIe bus interface for the LSI MegaSAS controllers */
57
58 #include "opt_mfi.h"
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/selinfo.h>
64 #include <sys/module.h>
65 #include <sys/bus.h>
66 #include <sys/conf.h>
67 #include <sys/bio.h>
68 #include <sys/malloc.h>
69 #include <sys/sysctl.h>
70 #include <sys/uio.h>
71
72 #include <machine/bus.h>
73 #include <machine/resource.h>
74 #include <sys/rman.h>
75
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcivar.h>
78
79 #include <dev/mfi/mfireg.h>
80 #include <dev/mfi/mfi_ioctl.h>
81 #include <dev/mfi/mfivar.h>
82
83 static int      mfi_pci_probe(device_t);
84 static int      mfi_pci_attach(device_t);
85 static int      mfi_pci_detach(device_t);
86 static int      mfi_pci_suspend(device_t);
87 static int      mfi_pci_resume(device_t);
88 static void     mfi_pci_free(struct mfi_softc *);
89
90 static device_method_t mfi_methods[] = {
91         DEVMETHOD(device_probe,         mfi_pci_probe),
92         DEVMETHOD(device_attach,        mfi_pci_attach),
93         DEVMETHOD(device_detach,        mfi_pci_detach),
94         DEVMETHOD(device_suspend,       mfi_pci_suspend),
95         DEVMETHOD(device_resume,        mfi_pci_resume),
96
97         DEVMETHOD_END
98 };
99
100 static driver_t mfi_pci_driver = {
101         "mfi",
102         mfi_methods,
103         sizeof(struct mfi_softc)
104 };
105
106 static devclass_t       mfi_devclass;
107 DRIVER_MODULE(mfi, pci, mfi_pci_driver, mfi_devclass, 0, 0);
108 MODULE_VERSION(mfi, 1);
109
110 static int      mfi_msi = 0;
111 TUNABLE_INT("hw.mfi.msi", &mfi_msi);
112 SYSCTL_INT(_hw_mfi, OID_AUTO, msi, CTLFLAG_RDTUN, &mfi_msi, 0,
113     "Enable use of MSI interrupts");
114
115 struct mfi_ident {
116         uint16_t        vendor;
117         uint16_t        device;
118         uint16_t        subvendor;
119         uint16_t        subdevice;
120         int             flags;
121         const char      *desc;
122 } mfi_identifiers[] = {
123         {0x1000, 0x0060, 0x1028, 0xffff, MFI_FLAGS_1078,  "Dell PERC 6"},
124         {0x1000, 0x0060, 0xffff, 0xffff, MFI_FLAGS_1078,  "LSI MegaSAS 1078"},
125         {0x1000, 0x0078, 0xffff, 0xffff, MFI_FLAGS_GEN2,  "LSI MegaSAS Gen2"},
126         {0x1000, 0x0079, 0x1028, 0x1f15, MFI_FLAGS_GEN2,  "Dell PERC H800 Adapter"},
127         {0x1000, 0x0079, 0x1028, 0x1f16, MFI_FLAGS_GEN2,  "Dell PERC H700 Adapter"},
128         {0x1000, 0x0079, 0x1028, 0x1f17, MFI_FLAGS_GEN2,  "Dell PERC H700 Integrated"},
129         {0x1000, 0x0079, 0x1028, 0x1f18, MFI_FLAGS_GEN2,  "Dell PERC H700 Modular"},
130         {0x1000, 0x0079, 0x1028, 0x1f19, MFI_FLAGS_GEN2,  "Dell PERC H700"},
131         {0x1000, 0x0079, 0x1028, 0x1f1b, MFI_FLAGS_GEN2,  "Dell PERC H800"},
132         {0x1000, 0x0079, 0x1028, 0xffff, MFI_FLAGS_GEN2,  "Dell PERC Gen2"},
133         {0x1000, 0x0079, 0xffff, 0xffff, MFI_FLAGS_GEN2,  "LSI MegaSAS Gen2"},
134         {0x1000, 0x007c, 0xffff, 0xffff, MFI_FLAGS_1078,  "LSI MegaSAS 1078"},
135         {0x1000, 0x0411, 0xffff, 0xffff, MFI_FLAGS_1064R, "LSI MegaSAS 1064R"}, /* Brocton IOP */
136         {0x1000, 0x0413, 0xffff, 0xffff, MFI_FLAGS_1064R, "LSI MegaSAS 1064R"}, /* Verde ZCR */
137         {0x1028, 0x0015, 0xffff, 0xffff, MFI_FLAGS_1064R, "Dell PERC 5/i"},
138         {0, 0, 0, 0, 0, NULL}
139 };
140
141 static struct mfi_ident *
142 mfi_find_ident(device_t dev)
143 {
144         struct mfi_ident *m;
145
146         for (m = mfi_identifiers; m->vendor != 0; m++) {
147                 if ((m->vendor == pci_get_vendor(dev)) &&
148                     (m->device == pci_get_device(dev)) &&
149                     ((m->subvendor == pci_get_subvendor(dev)) ||
150                     (m->subvendor == 0xffff)) &&
151                     ((m->subdevice == pci_get_subdevice(dev)) ||
152                     (m->subdevice == 0xffff)))
153                         return (m);
154         }
155
156         return (NULL);
157 }
158
159 static int
160 mfi_pci_probe(device_t dev)
161 {
162         struct mfi_ident *id;
163
164         if ((id = mfi_find_ident(dev)) != NULL) {
165                 device_set_desc(dev, id->desc);
166                 return (BUS_PROBE_DEFAULT);
167         }
168         return (ENXIO);
169 }
170
171 static int
172 mfi_pci_attach(device_t dev)
173 {
174         struct mfi_softc *sc;
175         struct mfi_ident *m;
176         uint32_t command;
177         int count, error;
178
179         sc = device_get_softc(dev);
180         bzero(sc, sizeof(*sc));
181         sc->mfi_dev = dev;
182         m = mfi_find_ident(dev);
183         sc->mfi_flags = m->flags;
184
185         /* Verify that the adapter can be set up in PCI space */
186         command = pci_read_config(dev, PCIR_COMMAND, 2);
187         command |= PCIM_CMD_BUSMASTEREN;
188         pci_write_config(dev, PCIR_COMMAND, command, 2);
189         command = pci_read_config(dev, PCIR_COMMAND, 2);
190         if ((command & PCIM_CMD_BUSMASTEREN) == 0) {
191                 device_printf(dev, "Can't enable PCI busmaster\n");
192                 return (ENXIO);
193         }
194         if ((command & PCIM_CMD_MEMEN) == 0) {
195                 device_printf(dev, "PCI memory window not available\n");
196                 return (ENXIO);
197         }
198
199         /* Allocate PCI registers */
200         if ((sc->mfi_flags & MFI_FLAGS_1064R) ||
201             (sc->mfi_flags & MFI_FLAGS_1078)) {
202                 /* 1068/1078: Memory mapped BAR is at offset 0x10 */
203                 sc->mfi_regs_rid = PCIR_BAR(0);
204         } else if (sc->mfi_flags & MFI_FLAGS_GEN2) {
205                 /* GEN2: Memory mapped BAR is at offset 0x14 */
206                 sc->mfi_regs_rid = PCIR_BAR(1);
207         }
208         if ((sc->mfi_regs_resource = bus_alloc_resource_any(sc->mfi_dev,
209             SYS_RES_MEMORY, &sc->mfi_regs_rid, RF_ACTIVE)) == NULL) {
210                 device_printf(dev, "Cannot allocate PCI registers\n");
211                 return (ENXIO);
212         }
213         sc->mfi_btag = rman_get_bustag(sc->mfi_regs_resource);
214         sc->mfi_bhandle = rman_get_bushandle(sc->mfi_regs_resource);
215
216         error = ENOMEM;
217
218         /* Allocate parent DMA tag */
219         if (bus_dma_tag_create( NULL,                   /* parent */
220                                 1, 0,                   /* algnmnt, boundary */
221                                 BUS_SPACE_MAXADDR,      /* lowaddr */
222                                 BUS_SPACE_MAXADDR,      /* highaddr */
223                                 NULL, NULL,             /* filter, filterarg */
224                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
225                                 BUS_SPACE_UNRESTRICTED, /* nsegments */
226                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
227                                 0,                      /* flags */
228                                 NULL, NULL,             /* lockfunc, lockarg */
229                                 &sc->mfi_parent_dmat)) {
230                 device_printf(dev, "Cannot allocate parent DMA tag\n");
231                 goto out;
232         }
233
234         /* Allocate IRQ resource. */
235         sc->mfi_irq_rid = 0;
236         count = 1;
237         if (mfi_msi && pci_alloc_msi(sc->mfi_dev, &count) == 0) {
238                 device_printf(sc->mfi_dev, "Using MSI\n");
239                 sc->mfi_irq_rid = 1;
240         }
241         if ((sc->mfi_irq = bus_alloc_resource_any(sc->mfi_dev, SYS_RES_IRQ,
242             &sc->mfi_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
243                 device_printf(sc->mfi_dev, "Cannot allocate interrupt\n");
244                 error = EINVAL;
245                 goto out;
246         }
247
248         error = mfi_attach(sc);
249 out:
250         if (error) {
251                 mfi_free(sc);
252                 mfi_pci_free(sc);
253         }
254
255         return (error);
256 }
257
258 static int
259 mfi_pci_detach(device_t dev)
260 {
261         struct mfi_softc *sc;
262         struct mfi_disk *ld;
263         int error;
264
265         sc = device_get_softc(dev);
266
267         sx_xlock(&sc->mfi_config_lock);
268         mtx_lock(&sc->mfi_io_lock);
269         if ((sc->mfi_flags & MFI_FLAGS_OPEN) != 0) {
270                 mtx_unlock(&sc->mfi_io_lock);
271                 sx_xunlock(&sc->mfi_config_lock);
272                 return (EBUSY);
273         }
274         sc->mfi_detaching = 1;
275         mtx_unlock(&sc->mfi_io_lock);
276
277         while ((ld = TAILQ_FIRST(&sc->mfi_ld_tqh)) != NULL) {
278                 if ((error = device_delete_child(dev, ld->ld_dev)) != 0) {
279                         sc->mfi_detaching = 0;
280                         sx_xunlock(&sc->mfi_config_lock);
281                         return (error);
282                 }
283         }
284         sx_xunlock(&sc->mfi_config_lock);
285
286         EVENTHANDLER_DEREGISTER(shutdown_final, sc->mfi_eh);
287
288         mfi_shutdown(sc);
289         mfi_free(sc);
290         mfi_pci_free(sc);
291         return (0);
292 }
293
294 static void
295 mfi_pci_free(struct mfi_softc *sc)
296 {
297
298         if (sc->mfi_regs_resource != NULL) {
299                 bus_release_resource(sc->mfi_dev, SYS_RES_MEMORY,
300                     sc->mfi_regs_rid, sc->mfi_regs_resource);
301         }
302         if (sc->mfi_irq_rid != 0)
303                 pci_release_msi(sc->mfi_dev);
304
305         return;
306 }
307
308 static int
309 mfi_pci_suspend(device_t dev)
310 {
311
312         return (EINVAL);
313 }
314
315 static int
316 mfi_pci_resume(device_t dev)
317 {
318
319         return (EINVAL);
320 }