• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* QLogic qedr NIC Driver
2  * Copyright (c) 2015-2016  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/dma-mapping.h>
33 #include <linux/crc32.h>
34 #include <net/ip.h>
35 #include <net/ipv6.h>
36 #include <net/udp.h>
37 #include <linux/iommu.h>
38 
39 #include <rdma/ib_verbs.h>
40 #include <rdma/ib_user_verbs.h>
41 #include <rdma/iw_cm.h>
42 #include <rdma/ib_umem.h>
43 #include <rdma/ib_addr.h>
44 #include <rdma/ib_cache.h>
45 #include <rdma/uverbs_ioctl.h>
46 
47 #include <linux/qed/common_hsi.h>
48 #include "qedr_hsi_rdma.h"
49 #include <linux/qed/qed_if.h>
50 #include "qedr.h"
51 #include "verbs.h"
52 #include <rdma/qedr-abi.h>
53 #include "qedr_roce_cm.h"
54 #include "qedr_iw_cm.h"
55 
56 #define QEDR_SRQ_WQE_ELEM_SIZE	sizeof(union rdma_srq_elm)
57 #define	RDMA_MAX_SGE_PER_SRQ	(4)
58 #define RDMA_MAX_SRQ_WQE_SIZE	(RDMA_MAX_SGE_PER_SRQ + 1)
59 
60 #define DB_ADDR_SHIFT(addr)		((addr) << DB_PWM_ADDR_OFFSET_SHIFT)
61 
62 enum {
63 	QEDR_USER_MMAP_IO_WC = 0,
64 	QEDR_USER_MMAP_PHYS_PAGE,
65 };
66 
qedr_ib_copy_to_udata(struct ib_udata * udata,void * src,size_t len)67 static inline int qedr_ib_copy_to_udata(struct ib_udata *udata, void *src,
68 					size_t len)
69 {
70 	size_t min_len = min_t(size_t, len, udata->outlen);
71 
72 	return ib_copy_to_udata(udata, src, min_len);
73 }
74 
qedr_query_pkey(struct ib_device * ibdev,u8 port,u16 index,u16 * pkey)75 int qedr_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
76 {
77 	if (index >= QEDR_ROCE_PKEY_TABLE_LEN)
78 		return -EINVAL;
79 
80 	*pkey = QEDR_ROCE_PKEY_DEFAULT;
81 	return 0;
82 }
83 
qedr_iw_query_gid(struct ib_device * ibdev,u8 port,int index,union ib_gid * sgid)84 int qedr_iw_query_gid(struct ib_device *ibdev, u8 port,
85 		      int index, union ib_gid *sgid)
86 {
87 	struct qedr_dev *dev = get_qedr_dev(ibdev);
88 
89 	memset(sgid->raw, 0, sizeof(sgid->raw));
90 	ether_addr_copy(sgid->raw, dev->ndev->dev_addr);
91 
92 	DP_DEBUG(dev, QEDR_MSG_INIT, "QUERY sgid[%d]=%llx:%llx\n", index,
93 		 sgid->global.interface_id, sgid->global.subnet_prefix);
94 
95 	return 0;
96 }
97 
qedr_query_srq(struct ib_srq * ibsrq,struct ib_srq_attr * srq_attr)98 int qedr_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
99 {
100 	struct qedr_dev *dev = get_qedr_dev(ibsrq->device);
101 	struct qedr_device_attr *qattr = &dev->attr;
102 	struct qedr_srq *srq = get_qedr_srq(ibsrq);
103 
104 	srq_attr->srq_limit = srq->srq_limit;
105 	srq_attr->max_wr = qattr->max_srq_wr;
106 	srq_attr->max_sge = qattr->max_sge;
107 
108 	return 0;
109 }
110 
qedr_query_device(struct ib_device * ibdev,struct ib_device_attr * attr,struct ib_udata * udata)111 int qedr_query_device(struct ib_device *ibdev,
112 		      struct ib_device_attr *attr, struct ib_udata *udata)
113 {
114 	struct qedr_dev *dev = get_qedr_dev(ibdev);
115 	struct qedr_device_attr *qattr = &dev->attr;
116 
117 	if (!dev->rdma_ctx) {
118 		DP_ERR(dev,
119 		       "qedr_query_device called with invalid params rdma_ctx=%p\n",
120 		       dev->rdma_ctx);
121 		return -EINVAL;
122 	}
123 
124 	memset(attr, 0, sizeof(*attr));
125 
126 	attr->fw_ver = qattr->fw_ver;
127 	attr->sys_image_guid = qattr->sys_image_guid;
128 	attr->max_mr_size = qattr->max_mr_size;
129 	attr->page_size_cap = qattr->page_size_caps;
130 	attr->vendor_id = qattr->vendor_id;
131 	attr->vendor_part_id = qattr->vendor_part_id;
132 	attr->hw_ver = qattr->hw_ver;
133 	attr->max_qp = qattr->max_qp;
134 	attr->max_qp_wr = max_t(u32, qattr->max_sqe, qattr->max_rqe);
135 	attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD |
136 	    IB_DEVICE_RC_RNR_NAK_GEN |
137 	    IB_DEVICE_LOCAL_DMA_LKEY | IB_DEVICE_MEM_MGT_EXTENSIONS;
138 
139 	if (!rdma_protocol_iwarp(&dev->ibdev, 1))
140 		attr->device_cap_flags |= IB_DEVICE_XRC;
141 	attr->max_send_sge = qattr->max_sge;
142 	attr->max_recv_sge = qattr->max_sge;
143 	attr->max_sge_rd = qattr->max_sge;
144 	attr->max_cq = qattr->max_cq;
145 	attr->max_cqe = qattr->max_cqe;
146 	attr->max_mr = qattr->max_mr;
147 	attr->max_mw = qattr->max_mw;
148 	attr->max_pd = qattr->max_pd;
149 	attr->atomic_cap = dev->atomic_cap;
150 	attr->max_qp_init_rd_atom =
151 	    1 << (fls(qattr->max_qp_req_rd_atomic_resc) - 1);
152 	attr->max_qp_rd_atom =
153 	    min(1 << (fls(qattr->max_qp_resp_rd_atomic_resc) - 1),
154 		attr->max_qp_init_rd_atom);
155 
156 	attr->max_srq = qattr->max_srq;
157 	attr->max_srq_sge = qattr->max_srq_sge;
158 	attr->max_srq_wr = qattr->max_srq_wr;
159 
160 	attr->local_ca_ack_delay = qattr->dev_ack_delay;
161 	attr->max_fast_reg_page_list_len = qattr->max_mr / 8;
162 	attr->max_pkeys = qattr->max_pkey;
163 	attr->max_ah = qattr->max_ah;
164 
165 	return 0;
166 }
167 
get_link_speed_and_width(int speed,u16 * ib_speed,u8 * ib_width)168 static inline void get_link_speed_and_width(int speed, u16 *ib_speed,
169 					    u8 *ib_width)
170 {
171 	switch (speed) {
172 	case 1000:
173 		*ib_speed = IB_SPEED_SDR;
174 		*ib_width = IB_WIDTH_1X;
175 		break;
176 	case 10000:
177 		*ib_speed = IB_SPEED_QDR;
178 		*ib_width = IB_WIDTH_1X;
179 		break;
180 
181 	case 20000:
182 		*ib_speed = IB_SPEED_DDR;
183 		*ib_width = IB_WIDTH_4X;
184 		break;
185 
186 	case 25000:
187 		*ib_speed = IB_SPEED_EDR;
188 		*ib_width = IB_WIDTH_1X;
189 		break;
190 
191 	case 40000:
192 		*ib_speed = IB_SPEED_QDR;
193 		*ib_width = IB_WIDTH_4X;
194 		break;
195 
196 	case 50000:
197 		*ib_speed = IB_SPEED_HDR;
198 		*ib_width = IB_WIDTH_1X;
199 		break;
200 
201 	case 100000:
202 		*ib_speed = IB_SPEED_EDR;
203 		*ib_width = IB_WIDTH_4X;
204 		break;
205 
206 	default:
207 		/* Unsupported */
208 		*ib_speed = IB_SPEED_SDR;
209 		*ib_width = IB_WIDTH_1X;
210 	}
211 }
212 
qedr_query_port(struct ib_device * ibdev,u8 port,struct ib_port_attr * attr)213 int qedr_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *attr)
214 {
215 	struct qedr_dev *dev;
216 	struct qed_rdma_port *rdma_port;
217 
218 	dev = get_qedr_dev(ibdev);
219 
220 	if (!dev->rdma_ctx) {
221 		DP_ERR(dev, "rdma_ctx is NULL\n");
222 		return -EINVAL;
223 	}
224 
225 	rdma_port = dev->ops->rdma_query_port(dev->rdma_ctx);
226 
227 	/* *attr being zeroed by the caller, avoid zeroing it here */
228 	if (rdma_port->port_state == QED_RDMA_PORT_UP) {
229 		attr->state = IB_PORT_ACTIVE;
230 		attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
231 	} else {
232 		attr->state = IB_PORT_DOWN;
233 		attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
234 	}
235 	attr->max_mtu = IB_MTU_4096;
236 	attr->lid = 0;
237 	attr->lmc = 0;
238 	attr->sm_lid = 0;
239 	attr->sm_sl = 0;
240 	attr->ip_gids = true;
241 	if (rdma_protocol_iwarp(&dev->ibdev, 1)) {
242 		attr->active_mtu = iboe_get_mtu(dev->iwarp_max_mtu);
243 		attr->gid_tbl_len = 1;
244 	} else {
245 		attr->active_mtu = iboe_get_mtu(dev->ndev->mtu);
246 		attr->gid_tbl_len = QEDR_MAX_SGID;
247 		attr->pkey_tbl_len = QEDR_ROCE_PKEY_TABLE_LEN;
248 	}
249 	attr->bad_pkey_cntr = rdma_port->pkey_bad_counter;
250 	attr->qkey_viol_cntr = 0;
251 	get_link_speed_and_width(rdma_port->link_speed,
252 				 &attr->active_speed, &attr->active_width);
253 	attr->max_msg_sz = rdma_port->max_msg_size;
254 	attr->max_vl_num = 4;
255 
256 	return 0;
257 }
258 
qedr_alloc_ucontext(struct ib_ucontext * uctx,struct ib_udata * udata)259 int qedr_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
260 {
261 	struct ib_device *ibdev = uctx->device;
262 	int rc;
263 	struct qedr_ucontext *ctx = get_qedr_ucontext(uctx);
264 	struct qedr_alloc_ucontext_resp uresp = {};
265 	struct qedr_alloc_ucontext_req ureq = {};
266 	struct qedr_dev *dev = get_qedr_dev(ibdev);
267 	struct qed_rdma_add_user_out_params oparams;
268 	struct qedr_user_mmap_entry *entry;
269 
270 	if (!udata)
271 		return -EFAULT;
272 
273 	if (udata->inlen) {
274 		rc = ib_copy_from_udata(&ureq, udata,
275 					min(sizeof(ureq), udata->inlen));
276 		if (rc) {
277 			DP_ERR(dev, "Problem copying data from user space\n");
278 			return -EFAULT;
279 		}
280 		ctx->edpm_mode = !!(ureq.context_flags &
281 				    QEDR_ALLOC_UCTX_EDPM_MODE);
282 		ctx->db_rec = !!(ureq.context_flags & QEDR_ALLOC_UCTX_DB_REC);
283 	}
284 
285 	rc = dev->ops->rdma_add_user(dev->rdma_ctx, &oparams);
286 	if (rc) {
287 		DP_ERR(dev,
288 		       "failed to allocate a DPI for a new RoCE application, rc=%d. To overcome this consider to increase the number of DPIs, increase the doorbell BAR size or just close unnecessary RoCE applications. In order to increase the number of DPIs consult the qedr readme\n",
289 		       rc);
290 		return rc;
291 	}
292 
293 	ctx->dpi = oparams.dpi;
294 	ctx->dpi_addr = oparams.dpi_addr;
295 	ctx->dpi_phys_addr = oparams.dpi_phys_addr;
296 	ctx->dpi_size = oparams.dpi_size;
297 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
298 	if (!entry) {
299 		rc = -ENOMEM;
300 		goto err;
301 	}
302 
303 	entry->io_address = ctx->dpi_phys_addr;
304 	entry->length = ctx->dpi_size;
305 	entry->mmap_flag = QEDR_USER_MMAP_IO_WC;
306 	entry->dpi = ctx->dpi;
307 	entry->dev = dev;
308 	rc = rdma_user_mmap_entry_insert(uctx, &entry->rdma_entry,
309 					 ctx->dpi_size);
310 	if (rc) {
311 		kfree(entry);
312 		goto err;
313 	}
314 	ctx->db_mmap_entry = &entry->rdma_entry;
315 
316 	if (!dev->user_dpm_enabled)
317 		uresp.dpm_flags = 0;
318 	else if (rdma_protocol_iwarp(&dev->ibdev, 1))
319 		uresp.dpm_flags = QEDR_DPM_TYPE_IWARP_LEGACY;
320 	else
321 		uresp.dpm_flags = QEDR_DPM_TYPE_ROCE_ENHANCED |
322 				  QEDR_DPM_TYPE_ROCE_LEGACY |
323 				  QEDR_DPM_TYPE_ROCE_EDPM_MODE;
324 
325 	if (ureq.context_flags & QEDR_SUPPORT_DPM_SIZES) {
326 		uresp.dpm_flags |= QEDR_DPM_SIZES_SET;
327 		uresp.ldpm_limit_size = QEDR_LDPM_MAX_SIZE;
328 		uresp.edpm_trans_size = QEDR_EDPM_TRANS_SIZE;
329 		uresp.edpm_limit_size = QEDR_EDPM_MAX_SIZE;
330 	}
331 
332 	uresp.wids_enabled = 1;
333 	uresp.wid_count = oparams.wid_count;
334 	uresp.db_pa = rdma_user_mmap_get_offset(ctx->db_mmap_entry);
335 	uresp.db_size = ctx->dpi_size;
336 	uresp.max_send_wr = dev->attr.max_sqe;
337 	uresp.max_recv_wr = dev->attr.max_rqe;
338 	uresp.max_srq_wr = dev->attr.max_srq_wr;
339 	uresp.sges_per_send_wr = QEDR_MAX_SQE_ELEMENTS_PER_SQE;
340 	uresp.sges_per_recv_wr = QEDR_MAX_RQE_ELEMENTS_PER_RQE;
341 	uresp.sges_per_srq_wr = dev->attr.max_srq_sge;
342 	uresp.max_cqes = QEDR_MAX_CQES;
343 
344 	rc = qedr_ib_copy_to_udata(udata, &uresp, sizeof(uresp));
345 	if (rc)
346 		goto err;
347 
348 	ctx->dev = dev;
349 
350 	DP_DEBUG(dev, QEDR_MSG_INIT, "Allocating user context %p\n",
351 		 &ctx->ibucontext);
352 	return 0;
353 
354 err:
355 	if (!ctx->db_mmap_entry)
356 		dev->ops->rdma_remove_user(dev->rdma_ctx, ctx->dpi);
357 	else
358 		rdma_user_mmap_entry_remove(ctx->db_mmap_entry);
359 
360 	return rc;
361 }
362 
qedr_dealloc_ucontext(struct ib_ucontext * ibctx)363 void qedr_dealloc_ucontext(struct ib_ucontext *ibctx)
364 {
365 	struct qedr_ucontext *uctx = get_qedr_ucontext(ibctx);
366 
367 	DP_DEBUG(uctx->dev, QEDR_MSG_INIT, "Deallocating user context %p\n",
368 		 uctx);
369 
370 	rdma_user_mmap_entry_remove(uctx->db_mmap_entry);
371 }
372 
qedr_mmap_free(struct rdma_user_mmap_entry * rdma_entry)373 void qedr_mmap_free(struct rdma_user_mmap_entry *rdma_entry)
374 {
375 	struct qedr_user_mmap_entry *entry = get_qedr_mmap_entry(rdma_entry);
376 	struct qedr_dev *dev = entry->dev;
377 
378 	if (entry->mmap_flag == QEDR_USER_MMAP_PHYS_PAGE)
379 		free_page((unsigned long)entry->address);
380 	else if (entry->mmap_flag == QEDR_USER_MMAP_IO_WC)
381 		dev->ops->rdma_remove_user(dev->rdma_ctx, entry->dpi);
382 
383 	kfree(entry);
384 }
385 
qedr_mmap(struct ib_ucontext * ucontext,struct vm_area_struct * vma)386 int qedr_mmap(struct ib_ucontext *ucontext, struct vm_area_struct *vma)
387 {
388 	struct ib_device *dev = ucontext->device;
389 	size_t length = vma->vm_end - vma->vm_start;
390 	struct rdma_user_mmap_entry *rdma_entry;
391 	struct qedr_user_mmap_entry *entry;
392 	int rc = 0;
393 	u64 pfn;
394 
395 	ibdev_dbg(dev,
396 		  "start %#lx, end %#lx, length = %#zx, pgoff = %#lx\n",
397 		  vma->vm_start, vma->vm_end, length, vma->vm_pgoff);
398 
399 	rdma_entry = rdma_user_mmap_entry_get(ucontext, vma);
400 	if (!rdma_entry) {
401 		ibdev_dbg(dev, "pgoff[%#lx] does not have valid entry\n",
402 			  vma->vm_pgoff);
403 		return -EINVAL;
404 	}
405 	entry = get_qedr_mmap_entry(rdma_entry);
406 	ibdev_dbg(dev,
407 		  "Mapping address[%#llx], length[%#zx], mmap_flag[%d]\n",
408 		  entry->io_address, length, entry->mmap_flag);
409 
410 	switch (entry->mmap_flag) {
411 	case QEDR_USER_MMAP_IO_WC:
412 		pfn = entry->io_address >> PAGE_SHIFT;
413 		rc = rdma_user_mmap_io(ucontext, vma, pfn, length,
414 				       pgprot_writecombine(vma->vm_page_prot),
415 				       rdma_entry);
416 		break;
417 	case QEDR_USER_MMAP_PHYS_PAGE:
418 		rc = vm_insert_page(vma, vma->vm_start,
419 				    virt_to_page(entry->address));
420 		break;
421 	default:
422 		rc = -EINVAL;
423 	}
424 
425 	if (rc)
426 		ibdev_dbg(dev,
427 			  "Couldn't mmap address[%#llx] length[%#zx] mmap_flag[%d] err[%d]\n",
428 			  entry->io_address, length, entry->mmap_flag, rc);
429 
430 	rdma_user_mmap_entry_put(rdma_entry);
431 	return rc;
432 }
433 
qedr_alloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)434 int qedr_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
435 {
436 	struct ib_device *ibdev = ibpd->device;
437 	struct qedr_dev *dev = get_qedr_dev(ibdev);
438 	struct qedr_pd *pd = get_qedr_pd(ibpd);
439 	u16 pd_id;
440 	int rc;
441 
442 	DP_DEBUG(dev, QEDR_MSG_INIT, "Function called from: %s\n",
443 		 udata ? "User Lib" : "Kernel");
444 
445 	if (!dev->rdma_ctx) {
446 		DP_ERR(dev, "invalid RDMA context\n");
447 		return -EINVAL;
448 	}
449 
450 	rc = dev->ops->rdma_alloc_pd(dev->rdma_ctx, &pd_id);
451 	if (rc)
452 		return rc;
453 
454 	pd->pd_id = pd_id;
455 
456 	if (udata) {
457 		struct qedr_alloc_pd_uresp uresp = {
458 			.pd_id = pd_id,
459 		};
460 		struct qedr_ucontext *context = rdma_udata_to_drv_context(
461 			udata, struct qedr_ucontext, ibucontext);
462 
463 		rc = qedr_ib_copy_to_udata(udata, &uresp, sizeof(uresp));
464 		if (rc) {
465 			DP_ERR(dev, "copy error pd_id=0x%x.\n", pd_id);
466 			dev->ops->rdma_dealloc_pd(dev->rdma_ctx, pd_id);
467 			return rc;
468 		}
469 
470 		pd->uctx = context;
471 		pd->uctx->pd = pd;
472 	}
473 
474 	return 0;
475 }
476 
qedr_dealloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)477 int qedr_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
478 {
479 	struct qedr_dev *dev = get_qedr_dev(ibpd->device);
480 	struct qedr_pd *pd = get_qedr_pd(ibpd);
481 
482 	DP_DEBUG(dev, QEDR_MSG_INIT, "Deallocating PD %d\n", pd->pd_id);
483 	dev->ops->rdma_dealloc_pd(dev->rdma_ctx, pd->pd_id);
484 	return 0;
485 }
486 
487 
qedr_alloc_xrcd(struct ib_xrcd * ibxrcd,struct ib_udata * udata)488 int qedr_alloc_xrcd(struct ib_xrcd *ibxrcd, struct ib_udata *udata)
489 {
490 	struct qedr_dev *dev = get_qedr_dev(ibxrcd->device);
491 	struct qedr_xrcd *xrcd = get_qedr_xrcd(ibxrcd);
492 
493 	return dev->ops->rdma_alloc_xrcd(dev->rdma_ctx, &xrcd->xrcd_id);
494 }
495 
qedr_dealloc_xrcd(struct ib_xrcd * ibxrcd,struct ib_udata * udata)496 int qedr_dealloc_xrcd(struct ib_xrcd *ibxrcd, struct ib_udata *udata)
497 {
498 	struct qedr_dev *dev = get_qedr_dev(ibxrcd->device);
499 	u16 xrcd_id = get_qedr_xrcd(ibxrcd)->xrcd_id;
500 
501 	dev->ops->rdma_dealloc_xrcd(dev->rdma_ctx, xrcd_id);
502 	return 0;
503 }
qedr_free_pbl(struct qedr_dev * dev,struct qedr_pbl_info * pbl_info,struct qedr_pbl * pbl)504 static void qedr_free_pbl(struct qedr_dev *dev,
505 			  struct qedr_pbl_info *pbl_info, struct qedr_pbl *pbl)
506 {
507 	struct pci_dev *pdev = dev->pdev;
508 	int i;
509 
510 	for (i = 0; i < pbl_info->num_pbls; i++) {
511 		if (!pbl[i].va)
512 			continue;
513 		dma_free_coherent(&pdev->dev, pbl_info->pbl_size,
514 				  pbl[i].va, pbl[i].pa);
515 	}
516 
517 	kfree(pbl);
518 }
519 
520 #define MIN_FW_PBL_PAGE_SIZE (4 * 1024)
521 #define MAX_FW_PBL_PAGE_SIZE (64 * 1024)
522 
523 #define NUM_PBES_ON_PAGE(_page_size) (_page_size / sizeof(u64))
524 #define MAX_PBES_ON_PAGE NUM_PBES_ON_PAGE(MAX_FW_PBL_PAGE_SIZE)
525 #define MAX_PBES_TWO_LAYER (MAX_PBES_ON_PAGE * MAX_PBES_ON_PAGE)
526 
qedr_alloc_pbl_tbl(struct qedr_dev * dev,struct qedr_pbl_info * pbl_info,gfp_t flags)527 static struct qedr_pbl *qedr_alloc_pbl_tbl(struct qedr_dev *dev,
528 					   struct qedr_pbl_info *pbl_info,
529 					   gfp_t flags)
530 {
531 	struct pci_dev *pdev = dev->pdev;
532 	struct qedr_pbl *pbl_table;
533 	dma_addr_t *pbl_main_tbl;
534 	dma_addr_t pa;
535 	void *va;
536 	int i;
537 
538 	pbl_table = kcalloc(pbl_info->num_pbls, sizeof(*pbl_table), flags);
539 	if (!pbl_table)
540 		return ERR_PTR(-ENOMEM);
541 
542 	for (i = 0; i < pbl_info->num_pbls; i++) {
543 		va = dma_alloc_coherent(&pdev->dev, pbl_info->pbl_size, &pa,
544 					flags);
545 		if (!va)
546 			goto err;
547 
548 		pbl_table[i].va = va;
549 		pbl_table[i].pa = pa;
550 	}
551 
552 	/* Two-Layer PBLs, if we have more than one pbl we need to initialize
553 	 * the first one with physical pointers to all of the rest
554 	 */
555 	pbl_main_tbl = (dma_addr_t *)pbl_table[0].va;
556 	for (i = 0; i < pbl_info->num_pbls - 1; i++)
557 		pbl_main_tbl[i] = pbl_table[i + 1].pa;
558 
559 	return pbl_table;
560 
561 err:
562 	for (i--; i >= 0; i--)
563 		dma_free_coherent(&pdev->dev, pbl_info->pbl_size,
564 				  pbl_table[i].va, pbl_table[i].pa);
565 
566 	qedr_free_pbl(dev, pbl_info, pbl_table);
567 
568 	return ERR_PTR(-ENOMEM);
569 }
570 
qedr_prepare_pbl_tbl(struct qedr_dev * dev,struct qedr_pbl_info * pbl_info,u32 num_pbes,int two_layer_capable)571 static int qedr_prepare_pbl_tbl(struct qedr_dev *dev,
572 				struct qedr_pbl_info *pbl_info,
573 				u32 num_pbes, int two_layer_capable)
574 {
575 	u32 pbl_capacity;
576 	u32 pbl_size;
577 	u32 num_pbls;
578 
579 	if ((num_pbes > MAX_PBES_ON_PAGE) && two_layer_capable) {
580 		if (num_pbes > MAX_PBES_TWO_LAYER) {
581 			DP_ERR(dev, "prepare pbl table: too many pages %d\n",
582 			       num_pbes);
583 			return -EINVAL;
584 		}
585 
586 		/* calculate required pbl page size */
587 		pbl_size = MIN_FW_PBL_PAGE_SIZE;
588 		pbl_capacity = NUM_PBES_ON_PAGE(pbl_size) *
589 			       NUM_PBES_ON_PAGE(pbl_size);
590 
591 		while (pbl_capacity < num_pbes) {
592 			pbl_size *= 2;
593 			pbl_capacity = pbl_size / sizeof(u64);
594 			pbl_capacity = pbl_capacity * pbl_capacity;
595 		}
596 
597 		num_pbls = DIV_ROUND_UP(num_pbes, NUM_PBES_ON_PAGE(pbl_size));
598 		num_pbls++;	/* One for the layer0 ( points to the pbls) */
599 		pbl_info->two_layered = true;
600 	} else {
601 		/* One layered PBL */
602 		num_pbls = 1;
603 		pbl_size = max_t(u32, MIN_FW_PBL_PAGE_SIZE,
604 				 roundup_pow_of_two((num_pbes * sizeof(u64))));
605 		pbl_info->two_layered = false;
606 	}
607 
608 	pbl_info->num_pbls = num_pbls;
609 	pbl_info->pbl_size = pbl_size;
610 	pbl_info->num_pbes = num_pbes;
611 
612 	DP_DEBUG(dev, QEDR_MSG_MR,
613 		 "prepare pbl table: num_pbes=%d, num_pbls=%d, pbl_size=%d\n",
614 		 pbl_info->num_pbes, pbl_info->num_pbls, pbl_info->pbl_size);
615 
616 	return 0;
617 }
618 
qedr_populate_pbls(struct qedr_dev * dev,struct ib_umem * umem,struct qedr_pbl * pbl,struct qedr_pbl_info * pbl_info,u32 pg_shift)619 static void qedr_populate_pbls(struct qedr_dev *dev, struct ib_umem *umem,
620 			       struct qedr_pbl *pbl,
621 			       struct qedr_pbl_info *pbl_info, u32 pg_shift)
622 {
623 	int pbe_cnt, total_num_pbes = 0;
624 	struct qedr_pbl *pbl_tbl;
625 	struct ib_block_iter biter;
626 	struct regpair *pbe;
627 
628 	if (!pbl_info->num_pbes)
629 		return;
630 
631 	/* If we have a two layered pbl, the first pbl points to the rest
632 	 * of the pbls and the first entry lays on the second pbl in the table
633 	 */
634 	if (pbl_info->two_layered)
635 		pbl_tbl = &pbl[1];
636 	else
637 		pbl_tbl = pbl;
638 
639 	pbe = (struct regpair *)pbl_tbl->va;
640 	if (!pbe) {
641 		DP_ERR(dev, "cannot populate PBL due to a NULL PBE\n");
642 		return;
643 	}
644 
645 	pbe_cnt = 0;
646 
647 	rdma_umem_for_each_dma_block (umem, &biter, BIT(pg_shift)) {
648 		u64 pg_addr = rdma_block_iter_dma_address(&biter);
649 
650 		pbe->lo = cpu_to_le32(pg_addr);
651 		pbe->hi = cpu_to_le32(upper_32_bits(pg_addr));
652 
653 		pbe_cnt++;
654 		total_num_pbes++;
655 		pbe++;
656 
657 		if (total_num_pbes == pbl_info->num_pbes)
658 			return;
659 
660 		/* If the given pbl is full storing the pbes, move to next pbl.
661 		 */
662 		if (pbe_cnt == (pbl_info->pbl_size / sizeof(u64))) {
663 			pbl_tbl++;
664 			pbe = (struct regpair *)pbl_tbl->va;
665 			pbe_cnt = 0;
666 		}
667 	}
668 }
669 
qedr_db_recovery_add(struct qedr_dev * dev,void __iomem * db_addr,void * db_data,enum qed_db_rec_width db_width,enum qed_db_rec_space db_space)670 static int qedr_db_recovery_add(struct qedr_dev *dev,
671 				void __iomem *db_addr,
672 				void *db_data,
673 				enum qed_db_rec_width db_width,
674 				enum qed_db_rec_space db_space)
675 {
676 	if (!db_data) {
677 		DP_DEBUG(dev, QEDR_MSG_INIT, "avoiding db rec since old lib\n");
678 		return 0;
679 	}
680 
681 	return dev->ops->common->db_recovery_add(dev->cdev, db_addr, db_data,
682 						 db_width, db_space);
683 }
684 
qedr_db_recovery_del(struct qedr_dev * dev,void __iomem * db_addr,void * db_data)685 static void qedr_db_recovery_del(struct qedr_dev *dev,
686 				 void __iomem *db_addr,
687 				 void *db_data)
688 {
689 	if (!db_data) {
690 		DP_DEBUG(dev, QEDR_MSG_INIT, "avoiding db rec since old lib\n");
691 		return;
692 	}
693 
694 	/* Ignore return code as there is not much we can do about it. Error
695 	 * log will be printed inside.
696 	 */
697 	dev->ops->common->db_recovery_del(dev->cdev, db_addr, db_data);
698 }
699 
qedr_copy_cq_uresp(struct qedr_dev * dev,struct qedr_cq * cq,struct ib_udata * udata,u32 db_offset)700 static int qedr_copy_cq_uresp(struct qedr_dev *dev,
701 			      struct qedr_cq *cq, struct ib_udata *udata,
702 			      u32 db_offset)
703 {
704 	struct qedr_create_cq_uresp uresp;
705 	int rc;
706 
707 	memset(&uresp, 0, sizeof(uresp));
708 
709 	uresp.db_offset = db_offset;
710 	uresp.icid = cq->icid;
711 	if (cq->q.db_mmap_entry)
712 		uresp.db_rec_addr =
713 			rdma_user_mmap_get_offset(cq->q.db_mmap_entry);
714 
715 	rc = qedr_ib_copy_to_udata(udata, &uresp, sizeof(uresp));
716 	if (rc)
717 		DP_ERR(dev, "copy error cqid=0x%x.\n", cq->icid);
718 
719 	return rc;
720 }
721 
consume_cqe(struct qedr_cq * cq)722 static void consume_cqe(struct qedr_cq *cq)
723 {
724 	if (cq->latest_cqe == cq->toggle_cqe)
725 		cq->pbl_toggle ^= RDMA_CQE_REQUESTER_TOGGLE_BIT_MASK;
726 
727 	cq->latest_cqe = qed_chain_consume(&cq->pbl);
728 }
729 
qedr_align_cq_entries(int entries)730 static inline int qedr_align_cq_entries(int entries)
731 {
732 	u64 size, aligned_size;
733 
734 	/* We allocate an extra entry that we don't report to the FW. */
735 	size = (entries + 1) * QEDR_CQE_SIZE;
736 	aligned_size = ALIGN(size, PAGE_SIZE);
737 
738 	return aligned_size / QEDR_CQE_SIZE;
739 }
740 
qedr_init_user_db_rec(struct ib_udata * udata,struct qedr_dev * dev,struct qedr_userq * q,bool requires_db_rec)741 static int qedr_init_user_db_rec(struct ib_udata *udata,
742 				 struct qedr_dev *dev, struct qedr_userq *q,
743 				 bool requires_db_rec)
744 {
745 	struct qedr_ucontext *uctx =
746 		rdma_udata_to_drv_context(udata, struct qedr_ucontext,
747 					  ibucontext);
748 	struct qedr_user_mmap_entry *entry;
749 	int rc;
750 
751 	/* Aborting for non doorbell userqueue (SRQ) or non-supporting lib */
752 	if (requires_db_rec == 0 || !uctx->db_rec)
753 		return 0;
754 
755 	/* Allocate a page for doorbell recovery, add to mmap */
756 	q->db_rec_data = (void *)get_zeroed_page(GFP_USER);
757 	if (!q->db_rec_data) {
758 		DP_ERR(dev, "get_zeroed_page failed\n");
759 		return -ENOMEM;
760 	}
761 
762 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
763 	if (!entry)
764 		goto err_free_db_data;
765 
766 	entry->address = q->db_rec_data;
767 	entry->length = PAGE_SIZE;
768 	entry->mmap_flag = QEDR_USER_MMAP_PHYS_PAGE;
769 	rc = rdma_user_mmap_entry_insert(&uctx->ibucontext,
770 					 &entry->rdma_entry,
771 					 PAGE_SIZE);
772 	if (rc)
773 		goto err_free_entry;
774 
775 	q->db_mmap_entry = &entry->rdma_entry;
776 
777 	return 0;
778 
779 err_free_entry:
780 	kfree(entry);
781 
782 err_free_db_data:
783 	free_page((unsigned long)q->db_rec_data);
784 	q->db_rec_data = NULL;
785 	return -ENOMEM;
786 }
787 
qedr_init_user_queue(struct ib_udata * udata,struct qedr_dev * dev,struct qedr_userq * q,u64 buf_addr,size_t buf_len,bool requires_db_rec,int access,int alloc_and_init)788 static inline int qedr_init_user_queue(struct ib_udata *udata,
789 				       struct qedr_dev *dev,
790 				       struct qedr_userq *q, u64 buf_addr,
791 				       size_t buf_len, bool requires_db_rec,
792 				       int access,
793 				       int alloc_and_init)
794 {
795 	u32 fw_pages;
796 	int rc;
797 
798 	q->buf_addr = buf_addr;
799 	q->buf_len = buf_len;
800 	q->umem = ib_umem_get(&dev->ibdev, q->buf_addr, q->buf_len, access);
801 	if (IS_ERR(q->umem)) {
802 		DP_ERR(dev, "create user queue: failed ib_umem_get, got %ld\n",
803 		       PTR_ERR(q->umem));
804 		return PTR_ERR(q->umem);
805 	}
806 
807 	fw_pages = ib_umem_num_dma_blocks(q->umem, 1 << FW_PAGE_SHIFT);
808 	rc = qedr_prepare_pbl_tbl(dev, &q->pbl_info, fw_pages, 0);
809 	if (rc)
810 		goto err0;
811 
812 	if (alloc_and_init) {
813 		q->pbl_tbl = qedr_alloc_pbl_tbl(dev, &q->pbl_info, GFP_KERNEL);
814 		if (IS_ERR(q->pbl_tbl)) {
815 			rc = PTR_ERR(q->pbl_tbl);
816 			goto err0;
817 		}
818 		qedr_populate_pbls(dev, q->umem, q->pbl_tbl, &q->pbl_info,
819 				   FW_PAGE_SHIFT);
820 	} else {
821 		q->pbl_tbl = kzalloc(sizeof(*q->pbl_tbl), GFP_KERNEL);
822 		if (!q->pbl_tbl) {
823 			rc = -ENOMEM;
824 			goto err0;
825 		}
826 	}
827 
828 	/* mmap the user address used to store doorbell data for recovery */
829 	return qedr_init_user_db_rec(udata, dev, q, requires_db_rec);
830 
831 err0:
832 	ib_umem_release(q->umem);
833 	q->umem = NULL;
834 
835 	return rc;
836 }
837 
qedr_init_cq_params(struct qedr_cq * cq,struct qedr_ucontext * ctx,struct qedr_dev * dev,int vector,int chain_entries,int page_cnt,u64 pbl_ptr,struct qed_rdma_create_cq_in_params * params)838 static inline void qedr_init_cq_params(struct qedr_cq *cq,
839 				       struct qedr_ucontext *ctx,
840 				       struct qedr_dev *dev, int vector,
841 				       int chain_entries, int page_cnt,
842 				       u64 pbl_ptr,
843 				       struct qed_rdma_create_cq_in_params
844 				       *params)
845 {
846 	memset(params, 0, sizeof(*params));
847 	params->cq_handle_hi = upper_32_bits((uintptr_t)cq);
848 	params->cq_handle_lo = lower_32_bits((uintptr_t)cq);
849 	params->cnq_id = vector;
850 	params->cq_size = chain_entries - 1;
851 	params->dpi = (ctx) ? ctx->dpi : dev->dpi;
852 	params->pbl_num_pages = page_cnt;
853 	params->pbl_ptr = pbl_ptr;
854 	params->pbl_two_level = 0;
855 }
856 
doorbell_cq(struct qedr_cq * cq,u32 cons,u8 flags)857 static void doorbell_cq(struct qedr_cq *cq, u32 cons, u8 flags)
858 {
859 	cq->db.data.agg_flags = flags;
860 	cq->db.data.value = cpu_to_le32(cons);
861 	writeq(cq->db.raw, cq->db_addr);
862 }
863 
qedr_arm_cq(struct ib_cq * ibcq,enum ib_cq_notify_flags flags)864 int qedr_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
865 {
866 	struct qedr_cq *cq = get_qedr_cq(ibcq);
867 	unsigned long sflags;
868 	struct qedr_dev *dev;
869 
870 	dev = get_qedr_dev(ibcq->device);
871 
872 	if (cq->destroyed) {
873 		DP_ERR(dev,
874 		       "warning: arm was invoked after destroy for cq %p (icid=%d)\n",
875 		       cq, cq->icid);
876 		return -EINVAL;
877 	}
878 
879 
880 	if (cq->cq_type == QEDR_CQ_TYPE_GSI)
881 		return 0;
882 
883 	spin_lock_irqsave(&cq->cq_lock, sflags);
884 
885 	cq->arm_flags = 0;
886 
887 	if (flags & IB_CQ_SOLICITED)
888 		cq->arm_flags |= DQ_UCM_ROCE_CQ_ARM_SE_CF_CMD;
889 
890 	if (flags & IB_CQ_NEXT_COMP)
891 		cq->arm_flags |= DQ_UCM_ROCE_CQ_ARM_CF_CMD;
892 
893 	doorbell_cq(cq, cq->cq_cons - 1, cq->arm_flags);
894 
895 	spin_unlock_irqrestore(&cq->cq_lock, sflags);
896 
897 	return 0;
898 }
899 
qedr_create_cq(struct ib_cq * ibcq,const struct ib_cq_init_attr * attr,struct ib_udata * udata)900 int qedr_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
901 		   struct ib_udata *udata)
902 {
903 	struct ib_device *ibdev = ibcq->device;
904 	struct qedr_ucontext *ctx = rdma_udata_to_drv_context(
905 		udata, struct qedr_ucontext, ibucontext);
906 	struct qed_rdma_destroy_cq_out_params destroy_oparams;
907 	struct qed_rdma_destroy_cq_in_params destroy_iparams;
908 	struct qed_chain_init_params chain_params = {
909 		.mode		= QED_CHAIN_MODE_PBL,
910 		.intended_use	= QED_CHAIN_USE_TO_CONSUME,
911 		.cnt_type	= QED_CHAIN_CNT_TYPE_U32,
912 		.elem_size	= sizeof(union rdma_cqe),
913 	};
914 	struct qedr_dev *dev = get_qedr_dev(ibdev);
915 	struct qed_rdma_create_cq_in_params params;
916 	struct qedr_create_cq_ureq ureq = {};
917 	int vector = attr->comp_vector;
918 	int entries = attr->cqe;
919 	struct qedr_cq *cq = get_qedr_cq(ibcq);
920 	int chain_entries;
921 	u32 db_offset;
922 	int page_cnt;
923 	u64 pbl_ptr;
924 	u16 icid;
925 	int rc;
926 
927 	DP_DEBUG(dev, QEDR_MSG_INIT,
928 		 "create_cq: called from %s. entries=%d, vector=%d\n",
929 		 udata ? "User Lib" : "Kernel", entries, vector);
930 
931 	if (entries > QEDR_MAX_CQES) {
932 		DP_ERR(dev,
933 		       "create cq: the number of entries %d is too high. Must be equal or below %d.\n",
934 		       entries, QEDR_MAX_CQES);
935 		return -EINVAL;
936 	}
937 
938 	chain_entries = qedr_align_cq_entries(entries);
939 	chain_entries = min_t(int, chain_entries, QEDR_MAX_CQES);
940 	chain_params.num_elems = chain_entries;
941 
942 	/* calc db offset. user will add DPI base, kernel will add db addr */
943 	db_offset = DB_ADDR_SHIFT(DQ_PWM_OFFSET_UCM_RDMA_CQ_CONS_32BIT);
944 
945 	if (udata) {
946 		if (ib_copy_from_udata(&ureq, udata, min(sizeof(ureq),
947 							 udata->inlen))) {
948 			DP_ERR(dev,
949 			       "create cq: problem copying data from user space\n");
950 			goto err0;
951 		}
952 
953 		if (!ureq.len) {
954 			DP_ERR(dev,
955 			       "create cq: cannot create a cq with 0 entries\n");
956 			goto err0;
957 		}
958 
959 		cq->cq_type = QEDR_CQ_TYPE_USER;
960 
961 		rc = qedr_init_user_queue(udata, dev, &cq->q, ureq.addr,
962 					  ureq.len, true, IB_ACCESS_LOCAL_WRITE,
963 					  1);
964 		if (rc)
965 			goto err0;
966 
967 		pbl_ptr = cq->q.pbl_tbl->pa;
968 		page_cnt = cq->q.pbl_info.num_pbes;
969 
970 		cq->ibcq.cqe = chain_entries;
971 		cq->q.db_addr = ctx->dpi_addr + db_offset;
972 	} else {
973 		cq->cq_type = QEDR_CQ_TYPE_KERNEL;
974 
975 		rc = dev->ops->common->chain_alloc(dev->cdev, &cq->pbl,
976 						   &chain_params);
977 		if (rc)
978 			goto err0;
979 
980 		page_cnt = qed_chain_get_page_cnt(&cq->pbl);
981 		pbl_ptr = qed_chain_get_pbl_phys(&cq->pbl);
982 		cq->ibcq.cqe = cq->pbl.capacity;
983 	}
984 
985 	qedr_init_cq_params(cq, ctx, dev, vector, chain_entries, page_cnt,
986 			    pbl_ptr, &params);
987 
988 	rc = dev->ops->rdma_create_cq(dev->rdma_ctx, &params, &icid);
989 	if (rc)
990 		goto err1;
991 
992 	cq->icid = icid;
993 	cq->sig = QEDR_CQ_MAGIC_NUMBER;
994 	spin_lock_init(&cq->cq_lock);
995 
996 	if (udata) {
997 		rc = qedr_copy_cq_uresp(dev, cq, udata, db_offset);
998 		if (rc)
999 			goto err2;
1000 
1001 		rc = qedr_db_recovery_add(dev, cq->q.db_addr,
1002 					  &cq->q.db_rec_data->db_data,
1003 					  DB_REC_WIDTH_64B,
1004 					  DB_REC_USER);
1005 		if (rc)
1006 			goto err2;
1007 
1008 	} else {
1009 		/* Generate doorbell address. */
1010 		cq->db.data.icid = cq->icid;
1011 		cq->db_addr = dev->db_addr + db_offset;
1012 		cq->db.data.params = DB_AGG_CMD_MAX <<
1013 		    RDMA_PWM_VAL32_DATA_AGG_CMD_SHIFT;
1014 
1015 		/* point to the very last element, passing it we will toggle */
1016 		cq->toggle_cqe = qed_chain_get_last_elem(&cq->pbl);
1017 		cq->pbl_toggle = RDMA_CQE_REQUESTER_TOGGLE_BIT_MASK;
1018 		cq->latest_cqe = NULL;
1019 		consume_cqe(cq);
1020 		cq->cq_cons = qed_chain_get_cons_idx_u32(&cq->pbl);
1021 
1022 		rc = qedr_db_recovery_add(dev, cq->db_addr, &cq->db.data,
1023 					  DB_REC_WIDTH_64B, DB_REC_KERNEL);
1024 		if (rc)
1025 			goto err2;
1026 	}
1027 
1028 	DP_DEBUG(dev, QEDR_MSG_CQ,
1029 		 "create cq: icid=0x%0x, addr=%p, size(entries)=0x%0x\n",
1030 		 cq->icid, cq, params.cq_size);
1031 
1032 	return 0;
1033 
1034 err2:
1035 	destroy_iparams.icid = cq->icid;
1036 	dev->ops->rdma_destroy_cq(dev->rdma_ctx, &destroy_iparams,
1037 				  &destroy_oparams);
1038 err1:
1039 	if (udata) {
1040 		qedr_free_pbl(dev, &cq->q.pbl_info, cq->q.pbl_tbl);
1041 		ib_umem_release(cq->q.umem);
1042 		if (cq->q.db_mmap_entry)
1043 			rdma_user_mmap_entry_remove(cq->q.db_mmap_entry);
1044 	} else {
1045 		dev->ops->common->chain_free(dev->cdev, &cq->pbl);
1046 	}
1047 err0:
1048 	return -EINVAL;
1049 }
1050 
qedr_resize_cq(struct ib_cq * ibcq,int new_cnt,struct ib_udata * udata)1051 int qedr_resize_cq(struct ib_cq *ibcq, int new_cnt, struct ib_udata *udata)
1052 {
1053 	struct qedr_dev *dev = get_qedr_dev(ibcq->device);
1054 	struct qedr_cq *cq = get_qedr_cq(ibcq);
1055 
1056 	DP_ERR(dev, "cq %p RESIZE NOT SUPPORTED\n", cq);
1057 
1058 	return 0;
1059 }
1060 
1061 #define QEDR_DESTROY_CQ_MAX_ITERATIONS		(10)
1062 #define QEDR_DESTROY_CQ_ITER_DURATION		(10)
1063 
qedr_destroy_cq(struct ib_cq * ibcq,struct ib_udata * udata)1064 int qedr_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
1065 {
1066 	struct qedr_dev *dev = get_qedr_dev(ibcq->device);
1067 	struct qed_rdma_destroy_cq_out_params oparams;
1068 	struct qed_rdma_destroy_cq_in_params iparams;
1069 	struct qedr_cq *cq = get_qedr_cq(ibcq);
1070 	int iter;
1071 
1072 	DP_DEBUG(dev, QEDR_MSG_CQ, "destroy cq %p (icid=%d)\n", cq, cq->icid);
1073 
1074 	cq->destroyed = 1;
1075 
1076 	/* GSIs CQs are handled by driver, so they don't exist in the FW */
1077 	if (cq->cq_type == QEDR_CQ_TYPE_GSI) {
1078 		qedr_db_recovery_del(dev, cq->db_addr, &cq->db.data);
1079 		return 0;
1080 	}
1081 
1082 	iparams.icid = cq->icid;
1083 	dev->ops->rdma_destroy_cq(dev->rdma_ctx, &iparams, &oparams);
1084 	dev->ops->common->chain_free(dev->cdev, &cq->pbl);
1085 
1086 	if (udata) {
1087 		qedr_free_pbl(dev, &cq->q.pbl_info, cq->q.pbl_tbl);
1088 		ib_umem_release(cq->q.umem);
1089 
1090 		if (cq->q.db_rec_data) {
1091 			qedr_db_recovery_del(dev, cq->q.db_addr,
1092 					     &cq->q.db_rec_data->db_data);
1093 			rdma_user_mmap_entry_remove(cq->q.db_mmap_entry);
1094 		}
1095 	} else {
1096 		qedr_db_recovery_del(dev, cq->db_addr, &cq->db.data);
1097 	}
1098 
1099 	/* We don't want the IRQ handler to handle a non-existing CQ so we
1100 	 * wait until all CNQ interrupts, if any, are received. This will always
1101 	 * happen and will always happen very fast. If not, then a serious error
1102 	 * has occured. That is why we can use a long delay.
1103 	 * We spin for a short time so we don’t lose time on context switching
1104 	 * in case all the completions are handled in that span. Otherwise
1105 	 * we sleep for a while and check again. Since the CNQ may be
1106 	 * associated with (only) the current CPU we use msleep to allow the
1107 	 * current CPU to be freed.
1108 	 * The CNQ notification is increased in qedr_irq_handler().
1109 	 */
1110 	iter = QEDR_DESTROY_CQ_MAX_ITERATIONS;
1111 	while (oparams.num_cq_notif != READ_ONCE(cq->cnq_notif) && iter) {
1112 		udelay(QEDR_DESTROY_CQ_ITER_DURATION);
1113 		iter--;
1114 	}
1115 
1116 	iter = QEDR_DESTROY_CQ_MAX_ITERATIONS;
1117 	while (oparams.num_cq_notif != READ_ONCE(cq->cnq_notif) && iter) {
1118 		msleep(QEDR_DESTROY_CQ_ITER_DURATION);
1119 		iter--;
1120 	}
1121 
1122 	/* Note that we don't need to have explicit code to wait for the
1123 	 * completion of the event handler because it is invoked from the EQ.
1124 	 * Since the destroy CQ ramrod has also been received on the EQ we can
1125 	 * be certain that there's no event handler in process.
1126 	 */
1127 	return 0;
1128 }
1129 
get_gid_info_from_table(struct ib_qp * ibqp,struct ib_qp_attr * attr,int attr_mask,struct qed_rdma_modify_qp_in_params * qp_params)1130 static inline int get_gid_info_from_table(struct ib_qp *ibqp,
1131 					  struct ib_qp_attr *attr,
1132 					  int attr_mask,
1133 					  struct qed_rdma_modify_qp_in_params
1134 					  *qp_params)
1135 {
1136 	const struct ib_gid_attr *gid_attr;
1137 	enum rdma_network_type nw_type;
1138 	const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
1139 	u32 ipv4_addr;
1140 	int ret;
1141 	int i;
1142 
1143 	gid_attr = grh->sgid_attr;
1144 	ret = rdma_read_gid_l2_fields(gid_attr, &qp_params->vlan_id, NULL);
1145 	if (ret)
1146 		return ret;
1147 
1148 	nw_type = rdma_gid_attr_network_type(gid_attr);
1149 	switch (nw_type) {
1150 	case RDMA_NETWORK_IPV6:
1151 		memcpy(&qp_params->sgid.bytes[0], &gid_attr->gid.raw[0],
1152 		       sizeof(qp_params->sgid));
1153 		memcpy(&qp_params->dgid.bytes[0],
1154 		       &grh->dgid,
1155 		       sizeof(qp_params->dgid));
1156 		qp_params->roce_mode = ROCE_V2_IPV6;
1157 		SET_FIELD(qp_params->modify_flags,
1158 			  QED_ROCE_MODIFY_QP_VALID_ROCE_MODE, 1);
1159 		break;
1160 	case RDMA_NETWORK_ROCE_V1:
1161 		memcpy(&qp_params->sgid.bytes[0], &gid_attr->gid.raw[0],
1162 		       sizeof(qp_params->sgid));
1163 		memcpy(&qp_params->dgid.bytes[0],
1164 		       &grh->dgid,
1165 		       sizeof(qp_params->dgid));
1166 		qp_params->roce_mode = ROCE_V1;
1167 		break;
1168 	case RDMA_NETWORK_IPV4:
1169 		memset(&qp_params->sgid, 0, sizeof(qp_params->sgid));
1170 		memset(&qp_params->dgid, 0, sizeof(qp_params->dgid));
1171 		ipv4_addr = qedr_get_ipv4_from_gid(gid_attr->gid.raw);
1172 		qp_params->sgid.ipv4_addr = ipv4_addr;
1173 		ipv4_addr =
1174 		    qedr_get_ipv4_from_gid(grh->dgid.raw);
1175 		qp_params->dgid.ipv4_addr = ipv4_addr;
1176 		SET_FIELD(qp_params->modify_flags,
1177 			  QED_ROCE_MODIFY_QP_VALID_ROCE_MODE, 1);
1178 		qp_params->roce_mode = ROCE_V2_IPV4;
1179 		break;
1180 	default:
1181 		return -EINVAL;
1182 	}
1183 
1184 	for (i = 0; i < 4; i++) {
1185 		qp_params->sgid.dwords[i] = ntohl(qp_params->sgid.dwords[i]);
1186 		qp_params->dgid.dwords[i] = ntohl(qp_params->dgid.dwords[i]);
1187 	}
1188 
1189 	if (qp_params->vlan_id >= VLAN_CFI_MASK)
1190 		qp_params->vlan_id = 0;
1191 
1192 	return 0;
1193 }
1194 
qedr_check_qp_attrs(struct ib_pd * ibpd,struct qedr_dev * dev,struct ib_qp_init_attr * attrs,struct ib_udata * udata)1195 static int qedr_check_qp_attrs(struct ib_pd *ibpd, struct qedr_dev *dev,
1196 			       struct ib_qp_init_attr *attrs,
1197 			       struct ib_udata *udata)
1198 {
1199 	struct qedr_device_attr *qattr = &dev->attr;
1200 
1201 	/* QP0... attrs->qp_type == IB_QPT_GSI */
1202 	if (attrs->qp_type != IB_QPT_RC &&
1203 	    attrs->qp_type != IB_QPT_GSI &&
1204 	    attrs->qp_type != IB_QPT_XRC_INI &&
1205 	    attrs->qp_type != IB_QPT_XRC_TGT) {
1206 		DP_DEBUG(dev, QEDR_MSG_QP,
1207 			 "create qp: unsupported qp type=0x%x requested\n",
1208 			 attrs->qp_type);
1209 		return -EOPNOTSUPP;
1210 	}
1211 
1212 	if (attrs->cap.max_send_wr > qattr->max_sqe) {
1213 		DP_ERR(dev,
1214 		       "create qp: cannot create a SQ with %d elements (max_send_wr=0x%x)\n",
1215 		       attrs->cap.max_send_wr, qattr->max_sqe);
1216 		return -EINVAL;
1217 	}
1218 
1219 	if (attrs->cap.max_inline_data > qattr->max_inline) {
1220 		DP_ERR(dev,
1221 		       "create qp: unsupported inline data size=0x%x requested (max_inline=0x%x)\n",
1222 		       attrs->cap.max_inline_data, qattr->max_inline);
1223 		return -EINVAL;
1224 	}
1225 
1226 	if (attrs->cap.max_send_sge > qattr->max_sge) {
1227 		DP_ERR(dev,
1228 		       "create qp: unsupported send_sge=0x%x requested (max_send_sge=0x%x)\n",
1229 		       attrs->cap.max_send_sge, qattr->max_sge);
1230 		return -EINVAL;
1231 	}
1232 
1233 	if (attrs->cap.max_recv_sge > qattr->max_sge) {
1234 		DP_ERR(dev,
1235 		       "create qp: unsupported recv_sge=0x%x requested (max_recv_sge=0x%x)\n",
1236 		       attrs->cap.max_recv_sge, qattr->max_sge);
1237 		return -EINVAL;
1238 	}
1239 
1240 	/* verify consumer QPs are not trying to use GSI QP's CQ.
1241 	 * TGT QP isn't associated with RQ/SQ
1242 	 */
1243 	if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created) &&
1244 	    (attrs->qp_type != IB_QPT_XRC_TGT) &&
1245 	    (attrs->qp_type != IB_QPT_XRC_INI)) {
1246 		struct qedr_cq *send_cq = get_qedr_cq(attrs->send_cq);
1247 		struct qedr_cq *recv_cq = get_qedr_cq(attrs->recv_cq);
1248 
1249 		if ((send_cq->cq_type == QEDR_CQ_TYPE_GSI) ||
1250 		    (recv_cq->cq_type == QEDR_CQ_TYPE_GSI)) {
1251 			DP_ERR(dev,
1252 			       "create qp: consumer QP cannot use GSI CQs.\n");
1253 			return -EINVAL;
1254 		}
1255 	}
1256 
1257 	return 0;
1258 }
1259 
qedr_copy_srq_uresp(struct qedr_dev * dev,struct qedr_srq * srq,struct ib_udata * udata)1260 static int qedr_copy_srq_uresp(struct qedr_dev *dev,
1261 			       struct qedr_srq *srq, struct ib_udata *udata)
1262 {
1263 	struct qedr_create_srq_uresp uresp = {};
1264 	int rc;
1265 
1266 	uresp.srq_id = srq->srq_id;
1267 
1268 	rc = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1269 	if (rc)
1270 		DP_ERR(dev, "create srq: problem copying data to user space\n");
1271 
1272 	return rc;
1273 }
1274 
qedr_copy_rq_uresp(struct qedr_dev * dev,struct qedr_create_qp_uresp * uresp,struct qedr_qp * qp)1275 static void qedr_copy_rq_uresp(struct qedr_dev *dev,
1276 			       struct qedr_create_qp_uresp *uresp,
1277 			       struct qedr_qp *qp)
1278 {
1279 	/* iWARP requires two doorbells per RQ. */
1280 	if (rdma_protocol_iwarp(&dev->ibdev, 1)) {
1281 		uresp->rq_db_offset =
1282 		    DB_ADDR_SHIFT(DQ_PWM_OFFSET_TCM_IWARP_RQ_PROD);
1283 		uresp->rq_db2_offset = DB_ADDR_SHIFT(DQ_PWM_OFFSET_TCM_FLAGS);
1284 	} else {
1285 		uresp->rq_db_offset =
1286 		    DB_ADDR_SHIFT(DQ_PWM_OFFSET_TCM_ROCE_RQ_PROD);
1287 	}
1288 
1289 	uresp->rq_icid = qp->icid;
1290 	if (qp->urq.db_mmap_entry)
1291 		uresp->rq_db_rec_addr =
1292 			rdma_user_mmap_get_offset(qp->urq.db_mmap_entry);
1293 }
1294 
qedr_copy_sq_uresp(struct qedr_dev * dev,struct qedr_create_qp_uresp * uresp,struct qedr_qp * qp)1295 static void qedr_copy_sq_uresp(struct qedr_dev *dev,
1296 			       struct qedr_create_qp_uresp *uresp,
1297 			       struct qedr_qp *qp)
1298 {
1299 	uresp->sq_db_offset = DB_ADDR_SHIFT(DQ_PWM_OFFSET_XCM_RDMA_SQ_PROD);
1300 
1301 	/* iWARP uses the same cid for rq and sq */
1302 	if (rdma_protocol_iwarp(&dev->ibdev, 1))
1303 		uresp->sq_icid = qp->icid;
1304 	else
1305 		uresp->sq_icid = qp->icid + 1;
1306 
1307 	if (qp->usq.db_mmap_entry)
1308 		uresp->sq_db_rec_addr =
1309 			rdma_user_mmap_get_offset(qp->usq.db_mmap_entry);
1310 }
1311 
qedr_copy_qp_uresp(struct qedr_dev * dev,struct qedr_qp * qp,struct ib_udata * udata,struct qedr_create_qp_uresp * uresp)1312 static int qedr_copy_qp_uresp(struct qedr_dev *dev,
1313 			      struct qedr_qp *qp, struct ib_udata *udata,
1314 			      struct qedr_create_qp_uresp *uresp)
1315 {
1316 	int rc;
1317 
1318 	memset(uresp, 0, sizeof(*uresp));
1319 
1320 	if (qedr_qp_has_sq(qp))
1321 		qedr_copy_sq_uresp(dev, uresp, qp);
1322 
1323 	if (qedr_qp_has_rq(qp))
1324 		qedr_copy_rq_uresp(dev, uresp, qp);
1325 
1326 	uresp->atomic_supported = dev->atomic_cap != IB_ATOMIC_NONE;
1327 	uresp->qp_id = qp->qp_id;
1328 
1329 	rc = qedr_ib_copy_to_udata(udata, uresp, sizeof(*uresp));
1330 	if (rc)
1331 		DP_ERR(dev,
1332 		       "create qp: failed a copy to user space with qp icid=0x%x.\n",
1333 		       qp->icid);
1334 
1335 	return rc;
1336 }
1337 
qedr_set_common_qp_params(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_pd * pd,struct ib_qp_init_attr * attrs)1338 static void qedr_set_common_qp_params(struct qedr_dev *dev,
1339 				      struct qedr_qp *qp,
1340 				      struct qedr_pd *pd,
1341 				      struct ib_qp_init_attr *attrs)
1342 {
1343 	spin_lock_init(&qp->q_lock);
1344 	if (rdma_protocol_iwarp(&dev->ibdev, 1)) {
1345 		kref_init(&qp->refcnt);
1346 		init_completion(&qp->iwarp_cm_comp);
1347 	}
1348 
1349 	qp->pd = pd;
1350 	qp->qp_type = attrs->qp_type;
1351 	qp->max_inline_data = attrs->cap.max_inline_data;
1352 	qp->state = QED_ROCE_QP_STATE_RESET;
1353 	qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
1354 	qp->dev = dev;
1355 	if (qedr_qp_has_sq(qp)) {
1356 		qp->sq.max_sges = attrs->cap.max_send_sge;
1357 		qp->sq_cq = get_qedr_cq(attrs->send_cq);
1358 		DP_DEBUG(dev, QEDR_MSG_QP,
1359 			 "SQ params:\tsq_max_sges = %d, sq_cq_id = %d\n",
1360 			 qp->sq.max_sges, qp->sq_cq->icid);
1361 	}
1362 
1363 	if (attrs->srq)
1364 		qp->srq = get_qedr_srq(attrs->srq);
1365 
1366 	if (qedr_qp_has_rq(qp)) {
1367 		qp->rq_cq = get_qedr_cq(attrs->recv_cq);
1368 		qp->rq.max_sges = attrs->cap.max_recv_sge;
1369 		DP_DEBUG(dev, QEDR_MSG_QP,
1370 			 "RQ params:\trq_max_sges = %d, rq_cq_id = %d\n",
1371 			 qp->rq.max_sges, qp->rq_cq->icid);
1372 	}
1373 
1374 	DP_DEBUG(dev, QEDR_MSG_QP,
1375 		 "QP params:\tpd = %d, qp_type = %d, max_inline_data = %d, state = %d, signaled = %d, use_srq=%d\n",
1376 		 pd->pd_id, qp->qp_type, qp->max_inline_data,
1377 		 qp->state, qp->signaled, (attrs->srq) ? 1 : 0);
1378 	DP_DEBUG(dev, QEDR_MSG_QP,
1379 		 "SQ params:\tsq_max_sges = %d, sq_cq_id = %d\n",
1380 		 qp->sq.max_sges, qp->sq_cq->icid);
1381 }
1382 
qedr_set_roce_db_info(struct qedr_dev * dev,struct qedr_qp * qp)1383 static int qedr_set_roce_db_info(struct qedr_dev *dev, struct qedr_qp *qp)
1384 {
1385 	int rc = 0;
1386 
1387 	if (qedr_qp_has_sq(qp)) {
1388 		qp->sq.db = dev->db_addr +
1389 			    DB_ADDR_SHIFT(DQ_PWM_OFFSET_XCM_RDMA_SQ_PROD);
1390 		qp->sq.db_data.data.icid = qp->icid + 1;
1391 		rc = qedr_db_recovery_add(dev, qp->sq.db, &qp->sq.db_data,
1392 					  DB_REC_WIDTH_32B, DB_REC_KERNEL);
1393 		if (rc)
1394 			return rc;
1395 	}
1396 
1397 	if (qedr_qp_has_rq(qp)) {
1398 		qp->rq.db = dev->db_addr +
1399 			    DB_ADDR_SHIFT(DQ_PWM_OFFSET_TCM_ROCE_RQ_PROD);
1400 		qp->rq.db_data.data.icid = qp->icid;
1401 		rc = qedr_db_recovery_add(dev, qp->rq.db, &qp->rq.db_data,
1402 					  DB_REC_WIDTH_32B, DB_REC_KERNEL);
1403 		if (rc && qedr_qp_has_sq(qp))
1404 			qedr_db_recovery_del(dev, qp->sq.db, &qp->sq.db_data);
1405 	}
1406 
1407 	return rc;
1408 }
1409 
qedr_check_srq_params(struct qedr_dev * dev,struct ib_srq_init_attr * attrs,struct ib_udata * udata)1410 static int qedr_check_srq_params(struct qedr_dev *dev,
1411 				 struct ib_srq_init_attr *attrs,
1412 				 struct ib_udata *udata)
1413 {
1414 	struct qedr_device_attr *qattr = &dev->attr;
1415 
1416 	if (attrs->attr.max_wr > qattr->max_srq_wr) {
1417 		DP_ERR(dev,
1418 		       "create srq: unsupported srq_wr=0x%x requested (max_srq_wr=0x%x)\n",
1419 		       attrs->attr.max_wr, qattr->max_srq_wr);
1420 		return -EINVAL;
1421 	}
1422 
1423 	if (attrs->attr.max_sge > qattr->max_sge) {
1424 		DP_ERR(dev,
1425 		       "create srq: unsupported sge=0x%x requested (max_srq_sge=0x%x)\n",
1426 		       attrs->attr.max_sge, qattr->max_sge);
1427 	}
1428 
1429 	if (!udata && attrs->srq_type == IB_SRQT_XRC) {
1430 		DP_ERR(dev, "XRC SRQs are not supported in kernel-space\n");
1431 		return -EINVAL;
1432 	}
1433 
1434 	return 0;
1435 }
1436 
qedr_free_srq_user_params(struct qedr_srq * srq)1437 static void qedr_free_srq_user_params(struct qedr_srq *srq)
1438 {
1439 	qedr_free_pbl(srq->dev, &srq->usrq.pbl_info, srq->usrq.pbl_tbl);
1440 	ib_umem_release(srq->usrq.umem);
1441 	ib_umem_release(srq->prod_umem);
1442 }
1443 
qedr_free_srq_kernel_params(struct qedr_srq * srq)1444 static void qedr_free_srq_kernel_params(struct qedr_srq *srq)
1445 {
1446 	struct qedr_srq_hwq_info *hw_srq = &srq->hw_srq;
1447 	struct qedr_dev *dev = srq->dev;
1448 
1449 	dev->ops->common->chain_free(dev->cdev, &hw_srq->pbl);
1450 
1451 	dma_free_coherent(&dev->pdev->dev, sizeof(struct rdma_srq_producers),
1452 			  hw_srq->virt_prod_pair_addr,
1453 			  hw_srq->phy_prod_pair_addr);
1454 }
1455 
qedr_init_srq_user_params(struct ib_udata * udata,struct qedr_srq * srq,struct qedr_create_srq_ureq * ureq,int access)1456 static int qedr_init_srq_user_params(struct ib_udata *udata,
1457 				     struct qedr_srq *srq,
1458 				     struct qedr_create_srq_ureq *ureq,
1459 				     int access)
1460 {
1461 	struct scatterlist *sg;
1462 	int rc;
1463 
1464 	rc = qedr_init_user_queue(udata, srq->dev, &srq->usrq, ureq->srq_addr,
1465 				  ureq->srq_len, false, access, 1);
1466 	if (rc)
1467 		return rc;
1468 
1469 	srq->prod_umem = ib_umem_get(srq->ibsrq.device, ureq->prod_pair_addr,
1470 				     sizeof(struct rdma_srq_producers), access);
1471 	if (IS_ERR(srq->prod_umem)) {
1472 		qedr_free_pbl(srq->dev, &srq->usrq.pbl_info, srq->usrq.pbl_tbl);
1473 		ib_umem_release(srq->usrq.umem);
1474 		DP_ERR(srq->dev,
1475 		       "create srq: failed ib_umem_get for producer, got %ld\n",
1476 		       PTR_ERR(srq->prod_umem));
1477 		return PTR_ERR(srq->prod_umem);
1478 	}
1479 
1480 	sg = srq->prod_umem->sg_head.sgl;
1481 	srq->hw_srq.phy_prod_pair_addr = sg_dma_address(sg);
1482 
1483 	return 0;
1484 }
1485 
qedr_alloc_srq_kernel_params(struct qedr_srq * srq,struct qedr_dev * dev,struct ib_srq_init_attr * init_attr)1486 static int qedr_alloc_srq_kernel_params(struct qedr_srq *srq,
1487 					struct qedr_dev *dev,
1488 					struct ib_srq_init_attr *init_attr)
1489 {
1490 	struct qedr_srq_hwq_info *hw_srq = &srq->hw_srq;
1491 	struct qed_chain_init_params params = {
1492 		.mode		= QED_CHAIN_MODE_PBL,
1493 		.intended_use	= QED_CHAIN_USE_TO_CONSUME_PRODUCE,
1494 		.cnt_type	= QED_CHAIN_CNT_TYPE_U32,
1495 		.elem_size	= QEDR_SRQ_WQE_ELEM_SIZE,
1496 	};
1497 	dma_addr_t phy_prod_pair_addr;
1498 	u32 num_elems;
1499 	void *va;
1500 	int rc;
1501 
1502 	va = dma_alloc_coherent(&dev->pdev->dev,
1503 				sizeof(struct rdma_srq_producers),
1504 				&phy_prod_pair_addr, GFP_KERNEL);
1505 	if (!va) {
1506 		DP_ERR(dev,
1507 		       "create srq: failed to allocate dma memory for producer\n");
1508 		return -ENOMEM;
1509 	}
1510 
1511 	hw_srq->phy_prod_pair_addr = phy_prod_pair_addr;
1512 	hw_srq->virt_prod_pair_addr = va;
1513 
1514 	num_elems = init_attr->attr.max_wr * RDMA_MAX_SRQ_WQE_SIZE;
1515 	params.num_elems = num_elems;
1516 
1517 	rc = dev->ops->common->chain_alloc(dev->cdev, &hw_srq->pbl, &params);
1518 	if (rc)
1519 		goto err0;
1520 
1521 	hw_srq->num_elems = num_elems;
1522 
1523 	return 0;
1524 
1525 err0:
1526 	dma_free_coherent(&dev->pdev->dev, sizeof(struct rdma_srq_producers),
1527 			  va, phy_prod_pair_addr);
1528 	return rc;
1529 }
1530 
qedr_create_srq(struct ib_srq * ibsrq,struct ib_srq_init_attr * init_attr,struct ib_udata * udata)1531 int qedr_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init_attr,
1532 		    struct ib_udata *udata)
1533 {
1534 	struct qed_rdma_destroy_srq_in_params destroy_in_params;
1535 	struct qed_rdma_create_srq_in_params in_params = {};
1536 	struct qedr_dev *dev = get_qedr_dev(ibsrq->device);
1537 	struct qed_rdma_create_srq_out_params out_params;
1538 	struct qedr_pd *pd = get_qedr_pd(ibsrq->pd);
1539 	struct qedr_create_srq_ureq ureq = {};
1540 	u64 pbl_base_addr, phy_prod_pair_addr;
1541 	struct qedr_srq_hwq_info *hw_srq;
1542 	u32 page_cnt, page_size;
1543 	struct qedr_srq *srq = get_qedr_srq(ibsrq);
1544 	int rc = 0;
1545 
1546 	DP_DEBUG(dev, QEDR_MSG_QP,
1547 		 "create SRQ called from %s (pd %p)\n",
1548 		 (udata) ? "User lib" : "kernel", pd);
1549 
1550 	rc = qedr_check_srq_params(dev, init_attr, udata);
1551 	if (rc)
1552 		return -EINVAL;
1553 
1554 	srq->dev = dev;
1555 	srq->is_xrc = (init_attr->srq_type == IB_SRQT_XRC);
1556 	hw_srq = &srq->hw_srq;
1557 	spin_lock_init(&srq->lock);
1558 
1559 	hw_srq->max_wr = init_attr->attr.max_wr;
1560 	hw_srq->max_sges = init_attr->attr.max_sge;
1561 
1562 	if (udata) {
1563 		if (ib_copy_from_udata(&ureq, udata, min(sizeof(ureq),
1564 							 udata->inlen))) {
1565 			DP_ERR(dev,
1566 			       "create srq: problem copying data from user space\n");
1567 			goto err0;
1568 		}
1569 
1570 		rc = qedr_init_srq_user_params(udata, srq, &ureq, 0);
1571 		if (rc)
1572 			goto err0;
1573 
1574 		page_cnt = srq->usrq.pbl_info.num_pbes;
1575 		pbl_base_addr = srq->usrq.pbl_tbl->pa;
1576 		phy_prod_pair_addr = hw_srq->phy_prod_pair_addr;
1577 		page_size = PAGE_SIZE;
1578 	} else {
1579 		struct qed_chain *pbl;
1580 
1581 		rc = qedr_alloc_srq_kernel_params(srq, dev, init_attr);
1582 		if (rc)
1583 			goto err0;
1584 
1585 		pbl = &hw_srq->pbl;
1586 		page_cnt = qed_chain_get_page_cnt(pbl);
1587 		pbl_base_addr = qed_chain_get_pbl_phys(pbl);
1588 		phy_prod_pair_addr = hw_srq->phy_prod_pair_addr;
1589 		page_size = QED_CHAIN_PAGE_SIZE;
1590 	}
1591 
1592 	in_params.pd_id = pd->pd_id;
1593 	in_params.pbl_base_addr = pbl_base_addr;
1594 	in_params.prod_pair_addr = phy_prod_pair_addr;
1595 	in_params.num_pages = page_cnt;
1596 	in_params.page_size = page_size;
1597 	if (srq->is_xrc) {
1598 		struct qedr_xrcd *xrcd = get_qedr_xrcd(init_attr->ext.xrc.xrcd);
1599 		struct qedr_cq *cq = get_qedr_cq(init_attr->ext.cq);
1600 
1601 		in_params.is_xrc = 1;
1602 		in_params.xrcd_id = xrcd->xrcd_id;
1603 		in_params.cq_cid = cq->icid;
1604 	}
1605 
1606 	rc = dev->ops->rdma_create_srq(dev->rdma_ctx, &in_params, &out_params);
1607 	if (rc)
1608 		goto err1;
1609 
1610 	srq->srq_id = out_params.srq_id;
1611 
1612 	if (udata) {
1613 		rc = qedr_copy_srq_uresp(dev, srq, udata);
1614 		if (rc)
1615 			goto err2;
1616 	}
1617 
1618 	rc = xa_insert_irq(&dev->srqs, srq->srq_id, srq, GFP_KERNEL);
1619 	if (rc)
1620 		goto err2;
1621 
1622 	DP_DEBUG(dev, QEDR_MSG_SRQ,
1623 		 "create srq: created srq with srq_id=0x%0x\n", srq->srq_id);
1624 	return 0;
1625 
1626 err2:
1627 	destroy_in_params.srq_id = srq->srq_id;
1628 
1629 	dev->ops->rdma_destroy_srq(dev->rdma_ctx, &destroy_in_params);
1630 err1:
1631 	if (udata)
1632 		qedr_free_srq_user_params(srq);
1633 	else
1634 		qedr_free_srq_kernel_params(srq);
1635 err0:
1636 	return -EFAULT;
1637 }
1638 
qedr_destroy_srq(struct ib_srq * ibsrq,struct ib_udata * udata)1639 int qedr_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
1640 {
1641 	struct qed_rdma_destroy_srq_in_params in_params = {};
1642 	struct qedr_dev *dev = get_qedr_dev(ibsrq->device);
1643 	struct qedr_srq *srq = get_qedr_srq(ibsrq);
1644 
1645 	xa_erase_irq(&dev->srqs, srq->srq_id);
1646 	in_params.srq_id = srq->srq_id;
1647 	in_params.is_xrc = srq->is_xrc;
1648 	dev->ops->rdma_destroy_srq(dev->rdma_ctx, &in_params);
1649 
1650 	if (ibsrq->uobject)
1651 		qedr_free_srq_user_params(srq);
1652 	else
1653 		qedr_free_srq_kernel_params(srq);
1654 
1655 	DP_DEBUG(dev, QEDR_MSG_SRQ,
1656 		 "destroy srq: destroyed srq with srq_id=0x%0x\n",
1657 		 srq->srq_id);
1658 	return 0;
1659 }
1660 
qedr_modify_srq(struct ib_srq * ibsrq,struct ib_srq_attr * attr,enum ib_srq_attr_mask attr_mask,struct ib_udata * udata)1661 int qedr_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
1662 		    enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
1663 {
1664 	struct qed_rdma_modify_srq_in_params in_params = {};
1665 	struct qedr_dev *dev = get_qedr_dev(ibsrq->device);
1666 	struct qedr_srq *srq = get_qedr_srq(ibsrq);
1667 	int rc;
1668 
1669 	if (attr_mask & IB_SRQ_MAX_WR) {
1670 		DP_ERR(dev,
1671 		       "modify srq: invalid attribute mask=0x%x specified for %p\n",
1672 		       attr_mask, srq);
1673 		return -EINVAL;
1674 	}
1675 
1676 	if (attr_mask & IB_SRQ_LIMIT) {
1677 		if (attr->srq_limit >= srq->hw_srq.max_wr) {
1678 			DP_ERR(dev,
1679 			       "modify srq: invalid srq_limit=0x%x (max_srq_limit=0x%x)\n",
1680 			       attr->srq_limit, srq->hw_srq.max_wr);
1681 			return -EINVAL;
1682 		}
1683 
1684 		in_params.srq_id = srq->srq_id;
1685 		in_params.wqe_limit = attr->srq_limit;
1686 		rc = dev->ops->rdma_modify_srq(dev->rdma_ctx, &in_params);
1687 		if (rc)
1688 			return rc;
1689 	}
1690 
1691 	srq->srq_limit = attr->srq_limit;
1692 
1693 	DP_DEBUG(dev, QEDR_MSG_SRQ,
1694 		 "modify srq: modified srq with srq_id=0x%0x\n", srq->srq_id);
1695 
1696 	return 0;
1697 }
1698 
qedr_ib_to_qed_qp_type(enum ib_qp_type ib_qp_type)1699 static enum qed_rdma_qp_type qedr_ib_to_qed_qp_type(enum ib_qp_type ib_qp_type)
1700 {
1701 	switch (ib_qp_type) {
1702 	case IB_QPT_RC:
1703 		return QED_RDMA_QP_TYPE_RC;
1704 	case IB_QPT_XRC_INI:
1705 		return QED_RDMA_QP_TYPE_XRC_INI;
1706 	case IB_QPT_XRC_TGT:
1707 		return QED_RDMA_QP_TYPE_XRC_TGT;
1708 	default:
1709 		return QED_RDMA_QP_TYPE_INVAL;
1710 	}
1711 }
1712 
1713 static inline void
qedr_init_common_qp_in_params(struct qedr_dev * dev,struct qedr_pd * pd,struct qedr_qp * qp,struct ib_qp_init_attr * attrs,bool fmr_and_reserved_lkey,struct qed_rdma_create_qp_in_params * params)1714 qedr_init_common_qp_in_params(struct qedr_dev *dev,
1715 			      struct qedr_pd *pd,
1716 			      struct qedr_qp *qp,
1717 			      struct ib_qp_init_attr *attrs,
1718 			      bool fmr_and_reserved_lkey,
1719 			      struct qed_rdma_create_qp_in_params *params)
1720 {
1721 	/* QP handle to be written in an async event */
1722 	params->qp_handle_async_lo = lower_32_bits((uintptr_t) qp);
1723 	params->qp_handle_async_hi = upper_32_bits((uintptr_t) qp);
1724 
1725 	params->signal_all = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR);
1726 	params->fmr_and_reserved_lkey = fmr_and_reserved_lkey;
1727 	params->qp_type = qedr_ib_to_qed_qp_type(attrs->qp_type);
1728 	params->stats_queue = 0;
1729 
1730 	if (pd) {
1731 		params->pd = pd->pd_id;
1732 		params->dpi = pd->uctx ? pd->uctx->dpi : dev->dpi;
1733 	}
1734 
1735 	if (qedr_qp_has_sq(qp))
1736 		params->sq_cq_id = get_qedr_cq(attrs->send_cq)->icid;
1737 
1738 	if (qedr_qp_has_rq(qp))
1739 		params->rq_cq_id = get_qedr_cq(attrs->recv_cq)->icid;
1740 
1741 	if (qedr_qp_has_srq(qp)) {
1742 		params->rq_cq_id = get_qedr_cq(attrs->recv_cq)->icid;
1743 		params->srq_id = qp->srq->srq_id;
1744 		params->use_srq = true;
1745 	} else {
1746 		params->srq_id = 0;
1747 		params->use_srq = false;
1748 	}
1749 }
1750 
qedr_qp_user_print(struct qedr_dev * dev,struct qedr_qp * qp)1751 static inline void qedr_qp_user_print(struct qedr_dev *dev, struct qedr_qp *qp)
1752 {
1753 	DP_DEBUG(dev, QEDR_MSG_QP, "create qp: successfully created user QP. "
1754 		 "qp=%p. "
1755 		 "sq_addr=0x%llx, "
1756 		 "sq_len=%zd, "
1757 		 "rq_addr=0x%llx, "
1758 		 "rq_len=%zd"
1759 		 "\n",
1760 		 qp,
1761 		 qedr_qp_has_sq(qp) ? qp->usq.buf_addr : 0x0,
1762 		 qedr_qp_has_sq(qp) ? qp->usq.buf_len : 0,
1763 		 qedr_qp_has_rq(qp) ? qp->urq.buf_addr : 0x0,
1764 		 qedr_qp_has_sq(qp) ? qp->urq.buf_len : 0);
1765 }
1766 
1767 static inline void
qedr_iwarp_populate_user_qp(struct qedr_dev * dev,struct qedr_qp * qp,struct qed_rdma_create_qp_out_params * out_params)1768 qedr_iwarp_populate_user_qp(struct qedr_dev *dev,
1769 			    struct qedr_qp *qp,
1770 			    struct qed_rdma_create_qp_out_params *out_params)
1771 {
1772 	qp->usq.pbl_tbl->va = out_params->sq_pbl_virt;
1773 	qp->usq.pbl_tbl->pa = out_params->sq_pbl_phys;
1774 
1775 	qedr_populate_pbls(dev, qp->usq.umem, qp->usq.pbl_tbl,
1776 			   &qp->usq.pbl_info, FW_PAGE_SHIFT);
1777 	if (!qp->srq) {
1778 		qp->urq.pbl_tbl->va = out_params->rq_pbl_virt;
1779 		qp->urq.pbl_tbl->pa = out_params->rq_pbl_phys;
1780 	}
1781 
1782 	qedr_populate_pbls(dev, qp->urq.umem, qp->urq.pbl_tbl,
1783 			   &qp->urq.pbl_info, FW_PAGE_SHIFT);
1784 }
1785 
qedr_cleanup_user(struct qedr_dev * dev,struct qedr_ucontext * ctx,struct qedr_qp * qp)1786 static void qedr_cleanup_user(struct qedr_dev *dev,
1787 			      struct qedr_ucontext *ctx,
1788 			      struct qedr_qp *qp)
1789 {
1790 	if (qedr_qp_has_sq(qp)) {
1791 		ib_umem_release(qp->usq.umem);
1792 		qp->usq.umem = NULL;
1793 	}
1794 
1795 	if (qedr_qp_has_rq(qp)) {
1796 		ib_umem_release(qp->urq.umem);
1797 		qp->urq.umem = NULL;
1798 	}
1799 
1800 	if (rdma_protocol_roce(&dev->ibdev, 1)) {
1801 		qedr_free_pbl(dev, &qp->usq.pbl_info, qp->usq.pbl_tbl);
1802 		qedr_free_pbl(dev, &qp->urq.pbl_info, qp->urq.pbl_tbl);
1803 	} else {
1804 		kfree(qp->usq.pbl_tbl);
1805 		kfree(qp->urq.pbl_tbl);
1806 	}
1807 
1808 	if (qp->usq.db_rec_data) {
1809 		qedr_db_recovery_del(dev, qp->usq.db_addr,
1810 				     &qp->usq.db_rec_data->db_data);
1811 		rdma_user_mmap_entry_remove(qp->usq.db_mmap_entry);
1812 	}
1813 
1814 	if (qp->urq.db_rec_data) {
1815 		qedr_db_recovery_del(dev, qp->urq.db_addr,
1816 				     &qp->urq.db_rec_data->db_data);
1817 		rdma_user_mmap_entry_remove(qp->urq.db_mmap_entry);
1818 	}
1819 
1820 	if (rdma_protocol_iwarp(&dev->ibdev, 1))
1821 		qedr_db_recovery_del(dev, qp->urq.db_rec_db2_addr,
1822 				     &qp->urq.db_rec_db2_data);
1823 }
1824 
qedr_create_user_qp(struct qedr_dev * dev,struct qedr_qp * qp,struct ib_pd * ibpd,struct ib_udata * udata,struct ib_qp_init_attr * attrs)1825 static int qedr_create_user_qp(struct qedr_dev *dev,
1826 			       struct qedr_qp *qp,
1827 			       struct ib_pd *ibpd,
1828 			       struct ib_udata *udata,
1829 			       struct ib_qp_init_attr *attrs)
1830 {
1831 	struct qed_rdma_create_qp_in_params in_params;
1832 	struct qed_rdma_create_qp_out_params out_params;
1833 	struct qedr_create_qp_uresp uresp = {};
1834 	struct qedr_create_qp_ureq ureq = {};
1835 	int alloc_and_init = rdma_protocol_roce(&dev->ibdev, 1);
1836 	struct qedr_ucontext *ctx = NULL;
1837 	struct qedr_pd *pd = NULL;
1838 	int rc = 0;
1839 
1840 	qp->create_type = QEDR_QP_CREATE_USER;
1841 
1842 	if (ibpd) {
1843 		pd = get_qedr_pd(ibpd);
1844 		ctx = pd->uctx;
1845 	}
1846 
1847 	if (udata) {
1848 		rc = ib_copy_from_udata(&ureq, udata, min(sizeof(ureq),
1849 					udata->inlen));
1850 		if (rc) {
1851 			DP_ERR(dev, "Problem copying data from user space\n");
1852 			return rc;
1853 		}
1854 	}
1855 
1856 	if (qedr_qp_has_sq(qp)) {
1857 		/* SQ - read access only (0) */
1858 		rc = qedr_init_user_queue(udata, dev, &qp->usq, ureq.sq_addr,
1859 					  ureq.sq_len, true, 0, alloc_and_init);
1860 		if (rc)
1861 			return rc;
1862 	}
1863 
1864 	if (qedr_qp_has_rq(qp)) {
1865 		/* RQ - read access only (0) */
1866 		rc = qedr_init_user_queue(udata, dev, &qp->urq, ureq.rq_addr,
1867 					  ureq.rq_len, true, 0, alloc_and_init);
1868 		if (rc) {
1869 			ib_umem_release(qp->usq.umem);
1870 			qp->usq.umem = NULL;
1871 			if (rdma_protocol_roce(&dev->ibdev, 1)) {
1872 				qedr_free_pbl(dev, &qp->usq.pbl_info,
1873 					      qp->usq.pbl_tbl);
1874 			} else {
1875 				kfree(qp->usq.pbl_tbl);
1876 			}
1877 			return rc;
1878 		}
1879 	}
1880 
1881 	memset(&in_params, 0, sizeof(in_params));
1882 	qedr_init_common_qp_in_params(dev, pd, qp, attrs, false, &in_params);
1883 	in_params.qp_handle_lo = ureq.qp_handle_lo;
1884 	in_params.qp_handle_hi = ureq.qp_handle_hi;
1885 
1886 	if (qp->qp_type == IB_QPT_XRC_TGT) {
1887 		struct qedr_xrcd *xrcd = get_qedr_xrcd(attrs->xrcd);
1888 
1889 		in_params.xrcd_id = xrcd->xrcd_id;
1890 		in_params.qp_handle_lo = qp->qp_id;
1891 		in_params.use_srq = 1;
1892 	}
1893 
1894 	if (qedr_qp_has_sq(qp)) {
1895 		in_params.sq_num_pages = qp->usq.pbl_info.num_pbes;
1896 		in_params.sq_pbl_ptr = qp->usq.pbl_tbl->pa;
1897 	}
1898 
1899 	if (qedr_qp_has_rq(qp)) {
1900 		in_params.rq_num_pages = qp->urq.pbl_info.num_pbes;
1901 		in_params.rq_pbl_ptr = qp->urq.pbl_tbl->pa;
1902 	}
1903 
1904 	if (ctx)
1905 		SET_FIELD(in_params.flags, QED_ROCE_EDPM_MODE, ctx->edpm_mode);
1906 
1907 	qp->qed_qp = dev->ops->rdma_create_qp(dev->rdma_ctx,
1908 					      &in_params, &out_params);
1909 
1910 	if (!qp->qed_qp) {
1911 		rc = -ENOMEM;
1912 		goto err1;
1913 	}
1914 
1915 	if (rdma_protocol_iwarp(&dev->ibdev, 1))
1916 		qedr_iwarp_populate_user_qp(dev, qp, &out_params);
1917 
1918 	qp->qp_id = out_params.qp_id;
1919 	qp->icid = out_params.icid;
1920 
1921 	if (udata) {
1922 		rc = qedr_copy_qp_uresp(dev, qp, udata, &uresp);
1923 		if (rc)
1924 			goto err;
1925 	}
1926 
1927 	/* db offset was calculated in copy_qp_uresp, now set in the user q */
1928 	if (qedr_qp_has_sq(qp)) {
1929 		qp->usq.db_addr = ctx->dpi_addr + uresp.sq_db_offset;
1930 		qp->sq.max_wr = attrs->cap.max_send_wr;
1931 		rc = qedr_db_recovery_add(dev, qp->usq.db_addr,
1932 					  &qp->usq.db_rec_data->db_data,
1933 					  DB_REC_WIDTH_32B,
1934 					  DB_REC_USER);
1935 		if (rc)
1936 			goto err;
1937 	}
1938 
1939 	if (qedr_qp_has_rq(qp)) {
1940 		qp->urq.db_addr = ctx->dpi_addr + uresp.rq_db_offset;
1941 		qp->rq.max_wr = attrs->cap.max_recv_wr;
1942 		rc = qedr_db_recovery_add(dev, qp->urq.db_addr,
1943 					  &qp->urq.db_rec_data->db_data,
1944 					  DB_REC_WIDTH_32B,
1945 					  DB_REC_USER);
1946 		if (rc)
1947 			goto err;
1948 	}
1949 
1950 	if (rdma_protocol_iwarp(&dev->ibdev, 1)) {
1951 		qp->urq.db_rec_db2_addr = ctx->dpi_addr + uresp.rq_db2_offset;
1952 
1953 		/* calculate the db_rec_db2 data since it is constant so no
1954 		 * need to reflect from user
1955 		 */
1956 		qp->urq.db_rec_db2_data.data.icid = cpu_to_le16(qp->icid);
1957 		qp->urq.db_rec_db2_data.data.value =
1958 			cpu_to_le16(DQ_TCM_IWARP_POST_RQ_CF_CMD);
1959 
1960 		rc = qedr_db_recovery_add(dev, qp->urq.db_rec_db2_addr,
1961 					  &qp->urq.db_rec_db2_data,
1962 					  DB_REC_WIDTH_32B,
1963 					  DB_REC_USER);
1964 		if (rc)
1965 			goto err;
1966 	}
1967 	qedr_qp_user_print(dev, qp);
1968 	return rc;
1969 err:
1970 	rc = dev->ops->rdma_destroy_qp(dev->rdma_ctx, qp->qed_qp);
1971 	if (rc)
1972 		DP_ERR(dev, "create qp: fatal fault. rc=%d", rc);
1973 
1974 err1:
1975 	qedr_cleanup_user(dev, ctx, qp);
1976 	return rc;
1977 }
1978 
qedr_set_iwarp_db_info(struct qedr_dev * dev,struct qedr_qp * qp)1979 static int qedr_set_iwarp_db_info(struct qedr_dev *dev, struct qedr_qp *qp)
1980 {
1981 	int rc;
1982 
1983 	qp->sq.db = dev->db_addr +
1984 	    DB_ADDR_SHIFT(DQ_PWM_OFFSET_XCM_RDMA_SQ_PROD);
1985 	qp->sq.db_data.data.icid = qp->icid;
1986 
1987 	rc = qedr_db_recovery_add(dev, qp->sq.db,
1988 				  &qp->sq.db_data,
1989 				  DB_REC_WIDTH_32B,
1990 				  DB_REC_KERNEL);
1991 	if (rc)
1992 		return rc;
1993 
1994 	qp->rq.db = dev->db_addr +
1995 		    DB_ADDR_SHIFT(DQ_PWM_OFFSET_TCM_IWARP_RQ_PROD);
1996 	qp->rq.db_data.data.icid = qp->icid;
1997 	qp->rq.iwarp_db2 = dev->db_addr +
1998 			   DB_ADDR_SHIFT(DQ_PWM_OFFSET_TCM_FLAGS);
1999 	qp->rq.iwarp_db2_data.data.icid = qp->icid;
2000 	qp->rq.iwarp_db2_data.data.value = DQ_TCM_IWARP_POST_RQ_CF_CMD;
2001 
2002 	rc = qedr_db_recovery_add(dev, qp->rq.db,
2003 				  &qp->rq.db_data,
2004 				  DB_REC_WIDTH_32B,
2005 				  DB_REC_KERNEL);
2006 	if (rc)
2007 		return rc;
2008 
2009 	rc = qedr_db_recovery_add(dev, qp->rq.iwarp_db2,
2010 				  &qp->rq.iwarp_db2_data,
2011 				  DB_REC_WIDTH_32B,
2012 				  DB_REC_KERNEL);
2013 	return rc;
2014 }
2015 
2016 static int
qedr_roce_create_kernel_qp(struct qedr_dev * dev,struct qedr_qp * qp,struct qed_rdma_create_qp_in_params * in_params,u32 n_sq_elems,u32 n_rq_elems)2017 qedr_roce_create_kernel_qp(struct qedr_dev *dev,
2018 			   struct qedr_qp *qp,
2019 			   struct qed_rdma_create_qp_in_params *in_params,
2020 			   u32 n_sq_elems, u32 n_rq_elems)
2021 {
2022 	struct qed_rdma_create_qp_out_params out_params;
2023 	struct qed_chain_init_params params = {
2024 		.mode		= QED_CHAIN_MODE_PBL,
2025 		.cnt_type	= QED_CHAIN_CNT_TYPE_U32,
2026 	};
2027 	int rc;
2028 
2029 	params.intended_use = QED_CHAIN_USE_TO_PRODUCE;
2030 	params.num_elems = n_sq_elems;
2031 	params.elem_size = QEDR_SQE_ELEMENT_SIZE;
2032 
2033 	rc = dev->ops->common->chain_alloc(dev->cdev, &qp->sq.pbl, &params);
2034 	if (rc)
2035 		return rc;
2036 
2037 	in_params->sq_num_pages = qed_chain_get_page_cnt(&qp->sq.pbl);
2038 	in_params->sq_pbl_ptr = qed_chain_get_pbl_phys(&qp->sq.pbl);
2039 
2040 	params.intended_use = QED_CHAIN_USE_TO_CONSUME_PRODUCE;
2041 	params.num_elems = n_rq_elems;
2042 	params.elem_size = QEDR_RQE_ELEMENT_SIZE;
2043 
2044 	rc = dev->ops->common->chain_alloc(dev->cdev, &qp->rq.pbl, &params);
2045 	if (rc)
2046 		return rc;
2047 
2048 	in_params->rq_num_pages = qed_chain_get_page_cnt(&qp->rq.pbl);
2049 	in_params->rq_pbl_ptr = qed_chain_get_pbl_phys(&qp->rq.pbl);
2050 
2051 	qp->qed_qp = dev->ops->rdma_create_qp(dev->rdma_ctx,
2052 					      in_params, &out_params);
2053 
2054 	if (!qp->qed_qp)
2055 		return -EINVAL;
2056 
2057 	qp->qp_id = out_params.qp_id;
2058 	qp->icid = out_params.icid;
2059 
2060 	return qedr_set_roce_db_info(dev, qp);
2061 }
2062 
2063 static int
qedr_iwarp_create_kernel_qp(struct qedr_dev * dev,struct qedr_qp * qp,struct qed_rdma_create_qp_in_params * in_params,u32 n_sq_elems,u32 n_rq_elems)2064 qedr_iwarp_create_kernel_qp(struct qedr_dev *dev,
2065 			    struct qedr_qp *qp,
2066 			    struct qed_rdma_create_qp_in_params *in_params,
2067 			    u32 n_sq_elems, u32 n_rq_elems)
2068 {
2069 	struct qed_rdma_create_qp_out_params out_params;
2070 	struct qed_chain_init_params params = {
2071 		.mode		= QED_CHAIN_MODE_PBL,
2072 		.cnt_type	= QED_CHAIN_CNT_TYPE_U32,
2073 	};
2074 	int rc;
2075 
2076 	in_params->sq_num_pages = QED_CHAIN_PAGE_CNT(n_sq_elems,
2077 						     QEDR_SQE_ELEMENT_SIZE,
2078 						     QED_CHAIN_PAGE_SIZE,
2079 						     QED_CHAIN_MODE_PBL);
2080 	in_params->rq_num_pages = QED_CHAIN_PAGE_CNT(n_rq_elems,
2081 						     QEDR_RQE_ELEMENT_SIZE,
2082 						     QED_CHAIN_PAGE_SIZE,
2083 						     QED_CHAIN_MODE_PBL);
2084 
2085 	qp->qed_qp = dev->ops->rdma_create_qp(dev->rdma_ctx,
2086 					      in_params, &out_params);
2087 
2088 	if (!qp->qed_qp)
2089 		return -EINVAL;
2090 
2091 	/* Now we allocate the chain */
2092 
2093 	params.intended_use = QED_CHAIN_USE_TO_PRODUCE;
2094 	params.num_elems = n_sq_elems;
2095 	params.elem_size = QEDR_SQE_ELEMENT_SIZE;
2096 	params.ext_pbl_virt = out_params.sq_pbl_virt;
2097 	params.ext_pbl_phys = out_params.sq_pbl_phys;
2098 
2099 	rc = dev->ops->common->chain_alloc(dev->cdev, &qp->sq.pbl, &params);
2100 	if (rc)
2101 		goto err;
2102 
2103 	params.intended_use = QED_CHAIN_USE_TO_CONSUME_PRODUCE;
2104 	params.num_elems = n_rq_elems;
2105 	params.elem_size = QEDR_RQE_ELEMENT_SIZE;
2106 	params.ext_pbl_virt = out_params.rq_pbl_virt;
2107 	params.ext_pbl_phys = out_params.rq_pbl_phys;
2108 
2109 	rc = dev->ops->common->chain_alloc(dev->cdev, &qp->rq.pbl, &params);
2110 	if (rc)
2111 		goto err;
2112 
2113 	qp->qp_id = out_params.qp_id;
2114 	qp->icid = out_params.icid;
2115 
2116 	return qedr_set_iwarp_db_info(dev, qp);
2117 
2118 err:
2119 	dev->ops->rdma_destroy_qp(dev->rdma_ctx, qp->qed_qp);
2120 
2121 	return rc;
2122 }
2123 
qedr_cleanup_kernel(struct qedr_dev * dev,struct qedr_qp * qp)2124 static void qedr_cleanup_kernel(struct qedr_dev *dev, struct qedr_qp *qp)
2125 {
2126 	dev->ops->common->chain_free(dev->cdev, &qp->sq.pbl);
2127 	kfree(qp->wqe_wr_id);
2128 
2129 	dev->ops->common->chain_free(dev->cdev, &qp->rq.pbl);
2130 	kfree(qp->rqe_wr_id);
2131 
2132 	/* GSI qp is not registered to db mechanism so no need to delete */
2133 	if (qp->qp_type == IB_QPT_GSI)
2134 		return;
2135 
2136 	qedr_db_recovery_del(dev, qp->sq.db, &qp->sq.db_data);
2137 
2138 	if (!qp->srq) {
2139 		qedr_db_recovery_del(dev, qp->rq.db, &qp->rq.db_data);
2140 
2141 		if (rdma_protocol_iwarp(&dev->ibdev, 1))
2142 			qedr_db_recovery_del(dev, qp->rq.iwarp_db2,
2143 					     &qp->rq.iwarp_db2_data);
2144 	}
2145 }
2146 
qedr_create_kernel_qp(struct qedr_dev * dev,struct qedr_qp * qp,struct ib_pd * ibpd,struct ib_qp_init_attr * attrs)2147 static int qedr_create_kernel_qp(struct qedr_dev *dev,
2148 				 struct qedr_qp *qp,
2149 				 struct ib_pd *ibpd,
2150 				 struct ib_qp_init_attr *attrs)
2151 {
2152 	struct qed_rdma_create_qp_in_params in_params;
2153 	struct qedr_pd *pd = get_qedr_pd(ibpd);
2154 	int rc = -EINVAL;
2155 	u32 n_rq_elems;
2156 	u32 n_sq_elems;
2157 	u32 n_sq_entries;
2158 
2159 	memset(&in_params, 0, sizeof(in_params));
2160 	qp->create_type = QEDR_QP_CREATE_KERNEL;
2161 
2162 	/* A single work request may take up to QEDR_MAX_SQ_WQE_SIZE elements in
2163 	 * the ring. The ring should allow at least a single WR, even if the
2164 	 * user requested none, due to allocation issues.
2165 	 * We should add an extra WR since the prod and cons indices of
2166 	 * wqe_wr_id are managed in such a way that the WQ is considered full
2167 	 * when (prod+1)%max_wr==cons. We currently don't do that because we
2168 	 * double the number of entries due an iSER issue that pushes far more
2169 	 * WRs than indicated. If we decline its ib_post_send() then we get
2170 	 * error prints in the dmesg we'd like to avoid.
2171 	 */
2172 	qp->sq.max_wr = min_t(u32, attrs->cap.max_send_wr * dev->wq_multiplier,
2173 			      dev->attr.max_sqe);
2174 
2175 	qp->wqe_wr_id = kcalloc(qp->sq.max_wr, sizeof(*qp->wqe_wr_id),
2176 				GFP_KERNEL);
2177 	if (!qp->wqe_wr_id) {
2178 		DP_ERR(dev, "create qp: failed SQ shadow memory allocation\n");
2179 		return -ENOMEM;
2180 	}
2181 
2182 	/* QP handle to be written in CQE */
2183 	in_params.qp_handle_lo = lower_32_bits((uintptr_t) qp);
2184 	in_params.qp_handle_hi = upper_32_bits((uintptr_t) qp);
2185 
2186 	/* A single work request may take up to QEDR_MAX_RQ_WQE_SIZE elements in
2187 	 * the ring. There ring should allow at least a single WR, even if the
2188 	 * user requested none, due to allocation issues.
2189 	 */
2190 	qp->rq.max_wr = (u16) max_t(u32, attrs->cap.max_recv_wr, 1);
2191 
2192 	/* Allocate driver internal RQ array */
2193 	qp->rqe_wr_id = kcalloc(qp->rq.max_wr, sizeof(*qp->rqe_wr_id),
2194 				GFP_KERNEL);
2195 	if (!qp->rqe_wr_id) {
2196 		DP_ERR(dev,
2197 		       "create qp: failed RQ shadow memory allocation\n");
2198 		kfree(qp->wqe_wr_id);
2199 		return -ENOMEM;
2200 	}
2201 
2202 	qedr_init_common_qp_in_params(dev, pd, qp, attrs, true, &in_params);
2203 
2204 	n_sq_entries = attrs->cap.max_send_wr;
2205 	n_sq_entries = min_t(u32, n_sq_entries, dev->attr.max_sqe);
2206 	n_sq_entries = max_t(u32, n_sq_entries, 1);
2207 	n_sq_elems = n_sq_entries * QEDR_MAX_SQE_ELEMENTS_PER_SQE;
2208 
2209 	n_rq_elems = qp->rq.max_wr * QEDR_MAX_RQE_ELEMENTS_PER_RQE;
2210 
2211 	if (rdma_protocol_iwarp(&dev->ibdev, 1))
2212 		rc = qedr_iwarp_create_kernel_qp(dev, qp, &in_params,
2213 						 n_sq_elems, n_rq_elems);
2214 	else
2215 		rc = qedr_roce_create_kernel_qp(dev, qp, &in_params,
2216 						n_sq_elems, n_rq_elems);
2217 	if (rc)
2218 		qedr_cleanup_kernel(dev, qp);
2219 
2220 	return rc;
2221 }
2222 
qedr_free_qp_resources(struct qedr_dev * dev,struct qedr_qp * qp,struct ib_udata * udata)2223 static int qedr_free_qp_resources(struct qedr_dev *dev, struct qedr_qp *qp,
2224 				  struct ib_udata *udata)
2225 {
2226 	struct qedr_ucontext *ctx =
2227 		rdma_udata_to_drv_context(udata, struct qedr_ucontext,
2228 					  ibucontext);
2229 	int rc;
2230 
2231 	if (qp->qp_type != IB_QPT_GSI) {
2232 		rc = dev->ops->rdma_destroy_qp(dev->rdma_ctx, qp->qed_qp);
2233 		if (rc)
2234 			return rc;
2235 	}
2236 
2237 	if (qp->create_type == QEDR_QP_CREATE_USER)
2238 		qedr_cleanup_user(dev, ctx, qp);
2239 	else
2240 		qedr_cleanup_kernel(dev, qp);
2241 
2242 	return 0;
2243 }
2244 
qedr_create_qp(struct ib_pd * ibpd,struct ib_qp_init_attr * attrs,struct ib_udata * udata)2245 struct ib_qp *qedr_create_qp(struct ib_pd *ibpd,
2246 			     struct ib_qp_init_attr *attrs,
2247 			     struct ib_udata *udata)
2248 {
2249 	struct qedr_xrcd *xrcd = NULL;
2250 	struct qedr_pd *pd = NULL;
2251 	struct qedr_dev *dev;
2252 	struct qedr_qp *qp;
2253 	struct ib_qp *ibqp;
2254 	int rc = 0;
2255 
2256 	if (attrs->qp_type == IB_QPT_XRC_TGT) {
2257 		xrcd = get_qedr_xrcd(attrs->xrcd);
2258 		dev = get_qedr_dev(xrcd->ibxrcd.device);
2259 	} else {
2260 		pd = get_qedr_pd(ibpd);
2261 		dev = get_qedr_dev(ibpd->device);
2262 	}
2263 
2264 	DP_DEBUG(dev, QEDR_MSG_QP, "create qp: called from %s, pd=%p\n",
2265 		 udata ? "user library" : "kernel", pd);
2266 
2267 	rc = qedr_check_qp_attrs(ibpd, dev, attrs, udata);
2268 	if (rc)
2269 		return ERR_PTR(rc);
2270 
2271 	DP_DEBUG(dev, QEDR_MSG_QP,
2272 		 "create qp: called from %s, event_handler=%p, eepd=%p sq_cq=%p, sq_icid=%d, rq_cq=%p, rq_icid=%d\n",
2273 		 udata ? "user library" : "kernel", attrs->event_handler, pd,
2274 		 get_qedr_cq(attrs->send_cq),
2275 		 get_qedr_cq(attrs->send_cq)->icid,
2276 		 get_qedr_cq(attrs->recv_cq),
2277 		 attrs->recv_cq ? get_qedr_cq(attrs->recv_cq)->icid : 0);
2278 
2279 	qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2280 	if (!qp) {
2281 		DP_ERR(dev, "create qp: failed allocating memory\n");
2282 		return ERR_PTR(-ENOMEM);
2283 	}
2284 
2285 	qedr_set_common_qp_params(dev, qp, pd, attrs);
2286 
2287 	if (attrs->qp_type == IB_QPT_GSI) {
2288 		ibqp = qedr_create_gsi_qp(dev, attrs, qp);
2289 		if (IS_ERR(ibqp))
2290 			kfree(qp);
2291 		return ibqp;
2292 	}
2293 
2294 	if (udata || xrcd)
2295 		rc = qedr_create_user_qp(dev, qp, ibpd, udata, attrs);
2296 	else
2297 		rc = qedr_create_kernel_qp(dev, qp, ibpd, attrs);
2298 
2299 	if (rc)
2300 		goto out_free_qp;
2301 
2302 	qp->ibqp.qp_num = qp->qp_id;
2303 
2304 	if (rdma_protocol_iwarp(&dev->ibdev, 1)) {
2305 		rc = xa_insert(&dev->qps, qp->qp_id, qp, GFP_KERNEL);
2306 		if (rc)
2307 			goto out_free_qp_resources;
2308 	}
2309 
2310 	return &qp->ibqp;
2311 
2312 out_free_qp_resources:
2313 	qedr_free_qp_resources(dev, qp, udata);
2314 out_free_qp:
2315 	kfree(qp);
2316 
2317 	return ERR_PTR(-EFAULT);
2318 }
2319 
qedr_get_ibqp_state(enum qed_roce_qp_state qp_state)2320 static enum ib_qp_state qedr_get_ibqp_state(enum qed_roce_qp_state qp_state)
2321 {
2322 	switch (qp_state) {
2323 	case QED_ROCE_QP_STATE_RESET:
2324 		return IB_QPS_RESET;
2325 	case QED_ROCE_QP_STATE_INIT:
2326 		return IB_QPS_INIT;
2327 	case QED_ROCE_QP_STATE_RTR:
2328 		return IB_QPS_RTR;
2329 	case QED_ROCE_QP_STATE_RTS:
2330 		return IB_QPS_RTS;
2331 	case QED_ROCE_QP_STATE_SQD:
2332 		return IB_QPS_SQD;
2333 	case QED_ROCE_QP_STATE_ERR:
2334 		return IB_QPS_ERR;
2335 	case QED_ROCE_QP_STATE_SQE:
2336 		return IB_QPS_SQE;
2337 	}
2338 	return IB_QPS_ERR;
2339 }
2340 
qedr_get_state_from_ibqp(enum ib_qp_state qp_state)2341 static enum qed_roce_qp_state qedr_get_state_from_ibqp(
2342 					enum ib_qp_state qp_state)
2343 {
2344 	switch (qp_state) {
2345 	case IB_QPS_RESET:
2346 		return QED_ROCE_QP_STATE_RESET;
2347 	case IB_QPS_INIT:
2348 		return QED_ROCE_QP_STATE_INIT;
2349 	case IB_QPS_RTR:
2350 		return QED_ROCE_QP_STATE_RTR;
2351 	case IB_QPS_RTS:
2352 		return QED_ROCE_QP_STATE_RTS;
2353 	case IB_QPS_SQD:
2354 		return QED_ROCE_QP_STATE_SQD;
2355 	case IB_QPS_ERR:
2356 		return QED_ROCE_QP_STATE_ERR;
2357 	default:
2358 		return QED_ROCE_QP_STATE_ERR;
2359 	}
2360 }
2361 
qedr_reset_qp_hwq_info(struct qedr_qp_hwq_info * qph)2362 static void qedr_reset_qp_hwq_info(struct qedr_qp_hwq_info *qph)
2363 {
2364 	qed_chain_reset(&qph->pbl);
2365 	qph->prod = 0;
2366 	qph->cons = 0;
2367 	qph->wqe_cons = 0;
2368 	qph->db_data.data.value = cpu_to_le16(0);
2369 }
2370 
qedr_update_qp_state(struct qedr_dev * dev,struct qedr_qp * qp,enum qed_roce_qp_state cur_state,enum qed_roce_qp_state new_state)2371 static int qedr_update_qp_state(struct qedr_dev *dev,
2372 				struct qedr_qp *qp,
2373 				enum qed_roce_qp_state cur_state,
2374 				enum qed_roce_qp_state new_state)
2375 {
2376 	int status = 0;
2377 
2378 	if (new_state == cur_state)
2379 		return 0;
2380 
2381 	switch (cur_state) {
2382 	case QED_ROCE_QP_STATE_RESET:
2383 		switch (new_state) {
2384 		case QED_ROCE_QP_STATE_INIT:
2385 			qp->prev_wqe_size = 0;
2386 			qedr_reset_qp_hwq_info(&qp->sq);
2387 			qedr_reset_qp_hwq_info(&qp->rq);
2388 			break;
2389 		default:
2390 			status = -EINVAL;
2391 			break;
2392 		}
2393 		break;
2394 	case QED_ROCE_QP_STATE_INIT:
2395 		switch (new_state) {
2396 		case QED_ROCE_QP_STATE_RTR:
2397 			/* Update doorbell (in case post_recv was
2398 			 * done before move to RTR)
2399 			 */
2400 
2401 			if (rdma_protocol_roce(&dev->ibdev, 1)) {
2402 				writel(qp->rq.db_data.raw, qp->rq.db);
2403 			}
2404 			break;
2405 		case QED_ROCE_QP_STATE_ERR:
2406 			break;
2407 		default:
2408 			/* Invalid state change. */
2409 			status = -EINVAL;
2410 			break;
2411 		}
2412 		break;
2413 	case QED_ROCE_QP_STATE_RTR:
2414 		/* RTR->XXX */
2415 		switch (new_state) {
2416 		case QED_ROCE_QP_STATE_RTS:
2417 			break;
2418 		case QED_ROCE_QP_STATE_ERR:
2419 			break;
2420 		default:
2421 			/* Invalid state change. */
2422 			status = -EINVAL;
2423 			break;
2424 		}
2425 		break;
2426 	case QED_ROCE_QP_STATE_RTS:
2427 		/* RTS->XXX */
2428 		switch (new_state) {
2429 		case QED_ROCE_QP_STATE_SQD:
2430 			break;
2431 		case QED_ROCE_QP_STATE_ERR:
2432 			break;
2433 		default:
2434 			/* Invalid state change. */
2435 			status = -EINVAL;
2436 			break;
2437 		}
2438 		break;
2439 	case QED_ROCE_QP_STATE_SQD:
2440 		/* SQD->XXX */
2441 		switch (new_state) {
2442 		case QED_ROCE_QP_STATE_RTS:
2443 		case QED_ROCE_QP_STATE_ERR:
2444 			break;
2445 		default:
2446 			/* Invalid state change. */
2447 			status = -EINVAL;
2448 			break;
2449 		}
2450 		break;
2451 	case QED_ROCE_QP_STATE_ERR:
2452 		/* ERR->XXX */
2453 		switch (new_state) {
2454 		case QED_ROCE_QP_STATE_RESET:
2455 			if ((qp->rq.prod != qp->rq.cons) ||
2456 			    (qp->sq.prod != qp->sq.cons)) {
2457 				DP_NOTICE(dev,
2458 					  "Error->Reset with rq/sq not empty rq.prod=%x rq.cons=%x sq.prod=%x sq.cons=%x\n",
2459 					  qp->rq.prod, qp->rq.cons, qp->sq.prod,
2460 					  qp->sq.cons);
2461 				status = -EINVAL;
2462 			}
2463 			break;
2464 		default:
2465 			status = -EINVAL;
2466 			break;
2467 		}
2468 		break;
2469 	default:
2470 		status = -EINVAL;
2471 		break;
2472 	}
2473 
2474 	return status;
2475 }
2476 
qedr_modify_qp(struct ib_qp * ibqp,struct ib_qp_attr * attr,int attr_mask,struct ib_udata * udata)2477 int qedr_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2478 		   int attr_mask, struct ib_udata *udata)
2479 {
2480 	struct qedr_qp *qp = get_qedr_qp(ibqp);
2481 	struct qed_rdma_modify_qp_in_params qp_params = { 0 };
2482 	struct qedr_dev *dev = get_qedr_dev(&qp->dev->ibdev);
2483 	const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
2484 	enum ib_qp_state old_qp_state, new_qp_state;
2485 	enum qed_roce_qp_state cur_state;
2486 	int rc = 0;
2487 
2488 	DP_DEBUG(dev, QEDR_MSG_QP,
2489 		 "modify qp: qp %p attr_mask=0x%x, state=%d", qp, attr_mask,
2490 		 attr->qp_state);
2491 
2492 	old_qp_state = qedr_get_ibqp_state(qp->state);
2493 	if (attr_mask & IB_QP_STATE)
2494 		new_qp_state = attr->qp_state;
2495 	else
2496 		new_qp_state = old_qp_state;
2497 
2498 	if (rdma_protocol_roce(&dev->ibdev, 1)) {
2499 		if (!ib_modify_qp_is_ok(old_qp_state, new_qp_state,
2500 					ibqp->qp_type, attr_mask)) {
2501 			DP_ERR(dev,
2502 			       "modify qp: invalid attribute mask=0x%x specified for\n"
2503 			       "qpn=0x%x of type=0x%x old_qp_state=0x%x, new_qp_state=0x%x\n",
2504 			       attr_mask, qp->qp_id, ibqp->qp_type,
2505 			       old_qp_state, new_qp_state);
2506 			rc = -EINVAL;
2507 			goto err;
2508 		}
2509 	}
2510 
2511 	/* Translate the masks... */
2512 	if (attr_mask & IB_QP_STATE) {
2513 		SET_FIELD(qp_params.modify_flags,
2514 			  QED_RDMA_MODIFY_QP_VALID_NEW_STATE, 1);
2515 		qp_params.new_state = qedr_get_state_from_ibqp(attr->qp_state);
2516 	}
2517 
2518 	if (attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
2519 		qp_params.sqd_async = true;
2520 
2521 	if (attr_mask & IB_QP_PKEY_INDEX) {
2522 		SET_FIELD(qp_params.modify_flags,
2523 			  QED_ROCE_MODIFY_QP_VALID_PKEY, 1);
2524 		if (attr->pkey_index >= QEDR_ROCE_PKEY_TABLE_LEN) {
2525 			rc = -EINVAL;
2526 			goto err;
2527 		}
2528 
2529 		qp_params.pkey = QEDR_ROCE_PKEY_DEFAULT;
2530 	}
2531 
2532 	if (attr_mask & IB_QP_QKEY)
2533 		qp->qkey = attr->qkey;
2534 
2535 	if (attr_mask & IB_QP_ACCESS_FLAGS) {
2536 		SET_FIELD(qp_params.modify_flags,
2537 			  QED_RDMA_MODIFY_QP_VALID_RDMA_OPS_EN, 1);
2538 		qp_params.incoming_rdma_read_en = attr->qp_access_flags &
2539 						  IB_ACCESS_REMOTE_READ;
2540 		qp_params.incoming_rdma_write_en = attr->qp_access_flags &
2541 						   IB_ACCESS_REMOTE_WRITE;
2542 		qp_params.incoming_atomic_en = attr->qp_access_flags &
2543 					       IB_ACCESS_REMOTE_ATOMIC;
2544 	}
2545 
2546 	if (attr_mask & (IB_QP_AV | IB_QP_PATH_MTU)) {
2547 		if (rdma_protocol_iwarp(&dev->ibdev, 1))
2548 			return -EINVAL;
2549 
2550 		if (attr_mask & IB_QP_PATH_MTU) {
2551 			if (attr->path_mtu < IB_MTU_256 ||
2552 			    attr->path_mtu > IB_MTU_4096) {
2553 				pr_err("error: Only MTU sizes of 256, 512, 1024, 2048 and 4096 are supported by RoCE\n");
2554 				rc = -EINVAL;
2555 				goto err;
2556 			}
2557 			qp->mtu = min(ib_mtu_enum_to_int(attr->path_mtu),
2558 				      ib_mtu_enum_to_int(iboe_get_mtu
2559 							 (dev->ndev->mtu)));
2560 		}
2561 
2562 		if (!qp->mtu) {
2563 			qp->mtu =
2564 			ib_mtu_enum_to_int(iboe_get_mtu(dev->ndev->mtu));
2565 			pr_err("Fixing zeroed MTU to qp->mtu = %d\n", qp->mtu);
2566 		}
2567 
2568 		SET_FIELD(qp_params.modify_flags,
2569 			  QED_ROCE_MODIFY_QP_VALID_ADDRESS_VECTOR, 1);
2570 
2571 		qp_params.traffic_class_tos = grh->traffic_class;
2572 		qp_params.flow_label = grh->flow_label;
2573 		qp_params.hop_limit_ttl = grh->hop_limit;
2574 
2575 		qp->sgid_idx = grh->sgid_index;
2576 
2577 		rc = get_gid_info_from_table(ibqp, attr, attr_mask, &qp_params);
2578 		if (rc) {
2579 			DP_ERR(dev,
2580 			       "modify qp: problems with GID index %d (rc=%d)\n",
2581 			       grh->sgid_index, rc);
2582 			return rc;
2583 		}
2584 
2585 		rc = qedr_get_dmac(dev, &attr->ah_attr,
2586 				   qp_params.remote_mac_addr);
2587 		if (rc)
2588 			return rc;
2589 
2590 		qp_params.use_local_mac = true;
2591 		ether_addr_copy(qp_params.local_mac_addr, dev->ndev->dev_addr);
2592 
2593 		DP_DEBUG(dev, QEDR_MSG_QP, "dgid=%x:%x:%x:%x\n",
2594 			 qp_params.dgid.dwords[0], qp_params.dgid.dwords[1],
2595 			 qp_params.dgid.dwords[2], qp_params.dgid.dwords[3]);
2596 		DP_DEBUG(dev, QEDR_MSG_QP, "sgid=%x:%x:%x:%x\n",
2597 			 qp_params.sgid.dwords[0], qp_params.sgid.dwords[1],
2598 			 qp_params.sgid.dwords[2], qp_params.sgid.dwords[3]);
2599 		DP_DEBUG(dev, QEDR_MSG_QP, "remote_mac=[%pM]\n",
2600 			 qp_params.remote_mac_addr);
2601 
2602 		qp_params.mtu = qp->mtu;
2603 		qp_params.lb_indication = false;
2604 	}
2605 
2606 	if (!qp_params.mtu) {
2607 		/* Stay with current MTU */
2608 		if (qp->mtu)
2609 			qp_params.mtu = qp->mtu;
2610 		else
2611 			qp_params.mtu =
2612 			    ib_mtu_enum_to_int(iboe_get_mtu(dev->ndev->mtu));
2613 	}
2614 
2615 	if (attr_mask & IB_QP_TIMEOUT) {
2616 		SET_FIELD(qp_params.modify_flags,
2617 			  QED_ROCE_MODIFY_QP_VALID_ACK_TIMEOUT, 1);
2618 
2619 		/* The received timeout value is an exponent used like this:
2620 		 *    "12.7.34 LOCAL ACK TIMEOUT
2621 		 *    Value representing the transport (ACK) timeout for use by
2622 		 *    the remote, expressed as: 4.096 * 2^timeout [usec]"
2623 		 * The FW expects timeout in msec so we need to divide the usec
2624 		 * result by 1000. We'll approximate 1000~2^10, and 4.096 ~ 2^2,
2625 		 * so we get: 2^2 * 2^timeout / 2^10 = 2^(timeout - 8).
2626 		 * The value of zero means infinite so we use a 'max_t' to make
2627 		 * sure that sub 1 msec values will be configured as 1 msec.
2628 		 */
2629 		if (attr->timeout)
2630 			qp_params.ack_timeout =
2631 					1 << max_t(int, attr->timeout - 8, 0);
2632 		else
2633 			qp_params.ack_timeout = 0;
2634 
2635 		qp->timeout = attr->timeout;
2636 	}
2637 
2638 	if (attr_mask & IB_QP_RETRY_CNT) {
2639 		SET_FIELD(qp_params.modify_flags,
2640 			  QED_ROCE_MODIFY_QP_VALID_RETRY_CNT, 1);
2641 		qp_params.retry_cnt = attr->retry_cnt;
2642 	}
2643 
2644 	if (attr_mask & IB_QP_RNR_RETRY) {
2645 		SET_FIELD(qp_params.modify_flags,
2646 			  QED_ROCE_MODIFY_QP_VALID_RNR_RETRY_CNT, 1);
2647 		qp_params.rnr_retry_cnt = attr->rnr_retry;
2648 	}
2649 
2650 	if (attr_mask & IB_QP_RQ_PSN) {
2651 		SET_FIELD(qp_params.modify_flags,
2652 			  QED_ROCE_MODIFY_QP_VALID_RQ_PSN, 1);
2653 		qp_params.rq_psn = attr->rq_psn;
2654 		qp->rq_psn = attr->rq_psn;
2655 	}
2656 
2657 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2658 		if (attr->max_rd_atomic > dev->attr.max_qp_req_rd_atomic_resc) {
2659 			rc = -EINVAL;
2660 			DP_ERR(dev,
2661 			       "unsupported max_rd_atomic=%d, supported=%d\n",
2662 			       attr->max_rd_atomic,
2663 			       dev->attr.max_qp_req_rd_atomic_resc);
2664 			goto err;
2665 		}
2666 
2667 		SET_FIELD(qp_params.modify_flags,
2668 			  QED_RDMA_MODIFY_QP_VALID_MAX_RD_ATOMIC_REQ, 1);
2669 		qp_params.max_rd_atomic_req = attr->max_rd_atomic;
2670 	}
2671 
2672 	if (attr_mask & IB_QP_MIN_RNR_TIMER) {
2673 		SET_FIELD(qp_params.modify_flags,
2674 			  QED_ROCE_MODIFY_QP_VALID_MIN_RNR_NAK_TIMER, 1);
2675 		qp_params.min_rnr_nak_timer = attr->min_rnr_timer;
2676 	}
2677 
2678 	if (attr_mask & IB_QP_SQ_PSN) {
2679 		SET_FIELD(qp_params.modify_flags,
2680 			  QED_ROCE_MODIFY_QP_VALID_SQ_PSN, 1);
2681 		qp_params.sq_psn = attr->sq_psn;
2682 		qp->sq_psn = attr->sq_psn;
2683 	}
2684 
2685 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2686 		if (attr->max_dest_rd_atomic >
2687 		    dev->attr.max_qp_resp_rd_atomic_resc) {
2688 			DP_ERR(dev,
2689 			       "unsupported max_dest_rd_atomic=%d, supported=%d\n",
2690 			       attr->max_dest_rd_atomic,
2691 			       dev->attr.max_qp_resp_rd_atomic_resc);
2692 
2693 			rc = -EINVAL;
2694 			goto err;
2695 		}
2696 
2697 		SET_FIELD(qp_params.modify_flags,
2698 			  QED_RDMA_MODIFY_QP_VALID_MAX_RD_ATOMIC_RESP, 1);
2699 		qp_params.max_rd_atomic_resp = attr->max_dest_rd_atomic;
2700 	}
2701 
2702 	if (attr_mask & IB_QP_DEST_QPN) {
2703 		SET_FIELD(qp_params.modify_flags,
2704 			  QED_ROCE_MODIFY_QP_VALID_DEST_QP, 1);
2705 
2706 		qp_params.dest_qp = attr->dest_qp_num;
2707 		qp->dest_qp_num = attr->dest_qp_num;
2708 	}
2709 
2710 	cur_state = qp->state;
2711 
2712 	/* Update the QP state before the actual ramrod to prevent a race with
2713 	 * fast path. Modifying the QP state to error will cause the device to
2714 	 * flush the CQEs and while polling the flushed CQEs will considered as
2715 	 * a potential issue if the QP isn't in error state.
2716 	 */
2717 	if ((attr_mask & IB_QP_STATE) && qp->qp_type != IB_QPT_GSI &&
2718 	    !udata && qp_params.new_state == QED_ROCE_QP_STATE_ERR)
2719 		qp->state = QED_ROCE_QP_STATE_ERR;
2720 
2721 	if (qp->qp_type != IB_QPT_GSI)
2722 		rc = dev->ops->rdma_modify_qp(dev->rdma_ctx,
2723 					      qp->qed_qp, &qp_params);
2724 
2725 	if (attr_mask & IB_QP_STATE) {
2726 		if ((qp->qp_type != IB_QPT_GSI) && (!udata))
2727 			rc = qedr_update_qp_state(dev, qp, cur_state,
2728 						  qp_params.new_state);
2729 		qp->state = qp_params.new_state;
2730 	}
2731 
2732 err:
2733 	return rc;
2734 }
2735 
qedr_to_ib_qp_acc_flags(struct qed_rdma_query_qp_out_params * params)2736 static int qedr_to_ib_qp_acc_flags(struct qed_rdma_query_qp_out_params *params)
2737 {
2738 	int ib_qp_acc_flags = 0;
2739 
2740 	if (params->incoming_rdma_write_en)
2741 		ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
2742 	if (params->incoming_rdma_read_en)
2743 		ib_qp_acc_flags |= IB_ACCESS_REMOTE_READ;
2744 	if (params->incoming_atomic_en)
2745 		ib_qp_acc_flags |= IB_ACCESS_REMOTE_ATOMIC;
2746 	ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
2747 	return ib_qp_acc_flags;
2748 }
2749 
qedr_query_qp(struct ib_qp * ibqp,struct ib_qp_attr * qp_attr,int attr_mask,struct ib_qp_init_attr * qp_init_attr)2750 int qedr_query_qp(struct ib_qp *ibqp,
2751 		  struct ib_qp_attr *qp_attr,
2752 		  int attr_mask, struct ib_qp_init_attr *qp_init_attr)
2753 {
2754 	struct qed_rdma_query_qp_out_params params;
2755 	struct qedr_qp *qp = get_qedr_qp(ibqp);
2756 	struct qedr_dev *dev = qp->dev;
2757 	int rc = 0;
2758 
2759 	memset(&params, 0, sizeof(params));
2760 	memset(qp_attr, 0, sizeof(*qp_attr));
2761 	memset(qp_init_attr, 0, sizeof(*qp_init_attr));
2762 
2763 	if (qp->qp_type != IB_QPT_GSI) {
2764 		rc = dev->ops->rdma_query_qp(dev->rdma_ctx, qp->qed_qp, &params);
2765 		if (rc)
2766 			goto err;
2767 		qp_attr->qp_state = qedr_get_ibqp_state(params.state);
2768 	} else {
2769 		qp_attr->qp_state = qedr_get_ibqp_state(QED_ROCE_QP_STATE_RTS);
2770 	}
2771 
2772 	qp_attr->cur_qp_state = qedr_get_ibqp_state(params.state);
2773 	qp_attr->path_mtu = ib_mtu_int_to_enum(params.mtu);
2774 	qp_attr->path_mig_state = IB_MIG_MIGRATED;
2775 	qp_attr->rq_psn = params.rq_psn;
2776 	qp_attr->sq_psn = params.sq_psn;
2777 	qp_attr->dest_qp_num = params.dest_qp;
2778 
2779 	qp_attr->qp_access_flags = qedr_to_ib_qp_acc_flags(&params);
2780 
2781 	qp_attr->cap.max_send_wr = qp->sq.max_wr;
2782 	qp_attr->cap.max_recv_wr = qp->rq.max_wr;
2783 	qp_attr->cap.max_send_sge = qp->sq.max_sges;
2784 	qp_attr->cap.max_recv_sge = qp->rq.max_sges;
2785 	qp_attr->cap.max_inline_data = dev->attr.max_inline;
2786 	qp_init_attr->cap = qp_attr->cap;
2787 
2788 	qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
2789 	rdma_ah_set_grh(&qp_attr->ah_attr, NULL,
2790 			params.flow_label, qp->sgid_idx,
2791 			params.hop_limit_ttl, params.traffic_class_tos);
2792 	rdma_ah_set_dgid_raw(&qp_attr->ah_attr, &params.dgid.bytes[0]);
2793 	rdma_ah_set_port_num(&qp_attr->ah_attr, 1);
2794 	rdma_ah_set_sl(&qp_attr->ah_attr, 0);
2795 	qp_attr->timeout = qp->timeout;
2796 	qp_attr->rnr_retry = params.rnr_retry;
2797 	qp_attr->retry_cnt = params.retry_cnt;
2798 	qp_attr->min_rnr_timer = params.min_rnr_nak_timer;
2799 	qp_attr->pkey_index = params.pkey_index;
2800 	qp_attr->port_num = 1;
2801 	rdma_ah_set_path_bits(&qp_attr->ah_attr, 0);
2802 	rdma_ah_set_static_rate(&qp_attr->ah_attr, 0);
2803 	qp_attr->alt_pkey_index = 0;
2804 	qp_attr->alt_port_num = 0;
2805 	qp_attr->alt_timeout = 0;
2806 	memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
2807 
2808 	qp_attr->sq_draining = (params.state == QED_ROCE_QP_STATE_SQD) ? 1 : 0;
2809 	qp_attr->max_dest_rd_atomic = params.max_dest_rd_atomic;
2810 	qp_attr->max_rd_atomic = params.max_rd_atomic;
2811 	qp_attr->en_sqd_async_notify = (params.sqd_async) ? 1 : 0;
2812 
2813 	DP_DEBUG(dev, QEDR_MSG_QP, "QEDR_QUERY_QP: max_inline_data=%d\n",
2814 		 qp_attr->cap.max_inline_data);
2815 
2816 err:
2817 	return rc;
2818 }
2819 
qedr_destroy_qp(struct ib_qp * ibqp,struct ib_udata * udata)2820 int qedr_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
2821 {
2822 	struct qedr_qp *qp = get_qedr_qp(ibqp);
2823 	struct qedr_dev *dev = qp->dev;
2824 	struct ib_qp_attr attr;
2825 	int attr_mask = 0;
2826 
2827 	DP_DEBUG(dev, QEDR_MSG_QP, "destroy qp: destroying %p, qp type=%d\n",
2828 		 qp, qp->qp_type);
2829 
2830 	if (rdma_protocol_roce(&dev->ibdev, 1)) {
2831 		if ((qp->state != QED_ROCE_QP_STATE_RESET) &&
2832 		    (qp->state != QED_ROCE_QP_STATE_ERR) &&
2833 		    (qp->state != QED_ROCE_QP_STATE_INIT)) {
2834 
2835 			attr.qp_state = IB_QPS_ERR;
2836 			attr_mask |= IB_QP_STATE;
2837 
2838 			/* Change the QP state to ERROR */
2839 			qedr_modify_qp(ibqp, &attr, attr_mask, NULL);
2840 		}
2841 	} else {
2842 		/* If connection establishment started the WAIT_FOR_CONNECT
2843 		 * bit will be on and we need to Wait for the establishment
2844 		 * to complete before destroying the qp.
2845 		 */
2846 		if (test_and_set_bit(QEDR_IWARP_CM_WAIT_FOR_CONNECT,
2847 				     &qp->iwarp_cm_flags))
2848 			wait_for_completion(&qp->iwarp_cm_comp);
2849 
2850 		/* If graceful disconnect started, the WAIT_FOR_DISCONNECT
2851 		 * bit will be on, and we need to wait for the disconnect to
2852 		 * complete before continuing. We can use the same completion,
2853 		 * iwarp_cm_comp, since this is the only place that waits for
2854 		 * this completion and it is sequential. In addition,
2855 		 * disconnect can't occur before the connection is fully
2856 		 * established, therefore if WAIT_FOR_DISCONNECT is on it
2857 		 * means WAIT_FOR_CONNECT is also on and the completion for
2858 		 * CONNECT already occurred.
2859 		 */
2860 		if (test_and_set_bit(QEDR_IWARP_CM_WAIT_FOR_DISCONNECT,
2861 				     &qp->iwarp_cm_flags))
2862 			wait_for_completion(&qp->iwarp_cm_comp);
2863 	}
2864 
2865 	if (qp->qp_type == IB_QPT_GSI)
2866 		qedr_destroy_gsi_qp(dev);
2867 
2868 	/* We need to remove the entry from the xarray before we release the
2869 	 * qp_id to avoid a race of the qp_id being reallocated and failing
2870 	 * on xa_insert
2871 	 */
2872 	if (rdma_protocol_iwarp(&dev->ibdev, 1))
2873 		xa_erase(&dev->qps, qp->qp_id);
2874 
2875 	qedr_free_qp_resources(dev, qp, udata);
2876 
2877 	if (rdma_protocol_iwarp(&dev->ibdev, 1))
2878 		qedr_iw_qp_rem_ref(&qp->ibqp);
2879 	else
2880 		kfree(qp);
2881 
2882 	return 0;
2883 }
2884 
qedr_create_ah(struct ib_ah * ibah,struct rdma_ah_init_attr * init_attr,struct ib_udata * udata)2885 int qedr_create_ah(struct ib_ah *ibah, struct rdma_ah_init_attr *init_attr,
2886 		   struct ib_udata *udata)
2887 {
2888 	struct qedr_ah *ah = get_qedr_ah(ibah);
2889 
2890 	rdma_copy_ah_attr(&ah->attr, init_attr->ah_attr);
2891 
2892 	return 0;
2893 }
2894 
qedr_destroy_ah(struct ib_ah * ibah,u32 flags)2895 int qedr_destroy_ah(struct ib_ah *ibah, u32 flags)
2896 {
2897 	struct qedr_ah *ah = get_qedr_ah(ibah);
2898 
2899 	rdma_destroy_ah_attr(&ah->attr);
2900 	return 0;
2901 }
2902 
free_mr_info(struct qedr_dev * dev,struct mr_info * info)2903 static void free_mr_info(struct qedr_dev *dev, struct mr_info *info)
2904 {
2905 	struct qedr_pbl *pbl, *tmp;
2906 
2907 	if (info->pbl_table)
2908 		list_add_tail(&info->pbl_table->list_entry,
2909 			      &info->free_pbl_list);
2910 
2911 	if (!list_empty(&info->inuse_pbl_list))
2912 		list_splice(&info->inuse_pbl_list, &info->free_pbl_list);
2913 
2914 	list_for_each_entry_safe(pbl, tmp, &info->free_pbl_list, list_entry) {
2915 		list_del(&pbl->list_entry);
2916 		qedr_free_pbl(dev, &info->pbl_info, pbl);
2917 	}
2918 }
2919 
init_mr_info(struct qedr_dev * dev,struct mr_info * info,size_t page_list_len,bool two_layered)2920 static int init_mr_info(struct qedr_dev *dev, struct mr_info *info,
2921 			size_t page_list_len, bool two_layered)
2922 {
2923 	struct qedr_pbl *tmp;
2924 	int rc;
2925 
2926 	INIT_LIST_HEAD(&info->free_pbl_list);
2927 	INIT_LIST_HEAD(&info->inuse_pbl_list);
2928 
2929 	rc = qedr_prepare_pbl_tbl(dev, &info->pbl_info,
2930 				  page_list_len, two_layered);
2931 	if (rc)
2932 		goto done;
2933 
2934 	info->pbl_table = qedr_alloc_pbl_tbl(dev, &info->pbl_info, GFP_KERNEL);
2935 	if (IS_ERR(info->pbl_table)) {
2936 		rc = PTR_ERR(info->pbl_table);
2937 		goto done;
2938 	}
2939 
2940 	DP_DEBUG(dev, QEDR_MSG_MR, "pbl_table_pa = %pa\n",
2941 		 &info->pbl_table->pa);
2942 
2943 	/* in usual case we use 2 PBLs, so we add one to free
2944 	 * list and allocating another one
2945 	 */
2946 	tmp = qedr_alloc_pbl_tbl(dev, &info->pbl_info, GFP_KERNEL);
2947 	if (IS_ERR(tmp)) {
2948 		DP_DEBUG(dev, QEDR_MSG_MR, "Extra PBL is not allocated\n");
2949 		goto done;
2950 	}
2951 
2952 	list_add_tail(&tmp->list_entry, &info->free_pbl_list);
2953 
2954 	DP_DEBUG(dev, QEDR_MSG_MR, "extra pbl_table_pa = %pa\n", &tmp->pa);
2955 
2956 done:
2957 	if (rc)
2958 		free_mr_info(dev, info);
2959 
2960 	return rc;
2961 }
2962 
qedr_reg_user_mr(struct ib_pd * ibpd,u64 start,u64 len,u64 usr_addr,int acc,struct ib_udata * udata)2963 struct ib_mr *qedr_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
2964 			       u64 usr_addr, int acc, struct ib_udata *udata)
2965 {
2966 	struct qedr_dev *dev = get_qedr_dev(ibpd->device);
2967 	struct qedr_mr *mr;
2968 	struct qedr_pd *pd;
2969 	int rc = -ENOMEM;
2970 
2971 	pd = get_qedr_pd(ibpd);
2972 	DP_DEBUG(dev, QEDR_MSG_MR,
2973 		 "qedr_register user mr pd = %d start = %lld, len = %lld, usr_addr = %lld, acc = %d\n",
2974 		 pd->pd_id, start, len, usr_addr, acc);
2975 
2976 	if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
2977 		return ERR_PTR(-EINVAL);
2978 
2979 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2980 	if (!mr)
2981 		return ERR_PTR(rc);
2982 
2983 	mr->type = QEDR_MR_USER;
2984 
2985 	mr->umem = ib_umem_get(ibpd->device, start, len, acc);
2986 	if (IS_ERR(mr->umem)) {
2987 		rc = -EFAULT;
2988 		goto err0;
2989 	}
2990 
2991 	rc = init_mr_info(dev, &mr->info,
2992 			  ib_umem_num_dma_blocks(mr->umem, PAGE_SIZE), 1);
2993 	if (rc)
2994 		goto err1;
2995 
2996 	qedr_populate_pbls(dev, mr->umem, mr->info.pbl_table,
2997 			   &mr->info.pbl_info, PAGE_SHIFT);
2998 
2999 	rc = dev->ops->rdma_alloc_tid(dev->rdma_ctx, &mr->hw_mr.itid);
3000 	if (rc) {
3001 		if (rc == -EINVAL)
3002 			DP_ERR(dev, "Out of MR resources\n");
3003 		else
3004 			DP_ERR(dev, "roce alloc tid returned error %d\n", rc);
3005 
3006 		goto err1;
3007 	}
3008 
3009 	/* Index only, 18 bit long, lkey = itid << 8 | key */
3010 	mr->hw_mr.tid_type = QED_RDMA_TID_REGISTERED_MR;
3011 	mr->hw_mr.key = 0;
3012 	mr->hw_mr.pd = pd->pd_id;
3013 	mr->hw_mr.local_read = 1;
3014 	mr->hw_mr.local_write = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
3015 	mr->hw_mr.remote_read = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
3016 	mr->hw_mr.remote_write = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
3017 	mr->hw_mr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
3018 	mr->hw_mr.mw_bind = false;
3019 	mr->hw_mr.pbl_ptr = mr->info.pbl_table[0].pa;
3020 	mr->hw_mr.pbl_two_level = mr->info.pbl_info.two_layered;
3021 	mr->hw_mr.pbl_page_size_log = ilog2(mr->info.pbl_info.pbl_size);
3022 	mr->hw_mr.page_size_log = PAGE_SHIFT;
3023 	mr->hw_mr.length = len;
3024 	mr->hw_mr.vaddr = usr_addr;
3025 	mr->hw_mr.phy_mr = false;
3026 	mr->hw_mr.dma_mr = false;
3027 
3028 	rc = dev->ops->rdma_register_tid(dev->rdma_ctx, &mr->hw_mr);
3029 	if (rc) {
3030 		DP_ERR(dev, "roce register tid returned an error %d\n", rc);
3031 		goto err2;
3032 	}
3033 
3034 	mr->ibmr.lkey = mr->hw_mr.itid << 8 | mr->hw_mr.key;
3035 	if (mr->hw_mr.remote_write || mr->hw_mr.remote_read ||
3036 	    mr->hw_mr.remote_atomic)
3037 		mr->ibmr.rkey = mr->hw_mr.itid << 8 | mr->hw_mr.key;
3038 
3039 	DP_DEBUG(dev, QEDR_MSG_MR, "register user mr lkey: %x\n",
3040 		 mr->ibmr.lkey);
3041 	return &mr->ibmr;
3042 
3043 err2:
3044 	dev->ops->rdma_free_tid(dev->rdma_ctx, mr->hw_mr.itid);
3045 err1:
3046 	qedr_free_pbl(dev, &mr->info.pbl_info, mr->info.pbl_table);
3047 err0:
3048 	kfree(mr);
3049 	return ERR_PTR(rc);
3050 }
3051 
qedr_dereg_mr(struct ib_mr * ib_mr,struct ib_udata * udata)3052 int qedr_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata)
3053 {
3054 	struct qedr_mr *mr = get_qedr_mr(ib_mr);
3055 	struct qedr_dev *dev = get_qedr_dev(ib_mr->device);
3056 	int rc = 0;
3057 
3058 	rc = dev->ops->rdma_deregister_tid(dev->rdma_ctx, mr->hw_mr.itid);
3059 	if (rc)
3060 		return rc;
3061 
3062 	dev->ops->rdma_free_tid(dev->rdma_ctx, mr->hw_mr.itid);
3063 
3064 	if (mr->type != QEDR_MR_DMA)
3065 		free_mr_info(dev, &mr->info);
3066 
3067 	/* it could be user registered memory. */
3068 	ib_umem_release(mr->umem);
3069 
3070 	kfree(mr);
3071 
3072 	return rc;
3073 }
3074 
__qedr_alloc_mr(struct ib_pd * ibpd,int max_page_list_len)3075 static struct qedr_mr *__qedr_alloc_mr(struct ib_pd *ibpd,
3076 				       int max_page_list_len)
3077 {
3078 	struct qedr_pd *pd = get_qedr_pd(ibpd);
3079 	struct qedr_dev *dev = get_qedr_dev(ibpd->device);
3080 	struct qedr_mr *mr;
3081 	int rc = -ENOMEM;
3082 
3083 	DP_DEBUG(dev, QEDR_MSG_MR,
3084 		 "qedr_alloc_frmr pd = %d max_page_list_len= %d\n", pd->pd_id,
3085 		 max_page_list_len);
3086 
3087 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3088 	if (!mr)
3089 		return ERR_PTR(rc);
3090 
3091 	mr->dev = dev;
3092 	mr->type = QEDR_MR_FRMR;
3093 
3094 	rc = init_mr_info(dev, &mr->info, max_page_list_len, 1);
3095 	if (rc)
3096 		goto err0;
3097 
3098 	rc = dev->ops->rdma_alloc_tid(dev->rdma_ctx, &mr->hw_mr.itid);
3099 	if (rc) {
3100 		if (rc == -EINVAL)
3101 			DP_ERR(dev, "Out of MR resources\n");
3102 		else
3103 			DP_ERR(dev, "roce alloc tid returned error %d\n", rc);
3104 
3105 		goto err1;
3106 	}
3107 
3108 	/* Index only, 18 bit long, lkey = itid << 8 | key */
3109 	mr->hw_mr.tid_type = QED_RDMA_TID_FMR;
3110 	mr->hw_mr.key = 0;
3111 	mr->hw_mr.pd = pd->pd_id;
3112 	mr->hw_mr.local_read = 1;
3113 	mr->hw_mr.local_write = 0;
3114 	mr->hw_mr.remote_read = 0;
3115 	mr->hw_mr.remote_write = 0;
3116 	mr->hw_mr.remote_atomic = 0;
3117 	mr->hw_mr.mw_bind = false;
3118 	mr->hw_mr.pbl_ptr = 0;
3119 	mr->hw_mr.pbl_two_level = mr->info.pbl_info.two_layered;
3120 	mr->hw_mr.pbl_page_size_log = ilog2(mr->info.pbl_info.pbl_size);
3121 	mr->hw_mr.length = 0;
3122 	mr->hw_mr.vaddr = 0;
3123 	mr->hw_mr.phy_mr = true;
3124 	mr->hw_mr.dma_mr = false;
3125 
3126 	rc = dev->ops->rdma_register_tid(dev->rdma_ctx, &mr->hw_mr);
3127 	if (rc) {
3128 		DP_ERR(dev, "roce register tid returned an error %d\n", rc);
3129 		goto err2;
3130 	}
3131 
3132 	mr->ibmr.lkey = mr->hw_mr.itid << 8 | mr->hw_mr.key;
3133 	mr->ibmr.rkey = mr->ibmr.lkey;
3134 
3135 	DP_DEBUG(dev, QEDR_MSG_MR, "alloc frmr: %x\n", mr->ibmr.lkey);
3136 	return mr;
3137 
3138 err2:
3139 	dev->ops->rdma_free_tid(dev->rdma_ctx, mr->hw_mr.itid);
3140 err1:
3141 	qedr_free_pbl(dev, &mr->info.pbl_info, mr->info.pbl_table);
3142 err0:
3143 	kfree(mr);
3144 	return ERR_PTR(rc);
3145 }
3146 
qedr_alloc_mr(struct ib_pd * ibpd,enum ib_mr_type mr_type,u32 max_num_sg)3147 struct ib_mr *qedr_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
3148 			    u32 max_num_sg)
3149 {
3150 	struct qedr_mr *mr;
3151 
3152 	if (mr_type != IB_MR_TYPE_MEM_REG)
3153 		return ERR_PTR(-EINVAL);
3154 
3155 	mr = __qedr_alloc_mr(ibpd, max_num_sg);
3156 
3157 	if (IS_ERR(mr))
3158 		return ERR_PTR(-EINVAL);
3159 
3160 	return &mr->ibmr;
3161 }
3162 
qedr_set_page(struct ib_mr * ibmr,u64 addr)3163 static int qedr_set_page(struct ib_mr *ibmr, u64 addr)
3164 {
3165 	struct qedr_mr *mr = get_qedr_mr(ibmr);
3166 	struct qedr_pbl *pbl_table;
3167 	struct regpair *pbe;
3168 	u32 pbes_in_page;
3169 
3170 	if (unlikely(mr->npages == mr->info.pbl_info.num_pbes)) {
3171 		DP_ERR(mr->dev, "qedr_set_page fails when %d\n", mr->npages);
3172 		return -ENOMEM;
3173 	}
3174 
3175 	DP_DEBUG(mr->dev, QEDR_MSG_MR, "qedr_set_page pages[%d] = 0x%llx\n",
3176 		 mr->npages, addr);
3177 
3178 	pbes_in_page = mr->info.pbl_info.pbl_size / sizeof(u64);
3179 	pbl_table = mr->info.pbl_table + (mr->npages / pbes_in_page);
3180 	pbe = (struct regpair *)pbl_table->va;
3181 	pbe +=  mr->npages % pbes_in_page;
3182 	pbe->lo = cpu_to_le32((u32)addr);
3183 	pbe->hi = cpu_to_le32((u32)upper_32_bits(addr));
3184 
3185 	mr->npages++;
3186 
3187 	return 0;
3188 }
3189 
handle_completed_mrs(struct qedr_dev * dev,struct mr_info * info)3190 static void handle_completed_mrs(struct qedr_dev *dev, struct mr_info *info)
3191 {
3192 	int work = info->completed - info->completed_handled - 1;
3193 
3194 	DP_DEBUG(dev, QEDR_MSG_MR, "Special FMR work = %d\n", work);
3195 	while (work-- > 0 && !list_empty(&info->inuse_pbl_list)) {
3196 		struct qedr_pbl *pbl;
3197 
3198 		/* Free all the page list that are possible to be freed
3199 		 * (all the ones that were invalidated), under the assumption
3200 		 * that if an FMR was completed successfully that means that
3201 		 * if there was an invalidate operation before it also ended
3202 		 */
3203 		pbl = list_first_entry(&info->inuse_pbl_list,
3204 				       struct qedr_pbl, list_entry);
3205 		list_move_tail(&pbl->list_entry, &info->free_pbl_list);
3206 		info->completed_handled++;
3207 	}
3208 }
3209 
qedr_map_mr_sg(struct ib_mr * ibmr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset)3210 int qedr_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
3211 		   int sg_nents, unsigned int *sg_offset)
3212 {
3213 	struct qedr_mr *mr = get_qedr_mr(ibmr);
3214 
3215 	mr->npages = 0;
3216 
3217 	handle_completed_mrs(mr->dev, &mr->info);
3218 	return ib_sg_to_pages(ibmr, sg, sg_nents, NULL, qedr_set_page);
3219 }
3220 
qedr_get_dma_mr(struct ib_pd * ibpd,int acc)3221 struct ib_mr *qedr_get_dma_mr(struct ib_pd *ibpd, int acc)
3222 {
3223 	struct qedr_dev *dev = get_qedr_dev(ibpd->device);
3224 	struct qedr_pd *pd = get_qedr_pd(ibpd);
3225 	struct qedr_mr *mr;
3226 	int rc;
3227 
3228 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3229 	if (!mr)
3230 		return ERR_PTR(-ENOMEM);
3231 
3232 	mr->type = QEDR_MR_DMA;
3233 
3234 	rc = dev->ops->rdma_alloc_tid(dev->rdma_ctx, &mr->hw_mr.itid);
3235 	if (rc) {
3236 		if (rc == -EINVAL)
3237 			DP_ERR(dev, "Out of MR resources\n");
3238 		else
3239 			DP_ERR(dev, "roce alloc tid returned error %d\n", rc);
3240 
3241 		goto err1;
3242 	}
3243 
3244 	/* index only, 18 bit long, lkey = itid << 8 | key */
3245 	mr->hw_mr.tid_type = QED_RDMA_TID_REGISTERED_MR;
3246 	mr->hw_mr.pd = pd->pd_id;
3247 	mr->hw_mr.local_read = 1;
3248 	mr->hw_mr.local_write = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
3249 	mr->hw_mr.remote_read = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
3250 	mr->hw_mr.remote_write = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
3251 	mr->hw_mr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
3252 	mr->hw_mr.dma_mr = true;
3253 
3254 	rc = dev->ops->rdma_register_tid(dev->rdma_ctx, &mr->hw_mr);
3255 	if (rc) {
3256 		DP_ERR(dev, "roce register tid returned an error %d\n", rc);
3257 		goto err2;
3258 	}
3259 
3260 	mr->ibmr.lkey = mr->hw_mr.itid << 8 | mr->hw_mr.key;
3261 	if (mr->hw_mr.remote_write || mr->hw_mr.remote_read ||
3262 	    mr->hw_mr.remote_atomic)
3263 		mr->ibmr.rkey = mr->hw_mr.itid << 8 | mr->hw_mr.key;
3264 
3265 	DP_DEBUG(dev, QEDR_MSG_MR, "get dma mr: lkey = %x\n", mr->ibmr.lkey);
3266 	return &mr->ibmr;
3267 
3268 err2:
3269 	dev->ops->rdma_free_tid(dev->rdma_ctx, mr->hw_mr.itid);
3270 err1:
3271 	kfree(mr);
3272 	return ERR_PTR(rc);
3273 }
3274 
qedr_wq_is_full(struct qedr_qp_hwq_info * wq)3275 static inline int qedr_wq_is_full(struct qedr_qp_hwq_info *wq)
3276 {
3277 	return (((wq->prod + 1) % wq->max_wr) == wq->cons);
3278 }
3279 
sge_data_len(struct ib_sge * sg_list,int num_sge)3280 static int sge_data_len(struct ib_sge *sg_list, int num_sge)
3281 {
3282 	int i, len = 0;
3283 
3284 	for (i = 0; i < num_sge; i++)
3285 		len += sg_list[i].length;
3286 
3287 	return len;
3288 }
3289 
swap_wqe_data64(u64 * p)3290 static void swap_wqe_data64(u64 *p)
3291 {
3292 	int i;
3293 
3294 	for (i = 0; i < QEDR_SQE_ELEMENT_SIZE / sizeof(u64); i++, p++)
3295 		*p = cpu_to_be64(cpu_to_le64(*p));
3296 }
3297 
qedr_prepare_sq_inline_data(struct qedr_dev * dev,struct qedr_qp * qp,u8 * wqe_size,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr,u8 * bits,u8 bit)3298 static u32 qedr_prepare_sq_inline_data(struct qedr_dev *dev,
3299 				       struct qedr_qp *qp, u8 *wqe_size,
3300 				       const struct ib_send_wr *wr,
3301 				       const struct ib_send_wr **bad_wr,
3302 				       u8 *bits, u8 bit)
3303 {
3304 	u32 data_size = sge_data_len(wr->sg_list, wr->num_sge);
3305 	char *seg_prt, *wqe;
3306 	int i, seg_siz;
3307 
3308 	if (data_size > ROCE_REQ_MAX_INLINE_DATA_SIZE) {
3309 		DP_ERR(dev, "Too much inline data in WR: %d\n", data_size);
3310 		*bad_wr = wr;
3311 		return 0;
3312 	}
3313 
3314 	if (!data_size)
3315 		return data_size;
3316 
3317 	*bits |= bit;
3318 
3319 	seg_prt = NULL;
3320 	wqe = NULL;
3321 	seg_siz = 0;
3322 
3323 	/* Copy data inline */
3324 	for (i = 0; i < wr->num_sge; i++) {
3325 		u32 len = wr->sg_list[i].length;
3326 		void *src = (void *)(uintptr_t)wr->sg_list[i].addr;
3327 
3328 		while (len > 0) {
3329 			u32 cur;
3330 
3331 			/* New segment required */
3332 			if (!seg_siz) {
3333 				wqe = (char *)qed_chain_produce(&qp->sq.pbl);
3334 				seg_prt = wqe;
3335 				seg_siz = sizeof(struct rdma_sq_common_wqe);
3336 				(*wqe_size)++;
3337 			}
3338 
3339 			/* Calculate currently allowed length */
3340 			cur = min_t(u32, len, seg_siz);
3341 			memcpy(seg_prt, src, cur);
3342 
3343 			/* Update segment variables */
3344 			seg_prt += cur;
3345 			seg_siz -= cur;
3346 
3347 			/* Update sge variables */
3348 			src += cur;
3349 			len -= cur;
3350 
3351 			/* Swap fully-completed segments */
3352 			if (!seg_siz)
3353 				swap_wqe_data64((u64 *)wqe);
3354 		}
3355 	}
3356 
3357 	/* swap last not completed segment */
3358 	if (seg_siz)
3359 		swap_wqe_data64((u64 *)wqe);
3360 
3361 	return data_size;
3362 }
3363 
3364 #define RQ_SGE_SET(sge, vaddr, vlength, vflags)			\
3365 	do {							\
3366 		DMA_REGPAIR_LE(sge->addr, vaddr);		\
3367 		(sge)->length = cpu_to_le32(vlength);		\
3368 		(sge)->flags = cpu_to_le32(vflags);		\
3369 	} while (0)
3370 
3371 #define SRQ_HDR_SET(hdr, vwr_id, num_sge)			\
3372 	do {							\
3373 		DMA_REGPAIR_LE(hdr->wr_id, vwr_id);		\
3374 		(hdr)->num_sges = num_sge;			\
3375 	} while (0)
3376 
3377 #define SRQ_SGE_SET(sge, vaddr, vlength, vlkey)			\
3378 	do {							\
3379 		DMA_REGPAIR_LE(sge->addr, vaddr);		\
3380 		(sge)->length = cpu_to_le32(vlength);		\
3381 		(sge)->l_key = cpu_to_le32(vlkey);		\
3382 	} while (0)
3383 
qedr_prepare_sq_sges(struct qedr_qp * qp,u8 * wqe_size,const struct ib_send_wr * wr)3384 static u32 qedr_prepare_sq_sges(struct qedr_qp *qp, u8 *wqe_size,
3385 				const struct ib_send_wr *wr)
3386 {
3387 	u32 data_size = 0;
3388 	int i;
3389 
3390 	for (i = 0; i < wr->num_sge; i++) {
3391 		struct rdma_sq_sge *sge = qed_chain_produce(&qp->sq.pbl);
3392 
3393 		DMA_REGPAIR_LE(sge->addr, wr->sg_list[i].addr);
3394 		sge->l_key = cpu_to_le32(wr->sg_list[i].lkey);
3395 		sge->length = cpu_to_le32(wr->sg_list[i].length);
3396 		data_size += wr->sg_list[i].length;
3397 	}
3398 
3399 	if (wqe_size)
3400 		*wqe_size += wr->num_sge;
3401 
3402 	return data_size;
3403 }
3404 
qedr_prepare_sq_rdma_data(struct qedr_dev * dev,struct qedr_qp * qp,struct rdma_sq_rdma_wqe_1st * rwqe,struct rdma_sq_rdma_wqe_2nd * rwqe2,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)3405 static u32 qedr_prepare_sq_rdma_data(struct qedr_dev *dev,
3406 				     struct qedr_qp *qp,
3407 				     struct rdma_sq_rdma_wqe_1st *rwqe,
3408 				     struct rdma_sq_rdma_wqe_2nd *rwqe2,
3409 				     const struct ib_send_wr *wr,
3410 				     const struct ib_send_wr **bad_wr)
3411 {
3412 	rwqe2->r_key = cpu_to_le32(rdma_wr(wr)->rkey);
3413 	DMA_REGPAIR_LE(rwqe2->remote_va, rdma_wr(wr)->remote_addr);
3414 
3415 	if (wr->send_flags & IB_SEND_INLINE &&
3416 	    (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
3417 	     wr->opcode == IB_WR_RDMA_WRITE)) {
3418 		u8 flags = 0;
3419 
3420 		SET_FIELD2(flags, RDMA_SQ_RDMA_WQE_1ST_INLINE_FLG, 1);
3421 		return qedr_prepare_sq_inline_data(dev, qp, &rwqe->wqe_size, wr,
3422 						   bad_wr, &rwqe->flags, flags);
3423 	}
3424 
3425 	return qedr_prepare_sq_sges(qp, &rwqe->wqe_size, wr);
3426 }
3427 
qedr_prepare_sq_send_data(struct qedr_dev * dev,struct qedr_qp * qp,struct rdma_sq_send_wqe_1st * swqe,struct rdma_sq_send_wqe_2st * swqe2,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)3428 static u32 qedr_prepare_sq_send_data(struct qedr_dev *dev,
3429 				     struct qedr_qp *qp,
3430 				     struct rdma_sq_send_wqe_1st *swqe,
3431 				     struct rdma_sq_send_wqe_2st *swqe2,
3432 				     const struct ib_send_wr *wr,
3433 				     const struct ib_send_wr **bad_wr)
3434 {
3435 	memset(swqe2, 0, sizeof(*swqe2));
3436 	if (wr->send_flags & IB_SEND_INLINE) {
3437 		u8 flags = 0;
3438 
3439 		SET_FIELD2(flags, RDMA_SQ_SEND_WQE_INLINE_FLG, 1);
3440 		return qedr_prepare_sq_inline_data(dev, qp, &swqe->wqe_size, wr,
3441 						   bad_wr, &swqe->flags, flags);
3442 	}
3443 
3444 	return qedr_prepare_sq_sges(qp, &swqe->wqe_size, wr);
3445 }
3446 
qedr_prepare_reg(struct qedr_qp * qp,struct rdma_sq_fmr_wqe_1st * fwqe1,const struct ib_reg_wr * wr)3447 static int qedr_prepare_reg(struct qedr_qp *qp,
3448 			    struct rdma_sq_fmr_wqe_1st *fwqe1,
3449 			    const struct ib_reg_wr *wr)
3450 {
3451 	struct qedr_mr *mr = get_qedr_mr(wr->mr);
3452 	struct rdma_sq_fmr_wqe_2nd *fwqe2;
3453 
3454 	fwqe2 = (struct rdma_sq_fmr_wqe_2nd *)qed_chain_produce(&qp->sq.pbl);
3455 	fwqe1->addr.hi = upper_32_bits(mr->ibmr.iova);
3456 	fwqe1->addr.lo = lower_32_bits(mr->ibmr.iova);
3457 	fwqe1->l_key = wr->key;
3458 
3459 	fwqe2->access_ctrl = 0;
3460 
3461 	SET_FIELD2(fwqe2->access_ctrl, RDMA_SQ_FMR_WQE_2ND_REMOTE_READ,
3462 		   !!(wr->access & IB_ACCESS_REMOTE_READ));
3463 	SET_FIELD2(fwqe2->access_ctrl, RDMA_SQ_FMR_WQE_2ND_REMOTE_WRITE,
3464 		   !!(wr->access & IB_ACCESS_REMOTE_WRITE));
3465 	SET_FIELD2(fwqe2->access_ctrl, RDMA_SQ_FMR_WQE_2ND_ENABLE_ATOMIC,
3466 		   !!(wr->access & IB_ACCESS_REMOTE_ATOMIC));
3467 	SET_FIELD2(fwqe2->access_ctrl, RDMA_SQ_FMR_WQE_2ND_LOCAL_READ, 1);
3468 	SET_FIELD2(fwqe2->access_ctrl, RDMA_SQ_FMR_WQE_2ND_LOCAL_WRITE,
3469 		   !!(wr->access & IB_ACCESS_LOCAL_WRITE));
3470 	fwqe2->fmr_ctrl = 0;
3471 
3472 	SET_FIELD2(fwqe2->fmr_ctrl, RDMA_SQ_FMR_WQE_2ND_PAGE_SIZE_LOG,
3473 		   ilog2(mr->ibmr.page_size) - 12);
3474 
3475 	fwqe2->length_hi = 0;
3476 	fwqe2->length_lo = mr->ibmr.length;
3477 	fwqe2->pbl_addr.hi = upper_32_bits(mr->info.pbl_table->pa);
3478 	fwqe2->pbl_addr.lo = lower_32_bits(mr->info.pbl_table->pa);
3479 
3480 	qp->wqe_wr_id[qp->sq.prod].mr = mr;
3481 
3482 	return 0;
3483 }
3484 
qedr_ib_to_wc_opcode(enum ib_wr_opcode opcode)3485 static enum ib_wc_opcode qedr_ib_to_wc_opcode(enum ib_wr_opcode opcode)
3486 {
3487 	switch (opcode) {
3488 	case IB_WR_RDMA_WRITE:
3489 	case IB_WR_RDMA_WRITE_WITH_IMM:
3490 		return IB_WC_RDMA_WRITE;
3491 	case IB_WR_SEND_WITH_IMM:
3492 	case IB_WR_SEND:
3493 	case IB_WR_SEND_WITH_INV:
3494 		return IB_WC_SEND;
3495 	case IB_WR_RDMA_READ:
3496 	case IB_WR_RDMA_READ_WITH_INV:
3497 		return IB_WC_RDMA_READ;
3498 	case IB_WR_ATOMIC_CMP_AND_SWP:
3499 		return IB_WC_COMP_SWAP;
3500 	case IB_WR_ATOMIC_FETCH_AND_ADD:
3501 		return IB_WC_FETCH_ADD;
3502 	case IB_WR_REG_MR:
3503 		return IB_WC_REG_MR;
3504 	case IB_WR_LOCAL_INV:
3505 		return IB_WC_LOCAL_INV;
3506 	default:
3507 		return IB_WC_SEND;
3508 	}
3509 }
3510 
qedr_can_post_send(struct qedr_qp * qp,const struct ib_send_wr * wr)3511 static inline bool qedr_can_post_send(struct qedr_qp *qp,
3512 				      const struct ib_send_wr *wr)
3513 {
3514 	int wq_is_full, err_wr, pbl_is_full;
3515 	struct qedr_dev *dev = qp->dev;
3516 
3517 	/* prevent SQ overflow and/or processing of a bad WR */
3518 	err_wr = wr->num_sge > qp->sq.max_sges;
3519 	wq_is_full = qedr_wq_is_full(&qp->sq);
3520 	pbl_is_full = qed_chain_get_elem_left_u32(&qp->sq.pbl) <
3521 		      QEDR_MAX_SQE_ELEMENTS_PER_SQE;
3522 	if (wq_is_full || err_wr || pbl_is_full) {
3523 		if (wq_is_full && !(qp->err_bitmap & QEDR_QP_ERR_SQ_FULL)) {
3524 			DP_ERR(dev,
3525 			       "error: WQ is full. Post send on QP %p failed (this error appears only once)\n",
3526 			       qp);
3527 			qp->err_bitmap |= QEDR_QP_ERR_SQ_FULL;
3528 		}
3529 
3530 		if (err_wr && !(qp->err_bitmap & QEDR_QP_ERR_BAD_SR)) {
3531 			DP_ERR(dev,
3532 			       "error: WR is bad. Post send on QP %p failed (this error appears only once)\n",
3533 			       qp);
3534 			qp->err_bitmap |= QEDR_QP_ERR_BAD_SR;
3535 		}
3536 
3537 		if (pbl_is_full &&
3538 		    !(qp->err_bitmap & QEDR_QP_ERR_SQ_PBL_FULL)) {
3539 			DP_ERR(dev,
3540 			       "error: WQ PBL is full. Post send on QP %p failed (this error appears only once)\n",
3541 			       qp);
3542 			qp->err_bitmap |= QEDR_QP_ERR_SQ_PBL_FULL;
3543 		}
3544 		return false;
3545 	}
3546 	return true;
3547 }
3548 
__qedr_post_send(struct ib_qp * ibqp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)3549 static int __qedr_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
3550 			    const struct ib_send_wr **bad_wr)
3551 {
3552 	struct qedr_dev *dev = get_qedr_dev(ibqp->device);
3553 	struct qedr_qp *qp = get_qedr_qp(ibqp);
3554 	struct rdma_sq_atomic_wqe_1st *awqe1;
3555 	struct rdma_sq_atomic_wqe_2nd *awqe2;
3556 	struct rdma_sq_atomic_wqe_3rd *awqe3;
3557 	struct rdma_sq_send_wqe_2st *swqe2;
3558 	struct rdma_sq_local_inv_wqe *iwqe;
3559 	struct rdma_sq_rdma_wqe_2nd *rwqe2;
3560 	struct rdma_sq_send_wqe_1st *swqe;
3561 	struct rdma_sq_rdma_wqe_1st *rwqe;
3562 	struct rdma_sq_fmr_wqe_1st *fwqe1;
3563 	struct rdma_sq_common_wqe *wqe;
3564 	u32 length;
3565 	int rc = 0;
3566 	bool comp;
3567 
3568 	if (!qedr_can_post_send(qp, wr)) {
3569 		*bad_wr = wr;
3570 		return -ENOMEM;
3571 	}
3572 
3573 	wqe = qed_chain_produce(&qp->sq.pbl);
3574 	qp->wqe_wr_id[qp->sq.prod].signaled =
3575 		!!(wr->send_flags & IB_SEND_SIGNALED) || qp->signaled;
3576 
3577 	wqe->flags = 0;
3578 	SET_FIELD2(wqe->flags, RDMA_SQ_SEND_WQE_SE_FLG,
3579 		   !!(wr->send_flags & IB_SEND_SOLICITED));
3580 	comp = (!!(wr->send_flags & IB_SEND_SIGNALED)) || qp->signaled;
3581 	SET_FIELD2(wqe->flags, RDMA_SQ_SEND_WQE_COMP_FLG, comp);
3582 	SET_FIELD2(wqe->flags, RDMA_SQ_SEND_WQE_RD_FENCE_FLG,
3583 		   !!(wr->send_flags & IB_SEND_FENCE));
3584 	wqe->prev_wqe_size = qp->prev_wqe_size;
3585 
3586 	qp->wqe_wr_id[qp->sq.prod].opcode = qedr_ib_to_wc_opcode(wr->opcode);
3587 
3588 	switch (wr->opcode) {
3589 	case IB_WR_SEND_WITH_IMM:
3590 		if (unlikely(rdma_protocol_iwarp(&dev->ibdev, 1))) {
3591 			rc = -EINVAL;
3592 			*bad_wr = wr;
3593 			break;
3594 		}
3595 		wqe->req_type = RDMA_SQ_REQ_TYPE_SEND_WITH_IMM;
3596 		swqe = (struct rdma_sq_send_wqe_1st *)wqe;
3597 		swqe->wqe_size = 2;
3598 		swqe2 = qed_chain_produce(&qp->sq.pbl);
3599 
3600 		swqe->inv_key_or_imm_data = cpu_to_le32(be32_to_cpu(wr->ex.imm_data));
3601 		length = qedr_prepare_sq_send_data(dev, qp, swqe, swqe2,
3602 						   wr, bad_wr);
3603 		swqe->length = cpu_to_le32(length);
3604 		qp->wqe_wr_id[qp->sq.prod].wqe_size = swqe->wqe_size;
3605 		qp->prev_wqe_size = swqe->wqe_size;
3606 		qp->wqe_wr_id[qp->sq.prod].bytes_len = swqe->length;
3607 		break;
3608 	case IB_WR_SEND:
3609 		wqe->req_type = RDMA_SQ_REQ_TYPE_SEND;
3610 		swqe = (struct rdma_sq_send_wqe_1st *)wqe;
3611 
3612 		swqe->wqe_size = 2;
3613 		swqe2 = qed_chain_produce(&qp->sq.pbl);
3614 		length = qedr_prepare_sq_send_data(dev, qp, swqe, swqe2,
3615 						   wr, bad_wr);
3616 		swqe->length = cpu_to_le32(length);
3617 		qp->wqe_wr_id[qp->sq.prod].wqe_size = swqe->wqe_size;
3618 		qp->prev_wqe_size = swqe->wqe_size;
3619 		qp->wqe_wr_id[qp->sq.prod].bytes_len = swqe->length;
3620 		break;
3621 	case IB_WR_SEND_WITH_INV:
3622 		wqe->req_type = RDMA_SQ_REQ_TYPE_SEND_WITH_INVALIDATE;
3623 		swqe = (struct rdma_sq_send_wqe_1st *)wqe;
3624 		swqe2 = qed_chain_produce(&qp->sq.pbl);
3625 		swqe->wqe_size = 2;
3626 		swqe->inv_key_or_imm_data = cpu_to_le32(wr->ex.invalidate_rkey);
3627 		length = qedr_prepare_sq_send_data(dev, qp, swqe, swqe2,
3628 						   wr, bad_wr);
3629 		swqe->length = cpu_to_le32(length);
3630 		qp->wqe_wr_id[qp->sq.prod].wqe_size = swqe->wqe_size;
3631 		qp->prev_wqe_size = swqe->wqe_size;
3632 		qp->wqe_wr_id[qp->sq.prod].bytes_len = swqe->length;
3633 		break;
3634 
3635 	case IB_WR_RDMA_WRITE_WITH_IMM:
3636 		if (unlikely(rdma_protocol_iwarp(&dev->ibdev, 1))) {
3637 			rc = -EINVAL;
3638 			*bad_wr = wr;
3639 			break;
3640 		}
3641 		wqe->req_type = RDMA_SQ_REQ_TYPE_RDMA_WR_WITH_IMM;
3642 		rwqe = (struct rdma_sq_rdma_wqe_1st *)wqe;
3643 
3644 		rwqe->wqe_size = 2;
3645 		rwqe->imm_data = htonl(cpu_to_le32(wr->ex.imm_data));
3646 		rwqe2 = qed_chain_produce(&qp->sq.pbl);
3647 		length = qedr_prepare_sq_rdma_data(dev, qp, rwqe, rwqe2,
3648 						   wr, bad_wr);
3649 		rwqe->length = cpu_to_le32(length);
3650 		qp->wqe_wr_id[qp->sq.prod].wqe_size = rwqe->wqe_size;
3651 		qp->prev_wqe_size = rwqe->wqe_size;
3652 		qp->wqe_wr_id[qp->sq.prod].bytes_len = rwqe->length;
3653 		break;
3654 	case IB_WR_RDMA_WRITE:
3655 		wqe->req_type = RDMA_SQ_REQ_TYPE_RDMA_WR;
3656 		rwqe = (struct rdma_sq_rdma_wqe_1st *)wqe;
3657 
3658 		rwqe->wqe_size = 2;
3659 		rwqe2 = qed_chain_produce(&qp->sq.pbl);
3660 		length = qedr_prepare_sq_rdma_data(dev, qp, rwqe, rwqe2,
3661 						   wr, bad_wr);
3662 		rwqe->length = cpu_to_le32(length);
3663 		qp->wqe_wr_id[qp->sq.prod].wqe_size = rwqe->wqe_size;
3664 		qp->prev_wqe_size = rwqe->wqe_size;
3665 		qp->wqe_wr_id[qp->sq.prod].bytes_len = rwqe->length;
3666 		break;
3667 	case IB_WR_RDMA_READ_WITH_INV:
3668 		SET_FIELD2(wqe->flags, RDMA_SQ_RDMA_WQE_1ST_READ_INV_FLG, 1);
3669 		fallthrough;	/* same is identical to RDMA READ */
3670 
3671 	case IB_WR_RDMA_READ:
3672 		wqe->req_type = RDMA_SQ_REQ_TYPE_RDMA_RD;
3673 		rwqe = (struct rdma_sq_rdma_wqe_1st *)wqe;
3674 
3675 		rwqe->wqe_size = 2;
3676 		rwqe2 = qed_chain_produce(&qp->sq.pbl);
3677 		length = qedr_prepare_sq_rdma_data(dev, qp, rwqe, rwqe2,
3678 						   wr, bad_wr);
3679 		rwqe->length = cpu_to_le32(length);
3680 		qp->wqe_wr_id[qp->sq.prod].wqe_size = rwqe->wqe_size;
3681 		qp->prev_wqe_size = rwqe->wqe_size;
3682 		qp->wqe_wr_id[qp->sq.prod].bytes_len = rwqe->length;
3683 		break;
3684 
3685 	case IB_WR_ATOMIC_CMP_AND_SWP:
3686 	case IB_WR_ATOMIC_FETCH_AND_ADD:
3687 		awqe1 = (struct rdma_sq_atomic_wqe_1st *)wqe;
3688 		awqe1->wqe_size = 4;
3689 
3690 		awqe2 = qed_chain_produce(&qp->sq.pbl);
3691 		DMA_REGPAIR_LE(awqe2->remote_va, atomic_wr(wr)->remote_addr);
3692 		awqe2->r_key = cpu_to_le32(atomic_wr(wr)->rkey);
3693 
3694 		awqe3 = qed_chain_produce(&qp->sq.pbl);
3695 
3696 		if (wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
3697 			wqe->req_type = RDMA_SQ_REQ_TYPE_ATOMIC_ADD;
3698 			DMA_REGPAIR_LE(awqe3->swap_data,
3699 				       atomic_wr(wr)->compare_add);
3700 		} else {
3701 			wqe->req_type = RDMA_SQ_REQ_TYPE_ATOMIC_CMP_AND_SWAP;
3702 			DMA_REGPAIR_LE(awqe3->swap_data,
3703 				       atomic_wr(wr)->swap);
3704 			DMA_REGPAIR_LE(awqe3->cmp_data,
3705 				       atomic_wr(wr)->compare_add);
3706 		}
3707 
3708 		qedr_prepare_sq_sges(qp, NULL, wr);
3709 
3710 		qp->wqe_wr_id[qp->sq.prod].wqe_size = awqe1->wqe_size;
3711 		qp->prev_wqe_size = awqe1->wqe_size;
3712 		break;
3713 
3714 	case IB_WR_LOCAL_INV:
3715 		iwqe = (struct rdma_sq_local_inv_wqe *)wqe;
3716 		iwqe->wqe_size = 1;
3717 
3718 		iwqe->req_type = RDMA_SQ_REQ_TYPE_LOCAL_INVALIDATE;
3719 		iwqe->inv_l_key = wr->ex.invalidate_rkey;
3720 		qp->wqe_wr_id[qp->sq.prod].wqe_size = iwqe->wqe_size;
3721 		qp->prev_wqe_size = iwqe->wqe_size;
3722 		break;
3723 	case IB_WR_REG_MR:
3724 		DP_DEBUG(dev, QEDR_MSG_CQ, "REG_MR\n");
3725 		wqe->req_type = RDMA_SQ_REQ_TYPE_FAST_MR;
3726 		fwqe1 = (struct rdma_sq_fmr_wqe_1st *)wqe;
3727 		fwqe1->wqe_size = 2;
3728 
3729 		rc = qedr_prepare_reg(qp, fwqe1, reg_wr(wr));
3730 		if (rc) {
3731 			DP_ERR(dev, "IB_REG_MR failed rc=%d\n", rc);
3732 			*bad_wr = wr;
3733 			break;
3734 		}
3735 
3736 		qp->wqe_wr_id[qp->sq.prod].wqe_size = fwqe1->wqe_size;
3737 		qp->prev_wqe_size = fwqe1->wqe_size;
3738 		break;
3739 	default:
3740 		DP_ERR(dev, "invalid opcode 0x%x!\n", wr->opcode);
3741 		rc = -EINVAL;
3742 		*bad_wr = wr;
3743 		break;
3744 	}
3745 
3746 	if (*bad_wr) {
3747 		u16 value;
3748 
3749 		/* Restore prod to its position before
3750 		 * this WR was processed
3751 		 */
3752 		value = le16_to_cpu(qp->sq.db_data.data.value);
3753 		qed_chain_set_prod(&qp->sq.pbl, value, wqe);
3754 
3755 		/* Restore prev_wqe_size */
3756 		qp->prev_wqe_size = wqe->prev_wqe_size;
3757 		rc = -EINVAL;
3758 		DP_ERR(dev, "POST SEND FAILED\n");
3759 	}
3760 
3761 	return rc;
3762 }
3763 
qedr_post_send(struct ib_qp * ibqp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)3764 int qedr_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
3765 		   const struct ib_send_wr **bad_wr)
3766 {
3767 	struct qedr_dev *dev = get_qedr_dev(ibqp->device);
3768 	struct qedr_qp *qp = get_qedr_qp(ibqp);
3769 	unsigned long flags;
3770 	int rc = 0;
3771 
3772 	*bad_wr = NULL;
3773 
3774 	if (qp->qp_type == IB_QPT_GSI)
3775 		return qedr_gsi_post_send(ibqp, wr, bad_wr);
3776 
3777 	spin_lock_irqsave(&qp->q_lock, flags);
3778 
3779 	if (rdma_protocol_roce(&dev->ibdev, 1)) {
3780 		if ((qp->state != QED_ROCE_QP_STATE_RTS) &&
3781 		    (qp->state != QED_ROCE_QP_STATE_ERR) &&
3782 		    (qp->state != QED_ROCE_QP_STATE_SQD)) {
3783 			spin_unlock_irqrestore(&qp->q_lock, flags);
3784 			*bad_wr = wr;
3785 			DP_DEBUG(dev, QEDR_MSG_CQ,
3786 				 "QP in wrong state! QP icid=0x%x state %d\n",
3787 				 qp->icid, qp->state);
3788 			return -EINVAL;
3789 		}
3790 	}
3791 
3792 	while (wr) {
3793 		rc = __qedr_post_send(ibqp, wr, bad_wr);
3794 		if (rc)
3795 			break;
3796 
3797 		qp->wqe_wr_id[qp->sq.prod].wr_id = wr->wr_id;
3798 
3799 		qedr_inc_sw_prod(&qp->sq);
3800 
3801 		qp->sq.db_data.data.value++;
3802 
3803 		wr = wr->next;
3804 	}
3805 
3806 	/* Trigger doorbell
3807 	 * If there was a failure in the first WR then it will be triggered in
3808 	 * vane. However this is not harmful (as long as the producer value is
3809 	 * unchanged). For performance reasons we avoid checking for this
3810 	 * redundant doorbell.
3811 	 *
3812 	 * qp->wqe_wr_id is accessed during qedr_poll_cq, as
3813 	 * soon as we give the doorbell, we could get a completion
3814 	 * for this wr, therefore we need to make sure that the
3815 	 * memory is updated before giving the doorbell.
3816 	 * During qedr_poll_cq, rmb is called before accessing the
3817 	 * cqe. This covers for the smp_rmb as well.
3818 	 */
3819 	smp_wmb();
3820 	writel(qp->sq.db_data.raw, qp->sq.db);
3821 
3822 	spin_unlock_irqrestore(&qp->q_lock, flags);
3823 
3824 	return rc;
3825 }
3826 
qedr_srq_elem_left(struct qedr_srq_hwq_info * hw_srq)3827 static u32 qedr_srq_elem_left(struct qedr_srq_hwq_info *hw_srq)
3828 {
3829 	u32 used;
3830 
3831 	/* Calculate number of elements used based on producer
3832 	 * count and consumer count and subtract it from max
3833 	 * work request supported so that we get elements left.
3834 	 */
3835 	used = hw_srq->wr_prod_cnt - (u32)atomic_read(&hw_srq->wr_cons_cnt);
3836 
3837 	return hw_srq->max_wr - used;
3838 }
3839 
qedr_post_srq_recv(struct ib_srq * ibsrq,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)3840 int qedr_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
3841 		       const struct ib_recv_wr **bad_wr)
3842 {
3843 	struct qedr_srq *srq = get_qedr_srq(ibsrq);
3844 	struct qedr_srq_hwq_info *hw_srq;
3845 	struct qedr_dev *dev = srq->dev;
3846 	struct qed_chain *pbl;
3847 	unsigned long flags;
3848 	int status = 0;
3849 	u32 num_sge;
3850 
3851 	spin_lock_irqsave(&srq->lock, flags);
3852 
3853 	hw_srq = &srq->hw_srq;
3854 	pbl = &srq->hw_srq.pbl;
3855 	while (wr) {
3856 		struct rdma_srq_wqe_header *hdr;
3857 		int i;
3858 
3859 		if (!qedr_srq_elem_left(hw_srq) ||
3860 		    wr->num_sge > srq->hw_srq.max_sges) {
3861 			DP_ERR(dev, "Can't post WR  (%d,%d) || (%d > %d)\n",
3862 			       hw_srq->wr_prod_cnt,
3863 			       atomic_read(&hw_srq->wr_cons_cnt),
3864 			       wr->num_sge, srq->hw_srq.max_sges);
3865 			status = -ENOMEM;
3866 			*bad_wr = wr;
3867 			break;
3868 		}
3869 
3870 		hdr = qed_chain_produce(pbl);
3871 		num_sge = wr->num_sge;
3872 		/* Set number of sge and work request id in header */
3873 		SRQ_HDR_SET(hdr, wr->wr_id, num_sge);
3874 
3875 		srq->hw_srq.wr_prod_cnt++;
3876 		hw_srq->wqe_prod++;
3877 		hw_srq->sge_prod++;
3878 
3879 		DP_DEBUG(dev, QEDR_MSG_SRQ,
3880 			 "SRQ WR: SGEs: %d with wr_id[%d] = %llx\n",
3881 			 wr->num_sge, hw_srq->wqe_prod, wr->wr_id);
3882 
3883 		for (i = 0; i < wr->num_sge; i++) {
3884 			struct rdma_srq_sge *srq_sge = qed_chain_produce(pbl);
3885 
3886 			/* Set SGE length, lkey and address */
3887 			SRQ_SGE_SET(srq_sge, wr->sg_list[i].addr,
3888 				    wr->sg_list[i].length, wr->sg_list[i].lkey);
3889 
3890 			DP_DEBUG(dev, QEDR_MSG_SRQ,
3891 				 "[%d]: len %d key %x addr %x:%x\n",
3892 				 i, srq_sge->length, srq_sge->l_key,
3893 				 srq_sge->addr.hi, srq_sge->addr.lo);
3894 			hw_srq->sge_prod++;
3895 		}
3896 
3897 		/* Update WQE and SGE information before
3898 		 * updating producer.
3899 		 */
3900 		dma_wmb();
3901 
3902 		/* SRQ producer is 8 bytes. Need to update SGE producer index
3903 		 * in first 4 bytes and need to update WQE producer in
3904 		 * next 4 bytes.
3905 		 */
3906 		srq->hw_srq.virt_prod_pair_addr->sge_prod = cpu_to_le32(hw_srq->sge_prod);
3907 		/* Make sure sge producer is updated first */
3908 		dma_wmb();
3909 		srq->hw_srq.virt_prod_pair_addr->wqe_prod = cpu_to_le32(hw_srq->wqe_prod);
3910 
3911 		wr = wr->next;
3912 	}
3913 
3914 	DP_DEBUG(dev, QEDR_MSG_SRQ, "POST: Elements in S-RQ: %d\n",
3915 		 qed_chain_get_elem_left(pbl));
3916 	spin_unlock_irqrestore(&srq->lock, flags);
3917 
3918 	return status;
3919 }
3920 
qedr_post_recv(struct ib_qp * ibqp,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)3921 int qedr_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
3922 		   const struct ib_recv_wr **bad_wr)
3923 {
3924 	struct qedr_qp *qp = get_qedr_qp(ibqp);
3925 	struct qedr_dev *dev = qp->dev;
3926 	unsigned long flags;
3927 	int status = 0;
3928 
3929 	if (qp->qp_type == IB_QPT_GSI)
3930 		return qedr_gsi_post_recv(ibqp, wr, bad_wr);
3931 
3932 	spin_lock_irqsave(&qp->q_lock, flags);
3933 
3934 	if (qp->state == QED_ROCE_QP_STATE_RESET) {
3935 		spin_unlock_irqrestore(&qp->q_lock, flags);
3936 		*bad_wr = wr;
3937 		return -EINVAL;
3938 	}
3939 
3940 	while (wr) {
3941 		int i;
3942 
3943 		if (qed_chain_get_elem_left_u32(&qp->rq.pbl) <
3944 		    QEDR_MAX_RQE_ELEMENTS_PER_RQE ||
3945 		    wr->num_sge > qp->rq.max_sges) {
3946 			DP_ERR(dev, "Can't post WR  (%d < %d) || (%d > %d)\n",
3947 			       qed_chain_get_elem_left_u32(&qp->rq.pbl),
3948 			       QEDR_MAX_RQE_ELEMENTS_PER_RQE, wr->num_sge,
3949 			       qp->rq.max_sges);
3950 			status = -ENOMEM;
3951 			*bad_wr = wr;
3952 			break;
3953 		}
3954 		for (i = 0; i < wr->num_sge; i++) {
3955 			u32 flags = 0;
3956 			struct rdma_rq_sge *rqe =
3957 			    qed_chain_produce(&qp->rq.pbl);
3958 
3959 			/* First one must include the number
3960 			 * of SGE in the list
3961 			 */
3962 			if (!i)
3963 				SET_FIELD(flags, RDMA_RQ_SGE_NUM_SGES,
3964 					  wr->num_sge);
3965 
3966 			SET_FIELD(flags, RDMA_RQ_SGE_L_KEY_LO,
3967 				  wr->sg_list[i].lkey);
3968 
3969 			RQ_SGE_SET(rqe, wr->sg_list[i].addr,
3970 				   wr->sg_list[i].length, flags);
3971 		}
3972 
3973 		/* Special case of no sges. FW requires between 1-4 sges...
3974 		 * in this case we need to post 1 sge with length zero. this is
3975 		 * because rdma write with immediate consumes an RQ.
3976 		 */
3977 		if (!wr->num_sge) {
3978 			u32 flags = 0;
3979 			struct rdma_rq_sge *rqe =
3980 			    qed_chain_produce(&qp->rq.pbl);
3981 
3982 			/* First one must include the number
3983 			 * of SGE in the list
3984 			 */
3985 			SET_FIELD(flags, RDMA_RQ_SGE_L_KEY_LO, 0);
3986 			SET_FIELD(flags, RDMA_RQ_SGE_NUM_SGES, 1);
3987 
3988 			RQ_SGE_SET(rqe, 0, 0, flags);
3989 			i = 1;
3990 		}
3991 
3992 		qp->rqe_wr_id[qp->rq.prod].wr_id = wr->wr_id;
3993 		qp->rqe_wr_id[qp->rq.prod].wqe_size = i;
3994 
3995 		qedr_inc_sw_prod(&qp->rq);
3996 
3997 		/* qp->rqe_wr_id is accessed during qedr_poll_cq, as
3998 		 * soon as we give the doorbell, we could get a completion
3999 		 * for this wr, therefore we need to make sure that the
4000 		 * memory is update before giving the doorbell.
4001 		 * During qedr_poll_cq, rmb is called before accessing the
4002 		 * cqe. This covers for the smp_rmb as well.
4003 		 */
4004 		smp_wmb();
4005 
4006 		qp->rq.db_data.data.value++;
4007 
4008 		writel(qp->rq.db_data.raw, qp->rq.db);
4009 
4010 		if (rdma_protocol_iwarp(&dev->ibdev, 1)) {
4011 			writel(qp->rq.iwarp_db2_data.raw, qp->rq.iwarp_db2);
4012 		}
4013 
4014 		wr = wr->next;
4015 	}
4016 
4017 	spin_unlock_irqrestore(&qp->q_lock, flags);
4018 
4019 	return status;
4020 }
4021 
is_valid_cqe(struct qedr_cq * cq,union rdma_cqe * cqe)4022 static int is_valid_cqe(struct qedr_cq *cq, union rdma_cqe *cqe)
4023 {
4024 	struct rdma_cqe_requester *resp_cqe = &cqe->req;
4025 
4026 	return (resp_cqe->flags & RDMA_CQE_REQUESTER_TOGGLE_BIT_MASK) ==
4027 		cq->pbl_toggle;
4028 }
4029 
cqe_get_qp(union rdma_cqe * cqe)4030 static struct qedr_qp *cqe_get_qp(union rdma_cqe *cqe)
4031 {
4032 	struct rdma_cqe_requester *resp_cqe = &cqe->req;
4033 	struct qedr_qp *qp;
4034 
4035 	qp = (struct qedr_qp *)(uintptr_t)HILO_GEN(resp_cqe->qp_handle.hi,
4036 						   resp_cqe->qp_handle.lo,
4037 						   u64);
4038 	return qp;
4039 }
4040 
cqe_get_type(union rdma_cqe * cqe)4041 static enum rdma_cqe_type cqe_get_type(union rdma_cqe *cqe)
4042 {
4043 	struct rdma_cqe_requester *resp_cqe = &cqe->req;
4044 
4045 	return GET_FIELD(resp_cqe->flags, RDMA_CQE_REQUESTER_TYPE);
4046 }
4047 
4048 /* Return latest CQE (needs processing) */
get_cqe(struct qedr_cq * cq)4049 static union rdma_cqe *get_cqe(struct qedr_cq *cq)
4050 {
4051 	return cq->latest_cqe;
4052 }
4053 
4054 /* In fmr we need to increase the number of fmr completed counter for the fmr
4055  * algorithm determining whether we can free a pbl or not.
4056  * we need to perform this whether the work request was signaled or not. for
4057  * this purpose we call this function from the condition that checks if a wr
4058  * should be skipped, to make sure we don't miss it ( possibly this fmr
4059  * operation was not signalted)
4060  */
qedr_chk_if_fmr(struct qedr_qp * qp)4061 static inline void qedr_chk_if_fmr(struct qedr_qp *qp)
4062 {
4063 	if (qp->wqe_wr_id[qp->sq.cons].opcode == IB_WC_REG_MR)
4064 		qp->wqe_wr_id[qp->sq.cons].mr->info.completed++;
4065 }
4066 
process_req(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,int num_entries,struct ib_wc * wc,u16 hw_cons,enum ib_wc_status status,int force)4067 static int process_req(struct qedr_dev *dev, struct qedr_qp *qp,
4068 		       struct qedr_cq *cq, int num_entries,
4069 		       struct ib_wc *wc, u16 hw_cons, enum ib_wc_status status,
4070 		       int force)
4071 {
4072 	u16 cnt = 0;
4073 
4074 	while (num_entries && qp->sq.wqe_cons != hw_cons) {
4075 		if (!qp->wqe_wr_id[qp->sq.cons].signaled && !force) {
4076 			qedr_chk_if_fmr(qp);
4077 			/* skip WC */
4078 			goto next_cqe;
4079 		}
4080 
4081 		/* fill WC */
4082 		wc->status = status;
4083 		wc->vendor_err = 0;
4084 		wc->wc_flags = 0;
4085 		wc->src_qp = qp->id;
4086 		wc->qp = &qp->ibqp;
4087 
4088 		wc->wr_id = qp->wqe_wr_id[qp->sq.cons].wr_id;
4089 		wc->opcode = qp->wqe_wr_id[qp->sq.cons].opcode;
4090 
4091 		switch (wc->opcode) {
4092 		case IB_WC_RDMA_WRITE:
4093 			wc->byte_len = qp->wqe_wr_id[qp->sq.cons].bytes_len;
4094 			break;
4095 		case IB_WC_COMP_SWAP:
4096 		case IB_WC_FETCH_ADD:
4097 			wc->byte_len = 8;
4098 			break;
4099 		case IB_WC_REG_MR:
4100 			qp->wqe_wr_id[qp->sq.cons].mr->info.completed++;
4101 			break;
4102 		case IB_WC_RDMA_READ:
4103 		case IB_WC_SEND:
4104 			wc->byte_len = qp->wqe_wr_id[qp->sq.cons].bytes_len;
4105 			break;
4106 		default:
4107 			break;
4108 		}
4109 
4110 		num_entries--;
4111 		wc++;
4112 		cnt++;
4113 next_cqe:
4114 		while (qp->wqe_wr_id[qp->sq.cons].wqe_size--)
4115 			qed_chain_consume(&qp->sq.pbl);
4116 		qedr_inc_sw_cons(&qp->sq);
4117 	}
4118 
4119 	return cnt;
4120 }
4121 
qedr_poll_cq_req(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,int num_entries,struct ib_wc * wc,struct rdma_cqe_requester * req)4122 static int qedr_poll_cq_req(struct qedr_dev *dev,
4123 			    struct qedr_qp *qp, struct qedr_cq *cq,
4124 			    int num_entries, struct ib_wc *wc,
4125 			    struct rdma_cqe_requester *req)
4126 {
4127 	int cnt = 0;
4128 
4129 	switch (req->status) {
4130 	case RDMA_CQE_REQ_STS_OK:
4131 		cnt = process_req(dev, qp, cq, num_entries, wc, req->sq_cons,
4132 				  IB_WC_SUCCESS, 0);
4133 		break;
4134 	case RDMA_CQE_REQ_STS_WORK_REQUEST_FLUSHED_ERR:
4135 		if (qp->state != QED_ROCE_QP_STATE_ERR)
4136 			DP_DEBUG(dev, QEDR_MSG_CQ,
4137 				 "Error: POLL CQ with RDMA_CQE_REQ_STS_WORK_REQUEST_FLUSHED_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4138 				 cq->icid, qp->icid);
4139 		cnt = process_req(dev, qp, cq, num_entries, wc, req->sq_cons,
4140 				  IB_WC_WR_FLUSH_ERR, 1);
4141 		break;
4142 	default:
4143 		/* process all WQE before the cosumer */
4144 		qp->state = QED_ROCE_QP_STATE_ERR;
4145 		cnt = process_req(dev, qp, cq, num_entries, wc,
4146 				  req->sq_cons - 1, IB_WC_SUCCESS, 0);
4147 		wc += cnt;
4148 		/* if we have extra WC fill it with actual error info */
4149 		if (cnt < num_entries) {
4150 			enum ib_wc_status wc_status;
4151 
4152 			switch (req->status) {
4153 			case RDMA_CQE_REQ_STS_BAD_RESPONSE_ERR:
4154 				DP_ERR(dev,
4155 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_BAD_RESPONSE_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4156 				       cq->icid, qp->icid);
4157 				wc_status = IB_WC_BAD_RESP_ERR;
4158 				break;
4159 			case RDMA_CQE_REQ_STS_LOCAL_LENGTH_ERR:
4160 				DP_ERR(dev,
4161 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_LOCAL_LENGTH_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4162 				       cq->icid, qp->icid);
4163 				wc_status = IB_WC_LOC_LEN_ERR;
4164 				break;
4165 			case RDMA_CQE_REQ_STS_LOCAL_QP_OPERATION_ERR:
4166 				DP_ERR(dev,
4167 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_LOCAL_QP_OPERATION_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4168 				       cq->icid, qp->icid);
4169 				wc_status = IB_WC_LOC_QP_OP_ERR;
4170 				break;
4171 			case RDMA_CQE_REQ_STS_LOCAL_PROTECTION_ERR:
4172 				DP_ERR(dev,
4173 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_LOCAL_PROTECTION_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4174 				       cq->icid, qp->icid);
4175 				wc_status = IB_WC_LOC_PROT_ERR;
4176 				break;
4177 			case RDMA_CQE_REQ_STS_MEMORY_MGT_OPERATION_ERR:
4178 				DP_ERR(dev,
4179 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_MEMORY_MGT_OPERATION_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4180 				       cq->icid, qp->icid);
4181 				wc_status = IB_WC_MW_BIND_ERR;
4182 				break;
4183 			case RDMA_CQE_REQ_STS_REMOTE_INVALID_REQUEST_ERR:
4184 				DP_ERR(dev,
4185 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_REMOTE_INVALID_REQUEST_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4186 				       cq->icid, qp->icid);
4187 				wc_status = IB_WC_REM_INV_REQ_ERR;
4188 				break;
4189 			case RDMA_CQE_REQ_STS_REMOTE_ACCESS_ERR:
4190 				DP_ERR(dev,
4191 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_REMOTE_ACCESS_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4192 				       cq->icid, qp->icid);
4193 				wc_status = IB_WC_REM_ACCESS_ERR;
4194 				break;
4195 			case RDMA_CQE_REQ_STS_REMOTE_OPERATION_ERR:
4196 				DP_ERR(dev,
4197 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_REMOTE_OPERATION_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4198 				       cq->icid, qp->icid);
4199 				wc_status = IB_WC_REM_OP_ERR;
4200 				break;
4201 			case RDMA_CQE_REQ_STS_RNR_NAK_RETRY_CNT_ERR:
4202 				DP_ERR(dev,
4203 				       "Error: POLL CQ with RDMA_CQE_REQ_STS_RNR_NAK_RETRY_CNT_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4204 				       cq->icid, qp->icid);
4205 				wc_status = IB_WC_RNR_RETRY_EXC_ERR;
4206 				break;
4207 			case RDMA_CQE_REQ_STS_TRANSPORT_RETRY_CNT_ERR:
4208 				DP_ERR(dev,
4209 				       "Error: POLL CQ with ROCE_CQE_REQ_STS_TRANSPORT_RETRY_CNT_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4210 				       cq->icid, qp->icid);
4211 				wc_status = IB_WC_RETRY_EXC_ERR;
4212 				break;
4213 			default:
4214 				DP_ERR(dev,
4215 				       "Error: POLL CQ with IB_WC_GENERAL_ERR. CQ icid=0x%x, QP icid=0x%x\n",
4216 				       cq->icid, qp->icid);
4217 				wc_status = IB_WC_GENERAL_ERR;
4218 			}
4219 			cnt += process_req(dev, qp, cq, 1, wc, req->sq_cons,
4220 					   wc_status, 1);
4221 		}
4222 	}
4223 
4224 	return cnt;
4225 }
4226 
qedr_cqe_resp_status_to_ib(u8 status)4227 static inline int qedr_cqe_resp_status_to_ib(u8 status)
4228 {
4229 	switch (status) {
4230 	case RDMA_CQE_RESP_STS_LOCAL_ACCESS_ERR:
4231 		return IB_WC_LOC_ACCESS_ERR;
4232 	case RDMA_CQE_RESP_STS_LOCAL_LENGTH_ERR:
4233 		return IB_WC_LOC_LEN_ERR;
4234 	case RDMA_CQE_RESP_STS_LOCAL_QP_OPERATION_ERR:
4235 		return IB_WC_LOC_QP_OP_ERR;
4236 	case RDMA_CQE_RESP_STS_LOCAL_PROTECTION_ERR:
4237 		return IB_WC_LOC_PROT_ERR;
4238 	case RDMA_CQE_RESP_STS_MEMORY_MGT_OPERATION_ERR:
4239 		return IB_WC_MW_BIND_ERR;
4240 	case RDMA_CQE_RESP_STS_REMOTE_INVALID_REQUEST_ERR:
4241 		return IB_WC_REM_INV_RD_REQ_ERR;
4242 	case RDMA_CQE_RESP_STS_OK:
4243 		return IB_WC_SUCCESS;
4244 	default:
4245 		return IB_WC_GENERAL_ERR;
4246 	}
4247 }
4248 
qedr_set_ok_cqe_resp_wc(struct rdma_cqe_responder * resp,struct ib_wc * wc)4249 static inline int qedr_set_ok_cqe_resp_wc(struct rdma_cqe_responder *resp,
4250 					  struct ib_wc *wc)
4251 {
4252 	wc->status = IB_WC_SUCCESS;
4253 	wc->byte_len = le32_to_cpu(resp->length);
4254 
4255 	if (resp->flags & QEDR_RESP_IMM) {
4256 		wc->ex.imm_data = cpu_to_be32(le32_to_cpu(resp->imm_data_or_inv_r_Key));
4257 		wc->wc_flags |= IB_WC_WITH_IMM;
4258 
4259 		if (resp->flags & QEDR_RESP_RDMA)
4260 			wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
4261 
4262 		if (resp->flags & QEDR_RESP_INV)
4263 			return -EINVAL;
4264 
4265 	} else if (resp->flags & QEDR_RESP_INV) {
4266 		wc->ex.imm_data = le32_to_cpu(resp->imm_data_or_inv_r_Key);
4267 		wc->wc_flags |= IB_WC_WITH_INVALIDATE;
4268 
4269 		if (resp->flags & QEDR_RESP_RDMA)
4270 			return -EINVAL;
4271 
4272 	} else if (resp->flags & QEDR_RESP_RDMA) {
4273 		return -EINVAL;
4274 	}
4275 
4276 	return 0;
4277 }
4278 
__process_resp_one(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,struct ib_wc * wc,struct rdma_cqe_responder * resp,u64 wr_id)4279 static void __process_resp_one(struct qedr_dev *dev, struct qedr_qp *qp,
4280 			       struct qedr_cq *cq, struct ib_wc *wc,
4281 			       struct rdma_cqe_responder *resp, u64 wr_id)
4282 {
4283 	/* Must fill fields before qedr_set_ok_cqe_resp_wc() */
4284 	wc->opcode = IB_WC_RECV;
4285 	wc->wc_flags = 0;
4286 
4287 	if (likely(resp->status == RDMA_CQE_RESP_STS_OK)) {
4288 		if (qedr_set_ok_cqe_resp_wc(resp, wc))
4289 			DP_ERR(dev,
4290 			       "CQ %p (icid=%d) has invalid CQE responder flags=0x%x\n",
4291 			       cq, cq->icid, resp->flags);
4292 
4293 	} else {
4294 		wc->status = qedr_cqe_resp_status_to_ib(resp->status);
4295 		if (wc->status == IB_WC_GENERAL_ERR)
4296 			DP_ERR(dev,
4297 			       "CQ %p (icid=%d) contains an invalid CQE status %d\n",
4298 			       cq, cq->icid, resp->status);
4299 	}
4300 
4301 	/* Fill the rest of the WC */
4302 	wc->vendor_err = 0;
4303 	wc->src_qp = qp->id;
4304 	wc->qp = &qp->ibqp;
4305 	wc->wr_id = wr_id;
4306 }
4307 
process_resp_one_srq(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,struct ib_wc * wc,struct rdma_cqe_responder * resp)4308 static int process_resp_one_srq(struct qedr_dev *dev, struct qedr_qp *qp,
4309 				struct qedr_cq *cq, struct ib_wc *wc,
4310 				struct rdma_cqe_responder *resp)
4311 {
4312 	struct qedr_srq *srq = qp->srq;
4313 	u64 wr_id;
4314 
4315 	wr_id = HILO_GEN(le32_to_cpu(resp->srq_wr_id.hi),
4316 			 le32_to_cpu(resp->srq_wr_id.lo), u64);
4317 
4318 	if (resp->status == RDMA_CQE_RESP_STS_WORK_REQUEST_FLUSHED_ERR) {
4319 		wc->status = IB_WC_WR_FLUSH_ERR;
4320 		wc->vendor_err = 0;
4321 		wc->wr_id = wr_id;
4322 		wc->byte_len = 0;
4323 		wc->src_qp = qp->id;
4324 		wc->qp = &qp->ibqp;
4325 		wc->wr_id = wr_id;
4326 	} else {
4327 		__process_resp_one(dev, qp, cq, wc, resp, wr_id);
4328 	}
4329 	atomic_inc(&srq->hw_srq.wr_cons_cnt);
4330 
4331 	return 1;
4332 }
process_resp_one(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,struct ib_wc * wc,struct rdma_cqe_responder * resp)4333 static int process_resp_one(struct qedr_dev *dev, struct qedr_qp *qp,
4334 			    struct qedr_cq *cq, struct ib_wc *wc,
4335 			    struct rdma_cqe_responder *resp)
4336 {
4337 	u64 wr_id = qp->rqe_wr_id[qp->rq.cons].wr_id;
4338 
4339 	__process_resp_one(dev, qp, cq, wc, resp, wr_id);
4340 
4341 	while (qp->rqe_wr_id[qp->rq.cons].wqe_size--)
4342 		qed_chain_consume(&qp->rq.pbl);
4343 	qedr_inc_sw_cons(&qp->rq);
4344 
4345 	return 1;
4346 }
4347 
process_resp_flush(struct qedr_qp * qp,struct qedr_cq * cq,int num_entries,struct ib_wc * wc,u16 hw_cons)4348 static int process_resp_flush(struct qedr_qp *qp, struct qedr_cq *cq,
4349 			      int num_entries, struct ib_wc *wc, u16 hw_cons)
4350 {
4351 	u16 cnt = 0;
4352 
4353 	while (num_entries && qp->rq.wqe_cons != hw_cons) {
4354 		/* fill WC */
4355 		wc->status = IB_WC_WR_FLUSH_ERR;
4356 		wc->vendor_err = 0;
4357 		wc->wc_flags = 0;
4358 		wc->src_qp = qp->id;
4359 		wc->byte_len = 0;
4360 		wc->wr_id = qp->rqe_wr_id[qp->rq.cons].wr_id;
4361 		wc->qp = &qp->ibqp;
4362 		num_entries--;
4363 		wc++;
4364 		cnt++;
4365 		while (qp->rqe_wr_id[qp->rq.cons].wqe_size--)
4366 			qed_chain_consume(&qp->rq.pbl);
4367 		qedr_inc_sw_cons(&qp->rq);
4368 	}
4369 
4370 	return cnt;
4371 }
4372 
try_consume_resp_cqe(struct qedr_cq * cq,struct qedr_qp * qp,struct rdma_cqe_responder * resp,int * update)4373 static void try_consume_resp_cqe(struct qedr_cq *cq, struct qedr_qp *qp,
4374 				 struct rdma_cqe_responder *resp, int *update)
4375 {
4376 	if (le16_to_cpu(resp->rq_cons_or_srq_id) == qp->rq.wqe_cons) {
4377 		consume_cqe(cq);
4378 		*update |= 1;
4379 	}
4380 }
4381 
qedr_poll_cq_resp_srq(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,int num_entries,struct ib_wc * wc,struct rdma_cqe_responder * resp)4382 static int qedr_poll_cq_resp_srq(struct qedr_dev *dev, struct qedr_qp *qp,
4383 				 struct qedr_cq *cq, int num_entries,
4384 				 struct ib_wc *wc,
4385 				 struct rdma_cqe_responder *resp)
4386 {
4387 	int cnt;
4388 
4389 	cnt = process_resp_one_srq(dev, qp, cq, wc, resp);
4390 	consume_cqe(cq);
4391 
4392 	return cnt;
4393 }
4394 
qedr_poll_cq_resp(struct qedr_dev * dev,struct qedr_qp * qp,struct qedr_cq * cq,int num_entries,struct ib_wc * wc,struct rdma_cqe_responder * resp,int * update)4395 static int qedr_poll_cq_resp(struct qedr_dev *dev, struct qedr_qp *qp,
4396 			     struct qedr_cq *cq, int num_entries,
4397 			     struct ib_wc *wc, struct rdma_cqe_responder *resp,
4398 			     int *update)
4399 {
4400 	int cnt;
4401 
4402 	if (resp->status == RDMA_CQE_RESP_STS_WORK_REQUEST_FLUSHED_ERR) {
4403 		cnt = process_resp_flush(qp, cq, num_entries, wc,
4404 					 resp->rq_cons_or_srq_id);
4405 		try_consume_resp_cqe(cq, qp, resp, update);
4406 	} else {
4407 		cnt = process_resp_one(dev, qp, cq, wc, resp);
4408 		consume_cqe(cq);
4409 		*update |= 1;
4410 	}
4411 
4412 	return cnt;
4413 }
4414 
try_consume_req_cqe(struct qedr_cq * cq,struct qedr_qp * qp,struct rdma_cqe_requester * req,int * update)4415 static void try_consume_req_cqe(struct qedr_cq *cq, struct qedr_qp *qp,
4416 				struct rdma_cqe_requester *req, int *update)
4417 {
4418 	if (le16_to_cpu(req->sq_cons) == qp->sq.wqe_cons) {
4419 		consume_cqe(cq);
4420 		*update |= 1;
4421 	}
4422 }
4423 
qedr_poll_cq(struct ib_cq * ibcq,int num_entries,struct ib_wc * wc)4424 int qedr_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
4425 {
4426 	struct qedr_dev *dev = get_qedr_dev(ibcq->device);
4427 	struct qedr_cq *cq = get_qedr_cq(ibcq);
4428 	union rdma_cqe *cqe;
4429 	u32 old_cons, new_cons;
4430 	unsigned long flags;
4431 	int update = 0;
4432 	int done = 0;
4433 
4434 	if (cq->destroyed) {
4435 		DP_ERR(dev,
4436 		       "warning: poll was invoked after destroy for cq %p (icid=%d)\n",
4437 		       cq, cq->icid);
4438 		return 0;
4439 	}
4440 
4441 	if (cq->cq_type == QEDR_CQ_TYPE_GSI)
4442 		return qedr_gsi_poll_cq(ibcq, num_entries, wc);
4443 
4444 	spin_lock_irqsave(&cq->cq_lock, flags);
4445 	cqe = cq->latest_cqe;
4446 	old_cons = qed_chain_get_cons_idx_u32(&cq->pbl);
4447 	while (num_entries && is_valid_cqe(cq, cqe)) {
4448 		struct qedr_qp *qp;
4449 		int cnt = 0;
4450 
4451 		/* prevent speculative reads of any field of CQE */
4452 		rmb();
4453 
4454 		qp = cqe_get_qp(cqe);
4455 		if (!qp) {
4456 			WARN(1, "Error: CQE QP pointer is NULL. CQE=%p\n", cqe);
4457 			break;
4458 		}
4459 
4460 		wc->qp = &qp->ibqp;
4461 
4462 		switch (cqe_get_type(cqe)) {
4463 		case RDMA_CQE_TYPE_REQUESTER:
4464 			cnt = qedr_poll_cq_req(dev, qp, cq, num_entries, wc,
4465 					       &cqe->req);
4466 			try_consume_req_cqe(cq, qp, &cqe->req, &update);
4467 			break;
4468 		case RDMA_CQE_TYPE_RESPONDER_RQ:
4469 			cnt = qedr_poll_cq_resp(dev, qp, cq, num_entries, wc,
4470 						&cqe->resp, &update);
4471 			break;
4472 		case RDMA_CQE_TYPE_RESPONDER_SRQ:
4473 			cnt = qedr_poll_cq_resp_srq(dev, qp, cq, num_entries,
4474 						    wc, &cqe->resp);
4475 			update = 1;
4476 			break;
4477 		case RDMA_CQE_TYPE_INVALID:
4478 		default:
4479 			DP_ERR(dev, "Error: invalid CQE type = %d\n",
4480 			       cqe_get_type(cqe));
4481 		}
4482 		num_entries -= cnt;
4483 		wc += cnt;
4484 		done += cnt;
4485 
4486 		cqe = get_cqe(cq);
4487 	}
4488 	new_cons = qed_chain_get_cons_idx_u32(&cq->pbl);
4489 
4490 	cq->cq_cons += new_cons - old_cons;
4491 
4492 	if (update)
4493 		/* doorbell notifies abount latest VALID entry,
4494 		 * but chain already point to the next INVALID one
4495 		 */
4496 		doorbell_cq(cq, cq->cq_cons - 1, cq->arm_flags);
4497 
4498 	spin_unlock_irqrestore(&cq->cq_lock, flags);
4499 	return done;
4500 }
4501 
qedr_process_mad(struct ib_device * ibdev,int process_mad_flags,u8 port_num,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const struct ib_mad * in,struct ib_mad * out_mad,size_t * out_mad_size,u16 * out_mad_pkey_index)4502 int qedr_process_mad(struct ib_device *ibdev, int process_mad_flags,
4503 		     u8 port_num, const struct ib_wc *in_wc,
4504 		     const struct ib_grh *in_grh, const struct ib_mad *in,
4505 		     struct ib_mad *out_mad, size_t *out_mad_size,
4506 		     u16 *out_mad_pkey_index)
4507 {
4508 	return IB_MAD_RESULT_SUCCESS;
4509 }
4510