GNU Linux-libre 5.10.153-gnu1
[releases.git] / net / ethtool / pause.c
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;
8 };
9
10 struct pause_reply_data {
11         struct ethnl_reply_data         base;
12         struct ethtool_pauseparam       pauseparam;
13         struct ethtool_pause_stats      pausestat;
14 };
15
16 #define PAUSE_REPDATA(__reply_base) \
17         container_of(__reply_base, struct pause_reply_data, base)
18
19 const struct nla_policy ethnl_pause_get_policy[] = {
20         [ETHTOOL_A_PAUSE_HEADER]                =
21                 NLA_POLICY_NESTED(ethnl_header_policy_stats),
22 };
23
24 static void ethtool_stats_init(u64 *stats, unsigned int n)
25 {
26         while (n--)
27                 stats[n] = ETHTOOL_STAT_NOT_SET;
28 }
29
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)
33 {
34         struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
35         struct net_device *dev = reply_base->dev;
36         int ret;
37
38         if (!dev->ethtool_ops->get_pauseparam)
39                 return -EOPNOTSUPP;
40
41         ethtool_stats_init((u64 *)&data->pausestat,
42                            sizeof(data->pausestat) / 8);
43
44         ret = ethnl_ops_begin(dev);
45         if (ret < 0)
46                 return ret;
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);
52
53         return 0;
54 }
55
56 static int pause_reply_size(const struct ethnl_req_info *req_base,
57                             const struct ethnl_reply_data *reply_base)
58 {
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 */
62
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;
66         return n;
67 }
68
69 static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype,
70                             u16 padtype)
71 {
72         if (val == ETHTOOL_STAT_NOT_SET)
73                 return 0;
74         if (nla_put_u64_64bit(skb, attrtype, val, padtype))
75                 return -EMSGSIZE;
76
77         return 0;
78 }
79
80 static int pause_put_stats(struct sk_buff *skb,
81                            const struct ethtool_pause_stats *pause_stats)
82 {
83         const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD;
84         struct nlattr *nest;
85
86         nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS);
87         if (!nest)
88                 return -EMSGSIZE;
89
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))
94                 goto err_cancel;
95
96         nla_nest_end(skb, nest);
97         return 0;
98
99 err_cancel:
100         nla_nest_cancel(skb, nest);
101         return -EMSGSIZE;
102 }
103
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)
107 {
108         const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
109         const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
110
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))
114                 return -EMSGSIZE;
115
116         if (req_base->flags & ETHTOOL_FLAG_STATS &&
117             pause_put_stats(skb, &data->pausestat))
118                 return -EMSGSIZE;
119
120         return 0;
121 }
122
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),
129
130         .prepare_data           = pause_prepare_data,
131         .reply_size             = pause_reply_size,
132         .fill_reply             = pause_fill_reply,
133 };
134
135 /* PAUSE_SET */
136
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 },
143 };
144
145 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info)
146 {
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;
152         bool mod = false;
153         int ret;
154
155         ret = ethnl_parse_header_dev_get(&req_info,
156                                          tb[ETHTOOL_A_PAUSE_HEADER],
157                                          genl_info_net(info), info->extack,
158                                          true);
159         if (ret < 0)
160                 return ret;
161         dev = req_info.dev;
162         ops = dev->ethtool_ops;
163         ret = -EOPNOTSUPP;
164         if (!ops->get_pauseparam || !ops->set_pauseparam)
165                 goto out_dev;
166
167         rtnl_lock();
168         ret = ethnl_ops_begin(dev);
169         if (ret < 0)
170                 goto out_rtnl;
171         ops->get_pauseparam(dev, &params);
172
173         ethnl_update_bool32(&params.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
174         ethnl_update_bool32(&params.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
175         ethnl_update_bool32(&params.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
176         ret = 0;
177         if (!mod)
178                 goto out_ops;
179
180         ret = dev->ethtool_ops->set_pauseparam(dev, &params);
181         if (ret < 0)
182                 goto out_ops;
183         ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
184
185 out_ops:
186         ethnl_ops_complete(dev);
187 out_rtnl:
188         rtnl_unlock();
189 out_dev:
190         dev_put(dev);
191         return ret;
192 }