From: Vladyslav Tarasiuk <vladysl...@nvidia.com> In case netlink get_module_eeprom_data_by_page() callback is not implemented by the driver, try to call old get_module_info() and get_module_eeprom() pair. Recalculate parameters to get_module_eeprom() offset and len using page number and their sizes. Return error if this can't be done.
Signed-off-by: Vladyslav Tarasiuk <vladysl...@nvidia.com> --- net/ethtool/eeprom.c | 84 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/net/ethtool/eeprom.c b/net/ethtool/eeprom.c index 2618a55b9a40..72c7714a9d37 100644 --- a/net/ethtool/eeprom.c +++ b/net/ethtool/eeprom.c @@ -26,6 +26,88 @@ struct eeprom_data_reply_data { #define EEPROM_DATA_REPDATA(__reply_base) \ container_of(__reply_base, struct eeprom_data_reply_data, base) +static int fallback_set_params(struct eeprom_data_req_info *request, + struct ethtool_modinfo *modinfo, + struct ethtool_eeprom *eeprom) +{ + u32 offset = request->offset; + u32 length = request->length; + + if (request->page) + offset = 128 + request->page * 128 + offset; + + if (!length) + length = modinfo->eeprom_len; + + if (offset >= modinfo->eeprom_len) + return -EINVAL; + + if (modinfo->eeprom_len < offset + length) + length = modinfo->eeprom_len - offset; + + eeprom->cmd = ETHTOOL_GMODULEEEPROM; + eeprom->len = length; + eeprom->offset = offset; + + switch (modinfo->type) { + case ETH_MODULE_SFF_8079: + if (request->page > 1) + return -EINVAL; + break; + case ETH_MODULE_SFF_8472: + if (request->page > 3) + return -EINVAL; + break; + case ETH_MODULE_SFF_8436: + case ETH_MODULE_SFF_8636: + if (request->page > 3) + return -EINVAL; + break; + } + return 0; +} + +static int eeprom_data_fallback(struct eeprom_data_req_info *request, + struct eeprom_data_reply_data *reply, + struct genl_info *info) +{ + struct net_device *dev = reply->base.dev; + struct ethtool_modinfo modinfo = {0}; + struct ethtool_eeprom eeprom = {0}; + u8 *data; + int err; + + if ((!dev->ethtool_ops->get_module_info && + !dev->ethtool_ops->get_module_eeprom) || + request->bank || request->i2c_address) { + return -EOPNOTSUPP; + } + modinfo.cmd = ETHTOOL_GMODULEINFO; + err = dev->ethtool_ops->get_module_info(dev, &modinfo); + if (err < 0) + return err; + + err = fallback_set_params(request, &modinfo, &eeprom); + if (err < 0) + return err; + + data = kmalloc(eeprom.len, GFP_KERNEL); + if (!data) + return -ENOMEM; + err = dev->ethtool_ops->get_module_eeprom(dev, &eeprom, data); + if (err < 0) + goto err_out; + + reply->data = data; + reply->length = eeprom.len; + + return 0; + +err_out: + kfree(data); + return err; +} + static int eeprom_data_prepare_data(const struct ethnl_req_info *req_base, struct ethnl_reply_data *reply_base, struct genl_info *info) @@ -37,7 +119,7 @@ static int eeprom_data_prepare_data(const struct ethnl_req_info *req_base, int err; if (!dev->ethtool_ops->get_module_eeprom_data_by_page) - return -EOPNOTSUPP; + return eeprom_data_fallback(request, reply, info); page_data.offset = request->offset; page_data.length = request->length; -- 2.18.2