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