qede: add SRIOV support
[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
19 unsigned long qede_log2_align(unsigned long n)
20 {
21         unsigned long ret = n ? 1 : 0;
22         unsigned long _n = n >> 1;
23
24         while (_n) {
25                 _n >>= 1;
26                 ret <<= 1;
27         }
28
29         if (ret < n)
30                 ret <<= 1;
31
32         return ret;
33 }
34
35 u32 qede_osal_log2(u32 val)
36 {
37         u32 log = 0;
38
39         while (val >>= 1)
40                 log++;
41
42         return log;
43 }
44
45 inline void qede_set_bit(u32 nr, unsigned long *addr)
46 {
47         __sync_fetch_and_or(addr, (1UL << nr));
48 }
49
50 inline void qede_clr_bit(u32 nr, unsigned long *addr)
51 {
52         __sync_fetch_and_and(addr, ~(1UL << nr));
53 }
54
55 inline bool qede_test_bit(u32 nr, unsigned long *addr)
56 {
57         bool res;
58
59         rte_mb();
60         res = ((*addr) & (1UL << nr)) != 0;
61         rte_mb();
62         return res;
63 }
64
65 static inline u32 qede_ffz(unsigned long word)
66 {
67         unsigned long first_zero;
68
69         first_zero = __builtin_ffsl(~word);
70         return first_zero ? (first_zero - 1) : OSAL_BITS_PER_UL;
71 }
72
73 inline u32 qede_find_first_zero_bit(unsigned long *addr, u32 limit)
74 {
75         u32 i;
76         u32 nwords = 0;
77         OSAL_BUILD_BUG_ON(!limit);
78         nwords = (limit - 1) / OSAL_BITS_PER_UL + 1;
79         for (i = 0; i < nwords; i++)
80                 if (~(addr[i] != 0))
81                         break;
82         return (i == nwords) ? limit : i * OSAL_BITS_PER_UL + qede_ffz(addr[i]);
83 }
84
85 void qede_vf_fill_driver_data(struct ecore_hwfn *hwfn,
86                               __rte_unused struct vf_pf_resc_request *resc_req,
87                               struct ecore_vf_acquire_sw_info *vf_sw_info)
88 {
89         vf_sw_info->os_type = VFPF_ACQUIRE_OS_LINUX_USERSPACE;
90         vf_sw_info->override_fw_version = 1;
91 }
92
93 void *osal_dma_alloc_coherent(struct ecore_dev *p_dev,
94                               dma_addr_t *phys, size_t size)
95 {
96         const struct rte_memzone *mz;
97         char mz_name[RTE_MEMZONE_NAMESIZE];
98         uint32_t core_id = rte_lcore_id();
99         unsigned int socket_id;
100
101         OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
102         snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
103                                         (unsigned long)rte_get_timer_cycles());
104         if (core_id == (unsigned int)LCORE_ID_ANY)
105                 core_id = 0;
106         socket_id = rte_lcore_to_socket_id(core_id);
107         mz = rte_memzone_reserve_aligned(mz_name, size,
108                                          socket_id, 0, RTE_CACHE_LINE_SIZE);
109         if (!mz) {
110                 DP_ERR(p_dev, "Unable to allocate DMA memory "
111                        "of size %zu bytes - %s\n",
112                        size, rte_strerror(rte_errno));
113                 *phys = 0;
114                 return OSAL_NULL;
115         }
116         *phys = mz->phys_addr;
117         DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
118                    "size=%zu phys=0x%lx virt=%p on socket=%u\n",
119                    mz->len, mz->phys_addr, mz->addr, socket_id);
120         return mz->addr;
121 }
122
123 void *osal_dma_alloc_coherent_aligned(struct ecore_dev *p_dev,
124                                       dma_addr_t *phys, size_t size, int align)
125 {
126         const struct rte_memzone *mz;
127         char mz_name[RTE_MEMZONE_NAMESIZE];
128         uint32_t core_id = rte_lcore_id();
129         unsigned int socket_id;
130
131         OSAL_MEM_ZERO(mz_name, sizeof(*mz_name));
132         snprintf(mz_name, sizeof(mz_name) - 1, "%lx",
133                                         (unsigned long)rte_get_timer_cycles());
134         if (core_id == (unsigned int)LCORE_ID_ANY)
135                 core_id = 0;
136         socket_id = rte_lcore_to_socket_id(core_id);
137         mz = rte_memzone_reserve_aligned(mz_name, size, socket_id, 0, align);
138         if (!mz) {
139                 DP_ERR(p_dev, "Unable to allocate DMA memory "
140                        "of size %zu bytes - %s\n",
141                        size, rte_strerror(rte_errno));
142                 *phys = 0;
143                 return OSAL_NULL;
144         }
145         *phys = mz->phys_addr;
146         DP_VERBOSE(p_dev, ECORE_MSG_PROBE,
147                    "aligned memory size=%zu phys=0x%lx virt=%p core=%d\n",
148                    mz->len, mz->phys_addr, mz->addr, core_id);
149         return mz->addr;
150 }
151
152 u32 qede_unzip_data(struct ecore_hwfn *p_hwfn, u32 input_len,
153                     u8 *input_buf, u32 max_size, u8 *unzip_buf)
154 {
155         int rc;
156
157         p_hwfn->stream->next_in = input_buf;
158         p_hwfn->stream->avail_in = input_len;
159         p_hwfn->stream->next_out = unzip_buf;
160         p_hwfn->stream->avail_out = max_size;
161
162         rc = inflateInit2(p_hwfn->stream, MAX_WBITS);
163
164         if (rc != Z_OK) {
165                 DP_ERR(p_hwfn,
166                            "zlib init failed, rc = %d\n", rc);
167                 return 0;
168         }
169
170         rc = inflate(p_hwfn->stream, Z_FINISH);
171         inflateEnd(p_hwfn->stream);
172
173         if (rc != Z_OK && rc != Z_STREAM_END) {
174                 DP_ERR(p_hwfn,
175                            "FW unzip error: %s, rc=%d\n", p_hwfn->stream->msg,
176                            rc);
177                 return 0;
178         }
179
180         return p_hwfn->stream->total_out / 4;
181 }