bus: introduce device level DMA memory mapping
[dpdk.git] / lib / librte_eal / common / include / rte_dev.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014 6WIND S.A.
3  */
4
5 #ifndef _RTE_DEV_H_
6 #define _RTE_DEV_H_
7
8 /**
9  * @file
10  *
11  * RTE PMD Driver Registration Interface
12  *
13  * This file manages the list of device drivers.
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdio.h>
21 #include <sys/queue.h>
22
23 #include <rte_config.h>
24 #include <rte_compat.h>
25 #include <rte_log.h>
26
27 /**
28  * The device event type.
29  */
30 enum rte_dev_event_type {
31         RTE_DEV_EVENT_ADD,      /**< device being added */
32         RTE_DEV_EVENT_REMOVE,   /**< device being removed */
33         RTE_DEV_EVENT_MAX       /**< max value of this enum */
34 };
35
36 struct rte_dev_event {
37         enum rte_dev_event_type type;   /**< device event type */
38         int subsystem;                  /**< subsystem id */
39         char *devname;                  /**< device name */
40 };
41
42 typedef void (*rte_dev_event_cb_fn)(const char *device_name,
43                                         enum rte_dev_event_type event,
44                                         void *cb_arg);
45
46 /* Macros to check for invalid function pointers */
47 #define RTE_FUNC_PTR_OR_ERR_RET(func, retval) do { \
48         if ((func) == NULL) \
49                 return retval; \
50 } while (0)
51
52 #define RTE_FUNC_PTR_OR_RET(func) do { \
53         if ((func) == NULL) \
54                 return; \
55 } while (0)
56
57 /**
58  * Device driver.
59  */
60 enum rte_kernel_driver {
61         RTE_KDRV_UNKNOWN = 0,
62         RTE_KDRV_IGB_UIO,
63         RTE_KDRV_VFIO,
64         RTE_KDRV_UIO_GENERIC,
65         RTE_KDRV_NIC_UIO,
66         RTE_KDRV_NONE,
67 };
68
69 /**
70  * Device policies.
71  */
72 enum rte_dev_policy {
73         RTE_DEV_WHITELISTED,
74         RTE_DEV_BLACKLISTED,
75 };
76
77 /**
78  * A generic memory resource representation.
79  */
80 struct rte_mem_resource {
81         uint64_t phys_addr; /**< Physical address, 0 if not resource. */
82         uint64_t len;       /**< Length of the resource. */
83         void *addr;         /**< Virtual address, NULL when not mapped. */
84 };
85
86 /**
87  * A structure describing a device driver.
88  */
89 struct rte_driver {
90         TAILQ_ENTRY(rte_driver) next;  /**< Next in list. */
91         const char *name;                   /**< Driver name. */
92         const char *alias;              /**< Driver alias. */
93 };
94
95 /*
96  * Internal identifier length
97  * Sufficiently large to allow for UUID or PCI address
98  */
99 #define RTE_DEV_NAME_MAX_LEN 64
100
101 /**
102  * A structure describing a generic device.
103  */
104 struct rte_device {
105         TAILQ_ENTRY(rte_device) next; /**< Next device */
106         const char *name;             /**< Device name */
107         const struct rte_driver *driver; /**< Driver assigned after probing */
108         const struct rte_bus *bus;    /**< Bus handle assigned on scan */
109         int numa_node;                /**< NUMA node connection */
110         struct rte_devargs *devargs;  /**< Arguments for latest probing */
111 };
112
113 /**
114  * @warning
115  * @b EXPERIMENTAL: this API may change without prior notice
116  *
117  * Query status of a device.
118  *
119  * @param dev
120  *   Generic device pointer.
121  * @return
122  *   (int)true if already probed successfully, 0 otherwise.
123  */
124 __rte_experimental
125 int rte_dev_is_probed(const struct rte_device *dev);
126
127 /**
128  * Hotplug add a given device to a specific bus.
129  *
130  * In multi-process, it will request other processes to add the same device.
131  * A failure, in any process, will rollback the action
132  *
133  * @param busname
134  *   The bus name the device is added to.
135  * @param devname
136  *   The device name. Based on this device name, eal will identify a driver
137  *   capable of handling it and pass it to the driver probing function.
138  * @param drvargs
139  *   Device arguments to be passed to the driver.
140  * @return
141  *   0 on success, negative on error.
142  */
143 int rte_eal_hotplug_add(const char *busname, const char *devname,
144                         const char *drvargs);
145
146 /**
147  * Add matching devices.
148  *
149  * In multi-process, it will request other processes to add the same device.
150  * A failure, in any process, will rollback the action
151  *
152  * @param devargs
153  *   Device arguments including bus, class and driver properties.
154  * @return
155  *   0 on success, negative on error.
156  */
157 int rte_dev_probe(const char *devargs);
158
159 /**
160  * Hotplug remove a given device from a specific bus.
161  *
162  * In multi-process, it will request other processes to remove the same device.
163  * A failure, in any process, will rollback the action
164  *
165  * @param busname
166  *   The bus name the device is removed from.
167  * @param devname
168  *   The device name being removed.
169  * @return
170  *   0 on success, negative on error.
171  */
172 int rte_eal_hotplug_remove(const char *busname, const char *devname);
173
174 /**
175  * Remove one device.
176  *
177  * In multi-process, it will request other processes to remove the same device.
178  * A failure, in any process, will rollback the action
179  *
180  * @param dev
181  *   Data structure of the device to remove.
182  * @return
183  *   0 on success, negative on error.
184  */
185 int rte_dev_remove(struct rte_device *dev);
186
187 /**
188  * Device comparison function.
189  *
190  * This type of function is used to compare an rte_device with arbitrary
191  * data.
192  *
193  * @param dev
194  *   Device handle.
195  *
196  * @param data
197  *   Data to compare against. The type of this parameter is determined by
198  *   the kind of comparison performed by the function.
199  *
200  * @return
201  *   0 if the device matches the data.
202  *   !0 if the device does not match.
203  *   <0 if ordering is possible and the device is lower than the data.
204  *   >0 if ordering is possible and the device is greater than the data.
205  */
206 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void *data);
207
208 #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]
209
210 #define RTE_PMD_EXPORT_NAME(name, idx) \
211 static const char RTE_PMD_EXPORT_NAME_ARRAY(this_pmd_name, idx) \
212 __attribute__((used)) = RTE_STR(name)
213
214 #define DRV_EXP_TAG(name, tag) __##name##_##tag
215
216 #define RTE_PMD_REGISTER_PCI_TABLE(name, table) \
217 static const char DRV_EXP_TAG(name, pci_tbl_export)[] __attribute__((used)) = \
218 RTE_STR(table)
219
220 #define RTE_PMD_REGISTER_PARAM_STRING(name, str) \
221 static const char DRV_EXP_TAG(name, param_string_export)[] \
222 __attribute__((used)) = str
223
224 /**
225  * Advertise the list of kernel modules required to run this driver
226  *
227  * This string lists the kernel modules required for the devices
228  * associated to a PMD. The format of each line of the string is:
229  * "<device-pattern> <kmod-expression>".
230  *
231  * The possible formats for the device pattern are:
232  *   "*"                     all devices supported by this driver
233  *   "pci:*"                 all PCI devices supported by this driver
234  *   "pci:v8086:d*:sv*:sd*"  all PCI devices supported by this driver
235  *                           whose vendor id is 0x8086.
236  *
237  * The format of the kernel modules list is a parenthesed expression
238  * containing logical-and (&) and logical-or (|).
239  *
240  * The device pattern and the kmod expression are separated by a space.
241  *
242  * Example:
243  * - "* igb_uio | uio_pci_generic | vfio"
244  */
245 #define RTE_PMD_REGISTER_KMOD_DEP(name, str) \
246 static const char DRV_EXP_TAG(name, kmod_dep_export)[] \
247 __attribute__((used)) = str
248
249 /**
250  * Iteration context.
251  *
252  * This context carries over the current iteration state.
253  */
254 struct rte_dev_iterator {
255         const char *dev_str; /**< device string. */
256         const char *bus_str; /**< bus-related part of device string. */
257         const char *cls_str; /**< class-related part of device string. */
258         struct rte_bus *bus; /**< bus handle. */
259         struct rte_class *cls; /**< class handle. */
260         struct rte_device *device; /**< current position. */
261         void *class_device; /**< additional specialized context. */
262 };
263
264 /**
265  * Device iteration function.
266  *
267  * Find the next device matching properties passed in parameters.
268  * The function takes an additional ``start`` parameter, that is
269  * used as starting context when relevant.
270  *
271  * The function returns the current element in the iteration.
272  * This return value will potentially be used as a start parameter
273  * in subsequent calls to the function.
274  *
275  * The additional iterator parameter is only there if a specific
276  * implementation needs additional context. It must not be modified by
277  * the iteration function itself.
278  *
279  * @param start
280  *   Starting iteration context.
281  *
282  * @param devstr
283  *   Device description string.
284  *
285  * @param it
286  *   Device iterator.
287  *
288  * @return
289  *   The address of the current element matching the device description
290  *   string.
291  */
292 typedef void *(*rte_dev_iterate_t)(const void *start,
293                                    const char *devstr,
294                                    const struct rte_dev_iterator *it);
295
296 /**
297  * Initializes a device iterator.
298  *
299  * This iterator allows accessing a list of devices matching a criteria.
300  * The device matching is made among all buses and classes currently registered,
301  * filtered by the device description given as parameter.
302  *
303  * This function will not allocate any memory. It is safe to stop the
304  * iteration at any moment and let the iterator go out of context.
305  *
306  * @param it
307  *   Device iterator handle.
308  *
309  * @param str
310  *   Device description string.
311  *
312  * @return
313  *   0 on successful initialization.
314  *   <0 on error.
315  */
316 __rte_experimental
317 int
318 rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str);
319
320 /**
321  * Iterates on a device iterator.
322  *
323  * Generates a new rte_device handle corresponding to the next element
324  * in the list described in comprehension by the iterator.
325  *
326  * The next object is returned, and the iterator is updated.
327  *
328  * @param it
329  *   Device iterator handle.
330  *
331  * @return
332  *   An rte_device handle if found.
333  *   NULL if an error occurred (rte_errno is set).
334  *   NULL if no device could be found (rte_errno is not set).
335  */
336 __rte_experimental
337 struct rte_device *
338 rte_dev_iterator_next(struct rte_dev_iterator *it);
339
340 #define RTE_DEV_FOREACH(dev, devstr, it) \
341         for (rte_dev_iterator_init(it, devstr), \
342              dev = rte_dev_iterator_next(it); \
343              dev != NULL; \
344              dev = rte_dev_iterator_next(it))
345
346 #ifdef __cplusplus
347 }
348 #endif
349
350 /**
351  * @warning
352  * @b EXPERIMENTAL: this API may change without prior notice
353  *
354  * It registers the callback for the specific device.
355  * Multiple callbacks cal be registered at the same time.
356  *
357  * @param device_name
358  *  The device name, that is the param name of the struct rte_device,
359  *  null value means for all devices.
360  * @param cb_fn
361  *  callback address.
362  * @param cb_arg
363  *  address of parameter for callback.
364  *
365  * @return
366  *  - On success, zero.
367  *  - On failure, a negative value.
368  */
369 int __rte_experimental
370 rte_dev_event_callback_register(const char *device_name,
371                                 rte_dev_event_cb_fn cb_fn,
372                                 void *cb_arg);
373
374 /**
375  * @warning
376  * @b EXPERIMENTAL: this API may change without prior notice
377  *
378  * It unregisters the callback according to the specified device.
379  *
380  * @param device_name
381  *  The device name, that is the param name of the struct rte_device,
382  *  null value means for all devices and their callbacks.
383  * @param cb_fn
384  *  callback address.
385  * @param cb_arg
386  *  address of parameter for callback, (void *)-1 means to remove all
387  *  registered which has the same callback address.
388  *
389  * @return
390  *  - On success, return the number of callback entities removed.
391  *  - On failure, a negative value.
392  */
393 int __rte_experimental
394 rte_dev_event_callback_unregister(const char *device_name,
395                                   rte_dev_event_cb_fn cb_fn,
396                                   void *cb_arg);
397
398 /**
399  * @warning
400  * @b EXPERIMENTAL: this API may change without prior notice
401  *
402  * Executes all the user application registered callbacks for
403  * the specific device.
404  *
405  * @param device_name
406  *  The device name.
407  * @param event
408  *  the device event type.
409  */
410 void  __rte_experimental
411 rte_dev_event_callback_process(const char *device_name,
412                                enum rte_dev_event_type event);
413
414 /**
415  * @warning
416  * @b EXPERIMENTAL: this API may change without prior notice
417  *
418  * Start the device event monitoring.
419  *
420  * @return
421  *   - On success, zero.
422  *   - On failure, a negative value.
423  */
424 int __rte_experimental
425 rte_dev_event_monitor_start(void);
426
427 /**
428  * @warning
429  * @b EXPERIMENTAL: this API may change without prior notice
430  *
431  * Stop the device event monitoring.
432  *
433  * @return
434  *   - On success, zero.
435  *   - On failure, a negative value.
436  */
437 int __rte_experimental
438 rte_dev_event_monitor_stop(void);
439
440 /**
441  * @warning
442  * @b EXPERIMENTAL: this API may change without prior notice
443  *
444  * Enable hotplug handling for devices.
445  *
446  * @return
447  *   - On success, zero.
448  *   - On failure, a negative value.
449  */
450 int __rte_experimental
451 rte_dev_hotplug_handle_enable(void);
452
453 /**
454  * @warning
455  * @b EXPERIMENTAL: this API may change without prior notice
456  *
457  * Disable hotplug handling for devices.
458  *
459  * @return
460  *   - On success, zero.
461  *   - On failure, a negative value.
462  */
463 int __rte_experimental
464 rte_dev_hotplug_handle_disable(void);
465
466 /**
467  * Device level DMA map function.
468  * After a successful call, the memory segment will be mapped to the
469  * given device.
470  *
471  * @note: Memory must be registered in advance using rte_extmem_* APIs.
472  *
473  * @param dev
474  *      Device pointer.
475  * @param addr
476  *      Virtual address to map.
477  * @param iova
478  *      IOVA address to map.
479  * @param len
480  *      Length of the memory segment being mapped.
481  *
482  * @return
483  *      0 if mapping was successful.
484  *      Negative value and rte_errno is set otherwise.
485  */
486 int __rte_experimental
487 rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len);
488
489 /**
490  * Device level DMA unmap function.
491  * After a successful call, the memory segment will no longer be
492  * accessible by the given device.
493  *
494  * @note: Memory must be registered in advance using rte_extmem_* APIs.
495  *
496  * @param dev
497  *      Device pointer.
498  * @param addr
499  *      Virtual address to unmap.
500  * @param iova
501  *      IOVA address to unmap.
502  * @param len
503  *      Length of the memory segment being mapped.
504  *
505  * @return
506  *      0 if un-mapping was successful.
507  *      Negative value and rte_errno is set otherwise.
508  */
509 int __rte_experimental
510 rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
511                   size_t len);
512
513 #endif /* _RTE_DEV_H_ */