Commit | Line | Data |
---|---|---|
7f59fb32 MK |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | ||
3 | #include "netlink.h" | |
4 | #include "common.h" | |
5 | ||
6 | struct pause_req_info { | |
7 | struct ethnl_req_info base; | |
04692c90 | 8 | enum ethtool_mac_stats_src src; |
7f59fb32 MK |
9 | }; |
10 | ||
04692c90 VO |
11 | #define PAUSE_REQINFO(__req_base) \ |
12 | container_of(__req_base, struct pause_req_info, base) | |
13 | ||
7f59fb32 MK |
14 | struct pause_reply_data { |
15 | struct ethnl_reply_data base; | |
16 | struct ethtool_pauseparam pauseparam; | |
9a27a330 | 17 | struct ethtool_pause_stats pausestat; |
7f59fb32 MK |
18 | }; |
19 | ||
20 | #define PAUSE_REPDATA(__reply_base) \ | |
21 | container_of(__reply_base, struct pause_reply_data, base) | |
22 | ||
ff419afa | 23 | const struct nla_policy ethnl_pause_get_policy[] = { |
329d9c33 | 24 | [ETHTOOL_A_PAUSE_HEADER] = |
a0de1cd3 | 25 | NLA_POLICY_NESTED(ethnl_header_policy_stats), |
04692c90 VO |
26 | [ETHTOOL_A_PAUSE_STATS_SRC] = |
27 | NLA_POLICY_MAX(NLA_U32, ETHTOOL_MAC_STATS_SRC_PMAC), | |
7f59fb32 MK |
28 | }; |
29 | ||
04692c90 VO |
30 | static int pause_parse_request(struct ethnl_req_info *req_base, |
31 | struct nlattr **tb, | |
32 | struct netlink_ext_ack *extack) | |
33 | { | |
34 | enum ethtool_mac_stats_src src = ETHTOOL_MAC_STATS_SRC_AGGREGATE; | |
35 | struct pause_req_info *req_info = PAUSE_REQINFO(req_base); | |
36 | ||
37 | if (tb[ETHTOOL_A_PAUSE_STATS_SRC]) { | |
38 | if (!(req_base->flags & ETHTOOL_FLAG_STATS)) { | |
39 | NL_SET_ERR_MSG_MOD(extack, | |
40 | "ETHTOOL_FLAG_STATS must be set when requesting a source of stats"); | |
41 | return -EINVAL; | |
42 | } | |
43 | ||
44 | src = nla_get_u32(tb[ETHTOOL_A_PAUSE_STATS_SRC]); | |
45 | } | |
46 | ||
47 | req_info->src = src; | |
48 | ||
49 | return 0; | |
50 | } | |
51 | ||
7f59fb32 MK |
52 | static int pause_prepare_data(const struct ethnl_req_info *req_base, |
53 | struct ethnl_reply_data *reply_base, | |
54 | struct genl_info *info) | |
55 | { | |
04692c90 | 56 | const struct pause_req_info *req_info = PAUSE_REQINFO(req_base); |
f5be9caf | 57 | struct netlink_ext_ack *extack = info ? info->extack : NULL; |
7f59fb32 | 58 | struct pause_reply_data *data = PAUSE_REPDATA(reply_base); |
04692c90 | 59 | enum ethtool_mac_stats_src src = req_info->src; |
7f59fb32 MK |
60 | struct net_device *dev = reply_base->dev; |
61 | int ret; | |
62 | ||
63 | if (!dev->ethtool_ops->get_pauseparam) | |
64 | return -EOPNOTSUPP; | |
9a27a330 | 65 | |
16756d3e JK |
66 | ethtool_stats_init((u64 *)&data->pausestat, |
67 | sizeof(data->pausestat) / 8); | |
04692c90 | 68 | data->pausestat.src = src; |
16756d3e | 69 | |
7f59fb32 MK |
70 | ret = ethnl_ops_begin(dev); |
71 | if (ret < 0) | |
72 | return ret; | |
04692c90 VO |
73 | |
74 | if ((src == ETHTOOL_MAC_STATS_SRC_EMAC || | |
75 | src == ETHTOOL_MAC_STATS_SRC_PMAC) && | |
76 | !__ethtool_dev_mm_supported(dev)) { | |
77 | NL_SET_ERR_MSG_MOD(extack, | |
78 | "Device does not support MAC merge layer"); | |
79 | ethnl_ops_complete(dev); | |
80 | return -EOPNOTSUPP; | |
81 | } | |
82 | ||
7f59fb32 | 83 | dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam); |
9a27a330 | 84 | if (req_base->flags & ETHTOOL_FLAG_STATS && |
16756d3e | 85 | dev->ethtool_ops->get_pause_stats) |
9a27a330 | 86 | dev->ethtool_ops->get_pause_stats(dev, &data->pausestat); |
04692c90 | 87 | |
7f59fb32 MK |
88 | ethnl_ops_complete(dev); |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | static int pause_reply_size(const struct ethnl_req_info *req_base, | |
94 | const struct ethnl_reply_data *reply_base) | |
95 | { | |
9a27a330 | 96 | int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */ |
7f59fb32 MK |
97 | nla_total_size(sizeof(u8)) + /* _PAUSE_RX */ |
98 | nla_total_size(sizeof(u8)); /* _PAUSE_TX */ | |
9a27a330 JK |
99 | |
100 | if (req_base->flags & ETHTOOL_FLAG_STATS) | |
101 | n += nla_total_size(0) + /* _PAUSE_STATS */ | |
04692c90 | 102 | nla_total_size(sizeof(u32)) + /* _PAUSE_STATS_SRC */ |
1aabe578 | 103 | nla_total_size_64bit(sizeof(u64)) * ETHTOOL_PAUSE_STAT_CNT; |
9a27a330 JK |
104 | return n; |
105 | } | |
106 | ||
107 | static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype, | |
108 | u16 padtype) | |
109 | { | |
110 | if (val == ETHTOOL_STAT_NOT_SET) | |
111 | return 0; | |
112 | if (nla_put_u64_64bit(skb, attrtype, val, padtype)) | |
113 | return -EMSGSIZE; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | static int pause_put_stats(struct sk_buff *skb, | |
119 | const struct ethtool_pause_stats *pause_stats) | |
120 | { | |
121 | const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD; | |
122 | struct nlattr *nest; | |
123 | ||
04692c90 VO |
124 | if (nla_put_u32(skb, ETHTOOL_A_PAUSE_STATS_SRC, pause_stats->src)) |
125 | return -EMSGSIZE; | |
126 | ||
9a27a330 JK |
127 | nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS); |
128 | if (!nest) | |
129 | return -EMSGSIZE; | |
130 | ||
131 | if (ethtool_put_stat(skb, pause_stats->tx_pause_frames, | |
132 | ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) || | |
133 | ethtool_put_stat(skb, pause_stats->rx_pause_frames, | |
134 | ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad)) | |
135 | goto err_cancel; | |
136 | ||
137 | nla_nest_end(skb, nest); | |
138 | return 0; | |
139 | ||
140 | err_cancel: | |
141 | nla_nest_cancel(skb, nest); | |
142 | return -EMSGSIZE; | |
7f59fb32 MK |
143 | } |
144 | ||
145 | static int pause_fill_reply(struct sk_buff *skb, | |
146 | const struct ethnl_req_info *req_base, | |
147 | const struct ethnl_reply_data *reply_base) | |
148 | { | |
149 | const struct pause_reply_data *data = PAUSE_REPDATA(reply_base); | |
150 | const struct ethtool_pauseparam *pauseparam = &data->pauseparam; | |
151 | ||
152 | if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) || | |
153 | nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) || | |
154 | nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause)) | |
155 | return -EMSGSIZE; | |
156 | ||
9a27a330 JK |
157 | if (req_base->flags & ETHTOOL_FLAG_STATS && |
158 | pause_put_stats(skb, &data->pausestat)) | |
159 | return -EMSGSIZE; | |
160 | ||
7f59fb32 MK |
161 | return 0; |
162 | } | |
163 | ||
164 | const struct ethnl_request_ops ethnl_pause_request_ops = { | |
165 | .request_cmd = ETHTOOL_MSG_PAUSE_GET, | |
166 | .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY, | |
167 | .hdr_attr = ETHTOOL_A_PAUSE_HEADER, | |
7f59fb32 MK |
168 | .req_info_size = sizeof(struct pause_req_info), |
169 | .reply_data_size = sizeof(struct pause_reply_data), | |
7f59fb32 | 170 | |
04692c90 | 171 | .parse_request = pause_parse_request, |
7f59fb32 MK |
172 | .prepare_data = pause_prepare_data, |
173 | .reply_size = pause_reply_size, | |
174 | .fill_reply = pause_fill_reply, | |
175 | }; | |
3ab87993 MK |
176 | |
177 | /* PAUSE_SET */ | |
178 | ||
ff419afa | 179 | const struct nla_policy ethnl_pause_set_policy[] = { |
329d9c33 JK |
180 | [ETHTOOL_A_PAUSE_HEADER] = |
181 | NLA_POLICY_NESTED(ethnl_header_policy), | |
3ab87993 MK |
182 | [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 }, |
183 | [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 }, | |
184 | [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 }, | |
185 | }; | |
186 | ||
187 | int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info) | |
188 | { | |
3ab87993 MK |
189 | struct ethtool_pauseparam params = {}; |
190 | struct ethnl_req_info req_info = {}; | |
5028588b | 191 | struct nlattr **tb = info->attrs; |
3ab87993 MK |
192 | const struct ethtool_ops *ops; |
193 | struct net_device *dev; | |
194 | bool mod = false; | |
195 | int ret; | |
196 | ||
3ab87993 MK |
197 | ret = ethnl_parse_header_dev_get(&req_info, |
198 | tb[ETHTOOL_A_PAUSE_HEADER], | |
199 | genl_info_net(info), info->extack, | |
200 | true); | |
201 | if (ret < 0) | |
202 | return ret; | |
203 | dev = req_info.dev; | |
204 | ops = dev->ethtool_ops; | |
205 | ret = -EOPNOTSUPP; | |
206 | if (!ops->get_pauseparam || !ops->set_pauseparam) | |
207 | goto out_dev; | |
208 | ||
209 | rtnl_lock(); | |
210 | ret = ethnl_ops_begin(dev); | |
211 | if (ret < 0) | |
212 | goto out_rtnl; | |
213 | ops->get_pauseparam(dev, ¶ms); | |
214 | ||
215 | ethnl_update_bool32(¶ms.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod); | |
216 | ethnl_update_bool32(¶ms.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod); | |
217 | ethnl_update_bool32(¶ms.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod); | |
218 | ret = 0; | |
219 | if (!mod) | |
220 | goto out_ops; | |
221 | ||
222 | ret = dev->ethtool_ops->set_pauseparam(dev, ¶ms); | |
bf37faa3 MK |
223 | if (ret < 0) |
224 | goto out_ops; | |
225 | ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); | |
3ab87993 MK |
226 | |
227 | out_ops: | |
228 | ethnl_ops_complete(dev); | |
229 | out_rtnl: | |
230 | rtnl_unlock(); | |
231 | out_dev: | |
34ac17ec | 232 | ethnl_parse_header_dev_put(&req_info); |
3ab87993 MK |
233 | return ret; |
234 | } |