1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <sys/param.h> /* defines used in kernel.h */
8 #include <sys/module.h>
9 #include <sys/kernel.h> /* types used in module initialization */
10 #include <sys/conf.h> /* cdevsw struct */
11 #include <sys/bus.h> /* structs, prototypes for pci bus stuff and DEVMETHOD */
13 #include <sys/systm.h>
15 #include <sys/rwlock.h>
18 #include <machine/bus.h>
19 #include <dev/pci/pcivar.h> /* For pci_get macros! */
20 #include <dev/pci/pcireg.h> /* The softc holds our per-instance data. */
23 #include <vm/vm_object.h>
24 #include <vm/vm_page.h>
25 #include <vm/vm_pager.h>
28 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
30 #define MAX_DETACHED_DEVICES 128
31 static device_t detached_devices[MAX_DETACHED_DEVICES] = {};
32 static int num_detached = 0;
34 struct nic_uio_softc {
38 struct resource *bar_res[MAX_BARS];
39 u_long bar_start[MAX_BARS];
40 u_long bar_size[MAX_BARS];
43 /* Function prototypes */
44 static d_open_t nic_uio_open;
45 static d_close_t nic_uio_close;
46 static d_mmap_t nic_uio_mmap;
47 static d_mmap_single_t nic_uio_mmap_single;
48 static int nic_uio_probe(device_t dev);
49 static int nic_uio_attach(device_t dev);
50 static int nic_uio_detach(device_t dev);
51 static int nic_uio_shutdown(void);
52 static int nic_uio_modevent(module_t mod, int type, void *arg);
54 static struct cdevsw uio_cdevsw = {
56 .d_version = D_VERSION,
57 .d_open = nic_uio_open,
58 .d_close = nic_uio_close,
59 .d_mmap = nic_uio_mmap,
60 .d_mmap_single = nic_uio_mmap_single,
63 static device_method_t nic_uio_methods[] = {
64 DEVMETHOD(device_probe, nic_uio_probe),
65 DEVMETHOD(device_attach, nic_uio_attach),
66 DEVMETHOD(device_detach, nic_uio_detach),
81 static devclass_t nic_uio_devclass;
83 DEFINE_CLASS_0(nic_uio, nic_uio_driver, nic_uio_methods, sizeof(struct nic_uio_softc));
84 DRIVER_MODULE(nic_uio, pci, nic_uio_driver, nic_uio_devclass, nic_uio_modevent, 0);
87 nic_uio_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
88 int prot, vm_memattr_t *memattr)
95 nic_uio_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
96 struct vm_object **obj, int nprot)
99 * The BAR index is encoded in the offset. Divide the offset by
100 * PAGE_SIZE to get the index of the bar requested by the user
103 unsigned bar = *offset/PAGE_SIZE;
104 struct nic_uio_softc *sc = cdev->si_drv1;
109 if (sc->bar_res[bar] == NULL) {
110 sc->bar_id[bar] = PCIR_BAR(bar);
112 if (PCI_BAR_IO(pci_read_config(sc->dev_t, sc->bar_id[bar], 4)))
113 sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_IOPORT,
114 &sc->bar_id[bar], RF_ACTIVE);
116 sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_MEMORY,
117 &sc->bar_id[bar], RF_ACTIVE);
119 if (sc->bar_res[bar] == NULL)
122 sc->bar_start[bar] = rman_get_start(sc->bar_res[bar]);
123 sc->bar_size[bar] = rman_get_size(sc->bar_res[bar]);
125 device_printf(sc->dev_t, "Bar %u @ %lx, size %lx\n", bar,
126 sc->bar_start[bar], sc->bar_size[bar]);
128 *offset = sc->bar_start[bar];
129 *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
130 curthread->td_ucred);
136 nic_uio_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
142 nic_uio_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
148 nic_uio_probe (device_t dev)
151 unsigned int bus = pci_get_bus(dev);
152 unsigned int device = pci_get_slot(dev);
153 unsigned int function = pci_get_function(dev);
156 char *token, *remaining;
158 /* First check if we found this on load */
159 for (i = 0; i < num_detached; i++)
160 if (bus == pci_get_bus(detached_devices[i]) &&
161 device == pci_get_slot(detached_devices[i]) &&
162 function == pci_get_function(detached_devices[i])) {
163 device_set_desc(dev, "DPDK PCI Device");
164 return BUS_PROBE_SPECIFIC;
167 /* otherwise check if it's a new device and if it matches the BDF */
168 memset(bdf_str, 0, sizeof(bdf_str));
169 TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
172 if (remaining == NULL || remaining[0] == '\0')
174 token = strsep(&remaining, ",:");
177 bus = strtol(token, NULL, 10);
178 token = strsep(&remaining, ",:");
181 device = strtol(token, NULL, 10);
182 token = strsep(&remaining, ",:");
185 function = strtol(token, NULL, 10);
187 if (bus == pci_get_bus(dev) &&
188 device == pci_get_slot(dev) &&
189 function == pci_get_function(dev)) {
191 if (num_detached < MAX_DETACHED_DEVICES) {
192 printf("%s: probed dev=%p\n",
194 detached_devices[num_detached++] = dev;
195 device_set_desc(dev, "DPDK PCI Device");
196 return BUS_PROBE_SPECIFIC;
198 printf("%s: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
199 __func__, MAX_DETACHED_DEVICES,
210 nic_uio_attach(device_t dev)
213 struct nic_uio_softc *sc;
215 sc = device_get_softc(dev);
217 sc->my_cdev = make_dev(&uio_cdevsw, device_get_unit(dev),
218 UID_ROOT, GID_WHEEL, 0600, "uio@pci:%u:%u:%u",
219 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
220 if (sc->my_cdev == NULL)
222 sc->my_cdev->si_drv1 = sc;
224 for (i = 0; i < MAX_BARS; i++)
225 sc->bar_res[i] = NULL;
227 pci_enable_busmaster(dev);
233 nic_uio_detach(device_t dev)
236 struct nic_uio_softc *sc;
237 sc = device_get_softc(dev);
239 for (i = 0; i < MAX_BARS; i++)
240 if (sc->bar_res[i] != NULL) {
242 if (PCI_BAR_IO(pci_read_config(dev, sc->bar_id[i], 4)))
243 bus_release_resource(dev, SYS_RES_IOPORT, sc->bar_id[i],
246 bus_release_resource(dev, SYS_RES_MEMORY, sc->bar_id[i],
250 if (sc->my_cdev != NULL)
251 destroy_dev(sc->my_cdev);
258 uint32_t bus, device, function;
261 char *token, *remaining;
263 memset(bdf_str, 0, sizeof(bdf_str));
264 TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
266 printf("nic_uio: hw.nic_uio.bdfs = '%s'\n", bdf_str);
268 * Users should specify PCI BDFs in the format "b:d:f,b:d:f,b:d:f".
269 * But the code below does not try differentiate between : and ,
270 * and just blindly uses 3 tokens at a time to construct a
271 * bus/device/function tuple.
273 * There is no checking on strtol() return values, but this should
274 * be OK. Worst case is it cannot convert and returns 0. This
275 * could give us a different BDF than intended, but as long as the
276 * PCI device/vendor ID does not match it will not matter.
279 if (remaining == NULL || remaining[0] == '\0')
281 token = strsep(&remaining, ",:");
284 bus = strtol(token, NULL, 10);
285 token = strsep(&remaining, ",:");
288 device = strtol(token, NULL, 10);
289 token = strsep(&remaining, ",:");
292 function = strtol(token, NULL, 10);
294 dev = pci_find_bsf(bus, device, function);
298 if (num_detached < MAX_DETACHED_DEVICES) {
299 printf("nic_uio_load: detaching and storing dev=%p\n",
301 detached_devices[num_detached++] = dev;
303 printf("nic_uio_load: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
304 MAX_DETACHED_DEVICES, dev);
314 printf("nic_uio_unload: entered...\n");
316 for (i = 0; i < num_detached; i++) {
317 printf("nic_uio_unload: calling to device_probe_and_attach for dev=%p...\n",
318 detached_devices[i]);
319 device_probe_and_attach(detached_devices[i]);
320 printf("nic_uio_unload: done.\n");
323 printf("nic_uio_unload: leaving...\n");
327 nic_uio_shutdown(void)
333 nic_uio_modevent(module_t mod, int type, void *arg)