blob: ff52c6e7660ca939f78a7c29582e84c4f7084b77 [file] [log] [blame]
Jeff Garzik32c80372005-10-25 01:56:48 -04001/* Portions Copyright 2001 Sun Microsystems (thockin@sun.com) */
2/* Portions Copyright 2002 Intel (scott.feldman@intel.com) */
Ben Hutchings5ba70c02011-11-01 16:48:31 +00003#ifndef ETHTOOL_INTERNAL_H__
4#define ETHTOOL_INTERNAL_H__
Jeff Garzik32c80372005-10-25 01:56:48 -04005
Maciej Żenczykowskic0a2c042016-03-11 09:58:14 -08006/* 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
Alexander Duyck8a7c0702011-05-04 11:41:40 -070012#ifdef HAVE_CONFIG_H
13#include "ethtool-config.h"
14#endif
David Decotigny33133ab2016-03-25 09:21:01 -070015#include <stdbool.h>
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +000016#include <stdio.h>
Paul Barker1e8811a2014-10-26 11:44:09 +000017#include <stdint.h>
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +000018#include <stdlib.h>
19#include <string.h>
Jeff Garzik32c80372005-10-25 01:56:48 -040020#include <sys/types.h>
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +000021#include <unistd.h>
Ben Hutchings18caa782010-06-25 15:46:58 +010022#include <endian.h>
Alexander Duyck8a7c0702011-05-04 11:41:40 -070023#include <sys/ioctl.h>
24#include <net/if.h>
Jeff Garzikf45bb4f2007-07-26 13:32:17 -040025
Maciej Żenczykowskid5432a92019-10-17 11:20:50 -070026#define maybe_unused __attribute__((__unused__))
27
Ben Hutchings4f1d68d2010-11-23 15:46:55 +000028/* ethtool.h expects these to be defined by <linux/types.h> */
29#ifndef HAVE_BE_TYPES
Paul Barker1e8811a2014-10-26 11:44:09 +000030typedef uint16_t __be16;
31typedef uint32_t __be32;
Alexander Duyck9a7baa62011-05-04 11:41:46 -070032typedef unsigned long long __be64;
Ben Hutchings4f1d68d2010-11-23 15:46:55 +000033#endif
34
Jeff Garzikf45bb4f2007-07-26 13:32:17 -040035typedef unsigned long long u64;
Paul Barker1e8811a2014-10-26 11:44:09 +000036typedef uint32_t u32;
37typedef uint16_t u16;
38typedef uint8_t u8;
39typedef int32_t s32;
Ben Hutchings18caa782010-06-25 15:46:58 +010040
Ben Hutchings4537f8e2016-03-13 15:43:29 +000041/* 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
Alexander Duyck8a7c0702011-05-04 11:41:40 -070047#include "ethtool-copy.h"
Richard Cochran0fdcc8c2012-04-03 19:58:23 +020048#include "net_tstamp-copy.h"
Alexander Duyck8a7c0702011-05-04 11:41:40 -070049
Ben Hutchings18caa782010-06-25 15:46:58 +010050#if __BYTE_ORDER == __BIG_ENDIAN
51static inline u16 cpu_to_be16(u16 value)
52{
Alexander Duyck9a7baa62011-05-04 11:41:46 -070053 return value;
Ben Hutchings18caa782010-06-25 15:46:58 +010054}
55static inline u32 cpu_to_be32(u32 value)
56{
Alexander Duyck9a7baa62011-05-04 11:41:46 -070057 return value;
58}
59static inline u64 cpu_to_be64(u64 value)
60{
61 return value;
Ben Hutchings18caa782010-06-25 15:46:58 +010062}
63#else
64static inline u16 cpu_to_be16(u16 value)
65{
66 return (value >> 8) | (value << 8);
67}
68static inline u32 cpu_to_be32(u32 value)
69{
70 return cpu_to_be16(value >> 16) | (cpu_to_be16(value) << 16);
71}
Alexander Duyck9a7baa62011-05-04 11:41:46 -070072static inline u64 cpu_to_be64(u64 value)
73{
74 return cpu_to_be32(value >> 32) | ((u64)cpu_to_be32(value) << 32);
75}
Ben Hutchings18caa782010-06-25 15:46:58 +010076#endif
Jeff Garzikf45bb4f2007-07-26 13:32:17 -040077
Alexander Duyck9a7baa62011-05-04 11:41:46 -070078#define ntohll cpu_to_be64
79#define htonll cpu_to_be64
80
Ben Hutchings5ba70c02011-11-01 16:48:31 +000081#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
Ben Hutchingsbe76a452011-10-27 19:11:35 +010086static inline void set_bit(unsigned int nr, unsigned long *addr)
Ben Hutchings5ba70c02011-11-01 16:48:31 +000087{
88 addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
89}
90
Ben Hutchingsbe76a452011-10-27 19:11:35 +010091static inline void clear_bit(unsigned int nr, unsigned long *addr)
Ben Hutchings5ba70c02011-11-01 16:48:31 +000092{
93 addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
94}
95
96static 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
Alexander Duyck8a7c0702011-05-04 11:41:40 -0700102#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
Ben Hutchingse5c984a2011-05-13 23:25:26 +0100110/* 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
David Decotigny33133ab2016-03-25 09:21:01 -0700128/* 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
139struct 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
156static inline void ethtool_link_mode_zero(u32 *dst)
157{
158 memset(dst, 0, ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NBYTES);
159}
160
161static 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
173static 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
178static 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
185static 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
Ben Hutchings37897ca2011-10-29 02:33:30 +0100193/* Context for sub-commands */
194struct cmd_context {
Ben Hutchings743eb0b2011-10-29 02:36:48 +0100195 const char *devname; /* net device name */
Ben Hutchings37897ca2011-10-29 02:33:30 +0100196 int fd; /* socket suitable for ethtool ioctl */
197 struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
Ben Hutchings127f8062011-10-29 01:15:34 +0100198 int argc; /* number of arguments to the sub-command */
199 char **argp; /* arguments to the sub-command */
Ben Hutchings37897ca2011-10-29 02:33:30 +0100200};
201
Ben Hutchingscfe91ed2011-10-31 18:42:29 +0000202#ifdef TEST_ETHTOOL
203int test_cmdline(const char *args);
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +0000204
Ben Hutchings70943b52012-06-01 23:08:21 +0100205struct 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};
212int test_ioctl(const struct cmd_expect *expect, void *cmd);
213#define TEST_IOCTL_MISMATCH (-2)
214
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +0000215int test_main(int argc, char **argp);
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +0000216void test_exit(int rc) __attribute__((noreturn));
Ben Hutchings0732e9a2015-10-09 03:59:30 +0100217
218#ifndef TEST_NO_WRAPPERS
219#define main(...) test_main(__VA_ARGS__)
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +0000220#undef exit
221#define exit(rc) test_exit(rc)
222void *test_malloc(size_t size);
223#undef malloc
224#define malloc(size) test_malloc(size)
225void *test_calloc(size_t nmemb, size_t size);
226#undef calloc
227#define calloc(nmemb, size) test_calloc(nmemb, size)
228char *test_strdup(const char *s);
229#undef strdup
230#define strdup(s) test_strdup(s)
Ben Hutchings89a949e2015-10-09 04:01:18 +0100231void test_free(void *ptr);
Ben Hutchingsdfacc4a2011-10-31 18:29:35 +0000232#undef free
233#define free(ptr) test_free(ptr)
234void *test_realloc(void *ptr, size_t size);
235#undef realloc
236#define realloc(ptr, size) test_realloc(ptr, size)
237int test_open(const char *pathname, int flag, ...);
238#undef open
239#define open(...) test_open(__VA_ARGS__)
240int test_socket(int domain, int type, int protocol);
241#undef socket
242#define socket(...) test_socket(__VA_ARGS__)
243int test_close(int fd);
244#undef close
245#define close(fd) test_close(fd)
246FILE *test_fopen(const char *path, const char *mode);
247#undef fopen
248#define fopen(path, mode) test_fopen(path, mode)
249int test_fclose(FILE *fh);
250#undef fclose
251#define fclose(fh) test_fclose(fh)
252#endif
Ben Hutchingscfe91ed2011-10-31 18:42:29 +0000253#endif
254
Ben Hutchings37897ca2011-10-29 02:33:30 +0100255int send_ioctl(struct cmd_context *ctx, void *cmd);
256
Ben Hutchings2038e9e2012-02-17 21:30:06 +0000257void dump_hex(FILE *f, const u8 *data, int len, int offset);
258
Jeff Garzik32c80372005-10-25 01:56:48 -0400259/* National Semiconductor DP83815, DP83816 */
260int natsemi_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
261int natsemi_dump_eeprom(struct ethtool_drvinfo *info,
262 struct ethtool_eeprom *ee);
263
264/* Digital/Intel 21040 and 21041 */
265int de2104x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
266
267/* Intel(R) PRO/1000 Gigabit Adapter Family */
268int e1000_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
269
Nicholas Nunleyae5505e2007-08-15 10:44:01 -0700270int igb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
271
Jeff Garzik32c80372005-10-25 01:56:48 -0400272/* RealTek PCI */
273int realtek_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
274
275/* Intel(R) PRO/100 Fast Ethernet Adapter Family */
276int e100_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
277
278/* Tigon3 */
279int tg3_dump_eeprom(struct ethtool_drvinfo *info, struct ethtool_eeprom *ee);
280
281/* Advanced Micro Devices AMD8111 based Adapter */
282int amd8111e_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
283
284/* Advanced Micro Devices PCnet32 Adapter */
285int pcnet32_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
286
287/* Motorola 8xx FEC Ethernet controller */
288int fec_8xx_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
289
Eugene Surovegina69768a2005-10-25 02:29:27 -0400290/* PowerPC 4xx on-chip Ethernet controller */
291int ibm_emac_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
292
Nicholas Nunleyfb522ac2006-09-19 11:27:35 -0700293/* Intel(R) PRO/10GBe Gigabit Adapter Family */
294int ixgb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
295
Nicholas Nunley4b448ec2007-08-15 10:44:11 -0700296int ixgbe_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
297
Jacob Keller5c271a22013-08-27 17:08:49 -0700298int ixgbevf_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
299
Michael Chan14004d12005-11-08 12:24:23 -0800300/* Broadcom Tigon3 Ethernet controller */
301int tg3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
302
Stephen Hemminger8f3b48b2006-08-23 14:16:09 -0700303/* SysKonnect Gigabit (Genesis and Yukon) */
304int skge_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
305
Stephen Hemmingerad702592006-09-26 13:09:14 -0700306/* SysKonnect Gigabit (Yukon2) */
307int sky2_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
308
Sriram Chidambaram8babd792006-10-24 14:30:41 -0700309/* Fabric7 VIOC */
310int vioc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
Steve Glendinninga5f8ce22007-07-16 20:04:40 +0100311
312/* SMSC LAN911x/LAN921x embedded ethernet controller */
313int smsc911x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
314
John W. Linvillec1af4412009-12-23 09:29:47 -0500315int at76c50x_usb_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
316
Ben Hutchings2c2ee7a2010-06-21 14:09:47 +0100317/* Solarflare Solarstorm controllers */
318int sfc_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
319
Giuseppe CAVALLARO02ea0ac2010-10-13 10:52:25 +0200320/* STMMAC embedded ethernet controller */
321int st_mac100_dump_regs(struct ethtool_drvinfo *info,
322 struct ethtool_regs *regs);
323int st_gmac_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
Alexander Duyck8d63f722011-05-04 11:41:51 -0700324
Mark Einon08f7d492012-10-30 21:57:23 +0000325/* Et131x ethernet controller */
326int et131x_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
327
Vince Bridgers0bbd4f52014-06-22 19:16:59 -0500328/* Altera TSE 10/100/1000 ethernet controller */
329int altera_tse_dump_regs(struct ethtool_drvinfo *info,
330 struct ethtool_regs *regs);
331
Shrikrishna Khare009799a2015-09-23 15:19:12 -0700332/* VMware vmxnet3 ethernet controller */
333int vmxnet3_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
334
Alexander Duyck8d63f722011-05-04 11:41:51 -0700335/* Rx flow classification */
Ben Hutchings127f8062011-10-29 01:15:34 +0100336int rxclass_parse_ruleopts(struct cmd_context *ctx,
Edward Creef5d55b92018-03-09 15:04:12 +0000337 struct ethtool_rx_flow_spec *fsp, __u32 *rss_context);
Ben Hutchings37897ca2011-10-29 02:33:30 +0100338int rxclass_rule_getall(struct cmd_context *ctx);
339int rxclass_rule_get(struct cmd_context *ctx, __u32 loc);
340int rxclass_rule_ins(struct cmd_context *ctx,
Edward Creef5d55b92018-03-09 15:04:12 +0000341 struct ethtool_rx_flow_spec *fsp, __u32 rss_context);
Ben Hutchings37897ca2011-10-29 02:33:30 +0100342int rxclass_rule_del(struct cmd_context *ctx, __u32 loc);
Alexander Duyck8d63f722011-05-04 11:41:51 -0700343
Stuart Hodgson2edf5672012-05-18 15:58:45 +0100344/* Module EEPROM parsing code */
345void sff8079_show_all(const __u8 *id);
346
Aurelien Guillaume749f3872012-12-02 21:21:01 +0100347/* Optics diagnostics */
348void sff8472_show_all(const __u8 *id);
349
Vidya Sagar Ravipatia5e73bb2016-08-23 06:30:32 -0700350/* QSFP Optics diagnostics */
351void sff8636_show_all(const __u8 *id, __u32 eeprom_len);
352
Taku Izumiacc3d3a2016-11-16 09:55:32 +0900353/* FUJITSU Extended Socket network device */
354int fjes_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
Raghuram Chary Jbf668742018-04-05 11:41:28 +0530355
356/* MICROCHIP LAN78XX USB ETHERNET Controller */
357int lan78xx_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
358
Vivien Didelotcb8e9802018-12-18 14:06:35 -0500359/* Distributed Switch Architecture */
360int dsa_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
361
Vivien Didelot8612d8b2019-02-14 11:15:36 -0500362/* i.MX Fast Ethernet Controller */
363int fec_dump_regs(struct ethtool_drvinfo *info, struct ethtool_regs *regs);
364
Ben Hutchings5ba70c02011-11-01 16:48:31 +0000365#endif /* ETHTOOL_INTERNAL_H__ */