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 struct genl_info *info)
43 struct module_reply_data *data = MODULE_REPDATA(reply_base);
44 struct netlink_ext_ack *extack = info ? info->extack : NULL;
45 struct net_device *dev = reply_base->dev;
48 ret = ethnl_ops_begin(dev);
52 ret = module_get_power_mode(dev, data, extack);
57 ethnl_ops_complete(dev);
61 static int module_reply_size(const struct ethnl_req_info *req_base,
62 const struct ethnl_reply_data *reply_base)
64 struct module_reply_data *data = MODULE_REPDATA(reply_base);
67 if (data->power.policy)
68 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE_POLICY */
71 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE */
76 static int module_fill_reply(struct sk_buff *skb,
77 const struct ethnl_req_info *req_base,
78 const struct ethnl_reply_data *reply_base)
80 const struct module_reply_data *data = MODULE_REPDATA(reply_base);
82 if (data->power.policy &&
83 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE_POLICY,
87 if (data->power.mode &&
88 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE, data->power.mode))
94 const struct ethnl_request_ops ethnl_module_request_ops = {
95 .request_cmd = ETHTOOL_MSG_MODULE_GET,
96 .reply_cmd = ETHTOOL_MSG_MODULE_GET_REPLY,
97 .hdr_attr = ETHTOOL_A_MODULE_HEADER,
98 .req_info_size = sizeof(struct module_req_info),
99 .reply_data_size = sizeof(struct module_reply_data),
101 .prepare_data = module_prepare_data,
102 .reply_size = module_reply_size,
103 .fill_reply = module_fill_reply,
108 const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1] = {
109 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
110 [ETHTOOL_A_MODULE_POWER_MODE_POLICY] =
111 NLA_POLICY_RANGE(NLA_U8, ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH,
112 ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO),
115 static int module_set_power_mode(struct net_device *dev, struct nlattr **tb,
116 bool *p_mod, struct netlink_ext_ack *extack)
118 struct ethtool_module_power_mode_params power = {};
119 struct ethtool_module_power_mode_params power_new;
120 const struct ethtool_ops *ops = dev->ethtool_ops;
123 if (!tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY])
126 if (!ops->get_module_power_mode || !ops->set_module_power_mode) {
127 NL_SET_ERR_MSG_ATTR(extack,
128 tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY],
129 "Setting power mode policy is not supported by this device");
133 power_new.policy = nla_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]);
134 ret = ops->get_module_power_mode(dev, &power, extack);
138 if (power_new.policy == power.policy)
142 return ops->set_module_power_mode(dev, &power_new, extack);
145 int ethnl_set_module(struct sk_buff *skb, struct genl_info *info)
147 struct ethnl_req_info req_info = {};
148 struct nlattr **tb = info->attrs;
149 struct net_device *dev;
153 ret = ethnl_parse_header_dev_get(&req_info, tb[ETHTOOL_A_MODULE_HEADER],
154 genl_info_net(info), info->extack,
161 ret = ethnl_ops_begin(dev);
165 ret = module_set_power_mode(dev, tb, &mod, info->extack);
172 ethtool_notify(dev, ETHTOOL_MSG_MODULE_NTF, NULL);
175 ethnl_ops_complete(dev);
178 ethnl_parse_header_dev_put(&req_info);