1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2016 - 2018 Cavium Inc.
7 #include <rte_memzone.h>
13 #include "ecore_dev_api.h"
14 #include "ecore_iov_api.h"
15 #include "ecore_mcp_api.h"
16 #include "ecore_l2_api.h"
17 #include "../qede_sriov.h"
19 int osal_pf_vf_msg(struct ecore_hwfn *p_hwfn)
23 rc = qed_schedule_iov(p_hwfn, QED_IOV_WQ_MSG_FLAG);
25 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
26 "Failed to schedule alarm handler rc=%d\n", rc);
32 void osal_vf_flr_update(struct ecore_hwfn *p_hwfn)
34 qed_schedule_iov(p_hwfn, QED_IOV_WQ_FLR_FLAG);
37 void osal_poll_mode_dpc(osal_int_ptr_t hwfn_cookie)
39 struct ecore_hwfn *p_hwfn = (struct ecore_hwfn *)hwfn_cookie;
44 OSAL_SPIN_LOCK(&p_hwfn->spq_lock);
45 ecore_int_sp_dpc((osal_int_ptr_t)(p_hwfn));
46 OSAL_SPIN_UNLOCK(&p_hwfn->spq_lock);
49 /* Array of memzone pointers */
50 static const struct rte_memzone *ecore_mz_mapping[RTE_MAX_MEMZONE];
51 /* Counter to track current memzone allocated */
52 static uint16_t ecore_mz_count;
54 unsigned long qede_log2_align(unsigned long n)
56 unsigned long ret = n ? 1 : 0;
57 unsigned long _n = n >> 1;
70 u32 qede_osal_log2(u32 val)
80 static inline u32 qede_ffb(unsigned long word)
82 unsigned long first_bit;
84 first_bit = __builtin_ffsl(word);
85 return first_bit ? (first_bit - 1) : OSAL_BITS_PER_UL;
88 inline u32 qede_find_first_bit(unsigned long *addr, u32 limit)
92 OSAL_BUILD_BUG_ON(!limit);
93 nwords = (limit - 1) / OSAL_BITS_PER_UL + 1;
94 for (i = 0; i < nwords; i++)
98 return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffb(addr[i]);
101 static inline u32 qede_ffz(unsigned long word)
103 unsigned long first_zero;
105 first_zero = __builtin_ffsl(~word);
106 return first_zero ? (first_zero - 1) : OSAL_BITS_PER_UL;
109 inline u32 qede_find_first_zero_bit(u32 *addr, u32 limit)
113 OSAL_BUILD_BUG_ON(!limit);
114 nwords = (limit - 1) / OSAL_BITS_PER_UL + 1;
115 for (i = 0; i < nwords && ~(addr[i]) == 0; i++);
116 return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffz(addr[i]);
119 void qede_vf_fill_driver_data(struct ecore_hwfn *hwfn,
120 __rte_unused struct vf_pf_resc_request *resc_req,
121 struct ecore_vf_acquire_sw_info *vf_sw_info)
123 vf_sw_info->os_type = VFPF_ACQUIRE_OS_LINUX_USERSPACE;
124 vf_sw_info->override_fw_version = 1;
127 void *osal_dma_alloc_coherent(struct ecore_dev *p_dev,
128 dma_addr_t *phys, size_t size)
130 const struct rte_memzone *mz;
131 char mz_name[RTE_MEMZONE_NAMESIZE];
132 uint32_t core_id = rte_lcore_id();
133 unsigned int socket_id;
135 if (ecore_mz_count >= RTE_MAX_MEMZONE) {
136 DP_ERR(p_dev, "Memzone allocation count exceeds %u\n",
142 OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
143 snprintf(mz_name, sizeof(mz_name), "%lx",
144 (unsigned long)rte_get_timer_cycles());
145 if (core_id == (unsigned int)LCORE_ID_ANY)
146 core_id = rte_get_main_lcore();
147 socket_id = rte_lcore_to_socket_id(core_id);
148 mz = rte_memzone_reserve_aligned(mz_name, size, socket_id,
149 RTE_MEMZONE_IOVA_CONTIG, RTE_CACHE_LINE_SIZE);
151 DP_ERR(p_dev, "Unable to allocate DMA memory "
152 "of size %zu bytes - %s\n",
153 size, rte_strerror(rte_errno));
158 ecore_mz_mapping[ecore_mz_count++] = mz;
159 DP_VERBOSE(p_dev, ECORE_MSG_SP,
160 "Allocated dma memory size=%zu phys=0x%lx"
161 " virt=%p core=%d\n",
162 mz->len, (unsigned long)mz->iova, mz->addr, core_id);
166 void *osal_dma_alloc_coherent_aligned(struct ecore_dev *p_dev,
167 dma_addr_t *phys, size_t size, int align)
169 const struct rte_memzone *mz;
170 char mz_name[RTE_MEMZONE_NAMESIZE];
171 uint32_t core_id = rte_lcore_id();
172 unsigned int socket_id;
174 if (ecore_mz_count >= RTE_MAX_MEMZONE) {
175 DP_ERR(p_dev, "Memzone allocation count exceeds %u\n",
181 OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
182 snprintf(mz_name, sizeof(mz_name), "%lx",
183 (unsigned long)rte_get_timer_cycles());
184 if (core_id == (unsigned int)LCORE_ID_ANY)
185 core_id = rte_get_main_lcore();
186 socket_id = rte_lcore_to_socket_id(core_id);
187 mz = rte_memzone_reserve_aligned(mz_name, size, socket_id,
188 RTE_MEMZONE_IOVA_CONTIG, align);
190 DP_ERR(p_dev, "Unable to allocate DMA memory "
191 "of size %zu bytes - %s\n",
192 size, rte_strerror(rte_errno));
197 ecore_mz_mapping[ecore_mz_count++] = mz;
198 DP_VERBOSE(p_dev, ECORE_MSG_SP,
199 "Allocated aligned dma memory size=%zu phys=0x%lx"
200 " virt=%p core=%d\n",
201 mz->len, (unsigned long)mz->iova, mz->addr, core_id);
205 void osal_dma_free_mem(struct ecore_dev *p_dev, dma_addr_t phys)
209 for (j = 0 ; j < ecore_mz_count; j++) {
210 if (phys == ecore_mz_mapping[j]->iova) {
211 DP_VERBOSE(p_dev, ECORE_MSG_SP,
212 "Free memzone %s\n", ecore_mz_mapping[j]->name);
213 rte_memzone_free(ecore_mz_mapping[j]);
214 while (j < ecore_mz_count - 1) {
215 ecore_mz_mapping[j] = ecore_mz_mapping[j + 1];
223 DP_ERR(p_dev, "Unexpected memory free request\n");
226 #ifdef CONFIG_ECORE_ZIPPED_FW
227 u32 qede_unzip_data(struct ecore_hwfn *p_hwfn, u32 input_len,
228 u8 *input_buf, u32 max_size, u8 *unzip_buf)
232 p_hwfn->stream->next_in = input_buf;
233 p_hwfn->stream->avail_in = input_len;
234 p_hwfn->stream->next_out = unzip_buf;
235 p_hwfn->stream->avail_out = max_size;
237 rc = inflateInit2(p_hwfn->stream, MAX_WBITS);
241 "zlib init failed, rc = %d\n", rc);
245 rc = inflate(p_hwfn->stream, Z_FINISH);
246 inflateEnd(p_hwfn->stream);
248 if (rc != Z_OK && rc != Z_STREAM_END) {
250 "FW unzip error: %s, rc=%d\n", p_hwfn->stream->msg,
255 return p_hwfn->stream->total_out / 4;
260 qede_get_mcp_proto_stats(struct ecore_dev *edev,
261 enum ecore_mcp_protocol_type type,
262 union ecore_mcp_protocol_stats *stats)
264 struct ecore_eth_stats lan_stats;
266 if (type == ECORE_MCP_LAN_STATS) {
267 ecore_get_vport_stats(edev, &lan_stats);
270 stats->lan_stats.ucast_rx_pkts = lan_stats.common.rx_ucast_pkts;
271 stats->lan_stats.ucast_tx_pkts = lan_stats.common.tx_ucast_pkts;
273 stats->lan_stats.fcs_err = -1;
275 DP_INFO(edev, "Statistics request type %d not supported\n",
280 static void qede_hw_err_handler(void *dev, enum ecore_hw_err_type err_type)
282 struct ecore_dev *edev = dev;
285 case ECORE_HW_ERR_FAN_FAIL:
288 case ECORE_HW_ERR_MFW_RESP_FAIL:
289 case ECORE_HW_ERR_HW_ATTN:
290 case ECORE_HW_ERR_DMAE_FAIL:
291 case ECORE_HW_ERR_RAMROD_FAIL:
292 case ECORE_HW_ERR_FW_ASSERT:
293 OSAL_SAVE_FW_DUMP(0); /* Using port 0 as default port_id */
297 DP_NOTICE(edev, false, "Unknown HW error [%d]\n", err_type);
303 qede_hw_err_notify(struct ecore_hwfn *p_hwfn, enum ecore_hw_err_type err_type)
308 case ECORE_HW_ERR_FAN_FAIL:
309 strcpy(err_str, "Fan Failure");
311 case ECORE_HW_ERR_MFW_RESP_FAIL:
312 strcpy(err_str, "MFW Response Failure");
314 case ECORE_HW_ERR_HW_ATTN:
315 strcpy(err_str, "HW Attention");
317 case ECORE_HW_ERR_DMAE_FAIL:
318 strcpy(err_str, "DMAE Failure");
320 case ECORE_HW_ERR_RAMROD_FAIL:
321 strcpy(err_str, "Ramrod Failure");
323 case ECORE_HW_ERR_FW_ASSERT:
324 strcpy(err_str, "FW Assertion");
327 strcpy(err_str, "Unknown");
330 DP_ERR(p_hwfn, "HW error occurred [%s]\n", err_str);
332 qede_hw_err_handler(p_hwfn->p_dev, err_type);
334 ecore_int_attn_clr_enable(p_hwfn->p_dev, true);
337 u32 qede_crc32(u32 crc, u8 *ptr, u32 length)
343 for (i = 0; i < 8; i++)
344 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
349 void qed_set_platform_str(struct ecore_hwfn *p_hwfn,
350 char *buf_str, u32 buf_size)
352 snprintf(buf_str, buf_size, "%s.", rte_version());