nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / raid / mps / mps_pci.c
CommitLineData
ad8cf91c
SW
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 *
c12c399a 26 * $FreeBSD: src/sys/dev/mps/mps_pci.c,v 1.4 2012/01/26 18:17:21 ken Exp $
ad8cf91c
SW
27 */
28
29/* PCI/PCI-X/PCIe bus interface for the LSI MPT2 controllers */
30
c12c399a 31/* TODO Move headers to mpsvar */
ad8cf91c
SW
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
43f7c553 37#include <sys/endian.h>
ad8cf91c
SW
38#include <sys/bus.h>
39#include <sys/conf.h>
c12c399a 40#include <sys/eventhandler.h>
ad8cf91c
SW
41#include <sys/malloc.h>
42#include <sys/sysctl.h>
43#include <sys/uio.h>
44
45#include <sys/rman.h>
ad8cf91c
SW
46
47#include <bus/pci/pcireg.h>
48#include <bus/pci/pcivar.h>
c12c399a 49#include <bus/pci/pci_private.h>
ad8cf91c 50
c12c399a
SW
51#include <dev/raid/mps/mpi/mpi2_type.h>
52#include <dev/raid/mps/mpi/mpi2.h>
53#include <dev/raid/mps/mpi/mpi2_ioc.h>
54#include <dev/raid/mps/mpi/mpi2_cnfg.h>
55#include <dev/raid/mps/mpi/mpi2_tool.h>
ad8cf91c 56
c12c399a
SW
57#include <sys/queue.h>
58#include <sys/kthread.h>
59#include <dev/raid/mps/mps_ioctl.h>
60#include <dev/raid/mps/mpsvar.h>
ad8cf91c
SW
61
62static int mps_pci_probe(device_t);
63static int mps_pci_attach(device_t);
64static int mps_pci_detach(device_t);
65static int mps_pci_suspend(device_t);
66static int mps_pci_resume(device_t);
67static void mps_pci_free(struct mps_softc *);
c12c399a 68#if 0 /* XXX swildner */
ad8cf91c 69static int mps_alloc_msix(struct mps_softc *sc, int msgs);
9dc47ee5 70#endif
ad8cf91c 71
ad8cf91c
SW
72static device_method_t mps_methods[] = {
73 DEVMETHOD(device_probe, mps_pci_probe),
74 DEVMETHOD(device_attach, mps_pci_attach),
75 DEVMETHOD(device_detach, mps_pci_detach),
76 DEVMETHOD(device_suspend, mps_pci_suspend),
77 DEVMETHOD(device_resume, mps_pci_resume),
c12c399a 78
d3c9c58e 79 DEVMETHOD_END
ad8cf91c
SW
80};
81
82static driver_t mps_pci_driver = {
83 "mps",
84 mps_methods,
85 sizeof(struct mps_softc)
86};
87
88static devclass_t mps_devclass;
dfc199f7 89DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, NULL, NULL);
259d5c76 90MODULE_VERSION(mps, 1);
ad8cf91c
SW
91
92struct mps_ident {
93 uint16_t vendor;
94 uint16_t device;
95 uint16_t subvendor;
96 uint16_t subdevice;
97 u_int flags;
98 const char *desc;
99} mps_identifiers[] = {
100 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
101 0xffff, 0xffff, 0, "LSI SAS2004" },
102 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
103 0xffff, 0xffff, 0, "LSI SAS2008" },
104 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
105 0xffff, 0xffff, 0, "LSI SAS2108" },
106 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
107 0xffff, 0xffff, 0, "LSI SAS2108" },
108 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
109 0xffff, 0xffff, 0, "LSI SAS2108" },
110 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
111 0xffff, 0xffff, 0, "LSI SAS2116" },
112 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
113 0xffff, 0xffff, 0, "LSI SAS2116" },
114 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
115 0xffff, 0xffff, 0, "LSI SAS2208" },
116 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
117 0xffff, 0xffff, 0, "LSI SAS2208" },
118 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
119 0xffff, 0xffff, 0, "LSI SAS2208" },
120 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
121 0xffff, 0xffff, 0, "LSI SAS2208" },
122 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
123 0xffff, 0xffff, 0, "LSI SAS2208" },
124 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
125 0xffff, 0xffff, 0, "LSI SAS2208" },
c12c399a
SW
126 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
127 0xffff, 0xffff, 0, "LSI SAS2308" },
128 // Add Customer specific vender/subdevice id before generic
129 // (0xffff) vender/subdevice id.
130 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
131 0x8086, 0x3516, 0, "Intel(R) Integrated RAID Module RMS25JB080" },
132 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
133 0x8086, 0x3517, 0, "Intel(R) Integrated RAID Module RMS25JB040" },
134 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
135 0x8086, 0x3518, 0, "Intel(R) Integrated RAID Module RMS25KB080" },
136 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
137 0x8086, 0x3519, 0, "Intel(R) Integrated RAID Module RMS25KB040" },
138 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
139 0xffff, 0xffff, 0, "LSI SAS2308" },
140 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
141 0xffff, 0xffff, 0, "LSI SAS2308" },
142 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
143 0xffff, 0xffff, MPS_FLAGS_WD_AVAILABLE, "LSI SSS6200" },
ad8cf91c
SW
144 { 0, 0, 0, 0, 0, NULL }
145};
146
147static struct mps_ident *
148mps_find_ident(device_t dev)
149{
150 struct mps_ident *m;
151
152 for (m = mps_identifiers; m->vendor != 0; m++) {
153 if (m->vendor != pci_get_vendor(dev))
154 continue;
155 if (m->device != pci_get_device(dev))
156 continue;
157 if ((m->subvendor != 0xffff) &&
158 (m->subvendor != pci_get_subvendor(dev)))
159 continue;
160 if ((m->subdevice != 0xffff) &&
161 (m->subdevice != pci_get_subdevice(dev)))
162 continue;
163 return (m);
164 }
165
166 return (NULL);
167}
168
169static int
170mps_pci_probe(device_t dev)
171{
172 struct mps_ident *id;
173
174 if ((id = mps_find_ident(dev)) != NULL) {
175 device_set_desc(dev, id->desc);
43f7c553 176 return (BUS_PROBE_DEFAULT);
ad8cf91c
SW
177 }
178 return (ENXIO);
179}
180
181static int
182mps_pci_attach(device_t dev)
183{
184 struct mps_softc *sc;
185 struct mps_ident *m;
186 uint16_t command;
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 /* Twiddle basic PCI config bits for a sanity check */
196 command = pci_read_config(dev, PCIR_COMMAND, 2);
197 command |= PCIM_CMD_BUSMASTEREN;
198 pci_write_config(dev, PCIR_COMMAND, command, 2);
199 command = pci_read_config(dev, PCIR_COMMAND, 2);
200 if ((command & PCIM_CMD_BUSMASTEREN) == 0) {
201 device_printf(dev, "Cannot enable PCI busmaster\n");
202 return (ENXIO);
203 }
204 if ((command & PCIM_CMD_MEMEN) == 0) {
205 device_printf(dev, "PCI memory window not available\n");
206 return (ENXIO);
207 }
208
209 /* Allocate the System Interface Register Set */
210 sc->mps_regs_rid = PCIR_BAR(1);
211 if ((sc->mps_regs_resource = bus_alloc_resource_any(dev,
212 SYS_RES_MEMORY, &sc->mps_regs_rid, RF_ACTIVE)) == NULL) {
213 device_printf(dev, "Cannot allocate PCI registers\n");
214 return (ENXIO);
215 }
216 sc->mps_btag = rman_get_bustag(sc->mps_regs_resource);
217 sc->mps_bhandle = rman_get_bushandle(sc->mps_regs_resource);
218
219 /* Allocate the parent DMA tag */
c12c399a 220 if (bus_dma_tag_create(NULL, /* parent */
ad8cf91c
SW
221 1, 0, /* algnmnt, boundary */
222 BUS_SPACE_MAXADDR, /* lowaddr */
223 BUS_SPACE_MAXADDR, /* highaddr */
ad8cf91c
SW
224 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
225 BUS_SPACE_UNRESTRICTED, /* nsegments */
226 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
227 0, /* flags */
228 &sc->mps_parent_dmat)) {
229 device_printf(dev, "Cannot allocate parent DMA tag\n");
230 mps_pci_free(sc);
231 return (ENOMEM);
232 }
233
234 if ((error = mps_attach(sc)) != 0)
235 mps_pci_free(sc);
236
237 return (error);
238}
239
240int
241mps_pci_setup_interrupts(struct mps_softc *sc)
242{
243 device_t dev;
c12c399a
SW
244 int error;
245 u_int irq_flags;
ad8cf91c
SW
246
247 dev = sc->mps_dev;
c12c399a
SW
248#if 0 /* XXX swildner */
249 if ((sc->disable_msix == 0) &&
ad8cf91c
SW
250 ((msgs = pci_msix_count(dev)) >= MPS_MSI_COUNT))
251 error = mps_alloc_msix(sc, MPS_MSI_COUNT);
9dc47ee5 252#endif
ad8cf91c 253
c12c399a
SW
254 sc->mps_irq_rid[0] = 0;
255 sc->mps_irq_type[0] = pci_alloc_1intr(dev, sc->enable_msi,
256 &sc->mps_irq_rid[0], &irq_flags);
257 sc->mps_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
258 &sc->mps_irq_rid[0], irq_flags);
259 if (sc->mps_irq[0] == NULL) {
260 device_printf(dev, "Cannot allocate interrupt\n");
261 return (ENXIO);
ad8cf91c 262 }
c12c399a
SW
263 error = bus_setup_intr(dev, sc->mps_irq[0], INTR_MPSAFE, mps_intr, sc,
264 &sc->mps_intrhand[0], NULL);
265 if (error)
266 device_printf(dev, "Cannot setup interrupt\n");
ad8cf91c
SW
267
268 return (error);
269}
270
271static int
272mps_pci_detach(device_t dev)
273{
274 struct mps_softc *sc;
275 int error;
276
277 sc = device_get_softc(dev);
278
279 if ((error = mps_free(sc)) != 0)
280 return (error);
281
282 mps_pci_free(sc);
283 return (0);
284}
285
286static void
287mps_pci_free(struct mps_softc *sc)
288{
ad8cf91c
SW
289 if (sc->mps_parent_dmat != NULL) {
290 bus_dma_tag_destroy(sc->mps_parent_dmat);
291 }
292
c12c399a
SW
293 bus_teardown_intr(sc->mps_dev, sc->mps_irq[0], sc->mps_intrhand[0]);
294 bus_release_resource(sc->mps_dev, SYS_RES_IRQ, sc->mps_irq_rid[0],
295 sc->mps_irq[0]);
296 if (sc->mps_irq_type[0] == PCI_INTR_TYPE_MSI)
ad8cf91c 297 pci_release_msi(sc->mps_dev);
ad8cf91c
SW
298
299 if (sc->mps_regs_resource != NULL) {
300 bus_release_resource(sc->mps_dev, SYS_RES_MEMORY,
301 sc->mps_regs_rid, sc->mps_regs_resource);
302 }
303
304 return;
305}
306
307static int
308mps_pci_suspend(device_t dev)
309{
310 return (EINVAL);
311}
312
313static int
314mps_pci_resume(device_t dev)
315{
316 return (EINVAL);
317}
318
c12c399a 319#if 0 /* XXX swildner */
ad8cf91c
SW
320static int
321mps_alloc_msix(struct mps_softc *sc, int msgs)
322{
323 int error;
324
325 error = pci_alloc_msix(sc->mps_dev, &msgs);
326 return (error);
327}
c12c399a 328#endif
ad8cf91c 329
c12c399a
SW
330int
331mps_pci_restore(struct mps_softc *sc)
ad8cf91c 332{
c12c399a 333 struct pci_devinfo *dinfo;
ad8cf91c 334
c12c399a
SW
335 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
336
337 dinfo = device_get_ivars(sc->mps_dev);
338 if (dinfo == NULL) {
339 mps_dprint(sc, MPS_FAULT, "%s: NULL dinfo\n", __func__);
340 return (EINVAL);
341 }
342
343 pci_cfg_restore(sc->mps_dev, dinfo);
344 return (0);
ad8cf91c 345}