xref: /openbmc/linux/drivers/vdpa/mlx5/net/mlx5_vnet.c (revision 5ee9cd065836e5934710ca35653bce7905add20b)
11a86b377SEli Cohen // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
21a86b377SEli Cohen /* Copyright (c) 2020 Mellanox Technologies Ltd. */
31a86b377SEli Cohen 
474c9729dSLeon Romanovsky #include <linux/module.h>
51a86b377SEli Cohen #include <linux/vdpa.h>
674c9729dSLeon Romanovsky #include <linux/vringh.h>
774c9729dSLeon Romanovsky #include <uapi/linux/virtio_net.h>
81a86b377SEli Cohen #include <uapi/linux/virtio_ids.h>
9a007d940SEli Cohen #include <uapi/linux/vdpa.h>
101a86b377SEli Cohen #include <linux/virtio_config.h>
1174c9729dSLeon Romanovsky #include <linux/auxiliary_bus.h>
1274c9729dSLeon Romanovsky #include <linux/mlx5/cq.h>
131a86b377SEli Cohen #include <linux/mlx5/qp.h>
141a86b377SEli Cohen #include <linux/mlx5/device.h>
1574c9729dSLeon Romanovsky #include <linux/mlx5/driver.h>
161a86b377SEli Cohen #include <linux/mlx5/vport.h>
171a86b377SEli Cohen #include <linux/mlx5/fs.h>
180aae392bSLeon Romanovsky #include <linux/mlx5/mlx5_ifc_vdpa.h>
197c9f131fSEli Cohen #include <linux/mlx5/mpfs.h>
201a86b377SEli Cohen #include "mlx5_vdpa.h"
2172c67e9bSEli Cohen #include "mlx5_vnet.h"
221a86b377SEli Cohen 
2374c9729dSLeon Romanovsky MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
2474c9729dSLeon Romanovsky MODULE_DESCRIPTION("Mellanox VDPA driver");
2574c9729dSLeon Romanovsky MODULE_LICENSE("Dual BSD/GPL");
2674c9729dSLeon Romanovsky 
271a86b377SEli Cohen #define VALID_FEATURES_MASK                                                                        \
28cbb52359SNathan Chancellor 	(BIT_ULL(VIRTIO_NET_F_CSUM) | BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |                                   \
29cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) | BIT_ULL(VIRTIO_NET_F_MTU) | BIT_ULL(VIRTIO_NET_F_MAC) |   \
30cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) | BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |                             \
31cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_GUEST_ECN) | BIT_ULL(VIRTIO_NET_F_GUEST_UFO) | BIT_ULL(VIRTIO_NET_F_HOST_TSO4) | \
32cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_HOST_TSO6) | BIT_ULL(VIRTIO_NET_F_HOST_ECN) | BIT_ULL(VIRTIO_NET_F_HOST_UFO) |   \
33cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) | BIT_ULL(VIRTIO_NET_F_STATUS) | BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |      \
34cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_CTRL_RX) | BIT_ULL(VIRTIO_NET_F_CTRL_VLAN) |                                 \
35cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA) | BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE) |                      \
36cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_MQ) | BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) | BIT_ULL(VIRTIO_NET_F_HASH_REPORT) |  \
37cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_RSS) | BIT_ULL(VIRTIO_NET_F_RSC_EXT) | BIT_ULL(VIRTIO_NET_F_STANDBY) |           \
38cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX) | BIT_ULL(VIRTIO_F_NOTIFY_ON_EMPTY) |                          \
39cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_F_ANY_LAYOUT) | BIT_ULL(VIRTIO_F_VERSION_1) | BIT_ULL(VIRTIO_F_ACCESS_PLATFORM) |      \
40cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_F_RING_PACKED) | BIT_ULL(VIRTIO_F_ORDER_PLATFORM) | BIT_ULL(VIRTIO_F_SR_IOV))
411a86b377SEli Cohen 
421a86b377SEli Cohen #define VALID_STATUS_MASK                                                                          \
431a86b377SEli Cohen 	(VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK |        \
441a86b377SEli Cohen 	 VIRTIO_CONFIG_S_FEATURES_OK | VIRTIO_CONFIG_S_NEEDS_RESET | VIRTIO_CONFIG_S_FAILED)
451a86b377SEli Cohen 
46e4fc6650SEli Cohen #define MLX5_FEATURE(_mvdev, _feature) (!!((_mvdev)->actual_features & BIT_ULL(_feature)))
47e4fc6650SEli Cohen 
48baf2ad3fSEli Cohen #define MLX5V_UNTAGGED 0x1000
49baf2ad3fSEli Cohen 
501a86b377SEli Cohen struct mlx5_vdpa_cq_buf {
511a86b377SEli Cohen 	struct mlx5_frag_buf_ctrl fbc;
521a86b377SEli Cohen 	struct mlx5_frag_buf frag_buf;
531a86b377SEli Cohen 	int cqe_size;
541a86b377SEli Cohen 	int nent;
551a86b377SEli Cohen };
561a86b377SEli Cohen 
571a86b377SEli Cohen struct mlx5_vdpa_cq {
581a86b377SEli Cohen 	struct mlx5_core_cq mcq;
591a86b377SEli Cohen 	struct mlx5_vdpa_cq_buf buf;
601a86b377SEli Cohen 	struct mlx5_db db;
611a86b377SEli Cohen 	int cqe;
621a86b377SEli Cohen };
631a86b377SEli Cohen 
641a86b377SEli Cohen struct mlx5_vdpa_umem {
651a86b377SEli Cohen 	struct mlx5_frag_buf_ctrl fbc;
661a86b377SEli Cohen 	struct mlx5_frag_buf frag_buf;
671a86b377SEli Cohen 	int size;
681a86b377SEli Cohen 	u32 id;
691a86b377SEli Cohen };
701a86b377SEli Cohen 
711a86b377SEli Cohen struct mlx5_vdpa_qp {
721a86b377SEli Cohen 	struct mlx5_core_qp mqp;
731a86b377SEli Cohen 	struct mlx5_frag_buf frag_buf;
741a86b377SEli Cohen 	struct mlx5_db db;
751a86b377SEli Cohen 	u16 head;
761a86b377SEli Cohen 	bool fw;
771a86b377SEli Cohen };
781a86b377SEli Cohen 
791a86b377SEli Cohen struct mlx5_vq_restore_info {
801a86b377SEli Cohen 	u32 num_ent;
811a86b377SEli Cohen 	u64 desc_addr;
821a86b377SEli Cohen 	u64 device_addr;
831a86b377SEli Cohen 	u64 driver_addr;
841a86b377SEli Cohen 	u16 avail_index;
85b35ccebeSEli Cohen 	u16 used_index;
86bc9a2b3eSEli Cohen 	struct msi_map map;
871a86b377SEli Cohen 	bool ready;
881a86b377SEli Cohen 	bool restore;
891a86b377SEli Cohen };
901a86b377SEli Cohen 
911a86b377SEli Cohen struct mlx5_vdpa_virtqueue {
921a86b377SEli Cohen 	bool ready;
931a86b377SEli Cohen 	u64 desc_addr;
941a86b377SEli Cohen 	u64 device_addr;
951a86b377SEli Cohen 	u64 driver_addr;
961a86b377SEli Cohen 	u32 num_ent;
971a86b377SEli Cohen 
981a86b377SEli Cohen 	/* Resources for implementing the notification channel from the device
991a86b377SEli Cohen 	 * to the driver. fwqp is the firmware end of an RC connection; the
1002f72b226SXiang wangx 	 * other end is vqqp used by the driver. cq is where completions are
1011a86b377SEli Cohen 	 * reported.
1021a86b377SEli Cohen 	 */
1031a86b377SEli Cohen 	struct mlx5_vdpa_cq cq;
1041a86b377SEli Cohen 	struct mlx5_vdpa_qp fwqp;
1051a86b377SEli Cohen 	struct mlx5_vdpa_qp vqqp;
1061a86b377SEli Cohen 
1071a86b377SEli Cohen 	/* umem resources are required for the virtqueue operation. They're use
1081a86b377SEli Cohen 	 * is internal and they must be provided by the driver.
1091a86b377SEli Cohen 	 */
1101a86b377SEli Cohen 	struct mlx5_vdpa_umem umem1;
1111a86b377SEli Cohen 	struct mlx5_vdpa_umem umem2;
1121a86b377SEli Cohen 	struct mlx5_vdpa_umem umem3;
1131a86b377SEli Cohen 
1141892a3d4SEli Cohen 	u32 counter_set_id;
1151a86b377SEli Cohen 	bool initialized;
1161a86b377SEli Cohen 	int index;
1171a86b377SEli Cohen 	u32 virtq_id;
1181a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev;
1191a86b377SEli Cohen 	u16 avail_idx;
120b35ccebeSEli Cohen 	u16 used_idx;
1211a86b377SEli Cohen 	int fw_state;
122bc9a2b3eSEli Cohen 	struct msi_map map;
1231a86b377SEli Cohen 
1241a86b377SEli Cohen 	/* keep last in the struct */
1251a86b377SEli Cohen 	struct mlx5_vq_restore_info ri;
1261a86b377SEli Cohen };
1271a86b377SEli Cohen 
is_index_valid(struct mlx5_vdpa_dev * mvdev,u16 idx)128e4fc6650SEli Cohen static bool is_index_valid(struct mlx5_vdpa_dev *mvdev, u16 idx)
129e4fc6650SEli Cohen {
130f8ae3a48SEli Cohen 	if (!(mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_MQ))) {
131f8ae3a48SEli Cohen 		if (!(mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)))
132f8ae3a48SEli Cohen 			return idx < 2;
133f8ae3a48SEli Cohen 		else
134f8ae3a48SEli Cohen 			return idx < 3;
135f8ae3a48SEli Cohen 	}
136e4fc6650SEli Cohen 
137f8ae3a48SEli Cohen 	return idx <= mvdev->max_idx;
138e4fc6650SEli Cohen }
139e4fc6650SEli Cohen 
1401a86b377SEli Cohen static void free_resources(struct mlx5_vdpa_net *ndev);
1411a86b377SEli Cohen static void init_mvqs(struct mlx5_vdpa_net *ndev);
142ae0428deSEli Cohen static int setup_driver(struct mlx5_vdpa_dev *mvdev);
1431a86b377SEli Cohen static void teardown_driver(struct mlx5_vdpa_net *ndev);
1441a86b377SEli Cohen 
1451a86b377SEli Cohen static bool mlx5_vdpa_debug;
1461a86b377SEli Cohen 
1471a86b377SEli Cohen #define MLX5_LOG_VIO_FLAG(_feature)                                                                \
1481a86b377SEli Cohen 	do {                                                                                       \
149cbb52359SNathan Chancellor 		if (features & BIT_ULL(_feature))                                                  \
1501a86b377SEli Cohen 			mlx5_vdpa_info(mvdev, "%s\n", #_feature);                                  \
1511a86b377SEli Cohen 	} while (0)
1521a86b377SEli Cohen 
1531a86b377SEli Cohen #define MLX5_LOG_VIO_STAT(_status)                                                                 \
1541a86b377SEli Cohen 	do {                                                                                       \
1551a86b377SEli Cohen 		if (status & (_status))                                                            \
1561a86b377SEli Cohen 			mlx5_vdpa_info(mvdev, "%s\n", #_status);                                   \
1571a86b377SEli Cohen 	} while (0)
1581a86b377SEli Cohen 
15952893733SEli Cohen /* TODO: cross-endian support */
mlx5_vdpa_is_little_endian(struct mlx5_vdpa_dev * mvdev)16052893733SEli Cohen static inline bool mlx5_vdpa_is_little_endian(struct mlx5_vdpa_dev *mvdev)
16152893733SEli Cohen {
16252893733SEli Cohen 	return virtio_legacy_is_little_endian() ||
16352893733SEli Cohen 		(mvdev->actual_features & BIT_ULL(VIRTIO_F_VERSION_1));
16452893733SEli Cohen }
16552893733SEli Cohen 
mlx5vdpa16_to_cpu(struct mlx5_vdpa_dev * mvdev,__virtio16 val)16652893733SEli Cohen static u16 mlx5vdpa16_to_cpu(struct mlx5_vdpa_dev *mvdev, __virtio16 val)
16752893733SEli Cohen {
16852893733SEli Cohen 	return __virtio16_to_cpu(mlx5_vdpa_is_little_endian(mvdev), val);
16952893733SEli Cohen }
17052893733SEli Cohen 
cpu_to_mlx5vdpa16(struct mlx5_vdpa_dev * mvdev,u16 val)17152893733SEli Cohen static __virtio16 cpu_to_mlx5vdpa16(struct mlx5_vdpa_dev *mvdev, u16 val)
17252893733SEli Cohen {
17352893733SEli Cohen 	return __cpu_to_virtio16(mlx5_vdpa_is_little_endian(mvdev), val);
17452893733SEli Cohen }
17552893733SEli Cohen 
ctrl_vq_idx(struct mlx5_vdpa_dev * mvdev)1765262912eSEli Cohen static u16 ctrl_vq_idx(struct mlx5_vdpa_dev *mvdev)
1775262912eSEli Cohen {
17852893733SEli Cohen 	if (!(mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_MQ)))
17952893733SEli Cohen 		return 2;
18052893733SEli Cohen 
181acde3929SEli Cohen 	return mvdev->max_vqs;
1825262912eSEli Cohen }
1835262912eSEli Cohen 
is_ctrl_vq_idx(struct mlx5_vdpa_dev * mvdev,u16 idx)1845262912eSEli Cohen static bool is_ctrl_vq_idx(struct mlx5_vdpa_dev *mvdev, u16 idx)
1855262912eSEli Cohen {
1865262912eSEli Cohen 	return idx == ctrl_vq_idx(mvdev);
1875262912eSEli Cohen }
1885262912eSEli Cohen 
print_status(struct mlx5_vdpa_dev * mvdev,u8 status,bool set)1891a86b377SEli Cohen static void print_status(struct mlx5_vdpa_dev *mvdev, u8 status, bool set)
1901a86b377SEli Cohen {
1911a86b377SEli Cohen 	if (status & ~VALID_STATUS_MASK)
1921a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "Warning: there are invalid status bits 0x%x\n",
1931a86b377SEli Cohen 			       status & ~VALID_STATUS_MASK);
1941a86b377SEli Cohen 
1951a86b377SEli Cohen 	if (!mlx5_vdpa_debug)
1961a86b377SEli Cohen 		return;
1971a86b377SEli Cohen 
1981a86b377SEli Cohen 	mlx5_vdpa_info(mvdev, "driver status %s", set ? "set" : "get");
1991a86b377SEli Cohen 	if (set && !status) {
2001a86b377SEli Cohen 		mlx5_vdpa_info(mvdev, "driver resets the device\n");
2011a86b377SEli Cohen 		return;
2021a86b377SEli Cohen 	}
2031a86b377SEli Cohen 
2041a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_ACKNOWLEDGE);
2051a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_DRIVER);
2061a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_DRIVER_OK);
2071a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_FEATURES_OK);
2081a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_NEEDS_RESET);
2091a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_FAILED);
2101a86b377SEli Cohen }
2111a86b377SEli Cohen 
print_features(struct mlx5_vdpa_dev * mvdev,u64 features,bool set)2121a86b377SEli Cohen static void print_features(struct mlx5_vdpa_dev *mvdev, u64 features, bool set)
2131a86b377SEli Cohen {
2141a86b377SEli Cohen 	if (features & ~VALID_FEATURES_MASK)
2151a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "There are invalid feature bits 0x%llx\n",
2161a86b377SEli Cohen 			       features & ~VALID_FEATURES_MASK);
2171a86b377SEli Cohen 
2181a86b377SEli Cohen 	if (!mlx5_vdpa_debug)
2191a86b377SEli Cohen 		return;
2201a86b377SEli Cohen 
2211a86b377SEli Cohen 	mlx5_vdpa_info(mvdev, "driver %s feature bits:\n", set ? "sets" : "reads");
2221a86b377SEli Cohen 	if (!features)
2231a86b377SEli Cohen 		mlx5_vdpa_info(mvdev, "all feature bits are cleared\n");
2241a86b377SEli Cohen 
2251a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CSUM);
2261a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_CSUM);
2271a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2281a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MTU);
2291a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MAC);
2301a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_TSO4);
2311a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_TSO6);
2321a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_ECN);
2331a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_UFO);
2341a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_TSO4);
2351a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_TSO6);
2361a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_ECN);
2371a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_UFO);
2381a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MRG_RXBUF);
2391a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_STATUS);
2401a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_VQ);
2411a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_RX);
2421a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_VLAN);
2431a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_RX_EXTRA);
2441a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_ANNOUNCE);
2451a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MQ);
2461a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_MAC_ADDR);
2471a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HASH_REPORT);
2481a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_RSS);
2491a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_RSC_EXT);
2501a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_STANDBY);
2511a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_SPEED_DUPLEX);
2521a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_NOTIFY_ON_EMPTY);
2531a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_ANY_LAYOUT);
2541a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_VERSION_1);
2551a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_ACCESS_PLATFORM);
2561a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_RING_PACKED);
2571a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_ORDER_PLATFORM);
2581a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_SR_IOV);
2591a86b377SEli Cohen }
2601a86b377SEli Cohen 
create_tis(struct mlx5_vdpa_net * ndev)2611a86b377SEli Cohen static int create_tis(struct mlx5_vdpa_net *ndev)
2621a86b377SEli Cohen {
2631a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = &ndev->mvdev;
2641a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
2651a86b377SEli Cohen 	void *tisc;
2661a86b377SEli Cohen 	int err;
2671a86b377SEli Cohen 
2681a86b377SEli Cohen 	tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2691a86b377SEli Cohen 	MLX5_SET(tisc, tisc, transport_domain, ndev->res.tdn);
2701a86b377SEli Cohen 	err = mlx5_vdpa_create_tis(mvdev, in, &ndev->res.tisn);
2711a86b377SEli Cohen 	if (err)
2721a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "create TIS (%d)\n", err);
2731a86b377SEli Cohen 
2741a86b377SEli Cohen 	return err;
2751a86b377SEli Cohen }
2761a86b377SEli Cohen 
destroy_tis(struct mlx5_vdpa_net * ndev)2771a86b377SEli Cohen static void destroy_tis(struct mlx5_vdpa_net *ndev)
2781a86b377SEli Cohen {
2791a86b377SEli Cohen 	mlx5_vdpa_destroy_tis(&ndev->mvdev, ndev->res.tisn);
2801a86b377SEli Cohen }
2811a86b377SEli Cohen 
2821a86b377SEli Cohen #define MLX5_VDPA_CQE_SIZE 64
2831a86b377SEli Cohen #define MLX5_VDPA_LOG_CQE_SIZE ilog2(MLX5_VDPA_CQE_SIZE)
2841a86b377SEli Cohen 
cq_frag_buf_alloc(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_cq_buf * buf,int nent)2851a86b377SEli Cohen static int cq_frag_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_cq_buf *buf, int nent)
2861a86b377SEli Cohen {
2871a86b377SEli Cohen 	struct mlx5_frag_buf *frag_buf = &buf->frag_buf;
2881a86b377SEli Cohen 	u8 log_wq_stride = MLX5_VDPA_LOG_CQE_SIZE;
2891a86b377SEli Cohen 	u8 log_wq_sz = MLX5_VDPA_LOG_CQE_SIZE;
2901a86b377SEli Cohen 	int err;
2911a86b377SEli Cohen 
2921a86b377SEli Cohen 	err = mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, nent * MLX5_VDPA_CQE_SIZE, frag_buf,
2931a86b377SEli Cohen 				       ndev->mvdev.mdev->priv.numa_node);
2941a86b377SEli Cohen 	if (err)
2951a86b377SEli Cohen 		return err;
2961a86b377SEli Cohen 
2971a86b377SEli Cohen 	mlx5_init_fbc(frag_buf->frags, log_wq_stride, log_wq_sz, &buf->fbc);
2981a86b377SEli Cohen 
2991a86b377SEli Cohen 	buf->cqe_size = MLX5_VDPA_CQE_SIZE;
3001a86b377SEli Cohen 	buf->nent = nent;
3011a86b377SEli Cohen 
3021a86b377SEli Cohen 	return 0;
3031a86b377SEli Cohen }
3041a86b377SEli Cohen 
umem_frag_buf_alloc(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_umem * umem,int size)3051a86b377SEli Cohen static int umem_frag_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_umem *umem, int size)
3061a86b377SEli Cohen {
3071a86b377SEli Cohen 	struct mlx5_frag_buf *frag_buf = &umem->frag_buf;
3081a86b377SEli Cohen 
3091a86b377SEli Cohen 	return mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, size, frag_buf,
3101a86b377SEli Cohen 					ndev->mvdev.mdev->priv.numa_node);
3111a86b377SEli Cohen }
3121a86b377SEli Cohen 
cq_frag_buf_free(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_cq_buf * buf)3131a86b377SEli Cohen static void cq_frag_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_cq_buf *buf)
3141a86b377SEli Cohen {
3151a86b377SEli Cohen 	mlx5_frag_buf_free(ndev->mvdev.mdev, &buf->frag_buf);
3161a86b377SEli Cohen }
3171a86b377SEli Cohen 
get_cqe(struct mlx5_vdpa_cq * vcq,int n)3181a86b377SEli Cohen static void *get_cqe(struct mlx5_vdpa_cq *vcq, int n)
3191a86b377SEli Cohen {
3201a86b377SEli Cohen 	return mlx5_frag_buf_get_wqe(&vcq->buf.fbc, n);
3211a86b377SEli Cohen }
3221a86b377SEli Cohen 
cq_frag_buf_init(struct mlx5_vdpa_cq * vcq,struct mlx5_vdpa_cq_buf * buf)3231a86b377SEli Cohen static void cq_frag_buf_init(struct mlx5_vdpa_cq *vcq, struct mlx5_vdpa_cq_buf *buf)
3241a86b377SEli Cohen {
3251a86b377SEli Cohen 	struct mlx5_cqe64 *cqe64;
3261a86b377SEli Cohen 	void *cqe;
3271a86b377SEli Cohen 	int i;
3281a86b377SEli Cohen 
3291a86b377SEli Cohen 	for (i = 0; i < buf->nent; i++) {
3301a86b377SEli Cohen 		cqe = get_cqe(vcq, i);
3311a86b377SEli Cohen 		cqe64 = cqe;
3321a86b377SEli Cohen 		cqe64->op_own = MLX5_CQE_INVALID << 4;
3331a86b377SEli Cohen 	}
3341a86b377SEli Cohen }
3351a86b377SEli Cohen 
get_sw_cqe(struct mlx5_vdpa_cq * cq,int n)3361a86b377SEli Cohen static void *get_sw_cqe(struct mlx5_vdpa_cq *cq, int n)
3371a86b377SEli Cohen {
3381a86b377SEli Cohen 	struct mlx5_cqe64 *cqe64 = get_cqe(cq, n & (cq->cqe - 1));
3391a86b377SEli Cohen 
3401a86b377SEli Cohen 	if (likely(get_cqe_opcode(cqe64) != MLX5_CQE_INVALID) &&
3411a86b377SEli Cohen 	    !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & cq->cqe)))
3421a86b377SEli Cohen 		return cqe64;
3431a86b377SEli Cohen 
3441a86b377SEli Cohen 	return NULL;
3451a86b377SEli Cohen }
3461a86b377SEli Cohen 
rx_post(struct mlx5_vdpa_qp * vqp,int n)3471a86b377SEli Cohen static void rx_post(struct mlx5_vdpa_qp *vqp, int n)
3481a86b377SEli Cohen {
3491a86b377SEli Cohen 	vqp->head += n;
3501a86b377SEli Cohen 	vqp->db.db[0] = cpu_to_be32(vqp->head);
3511a86b377SEli Cohen }
3521a86b377SEli Cohen 
qp_prepare(struct mlx5_vdpa_net * ndev,bool fw,void * in,struct mlx5_vdpa_virtqueue * mvq,u32 num_ent)3531a86b377SEli Cohen static void qp_prepare(struct mlx5_vdpa_net *ndev, bool fw, void *in,
3541a86b377SEli Cohen 		       struct mlx5_vdpa_virtqueue *mvq, u32 num_ent)
3551a86b377SEli Cohen {
3561a86b377SEli Cohen 	struct mlx5_vdpa_qp *vqp;
3571a86b377SEli Cohen 	__be64 *pas;
3581a86b377SEli Cohen 	void *qpc;
3591a86b377SEli Cohen 
3601a86b377SEli Cohen 	vqp = fw ? &mvq->fwqp : &mvq->vqqp;
3611a86b377SEli Cohen 	MLX5_SET(create_qp_in, in, uid, ndev->mvdev.res.uid);
3621a86b377SEli Cohen 	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
3631a86b377SEli Cohen 	if (vqp->fw) {
3641a86b377SEli Cohen 		/* Firmware QP is allocated by the driver for the firmware's
3651a86b377SEli Cohen 		 * use so we can skip part of the params as they will be chosen by firmware
3661a86b377SEli Cohen 		 */
3671a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
3681a86b377SEli Cohen 		MLX5_SET(qpc, qpc, rq_type, MLX5_ZERO_LEN_RQ);
3691a86b377SEli Cohen 		MLX5_SET(qpc, qpc, no_sq, 1);
3701a86b377SEli Cohen 		return;
3711a86b377SEli Cohen 	}
3721a86b377SEli Cohen 
3731a86b377SEli Cohen 	MLX5_SET(qpc, qpc, st, MLX5_QP_ST_RC);
3741a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
3751a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pd, ndev->mvdev.res.pdn);
3761a86b377SEli Cohen 	MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES);
3771a86b377SEli Cohen 	MLX5_SET(qpc, qpc, uar_page, ndev->mvdev.res.uar->index);
3781a86b377SEli Cohen 	MLX5_SET(qpc, qpc, log_page_size, vqp->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
3791a86b377SEli Cohen 	MLX5_SET(qpc, qpc, no_sq, 1);
3801a86b377SEli Cohen 	MLX5_SET(qpc, qpc, cqn_rcv, mvq->cq.mcq.cqn);
3811a86b377SEli Cohen 	MLX5_SET(qpc, qpc, log_rq_size, ilog2(num_ent));
3821a86b377SEli Cohen 	MLX5_SET(qpc, qpc, rq_type, MLX5_NON_ZERO_RQ);
3831a86b377SEli Cohen 	pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, in, pas);
3841a86b377SEli Cohen 	mlx5_fill_page_frag_array(&vqp->frag_buf, pas);
3851a86b377SEli Cohen }
3861a86b377SEli Cohen 
rq_buf_alloc(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_qp * vqp,u32 num_ent)3871a86b377SEli Cohen static int rq_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp, u32 num_ent)
3881a86b377SEli Cohen {
3891a86b377SEli Cohen 	return mlx5_frag_buf_alloc_node(ndev->mvdev.mdev,
3901a86b377SEli Cohen 					num_ent * sizeof(struct mlx5_wqe_data_seg), &vqp->frag_buf,
3911a86b377SEli Cohen 					ndev->mvdev.mdev->priv.numa_node);
3921a86b377SEli Cohen }
3931a86b377SEli Cohen 
rq_buf_free(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_qp * vqp)3941a86b377SEli Cohen static void rq_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp)
3951a86b377SEli Cohen {
3961a86b377SEli Cohen 	mlx5_frag_buf_free(ndev->mvdev.mdev, &vqp->frag_buf);
3971a86b377SEli Cohen }
3981a86b377SEli Cohen 
qp_create(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,struct mlx5_vdpa_qp * vqp)3991a86b377SEli Cohen static int qp_create(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq,
4001a86b377SEli Cohen 		     struct mlx5_vdpa_qp *vqp)
4011a86b377SEli Cohen {
4021a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
4031a86b377SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
4041a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
4051a86b377SEli Cohen 	void *qpc;
4061a86b377SEli Cohen 	void *in;
4071a86b377SEli Cohen 	int err;
4081a86b377SEli Cohen 
4091a86b377SEli Cohen 	if (!vqp->fw) {
4101a86b377SEli Cohen 		vqp = &mvq->vqqp;
4111a86b377SEli Cohen 		err = rq_buf_alloc(ndev, vqp, mvq->num_ent);
4121a86b377SEli Cohen 		if (err)
4131a86b377SEli Cohen 			return err;
4141a86b377SEli Cohen 
4151a86b377SEli Cohen 		err = mlx5_db_alloc(ndev->mvdev.mdev, &vqp->db);
4161a86b377SEli Cohen 		if (err)
4171a86b377SEli Cohen 			goto err_db;
4181a86b377SEli Cohen 		inlen += vqp->frag_buf.npages * sizeof(__be64);
4191a86b377SEli Cohen 	}
4201a86b377SEli Cohen 
4211a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
4221a86b377SEli Cohen 	if (!in) {
4231a86b377SEli Cohen 		err = -ENOMEM;
4241a86b377SEli Cohen 		goto err_kzalloc;
4251a86b377SEli Cohen 	}
4261a86b377SEli Cohen 
4271a86b377SEli Cohen 	qp_prepare(ndev, vqp->fw, in, mvq, mvq->num_ent);
4281a86b377SEli Cohen 	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
4291a86b377SEli Cohen 	MLX5_SET(qpc, qpc, st, MLX5_QP_ST_RC);
4301a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
4311a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pd, ndev->mvdev.res.pdn);
4321a86b377SEli Cohen 	MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES);
4331a86b377SEli Cohen 	if (!vqp->fw)
4341a86b377SEli Cohen 		MLX5_SET64(qpc, qpc, dbr_addr, vqp->db.dma);
4351a86b377SEli Cohen 	MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP);
4361a86b377SEli Cohen 	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
4371a86b377SEli Cohen 	kfree(in);
4381a86b377SEli Cohen 	if (err)
4391a86b377SEli Cohen 		goto err_kzalloc;
4401a86b377SEli Cohen 
4411a86b377SEli Cohen 	vqp->mqp.uid = ndev->mvdev.res.uid;
4421a86b377SEli Cohen 	vqp->mqp.qpn = MLX5_GET(create_qp_out, out, qpn);
4431a86b377SEli Cohen 
4441a86b377SEli Cohen 	if (!vqp->fw)
4451a86b377SEli Cohen 		rx_post(vqp, mvq->num_ent);
4461a86b377SEli Cohen 
4471a86b377SEli Cohen 	return 0;
4481a86b377SEli Cohen 
4491a86b377SEli Cohen err_kzalloc:
4501a86b377SEli Cohen 	if (!vqp->fw)
4511a86b377SEli Cohen 		mlx5_db_free(ndev->mvdev.mdev, &vqp->db);
4521a86b377SEli Cohen err_db:
4531a86b377SEli Cohen 	if (!vqp->fw)
4541a86b377SEli Cohen 		rq_buf_free(ndev, vqp);
4551a86b377SEli Cohen 
4561a86b377SEli Cohen 	return err;
4571a86b377SEli Cohen }
4581a86b377SEli Cohen 
qp_destroy(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_qp * vqp)4591a86b377SEli Cohen static void qp_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp)
4601a86b377SEli Cohen {
4611a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {};
4621a86b377SEli Cohen 
4631a86b377SEli Cohen 	MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
4641a86b377SEli Cohen 	MLX5_SET(destroy_qp_in, in, qpn, vqp->mqp.qpn);
4651a86b377SEli Cohen 	MLX5_SET(destroy_qp_in, in, uid, ndev->mvdev.res.uid);
4661a86b377SEli Cohen 	if (mlx5_cmd_exec_in(ndev->mvdev.mdev, destroy_qp, in))
4671a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "destroy qp 0x%x\n", vqp->mqp.qpn);
4681a86b377SEli Cohen 	if (!vqp->fw) {
4691a86b377SEli Cohen 		mlx5_db_free(ndev->mvdev.mdev, &vqp->db);
4701a86b377SEli Cohen 		rq_buf_free(ndev, vqp);
4711a86b377SEli Cohen 	}
4721a86b377SEli Cohen }
4731a86b377SEli Cohen 
next_cqe_sw(struct mlx5_vdpa_cq * cq)4741a86b377SEli Cohen static void *next_cqe_sw(struct mlx5_vdpa_cq *cq)
4751a86b377SEli Cohen {
4761a86b377SEli Cohen 	return get_sw_cqe(cq, cq->mcq.cons_index);
4771a86b377SEli Cohen }
4781a86b377SEli Cohen 
mlx5_vdpa_poll_one(struct mlx5_vdpa_cq * vcq)4791a86b377SEli Cohen static int mlx5_vdpa_poll_one(struct mlx5_vdpa_cq *vcq)
4801a86b377SEli Cohen {
4811a86b377SEli Cohen 	struct mlx5_cqe64 *cqe64;
4821a86b377SEli Cohen 
4831a86b377SEli Cohen 	cqe64 = next_cqe_sw(vcq);
4841a86b377SEli Cohen 	if (!cqe64)
4851a86b377SEli Cohen 		return -EAGAIN;
4861a86b377SEli Cohen 
4871a86b377SEli Cohen 	vcq->mcq.cons_index++;
4881a86b377SEli Cohen 	return 0;
4891a86b377SEli Cohen }
4901a86b377SEli Cohen 
mlx5_vdpa_handle_completions(struct mlx5_vdpa_virtqueue * mvq,int num)4911a86b377SEli Cohen static void mlx5_vdpa_handle_completions(struct mlx5_vdpa_virtqueue *mvq, int num)
4921a86b377SEli Cohen {
493db296d25SEli Cohen 	struct mlx5_vdpa_net *ndev = mvq->ndev;
494db296d25SEli Cohen 	struct vdpa_callback *event_cb;
495db296d25SEli Cohen 
496db296d25SEli Cohen 	event_cb = &ndev->event_cbs[mvq->index];
4971a86b377SEli Cohen 	mlx5_cq_set_ci(&mvq->cq.mcq);
49883ef73b2SEli Cohen 
49983ef73b2SEli Cohen 	/* make sure CQ cosumer update is visible to the hardware before updating
50083ef73b2SEli Cohen 	 * RX doorbell record.
50183ef73b2SEli Cohen 	 */
50283ef73b2SEli Cohen 	dma_wmb();
5031a86b377SEli Cohen 	rx_post(&mvq->vqqp, num);
504db296d25SEli Cohen 	if (event_cb->callback)
505db296d25SEli Cohen 		event_cb->callback(event_cb->private);
5061a86b377SEli Cohen }
5071a86b377SEli Cohen 
mlx5_vdpa_cq_comp(struct mlx5_core_cq * mcq,struct mlx5_eqe * eqe)5081a86b377SEli Cohen static void mlx5_vdpa_cq_comp(struct mlx5_core_cq *mcq, struct mlx5_eqe *eqe)
5091a86b377SEli Cohen {
5101a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq = container_of(mcq, struct mlx5_vdpa_virtqueue, cq.mcq);
5111a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = mvq->ndev;
5121a86b377SEli Cohen 	void __iomem *uar_page = ndev->mvdev.res.uar->map;
5131a86b377SEli Cohen 	int num = 0;
5141a86b377SEli Cohen 
5151a86b377SEli Cohen 	while (!mlx5_vdpa_poll_one(&mvq->cq)) {
5161a86b377SEli Cohen 		num++;
5171a86b377SEli Cohen 		if (num > mvq->num_ent / 2) {
5181a86b377SEli Cohen 			/* If completions keep coming while we poll, we want to
5191a86b377SEli Cohen 			 * let the hardware know that we consumed them by
5201a86b377SEli Cohen 			 * updating the doorbell record.  We also let vdpa core
5211a86b377SEli Cohen 			 * know about this so it passes it on the virtio driver
5221a86b377SEli Cohen 			 * on the guest.
5231a86b377SEli Cohen 			 */
5241a86b377SEli Cohen 			mlx5_vdpa_handle_completions(mvq, num);
5251a86b377SEli Cohen 			num = 0;
5261a86b377SEli Cohen 		}
5271a86b377SEli Cohen 	}
5281a86b377SEli Cohen 
5291a86b377SEli Cohen 	if (num)
5301a86b377SEli Cohen 		mlx5_vdpa_handle_completions(mvq, num);
5311a86b377SEli Cohen 
5321a86b377SEli Cohen 	mlx5_cq_arm(&mvq->cq.mcq, MLX5_CQ_DB_REQ_NOT, uar_page, mvq->cq.mcq.cons_index);
5331a86b377SEli Cohen }
5341a86b377SEli Cohen 
cq_create(struct mlx5_vdpa_net * ndev,u16 idx,u32 num_ent)5351a86b377SEli Cohen static int cq_create(struct mlx5_vdpa_net *ndev, u16 idx, u32 num_ent)
5361a86b377SEli Cohen {
5371a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx];
5381a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
5391a86b377SEli Cohen 	void __iomem *uar_page = ndev->mvdev.res.uar->map;
5401a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_cq_out)];
5411a86b377SEli Cohen 	struct mlx5_vdpa_cq *vcq = &mvq->cq;
5421a86b377SEli Cohen 	__be64 *pas;
5431a86b377SEli Cohen 	int inlen;
5441a86b377SEli Cohen 	void *cqc;
5451a86b377SEli Cohen 	void *in;
5461a86b377SEli Cohen 	int err;
5471a86b377SEli Cohen 	int eqn;
5481a86b377SEli Cohen 
5491a86b377SEli Cohen 	err = mlx5_db_alloc(mdev, &vcq->db);
5501a86b377SEli Cohen 	if (err)
5511a86b377SEli Cohen 		return err;
5521a86b377SEli Cohen 
5531a86b377SEli Cohen 	vcq->mcq.set_ci_db = vcq->db.db;
5541a86b377SEli Cohen 	vcq->mcq.arm_db = vcq->db.db + 1;
5551a86b377SEli Cohen 	vcq->mcq.cqe_sz = 64;
5561a86b377SEli Cohen 
5571a86b377SEli Cohen 	err = cq_frag_buf_alloc(ndev, &vcq->buf, num_ent);
5581a86b377SEli Cohen 	if (err)
5591a86b377SEli Cohen 		goto err_db;
5601a86b377SEli Cohen 
5611a86b377SEli Cohen 	cq_frag_buf_init(vcq, &vcq->buf);
5621a86b377SEli Cohen 
5631a86b377SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
5641a86b377SEli Cohen 		MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * vcq->buf.frag_buf.npages;
5651a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
5661a86b377SEli Cohen 	if (!in) {
5671a86b377SEli Cohen 		err = -ENOMEM;
5681a86b377SEli Cohen 		goto err_vzalloc;
5691a86b377SEli Cohen 	}
5701a86b377SEli Cohen 
5711a86b377SEli Cohen 	MLX5_SET(create_cq_in, in, uid, ndev->mvdev.res.uid);
5721a86b377SEli Cohen 	pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas);
5731a86b377SEli Cohen 	mlx5_fill_page_frag_array(&vcq->buf.frag_buf, pas);
5741a86b377SEli Cohen 
5751a86b377SEli Cohen 	cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
5761a86b377SEli Cohen 	MLX5_SET(cqc, cqc, log_page_size, vcq->buf.frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
5771a86b377SEli Cohen 
5781a86b377SEli Cohen 	/* Use vector 0 by default. Consider adding code to choose least used
5791a86b377SEli Cohen 	 * vector.
5801a86b377SEli Cohen 	 */
581f14c1a14SMaher Sanalla 	err = mlx5_comp_eqn_get(mdev, 0, &eqn);
5821a86b377SEli Cohen 	if (err)
5831a86b377SEli Cohen 		goto err_vec;
5841a86b377SEli Cohen 
5851a86b377SEli Cohen 	cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
5861a86b377SEli Cohen 	MLX5_SET(cqc, cqc, log_cq_size, ilog2(num_ent));
5871a86b377SEli Cohen 	MLX5_SET(cqc, cqc, uar_page, ndev->mvdev.res.uar->index);
588616d5769STal Gilboa 	MLX5_SET(cqc, cqc, c_eqn_or_apu_element, eqn);
5891a86b377SEli Cohen 	MLX5_SET64(cqc, cqc, dbr_addr, vcq->db.dma);
5901a86b377SEli Cohen 
5911a86b377SEli Cohen 	err = mlx5_core_create_cq(mdev, &vcq->mcq, in, inlen, out, sizeof(out));
5921a86b377SEli Cohen 	if (err)
5931a86b377SEli Cohen 		goto err_vec;
5941a86b377SEli Cohen 
5951a86b377SEli Cohen 	vcq->mcq.comp = mlx5_vdpa_cq_comp;
5961a86b377SEli Cohen 	vcq->cqe = num_ent;
5971a86b377SEli Cohen 	vcq->mcq.set_ci_db = vcq->db.db;
5981a86b377SEli Cohen 	vcq->mcq.arm_db = vcq->db.db + 1;
5991a86b377SEli Cohen 	mlx5_cq_arm(&mvq->cq.mcq, MLX5_CQ_DB_REQ_NOT, uar_page, mvq->cq.mcq.cons_index);
6001a86b377SEli Cohen 	kfree(in);
6011a86b377SEli Cohen 	return 0;
6021a86b377SEli Cohen 
6031a86b377SEli Cohen err_vec:
6041a86b377SEli Cohen 	kfree(in);
6051a86b377SEli Cohen err_vzalloc:
6061a86b377SEli Cohen 	cq_frag_buf_free(ndev, &vcq->buf);
6071a86b377SEli Cohen err_db:
6081a86b377SEli Cohen 	mlx5_db_free(ndev->mvdev.mdev, &vcq->db);
6091a86b377SEli Cohen 	return err;
6101a86b377SEli Cohen }
6111a86b377SEli Cohen 
cq_destroy(struct mlx5_vdpa_net * ndev,u16 idx)6121a86b377SEli Cohen static void cq_destroy(struct mlx5_vdpa_net *ndev, u16 idx)
6131a86b377SEli Cohen {
6141a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx];
6151a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
6161a86b377SEli Cohen 	struct mlx5_vdpa_cq *vcq = &mvq->cq;
6171a86b377SEli Cohen 
6181a86b377SEli Cohen 	if (mlx5_core_destroy_cq(mdev, &vcq->mcq)) {
6191a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "destroy CQ 0x%x\n", vcq->mcq.cqn);
6201a86b377SEli Cohen 		return;
6211a86b377SEli Cohen 	}
6221a86b377SEli Cohen 	cq_frag_buf_free(ndev, &vcq->buf);
6231a86b377SEli Cohen 	mlx5_db_free(ndev->mvdev.mdev, &vcq->db);
6241a86b377SEli Cohen }
6251a86b377SEli Cohen 
read_umem_params(struct mlx5_vdpa_net * ndev)626abb0dcf9SDragos Tatulea static int read_umem_params(struct mlx5_vdpa_net *ndev)
627abb0dcf9SDragos Tatulea {
628abb0dcf9SDragos Tatulea 	u32 in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {};
629abb0dcf9SDragos Tatulea 	u16 opmod = (MLX5_CAP_VDPA_EMULATION << 1) | (HCA_CAP_OPMOD_GET_CUR & 0x01);
630abb0dcf9SDragos Tatulea 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
631abb0dcf9SDragos Tatulea 	int out_size;
632abb0dcf9SDragos Tatulea 	void *caps;
633abb0dcf9SDragos Tatulea 	void *out;
634abb0dcf9SDragos Tatulea 	int err;
635abb0dcf9SDragos Tatulea 
636abb0dcf9SDragos Tatulea 	out_size = MLX5_ST_SZ_BYTES(query_hca_cap_out);
637abb0dcf9SDragos Tatulea 	out = kzalloc(out_size, GFP_KERNEL);
638abb0dcf9SDragos Tatulea 	if (!out)
639abb0dcf9SDragos Tatulea 		return -ENOMEM;
640abb0dcf9SDragos Tatulea 
641abb0dcf9SDragos Tatulea 	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
642abb0dcf9SDragos Tatulea 	MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
643abb0dcf9SDragos Tatulea 	err = mlx5_cmd_exec_inout(mdev, query_hca_cap, in, out);
644abb0dcf9SDragos Tatulea 	if (err) {
645abb0dcf9SDragos Tatulea 		mlx5_vdpa_warn(&ndev->mvdev,
646abb0dcf9SDragos Tatulea 			"Failed reading vdpa umem capabilities with err %d\n", err);
647abb0dcf9SDragos Tatulea 		goto out;
648abb0dcf9SDragos Tatulea 	}
649abb0dcf9SDragos Tatulea 
650abb0dcf9SDragos Tatulea 	caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
651abb0dcf9SDragos Tatulea 
652abb0dcf9SDragos Tatulea 	ndev->umem_1_buffer_param_a = MLX5_GET(virtio_emulation_cap, caps, umem_1_buffer_param_a);
653abb0dcf9SDragos Tatulea 	ndev->umem_1_buffer_param_b = MLX5_GET(virtio_emulation_cap, caps, umem_1_buffer_param_b);
654abb0dcf9SDragos Tatulea 
655abb0dcf9SDragos Tatulea 	ndev->umem_2_buffer_param_a = MLX5_GET(virtio_emulation_cap, caps, umem_2_buffer_param_a);
656abb0dcf9SDragos Tatulea 	ndev->umem_2_buffer_param_b = MLX5_GET(virtio_emulation_cap, caps, umem_2_buffer_param_b);
657abb0dcf9SDragos Tatulea 
658abb0dcf9SDragos Tatulea 	ndev->umem_3_buffer_param_a = MLX5_GET(virtio_emulation_cap, caps, umem_3_buffer_param_a);
659abb0dcf9SDragos Tatulea 	ndev->umem_3_buffer_param_b = MLX5_GET(virtio_emulation_cap, caps, umem_3_buffer_param_b);
660abb0dcf9SDragos Tatulea 
661abb0dcf9SDragos Tatulea out:
662abb0dcf9SDragos Tatulea 	kfree(out);
663abb0dcf9SDragos Tatulea 	return 0;
664abb0dcf9SDragos Tatulea }
665abb0dcf9SDragos Tatulea 
set_umem_size(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,int num,struct mlx5_vdpa_umem ** umemp)66671ab6a7cSEli Cohen static void set_umem_size(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num,
6671a86b377SEli Cohen 			  struct mlx5_vdpa_umem **umemp)
6681a86b377SEli Cohen {
669abb0dcf9SDragos Tatulea 	u32 p_a;
670abb0dcf9SDragos Tatulea 	u32 p_b;
6711a86b377SEli Cohen 
6721a86b377SEli Cohen 	switch (num) {
6731a86b377SEli Cohen 	case 1:
674abb0dcf9SDragos Tatulea 		p_a = ndev->umem_1_buffer_param_a;
675abb0dcf9SDragos Tatulea 		p_b = ndev->umem_1_buffer_param_b;
6761a86b377SEli Cohen 		*umemp = &mvq->umem1;
6771a86b377SEli Cohen 		break;
6781a86b377SEli Cohen 	case 2:
679abb0dcf9SDragos Tatulea 		p_a = ndev->umem_2_buffer_param_a;
680abb0dcf9SDragos Tatulea 		p_b = ndev->umem_2_buffer_param_b;
6811a86b377SEli Cohen 		*umemp = &mvq->umem2;
6821a86b377SEli Cohen 		break;
6831a86b377SEli Cohen 	case 3:
684abb0dcf9SDragos Tatulea 		p_a = ndev->umem_3_buffer_param_a;
685abb0dcf9SDragos Tatulea 		p_b = ndev->umem_3_buffer_param_b;
6861a86b377SEli Cohen 		*umemp = &mvq->umem3;
6871a86b377SEli Cohen 		break;
6881a86b377SEli Cohen 	}
689abb0dcf9SDragos Tatulea 
69071ab6a7cSEli Cohen 	(*umemp)->size = p_a * mvq->num_ent + p_b;
6911a86b377SEli Cohen }
6921a86b377SEli Cohen 
umem_frag_buf_free(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_umem * umem)6931a86b377SEli Cohen static void umem_frag_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_umem *umem)
6941a86b377SEli Cohen {
6951a86b377SEli Cohen 	mlx5_frag_buf_free(ndev->mvdev.mdev, &umem->frag_buf);
6961a86b377SEli Cohen }
6971a86b377SEli Cohen 
create_umem(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,int num)6981a86b377SEli Cohen static int create_umem(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num)
6991a86b377SEli Cohen {
7001a86b377SEli Cohen 	int inlen;
7011a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_umem_out)] = {};
7021a86b377SEli Cohen 	void *um;
7031a86b377SEli Cohen 	void *in;
7041a86b377SEli Cohen 	int err;
7051a86b377SEli Cohen 	__be64 *pas;
7061a86b377SEli Cohen 	struct mlx5_vdpa_umem *umem;
7071a86b377SEli Cohen 
70871ab6a7cSEli Cohen 	set_umem_size(ndev, mvq, num, &umem);
70971ab6a7cSEli Cohen 	err = umem_frag_buf_alloc(ndev, umem, umem->size);
7101a86b377SEli Cohen 	if (err)
7111a86b377SEli Cohen 		return err;
7121a86b377SEli Cohen 
7131a86b377SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(create_umem_in) + MLX5_ST_SZ_BYTES(mtt) * umem->frag_buf.npages;
7141a86b377SEli Cohen 
7151a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
7161a86b377SEli Cohen 	if (!in) {
7171a86b377SEli Cohen 		err = -ENOMEM;
7181a86b377SEli Cohen 		goto err_in;
7191a86b377SEli Cohen 	}
7201a86b377SEli Cohen 
7211a86b377SEli Cohen 	MLX5_SET(create_umem_in, in, opcode, MLX5_CMD_OP_CREATE_UMEM);
7221a86b377SEli Cohen 	MLX5_SET(create_umem_in, in, uid, ndev->mvdev.res.uid);
7231a86b377SEli Cohen 	um = MLX5_ADDR_OF(create_umem_in, in, umem);
7241a86b377SEli Cohen 	MLX5_SET(umem, um, log_page_size, umem->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
7251a86b377SEli Cohen 	MLX5_SET64(umem, um, num_of_mtt, umem->frag_buf.npages);
7261a86b377SEli Cohen 
7271a86b377SEli Cohen 	pas = (__be64 *)MLX5_ADDR_OF(umem, um, mtt[0]);
7281a86b377SEli Cohen 	mlx5_fill_page_frag_array_perm(&umem->frag_buf, pas, MLX5_MTT_PERM_RW);
7291a86b377SEli Cohen 
7301a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out));
7311a86b377SEli Cohen 	if (err) {
7321a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "create umem(%d)\n", err);
7331a86b377SEli Cohen 		goto err_cmd;
7341a86b377SEli Cohen 	}
7351a86b377SEli Cohen 
7361a86b377SEli Cohen 	kfree(in);
7371a86b377SEli Cohen 	umem->id = MLX5_GET(create_umem_out, out, umem_id);
7381a86b377SEli Cohen 
7391a86b377SEli Cohen 	return 0;
7401a86b377SEli Cohen 
7411a86b377SEli Cohen err_cmd:
7421a86b377SEli Cohen 	kfree(in);
7431a86b377SEli Cohen err_in:
7441a86b377SEli Cohen 	umem_frag_buf_free(ndev, umem);
7451a86b377SEli Cohen 	return err;
7461a86b377SEli Cohen }
7471a86b377SEli Cohen 
umem_destroy(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,int num)7481a86b377SEli Cohen static void umem_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num)
7491a86b377SEli Cohen {
7501a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_umem_in)] = {};
7511a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(destroy_umem_out)] = {};
7521a86b377SEli Cohen 	struct mlx5_vdpa_umem *umem;
7531a86b377SEli Cohen 
7541a86b377SEli Cohen 	switch (num) {
7551a86b377SEli Cohen 	case 1:
7561a86b377SEli Cohen 		umem = &mvq->umem1;
7571a86b377SEli Cohen 		break;
7581a86b377SEli Cohen 	case 2:
7591a86b377SEli Cohen 		umem = &mvq->umem2;
7601a86b377SEli Cohen 		break;
7611a86b377SEli Cohen 	case 3:
7621a86b377SEli Cohen 		umem = &mvq->umem3;
7631a86b377SEli Cohen 		break;
7641a86b377SEli Cohen 	}
7651a86b377SEli Cohen 
7661a86b377SEli Cohen 	MLX5_SET(destroy_umem_in, in, opcode, MLX5_CMD_OP_DESTROY_UMEM);
7671a86b377SEli Cohen 	MLX5_SET(destroy_umem_in, in, umem_id, umem->id);
7681a86b377SEli Cohen 	if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out)))
7691a86b377SEli Cohen 		return;
7701a86b377SEli Cohen 
7711a86b377SEli Cohen 	umem_frag_buf_free(ndev, umem);
7721a86b377SEli Cohen }
7731a86b377SEli Cohen 
umems_create(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)7741a86b377SEli Cohen static int umems_create(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
7751a86b377SEli Cohen {
7761a86b377SEli Cohen 	int num;
7771a86b377SEli Cohen 	int err;
7781a86b377SEli Cohen 
7791a86b377SEli Cohen 	for (num = 1; num <= 3; num++) {
7801a86b377SEli Cohen 		err = create_umem(ndev, mvq, num);
7811a86b377SEli Cohen 		if (err)
7821a86b377SEli Cohen 			goto err_umem;
7831a86b377SEli Cohen 	}
7841a86b377SEli Cohen 	return 0;
7851a86b377SEli Cohen 
7861a86b377SEli Cohen err_umem:
7871a86b377SEli Cohen 	for (num--; num > 0; num--)
7881a86b377SEli Cohen 		umem_destroy(ndev, mvq, num);
7891a86b377SEli Cohen 
7901a86b377SEli Cohen 	return err;
7911a86b377SEli Cohen }
7921a86b377SEli Cohen 
umems_destroy(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)7931a86b377SEli Cohen static void umems_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
7941a86b377SEli Cohen {
7951a86b377SEli Cohen 	int num;
7961a86b377SEli Cohen 
7971a86b377SEli Cohen 	for (num = 3; num > 0; num--)
7981a86b377SEli Cohen 		umem_destroy(ndev, mvq, num);
7991a86b377SEli Cohen }
8001a86b377SEli Cohen 
get_queue_type(struct mlx5_vdpa_net * ndev)8011a86b377SEli Cohen static int get_queue_type(struct mlx5_vdpa_net *ndev)
8021a86b377SEli Cohen {
8031a86b377SEli Cohen 	u32 type_mask;
8041a86b377SEli Cohen 
8051a86b377SEli Cohen 	type_mask = MLX5_CAP_DEV_VDPA_EMULATION(ndev->mvdev.mdev, virtio_queue_type);
8061a86b377SEli Cohen 
8071a86b377SEli Cohen 	/* prefer split queue */
808879753c8SEli Cohen 	if (type_mask & MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_SPLIT)
8091a86b377SEli Cohen 		return MLX5_VIRTIO_EMULATION_VIRTIO_QUEUE_TYPE_SPLIT;
810879753c8SEli Cohen 
811879753c8SEli Cohen 	WARN_ON(!(type_mask & MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_PACKED));
812879753c8SEli Cohen 
813879753c8SEli Cohen 	return MLX5_VIRTIO_EMULATION_VIRTIO_QUEUE_TYPE_PACKED;
8141a86b377SEli Cohen }
8151a86b377SEli Cohen 
vq_is_tx(u16 idx)8161a86b377SEli Cohen static bool vq_is_tx(u16 idx)
8171a86b377SEli Cohen {
8181a86b377SEli Cohen 	return idx % 2;
8191a86b377SEli Cohen }
8201a86b377SEli Cohen 
821e9d67e59SEli Cohen enum {
822e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_MRG_RXBUF = 2,
823e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_HOST_ECN = 4,
824e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_GUEST_ECN = 6,
825e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_GUEST_TSO6 = 7,
826e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_GUEST_TSO4 = 8,
827e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_GUEST_CSUM = 9,
828e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_CSUM = 10,
829e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_HOST_TSO6 = 11,
830e9d67e59SEli Cohen 	MLX5_VIRTIO_NET_F_HOST_TSO4 = 12,
831e9d67e59SEli Cohen };
832e9d67e59SEli Cohen 
get_features(u64 features)833e9d67e59SEli Cohen static u16 get_features(u64 features)
8341a86b377SEli Cohen {
835e9d67e59SEli Cohen 	return (!!(features & BIT_ULL(VIRTIO_NET_F_MRG_RXBUF)) << MLX5_VIRTIO_NET_F_MRG_RXBUF) |
836e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_ECN)) << MLX5_VIRTIO_NET_F_HOST_ECN) |
837e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_GUEST_ECN)) << MLX5_VIRTIO_NET_F_GUEST_ECN) |
838e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_GUEST_TSO6)) << MLX5_VIRTIO_NET_F_GUEST_TSO6) |
839e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_GUEST_TSO4)) << MLX5_VIRTIO_NET_F_GUEST_TSO4) |
840e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_CSUM)) << MLX5_VIRTIO_NET_F_CSUM) |
841e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_TSO6)) << MLX5_VIRTIO_NET_F_HOST_TSO6) |
842e9d67e59SEli Cohen 	       (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_TSO4)) << MLX5_VIRTIO_NET_F_HOST_TSO4);
8431a86b377SEli Cohen }
8441a86b377SEli Cohen 
counters_supported(const struct mlx5_vdpa_dev * mvdev)8451892a3d4SEli Cohen static bool counters_supported(const struct mlx5_vdpa_dev *mvdev)
8461892a3d4SEli Cohen {
8471892a3d4SEli Cohen 	return MLX5_CAP_GEN_64(mvdev->mdev, general_obj_types) &
8481892a3d4SEli Cohen 	       BIT_ULL(MLX5_OBJ_TYPE_VIRTIO_Q_COUNTERS);
8491892a3d4SEli Cohen }
8501892a3d4SEli Cohen 
msix_mode_supported(struct mlx5_vdpa_dev * mvdev)851bc9a2b3eSEli Cohen static bool msix_mode_supported(struct mlx5_vdpa_dev *mvdev)
852bc9a2b3eSEli Cohen {
853bc9a2b3eSEli Cohen 	return MLX5_CAP_DEV_VDPA_EMULATION(mvdev->mdev, event_mode) &
854bc9a2b3eSEli Cohen 		(1 << MLX5_VIRTIO_Q_EVENT_MODE_MSIX_MODE) &&
855bc9a2b3eSEli Cohen 		pci_msix_can_alloc_dyn(mvdev->mdev->pdev);
856bc9a2b3eSEli Cohen }
857bc9a2b3eSEli Cohen 
create_virtqueue(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)8581a86b377SEli Cohen static int create_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
8591a86b377SEli Cohen {
8601a86b377SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(create_virtio_net_q_in);
8611a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_virtio_net_q_out)] = {};
8621a86b377SEli Cohen 	void *obj_context;
863e9d67e59SEli Cohen 	u16 mlx_features;
8641a86b377SEli Cohen 	void *cmd_hdr;
8651a86b377SEli Cohen 	void *vq_ctx;
8661a86b377SEli Cohen 	void *in;
8671a86b377SEli Cohen 	int err;
8681a86b377SEli Cohen 
8691a86b377SEli Cohen 	err = umems_create(ndev, mvq);
8701a86b377SEli Cohen 	if (err)
8711a86b377SEli Cohen 		return err;
8721a86b377SEli Cohen 
8731a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
8741a86b377SEli Cohen 	if (!in) {
8751a86b377SEli Cohen 		err = -ENOMEM;
8761a86b377SEli Cohen 		goto err_alloc;
8771a86b377SEli Cohen 	}
8781a86b377SEli Cohen 
879e9d67e59SEli Cohen 	mlx_features = get_features(ndev->mvdev.actual_features);
8801a86b377SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(create_virtio_net_q_in, in, general_obj_in_cmd_hdr);
8811a86b377SEli Cohen 
8821a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
8831a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q);
8841a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
8851a86b377SEli Cohen 
8861a86b377SEli Cohen 	obj_context = MLX5_ADDR_OF(create_virtio_net_q_in, in, obj_context);
8871a86b377SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, hw_available_index, mvq->avail_idx);
888b35ccebeSEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, hw_used_index, mvq->used_idx);
8891a86b377SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, queue_feature_bit_mask_12_3,
890e9d67e59SEli Cohen 		 mlx_features >> 3);
891e9d67e59SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, queue_feature_bit_mask_2_0,
892e9d67e59SEli Cohen 		 mlx_features & 7);
8931a86b377SEli Cohen 	vq_ctx = MLX5_ADDR_OF(virtio_net_q_object, obj_context, virtio_q_context);
8941a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, virtio_q_type, get_queue_type(ndev));
8951a86b377SEli Cohen 
8961a86b377SEli Cohen 	if (vq_is_tx(mvq->index))
8971a86b377SEli Cohen 		MLX5_SET(virtio_net_q_object, obj_context, tisn_or_qpn, ndev->res.tisn);
8981a86b377SEli Cohen 
899bc9a2b3eSEli Cohen 	if (mvq->map.virq) {
900bc9a2b3eSEli Cohen 		MLX5_SET(virtio_q, vq_ctx, event_mode, MLX5_VIRTIO_Q_EVENT_MODE_MSIX_MODE);
901bc9a2b3eSEli Cohen 		MLX5_SET(virtio_q, vq_ctx, event_qpn_or_msix, mvq->map.index);
902bc9a2b3eSEli Cohen 	} else {
9031a86b377SEli Cohen 		MLX5_SET(virtio_q, vq_ctx, event_mode, MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE);
9041a86b377SEli Cohen 		MLX5_SET(virtio_q, vq_ctx, event_qpn_or_msix, mvq->fwqp.mqp.qpn);
905bc9a2b3eSEli Cohen 	}
906bc9a2b3eSEli Cohen 
907bc9a2b3eSEli Cohen 	MLX5_SET(virtio_q, vq_ctx, queue_index, mvq->index);
9081a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, queue_size, mvq->num_ent);
9091a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, virtio_version_1_0,
9104b454a82SEli Cohen 		 !!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_F_VERSION_1)));
9111a86b377SEli Cohen 	MLX5_SET64(virtio_q, vq_ctx, desc_addr, mvq->desc_addr);
9121a86b377SEli Cohen 	MLX5_SET64(virtio_q, vq_ctx, used_addr, mvq->device_addr);
9131a86b377SEli Cohen 	MLX5_SET64(virtio_q, vq_ctx, available_addr, mvq->driver_addr);
91483fec3f1SAharon Landau 	MLX5_SET(virtio_q, vq_ctx, virtio_q_mkey, ndev->mvdev.mr.mkey);
9151a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_1_id, mvq->umem1.id);
9161a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_1_size, mvq->umem1.size);
9171a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_2_id, mvq->umem2.id);
918e3011776SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_2_size, mvq->umem2.size);
9191a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_3_id, mvq->umem3.id);
920e3011776SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_3_size, mvq->umem3.size);
9211a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, pd, ndev->mvdev.res.pdn);
9221892a3d4SEli Cohen 	if (counters_supported(&ndev->mvdev))
9231892a3d4SEli Cohen 		MLX5_SET(virtio_q, vq_ctx, counter_set_id, mvq->counter_set_id);
9241a86b377SEli Cohen 
9251a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out));
9261a86b377SEli Cohen 	if (err)
9271a86b377SEli Cohen 		goto err_cmd;
9281a86b377SEli Cohen 
929cae15c2eSEli Cohen 	mvq->fw_state = MLX5_VIRTIO_NET_Q_OBJECT_STATE_INIT;
9301a86b377SEli Cohen 	kfree(in);
9311a86b377SEli Cohen 	mvq->virtq_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
9321a86b377SEli Cohen 
9331a86b377SEli Cohen 	return 0;
9341a86b377SEli Cohen 
9351a86b377SEli Cohen err_cmd:
9361a86b377SEli Cohen 	kfree(in);
9371a86b377SEli Cohen err_alloc:
9381a86b377SEli Cohen 	umems_destroy(ndev, mvq);
9391a86b377SEli Cohen 	return err;
9401a86b377SEli Cohen }
9411a86b377SEli Cohen 
destroy_virtqueue(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)9421a86b377SEli Cohen static void destroy_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
9431a86b377SEli Cohen {
9441a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_virtio_net_q_in)] = {};
9451a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(destroy_virtio_net_q_out)] = {};
9461a86b377SEli Cohen 
9471a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.opcode,
9481a86b377SEli Cohen 		 MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
9491a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.obj_id, mvq->virtq_id);
9501a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.uid, ndev->mvdev.res.uid);
9511a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.obj_type,
9521a86b377SEli Cohen 		 MLX5_OBJ_TYPE_VIRTIO_NET_Q);
9531a86b377SEli Cohen 	if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out))) {
9541a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "destroy virtqueue 0x%x\n", mvq->virtq_id);
9551a86b377SEli Cohen 		return;
9561a86b377SEli Cohen 	}
957cae15c2eSEli Cohen 	mvq->fw_state = MLX5_VIRTIO_NET_Q_OBJECT_NONE;
9581a86b377SEli Cohen 	umems_destroy(ndev, mvq);
9591a86b377SEli Cohen }
9601a86b377SEli Cohen 
get_rqpn(struct mlx5_vdpa_virtqueue * mvq,bool fw)9611a86b377SEli Cohen static u32 get_rqpn(struct mlx5_vdpa_virtqueue *mvq, bool fw)
9621a86b377SEli Cohen {
9631a86b377SEli Cohen 	return fw ? mvq->vqqp.mqp.qpn : mvq->fwqp.mqp.qpn;
9641a86b377SEli Cohen }
9651a86b377SEli Cohen 
get_qpn(struct mlx5_vdpa_virtqueue * mvq,bool fw)9661a86b377SEli Cohen static u32 get_qpn(struct mlx5_vdpa_virtqueue *mvq, bool fw)
9671a86b377SEli Cohen {
9681a86b377SEli Cohen 	return fw ? mvq->fwqp.mqp.qpn : mvq->vqqp.mqp.qpn;
9691a86b377SEli Cohen }
9701a86b377SEli Cohen 
alloc_inout(struct mlx5_vdpa_net * ndev,int cmd,void ** in,int * inlen,void ** out,int * outlen,u32 qpn,u32 rqpn)9711a86b377SEli Cohen static void alloc_inout(struct mlx5_vdpa_net *ndev, int cmd, void **in, int *inlen, void **out,
9721a86b377SEli Cohen 			int *outlen, u32 qpn, u32 rqpn)
9731a86b377SEli Cohen {
9741a86b377SEli Cohen 	void *qpc;
9751a86b377SEli Cohen 	void *pp;
9761a86b377SEli Cohen 
9771a86b377SEli Cohen 	switch (cmd) {
9781a86b377SEli Cohen 	case MLX5_CMD_OP_2RST_QP:
9791a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(qp_2rst_in);
9801a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(qp_2rst_out);
9811a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
9821a86b377SEli Cohen 		*out = kzalloc(*outlen, GFP_KERNEL);
983f31231bfSColin Ian King 		if (!*in || !*out)
9841a86b377SEli Cohen 			goto outerr;
9851a86b377SEli Cohen 
9861a86b377SEli Cohen 		MLX5_SET(qp_2rst_in, *in, opcode, cmd);
9871a86b377SEli Cohen 		MLX5_SET(qp_2rst_in, *in, uid, ndev->mvdev.res.uid);
9881a86b377SEli Cohen 		MLX5_SET(qp_2rst_in, *in, qpn, qpn);
9891a86b377SEli Cohen 		break;
9901a86b377SEli Cohen 	case MLX5_CMD_OP_RST2INIT_QP:
9911a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(rst2init_qp_in);
9921a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(rst2init_qp_out);
9931a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
9941a86b377SEli Cohen 		*out = kzalloc(MLX5_ST_SZ_BYTES(rst2init_qp_out), GFP_KERNEL);
995f31231bfSColin Ian King 		if (!*in || !*out)
9961a86b377SEli Cohen 			goto outerr;
9971a86b377SEli Cohen 
9981a86b377SEli Cohen 		MLX5_SET(rst2init_qp_in, *in, opcode, cmd);
9991a86b377SEli Cohen 		MLX5_SET(rst2init_qp_in, *in, uid, ndev->mvdev.res.uid);
10001a86b377SEli Cohen 		MLX5_SET(rst2init_qp_in, *in, qpn, qpn);
10011a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc);
10021a86b377SEli Cohen 		MLX5_SET(qpc, qpc, remote_qpn, rqpn);
10031a86b377SEli Cohen 		MLX5_SET(qpc, qpc, rwe, 1);
10041a86b377SEli Cohen 		pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
10051a86b377SEli Cohen 		MLX5_SET(ads, pp, vhca_port_num, 1);
10061a86b377SEli Cohen 		break;
10071a86b377SEli Cohen 	case MLX5_CMD_OP_INIT2RTR_QP:
10081a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(init2rtr_qp_in);
10091a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(init2rtr_qp_out);
10101a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
10111a86b377SEli Cohen 		*out = kzalloc(MLX5_ST_SZ_BYTES(init2rtr_qp_out), GFP_KERNEL);
1012f31231bfSColin Ian King 		if (!*in || !*out)
10131a86b377SEli Cohen 			goto outerr;
10141a86b377SEli Cohen 
10151a86b377SEli Cohen 		MLX5_SET(init2rtr_qp_in, *in, opcode, cmd);
10161a86b377SEli Cohen 		MLX5_SET(init2rtr_qp_in, *in, uid, ndev->mvdev.res.uid);
10171a86b377SEli Cohen 		MLX5_SET(init2rtr_qp_in, *in, qpn, qpn);
10181a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc);
10191a86b377SEli Cohen 		MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES);
10201a86b377SEli Cohen 		MLX5_SET(qpc, qpc, log_msg_max, 30);
10211a86b377SEli Cohen 		MLX5_SET(qpc, qpc, remote_qpn, rqpn);
10221a86b377SEli Cohen 		pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
10231a86b377SEli Cohen 		MLX5_SET(ads, pp, fl, 1);
10241a86b377SEli Cohen 		break;
10251a86b377SEli Cohen 	case MLX5_CMD_OP_RTR2RTS_QP:
10261a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(rtr2rts_qp_in);
10271a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(rtr2rts_qp_out);
10281a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
10291a86b377SEli Cohen 		*out = kzalloc(MLX5_ST_SZ_BYTES(rtr2rts_qp_out), GFP_KERNEL);
1030f31231bfSColin Ian King 		if (!*in || !*out)
10311a86b377SEli Cohen 			goto outerr;
10321a86b377SEli Cohen 
10331a86b377SEli Cohen 		MLX5_SET(rtr2rts_qp_in, *in, opcode, cmd);
10341a86b377SEli Cohen 		MLX5_SET(rtr2rts_qp_in, *in, uid, ndev->mvdev.res.uid);
10351a86b377SEli Cohen 		MLX5_SET(rtr2rts_qp_in, *in, qpn, qpn);
10361a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc);
10371a86b377SEli Cohen 		pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
10381a86b377SEli Cohen 		MLX5_SET(ads, pp, ack_timeout, 14);
10391a86b377SEli Cohen 		MLX5_SET(qpc, qpc, retry_count, 7);
10401a86b377SEli Cohen 		MLX5_SET(qpc, qpc, rnr_retry, 7);
10411a86b377SEli Cohen 		break;
10421a86b377SEli Cohen 	default:
1043f31231bfSColin Ian King 		goto outerr_nullify;
10441a86b377SEli Cohen 	}
10451a86b377SEli Cohen 
10461a86b377SEli Cohen 	return;
10471a86b377SEli Cohen 
10481a86b377SEli Cohen outerr:
10491a86b377SEli Cohen 	kfree(*in);
10501a86b377SEli Cohen 	kfree(*out);
1051f31231bfSColin Ian King outerr_nullify:
10521a86b377SEli Cohen 	*in = NULL;
10531a86b377SEli Cohen 	*out = NULL;
10541a86b377SEli Cohen }
10551a86b377SEli Cohen 
free_inout(void * in,void * out)10561a86b377SEli Cohen static void free_inout(void *in, void *out)
10571a86b377SEli Cohen {
10581a86b377SEli Cohen 	kfree(in);
10591a86b377SEli Cohen 	kfree(out);
10601a86b377SEli Cohen }
10611a86b377SEli Cohen 
10621a86b377SEli Cohen /* Two QPs are used by each virtqueue. One is used by the driver and one by
10631a86b377SEli Cohen  * firmware. The fw argument indicates whether the subjected QP is the one used
10641a86b377SEli Cohen  * by firmware.
10651a86b377SEli Cohen  */
modify_qp(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,bool fw,int cmd)10661a86b377SEli Cohen static int modify_qp(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, bool fw, int cmd)
10671a86b377SEli Cohen {
10681a86b377SEli Cohen 	int outlen;
10691a86b377SEli Cohen 	int inlen;
10701a86b377SEli Cohen 	void *out;
10711a86b377SEli Cohen 	void *in;
10721a86b377SEli Cohen 	int err;
10731a86b377SEli Cohen 
10741a86b377SEli Cohen 	alloc_inout(ndev, cmd, &in, &inlen, &out, &outlen, get_qpn(mvq, fw), get_rqpn(mvq, fw));
10751a86b377SEli Cohen 	if (!in || !out)
10761a86b377SEli Cohen 		return -ENOMEM;
10771a86b377SEli Cohen 
10781a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, outlen);
10791a86b377SEli Cohen 	free_inout(in, out);
10801a86b377SEli Cohen 	return err;
10811a86b377SEli Cohen }
10821a86b377SEli Cohen 
connect_qps(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)10831a86b377SEli Cohen static int connect_qps(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
10841a86b377SEli Cohen {
10851a86b377SEli Cohen 	int err;
10861a86b377SEli Cohen 
10871a86b377SEli Cohen 	err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_2RST_QP);
10881a86b377SEli Cohen 	if (err)
10891a86b377SEli Cohen 		return err;
10901a86b377SEli Cohen 
10911a86b377SEli Cohen 	err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_2RST_QP);
10921a86b377SEli Cohen 	if (err)
10931a86b377SEli Cohen 		return err;
10941a86b377SEli Cohen 
10951a86b377SEli Cohen 	err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_RST2INIT_QP);
10961a86b377SEli Cohen 	if (err)
10971a86b377SEli Cohen 		return err;
10981a86b377SEli Cohen 
10991a86b377SEli Cohen 	err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_RST2INIT_QP);
11001a86b377SEli Cohen 	if (err)
11011a86b377SEli Cohen 		return err;
11021a86b377SEli Cohen 
11031a86b377SEli Cohen 	err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_INIT2RTR_QP);
11041a86b377SEli Cohen 	if (err)
11051a86b377SEli Cohen 		return err;
11061a86b377SEli Cohen 
11071a86b377SEli Cohen 	err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_INIT2RTR_QP);
11081a86b377SEli Cohen 	if (err)
11091a86b377SEli Cohen 		return err;
11101a86b377SEli Cohen 
11111a86b377SEli Cohen 	return modify_qp(ndev, mvq, true, MLX5_CMD_OP_RTR2RTS_QP);
11121a86b377SEli Cohen }
11131a86b377SEli Cohen 
11141a86b377SEli Cohen struct mlx5_virtq_attr {
11151a86b377SEli Cohen 	u8 state;
11161a86b377SEli Cohen 	u16 available_index;
1117b35ccebeSEli Cohen 	u16 used_index;
11181a86b377SEli Cohen };
11191a86b377SEli Cohen 
query_virtqueue(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,struct mlx5_virtq_attr * attr)11201a86b377SEli Cohen static int query_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq,
11211a86b377SEli Cohen 			   struct mlx5_virtq_attr *attr)
11221a86b377SEli Cohen {
11231a86b377SEli Cohen 	int outlen = MLX5_ST_SZ_BYTES(query_virtio_net_q_out);
11241a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(query_virtio_net_q_in)] = {};
11251a86b377SEli Cohen 	void *out;
11261a86b377SEli Cohen 	void *obj_context;
11271a86b377SEli Cohen 	void *cmd_hdr;
11281a86b377SEli Cohen 	int err;
11291a86b377SEli Cohen 
11301a86b377SEli Cohen 	out = kzalloc(outlen, GFP_KERNEL);
11311a86b377SEli Cohen 	if (!out)
11321a86b377SEli Cohen 		return -ENOMEM;
11331a86b377SEli Cohen 
11341a86b377SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(query_virtio_net_q_in, in, general_obj_in_cmd_hdr);
11351a86b377SEli Cohen 
11361a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_QUERY_GENERAL_OBJECT);
11371a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q);
11381a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->virtq_id);
11391a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
11401a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, outlen);
11411a86b377SEli Cohen 	if (err)
11421a86b377SEli Cohen 		goto err_cmd;
11431a86b377SEli Cohen 
11441a86b377SEli Cohen 	obj_context = MLX5_ADDR_OF(query_virtio_net_q_out, out, obj_context);
11451a86b377SEli Cohen 	memset(attr, 0, sizeof(*attr));
11461a86b377SEli Cohen 	attr->state = MLX5_GET(virtio_net_q_object, obj_context, state);
11471a86b377SEli Cohen 	attr->available_index = MLX5_GET(virtio_net_q_object, obj_context, hw_available_index);
1148b35ccebeSEli Cohen 	attr->used_index = MLX5_GET(virtio_net_q_object, obj_context, hw_used_index);
11491a86b377SEli Cohen 	kfree(out);
11501a86b377SEli Cohen 	return 0;
11511a86b377SEli Cohen 
11521a86b377SEli Cohen err_cmd:
11531a86b377SEli Cohen 	kfree(out);
11541a86b377SEli Cohen 	return err;
11551a86b377SEli Cohen }
11561a86b377SEli Cohen 
is_valid_state_change(int oldstate,int newstate)1157cae15c2eSEli Cohen static bool is_valid_state_change(int oldstate, int newstate)
1158cae15c2eSEli Cohen {
1159cae15c2eSEli Cohen 	switch (oldstate) {
1160cae15c2eSEli Cohen 	case MLX5_VIRTIO_NET_Q_OBJECT_STATE_INIT:
1161cae15c2eSEli Cohen 		return newstate == MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY;
1162cae15c2eSEli Cohen 	case MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY:
1163cae15c2eSEli Cohen 		return newstate == MLX5_VIRTIO_NET_Q_OBJECT_STATE_SUSPEND;
1164cae15c2eSEli Cohen 	case MLX5_VIRTIO_NET_Q_OBJECT_STATE_SUSPEND:
1165cae15c2eSEli Cohen 	case MLX5_VIRTIO_NET_Q_OBJECT_STATE_ERR:
1166cae15c2eSEli Cohen 	default:
1167cae15c2eSEli Cohen 		return false;
1168cae15c2eSEli Cohen 	}
1169cae15c2eSEli Cohen }
1170cae15c2eSEli Cohen 
modify_virtqueue(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,int state)11711a86b377SEli Cohen static int modify_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int state)
11721a86b377SEli Cohen {
11731a86b377SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(modify_virtio_net_q_in);
11741a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(modify_virtio_net_q_out)] = {};
11751a86b377SEli Cohen 	void *obj_context;
11761a86b377SEli Cohen 	void *cmd_hdr;
11771a86b377SEli Cohen 	void *in;
11781a86b377SEli Cohen 	int err;
11791a86b377SEli Cohen 
1180cae15c2eSEli Cohen 	if (mvq->fw_state == MLX5_VIRTIO_NET_Q_OBJECT_NONE)
1181cae15c2eSEli Cohen 		return 0;
1182cae15c2eSEli Cohen 
1183cae15c2eSEli Cohen 	if (!is_valid_state_change(mvq->fw_state, state))
1184cae15c2eSEli Cohen 		return -EINVAL;
1185cae15c2eSEli Cohen 
11861a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
11871a86b377SEli Cohen 	if (!in)
11881a86b377SEli Cohen 		return -ENOMEM;
11891a86b377SEli Cohen 
11901a86b377SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(modify_virtio_net_q_in, in, general_obj_in_cmd_hdr);
11911a86b377SEli Cohen 
11921a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_MODIFY_GENERAL_OBJECT);
11931a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q);
11941a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->virtq_id);
11951a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
11961a86b377SEli Cohen 
11971a86b377SEli Cohen 	obj_context = MLX5_ADDR_OF(modify_virtio_net_q_in, in, obj_context);
11981a86b377SEli Cohen 	MLX5_SET64(virtio_net_q_object, obj_context, modify_field_select,
11991a86b377SEli Cohen 		   MLX5_VIRTQ_MODIFY_MASK_STATE);
12001a86b377SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, state, state);
12011a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out));
12021a86b377SEli Cohen 	kfree(in);
12031a86b377SEli Cohen 	if (!err)
12041a86b377SEli Cohen 		mvq->fw_state = state;
12051a86b377SEli Cohen 
12061a86b377SEli Cohen 	return err;
12071a86b377SEli Cohen }
12081a86b377SEli Cohen 
counter_set_alloc(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)12091892a3d4SEli Cohen static int counter_set_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
12101892a3d4SEli Cohen {
12111892a3d4SEli Cohen 	u32 in[MLX5_ST_SZ_DW(create_virtio_q_counters_in)] = {};
12121892a3d4SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_virtio_q_counters_out)] = {};
12131892a3d4SEli Cohen 	void *cmd_hdr;
12141892a3d4SEli Cohen 	int err;
12151892a3d4SEli Cohen 
12161892a3d4SEli Cohen 	if (!counters_supported(&ndev->mvdev))
12171892a3d4SEli Cohen 		return 0;
12181892a3d4SEli Cohen 
12191892a3d4SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(create_virtio_q_counters_in, in, hdr);
12201892a3d4SEli Cohen 
12211892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
12221892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_Q_COUNTERS);
12231892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
12241892a3d4SEli Cohen 
12251892a3d4SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out));
12261892a3d4SEli Cohen 	if (err)
12271892a3d4SEli Cohen 		return err;
12281892a3d4SEli Cohen 
12291892a3d4SEli Cohen 	mvq->counter_set_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
12301892a3d4SEli Cohen 
12311892a3d4SEli Cohen 	return 0;
12321892a3d4SEli Cohen }
12331892a3d4SEli Cohen 
counter_set_dealloc(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)12341892a3d4SEli Cohen static void counter_set_dealloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
12351892a3d4SEli Cohen {
12361892a3d4SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_virtio_q_counters_in)] = {};
12371892a3d4SEli Cohen 	u32 out[MLX5_ST_SZ_DW(destroy_virtio_q_counters_out)] = {};
12381892a3d4SEli Cohen 
12391892a3d4SEli Cohen 	if (!counters_supported(&ndev->mvdev))
12401892a3d4SEli Cohen 		return;
12411892a3d4SEli Cohen 
12421892a3d4SEli Cohen 	MLX5_SET(destroy_virtio_q_counters_in, in, hdr.opcode, MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
12431892a3d4SEli Cohen 	MLX5_SET(destroy_virtio_q_counters_in, in, hdr.obj_id, mvq->counter_set_id);
12441892a3d4SEli Cohen 	MLX5_SET(destroy_virtio_q_counters_in, in, hdr.uid, ndev->mvdev.res.uid);
12451892a3d4SEli Cohen 	MLX5_SET(destroy_virtio_q_counters_in, in, hdr.obj_type, MLX5_OBJ_TYPE_VIRTIO_Q_COUNTERS);
12461892a3d4SEli Cohen 	if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out)))
12471892a3d4SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "dealloc counter set 0x%x\n", mvq->counter_set_id);
12481892a3d4SEli Cohen }
12491892a3d4SEli Cohen 
mlx5_vdpa_int_handler(int irq,void * priv)1250bc9a2b3eSEli Cohen static irqreturn_t mlx5_vdpa_int_handler(int irq, void *priv)
1251bc9a2b3eSEli Cohen {
1252bc9a2b3eSEli Cohen 	struct vdpa_callback *cb = priv;
1253bc9a2b3eSEli Cohen 
1254bc9a2b3eSEli Cohen 	if (cb->callback)
1255bc9a2b3eSEli Cohen 		return cb->callback(cb->private);
1256bc9a2b3eSEli Cohen 
1257bc9a2b3eSEli Cohen 	return IRQ_HANDLED;
1258bc9a2b3eSEli Cohen }
1259bc9a2b3eSEli Cohen 
alloc_vector(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)1260bc9a2b3eSEli Cohen static void alloc_vector(struct mlx5_vdpa_net *ndev,
1261bc9a2b3eSEli Cohen 			 struct mlx5_vdpa_virtqueue *mvq)
1262bc9a2b3eSEli Cohen {
1263bc9a2b3eSEli Cohen 	struct mlx5_vdpa_irq_pool *irqp = &ndev->irqp;
1264bc9a2b3eSEli Cohen 	struct mlx5_vdpa_irq_pool_entry *ent;
1265bc9a2b3eSEli Cohen 	int err;
1266bc9a2b3eSEli Cohen 	int i;
1267bc9a2b3eSEli Cohen 
1268bc9a2b3eSEli Cohen 	for (i = 0; i < irqp->num_ent; i++) {
1269bc9a2b3eSEli Cohen 		ent = &irqp->entries[i];
1270bc9a2b3eSEli Cohen 		if (!ent->used) {
1271bc9a2b3eSEli Cohen 			snprintf(ent->name, MLX5_VDPA_IRQ_NAME_LEN, "%s-vq-%d",
1272bc9a2b3eSEli Cohen 				 dev_name(&ndev->mvdev.vdev.dev), mvq->index);
1273bc9a2b3eSEli Cohen 			ent->dev_id = &ndev->event_cbs[mvq->index];
1274bc9a2b3eSEli Cohen 			err = request_irq(ent->map.virq, mlx5_vdpa_int_handler, 0,
1275bc9a2b3eSEli Cohen 					  ent->name, ent->dev_id);
1276bc9a2b3eSEli Cohen 			if (err)
1277bc9a2b3eSEli Cohen 				return;
1278bc9a2b3eSEli Cohen 
1279bc9a2b3eSEli Cohen 			ent->used = true;
1280bc9a2b3eSEli Cohen 			mvq->map = ent->map;
1281bc9a2b3eSEli Cohen 			return;
1282bc9a2b3eSEli Cohen 		}
1283bc9a2b3eSEli Cohen 	}
1284bc9a2b3eSEli Cohen }
1285bc9a2b3eSEli Cohen 
dealloc_vector(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)1286bc9a2b3eSEli Cohen static void dealloc_vector(struct mlx5_vdpa_net *ndev,
1287bc9a2b3eSEli Cohen 			   struct mlx5_vdpa_virtqueue *mvq)
1288bc9a2b3eSEli Cohen {
1289bc9a2b3eSEli Cohen 	struct mlx5_vdpa_irq_pool *irqp = &ndev->irqp;
1290bc9a2b3eSEli Cohen 	int i;
1291bc9a2b3eSEli Cohen 
1292bc9a2b3eSEli Cohen 	for (i = 0; i < irqp->num_ent; i++)
1293bc9a2b3eSEli Cohen 		if (mvq->map.virq == irqp->entries[i].map.virq) {
1294bc9a2b3eSEli Cohen 			free_irq(mvq->map.virq, irqp->entries[i].dev_id);
1295bc9a2b3eSEli Cohen 			irqp->entries[i].used = false;
1296bc9a2b3eSEli Cohen 			return;
1297bc9a2b3eSEli Cohen 		}
1298bc9a2b3eSEli Cohen }
1299bc9a2b3eSEli Cohen 
setup_vq(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)13001a86b377SEli Cohen static int setup_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
13011a86b377SEli Cohen {
13021a86b377SEli Cohen 	u16 idx = mvq->index;
13031a86b377SEli Cohen 	int err;
13041a86b377SEli Cohen 
13051a86b377SEli Cohen 	if (!mvq->num_ent)
13061a86b377SEli Cohen 		return 0;
13071a86b377SEli Cohen 
130852893733SEli Cohen 	if (mvq->initialized)
130952893733SEli Cohen 		return 0;
13101a86b377SEli Cohen 
13111a86b377SEli Cohen 	err = cq_create(ndev, idx, mvq->num_ent);
13121a86b377SEli Cohen 	if (err)
13131a86b377SEli Cohen 		return err;
13141a86b377SEli Cohen 
13151a86b377SEli Cohen 	err = qp_create(ndev, mvq, &mvq->fwqp);
13161a86b377SEli Cohen 	if (err)
13171a86b377SEli Cohen 		goto err_fwqp;
13181a86b377SEli Cohen 
13191a86b377SEli Cohen 	err = qp_create(ndev, mvq, &mvq->vqqp);
13201a86b377SEli Cohen 	if (err)
13211a86b377SEli Cohen 		goto err_vqqp;
13221a86b377SEli Cohen 
13231a86b377SEli Cohen 	err = connect_qps(ndev, mvq);
13241a86b377SEli Cohen 	if (err)
13251a86b377SEli Cohen 		goto err_connect;
13261a86b377SEli Cohen 
13271892a3d4SEli Cohen 	err = counter_set_alloc(ndev, mvq);
13281892a3d4SEli Cohen 	if (err)
1329bc9a2b3eSEli Cohen 		goto err_connect;
13301892a3d4SEli Cohen 
1331bc9a2b3eSEli Cohen 	alloc_vector(ndev, mvq);
13321a86b377SEli Cohen 	err = create_virtqueue(ndev, mvq);
13331a86b377SEli Cohen 	if (err)
1334bc9a2b3eSEli Cohen 		goto err_vq;
13351a86b377SEli Cohen 
13361a86b377SEli Cohen 	if (mvq->ready) {
13371a86b377SEli Cohen 		err = modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY);
13381a86b377SEli Cohen 		if (err) {
13391a86b377SEli Cohen 			mlx5_vdpa_warn(&ndev->mvdev, "failed to modify to ready vq idx %d(%d)\n",
13401a86b377SEli Cohen 				       idx, err);
1341bc9a2b3eSEli Cohen 			goto err_modify;
13421a86b377SEli Cohen 		}
13431a86b377SEli Cohen 	}
13441a86b377SEli Cohen 
13451a86b377SEli Cohen 	mvq->initialized = true;
13461a86b377SEli Cohen 	return 0;
13471a86b377SEli Cohen 
1348bc9a2b3eSEli Cohen err_modify:
1349bc9a2b3eSEli Cohen 	destroy_virtqueue(ndev, mvq);
1350bc9a2b3eSEli Cohen err_vq:
1351bc9a2b3eSEli Cohen 	dealloc_vector(ndev, mvq);
13521892a3d4SEli Cohen 	counter_set_dealloc(ndev, mvq);
1353bc9a2b3eSEli Cohen err_connect:
13541a86b377SEli Cohen 	qp_destroy(ndev, &mvq->vqqp);
13551a86b377SEli Cohen err_vqqp:
13561a86b377SEli Cohen 	qp_destroy(ndev, &mvq->fwqp);
13571a86b377SEli Cohen err_fwqp:
13581a86b377SEli Cohen 	cq_destroy(ndev, idx);
13591a86b377SEli Cohen 	return err;
13601a86b377SEli Cohen }
13611a86b377SEli Cohen 
suspend_vq(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)13621a86b377SEli Cohen static void suspend_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
13631a86b377SEli Cohen {
13641a86b377SEli Cohen 	struct mlx5_virtq_attr attr;
13651a86b377SEli Cohen 
13661a86b377SEli Cohen 	if (!mvq->initialized)
13671a86b377SEli Cohen 		return;
13681a86b377SEli Cohen 
13691a86b377SEli Cohen 	if (mvq->fw_state != MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY)
13701a86b377SEli Cohen 		return;
13711a86b377SEli Cohen 
13721a86b377SEli Cohen 	if (modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_SUSPEND))
13731a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "modify to suspend failed\n");
13743176e974SSi-Wei Liu 
13753176e974SSi-Wei Liu 	if (query_virtqueue(ndev, mvq, &attr)) {
13763176e974SSi-Wei Liu 		mlx5_vdpa_warn(&ndev->mvdev, "failed to query virtqueue\n");
13773176e974SSi-Wei Liu 		return;
13783176e974SSi-Wei Liu 	}
13793176e974SSi-Wei Liu 	mvq->avail_idx = attr.available_index;
1380bc04d93eSEli Cohen 	mvq->used_idx = attr.used_index;
13811a86b377SEli Cohen }
13821a86b377SEli Cohen 
suspend_vqs(struct mlx5_vdpa_net * ndev)13831a86b377SEli Cohen static void suspend_vqs(struct mlx5_vdpa_net *ndev)
13841a86b377SEli Cohen {
13851a86b377SEli Cohen 	int i;
13861a86b377SEli Cohen 
138775560522SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++)
13881a86b377SEli Cohen 		suspend_vq(ndev, &ndev->vqs[i]);
13891a86b377SEli Cohen }
13901a86b377SEli Cohen 
teardown_vq(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)13911a86b377SEli Cohen static void teardown_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
13921a86b377SEli Cohen {
13931a86b377SEli Cohen 	if (!mvq->initialized)
13941a86b377SEli Cohen 		return;
13951a86b377SEli Cohen 
13961a86b377SEli Cohen 	suspend_vq(ndev, mvq);
13971a86b377SEli Cohen 	destroy_virtqueue(ndev, mvq);
1398bc9a2b3eSEli Cohen 	dealloc_vector(ndev, mvq);
13991892a3d4SEli Cohen 	counter_set_dealloc(ndev, mvq);
14001a86b377SEli Cohen 	qp_destroy(ndev, &mvq->vqqp);
14011a86b377SEli Cohen 	qp_destroy(ndev, &mvq->fwqp);
14021a86b377SEli Cohen 	cq_destroy(ndev, mvq->index);
14031a86b377SEli Cohen 	mvq->initialized = false;
14041a86b377SEli Cohen }
14051a86b377SEli Cohen 
create_rqt(struct mlx5_vdpa_net * ndev)14061a86b377SEli Cohen static int create_rqt(struct mlx5_vdpa_net *ndev)
14071a86b377SEli Cohen {
1408a43ae805SEli Cohen 	int rqt_table_size = roundup_pow_of_two(ndev->rqt_size);
1409a43ae805SEli Cohen 	int act_sz = roundup_pow_of_two(ndev->cur_num_vqs / 2);
14101a86b377SEli Cohen 	__be32 *list;
14111a86b377SEli Cohen 	void *rqtc;
14121a86b377SEli Cohen 	int inlen;
14131a86b377SEli Cohen 	void *in;
14141a86b377SEli Cohen 	int i, j;
14151a86b377SEli Cohen 	int err;
14161a86b377SEli Cohen 
1417a43ae805SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + rqt_table_size * MLX5_ST_SZ_BYTES(rq_num);
14181a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
14191a86b377SEli Cohen 	if (!in)
14201a86b377SEli Cohen 		return -ENOMEM;
14211a86b377SEli Cohen 
14221a86b377SEli Cohen 	MLX5_SET(create_rqt_in, in, uid, ndev->mvdev.res.uid);
14231a86b377SEli Cohen 	rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
14241a86b377SEli Cohen 
14251a86b377SEli Cohen 	MLX5_SET(rqtc, rqtc, list_q_type, MLX5_RQTC_LIST_Q_TYPE_VIRTIO_NET_Q);
1426a43ae805SEli Cohen 	MLX5_SET(rqtc, rqtc, rqt_max_size, rqt_table_size);
14271a86b377SEli Cohen 	list = MLX5_ADDR_OF(rqtc, rqtc, rq_num[0]);
1428a43ae805SEli Cohen 	for (i = 0, j = 0; i < act_sz; i++, j += 2)
1429acde3929SEli Cohen 		list[i] = cpu_to_be32(ndev->vqs[j % ndev->cur_num_vqs].virtq_id);
14301a86b377SEli Cohen 
1431a43ae805SEli Cohen 	MLX5_SET(rqtc, rqtc, rqt_actual_size, act_sz);
14321a86b377SEli Cohen 	err = mlx5_vdpa_create_rqt(&ndev->mvdev, in, inlen, &ndev->res.rqtn);
14331a86b377SEli Cohen 	kfree(in);
14341a86b377SEli Cohen 	if (err)
14351a86b377SEli Cohen 		return err;
14361a86b377SEli Cohen 
14371a86b377SEli Cohen 	return 0;
14381a86b377SEli Cohen }
14391a86b377SEli Cohen 
144052893733SEli Cohen #define MLX5_MODIFY_RQT_NUM_RQS ((u64)1)
144152893733SEli Cohen 
modify_rqt(struct mlx5_vdpa_net * ndev,int num)144252893733SEli Cohen static int modify_rqt(struct mlx5_vdpa_net *ndev, int num)
144352893733SEli Cohen {
1444a43ae805SEli Cohen 	int act_sz = roundup_pow_of_two(num / 2);
144552893733SEli Cohen 	__be32 *list;
144652893733SEli Cohen 	void *rqtc;
144752893733SEli Cohen 	int inlen;
144852893733SEli Cohen 	void *in;
144952893733SEli Cohen 	int i, j;
145052893733SEli Cohen 	int err;
145152893733SEli Cohen 
1452a43ae805SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + act_sz * MLX5_ST_SZ_BYTES(rq_num);
145352893733SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
145452893733SEli Cohen 	if (!in)
145552893733SEli Cohen 		return -ENOMEM;
145652893733SEli Cohen 
145752893733SEli Cohen 	MLX5_SET(modify_rqt_in, in, uid, ndev->mvdev.res.uid);
145852893733SEli Cohen 	MLX5_SET64(modify_rqt_in, in, bitmask, MLX5_MODIFY_RQT_NUM_RQS);
145952893733SEli Cohen 	rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
146052893733SEli Cohen 	MLX5_SET(rqtc, rqtc, list_q_type, MLX5_RQTC_LIST_Q_TYPE_VIRTIO_NET_Q);
146152893733SEli Cohen 
146252893733SEli Cohen 	list = MLX5_ADDR_OF(rqtc, rqtc, rq_num[0]);
1463a43ae805SEli Cohen 	for (i = 0, j = 0; i < act_sz; i++, j = j + 2)
1464a7f46ba4SEli Cohen 		list[i] = cpu_to_be32(ndev->vqs[j % num].virtq_id);
146552893733SEli Cohen 
1466a43ae805SEli Cohen 	MLX5_SET(rqtc, rqtc, rqt_actual_size, act_sz);
146752893733SEli Cohen 	err = mlx5_vdpa_modify_rqt(&ndev->mvdev, in, inlen, ndev->res.rqtn);
146852893733SEli Cohen 	kfree(in);
146952893733SEli Cohen 	if (err)
147052893733SEli Cohen 		return err;
147152893733SEli Cohen 
147252893733SEli Cohen 	return 0;
147352893733SEli Cohen }
147452893733SEli Cohen 
destroy_rqt(struct mlx5_vdpa_net * ndev)14751a86b377SEli Cohen static void destroy_rqt(struct mlx5_vdpa_net *ndev)
14761a86b377SEli Cohen {
14771a86b377SEli Cohen 	mlx5_vdpa_destroy_rqt(&ndev->mvdev, ndev->res.rqtn);
14781a86b377SEli Cohen }
14791a86b377SEli Cohen 
create_tir(struct mlx5_vdpa_net * ndev)14801a86b377SEli Cohen static int create_tir(struct mlx5_vdpa_net *ndev)
14811a86b377SEli Cohen {
14821a86b377SEli Cohen #define HASH_IP_L4PORTS                                                                            \
14831a86b377SEli Cohen 	(MLX5_HASH_FIELD_SEL_SRC_IP | MLX5_HASH_FIELD_SEL_DST_IP | MLX5_HASH_FIELD_SEL_L4_SPORT |  \
14841a86b377SEli Cohen 	 MLX5_HASH_FIELD_SEL_L4_DPORT)
14851a86b377SEli Cohen 	static const u8 rx_hash_toeplitz_key[] = { 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
14861a86b377SEli Cohen 						   0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
14871a86b377SEli Cohen 						   0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
14881a86b377SEli Cohen 						   0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
14891a86b377SEli Cohen 						   0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a };
14901a86b377SEli Cohen 	void *rss_key;
14911a86b377SEli Cohen 	void *outer;
14921a86b377SEli Cohen 	void *tirc;
14931a86b377SEli Cohen 	void *in;
14941a86b377SEli Cohen 	int err;
14951a86b377SEli Cohen 
14961a86b377SEli Cohen 	in = kzalloc(MLX5_ST_SZ_BYTES(create_tir_in), GFP_KERNEL);
14971a86b377SEli Cohen 	if (!in)
14981a86b377SEli Cohen 		return -ENOMEM;
14991a86b377SEli Cohen 
15001a86b377SEli Cohen 	MLX5_SET(create_tir_in, in, uid, ndev->mvdev.res.uid);
15011a86b377SEli Cohen 	tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
15021a86b377SEli Cohen 	MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
15031a86b377SEli Cohen 
15041a86b377SEli Cohen 	MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
15051a86b377SEli Cohen 	MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
15061a86b377SEli Cohen 	rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
15071a86b377SEli Cohen 	memcpy(rss_key, rx_hash_toeplitz_key, sizeof(rx_hash_toeplitz_key));
15081a86b377SEli Cohen 
15091a86b377SEli Cohen 	outer = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
15101a86b377SEli Cohen 	MLX5_SET(rx_hash_field_select, outer, l3_prot_type, MLX5_L3_PROT_TYPE_IPV4);
15111a86b377SEli Cohen 	MLX5_SET(rx_hash_field_select, outer, l4_prot_type, MLX5_L4_PROT_TYPE_TCP);
15121a86b377SEli Cohen 	MLX5_SET(rx_hash_field_select, outer, selected_fields, HASH_IP_L4PORTS);
15131a86b377SEli Cohen 
15141a86b377SEli Cohen 	MLX5_SET(tirc, tirc, indirect_table, ndev->res.rqtn);
15151a86b377SEli Cohen 	MLX5_SET(tirc, tirc, transport_domain, ndev->res.tdn);
15161a86b377SEli Cohen 
15171a86b377SEli Cohen 	err = mlx5_vdpa_create_tir(&ndev->mvdev, in, &ndev->res.tirn);
15181a86b377SEli Cohen 	kfree(in);
151929422100SEli Cohen 	if (err)
152029422100SEli Cohen 		return err;
152129422100SEli Cohen 
152229422100SEli Cohen 	mlx5_vdpa_add_tirn(ndev);
15231a86b377SEli Cohen 	return err;
15241a86b377SEli Cohen }
15251a86b377SEli Cohen 
destroy_tir(struct mlx5_vdpa_net * ndev)15261a86b377SEli Cohen static void destroy_tir(struct mlx5_vdpa_net *ndev)
15271a86b377SEli Cohen {
152829422100SEli Cohen 	mlx5_vdpa_remove_tirn(ndev);
15291a86b377SEli Cohen 	mlx5_vdpa_destroy_tir(&ndev->mvdev, ndev->res.tirn);
15301a86b377SEli Cohen }
15311a86b377SEli Cohen 
1532baf2ad3fSEli Cohen #define MAX_STEERING_ENT 0x8000
1533baf2ad3fSEli Cohen #define MAX_STEERING_GROUPS 2
1534baf2ad3fSEli Cohen 
15350a599750SEli Cohen #if defined(CONFIG_MLX5_VDPA_STEERING_DEBUG)
15360a599750SEli Cohen        #define NUM_DESTS 2
15370a599750SEli Cohen #else
15380a599750SEli Cohen        #define NUM_DESTS 1
15390a599750SEli Cohen #endif
15400a599750SEli Cohen 
add_steering_counters(struct mlx5_vdpa_net * ndev,struct macvlan_node * node,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dests)15410a599750SEli Cohen static int add_steering_counters(struct mlx5_vdpa_net *ndev,
15420a599750SEli Cohen 				 struct macvlan_node *node,
15430a599750SEli Cohen 				 struct mlx5_flow_act *flow_act,
15440a599750SEli Cohen 				 struct mlx5_flow_destination *dests)
15451a86b377SEli Cohen {
15460a599750SEli Cohen #if defined(CONFIG_MLX5_VDPA_STEERING_DEBUG)
15470a599750SEli Cohen 	int err;
15480a599750SEli Cohen 
15490a599750SEli Cohen 	node->ucast_counter.counter = mlx5_fc_create(ndev->mvdev.mdev, false);
15500a599750SEli Cohen 	if (IS_ERR(node->ucast_counter.counter))
15510a599750SEli Cohen 		return PTR_ERR(node->ucast_counter.counter);
15520a599750SEli Cohen 
15530a599750SEli Cohen 	node->mcast_counter.counter = mlx5_fc_create(ndev->mvdev.mdev, false);
15540a599750SEli Cohen 	if (IS_ERR(node->mcast_counter.counter)) {
15550a599750SEli Cohen 		err = PTR_ERR(node->mcast_counter.counter);
15560a599750SEli Cohen 		goto err_mcast_counter;
15570a599750SEli Cohen 	}
15580a599750SEli Cohen 
15590a599750SEli Cohen 	dests[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
15600a599750SEli Cohen 	flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
15610a599750SEli Cohen 	return 0;
15620a599750SEli Cohen 
15630a599750SEli Cohen err_mcast_counter:
15640a599750SEli Cohen 	mlx5_fc_destroy(ndev->mvdev.mdev, node->ucast_counter.counter);
15650a599750SEli Cohen 	return err;
15660a599750SEli Cohen #else
15670a599750SEli Cohen 	return 0;
15680a599750SEli Cohen #endif
15690a599750SEli Cohen }
15700a599750SEli Cohen 
remove_steering_counters(struct mlx5_vdpa_net * ndev,struct macvlan_node * node)15710a599750SEli Cohen static void remove_steering_counters(struct mlx5_vdpa_net *ndev,
15720a599750SEli Cohen 				     struct macvlan_node *node)
15730a599750SEli Cohen {
15740a599750SEli Cohen #if defined(CONFIG_MLX5_VDPA_STEERING_DEBUG)
15750a599750SEli Cohen 	mlx5_fc_destroy(ndev->mvdev.mdev, node->mcast_counter.counter);
15760a599750SEli Cohen 	mlx5_fc_destroy(ndev->mvdev.mdev, node->ucast_counter.counter);
15770a599750SEli Cohen #endif
15780a599750SEli Cohen }
15790a599750SEli Cohen 
mlx5_vdpa_add_mac_vlan_rules(struct mlx5_vdpa_net * ndev,u8 * mac,struct macvlan_node * node)15800a599750SEli Cohen static int mlx5_vdpa_add_mac_vlan_rules(struct mlx5_vdpa_net *ndev, u8 *mac,
15810a599750SEli Cohen 					struct macvlan_node *node)
15820a599750SEli Cohen {
15830a599750SEli Cohen 	struct mlx5_flow_destination dests[NUM_DESTS] = {};
15841a86b377SEli Cohen 	struct mlx5_flow_act flow_act = {};
1585540061acSEli Cohen 	struct mlx5_flow_spec *spec;
1586540061acSEli Cohen 	void *headers_c;
1587540061acSEli Cohen 	void *headers_v;
1588540061acSEli Cohen 	u8 *dmac_c;
1589540061acSEli Cohen 	u8 *dmac_v;
15901a86b377SEli Cohen 	int err;
15910a599750SEli Cohen 	u16 vid;
15921a86b377SEli Cohen 
1593540061acSEli Cohen 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1594540061acSEli Cohen 	if (!spec)
1595540061acSEli Cohen 		return -ENOMEM;
1596540061acSEli Cohen 
15970a599750SEli Cohen 	vid = key2vid(node->macvlan);
1598540061acSEli Cohen 	spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1599540061acSEli Cohen 	headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, outer_headers);
1600540061acSEli Cohen 	headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
1601baf2ad3fSEli Cohen 	dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c, outer_headers.dmac_47_16);
1602540061acSEli Cohen 	dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v, outer_headers.dmac_47_16);
160371aa95a6SXu Qiang 	eth_broadcast_addr(dmac_c);
1604baf2ad3fSEli Cohen 	ether_addr_copy(dmac_v, mac);
1605a6ce72c0SEli Cohen 	if (ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VLAN)) {
1606baf2ad3fSEli Cohen 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
1607a6ce72c0SEli Cohen 		MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, first_vid);
1608a6ce72c0SEli Cohen 	}
16090a599750SEli Cohen 	if (node->tagged) {
1610baf2ad3fSEli Cohen 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
1611a6ce72c0SEli Cohen 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, vid);
1612baf2ad3fSEli Cohen 	}
16137becdd13SEli Cohen 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
16140a599750SEli Cohen 	dests[0].type = MLX5_FLOW_DESTINATION_TYPE_TIR;
16150a599750SEli Cohen 	dests[0].tir_num = ndev->res.tirn;
16160a599750SEli Cohen 	err = add_steering_counters(ndev, node, &flow_act, dests);
16170a599750SEli Cohen 	if (err)
16180a599750SEli Cohen 		goto out_free;
1619540061acSEli Cohen 
16200a599750SEli Cohen #if defined(CONFIG_MLX5_VDPA_STEERING_DEBUG)
16210a599750SEli Cohen 	dests[1].counter_id = mlx5_fc_id(node->ucast_counter.counter);
16220a599750SEli Cohen #endif
16230a599750SEli Cohen 	node->ucast_rule = mlx5_add_flow_rules(ndev->rxft, spec, &flow_act, dests, NUM_DESTS);
16240a599750SEli Cohen 	if (IS_ERR(node->ucast_rule)) {
16250a599750SEli Cohen 		err = PTR_ERR(node->ucast_rule);
16260a599750SEli Cohen 		goto err_ucast;
16270a599750SEli Cohen 	}
16280a599750SEli Cohen 
16290a599750SEli Cohen #if defined(CONFIG_MLX5_VDPA_STEERING_DEBUG)
16300a599750SEli Cohen 	dests[1].counter_id = mlx5_fc_id(node->mcast_counter.counter);
16310a599750SEli Cohen #endif
16321a86b377SEli Cohen 
1633540061acSEli Cohen 	memset(dmac_c, 0, ETH_ALEN);
1634540061acSEli Cohen 	memset(dmac_v, 0, ETH_ALEN);
1635540061acSEli Cohen 	dmac_c[0] = 1;
1636540061acSEli Cohen 	dmac_v[0] = 1;
16370a599750SEli Cohen 	node->mcast_rule = mlx5_add_flow_rules(ndev->rxft, spec, &flow_act, dests, NUM_DESTS);
16380a599750SEli Cohen 	if (IS_ERR(node->mcast_rule)) {
16390a599750SEli Cohen 		err = PTR_ERR(node->mcast_rule);
1640baf2ad3fSEli Cohen 		goto err_mcast;
1641540061acSEli Cohen 	}
16420a599750SEli Cohen 	kvfree(spec);
16430a599750SEli Cohen 	mlx5_vdpa_add_rx_counters(ndev, node);
16441a86b377SEli Cohen 	return 0;
16451a86b377SEli Cohen 
1646baf2ad3fSEli Cohen err_mcast:
16470a599750SEli Cohen 	mlx5_del_flow_rules(node->ucast_rule);
16480a599750SEli Cohen err_ucast:
16490a599750SEli Cohen 	remove_steering_counters(ndev, node);
16500a599750SEli Cohen out_free:
16510a599750SEli Cohen 	kvfree(spec);
16521a86b377SEli Cohen 	return err;
16531a86b377SEli Cohen }
16541a86b377SEli Cohen 
mlx5_vdpa_del_mac_vlan_rules(struct mlx5_vdpa_net * ndev,struct macvlan_node * node)1655baf2ad3fSEli Cohen static void mlx5_vdpa_del_mac_vlan_rules(struct mlx5_vdpa_net *ndev,
16560a599750SEli Cohen 					 struct macvlan_node *node)
16571a86b377SEli Cohen {
16580a599750SEli Cohen 	mlx5_vdpa_remove_rx_counters(ndev, node);
16590a599750SEli Cohen 	mlx5_del_flow_rules(node->ucast_rule);
16600a599750SEli Cohen 	mlx5_del_flow_rules(node->mcast_rule);
1661baf2ad3fSEli Cohen }
1662baf2ad3fSEli Cohen 
search_val(u8 * mac,u16 vlan,bool tagged)1663baf2ad3fSEli Cohen static u64 search_val(u8 *mac, u16 vlan, bool tagged)
1664baf2ad3fSEli Cohen {
1665baf2ad3fSEli Cohen 	u64 val;
1666baf2ad3fSEli Cohen 
1667baf2ad3fSEli Cohen 	if (!tagged)
1668baf2ad3fSEli Cohen 		vlan = MLX5V_UNTAGGED;
1669baf2ad3fSEli Cohen 
1670baf2ad3fSEli Cohen 	val = (u64)vlan << 48 |
1671baf2ad3fSEli Cohen 	      (u64)mac[0] << 40 |
1672baf2ad3fSEli Cohen 	      (u64)mac[1] << 32 |
1673baf2ad3fSEli Cohen 	      (u64)mac[2] << 24 |
1674baf2ad3fSEli Cohen 	      (u64)mac[3] << 16 |
1675baf2ad3fSEli Cohen 	      (u64)mac[4] << 8 |
1676baf2ad3fSEli Cohen 	      (u64)mac[5];
1677baf2ad3fSEli Cohen 
1678baf2ad3fSEli Cohen 	return val;
1679baf2ad3fSEli Cohen }
1680baf2ad3fSEli Cohen 
mac_vlan_lookup(struct mlx5_vdpa_net * ndev,u64 value)1681baf2ad3fSEli Cohen static struct macvlan_node *mac_vlan_lookup(struct mlx5_vdpa_net *ndev, u64 value)
1682baf2ad3fSEli Cohen {
1683baf2ad3fSEli Cohen 	struct macvlan_node *pos;
1684baf2ad3fSEli Cohen 	u32 idx;
1685baf2ad3fSEli Cohen 
1686baf2ad3fSEli Cohen 	idx = hash_64(value, 8); // tbd 8
1687baf2ad3fSEli Cohen 	hlist_for_each_entry(pos, &ndev->macvlan_hash[idx], hlist) {
1688baf2ad3fSEli Cohen 		if (pos->macvlan == value)
1689baf2ad3fSEli Cohen 			return pos;
1690baf2ad3fSEli Cohen 	}
1691baf2ad3fSEli Cohen 	return NULL;
1692baf2ad3fSEli Cohen }
1693baf2ad3fSEli Cohen 
mac_vlan_add(struct mlx5_vdpa_net * ndev,u8 * mac,u16 vid,bool tagged)16940a599750SEli Cohen static int mac_vlan_add(struct mlx5_vdpa_net *ndev, u8 *mac, u16 vid, bool tagged)
1695baf2ad3fSEli Cohen {
1696baf2ad3fSEli Cohen 	struct macvlan_node *ptr;
1697baf2ad3fSEli Cohen 	u64 val;
1698baf2ad3fSEli Cohen 	u32 idx;
1699baf2ad3fSEli Cohen 	int err;
1700baf2ad3fSEli Cohen 
17010a599750SEli Cohen 	val = search_val(mac, vid, tagged);
1702baf2ad3fSEli Cohen 	if (mac_vlan_lookup(ndev, val))
1703baf2ad3fSEli Cohen 		return -EEXIST;
1704baf2ad3fSEli Cohen 
1705baf2ad3fSEli Cohen 	ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
1706baf2ad3fSEli Cohen 	if (!ptr)
1707baf2ad3fSEli Cohen 		return -ENOMEM;
1708baf2ad3fSEli Cohen 
17090a599750SEli Cohen 	ptr->tagged = tagged;
17100a599750SEli Cohen 	ptr->macvlan = val;
17110a599750SEli Cohen 	ptr->ndev = ndev;
17120a599750SEli Cohen 	err = mlx5_vdpa_add_mac_vlan_rules(ndev, ndev->config.mac, ptr);
1713baf2ad3fSEli Cohen 	if (err)
1714baf2ad3fSEli Cohen 		goto err_add;
1715baf2ad3fSEli Cohen 
1716baf2ad3fSEli Cohen 	idx = hash_64(val, 8);
1717baf2ad3fSEli Cohen 	hlist_add_head(&ptr->hlist, &ndev->macvlan_hash[idx]);
1718baf2ad3fSEli Cohen 	return 0;
1719baf2ad3fSEli Cohen 
1720baf2ad3fSEli Cohen err_add:
1721baf2ad3fSEli Cohen 	kfree(ptr);
1722baf2ad3fSEli Cohen 	return err;
1723baf2ad3fSEli Cohen }
1724baf2ad3fSEli Cohen 
mac_vlan_del(struct mlx5_vdpa_net * ndev,u8 * mac,u16 vlan,bool tagged)1725baf2ad3fSEli Cohen static void mac_vlan_del(struct mlx5_vdpa_net *ndev, u8 *mac, u16 vlan, bool tagged)
1726baf2ad3fSEli Cohen {
1727baf2ad3fSEli Cohen 	struct macvlan_node *ptr;
1728baf2ad3fSEli Cohen 
1729baf2ad3fSEli Cohen 	ptr = mac_vlan_lookup(ndev, search_val(mac, vlan, tagged));
1730baf2ad3fSEli Cohen 	if (!ptr)
17311a86b377SEli Cohen 		return;
17321a86b377SEli Cohen 
1733baf2ad3fSEli Cohen 	hlist_del(&ptr->hlist);
17340a599750SEli Cohen 	mlx5_vdpa_del_mac_vlan_rules(ndev, ptr);
17350a599750SEli Cohen 	remove_steering_counters(ndev, ptr);
1736baf2ad3fSEli Cohen 	kfree(ptr);
1737baf2ad3fSEli Cohen }
1738baf2ad3fSEli Cohen 
clear_mac_vlan_table(struct mlx5_vdpa_net * ndev)1739baf2ad3fSEli Cohen static void clear_mac_vlan_table(struct mlx5_vdpa_net *ndev)
1740baf2ad3fSEli Cohen {
1741baf2ad3fSEli Cohen 	struct macvlan_node *pos;
1742baf2ad3fSEli Cohen 	struct hlist_node *n;
1743baf2ad3fSEli Cohen 	int i;
1744baf2ad3fSEli Cohen 
1745baf2ad3fSEli Cohen 	for (i = 0; i < MLX5V_MACVLAN_SIZE; i++) {
1746baf2ad3fSEli Cohen 		hlist_for_each_entry_safe(pos, n, &ndev->macvlan_hash[i], hlist) {
1747baf2ad3fSEli Cohen 			hlist_del(&pos->hlist);
17480a599750SEli Cohen 			mlx5_vdpa_del_mac_vlan_rules(ndev, pos);
17490a599750SEli Cohen 			remove_steering_counters(ndev, pos);
1750baf2ad3fSEli Cohen 			kfree(pos);
1751baf2ad3fSEli Cohen 		}
1752baf2ad3fSEli Cohen 	}
1753baf2ad3fSEli Cohen }
1754baf2ad3fSEli Cohen 
setup_steering(struct mlx5_vdpa_net * ndev)1755baf2ad3fSEli Cohen static int setup_steering(struct mlx5_vdpa_net *ndev)
1756baf2ad3fSEli Cohen {
1757baf2ad3fSEli Cohen 	struct mlx5_flow_table_attr ft_attr = {};
1758baf2ad3fSEli Cohen 	struct mlx5_flow_namespace *ns;
1759baf2ad3fSEli Cohen 	int err;
1760baf2ad3fSEli Cohen 
1761baf2ad3fSEli Cohen 	ft_attr.max_fte = MAX_STEERING_ENT;
1762baf2ad3fSEli Cohen 	ft_attr.autogroup.max_num_groups = MAX_STEERING_GROUPS;
1763baf2ad3fSEli Cohen 
1764baf2ad3fSEli Cohen 	ns = mlx5_get_flow_namespace(ndev->mvdev.mdev, MLX5_FLOW_NAMESPACE_BYPASS);
1765baf2ad3fSEli Cohen 	if (!ns) {
1766baf2ad3fSEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "failed to get flow namespace\n");
1767baf2ad3fSEli Cohen 		return -EOPNOTSUPP;
1768baf2ad3fSEli Cohen 	}
1769baf2ad3fSEli Cohen 
1770baf2ad3fSEli Cohen 	ndev->rxft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
1771baf2ad3fSEli Cohen 	if (IS_ERR(ndev->rxft)) {
1772baf2ad3fSEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "failed to create flow table\n");
1773baf2ad3fSEli Cohen 		return PTR_ERR(ndev->rxft);
1774baf2ad3fSEli Cohen 	}
177529422100SEli Cohen 	mlx5_vdpa_add_rx_flow_table(ndev);
1776baf2ad3fSEli Cohen 
1777baf2ad3fSEli Cohen 	err = mac_vlan_add(ndev, ndev->config.mac, 0, false);
1778baf2ad3fSEli Cohen 	if (err)
1779baf2ad3fSEli Cohen 		goto err_add;
1780baf2ad3fSEli Cohen 
1781baf2ad3fSEli Cohen 	return 0;
1782baf2ad3fSEli Cohen 
1783baf2ad3fSEli Cohen err_add:
178429422100SEli Cohen 	mlx5_vdpa_remove_rx_flow_table(ndev);
1785baf2ad3fSEli Cohen 	mlx5_destroy_flow_table(ndev->rxft);
1786baf2ad3fSEli Cohen 	return err;
1787baf2ad3fSEli Cohen }
1788baf2ad3fSEli Cohen 
teardown_steering(struct mlx5_vdpa_net * ndev)1789baf2ad3fSEli Cohen static void teardown_steering(struct mlx5_vdpa_net *ndev)
1790baf2ad3fSEli Cohen {
1791baf2ad3fSEli Cohen 	clear_mac_vlan_table(ndev);
179229422100SEli Cohen 	mlx5_vdpa_remove_rx_flow_table(ndev);
17931a86b377SEli Cohen 	mlx5_destroy_flow_table(ndev->rxft);
17941a86b377SEli Cohen }
17951a86b377SEli Cohen 
handle_ctrl_mac(struct mlx5_vdpa_dev * mvdev,u8 cmd)17965262912eSEli Cohen static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
17975262912eSEli Cohen {
17985262912eSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
17995262912eSEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
18005262912eSEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
18015262912eSEli Cohen 	struct mlx5_core_dev *pfmdev;
18025262912eSEli Cohen 	size_t read;
1803f1781bedSMichael Qiu 	u8 mac[ETH_ALEN], mac_back[ETH_ALEN];
18045262912eSEli Cohen 
18055262912eSEli Cohen 	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
18065262912eSEli Cohen 	switch (cmd) {
18075262912eSEli Cohen 	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
18085262912eSEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)mac, ETH_ALEN);
18095262912eSEli Cohen 		if (read != ETH_ALEN)
18105262912eSEli Cohen 			break;
18115262912eSEli Cohen 
18125262912eSEli Cohen 		if (!memcmp(ndev->config.mac, mac, 6)) {
18135262912eSEli Cohen 			status = VIRTIO_NET_OK;
18145262912eSEli Cohen 			break;
18155262912eSEli Cohen 		}
18165262912eSEli Cohen 
1817f1781bedSMichael Qiu 		if (is_zero_ether_addr(mac))
1818f1781bedSMichael Qiu 			break;
1819f1781bedSMichael Qiu 
18205262912eSEli Cohen 		if (!is_zero_ether_addr(ndev->config.mac)) {
18215262912eSEli Cohen 			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
18225262912eSEli Cohen 				mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
18235262912eSEli Cohen 					       ndev->config.mac);
18245262912eSEli Cohen 				break;
18255262912eSEli Cohen 			}
18265262912eSEli Cohen 		}
18275262912eSEli Cohen 
18285262912eSEli Cohen 		if (mlx5_mpfs_add_mac(pfmdev, mac)) {
18295262912eSEli Cohen 			mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
18305262912eSEli Cohen 				       mac);
18315262912eSEli Cohen 			break;
18325262912eSEli Cohen 		}
18335262912eSEli Cohen 
1834f1781bedSMichael Qiu 		/* backup the original mac address so that if failed to add the forward rules
1835f1781bedSMichael Qiu 		 * we could restore it
1836f1781bedSMichael Qiu 		 */
1837f1781bedSMichael Qiu 		memcpy(mac_back, ndev->config.mac, ETH_ALEN);
1838f1781bedSMichael Qiu 
18395262912eSEli Cohen 		memcpy(ndev->config.mac, mac, ETH_ALEN);
1840f1781bedSMichael Qiu 
1841f1781bedSMichael Qiu 		/* Need recreate the flow table entry, so that the packet could forward back
1842f1781bedSMichael Qiu 		 */
18431ab53760SEli Cohen 		mac_vlan_del(ndev, mac_back, 0, false);
1844f1781bedSMichael Qiu 
1845baf2ad3fSEli Cohen 		if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
1846f1781bedSMichael Qiu 			mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
1847f1781bedSMichael Qiu 
1848f1781bedSMichael Qiu 			/* Although it hardly run here, we still need double check */
1849f1781bedSMichael Qiu 			if (is_zero_ether_addr(mac_back)) {
1850f1781bedSMichael Qiu 				mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
1851f1781bedSMichael Qiu 				break;
1852f1781bedSMichael Qiu 			}
1853f1781bedSMichael Qiu 
1854f1781bedSMichael Qiu 			/* Try to restore original mac address to MFPS table, and try to restore
1855f1781bedSMichael Qiu 			 * the forward rule entry.
1856f1781bedSMichael Qiu 			 */
1857f1781bedSMichael Qiu 			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
1858f1781bedSMichael Qiu 				mlx5_vdpa_warn(mvdev, "restore mac failed: delete MAC %pM from MPFS table failed\n",
1859f1781bedSMichael Qiu 					       ndev->config.mac);
1860f1781bedSMichael Qiu 			}
1861f1781bedSMichael Qiu 
1862f1781bedSMichael Qiu 			if (mlx5_mpfs_add_mac(pfmdev, mac_back)) {
1863f1781bedSMichael Qiu 				mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
1864f1781bedSMichael Qiu 					       mac_back);
1865f1781bedSMichael Qiu 			}
1866f1781bedSMichael Qiu 
1867f1781bedSMichael Qiu 			memcpy(ndev->config.mac, mac_back, ETH_ALEN);
1868f1781bedSMichael Qiu 
1869baf2ad3fSEli Cohen 			if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
1870f1781bedSMichael Qiu 				mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
1871f1781bedSMichael Qiu 
1872f1781bedSMichael Qiu 			break;
1873f1781bedSMichael Qiu 		}
1874f1781bedSMichael Qiu 
18755262912eSEli Cohen 		status = VIRTIO_NET_OK;
18765262912eSEli Cohen 		break;
18775262912eSEli Cohen 
18785262912eSEli Cohen 	default:
18795262912eSEli Cohen 		break;
18805262912eSEli Cohen 	}
18815262912eSEli Cohen 
18825262912eSEli Cohen 	return status;
18835262912eSEli Cohen }
18845262912eSEli Cohen 
change_num_qps(struct mlx5_vdpa_dev * mvdev,int newqps)188552893733SEli Cohen static int change_num_qps(struct mlx5_vdpa_dev *mvdev, int newqps)
188652893733SEli Cohen {
188752893733SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
188852893733SEli Cohen 	int cur_qps = ndev->cur_num_vqs / 2;
188952893733SEli Cohen 	int err;
189052893733SEli Cohen 	int i;
189152893733SEli Cohen 
189252893733SEli Cohen 	if (cur_qps > newqps) {
189352893733SEli Cohen 		err = modify_rqt(ndev, 2 * newqps);
189452893733SEli Cohen 		if (err)
189552893733SEli Cohen 			return err;
189652893733SEli Cohen 
189752893733SEli Cohen 		for (i = ndev->cur_num_vqs - 1; i >= 2 * newqps; i--)
189852893733SEli Cohen 			teardown_vq(ndev, &ndev->vqs[i]);
189952893733SEli Cohen 
190052893733SEli Cohen 		ndev->cur_num_vqs = 2 * newqps;
190152893733SEli Cohen 	} else {
190252893733SEli Cohen 		ndev->cur_num_vqs = 2 * newqps;
190352893733SEli Cohen 		for (i = cur_qps * 2; i < 2 * newqps; i++) {
190452893733SEli Cohen 			err = setup_vq(ndev, &ndev->vqs[i]);
190552893733SEli Cohen 			if (err)
190652893733SEli Cohen 				goto clean_added;
190752893733SEli Cohen 		}
190852893733SEli Cohen 		err = modify_rqt(ndev, 2 * newqps);
190952893733SEli Cohen 		if (err)
191052893733SEli Cohen 			goto clean_added;
191152893733SEli Cohen 	}
191252893733SEli Cohen 	return 0;
191352893733SEli Cohen 
191452893733SEli Cohen clean_added:
191537e07e70SEli Cohen 	for (--i; i >= 2 * cur_qps; --i)
191652893733SEli Cohen 		teardown_vq(ndev, &ndev->vqs[i]);
191752893733SEli Cohen 
191837e07e70SEli Cohen 	ndev->cur_num_vqs = 2 * cur_qps;
191937e07e70SEli Cohen 
192052893733SEli Cohen 	return err;
192152893733SEli Cohen }
192252893733SEli Cohen 
handle_ctrl_mq(struct mlx5_vdpa_dev * mvdev,u8 cmd)192352893733SEli Cohen static virtio_net_ctrl_ack handle_ctrl_mq(struct mlx5_vdpa_dev *mvdev, u8 cmd)
192452893733SEli Cohen {
192552893733SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
192652893733SEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
192752893733SEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
192852893733SEli Cohen 	struct virtio_net_ctrl_mq mq;
192952893733SEli Cohen 	size_t read;
193052893733SEli Cohen 	u16 newqps;
193152893733SEli Cohen 
193252893733SEli Cohen 	switch (cmd) {
193352893733SEli Cohen 	case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET:
1934ed0f849fSSi-Wei Liu 		/* This mq feature check aligns with pre-existing userspace
1935ed0f849fSSi-Wei Liu 		 * implementation.
1936ed0f849fSSi-Wei Liu 		 *
1937ed0f849fSSi-Wei Liu 		 * Without it, an untrusted driver could fake a multiqueue config
1938ed0f849fSSi-Wei Liu 		 * request down to a non-mq device that may cause kernel to
1939ed0f849fSSi-Wei Liu 		 * panic due to uninitialized resources for extra vqs. Even with
1940ed0f849fSSi-Wei Liu 		 * a well behaving guest driver, it is not expected to allow
1941ed0f849fSSi-Wei Liu 		 * changing the number of vqs on a non-mq device.
1942ed0f849fSSi-Wei Liu 		 */
1943ed0f849fSSi-Wei Liu 		if (!MLX5_FEATURE(mvdev, VIRTIO_NET_F_MQ))
1944ed0f849fSSi-Wei Liu 			break;
1945ed0f849fSSi-Wei Liu 
194652893733SEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)&mq, sizeof(mq));
194752893733SEli Cohen 		if (read != sizeof(mq))
194852893733SEli Cohen 			break;
194952893733SEli Cohen 
195052893733SEli Cohen 		newqps = mlx5vdpa16_to_cpu(mvdev, mq.virtqueue_pairs);
1951ed0f849fSSi-Wei Liu 		if (newqps < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1952acde3929SEli Cohen 		    newqps > ndev->rqt_size)
1953ed0f849fSSi-Wei Liu 			break;
1954ed0f849fSSi-Wei Liu 
195552893733SEli Cohen 		if (ndev->cur_num_vqs == 2 * newqps) {
195652893733SEli Cohen 			status = VIRTIO_NET_OK;
195752893733SEli Cohen 			break;
195852893733SEli Cohen 		}
195952893733SEli Cohen 
196052893733SEli Cohen 		if (!change_num_qps(mvdev, newqps))
196152893733SEli Cohen 			status = VIRTIO_NET_OK;
196252893733SEli Cohen 
196352893733SEli Cohen 		break;
196452893733SEli Cohen 	default:
196552893733SEli Cohen 		break;
196652893733SEli Cohen 	}
196752893733SEli Cohen 
196852893733SEli Cohen 	return status;
196952893733SEli Cohen }
197052893733SEli Cohen 
handle_ctrl_vlan(struct mlx5_vdpa_dev * mvdev,u8 cmd)1971baf2ad3fSEli Cohen static virtio_net_ctrl_ack handle_ctrl_vlan(struct mlx5_vdpa_dev *mvdev, u8 cmd)
1972baf2ad3fSEli Cohen {
1973baf2ad3fSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
1974baf2ad3fSEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
1975baf2ad3fSEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
1976baf2ad3fSEli Cohen 	__virtio16 vlan;
1977baf2ad3fSEli Cohen 	size_t read;
1978baf2ad3fSEli Cohen 	u16 id;
1979baf2ad3fSEli Cohen 
19805aec8049SEli Cohen 	if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VLAN)))
19815aec8049SEli Cohen 		return status;
19825aec8049SEli Cohen 
1983baf2ad3fSEli Cohen 	switch (cmd) {
1984baf2ad3fSEli Cohen 	case VIRTIO_NET_CTRL_VLAN_ADD:
1985baf2ad3fSEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, &vlan, sizeof(vlan));
1986baf2ad3fSEli Cohen 		if (read != sizeof(vlan))
1987baf2ad3fSEli Cohen 			break;
1988baf2ad3fSEli Cohen 
1989baf2ad3fSEli Cohen 		id = mlx5vdpa16_to_cpu(mvdev, vlan);
1990baf2ad3fSEli Cohen 		if (mac_vlan_add(ndev, ndev->config.mac, id, true))
1991baf2ad3fSEli Cohen 			break;
1992baf2ad3fSEli Cohen 
1993baf2ad3fSEli Cohen 		status = VIRTIO_NET_OK;
1994baf2ad3fSEli Cohen 		break;
1995baf2ad3fSEli Cohen 	case VIRTIO_NET_CTRL_VLAN_DEL:
1996baf2ad3fSEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, &vlan, sizeof(vlan));
1997baf2ad3fSEli Cohen 		if (read != sizeof(vlan))
1998baf2ad3fSEli Cohen 			break;
1999baf2ad3fSEli Cohen 
2000baf2ad3fSEli Cohen 		id = mlx5vdpa16_to_cpu(mvdev, vlan);
2001baf2ad3fSEli Cohen 		mac_vlan_del(ndev, ndev->config.mac, id, true);
2002f766c409SDan Carpenter 		status = VIRTIO_NET_OK;
2003baf2ad3fSEli Cohen 		break;
2004baf2ad3fSEli Cohen 	default:
2005baf2ad3fSEli Cohen 		break;
2006baf2ad3fSEli Cohen 	}
2007baf2ad3fSEli Cohen 
2008baf2ad3fSEli Cohen 	return status;
2009baf2ad3fSEli Cohen }
2010baf2ad3fSEli Cohen 
mlx5_cvq_kick_handler(struct work_struct * work)20115262912eSEli Cohen static void mlx5_cvq_kick_handler(struct work_struct *work)
20125262912eSEli Cohen {
20135262912eSEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
20145262912eSEli Cohen 	struct virtio_net_ctrl_hdr ctrl;
2015218bdd20SEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
20165262912eSEli Cohen 	struct mlx5_vdpa_dev *mvdev;
20175262912eSEli Cohen 	struct mlx5_control_vq *cvq;
20185262912eSEli Cohen 	struct mlx5_vdpa_net *ndev;
20195262912eSEli Cohen 	size_t read, write;
20205262912eSEli Cohen 	int err;
20215262912eSEli Cohen 
2022218bdd20SEli Cohen 	wqent = container_of(work, struct mlx5_vdpa_wq_ent, work);
20235262912eSEli Cohen 	mvdev = wqent->mvdev;
20245262912eSEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
20255262912eSEli Cohen 	cvq = &mvdev->cvq;
20261c80cf03SJason Wang 
2027759ae7f9SEli Cohen 	down_write(&ndev->reslock);
20281c80cf03SJason Wang 
20291c80cf03SJason Wang 	if (!(mvdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
20301c80cf03SJason Wang 		goto out;
20311c80cf03SJason Wang 
20325262912eSEli Cohen 	if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)))
20331c80cf03SJason Wang 		goto out;
20345262912eSEli Cohen 
20355262912eSEli Cohen 	if (!cvq->ready)
20361c80cf03SJason Wang 		goto out;
20375262912eSEli Cohen 
20385262912eSEli Cohen 	while (true) {
20395262912eSEli Cohen 		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->riov, &cvq->wiov, &cvq->head,
20405262912eSEli Cohen 					   GFP_ATOMIC);
20415262912eSEli Cohen 		if (err <= 0)
20425262912eSEli Cohen 			break;
20435262912eSEli Cohen 
20445262912eSEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, &ctrl, sizeof(ctrl));
20455262912eSEli Cohen 		if (read != sizeof(ctrl))
20465262912eSEli Cohen 			break;
20475262912eSEli Cohen 
20481892a3d4SEli Cohen 		cvq->received_desc++;
20495262912eSEli Cohen 		switch (ctrl.class) {
20505262912eSEli Cohen 		case VIRTIO_NET_CTRL_MAC:
20515262912eSEli Cohen 			status = handle_ctrl_mac(mvdev, ctrl.cmd);
20525262912eSEli Cohen 			break;
205352893733SEli Cohen 		case VIRTIO_NET_CTRL_MQ:
205452893733SEli Cohen 			status = handle_ctrl_mq(mvdev, ctrl.cmd);
205552893733SEli Cohen 			break;
2056baf2ad3fSEli Cohen 		case VIRTIO_NET_CTRL_VLAN:
2057baf2ad3fSEli Cohen 			status = handle_ctrl_vlan(mvdev, ctrl.cmd);
2058baf2ad3fSEli Cohen 			break;
20595262912eSEli Cohen 		default:
20605262912eSEli Cohen 			break;
20615262912eSEli Cohen 		}
20625262912eSEli Cohen 
20635262912eSEli Cohen 		/* Make sure data is written before advancing index */
20645262912eSEli Cohen 		smp_wmb();
20655262912eSEli Cohen 
20665262912eSEli Cohen 		write = vringh_iov_push_iotlb(&cvq->vring, &cvq->wiov, &status, sizeof(status));
20675262912eSEli Cohen 		vringh_complete_iotlb(&cvq->vring, cvq->head, write);
20685262912eSEli Cohen 		vringh_kiov_cleanup(&cvq->riov);
20695262912eSEli Cohen 		vringh_kiov_cleanup(&cvq->wiov);
20705262912eSEli Cohen 
20715262912eSEli Cohen 		if (vringh_need_notify_iotlb(&cvq->vring))
20725262912eSEli Cohen 			vringh_notify(&cvq->vring);
207355ebf0d6SJason Wang 
20741892a3d4SEli Cohen 		cvq->completed_desc++;
207555ebf0d6SJason Wang 		queue_work(mvdev->wq, &wqent->work);
207655ebf0d6SJason Wang 		break;
20775262912eSEli Cohen 	}
20781c80cf03SJason Wang 
20791c80cf03SJason Wang out:
2080759ae7f9SEli Cohen 	up_write(&ndev->reslock);
20815262912eSEli Cohen }
20825262912eSEli Cohen 
mlx5_vdpa_kick_vq(struct vdpa_device * vdev,u16 idx)20831a86b377SEli Cohen static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx)
20841a86b377SEli Cohen {
20851a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
20861a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
20875262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
20881a86b377SEli Cohen 
2089e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
2090e4fc6650SEli Cohen 		return;
2091e4fc6650SEli Cohen 
20925262912eSEli Cohen 	if (unlikely(is_ctrl_vq_idx(mvdev, idx))) {
2093ad6dc1daSEli Cohen 		if (!mvdev->wq || !mvdev->cvq.ready)
20945262912eSEli Cohen 			return;
20955262912eSEli Cohen 
209655ebf0d6SJason Wang 		queue_work(mvdev->wq, &ndev->cvq_ent.work);
20975262912eSEli Cohen 		return;
20985262912eSEli Cohen 	}
20995262912eSEli Cohen 
21005262912eSEli Cohen 	mvq = &ndev->vqs[idx];
21011a86b377SEli Cohen 	if (unlikely(!mvq->ready))
21021a86b377SEli Cohen 		return;
21031a86b377SEli Cohen 
21041a86b377SEli Cohen 	iowrite16(idx, ndev->mvdev.res.kick_addr);
21051a86b377SEli Cohen }
21061a86b377SEli Cohen 
mlx5_vdpa_set_vq_address(struct vdpa_device * vdev,u16 idx,u64 desc_area,u64 driver_area,u64 device_area)21071a86b377SEli Cohen static int mlx5_vdpa_set_vq_address(struct vdpa_device *vdev, u16 idx, u64 desc_area,
21081a86b377SEli Cohen 				    u64 driver_area, u64 device_area)
21091a86b377SEli Cohen {
21101a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
21111a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
21125262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
21131a86b377SEli Cohen 
2114e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
2115e4fc6650SEli Cohen 		return -EINVAL;
2116e4fc6650SEli Cohen 
21175262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
21185262912eSEli Cohen 		mvdev->cvq.desc_addr = desc_area;
21195262912eSEli Cohen 		mvdev->cvq.device_addr = device_area;
21205262912eSEli Cohen 		mvdev->cvq.driver_addr = driver_area;
21215262912eSEli Cohen 		return 0;
21225262912eSEli Cohen 	}
21235262912eSEli Cohen 
21245262912eSEli Cohen 	mvq = &ndev->vqs[idx];
21251a86b377SEli Cohen 	mvq->desc_addr = desc_area;
21261a86b377SEli Cohen 	mvq->device_addr = device_area;
21271a86b377SEli Cohen 	mvq->driver_addr = driver_area;
21281a86b377SEli Cohen 	return 0;
21291a86b377SEli Cohen }
21301a86b377SEli Cohen 
mlx5_vdpa_set_vq_num(struct vdpa_device * vdev,u16 idx,u32 num)21311a86b377SEli Cohen static void mlx5_vdpa_set_vq_num(struct vdpa_device *vdev, u16 idx, u32 num)
21321a86b377SEli Cohen {
21331a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
21341a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
21351a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
21361a86b377SEli Cohen 
2137*bf6f4d5dSJonah Palmer 	if (!is_index_valid(mvdev, idx))
2138e4fc6650SEli Cohen 		return;
2139e4fc6650SEli Cohen 
2140*bf6f4d5dSJonah Palmer         if (is_ctrl_vq_idx(mvdev, idx)) {
2141*bf6f4d5dSJonah Palmer                 struct mlx5_control_vq *cvq = &mvdev->cvq;
2142*bf6f4d5dSJonah Palmer 
2143*bf6f4d5dSJonah Palmer                 cvq->vring.vring.num = num;
2144*bf6f4d5dSJonah Palmer                 return;
2145*bf6f4d5dSJonah Palmer         }
2146*bf6f4d5dSJonah Palmer 
21471a86b377SEli Cohen 	mvq = &ndev->vqs[idx];
21481a86b377SEli Cohen 	mvq->num_ent = num;
21491a86b377SEli Cohen }
21501a86b377SEli Cohen 
mlx5_vdpa_set_vq_cb(struct vdpa_device * vdev,u16 idx,struct vdpa_callback * cb)21511a86b377SEli Cohen static void mlx5_vdpa_set_vq_cb(struct vdpa_device *vdev, u16 idx, struct vdpa_callback *cb)
21521a86b377SEli Cohen {
21531a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
21541a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
21551a86b377SEli Cohen 
2156db296d25SEli Cohen 	ndev->event_cbs[idx] = *cb;
215740f2f3e9SEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx))
215840f2f3e9SEli Cohen 		mvdev->cvq.event_cb = *cb;
21591a86b377SEli Cohen }
21601a86b377SEli Cohen 
mlx5_cvq_notify(struct vringh * vring)21615262912eSEli Cohen static void mlx5_cvq_notify(struct vringh *vring)
21625262912eSEli Cohen {
21635262912eSEli Cohen 	struct mlx5_control_vq *cvq = container_of(vring, struct mlx5_control_vq, vring);
21645262912eSEli Cohen 
21655262912eSEli Cohen 	if (!cvq->event_cb.callback)
21665262912eSEli Cohen 		return;
21675262912eSEli Cohen 
21685262912eSEli Cohen 	cvq->event_cb.callback(cvq->event_cb.private);
21695262912eSEli Cohen }
21705262912eSEli Cohen 
set_cvq_ready(struct mlx5_vdpa_dev * mvdev,bool ready)21715262912eSEli Cohen static void set_cvq_ready(struct mlx5_vdpa_dev *mvdev, bool ready)
21725262912eSEli Cohen {
21735262912eSEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
21745262912eSEli Cohen 
21755262912eSEli Cohen 	cvq->ready = ready;
21765262912eSEli Cohen 	if (!ready)
21775262912eSEli Cohen 		return;
21785262912eSEli Cohen 
21795262912eSEli Cohen 	cvq->vring.notify = mlx5_cvq_notify;
21801a86b377SEli Cohen }
21811a86b377SEli Cohen 
mlx5_vdpa_set_vq_ready(struct vdpa_device * vdev,u16 idx,bool ready)21821a86b377SEli Cohen static void mlx5_vdpa_set_vq_ready(struct vdpa_device *vdev, u16 idx, bool ready)
21831a86b377SEli Cohen {
21841a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
21851a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
21865262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
2187cae15c2eSEli Cohen 	int err;
21881a86b377SEli Cohen 
2189759be899SEli Cohen 	if (!mvdev->actual_features)
2190759be899SEli Cohen 		return;
2191759be899SEli Cohen 
2192e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
2193e4fc6650SEli Cohen 		return;
2194e4fc6650SEli Cohen 
21955262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
21965262912eSEli Cohen 		set_cvq_ready(mvdev, ready);
21975262912eSEli Cohen 		return;
21985262912eSEli Cohen 	}
21995262912eSEli Cohen 
22005262912eSEli Cohen 	mvq = &ndev->vqs[idx];
2201cae15c2eSEli Cohen 	if (!ready) {
22021a86b377SEli Cohen 		suspend_vq(ndev, mvq);
2203cae15c2eSEli Cohen 	} else {
2204cae15c2eSEli Cohen 		err = modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY);
2205cae15c2eSEli Cohen 		if (err) {
2206cae15c2eSEli Cohen 			mlx5_vdpa_warn(mvdev, "modify VQ %d to ready failed (%d)\n", idx, err);
2207cae15c2eSEli Cohen 			ready = false;
2208cae15c2eSEli Cohen 		}
2209cae15c2eSEli Cohen 	}
2210cae15c2eSEli Cohen 
22111a86b377SEli Cohen 
22121a86b377SEli Cohen 	mvq->ready = ready;
22131a86b377SEli Cohen }
22141a86b377SEli Cohen 
mlx5_vdpa_get_vq_ready(struct vdpa_device * vdev,u16 idx)22151a86b377SEli Cohen static bool mlx5_vdpa_get_vq_ready(struct vdpa_device *vdev, u16 idx)
22161a86b377SEli Cohen {
22171a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22181a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
22191a86b377SEli Cohen 
2220e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
2221e4fc6650SEli Cohen 		return false;
2222e4fc6650SEli Cohen 
22235262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx))
22245262912eSEli Cohen 		return mvdev->cvq.ready;
22255262912eSEli Cohen 
22265262912eSEli Cohen 	return ndev->vqs[idx].ready;
22271a86b377SEli Cohen }
22281a86b377SEli Cohen 
mlx5_vdpa_set_vq_state(struct vdpa_device * vdev,u16 idx,const struct vdpa_vq_state * state)22291a86b377SEli Cohen static int mlx5_vdpa_set_vq_state(struct vdpa_device *vdev, u16 idx,
22301a86b377SEli Cohen 				  const struct vdpa_vq_state *state)
22311a86b377SEli Cohen {
22321a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22331a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
22345262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
22351a86b377SEli Cohen 
2236e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
2237e4fc6650SEli Cohen 		return -EINVAL;
2238e4fc6650SEli Cohen 
22395262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
22405262912eSEli Cohen 		mvdev->cvq.vring.last_avail_idx = state->split.avail_index;
22415262912eSEli Cohen 		return 0;
22425262912eSEli Cohen 	}
22435262912eSEli Cohen 
22445262912eSEli Cohen 	mvq = &ndev->vqs[idx];
22451a86b377SEli Cohen 	if (mvq->fw_state == MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY) {
22461a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "can't modify available index\n");
22471a86b377SEli Cohen 		return -EINVAL;
22481a86b377SEli Cohen 	}
22491a86b377SEli Cohen 
2250530a5678SJason Wang 	mvq->used_idx = state->split.avail_index;
2251530a5678SJason Wang 	mvq->avail_idx = state->split.avail_index;
22521a86b377SEli Cohen 	return 0;
22531a86b377SEli Cohen }
22541a86b377SEli Cohen 
mlx5_vdpa_get_vq_state(struct vdpa_device * vdev,u16 idx,struct vdpa_vq_state * state)22551a86b377SEli Cohen static int mlx5_vdpa_get_vq_state(struct vdpa_device *vdev, u16 idx, struct vdpa_vq_state *state)
22561a86b377SEli Cohen {
22571a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22581a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
22595262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
22601a86b377SEli Cohen 	struct mlx5_virtq_attr attr;
22611a86b377SEli Cohen 	int err;
22621a86b377SEli Cohen 
2263e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
2264e4fc6650SEli Cohen 		return -EINVAL;
2265e4fc6650SEli Cohen 
22665262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
22675262912eSEli Cohen 		state->split.avail_index = mvdev->cvq.vring.last_avail_idx;
22685262912eSEli Cohen 		return 0;
22695262912eSEli Cohen 	}
22705262912eSEli Cohen 
22715262912eSEli Cohen 	mvq = &ndev->vqs[idx];
22723176e974SSi-Wei Liu 	/* If the virtq object was destroyed, use the value saved at
22733176e974SSi-Wei Liu 	 * the last minute of suspend_vq. This caters for userspace
22743176e974SSi-Wei Liu 	 * that cares about emulating the index after vq is stopped.
22753176e974SSi-Wei Liu 	 */
22763176e974SSi-Wei Liu 	if (!mvq->initialized) {
2277bc04d93eSEli Cohen 		/* Firmware returns a wrong value for the available index.
2278bc04d93eSEli Cohen 		 * Since both values should be identical, we take the value of
2279bc04d93eSEli Cohen 		 * used_idx which is reported correctly.
2280bc04d93eSEli Cohen 		 */
2281530a5678SJason Wang 		state->split.avail_index = mvq->used_idx;
22823176e974SSi-Wei Liu 		return 0;
22833176e974SSi-Wei Liu 	}
22841a86b377SEli Cohen 
22851a86b377SEli Cohen 	err = query_virtqueue(ndev, mvq, &attr);
22861a86b377SEli Cohen 	if (err) {
22871a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "failed to query virtqueue\n");
22881a86b377SEli Cohen 		return err;
22891a86b377SEli Cohen 	}
2290530a5678SJason Wang 	state->split.avail_index = attr.used_index;
22911a86b377SEli Cohen 	return 0;
22921a86b377SEli Cohen }
22931a86b377SEli Cohen 
mlx5_vdpa_get_vq_align(struct vdpa_device * vdev)22941a86b377SEli Cohen static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
22951a86b377SEli Cohen {
22961a86b377SEli Cohen 	return PAGE_SIZE;
22971a86b377SEli Cohen }
22981a86b377SEli Cohen 
mlx5_vdpa_get_vq_group(struct vdpa_device * vdev,u16 idx)22998fcd20c3SEli Cohen static u32 mlx5_vdpa_get_vq_group(struct vdpa_device *vdev, u16 idx)
2300d4821902SGautam Dawar {
23018fcd20c3SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
23028fcd20c3SEli Cohen 
23038fcd20c3SEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx))
23048fcd20c3SEli Cohen 		return MLX5_VDPA_CVQ_GROUP;
23058fcd20c3SEli Cohen 
23068fcd20c3SEli Cohen 	return MLX5_VDPA_DATAVQ_GROUP;
2307d4821902SGautam Dawar }
2308d4821902SGautam Dawar 
mlx_to_vritio_features(u16 dev_features)23091a86b377SEli Cohen static u64 mlx_to_vritio_features(u16 dev_features)
23101a86b377SEli Cohen {
23111a86b377SEli Cohen 	u64 result = 0;
23121a86b377SEli Cohen 
2313e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_MRG_RXBUF))
2314e9d67e59SEli Cohen 		result |= BIT_ULL(VIRTIO_NET_F_MRG_RXBUF);
2315e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_HOST_ECN))
2316e9d67e59SEli Cohen 		result |= BIT_ULL(VIRTIO_NET_F_HOST_ECN);
2317e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_GUEST_ECN))
2318e9d67e59SEli Cohen 		result |= BIT_ULL(VIRTIO_NET_F_GUEST_ECN);
2319e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_GUEST_TSO6))
2320e9d67e59SEli Cohen 		result |= BIT_ULL(VIRTIO_NET_F_GUEST_TSO6);
2321e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_GUEST_TSO4))
2322e9d67e59SEli Cohen 		result |= BIT_ULL(VIRTIO_NET_F_GUEST_TSO4);
2323e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_GUEST_CSUM))
2324cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_GUEST_CSUM);
2325e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_CSUM))
2326cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_CSUM);
2327e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_HOST_TSO6))
2328cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_HOST_TSO6);
2329e9d67e59SEli Cohen 	if (dev_features & BIT_ULL(MLX5_VIRTIO_NET_F_HOST_TSO4))
2330cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_HOST_TSO4);
23311a86b377SEli Cohen 
23321a86b377SEli Cohen 	return result;
23331a86b377SEli Cohen }
23341a86b377SEli Cohen 
get_supported_features(struct mlx5_core_dev * mdev)233579de65edSEli Cohen static u64 get_supported_features(struct mlx5_core_dev *mdev)
233679de65edSEli Cohen {
233779de65edSEli Cohen 	u64 mlx_vdpa_features = 0;
233879de65edSEli Cohen 	u16 dev_features;
233979de65edSEli Cohen 
234079de65edSEli Cohen 	dev_features = MLX5_CAP_DEV_VDPA_EMULATION(mdev, device_features_bits_mask);
234179de65edSEli Cohen 	mlx_vdpa_features |= mlx_to_vritio_features(dev_features);
234279de65edSEli Cohen 	if (MLX5_CAP_DEV_VDPA_EMULATION(mdev, virtio_version_1_0))
234379de65edSEli Cohen 		mlx_vdpa_features |= BIT_ULL(VIRTIO_F_VERSION_1);
234479de65edSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_F_ACCESS_PLATFORM);
234579de65edSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_CTRL_VQ);
234679de65edSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR);
234779de65edSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_MQ);
234879de65edSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_STATUS);
234979de65edSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_MTU);
2350baf2ad3fSEli Cohen 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_CTRL_VLAN);
2351deeacf35SSi-Wei Liu 	mlx_vdpa_features |= BIT_ULL(VIRTIO_NET_F_MAC);
235279de65edSEli Cohen 
235379de65edSEli Cohen 	return mlx_vdpa_features;
235479de65edSEli Cohen }
235579de65edSEli Cohen 
mlx5_vdpa_get_device_features(struct vdpa_device * vdev)2356a64917bcSEli Cohen static u64 mlx5_vdpa_get_device_features(struct vdpa_device *vdev)
23571a86b377SEli Cohen {
23581a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
23591a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
23605262912eSEli Cohen 
23611a86b377SEli Cohen 	print_features(mvdev, ndev->mvdev.mlx_features, false);
23621a86b377SEli Cohen 	return ndev->mvdev.mlx_features;
23631a86b377SEli Cohen }
23641a86b377SEli Cohen 
verify_driver_features(struct mlx5_vdpa_dev * mvdev,u64 features)236530c22f38SSi-Wei Liu static int verify_driver_features(struct mlx5_vdpa_dev *mvdev, u64 features)
23661a86b377SEli Cohen {
236730c22f38SSi-Wei Liu 	/* Minimum features to expect */
2368cbb52359SNathan Chancellor 	if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)))
23691a86b377SEli Cohen 		return -EOPNOTSUPP;
23701a86b377SEli Cohen 
237130c22f38SSi-Wei Liu 	/* Double check features combination sent down by the driver.
237230c22f38SSi-Wei Liu 	 * Fail invalid features due to absence of the depended feature.
237330c22f38SSi-Wei Liu 	 *
237430c22f38SSi-Wei Liu 	 * Per VIRTIO v1.1 specification, section 5.1.3.1 Feature bit
237530c22f38SSi-Wei Liu 	 * requirements: "VIRTIO_NET_F_MQ Requires VIRTIO_NET_F_CTRL_VQ".
237630c22f38SSi-Wei Liu 	 * By failing the invalid features sent down by untrusted drivers,
237730c22f38SSi-Wei Liu 	 * we're assured the assumption made upon is_index_valid() and
237830c22f38SSi-Wei Liu 	 * is_ctrl_vq_idx() will not be compromised.
237930c22f38SSi-Wei Liu 	 */
238030c22f38SSi-Wei Liu 	if ((features & (BIT_ULL(VIRTIO_NET_F_MQ) | BIT_ULL(VIRTIO_NET_F_CTRL_VQ))) ==
238130c22f38SSi-Wei Liu             BIT_ULL(VIRTIO_NET_F_MQ))
238230c22f38SSi-Wei Liu 		return -EINVAL;
238330c22f38SSi-Wei Liu 
23841a86b377SEli Cohen 	return 0;
23851a86b377SEli Cohen }
23861a86b377SEli Cohen 
setup_virtqueues(struct mlx5_vdpa_dev * mvdev)2387ae0428deSEli Cohen static int setup_virtqueues(struct mlx5_vdpa_dev *mvdev)
23881a86b377SEli Cohen {
2389ae0428deSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
23901a86b377SEli Cohen 	int err;
23911a86b377SEli Cohen 	int i;
23921a86b377SEli Cohen 
2393acde3929SEli Cohen 	for (i = 0; i < mvdev->max_vqs; i++) {
23941a86b377SEli Cohen 		err = setup_vq(ndev, &ndev->vqs[i]);
23951a86b377SEli Cohen 		if (err)
23961a86b377SEli Cohen 			goto err_vq;
23971a86b377SEli Cohen 	}
23981a86b377SEli Cohen 
23991a86b377SEli Cohen 	return 0;
24001a86b377SEli Cohen 
24011a86b377SEli Cohen err_vq:
24021a86b377SEli Cohen 	for (--i; i >= 0; i--)
24031a86b377SEli Cohen 		teardown_vq(ndev, &ndev->vqs[i]);
24041a86b377SEli Cohen 
24051a86b377SEli Cohen 	return err;
24061a86b377SEli Cohen }
24071a86b377SEli Cohen 
teardown_virtqueues(struct mlx5_vdpa_net * ndev)24081a86b377SEli Cohen static void teardown_virtqueues(struct mlx5_vdpa_net *ndev)
24091a86b377SEli Cohen {
24101a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
24111a86b377SEli Cohen 	int i;
24121a86b377SEli Cohen 
24131a86b377SEli Cohen 	for (i = ndev->mvdev.max_vqs - 1; i >= 0; i--) {
24141a86b377SEli Cohen 		mvq = &ndev->vqs[i];
24151a86b377SEli Cohen 		if (!mvq->initialized)
24161a86b377SEli Cohen 			continue;
24171a86b377SEli Cohen 
24181a86b377SEli Cohen 		teardown_vq(ndev, mvq);
24191a86b377SEli Cohen 	}
24201a86b377SEli Cohen }
24211a86b377SEli Cohen 
update_cvq_info(struct mlx5_vdpa_dev * mvdev)2422e4fc6650SEli Cohen static void update_cvq_info(struct mlx5_vdpa_dev *mvdev)
24238a7c3213SMichael S. Tsirkin {
2424e4fc6650SEli Cohen 	if (MLX5_FEATURE(mvdev, VIRTIO_NET_F_CTRL_VQ)) {
2425e4fc6650SEli Cohen 		if (MLX5_FEATURE(mvdev, VIRTIO_NET_F_MQ)) {
2426e4fc6650SEli Cohen 			/* MQ supported. CVQ index is right above the last data virtqueue's */
2427e4fc6650SEli Cohen 			mvdev->max_idx = mvdev->max_vqs;
2428e4fc6650SEli Cohen 		} else {
2429e4fc6650SEli Cohen 			/* Only CVQ supportted. data virtqueues occupy indices 0 and 1.
2430e4fc6650SEli Cohen 			 * CVQ gets index 2
2431e4fc6650SEli Cohen 			 */
2432e4fc6650SEli Cohen 			mvdev->max_idx = 2;
24338a7c3213SMichael S. Tsirkin 		}
2434e4fc6650SEli Cohen 	} else {
2435e4fc6650SEli Cohen 		/* Two data virtqueues only: one for rx and one for tx */
2436e4fc6650SEli Cohen 		mvdev->max_idx = 1;
2437e4fc6650SEli Cohen 	}
243836bdcf31SEli Cohen }
243936bdcf31SEli Cohen 
query_vport_state(struct mlx5_core_dev * mdev,u8 opmod,u16 vport)2440c384c240SEli Cohen static u8 query_vport_state(struct mlx5_core_dev *mdev, u8 opmod, u16 vport)
2441c384c240SEli Cohen {
2442c384c240SEli Cohen 	u32 out[MLX5_ST_SZ_DW(query_vport_state_out)] = {};
2443c384c240SEli Cohen 	u32 in[MLX5_ST_SZ_DW(query_vport_state_in)] = {};
2444c384c240SEli Cohen 	int err;
2445c384c240SEli Cohen 
2446c384c240SEli Cohen 	MLX5_SET(query_vport_state_in, in, opcode, MLX5_CMD_OP_QUERY_VPORT_STATE);
2447c384c240SEli Cohen 	MLX5_SET(query_vport_state_in, in, op_mod, opmod);
2448c384c240SEli Cohen 	MLX5_SET(query_vport_state_in, in, vport_number, vport);
2449c384c240SEli Cohen 	if (vport)
2450c384c240SEli Cohen 		MLX5_SET(query_vport_state_in, in, other_vport, 1);
2451c384c240SEli Cohen 
2452c384c240SEli Cohen 	err = mlx5_cmd_exec_inout(mdev, query_vport_state, in, out);
2453c384c240SEli Cohen 	if (err)
2454c384c240SEli Cohen 		return 0;
2455c384c240SEli Cohen 
2456c384c240SEli Cohen 	return MLX5_GET(query_vport_state_out, out, state);
2457c384c240SEli Cohen }
2458c384c240SEli Cohen 
get_link_state(struct mlx5_vdpa_dev * mvdev)2459c384c240SEli Cohen static bool get_link_state(struct mlx5_vdpa_dev *mvdev)
2460c384c240SEli Cohen {
2461c384c240SEli Cohen 	if (query_vport_state(mvdev->mdev, MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT, 0) ==
2462c384c240SEli Cohen 	    VPORT_STATE_UP)
2463c384c240SEli Cohen 		return true;
2464c384c240SEli Cohen 
2465c384c240SEli Cohen 	return false;
2466c384c240SEli Cohen }
2467c384c240SEli Cohen 
update_carrier(struct work_struct * work)2468c384c240SEli Cohen static void update_carrier(struct work_struct *work)
2469c384c240SEli Cohen {
2470c384c240SEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
2471c384c240SEli Cohen 	struct mlx5_vdpa_dev *mvdev;
2472c384c240SEli Cohen 	struct mlx5_vdpa_net *ndev;
2473c384c240SEli Cohen 
2474c384c240SEli Cohen 	wqent = container_of(work, struct mlx5_vdpa_wq_ent, work);
2475c384c240SEli Cohen 	mvdev = wqent->mvdev;
2476c384c240SEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
2477c384c240SEli Cohen 	if (get_link_state(mvdev))
2478c384c240SEli Cohen 		ndev->config.status |= cpu_to_mlx5vdpa16(mvdev, VIRTIO_NET_S_LINK_UP);
2479c384c240SEli Cohen 	else
2480c384c240SEli Cohen 		ndev->config.status &= cpu_to_mlx5vdpa16(mvdev, ~VIRTIO_NET_S_LINK_UP);
2481c384c240SEli Cohen 
2482c384c240SEli Cohen 	if (ndev->config_cb.callback)
2483c384c240SEli Cohen 		ndev->config_cb.callback(ndev->config_cb.private);
2484c384c240SEli Cohen 
2485c384c240SEli Cohen 	kfree(wqent);
2486c384c240SEli Cohen }
2487c384c240SEli Cohen 
queue_link_work(struct mlx5_vdpa_net * ndev)2488c384c240SEli Cohen static int queue_link_work(struct mlx5_vdpa_net *ndev)
2489c384c240SEli Cohen {
2490c384c240SEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
2491c384c240SEli Cohen 
2492c384c240SEli Cohen 	wqent = kzalloc(sizeof(*wqent), GFP_ATOMIC);
2493c384c240SEli Cohen 	if (!wqent)
2494c384c240SEli Cohen 		return -ENOMEM;
2495c384c240SEli Cohen 
2496c384c240SEli Cohen 	wqent->mvdev = &ndev->mvdev;
2497c384c240SEli Cohen 	INIT_WORK(&wqent->work, update_carrier);
2498c384c240SEli Cohen 	queue_work(ndev->mvdev.wq, &wqent->work);
2499c384c240SEli Cohen 	return 0;
2500c384c240SEli Cohen }
2501c384c240SEli Cohen 
event_handler(struct notifier_block * nb,unsigned long event,void * param)2502c384c240SEli Cohen static int event_handler(struct notifier_block *nb, unsigned long event, void *param)
2503c384c240SEli Cohen {
2504c384c240SEli Cohen 	struct mlx5_vdpa_net *ndev = container_of(nb, struct mlx5_vdpa_net, nb);
2505c384c240SEli Cohen 	struct mlx5_eqe *eqe = param;
2506c384c240SEli Cohen 	int ret = NOTIFY_DONE;
2507c384c240SEli Cohen 
2508c384c240SEli Cohen 	if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
2509c384c240SEli Cohen 		switch (eqe->sub_type) {
2510c384c240SEli Cohen 		case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2511c384c240SEli Cohen 		case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2512c384c240SEli Cohen 			if (queue_link_work(ndev))
2513c384c240SEli Cohen 				return NOTIFY_DONE;
2514c384c240SEli Cohen 
2515c384c240SEli Cohen 			ret = NOTIFY_OK;
2516c384c240SEli Cohen 			break;
2517c384c240SEli Cohen 		default:
2518c384c240SEli Cohen 			return NOTIFY_DONE;
2519c384c240SEli Cohen 		}
2520c384c240SEli Cohen 		return ret;
2521c384c240SEli Cohen 	}
2522c384c240SEli Cohen 	return ret;
2523c384c240SEli Cohen }
2524c384c240SEli Cohen 
register_link_notifier(struct mlx5_vdpa_net * ndev)2525c384c240SEli Cohen static void register_link_notifier(struct mlx5_vdpa_net *ndev)
2526c384c240SEli Cohen {
2527c384c240SEli Cohen 	if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_STATUS)))
2528c384c240SEli Cohen 		return;
2529c384c240SEli Cohen 
2530c384c240SEli Cohen 	ndev->nb.notifier_call = event_handler;
2531c384c240SEli Cohen 	mlx5_notifier_register(ndev->mvdev.mdev, &ndev->nb);
2532c384c240SEli Cohen 	ndev->nb_registered = true;
2533c384c240SEli Cohen 	queue_link_work(ndev);
2534c384c240SEli Cohen }
2535c384c240SEli Cohen 
unregister_link_notifier(struct mlx5_vdpa_net * ndev)2536c384c240SEli Cohen static void unregister_link_notifier(struct mlx5_vdpa_net *ndev)
2537c384c240SEli Cohen {
2538c384c240SEli Cohen 	if (!ndev->nb_registered)
2539c384c240SEli Cohen 		return;
2540c384c240SEli Cohen 
2541c384c240SEli Cohen 	ndev->nb_registered = false;
2542c384c240SEli Cohen 	mlx5_notifier_unregister(ndev->mvdev.mdev, &ndev->nb);
2543c384c240SEli Cohen 	if (ndev->mvdev.wq)
2544c384c240SEli Cohen 		flush_workqueue(ndev->mvdev.wq);
2545c384c240SEli Cohen }
2546c384c240SEli Cohen 
mlx5_vdpa_set_driver_features(struct vdpa_device * vdev,u64 features)2547a64917bcSEli Cohen static int mlx5_vdpa_set_driver_features(struct vdpa_device *vdev, u64 features)
25481a86b377SEli Cohen {
25491a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
25501a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
25511a86b377SEli Cohen 	int err;
25521a86b377SEli Cohen 
25531a86b377SEli Cohen 	print_features(mvdev, features, true);
25541a86b377SEli Cohen 
255530c22f38SSi-Wei Liu 	err = verify_driver_features(mvdev, features);
25561a86b377SEli Cohen 	if (err)
25571a86b377SEli Cohen 		return err;
25581a86b377SEli Cohen 
25591a86b377SEli Cohen 	ndev->mvdev.actual_features = features & ndev->mvdev.mlx_features;
2560b03fc43eSEli Cohen 	if (ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_MQ))
2561acde3929SEli Cohen 		ndev->rqt_size = mlx5vdpa16_to_cpu(mvdev, ndev->config.max_virtqueue_pairs);
2562b03fc43eSEli Cohen 	else
2563acde3929SEli Cohen 		ndev->rqt_size = 1;
2564acde3929SEli Cohen 
25653fe02419SDragos Tatulea 	/* Device must start with 1 queue pair, as per VIRTIO v1.2 spec, section
25663fe02419SDragos Tatulea 	 * 5.1.6.5.5 "Device operation in multiqueue mode":
25673fe02419SDragos Tatulea 	 *
25683fe02419SDragos Tatulea 	 * Multiqueue is disabled by default.
25693fe02419SDragos Tatulea 	 * The driver enables multiqueue by sending a command using class
25703fe02419SDragos Tatulea 	 * VIRTIO_NET_CTRL_MQ. The command selects the mode of multiqueue
25713fe02419SDragos Tatulea 	 * operation, as follows: ...
25723fe02419SDragos Tatulea 	 */
25733fe02419SDragos Tatulea 	ndev->cur_num_vqs = 2;
2574b03fc43eSEli Cohen 
2575e4fc6650SEli Cohen 	update_cvq_info(mvdev);
25761a86b377SEli Cohen 	return err;
25771a86b377SEli Cohen }
25781a86b377SEli Cohen 
mlx5_vdpa_set_config_cb(struct vdpa_device * vdev,struct vdpa_callback * cb)25791a86b377SEli Cohen static void mlx5_vdpa_set_config_cb(struct vdpa_device *vdev, struct vdpa_callback *cb)
25801a86b377SEli Cohen {
2581edf747afSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
2582edf747afSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
2583edf747afSEli Cohen 
2584edf747afSEli Cohen 	ndev->config_cb = *cb;
25851a86b377SEli Cohen }
25861a86b377SEli Cohen 
25871a86b377SEli Cohen #define MLX5_VDPA_MAX_VQ_ENTRIES 256
mlx5_vdpa_get_vq_num_max(struct vdpa_device * vdev)25881a86b377SEli Cohen static u16 mlx5_vdpa_get_vq_num_max(struct vdpa_device *vdev)
25891a86b377SEli Cohen {
25901a86b377SEli Cohen 	return MLX5_VDPA_MAX_VQ_ENTRIES;
25911a86b377SEli Cohen }
25921a86b377SEli Cohen 
mlx5_vdpa_get_device_id(struct vdpa_device * vdev)25931a86b377SEli Cohen static u32 mlx5_vdpa_get_device_id(struct vdpa_device *vdev)
25941a86b377SEli Cohen {
25951a86b377SEli Cohen 	return VIRTIO_ID_NET;
25961a86b377SEli Cohen }
25971a86b377SEli Cohen 
mlx5_vdpa_get_vendor_id(struct vdpa_device * vdev)25981a86b377SEli Cohen static u32 mlx5_vdpa_get_vendor_id(struct vdpa_device *vdev)
25991a86b377SEli Cohen {
26001a86b377SEli Cohen 	return PCI_VENDOR_ID_MELLANOX;
26011a86b377SEli Cohen }
26021a86b377SEli Cohen 
mlx5_vdpa_get_status(struct vdpa_device * vdev)26031a86b377SEli Cohen static u8 mlx5_vdpa_get_status(struct vdpa_device *vdev)
26041a86b377SEli Cohen {
26051a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
26061a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
26071a86b377SEli Cohen 
26081a86b377SEli Cohen 	print_status(mvdev, ndev->mvdev.status, false);
26091a86b377SEli Cohen 	return ndev->mvdev.status;
26101a86b377SEli Cohen }
26111a86b377SEli Cohen 
save_channel_info(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq)26121a86b377SEli Cohen static int save_channel_info(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
26131a86b377SEli Cohen {
26141a86b377SEli Cohen 	struct mlx5_vq_restore_info *ri = &mvq->ri;
261552893733SEli Cohen 	struct mlx5_virtq_attr attr = {};
26161a86b377SEli Cohen 	int err;
26171a86b377SEli Cohen 
261852893733SEli Cohen 	if (mvq->initialized) {
26191a86b377SEli Cohen 		err = query_virtqueue(ndev, mvq, &attr);
26201a86b377SEli Cohen 		if (err)
26211a86b377SEli Cohen 			return err;
262252893733SEli Cohen 	}
26231a86b377SEli Cohen 
26241a86b377SEli Cohen 	ri->avail_index = attr.available_index;
2625b35ccebeSEli Cohen 	ri->used_index = attr.used_index;
26261a86b377SEli Cohen 	ri->ready = mvq->ready;
26271a86b377SEli Cohen 	ri->num_ent = mvq->num_ent;
26281a86b377SEli Cohen 	ri->desc_addr = mvq->desc_addr;
26291a86b377SEli Cohen 	ri->device_addr = mvq->device_addr;
26301a86b377SEli Cohen 	ri->driver_addr = mvq->driver_addr;
2631bc9a2b3eSEli Cohen 	ri->map = mvq->map;
26321a86b377SEli Cohen 	ri->restore = true;
26331a86b377SEli Cohen 	return 0;
26341a86b377SEli Cohen }
26351a86b377SEli Cohen 
save_channels_info(struct mlx5_vdpa_net * ndev)26361a86b377SEli Cohen static int save_channels_info(struct mlx5_vdpa_net *ndev)
26371a86b377SEli Cohen {
26381a86b377SEli Cohen 	int i;
26391a86b377SEli Cohen 
26401a86b377SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++) {
26411a86b377SEli Cohen 		memset(&ndev->vqs[i].ri, 0, sizeof(ndev->vqs[i].ri));
26421a86b377SEli Cohen 		save_channel_info(ndev, &ndev->vqs[i]);
26431a86b377SEli Cohen 	}
26441a86b377SEli Cohen 	return 0;
26451a86b377SEli Cohen }
26461a86b377SEli Cohen 
mlx5_clear_vqs(struct mlx5_vdpa_net * ndev)26471a86b377SEli Cohen static void mlx5_clear_vqs(struct mlx5_vdpa_net *ndev)
26481a86b377SEli Cohen {
26491a86b377SEli Cohen 	int i;
26501a86b377SEli Cohen 
26511a86b377SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++)
26521a86b377SEli Cohen 		memset(&ndev->vqs[i], 0, offsetof(struct mlx5_vdpa_virtqueue, ri));
26531a86b377SEli Cohen }
26541a86b377SEli Cohen 
restore_channels_info(struct mlx5_vdpa_net * ndev)26551a86b377SEli Cohen static void restore_channels_info(struct mlx5_vdpa_net *ndev)
26561a86b377SEli Cohen {
26571a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
26581a86b377SEli Cohen 	struct mlx5_vq_restore_info *ri;
26591a86b377SEli Cohen 	int i;
26601a86b377SEli Cohen 
26611a86b377SEli Cohen 	mlx5_clear_vqs(ndev);
26621a86b377SEli Cohen 	init_mvqs(ndev);
26631a86b377SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++) {
26641a86b377SEli Cohen 		mvq = &ndev->vqs[i];
26651a86b377SEli Cohen 		ri = &mvq->ri;
26661a86b377SEli Cohen 		if (!ri->restore)
26671a86b377SEli Cohen 			continue;
26681a86b377SEli Cohen 
26691a86b377SEli Cohen 		mvq->avail_idx = ri->avail_index;
2670b35ccebeSEli Cohen 		mvq->used_idx = ri->used_index;
26711a86b377SEli Cohen 		mvq->ready = ri->ready;
26721a86b377SEli Cohen 		mvq->num_ent = ri->num_ent;
26731a86b377SEli Cohen 		mvq->desc_addr = ri->desc_addr;
26741a86b377SEli Cohen 		mvq->device_addr = ri->device_addr;
26751a86b377SEli Cohen 		mvq->driver_addr = ri->driver_addr;
2676bc9a2b3eSEli Cohen 		mvq->map = ri->map;
26771a86b377SEli Cohen 	}
26781a86b377SEli Cohen }
26791a86b377SEli Cohen 
mlx5_vdpa_change_map(struct mlx5_vdpa_dev * mvdev,struct vhost_iotlb * iotlb,unsigned int asid)268038fc462fSEli Cohen static int mlx5_vdpa_change_map(struct mlx5_vdpa_dev *mvdev,
268138fc462fSEli Cohen 				struct vhost_iotlb *iotlb, unsigned int asid)
26821a86b377SEli Cohen {
2683ae0428deSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
26841a86b377SEli Cohen 	int err;
26851a86b377SEli Cohen 
26861a86b377SEli Cohen 	suspend_vqs(ndev);
26871a86b377SEli Cohen 	err = save_channels_info(ndev);
26881a86b377SEli Cohen 	if (err)
26891a86b377SEli Cohen 		goto err_mr;
26901a86b377SEli Cohen 
26911a86b377SEli Cohen 	teardown_driver(ndev);
2692ad03a0f4SEugenio Pérez 	mlx5_vdpa_destroy_mr_asid(mvdev, asid);
269338fc462fSEli Cohen 	err = mlx5_vdpa_create_mr(mvdev, iotlb, asid);
26941a86b377SEli Cohen 	if (err)
26951a86b377SEli Cohen 		goto err_mr;
26961a86b377SEli Cohen 
269709e65ee9SSi-Wei Liu 	if (!(mvdev->status & VIRTIO_CONFIG_S_DRIVER_OK) || mvdev->suspended)
26981c80cf03SJason Wang 		goto err_mr;
26991897f0b6SEli Cohen 
27001a86b377SEli Cohen 	restore_channels_info(ndev);
2701ae0428deSEli Cohen 	err = setup_driver(mvdev);
27021a86b377SEli Cohen 	if (err)
27031a86b377SEli Cohen 		goto err_setup;
27041a86b377SEli Cohen 
27051a86b377SEli Cohen 	return 0;
27061a86b377SEli Cohen 
27071a86b377SEli Cohen err_setup:
2708ad03a0f4SEugenio Pérez 	mlx5_vdpa_destroy_mr_asid(mvdev, asid);
27091a86b377SEli Cohen err_mr:
27101a86b377SEli Cohen 	return err;
27111a86b377SEli Cohen }
27121a86b377SEli Cohen 
27131c80cf03SJason Wang /* reslock must be held for this function */
setup_driver(struct mlx5_vdpa_dev * mvdev)2714ae0428deSEli Cohen static int setup_driver(struct mlx5_vdpa_dev *mvdev)
27151a86b377SEli Cohen {
2716ae0428deSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
27171a86b377SEli Cohen 	int err;
27181a86b377SEli Cohen 
2719759ae7f9SEli Cohen 	WARN_ON(!rwsem_is_locked(&ndev->reslock));
27201c80cf03SJason Wang 
27211a86b377SEli Cohen 	if (ndev->setup) {
2722ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "setup driver called for already setup driver\n");
27231a86b377SEli Cohen 		err = 0;
27241a86b377SEli Cohen 		goto out;
27251a86b377SEli Cohen 	}
2726f0417e72SEli Cohen 	mlx5_vdpa_add_debugfs(ndev);
2727abb0dcf9SDragos Tatulea 
2728abb0dcf9SDragos Tatulea 	err = read_umem_params(ndev);
2729abb0dcf9SDragos Tatulea 	if (err)
2730abb0dcf9SDragos Tatulea 		goto err_setup;
2731abb0dcf9SDragos Tatulea 
2732ae0428deSEli Cohen 	err = setup_virtqueues(mvdev);
27331a86b377SEli Cohen 	if (err) {
2734ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "setup_virtqueues\n");
2735f0417e72SEli Cohen 		goto err_setup;
27361a86b377SEli Cohen 	}
27371a86b377SEli Cohen 
27381a86b377SEli Cohen 	err = create_rqt(ndev);
27391a86b377SEli Cohen 	if (err) {
2740ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "create_rqt\n");
27411a86b377SEli Cohen 		goto err_rqt;
27421a86b377SEli Cohen 	}
27431a86b377SEli Cohen 
27441a86b377SEli Cohen 	err = create_tir(ndev);
27451a86b377SEli Cohen 	if (err) {
2746ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "create_tir\n");
27471a86b377SEli Cohen 		goto err_tir;
27481a86b377SEli Cohen 	}
27491a86b377SEli Cohen 
2750baf2ad3fSEli Cohen 	err = setup_steering(ndev);
27511a86b377SEli Cohen 	if (err) {
2752baf2ad3fSEli Cohen 		mlx5_vdpa_warn(mvdev, "setup_steering\n");
27531a86b377SEli Cohen 		goto err_fwd;
27541a86b377SEli Cohen 	}
27551a86b377SEli Cohen 	ndev->setup = true;
27561a86b377SEli Cohen 
27571a86b377SEli Cohen 	return 0;
27581a86b377SEli Cohen 
27591a86b377SEli Cohen err_fwd:
27601a86b377SEli Cohen 	destroy_tir(ndev);
27611a86b377SEli Cohen err_tir:
27621a86b377SEli Cohen 	destroy_rqt(ndev);
27631a86b377SEli Cohen err_rqt:
27641a86b377SEli Cohen 	teardown_virtqueues(ndev);
2765f0417e72SEli Cohen err_setup:
2766f8a3db47SDragos Tatulea 	mlx5_vdpa_remove_debugfs(ndev);
27671a86b377SEli Cohen out:
27681a86b377SEli Cohen 	return err;
27691a86b377SEli Cohen }
27701a86b377SEli Cohen 
27711c80cf03SJason Wang /* reslock must be held for this function */
teardown_driver(struct mlx5_vdpa_net * ndev)27721a86b377SEli Cohen static void teardown_driver(struct mlx5_vdpa_net *ndev)
27731a86b377SEli Cohen {
27741c80cf03SJason Wang 
2775759ae7f9SEli Cohen 	WARN_ON(!rwsem_is_locked(&ndev->reslock));
27761c80cf03SJason Wang 
27771a86b377SEli Cohen 	if (!ndev->setup)
27781c80cf03SJason Wang 		return;
27791a86b377SEli Cohen 
2780f8a3db47SDragos Tatulea 	mlx5_vdpa_remove_debugfs(ndev);
2781baf2ad3fSEli Cohen 	teardown_steering(ndev);
27821a86b377SEli Cohen 	destroy_tir(ndev);
27831a86b377SEli Cohen 	destroy_rqt(ndev);
27841a86b377SEli Cohen 	teardown_virtqueues(ndev);
27851a86b377SEli Cohen 	ndev->setup = false;
27861a86b377SEli Cohen }
27871a86b377SEli Cohen 
clear_vqs_ready(struct mlx5_vdpa_net * ndev)2788e3aadf2eSEli Cohen static void clear_vqs_ready(struct mlx5_vdpa_net *ndev)
2789e3aadf2eSEli Cohen {
2790e3aadf2eSEli Cohen 	int i;
2791e3aadf2eSEli Cohen 
2792e3aadf2eSEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++)
2793e3aadf2eSEli Cohen 		ndev->vqs[i].ready = false;
2794ef12e4bfSEli Cohen 
2795ef12e4bfSEli Cohen 	ndev->mvdev.cvq.ready = false;
2796e3aadf2eSEli Cohen }
2797e3aadf2eSEli Cohen 
setup_cvq_vring(struct mlx5_vdpa_dev * mvdev)2798ace92524SEli Cohen static int setup_cvq_vring(struct mlx5_vdpa_dev *mvdev)
2799ace92524SEli Cohen {
2800ace92524SEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
2801ace92524SEli Cohen 	int err = 0;
2802ace92524SEli Cohen 
2803dd3438abSSteve Sistare 	if (mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)) {
2804dd3438abSSteve Sistare 		u16 idx = cvq->vring.last_avail_idx;
2805dd3438abSSteve Sistare 
2806ace92524SEli Cohen 		err = vringh_init_iotlb(&cvq->vring, mvdev->actual_features,
2807*bf6f4d5dSJonah Palmer 					cvq->vring.vring.num, false,
2808ace92524SEli Cohen 					(struct vring_desc *)(uintptr_t)cvq->desc_addr,
2809ace92524SEli Cohen 					(struct vring_avail *)(uintptr_t)cvq->driver_addr,
2810ace92524SEli Cohen 					(struct vring_used *)(uintptr_t)cvq->device_addr);
2811ace92524SEli Cohen 
2812dd3438abSSteve Sistare 		if (!err)
2813dd3438abSSteve Sistare 			cvq->vring.last_avail_idx = cvq->vring.last_used_idx = idx;
2814dd3438abSSteve Sistare 	}
2815ace92524SEli Cohen 	return err;
2816ace92524SEli Cohen }
2817ace92524SEli Cohen 
mlx5_vdpa_set_status(struct vdpa_device * vdev,u8 status)28181a86b377SEli Cohen static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status)
28191a86b377SEli Cohen {
28201a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
28211a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
28221a86b377SEli Cohen 	int err;
28231a86b377SEli Cohen 
28241a86b377SEli Cohen 	print_status(mvdev, status, true);
28251a86b377SEli Cohen 
2826759ae7f9SEli Cohen 	down_write(&ndev->reslock);
28271c80cf03SJason Wang 
28281a86b377SEli Cohen 	if ((status ^ ndev->mvdev.status) & VIRTIO_CONFIG_S_DRIVER_OK) {
28291a86b377SEli Cohen 		if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
2830ace92524SEli Cohen 			err = setup_cvq_vring(mvdev);
2831ace92524SEli Cohen 			if (err) {
2832ace92524SEli Cohen 				mlx5_vdpa_warn(mvdev, "failed to setup control VQ vring\n");
2833ace92524SEli Cohen 				goto err_setup;
2834ace92524SEli Cohen 			}
2835c384c240SEli Cohen 			register_link_notifier(ndev);
2836ae0428deSEli Cohen 			err = setup_driver(mvdev);
28371a86b377SEli Cohen 			if (err) {
28381a86b377SEli Cohen 				mlx5_vdpa_warn(mvdev, "failed to setup driver\n");
2839c384c240SEli Cohen 				goto err_driver;
28401a86b377SEli Cohen 			}
28411a86b377SEli Cohen 		} else {
28421a86b377SEli Cohen 			mlx5_vdpa_warn(mvdev, "did not expect DRIVER_OK to be cleared\n");
28431c80cf03SJason Wang 			goto err_clear;
28441a86b377SEli Cohen 		}
28451a86b377SEli Cohen 	}
28461a86b377SEli Cohen 
28471a86b377SEli Cohen 	ndev->mvdev.status = status;
2848759ae7f9SEli Cohen 	up_write(&ndev->reslock);
28491a86b377SEli Cohen 	return;
28501a86b377SEli Cohen 
2851c384c240SEli Cohen err_driver:
2852c384c240SEli Cohen 	unregister_link_notifier(ndev);
28531a86b377SEli Cohen err_setup:
28541a86b377SEli Cohen 	mlx5_vdpa_destroy_mr(&ndev->mvdev);
28551a86b377SEli Cohen 	ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED;
28561c80cf03SJason Wang err_clear:
2857759ae7f9SEli Cohen 	up_write(&ndev->reslock);
28581a86b377SEli Cohen }
28591a86b377SEli Cohen 
init_group_to_asid_map(struct mlx5_vdpa_dev * mvdev)28608fcd20c3SEli Cohen static void init_group_to_asid_map(struct mlx5_vdpa_dev *mvdev)
28618fcd20c3SEli Cohen {
28628fcd20c3SEli Cohen 	int i;
28638fcd20c3SEli Cohen 
28648fcd20c3SEli Cohen 	/* default mapping all groups are mapped to asid 0 */
28658fcd20c3SEli Cohen 	for (i = 0; i < MLX5_VDPA_NUMVQ_GROUPS; i++)
28668fcd20c3SEli Cohen 		mvdev->group2asid[i] = 0;
28678fcd20c3SEli Cohen }
28688fcd20c3SEli Cohen 
mlx5_vdpa_reset(struct vdpa_device * vdev)28690686082dSXie Yongji static int mlx5_vdpa_reset(struct vdpa_device *vdev)
28700686082dSXie Yongji {
28710686082dSXie Yongji 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
28720686082dSXie Yongji 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
28730686082dSXie Yongji 
28740686082dSXie Yongji 	print_status(mvdev, 0, true);
28750686082dSXie Yongji 	mlx5_vdpa_info(mvdev, "performing device reset\n");
28761c80cf03SJason Wang 
2877759ae7f9SEli Cohen 	down_write(&ndev->reslock);
2878c384c240SEli Cohen 	unregister_link_notifier(ndev);
28790686082dSXie Yongji 	teardown_driver(ndev);
28800686082dSXie Yongji 	clear_vqs_ready(ndev);
28810686082dSXie Yongji 	mlx5_vdpa_destroy_mr(&ndev->mvdev);
28820686082dSXie Yongji 	ndev->mvdev.status = 0;
288309e65ee9SSi-Wei Liu 	ndev->mvdev.suspended = false;
2884b03fc43eSEli Cohen 	ndev->cur_num_vqs = 0;
28851892a3d4SEli Cohen 	ndev->mvdev.cvq.received_desc = 0;
28861892a3d4SEli Cohen 	ndev->mvdev.cvq.completed_desc = 0;
288775560522SEli Cohen 	memset(ndev->event_cbs, 0, sizeof(*ndev->event_cbs) * (mvdev->max_vqs + 1));
28880686082dSXie Yongji 	ndev->mvdev.actual_features = 0;
28898fcd20c3SEli Cohen 	init_group_to_asid_map(mvdev);
28900686082dSXie Yongji 	++mvdev->generation;
28918fcd20c3SEli Cohen 
28920686082dSXie Yongji 	if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) {
289338fc462fSEli Cohen 		if (mlx5_vdpa_create_mr(mvdev, NULL, 0))
28940686082dSXie Yongji 			mlx5_vdpa_warn(mvdev, "create MR failed\n");
28950686082dSXie Yongji 	}
2896759ae7f9SEli Cohen 	up_write(&ndev->reslock);
28970686082dSXie Yongji 
28980686082dSXie Yongji 	return 0;
28990686082dSXie Yongji }
29000686082dSXie Yongji 
mlx5_vdpa_get_config_size(struct vdpa_device * vdev)2901442706f9SStefano Garzarella static size_t mlx5_vdpa_get_config_size(struct vdpa_device *vdev)
2902442706f9SStefano Garzarella {
2903442706f9SStefano Garzarella 	return sizeof(struct virtio_net_config);
2904442706f9SStefano Garzarella }
2905442706f9SStefano Garzarella 
mlx5_vdpa_get_config(struct vdpa_device * vdev,unsigned int offset,void * buf,unsigned int len)29061a86b377SEli Cohen static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf,
29071a86b377SEli Cohen 				 unsigned int len)
29081a86b377SEli Cohen {
29091a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
29101a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
29111a86b377SEli Cohen 
2912dcfde163SStefano Garzarella 	if (offset + len <= sizeof(struct virtio_net_config))
29132874211fSDan Carpenter 		memcpy(buf, (u8 *)&ndev->config + offset, len);
29141a86b377SEli Cohen }
29151a86b377SEli Cohen 
mlx5_vdpa_set_config(struct vdpa_device * vdev,unsigned int offset,const void * buf,unsigned int len)29161a86b377SEli Cohen static void mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf,
29171a86b377SEli Cohen 				 unsigned int len)
29181a86b377SEli Cohen {
29191a86b377SEli Cohen 	/* not supported */
29201a86b377SEli Cohen }
29211a86b377SEli Cohen 
mlx5_vdpa_get_generation(struct vdpa_device * vdev)29221a86b377SEli Cohen static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
29231a86b377SEli Cohen {
29241a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
29251a86b377SEli Cohen 
29261a86b377SEli Cohen 	return mvdev->generation;
29271a86b377SEli Cohen }
29281a86b377SEli Cohen 
set_map_data(struct mlx5_vdpa_dev * mvdev,struct vhost_iotlb * iotlb,unsigned int asid)292938fc462fSEli Cohen static int set_map_data(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
293038fc462fSEli Cohen 			unsigned int asid)
29318fcd20c3SEli Cohen {
29321a86b377SEli Cohen 	bool change_map;
29331a86b377SEli Cohen 	int err;
29341a86b377SEli Cohen 
293538fc462fSEli Cohen 	err = mlx5_vdpa_handle_set_map(mvdev, iotlb, &change_map, asid);
29361a86b377SEli Cohen 	if (err) {
29371a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "set map failed(%d)\n", err);
29388fcd20c3SEli Cohen 		return err;
29391a86b377SEli Cohen 	}
29401a86b377SEli Cohen 
29411a86b377SEli Cohen 	if (change_map)
294238fc462fSEli Cohen 		err = mlx5_vdpa_change_map(mvdev, iotlb, asid);
29431a86b377SEli Cohen 
29448fcd20c3SEli Cohen 	return err;
29458fcd20c3SEli Cohen }
29468fcd20c3SEli Cohen 
mlx5_vdpa_set_map(struct vdpa_device * vdev,unsigned int asid,struct vhost_iotlb * iotlb)29478fcd20c3SEli Cohen static int mlx5_vdpa_set_map(struct vdpa_device *vdev, unsigned int asid,
29488fcd20c3SEli Cohen 			     struct vhost_iotlb *iotlb)
29498fcd20c3SEli Cohen {
29508fcd20c3SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
29518fcd20c3SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
295293e530d2SEli Cohen 	int err = -EINVAL;
29538fcd20c3SEli Cohen 
29548fcd20c3SEli Cohen 	down_write(&ndev->reslock);
295538fc462fSEli Cohen 	err = set_map_data(mvdev, iotlb, asid);
2956759ae7f9SEli Cohen 	up_write(&ndev->reslock);
29571c80cf03SJason Wang 	return err;
29581a86b377SEli Cohen }
29591a86b377SEli Cohen 
mlx5_get_vq_dma_dev(struct vdpa_device * vdev,u16 idx)296036871fb9SJason Wang static struct device *mlx5_get_vq_dma_dev(struct vdpa_device *vdev, u16 idx)
296136871fb9SJason Wang {
296236871fb9SJason Wang 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
296336871fb9SJason Wang 
296436871fb9SJason Wang 	if (is_ctrl_vq_idx(mvdev, idx))
296536871fb9SJason Wang 		return &vdev->dev;
296636871fb9SJason Wang 
296736871fb9SJason Wang 	return mvdev->vdev.dma_dev;
296836871fb9SJason Wang }
296936871fb9SJason Wang 
free_irqs(struct mlx5_vdpa_net * ndev)2970bc9a2b3eSEli Cohen static void free_irqs(struct mlx5_vdpa_net *ndev)
2971bc9a2b3eSEli Cohen {
2972bc9a2b3eSEli Cohen 	struct mlx5_vdpa_irq_pool_entry *ent;
2973bc9a2b3eSEli Cohen 	int i;
2974bc9a2b3eSEli Cohen 
2975bc9a2b3eSEli Cohen 	if (!msix_mode_supported(&ndev->mvdev))
2976bc9a2b3eSEli Cohen 		return;
2977bc9a2b3eSEli Cohen 
2978bc9a2b3eSEli Cohen 	if (!ndev->irqp.entries)
2979bc9a2b3eSEli Cohen 		return;
2980bc9a2b3eSEli Cohen 
2981bc9a2b3eSEli Cohen 	for (i = ndev->irqp.num_ent - 1; i >= 0; i--) {
2982bc9a2b3eSEli Cohen 		ent = ndev->irqp.entries + i;
2983bc9a2b3eSEli Cohen 		if (ent->map.virq)
2984bc9a2b3eSEli Cohen 			pci_msix_free_irq(ndev->mvdev.mdev->pdev, ent->map);
2985bc9a2b3eSEli Cohen 	}
2986bc9a2b3eSEli Cohen 	kfree(ndev->irqp.entries);
2987bc9a2b3eSEli Cohen }
2988bc9a2b3eSEli Cohen 
mlx5_vdpa_free(struct vdpa_device * vdev)29891a86b377SEli Cohen static void mlx5_vdpa_free(struct vdpa_device *vdev)
29901a86b377SEli Cohen {
29911a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
29927c9f131fSEli Cohen 	struct mlx5_core_dev *pfmdev;
29931a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev;
29941a86b377SEli Cohen 
29951a86b377SEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
29961a86b377SEli Cohen 
29971a86b377SEli Cohen 	free_resources(ndev);
29986f5312f8SEli Cohen 	mlx5_vdpa_destroy_mr(mvdev);
29997c9f131fSEli Cohen 	if (!is_zero_ether_addr(ndev->config.mac)) {
30007c9f131fSEli Cohen 		pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
30017c9f131fSEli Cohen 		mlx5_mpfs_del_mac(pfmdev, ndev->config.mac);
30027c9f131fSEli Cohen 	}
30031a86b377SEli Cohen 	mlx5_vdpa_free_resources(&ndev->mvdev);
3004bc9a2b3eSEli Cohen 	free_irqs(ndev);
300575560522SEli Cohen 	kfree(ndev->event_cbs);
300675560522SEli Cohen 	kfree(ndev->vqs);
30071a86b377SEli Cohen }
30081a86b377SEli Cohen 
mlx5_get_vq_notification(struct vdpa_device * vdev,u16 idx)30091a86b377SEli Cohen static struct vdpa_notification_area mlx5_get_vq_notification(struct vdpa_device *vdev, u16 idx)
30101a86b377SEli Cohen {
3011b57c46cbSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
30121a86b377SEli Cohen 	struct vdpa_notification_area ret = {};
3013b57c46cbSEli Cohen 	struct mlx5_vdpa_net *ndev;
3014b57c46cbSEli Cohen 	phys_addr_t addr;
30151a86b377SEli Cohen 
30165262912eSEli Cohen 	if (!is_index_valid(mvdev, idx) || is_ctrl_vq_idx(mvdev, idx))
3017e4fc6650SEli Cohen 		return ret;
3018e4fc6650SEli Cohen 
3019b57c46cbSEli Cohen 	/* If SF BAR size is smaller than PAGE_SIZE, do not use direct
3020b57c46cbSEli Cohen 	 * notification to avoid the risk of mapping pages that contain BAR of more
3021b57c46cbSEli Cohen 	 * than one SF
3022b57c46cbSEli Cohen 	 */
3023b57c46cbSEli Cohen 	if (MLX5_CAP_GEN(mvdev->mdev, log_min_sf_size) + 12 < PAGE_SHIFT)
3024b57c46cbSEli Cohen 		return ret;
3025b57c46cbSEli Cohen 
3026b57c46cbSEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
3027b57c46cbSEli Cohen 	addr = (phys_addr_t)ndev->mvdev.res.phys_kick_addr;
3028b57c46cbSEli Cohen 	ret.addr = addr;
3029b57c46cbSEli Cohen 	ret.size = PAGE_SIZE;
30301a86b377SEli Cohen 	return ret;
30311a86b377SEli Cohen }
30321a86b377SEli Cohen 
mlx5_get_vq_irq(struct vdpa_device * vdev,u16 idx)3033bc9a2b3eSEli Cohen static int mlx5_get_vq_irq(struct vdpa_device *vdev, u16 idx)
30341a86b377SEli Cohen {
3035bc9a2b3eSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
3036bc9a2b3eSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
3037bc9a2b3eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
3038bc9a2b3eSEli Cohen 
3039bc9a2b3eSEli Cohen 	if (!is_index_valid(mvdev, idx))
3040bc9a2b3eSEli Cohen 		return -EINVAL;
3041bc9a2b3eSEli Cohen 
3042bc9a2b3eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx))
30431a86b377SEli Cohen 		return -EOPNOTSUPP;
3044bc9a2b3eSEli Cohen 
3045bc9a2b3eSEli Cohen 	mvq = &ndev->vqs[idx];
3046bc9a2b3eSEli Cohen 	if (!mvq->map.virq)
3047bc9a2b3eSEli Cohen 		return -EOPNOTSUPP;
3048bc9a2b3eSEli Cohen 
3049bc9a2b3eSEli Cohen 	return mvq->map.virq;
30501a86b377SEli Cohen }
30511a86b377SEli Cohen 
mlx5_vdpa_get_driver_features(struct vdpa_device * vdev)3052a64917bcSEli Cohen static u64 mlx5_vdpa_get_driver_features(struct vdpa_device *vdev)
3053a64917bcSEli Cohen {
3054a64917bcSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
3055a64917bcSEli Cohen 
3056a64917bcSEli Cohen 	return mvdev->actual_features;
3057a64917bcSEli Cohen }
3058a64917bcSEli Cohen 
counter_set_query(struct mlx5_vdpa_net * ndev,struct mlx5_vdpa_virtqueue * mvq,u64 * received_desc,u64 * completed_desc)30591892a3d4SEli Cohen static int counter_set_query(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq,
30601892a3d4SEli Cohen 			     u64 *received_desc, u64 *completed_desc)
30611892a3d4SEli Cohen {
30621892a3d4SEli Cohen 	u32 in[MLX5_ST_SZ_DW(query_virtio_q_counters_in)] = {};
30631892a3d4SEli Cohen 	u32 out[MLX5_ST_SZ_DW(query_virtio_q_counters_out)] = {};
30641892a3d4SEli Cohen 	void *cmd_hdr;
30651892a3d4SEli Cohen 	void *ctx;
30661892a3d4SEli Cohen 	int err;
30671892a3d4SEli Cohen 
30681892a3d4SEli Cohen 	if (!counters_supported(&ndev->mvdev))
30691892a3d4SEli Cohen 		return -EOPNOTSUPP;
30701892a3d4SEli Cohen 
30711892a3d4SEli Cohen 	if (mvq->fw_state != MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY)
30721892a3d4SEli Cohen 		return -EAGAIN;
30731892a3d4SEli Cohen 
30741892a3d4SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(query_virtio_q_counters_in, in, hdr);
30751892a3d4SEli Cohen 
30761892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_QUERY_GENERAL_OBJECT);
30771892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_Q_COUNTERS);
30781892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
30791892a3d4SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->counter_set_id);
30801892a3d4SEli Cohen 
30811892a3d4SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out));
30821892a3d4SEli Cohen 	if (err)
30831892a3d4SEli Cohen 		return err;
30841892a3d4SEli Cohen 
30851892a3d4SEli Cohen 	ctx = MLX5_ADDR_OF(query_virtio_q_counters_out, out, counters);
30861892a3d4SEli Cohen 	*received_desc = MLX5_GET64(virtio_q_counters, ctx, received_desc);
30871892a3d4SEli Cohen 	*completed_desc = MLX5_GET64(virtio_q_counters, ctx, completed_desc);
30881892a3d4SEli Cohen 	return 0;
30891892a3d4SEli Cohen }
30901892a3d4SEli Cohen 
mlx5_vdpa_get_vendor_vq_stats(struct vdpa_device * vdev,u16 idx,struct sk_buff * msg,struct netlink_ext_ack * extack)30911892a3d4SEli Cohen static int mlx5_vdpa_get_vendor_vq_stats(struct vdpa_device *vdev, u16 idx,
30921892a3d4SEli Cohen 					 struct sk_buff *msg,
30931892a3d4SEli Cohen 					 struct netlink_ext_ack *extack)
30941892a3d4SEli Cohen {
30951892a3d4SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
30961892a3d4SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
30971892a3d4SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
30981892a3d4SEli Cohen 	struct mlx5_control_vq *cvq;
30991892a3d4SEli Cohen 	u64 received_desc;
31001892a3d4SEli Cohen 	u64 completed_desc;
31011892a3d4SEli Cohen 	int err = 0;
31021892a3d4SEli Cohen 
3103759ae7f9SEli Cohen 	down_read(&ndev->reslock);
31041892a3d4SEli Cohen 	if (!is_index_valid(mvdev, idx)) {
31051892a3d4SEli Cohen 		NL_SET_ERR_MSG_MOD(extack, "virtqueue index is not valid");
31061892a3d4SEli Cohen 		err = -EINVAL;
31071892a3d4SEli Cohen 		goto out_err;
31081892a3d4SEli Cohen 	}
31091892a3d4SEli Cohen 
31101892a3d4SEli Cohen 	if (idx == ctrl_vq_idx(mvdev)) {
31111892a3d4SEli Cohen 		cvq = &mvdev->cvq;
31121892a3d4SEli Cohen 		received_desc = cvq->received_desc;
31131892a3d4SEli Cohen 		completed_desc = cvq->completed_desc;
31141892a3d4SEli Cohen 		goto out;
31151892a3d4SEli Cohen 	}
31161892a3d4SEli Cohen 
31171892a3d4SEli Cohen 	mvq = &ndev->vqs[idx];
31181892a3d4SEli Cohen 	err = counter_set_query(ndev, mvq, &received_desc, &completed_desc);
31191892a3d4SEli Cohen 	if (err) {
31201892a3d4SEli Cohen 		NL_SET_ERR_MSG_MOD(extack, "failed to query hardware");
31211892a3d4SEli Cohen 		goto out_err;
31221892a3d4SEli Cohen 	}
31231892a3d4SEli Cohen 
31241892a3d4SEli Cohen out:
31251892a3d4SEli Cohen 	err = -EMSGSIZE;
31261892a3d4SEli Cohen 	if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME, "received_desc"))
31271892a3d4SEli Cohen 		goto out_err;
31281892a3d4SEli Cohen 
31291892a3d4SEli Cohen 	if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE, received_desc,
31301892a3d4SEli Cohen 			      VDPA_ATTR_PAD))
31311892a3d4SEli Cohen 		goto out_err;
31321892a3d4SEli Cohen 
31331892a3d4SEli Cohen 	if (nla_put_string(msg, VDPA_ATTR_DEV_VENDOR_ATTR_NAME, "completed_desc"))
31341892a3d4SEli Cohen 		goto out_err;
31351892a3d4SEli Cohen 
31361892a3d4SEli Cohen 	if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_VENDOR_ATTR_VALUE, completed_desc,
31371892a3d4SEli Cohen 			      VDPA_ATTR_PAD))
31381892a3d4SEli Cohen 		goto out_err;
31391892a3d4SEli Cohen 
31401892a3d4SEli Cohen 	err = 0;
31411892a3d4SEli Cohen out_err:
3142759ae7f9SEli Cohen 	up_read(&ndev->reslock);
31431892a3d4SEli Cohen 	return err;
31441892a3d4SEli Cohen }
31451892a3d4SEli Cohen 
mlx5_vdpa_cvq_suspend(struct mlx5_vdpa_dev * mvdev)3146cae15c2eSEli Cohen static void mlx5_vdpa_cvq_suspend(struct mlx5_vdpa_dev *mvdev)
3147cae15c2eSEli Cohen {
3148cae15c2eSEli Cohen 	struct mlx5_control_vq *cvq;
3149cae15c2eSEli Cohen 
3150cae15c2eSEli Cohen 	if (!(mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)))
3151cae15c2eSEli Cohen 		return;
3152cae15c2eSEli Cohen 
3153cae15c2eSEli Cohen 	cvq = &mvdev->cvq;
3154cae15c2eSEli Cohen 	cvq->ready = false;
3155cae15c2eSEli Cohen }
3156cae15c2eSEli Cohen 
mlx5_vdpa_suspend(struct vdpa_device * vdev)3157cae15c2eSEli Cohen static int mlx5_vdpa_suspend(struct vdpa_device *vdev)
3158cae15c2eSEli Cohen {
3159cae15c2eSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
3160cae15c2eSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
3161cae15c2eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
3162cae15c2eSEli Cohen 	int i;
3163cae15c2eSEli Cohen 
316409e65ee9SSi-Wei Liu 	mlx5_vdpa_info(mvdev, "suspending device\n");
316509e65ee9SSi-Wei Liu 
3166cae15c2eSEli Cohen 	down_write(&ndev->reslock);
3167c384c240SEli Cohen 	unregister_link_notifier(ndev);
3168cae15c2eSEli Cohen 	for (i = 0; i < ndev->cur_num_vqs; i++) {
3169cae15c2eSEli Cohen 		mvq = &ndev->vqs[i];
3170cae15c2eSEli Cohen 		suspend_vq(ndev, mvq);
3171cae15c2eSEli Cohen 	}
3172cae15c2eSEli Cohen 	mlx5_vdpa_cvq_suspend(mvdev);
317309e65ee9SSi-Wei Liu 	mvdev->suspended = true;
3174cae15c2eSEli Cohen 	up_write(&ndev->reslock);
3175cae15c2eSEli Cohen 	return 0;
3176cae15c2eSEli Cohen }
3177cae15c2eSEli Cohen 
mlx5_set_group_asid(struct vdpa_device * vdev,u32 group,unsigned int asid)31788fcd20c3SEli Cohen static int mlx5_set_group_asid(struct vdpa_device *vdev, u32 group,
31798fcd20c3SEli Cohen 			       unsigned int asid)
31808fcd20c3SEli Cohen {
31818fcd20c3SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
31828fcd20c3SEli Cohen 
31838fcd20c3SEli Cohen 	if (group >= MLX5_VDPA_NUMVQ_GROUPS)
31848fcd20c3SEli Cohen 		return -EINVAL;
31858fcd20c3SEli Cohen 
31868fcd20c3SEli Cohen 	mvdev->group2asid[group] = asid;
31878fcd20c3SEli Cohen 	return 0;
31888fcd20c3SEli Cohen }
31898fcd20c3SEli Cohen 
31901a86b377SEli Cohen static const struct vdpa_config_ops mlx5_vdpa_ops = {
31911a86b377SEli Cohen 	.set_vq_address = mlx5_vdpa_set_vq_address,
31921a86b377SEli Cohen 	.set_vq_num = mlx5_vdpa_set_vq_num,
31931a86b377SEli Cohen 	.kick_vq = mlx5_vdpa_kick_vq,
31941a86b377SEli Cohen 	.set_vq_cb = mlx5_vdpa_set_vq_cb,
31951a86b377SEli Cohen 	.set_vq_ready = mlx5_vdpa_set_vq_ready,
31961a86b377SEli Cohen 	.get_vq_ready = mlx5_vdpa_get_vq_ready,
31971a86b377SEli Cohen 	.set_vq_state = mlx5_vdpa_set_vq_state,
31981a86b377SEli Cohen 	.get_vq_state = mlx5_vdpa_get_vq_state,
31991892a3d4SEli Cohen 	.get_vendor_vq_stats = mlx5_vdpa_get_vendor_vq_stats,
32001a86b377SEli Cohen 	.get_vq_notification = mlx5_get_vq_notification,
32011a86b377SEli Cohen 	.get_vq_irq = mlx5_get_vq_irq,
32021a86b377SEli Cohen 	.get_vq_align = mlx5_vdpa_get_vq_align,
3203d4821902SGautam Dawar 	.get_vq_group = mlx5_vdpa_get_vq_group,
3204a64917bcSEli Cohen 	.get_device_features = mlx5_vdpa_get_device_features,
3205a64917bcSEli Cohen 	.set_driver_features = mlx5_vdpa_set_driver_features,
3206a64917bcSEli Cohen 	.get_driver_features = mlx5_vdpa_get_driver_features,
32071a86b377SEli Cohen 	.set_config_cb = mlx5_vdpa_set_config_cb,
32081a86b377SEli Cohen 	.get_vq_num_max = mlx5_vdpa_get_vq_num_max,
32091a86b377SEli Cohen 	.get_device_id = mlx5_vdpa_get_device_id,
32101a86b377SEli Cohen 	.get_vendor_id = mlx5_vdpa_get_vendor_id,
32111a86b377SEli Cohen 	.get_status = mlx5_vdpa_get_status,
32121a86b377SEli Cohen 	.set_status = mlx5_vdpa_set_status,
32130686082dSXie Yongji 	.reset = mlx5_vdpa_reset,
3214442706f9SStefano Garzarella 	.get_config_size = mlx5_vdpa_get_config_size,
32151a86b377SEli Cohen 	.get_config = mlx5_vdpa_get_config,
32161a86b377SEli Cohen 	.set_config = mlx5_vdpa_set_config,
32171a86b377SEli Cohen 	.get_generation = mlx5_vdpa_get_generation,
32181a86b377SEli Cohen 	.set_map = mlx5_vdpa_set_map,
32198fcd20c3SEli Cohen 	.set_group_asid = mlx5_set_group_asid,
322036871fb9SJason Wang 	.get_vq_dma_dev = mlx5_get_vq_dma_dev,
32211a86b377SEli Cohen 	.free = mlx5_vdpa_free,
3222cae15c2eSEli Cohen 	.suspend = mlx5_vdpa_suspend,
32231a86b377SEli Cohen };
32241a86b377SEli Cohen 
query_mtu(struct mlx5_core_dev * mdev,u16 * mtu)3225d084d996SSi-Wei Liu static int query_mtu(struct mlx5_core_dev *mdev, u16 *mtu)
3226d084d996SSi-Wei Liu {
3227d084d996SSi-Wei Liu 	u16 hw_mtu;
3228d084d996SSi-Wei Liu 	int err;
3229d084d996SSi-Wei Liu 
3230d084d996SSi-Wei Liu 	err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
3231d084d996SSi-Wei Liu 	if (err)
3232d084d996SSi-Wei Liu 		return err;
3233d084d996SSi-Wei Liu 
3234d084d996SSi-Wei Liu 	*mtu = hw_mtu - MLX5V_ETH_HARD_MTU;
3235d084d996SSi-Wei Liu 	return 0;
3236d084d996SSi-Wei Liu }
3237d084d996SSi-Wei Liu 
alloc_resources(struct mlx5_vdpa_net * ndev)32381a86b377SEli Cohen static int alloc_resources(struct mlx5_vdpa_net *ndev)
32391a86b377SEli Cohen {
32401a86b377SEli Cohen 	struct mlx5_vdpa_net_resources *res = &ndev->res;
32411a86b377SEli Cohen 	int err;
32421a86b377SEli Cohen 
32431a86b377SEli Cohen 	if (res->valid) {
32441a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "resources already allocated\n");
32451a86b377SEli Cohen 		return -EEXIST;
32461a86b377SEli Cohen 	}
32471a86b377SEli Cohen 
32481a86b377SEli Cohen 	err = mlx5_vdpa_alloc_transport_domain(&ndev->mvdev, &res->tdn);
32491a86b377SEli Cohen 	if (err)
32501a86b377SEli Cohen 		return err;
32511a86b377SEli Cohen 
32521a86b377SEli Cohen 	err = create_tis(ndev);
32531a86b377SEli Cohen 	if (err)
32541a86b377SEli Cohen 		goto err_tis;
32551a86b377SEli Cohen 
32561a86b377SEli Cohen 	res->valid = true;
32571a86b377SEli Cohen 
32581a86b377SEli Cohen 	return 0;
32591a86b377SEli Cohen 
32601a86b377SEli Cohen err_tis:
32611a86b377SEli Cohen 	mlx5_vdpa_dealloc_transport_domain(&ndev->mvdev, res->tdn);
32621a86b377SEli Cohen 	return err;
32631a86b377SEli Cohen }
32641a86b377SEli Cohen 
free_resources(struct mlx5_vdpa_net * ndev)32651a86b377SEli Cohen static void free_resources(struct mlx5_vdpa_net *ndev)
32661a86b377SEli Cohen {
32671a86b377SEli Cohen 	struct mlx5_vdpa_net_resources *res = &ndev->res;
32681a86b377SEli Cohen 
32691a86b377SEli Cohen 	if (!res->valid)
32701a86b377SEli Cohen 		return;
32711a86b377SEli Cohen 
32721a86b377SEli Cohen 	destroy_tis(ndev);
32731a86b377SEli Cohen 	mlx5_vdpa_dealloc_transport_domain(&ndev->mvdev, res->tdn);
32741a86b377SEli Cohen 	res->valid = false;
32751a86b377SEli Cohen }
32761a86b377SEli Cohen 
init_mvqs(struct mlx5_vdpa_net * ndev)32771a86b377SEli Cohen static void init_mvqs(struct mlx5_vdpa_net *ndev)
32781a86b377SEli Cohen {
32791a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
32801a86b377SEli Cohen 	int i;
32811a86b377SEli Cohen 
3282acde3929SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; ++i) {
32831a86b377SEli Cohen 		mvq = &ndev->vqs[i];
32841a86b377SEli Cohen 		memset(mvq, 0, offsetof(struct mlx5_vdpa_virtqueue, ri));
32851a86b377SEli Cohen 		mvq->index = i;
32861a86b377SEli Cohen 		mvq->ndev = ndev;
32871a86b377SEli Cohen 		mvq->fwqp.fw = true;
3288cae15c2eSEli Cohen 		mvq->fw_state = MLX5_VIRTIO_NET_Q_OBJECT_NONE;
32891a86b377SEli Cohen 	}
32901a86b377SEli Cohen 	for (; i < ndev->mvdev.max_vqs; i++) {
32911a86b377SEli Cohen 		mvq = &ndev->vqs[i];
32921a86b377SEli Cohen 		memset(mvq, 0, offsetof(struct mlx5_vdpa_virtqueue, ri));
32931a86b377SEli Cohen 		mvq->index = i;
32941a86b377SEli Cohen 		mvq->ndev = ndev;
32951a86b377SEli Cohen 	}
32961a86b377SEli Cohen }
32971a86b377SEli Cohen 
329858926c8aSEli Cohen struct mlx5_vdpa_mgmtdev {
329958926c8aSEli Cohen 	struct vdpa_mgmt_dev mgtdev;
330058926c8aSEli Cohen 	struct mlx5_adev *madev;
330158926c8aSEli Cohen 	struct mlx5_vdpa_net *ndev;
330258926c8aSEli Cohen };
330358926c8aSEli Cohen 
config_func_mtu(struct mlx5_core_dev * mdev,u16 mtu)33041e00e821SEli Cohen static int config_func_mtu(struct mlx5_core_dev *mdev, u16 mtu)
33051e00e821SEli Cohen {
33061e00e821SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
33071e00e821SEli Cohen 	void *in;
33081e00e821SEli Cohen 	int err;
33091e00e821SEli Cohen 
33101e00e821SEli Cohen 	in = kvzalloc(inlen, GFP_KERNEL);
33111e00e821SEli Cohen 	if (!in)
33121e00e821SEli Cohen 		return -ENOMEM;
33131e00e821SEli Cohen 
33141e00e821SEli Cohen 	MLX5_SET(modify_nic_vport_context_in, in, field_select.mtu, 1);
33151e00e821SEli Cohen 	MLX5_SET(modify_nic_vport_context_in, in, nic_vport_context.mtu,
33161e00e821SEli Cohen 		 mtu + MLX5V_ETH_HARD_MTU);
33171e00e821SEli Cohen 	MLX5_SET(modify_nic_vport_context_in, in, opcode,
33181e00e821SEli Cohen 		 MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
33191e00e821SEli Cohen 
33201e00e821SEli Cohen 	err = mlx5_cmd_exec_in(mdev, modify_nic_vport_context, in);
33211e00e821SEli Cohen 
33221e00e821SEli Cohen 	kvfree(in);
33231e00e821SEli Cohen 	return err;
33241e00e821SEli Cohen }
33251e00e821SEli Cohen 
allocate_irqs(struct mlx5_vdpa_net * ndev)3326bc9a2b3eSEli Cohen static void allocate_irqs(struct mlx5_vdpa_net *ndev)
3327bc9a2b3eSEli Cohen {
3328bc9a2b3eSEli Cohen 	struct mlx5_vdpa_irq_pool_entry *ent;
3329bc9a2b3eSEli Cohen 	int i;
3330bc9a2b3eSEli Cohen 
3331bc9a2b3eSEli Cohen 	if (!msix_mode_supported(&ndev->mvdev))
3332bc9a2b3eSEli Cohen 		return;
3333bc9a2b3eSEli Cohen 
3334bc9a2b3eSEli Cohen 	if (!ndev->mvdev.mdev->pdev)
3335bc9a2b3eSEli Cohen 		return;
3336bc9a2b3eSEli Cohen 
3337bc9a2b3eSEli Cohen 	ndev->irqp.entries = kcalloc(ndev->mvdev.max_vqs, sizeof(*ndev->irqp.entries), GFP_KERNEL);
3338bc9a2b3eSEli Cohen 	if (!ndev->irqp.entries)
3339bc9a2b3eSEli Cohen 		return;
3340bc9a2b3eSEli Cohen 
3341bc9a2b3eSEli Cohen 
3342bc9a2b3eSEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++) {
3343bc9a2b3eSEli Cohen 		ent = ndev->irqp.entries + i;
3344bc9a2b3eSEli Cohen 		snprintf(ent->name, MLX5_VDPA_IRQ_NAME_LEN, "%s-vq-%d",
3345bc9a2b3eSEli Cohen 			 dev_name(&ndev->mvdev.vdev.dev), i);
3346bc9a2b3eSEli Cohen 		ent->map = pci_msix_alloc_irq_at(ndev->mvdev.mdev->pdev, MSI_ANY_INDEX, NULL);
3347bc9a2b3eSEli Cohen 		if (!ent->map.virq)
3348bc9a2b3eSEli Cohen 			return;
3349bc9a2b3eSEli Cohen 
3350bc9a2b3eSEli Cohen 		ndev->irqp.num_ent++;
3351bc9a2b3eSEli Cohen 	}
3352bc9a2b3eSEli Cohen }
3353bc9a2b3eSEli Cohen 
mlx5_vdpa_dev_add(struct vdpa_mgmt_dev * v_mdev,const char * name,const struct vdpa_dev_set_config * add_config)3354d8ca2fa5SParav Pandit static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
3355d8ca2fa5SParav Pandit 			     const struct vdpa_dev_set_config *add_config)
33561a86b377SEli Cohen {
335758926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev = container_of(v_mdev, struct mlx5_vdpa_mgmtdev, mgtdev);
33581a86b377SEli Cohen 	struct virtio_net_config *config;
33597c9f131fSEli Cohen 	struct mlx5_core_dev *pfmdev;
33601a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev;
33611a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev;
336258926c8aSEli Cohen 	struct mlx5_core_dev *mdev;
3363deeacf35SSi-Wei Liu 	u64 device_features;
33641a86b377SEli Cohen 	u32 max_vqs;
3365246fd1caSEli Cohen 	u16 mtu;
33661a86b377SEli Cohen 	int err;
33671a86b377SEli Cohen 
336858926c8aSEli Cohen 	if (mgtdev->ndev)
336958926c8aSEli Cohen 		return -ENOSPC;
337058926c8aSEli Cohen 
337158926c8aSEli Cohen 	mdev = mgtdev->madev->mdev;
3372deeacf35SSi-Wei Liu 	device_features = mgtdev->mgtdev.supported_features;
3373deeacf35SSi-Wei Liu 	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
3374deeacf35SSi-Wei Liu 		if (add_config->device_features & ~device_features) {
3375deeacf35SSi-Wei Liu 			dev_warn(mdev->device,
3376deeacf35SSi-Wei Liu 				 "The provisioned features 0x%llx are not supported by this device with features 0x%llx\n",
3377deeacf35SSi-Wei Liu 				 add_config->device_features, device_features);
3378deeacf35SSi-Wei Liu 			return -EINVAL;
3379deeacf35SSi-Wei Liu 		}
3380deeacf35SSi-Wei Liu 		device_features &= add_config->device_features;
3381791a1cb7SEli Cohen 	} else {
3382791a1cb7SEli Cohen 		device_features &= ~BIT_ULL(VIRTIO_NET_F_MRG_RXBUF);
3383deeacf35SSi-Wei Liu 	}
3384deeacf35SSi-Wei Liu 	if (!(device_features & BIT_ULL(VIRTIO_F_VERSION_1) &&
3385deeacf35SSi-Wei Liu 	      device_features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM))) {
3386deeacf35SSi-Wei Liu 		dev_warn(mdev->device,
3387deeacf35SSi-Wei Liu 			 "Must provision minimum features 0x%llx for this device",
3388deeacf35SSi-Wei Liu 			 BIT_ULL(VIRTIO_F_VERSION_1) | BIT_ULL(VIRTIO_F_ACCESS_PLATFORM));
3389deeacf35SSi-Wei Liu 		return -EOPNOTSUPP;
3390deeacf35SSi-Wei Liu 	}
3391deeacf35SSi-Wei Liu 
3392879753c8SEli Cohen 	if (!(MLX5_CAP_DEV_VDPA_EMULATION(mdev, virtio_queue_type) &
3393879753c8SEli Cohen 	    MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_SPLIT)) {
3394879753c8SEli Cohen 		dev_warn(mdev->device, "missing support for split virtqueues\n");
3395879753c8SEli Cohen 		return -EOPNOTSUPP;
3396879753c8SEli Cohen 	}
3397879753c8SEli Cohen 
3398acde3929SEli Cohen 	max_vqs = min_t(int, MLX5_CAP_DEV_VDPA_EMULATION(mdev, max_num_virtio_queues),
3399acde3929SEli Cohen 			1 << MLX5_CAP_GEN(mdev, log_max_rqt_size));
340075560522SEli Cohen 	if (max_vqs < 2) {
340175560522SEli Cohen 		dev_warn(mdev->device,
340275560522SEli Cohen 			 "%d virtqueues are supported. At least 2 are required\n",
340375560522SEli Cohen 			 max_vqs);
340475560522SEli Cohen 		return -EAGAIN;
340575560522SEli Cohen 	}
340675560522SEli Cohen 
340775560522SEli Cohen 	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP)) {
340875560522SEli Cohen 		if (add_config->net.max_vq_pairs > max_vqs / 2)
340975560522SEli Cohen 			return -EINVAL;
341075560522SEli Cohen 		max_vqs = min_t(u32, max_vqs, 2 * add_config->net.max_vq_pairs);
341175560522SEli Cohen 	} else {
341275560522SEli Cohen 		max_vqs = 2;
341375560522SEli Cohen 	}
34141a86b377SEli Cohen 
34151a86b377SEli Cohen 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
34168fcd20c3SEli Cohen 				 MLX5_VDPA_NUMVQ_GROUPS, MLX5_VDPA_NUM_AS, name, false);
34171a86b377SEli Cohen 	if (IS_ERR(ndev))
341874c9729dSLeon Romanovsky 		return PTR_ERR(ndev);
34191a86b377SEli Cohen 
34201a86b377SEli Cohen 	ndev->mvdev.max_vqs = max_vqs;
34211a86b377SEli Cohen 	mvdev = &ndev->mvdev;
34221a86b377SEli Cohen 	mvdev->mdev = mdev;
342375560522SEli Cohen 
342475560522SEli Cohen 	ndev->vqs = kcalloc(max_vqs, sizeof(*ndev->vqs), GFP_KERNEL);
342575560522SEli Cohen 	ndev->event_cbs = kcalloc(max_vqs + 1, sizeof(*ndev->event_cbs), GFP_KERNEL);
342675560522SEli Cohen 	if (!ndev->vqs || !ndev->event_cbs) {
342775560522SEli Cohen 		err = -ENOMEM;
342875560522SEli Cohen 		goto err_alloc;
342975560522SEli Cohen 	}
343075560522SEli Cohen 
34311a86b377SEli Cohen 	init_mvqs(ndev);
3432bc9a2b3eSEli Cohen 	allocate_irqs(ndev);
3433759ae7f9SEli Cohen 	init_rwsem(&ndev->reslock);
34341a86b377SEli Cohen 	config = &ndev->config;
34351e00e821SEli Cohen 
34361e00e821SEli Cohen 	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU)) {
34371e00e821SEli Cohen 		err = config_func_mtu(mdev, add_config->net.mtu);
34381e00e821SEli Cohen 		if (err)
3439759ae7f9SEli Cohen 			goto err_alloc;
34401e00e821SEli Cohen 	}
34411e00e821SEli Cohen 
3442deeacf35SSi-Wei Liu 	if (device_features & BIT_ULL(VIRTIO_NET_F_MTU)) {
3443246fd1caSEli Cohen 		err = query_mtu(mdev, &mtu);
34441a86b377SEli Cohen 		if (err)
3445759ae7f9SEli Cohen 			goto err_alloc;
34461a86b377SEli Cohen 
3447246fd1caSEli Cohen 		ndev->config.mtu = cpu_to_mlx5vdpa16(mvdev, mtu);
3448033779a7SSi-Wei Liu 	}
34491a86b377SEli Cohen 
3450deeacf35SSi-Wei Liu 	if (device_features & BIT_ULL(VIRTIO_NET_F_STATUS)) {
3451edf747afSEli Cohen 		if (get_link_state(mvdev))
3452edf747afSEli Cohen 			ndev->config.status |= cpu_to_mlx5vdpa16(mvdev, VIRTIO_NET_S_LINK_UP);
3453edf747afSEli Cohen 		else
3454edf747afSEli Cohen 			ndev->config.status &= cpu_to_mlx5vdpa16(mvdev, ~VIRTIO_NET_S_LINK_UP);
3455033779a7SSi-Wei Liu 	}
3456edf747afSEli Cohen 
3457a007d940SEli Cohen 	if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
3458a007d940SEli Cohen 		memcpy(ndev->config.mac, add_config->net.mac, ETH_ALEN);
3459deeacf35SSi-Wei Liu 	/* No bother setting mac address in config if not going to provision _F_MAC */
3460deeacf35SSi-Wei Liu 	} else if ((add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) == 0 ||
3461deeacf35SSi-Wei Liu 		   device_features & BIT_ULL(VIRTIO_NET_F_MAC)) {
34621a86b377SEli Cohen 		err = mlx5_query_nic_vport_mac_address(mdev, 0, 0, config->mac);
34631a86b377SEli Cohen 		if (err)
3464759ae7f9SEli Cohen 			goto err_alloc;
3465a007d940SEli Cohen 	}
34661a86b377SEli Cohen 
34677c9f131fSEli Cohen 	if (!is_zero_ether_addr(config->mac)) {
34687c9f131fSEli Cohen 		pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
34697c9f131fSEli Cohen 		err = mlx5_mpfs_add_mac(pfmdev, config->mac);
34707c9f131fSEli Cohen 		if (err)
3471759ae7f9SEli Cohen 			goto err_alloc;
3472deeacf35SSi-Wei Liu 	} else if ((add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) == 0) {
3473deeacf35SSi-Wei Liu 		/*
3474deeacf35SSi-Wei Liu 		 * We used to clear _F_MAC feature bit if seeing
3475deeacf35SSi-Wei Liu 		 * zero mac address when device features are not
3476deeacf35SSi-Wei Liu 		 * specifically provisioned. Keep the behaviour
3477deeacf35SSi-Wei Liu 		 * so old scripts do not break.
3478deeacf35SSi-Wei Liu 		 */
3479deeacf35SSi-Wei Liu 		device_features &= ~BIT_ULL(VIRTIO_NET_F_MAC);
3480deeacf35SSi-Wei Liu 	} else if (device_features & BIT_ULL(VIRTIO_NET_F_MAC)) {
3481deeacf35SSi-Wei Liu 		/* Don't provision zero mac address for _F_MAC */
3482deeacf35SSi-Wei Liu 		mlx5_vdpa_warn(&ndev->mvdev,
3483deeacf35SSi-Wei Liu 			       "No mac address provisioned?\n");
3484deeacf35SSi-Wei Liu 		err = -EINVAL;
3485deeacf35SSi-Wei Liu 		goto err_alloc;
34867c9f131fSEli Cohen 	}
34877c9f131fSEli Cohen 
3488deeacf35SSi-Wei Liu 	if (device_features & BIT_ULL(VIRTIO_NET_F_MQ))
3489acde3929SEli Cohen 		config->max_virtqueue_pairs = cpu_to_mlx5vdpa16(mvdev, max_vqs / 2);
3490deeacf35SSi-Wei Liu 
3491deeacf35SSi-Wei Liu 	ndev->mvdev.mlx_features = device_features;
34927d23dcdfSEli Cohen 	mvdev->vdev.dma_dev = &mdev->pdev->dev;
34931a86b377SEli Cohen 	err = mlx5_vdpa_alloc_resources(&ndev->mvdev);
34941a86b377SEli Cohen 	if (err)
34957c9f131fSEli Cohen 		goto err_mpfs;
34961a86b377SEli Cohen 
34976f5312f8SEli Cohen 	if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) {
349838fc462fSEli Cohen 		err = mlx5_vdpa_create_mr(mvdev, NULL, 0);
34991a86b377SEli Cohen 		if (err)
35001a86b377SEli Cohen 			goto err_res;
35016f5312f8SEli Cohen 	}
35026f5312f8SEli Cohen 
35036f5312f8SEli Cohen 	err = alloc_resources(ndev);
35046f5312f8SEli Cohen 	if (err)
35056f5312f8SEli Cohen 		goto err_mr;
35061a86b377SEli Cohen 
350755ebf0d6SJason Wang 	ndev->cvq_ent.mvdev = mvdev;
350855ebf0d6SJason Wang 	INIT_WORK(&ndev->cvq_ent.work, mlx5_cvq_kick_handler);
3509218bdd20SEli Cohen 	mvdev->wq = create_singlethread_workqueue("mlx5_vdpa_wq");
35105262912eSEli Cohen 	if (!mvdev->wq) {
35115262912eSEli Cohen 		err = -ENOMEM;
35125262912eSEli Cohen 		goto err_res2;
35135262912eSEli Cohen 	}
35145262912eSEli Cohen 
351558926c8aSEli Cohen 	mvdev->vdev.mdev = &mgtdev->mgtdev;
3516acde3929SEli Cohen 	err = _vdpa_register_device(&mvdev->vdev, max_vqs + 1);
35171a86b377SEli Cohen 	if (err)
35181a86b377SEli Cohen 		goto err_reg;
35191a86b377SEli Cohen 
352058926c8aSEli Cohen 	mgtdev->ndev = ndev;
352174c9729dSLeon Romanovsky 	return 0;
35221a86b377SEli Cohen 
35231a86b377SEli Cohen err_reg:
35245262912eSEli Cohen 	destroy_workqueue(mvdev->wq);
35255262912eSEli Cohen err_res2:
35261a86b377SEli Cohen 	free_resources(ndev);
35276f5312f8SEli Cohen err_mr:
35286f5312f8SEli Cohen 	mlx5_vdpa_destroy_mr(mvdev);
35291a86b377SEli Cohen err_res:
35301a86b377SEli Cohen 	mlx5_vdpa_free_resources(&ndev->mvdev);
35317c9f131fSEli Cohen err_mpfs:
35327c9f131fSEli Cohen 	if (!is_zero_ether_addr(config->mac))
35337c9f131fSEli Cohen 		mlx5_mpfs_del_mac(pfmdev, config->mac);
353475560522SEli Cohen err_alloc:
35351a86b377SEli Cohen 	put_device(&mvdev->vdev.dev);
353674c9729dSLeon Romanovsky 	return err;
35371a86b377SEli Cohen }
35381a86b377SEli Cohen 
mlx5_vdpa_dev_del(struct vdpa_mgmt_dev * v_mdev,struct vdpa_device * dev)353958926c8aSEli Cohen static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev)
354058926c8aSEli Cohen {
354158926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev = container_of(v_mdev, struct mlx5_vdpa_mgmtdev, mgtdev);
35425262912eSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(dev);
3543edf747afSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
3544ad6dc1daSEli Cohen 	struct workqueue_struct *wq;
354558926c8aSEli Cohen 
3546c384c240SEli Cohen 	unregister_link_notifier(ndev);
354773790bdfSDragos Tatulea 	_vdpa_unregister_device(dev);
3548ad6dc1daSEli Cohen 	wq = mvdev->wq;
3549ad6dc1daSEli Cohen 	mvdev->wq = NULL;
3550ad6dc1daSEli Cohen 	destroy_workqueue(wq);
355158926c8aSEli Cohen 	mgtdev->ndev = NULL;
355258926c8aSEli Cohen }
355358926c8aSEli Cohen 
355458926c8aSEli Cohen static const struct vdpa_mgmtdev_ops mdev_ops = {
355558926c8aSEli Cohen 	.dev_add = mlx5_vdpa_dev_add,
355658926c8aSEli Cohen 	.dev_del = mlx5_vdpa_dev_del,
355758926c8aSEli Cohen };
355858926c8aSEli Cohen 
355958926c8aSEli Cohen static struct virtio_device_id id_table[] = {
356058926c8aSEli Cohen 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
356158926c8aSEli Cohen 	{ 0 },
356258926c8aSEli Cohen };
356358926c8aSEli Cohen 
mlx5v_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)356458926c8aSEli Cohen static int mlx5v_probe(struct auxiliary_device *adev,
356558926c8aSEli Cohen 		       const struct auxiliary_device_id *id)
356658926c8aSEli Cohen 
356758926c8aSEli Cohen {
356858926c8aSEli Cohen 	struct mlx5_adev *madev = container_of(adev, struct mlx5_adev, adev);
356958926c8aSEli Cohen 	struct mlx5_core_dev *mdev = madev->mdev;
357058926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev;
357158926c8aSEli Cohen 	int err;
357258926c8aSEli Cohen 
357358926c8aSEli Cohen 	mgtdev = kzalloc(sizeof(*mgtdev), GFP_KERNEL);
357458926c8aSEli Cohen 	if (!mgtdev)
357558926c8aSEli Cohen 		return -ENOMEM;
357658926c8aSEli Cohen 
357758926c8aSEli Cohen 	mgtdev->mgtdev.ops = &mdev_ops;
357858926c8aSEli Cohen 	mgtdev->mgtdev.device = mdev->device;
357958926c8aSEli Cohen 	mgtdev->mgtdev.id_table = id_table;
358075560522SEli Cohen 	mgtdev->mgtdev.config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) |
35811e00e821SEli Cohen 					  BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP) |
3582deeacf35SSi-Wei Liu 					  BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU) |
3583deeacf35SSi-Wei Liu 					  BIT_ULL(VDPA_ATTR_DEV_FEATURES);
358479de65edSEli Cohen 	mgtdev->mgtdev.max_supported_vqs =
358579de65edSEli Cohen 		MLX5_CAP_DEV_VDPA_EMULATION(mdev, max_num_virtio_queues) + 1;
358679de65edSEli Cohen 	mgtdev->mgtdev.supported_features = get_supported_features(mdev);
358758926c8aSEli Cohen 	mgtdev->madev = madev;
358858926c8aSEli Cohen 
358958926c8aSEli Cohen 	err = vdpa_mgmtdev_register(&mgtdev->mgtdev);
359058926c8aSEli Cohen 	if (err)
359158926c8aSEli Cohen 		goto reg_err;
359258926c8aSEli Cohen 
359345e3a279SDavid E. Box 	auxiliary_set_drvdata(adev, mgtdev);
359458926c8aSEli Cohen 
359558926c8aSEli Cohen 	return 0;
359658926c8aSEli Cohen 
359758926c8aSEli Cohen reg_err:
359858926c8aSEli Cohen 	kfree(mgtdev);
359958926c8aSEli Cohen 	return err;
360058926c8aSEli Cohen }
360158926c8aSEli Cohen 
mlx5v_remove(struct auxiliary_device * adev)360274c9729dSLeon Romanovsky static void mlx5v_remove(struct auxiliary_device *adev)
36031a86b377SEli Cohen {
360458926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev;
360574c9729dSLeon Romanovsky 
360645e3a279SDavid E. Box 	mgtdev = auxiliary_get_drvdata(adev);
360758926c8aSEli Cohen 	vdpa_mgmtdev_unregister(&mgtdev->mgtdev);
360858926c8aSEli Cohen 	kfree(mgtdev);
36091a86b377SEli Cohen }
361074c9729dSLeon Romanovsky 
361174c9729dSLeon Romanovsky static const struct auxiliary_device_id mlx5v_id_table[] = {
361274c9729dSLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".vnet", },
361374c9729dSLeon Romanovsky 	{},
361474c9729dSLeon Romanovsky };
361574c9729dSLeon Romanovsky 
361674c9729dSLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5v_id_table);
361774c9729dSLeon Romanovsky 
361874c9729dSLeon Romanovsky static struct auxiliary_driver mlx5v_driver = {
361974c9729dSLeon Romanovsky 	.name = "vnet",
362074c9729dSLeon Romanovsky 	.probe = mlx5v_probe,
362174c9729dSLeon Romanovsky 	.remove = mlx5v_remove,
362274c9729dSLeon Romanovsky 	.id_table = mlx5v_id_table,
362374c9729dSLeon Romanovsky };
362474c9729dSLeon Romanovsky 
362574c9729dSLeon Romanovsky module_auxiliary_driver(mlx5v_driver);
3626