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:
25 #include <linux/module.h>
26 #include <linux/miscdevice.h>
27 #include <linux/netdevice.h>
28 #include <linux/pci.h>
29 #include <linux/kthread.h>
30 #include <linux/rwsem.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,
80 static char *lo_mode = NULL;
82 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
84 static volatile unsigned long device_in_use; /* device in use flag */
85 static struct task_struct *kni_kthread;
88 static DECLARE_RWSEM(kni_list_lock);
91 static struct list_head kni_list_head = LIST_HEAD_INIT(kni_list_head);
96 KNI_PRINT("######## DPDK kni module loading ########\n");
98 if (misc_register(&kni_misc) != 0) {
99 KNI_ERR("Misc registration failed\n");
103 /* Clear the bit of device in use */
104 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
106 /* Configure the lo mode according to the input parameter */
107 kni_net_config_lo_mode(lo_mode);
109 KNI_PRINT("######## DPDK kni module loaded ########\n");
117 misc_deregister(&kni_misc);
118 KNI_PRINT("####### DPDK kni module unloaded #######\n");
122 kni_open(struct inode *inode, struct file *file)
124 /* kni device can be opened by one user only, test and set bit */
125 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use))
128 /* Create kernel thread for RX */
129 kni_kthread = kthread_run(kni_thread, NULL, "kni_thread");
130 if (IS_ERR(kni_kthread)) {
131 KNI_ERR("Unable to create kernel threaed\n");
132 return PTR_ERR(kni_kthread);
135 KNI_PRINT("/dev/kni opened\n");
141 kni_release(struct inode *inode, struct file *file)
143 struct kni_dev *dev, *n;
145 KNI_PRINT("Stopping KNI thread...");
147 /* Stop kernel thread */
148 kthread_stop(kni_kthread);
151 down_write(&kni_list_lock);
152 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
153 /* Call the remove part to restore pci dev */
154 switch (dev->device_id) {
155 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
156 #include <rte_pci_dev_ids.h>
157 igb_kni_remove(dev->pci_dev);
159 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
160 #include <rte_pci_dev_ids.h>
161 ixgbe_kni_remove(dev->pci_dev);
166 unregister_netdev(dev->net_dev);
167 free_netdev(dev->net_dev);
168 list_del(&dev->list);
170 up_write(&kni_list_lock);
172 /* Clear the bit of device in use */
173 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
175 KNI_PRINT("/dev/kni closed\n");
181 kni_thread(void *unused)
184 struct kni_dev *dev, *n;
186 KNI_PRINT("Kernel thread for KNI started\n");
187 while (!kthread_should_stop()) {
188 down_read(&kni_list_lock);
189 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
190 list_for_each_entry_safe(dev, n,
191 &kni_list_head, list) {
193 kni_net_poll_resp(dev);
196 up_read(&kni_list_lock);
197 /* reschedule out for a while */
198 schedule_timeout_interruptible(usecs_to_jiffies( \
199 KNI_KTHREAD_RESCHEDULE_INTERVAL));
201 KNI_PRINT("Kernel thread for KNI stopped\n");
207 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
210 struct rte_kni_device_info dev_info;
211 struct pci_dev *pci = NULL;
212 struct pci_dev *found_pci = NULL;
213 struct net_device *net_dev = NULL;
214 struct net_device *lad_dev = NULL;
215 struct kni_dev *kni, *dev, *n;
217 printk(KERN_INFO "KNI: Creating kni...\n");
218 /* Check the buffer size, to avoid warning */
219 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
222 /* Copy kni info from user space */
223 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
225 KNI_ERR("copy_from_user in kni_ioctl_create");
229 /* Check if it has been created */
230 down_read(&kni_list_lock);
231 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
232 if (dev->port_id == dev_info.port_id) {
233 up_read(&kni_list_lock);
234 KNI_ERR("Port %d has already been created\n",
239 up_read(&kni_list_lock);
241 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
243 if (net_dev == NULL) {
244 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
248 kni = netdev_priv(net_dev);
250 kni->net_dev = net_dev;
251 kni->port_id = dev_info.port_id;
253 /* Translate user space info into kernel space info */
254 kni->tx_q = phys_to_virt(dev_info.tx_phys);
255 kni->rx_q = phys_to_virt(dev_info.rx_phys);
256 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
257 kni->free_q = phys_to_virt(dev_info.free_phys);
259 kni->req_q = phys_to_virt(dev_info.req_phys);
260 kni->resp_q = phys_to_virt(dev_info.resp_phys);
261 kni->sync_va = dev_info.sync_va;
262 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
264 kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
265 kni->mbuf_va = dev_info.mbuf_va;
267 kni->mbuf_size = dev_info.mbuf_size;
269 KNI_PRINT("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
270 (unsigned long long) dev_info.tx_phys, kni->tx_q);
271 KNI_PRINT("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
272 (unsigned long long) dev_info.rx_phys, kni->rx_q);
273 KNI_PRINT("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
274 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
275 KNI_PRINT("free_phys: 0x%016llx, free_q addr: 0x%p\n",
276 (unsigned long long) dev_info.free_phys, kni->free_q);
277 KNI_PRINT("req_phys: 0x%016llx, req_q addr: 0x%p\n",
278 (unsigned long long) dev_info.req_phys, kni->req_q);
279 KNI_PRINT("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
280 (unsigned long long) dev_info.resp_phys, kni->resp_q);
281 KNI_PRINT("mbuf_phys: 0x%016llx, mbuf_kva: 0x%p\n",
282 (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
283 KNI_PRINT("mbuf_va: 0x%p\n", dev_info.mbuf_va);
284 KNI_PRINT("mbuf_size: %u\n", kni->mbuf_size);
286 KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n", dev_info.bus, dev_info.devid,
287 dev_info.function, dev_info.vendor_id, dev_info.device_id);
289 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
291 /* Support Ethtool */
293 KNI_PRINT("pci_bus: %02x:%02x:%02x \n", pci->bus->number,
294 PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
296 if ((pci->bus->number == dev_info.bus) &&
297 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
298 (PCI_FUNC(pci->devfn) == dev_info.function)) {
300 switch (dev_info.device_id) {
301 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
302 #include <rte_pci_dev_ids.h>
303 ret = igb_kni_probe(found_pci, &lad_dev);
305 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
307 #include <rte_pci_dev_ids.h>
308 ret = ixgbe_kni_probe(found_pci, &lad_dev);
315 KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
318 kni->lad_dev = lad_dev;
319 kni_set_ethtool_ops(kni->net_dev);
321 KNI_ERR("Device not supported by ethtool");
325 kni->pci_dev = found_pci;
326 kni->device_id = dev_info.device_id;
329 pci = pci_get_device(dev_info.vendor_id,
330 dev_info.device_id, pci);
335 ret = register_netdev(net_dev);
337 KNI_ERR("error %i registering device \"%s\"\n",
339 free_netdev(net_dev);
343 down_write(&kni_list_lock);
344 list_add(&kni->list, &kni_list_head);
345 up_write(&kni_list_lock);
346 printk(KERN_INFO "KNI: Successfully create kni for port %d\n",
353 kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
357 struct kni_dev *dev, *n;
359 if (_IOC_SIZE(ioctl_num) > sizeof(port_id))
362 ret = copy_from_user(&port_id, (void *)ioctl_param, sizeof(port_id));
364 KNI_ERR("copy_from_user in kni_ioctl_release");
368 down_write(&kni_list_lock);
369 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
370 if (dev->port_id != port_id)
373 switch (dev->device_id) {
374 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
375 #include <rte_pci_dev_ids.h>
376 igb_kni_remove(dev->pci_dev);
378 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
379 #include <rte_pci_dev_ids.h>
380 ixgbe_kni_remove(dev->pci_dev);
385 unregister_netdev(dev->net_dev);
386 free_netdev(dev->net_dev);
387 list_del(&dev->list);
391 up_write(&kni_list_lock);
392 printk(KERN_INFO "KNI: %s release kni for port %d\n",
393 (ret == 0 ? "Successfully" : "Unsuccessfully"), port_id);
399 kni_ioctl(struct inode *inode,
400 unsigned int ioctl_num,
401 unsigned long ioctl_param)
405 KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
408 * Switch according to the ioctl called
410 switch (_IOC_NR(ioctl_num)) {
411 case _IOC_NR(RTE_KNI_IOCTL_TEST):
412 /* For test only, not used */
414 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
415 ret = kni_ioctl_create(ioctl_num, ioctl_param);
417 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
418 ret = kni_ioctl_release(ioctl_num, ioctl_param);
421 KNI_DBG("IOCTL default \n");
429 kni_compat_ioctl(struct inode *inode,
430 unsigned int ioctl_num,
431 unsigned long ioctl_param)
433 /* 32 bits app on 64 bits OS to be supported later */
434 KNI_PRINT("Not implemented.\n");
439 module_init(kni_init);
440 module_exit(kni_exit);
442 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
443 MODULE_PARM_DESC(lo_mode,
444 "KNI loopback mode (default=lo_mode_none):\n"
445 " lo_mode_none Kernel loopback disabled\n"
446 " lo_mode_fifo Enable kernel loopback with fifo\n"
447 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"