Add support for the following EEPROM function callbacks:

.get_eeprom_len
Get the maximum size of the device EEPROM data.

.get_eeprom
Get the device EEPROM data at a certain offset and length.

.set_eeprom
Set the device EEPROM data at a certain offset and length.

Note that for an nfp NIC, the "device EEPROM" is simply a field in the
hwinfo that is used to store a 6B mac address associated with a physical
port.

.get_module_info
Get information regarding the type and size of the plugin module EEPROM
for a specific port.

.get_module_eeprom
Get the data stored in the plugin module EEPROM for a specific port.

Signed-off-by: James Hershaw <james.hers...@corigine.com>
Signed-off-by: Chaoyong He <chaoyong...@corigine.com>
---
 .../net/nfp/flower/nfp_flower_representor.c   |  76 ++++++
 drivers/net/nfp/nfp_ethdev.c                  |   5 +
 drivers/net/nfp/nfp_net_common.c              | 256 ++++++++++++++++++
 drivers/net/nfp/nfp_net_common.h              |   6 +
 drivers/net/nfp/nfpcore/nfp_nsp.c             |  96 +++++++
 drivers/net/nfp/nfpcore/nfp_nsp.h             |   9 +
 6 files changed, 448 insertions(+)

diff --git a/drivers/net/nfp/flower/nfp_flower_representor.c 
b/drivers/net/nfp/flower/nfp_flower_representor.c
index 8efef482b5..3d043e052a 100644
--- a/drivers/net/nfp/flower/nfp_flower_representor.c
+++ b/drivers/net/nfp/flower/nfp_flower_representor.c
@@ -24,6 +24,70 @@ struct nfp_repr_init {
        struct nfp_net_hw_priv *hw_priv;
 };
 
+static int
+nfp_repr_get_eeprom_len(struct rte_eth_dev *dev)
+{
+       struct nfp_flower_representor *repr;
+
+       repr = dev->data->dev_private;
+       if (!nfp_flower_repr_is_phy(repr))
+               return -EOPNOTSUPP;
+
+       return nfp_net_get_eeprom_len(dev);
+}
+
+static int
+nfp_repr_get_eeprom(struct rte_eth_dev *dev,
+               struct rte_dev_eeprom_info *eeprom)
+{
+       struct nfp_flower_representor *repr;
+
+       repr = dev->data->dev_private;
+       if (!nfp_flower_repr_is_phy(repr))
+               return -EOPNOTSUPP;
+
+       return nfp_net_get_eeprom(dev, eeprom);
+}
+
+static int
+nfp_repr_set_eeprom(struct rte_eth_dev *dev,
+               struct rte_dev_eeprom_info *eeprom)
+{
+       struct nfp_flower_representor *repr;
+
+       repr = dev->data->dev_private;
+       if (!nfp_flower_repr_is_phy(repr))
+               return -EOPNOTSUPP;
+
+       return nfp_net_set_eeprom(dev, eeprom);
+}
+
+static int
+nfp_repr_get_module_info(struct rte_eth_dev *dev,
+               struct rte_eth_dev_module_info *info)
+{
+       struct nfp_flower_representor *repr;
+
+       repr = dev->data->dev_private;
+       if (!nfp_flower_repr_is_phy(repr))
+               return -EOPNOTSUPP;
+
+       return nfp_net_get_module_info(dev, info);
+}
+
+static int
+nfp_repr_get_module_eeprom(struct rte_eth_dev *dev,
+               struct rte_dev_eeprom_info *info)
+{
+       struct nfp_flower_representor *repr;
+
+       repr = dev->data->dev_private;
+       if (!nfp_flower_repr_is_phy(repr))
+               return -EOPNOTSUPP;
+
+       return nfp_net_get_module_eeprom(dev, info);
+}
+
 static int
 nfp_flower_repr_link_update(struct rte_eth_dev *dev,
                __rte_unused int wait_to_complete)
