xref: /OK3568_Linux_fs/kernel/drivers/vdpa/mlx5/net/main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2*4882a593Smuzhiyun /* Copyright (c) 2020 Mellanox Technologies Ltd. */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <linux/module.h>
5*4882a593Smuzhiyun #include <linux/mlx5/driver.h>
6*4882a593Smuzhiyun #include <linux/mlx5/device.h>
7*4882a593Smuzhiyun #include "mlx5_vdpa_ifc.h"
8*4882a593Smuzhiyun #include "mlx5_vnet.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
11*4882a593Smuzhiyun MODULE_DESCRIPTION("Mellanox VDPA driver");
12*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
13*4882a593Smuzhiyun 
required_caps_supported(struct mlx5_core_dev * mdev)14*4882a593Smuzhiyun static bool required_caps_supported(struct mlx5_core_dev *mdev)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	u8 event_mode;
17*4882a593Smuzhiyun 	u64 got;
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 	got = MLX5_CAP_GEN_64(mdev, general_obj_types);
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 	if (!(got & MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q))
22*4882a593Smuzhiyun 		return false;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	event_mode = MLX5_CAP_DEV_VDPA_EMULATION(mdev, event_mode);
25*4882a593Smuzhiyun 	if (!(event_mode & MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE))
26*4882a593Smuzhiyun 		return false;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	if (!MLX5_CAP_DEV_VDPA_EMULATION(mdev, eth_frame_offload_type))
29*4882a593Smuzhiyun 		return false;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	return true;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
mlx5_vdpa_add(struct mlx5_core_dev * mdev)34*4882a593Smuzhiyun static void *mlx5_vdpa_add(struct mlx5_core_dev *mdev)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct mlx5_vdpa_dev *vdev;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	if (mlx5_core_is_pf(mdev))
39*4882a593Smuzhiyun 		return NULL;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	if (!required_caps_supported(mdev)) {
42*4882a593Smuzhiyun 		dev_info(mdev->device, "virtio net emulation not supported\n");
43*4882a593Smuzhiyun 		return NULL;
44*4882a593Smuzhiyun 	}
45*4882a593Smuzhiyun 	vdev = mlx5_vdpa_add_dev(mdev);
46*4882a593Smuzhiyun 	if (IS_ERR(vdev))
47*4882a593Smuzhiyun 		return NULL;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return vdev;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
mlx5_vdpa_remove(struct mlx5_core_dev * mdev,void * context)52*4882a593Smuzhiyun static void mlx5_vdpa_remove(struct mlx5_core_dev *mdev, void *context)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	struct mlx5_vdpa_dev *vdev = context;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	mlx5_vdpa_remove_dev(vdev);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun static struct mlx5_interface mlx5_vdpa_interface = {
60*4882a593Smuzhiyun 	.add = mlx5_vdpa_add,
61*4882a593Smuzhiyun 	.remove = mlx5_vdpa_remove,
62*4882a593Smuzhiyun 	.protocol = MLX5_INTERFACE_PROTOCOL_VDPA,
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
mlx5_vdpa_init(void)65*4882a593Smuzhiyun static int __init mlx5_vdpa_init(void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	return mlx5_register_interface(&mlx5_vdpa_interface);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
mlx5_vdpa_exit(void)70*4882a593Smuzhiyun static void __exit mlx5_vdpa_exit(void)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	mlx5_unregister_interface(&mlx5_vdpa_interface);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun module_init(mlx5_vdpa_init);
76*4882a593Smuzhiyun module_exit(mlx5_vdpa_exit);
77