nic_uio: probe and attach devices after unload
[dpdk.git] / lib / librte_eal / bsdapp / nic_uio / nic_uio.c
1 /* -
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
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 */
41 #include <sys/rman.h>
42 #include <sys/systm.h>
43 #include <sys/rwlock.h>
44 #include <sys/proc.h>
45
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. */
49 #include <vm/vm.h>
50 #include <vm/uma.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
54
55
56 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
57
58 #define MAX_DETACHED_DEVICES    128
59 static device_t detached_devices[MAX_DETACHED_DEVICES] = {};
60 static int num_detached = 0;
61
62 struct nic_uio_softc {
63         device_t        dev_t;
64         struct cdev     *my_cdev;
65         int              bar_id[MAX_BARS];
66         struct resource *bar_res[MAX_BARS];
67         u_long           bar_start[MAX_BARS];
68         u_long           bar_size[MAX_BARS];
69 };
70
71 /* Function prototypes */
72 static d_open_t         nic_uio_open;
73 static d_close_t        nic_uio_close;
74 static d_mmap_t         nic_uio_mmap;
75 static d_mmap_single_t  nic_uio_mmap_single;
76 static int              nic_uio_probe(device_t dev);
77 static int              nic_uio_attach(device_t dev);
78 static int              nic_uio_detach(device_t dev);
79 static int              nic_uio_shutdown(void);
80 static int              nic_uio_modevent(module_t mod, int type, void *arg);
81
82 static struct cdevsw uio_cdevsw = {
83                 .d_name        = "nic_uio",
84                 .d_version     = D_VERSION,
85                 .d_open        = nic_uio_open,
86                 .d_close       = nic_uio_close,
87                 .d_mmap        = nic_uio_mmap,
88                 .d_mmap_single = nic_uio_mmap_single,
89 };
90
91 static device_method_t nic_uio_methods[] = {
92         DEVMETHOD(device_probe,    nic_uio_probe),
93         DEVMETHOD(device_attach,   nic_uio_attach),
94         DEVMETHOD(device_detach,   nic_uio_detach),
95         DEVMETHOD_END
96 };
97
98 struct device {
99     int vend;
100     int dev;
101 };
102
103 struct pci_bdf {
104         uint32_t bus;
105         uint32_t devid;
106         uint32_t function;
107 };
108
109
110 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev)      {vend, dev},
111 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev)     {vend, dev},
112 #define RTE_PCI_DEV_ID_DECL_IGBVF(vend, dev)   {vend, dev},
113 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev)   {vend, dev},
114 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {vend, dev},
115 #define RTE_PCI_DEV_ID_DECL_I40E(vend, dev)    {vend, dev},
116 #define RTE_PCI_DEV_ID_DECL_I40EVF(vend, dev)  {vend, dev},
117 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev)  {vend, dev},
118 #define RTE_PCI_DEV_ID_DECL_VMXNET3(vend, dev) {vend, dev},
119 #define RTE_PCI_DEV_ID_DECL_ENIC(vend, dev)    {vend, dev},
120
121 const struct device devices[] = {
122 #include <rte_pci_dev_ids.h>
123 };
124 #define NUM_DEVICES (sizeof(devices)/sizeof(devices[0]))
125
126
127 static devclass_t nic_uio_devclass;
128
129 DEFINE_CLASS_0(nic_uio, nic_uio_driver, nic_uio_methods, sizeof(struct nic_uio_softc));
130 DRIVER_MODULE(nic_uio, pci, nic_uio_driver, nic_uio_devclass, nic_uio_modevent, 0);
131
132 static int
133 nic_uio_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
134                 int prot, vm_memattr_t *memattr)
135 {
136         *paddr = offset;
137         return 0;
138 }
139
140 static int
141 nic_uio_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
142                 struct vm_object **obj, int nprot)
143 {
144         /*
145          * The BAR index is encoded in the offset.  Divide the offset by
146          *  PAGE_SIZE to get the index of the bar requested by the user
147          *  app.
148          */
149         unsigned bar = *offset/PAGE_SIZE;
150         struct nic_uio_softc *sc = cdev->si_drv1;
151
152         if (bar >= MAX_BARS)
153                 return EINVAL;
154
155         if (sc->bar_res[bar] == NULL) {
156                 sc->bar_id[bar] = PCIR_BAR(bar);
157
158                 if (PCI_BAR_IO(pci_read_config(sc->dev_t, sc->bar_id[bar], 4)))
159                         sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_IOPORT,
160                                         &sc->bar_id[bar], RF_ACTIVE);
161                 else
162                         sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_MEMORY,
163                                         &sc->bar_id[bar], RF_ACTIVE);
164         }
165         if (sc->bar_res[bar] == NULL)
166                 return ENXIO;
167
168         sc->bar_start[bar] = rman_get_start(sc->bar_res[bar]);
169         sc->bar_size[bar] = rman_get_size(sc->bar_res[bar]);
170
171         device_printf(sc->dev_t, "Bar %u @ %lx, size %lx\n", bar,
172                         sc->bar_start[bar], sc->bar_size[bar]);
173
174         *offset = sc->bar_start[bar];
175         *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
176                                 curthread->td_ucred);
177         return 0;
178 }
179
180
181 int
182 nic_uio_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
183 {
184         return 0;
185 }
186
187 int
188 nic_uio_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
189 {
190         return 0;
191 }
192
193 static int
194 nic_uio_probe (device_t dev)
195 {
196         int i;
197
198         for (i = 0; i < NUM_DEVICES; i++)
199                 if (pci_get_vendor(dev) == devices[i].vend &&
200                         pci_get_device(dev) == devices[i].dev) {
201
202                         device_set_desc(dev, "Intel(R) DPDK PCI Device");
203                         return BUS_PROBE_SPECIFIC;
204                 }
205
206         return ENXIO;
207 }
208
209 static int
210 nic_uio_attach(device_t dev)
211 {
212         int i;
213         struct nic_uio_softc *sc;
214
215         sc = device_get_softc(dev);
216         sc->dev_t = 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)
221                 return ENXIO;
222         sc->my_cdev->si_drv1 = sc;
223
224         for (i = 0; i < MAX_BARS; i++)
225                 sc->bar_res[i] = NULL;
226
227         pci_enable_busmaster(dev);
228
229         return 0;
230 }
231
232 static int
233 nic_uio_detach(device_t dev)
234 {
235         int i;
236         struct nic_uio_softc *sc;
237         sc = device_get_softc(dev);
238
239         for (i = 0; i < MAX_BARS; i++)
240                 if (sc->bar_res[i] != NULL) {
241
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],
244                                                 sc->bar_res[i]);
245                         else
246                                 bus_release_resource(dev, SYS_RES_MEMORY, sc->bar_id[i],
247                                                 sc->bar_res[i]);
248                 }
249
250         if (sc->my_cdev != NULL)
251                 destroy_dev(sc->my_cdev);
252         return 0;
253 }
254
255 static void
256 nic_uio_load(void)
257 {
258         uint32_t bus, device, function;
259         int i;
260         device_t dev;
261         char bdf_str[256];
262         char *token, *remaining;
263
264         memset(bdf_str, 0, sizeof(bdf_str));
265         TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
266         remaining = bdf_str;
267         /*
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.
272          *
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.
277          */
278         while (1) {
279                 if (remaining == NULL || remaining[0] == '\0')
280                         break;
281                 token = strsep(&remaining, ",:");
282                 if (token == NULL)
283                         break;
284                 bus = strtol(token, NULL, 10);
285                 token = strsep(&remaining, ",:");
286                 if (token == NULL)
287                         break;
288                 device = strtol(token, NULL, 10);
289                 token = strsep(&remaining, ",:");
290                 if (token == NULL)
291                         break;
292                 function = strtol(token, NULL, 10);
293
294                 dev = pci_find_bsf(bus, device, function);
295                 if (dev == NULL)
296                         continue;
297
298                 for (i = 0; i < NUM_DEVICES; i++)
299                         if (pci_get_vendor(dev) == devices[i].vend &&
300                                         pci_get_device(dev) == devices[i].dev) {
301                                                 if (num_detached < MAX_DETACHED_DEVICES) {
302                                                         printf("nic_uio_load: detaching and storing dev=%p\n", dev);
303                                                         detached_devices[num_detached++] = dev;
304                                                 } else
305                                                         printf("nic_uio_load: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
306                                                                 MAX_DETACHED_DEVICES, dev);
307                                                 device_detach(dev);
308                         }
309         }
310 }
311
312 static void
313 nic_uio_unload(void)
314 {
315         int i;
316         printf("nic_uio_unload: entered...\n");
317
318         for (i = 0; i < num_detached; i++) {
319                 printf("nic_uio_unload: calling to device_probe_and_attach for dev=%p...\n",
320                         detached_devices[i]);
321                 device_probe_and_attach(detached_devices[i]);
322                 printf("nic_uio_unload: done.\n");
323         }
324
325         printf("nic_uio_unload: leaving...\n");
326 }
327
328 static int
329 nic_uio_shutdown(void)
330 {
331         return 0;
332 }
333
334 static int
335 nic_uio_modevent(module_t mod, int type, void *arg)
336 {
337
338         switch (type) {
339         case MOD_LOAD:
340                 nic_uio_load();
341                 break;
342         case MOD_UNLOAD:
343                 nic_uio_unload();
344                 break;
345         case MOD_SHUTDOWN:
346                 nic_uio_shutdown();
347                 break;
348         default:
349                 break;
350         }
351
352         return 0;
353 }