]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/mps/mps_pci.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / sys / dev / mps / mps_pci.c
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
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 /* PCI/PCI-X/PCIe bus interface for the Avago Tech (LSI) MPT2 controllers */
31
32 /* TODO Move headers to mpsvar */
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
42 #include <sys/uio.h>
43
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <sys/rman.h>
47
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pci_private.h>
51
52 #include <dev/mps/mpi/mpi2_type.h>
53 #include <dev/mps/mpi/mpi2.h>
54 #include <dev/mps/mpi/mpi2_ioc.h>
55 #include <dev/mps/mpi/mpi2_cnfg.h>
56 #include <dev/mps/mpi/mpi2_tool.h>
57
58 #include <sys/queue.h>
59 #include <sys/kthread.h>
60 #include <dev/mps/mps_ioctl.h>
61 #include <dev/mps/mpsvar.h>
62
63 static int      mps_pci_probe(device_t);
64 static int      mps_pci_attach(device_t);
65 static int      mps_pci_detach(device_t);
66 static int      mps_pci_suspend(device_t);
67 static int      mps_pci_resume(device_t);
68 static void     mps_pci_free(struct mps_softc *);
69 static int      mps_alloc_msix(struct mps_softc *sc, int msgs);
70 static int      mps_alloc_msi(struct mps_softc *sc, int msgs);
71 static int      mps_pci_alloc_interrupts(struct mps_softc *sc);
72
73 static device_method_t mps_methods[] = {
74         DEVMETHOD(device_probe,         mps_pci_probe),
75         DEVMETHOD(device_attach,        mps_pci_attach),
76         DEVMETHOD(device_detach,        mps_pci_detach),
77         DEVMETHOD(device_suspend,       mps_pci_suspend),
78         DEVMETHOD(device_resume,        mps_pci_resume),
79
80         DEVMETHOD_END
81 };
82
83 static driver_t mps_pci_driver = {
84         "mps",
85         mps_methods,
86         sizeof(struct mps_softc)
87 };
88
89 static devclass_t       mps_devclass;
90 DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, 0, 0);
91 MODULE_DEPEND(mps, cam, 1, 1, 1);
92
93 struct mps_ident {
94         uint16_t        vendor;
95         uint16_t        device;
96         uint16_t        subvendor;
97         uint16_t        subdevice;
98         u_int           flags;
99         const char      *desc;
100 } mps_identifiers[] = {
101         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
102             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2004" },
103         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
104             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2008" },
105         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
106             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2108" },
107         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
108             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2108" },
109         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
110             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2108" },
111         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
112             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2116" },
113         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
114             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2116" },
115         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
116             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
117         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
118             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
119         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
120             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
121         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
122             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
123         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
124             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
125         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
126             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
127         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
128             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2308" },
129         // Add Customer specific vender/subdevice id before generic
130         // (0xffff) vender/subdevice id.
131         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
132             0x8086, 0x3516, 0, "Intel(R) Integrated RAID Module RMS25JB080" },
133         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
134             0x8086, 0x3517, 0, "Intel(R) Integrated RAID Module RMS25JB040" },
135         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
136             0x8086, 0x3518, 0, "Intel(R) Integrated RAID Module RMS25KB080" },
137         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
138             0x8086, 0x3519, 0, "Intel(R) Integrated RAID Module RMS25KB040" },
139         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
140             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2308" },
141         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
142             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2308" },
143         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
144             0xffff, 0xffff, 0, "Avago Technologies (LSI) SSS6200" },
145         { 0, 0, 0, 0, 0, NULL }
146 };
147
148 static struct mps_ident *
149 mps_find_ident(device_t dev)
150 {
151         struct mps_ident *m;
152
153         for (m = mps_identifiers; m->vendor != 0; m++) {
154                 if (m->vendor != pci_get_vendor(dev))
155                         continue;
156                 if (m->device != pci_get_device(dev))
157                         continue;
158                 if ((m->subvendor != 0xffff) &&
159                     (m->subvendor != pci_get_subvendor(dev)))
160                         continue;
161                 if ((m->subdevice != 0xffff) &&
162                     (m->subdevice != pci_get_subdevice(dev)))
163                         continue;
164                 return (m);
165         }
166
167         return (NULL);
168 }
169
170 static int
171 mps_pci_probe(device_t dev)
172 {
173         struct mps_ident *id;
174
175         if ((id = mps_find_ident(dev)) != NULL) {
176                 device_set_desc(dev, id->desc);
177                 return (BUS_PROBE_DEFAULT);
178         }
179         return (ENXIO);
180 }
181
182 static int
183 mps_pci_attach(device_t dev)
184 {
185         struct mps_softc *sc;
186         struct mps_ident *m;
187         int error;
188
189         sc = device_get_softc(dev);
190         bzero(sc, sizeof(*sc));
191         sc->mps_dev = dev;
192         m = mps_find_ident(dev);
193         sc->mps_flags = m->flags;
194
195         mps_get_tunables(sc);
196
197         /* Twiddle basic PCI config bits for a sanity check */
198         pci_enable_busmaster(dev);
199
200         /* Allocate the System Interface Register Set */
201         sc->mps_regs_rid = PCIR_BAR(1);
202         if ((sc->mps_regs_resource = bus_alloc_resource_any(dev,
203             SYS_RES_MEMORY, &sc->mps_regs_rid, RF_ACTIVE)) == NULL) {
204                 mps_printf(sc, "Cannot allocate PCI registers\n");
205                 return (ENXIO);
206         }
207         sc->mps_btag = rman_get_bustag(sc->mps_regs_resource);
208         sc->mps_bhandle = rman_get_bushandle(sc->mps_regs_resource);
209
210         /* Allocate the parent DMA tag */
211         if (bus_dma_tag_create( bus_get_dma_tag(dev),   /* 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                                 NULL, NULL,             /* lockfunc, lockarg */
221                                 &sc->mps_parent_dmat)) {
222                 mps_printf(sc, "Cannot allocate parent DMA tag\n");
223                 mps_pci_free(sc);
224                 return (ENOMEM);
225         }
226
227         if (((error = mps_pci_alloc_interrupts(sc)) != 0) ||
228             ((error = mps_attach(sc)) != 0))
229                 mps_pci_free(sc);
230
231         return (error);
232 }
233
234 /*
235  * Allocate, but don't assign interrupts early.  Doing it before requesting
236  * the IOCFacts message informs the firmware that we want to do MSI-X
237  * multiqueue.  We might not use all of the available messages, but there's
238  * no reason to re-alloc if we don't.
239  */
240 static int
241 mps_pci_alloc_interrupts(struct mps_softc *sc)
242 {
243         device_t dev;
244         int error, msgs;
245
246         dev = sc->mps_dev;
247         error = 0;
248         msgs = 0;
249
250         if ((sc->disable_msix == 0) &&
251             ((msgs = pci_msix_count(dev)) >= MPS_MSI_COUNT))
252                 error = mps_alloc_msix(sc, MPS_MSI_COUNT);
253         if ((error != 0) && (sc->disable_msi == 0) &&
254             ((msgs = pci_msi_count(dev)) >= MPS_MSI_COUNT))
255                 error = mps_alloc_msi(sc, MPS_MSI_COUNT);
256         if (error != 0)
257                 msgs = 0;
258
259         sc->msi_msgs = msgs;
260         return (error);
261 }
262
263 int
264 mps_pci_setup_interrupts(struct mps_softc *sc)
265 {
266         device_t dev;
267         int i, error;
268
269         dev = sc->mps_dev;
270         error = ENXIO;
271
272         if (sc->msi_msgs == 0) {
273                 sc->mps_flags |= MPS_FLAGS_INTX;
274                 sc->mps_irq_rid[0] = 0;
275                 sc->mps_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
276                     &sc->mps_irq_rid[0],  RF_SHAREABLE | RF_ACTIVE);
277                 if (sc->mps_irq[0] == NULL) {
278                         mps_printf(sc, "Cannot allocate INTx interrupt\n");
279                         return (ENXIO);
280                 }
281                 error = bus_setup_intr(dev, sc->mps_irq[0],
282                     INTR_TYPE_BIO | INTR_MPSAFE, NULL, mps_intr, sc,
283                     &sc->mps_intrhand[0]);
284                 if (error)
285                         mps_printf(sc, "Cannot setup INTx interrupt\n");
286         } else {
287                 sc->mps_flags |= MPS_FLAGS_MSI;
288                 for (i = 0; i < MPS_MSI_COUNT; i++) {
289                         sc->mps_irq_rid[i] = i + 1;
290                         sc->mps_irq[i] = bus_alloc_resource_any(dev,
291                             SYS_RES_IRQ, &sc->mps_irq_rid[i], RF_ACTIVE);
292                         if (sc->mps_irq[i] == NULL) {
293                                 mps_printf(sc,
294                                     "Cannot allocate MSI interrupt\n");
295                                 return (ENXIO);
296                         }
297                         error = bus_setup_intr(dev, sc->mps_irq[i],
298                             INTR_TYPE_BIO | INTR_MPSAFE, NULL, mps_intr_msi,
299                             sc, &sc->mps_intrhand[i]);
300                         if (error) {
301                                 mps_printf(sc,
302                                     "Cannot setup MSI interrupt %d\n", i);
303                                 break;
304                         }
305                 }
306         }
307
308         return (error);
309 }
310
311 static int
312 mps_pci_detach(device_t dev)
313 {
314         struct mps_softc *sc;
315         int error;
316
317         sc = device_get_softc(dev);
318
319         if ((error = mps_free(sc)) != 0)
320                 return (error);
321
322         mps_pci_free(sc);
323         return (0);
324 }
325
326 static void
327 mps_pci_free(struct mps_softc *sc)
328 {
329         int i;
330
331         if (sc->mps_parent_dmat != NULL) {
332                 bus_dma_tag_destroy(sc->mps_parent_dmat);
333         }
334
335         if (sc->mps_flags & MPS_FLAGS_MSI) {
336                 for (i = 0; i < MPS_MSI_COUNT; i++) {
337                         if (sc->mps_irq[i] != NULL) {
338                                 bus_teardown_intr(sc->mps_dev, sc->mps_irq[i],
339                                     sc->mps_intrhand[i]);
340                                 bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
341                                     sc->mps_irq_rid[i], sc->mps_irq[i]);
342                         }
343                 }
344                 pci_release_msi(sc->mps_dev);
345         }
346
347         if (sc->mps_flags & MPS_FLAGS_INTX) {
348                 bus_teardown_intr(sc->mps_dev, sc->mps_irq[0],
349                     sc->mps_intrhand[0]);
350                 bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
351                     sc->mps_irq_rid[0], sc->mps_irq[0]);
352         }
353
354         if (sc->mps_regs_resource != NULL) {
355                 bus_release_resource(sc->mps_dev, SYS_RES_MEMORY,
356                     sc->mps_regs_rid, sc->mps_regs_resource);
357         }
358
359         return;
360 }
361
362 static int
363 mps_pci_suspend(device_t dev)
364 {
365         return (EINVAL);
366 }
367
368 static int
369 mps_pci_resume(device_t dev)
370 {
371         return (EINVAL);
372 }
373
374 static int
375 mps_alloc_msix(struct mps_softc *sc, int msgs)
376 {
377         int error;
378
379         error = pci_alloc_msix(sc->mps_dev, &msgs);
380         return (error);
381 }
382
383 static int
384 mps_alloc_msi(struct mps_softc *sc, int msgs)
385 {
386         int error;
387
388         error = pci_alloc_msi(sc->mps_dev, &msgs);
389         return (error);
390 }
391
392 int
393 mps_pci_restore(struct mps_softc *sc)
394 {
395         struct pci_devinfo *dinfo;
396
397         mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
398
399         dinfo = device_get_ivars(sc->mps_dev);
400         if (dinfo == NULL) {
401                 mps_dprint(sc, MPS_FAULT, "%s: NULL dinfo\n", __func__);
402                 return (EINVAL);
403         }
404
405         pci_cfg_restore(sc->mps_dev, dinfo);
406         return (0);
407 }
408