1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2023 Bootlin 4 * 5 */ 6 #include "common.h" 7 #include "netlink.h" 8 9 #include <linux/phy.h> 10 #include <linux/phy_link_topology.h> 11 #include <linux/sfp.h> 12 13 struct phy_req_info { 14 struct ethnl_req_info base; 15 struct phy_device_node *pdn; 16 }; 17 18 #define PHY_REQINFO(__req_base) \ 19 container_of(__req_base, struct phy_req_info, base) 20 21 const struct nla_policy ethnl_phy_get_policy[ETHTOOL_A_PHY_HEADER + 1] = { 22 [ETHTOOL_A_PHY_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), 23 }; 24 25 /* Caller holds rtnl */ 26 static ssize_t 27 ethnl_phy_reply_size(const struct ethnl_req_info *req_base, 28 struct netlink_ext_ack *extack) 29 { 30 struct phy_req_info *req_info = PHY_REQINFO(req_base); 31 struct phy_device_node *pdn = req_info->pdn; 32 struct phy_device *phydev = pdn->phy; 33 size_t size = 0; 34 35 ASSERT_RTNL(); 36 37 /* ETHTOOL_A_PHY_INDEX */ 38 size += nla_total_size(sizeof(u32)); 39 40 /* ETHTOOL_A_DRVNAME */ 41 if (phydev->drv) 42 size += nla_total_size(strlen(phydev->drv->name) + 1); 43 44 /* ETHTOOL_A_NAME */ 45 size += nla_total_size(strlen(dev_name(&phydev->mdio.dev)) + 1); 46 47 /* ETHTOOL_A_PHY_UPSTREAM_TYPE */ 48 size += nla_total_size(sizeof(u32)); 49 50 if (phy_on_sfp(phydev)) { 51 const char *upstream_sfp_name = sfp_get_name(pdn->parent_sfp_bus); 52 53 /* ETHTOOL_A_PHY_UPSTREAM_SFP_NAME */ 54 if (upstream_sfp_name) 55 size += nla_total_size(strlen(upstream_sfp_name) + 1); 56 57 /* ETHTOOL_A_PHY_UPSTREAM_INDEX */ 58 size += nla_total_size(sizeof(u32)); 59 } 60 61 /* ETHTOOL_A_PHY_DOWNSTREAM_SFP_NAME */ 62 if (phydev->sfp_bus) { 63 const char *sfp_name = sfp_get_name(phydev->sfp_bus); 64 65 if (sfp_name) 66 size += nla_total_size(strlen(sfp_name) + 1); 67 } 68 69 return size; 70 } 71 72 static int 73 ethnl_phy_fill_reply(const struct ethnl_req_info *req_base, struct sk_buff *skb) 74 { 75 struct phy_req_info *req_info = PHY_REQINFO(req_base); 76 struct phy_device_node *pdn = req_info->pdn; 77 struct phy_device *phydev = pdn->phy; 78 enum phy_upstream ptype; 79 80 ptype = pdn->upstream_type; 81 82 if (nla_put_u32(skb, ETHTOOL_A_PHY_INDEX, phydev->phyindex) || 83 nla_put_string(skb, ETHTOOL_A_PHY_NAME, dev_name(&phydev->mdio.dev)) || 84 nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_TYPE, ptype)) 85 return -EMSGSIZE; 86 87 if (phydev->drv && 88 nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, phydev->drv->name)) 89 return -EMSGSIZE; 90 91 if (ptype == PHY_UPSTREAM_PHY) { 92 struct phy_device *upstream = pdn->upstream.phydev; 93 const char *sfp_upstream_name; 94 95 /* Parent index */ 96 if (nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_INDEX, upstream->phyindex)) 97 return -EMSGSIZE; 98 99 if (pdn->parent_sfp_bus) { 100 sfp_upstream_name = sfp_get_name(pdn->parent_sfp_bus); 101 if (sfp_upstream_name && 102 nla_put_string(skb, ETHTOOL_A_PHY_UPSTREAM_SFP_NAME, 103 sfp_upstream_name)) 104 return -EMSGSIZE; 105 } 106 } 107 108 if (phydev->sfp_bus) { 109 const char *sfp_name = sfp_get_name(phydev->sfp_bus); 110 111 if (sfp_name && 112 nla_put_string(skb, ETHTOOL_A_PHY_DOWNSTREAM_SFP_NAME, 113 sfp_name)) 114 return -EMSGSIZE; 115 } 116 117 return 0; 118 } 119 120 static int ethnl_phy_parse_request(struct ethnl_req_info *req_base, 121 struct nlattr **tb, 122 struct netlink_ext_ack *extack) 123 { 124 struct phy_link_topology *topo = req_base->dev->link_topo; 125 struct phy_req_info *req_info = PHY_REQINFO(req_base); 126 struct phy_device *phydev; 127 128 phydev = ethnl_req_get_phydev(req_base, tb, ETHTOOL_A_PHY_HEADER, 129 extack); 130 if (!phydev) 131 return 0; 132 133 if (IS_ERR(phydev)) 134 return PTR_ERR(phydev); 135 136 if (!topo) 137 return 0; 138 139 req_info->pdn = xa_load(&topo->phys, phydev->phyindex); 140 141 return 0; 142 } 143 144 int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info) 145 { 146 struct phy_req_info req_info = {}; 147 struct nlattr **tb = info->attrs; 148 struct sk_buff *rskb; 149 void *reply_payload; 150 int reply_len; 151 int ret; 152 153 ret = ethnl_parse_header_dev_get(&req_info.base, 154 tb[ETHTOOL_A_PHY_HEADER], 155 genl_info_net(info), info->extack, 156 true); 157 if (ret < 0) 158 return ret; 159 160 rtnl_lock(); 161 netdev_lock_ops(req_info.base.dev); 162 163 ret = ethnl_phy_parse_request(&req_info.base, tb, info->extack); 164 if (ret < 0) 165 goto err_unlock; 166 167 /* No PHY, return early */ 168 if (!req_info.pdn) 169 goto err_unlock; 170 171 ret = ethnl_phy_reply_size(&req_info.base, info->extack); 172 if (ret < 0) 173 goto err_unlock; 174 reply_len = ret + ethnl_reply_header_size(); 175 176 rskb = ethnl_reply_init(reply_len, req_info.base.dev, 177 ETHTOOL_MSG_PHY_GET_REPLY, 178 ETHTOOL_A_PHY_HEADER, 179 info, &reply_payload); 180 if (!rskb) { 181 ret = -ENOMEM; 182 goto err_unlock; 183 } 184 185 ret = ethnl_phy_fill_reply(&req_info.base, rskb); 186 if (ret) 187 goto err_free_msg; 188 189 netdev_unlock_ops(req_info.base.dev); 190 rtnl_unlock(); 191 ethnl_parse_header_dev_put(&req_info.base); 192 genlmsg_end(rskb, reply_payload); 193 194 return genlmsg_reply(rskb, info); 195 196 err_free_msg: 197 nlmsg_free(rskb); 198 err_unlock: 199 netdev_unlock_ops(req_info.base.dev); 200 rtnl_unlock(); 201 ethnl_parse_header_dev_put(&req_info.base); 202 return ret; 203 } 204 205 struct ethnl_phy_dump_ctx { 206 struct phy_req_info *phy_req_info; 207 unsigned long ifindex; 208 unsigned long phy_index; 209 }; 210 211 int ethnl_phy_start(struct netlink_callback *cb) 212 { 213 const struct genl_info *info = genl_info_dump(cb); 214 struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx; 215 int ret; 216 217 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 218 219 ctx->phy_req_info = kzalloc(sizeof(*ctx->phy_req_info), GFP_KERNEL); 220 if (!ctx->phy_req_info) 221 return -ENOMEM; 222 223 ret = ethnl_parse_header_dev_get(&ctx->phy_req_info->base, 224 info->attrs[ETHTOOL_A_PHY_HEADER], 225 sock_net(cb->skb->sk), cb->extack, 226 false); 227 ctx->ifindex = 0; 228 ctx->phy_index = 0; 229 230 if (ret) 231 kfree(ctx->phy_req_info); 232 233 return ret; 234 } 235 236 int ethnl_phy_done(struct netlink_callback *cb) 237 { 238 struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx; 239 240 if (ctx->phy_req_info->base.dev) 241 ethnl_parse_header_dev_put(&ctx->phy_req_info->base); 242 243 kfree(ctx->phy_req_info); 244 245 return 0; 246 } 247 248 static int ethnl_phy_dump_one_dev(struct sk_buff *skb, struct net_device *dev, 249 struct netlink_callback *cb) 250 { 251 struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx; 252 struct phy_req_info *pri = ctx->phy_req_info; 253 struct phy_device_node *pdn; 254 int ret = 0; 255 void *ehdr; 256 257 if (!dev->link_topo) 258 return 0; 259 260 xa_for_each_start(&dev->link_topo->phys, ctx->phy_index, pdn, ctx->phy_index) { 261 ehdr = ethnl_dump_put(skb, cb, ETHTOOL_MSG_PHY_GET_REPLY); 262 if (!ehdr) { 263 ret = -EMSGSIZE; 264 break; 265 } 266 267 ret = ethnl_fill_reply_header(skb, dev, ETHTOOL_A_PHY_HEADER); 268 if (ret < 0) { 269 genlmsg_cancel(skb, ehdr); 270 break; 271 } 272 273 pri->pdn = pdn; 274 ret = ethnl_phy_fill_reply(&pri->base, skb); 275 if (ret < 0) { 276 genlmsg_cancel(skb, ehdr); 277 break; 278 } 279 280 genlmsg_end(skb, ehdr); 281 } 282 283 return ret; 284 } 285 286 int ethnl_phy_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 287 { 288 struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx; 289 struct net *net = sock_net(skb->sk); 290 struct net_device *dev; 291 int ret = 0; 292 293 rtnl_lock(); 294 295 if (ctx->phy_req_info->base.dev) { 296 dev = ctx->phy_req_info->base.dev; 297 netdev_lock_ops(dev); 298 ret = ethnl_phy_dump_one_dev(skb, dev, cb); 299 netdev_unlock_ops(dev); 300 } else { 301 for_each_netdev_dump(net, dev, ctx->ifindex) { 302 netdev_lock_ops(dev); 303 ret = ethnl_phy_dump_one_dev(skb, dev, cb); 304 netdev_unlock_ops(dev); 305 if (ret) 306 break; 307 308 ctx->phy_index = 0; 309 } 310 } 311 rtnl_unlock(); 312 313 return ret; 314 } 315