2 * Copyright (c) 2016 QLogic Corporation.
6 * See LICENSE.qede_pmd for copyright and licensing details.
11 #include <rte_memzone.h>
12 #include <rte_errno.h>
18 unsigned long qede_log2_align(unsigned long n)
20 unsigned long ret = n ? 1 : 0;
21 unsigned long _n = n >> 1;
34 u32 qede_osal_log2(u32 val)
44 inline void qede_set_bit(u32 nr, unsigned long *addr)
46 __sync_fetch_and_or(addr, (1UL << nr));
49 inline void qede_clr_bit(u32 nr, unsigned long *addr)
51 __sync_fetch_and_and(addr, ~(1UL << nr));
54 inline bool qede_test_bit(u32 nr, unsigned long *addr)
59 res = ((*addr) & (1UL << nr)) != 0;
64 static inline u32 qede_ffz(unsigned long word)
66 unsigned long first_zero;
68 first_zero = __builtin_ffsl(~word);
69 return first_zero ? (first_zero - 1) : OSAL_BITS_PER_UL;
72 inline u32 qede_find_first_zero_bit(unsigned long *addr, u32 limit)
76 OSAL_BUILD_BUG_ON(!limit);
77 nwords = (limit - 1) / OSAL_BITS_PER_UL + 1;
78 for (i = 0; i < nwords; i++)
81 return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffz(addr[i]);
84 void *osal_dma_alloc_coherent(struct ecore_dev *p_dev,
85 dma_addr_t *phys, size_t size)
87 const struct rte_memzone *mz;
88 char mz_name[RTE_MEMZONE_NAMESIZE];
89 uint32_t core_id = rte_lcore_id();
90 unsigned int socket_id;
92 OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
93 snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
94 (unsigned long)rte_get_timer_cycles());
95 if (core_id == (unsigned int)LCORE_ID_ANY)
97 socket_id = rte_lcore_to_socket_id(core_id);
98 mz = rte_memzone_reserve_aligned(mz_name, size,
99 socket_id, 0, RTE_CACHE_LINE_SIZE);
101 DP_ERR(p_dev, "Unable to allocate DMA memory "
102 "of size %zu bytes - %s\n",
103 size, rte_strerror(rte_errno));
107 *phys = mz->phys_addr;
108 DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
109 "size=%zu phys=0x%lx virt=%p on socket=%u\n",
110 mz->len, mz->phys_addr, mz->addr, socket_id);
114 void *osal_dma_alloc_coherent_aligned(struct ecore_dev *p_dev,
115 dma_addr_t *phys, size_t size, int align)
117 const struct rte_memzone *mz;
118 char mz_name[RTE_MEMZONE_NAMESIZE];
119 uint32_t core_id = rte_lcore_id();
120 unsigned int socket_id;
122 OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
123 snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
124 (unsigned long)rte_get_timer_cycles());
125 if (core_id == (unsigned int)LCORE_ID_ANY)
127 socket_id = rte_lcore_to_socket_id(core_id);
128 mz = rte_memzone_reserve_aligned(mz_name, size, socket_id, 0, align);
130 DP_ERR(p_dev, "Unable to allocate DMA memory "
131 "of size %zu bytes - %s\n",
132 size, rte_strerror(rte_errno));
136 *phys = mz->phys_addr;
137 DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
138 "aligned memory size=%zu phys=0x%lx virt=%p core=%d\n",
139 mz->len, mz->phys_addr, mz->addr, core_id);
143 u32 qede_unzip_data(struct ecore_hwfn *p_hwfn, u32 input_len,
144 u8 *input_buf, u32 max_size, u8 *unzip_buf)
148 p_hwfn->stream->next_in = input_buf;
149 p_hwfn->stream->avail_in = input_len;
150 p_hwfn->stream->next_out = unzip_buf;
151 p_hwfn->stream->avail_out = max_size;
153 rc = inflateInit2(p_hwfn->stream, MAX_WBITS);
157 "zlib init failed, rc = %d\n", rc);
161 rc = inflate(p_hwfn->stream, Z_FINISH);
162 inflateEnd(p_hwfn->stream);
164 if (rc != Z_OK && rc != Z_STREAM_END) {
166 "FW unzip error: %s, rc=%d\n", p_hwfn->stream->msg,
171 return p_hwfn->stream->total_out / 4;