kni: identify device by name
[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 #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>
31
32 #include <exec-env/rte_kni_common.h>
33 #include "kni_dev.h"
34 #include <rte_config.h>
35
36 MODULE_LICENSE("Dual BSD/GPL");
37 MODULE_AUTHOR("Intel Corporation");
38 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
39
40 #define KNI_RX_LOOP_NUM 1000
41
42 #define KNI_MAX_DEVICES 32
43
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);
49
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);
54
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);
61
62 /* kni kernel thread for rx */
63 static int kni_thread(void *unused);
64
65 static struct file_operations kni_fops = {
66         .owner = THIS_MODULE,
67         .open = kni_open,
68         .release = kni_release,
69         .unlocked_ioctl = (void *)kni_ioctl,
70         .compat_ioctl = (void *)kni_compat_ioctl,
71 };
72
73 static struct miscdevice kni_misc = {
74         .minor = MISC_DYNAMIC_MINOR,
75         .name = KNI_DEVICE,
76         .fops = &kni_fops,
77 };
78
79 /* loopback mode */
80 static char *lo_mode = NULL;
81
82 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
83
84 static volatile unsigned long device_in_use; /* device in use flag */
85 static struct task_struct *kni_kthread;
86
87 /* kni list lock */
88 static DECLARE_RWSEM(kni_list_lock);
89
90 /* kni list */
91 static struct list_head kni_list_head = LIST_HEAD_INIT(kni_list_head);
92
93 static int __init
94 kni_init(void)
95 {
96         KNI_PRINT("######## DPDK kni module loading ########\n");
97
98         if (misc_register(&kni_misc) != 0) {
99                 KNI_ERR("Misc registration failed\n");
100                 return 1;
101         }
102
103         /* Clear the bit of device in use */
104         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
105
106         /* Configure the lo mode according to the input parameter */
107         kni_net_config_lo_mode(lo_mode);
108
109         KNI_PRINT("######## DPDK kni module loaded  ########\n");
110
111         return 0;
112 }
113
114 static void __exit
115 kni_exit(void)
116 {
117         misc_deregister(&kni_misc);
118         KNI_PRINT("####### DPDK kni module unloaded  #######\n");
119 }
120
121 static int
122 kni_open(struct inode *inode, struct file *file)
123 {
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))
126                 return -EBUSY;
127
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);
133         }
134
135         KNI_PRINT("/dev/kni opened\n");
136
137         return 0;
138 }
139
140 static int
141 kni_release(struct inode *inode, struct file *file)
142 {
143         struct kni_dev *dev, *n;
144
145         /* Stop kernel thread */
146         kthread_stop(kni_kthread);
147         kni_kthread = NULL;
148
149         down_write(&kni_list_lock);
150         list_for_each_entry_safe(dev, n, &kni_list_head, list) {
151                 /* Call the remove part to restore pci dev */
152                 switch (dev->device_id) {
153                 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
154                 #include <rte_pci_dev_ids.h>
155                         igb_kni_remove(dev->pci_dev);
156                         break;
157                 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
158                 #include <rte_pci_dev_ids.h>
159                         ixgbe_kni_remove(dev->pci_dev);
160                         break;
161                 default:
162                         break;
163                 }
164                 unregister_netdev(dev->net_dev);
165                 free_netdev(dev->net_dev);
166                 list_del(&dev->list);
167         }
168         up_write(&kni_list_lock);
169
170         /* Clear the bit of device in use */
171         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
172
173         KNI_PRINT("/dev/kni closed\n");
174
175         return 0;
176 }
177
178 static int
179 kni_thread(void *unused)
180 {
181         int j;
182         struct kni_dev *dev, *n;
183
184         while (!kthread_should_stop()) {
185                 down_read(&kni_list_lock);
186                 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
187                         list_for_each_entry_safe(dev, n,
188                                         &kni_list_head, list) {
189                                 kni_net_rx(dev);
190                                 kni_net_poll_resp(dev);
191                         }
192                 }
193                 up_read(&kni_list_lock);
194                 /* reschedule out for a while */
195                 schedule_timeout_interruptible(usecs_to_jiffies( \
196                                 KNI_KTHREAD_RESCHEDULE_INTERVAL));
197         }
198
199         return 0;
200 }
201
202 static int
203 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
204 {
205         if (!kni || !dev)
206                 return -1;
207
208         /* Check if network name has been used */
209         if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
210                 KNI_ERR("KNI name %s duplicated\n", dev->name);
211                 return -1;
212         }
213
214         return 0;
215 }
216
217 static int
218 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
219 {
220         int ret;
221         struct rte_kni_device_info dev_info;
222         struct pci_dev *pci = NULL;
223         struct pci_dev *found_pci = NULL;
224         struct net_device *net_dev = NULL;
225         struct net_device *lad_dev = NULL;
226         struct kni_dev *kni, *dev, *n;
227
228         printk(KERN_INFO "KNI: Creating kni...\n");
229         /* Check the buffer size, to avoid warning */
230         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
231                 return -EINVAL;
232
233         /* Copy kni info from user space */
234         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
235         if (ret) {
236                 KNI_ERR("copy_from_user in kni_ioctl_create");
237                 return -EIO;
238         }
239
240         /* Check if it has been created */
241         down_read(&kni_list_lock);
242         list_for_each_entry_safe(dev, n, &kni_list_head, list) {
243                 if (kni_check_param(dev, &dev_info) < 0) {
244                         up_read(&kni_list_lock);
245                         return -EINVAL;
246                 }
247         }
248         up_read(&kni_list_lock);
249
250         net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
251                                                         kni_net_init);
252         if (net_dev == NULL) {
253                 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
254                 return -EBUSY;
255         }
256
257         kni = netdev_priv(net_dev);
258
259         kni->net_dev = net_dev;
260         kni->group_id = dev_info.group_id;
261         strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
262
263         /* Translate user space info into kernel space info */
264         kni->tx_q = phys_to_virt(dev_info.tx_phys);
265         kni->rx_q = phys_to_virt(dev_info.rx_phys);
266         kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
267         kni->free_q = phys_to_virt(dev_info.free_phys);
268
269         kni->req_q = phys_to_virt(dev_info.req_phys);
270         kni->resp_q = phys_to_virt(dev_info.resp_phys);
271         kni->sync_va = dev_info.sync_va;
272         kni->sync_kva = phys_to_virt(dev_info.sync_phys);
273
274         kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
275         kni->mbuf_va = dev_info.mbuf_va;
276
277         kni->mbuf_size = dev_info.mbuf_size;
278
279         KNI_PRINT("tx_phys:      0x%016llx, tx_q addr:      0x%p\n",
280                 (unsigned long long) dev_info.tx_phys, kni->tx_q);
281         KNI_PRINT("rx_phys:      0x%016llx, rx_q addr:      0x%p\n",
282                 (unsigned long long) dev_info.rx_phys, kni->rx_q);
283         KNI_PRINT("alloc_phys:   0x%016llx, alloc_q addr:   0x%p\n",
284                 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
285         KNI_PRINT("free_phys:    0x%016llx, free_q addr:    0x%p\n",
286                 (unsigned long long) dev_info.free_phys, kni->free_q);
287         KNI_PRINT("req_phys:     0x%016llx, req_q addr:     0x%p\n",
288                 (unsigned long long) dev_info.req_phys, kni->req_q);
289         KNI_PRINT("resp_phys:    0x%016llx, resp_q addr:    0x%p\n",
290                 (unsigned long long) dev_info.resp_phys, kni->resp_q);
291         KNI_PRINT("mbuf_phys:    0x%016llx, mbuf_kva:       0x%p\n",
292                 (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
293         KNI_PRINT("mbuf_va:      0x%p\n", dev_info.mbuf_va);
294         KNI_PRINT("mbuf_size:    %u\n", kni->mbuf_size);
295
296         KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n",
297                                         dev_info.bus,
298                                         dev_info.devid,
299                                         dev_info.function,
300                                         dev_info.vendor_id,
301                                         dev_info.device_id);
302
303         pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
304
305         /* Support Ethtool */
306         while (pci) {
307                 KNI_PRINT("pci_bus: %02x:%02x:%02x \n",
308                                         pci->bus->number,
309                                         PCI_SLOT(pci->devfn),
310                                         PCI_FUNC(pci->devfn));
311
312                 if ((pci->bus->number == dev_info.bus) &&
313                         (PCI_SLOT(pci->devfn) == dev_info.devid) &&
314                         (PCI_FUNC(pci->devfn) == dev_info.function)) {
315                         found_pci = pci;
316                         switch (dev_info.device_id) {
317                         #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
318                         #include <rte_pci_dev_ids.h>
319                                 ret = igb_kni_probe(found_pci, &lad_dev);
320                                 break;
321                         #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
322                                                         case (dev):
323                         #include <rte_pci_dev_ids.h>
324                                 ret = ixgbe_kni_probe(found_pci, &lad_dev);
325                                 break;
326                         default:
327                                 ret = -1;
328                                 break;
329                         }
330
331                         KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
332                                                         pci, lad_dev);
333                         if (ret == 0) {
334                                 kni->lad_dev = lad_dev;
335                                 kni_set_ethtool_ops(kni->net_dev);
336                         } else {
337                                 KNI_ERR("Device not supported by ethtool");
338                                 kni->lad_dev = NULL;
339                         }
340
341                         kni->pci_dev = found_pci;
342                         kni->device_id = dev_info.device_id;
343                         break;
344                 }
345                 pci = pci_get_device(dev_info.vendor_id,
346                                 dev_info.device_id, pci);
347         }
348         if (pci)
349                 pci_dev_put(pci);
350
351         ret = register_netdev(net_dev);
352         if (ret) {
353                 KNI_ERR("error %i registering device \"%s\"\n",
354                                         ret, dev_info.name);
355                 free_netdev(net_dev);
356                 return -ENODEV;
357         }
358
359         down_write(&kni_list_lock);
360         list_add(&kni->list, &kni_list_head);
361         up_write(&kni_list_lock);
362
363         return 0;
364 }
365
366 static int
367 kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
368 {
369         int ret = -EINVAL;
370         struct kni_dev *dev, *n;
371         struct rte_kni_device_info dev_info;
372
373         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
374                         return -EINVAL;
375
376         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
377         if (ret) {
378                 KNI_ERR("copy_from_user in kni_ioctl_release");
379                 return -EIO;
380         }
381
382         /* Release the network device according to its name */
383         if (strlen(dev_info.name) == 0)
384                 return ret;
385
386         down_write(&kni_list_lock);
387         list_for_each_entry_safe(dev, n, &kni_list_head, list) {
388                 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
389                         continue;
390
391                 switch (dev->device_id) {
392                 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
393                 #include <rte_pci_dev_ids.h>
394                         igb_kni_remove(dev->pci_dev);
395                         break;
396                 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
397                 #include <rte_pci_dev_ids.h>
398                         ixgbe_kni_remove(dev->pci_dev);
399                         break;
400                 default:
401                         break;
402                 }
403                 unregister_netdev(dev->net_dev);
404                 free_netdev(dev->net_dev);
405                 list_del(&dev->list);
406                 ret = 0;
407                 break;
408         }
409         up_write(&kni_list_lock);
410         printk(KERN_INFO "KNI: %s release kni named %s\n",
411                 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
412
413         return ret;
414 }
415
416 static int
417 kni_ioctl(struct inode *inode,
418         unsigned int ioctl_num,
419         unsigned long ioctl_param)
420 {
421         int ret = -EINVAL;
422
423         KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
424
425         /*
426          * Switch according to the ioctl called
427          */
428         switch (_IOC_NR(ioctl_num)) {
429         case _IOC_NR(RTE_KNI_IOCTL_TEST):
430                 /* For test only, not used */
431                 break;
432         case _IOC_NR(RTE_KNI_IOCTL_CREATE):
433                 ret = kni_ioctl_create(ioctl_num, ioctl_param);
434                 break;
435         case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
436                 ret = kni_ioctl_release(ioctl_num, ioctl_param);
437                 break;
438         default:
439                 KNI_DBG("IOCTL default \n");
440                 break;
441         }
442
443         return ret;
444 }
445
446 static int
447 kni_compat_ioctl(struct inode *inode,
448                 unsigned int ioctl_num,
449                 unsigned long ioctl_param)
450 {
451         /* 32 bits app on 64 bits OS to be supported later */
452         KNI_PRINT("Not implemented.\n");
453
454         return -EINVAL;
455 }
456
457 module_init(kni_init);
458 module_exit(kni_exit);
459
460 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
461 MODULE_PARM_DESC(lo_mode,
462 "KNI loopback mode (default=lo_mode_none):\n"
463 "    lo_mode_none        Kernel loopback disabled\n"
464 "    lo_mode_fifo        Enable kernel loopback with fifo\n"
465 "    lo_mode_fifo_skb    Enable kernel loopback with fifo and skb buffer\n"
466 "\n"
467 );
468