eal/linux: add interrupt type for vdev
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_interrupts.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <sys/queue.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <inttypes.h>
44 #include <sys/epoll.h>
45 #include <sys/signalfd.h>
46 #include <sys/ioctl.h>
47 #include <sys/eventfd.h>
48 #include <assert.h>
49 #include <stdbool.h>
50
51 #include <rte_common.h>
52 #include <rte_interrupts.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_launch.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_debug.h>
62 #include <rte_log.h>
63 #include <rte_pci.h>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67
68 #include "eal_private.h"
69 #include "eal_vfio.h"
70 #include "eal_thread.h"
71
72 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
73 #define NB_OTHER_INTR               1
74
75 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
76
77 /**
78  * union for pipe fds.
79  */
80 union intr_pipefds{
81         struct {
82                 int pipefd[2];
83         };
84         struct {
85                 int readfd;
86                 int writefd;
87         };
88 };
89
90 /**
91  * union buffer for reading on different devices
92  */
93 union rte_intr_read_buffer {
94         int uio_intr_count;              /* for uio device */
95 #ifdef VFIO_PRESENT
96         uint64_t vfio_intr_count;        /* for vfio device */
97 #endif
98         uint64_t timerfd_num;            /* for timerfd */
99         char charbuf[16];                /* for others */
100 };
101
102 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
103 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
104
105 struct rte_intr_callback {
106         TAILQ_ENTRY(rte_intr_callback) next;
107         rte_intr_callback_fn cb_fn;  /**< callback address */
108         void *cb_arg;                /**< parameter for callback */
109 };
110
111 struct rte_intr_source {
112         TAILQ_ENTRY(rte_intr_source) next;
113         struct rte_intr_handle intr_handle; /**< interrupt handle */
114         struct rte_intr_cb_list callbacks;  /**< user callbacks */
115         uint32_t active;
116 };
117
118 /* global spinlock for interrupt data operation */
119 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
120
121 /* union buffer for pipe read/write */
122 static union intr_pipefds intr_pipe;
123
124 /* interrupt sources list */
125 static struct rte_intr_source_list intr_sources;
126
127 /* interrupt handling thread */
128 static pthread_t intr_thread;
129
130 /* VFIO interrupts */
131 #ifdef VFIO_PRESENT
132
133 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
134 /* irq set buffer length for queue interrupts and LSC interrupt */
135 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
136                               sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
137
138 /* enable legacy (INTx) interrupts */
139 static int
140 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
141         struct vfio_irq_set *irq_set;
142         char irq_set_buf[IRQ_SET_BUF_LEN];
143         int len, ret;
144         int *fd_ptr;
145
146         len = sizeof(irq_set_buf);
147
148         /* enable INTx */
149         irq_set = (struct vfio_irq_set *) irq_set_buf;
150         irq_set->argsz = len;
151         irq_set->count = 1;
152         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
153         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
154         irq_set->start = 0;
155         fd_ptr = (int *) &irq_set->data;
156         *fd_ptr = intr_handle->fd;
157
158         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
159
160         if (ret) {
161                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
162                                                 intr_handle->fd);
163                 return -1;
164         }
165
166         /* unmask INTx after enabling */
167         memset(irq_set, 0, len);
168         len = sizeof(struct vfio_irq_set);
169         irq_set->argsz = len;
170         irq_set->count = 1;
171         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
172         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
173         irq_set->start = 0;
174
175         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
176
177         if (ret) {
178                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
179                                                 intr_handle->fd);
180                 return -1;
181         }
182         return 0;
183 }
184
185 /* disable legacy (INTx) interrupts */
186 static int
187 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
188         struct vfio_irq_set *irq_set;
189         char irq_set_buf[IRQ_SET_BUF_LEN];
190         int len, ret;
191
192         len = sizeof(struct vfio_irq_set);
193
194         /* mask interrupts before disabling */
195         irq_set = (struct vfio_irq_set *) irq_set_buf;
196         irq_set->argsz = len;
197         irq_set->count = 1;
198         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
199         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
200         irq_set->start = 0;
201
202         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
203
204         if (ret) {
205                 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
206                                                 intr_handle->fd);
207                 return -1;
208         }
209
210         /* disable INTx*/
211         memset(irq_set, 0, len);
212         irq_set->argsz = len;
213         irq_set->count = 0;
214         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
215         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
216         irq_set->start = 0;
217
218         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
219
220         if (ret) {
221                 RTE_LOG(ERR, EAL,
222                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
223                 return -1;
224         }
225         return 0;
226 }
227
228 /* enable MSI interrupts */
229 static int
230 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
231         int len, ret;
232         char irq_set_buf[IRQ_SET_BUF_LEN];
233         struct vfio_irq_set *irq_set;
234         int *fd_ptr;
235
236         len = sizeof(irq_set_buf);
237
238         irq_set = (struct vfio_irq_set *) irq_set_buf;
239         irq_set->argsz = len;
240         irq_set->count = 1;
241         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
242         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
243         irq_set->start = 0;
244         fd_ptr = (int *) &irq_set->data;
245         *fd_ptr = intr_handle->fd;
246
247         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
248
249         if (ret) {
250                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
251                                                 intr_handle->fd);
252                 return -1;
253         }
254         return 0;
255 }
256
257 /* disable MSI interrupts */
258 static int
259 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
260         struct vfio_irq_set *irq_set;
261         char irq_set_buf[IRQ_SET_BUF_LEN];
262         int len, ret;
263
264         len = sizeof(struct vfio_irq_set);
265
266         irq_set = (struct vfio_irq_set *) irq_set_buf;
267         irq_set->argsz = len;
268         irq_set->count = 0;
269         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
270         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
271         irq_set->start = 0;
272
273         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
274
275         if (ret)
276                 RTE_LOG(ERR, EAL,
277                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
278
279         return ret;
280 }
281
282 static int
283 get_max_intr(const struct rte_intr_handle *intr_handle)
284 {
285         struct rte_intr_source *src;
286
287         TAILQ_FOREACH(src, &intr_sources, next) {
288                 if (src->intr_handle.fd != intr_handle->fd)
289                         continue;
290
291                 if (src->intr_handle.max_intr < intr_handle->max_intr)
292                         src->intr_handle.max_intr = intr_handle->max_intr;
293                 if (!src->intr_handle.max_intr)
294                         src->intr_handle.max_intr = 1;
295                 else if (src->intr_handle.max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
296                         src->intr_handle.max_intr
297                                 = RTE_MAX_RXTX_INTR_VEC_ID + 1;
298
299                 return src->intr_handle.max_intr;
300         }
301
302         return -1;
303 }
304
305 /* enable MSI-X interrupts */
306 static int
307 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
308         int len, ret;
309         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
310         struct vfio_irq_set *irq_set;
311         int *fd_ptr;
312
313         len = sizeof(irq_set_buf);
314
315         irq_set = (struct vfio_irq_set *) irq_set_buf;
316         irq_set->argsz = len;
317
318         ret = get_max_intr(intr_handle);
319         if (ret < 0) {
320                 RTE_LOG(ERR, EAL, "Invalid number of MSI-X irqs for fd %d\n",
321                         intr_handle->fd);
322                 return -1;
323         }
324
325         irq_set->count = ret;
326         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
327         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
328         irq_set->start = 0;
329         fd_ptr = (int *) &irq_set->data;
330         /* INTR vector offset 0 reserve for non-efds mapping */
331         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
332         memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
333                 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
334
335         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
336
337         if (ret) {
338                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
339                                                 intr_handle->fd);
340                 return -1;
341         }
342
343         return 0;
344 }
345
346 /* disable MSI-X interrupts */
347 static int
348 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
349         struct vfio_irq_set *irq_set;
350         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
351         int len, ret;
352
353         len = sizeof(struct vfio_irq_set);
354
355         irq_set = (struct vfio_irq_set *) irq_set_buf;
356         irq_set->argsz = len;
357         irq_set->count = 0;
358         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
359         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
360         irq_set->start = 0;
361
362         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
363
364         if (ret)
365                 RTE_LOG(ERR, EAL,
366                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
367
368         return ret;
369 }
370 #endif
371
372 static int
373 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
374 {
375         unsigned char command_high;
376
377         /* use UIO config file descriptor for uio_pci_generic */
378         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
379                 RTE_LOG(ERR, EAL,
380                         "Error reading interrupts status for fd %d\n",
381                         intr_handle->uio_cfg_fd);
382                 return -1;
383         }
384         /* disable interrupts */
385         command_high |= 0x4;
386         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
387                 RTE_LOG(ERR, EAL,
388                         "Error disabling interrupts for fd %d\n",
389                         intr_handle->uio_cfg_fd);
390                 return -1;
391         }
392
393         return 0;
394 }
395
396 static int
397 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
398 {
399         unsigned char command_high;
400
401         /* use UIO config file descriptor for uio_pci_generic */
402         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
403                 RTE_LOG(ERR, EAL,
404                         "Error reading interrupts status for fd %d\n",
405                         intr_handle->uio_cfg_fd);
406                 return -1;
407         }
408         /* enable interrupts */
409         command_high &= ~0x4;
410         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
411                 RTE_LOG(ERR, EAL,
412                         "Error enabling interrupts for fd %d\n",
413                         intr_handle->uio_cfg_fd);
414                 return -1;
415         }
416
417         return 0;
418 }
419
420 static int
421 uio_intr_disable(const struct rte_intr_handle *intr_handle)
422 {
423         const int value = 0;
424
425         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
426                 RTE_LOG(ERR, EAL,
427                         "Error disabling interrupts for fd %d (%s)\n",
428                         intr_handle->fd, strerror(errno));
429                 return -1;
430         }
431         return 0;
432 }
433
434 static int
435 uio_intr_enable(const struct rte_intr_handle *intr_handle)
436 {
437         const int value = 1;
438
439         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
440                 RTE_LOG(ERR, EAL,
441                         "Error enabling interrupts for fd %d (%s)\n",
442                         intr_handle->fd, strerror(errno));
443                 return -1;
444         }
445         return 0;
446 }
447
448 int
449 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
450                         rte_intr_callback_fn cb, void *cb_arg)
451 {
452         int ret, wake_thread;
453         struct rte_intr_source *src;
454         struct rte_intr_callback *callback;
455
456         wake_thread = 0;
457
458         /* first do parameter checking */
459         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
460                 RTE_LOG(ERR, EAL,
461                         "Registering with invalid input parameter\n");
462                 return -EINVAL;
463         }
464
465         /* allocate a new interrupt callback entity */
466         callback = rte_zmalloc("interrupt callback list",
467                                 sizeof(*callback), 0);
468         if (callback == NULL) {
469                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
470                 return -ENOMEM;
471         }
472         callback->cb_fn = cb;
473         callback->cb_arg = cb_arg;
474
475         rte_spinlock_lock(&intr_lock);
476
477         /* check if there is at least one callback registered for the fd */
478         TAILQ_FOREACH(src, &intr_sources, next) {
479                 if (src->intr_handle.fd == intr_handle->fd) {
480                         /* we had no interrupts for this */
481                         if TAILQ_EMPTY(&src->callbacks)
482                                 wake_thread = 1;
483
484                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
485                         ret = 0;
486                         break;
487                 }
488         }
489
490         /* no existing callbacks for this - add new source */
491         if (src == NULL) {
492                 if ((src = rte_zmalloc("interrupt source list",
493                                 sizeof(*src), 0)) == NULL) {
494                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
495                         rte_free(callback);
496                         ret = -ENOMEM;
497                 } else {
498                         src->intr_handle = *intr_handle;
499                         TAILQ_INIT(&src->callbacks);
500                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
501                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
502                         wake_thread = 1;
503                         ret = 0;
504                 }
505         }
506
507         rte_spinlock_unlock(&intr_lock);
508
509         /**
510          * check if need to notify the pipe fd waited by epoll_wait to
511          * rebuild the wait list.
512          */
513         if (wake_thread)
514                 if (write(intr_pipe.writefd, "1", 1) < 0)
515                         return -EPIPE;
516
517         return ret;
518 }
519
520 int
521 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
522                         rte_intr_callback_fn cb_fn, void *cb_arg)
523 {
524         int ret;
525         struct rte_intr_source *src;
526         struct rte_intr_callback *cb, *next;
527
528         /* do parameter checking first */
529         if (intr_handle == NULL || intr_handle->fd < 0) {
530                 RTE_LOG(ERR, EAL,
531                 "Unregistering with invalid input parameter\n");
532                 return -EINVAL;
533         }
534
535         rte_spinlock_lock(&intr_lock);
536
537         /* check if the insterrupt source for the fd is existent */
538         TAILQ_FOREACH(src, &intr_sources, next)
539                 if (src->intr_handle.fd == intr_handle->fd)
540                         break;
541
542         /* No interrupt source registered for the fd */
543         if (src == NULL) {
544                 ret = -ENOENT;
545
546         /* interrupt source has some active callbacks right now. */
547         } else if (src->active != 0) {
548                 ret = -EAGAIN;
549
550         /* ok to remove. */
551         } else {
552                 ret = 0;
553
554                 /*walk through the callbacks and remove all that match. */
555                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
556
557                         next = TAILQ_NEXT(cb, next);
558
559                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
560                                         cb->cb_arg == cb_arg)) {
561                                 TAILQ_REMOVE(&src->callbacks, cb, next);
562                                 rte_free(cb);
563                                 ret++;
564                         }
565                 }
566
567                 /* all callbacks for that source are removed. */
568                 if (TAILQ_EMPTY(&src->callbacks)) {
569                         TAILQ_REMOVE(&intr_sources, src, next);
570                         rte_free(src);
571                 }
572         }
573
574         rte_spinlock_unlock(&intr_lock);
575
576         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
577         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
578                 ret = -EPIPE;
579         }
580
581         return ret;
582 }
583
584 int
585 rte_intr_enable(const struct rte_intr_handle *intr_handle)
586 {
587         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
588                 return 0;
589
590         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
591                 return -1;
592
593         switch (intr_handle->type){
594         /* write to the uio fd to enable the interrupt */
595         case RTE_INTR_HANDLE_UIO:
596                 if (uio_intr_enable(intr_handle))
597                         return -1;
598                 break;
599         case RTE_INTR_HANDLE_UIO_INTX:
600                 if (uio_intx_intr_enable(intr_handle))
601                         return -1;
602                 break;
603         /* not used at this moment */
604         case RTE_INTR_HANDLE_ALARM:
605                 return -1;
606 #ifdef VFIO_PRESENT
607         case RTE_INTR_HANDLE_VFIO_MSIX:
608                 if (vfio_enable_msix(intr_handle))
609                         return -1;
610                 break;
611         case RTE_INTR_HANDLE_VFIO_MSI:
612                 if (vfio_enable_msi(intr_handle))
613                         return -1;
614                 break;
615         case RTE_INTR_HANDLE_VFIO_LEGACY:
616                 if (vfio_enable_intx(intr_handle))
617                         return -1;
618                 break;
619 #endif
620         /* unknown handle type */
621         default:
622                 RTE_LOG(ERR, EAL,
623                         "Unknown handle type of fd %d\n",
624                                         intr_handle->fd);
625                 return -1;
626         }
627
628         return 0;
629 }
630
631 int
632 rte_intr_disable(const struct rte_intr_handle *intr_handle)
633 {
634         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
635                 return 0;
636
637         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
638                 return -1;
639
640         switch (intr_handle->type){
641         /* write to the uio fd to disable the interrupt */
642         case RTE_INTR_HANDLE_UIO:
643                 if (uio_intr_disable(intr_handle))
644                         return -1;
645                 break;
646         case RTE_INTR_HANDLE_UIO_INTX:
647                 if (uio_intx_intr_disable(intr_handle))
648                         return -1;
649                 break;
650         /* not used at this moment */
651         case RTE_INTR_HANDLE_ALARM:
652                 return -1;
653 #ifdef VFIO_PRESENT
654         case RTE_INTR_HANDLE_VFIO_MSIX:
655                 if (vfio_disable_msix(intr_handle))
656                         return -1;
657                 break;
658         case RTE_INTR_HANDLE_VFIO_MSI:
659                 if (vfio_disable_msi(intr_handle))
660                         return -1;
661                 break;
662         case RTE_INTR_HANDLE_VFIO_LEGACY:
663                 if (vfio_disable_intx(intr_handle))
664                         return -1;
665                 break;
666 #endif
667         /* unknown handle type */
668         default:
669                 RTE_LOG(ERR, EAL,
670                         "Unknown handle type of fd %d\n",
671                                         intr_handle->fd);
672                 return -1;
673         }
674
675         return 0;
676 }
677
678 static int
679 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
680 {
681         bool call = false;
682         int n, bytes_read;
683         struct rte_intr_source *src;
684         struct rte_intr_callback *cb;
685         union rte_intr_read_buffer buf;
686         struct rte_intr_callback active_cb;
687
688         for (n = 0; n < nfds; n++) {
689
690                 /**
691                  * if the pipe fd is ready to read, return out to
692                  * rebuild the wait list.
693                  */
694                 if (events[n].data.fd == intr_pipe.readfd){
695                         int r = read(intr_pipe.readfd, buf.charbuf,
696                                         sizeof(buf.charbuf));
697                         RTE_SET_USED(r);
698                         return -1;
699                 }
700                 rte_spinlock_lock(&intr_lock);
701                 TAILQ_FOREACH(src, &intr_sources, next)
702                         if (src->intr_handle.fd ==
703                                         events[n].data.fd)
704                                 break;
705                 if (src == NULL){
706                         rte_spinlock_unlock(&intr_lock);
707                         continue;
708                 }
709
710                 /* mark this interrupt source as active and release the lock. */
711                 src->active = 1;
712                 rte_spinlock_unlock(&intr_lock);
713
714                 /* set the length to be read dor different handle type */
715                 switch (src->intr_handle.type) {
716                 case RTE_INTR_HANDLE_UIO:
717                 case RTE_INTR_HANDLE_UIO_INTX:
718                         bytes_read = sizeof(buf.uio_intr_count);
719                         break;
720                 case RTE_INTR_HANDLE_ALARM:
721                         bytes_read = sizeof(buf.timerfd_num);
722                         break;
723 #ifdef VFIO_PRESENT
724                 case RTE_INTR_HANDLE_VFIO_MSIX:
725                 case RTE_INTR_HANDLE_VFIO_MSI:
726                 case RTE_INTR_HANDLE_VFIO_LEGACY:
727                         bytes_read = sizeof(buf.vfio_intr_count);
728                         break;
729 #endif
730                 case RTE_INTR_HANDLE_VDEV:
731                 case RTE_INTR_HANDLE_EXT:
732                         bytes_read = 0;
733                         call = true;
734                         break;
735
736                 default:
737                         bytes_read = 1;
738                         break;
739                 }
740
741                 if (bytes_read > 0) {
742                         /**
743                          * read out to clear the ready-to-be-read flag
744                          * for epoll_wait.
745                          */
746                         bytes_read = read(events[n].data.fd, &buf, bytes_read);
747                         if (bytes_read < 0) {
748                                 if (errno == EINTR || errno == EWOULDBLOCK)
749                                         continue;
750
751                                 RTE_LOG(ERR, EAL, "Error reading from file "
752                                         "descriptor %d: %s\n",
753                                         events[n].data.fd,
754                                         strerror(errno));
755                         } else if (bytes_read == 0)
756                                 RTE_LOG(ERR, EAL, "Read nothing from file "
757                                         "descriptor %d\n", events[n].data.fd);
758                         else
759                                 call = true;
760                 }
761
762                 /* grab a lock, again to call callbacks and update status. */
763                 rte_spinlock_lock(&intr_lock);
764
765                 if (call) {
766
767                         /* Finally, call all callbacks. */
768                         TAILQ_FOREACH(cb, &src->callbacks, next) {
769
770                                 /* make a copy and unlock. */
771                                 active_cb = *cb;
772                                 rte_spinlock_unlock(&intr_lock);
773
774                                 /* call the actual callback */
775                                 active_cb.cb_fn(&src->intr_handle,
776                                         active_cb.cb_arg);
777
778                                 /*get the lock back. */
779                                 rte_spinlock_lock(&intr_lock);
780                         }
781                 }
782
783                 /* we done with that interrupt source, release it. */
784                 src->active = 0;
785                 rte_spinlock_unlock(&intr_lock);
786         }
787
788         return 0;
789 }
790
791 /**
792  * It handles all the interrupts.
793  *
794  * @param pfd
795  *  epoll file descriptor.
796  * @param totalfds
797  *  The number of file descriptors added in epoll.
798  *
799  * @return
800  *  void
801  */
802 static void
803 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
804 {
805         struct epoll_event events[totalfds];
806         int nfds = 0;
807
808         for(;;) {
809                 nfds = epoll_wait(pfd, events, totalfds,
810                         EAL_INTR_EPOLL_WAIT_FOREVER);
811                 /* epoll_wait fail */
812                 if (nfds < 0) {
813                         if (errno == EINTR)
814                                 continue;
815                         RTE_LOG(ERR, EAL,
816                                 "epoll_wait returns with fail\n");
817                         return;
818                 }
819                 /* epoll_wait timeout, will never happens here */
820                 else if (nfds == 0)
821                         continue;
822                 /* epoll_wait has at least one fd ready to read */
823                 if (eal_intr_process_interrupts(events, nfds) < 0)
824                         return;
825         }
826 }
827
828 /**
829  * It builds/rebuilds up the epoll file descriptor with all the
830  * file descriptors being waited on. Then handles the interrupts.
831  *
832  * @param arg
833  *  pointer. (unused)
834  *
835  * @return
836  *  never return;
837  */
838 static __attribute__((noreturn)) void *
839 eal_intr_thread_main(__rte_unused void *arg)
840 {
841         struct epoll_event ev;
842
843         /* host thread, never break out */
844         for (;;) {
845                 /* build up the epoll fd with all descriptors we are to
846                  * wait on then pass it to the handle_interrupts function
847                  */
848                 static struct epoll_event pipe_event = {
849                         .events = EPOLLIN | EPOLLPRI,
850                 };
851                 struct rte_intr_source *src;
852                 unsigned numfds = 0;
853
854                 /* create epoll fd */
855                 int pfd = epoll_create(1);
856                 if (pfd < 0)
857                         rte_panic("Cannot create epoll instance\n");
858
859                 pipe_event.data.fd = intr_pipe.readfd;
860                 /**
861                  * add pipe fd into wait list, this pipe is used to
862                  * rebuild the wait list.
863                  */
864                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
865                                                 &pipe_event) < 0) {
866                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
867                                         intr_pipe.readfd, strerror(errno));
868                 }
869                 numfds++;
870
871                 rte_spinlock_lock(&intr_lock);
872
873                 TAILQ_FOREACH(src, &intr_sources, next) {
874                         if (src->callbacks.tqh_first == NULL)
875                                 continue; /* skip those with no callbacks */
876                         ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
877                         ev.data.fd = src->intr_handle.fd;
878
879                         /**
880                          * add all the uio device file descriptor
881                          * into wait list.
882                          */
883                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
884                                         src->intr_handle.fd, &ev) < 0){
885                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
886                                         src->intr_handle.fd, strerror(errno));
887                         }
888                         else
889                                 numfds++;
890                 }
891                 rte_spinlock_unlock(&intr_lock);
892                 /* serve the interrupt */
893                 eal_intr_handle_interrupts(pfd, numfds);
894
895                 /**
896                  * when we return, we need to rebuild the
897                  * list of fds to monitor.
898                  */
899                 close(pfd);
900         }
901 }
902
903 int
904 rte_eal_intr_init(void)
905 {
906         int ret = 0, ret_1 = 0;
907         char thread_name[RTE_MAX_THREAD_NAME_LEN];
908
909         /* init the global interrupt source head */
910         TAILQ_INIT(&intr_sources);
911
912         /**
913          * create a pipe which will be waited by epoll and notified to
914          * rebuild the wait list of epoll.
915          */
916         if (pipe(intr_pipe.pipefd) < 0) {
917                 rte_errno = errno;
918                 return -1;
919         }
920
921         /* create the host thread to wait/handle the interrupt */
922         ret = pthread_create(&intr_thread, NULL,
923                         eal_intr_thread_main, NULL);
924         if (ret != 0) {
925                 rte_errno = ret;
926                 RTE_LOG(ERR, EAL,
927                         "Failed to create thread for interrupt handling\n");
928         } else {
929                 /* Set thread_name for aid in debugging. */
930                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
931                         "eal-intr-thread");
932                 ret_1 = rte_thread_setname(intr_thread, thread_name);
933                 if (ret_1 != 0)
934                         RTE_LOG(DEBUG, EAL,
935                         "Failed to set thread name for interrupt handling\n");
936         }
937
938         return -ret;
939 }
940
941 static void
942 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
943 {
944         union rte_intr_read_buffer buf;
945         int bytes_read = 1;
946         int nbytes;
947
948         switch (intr_handle->type) {
949         case RTE_INTR_HANDLE_UIO:
950         case RTE_INTR_HANDLE_UIO_INTX:
951                 bytes_read = sizeof(buf.uio_intr_count);
952                 break;
953 #ifdef VFIO_PRESENT
954         case RTE_INTR_HANDLE_VFIO_MSIX:
955         case RTE_INTR_HANDLE_VFIO_MSI:
956         case RTE_INTR_HANDLE_VFIO_LEGACY:
957                 bytes_read = sizeof(buf.vfio_intr_count);
958                 break;
959 #endif
960         case RTE_INTR_HANDLE_VDEV:
961                 /* for vdev, fd points to:
962                  * a. eventfd which does not need to read out;
963                  * b. datapath fd which needs PMD to read out.
964                  */
965                 return;
966         case RTE_INTR_HANDLE_EXT:
967                 return;
968         default:
969                 bytes_read = 1;
970                 RTE_LOG(INFO, EAL, "unexpected intr type\n");
971                 break;
972         }
973
974         /**
975          * read out to clear the ready-to-be-read flag
976          * for epoll_wait.
977          */
978         do {
979                 nbytes = read(fd, &buf, bytes_read);
980                 if (nbytes < 0) {
981                         if (errno == EINTR || errno == EWOULDBLOCK ||
982                             errno == EAGAIN)
983                                 continue;
984                         RTE_LOG(ERR, EAL,
985                                 "Error reading from fd %d: %s\n",
986                                 fd, strerror(errno));
987                 } else if (nbytes == 0)
988                         RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
989                 return;
990         } while (1);
991 }
992
993 static int
994 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
995                         struct rte_epoll_event *events)
996 {
997         unsigned int i, count = 0;
998         struct rte_epoll_event *rev;
999
1000         for (i = 0; i < n; i++) {
1001                 rev = evs[i].data.ptr;
1002                 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1003                                                  RTE_EPOLL_EXEC))
1004                         continue;
1005
1006                 events[count].status        = RTE_EPOLL_VALID;
1007                 events[count].fd            = rev->fd;
1008                 events[count].epfd          = rev->epfd;
1009                 events[count].epdata.event  = rev->epdata.event;
1010                 events[count].epdata.data   = rev->epdata.data;
1011                 if (rev->epdata.cb_fun)
1012                         rev->epdata.cb_fun(rev->fd,
1013                                            rev->epdata.cb_arg);
1014
1015                 rte_compiler_barrier();
1016                 rev->status = RTE_EPOLL_VALID;
1017                 count++;
1018         }
1019         return count;
1020 }
1021
1022 static inline int
1023 eal_init_tls_epfd(void)
1024 {
1025         int pfd = epoll_create(255);
1026
1027         if (pfd < 0) {
1028                 RTE_LOG(ERR, EAL,
1029                         "Cannot create epoll instance\n");
1030                 return -1;
1031         }
1032         return pfd;
1033 }
1034
1035 int
1036 rte_intr_tls_epfd(void)
1037 {
1038         if (RTE_PER_LCORE(_epfd) == -1)
1039                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1040
1041         return RTE_PER_LCORE(_epfd);
1042 }
1043
1044 int
1045 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1046                int maxevents, int timeout)
1047 {
1048         struct epoll_event evs[maxevents];
1049         int rc;
1050
1051         if (!events) {
1052                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1053                 return -1;
1054         }
1055
1056         /* using per thread epoll fd */
1057         if (epfd == RTE_EPOLL_PER_THREAD)
1058                 epfd = rte_intr_tls_epfd();
1059
1060         while (1) {
1061                 rc = epoll_wait(epfd, evs, maxevents, timeout);
1062                 if (likely(rc > 0)) {
1063                         /* epoll_wait has at least one fd ready to read */
1064                         rc = eal_epoll_process_event(evs, rc, events);
1065                         break;
1066                 } else if (rc < 0) {
1067                         if (errno == EINTR)
1068                                 continue;
1069                         /* epoll_wait fail */
1070                         RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1071                                 strerror(errno));
1072                         rc = -1;
1073                         break;
1074                 } else {
1075                         /* rc == 0, epoll_wait timed out */
1076                         break;
1077                 }
1078         }
1079
1080         return rc;
1081 }
1082
1083 static inline void
1084 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1085 {
1086         while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1087                                     RTE_EPOLL_INVALID))
1088                 while (ev->status != RTE_EPOLL_VALID)
1089                         rte_pause();
1090         memset(&ev->epdata, 0, sizeof(ev->epdata));
1091         ev->fd = -1;
1092         ev->epfd = -1;
1093 }
1094
1095 int
1096 rte_epoll_ctl(int epfd, int op, int fd,
1097               struct rte_epoll_event *event)
1098 {
1099         struct epoll_event ev;
1100
1101         if (!event) {
1102                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1103                 return -1;
1104         }
1105
1106         /* using per thread epoll fd */
1107         if (epfd == RTE_EPOLL_PER_THREAD)
1108                 epfd = rte_intr_tls_epfd();
1109
1110         if (op == EPOLL_CTL_ADD) {
1111                 event->status = RTE_EPOLL_VALID;
1112                 event->fd = fd;  /* ignore fd in event */
1113                 event->epfd = epfd;
1114                 ev.data.ptr = (void *)event;
1115         }
1116
1117         ev.events = event->epdata.event;
1118         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1119                 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1120                         op, fd, strerror(errno));
1121                 if (op == EPOLL_CTL_ADD)
1122                         /* rollback status when CTL_ADD fail */
1123                         event->status = RTE_EPOLL_INVALID;
1124                 return -1;
1125         }
1126
1127         if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1128                 eal_epoll_data_safe_free(event);
1129
1130         return 0;
1131 }
1132
1133 int
1134 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1135                 int op, unsigned int vec, void *data)
1136 {
1137         struct rte_epoll_event *rev;
1138         struct rte_epoll_data *epdata;
1139         int epfd_op;
1140         unsigned int efd_idx;
1141         int rc = 0;
1142
1143         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1144                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1145
1146         if (!intr_handle || intr_handle->nb_efd == 0 ||
1147             efd_idx >= intr_handle->nb_efd) {
1148                 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1149                 return -EPERM;
1150         }
1151
1152         switch (op) {
1153         case RTE_INTR_EVENT_ADD:
1154                 epfd_op = EPOLL_CTL_ADD;
1155                 rev = &intr_handle->elist[efd_idx];
1156                 if (rev->status != RTE_EPOLL_INVALID) {
1157                         RTE_LOG(INFO, EAL, "Event already been added.\n");
1158                         return -EEXIST;
1159                 }
1160
1161                 /* attach to intr vector fd */
1162                 epdata = &rev->epdata;
1163                 epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
1164                 epdata->data   = data;
1165                 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1166                 epdata->cb_arg = (void *)intr_handle;
1167                 rc = rte_epoll_ctl(epfd, epfd_op,
1168                                    intr_handle->efds[efd_idx], rev);
1169                 if (!rc)
1170                         RTE_LOG(DEBUG, EAL,
1171                                 "efd %d associated with vec %d added on epfd %d"
1172                                 "\n", rev->fd, vec, epfd);
1173                 else
1174                         rc = -EPERM;
1175                 break;
1176         case RTE_INTR_EVENT_DEL:
1177                 epfd_op = EPOLL_CTL_DEL;
1178                 rev = &intr_handle->elist[efd_idx];
1179                 if (rev->status == RTE_EPOLL_INVALID) {
1180                         RTE_LOG(INFO, EAL, "Event does not exist.\n");
1181                         return -EPERM;
1182                 }
1183
1184                 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1185                 if (rc)
1186                         rc = -EPERM;
1187                 break;
1188         default:
1189                 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1190                 rc = -EPERM;
1191         }
1192
1193         return rc;
1194 }
1195
1196 void
1197 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1198 {
1199         uint32_t i;
1200         struct rte_epoll_event *rev;
1201
1202         for (i = 0; i < intr_handle->nb_efd; i++) {
1203                 rev = &intr_handle->elist[i];
1204                 if (rev->status == RTE_EPOLL_INVALID)
1205                         continue;
1206                 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1207                         /* force free if the entry valid */
1208                         eal_epoll_data_safe_free(rev);
1209                         rev->status = RTE_EPOLL_INVALID;
1210                 }
1211         }
1212 }
1213
1214 int
1215 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1216 {
1217         uint32_t i;
1218         int fd;
1219         uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1220
1221         assert(nb_efd != 0);
1222
1223         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1224                 for (i = 0; i < n; i++) {
1225                         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1226                         if (fd < 0) {
1227                                 RTE_LOG(ERR, EAL,
1228                                         "can't setup eventfd, error %i (%s)\n",
1229                                         errno, strerror(errno));
1230                                 return -errno;
1231                         }
1232                         intr_handle->efds[i] = fd;
1233                 }
1234                 intr_handle->nb_efd   = n;
1235                 intr_handle->max_intr = NB_OTHER_INTR + n;
1236         } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1237                 /* do nothing, and let vdev driver to initialize this struct */
1238         } else {
1239                 intr_handle->efds[0]  = intr_handle->fd;
1240                 intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
1241                 intr_handle->max_intr = NB_OTHER_INTR;
1242         }
1243
1244         return 0;
1245 }
1246
1247 void
1248 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1249 {
1250         uint32_t i;
1251
1252         rte_intr_free_epoll_fd(intr_handle);
1253         if (intr_handle->max_intr > intr_handle->nb_efd) {
1254                 for (i = 0; i < intr_handle->nb_efd; i++)
1255                         close(intr_handle->efds[i]);
1256         }
1257         intr_handle->nb_efd = 0;
1258         intr_handle->max_intr = 0;
1259 }
1260
1261 int
1262 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1263 {
1264         return !(!intr_handle->nb_efd);
1265 }
1266
1267 int
1268 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1269 {
1270         if (!rte_intr_dp_is_en(intr_handle))
1271                 return 1;
1272         else
1273                 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1274 }
1275
1276 int
1277 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1278 {
1279         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1280                 return 1;
1281
1282         if (intr_handle->type == RTE_INTR_HANDLE_VDEV)
1283                 return 1;
1284
1285         return 0;
1286 }