@@ -553,6 +617,12 @@ static const struct eth_dev_ops 
nfp_flower_multiple_pf_repr_dev_ops = {
        .xstats_get_names       = nfp_net_xstats_get_names,
        .xstats_get_by_id       = nfp_net_xstats_get_by_id,
        .xstats_get_names_by_id = nfp_net_xstats_get_names_by_id,
+
+       .get_eeprom_length    = nfp_repr_get_eeprom_len,
+       .get_eeprom           = nfp_repr_get_eeprom,
+       .set_eeprom           = nfp_repr_set_eeprom,
+       .get_module_info      = nfp_repr_get_module_info,
+       .get_module_eeprom    = nfp_repr_get_module_eeprom,
 };
 
 static const struct eth_dev_ops nfp_flower_repr_dev_ops = {
@@ -585,6 +655,12 @@ static const struct eth_dev_ops nfp_flower_repr_dev_ops = {
        .xstats_get_names       = nfp_net_xstats_get_names,
        .xstats_get_by_id       = nfp_net_xstats_get_by_id,
        .xstats_get_names_by_id = nfp_net_xstats_get_names_by_id,
+
+       .get_eeprom_length    = nfp_repr_get_eeprom_len,
+       .get_eeprom           = nfp_repr_get_eeprom,
+       .set_eeprom           = nfp_repr_set_eeprom,
+       .get_module_info      = nfp_repr_get_module_info,
+       .get_module_eeprom    = nfp_repr_get_module_eeprom,
 };
 
 static uint32_t
diff --git a/drivers/net/nfp/nfp_ethdev.c b/drivers/net/nfp/nfp_ethdev.c
index 77b95d2c5e..2ee76d309c 100644
--- a/drivers/net/nfp/nfp_ethdev.c
+++ b/drivers/net/nfp/nfp_ethdev.c
@@ -978,6 +978,11 @@ static const struct eth_dev_ops nfp_net_eth_dev_ops = {
        .fec_get_capability     = nfp_net_fec_get_capability,
        .fec_get                = nfp_net_fec_get,
        .fec_set                = nfp_net_fec_set,
+       .get_eeprom_length      = nfp_net_get_eeprom_len,
+       .get_eeprom             = nfp_net_get_eeprom,
+       .set_eeprom             = nfp_net_set_eeprom,
+       .get_module_info        = nfp_net_get_module_info,
+       .get_module_eeprom      = nfp_net_get_module_eeprom,
 };
 
 static inline void
diff --git a/drivers/net/nfp/nfp_net_common.c b/drivers/net/nfp/nfp_net_common.c
index 4ad6c532ee..a45837353a 100644
--- a/drivers/net/nfp/nfp_net_common.c
+++ b/drivers/net/nfp/nfp_net_common.c
@@ -2925,3 +2925,259 @@ nfp_net_recv_pkt_meta_check_register(struct 
nfp_net_hw_priv *hw_priv)
 
        return true;
 }
