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(stats: (u64 *)&data->stats, |
43 | n: 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(payload: sizeof(u8)); /* _MM_PMAC_ENABLED */ |
68 | len += nla_total_size(payload: sizeof(u8)); /* _MM_TX_ENABLED */ |
69 | len += nla_total_size(payload: sizeof(u8)); /* _MM_TX_ACTIVE */ |
70 | len += nla_total_size(payload: sizeof(u8)); /* _MM_VERIFY_ENABLED */ |
71 | len += nla_total_size(payload: sizeof(u8)); /* _MM_VERIFY_STATUS */ |
72 | len += nla_total_size(payload: sizeof(u32)); /* _MM_VERIFY_TIME */ |
73 | len += nla_total_size(payload: sizeof(u32)); /* _MM_MAX_VERIFY_TIME */ |
74 | len += nla_total_size(payload: sizeof(u32)); /* _MM_TX_MIN_FRAG_SIZE */ |
75 | len += nla_total_size(payload: sizeof(u32)); /* _MM_RX_MIN_FRAG_SIZE */ |
76 | |
77 | if (req_base->flags & ETHTOOL_FLAG_STATS) |
78 | len += nla_total_size(payload: 0) + /* _MM_STATS */ |
79 | nla_total_size_64bit(payload: 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, value: val, padattr: 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, attrtype: ETHTOOL_A_MM_STATS); |
99 | if (!nest) |
100 | return -EMSGSIZE; |
101 | |
102 | if (mm_put_stat(skb, val: stats->MACMergeFrameAssErrorCount, |
103 | attrtype: ETHTOOL_A_MM_STAT_REASSEMBLY_ERRORS) || |
104 | mm_put_stat(skb, val: stats->MACMergeFrameSmdErrorCount, |
105 | attrtype: ETHTOOL_A_MM_STAT_SMD_ERRORS) || |
106 | mm_put_stat(skb, val: stats->MACMergeFrameAssOkCount, |
107 | attrtype: ETHTOOL_A_MM_STAT_REASSEMBLY_OK) || |
108 | mm_put_stat(skb, val: stats->MACMergeFragCountRx, |
109 | attrtype: ETHTOOL_A_MM_STAT_RX_FRAG_COUNT) || |
110 | mm_put_stat(skb, val: stats->MACMergeFragCountTx, |
111 | attrtype: ETHTOOL_A_MM_STAT_TX_FRAG_COUNT) || |
112 | mm_put_stat(skb, val: stats->MACMergeHoldCount, |
113 | attrtype: ETHTOOL_A_MM_STAT_HOLD_COUNT)) |
114 | goto err_cancel; |
115 | |
116 | nla_nest_end(skb, start: nest); |
117 | return 0; |
118 | |
119 | err_cancel: |
120 | nla_nest_cancel(skb, start: 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, attrtype: ETHTOOL_A_MM_TX_ENABLED, value: state->tx_enabled) || |
132 | nla_put_u8(skb, attrtype: ETHTOOL_A_MM_TX_ACTIVE, value: state->tx_active) || |
133 | nla_put_u8(skb, attrtype: ETHTOOL_A_MM_PMAC_ENABLED, value: state->pmac_enabled) || |
134 | nla_put_u8(skb, attrtype: ETHTOOL_A_MM_VERIFY_ENABLED, value: state->verify_enabled) || |
135 | nla_put_u8(skb, attrtype: ETHTOOL_A_MM_VERIFY_STATUS, value: state->verify_status) || |
136 | nla_put_u32(skb, attrtype: ETHTOOL_A_MM_VERIFY_TIME, value: state->verify_time) || |
137 | nla_put_u32(skb, attrtype: ETHTOOL_A_MM_MAX_VERIFY_TIME, value: state->max_verify_time) || |
138 | nla_put_u32(skb, attrtype: ETHTOOL_A_MM_TX_MIN_FRAG_SIZE, value: state->tx_min_frag_size) || |
139 | nla_put_u32(skb, attrtype: ETHTOOL_A_MM_RX_MIN_FRAG_SIZE, value: state->rx_min_frag_size)) |
140 | return -EMSGSIZE; |
141 | |
142 | if (req_base->flags & ETHTOOL_FLAG_STATS && |
143 | mm_put_stats(skb, stats: &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: &state, cfg: &cfg); |
198 | |
199 | ethnl_update_bool(dst: &cfg.verify_enabled, attr: tb[ETHTOOL_A_MM_VERIFY_ENABLED], |
200 | mod: &mod); |
201 | ethnl_update_u32(dst: &cfg.verify_time, attr: tb[ETHTOOL_A_MM_VERIFY_TIME], mod: &mod); |
202 | ethnl_update_bool(dst: &cfg.tx_enabled, attr: tb[ETHTOOL_A_MM_TX_ENABLED], mod: &mod); |
203 | ethnl_update_bool(dst: &cfg.pmac_enabled, attr: tb[ETHTOOL_A_MM_PMAC_ENABLED], |
204 | mod: &mod); |
205 | ethnl_update_u32(dst: &cfg.tx_min_frag_size, |
206 | attr: tb[ETHTOOL_A_MM_TX_MIN_FRAG_SIZE], mod: &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 | |