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>
14 #include <sys/rwlock.h>
17 #include <machine/bus.h>
18 #include <dev/pci/pcivar.h> /* For pci_get macros! */
19 #include <dev/pci/pcireg.h> /* The softc holds our per-instance data. */
22 #include <vm/vm_object.h>
23 #include <vm/vm_page.h>
24 #include <vm/vm_pager.h>
27 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
29 #define MAX_DETACHED_DEVICES 128
30 static device_t detached_devices[MAX_DETACHED_DEVICES] = {};
31 static int num_detached = 0;
33 struct nic_uio_softc {
37 struct resource *bar_res[MAX_BARS];
38 u_long bar_start[MAX_BARS];
39 u_long bar_size[MAX_BARS];
42 /* Function prototypes */
43 static d_open_t nic_uio_open;
44 static d_close_t nic_uio_close;
45 static d_mmap_t nic_uio_mmap;
46 static d_mmap_single_t nic_uio_mmap_single;
47 static int nic_uio_probe(device_t dev);
48 static int nic_uio_attach(device_t dev);
49 static int nic_uio_detach(device_t dev);
50 static int nic_uio_shutdown(void);
51 static int nic_uio_modevent(module_t mod, int type, void *arg);
53 static struct cdevsw uio_cdevsw = {
55 .d_version = D_VERSION,
56 .d_open = nic_uio_open,
57 .d_close = nic_uio_close,
58 .d_mmap = nic_uio_mmap,
59 .d_mmap_single = nic_uio_mmap_single,
62 static device_method_t nic_uio_methods[] = {
63 DEVMETHOD(device_probe, nic_uio_probe),
64 DEVMETHOD(device_attach, nic_uio_attach),
65 DEVMETHOD(device_detach, nic_uio_detach),
80 static devclass_t nic_uio_devclass;
82 DEFINE_CLASS_0(nic_uio, nic_uio_driver, nic_uio_methods, sizeof(struct nic_uio_softc));
83 DRIVER_MODULE(nic_uio, pci, nic_uio_driver, nic_uio_devclass, nic_uio_modevent, 0);
86 nic_uio_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
87 int prot, vm_memattr_t *memattr)
94 nic_uio_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
95 struct vm_object **obj, int nprot)
98 * The BAR index is encoded in the offset. Divide the offset by
99 * PAGE_SIZE to get the index of the bar requested by the user
102 unsigned bar = *offset/PAGE_SIZE;
103 struct nic_uio_softc *sc = cdev->si_drv1;
108 if (sc->bar_res[bar] == NULL) {
109 sc->bar_id[bar] = PCIR_BAR(bar);
111 if (PCI_BAR_IO(pci_read_config(sc->dev_t, sc->bar_id[bar], 4)))
112 sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_IOPORT,
113 &sc->bar_id[bar], RF_ACTIVE);
115 sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_MEMORY,
116 &sc->bar_id[bar], RF_ACTIVE);
118 if (sc->bar_res[bar] == NULL)
121 sc->bar_start[bar] = rman_get_start(sc->bar_res[bar]);
122 sc->bar_size[bar] = rman_get_size(sc->bar_res[bar]);
124 device_printf(sc->dev_t, "Bar %u @ %lx, size %lx\n", bar,
125 sc->bar_start[bar], sc->bar_size[bar]);
127 *offset = sc->bar_start[bar];
128 *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
129 curthread->td_ucred);
135 nic_uio_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
141 nic_uio_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
147 nic_uio_probe (device_t dev)
150 unsigned int bus = pci_get_bus(dev);
151 unsigned int device = pci_get_slot(dev);
152 unsigned int function = pci_get_function(dev);
155 char *token, *remaining;
157 /* First check if we found this on load */
158 for (i = 0; i < num_detached; i++)
159 if (bus == pci_get_bus(detached_devices[i]) &&
160 device == pci_get_slot(detached_devices[i]) &&
161 function == pci_get_function(detached_devices[i])) {
162 device_set_desc(dev, "DPDK PCI Device");
163 return BUS_PROBE_SPECIFIC;
166 /* otherwise check if it's a new device and if it matches the BDF */
167 memset(bdf_str, 0, sizeof(bdf_str));
168 TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
171 if (remaining == NULL || remaining[0] == '\0')
173 token = strsep(&remaining, ",:");
176 bus = strtol(token, NULL, 10);
177 token = strsep(&remaining, ",:");
180 device = strtol(token, NULL, 10);
181 token = strsep(&remaining, ",:");
184 function = strtol(token, NULL, 10);
186 if (bus == pci_get_bus(dev) &&
187 device == pci_get_slot(dev) &&
188 function == pci_get_function(dev)) {
190 if (num_detached < MAX_DETACHED_DEVICES) {
191 printf("%s: probed dev=%p\n",
193 detached_devices[num_detached++] = dev;
194 device_set_desc(dev, "DPDK PCI Device");
195 return BUS_PROBE_SPECIFIC;
197 printf("%s: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
198 __func__, MAX_DETACHED_DEVICES,
209 nic_uio_attach(device_t dev)
212 struct nic_uio_softc *sc;
214 sc = device_get_softc(dev);
216 sc->my_cdev = make_dev(&uio_cdevsw, device_get_unit(dev),
217 UID_ROOT, GID_WHEEL, 0600, "uio@pci:%u:%u:%u",
218 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
219 if (sc->my_cdev == NULL)
221 sc->my_cdev->si_drv1 = sc;
223 for (i = 0; i < MAX_BARS; i++)
224 sc->bar_res[i] = NULL;
226 pci_enable_busmaster(dev);
232 nic_uio_detach(device_t dev)
235 struct nic_uio_softc *sc;
236 sc = device_get_softc(dev);
238 for (i = 0; i < MAX_BARS; i++)
239 if (sc->bar_res[i] != NULL) {
241 if (PCI_BAR_IO(pci_read_config(dev, sc->bar_id[i], 4)))
242 bus_release_resource(dev, SYS_RES_IOPORT, sc->bar_id[i],
245 bus_release_resource(dev, SYS_RES_MEMORY, sc->bar_id[i],
249 if (sc->my_cdev != NULL)
250 destroy_dev(sc->my_cdev);
257 uint32_t bus, device, function;
260 char *token, *remaining;
262 memset(bdf_str, 0, sizeof(bdf_str));
263 TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
265 printf("nic_uio: hw.nic_uio.bdfs = '%s'\n", bdf_str);
267 * Users should specify PCI BDFs in the format "b:d:f,b:d:f,b:d:f".
268 * But the code below does not try differentiate between : and ,
269 * and just blindly uses 3 tokens at a time to construct a
270 * bus/device/function tuple.
272 * There is no checking on strtol() return values, but this should
273 * be OK. Worst case is it cannot convert and returns 0. This
274 * could give us a different BDF than intended, but as long as the
275 * PCI device/vendor ID does not match it will not matter.
278 if (remaining == NULL || remaining[0] == '\0')
280 token = strsep(&remaining, ",:");
283 bus = strtol(token, NULL, 10);
284 token = strsep(&remaining, ",:");
287 device = strtol(token, NULL, 10);
288 token = strsep(&remaining, ",:");
291 function = strtol(token, NULL, 10);
293 dev = pci_find_bsf(bus, device, function);
297 if (num_detached < MAX_DETACHED_DEVICES) {
298 printf("nic_uio_load: detaching and storing dev=%p\n",
300 detached_devices[num_detached++] = dev;
302 printf("nic_uio_load: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
303 MAX_DETACHED_DEVICES, dev);
313 printf("nic_uio_unload: entered...\n");
315 for (i = 0; i < num_detached; i++) {
316 printf("nic_uio_unload: calling to device_probe_and_attach for dev=%p...\n",
317 detached_devices[i]);
318 device_probe_and_attach(detached_devices[i]);
319 printf("nic_uio_unload: done.\n");
322 printf("nic_uio_unload: leaving...\n");
326 nic_uio_shutdown(void)
332 nic_uio_modevent(module_t mod, int type, void *arg)