New upstream version 18.08
[deb_dpdk.git] / kernel / freebsd / nic_uio / nic_uio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD$");
6
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 */
12 #include <sys/rman.h>
13 #include <sys/systm.h>
14 #include <sys/rwlock.h>
15 #include <sys/proc.h>
16
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. */
20 #include <vm/vm.h>
21 #include <vm/uma.h>
22 #include <vm/vm_object.h>
23 #include <vm/vm_page.h>
24 #include <vm/vm_pager.h>
25
26
27 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
28
29 #define MAX_DETACHED_DEVICES    128
30 static device_t detached_devices[MAX_DETACHED_DEVICES] = {};
31 static int num_detached = 0;
32
33 struct nic_uio_softc {
34         device_t        dev_t;
35         struct cdev     *my_cdev;
36         int              bar_id[MAX_BARS];
37         struct resource *bar_res[MAX_BARS];
38         u_long           bar_start[MAX_BARS];
39         u_long           bar_size[MAX_BARS];
40 };
41
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);
52
53 static struct cdevsw uio_cdevsw = {
54                 .d_name        = "nic_uio",
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,
60 };
61
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),
66         DEVMETHOD_END
67 };
68
69 struct device {
70     int vend;
71     int dev;
72 };
73
74 struct pci_bdf {
75         uint32_t bus;
76         uint32_t devid;
77         uint32_t function;
78 };
79
80 static devclass_t nic_uio_devclass;
81
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);
84
85 static int
86 nic_uio_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
87                 int prot, vm_memattr_t *memattr)
88 {
89         *paddr = offset;
90         return 0;
91 }
92
93 static int
94 nic_uio_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
95                 struct vm_object **obj, int nprot)
96 {
97         /*
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
100          *  app.
101          */
102         unsigned bar = *offset/PAGE_SIZE;
103         struct nic_uio_softc *sc = cdev->si_drv1;
104
105         if (bar >= MAX_BARS)
106                 return EINVAL;
107
108         if (sc->bar_res[bar] == NULL) {
109                 sc->bar_id[bar] = PCIR_BAR(bar);
110
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);
114                 else
115                         sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_MEMORY,
116                                         &sc->bar_id[bar], RF_ACTIVE);
117         }
118         if (sc->bar_res[bar] == NULL)
119                 return ENXIO;
120
121         sc->bar_start[bar] = rman_get_start(sc->bar_res[bar]);
122         sc->bar_size[bar] = rman_get_size(sc->bar_res[bar]);
123
124         device_printf(sc->dev_t, "Bar %u @ %lx, size %lx\n", bar,
125                         sc->bar_start[bar], sc->bar_size[bar]);
126
127         *offset = sc->bar_start[bar];
128         *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
129                                 curthread->td_ucred);
130         return 0;
131 }
132
133
134 int
135 nic_uio_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
136 {
137         return 0;
138 }
139
140 int
141 nic_uio_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
142 {
143         return 0;
144 }
145
146 static int
147 nic_uio_probe (device_t dev)
148 {
149         int i;
150         unsigned int bus = pci_get_bus(dev);
151         unsigned int device = pci_get_slot(dev);
152         unsigned int function = pci_get_function(dev);
153
154         char bdf_str[256];
155         char *token, *remaining;
156
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;
164                 }
165
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));
169         remaining = bdf_str;
170         while (1) {
171                 if (remaining == NULL || remaining[0] == '\0')
172                         break;
173                 token = strsep(&remaining, ",:");
174                 if (token == NULL)
175                         break;
176                 bus = strtol(token, NULL, 10);
177                 token = strsep(&remaining, ",:");
178                 if (token == NULL)
179                         break;
180                 device = strtol(token, NULL, 10);
181                 token = strsep(&remaining, ",:");
182                 if (token == NULL)
183                         break;
184                 function = strtol(token, NULL, 10);
185
186                 if (bus == pci_get_bus(dev) &&
187                                 device == pci_get_slot(dev) &&
188                                 function == pci_get_function(dev)) {
189
190                         if (num_detached < MAX_DETACHED_DEVICES) {
191                                 printf("%s: probed dev=%p\n",
192                                                __func__, dev);
193                                 detached_devices[num_detached++] = dev;
194                                 device_set_desc(dev, "DPDK PCI Device");
195                                 return BUS_PROBE_SPECIFIC;
196                         } else {
197                                 printf("%s: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
198                                                 __func__, MAX_DETACHED_DEVICES,
199                                                 dev);
200                                 break;
201                         }
202                 }
203         }
204
205         return ENXIO;
206 }
207
208 static int
209 nic_uio_attach(device_t dev)
210 {
211         int i;
212         struct nic_uio_softc *sc;
213
214         sc = device_get_softc(dev);
215         sc->dev_t = 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)
220                 return ENXIO;
221         sc->my_cdev->si_drv1 = sc;
222
223         for (i = 0; i < MAX_BARS; i++)
224                 sc->bar_res[i] = NULL;
225
226         pci_enable_busmaster(dev);
227
228         return 0;
229 }
230
231 static int
232 nic_uio_detach(device_t dev)
233 {
234         int i;
235         struct nic_uio_softc *sc;
236         sc = device_get_softc(dev);
237
238         for (i = 0; i < MAX_BARS; i++)
239                 if (sc->bar_res[i] != NULL) {
240
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],
243                                                 sc->bar_res[i]);
244                         else
245                                 bus_release_resource(dev, SYS_RES_MEMORY, sc->bar_id[i],
246                                                 sc->bar_res[i]);
247                 }
248
249         if (sc->my_cdev != NULL)
250                 destroy_dev(sc->my_cdev);
251         return 0;
252 }
253
254 static void
255 nic_uio_load(void)
256 {
257         uint32_t bus, device, function;
258         device_t dev;
259         char bdf_str[256];
260         char *token, *remaining;
261
262         memset(bdf_str, 0, sizeof(bdf_str));
263         TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
264         remaining = bdf_str;
265         printf("nic_uio: hw.nic_uio.bdfs = '%s'\n", bdf_str);
266         /*
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.
271          *
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.
276          */
277         while (1) {
278                 if (remaining == NULL || remaining[0] == '\0')
279                         break;
280                 token = strsep(&remaining, ",:");
281                 if (token == NULL)
282                         break;
283                 bus = strtol(token, NULL, 10);
284                 token = strsep(&remaining, ",:");
285                 if (token == NULL)
286                         break;
287                 device = strtol(token, NULL, 10);
288                 token = strsep(&remaining, ",:");
289                 if (token == NULL)
290                         break;
291                 function = strtol(token, NULL, 10);
292
293                 dev = pci_find_bsf(bus, device, function);
294                 if (dev == NULL)
295                         continue;
296
297                 if (num_detached < MAX_DETACHED_DEVICES) {
298                         printf("nic_uio_load: detaching and storing dev=%p\n",
299                                dev);
300                         detached_devices[num_detached++] = dev;
301                 } else {
302                         printf("nic_uio_load: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
303                                MAX_DETACHED_DEVICES, dev);
304                 }
305                 device_detach(dev);
306         }
307 }
308
309 static void
310 nic_uio_unload(void)
311 {
312         int i;
313         printf("nic_uio_unload: entered...\n");
314
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");
320         }
321
322         printf("nic_uio_unload: leaving...\n");
323 }
324
325 static int
326 nic_uio_shutdown(void)
327 {
328         return 0;
329 }
330
331 static int
332 nic_uio_modevent(module_t mod, int type, void *arg)
333 {
334
335         switch (type) {
336         case MOD_LOAD:
337                 nic_uio_load();
338                 break;
339         case MOD_UNLOAD:
340                 nic_uio_unload();
341                 break;
342         case MOD_SHUTDOWN:
343                 nic_uio_shutdown();
344                 break;
345         default:
346                 break;
347         }
348
349         return 0;
350 }