4 * Copyright(c) 2010-2013 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>
31 #include <linux/rwsem.h>
33 #include <exec-env/rte_kni_common.h>
35 #include <rte_config.h>
37 MODULE_LICENSE("Dual BSD/GPL");
38 MODULE_AUTHOR("Intel Corporation");
39 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
41 #define KNI_RX_LOOP_NUM 1000
43 #define KNI_MAX_DEVICES 32
45 extern void kni_net_rx(struct kni_dev *kni);
46 extern void kni_net_init(struct net_device *dev);
47 extern void kni_net_config_lo_mode(char *lo_str);
48 extern void kni_net_poll_resp(struct kni_dev *kni);
49 extern void kni_set_ethtool_ops(struct net_device *netdev);
51 extern int ixgbe_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
52 extern void ixgbe_kni_remove(struct pci_dev *pdev);
53 extern int igb_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
54 extern void igb_kni_remove(struct pci_dev *pdev);
56 static int kni_open(struct inode *inode, struct file *file);
57 static int kni_release(struct inode *inode, struct file *file);
58 static int kni_ioctl(struct inode *inode, unsigned int ioctl_num,
59 unsigned long ioctl_param);
60 static int kni_compat_ioctl(struct inode *inode, unsigned int ioctl_num,
61 unsigned long ioctl_param);
63 /* kni kernel thread for rx */
64 static int kni_thread(void *unused);
66 static struct file_operations kni_fops = {
69 .release = kni_release,
70 .unlocked_ioctl = (void *)kni_ioctl,
71 .compat_ioctl = (void *)kni_compat_ioctl,
74 static struct miscdevice kni_misc = {
75 .minor = MISC_DYNAMIC_MINOR,
81 static char *lo_mode = NULL;
83 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
85 static volatile unsigned long device_in_use; /* device in use flag */
86 static struct task_struct *kni_kthread;
89 static DECLARE_RWSEM(kni_list_lock);
92 static struct list_head kni_list_head = LIST_HEAD_INIT(kni_list_head);
97 KNI_PRINT("######## DPDK kni module loading ########\n");
99 if (misc_register(&kni_misc) != 0) {
100 KNI_ERR("Misc registration failed\n");
104 /* Clear the bit of device in use */
105 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
107 /* Configure the lo mode according to the input parameter */
108 kni_net_config_lo_mode(lo_mode);
110 KNI_PRINT("######## DPDK kni module loaded ########\n");
118 misc_deregister(&kni_misc);
119 KNI_PRINT("####### DPDK kni module unloaded #######\n");
123 kni_open(struct inode *inode, struct file *file)
125 /* kni device can be opened by one user only, test and set bit */
126 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use))
129 /* Create kernel thread for RX */
130 kni_kthread = kthread_run(kni_thread, NULL, "kni_thread");
131 if (IS_ERR(kni_kthread)) {
132 KNI_ERR("Unable to create kernel threaed\n");
133 return PTR_ERR(kni_kthread);
136 KNI_PRINT("/dev/kni opened\n");
142 kni_release(struct inode *inode, struct file *file)
144 struct kni_dev *dev, *n;
146 KNI_PRINT("Stopping KNI thread...");
148 /* Stop kernel thread */
149 kthread_stop(kni_kthread);
152 down_write(&kni_list_lock);
153 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
154 /* Call the remove part to restore pci dev */
155 switch (dev->device_id) {
156 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
157 #include <rte_pci_dev_ids.h>
158 igb_kni_remove(dev->pci_dev);
160 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
161 #include <rte_pci_dev_ids.h>
162 ixgbe_kni_remove(dev->pci_dev);
167 unregister_netdev(dev->net_dev);
168 free_netdev(dev->net_dev);
169 list_del(&dev->list);
171 up_write(&kni_list_lock);
173 /* Clear the bit of device in use */
174 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
176 KNI_PRINT("/dev/kni closed\n");
182 kni_thread(void *unused)
185 struct kni_dev *dev, *n;
187 KNI_PRINT("Kernel thread for KNI started\n");
188 while (!kthread_should_stop()) {
189 down_read(&kni_list_lock);
190 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
191 list_for_each_entry_safe(dev, n,
192 &kni_list_head, list) {
194 kni_net_poll_resp(dev);
197 up_read(&kni_list_lock);
198 /* reschedule out for a while */
199 schedule_timeout_interruptible(usecs_to_jiffies( \
200 KNI_KTHREAD_RESCHEDULE_INTERVAL));
202 KNI_PRINT("Kernel thread for KNI stopped\n");
208 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
211 struct rte_kni_device_info dev_info;
212 struct pci_dev *pci = NULL;
213 struct pci_dev *found_pci = NULL;
214 struct net_device *net_dev = NULL;
215 struct net_device *lad_dev = NULL;
216 struct kni_dev *kni, *dev, *n;
218 printk(KERN_INFO "KNI: Creating kni...\n");
219 /* Check the buffer size, to avoid warning */
220 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
223 /* Copy kni info from user space */
224 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
226 KNI_ERR("copy_from_user in kni_ioctl_create");
230 /* Check if it has been created */
231 down_read(&kni_list_lock);
232 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
233 if (dev->port_id == dev_info.port_id) {
234 up_read(&kni_list_lock);
235 KNI_ERR("Port %d has already been created\n",
240 up_read(&kni_list_lock);
242 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
244 if (net_dev == NULL) {
245 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
249 kni = netdev_priv(net_dev);
251 kni->net_dev = net_dev;
252 kni->port_id = dev_info.port_id;
254 /* Translate user space info into kernel space info */
255 kni->tx_q = phys_to_virt(dev_info.tx_phys);
256 kni->rx_q = phys_to_virt(dev_info.rx_phys);
257 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
258 kni->free_q = phys_to_virt(dev_info.free_phys);
260 kni->req_q = phys_to_virt(dev_info.req_phys);
261 kni->resp_q = phys_to_virt(dev_info.resp_phys);
263 kni->sync_va = dev_info.sync_va;
264 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
266 kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
267 kni->mbuf_va = dev_info.mbuf_va;
269 kni->mbuf_size = dev_info.mbuf_size;
271 KNI_PRINT("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
272 (unsigned long long) dev_info.tx_phys, kni->tx_q);
273 KNI_PRINT("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
274 (unsigned long long) dev_info.rx_phys, kni->rx_q);
275 KNI_PRINT("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
276 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
277 KNI_PRINT("free_phys: 0x%016llx, free_q addr: 0x%p\n",
278 (unsigned long long) dev_info.free_phys, kni->free_q);
279 KNI_PRINT("req_phys: 0x%016llx, req_q addr: 0x%p\n",
280 (unsigned long long) dev_info.req_phys, kni->req_q);
281 KNI_PRINT("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
282 (unsigned long long) dev_info.resp_phys, kni->resp_q);
283 KNI_PRINT("mbuf_phys: 0x%016llx, mbuf_kva: 0x%p\n",
284 (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
285 KNI_PRINT("mbuf_va: 0x%p\n", dev_info.mbuf_va);
286 KNI_PRINT("mbuf_size: %u\n", kni->mbuf_size);
288 KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n", dev_info.bus, dev_info.devid,
289 dev_info.function, dev_info.vendor_id, dev_info.device_id);
291 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
293 /* Support Ethtool */
295 KNI_PRINT("pci_bus: %02x:%02x:%02x \n", pci->bus->number,
296 PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
298 if ((pci->bus->number == dev_info.bus) &&
299 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
300 (PCI_FUNC(pci->devfn) == dev_info.function)) {
303 switch (dev_info.device_id) {
304 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
305 #include <rte_pci_dev_ids.h>
306 ret = igb_kni_probe(found_pci, &lad_dev);
308 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
309 #include <rte_pci_dev_ids.h>
310 ret = ixgbe_kni_probe(found_pci, &lad_dev);
317 KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n", pci, lad_dev);
319 kni->lad_dev = lad_dev;
320 kni_set_ethtool_ops(kni->net_dev);
323 KNI_ERR("Device not supported by ethtool");
327 kni->pci_dev = found_pci;
328 kni->device_id = dev_info.device_id;
331 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id,
338 ret = register_netdev(net_dev);
340 KNI_ERR("error %i registering device \"%s\"\n", ret,
342 free_netdev(net_dev);
346 down_write(&kni_list_lock);
347 list_add(&kni->list, &kni_list_head);
348 up_write(&kni_list_lock);
349 printk(KERN_INFO "KNI: Successfully create kni for port %d\n",
356 kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
360 struct kni_dev *dev, *n;
362 if (_IOC_SIZE(ioctl_num) > sizeof(port_id))
365 ret = copy_from_user(&port_id, (void *)ioctl_param, sizeof(port_id));
367 KNI_ERR("copy_from_user in kni_ioctl_release");
371 down_write(&kni_list_lock);
372 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
373 if (dev->port_id != port_id)
376 switch (dev->device_id) {
377 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
378 #include <rte_pci_dev_ids.h>
379 igb_kni_remove(dev->pci_dev);
381 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
382 #include <rte_pci_dev_ids.h>
383 ixgbe_kni_remove(dev->pci_dev);
388 unregister_netdev(dev->net_dev);
389 free_netdev(dev->net_dev);
390 list_del(&dev->list);
394 up_write(&kni_list_lock);
395 printk(KERN_INFO "KNI: %s release kni for port %d\n",
396 (ret == 0 ? "Successfully" : "Unsuccessfully"), port_id);
402 kni_ioctl(struct inode *inode,
403 unsigned int ioctl_num,
404 unsigned long ioctl_param)
408 KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
411 * Switch according to the ioctl called
413 switch (_IOC_NR(ioctl_num)) {
414 case _IOC_NR(RTE_KNI_IOCTL_TEST):
415 /* For test only, not used */
417 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
418 ret = kni_ioctl_create(ioctl_num, ioctl_param);
420 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
421 ret = kni_ioctl_release(ioctl_num, ioctl_param);
424 KNI_DBG("IOCTL default \n");
432 kni_compat_ioctl(struct inode *inode,
433 unsigned int ioctl_num,
434 unsigned long ioctl_param)
436 /* 32 bits app on 64 bits OS to be supported later */
437 KNI_PRINT("Not implemented.\n");
442 module_init(kni_init);
443 module_exit(kni_exit);
445 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
446 MODULE_PARM_DESC(lo_mode,
447 "KNI loopback mode (default=lo_mode_none):\n"
448 " lo_mode_none Kernel loopback disabled\n"
449 " lo_mode_fifo Enable kernel loopback with fifo\n"
450 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"