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