From: Mateusz Polchlopek <[email protected]>

Introduce a helper ice_get_vf_by_dev() to look up VF for a given struct
device.

Signed-off-by: Mateusz Polchlopek <[email protected]>
Reviewed-by: Przemek Kitszel <[email protected]>
Signed-off-by: Larysa Zaremba <[email protected]>
---
 drivers/net/ethernet/intel/ice/ice_vf_lib.c | 30 +++++++++++++++++++++
 drivers/net/ethernet/intel/ice/ice_vf_lib.h |  6 +++++
 2 files changed, 36 insertions(+)

diff --git a/drivers/net/ethernet/intel/ice/ice_vf_lib.c 
b/drivers/net/ethernet/intel/ice/ice_vf_lib.c
index 2ffdae9a82df..21d22e3ad0ba 100644
--- a/drivers/net/ethernet/intel/ice/ice_vf_lib.c
+++ b/drivers/net/ethernet/intel/ice/ice_vf_lib.c
@@ -45,6 +45,36 @@ struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
        return NULL;
 }
 
+/**
+ * ice_get_vf_by_dev - Get pointer to VF by device
+ * @dev: the device structure
+ *
+ * Locate and return a pointer to the VF structure associated with a given
+ * device.
+ * Return: valid VF structure associated with the device, NULL if none exists
+ */
+struct ice_vf *ice_get_vf_by_dev(struct device *dev)
+{
+       struct pci_dev *vfdev;
+       struct pci_dev *pdev;
+       struct ice_pf *pf;
+       struct ice_vf *vf;
+       unsigned int bkt;
+
+       vfdev = container_of(dev, struct pci_dev, dev);
+       pdev = vfdev->physfn;
+       pf = pci_get_drvdata(pdev);
+
+       rcu_read_lock();
+       ice_for_each_vf_rcu(pf, bkt, vf) {
+               if (vf->vfdev == vfdev)
+                       break;
+       }
+       rcu_read_unlock();
+
+       return vf;
+}
+
 /**
  * ice_release_vf - Release VF associated with a refcount
  * @ref: the kref decremented to zero
diff --git a/drivers/net/ethernet/intel/ice/ice_vf_lib.h 
b/drivers/net/ethernet/intel/ice/ice_vf_lib.h
index 0cc9034065c5..48f4cdbd6d27 100644
--- a/drivers/net/ethernet/intel/ice/ice_vf_lib.h
+++ b/drivers/net/ethernet/intel/ice/ice_vf_lib.h
@@ -213,6 +213,7 @@ static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf 
*vf)
 
 #ifdef CONFIG_PCI_IOV
 struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
+struct ice_vf *ice_get_vf_by_dev(struct device *dev);
 void ice_put_vf(struct ice_vf *vf);
 bool ice_has_vfs(struct ice_pf *pf);
 u16 ice_get_num_vfs(struct ice_pf *pf);
@@ -237,6 +238,11 @@ static inline struct ice_vf *ice_get_vf_by_id(struct 
ice_pf *pf, u16 vf_id)
        return NULL;
 }
 
+static inline struct ice_vf *ice_get_vf_by_dev(struct device *dev)
+{
+       return NULL;
+}
+
 static inline void ice_put_vf(struct ice_vf *vf)
 {
 }
-- 
2.43.0

Reply via email to