xref: /openbmc/linux/drivers/infiniband/hw/irdma/hw.c (revision bacf743e)
1 // SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
2 /* Copyright (c) 2015 - 2021 Intel Corporation */
3 #include "main.h"
4 
5 static struct irdma_rsrc_limits rsrc_limits_table[] = {
6 	[0] = {
7 		.qplimit = SZ_128,
8 	},
9 	[1] = {
10 		.qplimit = SZ_1K,
11 	},
12 	[2] = {
13 		.qplimit = SZ_2K,
14 	},
15 	[3] = {
16 		.qplimit = SZ_4K,
17 	},
18 	[4] = {
19 		.qplimit = SZ_16K,
20 	},
21 	[5] = {
22 		.qplimit = SZ_64K,
23 	},
24 	[6] = {
25 		.qplimit = SZ_128K,
26 	},
27 	[7] = {
28 		.qplimit = SZ_256K,
29 	},
30 };
31 
32 /* types of hmc objects */
33 static enum irdma_hmc_rsrc_type iw_hmc_obj_types[] = {
34 	IRDMA_HMC_IW_QP,
35 	IRDMA_HMC_IW_CQ,
36 	IRDMA_HMC_IW_HTE,
37 	IRDMA_HMC_IW_ARP,
38 	IRDMA_HMC_IW_APBVT_ENTRY,
39 	IRDMA_HMC_IW_MR,
40 	IRDMA_HMC_IW_XF,
41 	IRDMA_HMC_IW_XFFL,
42 	IRDMA_HMC_IW_Q1,
43 	IRDMA_HMC_IW_Q1FL,
44 	IRDMA_HMC_IW_TIMER,
45 	IRDMA_HMC_IW_FSIMC,
46 	IRDMA_HMC_IW_FSIAV,
47 	IRDMA_HMC_IW_RRF,
48 	IRDMA_HMC_IW_RRFFL,
49 	IRDMA_HMC_IW_HDR,
50 	IRDMA_HMC_IW_MD,
51 	IRDMA_HMC_IW_OOISC,
52 	IRDMA_HMC_IW_OOISCFFL,
53 };
54 
55 /**
56  * irdma_iwarp_ce_handler - handle iwarp completions
57  * @iwcq: iwarp cq receiving event
58  */
59 static void irdma_iwarp_ce_handler(struct irdma_sc_cq *iwcq)
60 {
61 	struct irdma_cq *cq = iwcq->back_cq;
62 
63 	if (!cq->user_mode)
64 		cq->armed = false;
65 	if (cq->ibcq.comp_handler)
66 		cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
67 }
68 
69 /**
70  * irdma_puda_ce_handler - handle puda completion events
71  * @rf: RDMA PCI function
72  * @cq: puda completion q for event
73  */
74 static void irdma_puda_ce_handler(struct irdma_pci_f *rf,
75 				  struct irdma_sc_cq *cq)
76 {
77 	struct irdma_sc_dev *dev = &rf->sc_dev;
78 	u32 compl_error;
79 	int status;
80 
81 	do {
82 		status = irdma_puda_poll_cmpl(dev, cq, &compl_error);
83 		if (status == -ENOENT)
84 			break;
85 		if (status) {
86 			ibdev_dbg(to_ibdev(dev), "ERR: puda status = %d\n", status);
87 			break;
88 		}
89 		if (compl_error) {
90 			ibdev_dbg(to_ibdev(dev), "ERR: puda compl_err  =0x%x\n",
91 				  compl_error);
92 			break;
93 		}
94 	} while (1);
95 
96 	irdma_sc_ccq_arm(cq);
97 }
98 
99 /**
100  * irdma_process_ceq - handle ceq for completions
101  * @rf: RDMA PCI function
102  * @ceq: ceq having cq for completion
103  */
104 static void irdma_process_ceq(struct irdma_pci_f *rf, struct irdma_ceq *ceq)
105 {
106 	struct irdma_sc_dev *dev = &rf->sc_dev;
107 	struct irdma_sc_ceq *sc_ceq;
108 	struct irdma_sc_cq *cq;
109 	unsigned long flags;
110 
111 	sc_ceq = &ceq->sc_ceq;
112 	do {
113 		spin_lock_irqsave(&ceq->ce_lock, flags);
114 		cq = irdma_sc_process_ceq(dev, sc_ceq);
115 		if (!cq) {
116 			spin_unlock_irqrestore(&ceq->ce_lock, flags);
117 			break;
118 		}
119 
120 		if (cq->cq_type == IRDMA_CQ_TYPE_IWARP)
121 			irdma_iwarp_ce_handler(cq);
122 
123 		spin_unlock_irqrestore(&ceq->ce_lock, flags);
124 
125 		if (cq->cq_type == IRDMA_CQ_TYPE_CQP)
126 			queue_work(rf->cqp_cmpl_wq, &rf->cqp_cmpl_work);
127 		else if (cq->cq_type == IRDMA_CQ_TYPE_ILQ ||
128 			 cq->cq_type == IRDMA_CQ_TYPE_IEQ)
129 			irdma_puda_ce_handler(rf, cq);
130 	} while (1);
131 }
132 
133 static void irdma_set_flush_fields(struct irdma_sc_qp *qp,
134 				   struct irdma_aeqe_info *info)
135 {
136 	qp->sq_flush_code = info->sq;
137 	qp->rq_flush_code = info->rq;
138 	qp->event_type = IRDMA_QP_EVENT_CATASTROPHIC;
139 
140 	switch (info->ae_id) {
141 	case IRDMA_AE_AMP_UNALLOCATED_STAG:
142 	case IRDMA_AE_AMP_BOUNDS_VIOLATION:
143 	case IRDMA_AE_AMP_INVALID_STAG:
144 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
145 		fallthrough;
146 	case IRDMA_AE_AMP_BAD_PD:
147 	case IRDMA_AE_UDA_XMIT_BAD_PD:
148 		qp->flush_code = FLUSH_PROT_ERR;
149 		break;
150 	case IRDMA_AE_AMP_BAD_QP:
151 	case IRDMA_AE_WQE_UNEXPECTED_OPCODE:
152 		qp->flush_code = FLUSH_LOC_QP_OP_ERR;
153 		break;
154 	case IRDMA_AE_AMP_BAD_STAG_KEY:
155 	case IRDMA_AE_AMP_BAD_STAG_INDEX:
156 	case IRDMA_AE_AMP_TO_WRAP:
157 	case IRDMA_AE_AMP_RIGHTS_VIOLATION:
158 	case IRDMA_AE_AMP_INVALIDATE_NO_REMOTE_ACCESS_RIGHTS:
159 	case IRDMA_AE_PRIV_OPERATION_DENIED:
160 	case IRDMA_AE_IB_INVALID_REQUEST:
161 	case IRDMA_AE_IB_REMOTE_ACCESS_ERROR:
162 		qp->flush_code = FLUSH_REM_ACCESS_ERR;
163 		qp->event_type = IRDMA_QP_EVENT_ACCESS_ERR;
164 		break;
165 	case IRDMA_AE_LLP_SEGMENT_TOO_SMALL:
166 	case IRDMA_AE_DDP_UBE_DDP_MESSAGE_TOO_LONG_FOR_AVAILABLE_BUFFER:
167 	case IRDMA_AE_UDA_XMIT_DGRAM_TOO_LONG:
168 	case IRDMA_AE_UDA_XMIT_DGRAM_TOO_SHORT:
169 	case IRDMA_AE_UDA_L4LEN_INVALID:
170 	case IRDMA_AE_ROCE_RSP_LENGTH_ERROR:
171 		qp->flush_code = FLUSH_LOC_LEN_ERR;
172 		break;
173 	case IRDMA_AE_LCE_QP_CATASTROPHIC:
174 		qp->flush_code = FLUSH_FATAL_ERR;
175 		break;
176 	case IRDMA_AE_DDP_UBE_INVALID_MO:
177 	case IRDMA_AE_IB_RREQ_AND_Q1_FULL:
178 	case IRDMA_AE_LLP_RECEIVED_MPA_CRC_ERROR:
179 		qp->flush_code = FLUSH_GENERAL_ERR;
180 		break;
181 	case IRDMA_AE_LLP_TOO_MANY_RETRIES:
182 		qp->flush_code = FLUSH_RETRY_EXC_ERR;
183 		break;
184 	case IRDMA_AE_AMP_MWBIND_INVALID_RIGHTS:
185 	case IRDMA_AE_AMP_MWBIND_BIND_DISABLED:
186 	case IRDMA_AE_AMP_MWBIND_INVALID_BOUNDS:
187 		qp->flush_code = FLUSH_MW_BIND_ERR;
188 		break;
189 	case IRDMA_AE_IB_REMOTE_OP_ERROR:
190 		qp->flush_code = FLUSH_REM_OP_ERR;
191 		break;
192 	default:
193 		qp->flush_code = FLUSH_FATAL_ERR;
194 		break;
195 	}
196 }
197 
198 /**
199  * irdma_process_aeq - handle aeq events
200  * @rf: RDMA PCI function
201  */
202 static void irdma_process_aeq(struct irdma_pci_f *rf)
203 {
204 	struct irdma_sc_dev *dev = &rf->sc_dev;
205 	struct irdma_aeq *aeq = &rf->aeq;
206 	struct irdma_sc_aeq *sc_aeq = &aeq->sc_aeq;
207 	struct irdma_aeqe_info aeinfo;
208 	struct irdma_aeqe_info *info = &aeinfo;
209 	int ret;
210 	struct irdma_qp *iwqp = NULL;
211 	struct irdma_sc_cq *cq = NULL;
212 	struct irdma_cq *iwcq = NULL;
213 	struct irdma_sc_qp *qp = NULL;
214 	struct irdma_qp_host_ctx_info *ctx_info = NULL;
215 	struct irdma_device *iwdev = rf->iwdev;
216 	unsigned long flags;
217 
218 	u32 aeqcnt = 0;
219 
220 	if (!sc_aeq->size)
221 		return;
222 
223 	do {
224 		memset(info, 0, sizeof(*info));
225 		ret = irdma_sc_get_next_aeqe(sc_aeq, info);
226 		if (ret)
227 			break;
228 
229 		aeqcnt++;
230 		ibdev_dbg(&iwdev->ibdev,
231 			  "AEQ: ae_id = 0x%x bool qp=%d qp_id = %d tcp_state=%d iwarp_state=%d ae_src=%d\n",
232 			  info->ae_id, info->qp, info->qp_cq_id, info->tcp_state,
233 			  info->iwarp_state, info->ae_src);
234 
235 		if (info->qp) {
236 			spin_lock_irqsave(&rf->qptable_lock, flags);
237 			iwqp = rf->qp_table[info->qp_cq_id];
238 			if (!iwqp) {
239 				spin_unlock_irqrestore(&rf->qptable_lock,
240 						       flags);
241 				if (info->ae_id == IRDMA_AE_QP_SUSPEND_COMPLETE) {
242 					atomic_dec(&iwdev->vsi.qp_suspend_reqs);
243 					wake_up(&iwdev->suspend_wq);
244 					continue;
245 				}
246 				ibdev_dbg(&iwdev->ibdev, "AEQ: qp_id %d is already freed\n",
247 					  info->qp_cq_id);
248 				continue;
249 			}
250 			irdma_qp_add_ref(&iwqp->ibqp);
251 			spin_unlock_irqrestore(&rf->qptable_lock, flags);
252 			qp = &iwqp->sc_qp;
253 			spin_lock_irqsave(&iwqp->lock, flags);
254 			iwqp->hw_tcp_state = info->tcp_state;
255 			iwqp->hw_iwarp_state = info->iwarp_state;
256 			if (info->ae_id != IRDMA_AE_QP_SUSPEND_COMPLETE)
257 				iwqp->last_aeq = info->ae_id;
258 			spin_unlock_irqrestore(&iwqp->lock, flags);
259 			ctx_info = &iwqp->ctx_info;
260 			if (rdma_protocol_roce(&iwqp->iwdev->ibdev, 1))
261 				ctx_info->roce_info->err_rq_idx_valid = true;
262 			else
263 				ctx_info->iwarp_info->err_rq_idx_valid = true;
264 		} else {
265 			if (info->ae_id != IRDMA_AE_CQ_OPERATION_ERROR)
266 				continue;
267 		}
268 
269 		switch (info->ae_id) {
270 			struct irdma_cm_node *cm_node;
271 		case IRDMA_AE_LLP_CONNECTION_ESTABLISHED:
272 			cm_node = iwqp->cm_node;
273 			if (cm_node->accept_pend) {
274 				atomic_dec(&cm_node->listener->pend_accepts_cnt);
275 				cm_node->accept_pend = 0;
276 			}
277 			iwqp->rts_ae_rcvd = 1;
278 			wake_up_interruptible(&iwqp->waitq);
279 			break;
280 		case IRDMA_AE_LLP_FIN_RECEIVED:
281 		case IRDMA_AE_RDMAP_ROE_BAD_LLP_CLOSE:
282 			if (qp->term_flags)
283 				break;
284 			if (atomic_inc_return(&iwqp->close_timer_started) == 1) {
285 				iwqp->hw_tcp_state = IRDMA_TCP_STATE_CLOSE_WAIT;
286 				if (iwqp->hw_tcp_state == IRDMA_TCP_STATE_CLOSE_WAIT &&
287 				    iwqp->ibqp_state == IB_QPS_RTS) {
288 					irdma_next_iw_state(iwqp,
289 							    IRDMA_QP_STATE_CLOSING,
290 							    0, 0, 0);
291 					irdma_cm_disconn(iwqp);
292 				}
293 				irdma_schedule_cm_timer(iwqp->cm_node,
294 							(struct irdma_puda_buf *)iwqp,
295 							IRDMA_TIMER_TYPE_CLOSE,
296 							1, 0);
297 			}
298 			break;
299 		case IRDMA_AE_LLP_CLOSE_COMPLETE:
300 			if (qp->term_flags)
301 				irdma_terminate_done(qp, 0);
302 			else
303 				irdma_cm_disconn(iwqp);
304 			break;
305 		case IRDMA_AE_BAD_CLOSE:
306 		case IRDMA_AE_RESET_SENT:
307 			irdma_next_iw_state(iwqp, IRDMA_QP_STATE_ERROR, 1, 0,
308 					    0);
309 			irdma_cm_disconn(iwqp);
310 			break;
311 		case IRDMA_AE_LLP_CONNECTION_RESET:
312 			if (atomic_read(&iwqp->close_timer_started))
313 				break;
314 			irdma_cm_disconn(iwqp);
315 			break;
316 		case IRDMA_AE_QP_SUSPEND_COMPLETE:
317 			if (iwqp->iwdev->vsi.tc_change_pending) {
318 				atomic_dec(&iwqp->sc_qp.vsi->qp_suspend_reqs);
319 				wake_up(&iwqp->iwdev->suspend_wq);
320 			}
321 			break;
322 		case IRDMA_AE_TERMINATE_SENT:
323 			irdma_terminate_send_fin(qp);
324 			break;
325 		case IRDMA_AE_LLP_TERMINATE_RECEIVED:
326 			irdma_terminate_received(qp, info);
327 			break;
328 		case IRDMA_AE_CQ_OPERATION_ERROR:
329 			ibdev_err(&iwdev->ibdev,
330 				  "Processing an iWARP related AE for CQ misc = 0x%04X\n",
331 				  info->ae_id);
332 			cq = (struct irdma_sc_cq *)(unsigned long)
333 			     info->compl_ctx;
334 
335 			iwcq = cq->back_cq;
336 
337 			if (iwcq->ibcq.event_handler) {
338 				struct ib_event ibevent;
339 
340 				ibevent.device = iwcq->ibcq.device;
341 				ibevent.event = IB_EVENT_CQ_ERR;
342 				ibevent.element.cq = &iwcq->ibcq;
343 				iwcq->ibcq.event_handler(&ibevent,
344 							 iwcq->ibcq.cq_context);
345 			}
346 			break;
347 		case IRDMA_AE_RESET_NOT_SENT:
348 		case IRDMA_AE_LLP_DOUBT_REACHABILITY:
349 		case IRDMA_AE_RESOURCE_EXHAUSTION:
350 			break;
351 		case IRDMA_AE_PRIV_OPERATION_DENIED:
352 		case IRDMA_AE_STAG_ZERO_INVALID:
353 		case IRDMA_AE_IB_RREQ_AND_Q1_FULL:
354 		case IRDMA_AE_DDP_UBE_INVALID_DDP_VERSION:
355 		case IRDMA_AE_DDP_UBE_INVALID_MO:
356 		case IRDMA_AE_DDP_UBE_INVALID_QN:
357 		case IRDMA_AE_DDP_NO_L_BIT:
358 		case IRDMA_AE_RDMAP_ROE_INVALID_RDMAP_VERSION:
359 		case IRDMA_AE_RDMAP_ROE_UNEXPECTED_OPCODE:
360 		case IRDMA_AE_ROE_INVALID_RDMA_READ_REQUEST:
361 		case IRDMA_AE_ROE_INVALID_RDMA_WRITE_OR_READ_RESP:
362 		case IRDMA_AE_INVALID_ARP_ENTRY:
363 		case IRDMA_AE_INVALID_TCP_OPTION_RCVD:
364 		case IRDMA_AE_STALE_ARP_ENTRY:
365 		case IRDMA_AE_LLP_RECEIVED_MPA_CRC_ERROR:
366 		case IRDMA_AE_LLP_SEGMENT_TOO_SMALL:
367 		case IRDMA_AE_LLP_SYN_RECEIVED:
368 		case IRDMA_AE_LLP_TOO_MANY_RETRIES:
369 		case IRDMA_AE_LCE_QP_CATASTROPHIC:
370 		case IRDMA_AE_LCE_FUNCTION_CATASTROPHIC:
371 		case IRDMA_AE_LCE_CQ_CATASTROPHIC:
372 		case IRDMA_AE_UDA_XMIT_DGRAM_TOO_LONG:
373 			if (rdma_protocol_roce(&iwdev->ibdev, 1))
374 				ctx_info->roce_info->err_rq_idx_valid = false;
375 			else
376 				ctx_info->iwarp_info->err_rq_idx_valid = false;
377 			fallthrough;
378 		default:
379 			ibdev_err(&iwdev->ibdev, "abnormal ae_id = 0x%x bool qp=%d qp_id = %d\n",
380 				  info->ae_id, info->qp, info->qp_cq_id);
381 			if (rdma_protocol_roce(&iwdev->ibdev, 1)) {
382 				if (!info->sq && ctx_info->roce_info->err_rq_idx_valid) {
383 					ctx_info->roce_info->err_rq_idx = info->wqe_idx;
384 					irdma_sc_qp_setctx_roce(&iwqp->sc_qp, iwqp->host_ctx.va,
385 								ctx_info);
386 				}
387 				irdma_set_flush_fields(qp, info);
388 				irdma_cm_disconn(iwqp);
389 				break;
390 			}
391 			if (!info->sq && ctx_info->iwarp_info->err_rq_idx_valid) {
392 				ctx_info->iwarp_info->err_rq_idx = info->wqe_idx;
393 				ctx_info->tcp_info_valid = false;
394 				ctx_info->iwarp_info_valid = true;
395 				irdma_sc_qp_setctx(&iwqp->sc_qp, iwqp->host_ctx.va,
396 						   ctx_info);
397 			}
398 			if (iwqp->hw_iwarp_state != IRDMA_QP_STATE_RTS &&
399 			    iwqp->hw_iwarp_state != IRDMA_QP_STATE_TERMINATE) {
400 				irdma_next_iw_state(iwqp, IRDMA_QP_STATE_ERROR, 1, 0, 0);
401 				irdma_cm_disconn(iwqp);
402 			} else {
403 				irdma_terminate_connection(qp, info);
404 			}
405 			break;
406 		}
407 		if (info->qp)
408 			irdma_qp_rem_ref(&iwqp->ibqp);
409 	} while (1);
410 
411 	if (aeqcnt)
412 		irdma_sc_repost_aeq_entries(dev, aeqcnt);
413 }
414 
415 /**
416  * irdma_ena_intr - set up device interrupts
417  * @dev: hardware control device structure
418  * @msix_id: id of the interrupt to be enabled
419  */
420 static void irdma_ena_intr(struct irdma_sc_dev *dev, u32 msix_id)
421 {
422 	dev->irq_ops->irdma_en_irq(dev, msix_id);
423 }
424 
425 /**
426  * irdma_dpc - tasklet for aeq and ceq 0
427  * @t: tasklet_struct ptr
428  */
429 static void irdma_dpc(struct tasklet_struct *t)
430 {
431 	struct irdma_pci_f *rf = from_tasklet(rf, t, dpc_tasklet);
432 
433 	if (rf->msix_shared)
434 		irdma_process_ceq(rf, rf->ceqlist);
435 	irdma_process_aeq(rf);
436 	irdma_ena_intr(&rf->sc_dev, rf->iw_msixtbl[0].idx);
437 }
438 
439 /**
440  * irdma_ceq_dpc - dpc handler for CEQ
441  * @t: tasklet_struct ptr
442  */
443 static void irdma_ceq_dpc(struct tasklet_struct *t)
444 {
445 	struct irdma_ceq *iwceq = from_tasklet(iwceq, t, dpc_tasklet);
446 	struct irdma_pci_f *rf = iwceq->rf;
447 
448 	irdma_process_ceq(rf, iwceq);
449 	irdma_ena_intr(&rf->sc_dev, iwceq->msix_idx);
450 }
451 
452 /**
453  * irdma_save_msix_info - copy msix vector information to iwarp device
454  * @rf: RDMA PCI function
455  *
456  * Allocate iwdev msix table and copy the msix info to the table
457  * Return 0 if successful, otherwise return error
458  */
459 static int irdma_save_msix_info(struct irdma_pci_f *rf)
460 {
461 	struct irdma_qvlist_info *iw_qvlist;
462 	struct irdma_qv_info *iw_qvinfo;
463 	struct msix_entry *pmsix;
464 	u32 ceq_idx;
465 	u32 i;
466 	size_t size;
467 
468 	if (!rf->msix_count)
469 		return -EINVAL;
470 
471 	size = sizeof(struct irdma_msix_vector) * rf->msix_count;
472 	size += struct_size(iw_qvlist, qv_info, rf->msix_count);
473 	rf->iw_msixtbl = kzalloc(size, GFP_KERNEL);
474 	if (!rf->iw_msixtbl)
475 		return -ENOMEM;
476 
477 	rf->iw_qvlist = (struct irdma_qvlist_info *)
478 			(&rf->iw_msixtbl[rf->msix_count]);
479 	iw_qvlist = rf->iw_qvlist;
480 	iw_qvinfo = iw_qvlist->qv_info;
481 	iw_qvlist->num_vectors = rf->msix_count;
482 	if (rf->msix_count <= num_online_cpus())
483 		rf->msix_shared = true;
484 
485 	pmsix = rf->msix_entries;
486 	for (i = 0, ceq_idx = 0; i < rf->msix_count; i++, iw_qvinfo++) {
487 		rf->iw_msixtbl[i].idx = pmsix->entry;
488 		rf->iw_msixtbl[i].irq = pmsix->vector;
489 		rf->iw_msixtbl[i].cpu_affinity = ceq_idx;
490 		if (!i) {
491 			iw_qvinfo->aeq_idx = 0;
492 			if (rf->msix_shared)
493 				iw_qvinfo->ceq_idx = ceq_idx++;
494 			else
495 				iw_qvinfo->ceq_idx = IRDMA_Q_INVALID_IDX;
496 		} else {
497 			iw_qvinfo->aeq_idx = IRDMA_Q_INVALID_IDX;
498 			iw_qvinfo->ceq_idx = ceq_idx++;
499 		}
500 		iw_qvinfo->itr_idx = 3;
501 		iw_qvinfo->v_idx = rf->iw_msixtbl[i].idx;
502 		pmsix++;
503 	}
504 
505 	return 0;
506 }
507 
508 /**
509  * irdma_irq_handler - interrupt handler for aeq and ceq0
510  * @irq: Interrupt request number
511  * @data: RDMA PCI function
512  */
513 static irqreturn_t irdma_irq_handler(int irq, void *data)
514 {
515 	struct irdma_pci_f *rf = data;
516 
517 	tasklet_schedule(&rf->dpc_tasklet);
518 
519 	return IRQ_HANDLED;
520 }
521 
522 /**
523  * irdma_ceq_handler - interrupt handler for ceq
524  * @irq: interrupt request number
525  * @data: ceq pointer
526  */
527 static irqreturn_t irdma_ceq_handler(int irq, void *data)
528 {
529 	struct irdma_ceq *iwceq = data;
530 
531 	if (iwceq->irq != irq)
532 		ibdev_err(to_ibdev(&iwceq->rf->sc_dev), "expected irq = %d received irq = %d\n",
533 			  iwceq->irq, irq);
534 	tasklet_schedule(&iwceq->dpc_tasklet);
535 
536 	return IRQ_HANDLED;
537 }
538 
539 /**
540  * irdma_destroy_irq - destroy device interrupts
541  * @rf: RDMA PCI function
542  * @msix_vec: msix vector to disable irq
543  * @dev_id: parameter to pass to free_irq (used during irq setup)
544  *
545  * The function is called when destroying aeq/ceq
546  */
547 static void irdma_destroy_irq(struct irdma_pci_f *rf,
548 			      struct irdma_msix_vector *msix_vec, void *dev_id)
549 {
550 	struct irdma_sc_dev *dev = &rf->sc_dev;
551 
552 	dev->irq_ops->irdma_dis_irq(dev, msix_vec->idx);
553 	irq_update_affinity_hint(msix_vec->irq, NULL);
554 	free_irq(msix_vec->irq, dev_id);
555 }
556 
557 /**
558  * irdma_destroy_cqp  - destroy control qp
559  * @rf: RDMA PCI function
560  * @free_hwcqp: 1 if hw cqp should be freed
561  *
562  * Issue destroy cqp request and
563  * free the resources associated with the cqp
564  */
565 static void irdma_destroy_cqp(struct irdma_pci_f *rf, bool free_hwcqp)
566 {
567 	struct irdma_sc_dev *dev = &rf->sc_dev;
568 	struct irdma_cqp *cqp = &rf->cqp;
569 	int status = 0;
570 
571 	if (rf->cqp_cmpl_wq)
572 		destroy_workqueue(rf->cqp_cmpl_wq);
573 	if (free_hwcqp)
574 		status = irdma_sc_cqp_destroy(dev->cqp);
575 	if (status)
576 		ibdev_dbg(to_ibdev(dev), "ERR: Destroy CQP failed %d\n", status);
577 
578 	irdma_cleanup_pending_cqp_op(rf);
579 	dma_free_coherent(dev->hw->device, cqp->sq.size, cqp->sq.va,
580 			  cqp->sq.pa);
581 	cqp->sq.va = NULL;
582 	kfree(cqp->scratch_array);
583 	cqp->scratch_array = NULL;
584 	kfree(cqp->cqp_requests);
585 	cqp->cqp_requests = NULL;
586 }
587 
588 static void irdma_destroy_virt_aeq(struct irdma_pci_f *rf)
589 {
590 	struct irdma_aeq *aeq = &rf->aeq;
591 	u32 pg_cnt = DIV_ROUND_UP(aeq->mem.size, PAGE_SIZE);
592 	dma_addr_t *pg_arr = (dma_addr_t *)aeq->palloc.level1.addr;
593 
594 	irdma_unmap_vm_page_list(&rf->hw, pg_arr, pg_cnt);
595 	irdma_free_pble(rf->pble_rsrc, &aeq->palloc);
596 	vfree(aeq->mem.va);
597 }
598 
599 /**
600  * irdma_destroy_aeq - destroy aeq
601  * @rf: RDMA PCI function
602  *
603  * Issue a destroy aeq request and
604  * free the resources associated with the aeq
605  * The function is called during driver unload
606  */
607 static void irdma_destroy_aeq(struct irdma_pci_f *rf)
608 {
609 	struct irdma_sc_dev *dev = &rf->sc_dev;
610 	struct irdma_aeq *aeq = &rf->aeq;
611 	int status = -EBUSY;
612 
613 	if (!rf->msix_shared) {
614 		rf->sc_dev.irq_ops->irdma_cfg_aeq(&rf->sc_dev, rf->iw_msixtbl->idx, false);
615 		irdma_destroy_irq(rf, rf->iw_msixtbl, rf);
616 	}
617 	if (rf->reset)
618 		goto exit;
619 
620 	aeq->sc_aeq.size = 0;
621 	status = irdma_cqp_aeq_cmd(dev, &aeq->sc_aeq, IRDMA_OP_AEQ_DESTROY);
622 	if (status)
623 		ibdev_dbg(to_ibdev(dev), "ERR: Destroy AEQ failed %d\n", status);
624 
625 exit:
626 	if (aeq->virtual_map) {
627 		irdma_destroy_virt_aeq(rf);
628 	} else {
629 		dma_free_coherent(dev->hw->device, aeq->mem.size, aeq->mem.va,
630 				  aeq->mem.pa);
631 		aeq->mem.va = NULL;
632 	}
633 }
634 
635 /**
636  * irdma_destroy_ceq - destroy ceq
637  * @rf: RDMA PCI function
638  * @iwceq: ceq to be destroyed
639  *
640  * Issue a destroy ceq request and
641  * free the resources associated with the ceq
642  */
643 static void irdma_destroy_ceq(struct irdma_pci_f *rf, struct irdma_ceq *iwceq)
644 {
645 	struct irdma_sc_dev *dev = &rf->sc_dev;
646 	int status;
647 
648 	if (rf->reset)
649 		goto exit;
650 
651 	status = irdma_sc_ceq_destroy(&iwceq->sc_ceq, 0, 1);
652 	if (status) {
653 		ibdev_dbg(to_ibdev(dev), "ERR: CEQ destroy command failed %d\n", status);
654 		goto exit;
655 	}
656 
657 	status = irdma_sc_cceq_destroy_done(&iwceq->sc_ceq);
658 	if (status)
659 		ibdev_dbg(to_ibdev(dev), "ERR: CEQ destroy completion failed %d\n",
660 			  status);
661 exit:
662 	dma_free_coherent(dev->hw->device, iwceq->mem.size, iwceq->mem.va,
663 			  iwceq->mem.pa);
664 	iwceq->mem.va = NULL;
665 }
666 
667 /**
668  * irdma_del_ceq_0 - destroy ceq 0
669  * @rf: RDMA PCI function
670  *
671  * Disable the ceq 0 interrupt and destroy the ceq 0
672  */
673 static void irdma_del_ceq_0(struct irdma_pci_f *rf)
674 {
675 	struct irdma_ceq *iwceq = rf->ceqlist;
676 	struct irdma_msix_vector *msix_vec;
677 
678 	if (rf->msix_shared) {
679 		msix_vec = &rf->iw_msixtbl[0];
680 		rf->sc_dev.irq_ops->irdma_cfg_ceq(&rf->sc_dev,
681 						  msix_vec->ceq_id,
682 						  msix_vec->idx, false);
683 		irdma_destroy_irq(rf, msix_vec, rf);
684 	} else {
685 		msix_vec = &rf->iw_msixtbl[1];
686 		irdma_destroy_irq(rf, msix_vec, iwceq);
687 	}
688 
689 	irdma_destroy_ceq(rf, iwceq);
690 	rf->sc_dev.ceq_valid = false;
691 	rf->ceqs_count = 0;
692 }
693 
694 /**
695  * irdma_del_ceqs - destroy all ceq's except CEQ 0
696  * @rf: RDMA PCI function
697  *
698  * Go through all of the device ceq's, except 0, and for each
699  * ceq disable the ceq interrupt and destroy the ceq
700  */
701 static void irdma_del_ceqs(struct irdma_pci_f *rf)
702 {
703 	struct irdma_ceq *iwceq = &rf->ceqlist[1];
704 	struct irdma_msix_vector *msix_vec;
705 	u32 i = 0;
706 
707 	if (rf->msix_shared)
708 		msix_vec = &rf->iw_msixtbl[1];
709 	else
710 		msix_vec = &rf->iw_msixtbl[2];
711 
712 	for (i = 1; i < rf->ceqs_count; i++, msix_vec++, iwceq++) {
713 		rf->sc_dev.irq_ops->irdma_cfg_ceq(&rf->sc_dev, msix_vec->ceq_id,
714 						  msix_vec->idx, false);
715 		irdma_destroy_irq(rf, msix_vec, iwceq);
716 		irdma_cqp_ceq_cmd(&rf->sc_dev, &iwceq->sc_ceq,
717 				  IRDMA_OP_CEQ_DESTROY);
718 		dma_free_coherent(rf->sc_dev.hw->device, iwceq->mem.size,
719 				  iwceq->mem.va, iwceq->mem.pa);
720 		iwceq->mem.va = NULL;
721 	}
722 	rf->ceqs_count = 1;
723 }
724 
725 /**
726  * irdma_destroy_ccq - destroy control cq
727  * @rf: RDMA PCI function
728  *
729  * Issue destroy ccq request and
730  * free the resources associated with the ccq
731  */
732 static void irdma_destroy_ccq(struct irdma_pci_f *rf)
733 {
734 	struct irdma_sc_dev *dev = &rf->sc_dev;
735 	struct irdma_ccq *ccq = &rf->ccq;
736 	int status = 0;
737 
738 	if (!rf->reset)
739 		status = irdma_sc_ccq_destroy(dev->ccq, 0, true);
740 	if (status)
741 		ibdev_dbg(to_ibdev(dev), "ERR: CCQ destroy failed %d\n", status);
742 	dma_free_coherent(dev->hw->device, ccq->mem_cq.size, ccq->mem_cq.va,
743 			  ccq->mem_cq.pa);
744 	ccq->mem_cq.va = NULL;
745 }
746 
747 /**
748  * irdma_close_hmc_objects_type - delete hmc objects of a given type
749  * @dev: iwarp device
750  * @obj_type: the hmc object type to be deleted
751  * @hmc_info: host memory info struct
752  * @privileged: permission to close HMC objects
753  * @reset: true if called before reset
754  */
755 static void irdma_close_hmc_objects_type(struct irdma_sc_dev *dev,
756 					 enum irdma_hmc_rsrc_type obj_type,
757 					 struct irdma_hmc_info *hmc_info,
758 					 bool privileged, bool reset)
759 {
760 	struct irdma_hmc_del_obj_info info = {};
761 
762 	info.hmc_info = hmc_info;
763 	info.rsrc_type = obj_type;
764 	info.count = hmc_info->hmc_obj[obj_type].cnt;
765 	info.privileged = privileged;
766 	if (irdma_sc_del_hmc_obj(dev, &info, reset))
767 		ibdev_dbg(to_ibdev(dev), "ERR: del HMC obj of type %d failed\n",
768 			  obj_type);
769 }
770 
771 /**
772  * irdma_del_hmc_objects - remove all device hmc objects
773  * @dev: iwarp device
774  * @hmc_info: hmc_info to free
775  * @privileged: permission to delete HMC objects
776  * @reset: true if called before reset
777  * @vers: hardware version
778  */
779 static void irdma_del_hmc_objects(struct irdma_sc_dev *dev,
780 				  struct irdma_hmc_info *hmc_info, bool privileged,
781 				  bool reset, enum irdma_vers vers)
782 {
783 	unsigned int i;
784 
785 	for (i = 0; i < IW_HMC_OBJ_TYPE_NUM; i++) {
786 		if (dev->hmc_info->hmc_obj[iw_hmc_obj_types[i]].cnt)
787 			irdma_close_hmc_objects_type(dev, iw_hmc_obj_types[i],
788 						     hmc_info, privileged, reset);
789 		if (vers == IRDMA_GEN_1 && i == IRDMA_HMC_IW_TIMER)
790 			break;
791 	}
792 }
793 
794 /**
795  * irdma_create_hmc_obj_type - create hmc object of a given type
796  * @dev: hardware control device structure
797  * @info: information for the hmc object to create
798  */
799 static int irdma_create_hmc_obj_type(struct irdma_sc_dev *dev,
800 				     struct irdma_hmc_create_obj_info *info)
801 {
802 	return irdma_sc_create_hmc_obj(dev, info);
803 }
804 
805 /**
806  * irdma_create_hmc_objs - create all hmc objects for the device
807  * @rf: RDMA PCI function
808  * @privileged: permission to create HMC objects
809  * @vers: HW version
810  *
811  * Create the device hmc objects and allocate hmc pages
812  * Return 0 if successful, otherwise clean up and return error
813  */
814 static int irdma_create_hmc_objs(struct irdma_pci_f *rf, bool privileged,
815 				 enum irdma_vers vers)
816 {
817 	struct irdma_sc_dev *dev = &rf->sc_dev;
818 	struct irdma_hmc_create_obj_info info = {};
819 	int i, status = 0;
820 
821 	info.hmc_info = dev->hmc_info;
822 	info.privileged = privileged;
823 	info.entry_type = rf->sd_type;
824 
825 	for (i = 0; i < IW_HMC_OBJ_TYPE_NUM; i++) {
826 		if (dev->hmc_info->hmc_obj[iw_hmc_obj_types[i]].cnt) {
827 			info.rsrc_type = iw_hmc_obj_types[i];
828 			info.count = dev->hmc_info->hmc_obj[info.rsrc_type].cnt;
829 			info.add_sd_cnt = 0;
830 			status = irdma_create_hmc_obj_type(dev, &info);
831 			if (status) {
832 				ibdev_dbg(to_ibdev(dev),
833 					  "ERR: create obj type %d status = %d\n",
834 					  iw_hmc_obj_types[i], status);
835 				break;
836 			}
837 		}
838 		if (vers == IRDMA_GEN_1 && i == IRDMA_HMC_IW_TIMER)
839 			break;
840 	}
841 
842 	if (!status)
843 		return irdma_sc_static_hmc_pages_allocated(dev->cqp, 0, dev->hmc_fn_id,
844 							   true, true);
845 
846 	while (i) {
847 		i--;
848 		/* destroy the hmc objects of a given type */
849 		if (dev->hmc_info->hmc_obj[iw_hmc_obj_types[i]].cnt)
850 			irdma_close_hmc_objects_type(dev, iw_hmc_obj_types[i],
851 						     dev->hmc_info, privileged,
852 						     false);
853 	}
854 
855 	return status;
856 }
857 
858 /**
859  * irdma_obj_aligned_mem - get aligned memory from device allocated memory
860  * @rf: RDMA PCI function
861  * @memptr: points to the memory addresses
862  * @size: size of memory needed
863  * @mask: mask for the aligned memory
864  *
865  * Get aligned memory of the requested size and
866  * update the memptr to point to the new aligned memory
867  * Return 0 if successful, otherwise return no memory error
868  */
869 static int irdma_obj_aligned_mem(struct irdma_pci_f *rf,
870 				 struct irdma_dma_mem *memptr, u32 size,
871 				 u32 mask)
872 {
873 	unsigned long va, newva;
874 	unsigned long extra;
875 
876 	va = (unsigned long)rf->obj_next.va;
877 	newva = va;
878 	if (mask)
879 		newva = ALIGN(va, (unsigned long)mask + 1ULL);
880 	extra = newva - va;
881 	memptr->va = (u8 *)va + extra;
882 	memptr->pa = rf->obj_next.pa + extra;
883 	memptr->size = size;
884 	if (((u8 *)memptr->va + size) > ((u8 *)rf->obj_mem.va + rf->obj_mem.size))
885 		return -ENOMEM;
886 
887 	rf->obj_next.va = (u8 *)memptr->va + size;
888 	rf->obj_next.pa = memptr->pa + size;
889 
890 	return 0;
891 }
892 
893 /**
894  * irdma_create_cqp - create control qp
895  * @rf: RDMA PCI function
896  *
897  * Return 0, if the cqp and all the resources associated with it
898  * are successfully created, otherwise return error
899  */
900 static int irdma_create_cqp(struct irdma_pci_f *rf)
901 {
902 	u32 sqsize = IRDMA_CQP_SW_SQSIZE_2048;
903 	struct irdma_dma_mem mem;
904 	struct irdma_sc_dev *dev = &rf->sc_dev;
905 	struct irdma_cqp_init_info cqp_init_info = {};
906 	struct irdma_cqp *cqp = &rf->cqp;
907 	u16 maj_err, min_err;
908 	int i, status;
909 
910 	cqp->cqp_requests = kcalloc(sqsize, sizeof(*cqp->cqp_requests), GFP_KERNEL);
911 	if (!cqp->cqp_requests)
912 		return -ENOMEM;
913 
914 	cqp->scratch_array = kcalloc(sqsize, sizeof(*cqp->scratch_array), GFP_KERNEL);
915 	if (!cqp->scratch_array) {
916 		kfree(cqp->cqp_requests);
917 		return -ENOMEM;
918 	}
919 
920 	dev->cqp = &cqp->sc_cqp;
921 	dev->cqp->dev = dev;
922 	cqp->sq.size = ALIGN(sizeof(struct irdma_cqp_sq_wqe) * sqsize,
923 			     IRDMA_CQP_ALIGNMENT);
924 	cqp->sq.va = dma_alloc_coherent(dev->hw->device, cqp->sq.size,
925 					&cqp->sq.pa, GFP_KERNEL);
926 	if (!cqp->sq.va) {
927 		kfree(cqp->scratch_array);
928 		kfree(cqp->cqp_requests);
929 		return -ENOMEM;
930 	}
931 
932 	status = irdma_obj_aligned_mem(rf, &mem, sizeof(struct irdma_cqp_ctx),
933 				       IRDMA_HOST_CTX_ALIGNMENT_M);
934 	if (status)
935 		goto exit;
936 
937 	dev->cqp->host_ctx_pa = mem.pa;
938 	dev->cqp->host_ctx = mem.va;
939 	/* populate the cqp init info */
940 	cqp_init_info.dev = dev;
941 	cqp_init_info.sq_size = sqsize;
942 	cqp_init_info.sq = cqp->sq.va;
943 	cqp_init_info.sq_pa = cqp->sq.pa;
944 	cqp_init_info.host_ctx_pa = mem.pa;
945 	cqp_init_info.host_ctx = mem.va;
946 	cqp_init_info.hmc_profile = rf->rsrc_profile;
947 	cqp_init_info.scratch_array = cqp->scratch_array;
948 	cqp_init_info.protocol_used = rf->protocol_used;
949 
950 	switch (rf->rdma_ver) {
951 	case IRDMA_GEN_1:
952 		cqp_init_info.hw_maj_ver = IRDMA_CQPHC_HW_MAJVER_GEN_1;
953 		break;
954 	case IRDMA_GEN_2:
955 		cqp_init_info.hw_maj_ver = IRDMA_CQPHC_HW_MAJVER_GEN_2;
956 		break;
957 	}
958 	status = irdma_sc_cqp_init(dev->cqp, &cqp_init_info);
959 	if (status) {
960 		ibdev_dbg(to_ibdev(dev), "ERR: cqp init status %d\n", status);
961 		goto exit;
962 	}
963 
964 	spin_lock_init(&cqp->req_lock);
965 	spin_lock_init(&cqp->compl_lock);
966 
967 	status = irdma_sc_cqp_create(dev->cqp, &maj_err, &min_err);
968 	if (status) {
969 		ibdev_dbg(to_ibdev(dev),
970 			  "ERR: cqp create failed - status %d maj_err %d min_err %d\n",
971 			  status, maj_err, min_err);
972 		goto exit;
973 	}
974 
975 	INIT_LIST_HEAD(&cqp->cqp_avail_reqs);
976 	INIT_LIST_HEAD(&cqp->cqp_pending_reqs);
977 
978 	/* init the waitqueue of the cqp_requests and add them to the list */
979 	for (i = 0; i < sqsize; i++) {
980 		init_waitqueue_head(&cqp->cqp_requests[i].waitq);
981 		list_add_tail(&cqp->cqp_requests[i].list, &cqp->cqp_avail_reqs);
982 	}
983 	init_waitqueue_head(&cqp->remove_wq);
984 	return 0;
985 
986 exit:
987 	irdma_destroy_cqp(rf, false);
988 
989 	return status;
990 }
991 
992 /**
993  * irdma_create_ccq - create control cq
994  * @rf: RDMA PCI function
995  *
996  * Return 0, if the ccq and the resources associated with it
997  * are successfully created, otherwise return error
998  */
999 static int irdma_create_ccq(struct irdma_pci_f *rf)
1000 {
1001 	struct irdma_sc_dev *dev = &rf->sc_dev;
1002 	struct irdma_ccq_init_info info = {};
1003 	struct irdma_ccq *ccq = &rf->ccq;
1004 	int status;
1005 
1006 	dev->ccq = &ccq->sc_cq;
1007 	dev->ccq->dev = dev;
1008 	info.dev = dev;
1009 	ccq->shadow_area.size = sizeof(struct irdma_cq_shadow_area);
1010 	ccq->mem_cq.size = ALIGN(sizeof(struct irdma_cqe) * IW_CCQ_SIZE,
1011 				 IRDMA_CQ0_ALIGNMENT);
1012 	ccq->mem_cq.va = dma_alloc_coherent(dev->hw->device, ccq->mem_cq.size,
1013 					    &ccq->mem_cq.pa, GFP_KERNEL);
1014 	if (!ccq->mem_cq.va)
1015 		return -ENOMEM;
1016 
1017 	status = irdma_obj_aligned_mem(rf, &ccq->shadow_area,
1018 				       ccq->shadow_area.size,
1019 				       IRDMA_SHADOWAREA_M);
1020 	if (status)
1021 		goto exit;
1022 
1023 	ccq->sc_cq.back_cq = ccq;
1024 	/* populate the ccq init info */
1025 	info.cq_base = ccq->mem_cq.va;
1026 	info.cq_pa = ccq->mem_cq.pa;
1027 	info.num_elem = IW_CCQ_SIZE;
1028 	info.shadow_area = ccq->shadow_area.va;
1029 	info.shadow_area_pa = ccq->shadow_area.pa;
1030 	info.ceqe_mask = false;
1031 	info.ceq_id_valid = true;
1032 	info.shadow_read_threshold = 16;
1033 	info.vsi = &rf->default_vsi;
1034 	status = irdma_sc_ccq_init(dev->ccq, &info);
1035 	if (!status)
1036 		status = irdma_sc_ccq_create(dev->ccq, 0, true, true);
1037 exit:
1038 	if (status) {
1039 		dma_free_coherent(dev->hw->device, ccq->mem_cq.size,
1040 				  ccq->mem_cq.va, ccq->mem_cq.pa);
1041 		ccq->mem_cq.va = NULL;
1042 	}
1043 
1044 	return status;
1045 }
1046 
1047 /**
1048  * irdma_alloc_set_mac - set up a mac address table entry
1049  * @iwdev: irdma device
1050  *
1051  * Allocate a mac ip entry and add it to the hw table Return 0
1052  * if successful, otherwise return error
1053  */
1054 static int irdma_alloc_set_mac(struct irdma_device *iwdev)
1055 {
1056 	int status;
1057 
1058 	status = irdma_alloc_local_mac_entry(iwdev->rf,
1059 					     &iwdev->mac_ip_table_idx);
1060 	if (!status) {
1061 		status = irdma_add_local_mac_entry(iwdev->rf,
1062 						   (const u8 *)iwdev->netdev->dev_addr,
1063 						   (u8)iwdev->mac_ip_table_idx);
1064 		if (status)
1065 			irdma_del_local_mac_entry(iwdev->rf,
1066 						  (u8)iwdev->mac_ip_table_idx);
1067 	}
1068 	return status;
1069 }
1070 
1071 /**
1072  * irdma_cfg_ceq_vector - set up the msix interrupt vector for
1073  * ceq
1074  * @rf: RDMA PCI function
1075  * @iwceq: ceq associated with the vector
1076  * @ceq_id: the id number of the iwceq
1077  * @msix_vec: interrupt vector information
1078  *
1079  * Allocate interrupt resources and enable irq handling
1080  * Return 0 if successful, otherwise return error
1081  */
1082 static int irdma_cfg_ceq_vector(struct irdma_pci_f *rf, struct irdma_ceq *iwceq,
1083 				u32 ceq_id, struct irdma_msix_vector *msix_vec)
1084 {
1085 	int status;
1086 
1087 	if (rf->msix_shared && !ceq_id) {
1088 		tasklet_setup(&rf->dpc_tasklet, irdma_dpc);
1089 		status = request_irq(msix_vec->irq, irdma_irq_handler, 0,
1090 				     "AEQCEQ", rf);
1091 	} else {
1092 		tasklet_setup(&iwceq->dpc_tasklet, irdma_ceq_dpc);
1093 
1094 		status = request_irq(msix_vec->irq, irdma_ceq_handler, 0,
1095 				     "CEQ", iwceq);
1096 	}
1097 	cpumask_clear(&msix_vec->mask);
1098 	cpumask_set_cpu(msix_vec->cpu_affinity, &msix_vec->mask);
1099 	irq_update_affinity_hint(msix_vec->irq, &msix_vec->mask);
1100 	if (status) {
1101 		ibdev_dbg(&rf->iwdev->ibdev, "ERR: ceq irq config fail\n");
1102 		return status;
1103 	}
1104 
1105 	msix_vec->ceq_id = ceq_id;
1106 	rf->sc_dev.irq_ops->irdma_cfg_ceq(&rf->sc_dev, ceq_id, msix_vec->idx, true);
1107 
1108 	return 0;
1109 }
1110 
1111 /**
1112  * irdma_cfg_aeq_vector - set up the msix vector for aeq
1113  * @rf: RDMA PCI function
1114  *
1115  * Allocate interrupt resources and enable irq handling
1116  * Return 0 if successful, otherwise return error
1117  */
1118 static int irdma_cfg_aeq_vector(struct irdma_pci_f *rf)
1119 {
1120 	struct irdma_msix_vector *msix_vec = rf->iw_msixtbl;
1121 	u32 ret = 0;
1122 
1123 	if (!rf->msix_shared) {
1124 		tasklet_setup(&rf->dpc_tasklet, irdma_dpc);
1125 		ret = request_irq(msix_vec->irq, irdma_irq_handler, 0,
1126 				  "irdma", rf);
1127 	}
1128 	if (ret) {
1129 		ibdev_dbg(&rf->iwdev->ibdev, "ERR: aeq irq config fail\n");
1130 		return -EINVAL;
1131 	}
1132 
1133 	rf->sc_dev.irq_ops->irdma_cfg_aeq(&rf->sc_dev, msix_vec->idx, true);
1134 
1135 	return 0;
1136 }
1137 
1138 /**
1139  * irdma_create_ceq - create completion event queue
1140  * @rf: RDMA PCI function
1141  * @iwceq: pointer to the ceq resources to be created
1142  * @ceq_id: the id number of the iwceq
1143  * @vsi: SC vsi struct
1144  *
1145  * Return 0, if the ceq and the resources associated with it
1146  * are successfully created, otherwise return error
1147  */
1148 static int irdma_create_ceq(struct irdma_pci_f *rf, struct irdma_ceq *iwceq,
1149 			    u32 ceq_id, struct irdma_sc_vsi *vsi)
1150 {
1151 	int status;
1152 	struct irdma_ceq_init_info info = {};
1153 	struct irdma_sc_dev *dev = &rf->sc_dev;
1154 	u64 scratch;
1155 	u32 ceq_size;
1156 
1157 	info.ceq_id = ceq_id;
1158 	iwceq->rf = rf;
1159 	ceq_size = min(rf->sc_dev.hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt,
1160 		       dev->hw_attrs.max_hw_ceq_size);
1161 	iwceq->mem.size = ALIGN(sizeof(struct irdma_ceqe) * ceq_size,
1162 				IRDMA_CEQ_ALIGNMENT);
1163 	iwceq->mem.va = dma_alloc_coherent(dev->hw->device, iwceq->mem.size,
1164 					   &iwceq->mem.pa, GFP_KERNEL);
1165 	if (!iwceq->mem.va)
1166 		return -ENOMEM;
1167 
1168 	info.ceq_id = ceq_id;
1169 	info.ceqe_base = iwceq->mem.va;
1170 	info.ceqe_pa = iwceq->mem.pa;
1171 	info.elem_cnt = ceq_size;
1172 	iwceq->sc_ceq.ceq_id = ceq_id;
1173 	info.dev = dev;
1174 	info.vsi = vsi;
1175 	scratch = (uintptr_t)&rf->cqp.sc_cqp;
1176 	status = irdma_sc_ceq_init(&iwceq->sc_ceq, &info);
1177 	if (!status) {
1178 		if (dev->ceq_valid)
1179 			status = irdma_cqp_ceq_cmd(&rf->sc_dev, &iwceq->sc_ceq,
1180 						   IRDMA_OP_CEQ_CREATE);
1181 		else
1182 			status = irdma_sc_cceq_create(&iwceq->sc_ceq, scratch);
1183 	}
1184 
1185 	if (status) {
1186 		dma_free_coherent(dev->hw->device, iwceq->mem.size,
1187 				  iwceq->mem.va, iwceq->mem.pa);
1188 		iwceq->mem.va = NULL;
1189 	}
1190 
1191 	return status;
1192 }
1193 
1194 /**
1195  * irdma_setup_ceq_0 - create CEQ 0 and it's interrupt resource
1196  * @rf: RDMA PCI function
1197  *
1198  * Allocate a list for all device completion event queues
1199  * Create the ceq 0 and configure it's msix interrupt vector
1200  * Return 0, if successfully set up, otherwise return error
1201  */
1202 static int irdma_setup_ceq_0(struct irdma_pci_f *rf)
1203 {
1204 	struct irdma_ceq *iwceq;
1205 	struct irdma_msix_vector *msix_vec;
1206 	u32 i;
1207 	int status = 0;
1208 	u32 num_ceqs;
1209 
1210 	num_ceqs = min(rf->msix_count, rf->sc_dev.hmc_fpm_misc.max_ceqs);
1211 	rf->ceqlist = kcalloc(num_ceqs, sizeof(*rf->ceqlist), GFP_KERNEL);
1212 	if (!rf->ceqlist) {
1213 		status = -ENOMEM;
1214 		goto exit;
1215 	}
1216 
1217 	iwceq = &rf->ceqlist[0];
1218 	status = irdma_create_ceq(rf, iwceq, 0, &rf->default_vsi);
1219 	if (status) {
1220 		ibdev_dbg(&rf->iwdev->ibdev, "ERR: create ceq status = %d\n",
1221 			  status);
1222 		goto exit;
1223 	}
1224 
1225 	spin_lock_init(&iwceq->ce_lock);
1226 	i = rf->msix_shared ? 0 : 1;
1227 	msix_vec = &rf->iw_msixtbl[i];
1228 	iwceq->irq = msix_vec->irq;
1229 	iwceq->msix_idx = msix_vec->idx;
1230 	status = irdma_cfg_ceq_vector(rf, iwceq, 0, msix_vec);
1231 	if (status) {
1232 		irdma_destroy_ceq(rf, iwceq);
1233 		goto exit;
1234 	}
1235 
1236 	irdma_ena_intr(&rf->sc_dev, msix_vec->idx);
1237 	rf->ceqs_count++;
1238 
1239 exit:
1240 	if (status && !rf->ceqs_count) {
1241 		kfree(rf->ceqlist);
1242 		rf->ceqlist = NULL;
1243 		return status;
1244 	}
1245 	rf->sc_dev.ceq_valid = true;
1246 
1247 	return 0;
1248 }
1249 
1250 /**
1251  * irdma_setup_ceqs - manage the device ceq's and their interrupt resources
1252  * @rf: RDMA PCI function
1253  * @vsi: VSI structure for this CEQ
1254  *
1255  * Allocate a list for all device completion event queues
1256  * Create the ceq's and configure their msix interrupt vectors
1257  * Return 0, if ceqs are successfully set up, otherwise return error
1258  */
1259 static int irdma_setup_ceqs(struct irdma_pci_f *rf, struct irdma_sc_vsi *vsi)
1260 {
1261 	u32 i;
1262 	u32 ceq_id;
1263 	struct irdma_ceq *iwceq;
1264 	struct irdma_msix_vector *msix_vec;
1265 	int status;
1266 	u32 num_ceqs;
1267 
1268 	num_ceqs = min(rf->msix_count, rf->sc_dev.hmc_fpm_misc.max_ceqs);
1269 	i = (rf->msix_shared) ? 1 : 2;
1270 	for (ceq_id = 1; i < num_ceqs; i++, ceq_id++) {
1271 		iwceq = &rf->ceqlist[ceq_id];
1272 		status = irdma_create_ceq(rf, iwceq, ceq_id, vsi);
1273 		if (status) {
1274 			ibdev_dbg(&rf->iwdev->ibdev,
1275 				  "ERR: create ceq status = %d\n", status);
1276 			goto del_ceqs;
1277 		}
1278 		spin_lock_init(&iwceq->ce_lock);
1279 		msix_vec = &rf->iw_msixtbl[i];
1280 		iwceq->irq = msix_vec->irq;
1281 		iwceq->msix_idx = msix_vec->idx;
1282 		status = irdma_cfg_ceq_vector(rf, iwceq, ceq_id, msix_vec);
1283 		if (status) {
1284 			irdma_destroy_ceq(rf, iwceq);
1285 			goto del_ceqs;
1286 		}
1287 		irdma_ena_intr(&rf->sc_dev, msix_vec->idx);
1288 		rf->ceqs_count++;
1289 	}
1290 
1291 	return 0;
1292 
1293 del_ceqs:
1294 	irdma_del_ceqs(rf);
1295 
1296 	return status;
1297 }
1298 
1299 static int irdma_create_virt_aeq(struct irdma_pci_f *rf, u32 size)
1300 {
1301 	struct irdma_aeq *aeq = &rf->aeq;
1302 	dma_addr_t *pg_arr;
1303 	u32 pg_cnt;
1304 	int status;
1305 
1306 	if (rf->rdma_ver < IRDMA_GEN_2)
1307 		return -EOPNOTSUPP;
1308 
1309 	aeq->mem.size = sizeof(struct irdma_sc_aeqe) * size;
1310 	aeq->mem.va = vzalloc(aeq->mem.size);
1311 
1312 	if (!aeq->mem.va)
1313 		return -ENOMEM;
1314 
1315 	pg_cnt = DIV_ROUND_UP(aeq->mem.size, PAGE_SIZE);
1316 	status = irdma_get_pble(rf->pble_rsrc, &aeq->palloc, pg_cnt, true);
1317 	if (status) {
1318 		vfree(aeq->mem.va);
1319 		return status;
1320 	}
1321 
1322 	pg_arr = (dma_addr_t *)aeq->palloc.level1.addr;
1323 	status = irdma_map_vm_page_list(&rf->hw, aeq->mem.va, pg_arr, pg_cnt);
1324 	if (status) {
1325 		irdma_free_pble(rf->pble_rsrc, &aeq->palloc);
1326 		vfree(aeq->mem.va);
1327 		return status;
1328 	}
1329 
1330 	return 0;
1331 }
1332 
1333 /**
1334  * irdma_create_aeq - create async event queue
1335  * @rf: RDMA PCI function
1336  *
1337  * Return 0, if the aeq and the resources associated with it
1338  * are successfully created, otherwise return error
1339  */
1340 static int irdma_create_aeq(struct irdma_pci_f *rf)
1341 {
1342 	struct irdma_aeq_init_info info = {};
1343 	struct irdma_sc_dev *dev = &rf->sc_dev;
1344 	struct irdma_aeq *aeq = &rf->aeq;
1345 	struct irdma_hmc_info *hmc_info = rf->sc_dev.hmc_info;
1346 	u32 aeq_size;
1347 	u8 multiplier = (rf->protocol_used == IRDMA_IWARP_PROTOCOL_ONLY) ? 2 : 1;
1348 	int status;
1349 
1350 	aeq_size = multiplier * hmc_info->hmc_obj[IRDMA_HMC_IW_QP].cnt +
1351 		   hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt;
1352 	aeq_size = min(aeq_size, dev->hw_attrs.max_hw_aeq_size);
1353 
1354 	aeq->mem.size = ALIGN(sizeof(struct irdma_sc_aeqe) * aeq_size,
1355 			      IRDMA_AEQ_ALIGNMENT);
1356 	aeq->mem.va = dma_alloc_coherent(dev->hw->device, aeq->mem.size,
1357 					 &aeq->mem.pa,
1358 					 GFP_KERNEL | __GFP_NOWARN);
1359 	if (aeq->mem.va)
1360 		goto skip_virt_aeq;
1361 
1362 	/* physically mapped aeq failed. setup virtual aeq */
1363 	status = irdma_create_virt_aeq(rf, aeq_size);
1364 	if (status)
1365 		return status;
1366 
1367 	info.virtual_map = true;
1368 	aeq->virtual_map = info.virtual_map;
1369 	info.pbl_chunk_size = 1;
1370 	info.first_pm_pbl_idx = aeq->palloc.level1.idx;
1371 
1372 skip_virt_aeq:
1373 	info.aeqe_base = aeq->mem.va;
1374 	info.aeq_elem_pa = aeq->mem.pa;
1375 	info.elem_cnt = aeq_size;
1376 	info.dev = dev;
1377 	info.msix_idx = rf->iw_msixtbl->idx;
1378 	status = irdma_sc_aeq_init(&aeq->sc_aeq, &info);
1379 	if (status)
1380 		goto err;
1381 
1382 	status = irdma_cqp_aeq_cmd(dev, &aeq->sc_aeq, IRDMA_OP_AEQ_CREATE);
1383 	if (status)
1384 		goto err;
1385 
1386 	return 0;
1387 
1388 err:
1389 	if (aeq->virtual_map) {
1390 		irdma_destroy_virt_aeq(rf);
1391 	} else {
1392 		dma_free_coherent(dev->hw->device, aeq->mem.size, aeq->mem.va,
1393 				  aeq->mem.pa);
1394 		aeq->mem.va = NULL;
1395 	}
1396 
1397 	return status;
1398 }
1399 
1400 /**
1401  * irdma_setup_aeq - set up the device aeq
1402  * @rf: RDMA PCI function
1403  *
1404  * Create the aeq and configure its msix interrupt vector
1405  * Return 0 if successful, otherwise return error
1406  */
1407 static int irdma_setup_aeq(struct irdma_pci_f *rf)
1408 {
1409 	struct irdma_sc_dev *dev = &rf->sc_dev;
1410 	int status;
1411 
1412 	status = irdma_create_aeq(rf);
1413 	if (status)
1414 		return status;
1415 
1416 	status = irdma_cfg_aeq_vector(rf);
1417 	if (status) {
1418 		irdma_destroy_aeq(rf);
1419 		return status;
1420 	}
1421 
1422 	if (!rf->msix_shared)
1423 		irdma_ena_intr(dev, rf->iw_msixtbl[0].idx);
1424 
1425 	return 0;
1426 }
1427 
1428 /**
1429  * irdma_initialize_ilq - create iwarp local queue for cm
1430  * @iwdev: irdma device
1431  *
1432  * Return 0 if successful, otherwise return error
1433  */
1434 static int irdma_initialize_ilq(struct irdma_device *iwdev)
1435 {
1436 	struct irdma_puda_rsrc_info info = {};
1437 	int status;
1438 
1439 	info.type = IRDMA_PUDA_RSRC_TYPE_ILQ;
1440 	info.cq_id = 1;
1441 	info.qp_id = 1;
1442 	info.count = 1;
1443 	info.pd_id = 1;
1444 	info.abi_ver = IRDMA_ABI_VER;
1445 	info.sq_size = min(iwdev->rf->max_qp / 2, (u32)32768);
1446 	info.rq_size = info.sq_size;
1447 	info.buf_size = 1024;
1448 	info.tx_buf_cnt = 2 * info.sq_size;
1449 	info.receive = irdma_receive_ilq;
1450 	info.xmit_complete = irdma_free_sqbuf;
1451 	status = irdma_puda_create_rsrc(&iwdev->vsi, &info);
1452 	if (status)
1453 		ibdev_dbg(&iwdev->ibdev, "ERR: ilq create fail\n");
1454 
1455 	return status;
1456 }
1457 
1458 /**
1459  * irdma_initialize_ieq - create iwarp exception queue
1460  * @iwdev: irdma device
1461  *
1462  * Return 0 if successful, otherwise return error
1463  */
1464 static int irdma_initialize_ieq(struct irdma_device *iwdev)
1465 {
1466 	struct irdma_puda_rsrc_info info = {};
1467 	int status;
1468 
1469 	info.type = IRDMA_PUDA_RSRC_TYPE_IEQ;
1470 	info.cq_id = 2;
1471 	info.qp_id = iwdev->vsi.exception_lan_q;
1472 	info.count = 1;
1473 	info.pd_id = 2;
1474 	info.abi_ver = IRDMA_ABI_VER;
1475 	info.sq_size = min(iwdev->rf->max_qp / 2, (u32)32768);
1476 	info.rq_size = info.sq_size;
1477 	info.buf_size = iwdev->vsi.mtu + IRDMA_IPV4_PAD;
1478 	info.tx_buf_cnt = 4096;
1479 	status = irdma_puda_create_rsrc(&iwdev->vsi, &info);
1480 	if (status)
1481 		ibdev_dbg(&iwdev->ibdev, "ERR: ieq create fail\n");
1482 
1483 	return status;
1484 }
1485 
1486 /**
1487  * irdma_reinitialize_ieq - destroy and re-create ieq
1488  * @vsi: VSI structure
1489  */
1490 void irdma_reinitialize_ieq(struct irdma_sc_vsi *vsi)
1491 {
1492 	struct irdma_device *iwdev = vsi->back_vsi;
1493 	struct irdma_pci_f *rf = iwdev->rf;
1494 
1495 	irdma_puda_dele_rsrc(vsi, IRDMA_PUDA_RSRC_TYPE_IEQ, false);
1496 	if (irdma_initialize_ieq(iwdev)) {
1497 		iwdev->rf->reset = true;
1498 		rf->gen_ops.request_reset(rf);
1499 	}
1500 }
1501 
1502 /**
1503  * irdma_hmc_setup - create hmc objects for the device
1504  * @rf: RDMA PCI function
1505  *
1506  * Set up the device private memory space for the number and size of
1507  * the hmc objects and create the objects
1508  * Return 0 if successful, otherwise return error
1509  */
1510 static int irdma_hmc_setup(struct irdma_pci_f *rf)
1511 {
1512 	int status;
1513 	u32 qpcnt;
1514 
1515 	if (rf->rdma_ver == IRDMA_GEN_1)
1516 		qpcnt = rsrc_limits_table[rf->limits_sel].qplimit * 2;
1517 	else
1518 		qpcnt = rsrc_limits_table[rf->limits_sel].qplimit;
1519 
1520 	rf->sd_type = IRDMA_SD_TYPE_DIRECT;
1521 	status = irdma_cfg_fpm_val(&rf->sc_dev, qpcnt);
1522 	if (status)
1523 		return status;
1524 
1525 	status = irdma_create_hmc_objs(rf, true, rf->rdma_ver);
1526 
1527 	return status;
1528 }
1529 
1530 /**
1531  * irdma_del_init_mem - deallocate memory resources
1532  * @rf: RDMA PCI function
1533  */
1534 static void irdma_del_init_mem(struct irdma_pci_f *rf)
1535 {
1536 	struct irdma_sc_dev *dev = &rf->sc_dev;
1537 
1538 	kfree(dev->hmc_info->sd_table.sd_entry);
1539 	dev->hmc_info->sd_table.sd_entry = NULL;
1540 	kfree(rf->mem_rsrc);
1541 	rf->mem_rsrc = NULL;
1542 	dma_free_coherent(rf->hw.device, rf->obj_mem.size, rf->obj_mem.va,
1543 			  rf->obj_mem.pa);
1544 	rf->obj_mem.va = NULL;
1545 	if (rf->rdma_ver != IRDMA_GEN_1) {
1546 		kfree(rf->allocated_ws_nodes);
1547 		rf->allocated_ws_nodes = NULL;
1548 	}
1549 	kfree(rf->ceqlist);
1550 	rf->ceqlist = NULL;
1551 	kfree(rf->iw_msixtbl);
1552 	rf->iw_msixtbl = NULL;
1553 	kfree(rf->hmc_info_mem);
1554 	rf->hmc_info_mem = NULL;
1555 }
1556 
1557 /**
1558  * irdma_initialize_dev - initialize device
1559  * @rf: RDMA PCI function
1560  *
1561  * Allocate memory for the hmc objects and initialize iwdev
1562  * Return 0 if successful, otherwise clean up the resources
1563  * and return error
1564  */
1565 static int irdma_initialize_dev(struct irdma_pci_f *rf)
1566 {
1567 	int status;
1568 	struct irdma_sc_dev *dev = &rf->sc_dev;
1569 	struct irdma_device_init_info info = {};
1570 	struct irdma_dma_mem mem;
1571 	u32 size;
1572 
1573 	size = sizeof(struct irdma_hmc_pble_rsrc) +
1574 	       sizeof(struct irdma_hmc_info) +
1575 	       (sizeof(struct irdma_hmc_obj_info) * IRDMA_HMC_IW_MAX);
1576 
1577 	rf->hmc_info_mem = kzalloc(size, GFP_KERNEL);
1578 	if (!rf->hmc_info_mem)
1579 		return -ENOMEM;
1580 
1581 	rf->pble_rsrc = (struct irdma_hmc_pble_rsrc *)rf->hmc_info_mem;
1582 	dev->hmc_info = &rf->hw.hmc;
1583 	dev->hmc_info->hmc_obj = (struct irdma_hmc_obj_info *)
1584 				 (rf->pble_rsrc + 1);
1585 
1586 	status = irdma_obj_aligned_mem(rf, &mem, IRDMA_QUERY_FPM_BUF_SIZE,
1587 				       IRDMA_FPM_QUERY_BUF_ALIGNMENT_M);
1588 	if (status)
1589 		goto error;
1590 
1591 	info.fpm_query_buf_pa = mem.pa;
1592 	info.fpm_query_buf = mem.va;
1593 
1594 	status = irdma_obj_aligned_mem(rf, &mem, IRDMA_COMMIT_FPM_BUF_SIZE,
1595 				       IRDMA_FPM_COMMIT_BUF_ALIGNMENT_M);
1596 	if (status)
1597 		goto error;
1598 
1599 	info.fpm_commit_buf_pa = mem.pa;
1600 	info.fpm_commit_buf = mem.va;
1601 
1602 	info.bar0 = rf->hw.hw_addr;
1603 	info.hmc_fn_id = rf->pf_id;
1604 	info.hw = &rf->hw;
1605 	status = irdma_sc_dev_init(rf->rdma_ver, &rf->sc_dev, &info);
1606 	if (status)
1607 		goto error;
1608 
1609 	return status;
1610 error:
1611 	kfree(rf->hmc_info_mem);
1612 	rf->hmc_info_mem = NULL;
1613 
1614 	return status;
1615 }
1616 
1617 /**
1618  * irdma_rt_deinit_hw - clean up the irdma device resources
1619  * @iwdev: irdma device
1620  *
1621  * remove the mac ip entry and ipv4/ipv6 addresses, destroy the
1622  * device queues and free the pble and the hmc objects
1623  */
1624 void irdma_rt_deinit_hw(struct irdma_device *iwdev)
1625 {
1626 	ibdev_dbg(&iwdev->ibdev, "INIT: state = %d\n", iwdev->init_state);
1627 
1628 	switch (iwdev->init_state) {
1629 	case IP_ADDR_REGISTERED:
1630 		if (iwdev->rf->sc_dev.hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
1631 			irdma_del_local_mac_entry(iwdev->rf,
1632 						  (u8)iwdev->mac_ip_table_idx);
1633 		fallthrough;
1634 	case AEQ_CREATED:
1635 	case PBLE_CHUNK_MEM:
1636 	case CEQS_CREATED:
1637 	case IEQ_CREATED:
1638 		if (!iwdev->roce_mode)
1639 			irdma_puda_dele_rsrc(&iwdev->vsi, IRDMA_PUDA_RSRC_TYPE_IEQ,
1640 					     iwdev->rf->reset);
1641 		fallthrough;
1642 	case ILQ_CREATED:
1643 		if (!iwdev->roce_mode)
1644 			irdma_puda_dele_rsrc(&iwdev->vsi,
1645 					     IRDMA_PUDA_RSRC_TYPE_ILQ,
1646 					     iwdev->rf->reset);
1647 		break;
1648 	default:
1649 		ibdev_warn(&iwdev->ibdev, "bad init_state = %d\n", iwdev->init_state);
1650 		break;
1651 	}
1652 
1653 	irdma_cleanup_cm_core(&iwdev->cm_core);
1654 	if (iwdev->vsi.pestat) {
1655 		irdma_vsi_stats_free(&iwdev->vsi);
1656 		kfree(iwdev->vsi.pestat);
1657 	}
1658 	if (iwdev->cleanup_wq)
1659 		destroy_workqueue(iwdev->cleanup_wq);
1660 }
1661 
1662 static int irdma_setup_init_state(struct irdma_pci_f *rf)
1663 {
1664 	int status;
1665 
1666 	status = irdma_save_msix_info(rf);
1667 	if (status)
1668 		return status;
1669 
1670 	rf->hw.device = &rf->pcidev->dev;
1671 	rf->obj_mem.size = ALIGN(8192, IRDMA_HW_PAGE_SIZE);
1672 	rf->obj_mem.va = dma_alloc_coherent(rf->hw.device, rf->obj_mem.size,
1673 					    &rf->obj_mem.pa, GFP_KERNEL);
1674 	if (!rf->obj_mem.va) {
1675 		status = -ENOMEM;
1676 		goto clean_msixtbl;
1677 	}
1678 
1679 	rf->obj_next = rf->obj_mem;
1680 	status = irdma_initialize_dev(rf);
1681 	if (status)
1682 		goto clean_obj_mem;
1683 
1684 	return 0;
1685 
1686 clean_obj_mem:
1687 	dma_free_coherent(rf->hw.device, rf->obj_mem.size, rf->obj_mem.va,
1688 			  rf->obj_mem.pa);
1689 	rf->obj_mem.va = NULL;
1690 clean_msixtbl:
1691 	kfree(rf->iw_msixtbl);
1692 	rf->iw_msixtbl = NULL;
1693 	return status;
1694 }
1695 
1696 /**
1697  * irdma_get_used_rsrc - determine resources used internally
1698  * @iwdev: irdma device
1699  *
1700  * Called at the end of open to get all internal allocations
1701  */
1702 static void irdma_get_used_rsrc(struct irdma_device *iwdev)
1703 {
1704 	iwdev->rf->used_pds = find_first_zero_bit(iwdev->rf->allocated_pds,
1705 						 iwdev->rf->max_pd);
1706 	iwdev->rf->used_qps = find_first_zero_bit(iwdev->rf->allocated_qps,
1707 						 iwdev->rf->max_qp);
1708 	iwdev->rf->used_cqs = find_first_zero_bit(iwdev->rf->allocated_cqs,
1709 						 iwdev->rf->max_cq);
1710 	iwdev->rf->used_mrs = find_first_zero_bit(iwdev->rf->allocated_mrs,
1711 						 iwdev->rf->max_mr);
1712 }
1713 
1714 void irdma_ctrl_deinit_hw(struct irdma_pci_f *rf)
1715 {
1716 	enum init_completion_state state = rf->init_state;
1717 
1718 	rf->init_state = INVALID_STATE;
1719 	if (rf->rsrc_created) {
1720 		irdma_destroy_aeq(rf);
1721 		irdma_destroy_pble_prm(rf->pble_rsrc);
1722 		irdma_del_ceqs(rf);
1723 		rf->rsrc_created = false;
1724 	}
1725 	switch (state) {
1726 	case CEQ0_CREATED:
1727 		irdma_del_ceq_0(rf);
1728 		fallthrough;
1729 	case CCQ_CREATED:
1730 		irdma_destroy_ccq(rf);
1731 		fallthrough;
1732 	case HW_RSRC_INITIALIZED:
1733 	case HMC_OBJS_CREATED:
1734 		irdma_del_hmc_objects(&rf->sc_dev, rf->sc_dev.hmc_info, true,
1735 				      rf->reset, rf->rdma_ver);
1736 		fallthrough;
1737 	case CQP_CREATED:
1738 		irdma_destroy_cqp(rf, true);
1739 		fallthrough;
1740 	case INITIAL_STATE:
1741 		irdma_del_init_mem(rf);
1742 		break;
1743 	case INVALID_STATE:
1744 	default:
1745 		ibdev_warn(&rf->iwdev->ibdev, "bad init_state = %d\n", rf->init_state);
1746 		break;
1747 	}
1748 }
1749 
1750 /**
1751  * irdma_rt_init_hw - Initializes runtime portion of HW
1752  * @iwdev: irdma device
1753  * @l2params: qos, tc, mtu info from netdev driver
1754  *
1755  * Create device queues ILQ, IEQ, CEQs and PBLEs. Setup irdma
1756  * device resource objects.
1757  */
1758 int irdma_rt_init_hw(struct irdma_device *iwdev,
1759 		     struct irdma_l2params *l2params)
1760 {
1761 	struct irdma_pci_f *rf = iwdev->rf;
1762 	struct irdma_sc_dev *dev = &rf->sc_dev;
1763 	struct irdma_vsi_init_info vsi_info = {};
1764 	struct irdma_vsi_stats_info stats_info = {};
1765 	int status;
1766 
1767 	vsi_info.dev = dev;
1768 	vsi_info.back_vsi = iwdev;
1769 	vsi_info.params = l2params;
1770 	vsi_info.pf_data_vsi_num = iwdev->vsi_num;
1771 	vsi_info.register_qset = rf->gen_ops.register_qset;
1772 	vsi_info.unregister_qset = rf->gen_ops.unregister_qset;
1773 	vsi_info.exception_lan_q = 2;
1774 	irdma_sc_vsi_init(&iwdev->vsi, &vsi_info);
1775 
1776 	status = irdma_setup_cm_core(iwdev, rf->rdma_ver);
1777 	if (status)
1778 		return status;
1779 
1780 	stats_info.pestat = kzalloc(sizeof(*stats_info.pestat), GFP_KERNEL);
1781 	if (!stats_info.pestat) {
1782 		irdma_cleanup_cm_core(&iwdev->cm_core);
1783 		return -ENOMEM;
1784 	}
1785 	stats_info.fcn_id = dev->hmc_fn_id;
1786 	status = irdma_vsi_stats_init(&iwdev->vsi, &stats_info);
1787 	if (status) {
1788 		irdma_cleanup_cm_core(&iwdev->cm_core);
1789 		kfree(stats_info.pestat);
1790 		return status;
1791 	}
1792 
1793 	do {
1794 		if (!iwdev->roce_mode) {
1795 			status = irdma_initialize_ilq(iwdev);
1796 			if (status)
1797 				break;
1798 			iwdev->init_state = ILQ_CREATED;
1799 			status = irdma_initialize_ieq(iwdev);
1800 			if (status)
1801 				break;
1802 			iwdev->init_state = IEQ_CREATED;
1803 		}
1804 		if (!rf->rsrc_created) {
1805 			status = irdma_setup_ceqs(rf, &iwdev->vsi);
1806 			if (status)
1807 				break;
1808 
1809 			iwdev->init_state = CEQS_CREATED;
1810 
1811 			status = irdma_hmc_init_pble(&rf->sc_dev,
1812 						     rf->pble_rsrc);
1813 			if (status) {
1814 				irdma_del_ceqs(rf);
1815 				break;
1816 			}
1817 
1818 			iwdev->init_state = PBLE_CHUNK_MEM;
1819 
1820 			status = irdma_setup_aeq(rf);
1821 			if (status) {
1822 				irdma_destroy_pble_prm(rf->pble_rsrc);
1823 				irdma_del_ceqs(rf);
1824 				break;
1825 			}
1826 			iwdev->init_state = AEQ_CREATED;
1827 			rf->rsrc_created = true;
1828 		}
1829 
1830 		iwdev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY |
1831 					  IB_DEVICE_MEM_WINDOW |
1832 					  IB_DEVICE_MEM_MGT_EXTENSIONS;
1833 
1834 		if (iwdev->rf->sc_dev.hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
1835 			irdma_alloc_set_mac(iwdev);
1836 		irdma_add_ip(iwdev);
1837 		iwdev->init_state = IP_ADDR_REGISTERED;
1838 
1839 		/* handles asynch cleanup tasks - disconnect CM , free qp,
1840 		 * free cq bufs
1841 		 */
1842 		iwdev->cleanup_wq = alloc_workqueue("irdma-cleanup-wq",
1843 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
1844 		if (!iwdev->cleanup_wq)
1845 			return -ENOMEM;
1846 		irdma_get_used_rsrc(iwdev);
1847 		init_waitqueue_head(&iwdev->suspend_wq);
1848 
1849 		return 0;
1850 	} while (0);
1851 
1852 	dev_err(&rf->pcidev->dev, "HW runtime init FAIL status = %d last cmpl = %d\n",
1853 		status, iwdev->init_state);
1854 	irdma_rt_deinit_hw(iwdev);
1855 
1856 	return status;
1857 }
1858 
1859 /**
1860  * irdma_ctrl_init_hw - Initializes control portion of HW
1861  * @rf: RDMA PCI function
1862  *
1863  * Create admin queues, HMC obejcts and RF resource objects
1864  */
1865 int irdma_ctrl_init_hw(struct irdma_pci_f *rf)
1866 {
1867 	struct irdma_sc_dev *dev = &rf->sc_dev;
1868 	int status;
1869 	do {
1870 		status = irdma_setup_init_state(rf);
1871 		if (status)
1872 			break;
1873 		rf->init_state = INITIAL_STATE;
1874 
1875 		status = irdma_create_cqp(rf);
1876 		if (status)
1877 			break;
1878 		rf->init_state = CQP_CREATED;
1879 
1880 		status = irdma_hmc_setup(rf);
1881 		if (status)
1882 			break;
1883 		rf->init_state = HMC_OBJS_CREATED;
1884 
1885 		status = irdma_initialize_hw_rsrc(rf);
1886 		if (status)
1887 			break;
1888 		rf->init_state = HW_RSRC_INITIALIZED;
1889 
1890 		status = irdma_create_ccq(rf);
1891 		if (status)
1892 			break;
1893 		rf->init_state = CCQ_CREATED;
1894 
1895 		dev->feature_info[IRDMA_FEATURE_FW_INFO] = IRDMA_FW_VER_DEFAULT;
1896 		if (rf->rdma_ver != IRDMA_GEN_1) {
1897 			status = irdma_get_rdma_features(dev);
1898 			if (status)
1899 				break;
1900 		}
1901 
1902 		status = irdma_setup_ceq_0(rf);
1903 		if (status)
1904 			break;
1905 		rf->init_state = CEQ0_CREATED;
1906 		/* Handles processing of CQP completions */
1907 		rf->cqp_cmpl_wq = alloc_ordered_workqueue("cqp_cmpl_wq",
1908 						WQ_HIGHPRI | WQ_UNBOUND);
1909 		if (!rf->cqp_cmpl_wq) {
1910 			status = -ENOMEM;
1911 			break;
1912 		}
1913 		INIT_WORK(&rf->cqp_cmpl_work, cqp_compl_worker);
1914 		irdma_sc_ccq_arm(dev->ccq);
1915 		return 0;
1916 	} while (0);
1917 
1918 	dev_err(&rf->pcidev->dev, "IRDMA hardware initialization FAILED init_state=%d status=%d\n",
1919 		rf->init_state, status);
1920 	irdma_ctrl_deinit_hw(rf);
1921 	return status;
1922 }
1923 
1924 /**
1925  * irdma_set_hw_rsrc - set hw memory resources.
1926  * @rf: RDMA PCI function
1927  */
1928 static void irdma_set_hw_rsrc(struct irdma_pci_f *rf)
1929 {
1930 	rf->allocated_qps = (void *)(rf->mem_rsrc +
1931 		   (sizeof(struct irdma_arp_entry) * rf->arp_table_size));
1932 	rf->allocated_cqs = &rf->allocated_qps[BITS_TO_LONGS(rf->max_qp)];
1933 	rf->allocated_mrs = &rf->allocated_cqs[BITS_TO_LONGS(rf->max_cq)];
1934 	rf->allocated_pds = &rf->allocated_mrs[BITS_TO_LONGS(rf->max_mr)];
1935 	rf->allocated_ahs = &rf->allocated_pds[BITS_TO_LONGS(rf->max_pd)];
1936 	rf->allocated_mcgs = &rf->allocated_ahs[BITS_TO_LONGS(rf->max_ah)];
1937 	rf->allocated_arps = &rf->allocated_mcgs[BITS_TO_LONGS(rf->max_mcg)];
1938 	rf->qp_table = (struct irdma_qp **)
1939 		(&rf->allocated_arps[BITS_TO_LONGS(rf->arp_table_size)]);
1940 
1941 	spin_lock_init(&rf->rsrc_lock);
1942 	spin_lock_init(&rf->arp_lock);
1943 	spin_lock_init(&rf->qptable_lock);
1944 	spin_lock_init(&rf->qh_list_lock);
1945 }
1946 
1947 /**
1948  * irdma_calc_mem_rsrc_size - calculate memory resources size.
1949  * @rf: RDMA PCI function
1950  */
1951 static u32 irdma_calc_mem_rsrc_size(struct irdma_pci_f *rf)
1952 {
1953 	u32 rsrc_size;
1954 
1955 	rsrc_size = sizeof(struct irdma_arp_entry) * rf->arp_table_size;
1956 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->max_qp);
1957 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->max_mr);
1958 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->max_cq);
1959 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->max_pd);
1960 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->arp_table_size);
1961 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->max_ah);
1962 	rsrc_size += sizeof(unsigned long) * BITS_TO_LONGS(rf->max_mcg);
1963 	rsrc_size += sizeof(struct irdma_qp **) * rf->max_qp;
1964 
1965 	return rsrc_size;
1966 }
1967 
1968 /**
1969  * irdma_initialize_hw_rsrc - initialize hw resource tracking array
1970  * @rf: RDMA PCI function
1971  */
1972 u32 irdma_initialize_hw_rsrc(struct irdma_pci_f *rf)
1973 {
1974 	u32 rsrc_size;
1975 	u32 mrdrvbits;
1976 	u32 ret;
1977 
1978 	if (rf->rdma_ver != IRDMA_GEN_1) {
1979 		rf->allocated_ws_nodes =
1980 			kcalloc(BITS_TO_LONGS(IRDMA_MAX_WS_NODES),
1981 				sizeof(unsigned long), GFP_KERNEL);
1982 		if (!rf->allocated_ws_nodes)
1983 			return -ENOMEM;
1984 
1985 		set_bit(0, rf->allocated_ws_nodes);
1986 		rf->max_ws_node_id = IRDMA_MAX_WS_NODES;
1987 	}
1988 	rf->max_cqe = rf->sc_dev.hw_attrs.uk_attrs.max_hw_cq_size;
1989 	rf->max_qp = rf->sc_dev.hmc_info->hmc_obj[IRDMA_HMC_IW_QP].cnt;
1990 	rf->max_mr = rf->sc_dev.hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt;
1991 	rf->max_cq = rf->sc_dev.hmc_info->hmc_obj[IRDMA_HMC_IW_CQ].cnt;
1992 	rf->max_pd = rf->sc_dev.hw_attrs.max_hw_pds;
1993 	rf->arp_table_size = rf->sc_dev.hmc_info->hmc_obj[IRDMA_HMC_IW_ARP].cnt;
1994 	rf->max_ah = rf->sc_dev.hmc_info->hmc_obj[IRDMA_HMC_IW_FSIAV].cnt;
1995 	rf->max_mcg = rf->max_qp;
1996 
1997 	rsrc_size = irdma_calc_mem_rsrc_size(rf);
1998 	rf->mem_rsrc = kzalloc(rsrc_size, GFP_KERNEL);
1999 	if (!rf->mem_rsrc) {
2000 		ret = -ENOMEM;
2001 		goto mem_rsrc_kzalloc_fail;
2002 	}
2003 
2004 	rf->arp_table = (struct irdma_arp_entry *)rf->mem_rsrc;
2005 
2006 	irdma_set_hw_rsrc(rf);
2007 
2008 	set_bit(0, rf->allocated_mrs);
2009 	set_bit(0, rf->allocated_qps);
2010 	set_bit(0, rf->allocated_cqs);
2011 	set_bit(0, rf->allocated_pds);
2012 	set_bit(0, rf->allocated_arps);
2013 	set_bit(0, rf->allocated_ahs);
2014 	set_bit(0, rf->allocated_mcgs);
2015 	set_bit(2, rf->allocated_qps); /* qp 2 IEQ */
2016 	set_bit(1, rf->allocated_qps); /* qp 1 ILQ */
2017 	set_bit(1, rf->allocated_cqs);
2018 	set_bit(1, rf->allocated_pds);
2019 	set_bit(2, rf->allocated_cqs);
2020 	set_bit(2, rf->allocated_pds);
2021 
2022 	INIT_LIST_HEAD(&rf->mc_qht_list.list);
2023 	/* stag index mask has a minimum of 14 bits */
2024 	mrdrvbits = 24 - max(get_count_order(rf->max_mr), 14);
2025 	rf->mr_stagmask = ~(((1 << mrdrvbits) - 1) << (32 - mrdrvbits));
2026 
2027 	return 0;
2028 
2029 mem_rsrc_kzalloc_fail:
2030 	kfree(rf->allocated_ws_nodes);
2031 	rf->allocated_ws_nodes = NULL;
2032 
2033 	return ret;
2034 }
2035 
2036 /**
2037  * irdma_cqp_ce_handler - handle cqp completions
2038  * @rf: RDMA PCI function
2039  * @cq: cq for cqp completions
2040  */
2041 void irdma_cqp_ce_handler(struct irdma_pci_f *rf, struct irdma_sc_cq *cq)
2042 {
2043 	struct irdma_cqp_request *cqp_request;
2044 	struct irdma_sc_dev *dev = &rf->sc_dev;
2045 	u32 cqe_count = 0;
2046 	struct irdma_ccq_cqe_info info;
2047 	unsigned long flags;
2048 	int ret;
2049 
2050 	do {
2051 		memset(&info, 0, sizeof(info));
2052 		spin_lock_irqsave(&rf->cqp.compl_lock, flags);
2053 		ret = irdma_sc_ccq_get_cqe_info(cq, &info);
2054 		spin_unlock_irqrestore(&rf->cqp.compl_lock, flags);
2055 		if (ret)
2056 			break;
2057 
2058 		cqp_request = (struct irdma_cqp_request *)
2059 			      (unsigned long)info.scratch;
2060 		if (info.error && irdma_cqp_crit_err(dev, cqp_request->info.cqp_cmd,
2061 						     info.maj_err_code,
2062 						     info.min_err_code))
2063 			ibdev_err(&rf->iwdev->ibdev, "cqp opcode = 0x%x maj_err_code = 0x%x min_err_code = 0x%x\n",
2064 				  info.op_code, info.maj_err_code, info.min_err_code);
2065 		if (cqp_request) {
2066 			cqp_request->compl_info.maj_err_code = info.maj_err_code;
2067 			cqp_request->compl_info.min_err_code = info.min_err_code;
2068 			cqp_request->compl_info.op_ret_val = info.op_ret_val;
2069 			cqp_request->compl_info.error = info.error;
2070 
2071 			if (cqp_request->waiting) {
2072 				cqp_request->request_done = true;
2073 				wake_up(&cqp_request->waitq);
2074 				irdma_put_cqp_request(&rf->cqp, cqp_request);
2075 			} else {
2076 				if (cqp_request->callback_fcn)
2077 					cqp_request->callback_fcn(cqp_request);
2078 				irdma_put_cqp_request(&rf->cqp, cqp_request);
2079 			}
2080 		}
2081 
2082 		cqe_count++;
2083 	} while (1);
2084 
2085 	if (cqe_count) {
2086 		irdma_process_bh(dev);
2087 		irdma_sc_ccq_arm(cq);
2088 	}
2089 }
2090 
2091 /**
2092  * cqp_compl_worker - Handle cqp completions
2093  * @work: Pointer to work structure
2094  */
2095 void cqp_compl_worker(struct work_struct *work)
2096 {
2097 	struct irdma_pci_f *rf = container_of(work, struct irdma_pci_f,
2098 					      cqp_cmpl_work);
2099 	struct irdma_sc_cq *cq = &rf->ccq.sc_cq;
2100 
2101 	irdma_cqp_ce_handler(rf, cq);
2102 }
2103 
2104 /**
2105  * irdma_lookup_apbvt_entry - lookup hash table for an existing apbvt entry corresponding to port
2106  * @cm_core: cm's core
2107  * @port: port to identify apbvt entry
2108  */
2109 static struct irdma_apbvt_entry *irdma_lookup_apbvt_entry(struct irdma_cm_core *cm_core,
2110 							  u16 port)
2111 {
2112 	struct irdma_apbvt_entry *entry;
2113 
2114 	hash_for_each_possible(cm_core->apbvt_hash_tbl, entry, hlist, port) {
2115 		if (entry->port == port) {
2116 			entry->use_cnt++;
2117 			return entry;
2118 		}
2119 	}
2120 
2121 	return NULL;
2122 }
2123 
2124 /**
2125  * irdma_next_iw_state - modify qp state
2126  * @iwqp: iwarp qp to modify
2127  * @state: next state for qp
2128  * @del_hash: del hash
2129  * @term: term message
2130  * @termlen: length of term message
2131  */
2132 void irdma_next_iw_state(struct irdma_qp *iwqp, u8 state, u8 del_hash, u8 term,
2133 			 u8 termlen)
2134 {
2135 	struct irdma_modify_qp_info info = {};
2136 
2137 	info.next_iwarp_state = state;
2138 	info.remove_hash_idx = del_hash;
2139 	info.cq_num_valid = true;
2140 	info.arp_cache_idx_valid = true;
2141 	info.dont_send_term = true;
2142 	info.dont_send_fin = true;
2143 	info.termlen = termlen;
2144 
2145 	if (term & IRDMAQP_TERM_SEND_TERM_ONLY)
2146 		info.dont_send_term = false;
2147 	if (term & IRDMAQP_TERM_SEND_FIN_ONLY)
2148 		info.dont_send_fin = false;
2149 	if (iwqp->sc_qp.term_flags && state == IRDMA_QP_STATE_ERROR)
2150 		info.reset_tcp_conn = true;
2151 	iwqp->hw_iwarp_state = state;
2152 	irdma_hw_modify_qp(iwqp->iwdev, iwqp, &info, 0);
2153 	iwqp->iwarp_state = info.next_iwarp_state;
2154 }
2155 
2156 /**
2157  * irdma_del_local_mac_entry - remove a mac entry from the hw
2158  * table
2159  * @rf: RDMA PCI function
2160  * @idx: the index of the mac ip address to delete
2161  */
2162 void irdma_del_local_mac_entry(struct irdma_pci_f *rf, u16 idx)
2163 {
2164 	struct irdma_cqp *iwcqp = &rf->cqp;
2165 	struct irdma_cqp_request *cqp_request;
2166 	struct cqp_cmds_info *cqp_info;
2167 
2168 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, true);
2169 	if (!cqp_request)
2170 		return;
2171 
2172 	cqp_info = &cqp_request->info;
2173 	cqp_info->cqp_cmd = IRDMA_OP_DELETE_LOCAL_MAC_ENTRY;
2174 	cqp_info->post_sq = 1;
2175 	cqp_info->in.u.del_local_mac_entry.cqp = &iwcqp->sc_cqp;
2176 	cqp_info->in.u.del_local_mac_entry.scratch = (uintptr_t)cqp_request;
2177 	cqp_info->in.u.del_local_mac_entry.entry_idx = idx;
2178 	cqp_info->in.u.del_local_mac_entry.ignore_ref_count = 0;
2179 
2180 	irdma_handle_cqp_op(rf, cqp_request);
2181 	irdma_put_cqp_request(iwcqp, cqp_request);
2182 }
2183 
2184 /**
2185  * irdma_add_local_mac_entry - add a mac ip address entry to the
2186  * hw table
2187  * @rf: RDMA PCI function
2188  * @mac_addr: pointer to mac address
2189  * @idx: the index of the mac ip address to add
2190  */
2191 int irdma_add_local_mac_entry(struct irdma_pci_f *rf, const u8 *mac_addr, u16 idx)
2192 {
2193 	struct irdma_local_mac_entry_info *info;
2194 	struct irdma_cqp *iwcqp = &rf->cqp;
2195 	struct irdma_cqp_request *cqp_request;
2196 	struct cqp_cmds_info *cqp_info;
2197 	int status;
2198 
2199 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, true);
2200 	if (!cqp_request)
2201 		return -ENOMEM;
2202 
2203 	cqp_info = &cqp_request->info;
2204 	cqp_info->post_sq = 1;
2205 	info = &cqp_info->in.u.add_local_mac_entry.info;
2206 	ether_addr_copy(info->mac_addr, mac_addr);
2207 	info->entry_idx = idx;
2208 	cqp_info->in.u.add_local_mac_entry.scratch = (uintptr_t)cqp_request;
2209 	cqp_info->cqp_cmd = IRDMA_OP_ADD_LOCAL_MAC_ENTRY;
2210 	cqp_info->in.u.add_local_mac_entry.cqp = &iwcqp->sc_cqp;
2211 	cqp_info->in.u.add_local_mac_entry.scratch = (uintptr_t)cqp_request;
2212 
2213 	status = irdma_handle_cqp_op(rf, cqp_request);
2214 	irdma_put_cqp_request(iwcqp, cqp_request);
2215 
2216 	return status;
2217 }
2218 
2219 /**
2220  * irdma_alloc_local_mac_entry - allocate a mac entry
2221  * @rf: RDMA PCI function
2222  * @mac_tbl_idx: the index of the new mac address
2223  *
2224  * Allocate a mac address entry and update the mac_tbl_idx
2225  * to hold the index of the newly created mac address
2226  * Return 0 if successful, otherwise return error
2227  */
2228 int irdma_alloc_local_mac_entry(struct irdma_pci_f *rf, u16 *mac_tbl_idx)
2229 {
2230 	struct irdma_cqp *iwcqp = &rf->cqp;
2231 	struct irdma_cqp_request *cqp_request;
2232 	struct cqp_cmds_info *cqp_info;
2233 	int status = 0;
2234 
2235 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, true);
2236 	if (!cqp_request)
2237 		return -ENOMEM;
2238 
2239 	cqp_info = &cqp_request->info;
2240 	cqp_info->cqp_cmd = IRDMA_OP_ALLOC_LOCAL_MAC_ENTRY;
2241 	cqp_info->post_sq = 1;
2242 	cqp_info->in.u.alloc_local_mac_entry.cqp = &iwcqp->sc_cqp;
2243 	cqp_info->in.u.alloc_local_mac_entry.scratch = (uintptr_t)cqp_request;
2244 	status = irdma_handle_cqp_op(rf, cqp_request);
2245 	if (!status)
2246 		*mac_tbl_idx = (u16)cqp_request->compl_info.op_ret_val;
2247 
2248 	irdma_put_cqp_request(iwcqp, cqp_request);
2249 
2250 	return status;
2251 }
2252 
2253 /**
2254  * irdma_cqp_manage_apbvt_cmd - send cqp command manage apbvt
2255  * @iwdev: irdma device
2256  * @accel_local_port: port for apbvt
2257  * @add_port: add ordelete port
2258  */
2259 static int irdma_cqp_manage_apbvt_cmd(struct irdma_device *iwdev,
2260 				      u16 accel_local_port, bool add_port)
2261 {
2262 	struct irdma_apbvt_info *info;
2263 	struct irdma_cqp_request *cqp_request;
2264 	struct cqp_cmds_info *cqp_info;
2265 	int status;
2266 
2267 	cqp_request = irdma_alloc_and_get_cqp_request(&iwdev->rf->cqp, add_port);
2268 	if (!cqp_request)
2269 		return -ENOMEM;
2270 
2271 	cqp_info = &cqp_request->info;
2272 	info = &cqp_info->in.u.manage_apbvt_entry.info;
2273 	memset(info, 0, sizeof(*info));
2274 	info->add = add_port;
2275 	info->port = accel_local_port;
2276 	cqp_info->cqp_cmd = IRDMA_OP_MANAGE_APBVT_ENTRY;
2277 	cqp_info->post_sq = 1;
2278 	cqp_info->in.u.manage_apbvt_entry.cqp = &iwdev->rf->cqp.sc_cqp;
2279 	cqp_info->in.u.manage_apbvt_entry.scratch = (uintptr_t)cqp_request;
2280 	ibdev_dbg(&iwdev->ibdev, "DEV: %s: port=0x%04x\n",
2281 		  (!add_port) ? "DELETE" : "ADD", accel_local_port);
2282 
2283 	status = irdma_handle_cqp_op(iwdev->rf, cqp_request);
2284 	irdma_put_cqp_request(&iwdev->rf->cqp, cqp_request);
2285 
2286 	return status;
2287 }
2288 
2289 /**
2290  * irdma_add_apbvt - add tcp port to HW apbvt table
2291  * @iwdev: irdma device
2292  * @port: port for apbvt
2293  */
2294 struct irdma_apbvt_entry *irdma_add_apbvt(struct irdma_device *iwdev, u16 port)
2295 {
2296 	struct irdma_cm_core *cm_core = &iwdev->cm_core;
2297 	struct irdma_apbvt_entry *entry;
2298 	unsigned long flags;
2299 
2300 	spin_lock_irqsave(&cm_core->apbvt_lock, flags);
2301 	entry = irdma_lookup_apbvt_entry(cm_core, port);
2302 	if (entry) {
2303 		spin_unlock_irqrestore(&cm_core->apbvt_lock, flags);
2304 		return entry;
2305 	}
2306 
2307 	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2308 	if (!entry) {
2309 		spin_unlock_irqrestore(&cm_core->apbvt_lock, flags);
2310 		return NULL;
2311 	}
2312 
2313 	entry->port = port;
2314 	entry->use_cnt = 1;
2315 	hash_add(cm_core->apbvt_hash_tbl, &entry->hlist, entry->port);
2316 	spin_unlock_irqrestore(&cm_core->apbvt_lock, flags);
2317 
2318 	if (irdma_cqp_manage_apbvt_cmd(iwdev, port, true)) {
2319 		kfree(entry);
2320 		return NULL;
2321 	}
2322 
2323 	return entry;
2324 }
2325 
2326 /**
2327  * irdma_del_apbvt - delete tcp port from HW apbvt table
2328  * @iwdev: irdma device
2329  * @entry: apbvt entry object
2330  */
2331 void irdma_del_apbvt(struct irdma_device *iwdev,
2332 		     struct irdma_apbvt_entry *entry)
2333 {
2334 	struct irdma_cm_core *cm_core = &iwdev->cm_core;
2335 	unsigned long flags;
2336 
2337 	spin_lock_irqsave(&cm_core->apbvt_lock, flags);
2338 	if (--entry->use_cnt) {
2339 		spin_unlock_irqrestore(&cm_core->apbvt_lock, flags);
2340 		return;
2341 	}
2342 
2343 	hash_del(&entry->hlist);
2344 	/* apbvt_lock is held across CQP delete APBVT OP (non-waiting) to
2345 	 * protect against race where add APBVT CQP can race ahead of the delete
2346 	 * APBVT for same port.
2347 	 */
2348 	irdma_cqp_manage_apbvt_cmd(iwdev, entry->port, false);
2349 	kfree(entry);
2350 	spin_unlock_irqrestore(&cm_core->apbvt_lock, flags);
2351 }
2352 
2353 /**
2354  * irdma_manage_arp_cache - manage hw arp cache
2355  * @rf: RDMA PCI function
2356  * @mac_addr: mac address ptr
2357  * @ip_addr: ip addr for arp cache
2358  * @ipv4: flag inicating IPv4
2359  * @action: add, delete or modify
2360  */
2361 void irdma_manage_arp_cache(struct irdma_pci_f *rf,
2362 			    const unsigned char *mac_addr,
2363 			    u32 *ip_addr, bool ipv4, u32 action)
2364 {
2365 	struct irdma_add_arp_cache_entry_info *info;
2366 	struct irdma_cqp_request *cqp_request;
2367 	struct cqp_cmds_info *cqp_info;
2368 	int arp_index;
2369 
2370 	arp_index = irdma_arp_table(rf, ip_addr, ipv4, mac_addr, action);
2371 	if (arp_index == -1)
2372 		return;
2373 
2374 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, false);
2375 	if (!cqp_request)
2376 		return;
2377 
2378 	cqp_info = &cqp_request->info;
2379 	if (action == IRDMA_ARP_ADD) {
2380 		cqp_info->cqp_cmd = IRDMA_OP_ADD_ARP_CACHE_ENTRY;
2381 		info = &cqp_info->in.u.add_arp_cache_entry.info;
2382 		memset(info, 0, sizeof(*info));
2383 		info->arp_index = (u16)arp_index;
2384 		info->permanent = true;
2385 		ether_addr_copy(info->mac_addr, mac_addr);
2386 		cqp_info->in.u.add_arp_cache_entry.scratch =
2387 			(uintptr_t)cqp_request;
2388 		cqp_info->in.u.add_arp_cache_entry.cqp = &rf->cqp.sc_cqp;
2389 	} else {
2390 		cqp_info->cqp_cmd = IRDMA_OP_DELETE_ARP_CACHE_ENTRY;
2391 		cqp_info->in.u.del_arp_cache_entry.scratch =
2392 			(uintptr_t)cqp_request;
2393 		cqp_info->in.u.del_arp_cache_entry.cqp = &rf->cqp.sc_cqp;
2394 		cqp_info->in.u.del_arp_cache_entry.arp_index = arp_index;
2395 	}
2396 
2397 	cqp_info->post_sq = 1;
2398 	irdma_handle_cqp_op(rf, cqp_request);
2399 	irdma_put_cqp_request(&rf->cqp, cqp_request);
2400 }
2401 
2402 /**
2403  * irdma_send_syn_cqp_callback - do syn/ack after qhash
2404  * @cqp_request: qhash cqp completion
2405  */
2406 static void irdma_send_syn_cqp_callback(struct irdma_cqp_request *cqp_request)
2407 {
2408 	struct irdma_cm_node *cm_node = cqp_request->param;
2409 
2410 	irdma_send_syn(cm_node, 1);
2411 	irdma_rem_ref_cm_node(cm_node);
2412 }
2413 
2414 /**
2415  * irdma_manage_qhash - add or modify qhash
2416  * @iwdev: irdma device
2417  * @cminfo: cm info for qhash
2418  * @etype: type (syn or quad)
2419  * @mtype: type of qhash
2420  * @cmnode: cmnode associated with connection
2421  * @wait: wait for completion
2422  */
2423 int irdma_manage_qhash(struct irdma_device *iwdev, struct irdma_cm_info *cminfo,
2424 		       enum irdma_quad_entry_type etype,
2425 		       enum irdma_quad_hash_manage_type mtype, void *cmnode,
2426 		       bool wait)
2427 {
2428 	struct irdma_qhash_table_info *info;
2429 	struct irdma_cqp *iwcqp = &iwdev->rf->cqp;
2430 	struct irdma_cqp_request *cqp_request;
2431 	struct cqp_cmds_info *cqp_info;
2432 	struct irdma_cm_node *cm_node = cmnode;
2433 	int status;
2434 
2435 	cqp_request = irdma_alloc_and_get_cqp_request(iwcqp, wait);
2436 	if (!cqp_request)
2437 		return -ENOMEM;
2438 
2439 	cqp_info = &cqp_request->info;
2440 	info = &cqp_info->in.u.manage_qhash_table_entry.info;
2441 	memset(info, 0, sizeof(*info));
2442 	info->vsi = &iwdev->vsi;
2443 	info->manage = mtype;
2444 	info->entry_type = etype;
2445 	if (cminfo->vlan_id < VLAN_N_VID) {
2446 		info->vlan_valid = true;
2447 		info->vlan_id = cminfo->vlan_id;
2448 	} else {
2449 		info->vlan_valid = false;
2450 	}
2451 	info->ipv4_valid = cminfo->ipv4;
2452 	info->user_pri = cminfo->user_pri;
2453 	ether_addr_copy(info->mac_addr, iwdev->netdev->dev_addr);
2454 	info->qp_num = cminfo->qh_qpid;
2455 	info->dest_port = cminfo->loc_port;
2456 	info->dest_ip[0] = cminfo->loc_addr[0];
2457 	info->dest_ip[1] = cminfo->loc_addr[1];
2458 	info->dest_ip[2] = cminfo->loc_addr[2];
2459 	info->dest_ip[3] = cminfo->loc_addr[3];
2460 	if (etype == IRDMA_QHASH_TYPE_TCP_ESTABLISHED ||
2461 	    etype == IRDMA_QHASH_TYPE_UDP_UNICAST ||
2462 	    etype == IRDMA_QHASH_TYPE_UDP_MCAST ||
2463 	    etype == IRDMA_QHASH_TYPE_ROCE_MCAST ||
2464 	    etype == IRDMA_QHASH_TYPE_ROCEV2_HW) {
2465 		info->src_port = cminfo->rem_port;
2466 		info->src_ip[0] = cminfo->rem_addr[0];
2467 		info->src_ip[1] = cminfo->rem_addr[1];
2468 		info->src_ip[2] = cminfo->rem_addr[2];
2469 		info->src_ip[3] = cminfo->rem_addr[3];
2470 	}
2471 	if (cmnode) {
2472 		cqp_request->callback_fcn = irdma_send_syn_cqp_callback;
2473 		cqp_request->param = cmnode;
2474 		if (!wait)
2475 			refcount_inc(&cm_node->refcnt);
2476 	}
2477 	if (info->ipv4_valid)
2478 		ibdev_dbg(&iwdev->ibdev,
2479 			  "CM: %s caller: %pS loc_port=0x%04x rem_port=0x%04x loc_addr=%pI4 rem_addr=%pI4 mac=%pM, vlan_id=%d cm_node=%p\n",
2480 			  (!mtype) ? "DELETE" : "ADD",
2481 			  __builtin_return_address(0), info->dest_port,
2482 			  info->src_port, info->dest_ip, info->src_ip,
2483 			  info->mac_addr, cminfo->vlan_id,
2484 			  cmnode ? cmnode : NULL);
2485 	else
2486 		ibdev_dbg(&iwdev->ibdev,
2487 			  "CM: %s caller: %pS loc_port=0x%04x rem_port=0x%04x loc_addr=%pI6 rem_addr=%pI6 mac=%pM, vlan_id=%d cm_node=%p\n",
2488 			  (!mtype) ? "DELETE" : "ADD",
2489 			  __builtin_return_address(0), info->dest_port,
2490 			  info->src_port, info->dest_ip, info->src_ip,
2491 			  info->mac_addr, cminfo->vlan_id,
2492 			  cmnode ? cmnode : NULL);
2493 
2494 	cqp_info->in.u.manage_qhash_table_entry.cqp = &iwdev->rf->cqp.sc_cqp;
2495 	cqp_info->in.u.manage_qhash_table_entry.scratch = (uintptr_t)cqp_request;
2496 	cqp_info->cqp_cmd = IRDMA_OP_MANAGE_QHASH_TABLE_ENTRY;
2497 	cqp_info->post_sq = 1;
2498 	status = irdma_handle_cqp_op(iwdev->rf, cqp_request);
2499 	if (status && cm_node && !wait)
2500 		irdma_rem_ref_cm_node(cm_node);
2501 
2502 	irdma_put_cqp_request(iwcqp, cqp_request);
2503 
2504 	return status;
2505 }
2506 
2507 /**
2508  * irdma_hw_flush_wqes_callback - Check return code after flush
2509  * @cqp_request: qhash cqp completion
2510  */
2511 static void irdma_hw_flush_wqes_callback(struct irdma_cqp_request *cqp_request)
2512 {
2513 	struct irdma_qp_flush_info *hw_info;
2514 	struct irdma_sc_qp *qp;
2515 	struct irdma_qp *iwqp;
2516 	struct cqp_cmds_info *cqp_info;
2517 
2518 	cqp_info = &cqp_request->info;
2519 	hw_info = &cqp_info->in.u.qp_flush_wqes.info;
2520 	qp = cqp_info->in.u.qp_flush_wqes.qp;
2521 	iwqp = qp->qp_uk.back_qp;
2522 
2523 	if (cqp_request->compl_info.maj_err_code)
2524 		return;
2525 
2526 	if (hw_info->rq &&
2527 	    (cqp_request->compl_info.min_err_code == IRDMA_CQP_COMPL_SQ_WQE_FLUSHED ||
2528 	     cqp_request->compl_info.min_err_code == 0)) {
2529 		/* RQ WQE flush was requested but did not happen */
2530 		qp->qp_uk.rq_flush_complete = true;
2531 	}
2532 	if (hw_info->sq &&
2533 	    (cqp_request->compl_info.min_err_code == IRDMA_CQP_COMPL_RQ_WQE_FLUSHED ||
2534 	     cqp_request->compl_info.min_err_code == 0)) {
2535 		if (IRDMA_RING_MORE_WORK(qp->qp_uk.sq_ring)) {
2536 			ibdev_err(&iwqp->iwdev->ibdev, "Flush QP[%d] failed, SQ has more work",
2537 				  qp->qp_uk.qp_id);
2538 			irdma_ib_qp_event(iwqp, IRDMA_QP_EVENT_CATASTROPHIC);
2539 		}
2540 		qp->qp_uk.sq_flush_complete = true;
2541 	}
2542 }
2543 
2544 /**
2545  * irdma_hw_flush_wqes - flush qp's wqe
2546  * @rf: RDMA PCI function
2547  * @qp: hardware control qp
2548  * @info: info for flush
2549  * @wait: flag wait for completion
2550  */
2551 int irdma_hw_flush_wqes(struct irdma_pci_f *rf, struct irdma_sc_qp *qp,
2552 			struct irdma_qp_flush_info *info, bool wait)
2553 {
2554 	int status;
2555 	struct irdma_qp_flush_info *hw_info;
2556 	struct irdma_cqp_request *cqp_request;
2557 	struct cqp_cmds_info *cqp_info;
2558 	struct irdma_qp *iwqp = qp->qp_uk.back_qp;
2559 
2560 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, wait);
2561 	if (!cqp_request)
2562 		return -ENOMEM;
2563 
2564 	cqp_info = &cqp_request->info;
2565 	if (!wait)
2566 		cqp_request->callback_fcn = irdma_hw_flush_wqes_callback;
2567 	hw_info = &cqp_request->info.in.u.qp_flush_wqes.info;
2568 	memcpy(hw_info, info, sizeof(*hw_info));
2569 	cqp_info->cqp_cmd = IRDMA_OP_QP_FLUSH_WQES;
2570 	cqp_info->post_sq = 1;
2571 	cqp_info->in.u.qp_flush_wqes.qp = qp;
2572 	cqp_info->in.u.qp_flush_wqes.scratch = (uintptr_t)cqp_request;
2573 	status = irdma_handle_cqp_op(rf, cqp_request);
2574 	if (status) {
2575 		qp->qp_uk.sq_flush_complete = true;
2576 		qp->qp_uk.rq_flush_complete = true;
2577 		irdma_put_cqp_request(&rf->cqp, cqp_request);
2578 		return status;
2579 	}
2580 
2581 	if (!wait || cqp_request->compl_info.maj_err_code)
2582 		goto put_cqp;
2583 
2584 	if (info->rq) {
2585 		if (cqp_request->compl_info.min_err_code == IRDMA_CQP_COMPL_SQ_WQE_FLUSHED ||
2586 		    cqp_request->compl_info.min_err_code == 0) {
2587 			/* RQ WQE flush was requested but did not happen */
2588 			qp->qp_uk.rq_flush_complete = true;
2589 		}
2590 	}
2591 	if (info->sq) {
2592 		if (cqp_request->compl_info.min_err_code == IRDMA_CQP_COMPL_RQ_WQE_FLUSHED ||
2593 		    cqp_request->compl_info.min_err_code == 0) {
2594 			/*
2595 			 * Handling case where WQE is posted to empty SQ when
2596 			 * flush has not completed
2597 			 */
2598 			if (IRDMA_RING_MORE_WORK(qp->qp_uk.sq_ring)) {
2599 				struct irdma_cqp_request *new_req;
2600 
2601 				if (!qp->qp_uk.sq_flush_complete)
2602 					goto put_cqp;
2603 				qp->qp_uk.sq_flush_complete = false;
2604 				qp->flush_sq = false;
2605 
2606 				info->rq = false;
2607 				info->sq = true;
2608 				new_req = irdma_alloc_and_get_cqp_request(&rf->cqp, true);
2609 				if (!new_req) {
2610 					status = -ENOMEM;
2611 					goto put_cqp;
2612 				}
2613 				cqp_info = &new_req->info;
2614 				hw_info = &new_req->info.in.u.qp_flush_wqes.info;
2615 				memcpy(hw_info, info, sizeof(*hw_info));
2616 				cqp_info->cqp_cmd = IRDMA_OP_QP_FLUSH_WQES;
2617 				cqp_info->post_sq = 1;
2618 				cqp_info->in.u.qp_flush_wqes.qp = qp;
2619 				cqp_info->in.u.qp_flush_wqes.scratch = (uintptr_t)new_req;
2620 
2621 				status = irdma_handle_cqp_op(rf, new_req);
2622 				if (new_req->compl_info.maj_err_code ||
2623 				    new_req->compl_info.min_err_code != IRDMA_CQP_COMPL_SQ_WQE_FLUSHED ||
2624 				    status) {
2625 					ibdev_err(&iwqp->iwdev->ibdev, "fatal QP event: SQ in error but not flushed, qp: %d",
2626 						  iwqp->ibqp.qp_num);
2627 					qp->qp_uk.sq_flush_complete = false;
2628 					irdma_ib_qp_event(iwqp, IRDMA_QP_EVENT_CATASTROPHIC);
2629 				}
2630 				irdma_put_cqp_request(&rf->cqp, new_req);
2631 			} else {
2632 				/* SQ WQE flush was requested but did not happen */
2633 				qp->qp_uk.sq_flush_complete = true;
2634 			}
2635 		} else {
2636 			if (!IRDMA_RING_MORE_WORK(qp->qp_uk.sq_ring))
2637 				qp->qp_uk.sq_flush_complete = true;
2638 		}
2639 	}
2640 
2641 	ibdev_dbg(&rf->iwdev->ibdev,
2642 		  "VERBS: qp_id=%d qp_type=%d qpstate=%d ibqpstate=%d last_aeq=%d hw_iw_state=%d maj_err_code=%d min_err_code=%d\n",
2643 		  iwqp->ibqp.qp_num, rf->protocol_used, iwqp->iwarp_state,
2644 		  iwqp->ibqp_state, iwqp->last_aeq, iwqp->hw_iwarp_state,
2645 		  cqp_request->compl_info.maj_err_code,
2646 		  cqp_request->compl_info.min_err_code);
2647 put_cqp:
2648 	irdma_put_cqp_request(&rf->cqp, cqp_request);
2649 
2650 	return status;
2651 }
2652 
2653 /**
2654  * irdma_gen_ae - generate AE
2655  * @rf: RDMA PCI function
2656  * @qp: qp associated with AE
2657  * @info: info for ae
2658  * @wait: wait for completion
2659  */
2660 void irdma_gen_ae(struct irdma_pci_f *rf, struct irdma_sc_qp *qp,
2661 		  struct irdma_gen_ae_info *info, bool wait)
2662 {
2663 	struct irdma_gen_ae_info *ae_info;
2664 	struct irdma_cqp_request *cqp_request;
2665 	struct cqp_cmds_info *cqp_info;
2666 
2667 	cqp_request = irdma_alloc_and_get_cqp_request(&rf->cqp, wait);
2668 	if (!cqp_request)
2669 		return;
2670 
2671 	cqp_info = &cqp_request->info;
2672 	ae_info = &cqp_request->info.in.u.gen_ae.info;
2673 	memcpy(ae_info, info, sizeof(*ae_info));
2674 	cqp_info->cqp_cmd = IRDMA_OP_GEN_AE;
2675 	cqp_info->post_sq = 1;
2676 	cqp_info->in.u.gen_ae.qp = qp;
2677 	cqp_info->in.u.gen_ae.scratch = (uintptr_t)cqp_request;
2678 
2679 	irdma_handle_cqp_op(rf, cqp_request);
2680 	irdma_put_cqp_request(&rf->cqp, cqp_request);
2681 }
2682 
2683 void irdma_flush_wqes(struct irdma_qp *iwqp, u32 flush_mask)
2684 {
2685 	struct irdma_qp_flush_info info = {};
2686 	struct irdma_pci_f *rf = iwqp->iwdev->rf;
2687 	u8 flush_code = iwqp->sc_qp.flush_code;
2688 
2689 	if (!(flush_mask & IRDMA_FLUSH_SQ) && !(flush_mask & IRDMA_FLUSH_RQ))
2690 		return;
2691 
2692 	/* Set flush info fields*/
2693 	info.sq = flush_mask & IRDMA_FLUSH_SQ;
2694 	info.rq = flush_mask & IRDMA_FLUSH_RQ;
2695 
2696 	if (flush_mask & IRDMA_REFLUSH) {
2697 		if (info.sq)
2698 			iwqp->sc_qp.flush_sq = false;
2699 		if (info.rq)
2700 			iwqp->sc_qp.flush_rq = false;
2701 	}
2702 
2703 	/* Generate userflush errors in CQE */
2704 	info.sq_major_code = IRDMA_FLUSH_MAJOR_ERR;
2705 	info.sq_minor_code = FLUSH_GENERAL_ERR;
2706 	info.rq_major_code = IRDMA_FLUSH_MAJOR_ERR;
2707 	info.rq_minor_code = FLUSH_GENERAL_ERR;
2708 	info.userflushcode = true;
2709 	if (flush_code) {
2710 		if (info.sq && iwqp->sc_qp.sq_flush_code)
2711 			info.sq_minor_code = flush_code;
2712 		if (info.rq && iwqp->sc_qp.rq_flush_code)
2713 			info.rq_minor_code = flush_code;
2714 	}
2715 
2716 	/* Issue flush */
2717 	(void)irdma_hw_flush_wqes(rf, &iwqp->sc_qp, &info,
2718 				  flush_mask & IRDMA_FLUSH_WAIT);
2719 	iwqp->flush_issued = true;
2720 }
2721