xref: /linux/net/ethtool/mm.c (revision ab52c59103002b49f2455371e4b9c56ba3ef1781)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2022-2023 NXP
4  */
5 #include "common.h"
6 #include "netlink.h"
7 
8 struct mm_req_info {
9 	struct ethnl_req_info		base;
10 };
11 
12 struct mm_reply_data {
13 	struct ethnl_reply_data		base;
14 	struct ethtool_mm_state		state;
15 	struct ethtool_mm_stats		stats;
16 };
17 
18 #define MM_REPDATA(__reply_base) \
19 	container_of(__reply_base, struct mm_reply_data, base)
20 
21 #define ETHTOOL_MM_STAT_CNT \
22 	(__ETHTOOL_A_MM_STAT_CNT - (ETHTOOL_A_MM_STAT_PAD + 1))
23 
24 const struct nla_policy ethnl_mm_get_policy[ETHTOOL_A_MM_HEADER + 1] = {
25 	[ETHTOOL_A_MM_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy_stats),
26 };
27 
28 static int mm_prepare_data(const struct ethnl_req_info *req_base,
29 			   struct ethnl_reply_data *reply_base,
30 			   const struct genl_info *info)
31 {
32 	struct mm_reply_data *data = MM_REPDATA(reply_base);
33 	struct net_device *dev = reply_base->dev;
34 	const struct ethtool_ops *ops;
35 	int ret;
36 
37 	ops = dev->ethtool_ops;
38 
39 	if (!ops->get_mm)
40 		return -EOPNOTSUPP;
41 
42 	ethtool_stats_init((u64 *)&data->stats,
43 			   sizeof(data->stats) / sizeof(u64));
44 
45 	ret = ethnl_ops_begin(dev);
46 	if (ret < 0)
47 		return ret;
48 
49 	ret = ops->get_mm(dev, &data->state);
50 	if (ret)
51 		goto out_complete;
52 
53 	if (ops->get_mm_stats && (req_base->flags & ETHTOOL_FLAG_STATS))
54 		ops->get_mm_stats(dev, &data->stats);
55 
56 out_complete:
57 	ethnl_ops_complete(dev);
58 
59 	return ret;
60 }
61 
62 static int mm_reply_size(const struct ethnl_req_info *req_base,
63 			 const struct ethnl_reply_data *reply_base)
64 {
65 	int len = 0;
66 
67 	len += nla_total_size(sizeof(u8)); /* _MM_PMAC_ENABLED */
68 	len += nla_total_size(sizeof(u8)); /* _MM_TX_ENABLED */
69 	len += nla_total_size(sizeof(u8)); /* _MM_TX_ACTIVE */
70 	len += nla_total_size(sizeof(u8)); /* _MM_VERIFY_ENABLED */
71 	len += nla_total_size(sizeof(u8)); /* _MM_VERIFY_STATUS */
72 	len += nla_total_size(sizeof(u32)); /* _MM_VERIFY_TIME */
73 	len += nla_total_size(sizeof(u32)); /* _MM_MAX_VERIFY_TIME */
74 	len += nla_total_size(sizeof(u32)); /* _MM_TX_MIN_FRAG_SIZE */
75 	len += nla_total_size(sizeof(u32)); /* _MM_RX_MIN_FRAG_SIZE */
76 
77 	if (req_base->flags & ETHTOOL_FLAG_STATS)
78 		len += nla_total_size(0) + /* _MM_STATS */
79 		       nla_total_size_64bit(sizeof(u64)) * ETHTOOL_MM_STAT_CNT;
80 
81 	return len;
82 }
83 
84 static int mm_put_stat(struct sk_buff *skb, u64 val, u16 attrtype)
85 {
86 	if (val == ETHTOOL_STAT_NOT_SET)
87 		return 0;
88 	if (nla_put_u64_64bit(skb, attrtype, val, ETHTOOL_A_MM_STAT_PAD))
89 		return -EMSGSIZE;
90 	return 0;
91 }
92 
93 static int mm_put_stats(struct sk_buff *skb,
94 			const struct ethtool_mm_stats *stats)
95 {
96 	struct nlattr *nest;
97 
98 	nest = nla_nest_start(skb, ETHTOOL_A_MM_STATS);
99 	if (!nest)
100 		return -EMSGSIZE;
101 
102 	if (mm_put_stat(skb, stats->MACMergeFrameAssErrorCount,
103 			ETHTOOL_A_MM_STAT_REASSEMBLY_ERRORS) ||
104 	    mm_put_stat(skb, stats->MACMergeFrameSmdErrorCount,
105 			ETHTOOL_A_MM_STAT_SMD_ERRORS) ||
106 	    mm_put_stat(skb, stats->MACMergeFrameAssOkCount,
107 			ETHTOOL_A_MM_STAT_REASSEMBLY_OK) ||
108 	    mm_put_stat(skb, stats->MACMergeFragCountRx,
109 			ETHTOOL_A_MM_STAT_RX_FRAG_COUNT) ||
110 	    mm_put_stat(skb, stats->MACMergeFragCountTx,
111 			ETHTOOL_A_MM_STAT_TX_FRAG_COUNT) ||
112 	    mm_put_stat(skb, stats->MACMergeHoldCount,
113 			ETHTOOL_A_MM_STAT_HOLD_COUNT))
114 		goto err_cancel;
115 
116 	nla_nest_end(skb, nest);
117 	return 0;
118 
119 err_cancel:
120 	nla_nest_cancel(skb, nest);
121 	return -EMSGSIZE;
122 }
123 
124 static int mm_fill_reply(struct sk_buff *skb,
125 			 const struct ethnl_req_info *req_base,
126 			 const struct ethnl_reply_data *reply_base)
127 {
128 	const struct mm_reply_data *data = MM_REPDATA(reply_base);
129 	const struct ethtool_mm_state *state = &data->state;
130 
131 	if (nla_put_u8(skb, ETHTOOL_A_MM_TX_ENABLED, state->tx_enabled) ||
132 	    nla_put_u8(skb, ETHTOOL_A_MM_TX_ACTIVE, state->tx_active) ||
133 	    nla_put_u8(skb, ETHTOOL_A_MM_PMAC_ENABLED, state->pmac_enabled) ||
134 	    nla_put_u8(skb, ETHTOOL_A_MM_VERIFY_ENABLED, state->verify_enabled) ||
135 	    nla_put_u8(skb, ETHTOOL_A_MM_VERIFY_STATUS, state->verify_status) ||
136 	    nla_put_u32(skb, ETHTOOL_A_MM_VERIFY_TIME, state->verify_time) ||
137 	    nla_put_u32(skb, ETHTOOL_A_MM_MAX_VERIFY_TIME, state->max_verify_time) ||
138 	    nla_put_u32(skb, ETHTOOL_A_MM_TX_MIN_FRAG_SIZE, state->tx_min_frag_size) ||
139 	    nla_put_u32(skb, ETHTOOL_A_MM_RX_MIN_FRAG_SIZE, state->rx_min_frag_size))
140 		return -EMSGSIZE;
141 
142 	if (req_base->flags & ETHTOOL_FLAG_STATS &&
143 	    mm_put_stats(skb, &data->stats))
144 		return -EMSGSIZE;
145 
146 	return 0;
147 }
148 
149 const struct nla_policy ethnl_mm_set_policy[ETHTOOL_A_MM_MAX + 1] = {
150 	[ETHTOOL_A_MM_HEADER]		= NLA_POLICY_NESTED(ethnl_header_policy),
151 	[ETHTOOL_A_MM_VERIFY_ENABLED]	= NLA_POLICY_MAX(NLA_U8, 1),
152 	[ETHTOOL_A_MM_VERIFY_TIME]	= NLA_POLICY_RANGE(NLA_U32, 1, 128),
153 	[ETHTOOL_A_MM_TX_ENABLED]	= NLA_POLICY_MAX(NLA_U8, 1),
154 	[ETHTOOL_A_MM_PMAC_ENABLED]	= NLA_POLICY_MAX(NLA_U8, 1),
155 	[ETHTOOL_A_MM_TX_MIN_FRAG_SIZE]	= NLA_POLICY_RANGE(NLA_U32, 60, 252),
156 };
157 
158 static void mm_state_to_cfg(const struct ethtool_mm_state *state,
159 			    struct ethtool_mm_cfg *cfg)
160 {
161 	/* We could also compare state->verify_status against
162 	 * ETHTOOL_MM_VERIFY_STATUS_DISABLED, but state->verify_enabled
163 	 * is more like an administrative state which should be seen in
164 	 * ETHTOOL_MSG_MM_GET replies. For example, a port with verification
165 	 * disabled might be in the ETHTOOL_MM_VERIFY_STATUS_INITIAL
166 	 * if it's down.
167 	 */
168 	cfg->verify_enabled = state->verify_enabled;
169 	cfg->verify_time = state->verify_time;
170 	cfg->tx_enabled = state->tx_enabled;
171 	cfg->pmac_enabled = state->pmac_enabled;
172 	cfg->tx_min_frag_size = state->tx_min_frag_size;
173 }
174 
175 static int
176 ethnl_set_mm_validate(struct ethnl_req_info *req_info, struct genl_info *info)
177 {
178 	const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
179 
180 	return ops->get_mm && ops->set_mm ? 1 : -EOPNOTSUPP;
181 }
182 
183 static int ethnl_set_mm(struct ethnl_req_info *req_info, struct genl_info *info)
184 {
185 	struct netlink_ext_ack *extack = info->extack;
186 	struct net_device *dev = req_info->dev;
187 	struct ethtool_mm_state state = {};
188 	struct nlattr **tb = info->attrs;
189 	struct ethtool_mm_cfg cfg = {};
190 	bool mod = false;
191 	int ret;
192 
193 	ret = dev->ethtool_ops->get_mm(dev, &state);
194 	if (ret)
195 		return ret;
196 
197 	mm_state_to_cfg(&state, &cfg);
198 
199 	ethnl_update_bool(&cfg.verify_enabled, tb[ETHTOOL_A_MM_VERIFY_ENABLED],
200 			  &mod);
201 	ethnl_update_u32(&cfg.verify_time, tb[ETHTOOL_A_MM_VERIFY_TIME], &mod);
202 	ethnl_update_bool(&cfg.tx_enabled, tb[ETHTOOL_A_MM_TX_ENABLED], &mod);
203 	ethnl_update_bool(&cfg.pmac_enabled, tb[ETHTOOL_A_MM_PMAC_ENABLED],
204 			  &mod);
205 	ethnl_update_u32(&cfg.tx_min_frag_size,
206 			 tb[ETHTOOL_A_MM_TX_MIN_FRAG_SIZE], &mod);
207 
208 	if (!mod)
209 		return 0;
210 
211 	if (cfg.verify_time > state.max_verify_time) {
212 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MM_VERIFY_TIME],
213 				    "verifyTime exceeds device maximum");
214 		return -ERANGE;
215 	}
216 
217 	if (cfg.verify_enabled && !cfg.tx_enabled) {
218 		NL_SET_ERR_MSG(extack, "Verification requires TX enabled");
219 		return -EINVAL;
220 	}
221 
222 	if (cfg.tx_enabled && !cfg.pmac_enabled) {
223 		NL_SET_ERR_MSG(extack, "TX enabled requires pMAC enabled");
224 		return -EINVAL;
225 	}
226 
227 	ret = dev->ethtool_ops->set_mm(dev, &cfg, extack);
228 	return ret < 0 ? ret : 1;
229 }
230 
231 const struct ethnl_request_ops ethnl_mm_request_ops = {
232 	.request_cmd		= ETHTOOL_MSG_MM_GET,
233 	.reply_cmd		= ETHTOOL_MSG_MM_GET_REPLY,
234 	.hdr_attr		= ETHTOOL_A_MM_HEADER,
235 	.req_info_size		= sizeof(struct mm_req_info),
236 	.reply_data_size	= sizeof(struct mm_reply_data),
237 
238 	.prepare_data		= mm_prepare_data,
239 	.reply_size		= mm_reply_size,
240 	.fill_reply		= mm_fill_reply,
241 
242 	.set_validate		= ethnl_set_mm_validate,
243 	.set			= ethnl_set_mm,
244 	.set_ntf_cmd		= ETHTOOL_MSG_MM_NTF,
245 };
246 
247 /* Returns whether a given device supports the MAC merge layer
248  * (has an eMAC and a pMAC). Must be called under rtnl_lock() and
249  * ethnl_ops_begin().
250  */
251 bool __ethtool_dev_mm_supported(struct net_device *dev)
252 {
253 	const struct ethtool_ops *ops = dev->ethtool_ops;
254 	struct ethtool_mm_state state = {};
255 	int ret = -EOPNOTSUPP;
256 
257 	if (ops && ops->get_mm)
258 		ret = ops->get_mm(dev, &state);
259 
260 	return !ret;
261 }
262 
263 bool ethtool_dev_mm_supported(struct net_device *dev)
264 {
265 	const struct ethtool_ops *ops = dev->ethtool_ops;
266 	bool supported;
267 	int ret;
268 
269 	ASSERT_RTNL();
270 
271 	if (!ops)
272 		return false;
273 
274 	ret = ethnl_ops_begin(dev);
275 	if (ret < 0)
276 		return false;
277 
278 	supported = __ethtool_dev_mm_supported(dev);
279 
280 	ethnl_ops_complete(dev);
281 
282 	return supported;
283 }
284 EXPORT_SYMBOL_GPL(ethtool_dev_mm_supported);
285