netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Moshe Shemesh <moshe@nvidia.com>
To: "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Andrew Lunn <andrew@lunn.ch>,
	Adrian Pop <pop.adrian61@gmail.com>,
	"Michal Kubecek" <mkubecek@suse.cz>,
	Don Bollinger <don@thebollingers.org>, <netdev@vger.kernel.org>
Cc: Vladyslav Tarasiuk <vladyslavt@nvidia.com>,
	Moshe Shemesh <moshe@nvidia.com>
Subject: [RFC PATCH V3 net-next 5/5] ethtool: Add fallback to get_module_eeprom from netlink command
Date: Mon, 15 Mar 2021 19:12:43 +0200	[thread overview]
Message-ID: <1615828363-464-6-git-send-email-moshe@nvidia.com> (raw)
In-Reply-To: <1615828363-464-1-git-send-email-moshe@nvidia.com>

From: Vladyslav Tarasiuk <vladyslavt@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 <vladyslavt@nvidia.com>
---
 net/ethtool/eeprom.c | 75 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/net/ethtool/eeprom.c b/net/ethtool/eeprom.c
index e110336dc231..33ba9ecc36cb 100644
--- a/net/ethtool/eeprom.c
+++ b/net/ethtool/eeprom.c
@@ -25,6 +25,79 @@ 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) {
+		if (offset < 128 || offset + length > ETH_MODULE_EEPROM_PAGE_LEN)
+			return -EINVAL;
+		offset = request->page * 128 + offset;
+	}
+
+	if (modinfo->type == ETH_MODULE_SFF_8079 &&
+	    request->i2c_address == 0x51)
+		offset += ETH_MODULE_EEPROM_PAGE_LEN;
+
+	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;
+
+	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) {
+		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)
@@ -36,7 +109,7 @@ static int eeprom_data_prepare_data(const struct ethnl_req_info *req_base,
 	int ret;
 
 	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.26.2


  parent reply	other threads:[~2021-03-15 17:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 17:12 [RFC PATCH V3 net-next 0/5] ethtool: Extend module EEPROM dump API Moshe Shemesh
2021-03-15 17:12 ` [RFC PATCH V3 net-next 1/5] ethtool: Allow network drivers to dump arbitrary EEPROM data Moshe Shemesh
2021-03-15 21:01   ` Jakub Kicinski
2021-03-16 17:59     ` Moshe Shemesh
2021-03-15 22:31   ` Don Bollinger
2021-03-16 18:23     ` Moshe Shemesh
2021-03-16 21:00       ` Don Bollinger
2021-03-18 22:36       ` Andrew Lunn
2021-03-18 13:03   ` Andrew Lunn
2021-03-18 14:25     ` Moshe Shemesh
2021-03-18 22:16       ` Andrew Lunn
2021-03-18 15:16     ` Michal Kubecek
2021-03-15 17:12 ` [RFC PATCH V3 net-next 2/5] net/mlx5: Refactor module EEPROM query Moshe Shemesh
2021-03-15 17:12 ` [RFC PATCH V3 net-next 3/5] net/mlx5: Implement get_module_eeprom_data_by_page() Moshe Shemesh
2021-03-15 17:12 ` [RFC PATCH V3 net-next 4/5] net/mlx5: Add support for DSFP module EEPROM dumps Moshe Shemesh
2021-03-15 17:12 ` Moshe Shemesh [this message]
2021-03-15 22:31   ` [RFC PATCH V3 net-next 5/5] ethtool: Add fallback to get_module_eeprom from netlink command Don Bollinger
2021-03-16 18:26     ` Moshe Shemesh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1615828363-464-6-git-send-email-moshe@nvidia.com \
    --to=moshe@nvidia.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=don@thebollingers.org \
    --cc=kuba@kernel.org \
    --cc=mkubecek@suse.cz \
    --cc=netdev@vger.kernel.org \
    --cc=pop.adrian61@gmail.com \
    --cc=vladyslavt@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).