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 phydev = ethnl_req_get_phydev(req_info, tb, ETHTOOL_A_PHY_HEADER, 82 info->extack); 83 if (IS_ERR_OR_NULL(phydev)) 84 return -EOPNOTSUPP; 85 86 pdn = xa_load(&topo->phys, phydev->phyindex); 87 if (!pdn) 88 return -EOPNOTSUPP; 89 90 rep_data->phyindex = phydev->phyindex; 91 92 rep_data->name = kstrdup(dev_name(&phydev->mdio.dev), GFP_KERNEL); 93 if (!rep_data->name) 94 return -ENOMEM; 95 96 if (phydev->drv) { 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 104 rep_data->upstream_type = pdn->upstream_type; 105 106 if (pdn->upstream_type == PHY_UPSTREAM_PHY) { 107 struct phy_device *upstream = pdn->upstream.phydev; 108 rep_data->upstream_index = upstream->phyindex; 109 } 110 111 if (pdn->parent_sfp_bus) { 112 rep_data->upstream_sfp_name = kstrdup(sfp_get_name(pdn->parent_sfp_bus), 113 GFP_KERNEL); 114 if (!rep_data->upstream_sfp_name) { 115 ret = -ENOMEM; 116 goto err_free_drvname; 117 } 118 } 119 120 if (phydev->sfp_bus) { 121 rep_data->downstream_sfp_name = kstrdup(sfp_get_name(phydev->sfp_bus), 122 GFP_KERNEL); 123 if (!rep_data->downstream_sfp_name) { 124 ret = -ENOMEM; 125 goto err_free_upstream_sfp; 126 } 127 } 128 129 return 0; 130 131 err_free_upstream_sfp: 132 kfree(rep_data->upstream_sfp_name); 133 err_free_drvname: 134 kfree(rep_data->drvname); 135 err_free_name: 136 kfree(rep_data->name); 137 return ret; 138 } 139 140 static int phy_fill_reply(struct sk_buff *skb, 141 const struct ethnl_req_info *req_info, 142 const struct ethnl_reply_data *reply_data) 143 { 144 struct phy_reply_data *rep_data = PHY_REPDATA(reply_data); 145 146 if (nla_put_u32(skb, ETHTOOL_A_PHY_INDEX, rep_data->phyindex) || 147 nla_put_string(skb, ETHTOOL_A_PHY_NAME, rep_data->name) || 148 nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_TYPE, rep_data->upstream_type)) 149 return -EMSGSIZE; 150 151 if (rep_data->drvname && 152 nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, rep_data->drvname)) 153 return -EMSGSIZE; 154 155 if (rep_data->upstream_index && 156 nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_INDEX, 157 rep_data->upstream_index)) 158 return -EMSGSIZE; 159 160 if (rep_data->upstream_sfp_name && 161 nla_put_string(skb, ETHTOOL_A_PHY_UPSTREAM_SFP_NAME, 162 rep_data->upstream_sfp_name)) 163 return -EMSGSIZE; 164 165 if (rep_data->downstream_sfp_name && 166 nla_put_string(skb, ETHTOOL_A_PHY_DOWNSTREAM_SFP_NAME, 167 rep_data->downstream_sfp_name)) 168 return -EMSGSIZE; 169 170 return 0; 171 } 172 173 static void phy_cleanup_data(struct ethnl_reply_data *reply_data) 174 { 175 struct phy_reply_data *rep_data = PHY_REPDATA(reply_data); 176 177 kfree(rep_data->drvname); 178 kfree(rep_data->name); 179 kfree(rep_data->upstream_sfp_name); 180 kfree(rep_data->downstream_sfp_name); 181 } 182 183 const struct ethnl_request_ops ethnl_phy_request_ops = { 184 .request_cmd = ETHTOOL_MSG_PHY_GET, 185 .reply_cmd = ETHTOOL_MSG_PHY_GET_REPLY, 186 .hdr_attr = ETHTOOL_A_PHY_HEADER, 187 .req_info_size = sizeof(struct phy_req_info), 188 .reply_data_size = sizeof(struct phy_reply_data), 189 190 .prepare_data = phy_prepare_data, 191 .reply_size = phy_reply_size, 192 .fill_reply = phy_fill_reply, 193 .cleanup_data = phy_cleanup_data, 194 }; 195