140909f66SGal Pressman // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 240909f66SGal Pressman /* 36dddd939SYonatan Nachum * Copyright 2018-2023 Amazon.com, Inc. or its affiliates. All rights reserved. 440909f66SGal Pressman */ 540909f66SGal Pressman 666f4817bSGal Pressman #include <linux/dma-buf.h> 766f4817bSGal Pressman #include <linux/dma-resv.h> 840909f66SGal Pressman #include <linux/vmalloc.h> 91f9b6827SJason Gunthorpe #include <linux/log2.h> 1040909f66SGal Pressman 1140909f66SGal Pressman #include <rdma/ib_addr.h> 1240909f66SGal Pressman #include <rdma/ib_umem.h> 1340909f66SGal Pressman #include <rdma/ib_user_verbs.h> 1440909f66SGal Pressman #include <rdma/ib_verbs.h> 1540909f66SGal Pressman #include <rdma/uverbs_ioctl.h> 1640909f66SGal Pressman 1740909f66SGal Pressman #include "efa.h" 18dc13fbf7SMichael Margolin #include "efa_io_defs.h" 1940909f66SGal Pressman 2040909f66SGal Pressman enum { 2140909f66SGal Pressman EFA_MMAP_DMA_PAGE = 0, 2240909f66SGal Pressman EFA_MMAP_IO_WC, 2340909f66SGal Pressman EFA_MMAP_IO_NC, 2440909f66SGal Pressman }; 2540909f66SGal Pressman 2640909f66SGal Pressman #define EFA_AENQ_ENABLED_GROUPS \ 2740909f66SGal Pressman (BIT(EFA_ADMIN_FATAL_ERROR) | BIT(EFA_ADMIN_WARNING) | \ 2840909f66SGal Pressman BIT(EFA_ADMIN_NOTIFICATION) | BIT(EFA_ADMIN_KEEP_ALIVE)) 2940909f66SGal Pressman 30e84d3c18SMichal Kalderon struct efa_user_mmap_entry { 31e84d3c18SMichal Kalderon struct rdma_user_mmap_entry rdma_entry; 3240909f66SGal Pressman u64 address; 3340909f66SGal Pressman u8 mmap_flag; 3440909f66SGal Pressman }; 3540909f66SGal Pressman 368c1b4316SGal Pressman #define EFA_DEFINE_DEVICE_STATS(op) \ 3716e9111eSGal Pressman op(EFA_SUBMITTED_CMDS, "submitted_cmds") \ 3816e9111eSGal Pressman op(EFA_COMPLETED_CMDS, "completed_cmds") \ 39f86e3437SGal Pressman op(EFA_CMDS_ERR, "cmds_err") \ 4016e9111eSGal Pressman op(EFA_NO_COMPLETION_CMDS, "no_completion_cmds") \ 4116e9111eSGal Pressman op(EFA_KEEP_ALIVE_RCVD, "keep_alive_rcvd") \ 4216e9111eSGal Pressman op(EFA_ALLOC_PD_ERR, "alloc_pd_err") \ 4316e9111eSGal Pressman op(EFA_CREATE_QP_ERR, "create_qp_err") \ 44b2ea69b3SGal Pressman op(EFA_CREATE_CQ_ERR, "create_cq_err") \ 4516e9111eSGal Pressman op(EFA_REG_MR_ERR, "reg_mr_err") \ 4616e9111eSGal Pressman op(EFA_ALLOC_UCONTEXT_ERR, "alloc_ucontext_err") \ 47eca5757fSGal Pressman op(EFA_CREATE_AH_ERR, "create_ah_err") \ 48eca5757fSGal Pressman op(EFA_MMAP_ERR, "mmap_err") 4916e9111eSGal Pressman 508c1b4316SGal Pressman #define EFA_DEFINE_PORT_STATS(op) \ 518c1b4316SGal Pressman op(EFA_TX_BYTES, "tx_bytes") \ 528c1b4316SGal Pressman op(EFA_TX_PKTS, "tx_pkts") \ 538c1b4316SGal Pressman op(EFA_RX_BYTES, "rx_bytes") \ 548c1b4316SGal Pressman op(EFA_RX_PKTS, "rx_pkts") \ 558c1b4316SGal Pressman op(EFA_RX_DROPS, "rx_drops") \ 568c1b4316SGal Pressman op(EFA_SEND_BYTES, "send_bytes") \ 578c1b4316SGal Pressman op(EFA_SEND_WRS, "send_wrs") \ 588c1b4316SGal Pressman op(EFA_RECV_BYTES, "recv_bytes") \ 598c1b4316SGal Pressman op(EFA_RECV_WRS, "recv_wrs") \ 608c1b4316SGal Pressman op(EFA_RDMA_READ_WRS, "rdma_read_wrs") \ 618c1b4316SGal Pressman op(EFA_RDMA_READ_BYTES, "rdma_read_bytes") \ 628c1b4316SGal Pressman op(EFA_RDMA_READ_WR_ERR, "rdma_read_wr_err") \ 638c1b4316SGal Pressman op(EFA_RDMA_READ_RESP_BYTES, "rdma_read_resp_bytes") \ 64*113383efSMichael Margolin op(EFA_RDMA_WRITE_WRS, "rdma_write_wrs") \ 65*113383efSMichael Margolin op(EFA_RDMA_WRITE_BYTES, "rdma_write_bytes") \ 66*113383efSMichael Margolin op(EFA_RDMA_WRITE_WR_ERR, "rdma_write_wr_err") \ 67*113383efSMichael Margolin op(EFA_RDMA_WRITE_RECV_BYTES, "rdma_write_recv_bytes") \ 688c1b4316SGal Pressman 6916e9111eSGal Pressman #define EFA_STATS_ENUM(ename, name) ename, 7013f30b0fSAharon Landau #define EFA_STATS_STR(ename, nam) \ 7113f30b0fSAharon Landau [ename].name = nam, 7216e9111eSGal Pressman 738c1b4316SGal Pressman enum efa_hw_device_stats { 748c1b4316SGal Pressman EFA_DEFINE_DEVICE_STATS(EFA_STATS_ENUM) 7516e9111eSGal Pressman }; 7616e9111eSGal Pressman 7713f30b0fSAharon Landau static const struct rdma_stat_desc efa_device_stats_descs[] = { 788c1b4316SGal Pressman EFA_DEFINE_DEVICE_STATS(EFA_STATS_STR) 798c1b4316SGal Pressman }; 808c1b4316SGal Pressman 818c1b4316SGal Pressman enum efa_hw_port_stats { 828c1b4316SGal Pressman EFA_DEFINE_PORT_STATS(EFA_STATS_ENUM) 838c1b4316SGal Pressman }; 848c1b4316SGal Pressman 8513f30b0fSAharon Landau static const struct rdma_stat_desc efa_port_stats_descs[] = { 868c1b4316SGal Pressman EFA_DEFINE_PORT_STATS(EFA_STATS_STR) 8716e9111eSGal Pressman }; 8816e9111eSGal Pressman 8940909f66SGal Pressman #define EFA_CHUNK_PAYLOAD_SHIFT 12 9040909f66SGal Pressman #define EFA_CHUNK_PAYLOAD_SIZE BIT(EFA_CHUNK_PAYLOAD_SHIFT) 9140909f66SGal Pressman #define EFA_CHUNK_PAYLOAD_PTR_SIZE 8 9240909f66SGal Pressman 9340909f66SGal Pressman #define EFA_CHUNK_SHIFT 12 9440909f66SGal Pressman #define EFA_CHUNK_SIZE BIT(EFA_CHUNK_SHIFT) 9540909f66SGal Pressman #define EFA_CHUNK_PTR_SIZE sizeof(struct efa_com_ctrl_buff_info) 9640909f66SGal Pressman 9740909f66SGal Pressman #define EFA_PTRS_PER_CHUNK \ 9840909f66SGal Pressman ((EFA_CHUNK_SIZE - EFA_CHUNK_PTR_SIZE) / EFA_CHUNK_PAYLOAD_PTR_SIZE) 9940909f66SGal Pressman 10040909f66SGal Pressman #define EFA_CHUNK_USED_SIZE \ 10140909f66SGal Pressman ((EFA_PTRS_PER_CHUNK * EFA_CHUNK_PAYLOAD_PTR_SIZE) + EFA_CHUNK_PTR_SIZE) 10240909f66SGal Pressman 10340909f66SGal Pressman struct pbl_chunk { 10440909f66SGal Pressman dma_addr_t dma_addr; 10540909f66SGal Pressman u64 *buf; 10640909f66SGal Pressman u32 length; 10740909f66SGal Pressman }; 10840909f66SGal Pressman 10940909f66SGal Pressman struct pbl_chunk_list { 11040909f66SGal Pressman struct pbl_chunk *chunks; 11140909f66SGal Pressman unsigned int size; 11240909f66SGal Pressman }; 11340909f66SGal Pressman 11440909f66SGal Pressman struct pbl_context { 11540909f66SGal Pressman union { 11640909f66SGal Pressman struct { 11740909f66SGal Pressman dma_addr_t dma_addr; 11840909f66SGal Pressman } continuous; 11940909f66SGal Pressman struct { 12040909f66SGal Pressman u32 pbl_buf_size_in_pages; 12140909f66SGal Pressman struct scatterlist *sgl; 12240909f66SGal Pressman int sg_dma_cnt; 12340909f66SGal Pressman struct pbl_chunk_list chunk_list; 12440909f66SGal Pressman } indirect; 12540909f66SGal Pressman } phys; 12640909f66SGal Pressman u64 *pbl_buf; 12740909f66SGal Pressman u32 pbl_buf_size_in_bytes; 12840909f66SGal Pressman u8 physically_continuous; 12940909f66SGal Pressman }; 13040909f66SGal Pressman 13140909f66SGal Pressman static inline struct efa_dev *to_edev(struct ib_device *ibdev) 13240909f66SGal Pressman { 13340909f66SGal Pressman return container_of(ibdev, struct efa_dev, ibdev); 13440909f66SGal Pressman } 13540909f66SGal Pressman 13640909f66SGal Pressman static inline struct efa_ucontext *to_eucontext(struct ib_ucontext *ibucontext) 13740909f66SGal Pressman { 13840909f66SGal Pressman return container_of(ibucontext, struct efa_ucontext, ibucontext); 13940909f66SGal Pressman } 14040909f66SGal Pressman 14140909f66SGal Pressman static inline struct efa_pd *to_epd(struct ib_pd *ibpd) 14240909f66SGal Pressman { 14340909f66SGal Pressman return container_of(ibpd, struct efa_pd, ibpd); 14440909f66SGal Pressman } 14540909f66SGal Pressman 14640909f66SGal Pressman static inline struct efa_mr *to_emr(struct ib_mr *ibmr) 14740909f66SGal Pressman { 14840909f66SGal Pressman return container_of(ibmr, struct efa_mr, ibmr); 14940909f66SGal Pressman } 15040909f66SGal Pressman 15140909f66SGal Pressman static inline struct efa_qp *to_eqp(struct ib_qp *ibqp) 15240909f66SGal Pressman { 15340909f66SGal Pressman return container_of(ibqp, struct efa_qp, ibqp); 15440909f66SGal Pressman } 15540909f66SGal Pressman 15640909f66SGal Pressman static inline struct efa_cq *to_ecq(struct ib_cq *ibcq) 15740909f66SGal Pressman { 15840909f66SGal Pressman return container_of(ibcq, struct efa_cq, ibcq); 15940909f66SGal Pressman } 16040909f66SGal Pressman 16140909f66SGal Pressman static inline struct efa_ah *to_eah(struct ib_ah *ibah) 16240909f66SGal Pressman { 16340909f66SGal Pressman return container_of(ibah, struct efa_ah, ibah); 16440909f66SGal Pressman } 16540909f66SGal Pressman 166e84d3c18SMichal Kalderon static inline struct efa_user_mmap_entry * 167e84d3c18SMichal Kalderon to_emmap(struct rdma_user_mmap_entry *rdma_entry) 168e84d3c18SMichal Kalderon { 169e84d3c18SMichal Kalderon return container_of(rdma_entry, struct efa_user_mmap_entry, rdma_entry); 170e84d3c18SMichal Kalderon } 171e84d3c18SMichal Kalderon 17234eb009fSGal Pressman #define EFA_DEV_CAP(dev, cap) \ 17334eb009fSGal Pressman ((dev)->dev_attr.device_caps & \ 17434eb009fSGal Pressman EFA_ADMIN_FEATURE_DEVICE_ATTR_DESC_##cap##_MASK) 175666e8ff5SDaniel Kranzdorf 17640909f66SGal Pressman #define is_reserved_cleared(reserved) \ 17740909f66SGal Pressman !memchr_inv(reserved, 0, sizeof(reserved)) 17840909f66SGal Pressman 17940909f66SGal Pressman static void *efa_zalloc_mapped(struct efa_dev *dev, dma_addr_t *dma_addr, 18040909f66SGal Pressman size_t size, enum dma_data_direction dir) 18140909f66SGal Pressman { 18240909f66SGal Pressman void *addr; 18340909f66SGal Pressman 18440909f66SGal Pressman addr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 18540909f66SGal Pressman if (!addr) 18640909f66SGal Pressman return NULL; 18740909f66SGal Pressman 18840909f66SGal Pressman *dma_addr = dma_map_single(&dev->pdev->dev, addr, size, dir); 18940909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, *dma_addr)) { 19040909f66SGal Pressman ibdev_err(&dev->ibdev, "Failed to map DMA address\n"); 19140909f66SGal Pressman free_pages_exact(addr, size); 19240909f66SGal Pressman return NULL; 19340909f66SGal Pressman } 19440909f66SGal Pressman 19540909f66SGal Pressman return addr; 19640909f66SGal Pressman } 19740909f66SGal Pressman 198ff6629f8SGal Pressman static void efa_free_mapped(struct efa_dev *dev, void *cpu_addr, 199ff6629f8SGal Pressman dma_addr_t dma_addr, 200ff6629f8SGal Pressman size_t size, enum dma_data_direction dir) 201ff6629f8SGal Pressman { 202ff6629f8SGal Pressman dma_unmap_single(&dev->pdev->dev, dma_addr, size, dir); 203ff6629f8SGal Pressman free_pages_exact(cpu_addr, size); 204ff6629f8SGal Pressman } 205ff6629f8SGal Pressman 20640909f66SGal Pressman int efa_query_device(struct ib_device *ibdev, 20740909f66SGal Pressman struct ib_device_attr *props, 20840909f66SGal Pressman struct ib_udata *udata) 20940909f66SGal Pressman { 21040909f66SGal Pressman struct efa_com_get_device_attr_result *dev_attr; 21140909f66SGal Pressman struct efa_ibv_ex_query_device_resp resp = {}; 21240909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 21340909f66SGal Pressman int err; 21440909f66SGal Pressman 21540909f66SGal Pressman if (udata && udata->inlen && 21640909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 21740909f66SGal Pressman ibdev_dbg(ibdev, 21840909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 21940909f66SGal Pressman return -EINVAL; 22040909f66SGal Pressman } 22140909f66SGal Pressman 22240909f66SGal Pressman dev_attr = &dev->dev_attr; 22340909f66SGal Pressman 22440909f66SGal Pressman memset(props, 0, sizeof(*props)); 22540909f66SGal Pressman props->max_mr_size = dev_attr->max_mr_pages * PAGE_SIZE; 22640909f66SGal Pressman props->page_size_cap = dev_attr->page_size_cap; 22740909f66SGal Pressman props->vendor_id = dev->pdev->vendor; 22840909f66SGal Pressman props->vendor_part_id = dev->pdev->device; 22940909f66SGal Pressman props->hw_ver = dev->pdev->subsystem_device; 23040909f66SGal Pressman props->max_qp = dev_attr->max_qp; 23140909f66SGal Pressman props->max_cq = dev_attr->max_cq; 23240909f66SGal Pressman props->max_pd = dev_attr->max_pd; 23340909f66SGal Pressman props->max_mr = dev_attr->max_mr; 23440909f66SGal Pressman props->max_ah = dev_attr->max_ah; 23540909f66SGal Pressman props->max_cqe = dev_attr->max_cq_depth; 23640909f66SGal Pressman props->max_qp_wr = min_t(u32, dev_attr->max_sq_depth, 23740909f66SGal Pressman dev_attr->max_rq_depth); 23840909f66SGal Pressman props->max_send_sge = dev_attr->max_sq_sge; 23940909f66SGal Pressman props->max_recv_sge = dev_attr->max_rq_sge; 240666e8ff5SDaniel Kranzdorf props->max_sge_rd = dev_attr->max_wr_rdma_sge; 2410133654dSGal Pressman props->max_pkeys = 1; 24240909f66SGal Pressman 24340909f66SGal Pressman if (udata && udata->outlen) { 24440909f66SGal Pressman resp.max_sq_sge = dev_attr->max_sq_sge; 24540909f66SGal Pressman resp.max_rq_sge = dev_attr->max_rq_sge; 24640909f66SGal Pressman resp.max_sq_wr = dev_attr->max_sq_depth; 24740909f66SGal Pressman resp.max_rq_wr = dev_attr->max_rq_depth; 248666e8ff5SDaniel Kranzdorf resp.max_rdma_size = dev_attr->max_rdma_size; 249666e8ff5SDaniel Kranzdorf 250dc13fbf7SMichael Margolin resp.device_caps |= EFA_QUERY_DEVICE_CAPS_CQ_WITH_SGID; 25134eb009fSGal Pressman if (EFA_DEV_CAP(dev, RDMA_READ)) 252666e8ff5SDaniel Kranzdorf resp.device_caps |= EFA_QUERY_DEVICE_CAPS_RDMA_READ; 25340909f66SGal Pressman 254a4e6a1ddSGal Pressman if (EFA_DEV_CAP(dev, RNR_RETRY)) 255a4e6a1ddSGal Pressman resp.device_caps |= EFA_QUERY_DEVICE_CAPS_RNR_RETRY; 256a4e6a1ddSGal Pressman 2576dddd939SYonatan Nachum if (EFA_DEV_CAP(dev, DATA_POLLING_128)) 2586dddd939SYonatan Nachum resp.device_caps |= EFA_QUERY_DEVICE_CAPS_DATA_POLLING_128; 2596dddd939SYonatan Nachum 260531094dcSYonatan Nachum if (EFA_DEV_CAP(dev, RDMA_WRITE)) 261531094dcSYonatan Nachum resp.device_caps |= EFA_QUERY_DEVICE_CAPS_RDMA_WRITE; 262531094dcSYonatan Nachum 2632a152512SGal Pressman if (dev->neqs) 2642a152512SGal Pressman resp.device_caps |= EFA_QUERY_DEVICE_CAPS_CQ_NOTIFICATIONS; 2652a152512SGal Pressman 26640909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 26740909f66SGal Pressman min(sizeof(resp), udata->outlen)); 26840909f66SGal Pressman if (err) { 26940909f66SGal Pressman ibdev_dbg(ibdev, 27040909f66SGal Pressman "Failed to copy udata for query_device\n"); 27140909f66SGal Pressman return err; 27240909f66SGal Pressman } 27340909f66SGal Pressman } 27440909f66SGal Pressman 27540909f66SGal Pressman return 0; 27640909f66SGal Pressman } 27740909f66SGal Pressman 2781fb7f897SMark Bloch int efa_query_port(struct ib_device *ibdev, u32 port, 27940909f66SGal Pressman struct ib_port_attr *props) 28040909f66SGal Pressman { 28140909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 28240909f66SGal Pressman 28340909f66SGal Pressman props->lmc = 1; 28440909f66SGal Pressman 28540909f66SGal Pressman props->state = IB_PORT_ACTIVE; 28672a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 28740909f66SGal Pressman props->gid_tbl_len = 1; 28840909f66SGal Pressman props->pkey_tbl_len = 1; 28940909f66SGal Pressman props->active_speed = IB_SPEED_EDR; 29040909f66SGal Pressman props->active_width = IB_WIDTH_4X; 291bcf7cc53SGal Pressman props->max_mtu = ib_mtu_int_to_enum(dev->dev_attr.mtu); 292bcf7cc53SGal Pressman props->active_mtu = ib_mtu_int_to_enum(dev->dev_attr.mtu); 293bcf7cc53SGal Pressman props->max_msg_sz = dev->dev_attr.mtu; 29440909f66SGal Pressman props->max_vl_num = 1; 29540909f66SGal Pressman 29640909f66SGal Pressman return 0; 29740909f66SGal Pressman } 29840909f66SGal Pressman 29940909f66SGal Pressman int efa_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, 30040909f66SGal Pressman int qp_attr_mask, 30140909f66SGal Pressman struct ib_qp_init_attr *qp_init_attr) 30240909f66SGal Pressman { 30340909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->device); 30440909f66SGal Pressman struct efa_com_query_qp_params params = {}; 30540909f66SGal Pressman struct efa_com_query_qp_result result; 30640909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 30740909f66SGal Pressman int err; 30840909f66SGal Pressman 30940909f66SGal Pressman #define EFA_QUERY_QP_SUPP_MASK \ 31040909f66SGal Pressman (IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT | \ 311a4e6a1ddSGal Pressman IB_QP_QKEY | IB_QP_SQ_PSN | IB_QP_CAP | IB_QP_RNR_RETRY) 31240909f66SGal Pressman 31340909f66SGal Pressman if (qp_attr_mask & ~EFA_QUERY_QP_SUPP_MASK) { 31440909f66SGal Pressman ibdev_dbg(&dev->ibdev, 31540909f66SGal Pressman "Unsupported qp_attr_mask[%#x] supported[%#x]\n", 31640909f66SGal Pressman qp_attr_mask, EFA_QUERY_QP_SUPP_MASK); 31740909f66SGal Pressman return -EOPNOTSUPP; 31840909f66SGal Pressman } 31940909f66SGal Pressman 32040909f66SGal Pressman memset(qp_attr, 0, sizeof(*qp_attr)); 32140909f66SGal Pressman memset(qp_init_attr, 0, sizeof(*qp_init_attr)); 32240909f66SGal Pressman 32340909f66SGal Pressman params.qp_handle = qp->qp_handle; 32440909f66SGal Pressman err = efa_com_query_qp(&dev->edev, ¶ms, &result); 32540909f66SGal Pressman if (err) 32640909f66SGal Pressman return err; 32740909f66SGal Pressman 32840909f66SGal Pressman qp_attr->qp_state = result.qp_state; 32940909f66SGal Pressman qp_attr->qkey = result.qkey; 33040909f66SGal Pressman qp_attr->sq_psn = result.sq_psn; 33140909f66SGal Pressman qp_attr->sq_draining = result.sq_draining; 33240909f66SGal Pressman qp_attr->port_num = 1; 333a4e6a1ddSGal Pressman qp_attr->rnr_retry = result.rnr_retry; 33440909f66SGal Pressman 33540909f66SGal Pressman qp_attr->cap.max_send_wr = qp->max_send_wr; 33640909f66SGal Pressman qp_attr->cap.max_recv_wr = qp->max_recv_wr; 33740909f66SGal Pressman qp_attr->cap.max_send_sge = qp->max_send_sge; 33840909f66SGal Pressman qp_attr->cap.max_recv_sge = qp->max_recv_sge; 33940909f66SGal Pressman qp_attr->cap.max_inline_data = qp->max_inline_data; 34040909f66SGal Pressman 34140909f66SGal Pressman qp_init_attr->qp_type = ibqp->qp_type; 34240909f66SGal Pressman qp_init_attr->recv_cq = ibqp->recv_cq; 34340909f66SGal Pressman qp_init_attr->send_cq = ibqp->send_cq; 34440909f66SGal Pressman qp_init_attr->qp_context = ibqp->qp_context; 34540909f66SGal Pressman qp_init_attr->cap = qp_attr->cap; 34640909f66SGal Pressman 34740909f66SGal Pressman return 0; 34840909f66SGal Pressman } 34940909f66SGal Pressman 3501fb7f897SMark Bloch int efa_query_gid(struct ib_device *ibdev, u32 port, int index, 35140909f66SGal Pressman union ib_gid *gid) 35240909f66SGal Pressman { 35340909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 35440909f66SGal Pressman 355bcf7cc53SGal Pressman memcpy(gid->raw, dev->dev_attr.addr, sizeof(dev->dev_attr.addr)); 35640909f66SGal Pressman 35740909f66SGal Pressman return 0; 35840909f66SGal Pressman } 35940909f66SGal Pressman 3601fb7f897SMark Bloch int efa_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 36140909f66SGal Pressman u16 *pkey) 36240909f66SGal Pressman { 36340909f66SGal Pressman if (index > 0) 36440909f66SGal Pressman return -EINVAL; 36540909f66SGal Pressman 36640909f66SGal Pressman *pkey = 0xffff; 36740909f66SGal Pressman return 0; 36840909f66SGal Pressman } 36940909f66SGal Pressman 37040909f66SGal Pressman static int efa_pd_dealloc(struct efa_dev *dev, u16 pdn) 37140909f66SGal Pressman { 37240909f66SGal Pressman struct efa_com_dealloc_pd_params params = { 37340909f66SGal Pressman .pdn = pdn, 37440909f66SGal Pressman }; 37540909f66SGal Pressman 37640909f66SGal Pressman return efa_com_dealloc_pd(&dev->edev, ¶ms); 37740909f66SGal Pressman } 37840909f66SGal Pressman 37940909f66SGal Pressman int efa_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 38040909f66SGal Pressman { 38140909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 38240909f66SGal Pressman struct efa_ibv_alloc_pd_resp resp = {}; 38340909f66SGal Pressman struct efa_com_alloc_pd_result result; 38440909f66SGal Pressman struct efa_pd *pd = to_epd(ibpd); 38540909f66SGal Pressman int err; 38640909f66SGal Pressman 38740909f66SGal Pressman if (udata->inlen && 38840909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 38940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 39040909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 39140909f66SGal Pressman err = -EINVAL; 39240909f66SGal Pressman goto err_out; 39340909f66SGal Pressman } 39440909f66SGal Pressman 39540909f66SGal Pressman err = efa_com_alloc_pd(&dev->edev, &result); 39640909f66SGal Pressman if (err) 39740909f66SGal Pressman goto err_out; 39840909f66SGal Pressman 39940909f66SGal Pressman pd->pdn = result.pdn; 40040909f66SGal Pressman resp.pdn = result.pdn; 40140909f66SGal Pressman 40240909f66SGal Pressman if (udata->outlen) { 40340909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 40440909f66SGal Pressman min(sizeof(resp), udata->outlen)); 40540909f66SGal Pressman if (err) { 40640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 40740909f66SGal Pressman "Failed to copy udata for alloc_pd\n"); 40840909f66SGal Pressman goto err_dealloc_pd; 40940909f66SGal Pressman } 41040909f66SGal Pressman } 41140909f66SGal Pressman 41240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Allocated pd[%d]\n", pd->pdn); 41340909f66SGal Pressman 41440909f66SGal Pressman return 0; 41540909f66SGal Pressman 41640909f66SGal Pressman err_dealloc_pd: 41740909f66SGal Pressman efa_pd_dealloc(dev, result.pdn); 41840909f66SGal Pressman err_out: 419215b88acSGal Pressman atomic64_inc(&dev->stats.alloc_pd_err); 42040909f66SGal Pressman return err; 42140909f66SGal Pressman } 42240909f66SGal Pressman 42391a7c58fSLeon Romanovsky int efa_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 42440909f66SGal Pressman { 42540909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 42640909f66SGal Pressman struct efa_pd *pd = to_epd(ibpd); 42740909f66SGal Pressman 42840909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Dealloc pd[%d]\n", pd->pdn); 42940909f66SGal Pressman efa_pd_dealloc(dev, pd->pdn); 43091a7c58fSLeon Romanovsky return 0; 43140909f66SGal Pressman } 43240909f66SGal Pressman 43340909f66SGal Pressman static int efa_destroy_qp_handle(struct efa_dev *dev, u32 qp_handle) 43440909f66SGal Pressman { 43540909f66SGal Pressman struct efa_com_destroy_qp_params params = { .qp_handle = qp_handle }; 43640909f66SGal Pressman 43740909f66SGal Pressman return efa_com_destroy_qp(&dev->edev, ¶ms); 43840909f66SGal Pressman } 43940909f66SGal Pressman 4400428c6efSGal Pressman static void efa_qp_user_mmap_entries_remove(struct efa_qp *qp) 441e84d3c18SMichal Kalderon { 442e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->rq_mmap_entry); 443e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->rq_db_mmap_entry); 444e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->llq_desc_mmap_entry); 445e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->sq_db_mmap_entry); 446e84d3c18SMichal Kalderon } 447e84d3c18SMichal Kalderon 44840909f66SGal Pressman int efa_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 44940909f66SGal Pressman { 45040909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->pd->device); 45140909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 45240909f66SGal Pressman int err; 45340909f66SGal Pressman 45440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Destroy qp[%u]\n", ibqp->qp_num); 455ff6629f8SGal Pressman 456ff6629f8SGal Pressman efa_qp_user_mmap_entries_remove(qp); 457ff6629f8SGal Pressman 45840909f66SGal Pressman err = efa_destroy_qp_handle(dev, qp->qp_handle); 45940909f66SGal Pressman if (err) 46040909f66SGal Pressman return err; 46140909f66SGal Pressman 46240909f66SGal Pressman if (qp->rq_cpu_addr) { 46340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 46440909f66SGal Pressman "qp->cpu_addr[0x%p] freed: size[%lu], dma[%pad]\n", 46540909f66SGal Pressman qp->rq_cpu_addr, qp->rq_size, 46640909f66SGal Pressman &qp->rq_dma_addr); 467ff6629f8SGal Pressman efa_free_mapped(dev, qp->rq_cpu_addr, qp->rq_dma_addr, 468ff6629f8SGal Pressman qp->rq_size, DMA_TO_DEVICE); 46940909f66SGal Pressman } 47040909f66SGal Pressman 47140909f66SGal Pressman return 0; 47240909f66SGal Pressman } 47340909f66SGal Pressman 474e84d3c18SMichal Kalderon static struct rdma_user_mmap_entry* 475e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(struct ib_ucontext *ucontext, 476e84d3c18SMichal Kalderon u64 address, size_t length, 477e84d3c18SMichal Kalderon u8 mmap_flag, u64 *offset) 478e84d3c18SMichal Kalderon { 479e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL); 480e84d3c18SMichal Kalderon int err; 481e84d3c18SMichal Kalderon 482e84d3c18SMichal Kalderon if (!entry) 483e84d3c18SMichal Kalderon return NULL; 484e84d3c18SMichal Kalderon 485e84d3c18SMichal Kalderon entry->address = address; 486e84d3c18SMichal Kalderon entry->mmap_flag = mmap_flag; 487e84d3c18SMichal Kalderon 488e84d3c18SMichal Kalderon err = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry, 489e84d3c18SMichal Kalderon length); 490e84d3c18SMichal Kalderon if (err) { 491e84d3c18SMichal Kalderon kfree(entry); 492e84d3c18SMichal Kalderon return NULL; 493e84d3c18SMichal Kalderon } 494e84d3c18SMichal Kalderon *offset = rdma_user_mmap_get_offset(&entry->rdma_entry); 495e84d3c18SMichal Kalderon 496e84d3c18SMichal Kalderon return &entry->rdma_entry; 497e84d3c18SMichal Kalderon } 498e84d3c18SMichal Kalderon 49940909f66SGal Pressman static int qp_mmap_entries_setup(struct efa_qp *qp, 50040909f66SGal Pressman struct efa_dev *dev, 50140909f66SGal Pressman struct efa_ucontext *ucontext, 50240909f66SGal Pressman struct efa_com_create_qp_params *params, 50340909f66SGal Pressman struct efa_ibv_create_qp_resp *resp) 50440909f66SGal Pressman { 505e84d3c18SMichal Kalderon size_t length; 506e84d3c18SMichal Kalderon u64 address; 507e84d3c18SMichal Kalderon 508e84d3c18SMichal Kalderon address = dev->db_bar_addr + resp->sq_db_offset; 509e84d3c18SMichal Kalderon qp->sq_db_mmap_entry = 510e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 511e84d3c18SMichal Kalderon address, 512e84d3c18SMichal Kalderon PAGE_SIZE, EFA_MMAP_IO_NC, 513e84d3c18SMichal Kalderon &resp->sq_db_mmap_key); 514e84d3c18SMichal Kalderon if (!qp->sq_db_mmap_entry) 51540909f66SGal Pressman return -ENOMEM; 51640909f66SGal Pressman 51740909f66SGal Pressman resp->sq_db_offset &= ~PAGE_MASK; 51840909f66SGal Pressman 519e84d3c18SMichal Kalderon address = dev->mem_bar_addr + resp->llq_desc_offset; 520e84d3c18SMichal Kalderon length = PAGE_ALIGN(params->sq_ring_size_in_bytes + 521e84d3c18SMichal Kalderon (resp->llq_desc_offset & ~PAGE_MASK)); 522e84d3c18SMichal Kalderon 523e84d3c18SMichal Kalderon qp->llq_desc_mmap_entry = 524e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 525e84d3c18SMichal Kalderon address, length, 526e84d3c18SMichal Kalderon EFA_MMAP_IO_WC, 527e84d3c18SMichal Kalderon &resp->llq_desc_mmap_key); 528e84d3c18SMichal Kalderon if (!qp->llq_desc_mmap_entry) 529e84d3c18SMichal Kalderon goto err_remove_mmap; 53040909f66SGal Pressman 53140909f66SGal Pressman resp->llq_desc_offset &= ~PAGE_MASK; 53240909f66SGal Pressman 53340909f66SGal Pressman if (qp->rq_size) { 534e84d3c18SMichal Kalderon address = dev->db_bar_addr + resp->rq_db_offset; 535e84d3c18SMichal Kalderon 536e84d3c18SMichal Kalderon qp->rq_db_mmap_entry = 537e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 538e84d3c18SMichal Kalderon address, PAGE_SIZE, 539e84d3c18SMichal Kalderon EFA_MMAP_IO_NC, 540e84d3c18SMichal Kalderon &resp->rq_db_mmap_key); 541e84d3c18SMichal Kalderon if (!qp->rq_db_mmap_entry) 542e84d3c18SMichal Kalderon goto err_remove_mmap; 54340909f66SGal Pressman 54440909f66SGal Pressman resp->rq_db_offset &= ~PAGE_MASK; 54540909f66SGal Pressman 546e84d3c18SMichal Kalderon address = virt_to_phys(qp->rq_cpu_addr); 547e84d3c18SMichal Kalderon qp->rq_mmap_entry = 548e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 549e84d3c18SMichal Kalderon address, qp->rq_size, 550e84d3c18SMichal Kalderon EFA_MMAP_DMA_PAGE, 551e84d3c18SMichal Kalderon &resp->rq_mmap_key); 552e84d3c18SMichal Kalderon if (!qp->rq_mmap_entry) 553e84d3c18SMichal Kalderon goto err_remove_mmap; 55440909f66SGal Pressman 55540909f66SGal Pressman resp->rq_mmap_size = qp->rq_size; 55640909f66SGal Pressman } 55740909f66SGal Pressman 55840909f66SGal Pressman return 0; 559e84d3c18SMichal Kalderon 560e84d3c18SMichal Kalderon err_remove_mmap: 5610428c6efSGal Pressman efa_qp_user_mmap_entries_remove(qp); 562e84d3c18SMichal Kalderon 563e84d3c18SMichal Kalderon return -ENOMEM; 56440909f66SGal Pressman } 56540909f66SGal Pressman 56640909f66SGal Pressman static int efa_qp_validate_cap(struct efa_dev *dev, 56740909f66SGal Pressman struct ib_qp_init_attr *init_attr) 56840909f66SGal Pressman { 56940909f66SGal Pressman if (init_attr->cap.max_send_wr > dev->dev_attr.max_sq_depth) { 57040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 57140909f66SGal Pressman "qp: requested send wr[%u] exceeds the max[%u]\n", 57240909f66SGal Pressman init_attr->cap.max_send_wr, 57340909f66SGal Pressman dev->dev_attr.max_sq_depth); 57440909f66SGal Pressman return -EINVAL; 57540909f66SGal Pressman } 57640909f66SGal Pressman if (init_attr->cap.max_recv_wr > dev->dev_attr.max_rq_depth) { 57740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 57840909f66SGal Pressman "qp: requested receive wr[%u] exceeds the max[%u]\n", 57940909f66SGal Pressman init_attr->cap.max_recv_wr, 58040909f66SGal Pressman dev->dev_attr.max_rq_depth); 58140909f66SGal Pressman return -EINVAL; 58240909f66SGal Pressman } 58340909f66SGal Pressman if (init_attr->cap.max_send_sge > dev->dev_attr.max_sq_sge) { 58440909f66SGal Pressman ibdev_dbg(&dev->ibdev, 58540909f66SGal Pressman "qp: requested sge send[%u] exceeds the max[%u]\n", 58640909f66SGal Pressman init_attr->cap.max_send_sge, dev->dev_attr.max_sq_sge); 58740909f66SGal Pressman return -EINVAL; 58840909f66SGal Pressman } 58940909f66SGal Pressman if (init_attr->cap.max_recv_sge > dev->dev_attr.max_rq_sge) { 59040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 59140909f66SGal Pressman "qp: requested sge recv[%u] exceeds the max[%u]\n", 59240909f66SGal Pressman init_attr->cap.max_recv_sge, dev->dev_attr.max_rq_sge); 59340909f66SGal Pressman return -EINVAL; 59440909f66SGal Pressman } 59540909f66SGal Pressman if (init_attr->cap.max_inline_data > dev->dev_attr.inline_buf_size) { 59640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 59740909f66SGal Pressman "qp: requested inline data[%u] exceeds the max[%u]\n", 59840909f66SGal Pressman init_attr->cap.max_inline_data, 59940909f66SGal Pressman dev->dev_attr.inline_buf_size); 60040909f66SGal Pressman return -EINVAL; 60140909f66SGal Pressman } 60240909f66SGal Pressman 60340909f66SGal Pressman return 0; 60440909f66SGal Pressman } 60540909f66SGal Pressman 60640909f66SGal Pressman static int efa_qp_validate_attr(struct efa_dev *dev, 60740909f66SGal Pressman struct ib_qp_init_attr *init_attr) 60840909f66SGal Pressman { 60940909f66SGal Pressman if (init_attr->qp_type != IB_QPT_DRIVER && 61040909f66SGal Pressman init_attr->qp_type != IB_QPT_UD) { 61140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 61240909f66SGal Pressman "Unsupported qp type %d\n", init_attr->qp_type); 61340909f66SGal Pressman return -EOPNOTSUPP; 61440909f66SGal Pressman } 61540909f66SGal Pressman 61640909f66SGal Pressman if (init_attr->srq) { 61740909f66SGal Pressman ibdev_dbg(&dev->ibdev, "SRQ is not supported\n"); 61840909f66SGal Pressman return -EOPNOTSUPP; 61940909f66SGal Pressman } 62040909f66SGal Pressman 62140909f66SGal Pressman if (init_attr->create_flags) { 62240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Unsupported create flags\n"); 62340909f66SGal Pressman return -EOPNOTSUPP; 62440909f66SGal Pressman } 62540909f66SGal Pressman 62640909f66SGal Pressman return 0; 62740909f66SGal Pressman } 62840909f66SGal Pressman 629514aee66SLeon Romanovsky int efa_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr, 63040909f66SGal Pressman struct ib_udata *udata) 63140909f66SGal Pressman { 63240909f66SGal Pressman struct efa_com_create_qp_params create_qp_params = {}; 63340909f66SGal Pressman struct efa_com_create_qp_result create_qp_resp; 634514aee66SLeon Romanovsky struct efa_dev *dev = to_edev(ibqp->device); 63540909f66SGal Pressman struct efa_ibv_create_qp_resp resp = {}; 63640909f66SGal Pressman struct efa_ibv_create_qp cmd = {}; 637514aee66SLeon Romanovsky struct efa_qp *qp = to_eqp(ibqp); 63840909f66SGal Pressman struct efa_ucontext *ucontext; 63940909f66SGal Pressman int err; 64040909f66SGal Pressman 64140909f66SGal Pressman ucontext = rdma_udata_to_drv_context(udata, struct efa_ucontext, 64240909f66SGal Pressman ibucontext); 64340909f66SGal Pressman 64440909f66SGal Pressman err = efa_qp_validate_cap(dev, init_attr); 64540909f66SGal Pressman if (err) 64640909f66SGal Pressman goto err_out; 64740909f66SGal Pressman 64840909f66SGal Pressman err = efa_qp_validate_attr(dev, init_attr); 64940909f66SGal Pressman if (err) 65040909f66SGal Pressman goto err_out; 65140909f66SGal Pressman 652fa8a44f6SLeon Romanovsky if (offsetofend(typeof(cmd), driver_qp_type) > udata->inlen) { 65340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 65440909f66SGal Pressman "Incompatible ABI params, no input udata\n"); 65540909f66SGal Pressman err = -EINVAL; 65640909f66SGal Pressman goto err_out; 65740909f66SGal Pressman } 65840909f66SGal Pressman 65940909f66SGal Pressman if (udata->inlen > sizeof(cmd) && 66040909f66SGal Pressman !ib_is_udata_cleared(udata, sizeof(cmd), 66140909f66SGal Pressman udata->inlen - sizeof(cmd))) { 66240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 66340909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 66440909f66SGal Pressman err = -EINVAL; 66540909f66SGal Pressman goto err_out; 66640909f66SGal Pressman } 66740909f66SGal Pressman 66840909f66SGal Pressman err = ib_copy_from_udata(&cmd, udata, 66940909f66SGal Pressman min(sizeof(cmd), udata->inlen)); 67040909f66SGal Pressman if (err) { 67140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 67240909f66SGal Pressman "Cannot copy udata for create_qp\n"); 67340909f66SGal Pressman goto err_out; 67440909f66SGal Pressman } 67540909f66SGal Pressman 67640909f66SGal Pressman if (cmd.comp_mask) { 67740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 67840909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 67940909f66SGal Pressman err = -EINVAL; 68040909f66SGal Pressman goto err_out; 68140909f66SGal Pressman } 68240909f66SGal Pressman 68340909f66SGal Pressman create_qp_params.uarn = ucontext->uarn; 684514aee66SLeon Romanovsky create_qp_params.pd = to_epd(ibqp->pd)->pdn; 68540909f66SGal Pressman 68640909f66SGal Pressman if (init_attr->qp_type == IB_QPT_UD) { 68740909f66SGal Pressman create_qp_params.qp_type = EFA_ADMIN_QP_TYPE_UD; 68840909f66SGal Pressman } else if (cmd.driver_qp_type == EFA_QP_DRIVER_TYPE_SRD) { 68940909f66SGal Pressman create_qp_params.qp_type = EFA_ADMIN_QP_TYPE_SRD; 69040909f66SGal Pressman } else { 69140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 69240909f66SGal Pressman "Unsupported qp type %d driver qp type %d\n", 69340909f66SGal Pressman init_attr->qp_type, cmd.driver_qp_type); 69440909f66SGal Pressman err = -EOPNOTSUPP; 695514aee66SLeon Romanovsky goto err_out; 69640909f66SGal Pressman } 69740909f66SGal Pressman 69840909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Create QP: qp type %d driver qp type %#x\n", 69940909f66SGal Pressman init_attr->qp_type, cmd.driver_qp_type); 70040909f66SGal Pressman create_qp_params.send_cq_idx = to_ecq(init_attr->send_cq)->cq_idx; 70140909f66SGal Pressman create_qp_params.recv_cq_idx = to_ecq(init_attr->recv_cq)->cq_idx; 70240909f66SGal Pressman create_qp_params.sq_depth = init_attr->cap.max_send_wr; 70340909f66SGal Pressman create_qp_params.sq_ring_size_in_bytes = cmd.sq_ring_size; 70440909f66SGal Pressman 70540909f66SGal Pressman create_qp_params.rq_depth = init_attr->cap.max_recv_wr; 70640909f66SGal Pressman create_qp_params.rq_ring_size_in_bytes = cmd.rq_ring_size; 70740909f66SGal Pressman qp->rq_size = PAGE_ALIGN(create_qp_params.rq_ring_size_in_bytes); 70840909f66SGal Pressman if (qp->rq_size) { 70940909f66SGal Pressman qp->rq_cpu_addr = efa_zalloc_mapped(dev, &qp->rq_dma_addr, 71040909f66SGal Pressman qp->rq_size, DMA_TO_DEVICE); 71140909f66SGal Pressman if (!qp->rq_cpu_addr) { 71240909f66SGal Pressman err = -ENOMEM; 713514aee66SLeon Romanovsky goto err_out; 71440909f66SGal Pressman } 71540909f66SGal Pressman 71640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 71740909f66SGal Pressman "qp->cpu_addr[0x%p] allocated: size[%lu], dma[%pad]\n", 71840909f66SGal Pressman qp->rq_cpu_addr, qp->rq_size, &qp->rq_dma_addr); 71940909f66SGal Pressman create_qp_params.rq_base_addr = qp->rq_dma_addr; 72040909f66SGal Pressman } 72140909f66SGal Pressman 72240909f66SGal Pressman err = efa_com_create_qp(&dev->edev, &create_qp_params, 72340909f66SGal Pressman &create_qp_resp); 72440909f66SGal Pressman if (err) 72540909f66SGal Pressman goto err_free_mapped; 72640909f66SGal Pressman 72740909f66SGal Pressman resp.sq_db_offset = create_qp_resp.sq_db_offset; 72840909f66SGal Pressman resp.rq_db_offset = create_qp_resp.rq_db_offset; 72940909f66SGal Pressman resp.llq_desc_offset = create_qp_resp.llq_descriptors_offset; 73040909f66SGal Pressman resp.send_sub_cq_idx = create_qp_resp.send_sub_cq_idx; 73140909f66SGal Pressman resp.recv_sub_cq_idx = create_qp_resp.recv_sub_cq_idx; 73240909f66SGal Pressman 73340909f66SGal Pressman err = qp_mmap_entries_setup(qp, dev, ucontext, &create_qp_params, 73440909f66SGal Pressman &resp); 73540909f66SGal Pressman if (err) 73640909f66SGal Pressman goto err_destroy_qp; 73740909f66SGal Pressman 73840909f66SGal Pressman qp->qp_handle = create_qp_resp.qp_handle; 73940909f66SGal Pressman qp->ibqp.qp_num = create_qp_resp.qp_num; 74040909f66SGal Pressman qp->max_send_wr = init_attr->cap.max_send_wr; 74140909f66SGal Pressman qp->max_recv_wr = init_attr->cap.max_recv_wr; 74240909f66SGal Pressman qp->max_send_sge = init_attr->cap.max_send_sge; 74340909f66SGal Pressman qp->max_recv_sge = init_attr->cap.max_recv_sge; 74440909f66SGal Pressman qp->max_inline_data = init_attr->cap.max_inline_data; 74540909f66SGal Pressman 74640909f66SGal Pressman if (udata->outlen) { 74740909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 74840909f66SGal Pressman min(sizeof(resp), udata->outlen)); 74940909f66SGal Pressman if (err) { 75040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 75140909f66SGal Pressman "Failed to copy udata for qp[%u]\n", 75240909f66SGal Pressman create_qp_resp.qp_num); 753e84d3c18SMichal Kalderon goto err_remove_mmap_entries; 75440909f66SGal Pressman } 75540909f66SGal Pressman } 75640909f66SGal Pressman 75740909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Created qp[%d]\n", qp->ibqp.qp_num); 75840909f66SGal Pressman 759514aee66SLeon Romanovsky return 0; 76040909f66SGal Pressman 761e84d3c18SMichal Kalderon err_remove_mmap_entries: 7620428c6efSGal Pressman efa_qp_user_mmap_entries_remove(qp); 76340909f66SGal Pressman err_destroy_qp: 76440909f66SGal Pressman efa_destroy_qp_handle(dev, create_qp_resp.qp_handle); 76540909f66SGal Pressman err_free_mapped: 766ff6629f8SGal Pressman if (qp->rq_size) 767ff6629f8SGal Pressman efa_free_mapped(dev, qp->rq_cpu_addr, qp->rq_dma_addr, 768ff6629f8SGal Pressman qp->rq_size, DMA_TO_DEVICE); 76940909f66SGal Pressman err_out: 770215b88acSGal Pressman atomic64_inc(&dev->stats.create_qp_err); 771514aee66SLeon Romanovsky return err; 77240909f66SGal Pressman } 77340909f66SGal Pressman 77422c50e06SGal Pressman static const struct { 77522c50e06SGal Pressman int valid; 77622c50e06SGal Pressman enum ib_qp_attr_mask req_param; 77722c50e06SGal Pressman enum ib_qp_attr_mask opt_param; 77822c50e06SGal Pressman } srd_qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = { 77922c50e06SGal Pressman [IB_QPS_RESET] = { 78022c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 78122c50e06SGal Pressman [IB_QPS_INIT] = { 78222c50e06SGal Pressman .valid = 1, 78322c50e06SGal Pressman .req_param = IB_QP_PKEY_INDEX | 78422c50e06SGal Pressman IB_QP_PORT | 78522c50e06SGal Pressman IB_QP_QKEY, 78622c50e06SGal Pressman }, 78722c50e06SGal Pressman }, 78822c50e06SGal Pressman [IB_QPS_INIT] = { 78922c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 79022c50e06SGal Pressman [IB_QPS_ERR] = { .valid = 1 }, 79122c50e06SGal Pressman [IB_QPS_INIT] = { 79222c50e06SGal Pressman .valid = 1, 79322c50e06SGal Pressman .opt_param = IB_QP_PKEY_INDEX | 79422c50e06SGal Pressman IB_QP_PORT | 79522c50e06SGal Pressman IB_QP_QKEY, 79622c50e06SGal Pressman }, 79722c50e06SGal Pressman [IB_QPS_RTR] = { 79822c50e06SGal Pressman .valid = 1, 79922c50e06SGal Pressman .opt_param = IB_QP_PKEY_INDEX | 80022c50e06SGal Pressman IB_QP_QKEY, 80122c50e06SGal Pressman }, 80222c50e06SGal Pressman }, 80322c50e06SGal Pressman [IB_QPS_RTR] = { 80422c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 80522c50e06SGal Pressman [IB_QPS_ERR] = { .valid = 1 }, 80622c50e06SGal Pressman [IB_QPS_RTS] = { 80722c50e06SGal Pressman .valid = 1, 80822c50e06SGal Pressman .req_param = IB_QP_SQ_PSN, 80922c50e06SGal Pressman .opt_param = IB_QP_CUR_STATE | 810a4e6a1ddSGal Pressman IB_QP_QKEY | 811a4e6a1ddSGal Pressman IB_QP_RNR_RETRY, 812a4e6a1ddSGal Pressman 81322c50e06SGal Pressman } 81422c50e06SGal Pressman }, 81522c50e06SGal Pressman [IB_QPS_RTS] = { 81622c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 81722c50e06SGal Pressman [IB_QPS_ERR] = { .valid = 1 }, 81822c50e06SGal Pressman [IB_QPS_RTS] = { 81922c50e06SGal Pressman .valid = 1, 82022c50e06SGal Pressman .opt_param = IB_QP_CUR_STATE | 82122c50e06SGal Pressman IB_QP_QKEY, 82222c50e06SGal Pressman }, 82322c50e06SGal Pressman [IB_QPS_SQD] = { 82422c50e06SGal Pressman .valid = 1, 82522c50e06SGal Pressman .opt_param = IB_QP_EN_SQD_ASYNC_NOTIFY, 82622c50e06SGal Pressman }, 82722c50e06SGal Pressman }, 82822c50e06SGal Pressman [IB_QPS_SQD] = { 82922c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 83022c50e06SGal Pressman [IB_QPS_ERR] = { .valid = 1 }, 83122c50e06SGal Pressman [IB_QPS_RTS] = { 83222c50e06SGal Pressman .valid = 1, 83322c50e06SGal Pressman .opt_param = IB_QP_CUR_STATE | 83422c50e06SGal Pressman IB_QP_QKEY, 83522c50e06SGal Pressman }, 83622c50e06SGal Pressman [IB_QPS_SQD] = { 83722c50e06SGal Pressman .valid = 1, 83822c50e06SGal Pressman .opt_param = IB_QP_PKEY_INDEX | 83922c50e06SGal Pressman IB_QP_QKEY, 84022c50e06SGal Pressman } 84122c50e06SGal Pressman }, 84222c50e06SGal Pressman [IB_QPS_SQE] = { 84322c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 84422c50e06SGal Pressman [IB_QPS_ERR] = { .valid = 1 }, 84522c50e06SGal Pressman [IB_QPS_RTS] = { 84622c50e06SGal Pressman .valid = 1, 84722c50e06SGal Pressman .opt_param = IB_QP_CUR_STATE | 84822c50e06SGal Pressman IB_QP_QKEY, 84922c50e06SGal Pressman } 85022c50e06SGal Pressman }, 85122c50e06SGal Pressman [IB_QPS_ERR] = { 85222c50e06SGal Pressman [IB_QPS_RESET] = { .valid = 1 }, 85322c50e06SGal Pressman [IB_QPS_ERR] = { .valid = 1 }, 85422c50e06SGal Pressman } 85522c50e06SGal Pressman }; 85622c50e06SGal Pressman 85722c50e06SGal Pressman static bool efa_modify_srd_qp_is_ok(enum ib_qp_state cur_state, 85822c50e06SGal Pressman enum ib_qp_state next_state, 85922c50e06SGal Pressman enum ib_qp_attr_mask mask) 86022c50e06SGal Pressman { 86122c50e06SGal Pressman enum ib_qp_attr_mask req_param, opt_param; 86222c50e06SGal Pressman 86322c50e06SGal Pressman if (mask & IB_QP_CUR_STATE && 86422c50e06SGal Pressman cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS && 86522c50e06SGal Pressman cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE) 86622c50e06SGal Pressman return false; 86722c50e06SGal Pressman 86822c50e06SGal Pressman if (!srd_qp_state_table[cur_state][next_state].valid) 86922c50e06SGal Pressman return false; 87022c50e06SGal Pressman 87122c50e06SGal Pressman req_param = srd_qp_state_table[cur_state][next_state].req_param; 87222c50e06SGal Pressman opt_param = srd_qp_state_table[cur_state][next_state].opt_param; 87322c50e06SGal Pressman 87422c50e06SGal Pressman if ((mask & req_param) != req_param) 87522c50e06SGal Pressman return false; 87622c50e06SGal Pressman 87722c50e06SGal Pressman if (mask & ~(req_param | opt_param | IB_QP_STATE)) 87822c50e06SGal Pressman return false; 87922c50e06SGal Pressman 88022c50e06SGal Pressman return true; 88122c50e06SGal Pressman } 88222c50e06SGal Pressman 88340909f66SGal Pressman static int efa_modify_qp_validate(struct efa_dev *dev, struct efa_qp *qp, 88440909f66SGal Pressman struct ib_qp_attr *qp_attr, int qp_attr_mask, 88540909f66SGal Pressman enum ib_qp_state cur_state, 88640909f66SGal Pressman enum ib_qp_state new_state) 88740909f66SGal Pressman { 88822c50e06SGal Pressman int err; 88922c50e06SGal Pressman 89040909f66SGal Pressman #define EFA_MODIFY_QP_SUPP_MASK \ 89140909f66SGal Pressman (IB_QP_STATE | IB_QP_CUR_STATE | IB_QP_EN_SQD_ASYNC_NOTIFY | \ 892a4e6a1ddSGal Pressman IB_QP_PKEY_INDEX | IB_QP_PORT | IB_QP_QKEY | IB_QP_SQ_PSN | \ 893a4e6a1ddSGal Pressman IB_QP_RNR_RETRY) 89440909f66SGal Pressman 89540909f66SGal Pressman if (qp_attr_mask & ~EFA_MODIFY_QP_SUPP_MASK) { 89640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 89740909f66SGal Pressman "Unsupported qp_attr_mask[%#x] supported[%#x]\n", 89840909f66SGal Pressman qp_attr_mask, EFA_MODIFY_QP_SUPP_MASK); 89940909f66SGal Pressman return -EOPNOTSUPP; 90040909f66SGal Pressman } 90140909f66SGal Pressman 90222c50e06SGal Pressman if (qp->ibqp.qp_type == IB_QPT_DRIVER) 90322c50e06SGal Pressman err = !efa_modify_srd_qp_is_ok(cur_state, new_state, 90422c50e06SGal Pressman qp_attr_mask); 90522c50e06SGal Pressman else 90622c50e06SGal Pressman err = !ib_modify_qp_is_ok(cur_state, new_state, IB_QPT_UD, 90722c50e06SGal Pressman qp_attr_mask); 90822c50e06SGal Pressman 90922c50e06SGal Pressman if (err) { 91040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Invalid modify QP parameters\n"); 91140909f66SGal Pressman return -EINVAL; 91240909f66SGal Pressman } 91340909f66SGal Pressman 91440909f66SGal Pressman if ((qp_attr_mask & IB_QP_PORT) && qp_attr->port_num != 1) { 91540909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Can't change port num\n"); 91640909f66SGal Pressman return -EOPNOTSUPP; 91740909f66SGal Pressman } 91840909f66SGal Pressman 91940909f66SGal Pressman if ((qp_attr_mask & IB_QP_PKEY_INDEX) && qp_attr->pkey_index) { 92040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Can't change pkey index\n"); 92140909f66SGal Pressman return -EOPNOTSUPP; 92240909f66SGal Pressman } 92340909f66SGal Pressman 92440909f66SGal Pressman return 0; 92540909f66SGal Pressman } 92640909f66SGal Pressman 92740909f66SGal Pressman int efa_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, 92840909f66SGal Pressman int qp_attr_mask, struct ib_udata *udata) 92940909f66SGal Pressman { 93040909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->device); 93140909f66SGal Pressman struct efa_com_modify_qp_params params = {}; 93240909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 93340909f66SGal Pressman enum ib_qp_state cur_state; 93440909f66SGal Pressman enum ib_qp_state new_state; 93540909f66SGal Pressman int err; 93640909f66SGal Pressman 93726e990baSJason Gunthorpe if (qp_attr_mask & ~IB_QP_ATTR_STANDARD_BITS) 93826e990baSJason Gunthorpe return -EOPNOTSUPP; 93926e990baSJason Gunthorpe 94040909f66SGal Pressman if (udata->inlen && 94140909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 94240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 94340909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 94440909f66SGal Pressman return -EINVAL; 94540909f66SGal Pressman } 94640909f66SGal Pressman 94740909f66SGal Pressman cur_state = qp_attr_mask & IB_QP_CUR_STATE ? qp_attr->cur_qp_state : 94840909f66SGal Pressman qp->state; 94940909f66SGal Pressman new_state = qp_attr_mask & IB_QP_STATE ? qp_attr->qp_state : cur_state; 95040909f66SGal Pressman 95140909f66SGal Pressman err = efa_modify_qp_validate(dev, qp, qp_attr, qp_attr_mask, cur_state, 95240909f66SGal Pressman new_state); 95340909f66SGal Pressman if (err) 95440909f66SGal Pressman return err; 95540909f66SGal Pressman 95640909f66SGal Pressman params.qp_handle = qp->qp_handle; 95740909f66SGal Pressman 95840909f66SGal Pressman if (qp_attr_mask & IB_QP_STATE) { 959ab67baddSGal Pressman EFA_SET(¶ms.modify_mask, EFA_ADMIN_MODIFY_QP_CMD_QP_STATE, 960ab67baddSGal Pressman 1); 961ab67baddSGal Pressman EFA_SET(¶ms.modify_mask, 962ab67baddSGal Pressman EFA_ADMIN_MODIFY_QP_CMD_CUR_QP_STATE, 1); 96393416ab0SGal Pressman params.cur_qp_state = cur_state; 96493416ab0SGal Pressman params.qp_state = new_state; 96540909f66SGal Pressman } 96640909f66SGal Pressman 96740909f66SGal Pressman if (qp_attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) { 968ab67baddSGal Pressman EFA_SET(¶ms.modify_mask, 969ab67baddSGal Pressman EFA_ADMIN_MODIFY_QP_CMD_SQ_DRAINED_ASYNC_NOTIFY, 1); 97040909f66SGal Pressman params.sq_drained_async_notify = qp_attr->en_sqd_async_notify; 97140909f66SGal Pressman } 97240909f66SGal Pressman 97340909f66SGal Pressman if (qp_attr_mask & IB_QP_QKEY) { 974ab67baddSGal Pressman EFA_SET(¶ms.modify_mask, EFA_ADMIN_MODIFY_QP_CMD_QKEY, 1); 97540909f66SGal Pressman params.qkey = qp_attr->qkey; 97640909f66SGal Pressman } 97740909f66SGal Pressman 97840909f66SGal Pressman if (qp_attr_mask & IB_QP_SQ_PSN) { 979ab67baddSGal Pressman EFA_SET(¶ms.modify_mask, EFA_ADMIN_MODIFY_QP_CMD_SQ_PSN, 1); 98040909f66SGal Pressman params.sq_psn = qp_attr->sq_psn; 98140909f66SGal Pressman } 98240909f66SGal Pressman 983a4e6a1ddSGal Pressman if (qp_attr_mask & IB_QP_RNR_RETRY) { 984a4e6a1ddSGal Pressman EFA_SET(¶ms.modify_mask, EFA_ADMIN_MODIFY_QP_CMD_RNR_RETRY, 985a4e6a1ddSGal Pressman 1); 986a4e6a1ddSGal Pressman params.rnr_retry = qp_attr->rnr_retry; 987a4e6a1ddSGal Pressman } 988a4e6a1ddSGal Pressman 98940909f66SGal Pressman err = efa_com_modify_qp(&dev->edev, ¶ms); 99040909f66SGal Pressman if (err) 99140909f66SGal Pressman return err; 99240909f66SGal Pressman 99340909f66SGal Pressman qp->state = new_state; 99440909f66SGal Pressman 99540909f66SGal Pressman return 0; 99640909f66SGal Pressman } 99740909f66SGal Pressman 99840909f66SGal Pressman static int efa_destroy_cq_idx(struct efa_dev *dev, int cq_idx) 99940909f66SGal Pressman { 100040909f66SGal Pressman struct efa_com_destroy_cq_params params = { .cq_idx = cq_idx }; 100140909f66SGal Pressman 100240909f66SGal Pressman return efa_com_destroy_cq(&dev->edev, ¶ms); 100340909f66SGal Pressman } 100440909f66SGal Pressman 10052a152512SGal Pressman static void efa_cq_user_mmap_entries_remove(struct efa_cq *cq) 10062a152512SGal Pressman { 10072a152512SGal Pressman rdma_user_mmap_entry_remove(cq->db_mmap_entry); 10082a152512SGal Pressman rdma_user_mmap_entry_remove(cq->mmap_entry); 10092a152512SGal Pressman } 10102a152512SGal Pressman 101143d781b9SLeon Romanovsky int efa_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 101240909f66SGal Pressman { 101340909f66SGal Pressman struct efa_dev *dev = to_edev(ibcq->device); 101440909f66SGal Pressman struct efa_cq *cq = to_ecq(ibcq); 101540909f66SGal Pressman 101640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 101740909f66SGal Pressman "Destroy cq[%d] virt[0x%p] freed: size[%lu], dma[%pad]\n", 101840909f66SGal Pressman cq->cq_idx, cq->cpu_addr, cq->size, &cq->dma_addr); 101940909f66SGal Pressman 10202a152512SGal Pressman efa_cq_user_mmap_entries_remove(cq); 1021ff6629f8SGal Pressman efa_destroy_cq_idx(dev, cq->cq_idx); 10222a152512SGal Pressman if (cq->eq) { 10232a152512SGal Pressman xa_erase(&dev->cqs_xa, cq->cq_idx); 10242a152512SGal Pressman synchronize_irq(cq->eq->irq.irqn); 10252a152512SGal Pressman } 1026ff6629f8SGal Pressman efa_free_mapped(dev, cq->cpu_addr, cq->dma_addr, cq->size, 1027ff6629f8SGal Pressman DMA_FROM_DEVICE); 102843d781b9SLeon Romanovsky return 0; 102940909f66SGal Pressman } 103040909f66SGal Pressman 10312a152512SGal Pressman static struct efa_eq *efa_vec2eq(struct efa_dev *dev, int vec) 10322a152512SGal Pressman { 10332a152512SGal Pressman return &dev->eqs[vec]; 10342a152512SGal Pressman } 10352a152512SGal Pressman 103640909f66SGal Pressman static int cq_mmap_entries_setup(struct efa_dev *dev, struct efa_cq *cq, 10372a152512SGal Pressman struct efa_ibv_create_cq_resp *resp, 10382a152512SGal Pressman bool db_valid) 103940909f66SGal Pressman { 104040909f66SGal Pressman resp->q_mmap_size = cq->size; 1041e84d3c18SMichal Kalderon cq->mmap_entry = efa_user_mmap_entry_insert(&cq->ucontext->ibucontext, 104240909f66SGal Pressman virt_to_phys(cq->cpu_addr), 1043e84d3c18SMichal Kalderon cq->size, EFA_MMAP_DMA_PAGE, 1044e84d3c18SMichal Kalderon &resp->q_mmap_key); 1045e84d3c18SMichal Kalderon if (!cq->mmap_entry) 104640909f66SGal Pressman return -ENOMEM; 104740909f66SGal Pressman 10482a152512SGal Pressman if (db_valid) { 10492a152512SGal Pressman cq->db_mmap_entry = 10502a152512SGal Pressman efa_user_mmap_entry_insert(&cq->ucontext->ibucontext, 10512a152512SGal Pressman dev->db_bar_addr + resp->db_off, 10522a152512SGal Pressman PAGE_SIZE, EFA_MMAP_IO_NC, 10532a152512SGal Pressman &resp->db_mmap_key); 10542a152512SGal Pressman if (!cq->db_mmap_entry) { 10552a152512SGal Pressman rdma_user_mmap_entry_remove(cq->mmap_entry); 10562a152512SGal Pressman return -ENOMEM; 10572a152512SGal Pressman } 10582a152512SGal Pressman 10592a152512SGal Pressman resp->db_off &= ~PAGE_MASK; 10602a152512SGal Pressman resp->comp_mask |= EFA_CREATE_CQ_RESP_DB_OFF; 10612a152512SGal Pressman } 10622a152512SGal Pressman 106340909f66SGal Pressman return 0; 106440909f66SGal Pressman } 106540909f66SGal Pressman 1066e39afe3dSLeon Romanovsky int efa_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 106740909f66SGal Pressman struct ib_udata *udata) 106840909f66SGal Pressman { 1069e39afe3dSLeon Romanovsky struct efa_ucontext *ucontext = rdma_udata_to_drv_context( 1070e39afe3dSLeon Romanovsky udata, struct efa_ucontext, ibucontext); 10712a152512SGal Pressman struct efa_com_create_cq_params params = {}; 107240909f66SGal Pressman struct efa_ibv_create_cq_resp resp = {}; 107340909f66SGal Pressman struct efa_com_create_cq_result result; 1074e39afe3dSLeon Romanovsky struct ib_device *ibdev = ibcq->device; 107540909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 107640909f66SGal Pressman struct efa_ibv_create_cq cmd = {}; 1077e39afe3dSLeon Romanovsky struct efa_cq *cq = to_ecq(ibcq); 1078e39afe3dSLeon Romanovsky int entries = attr->cqe; 1079dc13fbf7SMichael Margolin bool set_src_addr; 108040909f66SGal Pressman int err; 108140909f66SGal Pressman 108240909f66SGal Pressman ibdev_dbg(ibdev, "create_cq entries %d\n", entries); 108340909f66SGal Pressman 10841c407cb5SJason Gunthorpe if (attr->flags) 10851c407cb5SJason Gunthorpe return -EOPNOTSUPP; 10861c407cb5SJason Gunthorpe 108740909f66SGal Pressman if (entries < 1 || entries > dev->dev_attr.max_cq_depth) { 108840909f66SGal Pressman ibdev_dbg(ibdev, 108940909f66SGal Pressman "cq: requested entries[%u] non-positive or greater than max[%u]\n", 109040909f66SGal Pressman entries, dev->dev_attr.max_cq_depth); 109140909f66SGal Pressman err = -EINVAL; 109240909f66SGal Pressman goto err_out; 109340909f66SGal Pressman } 109440909f66SGal Pressman 1095fa8a44f6SLeon Romanovsky if (offsetofend(typeof(cmd), num_sub_cqs) > udata->inlen) { 109640909f66SGal Pressman ibdev_dbg(ibdev, 109740909f66SGal Pressman "Incompatible ABI params, no input udata\n"); 109840909f66SGal Pressman err = -EINVAL; 109940909f66SGal Pressman goto err_out; 110040909f66SGal Pressman } 110140909f66SGal Pressman 110240909f66SGal Pressman if (udata->inlen > sizeof(cmd) && 110340909f66SGal Pressman !ib_is_udata_cleared(udata, sizeof(cmd), 110440909f66SGal Pressman udata->inlen - sizeof(cmd))) { 110540909f66SGal Pressman ibdev_dbg(ibdev, 110640909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 110740909f66SGal Pressman err = -EINVAL; 110840909f66SGal Pressman goto err_out; 110940909f66SGal Pressman } 111040909f66SGal Pressman 111140909f66SGal Pressman err = ib_copy_from_udata(&cmd, udata, 111240909f66SGal Pressman min(sizeof(cmd), udata->inlen)); 111340909f66SGal Pressman if (err) { 111440909f66SGal Pressman ibdev_dbg(ibdev, "Cannot copy udata for create_cq\n"); 111540909f66SGal Pressman goto err_out; 111640909f66SGal Pressman } 111740909f66SGal Pressman 11182a152512SGal Pressman if (cmd.comp_mask || !is_reserved_cleared(cmd.reserved_58)) { 111940909f66SGal Pressman ibdev_dbg(ibdev, 112040909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 112140909f66SGal Pressman err = -EINVAL; 112240909f66SGal Pressman goto err_out; 112340909f66SGal Pressman } 112440909f66SGal Pressman 1125dc13fbf7SMichael Margolin set_src_addr = !!(cmd.flags & EFA_CREATE_CQ_WITH_SGID); 1126dc13fbf7SMichael Margolin if ((cmd.cq_entry_size != sizeof(struct efa_io_rx_cdesc_ex)) && 1127dc13fbf7SMichael Margolin (set_src_addr || 1128dc13fbf7SMichael Margolin cmd.cq_entry_size != sizeof(struct efa_io_rx_cdesc))) { 112940909f66SGal Pressman ibdev_dbg(ibdev, 113040909f66SGal Pressman "Invalid entry size [%u]\n", cmd.cq_entry_size); 113140909f66SGal Pressman err = -EINVAL; 113240909f66SGal Pressman goto err_out; 113340909f66SGal Pressman } 113440909f66SGal Pressman 113540909f66SGal Pressman if (cmd.num_sub_cqs != dev->dev_attr.sub_cqs_per_cq) { 113640909f66SGal Pressman ibdev_dbg(ibdev, 113740909f66SGal Pressman "Invalid number of sub cqs[%u] expected[%u]\n", 113840909f66SGal Pressman cmd.num_sub_cqs, dev->dev_attr.sub_cqs_per_cq); 113940909f66SGal Pressman err = -EINVAL; 114040909f66SGal Pressman goto err_out; 114140909f66SGal Pressman } 114240909f66SGal Pressman 1143e39afe3dSLeon Romanovsky cq->ucontext = ucontext; 114440909f66SGal Pressman cq->size = PAGE_ALIGN(cmd.cq_entry_size * entries * cmd.num_sub_cqs); 114540909f66SGal Pressman cq->cpu_addr = efa_zalloc_mapped(dev, &cq->dma_addr, cq->size, 114640909f66SGal Pressman DMA_FROM_DEVICE); 114740909f66SGal Pressman if (!cq->cpu_addr) { 114840909f66SGal Pressman err = -ENOMEM; 1149e39afe3dSLeon Romanovsky goto err_out; 115040909f66SGal Pressman } 115140909f66SGal Pressman 115240909f66SGal Pressman params.uarn = cq->ucontext->uarn; 115340909f66SGal Pressman params.cq_depth = entries; 115440909f66SGal Pressman params.dma_addr = cq->dma_addr; 115540909f66SGal Pressman params.entry_size_in_bytes = cmd.cq_entry_size; 115640909f66SGal Pressman params.num_sub_cqs = cmd.num_sub_cqs; 1157dc13fbf7SMichael Margolin params.set_src_addr = set_src_addr; 11582a152512SGal Pressman if (cmd.flags & EFA_CREATE_CQ_WITH_COMPLETION_CHANNEL) { 11592a152512SGal Pressman cq->eq = efa_vec2eq(dev, attr->comp_vector); 11602a152512SGal Pressman params.eqn = cq->eq->eeq.eqn; 11612a152512SGal Pressman params.interrupt_mode_enabled = true; 11622a152512SGal Pressman } 11632a152512SGal Pressman 116440909f66SGal Pressman err = efa_com_create_cq(&dev->edev, ¶ms, &result); 116540909f66SGal Pressman if (err) 116640909f66SGal Pressman goto err_free_mapped; 116740909f66SGal Pressman 11682a152512SGal Pressman resp.db_off = result.db_off; 116940909f66SGal Pressman resp.cq_idx = result.cq_idx; 117040909f66SGal Pressman cq->cq_idx = result.cq_idx; 117140909f66SGal Pressman cq->ibcq.cqe = result.actual_depth; 117240909f66SGal Pressman WARN_ON_ONCE(entries != result.actual_depth); 117340909f66SGal Pressman 11742a152512SGal Pressman err = cq_mmap_entries_setup(dev, cq, &resp, result.db_valid); 117540909f66SGal Pressman if (err) { 1176e39afe3dSLeon Romanovsky ibdev_dbg(ibdev, "Could not setup cq[%u] mmap entries\n", 1177e39afe3dSLeon Romanovsky cq->cq_idx); 117840909f66SGal Pressman goto err_destroy_cq; 117940909f66SGal Pressman } 118040909f66SGal Pressman 11812a152512SGal Pressman if (cq->eq) { 11822a152512SGal Pressman err = xa_err(xa_store(&dev->cqs_xa, cq->cq_idx, cq, GFP_KERNEL)); 11832a152512SGal Pressman if (err) { 11842a152512SGal Pressman ibdev_dbg(ibdev, "Failed to store cq[%u] in xarray\n", 11852a152512SGal Pressman cq->cq_idx); 11862a152512SGal Pressman goto err_remove_mmap; 11872a152512SGal Pressman } 11882a152512SGal Pressman } 11892a152512SGal Pressman 119040909f66SGal Pressman if (udata->outlen) { 119140909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 119240909f66SGal Pressman min(sizeof(resp), udata->outlen)); 119340909f66SGal Pressman if (err) { 119440909f66SGal Pressman ibdev_dbg(ibdev, 119540909f66SGal Pressman "Failed to copy udata for create_cq\n"); 11962a152512SGal Pressman goto err_xa_erase; 119740909f66SGal Pressman } 119840909f66SGal Pressman } 119940909f66SGal Pressman 1200e39afe3dSLeon Romanovsky ibdev_dbg(ibdev, "Created cq[%d], cq depth[%u]. dma[%pad] virt[0x%p]\n", 120140909f66SGal Pressman cq->cq_idx, result.actual_depth, &cq->dma_addr, cq->cpu_addr); 120240909f66SGal Pressman 1203e39afe3dSLeon Romanovsky return 0; 120440909f66SGal Pressman 12052a152512SGal Pressman err_xa_erase: 12062a152512SGal Pressman if (cq->eq) 12072a152512SGal Pressman xa_erase(&dev->cqs_xa, cq->cq_idx); 1208e84d3c18SMichal Kalderon err_remove_mmap: 12092a152512SGal Pressman efa_cq_user_mmap_entries_remove(cq); 121040909f66SGal Pressman err_destroy_cq: 121140909f66SGal Pressman efa_destroy_cq_idx(dev, cq->cq_idx); 121240909f66SGal Pressman err_free_mapped: 1213ff6629f8SGal Pressman efa_free_mapped(dev, cq->cpu_addr, cq->dma_addr, cq->size, 121440909f66SGal Pressman DMA_FROM_DEVICE); 1215e84d3c18SMichal Kalderon 121640909f66SGal Pressman err_out: 1217215b88acSGal Pressman atomic64_inc(&dev->stats.create_cq_err); 1218e39afe3dSLeon Romanovsky return err; 121940909f66SGal Pressman } 122040909f66SGal Pressman 122140909f66SGal Pressman static int umem_to_page_list(struct efa_dev *dev, 122240909f66SGal Pressman struct ib_umem *umem, 122340909f66SGal Pressman u64 *page_list, 122440909f66SGal Pressman u32 hp_cnt, 122540909f66SGal Pressman u8 hp_shift) 122640909f66SGal Pressman { 122740909f66SGal Pressman u32 pages_in_hp = BIT(hp_shift - PAGE_SHIFT); 122840ddb3f0SGal Pressman struct ib_block_iter biter; 122940909f66SGal Pressman unsigned int hp_idx = 0; 123040909f66SGal Pressman 123140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "hp_cnt[%u], pages_in_hp[%u]\n", 123240909f66SGal Pressman hp_cnt, pages_in_hp); 123340909f66SGal Pressman 1234ebc24096SJason Gunthorpe rdma_umem_for_each_dma_block(umem, &biter, BIT(hp_shift)) 123540ddb3f0SGal Pressman page_list[hp_idx++] = rdma_block_iter_dma_address(&biter); 123640909f66SGal Pressman 123740909f66SGal Pressman return 0; 123840909f66SGal Pressman } 123940909f66SGal Pressman 124040909f66SGal Pressman static struct scatterlist *efa_vmalloc_buf_to_sg(u64 *buf, int page_cnt) 124140909f66SGal Pressman { 124240909f66SGal Pressman struct scatterlist *sglist; 124340909f66SGal Pressman struct page *pg; 124440909f66SGal Pressman int i; 124540909f66SGal Pressman 12463de3c478SJulia Lawall sglist = kmalloc_array(page_cnt, sizeof(*sglist), GFP_KERNEL); 124740909f66SGal Pressman if (!sglist) 124840909f66SGal Pressman return NULL; 124940909f66SGal Pressman sg_init_table(sglist, page_cnt); 125040909f66SGal Pressman for (i = 0; i < page_cnt; i++) { 125140909f66SGal Pressman pg = vmalloc_to_page(buf); 125240909f66SGal Pressman if (!pg) 125340909f66SGal Pressman goto err; 125440909f66SGal Pressman sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); 125540909f66SGal Pressman buf += PAGE_SIZE / sizeof(*buf); 125640909f66SGal Pressman } 125740909f66SGal Pressman return sglist; 125840909f66SGal Pressman 125940909f66SGal Pressman err: 126040909f66SGal Pressman kfree(sglist); 126140909f66SGal Pressman return NULL; 126240909f66SGal Pressman } 126340909f66SGal Pressman 126440909f66SGal Pressman /* 126540909f66SGal Pressman * create a chunk list of physical pages dma addresses from the supplied 126640909f66SGal Pressman * scatter gather list 126740909f66SGal Pressman */ 126840909f66SGal Pressman static int pbl_chunk_list_create(struct efa_dev *dev, struct pbl_context *pbl) 126940909f66SGal Pressman { 127040909f66SGal Pressman struct pbl_chunk_list *chunk_list = &pbl->phys.indirect.chunk_list; 127140909f66SGal Pressman int page_cnt = pbl->phys.indirect.pbl_buf_size_in_pages; 127240909f66SGal Pressman struct scatterlist *pages_sgl = pbl->phys.indirect.sgl; 12734d50e084SGal Pressman unsigned int chunk_list_size, chunk_idx, payload_idx; 127440909f66SGal Pressman int sg_dma_cnt = pbl->phys.indirect.sg_dma_cnt; 127540909f66SGal Pressman struct efa_com_ctrl_buff_info *ctrl_buf; 127640909f66SGal Pressman u64 *cur_chunk_buf, *prev_chunk_buf; 12774d50e084SGal Pressman struct ib_block_iter biter; 127840909f66SGal Pressman dma_addr_t dma_addr; 127940909f66SGal Pressman int i; 128040909f66SGal Pressman 128140909f66SGal Pressman /* allocate a chunk list that consists of 4KB chunks */ 128240909f66SGal Pressman chunk_list_size = DIV_ROUND_UP(page_cnt, EFA_PTRS_PER_CHUNK); 128340909f66SGal Pressman 128440909f66SGal Pressman chunk_list->size = chunk_list_size; 128540909f66SGal Pressman chunk_list->chunks = kcalloc(chunk_list_size, 128640909f66SGal Pressman sizeof(*chunk_list->chunks), 128740909f66SGal Pressman GFP_KERNEL); 128840909f66SGal Pressman if (!chunk_list->chunks) 128940909f66SGal Pressman return -ENOMEM; 129040909f66SGal Pressman 129140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 129240909f66SGal Pressman "chunk_list_size[%u] - pages[%u]\n", chunk_list_size, 129340909f66SGal Pressman page_cnt); 129440909f66SGal Pressman 129540909f66SGal Pressman /* allocate chunk buffers: */ 129640909f66SGal Pressman for (i = 0; i < chunk_list_size; i++) { 129740909f66SGal Pressman chunk_list->chunks[i].buf = kzalloc(EFA_CHUNK_SIZE, GFP_KERNEL); 129840909f66SGal Pressman if (!chunk_list->chunks[i].buf) 129940909f66SGal Pressman goto chunk_list_dealloc; 130040909f66SGal Pressman 130140909f66SGal Pressman chunk_list->chunks[i].length = EFA_CHUNK_USED_SIZE; 130240909f66SGal Pressman } 130340909f66SGal Pressman chunk_list->chunks[chunk_list_size - 1].length = 130440909f66SGal Pressman ((page_cnt % EFA_PTRS_PER_CHUNK) * EFA_CHUNK_PAYLOAD_PTR_SIZE) + 130540909f66SGal Pressman EFA_CHUNK_PTR_SIZE; 130640909f66SGal Pressman 130740909f66SGal Pressman /* fill the dma addresses of sg list pages to chunks: */ 130840909f66SGal Pressman chunk_idx = 0; 130940909f66SGal Pressman payload_idx = 0; 131040909f66SGal Pressman cur_chunk_buf = chunk_list->chunks[0].buf; 13114d50e084SGal Pressman rdma_for_each_block(pages_sgl, &biter, sg_dma_cnt, 13124d50e084SGal Pressman EFA_CHUNK_PAYLOAD_SIZE) { 131340909f66SGal Pressman cur_chunk_buf[payload_idx++] = 13144d50e084SGal Pressman rdma_block_iter_dma_address(&biter); 131540909f66SGal Pressman 131640909f66SGal Pressman if (payload_idx == EFA_PTRS_PER_CHUNK) { 131740909f66SGal Pressman chunk_idx++; 131840909f66SGal Pressman cur_chunk_buf = chunk_list->chunks[chunk_idx].buf; 131940909f66SGal Pressman payload_idx = 0; 132040909f66SGal Pressman } 132140909f66SGal Pressman } 132240909f66SGal Pressman 132340909f66SGal Pressman /* map chunks to dma and fill chunks next ptrs */ 132440909f66SGal Pressman for (i = chunk_list_size - 1; i >= 0; i--) { 132540909f66SGal Pressman dma_addr = dma_map_single(&dev->pdev->dev, 132640909f66SGal Pressman chunk_list->chunks[i].buf, 132740909f66SGal Pressman chunk_list->chunks[i].length, 132840909f66SGal Pressman DMA_TO_DEVICE); 132940909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, dma_addr)) { 133040909f66SGal Pressman ibdev_err(&dev->ibdev, 133140909f66SGal Pressman "chunk[%u] dma_map_failed\n", i); 133240909f66SGal Pressman goto chunk_list_unmap; 133340909f66SGal Pressman } 133440909f66SGal Pressman 133540909f66SGal Pressman chunk_list->chunks[i].dma_addr = dma_addr; 133640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 133740909f66SGal Pressman "chunk[%u] mapped at [%pad]\n", i, &dma_addr); 133840909f66SGal Pressman 133940909f66SGal Pressman if (!i) 134040909f66SGal Pressman break; 134140909f66SGal Pressman 134240909f66SGal Pressman prev_chunk_buf = chunk_list->chunks[i - 1].buf; 134340909f66SGal Pressman 134440909f66SGal Pressman ctrl_buf = (struct efa_com_ctrl_buff_info *) 134540909f66SGal Pressman &prev_chunk_buf[EFA_PTRS_PER_CHUNK]; 134640909f66SGal Pressman ctrl_buf->length = chunk_list->chunks[i].length; 134740909f66SGal Pressman 134840909f66SGal Pressman efa_com_set_dma_addr(dma_addr, 134940909f66SGal Pressman &ctrl_buf->address.mem_addr_high, 135040909f66SGal Pressman &ctrl_buf->address.mem_addr_low); 135140909f66SGal Pressman } 135240909f66SGal Pressman 135340909f66SGal Pressman return 0; 135440909f66SGal Pressman 135540909f66SGal Pressman chunk_list_unmap: 135640909f66SGal Pressman for (; i < chunk_list_size; i++) { 135740909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, chunk_list->chunks[i].dma_addr, 135840909f66SGal Pressman chunk_list->chunks[i].length, DMA_TO_DEVICE); 135940909f66SGal Pressman } 136040909f66SGal Pressman chunk_list_dealloc: 136140909f66SGal Pressman for (i = 0; i < chunk_list_size; i++) 136240909f66SGal Pressman kfree(chunk_list->chunks[i].buf); 136340909f66SGal Pressman 136440909f66SGal Pressman kfree(chunk_list->chunks); 136540909f66SGal Pressman return -ENOMEM; 136640909f66SGal Pressman } 136740909f66SGal Pressman 136840909f66SGal Pressman static void pbl_chunk_list_destroy(struct efa_dev *dev, struct pbl_context *pbl) 136940909f66SGal Pressman { 137040909f66SGal Pressman struct pbl_chunk_list *chunk_list = &pbl->phys.indirect.chunk_list; 137140909f66SGal Pressman int i; 137240909f66SGal Pressman 137340909f66SGal Pressman for (i = 0; i < chunk_list->size; i++) { 137440909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, chunk_list->chunks[i].dma_addr, 137540909f66SGal Pressman chunk_list->chunks[i].length, DMA_TO_DEVICE); 137640909f66SGal Pressman kfree(chunk_list->chunks[i].buf); 137740909f66SGal Pressman } 137840909f66SGal Pressman 137940909f66SGal Pressman kfree(chunk_list->chunks); 138040909f66SGal Pressman } 138140909f66SGal Pressman 138240909f66SGal Pressman /* initialize pbl continuous mode: map pbl buffer to a dma address. */ 138340909f66SGal Pressman static int pbl_continuous_initialize(struct efa_dev *dev, 138440909f66SGal Pressman struct pbl_context *pbl) 138540909f66SGal Pressman { 138640909f66SGal Pressman dma_addr_t dma_addr; 138740909f66SGal Pressman 138840909f66SGal Pressman dma_addr = dma_map_single(&dev->pdev->dev, pbl->pbl_buf, 138940909f66SGal Pressman pbl->pbl_buf_size_in_bytes, DMA_TO_DEVICE); 139040909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, dma_addr)) { 139140909f66SGal Pressman ibdev_err(&dev->ibdev, "Unable to map pbl to DMA address\n"); 139240909f66SGal Pressman return -ENOMEM; 139340909f66SGal Pressman } 139440909f66SGal Pressman 139540909f66SGal Pressman pbl->phys.continuous.dma_addr = dma_addr; 139640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 139740909f66SGal Pressman "pbl continuous - dma_addr = %pad, size[%u]\n", 139840909f66SGal Pressman &dma_addr, pbl->pbl_buf_size_in_bytes); 139940909f66SGal Pressman 140040909f66SGal Pressman return 0; 140140909f66SGal Pressman } 140240909f66SGal Pressman 140340909f66SGal Pressman /* 140440909f66SGal Pressman * initialize pbl indirect mode: 140540909f66SGal Pressman * create a chunk list out of the dma addresses of the physical pages of 140640909f66SGal Pressman * pbl buffer. 140740909f66SGal Pressman */ 140840909f66SGal Pressman static int pbl_indirect_initialize(struct efa_dev *dev, struct pbl_context *pbl) 140940909f66SGal Pressman { 1410866422cdSYonatan Nachum u32 size_in_pages = DIV_ROUND_UP(pbl->pbl_buf_size_in_bytes, EFA_CHUNK_PAYLOAD_SIZE); 141140909f66SGal Pressman struct scatterlist *sgl; 141240909f66SGal Pressman int sg_dma_cnt, err; 141340909f66SGal Pressman 141440909f66SGal Pressman BUILD_BUG_ON(EFA_CHUNK_PAYLOAD_SIZE > PAGE_SIZE); 141540909f66SGal Pressman sgl = efa_vmalloc_buf_to_sg(pbl->pbl_buf, size_in_pages); 141640909f66SGal Pressman if (!sgl) 141740909f66SGal Pressman return -ENOMEM; 141840909f66SGal Pressman 141940909f66SGal Pressman sg_dma_cnt = dma_map_sg(&dev->pdev->dev, sgl, size_in_pages, DMA_TO_DEVICE); 142040909f66SGal Pressman if (!sg_dma_cnt) { 142140909f66SGal Pressman err = -EINVAL; 142240909f66SGal Pressman goto err_map; 142340909f66SGal Pressman } 142440909f66SGal Pressman 142540909f66SGal Pressman pbl->phys.indirect.pbl_buf_size_in_pages = size_in_pages; 142640909f66SGal Pressman pbl->phys.indirect.sgl = sgl; 142740909f66SGal Pressman pbl->phys.indirect.sg_dma_cnt = sg_dma_cnt; 142840909f66SGal Pressman err = pbl_chunk_list_create(dev, pbl); 142940909f66SGal Pressman if (err) { 143040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 143140909f66SGal Pressman "chunk_list creation failed[%d]\n", err); 143240909f66SGal Pressman goto err_chunk; 143340909f66SGal Pressman } 143440909f66SGal Pressman 143540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 143640909f66SGal Pressman "pbl indirect - size[%u], chunks[%u]\n", 143740909f66SGal Pressman pbl->pbl_buf_size_in_bytes, 143840909f66SGal Pressman pbl->phys.indirect.chunk_list.size); 143940909f66SGal Pressman 144040909f66SGal Pressman return 0; 144140909f66SGal Pressman 144240909f66SGal Pressman err_chunk: 144340909f66SGal Pressman dma_unmap_sg(&dev->pdev->dev, sgl, size_in_pages, DMA_TO_DEVICE); 144440909f66SGal Pressman err_map: 144540909f66SGal Pressman kfree(sgl); 144640909f66SGal Pressman return err; 144740909f66SGal Pressman } 144840909f66SGal Pressman 144940909f66SGal Pressman static void pbl_indirect_terminate(struct efa_dev *dev, struct pbl_context *pbl) 145040909f66SGal Pressman { 145140909f66SGal Pressman pbl_chunk_list_destroy(dev, pbl); 145240909f66SGal Pressman dma_unmap_sg(&dev->pdev->dev, pbl->phys.indirect.sgl, 145340909f66SGal Pressman pbl->phys.indirect.pbl_buf_size_in_pages, DMA_TO_DEVICE); 145440909f66SGal Pressman kfree(pbl->phys.indirect.sgl); 145540909f66SGal Pressman } 145640909f66SGal Pressman 145740909f66SGal Pressman /* create a page buffer list from a mapped user memory region */ 145840909f66SGal Pressman static int pbl_create(struct efa_dev *dev, 145940909f66SGal Pressman struct pbl_context *pbl, 146040909f66SGal Pressman struct ib_umem *umem, 146140909f66SGal Pressman int hp_cnt, 146240909f66SGal Pressman u8 hp_shift) 146340909f66SGal Pressman { 146440909f66SGal Pressman int err; 146540909f66SGal Pressman 146640909f66SGal Pressman pbl->pbl_buf_size_in_bytes = hp_cnt * EFA_CHUNK_PAYLOAD_PTR_SIZE; 1467255efcaeSGal Pressman pbl->pbl_buf = kvzalloc(pbl->pbl_buf_size_in_bytes, GFP_KERNEL); 1468255efcaeSGal Pressman if (!pbl->pbl_buf) 1469255efcaeSGal Pressman return -ENOMEM; 1470255efcaeSGal Pressman 1471255efcaeSGal Pressman if (is_vmalloc_addr(pbl->pbl_buf)) { 1472255efcaeSGal Pressman pbl->physically_continuous = 0; 1473255efcaeSGal Pressman err = umem_to_page_list(dev, umem, pbl->pbl_buf, hp_cnt, 1474255efcaeSGal Pressman hp_shift); 1475255efcaeSGal Pressman if (err) 1476255efcaeSGal Pressman goto err_free; 1477255efcaeSGal Pressman 1478255efcaeSGal Pressman err = pbl_indirect_initialize(dev, pbl); 1479255efcaeSGal Pressman if (err) 1480255efcaeSGal Pressman goto err_free; 1481255efcaeSGal Pressman } else { 148240909f66SGal Pressman pbl->physically_continuous = 1; 148340909f66SGal Pressman err = umem_to_page_list(dev, umem, pbl->pbl_buf, hp_cnt, 148440909f66SGal Pressman hp_shift); 148540909f66SGal Pressman if (err) 1486255efcaeSGal Pressman goto err_free; 1487255efcaeSGal Pressman 148840909f66SGal Pressman err = pbl_continuous_initialize(dev, pbl); 148940909f66SGal Pressman if (err) 1490255efcaeSGal Pressman goto err_free; 149140909f66SGal Pressman } 149240909f66SGal Pressman 149340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 149440909f66SGal Pressman "user_pbl_created: user_pages[%u], continuous[%u]\n", 149540909f66SGal Pressman hp_cnt, pbl->physically_continuous); 149640909f66SGal Pressman 149740909f66SGal Pressman return 0; 149840909f66SGal Pressman 1499255efcaeSGal Pressman err_free: 1500255efcaeSGal Pressman kvfree(pbl->pbl_buf); 150140909f66SGal Pressman return err; 150240909f66SGal Pressman } 150340909f66SGal Pressman 150440909f66SGal Pressman static void pbl_destroy(struct efa_dev *dev, struct pbl_context *pbl) 150540909f66SGal Pressman { 1506255efcaeSGal Pressman if (pbl->physically_continuous) 150740909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, pbl->phys.continuous.dma_addr, 150840909f66SGal Pressman pbl->pbl_buf_size_in_bytes, DMA_TO_DEVICE); 1509255efcaeSGal Pressman else 151040909f66SGal Pressman pbl_indirect_terminate(dev, pbl); 1511255efcaeSGal Pressman 1512255efcaeSGal Pressman kvfree(pbl->pbl_buf); 151340909f66SGal Pressman } 151440909f66SGal Pressman 151540909f66SGal Pressman static int efa_create_inline_pbl(struct efa_dev *dev, struct efa_mr *mr, 151640909f66SGal Pressman struct efa_com_reg_mr_params *params) 151740909f66SGal Pressman { 151840909f66SGal Pressman int err; 151940909f66SGal Pressman 152040909f66SGal Pressman params->inline_pbl = 1; 152140909f66SGal Pressman err = umem_to_page_list(dev, mr->umem, params->pbl.inline_pbl_array, 152240909f66SGal Pressman params->page_num, params->page_shift); 152340909f66SGal Pressman if (err) 152440909f66SGal Pressman return err; 152540909f66SGal Pressman 152640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 152740909f66SGal Pressman "inline_pbl_array - pages[%u]\n", params->page_num); 152840909f66SGal Pressman 152940909f66SGal Pressman return 0; 153040909f66SGal Pressman } 153140909f66SGal Pressman 153240909f66SGal Pressman static int efa_create_pbl(struct efa_dev *dev, 153340909f66SGal Pressman struct pbl_context *pbl, 153440909f66SGal Pressman struct efa_mr *mr, 153540909f66SGal Pressman struct efa_com_reg_mr_params *params) 153640909f66SGal Pressman { 153740909f66SGal Pressman int err; 153840909f66SGal Pressman 153940909f66SGal Pressman err = pbl_create(dev, pbl, mr->umem, params->page_num, 154040909f66SGal Pressman params->page_shift); 154140909f66SGal Pressman if (err) { 154240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Failed to create pbl[%d]\n", err); 154340909f66SGal Pressman return err; 154440909f66SGal Pressman } 154540909f66SGal Pressman 154640909f66SGal Pressman params->inline_pbl = 0; 154740909f66SGal Pressman params->indirect = !pbl->physically_continuous; 154840909f66SGal Pressman if (pbl->physically_continuous) { 154940909f66SGal Pressman params->pbl.pbl.length = pbl->pbl_buf_size_in_bytes; 155040909f66SGal Pressman 155140909f66SGal Pressman efa_com_set_dma_addr(pbl->phys.continuous.dma_addr, 155240909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_high, 155340909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_low); 155440909f66SGal Pressman } else { 155540909f66SGal Pressman params->pbl.pbl.length = 155640909f66SGal Pressman pbl->phys.indirect.chunk_list.chunks[0].length; 155740909f66SGal Pressman 155840909f66SGal Pressman efa_com_set_dma_addr(pbl->phys.indirect.chunk_list.chunks[0].dma_addr, 155940909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_high, 156040909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_low); 156140909f66SGal Pressman } 156240909f66SGal Pressman 156340909f66SGal Pressman return 0; 156440909f66SGal Pressman } 156540909f66SGal Pressman 156666f4817bSGal Pressman static struct efa_mr *efa_alloc_mr(struct ib_pd *ibpd, int access_flags, 156740909f66SGal Pressman struct ib_udata *udata) 156840909f66SGal Pressman { 156940909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 1570666e8ff5SDaniel Kranzdorf int supp_access_flags; 157140909f66SGal Pressman struct efa_mr *mr; 157240909f66SGal Pressman 157333006bd4SMoni Shoua if (udata && udata->inlen && 157440909f66SGal Pressman !ib_is_udata_cleared(udata, 0, sizeof(udata->inlen))) { 157540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 157640909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 157766f4817bSGal Pressman return ERR_PTR(-EINVAL); 157840909f66SGal Pressman } 157940909f66SGal Pressman 1580666e8ff5SDaniel Kranzdorf supp_access_flags = 1581666e8ff5SDaniel Kranzdorf IB_ACCESS_LOCAL_WRITE | 1582531094dcSYonatan Nachum (EFA_DEV_CAP(dev, RDMA_READ) ? IB_ACCESS_REMOTE_READ : 0) | 1583531094dcSYonatan Nachum (EFA_DEV_CAP(dev, RDMA_WRITE) ? IB_ACCESS_REMOTE_WRITE : 0); 1584666e8ff5SDaniel Kranzdorf 1585ba19e166SGal Pressman access_flags &= ~IB_ACCESS_OPTIONAL; 1586666e8ff5SDaniel Kranzdorf if (access_flags & ~supp_access_flags) { 158740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 158840909f66SGal Pressman "Unsupported access flags[%#x], supported[%#x]\n", 1589666e8ff5SDaniel Kranzdorf access_flags, supp_access_flags); 159066f4817bSGal Pressman return ERR_PTR(-EOPNOTSUPP); 159140909f66SGal Pressman } 159240909f66SGal Pressman 159340909f66SGal Pressman mr = kzalloc(sizeof(*mr), GFP_KERNEL); 159466f4817bSGal Pressman if (!mr) 159566f4817bSGal Pressman return ERR_PTR(-ENOMEM); 159666f4817bSGal Pressman 159766f4817bSGal Pressman return mr; 159840909f66SGal Pressman } 159940909f66SGal Pressman 160066f4817bSGal Pressman static int efa_register_mr(struct ib_pd *ibpd, struct efa_mr *mr, u64 start, 160166f4817bSGal Pressman u64 length, u64 virt_addr, int access_flags) 160266f4817bSGal Pressman { 160366f4817bSGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 160466f4817bSGal Pressman struct efa_com_reg_mr_params params = {}; 160566f4817bSGal Pressman struct efa_com_reg_mr_result result = {}; 160666f4817bSGal Pressman struct pbl_context pbl; 160766f4817bSGal Pressman unsigned int pg_sz; 160866f4817bSGal Pressman int inline_size; 160966f4817bSGal Pressman int err; 161040909f66SGal Pressman 161140909f66SGal Pressman params.pd = to_epd(ibpd)->pdn; 161240909f66SGal Pressman params.iova = virt_addr; 161340909f66SGal Pressman params.mr_length_in_bytes = length; 1614e6c4f3ffSDaniel Kranzdorf params.permissions = access_flags; 161540909f66SGal Pressman 161640ddb3f0SGal Pressman pg_sz = ib_umem_find_best_pgsz(mr->umem, 161740ddb3f0SGal Pressman dev->dev_attr.page_size_cap, 161840ddb3f0SGal Pressman virt_addr); 161940ddb3f0SGal Pressman if (!pg_sz) { 162040ddb3f0SGal Pressman ibdev_dbg(&dev->ibdev, "Failed to find a suitable page size in page_size_cap %#llx\n", 162140ddb3f0SGal Pressman dev->dev_attr.page_size_cap); 162266f4817bSGal Pressman return -EOPNOTSUPP; 162340ddb3f0SGal Pressman } 162440ddb3f0SGal Pressman 16251f9b6827SJason Gunthorpe params.page_shift = order_base_2(pg_sz); 16261f9b6827SJason Gunthorpe params.page_num = ib_umem_num_dma_blocks(mr->umem, pg_sz); 162740ddb3f0SGal Pressman 162840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 162940ddb3f0SGal Pressman "start %#llx length %#llx params.page_shift %u params.page_num %u\n", 163040ddb3f0SGal Pressman start, length, params.page_shift, params.page_num); 163140909f66SGal Pressman 163240909f66SGal Pressman inline_size = ARRAY_SIZE(params.pbl.inline_pbl_array); 163340909f66SGal Pressman if (params.page_num <= inline_size) { 163440909f66SGal Pressman err = efa_create_inline_pbl(dev, mr, ¶ms); 163540909f66SGal Pressman if (err) 163666f4817bSGal Pressman return err; 163740909f66SGal Pressman 163840909f66SGal Pressman err = efa_com_register_mr(&dev->edev, ¶ms, &result); 163940909f66SGal Pressman if (err) 164066f4817bSGal Pressman return err; 164140909f66SGal Pressman } else { 164240909f66SGal Pressman err = efa_create_pbl(dev, &pbl, mr, ¶ms); 164340909f66SGal Pressman if (err) 164466f4817bSGal Pressman return err; 164540909f66SGal Pressman 164640909f66SGal Pressman err = efa_com_register_mr(&dev->edev, ¶ms, &result); 164740909f66SGal Pressman pbl_destroy(dev, &pbl); 164840909f66SGal Pressman 164940909f66SGal Pressman if (err) 165066f4817bSGal Pressman return err; 165140909f66SGal Pressman } 165240909f66SGal Pressman 165340909f66SGal Pressman mr->ibmr.lkey = result.l_key; 165440909f66SGal Pressman mr->ibmr.rkey = result.r_key; 165540909f66SGal Pressman mr->ibmr.length = length; 165640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Registered mr[%d]\n", mr->ibmr.lkey); 165740909f66SGal Pressman 165866f4817bSGal Pressman return 0; 165966f4817bSGal Pressman } 166066f4817bSGal Pressman 166166f4817bSGal Pressman struct ib_mr *efa_reg_user_mr_dmabuf(struct ib_pd *ibpd, u64 start, 166266f4817bSGal Pressman u64 length, u64 virt_addr, 166366f4817bSGal Pressman int fd, int access_flags, 166466f4817bSGal Pressman struct ib_udata *udata) 166566f4817bSGal Pressman { 166666f4817bSGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 166766f4817bSGal Pressman struct ib_umem_dmabuf *umem_dmabuf; 166866f4817bSGal Pressman struct efa_mr *mr; 166966f4817bSGal Pressman int err; 167066f4817bSGal Pressman 167166f4817bSGal Pressman mr = efa_alloc_mr(ibpd, access_flags, udata); 167266f4817bSGal Pressman if (IS_ERR(mr)) { 167366f4817bSGal Pressman err = PTR_ERR(mr); 167466f4817bSGal Pressman goto err_out; 167566f4817bSGal Pressman } 167666f4817bSGal Pressman 167766f4817bSGal Pressman umem_dmabuf = ib_umem_dmabuf_get_pinned(ibpd->device, start, length, fd, 167866f4817bSGal Pressman access_flags); 167966f4817bSGal Pressman if (IS_ERR(umem_dmabuf)) { 168066f4817bSGal Pressman err = PTR_ERR(umem_dmabuf); 168166f4817bSGal Pressman ibdev_dbg(&dev->ibdev, "Failed to get dmabuf umem[%d]\n", err); 168266f4817bSGal Pressman goto err_free; 168366f4817bSGal Pressman } 168466f4817bSGal Pressman 168566f4817bSGal Pressman mr->umem = &umem_dmabuf->umem; 168666f4817bSGal Pressman err = efa_register_mr(ibpd, mr, start, length, virt_addr, access_flags); 168766f4817bSGal Pressman if (err) 168866f4817bSGal Pressman goto err_release; 168966f4817bSGal Pressman 169040909f66SGal Pressman return &mr->ibmr; 169140909f66SGal Pressman 169266f4817bSGal Pressman err_release: 169366f4817bSGal Pressman ib_umem_release(mr->umem); 169466f4817bSGal Pressman err_free: 169566f4817bSGal Pressman kfree(mr); 169666f4817bSGal Pressman err_out: 169766f4817bSGal Pressman atomic64_inc(&dev->stats.reg_mr_err); 169866f4817bSGal Pressman return ERR_PTR(err); 169966f4817bSGal Pressman } 170066f4817bSGal Pressman 170166f4817bSGal Pressman struct ib_mr *efa_reg_mr(struct ib_pd *ibpd, u64 start, u64 length, 170266f4817bSGal Pressman u64 virt_addr, int access_flags, 170366f4817bSGal Pressman struct ib_udata *udata) 170466f4817bSGal Pressman { 170566f4817bSGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 170666f4817bSGal Pressman struct efa_mr *mr; 170766f4817bSGal Pressman int err; 170866f4817bSGal Pressman 170966f4817bSGal Pressman mr = efa_alloc_mr(ibpd, access_flags, udata); 171066f4817bSGal Pressman if (IS_ERR(mr)) { 171166f4817bSGal Pressman err = PTR_ERR(mr); 171266f4817bSGal Pressman goto err_out; 171366f4817bSGal Pressman } 171466f4817bSGal Pressman 171566f4817bSGal Pressman mr->umem = ib_umem_get(ibpd->device, start, length, access_flags); 171666f4817bSGal Pressman if (IS_ERR(mr->umem)) { 171766f4817bSGal Pressman err = PTR_ERR(mr->umem); 171866f4817bSGal Pressman ibdev_dbg(&dev->ibdev, 171966f4817bSGal Pressman "Failed to pin and map user space memory[%d]\n", err); 172066f4817bSGal Pressman goto err_free; 172166f4817bSGal Pressman } 172266f4817bSGal Pressman 172366f4817bSGal Pressman err = efa_register_mr(ibpd, mr, start, length, virt_addr, access_flags); 172466f4817bSGal Pressman if (err) 172566f4817bSGal Pressman goto err_release; 172666f4817bSGal Pressman 172766f4817bSGal Pressman return &mr->ibmr; 172866f4817bSGal Pressman 172966f4817bSGal Pressman err_release: 173040909f66SGal Pressman ib_umem_release(mr->umem); 173140909f66SGal Pressman err_free: 173240909f66SGal Pressman kfree(mr); 173340909f66SGal Pressman err_out: 1734215b88acSGal Pressman atomic64_inc(&dev->stats.reg_mr_err); 173540909f66SGal Pressman return ERR_PTR(err); 173640909f66SGal Pressman } 173740909f66SGal Pressman 173840909f66SGal Pressman int efa_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata) 173940909f66SGal Pressman { 174040909f66SGal Pressman struct efa_dev *dev = to_edev(ibmr->device); 174140909f66SGal Pressman struct efa_com_dereg_mr_params params; 174240909f66SGal Pressman struct efa_mr *mr = to_emr(ibmr); 174340909f66SGal Pressman int err; 174440909f66SGal Pressman 174540909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Deregister mr[%d]\n", ibmr->lkey); 174640909f66SGal Pressman 174740909f66SGal Pressman params.l_key = mr->ibmr.lkey; 174840909f66SGal Pressman err = efa_com_dereg_mr(&dev->edev, ¶ms); 174940909f66SGal Pressman if (err) 175040909f66SGal Pressman return err; 175140909f66SGal Pressman 1752958b6813SGal Pressman ib_umem_release(mr->umem); 175340909f66SGal Pressman kfree(mr); 175440909f66SGal Pressman 175540909f66SGal Pressman return 0; 175640909f66SGal Pressman } 175740909f66SGal Pressman 17581fb7f897SMark Bloch int efa_get_port_immutable(struct ib_device *ibdev, u32 port_num, 175940909f66SGal Pressman struct ib_port_immutable *immutable) 176040909f66SGal Pressman { 176140909f66SGal Pressman struct ib_port_attr attr; 176240909f66SGal Pressman int err; 176340909f66SGal Pressman 176440909f66SGal Pressman err = ib_query_port(ibdev, port_num, &attr); 176540909f66SGal Pressman if (err) { 176640909f66SGal Pressman ibdev_dbg(ibdev, "Couldn't query port err[%d]\n", err); 176740909f66SGal Pressman return err; 176840909f66SGal Pressman } 176940909f66SGal Pressman 177040909f66SGal Pressman immutable->pkey_tbl_len = attr.pkey_tbl_len; 177140909f66SGal Pressman immutable->gid_tbl_len = attr.gid_tbl_len; 177240909f66SGal Pressman 177340909f66SGal Pressman return 0; 177440909f66SGal Pressman } 177540909f66SGal Pressman 177640909f66SGal Pressman static int efa_dealloc_uar(struct efa_dev *dev, u16 uarn) 177740909f66SGal Pressman { 177840909f66SGal Pressman struct efa_com_dealloc_uar_params params = { 177940909f66SGal Pressman .uarn = uarn, 178040909f66SGal Pressman }; 178140909f66SGal Pressman 178240909f66SGal Pressman return efa_com_dealloc_uar(&dev->edev, ¶ms); 178340909f66SGal Pressman } 178440909f66SGal Pressman 1785a5d87b69SGal Pressman #define EFA_CHECK_USER_COMP(_dev, _comp_mask, _attr, _mask, _attr_str) \ 1786a5d87b69SGal Pressman (_attr_str = (!(_dev)->dev_attr._attr || ((_comp_mask) & (_mask))) ? \ 1787a5d87b69SGal Pressman NULL : #_attr) 1788a5d87b69SGal Pressman 1789a5d87b69SGal Pressman static int efa_user_comp_handshake(const struct ib_ucontext *ibucontext, 1790a5d87b69SGal Pressman const struct efa_ibv_alloc_ucontext_cmd *cmd) 1791a5d87b69SGal Pressman { 1792a5d87b69SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 1793a5d87b69SGal Pressman char *attr_str; 1794a5d87b69SGal Pressman 1795a5d87b69SGal Pressman if (EFA_CHECK_USER_COMP(dev, cmd->comp_mask, max_tx_batch, 1796a5d87b69SGal Pressman EFA_ALLOC_UCONTEXT_CMD_COMP_TX_BATCH, attr_str)) 1797a5d87b69SGal Pressman goto err; 1798a5d87b69SGal Pressman 1799a5d87b69SGal Pressman if (EFA_CHECK_USER_COMP(dev, cmd->comp_mask, min_sq_depth, 1800a5d87b69SGal Pressman EFA_ALLOC_UCONTEXT_CMD_COMP_MIN_SQ_WR, 1801a5d87b69SGal Pressman attr_str)) 1802a5d87b69SGal Pressman goto err; 1803a5d87b69SGal Pressman 1804a5d87b69SGal Pressman return 0; 1805a5d87b69SGal Pressman 1806a5d87b69SGal Pressman err: 1807a5d87b69SGal Pressman ibdev_dbg(&dev->ibdev, "Userspace handshake failed for %s attribute\n", 1808a5d87b69SGal Pressman attr_str); 1809a5d87b69SGal Pressman return -EOPNOTSUPP; 1810a5d87b69SGal Pressman } 1811a5d87b69SGal Pressman 181240909f66SGal Pressman int efa_alloc_ucontext(struct ib_ucontext *ibucontext, struct ib_udata *udata) 181340909f66SGal Pressman { 181440909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 181540909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 181640909f66SGal Pressman struct efa_ibv_alloc_ucontext_resp resp = {}; 1817a5d87b69SGal Pressman struct efa_ibv_alloc_ucontext_cmd cmd = {}; 181840909f66SGal Pressman struct efa_com_alloc_uar_result result; 181940909f66SGal Pressman int err; 182040909f66SGal Pressman 182140909f66SGal Pressman /* 182240909f66SGal Pressman * it's fine if the driver does not know all request fields, 182340909f66SGal Pressman * we will ack input fields in our response. 182440909f66SGal Pressman */ 182540909f66SGal Pressman 1826a5d87b69SGal Pressman err = ib_copy_from_udata(&cmd, udata, 1827a5d87b69SGal Pressman min(sizeof(cmd), udata->inlen)); 1828a5d87b69SGal Pressman if (err) { 1829a5d87b69SGal Pressman ibdev_dbg(&dev->ibdev, 1830a5d87b69SGal Pressman "Cannot copy udata for alloc_ucontext\n"); 1831a5d87b69SGal Pressman goto err_out; 1832a5d87b69SGal Pressman } 1833a5d87b69SGal Pressman 1834a5d87b69SGal Pressman err = efa_user_comp_handshake(ibucontext, &cmd); 1835a5d87b69SGal Pressman if (err) 1836a5d87b69SGal Pressman goto err_out; 1837a5d87b69SGal Pressman 183840909f66SGal Pressman err = efa_com_alloc_uar(&dev->edev, &result); 183940909f66SGal Pressman if (err) 184040909f66SGal Pressman goto err_out; 184140909f66SGal Pressman 184240909f66SGal Pressman ucontext->uarn = result.uarn; 184340909f66SGal Pressman 184440909f66SGal Pressman resp.cmds_supp_udata_mask |= EFA_USER_CMDS_SUPP_UDATA_QUERY_DEVICE; 184540909f66SGal Pressman resp.cmds_supp_udata_mask |= EFA_USER_CMDS_SUPP_UDATA_CREATE_AH; 184640909f66SGal Pressman resp.sub_cqs_per_cq = dev->dev_attr.sub_cqs_per_cq; 184740909f66SGal Pressman resp.inline_buf_size = dev->dev_attr.inline_buf_size; 184840909f66SGal Pressman resp.max_llq_size = dev->dev_attr.max_llq_size; 1849556c811fSGal Pressman resp.max_tx_batch = dev->dev_attr.max_tx_batch; 1850da2924bdSGal Pressman resp.min_sq_wr = dev->dev_attr.min_sq_depth; 185140909f66SGal Pressman 185240909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 185340909f66SGal Pressman min(sizeof(resp), udata->outlen)); 185440909f66SGal Pressman if (err) 185540909f66SGal Pressman goto err_dealloc_uar; 185640909f66SGal Pressman 185740909f66SGal Pressman return 0; 185840909f66SGal Pressman 185940909f66SGal Pressman err_dealloc_uar: 186040909f66SGal Pressman efa_dealloc_uar(dev, result.uarn); 186140909f66SGal Pressman err_out: 1862215b88acSGal Pressman atomic64_inc(&dev->stats.alloc_ucontext_err); 186340909f66SGal Pressman return err; 186440909f66SGal Pressman } 186540909f66SGal Pressman 186640909f66SGal Pressman void efa_dealloc_ucontext(struct ib_ucontext *ibucontext) 186740909f66SGal Pressman { 186840909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 186940909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 187040909f66SGal Pressman 187140909f66SGal Pressman efa_dealloc_uar(dev, ucontext->uarn); 187240909f66SGal Pressman } 187340909f66SGal Pressman 1874e84d3c18SMichal Kalderon void efa_mmap_free(struct rdma_user_mmap_entry *rdma_entry) 187540909f66SGal Pressman { 1876e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry = to_emmap(rdma_entry); 187740909f66SGal Pressman 1878e84d3c18SMichal Kalderon kfree(entry); 187940909f66SGal Pressman } 188040909f66SGal Pressman 1881e84d3c18SMichal Kalderon static int __efa_mmap(struct efa_dev *dev, struct efa_ucontext *ucontext, 1882e84d3c18SMichal Kalderon struct vm_area_struct *vma) 1883e84d3c18SMichal Kalderon { 1884e84d3c18SMichal Kalderon struct rdma_user_mmap_entry *rdma_entry; 1885e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry; 1886e84d3c18SMichal Kalderon unsigned long va; 1887e84d3c18SMichal Kalderon int err = 0; 1888e84d3c18SMichal Kalderon u64 pfn; 1889e84d3c18SMichal Kalderon 1890e84d3c18SMichal Kalderon rdma_entry = rdma_user_mmap_entry_get(&ucontext->ibucontext, vma); 1891e84d3c18SMichal Kalderon if (!rdma_entry) { 189240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 1893e84d3c18SMichal Kalderon "pgoff[%#lx] does not have valid entry\n", 1894e84d3c18SMichal Kalderon vma->vm_pgoff); 1895215b88acSGal Pressman atomic64_inc(&dev->stats.mmap_err); 1896e84d3c18SMichal Kalderon return -EINVAL; 1897e84d3c18SMichal Kalderon } 1898e84d3c18SMichal Kalderon entry = to_emmap(rdma_entry); 1899e84d3c18SMichal Kalderon 1900e84d3c18SMichal Kalderon ibdev_dbg(&dev->ibdev, 1901e84d3c18SMichal Kalderon "Mapping address[%#llx], length[%#zx], mmap_flag[%d]\n", 1902e84d3c18SMichal Kalderon entry->address, rdma_entry->npages * PAGE_SIZE, 1903e84d3c18SMichal Kalderon entry->mmap_flag); 190440909f66SGal Pressman 190540909f66SGal Pressman pfn = entry->address >> PAGE_SHIFT; 190640909f66SGal Pressman switch (entry->mmap_flag) { 190740909f66SGal Pressman case EFA_MMAP_IO_NC: 1908e84d3c18SMichal Kalderon err = rdma_user_mmap_io(&ucontext->ibucontext, vma, pfn, 1909e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE, 1910c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 1911e84d3c18SMichal Kalderon rdma_entry); 191240909f66SGal Pressman break; 191340909f66SGal Pressman case EFA_MMAP_IO_WC: 1914e84d3c18SMichal Kalderon err = rdma_user_mmap_io(&ucontext->ibucontext, vma, pfn, 1915e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE, 1916c043ff2cSMichal Kalderon pgprot_writecombine(vma->vm_page_prot), 1917e84d3c18SMichal Kalderon rdma_entry); 191840909f66SGal Pressman break; 191940909f66SGal Pressman case EFA_MMAP_DMA_PAGE: 192040909f66SGal Pressman for (va = vma->vm_start; va < vma->vm_end; 192140909f66SGal Pressman va += PAGE_SIZE, pfn++) { 192240909f66SGal Pressman err = vm_insert_page(vma, va, pfn_to_page(pfn)); 192340909f66SGal Pressman if (err) 192440909f66SGal Pressman break; 192540909f66SGal Pressman } 192640909f66SGal Pressman break; 192740909f66SGal Pressman default: 192840909f66SGal Pressman err = -EINVAL; 192940909f66SGal Pressman } 193040909f66SGal Pressman 1931eca5757fSGal Pressman if (err) { 193240909f66SGal Pressman ibdev_dbg( 193340909f66SGal Pressman &dev->ibdev, 1934e84d3c18SMichal Kalderon "Couldn't mmap address[%#llx] length[%#zx] mmap_flag[%d] err[%d]\n", 1935e84d3c18SMichal Kalderon entry->address, rdma_entry->npages * PAGE_SIZE, 1936e84d3c18SMichal Kalderon entry->mmap_flag, err); 1937215b88acSGal Pressman atomic64_inc(&dev->stats.mmap_err); 1938eca5757fSGal Pressman } 193940909f66SGal Pressman 1940e84d3c18SMichal Kalderon rdma_user_mmap_entry_put(rdma_entry); 1941e84d3c18SMichal Kalderon return err; 1942b41f7572SGal Pressman } 1943b41f7572SGal Pressman 194440909f66SGal Pressman int efa_mmap(struct ib_ucontext *ibucontext, 194540909f66SGal Pressman struct vm_area_struct *vma) 194640909f66SGal Pressman { 194740909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 194840909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 1949e84d3c18SMichal Kalderon size_t length = vma->vm_end - vma->vm_start; 195040909f66SGal Pressman 195140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 1952e84d3c18SMichal Kalderon "start %#lx, end %#lx, length = %#zx, pgoff = %#lx\n", 1953e84d3c18SMichal Kalderon vma->vm_start, vma->vm_end, length, vma->vm_pgoff); 195440909f66SGal Pressman 1955e84d3c18SMichal Kalderon return __efa_mmap(dev, ucontext, vma); 195640909f66SGal Pressman } 195740909f66SGal Pressman 195840909f66SGal Pressman static int efa_ah_destroy(struct efa_dev *dev, struct efa_ah *ah) 195940909f66SGal Pressman { 196040909f66SGal Pressman struct efa_com_destroy_ah_params params = { 196140909f66SGal Pressman .ah = ah->ah, 196240909f66SGal Pressman .pdn = to_epd(ah->ibah.pd)->pdn, 196340909f66SGal Pressman }; 196440909f66SGal Pressman 196540909f66SGal Pressman return efa_com_destroy_ah(&dev->edev, ¶ms); 196640909f66SGal Pressman } 196740909f66SGal Pressman 196840909f66SGal Pressman int efa_create_ah(struct ib_ah *ibah, 1969fa5d010cSMaor Gottlieb struct rdma_ah_init_attr *init_attr, 197040909f66SGal Pressman struct ib_udata *udata) 197140909f66SGal Pressman { 1972fa5d010cSMaor Gottlieb struct rdma_ah_attr *ah_attr = init_attr->ah_attr; 197340909f66SGal Pressman struct efa_dev *dev = to_edev(ibah->device); 197440909f66SGal Pressman struct efa_com_create_ah_params params = {}; 197540909f66SGal Pressman struct efa_ibv_create_ah_resp resp = {}; 197640909f66SGal Pressman struct efa_com_create_ah_result result; 197740909f66SGal Pressman struct efa_ah *ah = to_eah(ibah); 197840909f66SGal Pressman int err; 197940909f66SGal Pressman 1980fa5d010cSMaor Gottlieb if (!(init_attr->flags & RDMA_CREATE_AH_SLEEPABLE)) { 198140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 198240909f66SGal Pressman "Create address handle is not supported in atomic context\n"); 198340909f66SGal Pressman err = -EOPNOTSUPP; 198440909f66SGal Pressman goto err_out; 198540909f66SGal Pressman } 198640909f66SGal Pressman 198740909f66SGal Pressman if (udata->inlen && 198840909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 198940909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Incompatible ABI params\n"); 199040909f66SGal Pressman err = -EINVAL; 199140909f66SGal Pressman goto err_out; 199240909f66SGal Pressman } 199340909f66SGal Pressman 199440909f66SGal Pressman memcpy(params.dest_addr, ah_attr->grh.dgid.raw, 199540909f66SGal Pressman sizeof(params.dest_addr)); 199640909f66SGal Pressman params.pdn = to_epd(ibah->pd)->pdn; 199740909f66SGal Pressman err = efa_com_create_ah(&dev->edev, ¶ms, &result); 199840909f66SGal Pressman if (err) 199940909f66SGal Pressman goto err_out; 200040909f66SGal Pressman 200140909f66SGal Pressman memcpy(ah->id, ah_attr->grh.dgid.raw, sizeof(ah->id)); 200240909f66SGal Pressman ah->ah = result.ah; 200340909f66SGal Pressman 200440909f66SGal Pressman resp.efa_address_handle = result.ah; 200540909f66SGal Pressman 200640909f66SGal Pressman if (udata->outlen) { 200740909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 200840909f66SGal Pressman min(sizeof(resp), udata->outlen)); 200940909f66SGal Pressman if (err) { 201040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 201140909f66SGal Pressman "Failed to copy udata for create_ah response\n"); 201240909f66SGal Pressman goto err_destroy_ah; 201340909f66SGal Pressman } 201440909f66SGal Pressman } 201540909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Created ah[%d]\n", ah->ah); 201640909f66SGal Pressman 201740909f66SGal Pressman return 0; 201840909f66SGal Pressman 201940909f66SGal Pressman err_destroy_ah: 202040909f66SGal Pressman efa_ah_destroy(dev, ah); 202140909f66SGal Pressman err_out: 2022215b88acSGal Pressman atomic64_inc(&dev->stats.create_ah_err); 202340909f66SGal Pressman return err; 202440909f66SGal Pressman } 202540909f66SGal Pressman 20269a9ebf8cSLeon Romanovsky int efa_destroy_ah(struct ib_ah *ibah, u32 flags) 202740909f66SGal Pressman { 202840909f66SGal Pressman struct efa_dev *dev = to_edev(ibah->pd->device); 202940909f66SGal Pressman struct efa_ah *ah = to_eah(ibah); 203040909f66SGal Pressman 203140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Destroy ah[%d]\n", ah->ah); 203240909f66SGal Pressman 203340909f66SGal Pressman if (!(flags & RDMA_DESTROY_AH_SLEEPABLE)) { 203440909f66SGal Pressman ibdev_dbg(&dev->ibdev, 203540909f66SGal Pressman "Destroy address handle is not supported in atomic context\n"); 20369a9ebf8cSLeon Romanovsky return -EOPNOTSUPP; 203740909f66SGal Pressman } 203840909f66SGal Pressman 203940909f66SGal Pressman efa_ah_destroy(dev, ah); 20409a9ebf8cSLeon Romanovsky return 0; 204140909f66SGal Pressman } 204240909f66SGal Pressman 20438c1b4316SGal Pressman struct rdma_hw_stats *efa_alloc_hw_port_stats(struct ib_device *ibdev, 20448c1b4316SGal Pressman u32 port_num) 204516e9111eSGal Pressman { 204613f30b0fSAharon Landau return rdma_alloc_hw_stats_struct(efa_port_stats_descs, 204713f30b0fSAharon Landau ARRAY_SIZE(efa_port_stats_descs), 204816e9111eSGal Pressman RDMA_HW_STATS_DEFAULT_LIFESPAN); 204916e9111eSGal Pressman } 205016e9111eSGal Pressman 20514b5f4d3fSJason Gunthorpe struct rdma_hw_stats *efa_alloc_hw_device_stats(struct ib_device *ibdev) 20524b5f4d3fSJason Gunthorpe { 205313f30b0fSAharon Landau return rdma_alloc_hw_stats_struct(efa_device_stats_descs, 205413f30b0fSAharon Landau ARRAY_SIZE(efa_device_stats_descs), 20558c1b4316SGal Pressman RDMA_HW_STATS_DEFAULT_LIFESPAN); 20564b5f4d3fSJason Gunthorpe } 20574b5f4d3fSJason Gunthorpe 20588c1b4316SGal Pressman static int efa_fill_device_stats(struct efa_dev *dev, 20598c1b4316SGal Pressman struct rdma_hw_stats *stats) 20608c1b4316SGal Pressman { 20618c1b4316SGal Pressman struct efa_com_stats_admin *as = &dev->edev.aq.stats; 20628c1b4316SGal Pressman struct efa_stats *s = &dev->stats; 20638c1b4316SGal Pressman 20648c1b4316SGal Pressman stats->value[EFA_SUBMITTED_CMDS] = atomic64_read(&as->submitted_cmd); 20658c1b4316SGal Pressman stats->value[EFA_COMPLETED_CMDS] = atomic64_read(&as->completed_cmd); 20668c1b4316SGal Pressman stats->value[EFA_CMDS_ERR] = atomic64_read(&as->cmd_err); 20678c1b4316SGal Pressman stats->value[EFA_NO_COMPLETION_CMDS] = atomic64_read(&as->no_completion); 20688c1b4316SGal Pressman 20698c1b4316SGal Pressman stats->value[EFA_KEEP_ALIVE_RCVD] = atomic64_read(&s->keep_alive_rcvd); 20708c1b4316SGal Pressman stats->value[EFA_ALLOC_PD_ERR] = atomic64_read(&s->alloc_pd_err); 20718c1b4316SGal Pressman stats->value[EFA_CREATE_QP_ERR] = atomic64_read(&s->create_qp_err); 20728c1b4316SGal Pressman stats->value[EFA_CREATE_CQ_ERR] = atomic64_read(&s->create_cq_err); 20738c1b4316SGal Pressman stats->value[EFA_REG_MR_ERR] = atomic64_read(&s->reg_mr_err); 20748c1b4316SGal Pressman stats->value[EFA_ALLOC_UCONTEXT_ERR] = 20758c1b4316SGal Pressman atomic64_read(&s->alloc_ucontext_err); 20768c1b4316SGal Pressman stats->value[EFA_CREATE_AH_ERR] = atomic64_read(&s->create_ah_err); 20778c1b4316SGal Pressman stats->value[EFA_MMAP_ERR] = atomic64_read(&s->mmap_err); 20788c1b4316SGal Pressman 207913f30b0fSAharon Landau return ARRAY_SIZE(efa_device_stats_descs); 20808c1b4316SGal Pressman } 20818c1b4316SGal Pressman 20828c1b4316SGal Pressman static int efa_fill_port_stats(struct efa_dev *dev, struct rdma_hw_stats *stats, 20838c1b4316SGal Pressman u32 port_num) 208416e9111eSGal Pressman { 208516e9111eSGal Pressman struct efa_com_get_stats_params params = {}; 208616e9111eSGal Pressman union efa_com_get_stats_result result; 2087*113383efSMichael Margolin struct efa_com_rdma_write_stats *rws; 2088b0cff387SDaniel Kranzdorf struct efa_com_rdma_read_stats *rrs; 2089b0cff387SDaniel Kranzdorf struct efa_com_messages_stats *ms; 209016e9111eSGal Pressman struct efa_com_basic_stats *bs; 209116e9111eSGal Pressman int err; 209216e9111eSGal Pressman 209316e9111eSGal Pressman params.scope = EFA_ADMIN_GET_STATS_SCOPE_ALL; 2094b0cff387SDaniel Kranzdorf params.type = EFA_ADMIN_GET_STATS_TYPE_BASIC; 209516e9111eSGal Pressman 209616e9111eSGal Pressman err = efa_com_get_stats(&dev->edev, ¶ms, &result); 209716e9111eSGal Pressman if (err) 209816e9111eSGal Pressman return err; 209916e9111eSGal Pressman 210016e9111eSGal Pressman bs = &result.basic_stats; 210116e9111eSGal Pressman stats->value[EFA_TX_BYTES] = bs->tx_bytes; 210216e9111eSGal Pressman stats->value[EFA_TX_PKTS] = bs->tx_pkts; 210316e9111eSGal Pressman stats->value[EFA_RX_BYTES] = bs->rx_bytes; 210416e9111eSGal Pressman stats->value[EFA_RX_PKTS] = bs->rx_pkts; 210516e9111eSGal Pressman stats->value[EFA_RX_DROPS] = bs->rx_drops; 210616e9111eSGal Pressman 2107b0cff387SDaniel Kranzdorf params.type = EFA_ADMIN_GET_STATS_TYPE_MESSAGES; 2108b0cff387SDaniel Kranzdorf err = efa_com_get_stats(&dev->edev, ¶ms, &result); 2109b0cff387SDaniel Kranzdorf if (err) 2110b0cff387SDaniel Kranzdorf return err; 2111b0cff387SDaniel Kranzdorf 2112b0cff387SDaniel Kranzdorf ms = &result.messages_stats; 2113b0cff387SDaniel Kranzdorf stats->value[EFA_SEND_BYTES] = ms->send_bytes; 2114b0cff387SDaniel Kranzdorf stats->value[EFA_SEND_WRS] = ms->send_wrs; 2115b0cff387SDaniel Kranzdorf stats->value[EFA_RECV_BYTES] = ms->recv_bytes; 2116b0cff387SDaniel Kranzdorf stats->value[EFA_RECV_WRS] = ms->recv_wrs; 2117b0cff387SDaniel Kranzdorf 2118b0cff387SDaniel Kranzdorf params.type = EFA_ADMIN_GET_STATS_TYPE_RDMA_READ; 2119b0cff387SDaniel Kranzdorf err = efa_com_get_stats(&dev->edev, ¶ms, &result); 2120b0cff387SDaniel Kranzdorf if (err) 2121b0cff387SDaniel Kranzdorf return err; 2122b0cff387SDaniel Kranzdorf 2123b0cff387SDaniel Kranzdorf rrs = &result.rdma_read_stats; 2124b0cff387SDaniel Kranzdorf stats->value[EFA_RDMA_READ_WRS] = rrs->read_wrs; 2125b0cff387SDaniel Kranzdorf stats->value[EFA_RDMA_READ_BYTES] = rrs->read_bytes; 2126b0cff387SDaniel Kranzdorf stats->value[EFA_RDMA_READ_WR_ERR] = rrs->read_wr_err; 2127b0cff387SDaniel Kranzdorf stats->value[EFA_RDMA_READ_RESP_BYTES] = rrs->read_resp_bytes; 2128b0cff387SDaniel Kranzdorf 2129*113383efSMichael Margolin if (EFA_DEV_CAP(dev, RDMA_WRITE)) { 2130*113383efSMichael Margolin params.type = EFA_ADMIN_GET_STATS_TYPE_RDMA_WRITE; 2131*113383efSMichael Margolin err = efa_com_get_stats(&dev->edev, ¶ms, &result); 2132*113383efSMichael Margolin if (err) 2133*113383efSMichael Margolin return err; 2134*113383efSMichael Margolin 2135*113383efSMichael Margolin rws = &result.rdma_write_stats; 2136*113383efSMichael Margolin stats->value[EFA_RDMA_WRITE_WRS] = rws->write_wrs; 2137*113383efSMichael Margolin stats->value[EFA_RDMA_WRITE_BYTES] = rws->write_bytes; 2138*113383efSMichael Margolin stats->value[EFA_RDMA_WRITE_WR_ERR] = rws->write_wr_err; 2139*113383efSMichael Margolin stats->value[EFA_RDMA_WRITE_RECV_BYTES] = rws->write_recv_bytes; 2140*113383efSMichael Margolin } 2141*113383efSMichael Margolin 214213f30b0fSAharon Landau return ARRAY_SIZE(efa_port_stats_descs); 21438c1b4316SGal Pressman } 214416e9111eSGal Pressman 21458c1b4316SGal Pressman int efa_get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats, 21468c1b4316SGal Pressman u32 port_num, int index) 21478c1b4316SGal Pressman { 21488c1b4316SGal Pressman if (port_num) 21498c1b4316SGal Pressman return efa_fill_port_stats(to_edev(ibdev), stats, port_num); 21508c1b4316SGal Pressman else 21518c1b4316SGal Pressman return efa_fill_device_stats(to_edev(ibdev), stats); 215216e9111eSGal Pressman } 215316e9111eSGal Pressman 215440909f66SGal Pressman enum rdma_link_layer efa_port_link_layer(struct ib_device *ibdev, 21551fb7f897SMark Bloch u32 port_num) 215640909f66SGal Pressman { 215740909f66SGal Pressman return IB_LINK_LAYER_UNSPECIFIED; 215840909f66SGal Pressman } 215940909f66SGal Pressman 2160