add FreeBSD support
[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/proc.h>
44
45 #include <machine/bus.h>
46 #include <dev/pci/pcivar.h> /* For pci_get macros! */
47 #include <dev/pci/pcireg.h> /* The softc holds our per-instance data. */
48 #include <vm/vm.h>
49 #include <vm/uma.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_pager.h>
53
54
55 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
56
57
58 struct nic_uio_softc {
59         device_t        dev_t;
60         struct cdev     *my_cdev;
61         int              bar_id[MAX_BARS];
62         struct resource *bar_res[MAX_BARS];
63         u_long           bar_start[MAX_BARS];
64         u_long           bar_size[MAX_BARS];
65 };
66
67 /* Function prototypes */
68 static d_open_t         nic_uio_open;
69 static d_close_t        nic_uio_close;
70 static d_mmap_t         nic_uio_mmap;
71 static d_mmap_single_t  nic_uio_mmap_single;
72 static int              nic_uio_probe(device_t dev);
73 static int              nic_uio_attach(device_t dev);
74 static int              nic_uio_detach(device_t dev);
75 static int              nic_uio_shutdown(void);
76 static int              nic_uio_modevent(module_t mod, int type, void *arg);
77
78 static struct cdevsw uio_cdevsw = {
79                 .d_name        = "nic_uio",
80                 .d_version     = D_VERSION,
81                 .d_open        = nic_uio_open,
82                 .d_close       = nic_uio_close,
83                 .d_mmap        = nic_uio_mmap,
84                 .d_mmap_single = nic_uio_mmap_single,
85 };
86
87 static device_method_t nic_uio_methods[] = {
88         DEVMETHOD(device_probe,    nic_uio_probe),
89         DEVMETHOD(device_attach,   nic_uio_attach),
90         DEVMETHOD(device_detach,   nic_uio_detach),
91         DEVMETHOD_END
92 };
93
94 struct device {
95     int vend;
96     int dev;
97 };
98
99 struct pci_bdf {
100         uint32_t bus;
101         uint32_t devid;
102         uint32_t function;
103 };
104
105
106 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev)      {vend, dev},
107 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev)     {vend, dev},
108 #define RTE_PCI_DEV_ID_DECL_IGBVF(vend, dev)   {vend, dev},
109 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev)   {vend, dev},
110 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {vend, dev},
111 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev)  {vend, dev},
112
113 const struct device devices[] = {
114 #include <rte_pci_dev_ids.h>
115 };
116 #define NUM_DEVICES (sizeof(devices)/sizeof(devices[0]))
117
118
119 static devclass_t nic_uio_devclass;
120
121 DEFINE_CLASS_0(nic_uio, nic_uio_driver, nic_uio_methods, sizeof(struct nic_uio_softc));
122 DRIVER_MODULE(nic_uio, pci, nic_uio_driver, nic_uio_devclass, nic_uio_modevent, 0);
123
124 static int
125 nic_uio_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
126                 int prot, vm_memattr_t *memattr)
127 {
128         *paddr = offset;
129         return (0);
130 }
131
132 static int
133 nic_uio_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
134                 struct vm_object **obj, int nprot)
135 {
136         /*
137          * The BAR index is encoded in the offset.  Divide the offset by
138          *  PAGE_SIZE to get the index of the bar requested by the user
139          *  app.
140          */
141         unsigned bar = *offset/PAGE_SIZE;
142         struct nic_uio_softc *sc = cdev->si_drv1;
143
144         if (bar >= MAX_BARS)
145                 return EINVAL;
146
147         if (sc->bar_res[bar] == NULL) {
148                 sc->bar_id[bar] = PCIR_BAR(bar);
149
150                 if (PCI_BAR_IO(pci_read_config(sc->dev_t, sc->bar_id[bar], 4)))
151                         sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_IOPORT,
152                                         &sc->bar_id[bar], RF_ACTIVE);
153                 else
154                         sc->bar_res[bar] = bus_alloc_resource_any(sc->dev_t, SYS_RES_MEMORY,
155                                         &sc->bar_id[bar], RF_ACTIVE);
156         }
157         if (sc->bar_res[bar] == NULL)
158                 return ENXIO;
159
160         sc->bar_start[bar] = rman_get_start(sc->bar_res[bar]);
161         sc->bar_size[bar] = rman_get_size(sc->bar_res[bar]);
162
163         device_printf(sc->dev_t, "Bar %u @ %lx, size %lx\n", bar,
164                         sc->bar_start[bar], sc->bar_size[bar]);
165
166         *offset = sc->bar_start[bar];
167         *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
168                                 curthread->td_ucred);
169         return 0;
170 }
171
172
173 int
174 nic_uio_open(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
175 {
176         return 0;
177 }
178
179 int
180 nic_uio_close(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
181 {
182         return 0;
183 }
184
185 static int
186 nic_uio_probe (device_t dev)
187 {
188         int i;
189
190         for (i = 0; i < NUM_DEVICES; i++)
191                 if (pci_get_vendor(dev) == devices[i].vend &&
192                         pci_get_device(dev) == devices[i].dev) {
193
194                         device_set_desc(dev, "Intel(R) DPDK PCI Device");
195                         return (BUS_PROBE_SPECIFIC);
196                 }
197
198         return (ENXIO);
199 }
200
201 static int
202 nic_uio_attach(device_t dev)
203 {
204         int i;
205         struct nic_uio_softc *sc;
206
207         sc = device_get_softc(dev);
208         sc->dev_t = dev;
209         sc->my_cdev = make_dev(&uio_cdevsw, device_get_unit(dev),
210                         UID_ROOT, GID_WHEEL, 0600, "uio@pci:%u:%u:%u",
211                         pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
212         if (sc->my_cdev == NULL)
213                 return ENXIO;
214         sc->my_cdev->si_drv1 = sc;
215
216         for (i = 0; i < MAX_BARS; i++)
217                 sc->bar_res[i] = NULL;
218
219         pci_enable_busmaster(dev);
220
221         return 0;
222 }
223
224 static int
225 nic_uio_detach(device_t dev)
226 {
227         int i;
228         struct nic_uio_softc *sc;
229         sc = device_get_softc(dev);
230
231         for (i = 0; i < MAX_BARS; i++)
232                 if (sc->bar_res[i] != NULL) {
233
234                         if (PCI_BAR_IO(pci_read_config(dev, sc->bar_id[i], 4)))
235                                 bus_release_resource(dev, SYS_RES_IOPORT, sc->bar_id[i],
236                                                 sc->bar_res[i]);
237                         else 
238                                 bus_release_resource(dev, SYS_RES_MEMORY, sc->bar_id[i],
239                                                 sc->bar_res[i]);
240                 }
241
242         if (sc->my_cdev != NULL)
243                 destroy_dev(sc->my_cdev);
244         return 0;
245 }
246
247 static void
248 nic_uio_load(void)
249 {
250         uint32_t bus, device, function;
251         int i;
252         device_t dev;
253         char bdf_str[256];
254         char *token, *remaining;
255
256         memset(bdf_str, 0, sizeof(bdf_str));
257         TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str, sizeof(bdf_str));
258         remaining = bdf_str;
259         /*
260          * Users should specify PCI BDFs in the format "b:d:f,b:d:f,b:d:f".
261          *  But the code below does not try differentiate between : and ,
262          *  and just blindly uses 3 tokens at a time to construct a
263          *  bus/device/function tuple.
264          *
265          * There is no checking on strtol() return values, but this should
266          *  be OK.  Worst case is it cannot convert and returns 0.  This
267          *  could give us a different BDF than intended, but as long as the
268          *  PCI device/vendor ID does not match it will not matter.
269          */
270         while (1) {
271                 if (remaining == NULL || remaining[0] == '\0')
272                         break;
273                 token = strsep(&remaining, ",:");
274                 if (token == NULL)
275                         break;
276                 bus = strtol(token, NULL, 10);
277                 token = strsep(&remaining, ",:");
278                 if (token == NULL)
279                         break;
280                 device = strtol(token, NULL, 10);
281                 token = strsep(&remaining, ",:");
282                 if (token == NULL)
283                         break;
284                 function = strtol(token, NULL, 10);
285
286                 dev = pci_find_bsf(bus, device, function);
287                 if (dev != NULL)
288                         for (i = 0; i < NUM_DEVICES; i++) 
289                                 if (pci_get_vendor(dev) == devices[i].vend &&
290                                                 pci_get_device(dev) == devices[i].dev)
291                                                         device_detach(dev);
292         }
293 }
294
295 static void
296 nic_uio_unload(void)
297 {
298 }
299
300 static int
301 nic_uio_shutdown(void)
302 {
303         return (0);
304 }
305
306 static int
307 nic_uio_modevent(module_t mod, int type, void *arg)
308 {
309
310         switch (type) {
311         case MOD_LOAD:
312                 nic_uio_load();
313                 break;
314         case MOD_UNLOAD:
315                 nic_uio_unload();
316                 break;
317         case MOD_SHUTDOWN:
318                 nic_uio_shutdown();
319                 break;
320         default:
321                 break;
322         }
323
324         return (0);
325 }