eal: fix debug macro redefinition
[dpdk.git] / lib / librte_eventdev / rte_eventdev_pmd.h
1 /*
2  *
3  *   Copyright(c) 2016 Cavium networks. All rights reserved.
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in
13  *       the documentation and/or other materials provided with the
14  *       distribution.
15  *     * Neither the name of Cavium networks nor the names of its
16  *       contributors may be used to endorse or promote products derived
17  *       from this software without specific prior written permission.
18  *
19  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef _RTE_EVENTDEV_PMD_H_
33 #define _RTE_EVENTDEV_PMD_H_
34
35 /** @file
36  * RTE Event PMD APIs
37  *
38  * @note
39  * These API are from event PMD only and user applications should not call
40  * them directly.
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <string.h>
48
49 #include <rte_dev.h>
50 #include <rte_pci.h>
51 #include <rte_malloc.h>
52 #include <rte_log.h>
53 #include <rte_common.h>
54
55 #include "rte_eventdev.h"
56
57 /* Logging Macros */
58 #define RTE_EDEV_LOG_ERR(fmt, args...) \
59         RTE_LOG(ERR, EVENTDEV, "%s() line %u: " fmt "\n",  \
60                         __func__, __LINE__, ## args)
61
62 #ifdef RTE_LIBRTE_EVENTDEV_DEBUG
63 #define RTE_EDEV_LOG_DEBUG(fmt, args...) \
64         RTE_LOG(DEBUG, EVENTDEV, "%s() line %u: " fmt "\n",  \
65                         __func__, __LINE__, ## args)
66 #else
67 #define RTE_EDEV_LOG_DEBUG(fmt, args...) (void)0
68 #endif
69
70 /* Macros to check for valid device */
71 #define RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, retval) do { \
72         if (!rte_event_pmd_is_valid_dev((dev_id))) { \
73                 RTE_EDEV_LOG_ERR("Invalid dev_id=%d\n", dev_id); \
74                 return retval; \
75         } \
76 } while (0)
77
78 #define RTE_EVENTDEV_VALID_DEVID_OR_RET(dev_id) do { \
79         if (!rte_event_pmd_is_valid_dev((dev_id))) { \
80                 RTE_EDEV_LOG_ERR("Invalid dev_id=%d\n", dev_id); \
81                 return; \
82         } \
83 } while (0)
84
85 #define RTE_EVENTDEV_DETACHED  (0)
86 #define RTE_EVENTDEV_ATTACHED  (1)
87
88 /**
89  * Initialisation function of a event driver invoked for each matching
90  * event PCI device detected during the PCI probing phase.
91  *
92  * @param dev
93  *   The dev pointer is the address of the *rte_eventdev* structure associated
94  *   with the matching device and which has been [automatically] allocated in
95  *   the *rte_event_devices* array.
96  *
97  * @return
98  *   - 0: Success, the device is properly initialised by the driver.
99  *        In particular, the driver MUST have set up the *dev_ops* pointer
100  *        of the *dev* structure.
101  *   - <0: Error code of the device initialisation failure.
102  */
103 typedef int (*eventdev_init_t)(struct rte_eventdev *dev);
104
105 /**
106  * Finalisation function of a driver invoked for each matching
107  * PCI device detected during the PCI closing phase.
108  *
109  * @param dev
110  *   The dev pointer is the address of the *rte_eventdev* structure associated
111  *   with the matching device and which has been [automatically] allocated in
112  *   the *rte_event_devices* array.
113  *
114  * @return
115  *   - 0: Success, the device is properly finalised by the driver.
116  *        In particular, the driver MUST free the *dev_ops* pointer
117  *        of the *dev* structure.
118  *   - <0: Error code of the device initialisation failure.
119  */
120 typedef int (*eventdev_uninit_t)(struct rte_eventdev *dev);
121
122 /**
123  * The structure associated with a PMD driver.
124  *
125  * Each driver acts as a PCI driver and is represented by a generic
126  * *event_driver* structure that holds:
127  *
128  * - An *rte_pci_driver* structure (which must be the first field).
129  *
130  * - The *eventdev_init* function invoked for each matching PCI device.
131  *
132  * - The size of the private data to allocate for each matching device.
133  */
134 struct rte_eventdev_driver {
135         struct rte_pci_driver pci_drv;  /**< The PMD is also a PCI driver. */
136         unsigned int dev_private_size;  /**< Size of device private data. */
137
138         eventdev_init_t eventdev_init;  /**< Device init function. */
139         eventdev_uninit_t eventdev_uninit; /**< Device uninit function. */
140 };
141
142 /** Global structure used for maintaining state of allocated event devices */
143 struct rte_eventdev_global {
144         uint8_t nb_devs;        /**< Number of devices found */
145 };
146
147 extern struct rte_eventdev_global *rte_eventdev_globals;
148 /** Pointer to global event devices data structure. */
149 extern struct rte_eventdev *rte_eventdevs;
150 /** The pool of rte_eventdev structures. */
151
152 /**
153  * Get the rte_eventdev structure device pointer for the named device.
154  *
155  * @param name
156  *   device name to select the device structure.
157  *
158  * @return
159  *   - The rte_eventdev structure pointer for the given device ID.
160  */
161 static inline struct rte_eventdev *
162 rte_event_pmd_get_named_dev(const char *name)
163 {
164         struct rte_eventdev *dev;
165         unsigned int i;
166
167         if (name == NULL)
168                 return NULL;
169
170         for (i = 0; i < RTE_EVENT_MAX_DEVS; i++) {
171                 dev = &rte_eventdevs[i];
172                 if ((dev->attached == RTE_EVENTDEV_ATTACHED) &&
173                                 (strcmp(dev->data->name, name) == 0))
174                         return dev;
175         }
176
177         return NULL;
178 }
179
180 /**
181  * Validate if the event device index is valid attached event device.
182  *
183  * @param dev_id
184  *   Event device index.
185  *
186  * @return
187  *   - If the device index is valid (1) or not (0).
188  */
189 static inline unsigned
190 rte_event_pmd_is_valid_dev(uint8_t dev_id)
191 {
192         struct rte_eventdev *dev;
193
194         if (dev_id >= RTE_EVENT_MAX_DEVS)
195                 return 0;
196
197         dev = &rte_eventdevs[dev_id];
198         if (dev->attached != RTE_EVENTDEV_ATTACHED)
199                 return 0;
200         else
201                 return 1;
202 }
203
204 /**
205  * Definitions of all functions exported by a driver through the
206  * the generic structure of type *event_dev_ops* supplied in the
207  * *rte_eventdev* structure associated with a device.
208  */
209
210 /**
211  * Get device information of a device.
212  *
213  * @param dev
214  *   Event device pointer
215  * @param dev_info
216  *   Event device information structure
217  *
218  * @return
219  *   Returns 0 on success
220  */
221 typedef void (*eventdev_info_get_t)(struct rte_eventdev *dev,
222                 struct rte_event_dev_info *dev_info);
223
224 /**
225  * Configure a device.
226  *
227  * @param dev
228  *   Event device pointer
229  *
230  * @return
231  *   Returns 0 on success
232  */
233 typedef int (*eventdev_configure_t)(const struct rte_eventdev *dev);
234
235 /**
236  * Start a configured device.
237  *
238  * @param dev
239  *   Event device pointer
240  *
241  * @return
242  *   Returns 0 on success
243  */
244 typedef int (*eventdev_start_t)(struct rte_eventdev *dev);
245
246 /**
247  * Stop a configured device.
248  *
249  * @param dev
250  *   Event device pointer
251  */
252 typedef void (*eventdev_stop_t)(struct rte_eventdev *dev);
253
254 /**
255  * Close a configured device.
256  *
257  * @param dev
258  *   Event device pointer
259  *
260  * @return
261  * - 0 on success
262  * - (-EAGAIN) if can't close as device is busy
263  */
264 typedef int (*eventdev_close_t)(struct rte_eventdev *dev);
265
266 /**
267  * Retrieve the default event queue configuration.
268  *
269  * @param dev
270  *   Event device pointer
271  * @param queue_id
272  *   Event queue index
273  * @param[out] queue_conf
274  *   Event queue configuration structure
275  *
276  */
277 typedef void (*eventdev_queue_default_conf_get_t)(struct rte_eventdev *dev,
278                 uint8_t queue_id, struct rte_event_queue_conf *queue_conf);
279
280 /**
281  * Setup an event queue.
282  *
283  * @param dev
284  *   Event device pointer
285  * @param queue_id
286  *   Event queue index
287  * @param queue_conf
288  *   Event queue configuration structure
289  *
290  * @return
291  *   Returns 0 on success.
292  */
293 typedef int (*eventdev_queue_setup_t)(struct rte_eventdev *dev,
294                 uint8_t queue_id,
295                 const struct rte_event_queue_conf *queue_conf);
296
297 /**
298  * Release resources allocated by given event queue.
299  *
300  * @param dev
301  *   Event device pointer
302  * @param queue_id
303  *   Event queue index
304  *
305  */
306 typedef void (*eventdev_queue_release_t)(struct rte_eventdev *dev,
307                 uint8_t queue_id);
308
309 /**
310  * Retrieve the default event port configuration.
311  *
312  * @param dev
313  *   Event device pointer
314  * @param port_id
315  *   Event port index
316  * @param[out] port_conf
317  *   Event port configuration structure
318  *
319  */
320 typedef void (*eventdev_port_default_conf_get_t)(struct rte_eventdev *dev,
321                 uint8_t port_id, struct rte_event_port_conf *port_conf);
322
323 /**
324  * Setup an event port.
325  *
326  * @param dev
327  *   Event device pointer
328  * @param port_id
329  *   Event port index
330  * @param port_conf
331  *   Event port configuration structure
332  *
333  * @return
334  *   Returns 0 on success.
335  */
336 typedef int (*eventdev_port_setup_t)(struct rte_eventdev *dev,
337                 uint8_t port_id,
338                 const struct rte_event_port_conf *port_conf);
339
340 /**
341  * Release memory resources allocated by given event port.
342  *
343  * @param port
344  *   Event port pointer
345  *
346  */
347 typedef void (*eventdev_port_release_t)(void *port);
348
349 /**
350  * Link multiple source event queues to destination event port.
351  *
352  * @param dev
353  *   Event device pointer
354  * @param port
355  *   Event port pointer
356  * @param link
357  *   Points to an array of *nb_links* event queues to be linked
358  *   to the event port.
359  * @param priorities
360  *   Points to an array of *nb_links* service priorities associated with each
361  *   event queue link to event port.
362  * @param nb_links
363  *   The number of links to establish
364  *
365  * @return
366  *   Returns 0 on success.
367  *
368  */
369 typedef int (*eventdev_port_link_t)(struct rte_eventdev *dev, void *port,
370                 const uint8_t queues[], const uint8_t priorities[],
371                 uint16_t nb_links);
372
373 /**
374  * Unlink multiple source event queues from destination event port.
375  *
376  * @param dev
377  *   Event device pointer
378  * @param port
379  *   Event port pointer
380  * @param queues
381  *   An array of *nb_unlinks* event queues to be unlinked from the event port.
382  * @param nb_unlinks
383  *   The number of unlinks to establish
384  *
385  * @return
386  *   Returns 0 on success.
387  *
388  */
389 typedef int (*eventdev_port_unlink_t)(struct rte_eventdev *dev, void *port,
390                 uint8_t queues[], uint16_t nb_unlinks);
391
392 /**
393  * Converts nanoseconds to *timeout_ticks* value for rte_event_dequeue()
394  *
395  * @param dev
396  *   Event device pointer
397  * @param ns
398  *   Wait time in nanosecond
399  * @param[out] timeout_ticks
400  *   Value for the *timeout_ticks* parameter in rte_event_dequeue() function
401  *
402  * @return
403  *   Returns 0 on success.
404  *
405  */
406 typedef int (*eventdev_dequeue_timeout_ticks_t)(struct rte_eventdev *dev,
407                 uint64_t ns, uint64_t *timeout_ticks);
408
409 /**
410  * Dump internal information
411  *
412  * @param dev
413  *   Event device pointer
414  * @param f
415  *   A pointer to a file for output
416  *
417  */
418 typedef void (*eventdev_dump_t)(struct rte_eventdev *dev, FILE *f);
419
420 /**
421  * Retrieve a set of statistics from device
422  *
423  * @param dev
424  *   Event device pointer
425  * @param ids
426  *   The stat ids to retrieve
427  * @param values
428  *   The returned stat values
429  * @param n
430  *   The number of id values and entries in the values array
431  * @return
432  *   The number of stat values successfully filled into the values array
433  */
434 typedef int (*eventdev_xstats_get_t)(const struct rte_eventdev *dev,
435                 enum rte_event_dev_xstats_mode mode, uint8_t queue_port_id,
436                 const unsigned int ids[], uint64_t values[], unsigned int n);
437
438 /**
439  * Resets the statistic values in xstats for the device, based on mode.
440  */
441 typedef int (*eventdev_xstats_reset_t)(struct rte_eventdev *dev,
442                 enum rte_event_dev_xstats_mode mode,
443                 int16_t queue_port_id,
444                 const uint32_t ids[],
445                 uint32_t nb_ids);
446
447 /**
448  * Get names of extended stats of an event device
449  *
450  * @param dev
451  *   Event device pointer
452  * @param xstats_names
453  *   Array of name values to be filled in
454  * @param size
455  *   Number of values in the xstats_names array
456  * @return
457  *   When size >= the number of stats, return the number of stat values filled
458  *   into the array.
459  *   When size < the number of available stats, return the number of stats
460  *   values, and do not fill in any data into xstats_names.
461  */
462 typedef int (*eventdev_xstats_get_names_t)(const struct rte_eventdev *dev,
463                 enum rte_event_dev_xstats_mode mode, uint8_t queue_port_id,
464                 struct rte_event_dev_xstats_name *xstats_names,
465                 unsigned int *ids, unsigned int size);
466
467 /**
468  * Get value of one stats and optionally return its id
469  *
470  * @param dev
471  *   Event device pointer
472  * @param name
473  *   The name of the stat to retrieve
474  * @param id
475  *   Pointer to an unsigned int where we store the stat-id for future reference.
476  *   This pointer may be null if the id is not required.
477  * @return
478  *   The value of the stat, or (uint64_t)-1 if the stat is not found.
479  *   If the stat is not found, the id value will be returned as (unsigned)-1,
480  *   if id pointer is non-NULL
481  */
482 typedef uint64_t (*eventdev_xstats_get_by_name)(const struct rte_eventdev *dev,
483                 const char *name, unsigned int *id);
484
485 /** Event device operations function pointer table */
486 struct rte_eventdev_ops {
487         eventdev_info_get_t dev_infos_get;      /**< Get device info. */
488         eventdev_configure_t dev_configure;     /**< Configure device. */
489         eventdev_start_t dev_start;             /**< Start device. */
490         eventdev_stop_t dev_stop;               /**< Stop device. */
491         eventdev_close_t dev_close;             /**< Close device. */
492
493         eventdev_queue_default_conf_get_t queue_def_conf;
494         /**< Get default queue configuration. */
495         eventdev_queue_setup_t queue_setup;
496         /**< Set up an event queue. */
497         eventdev_queue_release_t queue_release;
498         /**< Release an event queue. */
499
500         eventdev_port_default_conf_get_t port_def_conf;
501         /**< Get default port configuration. */
502         eventdev_port_setup_t port_setup;
503         /**< Set up an event port. */
504         eventdev_port_release_t port_release;
505         /**< Release an event port. */
506
507         eventdev_port_link_t port_link;
508         /**< Link event queues to an event port. */
509         eventdev_port_unlink_t port_unlink;
510         /**< Unlink event queues from an event port. */
511         eventdev_dequeue_timeout_ticks_t timeout_ticks;
512         /**< Converts ns to *timeout_ticks* value for rte_event_dequeue() */
513         eventdev_dump_t dump;
514         /* Dump internal information */
515
516         eventdev_xstats_get_t xstats_get;
517         /**< Get extended device statistics. */
518         eventdev_xstats_get_names_t xstats_get_names;
519         /**< Get names of extended stats. */
520         eventdev_xstats_get_by_name xstats_get_by_name;
521         /**< Get one value by name. */
522         eventdev_xstats_reset_t xstats_reset;
523         /**< Reset the statistics values in xstats. */
524 };
525
526 /**
527  * Allocates a new eventdev slot for an event device and returns the pointer
528  * to that slot for the driver to use.
529  *
530  * @param name
531  *   Unique identifier name for each device
532  * @param socket_id
533  *   Socket to allocate resources on.
534  * @return
535  *   - Slot in the rte_dev_devices array for a new device;
536  */
537 struct rte_eventdev *
538 rte_event_pmd_allocate(const char *name, int socket_id);
539
540 /**
541  * Release the specified eventdev device.
542  *
543  * @param eventdev
544  * The *eventdev* pointer is the address of the *rte_eventdev* structure.
545  * @return
546  *   - 0 on success, negative on error
547  */
548 int
549 rte_event_pmd_release(struct rte_eventdev *eventdev);
550
551 /**
552  * Creates a new virtual event device and returns the pointer to that device.
553  *
554  * @param name
555  *   PMD type name
556  * @param dev_private_size
557  *   Size of event PMDs private data
558  * @param socket_id
559  *   Socket to allocate resources on.
560  *
561  * @return
562  *   - Eventdev pointer if device is successfully created.
563  *   - NULL if device cannot be created.
564  */
565 struct rte_eventdev *
566 rte_event_pmd_vdev_init(const char *name, size_t dev_private_size,
567                 int socket_id);
568
569 /**
570  * Destroy the given virtual event device
571  *
572  * @param name
573  *   PMD type name
574  * @return
575  *   - 0 on success, negative on error
576  */
577 int
578 rte_event_pmd_vdev_uninit(const char *name);
579
580 /**
581  * Wrapper for use by pci drivers as a .probe function to attach to a event
582  * interface.
583  */
584 int rte_event_pmd_pci_probe(struct rte_pci_driver *pci_drv,
585                             struct rte_pci_device *pci_dev);
586
587 /**
588  * Wrapper for use by pci drivers as a .remove function to detach a event
589  * interface.
590  */
591 int rte_event_pmd_pci_remove(struct rte_pci_device *pci_dev);
592
593 #ifdef __cplusplus
594 }
595 #endif
596
597 #endif /* _RTE_EVENTDEV_PMD_H_ */