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