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