Imported Upstream version 16.07-rc1
[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 <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_ffz(unsigned long word)
69 {
70         unsigned long first_zero;
71
72         first_zero = __builtin_ffsl(~word);
73         return first_zero ? (first_zero - 1) : OSAL_BITS_PER_UL;
74 }
75
76 inline u32 qede_find_first_zero_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         return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffz(addr[i]);
86 }
87
88 void qede_vf_fill_driver_data(struct ecore_hwfn *hwfn,
89                               __rte_unused struct vf_pf_resc_request *resc_req,
90                               struct ecore_vf_acquire_sw_info *vf_sw_info)
91 {
92         vf_sw_info->os_type = VFPF_ACQUIRE_OS_LINUX_USERSPACE;
93         vf_sw_info->override_fw_version = 1;
94 }
95
96 void *osal_dma_alloc_coherent(struct ecore_dev *p_dev,
97                               dma_addr_t *phys, size_t size)
98 {
99         const struct rte_memzone *mz;
100         char mz_name[RTE_MEMZONE_NAMESIZE];
101         uint32_t core_id = rte_lcore_id();
102         unsigned int socket_id;
103
104         OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
105         snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
106                                         (unsigned long)rte_get_timer_cycles());
107         if (core_id == (unsigned int)LCORE_ID_ANY)
108                 core_id = 0;
109         socket_id = rte_lcore_to_socket_id(core_id);
110         mz = rte_memzone_reserve_aligned(mz_name, size,
111                                          socket_id, 0, RTE_CACHE_LINE_SIZE);
112         if (!mz) {
113                 DP_ERR(p_dev, "Unable to allocate DMA memory "
114                        "of size %zu bytes - %s\n",
115                        size, rte_strerror(rte_errno));
116                 *phys = 0;
117                 return OSAL_NULL;
118         }
119         *phys = mz->phys_addr;
120         DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
121                    "size=%zu phys=0x%" PRIx64 " virt=%p on socket=%u\n",
122                    mz->len, mz->phys_addr, mz->addr, socket_id);
123         return mz->addr;
124 }
125
126 void *osal_dma_alloc_coherent_aligned(struct ecore_dev *p_dev,
127                                       dma_addr_t *phys, size_t size, int align)
128 {
129         const struct rte_memzone *mz;
130         char mz_name[RTE_MEMZONE_NAMESIZE];
131         uint32_t core_id = rte_lcore_id();
132         unsigned int socket_id;
133
134         OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
135         snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
136                                         (unsigned long)rte_get_timer_cycles());
137         if (core_id == (unsigned int)LCORE_ID_ANY)
138                 core_id = 0;
139         socket_id = rte_lcore_to_socket_id(core_id);
140         mz = rte_memzone_reserve_aligned(mz_name, size, socket_id, 0, align);
141         if (!mz) {
142                 DP_ERR(p_dev, "Unable to allocate DMA memory "
143                        "of size %zu bytes - %s\n",
144                        size, rte_strerror(rte_errno));
145                 *phys = 0;
146                 return OSAL_NULL;
147         }
148         *phys = mz->phys_addr;
149         DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
150                    "aligned memory size=%zu phys=0x%" PRIx64 " virt=%p core=%d\n",
151                    mz->len, mz->phys_addr, mz->addr, core_id);
152         return mz->addr;
153 }
154
155 u32 qede_unzip_data(struct ecore_hwfn *p_hwfn, u32 input_len,
156                     u8 *input_buf, u32 max_size, u8 *unzip_buf)
157 {
158         int rc;
159
160         p_hwfn->stream->next_in = input_buf;
161         p_hwfn->stream->avail_in = input_len;
162         p_hwfn->stream->next_out = unzip_buf;
163         p_hwfn->stream->avail_out = max_size;
164
165         rc = inflateInit2(p_hwfn->stream, MAX_WBITS);
166
167         if (rc != Z_OK) {
168                 DP_ERR(p_hwfn,
169                            "zlib init failed, rc = %d\n", rc);
170                 return 0;
171         }
172
173         rc = inflate(p_hwfn->stream, Z_FINISH);
174         inflateEnd(p_hwfn->stream);
175
176         if (rc != Z_OK && rc != Z_STREAM_END) {
177                 DP_ERR(p_hwfn,
178                            "FW unzip error: %s, rc=%d\n", p_hwfn->stream->msg,
179                            rc);
180                 return 0;
181         }
182
183         return p_hwfn->stream->total_out / 4;
184 }
185
186 void
187 qede_get_mcp_proto_stats(struct ecore_dev *edev,
188                          enum ecore_mcp_protocol_type type,
189                          union ecore_mcp_protocol_stats *stats)
190 {
191         struct ecore_eth_stats lan_stats;
192
193         if (type == ECORE_MCP_LAN_STATS) {
194                 ecore_get_vport_stats(edev, &lan_stats);
195                 stats->lan_stats.ucast_rx_pkts = lan_stats.rx_ucast_pkts;
196                 stats->lan_stats.ucast_tx_pkts = lan_stats.tx_ucast_pkts;
197                 stats->lan_stats.fcs_err = -1;
198         } else {
199                 DP_INFO(edev, "Statistics request type %d not supported\n",
200                        type);
201         }
202 }