1 /* Portions Copyright 2001 Sun Microsystems (thockin@sun.com) */
2 /* Portions Copyright 2002 Intel (scott.feldman@intel.com) */
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 #include <net/if.h>
25
26 #define maybe_unused __attribute__((__unused__))
27
28 /* ethtool.h expects these to be defined by <linux/types.h> */
29 #ifndef HAVE_BE_TYPES
30 typedef uint16_t __be16;
31 typedef uint32_t __be32;
32 typedef unsigned long long __be64;
33 #endif
34
35 typedef unsigned long long u64;
36 typedef uint32_t u32;
37 typedef uint16_t u16;
38 typedef uint8_t u8;
39 typedef int32_t s32;
40
41 /* ethtool.h epxects __KERNEL_DIV_ROUND_UP to be defined by <linux/kernel.h> */
42 #include <linux/kernel.h>
43 #ifndef __KERNEL_DIV_ROUND_UP
44 #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
45 #endif
46
47 #include "ethtool-copy.h"
48 #include "net_tstamp-copy.h"
49
50 #if __BYTE_ORDER == __BIG_ENDIAN
cpu_to_be16(u16 value)51 static inline u16 cpu_to_be16(u16 value)
52 {
53 return value;
54 }
cpu_to_be32(u32 value)55 static inline u32 cpu_to_be32(u32 value)
56 {
57 return value;
58 }
cpu_to_be64(u64 value)59 static inline u64 cpu_to_be64(u64 value)
60 {
61 return value;
62 }
63 #else
cpu_to_be16(u16 value)64 static inline u16 cpu_to_be16(u16 value)
65 {
66 return (value >> 8) | (value << 8);
67 }
cpu_to_be32(u32 value)68 static inline u32 cpu_to_be32(u32 value)
69 {
70 return cpu_to_be16(value >> 16) | (cpu_to_be16(value) << 16);
71 }
cpu_to_be64(u64 value)72 static inline u64 cpu_to_be64(u64 value)
73 {
74 return cpu_to_be32(value >> 32) | ((u64)cpu_to_be32(value) << 32);
75 }
76 #endif
77
78 #define ntohll cpu_to_be64
79 #define htonll cpu_to_be64
80
81 #define BITS_PER_BYTE 8
82 #define BITS_PER_LONG (BITS_PER_BYTE * sizeof(long))
83 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
84 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)
85
set_bit(unsigned int nr,unsigned long * addr)86 static inline void set_bit(unsigned int nr, unsigned long *addr)
87 {
88 addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
89 }
90
clear_bit(unsigned int nr,unsigned long * addr)91 static inline void clear_bit(unsigned int nr, unsigned long *addr)
92 {
93 addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
94 }
95
test_bit(unsigned int nr,const unsigned long * addr)96 static inline int test_bit(unsigned int nr, const unsigned long *addr)
97 {
98 return !!((1UL << (nr % BITS_PER_LONG)) &
99 (((unsigned long *)addr)[nr / BITS_PER_LONG]));
100 }
101
102 #ifndef ARRAY_SIZE
103 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
104 #endif
105
106 #ifndef SIOCETHTOOL
107 #define SIOCETHTOOL 0x8946
108 #endif
109
110 /* Internal values for old-style offload flags. Values and names
111 * must not clash with the flags defined for ETHTOOL_{G,S}FLAGS.
112 */
113 #define ETH_FLAG_RXCSUM (1 << 0)
114 #define ETH_FLAG_TXCSUM (1 << 1)
115 #define ETH_FLAG_SG (1 << 2)
116 #define ETH_FLAG_TSO (1 << 3)
117 #define ETH_FLAG_UFO (1 << 4)
118 #define ETH_FLAG_GSO (1 << 5)
119 #define ETH_FLAG_GRO (1 << 6)
120 #define ETH_FLAG_INT_MASK (ETH_FLAG_RXCSUM | ETH_FLAG_TXCSUM | \
121 ETH_FLAG_SG | ETH_FLAG_TSO | ETH_FLAG_UFO | \
122 ETH_FLAG_GSO | ETH_FLAG_GRO),
123 /* Mask of all flags defined for ETHTOOL_{G,S}FLAGS. */
124 #define ETH_FLAG_EXT_MASK (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | \
125 ETH_FLAG_TXVLAN | ETH_FLAG_NTUPLE | \
126 ETH_FLAG_RXHASH)
127
128 /* internal API for link mode bitmap interaction with kernel. */
129
130 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32 \
131 (SCHAR_MAX)
132 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBITS \
133 (32 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32)
134 #define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES \
135 (4 * ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32)
136 #define ETHTOOL_DECLARE_LINK_MODE_MASK(name) \
137 u32 name[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]
138
139 struct ethtool_link_usettings {
140 struct {
141 __u8 transceiver;
142 } deprecated;
143 struct ethtool_link_settings base;
144 struct {
145 ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
146 ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
147 ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
148 } link_modes;
149 };
150
151 #define ethtool_link_mode_for_each_u32(index) \
152 for ((index) = 0; \
153 (index) < ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32; \
154 ++(index))
155
ethtool_link_mode_zero(u32 * dst)156 static inline void ethtool_link_mode_zero(u32 *dst)
157 {
158 memset(dst, 0, ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES);
159 }
160
ethtool_link_mode_is_empty(const u32 * mask)161 static inline bool ethtool_link_mode_is_empty(const u32 *mask)
162 {
163 unsigned int i;
164
165 ethtool_link_mode_for_each_u32(i) {
166 if (mask[i] != 0)
167 return false;
168 }
169
170 return true;
171 }
172
ethtool_link_mode_copy(u32 * dst,const u32 * src)173 static inline void ethtool_link_mode_copy(u32 *dst, const u32 *src)
174 {
175 memcpy(dst, src, ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES);
176 }
177
ethtool_link_mode_test_bit(unsigned int nr,const u32 * mask)178 static inline int ethtool_link_mode_test_bit(unsigned int nr, const u32 *mask)
179 {
180 if (nr >= ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBITS)
181 return !!0;
182 return !!(mask[nr / 32] & (1 << (nr % 32)));
183 }
184
ethtool_link_mode_set_bit(unsigned int nr,u32 * mask)185 static inline int ethtool_link_mode_set_bit(unsigned int nr, u32 *mask)
186 {
187 if (nr >= ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBITS)
188 return -1;
189 mask[nr / 32] |= (1 << (nr % 32));
190 return 0;
191 }
192
193 /* Context for sub-commands */
194 struct cmd_context {
195 const char *devname; /* net device name */
196 int fd; /* socket suitable for ethtool ioctl */
197 struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
198 int argc; /* number of arguments to the sub-command */
199 char **argp; /* arguments to the sub-command */
200 };
201
202 #ifdef TEST_ETHTOOL
203 int test_cmdline(const char *args);
204
205 struct cmd_expect {
206 const void *cmd; /* expected command; NULL at end of list */
207 size_t cmd_len; /* length to match (might be < sizeof struct) */
208 int rc; /* kernel return code */
209 const void *resp; /* response to write back; may be NULL */
210 size_t resp_len; /* length to write back */
211 };
212 int test_ioctl(const struct cmd_expect *expect, void *cmd);
213 #define TEST_IOCTL_MISMATCH (-2)
214
215 int test_main(int argc, char **argp);
216 void test_exit(int rc) __attribute__((noreturn));
217
218 #ifndef TEST_NO_WRAPPERS
219 #define main(...) test_main(__VA_ARGS__)
220 #undef exit
221 #define exit(rc) test_exit(rc)
222 void *test_malloc(size_t size);
223 #undef malloc
224 #define malloc(size) test_malloc(size)
225 void *test_calloc(size_t nmemb, size_t size);
226 #undef calloc
227 #define calloc(nmemb, size) test_calloc(nmemb, size)
228 char *test_strdup(const char *s);
229 #undef strdup
230 #define strdup(s) test_strdup(s)
231 void test_free(void *ptr);
232 #undef free
233 #define free(ptr) test_free(ptr)
234 void *test_realloc(void *ptr, size_t size);
235 #undef realloc
236 #define realloc(ptr, size) test_realloc(ptr, size)
237 int test_open(const char *pathname, int flag, ...);
238 #undef open
239 #define open(...) test_open(__VA_ARGS__)
240 int test_socket(int domain, int type, int protocol);
241 #undef socket
242 #define socket(...) test_socket(__VA_ARGS__)
243 int test_close(int fd);
244 #undef close
245 #define close(fd) test_close(fd)
246 FILE *test_fopen(const char *path, const char *mode);
247 #undef fopen
248 #define fopen(path, mode) test_fopen(path, mode)
249 int test_fclose(FILE *fh);
250 #undef fclose
251 #define fclose(fh) test_fclose(fh)
252 #endif
253 #endif
254
255 int send_ioctl(struct cmd_context *ctx, void *cmd);
256
257 void dump_hex(FILE *f, const u8 *data, int len, int offset);
258
259 /* National Semiconductor DP83815, DP83816 */
260 int natsemi_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
261 int natsemi_dump_eeprom(struct ethtool_drvinfo *info,
262 struct ethtool_eeprom *ee);
263
264 /* Digital/Intel 21040 and 21041 */
265 int de2104x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
266
267 /* Intel(R) PRO/1000 Gigabit Adapter Family */
268 int e1000_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
269
270 int igb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
271
272 /* RealTek PCI */
273 int realtek_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
274
275 /* Intel(R) PRO/100 Fast Ethernet Adapter Family */
276 int e100_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
277
278 /* Tigon3 */
279 int tg3_dump_eeprom(struct ethtool_drvinfo *info, struct ethtool_eeprom *ee);
280
281 /* Advanced Micro Devices AMD8111 based Adapter */
282 int amd8111e_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
283
284 /* Advanced Micro Devices PCnet32 Adapter */
285 int pcnet32_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
286
287 /* Motorola 8xx FEC Ethernet controller */
288 int fec_8xx_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
289
290 /* PowerPC 4xx on-chip Ethernet controller */
291 int ibm_emac_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
292
293 /* Intel(R) PRO/10GBe Gigabit Adapter Family */
294 int ixgb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
295
296 int ixgbe_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
297
298 int ixgbevf_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
299
300 /* Broadcom Tigon3 Ethernet controller */
301 int tg3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
302
303 /* SysKonnect Gigabit (Genesis and Yukon) */
304 int skge_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
305
306 /* SysKonnect Gigabit (Yukon2) */
307 int sky2_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
308
309 /* Fabric7 VIOC */
310 int vioc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
311
312 /* SMSC LAN911x/LAN921x embedded ethernet controller */
313 int smsc911x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
314
315 int at76c50x_usb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
316
317 /* Solarflare Solarstorm controllers */
318 int sfc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
319
320 /* STMMAC embedded ethernet controller */
321 int st_mac100_dump_regs(struct ethtool_drvinfo *info,
322 struct ethtool_regs *regs);
323 int st_gmac_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
324
325 /* Et131x ethernet controller */
326 int et131x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
327
328 /* Altera TSE 10/100/1000 ethernet controller */
329 int altera_tse_dump_regs(struct ethtool_drvinfo *info,
330 struct ethtool_regs *regs);
331
332 /* VMware vmxnet3 ethernet controller */
333 int vmxnet3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
334
335 /* Rx flow classification */
336 int rxclass_parse_ruleopts(struct cmd_context *ctx,
337 struct ethtool_rx_flow_spec *fsp, __u32 *rss_context);
338 int rxclass_rule_getall(struct cmd_context *ctx);
339 int rxclass_rule_get(struct cmd_context *ctx, __u32 loc);
340 int rxclass_rule_ins(struct cmd_context *ctx,
341 struct ethtool_rx_flow_spec *fsp, __u32 rss_context);
342 int rxclass_rule_del(struct cmd_context *ctx, __u32 loc);
343
344 /* Module EEPROM parsing code */
345 void sff8079_show_all(const __u8 *id);
346
347 /* Optics diagnostics */
348 void sff8472_show_all(const __u8 *id);
349
350 /* QSFP Optics diagnostics */
351 void sff8636_show_all(const __u8 *id, __u32 eeprom_len);
352
353 /* FUJITSU Extended Socket network device */
354 int fjes_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
355
356 /* MICROCHIP LAN78XX USB ETHERNET Controller */
357 int lan78xx_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
358
359 /* Distributed Switch Architecture */
360 int dsa_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
361
362 /* i.MX Fast Ethernet Controller */
363 int fec_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
364
365 #endif /* ETHTOOL_INTERNAL_H__ */
366