]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mpr/mpr_pci.c
linux: Make PTRACE_GET_SYSCALL_INFO handle EJUSTRETURN
[FreeBSD/FreeBSD.git] / sys / dev / mpr / mpr_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) MPT3 controllers */
31
32 /* TODO Move headers to mprvar */
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/mpr/mpi/mpi2_type.h>
53 #include <dev/mpr/mpi/mpi2.h>
54 #include <dev/mpr/mpi/mpi2_ioc.h>
55 #include <dev/mpr/mpi/mpi2_cnfg.h>
56 #include <dev/mpr/mpi/mpi2_tool.h>
57 #include <dev/mpr/mpi/mpi2_pci.h>
58
59 #include <sys/queue.h>
60 #include <sys/kthread.h>
61 #include <dev/mpr/mpr_ioctl.h>
62 #include <dev/mpr/mprvar.h>
63
64 static int      mpr_pci_probe(device_t);
65 static int      mpr_pci_attach(device_t);
66 static int      mpr_pci_detach(device_t);
67 static int      mpr_pci_suspend(device_t);
68 static int      mpr_pci_resume(device_t);
69 static void     mpr_pci_free(struct mpr_softc *);
70 static int      mpr_alloc_msix(struct mpr_softc *sc, int msgs);
71 static int      mpr_alloc_msi(struct mpr_softc *sc, int msgs);
72 static int      mpr_pci_alloc_interrupts(struct mpr_softc *sc);
73
74 static device_method_t mpr_methods[] = {
75         DEVMETHOD(device_probe,         mpr_pci_probe),
76         DEVMETHOD(device_attach,        mpr_pci_attach),
77         DEVMETHOD(device_detach,        mpr_pci_detach),
78         DEVMETHOD(device_suspend,       mpr_pci_suspend),
79         DEVMETHOD(device_resume,        mpr_pci_resume),
80         DEVMETHOD(bus_print_child,      bus_generic_print_child),
81         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
82         { 0, 0 }
83 };
84
85 static driver_t mpr_pci_driver = {
86         "mpr",
87         mpr_methods,
88         sizeof(struct mpr_softc)
89 };
90
91 struct mpr_ident {
92         uint16_t        vendor;
93         uint16_t        device;
94         uint16_t        subvendor;
95         uint16_t        subdevice;
96         u_int           flags;
97         const char      *desc;
98 } mpr_identifiers[] = {
99         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
100             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3004" },
101         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
102             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3008" },
103         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
104             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_1" },
105         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
106             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_2" },
107         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
108             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_5" },
109         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
110             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3108_6" },
111         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
112             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3216" },
113         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
114             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3224" },
115         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
116             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3316_1" },
117         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
118             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3316_2" },
119         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
120             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3324_1" },
121         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
122             0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS3324_2" },
123         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
124             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
125             "Avago Technologies (LSI) SAS3408" },
126         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
127             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
128             "Avago Technologies (LSI) SAS3416" },
129         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
130             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
131             "Avago Technologies (LSI) SAS3508" },
132         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
133             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
134             "Avago Technologies (LSI) SAS3508_1" },
135         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
136             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
137             "Avago Technologies (LSI) SAS3516" },
138         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
139             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
140             "Avago Technologies (LSI) SAS3516_1" },
141         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3616,
142             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
143             "Avago Technologies (LSI) SAS3616" },
144         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3708,
145             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
146             "Avago Technologies (LSI) SAS3708" },
147         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3716,
148             0xffff, 0xffff, MPR_FLAGS_GEN35_IOC,
149             "Avago Technologies (LSI) SAS3716" },
150         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_SAS3816,
151             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
152             "Broadcom Inc. (LSI) INVALID0 SAS3816" },
153         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3816,
154             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
155             "Broadcom Inc. (LSI) CFG SEC SAS3816" },
156         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_SAS3816,
157             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
158             "Broadcom Inc. (LSI) HARD SEC SAS3816" },
159         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_SAS3816,
160             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
161             "Broadcom Inc. (LSI) INVALID1 SAS3816" },
162         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_SAS3916,
163             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
164             "Broadcom Inc. (LSI) INVALID0 SAS3916" },
165         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3916,
166             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
167             "Broadcom Inc. (LSI) CFG SEC SAS3916" },
168         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_SAS3916,
169             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
170             "Broadcom Inc. (LSI) HARD SEC SAS3916" },
171         { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_SAS3916,
172             0xffff, 0xffff, (MPR_FLAGS_GEN35_IOC | MPR_FLAGS_SEA_IOC),
173             "Broadcom Inc. (LSI) INVALID1 SAS3916" },
174         { 0, 0, 0, 0, 0, NULL }
175 };
176
177 static devclass_t       mpr_devclass;
178 DRIVER_MODULE(mpr, pci, mpr_pci_driver, mpr_devclass, 0, 0);
179 MODULE_PNP_INFO("U16:vendor;U16:device;U16:subvendor;U16:subdevice;D:#", pci,
180     mpr, mpr_identifiers, nitems(mpr_identifiers) - 1);
181
182 MODULE_DEPEND(mpr, cam, 1, 1, 1);
183
184 static struct mpr_ident *
185 mpr_find_ident(device_t dev)
186 {
187         struct mpr_ident *m;
188
189         for (m = mpr_identifiers; m->vendor != 0; m++) {
190                 if (m->vendor != pci_get_vendor(dev))
191                         continue;
192                 if (m->device != pci_get_device(dev))
193                         continue;
194                 if ((m->subvendor != 0xffff) &&
195                     (m->subvendor != pci_get_subvendor(dev)))
196                         continue;
197                 if ((m->subdevice != 0xffff) &&
198                     (m->subdevice != pci_get_subdevice(dev)))
199                         continue;
200                 return (m);
201         }
202
203         return (NULL);
204 }
205
206 static int
207 mpr_pci_probe(device_t dev)
208 {
209         struct mpr_ident *id;
210
211         if ((id = mpr_find_ident(dev)) != NULL) {
212                 device_set_desc(dev, id->desc);
213                 return (BUS_PROBE_DEFAULT);
214         }
215         return (ENXIO);
216 }
217
218 static int
219 mpr_pci_attach(device_t dev)
220 {
221         bus_dma_template_t t;
222         struct mpr_softc *sc;
223         struct mpr_ident *m;
224         int error, i;
225
226         sc = device_get_softc(dev);
227         bzero(sc, sizeof(*sc));
228         sc->mpr_dev = dev;
229         m = mpr_find_ident(dev);
230         sc->mpr_flags = m->flags;
231
232         switch (m->device) {
233         case MPI26_MFGPAGE_DEVID_INVALID0_SAS3816:
234         case MPI26_MFGPAGE_DEVID_INVALID1_SAS3816:
235         case MPI26_MFGPAGE_DEVID_INVALID0_SAS3916:
236         case MPI26_MFGPAGE_DEVID_INVALID1_SAS3916:
237                 mpr_printf(sc, "HBA is in Non Secure mode\n");
238                 return (ENXIO);
239         case MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3816:
240         case MPI26_MFGPAGE_DEVID_CFG_SEC_SAS3916:
241                 mpr_printf(sc, "HBA is in Configurable Secure mode\n");
242                 break;
243         default:
244                 break;
245         }
246
247         mpr_get_tunables(sc);
248
249         /* Twiddle basic PCI config bits for a sanity check */
250         pci_enable_busmaster(dev);
251
252         for (i = 0; i < PCI_MAXMAPS_0; i++) {
253                 sc->mpr_regs_rid = PCIR_BAR(i);
254
255                 if ((sc->mpr_regs_resource = bus_alloc_resource_any(dev,
256                     SYS_RES_MEMORY, &sc->mpr_regs_rid, RF_ACTIVE)) != NULL)
257                         break;
258         }
259
260         if (sc->mpr_regs_resource == NULL) {
261                 mpr_printf(sc, "Cannot allocate PCI registers\n");
262                 return (ENXIO);
263         }
264
265         sc->mpr_btag = rman_get_bustag(sc->mpr_regs_resource);
266         sc->mpr_bhandle = rman_get_bushandle(sc->mpr_regs_resource);
267
268         /* Allocate the parent DMA tag */
269         bus_dma_template_init(&t, bus_get_dma_tag(dev));
270         if (bus_dma_template_tag(&t, &sc->mpr_parent_dmat)) {
271                 mpr_printf(sc, "Cannot allocate parent DMA tag\n");
272                 mpr_pci_free(sc);
273                 return (ENOMEM);
274         }
275
276         if (((error = mpr_pci_alloc_interrupts(sc)) != 0) ||
277             ((error = mpr_attach(sc)) != 0))
278                 mpr_pci_free(sc);
279
280         return (error);
281 }
282
283 /*
284  * Allocate, but don't assign interrupts early.  Doing it before requesting
285  * the IOCFacts message informs the firmware that we want to do MSI-X
286  * multiqueue.  We might not use all of the available messages, but there's
287  * no reason to re-alloc if we don't.
288  */
289 int
290 mpr_pci_alloc_interrupts(struct mpr_softc *sc)
291 {
292         device_t dev;
293         int error, msgs;
294
295         dev = sc->mpr_dev;
296         error = 0;
297         msgs = 0;
298
299         if (sc->disable_msix == 0) {
300                 msgs = pci_msix_count(dev);
301                 mpr_dprint(sc, MPR_INIT, "Counted %d MSI-X messages\n", msgs);
302                 msgs = min(msgs, sc->max_msix);
303                 msgs = min(msgs, MPR_MSIX_MAX);
304                 msgs = min(msgs, 1);    /* XXX */
305                 if (msgs != 0) {
306                         mpr_dprint(sc, MPR_INIT, "Attempting to allocate %d "
307                             "MSI-X messages\n", msgs);
308                         error = mpr_alloc_msix(sc, msgs);
309                 }
310         }
311         if (((error != 0) || (msgs == 0)) && (sc->disable_msi == 0)) {
312                 msgs = pci_msi_count(dev);
313                 mpr_dprint(sc, MPR_INIT, "Counted %d MSI messages\n", msgs);
314                 msgs = min(msgs, MPR_MSI_MAX);
315                 if (msgs != 0) {
316                         mpr_dprint(sc, MPR_INIT, "Attempting to allocated %d "
317                             "MSI messages\n", MPR_MSI_MAX);
318                         error = mpr_alloc_msi(sc, MPR_MSI_MAX);
319                 }
320         }
321         if ((error != 0) || (msgs == 0)) {
322                 /*
323                  * If neither MSI or MSI-X are available, assume legacy INTx.
324                  * This also implies that there will be only 1 queue.
325                  */
326                 mpr_dprint(sc, MPR_INIT, "Falling back to legacy INTx\n");
327                 sc->mpr_flags |= MPR_FLAGS_INTX;
328                 msgs = 1;
329         } else
330                 sc->mpr_flags |= MPR_FLAGS_MSI;
331
332         sc->msi_msgs = msgs;
333         mpr_dprint(sc, MPR_INIT, "Allocated %d interrupts\n", msgs);
334
335         return (error);
336 }
337
338 int
339 mpr_pci_setup_interrupts(struct mpr_softc *sc)
340 {
341         device_t dev;
342         struct mpr_queue *q;
343         void *ihandler;
344         int i, error, rid, initial_rid;
345
346         dev = sc->mpr_dev;
347         error = ENXIO;
348
349         if (sc->mpr_flags & MPR_FLAGS_INTX) {
350                 initial_rid = 0;
351                 ihandler = mpr_intr;
352         } else if (sc->mpr_flags & MPR_FLAGS_MSI) {
353                 initial_rid = 1;
354                 ihandler = mpr_intr_msi;
355         } else {
356                 mpr_dprint(sc, MPR_ERROR|MPR_INIT,
357                     "Unable to set up interrupts\n");
358                 return (EINVAL);
359         }
360
361         for (i = 0; i < sc->msi_msgs; i++) {
362                 q = &sc->queues[i];
363                 rid = i + initial_rid;
364                 q->irq_rid = rid;
365                 q->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
366                     &q->irq_rid, RF_ACTIVE);
367                 if (q->irq == NULL) {
368                         mpr_dprint(sc, MPR_ERROR|MPR_INIT,
369                             "Cannot allocate interrupt RID %d\n", rid);
370                         sc->msi_msgs = i;
371                         break;
372                 }
373                 error = bus_setup_intr(dev, q->irq,
374                     INTR_TYPE_BIO | INTR_MPSAFE, NULL, ihandler,
375                     sc, &q->intrhand);
376                 if (error) {
377                         mpr_dprint(sc, MPR_ERROR|MPR_INIT,
378                             "Cannot setup interrupt RID %d\n", rid);
379                         sc->msi_msgs = i;
380                         break;
381                 }
382         }
383
384         mpr_dprint(sc, MPR_INIT, "Set up %d interrupts\n", sc->msi_msgs);
385         return (error);
386 }
387
388 static int
389 mpr_pci_detach(device_t dev)
390 {
391         struct mpr_softc *sc;
392         int error;
393
394         sc = device_get_softc(dev);
395
396         if ((error = mpr_free(sc)) != 0)
397                 return (error);
398
399         mpr_pci_free(sc);
400         return (0);
401 }
402
403 void
404 mpr_pci_free_interrupts(struct mpr_softc *sc)
405 {
406         struct mpr_queue *q;
407         int i;
408
409         if (sc->queues == NULL)
410                 return;
411
412         for (i = 0; i < sc->msi_msgs; i++) {
413                 q = &sc->queues[i];
414                 if (q->irq != NULL) {
415                         bus_teardown_intr(sc->mpr_dev, q->irq,
416                             q->intrhand);
417                         bus_release_resource(sc->mpr_dev, SYS_RES_IRQ,
418                             q->irq_rid, q->irq);
419                 }
420         }
421 }
422
423 static void
424 mpr_pci_free(struct mpr_softc *sc)
425 {
426
427         if (sc->mpr_parent_dmat != NULL) {
428                 bus_dma_tag_destroy(sc->mpr_parent_dmat);
429         }
430
431         mpr_pci_free_interrupts(sc);
432
433         if (sc->mpr_flags & MPR_FLAGS_MSI)
434                 pci_release_msi(sc->mpr_dev);
435
436         if (sc->mpr_regs_resource != NULL) {
437                 bus_release_resource(sc->mpr_dev, SYS_RES_MEMORY,
438                     sc->mpr_regs_rid, sc->mpr_regs_resource);
439         }
440
441         return;
442 }
443
444 static int
445 mpr_pci_suspend(device_t dev)
446 {
447         return (EINVAL);
448 }
449
450 static int
451 mpr_pci_resume(device_t dev)
452 {
453         return (EINVAL);
454 }
455
456 static int
457 mpr_alloc_msix(struct mpr_softc *sc, int msgs)
458 {
459         int error;
460
461         error = pci_alloc_msix(sc->mpr_dev, &msgs);
462         return (error);
463 }
464
465 static int
466 mpr_alloc_msi(struct mpr_softc *sc, int msgs)
467 {
468         int error;
469
470         error = pci_alloc_msi(sc->mpr_dev, &msgs);
471         return (error);
472 }
473
474 int
475 mpr_pci_restore(struct mpr_softc *sc)
476 {
477         struct pci_devinfo *dinfo;
478
479         mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
480
481         dinfo = device_get_ivars(sc->mpr_dev);
482         if (dinfo == NULL) {
483                 mpr_dprint(sc, MPR_FAULT, "%s: NULL dinfo\n", __func__);
484                 return (EINVAL);
485         }
486
487         pci_cfg_restore(sc->mpr_dev, dinfo);
488         return (0);
489 }