1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2023 Bootlin 4 * 5 */ 6 #include <linux/phy.h> 7 #include <linux/phy_link_topology.h> 8 #include <linux/sfp.h> 9 #include <net/netdev_lock.h> 10 11 #include "common.h" 12 #include "netlink.h" 13 14 struct phy_req_info { 15 struct ethnl_req_info base; 16 }; 17 18 struct phy_reply_data { 19 struct ethnl_reply_data base; 20 u32 phyindex; 21 char *drvname; 22 char *name; 23 unsigned int upstream_type; 24 char *upstream_sfp_name; 25 unsigned int upstream_index; 26 char *downstream_sfp_name; 27 }; 28 29 #define PHY_REPDATA(__reply_base) \ 30 container_of(__reply_base, struct phy_reply_data, base) 31 32 const struct nla_policy ethnl_phy_get_policy[ETHTOOL_A_PHY_HEADER + 1] = { 33 [ETHTOOL_A_PHY_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), 34 }; 35 36 static int phy_reply_size(const struct ethnl_req_info *req_info, 37 const struct ethnl_reply_data *reply_data) 38 { 39 struct phy_reply_data *rep_data = PHY_REPDATA(reply_data); 40 size_t size = 0; 41 42 /* ETHTOOL_A_PHY_INDEX */ 43 size += nla_total_size(sizeof(u32)); 44 45 /* ETHTOOL_A_DRVNAME */ 46 if (rep_data->drvname) 47 size += nla_total_size(strlen(rep_data->drvname) + 1); 48 49 /* ETHTOOL_A_NAME */ 50 size += nla_total_size(strlen(rep_data->name) + 1); 51 52 /* ETHTOOL_A_PHY_UPSTREAM_TYPE */ 53 size += nla_total_size(sizeof(u32)); 54 55 /* ETHTOOL_A_PHY_UPSTREAM_SFP_NAME */ 56 if (rep_data->upstream_sfp_name) 57 size += nla_total_size(strlen(rep_data->upstream_sfp_name) + 1); 58 59 /* ETHTOOL_A_PHY_UPSTREAM_INDEX */ 60 if (rep_data->upstream_index) 61 size += nla_total_size(sizeof(u32)); 62 63 /* ETHTOOL_A_PHY_DOWNSTREAM_SFP_NAME */ 64 if (rep_data->downstream_sfp_name) 65 size += nla_total_size(strlen(rep_data->downstream_sfp_name) + 1); 66 67 return size; 68 } 69 70 static int phy_prepare_data(const struct ethnl_req_info *req_info, 71 struct ethnl_reply_data *reply_data, 72 const struct genl_info *info) 73 { 74 struct phy_link_topology *topo = reply_data->dev->link_topo; 75 struct phy_reply_data *rep_data = PHY_REPDATA(reply_data); 76 struct nlattr **tb = info->attrs; 77 struct phy_device_node *pdn; 78 struct phy_device *phydev; 79 int ret; 80 81 /* RTNL is held by the caller */ 82 phydev = ethnl_req_get_phydev(req_info, tb, ETHTOOL_A_PHY_HEADER, 83 info->extack); 84 if (IS_ERR_OR_NULL(phydev)) 85 return -EOPNOTSUPP; 86 87 pdn = xa_load(&topo->phys, phydev->phyindex); 88 if (!pdn) 89 return -EOPNOTSUPP; 90 91 rep_data->phyindex = phydev->phyindex; 92 93 rep_data->name = kstrdup(dev_name(&phydev->mdio.dev), GFP_KERNEL); 94 if (!rep_data->name) 95 return -ENOMEM; 96 97 rep_data->drvname = kstrdup(phydev->drv->name, GFP_KERNEL); 98 if (!rep_data->drvname) { 99 ret = -ENOMEM; 100 goto err_free_name; 101 } 102 103 rep_data->upstream_type = pdn->upstream_type; 104 105 if (pdn->upstream_type == PHY_UPSTREAM_PHY) { 106 struct phy_device *upstream = pdn->upstream.phydev; 107 rep_data->upstream_index = upstream->phyindex; 108 } 109 110 if (pdn->parent_sfp_bus) { 111 rep_data->upstream_sfp_name = kstrdup(sfp_get_name(pdn->parent_sfp_bus), 112 GFP_KERNEL); 113 if (!rep_data->upstream_sfp_name) { 114 ret = -ENOMEM; 115 goto err_free_drvname; 116 } 117 } 118 119 if (phydev->sfp_bus) { 120 rep_data->downstream_sfp_name = kstrdup(sfp_get_name(phydev->sfp_bus), 121 GFP_KERNEL); 122 if (!rep_data->downstream_sfp_name) { 123 ret = -ENOMEM; 124 goto err_free_upstream_sfp; 125 } 126 } 127 128 return 0; 129 130 err_free_upstream_sfp: 131 kfree(rep_data->upstream_sfp_name); 132 err_free_drvname: 133 kfree(rep_data->drvname); 134 err_free_name: 135 kfree(rep_data->name); 136 return ret; 137 } 138 139 static int phy_fill_reply(struct sk_buff *skb, 140 const struct ethnl_req_info *req_info, 141 const struct ethnl_reply_data *reply_data) 142 { 143 struct phy_reply_data *rep_data = PHY_REPDATA(reply_data); 144 145 if (nla_put_u32(skb, ETHTOOL_A_PHY_INDEX, rep_data->phyindex) || 146 nla_put_string(skb, ETHTOOL_A_PHY_NAME, rep_data->name) || 147 nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_TYPE, rep_data->upstream_type)) 148 return -EMSGSIZE; 149 150 if (rep_data->drvname && 151 nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, rep_data->drvname)) 152 return -EMSGSIZE; 153 154 if (rep_data->upstream_index && 155 nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_INDEX, 156 rep_data->upstream_index)) 157 return -EMSGSIZE; 158 159 if (rep_data->upstream_sfp_name && 160 nla_put_string(skb, ETHTOOL_A_PHY_UPSTREAM_SFP_NAME, 161 rep_data->upstream_sfp_name)) 162 return -EMSGSIZE; 163 164 if (rep_data->downstream_sfp_name && 165 nla_put_string(skb, ETHTOOL_A_PHY_DOWNSTREAM_SFP_NAME, 166 rep_data->downstream_sfp_name)) 167 return -EMSGSIZE; 168 169 return 0; 170 } 171 172 static void phy_cleanup_data(struct ethnl_reply_data *reply_data) 173 { 174 struct phy_reply_data *rep_data = PHY_REPDATA(reply_data); 175 176 kfree(rep_data->drvname); 177 kfree(rep_data->name); 178 kfree(rep_data->upstream_sfp_name); 179 kfree(rep_data->downstream_sfp_name); 180 } 181 182 const struct ethnl_request_ops ethnl_phy_request_ops = { 183 .request_cmd = ETHTOOL_MSG_PHY_GET, 184 .reply_cmd = ETHTOOL_MSG_PHY_GET_REPLY, 185 .hdr_attr = ETHTOOL_A_PHY_HEADER, 186 .req_info_size = sizeof(struct phy_req_info), 187 .reply_data_size = sizeof(struct phy_reply_data), 188 189 .prepare_data = phy_prepare_data, 190 .reply_size = phy_reply_size, 191 .fill_reply = phy_fill_reply, 192 .cleanup_data = phy_cleanup_data, 193 }; 194