PCI_SLOT(dev->pdev->devfn));
 }
 
-static int next_phys_dev(struct device *dev, const void *data)
+static int _next_phys_dev(struct mlx5_core_dev *mdev,
+                         const struct mlx5_core_dev *curr)
 {
-       struct mlx5_adev *madev = container_of(dev, struct mlx5_adev, adev.dev);
-       struct mlx5_core_dev *mdev = madev->mdev;
-       const struct mlx5_core_dev *curr = data;
-
        if (!mlx5_core_is_pf(mdev))
                return 0;
 
        return 1;
 }
 
-/* Must be called with intf_mutex held */
-struct mlx5_core_dev *mlx5_get_next_phys_dev(struct mlx5_core_dev *dev)
+static int next_phys_dev(struct device *dev, const void *data)
+{
+       struct mlx5_adev *madev = container_of(dev, struct mlx5_adev, adev.dev);
+       struct mlx5_core_dev *mdev = madev->mdev;
+
+       return _next_phys_dev(mdev, data);
+}
+
+static int next_phys_dev_lag(struct device *dev, const void *data)
+{
+       struct mlx5_adev *madev = container_of(dev, struct mlx5_adev, adev.dev);
+       struct mlx5_core_dev *mdev = madev->mdev;
+
+       if (!MLX5_CAP_GEN(mdev, vport_group_manager) ||
+           !MLX5_CAP_GEN(mdev, lag_master) ||
+           MLX5_CAP_GEN(mdev, num_lag_ports) != MLX5_MAX_PORTS)
+               return 0;
+
+       return _next_phys_dev(mdev, data);
+}
+
+static struct mlx5_core_dev *mlx5_get_next_dev(struct mlx5_core_dev *dev,
+                                              int (*match)(struct device *dev, const void *data))
 {
        struct auxiliary_device *adev;
        struct mlx5_adev *madev;
        if (!mlx5_core_is_pf(dev))
                return NULL;
 
-       adev = auxiliary_find_device(NULL, dev, &next_phys_dev);
+       adev = auxiliary_find_device(NULL, dev, match);
        if (!adev)
                return NULL;
 
        return madev->mdev;
 }
 
+/* Must be called with intf_mutex held */
+struct mlx5_core_dev *mlx5_get_next_phys_dev(struct mlx5_core_dev *dev)
+{
+       lockdep_assert_held(&mlx5_intf_mutex);
+       return mlx5_get_next_dev(dev, &next_phys_dev);
+}
+
+/* Must be called with intf_mutex held */
+struct mlx5_core_dev *mlx5_get_next_phys_dev_lag(struct mlx5_core_dev *dev)
+{
+       lockdep_assert_held(&mlx5_intf_mutex);
+       return mlx5_get_next_dev(dev, &next_phys_dev_lag);
+}
+
 void mlx5_dev_list_lock(void)
 {
        mutex_lock(&mlx5_intf_mutex);
 
        struct mlx5_lag *ldev = NULL;
        struct mlx5_core_dev *tmp_dev;
 
-       if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
-           !MLX5_CAP_GEN(dev, lag_master) ||
-           MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS)
-               return 0;
-
-       tmp_dev = mlx5_get_next_phys_dev(dev);
+       tmp_dev = mlx5_get_next_phys_dev_lag(dev);
        if (tmp_dev)
                ldev = tmp_dev->priv.lag;
 
 {
        int err;
 
+       if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
+           !MLX5_CAP_GEN(dev, lag_master) ||
+           MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS)
+               return;
+
 recheck:
        mlx5_dev_list_lock();
        err = __mlx5_lag_dev_add_mdev(dev);