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