4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * The full GNU General Public License is included in this distribution
19 * in the file called LICENSE.GPL.
21 * Contact Information:
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/netdevice.h>
29 #include <linux/pci.h>
30 #include <linux/kthread.h>
32 #include <exec-env/rte_kni_common.h>
34 #include <rte_config.h>
36 MODULE_LICENSE("Dual BSD/GPL");
37 MODULE_AUTHOR("Intel Corporation");
38 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
40 #define KNI_RX_LOOP_NUM 1000
42 #define KNI_MAX_DEVICES 32
44 extern void kni_net_rx(struct kni_dev *kni);
45 extern void kni_net_init(struct net_device *dev);
46 extern void kni_net_config_lo_mode(char *lo_str);
47 extern void kni_net_poll_resp(struct kni_dev *kni);
48 extern void kni_set_ethtool_ops(struct net_device *netdev);
50 extern int ixgbe_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
51 extern void ixgbe_kni_remove(struct pci_dev *pdev);
52 extern int igb_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
53 extern void igb_kni_remove(struct pci_dev *pdev);
55 static int kni_open(struct inode *inode, struct file *file);
56 static int kni_release(struct inode *inode, struct file *file);
57 static int kni_ioctl(struct inode *inode, unsigned int ioctl_num,
58 unsigned long ioctl_param);
59 static int kni_compat_ioctl(struct inode *inode, unsigned int ioctl_num,
60 unsigned long ioctl_param);
62 /* kni kernel thread for rx */
63 static int kni_thread(void *unused);
65 static struct file_operations kni_fops = {
68 .release = kni_release,
69 .unlocked_ioctl = (void *)kni_ioctl,
70 .compat_ioctl = (void *)kni_compat_ioctl,
73 static struct miscdevice kni_misc = {
74 .minor = MISC_DYNAMIC_MINOR,
79 /* Array of the kni supported PCI device ids */
80 static struct pci_device_id kni_pci_ids[] = {
81 /* EM and IGB to be supported in future */
82 //#define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {PCI_DEVICE(vend, dev)},
83 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {PCI_DEVICE(vend, dev)},
84 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {PCI_DEVICE(vend, dev)},
85 #include <rte_pci_dev_ids.h>
89 MODULE_DEVICE_TABLE(pci, kni_pci_ids);
92 static char *lo_mode = NULL;
94 static struct kni_dev *kni_devs[KNI_MAX_DEVICES];
95 static volatile int num_devs; /* number of kni devices */
97 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
99 static volatile unsigned long device_in_use; /* device in use flag */
100 static struct task_struct *kni_kthread;
105 KNI_PRINT("######## DPDK kni module loading ########\n");
107 if (misc_register(&kni_misc) != 0) {
108 KNI_ERR("Misc registration failed\n");
112 /* Clear the bit of device in use */
113 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
115 /* Configure the lo mode according to the input parameter */
116 kni_net_config_lo_mode(lo_mode);
118 KNI_PRINT("######## DPDK kni module loaded ########\n");
126 misc_deregister(&kni_misc);
127 KNI_PRINT("####### DPDK kni module unloaded #######\n");
131 kni_open(struct inode *inode, struct file *file)
133 /* kni device can be opened by one user only, test and set bit */
134 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use))
137 memset(kni_devs, 0, sizeof(kni_devs));
140 /* Create kernel thread for RX */
141 kni_kthread = kthread_run(kni_thread, NULL, "kni_thread");
142 if (IS_ERR(kni_kthread)) {
143 KNI_ERR("Unable to create kernel threaed\n");
144 return PTR_ERR(kni_kthread);
147 KNI_PRINT("/dev/kni opened\n");
153 kni_release(struct inode *inode, struct file *file)
157 KNI_PRINT("Stopping KNI thread...");
159 /* Stop kernel thread */
160 kthread_stop(kni_kthread);
163 for (i = 0; i < KNI_MAX_DEVICES; i++) {
164 if (kni_devs[i] != NULL) {
165 /* Call the remove part to restore pci dev */
166 switch (kni_devs[i]->device_id) {
167 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
168 #include <rte_pci_dev_ids.h>
169 igb_kni_remove(kni_devs[i]->pci_dev);
171 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
172 #include <rte_pci_dev_ids.h>
173 ixgbe_kni_remove(kni_devs[i]->pci_dev);
179 unregister_netdev(kni_devs[i]->net_dev);
180 free_netdev(kni_devs[i]->net_dev);
186 /* Clear the bit of device in use */
187 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
189 KNI_PRINT("/dev/kni closed\n");
195 kni_thread(void *unused)
199 KNI_PRINT("Kernel thread for KNI started\n");
200 while (!kthread_should_stop()) {
201 int n_devs = num_devs;
202 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
203 for (i = 0; i < n_devs; i++) {
204 /* This shouldn't be needed */
206 kni_net_rx(kni_devs[i]);
207 kni_net_poll_resp(kni_devs[i]);
210 KNI_ERR("kni_thread -no kni found!!!");
213 /* reschedule out for a while */
214 schedule_timeout_interruptible(usecs_to_jiffies( \
215 KNI_KTHREAD_RESCHEDULE_INTERVAL));
217 KNI_PRINT("Kernel thread for KNI stopped\n");
223 kni_check_pci_device_id(uint16_t vendor_id, uint16_t device_id)
225 int i, total = sizeof(kni_pci_ids)/sizeof(struct pci_device_id);
226 struct pci_device_id *p;
228 /* Check if the vendor id/device id are supported */
229 for (i = 0; i < total; i++) {
231 if (p->vendor != vendor_id && p->vendor != PCI_ANY_ID)
233 if (p->device != device_id && p->device != PCI_ANY_ID)
242 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
245 struct rte_kni_device_info dev_info;
246 struct pci_dev *pci = NULL;
247 struct pci_dev *found_pci = NULL;
248 struct net_device *net_dev = NULL;
249 struct net_device *lad_dev = NULL;
252 if (num_devs == KNI_MAX_DEVICES)
255 /* Check the buffer size, to avoid warning */
256 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
259 /* Copy kni info from user space */
260 ret = copy_from_user(&dev_info, (void *)ioctl_param,
261 _IOC_SIZE(ioctl_num));
263 KNI_ERR("copy_from_user");
267 /* Check if the PCI id is supported by KNI */
268 ret = kni_check_pci_device_id(dev_info.vendor_id, dev_info.device_id);
270 KNI_ERR("Invalid vendor_id: %x or device_id: %x\n",
271 dev_info.vendor_id, dev_info.device_id);
275 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
277 if (net_dev == NULL) {
278 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
282 kni = netdev_priv(net_dev);
284 kni->net_dev = net_dev;
287 /* Translate user space info into kernel space info */
288 kni->tx_q = phys_to_virt(dev_info.tx_phys);
289 kni->rx_q = phys_to_virt(dev_info.rx_phys);
290 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
291 kni->free_q = phys_to_virt(dev_info.free_phys);
293 kni->req_q = phys_to_virt(dev_info.req_phys);
294 kni->resp_q = phys_to_virt(dev_info.resp_phys);
296 kni->sync_va = dev_info.sync_va;
297 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
299 kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
300 kni->mbuf_va = dev_info.mbuf_va;
302 kni->mbuf_size = dev_info.mbuf_size;
304 KNI_PRINT("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
305 dev_info.tx_phys, kni->tx_q);
306 KNI_PRINT("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
307 dev_info.rx_phys, kni->rx_q);
308 KNI_PRINT("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
309 dev_info.alloc_phys, kni->alloc_q);
310 KNI_PRINT("free_phys: 0x%016llx, free_q addr: 0x%p\n",
311 dev_info.free_phys, kni->free_q);
312 KNI_PRINT("req_phys: 0x%016llx, req_q addr: 0x%p\n",
313 dev_info.req_phys, kni->req_q);
314 KNI_PRINT("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
315 dev_info.resp_phys, kni->resp_q);
316 KNI_PRINT("mbuf_phys: 0x%016llx, mbuf_kva: 0x%p\n",
317 dev_info.mbuf_phys, kni->mbuf_kva);
318 KNI_PRINT("mbuf_va: 0x%p\n", dev_info.mbuf_va);
319 KNI_PRINT("mbuf_size: %u\n", kni->mbuf_size);
321 KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n", dev_info.bus, dev_info.devid,
322 dev_info.function, dev_info.vendor_id, dev_info.device_id);
324 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
326 /* Support Ethtool */
328 KNI_PRINT("pci_bus: %02x:%02x:%02x \n", pci->bus->number,
329 PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
331 if ((pci->bus->number == dev_info.bus) &&
332 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
333 (PCI_FUNC(pci->devfn) == dev_info.function)) {
336 switch (dev_info.device_id) {
337 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
338 #include <rte_pci_dev_ids.h>
339 ret = igb_kni_probe(found_pci, &lad_dev);
341 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
342 #include <rte_pci_dev_ids.h>
343 ret = ixgbe_kni_probe(found_pci, &lad_dev);
350 KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n", pci, lad_dev);
352 kni->lad_dev = lad_dev;
353 kni_set_ethtool_ops(kni->net_dev);
356 KNI_ERR("Device not supported by ethtool");
360 kni->pci_dev = found_pci;
361 kni->device_id = dev_info.device_id;
364 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id,
371 ret = register_netdev(net_dev);
373 KNI_ERR("error %i registering device \"%s\"\n", ret,
375 free_netdev(net_dev);
379 kni_devs[num_devs++] = kni;
385 kni_ioctl(struct inode *inode,
386 unsigned int ioctl_num,
387 unsigned long ioctl_param)
391 KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
394 * Switch according to the ioctl called
396 switch (_IOC_NR(ioctl_num)) {
397 case _IOC_NR(RTE_KNI_IOCTL_TEST):
398 /* For test only, not used */
400 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
401 ret = kni_ioctl_create(ioctl_num, ioctl_param);
404 KNI_DBG("IOCTL default \n");
412 kni_compat_ioctl(struct inode *inode,
413 unsigned int ioctl_num,
414 unsigned long ioctl_param)
416 /* 32 bits app on 64 bits OS to be supported later */
417 KNI_PRINT("Not implemented.\n");
422 module_init(kni_init);
423 module_exit(kni_exit);
425 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
426 MODULE_PARM_DESC(lo_mode,
427 "KNI loopback mode (default=lo_mode_none):\n"
428 " lo_mode_none Kernel loopback disabled\n"
429 " lo_mode_fifo Enable kernel loopback with fifo\n"
430 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"