140909f66SGal Pressman // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 240909f66SGal Pressman /* 340909f66SGal Pressman * Copyright 2018-2019 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 73*e6c4f3ffSDaniel Kranzdorf #define EFA_SUPPORTED_ACCESS_FLAGS \ 74*e6c4f3ffSDaniel Kranzdorf (IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ) 7540909f66SGal Pressman 7640909f66SGal Pressman struct pbl_chunk { 7740909f66SGal Pressman dma_addr_t dma_addr; 7840909f66SGal Pressman u64 *buf; 7940909f66SGal Pressman u32 length; 8040909f66SGal Pressman }; 8140909f66SGal Pressman 8240909f66SGal Pressman struct pbl_chunk_list { 8340909f66SGal Pressman struct pbl_chunk *chunks; 8440909f66SGal Pressman unsigned int size; 8540909f66SGal Pressman }; 8640909f66SGal Pressman 8740909f66SGal Pressman struct pbl_context { 8840909f66SGal Pressman union { 8940909f66SGal Pressman struct { 9040909f66SGal Pressman dma_addr_t dma_addr; 9140909f66SGal Pressman } continuous; 9240909f66SGal Pressman struct { 9340909f66SGal Pressman u32 pbl_buf_size_in_pages; 9440909f66SGal Pressman struct scatterlist *sgl; 9540909f66SGal Pressman int sg_dma_cnt; 9640909f66SGal Pressman struct pbl_chunk_list chunk_list; 9740909f66SGal Pressman } indirect; 9840909f66SGal Pressman } phys; 9940909f66SGal Pressman u64 *pbl_buf; 10040909f66SGal Pressman u32 pbl_buf_size_in_bytes; 10140909f66SGal Pressman u8 physically_continuous; 10240909f66SGal Pressman }; 10340909f66SGal Pressman 10440909f66SGal Pressman static inline struct efa_dev *to_edev(struct ib_device *ibdev) 10540909f66SGal Pressman { 10640909f66SGal Pressman return container_of(ibdev, struct efa_dev, ibdev); 10740909f66SGal Pressman } 10840909f66SGal Pressman 10940909f66SGal Pressman static inline struct efa_ucontext *to_eucontext(struct ib_ucontext *ibucontext) 11040909f66SGal Pressman { 11140909f66SGal Pressman return container_of(ibucontext, struct efa_ucontext, ibucontext); 11240909f66SGal Pressman } 11340909f66SGal Pressman 11440909f66SGal Pressman static inline struct efa_pd *to_epd(struct ib_pd *ibpd) 11540909f66SGal Pressman { 11640909f66SGal Pressman return container_of(ibpd, struct efa_pd, ibpd); 11740909f66SGal Pressman } 11840909f66SGal Pressman 11940909f66SGal Pressman static inline struct efa_mr *to_emr(struct ib_mr *ibmr) 12040909f66SGal Pressman { 12140909f66SGal Pressman return container_of(ibmr, struct efa_mr, ibmr); 12240909f66SGal Pressman } 12340909f66SGal Pressman 12440909f66SGal Pressman static inline struct efa_qp *to_eqp(struct ib_qp *ibqp) 12540909f66SGal Pressman { 12640909f66SGal Pressman return container_of(ibqp, struct efa_qp, ibqp); 12740909f66SGal Pressman } 12840909f66SGal Pressman 12940909f66SGal Pressman static inline struct efa_cq *to_ecq(struct ib_cq *ibcq) 13040909f66SGal Pressman { 13140909f66SGal Pressman return container_of(ibcq, struct efa_cq, ibcq); 13240909f66SGal Pressman } 13340909f66SGal Pressman 13440909f66SGal Pressman static inline struct efa_ah *to_eah(struct ib_ah *ibah) 13540909f66SGal Pressman { 13640909f66SGal Pressman return container_of(ibah, struct efa_ah, ibah); 13740909f66SGal Pressman } 13840909f66SGal Pressman 139e84d3c18SMichal Kalderon static inline struct efa_user_mmap_entry * 140e84d3c18SMichal Kalderon to_emmap(struct rdma_user_mmap_entry *rdma_entry) 141e84d3c18SMichal Kalderon { 142e84d3c18SMichal Kalderon return container_of(rdma_entry, struct efa_user_mmap_entry, rdma_entry); 143e84d3c18SMichal Kalderon } 144e84d3c18SMichal Kalderon 14540909f66SGal Pressman #define field_avail(x, fld, sz) (offsetof(typeof(x), fld) + \ 1461bc5ba83SGal Pressman FIELD_SIZEOF(typeof(x), fld) <= (sz)) 14740909f66SGal Pressman 14840909f66SGal Pressman #define is_reserved_cleared(reserved) \ 14940909f66SGal Pressman !memchr_inv(reserved, 0, sizeof(reserved)) 15040909f66SGal Pressman 15140909f66SGal Pressman static void *efa_zalloc_mapped(struct efa_dev *dev, dma_addr_t *dma_addr, 15240909f66SGal Pressman size_t size, enum dma_data_direction dir) 15340909f66SGal Pressman { 15440909f66SGal Pressman void *addr; 15540909f66SGal Pressman 15640909f66SGal Pressman addr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); 15740909f66SGal Pressman if (!addr) 15840909f66SGal Pressman return NULL; 15940909f66SGal Pressman 16040909f66SGal Pressman *dma_addr = dma_map_single(&dev->pdev->dev, addr, size, dir); 16140909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, *dma_addr)) { 16240909f66SGal Pressman ibdev_err(&dev->ibdev, "Failed to map DMA address\n"); 16340909f66SGal Pressman free_pages_exact(addr, size); 16440909f66SGal Pressman return NULL; 16540909f66SGal Pressman } 16640909f66SGal Pressman 16740909f66SGal Pressman return addr; 16840909f66SGal Pressman } 16940909f66SGal Pressman 17040909f66SGal Pressman int efa_query_device(struct ib_device *ibdev, 17140909f66SGal Pressman struct ib_device_attr *props, 17240909f66SGal Pressman struct ib_udata *udata) 17340909f66SGal Pressman { 17440909f66SGal Pressman struct efa_com_get_device_attr_result *dev_attr; 17540909f66SGal Pressman struct efa_ibv_ex_query_device_resp resp = {}; 17640909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 17740909f66SGal Pressman int err; 17840909f66SGal Pressman 17940909f66SGal Pressman if (udata && udata->inlen && 18040909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 18140909f66SGal Pressman ibdev_dbg(ibdev, 18240909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 18340909f66SGal Pressman return -EINVAL; 18440909f66SGal Pressman } 18540909f66SGal Pressman 18640909f66SGal Pressman dev_attr = &dev->dev_attr; 18740909f66SGal Pressman 18840909f66SGal Pressman memset(props, 0, sizeof(*props)); 18940909f66SGal Pressman props->max_mr_size = dev_attr->max_mr_pages * PAGE_SIZE; 19040909f66SGal Pressman props->page_size_cap = dev_attr->page_size_cap; 19140909f66SGal Pressman props->vendor_id = dev->pdev->vendor; 19240909f66SGal Pressman props->vendor_part_id = dev->pdev->device; 19340909f66SGal Pressman props->hw_ver = dev->pdev->subsystem_device; 19440909f66SGal Pressman props->max_qp = dev_attr->max_qp; 19540909f66SGal Pressman props->max_cq = dev_attr->max_cq; 19640909f66SGal Pressman props->max_pd = dev_attr->max_pd; 19740909f66SGal Pressman props->max_mr = dev_attr->max_mr; 19840909f66SGal Pressman props->max_ah = dev_attr->max_ah; 19940909f66SGal Pressman props->max_cqe = dev_attr->max_cq_depth; 20040909f66SGal Pressman props->max_qp_wr = min_t(u32, dev_attr->max_sq_depth, 20140909f66SGal Pressman dev_attr->max_rq_depth); 20240909f66SGal Pressman props->max_send_sge = dev_attr->max_sq_sge; 20340909f66SGal Pressman props->max_recv_sge = dev_attr->max_rq_sge; 20440909f66SGal Pressman 20540909f66SGal Pressman if (udata && udata->outlen) { 20640909f66SGal Pressman resp.max_sq_sge = dev_attr->max_sq_sge; 20740909f66SGal Pressman resp.max_rq_sge = dev_attr->max_rq_sge; 20840909f66SGal Pressman resp.max_sq_wr = dev_attr->max_sq_depth; 20940909f66SGal Pressman resp.max_rq_wr = dev_attr->max_rq_depth; 21040909f66SGal Pressman 21140909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 21240909f66SGal Pressman min(sizeof(resp), udata->outlen)); 21340909f66SGal Pressman if (err) { 21440909f66SGal Pressman ibdev_dbg(ibdev, 21540909f66SGal Pressman "Failed to copy udata for query_device\n"); 21640909f66SGal Pressman return err; 21740909f66SGal Pressman } 21840909f66SGal Pressman } 21940909f66SGal Pressman 22040909f66SGal Pressman return 0; 22140909f66SGal Pressman } 22240909f66SGal Pressman 22340909f66SGal Pressman int efa_query_port(struct ib_device *ibdev, u8 port, 22440909f66SGal Pressman struct ib_port_attr *props) 22540909f66SGal Pressman { 22640909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 22740909f66SGal Pressman 22840909f66SGal Pressman props->lmc = 1; 22940909f66SGal Pressman 23040909f66SGal Pressman props->state = IB_PORT_ACTIVE; 23172a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 23240909f66SGal Pressman props->gid_tbl_len = 1; 23340909f66SGal Pressman props->pkey_tbl_len = 1; 23440909f66SGal Pressman props->active_speed = IB_SPEED_EDR; 23540909f66SGal Pressman props->active_width = IB_WIDTH_4X; 236bcf7cc53SGal Pressman props->max_mtu = ib_mtu_int_to_enum(dev->dev_attr.mtu); 237bcf7cc53SGal Pressman props->active_mtu = ib_mtu_int_to_enum(dev->dev_attr.mtu); 238bcf7cc53SGal Pressman props->max_msg_sz = dev->dev_attr.mtu; 23940909f66SGal Pressman props->max_vl_num = 1; 24040909f66SGal Pressman 24140909f66SGal Pressman return 0; 24240909f66SGal Pressman } 24340909f66SGal Pressman 24440909f66SGal Pressman int efa_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, 24540909f66SGal Pressman int qp_attr_mask, 24640909f66SGal Pressman struct ib_qp_init_attr *qp_init_attr) 24740909f66SGal Pressman { 24840909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->device); 24940909f66SGal Pressman struct efa_com_query_qp_params params = {}; 25040909f66SGal Pressman struct efa_com_query_qp_result result; 25140909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 25240909f66SGal Pressman int err; 25340909f66SGal Pressman 25440909f66SGal Pressman #define EFA_QUERY_QP_SUPP_MASK \ 25540909f66SGal Pressman (IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT | \ 25640909f66SGal Pressman IB_QP_QKEY | IB_QP_SQ_PSN | IB_QP_CAP) 25740909f66SGal Pressman 25840909f66SGal Pressman if (qp_attr_mask & ~EFA_QUERY_QP_SUPP_MASK) { 25940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 26040909f66SGal Pressman "Unsupported qp_attr_mask[%#x] supported[%#x]\n", 26140909f66SGal Pressman qp_attr_mask, EFA_QUERY_QP_SUPP_MASK); 26240909f66SGal Pressman return -EOPNOTSUPP; 26340909f66SGal Pressman } 26440909f66SGal Pressman 26540909f66SGal Pressman memset(qp_attr, 0, sizeof(*qp_attr)); 26640909f66SGal Pressman memset(qp_init_attr, 0, sizeof(*qp_init_attr)); 26740909f66SGal Pressman 26840909f66SGal Pressman params.qp_handle = qp->qp_handle; 26940909f66SGal Pressman err = efa_com_query_qp(&dev->edev, ¶ms, &result); 27040909f66SGal Pressman if (err) 27140909f66SGal Pressman return err; 27240909f66SGal Pressman 27340909f66SGal Pressman qp_attr->qp_state = result.qp_state; 27440909f66SGal Pressman qp_attr->qkey = result.qkey; 27540909f66SGal Pressman qp_attr->sq_psn = result.sq_psn; 27640909f66SGal Pressman qp_attr->sq_draining = result.sq_draining; 27740909f66SGal Pressman qp_attr->port_num = 1; 27840909f66SGal Pressman 27940909f66SGal Pressman qp_attr->cap.max_send_wr = qp->max_send_wr; 28040909f66SGal Pressman qp_attr->cap.max_recv_wr = qp->max_recv_wr; 28140909f66SGal Pressman qp_attr->cap.max_send_sge = qp->max_send_sge; 28240909f66SGal Pressman qp_attr->cap.max_recv_sge = qp->max_recv_sge; 28340909f66SGal Pressman qp_attr->cap.max_inline_data = qp->max_inline_data; 28440909f66SGal Pressman 28540909f66SGal Pressman qp_init_attr->qp_type = ibqp->qp_type; 28640909f66SGal Pressman qp_init_attr->recv_cq = ibqp->recv_cq; 28740909f66SGal Pressman qp_init_attr->send_cq = ibqp->send_cq; 28840909f66SGal Pressman qp_init_attr->qp_context = ibqp->qp_context; 28940909f66SGal Pressman qp_init_attr->cap = qp_attr->cap; 29040909f66SGal Pressman 29140909f66SGal Pressman return 0; 29240909f66SGal Pressman } 29340909f66SGal Pressman 29440909f66SGal Pressman int efa_query_gid(struct ib_device *ibdev, u8 port, int index, 29540909f66SGal Pressman union ib_gid *gid) 29640909f66SGal Pressman { 29740909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 29840909f66SGal Pressman 299bcf7cc53SGal Pressman memcpy(gid->raw, dev->dev_attr.addr, sizeof(dev->dev_attr.addr)); 30040909f66SGal Pressman 30140909f66SGal Pressman return 0; 30240909f66SGal Pressman } 30340909f66SGal Pressman 30440909f66SGal Pressman int efa_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 30540909f66SGal Pressman u16 *pkey) 30640909f66SGal Pressman { 30740909f66SGal Pressman if (index > 0) 30840909f66SGal Pressman return -EINVAL; 30940909f66SGal Pressman 31040909f66SGal Pressman *pkey = 0xffff; 31140909f66SGal Pressman return 0; 31240909f66SGal Pressman } 31340909f66SGal Pressman 31440909f66SGal Pressman static int efa_pd_dealloc(struct efa_dev *dev, u16 pdn) 31540909f66SGal Pressman { 31640909f66SGal Pressman struct efa_com_dealloc_pd_params params = { 31740909f66SGal Pressman .pdn = pdn, 31840909f66SGal Pressman }; 31940909f66SGal Pressman 32040909f66SGal Pressman return efa_com_dealloc_pd(&dev->edev, ¶ms); 32140909f66SGal Pressman } 32240909f66SGal Pressman 32340909f66SGal Pressman int efa_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 32440909f66SGal Pressman { 32540909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 32640909f66SGal Pressman struct efa_ibv_alloc_pd_resp resp = {}; 32740909f66SGal Pressman struct efa_com_alloc_pd_result result; 32840909f66SGal Pressman struct efa_pd *pd = to_epd(ibpd); 32940909f66SGal Pressman int err; 33040909f66SGal Pressman 33140909f66SGal Pressman if (udata->inlen && 33240909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 33340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 33440909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 33540909f66SGal Pressman err = -EINVAL; 33640909f66SGal Pressman goto err_out; 33740909f66SGal Pressman } 33840909f66SGal Pressman 33940909f66SGal Pressman err = efa_com_alloc_pd(&dev->edev, &result); 34040909f66SGal Pressman if (err) 34140909f66SGal Pressman goto err_out; 34240909f66SGal Pressman 34340909f66SGal Pressman pd->pdn = result.pdn; 34440909f66SGal Pressman resp.pdn = result.pdn; 34540909f66SGal Pressman 34640909f66SGal Pressman if (udata->outlen) { 34740909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 34840909f66SGal Pressman min(sizeof(resp), udata->outlen)); 34940909f66SGal Pressman if (err) { 35040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 35140909f66SGal Pressman "Failed to copy udata for alloc_pd\n"); 35240909f66SGal Pressman goto err_dealloc_pd; 35340909f66SGal Pressman } 35440909f66SGal Pressman } 35540909f66SGal Pressman 35640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Allocated pd[%d]\n", pd->pdn); 35740909f66SGal Pressman 35840909f66SGal Pressman return 0; 35940909f66SGal Pressman 36040909f66SGal Pressman err_dealloc_pd: 36140909f66SGal Pressman efa_pd_dealloc(dev, result.pdn); 36240909f66SGal Pressman err_out: 36340909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.alloc_pd_err); 36440909f66SGal Pressman return err; 36540909f66SGal Pressman } 36640909f66SGal Pressman 36740909f66SGal Pressman void efa_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 36840909f66SGal Pressman { 36940909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 37040909f66SGal Pressman struct efa_pd *pd = to_epd(ibpd); 37140909f66SGal Pressman 37240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Dealloc pd[%d]\n", pd->pdn); 37340909f66SGal Pressman efa_pd_dealloc(dev, pd->pdn); 37440909f66SGal Pressman } 37540909f66SGal Pressman 37640909f66SGal Pressman static int efa_destroy_qp_handle(struct efa_dev *dev, u32 qp_handle) 37740909f66SGal Pressman { 37840909f66SGal Pressman struct efa_com_destroy_qp_params params = { .qp_handle = qp_handle }; 37940909f66SGal Pressman 38040909f66SGal Pressman return efa_com_destroy_qp(&dev->edev, ¶ms); 38140909f66SGal Pressman } 38240909f66SGal Pressman 383e84d3c18SMichal Kalderon static void efa_qp_user_mmap_entries_remove(struct efa_ucontext *uctx, 384e84d3c18SMichal Kalderon struct efa_qp *qp) 385e84d3c18SMichal Kalderon { 386e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->rq_mmap_entry); 387e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->rq_db_mmap_entry); 388e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->llq_desc_mmap_entry); 389e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(qp->sq_db_mmap_entry); 390e84d3c18SMichal Kalderon } 391e84d3c18SMichal Kalderon 39240909f66SGal Pressman int efa_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 39340909f66SGal Pressman { 394e84d3c18SMichal Kalderon struct efa_ucontext *ucontext = rdma_udata_to_drv_context(udata, 395e84d3c18SMichal Kalderon struct efa_ucontext, ibucontext); 39640909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->pd->device); 39740909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 39840909f66SGal Pressman int err; 39940909f66SGal Pressman 40040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Destroy qp[%u]\n", ibqp->qp_num); 40140909f66SGal Pressman err = efa_destroy_qp_handle(dev, qp->qp_handle); 40240909f66SGal Pressman if (err) 40340909f66SGal Pressman return err; 40440909f66SGal Pressman 40540909f66SGal Pressman if (qp->rq_cpu_addr) { 40640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 40740909f66SGal Pressman "qp->cpu_addr[0x%p] freed: size[%lu], dma[%pad]\n", 40840909f66SGal Pressman qp->rq_cpu_addr, qp->rq_size, 40940909f66SGal Pressman &qp->rq_dma_addr); 41040909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, qp->rq_dma_addr, qp->rq_size, 41140909f66SGal Pressman DMA_TO_DEVICE); 41240909f66SGal Pressman } 41340909f66SGal Pressman 414e84d3c18SMichal Kalderon efa_qp_user_mmap_entries_remove(ucontext, qp); 41540909f66SGal Pressman kfree(qp); 41640909f66SGal Pressman return 0; 41740909f66SGal Pressman } 41840909f66SGal Pressman 419e84d3c18SMichal Kalderon static struct rdma_user_mmap_entry* 420e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(struct ib_ucontext *ucontext, 421e84d3c18SMichal Kalderon u64 address, size_t length, 422e84d3c18SMichal Kalderon u8 mmap_flag, u64 *offset) 423e84d3c18SMichal Kalderon { 424e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL); 425e84d3c18SMichal Kalderon int err; 426e84d3c18SMichal Kalderon 427e84d3c18SMichal Kalderon if (!entry) 428e84d3c18SMichal Kalderon return NULL; 429e84d3c18SMichal Kalderon 430e84d3c18SMichal Kalderon entry->address = address; 431e84d3c18SMichal Kalderon entry->mmap_flag = mmap_flag; 432e84d3c18SMichal Kalderon 433e84d3c18SMichal Kalderon err = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry, 434e84d3c18SMichal Kalderon length); 435e84d3c18SMichal Kalderon if (err) { 436e84d3c18SMichal Kalderon kfree(entry); 437e84d3c18SMichal Kalderon return NULL; 438e84d3c18SMichal Kalderon } 439e84d3c18SMichal Kalderon *offset = rdma_user_mmap_get_offset(&entry->rdma_entry); 440e84d3c18SMichal Kalderon 441e84d3c18SMichal Kalderon return &entry->rdma_entry; 442e84d3c18SMichal Kalderon } 443e84d3c18SMichal Kalderon 44440909f66SGal Pressman static int qp_mmap_entries_setup(struct efa_qp *qp, 44540909f66SGal Pressman struct efa_dev *dev, 44640909f66SGal Pressman struct efa_ucontext *ucontext, 44740909f66SGal Pressman struct efa_com_create_qp_params *params, 44840909f66SGal Pressman struct efa_ibv_create_qp_resp *resp) 44940909f66SGal Pressman { 450e84d3c18SMichal Kalderon size_t length; 451e84d3c18SMichal Kalderon u64 address; 452e84d3c18SMichal Kalderon 453e84d3c18SMichal Kalderon address = dev->db_bar_addr + resp->sq_db_offset; 454e84d3c18SMichal Kalderon qp->sq_db_mmap_entry = 455e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 456e84d3c18SMichal Kalderon address, 457e84d3c18SMichal Kalderon PAGE_SIZE, EFA_MMAP_IO_NC, 458e84d3c18SMichal Kalderon &resp->sq_db_mmap_key); 459e84d3c18SMichal Kalderon if (!qp->sq_db_mmap_entry) 46040909f66SGal Pressman return -ENOMEM; 46140909f66SGal Pressman 46240909f66SGal Pressman resp->sq_db_offset &= ~PAGE_MASK; 46340909f66SGal Pressman 464e84d3c18SMichal Kalderon address = dev->mem_bar_addr + resp->llq_desc_offset; 465e84d3c18SMichal Kalderon length = PAGE_ALIGN(params->sq_ring_size_in_bytes + 466e84d3c18SMichal Kalderon (resp->llq_desc_offset & ~PAGE_MASK)); 467e84d3c18SMichal Kalderon 468e84d3c18SMichal Kalderon qp->llq_desc_mmap_entry = 469e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 470e84d3c18SMichal Kalderon address, length, 471e84d3c18SMichal Kalderon EFA_MMAP_IO_WC, 472e84d3c18SMichal Kalderon &resp->llq_desc_mmap_key); 473e84d3c18SMichal Kalderon if (!qp->llq_desc_mmap_entry) 474e84d3c18SMichal Kalderon goto err_remove_mmap; 47540909f66SGal Pressman 47640909f66SGal Pressman resp->llq_desc_offset &= ~PAGE_MASK; 47740909f66SGal Pressman 47840909f66SGal Pressman if (qp->rq_size) { 479e84d3c18SMichal Kalderon address = dev->db_bar_addr + resp->rq_db_offset; 480e84d3c18SMichal Kalderon 481e84d3c18SMichal Kalderon qp->rq_db_mmap_entry = 482e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 483e84d3c18SMichal Kalderon address, PAGE_SIZE, 484e84d3c18SMichal Kalderon EFA_MMAP_IO_NC, 485e84d3c18SMichal Kalderon &resp->rq_db_mmap_key); 486e84d3c18SMichal Kalderon if (!qp->rq_db_mmap_entry) 487e84d3c18SMichal Kalderon goto err_remove_mmap; 48840909f66SGal Pressman 48940909f66SGal Pressman resp->rq_db_offset &= ~PAGE_MASK; 49040909f66SGal Pressman 491e84d3c18SMichal Kalderon address = virt_to_phys(qp->rq_cpu_addr); 492e84d3c18SMichal Kalderon qp->rq_mmap_entry = 493e84d3c18SMichal Kalderon efa_user_mmap_entry_insert(&ucontext->ibucontext, 494e84d3c18SMichal Kalderon address, qp->rq_size, 495e84d3c18SMichal Kalderon EFA_MMAP_DMA_PAGE, 496e84d3c18SMichal Kalderon &resp->rq_mmap_key); 497e84d3c18SMichal Kalderon if (!qp->rq_mmap_entry) 498e84d3c18SMichal Kalderon goto err_remove_mmap; 49940909f66SGal Pressman 50040909f66SGal Pressman resp->rq_mmap_size = qp->rq_size; 50140909f66SGal Pressman } 50240909f66SGal Pressman 50340909f66SGal Pressman return 0; 504e84d3c18SMichal Kalderon 505e84d3c18SMichal Kalderon err_remove_mmap: 506e84d3c18SMichal Kalderon efa_qp_user_mmap_entries_remove(ucontext, qp); 507e84d3c18SMichal Kalderon 508e84d3c18SMichal Kalderon return -ENOMEM; 50940909f66SGal Pressman } 51040909f66SGal Pressman 51140909f66SGal Pressman static int efa_qp_validate_cap(struct efa_dev *dev, 51240909f66SGal Pressman struct ib_qp_init_attr *init_attr) 51340909f66SGal Pressman { 51440909f66SGal Pressman if (init_attr->cap.max_send_wr > dev->dev_attr.max_sq_depth) { 51540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 51640909f66SGal Pressman "qp: requested send wr[%u] exceeds the max[%u]\n", 51740909f66SGal Pressman init_attr->cap.max_send_wr, 51840909f66SGal Pressman dev->dev_attr.max_sq_depth); 51940909f66SGal Pressman return -EINVAL; 52040909f66SGal Pressman } 52140909f66SGal Pressman if (init_attr->cap.max_recv_wr > dev->dev_attr.max_rq_depth) { 52240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 52340909f66SGal Pressman "qp: requested receive wr[%u] exceeds the max[%u]\n", 52440909f66SGal Pressman init_attr->cap.max_recv_wr, 52540909f66SGal Pressman dev->dev_attr.max_rq_depth); 52640909f66SGal Pressman return -EINVAL; 52740909f66SGal Pressman } 52840909f66SGal Pressman if (init_attr->cap.max_send_sge > dev->dev_attr.max_sq_sge) { 52940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 53040909f66SGal Pressman "qp: requested sge send[%u] exceeds the max[%u]\n", 53140909f66SGal Pressman init_attr->cap.max_send_sge, dev->dev_attr.max_sq_sge); 53240909f66SGal Pressman return -EINVAL; 53340909f66SGal Pressman } 53440909f66SGal Pressman if (init_attr->cap.max_recv_sge > dev->dev_attr.max_rq_sge) { 53540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 53640909f66SGal Pressman "qp: requested sge recv[%u] exceeds the max[%u]\n", 53740909f66SGal Pressman init_attr->cap.max_recv_sge, dev->dev_attr.max_rq_sge); 53840909f66SGal Pressman return -EINVAL; 53940909f66SGal Pressman } 54040909f66SGal Pressman if (init_attr->cap.max_inline_data > dev->dev_attr.inline_buf_size) { 54140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 54240909f66SGal Pressman "qp: requested inline data[%u] exceeds the max[%u]\n", 54340909f66SGal Pressman init_attr->cap.max_inline_data, 54440909f66SGal Pressman dev->dev_attr.inline_buf_size); 54540909f66SGal Pressman return -EINVAL; 54640909f66SGal Pressman } 54740909f66SGal Pressman 54840909f66SGal Pressman return 0; 54940909f66SGal Pressman } 55040909f66SGal Pressman 55140909f66SGal Pressman static int efa_qp_validate_attr(struct efa_dev *dev, 55240909f66SGal Pressman struct ib_qp_init_attr *init_attr) 55340909f66SGal Pressman { 55440909f66SGal Pressman if (init_attr->qp_type != IB_QPT_DRIVER && 55540909f66SGal Pressman init_attr->qp_type != IB_QPT_UD) { 55640909f66SGal Pressman ibdev_dbg(&dev->ibdev, 55740909f66SGal Pressman "Unsupported qp type %d\n", init_attr->qp_type); 55840909f66SGal Pressman return -EOPNOTSUPP; 55940909f66SGal Pressman } 56040909f66SGal Pressman 56140909f66SGal Pressman if (init_attr->srq) { 56240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "SRQ is not supported\n"); 56340909f66SGal Pressman return -EOPNOTSUPP; 56440909f66SGal Pressman } 56540909f66SGal Pressman 56640909f66SGal Pressman if (init_attr->create_flags) { 56740909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Unsupported create flags\n"); 56840909f66SGal Pressman return -EOPNOTSUPP; 56940909f66SGal Pressman } 57040909f66SGal Pressman 57140909f66SGal Pressman return 0; 57240909f66SGal Pressman } 57340909f66SGal Pressman 57440909f66SGal Pressman struct ib_qp *efa_create_qp(struct ib_pd *ibpd, 57540909f66SGal Pressman struct ib_qp_init_attr *init_attr, 57640909f66SGal Pressman struct ib_udata *udata) 57740909f66SGal Pressman { 57840909f66SGal Pressman struct efa_com_create_qp_params create_qp_params = {}; 57940909f66SGal Pressman struct efa_com_create_qp_result create_qp_resp; 58040909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 58140909f66SGal Pressman struct efa_ibv_create_qp_resp resp = {}; 58240909f66SGal Pressman struct efa_ibv_create_qp cmd = {}; 58340909f66SGal Pressman struct efa_ucontext *ucontext; 58440909f66SGal Pressman struct efa_qp *qp; 58540909f66SGal Pressman int err; 58640909f66SGal Pressman 58740909f66SGal Pressman ucontext = rdma_udata_to_drv_context(udata, struct efa_ucontext, 58840909f66SGal Pressman ibucontext); 58940909f66SGal Pressman 59040909f66SGal Pressman err = efa_qp_validate_cap(dev, init_attr); 59140909f66SGal Pressman if (err) 59240909f66SGal Pressman goto err_out; 59340909f66SGal Pressman 59440909f66SGal Pressman err = efa_qp_validate_attr(dev, init_attr); 59540909f66SGal Pressman if (err) 59640909f66SGal Pressman goto err_out; 59740909f66SGal Pressman 59840909f66SGal Pressman if (!field_avail(cmd, driver_qp_type, udata->inlen)) { 59940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 60040909f66SGal Pressman "Incompatible ABI params, no input udata\n"); 60140909f66SGal Pressman err = -EINVAL; 60240909f66SGal Pressman goto err_out; 60340909f66SGal Pressman } 60440909f66SGal Pressman 60540909f66SGal Pressman if (udata->inlen > sizeof(cmd) && 60640909f66SGal Pressman !ib_is_udata_cleared(udata, sizeof(cmd), 60740909f66SGal Pressman udata->inlen - sizeof(cmd))) { 60840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 60940909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 61040909f66SGal Pressman err = -EINVAL; 61140909f66SGal Pressman goto err_out; 61240909f66SGal Pressman } 61340909f66SGal Pressman 61440909f66SGal Pressman err = ib_copy_from_udata(&cmd, udata, 61540909f66SGal Pressman min(sizeof(cmd), udata->inlen)); 61640909f66SGal Pressman if (err) { 61740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 61840909f66SGal Pressman "Cannot copy udata for create_qp\n"); 61940909f66SGal Pressman goto err_out; 62040909f66SGal Pressman } 62140909f66SGal Pressman 62240909f66SGal Pressman if (cmd.comp_mask) { 62340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 62440909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 62540909f66SGal Pressman err = -EINVAL; 62640909f66SGal Pressman goto err_out; 62740909f66SGal Pressman } 62840909f66SGal Pressman 62940909f66SGal Pressman qp = kzalloc(sizeof(*qp), GFP_KERNEL); 63040909f66SGal Pressman if (!qp) { 63140909f66SGal Pressman err = -ENOMEM; 63240909f66SGal Pressman goto err_out; 63340909f66SGal Pressman } 63440909f66SGal Pressman 63540909f66SGal Pressman create_qp_params.uarn = ucontext->uarn; 63640909f66SGal Pressman create_qp_params.pd = to_epd(ibpd)->pdn; 63740909f66SGal Pressman 63840909f66SGal Pressman if (init_attr->qp_type == IB_QPT_UD) { 63940909f66SGal Pressman create_qp_params.qp_type = EFA_ADMIN_QP_TYPE_UD; 64040909f66SGal Pressman } else if (cmd.driver_qp_type == EFA_QP_DRIVER_TYPE_SRD) { 64140909f66SGal Pressman create_qp_params.qp_type = EFA_ADMIN_QP_TYPE_SRD; 64240909f66SGal Pressman } else { 64340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 64440909f66SGal Pressman "Unsupported qp type %d driver qp type %d\n", 64540909f66SGal Pressman init_attr->qp_type, cmd.driver_qp_type); 64640909f66SGal Pressman err = -EOPNOTSUPP; 64740909f66SGal Pressman goto err_free_qp; 64840909f66SGal Pressman } 64940909f66SGal Pressman 65040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Create QP: qp type %d driver qp type %#x\n", 65140909f66SGal Pressman init_attr->qp_type, cmd.driver_qp_type); 65240909f66SGal Pressman create_qp_params.send_cq_idx = to_ecq(init_attr->send_cq)->cq_idx; 65340909f66SGal Pressman create_qp_params.recv_cq_idx = to_ecq(init_attr->recv_cq)->cq_idx; 65440909f66SGal Pressman create_qp_params.sq_depth = init_attr->cap.max_send_wr; 65540909f66SGal Pressman create_qp_params.sq_ring_size_in_bytes = cmd.sq_ring_size; 65640909f66SGal Pressman 65740909f66SGal Pressman create_qp_params.rq_depth = init_attr->cap.max_recv_wr; 65840909f66SGal Pressman create_qp_params.rq_ring_size_in_bytes = cmd.rq_ring_size; 65940909f66SGal Pressman qp->rq_size = PAGE_ALIGN(create_qp_params.rq_ring_size_in_bytes); 66040909f66SGal Pressman if (qp->rq_size) { 66140909f66SGal Pressman qp->rq_cpu_addr = efa_zalloc_mapped(dev, &qp->rq_dma_addr, 66240909f66SGal Pressman qp->rq_size, DMA_TO_DEVICE); 66340909f66SGal Pressman if (!qp->rq_cpu_addr) { 66440909f66SGal Pressman err = -ENOMEM; 66540909f66SGal Pressman goto err_free_qp; 66640909f66SGal Pressman } 66740909f66SGal Pressman 66840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 66940909f66SGal Pressman "qp->cpu_addr[0x%p] allocated: size[%lu], dma[%pad]\n", 67040909f66SGal Pressman qp->rq_cpu_addr, qp->rq_size, &qp->rq_dma_addr); 67140909f66SGal Pressman create_qp_params.rq_base_addr = qp->rq_dma_addr; 67240909f66SGal Pressman } 67340909f66SGal Pressman 67440909f66SGal Pressman err = efa_com_create_qp(&dev->edev, &create_qp_params, 67540909f66SGal Pressman &create_qp_resp); 67640909f66SGal Pressman if (err) 67740909f66SGal Pressman goto err_free_mapped; 67840909f66SGal Pressman 67940909f66SGal Pressman resp.sq_db_offset = create_qp_resp.sq_db_offset; 68040909f66SGal Pressman resp.rq_db_offset = create_qp_resp.rq_db_offset; 68140909f66SGal Pressman resp.llq_desc_offset = create_qp_resp.llq_descriptors_offset; 68240909f66SGal Pressman resp.send_sub_cq_idx = create_qp_resp.send_sub_cq_idx; 68340909f66SGal Pressman resp.recv_sub_cq_idx = create_qp_resp.recv_sub_cq_idx; 68440909f66SGal Pressman 68540909f66SGal Pressman err = qp_mmap_entries_setup(qp, dev, ucontext, &create_qp_params, 68640909f66SGal Pressman &resp); 68740909f66SGal Pressman if (err) 68840909f66SGal Pressman goto err_destroy_qp; 68940909f66SGal Pressman 69040909f66SGal Pressman qp->qp_handle = create_qp_resp.qp_handle; 69140909f66SGal Pressman qp->ibqp.qp_num = create_qp_resp.qp_num; 69240909f66SGal Pressman qp->ibqp.qp_type = init_attr->qp_type; 69340909f66SGal Pressman qp->max_send_wr = init_attr->cap.max_send_wr; 69440909f66SGal Pressman qp->max_recv_wr = init_attr->cap.max_recv_wr; 69540909f66SGal Pressman qp->max_send_sge = init_attr->cap.max_send_sge; 69640909f66SGal Pressman qp->max_recv_sge = init_attr->cap.max_recv_sge; 69740909f66SGal Pressman qp->max_inline_data = init_attr->cap.max_inline_data; 69840909f66SGal Pressman 69940909f66SGal Pressman if (udata->outlen) { 70040909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 70140909f66SGal Pressman min(sizeof(resp), udata->outlen)); 70240909f66SGal Pressman if (err) { 70340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 70440909f66SGal Pressman "Failed to copy udata for qp[%u]\n", 70540909f66SGal Pressman create_qp_resp.qp_num); 706e84d3c18SMichal Kalderon goto err_remove_mmap_entries; 70740909f66SGal Pressman } 70840909f66SGal Pressman } 70940909f66SGal Pressman 71040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Created qp[%d]\n", qp->ibqp.qp_num); 71140909f66SGal Pressman 71240909f66SGal Pressman return &qp->ibqp; 71340909f66SGal Pressman 714e84d3c18SMichal Kalderon err_remove_mmap_entries: 715e84d3c18SMichal Kalderon efa_qp_user_mmap_entries_remove(ucontext, qp); 71640909f66SGal Pressman err_destroy_qp: 71740909f66SGal Pressman efa_destroy_qp_handle(dev, create_qp_resp.qp_handle); 71840909f66SGal Pressman err_free_mapped: 71940909f66SGal Pressman if (qp->rq_size) { 72040909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, qp->rq_dma_addr, qp->rq_size, 72140909f66SGal Pressman DMA_TO_DEVICE); 722e84d3c18SMichal Kalderon 723e84d3c18SMichal Kalderon if (!qp->rq_mmap_entry) 72440909f66SGal Pressman free_pages_exact(qp->rq_cpu_addr, qp->rq_size); 72540909f66SGal Pressman } 72640909f66SGal Pressman err_free_qp: 72740909f66SGal Pressman kfree(qp); 72840909f66SGal Pressman err_out: 72940909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.create_qp_err); 73040909f66SGal Pressman return ERR_PTR(err); 73140909f66SGal Pressman } 73240909f66SGal Pressman 73340909f66SGal Pressman static int efa_modify_qp_validate(struct efa_dev *dev, struct efa_qp *qp, 73440909f66SGal Pressman struct ib_qp_attr *qp_attr, int qp_attr_mask, 73540909f66SGal Pressman enum ib_qp_state cur_state, 73640909f66SGal Pressman enum ib_qp_state new_state) 73740909f66SGal Pressman { 73840909f66SGal Pressman #define EFA_MODIFY_QP_SUPP_MASK \ 73940909f66SGal Pressman (IB_QP_STATE | IB_QP_CUR_STATE | IB_QP_EN_SQD_ASYNC_NOTIFY | \ 74040909f66SGal Pressman IB_QP_PKEY_INDEX | IB_QP_PORT | IB_QP_QKEY | IB_QP_SQ_PSN) 74140909f66SGal Pressman 74240909f66SGal Pressman if (qp_attr_mask & ~EFA_MODIFY_QP_SUPP_MASK) { 74340909f66SGal Pressman ibdev_dbg(&dev->ibdev, 74440909f66SGal Pressman "Unsupported qp_attr_mask[%#x] supported[%#x]\n", 74540909f66SGal Pressman qp_attr_mask, EFA_MODIFY_QP_SUPP_MASK); 74640909f66SGal Pressman return -EOPNOTSUPP; 74740909f66SGal Pressman } 74840909f66SGal Pressman 74940909f66SGal Pressman if (!ib_modify_qp_is_ok(cur_state, new_state, IB_QPT_UD, 75040909f66SGal Pressman qp_attr_mask)) { 75140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Invalid modify QP parameters\n"); 75240909f66SGal Pressman return -EINVAL; 75340909f66SGal Pressman } 75440909f66SGal Pressman 75540909f66SGal Pressman if ((qp_attr_mask & IB_QP_PORT) && qp_attr->port_num != 1) { 75640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Can't change port num\n"); 75740909f66SGal Pressman return -EOPNOTSUPP; 75840909f66SGal Pressman } 75940909f66SGal Pressman 76040909f66SGal Pressman if ((qp_attr_mask & IB_QP_PKEY_INDEX) && qp_attr->pkey_index) { 76140909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Can't change pkey index\n"); 76240909f66SGal Pressman return -EOPNOTSUPP; 76340909f66SGal Pressman } 76440909f66SGal Pressman 76540909f66SGal Pressman return 0; 76640909f66SGal Pressman } 76740909f66SGal Pressman 76840909f66SGal Pressman int efa_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, 76940909f66SGal Pressman int qp_attr_mask, struct ib_udata *udata) 77040909f66SGal Pressman { 77140909f66SGal Pressman struct efa_dev *dev = to_edev(ibqp->device); 77240909f66SGal Pressman struct efa_com_modify_qp_params params = {}; 77340909f66SGal Pressman struct efa_qp *qp = to_eqp(ibqp); 77440909f66SGal Pressman enum ib_qp_state cur_state; 77540909f66SGal Pressman enum ib_qp_state new_state; 77640909f66SGal Pressman int err; 77740909f66SGal Pressman 77840909f66SGal Pressman if (udata->inlen && 77940909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 78040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 78140909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 78240909f66SGal Pressman return -EINVAL; 78340909f66SGal Pressman } 78440909f66SGal Pressman 78540909f66SGal Pressman cur_state = qp_attr_mask & IB_QP_CUR_STATE ? qp_attr->cur_qp_state : 78640909f66SGal Pressman qp->state; 78740909f66SGal Pressman new_state = qp_attr_mask & IB_QP_STATE ? qp_attr->qp_state : cur_state; 78840909f66SGal Pressman 78940909f66SGal Pressman err = efa_modify_qp_validate(dev, qp, qp_attr, qp_attr_mask, cur_state, 79040909f66SGal Pressman new_state); 79140909f66SGal Pressman if (err) 79240909f66SGal Pressman return err; 79340909f66SGal Pressman 79440909f66SGal Pressman params.qp_handle = qp->qp_handle; 79540909f66SGal Pressman 79640909f66SGal Pressman if (qp_attr_mask & IB_QP_STATE) { 79740909f66SGal Pressman params.modify_mask |= BIT(EFA_ADMIN_QP_STATE_BIT) | 79840909f66SGal Pressman BIT(EFA_ADMIN_CUR_QP_STATE_BIT); 79940909f66SGal Pressman params.cur_qp_state = qp_attr->cur_qp_state; 80040909f66SGal Pressman params.qp_state = qp_attr->qp_state; 80140909f66SGal Pressman } 80240909f66SGal Pressman 80340909f66SGal Pressman if (qp_attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) { 80440909f66SGal Pressman params.modify_mask |= 80540909f66SGal Pressman BIT(EFA_ADMIN_SQ_DRAINED_ASYNC_NOTIFY_BIT); 80640909f66SGal Pressman params.sq_drained_async_notify = qp_attr->en_sqd_async_notify; 80740909f66SGal Pressman } 80840909f66SGal Pressman 80940909f66SGal Pressman if (qp_attr_mask & IB_QP_QKEY) { 81040909f66SGal Pressman params.modify_mask |= BIT(EFA_ADMIN_QKEY_BIT); 81140909f66SGal Pressman params.qkey = qp_attr->qkey; 81240909f66SGal Pressman } 81340909f66SGal Pressman 81440909f66SGal Pressman if (qp_attr_mask & IB_QP_SQ_PSN) { 81540909f66SGal Pressman params.modify_mask |= BIT(EFA_ADMIN_SQ_PSN_BIT); 81640909f66SGal Pressman params.sq_psn = qp_attr->sq_psn; 81740909f66SGal Pressman } 81840909f66SGal Pressman 81940909f66SGal Pressman err = efa_com_modify_qp(&dev->edev, ¶ms); 82040909f66SGal Pressman if (err) 82140909f66SGal Pressman return err; 82240909f66SGal Pressman 82340909f66SGal Pressman qp->state = new_state; 82440909f66SGal Pressman 82540909f66SGal Pressman return 0; 82640909f66SGal Pressman } 82740909f66SGal Pressman 82840909f66SGal Pressman static int efa_destroy_cq_idx(struct efa_dev *dev, int cq_idx) 82940909f66SGal Pressman { 83040909f66SGal Pressman struct efa_com_destroy_cq_params params = { .cq_idx = cq_idx }; 83140909f66SGal Pressman 83240909f66SGal Pressman return efa_com_destroy_cq(&dev->edev, ¶ms); 83340909f66SGal Pressman } 83440909f66SGal Pressman 835a52c8e24SLeon Romanovsky void efa_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 83640909f66SGal Pressman { 83740909f66SGal Pressman struct efa_dev *dev = to_edev(ibcq->device); 83840909f66SGal Pressman struct efa_cq *cq = to_ecq(ibcq); 83940909f66SGal Pressman 84040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 84140909f66SGal Pressman "Destroy cq[%d] virt[0x%p] freed: size[%lu], dma[%pad]\n", 84240909f66SGal Pressman cq->cq_idx, cq->cpu_addr, cq->size, &cq->dma_addr); 84340909f66SGal Pressman 844a52c8e24SLeon Romanovsky efa_destroy_cq_idx(dev, cq->cq_idx); 84540909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, cq->dma_addr, cq->size, 84640909f66SGal Pressman DMA_FROM_DEVICE); 847e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(cq->mmap_entry); 84840909f66SGal Pressman } 84940909f66SGal Pressman 85040909f66SGal Pressman static int cq_mmap_entries_setup(struct efa_dev *dev, struct efa_cq *cq, 85140909f66SGal Pressman struct efa_ibv_create_cq_resp *resp) 85240909f66SGal Pressman { 85340909f66SGal Pressman resp->q_mmap_size = cq->size; 854e84d3c18SMichal Kalderon cq->mmap_entry = efa_user_mmap_entry_insert(&cq->ucontext->ibucontext, 85540909f66SGal Pressman virt_to_phys(cq->cpu_addr), 856e84d3c18SMichal Kalderon cq->size, EFA_MMAP_DMA_PAGE, 857e84d3c18SMichal Kalderon &resp->q_mmap_key); 858e84d3c18SMichal Kalderon if (!cq->mmap_entry) 85940909f66SGal Pressman return -ENOMEM; 86040909f66SGal Pressman 86140909f66SGal Pressman return 0; 86240909f66SGal Pressman } 86340909f66SGal Pressman 864e39afe3dSLeon Romanovsky int efa_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 86540909f66SGal Pressman struct ib_udata *udata) 86640909f66SGal Pressman { 867e39afe3dSLeon Romanovsky struct efa_ucontext *ucontext = rdma_udata_to_drv_context( 868e39afe3dSLeon Romanovsky udata, struct efa_ucontext, ibucontext); 86940909f66SGal Pressman struct efa_ibv_create_cq_resp resp = {}; 87040909f66SGal Pressman struct efa_com_create_cq_params params; 87140909f66SGal Pressman struct efa_com_create_cq_result result; 872e39afe3dSLeon Romanovsky struct ib_device *ibdev = ibcq->device; 87340909f66SGal Pressman struct efa_dev *dev = to_edev(ibdev); 87440909f66SGal Pressman struct efa_ibv_create_cq cmd = {}; 875e39afe3dSLeon Romanovsky struct efa_cq *cq = to_ecq(ibcq); 876e39afe3dSLeon Romanovsky int entries = attr->cqe; 87740909f66SGal Pressman int err; 87840909f66SGal Pressman 87940909f66SGal Pressman ibdev_dbg(ibdev, "create_cq entries %d\n", entries); 88040909f66SGal Pressman 88140909f66SGal Pressman if (entries < 1 || entries > dev->dev_attr.max_cq_depth) { 88240909f66SGal Pressman ibdev_dbg(ibdev, 88340909f66SGal Pressman "cq: requested entries[%u] non-positive or greater than max[%u]\n", 88440909f66SGal Pressman entries, dev->dev_attr.max_cq_depth); 88540909f66SGal Pressman err = -EINVAL; 88640909f66SGal Pressman goto err_out; 88740909f66SGal Pressman } 88840909f66SGal Pressman 88940909f66SGal Pressman if (!field_avail(cmd, num_sub_cqs, udata->inlen)) { 89040909f66SGal Pressman ibdev_dbg(ibdev, 89140909f66SGal Pressman "Incompatible ABI params, no input udata\n"); 89240909f66SGal Pressman err = -EINVAL; 89340909f66SGal Pressman goto err_out; 89440909f66SGal Pressman } 89540909f66SGal Pressman 89640909f66SGal Pressman if (udata->inlen > sizeof(cmd) && 89740909f66SGal Pressman !ib_is_udata_cleared(udata, sizeof(cmd), 89840909f66SGal Pressman udata->inlen - sizeof(cmd))) { 89940909f66SGal Pressman ibdev_dbg(ibdev, 90040909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 90140909f66SGal Pressman err = -EINVAL; 90240909f66SGal Pressman goto err_out; 90340909f66SGal Pressman } 90440909f66SGal Pressman 90540909f66SGal Pressman err = ib_copy_from_udata(&cmd, udata, 90640909f66SGal Pressman min(sizeof(cmd), udata->inlen)); 90740909f66SGal Pressman if (err) { 90840909f66SGal Pressman ibdev_dbg(ibdev, "Cannot copy udata for create_cq\n"); 90940909f66SGal Pressman goto err_out; 91040909f66SGal Pressman } 91140909f66SGal Pressman 91240909f66SGal Pressman if (cmd.comp_mask || !is_reserved_cleared(cmd.reserved_50)) { 91340909f66SGal Pressman ibdev_dbg(ibdev, 91440909f66SGal Pressman "Incompatible ABI params, unknown fields in udata\n"); 91540909f66SGal Pressman err = -EINVAL; 91640909f66SGal Pressman goto err_out; 91740909f66SGal Pressman } 91840909f66SGal Pressman 91940909f66SGal Pressman if (!cmd.cq_entry_size) { 92040909f66SGal Pressman ibdev_dbg(ibdev, 92140909f66SGal Pressman "Invalid entry size [%u]\n", cmd.cq_entry_size); 92240909f66SGal Pressman err = -EINVAL; 92340909f66SGal Pressman goto err_out; 92440909f66SGal Pressman } 92540909f66SGal Pressman 92640909f66SGal Pressman if (cmd.num_sub_cqs != dev->dev_attr.sub_cqs_per_cq) { 92740909f66SGal Pressman ibdev_dbg(ibdev, 92840909f66SGal Pressman "Invalid number of sub cqs[%u] expected[%u]\n", 92940909f66SGal Pressman cmd.num_sub_cqs, dev->dev_attr.sub_cqs_per_cq); 93040909f66SGal Pressman err = -EINVAL; 93140909f66SGal Pressman goto err_out; 93240909f66SGal Pressman } 93340909f66SGal Pressman 934e39afe3dSLeon Romanovsky cq->ucontext = ucontext; 93540909f66SGal Pressman cq->size = PAGE_ALIGN(cmd.cq_entry_size * entries * cmd.num_sub_cqs); 93640909f66SGal Pressman cq->cpu_addr = efa_zalloc_mapped(dev, &cq->dma_addr, cq->size, 93740909f66SGal Pressman DMA_FROM_DEVICE); 93840909f66SGal Pressman if (!cq->cpu_addr) { 93940909f66SGal Pressman err = -ENOMEM; 940e39afe3dSLeon Romanovsky goto err_out; 94140909f66SGal Pressman } 94240909f66SGal Pressman 94340909f66SGal Pressman params.uarn = cq->ucontext->uarn; 94440909f66SGal Pressman params.cq_depth = entries; 94540909f66SGal Pressman params.dma_addr = cq->dma_addr; 94640909f66SGal Pressman params.entry_size_in_bytes = cmd.cq_entry_size; 94740909f66SGal Pressman params.num_sub_cqs = cmd.num_sub_cqs; 94840909f66SGal Pressman err = efa_com_create_cq(&dev->edev, ¶ms, &result); 94940909f66SGal Pressman if (err) 95040909f66SGal Pressman goto err_free_mapped; 95140909f66SGal Pressman 95240909f66SGal Pressman resp.cq_idx = result.cq_idx; 95340909f66SGal Pressman cq->cq_idx = result.cq_idx; 95440909f66SGal Pressman cq->ibcq.cqe = result.actual_depth; 95540909f66SGal Pressman WARN_ON_ONCE(entries != result.actual_depth); 95640909f66SGal Pressman 95740909f66SGal Pressman err = cq_mmap_entries_setup(dev, cq, &resp); 95840909f66SGal Pressman if (err) { 959e39afe3dSLeon Romanovsky ibdev_dbg(ibdev, "Could not setup cq[%u] mmap entries\n", 960e39afe3dSLeon Romanovsky cq->cq_idx); 96140909f66SGal Pressman goto err_destroy_cq; 96240909f66SGal Pressman } 96340909f66SGal Pressman 96440909f66SGal Pressman if (udata->outlen) { 96540909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 96640909f66SGal Pressman min(sizeof(resp), udata->outlen)); 96740909f66SGal Pressman if (err) { 96840909f66SGal Pressman ibdev_dbg(ibdev, 96940909f66SGal Pressman "Failed to copy udata for create_cq\n"); 970e84d3c18SMichal Kalderon goto err_remove_mmap; 97140909f66SGal Pressman } 97240909f66SGal Pressman } 97340909f66SGal Pressman 974e39afe3dSLeon Romanovsky ibdev_dbg(ibdev, "Created cq[%d], cq depth[%u]. dma[%pad] virt[0x%p]\n", 97540909f66SGal Pressman cq->cq_idx, result.actual_depth, &cq->dma_addr, cq->cpu_addr); 97640909f66SGal Pressman 977e39afe3dSLeon Romanovsky return 0; 97840909f66SGal Pressman 979e84d3c18SMichal Kalderon err_remove_mmap: 980e84d3c18SMichal Kalderon rdma_user_mmap_entry_remove(cq->mmap_entry); 98140909f66SGal Pressman err_destroy_cq: 98240909f66SGal Pressman efa_destroy_cq_idx(dev, cq->cq_idx); 98340909f66SGal Pressman err_free_mapped: 98440909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, cq->dma_addr, cq->size, 98540909f66SGal Pressman DMA_FROM_DEVICE); 986e84d3c18SMichal Kalderon if (!cq->mmap_entry) 98740909f66SGal Pressman free_pages_exact(cq->cpu_addr, cq->size); 988e84d3c18SMichal Kalderon 98940909f66SGal Pressman err_out: 99040909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.create_cq_err); 991e39afe3dSLeon Romanovsky return err; 99240909f66SGal Pressman } 99340909f66SGal Pressman 99440909f66SGal Pressman static int umem_to_page_list(struct efa_dev *dev, 99540909f66SGal Pressman struct ib_umem *umem, 99640909f66SGal Pressman u64 *page_list, 99740909f66SGal Pressman u32 hp_cnt, 99840909f66SGal Pressman u8 hp_shift) 99940909f66SGal Pressman { 100040909f66SGal Pressman u32 pages_in_hp = BIT(hp_shift - PAGE_SHIFT); 100140ddb3f0SGal Pressman struct ib_block_iter biter; 100240909f66SGal Pressman unsigned int hp_idx = 0; 100340909f66SGal Pressman 100440909f66SGal Pressman ibdev_dbg(&dev->ibdev, "hp_cnt[%u], pages_in_hp[%u]\n", 100540909f66SGal Pressman hp_cnt, pages_in_hp); 100640909f66SGal Pressman 100740ddb3f0SGal Pressman rdma_for_each_block(umem->sg_head.sgl, &biter, umem->nmap, 100840ddb3f0SGal Pressman BIT(hp_shift)) 100940ddb3f0SGal Pressman page_list[hp_idx++] = rdma_block_iter_dma_address(&biter); 101040909f66SGal Pressman 101140909f66SGal Pressman return 0; 101240909f66SGal Pressman } 101340909f66SGal Pressman 101440909f66SGal Pressman static struct scatterlist *efa_vmalloc_buf_to_sg(u64 *buf, int page_cnt) 101540909f66SGal Pressman { 101640909f66SGal Pressman struct scatterlist *sglist; 101740909f66SGal Pressman struct page *pg; 101840909f66SGal Pressman int i; 101940909f66SGal Pressman 102040909f66SGal Pressman sglist = kcalloc(page_cnt, sizeof(*sglist), GFP_KERNEL); 102140909f66SGal Pressman if (!sglist) 102240909f66SGal Pressman return NULL; 102340909f66SGal Pressman sg_init_table(sglist, page_cnt); 102440909f66SGal Pressman for (i = 0; i < page_cnt; i++) { 102540909f66SGal Pressman pg = vmalloc_to_page(buf); 102640909f66SGal Pressman if (!pg) 102740909f66SGal Pressman goto err; 102840909f66SGal Pressman sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); 102940909f66SGal Pressman buf += PAGE_SIZE / sizeof(*buf); 103040909f66SGal Pressman } 103140909f66SGal Pressman return sglist; 103240909f66SGal Pressman 103340909f66SGal Pressman err: 103440909f66SGal Pressman kfree(sglist); 103540909f66SGal Pressman return NULL; 103640909f66SGal Pressman } 103740909f66SGal Pressman 103840909f66SGal Pressman /* 103940909f66SGal Pressman * create a chunk list of physical pages dma addresses from the supplied 104040909f66SGal Pressman * scatter gather list 104140909f66SGal Pressman */ 104240909f66SGal Pressman static int pbl_chunk_list_create(struct efa_dev *dev, struct pbl_context *pbl) 104340909f66SGal Pressman { 104440909f66SGal Pressman struct pbl_chunk_list *chunk_list = &pbl->phys.indirect.chunk_list; 104540909f66SGal Pressman int page_cnt = pbl->phys.indirect.pbl_buf_size_in_pages; 104640909f66SGal Pressman struct scatterlist *pages_sgl = pbl->phys.indirect.sgl; 10474d50e084SGal Pressman unsigned int chunk_list_size, chunk_idx, payload_idx; 104840909f66SGal Pressman int sg_dma_cnt = pbl->phys.indirect.sg_dma_cnt; 104940909f66SGal Pressman struct efa_com_ctrl_buff_info *ctrl_buf; 105040909f66SGal Pressman u64 *cur_chunk_buf, *prev_chunk_buf; 10514d50e084SGal Pressman struct ib_block_iter biter; 105240909f66SGal Pressman dma_addr_t dma_addr; 105340909f66SGal Pressman int i; 105440909f66SGal Pressman 105540909f66SGal Pressman /* allocate a chunk list that consists of 4KB chunks */ 105640909f66SGal Pressman chunk_list_size = DIV_ROUND_UP(page_cnt, EFA_PTRS_PER_CHUNK); 105740909f66SGal Pressman 105840909f66SGal Pressman chunk_list->size = chunk_list_size; 105940909f66SGal Pressman chunk_list->chunks = kcalloc(chunk_list_size, 106040909f66SGal Pressman sizeof(*chunk_list->chunks), 106140909f66SGal Pressman GFP_KERNEL); 106240909f66SGal Pressman if (!chunk_list->chunks) 106340909f66SGal Pressman return -ENOMEM; 106440909f66SGal Pressman 106540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 106640909f66SGal Pressman "chunk_list_size[%u] - pages[%u]\n", chunk_list_size, 106740909f66SGal Pressman page_cnt); 106840909f66SGal Pressman 106940909f66SGal Pressman /* allocate chunk buffers: */ 107040909f66SGal Pressman for (i = 0; i < chunk_list_size; i++) { 107140909f66SGal Pressman chunk_list->chunks[i].buf = kzalloc(EFA_CHUNK_SIZE, GFP_KERNEL); 107240909f66SGal Pressman if (!chunk_list->chunks[i].buf) 107340909f66SGal Pressman goto chunk_list_dealloc; 107440909f66SGal Pressman 107540909f66SGal Pressman chunk_list->chunks[i].length = EFA_CHUNK_USED_SIZE; 107640909f66SGal Pressman } 107740909f66SGal Pressman chunk_list->chunks[chunk_list_size - 1].length = 107840909f66SGal Pressman ((page_cnt % EFA_PTRS_PER_CHUNK) * EFA_CHUNK_PAYLOAD_PTR_SIZE) + 107940909f66SGal Pressman EFA_CHUNK_PTR_SIZE; 108040909f66SGal Pressman 108140909f66SGal Pressman /* fill the dma addresses of sg list pages to chunks: */ 108240909f66SGal Pressman chunk_idx = 0; 108340909f66SGal Pressman payload_idx = 0; 108440909f66SGal Pressman cur_chunk_buf = chunk_list->chunks[0].buf; 10854d50e084SGal Pressman rdma_for_each_block(pages_sgl, &biter, sg_dma_cnt, 10864d50e084SGal Pressman EFA_CHUNK_PAYLOAD_SIZE) { 108740909f66SGal Pressman cur_chunk_buf[payload_idx++] = 10884d50e084SGal Pressman rdma_block_iter_dma_address(&biter); 108940909f66SGal Pressman 109040909f66SGal Pressman if (payload_idx == EFA_PTRS_PER_CHUNK) { 109140909f66SGal Pressman chunk_idx++; 109240909f66SGal Pressman cur_chunk_buf = chunk_list->chunks[chunk_idx].buf; 109340909f66SGal Pressman payload_idx = 0; 109440909f66SGal Pressman } 109540909f66SGal Pressman } 109640909f66SGal Pressman 109740909f66SGal Pressman /* map chunks to dma and fill chunks next ptrs */ 109840909f66SGal Pressman for (i = chunk_list_size - 1; i >= 0; i--) { 109940909f66SGal Pressman dma_addr = dma_map_single(&dev->pdev->dev, 110040909f66SGal Pressman chunk_list->chunks[i].buf, 110140909f66SGal Pressman chunk_list->chunks[i].length, 110240909f66SGal Pressman DMA_TO_DEVICE); 110340909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, dma_addr)) { 110440909f66SGal Pressman ibdev_err(&dev->ibdev, 110540909f66SGal Pressman "chunk[%u] dma_map_failed\n", i); 110640909f66SGal Pressman goto chunk_list_unmap; 110740909f66SGal Pressman } 110840909f66SGal Pressman 110940909f66SGal Pressman chunk_list->chunks[i].dma_addr = dma_addr; 111040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 111140909f66SGal Pressman "chunk[%u] mapped at [%pad]\n", i, &dma_addr); 111240909f66SGal Pressman 111340909f66SGal Pressman if (!i) 111440909f66SGal Pressman break; 111540909f66SGal Pressman 111640909f66SGal Pressman prev_chunk_buf = chunk_list->chunks[i - 1].buf; 111740909f66SGal Pressman 111840909f66SGal Pressman ctrl_buf = (struct efa_com_ctrl_buff_info *) 111940909f66SGal Pressman &prev_chunk_buf[EFA_PTRS_PER_CHUNK]; 112040909f66SGal Pressman ctrl_buf->length = chunk_list->chunks[i].length; 112140909f66SGal Pressman 112240909f66SGal Pressman efa_com_set_dma_addr(dma_addr, 112340909f66SGal Pressman &ctrl_buf->address.mem_addr_high, 112440909f66SGal Pressman &ctrl_buf->address.mem_addr_low); 112540909f66SGal Pressman } 112640909f66SGal Pressman 112740909f66SGal Pressman return 0; 112840909f66SGal Pressman 112940909f66SGal Pressman chunk_list_unmap: 113040909f66SGal Pressman for (; i < chunk_list_size; i++) { 113140909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, chunk_list->chunks[i].dma_addr, 113240909f66SGal Pressman chunk_list->chunks[i].length, DMA_TO_DEVICE); 113340909f66SGal Pressman } 113440909f66SGal Pressman chunk_list_dealloc: 113540909f66SGal Pressman for (i = 0; i < chunk_list_size; i++) 113640909f66SGal Pressman kfree(chunk_list->chunks[i].buf); 113740909f66SGal Pressman 113840909f66SGal Pressman kfree(chunk_list->chunks); 113940909f66SGal Pressman return -ENOMEM; 114040909f66SGal Pressman } 114140909f66SGal Pressman 114240909f66SGal Pressman static void pbl_chunk_list_destroy(struct efa_dev *dev, struct pbl_context *pbl) 114340909f66SGal Pressman { 114440909f66SGal Pressman struct pbl_chunk_list *chunk_list = &pbl->phys.indirect.chunk_list; 114540909f66SGal Pressman int i; 114640909f66SGal Pressman 114740909f66SGal Pressman for (i = 0; i < chunk_list->size; i++) { 114840909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, chunk_list->chunks[i].dma_addr, 114940909f66SGal Pressman chunk_list->chunks[i].length, DMA_TO_DEVICE); 115040909f66SGal Pressman kfree(chunk_list->chunks[i].buf); 115140909f66SGal Pressman } 115240909f66SGal Pressman 115340909f66SGal Pressman kfree(chunk_list->chunks); 115440909f66SGal Pressman } 115540909f66SGal Pressman 115640909f66SGal Pressman /* initialize pbl continuous mode: map pbl buffer to a dma address. */ 115740909f66SGal Pressman static int pbl_continuous_initialize(struct efa_dev *dev, 115840909f66SGal Pressman struct pbl_context *pbl) 115940909f66SGal Pressman { 116040909f66SGal Pressman dma_addr_t dma_addr; 116140909f66SGal Pressman 116240909f66SGal Pressman dma_addr = dma_map_single(&dev->pdev->dev, pbl->pbl_buf, 116340909f66SGal Pressman pbl->pbl_buf_size_in_bytes, DMA_TO_DEVICE); 116440909f66SGal Pressman if (dma_mapping_error(&dev->pdev->dev, dma_addr)) { 116540909f66SGal Pressman ibdev_err(&dev->ibdev, "Unable to map pbl to DMA address\n"); 116640909f66SGal Pressman return -ENOMEM; 116740909f66SGal Pressman } 116840909f66SGal Pressman 116940909f66SGal Pressman pbl->phys.continuous.dma_addr = dma_addr; 117040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 117140909f66SGal Pressman "pbl continuous - dma_addr = %pad, size[%u]\n", 117240909f66SGal Pressman &dma_addr, pbl->pbl_buf_size_in_bytes); 117340909f66SGal Pressman 117440909f66SGal Pressman return 0; 117540909f66SGal Pressman } 117640909f66SGal Pressman 117740909f66SGal Pressman /* 117840909f66SGal Pressman * initialize pbl indirect mode: 117940909f66SGal Pressman * create a chunk list out of the dma addresses of the physical pages of 118040909f66SGal Pressman * pbl buffer. 118140909f66SGal Pressman */ 118240909f66SGal Pressman static int pbl_indirect_initialize(struct efa_dev *dev, struct pbl_context *pbl) 118340909f66SGal Pressman { 118440909f66SGal Pressman u32 size_in_pages = DIV_ROUND_UP(pbl->pbl_buf_size_in_bytes, PAGE_SIZE); 118540909f66SGal Pressman struct scatterlist *sgl; 118640909f66SGal Pressman int sg_dma_cnt, err; 118740909f66SGal Pressman 118840909f66SGal Pressman BUILD_BUG_ON(EFA_CHUNK_PAYLOAD_SIZE > PAGE_SIZE); 118940909f66SGal Pressman sgl = efa_vmalloc_buf_to_sg(pbl->pbl_buf, size_in_pages); 119040909f66SGal Pressman if (!sgl) 119140909f66SGal Pressman return -ENOMEM; 119240909f66SGal Pressman 119340909f66SGal Pressman sg_dma_cnt = dma_map_sg(&dev->pdev->dev, sgl, size_in_pages, DMA_TO_DEVICE); 119440909f66SGal Pressman if (!sg_dma_cnt) { 119540909f66SGal Pressman err = -EINVAL; 119640909f66SGal Pressman goto err_map; 119740909f66SGal Pressman } 119840909f66SGal Pressman 119940909f66SGal Pressman pbl->phys.indirect.pbl_buf_size_in_pages = size_in_pages; 120040909f66SGal Pressman pbl->phys.indirect.sgl = sgl; 120140909f66SGal Pressman pbl->phys.indirect.sg_dma_cnt = sg_dma_cnt; 120240909f66SGal Pressman err = pbl_chunk_list_create(dev, pbl); 120340909f66SGal Pressman if (err) { 120440909f66SGal Pressman ibdev_dbg(&dev->ibdev, 120540909f66SGal Pressman "chunk_list creation failed[%d]\n", err); 120640909f66SGal Pressman goto err_chunk; 120740909f66SGal Pressman } 120840909f66SGal Pressman 120940909f66SGal Pressman ibdev_dbg(&dev->ibdev, 121040909f66SGal Pressman "pbl indirect - size[%u], chunks[%u]\n", 121140909f66SGal Pressman pbl->pbl_buf_size_in_bytes, 121240909f66SGal Pressman pbl->phys.indirect.chunk_list.size); 121340909f66SGal Pressman 121440909f66SGal Pressman return 0; 121540909f66SGal Pressman 121640909f66SGal Pressman err_chunk: 121740909f66SGal Pressman dma_unmap_sg(&dev->pdev->dev, sgl, size_in_pages, DMA_TO_DEVICE); 121840909f66SGal Pressman err_map: 121940909f66SGal Pressman kfree(sgl); 122040909f66SGal Pressman return err; 122140909f66SGal Pressman } 122240909f66SGal Pressman 122340909f66SGal Pressman static void pbl_indirect_terminate(struct efa_dev *dev, struct pbl_context *pbl) 122440909f66SGal Pressman { 122540909f66SGal Pressman pbl_chunk_list_destroy(dev, pbl); 122640909f66SGal Pressman dma_unmap_sg(&dev->pdev->dev, pbl->phys.indirect.sgl, 122740909f66SGal Pressman pbl->phys.indirect.pbl_buf_size_in_pages, DMA_TO_DEVICE); 122840909f66SGal Pressman kfree(pbl->phys.indirect.sgl); 122940909f66SGal Pressman } 123040909f66SGal Pressman 123140909f66SGal Pressman /* create a page buffer list from a mapped user memory region */ 123240909f66SGal Pressman static int pbl_create(struct efa_dev *dev, 123340909f66SGal Pressman struct pbl_context *pbl, 123440909f66SGal Pressman struct ib_umem *umem, 123540909f66SGal Pressman int hp_cnt, 123640909f66SGal Pressman u8 hp_shift) 123740909f66SGal Pressman { 123840909f66SGal Pressman int err; 123940909f66SGal Pressman 124040909f66SGal Pressman pbl->pbl_buf_size_in_bytes = hp_cnt * EFA_CHUNK_PAYLOAD_PTR_SIZE; 1241255efcaeSGal Pressman pbl->pbl_buf = kvzalloc(pbl->pbl_buf_size_in_bytes, GFP_KERNEL); 1242255efcaeSGal Pressman if (!pbl->pbl_buf) 1243255efcaeSGal Pressman return -ENOMEM; 1244255efcaeSGal Pressman 1245255efcaeSGal Pressman if (is_vmalloc_addr(pbl->pbl_buf)) { 1246255efcaeSGal Pressman pbl->physically_continuous = 0; 1247255efcaeSGal Pressman err = umem_to_page_list(dev, umem, pbl->pbl_buf, hp_cnt, 1248255efcaeSGal Pressman hp_shift); 1249255efcaeSGal Pressman if (err) 1250255efcaeSGal Pressman goto err_free; 1251255efcaeSGal Pressman 1252255efcaeSGal Pressman err = pbl_indirect_initialize(dev, pbl); 1253255efcaeSGal Pressman if (err) 1254255efcaeSGal Pressman goto err_free; 1255255efcaeSGal Pressman } else { 125640909f66SGal Pressman pbl->physically_continuous = 1; 125740909f66SGal Pressman err = umem_to_page_list(dev, umem, pbl->pbl_buf, hp_cnt, 125840909f66SGal Pressman hp_shift); 125940909f66SGal Pressman if (err) 1260255efcaeSGal Pressman goto err_free; 1261255efcaeSGal Pressman 126240909f66SGal Pressman err = pbl_continuous_initialize(dev, pbl); 126340909f66SGal Pressman if (err) 1264255efcaeSGal Pressman goto err_free; 126540909f66SGal Pressman } 126640909f66SGal Pressman 126740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 126840909f66SGal Pressman "user_pbl_created: user_pages[%u], continuous[%u]\n", 126940909f66SGal Pressman hp_cnt, pbl->physically_continuous); 127040909f66SGal Pressman 127140909f66SGal Pressman return 0; 127240909f66SGal Pressman 1273255efcaeSGal Pressman err_free: 1274255efcaeSGal Pressman kvfree(pbl->pbl_buf); 127540909f66SGal Pressman return err; 127640909f66SGal Pressman } 127740909f66SGal Pressman 127840909f66SGal Pressman static void pbl_destroy(struct efa_dev *dev, struct pbl_context *pbl) 127940909f66SGal Pressman { 1280255efcaeSGal Pressman if (pbl->physically_continuous) 128140909f66SGal Pressman dma_unmap_single(&dev->pdev->dev, pbl->phys.continuous.dma_addr, 128240909f66SGal Pressman pbl->pbl_buf_size_in_bytes, DMA_TO_DEVICE); 1283255efcaeSGal Pressman else 128440909f66SGal Pressman pbl_indirect_terminate(dev, pbl); 1285255efcaeSGal Pressman 1286255efcaeSGal Pressman kvfree(pbl->pbl_buf); 128740909f66SGal Pressman } 128840909f66SGal Pressman 128940909f66SGal Pressman static int efa_create_inline_pbl(struct efa_dev *dev, struct efa_mr *mr, 129040909f66SGal Pressman struct efa_com_reg_mr_params *params) 129140909f66SGal Pressman { 129240909f66SGal Pressman int err; 129340909f66SGal Pressman 129440909f66SGal Pressman params->inline_pbl = 1; 129540909f66SGal Pressman err = umem_to_page_list(dev, mr->umem, params->pbl.inline_pbl_array, 129640909f66SGal Pressman params->page_num, params->page_shift); 129740909f66SGal Pressman if (err) 129840909f66SGal Pressman return err; 129940909f66SGal Pressman 130040909f66SGal Pressman ibdev_dbg(&dev->ibdev, 130140909f66SGal Pressman "inline_pbl_array - pages[%u]\n", params->page_num); 130240909f66SGal Pressman 130340909f66SGal Pressman return 0; 130440909f66SGal Pressman } 130540909f66SGal Pressman 130640909f66SGal Pressman static int efa_create_pbl(struct efa_dev *dev, 130740909f66SGal Pressman struct pbl_context *pbl, 130840909f66SGal Pressman struct efa_mr *mr, 130940909f66SGal Pressman struct efa_com_reg_mr_params *params) 131040909f66SGal Pressman { 131140909f66SGal Pressman int err; 131240909f66SGal Pressman 131340909f66SGal Pressman err = pbl_create(dev, pbl, mr->umem, params->page_num, 131440909f66SGal Pressman params->page_shift); 131540909f66SGal Pressman if (err) { 131640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Failed to create pbl[%d]\n", err); 131740909f66SGal Pressman return err; 131840909f66SGal Pressman } 131940909f66SGal Pressman 132040909f66SGal Pressman params->inline_pbl = 0; 132140909f66SGal Pressman params->indirect = !pbl->physically_continuous; 132240909f66SGal Pressman if (pbl->physically_continuous) { 132340909f66SGal Pressman params->pbl.pbl.length = pbl->pbl_buf_size_in_bytes; 132440909f66SGal Pressman 132540909f66SGal Pressman efa_com_set_dma_addr(pbl->phys.continuous.dma_addr, 132640909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_high, 132740909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_low); 132840909f66SGal Pressman } else { 132940909f66SGal Pressman params->pbl.pbl.length = 133040909f66SGal Pressman pbl->phys.indirect.chunk_list.chunks[0].length; 133140909f66SGal Pressman 133240909f66SGal Pressman efa_com_set_dma_addr(pbl->phys.indirect.chunk_list.chunks[0].dma_addr, 133340909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_high, 133440909f66SGal Pressman ¶ms->pbl.pbl.address.mem_addr_low); 133540909f66SGal Pressman } 133640909f66SGal Pressman 133740909f66SGal Pressman return 0; 133840909f66SGal Pressman } 133940909f66SGal Pressman 134040909f66SGal Pressman struct ib_mr *efa_reg_mr(struct ib_pd *ibpd, u64 start, u64 length, 134140909f66SGal Pressman u64 virt_addr, int access_flags, 134240909f66SGal Pressman struct ib_udata *udata) 134340909f66SGal Pressman { 134440909f66SGal Pressman struct efa_dev *dev = to_edev(ibpd->device); 134540909f66SGal Pressman struct efa_com_reg_mr_params params = {}; 134640909f66SGal Pressman struct efa_com_reg_mr_result result = {}; 134740909f66SGal Pressman struct pbl_context pbl; 134840ddb3f0SGal Pressman unsigned int pg_sz; 134940909f66SGal Pressman struct efa_mr *mr; 135040909f66SGal Pressman int inline_size; 135140909f66SGal Pressman int err; 135240909f66SGal Pressman 135340909f66SGal Pressman if (udata->inlen && 135440909f66SGal Pressman !ib_is_udata_cleared(udata, 0, sizeof(udata->inlen))) { 135540909f66SGal Pressman ibdev_dbg(&dev->ibdev, 135640909f66SGal Pressman "Incompatible ABI params, udata not cleared\n"); 135740909f66SGal Pressman err = -EINVAL; 135840909f66SGal Pressman goto err_out; 135940909f66SGal Pressman } 136040909f66SGal Pressman 136140909f66SGal Pressman if (access_flags & ~EFA_SUPPORTED_ACCESS_FLAGS) { 136240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 136340909f66SGal Pressman "Unsupported access flags[%#x], supported[%#x]\n", 136440909f66SGal Pressman access_flags, EFA_SUPPORTED_ACCESS_FLAGS); 136540909f66SGal Pressman err = -EOPNOTSUPP; 136640909f66SGal Pressman goto err_out; 136740909f66SGal Pressman } 136840909f66SGal Pressman 136940909f66SGal Pressman mr = kzalloc(sizeof(*mr), GFP_KERNEL); 137040909f66SGal Pressman if (!mr) { 137140909f66SGal Pressman err = -ENOMEM; 137240909f66SGal Pressman goto err_out; 137340909f66SGal Pressman } 137440909f66SGal Pressman 137572b894b0SChristoph Hellwig mr->umem = ib_umem_get(udata, start, length, access_flags); 137640909f66SGal Pressman if (IS_ERR(mr->umem)) { 137740909f66SGal Pressman err = PTR_ERR(mr->umem); 137840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 137940909f66SGal Pressman "Failed to pin and map user space memory[%d]\n", err); 138040909f66SGal Pressman goto err_free; 138140909f66SGal Pressman } 138240909f66SGal Pressman 138340909f66SGal Pressman params.pd = to_epd(ibpd)->pdn; 138440909f66SGal Pressman params.iova = virt_addr; 138540909f66SGal Pressman params.mr_length_in_bytes = length; 1386*e6c4f3ffSDaniel Kranzdorf params.permissions = access_flags; 138740909f66SGal Pressman 138840ddb3f0SGal Pressman pg_sz = ib_umem_find_best_pgsz(mr->umem, 138940ddb3f0SGal Pressman dev->dev_attr.page_size_cap, 139040ddb3f0SGal Pressman virt_addr); 139140ddb3f0SGal Pressman if (!pg_sz) { 139240ddb3f0SGal Pressman err = -EOPNOTSUPP; 139340ddb3f0SGal Pressman ibdev_dbg(&dev->ibdev, "Failed to find a suitable page size in page_size_cap %#llx\n", 139440ddb3f0SGal Pressman dev->dev_attr.page_size_cap); 139540ddb3f0SGal Pressman goto err_unmap; 139640ddb3f0SGal Pressman } 139740ddb3f0SGal Pressman 139840ddb3f0SGal Pressman params.page_shift = __ffs(pg_sz); 139940ddb3f0SGal Pressman params.page_num = DIV_ROUND_UP(length + (start & (pg_sz - 1)), 140040ddb3f0SGal Pressman pg_sz); 140140ddb3f0SGal Pressman 140240909f66SGal Pressman ibdev_dbg(&dev->ibdev, 140340ddb3f0SGal Pressman "start %#llx length %#llx params.page_shift %u params.page_num %u\n", 140440ddb3f0SGal Pressman start, length, params.page_shift, params.page_num); 140540909f66SGal Pressman 140640909f66SGal Pressman inline_size = ARRAY_SIZE(params.pbl.inline_pbl_array); 140740909f66SGal Pressman if (params.page_num <= inline_size) { 140840909f66SGal Pressman err = efa_create_inline_pbl(dev, mr, ¶ms); 140940909f66SGal Pressman if (err) 141040909f66SGal Pressman goto err_unmap; 141140909f66SGal Pressman 141240909f66SGal Pressman err = efa_com_register_mr(&dev->edev, ¶ms, &result); 141340909f66SGal Pressman if (err) 141440909f66SGal Pressman goto err_unmap; 141540909f66SGal Pressman } else { 141640909f66SGal Pressman err = efa_create_pbl(dev, &pbl, mr, ¶ms); 141740909f66SGal Pressman if (err) 141840909f66SGal Pressman goto err_unmap; 141940909f66SGal Pressman 142040909f66SGal Pressman err = efa_com_register_mr(&dev->edev, ¶ms, &result); 142140909f66SGal Pressman pbl_destroy(dev, &pbl); 142240909f66SGal Pressman 142340909f66SGal Pressman if (err) 142440909f66SGal Pressman goto err_unmap; 142540909f66SGal Pressman } 142640909f66SGal Pressman 142740909f66SGal Pressman mr->ibmr.lkey = result.l_key; 142840909f66SGal Pressman mr->ibmr.rkey = result.r_key; 142940909f66SGal Pressman mr->ibmr.length = length; 143040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Registered mr[%d]\n", mr->ibmr.lkey); 143140909f66SGal Pressman 143240909f66SGal Pressman return &mr->ibmr; 143340909f66SGal Pressman 143440909f66SGal Pressman err_unmap: 143540909f66SGal Pressman ib_umem_release(mr->umem); 143640909f66SGal Pressman err_free: 143740909f66SGal Pressman kfree(mr); 143840909f66SGal Pressman err_out: 143940909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.reg_mr_err); 144040909f66SGal Pressman return ERR_PTR(err); 144140909f66SGal Pressman } 144240909f66SGal Pressman 144340909f66SGal Pressman int efa_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata) 144440909f66SGal Pressman { 144540909f66SGal Pressman struct efa_dev *dev = to_edev(ibmr->device); 144640909f66SGal Pressman struct efa_com_dereg_mr_params params; 144740909f66SGal Pressman struct efa_mr *mr = to_emr(ibmr); 144840909f66SGal Pressman int err; 144940909f66SGal Pressman 145040909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Deregister mr[%d]\n", ibmr->lkey); 145140909f66SGal Pressman 145240909f66SGal Pressman params.l_key = mr->ibmr.lkey; 145340909f66SGal Pressman err = efa_com_dereg_mr(&dev->edev, ¶ms); 145440909f66SGal Pressman if (err) 145540909f66SGal Pressman return err; 145640909f66SGal Pressman 1457958b6813SGal Pressman ib_umem_release(mr->umem); 145840909f66SGal Pressman kfree(mr); 145940909f66SGal Pressman 146040909f66SGal Pressman return 0; 146140909f66SGal Pressman } 146240909f66SGal Pressman 146340909f66SGal Pressman int efa_get_port_immutable(struct ib_device *ibdev, u8 port_num, 146440909f66SGal Pressman struct ib_port_immutable *immutable) 146540909f66SGal Pressman { 146640909f66SGal Pressman struct ib_port_attr attr; 146740909f66SGal Pressman int err; 146840909f66SGal Pressman 146940909f66SGal Pressman err = ib_query_port(ibdev, port_num, &attr); 147040909f66SGal Pressman if (err) { 147140909f66SGal Pressman ibdev_dbg(ibdev, "Couldn't query port err[%d]\n", err); 147240909f66SGal Pressman return err; 147340909f66SGal Pressman } 147440909f66SGal Pressman 147540909f66SGal Pressman immutable->pkey_tbl_len = attr.pkey_tbl_len; 147640909f66SGal Pressman immutable->gid_tbl_len = attr.gid_tbl_len; 147740909f66SGal Pressman 147840909f66SGal Pressman return 0; 147940909f66SGal Pressman } 148040909f66SGal Pressman 148140909f66SGal Pressman static int efa_dealloc_uar(struct efa_dev *dev, u16 uarn) 148240909f66SGal Pressman { 148340909f66SGal Pressman struct efa_com_dealloc_uar_params params = { 148440909f66SGal Pressman .uarn = uarn, 148540909f66SGal Pressman }; 148640909f66SGal Pressman 148740909f66SGal Pressman return efa_com_dealloc_uar(&dev->edev, ¶ms); 148840909f66SGal Pressman } 148940909f66SGal Pressman 149040909f66SGal Pressman int efa_alloc_ucontext(struct ib_ucontext *ibucontext, struct ib_udata *udata) 149140909f66SGal Pressman { 149240909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 149340909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 149440909f66SGal Pressman struct efa_ibv_alloc_ucontext_resp resp = {}; 149540909f66SGal Pressman struct efa_com_alloc_uar_result result; 149640909f66SGal Pressman int err; 149740909f66SGal Pressman 149840909f66SGal Pressman /* 149940909f66SGal Pressman * it's fine if the driver does not know all request fields, 150040909f66SGal Pressman * we will ack input fields in our response. 150140909f66SGal Pressman */ 150240909f66SGal Pressman 150340909f66SGal Pressman err = efa_com_alloc_uar(&dev->edev, &result); 150440909f66SGal Pressman if (err) 150540909f66SGal Pressman goto err_out; 150640909f66SGal Pressman 150740909f66SGal Pressman ucontext->uarn = result.uarn; 150840909f66SGal Pressman 150940909f66SGal Pressman resp.cmds_supp_udata_mask |= EFA_USER_CMDS_SUPP_UDATA_QUERY_DEVICE; 151040909f66SGal Pressman resp.cmds_supp_udata_mask |= EFA_USER_CMDS_SUPP_UDATA_CREATE_AH; 151140909f66SGal Pressman resp.sub_cqs_per_cq = dev->dev_attr.sub_cqs_per_cq; 151240909f66SGal Pressman resp.inline_buf_size = dev->dev_attr.inline_buf_size; 151340909f66SGal Pressman resp.max_llq_size = dev->dev_attr.max_llq_size; 151440909f66SGal Pressman 151540909f66SGal Pressman if (udata && udata->outlen) { 151640909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 151740909f66SGal Pressman min(sizeof(resp), udata->outlen)); 151840909f66SGal Pressman if (err) 151940909f66SGal Pressman goto err_dealloc_uar; 152040909f66SGal Pressman } 152140909f66SGal Pressman 152240909f66SGal Pressman return 0; 152340909f66SGal Pressman 152440909f66SGal Pressman err_dealloc_uar: 152540909f66SGal Pressman efa_dealloc_uar(dev, result.uarn); 152640909f66SGal Pressman err_out: 152740909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.alloc_ucontext_err); 152840909f66SGal Pressman return err; 152940909f66SGal Pressman } 153040909f66SGal Pressman 153140909f66SGal Pressman void efa_dealloc_ucontext(struct ib_ucontext *ibucontext) 153240909f66SGal Pressman { 153340909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 153440909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 153540909f66SGal Pressman 153640909f66SGal Pressman efa_dealloc_uar(dev, ucontext->uarn); 153740909f66SGal Pressman } 153840909f66SGal Pressman 1539e84d3c18SMichal Kalderon void efa_mmap_free(struct rdma_user_mmap_entry *rdma_entry) 154040909f66SGal Pressman { 1541e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry = to_emmap(rdma_entry); 154240909f66SGal Pressman 1543e84d3c18SMichal Kalderon /* DMA mapping is already gone, now free the pages */ 1544e84d3c18SMichal Kalderon if (entry->mmap_flag == EFA_MMAP_DMA_PAGE) 1545e84d3c18SMichal Kalderon free_pages_exact(phys_to_virt(entry->address), 1546e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE); 1547e84d3c18SMichal Kalderon kfree(entry); 154840909f66SGal Pressman } 154940909f66SGal Pressman 1550e84d3c18SMichal Kalderon static int __efa_mmap(struct efa_dev *dev, struct efa_ucontext *ucontext, 1551e84d3c18SMichal Kalderon struct vm_area_struct *vma) 1552e84d3c18SMichal Kalderon { 1553e84d3c18SMichal Kalderon struct rdma_user_mmap_entry *rdma_entry; 1554e84d3c18SMichal Kalderon struct efa_user_mmap_entry *entry; 1555e84d3c18SMichal Kalderon unsigned long va; 1556e84d3c18SMichal Kalderon int err = 0; 1557e84d3c18SMichal Kalderon u64 pfn; 1558e84d3c18SMichal Kalderon 1559e84d3c18SMichal Kalderon rdma_entry = rdma_user_mmap_entry_get(&ucontext->ibucontext, vma); 1560e84d3c18SMichal Kalderon if (!rdma_entry) { 156140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 1562e84d3c18SMichal Kalderon "pgoff[%#lx] does not have valid entry\n", 1563e84d3c18SMichal Kalderon vma->vm_pgoff); 1564e84d3c18SMichal Kalderon return -EINVAL; 1565e84d3c18SMichal Kalderon } 1566e84d3c18SMichal Kalderon entry = to_emmap(rdma_entry); 1567e84d3c18SMichal Kalderon 1568e84d3c18SMichal Kalderon ibdev_dbg(&dev->ibdev, 1569e84d3c18SMichal Kalderon "Mapping address[%#llx], length[%#zx], mmap_flag[%d]\n", 1570e84d3c18SMichal Kalderon entry->address, rdma_entry->npages * PAGE_SIZE, 1571e84d3c18SMichal Kalderon entry->mmap_flag); 157240909f66SGal Pressman 157340909f66SGal Pressman pfn = entry->address >> PAGE_SHIFT; 157440909f66SGal Pressman switch (entry->mmap_flag) { 157540909f66SGal Pressman case EFA_MMAP_IO_NC: 1576e84d3c18SMichal Kalderon err = rdma_user_mmap_io(&ucontext->ibucontext, vma, pfn, 1577e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE, 1578c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 1579e84d3c18SMichal Kalderon rdma_entry); 158040909f66SGal Pressman break; 158140909f66SGal Pressman case EFA_MMAP_IO_WC: 1582e84d3c18SMichal Kalderon err = rdma_user_mmap_io(&ucontext->ibucontext, vma, pfn, 1583e84d3c18SMichal Kalderon entry->rdma_entry.npages * PAGE_SIZE, 1584c043ff2cSMichal Kalderon pgprot_writecombine(vma->vm_page_prot), 1585e84d3c18SMichal Kalderon rdma_entry); 158640909f66SGal Pressman break; 158740909f66SGal Pressman case EFA_MMAP_DMA_PAGE: 158840909f66SGal Pressman for (va = vma->vm_start; va < vma->vm_end; 158940909f66SGal Pressman va += PAGE_SIZE, pfn++) { 159040909f66SGal Pressman err = vm_insert_page(vma, va, pfn_to_page(pfn)); 159140909f66SGal Pressman if (err) 159240909f66SGal Pressman break; 159340909f66SGal Pressman } 159440909f66SGal Pressman break; 159540909f66SGal Pressman default: 159640909f66SGal Pressman err = -EINVAL; 159740909f66SGal Pressman } 159840909f66SGal Pressman 1599b41f7572SGal Pressman if (err) { 160040909f66SGal Pressman ibdev_dbg( 160140909f66SGal Pressman &dev->ibdev, 1602e84d3c18SMichal Kalderon "Couldn't mmap address[%#llx] length[%#zx] mmap_flag[%d] err[%d]\n", 1603e84d3c18SMichal Kalderon entry->address, rdma_entry->npages * PAGE_SIZE, 1604e84d3c18SMichal Kalderon entry->mmap_flag, err); 160540909f66SGal Pressman } 160640909f66SGal Pressman 1607e84d3c18SMichal Kalderon rdma_user_mmap_entry_put(rdma_entry); 1608e84d3c18SMichal Kalderon return err; 1609b41f7572SGal Pressman } 1610b41f7572SGal Pressman 161140909f66SGal Pressman int efa_mmap(struct ib_ucontext *ibucontext, 161240909f66SGal Pressman struct vm_area_struct *vma) 161340909f66SGal Pressman { 161440909f66SGal Pressman struct efa_ucontext *ucontext = to_eucontext(ibucontext); 161540909f66SGal Pressman struct efa_dev *dev = to_edev(ibucontext->device); 1616e84d3c18SMichal Kalderon size_t length = vma->vm_end - vma->vm_start; 161740909f66SGal Pressman 161840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 1619e84d3c18SMichal Kalderon "start %#lx, end %#lx, length = %#zx, pgoff = %#lx\n", 1620e84d3c18SMichal Kalderon vma->vm_start, vma->vm_end, length, vma->vm_pgoff); 162140909f66SGal Pressman 1622e84d3c18SMichal Kalderon return __efa_mmap(dev, ucontext, vma); 162340909f66SGal Pressman } 162440909f66SGal Pressman 162540909f66SGal Pressman static int efa_ah_destroy(struct efa_dev *dev, struct efa_ah *ah) 162640909f66SGal Pressman { 162740909f66SGal Pressman struct efa_com_destroy_ah_params params = { 162840909f66SGal Pressman .ah = ah->ah, 162940909f66SGal Pressman .pdn = to_epd(ah->ibah.pd)->pdn, 163040909f66SGal Pressman }; 163140909f66SGal Pressman 163240909f66SGal Pressman return efa_com_destroy_ah(&dev->edev, ¶ms); 163340909f66SGal Pressman } 163440909f66SGal Pressman 163540909f66SGal Pressman int efa_create_ah(struct ib_ah *ibah, 163640909f66SGal Pressman struct rdma_ah_attr *ah_attr, 163740909f66SGal Pressman u32 flags, 163840909f66SGal Pressman struct ib_udata *udata) 163940909f66SGal Pressman { 164040909f66SGal Pressman struct efa_dev *dev = to_edev(ibah->device); 164140909f66SGal Pressman struct efa_com_create_ah_params params = {}; 164240909f66SGal Pressman struct efa_ibv_create_ah_resp resp = {}; 164340909f66SGal Pressman struct efa_com_create_ah_result result; 164440909f66SGal Pressman struct efa_ah *ah = to_eah(ibah); 164540909f66SGal Pressman int err; 164640909f66SGal Pressman 164740909f66SGal Pressman if (!(flags & RDMA_CREATE_AH_SLEEPABLE)) { 164840909f66SGal Pressman ibdev_dbg(&dev->ibdev, 164940909f66SGal Pressman "Create address handle is not supported in atomic context\n"); 165040909f66SGal Pressman err = -EOPNOTSUPP; 165140909f66SGal Pressman goto err_out; 165240909f66SGal Pressman } 165340909f66SGal Pressman 165440909f66SGal Pressman if (udata->inlen && 165540909f66SGal Pressman !ib_is_udata_cleared(udata, 0, udata->inlen)) { 165640909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Incompatible ABI params\n"); 165740909f66SGal Pressman err = -EINVAL; 165840909f66SGal Pressman goto err_out; 165940909f66SGal Pressman } 166040909f66SGal Pressman 166140909f66SGal Pressman memcpy(params.dest_addr, ah_attr->grh.dgid.raw, 166240909f66SGal Pressman sizeof(params.dest_addr)); 166340909f66SGal Pressman params.pdn = to_epd(ibah->pd)->pdn; 166440909f66SGal Pressman err = efa_com_create_ah(&dev->edev, ¶ms, &result); 166540909f66SGal Pressman if (err) 166640909f66SGal Pressman goto err_out; 166740909f66SGal Pressman 166840909f66SGal Pressman memcpy(ah->id, ah_attr->grh.dgid.raw, sizeof(ah->id)); 166940909f66SGal Pressman ah->ah = result.ah; 167040909f66SGal Pressman 167140909f66SGal Pressman resp.efa_address_handle = result.ah; 167240909f66SGal Pressman 167340909f66SGal Pressman if (udata->outlen) { 167440909f66SGal Pressman err = ib_copy_to_udata(udata, &resp, 167540909f66SGal Pressman min(sizeof(resp), udata->outlen)); 167640909f66SGal Pressman if (err) { 167740909f66SGal Pressman ibdev_dbg(&dev->ibdev, 167840909f66SGal Pressman "Failed to copy udata for create_ah response\n"); 167940909f66SGal Pressman goto err_destroy_ah; 168040909f66SGal Pressman } 168140909f66SGal Pressman } 168240909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Created ah[%d]\n", ah->ah); 168340909f66SGal Pressman 168440909f66SGal Pressman return 0; 168540909f66SGal Pressman 168640909f66SGal Pressman err_destroy_ah: 168740909f66SGal Pressman efa_ah_destroy(dev, ah); 168840909f66SGal Pressman err_out: 168940909f66SGal Pressman atomic64_inc(&dev->stats.sw_stats.create_ah_err); 169040909f66SGal Pressman return err; 169140909f66SGal Pressman } 169240909f66SGal Pressman 169340909f66SGal Pressman void efa_destroy_ah(struct ib_ah *ibah, u32 flags) 169440909f66SGal Pressman { 169540909f66SGal Pressman struct efa_dev *dev = to_edev(ibah->pd->device); 169640909f66SGal Pressman struct efa_ah *ah = to_eah(ibah); 169740909f66SGal Pressman 169840909f66SGal Pressman ibdev_dbg(&dev->ibdev, "Destroy ah[%d]\n", ah->ah); 169940909f66SGal Pressman 170040909f66SGal Pressman if (!(flags & RDMA_DESTROY_AH_SLEEPABLE)) { 170140909f66SGal Pressman ibdev_dbg(&dev->ibdev, 170240909f66SGal Pressman "Destroy address handle is not supported in atomic context\n"); 170340909f66SGal Pressman return; 170440909f66SGal Pressman } 170540909f66SGal Pressman 170640909f66SGal Pressman efa_ah_destroy(dev, ah); 170740909f66SGal Pressman } 170840909f66SGal Pressman 170916e9111eSGal Pressman struct rdma_hw_stats *efa_alloc_hw_stats(struct ib_device *ibdev, u8 port_num) 171016e9111eSGal Pressman { 171116e9111eSGal Pressman return rdma_alloc_hw_stats_struct(efa_stats_names, 171216e9111eSGal Pressman ARRAY_SIZE(efa_stats_names), 171316e9111eSGal Pressman RDMA_HW_STATS_DEFAULT_LIFESPAN); 171416e9111eSGal Pressman } 171516e9111eSGal Pressman 171616e9111eSGal Pressman int efa_get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats, 171716e9111eSGal Pressman u8 port_num, int index) 171816e9111eSGal Pressman { 171916e9111eSGal Pressman struct efa_com_get_stats_params params = {}; 172016e9111eSGal Pressman union efa_com_get_stats_result result; 172116e9111eSGal Pressman struct efa_dev *dev = to_edev(ibdev); 172216e9111eSGal Pressman struct efa_com_basic_stats *bs; 172316e9111eSGal Pressman struct efa_com_stats_admin *as; 172416e9111eSGal Pressman struct efa_stats *s; 172516e9111eSGal Pressman int err; 172616e9111eSGal Pressman 172716e9111eSGal Pressman params.type = EFA_ADMIN_GET_STATS_TYPE_BASIC; 172816e9111eSGal Pressman params.scope = EFA_ADMIN_GET_STATS_SCOPE_ALL; 172916e9111eSGal Pressman 173016e9111eSGal Pressman err = efa_com_get_stats(&dev->edev, ¶ms, &result); 173116e9111eSGal Pressman if (err) 173216e9111eSGal Pressman return err; 173316e9111eSGal Pressman 173416e9111eSGal Pressman bs = &result.basic_stats; 173516e9111eSGal Pressman stats->value[EFA_TX_BYTES] = bs->tx_bytes; 173616e9111eSGal Pressman stats->value[EFA_TX_PKTS] = bs->tx_pkts; 173716e9111eSGal Pressman stats->value[EFA_RX_BYTES] = bs->rx_bytes; 173816e9111eSGal Pressman stats->value[EFA_RX_PKTS] = bs->rx_pkts; 173916e9111eSGal Pressman stats->value[EFA_RX_DROPS] = bs->rx_drops; 174016e9111eSGal Pressman 174116e9111eSGal Pressman as = &dev->edev.aq.stats; 174216e9111eSGal Pressman stats->value[EFA_SUBMITTED_CMDS] = atomic64_read(&as->submitted_cmd); 174316e9111eSGal Pressman stats->value[EFA_COMPLETED_CMDS] = atomic64_read(&as->completed_cmd); 174416e9111eSGal Pressman stats->value[EFA_NO_COMPLETION_CMDS] = atomic64_read(&as->no_completion); 174516e9111eSGal Pressman 174616e9111eSGal Pressman s = &dev->stats; 174716e9111eSGal Pressman stats->value[EFA_KEEP_ALIVE_RCVD] = atomic64_read(&s->keep_alive_rcvd); 174816e9111eSGal Pressman stats->value[EFA_ALLOC_PD_ERR] = atomic64_read(&s->sw_stats.alloc_pd_err); 174916e9111eSGal Pressman stats->value[EFA_CREATE_QP_ERR] = atomic64_read(&s->sw_stats.create_qp_err); 175016e9111eSGal Pressman stats->value[EFA_REG_MR_ERR] = atomic64_read(&s->sw_stats.reg_mr_err); 175116e9111eSGal Pressman stats->value[EFA_ALLOC_UCONTEXT_ERR] = atomic64_read(&s->sw_stats.alloc_ucontext_err); 175216e9111eSGal Pressman stats->value[EFA_CREATE_AH_ERR] = atomic64_read(&s->sw_stats.create_ah_err); 175316e9111eSGal Pressman 175416e9111eSGal Pressman return ARRAY_SIZE(efa_stats_names); 175516e9111eSGal Pressman } 175616e9111eSGal Pressman 175740909f66SGal Pressman enum rdma_link_layer efa_port_link_layer(struct ib_device *ibdev, 175840909f66SGal Pressman u8 port_num) 175940909f66SGal Pressman { 176040909f66SGal Pressman return IB_LINK_LAYER_UNSPECIFIED; 176140909f66SGal Pressman } 176240909f66SGal Pressman 1763