xref: /linux/net/ethtool/pse-pd.c (revision 447e140e66fd226350b3ce86cffc965eaae4c856)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // ethtool interface for Ethernet PSE (Power Sourcing Equipment)
4 // and PD (Powered Device)
5 //
6 // Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
7 //
8 
9 #include "common.h"
10 #include "linux/pse-pd/pse.h"
11 #include "netlink.h"
12 #include <linux/ethtool_netlink.h>
13 #include <linux/ethtool.h>
14 #include <linux/phy.h>
15 
16 struct pse_req_info {
17 	struct ethnl_req_info base;
18 };
19 
20 struct pse_reply_data {
21 	struct ethnl_reply_data	base;
22 	struct pse_control_status status;
23 };
24 
25 #define PSE_REPDATA(__reply_base) \
26 	container_of(__reply_base, struct pse_reply_data, base)
27 
28 /* PSE_GET */
29 
30 const struct nla_policy ethnl_pse_get_policy[ETHTOOL_A_PSE_HEADER + 1] = {
31 	[ETHTOOL_A_PSE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
32 };
33 
34 static int pse_get_pse_attributes(struct net_device *dev,
35 				  struct netlink_ext_ack *extack,
36 				  struct pse_reply_data *data)
37 {
38 	struct phy_device *phydev = dev->phydev;
39 
40 	if (!phydev) {
41 		NL_SET_ERR_MSG(extack, "No PHY is attached");
42 		return -EOPNOTSUPP;
43 	}
44 
45 	if (!phydev->psec) {
46 		NL_SET_ERR_MSG(extack, "No PSE is attached");
47 		return -EOPNOTSUPP;
48 	}
49 
50 	memset(&data->status, 0, sizeof(data->status));
51 
52 	return pse_ethtool_get_status(phydev->psec, extack, &data->status);
53 }
54 
55 static int pse_prepare_data(const struct ethnl_req_info *req_base,
56 			    struct ethnl_reply_data *reply_base,
57 			    const struct genl_info *info)
58 {
59 	struct pse_reply_data *data = PSE_REPDATA(reply_base);
60 	struct net_device *dev = reply_base->dev;
61 	int ret;
62 
63 	ret = ethnl_ops_begin(dev);
64 	if (ret < 0)
65 		return ret;
66 
67 	ret = pse_get_pse_attributes(dev, info->extack, data);
68 
69 	ethnl_ops_complete(dev);
70 
71 	return ret;
72 }
73 
74 static int pse_reply_size(const struct ethnl_req_info *req_base,
75 			  const struct ethnl_reply_data *reply_base)
76 {
77 	const struct pse_reply_data *data = PSE_REPDATA(reply_base);
78 	const struct pse_control_status *st = &data->status;
79 	int len = 0;
80 
81 	if (st->podl_admin_state > 0)
82 		len += nla_total_size(sizeof(u32)); /* _PODL_PSE_ADMIN_STATE */
83 	if (st->podl_pw_status > 0)
84 		len += nla_total_size(sizeof(u32)); /* _PODL_PSE_PW_D_STATUS */
85 	if (st->c33_admin_state > 0)
86 		len += nla_total_size(sizeof(u32)); /* _C33_PSE_ADMIN_STATE */
87 	if (st->c33_pw_status > 0)
88 		len += nla_total_size(sizeof(u32)); /* _C33_PSE_PW_D_STATUS */
89 
90 	return len;
91 }
92 
93 static int pse_fill_reply(struct sk_buff *skb,
94 			  const struct ethnl_req_info *req_base,
95 			  const struct ethnl_reply_data *reply_base)
96 {
97 	const struct pse_reply_data *data = PSE_REPDATA(reply_base);
98 	const struct pse_control_status *st = &data->status;
99 
100 	if (st->podl_admin_state > 0 &&
101 	    nla_put_u32(skb, ETHTOOL_A_PODL_PSE_ADMIN_STATE,
102 			st->podl_admin_state))
103 		return -EMSGSIZE;
104 
105 	if (st->podl_pw_status > 0 &&
106 	    nla_put_u32(skb, ETHTOOL_A_PODL_PSE_PW_D_STATUS,
107 			st->podl_pw_status))
108 		return -EMSGSIZE;
109 
110 	if (st->c33_admin_state > 0 &&
111 	    nla_put_u32(skb, ETHTOOL_A_C33_PSE_ADMIN_STATE,
112 			st->c33_admin_state))
113 		return -EMSGSIZE;
114 
115 	if (st->c33_pw_status > 0 &&
116 	    nla_put_u32(skb, ETHTOOL_A_C33_PSE_PW_D_STATUS,
117 			st->c33_pw_status))
118 		return -EMSGSIZE;
119 
120 	return 0;
121 }
122 
123 /* PSE_SET */
124 
125 const struct nla_policy ethnl_pse_set_policy[ETHTOOL_A_PSE_MAX + 1] = {
126 	[ETHTOOL_A_PSE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
127 	[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL] =
128 		NLA_POLICY_RANGE(NLA_U32, ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED,
129 				 ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED),
130 	[ETHTOOL_A_C33_PSE_ADMIN_CONTROL] =
131 		NLA_POLICY_RANGE(NLA_U32, ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED,
132 				 ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED),
133 };
134 
135 static int
136 ethnl_set_pse_validate(struct ethnl_req_info *req_info, struct genl_info *info)
137 {
138 	struct net_device *dev = req_info->dev;
139 	struct nlattr **tb = info->attrs;
140 	struct phy_device *phydev;
141 
142 	phydev = dev->phydev;
143 	if (!phydev) {
144 		NL_SET_ERR_MSG(info->extack, "No PHY is attached");
145 		return -EOPNOTSUPP;
146 	}
147 
148 	if (!phydev->psec) {
149 		NL_SET_ERR_MSG(info->extack, "No PSE is attached");
150 		return -EOPNOTSUPP;
151 	}
152 
153 	if (tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL] &&
154 	    !pse_has_podl(phydev->psec)) {
155 		NL_SET_ERR_MSG_ATTR(info->extack,
156 				    tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL],
157 				    "setting PoDL PSE admin control not supported");
158 		return -EOPNOTSUPP;
159 	}
160 	if (tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL] &&
161 	    !pse_has_c33(phydev->psec)) {
162 		NL_SET_ERR_MSG_ATTR(info->extack,
163 				    tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL],
164 				    "setting C33 PSE admin control not supported");
165 		return -EOPNOTSUPP;
166 	}
167 
168 	return 1;
169 }
170 
171 static int
172 ethnl_set_pse(struct ethnl_req_info *req_info, struct genl_info *info)
173 {
174 	struct net_device *dev = req_info->dev;
175 	struct pse_control_config config = {};
176 	struct nlattr **tb = info->attrs;
177 	struct phy_device *phydev;
178 
179 	phydev = dev->phydev;
180 	/* These values are already validated by the ethnl_pse_set_policy */
181 	if (pse_has_podl(phydev->psec))
182 		config.podl_admin_control = nla_get_u32(tb[ETHTOOL_A_PODL_PSE_ADMIN_CONTROL]);
183 	if (pse_has_c33(phydev->psec))
184 		config.c33_admin_control = nla_get_u32(tb[ETHTOOL_A_C33_PSE_ADMIN_CONTROL]);
185 
186 	/* Return errno directly - PSE has no notification */
187 	return pse_ethtool_set_config(phydev->psec, info->extack, &config);
188 }
189 
190 const struct ethnl_request_ops ethnl_pse_request_ops = {
191 	.request_cmd		= ETHTOOL_MSG_PSE_GET,
192 	.reply_cmd		= ETHTOOL_MSG_PSE_GET_REPLY,
193 	.hdr_attr		= ETHTOOL_A_PSE_HEADER,
194 	.req_info_size		= sizeof(struct pse_req_info),
195 	.reply_data_size	= sizeof(struct pse_reply_data),
196 
197 	.prepare_data		= pse_prepare_data,
198 	.reply_size		= pse_reply_size,
199 	.fill_reply		= pse_fill_reply,
200 
201 	.set_validate		= ethnl_set_pse_validate,
202 	.set			= ethnl_set_pse,
203 	/* PSE has no notification */
204 };
205