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