1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020 Mellanox Technologies Ltd */
4 #include <linux/mlx5/driver.h>
5 #include <linux/mlx5/device.h>
6 #include <linux/mlx5/eswitch.h>
11 static int mlx5_core_peer_devlink_set(struct mlx5_sf_dev *sf_dev, struct devlink *devlink)
13 struct mlx5_sf_peer_devlink_event_ctx event_ctx = {
14 .fn_id = sf_dev->fn_id,
19 ret = mlx5_blocking_notifier_call_chain(sf_dev->parent_mdev,
20 MLX5_DRIVER_EVENT_SF_PEER_DEVLINK,
22 return ret == NOTIFY_OK ? event_ctx.err : 0;
25 static int mlx5_sf_dev_probe(struct auxiliary_device *adev, const struct auxiliary_device_id *id)
27 struct mlx5_sf_dev *sf_dev = container_of(adev, struct mlx5_sf_dev, adev);
28 struct mlx5_core_dev *mdev;
29 struct devlink *devlink;
32 devlink = mlx5_devlink_alloc(&adev->dev);
36 mdev = devlink_priv(devlink);
37 mdev->device = &adev->dev;
38 mdev->pdev = sf_dev->parent_mdev->pdev;
39 mdev->bar_addr = sf_dev->bar_base_addr;
40 mdev->iseg_base = sf_dev->bar_base_addr;
41 mdev->coredev_type = MLX5_COREDEV_SF;
42 mdev->priv.parent_mdev = sf_dev->parent_mdev;
43 mdev->priv.adev_idx = adev->id;
46 /* Only local SFs do light probe */
47 if (MLX5_ESWITCH_MANAGER(sf_dev->parent_mdev))
48 mlx5_dev_set_lightweight(mdev);
50 err = mlx5_mdev_init(mdev, MLX5_SF_PROF);
52 mlx5_core_warn(mdev, "mlx5_mdev_init on err=%d\n", err);
56 mdev->iseg = ioremap(mdev->iseg_base, sizeof(*mdev->iseg));
58 mlx5_core_warn(mdev, "remap error\n");
63 if (MLX5_ESWITCH_MANAGER(sf_dev->parent_mdev))
64 err = mlx5_init_one_light(mdev);
66 err = mlx5_init_one(mdev);
68 mlx5_core_warn(mdev, "mlx5_init_one err=%d\n", err);
72 err = mlx5_core_peer_devlink_set(sf_dev, devlink);
74 mlx5_core_warn(mdev, "mlx5_core_peer_devlink_set err=%d\n", err);
75 goto peer_devlink_set_err;
78 devlink_register(devlink);
82 if (mlx5_dev_is_lightweight(sf_dev->mdev))
83 mlx5_uninit_one_light(sf_dev->mdev);
85 mlx5_uninit_one(sf_dev->mdev);
89 mlx5_mdev_uninit(mdev);
91 mlx5_devlink_free(devlink);
95 static void mlx5_sf_dev_remove(struct auxiliary_device *adev)
97 struct mlx5_sf_dev *sf_dev = container_of(adev, struct mlx5_sf_dev, adev);
98 struct mlx5_core_dev *mdev = sf_dev->mdev;
99 struct devlink *devlink;
101 devlink = priv_to_devlink(mdev);
102 set_bit(MLX5_BREAK_FW_WAIT, &mdev->intf_state);
103 mlx5_drain_health_wq(mdev);
104 if (mlx5_dev_is_lightweight(mdev))
105 mlx5_uninit_one_light(mdev);
107 mlx5_uninit_one(mdev);
109 mlx5_mdev_uninit(mdev);
110 mlx5_devlink_free(devlink);
113 static void mlx5_sf_dev_shutdown(struct auxiliary_device *adev)
115 struct mlx5_sf_dev *sf_dev = container_of(adev, struct mlx5_sf_dev, adev);
116 struct mlx5_core_dev *mdev = sf_dev->mdev;
118 set_bit(MLX5_BREAK_FW_WAIT, &mdev->intf_state);
119 mlx5_unload_one(mdev, false);
122 static const struct auxiliary_device_id mlx5_sf_dev_id_table[] = {
123 { .name = MLX5_ADEV_NAME "." MLX5_SF_DEV_ID_NAME, },
127 MODULE_DEVICE_TABLE(auxiliary, mlx5_sf_dev_id_table);
129 static struct auxiliary_driver mlx5_sf_driver = {
130 .name = MLX5_SF_DEV_ID_NAME,
131 .probe = mlx5_sf_dev_probe,
132 .remove = mlx5_sf_dev_remove,
133 .shutdown = mlx5_sf_dev_shutdown,
134 .id_table = mlx5_sf_dev_id_table,
137 int mlx5_sf_driver_register(void)
139 return auxiliary_driver_register(&mlx5_sf_driver);
142 void mlx5_sf_driver_unregister(void)
144 auxiliary_driver_unregister(&mlx5_sf_driver);