kni: initial import
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
1 /*-
2  * GPL LICENSE SUMMARY
3  * 
4  *   Copyright(c) 2010-2012 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
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 /* 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>
86         { 0, },
87 };
88
89 MODULE_DEVICE_TABLE(pci, kni_pci_ids);
90
91 /* loopback mode */
92 static char *lo_mode = NULL;
93
94 static struct kni_dev *kni_devs[KNI_MAX_DEVICES];
95 static volatile int num_devs;  /* number of kni devices */
96
97 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
98
99 static volatile unsigned long device_in_use; /* device in use flag */
100 static struct task_struct *kni_kthread;
101
102 static int __init
103 kni_init(void)
104 {
105         KNI_PRINT("######## DPDK kni module loading ########\n");
106
107         if (misc_register(&kni_misc) != 0) {
108                 KNI_ERR("Misc registration failed\n");
109                 return 1;
110         }
111
112         /* Clear the bit of device in use */
113         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
114
115         /* Configure the lo mode according to the input parameter */
116         kni_net_config_lo_mode(lo_mode);
117
118         KNI_PRINT("######## DPDK kni module loaded  ########\n");
119
120         return 0;
121 }
122
123 static void __exit
124 kni_exit(void)
125 {
126         misc_deregister(&kni_misc);
127         KNI_PRINT("####### DPDK kni module unloaded  #######\n");
128 }
129
130 static int
131 kni_open(struct inode *inode, struct file *file)
132 {
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))
135                 return -EBUSY;
136
137         memset(kni_devs, 0, sizeof(kni_devs));
138         num_devs = 0;
139
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);
145         }
146
147         KNI_PRINT("/dev/kni opened\n");
148
149         return 0;
150 }
151
152 static int
153 kni_release(struct inode *inode, struct file *file)
154 {
155         int i;
156
157         KNI_PRINT("Stopping KNI thread...");
158
159         /* Stop kernel thread */
160         kthread_stop(kni_kthread);
161         kni_kthread = NULL;
162
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);
170                                 break;
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);
174                                 break;
175                         default:
176                                 break;
177                         }
178
179                         unregister_netdev(kni_devs[i]->net_dev);
180                         free_netdev(kni_devs[i]->net_dev);
181                         kni_devs[i] = NULL;
182                 }
183         }
184         num_devs = 0;
185
186         /* Clear the bit of device in use */
187         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
188
189         KNI_PRINT("/dev/kni closed\n");
190
191         return 0;
192 }
193
194 static int
195 kni_thread(void *unused)
196 {
197         int i, j;
198
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 */
205                                 if (kni_devs[i]) {
206                                         kni_net_rx(kni_devs[i]);
207                                         kni_net_poll_resp(kni_devs[i]);
208                                 }
209                                 else
210                                         KNI_ERR("kni_thread -no kni found!!!");
211                         }
212                 }
213                 /* reschedule out for a while */
214                 schedule_timeout_interruptible(usecs_to_jiffies( \
215                                 KNI_KTHREAD_RESCHEDULE_INTERVAL));
216         }
217         KNI_PRINT("Kernel thread for KNI stopped\n");
218
219         return 0;
220 }
221
222 static int
223 kni_check_pci_device_id(uint16_t vendor_id, uint16_t device_id)
224 {
225         int i, total = sizeof(kni_pci_ids)/sizeof(struct pci_device_id);
226         struct pci_device_id *p;
227
228         /* Check if the vendor id/device id are supported */
229         for (i = 0; i < total; i++) {
230                 p = &kni_pci_ids[i];
231                 if (p->vendor != vendor_id && p->vendor != PCI_ANY_ID)
232                         continue;
233                 if (p->device != device_id && p->device != PCI_ANY_ID)
234                         continue;
235                 return 0;
236         }
237
238         return -1;
239 }
240
241 static int
242 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
243 {
244         int ret;
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;
250         struct kni_dev *kni;
251
252         if (num_devs == KNI_MAX_DEVICES)
253                 return -EBUSY;
254
255         /* Check the buffer size, to avoid warning */
256         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
257                 return -EINVAL;
258
259         /* Copy kni info from user space */
260         ret = copy_from_user(&dev_info, (void *)ioctl_param,
261                                         _IOC_SIZE(ioctl_num));
262         if (ret) {
263                 KNI_ERR("copy_from_user");
264                 return -EIO;
265         }
266
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);
269         if (ret < 0) {
270                 KNI_ERR("Invalid vendor_id: %x or device_id: %x\n",
271                                 dev_info.vendor_id, dev_info.device_id);
272                 return -EINVAL;
273         }
274
275         net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
276                                                         kni_net_init);
277         if (net_dev == NULL) {
278                 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
279                 return -EBUSY;
280         }
281
282         kni = netdev_priv(net_dev);
283
284         kni->net_dev = net_dev;
285         kni->idx = num_devs;
286
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);
292
293         kni->req_q = phys_to_virt(dev_info.req_phys);
294         kni->resp_q = phys_to_virt(dev_info.resp_phys);
295
296         kni->sync_va = dev_info.sync_va;
297         kni->sync_kva = phys_to_virt(dev_info.sync_phys);
298
299         kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
300         kni->mbuf_va = dev_info.mbuf_va;
301
302         kni->mbuf_size = dev_info.mbuf_size;
303
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);
320
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);
323
324         pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
325
326         /* Support Ethtool */
327         while (pci) {
328                 KNI_PRINT("pci_bus: %02x:%02x:%02x \n", pci->bus->number,
329                                 PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
330
331                 if ((pci->bus->number == dev_info.bus) &&
332                         (PCI_SLOT(pci->devfn) == dev_info.devid) &&
333                         (PCI_FUNC(pci->devfn) == dev_info.function)) {
334                         found_pci = pci;
335
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);
340                                 break;
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);
344                                 break;
345                         default:
346                                 ret = -1;
347                                 break;
348                         }
349
350                         KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n", pci, lad_dev);
351                         if (ret == 0) {
352                                 kni->lad_dev = lad_dev;
353                                 kni_set_ethtool_ops(kni->net_dev);
354                         }
355                         else {
356                                 KNI_ERR("Device not supported by ethtool");
357                                 kni->lad_dev = NULL;
358                         }
359
360                         kni->pci_dev = found_pci;
361                         kni->device_id = dev_info.device_id;
362                         break;
363                 }
364                 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id,
365                                                                         pci);
366         }
367
368         if (pci)
369                 pci_dev_put(pci);
370
371         ret = register_netdev(net_dev);
372         if (ret) {
373                 KNI_ERR("error %i registering device \"%s\"\n", ret,
374                                                         dev_info.name);
375                 free_netdev(net_dev);
376                 return -ENODEV;
377         }
378
379         kni_devs[num_devs++] = kni;
380
381         return 0;
382 }
383
384 static int
385 kni_ioctl(struct inode *inode,
386         unsigned int ioctl_num,
387         unsigned long ioctl_param)
388 {
389         int ret = -EINVAL;
390
391         KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
392
393         /*
394          * Switch according to the ioctl called
395          */
396         switch (_IOC_NR(ioctl_num)) {
397         case _IOC_NR(RTE_KNI_IOCTL_TEST):
398                 /* For test only, not used */
399                 break;
400         case _IOC_NR(RTE_KNI_IOCTL_CREATE):
401                 ret = kni_ioctl_create(ioctl_num, ioctl_param);
402                 break;
403         default:
404                 KNI_DBG("IOCTL default \n");
405                 break;
406         }
407
408         return ret;
409 }
410
411 static int
412 kni_compat_ioctl(struct inode *inode,
413                 unsigned int ioctl_num,
414                 unsigned long ioctl_param)
415 {
416         /* 32 bits app on 64 bits OS to be supported later */
417         KNI_PRINT("Not implemented.\n");
418
419         return -EINVAL;
420 }
421
422 module_init(kni_init);
423 module_exit(kni_exit);
424
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"
431 "\n"
432 );
433