15b071c59SMichal Kubecek // SPDX-License-Identifier: GPL-2.0-only
25b071c59SMichal Kubecek
35b071c59SMichal Kubecek #include <linux/net_tstamp.h>
45b071c59SMichal Kubecek
55b071c59SMichal Kubecek #include "netlink.h"
65b071c59SMichal Kubecek #include "common.h"
75b071c59SMichal Kubecek #include "bitset.h"
85b071c59SMichal Kubecek
95b071c59SMichal Kubecek struct tsinfo_req_info {
105b071c59SMichal Kubecek struct ethnl_req_info base;
115b071c59SMichal Kubecek };
125b071c59SMichal Kubecek
135b071c59SMichal Kubecek struct tsinfo_reply_data {
145b071c59SMichal Kubecek struct ethnl_reply_data base;
155b071c59SMichal Kubecek struct ethtool_ts_info ts_info;
165b071c59SMichal Kubecek };
175b071c59SMichal Kubecek
185b071c59SMichal Kubecek #define TSINFO_REPDATA(__reply_base) \
195b071c59SMichal Kubecek container_of(__reply_base, struct tsinfo_reply_data, base)
205b071c59SMichal Kubecek
21ff419afaSJakub Kicinski const struct nla_policy ethnl_tsinfo_get_policy[] = {
22*329d9c33SJakub Kicinski [ETHTOOL_A_TSINFO_HEADER] =
23*329d9c33SJakub Kicinski NLA_POLICY_NESTED(ethnl_header_policy),
245b071c59SMichal Kubecek };
255b071c59SMichal Kubecek
tsinfo_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,const struct genl_info * info)265b071c59SMichal Kubecek static int tsinfo_prepare_data(const struct ethnl_req_info *req_base,
275b071c59SMichal Kubecek struct ethnl_reply_data *reply_base,
285b071c59SMichal Kubecek const struct genl_info *info)
295b071c59SMichal Kubecek {
305b071c59SMichal Kubecek struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base);
315b071c59SMichal Kubecek struct net_device *dev = reply_base->dev;
325b071c59SMichal Kubecek int ret;
335b071c59SMichal Kubecek
345b071c59SMichal Kubecek ret = ethnl_ops_begin(dev);
355b071c59SMichal Kubecek if (ret < 0)
365b071c59SMichal Kubecek return ret;
375b071c59SMichal Kubecek ret = __ethtool_get_ts_info(dev, &data->ts_info);
385b071c59SMichal Kubecek ethnl_ops_complete(dev);
395b071c59SMichal Kubecek
405b071c59SMichal Kubecek return ret;
415b071c59SMichal Kubecek }
425b071c59SMichal Kubecek
tsinfo_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)435b071c59SMichal Kubecek static int tsinfo_reply_size(const struct ethnl_req_info *req_base,
445b071c59SMichal Kubecek const struct ethnl_reply_data *reply_base)
455b071c59SMichal Kubecek {
465b071c59SMichal Kubecek const struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base);
475b071c59SMichal Kubecek bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
485b071c59SMichal Kubecek const struct ethtool_ts_info *ts_info = &data->ts_info;
495b071c59SMichal Kubecek int len = 0;
505b071c59SMichal Kubecek int ret;
515b071c59SMichal Kubecek
525b071c59SMichal Kubecek BUILD_BUG_ON(__SOF_TIMESTAMPING_CNT > 32);
535b071c59SMichal Kubecek BUILD_BUG_ON(__HWTSTAMP_TX_CNT > 32);
545b071c59SMichal Kubecek BUILD_BUG_ON(__HWTSTAMP_FILTER_CNT > 32);
555b071c59SMichal Kubecek
565b071c59SMichal Kubecek if (ts_info->so_timestamping) {
575b071c59SMichal Kubecek ret = ethnl_bitset32_size(&ts_info->so_timestamping, NULL,
585b071c59SMichal Kubecek __SOF_TIMESTAMPING_CNT,
595b071c59SMichal Kubecek sof_timestamping_names, compact);
605b071c59SMichal Kubecek if (ret < 0)
615b071c59SMichal Kubecek return ret;
625b071c59SMichal Kubecek len += ret; /* _TSINFO_TIMESTAMPING */
635b071c59SMichal Kubecek }
645b071c59SMichal Kubecek if (ts_info->tx_types) {
655b071c59SMichal Kubecek ret = ethnl_bitset32_size(&ts_info->tx_types, NULL,
665b071c59SMichal Kubecek __HWTSTAMP_TX_CNT,
675b071c59SMichal Kubecek ts_tx_type_names, compact);
685b071c59SMichal Kubecek if (ret < 0)
695b071c59SMichal Kubecek return ret;
705b071c59SMichal Kubecek len += ret; /* _TSINFO_TX_TYPES */
715b071c59SMichal Kubecek }
725b071c59SMichal Kubecek if (ts_info->rx_filters) {
735b071c59SMichal Kubecek ret = ethnl_bitset32_size(&ts_info->rx_filters, NULL,
745b071c59SMichal Kubecek __HWTSTAMP_FILTER_CNT,
755b071c59SMichal Kubecek ts_rx_filter_names, compact);
765b071c59SMichal Kubecek if (ret < 0)
775b071c59SMichal Kubecek return ret;
785b071c59SMichal Kubecek len += ret; /* _TSINFO_RX_FILTERS */
795b071c59SMichal Kubecek }
805b071c59SMichal Kubecek if (ts_info->phc_index >= 0)
815b071c59SMichal Kubecek len += nla_total_size(sizeof(u32)); /* _TSINFO_PHC_INDEX */
825b071c59SMichal Kubecek
835b071c59SMichal Kubecek return len;
845b071c59SMichal Kubecek }
855b071c59SMichal Kubecek
tsinfo_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)865b071c59SMichal Kubecek static int tsinfo_fill_reply(struct sk_buff *skb,
875b071c59SMichal Kubecek const struct ethnl_req_info *req_base,
885b071c59SMichal Kubecek const struct ethnl_reply_data *reply_base)
895b071c59SMichal Kubecek {
905b071c59SMichal Kubecek const struct tsinfo_reply_data *data = TSINFO_REPDATA(reply_base);
915b071c59SMichal Kubecek bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
925b071c59SMichal Kubecek const struct ethtool_ts_info *ts_info = &data->ts_info;
935b071c59SMichal Kubecek int ret;
945b071c59SMichal Kubecek
955b071c59SMichal Kubecek if (ts_info->so_timestamping) {
965b071c59SMichal Kubecek ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_TIMESTAMPING,
975b071c59SMichal Kubecek &ts_info->so_timestamping, NULL,
985b071c59SMichal Kubecek __SOF_TIMESTAMPING_CNT,
995b071c59SMichal Kubecek sof_timestamping_names, compact);
1005b071c59SMichal Kubecek if (ret < 0)
1015b071c59SMichal Kubecek return ret;
1025b071c59SMichal Kubecek }
1035b071c59SMichal Kubecek if (ts_info->tx_types) {
1045b071c59SMichal Kubecek ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_TX_TYPES,
1055b071c59SMichal Kubecek &ts_info->tx_types, NULL,
1065b071c59SMichal Kubecek __HWTSTAMP_TX_CNT,
1075b071c59SMichal Kubecek ts_tx_type_names, compact);
1085b071c59SMichal Kubecek if (ret < 0)
1095b071c59SMichal Kubecek return ret;
1105b071c59SMichal Kubecek }
1115b071c59SMichal Kubecek if (ts_info->rx_filters) {
1125b071c59SMichal Kubecek ret = ethnl_put_bitset32(skb, ETHTOOL_A_TSINFO_RX_FILTERS,
1135b071c59SMichal Kubecek &ts_info->rx_filters, NULL,
1145b071c59SMichal Kubecek __HWTSTAMP_FILTER_CNT,
1155b071c59SMichal Kubecek ts_rx_filter_names, compact);
1165b071c59SMichal Kubecek if (ret < 0)
1175b071c59SMichal Kubecek return ret;
1185b071c59SMichal Kubecek }
1195b071c59SMichal Kubecek if (ts_info->phc_index >= 0 &&
1205b071c59SMichal Kubecek nla_put_u32(skb, ETHTOOL_A_TSINFO_PHC_INDEX, ts_info->phc_index))
1215b071c59SMichal Kubecek return -EMSGSIZE;
1225b071c59SMichal Kubecek
1235b071c59SMichal Kubecek return 0;
1245b071c59SMichal Kubecek }
1255b071c59SMichal Kubecek
1265b071c59SMichal Kubecek const struct ethnl_request_ops ethnl_tsinfo_request_ops = {
1275b071c59SMichal Kubecek .request_cmd = ETHTOOL_MSG_TSINFO_GET,
1285b071c59SMichal Kubecek .reply_cmd = ETHTOOL_MSG_TSINFO_GET_REPLY,
1295b071c59SMichal Kubecek .hdr_attr = ETHTOOL_A_TSINFO_HEADER,
1305b071c59SMichal Kubecek .req_info_size = sizeof(struct tsinfo_req_info),
1315b071c59SMichal Kubecek .reply_data_size = sizeof(struct tsinfo_reply_data),
1325b071c59SMichal Kubecek
1335b071c59SMichal Kubecek .prepare_data = tsinfo_prepare_data,
1345b071c59SMichal Kubecek .reply_size = tsinfo_reply_size,
1355b071c59SMichal Kubecek .fill_reply = tsinfo_fill_reply,
1365b071c59SMichal Kubecek };
137