New upstream version 18.02
[deb_dpdk.git] / drivers / net / szedata2 / szedata2_iobuf.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2017 CESNET
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of CESNET nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _SZEDATA2_IOBUF_H_
35 #define _SZEDATA2_IOBUF_H_
36
37 #include <stdint.h>
38 #include <stdbool.h>
39
40 #include <rte_byteorder.h>
41 #include <rte_io.h>
42 #include <rte_dev.h>
43
44 /* IBUF offsets from the beginning of the PCI resource address space. */
45 extern const uint32_t szedata2_ibuf_base_table[];
46 extern const uint32_t szedata2_ibuf_count;
47
48 /* OBUF offsets from the beginning of the PCI resource address space. */
49 extern const uint32_t szedata2_obuf_base_table[];
50 extern const uint32_t szedata2_obuf_count;
51
52 enum szedata2_link_speed {
53         SZEDATA2_LINK_SPEED_DEFAULT = 0,
54         SZEDATA2_LINK_SPEED_10G,
55         SZEDATA2_LINK_SPEED_40G,
56         SZEDATA2_LINK_SPEED_100G,
57 };
58
59 enum szedata2_mac_check_mode {
60         SZEDATA2_MAC_CHMODE_PROMISC       = 0x0,
61         SZEDATA2_MAC_CHMODE_ONLY_VALID    = 0x1,
62         SZEDATA2_MAC_CHMODE_ALL_BROADCAST = 0x2,
63         SZEDATA2_MAC_CHMODE_ALL_MULTICAST = 0x3,
64 };
65
66 /**
67  * Macro takes pointer to pci resource structure (rsc)
68  * and returns pointer to mapped resource memory at
69  * specified offset (offset) typecast to the type (type).
70  */
71 #define SZEDATA2_PCI_RESOURCE_PTR(rsc, offset, type) \
72         ((type)(((uint8_t *)(rsc)->addr) + (offset)))
73
74 /**
75  * Maximum possible number of MAC addresses (limited by IBUF status
76  * register value MAC_COUNT which has 5 bits).
77  */
78 #define SZEDATA2_IBUF_MAX_MAC_COUNT 32
79
80 /**
81  * Structure describes IBUF address space.
82  */
83 struct szedata2_ibuf {
84         /** Total Received Frames Counter low part */
85         uint32_t trfcl; /**< 0x00 */
86         /** Correct Frames Counter low part */
87         uint32_t cfcl; /**< 0x04 */
88         /** Discarded Frames Counter low part */
89         uint32_t dfcl; /**< 0x08 */
90         /** Counter of frames discarded due to buffer overflow low part */
91         uint32_t bodfcl; /**< 0x0C */
92         /** Total Received Frames Counter high part */
93         uint32_t trfch; /**< 0x10 */
94         /** Correct Frames Counter high part */
95         uint32_t cfch; /**< 0x14 */
96         /** Discarded Frames Counter high part */
97         uint32_t dfch; /**< 0x18 */
98         /** Counter of frames discarded due to buffer overflow high part */
99         uint32_t bodfch; /**< 0x1C */
100         /** IBUF enable register */
101         uint32_t ibuf_en; /**< 0x20 */
102         /** Error mask register */
103         uint32_t err_mask; /**< 0x24 */
104         /** IBUF status register */
105         uint32_t ibuf_st; /**< 0x28 */
106         /** IBUF command register */
107         uint32_t ibuf_cmd; /**< 0x2C */
108         /** Minimum frame length allowed */
109         uint32_t mfla; /**< 0x30 */
110         /** Frame MTU */
111         uint32_t mtu; /**< 0x34 */
112         /** MAC address check mode */
113         uint32_t mac_chmode; /**< 0x38 */
114         /** Octets Received OK Counter low part */
115         uint32_t orocl; /**< 0x3C */
116         /** Octets Received OK Counter high part */
117         uint32_t oroch; /**< 0x40 */
118         /** reserved */
119         uint8_t reserved[60]; /**< 0x4C */
120         /** IBUF memory for MAC addresses */
121         uint32_t mac_mem[2 * SZEDATA2_IBUF_MAX_MAC_COUNT]; /**< 0x80 */
122 } __rte_packed;
123
124 /**
125  * Structure describes OBUF address space.
126  */
127 struct szedata2_obuf {
128         /** Total Sent Frames Counter low part */
129         uint32_t tsfcl; /**< 0x00 */
130         /** Octets Sent Counter low part */
131         uint32_t oscl; /**< 0x04 */
132         /** Total Discarded Frames Counter low part */
133         uint32_t tdfcl; /**< 0x08 */
134         /** reserved */
135         uint32_t reserved1; /**< 0x0C */
136         /** Total Sent Frames Counter high part */
137         uint32_t tsfch; /**< 0x10 */
138         /** Octets Sent Counter high part */
139         uint32_t osch; /**< 0x14 */
140         /** Total Discarded Frames Counter high part */
141         uint32_t tdfch; /**< 0x18 */
142         /** reserved */
143         uint32_t reserved2; /**< 0x1C */
144         /** OBUF enable register */
145         uint32_t obuf_en; /**< 0x20 */
146         /** reserved */
147         uint64_t reserved3; /**< 0x24 */
148         /** OBUF control register */
149         uint32_t ctrl; /**< 0x2C */
150         /** OBUF status register */
151         uint32_t obuf_st; /**< 0x30 */
152 } __rte_packed;
153
154 /**
155  * Wrapper for reading 4 bytes from device memory in correct endianness.
156  *
157  * @param addr
158  *     Address for reading.
159  * @return
160  *     4 B value.
161  */
162 static inline uint32_t
163 szedata2_read32(const volatile void *addr)
164 {
165         return rte_le_to_cpu_32(rte_read32(addr));
166 }
167
168 /**
169  * Wrapper for writing 4 bytes to device memory in correct endianness.
170  *
171  * @param value
172  *     Value to write.
173  * @param addr
174  *     Address for writing.
175  */
176 static inline void
177 szedata2_write32(uint32_t value, volatile void *addr)
178 {
179         rte_write32(rte_cpu_to_le_32(value), addr);
180 }
181
182 /**
183  * Get pointer to IBUF structure according to specified index.
184  *
185  * @param rsc
186  *     Pointer to base address of memory resource.
187  * @param index
188  *     Index of IBUF.
189  * @return
190  *     Pointer to IBUF structure.
191  */
192 static inline struct szedata2_ibuf *
193 ibuf_ptr_by_index(struct rte_mem_resource *rsc, uint32_t index)
194 {
195         if (index >= szedata2_ibuf_count)
196                 index = szedata2_ibuf_count - 1;
197         return SZEDATA2_PCI_RESOURCE_PTR(rsc, szedata2_ibuf_base_table[index],
198                 struct szedata2_ibuf *);
199 }
200
201 /**
202  * Get pointer to OBUF structure according to specified idnex.
203  *
204  * @param rsc
205  *     Pointer to base address of memory resource.
206  * @param index
207  *     Index of OBUF.
208  * @return
209  *     Pointer to OBUF structure.
210  */
211 static inline struct szedata2_obuf *
212 obuf_ptr_by_index(struct rte_mem_resource *rsc, uint32_t index)
213 {
214         if (index >= szedata2_obuf_count)
215                 index = szedata2_obuf_count - 1;
216         return SZEDATA2_PCI_RESOURCE_PTR(rsc, szedata2_obuf_base_table[index],
217                 struct szedata2_obuf *);
218 }
219
220 /**
221  * Checks if IBUF is enabled.
222  *
223  * @param ibuf
224  *     Pointer to IBUF structure.
225  * @return
226  *     true if IBUF is enabled.
227  *     false if IBUF is disabled.
228  */
229 static inline bool
230 ibuf_is_enabled(const volatile struct szedata2_ibuf *ibuf)
231 {
232         return ((szedata2_read32(&ibuf->ibuf_en) & 0x1) != 0) ? true : false;
233 }
234
235 /**
236  * Enables IBUF.
237  *
238  * @param ibuf
239  *     Pointer to IBUF structure.
240  */
241 static inline void
242 ibuf_enable(volatile struct szedata2_ibuf *ibuf)
243 {
244         szedata2_write32(szedata2_read32(&ibuf->ibuf_en) | 0x1, &ibuf->ibuf_en);
245 }
246
247 /**
248  * Disables IBUF.
249  *
250  * @param ibuf
251  *     Pointer to IBUF structure.
252  */
253 static inline void
254 ibuf_disable(volatile struct szedata2_ibuf *ibuf)
255 {
256         szedata2_write32(szedata2_read32(&ibuf->ibuf_en) & ~0x1,
257                         &ibuf->ibuf_en);
258 }
259
260 /**
261  * Checks if link is up.
262  *
263  * @param ibuf
264  *     Pointer to IBUF structure.
265  * @return
266  *     true if ibuf link is up.
267  *     false if ibuf link is down.
268  */
269 static inline bool
270 ibuf_is_link_up(const volatile struct szedata2_ibuf *ibuf)
271 {
272         return ((szedata2_read32(&ibuf->ibuf_st) & 0x80) != 0) ? true : false;
273 }
274
275 /**
276  * Get current MAC address check mode from IBUF.
277  *
278  * @param ibuf
279  *     Pointer to IBUF structure.
280  * @return
281  *     MAC address check mode constant.
282  */
283 static inline enum szedata2_mac_check_mode
284 ibuf_mac_mode_read(const volatile struct szedata2_ibuf *ibuf)
285 {
286         switch (szedata2_read32(&ibuf->mac_chmode) & 0x3) {
287         case 0x0:
288                 return SZEDATA2_MAC_CHMODE_PROMISC;
289         case 0x1:
290                 return SZEDATA2_MAC_CHMODE_ONLY_VALID;
291         case 0x2:
292                 return SZEDATA2_MAC_CHMODE_ALL_BROADCAST;
293         case 0x3:
294                 return SZEDATA2_MAC_CHMODE_ALL_MULTICAST;
295         default:
296                 return SZEDATA2_MAC_CHMODE_PROMISC;
297         }
298 }
299
300 /**
301  * Writes mode in MAC address check mode register in IBUF.
302  *
303  * @param ibuf
304  *     Pointer to IBUF structure.
305  * @param mode
306  *     MAC address check mode to set.
307  */
308 static inline void
309 ibuf_mac_mode_write(volatile struct szedata2_ibuf *ibuf,
310                 enum szedata2_mac_check_mode mode)
311 {
312         szedata2_write32((szedata2_read32(&ibuf->mac_chmode) & ~0x3) | mode,
313                         &ibuf->mac_chmode);
314 }
315
316 /**
317  * Checks if obuf is enabled.
318  *
319  * @param obuf
320  *     Pointer to OBUF structure.
321  * @return
322  *     true if OBUF is enabled.
323  *     false if OBUF is disabled.
324  */
325 static inline bool
326 obuf_is_enabled(const volatile struct szedata2_obuf *obuf)
327 {
328         return ((szedata2_read32(&obuf->obuf_en) & 0x1) != 0) ? true : false;
329 }
330
331 /**
332  * Enables OBUF.
333  *
334  * @param obuf
335  *     Pointer to OBUF structure.
336  */
337 static inline void
338 obuf_enable(volatile struct szedata2_obuf *obuf)
339 {
340         szedata2_write32(szedata2_read32(&obuf->obuf_en) | 0x1, &obuf->obuf_en);
341 }
342
343 /**
344  * Disables OBUF.
345  *
346  * @param obuf
347  *     Pointer to OBUF structure.
348  */
349 static inline void
350 obuf_disable(volatile struct szedata2_obuf *obuf)
351 {
352         szedata2_write32(szedata2_read32(&obuf->obuf_en) & ~0x1,
353                         &obuf->obuf_en);
354 }
355
356 #endif /* _SZEDATA2_IOBUF_H_ */