4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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 */
42 #include <sys/systm.h>
43 #include <sys/rwlock.h>
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. */
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
56 #define MAX_BARS (PCIR_MAX_BAR_0 + 1)
58 #define MAX_DETACHED_DEVICES 128
59 static device_t detached_devices
[MAX_DETACHED_DEVICES
] = {};
60 static int num_detached
= 0;
62 struct nic_uio_softc
{
66 struct resource
*bar_res
[MAX_BARS
];
67 u_long bar_start
[MAX_BARS
];
68 u_long bar_size
[MAX_BARS
];
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
);
82 static struct cdevsw uio_cdevsw
= {
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
,
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
),
109 static devclass_t nic_uio_devclass
;
111 DEFINE_CLASS_0(nic_uio
, nic_uio_driver
, nic_uio_methods
, sizeof(struct nic_uio_softc
));
112 DRIVER_MODULE(nic_uio
, pci
, nic_uio_driver
, nic_uio_devclass
, nic_uio_modevent
, 0);
115 nic_uio_mmap(struct cdev
*cdev
, vm_ooffset_t offset
, vm_paddr_t
*paddr
,
116 int prot
, vm_memattr_t
*memattr
)
123 nic_uio_mmap_single(struct cdev
*cdev
, vm_ooffset_t
*offset
, vm_size_t size
,
124 struct vm_object
**obj
, int nprot
)
127 * The BAR index is encoded in the offset. Divide the offset by
128 * PAGE_SIZE to get the index of the bar requested by the user
131 unsigned bar
= *offset
/PAGE_SIZE
;
132 struct nic_uio_softc
*sc
= cdev
->si_drv1
;
137 if (sc
->bar_res
[bar
] == NULL
) {
138 sc
->bar_id
[bar
] = PCIR_BAR(bar
);
140 if (PCI_BAR_IO(pci_read_config(sc
->dev_t
, sc
->bar_id
[bar
], 4)))
141 sc
->bar_res
[bar
] = bus_alloc_resource_any(sc
->dev_t
, SYS_RES_IOPORT
,
142 &sc
->bar_id
[bar
], RF_ACTIVE
);
144 sc
->bar_res
[bar
] = bus_alloc_resource_any(sc
->dev_t
, SYS_RES_MEMORY
,
145 &sc
->bar_id
[bar
], RF_ACTIVE
);
147 if (sc
->bar_res
[bar
] == NULL
)
150 sc
->bar_start
[bar
] = rman_get_start(sc
->bar_res
[bar
]);
151 sc
->bar_size
[bar
] = rman_get_size(sc
->bar_res
[bar
]);
153 device_printf(sc
->dev_t
, "Bar %u @ %lx, size %lx\n", bar
,
154 sc
->bar_start
[bar
], sc
->bar_size
[bar
]);
156 *offset
= sc
->bar_start
[bar
];
157 *obj
= vm_pager_allocate(OBJT_DEVICE
, cdev
, size
, nprot
, *offset
,
158 curthread
->td_ucred
);
164 nic_uio_open(struct cdev
*dev
, int oflags
, int devtype
, struct thread
*td
)
170 nic_uio_close(struct cdev
*dev
, int fflag
, int devtype
, struct thread
*td
)
176 nic_uio_probe (device_t dev
)
179 unsigned int bus
= pci_get_bus(dev
);
180 unsigned int device
= pci_get_slot(dev
);
181 unsigned int function
= pci_get_function(dev
);
183 for (i
= 0; i
< num_detached
; i
++)
184 if (bus
== pci_get_bus(detached_devices
[i
]) &&
185 device
== pci_get_slot(detached_devices
[i
]) &&
186 function
== pci_get_function(detached_devices
[i
])) {
187 device_set_desc(dev
, "DPDK PCI Device");
188 return BUS_PROBE_SPECIFIC
;
195 nic_uio_attach(device_t dev
)
198 struct nic_uio_softc
*sc
;
200 sc
= device_get_softc(dev
);
202 sc
->my_cdev
= make_dev(&uio_cdevsw
, device_get_unit(dev
),
203 UID_ROOT
, GID_WHEEL
, 0600, "uio@pci:%u:%u:%u",
204 pci_get_bus(dev
), pci_get_slot(dev
), pci_get_function(dev
));
205 if (sc
->my_cdev
== NULL
)
207 sc
->my_cdev
->si_drv1
= sc
;
209 for (i
= 0; i
< MAX_BARS
; i
++)
210 sc
->bar_res
[i
] = NULL
;
212 pci_enable_busmaster(dev
);
218 nic_uio_detach(device_t dev
)
221 struct nic_uio_softc
*sc
;
222 sc
= device_get_softc(dev
);
224 for (i
= 0; i
< MAX_BARS
; i
++)
225 if (sc
->bar_res
[i
] != NULL
) {
227 if (PCI_BAR_IO(pci_read_config(dev
, sc
->bar_id
[i
], 4)))
228 bus_release_resource(dev
, SYS_RES_IOPORT
, sc
->bar_id
[i
],
231 bus_release_resource(dev
, SYS_RES_MEMORY
, sc
->bar_id
[i
],
235 if (sc
->my_cdev
!= NULL
)
236 destroy_dev(sc
->my_cdev
);
243 uint32_t bus
, device
, function
;
246 char *token
, *remaining
;
248 memset(bdf_str
, 0, sizeof(bdf_str
));
249 TUNABLE_STR_FETCH("hw.nic_uio.bdfs", bdf_str
, sizeof(bdf_str
));
252 * Users should specify PCI BDFs in the format "b:d:f,b:d:f,b:d:f".
253 * But the code below does not try differentiate between : and ,
254 * and just blindly uses 3 tokens at a time to construct a
255 * bus/device/function tuple.
257 * There is no checking on strtol() return values, but this should
258 * be OK. Worst case is it cannot convert and returns 0. This
259 * could give us a different BDF than intended, but as long as the
260 * PCI device/vendor ID does not match it will not matter.
263 if (remaining
== NULL
|| remaining
[0] == '\0')
265 token
= strsep(&remaining
, ",:");
268 bus
= strtol(token
, NULL
, 10);
269 token
= strsep(&remaining
, ",:");
272 device
= strtol(token
, NULL
, 10);
273 token
= strsep(&remaining
, ",:");
276 function
= strtol(token
, NULL
, 10);
278 dev
= pci_find_bsf(bus
, device
, function
);
282 if (num_detached
< MAX_DETACHED_DEVICES
) {
283 printf("nic_uio_load: detaching and storing dev=%p\n",
285 detached_devices
[num_detached
++] = dev
;
287 printf("nic_uio_load: reached MAX_DETACHED_DEVICES=%d. dev=%p won't be reattached\n",
288 MAX_DETACHED_DEVICES
, dev
);
298 printf("nic_uio_unload: entered...\n");
300 for (i
= 0; i
< num_detached
; i
++) {
301 printf("nic_uio_unload: calling to device_probe_and_attach for dev=%p...\n",
302 detached_devices
[i
]);
303 device_probe_and_attach(detached_devices
[i
]);
304 printf("nic_uio_unload: done.\n");
307 printf("nic_uio_unload: leaving...\n");
311 nic_uio_shutdown(void)
317 nic_uio_modevent(module_t mod
, int type
, void *arg
)