xref: /openbmc/linux/drivers/vdpa/mlx5/net/mlx5_vnet.c (revision 75560522eaef2f7c7b2fec3c22e1254244f52372)
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"
211a86b377SEli Cohen 
2274c9729dSLeon Romanovsky MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
2374c9729dSLeon Romanovsky MODULE_DESCRIPTION("Mellanox VDPA driver");
2474c9729dSLeon Romanovsky MODULE_LICENSE("Dual BSD/GPL");
2574c9729dSLeon Romanovsky 
2674c9729dSLeon Romanovsky #define to_mlx5_vdpa_ndev(__mvdev)                                             \
2774c9729dSLeon Romanovsky 	container_of(__mvdev, struct mlx5_vdpa_net, mvdev)
281a86b377SEli Cohen #define to_mvdev(__vdev) container_of((__vdev), struct mlx5_vdpa_dev, vdev)
291a86b377SEli Cohen 
301a86b377SEli Cohen #define VALID_FEATURES_MASK                                                                        \
31cbb52359SNathan Chancellor 	(BIT_ULL(VIRTIO_NET_F_CSUM) | BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |                                   \
32cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) | BIT_ULL(VIRTIO_NET_F_MTU) | BIT_ULL(VIRTIO_NET_F_MAC) |   \
33cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) | BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |                             \
34cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_GUEST_ECN) | BIT_ULL(VIRTIO_NET_F_GUEST_UFO) | BIT_ULL(VIRTIO_NET_F_HOST_TSO4) | \
35cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_HOST_TSO6) | BIT_ULL(VIRTIO_NET_F_HOST_ECN) | BIT_ULL(VIRTIO_NET_F_HOST_UFO) |   \
36cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) | BIT_ULL(VIRTIO_NET_F_STATUS) | BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |      \
37cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_CTRL_RX) | BIT_ULL(VIRTIO_NET_F_CTRL_VLAN) |                                 \
38cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA) | BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE) |                      \
39cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_MQ) | BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) | BIT_ULL(VIRTIO_NET_F_HASH_REPORT) |  \
40cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_RSS) | BIT_ULL(VIRTIO_NET_F_RSC_EXT) | BIT_ULL(VIRTIO_NET_F_STANDBY) |           \
41cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX) | BIT_ULL(VIRTIO_F_NOTIFY_ON_EMPTY) |                          \
42cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_F_ANY_LAYOUT) | BIT_ULL(VIRTIO_F_VERSION_1) | BIT_ULL(VIRTIO_F_ACCESS_PLATFORM) |      \
43cbb52359SNathan Chancellor 	 BIT_ULL(VIRTIO_F_RING_PACKED) | BIT_ULL(VIRTIO_F_ORDER_PLATFORM) | BIT_ULL(VIRTIO_F_SR_IOV))
441a86b377SEli Cohen 
451a86b377SEli Cohen #define VALID_STATUS_MASK                                                                          \
461a86b377SEli Cohen 	(VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK |        \
471a86b377SEli Cohen 	 VIRTIO_CONFIG_S_FEATURES_OK | VIRTIO_CONFIG_S_NEEDS_RESET | VIRTIO_CONFIG_S_FAILED)
481a86b377SEli Cohen 
49e4fc6650SEli Cohen #define MLX5_FEATURE(_mvdev, _feature) (!!((_mvdev)->actual_features & BIT_ULL(_feature)))
50e4fc6650SEli Cohen 
511a86b377SEli Cohen struct mlx5_vdpa_net_resources {
521a86b377SEli Cohen 	u32 tisn;
531a86b377SEli Cohen 	u32 tdn;
541a86b377SEli Cohen 	u32 tirn;
551a86b377SEli Cohen 	u32 rqtn;
561a86b377SEli Cohen 	bool valid;
571a86b377SEli Cohen };
581a86b377SEli Cohen 
591a86b377SEli Cohen struct mlx5_vdpa_cq_buf {
601a86b377SEli Cohen 	struct mlx5_frag_buf_ctrl fbc;
611a86b377SEli Cohen 	struct mlx5_frag_buf frag_buf;
621a86b377SEli Cohen 	int cqe_size;
631a86b377SEli Cohen 	int nent;
641a86b377SEli Cohen };
651a86b377SEli Cohen 
661a86b377SEli Cohen struct mlx5_vdpa_cq {
671a86b377SEli Cohen 	struct mlx5_core_cq mcq;
681a86b377SEli Cohen 	struct mlx5_vdpa_cq_buf buf;
691a86b377SEli Cohen 	struct mlx5_db db;
701a86b377SEli Cohen 	int cqe;
711a86b377SEli Cohen };
721a86b377SEli Cohen 
731a86b377SEli Cohen struct mlx5_vdpa_umem {
741a86b377SEli Cohen 	struct mlx5_frag_buf_ctrl fbc;
751a86b377SEli Cohen 	struct mlx5_frag_buf frag_buf;
761a86b377SEli Cohen 	int size;
771a86b377SEli Cohen 	u32 id;
781a86b377SEli Cohen };
791a86b377SEli Cohen 
801a86b377SEli Cohen struct mlx5_vdpa_qp {
811a86b377SEli Cohen 	struct mlx5_core_qp mqp;
821a86b377SEli Cohen 	struct mlx5_frag_buf frag_buf;
831a86b377SEli Cohen 	struct mlx5_db db;
841a86b377SEli Cohen 	u16 head;
851a86b377SEli Cohen 	bool fw;
861a86b377SEli Cohen };
871a86b377SEli Cohen 
881a86b377SEli Cohen struct mlx5_vq_restore_info {
891a86b377SEli Cohen 	u32 num_ent;
901a86b377SEli Cohen 	u64 desc_addr;
911a86b377SEli Cohen 	u64 device_addr;
921a86b377SEli Cohen 	u64 driver_addr;
931a86b377SEli Cohen 	u16 avail_index;
94b35ccebeSEli Cohen 	u16 used_index;
951a86b377SEli Cohen 	bool ready;
961a86b377SEli Cohen 	bool restore;
971a86b377SEli Cohen };
981a86b377SEli Cohen 
991a86b377SEli Cohen struct mlx5_vdpa_virtqueue {
1001a86b377SEli Cohen 	bool ready;
1011a86b377SEli Cohen 	u64 desc_addr;
1021a86b377SEli Cohen 	u64 device_addr;
1031a86b377SEli Cohen 	u64 driver_addr;
1041a86b377SEli Cohen 	u32 num_ent;
1051a86b377SEli Cohen 
1061a86b377SEli Cohen 	/* Resources for implementing the notification channel from the device
1071a86b377SEli Cohen 	 * to the driver. fwqp is the firmware end of an RC connection; the
1081a86b377SEli Cohen 	 * other end is vqqp used by the driver. cq is is where completions are
1091a86b377SEli Cohen 	 * reported.
1101a86b377SEli Cohen 	 */
1111a86b377SEli Cohen 	struct mlx5_vdpa_cq cq;
1121a86b377SEli Cohen 	struct mlx5_vdpa_qp fwqp;
1131a86b377SEli Cohen 	struct mlx5_vdpa_qp vqqp;
1141a86b377SEli Cohen 
1151a86b377SEli Cohen 	/* umem resources are required for the virtqueue operation. They're use
1161a86b377SEli Cohen 	 * is internal and they must be provided by the driver.
1171a86b377SEli Cohen 	 */
1181a86b377SEli Cohen 	struct mlx5_vdpa_umem umem1;
1191a86b377SEli Cohen 	struct mlx5_vdpa_umem umem2;
1201a86b377SEli Cohen 	struct mlx5_vdpa_umem umem3;
1211a86b377SEli Cohen 
1221a86b377SEli Cohen 	bool initialized;
1231a86b377SEli Cohen 	int index;
1241a86b377SEli Cohen 	u32 virtq_id;
1251a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev;
1261a86b377SEli Cohen 	u16 avail_idx;
127b35ccebeSEli Cohen 	u16 used_idx;
1281a86b377SEli Cohen 	int fw_state;
1291a86b377SEli Cohen 
1301a86b377SEli Cohen 	/* keep last in the struct */
1311a86b377SEli Cohen 	struct mlx5_vq_restore_info ri;
1321a86b377SEli Cohen };
1331a86b377SEli Cohen 
134e4fc6650SEli Cohen static bool is_index_valid(struct mlx5_vdpa_dev *mvdev, u16 idx)
135e4fc6650SEli Cohen {
136e4fc6650SEli Cohen 	if (unlikely(idx > mvdev->max_idx))
137e4fc6650SEli Cohen 		return false;
138e4fc6650SEli Cohen 
139e4fc6650SEli Cohen 	return true;
140e4fc6650SEli Cohen }
141e4fc6650SEli Cohen 
1421a86b377SEli Cohen struct mlx5_vdpa_net {
1431a86b377SEli Cohen 	struct mlx5_vdpa_dev mvdev;
1441a86b377SEli Cohen 	struct mlx5_vdpa_net_resources res;
1451a86b377SEli Cohen 	struct virtio_net_config config;
146*75560522SEli Cohen 	struct mlx5_vdpa_virtqueue *vqs;
147*75560522SEli Cohen 	struct vdpa_callback *event_cbs;
1481a86b377SEli Cohen 
1491a86b377SEli Cohen 	/* Serialize vq resources creation and destruction. This is required
1501a86b377SEli Cohen 	 * since memory map might change and we need to destroy and create
1511a86b377SEli Cohen 	 * resources while driver in operational.
1521a86b377SEli Cohen 	 */
1531a86b377SEli Cohen 	struct mutex reslock;
1541a86b377SEli Cohen 	struct mlx5_flow_table *rxft;
1551a86b377SEli Cohen 	struct mlx5_fc *rx_counter;
156540061acSEli Cohen 	struct mlx5_flow_handle *rx_rule_ucast;
157540061acSEli Cohen 	struct mlx5_flow_handle *rx_rule_mcast;
1581a86b377SEli Cohen 	bool setup;
1595262912eSEli Cohen 	u32 cur_num_vqs;
160edf747afSEli Cohen 	struct notifier_block nb;
161edf747afSEli Cohen 	struct vdpa_callback config_cb;
1621a86b377SEli Cohen };
1631a86b377SEli Cohen 
1641a86b377SEli Cohen static void free_resources(struct mlx5_vdpa_net *ndev);
1651a86b377SEli Cohen static void init_mvqs(struct mlx5_vdpa_net *ndev);
166ae0428deSEli Cohen static int setup_driver(struct mlx5_vdpa_dev *mvdev);
1671a86b377SEli Cohen static void teardown_driver(struct mlx5_vdpa_net *ndev);
1681a86b377SEli Cohen 
1691a86b377SEli Cohen static bool mlx5_vdpa_debug;
1701a86b377SEli Cohen 
1715262912eSEli Cohen #define MLX5_CVQ_MAX_ENT 16
1725262912eSEli Cohen 
1731a86b377SEli Cohen #define MLX5_LOG_VIO_FLAG(_feature)                                                                \
1741a86b377SEli Cohen 	do {                                                                                       \
175cbb52359SNathan Chancellor 		if (features & BIT_ULL(_feature))                                                  \
1761a86b377SEli Cohen 			mlx5_vdpa_info(mvdev, "%s\n", #_feature);                                  \
1771a86b377SEli Cohen 	} while (0)
1781a86b377SEli Cohen 
1791a86b377SEli Cohen #define MLX5_LOG_VIO_STAT(_status)                                                                 \
1801a86b377SEli Cohen 	do {                                                                                       \
1811a86b377SEli Cohen 		if (status & (_status))                                                            \
1821a86b377SEli Cohen 			mlx5_vdpa_info(mvdev, "%s\n", #_status);                                   \
1831a86b377SEli Cohen 	} while (0)
1841a86b377SEli Cohen 
18552893733SEli Cohen /* TODO: cross-endian support */
18652893733SEli Cohen static inline bool mlx5_vdpa_is_little_endian(struct mlx5_vdpa_dev *mvdev)
18752893733SEli Cohen {
18852893733SEli Cohen 	return virtio_legacy_is_little_endian() ||
18952893733SEli Cohen 		(mvdev->actual_features & BIT_ULL(VIRTIO_F_VERSION_1));
19052893733SEli Cohen }
19152893733SEli Cohen 
19252893733SEli Cohen static u16 mlx5vdpa16_to_cpu(struct mlx5_vdpa_dev *mvdev, __virtio16 val)
19352893733SEli Cohen {
19452893733SEli Cohen 	return __virtio16_to_cpu(mlx5_vdpa_is_little_endian(mvdev), val);
19552893733SEli Cohen }
19652893733SEli Cohen 
19752893733SEli Cohen static __virtio16 cpu_to_mlx5vdpa16(struct mlx5_vdpa_dev *mvdev, u16 val)
19852893733SEli Cohen {
19952893733SEli Cohen 	return __cpu_to_virtio16(mlx5_vdpa_is_little_endian(mvdev), val);
20052893733SEli Cohen }
20152893733SEli Cohen 
20274c9729dSLeon Romanovsky static inline u32 mlx5_vdpa_max_qps(int max_vqs)
20374c9729dSLeon Romanovsky {
20474c9729dSLeon Romanovsky 	return max_vqs / 2;
20574c9729dSLeon Romanovsky }
20674c9729dSLeon Romanovsky 
2075262912eSEli Cohen static u16 ctrl_vq_idx(struct mlx5_vdpa_dev *mvdev)
2085262912eSEli Cohen {
20952893733SEli Cohen 	if (!(mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_MQ)))
21052893733SEli Cohen 		return 2;
21152893733SEli Cohen 
2125262912eSEli Cohen 	return 2 * mlx5_vdpa_max_qps(mvdev->max_vqs);
2135262912eSEli Cohen }
2145262912eSEli Cohen 
2155262912eSEli Cohen static bool is_ctrl_vq_idx(struct mlx5_vdpa_dev *mvdev, u16 idx)
2165262912eSEli Cohen {
2175262912eSEli Cohen 	return idx == ctrl_vq_idx(mvdev);
2185262912eSEli Cohen }
2195262912eSEli Cohen 
2201a86b377SEli Cohen static void print_status(struct mlx5_vdpa_dev *mvdev, u8 status, bool set)
2211a86b377SEli Cohen {
2221a86b377SEli Cohen 	if (status & ~VALID_STATUS_MASK)
2231a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "Warning: there are invalid status bits 0x%x\n",
2241a86b377SEli Cohen 			       status & ~VALID_STATUS_MASK);
2251a86b377SEli Cohen 
2261a86b377SEli Cohen 	if (!mlx5_vdpa_debug)
2271a86b377SEli Cohen 		return;
2281a86b377SEli Cohen 
2291a86b377SEli Cohen 	mlx5_vdpa_info(mvdev, "driver status %s", set ? "set" : "get");
2301a86b377SEli Cohen 	if (set && !status) {
2311a86b377SEli Cohen 		mlx5_vdpa_info(mvdev, "driver resets the device\n");
2321a86b377SEli Cohen 		return;
2331a86b377SEli Cohen 	}
2341a86b377SEli Cohen 
2351a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_ACKNOWLEDGE);
2361a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_DRIVER);
2371a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_DRIVER_OK);
2381a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_FEATURES_OK);
2391a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_NEEDS_RESET);
2401a86b377SEli Cohen 	MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_FAILED);
2411a86b377SEli Cohen }
2421a86b377SEli Cohen 
2431a86b377SEli Cohen static void print_features(struct mlx5_vdpa_dev *mvdev, u64 features, bool set)
2441a86b377SEli Cohen {
2451a86b377SEli Cohen 	if (features & ~VALID_FEATURES_MASK)
2461a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "There are invalid feature bits 0x%llx\n",
2471a86b377SEli Cohen 			       features & ~VALID_FEATURES_MASK);
2481a86b377SEli Cohen 
2491a86b377SEli Cohen 	if (!mlx5_vdpa_debug)
2501a86b377SEli Cohen 		return;
2511a86b377SEli Cohen 
2521a86b377SEli Cohen 	mlx5_vdpa_info(mvdev, "driver %s feature bits:\n", set ? "sets" : "reads");
2531a86b377SEli Cohen 	if (!features)
2541a86b377SEli Cohen 		mlx5_vdpa_info(mvdev, "all feature bits are cleared\n");
2551a86b377SEli Cohen 
2561a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CSUM);
2571a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_CSUM);
2581a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2591a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MTU);
2601a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MAC);
2611a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_TSO4);
2621a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_TSO6);
2631a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_ECN);
2641a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_UFO);
2651a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_TSO4);
2661a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_TSO6);
2671a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_ECN);
2681a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_UFO);
2691a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MRG_RXBUF);
2701a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_STATUS);
2711a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_VQ);
2721a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_RX);
2731a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_VLAN);
2741a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_RX_EXTRA);
2751a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_ANNOUNCE);
2761a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MQ);
2771a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_MAC_ADDR);
2781a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HASH_REPORT);
2791a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_RSS);
2801a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_RSC_EXT);
2811a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_STANDBY);
2821a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_SPEED_DUPLEX);
2831a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_NOTIFY_ON_EMPTY);
2841a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_ANY_LAYOUT);
2851a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_VERSION_1);
2861a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_ACCESS_PLATFORM);
2871a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_RING_PACKED);
2881a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_ORDER_PLATFORM);
2891a86b377SEli Cohen 	MLX5_LOG_VIO_FLAG(VIRTIO_F_SR_IOV);
2901a86b377SEli Cohen }
2911a86b377SEli Cohen 
2921a86b377SEli Cohen static int create_tis(struct mlx5_vdpa_net *ndev)
2931a86b377SEli Cohen {
2941a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = &ndev->mvdev;
2951a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
2961a86b377SEli Cohen 	void *tisc;
2971a86b377SEli Cohen 	int err;
2981a86b377SEli Cohen 
2991a86b377SEli Cohen 	tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3001a86b377SEli Cohen 	MLX5_SET(tisc, tisc, transport_domain, ndev->res.tdn);
3011a86b377SEli Cohen 	err = mlx5_vdpa_create_tis(mvdev, in, &ndev->res.tisn);
3021a86b377SEli Cohen 	if (err)
3031a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "create TIS (%d)\n", err);
3041a86b377SEli Cohen 
3051a86b377SEli Cohen 	return err;
3061a86b377SEli Cohen }
3071a86b377SEli Cohen 
3081a86b377SEli Cohen static void destroy_tis(struct mlx5_vdpa_net *ndev)
3091a86b377SEli Cohen {
3101a86b377SEli Cohen 	mlx5_vdpa_destroy_tis(&ndev->mvdev, ndev->res.tisn);
3111a86b377SEli Cohen }
3121a86b377SEli Cohen 
3131a86b377SEli Cohen #define MLX5_VDPA_CQE_SIZE 64
3141a86b377SEli Cohen #define MLX5_VDPA_LOG_CQE_SIZE ilog2(MLX5_VDPA_CQE_SIZE)
3151a86b377SEli Cohen 
3161a86b377SEli Cohen static int cq_frag_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_cq_buf *buf, int nent)
3171a86b377SEli Cohen {
3181a86b377SEli Cohen 	struct mlx5_frag_buf *frag_buf = &buf->frag_buf;
3191a86b377SEli Cohen 	u8 log_wq_stride = MLX5_VDPA_LOG_CQE_SIZE;
3201a86b377SEli Cohen 	u8 log_wq_sz = MLX5_VDPA_LOG_CQE_SIZE;
3211a86b377SEli Cohen 	int err;
3221a86b377SEli Cohen 
3231a86b377SEli Cohen 	err = mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, nent * MLX5_VDPA_CQE_SIZE, frag_buf,
3241a86b377SEli Cohen 				       ndev->mvdev.mdev->priv.numa_node);
3251a86b377SEli Cohen 	if (err)
3261a86b377SEli Cohen 		return err;
3271a86b377SEli Cohen 
3281a86b377SEli Cohen 	mlx5_init_fbc(frag_buf->frags, log_wq_stride, log_wq_sz, &buf->fbc);
3291a86b377SEli Cohen 
3301a86b377SEli Cohen 	buf->cqe_size = MLX5_VDPA_CQE_SIZE;
3311a86b377SEli Cohen 	buf->nent = nent;
3321a86b377SEli Cohen 
3331a86b377SEli Cohen 	return 0;
3341a86b377SEli Cohen }
3351a86b377SEli Cohen 
3361a86b377SEli Cohen static int umem_frag_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_umem *umem, int size)
3371a86b377SEli Cohen {
3381a86b377SEli Cohen 	struct mlx5_frag_buf *frag_buf = &umem->frag_buf;
3391a86b377SEli Cohen 
3401a86b377SEli Cohen 	return mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, size, frag_buf,
3411a86b377SEli Cohen 					ndev->mvdev.mdev->priv.numa_node);
3421a86b377SEli Cohen }
3431a86b377SEli Cohen 
3441a86b377SEli Cohen static void cq_frag_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_cq_buf *buf)
3451a86b377SEli Cohen {
3461a86b377SEli Cohen 	mlx5_frag_buf_free(ndev->mvdev.mdev, &buf->frag_buf);
3471a86b377SEli Cohen }
3481a86b377SEli Cohen 
3491a86b377SEli Cohen static void *get_cqe(struct mlx5_vdpa_cq *vcq, int n)
3501a86b377SEli Cohen {
3511a86b377SEli Cohen 	return mlx5_frag_buf_get_wqe(&vcq->buf.fbc, n);
3521a86b377SEli Cohen }
3531a86b377SEli Cohen 
3541a86b377SEli Cohen static void cq_frag_buf_init(struct mlx5_vdpa_cq *vcq, struct mlx5_vdpa_cq_buf *buf)
3551a86b377SEli Cohen {
3561a86b377SEli Cohen 	struct mlx5_cqe64 *cqe64;
3571a86b377SEli Cohen 	void *cqe;
3581a86b377SEli Cohen 	int i;
3591a86b377SEli Cohen 
3601a86b377SEli Cohen 	for (i = 0; i < buf->nent; i++) {
3611a86b377SEli Cohen 		cqe = get_cqe(vcq, i);
3621a86b377SEli Cohen 		cqe64 = cqe;
3631a86b377SEli Cohen 		cqe64->op_own = MLX5_CQE_INVALID << 4;
3641a86b377SEli Cohen 	}
3651a86b377SEli Cohen }
3661a86b377SEli Cohen 
3671a86b377SEli Cohen static void *get_sw_cqe(struct mlx5_vdpa_cq *cq, int n)
3681a86b377SEli Cohen {
3691a86b377SEli Cohen 	struct mlx5_cqe64 *cqe64 = get_cqe(cq, n & (cq->cqe - 1));
3701a86b377SEli Cohen 
3711a86b377SEli Cohen 	if (likely(get_cqe_opcode(cqe64) != MLX5_CQE_INVALID) &&
3721a86b377SEli Cohen 	    !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & cq->cqe)))
3731a86b377SEli Cohen 		return cqe64;
3741a86b377SEli Cohen 
3751a86b377SEli Cohen 	return NULL;
3761a86b377SEli Cohen }
3771a86b377SEli Cohen 
3781a86b377SEli Cohen static void rx_post(struct mlx5_vdpa_qp *vqp, int n)
3791a86b377SEli Cohen {
3801a86b377SEli Cohen 	vqp->head += n;
3811a86b377SEli Cohen 	vqp->db.db[0] = cpu_to_be32(vqp->head);
3821a86b377SEli Cohen }
3831a86b377SEli Cohen 
3841a86b377SEli Cohen static void qp_prepare(struct mlx5_vdpa_net *ndev, bool fw, void *in,
3851a86b377SEli Cohen 		       struct mlx5_vdpa_virtqueue *mvq, u32 num_ent)
3861a86b377SEli Cohen {
3871a86b377SEli Cohen 	struct mlx5_vdpa_qp *vqp;
3881a86b377SEli Cohen 	__be64 *pas;
3891a86b377SEli Cohen 	void *qpc;
3901a86b377SEli Cohen 
3911a86b377SEli Cohen 	vqp = fw ? &mvq->fwqp : &mvq->vqqp;
3921a86b377SEli Cohen 	MLX5_SET(create_qp_in, in, uid, ndev->mvdev.res.uid);
3931a86b377SEli Cohen 	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
3941a86b377SEli Cohen 	if (vqp->fw) {
3951a86b377SEli Cohen 		/* Firmware QP is allocated by the driver for the firmware's
3961a86b377SEli Cohen 		 * use so we can skip part of the params as they will be chosen by firmware
3971a86b377SEli Cohen 		 */
3981a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
3991a86b377SEli Cohen 		MLX5_SET(qpc, qpc, rq_type, MLX5_ZERO_LEN_RQ);
4001a86b377SEli Cohen 		MLX5_SET(qpc, qpc, no_sq, 1);
4011a86b377SEli Cohen 		return;
4021a86b377SEli Cohen 	}
4031a86b377SEli Cohen 
4041a86b377SEli Cohen 	MLX5_SET(qpc, qpc, st, MLX5_QP_ST_RC);
4051a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
4061a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pd, ndev->mvdev.res.pdn);
4071a86b377SEli Cohen 	MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES);
4081a86b377SEli Cohen 	MLX5_SET(qpc, qpc, uar_page, ndev->mvdev.res.uar->index);
4091a86b377SEli Cohen 	MLX5_SET(qpc, qpc, log_page_size, vqp->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
4101a86b377SEli Cohen 	MLX5_SET(qpc, qpc, no_sq, 1);
4111a86b377SEli Cohen 	MLX5_SET(qpc, qpc, cqn_rcv, mvq->cq.mcq.cqn);
4121a86b377SEli Cohen 	MLX5_SET(qpc, qpc, log_rq_size, ilog2(num_ent));
4131a86b377SEli Cohen 	MLX5_SET(qpc, qpc, rq_type, MLX5_NON_ZERO_RQ);
4141a86b377SEli Cohen 	pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, in, pas);
4151a86b377SEli Cohen 	mlx5_fill_page_frag_array(&vqp->frag_buf, pas);
4161a86b377SEli Cohen }
4171a86b377SEli Cohen 
4181a86b377SEli Cohen static int rq_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp, u32 num_ent)
4191a86b377SEli Cohen {
4201a86b377SEli Cohen 	return mlx5_frag_buf_alloc_node(ndev->mvdev.mdev,
4211a86b377SEli Cohen 					num_ent * sizeof(struct mlx5_wqe_data_seg), &vqp->frag_buf,
4221a86b377SEli Cohen 					ndev->mvdev.mdev->priv.numa_node);
4231a86b377SEli Cohen }
4241a86b377SEli Cohen 
4251a86b377SEli Cohen static void rq_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp)
4261a86b377SEli Cohen {
4271a86b377SEli Cohen 	mlx5_frag_buf_free(ndev->mvdev.mdev, &vqp->frag_buf);
4281a86b377SEli Cohen }
4291a86b377SEli Cohen 
4301a86b377SEli Cohen static int qp_create(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq,
4311a86b377SEli Cohen 		     struct mlx5_vdpa_qp *vqp)
4321a86b377SEli Cohen {
4331a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
4341a86b377SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
4351a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
4361a86b377SEli Cohen 	void *qpc;
4371a86b377SEli Cohen 	void *in;
4381a86b377SEli Cohen 	int err;
4391a86b377SEli Cohen 
4401a86b377SEli Cohen 	if (!vqp->fw) {
4411a86b377SEli Cohen 		vqp = &mvq->vqqp;
4421a86b377SEli Cohen 		err = rq_buf_alloc(ndev, vqp, mvq->num_ent);
4431a86b377SEli Cohen 		if (err)
4441a86b377SEli Cohen 			return err;
4451a86b377SEli Cohen 
4461a86b377SEli Cohen 		err = mlx5_db_alloc(ndev->mvdev.mdev, &vqp->db);
4471a86b377SEli Cohen 		if (err)
4481a86b377SEli Cohen 			goto err_db;
4491a86b377SEli Cohen 		inlen += vqp->frag_buf.npages * sizeof(__be64);
4501a86b377SEli Cohen 	}
4511a86b377SEli Cohen 
4521a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
4531a86b377SEli Cohen 	if (!in) {
4541a86b377SEli Cohen 		err = -ENOMEM;
4551a86b377SEli Cohen 		goto err_kzalloc;
4561a86b377SEli Cohen 	}
4571a86b377SEli Cohen 
4581a86b377SEli Cohen 	qp_prepare(ndev, vqp->fw, in, mvq, mvq->num_ent);
4591a86b377SEli Cohen 	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
4601a86b377SEli Cohen 	MLX5_SET(qpc, qpc, st, MLX5_QP_ST_RC);
4611a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
4621a86b377SEli Cohen 	MLX5_SET(qpc, qpc, pd, ndev->mvdev.res.pdn);
4631a86b377SEli Cohen 	MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES);
4641a86b377SEli Cohen 	if (!vqp->fw)
4651a86b377SEli Cohen 		MLX5_SET64(qpc, qpc, dbr_addr, vqp->db.dma);
4661a86b377SEli Cohen 	MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP);
4671a86b377SEli Cohen 	err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
4681a86b377SEli Cohen 	kfree(in);
4691a86b377SEli Cohen 	if (err)
4701a86b377SEli Cohen 		goto err_kzalloc;
4711a86b377SEli Cohen 
4721a86b377SEli Cohen 	vqp->mqp.uid = ndev->mvdev.res.uid;
4731a86b377SEli Cohen 	vqp->mqp.qpn = MLX5_GET(create_qp_out, out, qpn);
4741a86b377SEli Cohen 
4751a86b377SEli Cohen 	if (!vqp->fw)
4761a86b377SEli Cohen 		rx_post(vqp, mvq->num_ent);
4771a86b377SEli Cohen 
4781a86b377SEli Cohen 	return 0;
4791a86b377SEli Cohen 
4801a86b377SEli Cohen err_kzalloc:
4811a86b377SEli Cohen 	if (!vqp->fw)
4821a86b377SEli Cohen 		mlx5_db_free(ndev->mvdev.mdev, &vqp->db);
4831a86b377SEli Cohen err_db:
4841a86b377SEli Cohen 	if (!vqp->fw)
4851a86b377SEli Cohen 		rq_buf_free(ndev, vqp);
4861a86b377SEli Cohen 
4871a86b377SEli Cohen 	return err;
4881a86b377SEli Cohen }
4891a86b377SEli Cohen 
4901a86b377SEli Cohen static void qp_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp)
4911a86b377SEli Cohen {
4921a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {};
4931a86b377SEli Cohen 
4941a86b377SEli Cohen 	MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
4951a86b377SEli Cohen 	MLX5_SET(destroy_qp_in, in, qpn, vqp->mqp.qpn);
4961a86b377SEli Cohen 	MLX5_SET(destroy_qp_in, in, uid, ndev->mvdev.res.uid);
4971a86b377SEli Cohen 	if (mlx5_cmd_exec_in(ndev->mvdev.mdev, destroy_qp, in))
4981a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "destroy qp 0x%x\n", vqp->mqp.qpn);
4991a86b377SEli Cohen 	if (!vqp->fw) {
5001a86b377SEli Cohen 		mlx5_db_free(ndev->mvdev.mdev, &vqp->db);
5011a86b377SEli Cohen 		rq_buf_free(ndev, vqp);
5021a86b377SEli Cohen 	}
5031a86b377SEli Cohen }
5041a86b377SEli Cohen 
5051a86b377SEli Cohen static void *next_cqe_sw(struct mlx5_vdpa_cq *cq)
5061a86b377SEli Cohen {
5071a86b377SEli Cohen 	return get_sw_cqe(cq, cq->mcq.cons_index);
5081a86b377SEli Cohen }
5091a86b377SEli Cohen 
5101a86b377SEli Cohen static int mlx5_vdpa_poll_one(struct mlx5_vdpa_cq *vcq)
5111a86b377SEli Cohen {
5121a86b377SEli Cohen 	struct mlx5_cqe64 *cqe64;
5131a86b377SEli Cohen 
5141a86b377SEli Cohen 	cqe64 = next_cqe_sw(vcq);
5151a86b377SEli Cohen 	if (!cqe64)
5161a86b377SEli Cohen 		return -EAGAIN;
5171a86b377SEli Cohen 
5181a86b377SEli Cohen 	vcq->mcq.cons_index++;
5191a86b377SEli Cohen 	return 0;
5201a86b377SEli Cohen }
5211a86b377SEli Cohen 
5221a86b377SEli Cohen static void mlx5_vdpa_handle_completions(struct mlx5_vdpa_virtqueue *mvq, int num)
5231a86b377SEli Cohen {
524db296d25SEli Cohen 	struct mlx5_vdpa_net *ndev = mvq->ndev;
525db296d25SEli Cohen 	struct vdpa_callback *event_cb;
526db296d25SEli Cohen 
527db296d25SEli Cohen 	event_cb = &ndev->event_cbs[mvq->index];
5281a86b377SEli Cohen 	mlx5_cq_set_ci(&mvq->cq.mcq);
52983ef73b2SEli Cohen 
53083ef73b2SEli Cohen 	/* make sure CQ cosumer update is visible to the hardware before updating
53183ef73b2SEli Cohen 	 * RX doorbell record.
53283ef73b2SEli Cohen 	 */
53383ef73b2SEli Cohen 	dma_wmb();
5341a86b377SEli Cohen 	rx_post(&mvq->vqqp, num);
535db296d25SEli Cohen 	if (event_cb->callback)
536db296d25SEli Cohen 		event_cb->callback(event_cb->private);
5371a86b377SEli Cohen }
5381a86b377SEli Cohen 
5391a86b377SEli Cohen static void mlx5_vdpa_cq_comp(struct mlx5_core_cq *mcq, struct mlx5_eqe *eqe)
5401a86b377SEli Cohen {
5411a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq = container_of(mcq, struct mlx5_vdpa_virtqueue, cq.mcq);
5421a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = mvq->ndev;
5431a86b377SEli Cohen 	void __iomem *uar_page = ndev->mvdev.res.uar->map;
5441a86b377SEli Cohen 	int num = 0;
5451a86b377SEli Cohen 
5461a86b377SEli Cohen 	while (!mlx5_vdpa_poll_one(&mvq->cq)) {
5471a86b377SEli Cohen 		num++;
5481a86b377SEli Cohen 		if (num > mvq->num_ent / 2) {
5491a86b377SEli Cohen 			/* If completions keep coming while we poll, we want to
5501a86b377SEli Cohen 			 * let the hardware know that we consumed them by
5511a86b377SEli Cohen 			 * updating the doorbell record.  We also let vdpa core
5521a86b377SEli Cohen 			 * know about this so it passes it on the virtio driver
5531a86b377SEli Cohen 			 * on the guest.
5541a86b377SEli Cohen 			 */
5551a86b377SEli Cohen 			mlx5_vdpa_handle_completions(mvq, num);
5561a86b377SEli Cohen 			num = 0;
5571a86b377SEli Cohen 		}
5581a86b377SEli Cohen 	}
5591a86b377SEli Cohen 
5601a86b377SEli Cohen 	if (num)
5611a86b377SEli Cohen 		mlx5_vdpa_handle_completions(mvq, num);
5621a86b377SEli Cohen 
5631a86b377SEli Cohen 	mlx5_cq_arm(&mvq->cq.mcq, MLX5_CQ_DB_REQ_NOT, uar_page, mvq->cq.mcq.cons_index);
5641a86b377SEli Cohen }
5651a86b377SEli Cohen 
5661a86b377SEli Cohen static int cq_create(struct mlx5_vdpa_net *ndev, u16 idx, u32 num_ent)
5671a86b377SEli Cohen {
5681a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx];
5691a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
5701a86b377SEli Cohen 	void __iomem *uar_page = ndev->mvdev.res.uar->map;
5711a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_cq_out)];
5721a86b377SEli Cohen 	struct mlx5_vdpa_cq *vcq = &mvq->cq;
5731a86b377SEli Cohen 	__be64 *pas;
5741a86b377SEli Cohen 	int inlen;
5751a86b377SEli Cohen 	void *cqc;
5761a86b377SEli Cohen 	void *in;
5771a86b377SEli Cohen 	int err;
5781a86b377SEli Cohen 	int eqn;
5791a86b377SEli Cohen 
5801a86b377SEli Cohen 	err = mlx5_db_alloc(mdev, &vcq->db);
5811a86b377SEli Cohen 	if (err)
5821a86b377SEli Cohen 		return err;
5831a86b377SEli Cohen 
5841a86b377SEli Cohen 	vcq->mcq.set_ci_db = vcq->db.db;
5851a86b377SEli Cohen 	vcq->mcq.arm_db = vcq->db.db + 1;
5861a86b377SEli Cohen 	vcq->mcq.cqe_sz = 64;
5871a86b377SEli Cohen 
5881a86b377SEli Cohen 	err = cq_frag_buf_alloc(ndev, &vcq->buf, num_ent);
5891a86b377SEli Cohen 	if (err)
5901a86b377SEli Cohen 		goto err_db;
5911a86b377SEli Cohen 
5921a86b377SEli Cohen 	cq_frag_buf_init(vcq, &vcq->buf);
5931a86b377SEli Cohen 
5941a86b377SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
5951a86b377SEli Cohen 		MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * vcq->buf.frag_buf.npages;
5961a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
5971a86b377SEli Cohen 	if (!in) {
5981a86b377SEli Cohen 		err = -ENOMEM;
5991a86b377SEli Cohen 		goto err_vzalloc;
6001a86b377SEli Cohen 	}
6011a86b377SEli Cohen 
6021a86b377SEli Cohen 	MLX5_SET(create_cq_in, in, uid, ndev->mvdev.res.uid);
6031a86b377SEli Cohen 	pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas);
6041a86b377SEli Cohen 	mlx5_fill_page_frag_array(&vcq->buf.frag_buf, pas);
6051a86b377SEli Cohen 
6061a86b377SEli Cohen 	cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
6071a86b377SEli Cohen 	MLX5_SET(cqc, cqc, log_page_size, vcq->buf.frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
6081a86b377SEli Cohen 
6091a86b377SEli Cohen 	/* Use vector 0 by default. Consider adding code to choose least used
6101a86b377SEli Cohen 	 * vector.
6111a86b377SEli Cohen 	 */
612563476aeSShay Drory 	err = mlx5_vector2eqn(mdev, 0, &eqn);
6131a86b377SEli Cohen 	if (err)
6141a86b377SEli Cohen 		goto err_vec;
6151a86b377SEli Cohen 
6161a86b377SEli Cohen 	cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
6171a86b377SEli Cohen 	MLX5_SET(cqc, cqc, log_cq_size, ilog2(num_ent));
6181a86b377SEli Cohen 	MLX5_SET(cqc, cqc, uar_page, ndev->mvdev.res.uar->index);
619616d5769STal Gilboa 	MLX5_SET(cqc, cqc, c_eqn_or_apu_element, eqn);
6201a86b377SEli Cohen 	MLX5_SET64(cqc, cqc, dbr_addr, vcq->db.dma);
6211a86b377SEli Cohen 
6221a86b377SEli Cohen 	err = mlx5_core_create_cq(mdev, &vcq->mcq, in, inlen, out, sizeof(out));
6231a86b377SEli Cohen 	if (err)
6241a86b377SEli Cohen 		goto err_vec;
6251a86b377SEli Cohen 
6261a86b377SEli Cohen 	vcq->mcq.comp = mlx5_vdpa_cq_comp;
6271a86b377SEli Cohen 	vcq->cqe = num_ent;
6281a86b377SEli Cohen 	vcq->mcq.set_ci_db = vcq->db.db;
6291a86b377SEli Cohen 	vcq->mcq.arm_db = vcq->db.db + 1;
6301a86b377SEli Cohen 	mlx5_cq_arm(&mvq->cq.mcq, MLX5_CQ_DB_REQ_NOT, uar_page, mvq->cq.mcq.cons_index);
6311a86b377SEli Cohen 	kfree(in);
6321a86b377SEli Cohen 	return 0;
6331a86b377SEli Cohen 
6341a86b377SEli Cohen err_vec:
6351a86b377SEli Cohen 	kfree(in);
6361a86b377SEli Cohen err_vzalloc:
6371a86b377SEli Cohen 	cq_frag_buf_free(ndev, &vcq->buf);
6381a86b377SEli Cohen err_db:
6391a86b377SEli Cohen 	mlx5_db_free(ndev->mvdev.mdev, &vcq->db);
6401a86b377SEli Cohen 	return err;
6411a86b377SEli Cohen }
6421a86b377SEli Cohen 
6431a86b377SEli Cohen static void cq_destroy(struct mlx5_vdpa_net *ndev, u16 idx)
6441a86b377SEli Cohen {
6451a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx];
6461a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
6471a86b377SEli Cohen 	struct mlx5_vdpa_cq *vcq = &mvq->cq;
6481a86b377SEli Cohen 
6491a86b377SEli Cohen 	if (mlx5_core_destroy_cq(mdev, &vcq->mcq)) {
6501a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "destroy CQ 0x%x\n", vcq->mcq.cqn);
6511a86b377SEli Cohen 		return;
6521a86b377SEli Cohen 	}
6531a86b377SEli Cohen 	cq_frag_buf_free(ndev, &vcq->buf);
6541a86b377SEli Cohen 	mlx5_db_free(ndev->mvdev.mdev, &vcq->db);
6551a86b377SEli Cohen }
6561a86b377SEli Cohen 
65771ab6a7cSEli Cohen static void set_umem_size(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num,
6581a86b377SEli Cohen 			  struct mlx5_vdpa_umem **umemp)
6591a86b377SEli Cohen {
6601a86b377SEli Cohen 	struct mlx5_core_dev *mdev = ndev->mvdev.mdev;
6611a86b377SEli Cohen 	int p_a;
6621a86b377SEli Cohen 	int p_b;
6631a86b377SEli Cohen 
6641a86b377SEli Cohen 	switch (num) {
6651a86b377SEli Cohen 	case 1:
6661a86b377SEli Cohen 		p_a = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_1_buffer_param_a);
6671a86b377SEli Cohen 		p_b = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_1_buffer_param_b);
6681a86b377SEli Cohen 		*umemp = &mvq->umem1;
6691a86b377SEli Cohen 		break;
6701a86b377SEli Cohen 	case 2:
6711a86b377SEli Cohen 		p_a = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_2_buffer_param_a);
6721a86b377SEli Cohen 		p_b = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_2_buffer_param_b);
6731a86b377SEli Cohen 		*umemp = &mvq->umem2;
6741a86b377SEli Cohen 		break;
6751a86b377SEli Cohen 	case 3:
6761a86b377SEli Cohen 		p_a = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_3_buffer_param_a);
6771a86b377SEli Cohen 		p_b = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_3_buffer_param_b);
6781a86b377SEli Cohen 		*umemp = &mvq->umem3;
6791a86b377SEli Cohen 		break;
6801a86b377SEli Cohen 	}
68171ab6a7cSEli Cohen 	(*umemp)->size = p_a * mvq->num_ent + p_b;
6821a86b377SEli Cohen }
6831a86b377SEli Cohen 
6841a86b377SEli Cohen static void umem_frag_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_umem *umem)
6851a86b377SEli Cohen {
6861a86b377SEli Cohen 	mlx5_frag_buf_free(ndev->mvdev.mdev, &umem->frag_buf);
6871a86b377SEli Cohen }
6881a86b377SEli Cohen 
6891a86b377SEli Cohen static int create_umem(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num)
6901a86b377SEli Cohen {
6911a86b377SEli Cohen 	int inlen;
6921a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_umem_out)] = {};
6931a86b377SEli Cohen 	void *um;
6941a86b377SEli Cohen 	void *in;
6951a86b377SEli Cohen 	int err;
6961a86b377SEli Cohen 	__be64 *pas;
6971a86b377SEli Cohen 	struct mlx5_vdpa_umem *umem;
6981a86b377SEli Cohen 
69971ab6a7cSEli Cohen 	set_umem_size(ndev, mvq, num, &umem);
70071ab6a7cSEli Cohen 	err = umem_frag_buf_alloc(ndev, umem, umem->size);
7011a86b377SEli Cohen 	if (err)
7021a86b377SEli Cohen 		return err;
7031a86b377SEli Cohen 
7041a86b377SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(create_umem_in) + MLX5_ST_SZ_BYTES(mtt) * umem->frag_buf.npages;
7051a86b377SEli Cohen 
7061a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
7071a86b377SEli Cohen 	if (!in) {
7081a86b377SEli Cohen 		err = -ENOMEM;
7091a86b377SEli Cohen 		goto err_in;
7101a86b377SEli Cohen 	}
7111a86b377SEli Cohen 
7121a86b377SEli Cohen 	MLX5_SET(create_umem_in, in, opcode, MLX5_CMD_OP_CREATE_UMEM);
7131a86b377SEli Cohen 	MLX5_SET(create_umem_in, in, uid, ndev->mvdev.res.uid);
7141a86b377SEli Cohen 	um = MLX5_ADDR_OF(create_umem_in, in, umem);
7151a86b377SEli Cohen 	MLX5_SET(umem, um, log_page_size, umem->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
7161a86b377SEli Cohen 	MLX5_SET64(umem, um, num_of_mtt, umem->frag_buf.npages);
7171a86b377SEli Cohen 
7181a86b377SEli Cohen 	pas = (__be64 *)MLX5_ADDR_OF(umem, um, mtt[0]);
7191a86b377SEli Cohen 	mlx5_fill_page_frag_array_perm(&umem->frag_buf, pas, MLX5_MTT_PERM_RW);
7201a86b377SEli Cohen 
7211a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out));
7221a86b377SEli Cohen 	if (err) {
7231a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "create umem(%d)\n", err);
7241a86b377SEli Cohen 		goto err_cmd;
7251a86b377SEli Cohen 	}
7261a86b377SEli Cohen 
7271a86b377SEli Cohen 	kfree(in);
7281a86b377SEli Cohen 	umem->id = MLX5_GET(create_umem_out, out, umem_id);
7291a86b377SEli Cohen 
7301a86b377SEli Cohen 	return 0;
7311a86b377SEli Cohen 
7321a86b377SEli Cohen err_cmd:
7331a86b377SEli Cohen 	kfree(in);
7341a86b377SEli Cohen err_in:
7351a86b377SEli Cohen 	umem_frag_buf_free(ndev, umem);
7361a86b377SEli Cohen 	return err;
7371a86b377SEli Cohen }
7381a86b377SEli Cohen 
7391a86b377SEli Cohen static void umem_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num)
7401a86b377SEli Cohen {
7411a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_umem_in)] = {};
7421a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(destroy_umem_out)] = {};
7431a86b377SEli Cohen 	struct mlx5_vdpa_umem *umem;
7441a86b377SEli Cohen 
7451a86b377SEli Cohen 	switch (num) {
7461a86b377SEli Cohen 	case 1:
7471a86b377SEli Cohen 		umem = &mvq->umem1;
7481a86b377SEli Cohen 		break;
7491a86b377SEli Cohen 	case 2:
7501a86b377SEli Cohen 		umem = &mvq->umem2;
7511a86b377SEli Cohen 		break;
7521a86b377SEli Cohen 	case 3:
7531a86b377SEli Cohen 		umem = &mvq->umem3;
7541a86b377SEli Cohen 		break;
7551a86b377SEli Cohen 	}
7561a86b377SEli Cohen 
7571a86b377SEli Cohen 	MLX5_SET(destroy_umem_in, in, opcode, MLX5_CMD_OP_DESTROY_UMEM);
7581a86b377SEli Cohen 	MLX5_SET(destroy_umem_in, in, umem_id, umem->id);
7591a86b377SEli Cohen 	if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out)))
7601a86b377SEli Cohen 		return;
7611a86b377SEli Cohen 
7621a86b377SEli Cohen 	umem_frag_buf_free(ndev, umem);
7631a86b377SEli Cohen }
7641a86b377SEli Cohen 
7651a86b377SEli Cohen static int umems_create(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
7661a86b377SEli Cohen {
7671a86b377SEli Cohen 	int num;
7681a86b377SEli Cohen 	int err;
7691a86b377SEli Cohen 
7701a86b377SEli Cohen 	for (num = 1; num <= 3; num++) {
7711a86b377SEli Cohen 		err = create_umem(ndev, mvq, num);
7721a86b377SEli Cohen 		if (err)
7731a86b377SEli Cohen 			goto err_umem;
7741a86b377SEli Cohen 	}
7751a86b377SEli Cohen 	return 0;
7761a86b377SEli Cohen 
7771a86b377SEli Cohen err_umem:
7781a86b377SEli Cohen 	for (num--; num > 0; num--)
7791a86b377SEli Cohen 		umem_destroy(ndev, mvq, num);
7801a86b377SEli Cohen 
7811a86b377SEli Cohen 	return err;
7821a86b377SEli Cohen }
7831a86b377SEli Cohen 
7841a86b377SEli Cohen static void umems_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
7851a86b377SEli Cohen {
7861a86b377SEli Cohen 	int num;
7871a86b377SEli Cohen 
7881a86b377SEli Cohen 	for (num = 3; num > 0; num--)
7891a86b377SEli Cohen 		umem_destroy(ndev, mvq, num);
7901a86b377SEli Cohen }
7911a86b377SEli Cohen 
7921a86b377SEli Cohen static int get_queue_type(struct mlx5_vdpa_net *ndev)
7931a86b377SEli Cohen {
7941a86b377SEli Cohen 	u32 type_mask;
7951a86b377SEli Cohen 
7961a86b377SEli Cohen 	type_mask = MLX5_CAP_DEV_VDPA_EMULATION(ndev->mvdev.mdev, virtio_queue_type);
7971a86b377SEli Cohen 
7981a86b377SEli Cohen 	/* prefer split queue */
799879753c8SEli Cohen 	if (type_mask & MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_SPLIT)
8001a86b377SEli Cohen 		return MLX5_VIRTIO_EMULATION_VIRTIO_QUEUE_TYPE_SPLIT;
801879753c8SEli Cohen 
802879753c8SEli Cohen 	WARN_ON(!(type_mask & MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_PACKED));
803879753c8SEli Cohen 
804879753c8SEli Cohen 	return MLX5_VIRTIO_EMULATION_VIRTIO_QUEUE_TYPE_PACKED;
8051a86b377SEli Cohen }
8061a86b377SEli Cohen 
8071a86b377SEli Cohen static bool vq_is_tx(u16 idx)
8081a86b377SEli Cohen {
8091a86b377SEli Cohen 	return idx % 2;
8101a86b377SEli Cohen }
8111a86b377SEli Cohen 
8121a86b377SEli Cohen static u16 get_features_12_3(u64 features)
8131a86b377SEli Cohen {
814cbb52359SNathan Chancellor 	return (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_TSO4)) << 9) |
815cbb52359SNathan Chancellor 	       (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_TSO6)) << 8) |
816cbb52359SNathan Chancellor 	       (!!(features & BIT_ULL(VIRTIO_NET_F_CSUM)) << 7) |
817cbb52359SNathan Chancellor 	       (!!(features & BIT_ULL(VIRTIO_NET_F_GUEST_CSUM)) << 6);
8181a86b377SEli Cohen }
8191a86b377SEli Cohen 
8201a86b377SEli Cohen static int create_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
8211a86b377SEli Cohen {
8221a86b377SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(create_virtio_net_q_in);
8231a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(create_virtio_net_q_out)] = {};
8241a86b377SEli Cohen 	void *obj_context;
8251a86b377SEli Cohen 	void *cmd_hdr;
8261a86b377SEli Cohen 	void *vq_ctx;
8271a86b377SEli Cohen 	void *in;
8281a86b377SEli Cohen 	int err;
8291a86b377SEli Cohen 
8301a86b377SEli Cohen 	err = umems_create(ndev, mvq);
8311a86b377SEli Cohen 	if (err)
8321a86b377SEli Cohen 		return err;
8331a86b377SEli Cohen 
8341a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
8351a86b377SEli Cohen 	if (!in) {
8361a86b377SEli Cohen 		err = -ENOMEM;
8371a86b377SEli Cohen 		goto err_alloc;
8381a86b377SEli Cohen 	}
8391a86b377SEli Cohen 
8401a86b377SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(create_virtio_net_q_in, in, general_obj_in_cmd_hdr);
8411a86b377SEli Cohen 
8421a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
8431a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q);
8441a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
8451a86b377SEli Cohen 
8461a86b377SEli Cohen 	obj_context = MLX5_ADDR_OF(create_virtio_net_q_in, in, obj_context);
8471a86b377SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, hw_available_index, mvq->avail_idx);
848b35ccebeSEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, hw_used_index, mvq->used_idx);
8491a86b377SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, queue_feature_bit_mask_12_3,
8501a86b377SEli Cohen 		 get_features_12_3(ndev->mvdev.actual_features));
8511a86b377SEli Cohen 	vq_ctx = MLX5_ADDR_OF(virtio_net_q_object, obj_context, virtio_q_context);
8521a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, virtio_q_type, get_queue_type(ndev));
8531a86b377SEli Cohen 
8541a86b377SEli Cohen 	if (vq_is_tx(mvq->index))
8551a86b377SEli Cohen 		MLX5_SET(virtio_net_q_object, obj_context, tisn_or_qpn, ndev->res.tisn);
8561a86b377SEli Cohen 
8571a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, event_mode, MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE);
8581a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, queue_index, mvq->index);
8591a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, event_qpn_or_msix, mvq->fwqp.mqp.qpn);
8601a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, queue_size, mvq->num_ent);
8611a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, virtio_version_1_0,
8624b454a82SEli Cohen 		 !!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_F_VERSION_1)));
8631a86b377SEli Cohen 	MLX5_SET64(virtio_q, vq_ctx, desc_addr, mvq->desc_addr);
8641a86b377SEli Cohen 	MLX5_SET64(virtio_q, vq_ctx, used_addr, mvq->device_addr);
8651a86b377SEli Cohen 	MLX5_SET64(virtio_q, vq_ctx, available_addr, mvq->driver_addr);
86683fec3f1SAharon Landau 	MLX5_SET(virtio_q, vq_ctx, virtio_q_mkey, ndev->mvdev.mr.mkey);
8671a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_1_id, mvq->umem1.id);
8681a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_1_size, mvq->umem1.size);
8691a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_2_id, mvq->umem2.id);
870e3011776SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_2_size, mvq->umem2.size);
8711a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_3_id, mvq->umem3.id);
872e3011776SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, umem_3_size, mvq->umem3.size);
8731a86b377SEli Cohen 	MLX5_SET(virtio_q, vq_ctx, pd, ndev->mvdev.res.pdn);
8741a86b377SEli Cohen 
8751a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out));
8761a86b377SEli Cohen 	if (err)
8771a86b377SEli Cohen 		goto err_cmd;
8781a86b377SEli Cohen 
8791a86b377SEli Cohen 	kfree(in);
8801a86b377SEli Cohen 	mvq->virtq_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
8811a86b377SEli Cohen 
8821a86b377SEli Cohen 	return 0;
8831a86b377SEli Cohen 
8841a86b377SEli Cohen err_cmd:
8851a86b377SEli Cohen 	kfree(in);
8861a86b377SEli Cohen err_alloc:
8871a86b377SEli Cohen 	umems_destroy(ndev, mvq);
8881a86b377SEli Cohen 	return err;
8891a86b377SEli Cohen }
8901a86b377SEli Cohen 
8911a86b377SEli Cohen static void destroy_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
8921a86b377SEli Cohen {
8931a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(destroy_virtio_net_q_in)] = {};
8941a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(destroy_virtio_net_q_out)] = {};
8951a86b377SEli Cohen 
8961a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.opcode,
8971a86b377SEli Cohen 		 MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
8981a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.obj_id, mvq->virtq_id);
8991a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.uid, ndev->mvdev.res.uid);
9001a86b377SEli Cohen 	MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.obj_type,
9011a86b377SEli Cohen 		 MLX5_OBJ_TYPE_VIRTIO_NET_Q);
9021a86b377SEli Cohen 	if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out))) {
9031a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "destroy virtqueue 0x%x\n", mvq->virtq_id);
9041a86b377SEli Cohen 		return;
9051a86b377SEli Cohen 	}
9061a86b377SEli Cohen 	umems_destroy(ndev, mvq);
9071a86b377SEli Cohen }
9081a86b377SEli Cohen 
9091a86b377SEli Cohen static u32 get_rqpn(struct mlx5_vdpa_virtqueue *mvq, bool fw)
9101a86b377SEli Cohen {
9111a86b377SEli Cohen 	return fw ? mvq->vqqp.mqp.qpn : mvq->fwqp.mqp.qpn;
9121a86b377SEli Cohen }
9131a86b377SEli Cohen 
9141a86b377SEli Cohen static u32 get_qpn(struct mlx5_vdpa_virtqueue *mvq, bool fw)
9151a86b377SEli Cohen {
9161a86b377SEli Cohen 	return fw ? mvq->fwqp.mqp.qpn : mvq->vqqp.mqp.qpn;
9171a86b377SEli Cohen }
9181a86b377SEli Cohen 
9191a86b377SEli Cohen static void alloc_inout(struct mlx5_vdpa_net *ndev, int cmd, void **in, int *inlen, void **out,
9201a86b377SEli Cohen 			int *outlen, u32 qpn, u32 rqpn)
9211a86b377SEli Cohen {
9221a86b377SEli Cohen 	void *qpc;
9231a86b377SEli Cohen 	void *pp;
9241a86b377SEli Cohen 
9251a86b377SEli Cohen 	switch (cmd) {
9261a86b377SEli Cohen 	case MLX5_CMD_OP_2RST_QP:
9271a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(qp_2rst_in);
9281a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(qp_2rst_out);
9291a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
9301a86b377SEli Cohen 		*out = kzalloc(*outlen, GFP_KERNEL);
931f31231bfSColin Ian King 		if (!*in || !*out)
9321a86b377SEli Cohen 			goto outerr;
9331a86b377SEli Cohen 
9341a86b377SEli Cohen 		MLX5_SET(qp_2rst_in, *in, opcode, cmd);
9351a86b377SEli Cohen 		MLX5_SET(qp_2rst_in, *in, uid, ndev->mvdev.res.uid);
9361a86b377SEli Cohen 		MLX5_SET(qp_2rst_in, *in, qpn, qpn);
9371a86b377SEli Cohen 		break;
9381a86b377SEli Cohen 	case MLX5_CMD_OP_RST2INIT_QP:
9391a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(rst2init_qp_in);
9401a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(rst2init_qp_out);
9411a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
9421a86b377SEli Cohen 		*out = kzalloc(MLX5_ST_SZ_BYTES(rst2init_qp_out), GFP_KERNEL);
943f31231bfSColin Ian King 		if (!*in || !*out)
9441a86b377SEli Cohen 			goto outerr;
9451a86b377SEli Cohen 
9461a86b377SEli Cohen 		MLX5_SET(rst2init_qp_in, *in, opcode, cmd);
9471a86b377SEli Cohen 		MLX5_SET(rst2init_qp_in, *in, uid, ndev->mvdev.res.uid);
9481a86b377SEli Cohen 		MLX5_SET(rst2init_qp_in, *in, qpn, qpn);
9491a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc);
9501a86b377SEli Cohen 		MLX5_SET(qpc, qpc, remote_qpn, rqpn);
9511a86b377SEli Cohen 		MLX5_SET(qpc, qpc, rwe, 1);
9521a86b377SEli Cohen 		pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
9531a86b377SEli Cohen 		MLX5_SET(ads, pp, vhca_port_num, 1);
9541a86b377SEli Cohen 		break;
9551a86b377SEli Cohen 	case MLX5_CMD_OP_INIT2RTR_QP:
9561a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(init2rtr_qp_in);
9571a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(init2rtr_qp_out);
9581a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
9591a86b377SEli Cohen 		*out = kzalloc(MLX5_ST_SZ_BYTES(init2rtr_qp_out), GFP_KERNEL);
960f31231bfSColin Ian King 		if (!*in || !*out)
9611a86b377SEli Cohen 			goto outerr;
9621a86b377SEli Cohen 
9631a86b377SEli Cohen 		MLX5_SET(init2rtr_qp_in, *in, opcode, cmd);
9641a86b377SEli Cohen 		MLX5_SET(init2rtr_qp_in, *in, uid, ndev->mvdev.res.uid);
9651a86b377SEli Cohen 		MLX5_SET(init2rtr_qp_in, *in, qpn, qpn);
9661a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc);
9671a86b377SEli Cohen 		MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES);
9681a86b377SEli Cohen 		MLX5_SET(qpc, qpc, log_msg_max, 30);
9691a86b377SEli Cohen 		MLX5_SET(qpc, qpc, remote_qpn, rqpn);
9701a86b377SEli Cohen 		pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
9711a86b377SEli Cohen 		MLX5_SET(ads, pp, fl, 1);
9721a86b377SEli Cohen 		break;
9731a86b377SEli Cohen 	case MLX5_CMD_OP_RTR2RTS_QP:
9741a86b377SEli Cohen 		*inlen = MLX5_ST_SZ_BYTES(rtr2rts_qp_in);
9751a86b377SEli Cohen 		*outlen = MLX5_ST_SZ_BYTES(rtr2rts_qp_out);
9761a86b377SEli Cohen 		*in = kzalloc(*inlen, GFP_KERNEL);
9771a86b377SEli Cohen 		*out = kzalloc(MLX5_ST_SZ_BYTES(rtr2rts_qp_out), GFP_KERNEL);
978f31231bfSColin Ian King 		if (!*in || !*out)
9791a86b377SEli Cohen 			goto outerr;
9801a86b377SEli Cohen 
9811a86b377SEli Cohen 		MLX5_SET(rtr2rts_qp_in, *in, opcode, cmd);
9821a86b377SEli Cohen 		MLX5_SET(rtr2rts_qp_in, *in, uid, ndev->mvdev.res.uid);
9831a86b377SEli Cohen 		MLX5_SET(rtr2rts_qp_in, *in, qpn, qpn);
9841a86b377SEli Cohen 		qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc);
9851a86b377SEli Cohen 		pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
9861a86b377SEli Cohen 		MLX5_SET(ads, pp, ack_timeout, 14);
9871a86b377SEli Cohen 		MLX5_SET(qpc, qpc, retry_count, 7);
9881a86b377SEli Cohen 		MLX5_SET(qpc, qpc, rnr_retry, 7);
9891a86b377SEli Cohen 		break;
9901a86b377SEli Cohen 	default:
991f31231bfSColin Ian King 		goto outerr_nullify;
9921a86b377SEli Cohen 	}
9931a86b377SEli Cohen 
9941a86b377SEli Cohen 	return;
9951a86b377SEli Cohen 
9961a86b377SEli Cohen outerr:
9971a86b377SEli Cohen 	kfree(*in);
9981a86b377SEli Cohen 	kfree(*out);
999f31231bfSColin Ian King outerr_nullify:
10001a86b377SEli Cohen 	*in = NULL;
10011a86b377SEli Cohen 	*out = NULL;
10021a86b377SEli Cohen }
10031a86b377SEli Cohen 
10041a86b377SEli Cohen static void free_inout(void *in, void *out)
10051a86b377SEli Cohen {
10061a86b377SEli Cohen 	kfree(in);
10071a86b377SEli Cohen 	kfree(out);
10081a86b377SEli Cohen }
10091a86b377SEli Cohen 
10101a86b377SEli Cohen /* Two QPs are used by each virtqueue. One is used by the driver and one by
10111a86b377SEli Cohen  * firmware. The fw argument indicates whether the subjected QP is the one used
10121a86b377SEli Cohen  * by firmware.
10131a86b377SEli Cohen  */
10141a86b377SEli Cohen static int modify_qp(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, bool fw, int cmd)
10151a86b377SEli Cohen {
10161a86b377SEli Cohen 	int outlen;
10171a86b377SEli Cohen 	int inlen;
10181a86b377SEli Cohen 	void *out;
10191a86b377SEli Cohen 	void *in;
10201a86b377SEli Cohen 	int err;
10211a86b377SEli Cohen 
10221a86b377SEli Cohen 	alloc_inout(ndev, cmd, &in, &inlen, &out, &outlen, get_qpn(mvq, fw), get_rqpn(mvq, fw));
10231a86b377SEli Cohen 	if (!in || !out)
10241a86b377SEli Cohen 		return -ENOMEM;
10251a86b377SEli Cohen 
10261a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, outlen);
10271a86b377SEli Cohen 	free_inout(in, out);
10281a86b377SEli Cohen 	return err;
10291a86b377SEli Cohen }
10301a86b377SEli Cohen 
10311a86b377SEli Cohen static int connect_qps(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
10321a86b377SEli Cohen {
10331a86b377SEli Cohen 	int err;
10341a86b377SEli Cohen 
10351a86b377SEli Cohen 	err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_2RST_QP);
10361a86b377SEli Cohen 	if (err)
10371a86b377SEli Cohen 		return err;
10381a86b377SEli Cohen 
10391a86b377SEli Cohen 	err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_2RST_QP);
10401a86b377SEli Cohen 	if (err)
10411a86b377SEli Cohen 		return err;
10421a86b377SEli Cohen 
10431a86b377SEli Cohen 	err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_RST2INIT_QP);
10441a86b377SEli Cohen 	if (err)
10451a86b377SEli Cohen 		return err;
10461a86b377SEli Cohen 
10471a86b377SEli Cohen 	err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_RST2INIT_QP);
10481a86b377SEli Cohen 	if (err)
10491a86b377SEli Cohen 		return err;
10501a86b377SEli Cohen 
10511a86b377SEli Cohen 	err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_INIT2RTR_QP);
10521a86b377SEli Cohen 	if (err)
10531a86b377SEli Cohen 		return err;
10541a86b377SEli Cohen 
10551a86b377SEli Cohen 	err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_INIT2RTR_QP);
10561a86b377SEli Cohen 	if (err)
10571a86b377SEli Cohen 		return err;
10581a86b377SEli Cohen 
10591a86b377SEli Cohen 	return modify_qp(ndev, mvq, true, MLX5_CMD_OP_RTR2RTS_QP);
10601a86b377SEli Cohen }
10611a86b377SEli Cohen 
10621a86b377SEli Cohen struct mlx5_virtq_attr {
10631a86b377SEli Cohen 	u8 state;
10641a86b377SEli Cohen 	u16 available_index;
1065b35ccebeSEli Cohen 	u16 used_index;
10661a86b377SEli Cohen };
10671a86b377SEli Cohen 
10681a86b377SEli Cohen static int query_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq,
10691a86b377SEli Cohen 			   struct mlx5_virtq_attr *attr)
10701a86b377SEli Cohen {
10711a86b377SEli Cohen 	int outlen = MLX5_ST_SZ_BYTES(query_virtio_net_q_out);
10721a86b377SEli Cohen 	u32 in[MLX5_ST_SZ_DW(query_virtio_net_q_in)] = {};
10731a86b377SEli Cohen 	void *out;
10741a86b377SEli Cohen 	void *obj_context;
10751a86b377SEli Cohen 	void *cmd_hdr;
10761a86b377SEli Cohen 	int err;
10771a86b377SEli Cohen 
10781a86b377SEli Cohen 	out = kzalloc(outlen, GFP_KERNEL);
10791a86b377SEli Cohen 	if (!out)
10801a86b377SEli Cohen 		return -ENOMEM;
10811a86b377SEli Cohen 
10821a86b377SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(query_virtio_net_q_in, in, general_obj_in_cmd_hdr);
10831a86b377SEli Cohen 
10841a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_QUERY_GENERAL_OBJECT);
10851a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q);
10861a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->virtq_id);
10871a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
10881a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, outlen);
10891a86b377SEli Cohen 	if (err)
10901a86b377SEli Cohen 		goto err_cmd;
10911a86b377SEli Cohen 
10921a86b377SEli Cohen 	obj_context = MLX5_ADDR_OF(query_virtio_net_q_out, out, obj_context);
10931a86b377SEli Cohen 	memset(attr, 0, sizeof(*attr));
10941a86b377SEli Cohen 	attr->state = MLX5_GET(virtio_net_q_object, obj_context, state);
10951a86b377SEli Cohen 	attr->available_index = MLX5_GET(virtio_net_q_object, obj_context, hw_available_index);
1096b35ccebeSEli Cohen 	attr->used_index = MLX5_GET(virtio_net_q_object, obj_context, hw_used_index);
10971a86b377SEli Cohen 	kfree(out);
10981a86b377SEli Cohen 	return 0;
10991a86b377SEli Cohen 
11001a86b377SEli Cohen err_cmd:
11011a86b377SEli Cohen 	kfree(out);
11021a86b377SEli Cohen 	return err;
11031a86b377SEli Cohen }
11041a86b377SEli Cohen 
11051a86b377SEli Cohen static int modify_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int state)
11061a86b377SEli Cohen {
11071a86b377SEli Cohen 	int inlen = MLX5_ST_SZ_BYTES(modify_virtio_net_q_in);
11081a86b377SEli Cohen 	u32 out[MLX5_ST_SZ_DW(modify_virtio_net_q_out)] = {};
11091a86b377SEli Cohen 	void *obj_context;
11101a86b377SEli Cohen 	void *cmd_hdr;
11111a86b377SEli Cohen 	void *in;
11121a86b377SEli Cohen 	int err;
11131a86b377SEli Cohen 
11141a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
11151a86b377SEli Cohen 	if (!in)
11161a86b377SEli Cohen 		return -ENOMEM;
11171a86b377SEli Cohen 
11181a86b377SEli Cohen 	cmd_hdr = MLX5_ADDR_OF(modify_virtio_net_q_in, in, general_obj_in_cmd_hdr);
11191a86b377SEli Cohen 
11201a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_MODIFY_GENERAL_OBJECT);
11211a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q);
11221a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->virtq_id);
11231a86b377SEli Cohen 	MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid);
11241a86b377SEli Cohen 
11251a86b377SEli Cohen 	obj_context = MLX5_ADDR_OF(modify_virtio_net_q_in, in, obj_context);
11261a86b377SEli Cohen 	MLX5_SET64(virtio_net_q_object, obj_context, modify_field_select,
11271a86b377SEli Cohen 		   MLX5_VIRTQ_MODIFY_MASK_STATE);
11281a86b377SEli Cohen 	MLX5_SET(virtio_net_q_object, obj_context, state, state);
11291a86b377SEli Cohen 	err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out));
11301a86b377SEli Cohen 	kfree(in);
11311a86b377SEli Cohen 	if (!err)
11321a86b377SEli Cohen 		mvq->fw_state = state;
11331a86b377SEli Cohen 
11341a86b377SEli Cohen 	return err;
11351a86b377SEli Cohen }
11361a86b377SEli Cohen 
11371a86b377SEli Cohen static int setup_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
11381a86b377SEli Cohen {
11391a86b377SEli Cohen 	u16 idx = mvq->index;
11401a86b377SEli Cohen 	int err;
11411a86b377SEli Cohen 
11421a86b377SEli Cohen 	if (!mvq->num_ent)
11431a86b377SEli Cohen 		return 0;
11441a86b377SEli Cohen 
114552893733SEli Cohen 	if (mvq->initialized)
114652893733SEli Cohen 		return 0;
11471a86b377SEli Cohen 
11481a86b377SEli Cohen 	err = cq_create(ndev, idx, mvq->num_ent);
11491a86b377SEli Cohen 	if (err)
11501a86b377SEli Cohen 		return err;
11511a86b377SEli Cohen 
11521a86b377SEli Cohen 	err = qp_create(ndev, mvq, &mvq->fwqp);
11531a86b377SEli Cohen 	if (err)
11541a86b377SEli Cohen 		goto err_fwqp;
11551a86b377SEli Cohen 
11561a86b377SEli Cohen 	err = qp_create(ndev, mvq, &mvq->vqqp);
11571a86b377SEli Cohen 	if (err)
11581a86b377SEli Cohen 		goto err_vqqp;
11591a86b377SEli Cohen 
11601a86b377SEli Cohen 	err = connect_qps(ndev, mvq);
11611a86b377SEli Cohen 	if (err)
11621a86b377SEli Cohen 		goto err_connect;
11631a86b377SEli Cohen 
11641a86b377SEli Cohen 	err = create_virtqueue(ndev, mvq);
11651a86b377SEli Cohen 	if (err)
11661a86b377SEli Cohen 		goto err_connect;
11671a86b377SEli Cohen 
11681a86b377SEli Cohen 	if (mvq->ready) {
11691a86b377SEli Cohen 		err = modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY);
11701a86b377SEli Cohen 		if (err) {
11711a86b377SEli Cohen 			mlx5_vdpa_warn(&ndev->mvdev, "failed to modify to ready vq idx %d(%d)\n",
11721a86b377SEli Cohen 				       idx, err);
11731a86b377SEli Cohen 			goto err_connect;
11741a86b377SEli Cohen 		}
11751a86b377SEli Cohen 	}
11761a86b377SEli Cohen 
11771a86b377SEli Cohen 	mvq->initialized = true;
11781a86b377SEli Cohen 	return 0;
11791a86b377SEli Cohen 
11801a86b377SEli Cohen err_connect:
11811a86b377SEli Cohen 	qp_destroy(ndev, &mvq->vqqp);
11821a86b377SEli Cohen err_vqqp:
11831a86b377SEli Cohen 	qp_destroy(ndev, &mvq->fwqp);
11841a86b377SEli Cohen err_fwqp:
11851a86b377SEli Cohen 	cq_destroy(ndev, idx);
11861a86b377SEli Cohen 	return err;
11871a86b377SEli Cohen }
11881a86b377SEli Cohen 
11891a86b377SEli Cohen static void suspend_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
11901a86b377SEli Cohen {
11911a86b377SEli Cohen 	struct mlx5_virtq_attr attr;
11921a86b377SEli Cohen 
11931a86b377SEli Cohen 	if (!mvq->initialized)
11941a86b377SEli Cohen 		return;
11951a86b377SEli Cohen 
11961a86b377SEli Cohen 	if (mvq->fw_state != MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY)
11971a86b377SEli Cohen 		return;
11981a86b377SEli Cohen 
11991a86b377SEli Cohen 	if (modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_SUSPEND))
12001a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "modify to suspend failed\n");
12013176e974SSi-Wei Liu 
12023176e974SSi-Wei Liu 	if (query_virtqueue(ndev, mvq, &attr)) {
12033176e974SSi-Wei Liu 		mlx5_vdpa_warn(&ndev->mvdev, "failed to query virtqueue\n");
12043176e974SSi-Wei Liu 		return;
12053176e974SSi-Wei Liu 	}
12063176e974SSi-Wei Liu 	mvq->avail_idx = attr.available_index;
1207bc04d93eSEli Cohen 	mvq->used_idx = attr.used_index;
12081a86b377SEli Cohen }
12091a86b377SEli Cohen 
12101a86b377SEli Cohen static void suspend_vqs(struct mlx5_vdpa_net *ndev)
12111a86b377SEli Cohen {
12121a86b377SEli Cohen 	int i;
12131a86b377SEli Cohen 
1214*75560522SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++)
12151a86b377SEli Cohen 		suspend_vq(ndev, &ndev->vqs[i]);
12161a86b377SEli Cohen }
12171a86b377SEli Cohen 
12181a86b377SEli Cohen static void teardown_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
12191a86b377SEli Cohen {
12201a86b377SEli Cohen 	if (!mvq->initialized)
12211a86b377SEli Cohen 		return;
12221a86b377SEli Cohen 
12231a86b377SEli Cohen 	suspend_vq(ndev, mvq);
12241a86b377SEli Cohen 	destroy_virtqueue(ndev, mvq);
12251a86b377SEli Cohen 	qp_destroy(ndev, &mvq->vqqp);
12261a86b377SEli Cohen 	qp_destroy(ndev, &mvq->fwqp);
12271a86b377SEli Cohen 	cq_destroy(ndev, mvq->index);
12281a86b377SEli Cohen 	mvq->initialized = false;
12291a86b377SEli Cohen }
12301a86b377SEli Cohen 
12311a86b377SEli Cohen static int create_rqt(struct mlx5_vdpa_net *ndev)
12321a86b377SEli Cohen {
12331a86b377SEli Cohen 	__be32 *list;
123452893733SEli Cohen 	int max_rqt;
12351a86b377SEli Cohen 	void *rqtc;
12361a86b377SEli Cohen 	int inlen;
12371a86b377SEli Cohen 	void *in;
12381a86b377SEli Cohen 	int i, j;
12391a86b377SEli Cohen 	int err;
1240*75560522SEli Cohen 	int num;
12411a86b377SEli Cohen 
1242*75560522SEli Cohen 	if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_MQ)))
1243*75560522SEli Cohen 		num = 1;
1244*75560522SEli Cohen 	else
1245*75560522SEli Cohen 		num = mlx5vdpa16_to_cpu(&ndev->mvdev,
1246*75560522SEli Cohen 					ndev->config.max_virtqueue_pairs);
1247*75560522SEli Cohen 
1248*75560522SEli Cohen 	max_rqt = min_t(int, roundup_pow_of_two(num),
124952893733SEli Cohen 			1 << MLX5_CAP_GEN(ndev->mvdev.mdev, log_max_rqt_size));
125052893733SEli Cohen 	if (max_rqt < 1)
12511a86b377SEli Cohen 		return -EOPNOTSUPP;
12521a86b377SEli Cohen 
125352893733SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + max_rqt * MLX5_ST_SZ_BYTES(rq_num);
12541a86b377SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
12551a86b377SEli Cohen 	if (!in)
12561a86b377SEli Cohen 		return -ENOMEM;
12571a86b377SEli Cohen 
12581a86b377SEli Cohen 	MLX5_SET(create_rqt_in, in, uid, ndev->mvdev.res.uid);
12591a86b377SEli Cohen 	rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
12601a86b377SEli Cohen 
12611a86b377SEli Cohen 	MLX5_SET(rqtc, rqtc, list_q_type, MLX5_RQTC_LIST_Q_TYPE_VIRTIO_NET_Q);
126252893733SEli Cohen 	MLX5_SET(rqtc, rqtc, rqt_max_size, max_rqt);
12631a86b377SEli Cohen 	list = MLX5_ADDR_OF(rqtc, rqtc, rq_num[0]);
1264a7f46ba4SEli Cohen 	for (i = 0, j = 0; i < max_rqt; i++, j += 2)
1265*75560522SEli Cohen 		list[i] = cpu_to_be32(ndev->vqs[j % (2 * num)].virtq_id);
12661a86b377SEli Cohen 
1267a7f46ba4SEli Cohen 	MLX5_SET(rqtc, rqtc, rqt_actual_size, max_rqt);
12681a86b377SEli Cohen 	err = mlx5_vdpa_create_rqt(&ndev->mvdev, in, inlen, &ndev->res.rqtn);
12691a86b377SEli Cohen 	kfree(in);
12701a86b377SEli Cohen 	if (err)
12711a86b377SEli Cohen 		return err;
12721a86b377SEli Cohen 
12731a86b377SEli Cohen 	return 0;
12741a86b377SEli Cohen }
12751a86b377SEli Cohen 
127652893733SEli Cohen #define MLX5_MODIFY_RQT_NUM_RQS ((u64)1)
127752893733SEli Cohen 
127852893733SEli Cohen static int modify_rqt(struct mlx5_vdpa_net *ndev, int num)
127952893733SEli Cohen {
128052893733SEli Cohen 	__be32 *list;
128152893733SEli Cohen 	int max_rqt;
128252893733SEli Cohen 	void *rqtc;
128352893733SEli Cohen 	int inlen;
128452893733SEli Cohen 	void *in;
128552893733SEli Cohen 	int i, j;
128652893733SEli Cohen 	int err;
128752893733SEli Cohen 
1288a7f46ba4SEli Cohen 	max_rqt = min_t(int, roundup_pow_of_two(ndev->cur_num_vqs / 2),
128952893733SEli Cohen 			1 << MLX5_CAP_GEN(ndev->mvdev.mdev, log_max_rqt_size));
129052893733SEli Cohen 	if (max_rqt < 1)
129152893733SEli Cohen 		return -EOPNOTSUPP;
129252893733SEli Cohen 
129352893733SEli Cohen 	inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + max_rqt * MLX5_ST_SZ_BYTES(rq_num);
129452893733SEli Cohen 	in = kzalloc(inlen, GFP_KERNEL);
129552893733SEli Cohen 	if (!in)
129652893733SEli Cohen 		return -ENOMEM;
129752893733SEli Cohen 
129852893733SEli Cohen 	MLX5_SET(modify_rqt_in, in, uid, ndev->mvdev.res.uid);
129952893733SEli Cohen 	MLX5_SET64(modify_rqt_in, in, bitmask, MLX5_MODIFY_RQT_NUM_RQS);
130052893733SEli Cohen 	rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
130152893733SEli Cohen 	MLX5_SET(rqtc, rqtc, list_q_type, MLX5_RQTC_LIST_Q_TYPE_VIRTIO_NET_Q);
130252893733SEli Cohen 
130352893733SEli Cohen 	list = MLX5_ADDR_OF(rqtc, rqtc, rq_num[0]);
1304a7f46ba4SEli Cohen 	for (i = 0, j = 0; i < max_rqt; i++, j += 2)
1305a7f46ba4SEli Cohen 		list[i] = cpu_to_be32(ndev->vqs[j % num].virtq_id);
130652893733SEli Cohen 
1307a7f46ba4SEli Cohen 	MLX5_SET(rqtc, rqtc, rqt_actual_size, max_rqt);
130852893733SEli Cohen 	err = mlx5_vdpa_modify_rqt(&ndev->mvdev, in, inlen, ndev->res.rqtn);
130952893733SEli Cohen 	kfree(in);
131052893733SEli Cohen 	if (err)
131152893733SEli Cohen 		return err;
131252893733SEli Cohen 
131352893733SEli Cohen 	return 0;
131452893733SEli Cohen }
131552893733SEli Cohen 
13161a86b377SEli Cohen static void destroy_rqt(struct mlx5_vdpa_net *ndev)
13171a86b377SEli Cohen {
13181a86b377SEli Cohen 	mlx5_vdpa_destroy_rqt(&ndev->mvdev, ndev->res.rqtn);
13191a86b377SEli Cohen }
13201a86b377SEli Cohen 
13211a86b377SEli Cohen static int create_tir(struct mlx5_vdpa_net *ndev)
13221a86b377SEli Cohen {
13231a86b377SEli Cohen #define HASH_IP_L4PORTS                                                                            \
13241a86b377SEli Cohen 	(MLX5_HASH_FIELD_SEL_SRC_IP | MLX5_HASH_FIELD_SEL_DST_IP | MLX5_HASH_FIELD_SEL_L4_SPORT |  \
13251a86b377SEli Cohen 	 MLX5_HASH_FIELD_SEL_L4_DPORT)
13261a86b377SEli Cohen 	static const u8 rx_hash_toeplitz_key[] = { 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
13271a86b377SEli Cohen 						   0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
13281a86b377SEli Cohen 						   0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
13291a86b377SEli Cohen 						   0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
13301a86b377SEli Cohen 						   0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a };
13311a86b377SEli Cohen 	void *rss_key;
13321a86b377SEli Cohen 	void *outer;
13331a86b377SEli Cohen 	void *tirc;
13341a86b377SEli Cohen 	void *in;
13351a86b377SEli Cohen 	int err;
13361a86b377SEli Cohen 
13371a86b377SEli Cohen 	in = kzalloc(MLX5_ST_SZ_BYTES(create_tir_in), GFP_KERNEL);
13381a86b377SEli Cohen 	if (!in)
13391a86b377SEli Cohen 		return -ENOMEM;
13401a86b377SEli Cohen 
13411a86b377SEli Cohen 	MLX5_SET(create_tir_in, in, uid, ndev->mvdev.res.uid);
13421a86b377SEli Cohen 	tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
13431a86b377SEli Cohen 	MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
13441a86b377SEli Cohen 
13451a86b377SEli Cohen 	MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
13461a86b377SEli Cohen 	MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
13471a86b377SEli Cohen 	rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
13481a86b377SEli Cohen 	memcpy(rss_key, rx_hash_toeplitz_key, sizeof(rx_hash_toeplitz_key));
13491a86b377SEli Cohen 
13501a86b377SEli Cohen 	outer = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
13511a86b377SEli Cohen 	MLX5_SET(rx_hash_field_select, outer, l3_prot_type, MLX5_L3_PROT_TYPE_IPV4);
13521a86b377SEli Cohen 	MLX5_SET(rx_hash_field_select, outer, l4_prot_type, MLX5_L4_PROT_TYPE_TCP);
13531a86b377SEli Cohen 	MLX5_SET(rx_hash_field_select, outer, selected_fields, HASH_IP_L4PORTS);
13541a86b377SEli Cohen 
13551a86b377SEli Cohen 	MLX5_SET(tirc, tirc, indirect_table, ndev->res.rqtn);
13561a86b377SEli Cohen 	MLX5_SET(tirc, tirc, transport_domain, ndev->res.tdn);
13571a86b377SEli Cohen 
13581a86b377SEli Cohen 	err = mlx5_vdpa_create_tir(&ndev->mvdev, in, &ndev->res.tirn);
13591a86b377SEli Cohen 	kfree(in);
13601a86b377SEli Cohen 	return err;
13611a86b377SEli Cohen }
13621a86b377SEli Cohen 
13631a86b377SEli Cohen static void destroy_tir(struct mlx5_vdpa_net *ndev)
13641a86b377SEli Cohen {
13651a86b377SEli Cohen 	mlx5_vdpa_destroy_tir(&ndev->mvdev, ndev->res.tirn);
13661a86b377SEli Cohen }
13671a86b377SEli Cohen 
13681a86b377SEli Cohen static int add_fwd_to_tir(struct mlx5_vdpa_net *ndev)
13691a86b377SEli Cohen {
13701a86b377SEli Cohen 	struct mlx5_flow_destination dest[2] = {};
13711a86b377SEli Cohen 	struct mlx5_flow_table_attr ft_attr = {};
13721a86b377SEli Cohen 	struct mlx5_flow_act flow_act = {};
13731a86b377SEli Cohen 	struct mlx5_flow_namespace *ns;
1374540061acSEli Cohen 	struct mlx5_flow_spec *spec;
1375540061acSEli Cohen 	void *headers_c;
1376540061acSEli Cohen 	void *headers_v;
1377540061acSEli Cohen 	u8 *dmac_c;
1378540061acSEli Cohen 	u8 *dmac_v;
13791a86b377SEli Cohen 	int err;
13801a86b377SEli Cohen 
1381540061acSEli Cohen 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1382540061acSEli Cohen 	if (!spec)
1383540061acSEli Cohen 		return -ENOMEM;
1384540061acSEli Cohen 
1385540061acSEli Cohen 	spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1386540061acSEli Cohen 	ft_attr.max_fte = 2;
1387540061acSEli Cohen 	ft_attr.autogroup.max_num_groups = 2;
13881a86b377SEli Cohen 
13891a86b377SEli Cohen 	ns = mlx5_get_flow_namespace(ndev->mvdev.mdev, MLX5_FLOW_NAMESPACE_BYPASS);
13901a86b377SEli Cohen 	if (!ns) {
1391540061acSEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "failed to get flow namespace\n");
1392540061acSEli Cohen 		err = -EOPNOTSUPP;
1393540061acSEli Cohen 		goto err_ns;
13941a86b377SEli Cohen 	}
13951a86b377SEli Cohen 
13961a86b377SEli Cohen 	ndev->rxft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
1397540061acSEli Cohen 	if (IS_ERR(ndev->rxft)) {
1398540061acSEli Cohen 		err = PTR_ERR(ndev->rxft);
1399540061acSEli Cohen 		goto err_ns;
1400540061acSEli Cohen 	}
14011a86b377SEli Cohen 
14021a86b377SEli Cohen 	ndev->rx_counter = mlx5_fc_create(ndev->mvdev.mdev, false);
14031a86b377SEli Cohen 	if (IS_ERR(ndev->rx_counter)) {
14041a86b377SEli Cohen 		err = PTR_ERR(ndev->rx_counter);
14051a86b377SEli Cohen 		goto err_fc;
14061a86b377SEli Cohen 	}
14071a86b377SEli Cohen 
1408540061acSEli Cohen 	headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, outer_headers);
1409540061acSEli Cohen 	dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c, outer_headers.dmac_47_16);
1410540061acSEli Cohen 	memset(dmac_c, 0xff, ETH_ALEN);
1411540061acSEli Cohen 	headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
1412540061acSEli Cohen 	dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v, outer_headers.dmac_47_16);
1413540061acSEli Cohen 	ether_addr_copy(dmac_v, ndev->config.mac);
1414540061acSEli Cohen 
14151a86b377SEli Cohen 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST | MLX5_FLOW_CONTEXT_ACTION_COUNT;
14161a86b377SEli Cohen 	dest[0].type = MLX5_FLOW_DESTINATION_TYPE_TIR;
14171a86b377SEli Cohen 	dest[0].tir_num = ndev->res.tirn;
14181a86b377SEli Cohen 	dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
14191a86b377SEli Cohen 	dest[1].counter_id = mlx5_fc_id(ndev->rx_counter);
1420540061acSEli Cohen 	ndev->rx_rule_ucast = mlx5_add_flow_rules(ndev->rxft, spec, &flow_act, dest, 2);
1421540061acSEli Cohen 
1422540061acSEli Cohen 	if (IS_ERR(ndev->rx_rule_ucast)) {
1423540061acSEli Cohen 		err = PTR_ERR(ndev->rx_rule_ucast);
1424540061acSEli Cohen 		ndev->rx_rule_ucast = NULL;
1425540061acSEli Cohen 		goto err_rule_ucast;
14261a86b377SEli Cohen 	}
14271a86b377SEli Cohen 
1428540061acSEli Cohen 	memset(dmac_c, 0, ETH_ALEN);
1429540061acSEli Cohen 	memset(dmac_v, 0, ETH_ALEN);
1430540061acSEli Cohen 	dmac_c[0] = 1;
1431540061acSEli Cohen 	dmac_v[0] = 1;
1432540061acSEli Cohen 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1433540061acSEli Cohen 	ndev->rx_rule_mcast = mlx5_add_flow_rules(ndev->rxft, spec, &flow_act, dest, 1);
1434540061acSEli Cohen 	if (IS_ERR(ndev->rx_rule_mcast)) {
1435540061acSEli Cohen 		err = PTR_ERR(ndev->rx_rule_mcast);
1436540061acSEli Cohen 		ndev->rx_rule_mcast = NULL;
1437540061acSEli Cohen 		goto err_rule_mcast;
1438540061acSEli Cohen 	}
1439540061acSEli Cohen 
1440540061acSEli Cohen 	kvfree(spec);
14411a86b377SEli Cohen 	return 0;
14421a86b377SEli Cohen 
1443540061acSEli Cohen err_rule_mcast:
1444540061acSEli Cohen 	mlx5_del_flow_rules(ndev->rx_rule_ucast);
1445540061acSEli Cohen 	ndev->rx_rule_ucast = NULL;
1446540061acSEli Cohen err_rule_ucast:
14471a86b377SEli Cohen 	mlx5_fc_destroy(ndev->mvdev.mdev, ndev->rx_counter);
14481a86b377SEli Cohen err_fc:
14491a86b377SEli Cohen 	mlx5_destroy_flow_table(ndev->rxft);
1450540061acSEli Cohen err_ns:
1451540061acSEli Cohen 	kvfree(spec);
14521a86b377SEli Cohen 	return err;
14531a86b377SEli Cohen }
14541a86b377SEli Cohen 
14551a86b377SEli Cohen static void remove_fwd_to_tir(struct mlx5_vdpa_net *ndev)
14561a86b377SEli Cohen {
1457540061acSEli Cohen 	if (!ndev->rx_rule_ucast)
14581a86b377SEli Cohen 		return;
14591a86b377SEli Cohen 
1460540061acSEli Cohen 	mlx5_del_flow_rules(ndev->rx_rule_mcast);
1461540061acSEli Cohen 	ndev->rx_rule_mcast = NULL;
1462540061acSEli Cohen 	mlx5_del_flow_rules(ndev->rx_rule_ucast);
1463540061acSEli Cohen 	ndev->rx_rule_ucast = NULL;
14641a86b377SEli Cohen 	mlx5_fc_destroy(ndev->mvdev.mdev, ndev->rx_counter);
14651a86b377SEli Cohen 	mlx5_destroy_flow_table(ndev->rxft);
14661a86b377SEli Cohen }
14671a86b377SEli Cohen 
14685262912eSEli Cohen static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
14695262912eSEli Cohen {
14705262912eSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
14715262912eSEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
14725262912eSEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
14735262912eSEli Cohen 	struct mlx5_core_dev *pfmdev;
14745262912eSEli Cohen 	size_t read;
14755262912eSEli Cohen 	u8 mac[ETH_ALEN];
14765262912eSEli Cohen 
14775262912eSEli Cohen 	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
14785262912eSEli Cohen 	switch (cmd) {
14795262912eSEli Cohen 	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
14805262912eSEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)mac, ETH_ALEN);
14815262912eSEli Cohen 		if (read != ETH_ALEN)
14825262912eSEli Cohen 			break;
14835262912eSEli Cohen 
14845262912eSEli Cohen 		if (!memcmp(ndev->config.mac, mac, 6)) {
14855262912eSEli Cohen 			status = VIRTIO_NET_OK;
14865262912eSEli Cohen 			break;
14875262912eSEli Cohen 		}
14885262912eSEli Cohen 
14895262912eSEli Cohen 		if (!is_zero_ether_addr(ndev->config.mac)) {
14905262912eSEli Cohen 			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
14915262912eSEli Cohen 				mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
14925262912eSEli Cohen 					       ndev->config.mac);
14935262912eSEli Cohen 				break;
14945262912eSEli Cohen 			}
14955262912eSEli Cohen 		}
14965262912eSEli Cohen 
14975262912eSEli Cohen 		if (mlx5_mpfs_add_mac(pfmdev, mac)) {
14985262912eSEli Cohen 			mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
14995262912eSEli Cohen 				       mac);
15005262912eSEli Cohen 			break;
15015262912eSEli Cohen 		}
15025262912eSEli Cohen 
15035262912eSEli Cohen 		memcpy(ndev->config.mac, mac, ETH_ALEN);
15045262912eSEli Cohen 		status = VIRTIO_NET_OK;
15055262912eSEli Cohen 		break;
15065262912eSEli Cohen 
15075262912eSEli Cohen 	default:
15085262912eSEli Cohen 		break;
15095262912eSEli Cohen 	}
15105262912eSEli Cohen 
15115262912eSEli Cohen 	return status;
15125262912eSEli Cohen }
15135262912eSEli Cohen 
151452893733SEli Cohen static int change_num_qps(struct mlx5_vdpa_dev *mvdev, int newqps)
151552893733SEli Cohen {
151652893733SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
151752893733SEli Cohen 	int cur_qps = ndev->cur_num_vqs / 2;
151852893733SEli Cohen 	int err;
151952893733SEli Cohen 	int i;
152052893733SEli Cohen 
152152893733SEli Cohen 	if (cur_qps > newqps) {
152252893733SEli Cohen 		err = modify_rqt(ndev, 2 * newqps);
152352893733SEli Cohen 		if (err)
152452893733SEli Cohen 			return err;
152552893733SEli Cohen 
152652893733SEli Cohen 		for (i = ndev->cur_num_vqs - 1; i >= 2 * newqps; i--)
152752893733SEli Cohen 			teardown_vq(ndev, &ndev->vqs[i]);
152852893733SEli Cohen 
152952893733SEli Cohen 		ndev->cur_num_vqs = 2 * newqps;
153052893733SEli Cohen 	} else {
153152893733SEli Cohen 		ndev->cur_num_vqs = 2 * newqps;
153252893733SEli Cohen 		for (i = cur_qps * 2; i < 2 * newqps; i++) {
153352893733SEli Cohen 			err = setup_vq(ndev, &ndev->vqs[i]);
153452893733SEli Cohen 			if (err)
153552893733SEli Cohen 				goto clean_added;
153652893733SEli Cohen 		}
153752893733SEli Cohen 		err = modify_rqt(ndev, 2 * newqps);
153852893733SEli Cohen 		if (err)
153952893733SEli Cohen 			goto clean_added;
154052893733SEli Cohen 	}
154152893733SEli Cohen 	return 0;
154252893733SEli Cohen 
154352893733SEli Cohen clean_added:
154452893733SEli Cohen 	for (--i; i >= cur_qps; --i)
154552893733SEli Cohen 		teardown_vq(ndev, &ndev->vqs[i]);
154652893733SEli Cohen 
154752893733SEli Cohen 	return err;
154852893733SEli Cohen }
154952893733SEli Cohen 
155052893733SEli Cohen static virtio_net_ctrl_ack handle_ctrl_mq(struct mlx5_vdpa_dev *mvdev, u8 cmd)
155152893733SEli Cohen {
155252893733SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
155352893733SEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
155452893733SEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
155552893733SEli Cohen 	struct virtio_net_ctrl_mq mq;
155652893733SEli Cohen 	size_t read;
155752893733SEli Cohen 	u16 newqps;
155852893733SEli Cohen 
155952893733SEli Cohen 	switch (cmd) {
156052893733SEli Cohen 	case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET:
156152893733SEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)&mq, sizeof(mq));
156252893733SEli Cohen 		if (read != sizeof(mq))
156352893733SEli Cohen 			break;
156452893733SEli Cohen 
156552893733SEli Cohen 		newqps = mlx5vdpa16_to_cpu(mvdev, mq.virtqueue_pairs);
156652893733SEli Cohen 		if (ndev->cur_num_vqs == 2 * newqps) {
156752893733SEli Cohen 			status = VIRTIO_NET_OK;
156852893733SEli Cohen 			break;
156952893733SEli Cohen 		}
157052893733SEli Cohen 
157152893733SEli Cohen 		if (!change_num_qps(mvdev, newqps))
157252893733SEli Cohen 			status = VIRTIO_NET_OK;
157352893733SEli Cohen 
157452893733SEli Cohen 		break;
157552893733SEli Cohen 	default:
157652893733SEli Cohen 		break;
157752893733SEli Cohen 	}
157852893733SEli Cohen 
157952893733SEli Cohen 	return status;
158052893733SEli Cohen }
158152893733SEli Cohen 
15825262912eSEli Cohen static void mlx5_cvq_kick_handler(struct work_struct *work)
15835262912eSEli Cohen {
15845262912eSEli Cohen 	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
15855262912eSEli Cohen 	struct virtio_net_ctrl_hdr ctrl;
1586218bdd20SEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
15875262912eSEli Cohen 	struct mlx5_vdpa_dev *mvdev;
15885262912eSEli Cohen 	struct mlx5_control_vq *cvq;
15895262912eSEli Cohen 	struct mlx5_vdpa_net *ndev;
15905262912eSEli Cohen 	size_t read, write;
15915262912eSEli Cohen 	int err;
15925262912eSEli Cohen 
1593218bdd20SEli Cohen 	wqent = container_of(work, struct mlx5_vdpa_wq_ent, work);
15945262912eSEli Cohen 	mvdev = wqent->mvdev;
15955262912eSEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
15965262912eSEli Cohen 	cvq = &mvdev->cvq;
15975262912eSEli Cohen 	if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)))
15985262912eSEli Cohen 		goto out;
15995262912eSEli Cohen 
16005262912eSEli Cohen 	if (!cvq->ready)
16015262912eSEli Cohen 		goto out;
16025262912eSEli Cohen 
16035262912eSEli Cohen 	while (true) {
16045262912eSEli Cohen 		err = vringh_getdesc_iotlb(&cvq->vring, &cvq->riov, &cvq->wiov, &cvq->head,
16055262912eSEli Cohen 					   GFP_ATOMIC);
16065262912eSEli Cohen 		if (err <= 0)
16075262912eSEli Cohen 			break;
16085262912eSEli Cohen 
16095262912eSEli Cohen 		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, &ctrl, sizeof(ctrl));
16105262912eSEli Cohen 		if (read != sizeof(ctrl))
16115262912eSEli Cohen 			break;
16125262912eSEli Cohen 
16135262912eSEli Cohen 		switch (ctrl.class) {
16145262912eSEli Cohen 		case VIRTIO_NET_CTRL_MAC:
16155262912eSEli Cohen 			status = handle_ctrl_mac(mvdev, ctrl.cmd);
16165262912eSEli Cohen 			break;
161752893733SEli Cohen 		case VIRTIO_NET_CTRL_MQ:
161852893733SEli Cohen 			status = handle_ctrl_mq(mvdev, ctrl.cmd);
161952893733SEli Cohen 			break;
16205262912eSEli Cohen 
16215262912eSEli Cohen 		default:
16225262912eSEli Cohen 			break;
16235262912eSEli Cohen 		}
16245262912eSEli Cohen 
16255262912eSEli Cohen 		/* Make sure data is written before advancing index */
16265262912eSEli Cohen 		smp_wmb();
16275262912eSEli Cohen 
16285262912eSEli Cohen 		write = vringh_iov_push_iotlb(&cvq->vring, &cvq->wiov, &status, sizeof(status));
16295262912eSEli Cohen 		vringh_complete_iotlb(&cvq->vring, cvq->head, write);
16305262912eSEli Cohen 		vringh_kiov_cleanup(&cvq->riov);
16315262912eSEli Cohen 		vringh_kiov_cleanup(&cvq->wiov);
16325262912eSEli Cohen 
16335262912eSEli Cohen 		if (vringh_need_notify_iotlb(&cvq->vring))
16345262912eSEli Cohen 			vringh_notify(&cvq->vring);
16355262912eSEli Cohen 	}
16365262912eSEli Cohen out:
16375262912eSEli Cohen 	kfree(wqent);
16385262912eSEli Cohen }
16395262912eSEli Cohen 
16401a86b377SEli Cohen static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx)
16411a86b377SEli Cohen {
16421a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
16431a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
16445262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
1645218bdd20SEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
16461a86b377SEli Cohen 
1647e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
1648e4fc6650SEli Cohen 		return;
1649e4fc6650SEli Cohen 
16505262912eSEli Cohen 	if (unlikely(is_ctrl_vq_idx(mvdev, idx))) {
16515262912eSEli Cohen 		if (!mvdev->cvq.ready)
16525262912eSEli Cohen 			return;
16535262912eSEli Cohen 
16545262912eSEli Cohen 		wqent = kzalloc(sizeof(*wqent), GFP_ATOMIC);
16555262912eSEli Cohen 		if (!wqent)
16565262912eSEli Cohen 			return;
16575262912eSEli Cohen 
16585262912eSEli Cohen 		wqent->mvdev = mvdev;
16595262912eSEli Cohen 		INIT_WORK(&wqent->work, mlx5_cvq_kick_handler);
16605262912eSEli Cohen 		queue_work(mvdev->wq, &wqent->work);
16615262912eSEli Cohen 		return;
16625262912eSEli Cohen 	}
16635262912eSEli Cohen 
16645262912eSEli Cohen 	mvq = &ndev->vqs[idx];
16651a86b377SEli Cohen 	if (unlikely(!mvq->ready))
16661a86b377SEli Cohen 		return;
16671a86b377SEli Cohen 
16681a86b377SEli Cohen 	iowrite16(idx, ndev->mvdev.res.kick_addr);
16691a86b377SEli Cohen }
16701a86b377SEli Cohen 
16711a86b377SEli Cohen static int mlx5_vdpa_set_vq_address(struct vdpa_device *vdev, u16 idx, u64 desc_area,
16721a86b377SEli Cohen 				    u64 driver_area, u64 device_area)
16731a86b377SEli Cohen {
16741a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
16751a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
16765262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
16771a86b377SEli Cohen 
1678e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
1679e4fc6650SEli Cohen 		return -EINVAL;
1680e4fc6650SEli Cohen 
16815262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
16825262912eSEli Cohen 		mvdev->cvq.desc_addr = desc_area;
16835262912eSEli Cohen 		mvdev->cvq.device_addr = device_area;
16845262912eSEli Cohen 		mvdev->cvq.driver_addr = driver_area;
16855262912eSEli Cohen 		return 0;
16865262912eSEli Cohen 	}
16875262912eSEli Cohen 
16885262912eSEli Cohen 	mvq = &ndev->vqs[idx];
16891a86b377SEli Cohen 	mvq->desc_addr = desc_area;
16901a86b377SEli Cohen 	mvq->device_addr = device_area;
16911a86b377SEli Cohen 	mvq->driver_addr = driver_area;
16921a86b377SEli Cohen 	return 0;
16931a86b377SEli Cohen }
16941a86b377SEli Cohen 
16951a86b377SEli Cohen static void mlx5_vdpa_set_vq_num(struct vdpa_device *vdev, u16 idx, u32 num)
16961a86b377SEli Cohen {
16971a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
16981a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
16991a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
17001a86b377SEli Cohen 
17015262912eSEli Cohen 	if (!is_index_valid(mvdev, idx) || is_ctrl_vq_idx(mvdev, idx))
1702e4fc6650SEli Cohen 		return;
1703e4fc6650SEli Cohen 
17041a86b377SEli Cohen 	mvq = &ndev->vqs[idx];
17051a86b377SEli Cohen 	mvq->num_ent = num;
17061a86b377SEli Cohen }
17071a86b377SEli Cohen 
17081a86b377SEli Cohen static void mlx5_vdpa_set_vq_cb(struct vdpa_device *vdev, u16 idx, struct vdpa_callback *cb)
17091a86b377SEli Cohen {
17101a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
17111a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
17121a86b377SEli Cohen 
1713db296d25SEli Cohen 	ndev->event_cbs[idx] = *cb;
17141a86b377SEli Cohen }
17151a86b377SEli Cohen 
17165262912eSEli Cohen static void mlx5_cvq_notify(struct vringh *vring)
17175262912eSEli Cohen {
17185262912eSEli Cohen 	struct mlx5_control_vq *cvq = container_of(vring, struct mlx5_control_vq, vring);
17195262912eSEli Cohen 
17205262912eSEli Cohen 	if (!cvq->event_cb.callback)
17215262912eSEli Cohen 		return;
17225262912eSEli Cohen 
17235262912eSEli Cohen 	cvq->event_cb.callback(cvq->event_cb.private);
17245262912eSEli Cohen }
17255262912eSEli Cohen 
17265262912eSEli Cohen static void set_cvq_ready(struct mlx5_vdpa_dev *mvdev, bool ready)
17275262912eSEli Cohen {
17285262912eSEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
17295262912eSEli Cohen 
17305262912eSEli Cohen 	cvq->ready = ready;
17315262912eSEli Cohen 	if (!ready)
17325262912eSEli Cohen 		return;
17335262912eSEli Cohen 
17345262912eSEli Cohen 	cvq->vring.notify = mlx5_cvq_notify;
17351a86b377SEli Cohen }
17361a86b377SEli Cohen 
17371a86b377SEli Cohen static void mlx5_vdpa_set_vq_ready(struct vdpa_device *vdev, u16 idx, bool ready)
17381a86b377SEli Cohen {
17391a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
17401a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
17415262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
17421a86b377SEli Cohen 
1743759be899SEli Cohen 	if (!mvdev->actual_features)
1744759be899SEli Cohen 		return;
1745759be899SEli Cohen 
1746e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
1747e4fc6650SEli Cohen 		return;
1748e4fc6650SEli Cohen 
17495262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
17505262912eSEli Cohen 		set_cvq_ready(mvdev, ready);
17515262912eSEli Cohen 		return;
17525262912eSEli Cohen 	}
17535262912eSEli Cohen 
17545262912eSEli Cohen 	mvq = &ndev->vqs[idx];
17551a86b377SEli Cohen 	if (!ready)
17561a86b377SEli Cohen 		suspend_vq(ndev, mvq);
17571a86b377SEli Cohen 
17581a86b377SEli Cohen 	mvq->ready = ready;
17591a86b377SEli Cohen }
17601a86b377SEli Cohen 
17611a86b377SEli Cohen static bool mlx5_vdpa_get_vq_ready(struct vdpa_device *vdev, u16 idx)
17621a86b377SEli Cohen {
17631a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
17641a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
17651a86b377SEli Cohen 
1766e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
1767e4fc6650SEli Cohen 		return false;
1768e4fc6650SEli Cohen 
17695262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx))
17705262912eSEli Cohen 		return mvdev->cvq.ready;
17715262912eSEli Cohen 
17725262912eSEli Cohen 	return ndev->vqs[idx].ready;
17731a86b377SEli Cohen }
17741a86b377SEli Cohen 
17751a86b377SEli Cohen static int mlx5_vdpa_set_vq_state(struct vdpa_device *vdev, u16 idx,
17761a86b377SEli Cohen 				  const struct vdpa_vq_state *state)
17771a86b377SEli Cohen {
17781a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
17791a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
17805262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
17811a86b377SEli Cohen 
1782e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
1783e4fc6650SEli Cohen 		return -EINVAL;
1784e4fc6650SEli Cohen 
17855262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
17865262912eSEli Cohen 		mvdev->cvq.vring.last_avail_idx = state->split.avail_index;
17875262912eSEli Cohen 		return 0;
17885262912eSEli Cohen 	}
17895262912eSEli Cohen 
17905262912eSEli Cohen 	mvq = &ndev->vqs[idx];
17911a86b377SEli Cohen 	if (mvq->fw_state == MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY) {
17921a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "can't modify available index\n");
17931a86b377SEli Cohen 		return -EINVAL;
17941a86b377SEli Cohen 	}
17951a86b377SEli Cohen 
1796530a5678SJason Wang 	mvq->used_idx = state->split.avail_index;
1797530a5678SJason Wang 	mvq->avail_idx = state->split.avail_index;
17981a86b377SEli Cohen 	return 0;
17991a86b377SEli Cohen }
18001a86b377SEli Cohen 
18011a86b377SEli Cohen static int mlx5_vdpa_get_vq_state(struct vdpa_device *vdev, u16 idx, struct vdpa_vq_state *state)
18021a86b377SEli Cohen {
18031a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
18041a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
18055262912eSEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
18061a86b377SEli Cohen 	struct mlx5_virtq_attr attr;
18071a86b377SEli Cohen 	int err;
18081a86b377SEli Cohen 
1809e4fc6650SEli Cohen 	if (!is_index_valid(mvdev, idx))
1810e4fc6650SEli Cohen 		return -EINVAL;
1811e4fc6650SEli Cohen 
18125262912eSEli Cohen 	if (is_ctrl_vq_idx(mvdev, idx)) {
18135262912eSEli Cohen 		state->split.avail_index = mvdev->cvq.vring.last_avail_idx;
18145262912eSEli Cohen 		return 0;
18155262912eSEli Cohen 	}
18165262912eSEli Cohen 
18175262912eSEli Cohen 	mvq = &ndev->vqs[idx];
18183176e974SSi-Wei Liu 	/* If the virtq object was destroyed, use the value saved at
18193176e974SSi-Wei Liu 	 * the last minute of suspend_vq. This caters for userspace
18203176e974SSi-Wei Liu 	 * that cares about emulating the index after vq is stopped.
18213176e974SSi-Wei Liu 	 */
18223176e974SSi-Wei Liu 	if (!mvq->initialized) {
1823bc04d93eSEli Cohen 		/* Firmware returns a wrong value for the available index.
1824bc04d93eSEli Cohen 		 * Since both values should be identical, we take the value of
1825bc04d93eSEli Cohen 		 * used_idx which is reported correctly.
1826bc04d93eSEli Cohen 		 */
1827530a5678SJason Wang 		state->split.avail_index = mvq->used_idx;
18283176e974SSi-Wei Liu 		return 0;
18293176e974SSi-Wei Liu 	}
18301a86b377SEli Cohen 
18311a86b377SEli Cohen 	err = query_virtqueue(ndev, mvq, &attr);
18321a86b377SEli Cohen 	if (err) {
18331a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "failed to query virtqueue\n");
18341a86b377SEli Cohen 		return err;
18351a86b377SEli Cohen 	}
1836530a5678SJason Wang 	state->split.avail_index = attr.used_index;
18371a86b377SEli Cohen 	return 0;
18381a86b377SEli Cohen }
18391a86b377SEli Cohen 
18401a86b377SEli Cohen static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev)
18411a86b377SEli Cohen {
18421a86b377SEli Cohen 	return PAGE_SIZE;
18431a86b377SEli Cohen }
18441a86b377SEli Cohen 
18451a86b377SEli Cohen enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9,
18461a86b377SEli Cohen 	MLX5_VIRTIO_NET_F_CSUM = 1 << 10,
18471a86b377SEli Cohen 	MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11,
18481a86b377SEli Cohen 	MLX5_VIRTIO_NET_F_HOST_TSO4 = 1 << 12,
18491a86b377SEli Cohen };
18501a86b377SEli Cohen 
18511a86b377SEli Cohen static u64 mlx_to_vritio_features(u16 dev_features)
18521a86b377SEli Cohen {
18531a86b377SEli Cohen 	u64 result = 0;
18541a86b377SEli Cohen 
18551a86b377SEli Cohen 	if (dev_features & MLX5_VIRTIO_NET_F_GUEST_CSUM)
1856cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_GUEST_CSUM);
18571a86b377SEli Cohen 	if (dev_features & MLX5_VIRTIO_NET_F_CSUM)
1858cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_CSUM);
18591a86b377SEli Cohen 	if (dev_features & MLX5_VIRTIO_NET_F_HOST_TSO6)
1860cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_HOST_TSO6);
18611a86b377SEli Cohen 	if (dev_features & MLX5_VIRTIO_NET_F_HOST_TSO4)
1862cbb52359SNathan Chancellor 		result |= BIT_ULL(VIRTIO_NET_F_HOST_TSO4);
18631a86b377SEli Cohen 
18641a86b377SEli Cohen 	return result;
18651a86b377SEli Cohen }
18661a86b377SEli Cohen 
1867a64917bcSEli Cohen static u64 mlx5_vdpa_get_device_features(struct vdpa_device *vdev)
18681a86b377SEli Cohen {
18691a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
18701a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
18711a86b377SEli Cohen 	u16 dev_features;
18721a86b377SEli Cohen 
18731a86b377SEli Cohen 	dev_features = MLX5_CAP_DEV_VDPA_EMULATION(mvdev->mdev, device_features_bits_mask);
18745262912eSEli Cohen 	ndev->mvdev.mlx_features |= mlx_to_vritio_features(dev_features);
18751a86b377SEli Cohen 	if (MLX5_CAP_DEV_VDPA_EMULATION(mvdev->mdev, virtio_version_1_0))
1876cbb52359SNathan Chancellor 		ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_F_VERSION_1);
1877cbb52359SNathan Chancellor 	ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_F_ACCESS_PLATFORM);
18785262912eSEli Cohen 	ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_CTRL_VQ);
18795262912eSEli Cohen 	ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR);
188052893733SEli Cohen 	ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_MQ);
1881edf747afSEli Cohen 	ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_STATUS);
188260af39c1SEli Cohen 	ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_MTU);
18835262912eSEli Cohen 
18841a86b377SEli Cohen 	print_features(mvdev, ndev->mvdev.mlx_features, false);
18851a86b377SEli Cohen 	return ndev->mvdev.mlx_features;
18861a86b377SEli Cohen }
18871a86b377SEli Cohen 
18881a86b377SEli Cohen static int verify_min_features(struct mlx5_vdpa_dev *mvdev, u64 features)
18891a86b377SEli Cohen {
1890cbb52359SNathan Chancellor 	if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)))
18911a86b377SEli Cohen 		return -EOPNOTSUPP;
18921a86b377SEli Cohen 
18931a86b377SEli Cohen 	return 0;
18941a86b377SEli Cohen }
18951a86b377SEli Cohen 
1896ae0428deSEli Cohen static int setup_virtqueues(struct mlx5_vdpa_dev *mvdev)
18971a86b377SEli Cohen {
1898ae0428deSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
18995262912eSEli Cohen 	struct mlx5_control_vq *cvq = &mvdev->cvq;
19001a86b377SEli Cohen 	int err;
19011a86b377SEli Cohen 	int i;
19021a86b377SEli Cohen 
1903ae0428deSEli Cohen 	for (i = 0; i < 2 * mlx5_vdpa_max_qps(mvdev->max_vqs); i++) {
19041a86b377SEli Cohen 		err = setup_vq(ndev, &ndev->vqs[i]);
19051a86b377SEli Cohen 		if (err)
19061a86b377SEli Cohen 			goto err_vq;
19071a86b377SEli Cohen 	}
19081a86b377SEli Cohen 
19095262912eSEli Cohen 	if (mvdev->actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)) {
19105262912eSEli Cohen 		err = vringh_init_iotlb(&cvq->vring, mvdev->actual_features,
19115262912eSEli Cohen 					MLX5_CVQ_MAX_ENT, false,
19125262912eSEli Cohen 					(struct vring_desc *)(uintptr_t)cvq->desc_addr,
19135262912eSEli Cohen 					(struct vring_avail *)(uintptr_t)cvq->driver_addr,
19145262912eSEli Cohen 					(struct vring_used *)(uintptr_t)cvq->device_addr);
19155262912eSEli Cohen 		if (err)
19165262912eSEli Cohen 			goto err_vq;
19175262912eSEli Cohen 	}
19185262912eSEli Cohen 
19191a86b377SEli Cohen 	return 0;
19201a86b377SEli Cohen 
19211a86b377SEli Cohen err_vq:
19221a86b377SEli Cohen 	for (--i; i >= 0; i--)
19231a86b377SEli Cohen 		teardown_vq(ndev, &ndev->vqs[i]);
19241a86b377SEli Cohen 
19251a86b377SEli Cohen 	return err;
19261a86b377SEli Cohen }
19271a86b377SEli Cohen 
19281a86b377SEli Cohen static void teardown_virtqueues(struct mlx5_vdpa_net *ndev)
19291a86b377SEli Cohen {
19301a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
19311a86b377SEli Cohen 	int i;
19321a86b377SEli Cohen 
19331a86b377SEli Cohen 	for (i = ndev->mvdev.max_vqs - 1; i >= 0; i--) {
19341a86b377SEli Cohen 		mvq = &ndev->vqs[i];
19351a86b377SEli Cohen 		if (!mvq->initialized)
19361a86b377SEli Cohen 			continue;
19371a86b377SEli Cohen 
19381a86b377SEli Cohen 		teardown_vq(ndev, mvq);
19391a86b377SEli Cohen 	}
19401a86b377SEli Cohen }
19411a86b377SEli Cohen 
1942e4fc6650SEli Cohen static void update_cvq_info(struct mlx5_vdpa_dev *mvdev)
19438a7c3213SMichael S. Tsirkin {
1944e4fc6650SEli Cohen 	if (MLX5_FEATURE(mvdev, VIRTIO_NET_F_CTRL_VQ)) {
1945e4fc6650SEli Cohen 		if (MLX5_FEATURE(mvdev, VIRTIO_NET_F_MQ)) {
1946e4fc6650SEli Cohen 			/* MQ supported. CVQ index is right above the last data virtqueue's */
1947e4fc6650SEli Cohen 			mvdev->max_idx = mvdev->max_vqs;
1948e4fc6650SEli Cohen 		} else {
1949e4fc6650SEli Cohen 			/* Only CVQ supportted. data virtqueues occupy indices 0 and 1.
1950e4fc6650SEli Cohen 			 * CVQ gets index 2
1951e4fc6650SEli Cohen 			 */
1952e4fc6650SEli Cohen 			mvdev->max_idx = 2;
19538a7c3213SMichael S. Tsirkin 		}
1954e4fc6650SEli Cohen 	} else {
1955e4fc6650SEli Cohen 		/* Two data virtqueues only: one for rx and one for tx */
1956e4fc6650SEli Cohen 		mvdev->max_idx = 1;
1957e4fc6650SEli Cohen 	}
195836bdcf31SEli Cohen }
195936bdcf31SEli Cohen 
1960a64917bcSEli Cohen static int mlx5_vdpa_set_driver_features(struct vdpa_device *vdev, u64 features)
19611a86b377SEli Cohen {
19621a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
19631a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
19641a86b377SEli Cohen 	int err;
19651a86b377SEli Cohen 
19661a86b377SEli Cohen 	print_features(mvdev, features, true);
19671a86b377SEli Cohen 
19681a86b377SEli Cohen 	err = verify_min_features(mvdev, features);
19691a86b377SEli Cohen 	if (err)
19701a86b377SEli Cohen 		return err;
19711a86b377SEli Cohen 
19721a86b377SEli Cohen 	ndev->mvdev.actual_features = features & ndev->mvdev.mlx_features;
1973e4fc6650SEli Cohen 	update_cvq_info(mvdev);
19741a86b377SEli Cohen 	return err;
19751a86b377SEli Cohen }
19761a86b377SEli Cohen 
19771a86b377SEli Cohen static void mlx5_vdpa_set_config_cb(struct vdpa_device *vdev, struct vdpa_callback *cb)
19781a86b377SEli Cohen {
1979edf747afSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
1980edf747afSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
1981edf747afSEli Cohen 
1982edf747afSEli Cohen 	ndev->config_cb = *cb;
19831a86b377SEli Cohen }
19841a86b377SEli Cohen 
19851a86b377SEli Cohen #define MLX5_VDPA_MAX_VQ_ENTRIES 256
19861a86b377SEli Cohen static u16 mlx5_vdpa_get_vq_num_max(struct vdpa_device *vdev)
19871a86b377SEli Cohen {
19881a86b377SEli Cohen 	return MLX5_VDPA_MAX_VQ_ENTRIES;
19891a86b377SEli Cohen }
19901a86b377SEli Cohen 
19911a86b377SEli Cohen static u32 mlx5_vdpa_get_device_id(struct vdpa_device *vdev)
19921a86b377SEli Cohen {
19931a86b377SEli Cohen 	return VIRTIO_ID_NET;
19941a86b377SEli Cohen }
19951a86b377SEli Cohen 
19961a86b377SEli Cohen static u32 mlx5_vdpa_get_vendor_id(struct vdpa_device *vdev)
19971a86b377SEli Cohen {
19981a86b377SEli Cohen 	return PCI_VENDOR_ID_MELLANOX;
19991a86b377SEli Cohen }
20001a86b377SEli Cohen 
20011a86b377SEli Cohen static u8 mlx5_vdpa_get_status(struct vdpa_device *vdev)
20021a86b377SEli Cohen {
20031a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
20041a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
20051a86b377SEli Cohen 
20061a86b377SEli Cohen 	print_status(mvdev, ndev->mvdev.status, false);
20071a86b377SEli Cohen 	return ndev->mvdev.status;
20081a86b377SEli Cohen }
20091a86b377SEli Cohen 
20101a86b377SEli Cohen static int save_channel_info(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq)
20111a86b377SEli Cohen {
20121a86b377SEli Cohen 	struct mlx5_vq_restore_info *ri = &mvq->ri;
201352893733SEli Cohen 	struct mlx5_virtq_attr attr = {};
20141a86b377SEli Cohen 	int err;
20151a86b377SEli Cohen 
201652893733SEli Cohen 	if (mvq->initialized) {
20171a86b377SEli Cohen 		err = query_virtqueue(ndev, mvq, &attr);
20181a86b377SEli Cohen 		if (err)
20191a86b377SEli Cohen 			return err;
202052893733SEli Cohen 	}
20211a86b377SEli Cohen 
20221a86b377SEli Cohen 	ri->avail_index = attr.available_index;
2023b35ccebeSEli Cohen 	ri->used_index = attr.used_index;
20241a86b377SEli Cohen 	ri->ready = mvq->ready;
20251a86b377SEli Cohen 	ri->num_ent = mvq->num_ent;
20261a86b377SEli Cohen 	ri->desc_addr = mvq->desc_addr;
20271a86b377SEli Cohen 	ri->device_addr = mvq->device_addr;
20281a86b377SEli Cohen 	ri->driver_addr = mvq->driver_addr;
20291a86b377SEli Cohen 	ri->restore = true;
20301a86b377SEli Cohen 	return 0;
20311a86b377SEli Cohen }
20321a86b377SEli Cohen 
20331a86b377SEli Cohen static int save_channels_info(struct mlx5_vdpa_net *ndev)
20341a86b377SEli Cohen {
20351a86b377SEli Cohen 	int i;
20361a86b377SEli Cohen 
20371a86b377SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++) {
20381a86b377SEli Cohen 		memset(&ndev->vqs[i].ri, 0, sizeof(ndev->vqs[i].ri));
20391a86b377SEli Cohen 		save_channel_info(ndev, &ndev->vqs[i]);
20401a86b377SEli Cohen 	}
20411a86b377SEli Cohen 	return 0;
20421a86b377SEli Cohen }
20431a86b377SEli Cohen 
20441a86b377SEli Cohen static void mlx5_clear_vqs(struct mlx5_vdpa_net *ndev)
20451a86b377SEli Cohen {
20461a86b377SEli Cohen 	int i;
20471a86b377SEli Cohen 
20481a86b377SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++)
20491a86b377SEli Cohen 		memset(&ndev->vqs[i], 0, offsetof(struct mlx5_vdpa_virtqueue, ri));
20501a86b377SEli Cohen }
20511a86b377SEli Cohen 
20521a86b377SEli Cohen static void restore_channels_info(struct mlx5_vdpa_net *ndev)
20531a86b377SEli Cohen {
20541a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
20551a86b377SEli Cohen 	struct mlx5_vq_restore_info *ri;
20561a86b377SEli Cohen 	int i;
20571a86b377SEli Cohen 
20581a86b377SEli Cohen 	mlx5_clear_vqs(ndev);
20591a86b377SEli Cohen 	init_mvqs(ndev);
20601a86b377SEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++) {
20611a86b377SEli Cohen 		mvq = &ndev->vqs[i];
20621a86b377SEli Cohen 		ri = &mvq->ri;
20631a86b377SEli Cohen 		if (!ri->restore)
20641a86b377SEli Cohen 			continue;
20651a86b377SEli Cohen 
20661a86b377SEli Cohen 		mvq->avail_idx = ri->avail_index;
2067b35ccebeSEli Cohen 		mvq->used_idx = ri->used_index;
20681a86b377SEli Cohen 		mvq->ready = ri->ready;
20691a86b377SEli Cohen 		mvq->num_ent = ri->num_ent;
20701a86b377SEli Cohen 		mvq->desc_addr = ri->desc_addr;
20711a86b377SEli Cohen 		mvq->device_addr = ri->device_addr;
20721a86b377SEli Cohen 		mvq->driver_addr = ri->driver_addr;
20731a86b377SEli Cohen 	}
20741a86b377SEli Cohen }
20751a86b377SEli Cohen 
2076ae0428deSEli Cohen static int mlx5_vdpa_change_map(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb)
20771a86b377SEli Cohen {
2078ae0428deSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
20791a86b377SEli Cohen 	int err;
20801a86b377SEli Cohen 
20811a86b377SEli Cohen 	suspend_vqs(ndev);
20821a86b377SEli Cohen 	err = save_channels_info(ndev);
20831a86b377SEli Cohen 	if (err)
20841a86b377SEli Cohen 		goto err_mr;
20851a86b377SEli Cohen 
20861a86b377SEli Cohen 	teardown_driver(ndev);
2087ae0428deSEli Cohen 	mlx5_vdpa_destroy_mr(mvdev);
2088ae0428deSEli Cohen 	err = mlx5_vdpa_create_mr(mvdev, iotlb);
20891a86b377SEli Cohen 	if (err)
20901a86b377SEli Cohen 		goto err_mr;
20911a86b377SEli Cohen 
2092ae0428deSEli Cohen 	if (!(mvdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
20931897f0b6SEli Cohen 		return 0;
20941897f0b6SEli Cohen 
20951a86b377SEli Cohen 	restore_channels_info(ndev);
2096ae0428deSEli Cohen 	err = setup_driver(mvdev);
20971a86b377SEli Cohen 	if (err)
20981a86b377SEli Cohen 		goto err_setup;
20991a86b377SEli Cohen 
21001a86b377SEli Cohen 	return 0;
21011a86b377SEli Cohen 
21021a86b377SEli Cohen err_setup:
2103ae0428deSEli Cohen 	mlx5_vdpa_destroy_mr(mvdev);
21041a86b377SEli Cohen err_mr:
21051a86b377SEli Cohen 	return err;
21061a86b377SEli Cohen }
21071a86b377SEli Cohen 
2108ae0428deSEli Cohen static int setup_driver(struct mlx5_vdpa_dev *mvdev)
21091a86b377SEli Cohen {
2110ae0428deSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
21111a86b377SEli Cohen 	int err;
21121a86b377SEli Cohen 
21131a86b377SEli Cohen 	mutex_lock(&ndev->reslock);
21141a86b377SEli Cohen 	if (ndev->setup) {
2115ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "setup driver called for already setup driver\n");
21161a86b377SEli Cohen 		err = 0;
21171a86b377SEli Cohen 		goto out;
21181a86b377SEli Cohen 	}
2119ae0428deSEli Cohen 	err = setup_virtqueues(mvdev);
21201a86b377SEli Cohen 	if (err) {
2121ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "setup_virtqueues\n");
21221a86b377SEli Cohen 		goto out;
21231a86b377SEli Cohen 	}
21241a86b377SEli Cohen 
21251a86b377SEli Cohen 	err = create_rqt(ndev);
21261a86b377SEli Cohen 	if (err) {
2127ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "create_rqt\n");
21281a86b377SEli Cohen 		goto err_rqt;
21291a86b377SEli Cohen 	}
21301a86b377SEli Cohen 
21311a86b377SEli Cohen 	err = create_tir(ndev);
21321a86b377SEli Cohen 	if (err) {
2133ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "create_tir\n");
21341a86b377SEli Cohen 		goto err_tir;
21351a86b377SEli Cohen 	}
21361a86b377SEli Cohen 
21371a86b377SEli Cohen 	err = add_fwd_to_tir(ndev);
21381a86b377SEli Cohen 	if (err) {
2139ae0428deSEli Cohen 		mlx5_vdpa_warn(mvdev, "add_fwd_to_tir\n");
21401a86b377SEli Cohen 		goto err_fwd;
21411a86b377SEli Cohen 	}
21421a86b377SEli Cohen 	ndev->setup = true;
21431a86b377SEli Cohen 	mutex_unlock(&ndev->reslock);
21441a86b377SEli Cohen 
21451a86b377SEli Cohen 	return 0;
21461a86b377SEli Cohen 
21471a86b377SEli Cohen err_fwd:
21481a86b377SEli Cohen 	destroy_tir(ndev);
21491a86b377SEli Cohen err_tir:
21501a86b377SEli Cohen 	destroy_rqt(ndev);
21511a86b377SEli Cohen err_rqt:
21521a86b377SEli Cohen 	teardown_virtqueues(ndev);
21531a86b377SEli Cohen out:
21541a86b377SEli Cohen 	mutex_unlock(&ndev->reslock);
21551a86b377SEli Cohen 	return err;
21561a86b377SEli Cohen }
21571a86b377SEli Cohen 
21581a86b377SEli Cohen static void teardown_driver(struct mlx5_vdpa_net *ndev)
21591a86b377SEli Cohen {
21601a86b377SEli Cohen 	mutex_lock(&ndev->reslock);
21611a86b377SEli Cohen 	if (!ndev->setup)
21621a86b377SEli Cohen 		goto out;
21631a86b377SEli Cohen 
21641a86b377SEli Cohen 	remove_fwd_to_tir(ndev);
21651a86b377SEli Cohen 	destroy_tir(ndev);
21661a86b377SEli Cohen 	destroy_rqt(ndev);
21671a86b377SEli Cohen 	teardown_virtqueues(ndev);
21681a86b377SEli Cohen 	ndev->setup = false;
21691a86b377SEli Cohen out:
21701a86b377SEli Cohen 	mutex_unlock(&ndev->reslock);
21711a86b377SEli Cohen }
21721a86b377SEli Cohen 
2173e3aadf2eSEli Cohen static void clear_vqs_ready(struct mlx5_vdpa_net *ndev)
2174e3aadf2eSEli Cohen {
2175e3aadf2eSEli Cohen 	int i;
2176e3aadf2eSEli Cohen 
2177e3aadf2eSEli Cohen 	for (i = 0; i < ndev->mvdev.max_vqs; i++)
2178e3aadf2eSEli Cohen 		ndev->vqs[i].ready = false;
2179ef12e4bfSEli Cohen 
2180ef12e4bfSEli Cohen 	ndev->mvdev.cvq.ready = false;
2181e3aadf2eSEli Cohen }
2182e3aadf2eSEli Cohen 
21831a86b377SEli Cohen static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status)
21841a86b377SEli Cohen {
21851a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
21861a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
21871a86b377SEli Cohen 	int err;
21881a86b377SEli Cohen 
21891a86b377SEli Cohen 	print_status(mvdev, status, true);
21901a86b377SEli Cohen 
21911a86b377SEli Cohen 	if ((status ^ ndev->mvdev.status) & VIRTIO_CONFIG_S_DRIVER_OK) {
21921a86b377SEli Cohen 		if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
2193ae0428deSEli Cohen 			err = setup_driver(mvdev);
21941a86b377SEli Cohen 			if (err) {
21951a86b377SEli Cohen 				mlx5_vdpa_warn(mvdev, "failed to setup driver\n");
21961a86b377SEli Cohen 				goto err_setup;
21971a86b377SEli Cohen 			}
21981a86b377SEli Cohen 		} else {
21991a86b377SEli Cohen 			mlx5_vdpa_warn(mvdev, "did not expect DRIVER_OK to be cleared\n");
22001a86b377SEli Cohen 			return;
22011a86b377SEli Cohen 		}
22021a86b377SEli Cohen 	}
22031a86b377SEli Cohen 
22041a86b377SEli Cohen 	ndev->mvdev.status = status;
22051a86b377SEli Cohen 	return;
22061a86b377SEli Cohen 
22071a86b377SEli Cohen err_setup:
22081a86b377SEli Cohen 	mlx5_vdpa_destroy_mr(&ndev->mvdev);
22091a86b377SEli Cohen 	ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED;
22101a86b377SEli Cohen }
22111a86b377SEli Cohen 
22120686082dSXie Yongji static int mlx5_vdpa_reset(struct vdpa_device *vdev)
22130686082dSXie Yongji {
22140686082dSXie Yongji 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22150686082dSXie Yongji 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
22160686082dSXie Yongji 
22170686082dSXie Yongji 	print_status(mvdev, 0, true);
22180686082dSXie Yongji 	mlx5_vdpa_info(mvdev, "performing device reset\n");
22190686082dSXie Yongji 	teardown_driver(ndev);
22200686082dSXie Yongji 	clear_vqs_ready(ndev);
22210686082dSXie Yongji 	mlx5_vdpa_destroy_mr(&ndev->mvdev);
22220686082dSXie Yongji 	ndev->mvdev.status = 0;
2223*75560522SEli Cohen 	memset(ndev->event_cbs, 0, sizeof(*ndev->event_cbs) * (mvdev->max_vqs + 1));
22240686082dSXie Yongji 	ndev->mvdev.actual_features = 0;
22250686082dSXie Yongji 	++mvdev->generation;
22260686082dSXie Yongji 	if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) {
22270686082dSXie Yongji 		if (mlx5_vdpa_create_mr(mvdev, NULL))
22280686082dSXie Yongji 			mlx5_vdpa_warn(mvdev, "create MR failed\n");
22290686082dSXie Yongji 	}
22300686082dSXie Yongji 
22310686082dSXie Yongji 	return 0;
22320686082dSXie Yongji }
22330686082dSXie Yongji 
2234442706f9SStefano Garzarella static size_t mlx5_vdpa_get_config_size(struct vdpa_device *vdev)
2235442706f9SStefano Garzarella {
2236442706f9SStefano Garzarella 	return sizeof(struct virtio_net_config);
2237442706f9SStefano Garzarella }
2238442706f9SStefano Garzarella 
22391a86b377SEli Cohen static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf,
22401a86b377SEli Cohen 				 unsigned int len)
22411a86b377SEli Cohen {
22421a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22431a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
22441a86b377SEli Cohen 
2245dcfde163SStefano Garzarella 	if (offset + len <= sizeof(struct virtio_net_config))
22462874211fSDan Carpenter 		memcpy(buf, (u8 *)&ndev->config + offset, len);
22471a86b377SEli Cohen }
22481a86b377SEli Cohen 
22491a86b377SEli Cohen static void mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf,
22501a86b377SEli Cohen 				 unsigned int len)
22511a86b377SEli Cohen {
22521a86b377SEli Cohen 	/* not supported */
22531a86b377SEli Cohen }
22541a86b377SEli Cohen 
22551a86b377SEli Cohen static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev)
22561a86b377SEli Cohen {
22571a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22581a86b377SEli Cohen 
22591a86b377SEli Cohen 	return mvdev->generation;
22601a86b377SEli Cohen }
22611a86b377SEli Cohen 
22621a86b377SEli Cohen static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb)
22631a86b377SEli Cohen {
22641a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22651a86b377SEli Cohen 	bool change_map;
22661a86b377SEli Cohen 	int err;
22671a86b377SEli Cohen 
22681a86b377SEli Cohen 	err = mlx5_vdpa_handle_set_map(mvdev, iotlb, &change_map);
22691a86b377SEli Cohen 	if (err) {
22701a86b377SEli Cohen 		mlx5_vdpa_warn(mvdev, "set map failed(%d)\n", err);
22711a86b377SEli Cohen 		return err;
22721a86b377SEli Cohen 	}
22731a86b377SEli Cohen 
22741a86b377SEli Cohen 	if (change_map)
2275ae0428deSEli Cohen 		return mlx5_vdpa_change_map(mvdev, iotlb);
22761a86b377SEli Cohen 
22771a86b377SEli Cohen 	return 0;
22781a86b377SEli Cohen }
22791a86b377SEli Cohen 
22801a86b377SEli Cohen static void mlx5_vdpa_free(struct vdpa_device *vdev)
22811a86b377SEli Cohen {
22821a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
22837c9f131fSEli Cohen 	struct mlx5_core_dev *pfmdev;
22841a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev;
22851a86b377SEli Cohen 
22861a86b377SEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
22871a86b377SEli Cohen 
22881a86b377SEli Cohen 	free_resources(ndev);
22896f5312f8SEli Cohen 	mlx5_vdpa_destroy_mr(mvdev);
22907c9f131fSEli Cohen 	if (!is_zero_ether_addr(ndev->config.mac)) {
22917c9f131fSEli Cohen 		pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
22927c9f131fSEli Cohen 		mlx5_mpfs_del_mac(pfmdev, ndev->config.mac);
22937c9f131fSEli Cohen 	}
22941a86b377SEli Cohen 	mlx5_vdpa_free_resources(&ndev->mvdev);
22951a86b377SEli Cohen 	mutex_destroy(&ndev->reslock);
2296*75560522SEli Cohen 	kfree(ndev->event_cbs);
2297*75560522SEli Cohen 	kfree(ndev->vqs);
22981a86b377SEli Cohen }
22991a86b377SEli Cohen 
23001a86b377SEli Cohen static struct vdpa_notification_area mlx5_get_vq_notification(struct vdpa_device *vdev, u16 idx)
23011a86b377SEli Cohen {
2302b57c46cbSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
23031a86b377SEli Cohen 	struct vdpa_notification_area ret = {};
2304b57c46cbSEli Cohen 	struct mlx5_vdpa_net *ndev;
2305b57c46cbSEli Cohen 	phys_addr_t addr;
23061a86b377SEli Cohen 
23075262912eSEli Cohen 	if (!is_index_valid(mvdev, idx) || is_ctrl_vq_idx(mvdev, idx))
2308e4fc6650SEli Cohen 		return ret;
2309e4fc6650SEli Cohen 
2310b57c46cbSEli Cohen 	/* If SF BAR size is smaller than PAGE_SIZE, do not use direct
2311b57c46cbSEli Cohen 	 * notification to avoid the risk of mapping pages that contain BAR of more
2312b57c46cbSEli Cohen 	 * than one SF
2313b57c46cbSEli Cohen 	 */
2314b57c46cbSEli Cohen 	if (MLX5_CAP_GEN(mvdev->mdev, log_min_sf_size) + 12 < PAGE_SHIFT)
2315b57c46cbSEli Cohen 		return ret;
2316b57c46cbSEli Cohen 
2317b57c46cbSEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
2318b57c46cbSEli Cohen 	addr = (phys_addr_t)ndev->mvdev.res.phys_kick_addr;
2319b57c46cbSEli Cohen 	ret.addr = addr;
2320b57c46cbSEli Cohen 	ret.size = PAGE_SIZE;
23211a86b377SEli Cohen 	return ret;
23221a86b377SEli Cohen }
23231a86b377SEli Cohen 
23241a86b377SEli Cohen static int mlx5_get_vq_irq(struct vdpa_device *vdv, u16 idx)
23251a86b377SEli Cohen {
23261a86b377SEli Cohen 	return -EOPNOTSUPP;
23271a86b377SEli Cohen }
23281a86b377SEli Cohen 
2329a64917bcSEli Cohen static u64 mlx5_vdpa_get_driver_features(struct vdpa_device *vdev)
2330a64917bcSEli Cohen {
2331a64917bcSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev);
2332a64917bcSEli Cohen 
2333a64917bcSEli Cohen 	return mvdev->actual_features;
2334a64917bcSEli Cohen }
2335a64917bcSEli Cohen 
23361a86b377SEli Cohen static const struct vdpa_config_ops mlx5_vdpa_ops = {
23371a86b377SEli Cohen 	.set_vq_address = mlx5_vdpa_set_vq_address,
23381a86b377SEli Cohen 	.set_vq_num = mlx5_vdpa_set_vq_num,
23391a86b377SEli Cohen 	.kick_vq = mlx5_vdpa_kick_vq,
23401a86b377SEli Cohen 	.set_vq_cb = mlx5_vdpa_set_vq_cb,
23411a86b377SEli Cohen 	.set_vq_ready = mlx5_vdpa_set_vq_ready,
23421a86b377SEli Cohen 	.get_vq_ready = mlx5_vdpa_get_vq_ready,
23431a86b377SEli Cohen 	.set_vq_state = mlx5_vdpa_set_vq_state,
23441a86b377SEli Cohen 	.get_vq_state = mlx5_vdpa_get_vq_state,
23451a86b377SEli Cohen 	.get_vq_notification = mlx5_get_vq_notification,
23461a86b377SEli Cohen 	.get_vq_irq = mlx5_get_vq_irq,
23471a86b377SEli Cohen 	.get_vq_align = mlx5_vdpa_get_vq_align,
2348a64917bcSEli Cohen 	.get_device_features = mlx5_vdpa_get_device_features,
2349a64917bcSEli Cohen 	.set_driver_features = mlx5_vdpa_set_driver_features,
2350a64917bcSEli Cohen 	.get_driver_features = mlx5_vdpa_get_driver_features,
23511a86b377SEli Cohen 	.set_config_cb = mlx5_vdpa_set_config_cb,
23521a86b377SEli Cohen 	.get_vq_num_max = mlx5_vdpa_get_vq_num_max,
23531a86b377SEli Cohen 	.get_device_id = mlx5_vdpa_get_device_id,
23541a86b377SEli Cohen 	.get_vendor_id = mlx5_vdpa_get_vendor_id,
23551a86b377SEli Cohen 	.get_status = mlx5_vdpa_get_status,
23561a86b377SEli Cohen 	.set_status = mlx5_vdpa_set_status,
23570686082dSXie Yongji 	.reset = mlx5_vdpa_reset,
2358442706f9SStefano Garzarella 	.get_config_size = mlx5_vdpa_get_config_size,
23591a86b377SEli Cohen 	.get_config = mlx5_vdpa_get_config,
23601a86b377SEli Cohen 	.set_config = mlx5_vdpa_set_config,
23611a86b377SEli Cohen 	.get_generation = mlx5_vdpa_get_generation,
23621a86b377SEli Cohen 	.set_map = mlx5_vdpa_set_map,
23631a86b377SEli Cohen 	.free = mlx5_vdpa_free,
23641a86b377SEli Cohen };
23651a86b377SEli Cohen 
2366d084d996SSi-Wei Liu static int query_mtu(struct mlx5_core_dev *mdev, u16 *mtu)
2367d084d996SSi-Wei Liu {
2368d084d996SSi-Wei Liu 	u16 hw_mtu;
2369d084d996SSi-Wei Liu 	int err;
2370d084d996SSi-Wei Liu 
2371d084d996SSi-Wei Liu 	err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2372d084d996SSi-Wei Liu 	if (err)
2373d084d996SSi-Wei Liu 		return err;
2374d084d996SSi-Wei Liu 
2375d084d996SSi-Wei Liu 	*mtu = hw_mtu - MLX5V_ETH_HARD_MTU;
2376d084d996SSi-Wei Liu 	return 0;
2377d084d996SSi-Wei Liu }
2378d084d996SSi-Wei Liu 
23791a86b377SEli Cohen static int alloc_resources(struct mlx5_vdpa_net *ndev)
23801a86b377SEli Cohen {
23811a86b377SEli Cohen 	struct mlx5_vdpa_net_resources *res = &ndev->res;
23821a86b377SEli Cohen 	int err;
23831a86b377SEli Cohen 
23841a86b377SEli Cohen 	if (res->valid) {
23851a86b377SEli Cohen 		mlx5_vdpa_warn(&ndev->mvdev, "resources already allocated\n");
23861a86b377SEli Cohen 		return -EEXIST;
23871a86b377SEli Cohen 	}
23881a86b377SEli Cohen 
23891a86b377SEli Cohen 	err = mlx5_vdpa_alloc_transport_domain(&ndev->mvdev, &res->tdn);
23901a86b377SEli Cohen 	if (err)
23911a86b377SEli Cohen 		return err;
23921a86b377SEli Cohen 
23931a86b377SEli Cohen 	err = create_tis(ndev);
23941a86b377SEli Cohen 	if (err)
23951a86b377SEli Cohen 		goto err_tis;
23961a86b377SEli Cohen 
23971a86b377SEli Cohen 	res->valid = true;
23981a86b377SEli Cohen 
23991a86b377SEli Cohen 	return 0;
24001a86b377SEli Cohen 
24011a86b377SEli Cohen err_tis:
24021a86b377SEli Cohen 	mlx5_vdpa_dealloc_transport_domain(&ndev->mvdev, res->tdn);
24031a86b377SEli Cohen 	return err;
24041a86b377SEli Cohen }
24051a86b377SEli Cohen 
24061a86b377SEli Cohen static void free_resources(struct mlx5_vdpa_net *ndev)
24071a86b377SEli Cohen {
24081a86b377SEli Cohen 	struct mlx5_vdpa_net_resources *res = &ndev->res;
24091a86b377SEli Cohen 
24101a86b377SEli Cohen 	if (!res->valid)
24111a86b377SEli Cohen 		return;
24121a86b377SEli Cohen 
24131a86b377SEli Cohen 	destroy_tis(ndev);
24141a86b377SEli Cohen 	mlx5_vdpa_dealloc_transport_domain(&ndev->mvdev, res->tdn);
24151a86b377SEli Cohen 	res->valid = false;
24161a86b377SEli Cohen }
24171a86b377SEli Cohen 
24181a86b377SEli Cohen static void init_mvqs(struct mlx5_vdpa_net *ndev)
24191a86b377SEli Cohen {
24201a86b377SEli Cohen 	struct mlx5_vdpa_virtqueue *mvq;
24211a86b377SEli Cohen 	int i;
24221a86b377SEli Cohen 
24231a86b377SEli Cohen 	for (i = 0; i < 2 * mlx5_vdpa_max_qps(ndev->mvdev.max_vqs); ++i) {
24241a86b377SEli Cohen 		mvq = &ndev->vqs[i];
24251a86b377SEli Cohen 		memset(mvq, 0, offsetof(struct mlx5_vdpa_virtqueue, ri));
24261a86b377SEli Cohen 		mvq->index = i;
24271a86b377SEli Cohen 		mvq->ndev = ndev;
24281a86b377SEli Cohen 		mvq->fwqp.fw = true;
24291a86b377SEli Cohen 	}
24301a86b377SEli Cohen 	for (; i < ndev->mvdev.max_vqs; i++) {
24311a86b377SEli Cohen 		mvq = &ndev->vqs[i];
24321a86b377SEli Cohen 		memset(mvq, 0, offsetof(struct mlx5_vdpa_virtqueue, ri));
24331a86b377SEli Cohen 		mvq->index = i;
24341a86b377SEli Cohen 		mvq->ndev = ndev;
24351a86b377SEli Cohen 	}
24361a86b377SEli Cohen }
24371a86b377SEli Cohen 
243858926c8aSEli Cohen struct mlx5_vdpa_mgmtdev {
243958926c8aSEli Cohen 	struct vdpa_mgmt_dev mgtdev;
244058926c8aSEli Cohen 	struct mlx5_adev *madev;
244158926c8aSEli Cohen 	struct mlx5_vdpa_net *ndev;
244258926c8aSEli Cohen };
244358926c8aSEli Cohen 
2444edf747afSEli Cohen static u8 query_vport_state(struct mlx5_core_dev *mdev, u8 opmod, u16 vport)
2445edf747afSEli Cohen {
2446edf747afSEli Cohen 	u32 out[MLX5_ST_SZ_DW(query_vport_state_out)] = {};
2447edf747afSEli Cohen 	u32 in[MLX5_ST_SZ_DW(query_vport_state_in)] = {};
2448edf747afSEli Cohen 	int err;
2449edf747afSEli Cohen 
2450edf747afSEli Cohen 	MLX5_SET(query_vport_state_in, in, opcode, MLX5_CMD_OP_QUERY_VPORT_STATE);
2451edf747afSEli Cohen 	MLX5_SET(query_vport_state_in, in, op_mod, opmod);
2452edf747afSEli Cohen 	MLX5_SET(query_vport_state_in, in, vport_number, vport);
2453edf747afSEli Cohen 	if (vport)
2454edf747afSEli Cohen 		MLX5_SET(query_vport_state_in, in, other_vport, 1);
2455edf747afSEli Cohen 
2456edf747afSEli Cohen 	err = mlx5_cmd_exec_inout(mdev, query_vport_state, in, out);
2457edf747afSEli Cohen 	if (err)
2458edf747afSEli Cohen 		return 0;
2459edf747afSEli Cohen 
2460edf747afSEli Cohen 	return MLX5_GET(query_vport_state_out, out, state);
2461edf747afSEli Cohen }
2462edf747afSEli Cohen 
2463edf747afSEli Cohen static bool get_link_state(struct mlx5_vdpa_dev *mvdev)
2464edf747afSEli Cohen {
2465edf747afSEli Cohen 	if (query_vport_state(mvdev->mdev, MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT, 0) ==
2466edf747afSEli Cohen 	    VPORT_STATE_UP)
2467edf747afSEli Cohen 		return true;
2468edf747afSEli Cohen 
2469edf747afSEli Cohen 	return false;
2470edf747afSEli Cohen }
2471edf747afSEli Cohen 
2472edf747afSEli Cohen static void update_carrier(struct work_struct *work)
2473edf747afSEli Cohen {
2474edf747afSEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
2475edf747afSEli Cohen 	struct mlx5_vdpa_dev *mvdev;
2476edf747afSEli Cohen 	struct mlx5_vdpa_net *ndev;
2477edf747afSEli Cohen 
2478edf747afSEli Cohen 	wqent = container_of(work, struct mlx5_vdpa_wq_ent, work);
2479edf747afSEli Cohen 	mvdev = wqent->mvdev;
2480edf747afSEli Cohen 	ndev = to_mlx5_vdpa_ndev(mvdev);
2481edf747afSEli Cohen 	if (get_link_state(mvdev))
2482edf747afSEli Cohen 		ndev->config.status |= cpu_to_mlx5vdpa16(mvdev, VIRTIO_NET_S_LINK_UP);
2483edf747afSEli Cohen 	else
2484edf747afSEli Cohen 		ndev->config.status &= cpu_to_mlx5vdpa16(mvdev, ~VIRTIO_NET_S_LINK_UP);
2485edf747afSEli Cohen 
2486edf747afSEli Cohen 	if (ndev->config_cb.callback)
2487edf747afSEli Cohen 		ndev->config_cb.callback(ndev->config_cb.private);
2488edf747afSEli Cohen 
2489edf747afSEli Cohen 	kfree(wqent);
2490edf747afSEli Cohen }
2491edf747afSEli Cohen 
2492edf747afSEli Cohen static int event_handler(struct notifier_block *nb, unsigned long event, void *param)
2493edf747afSEli Cohen {
2494edf747afSEli Cohen 	struct mlx5_vdpa_net *ndev = container_of(nb, struct mlx5_vdpa_net, nb);
2495edf747afSEli Cohen 	struct mlx5_eqe *eqe = param;
2496edf747afSEli Cohen 	int ret = NOTIFY_DONE;
2497edf747afSEli Cohen 	struct mlx5_vdpa_wq_ent *wqent;
2498edf747afSEli Cohen 
2499edf747afSEli Cohen 	if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
2500edf747afSEli Cohen 		switch (eqe->sub_type) {
2501edf747afSEli Cohen 		case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2502edf747afSEli Cohen 		case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2503edf747afSEli Cohen 			wqent = kzalloc(sizeof(*wqent), GFP_ATOMIC);
2504edf747afSEli Cohen 			if (!wqent)
2505edf747afSEli Cohen 				return NOTIFY_DONE;
2506edf747afSEli Cohen 
2507edf747afSEli Cohen 			wqent->mvdev = &ndev->mvdev;
2508edf747afSEli Cohen 			INIT_WORK(&wqent->work, update_carrier);
2509edf747afSEli Cohen 			queue_work(ndev->mvdev.wq, &wqent->work);
2510edf747afSEli Cohen 			ret = NOTIFY_OK;
2511edf747afSEli Cohen 			break;
2512edf747afSEli Cohen 		default:
2513edf747afSEli Cohen 			return NOTIFY_DONE;
2514edf747afSEli Cohen 		}
2515edf747afSEli Cohen 		return ret;
2516edf747afSEli Cohen 	}
2517edf747afSEli Cohen 	return ret;
2518edf747afSEli Cohen }
2519edf747afSEli Cohen 
2520d8ca2fa5SParav Pandit static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
2521d8ca2fa5SParav Pandit 			     const struct vdpa_dev_set_config *add_config)
25221a86b377SEli Cohen {
252358926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev = container_of(v_mdev, struct mlx5_vdpa_mgmtdev, mgtdev);
25241a86b377SEli Cohen 	struct virtio_net_config *config;
25257c9f131fSEli Cohen 	struct mlx5_core_dev *pfmdev;
25261a86b377SEli Cohen 	struct mlx5_vdpa_dev *mvdev;
25271a86b377SEli Cohen 	struct mlx5_vdpa_net *ndev;
252858926c8aSEli Cohen 	struct mlx5_core_dev *mdev;
25291a86b377SEli Cohen 	u32 max_vqs;
2530246fd1caSEli Cohen 	u16 mtu;
25311a86b377SEli Cohen 	int err;
25321a86b377SEli Cohen 
253358926c8aSEli Cohen 	if (mgtdev->ndev)
253458926c8aSEli Cohen 		return -ENOSPC;
253558926c8aSEli Cohen 
253658926c8aSEli Cohen 	mdev = mgtdev->madev->mdev;
2537879753c8SEli Cohen 	if (!(MLX5_CAP_DEV_VDPA_EMULATION(mdev, virtio_queue_type) &
2538879753c8SEli Cohen 	    MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_SPLIT)) {
2539879753c8SEli Cohen 		dev_warn(mdev->device, "missing support for split virtqueues\n");
2540879753c8SEli Cohen 		return -EOPNOTSUPP;
2541879753c8SEli Cohen 	}
2542879753c8SEli Cohen 
25431a86b377SEli Cohen 	max_vqs = MLX5_CAP_DEV_VDPA_EMULATION(mdev, max_num_virtio_queues);
2544*75560522SEli Cohen 	if (max_vqs < 2) {
2545*75560522SEli Cohen 		dev_warn(mdev->device,
2546*75560522SEli Cohen 			 "%d virtqueues are supported. At least 2 are required\n",
2547*75560522SEli Cohen 			 max_vqs);
2548*75560522SEli Cohen 		return -EAGAIN;
2549*75560522SEli Cohen 	}
2550*75560522SEli Cohen 
2551*75560522SEli Cohen 	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP)) {
2552*75560522SEli Cohen 		if (add_config->net.max_vq_pairs > max_vqs / 2)
2553*75560522SEli Cohen 			return -EINVAL;
2554*75560522SEli Cohen 		max_vqs = min_t(u32, max_vqs, 2 * add_config->net.max_vq_pairs);
2555*75560522SEli Cohen 	} else {
2556*75560522SEli Cohen 		max_vqs = 2;
2557*75560522SEli Cohen 	}
25581a86b377SEli Cohen 
25591a86b377SEli Cohen 	ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
2560d8945ec4SXie Yongji 				 name, false);
25611a86b377SEli Cohen 	if (IS_ERR(ndev))
256274c9729dSLeon Romanovsky 		return PTR_ERR(ndev);
25631a86b377SEli Cohen 
25641a86b377SEli Cohen 	ndev->mvdev.max_vqs = max_vqs;
25651a86b377SEli Cohen 	mvdev = &ndev->mvdev;
25661a86b377SEli Cohen 	mvdev->mdev = mdev;
2567*75560522SEli Cohen 
2568*75560522SEli Cohen 	ndev->vqs = kcalloc(max_vqs, sizeof(*ndev->vqs), GFP_KERNEL);
2569*75560522SEli Cohen 	ndev->event_cbs = kcalloc(max_vqs + 1, sizeof(*ndev->event_cbs), GFP_KERNEL);
2570*75560522SEli Cohen 	if (!ndev->vqs || !ndev->event_cbs) {
2571*75560522SEli Cohen 		err = -ENOMEM;
2572*75560522SEli Cohen 		goto err_alloc;
2573*75560522SEli Cohen 	}
2574*75560522SEli Cohen 
25751a86b377SEli Cohen 	init_mvqs(ndev);
25761a86b377SEli Cohen 	mutex_init(&ndev->reslock);
25771a86b377SEli Cohen 	config = &ndev->config;
2578246fd1caSEli Cohen 	err = query_mtu(mdev, &mtu);
25791a86b377SEli Cohen 	if (err)
25801a86b377SEli Cohen 		goto err_mtu;
25811a86b377SEli Cohen 
2582246fd1caSEli Cohen 	ndev->config.mtu = cpu_to_mlx5vdpa16(mvdev, mtu);
25831a86b377SEli Cohen 
2584edf747afSEli Cohen 	if (get_link_state(mvdev))
2585edf747afSEli Cohen 		ndev->config.status |= cpu_to_mlx5vdpa16(mvdev, VIRTIO_NET_S_LINK_UP);
2586edf747afSEli Cohen 	else
2587edf747afSEli Cohen 		ndev->config.status &= cpu_to_mlx5vdpa16(mvdev, ~VIRTIO_NET_S_LINK_UP);
2588edf747afSEli Cohen 
2589a007d940SEli Cohen 	if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
2590a007d940SEli Cohen 		memcpy(ndev->config.mac, add_config->net.mac, ETH_ALEN);
2591a007d940SEli Cohen 	} else {
25921a86b377SEli Cohen 		err = mlx5_query_nic_vport_mac_address(mdev, 0, 0, config->mac);
25931a86b377SEli Cohen 		if (err)
25941a86b377SEli Cohen 			goto err_mtu;
2595a007d940SEli Cohen 	}
25961a86b377SEli Cohen 
25977c9f131fSEli Cohen 	if (!is_zero_ether_addr(config->mac)) {
25987c9f131fSEli Cohen 		pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
25997c9f131fSEli Cohen 		err = mlx5_mpfs_add_mac(pfmdev, config->mac);
26007c9f131fSEli Cohen 		if (err)
26017c9f131fSEli Cohen 			goto err_mtu;
26025262912eSEli Cohen 
26035262912eSEli Cohen 		ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_MAC);
26047c9f131fSEli Cohen 	}
26057c9f131fSEli Cohen 
26065262912eSEli Cohen 	config->max_virtqueue_pairs = cpu_to_mlx5vdpa16(mvdev, mlx5_vdpa_max_qps(max_vqs));
26077d23dcdfSEli Cohen 	mvdev->vdev.dma_dev = &mdev->pdev->dev;
26081a86b377SEli Cohen 	err = mlx5_vdpa_alloc_resources(&ndev->mvdev);
26091a86b377SEli Cohen 	if (err)
26107c9f131fSEli Cohen 		goto err_mpfs;
26111a86b377SEli Cohen 
26126f5312f8SEli Cohen 	if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) {
26136f5312f8SEli Cohen 		err = mlx5_vdpa_create_mr(mvdev, NULL);
26141a86b377SEli Cohen 		if (err)
26151a86b377SEli Cohen 			goto err_res;
26166f5312f8SEli Cohen 	}
26176f5312f8SEli Cohen 
26186f5312f8SEli Cohen 	err = alloc_resources(ndev);
26196f5312f8SEli Cohen 	if (err)
26206f5312f8SEli Cohen 		goto err_mr;
26211a86b377SEli Cohen 
2622218bdd20SEli Cohen 	mvdev->wq = create_singlethread_workqueue("mlx5_vdpa_wq");
26235262912eSEli Cohen 	if (!mvdev->wq) {
26245262912eSEli Cohen 		err = -ENOMEM;
26255262912eSEli Cohen 		goto err_res2;
26265262912eSEli Cohen 	}
26275262912eSEli Cohen 
2628edf747afSEli Cohen 	ndev->nb.notifier_call = event_handler;
2629edf747afSEli Cohen 	mlx5_notifier_register(mdev, &ndev->nb);
26305262912eSEli Cohen 	ndev->cur_num_vqs = 2 * mlx5_vdpa_max_qps(max_vqs);
263158926c8aSEli Cohen 	mvdev->vdev.mdev = &mgtdev->mgtdev;
26325262912eSEli Cohen 	err = _vdpa_register_device(&mvdev->vdev, ndev->cur_num_vqs + 1);
26331a86b377SEli Cohen 	if (err)
26341a86b377SEli Cohen 		goto err_reg;
26351a86b377SEli Cohen 
263658926c8aSEli Cohen 	mgtdev->ndev = ndev;
263774c9729dSLeon Romanovsky 	return 0;
26381a86b377SEli Cohen 
26391a86b377SEli Cohen err_reg:
26405262912eSEli Cohen 	destroy_workqueue(mvdev->wq);
26415262912eSEli Cohen err_res2:
26421a86b377SEli Cohen 	free_resources(ndev);
26436f5312f8SEli Cohen err_mr:
26446f5312f8SEli Cohen 	mlx5_vdpa_destroy_mr(mvdev);
26451a86b377SEli Cohen err_res:
26461a86b377SEli Cohen 	mlx5_vdpa_free_resources(&ndev->mvdev);
26477c9f131fSEli Cohen err_mpfs:
26487c9f131fSEli Cohen 	if (!is_zero_ether_addr(config->mac))
26497c9f131fSEli Cohen 		mlx5_mpfs_del_mac(pfmdev, config->mac);
26501a86b377SEli Cohen err_mtu:
26511a86b377SEli Cohen 	mutex_destroy(&ndev->reslock);
2652*75560522SEli Cohen err_alloc:
26531a86b377SEli Cohen 	put_device(&mvdev->vdev.dev);
265474c9729dSLeon Romanovsky 	return err;
26551a86b377SEli Cohen }
26561a86b377SEli Cohen 
265758926c8aSEli Cohen static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev)
265858926c8aSEli Cohen {
265958926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev = container_of(v_mdev, struct mlx5_vdpa_mgmtdev, mgtdev);
26605262912eSEli Cohen 	struct mlx5_vdpa_dev *mvdev = to_mvdev(dev);
2661edf747afSEli Cohen 	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
266258926c8aSEli Cohen 
2663edf747afSEli Cohen 	mlx5_notifier_unregister(mvdev->mdev, &ndev->nb);
26645262912eSEli Cohen 	destroy_workqueue(mvdev->wq);
266558926c8aSEli Cohen 	_vdpa_unregister_device(dev);
266658926c8aSEli Cohen 	mgtdev->ndev = NULL;
266758926c8aSEli Cohen }
266858926c8aSEli Cohen 
266958926c8aSEli Cohen static const struct vdpa_mgmtdev_ops mdev_ops = {
267058926c8aSEli Cohen 	.dev_add = mlx5_vdpa_dev_add,
267158926c8aSEli Cohen 	.dev_del = mlx5_vdpa_dev_del,
267258926c8aSEli Cohen };
267358926c8aSEli Cohen 
267458926c8aSEli Cohen static struct virtio_device_id id_table[] = {
267558926c8aSEli Cohen 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
267658926c8aSEli Cohen 	{ 0 },
267758926c8aSEli Cohen };
267858926c8aSEli Cohen 
267958926c8aSEli Cohen static int mlx5v_probe(struct auxiliary_device *adev,
268058926c8aSEli Cohen 		       const struct auxiliary_device_id *id)
268158926c8aSEli Cohen 
268258926c8aSEli Cohen {
268358926c8aSEli Cohen 	struct mlx5_adev *madev = container_of(adev, struct mlx5_adev, adev);
268458926c8aSEli Cohen 	struct mlx5_core_dev *mdev = madev->mdev;
268558926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev;
268658926c8aSEli Cohen 	int err;
268758926c8aSEli Cohen 
268858926c8aSEli Cohen 	mgtdev = kzalloc(sizeof(*mgtdev), GFP_KERNEL);
268958926c8aSEli Cohen 	if (!mgtdev)
269058926c8aSEli Cohen 		return -ENOMEM;
269158926c8aSEli Cohen 
269258926c8aSEli Cohen 	mgtdev->mgtdev.ops = &mdev_ops;
269358926c8aSEli Cohen 	mgtdev->mgtdev.device = mdev->device;
269458926c8aSEli Cohen 	mgtdev->mgtdev.id_table = id_table;
2695*75560522SEli Cohen 	mgtdev->mgtdev.config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) |
2696*75560522SEli Cohen 					  BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
269758926c8aSEli Cohen 	mgtdev->madev = madev;
269858926c8aSEli Cohen 
269958926c8aSEli Cohen 	err = vdpa_mgmtdev_register(&mgtdev->mgtdev);
270058926c8aSEli Cohen 	if (err)
270158926c8aSEli Cohen 		goto reg_err;
270258926c8aSEli Cohen 
270358926c8aSEli Cohen 	dev_set_drvdata(&adev->dev, mgtdev);
270458926c8aSEli Cohen 
270558926c8aSEli Cohen 	return 0;
270658926c8aSEli Cohen 
270758926c8aSEli Cohen reg_err:
270858926c8aSEli Cohen 	kfree(mgtdev);
270958926c8aSEli Cohen 	return err;
271058926c8aSEli Cohen }
271158926c8aSEli Cohen 
271274c9729dSLeon Romanovsky static void mlx5v_remove(struct auxiliary_device *adev)
27131a86b377SEli Cohen {
271458926c8aSEli Cohen 	struct mlx5_vdpa_mgmtdev *mgtdev;
271574c9729dSLeon Romanovsky 
271658926c8aSEli Cohen 	mgtdev = dev_get_drvdata(&adev->dev);
271758926c8aSEli Cohen 	vdpa_mgmtdev_unregister(&mgtdev->mgtdev);
271858926c8aSEli Cohen 	kfree(mgtdev);
27191a86b377SEli Cohen }
272074c9729dSLeon Romanovsky 
272174c9729dSLeon Romanovsky static const struct auxiliary_device_id mlx5v_id_table[] = {
272274c9729dSLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".vnet", },
272374c9729dSLeon Romanovsky 	{},
272474c9729dSLeon Romanovsky };
272574c9729dSLeon Romanovsky 
272674c9729dSLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5v_id_table);
272774c9729dSLeon Romanovsky 
272874c9729dSLeon Romanovsky static struct auxiliary_driver mlx5v_driver = {
272974c9729dSLeon Romanovsky 	.name = "vnet",
273074c9729dSLeon Romanovsky 	.probe = mlx5v_probe,
273174c9729dSLeon Romanovsky 	.remove = mlx5v_remove,
273274c9729dSLeon Romanovsky 	.id_table = mlx5v_id_table,
273374c9729dSLeon Romanovsky };
273474c9729dSLeon Romanovsky 
273574c9729dSLeon Romanovsky module_auxiliary_driver(mlx5v_driver);
2736