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