xref: /openbmc/linux/net/ethtool/module.c (revision 06ba8020)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/ethtool.h>
4 
5 #include "netlink.h"
6 #include "common.h"
7 #include "bitset.h"
8 
9 struct module_req_info {
10 	struct ethnl_req_info base;
11 };
12 
13 struct module_reply_data {
14 	struct ethnl_reply_data	base;
15 	struct ethtool_module_power_mode_params power;
16 };
17 
18 #define MODULE_REPDATA(__reply_base) \
19 	container_of(__reply_base, struct module_reply_data, base)
20 
21 /* MODULE_GET */
22 
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),
25 };
26 
27 static int module_get_power_mode(struct net_device *dev,
28 				 struct module_reply_data *data,
29 				 struct netlink_ext_ack *extack)
30 {
31 	const struct ethtool_ops *ops = dev->ethtool_ops;
32 
33 	if (!ops->get_module_power_mode)
34 		return 0;
35 
36 	return ops->get_module_power_mode(dev, &data->power, extack);
37 }
38 
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)
42 {
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;
46 	int ret;
47 
48 	ret = ethnl_ops_begin(dev);
49 	if (ret < 0)
50 		return ret;
51 
52 	ret = module_get_power_mode(dev, data, extack);
53 	if (ret < 0)
54 		goto out_complete;
55 
56 out_complete:
57 	ethnl_ops_complete(dev);
58 	return ret;
59 }
60 
61 static int module_reply_size(const struct ethnl_req_info *req_base,
62 			     const struct ethnl_reply_data *reply_base)
63 {
64 	struct module_reply_data *data = MODULE_REPDATA(reply_base);
65 	int len = 0;
66 
67 	if (data->power.policy)
68 		len += nla_total_size(sizeof(u8));	/* _MODULE_POWER_MODE_POLICY */
69 
70 	if (data->power.mode)
71 		len += nla_total_size(sizeof(u8));	/* _MODULE_POWER_MODE */
72 
73 	return len;
74 }
75 
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)
79 {
80 	const struct module_reply_data *data = MODULE_REPDATA(reply_base);
81 
82 	if (data->power.policy &&
83 	    nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE_POLICY,
84 		       data->power.policy))
85 		return -EMSGSIZE;
86 
87 	if (data->power.mode &&
88 	    nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE, data->power.mode))
89 		return -EMSGSIZE;
90 
91 	return 0;
92 }
93 
94 /* MODULE_SET */
95 
96 const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1] = {
97 	[ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
98 	[ETHTOOL_A_MODULE_POWER_MODE_POLICY] =
99 		NLA_POLICY_RANGE(NLA_U8, ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH,
100 				 ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO),
101 };
102 
103 static int
104 ethnl_set_module_validate(struct ethnl_req_info *req_info,
105 			  struct genl_info *info)
106 {
107 	const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
108 	struct nlattr **tb = info->attrs;
109 
110 	if (!tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY])
111 		return 0;
112 
113 	if (!ops->get_module_power_mode || !ops->set_module_power_mode) {
114 		NL_SET_ERR_MSG_ATTR(info->extack,
115 				    tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY],
116 				    "Setting power mode policy is not supported by this device");
117 		return -EOPNOTSUPP;
118 	}
119 
120 	return 1;
121 }
122 
123 static int
124 ethnl_set_module(struct ethnl_req_info *req_info, struct genl_info *info)
125 {
126 	struct ethtool_module_power_mode_params power = {};
127 	struct ethtool_module_power_mode_params power_new;
128 	const struct ethtool_ops *ops;
129 	struct net_device *dev = req_info->dev;
130 	struct nlattr **tb = info->attrs;
131 	int ret;
132 
133 	ops = dev->ethtool_ops;
134 
135 	power_new.policy = nla_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]);
136 	ret = ops->get_module_power_mode(dev, &power, info->extack);
137 	if (ret < 0)
138 		return ret;
139 
140 	if (power_new.policy == power.policy)
141 		return 0;
142 
143 	ret = ops->set_module_power_mode(dev, &power_new, info->extack);
144 	return ret < 0 ? ret : 1;
145 }
146 
147 const struct ethnl_request_ops ethnl_module_request_ops = {
148 	.request_cmd		= ETHTOOL_MSG_MODULE_GET,
149 	.reply_cmd		= ETHTOOL_MSG_MODULE_GET_REPLY,
150 	.hdr_attr		= ETHTOOL_A_MODULE_HEADER,
151 	.req_info_size		= sizeof(struct module_req_info),
152 	.reply_data_size	= sizeof(struct module_reply_data),
153 
154 	.prepare_data		= module_prepare_data,
155 	.reply_size		= module_reply_size,
156 	.fill_reply		= module_fill_reply,
157 
158 	.set_validate		= ethnl_set_module_validate,
159 	.set			= ethnl_set_module,
160 	.set_ntf_cmd		= ETHTOOL_MSG_MODULE_NTF,
161 };
162