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