1 // SPDX-License-Identifier: GPL-2.0-only
6 struct pause_req_info {
7 struct ethnl_req_info base;
10 struct pause_reply_data {
11 struct ethnl_reply_data base;
12 struct ethtool_pauseparam pauseparam;
13 struct ethtool_pause_stats pausestat;
16 #define PAUSE_REPDATA(__reply_base) \
17 container_of(__reply_base, struct pause_reply_data, base)
19 const struct nla_policy ethnl_pause_get_policy[] = {
20 [ETHTOOL_A_PAUSE_HEADER] =
21 NLA_POLICY_NESTED(ethnl_header_policy_stats),
24 static void ethtool_stats_init(u64 *stats, unsigned int n)
27 stats[n] = ETHTOOL_STAT_NOT_SET;
30 static int pause_prepare_data(const struct ethnl_req_info *req_base,
31 struct ethnl_reply_data *reply_base,
32 struct genl_info *info)
34 struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
35 struct net_device *dev = reply_base->dev;
38 if (!dev->ethtool_ops->get_pauseparam)
41 ethtool_stats_init((u64 *)&data->pausestat,
42 sizeof(data->pausestat) / 8);
44 ret = ethnl_ops_begin(dev);
47 dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam);
48 if (req_base->flags & ETHTOOL_FLAG_STATS &&
49 dev->ethtool_ops->get_pause_stats)
50 dev->ethtool_ops->get_pause_stats(dev, &data->pausestat);
51 ethnl_ops_complete(dev);
56 static int pause_reply_size(const struct ethnl_req_info *req_base,
57 const struct ethnl_reply_data *reply_base)
59 int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */
60 nla_total_size(sizeof(u8)) + /* _PAUSE_RX */
61 nla_total_size(sizeof(u8)); /* _PAUSE_TX */
63 if (req_base->flags & ETHTOOL_FLAG_STATS)
64 n += nla_total_size(0) + /* _PAUSE_STATS */
65 nla_total_size_64bit(sizeof(u64)) * ETHTOOL_PAUSE_STAT_CNT;
69 static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype,
72 if (val == ETHTOOL_STAT_NOT_SET)
74 if (nla_put_u64_64bit(skb, attrtype, val, padtype))
80 static int pause_put_stats(struct sk_buff *skb,
81 const struct ethtool_pause_stats *pause_stats)
83 const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD;
86 nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS);
90 if (ethtool_put_stat(skb, pause_stats->tx_pause_frames,
91 ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) ||
92 ethtool_put_stat(skb, pause_stats->rx_pause_frames,
93 ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad))
96 nla_nest_end(skb, nest);
100 nla_nest_cancel(skb, nest);
104 static int pause_fill_reply(struct sk_buff *skb,
105 const struct ethnl_req_info *req_base,
106 const struct ethnl_reply_data *reply_base)
108 const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
109 const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
111 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) ||
112 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) ||
113 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause))
116 if (req_base->flags & ETHTOOL_FLAG_STATS &&
117 pause_put_stats(skb, &data->pausestat))
123 const struct ethnl_request_ops ethnl_pause_request_ops = {
124 .request_cmd = ETHTOOL_MSG_PAUSE_GET,
125 .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY,
126 .hdr_attr = ETHTOOL_A_PAUSE_HEADER,
127 .req_info_size = sizeof(struct pause_req_info),
128 .reply_data_size = sizeof(struct pause_reply_data),
130 .prepare_data = pause_prepare_data,
131 .reply_size = pause_reply_size,
132 .fill_reply = pause_fill_reply,
137 const struct nla_policy ethnl_pause_set_policy[] = {
138 [ETHTOOL_A_PAUSE_HEADER] =
139 NLA_POLICY_NESTED(ethnl_header_policy),
140 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 },
141 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 },
142 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 },
145 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info)
147 struct ethtool_pauseparam params = {};
148 struct ethnl_req_info req_info = {};
149 struct nlattr **tb = info->attrs;
150 const struct ethtool_ops *ops;
151 struct net_device *dev;
155 ret = ethnl_parse_header_dev_get(&req_info,
156 tb[ETHTOOL_A_PAUSE_HEADER],
157 genl_info_net(info), info->extack,
162 ops = dev->ethtool_ops;
164 if (!ops->get_pauseparam || !ops->set_pauseparam)
168 ret = ethnl_ops_begin(dev);
171 ops->get_pauseparam(dev, ¶ms);
173 ethnl_update_bool32(¶ms.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
174 ethnl_update_bool32(¶ms.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
175 ethnl_update_bool32(¶ms.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
180 ret = dev->ethtool_ops->set_pauseparam(dev, ¶ms);
183 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
186 ethnl_ops_complete(dev);