fec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. #include "bitset.h"
  5. struct fec_req_info {
  6. struct ethnl_req_info base;
  7. };
  8. struct fec_reply_data {
  9. struct ethnl_reply_data base;
  10. __ETHTOOL_DECLARE_LINK_MODE_MASK(fec_link_modes);
  11. u32 active_fec;
  12. u8 fec_auto;
  13. struct fec_stat_grp {
  14. u64 stats[1 + ETHTOOL_MAX_LANES];
  15. u8 cnt;
  16. } corr, uncorr, corr_bits;
  17. struct ethtool_fec_hist fec_stat_hist;
  18. };
  19. #define FEC_REPDATA(__reply_base) \
  20. container_of(__reply_base, struct fec_reply_data, base)
  21. #define ETHTOOL_FEC_MASK ((ETHTOOL_FEC_LLRS << 1) - 1)
  22. const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1] = {
  23. [ETHTOOL_A_FEC_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy_stats),
  24. };
  25. static void
  26. ethtool_fec_to_link_modes(u32 fec, unsigned long *link_modes, u8 *fec_auto)
  27. {
  28. if (fec_auto)
  29. *fec_auto = !!(fec & ETHTOOL_FEC_AUTO);
  30. if (fec & ETHTOOL_FEC_OFF)
  31. __set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, link_modes);
  32. if (fec & ETHTOOL_FEC_RS)
  33. __set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT, link_modes);
  34. if (fec & ETHTOOL_FEC_BASER)
  35. __set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT, link_modes);
  36. if (fec & ETHTOOL_FEC_LLRS)
  37. __set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT, link_modes);
  38. }
  39. static int
  40. ethtool_link_modes_to_fecparam(struct ethtool_fecparam *fec,
  41. unsigned long *link_modes, u8 fec_auto)
  42. {
  43. memset(fec, 0, sizeof(*fec));
  44. if (fec_auto)
  45. fec->fec |= ETHTOOL_FEC_AUTO;
  46. if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, link_modes))
  47. fec->fec |= ETHTOOL_FEC_OFF;
  48. if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT, link_modes))
  49. fec->fec |= ETHTOOL_FEC_RS;
  50. if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT, link_modes))
  51. fec->fec |= ETHTOOL_FEC_BASER;
  52. if (__test_and_clear_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT, link_modes))
  53. fec->fec |= ETHTOOL_FEC_LLRS;
  54. if (!bitmap_empty(link_modes, __ETHTOOL_LINK_MODE_MASK_NBITS))
  55. return -EINVAL;
  56. return 0;
  57. }
  58. static void
  59. fec_stats_recalc(struct fec_stat_grp *grp, struct ethtool_fec_stat *stats)
  60. {
  61. int i;
  62. if (stats->lanes[0] == ETHTOOL_STAT_NOT_SET) {
  63. grp->stats[0] = stats->total;
  64. grp->cnt = stats->total != ETHTOOL_STAT_NOT_SET;
  65. return;
  66. }
  67. grp->cnt = 1;
  68. grp->stats[0] = 0;
  69. for (i = 0; i < ETHTOOL_MAX_LANES; i++) {
  70. if (stats->lanes[i] == ETHTOOL_STAT_NOT_SET)
  71. break;
  72. grp->stats[0] += stats->lanes[i];
  73. grp->stats[grp->cnt++] = stats->lanes[i];
  74. }
  75. }
  76. static int fec_prepare_data(const struct ethnl_req_info *req_base,
  77. struct ethnl_reply_data *reply_base,
  78. const struct genl_info *info)
  79. {
  80. __ETHTOOL_DECLARE_LINK_MODE_MASK(active_fec_modes) = {};
  81. struct fec_reply_data *data = FEC_REPDATA(reply_base);
  82. struct net_device *dev = reply_base->dev;
  83. struct ethtool_fecparam fec = {};
  84. int ret;
  85. if (!dev->ethtool_ops->get_fecparam)
  86. return -EOPNOTSUPP;
  87. ret = ethnl_ops_begin(dev);
  88. if (ret < 0)
  89. return ret;
  90. ret = dev->ethtool_ops->get_fecparam(dev, &fec);
  91. if (ret)
  92. goto out_complete;
  93. if (req_base->flags & ETHTOOL_FLAG_STATS &&
  94. dev->ethtool_ops->get_fec_stats) {
  95. struct ethtool_fec_stats stats;
  96. ethtool_stats_init((u64 *)&stats, sizeof(stats) / 8);
  97. ethtool_stats_init((u64 *)data->fec_stat_hist.values,
  98. sizeof(data->fec_stat_hist.values) / 8);
  99. dev->ethtool_ops->get_fec_stats(dev, &stats,
  100. &data->fec_stat_hist);
  101. fec_stats_recalc(&data->corr, &stats.corrected_blocks);
  102. fec_stats_recalc(&data->uncorr, &stats.uncorrectable_blocks);
  103. fec_stats_recalc(&data->corr_bits, &stats.corrected_bits);
  104. }
  105. WARN_ON_ONCE(fec.reserved);
  106. ethtool_fec_to_link_modes(fec.fec, data->fec_link_modes,
  107. &data->fec_auto);
  108. ethtool_fec_to_link_modes(fec.active_fec, active_fec_modes, NULL);
  109. data->active_fec = find_first_bit(active_fec_modes,
  110. __ETHTOOL_LINK_MODE_MASK_NBITS);
  111. /* Don't report attr if no FEC mode set. Note that
  112. * ethtool_fecparam_to_link_modes() ignores NONE and AUTO.
  113. */
  114. if (data->active_fec == __ETHTOOL_LINK_MODE_MASK_NBITS)
  115. data->active_fec = 0;
  116. out_complete:
  117. ethnl_ops_complete(dev);
  118. return ret;
  119. }
  120. static int fec_reply_size(const struct ethnl_req_info *req_base,
  121. const struct ethnl_reply_data *reply_base)
  122. {
  123. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  124. const struct fec_reply_data *data = FEC_REPDATA(reply_base);
  125. int len = 0;
  126. int ret;
  127. ret = ethnl_bitset_size(data->fec_link_modes, NULL,
  128. __ETHTOOL_LINK_MODE_MASK_NBITS,
  129. link_mode_names, compact);
  130. if (ret < 0)
  131. return ret;
  132. len += ret;
  133. len += nla_total_size(sizeof(u8)) + /* _FEC_AUTO */
  134. nla_total_size(sizeof(u32)); /* _FEC_ACTIVE */
  135. if (req_base->flags & ETHTOOL_FLAG_STATS) {
  136. len += 3 * nla_total_size_64bit(sizeof(u64) *
  137. (1 + ETHTOOL_MAX_LANES));
  138. /* add FEC bins information */
  139. len += (nla_total_size(0) + /* _A_FEC_HIST */
  140. nla_total_size(4) + /* _A_FEC_HIST_BIN_LOW */
  141. nla_total_size(4) + /* _A_FEC_HIST_BIN_HI */
  142. /* _A_FEC_HIST_BIN_VAL + per-lane values */
  143. nla_total_size_64bit(sizeof(u64)) +
  144. nla_total_size_64bit(sizeof(u64) * ETHTOOL_MAX_LANES)) *
  145. ETHTOOL_FEC_HIST_MAX;
  146. }
  147. return len;
  148. }
  149. static int fec_put_hist(struct sk_buff *skb,
  150. const struct ethtool_fec_hist *hist)
  151. {
  152. const struct ethtool_fec_hist_range *ranges = hist->ranges;
  153. const struct ethtool_fec_hist_value *values = hist->values;
  154. struct nlattr *nest;
  155. int i, j;
  156. u64 sum;
  157. if (!ranges)
  158. return 0;
  159. for (i = 0; i < ETHTOOL_FEC_HIST_MAX; i++) {
  160. if (i && !ranges[i].low && !ranges[i].high)
  161. break;
  162. if (WARN_ON_ONCE(values[i].sum == ETHTOOL_STAT_NOT_SET &&
  163. values[i].per_lane[0] == ETHTOOL_STAT_NOT_SET))
  164. break;
  165. nest = nla_nest_start(skb, ETHTOOL_A_FEC_STAT_HIST);
  166. if (!nest)
  167. return -EMSGSIZE;
  168. if (nla_put_u32(skb, ETHTOOL_A_FEC_HIST_BIN_LOW,
  169. ranges[i].low) ||
  170. nla_put_u32(skb, ETHTOOL_A_FEC_HIST_BIN_HIGH,
  171. ranges[i].high))
  172. goto err_cancel_hist;
  173. sum = 0;
  174. for (j = 0; j < ETHTOOL_MAX_LANES; j++) {
  175. if (values[i].per_lane[j] == ETHTOOL_STAT_NOT_SET)
  176. break;
  177. sum += values[i].per_lane[j];
  178. }
  179. if (nla_put_uint(skb, ETHTOOL_A_FEC_HIST_BIN_VAL,
  180. values[i].sum == ETHTOOL_STAT_NOT_SET ?
  181. sum : values[i].sum))
  182. goto err_cancel_hist;
  183. if (j && nla_put_64bit(skb, ETHTOOL_A_FEC_HIST_BIN_VAL_PER_LANE,
  184. sizeof(u64) * j,
  185. values[i].per_lane,
  186. ETHTOOL_A_FEC_HIST_PAD))
  187. goto err_cancel_hist;
  188. nla_nest_end(skb, nest);
  189. }
  190. return 0;
  191. err_cancel_hist:
  192. nla_nest_cancel(skb, nest);
  193. return -EMSGSIZE;
  194. }
  195. static int fec_put_stats(struct sk_buff *skb, const struct fec_reply_data *data)
  196. {
  197. struct nlattr *nest;
  198. nest = nla_nest_start(skb, ETHTOOL_A_FEC_STATS);
  199. if (!nest)
  200. return -EMSGSIZE;
  201. if (nla_put_64bit(skb, ETHTOOL_A_FEC_STAT_CORRECTED,
  202. sizeof(u64) * data->corr.cnt,
  203. data->corr.stats, ETHTOOL_A_FEC_STAT_PAD) ||
  204. nla_put_64bit(skb, ETHTOOL_A_FEC_STAT_UNCORR,
  205. sizeof(u64) * data->uncorr.cnt,
  206. data->uncorr.stats, ETHTOOL_A_FEC_STAT_PAD) ||
  207. nla_put_64bit(skb, ETHTOOL_A_FEC_STAT_CORR_BITS,
  208. sizeof(u64) * data->corr_bits.cnt,
  209. data->corr_bits.stats, ETHTOOL_A_FEC_STAT_PAD))
  210. goto err_cancel;
  211. if (fec_put_hist(skb, &data->fec_stat_hist))
  212. goto err_cancel;
  213. nla_nest_end(skb, nest);
  214. return 0;
  215. err_cancel:
  216. nla_nest_cancel(skb, nest);
  217. return -EMSGSIZE;
  218. }
  219. static int fec_fill_reply(struct sk_buff *skb,
  220. const struct ethnl_req_info *req_base,
  221. const struct ethnl_reply_data *reply_base)
  222. {
  223. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  224. const struct fec_reply_data *data = FEC_REPDATA(reply_base);
  225. int ret;
  226. ret = ethnl_put_bitset(skb, ETHTOOL_A_FEC_MODES,
  227. data->fec_link_modes, NULL,
  228. __ETHTOOL_LINK_MODE_MASK_NBITS,
  229. link_mode_names, compact);
  230. if (ret < 0)
  231. return ret;
  232. if (nla_put_u8(skb, ETHTOOL_A_FEC_AUTO, data->fec_auto) ||
  233. (data->active_fec &&
  234. nla_put_u32(skb, ETHTOOL_A_FEC_ACTIVE, data->active_fec)))
  235. return -EMSGSIZE;
  236. if (req_base->flags & ETHTOOL_FLAG_STATS && fec_put_stats(skb, data))
  237. return -EMSGSIZE;
  238. return 0;
  239. }
  240. /* FEC_SET */
  241. const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1] = {
  242. [ETHTOOL_A_FEC_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
  243. [ETHTOOL_A_FEC_MODES] = { .type = NLA_NESTED },
  244. [ETHTOOL_A_FEC_AUTO] = NLA_POLICY_MAX(NLA_U8, 1),
  245. };
  246. static int
  247. ethnl_set_fec_validate(struct ethnl_req_info *req_info, struct genl_info *info)
  248. {
  249. const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
  250. return ops->get_fecparam && ops->set_fecparam ? 1 : -EOPNOTSUPP;
  251. }
  252. static int
  253. ethnl_set_fec(struct ethnl_req_info *req_info, struct genl_info *info)
  254. {
  255. __ETHTOOL_DECLARE_LINK_MODE_MASK(fec_link_modes) = {};
  256. struct net_device *dev = req_info->dev;
  257. struct nlattr **tb = info->attrs;
  258. struct ethtool_fecparam fec = {};
  259. bool mod = false;
  260. u8 fec_auto;
  261. int ret;
  262. ret = dev->ethtool_ops->get_fecparam(dev, &fec);
  263. if (ret < 0)
  264. return ret;
  265. ethtool_fec_to_link_modes(fec.fec, fec_link_modes, &fec_auto);
  266. ret = ethnl_update_bitset(fec_link_modes,
  267. __ETHTOOL_LINK_MODE_MASK_NBITS,
  268. tb[ETHTOOL_A_FEC_MODES],
  269. link_mode_names, info->extack, &mod);
  270. if (ret < 0)
  271. return ret;
  272. ethnl_update_u8(&fec_auto, tb[ETHTOOL_A_FEC_AUTO], &mod);
  273. if (!mod)
  274. return 0;
  275. ret = ethtool_link_modes_to_fecparam(&fec, fec_link_modes, fec_auto);
  276. if (ret) {
  277. NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_FEC_MODES],
  278. "invalid FEC modes requested");
  279. return ret;
  280. }
  281. if (!fec.fec) {
  282. NL_SET_ERR_MSG_ATTR(info->extack, tb[ETHTOOL_A_FEC_MODES],
  283. "no FEC modes set");
  284. return -EINVAL;
  285. }
  286. ret = dev->ethtool_ops->set_fecparam(dev, &fec);
  287. return ret < 0 ? ret : 1;
  288. }
  289. const struct ethnl_request_ops ethnl_fec_request_ops = {
  290. .request_cmd = ETHTOOL_MSG_FEC_GET,
  291. .reply_cmd = ETHTOOL_MSG_FEC_GET_REPLY,
  292. .hdr_attr = ETHTOOL_A_FEC_HEADER,
  293. .req_info_size = sizeof(struct fec_req_info),
  294. .reply_data_size = sizeof(struct fec_reply_data),
  295. .prepare_data = fec_prepare_data,
  296. .reply_size = fec_reply_size,
  297. .fill_reply = fec_fill_reply,
  298. .set_validate = ethnl_set_fec_validate,
  299. .set = ethnl_set_fec,
  300. .set_ntf_cmd = ETHTOOL_MSG_FEC_NTF,
  301. };