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