4 * Copyright (c) 2017 CESNET
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _SZEDATA2_IOBUF_H_
35 #define _SZEDATA2_IOBUF_H_
40 #include <rte_byteorder.h>
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;
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;
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,
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,
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).
71 #define SZEDATA2_PCI_RESOURCE_PTR(rsc, offset, type) \
72 ((type)(((uint8_t *)(rsc)->addr) + (offset)))
75 * Maximum possible number of MAC addresses (limited by IBUF status
76 * register value MAC_COUNT which has 5 bits).
78 #define SZEDATA2_IBUF_MAX_MAC_COUNT 32
81 * Structure describes IBUF address space.
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 */
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 */
119 uint8_t reserved[60]; /**< 0x4C */
120 /** IBUF memory for MAC addresses */
121 uint32_t mac_mem[2 * SZEDATA2_IBUF_MAX_MAC_COUNT]; /**< 0x80 */
125 * Structure describes OBUF address space.
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 */
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 */
143 uint32_t reserved2; /**< 0x1C */
144 /** OBUF enable register */
145 uint32_t obuf_en; /**< 0x20 */
147 uint64_t reserved3; /**< 0x24 */
148 /** OBUF control register */
149 uint32_t ctrl; /**< 0x2C */
150 /** OBUF status register */
151 uint32_t obuf_st; /**< 0x30 */
155 * Wrapper for reading 4 bytes from device memory in correct endianness.
158 * Address for reading.
162 static inline uint32_t
163 szedata2_read32(const volatile void *addr)
165 return rte_le_to_cpu_32(rte_read32(addr));
169 * Wrapper for writing 4 bytes to device memory in correct endianness.
174 * Address for writing.
177 szedata2_write32(uint32_t value, volatile void *addr)
179 rte_write32(rte_cpu_to_le_32(value), addr);
183 * Get pointer to IBUF structure according to specified index.
186 * Pointer to base address of memory resource.
190 * Pointer to IBUF structure.
192 static inline struct szedata2_ibuf *
193 ibuf_ptr_by_index(struct rte_mem_resource *rsc, uint32_t index)
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 *);
202 * Get pointer to OBUF structure according to specified idnex.
205 * Pointer to base address of memory resource.
209 * Pointer to OBUF structure.
211 static inline struct szedata2_obuf *
212 obuf_ptr_by_index(struct rte_mem_resource *rsc, uint32_t index)
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 *);
221 * Checks if IBUF is enabled.
224 * Pointer to IBUF structure.
226 * true if IBUF is enabled.
227 * false if IBUF is disabled.
230 ibuf_is_enabled(const volatile struct szedata2_ibuf *ibuf)
232 return ((szedata2_read32(&ibuf->ibuf_en) & 0x1) != 0) ? true : false;
239 * Pointer to IBUF structure.
242 ibuf_enable(volatile struct szedata2_ibuf *ibuf)
244 szedata2_write32(szedata2_read32(&ibuf->ibuf_en) | 0x1, &ibuf->ibuf_en);
251 * Pointer to IBUF structure.
254 ibuf_disable(volatile struct szedata2_ibuf *ibuf)
256 szedata2_write32(szedata2_read32(&ibuf->ibuf_en) & ~0x1,
261 * Checks if link is up.
264 * Pointer to IBUF structure.
266 * true if ibuf link is up.
267 * false if ibuf link is down.
270 ibuf_is_link_up(const volatile struct szedata2_ibuf *ibuf)
272 return ((szedata2_read32(&ibuf->ibuf_st) & 0x80) != 0) ? true : false;
276 * Get current MAC address check mode from IBUF.
279 * Pointer to IBUF structure.
281 * MAC address check mode constant.
283 static inline enum szedata2_mac_check_mode
284 ibuf_mac_mode_read(const volatile struct szedata2_ibuf *ibuf)
286 switch (szedata2_read32(&ibuf->mac_chmode) & 0x3) {
288 return SZEDATA2_MAC_CHMODE_PROMISC;
290 return SZEDATA2_MAC_CHMODE_ONLY_VALID;
292 return SZEDATA2_MAC_CHMODE_ALL_BROADCAST;
294 return SZEDATA2_MAC_CHMODE_ALL_MULTICAST;
296 return SZEDATA2_MAC_CHMODE_PROMISC;
301 * Writes mode in MAC address check mode register in IBUF.
304 * Pointer to IBUF structure.
306 * MAC address check mode to set.
309 ibuf_mac_mode_write(volatile struct szedata2_ibuf *ibuf,
310 enum szedata2_mac_check_mode mode)
312 szedata2_write32((szedata2_read32(&ibuf->mac_chmode) & ~0x3) | mode,
317 * Checks if obuf is enabled.
320 * Pointer to OBUF structure.
322 * true if OBUF is enabled.
323 * false if OBUF is disabled.
326 obuf_is_enabled(const volatile struct szedata2_obuf *obuf)
328 return ((szedata2_read32(&obuf->obuf_en) & 0x1) != 0) ? true : false;
335 * Pointer to OBUF structure.
338 obuf_enable(volatile struct szedata2_obuf *obuf)
340 szedata2_write32(szedata2_read32(&obuf->obuf_en) | 0x1, &obuf->obuf_en);
347 * Pointer to OBUF structure.
350 obuf_disable(volatile struct szedata2_obuf *obuf)
352 szedata2_write32(szedata2_read32(&obuf->obuf_en) & ~0x1,
356 #endif /* _SZEDATA2_IOBUF_H_ */