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