4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h> /* defines used in kernel.h */
37 #include <sys/module.h>
38 #include <sys/kernel.h> /* types used in module initialization */
39 #include <sys/conf.h> /* cdevsw struct */
40 #include <sys/bus.h> /* structs, prototypes for pci bus stuff and DEVMETHOD */
42 #include <sys/systm.h>
43 #include <sys/rwlock.h>
46 #include <machine/bus.h>
47 #include <dev/pci/pcivar.h> /* For pci_get macros! */
48 #include <dev/pci/pcireg.h> /* The softc holds our per-instance data. */
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
56 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
59 struct nic_uio_softc {
63 struct resource *bar_res[MAX_BARS];
64 u_long bar_start[MAX_BARS];
65 u_long bar_size[MAX_BARS];
68 /* Function prototypes */
69 static d_open_t nic_uio_open;
70 static d_close_t nic_uio_close;
71 static d_mmap_t nic_uio_mmap;
72 static d_mmap_single_t nic_uio_mmap_single;
73 static int nic_uio_probe(device_t dev);
74 static int nic_uio_attach(device_t dev);
75 static int nic_uio_detach(device_t dev);
76 static int nic_uio_shutdown(void);
77 static int nic_uio_modevent(module_t mod, int type, void *arg);
79 static struct cdevsw uio_cdevsw = {
81 .d_version = D_VERSION,
82 .d_open = nic_uio_open,
83 .d_close = nic_uio_close,
84 .d_mmap = nic_uio_mmap,
85 .d_mmap_single = nic_uio_mmap_single,
88 static device_method_t nic_uio_methods[] = {
89 DEVMETHOD(device_probe, nic_uio_probe),
90 DEVMETHOD(device_attach, nic_uio_attach),
91 DEVMETHOD(device_detach, nic_uio_detach),
107 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {vend, dev},
108 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {vend, dev},
109 #define RTE_PCI_DEV_ID_DECL_IGBVF(vend, dev) {vend, dev},
110 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {vend, dev},
111 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {vend, dev},
112 #define RTE_PCI_DEV_ID_DECL_I40E(vend, dev) {vend, dev},
113 #define RTE_PCI_DEV_ID_DECL_I40EVF(vend, dev) {vend, dev},
114 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {vend, dev},
115 #define RTE_PCI_DEV_ID_DECL_VMXNET3(vend, dev) {vend, dev},
116 #define RTE_PCI_DEV_ID_DECL_ENIC(vend, dev) {vend, dev},
118 const struct device devices[] = {
119 #include <rte_pci_dev_ids.h>
121 #define NUM_DEVICES (sizeof(devices)/sizeof(devices[0]))
124 static devclass_t nic_uio_devclass;
126 DEFINE_CLASS_0(nic_uio, nic_uio_driver, nic_uio_methods, sizeof(struct nic_uio_softc));
127 DRIVER_MODULE(nic_uio, pci, nic_uio_driver, nic_uio_devclass, nic_uio_modevent, 0);
130 nic_uio_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
131 int prot, vm_memattr_t *memattr)
138 nic_uio_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
139 struct vm_object **obj, int nprot)
142 * The BAR index is encoded in the offset. Divide the offset by
143 * PAGE_SIZE to get the index of the bar requested by the user
146 unsigned bar = *offset/PAGE_SIZE;
147 struct nic_uio_softc *sc = cdev->si_drv1;
152 if (sc->bar_res[bar] == NULL) {
153 sc->bar_id[bar] = PCIR_BAR(bar);
155 if (PCI_BAR_IO(pci_read_config(sc->dev_t, sc->bar_id[bar], 4)))
156 sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_IOPORT,
157 &sc->bar_id[bar], RF_ACTIVE);
159 sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_MEMORY,
160 &sc->bar_id[bar], RF_ACTIVE);
162 if (sc->bar_res[bar] == NULL)
165 sc->bar_start[bar] = rman_get_start(sc->bar_res[bar]);
166 sc->bar_size[bar] = rman_get_size(sc->bar_res[bar]);
168 device_printf(sc->dev_t, "Bar %u @ %lx, size %lx\n", bar,
169 sc->bar_start[bar], sc->bar_size[bar]);
171 *offset = sc->bar_start[bar];
172 *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
173 curthread->td_ucred);
179 nic_uio_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
185 nic_uio_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
191 nic_uio_probe (device_t dev)
195 for (i = 0; i < NUM_DEVICES; i++)
196 if (pci_get_vendor(dev) == devices[i].vend &&
197 pci_get_device(dev) == devices[i].dev) {
199 device_set_desc(dev, "Intel(R) DPDK PCI Device");
200 return BUS_PROBE_SPECIFIC;
207 nic_uio_attach(device_t dev)
210 struct nic_uio_softc *sc;
212 sc = device_get_softc(dev);
214 sc->my_cdev = make_dev(&uio_cdevsw, device_get_unit(dev),
215 UID_ROOT, GID_WHEEL, 0600, "uio@pci:%u:%u:%u",
216 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
217 if (sc->my_cdev == NULL)
219 sc->my_cdev->si_drv1 = sc;
221 for (i = 0; i < MAX_BARS; i++)
222 sc->bar_res[i] = NULL;
224 pci_enable_busmaster(dev);
230 nic_uio_detach(device_t dev)
233 struct nic_uio_softc *sc;
234 sc = device_get_softc(dev);
236 for (i = 0; i < MAX_BARS; i++)
237 if (sc->bar_res[i] != NULL) {
239 if (PCI_BAR_IO(pci_read_config(dev, sc->bar_id[i], 4)))
240 bus_release_resource(dev, SYS_RES_IOPORT, sc->bar_id[i],
243 bus_release_resource(dev, SYS_RES_MEMORY, sc->bar_id[i],
247 if (sc->my_cdev != NULL)
248 destroy_dev(sc->my_cdev);
255 uint32_t bus, device, function;
259 char *token, *remaining;
261 memset(bdf_str, 0, sizeof(bdf_str));
262 TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
265 * Users should specify PCI BDFs in the format "b:d:f,b:d:f,b:d:f".
266 * But the code below does not try differentiate between : and ,
267 * and just blindly uses 3 tokens at a time to construct a
268 * bus/device/function tuple.
270 * There is no checking on strtol() return values, but this should
271 * be OK. Worst case is it cannot convert and returns 0. This
272 * could give us a different BDF than intended, but as long as the
273 * PCI device/vendor ID does not match it will not matter.
276 if (remaining == NULL || remaining[0] == '\0')
278 token = strsep(&remaining, ",:");
281 bus = strtol(token, NULL, 10);
282 token = strsep(&remaining, ",:");
285 device = strtol(token, NULL, 10);
286 token = strsep(&remaining, ",:");
289 function = strtol(token, NULL, 10);
291 dev = pci_find_bsf(bus, device, function);
293 for (i = 0; i < NUM_DEVICES; i++)
294 if (pci_get_vendor(dev) == devices[i].vend &&
295 pci_get_device(dev) == devices[i].dev)
306 nic_uio_shutdown(void)
312 nic_uio_modevent(module_t mod, int type, void *arg)