140909f66SGal Pressman // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 240909f66SGal Pressman /* 3*ff6629f8SGal Pressman * Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All rights reserved. 440909f66SGal Pressman */ 540909f66SGal Pressman 640909f66SGal Pressman #include <linux/vmalloc.h> 740909f66SGal Pressman 840909f66SGal Pressman #include <rdma/ib_addr.h> 940909f66SGal Pressman #include <rdma/ib_umem.h> 1040909f66SGal Pressman #include <rdma/ib_user_verbs.h> 1140909f66SGal Pressman #include <rdma/ib_verbs.h> 1240909f66SGal Pressman #include <rdma/uverbs_ioctl.h> 1340909f66SGal Pressman 1440909f66SGal Pressman #include "efa.h" 1540909f66SGal Pressman 1640909f66SGal Pressman enum { 1740909f66SGal Pressman EFA_MMAP_DMA_PAGE = 0, 1840909f66SGal Pressman EFA_MMAP_IO_WC, 1940909f66SGal Pressman EFA_MMAP_IO_NC, 2040909f66SGal Pressman }; 2140909f66SGal Pressman 2240909f66SGal Pressman #define EFA_AENQ_ENABLED_GROUPS \ 2340909f66SGal Pressman (BIT(EFA_ADMIN_FATAL_ERROR) | BIT(EFA_ADMIN_WARNING) | \ 2440909f66SGal Pressman BIT(EFA_ADMIN_NOTIFICATION) | BIT(EFA_ADMIN_KEEP_ALIVE)) 2540909f66SGal Pressman 26e84d3c18SMichal Kalderon struct efa_user_mmap_entry { 27e84d3c18SMichal Kalderon struct rdma_user_mmap_entry rdma_entry; 2840909f66SGal Pressman u64 address; 2940909f66SGal Pressman u8 mmap_flag; 3040909f66SGal Pressman }; 3140909f66SGal Pressman 3216e9111eSGal Pressman #define EFA_DEFINE_STATS(op) \ 3316e9111eSGal Pressman op(EFA_TX_BYTES, "tx_bytes") \ 3416e9111eSGal Pressman op(EFA_TX_PKTS, "tx_pkts") \ 3516e9111eSGal Pressman op(EFA_RX_BYTES, "rx_bytes") \ 3616e9111eSGal Pressman op(EFA_RX_PKTS, "rx_pkts") \ 3716e9111eSGal Pressman op(EFA_RX_DROPS, "rx_drops") \ 3816e9111eSGal Pressman op(EFA_SUBMITTED_CMDS, "submitted_cmds") \ 3916e9111eSGal Pressman op(EFA_COMPLETED_CMDS, "completed_cmds") \ 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") \ 4416e9111eSGal Pressman op(EFA_REG_MR_ERR, "reg_mr_err") \ 4516e9111eSGal Pressman op(EFA_ALLOC_UCONTEXT_ERR, "alloc_ucontext_err") \ 4616e9111eSGal Pressman op(EFA_CREATE_AH_ERR, "create_ah_err") 4716e9111eSGal Pressman 4816e9111eSGal Pressman #define EFA_STATS_ENUM(ename, name) ename, 4916e9111eSGal Pressman #define EFA_STATS_STR(ename, name) [ename] = name, 5016e9111eSGal Pressman 5116e9111eSGal Pressman enum efa_hw_stats { 5216e9111eSGal Pressman EFA_DEFINE_STATS(EFA_STATS_ENUM) 5316e9111eSGal Pressman }; 5416e9111eSGal Pressman 5516e9111eSGal Pressman static const char *const efa_stats_names[] = { 5616e9111eSGal Pressman EFA_DEFINE_STATS(EFA_STATS_STR) 5716e9111eSGal Pressman }; 5816e9111eSGal Pressman 5940909f66SGal Pressman #define EFA_CHUNK_PAYLOAD_SHIFT 12 6040909f66SGal Pressman #define EFA_CHUNK_PAYLOAD_SIZE BIT(EFA_CHUNK_PAYLOAD_SHIFT) 6140909f66SGal Pressman #define EFA_CHUNK_PAYLOAD_PTR_SIZE 8 6240909f66SGal Pressman 6340909f66SGal Pressman #define EFA_CHUNK_SHIFT 12 6440909f66SGal Pressman #define EFA_CHUNK_SIZE BIT(EFA_CHUNK_SHIFT) 6540909f66SGal Pressman #define EFA_CHUNK_PTR_SIZE sizeof(struct efa_com_ctrl_buff_info) 6640909f66SGal Pressman 6740909f66SGal Pressman #define EFA_PTRS_PER_CHUNK \ 6840909f66SGal Pressman ((EFA_CHUNK_SIZE - EFA_CHUNK_PTR_SIZE) / EFA_CHUNK_PAYLOAD_PTR_SIZE) 6940909f66SGal Pressman 7040909f66SGal Pressman #define EFA_CHUNK_USED_SIZE \ 7140909f66SGal Pressman ((EFA_PTRS_PER_CHUNK * EFA_CHUNK_PAYLOAD_PTR_SIZE) + EFA_CHUNK_PTR_SIZE) 7240909f66SGal Pressman 7340909f66SGal Pressman struct pbl_chunk { 7440909f66SGal Pressman dma_addr_t dma_addr; 7540909f66SGal Pressman u64 *buf; 7640909f66SGal Pressman u32 length; 7740909f66SGal Pressman }; 7840909f66SGal Pressman 7940909f66SGal Pressman struct pbl_chunk_list { 8040909f66SGal Pressman struct pbl_chunk *chunks; 8140909f66SGal Pressman unsigned int size; 8240909f66SGal Pressman }; 8340909f66SGal Pressman 8440909f66SGal Pressman struct pbl_context { 8540909f66SGal Pressman union { 8640909f66SGal Pressman struct { 8740909f66SGal Pressman dma_addr_t dma_addr; 8840909f66SGal Pressman } continuous; 8940909f66SGal Pressman struct { 9040909f66SGal Pressman u32 pbl_buf_size_in_pages; 9140909f66SGal Pressman struct scatterlist *sgl; 9240909f66SGal Pressman int sg_dma_cnt; 9340909f66SGal Pressman struct pbl_chunk_list chunk_list; 9440909f66SGal Pressman } indirect; 9540909f66SGal Pressman } phys; 9640909f66SGal Pressman u64 *pbl_buf; 9740909f66SGal Pressman u32 pbl_buf_size_in_bytes; 9840909f66SGal Pressman u8 physically_continuous; 9940909f66SGal Pressman }; 10040909f66SGal Pressman 10140909f66SGal Pressman static inline struct efa_dev *to_edev(struct ib_device *ibdev) 10240909f66SGal Pressman { 10340909f66SGal Pressman return container_of(ibdev, struct efa_dev, ibdev); 10440909f66SGal Pressman } 10540909f66SGal Pressman 10640909f66SGal Pressman static inline struct efa_ucontext *to_eucontext(struct ib_ucontext *ibucontext) 10740909f66SGal Pressman { 10840909f66SGal Pressman return container_of(ibucontext, struct efa_ucontext, ibucontext); 10940909f66SGal Pressman } 11040909f66SGal Pressman 11140909f66SGal Pressman static inline struct efa_pd *to_epd(struct ib_pd *ibpd) 11240909f66SGal Pressman { 11340909f66SGal Pressman return container_of(ibpd, struct efa_pd, ibpd); 11440909f66SGal Pressman } 11540909f66SGal Pressman 11640909f66SGal Pressman static inline struct efa_mr *to_emr(struct ib_mr *ibmr) 11740909f66SGal Pressman { 11840909f66SGal Pressman return container_of(ibmr, struct efa_mr, ibmr); 11940909f66SGal Pressman } 12040909f66SGal Pressman 12140909f66SGal Pressman static inline struct efa_qp *to_eqp(struct ib_qp *ibqp) 12240909f66SGal Pressman { 12340909f66SGal Pressman return container_of(ibqp, struct efa_qp, ibqp); 12440909f66SGal Pressman } 12540909f66SGal Pressman 12640909f66SGal Pressman static inline struct efa_cq *to_ecq(struct ib_cq *ibcq) 12740909f66SGal Pressman { 12840909f66SGal Pressman return container_of(ibcq, struct efa_cq, ibcq); 12940909f66SGal Pressman } 13040909f66SGal Pressman 13140909f66SGal Pressman static inline struct efa_ah *to_eah(struct ib_ah *ibah) 13240909f66SGal Pressman { 13340909f66SGal Pressman return container_of(ibah, struct efa_ah, ibah); 13440909f66SGal Pressman } 13540909f66SGal Pressman 136e84d3c18SMichal Kalderon static inline struct efa_user_mmap_entry * 137e84d3c18SMichal Kalderon to_emmap(struct rdma_user_mmap_entry *rdma_entry) 138e84d3c18SMichal Kalderon { 139e84d3c18SMichal Kalderon return container_of(rdma_entry, struct efa_user_mmap_entry, rdma_entry); 140e84d3c18SMichal Kalderon } 141e84d3c18SMichal Kalderon 142666e8ff5SDaniel Kranzdorf static inline bool is_rdma_read_cap(struct efa_dev *dev) 143666e8ff5SDaniel Kranzdorf { 144666e8ff5SDaniel Kranzdorf return dev->dev_attr.device_caps & EFA_ADMIN_FEATURE_DEVICE_ATTR_DESC_RDMA_READ_MASK; 145666e8ff5SDaniel Kranzdorf } 146666e8ff5SDaniel Kranzdorf 14740909f66SGal Pressman #define field_avail(x, fld, sz) (offsetof(typeof(x), fld) + \ 148c593642cSPankaj Bharadiya sizeof_field(typeof(x), fld) <= (sz)) 14940909f66SGal Pressman 15040909f66SGal Pressman #define is_reserved_cleared(reserved) \ 15140909f66SGal Pressman !memchr_inv(reserved, 0, sizeof(reserved)) 15240909f66SGal Pressman 15340909f66SGal Pressman static void *efa_zalloc_mapped(struct efa_dev *dev, dma_addr_t *dma_addr, 15440909f66SGal Pressman size_t size, enum dma_data_direction dir) 15540909f66SGal Pressman { 15640909f66SGal Pressman void *addr; 15740909f66SGal Pressman 15840909f66SGal Pressman addr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 15940909f66SGal Pressman if (!addr) 16040909f66SGal Pressman return NULL; 16140909f66SGal Pressman 16240909f66SGal Pressman *dma_addr = dma_map_single(&dev->pdev->dev, addr, size, dir); 16340909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, *dma_addr)) { 16440909f66SGal Pressman ibdev_err(&dev->ibdev, "Failed to map DMA address\n"); 16540909f66SGal Pressman free_pages_exact(addr, size); 16640909f66SGal Pressman return NULL; 16740909f66SGal Pressman } 16840909f66SGal Pressman 16940909f66SGal Pressman return addr; 17040909f66SGal Pressman } 17140909f66SGal Pressman 172*ff6629f8SGal Pressman static void efa_free_mapped(struct efa_dev *dev, void *cpu_addr, 173*ff6629f8SGal Pressman dma_addr_t dma_addr, 174*ff6629f8SGal Pressman size_t size, enum dma_data_direction dir) 175*ff6629f8SGal Pressman { 176*ff6629f8SGal Pressman dma_unmap_single(&dev->pdev->dev, dma_addr, size, dir); 177*ff6629f8SGal Pressman free_pages_exact(cpu_addr, size); 178*ff6629f8SGal Pressman } 179*ff6629f8SGal Pressman 18040909f66SGal Pressman int efa_query_device(struct ib_device *ibdev, 18140909f66SGal Pressman struct ib_device_attr *props, 18240909f66SGal Pressman struct ib_udata *udata) 18340909f66SGal Pressman { 18440909f66SGal Pressman struct efa_com_get_device_attr_result *dev_attr; 18540909f66SGal Pressman struct efa_ibv_ex_query_device_resp resp = {}; 18640909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 18740909f66SGal Pressman int err; 18840909f66SGal Pressman 18940909f66SGal Pressman if (udata && udata->inlen && 19040909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 19140909f66SGal Pressman ibdev_dbg(ibdev, 19240909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 19340909f66SGal Pressman return -EINVAL; 19440909f66SGal Pressman } 19540909f66SGal Pressman 19640909f66SGal Pressman dev_attr = &dev->dev_attr; 19740909f66SGal Pressman 19840909f66SGal Pressman memset(props, 0, sizeof(*props)); 19940909f66SGal Pressman props->max_mr_size = dev_attr->max_mr_pages * PAGE_SIZE; 20040909f66SGal Pressman props->page_size_cap = dev_attr->page_size_cap; 20140909f66SGal Pressman props->vendor_id = dev->pdev->vendor; 20240909f66SGal Pressman props->vendor_part_id = dev->pdev->device; 20340909f66SGal Pressman props->hw_ver = dev->pdev->subsystem_device; 20440909f66SGal Pressman props->max_qp = dev_attr->max_qp; 20540909f66SGal Pressman props->max_cq = dev_attr->max_cq; 20640909f66SGal Pressman props->max_pd = dev_attr->max_pd; 20740909f66SGal Pressman props->max_mr = dev_attr->max_mr; 20840909f66SGal Pressman props->max_ah = dev_attr->max_ah; 20940909f66SGal Pressman props->max_cqe = dev_attr->max_cq_depth; 21040909f66SGal Pressman props->max_qp_wr = min_t(u32, dev_attr->max_sq_depth, 21140909f66SGal Pressman dev_attr->max_rq_depth); 21240909f66SGal Pressman props->max_send_sge = dev_attr->max_sq_sge; 21340909f66SGal Pressman props->max_recv_sge = dev_attr->max_rq_sge; 214666e8ff5SDaniel Kranzdorf props->max_sge_rd = dev_attr->max_wr_rdma_sge; 21540909f66SGal Pressman 21640909f66SGal Pressman if (udata && udata->outlen) { 21740909f66SGal Pressman resp.max_sq_sge = dev_attr->max_sq_sge; 21840909f66SGal Pressman resp.max_rq_sge = dev_attr->max_rq_sge; 21940909f66SGal Pressman resp.max_sq_wr = dev_attr->max_sq_depth; 22040909f66SGal Pressman resp.max_rq_wr = dev_attr->max_rq_depth; 221666e8ff5SDaniel Kranzdorf resp.max_rdma_size = dev_attr->max_rdma_size; 222666e8ff5SDaniel Kranzdorf 223666e8ff5SDaniel Kranzdorf if (is_rdma_read_cap(dev)) 224666e8ff5SDaniel Kranzdorf resp.device_caps |= EFA_QUERY_DEVICE_CAPS_RDMA_READ; 22540909f66SGal Pressman 22640909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 22740909f66SGal Pressman min(sizeof(resp), udata->outlen)); 22840909f66SGal Pressman if (err) { 22940909f66SGal Pressman ibdev_dbg(ibdev, 23040909f66SGal Pressman "Failed to copy udata for query_device\n"); 23140909f66SGal Pressman return err; 23240909f66SGal Pressman } 23340909f66SGal Pressman } 23440909f66SGal Pressman 23540909f66SGal Pressman return 0; 23640909f66SGal Pressman } 23740909f66SGal Pressman 23840909f66SGal Pressman int efa_query_port(struct ib_device *ibdev, u8 port, 23940909f66SGal Pressman struct ib_port_attr *props) 24040909f66SGal Pressman { 24140909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 24240909f66SGal Pressman 24340909f66SGal Pressman props->lmc = 1; 24440909f66SGal Pressman 24540909f66SGal Pressman props->state = IB_PORT_ACTIVE; 24672a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 24740909f66SGal Pressman props->gid_tbl_len = 1; 24840909f66SGal Pressman props->pkey_tbl_len = 1; 24940909f66SGal Pressman props->active_speed = IB_SPEED_EDR; 25040909f66SGal Pressman props->active_width = IB_WIDTH_4X; 251bcf7cc53SGal Pressman props->max_mtu = ib_mtu_int_to_enum(dev->dev_attr.mtu); 252bcf7cc53SGal Pressman props->active_mtu = ib_mtu_int_to_enum(dev->dev_attr.mtu); 253bcf7cc53SGal Pressman props->max_msg_sz = dev->dev_attr.mtu; 25440909f66SGal Pressman props->max_vl_num = 1; 25540909f66SGal Pressman 25640909f66SGal Pressman return 0; 25740909f66SGal Pressman } 25840909f66SGal Pressman 25940909f66SGal Pressman int efa_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, 26040909f66SGal Pressman int qp_attr_mask, 26140909f66SGal Pressman struct ib_qp_init_attr *qp_init_attr) 26240909f66SGal Pressman { 26340909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->device); 26440909f66SGal Pressman struct efa_com_query_qp_params params = {}; 26540909f66SGal Pressman struct efa_com_query_qp_result result; 26640909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 26740909f66SGal Pressman int err; 26840909f66SGal Pressman 26940909f66SGal Pressman #define EFA_QUERY_QP_SUPP_MASK \ 27040909f66SGal Pressman (IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT | \ 27140909f66SGal Pressman IB_QP_QKEY | IB_QP_SQ_PSN | IB_QP_CAP) 27240909f66SGal Pressman 27340909f66SGal Pressman if (qp_attr_mask & ~EFA_QUERY_QP_SUPP_MASK) { 27440909f66SGal Pressman ibdev_dbg(&dev->ibdev, 27540909f66SGal Pressman "Unsupported qp_attr_mask[%#x] supported[%#x]\n", 27640909f66SGal Pressman qp_attr_mask, EFA_QUERY_QP_SUPP_MASK); 27740909f66SGal Pressman return -EOPNOTSUPP; 27840909f66SGal Pressman } 27940909f66SGal Pressman 28040909f66SGal Pressman memset(qp_attr, 0, sizeof(*qp_attr)); 28140909f66SGal Pressman memset(qp_init_attr, 0, sizeof(*qp_init_attr)); 28240909f66SGal Pressman 28340909f66SGal Pressman params.qp_handle = qp->qp_handle; 28440909f66SGal Pressman err = efa_com_query_qp(&dev->edev, ¶ms, &result); 28540909f66SGal Pressman if (err) 28640909f66SGal Pressman return err; 28740909f66SGal Pressman 28840909f66SGal Pressman qp_attr->qp_state = result.qp_state; 28940909f66SGal Pressman qp_attr->qkey = result.qkey; 29040909f66SGal Pressman qp_attr->sq_psn = result.sq_psn; 29140909f66SGal Pressman qp_attr->sq_draining = result.sq_draining; 29240909f66SGal Pressman qp_attr->port_num = 1; 29340909f66SGal Pressman 29440909f66SGal Pressman qp_attr->cap.max_send_wr = qp->max_send_wr; 29540909f66SGal Pressman qp_attr->cap.max_recv_wr = qp->max_recv_wr; 29640909f66SGal Pressman qp_attr->cap.max_send_sge = qp->max_send_sge; 29740909f66SGal Pressman qp_attr->cap.max_recv_sge = qp->max_recv_sge; 29840909f66SGal Pressman qp_attr->cap.max_inline_data = qp->max_inline_data; 29940909f66SGal Pressman 30040909f66SGal Pressman qp_init_attr->qp_type = ibqp->qp_type; 30140909f66SGal Pressman qp_init_attr->recv_cq = ibqp->recv_cq; 30240909f66SGal Pressman qp_init_attr->send_cq = ibqp->send_cq; 30340909f66SGal Pressman qp_init_attr->qp_context = ibqp->qp_context; 30440909f66SGal Pressman qp_init_attr->cap = qp_attr->cap; 30540909f66SGal Pressman 30640909f66SGal Pressman return 0; 30740909f66SGal Pressman } 30840909f66SGal Pressman 30940909f66SGal Pressman int efa_query_gid(struct ib_device *ibdev, u8 port, int index, 31040909f66SGal Pressman union ib_gid *gid) 31140909f66SGal Pressman { 31240909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 31340909f66SGal Pressman 314bcf7cc53SGal Pressman memcpy(gid->raw, dev->dev_attr.addr, sizeof(dev->dev_attr.addr)); 31540909f66SGal Pressman 31640909f66SGal Pressman return 0; 31740909f66SGal Pressman } 31840909f66SGal Pressman 31940909f66SGal Pressman int efa_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 32040909f66SGal Pressman u16 *pkey) 32140909f66SGal Pressman { 32240909f66SGal Pressman if (index > 0) 32340909f66SGal Pressman return -EINVAL; 32440909f66SGal Pressman 32540909f66SGal Pressman *pkey = 0xffff; 32640909f66SGal Pressman return 0; 32740909f66SGal Pressman } 32840909f66SGal Pressman 32940909f66SGal Pressman static int efa_pd_dealloc(struct efa_dev *dev, u16 pdn) 33040909f66SGal Pressman { 33140909f66SGal Pressman struct efa_com_dealloc_pd_params params = { 33240909f66SGal Pressman .pdn = pdn, 33340909f66SGal Pressman }; 33440909f66SGal Pressman 33540909f66SGal Pressman return efa_com_dealloc_pd(&dev->edev, ¶ms); 33640909f66SGal Pressman } 33740909f66SGal Pressman 33840909f66SGal Pressman int efa_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 33940909f66SGal Pressman { 34040909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 34140909f66SGal Pressman struct efa_ibv_alloc_pd_resp resp = {}; 34240909f66SGal Pressman struct efa_com_alloc_pd_result result; 34340909f66SGal Pressman struct efa_pd *pd = to_epd(ibpd); 34440909f66SGal Pressman int err; 34540909f66SGal Pressman 34640909f66SGal Pressman if (udata->inlen && 34740909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 34840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 34940909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 35040909f66SGal Pressman err = -EINVAL; 35140909f66SGal Pressman goto err_out; 35240909f66SGal Pressman } 35340909f66SGal Pressman 35440909f66SGal Pressman err = efa_com_alloc_pd(&dev->edev, &result); 35540909f66SGal Pressman if (err) 35640909f66SGal Pressman goto err_out; 35740909f66SGal Pressman 35840909f66SGal Pressman pd->pdn = result.pdn; 35940909f66SGal Pressman resp.pdn = result.pdn; 36040909f66SGal Pressman 36140909f66SGal Pressman if (udata->outlen) { 36240909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 36340909f66SGal Pressman min(sizeof(resp), udata->outlen)); 36440909f66SGal Pressman if (err) { 36540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 36640909f66SGal Pressman "Failed to copy udata for alloc_pd\n"); 36740909f66SGal Pressman goto err_dealloc_pd; 36840909f66SGal Pressman } 36940909f66SGal Pressman } 37040909f66SGal Pressman 37140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Allocated pd[%d]\n", pd->pdn); 37240909f66SGal Pressman 37340909f66SGal Pressman return 0; 37440909f66SGal Pressman 37540909f66SGal Pressman err_dealloc_pd: 37640909f66SGal Pressman efa_pd_dealloc(dev, result.pdn); 37740909f66SGal Pressman err_out: 37840909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.alloc_pd_err); 37940909f66SGal Pressman return err; 38040909f66SGal Pressman } 38140909f66SGal Pressman 38240909f66SGal Pressman void efa_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 38340909f66SGal Pressman { 38440909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 38540909f66SGal Pressman struct efa_pd *pd = to_epd(ibpd); 38640909f66SGal Pressman 38740909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Dealloc pd[%d]\n", pd->pdn); 38840909f66SGal Pressman efa_pd_dealloc(dev, pd->pdn); 38940909f66SGal Pressman } 39040909f66SGal Pressman 39140909f66SGal Pressman static int efa_destroy_qp_handle(struct efa_dev *dev, u32 qp_handle) 39240909f66SGal Pressman { 39340909f66SGal Pressman struct efa_com_destroy_qp_params params = { .qp_handle = qp_handle }; 39440909f66SGal Pressman 39540909f66SGal Pressman return efa_com_destroy_qp(&dev->edev, ¶ms); 39640909f66SGal Pressman } 39740909f66SGal Pressman 3980428c6efSGal Pressman static void efa_qp_user_mmap_entries_remove(struct efa_qp *qp) 399e84d3c18SMichal Kalderon { 400e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->rq_mmap_entry); 401e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->rq_db_mmap_entry); 402e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->llq_desc_mmap_entry); 403e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->sq_db_mmap_entry); 404e84d3c18SMichal Kalderon } 405e84d3c18SMichal Kalderon 40640909f66SGal Pressman int efa_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 40740909f66SGal Pressman { 40840909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->pd->device); 40940909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 41040909f66SGal Pressman int err; 41140909f66SGal Pressman 41240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Destroy qp[%u]\n", ibqp->qp_num); 413*ff6629f8SGal Pressman 414*ff6629f8SGal Pressman efa_qp_user_mmap_entries_remove(qp); 415*ff6629f8SGal Pressman 41640909f66SGal Pressman err = efa_destroy_qp_handle(dev, qp->qp_handle); 41740909f66SGal Pressman if (err) 41840909f66SGal Pressman return err; 41940909f66SGal Pressman 42040909f66SGal Pressman if (qp->rq_cpu_addr) { 42140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 42240909f66SGal Pressman "qp->cpu_addr[0x%p] freed: size[%lu], dma[%pad]\n", 42340909f66SGal Pressman qp->rq_cpu_addr, qp->rq_size, 42440909f66SGal Pressman &qp->rq_dma_addr); 425*ff6629f8SGal Pressman efa_free_mapped(dev, qp->rq_cpu_addr, qp->rq_dma_addr, 426*ff6629f8SGal Pressman qp->rq_size, DMA_TO_DEVICE); 42740909f66SGal Pressman } 42840909f66SGal Pressman 42940909f66SGal Pressman kfree(qp); 43040909f66SGal Pressman return 0; 43140909f66SGal Pressman } 43240909f66SGal Pressman 433e84d3c18SMichal Kalderon static struct rdma_user_mmap_entry* 434e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(struct ib_ucontext *ucontext, 435e84d3c18SMichal Kalderon u64 address, size_t length, 436e84d3c18SMichal Kalderon u8 mmap_flag, u64 *offset) 437e84d3c18SMichal Kalderon { 438e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL); 439e84d3c18SMichal Kalderon int err; 440e84d3c18SMichal Kalderon 441e84d3c18SMichal Kalderon if (!entry) 442e84d3c18SMichal Kalderon return NULL; 443e84d3c18SMichal Kalderon 444e84d3c18SMichal Kalderon entry->address = address; 445e84d3c18SMichal Kalderon entry->mmap_flag = mmap_flag; 446e84d3c18SMichal Kalderon 447e84d3c18SMichal Kalderon err = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry, 448e84d3c18SMichal Kalderon length); 449e84d3c18SMichal Kalderon if (err) { 450e84d3c18SMichal Kalderon kfree(entry); 451e84d3c18SMichal Kalderon return NULL; 452e84d3c18SMichal Kalderon } 453e84d3c18SMichal Kalderon *offset = rdma_user_mmap_get_offset(&entry->rdma_entry); 454e84d3c18SMichal Kalderon 455e84d3c18SMichal Kalderon return &entry->rdma_entry; 456e84d3c18SMichal Kalderon } 457e84d3c18SMichal Kalderon 45840909f66SGal Pressman static int qp_mmap_entries_setup(struct efa_qp *qp, 45940909f66SGal Pressman struct efa_dev *dev, 46040909f66SGal Pressman struct efa_ucontext *ucontext, 46140909f66SGal Pressman struct efa_com_create_qp_params *params, 46240909f66SGal Pressman struct efa_ibv_create_qp_resp *resp) 46340909f66SGal Pressman { 464e84d3c18SMichal Kalderon size_t length; 465e84d3c18SMichal Kalderon u64 address; 466e84d3c18SMichal Kalderon 467e84d3c18SMichal Kalderon address = dev->db_bar_addr + resp->sq_db_offset; 468e84d3c18SMichal Kalderon qp->sq_db_mmap_entry = 469e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 470e84d3c18SMichal Kalderon address, 471e84d3c18SMichal Kalderon PAGE_SIZE, EFA_MMAP_IO_NC, 472e84d3c18SMichal Kalderon &resp->sq_db_mmap_key); 473e84d3c18SMichal Kalderon if (!qp->sq_db_mmap_entry) 47440909f66SGal Pressman return -ENOMEM; 47540909f66SGal Pressman 47640909f66SGal Pressman resp->sq_db_offset &= ~PAGE_MASK; 47740909f66SGal Pressman 478e84d3c18SMichal Kalderon address = dev->mem_bar_addr + resp->llq_desc_offset; 479e84d3c18SMichal Kalderon length = PAGE_ALIGN(params->sq_ring_size_in_bytes + 480e84d3c18SMichal Kalderon (resp->llq_desc_offset & ~PAGE_MASK)); 481e84d3c18SMichal Kalderon 482e84d3c18SMichal Kalderon qp->llq_desc_mmap_entry = 483e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 484e84d3c18SMichal Kalderon address, length, 485e84d3c18SMichal Kalderon EFA_MMAP_IO_WC, 486e84d3c18SMichal Kalderon &resp->llq_desc_mmap_key); 487e84d3c18SMichal Kalderon if (!qp->llq_desc_mmap_entry) 488e84d3c18SMichal Kalderon goto err_remove_mmap; 48940909f66SGal Pressman 49040909f66SGal Pressman resp->llq_desc_offset &= ~PAGE_MASK; 49140909f66SGal Pressman 49240909f66SGal Pressman if (qp->rq_size) { 493e84d3c18SMichal Kalderon address = dev->db_bar_addr + resp->rq_db_offset; 494e84d3c18SMichal Kalderon 495e84d3c18SMichal Kalderon qp->rq_db_mmap_entry = 496e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 497e84d3c18SMichal Kalderon address, PAGE_SIZE, 498e84d3c18SMichal Kalderon EFA_MMAP_IO_NC, 499e84d3c18SMichal Kalderon &resp->rq_db_mmap_key); 500e84d3c18SMichal Kalderon if (!qp->rq_db_mmap_entry) 501e84d3c18SMichal Kalderon goto err_remove_mmap; 50240909f66SGal Pressman 50340909f66SGal Pressman resp->rq_db_offset &= ~PAGE_MASK; 50440909f66SGal Pressman 505e84d3c18SMichal Kalderon address = virt_to_phys(qp->rq_cpu_addr); 506e84d3c18SMichal Kalderon qp->rq_mmap_entry = 507e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 508e84d3c18SMichal Kalderon address, qp->rq_size, 509e84d3c18SMichal Kalderon EFA_MMAP_DMA_PAGE, 510e84d3c18SMichal Kalderon &resp->rq_mmap_key); 511e84d3c18SMichal Kalderon if (!qp->rq_mmap_entry) 512e84d3c18SMichal Kalderon goto err_remove_mmap; 51340909f66SGal Pressman 51440909f66SGal Pressman resp->rq_mmap_size = qp->rq_size; 51540909f66SGal Pressman } 51640909f66SGal Pressman 51740909f66SGal Pressman return 0; 518e84d3c18SMichal Kalderon 519e84d3c18SMichal Kalderon err_remove_mmap: 5200428c6efSGal Pressman efa_qp_user_mmap_entries_remove(qp); 521e84d3c18SMichal Kalderon 522e84d3c18SMichal Kalderon return -ENOMEM; 52340909f66SGal Pressman } 52440909f66SGal Pressman 52540909f66SGal Pressman static int efa_qp_validate_cap(struct efa_dev *dev, 52640909f66SGal Pressman struct ib_qp_init_attr *init_attr) 52740909f66SGal Pressman { 52840909f66SGal Pressman if (init_attr->cap.max_send_wr > dev->dev_attr.max_sq_depth) { 52940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 53040909f66SGal Pressman "qp: requested send wr[%u] exceeds the max[%u]\n", 53140909f66SGal Pressman init_attr->cap.max_send_wr, 53240909f66SGal Pressman dev->dev_attr.max_sq_depth); 53340909f66SGal Pressman return -EINVAL; 53440909f66SGal Pressman } 53540909f66SGal Pressman if (init_attr->cap.max_recv_wr > dev->dev_attr.max_rq_depth) { 53640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 53740909f66SGal Pressman "qp: requested receive wr[%u] exceeds the max[%u]\n", 53840909f66SGal Pressman init_attr->cap.max_recv_wr, 53940909f66SGal Pressman dev->dev_attr.max_rq_depth); 54040909f66SGal Pressman return -EINVAL; 54140909f66SGal Pressman } 54240909f66SGal Pressman if (init_attr->cap.max_send_sge > dev->dev_attr.max_sq_sge) { 54340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 54440909f66SGal Pressman "qp: requested sge send[%u] exceeds the max[%u]\n", 54540909f66SGal Pressman init_attr->cap.max_send_sge, dev->dev_attr.max_sq_sge); 54640909f66SGal Pressman return -EINVAL; 54740909f66SGal Pressman } 54840909f66SGal Pressman if (init_attr->cap.max_recv_sge > dev->dev_attr.max_rq_sge) { 54940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 55040909f66SGal Pressman "qp: requested sge recv[%u] exceeds the max[%u]\n", 55140909f66SGal Pressman init_attr->cap.max_recv_sge, dev->dev_attr.max_rq_sge); 55240909f66SGal Pressman return -EINVAL; 55340909f66SGal Pressman } 55440909f66SGal Pressman if (init_attr->cap.max_inline_data > dev->dev_attr.inline_buf_size) { 55540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 55640909f66SGal Pressman "qp: requested inline data[%u] exceeds the max[%u]\n", 55740909f66SGal Pressman init_attr->cap.max_inline_data, 55840909f66SGal Pressman dev->dev_attr.inline_buf_size); 55940909f66SGal Pressman return -EINVAL; 56040909f66SGal Pressman } 56140909f66SGal Pressman 56240909f66SGal Pressman return 0; 56340909f66SGal Pressman } 56440909f66SGal Pressman 56540909f66SGal Pressman static int efa_qp_validate_attr(struct efa_dev *dev, 56640909f66SGal Pressman struct ib_qp_init_attr *init_attr) 56740909f66SGal Pressman { 56840909f66SGal Pressman if (init_attr->qp_type != IB_QPT_DRIVER && 56940909f66SGal Pressman init_attr->qp_type != IB_QPT_UD) { 57040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 57140909f66SGal Pressman "Unsupported qp type %d\n", init_attr->qp_type); 57240909f66SGal Pressman return -EOPNOTSUPP; 57340909f66SGal Pressman } 57440909f66SGal Pressman 57540909f66SGal Pressman if (init_attr->srq) { 57640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "SRQ is not supported\n"); 57740909f66SGal Pressman return -EOPNOTSUPP; 57840909f66SGal Pressman } 57940909f66SGal Pressman 58040909f66SGal Pressman if (init_attr->create_flags) { 58140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Unsupported create flags\n"); 58240909f66SGal Pressman return -EOPNOTSUPP; 58340909f66SGal Pressman } 58440909f66SGal Pressman 58540909f66SGal Pressman return 0; 58640909f66SGal Pressman } 58740909f66SGal Pressman 58840909f66SGal Pressman struct ib_qp *efa_create_qp(struct ib_pd *ibpd, 58940909f66SGal Pressman struct ib_qp_init_attr *init_attr, 59040909f66SGal Pressman struct ib_udata *udata) 59140909f66SGal Pressman { 59240909f66SGal Pressman struct efa_com_create_qp_params create_qp_params = {}; 59340909f66SGal Pressman struct efa_com_create_qp_result create_qp_resp; 59440909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 59540909f66SGal Pressman struct efa_ibv_create_qp_resp resp = {}; 59640909f66SGal Pressman struct efa_ibv_create_qp cmd = {}; 59740909f66SGal Pressman struct efa_ucontext *ucontext; 59840909f66SGal Pressman struct efa_qp *qp; 59940909f66SGal Pressman int err; 60040909f66SGal Pressman 60140909f66SGal Pressman ucontext = rdma_udata_to_drv_context(udata, struct efa_ucontext, 60240909f66SGal Pressman ibucontext); 60340909f66SGal Pressman 60440909f66SGal Pressman err = efa_qp_validate_cap(dev, init_attr); 60540909f66SGal Pressman if (err) 60640909f66SGal Pressman goto err_out; 60740909f66SGal Pressman 60840909f66SGal Pressman err = efa_qp_validate_attr(dev, init_attr); 60940909f66SGal Pressman if (err) 61040909f66SGal Pressman goto err_out; 61140909f66SGal Pressman 61240909f66SGal Pressman if (!field_avail(cmd, driver_qp_type, udata->inlen)) { 61340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 61440909f66SGal Pressman "Incompatible ABI params, no input udata\n"); 61540909f66SGal Pressman err = -EINVAL; 61640909f66SGal Pressman goto err_out; 61740909f66SGal Pressman } 61840909f66SGal Pressman 61940909f66SGal Pressman if (udata->inlen > sizeof(cmd) && 62040909f66SGal Pressman !ib_is_udata_cleared(udata, sizeof(cmd), 62140909f66SGal Pressman udata->inlen - sizeof(cmd))) { 62240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 62340909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 62440909f66SGal Pressman err = -EINVAL; 62540909f66SGal Pressman goto err_out; 62640909f66SGal Pressman } 62740909f66SGal Pressman 62840909f66SGal Pressman err = ib_copy_from_udata(&cmd, udata, 62940909f66SGal Pressman min(sizeof(cmd), udata->inlen)); 63040909f66SGal Pressman if (err) { 63140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 63240909f66SGal Pressman "Cannot copy udata for create_qp\n"); 63340909f66SGal Pressman goto err_out; 63440909f66SGal Pressman } 63540909f66SGal Pressman 63640909f66SGal Pressman if (cmd.comp_mask) { 63740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 63840909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 63940909f66SGal Pressman err = -EINVAL; 64040909f66SGal Pressman goto err_out; 64140909f66SGal Pressman } 64240909f66SGal Pressman 64340909f66SGal Pressman qp = kzalloc(sizeof(*qp), GFP_KERNEL); 64440909f66SGal Pressman if (!qp) { 64540909f66SGal Pressman err = -ENOMEM; 64640909f66SGal Pressman goto err_out; 64740909f66SGal Pressman } 64840909f66SGal Pressman 64940909f66SGal Pressman create_qp_params.uarn = ucontext->uarn; 65040909f66SGal Pressman create_qp_params.pd = to_epd(ibpd)->pdn; 65140909f66SGal Pressman 65240909f66SGal Pressman if (init_attr->qp_type == IB_QPT_UD) { 65340909f66SGal Pressman create_qp_params.qp_type = EFA_ADMIN_QP_TYPE_UD; 65440909f66SGal Pressman } else if (cmd.driver_qp_type == EFA_QP_DRIVER_TYPE_SRD) { 65540909f66SGal Pressman create_qp_params.qp_type = EFA_ADMIN_QP_TYPE_SRD; 65640909f66SGal Pressman } else { 65740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 65840909f66SGal Pressman "Unsupported qp type %d driver qp type %d\n", 65940909f66SGal Pressman init_attr->qp_type, cmd.driver_qp_type); 66040909f66SGal Pressman err = -EOPNOTSUPP; 66140909f66SGal Pressman goto err_free_qp; 66240909f66SGal Pressman } 66340909f66SGal Pressman 66440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Create QP: qp type %d driver qp type %#x\n", 66540909f66SGal Pressman init_attr->qp_type, cmd.driver_qp_type); 66640909f66SGal Pressman create_qp_params.send_cq_idx = to_ecq(init_attr->send_cq)->cq_idx; 66740909f66SGal Pressman create_qp_params.recv_cq_idx = to_ecq(init_attr->recv_cq)->cq_idx; 66840909f66SGal Pressman create_qp_params.sq_depth = init_attr->cap.max_send_wr; 66940909f66SGal Pressman create_qp_params.sq_ring_size_in_bytes = cmd.sq_ring_size; 67040909f66SGal Pressman 67140909f66SGal Pressman create_qp_params.rq_depth = init_attr->cap.max_recv_wr; 67240909f66SGal Pressman create_qp_params.rq_ring_size_in_bytes = cmd.rq_ring_size; 67340909f66SGal Pressman qp->rq_size = PAGE_ALIGN(create_qp_params.rq_ring_size_in_bytes); 67440909f66SGal Pressman if (qp->rq_size) { 67540909f66SGal Pressman qp->rq_cpu_addr = efa_zalloc_mapped(dev, &qp->rq_dma_addr, 67640909f66SGal Pressman qp->rq_size, DMA_TO_DEVICE); 67740909f66SGal Pressman if (!qp->rq_cpu_addr) { 67840909f66SGal Pressman err = -ENOMEM; 67940909f66SGal Pressman goto err_free_qp; 68040909f66SGal Pressman } 68140909f66SGal Pressman 68240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 68340909f66SGal Pressman "qp->cpu_addr[0x%p] allocated: size[%lu], dma[%pad]\n", 68440909f66SGal Pressman qp->rq_cpu_addr, qp->rq_size, &qp->rq_dma_addr); 68540909f66SGal Pressman create_qp_params.rq_base_addr = qp->rq_dma_addr; 68640909f66SGal Pressman } 68740909f66SGal Pressman 68840909f66SGal Pressman err = efa_com_create_qp(&dev->edev, &create_qp_params, 68940909f66SGal Pressman &create_qp_resp); 69040909f66SGal Pressman if (err) 69140909f66SGal Pressman goto err_free_mapped; 69240909f66SGal Pressman 69340909f66SGal Pressman resp.sq_db_offset = create_qp_resp.sq_db_offset; 69440909f66SGal Pressman resp.rq_db_offset = create_qp_resp.rq_db_offset; 69540909f66SGal Pressman resp.llq_desc_offset = create_qp_resp.llq_descriptors_offset; 69640909f66SGal Pressman resp.send_sub_cq_idx = create_qp_resp.send_sub_cq_idx; 69740909f66SGal Pressman resp.recv_sub_cq_idx = create_qp_resp.recv_sub_cq_idx; 69840909f66SGal Pressman 69940909f66SGal Pressman err = qp_mmap_entries_setup(qp, dev, ucontext, &create_qp_params, 70040909f66SGal Pressman &resp); 70140909f66SGal Pressman if (err) 70240909f66SGal Pressman goto err_destroy_qp; 70340909f66SGal Pressman 70440909f66SGal Pressman qp->qp_handle = create_qp_resp.qp_handle; 70540909f66SGal Pressman qp->ibqp.qp_num = create_qp_resp.qp_num; 70640909f66SGal Pressman qp->ibqp.qp_type = init_attr->qp_type; 70740909f66SGal Pressman qp->max_send_wr = init_attr->cap.max_send_wr; 70840909f66SGal Pressman qp->max_recv_wr = init_attr->cap.max_recv_wr; 70940909f66SGal Pressman qp->max_send_sge = init_attr->cap.max_send_sge; 71040909f66SGal Pressman qp->max_recv_sge = init_attr->cap.max_recv_sge; 71140909f66SGal Pressman qp->max_inline_data = init_attr->cap.max_inline_data; 71240909f66SGal Pressman 71340909f66SGal Pressman if (udata->outlen) { 71440909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 71540909f66SGal Pressman min(sizeof(resp), udata->outlen)); 71640909f66SGal Pressman if (err) { 71740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 71840909f66SGal Pressman "Failed to copy udata for qp[%u]\n", 71940909f66SGal Pressman create_qp_resp.qp_num); 720e84d3c18SMichal Kalderon goto err_remove_mmap_entries; 72140909f66SGal Pressman } 72240909f66SGal Pressman } 72340909f66SGal Pressman 72440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Created qp[%d]\n", qp->ibqp.qp_num); 72540909f66SGal Pressman 72640909f66SGal Pressman return &qp->ibqp; 72740909f66SGal Pressman 728e84d3c18SMichal Kalderon err_remove_mmap_entries: 7290428c6efSGal Pressman efa_qp_user_mmap_entries_remove(qp); 73040909f66SGal Pressman err_destroy_qp: 73140909f66SGal Pressman efa_destroy_qp_handle(dev, create_qp_resp.qp_handle); 73240909f66SGal Pressman err_free_mapped: 733*ff6629f8SGal Pressman if (qp->rq_size) 734*ff6629f8SGal Pressman efa_free_mapped(dev, qp->rq_cpu_addr, qp->rq_dma_addr, 735*ff6629f8SGal Pressman qp->rq_size, DMA_TO_DEVICE); 73640909f66SGal Pressman err_free_qp: 73740909f66SGal Pressman kfree(qp); 73840909f66SGal Pressman err_out: 73940909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.create_qp_err); 74040909f66SGal Pressman return ERR_PTR(err); 74140909f66SGal Pressman } 74240909f66SGal Pressman 74340909f66SGal Pressman static int efa_modify_qp_validate(struct efa_dev *dev, struct efa_qp *qp, 74440909f66SGal Pressman struct ib_qp_attr *qp_attr, int qp_attr_mask, 74540909f66SGal Pressman enum ib_qp_state cur_state, 74640909f66SGal Pressman enum ib_qp_state new_state) 74740909f66SGal Pressman { 74840909f66SGal Pressman #define EFA_MODIFY_QP_SUPP_MASK \ 74940909f66SGal Pressman (IB_QP_STATE | IB_QP_CUR_STATE | IB_QP_EN_SQD_ASYNC_NOTIFY | \ 75040909f66SGal Pressman IB_QP_PKEY_INDEX | IB_QP_PORT | IB_QP_QKEY | IB_QP_SQ_PSN) 75140909f66SGal Pressman 75240909f66SGal Pressman if (qp_attr_mask & ~EFA_MODIFY_QP_SUPP_MASK) { 75340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 75440909f66SGal Pressman "Unsupported qp_attr_mask[%#x] supported[%#x]\n", 75540909f66SGal Pressman qp_attr_mask, EFA_MODIFY_QP_SUPP_MASK); 75640909f66SGal Pressman return -EOPNOTSUPP; 75740909f66SGal Pressman } 75840909f66SGal Pressman 75940909f66SGal Pressman if (!ib_modify_qp_is_ok(cur_state, new_state, IB_QPT_UD, 76040909f66SGal Pressman qp_attr_mask)) { 76140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Invalid modify QP parameters\n"); 76240909f66SGal Pressman return -EINVAL; 76340909f66SGal Pressman } 76440909f66SGal Pressman 76540909f66SGal Pressman if ((qp_attr_mask & IB_QP_PORT) && qp_attr->port_num != 1) { 76640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Can't change port num\n"); 76740909f66SGal Pressman return -EOPNOTSUPP; 76840909f66SGal Pressman } 76940909f66SGal Pressman 77040909f66SGal Pressman if ((qp_attr_mask & IB_QP_PKEY_INDEX) && qp_attr->pkey_index) { 77140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Can't change pkey index\n"); 77240909f66SGal Pressman return -EOPNOTSUPP; 77340909f66SGal Pressman } 77440909f66SGal Pressman 77540909f66SGal Pressman return 0; 77640909f66SGal Pressman } 77740909f66SGal Pressman 77840909f66SGal Pressman int efa_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, 77940909f66SGal Pressman int qp_attr_mask, struct ib_udata *udata) 78040909f66SGal Pressman { 78140909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->device); 78240909f66SGal Pressman struct efa_com_modify_qp_params params = {}; 78340909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 78440909f66SGal Pressman enum ib_qp_state cur_state; 78540909f66SGal Pressman enum ib_qp_state new_state; 78640909f66SGal Pressman int err; 78740909f66SGal Pressman 78840909f66SGal Pressman if (udata->inlen && 78940909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 79040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 79140909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 79240909f66SGal Pressman return -EINVAL; 79340909f66SGal Pressman } 79440909f66SGal Pressman 79540909f66SGal Pressman cur_state = qp_attr_mask & IB_QP_CUR_STATE ? qp_attr->cur_qp_state : 79640909f66SGal Pressman qp->state; 79740909f66SGal Pressman new_state = qp_attr_mask & IB_QP_STATE ? qp_attr->qp_state : cur_state; 79840909f66SGal Pressman 79940909f66SGal Pressman err = efa_modify_qp_validate(dev, qp, qp_attr, qp_attr_mask, cur_state, 80040909f66SGal Pressman new_state); 80140909f66SGal Pressman if (err) 80240909f66SGal Pressman return err; 80340909f66SGal Pressman 80440909f66SGal Pressman params.qp_handle = qp->qp_handle; 80540909f66SGal Pressman 80640909f66SGal Pressman if (qp_attr_mask & IB_QP_STATE) { 80740909f66SGal Pressman params.modify_mask |= BIT(EFA_ADMIN_QP_STATE_BIT) | 80840909f66SGal Pressman BIT(EFA_ADMIN_CUR_QP_STATE_BIT); 80940909f66SGal Pressman params.cur_qp_state = qp_attr->cur_qp_state; 81040909f66SGal Pressman params.qp_state = qp_attr->qp_state; 81140909f66SGal Pressman } 81240909f66SGal Pressman 81340909f66SGal Pressman if (qp_attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) { 81440909f66SGal Pressman params.modify_mask |= 81540909f66SGal Pressman BIT(EFA_ADMIN_SQ_DRAINED_ASYNC_NOTIFY_BIT); 81640909f66SGal Pressman params.sq_drained_async_notify = qp_attr->en_sqd_async_notify; 81740909f66SGal Pressman } 81840909f66SGal Pressman 81940909f66SGal Pressman if (qp_attr_mask & IB_QP_QKEY) { 82040909f66SGal Pressman params.modify_mask |= BIT(EFA_ADMIN_QKEY_BIT); 82140909f66SGal Pressman params.qkey = qp_attr->qkey; 82240909f66SGal Pressman } 82340909f66SGal Pressman 82440909f66SGal Pressman if (qp_attr_mask & IB_QP_SQ_PSN) { 82540909f66SGal Pressman params.modify_mask |= BIT(EFA_ADMIN_SQ_PSN_BIT); 82640909f66SGal Pressman params.sq_psn = qp_attr->sq_psn; 82740909f66SGal Pressman } 82840909f66SGal Pressman 82940909f66SGal Pressman err = efa_com_modify_qp(&dev->edev, ¶ms); 83040909f66SGal Pressman if (err) 83140909f66SGal Pressman return err; 83240909f66SGal Pressman 83340909f66SGal Pressman qp->state = new_state; 83440909f66SGal Pressman 83540909f66SGal Pressman return 0; 83640909f66SGal Pressman } 83740909f66SGal Pressman 83840909f66SGal Pressman static int efa_destroy_cq_idx(struct efa_dev *dev, int cq_idx) 83940909f66SGal Pressman { 84040909f66SGal Pressman struct efa_com_destroy_cq_params params = { .cq_idx = cq_idx }; 84140909f66SGal Pressman 84240909f66SGal Pressman return efa_com_destroy_cq(&dev->edev, ¶ms); 84340909f66SGal Pressman } 84440909f66SGal Pressman 845a52c8e24SLeon Romanovsky void efa_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 84640909f66SGal Pressman { 84740909f66SGal Pressman struct efa_dev *dev = to_edev(ibcq->device); 84840909f66SGal Pressman struct efa_cq *cq = to_ecq(ibcq); 84940909f66SGal Pressman 85040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 85140909f66SGal Pressman "Destroy cq[%d] virt[0x%p] freed: size[%lu], dma[%pad]\n", 85240909f66SGal Pressman cq->cq_idx, cq->cpu_addr, cq->size, &cq->dma_addr); 85340909f66SGal Pressman 854e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(cq->mmap_entry); 855*ff6629f8SGal Pressman efa_destroy_cq_idx(dev, cq->cq_idx); 856*ff6629f8SGal Pressman efa_free_mapped(dev, cq->cpu_addr, cq->dma_addr, cq->size, 857*ff6629f8SGal Pressman DMA_FROM_DEVICE); 85840909f66SGal Pressman } 85940909f66SGal Pressman 86040909f66SGal Pressman static int cq_mmap_entries_setup(struct efa_dev *dev, struct efa_cq *cq, 86140909f66SGal Pressman struct efa_ibv_create_cq_resp *resp) 86240909f66SGal Pressman { 86340909f66SGal Pressman resp->q_mmap_size = cq->size; 864e84d3c18SMichal Kalderon cq->mmap_entry = efa_user_mmap_entry_insert(&cq->ucontext->ibucontext, 86540909f66SGal Pressman virt_to_phys(cq->cpu_addr), 866e84d3c18SMichal Kalderon cq->size, EFA_MMAP_DMA_PAGE, 867e84d3c18SMichal Kalderon &resp->q_mmap_key); 868e84d3c18SMichal Kalderon if (!cq->mmap_entry) 86940909f66SGal Pressman return -ENOMEM; 87040909f66SGal Pressman 87140909f66SGal Pressman return 0; 87240909f66SGal Pressman } 87340909f66SGal Pressman 874e39afe3dSLeon Romanovsky int efa_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 87540909f66SGal Pressman struct ib_udata *udata) 87640909f66SGal Pressman { 877e39afe3dSLeon Romanovsky struct efa_ucontext *ucontext = rdma_udata_to_drv_context( 878e39afe3dSLeon Romanovsky udata, struct efa_ucontext, ibucontext); 87940909f66SGal Pressman struct efa_ibv_create_cq_resp resp = {}; 88040909f66SGal Pressman struct efa_com_create_cq_params params; 88140909f66SGal Pressman struct efa_com_create_cq_result result; 882e39afe3dSLeon Romanovsky struct ib_device *ibdev = ibcq->device; 88340909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 88440909f66SGal Pressman struct efa_ibv_create_cq cmd = {}; 885e39afe3dSLeon Romanovsky struct efa_cq *cq = to_ecq(ibcq); 886e39afe3dSLeon Romanovsky int entries = attr->cqe; 88740909f66SGal Pressman int err; 88840909f66SGal Pressman 88940909f66SGal Pressman ibdev_dbg(ibdev, "create_cq entries %d\n", entries); 89040909f66SGal Pressman 89140909f66SGal Pressman if (entries < 1 || entries > dev->dev_attr.max_cq_depth) { 89240909f66SGal Pressman ibdev_dbg(ibdev, 89340909f66SGal Pressman "cq: requested entries[%u] non-positive or greater than max[%u]\n", 89440909f66SGal Pressman entries, dev->dev_attr.max_cq_depth); 89540909f66SGal Pressman err = -EINVAL; 89640909f66SGal Pressman goto err_out; 89740909f66SGal Pressman } 89840909f66SGal Pressman 89940909f66SGal Pressman if (!field_avail(cmd, num_sub_cqs, udata->inlen)) { 90040909f66SGal Pressman ibdev_dbg(ibdev, 90140909f66SGal Pressman "Incompatible ABI params, no input udata\n"); 90240909f66SGal Pressman err = -EINVAL; 90340909f66SGal Pressman goto err_out; 90440909f66SGal Pressman } 90540909f66SGal Pressman 90640909f66SGal Pressman if (udata->inlen > sizeof(cmd) && 90740909f66SGal Pressman !ib_is_udata_cleared(udata, sizeof(cmd), 90840909f66SGal Pressman udata->inlen - sizeof(cmd))) { 90940909f66SGal Pressman ibdev_dbg(ibdev, 91040909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 91140909f66SGal Pressman err = -EINVAL; 91240909f66SGal Pressman goto err_out; 91340909f66SGal Pressman } 91440909f66SGal Pressman 91540909f66SGal Pressman err = ib_copy_from_udata(&cmd, udata, 91640909f66SGal Pressman min(sizeof(cmd), udata->inlen)); 91740909f66SGal Pressman if (err) { 91840909f66SGal Pressman ibdev_dbg(ibdev, "Cannot copy udata for create_cq\n"); 91940909f66SGal Pressman goto err_out; 92040909f66SGal Pressman } 92140909f66SGal Pressman 92240909f66SGal Pressman if (cmd.comp_mask || !is_reserved_cleared(cmd.reserved_50)) { 92340909f66SGal Pressman ibdev_dbg(ibdev, 92440909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 92540909f66SGal Pressman err = -EINVAL; 92640909f66SGal Pressman goto err_out; 92740909f66SGal Pressman } 92840909f66SGal Pressman 92940909f66SGal Pressman if (!cmd.cq_entry_size) { 93040909f66SGal Pressman ibdev_dbg(ibdev, 93140909f66SGal Pressman "Invalid entry size [%u]\n", cmd.cq_entry_size); 93240909f66SGal Pressman err = -EINVAL; 93340909f66SGal Pressman goto err_out; 93440909f66SGal Pressman } 93540909f66SGal Pressman 93640909f66SGal Pressman if (cmd.num_sub_cqs != dev->dev_attr.sub_cqs_per_cq) { 93740909f66SGal Pressman ibdev_dbg(ibdev, 93840909f66SGal Pressman "Invalid number of sub cqs[%u] expected[%u]\n", 93940909f66SGal Pressman cmd.num_sub_cqs, dev->dev_attr.sub_cqs_per_cq); 94040909f66SGal Pressman err = -EINVAL; 94140909f66SGal Pressman goto err_out; 94240909f66SGal Pressman } 94340909f66SGal Pressman 944e39afe3dSLeon Romanovsky cq->ucontext = ucontext; 94540909f66SGal Pressman cq->size = PAGE_ALIGN(cmd.cq_entry_size * entries * cmd.num_sub_cqs); 94640909f66SGal Pressman cq->cpu_addr = efa_zalloc_mapped(dev, &cq->dma_addr, cq->size, 94740909f66SGal Pressman DMA_FROM_DEVICE); 94840909f66SGal Pressman if (!cq->cpu_addr) { 94940909f66SGal Pressman err = -ENOMEM; 950e39afe3dSLeon Romanovsky goto err_out; 95140909f66SGal Pressman } 95240909f66SGal Pressman 95340909f66SGal Pressman params.uarn = cq->ucontext->uarn; 95440909f66SGal Pressman params.cq_depth = entries; 95540909f66SGal Pressman params.dma_addr = cq->dma_addr; 95640909f66SGal Pressman params.entry_size_in_bytes = cmd.cq_entry_size; 95740909f66SGal Pressman params.num_sub_cqs = cmd.num_sub_cqs; 95840909f66SGal Pressman err = efa_com_create_cq(&dev->edev, ¶ms, &result); 95940909f66SGal Pressman if (err) 96040909f66SGal Pressman goto err_free_mapped; 96140909f66SGal Pressman 96240909f66SGal Pressman resp.cq_idx = result.cq_idx; 96340909f66SGal Pressman cq->cq_idx = result.cq_idx; 96440909f66SGal Pressman cq->ibcq.cqe = result.actual_depth; 96540909f66SGal Pressman WARN_ON_ONCE(entries != result.actual_depth); 96640909f66SGal Pressman 96740909f66SGal Pressman err = cq_mmap_entries_setup(dev, cq, &resp); 96840909f66SGal Pressman if (err) { 969e39afe3dSLeon Romanovsky ibdev_dbg(ibdev, "Could not setup cq[%u] mmap entries\n", 970e39afe3dSLeon Romanovsky cq->cq_idx); 97140909f66SGal Pressman goto err_destroy_cq; 97240909f66SGal Pressman } 97340909f66SGal Pressman 97440909f66SGal Pressman if (udata->outlen) { 97540909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 97640909f66SGal Pressman min(sizeof(resp), udata->outlen)); 97740909f66SGal Pressman if (err) { 97840909f66SGal Pressman ibdev_dbg(ibdev, 97940909f66SGal Pressman "Failed to copy udata for create_cq\n"); 980e84d3c18SMichal Kalderon goto err_remove_mmap; 98140909f66SGal Pressman } 98240909f66SGal Pressman } 98340909f66SGal Pressman 984e39afe3dSLeon Romanovsky ibdev_dbg(ibdev, "Created cq[%d], cq depth[%u]. dma[%pad] virt[0x%p]\n", 98540909f66SGal Pressman cq->cq_idx, result.actual_depth, &cq->dma_addr, cq->cpu_addr); 98640909f66SGal Pressman 987e39afe3dSLeon Romanovsky return 0; 98840909f66SGal Pressman 989e84d3c18SMichal Kalderon err_remove_mmap: 990e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(cq->mmap_entry); 99140909f66SGal Pressman err_destroy_cq: 99240909f66SGal Pressman efa_destroy_cq_idx(dev, cq->cq_idx); 99340909f66SGal Pressman err_free_mapped: 994*ff6629f8SGal Pressman efa_free_mapped(dev, cq->cpu_addr, cq->dma_addr, cq->size, 99540909f66SGal Pressman DMA_FROM_DEVICE); 996e84d3c18SMichal Kalderon 99740909f66SGal Pressman err_out: 99840909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.create_cq_err); 999e39afe3dSLeon Romanovsky return err; 100040909f66SGal Pressman } 100140909f66SGal Pressman 100240909f66SGal Pressman static int umem_to_page_list(struct efa_dev *dev, 100340909f66SGal Pressman struct ib_umem *umem, 100440909f66SGal Pressman u64 *page_list, 100540909f66SGal Pressman u32 hp_cnt, 100640909f66SGal Pressman u8 hp_shift) 100740909f66SGal Pressman { 100840909f66SGal Pressman u32 pages_in_hp = BIT(hp_shift - PAGE_SHIFT); 100940ddb3f0SGal Pressman struct ib_block_iter biter; 101040909f66SGal Pressman unsigned int hp_idx = 0; 101140909f66SGal Pressman 101240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "hp_cnt[%u], pages_in_hp[%u]\n", 101340909f66SGal Pressman hp_cnt, pages_in_hp); 101440909f66SGal Pressman 101540ddb3f0SGal Pressman rdma_for_each_block(umem->sg_head.sgl, &biter, umem->nmap, 101640ddb3f0SGal Pressman BIT(hp_shift)) 101740ddb3f0SGal Pressman page_list[hp_idx++] = rdma_block_iter_dma_address(&biter); 101840909f66SGal Pressman 101940909f66SGal Pressman return 0; 102040909f66SGal Pressman } 102140909f66SGal Pressman 102240909f66SGal Pressman static struct scatterlist *efa_vmalloc_buf_to_sg(u64 *buf, int page_cnt) 102340909f66SGal Pressman { 102440909f66SGal Pressman struct scatterlist *sglist; 102540909f66SGal Pressman struct page *pg; 102640909f66SGal Pressman int i; 102740909f66SGal Pressman 102840909f66SGal Pressman sglist = kcalloc(page_cnt, sizeof(*sglist), GFP_KERNEL); 102940909f66SGal Pressman if (!sglist) 103040909f66SGal Pressman return NULL; 103140909f66SGal Pressman sg_init_table(sglist, page_cnt); 103240909f66SGal Pressman for (i = 0; i < page_cnt; i++) { 103340909f66SGal Pressman pg = vmalloc_to_page(buf); 103440909f66SGal Pressman if (!pg) 103540909f66SGal Pressman goto err; 103640909f66SGal Pressman sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); 103740909f66SGal Pressman buf += PAGE_SIZE / sizeof(*buf); 103840909f66SGal Pressman } 103940909f66SGal Pressman return sglist; 104040909f66SGal Pressman 104140909f66SGal Pressman err: 104240909f66SGal Pressman kfree(sglist); 104340909f66SGal Pressman return NULL; 104440909f66SGal Pressman } 104540909f66SGal Pressman 104640909f66SGal Pressman /* 104740909f66SGal Pressman * create a chunk list of physical pages dma addresses from the supplied 104840909f66SGal Pressman * scatter gather list 104940909f66SGal Pressman */ 105040909f66SGal Pressman static int pbl_chunk_list_create(struct efa_dev *dev, struct pbl_context *pbl) 105140909f66SGal Pressman { 105240909f66SGal Pressman struct pbl_chunk_list *chunk_list = &pbl->phys.indirect.chunk_list; 105340909f66SGal Pressman int page_cnt = pbl->phys.indirect.pbl_buf_size_in_pages; 105440909f66SGal Pressman struct scatterlist *pages_sgl = pbl->phys.indirect.sgl; 10554d50e084SGal Pressman unsigned int chunk_list_size, chunk_idx, payload_idx; 105640909f66SGal Pressman int sg_dma_cnt = pbl->phys.indirect.sg_dma_cnt; 105740909f66SGal Pressman struct efa_com_ctrl_buff_info *ctrl_buf; 105840909f66SGal Pressman u64 *cur_chunk_buf, *prev_chunk_buf; 10594d50e084SGal Pressman struct ib_block_iter biter; 106040909f66SGal Pressman dma_addr_t dma_addr; 106140909f66SGal Pressman int i; 106240909f66SGal Pressman 106340909f66SGal Pressman /* allocate a chunk list that consists of 4KB chunks */ 106440909f66SGal Pressman chunk_list_size = DIV_ROUND_UP(page_cnt, EFA_PTRS_PER_CHUNK); 106540909f66SGal Pressman 106640909f66SGal Pressman chunk_list->size = chunk_list_size; 106740909f66SGal Pressman chunk_list->chunks = kcalloc(chunk_list_size, 106840909f66SGal Pressman sizeof(*chunk_list->chunks), 106940909f66SGal Pressman GFP_KERNEL); 107040909f66SGal Pressman if (!chunk_list->chunks) 107140909f66SGal Pressman return -ENOMEM; 107240909f66SGal Pressman 107340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 107440909f66SGal Pressman "chunk_list_size[%u] - pages[%u]\n", chunk_list_size, 107540909f66SGal Pressman page_cnt); 107640909f66SGal Pressman 107740909f66SGal Pressman /* allocate chunk buffers: */ 107840909f66SGal Pressman for (i = 0; i < chunk_list_size; i++) { 107940909f66SGal Pressman chunk_list->chunks[i].buf = kzalloc(EFA_CHUNK_SIZE, GFP_KERNEL); 108040909f66SGal Pressman if (!chunk_list->chunks[i].buf) 108140909f66SGal Pressman goto chunk_list_dealloc; 108240909f66SGal Pressman 108340909f66SGal Pressman chunk_list->chunks[i].length = EFA_CHUNK_USED_SIZE; 108440909f66SGal Pressman } 108540909f66SGal Pressman chunk_list->chunks[chunk_list_size - 1].length = 108640909f66SGal Pressman ((page_cnt % EFA_PTRS_PER_CHUNK) * EFA_CHUNK_PAYLOAD_PTR_SIZE) + 108740909f66SGal Pressman EFA_CHUNK_PTR_SIZE; 108840909f66SGal Pressman 108940909f66SGal Pressman /* fill the dma addresses of sg list pages to chunks: */ 109040909f66SGal Pressman chunk_idx = 0; 109140909f66SGal Pressman payload_idx = 0; 109240909f66SGal Pressman cur_chunk_buf = chunk_list->chunks[0].buf; 10934d50e084SGal Pressman rdma_for_each_block(pages_sgl, &biter, sg_dma_cnt, 10944d50e084SGal Pressman EFA_CHUNK_PAYLOAD_SIZE) { 109540909f66SGal Pressman cur_chunk_buf[payload_idx++] = 10964d50e084SGal Pressman rdma_block_iter_dma_address(&biter); 109740909f66SGal Pressman 109840909f66SGal Pressman if (payload_idx == EFA_PTRS_PER_CHUNK) { 109940909f66SGal Pressman chunk_idx++; 110040909f66SGal Pressman cur_chunk_buf = chunk_list->chunks[chunk_idx].buf; 110140909f66SGal Pressman payload_idx = 0; 110240909f66SGal Pressman } 110340909f66SGal Pressman } 110440909f66SGal Pressman 110540909f66SGal Pressman /* map chunks to dma and fill chunks next ptrs */ 110640909f66SGal Pressman for (i = chunk_list_size - 1; i >= 0; i--) { 110740909f66SGal Pressman dma_addr = dma_map_single(&dev->pdev->dev, 110840909f66SGal Pressman chunk_list->chunks[i].buf, 110940909f66SGal Pressman chunk_list->chunks[i].length, 111040909f66SGal Pressman DMA_TO_DEVICE); 111140909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, dma_addr)) { 111240909f66SGal Pressman ibdev_err(&dev->ibdev, 111340909f66SGal Pressman "chunk[%u] dma_map_failed\n", i); 111440909f66SGal Pressman goto chunk_list_unmap; 111540909f66SGal Pressman } 111640909f66SGal Pressman 111740909f66SGal Pressman chunk_list->chunks[i].dma_addr = dma_addr; 111840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 111940909f66SGal Pressman "chunk[%u] mapped at [%pad]\n", i, &dma_addr); 112040909f66SGal Pressman 112140909f66SGal Pressman if (!i) 112240909f66SGal Pressman break; 112340909f66SGal Pressman 112440909f66SGal Pressman prev_chunk_buf = chunk_list->chunks[i - 1].buf; 112540909f66SGal Pressman 112640909f66SGal Pressman ctrl_buf = (struct efa_com_ctrl_buff_info *) 112740909f66SGal Pressman &prev_chunk_buf[EFA_PTRS_PER_CHUNK]; 112840909f66SGal Pressman ctrl_buf->length = chunk_list->chunks[i].length; 112940909f66SGal Pressman 113040909f66SGal Pressman efa_com_set_dma_addr(dma_addr, 113140909f66SGal Pressman &ctrl_buf->address.mem_addr_high, 113240909f66SGal Pressman &ctrl_buf->address.mem_addr_low); 113340909f66SGal Pressman } 113440909f66SGal Pressman 113540909f66SGal Pressman return 0; 113640909f66SGal Pressman 113740909f66SGal Pressman chunk_list_unmap: 113840909f66SGal Pressman for (; i < chunk_list_size; i++) { 113940909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, chunk_list->chunks[i].dma_addr, 114040909f66SGal Pressman chunk_list->chunks[i].length, DMA_TO_DEVICE); 114140909f66SGal Pressman } 114240909f66SGal Pressman chunk_list_dealloc: 114340909f66SGal Pressman for (i = 0; i < chunk_list_size; i++) 114440909f66SGal Pressman kfree(chunk_list->chunks[i].buf); 114540909f66SGal Pressman 114640909f66SGal Pressman kfree(chunk_list->chunks); 114740909f66SGal Pressman return -ENOMEM; 114840909f66SGal Pressman } 114940909f66SGal Pressman 115040909f66SGal Pressman static void pbl_chunk_list_destroy(struct efa_dev *dev, struct pbl_context *pbl) 115140909f66SGal Pressman { 115240909f66SGal Pressman struct pbl_chunk_list *chunk_list = &pbl->phys.indirect.chunk_list; 115340909f66SGal Pressman int i; 115440909f66SGal Pressman 115540909f66SGal Pressman for (i = 0; i < chunk_list->size; i++) { 115640909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, chunk_list->chunks[i].dma_addr, 115740909f66SGal Pressman chunk_list->chunks[i].length, DMA_TO_DEVICE); 115840909f66SGal Pressman kfree(chunk_list->chunks[i].buf); 115940909f66SGal Pressman } 116040909f66SGal Pressman 116140909f66SGal Pressman kfree(chunk_list->chunks); 116240909f66SGal Pressman } 116340909f66SGal Pressman 116440909f66SGal Pressman /* initialize pbl continuous mode: map pbl buffer to a dma address. */ 116540909f66SGal Pressman static int pbl_continuous_initialize(struct efa_dev *dev, 116640909f66SGal Pressman struct pbl_context *pbl) 116740909f66SGal Pressman { 116840909f66SGal Pressman dma_addr_t dma_addr; 116940909f66SGal Pressman 117040909f66SGal Pressman dma_addr = dma_map_single(&dev->pdev->dev, pbl->pbl_buf, 117140909f66SGal Pressman pbl->pbl_buf_size_in_bytes, DMA_TO_DEVICE); 117240909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, dma_addr)) { 117340909f66SGal Pressman ibdev_err(&dev->ibdev, "Unable to map pbl to DMA address\n"); 117440909f66SGal Pressman return -ENOMEM; 117540909f66SGal Pressman } 117640909f66SGal Pressman 117740909f66SGal Pressman pbl->phys.continuous.dma_addr = dma_addr; 117840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 117940909f66SGal Pressman "pbl continuous - dma_addr = %pad, size[%u]\n", 118040909f66SGal Pressman &dma_addr, pbl->pbl_buf_size_in_bytes); 118140909f66SGal Pressman 118240909f66SGal Pressman return 0; 118340909f66SGal Pressman } 118440909f66SGal Pressman 118540909f66SGal Pressman /* 118640909f66SGal Pressman * initialize pbl indirect mode: 118740909f66SGal Pressman * create a chunk list out of the dma addresses of the physical pages of 118840909f66SGal Pressman * pbl buffer. 118940909f66SGal Pressman */ 119040909f66SGal Pressman static int pbl_indirect_initialize(struct efa_dev *dev, struct pbl_context *pbl) 119140909f66SGal Pressman { 119240909f66SGal Pressman u32 size_in_pages = DIV_ROUND_UP(pbl->pbl_buf_size_in_bytes, PAGE_SIZE); 119340909f66SGal Pressman struct scatterlist *sgl; 119440909f66SGal Pressman int sg_dma_cnt, err; 119540909f66SGal Pressman 119640909f66SGal Pressman BUILD_BUG_ON(EFA_CHUNK_PAYLOAD_SIZE > PAGE_SIZE); 119740909f66SGal Pressman sgl = efa_vmalloc_buf_to_sg(pbl->pbl_buf, size_in_pages); 119840909f66SGal Pressman if (!sgl) 119940909f66SGal Pressman return -ENOMEM; 120040909f66SGal Pressman 120140909f66SGal Pressman sg_dma_cnt = dma_map_sg(&dev->pdev->dev, sgl, size_in_pages, DMA_TO_DEVICE); 120240909f66SGal Pressman if (!sg_dma_cnt) { 120340909f66SGal Pressman err = -EINVAL; 120440909f66SGal Pressman goto err_map; 120540909f66SGal Pressman } 120640909f66SGal Pressman 120740909f66SGal Pressman pbl->phys.indirect.pbl_buf_size_in_pages = size_in_pages; 120840909f66SGal Pressman pbl->phys.indirect.sgl = sgl; 120940909f66SGal Pressman pbl->phys.indirect.sg_dma_cnt = sg_dma_cnt; 121040909f66SGal Pressman err = pbl_chunk_list_create(dev, pbl); 121140909f66SGal Pressman if (err) { 121240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 121340909f66SGal Pressman "chunk_list creation failed[%d]\n", err); 121440909f66SGal Pressman goto err_chunk; 121540909f66SGal Pressman } 121640909f66SGal Pressman 121740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 121840909f66SGal Pressman "pbl indirect - size[%u], chunks[%u]\n", 121940909f66SGal Pressman pbl->pbl_buf_size_in_bytes, 122040909f66SGal Pressman pbl->phys.indirect.chunk_list.size); 122140909f66SGal Pressman 122240909f66SGal Pressman return 0; 122340909f66SGal Pressman 122440909f66SGal Pressman err_chunk: 122540909f66SGal Pressman dma_unmap_sg(&dev->pdev->dev, sgl, size_in_pages, DMA_TO_DEVICE); 122640909f66SGal Pressman err_map: 122740909f66SGal Pressman kfree(sgl); 122840909f66SGal Pressman return err; 122940909f66SGal Pressman } 123040909f66SGal Pressman 123140909f66SGal Pressman static void pbl_indirect_terminate(struct efa_dev *dev, struct pbl_context *pbl) 123240909f66SGal Pressman { 123340909f66SGal Pressman pbl_chunk_list_destroy(dev, pbl); 123440909f66SGal Pressman dma_unmap_sg(&dev->pdev->dev, pbl->phys.indirect.sgl, 123540909f66SGal Pressman pbl->phys.indirect.pbl_buf_size_in_pages, DMA_TO_DEVICE); 123640909f66SGal Pressman kfree(pbl->phys.indirect.sgl); 123740909f66SGal Pressman } 123840909f66SGal Pressman 123940909f66SGal Pressman /* create a page buffer list from a mapped user memory region */ 124040909f66SGal Pressman static int pbl_create(struct efa_dev *dev, 124140909f66SGal Pressman struct pbl_context *pbl, 124240909f66SGal Pressman struct ib_umem *umem, 124340909f66SGal Pressman int hp_cnt, 124440909f66SGal Pressman u8 hp_shift) 124540909f66SGal Pressman { 124640909f66SGal Pressman int err; 124740909f66SGal Pressman 124840909f66SGal Pressman pbl->pbl_buf_size_in_bytes = hp_cnt * EFA_CHUNK_PAYLOAD_PTR_SIZE; 1249255efcaeSGal Pressman pbl->pbl_buf = kvzalloc(pbl->pbl_buf_size_in_bytes, GFP_KERNEL); 1250255efcaeSGal Pressman if (!pbl->pbl_buf) 1251255efcaeSGal Pressman return -ENOMEM; 1252255efcaeSGal Pressman 1253255efcaeSGal Pressman if (is_vmalloc_addr(pbl->pbl_buf)) { 1254255efcaeSGal Pressman pbl->physically_continuous = 0; 1255255efcaeSGal Pressman err = umem_to_page_list(dev, umem, pbl->pbl_buf, hp_cnt, 1256255efcaeSGal Pressman hp_shift); 1257255efcaeSGal Pressman if (err) 1258255efcaeSGal Pressman goto err_free; 1259255efcaeSGal Pressman 1260255efcaeSGal Pressman err = pbl_indirect_initialize(dev, pbl); 1261255efcaeSGal Pressman if (err) 1262255efcaeSGal Pressman goto err_free; 1263255efcaeSGal Pressman } else { 126440909f66SGal Pressman pbl->physically_continuous = 1; 126540909f66SGal Pressman err = umem_to_page_list(dev, umem, pbl->pbl_buf, hp_cnt, 126640909f66SGal Pressman hp_shift); 126740909f66SGal Pressman if (err) 1268255efcaeSGal Pressman goto err_free; 1269255efcaeSGal Pressman 127040909f66SGal Pressman err = pbl_continuous_initialize(dev, pbl); 127140909f66SGal Pressman if (err) 1272255efcaeSGal Pressman goto err_free; 127340909f66SGal Pressman } 127440909f66SGal Pressman 127540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 127640909f66SGal Pressman "user_pbl_created: user_pages[%u], continuous[%u]\n", 127740909f66SGal Pressman hp_cnt, pbl->physically_continuous); 127840909f66SGal Pressman 127940909f66SGal Pressman return 0; 128040909f66SGal Pressman 1281255efcaeSGal Pressman err_free: 1282255efcaeSGal Pressman kvfree(pbl->pbl_buf); 128340909f66SGal Pressman return err; 128440909f66SGal Pressman } 128540909f66SGal Pressman 128640909f66SGal Pressman static void pbl_destroy(struct efa_dev *dev, struct pbl_context *pbl) 128740909f66SGal Pressman { 1288255efcaeSGal Pressman if (pbl->physically_continuous) 128940909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, pbl->phys.continuous.dma_addr, 129040909f66SGal Pressman pbl->pbl_buf_size_in_bytes, DMA_TO_DEVICE); 1291255efcaeSGal Pressman else 129240909f66SGal Pressman pbl_indirect_terminate(dev, pbl); 1293255efcaeSGal Pressman 1294255efcaeSGal Pressman kvfree(pbl->pbl_buf); 129540909f66SGal Pressman } 129640909f66SGal Pressman 129740909f66SGal Pressman static int efa_create_inline_pbl(struct efa_dev *dev, struct efa_mr *mr, 129840909f66SGal Pressman struct efa_com_reg_mr_params *params) 129940909f66SGal Pressman { 130040909f66SGal Pressman int err; 130140909f66SGal Pressman 130240909f66SGal Pressman params->inline_pbl = 1; 130340909f66SGal Pressman err = umem_to_page_list(dev, mr->umem, params->pbl.inline_pbl_array, 130440909f66SGal Pressman params->page_num, params->page_shift); 130540909f66SGal Pressman if (err) 130640909f66SGal Pressman return err; 130740909f66SGal Pressman 130840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 130940909f66SGal Pressman "inline_pbl_array - pages[%u]\n", params->page_num); 131040909f66SGal Pressman 131140909f66SGal Pressman return 0; 131240909f66SGal Pressman } 131340909f66SGal Pressman 131440909f66SGal Pressman static int efa_create_pbl(struct efa_dev *dev, 131540909f66SGal Pressman struct pbl_context *pbl, 131640909f66SGal Pressman struct efa_mr *mr, 131740909f66SGal Pressman struct efa_com_reg_mr_params *params) 131840909f66SGal Pressman { 131940909f66SGal Pressman int err; 132040909f66SGal Pressman 132140909f66SGal Pressman err = pbl_create(dev, pbl, mr->umem, params->page_num, 132240909f66SGal Pressman params->page_shift); 132340909f66SGal Pressman if (err) { 132440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Failed to create pbl[%d]\n", err); 132540909f66SGal Pressman return err; 132640909f66SGal Pressman } 132740909f66SGal Pressman 132840909f66SGal Pressman params->inline_pbl = 0; 132940909f66SGal Pressman params->indirect = !pbl->physically_continuous; 133040909f66SGal Pressman if (pbl->physically_continuous) { 133140909f66SGal Pressman params->pbl.pbl.length = pbl->pbl_buf_size_in_bytes; 133240909f66SGal Pressman 133340909f66SGal Pressman efa_com_set_dma_addr(pbl->phys.continuous.dma_addr, 133440909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_high, 133540909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_low); 133640909f66SGal Pressman } else { 133740909f66SGal Pressman params->pbl.pbl.length = 133840909f66SGal Pressman pbl->phys.indirect.chunk_list.chunks[0].length; 133940909f66SGal Pressman 134040909f66SGal Pressman efa_com_set_dma_addr(pbl->phys.indirect.chunk_list.chunks[0].dma_addr, 134140909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_high, 134240909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_low); 134340909f66SGal Pressman } 134440909f66SGal Pressman 134540909f66SGal Pressman return 0; 134640909f66SGal Pressman } 134740909f66SGal Pressman 134840909f66SGal Pressman struct ib_mr *efa_reg_mr(struct ib_pd *ibpd, u64 start, u64 length, 134940909f66SGal Pressman u64 virt_addr, int access_flags, 135040909f66SGal Pressman struct ib_udata *udata) 135140909f66SGal Pressman { 135240909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 135340909f66SGal Pressman struct efa_com_reg_mr_params params = {}; 135440909f66SGal Pressman struct efa_com_reg_mr_result result = {}; 135540909f66SGal Pressman struct pbl_context pbl; 1356666e8ff5SDaniel Kranzdorf int supp_access_flags; 135740ddb3f0SGal Pressman unsigned int pg_sz; 135840909f66SGal Pressman struct efa_mr *mr; 135940909f66SGal Pressman int inline_size; 136040909f66SGal Pressman int err; 136140909f66SGal Pressman 136233006bd4SMoni Shoua if (udata && udata->inlen && 136340909f66SGal Pressman !ib_is_udata_cleared(udata, 0, sizeof(udata->inlen))) { 136440909f66SGal Pressman ibdev_dbg(&dev->ibdev, 136540909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 136640909f66SGal Pressman err = -EINVAL; 136740909f66SGal Pressman goto err_out; 136840909f66SGal Pressman } 136940909f66SGal Pressman 1370666e8ff5SDaniel Kranzdorf supp_access_flags = 1371666e8ff5SDaniel Kranzdorf IB_ACCESS_LOCAL_WRITE | 1372666e8ff5SDaniel Kranzdorf (is_rdma_read_cap(dev) ? IB_ACCESS_REMOTE_READ : 0); 1373666e8ff5SDaniel Kranzdorf 1374ba19e166SGal Pressman access_flags &= ~IB_ACCESS_OPTIONAL; 1375666e8ff5SDaniel Kranzdorf if (access_flags & ~supp_access_flags) { 137640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 137740909f66SGal Pressman "Unsupported access flags[%#x], supported[%#x]\n", 1378666e8ff5SDaniel Kranzdorf access_flags, supp_access_flags); 137940909f66SGal Pressman err = -EOPNOTSUPP; 138040909f66SGal Pressman goto err_out; 138140909f66SGal Pressman } 138240909f66SGal Pressman 138340909f66SGal Pressman mr = kzalloc(sizeof(*mr), GFP_KERNEL); 138440909f66SGal Pressman if (!mr) { 138540909f66SGal Pressman err = -ENOMEM; 138640909f66SGal Pressman goto err_out; 138740909f66SGal Pressman } 138840909f66SGal Pressman 1389c320e527SMoni Shoua mr->umem = ib_umem_get(ibpd->device, start, length, access_flags); 139040909f66SGal Pressman if (IS_ERR(mr->umem)) { 139140909f66SGal Pressman err = PTR_ERR(mr->umem); 139240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 139340909f66SGal Pressman "Failed to pin and map user space memory[%d]\n", err); 139440909f66SGal Pressman goto err_free; 139540909f66SGal Pressman } 139640909f66SGal Pressman 139740909f66SGal Pressman params.pd = to_epd(ibpd)->pdn; 139840909f66SGal Pressman params.iova = virt_addr; 139940909f66SGal Pressman params.mr_length_in_bytes = length; 1400e6c4f3ffSDaniel Kranzdorf params.permissions = access_flags; 140140909f66SGal Pressman 140240ddb3f0SGal Pressman pg_sz = ib_umem_find_best_pgsz(mr->umem, 140340ddb3f0SGal Pressman dev->dev_attr.page_size_cap, 140440ddb3f0SGal Pressman virt_addr); 140540ddb3f0SGal Pressman if (!pg_sz) { 140640ddb3f0SGal Pressman err = -EOPNOTSUPP; 140740ddb3f0SGal Pressman ibdev_dbg(&dev->ibdev, "Failed to find a suitable page size in page_size_cap %#llx\n", 140840ddb3f0SGal Pressman dev->dev_attr.page_size_cap); 140940ddb3f0SGal Pressman goto err_unmap; 141040ddb3f0SGal Pressman } 141140ddb3f0SGal Pressman 141240ddb3f0SGal Pressman params.page_shift = __ffs(pg_sz); 141340ddb3f0SGal Pressman params.page_num = DIV_ROUND_UP(length + (start & (pg_sz - 1)), 141440ddb3f0SGal Pressman pg_sz); 141540ddb3f0SGal Pressman 141640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 141740ddb3f0SGal Pressman "start %#llx length %#llx params.page_shift %u params.page_num %u\n", 141840ddb3f0SGal Pressman start, length, params.page_shift, params.page_num); 141940909f66SGal Pressman 142040909f66SGal Pressman inline_size = ARRAY_SIZE(params.pbl.inline_pbl_array); 142140909f66SGal Pressman if (params.page_num <= inline_size) { 142240909f66SGal Pressman err = efa_create_inline_pbl(dev, mr, ¶ms); 142340909f66SGal Pressman if (err) 142440909f66SGal Pressman goto err_unmap; 142540909f66SGal Pressman 142640909f66SGal Pressman err = efa_com_register_mr(&dev->edev, ¶ms, &result); 142740909f66SGal Pressman if (err) 142840909f66SGal Pressman goto err_unmap; 142940909f66SGal Pressman } else { 143040909f66SGal Pressman err = efa_create_pbl(dev, &pbl, mr, ¶ms); 143140909f66SGal Pressman if (err) 143240909f66SGal Pressman goto err_unmap; 143340909f66SGal Pressman 143440909f66SGal Pressman err = efa_com_register_mr(&dev->edev, ¶ms, &result); 143540909f66SGal Pressman pbl_destroy(dev, &pbl); 143640909f66SGal Pressman 143740909f66SGal Pressman if (err) 143840909f66SGal Pressman goto err_unmap; 143940909f66SGal Pressman } 144040909f66SGal Pressman 144140909f66SGal Pressman mr->ibmr.lkey = result.l_key; 144240909f66SGal Pressman mr->ibmr.rkey = result.r_key; 144340909f66SGal Pressman mr->ibmr.length = length; 144440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Registered mr[%d]\n", mr->ibmr.lkey); 144540909f66SGal Pressman 144640909f66SGal Pressman return &mr->ibmr; 144740909f66SGal Pressman 144840909f66SGal Pressman err_unmap: 144940909f66SGal Pressman ib_umem_release(mr->umem); 145040909f66SGal Pressman err_free: 145140909f66SGal Pressman kfree(mr); 145240909f66SGal Pressman err_out: 145340909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.reg_mr_err); 145440909f66SGal Pressman return ERR_PTR(err); 145540909f66SGal Pressman } 145640909f66SGal Pressman 145740909f66SGal Pressman int efa_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata) 145840909f66SGal Pressman { 145940909f66SGal Pressman struct efa_dev *dev = to_edev(ibmr->device); 146040909f66SGal Pressman struct efa_com_dereg_mr_params params; 146140909f66SGal Pressman struct efa_mr *mr = to_emr(ibmr); 146240909f66SGal Pressman int err; 146340909f66SGal Pressman 146440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Deregister mr[%d]\n", ibmr->lkey); 146540909f66SGal Pressman 146640909f66SGal Pressman params.l_key = mr->ibmr.lkey; 146740909f66SGal Pressman err = efa_com_dereg_mr(&dev->edev, ¶ms); 146840909f66SGal Pressman if (err) 146940909f66SGal Pressman return err; 147040909f66SGal Pressman 1471958b6813SGal Pressman ib_umem_release(mr->umem); 147240909f66SGal Pressman kfree(mr); 147340909f66SGal Pressman 147440909f66SGal Pressman return 0; 147540909f66SGal Pressman } 147640909f66SGal Pressman 147740909f66SGal Pressman int efa_get_port_immutable(struct ib_device *ibdev, u8 port_num, 147840909f66SGal Pressman struct ib_port_immutable *immutable) 147940909f66SGal Pressman { 148040909f66SGal Pressman struct ib_port_attr attr; 148140909f66SGal Pressman int err; 148240909f66SGal Pressman 148340909f66SGal Pressman err = ib_query_port(ibdev, port_num, &attr); 148440909f66SGal Pressman if (err) { 148540909f66SGal Pressman ibdev_dbg(ibdev, "Couldn't query port err[%d]\n", err); 148640909f66SGal Pressman return err; 148740909f66SGal Pressman } 148840909f66SGal Pressman 148940909f66SGal Pressman immutable->pkey_tbl_len = attr.pkey_tbl_len; 149040909f66SGal Pressman immutable->gid_tbl_len = attr.gid_tbl_len; 149140909f66SGal Pressman 149240909f66SGal Pressman return 0; 149340909f66SGal Pressman } 149440909f66SGal Pressman 149540909f66SGal Pressman static int efa_dealloc_uar(struct efa_dev *dev, u16 uarn) 149640909f66SGal Pressman { 149740909f66SGal Pressman struct efa_com_dealloc_uar_params params = { 149840909f66SGal Pressman .uarn = uarn, 149940909f66SGal Pressman }; 150040909f66SGal Pressman 150140909f66SGal Pressman return efa_com_dealloc_uar(&dev->edev, ¶ms); 150240909f66SGal Pressman } 150340909f66SGal Pressman 150440909f66SGal Pressman int efa_alloc_ucontext(struct ib_ucontext *ibucontext, struct ib_udata *udata) 150540909f66SGal Pressman { 150640909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 150740909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 150840909f66SGal Pressman struct efa_ibv_alloc_ucontext_resp resp = {}; 150940909f66SGal Pressman struct efa_com_alloc_uar_result result; 151040909f66SGal Pressman int err; 151140909f66SGal Pressman 151240909f66SGal Pressman /* 151340909f66SGal Pressman * it's fine if the driver does not know all request fields, 151440909f66SGal Pressman * we will ack input fields in our response. 151540909f66SGal Pressman */ 151640909f66SGal Pressman 151740909f66SGal Pressman err = efa_com_alloc_uar(&dev->edev, &result); 151840909f66SGal Pressman if (err) 151940909f66SGal Pressman goto err_out; 152040909f66SGal Pressman 152140909f66SGal Pressman ucontext->uarn = result.uarn; 152240909f66SGal Pressman 152340909f66SGal Pressman resp.cmds_supp_udata_mask |= EFA_USER_CMDS_SUPP_UDATA_QUERY_DEVICE; 152440909f66SGal Pressman resp.cmds_supp_udata_mask |= EFA_USER_CMDS_SUPP_UDATA_CREATE_AH; 152540909f66SGal Pressman resp.sub_cqs_per_cq = dev->dev_attr.sub_cqs_per_cq; 152640909f66SGal Pressman resp.inline_buf_size = dev->dev_attr.inline_buf_size; 152740909f66SGal Pressman resp.max_llq_size = dev->dev_attr.max_llq_size; 152840909f66SGal Pressman 152940909f66SGal Pressman if (udata && udata->outlen) { 153040909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 153140909f66SGal Pressman min(sizeof(resp), udata->outlen)); 153240909f66SGal Pressman if (err) 153340909f66SGal Pressman goto err_dealloc_uar; 153440909f66SGal Pressman } 153540909f66SGal Pressman 153640909f66SGal Pressman return 0; 153740909f66SGal Pressman 153840909f66SGal Pressman err_dealloc_uar: 153940909f66SGal Pressman efa_dealloc_uar(dev, result.uarn); 154040909f66SGal Pressman err_out: 154140909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.alloc_ucontext_err); 154240909f66SGal Pressman return err; 154340909f66SGal Pressman } 154440909f66SGal Pressman 154540909f66SGal Pressman void efa_dealloc_ucontext(struct ib_ucontext *ibucontext) 154640909f66SGal Pressman { 154740909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 154840909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 154940909f66SGal Pressman 155040909f66SGal Pressman efa_dealloc_uar(dev, ucontext->uarn); 155140909f66SGal Pressman } 155240909f66SGal Pressman 1553e84d3c18SMichal Kalderon void efa_mmap_free(struct rdma_user_mmap_entry *rdma_entry) 155440909f66SGal Pressman { 1555e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry = to_emmap(rdma_entry); 155640909f66SGal Pressman 1557e84d3c18SMichal Kalderon kfree(entry); 155840909f66SGal Pressman } 155940909f66SGal Pressman 1560e84d3c18SMichal Kalderon static int __efa_mmap(struct efa_dev *dev, struct efa_ucontext *ucontext, 1561e84d3c18SMichal Kalderon struct vm_area_struct *vma) 1562e84d3c18SMichal Kalderon { 1563e84d3c18SMichal Kalderon struct rdma_user_mmap_entry *rdma_entry; 1564e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry; 1565e84d3c18SMichal Kalderon unsigned long va; 1566e84d3c18SMichal Kalderon int err = 0; 1567e84d3c18SMichal Kalderon u64 pfn; 1568e84d3c18SMichal Kalderon 1569e84d3c18SMichal Kalderon rdma_entry = rdma_user_mmap_entry_get(&ucontext->ibucontext, vma); 1570e84d3c18SMichal Kalderon if (!rdma_entry) { 157140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 1572e84d3c18SMichal Kalderon "pgoff[%#lx] does not have valid entry\n", 1573e84d3c18SMichal Kalderon vma->vm_pgoff); 1574e84d3c18SMichal Kalderon return -EINVAL; 1575e84d3c18SMichal Kalderon } 1576e84d3c18SMichal Kalderon entry = to_emmap(rdma_entry); 1577e84d3c18SMichal Kalderon 1578e84d3c18SMichal Kalderon ibdev_dbg(&dev->ibdev, 1579e84d3c18SMichal Kalderon "Mapping address[%#llx], length[%#zx], mmap_flag[%d]\n", 1580e84d3c18SMichal Kalderon entry->address, rdma_entry->npages * PAGE_SIZE, 1581e84d3c18SMichal Kalderon entry->mmap_flag); 158240909f66SGal Pressman 158340909f66SGal Pressman pfn = entry->address >> PAGE_SHIFT; 158440909f66SGal Pressman switch (entry->mmap_flag) { 158540909f66SGal Pressman case EFA_MMAP_IO_NC: 1586e84d3c18SMichal Kalderon err = rdma_user_mmap_io(&ucontext->ibucontext, vma, pfn, 1587e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE, 1588c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 1589e84d3c18SMichal Kalderon rdma_entry); 159040909f66SGal Pressman break; 159140909f66SGal Pressman case EFA_MMAP_IO_WC: 1592e84d3c18SMichal Kalderon err = rdma_user_mmap_io(&ucontext->ibucontext, vma, pfn, 1593e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE, 1594c043ff2cSMichal Kalderon pgprot_writecombine(vma->vm_page_prot), 1595e84d3c18SMichal Kalderon rdma_entry); 159640909f66SGal Pressman break; 159740909f66SGal Pressman case EFA_MMAP_DMA_PAGE: 159840909f66SGal Pressman for (va = vma->vm_start; va < vma->vm_end; 159940909f66SGal Pressman va += PAGE_SIZE, pfn++) { 160040909f66SGal Pressman err = vm_insert_page(vma, va, pfn_to_page(pfn)); 160140909f66SGal Pressman if (err) 160240909f66SGal Pressman break; 160340909f66SGal Pressman } 160440909f66SGal Pressman break; 160540909f66SGal Pressman default: 160640909f66SGal Pressman err = -EINVAL; 160740909f66SGal Pressman } 160840909f66SGal Pressman 1609f5f5ddbeSGal Pressman if (err) 161040909f66SGal Pressman ibdev_dbg( 161140909f66SGal Pressman &dev->ibdev, 1612e84d3c18SMichal Kalderon "Couldn't mmap address[%#llx] length[%#zx] mmap_flag[%d] err[%d]\n", 1613e84d3c18SMichal Kalderon entry->address, rdma_entry->npages * PAGE_SIZE, 1614e84d3c18SMichal Kalderon entry->mmap_flag, err); 161540909f66SGal Pressman 1616e84d3c18SMichal Kalderon rdma_user_mmap_entry_put(rdma_entry); 1617e84d3c18SMichal Kalderon return err; 1618b41f7572SGal Pressman } 1619b41f7572SGal Pressman 162040909f66SGal Pressman int efa_mmap(struct ib_ucontext *ibucontext, 162140909f66SGal Pressman struct vm_area_struct *vma) 162240909f66SGal Pressman { 162340909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 162440909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 1625e84d3c18SMichal Kalderon size_t length = vma->vm_end - vma->vm_start; 162640909f66SGal Pressman 162740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 1628e84d3c18SMichal Kalderon "start %#lx, end %#lx, length = %#zx, pgoff = %#lx\n", 1629e84d3c18SMichal Kalderon vma->vm_start, vma->vm_end, length, vma->vm_pgoff); 163040909f66SGal Pressman 1631e84d3c18SMichal Kalderon return __efa_mmap(dev, ucontext, vma); 163240909f66SGal Pressman } 163340909f66SGal Pressman 163440909f66SGal Pressman static int efa_ah_destroy(struct efa_dev *dev, struct efa_ah *ah) 163540909f66SGal Pressman { 163640909f66SGal Pressman struct efa_com_destroy_ah_params params = { 163740909f66SGal Pressman .ah = ah->ah, 163840909f66SGal Pressman .pdn = to_epd(ah->ibah.pd)->pdn, 163940909f66SGal Pressman }; 164040909f66SGal Pressman 164140909f66SGal Pressman return efa_com_destroy_ah(&dev->edev, ¶ms); 164240909f66SGal Pressman } 164340909f66SGal Pressman 164440909f66SGal Pressman int efa_create_ah(struct ib_ah *ibah, 164540909f66SGal Pressman struct rdma_ah_attr *ah_attr, 164640909f66SGal Pressman u32 flags, 164740909f66SGal Pressman struct ib_udata *udata) 164840909f66SGal Pressman { 164940909f66SGal Pressman struct efa_dev *dev = to_edev(ibah->device); 165040909f66SGal Pressman struct efa_com_create_ah_params params = {}; 165140909f66SGal Pressman struct efa_ibv_create_ah_resp resp = {}; 165240909f66SGal Pressman struct efa_com_create_ah_result result; 165340909f66SGal Pressman struct efa_ah *ah = to_eah(ibah); 165440909f66SGal Pressman int err; 165540909f66SGal Pressman 165640909f66SGal Pressman if (!(flags & RDMA_CREATE_AH_SLEEPABLE)) { 165740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 165840909f66SGal Pressman "Create address handle is not supported in atomic context\n"); 165940909f66SGal Pressman err = -EOPNOTSUPP; 166040909f66SGal Pressman goto err_out; 166140909f66SGal Pressman } 166240909f66SGal Pressman 166340909f66SGal Pressman if (udata->inlen && 166440909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 166540909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Incompatible ABI params\n"); 166640909f66SGal Pressman err = -EINVAL; 166740909f66SGal Pressman goto err_out; 166840909f66SGal Pressman } 166940909f66SGal Pressman 167040909f66SGal Pressman memcpy(params.dest_addr, ah_attr->grh.dgid.raw, 167140909f66SGal Pressman sizeof(params.dest_addr)); 167240909f66SGal Pressman params.pdn = to_epd(ibah->pd)->pdn; 167340909f66SGal Pressman err = efa_com_create_ah(&dev->edev, ¶ms, &result); 167440909f66SGal Pressman if (err) 167540909f66SGal Pressman goto err_out; 167640909f66SGal Pressman 167740909f66SGal Pressman memcpy(ah->id, ah_attr->grh.dgid.raw, sizeof(ah->id)); 167840909f66SGal Pressman ah->ah = result.ah; 167940909f66SGal Pressman 168040909f66SGal Pressman resp.efa_address_handle = result.ah; 168140909f66SGal Pressman 168240909f66SGal Pressman if (udata->outlen) { 168340909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 168440909f66SGal Pressman min(sizeof(resp), udata->outlen)); 168540909f66SGal Pressman if (err) { 168640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 168740909f66SGal Pressman "Failed to copy udata for create_ah response\n"); 168840909f66SGal Pressman goto err_destroy_ah; 168940909f66SGal Pressman } 169040909f66SGal Pressman } 169140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Created ah[%d]\n", ah->ah); 169240909f66SGal Pressman 169340909f66SGal Pressman return 0; 169440909f66SGal Pressman 169540909f66SGal Pressman err_destroy_ah: 169640909f66SGal Pressman efa_ah_destroy(dev, ah); 169740909f66SGal Pressman err_out: 169840909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.create_ah_err); 169940909f66SGal Pressman return err; 170040909f66SGal Pressman } 170140909f66SGal Pressman 170240909f66SGal Pressman void efa_destroy_ah(struct ib_ah *ibah, u32 flags) 170340909f66SGal Pressman { 170440909f66SGal Pressman struct efa_dev *dev = to_edev(ibah->pd->device); 170540909f66SGal Pressman struct efa_ah *ah = to_eah(ibah); 170640909f66SGal Pressman 170740909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Destroy ah[%d]\n", ah->ah); 170840909f66SGal Pressman 170940909f66SGal Pressman if (!(flags & RDMA_DESTROY_AH_SLEEPABLE)) { 171040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 171140909f66SGal Pressman "Destroy address handle is not supported in atomic context\n"); 171240909f66SGal Pressman return; 171340909f66SGal Pressman } 171440909f66SGal Pressman 171540909f66SGal Pressman efa_ah_destroy(dev, ah); 171640909f66SGal Pressman } 171740909f66SGal Pressman 171816e9111eSGal Pressman struct rdma_hw_stats *efa_alloc_hw_stats(struct ib_device *ibdev, u8 port_num) 171916e9111eSGal Pressman { 172016e9111eSGal Pressman return rdma_alloc_hw_stats_struct(efa_stats_names, 172116e9111eSGal Pressman ARRAY_SIZE(efa_stats_names), 172216e9111eSGal Pressman RDMA_HW_STATS_DEFAULT_LIFESPAN); 172316e9111eSGal Pressman } 172416e9111eSGal Pressman 172516e9111eSGal Pressman int efa_get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats, 172616e9111eSGal Pressman u8 port_num, int index) 172716e9111eSGal Pressman { 172816e9111eSGal Pressman struct efa_com_get_stats_params params = {}; 172916e9111eSGal Pressman union efa_com_get_stats_result result; 173016e9111eSGal Pressman struct efa_dev *dev = to_edev(ibdev); 173116e9111eSGal Pressman struct efa_com_basic_stats *bs; 173216e9111eSGal Pressman struct efa_com_stats_admin *as; 173316e9111eSGal Pressman struct efa_stats *s; 173416e9111eSGal Pressman int err; 173516e9111eSGal Pressman 173616e9111eSGal Pressman params.type = EFA_ADMIN_GET_STATS_TYPE_BASIC; 173716e9111eSGal Pressman params.scope = EFA_ADMIN_GET_STATS_SCOPE_ALL; 173816e9111eSGal Pressman 173916e9111eSGal Pressman err = efa_com_get_stats(&dev->edev, ¶ms, &result); 174016e9111eSGal Pressman if (err) 174116e9111eSGal Pressman return err; 174216e9111eSGal Pressman 174316e9111eSGal Pressman bs = &result.basic_stats; 174416e9111eSGal Pressman stats->value[EFA_TX_BYTES] = bs->tx_bytes; 174516e9111eSGal Pressman stats->value[EFA_TX_PKTS] = bs->tx_pkts; 174616e9111eSGal Pressman stats->value[EFA_RX_BYTES] = bs->rx_bytes; 174716e9111eSGal Pressman stats->value[EFA_RX_PKTS] = bs->rx_pkts; 174816e9111eSGal Pressman stats->value[EFA_RX_DROPS] = bs->rx_drops; 174916e9111eSGal Pressman 175016e9111eSGal Pressman as = &dev->edev.aq.stats; 175116e9111eSGal Pressman stats->value[EFA_SUBMITTED_CMDS] = atomic64_read(&as->submitted_cmd); 175216e9111eSGal Pressman stats->value[EFA_COMPLETED_CMDS] = atomic64_read(&as->completed_cmd); 175316e9111eSGal Pressman stats->value[EFA_NO_COMPLETION_CMDS] = atomic64_read(&as->no_completion); 175416e9111eSGal Pressman 175516e9111eSGal Pressman s = &dev->stats; 175616e9111eSGal Pressman stats->value[EFA_KEEP_ALIVE_RCVD] = atomic64_read(&s->keep_alive_rcvd); 175716e9111eSGal Pressman stats->value[EFA_ALLOC_PD_ERR] = atomic64_read(&s->sw_stats.alloc_pd_err); 175816e9111eSGal Pressman stats->value[EFA_CREATE_QP_ERR] = atomic64_read(&s->sw_stats.create_qp_err); 175916e9111eSGal Pressman stats->value[EFA_REG_MR_ERR] = atomic64_read(&s->sw_stats.reg_mr_err); 176016e9111eSGal Pressman stats->value[EFA_ALLOC_UCONTEXT_ERR] = atomic64_read(&s->sw_stats.alloc_ucontext_err); 176116e9111eSGal Pressman stats->value[EFA_CREATE_AH_ERR] = atomic64_read(&s->sw_stats.create_ah_err); 176216e9111eSGal Pressman 176316e9111eSGal Pressman return ARRAY_SIZE(efa_stats_names); 176416e9111eSGal Pressman } 176516e9111eSGal Pressman 176640909f66SGal Pressman enum rdma_link_layer efa_port_link_layer(struct ib_device *ibdev, 176740909f66SGal Pressman u8 port_num) 176840909f66SGal Pressman { 176940909f66SGal Pressman return IB_LINK_LAYER_UNSPECIFIED; 177040909f66SGal Pressman } 177140909f66SGal Pressman 1772