Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(C) 2022 Intel Corporation 3 : : */ 4 : : 5 : : #ifndef _GVE_OSDEP_H_ 6 : : #define _GVE_OSDEP_H_ 7 : : 8 : : #include <string.h> 9 : : #include <stdint.h> 10 : : #include <stdio.h> 11 : : #include <stdarg.h> 12 : : #include <inttypes.h> 13 : : #include <stdbool.h> 14 : : 15 : : #include <rte_bitops.h> 16 : : #include <rte_byteorder.h> 17 : : #include <rte_common.h> 18 : : #include <rte_ether.h> 19 : : #include <rte_io.h> 20 : : #include <rte_log.h> 21 : : #include <rte_malloc.h> 22 : : #include <rte_memcpy.h> 23 : : #include <rte_memzone.h> 24 : : #include <rte_version.h> 25 : : 26 : : #include "../gve_logs.h" 27 : : 28 : : #ifdef RTE_EXEC_ENV_LINUX 29 : : #include <sys/utsname.h> 30 : : #endif 31 : : 32 : : typedef uint8_t u8; 33 : : typedef uint16_t u16; 34 : : typedef uint32_t u32; 35 : : typedef uint64_t u64; 36 : : 37 : : typedef rte_be16_t __sum16; 38 : : 39 : : typedef rte_be16_t __be16; 40 : : typedef rte_be32_t __be32; 41 : : typedef rte_be64_t __be64; 42 : : 43 : : typedef rte_le16_t __le16; 44 : : typedef rte_le32_t __le32; 45 : : typedef rte_le64_t __le64; 46 : : 47 : : typedef rte_iova_t dma_addr_t; 48 : : 49 : : #define ETH_MIN_MTU RTE_ETHER_MIN_MTU 50 : : #define ETH_ALEN RTE_ETHER_ADDR_LEN 51 : : 52 : : #ifndef PAGE_SHIFT 53 : : #define PAGE_SHIFT 12 54 : : #endif 55 : : #ifndef PAGE_SIZE 56 : : #define PAGE_SIZE (1UL << PAGE_SHIFT) 57 : : #endif 58 : : 59 : : #define BIT(nr) RTE_BIT32(nr) 60 : : 61 : : #define be16_to_cpu(x) rte_be_to_cpu_16(x) 62 : : #define be32_to_cpu(x) rte_be_to_cpu_32(x) 63 : : #define be64_to_cpu(x) rte_be_to_cpu_64(x) 64 : : 65 : : #define cpu_to_be16(x) rte_cpu_to_be_16(x) 66 : : #define cpu_to_be32(x) rte_cpu_to_be_32(x) 67 : : #define cpu_to_be64(x) rte_cpu_to_be_64(x) 68 : : 69 : : #define READ_ONCE32(x) rte_read32(&(x)) 70 : : 71 : : #ifndef ____cacheline_aligned 72 : : #define ____cacheline_aligned __rte_cache_aligned 73 : : #endif 74 : : #ifndef __packed 75 : : #define __packed __rte_packed 76 : : #endif 77 : : #define __iomem 78 : : 79 : : #define msleep(ms) rte_delay_ms(ms) 80 : : 81 : : #define OS_VERSION_STRLEN 128 82 : : struct os_version_string { 83 : : char os_version_str1[OS_VERSION_STRLEN]; 84 : : char os_version_str2[OS_VERSION_STRLEN]; 85 : : }; 86 : : 87 : : /* These macros are used to generate compilation errors if a struct/union 88 : : * is not exactly the correct length. It gives a divide by zero error if 89 : : * the struct/union is not of the correct size, otherwise it creates an 90 : : * enum that is never used. 91 : : */ 92 : : #define GVE_CHECK_STRUCT_LEN(n, X) enum gve_static_assert_enum_##X \ 93 : : { gve_static_assert_##X = (n) / ((sizeof(struct X) == (n)) ? 1 : 0) } 94 : : #define GVE_CHECK_UNION_LEN(n, X) enum gve_static_asset_enum_##X \ 95 : : { gve_static_assert_##X = (n) / ((sizeof(union X) == (n)) ? 1 : 0) } 96 : : 97 : : static __rte_always_inline u8 98 : : readb(volatile void *addr) 99 : : { 100 : : return rte_read8(addr); 101 : : } 102 : : 103 : : static __rte_always_inline void 104 : : writeb(u8 value, volatile void *addr) 105 : : { 106 : : rte_write8(value, addr); 107 : : } 108 : : 109 : : static __rte_always_inline void 110 : : writel(u32 value, volatile void *addr) 111 : : { 112 : : rte_write32(value, addr); 113 : : } 114 : : 115 : : static __rte_always_inline u32 116 : : ioread32be(const volatile void *addr) 117 : : { 118 : : return rte_be_to_cpu_32(rte_read32(addr)); 119 : : } 120 : : 121 : : static __rte_always_inline void 122 : : iowrite32be(u32 value, volatile void *addr) 123 : : { 124 [ # # # # ]: 0 : writel(rte_cpu_to_be_32(value), addr); 125 : 0 : } 126 : : 127 : : /* DMA memory allocation tracking */ 128 : : struct gve_dma_mem { 129 : : void *va; 130 : : rte_iova_t pa; 131 : : uint32_t size; 132 : : const void *zone; 133 : : }; 134 : : 135 : : static inline void * 136 : 0 : gve_alloc_dma_mem(struct gve_dma_mem *mem, u64 size) 137 : : { 138 : : static uint16_t gve_dma_memzone_id; 139 : : const struct rte_memzone *mz = NULL; 140 : : char z_name[RTE_MEMZONE_NAMESIZE]; 141 : : 142 [ # # ]: 0 : if (!mem) 143 : : return NULL; 144 : : 145 : 0 : snprintf(z_name, sizeof(z_name), "gve_dma_%u", 146 : 0 : __atomic_fetch_add(&gve_dma_memzone_id, 1, __ATOMIC_RELAXED)); 147 : 0 : mz = rte_memzone_reserve_aligned(z_name, size, SOCKET_ID_ANY, 148 : : RTE_MEMZONE_IOVA_CONTIG, 149 : : PAGE_SIZE); 150 [ # # ]: 0 : if (!mz) 151 : : return NULL; 152 : : 153 : 0 : mem->size = size; 154 : 0 : mem->va = mz->addr; 155 : 0 : mem->pa = mz->iova; 156 : 0 : mem->zone = mz; 157 : 0 : PMD_DRV_LOG(DEBUG, "memzone %s is allocated", mz->name); 158 : : 159 : 0 : return mem->va; 160 : : } 161 : : 162 : : static inline void 163 : 0 : gve_free_dma_mem(struct gve_dma_mem *mem) 164 : : { 165 : 0 : PMD_DRV_LOG(DEBUG, "memzone %s to be freed", 166 : : ((const struct rte_memzone *)mem->zone)->name); 167 : : 168 : 0 : rte_memzone_free(mem->zone); 169 : 0 : mem->zone = NULL; 170 : 0 : mem->va = NULL; 171 : 0 : mem->pa = 0; 172 : 0 : } 173 : : 174 : : static inline void 175 : 0 : populate_driver_version_strings(char *str1, char *str2) 176 : : { 177 : : struct utsname uts; 178 [ # # ]: 0 : if (uname(&uts) >= 0) { 179 : : /* release */ 180 : 0 : rte_strscpy(str1, uts.release, 181 : : OS_VERSION_STRLEN); 182 : : /* version */ 183 : 0 : rte_strscpy(str2, uts.version, 184 : : OS_VERSION_STRLEN); 185 : : } 186 : 0 : } 187 : : #endif /* _GVE_OSDEP_H_ */