net/mlx5: Split mdev init and pci init
authorSaeed Mahameed <saeedm@mellanox.com>
Fri, 29 Mar 2019 22:37:54 +0000 (15:37 -0700)
committerSaeed Mahameed <saeedm@mellanox.com>
Tue, 2 Apr 2019 19:49:37 +0000 (12:49 -0700)
Separate resources initialization from pci initialization.

This provides a better logical separation of mlx5 core device
initialization flow and will help to seamlessly support creating different
mlx5 device types such as PF, VF and SF mlx5 sub-function virtual device.

This patch does not change any functionality.

Signed-off-by: Vu Pham <vuhuong@mellanox.com>
Signed-off-by: Saeed Mahameed <saeedm@mellanox.com>
drivers/net/ethernet/mellanox/mlx5/core/main.c

index cff7f2ed823d93cd8c1ec2e9d836377ea8f00a94..e26246eacea525956534955da7fee36e7be050c0 100644 (file)
@@ -729,32 +729,23 @@ static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
        return -EOPNOTSUPP;
 }
 
-static int mlx5_pci_init(struct mlx5_core_dev *dev)
+static int mlx5_pci_init(struct mlx5_core_dev *dev, struct pci_dev *pdev,
+                        const struct pci_device_id *id)
 {
        struct mlx5_priv *priv = &dev->priv;
-       struct pci_dev *pdev = dev->pdev;
        int err = 0;
 
-       pci_set_drvdata(dev->pdev, dev);
-       strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
-       priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
-
-       mutex_init(&priv->pgdir_mutex);
-       INIT_LIST_HEAD(&priv->pgdir_list);
-       spin_lock_init(&priv->mkey_lock);
+       dev->pdev = pdev;
+       priv->pci_dev_data = id->driver_data;
 
-       mutex_init(&priv->alloc_mutex);
+       pci_set_drvdata(dev->pdev, dev);
 
        priv->numa_node = dev_to_node(&dev->pdev->dev);
 
-       if (mlx5_debugfs_root)
-               priv->dbg_root =
-                       debugfs_create_dir(pci_name(pdev), mlx5_debugfs_root);
-
        err = mlx5_pci_enable_device(dev);
        if (err) {
                dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
-               goto err_dbg;
+               return err;
        }
 
        err = request_bar(pdev);
@@ -791,9 +782,6 @@ err_clr_master:
        release_bar(dev->pdev);
 err_disable:
        mlx5_pci_disable_device(dev);
-
-err_dbg:
-       debugfs_remove(priv->dbg_root);
        return err;
 }
 
@@ -803,7 +791,6 @@ static void mlx5_pci_close(struct mlx5_core_dev *dev)
        pci_clear_master(dev->pdev);
        release_bar(dev->pdev);
        mlx5_pci_disable_device(dev);
-       debugfs_remove_recursive(dev->priv.dbg_root);
 }
 
 static int mlx5_init_once(struct mlx5_core_dev *dev)
@@ -1230,13 +1217,49 @@ static const struct devlink_ops mlx5_devlink_ops = {
 #endif
 };
 
+static int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx, const char *name)
+{
+       struct mlx5_priv *priv = &dev->priv;
+
+       strncpy(priv->name, name, MLX5_MAX_NAME_LEN);
+       priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
+
+       dev->profile = &profile[profile_idx];
+
+       INIT_LIST_HEAD(&priv->ctx_list);
+       spin_lock_init(&priv->ctx_lock);
+       mutex_init(&dev->pci_status_mutex);
+       mutex_init(&dev->intf_state_mutex);
+
+       mutex_init(&priv->bfregs.reg_head.lock);
+       mutex_init(&priv->bfregs.wc_head.lock);
+       INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
+       INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
+
+       mutex_init(&priv->alloc_mutex);
+       mutex_init(&priv->pgdir_mutex);
+       INIT_LIST_HEAD(&priv->pgdir_list);
+       spin_lock_init(&priv->mkey_lock);
+
+       priv->dbg_root = debugfs_create_dir(name, mlx5_debugfs_root);
+       if (!priv->dbg_root) {
+               pr_err("mlx5_core: %s error, Cannot create debugfs dir, aborting\n", name);
+               return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
+{
+       debugfs_remove_recursive(dev->priv.dbg_root);
+}
+
 #define MLX5_IB_MOD "mlx5_ib"
-static int init_one(struct pci_dev *pdev,
-                   const struct pci_device_id *id)
+static int init_one(struct pci_dev *pdev, const struct pci_device_id *id)
 {
        struct mlx5_core_dev *dev;
        struct devlink *devlink;
-       struct mlx5_priv *priv;
        int err;
 
        devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
@@ -1246,28 +1269,15 @@ static int init_one(struct pci_dev *pdev,
        }
 
        dev = devlink_priv(devlink);
-       priv = &dev->priv;
-       priv->pci_dev_data = id->driver_data;
-
-       pci_set_drvdata(pdev, dev);
-
-       dev->pdev = pdev;
-       dev->profile = &profile[prof_sel];
 
-       INIT_LIST_HEAD(&priv->ctx_list);
-       spin_lock_init(&priv->ctx_lock);
-       mutex_init(&dev->pci_status_mutex);
-       mutex_init(&dev->intf_state_mutex);
-
-       mutex_init(&priv->bfregs.reg_head.lock);
-       mutex_init(&priv->bfregs.wc_head.lock);
-       INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
-       INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
+       err = mlx5_mdev_init(dev, prof_sel, dev_name(&pdev->dev));
+       if (err)
+               goto mdev_init_err;
 
-       err = mlx5_pci_init(dev);
+       err = mlx5_pci_init(dev, pdev, id);
        if (err) {
                dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
-               goto clean_dev;
+               goto pci_init_err;
        }
 
        err = mlx5_health_init(dev);
@@ -1303,7 +1313,9 @@ err_pagealloc_init:
        mlx5_health_cleanup(dev);
 close_pci:
        mlx5_pci_close(dev);
-clean_dev:
+pci_init_err:
+       mlx5_mdev_uninit(dev);
+mdev_init_err:
        devlink_free(devlink);
 
        return err;
@@ -1326,6 +1338,7 @@ static void remove_one(struct pci_dev *pdev)
        mlx5_pagealloc_cleanup(dev);
        mlx5_health_cleanup(dev);
        mlx5_pci_close(dev);
+       mlx5_mdev_uninit(dev);
        devlink_free(devlink);
 }