lib: remove extra parenthesis after return
[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
48 #include <rte_common.h>
49 #include <rte_interrupts.h>
50 #include <rte_memory.h>
51 #include <rte_memzone.h>
52 #include <rte_launch.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_ring.h>
59 #include <rte_debug.h>
60 #include <rte_log.h>
61 #include <rte_mempool.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
70 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
71
72 /**
73  * union for pipe fds.
74  */
75 union intr_pipefds{
76         struct {
77                 int pipefd[2];
78         };
79         struct {
80                 int readfd;
81                 int writefd;
82         };
83 };
84
85 /**
86  * union buffer for reading on different devices
87  */
88 union rte_intr_read_buffer {
89         int uio_intr_count;              /* for uio device */
90 #ifdef VFIO_PRESENT
91         uint64_t vfio_intr_count;        /* for vfio device */
92 #endif
93         uint64_t timerfd_num;            /* for timerfd */
94         char charbuf[16];                /* for others */
95 };
96
97 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
98 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
99
100 struct rte_intr_callback {
101         TAILQ_ENTRY(rte_intr_callback) next;
102         rte_intr_callback_fn cb_fn;  /**< callback address */
103         void *cb_arg;                /**< parameter for callback */
104 };
105
106 struct rte_intr_source {
107         TAILQ_ENTRY(rte_intr_source) next;
108         struct rte_intr_handle intr_handle; /**< interrupt handle */
109         struct rte_intr_cb_list callbacks;  /**< user callbacks */
110         uint32_t active;
111 };
112
113 /* global spinlock for interrupt data operation */
114 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
115
116 /* union buffer for pipe read/write */
117 static union intr_pipefds intr_pipe;
118
119 /* interrupt sources list */
120 static struct rte_intr_source_list intr_sources;
121
122 /* interrupt handling thread */
123 static pthread_t intr_thread;
124
125 /* VFIO interrupts */
126 #ifdef VFIO_PRESENT
127
128 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
129
130 /* enable legacy (INTx) interrupts */
131 static int
132 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
133         struct vfio_irq_set *irq_set;
134         char irq_set_buf[IRQ_SET_BUF_LEN];
135         int len, ret;
136         int *fd_ptr;
137
138         len = sizeof(irq_set_buf);
139
140         /* enable INTx */
141         irq_set = (struct vfio_irq_set *) irq_set_buf;
142         irq_set->argsz = len;
143         irq_set->count = 1;
144         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
145         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
146         irq_set->start = 0;
147         fd_ptr = (int *) &irq_set->data;
148         *fd_ptr = intr_handle->fd;
149
150         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
151
152         if (ret) {
153                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
154                                                 intr_handle->fd);
155                 return -1;
156         }
157
158         /* unmask INTx after enabling */
159         memset(irq_set, 0, len);
160         len = sizeof(struct vfio_irq_set);
161         irq_set->argsz = len;
162         irq_set->count = 1;
163         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
164         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
165         irq_set->start = 0;
166
167         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
168
169         if (ret) {
170                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
171                                                 intr_handle->fd);
172                 return -1;
173         }
174         return 0;
175 }
176
177 /* disable legacy (INTx) interrupts */
178 static int
179 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
180         struct vfio_irq_set *irq_set;
181         char irq_set_buf[IRQ_SET_BUF_LEN];
182         int len, ret;
183
184         len = sizeof(struct vfio_irq_set);
185
186         /* mask interrupts before disabling */
187         irq_set = (struct vfio_irq_set *) irq_set_buf;
188         irq_set->argsz = len;
189         irq_set->count = 1;
190         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
191         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
192         irq_set->start = 0;
193
194         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
195
196         if (ret) {
197                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
198                                                 intr_handle->fd);
199                 return -1;
200         }
201
202         /* disable INTx*/
203         memset(irq_set, 0, len);
204         irq_set->argsz = len;
205         irq_set->count = 0;
206         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
207         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
208         irq_set->start = 0;
209
210         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
211
212         if (ret) {
213                 RTE_LOG(ERR, EAL,
214                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
215                 return -1;
216         }
217         return 0;
218 }
219
220 /* enable MSI-X interrupts */
221 static int
222 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
223         int len, ret;
224         char irq_set_buf[IRQ_SET_BUF_LEN];
225         struct vfio_irq_set *irq_set;
226         int *fd_ptr;
227
228         len = sizeof(irq_set_buf);
229
230         irq_set = (struct vfio_irq_set *) irq_set_buf;
231         irq_set->argsz = len;
232         irq_set->count = 1;
233         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
234         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
235         irq_set->start = 0;
236         fd_ptr = (int *) &irq_set->data;
237         *fd_ptr = intr_handle->fd;
238
239         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
240
241         if (ret) {
242                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
243                                                 intr_handle->fd);
244                 return -1;
245         }
246
247         /* manually trigger interrupt to enable it */
248         memset(irq_set, 0, len);
249         len = sizeof(struct vfio_irq_set);
250         irq_set->argsz = len;
251         irq_set->count = 1;
252         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
253         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
254         irq_set->start = 0;
255
256         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
257
258         if (ret) {
259                 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
260                                                 intr_handle->fd);
261                 return -1;
262         }
263         return 0;
264 }
265
266 /* disable MSI-X interrupts */
267 static int
268 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
269         struct vfio_irq_set *irq_set;
270         char irq_set_buf[IRQ_SET_BUF_LEN];
271         int len, ret;
272
273         len = sizeof(struct vfio_irq_set);
274
275         irq_set = (struct vfio_irq_set *) irq_set_buf;
276         irq_set->argsz = len;
277         irq_set->count = 0;
278         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
279         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
280         irq_set->start = 0;
281
282         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
283
284         if (ret)
285                 RTE_LOG(ERR, EAL,
286                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
287
288         return ret;
289 }
290
291 /* enable MSI-X interrupts */
292 static int
293 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
294         int len, ret;
295         char irq_set_buf[IRQ_SET_BUF_LEN];
296         struct vfio_irq_set *irq_set;
297         int *fd_ptr;
298
299         len = sizeof(irq_set_buf);
300
301         irq_set = (struct vfio_irq_set *) irq_set_buf;
302         irq_set->argsz = len;
303         irq_set->count = 1;
304         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
305         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
306         irq_set->start = 0;
307         fd_ptr = (int *) &irq_set->data;
308         *fd_ptr = intr_handle->fd;
309
310         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
311
312         if (ret) {
313                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
314                                                 intr_handle->fd);
315                 return -1;
316         }
317
318         /* manually trigger interrupt to enable it */
319         memset(irq_set, 0, len);
320         len = sizeof(struct vfio_irq_set);
321         irq_set->argsz = len;
322         irq_set->count = 1;
323         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
324         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
325         irq_set->start = 0;
326
327         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
328
329         if (ret) {
330                 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
331                                                 intr_handle->fd);
332                 return -1;
333         }
334         return 0;
335 }
336
337 /* disable MSI-X interrupts */
338 static int
339 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
340         struct vfio_irq_set *irq_set;
341         char irq_set_buf[IRQ_SET_BUF_LEN];
342         int len, ret;
343
344         len = sizeof(struct vfio_irq_set);
345
346         irq_set = (struct vfio_irq_set *) irq_set_buf;
347         irq_set->argsz = len;
348         irq_set->count = 0;
349         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
350         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
351         irq_set->start = 0;
352
353         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
354
355         if (ret)
356                 RTE_LOG(ERR, EAL,
357                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
358
359         return ret;
360 }
361 #endif
362
363 static int
364 uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
365 {
366         unsigned char command_high;
367
368         /* use UIO config file descriptor for uio_pci_generic */
369         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
370                 RTE_LOG(ERR, EAL,
371                         "Error reading interrupts status for fd %d\n",
372                         intr_handle->uio_cfg_fd);
373                 return -1;
374         }
375         /* disable interrupts */
376         command_high |= 0x4;
377         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
378                 RTE_LOG(ERR, EAL,
379                         "Error disabling interrupts for fd %d\n",
380                         intr_handle->uio_cfg_fd);
381                 return -1;
382         }
383
384         return 0;
385 }
386
387 static int
388 uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
389 {
390         unsigned char command_high;
391
392         /* use UIO config file descriptor for uio_pci_generic */
393         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
394                 RTE_LOG(ERR, EAL,
395                         "Error reading interrupts status for fd %d\n",
396                         intr_handle->uio_cfg_fd);
397                 return -1;
398         }
399         /* enable interrupts */
400         command_high &= ~0x4;
401         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
402                 RTE_LOG(ERR, EAL,
403                         "Error enabling interrupts for fd %d\n",
404                         intr_handle->uio_cfg_fd);
405                 return -1;
406         }
407
408         return 0;
409 }
410
411 static int
412 uio_intr_disable(struct rte_intr_handle *intr_handle)
413 {
414         const int value = 0;
415
416         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
417                 RTE_LOG(ERR, EAL,
418                         "Error disabling interrupts for fd %d (%s)\n",
419                         intr_handle->fd, strerror(errno));
420                 return -1;
421         }
422         return 0;
423 }
424
425 static int
426 uio_intr_enable(struct rte_intr_handle *intr_handle)
427 {
428         const int value = 1;
429
430         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
431                 RTE_LOG(ERR, EAL,
432                         "Error enabling interrupts for fd %d (%s)\n",
433                         intr_handle->fd, strerror(errno));
434                 return -1;
435         }
436         return 0;
437 }
438
439 int
440 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
441                         rte_intr_callback_fn cb, void *cb_arg)
442 {
443         int ret, wake_thread;
444         struct rte_intr_source *src;
445         struct rte_intr_callback *callback;
446
447         wake_thread = 0;
448
449         /* first do parameter checking */
450         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
451                 RTE_LOG(ERR, EAL,
452                         "Registering with invalid input parameter\n");
453                 return -EINVAL;
454         }
455
456         /* allocate a new interrupt callback entity */
457         callback = rte_zmalloc("interrupt callback list",
458                                 sizeof(*callback), 0);
459         if (callback == NULL) {
460                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
461                 return -ENOMEM;
462         }
463         callback->cb_fn = cb;
464         callback->cb_arg = cb_arg;
465
466         rte_spinlock_lock(&intr_lock);
467
468         /* check if there is at least one callback registered for the fd */
469         TAILQ_FOREACH(src, &intr_sources, next) {
470                 if (src->intr_handle.fd == intr_handle->fd) {
471                         /* we had no interrupts for this */
472                         if TAILQ_EMPTY(&src->callbacks)
473                                 wake_thread = 1;
474
475                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
476                         ret = 0;
477                         break;
478                 }
479         }
480
481         /* no existing callbacks for this - add new source */
482         if (src == NULL) {
483                 if ((src = rte_zmalloc("interrupt source list",
484                                 sizeof(*src), 0)) == NULL) {
485                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
486                         rte_free(callback);
487                         ret = -ENOMEM;
488                 } else {
489                         src->intr_handle = *intr_handle;
490                         TAILQ_INIT(&src->callbacks);
491                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
492                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
493                         wake_thread = 1;
494                         ret = 0;
495                 }
496         }
497
498         rte_spinlock_unlock(&intr_lock);
499
500         /**
501          * check if need to notify the pipe fd waited by epoll_wait to
502          * rebuild the wait list.
503          */
504         if (wake_thread)
505                 if (write(intr_pipe.writefd, "1", 1) < 0)
506                         return -EPIPE;
507
508         return ret;
509 }
510
511 int
512 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
513                         rte_intr_callback_fn cb_fn, void *cb_arg)
514 {
515         int ret;
516         struct rte_intr_source *src;
517         struct rte_intr_callback *cb, *next;
518
519         /* do parameter checking first */
520         if (intr_handle == NULL || intr_handle->fd < 0) {
521                 RTE_LOG(ERR, EAL,
522                 "Unregistering with invalid input parameter\n");
523                 return -EINVAL;
524         }
525
526         rte_spinlock_lock(&intr_lock);
527
528         /* check if the insterrupt source for the fd is existent */
529         TAILQ_FOREACH(src, &intr_sources, next)
530                 if (src->intr_handle.fd == intr_handle->fd)
531                         break;
532
533         /* No interrupt source registered for the fd */
534         if (src == NULL) {
535                 ret = -ENOENT;
536
537         /* interrupt source has some active callbacks right now. */
538         } else if (src->active != 0) {
539                 ret = -EAGAIN;
540
541         /* ok to remove. */
542         } else {
543                 ret = 0;
544
545                 /*walk through the callbacks and remove all that match. */
546                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
547
548                         next = TAILQ_NEXT(cb, next);
549
550                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
551                                         cb->cb_arg == cb_arg)) {
552                                 TAILQ_REMOVE(&src->callbacks, cb, next);
553                                 rte_free(cb);
554                                 ret++;
555                         }
556                 }
557
558                 /* all callbacks for that source are removed. */
559                 if (TAILQ_EMPTY(&src->callbacks)) {
560                         TAILQ_REMOVE(&intr_sources, src, next);
561                         rte_free(src);
562                 }
563         }
564
565         rte_spinlock_unlock(&intr_lock);
566
567         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
568         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
569                 ret = -EPIPE;
570         }
571
572         return ret;
573 }
574
575 int
576 rte_intr_enable(struct rte_intr_handle *intr_handle)
577 {
578         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
579                 return -1;
580
581         switch (intr_handle->type){
582         /* write to the uio fd to enable the interrupt */
583         case RTE_INTR_HANDLE_UIO:
584                 if (uio_intr_enable(intr_handle))
585                         return -1;
586                 break;
587         case RTE_INTR_HANDLE_UIO_INTX:
588                 if (uio_intx_intr_enable(intr_handle))
589                         return -1;
590                 break;
591         /* not used at this moment */
592         case RTE_INTR_HANDLE_ALARM:
593                 return -1;
594 #ifdef VFIO_PRESENT
595         case RTE_INTR_HANDLE_VFIO_MSIX:
596                 if (vfio_enable_msix(intr_handle))
597                         return -1;
598                 break;
599         case RTE_INTR_HANDLE_VFIO_MSI:
600                 if (vfio_enable_msi(intr_handle))
601                         return -1;
602                 break;
603         case RTE_INTR_HANDLE_VFIO_LEGACY:
604                 if (vfio_enable_intx(intr_handle))
605                         return -1;
606                 break;
607 #endif
608         /* unknown handle type */
609         default:
610                 RTE_LOG(ERR, EAL,
611                         "Unknown handle type of fd %d\n",
612                                         intr_handle->fd);
613                 return -1;
614         }
615
616         return 0;
617 }
618
619 int
620 rte_intr_disable(struct rte_intr_handle *intr_handle)
621 {
622         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
623                 return -1;
624
625         switch (intr_handle->type){
626         /* write to the uio fd to disable the interrupt */
627         case RTE_INTR_HANDLE_UIO:
628                 if (uio_intr_disable(intr_handle))
629                         return -1;
630                 break;
631         case RTE_INTR_HANDLE_UIO_INTX:
632                 if (uio_intx_intr_disable(intr_handle))
633                         return -1;
634                 break;
635         /* not used at this moment */
636         case RTE_INTR_HANDLE_ALARM:
637                 return -1;
638 #ifdef VFIO_PRESENT
639         case RTE_INTR_HANDLE_VFIO_MSIX:
640                 if (vfio_disable_msix(intr_handle))
641                         return -1;
642                 break;
643         case RTE_INTR_HANDLE_VFIO_MSI:
644                 if (vfio_disable_msi(intr_handle))
645                         return -1;
646                 break;
647         case RTE_INTR_HANDLE_VFIO_LEGACY:
648                 if (vfio_disable_intx(intr_handle))
649                         return -1;
650                 break;
651 #endif
652         /* unknown handle type */
653         default:
654                 RTE_LOG(ERR, EAL,
655                         "Unknown handle type of fd %d\n",
656                                         intr_handle->fd);
657                 return -1;
658         }
659
660         return 0;
661 }
662
663 static int
664 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
665 {
666         int n, bytes_read;
667         struct rte_intr_source *src;
668         struct rte_intr_callback *cb;
669         union rte_intr_read_buffer buf;
670         struct rte_intr_callback active_cb;
671
672         for (n = 0; n < nfds; n++) {
673
674                 /**
675                  * if the pipe fd is ready to read, return out to
676                  * rebuild the wait list.
677                  */
678                 if (events[n].data.fd == intr_pipe.readfd){
679                         int r = read(intr_pipe.readfd, buf.charbuf,
680                                         sizeof(buf.charbuf));
681                         RTE_SET_USED(r);
682                         return -1;
683                 }
684                 rte_spinlock_lock(&intr_lock);
685                 TAILQ_FOREACH(src, &intr_sources, next)
686                         if (src->intr_handle.fd ==
687                                         events[n].data.fd)
688                                 break;
689                 if (src == NULL){
690                         rte_spinlock_unlock(&intr_lock);
691                         continue;
692                 }
693
694                 /* mark this interrupt source as active and release the lock. */
695                 src->active = 1;
696                 rte_spinlock_unlock(&intr_lock);
697
698                 /* set the length to be read dor different handle type */
699                 switch (src->intr_handle.type) {
700                 case RTE_INTR_HANDLE_UIO:
701                         bytes_read = sizeof(buf.uio_intr_count);
702                         break;
703                 case RTE_INTR_HANDLE_ALARM:
704                         bytes_read = sizeof(buf.timerfd_num);
705                         break;
706 #ifdef VFIO_PRESENT
707                 case RTE_INTR_HANDLE_VFIO_MSIX:
708                 case RTE_INTR_HANDLE_VFIO_MSI:
709                 case RTE_INTR_HANDLE_VFIO_LEGACY:
710                         bytes_read = sizeof(buf.vfio_intr_count);
711                         break;
712 #endif
713                 default:
714                         bytes_read = 1;
715                         break;
716                 }
717
718                 /**
719                  * read out to clear the ready-to-be-read flag
720                  * for epoll_wait.
721                  */
722                 bytes_read = read(events[n].data.fd, &buf, bytes_read);
723
724                 if (bytes_read < 0)
725                         RTE_LOG(ERR, EAL, "Error reading from file "
726                                 "descriptor %d: %s\n", events[n].data.fd,
727                                                         strerror(errno));
728                 else if (bytes_read == 0)
729                         RTE_LOG(ERR, EAL, "Read nothing from file "
730                                 "descriptor %d\n", events[n].data.fd);
731
732                 /* grab a lock, again to call callbacks and update status. */
733                 rte_spinlock_lock(&intr_lock);
734
735                 if (bytes_read > 0) {
736
737                         /* Finally, call all callbacks. */
738                         TAILQ_FOREACH(cb, &src->callbacks, next) {
739
740                                 /* make a copy and unlock. */
741                                 active_cb = *cb;
742                                 rte_spinlock_unlock(&intr_lock);
743
744                                 /* call the actual callback */
745                                 active_cb.cb_fn(&src->intr_handle,
746                                         active_cb.cb_arg);
747
748                                 /*get the lock back. */
749                                 rte_spinlock_lock(&intr_lock);
750                         }
751                 }
752
753                 /* we done with that interrupt source, release it. */
754                 src->active = 0;
755                 rte_spinlock_unlock(&intr_lock);
756         }
757
758         return 0;
759 }
760
761 /**
762  * It handles all the interrupts.
763  *
764  * @param pfd
765  *  epoll file descriptor.
766  * @param totalfds
767  *  The number of file descriptors added in epoll.
768  *
769  * @return
770  *  void
771  */
772 static void
773 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
774 {
775         struct epoll_event events[totalfds];
776         int nfds = 0;
777
778         for(;;) {
779                 nfds = epoll_wait(pfd, events, totalfds,
780                         EAL_INTR_EPOLL_WAIT_FOREVER);
781                 /* epoll_wait fail */
782                 if (nfds < 0) {
783                         if (errno == EINTR)
784                                 continue;
785                         RTE_LOG(ERR, EAL,
786                                 "epoll_wait returns with fail\n");
787                         return;
788                 }
789                 /* epoll_wait timeout, will never happens here */
790                 else if (nfds == 0)
791                         continue;
792                 /* epoll_wait has at least one fd ready to read */
793                 if (eal_intr_process_interrupts(events, nfds) < 0)
794                         return;
795         }
796 }
797
798 /**
799  * It builds/rebuilds up the epoll file descriptor with all the
800  * file descriptors being waited on. Then handles the interrupts.
801  *
802  * @param arg
803  *  pointer. (unused)
804  *
805  * @return
806  *  never return;
807  */
808 static __attribute__((noreturn)) void *
809 eal_intr_thread_main(__rte_unused void *arg)
810 {
811         struct epoll_event ev;
812
813         /* host thread, never break out */
814         for (;;) {
815                 /* build up the epoll fd with all descriptors we are to
816                  * wait on then pass it to the handle_interrupts function
817                  */
818                 static struct epoll_event pipe_event = {
819                         .events = EPOLLIN | EPOLLPRI,
820                 };
821                 struct rte_intr_source *src;
822                 unsigned numfds = 0;
823
824                 /* create epoll fd */
825                 int pfd = epoll_create(1);
826                 if (pfd < 0)
827                         rte_panic("Cannot create epoll instance\n");
828
829                 pipe_event.data.fd = intr_pipe.readfd;
830                 /**
831                  * add pipe fd into wait list, this pipe is used to
832                  * rebuild the wait list.
833                  */
834                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
835                                                 &pipe_event) < 0) {
836                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
837                                         intr_pipe.readfd, strerror(errno));
838                 }
839                 numfds++;
840
841                 rte_spinlock_lock(&intr_lock);
842
843                 TAILQ_FOREACH(src, &intr_sources, next) {
844                         if (src->callbacks.tqh_first == NULL)
845                                 continue; /* skip those with no callbacks */
846                         ev.events = EPOLLIN | EPOLLPRI;
847                         ev.data.fd = src->intr_handle.fd;
848
849                         /**
850                          * add all the uio device file descriptor
851                          * into wait list.
852                          */
853                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
854                                         src->intr_handle.fd, &ev) < 0){
855                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
856                                         src->intr_handle.fd, strerror(errno));
857                         }
858                         else
859                                 numfds++;
860                 }
861                 rte_spinlock_unlock(&intr_lock);
862                 /* serve the interrupt */
863                 eal_intr_handle_interrupts(pfd, numfds);
864
865                 /**
866                  * when we return, we need to rebuild the
867                  * list of fds to monitor.
868                  */
869                 close(pfd);
870         }
871 }
872
873 int
874 rte_eal_intr_init(void)
875 {
876         int ret = 0;
877
878         /* init the global interrupt source head */
879         TAILQ_INIT(&intr_sources);
880
881         /**
882          * create a pipe which will be waited by epoll and notified to
883          * rebuild the wait list of epoll.
884          */
885         if (pipe(intr_pipe.pipefd) < 0)
886                 return -1;
887
888         /* create the host thread to wait/handle the interrupt */
889         ret = pthread_create(&intr_thread, NULL,
890                         eal_intr_thread_main, NULL);
891         if (ret != 0)
892                 RTE_LOG(ERR, EAL,
893                         "Failed to create thread for interrupt handling\n");
894
895         return -ret;
896 }