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 11391b8bccSOphir Munk #include <rte_pci.h> 12391b8bccSOphir Munk #include <rte_debug.h> 13391b8bccSOphir Munk #include <rte_atomic.h> 14391b8bccSOphir Munk #include <rte_log.h> 15391b8bccSOphir Munk #include <rte_kvargs.h> 16391b8bccSOphir Munk #include <rte_devargs.h> 17391b8bccSOphir Munk 18391b8bccSOphir Munk #include "mlx5_autoconf.h" 199d60f545SOphir Munk #include "mlx5_glue.h" 207e7af4e9SOphir Munk #include "mlx5_malloc.h" 21391b8bccSOphir Munk 22391b8bccSOphir Munk /** 23391b8bccSOphir Munk * Get device name. Given an ibv_device pointer - return a 24391b8bccSOphir Munk * pointer to the corresponding device name. 25391b8bccSOphir Munk * 26391b8bccSOphir Munk * @param[in] dev 27391b8bccSOphir Munk * Pointer to ibv device. 28391b8bccSOphir Munk * 29391b8bccSOphir Munk * @return 30391b8bccSOphir Munk * Pointer to device name if dev is valid, NULL otherwise. 31391b8bccSOphir Munk */ 32391b8bccSOphir Munk static inline const char * 33391b8bccSOphir Munk mlx5_os_get_dev_device_name(void *dev) 34391b8bccSOphir Munk { 35391b8bccSOphir Munk if (!dev) 36391b8bccSOphir Munk return NULL; 37391b8bccSOphir Munk return ((struct ibv_device *)dev)->name; 38391b8bccSOphir Munk } 39391b8bccSOphir Munk 40391b8bccSOphir Munk /** 41391b8bccSOphir Munk * Get ibv device name. Given an ibv_context pointer - return a 42391b8bccSOphir Munk * pointer to the corresponding device name. 43391b8bccSOphir Munk * 44391b8bccSOphir Munk * @param[in] ctx 45391b8bccSOphir Munk * Pointer to ibv context. 46391b8bccSOphir Munk * 47391b8bccSOphir Munk * @return 48391b8bccSOphir Munk * Pointer to device name if ctx is valid, NULL otherwise. 49391b8bccSOphir Munk */ 50391b8bccSOphir Munk static inline const char * 51391b8bccSOphir Munk mlx5_os_get_ctx_device_name(void *ctx) 52391b8bccSOphir Munk { 53391b8bccSOphir Munk if (!ctx) 54391b8bccSOphir Munk return NULL; 55391b8bccSOphir Munk return ((struct ibv_context *)ctx)->device->name; 56391b8bccSOphir Munk } 57391b8bccSOphir Munk 58391b8bccSOphir Munk /** 59391b8bccSOphir Munk * Get ibv device path name. Given an ibv_context pointer - return a 60391b8bccSOphir Munk * pointer to the corresponding device path name. 61391b8bccSOphir Munk * 62391b8bccSOphir Munk * @param[in] ctx 63391b8bccSOphir Munk * Pointer to ibv context. 64391b8bccSOphir Munk * 65391b8bccSOphir Munk * @return 66391b8bccSOphir Munk * Pointer to device path name if ctx is valid, NULL otherwise. 67391b8bccSOphir Munk */ 68391b8bccSOphir Munk 69391b8bccSOphir Munk static inline const char * 70391b8bccSOphir Munk mlx5_os_get_ctx_device_path(void *ctx) 71391b8bccSOphir Munk { 72391b8bccSOphir Munk if (!ctx) 73391b8bccSOphir Munk return NULL; 74391b8bccSOphir Munk 75391b8bccSOphir Munk return ((struct ibv_context *)ctx)->device->ibdev_path; 76391b8bccSOphir Munk } 77391b8bccSOphir Munk 78391b8bccSOphir Munk /** 79391b8bccSOphir Munk * Get umem id. Given a pointer to umem object of type 80391b8bccSOphir Munk * 'struct mlx5dv_devx_umem *' - return its id. 81391b8bccSOphir Munk * 82391b8bccSOphir Munk * @param[in] umem 83391b8bccSOphir Munk * Pointer to umem object. 84391b8bccSOphir Munk * 85391b8bccSOphir Munk * @return 86391b8bccSOphir Munk * The umem id if umem is valid, 0 otherwise. 87391b8bccSOphir Munk */ 88391b8bccSOphir Munk static inline uint32_t 89391b8bccSOphir Munk mlx5_os_get_umem_id(void *umem) 90391b8bccSOphir Munk { 91391b8bccSOphir Munk if (!umem) 92391b8bccSOphir Munk return 0; 93391b8bccSOphir Munk return ((struct mlx5dv_devx_umem *)umem)->umem_id; 94391b8bccSOphir Munk } 951f66ac5bSOphir Munk 961f66ac5bSOphir Munk /** 971f66ac5bSOphir Munk * Get fd. Given a pointer to DevX channel object of type 981f66ac5bSOphir Munk * 'struct mlx5dv_devx_event_channel*' - return its fd. 991f66ac5bSOphir Munk * 1001f66ac5bSOphir Munk * @param[in] channel 1011f66ac5bSOphir Munk * Pointer to channel object. 1021f66ac5bSOphir Munk * 1031f66ac5bSOphir Munk * @return 1041f66ac5bSOphir Munk * The fd if channel is valid, 0 otherwise. 1051f66ac5bSOphir Munk */ 1061f66ac5bSOphir Munk static inline int 1071f66ac5bSOphir Munk mlx5_os_get_devx_channel_fd(void *channel) 1081f66ac5bSOphir Munk { 1091f66ac5bSOphir Munk if (!channel) 1101f66ac5bSOphir Munk return 0; 1111f66ac5bSOphir Munk return ((struct mlx5dv_devx_event_channel *)channel)->fd; 1121f66ac5bSOphir Munk } 1131f66ac5bSOphir Munk 1141f66ac5bSOphir Munk /** 1151f66ac5bSOphir Munk * Get mmap offset. Given a pointer to an DevX UAR object of type 1161f66ac5bSOphir Munk * 'struct mlx5dv_devx_uar *' - return its mmap offset. 1171f66ac5bSOphir Munk * 1181f66ac5bSOphir Munk * @param[in] uar 1191f66ac5bSOphir Munk * Pointer to UAR object. 1201f66ac5bSOphir Munk * 1211f66ac5bSOphir Munk * @return 1221f66ac5bSOphir Munk * The mmap offset if uar is valid, 0 otherwise. 1231f66ac5bSOphir Munk */ 1241f66ac5bSOphir Munk static inline off_t 1251f66ac5bSOphir Munk mlx5_os_get_devx_uar_mmap_offset(void *uar) 1261f66ac5bSOphir Munk { 1271f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET 1281f66ac5bSOphir Munk if (!uar) 1291f66ac5bSOphir Munk return 0; 1301f66ac5bSOphir Munk return ((struct mlx5dv_devx_uar *)uar)->mmap_off; 1311f66ac5bSOphir Munk #else 1321f66ac5bSOphir Munk RTE_SET_USED(uar); 1331f66ac5bSOphir Munk return 0; 1341f66ac5bSOphir Munk #endif 1351f66ac5bSOphir Munk } 1361f66ac5bSOphir Munk 1371f66ac5bSOphir Munk /** 1381f66ac5bSOphir Munk * Get base addr pointer. Given a pointer to an UAR object of type 1391f66ac5bSOphir Munk * 'struct mlx5dv_devx_uar *' - return its base address. 1401f66ac5bSOphir Munk * 1411f66ac5bSOphir Munk * @param[in] uar 1421f66ac5bSOphir Munk * Pointer to an UAR object. 1431f66ac5bSOphir Munk * 1441f66ac5bSOphir Munk * @return 1451f66ac5bSOphir Munk * The base address if UAR is valid, 0 otherwise. 1461f66ac5bSOphir Munk */ 1471f66ac5bSOphir Munk static inline void * 1481f66ac5bSOphir Munk mlx5_os_get_devx_uar_base_addr(void *uar) 1491f66ac5bSOphir Munk { 1501f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET 1511f66ac5bSOphir Munk if (!uar) 1521f66ac5bSOphir Munk return NULL; 1531f66ac5bSOphir Munk return ((struct mlx5dv_devx_uar *)uar)->base_addr; 1541f66ac5bSOphir Munk #else 1551f66ac5bSOphir Munk RTE_SET_USED(uar); 1561f66ac5bSOphir Munk return NULL; 1571f66ac5bSOphir Munk #endif 1581f66ac5bSOphir Munk } 1591f66ac5bSOphir Munk 1601f66ac5bSOphir Munk /** 1611f66ac5bSOphir Munk * Get reg addr pointer. Given a pointer to an UAR object of type 1621f66ac5bSOphir Munk * 'struct mlx5dv_devx_uar *' - return its reg address. 1631f66ac5bSOphir Munk * 1641f66ac5bSOphir Munk * @param[in] uar 1651f66ac5bSOphir Munk * Pointer to an UAR object. 1661f66ac5bSOphir Munk * 1671f66ac5bSOphir Munk * @return 1681f66ac5bSOphir Munk * The reg address if UAR is valid, 0 otherwise. 1691f66ac5bSOphir Munk */ 1701f66ac5bSOphir Munk static inline void * 1711f66ac5bSOphir Munk mlx5_os_get_devx_uar_reg_addr(void *uar) 1721f66ac5bSOphir Munk { 1731f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET 1741f66ac5bSOphir Munk if (!uar) 1751f66ac5bSOphir Munk return NULL; 1761f66ac5bSOphir Munk return ((struct mlx5dv_devx_uar *)uar)->reg_addr; 1771f66ac5bSOphir Munk #else 1781f66ac5bSOphir Munk RTE_SET_USED(uar); 1791f66ac5bSOphir Munk return NULL; 1801f66ac5bSOphir Munk #endif 1811f66ac5bSOphir Munk } 1821f66ac5bSOphir Munk 1831f66ac5bSOphir Munk /** 1841f66ac5bSOphir Munk * Get page id. Given a pointer to an UAR object of type 1851f66ac5bSOphir Munk * 'struct mlx5dv_devx_uar *' - return its page id. 1861f66ac5bSOphir Munk * 1871f66ac5bSOphir Munk * @param[in] uar 1881f66ac5bSOphir Munk * Pointer to an UAR object. 1891f66ac5bSOphir Munk * 1901f66ac5bSOphir Munk * @return 1911f66ac5bSOphir Munk * The page id if UAR is valid, 0 otherwise. 1921f66ac5bSOphir Munk */ 1931f66ac5bSOphir Munk static inline uint32_t 1941f66ac5bSOphir Munk mlx5_os_get_devx_uar_page_id(void *uar) 1951f66ac5bSOphir Munk { 1961f66ac5bSOphir Munk #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET 1971f66ac5bSOphir Munk if (!uar) 1981f66ac5bSOphir Munk return 0; 1991f66ac5bSOphir Munk return ((struct mlx5dv_devx_uar *)uar)->page_id; 2001f66ac5bSOphir Munk #else 2011f66ac5bSOphir Munk RTE_SET_USED(uar); 2021f66ac5bSOphir Munk return 0; 2031f66ac5bSOphir Munk #endif 2041f66ac5bSOphir Munk } 2051f66ac5bSOphir Munk 2061cb210abSOphir Munk static inline int 2071cb210abSOphir Munk mlx5_os_dealloc_pd(void *pd) 2081cb210abSOphir Munk { 2091cb210abSOphir Munk return mlx5_glue->dealloc_pd(pd); 2101cb210abSOphir Munk } 21107a99de8STal Shnaiderman 2128ec36fb1STal Shnaiderman __rte_internal 21307a99de8STal Shnaiderman static inline void * 21407a99de8STal Shnaiderman mlx5_os_umem_reg(void *ctx, void *addr, size_t size, uint32_t access) 21507a99de8STal Shnaiderman { 21607a99de8STal Shnaiderman return mlx5_glue->devx_umem_reg(ctx, addr, size, access); 21707a99de8STal Shnaiderman } 21807a99de8STal Shnaiderman 2198ec36fb1STal Shnaiderman __rte_internal 22007a99de8STal Shnaiderman static inline int 22107a99de8STal Shnaiderman mlx5_os_umem_dereg(void *pumem) 22207a99de8STal Shnaiderman { 22307a99de8STal Shnaiderman return mlx5_glue->devx_umem_dereg(pumem); 22407a99de8STal Shnaiderman } 2257e7af4e9SOphir Munk 22698174626STal Shnaiderman static inline void * 22798174626STal Shnaiderman mlx5_os_devx_create_event_channel(void *ctx, int flags) 22898174626STal Shnaiderman { 22998174626STal Shnaiderman return mlx5_glue->devx_create_event_channel(ctx, flags); 23098174626STal Shnaiderman } 23198174626STal Shnaiderman 23298174626STal Shnaiderman static inline void 23398174626STal Shnaiderman mlx5_os_devx_destroy_event_channel(void *eventc) 23498174626STal Shnaiderman { 23598174626STal Shnaiderman mlx5_glue->devx_destroy_event_channel(eventc); 23698174626STal Shnaiderman } 23798174626STal Shnaiderman 23898174626STal Shnaiderman static inline int 23998174626STal Shnaiderman mlx5_os_devx_subscribe_devx_event(void *eventc, 24098174626STal Shnaiderman void *obj, 24198174626STal Shnaiderman uint16_t events_sz, uint16_t events_num[], 24298174626STal Shnaiderman uint64_t cookie) 24398174626STal Shnaiderman { 24498174626STal Shnaiderman return mlx5_glue->devx_subscribe_devx_event(eventc, obj, events_sz, 24598174626STal Shnaiderman events_num, cookie); 24698174626STal Shnaiderman } 24798174626STal Shnaiderman 2487e7af4e9SOphir Munk /** 2497e7af4e9SOphir Munk * Memory allocation optionally with alignment. 2507e7af4e9SOphir Munk * 2517e7af4e9SOphir Munk * @param[in] align 2527e7af4e9SOphir Munk * Alignment size (may be zero) 2537e7af4e9SOphir Munk * @param[in] size 2547e7af4e9SOphir Munk * Size in bytes to allocate 2557e7af4e9SOphir Munk * 2567e7af4e9SOphir Munk * @return 2577e7af4e9SOphir Munk * Valid pointer to allocated memory, NULL in case of failure 2587e7af4e9SOphir Munk */ 2597e7af4e9SOphir Munk static inline void * 2607e7af4e9SOphir Munk mlx5_os_malloc(size_t align, size_t size) 2617e7af4e9SOphir Munk { 2627e7af4e9SOphir Munk void *buf; 2637e7af4e9SOphir Munk 2647e7af4e9SOphir Munk if (posix_memalign(&buf, align, size)) 2657e7af4e9SOphir Munk return NULL; 2667e7af4e9SOphir Munk return buf; 2677e7af4e9SOphir Munk } 2687e7af4e9SOphir Munk 2697e7af4e9SOphir Munk /** 2707e7af4e9SOphir Munk * This API de-allocates a memory that originally could have been 2717e7af4e9SOphir Munk * allocated aligned or non-aligned. In Linux it is a wrapper 2727e7af4e9SOphir Munk * around free(). 2737e7af4e9SOphir Munk * 2747e7af4e9SOphir Munk * @param[in] addr 2757e7af4e9SOphir Munk * Pointer to address to free 2767e7af4e9SOphir Munk * 2777e7af4e9SOphir Munk */ 2787e7af4e9SOphir Munk static inline void 2797e7af4e9SOphir Munk mlx5_os_free(void *addr) 2807e7af4e9SOphir Munk { 2817e7af4e9SOphir Munk free(addr); 2827e7af4e9SOphir Munk } 283c31f3f7fSShiri Kuzin 284887183efSMichael Baum void 285887183efSMichael Baum mlx5_set_context_attr(struct rte_device *dev, struct ibv_context *ctx); 286887183efSMichael Baum 287*4c74ad3eSRongwei Liu /** 288*4c74ad3eSRongwei Liu * This is used to query system_image_guid as describing in PRM. 289*4c74ad3eSRongwei Liu * 290*4c74ad3eSRongwei Liu * @param dev[in] 291*4c74ad3eSRongwei Liu * Pointer to a device instance as PCIe id. 292*4c74ad3eSRongwei Liu * @param guid[out] 293*4c74ad3eSRongwei Liu * Pointer to the buffer to hold device guid. 294*4c74ad3eSRongwei Liu * Guid is uint64_t and corresponding to 17 bytes string. 295*4c74ad3eSRongwei Liu * @param len[in] 296*4c74ad3eSRongwei Liu * Guid buffer length, 17 bytes at least. 297*4c74ad3eSRongwei Liu * 298*4c74ad3eSRongwei Liu * @return 299*4c74ad3eSRongwei Liu * -1 if internal failure. 300*4c74ad3eSRongwei Liu * 0 if OFED doesn't support. 301*4c74ad3eSRongwei Liu * >0 if success. 302*4c74ad3eSRongwei Liu */ 303*4c74ad3eSRongwei Liu int 304*4c74ad3eSRongwei Liu mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len); 305*4c74ad3eSRongwei Liu 306391b8bccSOphir Munk #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */ 307