1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/ethtool.h> 4 #include <linux/sfp.h> 5 6 #include "common.h" 7 #include "netlink.h" 8 9 struct eeprom_req_info { 10 struct ethnl_req_info base; 11 u32 offset; 12 u32 length; 13 u8 page; 14 u8 bank; 15 u8 i2c_address; 16 }; 17 18 struct eeprom_reply_data { 19 struct ethnl_reply_data base; 20 u32 length; 21 u8 *data; 22 }; 23 24 #define MODULE_EEPROM_REQINFO(__req_base) \ 25 container_of(__req_base, struct eeprom_req_info, base) 26 27 #define MODULE_EEPROM_REPDATA(__reply_base) \ 28 container_of(__reply_base, struct eeprom_reply_data, base) 29 30 static int fallback_set_params(struct eeprom_req_info *request, 31 struct ethtool_modinfo *modinfo, 32 struct ethtool_eeprom *eeprom) 33 { 34 u32 offset = request->offset; 35 u32 length = request->length; 36 37 if (request->page) 38 offset = request->page * ETH_MODULE_EEPROM_PAGE_LEN + offset; 39 40 if (modinfo->type == ETH_MODULE_SFF_8472 && 41 request->i2c_address == 0x51) 42 offset += ETH_MODULE_EEPROM_PAGE_LEN * 2; 43 44 if (offset >= modinfo->eeprom_len) 45 return -EINVAL; 46 47 eeprom->cmd = ETHTOOL_GMODULEEEPROM; 48 eeprom->len = length; 49 eeprom->offset = offset; 50 51 return 0; 52 } 53 54 static int eeprom_fallback(struct eeprom_req_info *request, 55 struct eeprom_reply_data *reply) 56 { 57 struct net_device *dev = reply->base.dev; 58 struct ethtool_modinfo modinfo = {0}; 59 struct ethtool_eeprom eeprom = {0}; 60 u8 *data; 61 int err; 62 63 modinfo.cmd = ETHTOOL_GMODULEINFO; 64 err = ethtool_get_module_info_call(dev, &modinfo); 65 if (err < 0) 66 return err; 67 68 err = fallback_set_params(request, &modinfo, &eeprom); 69 if (err < 0) 70 return err; 71 72 data = kmalloc(eeprom.len, GFP_KERNEL); 73 if (!data) 74 return -ENOMEM; 75 err = ethtool_get_module_eeprom_call(dev, &eeprom, data); 76 if (err < 0) 77 goto err_out; 78 79 reply->data = data; 80 reply->length = eeprom.len; 81 82 return 0; 83 84 err_out: 85 kfree(data); 86 return err; 87 } 88 89 static int get_module_eeprom_by_page(struct net_device *dev, 90 struct ethtool_module_eeprom *page_data, 91 struct netlink_ext_ack *extack) 92 { 93 const struct ethtool_ops *ops = dev->ethtool_ops; 94 95 if (dev->ethtool->module_fw_flash_in_progress) { 96 NL_SET_ERR_MSG(extack, 97 "Module firmware flashing is in progress"); 98 return -EBUSY; 99 } 100 101 if (dev->sfp_bus) 102 return sfp_get_module_eeprom_by_page(dev->sfp_bus, page_data, extack); 103 104 if (ops->get_module_eeprom_by_page) 105 return ops->get_module_eeprom_by_page(dev, page_data, extack); 106 107 return -EOPNOTSUPP; 108 } 109 110 static int eeprom_prepare_data(const struct ethnl_req_info *req_base, 111 struct ethnl_reply_data *reply_base, 112 const struct genl_info *info) 113 { 114 struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); 115 struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base); 116 struct ethtool_module_eeprom page_data = {0}; 117 struct net_device *dev = reply_base->dev; 118 int ret; 119 120 page_data.offset = request->offset; 121 page_data.length = request->length; 122 page_data.i2c_address = request->i2c_address; 123 page_data.page = request->page; 124 page_data.bank = request->bank; 125 page_data.data = kmalloc(page_data.length, GFP_KERNEL); 126 if (!page_data.data) 127 return -ENOMEM; 128 129 ret = ethnl_ops_begin(dev); 130 if (ret) 131 goto err_free; 132 133 ret = get_module_eeprom_by_page(dev, &page_data, info->extack); 134 if (ret < 0) 135 goto err_ops; 136 137 reply->length = ret; 138 reply->data = page_data.data; 139 140 ethnl_ops_complete(dev); 141 return 0; 142 143 err_ops: 144 ethnl_ops_complete(dev); 145 err_free: 146 kfree(page_data.data); 147 148 if (ret == -EOPNOTSUPP) 149 return eeprom_fallback(request, reply); 150 return ret; 151 } 152 153 static int eeprom_parse_request(struct ethnl_req_info *req_info, 154 const struct genl_info *info, 155 struct nlattr **tb, 156 struct netlink_ext_ack *extack) 157 { 158 struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_info); 159 160 if (GENL_REQ_ATTR_CHECK(info, ETHTOOL_A_MODULE_EEPROM_OFFSET) || 161 GENL_REQ_ATTR_CHECK(info, ETHTOOL_A_MODULE_EEPROM_LENGTH) || 162 GENL_REQ_ATTR_CHECK(info, ETHTOOL_A_MODULE_EEPROM_PAGE) || 163 GENL_REQ_ATTR_CHECK(info, ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS)) 164 return -EINVAL; 165 166 request->i2c_address = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]); 167 request->offset = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_OFFSET]); 168 request->length = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_LENGTH]); 169 170 /* The following set of conditions limit the API to only dump 1/2 171 * EEPROM page without crossing low page boundary located at offset 128. 172 * This means user may only request dumps of length limited to 128 from 173 * either low 128 bytes or high 128 bytes. 174 * For pages higher than 0 only high 128 bytes are accessible. 175 */ 176 request->page = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_PAGE]); 177 if (request->page && request->offset < ETH_MODULE_EEPROM_PAGE_LEN) { 178 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_PAGE], 179 "reading from lower half page is allowed for page 0 only"); 180 return -EINVAL; 181 } 182 183 if (request->offset < ETH_MODULE_EEPROM_PAGE_LEN && 184 request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN) { 185 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], 186 "reading cross half page boundary is illegal"); 187 return -EINVAL; 188 } else if (request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN * 2) { 189 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], 190 "reading cross page boundary is illegal"); 191 return -EINVAL; 192 } 193 194 if (tb[ETHTOOL_A_MODULE_EEPROM_BANK]) 195 request->bank = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_BANK]); 196 197 return 0; 198 } 199 200 static int eeprom_reply_size(const struct ethnl_req_info *req_base, 201 const struct ethnl_reply_data *reply_base) 202 { 203 const struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base); 204 205 return nla_total_size(sizeof(u8) * request->length); /* _EEPROM_DATA */ 206 } 207 208 static int eeprom_fill_reply(struct sk_buff *skb, 209 const struct ethnl_req_info *req_base, 210 const struct ethnl_reply_data *reply_base) 211 { 212 struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); 213 214 return nla_put(skb, ETHTOOL_A_MODULE_EEPROM_DATA, reply->length, reply->data); 215 } 216 217 static void eeprom_cleanup_data(struct ethnl_reply_data *reply_base) 218 { 219 struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); 220 221 kfree(reply->data); 222 } 223 224 const struct ethnl_request_ops ethnl_module_eeprom_request_ops = { 225 .request_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET, 226 .reply_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY, 227 .hdr_attr = ETHTOOL_A_MODULE_EEPROM_HEADER, 228 .req_info_size = sizeof(struct eeprom_req_info), 229 .reply_data_size = sizeof(struct eeprom_reply_data), 230 231 .parse_request = eeprom_parse_request, 232 .prepare_data = eeprom_prepare_data, 233 .reply_size = eeprom_reply_size, 234 .fill_reply = eeprom_fill_reply, 235 .cleanup_data = eeprom_cleanup_data, 236 }; 237 238 const struct nla_policy ethnl_module_eeprom_get_policy[] = { 239 [ETHTOOL_A_MODULE_EEPROM_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), 240 [ETHTOOL_A_MODULE_EEPROM_OFFSET] = 241 NLA_POLICY_MAX(NLA_U32, ETH_MODULE_EEPROM_PAGE_LEN * 2 - 1), 242 [ETHTOOL_A_MODULE_EEPROM_LENGTH] = 243 NLA_POLICY_RANGE(NLA_U32, 1, ETH_MODULE_EEPROM_PAGE_LEN), 244 [ETHTOOL_A_MODULE_EEPROM_PAGE] = { .type = NLA_U8 }, 245 [ETHTOOL_A_MODULE_EEPROM_BANK] = { .type = NLA_U8 }, 246 [ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS] = 247 NLA_POLICY_RANGE(NLA_U8, 0, ETH_MODULE_MAX_I2C_ADDRESS), 248 }; 249 250