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