dca1ba56a168c5b190f5023dddbcdf3b56e03cee
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
1 /*-
2  * GPL LICENSE SUMMARY
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  * 
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.
9  * 
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.
14  * 
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.
20  * 
21  *   Contact Information:
22  *   Intel Corporation
23  * 
24  */
25
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>
32
33 #include <exec-env/rte_kni_common.h>
34 #include "kni_dev.h"
35 #include <rte_config.h>
36
37 MODULE_LICENSE("Dual BSD/GPL");
38 MODULE_AUTHOR("Intel Corporation");
39 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
40
41 #define KNI_RX_LOOP_NUM 1000
42
43 #define KNI_MAX_DEVICES 32
44
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);
50
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);
55
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);
62
63 /* kni kernel thread for rx */
64 static int kni_thread(void *unused);
65
66 static struct file_operations kni_fops = {
67         .owner = THIS_MODULE,
68         .open = kni_open,
69         .release = kni_release,
70         .unlocked_ioctl = (void *)kni_ioctl,
71         .compat_ioctl = (void *)kni_compat_ioctl,
72 };
73
74 static struct miscdevice kni_misc = {
75         .minor = MISC_DYNAMIC_MINOR,
76         .name = KNI_DEVICE,
77         .fops = &kni_fops,
78 };
79
80 /* loopback mode */
81 static char *lo_mode = NULL;
82
83 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
84
85 static volatile unsigned long device_in_use; /* device in use flag */
86 static struct task_struct *kni_kthread;
87
88 /* kni list lock */
89 static DECLARE_RWSEM(kni_list_lock);
90
91 /* kni list */
92 static struct list_head kni_list_head = LIST_HEAD_INIT(kni_list_head);
93
94 static int __init
95 kni_init(void)
96 {
97         KNI_PRINT("######## DPDK kni module loading ########\n");
98
99         if (misc_register(&kni_misc) != 0) {
100                 KNI_ERR("Misc registration failed\n");
101                 return 1;
102         }
103
104         /* Clear the bit of device in use */
105         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
106
107         /* Configure the lo mode according to the input parameter */
108         kni_net_config_lo_mode(lo_mode);
109
110         KNI_PRINT("######## DPDK kni module loaded  ########\n");
111
112         return 0;
113 }
114
115 static void __exit
116 kni_exit(void)
117 {
118         misc_deregister(&kni_misc);
119         KNI_PRINT("####### DPDK kni module unloaded  #######\n");
120 }
121
122 static int
123 kni_open(struct inode *inode, struct file *file)
124 {
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))
127                 return -EBUSY;
128
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);
134         }
135
136         KNI_PRINT("/dev/kni opened\n");
137
138         return 0;
139 }
140
141 static int
142 kni_release(struct inode *inode, struct file *file)
143 {
144         struct kni_dev *dev, *n;
145
146         KNI_PRINT("Stopping KNI thread...");
147
148         /* Stop kernel thread */
149         kthread_stop(kni_kthread);
150         kni_kthread = NULL;
151
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);
159                         break;
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);
163                         break;
164                 default:
165                         break;
166                 }
167                 unregister_netdev(dev->net_dev);
168                 free_netdev(dev->net_dev);
169                 list_del(&dev->list);
170         }
171         up_write(&kni_list_lock);
172
173         /* Clear the bit of device in use */
174         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
175
176         KNI_PRINT("/dev/kni closed\n");
177
178         return 0;
179 }
180
181 static int
182 kni_thread(void *unused)
183 {
184         int j;
185         struct kni_dev *dev, *n;
186
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) {
193                                 kni_net_rx(dev);
194                                 kni_net_poll_resp(dev);
195                         }
196                 }
197                 up_read(&kni_list_lock);
198                 /* reschedule out for a while */
199                 schedule_timeout_interruptible(usecs_to_jiffies( \
200                                 KNI_KTHREAD_RESCHEDULE_INTERVAL));
201         }
202         KNI_PRINT("Kernel thread for KNI stopped\n");
203
204         return 0;
205 }
206
207 static int
208 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
209 {
210         int ret;
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;
217
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))
221                 return -EINVAL;
222
223         /* Copy kni info from user space */
224         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
225         if (ret) {
226                 KNI_ERR("copy_from_user in kni_ioctl_create");
227                 return -EIO;
228         }
229
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",
236                                                 dev_info.port_id);
237                         return -EINVAL;
238                 }
239         }
240         up_read(&kni_list_lock);
241
242         net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
243                                                         kni_net_init);
244         if (net_dev == NULL) {
245                 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
246                 return -EBUSY;
247         }
248
249         kni = netdev_priv(net_dev);
250
251         kni->net_dev = net_dev;
252         kni->port_id = dev_info.port_id;
253
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);
259
260         kni->req_q = phys_to_virt(dev_info.req_phys);
261         kni->resp_q = phys_to_virt(dev_info.resp_phys);
262
263         kni->sync_va = dev_info.sync_va;
264         kni->sync_kva = phys_to_virt(dev_info.sync_phys);
265
266         kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
267         kni->mbuf_va = dev_info.mbuf_va;
268
269         kni->mbuf_size = dev_info.mbuf_size;
270
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);
287
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);
290
291         pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
292
293         /* Support Ethtool */
294         while (pci) {
295                 KNI_PRINT("pci_bus: %02x:%02x:%02x \n", pci->bus->number,
296                                 PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
297
298                 if ((pci->bus->number == dev_info.bus) &&
299                         (PCI_SLOT(pci->devfn) == dev_info.devid) &&
300                         (PCI_FUNC(pci->devfn) == dev_info.function)) {
301                         found_pci = pci;
302
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);
307                                 break;
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);
311                                 break;
312                         default:
313                                 ret = -1;
314                                 break;
315                         }
316
317                         KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n", pci, lad_dev);
318                         if (ret == 0) {
319                                 kni->lad_dev = lad_dev;
320                                 kni_set_ethtool_ops(kni->net_dev);
321                         }
322                         else {
323                                 KNI_ERR("Device not supported by ethtool");
324                                 kni->lad_dev = NULL;
325                         }
326
327                         kni->pci_dev = found_pci;
328                         kni->device_id = dev_info.device_id;
329                         break;
330                 }
331                 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id,
332                                                                         pci);
333         }
334
335         if (pci)
336                 pci_dev_put(pci);
337
338         ret = register_netdev(net_dev);
339         if (ret) {
340                 KNI_ERR("error %i registering device \"%s\"\n", ret,
341                                                         dev_info.name);
342                 free_netdev(net_dev);
343                 return -ENODEV;
344         }
345
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",
350                                                 dev_info.port_id);
351
352         return 0;
353 }
354
355 static int
356 kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
357 {
358         int ret = -EINVAL;
359         uint8_t port_id;
360         struct kni_dev *dev, *n;
361
362         if (_IOC_SIZE(ioctl_num) > sizeof(port_id))
363                         return -EINVAL;
364
365         ret = copy_from_user(&port_id, (void *)ioctl_param, sizeof(port_id));
366         if (ret) {
367                 KNI_ERR("copy_from_user in kni_ioctl_release");
368                 return -EIO;
369         }
370
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)
374                         continue;
375
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);
380                         break;
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);
384                         break;
385                 default:
386                         break;
387                 }
388                 unregister_netdev(dev->net_dev);
389                 free_netdev(dev->net_dev);
390                 list_del(&dev->list);
391                 ret = 0;
392                 break;
393         }
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);
397
398         return ret;
399 }
400
401 static int
402 kni_ioctl(struct inode *inode,
403         unsigned int ioctl_num,
404         unsigned long ioctl_param)
405 {
406         int ret = -EINVAL;
407
408         KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
409
410         /*
411          * Switch according to the ioctl called
412          */
413         switch (_IOC_NR(ioctl_num)) {
414         case _IOC_NR(RTE_KNI_IOCTL_TEST):
415                 /* For test only, not used */
416                 break;
417         case _IOC_NR(RTE_KNI_IOCTL_CREATE):
418                 ret = kni_ioctl_create(ioctl_num, ioctl_param);
419                 break;
420         case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
421                 ret = kni_ioctl_release(ioctl_num, ioctl_param);
422                 break;
423         default:
424                 KNI_DBG("IOCTL default \n");
425                 break;
426         }
427
428         return ret;
429 }
430
431 static int
432 kni_compat_ioctl(struct inode *inode,
433                 unsigned int ioctl_num,
434                 unsigned long ioctl_param)
435 {
436         /* 32 bits app on 64 bits OS to be supported later */
437         KNI_PRINT("Not implemented.\n");
438
439         return -EINVAL;
440 }
441
442 module_init(kni_init);
443 module_exit(kni_exit);
444
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"
451 "\n"
452 );
453