+
+static int
+nfp_net_get_nfp_index(struct rte_eth_dev *dev)
+{
+       int nfp_idx;
+
+       if (rte_eth_dev_is_repr(dev)) {
+               struct nfp_flower_representor *repr;
+               repr = dev->data->dev_private;
+               nfp_idx = repr->nfp_idx;
+       } else {
+               struct nfp_net_hw *net_hw;
+               net_hw = dev->data->dev_private;
+               nfp_idx = net_hw->nfp_idx;
+       }
+
+       return nfp_idx;
+}
+
+int
+nfp_net_get_eeprom_len(__rte_unused struct rte_eth_dev *dev)
+{
+       return RTE_ETHER_ADDR_LEN;
+}
+
+static int
+nfp_net_get_port_mac_hwinfo(struct nfp_net_hw_priv *hw_priv,
+               uint32_t index,
+               struct rte_ether_addr *mac_addr)
+{
+       int ret;
+       char hwinfo[32];
+       struct nfp_nsp *nsp;
+
+       snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac", index);
+
+       nsp = nfp_nsp_open(hw_priv->pf_dev->cpp);
+       if (nsp == NULL)
+               return -EOPNOTSUPP;
+
+       ret = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
+       nfp_nsp_close(nsp);
+
+       if (ret != 0) {
+               PMD_DRV_LOG(ERR, "Read persistent MAC address failed for 
eth_index %u.", index);
+               return ret;
+       }
+
+       ret = rte_ether_unformat_addr(hwinfo, mac_addr);
+       if (ret != 0) {
+               PMD_DRV_LOG(ERR, "Can not parse persistent MAC address.");
+               return -EOPNOTSUPP;
+       }
+
+       return 0;
+}
+
+static int
+nfp_net_set_port_mac_hwinfo(struct nfp_net_hw_priv *hw_priv,
+               uint32_t index,
+               struct rte_ether_addr *mac_addr)
+{
+       int ret;
+       char hwinfo_mac[32];
+       struct nfp_nsp *nsp;
+       char buf[RTE_ETHER_ADDR_FMT_SIZE];
+
+       rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
+       snprintf(hwinfo_mac, sizeof(hwinfo_mac), "eth%u.mac=%s", index, buf);
+
+       nsp = nfp_nsp_open(hw_priv->pf_dev->cpp);
+       if (nsp == NULL)
+               return -EOPNOTSUPP;
+
+       ret = nfp_nsp_hwinfo_set(nsp, hwinfo_mac, sizeof(hwinfo_mac));
+       nfp_nsp_close(nsp);
+
+       if (ret != 0) {
+               PMD_DRV_LOG(ERR, "HWinfo set failed: %d.", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
+int
+nfp_net_get_eeprom(struct rte_eth_dev *dev,
+               struct rte_dev_eeprom_info *eeprom)
+{
+       int ret;
+       uint32_t nfp_idx;
+       struct nfp_net_hw *net_hw;
+       struct rte_ether_addr mac_addr;
+       struct nfp_net_hw_priv *hw_priv;
+
+       if (eeprom->length == 0)
+               return -EINVAL;
+
+       hw_priv = dev->process_private;
+       nfp_idx = nfp_net_get_nfp_index(dev);
+
+       ret = nfp_net_get_port_mac_hwinfo(hw_priv, nfp_idx, &mac_addr);
+       if (ret != 0)
+               return -EOPNOTSUPP;
+
+       net_hw = nfp_net_get_hw(dev);
+       eeprom->magic = net_hw->vendor_id | (net_hw->device_id << 16);
+       memcpy(eeprom->data, mac_addr.addr_bytes + eeprom->offset, 
eeprom->length);
+
+       return 0;
+}
+
+int
+nfp_net_set_eeprom(struct rte_eth_dev *dev,
+               struct rte_dev_eeprom_info *eeprom)
+{
+       int ret;
+       uint32_t nfp_idx;
+       struct nfp_net_hw *net_hw;
+       struct rte_ether_addr mac_addr;
+       struct nfp_net_hw_priv *hw_priv;
+
+       if (eeprom->length == 0)
+               return -EINVAL;
+
+       net_hw = nfp_net_get_hw(dev);
+       if (eeprom->magic != (uint32_t)(net_hw->vendor_id | (net_hw->device_id 
<< 16)))
+               return -EINVAL;
+
+       hw_priv = dev->process_private;
+       nfp_idx = nfp_net_get_nfp_index(dev);
+       ret = nfp_net_get_port_mac_hwinfo(hw_priv, nfp_idx, &mac_addr);
+       if (ret != 0)
+               return -EOPNOTSUPP;
+
+       memcpy(mac_addr.addr_bytes + eeprom->offset, eeprom->data, 
eeprom->length);
+       ret = nfp_net_set_port_mac_hwinfo(hw_priv, nfp_idx, &mac_addr);
+       if (ret != 0)
+               return -EOPNOTSUPP;
+
+       return 0;
+}
+
+int
+nfp_net_get_module_info(struct rte_eth_dev *dev,
+               struct rte_eth_dev_module_info *info)
+{
+       int ret = 0;
+       uint8_t data;
+       uint32_t idx;
+       uint32_t read_len;
+       struct nfp_nsp *nsp;
+       struct nfp_net_hw_priv *hw_priv;
+       struct nfp_eth_table_port *eth_port;
+
+       hw_priv = dev->process_private;
+       nsp = nfp_nsp_open(hw_priv->pf_dev->cpp);
+       if (nsp == NULL) {
+               PMD_DRV_LOG(ERR, "Unable to open NSP.");
+               return -EIO;
+       }
+
+       if (!nfp_nsp_has_read_module_eeprom(nsp)) {
+               PMD_DRV_LOG(ERR, "Read module eeprom not supported. Please 
update flash.");
+               ret = -EOPNOTSUPP;
+               goto exit_close_nsp;
+       }
+
+       idx = nfp_net_get_idx(dev);
+       eth_port = &hw_priv->pf_dev->nfp_eth_table->ports[idx];
+       switch (eth_port->interface) {
+       case NFP_INTERFACE_SFP:
+               /* FALLTHROUGH */
+       case NFP_INTERFACE_SFP28:
+               /* Read which revision the transceiver compiles with */
+               ret = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
+                               SFP_SFF8472_COMPLIANCE, &data, 1, &read_len);
+               if (ret != 0)
+                       goto exit_close_nsp;
+
+               if (data == 0) {
+                       info->type = RTE_ETH_MODULE_SFF_8079;
+                       info->eeprom_len = RTE_ETH_MODULE_SFF_8079_LEN;
+               } else {
+                       info->type = RTE_ETH_MODULE_SFF_8472;
+                       info->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
+               }
+               break;
+       case NFP_INTERFACE_QSFP:
+               /* Read which revision the transceiver compiles with */
+               ret = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
+                               SFP_SFF_REV_COMPLIANCE, &data, 1, &read_len);
+               if (ret != 0)
+                       goto exit_close_nsp;
+
+               if (data == 0) {
+                       info->type = RTE_ETH_MODULE_SFF_8436;
+                       info->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
+               } else {
+                       info->type = RTE_ETH_MODULE_SFF_8636;
+                       info->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
+               }
+               break;
+       case NFP_INTERFACE_QSFP28:
+               info->type = RTE_ETH_MODULE_SFF_8636;
+               info->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
+               break;
+       default:
+               PMD_DRV_LOG(ERR, "Unsupported module %#x detected.",
+                               eth_port->interface);
+               ret = -EINVAL;
+       }
+
+exit_close_nsp:
+       nfp_nsp_close(nsp);
+       return ret;
+}
+
+int
+nfp_net_get_module_eeprom(struct rte_eth_dev *dev,
+               struct rte_dev_eeprom_info *info)
+{
+       int ret = 0;
+       uint32_t idx;
+       struct nfp_nsp *nsp;
+       struct nfp_net_hw_priv *hw_priv;
+       struct nfp_eth_table_port *eth_port;
+
+       hw_priv = dev->process_private;
+       nsp = nfp_nsp_open(hw_priv->pf_dev->cpp);
+       if (nsp == NULL) {
+               PMD_DRV_LOG(ERR, "Unable to open NSP.");
+               return -EIO;
+       }
+
+       if (!nfp_nsp_has_read_module_eeprom(nsp)) {
+               PMD_DRV_LOG(ERR, "Read module eeprom not supported. Please 
update flash.");
+               ret = -EOPNOTSUPP;
+               goto exit_close_nsp;
+       }
+
+       idx = nfp_net_get_idx(dev);
+       eth_port = &hw_priv->pf_dev->nfp_eth_table->ports[idx];
+       ret = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index, info->offset,
+                       info->data, info->length, &info->length);
+       if (ret != 0) {
+               if (info->length)
+                       PMD_DRV_LOG(ERR, "Incomplete read from module EEPROM: 
%d.", ret);
+               else
+                       PMD_DRV_LOG(ERR, "Read from module EEPROM failed: %d.", 
ret);
+       }
+
+exit_close_nsp:
+       nfp_nsp_close(nsp);
+       return ret;
+}
diff --git a/drivers/net/nfp/nfp_net_common.h b/drivers/net/nfp/nfp_net_common.h
index a9581827f9..5ad698cad2 100644
--- a/drivers/net/nfp/nfp_net_common.h
+++ b/drivers/net/nfp/nfp_net_common.h
@@ -394,6 +394,12 @@ void nfp_net_notify_port_speed(struct nfp_net_hw *hw,
                struct rte_eth_link *link);
 bool nfp_net_recv_pkt_meta_check_register(struct nfp_net_hw_priv *hw_priv);
 
+int nfp_net_get_eeprom_len(struct rte_eth_dev *dev);
+int nfp_net_get_eeprom(struct rte_eth_dev *dev, struct rte_dev_eeprom_info 
*eeprom);
+int nfp_net_set_eeprom(struct rte_eth_dev *dev, struct rte_dev_eeprom_info 
*eeprom);
+int nfp_net_get_module_info(struct rte_eth_dev *dev, struct 
rte_eth_dev_module_info *info);
+int nfp_net_get_module_eeprom(struct rte_eth_dev *dev, struct 
rte_dev_eeprom_info *info);
+
 #define NFP_PRIV_TO_APP_FW_NIC(app_fw_priv)\
        ((struct nfp_app_fw_nic *)app_fw_priv)
 
diff --git a/drivers/net/nfp/nfpcore/nfp_nsp.c 
b/drivers/net/nfp/nfpcore/nfp_nsp.c
index 3afbcffa42..9837b3354b 100644
--- a/drivers/net/nfp/nfpcore/nfp_nsp.c
+++ b/drivers/net/nfp/nfpcore/nfp_nsp.c
@@ -808,6 +808,102 @@ nfp_nsp_hwinfo_lookup_real(struct nfp_nsp *state,
        return nfp_nsp_command_buf(state, &hwinfo_lookup);
 }
 
+static int
+nfp_nsp_read_module_eeprom_real(struct nfp_nsp *state,
+               void *buf,
+               uint32_t size)
+{
+       struct nfp_nsp_command_buf_arg module_eeprom = {
+               {
+                       .code   = SPCODE_READ_SFF_EEPROM,
+                       .option = size,
+               },
+               .in_buf         = buf,
+               .in_size        = size,
+               .out_buf        = buf,
+               .out_size       = size,
+       };
+
+       return nfp_nsp_command_buf(state, &module_eeprom);
+}
+
+int
+nfp_nsp_read_module_eeprom(struct nfp_nsp *state,
+               int eth_index,
+               uint32_t offset,
+               void *data,
+               uint32_t len,
+               uint32_t *read_len)
+{
+       int ret;
+       int bufsz;
+       struct eeprom_buf {
+               uint8_t metalen;
+               rte_le16_t length;
+               rte_le16_t offset;
+               rte_le16_t readlen;
+               uint8_t eth_index;
+               uint8_t data[];
+       } __rte_packed * buf;
+
+       /* Buffer must be large enough and rounded to the next block size. */
+       bufsz = sizeof(*(buf)) + sizeof((buf)->data[0]) *
+                       (RTE_ALIGN_CEIL(len, NSP_SFF_EEPROM_BLOCK_LEN));
+       buf = calloc(1, bufsz);
+       if (buf == NULL)
+               return -ENOMEM;
+
+       buf->metalen = offsetof(struct eeprom_buf, data) / 
NSP_SFF_EEPROM_BLOCK_LEN;
+       buf->length = rte_cpu_to_le_16(len);
+       buf->offset = rte_cpu_to_le_16(offset);
+       buf->eth_index = eth_index;
+
+       ret = nfp_nsp_read_module_eeprom_real(state, buf, bufsz);
+       if (ret != 0)
+               goto free_exit;
+
+       if (rte_le_to_cpu_16(buf->readlen) < len) {
+               ret = -EIO;
+               goto free_exit;
+       }
+
+       if (len != 0)
+               memcpy(data, buf->data, len);
+
+       *read_len = len;
+
+free_exit:
+       free(buf);
+       return ret;
+}
+
+int
+nfp_nsp_hwinfo_lookup(struct nfp_nsp *state,
+               void *buf,
+               uint32_t size)
+{
+       int ret;
+       uint32_t size_tmp;
+
+       if (!nfp_nsp_has_hwinfo_lookup(state)) {
+               PMD_DRV_LOG(ERR, "NSP HWinfo lookup not supported. Please 
update flash.");
+               return -EOPNOTSUPP;
+       }
+
+       size_tmp = RTE_MIN(size, NFP_HWINFO_LOOKUP_SIZE);
+
+       ret = nfp_nsp_hwinfo_lookup_real(state, buf, size, false);
+       if (ret != 0)
+               return ret;
+
+       if (strnlen(buf, size_tmp) == size_tmp) {
+               PMD_DRV_LOG(ERR, "NSP HWinfo value not NULL terminated.");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 int
 nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state,
                void *buf,
diff --git a/drivers/net/nfp/nfpcore/nfp_nsp.h 
b/drivers/net/nfp/nfpcore/nfp_nsp.h
index cfb5066fc9..0ae10dabfb 100644
--- a/drivers/net/nfp/nfpcore/nfp_nsp.h
+++ b/drivers/net/nfp/nfpcore/nfp_nsp.h
@@ -8,6 +8,11 @@
 
 #include "nfp_cpp.h"
 
+/* EEPROM byte offsets */
+#define SFP_SFF8472_COMPLIANCE          0x5e
+#define SFP_SFF_REV_COMPLIANCE          1
+#define NSP_SFF_EEPROM_BLOCK_LEN        8
+
 /* Defines the valid values of the 'abi_drv_reset' hwinfo key */
 #define NFP_NSP_DRV_RESET_DISK                  0
 #define NFP_NSP_DRV_RESET_ALWAYS                1
@@ -238,8 +243,12 @@ int nfp_hwmon_read_sensor(struct nfp_cpp *cpp, enum 
nfp_nsp_sensor_id id,
                uint32_t *val);
 bool nfp_nsp_fw_loaded(struct nfp_nsp *state);
 int nfp_nsp_load_stored_fw(struct nfp_nsp *state);
+int nfp_nsp_hwinfo_lookup(struct nfp_nsp *state, void *buf, uint32_t size);
 int nfp_nsp_hwinfo_lookup_optional(struct nfp_nsp *state,
                void *buf, size_t size, const char *default_val);
+int nfp_nsp_read_module_eeprom(struct nfp_nsp *state, int eth_index,
+               uint32_t offset, void *data,
+               uint32_t len, uint32_t *read_len);
 
 /* The buf used to receive bitmap of link modes */
 struct nfp_eth_media_buf {
-- 
2.43.5

Reply via email to