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