xref: /linux/net/ethtool/common.h (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef _ETHTOOL_COMMON_H
4 #define _ETHTOOL_COMMON_H
5 
6 #include <linux/netdevice.h>
7 #include <linux/ethtool.h>
8 
9 #define ETHTOOL_DEV_FEATURE_WORDS	DIV_ROUND_UP(NETDEV_FEATURE_COUNT, 32)
10 
11 /* compose link mode index from speed, type and duplex */
12 #define ETHTOOL_LINK_MODE(speed, type, duplex) \
13 	ETHTOOL_LINK_MODE_ ## speed ## base ## type ## _ ## duplex ## _BIT
14 
15 #define __SOF_TIMESTAMPING_CNT (const_ilog2(SOF_TIMESTAMPING_LAST) + 1)
16 #define __HWTSTAMP_FLAG_CNT (const_ilog2(HWTSTAMP_FLAG_LAST) + 1)
17 
18 struct genl_info;
19 struct hwtstamp_provider_desc;
20 
21 extern const char
22 netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN];
23 extern const char
24 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN];
25 extern const char
26 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN];
27 extern const char
28 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN];
29 extern const char link_mode_names[][ETH_GSTRING_LEN];
30 extern const char netif_msg_class_names[][ETH_GSTRING_LEN];
31 extern const char wol_mode_names[][ETH_GSTRING_LEN];
32 extern const char sof_timestamping_names[][ETH_GSTRING_LEN];
33 extern const char ts_tx_type_names[][ETH_GSTRING_LEN];
34 extern const char ts_rx_filter_names[][ETH_GSTRING_LEN];
35 extern const char ts_flags_names[][ETH_GSTRING_LEN];
36 extern const char udp_tunnel_type_names[][ETH_GSTRING_LEN];
37 
38 int __ethtool_get_link(struct net_device *dev);
39 
40 bool convert_legacy_settings_to_link_ksettings(
41 	struct ethtool_link_ksettings *link_ksettings,
42 	const struct ethtool_cmd *legacy_settings);
43 int ethtool_check_max_channel(struct net_device *dev,
44 			      struct ethtool_channels channels,
45 			      struct genl_info *info);
46 struct ethtool_rxfh_context *
47 ethtool_rxfh_ctx_alloc(const struct ethtool_ops *ops,
48 		       u32 indir_size, u32 key_size);
49 int ethtool_check_rss_ctx_busy(struct net_device *dev, u32 rss_context);
50 int ethtool_rxfh_config_is_sym(u64 rxfh);
51 
52 void ethtool_ringparam_get_cfg(struct net_device *dev,
53 			       struct ethtool_ringparam *param,
54 			       struct kernel_ethtool_ringparam *kparam,
55 			       struct netlink_ext_ack *extack);
56 void ethtool_ringparam_set_cfg(struct netdev_config *cfg,
57 			       const struct ethtool_ringparam *param);
58 
59 int ethtool_get_rx_ring_count(struct net_device *dev);
60 
61 int __ethtool_get_ts_info(struct net_device *dev, struct kernel_ethtool_ts_info *info);
62 int ethtool_get_ts_info_by_phc(struct net_device *dev,
63 			       struct kernel_ethtool_ts_info *info,
64 			       struct hwtstamp_provider_desc *hwprov_desc);
65 int ethtool_net_get_ts_info_by_phc(struct net_device *dev,
66 				   struct kernel_ethtool_ts_info *info,
67 				   struct hwtstamp_provider_desc *hwprov_desc);
68 struct phy_device *
69 ethtool_phy_get_ts_info_by_phc(struct net_device *dev,
70 			       struct kernel_ethtool_ts_info *info,
71 			       struct hwtstamp_provider_desc *hwprov_desc);
72 bool net_support_hwtstamp_qualifier(struct net_device *dev,
73 				    enum hwtstamp_provider_qualifier qualifier);
74 
75 extern const struct ethtool_phy_ops *ethtool_phy_ops;
76 extern const struct ethtool_pse_ops *ethtool_pse_ops;
77 
78 int ethtool_get_module_info_call(struct net_device *dev,
79 				 struct ethtool_modinfo *modinfo);
80 int ethtool_get_module_eeprom_call(struct net_device *dev,
81 				   struct ethtool_eeprom *ee, u8 *data);
82 
83 bool __ethtool_dev_mm_supported(struct net_device *dev);
84 
85 /**
86  * ethtool_nl_msg_needs_rtnl() - does this Netlink cmd need rtnl_lock?
87  * @dev: target device
88  * @cmd: ETHTOOL_MSG_* Netlink command value
89  *
90  * Return: true if @cmd is a command for which @dev has opted-in to
91  * keeping rtnl_lock held across the call (via op_needs_rtnl).
92  */
93 static inline bool
94 ethtool_nl_msg_needs_rtnl(const struct net_device *dev, u8 cmd)
95 {
96 	const struct ethtool_ops *ops = dev->ethtool_ops;
97 
98 	switch (cmd) {
99 	case ETHTOOL_MSG_LINKINFO_GET:
100 	case ETHTOOL_MSG_LINKINFO_SET:
101 	case ETHTOOL_MSG_LINKMODES_GET:
102 	case ETHTOOL_MSG_LINKMODES_SET:
103 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_LINKSETTINGS;
104 	case ETHTOOL_MSG_PRIVFLAGS_SET:
105 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SPFLAGS;
106 	case ETHTOOL_MSG_RINGS_SET:
107 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SRINGPARAM;
108 	case ETHTOOL_MSG_CHANNELS_SET:
109 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SCHANNELS;
110 	case ETHTOOL_MSG_COALESCE_SET:
111 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SCOALESCE;
112 	case ETHTOOL_MSG_PAUSE_GET:
113 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_GPAUSEPARAM;
114 	case ETHTOOL_MSG_PAUSE_SET:
115 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SPAUSEPARAM;
116 	case ETHTOOL_MSG_RSS_SET:
117 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_RSS;
118 	case ETHTOOL_MSG_LINKSTATE_GET:
119 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_GLINK;
120 	case ETHTOOL_MSG_TSCONFIG_GET:
121 	case ETHTOOL_MSG_TSCONFIG_SET:
122 		/* tsconfig calls ndos (ndo_hwtstamp_set/get), not ethtool ops.
123 		 * Also, there is no corresponding ethtool ioctl, therefore
124 		 * these cases are Netlink-only.
125 		 */
126 		return true;
127 	}
128 	return false;
129 }
130 
131 /**
132  * ethtool_ioctl_needs_rtnl() - does this legacy ioctl cmd need rtnl_lock?
133  * @dev: target device
134  * @ethcmd: ETHTOOL_* ioctl command value
135  *
136  * Return: true if @ethcmd is a command for which @dev has opted-in to
137  * keeping rtnl_lock held across the call (via op_needs_rtnl).
138  */
139 static inline bool
140 ethtool_ioctl_needs_rtnl(const struct net_device *dev, u32 ethcmd)
141 {
142 	const struct ethtool_ops *ops = dev->ethtool_ops;
143 
144 	switch (ethcmd) {
145 	case ETHTOOL_GLINKSETTINGS:
146 	case ETHTOOL_GSET:
147 	case ETHTOOL_SLINKSETTINGS:
148 	case ETHTOOL_SSET:
149 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_LINKSETTINGS;
150 	case ETHTOOL_SPFLAGS:
151 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SPFLAGS;
152 	case ETHTOOL_SRINGPARAM:
153 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SRINGPARAM;
154 	case ETHTOOL_SCHANNELS:
155 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SCHANNELS;
156 	case ETHTOOL_SCOALESCE:
157 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SCOALESCE;
158 	case ETHTOOL_GPAUSEPARAM:
159 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_GPAUSEPARAM;
160 	case ETHTOOL_SPAUSEPARAM:
161 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_SPAUSEPARAM;
162 	case ETHTOOL_SRSSH:
163 	case ETHTOOL_SRXFH:
164 	case ETHTOOL_SRXFHINDIR:
165 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_RSS;
166 	case ETHTOOL_GLINK:
167 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_GLINK;
168 	case ETHTOOL_TEST:
169 		return ops->op_needs_rtnl & ETHTOOL_OP_NEEDS_RTNL_TEST;
170 	}
171 	return false;
172 }
173 
174 #if IS_ENABLED(CONFIG_ETHTOOL_NETLINK)
175 void ethtool_rss_notify(struct net_device *dev, u32 type, u32 rss_context);
176 #else
177 static inline void
178 ethtool_rss_notify(struct net_device *dev, u32 type, u32 rss_context)
179 {
180 }
181 #endif
182 
183 #endif /* _ETHTOOL_COMMON_H */
184