1 // SPDX-License-Identifier: GPL-2.0-only
8 struct ethnl_req_info base;
11 struct fec_reply_data {
12 struct ethnl_reply_data base;
13 __ETHTOOL_DECLARE_LINK_MODE_MASK(fec_link_modes);
17 u64 stats[1 + ETHTOOL_MAX_LANES];
19 } corr, uncorr, corr_bits;
22 #define FEC_REPDATA(__reply_base) \
23 container_of(__reply_base, struct fec_reply_data, base)
25 #define ETHTOOL_FEC_MASK ((ETHTOOL_FEC_LLRS << 1) - 1)
27 const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1] = {
28 [ETHTOOL_A_FEC_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy_stats),
32 ethtool_fec_to_link_modes(u32 fec, unsigned long *link_modes, u8 *fec_auto)
35 *fec_auto = !!(fec & ETHTOOL_FEC_AUTO);
37 if (fec & ETHTOOL_FEC_OFF)
38 __set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, link_modes);
39 if (fec & ETHTOOL_FEC_RS)
40 __set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT, link_modes);
41 if (fec & ETHTOOL_FEC_BASER)
42 __set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT, link_modes);
43 if (fec & ETHTOOL_FEC_LLRS)
44 __set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT, link_modes);
48 ethtool_link_modes_to_fecparam(struct ethtool_fecparam *fec,
49 unsigned long *link_modes, u8 fec_auto)
51 memset(fec, 0, sizeof(*fec));
54 fec->fec |= ETHTOOL_FEC_AUTO;
56 if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, link_modes))
57 fec->fec |= ETHTOOL_FEC_OFF;
58 if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT, link_modes))
59 fec->fec |= ETHTOOL_FEC_RS;
60 if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT, link_modes))
61 fec->fec |= ETHTOOL_FEC_BASER;
62 if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT, link_modes))
63 fec->fec |= ETHTOOL_FEC_LLRS;
65 if (!bitmap_empty(link_modes, __ETHTOOL_LINK_MODE_MASK_NBITS))
72 fec_stats_recalc(struct fec_stat_grp *grp, struct ethtool_fec_stat *stats)
76 if (stats->lanes[0] == ETHTOOL_STAT_NOT_SET) {
77 grp->stats[0] = stats->total;
78 grp->cnt = stats->total != ETHTOOL_STAT_NOT_SET;
84 for (i = 0; i < ETHTOOL_MAX_LANES; i++) {
85 if (stats->lanes[i] == ETHTOOL_STAT_NOT_SET)
88 grp->stats[0] += stats->lanes[i];
89 grp->stats[grp->cnt++] = stats->lanes[i];
93 static int fec_prepare_data(const struct ethnl_req_info *req_base,
94 struct ethnl_reply_data *reply_base,
95 const struct genl_info *info)
97 __ETHTOOL_DECLARE_LINK_MODE_MASK(active_fec_modes) = {};
98 struct fec_reply_data *data = FEC_REPDATA(reply_base);
99 struct net_device *dev = reply_base->dev;
100 struct ethtool_fecparam fec = {};
103 if (!dev->ethtool_ops->get_fecparam)
105 ret = ethnl_ops_begin(dev);
108 ret = dev->ethtool_ops->get_fecparam(dev, &fec);
111 if (req_base->flags & ETHTOOL_FLAG_STATS &&
112 dev->ethtool_ops->get_fec_stats) {
113 struct ethtool_fec_stats stats;
115 ethtool_stats_init((u64 *)&stats, sizeof(stats) / 8);
116 dev->ethtool_ops->get_fec_stats(dev, &stats);
118 fec_stats_recalc(&data->corr, &stats.corrected_blocks);
119 fec_stats_recalc(&data->uncorr, &stats.uncorrectable_blocks);
120 fec_stats_recalc(&data->corr_bits, &stats.corrected_bits);
123 WARN_ON_ONCE(fec.reserved);
125 ethtool_fec_to_link_modes(fec.fec, data->fec_link_modes,
128 ethtool_fec_to_link_modes(fec.active_fec, active_fec_modes, NULL);
129 data->active_fec = find_first_bit(active_fec_modes,
130 __ETHTOOL_LINK_MODE_MASK_NBITS);
131 /* Don't report attr if no FEC mode set. Note that
132 * ethtool_fecparam_to_link_modes() ignores NONE and AUTO.
134 if (data->active_fec == __ETHTOOL_LINK_MODE_MASK_NBITS)
135 data->active_fec = 0;
138 ethnl_ops_complete(dev);
142 static int fec_reply_size(const struct ethnl_req_info *req_base,
143 const struct ethnl_reply_data *reply_base)
145 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
146 const struct fec_reply_data *data = FEC_REPDATA(reply_base);
150 ret = ethnl_bitset_size(data->fec_link_modes, NULL,
151 __ETHTOOL_LINK_MODE_MASK_NBITS,
152 link_mode_names, compact);
157 len += nla_total_size(sizeof(u8)) + /* _FEC_AUTO */
158 nla_total_size(sizeof(u32)); /* _FEC_ACTIVE */
160 if (req_base->flags & ETHTOOL_FLAG_STATS)
161 len += 3 * nla_total_size_64bit(sizeof(u64) *
162 (1 + ETHTOOL_MAX_LANES));
167 static int fec_put_stats(struct sk_buff *skb, const struct fec_reply_data *data)
171 nest = nla_nest_start(skb, ETHTOOL_A_FEC_STATS);
175 if (nla_put_64bit(skb, ETHTOOL_A_FEC_STAT_CORRECTED,
176 sizeof(u64) * data->corr.cnt,
177 data->corr.stats, ETHTOOL_A_FEC_STAT_PAD) ||
178 nla_put_64bit(skb, ETHTOOL_A_FEC_STAT_UNCORR,
179 sizeof(u64) * data->uncorr.cnt,
180 data->uncorr.stats, ETHTOOL_A_FEC_STAT_PAD) ||
181 nla_put_64bit(skb, ETHTOOL_A_FEC_STAT_CORR_BITS,
182 sizeof(u64) * data->corr_bits.cnt,
183 data->corr_bits.stats, ETHTOOL_A_FEC_STAT_PAD))
186 nla_nest_end(skb, nest);
190 nla_nest_cancel(skb, nest);
194 static int fec_fill_reply(struct sk_buff *skb,
195 const struct ethnl_req_info *req_base,
196 const struct ethnl_reply_data *reply_base)
198 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
199 const struct fec_reply_data *data = FEC_REPDATA(reply_base);
202 ret = ethnl_put_bitset(skb, ETHTOOL_A_FEC_MODES,
203 data->fec_link_modes, NULL,
204 __ETHTOOL_LINK_MODE_MASK_NBITS,
205 link_mode_names, compact);
209 if (nla_put_u8(skb, ETHTOOL_A_FEC_AUTO, data->fec_auto) ||
211 nla_put_u32(skb, ETHTOOL_A_FEC_ACTIVE, data->active_fec)))
214 if (req_base->flags & ETHTOOL_FLAG_STATS && fec_put_stats(skb, data))
222 const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1] = {
223 [ETHTOOL_A_FEC_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
224 [ETHTOOL_A_FEC_MODES] = { .type = NLA_NESTED },
225 [ETHTOOL_A_FEC_AUTO] = NLA_POLICY_MAX(NLA_U8, 1),
229 ethnl_set_fec_validate(struct ethnl_req_info *req_info, struct genl_info *info)
231 const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
233 return ops->get_fecparam && ops->set_fecparam ? 1 : -EOPNOTSUPP;
237 ethnl_set_fec(struct ethnl_req_info *req_info, struct genl_info *info)
239 __ETHTOOL_DECLARE_LINK_MODE_MASK(fec_link_modes) = {};
240 struct net_device *dev = req_info->dev;
241 struct nlattr **tb = info->attrs;
242 struct ethtool_fecparam fec = {};
247 ret = dev->ethtool_ops->get_fecparam(dev, &fec);
251 ethtool_fec_to_link_modes(fec.fec, fec_link_modes, &fec_auto);
253 ret = ethnl_update_bitset(fec_link_modes,
254 __ETHTOOL_LINK_MODE_MASK_NBITS,
255 tb[ETHTOOL_A_FEC_MODES],
256 link_mode_names, info->extack, &mod);
259 ethnl_update_u8(&fec_auto, tb[ETHTOOL_A_FEC_AUTO], &mod);
263 ret = ethtool_link_modes_to_fecparam(&fec, fec_link_modes, fec_auto);
265 NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_FEC_MODES],
266 "invalid FEC modes requested");
270 NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_FEC_MODES],
275 ret = dev->ethtool_ops->set_fecparam(dev, &fec);
276 return ret < 0 ? ret : 1;
279 const struct ethnl_request_ops ethnl_fec_request_ops = {
280 .request_cmd = ETHTOOL_MSG_FEC_GET,
281 .reply_cmd = ETHTOOL_MSG_FEC_GET_REPLY,
282 .hdr_attr = ETHTOOL_A_FEC_HEADER,
283 .req_info_size = sizeof(struct fec_req_info),
284 .reply_data_size = sizeof(struct fec_reply_data),
286 .prepare_data = fec_prepare_data,
287 .reply_size = fec_reply_size,
288 .fill_reply = fec_fill_reply,
290 .set_validate = ethnl_set_fec_validate,
291 .set = ethnl_set_fec,
292 .set_ntf_cmd = ETHTOOL_MSG_FEC_NTF,