xref: /freebsd/sys/dev/cxgbe/t4_iov.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /*-
2  * Copyright (c) 2015-2016 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/systm.h>
36 #include <dev/pci/pcivar.h>
37 
38 #ifdef PCI_IOV
39 #include <sys/nv.h>
40 #include <sys/iov_schema.h>
41 #include <dev/pci/pci_iov.h>
42 #endif
43 
44 #include "t4_if.h"
45 
46 struct t4iov_softc {
47 	device_t sc_dev;
48 	device_t sc_main;
49 	bool sc_attached;
50 };
51 
52 struct {
53 	uint16_t device;
54 	char *desc;
55 } t4iov_pciids[] = {
56 	{0x4000, "Chelsio T440-dbg"},
57 	{0x4001, "Chelsio T420-CR"},
58 	{0x4002, "Chelsio T422-CR"},
59 	{0x4003, "Chelsio T440-CR"},
60 	{0x4004, "Chelsio T420-BCH"},
61 	{0x4005, "Chelsio T440-BCH"},
62 	{0x4006, "Chelsio T440-CH"},
63 	{0x4007, "Chelsio T420-SO"},
64 	{0x4008, "Chelsio T420-CX"},
65 	{0x4009, "Chelsio T420-BT"},
66 	{0x400a, "Chelsio T404-BT"},
67 	{0x400e, "Chelsio T440-LP-CR"},
68 }, t5iov_pciids[] = {
69 	{0x5000, "Chelsio T580-dbg"},
70 	{0x5001,  "Chelsio T520-CR"},		/* 2 x 10G */
71 	{0x5002,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
72 	{0x5003,  "Chelsio T540-CR"},		/* 4 x 10G */
73 	{0x5007,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
74 	{0x5009,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
75 	{0x500a,  "Chelsio T504-BT"},		/* 4 x 1G */
76 	{0x500d,  "Chelsio T580-CR"},		/* 2 x 40G */
77 	{0x500e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
78 	{0x5010,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
79 	{0x5011,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
80 	{0x5012,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
81 	{0x5014,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
82 	{0x5015,  "Chelsio T502-BT"},		/* 2 x 1G */
83 #ifdef notyet
84 	{0x5004,  "Chelsio T520-BCH"},
85 	{0x5005,  "Chelsio T540-BCH"},
86 	{0x5006,  "Chelsio T540-CH"},
87 	{0x5008,  "Chelsio T520-CX"},
88 	{0x500b,  "Chelsio B520-SR"},
89 	{0x500c,  "Chelsio B504-BT"},
90 	{0x500f,  "Chelsio Amsterdam"},
91 	{0x5013,  "Chelsio T580-CHR"},
92 #endif
93 }, t6iov_pciids[] = {
94 	{0x6001, "Chelsio T6225-CR"},		/* 2 x 10/25G */
95 	{0x6002, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
96 	{0x6007, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
97 	{0x6008, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
98 	{0x600d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
99 };
100 
101 static int	t4iov_attach_child(device_t dev);
102 
103 static int
104 t4iov_probe(device_t dev)
105 {
106 	uint16_t d;
107 	size_t i;
108 
109 	d = pci_get_device(dev);
110 	for (i = 0; i < nitems(t4iov_pciids); i++) {
111 		if (d == t4iov_pciids[i].device) {
112 			device_set_desc(dev, t4iov_pciids[i].desc);
113 			device_quiet(dev);
114 			return (BUS_PROBE_DEFAULT);
115 		}
116 	}
117 	return (ENXIO);
118 }
119 
120 static int
121 t5iov_probe(device_t dev)
122 {
123 	uint16_t d;
124 	size_t i;
125 
126 	d = pci_get_device(dev);
127 	for (i = 0; i < nitems(t5iov_pciids); i++) {
128 		if (d == t5iov_pciids[i].device) {
129 			device_set_desc(dev, t5iov_pciids[i].desc);
130 			device_quiet(dev);
131 			return (BUS_PROBE_DEFAULT);
132 		}
133 	}
134 	return (ENXIO);
135 }
136 
137 static int
138 t6iov_probe(device_t dev)
139 {
140 	uint16_t d;
141 	size_t i;
142 
143 	d = pci_get_device(dev);
144 	for (i = 0; i < nitems(t6iov_pciids); i++) {
145 		if (d == t6iov_pciids[i].device) {
146 			device_set_desc(dev, t6iov_pciids[i].desc);
147 			device_quiet(dev);
148 			return (BUS_PROBE_DEFAULT);
149 		}
150 	}
151 	return (ENXIO);
152 }
153 
154 static int
155 t4iov_attach(device_t dev)
156 {
157 	struct t4iov_softc *sc;
158 
159 	sc = device_get_softc(dev);
160 	sc->sc_dev = dev;
161 
162 	sc->sc_main = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
163 	    pci_get_slot(dev), 4);
164 	if (T4_IS_MAIN_READY(sc->sc_main) == 0)
165 		return (t4iov_attach_child(dev));
166 	return (0);
167 }
168 
169 static int
170 t4iov_attach_child(device_t dev)
171 {
172 	struct t4iov_softc *sc;
173 #ifdef PCI_IOV
174 	nvlist_t *pf_schema, *vf_schema;
175 #endif
176 	device_t pdev;
177 	int error;
178 
179 	sc = device_get_softc(dev);
180 	MPASS(!sc->sc_attached);
181 
182 	/*
183 	 * PF0-3 are associated with a specific port on the NIC (PF0
184 	 * with port 0, etc.).  Ask the PF4 driver for the device for
185 	 * this function's associated port to determine if the port is
186 	 * present.
187 	 */
188 	error = T4_READ_PORT_DEVICE(sc->sc_main, pci_get_function(dev), &pdev);
189 	if (error)
190 		return (0);
191 
192 #ifdef PCI_IOV
193 	pf_schema = pci_iov_schema_alloc_node();
194 	vf_schema = pci_iov_schema_alloc_node();
195 	error = pci_iov_attach_name(dev, pf_schema, vf_schema, "%s",
196 	    device_get_nameunit(pdev));
197 	if (error) {
198 		device_printf(dev, "Failed to initialize SR-IOV: %d\n", error);
199 		return (0);
200 	}
201 #endif
202 
203 	sc->sc_attached = true;
204 	return (0);
205 }
206 
207 static int
208 t4iov_detach_child(device_t dev)
209 {
210 	struct t4iov_softc *sc;
211 #ifdef PCI_IOV
212 	int error;
213 #endif
214 
215 	sc = device_get_softc(dev);
216 	if (!sc->sc_attached)
217 		return (0);
218 
219 #ifdef PCI_IOV
220 	error = pci_iov_detach(dev);
221 	if (error != 0) {
222 		device_printf(dev, "Failed to disable SR-IOV\n");
223 		return (error);
224 	}
225 #endif
226 
227 	sc->sc_attached = false;
228 	return (0);
229 }
230 
231 static int
232 t4iov_detach(device_t dev)
233 {
234 	struct t4iov_softc *sc;
235 	int error;
236 
237 	sc = device_get_softc(dev);
238 	if (sc->sc_attached) {
239 		error = t4iov_detach_child(dev);
240 		if (error)
241 			return (error);
242 	}
243 	return (0);
244 }
245 
246 #ifdef PCI_IOV
247 static int
248 t4iov_iov_init(device_t dev, uint16_t num_vfs, const struct nvlist *config)
249 {
250 
251 	/* XXX: The Linux driver sets up a vf_monitor task on T4 adapters. */
252 	return (0);
253 }
254 
255 static void
256 t4iov_iov_uninit(device_t dev)
257 {
258 }
259 
260 static int
261 t4iov_add_vf(device_t dev, uint16_t vfnum, const struct nvlist *config)
262 {
263 
264 	return (0);
265 }
266 #endif
267 
268 static device_method_t t4iov_methods[] = {
269 	DEVMETHOD(device_probe,		t4iov_probe),
270 	DEVMETHOD(device_attach,	t4iov_attach),
271 	DEVMETHOD(device_detach,	t4iov_detach),
272 
273 #ifdef PCI_IOV
274 	DEVMETHOD(pci_iov_init,		t4iov_iov_init),
275 	DEVMETHOD(pci_iov_uninit,	t4iov_iov_uninit),
276 	DEVMETHOD(pci_iov_add_vf,	t4iov_add_vf),
277 #endif
278 
279 	DEVMETHOD(t4_attach_child,	t4iov_attach_child),
280 	DEVMETHOD(t4_detach_child,	t4iov_detach_child),
281 
282 	DEVMETHOD_END
283 };
284 
285 static driver_t t4iov_driver = {
286 	"t4iov",
287 	t4iov_methods,
288 	sizeof(struct t4iov_softc)
289 };
290 
291 static device_method_t t5iov_methods[] = {
292 	DEVMETHOD(device_probe,		t5iov_probe),
293 	DEVMETHOD(device_attach,	t4iov_attach),
294 	DEVMETHOD(device_detach,	t4iov_detach),
295 
296 #ifdef PCI_IOV
297 	DEVMETHOD(pci_iov_init,		t4iov_iov_init),
298 	DEVMETHOD(pci_iov_uninit,	t4iov_iov_uninit),
299 	DEVMETHOD(pci_iov_add_vf,	t4iov_add_vf),
300 #endif
301 
302 	DEVMETHOD(t4_attach_child,	t4iov_attach_child),
303 	DEVMETHOD(t4_detach_child,	t4iov_detach_child),
304 
305 	DEVMETHOD_END
306 };
307 
308 static driver_t t5iov_driver = {
309 	"t5iov",
310 	t5iov_methods,
311 	sizeof(struct t4iov_softc)
312 };
313 
314 static device_method_t t6iov_methods[] = {
315 	DEVMETHOD(device_probe,		t6iov_probe),
316 	DEVMETHOD(device_attach,	t4iov_attach),
317 	DEVMETHOD(device_detach,	t4iov_detach),
318 
319 #ifdef PCI_IOV
320 	DEVMETHOD(pci_iov_init,		t4iov_iov_init),
321 	DEVMETHOD(pci_iov_uninit,	t4iov_iov_uninit),
322 	DEVMETHOD(pci_iov_add_vf,	t4iov_add_vf),
323 #endif
324 
325 	DEVMETHOD(t4_attach_child,	t4iov_attach_child),
326 	DEVMETHOD(t4_detach_child,	t4iov_detach_child),
327 
328 	DEVMETHOD_END
329 };
330 
331 static driver_t t6iov_driver = {
332 	"t6iov",
333 	t6iov_methods,
334 	sizeof(struct t4iov_softc)
335 };
336 
337 static devclass_t t4iov_devclass, t5iov_devclass, t6iov_devclass;
338 
339 DRIVER_MODULE(t4iov, pci, t4iov_driver, t4iov_devclass, 0, 0);
340 MODULE_VERSION(t4iov, 1);
341 
342 DRIVER_MODULE(t5iov, pci, t5iov_driver, t5iov_devclass, 0, 0);
343 MODULE_VERSION(t5iov, 1);
344 
345 DRIVER_MODULE(t6iov, pci, t6iov_driver, t6iov_devclass, 0, 0);
346 MODULE_VERSION(t6iov, 1);
347