xref: /linux/drivers/infiniband/hw/irdma/ctrl.c (revision 6093a688a07da07808f0122f9aa2a3eed250d853)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2015 - 2021 Intel Corporation */
3 #include <linux/etherdevice.h>
4 
5 #include "osdep.h"
6 #include "hmc.h"
7 #include "defs.h"
8 #include "type.h"
9 #include "ws.h"
10 #include "protos.h"
11 
12 /**
13  * irdma_get_qp_from_list - get next qp from a list
14  * @head: Listhead of qp's
15  * @qp: current qp
16  */
17 struct irdma_sc_qp *irdma_get_qp_from_list(struct list_head *head,
18 					   struct irdma_sc_qp *qp)
19 {
20 	struct list_head *lastentry;
21 	struct list_head *entry = NULL;
22 
23 	if (list_empty(head))
24 		return NULL;
25 
26 	if (!qp) {
27 		entry = head->next;
28 	} else {
29 		lastentry = &qp->list;
30 		entry = lastentry->next;
31 		if (entry == head)
32 			return NULL;
33 	}
34 
35 	return container_of(entry, struct irdma_sc_qp, list);
36 }
37 
38 /**
39  * irdma_sc_suspend_resume_qps - suspend/resume all qp's on VSI
40  * @vsi: the VSI struct pointer
41  * @op: Set to IRDMA_OP_RESUME or IRDMA_OP_SUSPEND
42  */
43 void irdma_sc_suspend_resume_qps(struct irdma_sc_vsi *vsi, u8 op)
44 {
45 	struct irdma_sc_qp *qp = NULL;
46 	u8 i;
47 
48 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++) {
49 		mutex_lock(&vsi->qos[i].qos_mutex);
50 		qp = irdma_get_qp_from_list(&vsi->qos[i].qplist, qp);
51 		while (qp) {
52 			if (op == IRDMA_OP_RESUME) {
53 				if (!qp->dev->ws_add(vsi, i)) {
54 					qp->qs_handle =
55 						vsi->qos[qp->user_pri].qs_handle;
56 					irdma_cqp_qp_suspend_resume(qp, op);
57 				} else {
58 					irdma_cqp_qp_suspend_resume(qp, op);
59 					irdma_modify_qp_to_err(qp);
60 				}
61 			} else if (op == IRDMA_OP_SUSPEND) {
62 				/* issue cqp suspend command */
63 				if (!irdma_cqp_qp_suspend_resume(qp, op))
64 					atomic_inc(&vsi->qp_suspend_reqs);
65 			}
66 			qp = irdma_get_qp_from_list(&vsi->qos[i].qplist, qp);
67 		}
68 		mutex_unlock(&vsi->qos[i].qos_mutex);
69 	}
70 }
71 
72 static void irdma_set_qos_info(struct irdma_sc_vsi  *vsi,
73 			       struct irdma_l2params *l2p)
74 {
75 	u8 i;
76 
77 	if (vsi->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
78 		for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++) {
79 			vsi->qos[i].qs_handle = vsi->dev->qos[i].qs_handle;
80 			vsi->qos[i].valid = true;
81 		}
82 
83 		return;
84 	}
85 	vsi->qos_rel_bw = l2p->vsi_rel_bw;
86 	vsi->qos_prio_type = l2p->vsi_prio_type;
87 	vsi->dscp_mode = l2p->dscp_mode;
88 	if (l2p->dscp_mode) {
89 		memcpy(vsi->dscp_map, l2p->dscp_map, sizeof(vsi->dscp_map));
90 		for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++)
91 			l2p->up2tc[i] = i;
92 	}
93 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++) {
94 		if (vsi->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
95 			vsi->qos[i].qs_handle = l2p->qs_handle_list[i];
96 		vsi->qos[i].traffic_class = l2p->up2tc[i];
97 		vsi->qos[i].rel_bw =
98 			l2p->tc_info[vsi->qos[i].traffic_class].rel_bw;
99 		vsi->qos[i].prio_type =
100 			l2p->tc_info[vsi->qos[i].traffic_class].prio_type;
101 		vsi->qos[i].valid = false;
102 	}
103 }
104 
105 /**
106  * irdma_change_l2params - given the new l2 parameters, change all qp
107  * @vsi: RDMA VSI pointer
108  * @l2params: New parameters from l2
109  */
110 void irdma_change_l2params(struct irdma_sc_vsi *vsi,
111 			   struct irdma_l2params *l2params)
112 {
113 	if (l2params->mtu_changed) {
114 		vsi->mtu = l2params->mtu;
115 		if (vsi->ieq)
116 			irdma_reinitialize_ieq(vsi);
117 	}
118 
119 	if (!l2params->tc_changed)
120 		return;
121 
122 	vsi->tc_change_pending = false;
123 	irdma_set_qos_info(vsi, l2params);
124 	irdma_sc_suspend_resume_qps(vsi, IRDMA_OP_RESUME);
125 }
126 
127 /**
128  * irdma_qp_rem_qos - remove qp from qos lists during destroy qp
129  * @qp: qp to be removed from qos
130  */
131 void irdma_qp_rem_qos(struct irdma_sc_qp *qp)
132 {
133 	struct irdma_sc_vsi *vsi = qp->vsi;
134 
135 	ibdev_dbg(to_ibdev(qp->dev),
136 		  "DCB: DCB: Remove qp[%d] UP[%d] qset[%d] on_qoslist[%d]\n",
137 		  qp->qp_uk.qp_id, qp->user_pri, qp->qs_handle,
138 		  qp->on_qoslist);
139 	mutex_lock(&vsi->qos[qp->user_pri].qos_mutex);
140 	if (qp->on_qoslist) {
141 		qp->on_qoslist = false;
142 		list_del(&qp->list);
143 	}
144 	mutex_unlock(&vsi->qos[qp->user_pri].qos_mutex);
145 }
146 
147 /**
148  * irdma_qp_add_qos - called during setctx for qp to be added to qos
149  * @qp: qp to be added to qos
150  */
151 void irdma_qp_add_qos(struct irdma_sc_qp *qp)
152 {
153 	struct irdma_sc_vsi *vsi = qp->vsi;
154 
155 	ibdev_dbg(to_ibdev(qp->dev),
156 		  "DCB: DCB: Add qp[%d] UP[%d] qset[%d] on_qoslist[%d]\n",
157 		  qp->qp_uk.qp_id, qp->user_pri, qp->qs_handle,
158 		  qp->on_qoslist);
159 	mutex_lock(&vsi->qos[qp->user_pri].qos_mutex);
160 	if (!qp->on_qoslist) {
161 		list_add(&qp->list, &vsi->qos[qp->user_pri].qplist);
162 		qp->on_qoslist = true;
163 		qp->qs_handle = vsi->qos[qp->user_pri].qs_handle;
164 	}
165 	mutex_unlock(&vsi->qos[qp->user_pri].qos_mutex);
166 }
167 
168 /**
169  * irdma_sc_pd_init - initialize sc pd struct
170  * @dev: sc device struct
171  * @pd: sc pd ptr
172  * @pd_id: pd_id for allocated pd
173  * @abi_ver: User/Kernel ABI version
174  */
175 void irdma_sc_pd_init(struct irdma_sc_dev *dev, struct irdma_sc_pd *pd, u32 pd_id,
176 		      int abi_ver)
177 {
178 	pd->pd_id = pd_id;
179 	pd->abi_ver = abi_ver;
180 	pd->dev = dev;
181 }
182 
183 /**
184  * irdma_sc_add_arp_cache_entry - cqp wqe add arp cache entry
185  * @cqp: struct for cqp hw
186  * @info: arp entry information
187  * @scratch: u64 saved to be used during cqp completion
188  * @post_sq: flag for cqp db to ring
189  */
190 static int irdma_sc_add_arp_cache_entry(struct irdma_sc_cqp *cqp,
191 					struct irdma_add_arp_cache_entry_info *info,
192 					u64 scratch, bool post_sq)
193 {
194 	__le64 *wqe;
195 	u64 hdr;
196 
197 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
198 	if (!wqe)
199 		return -ENOMEM;
200 	set_64bit_val(wqe, 8, info->reach_max);
201 	set_64bit_val(wqe, 16, ether_addr_to_u64(info->mac_addr));
202 
203 	hdr = info->arp_index |
204 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MANAGE_ARP) |
205 	      FIELD_PREP(IRDMA_CQPSQ_MAT_PERMANENT, (info->permanent ? 1 : 0)) |
206 	      FIELD_PREP(IRDMA_CQPSQ_MAT_ENTRYVALID, 1) |
207 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
208 	dma_wmb(); /* make sure WQE is written before valid bit is set */
209 
210 	set_64bit_val(wqe, 24, hdr);
211 
212 	print_hex_dump_debug("WQE: ARP_CACHE_ENTRY WQE", DUMP_PREFIX_OFFSET,
213 			     16, 8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
214 	if (post_sq)
215 		irdma_sc_cqp_post_sq(cqp);
216 
217 	return 0;
218 }
219 
220 /**
221  * irdma_sc_del_arp_cache_entry - dele arp cache entry
222  * @cqp: struct for cqp hw
223  * @scratch: u64 saved to be used during cqp completion
224  * @arp_index: arp index to delete arp entry
225  * @post_sq: flag for cqp db to ring
226  */
227 static int irdma_sc_del_arp_cache_entry(struct irdma_sc_cqp *cqp, u64 scratch,
228 					u16 arp_index, bool post_sq)
229 {
230 	__le64 *wqe;
231 	u64 hdr;
232 
233 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
234 	if (!wqe)
235 		return -ENOMEM;
236 
237 	hdr = arp_index |
238 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MANAGE_ARP) |
239 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
240 	dma_wmb(); /* make sure WQE is written before valid bit is set */
241 
242 	set_64bit_val(wqe, 24, hdr);
243 
244 	print_hex_dump_debug("WQE: ARP_CACHE_DEL_ENTRY WQE",
245 			     DUMP_PREFIX_OFFSET, 16, 8, wqe,
246 			     IRDMA_CQP_WQE_SIZE * 8, false);
247 	if (post_sq)
248 		irdma_sc_cqp_post_sq(cqp);
249 
250 	return 0;
251 }
252 
253 /**
254  * irdma_sc_manage_apbvt_entry - for adding and deleting apbvt entries
255  * @cqp: struct for cqp hw
256  * @info: info for apbvt entry to add or delete
257  * @scratch: u64 saved to be used during cqp completion
258  * @post_sq: flag for cqp db to ring
259  */
260 static int irdma_sc_manage_apbvt_entry(struct irdma_sc_cqp *cqp,
261 				       struct irdma_apbvt_info *info,
262 				       u64 scratch, bool post_sq)
263 {
264 	__le64 *wqe;
265 	u64 hdr;
266 
267 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
268 	if (!wqe)
269 		return -ENOMEM;
270 
271 	set_64bit_val(wqe, 16, info->port);
272 
273 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MANAGE_APBVT) |
274 	      FIELD_PREP(IRDMA_CQPSQ_MAPT_ADDPORT, info->add) |
275 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
276 	dma_wmb(); /* make sure WQE is written before valid bit is set */
277 
278 	set_64bit_val(wqe, 24, hdr);
279 
280 	print_hex_dump_debug("WQE: MANAGE_APBVT WQE", DUMP_PREFIX_OFFSET, 16,
281 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
282 	if (post_sq)
283 		irdma_sc_cqp_post_sq(cqp);
284 
285 	return 0;
286 }
287 
288 /**
289  * irdma_sc_manage_qhash_table_entry - manage quad hash entries
290  * @cqp: struct for cqp hw
291  * @info: info for quad hash to manage
292  * @scratch: u64 saved to be used during cqp completion
293  * @post_sq: flag for cqp db to ring
294  *
295  * This is called before connection establishment is started.
296  * For passive connections, when listener is created, it will
297  * call with entry type of  IRDMA_QHASH_TYPE_TCP_SYN with local
298  * ip address and tcp port. When SYN is received (passive
299  * connections) or sent (active connections), this routine is
300  * called with entry type of IRDMA_QHASH_TYPE_TCP_ESTABLISHED
301  * and quad is passed in info.
302  *
303  * When iwarp connection is done and its state moves to RTS, the
304  * quad hash entry in the hardware will point to iwarp's qp
305  * number and requires no calls from the driver.
306  */
307 static int
308 irdma_sc_manage_qhash_table_entry(struct irdma_sc_cqp *cqp,
309 				  struct irdma_qhash_table_info *info,
310 				  u64 scratch, bool post_sq)
311 {
312 	__le64 *wqe;
313 	u64 qw1 = 0;
314 	u64 qw2 = 0;
315 	u64 temp;
316 	struct irdma_sc_vsi *vsi = info->vsi;
317 
318 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
319 	if (!wqe)
320 		return -ENOMEM;
321 
322 	set_64bit_val(wqe, 0, ether_addr_to_u64(info->mac_addr));
323 
324 	qw1 = FIELD_PREP(IRDMA_CQPSQ_QHASH_QPN, info->qp_num) |
325 	      FIELD_PREP(IRDMA_CQPSQ_QHASH_DEST_PORT, info->dest_port);
326 	if (info->ipv4_valid) {
327 		set_64bit_val(wqe, 48,
328 			      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR3, info->dest_ip[0]));
329 	} else {
330 		set_64bit_val(wqe, 56,
331 			      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR0, info->dest_ip[0]) |
332 			      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR1, info->dest_ip[1]));
333 
334 		set_64bit_val(wqe, 48,
335 			      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR2, info->dest_ip[2]) |
336 			      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR3, info->dest_ip[3]));
337 	}
338 	qw2 = FIELD_PREP(IRDMA_CQPSQ_QHASH_QS_HANDLE,
339 			 vsi->qos[info->user_pri].qs_handle);
340 	if (info->vlan_valid)
341 		qw2 |= FIELD_PREP(IRDMA_CQPSQ_QHASH_VLANID, info->vlan_id);
342 	set_64bit_val(wqe, 16, qw2);
343 	if (info->entry_type == IRDMA_QHASH_TYPE_TCP_ESTABLISHED) {
344 		qw1 |= FIELD_PREP(IRDMA_CQPSQ_QHASH_SRC_PORT, info->src_port);
345 		if (!info->ipv4_valid) {
346 			set_64bit_val(wqe, 40,
347 				      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR0, info->src_ip[0]) |
348 				      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR1, info->src_ip[1]));
349 			set_64bit_val(wqe, 32,
350 				      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR2, info->src_ip[2]) |
351 				      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR3, info->src_ip[3]));
352 		} else {
353 			set_64bit_val(wqe, 32,
354 				      FIELD_PREP(IRDMA_CQPSQ_QHASH_ADDR3, info->src_ip[0]));
355 		}
356 	}
357 
358 	set_64bit_val(wqe, 8, qw1);
359 	temp = FIELD_PREP(IRDMA_CQPSQ_QHASH_WQEVALID, cqp->polarity) |
360 	       FIELD_PREP(IRDMA_CQPSQ_QHASH_OPCODE,
361 			  IRDMA_CQP_OP_MANAGE_QUAD_HASH_TABLE_ENTRY) |
362 	       FIELD_PREP(IRDMA_CQPSQ_QHASH_MANAGE, info->manage) |
363 	       FIELD_PREP(IRDMA_CQPSQ_QHASH_IPV4VALID, info->ipv4_valid) |
364 	       FIELD_PREP(IRDMA_CQPSQ_QHASH_VLANVALID, info->vlan_valid) |
365 	       FIELD_PREP(IRDMA_CQPSQ_QHASH_ENTRYTYPE, info->entry_type);
366 	dma_wmb(); /* make sure WQE is written before valid bit is set */
367 
368 	set_64bit_val(wqe, 24, temp);
369 
370 	print_hex_dump_debug("WQE: MANAGE_QHASH WQE", DUMP_PREFIX_OFFSET, 16,
371 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
372 	if (post_sq)
373 		irdma_sc_cqp_post_sq(cqp);
374 
375 	return 0;
376 }
377 
378 /**
379  * irdma_sc_qp_init - initialize qp
380  * @qp: sc qp
381  * @info: initialization qp info
382  */
383 int irdma_sc_qp_init(struct irdma_sc_qp *qp, struct irdma_qp_init_info *info)
384 {
385 	int ret_code;
386 	u32 pble_obj_cnt;
387 	u16 wqe_size;
388 
389 	if (info->qp_uk_init_info.max_sq_frag_cnt >
390 	    info->pd->dev->hw_attrs.uk_attrs.max_hw_wq_frags ||
391 	    info->qp_uk_init_info.max_rq_frag_cnt >
392 	    info->pd->dev->hw_attrs.uk_attrs.max_hw_wq_frags)
393 		return -EINVAL;
394 
395 	qp->dev = info->pd->dev;
396 	qp->vsi = info->vsi;
397 	qp->ieq_qp = info->vsi->exception_lan_q;
398 	qp->sq_pa = info->sq_pa;
399 	qp->rq_pa = info->rq_pa;
400 	qp->hw_host_ctx_pa = info->host_ctx_pa;
401 	qp->q2_pa = info->q2_pa;
402 	qp->shadow_area_pa = info->shadow_area_pa;
403 	qp->q2_buf = info->q2;
404 	qp->pd = info->pd;
405 	qp->hw_host_ctx = info->host_ctx;
406 	info->qp_uk_init_info.wqe_alloc_db = qp->pd->dev->wqe_alloc_db;
407 	ret_code = irdma_uk_qp_init(&qp->qp_uk, &info->qp_uk_init_info);
408 	if (ret_code)
409 		return ret_code;
410 
411 	qp->virtual_map = info->virtual_map;
412 	pble_obj_cnt = info->pd->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
413 
414 	if ((info->virtual_map && info->sq_pa >= pble_obj_cnt) ||
415 	    (!info->qp_uk_init_info.srq_uk &&
416 	     info->virtual_map && info->rq_pa >= pble_obj_cnt))
417 		return -EINVAL;
418 
419 	qp->llp_stream_handle = (void *)(-1);
420 	qp->hw_sq_size = irdma_get_encoded_wqe_size(qp->qp_uk.sq_ring.size,
421 						    IRDMA_QUEUE_TYPE_SQ_RQ);
422 	ibdev_dbg(to_ibdev(qp->dev),
423 		  "WQE: hw_sq_size[%04d] sq_ring.size[%04d]\n",
424 		  qp->hw_sq_size, qp->qp_uk.sq_ring.size);
425 	if (qp->qp_uk.uk_attrs->hw_rev == IRDMA_GEN_1 && qp->pd->abi_ver > 4)
426 		wqe_size = IRDMA_WQE_SIZE_128;
427 	else
428 		ret_code = irdma_fragcnt_to_wqesize_rq(qp->qp_uk.max_rq_frag_cnt,
429 						       &wqe_size);
430 	if (ret_code)
431 		return ret_code;
432 
433 	qp->hw_rq_size = irdma_get_encoded_wqe_size(qp->qp_uk.rq_size *
434 				(wqe_size / IRDMA_QP_WQE_MIN_SIZE), IRDMA_QUEUE_TYPE_SQ_RQ);
435 	ibdev_dbg(to_ibdev(qp->dev),
436 		  "WQE: hw_rq_size[%04d] qp_uk.rq_size[%04d] wqe_size[%04d]\n",
437 		  qp->hw_rq_size, qp->qp_uk.rq_size, wqe_size);
438 	qp->sq_tph_val = info->sq_tph_val;
439 	qp->rq_tph_val = info->rq_tph_val;
440 	qp->sq_tph_en = info->sq_tph_en;
441 	qp->rq_tph_en = info->rq_tph_en;
442 	qp->rcv_tph_en = info->rcv_tph_en;
443 	qp->xmit_tph_en = info->xmit_tph_en;
444 	qp->qp_uk.first_sq_wq = info->qp_uk_init_info.first_sq_wq;
445 	qp->qs_handle = qp->vsi->qos[qp->user_pri].qs_handle;
446 
447 	return 0;
448 }
449 
450 /**
451  * irdma_sc_srq_init - init sc_srq structure
452  * @srq: srq sc struct
453  * @info: parameters for srq init
454  */
455 int irdma_sc_srq_init(struct irdma_sc_srq *srq,
456 		      struct irdma_srq_init_info *info)
457 {
458 	u32 srq_size_quanta;
459 	int ret_code;
460 
461 	ret_code = irdma_uk_srq_init(&srq->srq_uk, &info->srq_uk_init_info);
462 	if (ret_code)
463 		return ret_code;
464 
465 	srq->dev = info->pd->dev;
466 	srq->pd = info->pd;
467 	srq->vsi = info->vsi;
468 	srq->srq_pa = info->srq_pa;
469 	srq->first_pm_pbl_idx = info->first_pm_pbl_idx;
470 	srq->pasid = info->pasid;
471 	srq->pasid_valid = info->pasid_valid;
472 	srq->srq_limit = info->srq_limit;
473 	srq->leaf_pbl_size = info->leaf_pbl_size;
474 	srq->virtual_map = info->virtual_map;
475 	srq->tph_en = info->tph_en;
476 	srq->arm_limit_event = info->arm_limit_event;
477 	srq->tph_val = info->tph_value;
478 	srq->shadow_area_pa = info->shadow_area_pa;
479 
480 	/* Smallest SRQ size is 256B i.e. 8 quanta */
481 	srq_size_quanta = max((u32)IRDMA_SRQ_MIN_QUANTA,
482 			      srq->srq_uk.srq_size *
483 			      srq->srq_uk.wqe_size_multiplier);
484 	srq->hw_srq_size = irdma_get_encoded_wqe_size(srq_size_quanta,
485 						      IRDMA_QUEUE_TYPE_SRQ);
486 
487 	return 0;
488 }
489 
490 /**
491  * irdma_sc_srq_create - send srq create CQP WQE
492  * @srq: srq sc struct
493  * @scratch: u64 saved to be used during cqp completion
494  * @post_sq: flag for cqp db to ring
495  */
496 static int irdma_sc_srq_create(struct irdma_sc_srq *srq, u64 scratch,
497 			       bool post_sq)
498 {
499 	struct irdma_sc_cqp *cqp;
500 	__le64 *wqe;
501 	u64 hdr;
502 
503 	cqp = srq->pd->dev->cqp;
504 	if (srq->srq_uk.srq_id < cqp->dev->hw_attrs.min_hw_srq_id ||
505 	    srq->srq_uk.srq_id >
506 	    (cqp->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_SRQ].max_cnt - 1))
507 		return -EINVAL;
508 
509 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
510 	if (!wqe)
511 		return -ENOMEM;
512 
513 	set_64bit_val(wqe, 0,
514 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_SRQ_LIMIT, srq->srq_limit) |
515 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_RQSIZE, srq->hw_srq_size) |
516 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_RQ_WQE_SIZE, srq->srq_uk.wqe_size));
517 	set_64bit_val(wqe, 8, (uintptr_t)srq);
518 	set_64bit_val(wqe, 16,
519 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_PD_ID, srq->pd->pd_id));
520 	set_64bit_val(wqe, 32,
521 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_PHYSICAL_BUFFER_ADDR,
522 				 srq->srq_pa >>
523 				 IRDMA_CQPSQ_SRQ_PHYSICAL_BUFFER_ADDR_S));
524 	set_64bit_val(wqe, 40,
525 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_DB_SHADOW_ADDR,
526 				 srq->shadow_area_pa >>
527 				 IRDMA_CQPSQ_SRQ_DB_SHADOW_ADDR_S));
528 	set_64bit_val(wqe, 48,
529 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_FIRST_PM_PBL_IDX,
530 				 srq->first_pm_pbl_idx));
531 
532 	hdr = srq->srq_uk.srq_id |
533 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_CREATE_SRQ) |
534 	      FIELD_PREP(IRDMA_CQPSQ_SRQ_LEAF_PBL_SIZE, srq->leaf_pbl_size) |
535 	      FIELD_PREP(IRDMA_CQPSQ_SRQ_VIRTMAP, srq->virtual_map) |
536 	      FIELD_PREP(IRDMA_CQPSQ_SRQ_ARM_LIMIT_EVENT,
537 			 srq->arm_limit_event) |
538 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
539 
540 	dma_wmb(); /* make sure WQE is written before valid bit is set */
541 
542 	set_64bit_val(wqe, 24, hdr);
543 
544 	print_hex_dump_debug("WQE: SRQ_CREATE WQE", DUMP_PREFIX_OFFSET, 16, 8,
545 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
546 	if (post_sq)
547 		irdma_sc_cqp_post_sq(cqp);
548 
549 	return 0;
550 }
551 
552 /**
553  * irdma_sc_srq_modify - send modify_srq CQP WQE
554  * @srq: srq sc struct
555  * @info: parameters for srq modification
556  * @scratch: u64 saved to be used during cqp completion
557  * @post_sq: flag for cqp db to ring
558  */
559 static int irdma_sc_srq_modify(struct irdma_sc_srq *srq,
560 			       struct irdma_modify_srq_info *info, u64 scratch,
561 			       bool post_sq)
562 {
563 	struct irdma_sc_cqp *cqp;
564 	__le64 *wqe;
565 	u64 hdr;
566 
567 	cqp = srq->dev->cqp;
568 	if (srq->srq_uk.srq_id < cqp->dev->hw_attrs.min_hw_srq_id ||
569 	    srq->srq_uk.srq_id >
570 	    (cqp->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_SRQ].max_cnt - 1))
571 		return -EINVAL;
572 
573 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
574 	if (!wqe)
575 		return -ENOMEM;
576 
577 	set_64bit_val(wqe, 0,
578 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_SRQ_LIMIT, info->srq_limit) |
579 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_RQSIZE, srq->hw_srq_size) |
580 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_RQ_WQE_SIZE, srq->srq_uk.wqe_size));
581 	set_64bit_val(wqe, 8,
582 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_SRQCTX, srq->srq_uk.srq_id));
583 	set_64bit_val(wqe, 16,
584 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_PD_ID, srq->pd->pd_id));
585 	set_64bit_val(wqe, 32,
586 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_PHYSICAL_BUFFER_ADDR,
587 				 srq->srq_pa >>
588 				 IRDMA_CQPSQ_SRQ_PHYSICAL_BUFFER_ADDR_S));
589 	set_64bit_val(wqe, 40,
590 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_DB_SHADOW_ADDR,
591 				 srq->shadow_area_pa >>
592 				 IRDMA_CQPSQ_SRQ_DB_SHADOW_ADDR_S));
593 	set_64bit_val(wqe, 48,
594 		      FIELD_PREP(IRDMA_CQPSQ_SRQ_FIRST_PM_PBL_IDX,
595 				 srq->first_pm_pbl_idx));
596 
597 	hdr = srq->srq_uk.srq_id |
598 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MODIFY_SRQ) |
599 	      FIELD_PREP(IRDMA_CQPSQ_SRQ_LEAF_PBL_SIZE, srq->leaf_pbl_size) |
600 	      FIELD_PREP(IRDMA_CQPSQ_SRQ_VIRTMAP, srq->virtual_map) |
601 	      FIELD_PREP(IRDMA_CQPSQ_SRQ_ARM_LIMIT_EVENT,
602 			 info->arm_limit_event) |
603 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
604 	dma_wmb(); /* make sure WQE is written before valid bit is set */
605 
606 	set_64bit_val(wqe, 24, hdr);
607 
608 	print_hex_dump_debug("WQE: SRQ_MODIFY WQE", DUMP_PREFIX_OFFSET, 16, 8,
609 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
610 	if (post_sq)
611 		irdma_sc_cqp_post_sq(cqp);
612 
613 	return 0;
614 }
615 
616 /**
617  * irdma_sc_srq_destroy - send srq_destroy CQP WQE
618  * @srq: srq sc struct
619  * @scratch: u64 saved to be used during cqp completion
620  * @post_sq: flag for cqp db to ring
621  */
622 static int irdma_sc_srq_destroy(struct irdma_sc_srq *srq, u64 scratch,
623 				bool post_sq)
624 {
625 	struct irdma_sc_cqp *cqp;
626 	__le64 *wqe;
627 	u64 hdr;
628 
629 	cqp = srq->dev->cqp;
630 
631 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
632 	if (!wqe)
633 		return -ENOMEM;
634 
635 	set_64bit_val(wqe, 8, (uintptr_t)srq);
636 
637 	hdr = srq->srq_uk.srq_id |
638 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DESTROY_SRQ) |
639 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
640 	dma_wmb(); /* make sure WQE is written before valid bit is set */
641 
642 	set_64bit_val(wqe, 24, hdr);
643 
644 	print_hex_dump_debug("WQE: SRQ_DESTROY WQE", DUMP_PREFIX_OFFSET, 16,
645 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
646 	if (post_sq)
647 		irdma_sc_cqp_post_sq(cqp);
648 
649 	return 0;
650 }
651 
652 /**
653  * irdma_sc_qp_create - create qp
654  * @qp: sc qp
655  * @info: qp create info
656  * @scratch: u64 saved to be used during cqp completion
657  * @post_sq: flag for cqp db to ring
658  */
659 int irdma_sc_qp_create(struct irdma_sc_qp *qp, struct irdma_create_qp_info *info,
660 		       u64 scratch, bool post_sq)
661 {
662 	struct irdma_sc_cqp *cqp;
663 	__le64 *wqe;
664 	u64 hdr;
665 
666 	cqp = qp->dev->cqp;
667 	if (qp->qp_uk.qp_id < cqp->dev->hw_attrs.min_hw_qp_id ||
668 	    qp->qp_uk.qp_id >= cqp->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_QP].max_cnt)
669 		return -EINVAL;
670 
671 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
672 	if (!wqe)
673 		return -ENOMEM;
674 
675 	set_64bit_val(wqe, 16, qp->hw_host_ctx_pa);
676 	set_64bit_val(wqe, 40, qp->shadow_area_pa);
677 
678 	hdr = qp->qp_uk.qp_id |
679 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_CREATE_QP) |
680 	      FIELD_PREP(IRDMA_CQPSQ_QP_ORDVALID, (info->ord_valid ? 1 : 0)) |
681 	      FIELD_PREP(IRDMA_CQPSQ_QP_TOECTXVALID, info->tcp_ctx_valid) |
682 	      FIELD_PREP(IRDMA_CQPSQ_QP_MACVALID, info->mac_valid) |
683 	      FIELD_PREP(IRDMA_CQPSQ_QP_QPTYPE, qp->qp_uk.qp_type) |
684 	      FIELD_PREP(IRDMA_CQPSQ_QP_VQ, qp->virtual_map) |
685 	      FIELD_PREP(IRDMA_CQPSQ_QP_FORCELOOPBACK, info->force_lpb) |
686 	      FIELD_PREP(IRDMA_CQPSQ_QP_CQNUMVALID, info->cq_num_valid) |
687 	      FIELD_PREP(IRDMA_CQPSQ_QP_ARPTABIDXVALID,
688 			 info->arp_cache_idx_valid) |
689 	      FIELD_PREP(IRDMA_CQPSQ_QP_NEXTIWSTATE, info->next_iwarp_state) |
690 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
691 	dma_wmb(); /* make sure WQE is written before valid bit is set */
692 
693 	set_64bit_val(wqe, 24, hdr);
694 
695 	print_hex_dump_debug("WQE: QP_CREATE WQE", DUMP_PREFIX_OFFSET, 16, 8,
696 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
697 	if (post_sq)
698 		irdma_sc_cqp_post_sq(cqp);
699 
700 	return 0;
701 }
702 
703 /**
704  * irdma_sc_qp_modify - modify qp cqp wqe
705  * @qp: sc qp
706  * @info: modify qp info
707  * @scratch: u64 saved to be used during cqp completion
708  * @post_sq: flag for cqp db to ring
709  */
710 int irdma_sc_qp_modify(struct irdma_sc_qp *qp, struct irdma_modify_qp_info *info,
711 		       u64 scratch, bool post_sq)
712 {
713 	__le64 *wqe;
714 	struct irdma_sc_cqp *cqp;
715 	u64 hdr;
716 	u8 term_actions = 0;
717 	u8 term_len = 0;
718 
719 	cqp = qp->dev->cqp;
720 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
721 	if (!wqe)
722 		return -ENOMEM;
723 
724 	if (info->next_iwarp_state == IRDMA_QP_STATE_TERMINATE) {
725 		if (info->dont_send_fin)
726 			term_actions += IRDMAQP_TERM_SEND_TERM_ONLY;
727 		if (info->dont_send_term)
728 			term_actions += IRDMAQP_TERM_SEND_FIN_ONLY;
729 		if (term_actions == IRDMAQP_TERM_SEND_TERM_AND_FIN ||
730 		    term_actions == IRDMAQP_TERM_SEND_TERM_ONLY)
731 			term_len = info->termlen;
732 	}
733 
734 	set_64bit_val(wqe, 8,
735 		      FIELD_PREP(IRDMA_CQPSQ_QP_NEWMSS, info->new_mss) |
736 		      FIELD_PREP(IRDMA_CQPSQ_QP_TERMLEN, term_len));
737 	set_64bit_val(wqe, 16, qp->hw_host_ctx_pa);
738 	set_64bit_val(wqe, 40, qp->shadow_area_pa);
739 
740 	hdr = qp->qp_uk.qp_id |
741 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MODIFY_QP) |
742 	      FIELD_PREP(IRDMA_CQPSQ_QP_ORDVALID, info->ord_valid) |
743 	      FIELD_PREP(IRDMA_CQPSQ_QP_TOECTXVALID, info->tcp_ctx_valid) |
744 	      FIELD_PREP(IRDMA_CQPSQ_QP_CACHEDVARVALID,
745 			 info->cached_var_valid) |
746 	      FIELD_PREP(IRDMA_CQPSQ_QP_VQ, qp->virtual_map) |
747 	      FIELD_PREP(IRDMA_CQPSQ_QP_FORCELOOPBACK, info->force_lpb) |
748 	      FIELD_PREP(IRDMA_CQPSQ_QP_CQNUMVALID, info->cq_num_valid) |
749 	      FIELD_PREP(IRDMA_CQPSQ_QP_MACVALID, info->mac_valid) |
750 	      FIELD_PREP(IRDMA_CQPSQ_QP_QPTYPE, qp->qp_uk.qp_type) |
751 	      FIELD_PREP(IRDMA_CQPSQ_QP_MSSCHANGE, info->mss_change) |
752 	      FIELD_PREP(IRDMA_CQPSQ_QP_REMOVEHASHENTRY,
753 			 info->remove_hash_idx) |
754 	      FIELD_PREP(IRDMA_CQPSQ_QP_TERMACT, term_actions) |
755 	      FIELD_PREP(IRDMA_CQPSQ_QP_RESETCON, info->reset_tcp_conn) |
756 	      FIELD_PREP(IRDMA_CQPSQ_QP_ARPTABIDXVALID,
757 			 info->arp_cache_idx_valid) |
758 	      FIELD_PREP(IRDMA_CQPSQ_QP_NEXTIWSTATE, info->next_iwarp_state) |
759 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
760 	dma_wmb(); /* make sure WQE is written before valid bit is set */
761 
762 	set_64bit_val(wqe, 24, hdr);
763 
764 	print_hex_dump_debug("WQE: QP_MODIFY WQE", DUMP_PREFIX_OFFSET, 16, 8,
765 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
766 	if (post_sq)
767 		irdma_sc_cqp_post_sq(cqp);
768 
769 	return 0;
770 }
771 
772 /**
773  * irdma_sc_qp_destroy - cqp destroy qp
774  * @qp: sc qp
775  * @scratch: u64 saved to be used during cqp completion
776  * @remove_hash_idx: flag if to remove hash idx
777  * @ignore_mw_bnd: memory window bind flag
778  * @post_sq: flag for cqp db to ring
779  */
780 int irdma_sc_qp_destroy(struct irdma_sc_qp *qp, u64 scratch,
781 			bool remove_hash_idx, bool ignore_mw_bnd, bool post_sq)
782 {
783 	__le64 *wqe;
784 	struct irdma_sc_cqp *cqp;
785 	u64 hdr;
786 
787 	cqp = qp->dev->cqp;
788 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
789 	if (!wqe)
790 		return -ENOMEM;
791 
792 	set_64bit_val(wqe, 16, qp->hw_host_ctx_pa);
793 	set_64bit_val(wqe, 40, qp->shadow_area_pa);
794 
795 	hdr = qp->qp_uk.qp_id |
796 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DESTROY_QP) |
797 	      FIELD_PREP(IRDMA_CQPSQ_QP_QPTYPE, qp->qp_uk.qp_type) |
798 	      FIELD_PREP(IRDMA_CQPSQ_QP_IGNOREMWBOUND, ignore_mw_bnd) |
799 	      FIELD_PREP(IRDMA_CQPSQ_QP_REMOVEHASHENTRY, remove_hash_idx) |
800 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
801 	dma_wmb(); /* make sure WQE is written before valid bit is set */
802 
803 	set_64bit_val(wqe, 24, hdr);
804 
805 	print_hex_dump_debug("WQE: QP_DESTROY WQE", DUMP_PREFIX_OFFSET, 16, 8,
806 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
807 	if (post_sq)
808 		irdma_sc_cqp_post_sq(cqp);
809 
810 	return 0;
811 }
812 
813 /**
814  * irdma_sc_get_encoded_ird_size -
815  * @ird_size: IRD size
816  * The ird from the connection is rounded to a supported HW setting and then encoded
817  * for ird_size field of qp_ctx. Consumers are expected to provide valid ird size based
818  * on hardware attributes. IRD size defaults to a value of 4 in case of invalid input
819  */
820 static u8 irdma_sc_get_encoded_ird_size(u16 ird_size)
821 {
822 	switch (ird_size ?
823 		roundup_pow_of_two(2 * ird_size) : 4) {
824 	case 256:
825 		return IRDMA_IRD_HW_SIZE_256;
826 	case 128:
827 		return IRDMA_IRD_HW_SIZE_128;
828 	case 64:
829 	case 32:
830 		return IRDMA_IRD_HW_SIZE_64;
831 	case 16:
832 	case 8:
833 		return IRDMA_IRD_HW_SIZE_16;
834 	case 4:
835 	default:
836 		break;
837 	}
838 
839 	return IRDMA_IRD_HW_SIZE_4;
840 }
841 
842 /**
843  * irdma_sc_qp_setctx_roce_gen_2 - set qp's context
844  * @qp: sc qp
845  * @qp_ctx: context ptr
846  * @info: ctx info
847  */
848 static void irdma_sc_qp_setctx_roce_gen_2(struct irdma_sc_qp *qp,
849 					  __le64 *qp_ctx,
850 					  struct irdma_qp_host_ctx_info *info)
851 {
852 	struct irdma_roce_offload_info *roce_info;
853 	struct irdma_udp_offload_info *udp;
854 	u8 push_mode_en;
855 	u32 push_idx;
856 
857 	roce_info = info->roce_info;
858 	udp = info->udp_info;
859 	qp->user_pri = info->user_pri;
860 	if (qp->push_idx == IRDMA_INVALID_PUSH_PAGE_INDEX) {
861 		push_mode_en = 0;
862 		push_idx = 0;
863 	} else {
864 		push_mode_en = 1;
865 		push_idx = qp->push_idx;
866 	}
867 	set_64bit_val(qp_ctx, 0,
868 		      FIELD_PREP(IRDMAQPC_RQWQESIZE, qp->qp_uk.rq_wqe_size) |
869 		      FIELD_PREP(IRDMAQPC_RCVTPHEN, qp->rcv_tph_en) |
870 		      FIELD_PREP(IRDMAQPC_XMITTPHEN, qp->xmit_tph_en) |
871 		      FIELD_PREP(IRDMAQPC_RQTPHEN, qp->rq_tph_en) |
872 		      FIELD_PREP(IRDMAQPC_SQTPHEN, qp->sq_tph_en) |
873 		      FIELD_PREP(IRDMAQPC_PPIDX, push_idx) |
874 		      FIELD_PREP(IRDMAQPC_PMENA, push_mode_en) |
875 		      FIELD_PREP(IRDMAQPC_PDIDXHI, roce_info->pd_id >> 16) |
876 		      FIELD_PREP(IRDMAQPC_DC_TCP_EN, roce_info->dctcp_en) |
877 		      FIELD_PREP(IRDMAQPC_ERR_RQ_IDX_VALID, roce_info->err_rq_idx_valid) |
878 		      FIELD_PREP(IRDMAQPC_ISQP1, roce_info->is_qp1) |
879 		      FIELD_PREP(IRDMAQPC_ROCE_TVER, roce_info->roce_tver) |
880 		      FIELD_PREP(IRDMAQPC_IPV4, udp->ipv4) |
881 		      FIELD_PREP(IRDMAQPC_INSERTVLANTAG, udp->insert_vlan_tag));
882 	set_64bit_val(qp_ctx, 8, qp->sq_pa);
883 	set_64bit_val(qp_ctx, 16, qp->rq_pa);
884 	if ((roce_info->dcqcn_en || roce_info->dctcp_en) &&
885 	    !(udp->tos & 0x03))
886 		udp->tos |= ECN_CODE_PT_VAL;
887 	set_64bit_val(qp_ctx, 24,
888 		      FIELD_PREP(IRDMAQPC_RQSIZE, qp->hw_rq_size) |
889 		      FIELD_PREP(IRDMAQPC_SQSIZE, qp->hw_sq_size) |
890 		      FIELD_PREP(IRDMAQPC_TTL, udp->ttl) | FIELD_PREP(IRDMAQPC_TOS, udp->tos) |
891 		      FIELD_PREP(IRDMAQPC_SRCPORTNUM, udp->src_port) |
892 		      FIELD_PREP(IRDMAQPC_DESTPORTNUM, udp->dst_port));
893 	set_64bit_val(qp_ctx, 32,
894 		      FIELD_PREP(IRDMAQPC_DESTIPADDR2, udp->dest_ip_addr[2]) |
895 		      FIELD_PREP(IRDMAQPC_DESTIPADDR3, udp->dest_ip_addr[3]));
896 	set_64bit_val(qp_ctx, 40,
897 		      FIELD_PREP(IRDMAQPC_DESTIPADDR0, udp->dest_ip_addr[0]) |
898 		      FIELD_PREP(IRDMAQPC_DESTIPADDR1, udp->dest_ip_addr[1]));
899 	set_64bit_val(qp_ctx, 48,
900 		      FIELD_PREP(IRDMAQPC_SNDMSS, udp->snd_mss) |
901 		      FIELD_PREP(IRDMAQPC_VLANTAG, udp->vlan_tag) |
902 		      FIELD_PREP(IRDMAQPC_ARPIDX, udp->arp_idx));
903 	set_64bit_val(qp_ctx, 56,
904 		      FIELD_PREP(IRDMAQPC_PKEY, roce_info->p_key) |
905 		      FIELD_PREP(IRDMAQPC_PDIDX, roce_info->pd_id) |
906 		      FIELD_PREP(IRDMAQPC_ACKCREDITS, roce_info->ack_credits) |
907 		      FIELD_PREP(IRDMAQPC_FLOWLABEL, udp->flow_label));
908 	set_64bit_val(qp_ctx, 64,
909 		      FIELD_PREP(IRDMAQPC_QKEY, roce_info->qkey) |
910 		      FIELD_PREP(IRDMAQPC_DESTQP, roce_info->dest_qp));
911 	set_64bit_val(qp_ctx, 80,
912 		      FIELD_PREP(IRDMAQPC_PSNNXT, udp->psn_nxt) |
913 		      FIELD_PREP(IRDMAQPC_LSN, udp->lsn));
914 	set_64bit_val(qp_ctx, 88,
915 		      FIELD_PREP(IRDMAQPC_EPSN, udp->epsn));
916 	set_64bit_val(qp_ctx, 96,
917 		      FIELD_PREP(IRDMAQPC_PSNMAX, udp->psn_max) |
918 		      FIELD_PREP(IRDMAQPC_PSNUNA, udp->psn_una));
919 	set_64bit_val(qp_ctx, 112,
920 		      FIELD_PREP(IRDMAQPC_CWNDROCE, udp->cwnd));
921 	set_64bit_val(qp_ctx, 128,
922 		      FIELD_PREP(IRDMAQPC_ERR_RQ_IDX, roce_info->err_rq_idx) |
923 		      FIELD_PREP(IRDMAQPC_RNRNAK_THRESH, udp->rnr_nak_thresh) |
924 		      FIELD_PREP(IRDMAQPC_REXMIT_THRESH, udp->rexmit_thresh) |
925 		      FIELD_PREP(IRDMAQPC_RTOMIN, roce_info->rtomin));
926 	set_64bit_val(qp_ctx, 136,
927 		      FIELD_PREP(IRDMAQPC_TXCQNUM, info->send_cq_num) |
928 		      FIELD_PREP(IRDMAQPC_RXCQNUM, info->rcv_cq_num));
929 	set_64bit_val(qp_ctx, 144,
930 		      FIELD_PREP(IRDMAQPC_STAT_INDEX, info->stats_idx));
931 	set_64bit_val(qp_ctx, 152, ether_addr_to_u64(roce_info->mac_addr) << 16);
932 	set_64bit_val(qp_ctx, 160,
933 		      FIELD_PREP(IRDMAQPC_ORDSIZE, roce_info->ord_size) |
934 		      FIELD_PREP(IRDMAQPC_IRDSIZE, irdma_sc_get_encoded_ird_size(roce_info->ird_size)) |
935 		      FIELD_PREP(IRDMAQPC_WRRDRSPOK, roce_info->wr_rdresp_en) |
936 		      FIELD_PREP(IRDMAQPC_RDOK, roce_info->rd_en) |
937 		      FIELD_PREP(IRDMAQPC_USESTATSINSTANCE, info->stats_idx_valid) |
938 		      FIELD_PREP(IRDMAQPC_BINDEN, roce_info->bind_en) |
939 		      FIELD_PREP(IRDMAQPC_FASTREGEN, roce_info->fast_reg_en) |
940 		      FIELD_PREP(IRDMAQPC_DCQCNENABLE, roce_info->dcqcn_en) |
941 		      FIELD_PREP(IRDMAQPC_RCVNOICRC, roce_info->rcv_no_icrc) |
942 		      FIELD_PREP(IRDMAQPC_FW_CC_ENABLE, roce_info->fw_cc_enable) |
943 		      FIELD_PREP(IRDMAQPC_UDPRIVCQENABLE, roce_info->udprivcq_en) |
944 		      FIELD_PREP(IRDMAQPC_PRIVEN, roce_info->priv_mode_en) |
945 		      FIELD_PREP(IRDMAQPC_TIMELYENABLE, roce_info->timely_en));
946 	set_64bit_val(qp_ctx, 168,
947 		      FIELD_PREP(IRDMAQPC_QPCOMPCTX, info->qp_compl_ctx));
948 	set_64bit_val(qp_ctx, 176,
949 		      FIELD_PREP(IRDMAQPC_SQTPHVAL, qp->sq_tph_val) |
950 		      FIELD_PREP(IRDMAQPC_RQTPHVAL, qp->rq_tph_val) |
951 		      FIELD_PREP(IRDMAQPC_QSHANDLE, qp->qs_handle));
952 	set_64bit_val(qp_ctx, 184,
953 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR3, udp->local_ipaddr[3]) |
954 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR2, udp->local_ipaddr[2]));
955 	set_64bit_val(qp_ctx, 192,
956 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR1, udp->local_ipaddr[1]) |
957 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR0, udp->local_ipaddr[0]));
958 	set_64bit_val(qp_ctx, 200,
959 		      FIELD_PREP(IRDMAQPC_THIGH, roce_info->t_high) |
960 		      FIELD_PREP(IRDMAQPC_TLOW, roce_info->t_low));
961 	set_64bit_val(qp_ctx, 208,
962 		      FIELD_PREP(IRDMAQPC_REMENDPOINTIDX, info->rem_endpoint_idx));
963 
964 	print_hex_dump_debug("WQE: QP_HOST CTX WQE", DUMP_PREFIX_OFFSET, 16,
965 			     8, qp_ctx, IRDMA_QP_CTX_SIZE, false);
966 }
967 
968 /**
969  * irdma_sc_get_encoded_ird_size_gen_3 - get encoded IRD size for GEN 3
970  * @ird_size: IRD size
971  * The ird from the connection is rounded to a supported HW setting and then encoded
972  * for ird_size field of qp_ctx. Consumers are expected to provide valid ird size based
973  * on hardware attributes. IRD size defaults to a value of 4 in case of invalid input.
974  */
975 static u8 irdma_sc_get_encoded_ird_size_gen_3(u16 ird_size)
976 {
977 	switch (ird_size ?
978 		roundup_pow_of_two(2 * ird_size) : 4) {
979 	case 4096:
980 		return IRDMA_IRD_HW_SIZE_4096_GEN3;
981 	case 2048:
982 		return IRDMA_IRD_HW_SIZE_2048_GEN3;
983 	case 1024:
984 		return IRDMA_IRD_HW_SIZE_1024_GEN3;
985 	case 512:
986 		return IRDMA_IRD_HW_SIZE_512_GEN3;
987 	case 256:
988 		return IRDMA_IRD_HW_SIZE_256_GEN3;
989 	case 128:
990 		return IRDMA_IRD_HW_SIZE_128_GEN3;
991 	case 64:
992 		return IRDMA_IRD_HW_SIZE_64_GEN3;
993 	case 32:
994 		return IRDMA_IRD_HW_SIZE_32_GEN3;
995 	case 16:
996 		return IRDMA_IRD_HW_SIZE_16_GEN3;
997 	case 8:
998 		return IRDMA_IRD_HW_SIZE_8_GEN3;
999 	case 4:
1000 	default:
1001 		break;
1002 	}
1003 
1004 	return IRDMA_IRD_HW_SIZE_4_GEN3;
1005 }
1006 
1007 /**
1008  * irdma_sc_qp_setctx_roce_gen_3 - set qp's context
1009  * @qp: sc qp
1010  * @qp_ctx: context ptr
1011  * @info: ctx info
1012  */
1013 static void irdma_sc_qp_setctx_roce_gen_3(struct irdma_sc_qp *qp,
1014 					  __le64 *qp_ctx,
1015 					  struct irdma_qp_host_ctx_info *info)
1016 {
1017 	struct irdma_roce_offload_info *roce_info = info->roce_info;
1018 	struct irdma_udp_offload_info *udp = info->udp_info;
1019 	u64 qw0, qw3, qw7 = 0, qw8 = 0;
1020 	u8 push_mode_en;
1021 	u32 push_idx;
1022 
1023 	qp->user_pri = info->user_pri;
1024 	if (qp->push_idx == IRDMA_INVALID_PUSH_PAGE_INDEX) {
1025 		push_mode_en = 0;
1026 		push_idx = 0;
1027 	} else {
1028 		push_mode_en = 1;
1029 		push_idx = qp->push_idx;
1030 	}
1031 
1032 	qw0 = FIELD_PREP(IRDMAQPC_RQWQESIZE, qp->qp_uk.rq_wqe_size) |
1033 	      FIELD_PREP(IRDMAQPC_RCVTPHEN, qp->rcv_tph_en) |
1034 	      FIELD_PREP(IRDMAQPC_XMITTPHEN, qp->xmit_tph_en) |
1035 	      FIELD_PREP(IRDMAQPC_RQTPHEN, qp->rq_tph_en) |
1036 	      FIELD_PREP(IRDMAQPC_SQTPHEN, qp->sq_tph_en) |
1037 	      FIELD_PREP(IRDMAQPC_PPIDX, push_idx) |
1038 	      FIELD_PREP(IRDMAQPC_PMENA, push_mode_en) |
1039 	      FIELD_PREP(IRDMAQPC_DC_TCP_EN, roce_info->dctcp_en) |
1040 	      FIELD_PREP(IRDMAQPC_ISQP1, roce_info->is_qp1) |
1041 	      FIELD_PREP(IRDMAQPC_ROCE_TVER, roce_info->roce_tver) |
1042 	      FIELD_PREP(IRDMAQPC_IPV4, udp->ipv4) |
1043 	      FIELD_PREP(IRDMAQPC_USE_SRQ, !qp->qp_uk.srq_uk ? 0 : 1) |
1044 	      FIELD_PREP(IRDMAQPC_INSERTVLANTAG, udp->insert_vlan_tag);
1045 	set_64bit_val(qp_ctx, 0, qw0);
1046 	set_64bit_val(qp_ctx, 8, qp->sq_pa);
1047 	set_64bit_val(qp_ctx, 16, qp->rq_pa);
1048 	qw3 = FIELD_PREP(IRDMAQPC_RQSIZE, qp->hw_rq_size) |
1049 	      FIELD_PREP(IRDMAQPC_SQSIZE, qp->hw_sq_size) |
1050 	      FIELD_PREP(IRDMAQPC_TTL, udp->ttl) |
1051 	      FIELD_PREP(IRDMAQPC_TOS, udp->tos) |
1052 	      FIELD_PREP(IRDMAQPC_SRCPORTNUM, udp->src_port) |
1053 	      FIELD_PREP(IRDMAQPC_DESTPORTNUM, udp->dst_port);
1054 	set_64bit_val(qp_ctx, 24, qw3);
1055 	set_64bit_val(qp_ctx, 32,
1056 		      FIELD_PREP(IRDMAQPC_DESTIPADDR2, udp->dest_ip_addr[2]) |
1057 		      FIELD_PREP(IRDMAQPC_DESTIPADDR3, udp->dest_ip_addr[3]));
1058 	set_64bit_val(qp_ctx, 40,
1059 		      FIELD_PREP(IRDMAQPC_DESTIPADDR0, udp->dest_ip_addr[0]) |
1060 		      FIELD_PREP(IRDMAQPC_DESTIPADDR1, udp->dest_ip_addr[1]));
1061 	set_64bit_val(qp_ctx, 48,
1062 		      FIELD_PREP(IRDMAQPC_SNDMSS, udp->snd_mss) |
1063 		      FIELD_PREP(IRDMAQPC_VLANTAG, udp->vlan_tag) |
1064 		      FIELD_PREP(IRDMAQPC_ARPIDX, udp->arp_idx));
1065 	qw7 =  FIELD_PREP(IRDMAQPC_PKEY, roce_info->p_key) |
1066 	       FIELD_PREP(IRDMAQPC_ACKCREDITS, roce_info->ack_credits) |
1067 	       FIELD_PREP(IRDMAQPC_FLOWLABEL, udp->flow_label);
1068 	set_64bit_val(qp_ctx, 56, qw7);
1069 	qw8 = FIELD_PREP(IRDMAQPC_QKEY, roce_info->qkey) |
1070 	      FIELD_PREP(IRDMAQPC_DESTQP, roce_info->dest_qp);
1071 	set_64bit_val(qp_ctx, 64, qw8);
1072 	set_64bit_val(qp_ctx, 80,
1073 		      FIELD_PREP(IRDMAQPC_PSNNXT, udp->psn_nxt) |
1074 		      FIELD_PREP(IRDMAQPC_LSN, udp->lsn));
1075 	set_64bit_val(qp_ctx, 88,
1076 		      FIELD_PREP(IRDMAQPC_EPSN, udp->epsn));
1077 	set_64bit_val(qp_ctx, 96,
1078 		      FIELD_PREP(IRDMAQPC_PSNMAX, udp->psn_max) |
1079 		      FIELD_PREP(IRDMAQPC_PSNUNA, udp->psn_una));
1080 	set_64bit_val(qp_ctx, 112,
1081 		      FIELD_PREP(IRDMAQPC_CWNDROCE, udp->cwnd));
1082 	set_64bit_val(qp_ctx, 128,
1083 		      FIELD_PREP(IRDMAQPC_MINRNR_TIMER, udp->min_rnr_timer) |
1084 		      FIELD_PREP(IRDMAQPC_RNRNAK_THRESH, udp->rnr_nak_thresh) |
1085 		      FIELD_PREP(IRDMAQPC_REXMIT_THRESH, udp->rexmit_thresh) |
1086 		      FIELD_PREP(IRDMAQPC_RNRNAK_TMR, udp->rnr_nak_tmr) |
1087 		      FIELD_PREP(IRDMAQPC_RTOMIN, roce_info->rtomin));
1088 	set_64bit_val(qp_ctx, 136,
1089 		      FIELD_PREP(IRDMAQPC_TXCQNUM, info->send_cq_num) |
1090 		      FIELD_PREP(IRDMAQPC_RXCQNUM, info->rcv_cq_num));
1091 	set_64bit_val(qp_ctx, 152,
1092 		      FIELD_PREP(IRDMAQPC_MACADDRESS,
1093 				 ether_addr_to_u64(roce_info->mac_addr)) |
1094 		      FIELD_PREP(IRDMAQPC_LOCALACKTIMEOUT,
1095 				 roce_info->local_ack_timeout));
1096 	set_64bit_val(qp_ctx, 160,
1097 		      FIELD_PREP(IRDMAQPC_ORDSIZE_GEN3, roce_info->ord_size) |
1098 		      FIELD_PREP(IRDMAQPC_IRDSIZE_GEN3,
1099 				 irdma_sc_get_encoded_ird_size_gen_3(roce_info->ird_size)) |
1100 		      FIELD_PREP(IRDMAQPC_WRRDRSPOK, roce_info->wr_rdresp_en) |
1101 		      FIELD_PREP(IRDMAQPC_RDOK, roce_info->rd_en) |
1102 		      FIELD_PREP(IRDMAQPC_USESTATSINSTANCE,
1103 				 info->stats_idx_valid) |
1104 		      FIELD_PREP(IRDMAQPC_BINDEN, roce_info->bind_en) |
1105 		      FIELD_PREP(IRDMAQPC_FASTREGEN, roce_info->fast_reg_en) |
1106 		      FIELD_PREP(IRDMAQPC_DCQCNENABLE, roce_info->dcqcn_en) |
1107 		      FIELD_PREP(IRDMAQPC_RCVNOICRC, roce_info->rcv_no_icrc) |
1108 		      FIELD_PREP(IRDMAQPC_FW_CC_ENABLE,
1109 				 roce_info->fw_cc_enable) |
1110 		      FIELD_PREP(IRDMAQPC_UDPRIVCQENABLE,
1111 				 roce_info->udprivcq_en) |
1112 		      FIELD_PREP(IRDMAQPC_PRIVEN, roce_info->priv_mode_en) |
1113 		      FIELD_PREP(IRDMAQPC_REMOTE_ATOMIC_EN,
1114 				 info->remote_atomics_en) |
1115 		      FIELD_PREP(IRDMAQPC_TIMELYENABLE, roce_info->timely_en));
1116 	set_64bit_val(qp_ctx, 168,
1117 		      FIELD_PREP(IRDMAQPC_QPCOMPCTX, info->qp_compl_ctx));
1118 	set_64bit_val(qp_ctx, 176,
1119 		      FIELD_PREP(IRDMAQPC_SQTPHVAL, qp->sq_tph_val) |
1120 		      FIELD_PREP(IRDMAQPC_RQTPHVAL, qp->rq_tph_val) |
1121 		      FIELD_PREP(IRDMAQPC_QSHANDLE, qp->qs_handle));
1122 	set_64bit_val(qp_ctx, 184,
1123 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR3, udp->local_ipaddr[3]) |
1124 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR2, udp->local_ipaddr[2]));
1125 	set_64bit_val(qp_ctx, 192,
1126 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR1, udp->local_ipaddr[1]) |
1127 		      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR0, udp->local_ipaddr[0]));
1128 	set_64bit_val(qp_ctx, 200,
1129 		      FIELD_PREP(IRDMAQPC_THIGH, roce_info->t_high) |
1130 		      FIELD_PREP(IRDMAQPC_SRQ_ID,
1131 				 !qp->qp_uk.srq_uk ?
1132 					0 : qp->qp_uk.srq_uk->srq_id) |
1133 		      FIELD_PREP(IRDMAQPC_TLOW, roce_info->t_low));
1134 	set_64bit_val(qp_ctx, 208, roce_info->pd_id |
1135 		      FIELD_PREP(IRDMAQPC_STAT_INDEX_GEN3, info->stats_idx) |
1136 		      FIELD_PREP(IRDMAQPC_PKT_LIMIT, qp->pkt_limit));
1137 
1138 	print_hex_dump_debug("WQE: QP_HOST ROCE CTX WQE", DUMP_PREFIX_OFFSET,
1139 			     16, 8, qp_ctx, IRDMA_QP_CTX_SIZE, false);
1140 }
1141 
1142 void irdma_sc_qp_setctx_roce(struct irdma_sc_qp *qp, __le64 *qp_ctx,
1143 			     struct irdma_qp_host_ctx_info *info)
1144 {
1145 	if (qp->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_2)
1146 		irdma_sc_qp_setctx_roce_gen_2(qp, qp_ctx, info);
1147 	else
1148 		irdma_sc_qp_setctx_roce_gen_3(qp, qp_ctx, info);
1149 }
1150 
1151 /* irdma_sc_alloc_local_mac_entry - allocate a mac entry
1152  * @cqp: struct for cqp hw
1153  * @scratch: u64 saved to be used during cqp completion
1154  * @post_sq: flag for cqp db to ring
1155  */
1156 static int irdma_sc_alloc_local_mac_entry(struct irdma_sc_cqp *cqp, u64 scratch,
1157 					  bool post_sq)
1158 {
1159 	__le64 *wqe;
1160 	u64 hdr;
1161 
1162 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1163 	if (!wqe)
1164 		return -ENOMEM;
1165 
1166 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE,
1167 			 IRDMA_CQP_OP_ALLOCATE_LOC_MAC_TABLE_ENTRY) |
1168 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
1169 
1170 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1171 
1172 	set_64bit_val(wqe, 24, hdr);
1173 
1174 	print_hex_dump_debug("WQE: ALLOCATE_LOCAL_MAC WQE",
1175 			     DUMP_PREFIX_OFFSET, 16, 8, wqe,
1176 			     IRDMA_CQP_WQE_SIZE * 8, false);
1177 
1178 	if (post_sq)
1179 		irdma_sc_cqp_post_sq(cqp);
1180 	return 0;
1181 }
1182 
1183 /**
1184  * irdma_sc_add_local_mac_entry - add mac enry
1185  * @cqp: struct for cqp hw
1186  * @info:mac addr info
1187  * @scratch: u64 saved to be used during cqp completion
1188  * @post_sq: flag for cqp db to ring
1189  */
1190 static int irdma_sc_add_local_mac_entry(struct irdma_sc_cqp *cqp,
1191 					struct irdma_local_mac_entry_info *info,
1192 					u64 scratch, bool post_sq)
1193 {
1194 	__le64 *wqe;
1195 	u64 header;
1196 
1197 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1198 	if (!wqe)
1199 		return -ENOMEM;
1200 
1201 	set_64bit_val(wqe, 32, ether_addr_to_u64(info->mac_addr));
1202 
1203 	header = FIELD_PREP(IRDMA_CQPSQ_MLM_TABLEIDX, info->entry_idx) |
1204 		 FIELD_PREP(IRDMA_CQPSQ_OPCODE,
1205 			    IRDMA_CQP_OP_MANAGE_LOC_MAC_TABLE) |
1206 		 FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
1207 
1208 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1209 
1210 	set_64bit_val(wqe, 24, header);
1211 
1212 	print_hex_dump_debug("WQE: ADD_LOCAL_MAC WQE", DUMP_PREFIX_OFFSET, 16,
1213 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
1214 
1215 	if (post_sq)
1216 		irdma_sc_cqp_post_sq(cqp);
1217 	return 0;
1218 }
1219 
1220 /**
1221  * irdma_sc_del_local_mac_entry - cqp wqe to dele local mac
1222  * @cqp: struct for cqp hw
1223  * @scratch: u64 saved to be used during cqp completion
1224  * @entry_idx: index of mac entry
1225  * @ignore_ref_count: to force mac adde delete
1226  * @post_sq: flag for cqp db to ring
1227  */
1228 static int irdma_sc_del_local_mac_entry(struct irdma_sc_cqp *cqp, u64 scratch,
1229 					u16 entry_idx, u8 ignore_ref_count,
1230 					bool post_sq)
1231 {
1232 	__le64 *wqe;
1233 	u64 header;
1234 
1235 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1236 	if (!wqe)
1237 		return -ENOMEM;
1238 	header = FIELD_PREP(IRDMA_CQPSQ_MLM_TABLEIDX, entry_idx) |
1239 		 FIELD_PREP(IRDMA_CQPSQ_OPCODE,
1240 			    IRDMA_CQP_OP_MANAGE_LOC_MAC_TABLE) |
1241 		 FIELD_PREP(IRDMA_CQPSQ_MLM_FREEENTRY, 1) |
1242 		 FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity) |
1243 		 FIELD_PREP(IRDMA_CQPSQ_MLM_IGNORE_REF_CNT, ignore_ref_count);
1244 
1245 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1246 
1247 	set_64bit_val(wqe, 24, header);
1248 
1249 	print_hex_dump_debug("WQE: DEL_LOCAL_MAC_IPADDR WQE",
1250 			     DUMP_PREFIX_OFFSET, 16, 8, wqe,
1251 			     IRDMA_CQP_WQE_SIZE * 8, false);
1252 
1253 	if (post_sq)
1254 		irdma_sc_cqp_post_sq(cqp);
1255 	return 0;
1256 }
1257 
1258 /**
1259  * irdma_sc_qp_setctx - set qp's context
1260  * @qp: sc qp
1261  * @qp_ctx: context ptr
1262  * @info: ctx info
1263  */
1264 void irdma_sc_qp_setctx(struct irdma_sc_qp *qp, __le64 *qp_ctx,
1265 			struct irdma_qp_host_ctx_info *info)
1266 {
1267 	struct irdma_iwarp_offload_info *iw;
1268 	struct irdma_tcp_offload_info *tcp;
1269 	struct irdma_sc_dev *dev;
1270 	u8 push_mode_en;
1271 	u32 push_idx;
1272 	u64 qw0, qw3, qw7 = 0, qw16 = 0;
1273 	u64 mac = 0;
1274 
1275 	iw = info->iwarp_info;
1276 	tcp = info->tcp_info;
1277 	dev = qp->dev;
1278 	if (iw->rcv_mark_en) {
1279 		qp->pfpdu.marker_len = 4;
1280 		qp->pfpdu.rcv_start_seq = tcp->rcv_nxt;
1281 	}
1282 	qp->user_pri = info->user_pri;
1283 	if (qp->push_idx == IRDMA_INVALID_PUSH_PAGE_INDEX) {
1284 		push_mode_en = 0;
1285 		push_idx = 0;
1286 	} else {
1287 		push_mode_en = 1;
1288 		push_idx = qp->push_idx;
1289 	}
1290 	qw0 = FIELD_PREP(IRDMAQPC_RQWQESIZE, qp->qp_uk.rq_wqe_size) |
1291 	      FIELD_PREP(IRDMAQPC_RCVTPHEN, qp->rcv_tph_en) |
1292 	      FIELD_PREP(IRDMAQPC_XMITTPHEN, qp->xmit_tph_en) |
1293 	      FIELD_PREP(IRDMAQPC_RQTPHEN, qp->rq_tph_en) |
1294 	      FIELD_PREP(IRDMAQPC_SQTPHEN, qp->sq_tph_en) |
1295 	      FIELD_PREP(IRDMAQPC_PPIDX, push_idx) |
1296 	      FIELD_PREP(IRDMAQPC_PMENA, push_mode_en);
1297 
1298 	set_64bit_val(qp_ctx, 8, qp->sq_pa);
1299 	set_64bit_val(qp_ctx, 16, qp->rq_pa);
1300 
1301 	qw3 = FIELD_PREP(IRDMAQPC_RQSIZE, qp->hw_rq_size) |
1302 	      FIELD_PREP(IRDMAQPC_SQSIZE, qp->hw_sq_size);
1303 	if (dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
1304 		qw3 |= FIELD_PREP(IRDMAQPC_GEN1_SRCMACADDRIDX,
1305 				  qp->src_mac_addr_idx);
1306 	set_64bit_val(qp_ctx, 136,
1307 		      FIELD_PREP(IRDMAQPC_TXCQNUM, info->send_cq_num) |
1308 		      FIELD_PREP(IRDMAQPC_RXCQNUM, info->rcv_cq_num));
1309 	set_64bit_val(qp_ctx, 168,
1310 		      FIELD_PREP(IRDMAQPC_QPCOMPCTX, info->qp_compl_ctx));
1311 	set_64bit_val(qp_ctx, 176,
1312 		      FIELD_PREP(IRDMAQPC_SQTPHVAL, qp->sq_tph_val) |
1313 		      FIELD_PREP(IRDMAQPC_RQTPHVAL, qp->rq_tph_val) |
1314 		      FIELD_PREP(IRDMAQPC_QSHANDLE, qp->qs_handle) |
1315 		      FIELD_PREP(IRDMAQPC_EXCEPTION_LAN_QUEUE, qp->ieq_qp));
1316 	if (info->iwarp_info_valid) {
1317 		qw0 |= FIELD_PREP(IRDMAQPC_DDP_VER, iw->ddp_ver) |
1318 		       FIELD_PREP(IRDMAQPC_RDMAP_VER, iw->rdmap_ver) |
1319 		       FIELD_PREP(IRDMAQPC_DC_TCP_EN, iw->dctcp_en) |
1320 		       FIELD_PREP(IRDMAQPC_ECN_EN, iw->ecn_en) |
1321 		       FIELD_PREP(IRDMAQPC_IBRDENABLE, iw->ib_rd_en) |
1322 		       FIELD_PREP(IRDMAQPC_PDIDXHI, iw->pd_id >> 16) |
1323 		       FIELD_PREP(IRDMAQPC_ERR_RQ_IDX_VALID,
1324 				  iw->err_rq_idx_valid);
1325 		qw7 |= FIELD_PREP(IRDMAQPC_PDIDX, iw->pd_id);
1326 		qw16 |= FIELD_PREP(IRDMAQPC_ERR_RQ_IDX, iw->err_rq_idx) |
1327 			FIELD_PREP(IRDMAQPC_RTOMIN, iw->rtomin);
1328 		set_64bit_val(qp_ctx, 144,
1329 			      FIELD_PREP(IRDMAQPC_Q2ADDR, qp->q2_pa >> 8) |
1330 			      FIELD_PREP(IRDMAQPC_STAT_INDEX, info->stats_idx));
1331 
1332 		if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_2)
1333 			mac = ether_addr_to_u64(iw->mac_addr);
1334 
1335 		set_64bit_val(qp_ctx, 152,
1336 			      mac << 16 | FIELD_PREP(IRDMAQPC_LASTBYTESENT, iw->last_byte_sent));
1337 		set_64bit_val(qp_ctx, 160,
1338 			      FIELD_PREP(IRDMAQPC_ORDSIZE, iw->ord_size) |
1339 			      FIELD_PREP(IRDMAQPC_IRDSIZE, irdma_sc_get_encoded_ird_size(iw->ird_size)) |
1340 			      FIELD_PREP(IRDMAQPC_WRRDRSPOK, iw->wr_rdresp_en) |
1341 			      FIELD_PREP(IRDMAQPC_RDOK, iw->rd_en) |
1342 			      FIELD_PREP(IRDMAQPC_SNDMARKERS, iw->snd_mark_en) |
1343 			      FIELD_PREP(IRDMAQPC_BINDEN, iw->bind_en) |
1344 			      FIELD_PREP(IRDMAQPC_FASTREGEN, iw->fast_reg_en) |
1345 			      FIELD_PREP(IRDMAQPC_PRIVEN, iw->priv_mode_en) |
1346 			      FIELD_PREP(IRDMAQPC_USESTATSINSTANCE, info->stats_idx_valid) |
1347 			      FIELD_PREP(IRDMAQPC_IWARPMODE, 1) |
1348 			      FIELD_PREP(IRDMAQPC_RCVMARKERS, iw->rcv_mark_en) |
1349 			      FIELD_PREP(IRDMAQPC_ALIGNHDRS, iw->align_hdrs) |
1350 			      FIELD_PREP(IRDMAQPC_RCVNOMPACRC, iw->rcv_no_mpa_crc) |
1351 			      FIELD_PREP(IRDMAQPC_RCVMARKOFFSET, iw->rcv_mark_offset || !tcp ? iw->rcv_mark_offset : tcp->rcv_nxt) |
1352 			      FIELD_PREP(IRDMAQPC_SNDMARKOFFSET, iw->snd_mark_offset || !tcp ? iw->snd_mark_offset : tcp->snd_nxt) |
1353 			      FIELD_PREP(IRDMAQPC_TIMELYENABLE, iw->timely_en));
1354 	}
1355 	if (info->tcp_info_valid) {
1356 		qw0 |= FIELD_PREP(IRDMAQPC_IPV4, tcp->ipv4) |
1357 		       FIELD_PREP(IRDMAQPC_NONAGLE, tcp->no_nagle) |
1358 		       FIELD_PREP(IRDMAQPC_INSERTVLANTAG,
1359 				  tcp->insert_vlan_tag) |
1360 		       FIELD_PREP(IRDMAQPC_TIMESTAMP, tcp->time_stamp) |
1361 		       FIELD_PREP(IRDMAQPC_LIMIT, tcp->cwnd_inc_limit) |
1362 		       FIELD_PREP(IRDMAQPC_DROPOOOSEG, tcp->drop_ooo_seg) |
1363 		       FIELD_PREP(IRDMAQPC_DUPACK_THRESH, tcp->dup_ack_thresh);
1364 
1365 		if ((iw->ecn_en || iw->dctcp_en) && !(tcp->tos & 0x03))
1366 			tcp->tos |= ECN_CODE_PT_VAL;
1367 
1368 		qw3 |= FIELD_PREP(IRDMAQPC_TTL, tcp->ttl) |
1369 		       FIELD_PREP(IRDMAQPC_AVOIDSTRETCHACK, tcp->avoid_stretch_ack) |
1370 		       FIELD_PREP(IRDMAQPC_TOS, tcp->tos) |
1371 		       FIELD_PREP(IRDMAQPC_SRCPORTNUM, tcp->src_port) |
1372 		       FIELD_PREP(IRDMAQPC_DESTPORTNUM, tcp->dst_port);
1373 		if (dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1) {
1374 			qw3 |= FIELD_PREP(IRDMAQPC_GEN1_SRCMACADDRIDX, tcp->src_mac_addr_idx);
1375 
1376 			qp->src_mac_addr_idx = tcp->src_mac_addr_idx;
1377 		}
1378 		set_64bit_val(qp_ctx, 32,
1379 			      FIELD_PREP(IRDMAQPC_DESTIPADDR2, tcp->dest_ip_addr[2]) |
1380 			      FIELD_PREP(IRDMAQPC_DESTIPADDR3, tcp->dest_ip_addr[3]));
1381 		set_64bit_val(qp_ctx, 40,
1382 			      FIELD_PREP(IRDMAQPC_DESTIPADDR0, tcp->dest_ip_addr[0]) |
1383 			      FIELD_PREP(IRDMAQPC_DESTIPADDR1, tcp->dest_ip_addr[1]));
1384 		set_64bit_val(qp_ctx, 48,
1385 			      FIELD_PREP(IRDMAQPC_SNDMSS, tcp->snd_mss) |
1386 			      FIELD_PREP(IRDMAQPC_SYN_RST_HANDLING, tcp->syn_rst_handling) |
1387 			      FIELD_PREP(IRDMAQPC_VLANTAG, tcp->vlan_tag) |
1388 			      FIELD_PREP(IRDMAQPC_ARPIDX, tcp->arp_idx));
1389 		qw7 |= FIELD_PREP(IRDMAQPC_FLOWLABEL, tcp->flow_label) |
1390 		       FIELD_PREP(IRDMAQPC_WSCALE, tcp->wscale) |
1391 		       FIELD_PREP(IRDMAQPC_IGNORE_TCP_OPT,
1392 				  tcp->ignore_tcp_opt) |
1393 		       FIELD_PREP(IRDMAQPC_IGNORE_TCP_UNS_OPT,
1394 				  tcp->ignore_tcp_uns_opt) |
1395 		       FIELD_PREP(IRDMAQPC_TCPSTATE, tcp->tcp_state) |
1396 		       FIELD_PREP(IRDMAQPC_RCVSCALE, tcp->rcv_wscale) |
1397 		       FIELD_PREP(IRDMAQPC_SNDSCALE, tcp->snd_wscale);
1398 		set_64bit_val(qp_ctx, 72,
1399 			      FIELD_PREP(IRDMAQPC_TIMESTAMP_RECENT, tcp->time_stamp_recent) |
1400 			      FIELD_PREP(IRDMAQPC_TIMESTAMP_AGE, tcp->time_stamp_age));
1401 		set_64bit_val(qp_ctx, 80,
1402 			      FIELD_PREP(IRDMAQPC_SNDNXT, tcp->snd_nxt) |
1403 			      FIELD_PREP(IRDMAQPC_SNDWND, tcp->snd_wnd));
1404 		set_64bit_val(qp_ctx, 88,
1405 			      FIELD_PREP(IRDMAQPC_RCVNXT, tcp->rcv_nxt) |
1406 			      FIELD_PREP(IRDMAQPC_RCVWND, tcp->rcv_wnd));
1407 		set_64bit_val(qp_ctx, 96,
1408 			      FIELD_PREP(IRDMAQPC_SNDMAX, tcp->snd_max) |
1409 			      FIELD_PREP(IRDMAQPC_SNDUNA, tcp->snd_una));
1410 		set_64bit_val(qp_ctx, 104,
1411 			      FIELD_PREP(IRDMAQPC_SRTT, tcp->srtt) |
1412 			      FIELD_PREP(IRDMAQPC_RTTVAR, tcp->rtt_var));
1413 		set_64bit_val(qp_ctx, 112,
1414 			      FIELD_PREP(IRDMAQPC_SSTHRESH, tcp->ss_thresh) |
1415 			      FIELD_PREP(IRDMAQPC_CWND, tcp->cwnd));
1416 		set_64bit_val(qp_ctx, 120,
1417 			      FIELD_PREP(IRDMAQPC_SNDWL1, tcp->snd_wl1) |
1418 			      FIELD_PREP(IRDMAQPC_SNDWL2, tcp->snd_wl2));
1419 		qw16 |= FIELD_PREP(IRDMAQPC_MAXSNDWND, tcp->max_snd_window) |
1420 			FIELD_PREP(IRDMAQPC_REXMIT_THRESH, tcp->rexmit_thresh);
1421 		set_64bit_val(qp_ctx, 184,
1422 			      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR3, tcp->local_ipaddr[3]) |
1423 			      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR2, tcp->local_ipaddr[2]));
1424 		set_64bit_val(qp_ctx, 192,
1425 			      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR1, tcp->local_ipaddr[1]) |
1426 			      FIELD_PREP(IRDMAQPC_LOCAL_IPADDR0, tcp->local_ipaddr[0]));
1427 		set_64bit_val(qp_ctx, 200,
1428 			      FIELD_PREP(IRDMAQPC_THIGH, iw->t_high) |
1429 			      FIELD_PREP(IRDMAQPC_TLOW, iw->t_low));
1430 		set_64bit_val(qp_ctx, 208,
1431 			      FIELD_PREP(IRDMAQPC_REMENDPOINTIDX, info->rem_endpoint_idx));
1432 	}
1433 
1434 	set_64bit_val(qp_ctx, 0, qw0);
1435 	set_64bit_val(qp_ctx, 24, qw3);
1436 	set_64bit_val(qp_ctx, 56, qw7);
1437 	set_64bit_val(qp_ctx, 128, qw16);
1438 
1439 	print_hex_dump_debug("WQE: QP_HOST CTX", DUMP_PREFIX_OFFSET, 16, 8,
1440 			     qp_ctx, IRDMA_QP_CTX_SIZE, false);
1441 }
1442 
1443 /**
1444  * irdma_sc_alloc_stag - mr stag alloc
1445  * @dev: sc device struct
1446  * @info: stag info
1447  * @scratch: u64 saved to be used during cqp completion
1448  * @post_sq: flag for cqp db to ring
1449  */
1450 static int irdma_sc_alloc_stag(struct irdma_sc_dev *dev,
1451 			       struct irdma_allocate_stag_info *info,
1452 			       u64 scratch, bool post_sq)
1453 {
1454 	__le64 *wqe;
1455 	struct irdma_sc_cqp *cqp;
1456 	u64 hdr;
1457 	enum irdma_page_size page_size;
1458 
1459 	if (!info->total_len && !info->all_memory)
1460 		return -EINVAL;
1461 
1462 	if (info->page_size == 0x40000000)
1463 		page_size = IRDMA_PAGE_SIZE_1G;
1464 	else if (info->page_size == 0x200000)
1465 		page_size = IRDMA_PAGE_SIZE_2M;
1466 	else
1467 		page_size = IRDMA_PAGE_SIZE_4K;
1468 
1469 	cqp = dev->cqp;
1470 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1471 	if (!wqe)
1472 		return -ENOMEM;
1473 
1474 	set_64bit_val(wqe, 8,
1475 		      FLD_LS_64(dev, info->pd_id, IRDMA_CQPSQ_STAG_PDID) |
1476 		      FIELD_PREP(IRDMA_CQPSQ_STAG_STAGLEN, info->total_len));
1477 	set_64bit_val(wqe, 16,
1478 		      FIELD_PREP(IRDMA_CQPSQ_STAG_IDX, info->stag_idx) |
1479 		      FIELD_PREP(IRDMA_CQPSQ_STAG_PDID_HI, info->pd_id >> 18));
1480 	set_64bit_val(wqe, 40,
1481 		      FIELD_PREP(IRDMA_CQPSQ_STAG_HMCFNIDX, info->hmc_fcn_index));
1482 
1483 	if (info->chunk_size)
1484 		set_64bit_val(wqe, 48,
1485 			      FIELD_PREP(IRDMA_CQPSQ_STAG_FIRSTPMPBLIDX, info->first_pm_pbl_idx));
1486 
1487 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_ALLOC_STAG) |
1488 	      FIELD_PREP(IRDMA_CQPSQ_STAG_MR, 1) |
1489 	      FIELD_PREP(IRDMA_CQPSQ_STAG_ARIGHTS, info->access_rights) |
1490 	      FIELD_PREP(IRDMA_CQPSQ_STAG_LPBLSIZE, info->chunk_size) |
1491 	      FIELD_PREP(IRDMA_CQPSQ_STAG_HPAGESIZE, page_size) |
1492 	      FIELD_PREP(IRDMA_CQPSQ_STAG_REMACCENABLED, info->remote_access) |
1493 	      FIELD_PREP(IRDMA_CQPSQ_STAG_USEHMCFNIDX, info->use_hmc_fcn_index) |
1494 	      FIELD_PREP(IRDMA_CQPSQ_STAG_USEPFRID, info->use_pf_rid) |
1495 	      FIELD_PREP(IRDMA_CQPSQ_STAG_REMOTE_ATOMIC_EN,
1496 			 info->remote_atomics_en) |
1497 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
1498 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1499 
1500 	set_64bit_val(wqe, 24, hdr);
1501 
1502 	print_hex_dump_debug("WQE: ALLOC_STAG WQE", DUMP_PREFIX_OFFSET, 16, 8,
1503 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
1504 	if (post_sq)
1505 		irdma_sc_cqp_post_sq(cqp);
1506 
1507 	return 0;
1508 }
1509 
1510 /**
1511  * irdma_sc_mr_reg_non_shared - non-shared mr registration
1512  * @dev: sc device struct
1513  * @info: mr info
1514  * @scratch: u64 saved to be used during cqp completion
1515  * @post_sq: flag for cqp db to ring
1516  */
1517 static int irdma_sc_mr_reg_non_shared(struct irdma_sc_dev *dev,
1518 				      struct irdma_reg_ns_stag_info *info,
1519 				      u64 scratch, bool post_sq)
1520 {
1521 	__le64 *wqe;
1522 	u64 fbo;
1523 	struct irdma_sc_cqp *cqp;
1524 	u64 hdr;
1525 	u32 pble_obj_cnt;
1526 	bool remote_access;
1527 	u8 addr_type;
1528 	enum irdma_page_size page_size;
1529 
1530 	if (!info->total_len && !info->all_memory)
1531 		return -EINVAL;
1532 
1533 	if (info->page_size == 0x40000000)
1534 		page_size = IRDMA_PAGE_SIZE_1G;
1535 	else if (info->page_size == 0x200000)
1536 		page_size = IRDMA_PAGE_SIZE_2M;
1537 	else if (info->page_size == 0x1000)
1538 		page_size = IRDMA_PAGE_SIZE_4K;
1539 	else
1540 		return -EINVAL;
1541 
1542 	if (info->access_rights & (IRDMA_ACCESS_FLAGS_REMOTEREAD_ONLY |
1543 				   IRDMA_ACCESS_FLAGS_REMOTEWRITE_ONLY))
1544 		remote_access = true;
1545 	else
1546 		remote_access = false;
1547 
1548 	pble_obj_cnt = dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
1549 	if (info->chunk_size && info->first_pm_pbl_index >= pble_obj_cnt)
1550 		return -EINVAL;
1551 
1552 	cqp = dev->cqp;
1553 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1554 	if (!wqe)
1555 		return -ENOMEM;
1556 	fbo = info->va & (info->page_size - 1);
1557 
1558 	set_64bit_val(wqe, 0,
1559 		      (info->addr_type == IRDMA_ADDR_TYPE_VA_BASED ?
1560 		      info->va : fbo));
1561 	set_64bit_val(wqe, 8,
1562 		      FIELD_PREP(IRDMA_CQPSQ_STAG_STAGLEN, info->total_len) |
1563 		      FLD_LS_64(dev, info->pd_id, IRDMA_CQPSQ_STAG_PDID));
1564 	set_64bit_val(wqe, 16,
1565 		      FIELD_PREP(IRDMA_CQPSQ_STAG_KEY, info->stag_key) |
1566 		      FIELD_PREP(IRDMA_CQPSQ_STAG_PDID_HI, info->pd_id >> 18) |
1567 		      FIELD_PREP(IRDMA_CQPSQ_STAG_IDX, info->stag_idx));
1568 	if (!info->chunk_size) {
1569 		set_64bit_val(wqe, 32, info->reg_addr_pa);
1570 		set_64bit_val(wqe, 48, 0);
1571 	} else {
1572 		set_64bit_val(wqe, 32, 0);
1573 		set_64bit_val(wqe, 48,
1574 			      FIELD_PREP(IRDMA_CQPSQ_STAG_FIRSTPMPBLIDX, info->first_pm_pbl_index));
1575 	}
1576 	set_64bit_val(wqe, 40, info->hmc_fcn_index);
1577 	set_64bit_val(wqe, 56, 0);
1578 
1579 	addr_type = (info->addr_type == IRDMA_ADDR_TYPE_VA_BASED) ? 1 : 0;
1580 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_REG_MR) |
1581 	      FIELD_PREP(IRDMA_CQPSQ_STAG_MR, 1) |
1582 	      FIELD_PREP(IRDMA_CQPSQ_STAG_LPBLSIZE, info->chunk_size) |
1583 	      FIELD_PREP(IRDMA_CQPSQ_STAG_HPAGESIZE, page_size) |
1584 	      FIELD_PREP(IRDMA_CQPSQ_STAG_ARIGHTS, info->access_rights) |
1585 	      FIELD_PREP(IRDMA_CQPSQ_STAG_REMACCENABLED, remote_access) |
1586 	      FIELD_PREP(IRDMA_CQPSQ_STAG_VABASEDTO, addr_type) |
1587 	      FIELD_PREP(IRDMA_CQPSQ_STAG_USEHMCFNIDX, info->use_hmc_fcn_index) |
1588 	      FIELD_PREP(IRDMA_CQPSQ_STAG_USEPFRID, info->use_pf_rid) |
1589 	      FIELD_PREP(IRDMA_CQPSQ_STAG_REMOTE_ATOMIC_EN,
1590 			 info->remote_atomics_en) |
1591 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
1592 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1593 
1594 	set_64bit_val(wqe, 24, hdr);
1595 
1596 	print_hex_dump_debug("WQE: MR_REG_NS WQE", DUMP_PREFIX_OFFSET, 16, 8,
1597 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
1598 	if (post_sq)
1599 		irdma_sc_cqp_post_sq(cqp);
1600 
1601 	return 0;
1602 }
1603 
1604 /**
1605  * irdma_sc_dealloc_stag - deallocate stag
1606  * @dev: sc device struct
1607  * @info: dealloc stag info
1608  * @scratch: u64 saved to be used during cqp completion
1609  * @post_sq: flag for cqp db to ring
1610  */
1611 static int irdma_sc_dealloc_stag(struct irdma_sc_dev *dev,
1612 				 struct irdma_dealloc_stag_info *info,
1613 				 u64 scratch, bool post_sq)
1614 {
1615 	u64 hdr;
1616 	__le64 *wqe;
1617 	struct irdma_sc_cqp *cqp;
1618 
1619 	cqp = dev->cqp;
1620 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1621 	if (!wqe)
1622 		return -ENOMEM;
1623 
1624 	set_64bit_val(wqe, 8,
1625 		      FLD_LS_64(dev, info->pd_id, IRDMA_CQPSQ_STAG_PDID));
1626 	set_64bit_val(wqe, 16,
1627 		      FIELD_PREP(IRDMA_CQPSQ_STAG_IDX, info->stag_idx) |
1628 		      FIELD_PREP(IRDMA_CQPSQ_STAG_PDID_HI, info->pd_id >> 18));
1629 
1630 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DEALLOC_STAG) |
1631 	      FIELD_PREP(IRDMA_CQPSQ_STAG_MR, info->mr) |
1632 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
1633 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1634 
1635 	set_64bit_val(wqe, 24, hdr);
1636 
1637 	print_hex_dump_debug("WQE: DEALLOC_STAG WQE", DUMP_PREFIX_OFFSET, 16,
1638 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
1639 	if (post_sq)
1640 		irdma_sc_cqp_post_sq(cqp);
1641 
1642 	return 0;
1643 }
1644 
1645 /**
1646  * irdma_sc_mw_alloc - mw allocate
1647  * @dev: sc device struct
1648  * @info: memory window allocation information
1649  * @scratch: u64 saved to be used during cqp completion
1650  * @post_sq: flag for cqp db to ring
1651  */
1652 static int irdma_sc_mw_alloc(struct irdma_sc_dev *dev,
1653 			     struct irdma_mw_alloc_info *info, u64 scratch,
1654 			     bool post_sq)
1655 {
1656 	u64 hdr;
1657 	struct irdma_sc_cqp *cqp;
1658 	__le64 *wqe;
1659 
1660 	cqp = dev->cqp;
1661 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
1662 	if (!wqe)
1663 		return -ENOMEM;
1664 
1665 	set_64bit_val(wqe, 8,
1666 		      FLD_LS_64(dev, info->pd_id, IRDMA_CQPSQ_STAG_PDID));
1667 	set_64bit_val(wqe, 16,
1668 		      FIELD_PREP(IRDMA_CQPSQ_STAG_IDX, info->mw_stag_index) |
1669 		      FIELD_PREP(IRDMA_CQPSQ_STAG_PDID_HI, info->pd_id >> 18));
1670 
1671 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_ALLOC_STAG) |
1672 	      FIELD_PREP(IRDMA_CQPSQ_STAG_MWTYPE, info->mw_wide) |
1673 	      FIELD_PREP(IRDMA_CQPSQ_STAG_MW1_BIND_DONT_VLDT_KEY,
1674 			 info->mw1_bind_dont_vldt_key) |
1675 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
1676 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1677 
1678 	set_64bit_val(wqe, 24, hdr);
1679 
1680 	print_hex_dump_debug("WQE: MW_ALLOC WQE", DUMP_PREFIX_OFFSET, 16, 8,
1681 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
1682 	if (post_sq)
1683 		irdma_sc_cqp_post_sq(cqp);
1684 
1685 	return 0;
1686 }
1687 
1688 /**
1689  * irdma_sc_mr_fast_register - Posts RDMA fast register mr WR to iwarp qp
1690  * @qp: sc qp struct
1691  * @info: fast mr info
1692  * @post_sq: flag for cqp db to ring
1693  */
1694 int irdma_sc_mr_fast_register(struct irdma_sc_qp *qp,
1695 			      struct irdma_fast_reg_stag_info *info,
1696 			      bool post_sq)
1697 {
1698 	u64 temp, hdr;
1699 	__le64 *wqe;
1700 	u32 wqe_idx;
1701 	enum irdma_page_size page_size;
1702 	struct irdma_post_sq_info sq_info = {};
1703 
1704 	if (info->page_size == 0x40000000)
1705 		page_size = IRDMA_PAGE_SIZE_1G;
1706 	else if (info->page_size == 0x200000)
1707 		page_size = IRDMA_PAGE_SIZE_2M;
1708 	else
1709 		page_size = IRDMA_PAGE_SIZE_4K;
1710 
1711 	sq_info.wr_id = info->wr_id;
1712 	sq_info.signaled = info->signaled;
1713 
1714 	wqe = irdma_qp_get_next_send_wqe(&qp->qp_uk, &wqe_idx,
1715 					 IRDMA_QP_WQE_MIN_QUANTA, 0, &sq_info);
1716 	if (!wqe)
1717 		return -ENOMEM;
1718 
1719 	irdma_clr_wqes(&qp->qp_uk, wqe_idx);
1720 
1721 	ibdev_dbg(to_ibdev(qp->dev),
1722 		  "MR: wr_id[%llxh] wqe_idx[%04d] location[%p]\n",
1723 		  info->wr_id, wqe_idx,
1724 		  &qp->qp_uk.sq_wrtrk_array[wqe_idx].wrid);
1725 
1726 	temp = (info->addr_type == IRDMA_ADDR_TYPE_VA_BASED) ?
1727 		(uintptr_t)info->va : info->fbo;
1728 	set_64bit_val(wqe, 0, temp);
1729 
1730 	temp = FIELD_GET(IRDMAQPSQ_FIRSTPMPBLIDXHI,
1731 			 info->first_pm_pbl_index >> 16);
1732 	set_64bit_val(wqe, 8,
1733 		      FIELD_PREP(IRDMAQPSQ_FIRSTPMPBLIDXHI, temp) |
1734 		      FIELD_PREP(IRDMAQPSQ_PBLADDR >> IRDMA_HW_PAGE_SHIFT, info->reg_addr_pa));
1735 	set_64bit_val(wqe, 16,
1736 		      info->total_len |
1737 		      FIELD_PREP(IRDMAQPSQ_FIRSTPMPBLIDXLO, info->first_pm_pbl_index));
1738 
1739 	hdr = FIELD_PREP(IRDMAQPSQ_STAGKEY, info->stag_key) |
1740 	      FIELD_PREP(IRDMAQPSQ_STAGINDEX, info->stag_idx) |
1741 	      FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_FAST_REGISTER) |
1742 	      FIELD_PREP(IRDMAQPSQ_LPBLSIZE, info->chunk_size) |
1743 	      FIELD_PREP(IRDMAQPSQ_HPAGESIZE, page_size) |
1744 	      FIELD_PREP(IRDMAQPSQ_STAGRIGHTS, info->access_rights) |
1745 	      FIELD_PREP(IRDMAQPSQ_VABASEDTO, info->addr_type) |
1746 	      FIELD_PREP(IRDMAQPSQ_READFENCE, info->read_fence) |
1747 	      FIELD_PREP(IRDMAQPSQ_LOCALFENCE, info->local_fence) |
1748 	      FIELD_PREP(IRDMAQPSQ_SIGCOMPL, info->signaled) |
1749 	      FIELD_PREP(IRDMAQPSQ_REMOTE_ATOMICS_EN, info->remote_atomics_en) |
1750 	      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity);
1751 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1752 
1753 	set_64bit_val(wqe, 24, hdr);
1754 
1755 	print_hex_dump_debug("WQE: FAST_REG WQE", DUMP_PREFIX_OFFSET, 16, 8,
1756 			     wqe, IRDMA_QP_WQE_MIN_SIZE, false);
1757 
1758 	if (post_sq)
1759 		irdma_uk_qp_post_wr(&qp->qp_uk);
1760 
1761 	return 0;
1762 }
1763 
1764 /**
1765  * irdma_sc_gen_rts_ae - request AE generated after RTS
1766  * @qp: sc qp struct
1767  */
1768 static void irdma_sc_gen_rts_ae(struct irdma_sc_qp *qp)
1769 {
1770 	__le64 *wqe;
1771 	u64 hdr;
1772 	struct irdma_qp_uk *qp_uk;
1773 
1774 	qp_uk = &qp->qp_uk;
1775 
1776 	wqe = qp_uk->sq_base[1].elem;
1777 
1778 	hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_NOP) |
1779 	      FIELD_PREP(IRDMAQPSQ_LOCALFENCE, 1) |
1780 	      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity);
1781 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1782 
1783 	set_64bit_val(wqe, 24, hdr);
1784 	print_hex_dump_debug("QP: NOP W/LOCAL FENCE WQE", DUMP_PREFIX_OFFSET,
1785 			     16, 8, wqe, IRDMA_QP_WQE_MIN_SIZE, false);
1786 
1787 	wqe = qp_uk->sq_base[2].elem;
1788 	hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_GEN_RTS_AE) |
1789 	      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity);
1790 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1791 
1792 	set_64bit_val(wqe, 24, hdr);
1793 	print_hex_dump_debug("QP: CONN EST WQE", DUMP_PREFIX_OFFSET, 16, 8,
1794 			     wqe, IRDMA_QP_WQE_MIN_SIZE, false);
1795 }
1796 
1797 /**
1798  * irdma_sc_send_lsmm - send last streaming mode message
1799  * @qp: sc qp struct
1800  * @lsmm_buf: buffer with lsmm message
1801  * @size: size of lsmm buffer
1802  * @stag: stag of lsmm buffer
1803  */
1804 void irdma_sc_send_lsmm(struct irdma_sc_qp *qp, void *lsmm_buf, u32 size,
1805 			irdma_stag stag)
1806 {
1807 	__le64 *wqe;
1808 	u64 hdr;
1809 	struct irdma_qp_uk *qp_uk;
1810 
1811 	qp_uk = &qp->qp_uk;
1812 	wqe = qp_uk->sq_base->elem;
1813 
1814 	set_64bit_val(wqe, 0, (uintptr_t)lsmm_buf);
1815 	if (qp->qp_uk.uk_attrs->hw_rev == IRDMA_GEN_1) {
1816 		set_64bit_val(wqe, 8,
1817 			      FIELD_PREP(IRDMAQPSQ_GEN1_FRAG_LEN, size) |
1818 			      FIELD_PREP(IRDMAQPSQ_GEN1_FRAG_STAG, stag));
1819 	} else {
1820 		set_64bit_val(wqe, 8,
1821 			      FIELD_PREP(IRDMAQPSQ_FRAG_LEN, size) |
1822 			      FIELD_PREP(IRDMAQPSQ_FRAG_STAG, stag) |
1823 			      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity));
1824 	}
1825 	set_64bit_val(wqe, 16, 0);
1826 
1827 	hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_RDMA_SEND) |
1828 	      FIELD_PREP(IRDMAQPSQ_STREAMMODE, 1) |
1829 	      FIELD_PREP(IRDMAQPSQ_WAITFORRCVPDU, 1) |
1830 	      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity);
1831 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1832 
1833 	set_64bit_val(wqe, 24, hdr);
1834 
1835 	print_hex_dump_debug("WQE: SEND_LSMM WQE", DUMP_PREFIX_OFFSET, 16, 8,
1836 			     wqe, IRDMA_QP_WQE_MIN_SIZE, false);
1837 
1838 	if (qp->dev->hw_attrs.uk_attrs.feature_flags & IRDMA_FEATURE_RTS_AE)
1839 		irdma_sc_gen_rts_ae(qp);
1840 }
1841 
1842 /**
1843  * irdma_sc_send_rtt - send last read0 or write0
1844  * @qp: sc qp struct
1845  * @read: Do read0 or write0
1846  */
1847 void irdma_sc_send_rtt(struct irdma_sc_qp *qp, bool read)
1848 {
1849 	__le64 *wqe;
1850 	u64 hdr;
1851 	struct irdma_qp_uk *qp_uk;
1852 
1853 	qp_uk = &qp->qp_uk;
1854 	wqe = qp_uk->sq_base->elem;
1855 
1856 	set_64bit_val(wqe, 0, 0);
1857 	set_64bit_val(wqe, 16, 0);
1858 	if (read) {
1859 		if (qp->qp_uk.uk_attrs->hw_rev == IRDMA_GEN_1) {
1860 			set_64bit_val(wqe, 8,
1861 				      FIELD_PREP(IRDMAQPSQ_GEN1_FRAG_STAG, 0xabcd));
1862 		} else {
1863 			set_64bit_val(wqe, 8,
1864 				      (u64)0xabcd | FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity));
1865 		}
1866 		hdr = FIELD_PREP(IRDMAQPSQ_REMSTAG, 0x1234) |
1867 		      FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_RDMA_READ) |
1868 		      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity);
1869 
1870 	} else {
1871 		if (qp->qp_uk.uk_attrs->hw_rev == IRDMA_GEN_1) {
1872 			set_64bit_val(wqe, 8, 0);
1873 		} else {
1874 			set_64bit_val(wqe, 8,
1875 				      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity));
1876 		}
1877 		hdr = FIELD_PREP(IRDMAQPSQ_OPCODE, IRDMAQP_OP_RDMA_WRITE) |
1878 		      FIELD_PREP(IRDMAQPSQ_VALID, qp->qp_uk.swqe_polarity);
1879 	}
1880 
1881 	dma_wmb(); /* make sure WQE is written before valid bit is set */
1882 
1883 	set_64bit_val(wqe, 24, hdr);
1884 
1885 	print_hex_dump_debug("WQE: RTR WQE", DUMP_PREFIX_OFFSET, 16, 8, wqe,
1886 			     IRDMA_QP_WQE_MIN_SIZE, false);
1887 
1888 	if (qp->dev->hw_attrs.uk_attrs.feature_flags & IRDMA_FEATURE_RTS_AE)
1889 		irdma_sc_gen_rts_ae(qp);
1890 }
1891 
1892 /**
1893  * irdma_iwarp_opcode - determine if incoming is rdma layer
1894  * @info: aeq info for the packet
1895  * @pkt: packet for error
1896  */
1897 static u32 irdma_iwarp_opcode(struct irdma_aeqe_info *info, u8 *pkt)
1898 {
1899 	__be16 *mpa;
1900 	u32 opcode = 0xffffffff;
1901 
1902 	if (info->q2_data_written) {
1903 		mpa = (__be16 *)pkt;
1904 		opcode = ntohs(mpa[1]) & 0xf;
1905 	}
1906 
1907 	return opcode;
1908 }
1909 
1910 /**
1911  * irdma_locate_mpa - return pointer to mpa in the pkt
1912  * @pkt: packet with data
1913  */
1914 static u8 *irdma_locate_mpa(u8 *pkt)
1915 {
1916 	/* skip over ethernet header */
1917 	pkt += IRDMA_MAC_HLEN;
1918 
1919 	/* Skip over IP and TCP headers */
1920 	pkt += 4 * (pkt[0] & 0x0f);
1921 	pkt += 4 * ((pkt[12] >> 4) & 0x0f);
1922 
1923 	return pkt;
1924 }
1925 
1926 /**
1927  * irdma_bld_termhdr_ctrl - setup terminate hdr control fields
1928  * @qp: sc qp ptr for pkt
1929  * @hdr: term hdr
1930  * @opcode: flush opcode for termhdr
1931  * @layer_etype: error layer + error type
1932  * @err: error cod ein the header
1933  */
1934 static void irdma_bld_termhdr_ctrl(struct irdma_sc_qp *qp,
1935 				   struct irdma_terminate_hdr *hdr,
1936 				   enum irdma_flush_opcode opcode,
1937 				   u8 layer_etype, u8 err)
1938 {
1939 	qp->flush_code = opcode;
1940 	hdr->layer_etype = layer_etype;
1941 	hdr->error_code = err;
1942 }
1943 
1944 /**
1945  * irdma_bld_termhdr_ddp_rdma - setup ddp and rdma hdrs in terminate hdr
1946  * @pkt: ptr to mpa in offending pkt
1947  * @hdr: term hdr
1948  * @copy_len: offending pkt length to be copied to term hdr
1949  * @is_tagged: DDP tagged or untagged
1950  */
1951 static void irdma_bld_termhdr_ddp_rdma(u8 *pkt, struct irdma_terminate_hdr *hdr,
1952 				       int *copy_len, u8 *is_tagged)
1953 {
1954 	u16 ddp_seg_len;
1955 
1956 	ddp_seg_len = ntohs(*(__be16 *)pkt);
1957 	if (ddp_seg_len) {
1958 		*copy_len = 2;
1959 		hdr->hdrct = DDP_LEN_FLAG;
1960 		if (pkt[2] & 0x80) {
1961 			*is_tagged = 1;
1962 			if (ddp_seg_len >= TERM_DDP_LEN_TAGGED) {
1963 				*copy_len += TERM_DDP_LEN_TAGGED;
1964 				hdr->hdrct |= DDP_HDR_FLAG;
1965 			}
1966 		} else {
1967 			if (ddp_seg_len >= TERM_DDP_LEN_UNTAGGED) {
1968 				*copy_len += TERM_DDP_LEN_UNTAGGED;
1969 				hdr->hdrct |= DDP_HDR_FLAG;
1970 			}
1971 			if (ddp_seg_len >= (TERM_DDP_LEN_UNTAGGED + TERM_RDMA_LEN) &&
1972 			    ((pkt[3] & RDMA_OPCODE_M) == RDMA_READ_REQ_OPCODE)) {
1973 				*copy_len += TERM_RDMA_LEN;
1974 				hdr->hdrct |= RDMA_HDR_FLAG;
1975 			}
1976 		}
1977 	}
1978 }
1979 
1980 /**
1981  * irdma_bld_terminate_hdr - build terminate message header
1982  * @qp: qp associated with received terminate AE
1983  * @info: the struct contiaing AE information
1984  */
1985 static int irdma_bld_terminate_hdr(struct irdma_sc_qp *qp,
1986 				   struct irdma_aeqe_info *info)
1987 {
1988 	u8 *pkt = qp->q2_buf + Q2_BAD_FRAME_OFFSET;
1989 	int copy_len = 0;
1990 	u8 is_tagged = 0;
1991 	u32 opcode;
1992 	struct irdma_terminate_hdr *termhdr;
1993 
1994 	termhdr = (struct irdma_terminate_hdr *)qp->q2_buf;
1995 	memset(termhdr, 0, Q2_BAD_FRAME_OFFSET);
1996 
1997 	if (info->q2_data_written) {
1998 		pkt = irdma_locate_mpa(pkt);
1999 		irdma_bld_termhdr_ddp_rdma(pkt, termhdr, &copy_len, &is_tagged);
2000 	}
2001 
2002 	opcode = irdma_iwarp_opcode(info, pkt);
2003 	qp->event_type = IRDMA_QP_EVENT_CATASTROPHIC;
2004 	qp->sq_flush_code = info->sq;
2005 	qp->rq_flush_code = info->rq;
2006 
2007 	switch (info->ae_id) {
2008 	case IRDMA_AE_AMP_UNALLOCATED_STAG:
2009 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2010 		if (opcode == IRDMA_OP_TYPE_RDMA_WRITE)
2011 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_PROT_ERR,
2012 					       (LAYER_DDP << 4) | DDP_TAGGED_BUF,
2013 					       DDP_TAGGED_INV_STAG);
2014 		else
2015 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2016 					       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2017 					       RDMAP_INV_STAG);
2018 		break;
2019 	case IRDMA_AE_AMP_BOUNDS_VIOLATION:
2020 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2021 		if (info->q2_data_written)
2022 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_PROT_ERR,
2023 					       (LAYER_DDP << 4) | DDP_TAGGED_BUF,
2024 					       DDP_TAGGED_BOUNDS);
2025 		else
2026 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2027 					       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2028 					       RDMAP_INV_BOUNDS);
2029 		break;
2030 	case IRDMA_AE_AMP_BAD_PD:
2031 		switch (opcode) {
2032 		case IRDMA_OP_TYPE_RDMA_WRITE:
2033 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_PROT_ERR,
2034 					       (LAYER_DDP << 4) | DDP_TAGGED_BUF,
2035 					       DDP_TAGGED_UNASSOC_STAG);
2036 			break;
2037 		case IRDMA_OP_TYPE_SEND_INV:
2038 		case IRDMA_OP_TYPE_SEND_SOL_INV:
2039 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2040 					       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2041 					       RDMAP_CANT_INV_STAG);
2042 			break;
2043 		default:
2044 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2045 					       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2046 					       RDMAP_UNASSOC_STAG);
2047 		}
2048 		break;
2049 	case IRDMA_AE_AMP_INVALID_STAG:
2050 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2051 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2052 				       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2053 				       RDMAP_INV_STAG);
2054 		break;
2055 	case IRDMA_AE_AMP_BAD_QP:
2056 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_LOC_QP_OP_ERR,
2057 				       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2058 				       DDP_UNTAGGED_INV_QN);
2059 		break;
2060 	case IRDMA_AE_AMP_BAD_STAG_KEY:
2061 	case IRDMA_AE_AMP_BAD_STAG_INDEX:
2062 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2063 		switch (opcode) {
2064 		case IRDMA_OP_TYPE_SEND_INV:
2065 		case IRDMA_OP_TYPE_SEND_SOL_INV:
2066 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_OP_ERR,
2067 					       (LAYER_RDMA << 4) | RDMAP_REMOTE_OP,
2068 					       RDMAP_CANT_INV_STAG);
2069 			break;
2070 		default:
2071 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2072 					       (LAYER_RDMA << 4) | RDMAP_REMOTE_OP,
2073 					       RDMAP_INV_STAG);
2074 		}
2075 		break;
2076 	case IRDMA_AE_AMP_RIGHTS_VIOLATION:
2077 	case IRDMA_AE_AMP_INVALIDATE_NO_REMOTE_ACCESS_RIGHTS:
2078 	case IRDMA_AE_PRIV_OPERATION_DENIED:
2079 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2080 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2081 				       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2082 				       RDMAP_ACCESS);
2083 		break;
2084 	case IRDMA_AE_AMP_TO_WRAP:
2085 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2086 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_ACCESS_ERR,
2087 				       (LAYER_RDMA << 4) | RDMAP_REMOTE_PROT,
2088 				       RDMAP_TO_WRAP);
2089 		break;
2090 	case IRDMA_AE_LLP_RECEIVED_MPA_CRC_ERROR:
2091 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2092 				       (LAYER_MPA << 4) | DDP_LLP, MPA_CRC);
2093 		break;
2094 	case IRDMA_AE_LLP_SEGMENT_TOO_SMALL:
2095 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_LOC_LEN_ERR,
2096 				       (LAYER_DDP << 4) | DDP_CATASTROPHIC,
2097 				       DDP_CATASTROPHIC_LOCAL);
2098 		break;
2099 	case IRDMA_AE_LCE_QP_CATASTROPHIC:
2100 	case IRDMA_AE_DDP_NO_L_BIT:
2101 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_FATAL_ERR,
2102 				       (LAYER_DDP << 4) | DDP_CATASTROPHIC,
2103 				       DDP_CATASTROPHIC_LOCAL);
2104 		break;
2105 	case IRDMA_AE_DDP_INVALID_MSN_GAP_IN_MSN:
2106 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2107 				       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2108 				       DDP_UNTAGGED_INV_MSN_RANGE);
2109 		break;
2110 	case IRDMA_AE_DDP_UBE_DDP_MESSAGE_TOO_LONG_FOR_AVAILABLE_BUFFER:
2111 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
2112 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_LOC_LEN_ERR,
2113 				       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2114 				       DDP_UNTAGGED_INV_TOO_LONG);
2115 		break;
2116 	case IRDMA_AE_DDP_UBE_INVALID_DDP_VERSION:
2117 		if (is_tagged)
2118 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2119 					       (LAYER_DDP << 4) | DDP_TAGGED_BUF,
2120 					       DDP_TAGGED_INV_DDP_VER);
2121 		else
2122 			irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2123 					       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2124 					       DDP_UNTAGGED_INV_DDP_VER);
2125 		break;
2126 	case IRDMA_AE_DDP_UBE_INVALID_MO:
2127 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2128 				       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2129 				       DDP_UNTAGGED_INV_MO);
2130 		break;
2131 	case IRDMA_AE_DDP_UBE_INVALID_MSN_NO_BUFFER_AVAILABLE:
2132 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_REM_OP_ERR,
2133 				       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2134 				       DDP_UNTAGGED_INV_MSN_NO_BUF);
2135 		break;
2136 	case IRDMA_AE_DDP_UBE_INVALID_QN:
2137 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2138 				       (LAYER_DDP << 4) | DDP_UNTAGGED_BUF,
2139 				       DDP_UNTAGGED_INV_QN);
2140 		break;
2141 	case IRDMA_AE_RDMAP_ROE_INVALID_RDMAP_VERSION:
2142 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_GENERAL_ERR,
2143 				       (LAYER_RDMA << 4) | RDMAP_REMOTE_OP,
2144 				       RDMAP_INV_RDMAP_VER);
2145 		break;
2146 	default:
2147 		irdma_bld_termhdr_ctrl(qp, termhdr, FLUSH_FATAL_ERR,
2148 				       (LAYER_RDMA << 4) | RDMAP_REMOTE_OP,
2149 				       RDMAP_UNSPECIFIED);
2150 		break;
2151 	}
2152 
2153 	if (copy_len)
2154 		memcpy(termhdr + 1, pkt, copy_len);
2155 
2156 	return sizeof(struct irdma_terminate_hdr) + copy_len;
2157 }
2158 
2159 /**
2160  * irdma_terminate_send_fin() - Send fin for terminate message
2161  * @qp: qp associated with received terminate AE
2162  */
2163 void irdma_terminate_send_fin(struct irdma_sc_qp *qp)
2164 {
2165 	irdma_term_modify_qp(qp, IRDMA_QP_STATE_TERMINATE,
2166 			     IRDMAQP_TERM_SEND_FIN_ONLY, 0);
2167 }
2168 
2169 /**
2170  * irdma_terminate_connection() - Bad AE and send terminate to remote QP
2171  * @qp: qp associated with received terminate AE
2172  * @info: the struct contiaing AE information
2173  */
2174 void irdma_terminate_connection(struct irdma_sc_qp *qp,
2175 				struct irdma_aeqe_info *info)
2176 {
2177 	u8 termlen = 0;
2178 
2179 	if (qp->term_flags & IRDMA_TERM_SENT)
2180 		return;
2181 
2182 	termlen = irdma_bld_terminate_hdr(qp, info);
2183 	irdma_terminate_start_timer(qp);
2184 	qp->term_flags |= IRDMA_TERM_SENT;
2185 	irdma_term_modify_qp(qp, IRDMA_QP_STATE_TERMINATE,
2186 			     IRDMAQP_TERM_SEND_TERM_ONLY, termlen);
2187 }
2188 
2189 /**
2190  * irdma_terminate_received - handle terminate received AE
2191  * @qp: qp associated with received terminate AE
2192  * @info: the struct contiaing AE information
2193  */
2194 void irdma_terminate_received(struct irdma_sc_qp *qp,
2195 			      struct irdma_aeqe_info *info)
2196 {
2197 	u8 *pkt = qp->q2_buf + Q2_BAD_FRAME_OFFSET;
2198 	__be32 *mpa;
2199 	u8 ddp_ctl;
2200 	u8 rdma_ctl;
2201 	u16 aeq_id = 0;
2202 	struct irdma_terminate_hdr *termhdr;
2203 
2204 	mpa = (__be32 *)irdma_locate_mpa(pkt);
2205 	if (info->q2_data_written) {
2206 		/* did not validate the frame - do it now */
2207 		ddp_ctl = (ntohl(mpa[0]) >> 8) & 0xff;
2208 		rdma_ctl = ntohl(mpa[0]) & 0xff;
2209 		if ((ddp_ctl & 0xc0) != 0x40)
2210 			aeq_id = IRDMA_AE_LCE_QP_CATASTROPHIC;
2211 		else if ((ddp_ctl & 0x03) != 1)
2212 			aeq_id = IRDMA_AE_DDP_UBE_INVALID_DDP_VERSION;
2213 		else if (ntohl(mpa[2]) != 2)
2214 			aeq_id = IRDMA_AE_DDP_UBE_INVALID_QN;
2215 		else if (ntohl(mpa[3]) != 1)
2216 			aeq_id = IRDMA_AE_DDP_INVALID_MSN_GAP_IN_MSN;
2217 		else if (ntohl(mpa[4]) != 0)
2218 			aeq_id = IRDMA_AE_DDP_UBE_INVALID_MO;
2219 		else if ((rdma_ctl & 0xc0) != 0x40)
2220 			aeq_id = IRDMA_AE_RDMAP_ROE_INVALID_RDMAP_VERSION;
2221 
2222 		info->ae_id = aeq_id;
2223 		if (info->ae_id) {
2224 			/* Bad terminate recvd - send back a terminate */
2225 			irdma_terminate_connection(qp, info);
2226 			return;
2227 		}
2228 	}
2229 
2230 	qp->term_flags |= IRDMA_TERM_RCVD;
2231 	qp->event_type = IRDMA_QP_EVENT_CATASTROPHIC;
2232 	termhdr = (struct irdma_terminate_hdr *)&mpa[5];
2233 	if (termhdr->layer_etype == RDMAP_REMOTE_PROT ||
2234 	    termhdr->layer_etype == RDMAP_REMOTE_OP) {
2235 		irdma_terminate_done(qp, 0);
2236 	} else {
2237 		irdma_terminate_start_timer(qp);
2238 		irdma_terminate_send_fin(qp);
2239 	}
2240 }
2241 
2242 static int irdma_null_ws_add(struct irdma_sc_vsi *vsi, u8 user_pri)
2243 {
2244 	return 0;
2245 }
2246 
2247 static void irdma_null_ws_remove(struct irdma_sc_vsi *vsi, u8 user_pri)
2248 {
2249 	/* do nothing */
2250 }
2251 
2252 static void irdma_null_ws_reset(struct irdma_sc_vsi *vsi)
2253 {
2254 	/* do nothing */
2255 }
2256 
2257 /**
2258  * irdma_sc_vsi_init - Init the vsi structure
2259  * @vsi: pointer to vsi structure to initialize
2260  * @info: the info used to initialize the vsi struct
2261  */
2262 void irdma_sc_vsi_init(struct irdma_sc_vsi  *vsi,
2263 		       struct irdma_vsi_init_info *info)
2264 {
2265 	int i;
2266 
2267 	vsi->dev = info->dev;
2268 	vsi->back_vsi = info->back_vsi;
2269 	vsi->register_qset = info->register_qset;
2270 	vsi->unregister_qset = info->unregister_qset;
2271 	vsi->mtu = info->params->mtu;
2272 	vsi->exception_lan_q = info->exception_lan_q;
2273 	vsi->vsi_idx = info->pf_data_vsi_num;
2274 
2275 	irdma_set_qos_info(vsi, info->params);
2276 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++) {
2277 		mutex_init(&vsi->qos[i].qos_mutex);
2278 		INIT_LIST_HEAD(&vsi->qos[i].qplist);
2279 	}
2280 	if (vsi->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_2) {
2281 		vsi->dev->ws_add = irdma_ws_add;
2282 		vsi->dev->ws_remove = irdma_ws_remove;
2283 		vsi->dev->ws_reset = irdma_ws_reset;
2284 	} else {
2285 		vsi->dev->ws_add = irdma_null_ws_add;
2286 		vsi->dev->ws_remove = irdma_null_ws_remove;
2287 		vsi->dev->ws_reset = irdma_null_ws_reset;
2288 	}
2289 }
2290 
2291 /**
2292  * irdma_get_stats_idx - Return stats index
2293  * @vsi: pointer to the vsi
2294  */
2295 static u16 irdma_get_stats_idx(struct irdma_sc_vsi *vsi)
2296 {
2297 	struct irdma_stats_inst_info stats_info = {};
2298 	struct irdma_sc_dev *dev = vsi->dev;
2299 	u8 i;
2300 
2301 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_2) {
2302 		if (!irdma_cqp_stats_inst_cmd(vsi, IRDMA_OP_STATS_ALLOCATE,
2303 					      &stats_info))
2304 			return stats_info.stats_idx;
2305 	}
2306 
2307 	for (i = 0; i < IRDMA_MAX_STATS_COUNT_GEN_1; i++) {
2308 		if (!dev->stats_idx_array[i]) {
2309 			dev->stats_idx_array[i] = true;
2310 			return i;
2311 		}
2312 	}
2313 
2314 	return IRDMA_INVALID_STATS_IDX;
2315 }
2316 
2317 /**
2318  * irdma_hw_stats_init_gen1 - Initialize stat reg table used for gen1
2319  * @vsi: vsi structure where hw_regs are set
2320  *
2321  * Populate the HW stats table
2322  */
2323 static void irdma_hw_stats_init_gen1(struct irdma_sc_vsi *vsi)
2324 {
2325 	struct irdma_sc_dev *dev = vsi->dev;
2326 	const struct irdma_hw_stat_map *map;
2327 	u64 *stat_reg = vsi->hw_stats_regs;
2328 	u64 *regs = dev->hw_stats_regs;
2329 	u16 i, stats_reg_set = vsi->stats_idx;
2330 
2331 	map = dev->hw_stats_map;
2332 
2333 	/* First 4 stat instances are reserved for port level statistics. */
2334 	stats_reg_set += vsi->stats_inst_alloc ? IRDMA_FIRST_NON_PF_STAT : 0;
2335 
2336 	for (i = 0; i < dev->hw_attrs.max_stat_idx; i++) {
2337 		if (map[i].bitmask <= IRDMA_MAX_STATS_32)
2338 			stat_reg[i] = regs[i] + stats_reg_set * sizeof(u32);
2339 		else
2340 			stat_reg[i] = regs[i] + stats_reg_set * sizeof(u64);
2341 	}
2342 }
2343 
2344 /**
2345  * irdma_vsi_stats_init - Initialize the vsi statistics
2346  * @vsi: pointer to the vsi structure
2347  * @info: The info structure used for initialization
2348  */
2349 int irdma_vsi_stats_init(struct irdma_sc_vsi *vsi,
2350 			 struct irdma_vsi_stats_info *info)
2351 {
2352 	struct irdma_dma_mem *stats_buff_mem;
2353 
2354 	vsi->pestat = info->pestat;
2355 	vsi->pestat->hw = vsi->dev->hw;
2356 	vsi->pestat->vsi = vsi;
2357 	stats_buff_mem = &vsi->pestat->gather_info.stats_buff_mem;
2358 	stats_buff_mem->size = ALIGN(IRDMA_GATHER_STATS_BUF_SIZE * 2, 1);
2359 	stats_buff_mem->va = dma_alloc_coherent(vsi->pestat->hw->device,
2360 						stats_buff_mem->size,
2361 						&stats_buff_mem->pa,
2362 						GFP_KERNEL);
2363 	if (!stats_buff_mem->va)
2364 		return -ENOMEM;
2365 
2366 	vsi->pestat->gather_info.gather_stats_va = stats_buff_mem->va;
2367 	vsi->pestat->gather_info.last_gather_stats_va =
2368 		(void *)((uintptr_t)stats_buff_mem->va +
2369 			 IRDMA_GATHER_STATS_BUF_SIZE);
2370 
2371 	if (vsi->dev->hw_attrs.uk_attrs.hw_rev < IRDMA_GEN_3)
2372 		irdma_hw_stats_start_timer(vsi);
2373 
2374 	/* when stat allocation is not required default to fcn_id. */
2375 	vsi->stats_idx = info->fcn_id;
2376 	if (info->alloc_stats_inst) {
2377 		u16 stats_idx = irdma_get_stats_idx(vsi);
2378 
2379 		if (stats_idx != IRDMA_INVALID_STATS_IDX) {
2380 			vsi->stats_inst_alloc = true;
2381 			vsi->stats_idx = stats_idx;
2382 			vsi->pestat->gather_info.use_stats_inst = true;
2383 			vsi->pestat->gather_info.stats_inst_index = stats_idx;
2384 		}
2385 	}
2386 
2387 	if (vsi->dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
2388 		irdma_hw_stats_init_gen1(vsi);
2389 
2390 	return 0;
2391 }
2392 
2393 /**
2394  * irdma_vsi_stats_free - Free the vsi stats
2395  * @vsi: pointer to the vsi structure
2396  */
2397 void irdma_vsi_stats_free(struct irdma_sc_vsi *vsi)
2398 {
2399 	struct irdma_stats_inst_info stats_info = {};
2400 	struct irdma_sc_dev *dev = vsi->dev;
2401 	u16 stats_idx = vsi->stats_idx;
2402 
2403 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_2) {
2404 		if (vsi->stats_inst_alloc) {
2405 			stats_info.stats_idx = vsi->stats_idx;
2406 			irdma_cqp_stats_inst_cmd(vsi, IRDMA_OP_STATS_FREE,
2407 						 &stats_info);
2408 		}
2409 	} else {
2410 		if (vsi->stats_inst_alloc &&
2411 		    stats_idx < vsi->dev->hw_attrs.max_stat_inst)
2412 			vsi->dev->stats_idx_array[stats_idx] = false;
2413 	}
2414 
2415 	if (!vsi->pestat)
2416 		return;
2417 
2418 	if (dev->hw_attrs.uk_attrs.hw_rev < IRDMA_GEN_3)
2419 		irdma_hw_stats_stop_timer(vsi);
2420 	dma_free_coherent(vsi->pestat->hw->device,
2421 			  vsi->pestat->gather_info.stats_buff_mem.size,
2422 			  vsi->pestat->gather_info.stats_buff_mem.va,
2423 			  vsi->pestat->gather_info.stats_buff_mem.pa);
2424 	vsi->pestat->gather_info.stats_buff_mem.va = NULL;
2425 }
2426 
2427 /**
2428  * irdma_get_encoded_wqe_size - given wq size, returns hardware encoded size
2429  * @wqsize: size of the wq (sq, rq) to encoded_size
2430  * @queue_type: queue type selected for the calculation algorithm
2431  */
2432 u8 irdma_get_encoded_wqe_size(u32 wqsize, enum irdma_queue_type queue_type)
2433 {
2434 	u8 encoded_size = 0;
2435 
2436 	if (queue_type == IRDMA_QUEUE_TYPE_SRQ) {
2437 		/* Smallest SRQ size is 256B (8 quanta) that gets
2438 		 * encoded to 0.
2439 		 */
2440 		encoded_size = ilog2(wqsize) - 3;
2441 
2442 		return encoded_size;
2443 	}
2444 	/* cqp sq's hw coded value starts from 1 for size of 4
2445 	 * while it starts from 0 for qp' wq's.
2446 	 */
2447 	if (queue_type == IRDMA_QUEUE_TYPE_CQP)
2448 		encoded_size = 1;
2449 	wqsize >>= 2;
2450 	while (wqsize >>= 1)
2451 		encoded_size++;
2452 
2453 	return encoded_size;
2454 }
2455 
2456 /**
2457  * irdma_sc_gather_stats - collect the statistics
2458  * @cqp: struct for cqp hw
2459  * @info: gather stats info structure
2460  * @scratch: u64 saved to be used during cqp completion
2461  */
2462 static int irdma_sc_gather_stats(struct irdma_sc_cqp *cqp,
2463 				 struct irdma_stats_gather_info *info,
2464 				 u64 scratch)
2465 {
2466 	__le64 *wqe;
2467 	u64 temp;
2468 
2469 	if (info->stats_buff_mem.size < IRDMA_GATHER_STATS_BUF_SIZE)
2470 		return -ENOMEM;
2471 
2472 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2473 	if (!wqe)
2474 		return -ENOMEM;
2475 
2476 	set_64bit_val(wqe, 40,
2477 		      FIELD_PREP(IRDMA_CQPSQ_STATS_HMC_FCN_INDEX, info->hmc_fcn_index));
2478 	set_64bit_val(wqe, 32, info->stats_buff_mem.pa);
2479 
2480 	temp = FIELD_PREP(IRDMA_CQPSQ_STATS_WQEVALID, cqp->polarity) |
2481 	       FIELD_PREP(IRDMA_CQPSQ_STATS_USE_INST, info->use_stats_inst) |
2482 	       FIELD_PREP(IRDMA_CQPSQ_STATS_INST_INDEX,
2483 			  info->stats_inst_index) |
2484 	       FIELD_PREP(IRDMA_CQPSQ_STATS_USE_HMC_FCN_INDEX,
2485 			  info->use_hmc_fcn_index) |
2486 	       FIELD_PREP(IRDMA_CQPSQ_STATS_OP, IRDMA_CQP_OP_GATHER_STATS);
2487 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2488 
2489 	set_64bit_val(wqe, 24, temp);
2490 
2491 	print_hex_dump_debug("STATS: GATHER_STATS WQE", DUMP_PREFIX_OFFSET,
2492 			     16, 8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2493 
2494 	irdma_sc_cqp_post_sq(cqp);
2495 	ibdev_dbg(to_ibdev(cqp->dev),
2496 		  "STATS: CQP SQ head 0x%x tail 0x%x size 0x%x\n",
2497 		  cqp->sq_ring.head, cqp->sq_ring.tail, cqp->sq_ring.size);
2498 
2499 	return 0;
2500 }
2501 
2502 /**
2503  * irdma_sc_manage_stats_inst - allocate or free stats instance
2504  * @cqp: struct for cqp hw
2505  * @info: stats info structure
2506  * @alloc: alloc vs. delete flag
2507  * @scratch: u64 saved to be used during cqp completion
2508  */
2509 static int irdma_sc_manage_stats_inst(struct irdma_sc_cqp *cqp,
2510 				      struct irdma_stats_inst_info *info,
2511 				      bool alloc, u64 scratch)
2512 {
2513 	__le64 *wqe;
2514 	u64 temp;
2515 
2516 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2517 	if (!wqe)
2518 		return -ENOMEM;
2519 
2520 	set_64bit_val(wqe, 40,
2521 		      FIELD_PREP(IRDMA_CQPSQ_STATS_HMC_FCN_INDEX, info->hmc_fn_id));
2522 	temp = FIELD_PREP(IRDMA_CQPSQ_STATS_WQEVALID, cqp->polarity) |
2523 	       FIELD_PREP(IRDMA_CQPSQ_STATS_ALLOC_INST, alloc) |
2524 	       FIELD_PREP(IRDMA_CQPSQ_STATS_USE_HMC_FCN_INDEX,
2525 			  info->use_hmc_fcn_index) |
2526 	       FIELD_PREP(IRDMA_CQPSQ_STATS_INST_INDEX, info->stats_idx) |
2527 	       FIELD_PREP(IRDMA_CQPSQ_STATS_OP, IRDMA_CQP_OP_MANAGE_STATS);
2528 
2529 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2530 
2531 	set_64bit_val(wqe, 24, temp);
2532 
2533 	print_hex_dump_debug("WQE: MANAGE_STATS WQE", DUMP_PREFIX_OFFSET, 16,
2534 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2535 
2536 	irdma_sc_cqp_post_sq(cqp);
2537 	return 0;
2538 }
2539 
2540 /**
2541  * irdma_sc_set_up_map - set the up map table
2542  * @cqp: struct for cqp hw
2543  * @info: User priority map info
2544  * @scratch: u64 saved to be used during cqp completion
2545  */
2546 static int irdma_sc_set_up_map(struct irdma_sc_cqp *cqp,
2547 			       struct irdma_up_info *info, u64 scratch)
2548 {
2549 	__le64 *wqe;
2550 	u64 temp = 0;
2551 	int i;
2552 
2553 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2554 	if (!wqe)
2555 		return -ENOMEM;
2556 
2557 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++)
2558 		temp |= (u64)info->map[i] << (i * 8);
2559 
2560 	set_64bit_val(wqe, 0, temp);
2561 	set_64bit_val(wqe, 40,
2562 		      FIELD_PREP(IRDMA_CQPSQ_UP_CNPOVERRIDE, info->cnp_up_override) |
2563 		      FIELD_PREP(IRDMA_CQPSQ_UP_HMCFCNIDX, info->hmc_fcn_idx));
2564 
2565 	temp = FIELD_PREP(IRDMA_CQPSQ_UP_WQEVALID, cqp->polarity) |
2566 	       FIELD_PREP(IRDMA_CQPSQ_UP_USEVLAN, info->use_vlan) |
2567 	       FIELD_PREP(IRDMA_CQPSQ_UP_USEOVERRIDE,
2568 			  info->use_cnp_up_override) |
2569 	       FIELD_PREP(IRDMA_CQPSQ_UP_OP, IRDMA_CQP_OP_UP_MAP);
2570 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2571 
2572 	set_64bit_val(wqe, 24, temp);
2573 
2574 	print_hex_dump_debug("WQE: UPMAP WQE", DUMP_PREFIX_OFFSET, 16, 8, wqe,
2575 			     IRDMA_CQP_WQE_SIZE * 8, false);
2576 	irdma_sc_cqp_post_sq(cqp);
2577 
2578 	return 0;
2579 }
2580 
2581 /**
2582  * irdma_sc_manage_ws_node - create/modify/destroy WS node
2583  * @cqp: struct for cqp hw
2584  * @info: node info structure
2585  * @node_op: 0 for add 1 for modify, 2 for delete
2586  * @scratch: u64 saved to be used during cqp completion
2587  */
2588 static int irdma_sc_manage_ws_node(struct irdma_sc_cqp *cqp,
2589 				   struct irdma_ws_node_info *info,
2590 				   enum irdma_ws_node_op node_op, u64 scratch)
2591 {
2592 	__le64 *wqe;
2593 	u64 temp = 0;
2594 
2595 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2596 	if (!wqe)
2597 		return -ENOMEM;
2598 
2599 	set_64bit_val(wqe, 32,
2600 		      FIELD_PREP(IRDMA_CQPSQ_WS_VSI, info->vsi) |
2601 		      FIELD_PREP(IRDMA_CQPSQ_WS_WEIGHT, info->weight));
2602 
2603 	temp = FIELD_PREP(IRDMA_CQPSQ_WS_WQEVALID, cqp->polarity) |
2604 	       FIELD_PREP(IRDMA_CQPSQ_WS_NODEOP, node_op) |
2605 	       FIELD_PREP(IRDMA_CQPSQ_WS_ENABLENODE, info->enable) |
2606 	       FIELD_PREP(IRDMA_CQPSQ_WS_NODETYPE, info->type_leaf) |
2607 	       FIELD_PREP(IRDMA_CQPSQ_WS_PRIOTYPE, info->prio_type) |
2608 	       FIELD_PREP(IRDMA_CQPSQ_WS_TC, info->tc) |
2609 	       FIELD_PREP(IRDMA_CQPSQ_WS_OP, IRDMA_CQP_OP_WORK_SCHED_NODE) |
2610 	       FIELD_PREP(IRDMA_CQPSQ_WS_PARENTID, info->parent_id) |
2611 	       FIELD_PREP(IRDMA_CQPSQ_WS_NODEID, info->id);
2612 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2613 
2614 	set_64bit_val(wqe, 24, temp);
2615 
2616 	print_hex_dump_debug("WQE: MANAGE_WS WQE", DUMP_PREFIX_OFFSET, 16, 8,
2617 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2618 	irdma_sc_cqp_post_sq(cqp);
2619 
2620 	return 0;
2621 }
2622 
2623 /**
2624  * irdma_sc_qp_flush_wqes - flush qp's wqe
2625  * @qp: sc qp
2626  * @info: dlush information
2627  * @scratch: u64 saved to be used during cqp completion
2628  * @post_sq: flag for cqp db to ring
2629  */
2630 int irdma_sc_qp_flush_wqes(struct irdma_sc_qp *qp,
2631 			   struct irdma_qp_flush_info *info, u64 scratch,
2632 			   bool post_sq)
2633 {
2634 	u64 temp = 0;
2635 	__le64 *wqe;
2636 	struct irdma_sc_cqp *cqp;
2637 	u64 hdr;
2638 	bool flush_sq = false, flush_rq = false;
2639 
2640 	if (info->rq && !qp->flush_rq)
2641 		flush_rq = true;
2642 	if (info->sq && !qp->flush_sq)
2643 		flush_sq = true;
2644 	qp->flush_sq |= flush_sq;
2645 	qp->flush_rq |= flush_rq;
2646 
2647 	if (!flush_sq && !flush_rq) {
2648 		ibdev_dbg(to_ibdev(qp->dev),
2649 			  "CQP: Additional flush request ignored for qp %x\n",
2650 			  qp->qp_uk.qp_id);
2651 		return -EALREADY;
2652 	}
2653 
2654 	cqp = qp->pd->dev->cqp;
2655 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2656 	if (!wqe)
2657 		return -ENOMEM;
2658 
2659 	if (info->userflushcode) {
2660 		if (flush_rq)
2661 			temp |= FIELD_PREP(IRDMA_CQPSQ_FWQE_RQMNERR,
2662 					   info->rq_minor_code) |
2663 				FIELD_PREP(IRDMA_CQPSQ_FWQE_RQMJERR,
2664 					   info->rq_major_code);
2665 		if (flush_sq)
2666 			temp |= FIELD_PREP(IRDMA_CQPSQ_FWQE_SQMNERR,
2667 					   info->sq_minor_code) |
2668 				FIELD_PREP(IRDMA_CQPSQ_FWQE_SQMJERR,
2669 					   info->sq_major_code);
2670 	}
2671 	set_64bit_val(wqe, 16, temp);
2672 
2673 	temp = (info->generate_ae) ?
2674 		info->ae_code | FIELD_PREP(IRDMA_CQPSQ_FWQE_AESOURCE,
2675 					   info->ae_src) : 0;
2676 	set_64bit_val(wqe, 8, temp);
2677 	if (cqp->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
2678 		set_64bit_val(wqe, 40,
2679 			      FIELD_PREP(IRDMA_CQPSQ_FWQE_ERR_SQ_IDX, info->err_sq_idx));
2680 		set_64bit_val(wqe, 48,
2681 			      FIELD_PREP(IRDMA_CQPSQ_FWQE_ERR_RQ_IDX, info->err_rq_idx));
2682 	}
2683 
2684 	hdr = qp->qp_uk.qp_id |
2685 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_FLUSH_WQES) |
2686 	      FIELD_PREP(IRDMA_CQPSQ_FWQE_GENERATE_AE, info->generate_ae) |
2687 	      FIELD_PREP(IRDMA_CQPSQ_FWQE_USERFLCODE, info->userflushcode) |
2688 	      FIELD_PREP(IRDMA_CQPSQ_FWQE_FLUSHSQ, flush_sq) |
2689 	      FIELD_PREP(IRDMA_CQPSQ_FWQE_FLUSHRQ, flush_rq) |
2690 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
2691 	if (cqp->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3)
2692 		hdr |= FIELD_PREP(IRDMA_CQPSQ_FWQE_ERR_SQ_IDX_VALID, info->err_sq_idx_valid) |
2693 		       FIELD_PREP(IRDMA_CQPSQ_FWQE_ERR_RQ_IDX_VALID, info->err_rq_idx_valid);
2694 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2695 
2696 	set_64bit_val(wqe, 24, hdr);
2697 
2698 	print_hex_dump_debug("WQE: QP_FLUSH WQE", DUMP_PREFIX_OFFSET, 16, 8,
2699 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2700 	if (post_sq)
2701 		irdma_sc_cqp_post_sq(cqp);
2702 
2703 	return 0;
2704 }
2705 
2706 /**
2707  * irdma_sc_gen_ae - generate AE, uses flush WQE CQP OP
2708  * @qp: sc qp
2709  * @info: gen ae information
2710  * @scratch: u64 saved to be used during cqp completion
2711  * @post_sq: flag for cqp db to ring
2712  */
2713 static int irdma_sc_gen_ae(struct irdma_sc_qp *qp,
2714 			   struct irdma_gen_ae_info *info, u64 scratch,
2715 			   bool post_sq)
2716 {
2717 	u64 temp;
2718 	__le64 *wqe;
2719 	struct irdma_sc_cqp *cqp;
2720 	u64 hdr;
2721 
2722 	cqp = qp->pd->dev->cqp;
2723 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2724 	if (!wqe)
2725 		return -ENOMEM;
2726 
2727 	temp = info->ae_code | FIELD_PREP(IRDMA_CQPSQ_FWQE_AESOURCE,
2728 					  info->ae_src);
2729 	set_64bit_val(wqe, 8, temp);
2730 
2731 	hdr = qp->qp_uk.qp_id | FIELD_PREP(IRDMA_CQPSQ_OPCODE,
2732 					   IRDMA_CQP_OP_GEN_AE) |
2733 	      FIELD_PREP(IRDMA_CQPSQ_FWQE_GENERATE_AE, 1) |
2734 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
2735 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2736 
2737 	set_64bit_val(wqe, 24, hdr);
2738 
2739 	print_hex_dump_debug("WQE: GEN_AE WQE", DUMP_PREFIX_OFFSET, 16, 8,
2740 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2741 	if (post_sq)
2742 		irdma_sc_cqp_post_sq(cqp);
2743 
2744 	return 0;
2745 }
2746 
2747 /*** irdma_sc_qp_upload_context - upload qp's context
2748  * @dev: sc device struct
2749  * @info: upload context info ptr for return
2750  * @scratch: u64 saved to be used during cqp completion
2751  * @post_sq: flag for cqp db to ring
2752  */
2753 static int irdma_sc_qp_upload_context(struct irdma_sc_dev *dev,
2754 				      struct irdma_upload_context_info *info,
2755 				      u64 scratch, bool post_sq)
2756 {
2757 	__le64 *wqe;
2758 	struct irdma_sc_cqp *cqp;
2759 	u64 hdr;
2760 
2761 	cqp = dev->cqp;
2762 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2763 	if (!wqe)
2764 		return -ENOMEM;
2765 
2766 	set_64bit_val(wqe, 16, info->buf_pa);
2767 
2768 	hdr = FIELD_PREP(IRDMA_CQPSQ_UCTX_QPID, info->qp_id) |
2769 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_UPLOAD_CONTEXT) |
2770 	      FIELD_PREP(IRDMA_CQPSQ_UCTX_QPTYPE, info->qp_type) |
2771 	      FIELD_PREP(IRDMA_CQPSQ_UCTX_RAWFORMAT, info->raw_format) |
2772 	      FIELD_PREP(IRDMA_CQPSQ_UCTX_FREEZEQP, info->freeze_qp) |
2773 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
2774 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2775 
2776 	set_64bit_val(wqe, 24, hdr);
2777 
2778 	print_hex_dump_debug("WQE: QP_UPLOAD_CTX WQE", DUMP_PREFIX_OFFSET, 16,
2779 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2780 	if (post_sq)
2781 		irdma_sc_cqp_post_sq(cqp);
2782 
2783 	return 0;
2784 }
2785 
2786 /**
2787  * irdma_sc_manage_push_page - Handle push page
2788  * @cqp: struct for cqp hw
2789  * @info: push page info
2790  * @scratch: u64 saved to be used during cqp completion
2791  * @post_sq: flag for cqp db to ring
2792  */
2793 static int irdma_sc_manage_push_page(struct irdma_sc_cqp *cqp,
2794 				     struct irdma_cqp_manage_push_page_info *info,
2795 				     u64 scratch, bool post_sq)
2796 {
2797 	__le64 *wqe;
2798 	u64 hdr;
2799 
2800 	if (info->free_page &&
2801 	    info->push_idx >= cqp->dev->hw_attrs.max_hw_device_pages)
2802 		return -EINVAL;
2803 
2804 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2805 	if (!wqe)
2806 		return -ENOMEM;
2807 
2808 	set_64bit_val(wqe, 16, info->qs_handle);
2809 	hdr = FIELD_PREP(IRDMA_CQPSQ_MPP_PPIDX, info->push_idx) |
2810 	      FIELD_PREP(IRDMA_CQPSQ_MPP_PPTYPE, info->push_page_type) |
2811 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MANAGE_PUSH_PAGES) |
2812 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity) |
2813 	      FIELD_PREP(IRDMA_CQPSQ_MPP_FREE_PAGE, info->free_page);
2814 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2815 
2816 	set_64bit_val(wqe, 24, hdr);
2817 
2818 	print_hex_dump_debug("WQE: MANAGE_PUSH_PAGES WQE", DUMP_PREFIX_OFFSET,
2819 			     16, 8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2820 	if (post_sq)
2821 		irdma_sc_cqp_post_sq(cqp);
2822 
2823 	return 0;
2824 }
2825 
2826 /**
2827  * irdma_sc_suspend_qp - suspend qp for param change
2828  * @cqp: struct for cqp hw
2829  * @qp: sc qp struct
2830  * @scratch: u64 saved to be used during cqp completion
2831  */
2832 static int irdma_sc_suspend_qp(struct irdma_sc_cqp *cqp, struct irdma_sc_qp *qp,
2833 			       u64 scratch)
2834 {
2835 	u64 hdr;
2836 	__le64 *wqe;
2837 
2838 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2839 	if (!wqe)
2840 		return -ENOMEM;
2841 
2842 	hdr = FIELD_PREP(IRDMA_CQPSQ_SUSPENDQP_QPID, qp->qp_uk.qp_id) |
2843 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_SUSPEND_QP) |
2844 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
2845 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2846 
2847 	set_64bit_val(wqe, 24, hdr);
2848 
2849 	print_hex_dump_debug("WQE: SUSPEND_QP WQE", DUMP_PREFIX_OFFSET, 16, 8,
2850 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2851 	irdma_sc_cqp_post_sq(cqp);
2852 
2853 	return 0;
2854 }
2855 
2856 /**
2857  * irdma_sc_resume_qp - resume qp after suspend
2858  * @cqp: struct for cqp hw
2859  * @qp: sc qp struct
2860  * @scratch: u64 saved to be used during cqp completion
2861  */
2862 static int irdma_sc_resume_qp(struct irdma_sc_cqp *cqp, struct irdma_sc_qp *qp,
2863 			      u64 scratch)
2864 {
2865 	u64 hdr;
2866 	__le64 *wqe;
2867 
2868 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2869 	if (!wqe)
2870 		return -ENOMEM;
2871 
2872 	set_64bit_val(wqe, 16,
2873 		      FIELD_PREP(IRDMA_CQPSQ_RESUMEQP_QSHANDLE, qp->qs_handle));
2874 
2875 	hdr = FIELD_PREP(IRDMA_CQPSQ_RESUMEQP_QPID, qp->qp_uk.qp_id) |
2876 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_RESUME_QP) |
2877 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
2878 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2879 
2880 	set_64bit_val(wqe, 24, hdr);
2881 
2882 	print_hex_dump_debug("WQE: RESUME_QP WQE", DUMP_PREFIX_OFFSET, 16, 8,
2883 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
2884 	irdma_sc_cqp_post_sq(cqp);
2885 
2886 	return 0;
2887 }
2888 
2889 /**
2890  * irdma_sc_cq_ack - acknowledge completion q
2891  * @cq: cq struct
2892  */
2893 static inline void irdma_sc_cq_ack(struct irdma_sc_cq *cq)
2894 {
2895 	writel(cq->cq_uk.cq_id, cq->cq_uk.cq_ack_db);
2896 }
2897 
2898 /**
2899  * irdma_sc_cq_init - initialize completion q
2900  * @cq: cq struct
2901  * @info: cq initialization info
2902  */
2903 int irdma_sc_cq_init(struct irdma_sc_cq *cq, struct irdma_cq_init_info *info)
2904 {
2905 	u32 pble_obj_cnt;
2906 
2907 	pble_obj_cnt = info->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
2908 	if (info->virtual_map && info->first_pm_pbl_idx >= pble_obj_cnt)
2909 		return -EINVAL;
2910 
2911 	cq->cq_pa = info->cq_base_pa;
2912 	cq->dev = info->dev;
2913 	cq->ceq_id = info->ceq_id;
2914 	info->cq_uk_init_info.cqe_alloc_db = cq->dev->cq_arm_db;
2915 	info->cq_uk_init_info.cq_ack_db = cq->dev->cq_ack_db;
2916 	irdma_uk_cq_init(&cq->cq_uk, &info->cq_uk_init_info);
2917 
2918 	cq->virtual_map = info->virtual_map;
2919 	cq->pbl_chunk_size = info->pbl_chunk_size;
2920 	cq->ceqe_mask = info->ceqe_mask;
2921 	cq->cq_type = (info->type) ? info->type : IRDMA_CQ_TYPE_IWARP;
2922 	cq->shadow_area_pa = info->shadow_area_pa;
2923 	cq->shadow_read_threshold = info->shadow_read_threshold;
2924 	cq->ceq_id_valid = info->ceq_id_valid;
2925 	cq->tph_en = info->tph_en;
2926 	cq->tph_val = info->tph_val;
2927 	cq->first_pm_pbl_idx = info->first_pm_pbl_idx;
2928 	cq->vsi = info->vsi;
2929 
2930 	return 0;
2931 }
2932 
2933 /**
2934  * irdma_sc_cq_create - create completion q
2935  * @cq: cq struct
2936  * @scratch: u64 saved to be used during cqp completion
2937  * @check_overflow: flag for overflow check
2938  * @post_sq: flag for cqp db to ring
2939  */
2940 static int irdma_sc_cq_create(struct irdma_sc_cq *cq, u64 scratch,
2941 			      bool check_overflow, bool post_sq)
2942 {
2943 	__le64 *wqe;
2944 	struct irdma_sc_cqp *cqp;
2945 	u64 hdr;
2946 	struct irdma_sc_ceq *ceq;
2947 	int ret_code = 0;
2948 
2949 	cqp = cq->dev->cqp;
2950 	if (cq->cq_uk.cq_id >= cqp->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].max_cnt)
2951 		return -EINVAL;
2952 
2953 	if (cq->ceq_id >= cq->dev->hmc_fpm_misc.max_ceqs)
2954 		return -EINVAL;
2955 
2956 	ceq = cq->dev->ceq[cq->ceq_id];
2957 	if (ceq && ceq->reg_cq)
2958 		ret_code = irdma_sc_add_cq_ctx(ceq, cq);
2959 
2960 	if (ret_code)
2961 		return ret_code;
2962 
2963 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2964 	if (!wqe) {
2965 		if (ceq && ceq->reg_cq)
2966 			irdma_sc_remove_cq_ctx(ceq, cq);
2967 		return -ENOMEM;
2968 	}
2969 
2970 	set_64bit_val(wqe, 0, cq->cq_uk.cq_size);
2971 	set_64bit_val(wqe, 8, (uintptr_t)cq >> 1);
2972 	set_64bit_val(wqe, 16,
2973 		      FIELD_PREP(IRDMA_CQPSQ_CQ_SHADOW_READ_THRESHOLD, cq->shadow_read_threshold));
2974 	set_64bit_val(wqe, 32, (cq->virtual_map ? 0 : cq->cq_pa));
2975 	set_64bit_val(wqe, 40, cq->shadow_area_pa);
2976 	set_64bit_val(wqe, 48,
2977 		      FIELD_PREP(IRDMA_CQPSQ_CQ_FIRSTPMPBLIDX, (cq->virtual_map ? cq->first_pm_pbl_idx : 0)));
2978 	set_64bit_val(wqe, 56,
2979 		      FIELD_PREP(IRDMA_CQPSQ_TPHVAL, cq->tph_val) |
2980 		      FIELD_PREP(IRDMA_CQPSQ_VSIIDX, cq->vsi->vsi_idx));
2981 
2982 	hdr = FLD_LS_64(cq->dev, cq->cq_uk.cq_id, IRDMA_CQPSQ_CQ_CQID) |
2983 	      FLD_LS_64(cq->dev, (cq->ceq_id_valid ? cq->ceq_id : 0),
2984 			IRDMA_CQPSQ_CQ_CEQID) |
2985 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_CREATE_CQ) |
2986 	      FIELD_PREP(IRDMA_CQPSQ_CQ_LPBLSIZE, cq->pbl_chunk_size) |
2987 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CHKOVERFLOW, check_overflow) |
2988 	      FIELD_PREP(IRDMA_CQPSQ_CQ_VIRTMAP, cq->virtual_map) |
2989 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CQID_HIGH, cq->cq_uk.cq_id >> 22) |
2990 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CEQID_HIGH,
2991 			 (cq->ceq_id_valid ? cq->ceq_id : 0) >> 10) |
2992 	      FIELD_PREP(IRDMA_CQPSQ_CQ_ENCEQEMASK, cq->ceqe_mask) |
2993 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CEQIDVALID, cq->ceq_id_valid) |
2994 	      FIELD_PREP(IRDMA_CQPSQ_TPHEN, cq->tph_en) |
2995 	      FIELD_PREP(IRDMA_CQPSQ_CQ_AVOIDMEMCNFLCT,
2996 			 cq->cq_uk.avoid_mem_cflct) |
2997 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
2998 	dma_wmb(); /* make sure WQE is written before valid bit is set */
2999 
3000 	set_64bit_val(wqe, 24, hdr);
3001 
3002 	print_hex_dump_debug("WQE: CQ_CREATE WQE", DUMP_PREFIX_OFFSET, 16, 8,
3003 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
3004 	if (post_sq)
3005 		irdma_sc_cqp_post_sq(cqp);
3006 
3007 	return 0;
3008 }
3009 
3010 /**
3011  * irdma_sc_cq_destroy - destroy completion q
3012  * @cq: cq struct
3013  * @scratch: u64 saved to be used during cqp completion
3014  * @post_sq: flag for cqp db to ring
3015  */
3016 int irdma_sc_cq_destroy(struct irdma_sc_cq *cq, u64 scratch, bool post_sq)
3017 {
3018 	struct irdma_sc_cqp *cqp;
3019 	__le64 *wqe;
3020 	u64 hdr;
3021 	struct irdma_sc_ceq *ceq;
3022 
3023 	cqp = cq->dev->cqp;
3024 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
3025 	if (!wqe)
3026 		return -ENOMEM;
3027 
3028 	ceq = cq->dev->ceq[cq->ceq_id];
3029 	if (ceq && ceq->reg_cq)
3030 		irdma_sc_remove_cq_ctx(ceq, cq);
3031 
3032 	set_64bit_val(wqe, 0, cq->cq_uk.cq_size);
3033 	set_64bit_val(wqe, 8, (uintptr_t)cq >> 1);
3034 	set_64bit_val(wqe, 40, cq->shadow_area_pa);
3035 	set_64bit_val(wqe, 48,
3036 		      (cq->virtual_map ? cq->first_pm_pbl_idx : 0));
3037 
3038 	hdr = cq->cq_uk.cq_id |
3039 	      FLD_LS_64(cq->dev, (cq->ceq_id_valid ? cq->ceq_id : 0),
3040 			IRDMA_CQPSQ_CQ_CEQID) |
3041 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DESTROY_CQ) |
3042 	      FIELD_PREP(IRDMA_CQPSQ_CQ_LPBLSIZE, cq->pbl_chunk_size) |
3043 	      FIELD_PREP(IRDMA_CQPSQ_CQ_VIRTMAP, cq->virtual_map) |
3044 	      FIELD_PREP(IRDMA_CQPSQ_CQ_ENCEQEMASK, cq->ceqe_mask) |
3045 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CEQIDVALID, cq->ceq_id_valid) |
3046 	      FIELD_PREP(IRDMA_CQPSQ_TPHEN, cq->tph_en) |
3047 	      FIELD_PREP(IRDMA_CQPSQ_CQ_AVOIDMEMCNFLCT, cq->cq_uk.avoid_mem_cflct) |
3048 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
3049 	dma_wmb(); /* make sure WQE is written before valid bit is set */
3050 
3051 	set_64bit_val(wqe, 24, hdr);
3052 
3053 	print_hex_dump_debug("WQE: CQ_DESTROY WQE", DUMP_PREFIX_OFFSET, 16, 8,
3054 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
3055 	if (post_sq)
3056 		irdma_sc_cqp_post_sq(cqp);
3057 
3058 	return 0;
3059 }
3060 
3061 /**
3062  * irdma_sc_cq_resize - set resized cq buffer info
3063  * @cq: resized cq
3064  * @info: resized cq buffer info
3065  */
3066 void irdma_sc_cq_resize(struct irdma_sc_cq *cq, struct irdma_modify_cq_info *info)
3067 {
3068 	cq->virtual_map = info->virtual_map;
3069 	cq->cq_pa = info->cq_pa;
3070 	cq->first_pm_pbl_idx = info->first_pm_pbl_idx;
3071 	cq->pbl_chunk_size = info->pbl_chunk_size;
3072 	irdma_uk_cq_resize(&cq->cq_uk, info->cq_base, info->cq_size);
3073 }
3074 
3075 /**
3076  * irdma_sc_cq_modify - modify a Completion Queue
3077  * @cq: cq struct
3078  * @info: modification info struct
3079  * @scratch: u64 saved to be used during cqp completion
3080  * @post_sq: flag to post to sq
3081  */
3082 static int irdma_sc_cq_modify(struct irdma_sc_cq *cq,
3083 			      struct irdma_modify_cq_info *info, u64 scratch,
3084 			      bool post_sq)
3085 {
3086 	struct irdma_sc_cqp *cqp;
3087 	__le64 *wqe;
3088 	u64 hdr;
3089 	u32 pble_obj_cnt;
3090 
3091 	pble_obj_cnt = cq->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
3092 	if (info->cq_resize && info->virtual_map &&
3093 	    info->first_pm_pbl_idx >= pble_obj_cnt)
3094 		return -EINVAL;
3095 
3096 	cqp = cq->dev->cqp;
3097 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
3098 	if (!wqe)
3099 		return -ENOMEM;
3100 
3101 	set_64bit_val(wqe, 0, info->cq_size);
3102 	set_64bit_val(wqe, 8, (uintptr_t)cq >> 1);
3103 	set_64bit_val(wqe, 16,
3104 		      FIELD_PREP(IRDMA_CQPSQ_CQ_SHADOW_READ_THRESHOLD, info->shadow_read_threshold));
3105 	set_64bit_val(wqe, 32, info->cq_pa);
3106 	set_64bit_val(wqe, 40, cq->shadow_area_pa);
3107 	set_64bit_val(wqe, 48, info->first_pm_pbl_idx);
3108 	set_64bit_val(wqe, 56,
3109 		      FIELD_PREP(IRDMA_CQPSQ_TPHVAL, cq->tph_val) |
3110 		      FIELD_PREP(IRDMA_CQPSQ_VSIIDX, cq->vsi->vsi_idx));
3111 
3112 	hdr = cq->cq_uk.cq_id |
3113 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_MODIFY_CQ) |
3114 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CQRESIZE, info->cq_resize) |
3115 	      FIELD_PREP(IRDMA_CQPSQ_CQ_LPBLSIZE, info->pbl_chunk_size) |
3116 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CHKOVERFLOW, info->check_overflow) |
3117 	      FIELD_PREP(IRDMA_CQPSQ_CQ_VIRTMAP, info->virtual_map) |
3118 	      FIELD_PREP(IRDMA_CQPSQ_CQ_ENCEQEMASK, cq->ceqe_mask) |
3119 	      FIELD_PREP(IRDMA_CQPSQ_TPHEN, cq->tph_en) |
3120 	      FIELD_PREP(IRDMA_CQPSQ_CQ_AVOIDMEMCNFLCT,
3121 			 cq->cq_uk.avoid_mem_cflct) |
3122 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
3123 	dma_wmb(); /* make sure WQE is written before valid bit is set */
3124 
3125 	set_64bit_val(wqe, 24, hdr);
3126 
3127 	print_hex_dump_debug("WQE: CQ_MODIFY WQE", DUMP_PREFIX_OFFSET, 16, 8,
3128 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
3129 	if (post_sq)
3130 		irdma_sc_cqp_post_sq(cqp);
3131 
3132 	return 0;
3133 }
3134 
3135 /**
3136  * irdma_sc_get_decoded_ird_size_gen_3 - get decoded IRD size for GEN 3
3137  * @ird_enc: IRD encoding
3138  * IRD size defaults to a value of 4 in case of invalid input.
3139  */
3140 static u16 irdma_sc_get_decoded_ird_size_gen_3(u8 ird_enc)
3141 {
3142 	switch (ird_enc) {
3143 	case IRDMA_IRD_HW_SIZE_4096_GEN3:
3144 		return 4096;
3145 	case IRDMA_IRD_HW_SIZE_2048_GEN3:
3146 		return 2048;
3147 	case IRDMA_IRD_HW_SIZE_1024_GEN3:
3148 		return 1024;
3149 	case IRDMA_IRD_HW_SIZE_512_GEN3:
3150 		return 512;
3151 	case IRDMA_IRD_HW_SIZE_256_GEN3:
3152 		return 256;
3153 	case IRDMA_IRD_HW_SIZE_128_GEN3:
3154 		return 128;
3155 	case IRDMA_IRD_HW_SIZE_64_GEN3:
3156 		return 64;
3157 	case IRDMA_IRD_HW_SIZE_32_GEN3:
3158 		return 32;
3159 	case IRDMA_IRD_HW_SIZE_16_GEN3:
3160 		return 16;
3161 	case IRDMA_IRD_HW_SIZE_8_GEN3:
3162 		return 8;
3163 	case IRDMA_IRD_HW_SIZE_4_GEN3:
3164 		return 4;
3165 	default:
3166 		return 4;
3167 	}
3168 }
3169 
3170 /**
3171  * irdma_check_cqp_progress - check cqp processing progress
3172  * @timeout: timeout info struct
3173  * @dev: sc device struct
3174  */
3175 void irdma_check_cqp_progress(struct irdma_cqp_timeout *timeout, struct irdma_sc_dev *dev)
3176 {
3177 	u64 completed_ops = atomic64_read(&dev->cqp->completed_ops);
3178 
3179 	if (timeout->compl_cqp_cmds != completed_ops) {
3180 		timeout->compl_cqp_cmds = completed_ops;
3181 		timeout->count = 0;
3182 	} else if (timeout->compl_cqp_cmds != dev->cqp->requested_ops) {
3183 		timeout->count++;
3184 	}
3185 }
3186 
3187 /**
3188  * irdma_get_cqp_reg_info - get head and tail for cqp using registers
3189  * @cqp: struct for cqp hw
3190  * @val: cqp tail register value
3191  * @tail: wqtail register value
3192  * @error: cqp processing err
3193  */
3194 static inline void irdma_get_cqp_reg_info(struct irdma_sc_cqp *cqp, u32 *val,
3195 					  u32 *tail, u32 *error)
3196 {
3197 	*val = readl(cqp->dev->hw_regs[IRDMA_CQPTAIL]);
3198 	*tail = FIELD_GET(IRDMA_CQPTAIL_WQTAIL, *val);
3199 	*error = FIELD_GET(IRDMA_CQPTAIL_CQP_OP_ERR, *val);
3200 }
3201 
3202 /**
3203  * irdma_sc_cqp_def_cmpl_ae_handler - remove completed requests from pending list
3204  * @dev: sc device struct
3205  * @info: AE entry info
3206  * @first: true if this is the first call to this handler for given AEQE
3207  * @scratch: (out) scratch entry pointer
3208  * @sw_def_info: (in/out) SW ticket value for this AE
3209  *
3210  * In case of AE_DEF_CMPL event, this function should be called in a loop
3211  * until it returns NULL-ptr via scratch.
3212  * For each call, it looks for a matching CQP request on pending list,
3213  * removes it from the list and returns the pointer to the associated scratch
3214  * entry.
3215  * If this is the first call to this function for given AEQE, sw_def_info
3216  * value is not used to find matching requests.  Instead, it is populated
3217  * with the value from the first matching cqp_request on the list.
3218  * For subsequent calls, ooo_op->sw_def_info need to match the value passed
3219  * by a caller.
3220  *
3221  * Return: scratch entry pointer for cqp_request to be released or NULL
3222  * if no matching request is found.
3223  */
3224 void irdma_sc_cqp_def_cmpl_ae_handler(struct irdma_sc_dev *dev,
3225 				      struct irdma_aeqe_info *info,
3226 				      bool first, u64 *scratch,
3227 				      u32 *sw_def_info)
3228 {
3229 	struct irdma_ooo_cqp_op *ooo_op;
3230 	unsigned long flags;
3231 
3232 	*scratch = 0;
3233 
3234 	spin_lock_irqsave(&dev->cqp->ooo_list_lock, flags);
3235 	list_for_each_entry(ooo_op, &dev->cqp->ooo_pnd, list_entry) {
3236 		if (ooo_op->deferred &&
3237 		    ((first && ooo_op->def_info == info->def_info) ||
3238 		     (!first && ooo_op->sw_def_info == *sw_def_info))) {
3239 			*sw_def_info = ooo_op->sw_def_info;
3240 			*scratch = ooo_op->scratch;
3241 
3242 			list_move(&ooo_op->list_entry, &dev->cqp->ooo_avail);
3243 			atomic64_inc(&dev->cqp->completed_ops);
3244 
3245 			break;
3246 		}
3247 	}
3248 	spin_unlock_irqrestore(&dev->cqp->ooo_list_lock, flags);
3249 
3250 	if (first && !*scratch)
3251 		ibdev_dbg(to_ibdev(dev),
3252 			  "AEQ: deferred completion with unknown ticket: def_info 0x%x\n",
3253 			   info->def_info);
3254 }
3255 
3256 /**
3257  * irdma_sc_cqp_cleanup_handler - remove requests from pending list
3258  * @dev: sc device struct
3259  *
3260  * This function should be called in a loop from irdma_cleanup_pending_cqp_op.
3261  * For each call, it returns first CQP request on pending list, removes it
3262  * from the list and returns the pointer to the associated scratch entry.
3263  *
3264  * Return: scratch entry pointer for cqp_request to be released or NULL
3265  * if pending list is empty.
3266  */
3267 u64 irdma_sc_cqp_cleanup_handler(struct irdma_sc_dev *dev)
3268 {
3269 	struct irdma_ooo_cqp_op *ooo_op;
3270 	u64 scratch = 0;
3271 
3272 	list_for_each_entry(ooo_op, &dev->cqp->ooo_pnd, list_entry) {
3273 		scratch = ooo_op->scratch;
3274 
3275 		list_del(&ooo_op->list_entry);
3276 		list_add(&ooo_op->list_entry, &dev->cqp->ooo_avail);
3277 		atomic64_inc(&dev->cqp->completed_ops);
3278 
3279 		break;
3280 	}
3281 
3282 	return scratch;
3283 }
3284 
3285 /**
3286  * irdma_cqp_poll_registers - poll cqp registers
3287  * @cqp: struct for cqp hw
3288  * @tail: wqtail register value
3289  * @count: how many times to try for completion
3290  */
3291 static int irdma_cqp_poll_registers(struct irdma_sc_cqp *cqp, u32 tail,
3292 				    u32 count)
3293 {
3294 	u32 i = 0;
3295 	u32 newtail, error, val;
3296 
3297 	while (i++ < count) {
3298 		irdma_get_cqp_reg_info(cqp, &val, &newtail, &error);
3299 		if (error) {
3300 			error = readl(cqp->dev->hw_regs[IRDMA_CQPERRCODES]);
3301 			ibdev_dbg(to_ibdev(cqp->dev),
3302 				  "CQP: CQPERRCODES error_code[x%08X]\n",
3303 				  error);
3304 			return -EIO;
3305 		}
3306 		if (newtail != tail) {
3307 			/* SUCCESS */
3308 			IRDMA_RING_MOVE_TAIL(cqp->sq_ring);
3309 			atomic64_inc(&cqp->completed_ops);
3310 			return 0;
3311 		}
3312 		udelay(cqp->dev->hw_attrs.max_sleep_count);
3313 	}
3314 
3315 	return -ETIMEDOUT;
3316 }
3317 
3318 /**
3319  * irdma_sc_decode_fpm_commit - decode a 64 bit value into count and base
3320  * @dev: sc device struct
3321  * @buf: pointer to commit buffer
3322  * @buf_idx: buffer index
3323  * @obj_info: object info pointer
3324  * @rsrc_idx: indexs of memory resource
3325  */
3326 static u64 irdma_sc_decode_fpm_commit(struct irdma_sc_dev *dev, __le64 *buf,
3327 				      u32 buf_idx, struct irdma_hmc_obj_info *obj_info,
3328 				      u32 rsrc_idx)
3329 {
3330 	u64 temp;
3331 
3332 	get_64bit_val(buf, buf_idx, &temp);
3333 
3334 	switch (rsrc_idx) {
3335 	case IRDMA_HMC_IW_QP:
3336 		obj_info[rsrc_idx].cnt = (u32)FIELD_GET(IRDMA_COMMIT_FPM_QPCNT, temp);
3337 		break;
3338 	case IRDMA_HMC_IW_CQ:
3339 		obj_info[rsrc_idx].cnt = (u32)FLD_RS_64(dev, temp, IRDMA_COMMIT_FPM_CQCNT);
3340 		break;
3341 	case IRDMA_HMC_IW_APBVT_ENTRY:
3342 		if (dev->hw_attrs.uk_attrs.hw_rev <= IRDMA_GEN_2)
3343 			obj_info[rsrc_idx].cnt = 1;
3344 		else
3345 			obj_info[rsrc_idx].cnt = 0;
3346 		break;
3347 	default:
3348 		obj_info[rsrc_idx].cnt = (u32)temp;
3349 		break;
3350 	}
3351 
3352 	obj_info[rsrc_idx].base = (temp >> IRDMA_COMMIT_FPM_BASE_S) * 512;
3353 
3354 	return temp;
3355 }
3356 
3357 /**
3358  * irdma_sc_parse_fpm_commit_buf - parse fpm commit buffer
3359  * @dev: pointer to dev struct
3360  * @buf: ptr to fpm commit buffer
3361  * @info: ptr to irdma_hmc_obj_info struct
3362  * @sd: number of SDs for HMC objects
3363  *
3364  * parses fpm commit info and copy base value
3365  * of hmc objects in hmc_info
3366  */
3367 static void
3368 irdma_sc_parse_fpm_commit_buf(struct irdma_sc_dev *dev, __le64 *buf,
3369 			      struct irdma_hmc_obj_info *info, u32 *sd)
3370 {
3371 	u64 size;
3372 	u32 i;
3373 	u64 max_base = 0;
3374 	u32 last_hmc_obj = 0;
3375 
3376 	irdma_sc_decode_fpm_commit(dev, buf, 0, info,
3377 				   IRDMA_HMC_IW_QP);
3378 	irdma_sc_decode_fpm_commit(dev, buf, 8, info,
3379 				   IRDMA_HMC_IW_CQ);
3380 	irdma_sc_decode_fpm_commit(dev, buf, 16, info,
3381 				   IRDMA_HMC_IW_SRQ);
3382 	irdma_sc_decode_fpm_commit(dev, buf, 24, info,
3383 				   IRDMA_HMC_IW_HTE);
3384 	irdma_sc_decode_fpm_commit(dev, buf, 32, info,
3385 				   IRDMA_HMC_IW_ARP);
3386 	irdma_sc_decode_fpm_commit(dev, buf, 40, info,
3387 				   IRDMA_HMC_IW_APBVT_ENTRY);
3388 	irdma_sc_decode_fpm_commit(dev, buf, 48, info,
3389 				   IRDMA_HMC_IW_MR);
3390 	irdma_sc_decode_fpm_commit(dev, buf, 56, info,
3391 				   IRDMA_HMC_IW_XF);
3392 	irdma_sc_decode_fpm_commit(dev, buf, 64, info,
3393 				   IRDMA_HMC_IW_XFFL);
3394 	irdma_sc_decode_fpm_commit(dev, buf, 72, info,
3395 				   IRDMA_HMC_IW_Q1);
3396 	irdma_sc_decode_fpm_commit(dev, buf, 80, info,
3397 				   IRDMA_HMC_IW_Q1FL);
3398 	irdma_sc_decode_fpm_commit(dev, buf, 88, info,
3399 				   IRDMA_HMC_IW_TIMER);
3400 	irdma_sc_decode_fpm_commit(dev, buf, 112, info,
3401 				   IRDMA_HMC_IW_PBLE);
3402 	/* skipping RSVD. */
3403 	if (dev->hw_attrs.uk_attrs.hw_rev != IRDMA_GEN_1) {
3404 		irdma_sc_decode_fpm_commit(dev, buf, 96, info,
3405 					   IRDMA_HMC_IW_FSIMC);
3406 		irdma_sc_decode_fpm_commit(dev, buf, 104, info,
3407 					   IRDMA_HMC_IW_FSIAV);
3408 		irdma_sc_decode_fpm_commit(dev, buf, 128, info,
3409 					   IRDMA_HMC_IW_RRF);
3410 		irdma_sc_decode_fpm_commit(dev, buf, 136, info,
3411 					   IRDMA_HMC_IW_RRFFL);
3412 		irdma_sc_decode_fpm_commit(dev, buf, 144, info,
3413 					   IRDMA_HMC_IW_HDR);
3414 		irdma_sc_decode_fpm_commit(dev, buf, 152, info,
3415 					   IRDMA_HMC_IW_MD);
3416 		if (dev->cqp->protocol_used == IRDMA_IWARP_PROTOCOL_ONLY) {
3417 			irdma_sc_decode_fpm_commit(dev, buf, 160, info,
3418 						   IRDMA_HMC_IW_OOISC);
3419 			irdma_sc_decode_fpm_commit(dev, buf, 168, info,
3420 						   IRDMA_HMC_IW_OOISCFFL);
3421 		}
3422 	}
3423 
3424 	/* searching for the last object in HMC to find the size of the HMC area. */
3425 	for (i = IRDMA_HMC_IW_QP; i < IRDMA_HMC_IW_MAX; i++) {
3426 		if (info[i].base > max_base && info[i].cnt) {
3427 			max_base = info[i].base;
3428 			last_hmc_obj = i;
3429 		}
3430 	}
3431 
3432 	size = info[last_hmc_obj].cnt * info[last_hmc_obj].size +
3433 	       info[last_hmc_obj].base;
3434 
3435 	if (size & 0x1FFFFF)
3436 		*sd = (u32)((size >> 21) + 1); /* add 1 for remainder */
3437 	else
3438 		*sd = (u32)(size >> 21);
3439 
3440 }
3441 
3442 /**
3443  * irdma_sc_decode_fpm_query() - Decode a 64 bit value into max count and size
3444  * @buf: ptr to fpm query buffer
3445  * @buf_idx: index into buf
3446  * @obj_info: ptr to irdma_hmc_obj_info struct
3447  * @rsrc_idx: resource index into info
3448  *
3449  * Decode a 64 bit value from fpm query buffer into max count and size
3450  */
3451 static u64 irdma_sc_decode_fpm_query(__le64 *buf, u32 buf_idx,
3452 				     struct irdma_hmc_obj_info *obj_info,
3453 				     u32 rsrc_idx)
3454 {
3455 	u64 temp;
3456 	u32 size;
3457 
3458 	get_64bit_val(buf, buf_idx, &temp);
3459 	obj_info[rsrc_idx].max_cnt = (u32)temp;
3460 	size = (u32)(temp >> 32);
3461 	obj_info[rsrc_idx].size = BIT_ULL(size);
3462 
3463 	return temp;
3464 }
3465 
3466 /**
3467  * irdma_sc_parse_fpm_query_buf() - parses fpm query buffer
3468  * @dev: ptr to shared code device
3469  * @buf: ptr to fpm query buffer
3470  * @hmc_info: ptr to irdma_hmc_obj_info struct
3471  * @hmc_fpm_misc: ptr to fpm data
3472  *
3473  * parses fpm query buffer and copy max_cnt and
3474  * size value of hmc objects in hmc_info
3475  */
3476 static int irdma_sc_parse_fpm_query_buf(struct irdma_sc_dev *dev, __le64 *buf,
3477 					struct irdma_hmc_info *hmc_info,
3478 					struct irdma_hmc_fpm_misc *hmc_fpm_misc)
3479 {
3480 	struct irdma_hmc_obj_info *obj_info;
3481 	u8 ird_encoding;
3482 	u64 temp;
3483 	u32 size;
3484 	u16 max_pe_sds;
3485 
3486 	obj_info = hmc_info->hmc_obj;
3487 
3488 	get_64bit_val(buf, 0, &temp);
3489 	hmc_info->first_sd_index = (u16)FIELD_GET(IRDMA_QUERY_FPM_FIRST_PE_SD_INDEX, temp);
3490 
3491 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3)
3492 		max_pe_sds = (u16)FIELD_GET(IRDMA_QUERY_FPM_MAX_PE_SDS_GEN3, temp);
3493 	else
3494 		max_pe_sds = (u16)FIELD_GET(IRDMA_QUERY_FPM_MAX_PE_SDS, temp);
3495 
3496 	/* Reduce SD count for unprivleged functions by 1 to account for PBLE
3497 	 * backing page rounding
3498 	 */
3499 	if (dev->hw_attrs.uk_attrs.hw_rev <= IRDMA_GEN_2 &&
3500 	    (hmc_info->hmc_fn_id >= dev->hw_attrs.first_hw_vf_fpm_id ||
3501 	    !dev->privileged))
3502 		max_pe_sds--;
3503 
3504 	hmc_fpm_misc->max_sds = max_pe_sds;
3505 	hmc_info->sd_table.sd_cnt = max_pe_sds + hmc_info->first_sd_index;
3506 	get_64bit_val(buf, 8, &temp);
3507 	obj_info[IRDMA_HMC_IW_QP].max_cnt = (u32)FIELD_GET(IRDMA_QUERY_FPM_MAX_QPS, temp);
3508 	size = (u32)(temp >> 32);
3509 	obj_info[IRDMA_HMC_IW_QP].size = BIT_ULL(size);
3510 
3511 	get_64bit_val(buf, 16, &temp);
3512 	obj_info[IRDMA_HMC_IW_CQ].max_cnt = (u32)FIELD_GET(IRDMA_QUERY_FPM_MAX_CQS, temp);
3513 	size = (u32)(temp >> 32);
3514 	obj_info[IRDMA_HMC_IW_CQ].size = BIT_ULL(size);
3515 
3516 	irdma_sc_decode_fpm_query(buf, 24, obj_info, IRDMA_HMC_IW_SRQ);
3517 	irdma_sc_decode_fpm_query(buf, 32, obj_info, IRDMA_HMC_IW_HTE);
3518 	irdma_sc_decode_fpm_query(buf, 40, obj_info, IRDMA_HMC_IW_ARP);
3519 
3520 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
3521 		obj_info[IRDMA_HMC_IW_APBVT_ENTRY].size = 0;
3522 		obj_info[IRDMA_HMC_IW_APBVT_ENTRY].max_cnt = 0;
3523 	} else {
3524 		obj_info[IRDMA_HMC_IW_APBVT_ENTRY].size = 8192;
3525 		obj_info[IRDMA_HMC_IW_APBVT_ENTRY].max_cnt = 1;
3526 	}
3527 
3528 	irdma_sc_decode_fpm_query(buf, 48, obj_info, IRDMA_HMC_IW_MR);
3529 	irdma_sc_decode_fpm_query(buf, 56, obj_info, IRDMA_HMC_IW_XF);
3530 
3531 	get_64bit_val(buf, 64, &temp);
3532 	obj_info[IRDMA_HMC_IW_XFFL].max_cnt = (u32)temp;
3533 	obj_info[IRDMA_HMC_IW_XFFL].size = 4;
3534 	hmc_fpm_misc->xf_block_size = FIELD_GET(IRDMA_QUERY_FPM_XFBLOCKSIZE, temp);
3535 	if (obj_info[IRDMA_HMC_IW_XF].max_cnt && !hmc_fpm_misc->xf_block_size)
3536 		return -EINVAL;
3537 
3538 	irdma_sc_decode_fpm_query(buf, 72, obj_info, IRDMA_HMC_IW_Q1);
3539 	get_64bit_val(buf, 80, &temp);
3540 	obj_info[IRDMA_HMC_IW_Q1FL].max_cnt = (u32)temp;
3541 	obj_info[IRDMA_HMC_IW_Q1FL].size = 4;
3542 
3543 	hmc_fpm_misc->q1_block_size = FIELD_GET(IRDMA_QUERY_FPM_Q1BLOCKSIZE, temp);
3544 	if (!hmc_fpm_misc->q1_block_size)
3545 		return -EINVAL;
3546 
3547 	irdma_sc_decode_fpm_query(buf, 88, obj_info, IRDMA_HMC_IW_TIMER);
3548 
3549 	get_64bit_val(buf, 112, &temp);
3550 	obj_info[IRDMA_HMC_IW_PBLE].max_cnt = (u32)temp;
3551 	obj_info[IRDMA_HMC_IW_PBLE].size = 8;
3552 
3553 	get_64bit_val(buf, 120, &temp);
3554 	hmc_fpm_misc->max_ceqs = FIELD_GET(IRDMA_QUERY_FPM_MAX_CEQS, temp);
3555 	hmc_fpm_misc->ht_multiplier = FIELD_GET(IRDMA_QUERY_FPM_HTMULTIPLIER, temp);
3556 	hmc_fpm_misc->timer_bucket = FIELD_GET(IRDMA_QUERY_FPM_TIMERBUCKET, temp);
3557 	if (FIELD_GET(IRDMA_MANAGE_RSRC_VER2,
3558 		      dev->feature_info[IRDMA_FTN_FLAGS])) {
3559 		ird_encoding = (u8)FIELD_GET(IRDMA_QUERY_FPM_MAX_IRD, temp);
3560 		hmc_fpm_misc->ird =
3561 			irdma_sc_get_decoded_ird_size_gen_3(ird_encoding) / 2;
3562 		dev->hw_attrs.max_hw_ird = hmc_fpm_misc->ird;
3563 		dev->hw_attrs.max_hw_ord = hmc_fpm_misc->ird;
3564 	}
3565 	if (dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
3566 		return 0;
3567 	irdma_sc_decode_fpm_query(buf, 96, obj_info, IRDMA_HMC_IW_FSIMC);
3568 	irdma_sc_decode_fpm_query(buf, 104, obj_info, IRDMA_HMC_IW_FSIAV);
3569 	irdma_sc_decode_fpm_query(buf, 128, obj_info, IRDMA_HMC_IW_RRF);
3570 
3571 	get_64bit_val(buf, 136, &temp);
3572 	obj_info[IRDMA_HMC_IW_RRFFL].max_cnt = (u32)temp;
3573 	obj_info[IRDMA_HMC_IW_RRFFL].size = 4;
3574 	hmc_fpm_misc->rrf_block_size = FIELD_GET(IRDMA_QUERY_FPM_RRFBLOCKSIZE, temp);
3575 	if (!hmc_fpm_misc->rrf_block_size &&
3576 	    obj_info[IRDMA_HMC_IW_RRFFL].max_cnt)
3577 		return -EINVAL;
3578 
3579 	irdma_sc_decode_fpm_query(buf, 144, obj_info, IRDMA_HMC_IW_HDR);
3580 	irdma_sc_decode_fpm_query(buf, 152, obj_info, IRDMA_HMC_IW_MD);
3581 
3582 	if (dev->cqp->protocol_used == IRDMA_IWARP_PROTOCOL_ONLY) {
3583 		irdma_sc_decode_fpm_query(buf, 160, obj_info, IRDMA_HMC_IW_OOISC);
3584 
3585 		get_64bit_val(buf, 168, &temp);
3586 		obj_info[IRDMA_HMC_IW_OOISCFFL].max_cnt = (u32)temp;
3587 		obj_info[IRDMA_HMC_IW_OOISCFFL].size = 4;
3588 		hmc_fpm_misc->ooiscf_block_size = FIELD_GET(IRDMA_QUERY_FPM_OOISCFBLOCKSIZE, temp);
3589 		if (!hmc_fpm_misc->ooiscf_block_size &&
3590 		    obj_info[IRDMA_HMC_IW_OOISCFFL].max_cnt)
3591 			return -EINVAL;
3592 	}
3593 
3594 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
3595 		get_64bit_val(buf, 176, &temp);
3596 		hmc_fpm_misc->loc_mem_pages = (u32)FIELD_GET(IRDMA_QUERY_FPM_LOC_MEM_PAGES, temp);
3597 		if (!hmc_fpm_misc->loc_mem_pages)
3598 			return -EINVAL;
3599 	}
3600 
3601 	return 0;
3602 }
3603 
3604 /**
3605  * irdma_sc_find_reg_cq - find cq ctx index
3606  * @ceq: ceq sc structure
3607  * @cq: cq sc structure
3608  */
3609 static u32 irdma_sc_find_reg_cq(struct irdma_sc_ceq *ceq,
3610 				struct irdma_sc_cq *cq)
3611 {
3612 	u32 i;
3613 
3614 	for (i = 0; i < ceq->reg_cq_size; i++) {
3615 		if (cq == ceq->reg_cq[i])
3616 			return i;
3617 	}
3618 
3619 	return IRDMA_INVALID_CQ_IDX;
3620 }
3621 
3622 /**
3623  * irdma_sc_add_cq_ctx - add cq ctx tracking for ceq
3624  * @ceq: ceq sc structure
3625  * @cq: cq sc structure
3626  */
3627 int irdma_sc_add_cq_ctx(struct irdma_sc_ceq *ceq, struct irdma_sc_cq *cq)
3628 {
3629 	unsigned long flags;
3630 
3631 	spin_lock_irqsave(&ceq->req_cq_lock, flags);
3632 
3633 	if (ceq->reg_cq_size == ceq->elem_cnt) {
3634 		spin_unlock_irqrestore(&ceq->req_cq_lock, flags);
3635 		return -ENOMEM;
3636 	}
3637 
3638 	ceq->reg_cq[ceq->reg_cq_size++] = cq;
3639 
3640 	spin_unlock_irqrestore(&ceq->req_cq_lock, flags);
3641 
3642 	return 0;
3643 }
3644 
3645 /**
3646  * irdma_sc_remove_cq_ctx - remove cq ctx tracking for ceq
3647  * @ceq: ceq sc structure
3648  * @cq: cq sc structure
3649  */
3650 void irdma_sc_remove_cq_ctx(struct irdma_sc_ceq *ceq, struct irdma_sc_cq *cq)
3651 {
3652 	unsigned long flags;
3653 	u32 cq_ctx_idx;
3654 
3655 	spin_lock_irqsave(&ceq->req_cq_lock, flags);
3656 	cq_ctx_idx = irdma_sc_find_reg_cq(ceq, cq);
3657 	if (cq_ctx_idx == IRDMA_INVALID_CQ_IDX)
3658 		goto exit;
3659 
3660 	ceq->reg_cq_size--;
3661 	if (cq_ctx_idx != ceq->reg_cq_size)
3662 		ceq->reg_cq[cq_ctx_idx] = ceq->reg_cq[ceq->reg_cq_size];
3663 	ceq->reg_cq[ceq->reg_cq_size] = NULL;
3664 
3665 exit:
3666 	spin_unlock_irqrestore(&ceq->req_cq_lock, flags);
3667 }
3668 
3669 /**
3670  * irdma_sc_cqp_init - Initialize buffers for a control Queue Pair
3671  * @cqp: IWARP control queue pair pointer
3672  * @info: IWARP control queue pair init info pointer
3673  *
3674  * Initializes the object and context buffers for a control Queue Pair.
3675  */
3676 int irdma_sc_cqp_init(struct irdma_sc_cqp *cqp,
3677 		      struct irdma_cqp_init_info *info)
3678 {
3679 	struct irdma_ooo_cqp_op *ooo_op;
3680 	u32 num_ooo_ops;
3681 	u8 hw_sq_size;
3682 
3683 	if (info->sq_size > IRDMA_CQP_SW_SQSIZE_2048 ||
3684 	    info->sq_size < IRDMA_CQP_SW_SQSIZE_4 ||
3685 	    ((info->sq_size & (info->sq_size - 1))))
3686 		return -EINVAL;
3687 
3688 	hw_sq_size = irdma_get_encoded_wqe_size(info->sq_size,
3689 						IRDMA_QUEUE_TYPE_CQP);
3690 	cqp->size = sizeof(*cqp);
3691 	cqp->sq_size = info->sq_size;
3692 	cqp->hw_sq_size = hw_sq_size;
3693 	cqp->sq_base = info->sq;
3694 	cqp->host_ctx = info->host_ctx;
3695 	cqp->sq_pa = info->sq_pa;
3696 	cqp->host_ctx_pa = info->host_ctx_pa;
3697 	cqp->dev = info->dev;
3698 	cqp->struct_ver = info->struct_ver;
3699 	cqp->hw_maj_ver = info->hw_maj_ver;
3700 	cqp->hw_min_ver = info->hw_min_ver;
3701 	cqp->scratch_array = info->scratch_array;
3702 	cqp->polarity = 0;
3703 	cqp->en_datacenter_tcp = info->en_datacenter_tcp;
3704 	cqp->ena_vf_count = info->ena_vf_count;
3705 	cqp->hmc_profile = info->hmc_profile;
3706 	cqp->ceqs_per_vf = info->ceqs_per_vf;
3707 	cqp->disable_packed = info->disable_packed;
3708 	cqp->rocev2_rto_policy = info->rocev2_rto_policy;
3709 	cqp->protocol_used = info->protocol_used;
3710 	memcpy(&cqp->dcqcn_params, &info->dcqcn_params, sizeof(cqp->dcqcn_params));
3711 	if (cqp->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
3712 		cqp->ooisc_blksize = info->ooisc_blksize;
3713 		cqp->rrsp_blksize = info->rrsp_blksize;
3714 		cqp->q1_blksize = info->q1_blksize;
3715 		cqp->xmit_blksize = info->xmit_blksize;
3716 		cqp->blksizes_valid = info->blksizes_valid;
3717 		cqp->ts_shift = info->ts_shift;
3718 		cqp->ts_override = info->ts_override;
3719 		cqp->en_fine_grained_timers = info->en_fine_grained_timers;
3720 		cqp->pe_en_vf_cnt = info->pe_en_vf_cnt;
3721 		cqp->ooo_op_array = info->ooo_op_array;
3722 		/* initialize the OOO lists */
3723 		INIT_LIST_HEAD(&cqp->ooo_avail);
3724 		INIT_LIST_HEAD(&cqp->ooo_pnd);
3725 		if (cqp->ooo_op_array) {
3726 			/* Populate avail list entries */
3727 			for (num_ooo_ops = 0, ooo_op = info->ooo_op_array;
3728 			     num_ooo_ops < cqp->sq_size;
3729 			     num_ooo_ops++, ooo_op++)
3730 				list_add(&ooo_op->list_entry, &cqp->ooo_avail);
3731 		}
3732 	}
3733 	info->dev->cqp = cqp;
3734 
3735 	IRDMA_RING_INIT(cqp->sq_ring, cqp->sq_size);
3736 	cqp->last_def_cmpl_ticket = 0;
3737 	cqp->sw_def_cmpl_ticket = 0;
3738 	cqp->requested_ops = 0;
3739 	atomic64_set(&cqp->completed_ops, 0);
3740 	/* for the cqp commands backlog. */
3741 	INIT_LIST_HEAD(&cqp->dev->cqp_cmd_head);
3742 
3743 	writel(0, cqp->dev->hw_regs[IRDMA_CQPTAIL]);
3744 	if (cqp->dev->hw_attrs.uk_attrs.hw_rev <= IRDMA_GEN_2) {
3745 		writel(0, cqp->dev->hw_regs[IRDMA_CQPDB]);
3746 		writel(0, cqp->dev->hw_regs[IRDMA_CCQPSTATUS]);
3747 	}
3748 
3749 	ibdev_dbg(to_ibdev(cqp->dev),
3750 		  "WQE: sq_size[%04d] hw_sq_size[%04d] sq_base[%p] sq_pa[%p] cqp[%p] polarity[x%04x]\n",
3751 		  cqp->sq_size, cqp->hw_sq_size, cqp->sq_base,
3752 		  (u64 *)(uintptr_t)cqp->sq_pa, cqp, cqp->polarity);
3753 	return 0;
3754 }
3755 
3756 /**
3757  * irdma_sc_cqp_create - create cqp during bringup
3758  * @cqp: struct for cqp hw
3759  * @maj_err: If error, major err number
3760  * @min_err: If error, minor err number
3761  */
3762 int irdma_sc_cqp_create(struct irdma_sc_cqp *cqp, u16 *maj_err, u16 *min_err)
3763 {
3764 	u64 temp;
3765 	u8 hw_rev;
3766 	u32 cnt = 0, p1, p2, val = 0, err_code;
3767 	int ret_code;
3768 
3769 	hw_rev = cqp->dev->hw_attrs.uk_attrs.hw_rev;
3770 	cqp->sdbuf.size = ALIGN(IRDMA_UPDATE_SD_BUFF_SIZE * cqp->sq_size,
3771 				IRDMA_SD_BUF_ALIGNMENT);
3772 	cqp->sdbuf.va = dma_alloc_coherent(cqp->dev->hw->device,
3773 					   cqp->sdbuf.size, &cqp->sdbuf.pa,
3774 					   GFP_KERNEL);
3775 	if (!cqp->sdbuf.va)
3776 		return -ENOMEM;
3777 
3778 	spin_lock_init(&cqp->dev->cqp_lock);
3779 	spin_lock_init(&cqp->ooo_list_lock);
3780 
3781 	temp = FIELD_PREP(IRDMA_CQPHC_SQSIZE, cqp->hw_sq_size) |
3782 	       FIELD_PREP(IRDMA_CQPHC_SVER, cqp->struct_ver) |
3783 	       FIELD_PREP(IRDMA_CQPHC_DISABLE_PFPDUS, cqp->disable_packed) |
3784 	       FIELD_PREP(IRDMA_CQPHC_CEQPERVF, cqp->ceqs_per_vf);
3785 	if (hw_rev >= IRDMA_GEN_2) {
3786 		temp |= FIELD_PREP(IRDMA_CQPHC_ROCEV2_RTO_POLICY,
3787 				   cqp->rocev2_rto_policy) |
3788 			FIELD_PREP(IRDMA_CQPHC_PROTOCOL_USED,
3789 				   cqp->protocol_used);
3790 	}
3791 	if (hw_rev >= IRDMA_GEN_3)
3792 		temp |= FIELD_PREP(IRDMA_CQPHC_EN_FINE_GRAINED_TIMERS,
3793 				   cqp->en_fine_grained_timers);
3794 
3795 	set_64bit_val(cqp->host_ctx, 0, temp);
3796 	set_64bit_val(cqp->host_ctx, 8, cqp->sq_pa);
3797 
3798 	temp = FIELD_PREP(IRDMA_CQPHC_ENABLED_VFS, cqp->ena_vf_count) |
3799 	       FIELD_PREP(IRDMA_CQPHC_HMC_PROFILE, cqp->hmc_profile);
3800 
3801 	if (hw_rev >= IRDMA_GEN_3)
3802 		temp |= FIELD_PREP(IRDMA_CQPHC_OOISC_BLKSIZE,
3803 				   cqp->ooisc_blksize) |
3804 			FIELD_PREP(IRDMA_CQPHC_RRSP_BLKSIZE,
3805 				   cqp->rrsp_blksize) |
3806 			FIELD_PREP(IRDMA_CQPHC_Q1_BLKSIZE, cqp->q1_blksize) |
3807 			FIELD_PREP(IRDMA_CQPHC_XMIT_BLKSIZE,
3808 				   cqp->xmit_blksize) |
3809 			FIELD_PREP(IRDMA_CQPHC_BLKSIZES_VALID,
3810 				   cqp->blksizes_valid) |
3811 			FIELD_PREP(IRDMA_CQPHC_TIMESTAMP_OVERRIDE,
3812 				   cqp->ts_override) |
3813 			FIELD_PREP(IRDMA_CQPHC_TS_SHIFT, cqp->ts_shift);
3814 	set_64bit_val(cqp->host_ctx, 16, temp);
3815 	set_64bit_val(cqp->host_ctx, 24, (uintptr_t)cqp);
3816 	temp = FIELD_PREP(IRDMA_CQPHC_HW_MAJVER, cqp->hw_maj_ver) |
3817 	       FIELD_PREP(IRDMA_CQPHC_HW_MINVER, cqp->hw_min_ver);
3818 	if (hw_rev >= IRDMA_GEN_2) {
3819 		temp |= FIELD_PREP(IRDMA_CQPHC_MIN_RATE, cqp->dcqcn_params.min_rate) |
3820 			FIELD_PREP(IRDMA_CQPHC_MIN_DEC_FACTOR, cqp->dcqcn_params.min_dec_factor);
3821 	}
3822 	set_64bit_val(cqp->host_ctx, 32, temp);
3823 	set_64bit_val(cqp->host_ctx, 40, 0);
3824 	temp = 0;
3825 	if (hw_rev >= IRDMA_GEN_2) {
3826 		temp |= FIELD_PREP(IRDMA_CQPHC_DCQCN_T, cqp->dcqcn_params.dcqcn_t) |
3827 			FIELD_PREP(IRDMA_CQPHC_RAI_FACTOR, cqp->dcqcn_params.rai_factor) |
3828 			FIELD_PREP(IRDMA_CQPHC_HAI_FACTOR, cqp->dcqcn_params.hai_factor);
3829 	}
3830 	set_64bit_val(cqp->host_ctx, 48, temp);
3831 	temp = 0;
3832 	if (hw_rev >= IRDMA_GEN_2) {
3833 		temp |= FIELD_PREP(IRDMA_CQPHC_DCQCN_B, cqp->dcqcn_params.dcqcn_b) |
3834 			FIELD_PREP(IRDMA_CQPHC_DCQCN_F, cqp->dcqcn_params.dcqcn_f) |
3835 			FIELD_PREP(IRDMA_CQPHC_CC_CFG_VALID, cqp->dcqcn_params.cc_cfg_valid) |
3836 			FIELD_PREP(IRDMA_CQPHC_RREDUCE_MPERIOD, cqp->dcqcn_params.rreduce_mperiod);
3837 	}
3838 	set_64bit_val(cqp->host_ctx, 56, temp);
3839 	print_hex_dump_debug("WQE: CQP_HOST_CTX WQE", DUMP_PREFIX_OFFSET, 16,
3840 			     8, cqp->host_ctx, IRDMA_CQP_CTX_SIZE * 8, false);
3841 	p1 = cqp->host_ctx_pa >> 32;
3842 	p2 = (u32)cqp->host_ctx_pa;
3843 
3844 	writel(p1, cqp->dev->hw_regs[IRDMA_CCQPHIGH]);
3845 	writel(p2, cqp->dev->hw_regs[IRDMA_CCQPLOW]);
3846 
3847 	do {
3848 		if (cnt++ > cqp->dev->hw_attrs.max_done_count) {
3849 			ret_code = -ETIMEDOUT;
3850 			goto err;
3851 		}
3852 		udelay(cqp->dev->hw_attrs.max_sleep_count);
3853 		val = readl(cqp->dev->hw_regs[IRDMA_CCQPSTATUS]);
3854 	} while (!val);
3855 
3856 	if (FLD_RS_32(cqp->dev, val, IRDMA_CCQPSTATUS_CCQP_ERR)) {
3857 		ret_code = -EOPNOTSUPP;
3858 		goto err;
3859 	}
3860 
3861 	cqp->process_cqp_sds = irdma_update_sds_noccq;
3862 	return 0;
3863 
3864 err:
3865 	dma_free_coherent(cqp->dev->hw->device, cqp->sdbuf.size,
3866 			  cqp->sdbuf.va, cqp->sdbuf.pa);
3867 	cqp->sdbuf.va = NULL;
3868 	err_code = readl(cqp->dev->hw_regs[IRDMA_CQPERRCODES]);
3869 	*min_err = FIELD_GET(IRDMA_CQPERRCODES_CQP_MINOR_CODE, err_code);
3870 	*maj_err = FIELD_GET(IRDMA_CQPERRCODES_CQP_MAJOR_CODE, err_code);
3871 	return ret_code;
3872 }
3873 
3874 /**
3875  * irdma_sc_cqp_post_sq - post of cqp's sq
3876  * @cqp: struct for cqp hw
3877  */
3878 void irdma_sc_cqp_post_sq(struct irdma_sc_cqp *cqp)
3879 {
3880 	writel(IRDMA_RING_CURRENT_HEAD(cqp->sq_ring), cqp->dev->cqp_db);
3881 
3882 	ibdev_dbg(to_ibdev(cqp->dev),
3883 		  "WQE: CQP SQ head 0x%x tail 0x%x size 0x%x\n",
3884 		  cqp->sq_ring.head, cqp->sq_ring.tail, cqp->sq_ring.size);
3885 }
3886 
3887 /**
3888  * irdma_sc_cqp_get_next_send_wqe_idx - get next wqe on cqp sq
3889  * and pass back index
3890  * @cqp: CQP HW structure
3891  * @scratch: private data for CQP WQE
3892  * @wqe_idx: WQE index of CQP SQ
3893  */
3894 __le64 *irdma_sc_cqp_get_next_send_wqe_idx(struct irdma_sc_cqp *cqp, u64 scratch,
3895 					   u32 *wqe_idx)
3896 {
3897 	__le64 *wqe = NULL;
3898 	int ret_code;
3899 
3900 	if (IRDMA_RING_FULL_ERR(cqp->sq_ring)) {
3901 		ibdev_dbg(to_ibdev(cqp->dev),
3902 			  "WQE: CQP SQ is full, head 0x%x tail 0x%x size 0x%x\n",
3903 			  cqp->sq_ring.head, cqp->sq_ring.tail,
3904 			  cqp->sq_ring.size);
3905 		return NULL;
3906 	}
3907 	IRDMA_ATOMIC_RING_MOVE_HEAD(cqp->sq_ring, *wqe_idx, ret_code);
3908 	if (ret_code)
3909 		return NULL;
3910 
3911 	cqp->requested_ops++;
3912 	if (!*wqe_idx)
3913 		cqp->polarity = !cqp->polarity;
3914 	wqe = cqp->sq_base[*wqe_idx].elem;
3915 	cqp->scratch_array[*wqe_idx] = scratch;
3916 	IRDMA_CQP_INIT_WQE(wqe);
3917 
3918 	return wqe;
3919 }
3920 
3921 /**
3922  * irdma_sc_cqp_destroy - destroy cqp during close
3923  * @cqp: struct for cqp hw
3924  */
3925 int irdma_sc_cqp_destroy(struct irdma_sc_cqp *cqp)
3926 {
3927 	u32 cnt = 0, val;
3928 	int ret_code = 0;
3929 
3930 	writel(0, cqp->dev->hw_regs[IRDMA_CCQPHIGH]);
3931 	writel(0, cqp->dev->hw_regs[IRDMA_CCQPLOW]);
3932 	do {
3933 		if (cnt++ > cqp->dev->hw_attrs.max_done_count) {
3934 			ret_code = -ETIMEDOUT;
3935 			break;
3936 		}
3937 		udelay(cqp->dev->hw_attrs.max_sleep_count);
3938 		val = readl(cqp->dev->hw_regs[IRDMA_CCQPSTATUS]);
3939 	} while (FLD_RS_32(cqp->dev, val, IRDMA_CCQPSTATUS_CCQP_DONE));
3940 
3941 	dma_free_coherent(cqp->dev->hw->device, cqp->sdbuf.size,
3942 			  cqp->sdbuf.va, cqp->sdbuf.pa);
3943 	cqp->sdbuf.va = NULL;
3944 	return ret_code;
3945 }
3946 
3947 /**
3948  * irdma_sc_ccq_arm - enable intr for control cq
3949  * @ccq: ccq sc struct
3950  */
3951 void irdma_sc_ccq_arm(struct irdma_sc_cq *ccq)
3952 {
3953 	u64 temp_val;
3954 	u16 sw_cq_sel;
3955 	u8 arm_next_se;
3956 	u8 arm_seq_num;
3957 
3958 	get_64bit_val(ccq->cq_uk.shadow_area, 32, &temp_val);
3959 	sw_cq_sel = (u16)FIELD_GET(IRDMA_CQ_DBSA_SW_CQ_SELECT, temp_val);
3960 	arm_next_se = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_NEXT_SE, temp_val);
3961 	arm_seq_num = (u8)FIELD_GET(IRDMA_CQ_DBSA_ARM_SEQ_NUM, temp_val);
3962 	arm_seq_num++;
3963 	temp_val = FIELD_PREP(IRDMA_CQ_DBSA_ARM_SEQ_NUM, arm_seq_num) |
3964 		   FIELD_PREP(IRDMA_CQ_DBSA_SW_CQ_SELECT, sw_cq_sel) |
3965 		   FIELD_PREP(IRDMA_CQ_DBSA_ARM_NEXT_SE, arm_next_se) |
3966 		   FIELD_PREP(IRDMA_CQ_DBSA_ARM_NEXT, 1);
3967 	set_64bit_val(ccq->cq_uk.shadow_area, 32, temp_val);
3968 
3969 	dma_wmb(); /* make sure shadow area is updated before arming */
3970 
3971 	writel(ccq->cq_uk.cq_id, ccq->dev->cq_arm_db);
3972 }
3973 
3974 /**
3975  * irdma_sc_process_def_cmpl - process deferred or pending completion
3976  * @cqp: CQP sc struct
3977  * @info: CQP CQE info
3978  * @wqe_idx: CQP WQE descriptor index
3979  * @def_info: deferred op ticket value or out-of-order completion id
3980  * @def_cmpl: true for deferred completion, false for pending (RCA)
3981  */
3982 static void irdma_sc_process_def_cmpl(struct irdma_sc_cqp *cqp,
3983 				      struct irdma_ccq_cqe_info *info,
3984 				      u32 wqe_idx, u32 def_info, bool def_cmpl)
3985 {
3986 	struct irdma_ooo_cqp_op *ooo_op;
3987 	unsigned long flags;
3988 
3989 	/* Deferred and out-of-order completions share the same list of pending
3990 	 * completions.  Since the list can be also accessed from AE handler,
3991 	 * it must be protected by a lock.
3992 	 */
3993 	spin_lock_irqsave(&cqp->ooo_list_lock, flags);
3994 
3995 	/* For deferred completions bump up SW completion ticket value. */
3996 	if (def_cmpl) {
3997 		cqp->last_def_cmpl_ticket = def_info;
3998 		cqp->sw_def_cmpl_ticket++;
3999 	}
4000 	if (!list_empty(&cqp->ooo_avail)) {
4001 		ooo_op = (struct irdma_ooo_cqp_op *)
4002 			 list_entry(cqp->ooo_avail.next,
4003 				    struct irdma_ooo_cqp_op, list_entry);
4004 
4005 		list_del(&ooo_op->list_entry);
4006 		ooo_op->scratch = info->scratch;
4007 		ooo_op->def_info = def_info;
4008 		ooo_op->sw_def_info = cqp->sw_def_cmpl_ticket;
4009 		ooo_op->deferred = def_cmpl;
4010 		ooo_op->wqe_idx = wqe_idx;
4011 		/* Pending completions must be chronologically ordered,
4012 		 * so adding at the end of list.
4013 		 */
4014 		list_add_tail(&ooo_op->list_entry, &cqp->ooo_pnd);
4015 	}
4016 	spin_unlock_irqrestore(&cqp->ooo_list_lock, flags);
4017 
4018 	info->pending = true;
4019 }
4020 
4021 /**
4022  * irdma_sc_process_ooo_cmpl - process out-of-order (final) completion
4023  * @cqp: CQP sc struct
4024  * @info: CQP CQE info
4025  * @def_info: out-of-order completion id
4026  */
4027 static void irdma_sc_process_ooo_cmpl(struct irdma_sc_cqp *cqp,
4028 				      struct irdma_ccq_cqe_info *info,
4029 				      u32 def_info)
4030 {
4031 	struct irdma_ooo_cqp_op *ooo_op_tmp;
4032 	struct irdma_ooo_cqp_op *ooo_op;
4033 	unsigned long flags;
4034 
4035 	info->scratch = 0;
4036 
4037 	spin_lock_irqsave(&cqp->ooo_list_lock, flags);
4038 	list_for_each_entry_safe(ooo_op, ooo_op_tmp, &cqp->ooo_pnd,
4039 				 list_entry) {
4040 		if (!ooo_op->deferred && ooo_op->def_info == def_info) {
4041 			list_del(&ooo_op->list_entry);
4042 			info->scratch = ooo_op->scratch;
4043 			list_add(&ooo_op->list_entry, &cqp->ooo_avail);
4044 			break;
4045 		}
4046 	}
4047 	spin_unlock_irqrestore(&cqp->ooo_list_lock, flags);
4048 
4049 	if (!info->scratch)
4050 		ibdev_dbg(to_ibdev(cqp->dev),
4051 			  "CQP: DEBUG_FW_OOO out-of-order completion with unknown def_info = 0x%x\n",
4052 			  def_info);
4053 }
4054 
4055 /**
4056  * irdma_sc_ccq_get_cqe_info - get ccq's cq entry
4057  * @ccq: ccq sc struct
4058  * @info: completion q entry to return
4059  */
4060 int irdma_sc_ccq_get_cqe_info(struct irdma_sc_cq *ccq,
4061 			      struct irdma_ccq_cqe_info *info)
4062 {
4063 	u32 def_info;
4064 	bool def_cmpl = false;
4065 	bool pend_cmpl = false;
4066 	bool ooo_final_cmpl = false;
4067 	u64 qp_ctx, temp, temp1;
4068 	__le64 *cqe;
4069 	struct irdma_sc_cqp *cqp;
4070 	u32 wqe_idx;
4071 	u32 error;
4072 	u8 polarity;
4073 	int ret_code = 0;
4074 	unsigned long flags;
4075 
4076 	if (ccq->cq_uk.avoid_mem_cflct)
4077 		cqe = IRDMA_GET_CURRENT_EXTENDED_CQ_ELEM(&ccq->cq_uk);
4078 	else
4079 		cqe = IRDMA_GET_CURRENT_CQ_ELEM(&ccq->cq_uk);
4080 
4081 	get_64bit_val(cqe, 24, &temp);
4082 	polarity = (u8)FIELD_GET(IRDMA_CQ_VALID, temp);
4083 	if (polarity != ccq->cq_uk.polarity)
4084 		return -ENOENT;
4085 
4086 	/* Ensure CEQE contents are read after valid bit is checked */
4087 	dma_rmb();
4088 
4089 	get_64bit_val(cqe, 8, &qp_ctx);
4090 	cqp = (struct irdma_sc_cqp *)(unsigned long)qp_ctx;
4091 	info->error = (bool)FIELD_GET(IRDMA_CQ_ERROR, temp);
4092 	info->maj_err_code = IRDMA_CQPSQ_MAJ_NO_ERROR;
4093 	info->min_err_code = (u16)FIELD_GET(IRDMA_CQ_MINERR, temp);
4094 	if (info->error) {
4095 		info->maj_err_code = (u16)FIELD_GET(IRDMA_CQ_MAJERR, temp);
4096 		error = readl(cqp->dev->hw_regs[IRDMA_CQPERRCODES]);
4097 		ibdev_dbg(to_ibdev(cqp->dev),
4098 			  "CQP: CQPERRCODES error_code[x%08X]\n", error);
4099 	}
4100 
4101 	wqe_idx = (u32)FIELD_GET(IRDMA_CQ_WQEIDX, temp);
4102 	info->scratch = cqp->scratch_array[wqe_idx];
4103 
4104 	get_64bit_val(cqe, 16, &temp1);
4105 	info->op_ret_val = (u32)FIELD_GET(IRDMA_CCQ_OPRETVAL, temp1);
4106 	if (cqp->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
4107 		def_cmpl = info->maj_err_code == IRDMA_CQPSQ_MAJ_NO_ERROR &&
4108 			   info->min_err_code == IRDMA_CQPSQ_MIN_DEF_CMPL;
4109 		def_info = (u32)FIELD_GET(IRDMA_CCQ_DEFINFO, temp1);
4110 
4111 		pend_cmpl = info->maj_err_code == IRDMA_CQPSQ_MAJ_NO_ERROR &&
4112 			    info->min_err_code == IRDMA_CQPSQ_MIN_OOO_CMPL;
4113 
4114 		ooo_final_cmpl = (bool)FIELD_GET(IRDMA_OOO_CMPL, temp);
4115 
4116 		if (def_cmpl || pend_cmpl || ooo_final_cmpl) {
4117 			if (ooo_final_cmpl)
4118 				irdma_sc_process_ooo_cmpl(cqp, info, def_info);
4119 			else
4120 				irdma_sc_process_def_cmpl(cqp, info, wqe_idx,
4121 							  def_info, def_cmpl);
4122 		}
4123 	}
4124 
4125 	get_64bit_val(cqp->sq_base[wqe_idx].elem, 24, &temp1);
4126 	info->op_code = (u8)FIELD_GET(IRDMA_CQPSQ_OPCODE, temp1);
4127 	info->cqp = cqp;
4128 
4129 	/*  move the head for cq */
4130 	IRDMA_RING_MOVE_HEAD(ccq->cq_uk.cq_ring, ret_code);
4131 	if (!IRDMA_RING_CURRENT_HEAD(ccq->cq_uk.cq_ring))
4132 		ccq->cq_uk.polarity ^= 1;
4133 
4134 	/* update cq tail in cq shadow memory also */
4135 	IRDMA_RING_MOVE_TAIL(ccq->cq_uk.cq_ring);
4136 	set_64bit_val(ccq->cq_uk.shadow_area, 0,
4137 		      IRDMA_RING_CURRENT_HEAD(ccq->cq_uk.cq_ring));
4138 
4139 	dma_wmb(); /* make sure shadow area is updated before moving tail */
4140 
4141 	spin_lock_irqsave(&cqp->dev->cqp_lock, flags);
4142 	if (!ooo_final_cmpl)
4143 		IRDMA_RING_MOVE_TAIL(cqp->sq_ring);
4144 	spin_unlock_irqrestore(&cqp->dev->cqp_lock, flags);
4145 
4146 	/* Do not increment completed_ops counter on pending or deferred
4147 	 * completions.
4148 	 */
4149 	if (pend_cmpl || def_cmpl)
4150 		return ret_code;
4151 	atomic64_inc(&cqp->completed_ops);
4152 
4153 	return ret_code;
4154 }
4155 
4156 /**
4157  * irdma_sc_poll_for_cqp_op_done - Waits for last write to complete in CQP SQ
4158  * @cqp: struct for cqp hw
4159  * @op_code: cqp opcode for completion
4160  * @compl_info: completion q entry to return
4161  */
4162 int irdma_sc_poll_for_cqp_op_done(struct irdma_sc_cqp *cqp, u8 op_code,
4163 				  struct irdma_ccq_cqe_info *compl_info)
4164 {
4165 	struct irdma_ccq_cqe_info info = {};
4166 	struct irdma_sc_cq *ccq;
4167 	int ret_code = 0;
4168 	u32 cnt = 0;
4169 
4170 	ccq = cqp->dev->ccq;
4171 	while (1) {
4172 		if (cnt++ > 100 * cqp->dev->hw_attrs.max_done_count)
4173 			return -ETIMEDOUT;
4174 
4175 		if (irdma_sc_ccq_get_cqe_info(ccq, &info)) {
4176 			udelay(cqp->dev->hw_attrs.max_sleep_count);
4177 			continue;
4178 		}
4179 		if (info.error && info.op_code != IRDMA_CQP_OP_QUERY_STAG) {
4180 			ret_code = -EIO;
4181 			break;
4182 		}
4183 		/* make sure op code matches*/
4184 		if (op_code == info.op_code)
4185 			break;
4186 		ibdev_dbg(to_ibdev(cqp->dev),
4187 			  "WQE: opcode mismatch for my op code 0x%x, returned opcode %x\n",
4188 			  op_code, info.op_code);
4189 	}
4190 
4191 	if (compl_info)
4192 		memcpy(compl_info, &info, sizeof(*compl_info));
4193 
4194 	return ret_code;
4195 }
4196 
4197 /**
4198  * irdma_sc_manage_hmc_pm_func_table - manage of function table
4199  * @cqp: struct for cqp hw
4200  * @scratch: u64 saved to be used during cqp completion
4201  * @info: info for the manage function table operation
4202  * @post_sq: flag for cqp db to ring
4203  */
4204 static int irdma_sc_manage_hmc_pm_func_table(struct irdma_sc_cqp *cqp,
4205 					     struct irdma_hmc_fcn_info *info,
4206 					     u64 scratch, bool post_sq)
4207 {
4208 	__le64 *wqe;
4209 	u64 hdr;
4210 
4211 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4212 	if (!wqe)
4213 		return -ENOMEM;
4214 
4215 	set_64bit_val(wqe, 0, 0);
4216 	set_64bit_val(wqe, 8, 0);
4217 	set_64bit_val(wqe, 16, 0);
4218 	set_64bit_val(wqe, 32, 0);
4219 	set_64bit_val(wqe, 40, 0);
4220 	set_64bit_val(wqe, 48, 0);
4221 	set_64bit_val(wqe, 56, 0);
4222 
4223 	hdr = FIELD_PREP(IRDMA_CQPSQ_MHMC_VFIDX, info->vf_id) |
4224 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE,
4225 			 IRDMA_CQP_OP_MANAGE_HMC_PM_FUNC_TABLE) |
4226 	      FIELD_PREP(IRDMA_CQPSQ_MHMC_FREEPMFN, info->free_fcn) |
4227 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4228 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4229 
4230 	set_64bit_val(wqe, 24, hdr);
4231 
4232 	print_hex_dump_debug("WQE: MANAGE_HMC_PM_FUNC_TABLE WQE",
4233 			     DUMP_PREFIX_OFFSET, 16, 8, wqe,
4234 			     IRDMA_CQP_WQE_SIZE * 8, false);
4235 	if (post_sq)
4236 		irdma_sc_cqp_post_sq(cqp);
4237 
4238 	return 0;
4239 }
4240 
4241 /**
4242  * irdma_sc_commit_fpm_val_done - wait for cqp eqe completion
4243  * for fpm commit
4244  * @cqp: struct for cqp hw
4245  */
4246 static int irdma_sc_commit_fpm_val_done(struct irdma_sc_cqp *cqp)
4247 {
4248 	return irdma_sc_poll_for_cqp_op_done(cqp, IRDMA_CQP_OP_COMMIT_FPM_VAL,
4249 					     NULL);
4250 }
4251 
4252 /**
4253  * irdma_sc_commit_fpm_val - cqp wqe for commit fpm values
4254  * @cqp: struct for cqp hw
4255  * @scratch: u64 saved to be used during cqp completion
4256  * @hmc_fn_id: hmc function id
4257  * @commit_fpm_mem: Memory for fpm values
4258  * @post_sq: flag for cqp db to ring
4259  * @wait_type: poll ccq or cqp registers for cqp completion
4260  */
4261 static int irdma_sc_commit_fpm_val(struct irdma_sc_cqp *cqp, u64 scratch,
4262 				   u8 hmc_fn_id,
4263 				   struct irdma_dma_mem *commit_fpm_mem,
4264 				   bool post_sq, u8 wait_type)
4265 {
4266 	__le64 *wqe;
4267 	u64 hdr;
4268 	u32 tail, val, error;
4269 	int ret_code = 0;
4270 
4271 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4272 	if (!wqe)
4273 		return -ENOMEM;
4274 
4275 	set_64bit_val(wqe, 16, hmc_fn_id);
4276 	set_64bit_val(wqe, 32, commit_fpm_mem->pa);
4277 
4278 	hdr = FIELD_PREP(IRDMA_CQPSQ_BUFSIZE, IRDMA_COMMIT_FPM_BUF_SIZE) |
4279 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_COMMIT_FPM_VAL) |
4280 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4281 
4282 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4283 
4284 	set_64bit_val(wqe, 24, hdr);
4285 
4286 	print_hex_dump_debug("WQE: COMMIT_FPM_VAL WQE", DUMP_PREFIX_OFFSET,
4287 			     16, 8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
4288 	irdma_get_cqp_reg_info(cqp, &val, &tail, &error);
4289 
4290 	if (post_sq) {
4291 		irdma_sc_cqp_post_sq(cqp);
4292 		if (wait_type == IRDMA_CQP_WAIT_POLL_REGS)
4293 			ret_code = irdma_cqp_poll_registers(cqp, tail,
4294 							    cqp->dev->hw_attrs.max_done_count);
4295 		else if (wait_type == IRDMA_CQP_WAIT_POLL_CQ)
4296 			ret_code = irdma_sc_commit_fpm_val_done(cqp);
4297 	}
4298 
4299 	return ret_code;
4300 }
4301 
4302 /**
4303  * irdma_sc_query_fpm_val_done - poll for cqp wqe completion for
4304  * query fpm
4305  * @cqp: struct for cqp hw
4306  */
4307 static int irdma_sc_query_fpm_val_done(struct irdma_sc_cqp *cqp)
4308 {
4309 	return irdma_sc_poll_for_cqp_op_done(cqp, IRDMA_CQP_OP_QUERY_FPM_VAL,
4310 					     NULL);
4311 }
4312 
4313 /**
4314  * irdma_sc_query_fpm_val - cqp wqe query fpm values
4315  * @cqp: struct for cqp hw
4316  * @scratch: u64 saved to be used during cqp completion
4317  * @hmc_fn_id: hmc function id
4318  * @query_fpm_mem: memory for return fpm values
4319  * @post_sq: flag for cqp db to ring
4320  * @wait_type: poll ccq or cqp registers for cqp completion
4321  */
4322 static int irdma_sc_query_fpm_val(struct irdma_sc_cqp *cqp, u64 scratch,
4323 				  u8 hmc_fn_id,
4324 				  struct irdma_dma_mem *query_fpm_mem,
4325 				  bool post_sq, u8 wait_type)
4326 {
4327 	__le64 *wqe;
4328 	u64 hdr;
4329 	u32 tail, val, error;
4330 	int ret_code = 0;
4331 
4332 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4333 	if (!wqe)
4334 		return -ENOMEM;
4335 
4336 	set_64bit_val(wqe, 16, hmc_fn_id);
4337 	set_64bit_val(wqe, 32, query_fpm_mem->pa);
4338 
4339 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_QUERY_FPM_VAL) |
4340 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4341 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4342 
4343 	set_64bit_val(wqe, 24, hdr);
4344 
4345 	print_hex_dump_debug("WQE: QUERY_FPM WQE", DUMP_PREFIX_OFFSET, 16, 8,
4346 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
4347 	irdma_get_cqp_reg_info(cqp, &val, &tail, &error);
4348 
4349 	if (post_sq) {
4350 		irdma_sc_cqp_post_sq(cqp);
4351 		if (wait_type == IRDMA_CQP_WAIT_POLL_REGS)
4352 			ret_code = irdma_cqp_poll_registers(cqp, tail,
4353 							    cqp->dev->hw_attrs.max_done_count);
4354 		else if (wait_type == IRDMA_CQP_WAIT_POLL_CQ)
4355 			ret_code = irdma_sc_query_fpm_val_done(cqp);
4356 	}
4357 
4358 	return ret_code;
4359 }
4360 
4361 /**
4362  * irdma_sc_ceq_init - initialize ceq
4363  * @ceq: ceq sc structure
4364  * @info: ceq initialization info
4365  */
4366 int irdma_sc_ceq_init(struct irdma_sc_ceq *ceq,
4367 		      struct irdma_ceq_init_info *info)
4368 {
4369 	u32 pble_obj_cnt;
4370 
4371 	if (info->elem_cnt < info->dev->hw_attrs.min_hw_ceq_size ||
4372 	    info->elem_cnt > info->dev->hw_attrs.max_hw_ceq_size)
4373 		return -EINVAL;
4374 
4375 	if (info->ceq_id >= info->dev->hmc_fpm_misc.max_ceqs)
4376 		return -EINVAL;
4377 	pble_obj_cnt = info->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
4378 
4379 	if (info->virtual_map && info->first_pm_pbl_idx >= pble_obj_cnt)
4380 		return -EINVAL;
4381 
4382 	ceq->size = sizeof(*ceq);
4383 	ceq->ceqe_base = (struct irdma_ceqe *)info->ceqe_base;
4384 	ceq->ceq_id = info->ceq_id;
4385 	ceq->dev = info->dev;
4386 	ceq->elem_cnt = info->elem_cnt;
4387 	ceq->ceq_elem_pa = info->ceqe_pa;
4388 	ceq->virtual_map = info->virtual_map;
4389 	ceq->itr_no_expire = info->itr_no_expire;
4390 	ceq->reg_cq = info->reg_cq;
4391 	ceq->reg_cq_size = 0;
4392 	spin_lock_init(&ceq->req_cq_lock);
4393 	ceq->pbl_chunk_size = (ceq->virtual_map ? info->pbl_chunk_size : 0);
4394 	ceq->first_pm_pbl_idx = (ceq->virtual_map ? info->first_pm_pbl_idx : 0);
4395 	ceq->pbl_list = (ceq->virtual_map ? info->pbl_list : NULL);
4396 	ceq->tph_en = info->tph_en;
4397 	ceq->tph_val = info->tph_val;
4398 	ceq->vsi_idx = info->vsi_idx;
4399 	ceq->polarity = 1;
4400 	IRDMA_RING_INIT(ceq->ceq_ring, ceq->elem_cnt);
4401 	ceq->dev->ceq[info->ceq_id] = ceq;
4402 
4403 	return 0;
4404 }
4405 
4406 /**
4407  * irdma_sc_ceq_create - create ceq wqe
4408  * @ceq: ceq sc structure
4409  * @scratch: u64 saved to be used during cqp completion
4410  * @post_sq: flag for cqp db to ring
4411  */
4412 
4413 static int irdma_sc_ceq_create(struct irdma_sc_ceq *ceq, u64 scratch,
4414 			       bool post_sq)
4415 {
4416 	struct irdma_sc_cqp *cqp;
4417 	__le64 *wqe;
4418 	u64 hdr;
4419 
4420 	cqp = ceq->dev->cqp;
4421 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4422 	if (!wqe)
4423 		return -ENOMEM;
4424 	set_64bit_val(wqe, 16, ceq->elem_cnt);
4425 	set_64bit_val(wqe, 32,
4426 		      (ceq->virtual_map ? 0 : ceq->ceq_elem_pa));
4427 	set_64bit_val(wqe, 48,
4428 		      (ceq->virtual_map ? ceq->first_pm_pbl_idx : 0));
4429 	set_64bit_val(wqe, 56,
4430 		      FIELD_PREP(IRDMA_CQPSQ_TPHVAL, ceq->tph_val) |
4431 		      FIELD_PREP(IRDMA_CQPSQ_PASID, ceq->pasid) |
4432 		      FIELD_PREP(IRDMA_CQPSQ_VSIIDX, ceq->vsi_idx));
4433 	hdr = FIELD_PREP(IRDMA_CQPSQ_CEQ_CEQID, ceq->ceq_id) |
4434 	      FIELD_PREP(IRDMA_CQPSQ_CEQ_CEQID_HIGH, ceq->ceq_id >> 10) |
4435 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_CREATE_CEQ) |
4436 	      FIELD_PREP(IRDMA_CQPSQ_CEQ_LPBLSIZE, ceq->pbl_chunk_size) |
4437 	      FIELD_PREP(IRDMA_CQPSQ_CEQ_VMAP, ceq->virtual_map) |
4438 	      FIELD_PREP(IRDMA_CQPSQ_CEQ_ITRNOEXPIRE, ceq->itr_no_expire) |
4439 	      FIELD_PREP(IRDMA_CQPSQ_TPHEN, ceq->tph_en) |
4440 	      FIELD_PREP(IRDMA_CQPSQ_PASID_VALID, ceq->pasid_valid) |
4441 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4442 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4443 
4444 	set_64bit_val(wqe, 24, hdr);
4445 
4446 	print_hex_dump_debug("WQE: CEQ_CREATE WQE", DUMP_PREFIX_OFFSET, 16, 8,
4447 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
4448 	if (post_sq)
4449 		irdma_sc_cqp_post_sq(cqp);
4450 
4451 	return 0;
4452 }
4453 
4454 /**
4455  * irdma_sc_cceq_create_done - poll for control ceq wqe to complete
4456  * @ceq: ceq sc structure
4457  */
4458 static int irdma_sc_cceq_create_done(struct irdma_sc_ceq *ceq)
4459 {
4460 	struct irdma_sc_cqp *cqp;
4461 
4462 	cqp = ceq->dev->cqp;
4463 	return irdma_sc_poll_for_cqp_op_done(cqp, IRDMA_CQP_OP_CREATE_CEQ,
4464 					     NULL);
4465 }
4466 
4467 /**
4468  * irdma_sc_cceq_destroy_done - poll for destroy cceq to complete
4469  * @ceq: ceq sc structure
4470  */
4471 int irdma_sc_cceq_destroy_done(struct irdma_sc_ceq *ceq)
4472 {
4473 	struct irdma_sc_cqp *cqp;
4474 
4475 	if (ceq->reg_cq)
4476 		irdma_sc_remove_cq_ctx(ceq, ceq->dev->ccq);
4477 
4478 	cqp = ceq->dev->cqp;
4479 	cqp->process_cqp_sds = irdma_update_sds_noccq;
4480 
4481 	return irdma_sc_poll_for_cqp_op_done(cqp, IRDMA_CQP_OP_DESTROY_CEQ,
4482 					     NULL);
4483 }
4484 
4485 /**
4486  * irdma_sc_cceq_create - create cceq
4487  * @ceq: ceq sc structure
4488  * @scratch: u64 saved to be used during cqp completion
4489  */
4490 int irdma_sc_cceq_create(struct irdma_sc_ceq *ceq, u64 scratch)
4491 {
4492 	int ret_code;
4493 	struct irdma_sc_dev *dev = ceq->dev;
4494 
4495 	dev->ccq->vsi_idx = ceq->vsi_idx;
4496 	if (ceq->reg_cq) {
4497 		ret_code = irdma_sc_add_cq_ctx(ceq, ceq->dev->ccq);
4498 		if (ret_code)
4499 			return ret_code;
4500 	}
4501 
4502 	ret_code = irdma_sc_ceq_create(ceq, scratch, true);
4503 	if (!ret_code)
4504 		return irdma_sc_cceq_create_done(ceq);
4505 
4506 	return ret_code;
4507 }
4508 
4509 /**
4510  * irdma_sc_ceq_destroy - destroy ceq
4511  * @ceq: ceq sc structure
4512  * @scratch: u64 saved to be used during cqp completion
4513  * @post_sq: flag for cqp db to ring
4514  */
4515 int irdma_sc_ceq_destroy(struct irdma_sc_ceq *ceq, u64 scratch, bool post_sq)
4516 {
4517 	struct irdma_sc_cqp *cqp;
4518 	__le64 *wqe;
4519 	u64 hdr;
4520 
4521 	cqp = ceq->dev->cqp;
4522 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4523 	if (!wqe)
4524 		return -ENOMEM;
4525 
4526 	set_64bit_val(wqe, 16, ceq->elem_cnt);
4527 	set_64bit_val(wqe, 48, ceq->first_pm_pbl_idx);
4528 	set_64bit_val(wqe, 56,
4529 		      FIELD_PREP(IRDMA_CQPSQ_PASID, ceq->pasid));
4530 	hdr = ceq->ceq_id |
4531 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DESTROY_CEQ) |
4532 	      FIELD_PREP(IRDMA_CQPSQ_CEQ_LPBLSIZE, ceq->pbl_chunk_size) |
4533 	      FIELD_PREP(IRDMA_CQPSQ_CEQ_VMAP, ceq->virtual_map) |
4534 	      FIELD_PREP(IRDMA_CQPSQ_TPHEN, ceq->tph_en) |
4535 	      FIELD_PREP(IRDMA_CQPSQ_PASID_VALID, ceq->pasid_valid) |
4536 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4537 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4538 
4539 	set_64bit_val(wqe, 24, hdr);
4540 
4541 	print_hex_dump_debug("WQE: CEQ_DESTROY WQE", DUMP_PREFIX_OFFSET, 16,
4542 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
4543 	if (post_sq)
4544 		irdma_sc_cqp_post_sq(cqp);
4545 
4546 	return 0;
4547 }
4548 
4549 /**
4550  * irdma_sc_process_ceq - process ceq
4551  * @dev: sc device struct
4552  * @ceq: ceq sc structure
4553  *
4554  * It is expected caller serializes this function with cleanup_ceqes()
4555  * because these functions manipulate the same ceq
4556  */
4557 void *irdma_sc_process_ceq(struct irdma_sc_dev *dev, struct irdma_sc_ceq *ceq)
4558 {
4559 	u64 temp;
4560 	__le64 *ceqe;
4561 	struct irdma_sc_cq *cq = NULL;
4562 	struct irdma_sc_cq *temp_cq;
4563 	u8 polarity;
4564 	u32 cq_idx;
4565 	unsigned long flags;
4566 
4567 	do {
4568 		cq_idx = 0;
4569 		ceqe = IRDMA_GET_CURRENT_CEQ_ELEM(ceq);
4570 		get_64bit_val(ceqe, 0, &temp);
4571 		polarity = (u8)FIELD_GET(IRDMA_CEQE_VALID, temp);
4572 		if (polarity != ceq->polarity)
4573 			return NULL;
4574 
4575 		temp_cq = (struct irdma_sc_cq *)(unsigned long)(temp << 1);
4576 		if (!temp_cq) {
4577 			cq_idx = IRDMA_INVALID_CQ_IDX;
4578 			IRDMA_RING_MOVE_TAIL(ceq->ceq_ring);
4579 
4580 			if (!IRDMA_RING_CURRENT_TAIL(ceq->ceq_ring))
4581 				ceq->polarity ^= 1;
4582 			continue;
4583 		}
4584 
4585 		cq = temp_cq;
4586 		if (ceq->reg_cq) {
4587 			spin_lock_irqsave(&ceq->req_cq_lock, flags);
4588 			cq_idx = irdma_sc_find_reg_cq(ceq, cq);
4589 			spin_unlock_irqrestore(&ceq->req_cq_lock, flags);
4590 		}
4591 
4592 		IRDMA_RING_MOVE_TAIL(ceq->ceq_ring);
4593 		if (!IRDMA_RING_CURRENT_TAIL(ceq->ceq_ring))
4594 			ceq->polarity ^= 1;
4595 	} while (cq_idx == IRDMA_INVALID_CQ_IDX);
4596 
4597 	if (cq)
4598 		irdma_sc_cq_ack(cq);
4599 	return cq;
4600 }
4601 
4602 /**
4603  * irdma_sc_cleanup_ceqes - clear the valid ceqes ctx matching the cq
4604  * @cq: cq for which the ceqes need to be cleaned up
4605  * @ceq: ceq ptr
4606  *
4607  * The function is called after the cq is destroyed to cleanup
4608  * its pending ceqe entries. It is expected caller serializes this
4609  * function with process_ceq() in interrupt context.
4610  */
4611 void irdma_sc_cleanup_ceqes(struct irdma_sc_cq *cq, struct irdma_sc_ceq *ceq)
4612 {
4613 	struct irdma_sc_cq *next_cq;
4614 	u8 ceq_polarity = ceq->polarity;
4615 	__le64 *ceqe;
4616 	u8 polarity;
4617 	u64 temp;
4618 	int next;
4619 	u32 i;
4620 
4621 	next = IRDMA_RING_GET_NEXT_TAIL(ceq->ceq_ring, 0);
4622 
4623 	for (i = 1; i <= IRDMA_RING_SIZE(*ceq); i++) {
4624 		ceqe = IRDMA_GET_CEQ_ELEM_AT_POS(ceq, next);
4625 
4626 		get_64bit_val(ceqe, 0, &temp);
4627 		polarity = (u8)FIELD_GET(IRDMA_CEQE_VALID, temp);
4628 		if (polarity != ceq_polarity)
4629 			return;
4630 
4631 		next_cq = (struct irdma_sc_cq *)(unsigned long)(temp << 1);
4632 		if (cq == next_cq)
4633 			set_64bit_val(ceqe, 0, temp & IRDMA_CEQE_VALID);
4634 
4635 		next = IRDMA_RING_GET_NEXT_TAIL(ceq->ceq_ring, i);
4636 		if (!next)
4637 			ceq_polarity ^= 1;
4638 	}
4639 }
4640 
4641 /**
4642  * irdma_sc_aeq_init - initialize aeq
4643  * @aeq: aeq structure ptr
4644  * @info: aeq initialization info
4645  */
4646 int irdma_sc_aeq_init(struct irdma_sc_aeq *aeq,
4647 		      struct irdma_aeq_init_info *info)
4648 {
4649 	u32 pble_obj_cnt;
4650 
4651 	if (info->elem_cnt < info->dev->hw_attrs.min_hw_aeq_size ||
4652 	    info->elem_cnt > info->dev->hw_attrs.max_hw_aeq_size)
4653 		return -EINVAL;
4654 
4655 	pble_obj_cnt = info->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
4656 
4657 	if (info->virtual_map && info->first_pm_pbl_idx >= pble_obj_cnt)
4658 		return -EINVAL;
4659 
4660 	aeq->size = sizeof(*aeq);
4661 	aeq->polarity = 1;
4662 	aeq->aeqe_base = (struct irdma_sc_aeqe *)info->aeqe_base;
4663 	aeq->dev = info->dev;
4664 	aeq->elem_cnt = info->elem_cnt;
4665 	aeq->aeq_elem_pa = info->aeq_elem_pa;
4666 	IRDMA_RING_INIT(aeq->aeq_ring, aeq->elem_cnt);
4667 	aeq->virtual_map = info->virtual_map;
4668 	aeq->pbl_list = (aeq->virtual_map ? info->pbl_list : NULL);
4669 	aeq->pbl_chunk_size = (aeq->virtual_map ? info->pbl_chunk_size : 0);
4670 	aeq->first_pm_pbl_idx = (aeq->virtual_map ? info->first_pm_pbl_idx : 0);
4671 	aeq->msix_idx = info->msix_idx;
4672 	info->dev->aeq = aeq;
4673 
4674 	return 0;
4675 }
4676 
4677 /**
4678  * irdma_sc_aeq_create - create aeq
4679  * @aeq: aeq structure ptr
4680  * @scratch: u64 saved to be used during cqp completion
4681  * @post_sq: flag for cqp db to ring
4682  */
4683 static int irdma_sc_aeq_create(struct irdma_sc_aeq *aeq, u64 scratch,
4684 			       bool post_sq)
4685 {
4686 	__le64 *wqe;
4687 	struct irdma_sc_cqp *cqp;
4688 	u64 hdr;
4689 
4690 	cqp = aeq->dev->cqp;
4691 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4692 	if (!wqe)
4693 		return -ENOMEM;
4694 	set_64bit_val(wqe, 16, aeq->elem_cnt);
4695 	set_64bit_val(wqe, 32,
4696 		      (aeq->virtual_map ? 0 : aeq->aeq_elem_pa));
4697 	set_64bit_val(wqe, 48,
4698 		      (aeq->virtual_map ? aeq->first_pm_pbl_idx : 0));
4699 	set_64bit_val(wqe, 56,
4700 		      FIELD_PREP(IRDMA_CQPSQ_PASID, aeq->pasid));
4701 
4702 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_CREATE_AEQ) |
4703 	      FIELD_PREP(IRDMA_CQPSQ_AEQ_LPBLSIZE, aeq->pbl_chunk_size) |
4704 	      FIELD_PREP(IRDMA_CQPSQ_AEQ_VMAP, aeq->virtual_map) |
4705 	      FIELD_PREP(IRDMA_CQPSQ_PASID_VALID, aeq->pasid_valid) |
4706 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4707 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4708 
4709 	set_64bit_val(wqe, 24, hdr);
4710 
4711 	print_hex_dump_debug("WQE: AEQ_CREATE WQE", DUMP_PREFIX_OFFSET, 16, 8,
4712 			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
4713 	if (post_sq)
4714 		irdma_sc_cqp_post_sq(cqp);
4715 
4716 	return 0;
4717 }
4718 
4719 /**
4720  * irdma_sc_aeq_destroy - destroy aeq during close
4721  * @aeq: aeq structure ptr
4722  * @scratch: u64 saved to be used during cqp completion
4723  * @post_sq: flag for cqp db to ring
4724  */
4725 static int irdma_sc_aeq_destroy(struct irdma_sc_aeq *aeq, u64 scratch,
4726 				bool post_sq)
4727 {
4728 	__le64 *wqe;
4729 	struct irdma_sc_cqp *cqp;
4730 	struct irdma_sc_dev *dev;
4731 	u64 hdr;
4732 
4733 	dev = aeq->dev;
4734 	if (dev->privileged)
4735 		writel(0, dev->hw_regs[IRDMA_PFINT_AEQCTL]);
4736 
4737 	cqp = dev->cqp;
4738 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
4739 	if (!wqe)
4740 		return -ENOMEM;
4741 	set_64bit_val(wqe, 16, aeq->elem_cnt);
4742 	set_64bit_val(wqe, 48, aeq->first_pm_pbl_idx);
4743 	set_64bit_val(wqe, 56,
4744 		      FIELD_PREP(IRDMA_CQPSQ_PASID, aeq->pasid));
4745 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DESTROY_AEQ) |
4746 	      FIELD_PREP(IRDMA_CQPSQ_AEQ_LPBLSIZE, aeq->pbl_chunk_size) |
4747 	      FIELD_PREP(IRDMA_CQPSQ_AEQ_VMAP, aeq->virtual_map) |
4748 	      FIELD_PREP(IRDMA_CQPSQ_PASID_VALID, aeq->pasid_valid) |
4749 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
4750 	dma_wmb(); /* make sure WQE is written before valid bit is set */
4751 
4752 	set_64bit_val(wqe, 24, hdr);
4753 
4754 	print_hex_dump_debug("WQE: AEQ_DESTROY WQE", DUMP_PREFIX_OFFSET, 16,
4755 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
4756 	if (post_sq)
4757 		irdma_sc_cqp_post_sq(cqp);
4758 	return 0;
4759 }
4760 
4761 /**
4762  * irdma_sc_get_next_aeqe - get next aeq entry
4763  * @aeq: aeq structure ptr
4764  * @info: aeqe info to be returned
4765  */
4766 int irdma_sc_get_next_aeqe(struct irdma_sc_aeq *aeq,
4767 			   struct irdma_aeqe_info *info)
4768 {
4769 	u64 temp, compl_ctx;
4770 	__le64 *aeqe;
4771 	u8 ae_src;
4772 	u8 polarity;
4773 
4774 	aeqe = IRDMA_GET_CURRENT_AEQ_ELEM(aeq);
4775 	get_64bit_val(aeqe, 8, &temp);
4776 	polarity = (u8)FIELD_GET(IRDMA_AEQE_VALID, temp);
4777 
4778 	if (aeq->polarity != polarity)
4779 		return -ENOENT;
4780 
4781 	/* Ensure AEQE contents are read after valid bit is checked */
4782 	dma_rmb();
4783 
4784 	get_64bit_val(aeqe, 0, &compl_ctx);
4785 
4786 	print_hex_dump_debug("WQE: AEQ_ENTRY WQE", DUMP_PREFIX_OFFSET, 16, 8,
4787 			     aeqe, 16, false);
4788 
4789 	if (aeq->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
4790 		ae_src = (u8)FIELD_GET(IRDMA_AEQE_AESRC_GEN_3, temp);
4791 		info->wqe_idx = (u16)FIELD_GET(IRDMA_AEQE_WQDESCIDX_GEN_3,
4792 					       temp);
4793 		info->qp_cq_id = (u32)FIELD_GET(IRDMA_AEQE_QPCQID_GEN_3, temp);
4794 		info->ae_id = (u16)FIELD_GET(IRDMA_AEQE_AECODE_GEN_3, temp);
4795 		info->tcp_state = (u8)FIELD_GET(IRDMA_AEQE_TCPSTATE_GEN_3, compl_ctx);
4796 		info->iwarp_state = (u8)FIELD_GET(IRDMA_AEQE_IWSTATE_GEN_3, temp);
4797 		info->q2_data_written = (u8)FIELD_GET(IRDMA_AEQE_Q2DATA_GEN_3, compl_ctx);
4798 		info->aeqe_overflow = (bool)FIELD_GET(IRDMA_AEQE_OVERFLOW_GEN_3, temp);
4799 		info->compl_ctx = FIELD_GET(IRDMA_AEQE_CMPL_CTXT, compl_ctx);
4800 		compl_ctx = FIELD_GET(IRDMA_AEQE_CMPL_CTXT, compl_ctx) << IRDMA_AEQE_CMPL_CTXT_S;
4801 	} else {
4802 		ae_src = (u8)FIELD_GET(IRDMA_AEQE_AESRC, temp);
4803 		info->wqe_idx = (u16)FIELD_GET(IRDMA_AEQE_WQDESCIDX, temp);
4804 		info->qp_cq_id = (u32)FIELD_GET(IRDMA_AEQE_QPCQID_LOW, temp) |
4805 			 ((u32)FIELD_GET(IRDMA_AEQE_QPCQID_HI, temp) << 18);
4806 		info->ae_id = (u16)FIELD_GET(IRDMA_AEQE_AECODE, temp);
4807 		info->tcp_state = (u8)FIELD_GET(IRDMA_AEQE_TCPSTATE, temp);
4808 		info->iwarp_state = (u8)FIELD_GET(IRDMA_AEQE_IWSTATE, temp);
4809 		info->q2_data_written = (u8)FIELD_GET(IRDMA_AEQE_Q2DATA, temp);
4810 		info->aeqe_overflow = (bool)FIELD_GET(IRDMA_AEQE_OVERFLOW,
4811 						      temp);
4812 	}
4813 
4814 	info->ae_src = ae_src;
4815 	switch (info->ae_id) {
4816 	case IRDMA_AE_SRQ_LIMIT:
4817 		info->srq = true;
4818 		/* [63:6] from CMPL_CTXT, [5:0] from WQDESCIDX. */
4819 		info->compl_ctx = compl_ctx;
4820 		ae_src = IRDMA_AE_SOURCE_RSVD;
4821 		break;
4822 	case IRDMA_AE_PRIV_OPERATION_DENIED:
4823 	case IRDMA_AE_AMP_INVALIDATE_TYPE1_MW:
4824 	case IRDMA_AE_AMP_MWBIND_ZERO_BASED_TYPE1_MW:
4825 	case IRDMA_AE_AMP_FASTREG_INVALID_PBL_HPS_CFG:
4826 	case IRDMA_AE_AMP_FASTREG_PBLE_MISMATCH:
4827 	case IRDMA_AE_UDA_XMIT_DGRAM_TOO_LONG:
4828 	case IRDMA_AE_UDA_XMIT_BAD_PD:
4829 	case IRDMA_AE_UDA_XMIT_DGRAM_TOO_SHORT:
4830 	case IRDMA_AE_BAD_CLOSE:
4831 	case IRDMA_AE_RDMA_READ_WHILE_ORD_ZERO:
4832 	case IRDMA_AE_STAG_ZERO_INVALID:
4833 	case IRDMA_AE_IB_RREQ_AND_Q1_FULL:
4834 	case IRDMA_AE_IB_INVALID_REQUEST:
4835 	case IRDMA_AE_WQE_UNEXPECTED_OPCODE:
4836 	case IRDMA_AE_IB_REMOTE_ACCESS_ERROR:
4837 	case IRDMA_AE_IB_REMOTE_OP_ERROR:
4838 	case IRDMA_AE_DDP_UBE_INVALID_DDP_VERSION:
4839 	case IRDMA_AE_DDP_UBE_INVALID_MO:
4840 	case IRDMA_AE_DDP_UBE_INVALID_QN:
4841 	case IRDMA_AE_DDP_NO_L_BIT:
4842 	case IRDMA_AE_RDMAP_ROE_INVALID_RDMAP_VERSION:
4843 	case IRDMA_AE_RDMAP_ROE_UNEXPECTED_OPCODE:
4844 	case IRDMA_AE_ROE_INVALID_RDMA_READ_REQUEST:
4845 	case IRDMA_AE_ROE_INVALID_RDMA_WRITE_OR_READ_RESP:
4846 	case IRDMA_AE_ROCE_RSP_LENGTH_ERROR:
4847 	case IRDMA_AE_INVALID_ARP_ENTRY:
4848 	case IRDMA_AE_INVALID_TCP_OPTION_RCVD:
4849 	case IRDMA_AE_STALE_ARP_ENTRY:
4850 	case IRDMA_AE_INVALID_AH_ENTRY:
4851 	case IRDMA_AE_LLP_RECEIVED_MPA_CRC_ERROR:
4852 	case IRDMA_AE_LLP_SEGMENT_TOO_SMALL:
4853 	case IRDMA_AE_LLP_TOO_MANY_RETRIES:
4854 	case IRDMA_AE_LLP_TOO_MANY_RNRS:
4855 	case IRDMA_AE_REMOTE_QP_CATASTROPHIC:
4856 	case IRDMA_AE_LOCAL_QP_CATASTROPHIC:
4857 	case IRDMA_AE_RCE_QP_CATASTROPHIC:
4858 	case IRDMA_AE_LLP_DOUBT_REACHABILITY:
4859 	case IRDMA_AE_LLP_CONNECTION_ESTABLISHED:
4860 	case IRDMA_AE_RESET_SENT:
4861 	case IRDMA_AE_TERMINATE_SENT:
4862 	case IRDMA_AE_RESET_NOT_SENT:
4863 	case IRDMA_AE_LCE_QP_CATASTROPHIC:
4864 	case IRDMA_AE_QP_SUSPEND_COMPLETE:
4865 	case IRDMA_AE_UDA_L4LEN_INVALID:
4866 		info->qp = true;
4867 		info->compl_ctx = compl_ctx;
4868 		break;
4869 	case IRDMA_AE_LCE_CQ_CATASTROPHIC:
4870 		info->cq = true;
4871 		info->compl_ctx = compl_ctx << 1;
4872 		ae_src = IRDMA_AE_SOURCE_RSVD;
4873 		break;
4874 	case IRDMA_AE_CQP_DEFERRED_COMPLETE:
4875 		info->def_info = info->wqe_idx;
4876 		ae_src = IRDMA_AE_SOURCE_RSVD;
4877 		break;
4878 	case IRDMA_AE_ROCE_EMPTY_MCG:
4879 	case IRDMA_AE_ROCE_BAD_MC_IP_ADDR:
4880 	case IRDMA_AE_ROCE_BAD_MC_QPID:
4881 	case IRDMA_AE_MCG_QP_PROTOCOL_MISMATCH:
4882 		fallthrough;
4883 	case IRDMA_AE_LLP_CONNECTION_RESET:
4884 	case IRDMA_AE_LLP_SYN_RECEIVED:
4885 	case IRDMA_AE_LLP_FIN_RECEIVED:
4886 	case IRDMA_AE_LLP_CLOSE_COMPLETE:
4887 	case IRDMA_AE_LLP_TERMINATE_RECEIVED:
4888 	case IRDMA_AE_RDMAP_ROE_BAD_LLP_CLOSE:
4889 		ae_src = IRDMA_AE_SOURCE_RSVD;
4890 		info->qp = true;
4891 		info->compl_ctx = compl_ctx;
4892 		break;
4893 	default:
4894 		break;
4895 	}
4896 
4897 	switch (ae_src) {
4898 	case IRDMA_AE_SOURCE_RQ:
4899 	case IRDMA_AE_SOURCE_RQ_0011:
4900 		info->qp = true;
4901 		info->rq = true;
4902 		info->compl_ctx = compl_ctx;
4903 		info->err_rq_idx_valid = true;
4904 		break;
4905 	case IRDMA_AE_SOURCE_CQ:
4906 	case IRDMA_AE_SOURCE_CQ_0110:
4907 	case IRDMA_AE_SOURCE_CQ_1010:
4908 	case IRDMA_AE_SOURCE_CQ_1110:
4909 		info->cq = true;
4910 		info->compl_ctx = compl_ctx << 1;
4911 		break;
4912 	case IRDMA_AE_SOURCE_SQ:
4913 	case IRDMA_AE_SOURCE_SQ_0111:
4914 		info->qp = true;
4915 		info->sq = true;
4916 		info->compl_ctx = compl_ctx;
4917 		break;
4918 	case IRDMA_AE_SOURCE_IN_RR_WR:
4919 		info->qp = true;
4920 		if (aeq->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3)
4921 			info->err_rq_idx_valid = true;
4922 		info->compl_ctx = compl_ctx;
4923 		info->in_rdrsp_wr = true;
4924 		break;
4925 	case IRDMA_AE_SOURCE_IN_RR_WR_1011:
4926 		info->qp = true;
4927 		if (aeq->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
4928 			info->sq = true;
4929 			info->err_rq_idx_valid = true;
4930 		}
4931 		info->compl_ctx = compl_ctx;
4932 		info->in_rdrsp_wr = true;
4933 		break;
4934 	case IRDMA_AE_SOURCE_OUT_RR:
4935 	case IRDMA_AE_SOURCE_OUT_RR_1111:
4936 		info->qp = true;
4937 		info->compl_ctx = compl_ctx;
4938 		info->out_rdrsp = true;
4939 		break;
4940 	case IRDMA_AE_SOURCE_RSVD:
4941 	default:
4942 		break;
4943 	}
4944 
4945 	IRDMA_RING_MOVE_TAIL(aeq->aeq_ring);
4946 	if (!IRDMA_RING_CURRENT_TAIL(aeq->aeq_ring))
4947 		aeq->polarity ^= 1;
4948 
4949 	return 0;
4950 }
4951 
4952 /**
4953  * irdma_sc_repost_aeq_entries - repost completed aeq entries
4954  * @dev: sc device struct
4955  * @count: allocate count
4956  */
4957 void irdma_sc_repost_aeq_entries(struct irdma_sc_dev *dev, u32 count)
4958 {
4959 	writel(count, dev->hw_regs[IRDMA_AEQALLOC]);
4960 }
4961 
4962 /**
4963  * irdma_sc_ccq_init - initialize control cq
4964  * @cq: sc's cq ctruct
4965  * @info: info for control cq initialization
4966  */
4967 int irdma_sc_ccq_init(struct irdma_sc_cq *cq, struct irdma_ccq_init_info *info)
4968 {
4969 	u32 pble_obj_cnt;
4970 
4971 	if (info->num_elem < info->dev->hw_attrs.uk_attrs.min_hw_cq_size ||
4972 	    info->num_elem > info->dev->hw_attrs.uk_attrs.max_hw_cq_size)
4973 		return -EINVAL;
4974 
4975 	if (info->ceq_id >= info->dev->hmc_fpm_misc.max_ceqs)
4976 		return -EINVAL;
4977 
4978 	pble_obj_cnt = info->dev->hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt;
4979 
4980 	if (info->virtual_map && info->first_pm_pbl_idx >= pble_obj_cnt)
4981 		return -EINVAL;
4982 
4983 	cq->cq_pa = info->cq_pa;
4984 	cq->cq_uk.cq_base = info->cq_base;
4985 	cq->shadow_area_pa = info->shadow_area_pa;
4986 	cq->cq_uk.shadow_area = info->shadow_area;
4987 	cq->shadow_read_threshold = info->shadow_read_threshold;
4988 	cq->dev = info->dev;
4989 	cq->ceq_id = info->ceq_id;
4990 	cq->cq_uk.cq_size = info->num_elem;
4991 	cq->cq_type = IRDMA_CQ_TYPE_CQP;
4992 	cq->ceqe_mask = info->ceqe_mask;
4993 	IRDMA_RING_INIT(cq->cq_uk.cq_ring, info->num_elem);
4994 	cq->cq_uk.cq_id = 0; /* control cq is id 0 always */
4995 	cq->ceq_id_valid = info->ceq_id_valid;
4996 	cq->tph_en = info->tph_en;
4997 	cq->tph_val = info->tph_val;
4998 	cq->cq_uk.avoid_mem_cflct = info->avoid_mem_cflct;
4999 	cq->pbl_list = info->pbl_list;
5000 	cq->virtual_map = info->virtual_map;
5001 	cq->pbl_chunk_size = info->pbl_chunk_size;
5002 	cq->first_pm_pbl_idx = info->first_pm_pbl_idx;
5003 	cq->cq_uk.polarity = true;
5004 	cq->vsi = info->vsi;
5005 	cq->cq_uk.cq_ack_db = cq->dev->cq_ack_db;
5006 
5007 	/* Only applicable to CQs other than CCQ so initialize to zero */
5008 	cq->cq_uk.cqe_alloc_db = NULL;
5009 
5010 	info->dev->ccq = cq;
5011 	return 0;
5012 }
5013 
5014 /**
5015  * irdma_sc_ccq_create_done - poll cqp for ccq create
5016  * @ccq: ccq sc struct
5017  */
5018 static inline int irdma_sc_ccq_create_done(struct irdma_sc_cq *ccq)
5019 {
5020 	struct irdma_sc_cqp *cqp;
5021 
5022 	cqp = ccq->dev->cqp;
5023 
5024 	return irdma_sc_poll_for_cqp_op_done(cqp, IRDMA_CQP_OP_CREATE_CQ, NULL);
5025 }
5026 
5027 /**
5028  * irdma_sc_ccq_create - create control cq
5029  * @ccq: ccq sc struct
5030  * @scratch: u64 saved to be used during cqp completion
5031  * @check_overflow: overlow flag for ccq
5032  * @post_sq: flag for cqp db to ring
5033  */
5034 int irdma_sc_ccq_create(struct irdma_sc_cq *ccq, u64 scratch,
5035 			bool check_overflow, bool post_sq)
5036 {
5037 	int ret_code;
5038 
5039 	ret_code = irdma_sc_cq_create(ccq, scratch, check_overflow, post_sq);
5040 	if (ret_code)
5041 		return ret_code;
5042 
5043 	if (post_sq) {
5044 		ret_code = irdma_sc_ccq_create_done(ccq);
5045 		if (ret_code)
5046 			return ret_code;
5047 	}
5048 	ccq->dev->cqp->process_cqp_sds = irdma_cqp_sds_cmd;
5049 
5050 	return 0;
5051 }
5052 
5053 /**
5054  * irdma_sc_ccq_destroy - destroy ccq during close
5055  * @ccq: ccq sc struct
5056  * @scratch: u64 saved to be used during cqp completion
5057  * @post_sq: flag for cqp db to ring
5058  */
5059 int irdma_sc_ccq_destroy(struct irdma_sc_cq *ccq, u64 scratch, bool post_sq)
5060 {
5061 	struct irdma_sc_cqp *cqp;
5062 	__le64 *wqe;
5063 	u64 hdr;
5064 	int ret_code = 0;
5065 	u32 tail, val, error;
5066 
5067 	cqp = ccq->dev->cqp;
5068 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
5069 	if (!wqe)
5070 		return -ENOMEM;
5071 
5072 	set_64bit_val(wqe, 0, ccq->cq_uk.cq_size);
5073 	set_64bit_val(wqe, 8, (uintptr_t)ccq >> 1);
5074 	set_64bit_val(wqe, 40, ccq->shadow_area_pa);
5075 
5076 	hdr = ccq->cq_uk.cq_id |
5077 	      FLD_LS_64(ccq->dev, (ccq->ceq_id_valid ? ccq->ceq_id : 0),
5078 			IRDMA_CQPSQ_CQ_CEQID) |
5079 	      FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_DESTROY_CQ) |
5080 	      FIELD_PREP(IRDMA_CQPSQ_CQ_ENCEQEMASK, ccq->ceqe_mask) |
5081 	      FIELD_PREP(IRDMA_CQPSQ_CQ_CEQIDVALID, ccq->ceq_id_valid) |
5082 	      FIELD_PREP(IRDMA_CQPSQ_TPHEN, ccq->tph_en) |
5083 	      FIELD_PREP(IRDMA_CQPSQ_CQ_AVOIDMEMCNFLCT, ccq->cq_uk.avoid_mem_cflct) |
5084 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
5085 	dma_wmb(); /* make sure WQE is written before valid bit is set */
5086 
5087 	set_64bit_val(wqe, 24, hdr);
5088 
5089 	print_hex_dump_debug("WQE: CCQ_DESTROY WQE", DUMP_PREFIX_OFFSET, 16,
5090 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
5091 	irdma_get_cqp_reg_info(cqp, &val, &tail, &error);
5092 
5093 	if (post_sq) {
5094 		irdma_sc_cqp_post_sq(cqp);
5095 		ret_code = irdma_cqp_poll_registers(cqp, tail,
5096 						    cqp->dev->hw_attrs.max_done_count);
5097 	}
5098 
5099 	cqp->process_cqp_sds = irdma_update_sds_noccq;
5100 
5101 	return ret_code;
5102 }
5103 
5104 /**
5105  * irdma_sc_init_iw_hmc() - queries fpm values using cqp and populates hmc_info
5106  * @dev : ptr to irdma_dev struct
5107  * @hmc_fn_id: hmc function id
5108  */
5109 int irdma_sc_init_iw_hmc(struct irdma_sc_dev *dev, u8 hmc_fn_id)
5110 {
5111 	struct irdma_hmc_info *hmc_info;
5112 	struct irdma_hmc_fpm_misc *hmc_fpm_misc;
5113 	struct irdma_dma_mem query_fpm_mem;
5114 	int ret_code = 0;
5115 	u8 wait_type;
5116 
5117 	hmc_info = dev->hmc_info;
5118 	hmc_fpm_misc = &dev->hmc_fpm_misc;
5119 	query_fpm_mem.pa = dev->fpm_query_buf_pa;
5120 	query_fpm_mem.va = dev->fpm_query_buf;
5121 	hmc_info->hmc_fn_id = hmc_fn_id;
5122 	wait_type = (u8)IRDMA_CQP_WAIT_POLL_REGS;
5123 
5124 	ret_code = irdma_sc_query_fpm_val(dev->cqp, 0, hmc_info->hmc_fn_id,
5125 					  &query_fpm_mem, true, wait_type);
5126 	if (ret_code)
5127 		return ret_code;
5128 
5129 	/* parse the fpm_query_buf and fill hmc obj info */
5130 	ret_code = irdma_sc_parse_fpm_query_buf(dev, query_fpm_mem.va, hmc_info,
5131 						hmc_fpm_misc);
5132 
5133 	print_hex_dump_debug("HMC: QUERY FPM BUFFER", DUMP_PREFIX_OFFSET, 16,
5134 			     8, query_fpm_mem.va, IRDMA_QUERY_FPM_BUF_SIZE,
5135 			     false);
5136 	return ret_code;
5137 }
5138 
5139 /**
5140  * irdma_set_loc_mem() - set a local memory bit field
5141  * @buf: ptr to a buffer where local memory gets enabled
5142  */
5143 static void irdma_set_loc_mem(__le64 *buf)
5144 {
5145 	u64 loc_mem_en = BIT_ULL(ENABLE_LOC_MEM);
5146 	u32 offset;
5147 	u64 temp;
5148 
5149 	for (offset = 0; offset < IRDMA_COMMIT_FPM_BUF_SIZE;
5150 	     offset += sizeof(__le64)) {
5151 		if (offset == IRDMA_PBLE_COMMIT_OFFSET)
5152 			continue;
5153 		get_64bit_val(buf, offset, &temp);
5154 		if (temp)
5155 			set_64bit_val(buf, offset, temp | loc_mem_en);
5156 	}
5157 }
5158 
5159 /**
5160  * irdma_sc_cfg_iw_fpm() - commits hmc obj cnt values using cqp
5161  * command and populates fpm base address in hmc_info
5162  * @dev : ptr to irdma_dev struct
5163  * @hmc_fn_id: hmc function id
5164  */
5165 static int irdma_sc_cfg_iw_fpm(struct irdma_sc_dev *dev, u8 hmc_fn_id)
5166 {
5167 	struct irdma_hmc_info *hmc_info;
5168 	struct irdma_hmc_obj_info *obj_info;
5169 	__le64 *buf;
5170 	struct irdma_dma_mem commit_fpm_mem;
5171 	int ret_code = 0;
5172 	u8 wait_type;
5173 
5174 	hmc_info = dev->hmc_info;
5175 	obj_info = hmc_info->hmc_obj;
5176 	buf = dev->fpm_commit_buf;
5177 
5178 	set_64bit_val(buf, 0, (u64)obj_info[IRDMA_HMC_IW_QP].cnt);
5179 	set_64bit_val(buf, 8, (u64)obj_info[IRDMA_HMC_IW_CQ].cnt);
5180 	set_64bit_val(buf, 16, (u64)obj_info[IRDMA_HMC_IW_SRQ].cnt);
5181 	set_64bit_val(buf, 24, (u64)obj_info[IRDMA_HMC_IW_HTE].cnt);
5182 	set_64bit_val(buf, 32, (u64)obj_info[IRDMA_HMC_IW_ARP].cnt);
5183 	set_64bit_val(buf, 40, (u64)0); /* RSVD */
5184 	set_64bit_val(buf, 48, (u64)obj_info[IRDMA_HMC_IW_MR].cnt);
5185 	set_64bit_val(buf, 56, (u64)obj_info[IRDMA_HMC_IW_XF].cnt);
5186 	set_64bit_val(buf, 64, (u64)obj_info[IRDMA_HMC_IW_XFFL].cnt);
5187 	set_64bit_val(buf, 72, (u64)obj_info[IRDMA_HMC_IW_Q1].cnt);
5188 	set_64bit_val(buf, 80, (u64)obj_info[IRDMA_HMC_IW_Q1FL].cnt);
5189 	set_64bit_val(buf, 88,
5190 		      (u64)obj_info[IRDMA_HMC_IW_TIMER].cnt);
5191 	set_64bit_val(buf, 96,
5192 		      (u64)obj_info[IRDMA_HMC_IW_FSIMC].cnt);
5193 	set_64bit_val(buf, 104,
5194 		      (u64)obj_info[IRDMA_HMC_IW_FSIAV].cnt);
5195 	set_64bit_val(buf, 112,
5196 		      (u64)obj_info[IRDMA_HMC_IW_PBLE].cnt);
5197 	set_64bit_val(buf, 120, (u64)0); /* RSVD */
5198 	set_64bit_val(buf, 128, (u64)obj_info[IRDMA_HMC_IW_RRF].cnt);
5199 	set_64bit_val(buf, 136,
5200 		      (u64)obj_info[IRDMA_HMC_IW_RRFFL].cnt);
5201 	set_64bit_val(buf, 144, (u64)obj_info[IRDMA_HMC_IW_HDR].cnt);
5202 	set_64bit_val(buf, 152, (u64)obj_info[IRDMA_HMC_IW_MD].cnt);
5203 	set_64bit_val(buf, 160,
5204 		      (u64)obj_info[IRDMA_HMC_IW_OOISC].cnt);
5205 	set_64bit_val(buf, 168,
5206 		      (u64)obj_info[IRDMA_HMC_IW_OOISCFFL].cnt);
5207 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3 &&
5208 	    dev->hmc_fpm_misc.loc_mem_pages)
5209 		irdma_set_loc_mem(buf);
5210 	commit_fpm_mem.pa = dev->fpm_commit_buf_pa;
5211 	commit_fpm_mem.va = dev->fpm_commit_buf;
5212 
5213 	wait_type = (u8)IRDMA_CQP_WAIT_POLL_REGS;
5214 	print_hex_dump_debug("HMC: COMMIT FPM BUFFER", DUMP_PREFIX_OFFSET, 16,
5215 			     8, commit_fpm_mem.va, IRDMA_COMMIT_FPM_BUF_SIZE,
5216 			     false);
5217 	ret_code = irdma_sc_commit_fpm_val(dev->cqp, 0, hmc_info->hmc_fn_id,
5218 					   &commit_fpm_mem, true, wait_type);
5219 	if (!ret_code)
5220 		irdma_sc_parse_fpm_commit_buf(dev, dev->fpm_commit_buf,
5221 					      hmc_info->hmc_obj,
5222 					      &hmc_info->sd_table.sd_cnt);
5223 	print_hex_dump_debug("HMC: COMMIT FPM BUFFER", DUMP_PREFIX_OFFSET, 16,
5224 			     8, commit_fpm_mem.va, IRDMA_COMMIT_FPM_BUF_SIZE,
5225 			     false);
5226 
5227 	return ret_code;
5228 }
5229 
5230 /**
5231  * cqp_sds_wqe_fill - fill cqp wqe doe sd
5232  * @cqp: struct for cqp hw
5233  * @info: sd info for wqe
5234  * @scratch: u64 saved to be used during cqp completion
5235  */
5236 static int cqp_sds_wqe_fill(struct irdma_sc_cqp *cqp,
5237 			    struct irdma_update_sds_info *info, u64 scratch)
5238 {
5239 	u64 data;
5240 	u64 hdr;
5241 	__le64 *wqe;
5242 	int mem_entries, wqe_entries;
5243 	struct irdma_dma_mem *sdbuf = &cqp->sdbuf;
5244 	u64 offset = 0;
5245 	u32 wqe_idx;
5246 
5247 	wqe = irdma_sc_cqp_get_next_send_wqe_idx(cqp, scratch, &wqe_idx);
5248 	if (!wqe)
5249 		return -ENOMEM;
5250 
5251 	wqe_entries = (info->cnt > 3) ? 3 : info->cnt;
5252 	mem_entries = info->cnt - wqe_entries;
5253 
5254 	if (mem_entries) {
5255 		offset = wqe_idx * IRDMA_UPDATE_SD_BUFF_SIZE;
5256 		memcpy(((char *)sdbuf->va + offset), &info->entry[3], mem_entries << 4);
5257 
5258 		data = (u64)sdbuf->pa + offset;
5259 	} else {
5260 		data = 0;
5261 	}
5262 	data |= FIELD_PREP(IRDMA_CQPSQ_UPESD_HMCFNID, info->hmc_fn_id);
5263 	set_64bit_val(wqe, 16, data);
5264 
5265 	switch (wqe_entries) {
5266 	case 3:
5267 		set_64bit_val(wqe, 48,
5268 			      (FIELD_PREP(IRDMA_CQPSQ_UPESD_SDCMD, info->entry[2].cmd) |
5269 			       FIELD_PREP(IRDMA_CQPSQ_UPESD_ENTRY_VALID, 1)));
5270 
5271 		set_64bit_val(wqe, 56, info->entry[2].data);
5272 		fallthrough;
5273 	case 2:
5274 		set_64bit_val(wqe, 32,
5275 			      (FIELD_PREP(IRDMA_CQPSQ_UPESD_SDCMD, info->entry[1].cmd) |
5276 			       FIELD_PREP(IRDMA_CQPSQ_UPESD_ENTRY_VALID, 1)));
5277 
5278 		set_64bit_val(wqe, 40, info->entry[1].data);
5279 		fallthrough;
5280 	case 1:
5281 		set_64bit_val(wqe, 0,
5282 			      FIELD_PREP(IRDMA_CQPSQ_UPESD_SDCMD, info->entry[0].cmd));
5283 
5284 		set_64bit_val(wqe, 8, info->entry[0].data);
5285 		break;
5286 	default:
5287 		break;
5288 	}
5289 
5290 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE, IRDMA_CQP_OP_UPDATE_PE_SDS) |
5291 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity) |
5292 	      FIELD_PREP(IRDMA_CQPSQ_UPESD_ENTRY_COUNT, mem_entries);
5293 	dma_wmb(); /* make sure WQE is written before valid bit is set */
5294 
5295 	set_64bit_val(wqe, 24, hdr);
5296 
5297 	if (mem_entries)
5298 		print_hex_dump_debug("WQE: UPDATE_PE_SDS WQE Buffer",
5299 				     DUMP_PREFIX_OFFSET, 16, 8,
5300 				     (char *)sdbuf->va + offset,
5301 				     mem_entries << 4, false);
5302 
5303 	print_hex_dump_debug("WQE: UPDATE_PE_SDS WQE", DUMP_PREFIX_OFFSET, 16,
5304 			     8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
5305 
5306 	return 0;
5307 }
5308 
5309 /**
5310  * irdma_update_pe_sds - cqp wqe for sd
5311  * @dev: ptr to irdma_dev struct
5312  * @info: sd info for sd's
5313  * @scratch: u64 saved to be used during cqp completion
5314  */
5315 static int irdma_update_pe_sds(struct irdma_sc_dev *dev,
5316 			       struct irdma_update_sds_info *info, u64 scratch)
5317 {
5318 	struct irdma_sc_cqp *cqp = dev->cqp;
5319 	int ret_code;
5320 
5321 	ret_code = cqp_sds_wqe_fill(cqp, info, scratch);
5322 	if (!ret_code)
5323 		irdma_sc_cqp_post_sq(cqp);
5324 
5325 	return ret_code;
5326 }
5327 
5328 /**
5329  * irdma_update_sds_noccq - update sd before ccq created
5330  * @dev: sc device struct
5331  * @info: sd info for sd's
5332  */
5333 int irdma_update_sds_noccq(struct irdma_sc_dev *dev,
5334 			   struct irdma_update_sds_info *info)
5335 {
5336 	u32 error, val, tail;
5337 	struct irdma_sc_cqp *cqp = dev->cqp;
5338 	int ret_code;
5339 
5340 	ret_code = cqp_sds_wqe_fill(cqp, info, 0);
5341 	if (ret_code)
5342 		return ret_code;
5343 
5344 	irdma_get_cqp_reg_info(cqp, &val, &tail, &error);
5345 
5346 	irdma_sc_cqp_post_sq(cqp);
5347 	return irdma_cqp_poll_registers(cqp, tail,
5348 					cqp->dev->hw_attrs.max_done_count);
5349 }
5350 
5351 /**
5352  * irdma_sc_static_hmc_pages_allocated - cqp wqe to allocate hmc pages
5353  * @cqp: struct for cqp hw
5354  * @scratch: u64 saved to be used during cqp completion
5355  * @hmc_fn_id: hmc function id
5356  * @post_sq: flag for cqp db to ring
5357  * @poll_registers: flag to poll register for cqp completion
5358  */
5359 int irdma_sc_static_hmc_pages_allocated(struct irdma_sc_cqp *cqp, u64 scratch,
5360 					u8 hmc_fn_id, bool post_sq,
5361 					bool poll_registers)
5362 {
5363 	u64 hdr;
5364 	__le64 *wqe;
5365 	u32 tail, val, error;
5366 
5367 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
5368 	if (!wqe)
5369 		return -ENOMEM;
5370 
5371 	set_64bit_val(wqe, 16,
5372 		      FIELD_PREP(IRDMA_SHMC_PAGE_ALLOCATED_HMC_FN_ID, hmc_fn_id));
5373 
5374 	hdr = FIELD_PREP(IRDMA_CQPSQ_OPCODE,
5375 			 IRDMA_CQP_OP_SHMC_PAGES_ALLOCATED) |
5376 	      FIELD_PREP(IRDMA_CQPSQ_WQEVALID, cqp->polarity);
5377 	dma_wmb(); /* make sure WQE is written before valid bit is set */
5378 
5379 	set_64bit_val(wqe, 24, hdr);
5380 
5381 	print_hex_dump_debug("WQE: SHMC_PAGES_ALLOCATED WQE",
5382 			     DUMP_PREFIX_OFFSET, 16, 8, wqe,
5383 			     IRDMA_CQP_WQE_SIZE * 8, false);
5384 	irdma_get_cqp_reg_info(cqp, &val, &tail, &error);
5385 
5386 	if (post_sq) {
5387 		irdma_sc_cqp_post_sq(cqp);
5388 		if (poll_registers)
5389 			/* check for cqp sq tail update */
5390 			return irdma_cqp_poll_registers(cqp, tail,
5391 							cqp->dev->hw_attrs.max_done_count);
5392 		else
5393 			return irdma_sc_poll_for_cqp_op_done(cqp,
5394 							     IRDMA_CQP_OP_SHMC_PAGES_ALLOCATED,
5395 							     NULL);
5396 	}
5397 
5398 	return 0;
5399 }
5400 
5401 /**
5402  * irdma_cqp_ring_full - check if cqp ring is full
5403  * @cqp: struct for cqp hw
5404  */
5405 static bool irdma_cqp_ring_full(struct irdma_sc_cqp *cqp)
5406 {
5407 	return IRDMA_RING_FULL_ERR(cqp->sq_ring);
5408 }
5409 
5410 /**
5411  * irdma_est_sd - returns approximate number of SDs for HMC
5412  * @dev: sc device struct
5413  * @hmc_info: hmc structure, size and count for HMC objects
5414  */
5415 static u32 irdma_est_sd(struct irdma_sc_dev *dev,
5416 			struct irdma_hmc_info *hmc_info)
5417 {
5418 	struct irdma_hmc_obj_info *pble_info;
5419 	int i;
5420 	u64 size = 0;
5421 	u64 sd;
5422 
5423 	for (i = IRDMA_HMC_IW_QP; i < IRDMA_HMC_IW_MAX; i++)
5424 		if (i != IRDMA_HMC_IW_PBLE)
5425 			size += round_up(hmc_info->hmc_obj[i].cnt *
5426 					 hmc_info->hmc_obj[i].size, 512);
5427 
5428 	pble_info = &hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE];
5429 	if (dev->privileged)
5430 		size += round_up(pble_info->cnt * pble_info->size, 512);
5431 	if (size & 0x1FFFFF)
5432 		sd = (size >> 21) + 1; /* add 1 for remainder */
5433 	else
5434 		sd = size >> 21;
5435 	if (!dev->privileged && !dev->hmc_fpm_misc.loc_mem_pages) {
5436 		/* 2MB alignment for VF PBLE HMC */
5437 		size = pble_info->cnt * pble_info->size;
5438 		if (size & 0x1FFFFF)
5439 			sd += (size >> 21) + 1; /* add 1 for remainder */
5440 		else
5441 			sd += size >> 21;
5442 	}
5443 	if (sd > 0xFFFFFFFF) {
5444 		ibdev_dbg(to_ibdev(dev), "HMC: sd overflow[%lld]\n", sd);
5445 		sd = 0xFFFFFFFF - 1;
5446 	}
5447 
5448 	return (u32)sd;
5449 }
5450 
5451 /**
5452  * irdma_sc_query_rdma_features - query RDMA features and FW ver
5453  * @cqp: struct for cqp hw
5454  * @buf: buffer to hold query info
5455  * @scratch: u64 saved to be used during cqp completion
5456  */
5457 static int irdma_sc_query_rdma_features(struct irdma_sc_cqp *cqp,
5458 					struct irdma_dma_mem *buf, u64 scratch)
5459 {
5460 	u32 tail, val, error;
5461 	__le64 *wqe;
5462 	int status;
5463 	u64 temp;
5464 
5465 	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
5466 	if (!wqe)
5467 		return -ENOMEM;
5468 
5469 	temp = buf->pa;
5470 	set_64bit_val(wqe, 32, temp);
5471 
5472 	temp = FIELD_PREP(IRDMA_CQPSQ_QUERY_RDMA_FEATURES_WQEVALID,
5473 			  cqp->polarity) |
5474 	       FIELD_PREP(IRDMA_CQPSQ_QUERY_RDMA_FEATURES_BUF_LEN, buf->size) |
5475 	       FIELD_PREP(IRDMA_CQPSQ_UP_OP, IRDMA_CQP_OP_QUERY_RDMA_FEATURES);
5476 	dma_wmb(); /* make sure WQE is written before valid bit is set */
5477 
5478 	set_64bit_val(wqe, 24, temp);
5479 
5480 	print_hex_dump_debug("WQE: QUERY RDMA FEATURES", DUMP_PREFIX_OFFSET,
5481 			     16, 8, wqe, IRDMA_CQP_WQE_SIZE * 8, false);
5482 	irdma_get_cqp_reg_info(cqp, &val, &tail, &error);
5483 
5484 	irdma_sc_cqp_post_sq(cqp);
5485 	status = irdma_cqp_poll_registers(cqp, tail,
5486 					  cqp->dev->hw_attrs.max_done_count);
5487 	if (error || status)
5488 		status = -EINVAL;
5489 
5490 	return status;
5491 }
5492 
5493 /**
5494  * irdma_get_rdma_features - get RDMA features
5495  * @dev: sc device struct
5496  */
5497 int irdma_get_rdma_features(struct irdma_sc_dev *dev)
5498 {
5499 	int ret_code;
5500 	struct irdma_dma_mem feat_buf;
5501 	u64 temp;
5502 	u16 byte_idx, feat_type, feat_cnt, feat_idx;
5503 
5504 	feat_buf.size = ALIGN(IRDMA_FEATURE_BUF_SIZE,
5505 			      IRDMA_FEATURE_BUF_ALIGNMENT);
5506 	feat_buf.va = dma_alloc_coherent(dev->hw->device, feat_buf.size,
5507 					 &feat_buf.pa, GFP_KERNEL);
5508 	if (!feat_buf.va)
5509 		return -ENOMEM;
5510 
5511 	ret_code = irdma_sc_query_rdma_features(dev->cqp, &feat_buf, 0);
5512 	if (ret_code)
5513 		goto exit;
5514 
5515 	get_64bit_val(feat_buf.va, 0, &temp);
5516 	feat_cnt = (u16)FIELD_GET(IRDMA_FEATURE_CNT, temp);
5517 	if (feat_cnt < 2) {
5518 		ret_code = -EINVAL;
5519 		goto exit;
5520 	} else if (feat_cnt > IRDMA_MAX_FEATURES) {
5521 		ibdev_dbg(to_ibdev(dev),
5522 			  "DEV: feature buf size insufficient, retrying with larger buffer\n");
5523 		dma_free_coherent(dev->hw->device, feat_buf.size, feat_buf.va,
5524 				  feat_buf.pa);
5525 		feat_buf.va = NULL;
5526 		feat_buf.size = ALIGN(8 * feat_cnt,
5527 				      IRDMA_FEATURE_BUF_ALIGNMENT);
5528 		feat_buf.va = dma_alloc_coherent(dev->hw->device,
5529 						 feat_buf.size, &feat_buf.pa,
5530 						 GFP_KERNEL);
5531 		if (!feat_buf.va)
5532 			return -ENOMEM;
5533 
5534 		ret_code = irdma_sc_query_rdma_features(dev->cqp, &feat_buf, 0);
5535 		if (ret_code)
5536 			goto exit;
5537 
5538 		get_64bit_val(feat_buf.va, 0, &temp);
5539 		feat_cnt = (u16)FIELD_GET(IRDMA_FEATURE_CNT, temp);
5540 		if (feat_cnt < 2) {
5541 			ret_code = -EINVAL;
5542 			goto exit;
5543 		}
5544 	}
5545 
5546 	print_hex_dump_debug("WQE: QUERY RDMA FEATURES", DUMP_PREFIX_OFFSET,
5547 			     16, 8, feat_buf.va, feat_cnt * 8, false);
5548 
5549 	for (byte_idx = 0, feat_idx = 0; feat_idx < min(feat_cnt, (u16)IRDMA_MAX_FEATURES);
5550 	     feat_idx++, byte_idx += 8) {
5551 		get_64bit_val(feat_buf.va, byte_idx, &temp);
5552 		feat_type = FIELD_GET(IRDMA_FEATURE_TYPE, temp);
5553 		if (feat_type >= IRDMA_MAX_FEATURES) {
5554 			ibdev_dbg(to_ibdev(dev),
5555 				  "DEV: found unrecognized feature type %d\n",
5556 				  feat_type);
5557 			continue;
5558 		}
5559 		dev->feature_info[feat_type] = temp;
5560 	}
5561 
5562 	if (dev->feature_info[IRDMA_FTN_FLAGS] & IRDMA_ATOMICS_ALLOWED_BIT)
5563 		dev->hw_attrs.uk_attrs.feature_flags |= IRDMA_FEATURE_ATOMIC_OPS;
5564 
5565 exit:
5566 	dma_free_coherent(dev->hw->device, feat_buf.size, feat_buf.va,
5567 			  feat_buf.pa);
5568 	feat_buf.va = NULL;
5569 	return ret_code;
5570 }
5571 
5572 static u32 irdma_q1_cnt(struct irdma_sc_dev *dev,
5573 			struct irdma_hmc_info *hmc_info, u32 qpwanted)
5574 {
5575 	u32 q1_cnt;
5576 
5577 	if (dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1) {
5578 		q1_cnt = roundup_pow_of_two(dev->hw_attrs.max_hw_ird * 2 * qpwanted);
5579 	} else {
5580 		if (dev->cqp->protocol_used != IRDMA_IWARP_PROTOCOL_ONLY)
5581 			q1_cnt = roundup_pow_of_two(dev->hw_attrs.max_hw_ird * 2 * qpwanted + 512);
5582 		else
5583 			q1_cnt = dev->hw_attrs.max_hw_ird * 2 * qpwanted;
5584 	}
5585 
5586 	return q1_cnt;
5587 }
5588 
5589 static void cfg_fpm_value_gen_1(struct irdma_sc_dev *dev,
5590 				struct irdma_hmc_info *hmc_info, u32 qpwanted)
5591 {
5592 	hmc_info->hmc_obj[IRDMA_HMC_IW_XF].cnt = roundup_pow_of_two(qpwanted * dev->hw_attrs.max_hw_wqes);
5593 }
5594 
5595 static void cfg_fpm_value_gen_2(struct irdma_sc_dev *dev,
5596 				struct irdma_hmc_info *hmc_info, u32 qpwanted)
5597 {
5598 	struct irdma_hmc_fpm_misc *hmc_fpm_misc = &dev->hmc_fpm_misc;
5599 
5600 	hmc_info->hmc_obj[IRDMA_HMC_IW_XF].cnt =
5601 		4 * hmc_fpm_misc->xf_block_size * qpwanted;
5602 
5603 	hmc_info->hmc_obj[IRDMA_HMC_IW_HDR].cnt = qpwanted;
5604 
5605 	if (hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].max_cnt)
5606 		hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].cnt = 32 * qpwanted;
5607 	if (hmc_info->hmc_obj[IRDMA_HMC_IW_RRFFL].max_cnt)
5608 		hmc_info->hmc_obj[IRDMA_HMC_IW_RRFFL].cnt =
5609 			hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].cnt /
5610 			hmc_fpm_misc->rrf_block_size;
5611 	if (hmc_info->hmc_obj[IRDMA_HMC_IW_OOISC].max_cnt)
5612 		hmc_info->hmc_obj[IRDMA_HMC_IW_OOISC].cnt = 32 * qpwanted;
5613 	if (hmc_info->hmc_obj[IRDMA_HMC_IW_OOISCFFL].max_cnt)
5614 		hmc_info->hmc_obj[IRDMA_HMC_IW_OOISCFFL].cnt =
5615 			hmc_info->hmc_obj[IRDMA_HMC_IW_OOISC].cnt /
5616 			hmc_fpm_misc->ooiscf_block_size;
5617 }
5618 
5619 /**
5620  * irdma_get_rsrc_mem_config - configure resources if local memory or host
5621  * @dev: sc device struct
5622  * @is_mrte_loc_mem: if true, MR's to be in local memory because sd=loc pages
5623  *
5624  * Only mr can be configured host or local memory if qp's are in local memory.
5625  * If qp is in local memory, then all resource object will be in local memory
5626  * except mr which can be either host or local memory.  The only exception
5627  * is pble's which are always in host memory.
5628  */
5629 static void irdma_get_rsrc_mem_config(struct irdma_sc_dev *dev, bool is_mrte_loc_mem)
5630 {
5631 	struct irdma_hmc_info *hmc_info = dev->hmc_info;
5632 	int i;
5633 
5634 	for (i = IRDMA_HMC_IW_QP; i < IRDMA_HMC_IW_MAX; i++)
5635 		hmc_info->hmc_obj[i].mem_loc = IRDMA_LOC_MEM;
5636 
5637 	if (dev->feature_info[IRDMA_OBJ_1] && !is_mrte_loc_mem) {
5638 		u8 mem_type;
5639 
5640 		mem_type = (u8)FIELD_GET(IRDMA_MR_MEM_LOC, dev->feature_info[IRDMA_OBJ_1]);
5641 
5642 		hmc_info->hmc_obj[IRDMA_HMC_IW_MR].mem_loc =
5643 			(mem_type & IRDMA_OBJ_LOC_MEM_BIT) ?
5644 			IRDMA_LOC_MEM : IRDMA_HOST_MEM;
5645 	} else {
5646 		hmc_info->hmc_obj[IRDMA_HMC_IW_MR].mem_loc = IRDMA_LOC_MEM;
5647 	}
5648 
5649 	hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].mem_loc = IRDMA_HOST_MEM;
5650 
5651 	ibdev_dbg(to_ibdev(dev), "HMC: INFO: mrte_mem_loc = %d pble = %d\n",
5652 		  hmc_info->hmc_obj[IRDMA_HMC_IW_MR].mem_loc,
5653 		  hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].mem_loc);
5654 }
5655 
5656 /**
5657  * irdma_cfg_sd_mem - allocate sd memory
5658  * @dev: sc device struct
5659  * @hmc_info: ptr to irdma_hmc_obj_info struct
5660  */
5661 static int irdma_cfg_sd_mem(struct irdma_sc_dev *dev,
5662 			    struct irdma_hmc_info *hmc_info)
5663 {
5664 	struct irdma_virt_mem virt_mem;
5665 	u32 mem_size;
5666 
5667 	mem_size = sizeof(struct irdma_hmc_sd_entry) * hmc_info->sd_table.sd_cnt;
5668 	virt_mem.size = mem_size;
5669 	virt_mem.va = kzalloc(virt_mem.size, GFP_KERNEL);
5670 	if (!virt_mem.va)
5671 		return -ENOMEM;
5672 	hmc_info->sd_table.sd_entry = virt_mem.va;
5673 
5674 	return 0;
5675 }
5676 
5677 /**
5678  * irdma_get_objs_pages - get number of 2M pages needed
5679  * @dev: sc device struct
5680  * @hmc_info: pointer to the HMC configuration information struct
5681  * @mem_loc: pages for local or host memory
5682  */
5683 static u32 irdma_get_objs_pages(struct irdma_sc_dev *dev,
5684 				struct irdma_hmc_info *hmc_info,
5685 				enum irdma_hmc_obj_mem mem_loc)
5686 {
5687 	u64 size = 0;
5688 	int i;
5689 
5690 	for (i = IRDMA_HMC_IW_QP; i < IRDMA_HMC_IW_MAX; i++) {
5691 		if (hmc_info->hmc_obj[i].mem_loc == mem_loc) {
5692 			size += round_up(hmc_info->hmc_obj[i].cnt *
5693 					 hmc_info->hmc_obj[i].size, 512);
5694 		}
5695 	}
5696 
5697 	return DIV_ROUND_UP(size, IRDMA_HMC_PAGE_SIZE);
5698 }
5699 
5700 /**
5701  * irdma_set_host_hmc_rsrc_gen_3 - calculate host hmc resources for gen 3
5702  * @dev: sc device struct
5703  */
5704 static void irdma_set_host_hmc_rsrc_gen_3(struct irdma_sc_dev *dev)
5705 {
5706 	struct irdma_hmc_fpm_misc *hmc_fpm_misc;
5707 	struct irdma_hmc_info *hmc_info;
5708 	enum irdma_hmc_obj_mem mrte_loc;
5709 	u32 mrwanted, pblewanted;
5710 	u32  avail_sds, mr_sds;
5711 
5712 	hmc_info = dev->hmc_info;
5713 	hmc_fpm_misc = &dev->hmc_fpm_misc;
5714 	avail_sds = hmc_fpm_misc->max_sds;
5715 	mrte_loc = hmc_info->hmc_obj[IRDMA_HMC_IW_MR].mem_loc;
5716 	mrwanted = hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt;
5717 	pblewanted = hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].max_cnt;
5718 
5719 	if (mrte_loc == IRDMA_HOST_MEM && avail_sds > IRDMA_MIN_PBLE_PAGES) {
5720 		mr_sds = avail_sds - IRDMA_MIN_PBLE_PAGES;
5721 		mrwanted = min(mrwanted, mr_sds * MAX_MR_PER_SD);
5722 		hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt = mrwanted;
5723 		avail_sds -= DIV_ROUND_UP(mrwanted, MAX_MR_PER_SD);
5724 	}
5725 
5726 	if (FIELD_GET(IRDMA_MANAGE_RSRC_VER2, dev->feature_info[IRDMA_FTN_FLAGS]) &&
5727 	    pblewanted > avail_sds * MAX_PBLE_PER_SD)
5728 		ibdev_dbg(to_ibdev(dev),
5729 			  "HMC: Warn: Resource version 2: pble wanted = 0x%x available = 0x%x\n",
5730 			  pblewanted, avail_sds * MAX_PBLE_PER_SD);
5731 
5732 	pblewanted = min(pblewanted, avail_sds * MAX_PBLE_PER_SD);
5733 	hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt = pblewanted;
5734 }
5735 
5736 /**
5737  * irdma_verify_commit_fpm_gen_3 - verify query fpm values
5738  * @dev: sc device struct
5739  * @max_pages: max local memory available
5740  * @qpwanted: number of qp's wanted
5741  */
5742 static int irdma_verify_commit_fpm_gen_3(struct irdma_sc_dev *dev,
5743 					 u32 max_pages,
5744 					 u32 qpwanted)
5745 {
5746 	struct irdma_hmc_fpm_misc *hmc_fpm_misc;
5747 	u32 rrf_cnt, xf_cnt, timer_cnt, pages_needed;
5748 	struct irdma_hmc_info *hmc_info;
5749 	u32 rrffl_cnt = 0;
5750 	u32 xffl_cnt = 0;
5751 	u32 q1fl_cnt;
5752 
5753 	hmc_info = dev->hmc_info;
5754 	hmc_fpm_misc = &dev->hmc_fpm_misc;
5755 
5756 	rrf_cnt = roundup_pow_of_two(IRDMA_RRF_MULTIPLIER * qpwanted);
5757 
5758 	if (hmc_info->hmc_obj[IRDMA_HMC_IW_RRFFL].max_cnt)
5759 		rrffl_cnt =
5760 			hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].cnt /
5761 			hmc_fpm_misc->rrf_block_size;
5762 
5763 	xf_cnt = roundup_pow_of_two(IRDMA_XF_MULTIPLIER * qpwanted);
5764 
5765 	if (xf_cnt)
5766 		xffl_cnt = xf_cnt / hmc_fpm_misc->xf_block_size;
5767 
5768 	timer_cnt = (round_up(qpwanted, 512) / 512 + 1) *
5769 		hmc_fpm_misc->timer_bucket;
5770 
5771 	q1fl_cnt = hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].cnt / hmc_fpm_misc->q1_block_size;
5772 
5773 	pages_needed = irdma_get_objs_pages(dev, hmc_info, IRDMA_LOC_MEM);
5774 	if (pages_needed > max_pages) {
5775 		ibdev_dbg(to_ibdev(dev),
5776 			  "HMC: FAIL: SW counts rrf_cnt = %u rrffl_cnt = %u timer_cnt = %u",
5777 			  rrf_cnt, rrffl_cnt, timer_cnt);
5778 		ibdev_dbg(to_ibdev(dev),
5779 			  "HMC: FAIL: SW counts xf_cnt = %u xffl_cnt = %u q1fl_cnt = %u",
5780 			  xf_cnt, xffl_cnt, q1fl_cnt);
5781 
5782 		return -EINVAL;
5783 	}
5784 
5785 	hmc_fpm_misc->max_sds -= pages_needed;
5786 	hmc_fpm_misc->loc_mem_pages -= pages_needed;
5787 
5788 	return 0;
5789 }
5790 
5791 /**
5792  * irdma_set_loc_hmc_rsrc_gen_3 - calculate hmc resources for gen 3
5793  * @dev: sc device struct
5794  * @max_pages: max local memory available
5795  * @qpwanted: number of qp's wanted
5796  */
5797 static int irdma_set_loc_hmc_rsrc_gen_3(struct irdma_sc_dev *dev,
5798 					u32 max_pages,
5799 					u32 qpwanted)
5800 {
5801 	struct irdma_hmc_fpm_misc *hmc_fpm_misc;
5802 	u32 rrf_cnt, xf_cnt, timer_cnt, pages_needed;
5803 	struct irdma_hmc_info *hmc_info;
5804 	u32 ird, ord;
5805 
5806 	if (FIELD_GET(IRDMA_MANAGE_RSRC_VER2, dev->feature_info[IRDMA_FTN_FLAGS]))
5807 		return irdma_verify_commit_fpm_gen_3(dev, max_pages, qpwanted);
5808 
5809 	hmc_info = dev->hmc_info;
5810 	hmc_fpm_misc = &dev->hmc_fpm_misc;
5811 	ird = dev->hw_attrs.max_hw_ird;
5812 	ord = dev->hw_attrs.max_hw_ord;
5813 
5814 	hmc_info->hmc_obj[IRDMA_HMC_IW_HDR].cnt = qpwanted;
5815 	hmc_info->hmc_obj[IRDMA_HMC_IW_QP].cnt = qpwanted;
5816 
5817 	hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt =
5818 		min(hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt, qpwanted * 2);
5819 
5820 	hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt =
5821 		min(qpwanted * 8, hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].max_cnt);
5822 
5823 	rrf_cnt = roundup_pow_of_two(IRDMA_RRF_MULTIPLIER * qpwanted);
5824 	hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].cnt =
5825 		min(hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].max_cnt, rrf_cnt);
5826 
5827 	if (hmc_info->hmc_obj[IRDMA_HMC_IW_RRFFL].max_cnt)
5828 		hmc_info->hmc_obj[IRDMA_HMC_IW_RRFFL].cnt =
5829 			hmc_info->hmc_obj[IRDMA_HMC_IW_RRF].cnt /
5830 			hmc_fpm_misc->rrf_block_size;
5831 
5832 	xf_cnt = roundup_pow_of_two(IRDMA_XF_MULTIPLIER * qpwanted);
5833 	hmc_info->hmc_obj[IRDMA_HMC_IW_XF].cnt =
5834 		min(hmc_info->hmc_obj[IRDMA_HMC_IW_XF].max_cnt, xf_cnt);
5835 	hmc_info->hmc_obj[IRDMA_HMC_IW_XFFL].cnt =
5836 			xf_cnt / hmc_fpm_misc->xf_block_size;
5837 
5838 	timer_cnt = (round_up(qpwanted, 512) / 512 + 1) *
5839 		hmc_fpm_misc->timer_bucket;
5840 	hmc_info->hmc_obj[IRDMA_HMC_IW_TIMER].cnt =
5841 		min(timer_cnt, hmc_info->hmc_obj[IRDMA_HMC_IW_TIMER].cnt);
5842 
5843 	do {
5844 		hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].cnt = roundup_pow_of_two(ird * 2 * qpwanted);
5845 		hmc_info->hmc_obj[IRDMA_HMC_IW_Q1FL].cnt =
5846 			hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].cnt / hmc_fpm_misc->q1_block_size;
5847 
5848 		pages_needed = irdma_get_objs_pages(dev, hmc_info, IRDMA_LOC_MEM);
5849 		if (pages_needed <= max_pages)
5850 			break;
5851 
5852 		ird /= 2;
5853 		ord /= 2;
5854 	} while (ird >= IRDMA_MIN_IRD);
5855 
5856 	if (ird < IRDMA_MIN_IRD) {
5857 		ibdev_dbg(to_ibdev(dev), "HMC: FAIL: IRD=%u Q1 CNT = %u\n",
5858 			  ird, hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].cnt);
5859 		return -EINVAL;
5860 	}
5861 
5862 	dev->hw_attrs.max_hw_ird = ird;
5863 	dev->hw_attrs.max_hw_ord = ord;
5864 	hmc_fpm_misc->max_sds -= pages_needed;
5865 
5866 	return 0;
5867 }
5868 
5869 /**
5870  * cfg_fpm_value_gen_3 - configure fpm for gen 3
5871  * @dev: sc device struct
5872  * @hmc_info: ptr to irdma_hmc_obj_info struct
5873  * @hmc_fpm_misc: ptr to fpm data
5874  */
5875 static int cfg_fpm_value_gen_3(struct irdma_sc_dev *dev,
5876 			       struct irdma_hmc_info *hmc_info,
5877 			       struct irdma_hmc_fpm_misc *hmc_fpm_misc)
5878 {
5879 	enum irdma_hmc_obj_mem mrte_loc;
5880 	u32 mrwanted,  qpwanted;
5881 	int i, ret_code = 0;
5882 	u32 loc_mem_pages;
5883 	bool is_mrte_loc_mem;
5884 
5885 	loc_mem_pages = hmc_fpm_misc->loc_mem_pages;
5886 	is_mrte_loc_mem = hmc_fpm_misc->loc_mem_pages == hmc_fpm_misc->max_sds ?
5887 			true : false;
5888 
5889 	irdma_get_rsrc_mem_config(dev, is_mrte_loc_mem);
5890 	mrte_loc = hmc_info->hmc_obj[IRDMA_HMC_IW_MR].mem_loc;
5891 
5892 	if (is_mrte_loc_mem)
5893 		loc_mem_pages -= IRDMA_MIN_PBLE_PAGES;
5894 
5895 	ibdev_dbg(to_ibdev(dev),
5896 		  "HMC: mrte_loc %d loc_mem %u fpm max sds %u host_obj %d\n",
5897 		  hmc_info->hmc_obj[IRDMA_HMC_IW_MR].mem_loc,
5898 		  hmc_fpm_misc->loc_mem_pages, hmc_fpm_misc->max_sds,
5899 		  is_mrte_loc_mem);
5900 
5901 	mrwanted = hmc_info->hmc_obj[IRDMA_HMC_IW_MR].max_cnt;
5902 	qpwanted = hmc_info->hmc_obj[IRDMA_HMC_IW_QP].max_cnt;
5903 	hmc_info->hmc_obj[IRDMA_HMC_IW_HDR].cnt = qpwanted;
5904 
5905 	hmc_info->hmc_obj[IRDMA_HMC_IW_OOISC].max_cnt = 0;
5906 	hmc_info->hmc_obj[IRDMA_HMC_IW_OOISCFFL].max_cnt = 0;
5907 	hmc_info->hmc_obj[IRDMA_HMC_IW_HTE].max_cnt = 0;
5908 	hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].max_cnt = 0;
5909 
5910 	if (!FIELD_GET(IRDMA_MANAGE_RSRC_VER2, dev->feature_info[IRDMA_FTN_FLAGS]))
5911 		hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].max_cnt =
5912 			min(hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].max_cnt,
5913 			(u32)IRDMA_FSIAV_CNT_MAX);
5914 
5915 	for (i = IRDMA_HMC_IW_QP; i < IRDMA_HMC_IW_MAX; i++)
5916 		hmc_info->hmc_obj[i].cnt = hmc_info->hmc_obj[i].max_cnt;
5917 
5918 	while (qpwanted >= IRDMA_MIN_QP_CNT) {
5919 		if (!irdma_set_loc_hmc_rsrc_gen_3(dev, loc_mem_pages, qpwanted))
5920 			break;
5921 
5922 		if (FIELD_GET(IRDMA_MANAGE_RSRC_VER2, dev->feature_info[IRDMA_FTN_FLAGS]))
5923 			return -EINVAL;
5924 
5925 		qpwanted /= 2;
5926 		if (mrte_loc == IRDMA_LOC_MEM) {
5927 			mrwanted = qpwanted * IRDMA_MIN_MR_PER_QP;
5928 			hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt =
5929 				min(hmc_info->hmc_obj[IRDMA_HMC_IW_MR].max_cnt, mrwanted);
5930 		}
5931 	}
5932 
5933 	if (qpwanted < IRDMA_MIN_QP_CNT) {
5934 		ibdev_dbg(to_ibdev(dev),
5935 			  "HMC: ERROR: could not allocate fpm resources\n");
5936 		return -EINVAL;
5937 	}
5938 
5939 	irdma_set_host_hmc_rsrc_gen_3(dev);
5940 	ret_code = irdma_sc_cfg_iw_fpm(dev, dev->hmc_fn_id);
5941 	if (ret_code) {
5942 		ibdev_dbg(to_ibdev(dev),
5943 			  "HMC: cfg_iw_fpm returned error_code[x%08X]\n",
5944 			  readl(dev->hw_regs[IRDMA_CQPERRCODES]));
5945 
5946 		return ret_code;
5947 	}
5948 
5949 	return irdma_cfg_sd_mem(dev, hmc_info);
5950 }
5951 
5952 /**
5953  * irdma_cfg_fpm_val - configure HMC objects
5954  * @dev: sc device struct
5955  * @qp_count: desired qp count
5956  */
5957 int irdma_cfg_fpm_val(struct irdma_sc_dev *dev, u32 qp_count)
5958 {
5959 	u32 qpwanted, mrwanted, pblewanted;
5960 	u32 powerof2, hte, i;
5961 	u32 sd_needed;
5962 	u32 sd_diff;
5963 	u32 loop_count = 0;
5964 	struct irdma_hmc_info *hmc_info;
5965 	struct irdma_hmc_fpm_misc *hmc_fpm_misc;
5966 	int ret_code = 0;
5967 	u32 max_sds;
5968 
5969 	hmc_info = dev->hmc_info;
5970 	hmc_fpm_misc = &dev->hmc_fpm_misc;
5971 
5972 	ret_code = irdma_sc_init_iw_hmc(dev, dev->hmc_fn_id);
5973 	if (ret_code) {
5974 		ibdev_dbg(to_ibdev(dev),
5975 			  "HMC: irdma_sc_init_iw_hmc returned error_code = %d\n",
5976 			  ret_code);
5977 		return ret_code;
5978 	}
5979 
5980 	max_sds = hmc_fpm_misc->max_sds;
5981 
5982 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3)
5983 		return cfg_fpm_value_gen_3(dev, hmc_info, hmc_fpm_misc);
5984 
5985 	for (i = IRDMA_HMC_IW_QP; i < IRDMA_HMC_IW_MAX; i++)
5986 		hmc_info->hmc_obj[i].cnt = hmc_info->hmc_obj[i].max_cnt;
5987 	sd_needed = irdma_est_sd(dev, hmc_info);
5988 	ibdev_dbg(to_ibdev(dev), "HMC: sd count %u where max sd is %u\n",
5989 		  hmc_info->sd_table.sd_cnt, max_sds);
5990 
5991 	qpwanted = min(qp_count, hmc_info->hmc_obj[IRDMA_HMC_IW_QP].max_cnt);
5992 
5993 	powerof2 = 1;
5994 	while (powerof2 <= qpwanted)
5995 		powerof2 *= 2;
5996 	powerof2 /= 2;
5997 	qpwanted = powerof2;
5998 
5999 	mrwanted = hmc_info->hmc_obj[IRDMA_HMC_IW_MR].max_cnt;
6000 	pblewanted = hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].max_cnt;
6001 
6002 	ibdev_dbg(to_ibdev(dev),
6003 		  "HMC: req_qp=%d max_sd=%u, max_qp = %u, max_cq=%u, max_mr=%u, max_pble=%u, mc=%d, av=%u\n",
6004 		  qp_count, max_sds,
6005 		  hmc_info->hmc_obj[IRDMA_HMC_IW_QP].max_cnt,
6006 		  hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].max_cnt,
6007 		  hmc_info->hmc_obj[IRDMA_HMC_IW_MR].max_cnt,
6008 		  hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].max_cnt,
6009 		  hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].max_cnt,
6010 		  hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].max_cnt);
6011 
6012 	hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].cnt =
6013 		hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].max_cnt;
6014 	hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt =
6015 		hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].max_cnt;
6016 	hmc_info->hmc_obj[IRDMA_HMC_IW_ARP].cnt =
6017 		hmc_info->hmc_obj[IRDMA_HMC_IW_ARP].max_cnt;
6018 	hmc_info->hmc_obj[IRDMA_HMC_IW_APBVT_ENTRY].cnt = 1;
6019 
6020 	while (irdma_q1_cnt(dev, hmc_info, qpwanted) > hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].max_cnt)
6021 		qpwanted /= 2;
6022 
6023 	do {
6024 		++loop_count;
6025 		hmc_info->hmc_obj[IRDMA_HMC_IW_QP].cnt = qpwanted;
6026 		hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt =
6027 			min(2 * qpwanted, hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt);
6028 		hmc_info->hmc_obj[IRDMA_HMC_IW_SRQ].cnt = 0; /* Reserved */
6029 		hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt = mrwanted;
6030 
6031 		hte = round_up(qpwanted + hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].cnt, 512);
6032 		powerof2 = 1;
6033 		while (powerof2 < hte)
6034 			powerof2 *= 2;
6035 		hmc_info->hmc_obj[IRDMA_HMC_IW_HTE].cnt =
6036 			powerof2 * hmc_fpm_misc->ht_multiplier;
6037 		if (dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
6038 			cfg_fpm_value_gen_1(dev, hmc_info, qpwanted);
6039 		else
6040 			cfg_fpm_value_gen_2(dev, hmc_info, qpwanted);
6041 
6042 		hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].cnt = irdma_q1_cnt(dev, hmc_info, qpwanted);
6043 		hmc_info->hmc_obj[IRDMA_HMC_IW_XFFL].cnt =
6044 			hmc_info->hmc_obj[IRDMA_HMC_IW_XF].cnt / hmc_fpm_misc->xf_block_size;
6045 		hmc_info->hmc_obj[IRDMA_HMC_IW_Q1FL].cnt =
6046 			hmc_info->hmc_obj[IRDMA_HMC_IW_Q1].cnt / hmc_fpm_misc->q1_block_size;
6047 		hmc_info->hmc_obj[IRDMA_HMC_IW_TIMER].cnt =
6048 			(round_up(qpwanted, 512) / 512 + 1) * hmc_fpm_misc->timer_bucket;
6049 
6050 		hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt = pblewanted;
6051 		sd_needed = irdma_est_sd(dev, hmc_info);
6052 		ibdev_dbg(to_ibdev(dev),
6053 			  "HMC: sd_needed = %d, hmc_fpm_misc->max_sds=%d, mrwanted=%d, pblewanted=%d qpwanted=%d\n",
6054 			  sd_needed, hmc_fpm_misc->max_sds, mrwanted,
6055 			  pblewanted, qpwanted);
6056 
6057 		/* Do not reduce resources further. All objects fit with max SDs */
6058 		if (sd_needed <= hmc_fpm_misc->max_sds)
6059 			break;
6060 
6061 		sd_diff = sd_needed - hmc_fpm_misc->max_sds;
6062 		if (sd_diff > 128) {
6063 			if (!(loop_count % 2) && qpwanted > 128) {
6064 				qpwanted /= 2;
6065 			} else {
6066 				pblewanted /= 2;
6067 				mrwanted /= 2;
6068 			}
6069 			continue;
6070 		}
6071 
6072 		if (dev->cqp->hmc_profile != IRDMA_HMC_PROFILE_FAVOR_VF &&
6073 		    pblewanted > (512 * FPM_MULTIPLIER * sd_diff)) {
6074 			pblewanted -= 256 * FPM_MULTIPLIER * sd_diff;
6075 			continue;
6076 		} else if (pblewanted > (100 * FPM_MULTIPLIER)) {
6077 			pblewanted -= 10 * FPM_MULTIPLIER;
6078 		} else if (pblewanted > FPM_MULTIPLIER) {
6079 			pblewanted -= FPM_MULTIPLIER;
6080 		} else if (qpwanted <= 128) {
6081 			if (hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].cnt > 256)
6082 				hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].cnt /= 2;
6083 			if (hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt > 256)
6084 				hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt /= 2;
6085 		}
6086 		if (mrwanted > FPM_MULTIPLIER)
6087 			mrwanted -= FPM_MULTIPLIER;
6088 		if (!(loop_count % 10) && qpwanted > 128) {
6089 			qpwanted /= 2;
6090 			if (hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt > 256)
6091 				hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt /= 2;
6092 		}
6093 	} while (loop_count < 2000);
6094 
6095 	if (sd_needed > hmc_fpm_misc->max_sds) {
6096 		ibdev_dbg(to_ibdev(dev),
6097 			  "HMC: cfg_fpm failed loop_cnt=%u, sd_needed=%u, max sd count %u\n",
6098 			  loop_count, sd_needed, hmc_info->sd_table.sd_cnt);
6099 		return -EINVAL;
6100 	}
6101 
6102 	if (loop_count > 1 && sd_needed < max_sds) {
6103 		pblewanted += (max_sds - sd_needed) * 256 * FPM_MULTIPLIER;
6104 		hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt = pblewanted;
6105 		sd_needed = irdma_est_sd(dev, hmc_info);
6106 	}
6107 
6108 	ibdev_dbg(to_ibdev(dev),
6109 		  "HMC: loop_cnt=%d, sd_needed=%d, qpcnt = %d, cqcnt=%d, mrcnt=%d, pblecnt=%d, mc=%d, ah=%d, max sd count %d, first sd index %d\n",
6110 		  loop_count, sd_needed,
6111 		  hmc_info->hmc_obj[IRDMA_HMC_IW_QP].cnt,
6112 		  hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt,
6113 		  hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt,
6114 		  hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].cnt,
6115 		  hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].cnt,
6116 		  hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt,
6117 		  hmc_info->sd_table.sd_cnt, hmc_info->first_sd_index);
6118 
6119 	ret_code = irdma_sc_cfg_iw_fpm(dev, dev->hmc_fn_id);
6120 	if (ret_code) {
6121 		ibdev_dbg(to_ibdev(dev),
6122 			  "HMC: cfg_iw_fpm returned error_code[x%08X]\n",
6123 			  readl(dev->hw_regs[IRDMA_CQPERRCODES]));
6124 		return ret_code;
6125 	}
6126 
6127 	return irdma_cfg_sd_mem(dev, hmc_info);
6128 }
6129 
6130 /**
6131  * irdma_exec_cqp_cmd - execute cqp cmd when wqe are available
6132  * @dev: rdma device
6133  * @pcmdinfo: cqp command info
6134  */
6135 static int irdma_exec_cqp_cmd(struct irdma_sc_dev *dev,
6136 			      struct cqp_cmds_info *pcmdinfo)
6137 {
6138 	int status;
6139 	struct irdma_dma_mem val_mem;
6140 	bool alloc = false;
6141 
6142 	dev->cqp_cmd_stats[pcmdinfo->cqp_cmd]++;
6143 	switch (pcmdinfo->cqp_cmd) {
6144 	case IRDMA_OP_CEQ_DESTROY:
6145 		status = irdma_sc_ceq_destroy(pcmdinfo->in.u.ceq_destroy.ceq,
6146 					      pcmdinfo->in.u.ceq_destroy.scratch,
6147 					      pcmdinfo->post_sq);
6148 		break;
6149 	case IRDMA_OP_AEQ_DESTROY:
6150 		status = irdma_sc_aeq_destroy(pcmdinfo->in.u.aeq_destroy.aeq,
6151 					      pcmdinfo->in.u.aeq_destroy.scratch,
6152 					      pcmdinfo->post_sq);
6153 
6154 		break;
6155 	case IRDMA_OP_CEQ_CREATE:
6156 		status = irdma_sc_ceq_create(pcmdinfo->in.u.ceq_create.ceq,
6157 					     pcmdinfo->in.u.ceq_create.scratch,
6158 					     pcmdinfo->post_sq);
6159 		break;
6160 	case IRDMA_OP_AEQ_CREATE:
6161 		status = irdma_sc_aeq_create(pcmdinfo->in.u.aeq_create.aeq,
6162 					     pcmdinfo->in.u.aeq_create.scratch,
6163 					     pcmdinfo->post_sq);
6164 		break;
6165 	case IRDMA_OP_QP_UPLOAD_CONTEXT:
6166 		status = irdma_sc_qp_upload_context(pcmdinfo->in.u.qp_upload_context.dev,
6167 						    &pcmdinfo->in.u.qp_upload_context.info,
6168 						    pcmdinfo->in.u.qp_upload_context.scratch,
6169 						    pcmdinfo->post_sq);
6170 		break;
6171 	case IRDMA_OP_CQ_CREATE:
6172 		status = irdma_sc_cq_create(pcmdinfo->in.u.cq_create.cq,
6173 					    pcmdinfo->in.u.cq_create.scratch,
6174 					    pcmdinfo->in.u.cq_create.check_overflow,
6175 					    pcmdinfo->post_sq);
6176 		break;
6177 	case IRDMA_OP_CQ_MODIFY:
6178 		status = irdma_sc_cq_modify(pcmdinfo->in.u.cq_modify.cq,
6179 					    &pcmdinfo->in.u.cq_modify.info,
6180 					    pcmdinfo->in.u.cq_modify.scratch,
6181 					    pcmdinfo->post_sq);
6182 		break;
6183 	case IRDMA_OP_CQ_DESTROY:
6184 		status = irdma_sc_cq_destroy(pcmdinfo->in.u.cq_destroy.cq,
6185 					     pcmdinfo->in.u.cq_destroy.scratch,
6186 					     pcmdinfo->post_sq);
6187 		break;
6188 	case IRDMA_OP_QP_FLUSH_WQES:
6189 		status = irdma_sc_qp_flush_wqes(pcmdinfo->in.u.qp_flush_wqes.qp,
6190 						&pcmdinfo->in.u.qp_flush_wqes.info,
6191 						pcmdinfo->in.u.qp_flush_wqes.scratch,
6192 						pcmdinfo->post_sq);
6193 		break;
6194 	case IRDMA_OP_GEN_AE:
6195 		status = irdma_sc_gen_ae(pcmdinfo->in.u.gen_ae.qp,
6196 					 &pcmdinfo->in.u.gen_ae.info,
6197 					 pcmdinfo->in.u.gen_ae.scratch,
6198 					 pcmdinfo->post_sq);
6199 		break;
6200 	case IRDMA_OP_MANAGE_PUSH_PAGE:
6201 		status = irdma_sc_manage_push_page(pcmdinfo->in.u.manage_push_page.cqp,
6202 						   &pcmdinfo->in.u.manage_push_page.info,
6203 						   pcmdinfo->in.u.manage_push_page.scratch,
6204 						   pcmdinfo->post_sq);
6205 		break;
6206 	case IRDMA_OP_UPDATE_PE_SDS:
6207 		status = irdma_update_pe_sds(pcmdinfo->in.u.update_pe_sds.dev,
6208 					     &pcmdinfo->in.u.update_pe_sds.info,
6209 					     pcmdinfo->in.u.update_pe_sds.scratch);
6210 		break;
6211 	case IRDMA_OP_MANAGE_HMC_PM_FUNC_TABLE:
6212 		/* switch to calling through the call table */
6213 		status =
6214 			irdma_sc_manage_hmc_pm_func_table(pcmdinfo->in.u.manage_hmc_pm.dev->cqp,
6215 							  &pcmdinfo->in.u.manage_hmc_pm.info,
6216 							  pcmdinfo->in.u.manage_hmc_pm.scratch,
6217 							  true);
6218 		break;
6219 	case IRDMA_OP_SUSPEND:
6220 		status = irdma_sc_suspend_qp(pcmdinfo->in.u.suspend_resume.cqp,
6221 					     pcmdinfo->in.u.suspend_resume.qp,
6222 					     pcmdinfo->in.u.suspend_resume.scratch);
6223 		break;
6224 	case IRDMA_OP_RESUME:
6225 		status = irdma_sc_resume_qp(pcmdinfo->in.u.suspend_resume.cqp,
6226 					    pcmdinfo->in.u.suspend_resume.qp,
6227 					    pcmdinfo->in.u.suspend_resume.scratch);
6228 		break;
6229 	case IRDMA_OP_QUERY_FPM_VAL:
6230 		val_mem.pa = pcmdinfo->in.u.query_fpm_val.fpm_val_pa;
6231 		val_mem.va = pcmdinfo->in.u.query_fpm_val.fpm_val_va;
6232 		status = irdma_sc_query_fpm_val(pcmdinfo->in.u.query_fpm_val.cqp,
6233 						pcmdinfo->in.u.query_fpm_val.scratch,
6234 						pcmdinfo->in.u.query_fpm_val.hmc_fn_id,
6235 						&val_mem, true, IRDMA_CQP_WAIT_EVENT);
6236 		break;
6237 	case IRDMA_OP_COMMIT_FPM_VAL:
6238 		val_mem.pa = pcmdinfo->in.u.commit_fpm_val.fpm_val_pa;
6239 		val_mem.va = pcmdinfo->in.u.commit_fpm_val.fpm_val_va;
6240 		status = irdma_sc_commit_fpm_val(pcmdinfo->in.u.commit_fpm_val.cqp,
6241 						 pcmdinfo->in.u.commit_fpm_val.scratch,
6242 						 pcmdinfo->in.u.commit_fpm_val.hmc_fn_id,
6243 						 &val_mem,
6244 						 true,
6245 						 IRDMA_CQP_WAIT_EVENT);
6246 		break;
6247 	case IRDMA_OP_STATS_ALLOCATE:
6248 		alloc = true;
6249 		fallthrough;
6250 	case IRDMA_OP_STATS_FREE:
6251 		status = irdma_sc_manage_stats_inst(pcmdinfo->in.u.stats_manage.cqp,
6252 						    &pcmdinfo->in.u.stats_manage.info,
6253 						    alloc,
6254 						    pcmdinfo->in.u.stats_manage.scratch);
6255 		break;
6256 	case IRDMA_OP_STATS_GATHER:
6257 		status = irdma_sc_gather_stats(pcmdinfo->in.u.stats_gather.cqp,
6258 					       &pcmdinfo->in.u.stats_gather.info,
6259 					       pcmdinfo->in.u.stats_gather.scratch);
6260 		break;
6261 	case IRDMA_OP_WS_MODIFY_NODE:
6262 		status = irdma_sc_manage_ws_node(pcmdinfo->in.u.ws_node.cqp,
6263 						 &pcmdinfo->in.u.ws_node.info,
6264 						 IRDMA_MODIFY_NODE,
6265 						 pcmdinfo->in.u.ws_node.scratch);
6266 		break;
6267 	case IRDMA_OP_WS_DELETE_NODE:
6268 		status = irdma_sc_manage_ws_node(pcmdinfo->in.u.ws_node.cqp,
6269 						 &pcmdinfo->in.u.ws_node.info,
6270 						 IRDMA_DEL_NODE,
6271 						 pcmdinfo->in.u.ws_node.scratch);
6272 		break;
6273 	case IRDMA_OP_WS_ADD_NODE:
6274 		status = irdma_sc_manage_ws_node(pcmdinfo->in.u.ws_node.cqp,
6275 						 &pcmdinfo->in.u.ws_node.info,
6276 						 IRDMA_ADD_NODE,
6277 						 pcmdinfo->in.u.ws_node.scratch);
6278 		break;
6279 	case IRDMA_OP_SET_UP_MAP:
6280 		status = irdma_sc_set_up_map(pcmdinfo->in.u.up_map.cqp,
6281 					     &pcmdinfo->in.u.up_map.info,
6282 					     pcmdinfo->in.u.up_map.scratch);
6283 		break;
6284 	case IRDMA_OP_QUERY_RDMA_FEATURES:
6285 		status = irdma_sc_query_rdma_features(pcmdinfo->in.u.query_rdma.cqp,
6286 						      &pcmdinfo->in.u.query_rdma.query_buff_mem,
6287 						      pcmdinfo->in.u.query_rdma.scratch);
6288 		break;
6289 	case IRDMA_OP_DELETE_ARP_CACHE_ENTRY:
6290 		status = irdma_sc_del_arp_cache_entry(pcmdinfo->in.u.del_arp_cache_entry.cqp,
6291 						      pcmdinfo->in.u.del_arp_cache_entry.scratch,
6292 						      pcmdinfo->in.u.del_arp_cache_entry.arp_index,
6293 						      pcmdinfo->post_sq);
6294 		break;
6295 	case IRDMA_OP_MANAGE_APBVT_ENTRY:
6296 		status = irdma_sc_manage_apbvt_entry(pcmdinfo->in.u.manage_apbvt_entry.cqp,
6297 						     &pcmdinfo->in.u.manage_apbvt_entry.info,
6298 						     pcmdinfo->in.u.manage_apbvt_entry.scratch,
6299 						     pcmdinfo->post_sq);
6300 		break;
6301 	case IRDMA_OP_MANAGE_QHASH_TABLE_ENTRY:
6302 		status = irdma_sc_manage_qhash_table_entry(pcmdinfo->in.u.manage_qhash_table_entry.cqp,
6303 							   &pcmdinfo->in.u.manage_qhash_table_entry.info,
6304 							   pcmdinfo->in.u.manage_qhash_table_entry.scratch,
6305 							   pcmdinfo->post_sq);
6306 		break;
6307 	case IRDMA_OP_QP_MODIFY:
6308 		status = irdma_sc_qp_modify(pcmdinfo->in.u.qp_modify.qp,
6309 					    &pcmdinfo->in.u.qp_modify.info,
6310 					    pcmdinfo->in.u.qp_modify.scratch,
6311 					    pcmdinfo->post_sq);
6312 		break;
6313 	case IRDMA_OP_QP_CREATE:
6314 		status = irdma_sc_qp_create(pcmdinfo->in.u.qp_create.qp,
6315 					    &pcmdinfo->in.u.qp_create.info,
6316 					    pcmdinfo->in.u.qp_create.scratch,
6317 					    pcmdinfo->post_sq);
6318 		break;
6319 	case IRDMA_OP_QP_DESTROY:
6320 		status = irdma_sc_qp_destroy(pcmdinfo->in.u.qp_destroy.qp,
6321 					     pcmdinfo->in.u.qp_destroy.scratch,
6322 					     pcmdinfo->in.u.qp_destroy.remove_hash_idx,
6323 					     pcmdinfo->in.u.qp_destroy.ignore_mw_bnd,
6324 					     pcmdinfo->post_sq);
6325 		break;
6326 	case IRDMA_OP_ALLOC_STAG:
6327 		status = irdma_sc_alloc_stag(pcmdinfo->in.u.alloc_stag.dev,
6328 					     &pcmdinfo->in.u.alloc_stag.info,
6329 					     pcmdinfo->in.u.alloc_stag.scratch,
6330 					     pcmdinfo->post_sq);
6331 		break;
6332 	case IRDMA_OP_MR_REG_NON_SHARED:
6333 		status = irdma_sc_mr_reg_non_shared(pcmdinfo->in.u.mr_reg_non_shared.dev,
6334 						    &pcmdinfo->in.u.mr_reg_non_shared.info,
6335 						    pcmdinfo->in.u.mr_reg_non_shared.scratch,
6336 						    pcmdinfo->post_sq);
6337 		break;
6338 	case IRDMA_OP_DEALLOC_STAG:
6339 		status = irdma_sc_dealloc_stag(pcmdinfo->in.u.dealloc_stag.dev,
6340 					       &pcmdinfo->in.u.dealloc_stag.info,
6341 					       pcmdinfo->in.u.dealloc_stag.scratch,
6342 					       pcmdinfo->post_sq);
6343 		break;
6344 	case IRDMA_OP_MW_ALLOC:
6345 		status = irdma_sc_mw_alloc(pcmdinfo->in.u.mw_alloc.dev,
6346 					   &pcmdinfo->in.u.mw_alloc.info,
6347 					   pcmdinfo->in.u.mw_alloc.scratch,
6348 					   pcmdinfo->post_sq);
6349 		break;
6350 	case IRDMA_OP_ADD_ARP_CACHE_ENTRY:
6351 		status = irdma_sc_add_arp_cache_entry(pcmdinfo->in.u.add_arp_cache_entry.cqp,
6352 						      &pcmdinfo->in.u.add_arp_cache_entry.info,
6353 						      pcmdinfo->in.u.add_arp_cache_entry.scratch,
6354 						      pcmdinfo->post_sq);
6355 		break;
6356 	case IRDMA_OP_ALLOC_LOCAL_MAC_ENTRY:
6357 		status = irdma_sc_alloc_local_mac_entry(pcmdinfo->in.u.alloc_local_mac_entry.cqp,
6358 							pcmdinfo->in.u.alloc_local_mac_entry.scratch,
6359 							pcmdinfo->post_sq);
6360 		break;
6361 	case IRDMA_OP_ADD_LOCAL_MAC_ENTRY:
6362 		status = irdma_sc_add_local_mac_entry(pcmdinfo->in.u.add_local_mac_entry.cqp,
6363 						      &pcmdinfo->in.u.add_local_mac_entry.info,
6364 						      pcmdinfo->in.u.add_local_mac_entry.scratch,
6365 						      pcmdinfo->post_sq);
6366 		break;
6367 	case IRDMA_OP_DELETE_LOCAL_MAC_ENTRY:
6368 		status = irdma_sc_del_local_mac_entry(pcmdinfo->in.u.del_local_mac_entry.cqp,
6369 						      pcmdinfo->in.u.del_local_mac_entry.scratch,
6370 						      pcmdinfo->in.u.del_local_mac_entry.entry_idx,
6371 						      pcmdinfo->in.u.del_local_mac_entry.ignore_ref_count,
6372 						      pcmdinfo->post_sq);
6373 		break;
6374 	case IRDMA_OP_AH_CREATE:
6375 		status = irdma_sc_create_ah(pcmdinfo->in.u.ah_create.cqp,
6376 					    &pcmdinfo->in.u.ah_create.info,
6377 					    pcmdinfo->in.u.ah_create.scratch);
6378 		break;
6379 	case IRDMA_OP_AH_DESTROY:
6380 		status = irdma_sc_destroy_ah(pcmdinfo->in.u.ah_destroy.cqp,
6381 					     &pcmdinfo->in.u.ah_destroy.info,
6382 					     pcmdinfo->in.u.ah_destroy.scratch);
6383 		break;
6384 	case IRDMA_OP_MC_CREATE:
6385 		status = irdma_sc_create_mcast_grp(pcmdinfo->in.u.mc_create.cqp,
6386 						   &pcmdinfo->in.u.mc_create.info,
6387 						   pcmdinfo->in.u.mc_create.scratch);
6388 		break;
6389 	case IRDMA_OP_MC_DESTROY:
6390 		status = irdma_sc_destroy_mcast_grp(pcmdinfo->in.u.mc_destroy.cqp,
6391 						    &pcmdinfo->in.u.mc_destroy.info,
6392 						    pcmdinfo->in.u.mc_destroy.scratch);
6393 		break;
6394 	case IRDMA_OP_MC_MODIFY:
6395 		status = irdma_sc_modify_mcast_grp(pcmdinfo->in.u.mc_modify.cqp,
6396 						   &pcmdinfo->in.u.mc_modify.info,
6397 						   pcmdinfo->in.u.mc_modify.scratch);
6398 		break;
6399 	case IRDMA_OP_SRQ_CREATE:
6400 		status = irdma_sc_srq_create(pcmdinfo->in.u.srq_create.srq,
6401 					     pcmdinfo->in.u.srq_create.scratch,
6402 					     pcmdinfo->post_sq);
6403 		break;
6404 	case IRDMA_OP_SRQ_MODIFY:
6405 		status = irdma_sc_srq_modify(pcmdinfo->in.u.srq_modify.srq,
6406 					     &pcmdinfo->in.u.srq_modify.info,
6407 					     pcmdinfo->in.u.srq_modify.scratch,
6408 					     pcmdinfo->post_sq);
6409 		break;
6410 	case IRDMA_OP_SRQ_DESTROY:
6411 		status = irdma_sc_srq_destroy(pcmdinfo->in.u.srq_destroy.srq,
6412 					      pcmdinfo->in.u.srq_destroy.scratch,
6413 					      pcmdinfo->post_sq);
6414 		break;
6415 	default:
6416 		status = -EOPNOTSUPP;
6417 		break;
6418 	}
6419 
6420 	return status;
6421 }
6422 
6423 /**
6424  * irdma_process_cqp_cmd - process all cqp commands
6425  * @dev: sc device struct
6426  * @pcmdinfo: cqp command info
6427  */
6428 int irdma_process_cqp_cmd(struct irdma_sc_dev *dev,
6429 			  struct cqp_cmds_info *pcmdinfo)
6430 {
6431 	int status = 0;
6432 	unsigned long flags;
6433 
6434 	spin_lock_irqsave(&dev->cqp_lock, flags);
6435 	if (list_empty(&dev->cqp_cmd_head) && !irdma_cqp_ring_full(dev->cqp))
6436 		status = irdma_exec_cqp_cmd(dev, pcmdinfo);
6437 	else
6438 		list_add_tail(&pcmdinfo->cqp_cmd_entry, &dev->cqp_cmd_head);
6439 	spin_unlock_irqrestore(&dev->cqp_lock, flags);
6440 	return status;
6441 }
6442 
6443 /**
6444  * irdma_process_bh - called from tasklet for cqp list
6445  * @dev: sc device struct
6446  */
6447 int irdma_process_bh(struct irdma_sc_dev *dev)
6448 {
6449 	int status = 0;
6450 	struct cqp_cmds_info *pcmdinfo;
6451 	unsigned long flags;
6452 
6453 	spin_lock_irqsave(&dev->cqp_lock, flags);
6454 	while (!list_empty(&dev->cqp_cmd_head) &&
6455 	       !irdma_cqp_ring_full(dev->cqp)) {
6456 		pcmdinfo = (struct cqp_cmds_info *)irdma_remove_cqp_head(dev);
6457 		status = irdma_exec_cqp_cmd(dev, pcmdinfo);
6458 		if (status)
6459 			break;
6460 	}
6461 	spin_unlock_irqrestore(&dev->cqp_lock, flags);
6462 	return status;
6463 }
6464 
6465 /**
6466  * irdma_cfg_aeq- Configure AEQ interrupt
6467  * @dev: pointer to the device structure
6468  * @idx: vector index
6469  * @enable: True to enable, False disables
6470  */
6471 void irdma_cfg_aeq(struct irdma_sc_dev *dev, u32 idx, bool enable)
6472 {
6473 	u32 reg_val;
6474 
6475 	reg_val = FIELD_PREP(IRDMA_PFINT_AEQCTL_CAUSE_ENA, enable) |
6476 		  FIELD_PREP(IRDMA_PFINT_AEQCTL_MSIX_INDX, idx) |
6477 		  FIELD_PREP(IRDMA_PFINT_AEQCTL_ITR_INDX, 3);
6478 	writel(reg_val, dev->hw_regs[IRDMA_PFINT_AEQCTL]);
6479 }
6480 
6481 /**
6482  * sc_vsi_update_stats - Update statistics
6483  * @vsi: sc_vsi instance to update
6484  */
6485 void sc_vsi_update_stats(struct irdma_sc_vsi *vsi)
6486 {
6487 	struct irdma_dev_hw_stats *hw_stats = &vsi->pestat->hw_stats;
6488 	struct irdma_gather_stats *gather_stats =
6489 		vsi->pestat->gather_info.gather_stats_va;
6490 	struct irdma_gather_stats *last_gather_stats =
6491 		vsi->pestat->gather_info.last_gather_stats_va;
6492 	const struct irdma_hw_stat_map *map = vsi->dev->hw_stats_map;
6493 	u16 max_stat_idx = vsi->dev->hw_attrs.max_stat_idx;
6494 	u16 i;
6495 
6496 	if (vsi->dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3) {
6497 		for (i = 0; i < max_stat_idx; i++) {
6498 			u16 idx = map[i].byteoff / sizeof(u64);
6499 
6500 			hw_stats->stats_val[i] = gather_stats->val[idx];
6501 		}
6502 		return;
6503 	}
6504 
6505 	irdma_update_stats(hw_stats, gather_stats, last_gather_stats,
6506 			   map, max_stat_idx);
6507 }
6508 
6509 /**
6510  * irdma_wait_pe_ready - Check if firmware is ready
6511  * @dev: provides access to registers
6512  */
6513 static int irdma_wait_pe_ready(struct irdma_sc_dev *dev)
6514 {
6515 	u32 statuscpu0;
6516 	u32 statuscpu1;
6517 	u32 statuscpu2;
6518 	u32 retrycount = 0;
6519 
6520 	do {
6521 		statuscpu0 = readl(dev->hw_regs[IRDMA_GLPE_CPUSTATUS0]);
6522 		statuscpu1 = readl(dev->hw_regs[IRDMA_GLPE_CPUSTATUS1]);
6523 		statuscpu2 = readl(dev->hw_regs[IRDMA_GLPE_CPUSTATUS2]);
6524 		if (statuscpu0 == 0x80 && statuscpu1 == 0x80 &&
6525 		    statuscpu2 == 0x80)
6526 			return 0;
6527 		mdelay(1000);
6528 	} while (retrycount++ < dev->hw_attrs.max_pe_ready_count);
6529 	return -1;
6530 }
6531 
6532 static inline void irdma_sc_init_hw(struct irdma_sc_dev *dev)
6533 {
6534 	switch (dev->hw_attrs.uk_attrs.hw_rev) {
6535 	case IRDMA_GEN_1:
6536 		i40iw_init_hw(dev);
6537 		break;
6538 	case IRDMA_GEN_2:
6539 		icrdma_init_hw(dev);
6540 		break;
6541 	case IRDMA_GEN_3:
6542 		ig3rdma_init_hw(dev);
6543 		break;
6544 	}
6545 }
6546 
6547 /**
6548  * irdma_sc_dev_init - Initialize control part of device
6549  * @ver: version
6550  * @dev: Device pointer
6551  * @info: Device init info
6552  */
6553 int irdma_sc_dev_init(enum irdma_vers ver, struct irdma_sc_dev *dev,
6554 		      struct irdma_device_init_info *info)
6555 {
6556 	u32 val;
6557 	int ret_code = 0;
6558 	u8 db_size;
6559 
6560 	INIT_LIST_HEAD(&dev->cqp_cmd_head); /* for CQP command backlog */
6561 	mutex_init(&dev->ws_mutex);
6562 	dev->hmc_fn_id = info->hmc_fn_id;
6563 	dev->fpm_query_buf_pa = info->fpm_query_buf_pa;
6564 	dev->fpm_query_buf = info->fpm_query_buf;
6565 	dev->fpm_commit_buf_pa = info->fpm_commit_buf_pa;
6566 	dev->fpm_commit_buf = info->fpm_commit_buf;
6567 	dev->hw = info->hw;
6568 	dev->hw->hw_addr = info->bar0;
6569 	dev->protocol_used = info->protocol_used;
6570 	/* Setup the hardware limits, hmc may limit further */
6571 	dev->hw_attrs.min_hw_qp_id = IRDMA_MIN_IW_QP_ID;
6572 	dev->hw_attrs.min_hw_srq_id = IRDMA_MIN_IW_SRQ_ID;
6573 	dev->hw_attrs.min_hw_aeq_size = IRDMA_MIN_AEQ_ENTRIES;
6574 	if (dev->hw_attrs.uk_attrs.hw_rev >= IRDMA_GEN_3)
6575 		dev->hw_attrs.max_hw_aeq_size = IRDMA_MAX_AEQ_ENTRIES_GEN_3;
6576 	else
6577 		dev->hw_attrs.max_hw_aeq_size = IRDMA_MAX_AEQ_ENTRIES;
6578 	dev->hw_attrs.min_hw_ceq_size = IRDMA_MIN_CEQ_ENTRIES;
6579 	dev->hw_attrs.max_hw_ceq_size = IRDMA_MAX_CEQ_ENTRIES;
6580 	dev->hw_attrs.uk_attrs.min_hw_cq_size = IRDMA_MIN_CQ_SIZE;
6581 	dev->hw_attrs.uk_attrs.max_hw_cq_size = IRDMA_MAX_CQ_SIZE;
6582 	dev->hw_attrs.uk_attrs.max_hw_wq_frags = IRDMA_MAX_WQ_FRAGMENT_COUNT;
6583 	dev->hw_attrs.uk_attrs.max_hw_read_sges = IRDMA_MAX_SGE_RD;
6584 	dev->hw_attrs.max_hw_outbound_msg_size = IRDMA_MAX_OUTBOUND_MSG_SIZE;
6585 	dev->hw_attrs.max_mr_size = IRDMA_MAX_MR_SIZE;
6586 	dev->hw_attrs.max_hw_inbound_msg_size = IRDMA_MAX_INBOUND_MSG_SIZE;
6587 	dev->hw_attrs.max_hw_device_pages = IRDMA_MAX_PUSH_PAGE_COUNT;
6588 	dev->hw_attrs.uk_attrs.max_hw_inline = IRDMA_MAX_INLINE_DATA_SIZE;
6589 	dev->hw_attrs.max_hw_wqes = IRDMA_MAX_WQ_ENTRIES;
6590 	dev->hw_attrs.max_qp_wr = IRDMA_MAX_QP_WRS(IRDMA_MAX_QUANTA_PER_WR);
6591 
6592 	dev->hw_attrs.uk_attrs.max_hw_rq_quanta = IRDMA_QP_SW_MAX_RQ_QUANTA;
6593 	dev->hw_attrs.uk_attrs.max_hw_wq_quanta = IRDMA_QP_SW_MAX_WQ_QUANTA;
6594 	dev->hw_attrs.max_hw_pds = IRDMA_MAX_PDS;
6595 	dev->hw_attrs.max_hw_ena_vf_count = IRDMA_MAX_PE_ENA_VF_COUNT;
6596 
6597 	dev->hw_attrs.max_pe_ready_count = 14;
6598 	dev->hw_attrs.max_done_count = IRDMA_DONE_COUNT;
6599 	dev->hw_attrs.max_sleep_count = IRDMA_SLEEP_COUNT;
6600 	dev->hw_attrs.max_cqp_compl_wait_time_ms = CQP_COMPL_WAIT_TIME_MS;
6601 
6602 	if (!dev->privileged) {
6603 		ret_code = irdma_vchnl_req_get_hmc_fcn(dev);
6604 		if (ret_code) {
6605 			ibdev_dbg(to_ibdev(dev),
6606 				  "DEV: Get HMC function ret = %d\n",
6607 				  ret_code);
6608 
6609 			return ret_code;
6610 		}
6611 	}
6612 
6613 	irdma_sc_init_hw(dev);
6614 
6615 	if (dev->privileged) {
6616 		if (irdma_wait_pe_ready(dev))
6617 			return -ETIMEDOUT;
6618 
6619 		val = readl(dev->hw_regs[IRDMA_GLPCI_LBARCTRL]);
6620 		db_size = (u8)FIELD_GET(IRDMA_GLPCI_LBARCTRL_PE_DB_SIZE, val);
6621 		if (db_size != IRDMA_PE_DB_SIZE_4M &&
6622 		    db_size != IRDMA_PE_DB_SIZE_8M) {
6623 			ibdev_dbg(to_ibdev(dev),
6624 				  "DEV: RDMA PE doorbell is not enabled in CSR val 0x%x db_size=%d\n",
6625 				  val, db_size);
6626 			return -ENODEV;
6627 			}
6628 	} else {
6629 		ret_code = irdma_vchnl_req_get_reg_layout(dev);
6630 		if (ret_code)
6631 			ibdev_dbg(to_ibdev(dev),
6632 				  "DEV: Get Register layout failed ret = %d\n",
6633 				  ret_code);
6634 	}
6635 
6636 	return ret_code;
6637 }
6638 
6639 /**
6640  * irdma_stat_val - Extract HW counter value from statistics buffer
6641  * @stats_val: pointer to statistics buffer
6642  * @byteoff: byte offset of counter value in the buffer (8B-aligned)
6643  * @bitoff: bit offset of counter value within 8B entry
6644  * @bitmask: maximum counter value (e.g. 0xffffff for 24-bit counter)
6645  */
6646 static inline u64 irdma_stat_val(const u64 *stats_val, u16 byteoff, u8 bitoff,
6647 				 u64 bitmask)
6648 {
6649 	u16 idx = byteoff / sizeof(*stats_val);
6650 
6651 	return (stats_val[idx] >> bitoff) & bitmask;
6652 }
6653 
6654 /**
6655  * irdma_stat_delta - Calculate counter delta
6656  * @new_val: updated counter value
6657  * @old_val: last counter value
6658  * @max_val: maximum counter value (e.g. 0xffffff for 24-bit counter)
6659  */
6660 static inline u64 irdma_stat_delta(u64 new_val, u64 old_val, u64 max_val)
6661 {
6662 	if (new_val >= old_val)
6663 		return new_val - old_val;
6664 
6665 	/* roll-over case */
6666 	return max_val - old_val + new_val + 1;
6667 }
6668 
6669 /**
6670  * irdma_update_stats - Update statistics
6671  * @hw_stats: hw_stats instance to update
6672  * @gather_stats: updated stat counters
6673  * @last_gather_stats: last stat counters
6674  * @map: HW stat map (hw_stats => gather_stats)
6675  * @max_stat_idx: number of HW stats
6676  */
6677 void irdma_update_stats(struct irdma_dev_hw_stats *hw_stats,
6678 			struct irdma_gather_stats *gather_stats,
6679 			struct irdma_gather_stats *last_gather_stats,
6680 			const struct irdma_hw_stat_map *map, u16 max_stat_idx)
6681 {
6682 	u64 *stats_val = hw_stats->stats_val;
6683 	u16 i;
6684 
6685 	for (i = 0; i < max_stat_idx; i++) {
6686 		u64 new_val = irdma_stat_val(gather_stats->val, map[i].byteoff,
6687 					     map[i].bitoff, map[i].bitmask);
6688 		u64 last_val = irdma_stat_val(last_gather_stats->val,
6689 					      map[i].byteoff, map[i].bitoff,
6690 					      map[i].bitmask);
6691 
6692 		stats_val[i] +=
6693 			irdma_stat_delta(new_val, last_val, map[i].bitmask);
6694 	}
6695 
6696 	memcpy(last_gather_stats, gather_stats, sizeof(*last_gather_stats));
6697 }
6698