1 #ifndef __CB_BDK_HAL_IF_BDK_IF_H__
2 #define __CB_BDK_HAL_IF_BDK_IF_H__
3 /***********************license start***********************************
4 * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). All rights
5 * reserved.
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * * Neither the name of Cavium Inc. nor the names of
21 * its contributors may be used to endorse or promote products
22 * derived from this software without specific prior written
23 * permission.
24 *
25 * This Software, including technical data, may be subject to U.S. export
26 * control laws, including the U.S. Export Administration Act and its
27 * associated regulations, and may be subject to export or import
28 * regulations in other countries.
29 *
30 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
31 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
32 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
33 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
34 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
35 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
36 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
37 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
38 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK
39 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
40 ***********************license end**************************************/
41
42 /**
43 * @file
44 *
45 * Interface to external interfaces (SGMII, XAUI, etc)
46 *
47 * <hr>$Revision: 45089 $<hr>
48 *
49 * @defgroup packetio Packet Input / Output
50 * @addtogroup packetio
51 * @{
52 */
53
54
55 #define BDK_BGX_PKI 0 /* When defined and non zero BGX will use PKI interface for receive */
56 #define BDK_BGX_PKO 0 /* When defined and non zero BGX will use PKO interface for transmit */
57
58 #define BDK_IF_MAX_GATHER 12 /* CN88XX RX supports 12 at most */
59
60 /* PHY address encoding:
61 Bits[31:24]: Node ID, 0xff for node the ethernet device is on
62 Bits[23:16]: Only used for TWSI
63 Bits[15:12]: PHY connection type (0=MDIO, 1=Fixed, 2=TWSI)
64 For MDIO:
65 Bits[31:24]: Node ID, 0xff for node the ethernet device is on
66 Bits[23:16]: 0
67 Bits[15:12]: 0=MDIO
68 Bits[11:8]: MDIO bus number
69 Bits[7:0]: MDIO address
70 For Fixed:
71 Bits[31:24]: 0
72 Bits[23:16]: Zero
73 Bits[15:12]: 1=Fixed
74 Bits[11:0]: 0 = 1Gb, 1 = 100Mb
75 For TWSI:
76 Bits[31:24]: Node ID, 0xff for node the ethernet device is on
77 Bits[23:16]: TWSI internal address width in bytes (0-2)
78 Bits[15:12]: 2=TWSI
79 Bits[11:8]: TWSI bus number
80 Bits[7:0]: TWSI address
81 */
82 #define BDK_IF_PHY_TYPE_MASK 0xf000
83 #define BDK_IF_PHY_MDIO 0x0000
84 #define BDK_IF_PHY_TWSI 0x2000
85 #define BDK_IF_PHY_FIXED_1GB 0x1000
86 #define BDK_IF_PHY_FIXED_100MB 0x1001
87
88 /**
89 * Enumeration of different interfaces.
90 */
91 typedef enum
92 {
93 BDK_IF_BGX,
94 BDK_IF_PCIE,
95 BDK_IF_FAKE,
96 BDK_IF_LBK,
97 BDK_IF_RGX,
98 BDK_IF_CGX,
99 __BDK_IF_LAST
100 } bdk_if_t;
101
102 typedef enum
103 {
104 BDK_IF_FLAGS_HAS_FCS = 1, /* The device adds and removes an FCS. On thunder we assume the hardware strips FCS */
105 BDK_IF_FLAGS_ENABLED = 2,
106 } bdk_if_flags_t;
107
108 typedef union
109 {
110 uint64_t u64;
111 struct
112 {
113 uint64_t reserved2 : 32;
114 unsigned speed : 24;
115 unsigned lanes : 6;
116 unsigned full_duplex : 1;
117 unsigned up : 1;
118 } s;
119 } bdk_if_link_t;
120
121 typedef struct
122 {
123 uint64_t dropped_octets;
124 uint64_t dropped_packets;
125 uint64_t octets;
126 uint64_t packets;
127 uint64_t errors;
128 } __bdk_if_stats_t;
129
130 typedef struct
131 {
132 __bdk_if_stats_t rx;
133 __bdk_if_stats_t tx;
134 } bdk_if_stats_t;
135
136 typedef struct __bdk_if_port
137 {
138 bdk_if_t iftype : 8;
139 bdk_node_t node : 8;
140 int interface : 8;
141 int index : 8;
142 bdk_if_flags_t flags : 8;
143 int pknd : 8; /* NIC/PKI packet kind */
144 int16_t pki_channel; /* PKI/SSO channel number, or -1 if not used */
145 int16_t pki_dstat; /* PKI DSTAT ID used for receive stats */
146 int16_t pko_queue; /* PKO DQ number, or -1 if not used */
147 int16_t aura; /* FPA aura number */
148 int16_t nic_id; /* NIC ID in use, or -1 if not used. Encoded as NIC_VF * 8 + index */
149 void * receiver; /* This is a bdk_if_packet_receiver_t */
150 void * receiver_arg;
151 bdk_if_stats_t stats;
152 bdk_if_link_t link_info;
153 char name[16];
154 struct __bdk_if_port *next;
155 struct __bdk_if_port *poll_next;
156 char priv[];
157 } __bdk_if_port_t;
158
159 typedef __bdk_if_port_t *bdk_if_handle_t;
160
161 /**
162 * Format of each gather/segment entry in a packet. This is unrelated to
163 * the underlying hardware format, but is designed to be simple to munge
164 * into a hardware format. Note that only 48 bits are stored for the
165 * address. This address is a physical address not mean for SMMU translation.
166 */
167 typedef union
168 {
169 uint64_t u;
170 struct
171 {
172 #if __BYTE_ORDER == __BIG_ENDIAN
173 uint64_t size : 16;
174 uint64_t address : 48;
175 #else
176 uint64_t address : 48;
177 uint64_t size : 16;
178 #endif
179 } s;
180 } bdk_packet_ptr_t;
181
182 typedef enum
183 {
184 BDK_IF_TYPE_UNKNOWN, /* Not set */
185 BDK_IF_TYPE_UDP4, /* IPv4 + UDP */
186 BDK_IF_TYPE_TCP4, /* IPv4 + TCP */
187 } bdk_if_type_t;
188
189 /**
190 * The packet format for the BDK. This structure is designed to be exactly
191 * one cache line to promote alignment and avoid false aliasing. Note that the
192 * packet structure is independent from the packet data and can have a shorter
193 * lifespan. The packet structure is normally on the stack and disappears on
194 * stack unwind. Code requiring its data to stick around needs to copy it, but
195 * not the data in the gather list.
196 */
197 typedef struct
198 {
199 bdk_if_handle_t if_handle; /* Handle to interface this packet was received on */
200 int length; /* Length of the packet in bytes */
201 int segments; /* Number of segments the packet is spread over */
202 int rx_error; /* Error number when packet was receive or zero for no error */
203 bdk_if_type_t packet_type : 16; /* Type of the packet, so sender doesn't need to walk packet */
204 uint16_t mtu; /* MTU for hardware fragment, such as TSO */
205 uint64_t reserved2; /* Reserved for future use */
206 bdk_packet_ptr_t packet[BDK_IF_MAX_GATHER]; /* List of segements. Each has a physical address and length */
207 } bdk_if_packet_t;
208
209 typedef enum
210 {
211 BDK_IF_LOOPBACK_NONE = 0,
212 BDK_IF_LOOPBACK_INTERNAL = 1,
213 BDK_IF_LOOPBACK_EXTERNAL = 2,
214 BDK_IF_LOOPBACK_INTERNAL_EXTERNAL = 3,
215 } bdk_if_loopback_t;
216
217 typedef void (*bdk_if_packet_receiver_t)(const bdk_if_packet_t *packet, void *arg);
218
219 typedef struct
220 {
221 int priv_size;
222 int (*if_num_interfaces)(bdk_node_t node); /* Returns the number of interfaces possible on this chip */
223 int (*if_num_ports)(bdk_node_t node, int interface); /* For given interface, returns the number of ports on it */
224 int (*if_probe)(bdk_if_handle_t handle); /* Probe to see if a port exists */
225 int (*if_init)(bdk_if_handle_t handle); /* One time hardware init */
226 int (*if_enable)(bdk_if_handle_t handle); /* Enable packet IO. must be called after init */
227 int (*if_disable)(bdk_if_handle_t handle); /* Disable packet IO */
228 bdk_if_link_t (*if_link_get)(bdk_if_handle_t handle); /* Get link speed and state */
229 void (*if_link_set)(bdk_if_handle_t handle, bdk_if_link_t link_info); /* Set link speed and state */
230 const bdk_if_stats_t *(*if_get_stats)(bdk_if_handle_t handle); /* Get stats */
231 int (*if_transmit)(bdk_if_handle_t handle, const bdk_if_packet_t *packet); /* TX a packet */
232 int (*if_loopback)(bdk_if_handle_t handle, bdk_if_loopback_t loopback); /* Configure loopback for the port */
233 int (*if_get_queue_depth)(bdk_if_handle_t handle); /* Get the current TX queue depth */
234 uint64_t (*if_get_lane_mask)(bdk_if_handle_t handle); /* Get a mask of the QLM lanes used by this handle */
235 } __bdk_if_ops_t;
236
237 typedef struct
238 {
239 int (*init)(void);
240 uint64_t (*alloc)(int length);
241 void (*free)(uint64_t address, int length);
242 } __bdk_if_global_ops_t;
243
244 extern int bdk_if_is_configured(void);
245 extern int bdk_if_num_interfaces(bdk_node_t node, bdk_if_t iftype);
246 extern int bdk_if_num_ports(bdk_node_t node, bdk_if_t iftype, int interface);
247 extern bdk_if_handle_t bdk_if_next_port(bdk_if_handle_t handle);
248
249 extern int bdk_if_enable(bdk_if_handle_t handle);
250 extern int bdk_if_disable(bdk_if_handle_t handle);
251 extern int bdk_if_loopback(bdk_if_handle_t handle, bdk_if_loopback_t loopback);
252
253 extern const char *bdk_if_name(bdk_if_handle_t handle);
254 extern bdk_if_link_t bdk_if_link_get(bdk_if_handle_t handle);
255 extern bdk_if_link_t bdk_if_link_autoconf(bdk_if_handle_t handle);
256 extern const bdk_if_stats_t *bdk_if_get_stats(bdk_if_handle_t handle);
257 extern bdk_if_link_t __bdk_if_phy_get(bdk_node_t dev_node, int phy_addr);
258 extern int bdk_if_get_queue_depth(bdk_if_handle_t handle);
259 extern int bdk_if_link_wait_all(uint64_t timeout_us);
260 extern int bdk_if_phy_setup(bdk_node_t node);
261 extern int bdk_if_phy_vetesse_setup(bdk_node_t node, int qlm, int mdio_bus, int phy_addr);
262 extern int bdk_if_phy_marvell_setup(bdk_node_t node, int qlm, int mdio_bus, int phy_addr);
263 extern int bdk_if_phy_vsc8514_setup(bdk_node_t node, int qlm, int mdio_bus, int phy_addr);
264 extern int bdk_if_transmit(bdk_if_handle_t handle, bdk_if_packet_t *packet);
265 extern void bdk_if_register_for_packets(bdk_if_handle_t handle, bdk_if_packet_receiver_t receiver, void *arg);
266 extern int bdk_if_alloc(bdk_if_packet_t *packet, int length);
267 extern void bdk_if_free(bdk_if_packet_t *packet);
268 extern void bdk_if_packet_read(const bdk_if_packet_t *packet, int location, int length, void *data);
269 extern void bdk_if_packet_write(bdk_if_packet_t *packet, int location, int length, const void *data);
270
271 extern uint64_t bdk_update_stat_with_overflow(uint64_t new_value, uint64_t old_value, int bit_size);
272 extern void __bdk_if_phy_xs_init(bdk_node_t dev_node, int phy_addr);
273
274 /**
275 * Get interface type. Use this function instead of accessing the handle
276 * directly. The handle is considered private and may change.
277 *
278 * @param handle Handle of port to get info for
279 *
280 * @return Interface type
281 */
bdk_if_get_type(bdk_if_handle_t handle)282 static inline bdk_if_t bdk_if_get_type(bdk_if_handle_t handle)
283 {
284 return handle->iftype;
285 }
286
287 /**
288 * Called by each interface driver to process a received packet. After calling
289 * this function, it is the responsibility of each driver to free any resources
290 * used by the packet, probably by calling bdk_if_free().
291 *
292 * @param packet Packet that was received
293 */
bdk_if_dispatch_packet(const bdk_if_packet_t * packet)294 static inline void bdk_if_dispatch_packet(const bdk_if_packet_t *packet)
295 {
296 void *receiver_arg = packet->if_handle->receiver_arg;
297 bdk_if_packet_receiver_t receiver = packet->if_handle->receiver;
298 if (receiver)
299 receiver(packet, receiver_arg);
300 }
301
302 /**
303 * Get the base QLM used by this handle. For network interfaces that uses QLMs,
304 * return the QLM number of lane 0. Note that some network interfaces span multiple
305 * QLM/DLM. This will return the lowest QLM/DLM number.
306 *
307 * @param handle Handle to query
308 *
309 * @return QLM/DLM number, or -1 if handle doesn't use SERDES
310 */
311 extern int bdk_if_get_qlm(bdk_if_handle_t handle);
312
313 /**
314 * Get a mask of the QLM/DLM lanes used by this handle. A bit is set for each lane
315 * used by the interface. Some ports span multiple QLM/DLM. In this case the bits
316 * set will be wider than the QLM/DLM, signalling that the next QLM/DLM is needed
317 * too.
318 *
319 * @param handle Handle to query
320 *
321 * @return Lane mask, or zero if no SERDES lanes are used
322 */
323 extern uint64_t bdk_if_get_lane_mask(bdk_if_handle_t handle);
324
325 /** @} */
326 #endif
327