net/qede/base: add attention formatting string
[dpdk.git] / drivers / net / qede / base / bcm_osal.c
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #include <zlib.h>
10
11 #include <rte_memzone.h>
12 #include <rte_errno.h>
13
14 #include "bcm_osal.h"
15 #include "ecore.h"
16 #include "ecore_hw.h"
17 #include "ecore_iov_api.h"
18 #include "ecore_mcp_api.h"
19 #include "ecore_l2_api.h"
20
21
22 unsigned long qede_log2_align(unsigned long n)
23 {
24         unsigned long ret = n ? 1 : 0;
25         unsigned long _n = n >> 1;
26
27         while (_n) {
28                 _n >>= 1;
29                 ret <<= 1;
30         }
31
32         if (ret < n)
33                 ret <<= 1;
34
35         return ret;
36 }
37
38 u32 qede_osal_log2(u32 val)
39 {
40         u32 log = 0;
41
42         while (val >>= 1)
43                 log++;
44
45         return log;
46 }
47
48 inline void qede_set_bit(u32 nr, unsigned long *addr)
49 {
50         __sync_fetch_and_or(addr, (1UL << nr));
51 }
52
53 inline void qede_clr_bit(u32 nr, unsigned long *addr)
54 {
55         __sync_fetch_and_and(addr, ~(1UL << nr));
56 }
57
58 inline bool qede_test_bit(u32 nr, unsigned long *addr)
59 {
60         bool res;
61
62         rte_mb();
63         res = ((*addr) & (1UL << nr)) != 0;
64         rte_mb();
65         return res;
66 }
67
68 static inline u32 qede_ffb(unsigned long word)
69 {
70         unsigned long first_bit;
71
72         first_bit = __builtin_ffsl(word);
73         return first_bit ? (first_bit - 1) : OSAL_BITS_PER_UL;
74 }
75
76 inline u32 qede_find_first_bit(unsigned long *addr, u32 limit)
77 {
78         u32 i;
79         u32 nwords = 0;
80         OSAL_BUILD_BUG_ON(!limit);
81         nwords = (limit - 1) / OSAL_BITS_PER_UL + 1;
82         for (i = 0; i < nwords; i++)
83                 if (addr[i] != 0)
84                         break;
85
86         return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffb(addr[i]);
87 }
88
89 static inline u32 qede_ffz(unsigned long word)
90 {
91         unsigned long first_zero;
92
93         first_zero = __builtin_ffsl(~word);
94         return first_zero ? (first_zero - 1) : OSAL_BITS_PER_UL;
95 }
96
97 inline u32 qede_find_first_zero_bit(unsigned long *addr, u32 limit)
98 {
99         u32 i;
100         u32 nwords = 0;
101         OSAL_BUILD_BUG_ON(!limit);
102         nwords = (limit - 1) / OSAL_BITS_PER_UL + 1;
103         for (i = 0; i < nwords; i++)
104                 if (~(addr[i] != 0))
105                         break;
106         return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffz(addr[i]);
107 }
108
109 void qede_vf_fill_driver_data(struct ecore_hwfn *hwfn,
110                               __rte_unused struct vf_pf_resc_request *resc_req,
111                               struct ecore_vf_acquire_sw_info *vf_sw_info)
112 {
113         vf_sw_info->os_type = VFPF_ACQUIRE_OS_LINUX_USERSPACE;
114         vf_sw_info->override_fw_version = 1;
115 }
116
117 void *osal_dma_alloc_coherent(struct ecore_dev *p_dev,
118                               dma_addr_t *phys, size_t size)
119 {
120         const struct rte_memzone *mz;
121         char mz_name[RTE_MEMZONE_NAMESIZE];
122         uint32_t core_id = rte_lcore_id();
123         unsigned int socket_id;
124
125         OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
126         snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
127                                         (unsigned long)rte_get_timer_cycles());
128         if (core_id == (unsigned int)LCORE_ID_ANY)
129                 core_id = 0;
130         socket_id = rte_lcore_to_socket_id(core_id);
131         mz = rte_memzone_reserve_aligned(mz_name, size,
132                                          socket_id, 0, RTE_CACHE_LINE_SIZE);
133         if (!mz) {
134                 DP_ERR(p_dev, "Unable to allocate DMA memory "
135                        "of size %zu bytes - %s\n",
136                        size, rte_strerror(rte_errno));
137                 *phys = 0;
138                 return OSAL_NULL;
139         }
140         *phys = mz->phys_addr;
141         DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
142                    "size=%zu phys=0x%" PRIx64 " virt=%p on socket=%u\n",
143                    mz->len, mz->phys_addr, mz->addr, socket_id);
144         return mz->addr;
145 }
146
147 void *osal_dma_alloc_coherent_aligned(struct ecore_dev *p_dev,
148                                       dma_addr_t *phys, size_t size, int align)
149 {
150         const struct rte_memzone *mz;
151         char mz_name[RTE_MEMZONE_NAMESIZE];
152         uint32_t core_id = rte_lcore_id();
153         unsigned int socket_id;
154
155         OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
156         snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
157                                         (unsigned long)rte_get_timer_cycles());
158         if (core_id == (unsigned int)LCORE_ID_ANY)
159                 core_id = 0;
160         socket_id = rte_lcore_to_socket_id(core_id);
161         mz = rte_memzone_reserve_aligned(mz_name, size, socket_id, 0, align);
162         if (!mz) {
163                 DP_ERR(p_dev, "Unable to allocate DMA memory "
164                        "of size %zu bytes - %s\n",
165                        size, rte_strerror(rte_errno));
166                 *phys = 0;
167                 return OSAL_NULL;
168         }
169         *phys = mz->phys_addr;
170         DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
171                    "aligned memory size=%zu phys=0x%" PRIx64 " virt=%p core=%d\n",
172                    mz->len, mz->phys_addr, mz->addr, core_id);
173         return mz->addr;
174 }
175
176 #ifdef CONFIG_ECORE_ZIPPED_FW
177 u32 qede_unzip_data(struct ecore_hwfn *p_hwfn, u32 input_len,
178                     u8 *input_buf, u32 max_size, u8 *unzip_buf)
179 {
180         int rc;
181
182         p_hwfn->stream->next_in = input_buf;
183         p_hwfn->stream->avail_in = input_len;
184         p_hwfn->stream->next_out = unzip_buf;
185         p_hwfn->stream->avail_out = max_size;
186
187         rc = inflateInit2(p_hwfn->stream, MAX_WBITS);
188
189         if (rc != Z_OK) {
190                 DP_ERR(p_hwfn,
191                            "zlib init failed, rc = %d\n", rc);
192                 return 0;
193         }
194
195         rc = inflate(p_hwfn->stream, Z_FINISH);
196         inflateEnd(p_hwfn->stream);
197
198         if (rc != Z_OK && rc != Z_STREAM_END) {
199                 DP_ERR(p_hwfn,
200                            "FW unzip error: %s, rc=%d\n", p_hwfn->stream->msg,
201                            rc);
202                 return 0;
203         }
204
205         return p_hwfn->stream->total_out / 4;
206 }
207 #endif
208
209 void
210 qede_get_mcp_proto_stats(struct ecore_dev *edev,
211                          enum ecore_mcp_protocol_type type,
212                          union ecore_mcp_protocol_stats *stats)
213 {
214         struct ecore_eth_stats lan_stats;
215
216         if (type == ECORE_MCP_LAN_STATS) {
217                 ecore_get_vport_stats(edev, &lan_stats);
218                 stats->lan_stats.ucast_rx_pkts = lan_stats.rx_ucast_pkts;
219                 stats->lan_stats.ucast_tx_pkts = lan_stats.tx_ucast_pkts;
220                 stats->lan_stats.fcs_err = -1;
221         } else {
222                 DP_INFO(edev, "Statistics request type %d not supported\n",
223                        type);
224         }
225 }