szedata2: support link state operations
[dpdk.git] / drivers / net / szedata2 / rte_eth_szedata2.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2015 - 2016 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 RTE_PMD_SZEDATA2_H_
35 #define RTE_PMD_SZEDATA2_H_
36
37 #include <stdbool.h>
38
39 #include <rte_byteorder.h>
40
41 /* PCI Vendor ID */
42 #define PCI_VENDOR_ID_NETCOPE 0x1b26
43
44 /* PCI Device IDs */
45 #define PCI_DEVICE_ID_NETCOPE_COMBO80G 0xcb80
46 #define PCI_DEVICE_ID_NETCOPE_COMBO100G 0xc1c1
47 #define PCI_DEVICE_ID_NETCOPE_COMBO100G2 0xc2c1
48
49 /* number of PCI resource used by COMBO card */
50 #define PCI_RESOURCE_NUMBER 0
51
52 /* szedata2_packet header length == 4 bytes == 2B segment size + 2B hw size */
53 #define RTE_SZE2_PACKET_HEADER_SIZE 4
54
55 #define RTE_SZE2_MMIO_MAX 10
56
57 /*!
58  * Round 'what' to the nearest larger (or equal) multiple of '8'
59  * (szedata2 packet is aligned to 8 bytes)
60  */
61 #define RTE_SZE2_ALIGN8(what) (((what) + ((8) - 1)) & (~((8) - 1)))
62
63 /*! main handle structure */
64 struct szedata {
65         int fd;
66         struct sze2_instance_info *info;
67         uint32_t *write_size;
68         void *space[RTE_SZE2_MMIO_MAX];
69         struct szedata_lock lock[2][2];
70
71         __u32 *rx_asize, *tx_asize;
72
73         /* szedata_read_next variables - to keep context (ct) */
74
75         /*
76          * rx
77          */
78         /** initial sze lock ptr */
79         const struct szedata_lock   *ct_rx_lck_orig;
80         /** current sze lock ptr (initial or next) */
81         const struct szedata_lock   *ct_rx_lck;
82         /** remaining bytes (not read) within current lock */
83         unsigned int                ct_rx_rem_bytes;
84         /** current pointer to locked memory */
85         unsigned char               *ct_rx_cur_ptr;
86         /**
87          * allocated buffer to store RX packet if it was split
88          * into 2 buffers
89          */
90         unsigned char               *ct_rx_buffer;
91         /** registered function to provide filtering based on hwdata */
92         int (*ct_rx_filter)(u_int16_t hwdata_len, u_char *hwdata);
93
94         /*
95          * tx
96          */
97         /**
98          * buffer for tx - packet is prepared here
99          * (in future for burst write)
100          */
101         unsigned char               *ct_tx_buffer;
102         /** initial sze TX lock ptrs - number according to TX interfaces */
103         const struct szedata_lock   **ct_tx_lck_orig;
104         /** current sze TX lock ptrs - number according to TX interfaces */
105         const struct szedata_lock   **ct_tx_lck;
106         /** already written bytes in both locks */
107         unsigned int                *ct_tx_written_bytes;
108         /** remaining bytes (not written) within current lock */
109         unsigned int                *ct_tx_rem_bytes;
110         /** current pointers to locked memory */
111         unsigned char               **ct_tx_cur_ptr;
112         /** NUMA node closest to PCIe device, or -1 */
113         int                         numa_node;
114 };
115
116 /*
117  * @return Byte from PCI resource at offset "offset".
118  */
119 static inline uint8_t
120 pci_resource_read8(struct rte_eth_dev *dev, uint32_t offset)
121 {
122         return *((uint8_t *)((uint8_t *)
123                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
124                         offset));
125 }
126
127 /*
128  * @return Two bytes from PCI resource starting at offset "offset".
129  */
130 static inline uint16_t
131 pci_resource_read16(struct rte_eth_dev *dev, uint32_t offset)
132 {
133         return rte_le_to_cpu_16(*((uint16_t *)((uint8_t *)
134                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
135                         offset)));
136 }
137
138 /*
139  * @return Four bytes from PCI resource starting at offset "offset".
140  */
141 static inline uint32_t
142 pci_resource_read32(struct rte_eth_dev *dev, uint32_t offset)
143 {
144         return rte_le_to_cpu_32(*((uint32_t *)((uint8_t *)
145                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
146                         offset)));
147 }
148
149 /*
150  * @return Eight bytes from PCI resource starting at offset "offset".
151  */
152 static inline uint64_t
153 pci_resource_read64(struct rte_eth_dev *dev, uint32_t offset)
154 {
155         return rte_le_to_cpu_64(*((uint64_t *)((uint8_t *)
156                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
157                         offset)));
158 }
159
160 /*
161  * Write one byte to PCI resource address space at offset "offset".
162  */
163 static inline void
164 pci_resource_write8(struct rte_eth_dev *dev, uint32_t offset, uint8_t val)
165 {
166         *((uint8_t *)((uint8_t *)
167                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
168                         offset)) = val;
169 }
170
171 /*
172  * Write two bytes to PCI resource address space at offset "offset".
173  */
174 static inline void
175 pci_resource_write16(struct rte_eth_dev *dev, uint32_t offset, uint16_t val)
176 {
177         *((uint16_t *)((uint8_t *)
178                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
179                         offset)) = rte_cpu_to_le_16(val);
180 }
181
182 /*
183  * Write four bytes to PCI resource address space at offset "offset".
184  */
185 static inline void
186 pci_resource_write32(struct rte_eth_dev *dev, uint32_t offset, uint32_t val)
187 {
188         *((uint32_t *)((uint8_t *)
189                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
190                         offset)) = rte_cpu_to_le_32(val);
191 }
192
193 /*
194  * Write eight bytes to PCI resource address space at offset "offset".
195  */
196 static inline void
197 pci_resource_write64(struct rte_eth_dev *dev, uint32_t offset, uint64_t val)
198 {
199         *((uint64_t *)((uint8_t *)
200                         dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr +
201                         offset)) = rte_cpu_to_le_64(val);
202 }
203
204 #define SZEDATA2_PCI_RESOURCE_PTR(dev, offset, type) \
205         ((type)((uint8_t *) \
206         ((dev)->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr) \
207         + (offset)))
208
209 enum szedata2_link_speed {
210         SZEDATA2_LINK_SPEED_DEFAULT = 0,
211         SZEDATA2_LINK_SPEED_10G,
212         SZEDATA2_LINK_SPEED_40G,
213         SZEDATA2_LINK_SPEED_100G,
214 };
215
216 /*
217  * Structure describes CGMII IBUF address space
218  */
219 struct szedata2_cgmii_ibuf {
220         /** Total Received Frames Counter low part */
221         uint32_t trfcl;
222         /** Correct Frames Counter low part */
223         uint32_t cfcl;
224         /** Discarded Frames Counter low part */
225         uint32_t dfcl;
226         /** Counter of frames discarded due to buffer overflow low part */
227         uint32_t bodfcl;
228         /** Total Received Frames Counter high part */
229         uint32_t trfch;
230         /** Correct Frames Counter high part */
231         uint32_t cfch;
232         /** Discarded Frames Counter high part */
233         uint32_t dfch;
234         /** Counter of frames discarded due to buffer overflow high part */
235         uint32_t bodfch;
236         /** IBUF enable register */
237         uint32_t ibuf_en;
238         /** Error mask register */
239         uint32_t err_mask;
240         /** IBUF status register */
241         uint32_t ibuf_st;
242         /** IBUF command register */
243         uint32_t ibuf_cmd;
244         /** Minimum frame length allowed */
245         uint32_t mfla;
246         /** Frame MTU */
247         uint32_t mtu;
248         /** MAC address check mode */
249         uint32_t mac_chmode;
250         /** Octets Received OK Counter low part */
251         uint32_t orocl;
252         /** Octets Received OK Counter high part */
253         uint32_t oroch;
254 } __rte_packed;
255
256 /* Offset of CGMII IBUF memory for MAC addresses */
257 #define SZEDATA2_CGMII_IBUF_MAC_MEM_OFF 0x80
258
259 /*
260  * @return
261  *     true if IBUF is enabled
262  *     false if IBUF is disabled
263  */
264 static inline bool
265 cgmii_ibuf_is_enabled(volatile struct szedata2_cgmii_ibuf *ibuf)
266 {
267         return ((rte_le_to_cpu_32(ibuf->ibuf_en) & 0x1) != 0) ? true : false;
268 }
269
270 /*
271  * Enables IBUF.
272  */
273 static inline void
274 cgmii_ibuf_enable(volatile struct szedata2_cgmii_ibuf *ibuf)
275 {
276         ibuf->ibuf_en =
277                 rte_cpu_to_le_32(rte_le_to_cpu_32(ibuf->ibuf_en) | 0x1);
278 }
279
280 /*
281  * Disables IBUF.
282  */
283 static inline void
284 cgmii_ibuf_disable(volatile struct szedata2_cgmii_ibuf *ibuf)
285 {
286         ibuf->ibuf_en =
287                 rte_cpu_to_le_32(rte_le_to_cpu_32(ibuf->ibuf_en) & ~0x1);
288 }
289
290 /*
291  * @return
292  *     true if ibuf link is up
293  *     false if ibuf link is down
294  */
295 static inline bool
296 cgmii_ibuf_is_link_up(volatile struct szedata2_cgmii_ibuf *ibuf)
297 {
298         return ((rte_le_to_cpu_32(ibuf->ibuf_st) & 0x80) != 0) ? true : false;
299 }
300
301 /*
302  * Structure describes CGMII OBUF address space
303  */
304 struct szedata2_cgmii_obuf {
305         /** Total Sent Frames Counter low part */
306         uint32_t tsfcl;
307         /** Octets Sent Counter low part */
308         uint32_t oscl;
309         /** Total Discarded Frames Counter low part */
310         uint32_t tdfcl;
311         /** reserved */
312         uint32_t reserved1;
313         /** Total Sent Frames Counter high part */
314         uint32_t tsfch;
315         /** Octets Sent Counter high part */
316         uint32_t osch;
317         /** Total Discarded Frames Counter high part */
318         uint32_t tdfch;
319         /** reserved */
320         uint32_t reserved2;
321         /** OBUF enable register */
322         uint32_t obuf_en;
323         /** reserved */
324         uint64_t reserved3;
325         /** OBUF control register */
326         uint32_t ctrl;
327         /** OBUF status register */
328         uint32_t obuf_st;
329 } __rte_packed;
330
331 /*
332  * @return
333  *     true if OBUF is enabled
334  *     false if OBUF is disabled
335  */
336 static inline bool
337 cgmii_obuf_is_enabled(volatile struct szedata2_cgmii_obuf *obuf)
338 {
339         return ((rte_le_to_cpu_32(obuf->obuf_en) & 0x1) != 0) ? true : false;
340 }
341
342 /*
343  * Enables OBUF.
344  */
345 static inline void
346 cgmii_obuf_enable(volatile struct szedata2_cgmii_obuf *obuf)
347 {
348         obuf->obuf_en =
349                 rte_cpu_to_le_32(rte_le_to_cpu_32(obuf->obuf_en) | 0x1);
350 }
351
352 /*
353  * Disables OBUF.
354  */
355 static inline void
356 cgmii_obuf_disable(volatile struct szedata2_cgmii_obuf *obuf)
357 {
358         obuf->obuf_en =
359                 rte_cpu_to_le_32(rte_le_to_cpu_32(obuf->obuf_en) & ~0x1);
360 }
361
362 /*
363  * Function takes value from IBUF status register. Values in IBUF and OBUF
364  * should be same.
365  *
366  * @return Link speed constant.
367  */
368 static inline enum szedata2_link_speed
369 cgmii_link_speed(volatile struct szedata2_cgmii_ibuf *ibuf)
370 {
371         uint32_t speed = (rte_le_to_cpu_32(ibuf->ibuf_st) & 0x70) >> 4;
372         switch (speed) {
373         case 0x03:
374                 return SZEDATA2_LINK_SPEED_10G;
375         case 0x04:
376                 return SZEDATA2_LINK_SPEED_40G;
377         case 0x05:
378                 return SZEDATA2_LINK_SPEED_100G;
379         default:
380                 return SZEDATA2_LINK_SPEED_DEFAULT;
381         }
382 }
383
384 /*
385  * IBUFs and OBUFs can generally be located at different offsets in different
386  * firmwares.
387  * This part defines base offsets of IBUFs and OBUFs through various firmwares.
388  * Currently one firmware type is supported.
389  * Type of firmware is set through configuration option
390  * CONFIG_RTE_LIBRTE_PMD_SZEDATA_AS.
391  * Possible values are:
392  * 0 - for firmwares:
393  *     NIC_100G1_LR4
394  *     HANIC_100G1_LR4
395  *     HANIC_100G1_SR10
396  */
397 #if !defined(RTE_LIBRTE_PMD_SZEDATA2_AS)
398 #error "RTE_LIBRTE_PMD_SZEDATA2_AS has to be defined"
399 #elif RTE_LIBRTE_PMD_SZEDATA2_AS == 0
400
401 /*
402  * CGMII IBUF offset from the beginning of PCI resource address space.
403  */
404 #define SZEDATA2_CGMII_IBUF_BASE_OFF 0x8000
405 /*
406  * Size of CGMII IBUF.
407  */
408 #define SZEDATA2_CGMII_IBUF_SIZE 0x200
409
410 /*
411  * GCMII OBUF offset from the beginning of PCI resource address space.
412  */
413 #define SZEDATA2_CGMII_OBUF_BASE_OFF 0x9000
414 /*
415  * Size of CGMII OBUF.
416  */
417 #define SZEDATA2_CGMII_OBUF_SIZE 0x100
418
419 #else
420 #error "RTE_LIBRTE_PMD_SZEDATA2_AS has wrong value, see comments in config file"
421 #endif
422
423 #endif