xref: /aosp_15_r20/external/ethtool/internal.h (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /* Portions Copyright 2001 Sun Microsystems ([email protected]) */
2 /* Portions Copyright 2002 Intel ([email protected]) */
3 #ifndef ETHTOOL_INTERNAL_H__
4 #define ETHTOOL_INTERNAL_H__
5 
6 /* Some platforms (eg. ppc64) need __SANE_USERSPACE_TYPES__ before
7  * <linux/types.h> to select 'int-ll64.h' and avoid compile warnings
8  * when printing __u64 with %llu.
9  */
10 #define __SANE_USERSPACE_TYPES__
11 
12 #ifdef HAVE_CONFIG_H
13 #include "ethtool-config.h"
14 #endif
15 #include <stdbool.h>
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <endian.h>
23 #include <sys/ioctl.h>
24 #define __UAPI_DEF_IF_IFNAMSIZ	1
25 #define __UAPI_DEF_IF_IFMAP	1
26 #define __UAPI_DEF_IF_IFREQ	1
27 #include <linux/if.h>
28 
29 #include "json_writer.h"
30 #include "json_print.h"
31 
32 #define __maybe_unused __attribute__((__unused__))
33 
34 /* internal for netlink interface */
35 #ifdef ETHTOOL_ENABLE_NETLINK
36 struct nl_context;
37 #endif
38 
39 typedef unsigned long long u64;
40 typedef uint32_t u32;
41 typedef uint16_t u16;
42 typedef uint8_t u8;
43 typedef int32_t s32;
44 
45 #include <linux/ethtool.h>
46 #include <linux/net_tstamp.h>
47 
48 #if __BYTE_ORDER == __BIG_ENDIAN
cpu_to_be16(u16 value)49 static inline u16 cpu_to_be16(u16 value)
50 {
51 	return value;
52 }
cpu_to_be32(u32 value)53 static inline u32 cpu_to_be32(u32 value)
54 {
55 	return value;
56 }
cpu_to_be64(u64 value)57 static inline u64 cpu_to_be64(u64 value)
58 {
59 	return value;
60 }
61 #else
cpu_to_be16(u16 value)62 static inline u16 cpu_to_be16(u16 value)
63 {
64 	return (value >> 8) | (value << 8);
65 }
cpu_to_be32(u32 value)66 static inline u32 cpu_to_be32(u32 value)
67 {
68 	return cpu_to_be16(value >> 16) | (cpu_to_be16(value) << 16);
69 }
cpu_to_be64(u64 value)70 static inline u64 cpu_to_be64(u64 value)
71 {
72 	return cpu_to_be32(value >> 32) | ((u64)cpu_to_be32(value) << 32);
73 }
74 #endif
75 
76 #define ntohll cpu_to_be64
77 #define htonll cpu_to_be64
78 
79 #define BITS_PER_BYTE		8
80 #define BITS_PER_LONG		(BITS_PER_BYTE * sizeof(long))
81 #define DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
82 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_LONG)
83 
set_bit(unsigned int nr,unsigned long * addr)84 static inline void set_bit(unsigned int nr, unsigned long *addr)
85 {
86 	addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
87 }
88 
clear_bit(unsigned int nr,unsigned long * addr)89 static inline void clear_bit(unsigned int nr, unsigned long *addr)
90 {
91 	addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
92 }
93 
test_bit(unsigned int nr,const unsigned long * addr)94 static inline int test_bit(unsigned int nr, const unsigned long *addr)
95 {
96 	return !!((1UL << (nr % BITS_PER_LONG)) &
97 		  (((unsigned long *)addr)[nr / BITS_PER_LONG]));
98 }
99 
100 #ifndef ARRAY_SIZE
101 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
102 #endif
103 
104 #ifndef SIOCETHTOOL
105 #define SIOCETHTOOL     0x8946
106 #endif
107 
108 /* debugging flags */
109 enum {
110 	DEBUG_PARSE,
111 	DEBUG_NL_MSGS,		/* incoming/outgoing netlink messages */
112 	DEBUG_NL_DUMP_SND,	/* dump outgoing netlink messages */
113 	DEBUG_NL_DUMP_RCV,	/* dump incoming netlink messages */
114 	DEBUG_NL_PRETTY_MSG,	/* pretty print of messages and errors */
115 };
116 
debug_on(unsigned long debug,unsigned int bit)117 static inline bool debug_on(unsigned long debug, unsigned int bit)
118 {
119 	return (debug & (1 << bit));
120 }
121 
122 /* Internal values for old-style offload flags.  Values and names
123  * must not clash with the flags defined for ETHTOOL_{G,S}FLAGS.
124  */
125 #define ETH_FLAG_RXCSUM		(1 << 0)
126 #define ETH_FLAG_TXCSUM		(1 << 1)
127 #define ETH_FLAG_SG		(1 << 2)
128 #define ETH_FLAG_TSO		(1 << 3)
129 #define ETH_FLAG_UFO		(1 << 4)
130 #define ETH_FLAG_GSO		(1 << 5)
131 #define ETH_FLAG_GRO		(1 << 6)
132 #define ETH_FLAG_INT_MASK	(ETH_FLAG_RXCSUM | ETH_FLAG_TXCSUM |	\
133 				 ETH_FLAG_SG | ETH_FLAG_TSO | ETH_FLAG_UFO | \
134 				 ETH_FLAG_GSO | ETH_FLAG_GRO),
135 /* Mask of all flags defined for ETHTOOL_{G,S}FLAGS. */
136 #define ETH_FLAG_EXT_MASK	(ETH_FLAG_LRO | ETH_FLAG_RXVLAN |	\
137 				 ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE |	\
138 				 ETH_FLAG_RXHASH)
139 
140 /* internal API for link mode bitmap interaction with kernel. */
141 
142 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32		\
143 	(SCHAR_MAX)
144 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBITS		\
145 	(32 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32)
146 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES	\
147 	(4 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32)
148 #define ETHTOOL_DECLARE_LINK_MODE_MASK(name)		\
149 	u32 name[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]
150 
151 struct ethtool_link_usettings {
152 	struct {
153 		__u8 transceiver;
154 	} deprecated;
155 	struct ethtool_link_settings base;
156 	struct {
157 		ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
158 		ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
159 		ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
160 	} link_modes;
161 };
162 
163 #define ethtool_link_mode_for_each_u32(index)			\
164 	for ((index) = 0;					\
165 	     (index) < ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32;	\
166 	     ++(index))
167 
ethtool_link_mode_zero(u32 * dst)168 static inline void ethtool_link_mode_zero(u32 *dst)
169 {
170 	memset(dst, 0, ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES);
171 }
172 
ethtool_link_mode_is_empty(const u32 * mask)173 static inline bool ethtool_link_mode_is_empty(const u32 *mask)
174 {
175 	unsigned int i;
176 
177 	ethtool_link_mode_for_each_u32(i) {
178 		if (mask[i] != 0)
179 			return false;
180 	}
181 
182 	return true;
183 }
184 
ethtool_link_mode_copy(u32 * dst,const u32 * src)185 static inline void ethtool_link_mode_copy(u32 *dst, const u32 *src)
186 {
187 	memcpy(dst, src, ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES);
188 }
189 
ethtool_link_mode_test_bit(unsigned int nr,const u32 * mask)190 static inline int ethtool_link_mode_test_bit(unsigned int nr, const u32 *mask)
191 {
192 	if (nr >= ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBITS)
193 		return !!0;
194 	return !!(mask[nr / 32] & (1 << (nr % 32)));
195 }
196 
ethtool_link_mode_set_bit(unsigned int nr,u32 * mask)197 static inline int ethtool_link_mode_set_bit(unsigned int nr, u32 *mask)
198 {
199 	if (nr >= ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBITS)
200 		return -1;
201 	mask[nr / 32] |= (1 << (nr % 32));
202 	return 0;
203 }
204 
205 /* Struct for managing module EEPROM pages */
206 struct ethtool_module_eeprom {
207 	u32	offset;
208 	u32	length;
209 	u8	page;
210 	u8	bank;
211 	u8	i2c_address;
212 	u8	*data;
213 };
214 
215 /* Context for sub-commands */
216 struct cmd_context {
217 	const char *devname;	/* net device name */
218 	int fd;			/* socket suitable for ethtool ioctl */
219 	struct ifreq ifr;	/* ifreq suitable for ethtool ioctl */
220 	unsigned int argc;	/* number of arguments to the sub-command */
221 	char **argp;		/* arguments to the sub-command */
222 	unsigned long debug;	/* debugging mask */
223 	bool json;		/* Output JSON, if supported */
224 	bool show_stats;	/* include command-specific stats */
225 #ifdef ETHTOOL_ENABLE_NETLINK
226 	struct nl_context *nlctx;	/* netlink context (opaque) */
227 #endif
228 };
229 
230 #ifdef TEST_ETHTOOL
231 int test_cmdline(const char *args);
232 
233 struct cmd_expect {
234 	const void *cmd;	/* expected command; NULL at end of list */
235 	size_t cmd_len;		/* length to match (might be < sizeof struct) */
236 	int rc;			/* kernel return code */
237 	const void *resp;	/* response to write back; may be NULL */
238 	size_t resp_len;	/* length to write back */
239 };
240 int test_ioctl(const struct cmd_expect *expect, void *cmd);
241 #define TEST_IOCTL_MISMATCH (-2)
242 
243 int test_main(int argc, char **argp);
244 void test_exit(int rc) __attribute__((noreturn));
245 
246 #ifndef TEST_NO_WRAPPERS
247 #define main(...) test_main(__VA_ARGS__)
248 #undef exit
249 #define exit(rc) test_exit(rc)
250 void *test_malloc(size_t size);
251 #undef malloc
252 #define malloc(size) test_malloc(size)
253 void *test_calloc(size_t nmemb, size_t size);
254 #undef calloc
255 #define calloc(nmemb, size) test_calloc(nmemb, size)
256 char *test_strdup(const char *s);
257 #undef strdup
258 #define strdup(s) test_strdup(s)
259 void test_free(void *ptr);
260 #undef free
261 #define free(ptr) test_free(ptr)
262 void *test_realloc(void *ptr, size_t size);
263 #undef realloc
264 #define realloc(ptr, size) test_realloc(ptr, size)
265 int test_open(const char *pathname, int flag, ...);
266 #undef open
267 #define open(...) test_open(__VA_ARGS__)
268 int test_socket(int domain, int type, int protocol);
269 #undef socket
270 #define socket(...) test_socket(__VA_ARGS__)
271 int test_close(int fd);
272 #undef close
273 #define close(fd) test_close(fd)
274 FILE *test_fopen(const char *path, const char *mode);
275 #undef fopen
276 #define fopen(path, mode) test_fopen(path, mode)
277 int test_fclose(FILE *fh);
278 #undef fclose
279 #define fclose(fh) test_fclose(fh)
280 #endif
281 #endif
282 
283 int send_ioctl(struct cmd_context *ctx, void *cmd);
284 
285 void dump_hex(FILE *f, const u8 *data, int len, int offset);
286 
287 /* National Semiconductor DP83815, DP83816 */
288 int natsemi_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
289 int natsemi_dump_eeprom(struct ethtool_drvinfo *info,
290 	struct ethtool_eeprom *ee);
291 
292 /* Digital/Intel 21040 and 21041 */
293 int de2104x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
294 
295 /* Intel(R) PRO/1000 Gigabit Adapter Family */
296 int e1000_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
297 
298 int igb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
299 
300 /* RealTek PCI */
301 int realtek_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
302 
303 /* Intel(R) PRO/100 Fast Ethernet Adapter Family */
304 int e100_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
305 
306 /* Tigon3 */
307 int tg3_dump_eeprom(struct ethtool_drvinfo *info, struct ethtool_eeprom *ee);
308 
309 /* Advanced Micro Devices  AMD8111 based Adapter */
310 int amd8111e_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
311 
312 /* Advanced Micro Devices PCnet32 Adapter */
313 int pcnet32_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
314 
315 /* Motorola 8xx FEC Ethernet controller */
316 int fec_8xx_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
317 
318 /* PowerPC 4xx on-chip Ethernet controller */
319 int ibm_emac_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
320 
321 /* Intel(R) PRO/10GBe Gigabit Adapter Family */
322 int ixgb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
323 
324 int ixgbe_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
325 
326 int ixgbevf_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
327 
328 /* Broadcom Tigon3 Ethernet controller */
329 int tg3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
330 
331 /* SysKonnect Gigabit (Genesis and Yukon) */
332 int skge_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
333 
334 /* SysKonnect Gigabit (Yukon2) */
335 int sky2_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
336 
337 /* Fabric7 VIOC */
338 int vioc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
339 
340 /* SMSC LAN911x/LAN921x embedded ethernet controller */
341 int smsc911x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
342 
343 int at76c50x_usb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
344 
345 /* Solarflare Solarstorm controllers */
346 int sfc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
347 
348 /* STMMAC embedded ethernet controller */
349 int st_mac100_dump_regs(struct ethtool_drvinfo *info,
350 			struct ethtool_regs *regs);
351 int st_gmac_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
352 
353 /* Et131x ethernet controller */
354 int et131x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
355 
356 /* Altera TSE 10/100/1000 ethernet controller */
357 int altera_tse_dump_regs(struct ethtool_drvinfo *info,
358 			 struct ethtool_regs *regs);
359 
360 /* VMware vmxnet3 ethernet controller */
361 int vmxnet3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
362 
363 /* hns3 ethernet controller */
364 int hns3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
365 
366 /* Rx flow classification */
367 int rxclass_parse_ruleopts(struct cmd_context *ctx,
368 			   struct ethtool_rx_flow_spec *fsp, __u32 *rss_context);
369 int rxclass_rule_getall(struct cmd_context *ctx);
370 int rxclass_rule_get(struct cmd_context *ctx, __u32 loc);
371 int rxclass_rule_ins(struct cmd_context *ctx,
372 		     struct ethtool_rx_flow_spec *fsp, __u32 rss_context);
373 int rxclass_rule_del(struct cmd_context *ctx, __u32 loc);
374 
375 /* Module EEPROM parsing code */
376 void sff8079_show_all_ioctl(const __u8 *id);
377 int sff8079_show_all_nl(struct cmd_context *ctx);
378 
379 /* Optics diagnostics */
380 void sff8472_show_all(const __u8 *id);
381 
382 /* QSFP Optics diagnostics */
383 void sff8636_show_all_ioctl(const __u8 *id, __u32 eeprom_len);
384 int sff8636_show_all_nl(struct cmd_context *ctx);
385 
386 /* FUJITSU Extended Socket network device */
387 int fjes_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
388 
389 /* MICROCHIP LAN78XX USB ETHERNET Controller */
390 int lan78xx_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
391 
392 /* Distributed Switch Architecture */
393 int dsa_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
394 
395 /* i.MX Fast Ethernet Controller */
396 int fec_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
397 
398 /* Freescale/NXP ENETC Ethernet Controller */
399 int fsl_enetc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
400 
401 /* Intel(R) Ethernet Controller I225-LM/I225-V adapter family */
402 int igc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
403 
404 /* Broadcom Ethernet Controller */
405 int bnxt_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
406 
407 /* TI CPSW Ethernet Switch */
408 int cpsw_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
409 
410 /* Microchip Ethernet Controller */
411 int lan743x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
412 
413 #endif /* ETHTOOL_INTERNAL_H__ */
414