1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/ethtool.h>
9 struct module_req_info {
10 struct ethnl_req_info base;
13 struct module_reply_data {
14 struct ethnl_reply_data base;
15 struct ethtool_module_power_mode_params power;
18 #define MODULE_REPDATA(__reply_base) \
19 container_of(__reply_base, struct module_reply_data, base)
23 const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1] = {
24 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
27 static int module_get_power_mode(struct net_device *dev,
28 struct module_reply_data *data,
29 struct netlink_ext_ack *extack)
31 const struct ethtool_ops *ops = dev->ethtool_ops;
33 if (!ops->get_module_power_mode)
36 return ops->get_module_power_mode(dev, &data->power, extack);
39 static int module_prepare_data(const struct ethnl_req_info *req_base,
40 struct ethnl_reply_data *reply_base,
41 const struct genl_info *info)
43 struct module_reply_data *data = MODULE_REPDATA(reply_base);
44 struct net_device *dev = reply_base->dev;
47 ret = ethnl_ops_begin(dev);
51 ret = module_get_power_mode(dev, data, info->extack);
56 ethnl_ops_complete(dev);
60 static int module_reply_size(const struct ethnl_req_info *req_base,
61 const struct ethnl_reply_data *reply_base)
63 struct module_reply_data *data = MODULE_REPDATA(reply_base);
66 if (data->power.policy)
67 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE_POLICY */
70 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE */
75 static int module_fill_reply(struct sk_buff *skb,
76 const struct ethnl_req_info *req_base,
77 const struct ethnl_reply_data *reply_base)
79 const struct module_reply_data *data = MODULE_REPDATA(reply_base);
81 if (data->power.policy &&
82 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE_POLICY,
86 if (data->power.mode &&
87 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE, data->power.mode))
95 const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1] = {
96 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
97 [ETHTOOL_A_MODULE_POWER_MODE_POLICY] =
98 NLA_POLICY_RANGE(NLA_U8, ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH,
99 ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO),
103 ethnl_set_module_validate(struct ethnl_req_info *req_info,
104 struct genl_info *info)
106 const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
107 struct nlattr **tb = info->attrs;
109 if (!tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY])
112 if (!ops->get_module_power_mode || !ops->set_module_power_mode) {
113 NL_SET_ERR_MSG_ATTR(info->extack,
114 tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY],
115 "Setting power mode policy is not supported by this device");
123 ethnl_set_module(struct ethnl_req_info *req_info, struct genl_info *info)
125 struct ethtool_module_power_mode_params power = {};
126 struct ethtool_module_power_mode_params power_new;
127 const struct ethtool_ops *ops;
128 struct net_device *dev = req_info->dev;
129 struct nlattr **tb = info->attrs;
132 ops = dev->ethtool_ops;
134 power_new.policy = nla_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]);
135 ret = ops->get_module_power_mode(dev, &power, info->extack);
139 if (power_new.policy == power.policy)
142 ret = ops->set_module_power_mode(dev, &power_new, info->extack);
143 return ret < 0 ? ret : 1;
146 const struct ethnl_request_ops ethnl_module_request_ops = {
147 .request_cmd = ETHTOOL_MSG_MODULE_GET,
148 .reply_cmd = ETHTOOL_MSG_MODULE_GET_REPLY,
149 .hdr_attr = ETHTOOL_A_MODULE_HEADER,
150 .req_info_size = sizeof(struct module_req_info),
151 .reply_data_size = sizeof(struct module_reply_data),
153 .prepare_data = module_prepare_data,
154 .reply_size = module_reply_size,
155 .fill_reply = module_fill_reply,
157 .set_validate = ethnl_set_module_validate,
158 .set = ethnl_set_module,
159 .set_ntf_cmd = ETHTOOL_MSG_MODULE_NTF,