1720f1dd6b9193ebf28eaeca14a01adb33a6f546
[dpdk.git] / lib / librte_rawdev / rte_rawdev.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 NXP
3  */
4
5 #ifndef _RTE_RAWDEV_H_
6 #define _RTE_RAWDEV_H_
7
8 /**
9  * @file rte_rawdev.h
10  *
11  * Generic device abstraction APIs.
12  *
13  * This API allow applications to configure and use generic devices having
14  * no specific type already available in DPDK.
15  */
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #include <rte_common.h>
22 #include <rte_memory.h>
23 #include <rte_errno.h>
24
25 /* Rawdevice object - essentially a void to be typecast by implementation */
26 typedef void *rte_rawdev_obj_t;
27
28 /**
29  * Get the total number of raw devices that have been successfully
30  * initialised.
31  *
32  * @return
33  *   The total number of usable raw devices.
34  */
35 uint8_t
36 rte_rawdev_count(void);
37
38 /**
39  * Get the device identifier for the named raw device.
40  *
41  * @param name
42  *   Raw device name to select the raw device identifier.
43  *
44  * @return
45  *   Returns raw device identifier on success.
46  *   - <0: Failure to find named raw device.
47  */
48 uint16_t
49 rte_rawdev_get_dev_id(const char *name);
50
51 /**
52  * Return the NUMA socket to which a device is connected.
53  *
54  * @param dev_id
55  *   The identifier of the device.
56  * @return
57  *   The NUMA socket id to which the device is connected or
58  *   a default of zero if the socket could not be determined.
59  *   -(-EINVAL)  dev_id value is out of range.
60  */
61 int
62 rte_rawdev_socket_id(uint16_t dev_id);
63
64 /**
65  * Raw device information forward declaration
66  */
67 struct rte_rawdev_info;
68
69 /**
70  * Retrieve the contextual information of a raw device.
71  *
72  * @param dev_id
73  *   The identifier of the device.
74  *
75  * @param[out] dev_info
76  *   A pointer to a structure of type *rte_rawdev_info* to be filled with the
77  *   contextual information of the device. The dev_info->dev_private field
78  *   should point to an appropriate buffer space for holding the device-
79  *   specific info for that hardware.
80  *   If the dev_private field is set to NULL, then the device-specific info
81  *   function will not be called and only basic information about the device
82  *   will be returned. This can be used to safely query the type of a rawdev
83  *   instance without needing to know the size of the private data to return.
84  *
85  * @param dev_private_size
86  *   The length of the memory space pointed to by dev_private in dev_info.
87  *   This should be set to the size of the expected private structure to be
88  *   returned, and may be checked by drivers to ensure the expected struct
89  *   type is provided.
90  *
91  * @return
92  *   - 0: Success, driver updates the contextual information of the raw device
93  *   - <0: Error code returned by the driver info get function.
94  *
95  */
96 int
97 rte_rawdev_info_get(uint16_t dev_id, struct rte_rawdev_info *dev_info,
98                 size_t dev_private_size);
99
100 /**
101  * Configure a raw device.
102  *
103  * This function must be invoked first before any other function in the
104  * API. This function can also be re-invoked when a device is in the
105  * stopped state.
106  *
107  * The caller may use rte_rawdev_info_get() to get the capability of each
108  * resources available for this raw device.
109  *
110  * @param dev_id
111  *   The identifier of the device to configure.
112  * @param dev_conf
113  *   The raw device configuration structure encapsulated into rte_rawdev_info
114  *   object.
115  *   It is assumed that the opaque object has enough information which the
116  *   driver/implementation can use to configure the device. It is also assumed
117  *   that once the configuration is done, a `queue_id` type field can be used
118  *   to refer to some arbitrary internal representation of a queue.
119  * @param dev_private_size
120  *   The length of the memory space pointed to by dev_private in dev_info.
121  *   This should be set to the size of the expected private structure to be
122  *   used by the driver, and may be checked by drivers to ensure the expected
123  *   struct type is provided.
124  *
125  * @return
126  *   - 0: Success, device configured.
127  *   - <0: Error code returned by the driver configuration function.
128  */
129 int
130 rte_rawdev_configure(uint16_t dev_id, struct rte_rawdev_info *dev_conf,
131                 size_t dev_private_size);
132
133
134 /**
135  * Retrieve the current configuration information of a raw queue designated
136  * by its *queue_id* from the raw driver for a raw device.
137  *
138  * This function intended to be used in conjunction with rte_raw_queue_setup()
139  * where caller needs to set up the queue by overriding few default values.
140  *
141  * @param dev_id
142  *   The identifier of the device.
143  * @param queue_id
144  *   The index of the raw queue to get the configuration information.
145  *   The value must be in the range [0, nb_raw_queues - 1]
146  *   previously supplied to rte_rawdev_configure().
147  * @param[out] queue_conf
148  *   The pointer to the default raw queue configuration data.
149  * @return
150  *   - 0: Success, driver updates the default raw queue configuration data.
151  *   - <0: Error code returned by the driver info get function.
152  *
153  * @see rte_raw_queue_setup()
154  *
155  */
156 int
157 rte_rawdev_queue_conf_get(uint16_t dev_id,
158                           uint16_t queue_id,
159                           rte_rawdev_obj_t queue_conf);
160
161 /**
162  * Allocate and set up a raw queue for a raw device.
163  *
164  * @param dev_id
165  *   The identifier of the device.
166  * @param queue_id
167  *   The index of the raw queue to setup. The value must be in the range
168  *   [0, nb_raw_queues - 1] previously supplied to rte_rawdev_configure().
169  * @param queue_conf
170  *   The pointer to the configuration data to be used for the raw queue.
171  *   NULL value is allowed, in which case default configuration used.
172  *
173  * @see rte_rawdev_queue_conf_get()
174  *
175  * @return
176  *   - 0: Success, raw queue correctly set up.
177  *   - <0: raw queue configuration failed
178  */
179 int
180 rte_rawdev_queue_setup(uint16_t dev_id,
181                        uint16_t queue_id,
182                        rte_rawdev_obj_t queue_conf);
183
184 /**
185  * Release and deallocate a raw queue from a raw device.
186  *
187  * @param dev_id
188  *   The identifier of the device.
189  * @param queue_id
190  *   The index of the raw queue to release. The value must be in the range
191  *   [0, nb_raw_queues - 1] previously supplied to rte_rawdev_configure().
192  *
193  * @see rte_rawdev_queue_conf_get()
194  *
195  * @return
196  *   - 0: Success, raw queue released.
197  *   - <0: raw queue configuration failed
198  */
199 int
200 rte_rawdev_queue_release(uint16_t dev_id, uint16_t queue_id);
201
202 /**
203  * Get the number of raw queues on a specific raw device
204  *
205  * @param dev_id
206  *   Raw device identifier.
207  * @return
208  *   - The number of configured raw queues
209  */
210 uint16_t
211 rte_rawdev_queue_count(uint16_t dev_id);
212
213 /**
214  * Start a raw device.
215  *
216  * The device start step is the last one and consists of setting the raw
217  * queues to start accepting the raws and schedules to raw ports.
218  *
219  * On success, all basic functions exported by the API (raw enqueue,
220  * raw dequeue and so on) can be invoked.
221  *
222  * @param dev_id
223  *   Raw device identifier
224  * @return
225  *   - 0: Success, device started.
226  *   < 0: Failure
227  */
228 int
229 rte_rawdev_start(uint16_t dev_id);
230
231 /**
232  * Stop a raw device. The device can be restarted with a call to
233  * rte_rawdev_start()
234  *
235  * @param dev_id
236  *   Raw device identifier.
237  */
238 void
239 rte_rawdev_stop(uint16_t dev_id);
240
241 /**
242  * Close a raw device. The device cannot be restarted after this call.
243  *
244  * @param dev_id
245  *   Raw device identifier
246  *
247  * @return
248  *  - 0 on successfully closing device
249  *  - <0 on failure to close device
250  *  - (-EAGAIN) if device is busy
251  */
252 int
253 rte_rawdev_close(uint16_t dev_id);
254
255 /**
256  * Reset a raw device.
257  * This is different from cycle of rte_rawdev_start->rte_rawdev_stop in the
258  * sense similar to hard or soft reset.
259  *
260  * @param dev_id
261  *   Raw device identifiers
262  * @return
263  *   0 for successful reset,
264  *  !0 for failure in resetting
265  */
266 int
267 rte_rawdev_reset(uint16_t dev_id);
268
269 #define RTE_RAWDEV_NAME_MAX_LEN (64)
270 /**< @internal Max length of name of raw PMD */
271
272
273
274 /** @internal
275  * The data structure associated with each raw device.
276  * It is a placeholder for PMD specific data, encapsulating only information
277  * related to framework.
278  */
279 struct rte_rawdev {
280         /**< Socket ID where memory is allocated */
281         int socket_id;
282         /**< Device ID for this instance */
283         uint16_t dev_id;
284         /**< Functions exported by PMD */
285         const struct rte_rawdev_ops *dev_ops;
286         /**< Device info. supplied during device initialization */
287         struct rte_device *device;
288         /**< Driver info. supplied by probing */
289         const char *driver_name;
290
291         RTE_STD_C11
292         /**< Flag indicating the device is attached */
293         uint8_t attached : 1;
294         /**< Device state: STARTED(1)/STOPPED(0) */
295         uint8_t started : 1;
296
297         /**< PMD-specific private data */
298         rte_rawdev_obj_t dev_private;
299         /**< Device name */
300         char name[RTE_RAWDEV_NAME_MAX_LEN];
301 } __rte_cache_aligned;
302
303 /** @internal The pool of rte_rawdev structures. */
304 extern struct rte_rawdev *rte_rawdevs;
305
306
307 struct rte_rawdev_info {
308         /**< Name of driver handling this device */
309         const char *driver_name;
310         /**< Device encapsulation */
311         struct rte_device *device;
312         /**< Socket ID where memory is allocated */
313         int socket_id;
314         /**< PMD-specific private data */
315         rte_rawdev_obj_t dev_private;
316 };
317
318 struct rte_rawdev_buf {
319         /**< Opaque buffer reference */
320         void *buf_addr;
321 };
322
323 /**
324  * Dump internal information about *dev_id* to the FILE* provided in *f*.
325  *
326  * @param dev_id
327  *   The identifier of the device.
328  *
329  * @param f
330  *   A pointer to a file for output
331  *
332  * @return
333  *   - 0: on success
334  *   - <0: on failure.
335  */
336 int
337 rte_rawdev_dump(uint16_t dev_id, FILE *f);
338
339 /**
340  * Get an attribute value from implementation.
341  * Attribute is an opaque handle agreed upon between application and PMD.
342  *
343  * Implementations are expected to maintain an array of attribute-value pairs
344  * based on application calls. Memory management for this structure is
345  * shared responsibility of implementation and application.
346  *
347  * @param dev_id
348  *   The identifier of the device to configure.
349  * @param attr_name
350  *   Opaque object representing an attribute in implementation.
351  * @param attr_value [out]
352  *   Opaque response to the attribute value. In case of error, this remains
353  *   untouched. This is double pointer of void type.
354  * @return
355  *   0 for success
356  *  !0 Error; attr_value remains untouched in case of error.
357  */
358 int
359 rte_rawdev_get_attr(uint16_t dev_id,
360                     const char *attr_name,
361                     uint64_t *attr_value);
362
363 /**
364  * Set an attribute value.
365  * Attribute is an opaque handle agreed upon between application and PMD.
366  *
367  * @param dev_id
368  *   The identifier of the device to configure.
369  * @param attr_name
370  *   Opaque object representing an attribute in implementation.
371  * @param attr_value
372  *   Value of the attribute represented by attr_name
373  * @return
374  *   0 for success
375  *  !0 Error
376  */
377 int
378 rte_rawdev_set_attr(uint16_t dev_id,
379                     const char *attr_name,
380                     const uint64_t attr_value);
381
382 /**
383  * Enqueue a stream of buffers to the device.
384  *
385  * Rather than specifying a queue, this API passes along an opaque object
386  * to the driver implementation. That object can be a queue or any other
387  * contextual information necessary for the device to enqueue buffers.
388  *
389  * @param dev_id
390  *   The identifier of the device to configure.
391  * @param buffers
392  *   Collection of buffers for enqueuing
393  * @param count
394  *   Count of buffers to enqueue
395  * @param context
396  *   Opaque context information.
397  * @return
398  *   >=0 for buffers enqueued
399  *  !0 for failure.
400  *  Whether partial enqueue is failure or success is defined between app
401  *  and driver implementation.
402  */
403 int
404 rte_rawdev_enqueue_buffers(uint16_t dev_id,
405                            struct rte_rawdev_buf **buffers,
406                            unsigned int count,
407                            rte_rawdev_obj_t context);
408
409 /**
410  * Dequeue a stream of buffers from the device.
411  *
412  * Rather than specifying a queue, this API passes along an opaque object
413  * to the driver implementation. That object can be a queue or any other
414  * contextual information necessary for the device to dequeue buffers.
415  *
416  * Application should have allocated enough space to store `count` response
417  * buffers.
418  * Releasing buffers dequeued is responsibility of the application.
419  *
420  * @param dev_id
421  *   The identifier of the device to configure.
422  * @param buffers
423  *   Collection of buffers dequeued
424  * @param count
425  *   Max buffers expected to be dequeued
426  * @param context
427  *   Opaque context information.
428  * @return
429  *   >=0 for buffers dequeued
430  *  !0 for failure.
431  *  Whether partial enqueue is failure or success is defined between app
432  *  and driver implementation.
433  */
434 int
435 rte_rawdev_dequeue_buffers(uint16_t dev_id,
436                            struct rte_rawdev_buf **buffers,
437                            unsigned int count,
438                            rte_rawdev_obj_t context);
439
440 /** Maximum name length for extended statistics counters */
441 #define RTE_RAW_DEV_XSTATS_NAME_SIZE 64
442
443 /**
444  * A name-key lookup element for extended statistics.
445  *
446  * This structure is used to map between names and ID numbers
447  * for extended ethdev statistics.
448  */
449 struct rte_rawdev_xstats_name {
450         char name[RTE_RAW_DEV_XSTATS_NAME_SIZE];
451 };
452
453 /**
454  * Retrieve names of extended statistics of a raw device.
455  *
456  * @param dev_id
457  *   The identifier of the raw device.
458  * @param[out] xstats_names
459  *   Block of memory to insert names into. Must be at least size in capacity.
460  *   If set to NULL, function returns required capacity.
461  * @param size
462  *   Capacity of xstats_names (number of names).
463  * @return
464  *   - positive value lower or equal to size: success. The return value
465  *     is the number of entries filled in the stats table.
466  *   - positive value higher than size: error, the given statistics table
467  *     is too small. The return value corresponds to the size that should
468  *     be given to succeed. The entries in the table are not valid and
469  *     shall not be used by the caller.
470  *   - negative value on error:
471  *        -ENODEV for invalid *dev_id*
472  *        -ENOTSUP if the device doesn't support this function.
473  */
474 int
475 rte_rawdev_xstats_names_get(uint16_t dev_id,
476                             struct rte_rawdev_xstats_name *xstats_names,
477                             unsigned int size);
478
479 /**
480  * Retrieve extended statistics of a raw device.
481  *
482  * @param dev_id
483  *   The identifier of the device.
484  * @param ids
485  *   The id numbers of the stats to get. The ids can be got from the stat
486  *   position in the stat list from rte_rawdev_get_xstats_names(), or
487  *   by using rte_rawdev_get_xstats_by_name()
488  * @param[out] values
489  *   The values for each stats request by ID.
490  * @param n
491  *   The number of stats requested
492  * @return
493  *   - positive value: number of stat entries filled into the values array
494  *   - negative value on error:
495  *        -ENODEV for invalid *dev_id*
496  *        -ENOTSUP if the device doesn't support this function.
497  */
498 int
499 rte_rawdev_xstats_get(uint16_t dev_id,
500                       const unsigned int ids[],
501                       uint64_t values[],
502                       unsigned int n);
503
504 /**
505  * Retrieve the value of a single stat by requesting it by name.
506  *
507  * @param dev_id
508  *   The identifier of the device
509  * @param name
510  *   The stat name to retrieve
511  * @param[out] id
512  *   If non-NULL, the numerical id of the stat will be returned, so that further
513  *   requests for the stat can be got using rte_rawdev_xstats_get, which will
514  *   be faster as it doesn't need to scan a list of names for the stat.
515  *   If the stat cannot be found, the id returned will be (unsigned)-1.
516  * @return
517  *   - positive value or zero: the stat value
518  *   - negative value: -EINVAL if stat not found, -ENOTSUP if not supported.
519  */
520 uint64_t
521 rte_rawdev_xstats_by_name_get(uint16_t dev_id,
522                               const char *name,
523                               unsigned int *id);
524
525 /**
526  * Reset the values of the xstats of the selected component in the device.
527  *
528  * @param dev_id
529  *   The identifier of the device
530  * @param ids
531  *   Selects specific statistics to be reset. When NULL, all statistics
532  *   will be reset. If non-NULL, must point to array of at least
533  *   *nb_ids* size.
534  * @param nb_ids
535  *   The number of ids available from the *ids* array. Ignored when ids is NULL.
536  * @return
537  *   - zero: successfully reset the statistics to zero
538  *   - negative value: -EINVAL invalid parameters, -ENOTSUP if not supported.
539  */
540 int
541 rte_rawdev_xstats_reset(uint16_t dev_id,
542                         const uint32_t ids[],
543                         uint32_t nb_ids);
544
545 /**
546  * Get Firmware status of the device..
547  * Returns a memory allocated by driver/implementation containing status
548  * information block. It is responsibility of caller to release the buffer.
549  *
550  * @param dev_id
551  *   Raw device identifier
552  * @param status_info
553  *   Pointer to status information area. Caller is responsible for releasing
554  *   the memory associated.
555  * @return
556  *   0 for success,
557  *  !0 for failure, `status_info` argument state is undefined
558  */
559 int
560 rte_rawdev_firmware_status_get(uint16_t dev_id,
561                                rte_rawdev_obj_t status_info);
562
563 /**
564  * Get Firmware version of the device.
565  * Returns a memory allocated by driver/implementation containing version
566  * information block. It is responsibility of caller to release the buffer.
567  *
568  * @param dev_id
569  *   Raw device identifier
570  * @param version_info
571  *   Pointer to version information area. Caller is responsible for releasing
572  *   the memory associated.
573  * @return
574  *   0 for success,
575  *  !0 for failure, `version_info` argument state is undefined
576  */
577 int
578 rte_rawdev_firmware_version_get(uint16_t dev_id,
579                                 rte_rawdev_obj_t version_info);
580
581 /**
582  * Load firmware on the device.
583  * TODO: In future, methods like directly flashing from file too can be
584  * supported.
585  *
586  * @param dev_id
587  *   Raw device identifier
588  * @param firmware_image
589  *   Pointer to buffer containing image binary data
590  * @return
591  *   0 for successful load
592  *  !0 for failure to load the provided image, or image incorrect.
593  */
594 int
595 rte_rawdev_firmware_load(uint16_t dev_id, rte_rawdev_obj_t firmware_image);
596
597 /**
598  * Unload firmware from the device.
599  *
600  * @param dev_id
601  *   Raw device identifiers
602  * @return
603  *   0 for successful Unload
604  *  !0 for failure in unloading
605  */
606 int
607 rte_rawdev_firmware_unload(uint16_t dev_id);
608
609 /**
610  * Trigger the rawdev self test.
611  *
612  * @param dev_id
613  *   The identifier of the device
614  * @return
615  *   - 0: Selftest successful
616  *   - -ENOTSUP if the device doesn't support selftest
617  *   - other values < 0 on failure.
618  */
619 int
620 rte_rawdev_selftest(uint16_t dev_id);
621
622 #ifdef __cplusplus
623 }
624 #endif
625
626 #endif /* _RTE_RAWDEV_H_ */