3a93b81044c9ece969cd31c85c6a98b74368bcdc
[dragonfly.git] / sys / dev / disk / 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  * $FreeBSD: src/sys/dev/mps/mps_pci.c,v 1.2 2010/11/30 22:39:46 ken Exp $
27  */
28
29 /* PCI/PCI-X/PCIe bus interface for the LSI MPT2 controllers */
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <sys/uio.h>
41
42 #include <sys/rman.h>
43 #include <sys/lock.h>
44
45 #include <bus/pci/pcireg.h>
46 #include <bus/pci/pcivar.h>
47
48 #include <dev/disk/mps/mpi/mpi2_type.h>
49 #include <dev/disk/mps/mpi/mpi2.h>
50 #include <dev/disk/mps/mpi/mpi2_ioc.h>
51 #include <dev/disk/mps/mpi/mpi2_cnfg.h>
52
53 #include <dev/disk/mps/mpsvar.h>
54
55 static int      mps_pci_probe(device_t);
56 static int      mps_pci_attach(device_t);
57 static int      mps_pci_detach(device_t);
58 static int      mps_pci_suspend(device_t);
59 static int      mps_pci_resume(device_t);
60 static void     mps_pci_free(struct mps_softc *);
61 static int      mps_alloc_msix(struct mps_softc *sc, int msgs);
62 static int      mps_alloc_msi(struct mps_softc *sc, int msgs);
63
64 int mps_disable_msix = 0;
65 TUNABLE_INT("hw.mps.disable_msix", &mps_disable_msix);
66 SYSCTL_INT(_hw_mps, OID_AUTO, disable_msix, CTLFLAG_RD, &mps_disable_msix, 0,
67     "Disable MSIX interrupts\n");
68 int mps_disable_msi = 0;
69 TUNABLE_INT("hw.mps.disable_msi", &mps_disable_msi);
70 SYSCTL_INT(_hw_mps, OID_AUTO, disable_msi, CTLFLAG_RD, &mps_disable_msi, 0,
71     "Disable MSI interrupts\n");
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         DEVMETHOD(bus_print_child,      bus_generic_print_child),
80         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
81         { 0, 0 }
82 };
83
84 static driver_t mps_pci_driver = {
85         "mps",
86         mps_methods,
87         sizeof(struct mps_softc)
88 };
89
90 static devclass_t       mps_devclass;
91 DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, NULL, NULL);
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, "LSI SAS2004" },
103         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
104             0xffff, 0xffff, 0, "LSI SAS2008" },
105         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
106             0xffff, 0xffff, 0, "LSI SAS2108" },
107         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
108             0xffff, 0xffff, 0, "LSI SAS2108" },
109         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
110             0xffff, 0xffff, 0, "LSI SAS2108" },
111         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
112             0xffff, 0xffff, 0, "LSI SAS2116" },
113         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
114             0xffff, 0xffff, 0, "LSI SAS2116" },
115         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
116             0xffff, 0xffff, 0, "LSI SAS2208" },
117         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
118             0xffff, 0xffff, 0, "LSI SAS2208" },
119         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
120             0xffff, 0xffff, 0, "LSI SAS2208" },
121         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
122             0xffff, 0xffff, 0, "LSI SAS2208" },
123         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
124             0xffff, 0xffff, 0, "LSI SAS2208" },
125         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
126             0xffff, 0xffff, 0, "LSI SAS2208" },
127         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
128             0xffff, 0xffff, 0, "LSI SAS2208" },
129         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
130             0xffff, 0xffff, 0, "LSI SAS2208" },
131         { 0, 0, 0, 0, 0, NULL }
132 };
133
134 static struct mps_ident *
135 mps_find_ident(device_t dev)
136 {
137         struct mps_ident *m;
138
139         for (m = mps_identifiers; m->vendor != 0; m++) {
140                 if (m->vendor != pci_get_vendor(dev))
141                         continue;
142                 if (m->device != pci_get_device(dev))
143                         continue;
144                 if ((m->subvendor != 0xffff) &&
145                     (m->subvendor != pci_get_subvendor(dev)))
146                         continue;
147                 if ((m->subdevice != 0xffff) &&
148                     (m->subdevice != pci_get_subdevice(dev)))
149                         continue;
150                 return (m);
151         }
152
153         return (NULL);
154 }
155
156 static int
157 mps_pci_probe(device_t dev)
158 {
159         struct mps_ident *id;
160
161         if ((id = mps_find_ident(dev)) != NULL) {
162                 device_set_desc(dev, id->desc);
163                 return (BUS_PROBE_DEFAULT);
164         }
165         return (ENXIO);
166 }
167
168 static int
169 mps_pci_attach(device_t dev)
170 {
171         struct mps_softc *sc;
172         struct mps_ident *m;
173         uint16_t command;
174         int error;
175
176         sc = device_get_softc(dev);
177         bzero(sc, sizeof(*sc));
178         sc->mps_dev = dev;
179         m = mps_find_ident(dev);
180         sc->mps_flags = m->flags;
181
182         /* Twiddle basic PCI config bits for a sanity check */
183         command = pci_read_config(dev, PCIR_COMMAND, 2);
184         command |= PCIM_CMD_BUSMASTEREN;
185         pci_write_config(dev, PCIR_COMMAND, command, 2);
186         command = pci_read_config(dev, PCIR_COMMAND, 2);
187         if ((command & PCIM_CMD_BUSMASTEREN) == 0) {
188                 device_printf(dev, "Cannot enable PCI busmaster\n");
189                 return (ENXIO);
190         }
191         if ((command & PCIM_CMD_MEMEN) == 0) {
192                 device_printf(dev, "PCI memory window not available\n");
193                 return (ENXIO);
194         }
195
196         /* Allocate the System Interface Register Set */
197         sc->mps_regs_rid = PCIR_BAR(1);
198         if ((sc->mps_regs_resource = bus_alloc_resource_any(dev,
199             SYS_RES_MEMORY, &sc->mps_regs_rid, RF_ACTIVE)) == NULL) {
200                 device_printf(dev, "Cannot allocate PCI registers\n");
201                 return (ENXIO);
202         }
203         sc->mps_btag = rman_get_bustag(sc->mps_regs_resource);
204         sc->mps_bhandle = rman_get_bushandle(sc->mps_regs_resource);
205
206         /* Allocate the parent DMA tag */
207         if (bus_dma_tag_create( NULL,                   /* parent */
208                                 1, 0,                   /* algnmnt, boundary */
209                                 BUS_SPACE_MAXADDR,      /* lowaddr */
210                                 BUS_SPACE_MAXADDR,      /* highaddr */
211                                 NULL, NULL,             /* filter, filterarg */
212                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
213                                 BUS_SPACE_UNRESTRICTED, /* nsegments */
214                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
215                                 0,                      /* flags */
216                                 &sc->mps_parent_dmat)) {
217                 device_printf(dev, "Cannot allocate parent DMA tag\n");
218                 mps_pci_free(sc);
219                 return (ENOMEM);
220         }
221
222         if ((error = mps_attach(sc)) != 0)
223                 mps_pci_free(sc);
224
225         return (error);
226 }
227
228 int
229 mps_pci_setup_interrupts(struct mps_softc *sc)
230 {
231         device_t dev;
232         int i, error, msgs;
233
234         dev = sc->mps_dev;
235         error = ENXIO;
236         if ((mps_disable_msix == 0) &&
237             ((msgs = pci_msix_count(dev)) >= MPS_MSI_COUNT))
238                 error = mps_alloc_msix(sc, MPS_MSI_COUNT);
239         if ((error != 0) && (mps_disable_msi == 0) &&
240             ((msgs = pci_msi_count(dev)) >= MPS_MSI_COUNT))
241                 error = mps_alloc_msi(sc, MPS_MSI_COUNT);
242
243         if (error != 0) {
244                 sc->mps_flags |= MPS_FLAGS_INTX;
245                 sc->mps_irq_rid[0] = 0;
246                 sc->mps_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
247                     &sc->mps_irq_rid[0],  RF_SHAREABLE | RF_ACTIVE);
248                 if (sc->mps_irq[0] == NULL) {
249                         device_printf(dev, "Cannot allocate INTx interrupt\n");
250                         return (ENXIO);
251                 }
252                 error = bus_setup_intr(dev, sc->mps_irq[0],
253                     INTR_MPSAFE, mps_intr, sc,
254                     &sc->mps_intrhand[0], NULL);
255                 if (error)
256                         device_printf(dev, "Cannot setup INTx interrupt\n");
257         } else {
258                 sc->mps_flags |= MPS_FLAGS_MSI;
259                 for (i = 0; i < MPS_MSI_COUNT; i++) {
260                         sc->mps_irq_rid[i] = i + 1;
261                         sc->mps_irq[i] = bus_alloc_resource_any(dev,
262                             SYS_RES_IRQ, &sc->mps_irq_rid[i], RF_ACTIVE);
263                         if (sc->mps_irq[i] == NULL) {
264                                 device_printf(dev,
265                                     "Cannot allocate MSI interrupt\n");
266                                 return (ENXIO);
267                         }
268                         error = bus_setup_intr(dev, sc->mps_irq[i],
269                             INTR_MPSAFE, mps_intr_msi,
270                             sc, &sc->mps_intrhand[i], NULL);
271                         if (error) {
272                                 device_printf(dev,
273                                     "Cannot setup MSI interrupt %d\n", i);
274                                 break;
275                         }
276                 }
277         }
278
279         return (error);
280 }
281
282 static int
283 mps_pci_detach(device_t dev)
284 {
285         struct mps_softc *sc;
286         int error;
287
288         sc = device_get_softc(dev);
289
290         if ((error = mps_free(sc)) != 0)
291                 return (error);
292
293         mps_pci_free(sc);
294         return (0);
295 }
296
297 static void
298 mps_pci_free(struct mps_softc *sc)
299 {
300         int i;
301
302         if (sc->mps_parent_dmat != NULL) {
303                 bus_dma_tag_destroy(sc->mps_parent_dmat);
304         }
305
306         if (sc->mps_flags & MPS_FLAGS_MSI) {
307                 for (i = 0; i < MPS_MSI_COUNT; i++) {
308                         if (sc->mps_irq[i] != NULL) {
309                                 bus_teardown_intr(sc->mps_dev, sc->mps_irq[i],
310                                     sc->mps_intrhand[i]);
311                                 bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
312                                     sc->mps_irq_rid[i], sc->mps_irq[i]);
313                         }
314                 }
315                 pci_release_msi(sc->mps_dev);
316         }
317
318         if (sc->mps_flags & MPS_FLAGS_INTX) {
319                 bus_teardown_intr(sc->mps_dev, sc->mps_irq[0],
320                     sc->mps_intrhand[0]);
321                 bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
322                     sc->mps_irq_rid[0], sc->mps_irq[0]);
323         }
324
325         if (sc->mps_regs_resource != NULL) {
326                 bus_release_resource(sc->mps_dev, SYS_RES_MEMORY,
327                     sc->mps_regs_rid, sc->mps_regs_resource);
328         }
329
330         return;
331 }
332
333 static int
334 mps_pci_suspend(device_t dev)
335 {
336         return (EINVAL);
337 }
338
339 static int
340 mps_pci_resume(device_t dev)
341 {
342         return (EINVAL);
343 }
344
345 static int
346 mps_alloc_msix(struct mps_softc *sc, int msgs)
347 {
348         int error;
349
350         error = pci_alloc_msix(sc->mps_dev, &msgs);
351         return (error);
352 }
353
354 static int
355 mps_alloc_msi(struct mps_softc *sc, int msgs)
356 {
357         int error;
358
359         error = pci_alloc_msi(sc->mps_dev, &msgs);
360         return (error);
361 }