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> 91a86b377SEli Cohen #include <linux/virtio_config.h> 1074c9729dSLeon Romanovsky #include <linux/auxiliary_bus.h> 1174c9729dSLeon Romanovsky #include <linux/mlx5/cq.h> 121a86b377SEli Cohen #include <linux/mlx5/qp.h> 131a86b377SEli Cohen #include <linux/mlx5/device.h> 1474c9729dSLeon Romanovsky #include <linux/mlx5/driver.h> 151a86b377SEli Cohen #include <linux/mlx5/vport.h> 161a86b377SEli Cohen #include <linux/mlx5/fs.h> 170aae392bSLeon Romanovsky #include <linux/mlx5/mlx5_ifc_vdpa.h> 187c9f131fSEli Cohen #include <linux/mlx5/mpfs.h> 191a86b377SEli Cohen #include "mlx5_vdpa.h" 201a86b377SEli Cohen 2174c9729dSLeon Romanovsky MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 2274c9729dSLeon Romanovsky MODULE_DESCRIPTION("Mellanox VDPA driver"); 2374c9729dSLeon Romanovsky MODULE_LICENSE("Dual BSD/GPL"); 2474c9729dSLeon Romanovsky 2574c9729dSLeon Romanovsky #define to_mlx5_vdpa_ndev(__mvdev) \ 2674c9729dSLeon Romanovsky container_of(__mvdev, struct mlx5_vdpa_net, mvdev) 271a86b377SEli Cohen #define to_mvdev(__vdev) container_of((__vdev), struct mlx5_vdpa_dev, vdev) 281a86b377SEli Cohen 291a86b377SEli Cohen #define VALID_FEATURES_MASK \ 30cbb52359SNathan Chancellor (BIT_ULL(VIRTIO_NET_F_CSUM) | BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) | \ 31cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) | BIT_ULL(VIRTIO_NET_F_MTU) | BIT_ULL(VIRTIO_NET_F_MAC) | \ 32cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) | BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) | \ 33cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_GUEST_ECN) | BIT_ULL(VIRTIO_NET_F_GUEST_UFO) | BIT_ULL(VIRTIO_NET_F_HOST_TSO4) | \ 34cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_HOST_TSO6) | BIT_ULL(VIRTIO_NET_F_HOST_ECN) | BIT_ULL(VIRTIO_NET_F_HOST_UFO) | \ 35cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) | BIT_ULL(VIRTIO_NET_F_STATUS) | BIT_ULL(VIRTIO_NET_F_CTRL_VQ) | \ 36cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_CTRL_RX) | BIT_ULL(VIRTIO_NET_F_CTRL_VLAN) | \ 37cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA) | BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE) | \ 38cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_MQ) | BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) | BIT_ULL(VIRTIO_NET_F_HASH_REPORT) | \ 39cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_RSS) | BIT_ULL(VIRTIO_NET_F_RSC_EXT) | BIT_ULL(VIRTIO_NET_F_STANDBY) | \ 40cbb52359SNathan Chancellor BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX) | BIT_ULL(VIRTIO_F_NOTIFY_ON_EMPTY) | \ 41cbb52359SNathan Chancellor BIT_ULL(VIRTIO_F_ANY_LAYOUT) | BIT_ULL(VIRTIO_F_VERSION_1) | BIT_ULL(VIRTIO_F_ACCESS_PLATFORM) | \ 42cbb52359SNathan Chancellor BIT_ULL(VIRTIO_F_RING_PACKED) | BIT_ULL(VIRTIO_F_ORDER_PLATFORM) | BIT_ULL(VIRTIO_F_SR_IOV)) 431a86b377SEli Cohen 441a86b377SEli Cohen #define VALID_STATUS_MASK \ 451a86b377SEli Cohen (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK | \ 461a86b377SEli Cohen VIRTIO_CONFIG_S_FEATURES_OK | VIRTIO_CONFIG_S_NEEDS_RESET | VIRTIO_CONFIG_S_FAILED) 471a86b377SEli Cohen 481a86b377SEli Cohen struct mlx5_vdpa_net_resources { 491a86b377SEli Cohen u32 tisn; 501a86b377SEli Cohen u32 tdn; 511a86b377SEli Cohen u32 tirn; 521a86b377SEli Cohen u32 rqtn; 531a86b377SEli Cohen bool valid; 541a86b377SEli Cohen }; 551a86b377SEli Cohen 561a86b377SEli Cohen struct mlx5_vdpa_cq_buf { 571a86b377SEli Cohen struct mlx5_frag_buf_ctrl fbc; 581a86b377SEli Cohen struct mlx5_frag_buf frag_buf; 591a86b377SEli Cohen int cqe_size; 601a86b377SEli Cohen int nent; 611a86b377SEli Cohen }; 621a86b377SEli Cohen 631a86b377SEli Cohen struct mlx5_vdpa_cq { 641a86b377SEli Cohen struct mlx5_core_cq mcq; 651a86b377SEli Cohen struct mlx5_vdpa_cq_buf buf; 661a86b377SEli Cohen struct mlx5_db db; 671a86b377SEli Cohen int cqe; 681a86b377SEli Cohen }; 691a86b377SEli Cohen 701a86b377SEli Cohen struct mlx5_vdpa_umem { 711a86b377SEli Cohen struct mlx5_frag_buf_ctrl fbc; 721a86b377SEli Cohen struct mlx5_frag_buf frag_buf; 731a86b377SEli Cohen int size; 741a86b377SEli Cohen u32 id; 751a86b377SEli Cohen }; 761a86b377SEli Cohen 771a86b377SEli Cohen struct mlx5_vdpa_qp { 781a86b377SEli Cohen struct mlx5_core_qp mqp; 791a86b377SEli Cohen struct mlx5_frag_buf frag_buf; 801a86b377SEli Cohen struct mlx5_db db; 811a86b377SEli Cohen u16 head; 821a86b377SEli Cohen bool fw; 831a86b377SEli Cohen }; 841a86b377SEli Cohen 851a86b377SEli Cohen struct mlx5_vq_restore_info { 861a86b377SEli Cohen u32 num_ent; 871a86b377SEli Cohen u64 desc_addr; 881a86b377SEli Cohen u64 device_addr; 891a86b377SEli Cohen u64 driver_addr; 901a86b377SEli Cohen u16 avail_index; 91b35ccebeSEli Cohen u16 used_index; 921a86b377SEli Cohen bool ready; 931a86b377SEli Cohen struct vdpa_callback cb; 941a86b377SEli Cohen bool restore; 951a86b377SEli Cohen }; 961a86b377SEli Cohen 971a86b377SEli Cohen struct mlx5_vdpa_virtqueue { 981a86b377SEli Cohen bool ready; 991a86b377SEli Cohen u64 desc_addr; 1001a86b377SEli Cohen u64 device_addr; 1011a86b377SEli Cohen u64 driver_addr; 1021a86b377SEli Cohen u32 num_ent; 1031a86b377SEli Cohen struct vdpa_callback event_cb; 1041a86b377SEli Cohen 1051a86b377SEli Cohen /* Resources for implementing the notification channel from the device 1061a86b377SEli Cohen * to the driver. fwqp is the firmware end of an RC connection; the 1071a86b377SEli Cohen * other end is vqqp used by the driver. cq is is where completions are 1081a86b377SEli Cohen * reported. 1091a86b377SEli Cohen */ 1101a86b377SEli Cohen struct mlx5_vdpa_cq cq; 1111a86b377SEli Cohen struct mlx5_vdpa_qp fwqp; 1121a86b377SEli Cohen struct mlx5_vdpa_qp vqqp; 1131a86b377SEli Cohen 1141a86b377SEli Cohen /* umem resources are required for the virtqueue operation. They're use 1151a86b377SEli Cohen * is internal and they must be provided by the driver. 1161a86b377SEli Cohen */ 1171a86b377SEli Cohen struct mlx5_vdpa_umem umem1; 1181a86b377SEli Cohen struct mlx5_vdpa_umem umem2; 1191a86b377SEli Cohen struct mlx5_vdpa_umem umem3; 1201a86b377SEli Cohen 1211a86b377SEli Cohen bool initialized; 1221a86b377SEli Cohen int index; 1231a86b377SEli Cohen u32 virtq_id; 1241a86b377SEli Cohen struct mlx5_vdpa_net *ndev; 1251a86b377SEli Cohen u16 avail_idx; 126b35ccebeSEli Cohen u16 used_idx; 1271a86b377SEli Cohen int fw_state; 1281a86b377SEli Cohen 1291a86b377SEli Cohen /* keep last in the struct */ 1301a86b377SEli Cohen struct mlx5_vq_restore_info ri; 1311a86b377SEli Cohen }; 1321a86b377SEli Cohen 1331a86b377SEli Cohen /* We will remove this limitation once mlx5_vdpa_alloc_resources() 1341a86b377SEli Cohen * provides for driver space allocation 1351a86b377SEli Cohen */ 1361a86b377SEli Cohen #define MLX5_MAX_SUPPORTED_VQS 16 1371a86b377SEli Cohen 1381a86b377SEli Cohen struct mlx5_vdpa_net { 1391a86b377SEli Cohen struct mlx5_vdpa_dev mvdev; 1401a86b377SEli Cohen struct mlx5_vdpa_net_resources res; 1411a86b377SEli Cohen struct virtio_net_config config; 1421a86b377SEli Cohen struct mlx5_vdpa_virtqueue vqs[MLX5_MAX_SUPPORTED_VQS]; 1431a86b377SEli Cohen 1441a86b377SEli Cohen /* Serialize vq resources creation and destruction. This is required 1451a86b377SEli Cohen * since memory map might change and we need to destroy and create 1461a86b377SEli Cohen * resources while driver in operational. 1471a86b377SEli Cohen */ 1481a86b377SEli Cohen struct mutex reslock; 1491a86b377SEli Cohen struct mlx5_flow_table *rxft; 1501a86b377SEli Cohen struct mlx5_fc *rx_counter; 1511a86b377SEli Cohen struct mlx5_flow_handle *rx_rule; 1521a86b377SEli Cohen bool setup; 1538a7c3213SMichael S. Tsirkin u16 mtu; 1541a86b377SEli Cohen }; 1551a86b377SEli Cohen 1561a86b377SEli Cohen static void free_resources(struct mlx5_vdpa_net *ndev); 1571a86b377SEli Cohen static void init_mvqs(struct mlx5_vdpa_net *ndev); 1581a86b377SEli Cohen static int setup_driver(struct mlx5_vdpa_net *ndev); 1591a86b377SEli Cohen static void teardown_driver(struct mlx5_vdpa_net *ndev); 1601a86b377SEli Cohen 1611a86b377SEli Cohen static bool mlx5_vdpa_debug; 1621a86b377SEli Cohen 1631a86b377SEli Cohen #define MLX5_LOG_VIO_FLAG(_feature) \ 1641a86b377SEli Cohen do { \ 165cbb52359SNathan Chancellor if (features & BIT_ULL(_feature)) \ 1661a86b377SEli Cohen mlx5_vdpa_info(mvdev, "%s\n", #_feature); \ 1671a86b377SEli Cohen } while (0) 1681a86b377SEli Cohen 1691a86b377SEli Cohen #define MLX5_LOG_VIO_STAT(_status) \ 1701a86b377SEli Cohen do { \ 1711a86b377SEli Cohen if (status & (_status)) \ 1721a86b377SEli Cohen mlx5_vdpa_info(mvdev, "%s\n", #_status); \ 1731a86b377SEli Cohen } while (0) 1741a86b377SEli Cohen 17574c9729dSLeon Romanovsky static inline u32 mlx5_vdpa_max_qps(int max_vqs) 17674c9729dSLeon Romanovsky { 17774c9729dSLeon Romanovsky return max_vqs / 2; 17874c9729dSLeon Romanovsky } 17974c9729dSLeon Romanovsky 1801a86b377SEli Cohen static void print_status(struct mlx5_vdpa_dev *mvdev, u8 status, bool set) 1811a86b377SEli Cohen { 1821a86b377SEli Cohen if (status & ~VALID_STATUS_MASK) 1831a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "Warning: there are invalid status bits 0x%x\n", 1841a86b377SEli Cohen status & ~VALID_STATUS_MASK); 1851a86b377SEli Cohen 1861a86b377SEli Cohen if (!mlx5_vdpa_debug) 1871a86b377SEli Cohen return; 1881a86b377SEli Cohen 1891a86b377SEli Cohen mlx5_vdpa_info(mvdev, "driver status %s", set ? "set" : "get"); 1901a86b377SEli Cohen if (set && !status) { 1911a86b377SEli Cohen mlx5_vdpa_info(mvdev, "driver resets the device\n"); 1921a86b377SEli Cohen return; 1931a86b377SEli Cohen } 1941a86b377SEli Cohen 1951a86b377SEli Cohen MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_ACKNOWLEDGE); 1961a86b377SEli Cohen MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_DRIVER); 1971a86b377SEli Cohen MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_DRIVER_OK); 1981a86b377SEli Cohen MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_FEATURES_OK); 1991a86b377SEli Cohen MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_NEEDS_RESET); 2001a86b377SEli Cohen MLX5_LOG_VIO_STAT(VIRTIO_CONFIG_S_FAILED); 2011a86b377SEli Cohen } 2021a86b377SEli Cohen 2031a86b377SEli Cohen static void print_features(struct mlx5_vdpa_dev *mvdev, u64 features, bool set) 2041a86b377SEli Cohen { 2051a86b377SEli Cohen if (features & ~VALID_FEATURES_MASK) 2061a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "There are invalid feature bits 0x%llx\n", 2071a86b377SEli Cohen features & ~VALID_FEATURES_MASK); 2081a86b377SEli Cohen 2091a86b377SEli Cohen if (!mlx5_vdpa_debug) 2101a86b377SEli Cohen return; 2111a86b377SEli Cohen 2121a86b377SEli Cohen mlx5_vdpa_info(mvdev, "driver %s feature bits:\n", set ? "sets" : "reads"); 2131a86b377SEli Cohen if (!features) 2141a86b377SEli Cohen mlx5_vdpa_info(mvdev, "all feature bits are cleared\n"); 2151a86b377SEli Cohen 2161a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CSUM); 2171a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_CSUM); 2181a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS); 2191a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MTU); 2201a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MAC); 2211a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_TSO4); 2221a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_TSO6); 2231a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_ECN); 2241a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_UFO); 2251a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_TSO4); 2261a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_TSO6); 2271a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_ECN); 2281a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HOST_UFO); 2291a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MRG_RXBUF); 2301a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_STATUS); 2311a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_VQ); 2321a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_RX); 2331a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_VLAN); 2341a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_RX_EXTRA); 2351a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_GUEST_ANNOUNCE); 2361a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_MQ); 2371a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_CTRL_MAC_ADDR); 2381a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_HASH_REPORT); 2391a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_RSS); 2401a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_RSC_EXT); 2411a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_STANDBY); 2421a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_NET_F_SPEED_DUPLEX); 2431a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_NOTIFY_ON_EMPTY); 2441a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_ANY_LAYOUT); 2451a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_VERSION_1); 2461a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_ACCESS_PLATFORM); 2471a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_RING_PACKED); 2481a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_ORDER_PLATFORM); 2491a86b377SEli Cohen MLX5_LOG_VIO_FLAG(VIRTIO_F_SR_IOV); 2501a86b377SEli Cohen } 2511a86b377SEli Cohen 2521a86b377SEli Cohen static int create_tis(struct mlx5_vdpa_net *ndev) 2531a86b377SEli Cohen { 2541a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = &ndev->mvdev; 2551a86b377SEli Cohen u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {}; 2561a86b377SEli Cohen void *tisc; 2571a86b377SEli Cohen int err; 2581a86b377SEli Cohen 2591a86b377SEli Cohen tisc = MLX5_ADDR_OF(create_tis_in, in, ctx); 2601a86b377SEli Cohen MLX5_SET(tisc, tisc, transport_domain, ndev->res.tdn); 2611a86b377SEli Cohen err = mlx5_vdpa_create_tis(mvdev, in, &ndev->res.tisn); 2621a86b377SEli Cohen if (err) 2631a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "create TIS (%d)\n", err); 2641a86b377SEli Cohen 2651a86b377SEli Cohen return err; 2661a86b377SEli Cohen } 2671a86b377SEli Cohen 2681a86b377SEli Cohen static void destroy_tis(struct mlx5_vdpa_net *ndev) 2691a86b377SEli Cohen { 2701a86b377SEli Cohen mlx5_vdpa_destroy_tis(&ndev->mvdev, ndev->res.tisn); 2711a86b377SEli Cohen } 2721a86b377SEli Cohen 2731a86b377SEli Cohen #define MLX5_VDPA_CQE_SIZE 64 2741a86b377SEli Cohen #define MLX5_VDPA_LOG_CQE_SIZE ilog2(MLX5_VDPA_CQE_SIZE) 2751a86b377SEli Cohen 2761a86b377SEli Cohen static int cq_frag_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_cq_buf *buf, int nent) 2771a86b377SEli Cohen { 2781a86b377SEli Cohen struct mlx5_frag_buf *frag_buf = &buf->frag_buf; 2791a86b377SEli Cohen u8 log_wq_stride = MLX5_VDPA_LOG_CQE_SIZE; 2801a86b377SEli Cohen u8 log_wq_sz = MLX5_VDPA_LOG_CQE_SIZE; 2811a86b377SEli Cohen int err; 2821a86b377SEli Cohen 2831a86b377SEli Cohen err = mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, nent * MLX5_VDPA_CQE_SIZE, frag_buf, 2841a86b377SEli Cohen ndev->mvdev.mdev->priv.numa_node); 2851a86b377SEli Cohen if (err) 2861a86b377SEli Cohen return err; 2871a86b377SEli Cohen 2881a86b377SEli Cohen mlx5_init_fbc(frag_buf->frags, log_wq_stride, log_wq_sz, &buf->fbc); 2891a86b377SEli Cohen 2901a86b377SEli Cohen buf->cqe_size = MLX5_VDPA_CQE_SIZE; 2911a86b377SEli Cohen buf->nent = nent; 2921a86b377SEli Cohen 2931a86b377SEli Cohen return 0; 2941a86b377SEli Cohen } 2951a86b377SEli Cohen 2961a86b377SEli Cohen static int umem_frag_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_umem *umem, int size) 2971a86b377SEli Cohen { 2981a86b377SEli Cohen struct mlx5_frag_buf *frag_buf = &umem->frag_buf; 2991a86b377SEli Cohen 3001a86b377SEli Cohen return mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, size, frag_buf, 3011a86b377SEli Cohen ndev->mvdev.mdev->priv.numa_node); 3021a86b377SEli Cohen } 3031a86b377SEli Cohen 3041a86b377SEli Cohen static void cq_frag_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_cq_buf *buf) 3051a86b377SEli Cohen { 3061a86b377SEli Cohen mlx5_frag_buf_free(ndev->mvdev.mdev, &buf->frag_buf); 3071a86b377SEli Cohen } 3081a86b377SEli Cohen 3091a86b377SEli Cohen static void *get_cqe(struct mlx5_vdpa_cq *vcq, int n) 3101a86b377SEli Cohen { 3111a86b377SEli Cohen return mlx5_frag_buf_get_wqe(&vcq->buf.fbc, n); 3121a86b377SEli Cohen } 3131a86b377SEli Cohen 3141a86b377SEli Cohen static void cq_frag_buf_init(struct mlx5_vdpa_cq *vcq, struct mlx5_vdpa_cq_buf *buf) 3151a86b377SEli Cohen { 3161a86b377SEli Cohen struct mlx5_cqe64 *cqe64; 3171a86b377SEli Cohen void *cqe; 3181a86b377SEli Cohen int i; 3191a86b377SEli Cohen 3201a86b377SEli Cohen for (i = 0; i < buf->nent; i++) { 3211a86b377SEli Cohen cqe = get_cqe(vcq, i); 3221a86b377SEli Cohen cqe64 = cqe; 3231a86b377SEli Cohen cqe64->op_own = MLX5_CQE_INVALID << 4; 3241a86b377SEli Cohen } 3251a86b377SEli Cohen } 3261a86b377SEli Cohen 3271a86b377SEli Cohen static void *get_sw_cqe(struct mlx5_vdpa_cq *cq, int n) 3281a86b377SEli Cohen { 3291a86b377SEli Cohen struct mlx5_cqe64 *cqe64 = get_cqe(cq, n & (cq->cqe - 1)); 3301a86b377SEli Cohen 3311a86b377SEli Cohen if (likely(get_cqe_opcode(cqe64) != MLX5_CQE_INVALID) && 3321a86b377SEli Cohen !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & cq->cqe))) 3331a86b377SEli Cohen return cqe64; 3341a86b377SEli Cohen 3351a86b377SEli Cohen return NULL; 3361a86b377SEli Cohen } 3371a86b377SEli Cohen 3381a86b377SEli Cohen static void rx_post(struct mlx5_vdpa_qp *vqp, int n) 3391a86b377SEli Cohen { 3401a86b377SEli Cohen vqp->head += n; 3411a86b377SEli Cohen vqp->db.db[0] = cpu_to_be32(vqp->head); 3421a86b377SEli Cohen } 3431a86b377SEli Cohen 3441a86b377SEli Cohen static void qp_prepare(struct mlx5_vdpa_net *ndev, bool fw, void *in, 3451a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq, u32 num_ent) 3461a86b377SEli Cohen { 3471a86b377SEli Cohen struct mlx5_vdpa_qp *vqp; 3481a86b377SEli Cohen __be64 *pas; 3491a86b377SEli Cohen void *qpc; 3501a86b377SEli Cohen 3511a86b377SEli Cohen vqp = fw ? &mvq->fwqp : &mvq->vqqp; 3521a86b377SEli Cohen MLX5_SET(create_qp_in, in, uid, ndev->mvdev.res.uid); 3531a86b377SEli Cohen qpc = MLX5_ADDR_OF(create_qp_in, in, qpc); 3541a86b377SEli Cohen if (vqp->fw) { 3551a86b377SEli Cohen /* Firmware QP is allocated by the driver for the firmware's 3561a86b377SEli Cohen * use so we can skip part of the params as they will be chosen by firmware 3571a86b377SEli Cohen */ 3581a86b377SEli Cohen qpc = MLX5_ADDR_OF(create_qp_in, in, qpc); 3591a86b377SEli Cohen MLX5_SET(qpc, qpc, rq_type, MLX5_ZERO_LEN_RQ); 3601a86b377SEli Cohen MLX5_SET(qpc, qpc, no_sq, 1); 3611a86b377SEli Cohen return; 3621a86b377SEli Cohen } 3631a86b377SEli Cohen 3641a86b377SEli Cohen MLX5_SET(qpc, qpc, st, MLX5_QP_ST_RC); 3651a86b377SEli Cohen MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED); 3661a86b377SEli Cohen MLX5_SET(qpc, qpc, pd, ndev->mvdev.res.pdn); 3671a86b377SEli Cohen MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES); 3681a86b377SEli Cohen MLX5_SET(qpc, qpc, uar_page, ndev->mvdev.res.uar->index); 3691a86b377SEli Cohen MLX5_SET(qpc, qpc, log_page_size, vqp->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT); 3701a86b377SEli Cohen MLX5_SET(qpc, qpc, no_sq, 1); 3711a86b377SEli Cohen MLX5_SET(qpc, qpc, cqn_rcv, mvq->cq.mcq.cqn); 3721a86b377SEli Cohen MLX5_SET(qpc, qpc, log_rq_size, ilog2(num_ent)); 3731a86b377SEli Cohen MLX5_SET(qpc, qpc, rq_type, MLX5_NON_ZERO_RQ); 3741a86b377SEli Cohen pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, in, pas); 3751a86b377SEli Cohen mlx5_fill_page_frag_array(&vqp->frag_buf, pas); 3761a86b377SEli Cohen } 3771a86b377SEli Cohen 3781a86b377SEli Cohen static int rq_buf_alloc(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp, u32 num_ent) 3791a86b377SEli Cohen { 3801a86b377SEli Cohen return mlx5_frag_buf_alloc_node(ndev->mvdev.mdev, 3811a86b377SEli Cohen num_ent * sizeof(struct mlx5_wqe_data_seg), &vqp->frag_buf, 3821a86b377SEli Cohen ndev->mvdev.mdev->priv.numa_node); 3831a86b377SEli Cohen } 3841a86b377SEli Cohen 3851a86b377SEli Cohen static void rq_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp) 3861a86b377SEli Cohen { 3871a86b377SEli Cohen mlx5_frag_buf_free(ndev->mvdev.mdev, &vqp->frag_buf); 3881a86b377SEli Cohen } 3891a86b377SEli Cohen 3901a86b377SEli Cohen static int qp_create(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, 3911a86b377SEli Cohen struct mlx5_vdpa_qp *vqp) 3921a86b377SEli Cohen { 3931a86b377SEli Cohen struct mlx5_core_dev *mdev = ndev->mvdev.mdev; 3941a86b377SEli Cohen int inlen = MLX5_ST_SZ_BYTES(create_qp_in); 3951a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {}; 3961a86b377SEli Cohen void *qpc; 3971a86b377SEli Cohen void *in; 3981a86b377SEli Cohen int err; 3991a86b377SEli Cohen 4001a86b377SEli Cohen if (!vqp->fw) { 4011a86b377SEli Cohen vqp = &mvq->vqqp; 4021a86b377SEli Cohen err = rq_buf_alloc(ndev, vqp, mvq->num_ent); 4031a86b377SEli Cohen if (err) 4041a86b377SEli Cohen return err; 4051a86b377SEli Cohen 4061a86b377SEli Cohen err = mlx5_db_alloc(ndev->mvdev.mdev, &vqp->db); 4071a86b377SEli Cohen if (err) 4081a86b377SEli Cohen goto err_db; 4091a86b377SEli Cohen inlen += vqp->frag_buf.npages * sizeof(__be64); 4101a86b377SEli Cohen } 4111a86b377SEli Cohen 4121a86b377SEli Cohen in = kzalloc(inlen, GFP_KERNEL); 4131a86b377SEli Cohen if (!in) { 4141a86b377SEli Cohen err = -ENOMEM; 4151a86b377SEli Cohen goto err_kzalloc; 4161a86b377SEli Cohen } 4171a86b377SEli Cohen 4181a86b377SEli Cohen qp_prepare(ndev, vqp->fw, in, mvq, mvq->num_ent); 4191a86b377SEli Cohen qpc = MLX5_ADDR_OF(create_qp_in, in, qpc); 4201a86b377SEli Cohen MLX5_SET(qpc, qpc, st, MLX5_QP_ST_RC); 4211a86b377SEli Cohen MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED); 4221a86b377SEli Cohen MLX5_SET(qpc, qpc, pd, ndev->mvdev.res.pdn); 4231a86b377SEli Cohen MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES); 4241a86b377SEli Cohen if (!vqp->fw) 4251a86b377SEli Cohen MLX5_SET64(qpc, qpc, dbr_addr, vqp->db.dma); 4261a86b377SEli Cohen MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP); 4271a86b377SEli Cohen err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out)); 4281a86b377SEli Cohen kfree(in); 4291a86b377SEli Cohen if (err) 4301a86b377SEli Cohen goto err_kzalloc; 4311a86b377SEli Cohen 4321a86b377SEli Cohen vqp->mqp.uid = ndev->mvdev.res.uid; 4331a86b377SEli Cohen vqp->mqp.qpn = MLX5_GET(create_qp_out, out, qpn); 4341a86b377SEli Cohen 4351a86b377SEli Cohen if (!vqp->fw) 4361a86b377SEli Cohen rx_post(vqp, mvq->num_ent); 4371a86b377SEli Cohen 4381a86b377SEli Cohen return 0; 4391a86b377SEli Cohen 4401a86b377SEli Cohen err_kzalloc: 4411a86b377SEli Cohen if (!vqp->fw) 4421a86b377SEli Cohen mlx5_db_free(ndev->mvdev.mdev, &vqp->db); 4431a86b377SEli Cohen err_db: 4441a86b377SEli Cohen if (!vqp->fw) 4451a86b377SEli Cohen rq_buf_free(ndev, vqp); 4461a86b377SEli Cohen 4471a86b377SEli Cohen return err; 4481a86b377SEli Cohen } 4491a86b377SEli Cohen 4501a86b377SEli Cohen static void qp_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_qp *vqp) 4511a86b377SEli Cohen { 4521a86b377SEli Cohen u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {}; 4531a86b377SEli Cohen 4541a86b377SEli Cohen MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP); 4551a86b377SEli Cohen MLX5_SET(destroy_qp_in, in, qpn, vqp->mqp.qpn); 4561a86b377SEli Cohen MLX5_SET(destroy_qp_in, in, uid, ndev->mvdev.res.uid); 4571a86b377SEli Cohen if (mlx5_cmd_exec_in(ndev->mvdev.mdev, destroy_qp, in)) 4581a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "destroy qp 0x%x\n", vqp->mqp.qpn); 4591a86b377SEli Cohen if (!vqp->fw) { 4601a86b377SEli Cohen mlx5_db_free(ndev->mvdev.mdev, &vqp->db); 4611a86b377SEli Cohen rq_buf_free(ndev, vqp); 4621a86b377SEli Cohen } 4631a86b377SEli Cohen } 4641a86b377SEli Cohen 4651a86b377SEli Cohen static void *next_cqe_sw(struct mlx5_vdpa_cq *cq) 4661a86b377SEli Cohen { 4671a86b377SEli Cohen return get_sw_cqe(cq, cq->mcq.cons_index); 4681a86b377SEli Cohen } 4691a86b377SEli Cohen 4701a86b377SEli Cohen static int mlx5_vdpa_poll_one(struct mlx5_vdpa_cq *vcq) 4711a86b377SEli Cohen { 4721a86b377SEli Cohen struct mlx5_cqe64 *cqe64; 4731a86b377SEli Cohen 4741a86b377SEli Cohen cqe64 = next_cqe_sw(vcq); 4751a86b377SEli Cohen if (!cqe64) 4761a86b377SEli Cohen return -EAGAIN; 4771a86b377SEli Cohen 4781a86b377SEli Cohen vcq->mcq.cons_index++; 4791a86b377SEli Cohen return 0; 4801a86b377SEli Cohen } 4811a86b377SEli Cohen 4821a86b377SEli Cohen static void mlx5_vdpa_handle_completions(struct mlx5_vdpa_virtqueue *mvq, int num) 4831a86b377SEli Cohen { 4841a86b377SEli Cohen mlx5_cq_set_ci(&mvq->cq.mcq); 48583ef73b2SEli Cohen 48683ef73b2SEli Cohen /* make sure CQ cosumer update is visible to the hardware before updating 48783ef73b2SEli Cohen * RX doorbell record. 48883ef73b2SEli Cohen */ 48983ef73b2SEli Cohen dma_wmb(); 4901a86b377SEli Cohen rx_post(&mvq->vqqp, num); 4911a86b377SEli Cohen if (mvq->event_cb.callback) 4921a86b377SEli Cohen mvq->event_cb.callback(mvq->event_cb.private); 4931a86b377SEli Cohen } 4941a86b377SEli Cohen 4951a86b377SEli Cohen static void mlx5_vdpa_cq_comp(struct mlx5_core_cq *mcq, struct mlx5_eqe *eqe) 4961a86b377SEli Cohen { 4971a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = container_of(mcq, struct mlx5_vdpa_virtqueue, cq.mcq); 4981a86b377SEli Cohen struct mlx5_vdpa_net *ndev = mvq->ndev; 4991a86b377SEli Cohen void __iomem *uar_page = ndev->mvdev.res.uar->map; 5001a86b377SEli Cohen int num = 0; 5011a86b377SEli Cohen 5021a86b377SEli Cohen while (!mlx5_vdpa_poll_one(&mvq->cq)) { 5031a86b377SEli Cohen num++; 5041a86b377SEli Cohen if (num > mvq->num_ent / 2) { 5051a86b377SEli Cohen /* If completions keep coming while we poll, we want to 5061a86b377SEli Cohen * let the hardware know that we consumed them by 5071a86b377SEli Cohen * updating the doorbell record. We also let vdpa core 5081a86b377SEli Cohen * know about this so it passes it on the virtio driver 5091a86b377SEli Cohen * on the guest. 5101a86b377SEli Cohen */ 5111a86b377SEli Cohen mlx5_vdpa_handle_completions(mvq, num); 5121a86b377SEli Cohen num = 0; 5131a86b377SEli Cohen } 5141a86b377SEli Cohen } 5151a86b377SEli Cohen 5161a86b377SEli Cohen if (num) 5171a86b377SEli Cohen mlx5_vdpa_handle_completions(mvq, num); 5181a86b377SEli Cohen 5191a86b377SEli Cohen mlx5_cq_arm(&mvq->cq.mcq, MLX5_CQ_DB_REQ_NOT, uar_page, mvq->cq.mcq.cons_index); 5201a86b377SEli Cohen } 5211a86b377SEli Cohen 5221a86b377SEli Cohen static int cq_create(struct mlx5_vdpa_net *ndev, u16 idx, u32 num_ent) 5231a86b377SEli Cohen { 5241a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 5251a86b377SEli Cohen struct mlx5_core_dev *mdev = ndev->mvdev.mdev; 5261a86b377SEli Cohen void __iomem *uar_page = ndev->mvdev.res.uar->map; 5271a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(create_cq_out)]; 5281a86b377SEli Cohen struct mlx5_vdpa_cq *vcq = &mvq->cq; 5291a86b377SEli Cohen __be64 *pas; 5301a86b377SEli Cohen int inlen; 5311a86b377SEli Cohen void *cqc; 5321a86b377SEli Cohen void *in; 5331a86b377SEli Cohen int err; 5341a86b377SEli Cohen int eqn; 5351a86b377SEli Cohen 5361a86b377SEli Cohen err = mlx5_db_alloc(mdev, &vcq->db); 5371a86b377SEli Cohen if (err) 5381a86b377SEli Cohen return err; 5391a86b377SEli Cohen 5401a86b377SEli Cohen vcq->mcq.set_ci_db = vcq->db.db; 5411a86b377SEli Cohen vcq->mcq.arm_db = vcq->db.db + 1; 5421a86b377SEli Cohen vcq->mcq.cqe_sz = 64; 5431a86b377SEli Cohen 5441a86b377SEli Cohen err = cq_frag_buf_alloc(ndev, &vcq->buf, num_ent); 5451a86b377SEli Cohen if (err) 5461a86b377SEli Cohen goto err_db; 5471a86b377SEli Cohen 5481a86b377SEli Cohen cq_frag_buf_init(vcq, &vcq->buf); 5491a86b377SEli Cohen 5501a86b377SEli Cohen inlen = MLX5_ST_SZ_BYTES(create_cq_in) + 5511a86b377SEli Cohen MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * vcq->buf.frag_buf.npages; 5521a86b377SEli Cohen in = kzalloc(inlen, GFP_KERNEL); 5531a86b377SEli Cohen if (!in) { 5541a86b377SEli Cohen err = -ENOMEM; 5551a86b377SEli Cohen goto err_vzalloc; 5561a86b377SEli Cohen } 5571a86b377SEli Cohen 5581a86b377SEli Cohen MLX5_SET(create_cq_in, in, uid, ndev->mvdev.res.uid); 5591a86b377SEli Cohen pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas); 5601a86b377SEli Cohen mlx5_fill_page_frag_array(&vcq->buf.frag_buf, pas); 5611a86b377SEli Cohen 5621a86b377SEli Cohen cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context); 5631a86b377SEli Cohen MLX5_SET(cqc, cqc, log_page_size, vcq->buf.frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT); 5641a86b377SEli Cohen 5651a86b377SEli Cohen /* Use vector 0 by default. Consider adding code to choose least used 5661a86b377SEli Cohen * vector. 5671a86b377SEli Cohen */ 568*563476aeSShay Drory err = mlx5_vector2eqn(mdev, 0, &eqn); 5691a86b377SEli Cohen if (err) 5701a86b377SEli Cohen goto err_vec; 5711a86b377SEli Cohen 5721a86b377SEli Cohen cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context); 5731a86b377SEli Cohen MLX5_SET(cqc, cqc, log_cq_size, ilog2(num_ent)); 5741a86b377SEli Cohen MLX5_SET(cqc, cqc, uar_page, ndev->mvdev.res.uar->index); 5751a86b377SEli Cohen MLX5_SET(cqc, cqc, c_eqn, eqn); 5761a86b377SEli Cohen MLX5_SET64(cqc, cqc, dbr_addr, vcq->db.dma); 5771a86b377SEli Cohen 5781a86b377SEli Cohen err = mlx5_core_create_cq(mdev, &vcq->mcq, in, inlen, out, sizeof(out)); 5791a86b377SEli Cohen if (err) 5801a86b377SEli Cohen goto err_vec; 5811a86b377SEli Cohen 5821a86b377SEli Cohen vcq->mcq.comp = mlx5_vdpa_cq_comp; 5831a86b377SEli Cohen vcq->cqe = num_ent; 5841a86b377SEli Cohen vcq->mcq.set_ci_db = vcq->db.db; 5851a86b377SEli Cohen vcq->mcq.arm_db = vcq->db.db + 1; 5861a86b377SEli Cohen mlx5_cq_arm(&mvq->cq.mcq, MLX5_CQ_DB_REQ_NOT, uar_page, mvq->cq.mcq.cons_index); 5871a86b377SEli Cohen kfree(in); 5881a86b377SEli Cohen return 0; 5891a86b377SEli Cohen 5901a86b377SEli Cohen err_vec: 5911a86b377SEli Cohen kfree(in); 5921a86b377SEli Cohen err_vzalloc: 5931a86b377SEli Cohen cq_frag_buf_free(ndev, &vcq->buf); 5941a86b377SEli Cohen err_db: 5951a86b377SEli Cohen mlx5_db_free(ndev->mvdev.mdev, &vcq->db); 5961a86b377SEli Cohen return err; 5971a86b377SEli Cohen } 5981a86b377SEli Cohen 5991a86b377SEli Cohen static void cq_destroy(struct mlx5_vdpa_net *ndev, u16 idx) 6001a86b377SEli Cohen { 6011a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 6021a86b377SEli Cohen struct mlx5_core_dev *mdev = ndev->mvdev.mdev; 6031a86b377SEli Cohen struct mlx5_vdpa_cq *vcq = &mvq->cq; 6041a86b377SEli Cohen 6051a86b377SEli Cohen if (mlx5_core_destroy_cq(mdev, &vcq->mcq)) { 6061a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "destroy CQ 0x%x\n", vcq->mcq.cqn); 6071a86b377SEli Cohen return; 6081a86b377SEli Cohen } 6091a86b377SEli Cohen cq_frag_buf_free(ndev, &vcq->buf); 6101a86b377SEli Cohen mlx5_db_free(ndev->mvdev.mdev, &vcq->db); 6111a86b377SEli Cohen } 6121a86b377SEli Cohen 61371ab6a7cSEli Cohen static void set_umem_size(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num, 6141a86b377SEli Cohen struct mlx5_vdpa_umem **umemp) 6151a86b377SEli Cohen { 6161a86b377SEli Cohen struct mlx5_core_dev *mdev = ndev->mvdev.mdev; 6171a86b377SEli Cohen int p_a; 6181a86b377SEli Cohen int p_b; 6191a86b377SEli Cohen 6201a86b377SEli Cohen switch (num) { 6211a86b377SEli Cohen case 1: 6221a86b377SEli Cohen p_a = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_1_buffer_param_a); 6231a86b377SEli Cohen p_b = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_1_buffer_param_b); 6241a86b377SEli Cohen *umemp = &mvq->umem1; 6251a86b377SEli Cohen break; 6261a86b377SEli Cohen case 2: 6271a86b377SEli Cohen p_a = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_2_buffer_param_a); 6281a86b377SEli Cohen p_b = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_2_buffer_param_b); 6291a86b377SEli Cohen *umemp = &mvq->umem2; 6301a86b377SEli Cohen break; 6311a86b377SEli Cohen case 3: 6321a86b377SEli Cohen p_a = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_3_buffer_param_a); 6331a86b377SEli Cohen p_b = MLX5_CAP_DEV_VDPA_EMULATION(mdev, umem_3_buffer_param_b); 6341a86b377SEli Cohen *umemp = &mvq->umem3; 6351a86b377SEli Cohen break; 6361a86b377SEli Cohen } 63771ab6a7cSEli Cohen (*umemp)->size = p_a * mvq->num_ent + p_b; 6381a86b377SEli Cohen } 6391a86b377SEli Cohen 6401a86b377SEli Cohen static void umem_frag_buf_free(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_umem *umem) 6411a86b377SEli Cohen { 6421a86b377SEli Cohen mlx5_frag_buf_free(ndev->mvdev.mdev, &umem->frag_buf); 6431a86b377SEli Cohen } 6441a86b377SEli Cohen 6451a86b377SEli Cohen static int create_umem(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num) 6461a86b377SEli Cohen { 6471a86b377SEli Cohen int inlen; 6481a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(create_umem_out)] = {}; 6491a86b377SEli Cohen void *um; 6501a86b377SEli Cohen void *in; 6511a86b377SEli Cohen int err; 6521a86b377SEli Cohen __be64 *pas; 6531a86b377SEli Cohen struct mlx5_vdpa_umem *umem; 6541a86b377SEli Cohen 65571ab6a7cSEli Cohen set_umem_size(ndev, mvq, num, &umem); 65671ab6a7cSEli Cohen err = umem_frag_buf_alloc(ndev, umem, umem->size); 6571a86b377SEli Cohen if (err) 6581a86b377SEli Cohen return err; 6591a86b377SEli Cohen 6601a86b377SEli Cohen inlen = MLX5_ST_SZ_BYTES(create_umem_in) + MLX5_ST_SZ_BYTES(mtt) * umem->frag_buf.npages; 6611a86b377SEli Cohen 6621a86b377SEli Cohen in = kzalloc(inlen, GFP_KERNEL); 6631a86b377SEli Cohen if (!in) { 6641a86b377SEli Cohen err = -ENOMEM; 6651a86b377SEli Cohen goto err_in; 6661a86b377SEli Cohen } 6671a86b377SEli Cohen 6681a86b377SEli Cohen MLX5_SET(create_umem_in, in, opcode, MLX5_CMD_OP_CREATE_UMEM); 6691a86b377SEli Cohen MLX5_SET(create_umem_in, in, uid, ndev->mvdev.res.uid); 6701a86b377SEli Cohen um = MLX5_ADDR_OF(create_umem_in, in, umem); 6711a86b377SEli Cohen MLX5_SET(umem, um, log_page_size, umem->frag_buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT); 6721a86b377SEli Cohen MLX5_SET64(umem, um, num_of_mtt, umem->frag_buf.npages); 6731a86b377SEli Cohen 6741a86b377SEli Cohen pas = (__be64 *)MLX5_ADDR_OF(umem, um, mtt[0]); 6751a86b377SEli Cohen mlx5_fill_page_frag_array_perm(&umem->frag_buf, pas, MLX5_MTT_PERM_RW); 6761a86b377SEli Cohen 6771a86b377SEli Cohen err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out)); 6781a86b377SEli Cohen if (err) { 6791a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "create umem(%d)\n", err); 6801a86b377SEli Cohen goto err_cmd; 6811a86b377SEli Cohen } 6821a86b377SEli Cohen 6831a86b377SEli Cohen kfree(in); 6841a86b377SEli Cohen umem->id = MLX5_GET(create_umem_out, out, umem_id); 6851a86b377SEli Cohen 6861a86b377SEli Cohen return 0; 6871a86b377SEli Cohen 6881a86b377SEli Cohen err_cmd: 6891a86b377SEli Cohen kfree(in); 6901a86b377SEli Cohen err_in: 6911a86b377SEli Cohen umem_frag_buf_free(ndev, umem); 6921a86b377SEli Cohen return err; 6931a86b377SEli Cohen } 6941a86b377SEli Cohen 6951a86b377SEli Cohen static void umem_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int num) 6961a86b377SEli Cohen { 6971a86b377SEli Cohen u32 in[MLX5_ST_SZ_DW(destroy_umem_in)] = {}; 6981a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(destroy_umem_out)] = {}; 6991a86b377SEli Cohen struct mlx5_vdpa_umem *umem; 7001a86b377SEli Cohen 7011a86b377SEli Cohen switch (num) { 7021a86b377SEli Cohen case 1: 7031a86b377SEli Cohen umem = &mvq->umem1; 7041a86b377SEli Cohen break; 7051a86b377SEli Cohen case 2: 7061a86b377SEli Cohen umem = &mvq->umem2; 7071a86b377SEli Cohen break; 7081a86b377SEli Cohen case 3: 7091a86b377SEli Cohen umem = &mvq->umem3; 7101a86b377SEli Cohen break; 7111a86b377SEli Cohen } 7121a86b377SEli Cohen 7131a86b377SEli Cohen MLX5_SET(destroy_umem_in, in, opcode, MLX5_CMD_OP_DESTROY_UMEM); 7141a86b377SEli Cohen MLX5_SET(destroy_umem_in, in, umem_id, umem->id); 7151a86b377SEli Cohen if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out))) 7161a86b377SEli Cohen return; 7171a86b377SEli Cohen 7181a86b377SEli Cohen umem_frag_buf_free(ndev, umem); 7191a86b377SEli Cohen } 7201a86b377SEli Cohen 7211a86b377SEli Cohen static int umems_create(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 7221a86b377SEli Cohen { 7231a86b377SEli Cohen int num; 7241a86b377SEli Cohen int err; 7251a86b377SEli Cohen 7261a86b377SEli Cohen for (num = 1; num <= 3; num++) { 7271a86b377SEli Cohen err = create_umem(ndev, mvq, num); 7281a86b377SEli Cohen if (err) 7291a86b377SEli Cohen goto err_umem; 7301a86b377SEli Cohen } 7311a86b377SEli Cohen return 0; 7321a86b377SEli Cohen 7331a86b377SEli Cohen err_umem: 7341a86b377SEli Cohen for (num--; num > 0; num--) 7351a86b377SEli Cohen umem_destroy(ndev, mvq, num); 7361a86b377SEli Cohen 7371a86b377SEli Cohen return err; 7381a86b377SEli Cohen } 7391a86b377SEli Cohen 7401a86b377SEli Cohen static void umems_destroy(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 7411a86b377SEli Cohen { 7421a86b377SEli Cohen int num; 7431a86b377SEli Cohen 7441a86b377SEli Cohen for (num = 3; num > 0; num--) 7451a86b377SEli Cohen umem_destroy(ndev, mvq, num); 7461a86b377SEli Cohen } 7471a86b377SEli Cohen 7481a86b377SEli Cohen static int get_queue_type(struct mlx5_vdpa_net *ndev) 7491a86b377SEli Cohen { 7501a86b377SEli Cohen u32 type_mask; 7511a86b377SEli Cohen 7521a86b377SEli Cohen type_mask = MLX5_CAP_DEV_VDPA_EMULATION(ndev->mvdev.mdev, virtio_queue_type); 7531a86b377SEli Cohen 7541a86b377SEli Cohen /* prefer split queue */ 7551a86b377SEli Cohen if (type_mask & MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_PACKED) 7561a86b377SEli Cohen return MLX5_VIRTIO_EMULATION_VIRTIO_QUEUE_TYPE_PACKED; 7571a86b377SEli Cohen 7581a86b377SEli Cohen WARN_ON(!(type_mask & MLX5_VIRTIO_EMULATION_CAP_VIRTIO_QUEUE_TYPE_SPLIT)); 7591a86b377SEli Cohen 7601a86b377SEli Cohen return MLX5_VIRTIO_EMULATION_VIRTIO_QUEUE_TYPE_SPLIT; 7611a86b377SEli Cohen } 7621a86b377SEli Cohen 7631a86b377SEli Cohen static bool vq_is_tx(u16 idx) 7641a86b377SEli Cohen { 7651a86b377SEli Cohen return idx % 2; 7661a86b377SEli Cohen } 7671a86b377SEli Cohen 7681a86b377SEli Cohen static u16 get_features_12_3(u64 features) 7691a86b377SEli Cohen { 770cbb52359SNathan Chancellor return (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_TSO4)) << 9) | 771cbb52359SNathan Chancellor (!!(features & BIT_ULL(VIRTIO_NET_F_HOST_TSO6)) << 8) | 772cbb52359SNathan Chancellor (!!(features & BIT_ULL(VIRTIO_NET_F_CSUM)) << 7) | 773cbb52359SNathan Chancellor (!!(features & BIT_ULL(VIRTIO_NET_F_GUEST_CSUM)) << 6); 7741a86b377SEli Cohen } 7751a86b377SEli Cohen 7761a86b377SEli Cohen static int create_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 7771a86b377SEli Cohen { 7781a86b377SEli Cohen int inlen = MLX5_ST_SZ_BYTES(create_virtio_net_q_in); 7791a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(create_virtio_net_q_out)] = {}; 7801a86b377SEli Cohen void *obj_context; 7811a86b377SEli Cohen void *cmd_hdr; 7821a86b377SEli Cohen void *vq_ctx; 7831a86b377SEli Cohen void *in; 7841a86b377SEli Cohen int err; 7851a86b377SEli Cohen 7861a86b377SEli Cohen err = umems_create(ndev, mvq); 7871a86b377SEli Cohen if (err) 7881a86b377SEli Cohen return err; 7891a86b377SEli Cohen 7901a86b377SEli Cohen in = kzalloc(inlen, GFP_KERNEL); 7911a86b377SEli Cohen if (!in) { 7921a86b377SEli Cohen err = -ENOMEM; 7931a86b377SEli Cohen goto err_alloc; 7941a86b377SEli Cohen } 7951a86b377SEli Cohen 7961a86b377SEli Cohen cmd_hdr = MLX5_ADDR_OF(create_virtio_net_q_in, in, general_obj_in_cmd_hdr); 7971a86b377SEli Cohen 7981a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT); 7991a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q); 8001a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid); 8011a86b377SEli Cohen 8021a86b377SEli Cohen obj_context = MLX5_ADDR_OF(create_virtio_net_q_in, in, obj_context); 8031a86b377SEli Cohen MLX5_SET(virtio_net_q_object, obj_context, hw_available_index, mvq->avail_idx); 804b35ccebeSEli Cohen MLX5_SET(virtio_net_q_object, obj_context, hw_used_index, mvq->used_idx); 8051a86b377SEli Cohen MLX5_SET(virtio_net_q_object, obj_context, queue_feature_bit_mask_12_3, 8061a86b377SEli Cohen get_features_12_3(ndev->mvdev.actual_features)); 8071a86b377SEli Cohen vq_ctx = MLX5_ADDR_OF(virtio_net_q_object, obj_context, virtio_q_context); 8081a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, virtio_q_type, get_queue_type(ndev)); 8091a86b377SEli Cohen 8101a86b377SEli Cohen if (vq_is_tx(mvq->index)) 8111a86b377SEli Cohen MLX5_SET(virtio_net_q_object, obj_context, tisn_or_qpn, ndev->res.tisn); 8121a86b377SEli Cohen 8131a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, event_mode, MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE); 8141a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, queue_index, mvq->index); 8151a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, event_qpn_or_msix, mvq->fwqp.mqp.qpn); 8161a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, queue_size, mvq->num_ent); 8171a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, virtio_version_1_0, 8184b454a82SEli Cohen !!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_F_VERSION_1))); 8191a86b377SEli Cohen MLX5_SET64(virtio_q, vq_ctx, desc_addr, mvq->desc_addr); 8201a86b377SEli Cohen MLX5_SET64(virtio_q, vq_ctx, used_addr, mvq->device_addr); 8211a86b377SEli Cohen MLX5_SET64(virtio_q, vq_ctx, available_addr, mvq->driver_addr); 8221a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, virtio_q_mkey, ndev->mvdev.mr.mkey.key); 8231a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, umem_1_id, mvq->umem1.id); 8241a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, umem_1_size, mvq->umem1.size); 8251a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, umem_2_id, mvq->umem2.id); 826e3011776SEli Cohen MLX5_SET(virtio_q, vq_ctx, umem_2_size, mvq->umem2.size); 8271a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, umem_3_id, mvq->umem3.id); 828e3011776SEli Cohen MLX5_SET(virtio_q, vq_ctx, umem_3_size, mvq->umem3.size); 8291a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, pd, ndev->mvdev.res.pdn); 8301a86b377SEli Cohen if (MLX5_CAP_DEV_VDPA_EMULATION(ndev->mvdev.mdev, eth_frame_offload_type)) 8311a86b377SEli Cohen MLX5_SET(virtio_q, vq_ctx, virtio_version_1_0, 1); 8321a86b377SEli Cohen 8331a86b377SEli Cohen err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out)); 8341a86b377SEli Cohen if (err) 8351a86b377SEli Cohen goto err_cmd; 8361a86b377SEli Cohen 8371a86b377SEli Cohen kfree(in); 8381a86b377SEli Cohen mvq->virtq_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id); 8391a86b377SEli Cohen 8401a86b377SEli Cohen return 0; 8411a86b377SEli Cohen 8421a86b377SEli Cohen err_cmd: 8431a86b377SEli Cohen kfree(in); 8441a86b377SEli Cohen err_alloc: 8451a86b377SEli Cohen umems_destroy(ndev, mvq); 8461a86b377SEli Cohen return err; 8471a86b377SEli Cohen } 8481a86b377SEli Cohen 8491a86b377SEli Cohen static void destroy_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 8501a86b377SEli Cohen { 8511a86b377SEli Cohen u32 in[MLX5_ST_SZ_DW(destroy_virtio_net_q_in)] = {}; 8521a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(destroy_virtio_net_q_out)] = {}; 8531a86b377SEli Cohen 8541a86b377SEli Cohen MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.opcode, 8551a86b377SEli Cohen MLX5_CMD_OP_DESTROY_GENERAL_OBJECT); 8561a86b377SEli Cohen MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.obj_id, mvq->virtq_id); 8571a86b377SEli Cohen MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.uid, ndev->mvdev.res.uid); 8581a86b377SEli Cohen MLX5_SET(destroy_virtio_net_q_in, in, general_obj_out_cmd_hdr.obj_type, 8591a86b377SEli Cohen MLX5_OBJ_TYPE_VIRTIO_NET_Q); 8601a86b377SEli Cohen if (mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, sizeof(out))) { 8611a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "destroy virtqueue 0x%x\n", mvq->virtq_id); 8621a86b377SEli Cohen return; 8631a86b377SEli Cohen } 8641a86b377SEli Cohen umems_destroy(ndev, mvq); 8651a86b377SEli Cohen } 8661a86b377SEli Cohen 8671a86b377SEli Cohen static u32 get_rqpn(struct mlx5_vdpa_virtqueue *mvq, bool fw) 8681a86b377SEli Cohen { 8691a86b377SEli Cohen return fw ? mvq->vqqp.mqp.qpn : mvq->fwqp.mqp.qpn; 8701a86b377SEli Cohen } 8711a86b377SEli Cohen 8721a86b377SEli Cohen static u32 get_qpn(struct mlx5_vdpa_virtqueue *mvq, bool fw) 8731a86b377SEli Cohen { 8741a86b377SEli Cohen return fw ? mvq->fwqp.mqp.qpn : mvq->vqqp.mqp.qpn; 8751a86b377SEli Cohen } 8761a86b377SEli Cohen 8771a86b377SEli Cohen static void alloc_inout(struct mlx5_vdpa_net *ndev, int cmd, void **in, int *inlen, void **out, 8781a86b377SEli Cohen int *outlen, u32 qpn, u32 rqpn) 8791a86b377SEli Cohen { 8801a86b377SEli Cohen void *qpc; 8811a86b377SEli Cohen void *pp; 8821a86b377SEli Cohen 8831a86b377SEli Cohen switch (cmd) { 8841a86b377SEli Cohen case MLX5_CMD_OP_2RST_QP: 8851a86b377SEli Cohen *inlen = MLX5_ST_SZ_BYTES(qp_2rst_in); 8861a86b377SEli Cohen *outlen = MLX5_ST_SZ_BYTES(qp_2rst_out); 8871a86b377SEli Cohen *in = kzalloc(*inlen, GFP_KERNEL); 8881a86b377SEli Cohen *out = kzalloc(*outlen, GFP_KERNEL); 889f31231bfSColin Ian King if (!*in || !*out) 8901a86b377SEli Cohen goto outerr; 8911a86b377SEli Cohen 8921a86b377SEli Cohen MLX5_SET(qp_2rst_in, *in, opcode, cmd); 8931a86b377SEli Cohen MLX5_SET(qp_2rst_in, *in, uid, ndev->mvdev.res.uid); 8941a86b377SEli Cohen MLX5_SET(qp_2rst_in, *in, qpn, qpn); 8951a86b377SEli Cohen break; 8961a86b377SEli Cohen case MLX5_CMD_OP_RST2INIT_QP: 8971a86b377SEli Cohen *inlen = MLX5_ST_SZ_BYTES(rst2init_qp_in); 8981a86b377SEli Cohen *outlen = MLX5_ST_SZ_BYTES(rst2init_qp_out); 8991a86b377SEli Cohen *in = kzalloc(*inlen, GFP_KERNEL); 9001a86b377SEli Cohen *out = kzalloc(MLX5_ST_SZ_BYTES(rst2init_qp_out), GFP_KERNEL); 901f31231bfSColin Ian King if (!*in || !*out) 9021a86b377SEli Cohen goto outerr; 9031a86b377SEli Cohen 9041a86b377SEli Cohen MLX5_SET(rst2init_qp_in, *in, opcode, cmd); 9051a86b377SEli Cohen MLX5_SET(rst2init_qp_in, *in, uid, ndev->mvdev.res.uid); 9061a86b377SEli Cohen MLX5_SET(rst2init_qp_in, *in, qpn, qpn); 9071a86b377SEli Cohen qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc); 9081a86b377SEli Cohen MLX5_SET(qpc, qpc, remote_qpn, rqpn); 9091a86b377SEli Cohen MLX5_SET(qpc, qpc, rwe, 1); 9101a86b377SEli Cohen pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path); 9111a86b377SEli Cohen MLX5_SET(ads, pp, vhca_port_num, 1); 9121a86b377SEli Cohen break; 9131a86b377SEli Cohen case MLX5_CMD_OP_INIT2RTR_QP: 9141a86b377SEli Cohen *inlen = MLX5_ST_SZ_BYTES(init2rtr_qp_in); 9151a86b377SEli Cohen *outlen = MLX5_ST_SZ_BYTES(init2rtr_qp_out); 9161a86b377SEli Cohen *in = kzalloc(*inlen, GFP_KERNEL); 9171a86b377SEli Cohen *out = kzalloc(MLX5_ST_SZ_BYTES(init2rtr_qp_out), GFP_KERNEL); 918f31231bfSColin Ian King if (!*in || !*out) 9191a86b377SEli Cohen goto outerr; 9201a86b377SEli Cohen 9211a86b377SEli Cohen MLX5_SET(init2rtr_qp_in, *in, opcode, cmd); 9221a86b377SEli Cohen MLX5_SET(init2rtr_qp_in, *in, uid, ndev->mvdev.res.uid); 9231a86b377SEli Cohen MLX5_SET(init2rtr_qp_in, *in, qpn, qpn); 9241a86b377SEli Cohen qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc); 9251a86b377SEli Cohen MLX5_SET(qpc, qpc, mtu, MLX5_QPC_MTU_256_BYTES); 9261a86b377SEli Cohen MLX5_SET(qpc, qpc, log_msg_max, 30); 9271a86b377SEli Cohen MLX5_SET(qpc, qpc, remote_qpn, rqpn); 9281a86b377SEli Cohen pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path); 9291a86b377SEli Cohen MLX5_SET(ads, pp, fl, 1); 9301a86b377SEli Cohen break; 9311a86b377SEli Cohen case MLX5_CMD_OP_RTR2RTS_QP: 9321a86b377SEli Cohen *inlen = MLX5_ST_SZ_BYTES(rtr2rts_qp_in); 9331a86b377SEli Cohen *outlen = MLX5_ST_SZ_BYTES(rtr2rts_qp_out); 9341a86b377SEli Cohen *in = kzalloc(*inlen, GFP_KERNEL); 9351a86b377SEli Cohen *out = kzalloc(MLX5_ST_SZ_BYTES(rtr2rts_qp_out), GFP_KERNEL); 936f31231bfSColin Ian King if (!*in || !*out) 9371a86b377SEli Cohen goto outerr; 9381a86b377SEli Cohen 9391a86b377SEli Cohen MLX5_SET(rtr2rts_qp_in, *in, opcode, cmd); 9401a86b377SEli Cohen MLX5_SET(rtr2rts_qp_in, *in, uid, ndev->mvdev.res.uid); 9411a86b377SEli Cohen MLX5_SET(rtr2rts_qp_in, *in, qpn, qpn); 9421a86b377SEli Cohen qpc = MLX5_ADDR_OF(rst2init_qp_in, *in, qpc); 9431a86b377SEli Cohen pp = MLX5_ADDR_OF(qpc, qpc, primary_address_path); 9441a86b377SEli Cohen MLX5_SET(ads, pp, ack_timeout, 14); 9451a86b377SEli Cohen MLX5_SET(qpc, qpc, retry_count, 7); 9461a86b377SEli Cohen MLX5_SET(qpc, qpc, rnr_retry, 7); 9471a86b377SEli Cohen break; 9481a86b377SEli Cohen default: 949f31231bfSColin Ian King goto outerr_nullify; 9501a86b377SEli Cohen } 9511a86b377SEli Cohen 9521a86b377SEli Cohen return; 9531a86b377SEli Cohen 9541a86b377SEli Cohen outerr: 9551a86b377SEli Cohen kfree(*in); 9561a86b377SEli Cohen kfree(*out); 957f31231bfSColin Ian King outerr_nullify: 9581a86b377SEli Cohen *in = NULL; 9591a86b377SEli Cohen *out = NULL; 9601a86b377SEli Cohen } 9611a86b377SEli Cohen 9621a86b377SEli Cohen static void free_inout(void *in, void *out) 9631a86b377SEli Cohen { 9641a86b377SEli Cohen kfree(in); 9651a86b377SEli Cohen kfree(out); 9661a86b377SEli Cohen } 9671a86b377SEli Cohen 9681a86b377SEli Cohen /* Two QPs are used by each virtqueue. One is used by the driver and one by 9691a86b377SEli Cohen * firmware. The fw argument indicates whether the subjected QP is the one used 9701a86b377SEli Cohen * by firmware. 9711a86b377SEli Cohen */ 9721a86b377SEli Cohen static int modify_qp(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, bool fw, int cmd) 9731a86b377SEli Cohen { 9741a86b377SEli Cohen int outlen; 9751a86b377SEli Cohen int inlen; 9761a86b377SEli Cohen void *out; 9771a86b377SEli Cohen void *in; 9781a86b377SEli Cohen int err; 9791a86b377SEli Cohen 9801a86b377SEli Cohen alloc_inout(ndev, cmd, &in, &inlen, &out, &outlen, get_qpn(mvq, fw), get_rqpn(mvq, fw)); 9811a86b377SEli Cohen if (!in || !out) 9821a86b377SEli Cohen return -ENOMEM; 9831a86b377SEli Cohen 9841a86b377SEli Cohen err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, outlen); 9851a86b377SEli Cohen free_inout(in, out); 9861a86b377SEli Cohen return err; 9871a86b377SEli Cohen } 9881a86b377SEli Cohen 9891a86b377SEli Cohen static int connect_qps(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 9901a86b377SEli Cohen { 9911a86b377SEli Cohen int err; 9921a86b377SEli Cohen 9931a86b377SEli Cohen err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_2RST_QP); 9941a86b377SEli Cohen if (err) 9951a86b377SEli Cohen return err; 9961a86b377SEli Cohen 9971a86b377SEli Cohen err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_2RST_QP); 9981a86b377SEli Cohen if (err) 9991a86b377SEli Cohen return err; 10001a86b377SEli Cohen 10011a86b377SEli Cohen err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_RST2INIT_QP); 10021a86b377SEli Cohen if (err) 10031a86b377SEli Cohen return err; 10041a86b377SEli Cohen 10051a86b377SEli Cohen err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_RST2INIT_QP); 10061a86b377SEli Cohen if (err) 10071a86b377SEli Cohen return err; 10081a86b377SEli Cohen 10091a86b377SEli Cohen err = modify_qp(ndev, mvq, true, MLX5_CMD_OP_INIT2RTR_QP); 10101a86b377SEli Cohen if (err) 10111a86b377SEli Cohen return err; 10121a86b377SEli Cohen 10131a86b377SEli Cohen err = modify_qp(ndev, mvq, false, MLX5_CMD_OP_INIT2RTR_QP); 10141a86b377SEli Cohen if (err) 10151a86b377SEli Cohen return err; 10161a86b377SEli Cohen 10171a86b377SEli Cohen return modify_qp(ndev, mvq, true, MLX5_CMD_OP_RTR2RTS_QP); 10181a86b377SEli Cohen } 10191a86b377SEli Cohen 10201a86b377SEli Cohen struct mlx5_virtq_attr { 10211a86b377SEli Cohen u8 state; 10221a86b377SEli Cohen u16 available_index; 1023b35ccebeSEli Cohen u16 used_index; 10241a86b377SEli Cohen }; 10251a86b377SEli Cohen 10261a86b377SEli Cohen static int query_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, 10271a86b377SEli Cohen struct mlx5_virtq_attr *attr) 10281a86b377SEli Cohen { 10291a86b377SEli Cohen int outlen = MLX5_ST_SZ_BYTES(query_virtio_net_q_out); 10301a86b377SEli Cohen u32 in[MLX5_ST_SZ_DW(query_virtio_net_q_in)] = {}; 10311a86b377SEli Cohen void *out; 10321a86b377SEli Cohen void *obj_context; 10331a86b377SEli Cohen void *cmd_hdr; 10341a86b377SEli Cohen int err; 10351a86b377SEli Cohen 10361a86b377SEli Cohen out = kzalloc(outlen, GFP_KERNEL); 10371a86b377SEli Cohen if (!out) 10381a86b377SEli Cohen return -ENOMEM; 10391a86b377SEli Cohen 10401a86b377SEli Cohen cmd_hdr = MLX5_ADDR_OF(query_virtio_net_q_in, in, general_obj_in_cmd_hdr); 10411a86b377SEli Cohen 10421a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_QUERY_GENERAL_OBJECT); 10431a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q); 10441a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->virtq_id); 10451a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid); 10461a86b377SEli Cohen err = mlx5_cmd_exec(ndev->mvdev.mdev, in, sizeof(in), out, outlen); 10471a86b377SEli Cohen if (err) 10481a86b377SEli Cohen goto err_cmd; 10491a86b377SEli Cohen 10501a86b377SEli Cohen obj_context = MLX5_ADDR_OF(query_virtio_net_q_out, out, obj_context); 10511a86b377SEli Cohen memset(attr, 0, sizeof(*attr)); 10521a86b377SEli Cohen attr->state = MLX5_GET(virtio_net_q_object, obj_context, state); 10531a86b377SEli Cohen attr->available_index = MLX5_GET(virtio_net_q_object, obj_context, hw_available_index); 1054b35ccebeSEli Cohen attr->used_index = MLX5_GET(virtio_net_q_object, obj_context, hw_used_index); 10551a86b377SEli Cohen kfree(out); 10561a86b377SEli Cohen return 0; 10571a86b377SEli Cohen 10581a86b377SEli Cohen err_cmd: 10591a86b377SEli Cohen kfree(out); 10601a86b377SEli Cohen return err; 10611a86b377SEli Cohen } 10621a86b377SEli Cohen 10631a86b377SEli Cohen static int modify_virtqueue(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq, int state) 10641a86b377SEli Cohen { 10651a86b377SEli Cohen int inlen = MLX5_ST_SZ_BYTES(modify_virtio_net_q_in); 10661a86b377SEli Cohen u32 out[MLX5_ST_SZ_DW(modify_virtio_net_q_out)] = {}; 10671a86b377SEli Cohen void *obj_context; 10681a86b377SEli Cohen void *cmd_hdr; 10691a86b377SEli Cohen void *in; 10701a86b377SEli Cohen int err; 10711a86b377SEli Cohen 10721a86b377SEli Cohen in = kzalloc(inlen, GFP_KERNEL); 10731a86b377SEli Cohen if (!in) 10741a86b377SEli Cohen return -ENOMEM; 10751a86b377SEli Cohen 10761a86b377SEli Cohen cmd_hdr = MLX5_ADDR_OF(modify_virtio_net_q_in, in, general_obj_in_cmd_hdr); 10771a86b377SEli Cohen 10781a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, opcode, MLX5_CMD_OP_MODIFY_GENERAL_OBJECT); 10791a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_type, MLX5_OBJ_TYPE_VIRTIO_NET_Q); 10801a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, obj_id, mvq->virtq_id); 10811a86b377SEli Cohen MLX5_SET(general_obj_in_cmd_hdr, cmd_hdr, uid, ndev->mvdev.res.uid); 10821a86b377SEli Cohen 10831a86b377SEli Cohen obj_context = MLX5_ADDR_OF(modify_virtio_net_q_in, in, obj_context); 10841a86b377SEli Cohen MLX5_SET64(virtio_net_q_object, obj_context, modify_field_select, 10851a86b377SEli Cohen MLX5_VIRTQ_MODIFY_MASK_STATE); 10861a86b377SEli Cohen MLX5_SET(virtio_net_q_object, obj_context, state, state); 10871a86b377SEli Cohen err = mlx5_cmd_exec(ndev->mvdev.mdev, in, inlen, out, sizeof(out)); 10881a86b377SEli Cohen kfree(in); 10891a86b377SEli Cohen if (!err) 10901a86b377SEli Cohen mvq->fw_state = state; 10911a86b377SEli Cohen 10921a86b377SEli Cohen return err; 10931a86b377SEli Cohen } 10941a86b377SEli Cohen 10951a86b377SEli Cohen static int setup_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 10961a86b377SEli Cohen { 10971a86b377SEli Cohen u16 idx = mvq->index; 10981a86b377SEli Cohen int err; 10991a86b377SEli Cohen 11001a86b377SEli Cohen if (!mvq->num_ent) 11011a86b377SEli Cohen return 0; 11021a86b377SEli Cohen 11031a86b377SEli Cohen if (mvq->initialized) { 11041a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "attempt re init\n"); 11051a86b377SEli Cohen return -EINVAL; 11061a86b377SEli Cohen } 11071a86b377SEli Cohen 11081a86b377SEli Cohen err = cq_create(ndev, idx, mvq->num_ent); 11091a86b377SEli Cohen if (err) 11101a86b377SEli Cohen return err; 11111a86b377SEli Cohen 11121a86b377SEli Cohen err = qp_create(ndev, mvq, &mvq->fwqp); 11131a86b377SEli Cohen if (err) 11141a86b377SEli Cohen goto err_fwqp; 11151a86b377SEli Cohen 11161a86b377SEli Cohen err = qp_create(ndev, mvq, &mvq->vqqp); 11171a86b377SEli Cohen if (err) 11181a86b377SEli Cohen goto err_vqqp; 11191a86b377SEli Cohen 11201a86b377SEli Cohen err = connect_qps(ndev, mvq); 11211a86b377SEli Cohen if (err) 11221a86b377SEli Cohen goto err_connect; 11231a86b377SEli Cohen 11241a86b377SEli Cohen err = create_virtqueue(ndev, mvq); 11251a86b377SEli Cohen if (err) 11261a86b377SEli Cohen goto err_connect; 11271a86b377SEli Cohen 11281a86b377SEli Cohen if (mvq->ready) { 11291a86b377SEli Cohen err = modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY); 11301a86b377SEli Cohen if (err) { 11311a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "failed to modify to ready vq idx %d(%d)\n", 11321a86b377SEli Cohen idx, err); 11331a86b377SEli Cohen goto err_connect; 11341a86b377SEli Cohen } 11351a86b377SEli Cohen } 11361a86b377SEli Cohen 11371a86b377SEli Cohen mvq->initialized = true; 11381a86b377SEli Cohen return 0; 11391a86b377SEli Cohen 11401a86b377SEli Cohen err_connect: 11411a86b377SEli Cohen qp_destroy(ndev, &mvq->vqqp); 11421a86b377SEli Cohen err_vqqp: 11431a86b377SEli Cohen qp_destroy(ndev, &mvq->fwqp); 11441a86b377SEli Cohen err_fwqp: 11451a86b377SEli Cohen cq_destroy(ndev, idx); 11461a86b377SEli Cohen return err; 11471a86b377SEli Cohen } 11481a86b377SEli Cohen 11491a86b377SEli Cohen static void suspend_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 11501a86b377SEli Cohen { 11511a86b377SEli Cohen struct mlx5_virtq_attr attr; 11521a86b377SEli Cohen 11531a86b377SEli Cohen if (!mvq->initialized) 11541a86b377SEli Cohen return; 11551a86b377SEli Cohen 11561a86b377SEli Cohen if (mvq->fw_state != MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY) 11571a86b377SEli Cohen return; 11581a86b377SEli Cohen 11591a86b377SEli Cohen if (modify_virtqueue(ndev, mvq, MLX5_VIRTIO_NET_Q_OBJECT_STATE_SUSPEND)) 11601a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "modify to suspend failed\n"); 11613176e974SSi-Wei Liu 11623176e974SSi-Wei Liu if (query_virtqueue(ndev, mvq, &attr)) { 11633176e974SSi-Wei Liu mlx5_vdpa_warn(&ndev->mvdev, "failed to query virtqueue\n"); 11643176e974SSi-Wei Liu return; 11653176e974SSi-Wei Liu } 11663176e974SSi-Wei Liu mvq->avail_idx = attr.available_index; 1167bc04d93eSEli Cohen mvq->used_idx = attr.used_index; 11681a86b377SEli Cohen } 11691a86b377SEli Cohen 11701a86b377SEli Cohen static void suspend_vqs(struct mlx5_vdpa_net *ndev) 11711a86b377SEli Cohen { 11721a86b377SEli Cohen int i; 11731a86b377SEli Cohen 11741a86b377SEli Cohen for (i = 0; i < MLX5_MAX_SUPPORTED_VQS; i++) 11751a86b377SEli Cohen suspend_vq(ndev, &ndev->vqs[i]); 11761a86b377SEli Cohen } 11771a86b377SEli Cohen 11781a86b377SEli Cohen static void teardown_vq(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 11791a86b377SEli Cohen { 11801a86b377SEli Cohen if (!mvq->initialized) 11811a86b377SEli Cohen return; 11821a86b377SEli Cohen 11831a86b377SEli Cohen suspend_vq(ndev, mvq); 11841a86b377SEli Cohen destroy_virtqueue(ndev, mvq); 11851a86b377SEli Cohen qp_destroy(ndev, &mvq->vqqp); 11861a86b377SEli Cohen qp_destroy(ndev, &mvq->fwqp); 11871a86b377SEli Cohen cq_destroy(ndev, mvq->index); 11881a86b377SEli Cohen mvq->initialized = false; 11891a86b377SEli Cohen } 11901a86b377SEli Cohen 11911a86b377SEli Cohen static int create_rqt(struct mlx5_vdpa_net *ndev) 11921a86b377SEli Cohen { 11931a86b377SEli Cohen int log_max_rqt; 11941a86b377SEli Cohen __be32 *list; 11951a86b377SEli Cohen void *rqtc; 11961a86b377SEli Cohen int inlen; 11971a86b377SEli Cohen void *in; 11981a86b377SEli Cohen int i, j; 11991a86b377SEli Cohen int err; 12001a86b377SEli Cohen 12011a86b377SEli Cohen log_max_rqt = min_t(int, 1, MLX5_CAP_GEN(ndev->mvdev.mdev, log_max_rqt_size)); 12021a86b377SEli Cohen if (log_max_rqt < 1) 12031a86b377SEli Cohen return -EOPNOTSUPP; 12041a86b377SEli Cohen 12051a86b377SEli Cohen inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + (1 << log_max_rqt) * MLX5_ST_SZ_BYTES(rq_num); 12061a86b377SEli Cohen in = kzalloc(inlen, GFP_KERNEL); 12071a86b377SEli Cohen if (!in) 12081a86b377SEli Cohen return -ENOMEM; 12091a86b377SEli Cohen 12101a86b377SEli Cohen MLX5_SET(create_rqt_in, in, uid, ndev->mvdev.res.uid); 12111a86b377SEli Cohen rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context); 12121a86b377SEli Cohen 12131a86b377SEli Cohen MLX5_SET(rqtc, rqtc, list_q_type, MLX5_RQTC_LIST_Q_TYPE_VIRTIO_NET_Q); 12141a86b377SEli Cohen MLX5_SET(rqtc, rqtc, rqt_max_size, 1 << log_max_rqt); 12151a86b377SEli Cohen MLX5_SET(rqtc, rqtc, rqt_actual_size, 1); 12161a86b377SEli Cohen list = MLX5_ADDR_OF(rqtc, rqtc, rq_num[0]); 12171a86b377SEli Cohen for (i = 0, j = 0; j < ndev->mvdev.max_vqs; j++) { 12181a86b377SEli Cohen if (!ndev->vqs[j].initialized) 12191a86b377SEli Cohen continue; 12201a86b377SEli Cohen 12211a86b377SEli Cohen if (!vq_is_tx(ndev->vqs[j].index)) { 12221a86b377SEli Cohen list[i] = cpu_to_be32(ndev->vqs[j].virtq_id); 12231a86b377SEli Cohen i++; 12241a86b377SEli Cohen } 12251a86b377SEli Cohen } 12261a86b377SEli Cohen 12271a86b377SEli Cohen err = mlx5_vdpa_create_rqt(&ndev->mvdev, in, inlen, &ndev->res.rqtn); 12281a86b377SEli Cohen kfree(in); 12291a86b377SEli Cohen if (err) 12301a86b377SEli Cohen return err; 12311a86b377SEli Cohen 12321a86b377SEli Cohen return 0; 12331a86b377SEli Cohen } 12341a86b377SEli Cohen 12351a86b377SEli Cohen static void destroy_rqt(struct mlx5_vdpa_net *ndev) 12361a86b377SEli Cohen { 12371a86b377SEli Cohen mlx5_vdpa_destroy_rqt(&ndev->mvdev, ndev->res.rqtn); 12381a86b377SEli Cohen } 12391a86b377SEli Cohen 12401a86b377SEli Cohen static int create_tir(struct mlx5_vdpa_net *ndev) 12411a86b377SEli Cohen { 12421a86b377SEli Cohen #define HASH_IP_L4PORTS \ 12431a86b377SEli Cohen (MLX5_HASH_FIELD_SEL_SRC_IP | MLX5_HASH_FIELD_SEL_DST_IP | MLX5_HASH_FIELD_SEL_L4_SPORT | \ 12441a86b377SEli Cohen MLX5_HASH_FIELD_SEL_L4_DPORT) 12451a86b377SEli Cohen static const u8 rx_hash_toeplitz_key[] = { 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7, 12461a86b377SEli Cohen 0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94, 12471a86b377SEli Cohen 0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1, 12481a86b377SEli Cohen 0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59, 12491a86b377SEli Cohen 0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a }; 12501a86b377SEli Cohen void *rss_key; 12511a86b377SEli Cohen void *outer; 12521a86b377SEli Cohen void *tirc; 12531a86b377SEli Cohen void *in; 12541a86b377SEli Cohen int err; 12551a86b377SEli Cohen 12561a86b377SEli Cohen in = kzalloc(MLX5_ST_SZ_BYTES(create_tir_in), GFP_KERNEL); 12571a86b377SEli Cohen if (!in) 12581a86b377SEli Cohen return -ENOMEM; 12591a86b377SEli Cohen 12601a86b377SEli Cohen MLX5_SET(create_tir_in, in, uid, ndev->mvdev.res.uid); 12611a86b377SEli Cohen tirc = MLX5_ADDR_OF(create_tir_in, in, ctx); 12621a86b377SEli Cohen MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT); 12631a86b377SEli Cohen 12641a86b377SEli Cohen MLX5_SET(tirc, tirc, rx_hash_symmetric, 1); 12651a86b377SEli Cohen MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ); 12661a86b377SEli Cohen rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key); 12671a86b377SEli Cohen memcpy(rss_key, rx_hash_toeplitz_key, sizeof(rx_hash_toeplitz_key)); 12681a86b377SEli Cohen 12691a86b377SEli Cohen outer = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer); 12701a86b377SEli Cohen MLX5_SET(rx_hash_field_select, outer, l3_prot_type, MLX5_L3_PROT_TYPE_IPV4); 12711a86b377SEli Cohen MLX5_SET(rx_hash_field_select, outer, l4_prot_type, MLX5_L4_PROT_TYPE_TCP); 12721a86b377SEli Cohen MLX5_SET(rx_hash_field_select, outer, selected_fields, HASH_IP_L4PORTS); 12731a86b377SEli Cohen 12741a86b377SEli Cohen MLX5_SET(tirc, tirc, indirect_table, ndev->res.rqtn); 12751a86b377SEli Cohen MLX5_SET(tirc, tirc, transport_domain, ndev->res.tdn); 12761a86b377SEli Cohen 12771a86b377SEli Cohen err = mlx5_vdpa_create_tir(&ndev->mvdev, in, &ndev->res.tirn); 12781a86b377SEli Cohen kfree(in); 12791a86b377SEli Cohen return err; 12801a86b377SEli Cohen } 12811a86b377SEli Cohen 12821a86b377SEli Cohen static void destroy_tir(struct mlx5_vdpa_net *ndev) 12831a86b377SEli Cohen { 12841a86b377SEli Cohen mlx5_vdpa_destroy_tir(&ndev->mvdev, ndev->res.tirn); 12851a86b377SEli Cohen } 12861a86b377SEli Cohen 12871a86b377SEli Cohen static int add_fwd_to_tir(struct mlx5_vdpa_net *ndev) 12881a86b377SEli Cohen { 12891a86b377SEli Cohen struct mlx5_flow_destination dest[2] = {}; 12901a86b377SEli Cohen struct mlx5_flow_table_attr ft_attr = {}; 12911a86b377SEli Cohen struct mlx5_flow_act flow_act = {}; 12921a86b377SEli Cohen struct mlx5_flow_namespace *ns; 12931a86b377SEli Cohen int err; 12941a86b377SEli Cohen 12951a86b377SEli Cohen /* for now, one entry, match all, forward to tir */ 12961a86b377SEli Cohen ft_attr.max_fte = 1; 12971a86b377SEli Cohen ft_attr.autogroup.max_num_groups = 1; 12981a86b377SEli Cohen 12991a86b377SEli Cohen ns = mlx5_get_flow_namespace(ndev->mvdev.mdev, MLX5_FLOW_NAMESPACE_BYPASS); 13001a86b377SEli Cohen if (!ns) { 13011a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "get flow namespace\n"); 13021a86b377SEli Cohen return -EOPNOTSUPP; 13031a86b377SEli Cohen } 13041a86b377SEli Cohen 13051a86b377SEli Cohen ndev->rxft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr); 13061a86b377SEli Cohen if (IS_ERR(ndev->rxft)) 13071a86b377SEli Cohen return PTR_ERR(ndev->rxft); 13081a86b377SEli Cohen 13091a86b377SEli Cohen ndev->rx_counter = mlx5_fc_create(ndev->mvdev.mdev, false); 13101a86b377SEli Cohen if (IS_ERR(ndev->rx_counter)) { 13111a86b377SEli Cohen err = PTR_ERR(ndev->rx_counter); 13121a86b377SEli Cohen goto err_fc; 13131a86b377SEli Cohen } 13141a86b377SEli Cohen 13151a86b377SEli Cohen flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST | MLX5_FLOW_CONTEXT_ACTION_COUNT; 13161a86b377SEli Cohen dest[0].type = MLX5_FLOW_DESTINATION_TYPE_TIR; 13171a86b377SEli Cohen dest[0].tir_num = ndev->res.tirn; 13181a86b377SEli Cohen dest[1].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER; 13191a86b377SEli Cohen dest[1].counter_id = mlx5_fc_id(ndev->rx_counter); 13201a86b377SEli Cohen ndev->rx_rule = mlx5_add_flow_rules(ndev->rxft, NULL, &flow_act, dest, 2); 13211a86b377SEli Cohen if (IS_ERR(ndev->rx_rule)) { 13221a86b377SEli Cohen err = PTR_ERR(ndev->rx_rule); 13231a86b377SEli Cohen ndev->rx_rule = NULL; 13241a86b377SEli Cohen goto err_rule; 13251a86b377SEli Cohen } 13261a86b377SEli Cohen 13271a86b377SEli Cohen return 0; 13281a86b377SEli Cohen 13291a86b377SEli Cohen err_rule: 13301a86b377SEli Cohen mlx5_fc_destroy(ndev->mvdev.mdev, ndev->rx_counter); 13311a86b377SEli Cohen err_fc: 13321a86b377SEli Cohen mlx5_destroy_flow_table(ndev->rxft); 13331a86b377SEli Cohen return err; 13341a86b377SEli Cohen } 13351a86b377SEli Cohen 13361a86b377SEli Cohen static void remove_fwd_to_tir(struct mlx5_vdpa_net *ndev) 13371a86b377SEli Cohen { 13381a86b377SEli Cohen if (!ndev->rx_rule) 13391a86b377SEli Cohen return; 13401a86b377SEli Cohen 13411a86b377SEli Cohen mlx5_del_flow_rules(ndev->rx_rule); 13421a86b377SEli Cohen mlx5_fc_destroy(ndev->mvdev.mdev, ndev->rx_counter); 13431a86b377SEli Cohen mlx5_destroy_flow_table(ndev->rxft); 13441a86b377SEli Cohen 13451a86b377SEli Cohen ndev->rx_rule = NULL; 13461a86b377SEli Cohen } 13471a86b377SEli Cohen 13481a86b377SEli Cohen static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx) 13491a86b377SEli Cohen { 13501a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 13511a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 13521a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 13531a86b377SEli Cohen 13541a86b377SEli Cohen if (unlikely(!mvq->ready)) 13551a86b377SEli Cohen return; 13561a86b377SEli Cohen 13571a86b377SEli Cohen iowrite16(idx, ndev->mvdev.res.kick_addr); 13581a86b377SEli Cohen } 13591a86b377SEli Cohen 13601a86b377SEli Cohen static int mlx5_vdpa_set_vq_address(struct vdpa_device *vdev, u16 idx, u64 desc_area, 13611a86b377SEli Cohen u64 driver_area, u64 device_area) 13621a86b377SEli Cohen { 13631a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 13641a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 13651a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 13661a86b377SEli Cohen 13671a86b377SEli Cohen mvq->desc_addr = desc_area; 13681a86b377SEli Cohen mvq->device_addr = device_area; 13691a86b377SEli Cohen mvq->driver_addr = driver_area; 13701a86b377SEli Cohen return 0; 13711a86b377SEli Cohen } 13721a86b377SEli Cohen 13731a86b377SEli Cohen static void mlx5_vdpa_set_vq_num(struct vdpa_device *vdev, u16 idx, u32 num) 13741a86b377SEli Cohen { 13751a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 13761a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 13771a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq; 13781a86b377SEli Cohen 13791a86b377SEli Cohen mvq = &ndev->vqs[idx]; 13801a86b377SEli Cohen mvq->num_ent = num; 13811a86b377SEli Cohen } 13821a86b377SEli Cohen 13831a86b377SEli Cohen static void mlx5_vdpa_set_vq_cb(struct vdpa_device *vdev, u16 idx, struct vdpa_callback *cb) 13841a86b377SEli Cohen { 13851a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 13861a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 13871a86b377SEli Cohen struct mlx5_vdpa_virtqueue *vq = &ndev->vqs[idx]; 13881a86b377SEli Cohen 13891a86b377SEli Cohen vq->event_cb = *cb; 13901a86b377SEli Cohen } 13911a86b377SEli Cohen 13921a86b377SEli Cohen static void mlx5_vdpa_set_vq_ready(struct vdpa_device *vdev, u16 idx, bool ready) 13931a86b377SEli Cohen { 13941a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 13951a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 13961a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 13971a86b377SEli Cohen 13981a86b377SEli Cohen if (!ready) 13991a86b377SEli Cohen suspend_vq(ndev, mvq); 14001a86b377SEli Cohen 14011a86b377SEli Cohen mvq->ready = ready; 14021a86b377SEli Cohen } 14031a86b377SEli Cohen 14041a86b377SEli Cohen static bool mlx5_vdpa_get_vq_ready(struct vdpa_device *vdev, u16 idx) 14051a86b377SEli Cohen { 14061a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 14071a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 14081a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 14091a86b377SEli Cohen 14101a86b377SEli Cohen return mvq->ready; 14111a86b377SEli Cohen } 14121a86b377SEli Cohen 14131a86b377SEli Cohen static int mlx5_vdpa_set_vq_state(struct vdpa_device *vdev, u16 idx, 14141a86b377SEli Cohen const struct vdpa_vq_state *state) 14151a86b377SEli Cohen { 14161a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 14171a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 14181a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 14191a86b377SEli Cohen 14201a86b377SEli Cohen if (mvq->fw_state == MLX5_VIRTIO_NET_Q_OBJECT_STATE_RDY) { 14211a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "can't modify available index\n"); 14221a86b377SEli Cohen return -EINVAL; 14231a86b377SEli Cohen } 14241a86b377SEli Cohen 1425530a5678SJason Wang mvq->used_idx = state->split.avail_index; 1426530a5678SJason Wang mvq->avail_idx = state->split.avail_index; 14271a86b377SEli Cohen return 0; 14281a86b377SEli Cohen } 14291a86b377SEli Cohen 14301a86b377SEli Cohen static int mlx5_vdpa_get_vq_state(struct vdpa_device *vdev, u16 idx, struct vdpa_vq_state *state) 14311a86b377SEli Cohen { 14321a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 14331a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 14341a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq = &ndev->vqs[idx]; 14351a86b377SEli Cohen struct mlx5_virtq_attr attr; 14361a86b377SEli Cohen int err; 14371a86b377SEli Cohen 14383176e974SSi-Wei Liu /* If the virtq object was destroyed, use the value saved at 14393176e974SSi-Wei Liu * the last minute of suspend_vq. This caters for userspace 14403176e974SSi-Wei Liu * that cares about emulating the index after vq is stopped. 14413176e974SSi-Wei Liu */ 14423176e974SSi-Wei Liu if (!mvq->initialized) { 1443bc04d93eSEli Cohen /* Firmware returns a wrong value for the available index. 1444bc04d93eSEli Cohen * Since both values should be identical, we take the value of 1445bc04d93eSEli Cohen * used_idx which is reported correctly. 1446bc04d93eSEli Cohen */ 1447530a5678SJason Wang state->split.avail_index = mvq->used_idx; 14483176e974SSi-Wei Liu return 0; 14493176e974SSi-Wei Liu } 14501a86b377SEli Cohen 14511a86b377SEli Cohen err = query_virtqueue(ndev, mvq, &attr); 14521a86b377SEli Cohen if (err) { 14531a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "failed to query virtqueue\n"); 14541a86b377SEli Cohen return err; 14551a86b377SEli Cohen } 1456530a5678SJason Wang state->split.avail_index = attr.used_index; 14571a86b377SEli Cohen return 0; 14581a86b377SEli Cohen } 14591a86b377SEli Cohen 14601a86b377SEli Cohen static u32 mlx5_vdpa_get_vq_align(struct vdpa_device *vdev) 14611a86b377SEli Cohen { 14621a86b377SEli Cohen return PAGE_SIZE; 14631a86b377SEli Cohen } 14641a86b377SEli Cohen 14651a86b377SEli Cohen enum { MLX5_VIRTIO_NET_F_GUEST_CSUM = 1 << 9, 14661a86b377SEli Cohen MLX5_VIRTIO_NET_F_CSUM = 1 << 10, 14671a86b377SEli Cohen MLX5_VIRTIO_NET_F_HOST_TSO6 = 1 << 11, 14681a86b377SEli Cohen MLX5_VIRTIO_NET_F_HOST_TSO4 = 1 << 12, 14691a86b377SEli Cohen }; 14701a86b377SEli Cohen 14711a86b377SEli Cohen static u64 mlx_to_vritio_features(u16 dev_features) 14721a86b377SEli Cohen { 14731a86b377SEli Cohen u64 result = 0; 14741a86b377SEli Cohen 14751a86b377SEli Cohen if (dev_features & MLX5_VIRTIO_NET_F_GUEST_CSUM) 1476cbb52359SNathan Chancellor result |= BIT_ULL(VIRTIO_NET_F_GUEST_CSUM); 14771a86b377SEli Cohen if (dev_features & MLX5_VIRTIO_NET_F_CSUM) 1478cbb52359SNathan Chancellor result |= BIT_ULL(VIRTIO_NET_F_CSUM); 14791a86b377SEli Cohen if (dev_features & MLX5_VIRTIO_NET_F_HOST_TSO6) 1480cbb52359SNathan Chancellor result |= BIT_ULL(VIRTIO_NET_F_HOST_TSO6); 14811a86b377SEli Cohen if (dev_features & MLX5_VIRTIO_NET_F_HOST_TSO4) 1482cbb52359SNathan Chancellor result |= BIT_ULL(VIRTIO_NET_F_HOST_TSO4); 14831a86b377SEli Cohen 14841a86b377SEli Cohen return result; 14851a86b377SEli Cohen } 14861a86b377SEli Cohen 14871a86b377SEli Cohen static u64 mlx5_vdpa_get_features(struct vdpa_device *vdev) 14881a86b377SEli Cohen { 14891a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 14901a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 14911a86b377SEli Cohen u16 dev_features; 14921a86b377SEli Cohen 14931a86b377SEli Cohen dev_features = MLX5_CAP_DEV_VDPA_EMULATION(mvdev->mdev, device_features_bits_mask); 14941a86b377SEli Cohen ndev->mvdev.mlx_features = mlx_to_vritio_features(dev_features); 14951a86b377SEli Cohen if (MLX5_CAP_DEV_VDPA_EMULATION(mvdev->mdev, virtio_version_1_0)) 1496cbb52359SNathan Chancellor ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_F_VERSION_1); 1497cbb52359SNathan Chancellor ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_F_ACCESS_PLATFORM); 14981a86b377SEli Cohen print_features(mvdev, ndev->mvdev.mlx_features, false); 14991a86b377SEli Cohen return ndev->mvdev.mlx_features; 15001a86b377SEli Cohen } 15011a86b377SEli Cohen 15021a86b377SEli Cohen static int verify_min_features(struct mlx5_vdpa_dev *mvdev, u64 features) 15031a86b377SEli Cohen { 1504cbb52359SNathan Chancellor if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM))) 15051a86b377SEli Cohen return -EOPNOTSUPP; 15061a86b377SEli Cohen 15071a86b377SEli Cohen return 0; 15081a86b377SEli Cohen } 15091a86b377SEli Cohen 15101a86b377SEli Cohen static int setup_virtqueues(struct mlx5_vdpa_net *ndev) 15111a86b377SEli Cohen { 15121a86b377SEli Cohen int err; 15131a86b377SEli Cohen int i; 15141a86b377SEli Cohen 15151a86b377SEli Cohen for (i = 0; i < 2 * mlx5_vdpa_max_qps(ndev->mvdev.max_vqs); i++) { 15161a86b377SEli Cohen err = setup_vq(ndev, &ndev->vqs[i]); 15171a86b377SEli Cohen if (err) 15181a86b377SEli Cohen goto err_vq; 15191a86b377SEli Cohen } 15201a86b377SEli Cohen 15211a86b377SEli Cohen return 0; 15221a86b377SEli Cohen 15231a86b377SEli Cohen err_vq: 15241a86b377SEli Cohen for (--i; i >= 0; i--) 15251a86b377SEli Cohen teardown_vq(ndev, &ndev->vqs[i]); 15261a86b377SEli Cohen 15271a86b377SEli Cohen return err; 15281a86b377SEli Cohen } 15291a86b377SEli Cohen 15301a86b377SEli Cohen static void teardown_virtqueues(struct mlx5_vdpa_net *ndev) 15311a86b377SEli Cohen { 15321a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq; 15331a86b377SEli Cohen int i; 15341a86b377SEli Cohen 15351a86b377SEli Cohen for (i = ndev->mvdev.max_vqs - 1; i >= 0; i--) { 15361a86b377SEli Cohen mvq = &ndev->vqs[i]; 15371a86b377SEli Cohen if (!mvq->initialized) 15381a86b377SEli Cohen continue; 15391a86b377SEli Cohen 15401a86b377SEli Cohen teardown_vq(ndev, mvq); 15411a86b377SEli Cohen } 15421a86b377SEli Cohen } 15431a86b377SEli Cohen 15448a7c3213SMichael S. Tsirkin /* TODO: cross-endian support */ 15458a7c3213SMichael S. Tsirkin static inline bool mlx5_vdpa_is_little_endian(struct mlx5_vdpa_dev *mvdev) 15468a7c3213SMichael S. Tsirkin { 15478a7c3213SMichael S. Tsirkin return virtio_legacy_is_little_endian() || 15484b454a82SEli Cohen (mvdev->actual_features & BIT_ULL(VIRTIO_F_VERSION_1)); 15498a7c3213SMichael S. Tsirkin } 15508a7c3213SMichael S. Tsirkin 155136bdcf31SEli Cohen static __virtio16 cpu_to_mlx5vdpa16(struct mlx5_vdpa_dev *mvdev, u16 val) 155236bdcf31SEli Cohen { 155336bdcf31SEli Cohen return __cpu_to_virtio16(mlx5_vdpa_is_little_endian(mvdev), val); 155436bdcf31SEli Cohen } 155536bdcf31SEli Cohen 15561a86b377SEli Cohen static int mlx5_vdpa_set_features(struct vdpa_device *vdev, u64 features) 15571a86b377SEli Cohen { 15581a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 15591a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 15601a86b377SEli Cohen int err; 15611a86b377SEli Cohen 15621a86b377SEli Cohen print_features(mvdev, features, true); 15631a86b377SEli Cohen 15641a86b377SEli Cohen err = verify_min_features(mvdev, features); 15651a86b377SEli Cohen if (err) 15661a86b377SEli Cohen return err; 15671a86b377SEli Cohen 15681a86b377SEli Cohen ndev->mvdev.actual_features = features & ndev->mvdev.mlx_features; 156936bdcf31SEli Cohen ndev->config.mtu = cpu_to_mlx5vdpa16(mvdev, ndev->mtu); 157036b02df2SEli Cohen ndev->config.status |= cpu_to_mlx5vdpa16(mvdev, VIRTIO_NET_S_LINK_UP); 15711a86b377SEli Cohen return err; 15721a86b377SEli Cohen } 15731a86b377SEli Cohen 15741a86b377SEli Cohen static void mlx5_vdpa_set_config_cb(struct vdpa_device *vdev, struct vdpa_callback *cb) 15751a86b377SEli Cohen { 15761a86b377SEli Cohen /* not implemented */ 15771a86b377SEli Cohen mlx5_vdpa_warn(to_mvdev(vdev), "set config callback not supported\n"); 15781a86b377SEli Cohen } 15791a86b377SEli Cohen 15801a86b377SEli Cohen #define MLX5_VDPA_MAX_VQ_ENTRIES 256 15811a86b377SEli Cohen static u16 mlx5_vdpa_get_vq_num_max(struct vdpa_device *vdev) 15821a86b377SEli Cohen { 15831a86b377SEli Cohen return MLX5_VDPA_MAX_VQ_ENTRIES; 15841a86b377SEli Cohen } 15851a86b377SEli Cohen 15861a86b377SEli Cohen static u32 mlx5_vdpa_get_device_id(struct vdpa_device *vdev) 15871a86b377SEli Cohen { 15881a86b377SEli Cohen return VIRTIO_ID_NET; 15891a86b377SEli Cohen } 15901a86b377SEli Cohen 15911a86b377SEli Cohen static u32 mlx5_vdpa_get_vendor_id(struct vdpa_device *vdev) 15921a86b377SEli Cohen { 15931a86b377SEli Cohen return PCI_VENDOR_ID_MELLANOX; 15941a86b377SEli Cohen } 15951a86b377SEli Cohen 15961a86b377SEli Cohen static u8 mlx5_vdpa_get_status(struct vdpa_device *vdev) 15971a86b377SEli Cohen { 15981a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 15991a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 16001a86b377SEli Cohen 16011a86b377SEli Cohen print_status(mvdev, ndev->mvdev.status, false); 16021a86b377SEli Cohen return ndev->mvdev.status; 16031a86b377SEli Cohen } 16041a86b377SEli Cohen 16051a86b377SEli Cohen static int save_channel_info(struct mlx5_vdpa_net *ndev, struct mlx5_vdpa_virtqueue *mvq) 16061a86b377SEli Cohen { 16071a86b377SEli Cohen struct mlx5_vq_restore_info *ri = &mvq->ri; 16081a86b377SEli Cohen struct mlx5_virtq_attr attr; 16091a86b377SEli Cohen int err; 16101a86b377SEli Cohen 16111a86b377SEli Cohen if (!mvq->initialized) 16121a86b377SEli Cohen return 0; 16131a86b377SEli Cohen 16141a86b377SEli Cohen err = query_virtqueue(ndev, mvq, &attr); 16151a86b377SEli Cohen if (err) 16161a86b377SEli Cohen return err; 16171a86b377SEli Cohen 16181a86b377SEli Cohen ri->avail_index = attr.available_index; 1619b35ccebeSEli Cohen ri->used_index = attr.used_index; 16201a86b377SEli Cohen ri->ready = mvq->ready; 16211a86b377SEli Cohen ri->num_ent = mvq->num_ent; 16221a86b377SEli Cohen ri->desc_addr = mvq->desc_addr; 16231a86b377SEli Cohen ri->device_addr = mvq->device_addr; 16241a86b377SEli Cohen ri->driver_addr = mvq->driver_addr; 16251a86b377SEli Cohen ri->cb = mvq->event_cb; 16261a86b377SEli Cohen ri->restore = true; 16271a86b377SEli Cohen return 0; 16281a86b377SEli Cohen } 16291a86b377SEli Cohen 16301a86b377SEli Cohen static int save_channels_info(struct mlx5_vdpa_net *ndev) 16311a86b377SEli Cohen { 16321a86b377SEli Cohen int i; 16331a86b377SEli Cohen 16341a86b377SEli Cohen for (i = 0; i < ndev->mvdev.max_vqs; i++) { 16351a86b377SEli Cohen memset(&ndev->vqs[i].ri, 0, sizeof(ndev->vqs[i].ri)); 16361a86b377SEli Cohen save_channel_info(ndev, &ndev->vqs[i]); 16371a86b377SEli Cohen } 16381a86b377SEli Cohen return 0; 16391a86b377SEli Cohen } 16401a86b377SEli Cohen 16411a86b377SEli Cohen static void mlx5_clear_vqs(struct mlx5_vdpa_net *ndev) 16421a86b377SEli Cohen { 16431a86b377SEli Cohen int i; 16441a86b377SEli Cohen 16451a86b377SEli Cohen for (i = 0; i < ndev->mvdev.max_vqs; i++) 16461a86b377SEli Cohen memset(&ndev->vqs[i], 0, offsetof(struct mlx5_vdpa_virtqueue, ri)); 16471a86b377SEli Cohen } 16481a86b377SEli Cohen 16491a86b377SEli Cohen static void restore_channels_info(struct mlx5_vdpa_net *ndev) 16501a86b377SEli Cohen { 16511a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq; 16521a86b377SEli Cohen struct mlx5_vq_restore_info *ri; 16531a86b377SEli Cohen int i; 16541a86b377SEli Cohen 16551a86b377SEli Cohen mlx5_clear_vqs(ndev); 16561a86b377SEli Cohen init_mvqs(ndev); 16571a86b377SEli Cohen for (i = 0; i < ndev->mvdev.max_vqs; i++) { 16581a86b377SEli Cohen mvq = &ndev->vqs[i]; 16591a86b377SEli Cohen ri = &mvq->ri; 16601a86b377SEli Cohen if (!ri->restore) 16611a86b377SEli Cohen continue; 16621a86b377SEli Cohen 16631a86b377SEli Cohen mvq->avail_idx = ri->avail_index; 1664b35ccebeSEli Cohen mvq->used_idx = ri->used_index; 16651a86b377SEli Cohen mvq->ready = ri->ready; 16661a86b377SEli Cohen mvq->num_ent = ri->num_ent; 16671a86b377SEli Cohen mvq->desc_addr = ri->desc_addr; 16681a86b377SEli Cohen mvq->device_addr = ri->device_addr; 16691a86b377SEli Cohen mvq->driver_addr = ri->driver_addr; 16701a86b377SEli Cohen mvq->event_cb = ri->cb; 16711a86b377SEli Cohen } 16721a86b377SEli Cohen } 16731a86b377SEli Cohen 16741a86b377SEli Cohen static int mlx5_vdpa_change_map(struct mlx5_vdpa_net *ndev, struct vhost_iotlb *iotlb) 16751a86b377SEli Cohen { 16761a86b377SEli Cohen int err; 16771a86b377SEli Cohen 16781a86b377SEli Cohen suspend_vqs(ndev); 16791a86b377SEli Cohen err = save_channels_info(ndev); 16801a86b377SEli Cohen if (err) 16811a86b377SEli Cohen goto err_mr; 16821a86b377SEli Cohen 16831a86b377SEli Cohen teardown_driver(ndev); 16841a86b377SEli Cohen mlx5_vdpa_destroy_mr(&ndev->mvdev); 16851a86b377SEli Cohen err = mlx5_vdpa_create_mr(&ndev->mvdev, iotlb); 16861a86b377SEli Cohen if (err) 16871a86b377SEli Cohen goto err_mr; 16881a86b377SEli Cohen 16891897f0b6SEli Cohen if (!(ndev->mvdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) 16901897f0b6SEli Cohen return 0; 16911897f0b6SEli Cohen 16921a86b377SEli Cohen restore_channels_info(ndev); 16931a86b377SEli Cohen err = setup_driver(ndev); 16941a86b377SEli Cohen if (err) 16951a86b377SEli Cohen goto err_setup; 16961a86b377SEli Cohen 16971a86b377SEli Cohen return 0; 16981a86b377SEli Cohen 16991a86b377SEli Cohen err_setup: 17001a86b377SEli Cohen mlx5_vdpa_destroy_mr(&ndev->mvdev); 17011a86b377SEli Cohen err_mr: 17021a86b377SEli Cohen return err; 17031a86b377SEli Cohen } 17041a86b377SEli Cohen 17051a86b377SEli Cohen static int setup_driver(struct mlx5_vdpa_net *ndev) 17061a86b377SEli Cohen { 17071a86b377SEli Cohen int err; 17081a86b377SEli Cohen 17091a86b377SEli Cohen mutex_lock(&ndev->reslock); 17101a86b377SEli Cohen if (ndev->setup) { 17111a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "setup driver called for already setup driver\n"); 17121a86b377SEli Cohen err = 0; 17131a86b377SEli Cohen goto out; 17141a86b377SEli Cohen } 17151a86b377SEli Cohen err = setup_virtqueues(ndev); 17161a86b377SEli Cohen if (err) { 17171a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "setup_virtqueues\n"); 17181a86b377SEli Cohen goto out; 17191a86b377SEli Cohen } 17201a86b377SEli Cohen 17211a86b377SEli Cohen err = create_rqt(ndev); 17221a86b377SEli Cohen if (err) { 17231a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "create_rqt\n"); 17241a86b377SEli Cohen goto err_rqt; 17251a86b377SEli Cohen } 17261a86b377SEli Cohen 17271a86b377SEli Cohen err = create_tir(ndev); 17281a86b377SEli Cohen if (err) { 17291a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "create_tir\n"); 17301a86b377SEli Cohen goto err_tir; 17311a86b377SEli Cohen } 17321a86b377SEli Cohen 17331a86b377SEli Cohen err = add_fwd_to_tir(ndev); 17341a86b377SEli Cohen if (err) { 17351a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "add_fwd_to_tir\n"); 17361a86b377SEli Cohen goto err_fwd; 17371a86b377SEli Cohen } 17381a86b377SEli Cohen ndev->setup = true; 17391a86b377SEli Cohen mutex_unlock(&ndev->reslock); 17401a86b377SEli Cohen 17411a86b377SEli Cohen return 0; 17421a86b377SEli Cohen 17431a86b377SEli Cohen err_fwd: 17441a86b377SEli Cohen destroy_tir(ndev); 17451a86b377SEli Cohen err_tir: 17461a86b377SEli Cohen destroy_rqt(ndev); 17471a86b377SEli Cohen err_rqt: 17481a86b377SEli Cohen teardown_virtqueues(ndev); 17491a86b377SEli Cohen out: 17501a86b377SEli Cohen mutex_unlock(&ndev->reslock); 17511a86b377SEli Cohen return err; 17521a86b377SEli Cohen } 17531a86b377SEli Cohen 17541a86b377SEli Cohen static void teardown_driver(struct mlx5_vdpa_net *ndev) 17551a86b377SEli Cohen { 17561a86b377SEli Cohen mutex_lock(&ndev->reslock); 17571a86b377SEli Cohen if (!ndev->setup) 17581a86b377SEli Cohen goto out; 17591a86b377SEli Cohen 17601a86b377SEli Cohen remove_fwd_to_tir(ndev); 17611a86b377SEli Cohen destroy_tir(ndev); 17621a86b377SEli Cohen destroy_rqt(ndev); 17631a86b377SEli Cohen teardown_virtqueues(ndev); 17641a86b377SEli Cohen ndev->setup = false; 17651a86b377SEli Cohen out: 17661a86b377SEli Cohen mutex_unlock(&ndev->reslock); 17671a86b377SEli Cohen } 17681a86b377SEli Cohen 1769e3aadf2eSEli Cohen static void clear_vqs_ready(struct mlx5_vdpa_net *ndev) 1770e3aadf2eSEli Cohen { 1771e3aadf2eSEli Cohen int i; 1772e3aadf2eSEli Cohen 1773e3aadf2eSEli Cohen for (i = 0; i < ndev->mvdev.max_vqs; i++) 1774e3aadf2eSEli Cohen ndev->vqs[i].ready = false; 1775e3aadf2eSEli Cohen } 1776e3aadf2eSEli Cohen 17771a86b377SEli Cohen static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) 17781a86b377SEli Cohen { 17791a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 17801a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 17811a86b377SEli Cohen int err; 17821a86b377SEli Cohen 17831a86b377SEli Cohen print_status(mvdev, status, true); 17841a86b377SEli Cohen if (!status) { 17851a86b377SEli Cohen mlx5_vdpa_info(mvdev, "performing device reset\n"); 17861a86b377SEli Cohen teardown_driver(ndev); 1787e3aadf2eSEli Cohen clear_vqs_ready(ndev); 17881a86b377SEli Cohen mlx5_vdpa_destroy_mr(&ndev->mvdev); 17891a86b377SEli Cohen ndev->mvdev.status = 0; 17901a86b377SEli Cohen ndev->mvdev.mlx_features = 0; 17911a86b377SEli Cohen ++mvdev->generation; 17926f5312f8SEli Cohen if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) { 17936f5312f8SEli Cohen if (mlx5_vdpa_create_mr(mvdev, NULL)) 17946f5312f8SEli Cohen mlx5_vdpa_warn(mvdev, "create MR failed\n"); 17956f5312f8SEli Cohen } 17961a86b377SEli Cohen return; 17971a86b377SEli Cohen } 17981a86b377SEli Cohen 17991a86b377SEli Cohen if ((status ^ ndev->mvdev.status) & VIRTIO_CONFIG_S_DRIVER_OK) { 18001a86b377SEli Cohen if (status & VIRTIO_CONFIG_S_DRIVER_OK) { 18011a86b377SEli Cohen err = setup_driver(ndev); 18021a86b377SEli Cohen if (err) { 18031a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "failed to setup driver\n"); 18041a86b377SEli Cohen goto err_setup; 18051a86b377SEli Cohen } 18061a86b377SEli Cohen } else { 18071a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "did not expect DRIVER_OK to be cleared\n"); 18081a86b377SEli Cohen return; 18091a86b377SEli Cohen } 18101a86b377SEli Cohen } 18111a86b377SEli Cohen 18121a86b377SEli Cohen ndev->mvdev.status = status; 18131a86b377SEli Cohen return; 18141a86b377SEli Cohen 18151a86b377SEli Cohen err_setup: 18161a86b377SEli Cohen mlx5_vdpa_destroy_mr(&ndev->mvdev); 18171a86b377SEli Cohen ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; 18181a86b377SEli Cohen } 18191a86b377SEli Cohen 1820442706f9SStefano Garzarella static size_t mlx5_vdpa_get_config_size(struct vdpa_device *vdev) 1821442706f9SStefano Garzarella { 1822442706f9SStefano Garzarella return sizeof(struct virtio_net_config); 1823442706f9SStefano Garzarella } 1824442706f9SStefano Garzarella 18251a86b377SEli Cohen static void mlx5_vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, void *buf, 18261a86b377SEli Cohen unsigned int len) 18271a86b377SEli Cohen { 18281a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 18291a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 18301a86b377SEli Cohen 1831dcfde163SStefano Garzarella if (offset + len <= sizeof(struct virtio_net_config)) 18322874211fSDan Carpenter memcpy(buf, (u8 *)&ndev->config + offset, len); 18331a86b377SEli Cohen } 18341a86b377SEli Cohen 18351a86b377SEli Cohen static void mlx5_vdpa_set_config(struct vdpa_device *vdev, unsigned int offset, const void *buf, 18361a86b377SEli Cohen unsigned int len) 18371a86b377SEli Cohen { 18381a86b377SEli Cohen /* not supported */ 18391a86b377SEli Cohen } 18401a86b377SEli Cohen 18411a86b377SEli Cohen static u32 mlx5_vdpa_get_generation(struct vdpa_device *vdev) 18421a86b377SEli Cohen { 18431a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 18441a86b377SEli Cohen 18451a86b377SEli Cohen return mvdev->generation; 18461a86b377SEli Cohen } 18471a86b377SEli Cohen 18481a86b377SEli Cohen static int mlx5_vdpa_set_map(struct vdpa_device *vdev, struct vhost_iotlb *iotlb) 18491a86b377SEli Cohen { 18501a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 18511a86b377SEli Cohen struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); 18521a86b377SEli Cohen bool change_map; 18531a86b377SEli Cohen int err; 18541a86b377SEli Cohen 18551a86b377SEli Cohen err = mlx5_vdpa_handle_set_map(mvdev, iotlb, &change_map); 18561a86b377SEli Cohen if (err) { 18571a86b377SEli Cohen mlx5_vdpa_warn(mvdev, "set map failed(%d)\n", err); 18581a86b377SEli Cohen return err; 18591a86b377SEli Cohen } 18601a86b377SEli Cohen 18611a86b377SEli Cohen if (change_map) 18621a86b377SEli Cohen return mlx5_vdpa_change_map(ndev, iotlb); 18631a86b377SEli Cohen 18641a86b377SEli Cohen return 0; 18651a86b377SEli Cohen } 18661a86b377SEli Cohen 18671a86b377SEli Cohen static void mlx5_vdpa_free(struct vdpa_device *vdev) 18681a86b377SEli Cohen { 18691a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 18707c9f131fSEli Cohen struct mlx5_core_dev *pfmdev; 18711a86b377SEli Cohen struct mlx5_vdpa_net *ndev; 18721a86b377SEli Cohen 18731a86b377SEli Cohen ndev = to_mlx5_vdpa_ndev(mvdev); 18741a86b377SEli Cohen 18751a86b377SEli Cohen free_resources(ndev); 18766f5312f8SEli Cohen mlx5_vdpa_destroy_mr(mvdev); 18777c9f131fSEli Cohen if (!is_zero_ether_addr(ndev->config.mac)) { 18787c9f131fSEli Cohen pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev)); 18797c9f131fSEli Cohen mlx5_mpfs_del_mac(pfmdev, ndev->config.mac); 18807c9f131fSEli Cohen } 18811a86b377SEli Cohen mlx5_vdpa_free_resources(&ndev->mvdev); 18821a86b377SEli Cohen mutex_destroy(&ndev->reslock); 18831a86b377SEli Cohen } 18841a86b377SEli Cohen 18851a86b377SEli Cohen static struct vdpa_notification_area mlx5_get_vq_notification(struct vdpa_device *vdev, u16 idx) 18861a86b377SEli Cohen { 1887b57c46cbSEli Cohen struct mlx5_vdpa_dev *mvdev = to_mvdev(vdev); 18881a86b377SEli Cohen struct vdpa_notification_area ret = {}; 1889b57c46cbSEli Cohen struct mlx5_vdpa_net *ndev; 1890b57c46cbSEli Cohen phys_addr_t addr; 18911a86b377SEli Cohen 1892b57c46cbSEli Cohen /* If SF BAR size is smaller than PAGE_SIZE, do not use direct 1893b57c46cbSEli Cohen * notification to avoid the risk of mapping pages that contain BAR of more 1894b57c46cbSEli Cohen * than one SF 1895b57c46cbSEli Cohen */ 1896b57c46cbSEli Cohen if (MLX5_CAP_GEN(mvdev->mdev, log_min_sf_size) + 12 < PAGE_SHIFT) 1897b57c46cbSEli Cohen return ret; 1898b57c46cbSEli Cohen 1899b57c46cbSEli Cohen ndev = to_mlx5_vdpa_ndev(mvdev); 1900b57c46cbSEli Cohen addr = (phys_addr_t)ndev->mvdev.res.phys_kick_addr; 1901b57c46cbSEli Cohen ret.addr = addr; 1902b57c46cbSEli Cohen ret.size = PAGE_SIZE; 19031a86b377SEli Cohen return ret; 19041a86b377SEli Cohen } 19051a86b377SEli Cohen 19061a86b377SEli Cohen static int mlx5_get_vq_irq(struct vdpa_device *vdv, u16 idx) 19071a86b377SEli Cohen { 19081a86b377SEli Cohen return -EOPNOTSUPP; 19091a86b377SEli Cohen } 19101a86b377SEli Cohen 19111a86b377SEli Cohen static const struct vdpa_config_ops mlx5_vdpa_ops = { 19121a86b377SEli Cohen .set_vq_address = mlx5_vdpa_set_vq_address, 19131a86b377SEli Cohen .set_vq_num = mlx5_vdpa_set_vq_num, 19141a86b377SEli Cohen .kick_vq = mlx5_vdpa_kick_vq, 19151a86b377SEli Cohen .set_vq_cb = mlx5_vdpa_set_vq_cb, 19161a86b377SEli Cohen .set_vq_ready = mlx5_vdpa_set_vq_ready, 19171a86b377SEli Cohen .get_vq_ready = mlx5_vdpa_get_vq_ready, 19181a86b377SEli Cohen .set_vq_state = mlx5_vdpa_set_vq_state, 19191a86b377SEli Cohen .get_vq_state = mlx5_vdpa_get_vq_state, 19201a86b377SEli Cohen .get_vq_notification = mlx5_get_vq_notification, 19211a86b377SEli Cohen .get_vq_irq = mlx5_get_vq_irq, 19221a86b377SEli Cohen .get_vq_align = mlx5_vdpa_get_vq_align, 19231a86b377SEli Cohen .get_features = mlx5_vdpa_get_features, 19241a86b377SEli Cohen .set_features = mlx5_vdpa_set_features, 19251a86b377SEli Cohen .set_config_cb = mlx5_vdpa_set_config_cb, 19261a86b377SEli Cohen .get_vq_num_max = mlx5_vdpa_get_vq_num_max, 19271a86b377SEli Cohen .get_device_id = mlx5_vdpa_get_device_id, 19281a86b377SEli Cohen .get_vendor_id = mlx5_vdpa_get_vendor_id, 19291a86b377SEli Cohen .get_status = mlx5_vdpa_get_status, 19301a86b377SEli Cohen .set_status = mlx5_vdpa_set_status, 1931442706f9SStefano Garzarella .get_config_size = mlx5_vdpa_get_config_size, 19321a86b377SEli Cohen .get_config = mlx5_vdpa_get_config, 19331a86b377SEli Cohen .set_config = mlx5_vdpa_set_config, 19341a86b377SEli Cohen .get_generation = mlx5_vdpa_get_generation, 19351a86b377SEli Cohen .set_map = mlx5_vdpa_set_map, 19361a86b377SEli Cohen .free = mlx5_vdpa_free, 19371a86b377SEli Cohen }; 19381a86b377SEli Cohen 1939d084d996SSi-Wei Liu static int query_mtu(struct mlx5_core_dev *mdev, u16 *mtu) 1940d084d996SSi-Wei Liu { 1941d084d996SSi-Wei Liu u16 hw_mtu; 1942d084d996SSi-Wei Liu int err; 1943d084d996SSi-Wei Liu 1944d084d996SSi-Wei Liu err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu); 1945d084d996SSi-Wei Liu if (err) 1946d084d996SSi-Wei Liu return err; 1947d084d996SSi-Wei Liu 1948d084d996SSi-Wei Liu *mtu = hw_mtu - MLX5V_ETH_HARD_MTU; 1949d084d996SSi-Wei Liu return 0; 1950d084d996SSi-Wei Liu } 1951d084d996SSi-Wei Liu 19521a86b377SEli Cohen static int alloc_resources(struct mlx5_vdpa_net *ndev) 19531a86b377SEli Cohen { 19541a86b377SEli Cohen struct mlx5_vdpa_net_resources *res = &ndev->res; 19551a86b377SEli Cohen int err; 19561a86b377SEli Cohen 19571a86b377SEli Cohen if (res->valid) { 19581a86b377SEli Cohen mlx5_vdpa_warn(&ndev->mvdev, "resources already allocated\n"); 19591a86b377SEli Cohen return -EEXIST; 19601a86b377SEli Cohen } 19611a86b377SEli Cohen 19621a86b377SEli Cohen err = mlx5_vdpa_alloc_transport_domain(&ndev->mvdev, &res->tdn); 19631a86b377SEli Cohen if (err) 19641a86b377SEli Cohen return err; 19651a86b377SEli Cohen 19661a86b377SEli Cohen err = create_tis(ndev); 19671a86b377SEli Cohen if (err) 19681a86b377SEli Cohen goto err_tis; 19691a86b377SEli Cohen 19701a86b377SEli Cohen res->valid = true; 19711a86b377SEli Cohen 19721a86b377SEli Cohen return 0; 19731a86b377SEli Cohen 19741a86b377SEli Cohen err_tis: 19751a86b377SEli Cohen mlx5_vdpa_dealloc_transport_domain(&ndev->mvdev, res->tdn); 19761a86b377SEli Cohen return err; 19771a86b377SEli Cohen } 19781a86b377SEli Cohen 19791a86b377SEli Cohen static void free_resources(struct mlx5_vdpa_net *ndev) 19801a86b377SEli Cohen { 19811a86b377SEli Cohen struct mlx5_vdpa_net_resources *res = &ndev->res; 19821a86b377SEli Cohen 19831a86b377SEli Cohen if (!res->valid) 19841a86b377SEli Cohen return; 19851a86b377SEli Cohen 19861a86b377SEli Cohen destroy_tis(ndev); 19871a86b377SEli Cohen mlx5_vdpa_dealloc_transport_domain(&ndev->mvdev, res->tdn); 19881a86b377SEli Cohen res->valid = false; 19891a86b377SEli Cohen } 19901a86b377SEli Cohen 19911a86b377SEli Cohen static void init_mvqs(struct mlx5_vdpa_net *ndev) 19921a86b377SEli Cohen { 19931a86b377SEli Cohen struct mlx5_vdpa_virtqueue *mvq; 19941a86b377SEli Cohen int i; 19951a86b377SEli Cohen 19961a86b377SEli Cohen for (i = 0; i < 2 * mlx5_vdpa_max_qps(ndev->mvdev.max_vqs); ++i) { 19971a86b377SEli Cohen mvq = &ndev->vqs[i]; 19981a86b377SEli Cohen memset(mvq, 0, offsetof(struct mlx5_vdpa_virtqueue, ri)); 19991a86b377SEli Cohen mvq->index = i; 20001a86b377SEli Cohen mvq->ndev = ndev; 20011a86b377SEli Cohen mvq->fwqp.fw = true; 20021a86b377SEli Cohen } 20031a86b377SEli Cohen for (; i < ndev->mvdev.max_vqs; i++) { 20041a86b377SEli Cohen mvq = &ndev->vqs[i]; 20051a86b377SEli Cohen memset(mvq, 0, offsetof(struct mlx5_vdpa_virtqueue, ri)); 20061a86b377SEli Cohen mvq->index = i; 20071a86b377SEli Cohen mvq->ndev = ndev; 20081a86b377SEli Cohen } 20091a86b377SEli Cohen } 20101a86b377SEli Cohen 201158926c8aSEli Cohen struct mlx5_vdpa_mgmtdev { 201258926c8aSEli Cohen struct vdpa_mgmt_dev mgtdev; 201358926c8aSEli Cohen struct mlx5_adev *madev; 201458926c8aSEli Cohen struct mlx5_vdpa_net *ndev; 201558926c8aSEli Cohen }; 201658926c8aSEli Cohen 201758926c8aSEli Cohen static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name) 20181a86b377SEli Cohen { 201958926c8aSEli Cohen struct mlx5_vdpa_mgmtdev *mgtdev = container_of(v_mdev, struct mlx5_vdpa_mgmtdev, mgtdev); 20201a86b377SEli Cohen struct virtio_net_config *config; 20217c9f131fSEli Cohen struct mlx5_core_dev *pfmdev; 20221a86b377SEli Cohen struct mlx5_vdpa_dev *mvdev; 20231a86b377SEli Cohen struct mlx5_vdpa_net *ndev; 202458926c8aSEli Cohen struct mlx5_core_dev *mdev; 20251a86b377SEli Cohen u32 max_vqs; 20261a86b377SEli Cohen int err; 20271a86b377SEli Cohen 202858926c8aSEli Cohen if (mgtdev->ndev) 202958926c8aSEli Cohen return -ENOSPC; 203058926c8aSEli Cohen 203158926c8aSEli Cohen mdev = mgtdev->madev->mdev; 20321a86b377SEli Cohen /* we save one virtqueue for control virtqueue should we require it */ 20331a86b377SEli Cohen max_vqs = MLX5_CAP_DEV_VDPA_EMULATION(mdev, max_num_virtio_queues); 20341a86b377SEli Cohen max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS); 20351a86b377SEli Cohen 20361a86b377SEli Cohen ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops, 203758926c8aSEli Cohen name); 20381a86b377SEli Cohen if (IS_ERR(ndev)) 203974c9729dSLeon Romanovsky return PTR_ERR(ndev); 20401a86b377SEli Cohen 20411a86b377SEli Cohen ndev->mvdev.max_vqs = max_vqs; 20421a86b377SEli Cohen mvdev = &ndev->mvdev; 20431a86b377SEli Cohen mvdev->mdev = mdev; 20441a86b377SEli Cohen init_mvqs(ndev); 20451a86b377SEli Cohen mutex_init(&ndev->reslock); 20461a86b377SEli Cohen config = &ndev->config; 2047d084d996SSi-Wei Liu err = query_mtu(mdev, &ndev->mtu); 20481a86b377SEli Cohen if (err) 20491a86b377SEli Cohen goto err_mtu; 20501a86b377SEli Cohen 20511a86b377SEli Cohen err = mlx5_query_nic_vport_mac_address(mdev, 0, 0, config->mac); 20521a86b377SEli Cohen if (err) 20531a86b377SEli Cohen goto err_mtu; 20541a86b377SEli Cohen 20557c9f131fSEli Cohen if (!is_zero_ether_addr(config->mac)) { 20567c9f131fSEli Cohen pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev)); 20577c9f131fSEli Cohen err = mlx5_mpfs_add_mac(pfmdev, config->mac); 20587c9f131fSEli Cohen if (err) 20597c9f131fSEli Cohen goto err_mtu; 20607c9f131fSEli Cohen } 20617c9f131fSEli Cohen 20627d23dcdfSEli Cohen mvdev->vdev.dma_dev = &mdev->pdev->dev; 20631a86b377SEli Cohen err = mlx5_vdpa_alloc_resources(&ndev->mvdev); 20641a86b377SEli Cohen if (err) 20657c9f131fSEli Cohen goto err_mpfs; 20661a86b377SEli Cohen 20676f5312f8SEli Cohen if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) { 20686f5312f8SEli Cohen err = mlx5_vdpa_create_mr(mvdev, NULL); 20691a86b377SEli Cohen if (err) 20701a86b377SEli Cohen goto err_res; 20716f5312f8SEli Cohen } 20726f5312f8SEli Cohen 20736f5312f8SEli Cohen err = alloc_resources(ndev); 20746f5312f8SEli Cohen if (err) 20756f5312f8SEli Cohen goto err_mr; 20761a86b377SEli Cohen 207758926c8aSEli Cohen mvdev->vdev.mdev = &mgtdev->mgtdev; 207858926c8aSEli Cohen err = _vdpa_register_device(&mvdev->vdev, 2 * mlx5_vdpa_max_qps(max_vqs)); 20791a86b377SEli Cohen if (err) 20801a86b377SEli Cohen goto err_reg; 20811a86b377SEli Cohen 208258926c8aSEli Cohen mgtdev->ndev = ndev; 208374c9729dSLeon Romanovsky return 0; 20841a86b377SEli Cohen 20851a86b377SEli Cohen err_reg: 20861a86b377SEli Cohen free_resources(ndev); 20876f5312f8SEli Cohen err_mr: 20886f5312f8SEli Cohen mlx5_vdpa_destroy_mr(mvdev); 20891a86b377SEli Cohen err_res: 20901a86b377SEli Cohen mlx5_vdpa_free_resources(&ndev->mvdev); 20917c9f131fSEli Cohen err_mpfs: 20927c9f131fSEli Cohen if (!is_zero_ether_addr(config->mac)) 20937c9f131fSEli Cohen mlx5_mpfs_del_mac(pfmdev, config->mac); 20941a86b377SEli Cohen err_mtu: 20951a86b377SEli Cohen mutex_destroy(&ndev->reslock); 20961a86b377SEli Cohen put_device(&mvdev->vdev.dev); 209774c9729dSLeon Romanovsky return err; 20981a86b377SEli Cohen } 20991a86b377SEli Cohen 210058926c8aSEli Cohen static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev) 210158926c8aSEli Cohen { 210258926c8aSEli Cohen struct mlx5_vdpa_mgmtdev *mgtdev = container_of(v_mdev, struct mlx5_vdpa_mgmtdev, mgtdev); 210358926c8aSEli Cohen 210458926c8aSEli Cohen _vdpa_unregister_device(dev); 210558926c8aSEli Cohen mgtdev->ndev = NULL; 210658926c8aSEli Cohen } 210758926c8aSEli Cohen 210858926c8aSEli Cohen static const struct vdpa_mgmtdev_ops mdev_ops = { 210958926c8aSEli Cohen .dev_add = mlx5_vdpa_dev_add, 211058926c8aSEli Cohen .dev_del = mlx5_vdpa_dev_del, 211158926c8aSEli Cohen }; 211258926c8aSEli Cohen 211358926c8aSEli Cohen static struct virtio_device_id id_table[] = { 211458926c8aSEli Cohen { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 211558926c8aSEli Cohen { 0 }, 211658926c8aSEli Cohen }; 211758926c8aSEli Cohen 211858926c8aSEli Cohen static int mlx5v_probe(struct auxiliary_device *adev, 211958926c8aSEli Cohen const struct auxiliary_device_id *id) 212058926c8aSEli Cohen 212158926c8aSEli Cohen { 212258926c8aSEli Cohen struct mlx5_adev *madev = container_of(adev, struct mlx5_adev, adev); 212358926c8aSEli Cohen struct mlx5_core_dev *mdev = madev->mdev; 212458926c8aSEli Cohen struct mlx5_vdpa_mgmtdev *mgtdev; 212558926c8aSEli Cohen int err; 212658926c8aSEli Cohen 212758926c8aSEli Cohen mgtdev = kzalloc(sizeof(*mgtdev), GFP_KERNEL); 212858926c8aSEli Cohen if (!mgtdev) 212958926c8aSEli Cohen return -ENOMEM; 213058926c8aSEli Cohen 213158926c8aSEli Cohen mgtdev->mgtdev.ops = &mdev_ops; 213258926c8aSEli Cohen mgtdev->mgtdev.device = mdev->device; 213358926c8aSEli Cohen mgtdev->mgtdev.id_table = id_table; 213458926c8aSEli Cohen mgtdev->madev = madev; 213558926c8aSEli Cohen 213658926c8aSEli Cohen err = vdpa_mgmtdev_register(&mgtdev->mgtdev); 213758926c8aSEli Cohen if (err) 213858926c8aSEli Cohen goto reg_err; 213958926c8aSEli Cohen 214058926c8aSEli Cohen dev_set_drvdata(&adev->dev, mgtdev); 214158926c8aSEli Cohen 214258926c8aSEli Cohen return 0; 214358926c8aSEli Cohen 214458926c8aSEli Cohen reg_err: 214558926c8aSEli Cohen kfree(mgtdev); 214658926c8aSEli Cohen return err; 214758926c8aSEli Cohen } 214858926c8aSEli Cohen 214974c9729dSLeon Romanovsky static void mlx5v_remove(struct auxiliary_device *adev) 21501a86b377SEli Cohen { 215158926c8aSEli Cohen struct mlx5_vdpa_mgmtdev *mgtdev; 215274c9729dSLeon Romanovsky 215358926c8aSEli Cohen mgtdev = dev_get_drvdata(&adev->dev); 215458926c8aSEli Cohen vdpa_mgmtdev_unregister(&mgtdev->mgtdev); 215558926c8aSEli Cohen kfree(mgtdev); 21561a86b377SEli Cohen } 215774c9729dSLeon Romanovsky 215874c9729dSLeon Romanovsky static const struct auxiliary_device_id mlx5v_id_table[] = { 215974c9729dSLeon Romanovsky { .name = MLX5_ADEV_NAME ".vnet", }, 216074c9729dSLeon Romanovsky {}, 216174c9729dSLeon Romanovsky }; 216274c9729dSLeon Romanovsky 216374c9729dSLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5v_id_table); 216474c9729dSLeon Romanovsky 216574c9729dSLeon Romanovsky static struct auxiliary_driver mlx5v_driver = { 216674c9729dSLeon Romanovsky .name = "vnet", 216774c9729dSLeon Romanovsky .probe = mlx5v_probe, 216874c9729dSLeon Romanovsky .remove = mlx5v_remove, 216974c9729dSLeon Romanovsky .id_table = mlx5v_id_table, 217074c9729dSLeon Romanovsky }; 217174c9729dSLeon Romanovsky 217274c9729dSLeon Romanovsky module_auxiliary_driver(mlx5v_driver); 2173