795f0f52b9f08c985d4a0255eb5c0544d4f38962
[dpdk.git] / drivers / net / szedata2 / szedata2_iobuf.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 CESNET
3  */
4
5 #ifndef _SZEDATA2_IOBUF_H_
6 #define _SZEDATA2_IOBUF_H_
7
8 #include <stdint.h>
9 #include <stdbool.h>
10
11 #include <rte_byteorder.h>
12 #include <rte_io.h>
13 #include <rte_dev.h>
14
15 /* IBUF offsets from the beginning of the PCI resource address space. */
16 extern const uint32_t szedata2_ibuf_base_table[];
17 extern const uint32_t szedata2_ibuf_count;
18
19 /* OBUF offsets from the beginning of the PCI resource address space. */
20 extern const uint32_t szedata2_obuf_base_table[];
21 extern const uint32_t szedata2_obuf_count;
22
23 enum szedata2_link_speed {
24         SZEDATA2_LINK_SPEED_DEFAULT = 0,
25         SZEDATA2_LINK_SPEED_10G,
26         SZEDATA2_LINK_SPEED_40G,
27         SZEDATA2_LINK_SPEED_100G,
28 };
29
30 enum szedata2_mac_check_mode {
31         SZEDATA2_MAC_CHMODE_PROMISC       = 0x0,
32         SZEDATA2_MAC_CHMODE_ONLY_VALID    = 0x1,
33         SZEDATA2_MAC_CHMODE_ALL_BROADCAST = 0x2,
34         SZEDATA2_MAC_CHMODE_ALL_MULTICAST = 0x3,
35 };
36
37 /**
38  * Macro takes pointer to pci resource structure (rsc)
39  * and returns pointer to mapped resource memory at
40  * specified offset (offset) typecast to the type (type).
41  */
42 #define SZEDATA2_PCI_RESOURCE_PTR(rsc, offset, type) \
43         ((type)(((uint8_t *)(rsc)->addr) + (offset)))
44
45 /**
46  * Maximum possible number of MAC addresses (limited by IBUF status
47  * register value MAC_COUNT which has 5 bits).
48  */
49 #define SZEDATA2_IBUF_MAX_MAC_COUNT 32
50
51 /**
52  * Structure describes IBUF address space.
53  */
54 struct szedata2_ibuf {
55         /** Total Received Frames Counter low part */
56         uint32_t trfcl; /**< 0x00 */
57         /** Correct Frames Counter low part */
58         uint32_t cfcl; /**< 0x04 */
59         /** Discarded Frames Counter low part */
60         uint32_t dfcl; /**< 0x08 */
61         /** Counter of frames discarded due to buffer overflow low part */
62         uint32_t bodfcl; /**< 0x0C */
63         /** Total Received Frames Counter high part */
64         uint32_t trfch; /**< 0x10 */
65         /** Correct Frames Counter high part */
66         uint32_t cfch; /**< 0x14 */
67         /** Discarded Frames Counter high part */
68         uint32_t dfch; /**< 0x18 */
69         /** Counter of frames discarded due to buffer overflow high part */
70         uint32_t bodfch; /**< 0x1C */
71         /** IBUF enable register */
72         uint32_t ibuf_en; /**< 0x20 */
73         /** Error mask register */
74         uint32_t err_mask; /**< 0x24 */
75         /** IBUF status register */
76         uint32_t ibuf_st; /**< 0x28 */
77         /** IBUF command register */
78         uint32_t ibuf_cmd; /**< 0x2C */
79         /** Minimum frame length allowed */
80         uint32_t mfla; /**< 0x30 */
81         /** Frame MTU */
82         uint32_t mtu; /**< 0x34 */
83         /** MAC address check mode */
84         uint32_t mac_chmode; /**< 0x38 */
85         /** Octets Received OK Counter low part */
86         uint32_t orocl; /**< 0x3C */
87         /** Octets Received OK Counter high part */
88         uint32_t oroch; /**< 0x40 */
89         /** reserved */
90         uint8_t reserved[60]; /**< 0x4C */
91         /** IBUF memory for MAC addresses */
92         uint32_t mac_mem[2 * SZEDATA2_IBUF_MAX_MAC_COUNT]; /**< 0x80 */
93 } __rte_packed;
94
95 /**
96  * Structure describes OBUF address space.
97  */
98 struct szedata2_obuf {
99         /** Total Sent Frames Counter low part */
100         uint32_t tsfcl; /**< 0x00 */
101         /** Octets Sent Counter low part */
102         uint32_t oscl; /**< 0x04 */
103         /** Total Discarded Frames Counter low part */
104         uint32_t tdfcl; /**< 0x08 */
105         /** reserved */
106         uint32_t reserved1; /**< 0x0C */
107         /** Total Sent Frames Counter high part */
108         uint32_t tsfch; /**< 0x10 */
109         /** Octets Sent Counter high part */
110         uint32_t osch; /**< 0x14 */
111         /** Total Discarded Frames Counter high part */
112         uint32_t tdfch; /**< 0x18 */
113         /** reserved */
114         uint32_t reserved2; /**< 0x1C */
115         /** OBUF enable register */
116         uint32_t obuf_en; /**< 0x20 */
117         /** reserved */
118         uint64_t reserved3; /**< 0x24 */
119         /** OBUF control register */
120         uint32_t ctrl; /**< 0x2C */
121         /** OBUF status register */
122         uint32_t obuf_st; /**< 0x30 */
123 } __rte_packed;
124
125 /**
126  * Wrapper for reading 4 bytes from device memory in correct endianness.
127  *
128  * @param addr
129  *     Address for reading.
130  * @return
131  *     4 B value.
132  */
133 static inline uint32_t
134 szedata2_read32(const volatile void *addr)
135 {
136         return rte_le_to_cpu_32(rte_read32(addr));
137 }
138
139 /**
140  * Wrapper for writing 4 bytes to device memory in correct endianness.
141  *
142  * @param value
143  *     Value to write.
144  * @param addr
145  *     Address for writing.
146  */
147 static inline void
148 szedata2_write32(uint32_t value, volatile void *addr)
149 {
150         rte_write32(rte_cpu_to_le_32(value), addr);
151 }
152
153 /**
154  * Get pointer to IBUF structure according to specified index.
155  *
156  * @param rsc
157  *     Pointer to base address of memory resource.
158  * @param index
159  *     Index of IBUF.
160  * @return
161  *     Pointer to IBUF structure.
162  */
163 static inline struct szedata2_ibuf *
164 ibuf_ptr_by_index(struct rte_mem_resource *rsc, uint32_t index)
165 {
166         if (index >= szedata2_ibuf_count)
167                 index = szedata2_ibuf_count - 1;
168         return SZEDATA2_PCI_RESOURCE_PTR(rsc, szedata2_ibuf_base_table[index],
169                 struct szedata2_ibuf *);
170 }
171
172 /**
173  * Get pointer to OBUF structure according to specified idnex.
174  *
175  * @param rsc
176  *     Pointer to base address of memory resource.
177  * @param index
178  *     Index of OBUF.
179  * @return
180  *     Pointer to OBUF structure.
181  */
182 static inline struct szedata2_obuf *
183 obuf_ptr_by_index(struct rte_mem_resource *rsc, uint32_t index)
184 {
185         if (index >= szedata2_obuf_count)
186                 index = szedata2_obuf_count - 1;
187         return SZEDATA2_PCI_RESOURCE_PTR(rsc, szedata2_obuf_base_table[index],
188                 struct szedata2_obuf *);
189 }
190
191 /**
192  * Checks if IBUF is enabled.
193  *
194  * @param ibuf
195  *     Pointer to IBUF structure.
196  * @return
197  *     true if IBUF is enabled.
198  *     false if IBUF is disabled.
199  */
200 static inline bool
201 ibuf_is_enabled(const volatile struct szedata2_ibuf *ibuf)
202 {
203         return ((szedata2_read32(&ibuf->ibuf_en) & 0x1) != 0) ? true : false;
204 }
205
206 /**
207  * Enables IBUF.
208  *
209  * @param ibuf
210  *     Pointer to IBUF structure.
211  */
212 static inline void
213 ibuf_enable(volatile struct szedata2_ibuf *ibuf)
214 {
215         szedata2_write32(szedata2_read32(&ibuf->ibuf_en) | 0x1, &ibuf->ibuf_en);
216 }
217
218 /**
219  * Disables IBUF.
220  *
221  * @param ibuf
222  *     Pointer to IBUF structure.
223  */
224 static inline void
225 ibuf_disable(volatile struct szedata2_ibuf *ibuf)
226 {
227         szedata2_write32(szedata2_read32(&ibuf->ibuf_en) & ~0x1,
228                         &ibuf->ibuf_en);
229 }
230
231 /**
232  * Checks if link is up.
233  *
234  * @param ibuf
235  *     Pointer to IBUF structure.
236  * @return
237  *     true if ibuf link is up.
238  *     false if ibuf link is down.
239  */
240 static inline bool
241 ibuf_is_link_up(const volatile struct szedata2_ibuf *ibuf)
242 {
243         return ((szedata2_read32(&ibuf->ibuf_st) & 0x80) != 0) ? true : false;
244 }
245
246 /**
247  * Get current MAC address check mode from IBUF.
248  *
249  * @param ibuf
250  *     Pointer to IBUF structure.
251  * @return
252  *     MAC address check mode constant.
253  */
254 static inline enum szedata2_mac_check_mode
255 ibuf_mac_mode_read(const volatile struct szedata2_ibuf *ibuf)
256 {
257         switch (szedata2_read32(&ibuf->mac_chmode) & 0x3) {
258         case 0x0:
259                 return SZEDATA2_MAC_CHMODE_PROMISC;
260         case 0x1:
261                 return SZEDATA2_MAC_CHMODE_ONLY_VALID;
262         case 0x2:
263                 return SZEDATA2_MAC_CHMODE_ALL_BROADCAST;
264         case 0x3:
265                 return SZEDATA2_MAC_CHMODE_ALL_MULTICAST;
266         default:
267                 return SZEDATA2_MAC_CHMODE_PROMISC;
268         }
269 }
270
271 /**
272  * Writes mode in MAC address check mode register in IBUF.
273  *
274  * @param ibuf
275  *     Pointer to IBUF structure.
276  * @param mode
277  *     MAC address check mode to set.
278  */
279 static inline void
280 ibuf_mac_mode_write(volatile struct szedata2_ibuf *ibuf,
281                 enum szedata2_mac_check_mode mode)
282 {
283         szedata2_write32((szedata2_read32(&ibuf->mac_chmode) & ~0x3) | mode,
284                         &ibuf->mac_chmode);
285 }
286
287 /**
288  * Checks if obuf is enabled.
289  *
290  * @param obuf
291  *     Pointer to OBUF structure.
292  * @return
293  *     true if OBUF is enabled.
294  *     false if OBUF is disabled.
295  */
296 static inline bool
297 obuf_is_enabled(const volatile struct szedata2_obuf *obuf)
298 {
299         return ((szedata2_read32(&obuf->obuf_en) & 0x1) != 0) ? true : false;
300 }
301
302 /**
303  * Enables OBUF.
304  *
305  * @param obuf
306  *     Pointer to OBUF structure.
307  */
308 static inline void
309 obuf_enable(volatile struct szedata2_obuf *obuf)
310 {
311         szedata2_write32(szedata2_read32(&obuf->obuf_en) | 0x1, &obuf->obuf_en);
312 }
313
314 /**
315  * Disables OBUF.
316  *
317  * @param obuf
318  *     Pointer to OBUF structure.
319  */
320 static inline void
321 obuf_disable(volatile struct szedata2_obuf *obuf)
322 {
323         szedata2_write32(szedata2_read32(&obuf->obuf_en) & ~0x1,
324                         &obuf->obuf_en);
325 }
326
327 #endif /* _SZEDATA2_IOBUF_H_ */