1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/ethtool.h>
#include <linux/phy.h>
#include <linux/slab.h>
#include "netlink.h"
#include "common.h"
/* Channels A-D only; WORST and LINK are exclusive alternatives */
#define PHY_MSE_CHANNEL_COUNT 4
struct mse_req_info {
struct ethnl_req_info base;
};
struct mse_snapshot_entry {
struct phy_mse_snapshot snapshot;
int channel;
};
struct mse_reply_data {
struct ethnl_reply_data base;
struct phy_mse_capability capability;
struct mse_snapshot_entry *snapshots;
unsigned int num_snapshots;
};
static struct mse_reply_data *
mse_repdata(const struct ethnl_reply_data *reply_base)
{
return container_of(reply_base, struct mse_reply_data, base);
}
const struct nla_policy ethnl_mse_get_policy[] = {
[ETHTOOL_A_MSE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy_phy),
};
static int get_snapshot_if_supported(struct phy_device *phydev,
struct mse_reply_data *data,
unsigned int *idx, u32 cap_bit,
enum phy_mse_channel channel)
{
int ret;
if (data->capability.supported_caps & cap_bit) {
ret = phydev->drv->get_mse_snapshot(phydev, channel,
&data->snapshots[*idx].snapshot);
if (ret)
return ret;
data->snapshots[*idx].channel = channel;
(*idx)++;
}
return 0;
}
static int mse_get_channels(struct phy_device *phydev,
struct mse_reply_data *data)
{
unsigned int i = 0;
int ret;
if (!data->capability.supported_caps)
return 0;
data->snapshots = kcalloc(PHY_MSE_CHANNEL_COUNT,
sizeof(*data->snapshots), GFP_KERNEL);
if (!data->snapshots)
return -ENOMEM;
/* Priority 1: Individual channels */
ret = get_snapshot_if_supported(phydev, data, &i, PHY_MSE_CAP_CHANNEL_A,
PHY_MSE_CHANNEL_A);
if (ret)
return ret;
ret = get_snapshot_if_supported(phydev, data, &i, PHY_MSE_CAP_CHANNEL_B,
PHY_MSE_CHANNEL_B);
if (ret)
return ret;
ret = get_snapshot_if_supported(phydev, data, &i, PHY_MSE_CAP_CHANNEL_C,
PHY_MSE_CHANNEL_C);
if (ret)
return ret;
ret = get_snapshot_if_supported(phydev, data, &i, PHY_MSE_CAP_CHANNEL_D,
PHY_MSE_CHANNEL_D);
if (ret)
return ret;
/* If any individual channels were found, we are done. */
if (i > 0) {
data->num_snapshots = i;
return 0;
}
/* Priority 2: Worst channel, if no individual channels supported. */
ret = get_snapshot_if_supported(phydev, data, &i,
PHY_MSE_CAP_WORST_CHANNEL,
PHY_MSE_CHANNEL_WORST);
if (ret)
return ret;
/* If worst channel was found, we are done. */
if (i > 0) {
data->num_snapshots = i;
return 0;
}
/* Priority 3: Link-wide, if nothing else is supported. */
ret = get_snapshot_if_supported(phydev, data, &i, PHY_MSE_CAP_LINK,
PHY_MSE_CHANNEL_LINK);
if (ret)
return ret;
data->num_snapshots = i;
return 0;
}
static int mse_prepare_data(const struct ethnl_req_info *req_base,
struct ethnl_reply_data *reply_base,
const struct genl_info *info)
{
struct mse_reply_data *data = mse_repdata(reply_base);
struct net_device *dev = reply_base->dev;
struct phy_device *phydev;
int ret;
phydev = ethnl_req_get_phydev(req_base, info->attrs,
ETHTOOL_A_MSE_HEADER, info->extack);
if (IS_ERR(phydev))
return PTR_ERR(phydev);
if (!phydev)
return -EOPNOTSUPP;
ret = ethnl_ops_begin(dev);
if (ret)
return ret;
mutex_lock(&phydev->lock);
if (!phydev->drv || !phydev->drv->get_mse_capability ||
!phydev->drv->get_mse_snapshot) {
ret = -EOPNOTSUPP;
goto out_unlock;
}
if (!phydev->link) {
ret = -ENETDOWN;
goto out_unlock;
}
ret = phydev->drv->get_mse_capability(phydev, &data->capability);
if (ret)
goto out_unlock;
ret = mse_get_channels(phydev, data);
out_unlock:
mutex_unlock(&phydev->lock);
ethnl_ops_complete(dev);
if (ret)
kfree(data->snapshots);
return ret;
}
static void mse_cleanup_data(struct ethnl_reply_data *reply_base)
{
struct mse_reply_data *data = mse_repdata(reply_base);
kfree(data->snapshots);
}
static int mse_reply_size(const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
const struct mse_reply_data *data = mse_repdata(reply_base);
size_t len = 0;
unsigned int i;
/* ETHTOOL_A_MSE_CAPABILITIES */
len += nla_total_size(0);
if (data->capability.supported_caps & PHY_MSE_CAP_AVG)
/* ETHTOOL_A_MSE_CAPABILITIES_MAX_AVERAGE_MSE */
len += nla_total_size(sizeof(u64));
if (data->capability.supported_caps & (PHY_MSE_CAP_PEAK |
PHY_MSE_CAP_WORST_PEAK))
/* ETHTOOL_A_MSE_CAPABILITIES_MAX_PEAK_MSE */
len += nla_total_size(sizeof(u64));
/* ETHTOOL_A_MSE_CAPABILITIES_REFRESH_RATE_PS */
len += nla_total_size(sizeof(u64));
/* ETHTOOL_A_MSE_CAPABILITIES_NUM_SYMBOLS */
len += nla_total_size(sizeof(u64));
for (i = 0; i < data->num_snapshots; i++) {
size_t snapshot_len = 0;
/* Per-channel nest (e.g., ETHTOOL_A_MSE_CHANNEL_A / _B / _C /
* _D / _WORST_CHANNEL / _LINK)
*/
snapshot_len += nla_total_size(0);
if (data->capability.supported_caps & PHY_MSE_CAP_AVG)
snapshot_len += nla_total_size(sizeof(u64));
if (data->capability.supported_caps & PHY_MSE_CAP_PEAK)
snapshot_len += nla_total_size(sizeof(u64));
if (data->capability.supported_caps & PHY_MSE_CAP_WORST_PEAK)
snapshot_len += nla_total_size(sizeof(u64));
len += snapshot_len;
}
return len;
}
static int mse_channel_to_attr(int ch)
{
switch (ch) {
case PHY_MSE_CHANNEL_A:
return ETHTOOL_A_MSE_CHANNEL_A;
case PHY_MSE_CHANNEL_B:
return ETHTOOL_A_MSE_CHANNEL_B;
case PHY_MSE_CHANNEL_C:
return ETHTOOL_A_MSE_CHANNEL_C;
case PHY_MSE_CHANNEL_D:
return ETHTOOL_A_MSE_CHANNEL_D;
case PHY_MSE_CHANNEL_WORST:
return ETHTOOL_A_MSE_WORST_CHANNEL;
case PHY_MSE_CHANNEL_LINK:
return ETHTOOL_A_MSE_LINK;
default:
return -EINVAL;
}
}
static int mse_fill_reply(struct sk_buff *skb,
const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
const struct mse_reply_data *data = mse_repdata(reply_base);
struct nlattr *nest;
unsigned int i;
int ret;
nest = nla_nest_start(skb, ETHTOOL_A_MSE_CAPABILITIES);
if (!nest)
return -EMSGSIZE;
if (data->capability.supported_caps & PHY_MSE_CAP_AVG) {
ret = nla_put_uint(skb,
ETHTOOL_A_MSE_CAPABILITIES_MAX_AVERAGE_MSE,
data->capability.max_average_mse);
if (ret < 0)
goto nla_put_nest_failure;
}
if (data->capability.supported_caps & (PHY_MSE_CAP_PEAK |
PHY_MSE_CAP_WORST_PEAK)) {
ret = nla_put_uint(skb, ETHTOOL_A_MSE_CAPABILITIES_MAX_PEAK_MSE,
data->capability.max_peak_mse);
if (ret < 0)
goto nla_put_nest_failure;
}
ret = nla_put_uint(skb, ETHTOOL_A_MSE_CAPABILITIES_REFRESH_RATE_PS,
data->capability.refresh_rate_ps);
if (ret < 0)
goto nla_put_nest_failure;
ret = nla_put_uint(skb, ETHTOOL_A_MSE_CAPABILITIES_NUM_SYMBOLS,
data->capability.num_symbols);
if (ret < 0)
goto nla_put_nest_failure;
nla_nest_end(skb, nest);
for (i = 0; i < data->num_snapshots; i++) {
const struct mse_snapshot_entry *s = &data->snapshots[i];
int chan_attr;
chan_attr = mse_channel_to_attr(s->channel);
if (chan_attr < 0)
return chan_attr;
nest = nla_nest_start(skb, chan_attr);
if (!nest)
return -EMSGSIZE;
if (data->capability.supported_caps & PHY_MSE_CAP_AVG) {
ret = nla_put_uint(skb,
ETHTOOL_A_MSE_SNAPSHOT_AVERAGE_MSE,
s->snapshot.average_mse);
if (ret)
goto nla_put_nest_failure;
}
if (data->capability.supported_caps & PHY_MSE_CAP_PEAK) {
ret = nla_put_uint(skb, ETHTOOL_A_MSE_SNAPSHOT_PEAK_MSE,
s->snapshot.peak_mse);
if (ret)
goto nla_put_nest_failure;
}
if (data->capability.supported_caps & PHY_MSE_CAP_WORST_PEAK) {
ret = nla_put_uint(skb,
ETHTOOL_A_MSE_SNAPSHOT_WORST_PEAK_MSE,
s->snapshot.worst_peak_mse);
if (ret)
goto nla_put_nest_failure;
}
nla_nest_end(skb, nest);
}
return 0;
nla_put_nest_failure:
nla_nest_cancel(skb, nest);
return ret;
}
const struct ethnl_request_ops ethnl_mse_request_ops = {
.request_cmd = ETHTOOL_MSG_MSE_GET,
.reply_cmd = ETHTOOL_MSG_MSE_GET_REPLY,
.hdr_attr = ETHTOOL_A_MSE_HEADER,
.req_info_size = sizeof(struct mse_req_info),
.reply_data_size = sizeof(struct mse_reply_data),
.prepare_data = mse_prepare_data,
.cleanup_data = mse_cleanup_data,
.reply_size = mse_reply_size,
.fill_reply = mse_fill_reply,
};
|