xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.h (revision 1094dd940ec0cc4e3ce2c5cd94807350855a17f9)
1391b8bccSOphir Munk /* SPDX-License-Identifier: BSD-3-Clause
2391b8bccSOphir Munk  * Copyright 2020 Mellanox Technologies, Ltd
3391b8bccSOphir Munk  */
4391b8bccSOphir Munk 
5391b8bccSOphir Munk #ifndef RTE_PMD_MLX5_COMMON_OS_H_
6391b8bccSOphir Munk #define RTE_PMD_MLX5_COMMON_OS_H_
7391b8bccSOphir Munk 
8391b8bccSOphir Munk #include <stdio.h>
97e7af4e9SOphir Munk #include <malloc.h>
10391b8bccSOphir Munk 
11*1094dd94SDavid Marchand #include <rte_compat.h>
12391b8bccSOphir Munk #include <rte_pci.h>
131f37cb2bSDavid Marchand #include <bus_pci_driver.h>
14391b8bccSOphir Munk #include <rte_debug.h>
15391b8bccSOphir Munk #include <rte_atomic.h>
16391b8bccSOphir Munk #include <rte_log.h>
17391b8bccSOphir Munk #include <rte_kvargs.h>
18391b8bccSOphir Munk #include <rte_devargs.h>
1972d7efe4SSpike Du #include <rte_interrupts.h>
20391b8bccSOphir Munk 
21391b8bccSOphir Munk #include "mlx5_autoconf.h"
229d60f545SOphir Munk #include "mlx5_glue.h"
237e7af4e9SOphir Munk #include "mlx5_malloc.h"
24391b8bccSOphir Munk 
25391b8bccSOphir Munk /**
26391b8bccSOphir Munk  * Get device name. Given an ibv_device pointer - return a
27391b8bccSOphir Munk  * pointer to the corresponding device name.
28391b8bccSOphir Munk  *
29391b8bccSOphir Munk  * @param[in] dev
30391b8bccSOphir Munk  *   Pointer to ibv device.
31391b8bccSOphir Munk  *
32391b8bccSOphir Munk  * @return
33391b8bccSOphir Munk  *   Pointer to device name if dev is valid, NULL otherwise.
34391b8bccSOphir Munk  */
35391b8bccSOphir Munk static inline const char *
mlx5_os_get_dev_device_name(void * dev)36391b8bccSOphir Munk mlx5_os_get_dev_device_name(void *dev)
37391b8bccSOphir Munk {
38391b8bccSOphir Munk 	if (!dev)
39391b8bccSOphir Munk 		return NULL;
40391b8bccSOphir Munk 	return ((struct ibv_device *)dev)->name;
41391b8bccSOphir Munk }
42391b8bccSOphir Munk 
43391b8bccSOphir Munk /**
44391b8bccSOphir Munk  * Get ibv device name. Given an ibv_context pointer - return a
45391b8bccSOphir Munk  * pointer to the corresponding device name.
46391b8bccSOphir Munk  *
47391b8bccSOphir Munk  * @param[in] ctx
48391b8bccSOphir Munk  *   Pointer to ibv context.
49391b8bccSOphir Munk  *
50391b8bccSOphir Munk  * @return
51391b8bccSOphir Munk  *   Pointer to device name if ctx is valid, NULL otherwise.
52391b8bccSOphir Munk  */
53391b8bccSOphir Munk static inline const char *
mlx5_os_get_ctx_device_name(void * ctx)54391b8bccSOphir Munk mlx5_os_get_ctx_device_name(void *ctx)
55391b8bccSOphir Munk {
56391b8bccSOphir Munk 	if (!ctx)
57391b8bccSOphir Munk 		return NULL;
58391b8bccSOphir Munk 	return ((struct ibv_context *)ctx)->device->name;
59391b8bccSOphir Munk }
60391b8bccSOphir Munk 
61391b8bccSOphir Munk /**
62391b8bccSOphir Munk  * Get ibv device path name. Given an ibv_context pointer - return a
63391b8bccSOphir Munk  * pointer to the corresponding device path name.
64391b8bccSOphir Munk  *
65391b8bccSOphir Munk  * @param[in] ctx
66391b8bccSOphir Munk  *   Pointer to ibv context.
67391b8bccSOphir Munk  *
68391b8bccSOphir Munk  * @return
69391b8bccSOphir Munk  *   Pointer to device path name if ctx is valid, NULL otherwise.
70391b8bccSOphir Munk  */
71391b8bccSOphir Munk 
72391b8bccSOphir Munk static inline const char *
mlx5_os_get_ctx_device_path(void * ctx)73391b8bccSOphir Munk mlx5_os_get_ctx_device_path(void *ctx)
74391b8bccSOphir Munk {
75391b8bccSOphir Munk 	if (!ctx)
76391b8bccSOphir Munk 		return NULL;
77391b8bccSOphir Munk 
78391b8bccSOphir Munk 	return ((struct ibv_context *)ctx)->device->ibdev_path;
79391b8bccSOphir Munk }
80391b8bccSOphir Munk 
81391b8bccSOphir Munk /**
82391b8bccSOphir Munk  * Get umem id. Given a pointer to umem object of type
83391b8bccSOphir Munk  * 'struct mlx5dv_devx_umem *' - return its id.
84391b8bccSOphir Munk  *
85391b8bccSOphir Munk  * @param[in] umem
86391b8bccSOphir Munk  *    Pointer to umem object.
87391b8bccSOphir Munk  *
88391b8bccSOphir Munk  * @return
89391b8bccSOphir Munk  *    The umem id if umem is valid, 0 otherwise.
90391b8bccSOphir Munk  */
91391b8bccSOphir Munk static inline uint32_t
mlx5_os_get_umem_id(void * umem)92391b8bccSOphir Munk mlx5_os_get_umem_id(void *umem)
93391b8bccSOphir Munk {
94391b8bccSOphir Munk 	if (!umem)
95391b8bccSOphir Munk 		return 0;
96391b8bccSOphir Munk 	return ((struct mlx5dv_devx_umem *)umem)->umem_id;
97391b8bccSOphir Munk }
981f66ac5bSOphir Munk 
991f66ac5bSOphir Munk /**
1001f66ac5bSOphir Munk  * Get fd. Given a pointer to DevX channel object of type
1011f66ac5bSOphir Munk  * 'struct mlx5dv_devx_event_channel*' - return its fd.
1021f66ac5bSOphir Munk  *
1031f66ac5bSOphir Munk  * @param[in] channel
1041f66ac5bSOphir Munk  *    Pointer to channel object.
1051f66ac5bSOphir Munk  *
1061f66ac5bSOphir Munk  * @return
1071f66ac5bSOphir Munk  *    The fd if channel is valid, 0 otherwise.
1081f66ac5bSOphir Munk  */
1091f66ac5bSOphir Munk static inline int
mlx5_os_get_devx_channel_fd(void * channel)1101f66ac5bSOphir Munk mlx5_os_get_devx_channel_fd(void *channel)
1111f66ac5bSOphir Munk {
1121f66ac5bSOphir Munk 	if (!channel)
1131f66ac5bSOphir Munk 		return 0;
1141f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_event_channel *)channel)->fd;
1151f66ac5bSOphir Munk }
1161f66ac5bSOphir Munk 
1171f66ac5bSOphir Munk /**
1181f66ac5bSOphir Munk  * Get mmap offset. Given a pointer to an DevX UAR object of type
1191f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its mmap offset.
1201f66ac5bSOphir Munk  *
1211f66ac5bSOphir Munk  * @param[in] uar
1221f66ac5bSOphir Munk  *    Pointer to UAR object.
1231f66ac5bSOphir Munk  *
1241f66ac5bSOphir Munk  * @return
1251f66ac5bSOphir Munk  *    The mmap offset if uar is valid, 0 otherwise.
1261f66ac5bSOphir Munk  */
1271f66ac5bSOphir Munk static inline off_t
mlx5_os_get_devx_uar_mmap_offset(void * uar)1281f66ac5bSOphir Munk mlx5_os_get_devx_uar_mmap_offset(void *uar)
1291f66ac5bSOphir Munk {
1301f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1311f66ac5bSOphir Munk 	if (!uar)
1321f66ac5bSOphir Munk 		return 0;
1331f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->mmap_off;
1341f66ac5bSOphir Munk #else
1351f66ac5bSOphir Munk 	RTE_SET_USED(uar);
1361f66ac5bSOphir Munk 	return 0;
1371f66ac5bSOphir Munk #endif
1381f66ac5bSOphir Munk }
1391f66ac5bSOphir Munk 
1401f66ac5bSOphir Munk /**
1411f66ac5bSOphir Munk  * Get base addr pointer. Given a pointer to an UAR object of type
1421f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its base address.
1431f66ac5bSOphir Munk  *
1441f66ac5bSOphir Munk  * @param[in] uar
1451f66ac5bSOphir Munk  *    Pointer to an UAR object.
1461f66ac5bSOphir Munk  *
1471f66ac5bSOphir Munk  * @return
1481f66ac5bSOphir Munk  *    The base address if UAR is valid, 0 otherwise.
1491f66ac5bSOphir Munk  */
1501f66ac5bSOphir Munk static inline void *
mlx5_os_get_devx_uar_base_addr(void * uar)1511f66ac5bSOphir Munk mlx5_os_get_devx_uar_base_addr(void *uar)
1521f66ac5bSOphir Munk {
1531f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1541f66ac5bSOphir Munk 	if (!uar)
1551f66ac5bSOphir Munk 		return NULL;
1561f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->base_addr;
1571f66ac5bSOphir Munk #else
1581f66ac5bSOphir Munk 	RTE_SET_USED(uar);
1591f66ac5bSOphir Munk 	return NULL;
1601f66ac5bSOphir Munk #endif
1611f66ac5bSOphir Munk }
1621f66ac5bSOphir Munk 
1631f66ac5bSOphir Munk /**
1641f66ac5bSOphir Munk  * Get reg addr pointer. Given a pointer to an UAR object of type
1651f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its reg address.
1661f66ac5bSOphir Munk  *
1671f66ac5bSOphir Munk  * @param[in] uar
1681f66ac5bSOphir Munk  *    Pointer to an UAR object.
1691f66ac5bSOphir Munk  *
1701f66ac5bSOphir Munk  * @return
1711f66ac5bSOphir Munk  *    The reg address if UAR is valid, 0 otherwise.
1721f66ac5bSOphir Munk  */
1731f66ac5bSOphir Munk static inline void *
mlx5_os_get_devx_uar_reg_addr(void * uar)1741f66ac5bSOphir Munk mlx5_os_get_devx_uar_reg_addr(void *uar)
1751f66ac5bSOphir Munk {
1761f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1771f66ac5bSOphir Munk 	if (!uar)
1781f66ac5bSOphir Munk 		return NULL;
1791f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->reg_addr;
1801f66ac5bSOphir Munk #else
1811f66ac5bSOphir Munk 	RTE_SET_USED(uar);
1821f66ac5bSOphir Munk 	return NULL;
1831f66ac5bSOphir Munk #endif
1841f66ac5bSOphir Munk }
1851f66ac5bSOphir Munk 
1861f66ac5bSOphir Munk /**
1871f66ac5bSOphir Munk  * Get page id. Given a pointer to an UAR object of type
1881f66ac5bSOphir Munk  * 'struct mlx5dv_devx_uar *' - return its page id.
1891f66ac5bSOphir Munk  *
1901f66ac5bSOphir Munk  * @param[in] uar
1911f66ac5bSOphir Munk  *    Pointer to an UAR object.
1921f66ac5bSOphir Munk  *
1931f66ac5bSOphir Munk  * @return
1941f66ac5bSOphir Munk  *    The page id if UAR is valid, 0 otherwise.
1951f66ac5bSOphir Munk  */
1961f66ac5bSOphir Munk static inline uint32_t
mlx5_os_get_devx_uar_page_id(void * uar)1971f66ac5bSOphir Munk mlx5_os_get_devx_uar_page_id(void *uar)
1981f66ac5bSOphir Munk {
1991f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
2001f66ac5bSOphir Munk 	if (!uar)
2011f66ac5bSOphir Munk 		return 0;
2021f66ac5bSOphir Munk 	return ((struct mlx5dv_devx_uar *)uar)->page_id;
2031f66ac5bSOphir Munk #else
2041f66ac5bSOphir Munk 	RTE_SET_USED(uar);
2051f66ac5bSOphir Munk 	return 0;
2061f66ac5bSOphir Munk #endif
2071f66ac5bSOphir Munk }
2081f66ac5bSOphir Munk 
2098ec36fb1STal Shnaiderman __rte_internal
21007a99de8STal Shnaiderman static inline void *
mlx5_os_umem_reg(void * ctx,void * addr,size_t size,uint32_t access)21107a99de8STal Shnaiderman mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access)
21207a99de8STal Shnaiderman {
21307a99de8STal Shnaiderman 	return mlx5_glue->devx_umem_reg(ctx, addr, size, access);
21407a99de8STal Shnaiderman }
21507a99de8STal Shnaiderman 
2168ec36fb1STal Shnaiderman __rte_internal
21707a99de8STal Shnaiderman static inline int
mlx5_os_umem_dereg(void * pumem)21807a99de8STal Shnaiderman mlx5_os_umem_dereg(void *pumem)
21907a99de8STal Shnaiderman {
22007a99de8STal Shnaiderman 	return mlx5_glue->devx_umem_dereg(pumem);
22107a99de8STal Shnaiderman }
2227e7af4e9SOphir Munk 
22398174626STal Shnaiderman static inline void *
mlx5_os_devx_create_event_channel(void * ctx,int flags)22498174626STal Shnaiderman mlx5_os_devx_create_event_channel(void *ctx, int flags)
22598174626STal Shnaiderman {
22698174626STal Shnaiderman 	return mlx5_glue->devx_create_event_channel(ctx, flags);
22798174626STal Shnaiderman }
22898174626STal Shnaiderman 
22998174626STal Shnaiderman static inline void
mlx5_os_devx_destroy_event_channel(void * eventc)23098174626STal Shnaiderman mlx5_os_devx_destroy_event_channel(void *eventc)
23198174626STal Shnaiderman {
23298174626STal Shnaiderman 	mlx5_glue->devx_destroy_event_channel(eventc);
23398174626STal Shnaiderman }
23498174626STal Shnaiderman 
23598174626STal Shnaiderman static inline int
mlx5_os_devx_subscribe_devx_event(void * eventc,void * obj,uint16_t events_sz,uint16_t events_num[],uint64_t cookie)23698174626STal Shnaiderman mlx5_os_devx_subscribe_devx_event(void *eventc,
23798174626STal Shnaiderman 				  void *obj,
23898174626STal Shnaiderman 				  uint16_t events_sz, uint16_t events_num[],
23998174626STal Shnaiderman 				  uint64_t cookie)
24098174626STal Shnaiderman {
24198174626STal Shnaiderman 	return mlx5_glue->devx_subscribe_devx_event(eventc, obj, events_sz,
24298174626STal Shnaiderman 						    events_num, cookie);
24398174626STal Shnaiderman }
24498174626STal Shnaiderman 
2457e7af4e9SOphir Munk /**
2467e7af4e9SOphir Munk  * Memory allocation optionally with alignment.
2477e7af4e9SOphir Munk  *
2487e7af4e9SOphir Munk  * @param[in] align
2497e7af4e9SOphir Munk  *    Alignment size (may be zero)
2507e7af4e9SOphir Munk  * @param[in] size
2517e7af4e9SOphir Munk  *    Size in bytes to allocate
2527e7af4e9SOphir Munk  *
2537e7af4e9SOphir Munk  * @return
2547e7af4e9SOphir Munk  *    Valid pointer to allocated memory, NULL in case of failure
2557e7af4e9SOphir Munk  */
2567e7af4e9SOphir Munk static inline void *
mlx5_os_malloc(size_t align,size_t size)2577e7af4e9SOphir Munk mlx5_os_malloc(size_t align, size_t size)
2587e7af4e9SOphir Munk {
2597e7af4e9SOphir Munk 	void *buf;
2607e7af4e9SOphir Munk 
2617e7af4e9SOphir Munk 	if (posix_memalign(&buf, align, size))
2627e7af4e9SOphir Munk 		return NULL;
2637e7af4e9SOphir Munk 	return buf;
2647e7af4e9SOphir Munk }
2657e7af4e9SOphir Munk 
2667e7af4e9SOphir Munk /**
2677e7af4e9SOphir Munk  * This API de-allocates a memory that originally could have been
2687e7af4e9SOphir Munk  * allocated aligned or non-aligned. In Linux it is a wrapper
2697e7af4e9SOphir Munk  * around free().
2707e7af4e9SOphir Munk  *
2717e7af4e9SOphir Munk  * @param[in] addr
2727e7af4e9SOphir Munk  *    Pointer to address to free
2737e7af4e9SOphir Munk  *
2747e7af4e9SOphir Munk  */
2757e7af4e9SOphir Munk static inline void
mlx5_os_free(void * addr)2767e7af4e9SOphir Munk mlx5_os_free(void *addr)
2777e7af4e9SOphir Munk {
2787e7af4e9SOphir Munk 	free(addr);
2797e7af4e9SOphir Munk }
280c31f3f7fSShiri Kuzin 
281887183efSMichael Baum void
282887183efSMichael Baum mlx5_set_context_attr(struct rte_device *dev, struct ibv_context *ctx);
283887183efSMichael Baum 
2844c74ad3eSRongwei Liu /**
2854c74ad3eSRongwei Liu  * This is used to query system_image_guid as describing in PRM.
2864c74ad3eSRongwei Liu  *
2874c74ad3eSRongwei Liu  * @param dev[in]
2884c74ad3eSRongwei Liu  *  Pointer to a device instance as PCIe id.
2894c74ad3eSRongwei Liu  * @param guid[out]
2904c74ad3eSRongwei Liu  *  Pointer to the buffer to hold device guid.
2914c74ad3eSRongwei Liu  *  Guid is uint64_t and corresponding to 17 bytes string.
2924c74ad3eSRongwei Liu  * @param len[in]
2934c74ad3eSRongwei Liu  *  Guid buffer length, 17 bytes at least.
2944c74ad3eSRongwei Liu  *
2954c74ad3eSRongwei Liu  * @return
2964c74ad3eSRongwei Liu  *  -1 if internal failure.
2974c74ad3eSRongwei Liu  *  0 if OFED doesn't support.
2984c74ad3eSRongwei Liu  *  >0 if success.
2994c74ad3eSRongwei Liu  */
300c2e3059aSMichael Baum __rte_internal
3014c74ad3eSRongwei Liu int
3024c74ad3eSRongwei Liu mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len);
3034c74ad3eSRongwei Liu 
30472d7efe4SSpike Du __rte_internal
30572d7efe4SSpike Du struct rte_intr_handle *
30672d7efe4SSpike Du mlx5_os_interrupt_handler_create(int mode, bool set_fd_nonblock, int fd,
30772d7efe4SSpike Du 				 rte_intr_callback_fn cb, void *cb_arg);
30872d7efe4SSpike Du 
30972d7efe4SSpike Du __rte_internal
31072d7efe4SSpike Du void
31172d7efe4SSpike Du mlx5_os_interrupt_handler_destroy(struct rte_intr_handle *intr_handle,
31272d7efe4SSpike Du 				  rte_intr_callback_fn cb, void *cb_arg);
31372d7efe4SSpike Du 
314391b8bccSOphir Munk #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */
315