ethtool: wire up set policies to ops
[linux-block.git] / net / ethtool / tsinfo.c
CommitLineData
5b071c59
MK
1// SPDX-License-Identifier: GPL-2.0-only
2
3#include <linux/net_tstamp.h>
4
5#include "netlink.h"
6#include "common.h"
7#include "bitset.h"
8
9struct tsinfo_req_info {
10 struct ethnl_req_info base;
11};
12
13struct tsinfo_reply_data {
14 struct ethnl_reply_data base;
15 struct ethtool_ts_info ts_info;
16};
17
18#define TSINFO_REPDATA(__reply_base) \
19 container_of(__reply_base, struct tsinfo_reply_data, base)
20
4f30974f 21const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_MAX + 1] = {
5b071c59
MK
22 [ETHTOOL_A_TSINFO_UNSPEC] = { .type = NLA_REJECT },
23 [ETHTOOL_A_TSINFO_HEADER] = { .type = NLA_NESTED },
24 [ETHTOOL_A_TSINFO_TIMESTAMPING] = { .type = NLA_REJECT },
25 [ETHTOOL_A_TSINFO_TX_TYPES] = { .type = NLA_REJECT },
26 [ETHTOOL_A_TSINFO_RX_FILTERS] = { .type = NLA_REJECT },
27 [ETHTOOL_A_TSINFO_PHC_INDEX] = { .type = NLA_REJECT },
28};
29
30static int tsinfo_prepare_data(const struct ethnl_req_info *req_base,
31 struct ethnl_reply_data *reply_base,
32 struct genl_info *info)
33{
34 struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base);
35 struct net_device *dev = reply_base->dev;
36 int ret;
37
38 ret = ethnl_ops_begin(dev);
39 if (ret < 0)
40 return ret;
41 ret = __ethtool_get_ts_info(dev, &data->ts_info);
42 ethnl_ops_complete(dev);
43
44 return ret;
45}
46
47static int tsinfo_reply_size(const struct ethnl_req_info *req_base,
48 const struct ethnl_reply_data *reply_base)
49{
50 const struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base);
51 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
52 const struct ethtool_ts_info *ts_info = &data->ts_info;
53 int len = 0;
54 int ret;
55
56 BUILD_BUG_ON(__SOF_TIMESTAMPING_CNT > 32);
57 BUILD_BUG_ON(__HWTSTAMP_TX_CNT > 32);
58 BUILD_BUG_ON(__HWTSTAMP_FILTER_CNT > 32);
59
60 if (ts_info->so_timestamping) {
61 ret = ethnl_bitset32_size(&ts_info->so_timestamping, NULL,
62 __SOF_TIMESTAMPING_CNT,
63 sof_timestamping_names, compact);
64 if (ret < 0)
65 return ret;
66 len += ret; /* _TSINFO_TIMESTAMPING */
67 }
68 if (ts_info->tx_types) {
69 ret = ethnl_bitset32_size(&ts_info->tx_types, NULL,
70 __HWTSTAMP_TX_CNT,
71 ts_tx_type_names, compact);
72 if (ret < 0)
73 return ret;
74 len += ret; /* _TSINFO_TX_TYPES */
75 }
76 if (ts_info->rx_filters) {
77 ret = ethnl_bitset32_size(&ts_info->rx_filters, NULL,
78 __HWTSTAMP_FILTER_CNT,
79 ts_rx_filter_names, compact);
80 if (ret < 0)
81 return ret;
82 len += ret; /* _TSINFO_RX_FILTERS */
83 }
84 if (ts_info->phc_index >= 0)
85 len += nla_total_size(sizeof(u32)); /* _TSINFO_PHC_INDEX */
86
87 return len;
88}
89
90static int tsinfo_fill_reply(struct sk_buff *skb,
91 const struct ethnl_req_info *req_base,
92 const struct ethnl_reply_data *reply_base)
93{
94 const struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base);
95 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
96 const struct ethtool_ts_info *ts_info = &data->ts_info;
97 int ret;
98
99 if (ts_info->so_timestamping) {
100 ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_TIMESTAMPING,
101 &ts_info->so_timestamping, NULL,
102 __SOF_TIMESTAMPING_CNT,
103 sof_timestamping_names, compact);
104 if (ret < 0)
105 return ret;
106 }
107 if (ts_info->tx_types) {
108 ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_TX_TYPES,
109 &ts_info->tx_types, NULL,
110 __HWTSTAMP_TX_CNT,
111 ts_tx_type_names, compact);
112 if (ret < 0)
113 return ret;
114 }
115 if (ts_info->rx_filters) {
116 ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_RX_FILTERS,
117 &ts_info->rx_filters, NULL,
118 __HWTSTAMP_FILTER_CNT,
119 ts_rx_filter_names, compact);
120 if (ret < 0)
121 return ret;
122 }
123 if (ts_info->phc_index >= 0 &&
124 nla_put_u32(skb, ETHTOOL_A_TSINFO_PHC_INDEX, ts_info->phc_index))
125 return -EMSGSIZE;
126
127 return 0;
128}
129
130const struct ethnl_request_ops ethnl_tsinfo_request_ops = {
131 .request_cmd = ETHTOOL_MSG_TSINFO_GET,
132 .reply_cmd = ETHTOOL_MSG_TSINFO_GET_REPLY,
133 .hdr_attr = ETHTOOL_A_TSINFO_HEADER,
5b071c59
MK
134 .req_info_size = sizeof(struct tsinfo_req_info),
135 .reply_data_size = sizeof(struct tsinfo_reply_data),
5b071c59
MK
136
137 .prepare_data = tsinfo_prepare_data,
138 .reply_size = tsinfo_reply_size,
139 .fill_reply = tsinfo_fill_reply,
140};