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