1 /*
2  * Copyright (c) 2016-2017 Hisilicon Limited.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/acpi.h>
34 #include <linux/etherdevice.h>
35 #include <linux/interrupt.h>
36 #include <linux/iopoll.h>
37 #include <linux/kernel.h>
38 #include <linux/types.h>
39 #include <net/addrconf.h>
40 #include <rdma/ib_addr.h>
41 #include <rdma/ib_cache.h>
42 #include <rdma/ib_umem.h>
43 #include <rdma/uverbs_ioctl.h>
44 
45 #include "hnae3.h"
46 #include "hns_roce_common.h"
47 #include "hns_roce_device.h"
48 #include "hns_roce_cmd.h"
49 #include "hns_roce_hem.h"
50 #include "hns_roce_hw_v2.h"
51 
52 enum {
53 	CMD_RST_PRC_OTHERS,
54 	CMD_RST_PRC_SUCCESS,
55 	CMD_RST_PRC_EBUSY,
56 };
57 
58 enum ecc_resource_type {
59 	ECC_RESOURCE_QPC,
60 	ECC_RESOURCE_CQC,
61 	ECC_RESOURCE_MPT,
62 	ECC_RESOURCE_SRQC,
63 	ECC_RESOURCE_GMV,
64 	ECC_RESOURCE_QPC_TIMER,
65 	ECC_RESOURCE_CQC_TIMER,
66 	ECC_RESOURCE_SCCC,
67 	ECC_RESOURCE_COUNT,
68 };
69 
70 static const struct {
71 	const char *name;
72 	u8 read_bt0_op;
73 	u8 write_bt0_op;
74 } fmea_ram_res[] = {
75 	{ "ECC_RESOURCE_QPC",
76 	  HNS_ROCE_CMD_READ_QPC_BT0, HNS_ROCE_CMD_WRITE_QPC_BT0 },
77 	{ "ECC_RESOURCE_CQC",
78 	  HNS_ROCE_CMD_READ_CQC_BT0, HNS_ROCE_CMD_WRITE_CQC_BT0 },
79 	{ "ECC_RESOURCE_MPT",
80 	  HNS_ROCE_CMD_READ_MPT_BT0, HNS_ROCE_CMD_WRITE_MPT_BT0 },
81 	{ "ECC_RESOURCE_SRQC",
82 	  HNS_ROCE_CMD_READ_SRQC_BT0, HNS_ROCE_CMD_WRITE_SRQC_BT0 },
83 	/* ECC_RESOURCE_GMV is handled by cmdq, not mailbox */
84 	{ "ECC_RESOURCE_GMV",
85 	  0, 0 },
86 	{ "ECC_RESOURCE_QPC_TIMER",
87 	  HNS_ROCE_CMD_READ_QPC_TIMER_BT0, HNS_ROCE_CMD_WRITE_QPC_TIMER_BT0 },
88 	{ "ECC_RESOURCE_CQC_TIMER",
89 	  HNS_ROCE_CMD_READ_CQC_TIMER_BT0, HNS_ROCE_CMD_WRITE_CQC_TIMER_BT0 },
90 	{ "ECC_RESOURCE_SCCC",
91 	  HNS_ROCE_CMD_READ_SCCC_BT0, HNS_ROCE_CMD_WRITE_SCCC_BT0 },
92 };
93 
94 static inline void set_data_seg_v2(struct hns_roce_v2_wqe_data_seg *dseg,
95 				   struct ib_sge *sg)
96 {
97 	dseg->lkey = cpu_to_le32(sg->lkey);
98 	dseg->addr = cpu_to_le64(sg->addr);
99 	dseg->len  = cpu_to_le32(sg->length);
100 }
101 
102 /*
103  * mapped-value = 1 + real-value
104  * The hns wr opcode real value is start from 0, In order to distinguish between
105  * initialized and uninitialized map values, we plus 1 to the actual value when
106  * defining the mapping, so that the validity can be identified by checking the
107  * mapped value is greater than 0.
108  */
109 #define HR_OPC_MAP(ib_key, hr_key) \
110 		[IB_WR_ ## ib_key] = 1 + HNS_ROCE_V2_WQE_OP_ ## hr_key
111 
112 static const u32 hns_roce_op_code[] = {
113 	HR_OPC_MAP(RDMA_WRITE,			RDMA_WRITE),
114 	HR_OPC_MAP(RDMA_WRITE_WITH_IMM,		RDMA_WRITE_WITH_IMM),
115 	HR_OPC_MAP(SEND,			SEND),
116 	HR_OPC_MAP(SEND_WITH_IMM,		SEND_WITH_IMM),
117 	HR_OPC_MAP(RDMA_READ,			RDMA_READ),
118 	HR_OPC_MAP(ATOMIC_CMP_AND_SWP,		ATOM_CMP_AND_SWAP),
119 	HR_OPC_MAP(ATOMIC_FETCH_AND_ADD,	ATOM_FETCH_AND_ADD),
120 	HR_OPC_MAP(SEND_WITH_INV,		SEND_WITH_INV),
121 	HR_OPC_MAP(MASKED_ATOMIC_CMP_AND_SWP,	ATOM_MSK_CMP_AND_SWAP),
122 	HR_OPC_MAP(MASKED_ATOMIC_FETCH_AND_ADD,	ATOM_MSK_FETCH_AND_ADD),
123 	HR_OPC_MAP(REG_MR,			FAST_REG_PMR),
124 };
125 
126 static u32 to_hr_opcode(u32 ib_opcode)
127 {
128 	if (ib_opcode >= ARRAY_SIZE(hns_roce_op_code))
129 		return HNS_ROCE_V2_WQE_OP_MASK;
130 
131 	return hns_roce_op_code[ib_opcode] ? hns_roce_op_code[ib_opcode] - 1 :
132 					     HNS_ROCE_V2_WQE_OP_MASK;
133 }
134 
135 static void set_frmr_seg(struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
136 			 const struct ib_reg_wr *wr)
137 {
138 	struct hns_roce_wqe_frmr_seg *fseg =
139 		(void *)rc_sq_wqe + sizeof(struct hns_roce_v2_rc_send_wqe);
140 	struct hns_roce_mr *mr = to_hr_mr(wr->mr);
141 	u64 pbl_ba;
142 
143 	/* use ib_access_flags */
144 	hr_reg_write_bool(fseg, FRMR_BIND_EN, wr->access & IB_ACCESS_MW_BIND);
145 	hr_reg_write_bool(fseg, FRMR_ATOMIC,
146 			  wr->access & IB_ACCESS_REMOTE_ATOMIC);
147 	hr_reg_write_bool(fseg, FRMR_RR, wr->access & IB_ACCESS_REMOTE_READ);
148 	hr_reg_write_bool(fseg, FRMR_RW, wr->access & IB_ACCESS_REMOTE_WRITE);
149 	hr_reg_write_bool(fseg, FRMR_LW, wr->access & IB_ACCESS_LOCAL_WRITE);
150 
151 	/* Data structure reuse may lead to confusion */
152 	pbl_ba = mr->pbl_mtr.hem_cfg.root_ba;
153 	rc_sq_wqe->msg_len = cpu_to_le32(lower_32_bits(pbl_ba));
154 	rc_sq_wqe->inv_key = cpu_to_le32(upper_32_bits(pbl_ba));
155 
156 	rc_sq_wqe->byte_16 = cpu_to_le32(wr->mr->length & 0xffffffff);
157 	rc_sq_wqe->byte_20 = cpu_to_le32(wr->mr->length >> 32);
158 	rc_sq_wqe->rkey = cpu_to_le32(wr->key);
159 	rc_sq_wqe->va = cpu_to_le64(wr->mr->iova);
160 
161 	hr_reg_write(fseg, FRMR_PBL_SIZE, mr->npages);
162 	hr_reg_write(fseg, FRMR_PBL_BUF_PG_SZ,
163 		     to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.buf_pg_shift));
164 	hr_reg_clear(fseg, FRMR_BLK_MODE);
165 }
166 
167 static void set_atomic_seg(const struct ib_send_wr *wr,
168 			   struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
169 			   unsigned int valid_num_sge)
170 {
171 	struct hns_roce_v2_wqe_data_seg *dseg =
172 		(void *)rc_sq_wqe + sizeof(struct hns_roce_v2_rc_send_wqe);
173 	struct hns_roce_wqe_atomic_seg *aseg =
174 		(void *)dseg + sizeof(struct hns_roce_v2_wqe_data_seg);
175 
176 	set_data_seg_v2(dseg, wr->sg_list);
177 
178 	if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
179 		aseg->fetchadd_swap_data = cpu_to_le64(atomic_wr(wr)->swap);
180 		aseg->cmp_data = cpu_to_le64(atomic_wr(wr)->compare_add);
181 	} else {
182 		aseg->fetchadd_swap_data =
183 			cpu_to_le64(atomic_wr(wr)->compare_add);
184 		aseg->cmp_data = 0;
185 	}
186 
187 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SGE_NUM, valid_num_sge);
188 }
189 
190 static int fill_ext_sge_inl_data(struct hns_roce_qp *qp,
191 				 const struct ib_send_wr *wr,
192 				 unsigned int *sge_idx, u32 msg_len)
193 {
194 	struct ib_device *ibdev = &(to_hr_dev(qp->ibqp.device))->ib_dev;
195 	unsigned int left_len_in_pg;
196 	unsigned int idx = *sge_idx;
197 	unsigned int i = 0;
198 	unsigned int len;
199 	void *addr;
200 	void *dseg;
201 
202 	if (msg_len > qp->sq.ext_sge_cnt * HNS_ROCE_SGE_SIZE) {
203 		ibdev_err(ibdev,
204 			  "no enough extended sge space for inline data.\n");
205 		return -EINVAL;
206 	}
207 
208 	dseg = hns_roce_get_extend_sge(qp, idx & (qp->sge.sge_cnt - 1));
209 	left_len_in_pg = hr_hw_page_align((uintptr_t)dseg) - (uintptr_t)dseg;
210 	len = wr->sg_list[0].length;
211 	addr = (void *)(unsigned long)(wr->sg_list[0].addr);
212 
213 	/* When copying data to extended sge space, the left length in page may
214 	 * not long enough for current user's sge. So the data should be
215 	 * splited into several parts, one in the first page, and the others in
216 	 * the subsequent pages.
217 	 */
218 	while (1) {
219 		if (len <= left_len_in_pg) {
220 			memcpy(dseg, addr, len);
221 
222 			idx += len / HNS_ROCE_SGE_SIZE;
223 
224 			i++;
225 			if (i >= wr->num_sge)
226 				break;
227 
228 			left_len_in_pg -= len;
229 			len = wr->sg_list[i].length;
230 			addr = (void *)(unsigned long)(wr->sg_list[i].addr);
231 			dseg += len;
232 		} else {
233 			memcpy(dseg, addr, left_len_in_pg);
234 
235 			len -= left_len_in_pg;
236 			addr += left_len_in_pg;
237 			idx += left_len_in_pg / HNS_ROCE_SGE_SIZE;
238 			dseg = hns_roce_get_extend_sge(qp,
239 						idx & (qp->sge.sge_cnt - 1));
240 			left_len_in_pg = 1 << HNS_HW_PAGE_SHIFT;
241 		}
242 	}
243 
244 	*sge_idx = idx;
245 
246 	return 0;
247 }
248 
249 static void set_extend_sge(struct hns_roce_qp *qp, struct ib_sge *sge,
250 			   unsigned int *sge_ind, unsigned int cnt)
251 {
252 	struct hns_roce_v2_wqe_data_seg *dseg;
253 	unsigned int idx = *sge_ind;
254 
255 	while (cnt > 0) {
256 		dseg = hns_roce_get_extend_sge(qp, idx & (qp->sge.sge_cnt - 1));
257 		if (likely(sge->length)) {
258 			set_data_seg_v2(dseg, sge);
259 			idx++;
260 			cnt--;
261 		}
262 		sge++;
263 	}
264 
265 	*sge_ind = idx;
266 }
267 
268 static bool check_inl_data_len(struct hns_roce_qp *qp, unsigned int len)
269 {
270 	struct hns_roce_dev *hr_dev = to_hr_dev(qp->ibqp.device);
271 	int mtu = ib_mtu_enum_to_int(qp->path_mtu);
272 
273 	if (len > qp->max_inline_data || len > mtu) {
274 		ibdev_err(&hr_dev->ib_dev,
275 			  "invalid length of data, data len = %u, max inline len = %u, path mtu = %d.\n",
276 			  len, qp->max_inline_data, mtu);
277 		return false;
278 	}
279 
280 	return true;
281 }
282 
283 static int set_rc_inl(struct hns_roce_qp *qp, const struct ib_send_wr *wr,
284 		      struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
285 		      unsigned int *sge_idx)
286 {
287 	struct hns_roce_dev *hr_dev = to_hr_dev(qp->ibqp.device);
288 	u32 msg_len = le32_to_cpu(rc_sq_wqe->msg_len);
289 	struct ib_device *ibdev = &hr_dev->ib_dev;
290 	unsigned int curr_idx = *sge_idx;
291 	void *dseg = rc_sq_wqe;
292 	unsigned int i;
293 	int ret;
294 
295 	if (unlikely(wr->opcode == IB_WR_RDMA_READ)) {
296 		ibdev_err(ibdev, "invalid inline parameters!\n");
297 		return -EINVAL;
298 	}
299 
300 	if (!check_inl_data_len(qp, msg_len))
301 		return -EINVAL;
302 
303 	dseg += sizeof(struct hns_roce_v2_rc_send_wqe);
304 
305 	if (msg_len <= HNS_ROCE_V2_MAX_RC_INL_INN_SZ) {
306 		hr_reg_clear(rc_sq_wqe, RC_SEND_WQE_INL_TYPE);
307 
308 		for (i = 0; i < wr->num_sge; i++) {
309 			memcpy(dseg, ((void *)wr->sg_list[i].addr),
310 			       wr->sg_list[i].length);
311 			dseg += wr->sg_list[i].length;
312 		}
313 	} else {
314 		hr_reg_enable(rc_sq_wqe, RC_SEND_WQE_INL_TYPE);
315 
316 		ret = fill_ext_sge_inl_data(qp, wr, &curr_idx, msg_len);
317 		if (ret)
318 			return ret;
319 
320 		hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SGE_NUM, curr_idx - *sge_idx);
321 	}
322 
323 	*sge_idx = curr_idx;
324 
325 	return 0;
326 }
327 
328 static int set_rwqe_data_seg(struct ib_qp *ibqp, const struct ib_send_wr *wr,
329 			     struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
330 			     unsigned int *sge_ind,
331 			     unsigned int valid_num_sge)
332 {
333 	struct hns_roce_v2_wqe_data_seg *dseg =
334 		(void *)rc_sq_wqe + sizeof(struct hns_roce_v2_rc_send_wqe);
335 	struct hns_roce_qp *qp = to_hr_qp(ibqp);
336 	int j = 0;
337 	int i;
338 
339 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_MSG_START_SGE_IDX,
340 		     (*sge_ind) & (qp->sge.sge_cnt - 1));
341 
342 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_INLINE,
343 		     !!(wr->send_flags & IB_SEND_INLINE));
344 	if (wr->send_flags & IB_SEND_INLINE)
345 		return set_rc_inl(qp, wr, rc_sq_wqe, sge_ind);
346 
347 	if (valid_num_sge <= HNS_ROCE_SGE_IN_WQE) {
348 		for (i = 0; i < wr->num_sge; i++) {
349 			if (likely(wr->sg_list[i].length)) {
350 				set_data_seg_v2(dseg, wr->sg_list + i);
351 				dseg++;
352 			}
353 		}
354 	} else {
355 		for (i = 0; i < wr->num_sge && j < HNS_ROCE_SGE_IN_WQE; i++) {
356 			if (likely(wr->sg_list[i].length)) {
357 				set_data_seg_v2(dseg, wr->sg_list + i);
358 				dseg++;
359 				j++;
360 			}
361 		}
362 
363 		set_extend_sge(qp, wr->sg_list + i, sge_ind,
364 			       valid_num_sge - HNS_ROCE_SGE_IN_WQE);
365 	}
366 
367 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SGE_NUM, valid_num_sge);
368 
369 	return 0;
370 }
371 
372 static int check_send_valid(struct hns_roce_dev *hr_dev,
373 			    struct hns_roce_qp *hr_qp)
374 {
375 	struct ib_device *ibdev = &hr_dev->ib_dev;
376 	struct ib_qp *ibqp = &hr_qp->ibqp;
377 
378 	if (unlikely(ibqp->qp_type != IB_QPT_RC &&
379 		     ibqp->qp_type != IB_QPT_GSI &&
380 		     ibqp->qp_type != IB_QPT_UD)) {
381 		ibdev_err(ibdev, "not supported QP(0x%x)type!\n",
382 			  ibqp->qp_type);
383 		return -EOPNOTSUPP;
384 	} else if (unlikely(hr_qp->state == IB_QPS_RESET ||
385 		   hr_qp->state == IB_QPS_INIT ||
386 		   hr_qp->state == IB_QPS_RTR)) {
387 		ibdev_err(ibdev, "failed to post WQE, QP state %u!\n",
388 			  hr_qp->state);
389 		return -EINVAL;
390 	} else if (unlikely(hr_dev->state >= HNS_ROCE_DEVICE_STATE_RST_DOWN)) {
391 		ibdev_err(ibdev, "failed to post WQE, dev state %d!\n",
392 			  hr_dev->state);
393 		return -EIO;
394 	}
395 
396 	return 0;
397 }
398 
399 static unsigned int calc_wr_sge_num(const struct ib_send_wr *wr,
400 				    unsigned int *sge_len)
401 {
402 	unsigned int valid_num = 0;
403 	unsigned int len = 0;
404 	int i;
405 
406 	for (i = 0; i < wr->num_sge; i++) {
407 		if (likely(wr->sg_list[i].length)) {
408 			len += wr->sg_list[i].length;
409 			valid_num++;
410 		}
411 	}
412 
413 	*sge_len = len;
414 	return valid_num;
415 }
416 
417 static __le32 get_immtdata(const struct ib_send_wr *wr)
418 {
419 	switch (wr->opcode) {
420 	case IB_WR_SEND_WITH_IMM:
421 	case IB_WR_RDMA_WRITE_WITH_IMM:
422 		return cpu_to_le32(be32_to_cpu(wr->ex.imm_data));
423 	default:
424 		return 0;
425 	}
426 }
427 
428 static int set_ud_opcode(struct hns_roce_v2_ud_send_wqe *ud_sq_wqe,
429 			 const struct ib_send_wr *wr)
430 {
431 	u32 ib_op = wr->opcode;
432 
433 	if (ib_op != IB_WR_SEND && ib_op != IB_WR_SEND_WITH_IMM)
434 		return -EINVAL;
435 
436 	ud_sq_wqe->immtdata = get_immtdata(wr);
437 
438 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_OPCODE, to_hr_opcode(ib_op));
439 
440 	return 0;
441 }
442 
443 static int fill_ud_av(struct hns_roce_v2_ud_send_wqe *ud_sq_wqe,
444 		      struct hns_roce_ah *ah)
445 {
446 	struct ib_device *ib_dev = ah->ibah.device;
447 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
448 
449 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_UDPSPN, ah->av.udp_sport);
450 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_HOPLIMIT, ah->av.hop_limit);
451 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_TCLASS, ah->av.tclass);
452 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_FLOW_LABEL, ah->av.flowlabel);
453 
454 	if (WARN_ON(ah->av.sl > MAX_SERVICE_LEVEL))
455 		return -EINVAL;
456 
457 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_SL, ah->av.sl);
458 
459 	ud_sq_wqe->sgid_index = ah->av.gid_index;
460 
461 	memcpy(ud_sq_wqe->dmac, ah->av.mac, ETH_ALEN);
462 	memcpy(ud_sq_wqe->dgid, ah->av.dgid, GID_LEN_V2);
463 
464 	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
465 		return 0;
466 
467 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_VLAN_EN, ah->av.vlan_en);
468 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_VLAN, ah->av.vlan_id);
469 
470 	return 0;
471 }
472 
473 static inline int set_ud_wqe(struct hns_roce_qp *qp,
474 			     const struct ib_send_wr *wr,
475 			     void *wqe, unsigned int *sge_idx,
476 			     unsigned int owner_bit)
477 {
478 	struct hns_roce_ah *ah = to_hr_ah(ud_wr(wr)->ah);
479 	struct hns_roce_v2_ud_send_wqe *ud_sq_wqe = wqe;
480 	unsigned int curr_idx = *sge_idx;
481 	unsigned int valid_num_sge;
482 	u32 msg_len = 0;
483 	int ret;
484 
485 	valid_num_sge = calc_wr_sge_num(wr, &msg_len);
486 
487 	ret = set_ud_opcode(ud_sq_wqe, wr);
488 	if (WARN_ON(ret))
489 		return ret;
490 
491 	ud_sq_wqe->msg_len = cpu_to_le32(msg_len);
492 
493 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_CQE,
494 		     !!(wr->send_flags & IB_SEND_SIGNALED));
495 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_SE,
496 		     !!(wr->send_flags & IB_SEND_SOLICITED));
497 
498 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_PD, to_hr_pd(qp->ibqp.pd)->pdn);
499 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_SGE_NUM, valid_num_sge);
500 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_MSG_START_SGE_IDX,
501 		     curr_idx & (qp->sge.sge_cnt - 1));
502 
503 	ud_sq_wqe->qkey = cpu_to_le32(ud_wr(wr)->remote_qkey & 0x80000000 ?
504 			  qp->qkey : ud_wr(wr)->remote_qkey);
505 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_DQPN, ud_wr(wr)->remote_qpn);
506 
507 	ret = fill_ud_av(ud_sq_wqe, ah);
508 	if (ret)
509 		return ret;
510 
511 	qp->sl = to_hr_ah(ud_wr(wr)->ah)->av.sl;
512 
513 	set_extend_sge(qp, wr->sg_list, &curr_idx, valid_num_sge);
514 
515 	/*
516 	 * The pipeline can sequentially post all valid WQEs into WQ buffer,
517 	 * including new WQEs waiting for the doorbell to update the PI again.
518 	 * Therefore, the owner bit of WQE MUST be updated after all fields
519 	 * and extSGEs have been written into DDR instead of cache.
520 	 */
521 	if (qp->en_flags & HNS_ROCE_QP_CAP_OWNER_DB)
522 		dma_wmb();
523 
524 	*sge_idx = curr_idx;
525 	hr_reg_write(ud_sq_wqe, UD_SEND_WQE_OWNER, owner_bit);
526 
527 	return 0;
528 }
529 
530 static int set_rc_opcode(struct hns_roce_dev *hr_dev,
531 			 struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
532 			 const struct ib_send_wr *wr)
533 {
534 	u32 ib_op = wr->opcode;
535 	int ret = 0;
536 
537 	rc_sq_wqe->immtdata = get_immtdata(wr);
538 
539 	switch (ib_op) {
540 	case IB_WR_RDMA_READ:
541 	case IB_WR_RDMA_WRITE:
542 	case IB_WR_RDMA_WRITE_WITH_IMM:
543 		rc_sq_wqe->rkey = cpu_to_le32(rdma_wr(wr)->rkey);
544 		rc_sq_wqe->va = cpu_to_le64(rdma_wr(wr)->remote_addr);
545 		break;
546 	case IB_WR_SEND:
547 	case IB_WR_SEND_WITH_IMM:
548 		break;
549 	case IB_WR_ATOMIC_CMP_AND_SWP:
550 	case IB_WR_ATOMIC_FETCH_AND_ADD:
551 		rc_sq_wqe->rkey = cpu_to_le32(atomic_wr(wr)->rkey);
552 		rc_sq_wqe->va = cpu_to_le64(atomic_wr(wr)->remote_addr);
553 		break;
554 	case IB_WR_REG_MR:
555 		if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
556 			set_frmr_seg(rc_sq_wqe, reg_wr(wr));
557 		else
558 			ret = -EOPNOTSUPP;
559 		break;
560 	case IB_WR_SEND_WITH_INV:
561 		rc_sq_wqe->inv_key = cpu_to_le32(wr->ex.invalidate_rkey);
562 		break;
563 	default:
564 		ret = -EINVAL;
565 	}
566 
567 	if (unlikely(ret))
568 		return ret;
569 
570 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_OPCODE, to_hr_opcode(ib_op));
571 
572 	return ret;
573 }
574 
575 static inline int set_rc_wqe(struct hns_roce_qp *qp,
576 			     const struct ib_send_wr *wr,
577 			     void *wqe, unsigned int *sge_idx,
578 			     unsigned int owner_bit)
579 {
580 	struct hns_roce_dev *hr_dev = to_hr_dev(qp->ibqp.device);
581 	struct hns_roce_v2_rc_send_wqe *rc_sq_wqe = wqe;
582 	unsigned int curr_idx = *sge_idx;
583 	unsigned int valid_num_sge;
584 	u32 msg_len = 0;
585 	int ret;
586 
587 	valid_num_sge = calc_wr_sge_num(wr, &msg_len);
588 
589 	rc_sq_wqe->msg_len = cpu_to_le32(msg_len);
590 
591 	ret = set_rc_opcode(hr_dev, rc_sq_wqe, wr);
592 	if (WARN_ON(ret))
593 		return ret;
594 
595 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_FENCE,
596 		     (wr->send_flags & IB_SEND_FENCE) ? 1 : 0);
597 
598 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SE,
599 		     (wr->send_flags & IB_SEND_SOLICITED) ? 1 : 0);
600 
601 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_CQE,
602 		     (wr->send_flags & IB_SEND_SIGNALED) ? 1 : 0);
603 
604 	if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
605 	    wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
606 		set_atomic_seg(wr, rc_sq_wqe, valid_num_sge);
607 	else if (wr->opcode != IB_WR_REG_MR)
608 		ret = set_rwqe_data_seg(&qp->ibqp, wr, rc_sq_wqe,
609 					&curr_idx, valid_num_sge);
610 
611 	/*
612 	 * The pipeline can sequentially post all valid WQEs into WQ buffer,
613 	 * including new WQEs waiting for the doorbell to update the PI again.
614 	 * Therefore, the owner bit of WQE MUST be updated after all fields
615 	 * and extSGEs have been written into DDR instead of cache.
616 	 */
617 	if (qp->en_flags & HNS_ROCE_QP_CAP_OWNER_DB)
618 		dma_wmb();
619 
620 	*sge_idx = curr_idx;
621 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_OWNER, owner_bit);
622 
623 	return ret;
624 }
625 
626 static inline void update_sq_db(struct hns_roce_dev *hr_dev,
627 				struct hns_roce_qp *qp)
628 {
629 	if (unlikely(qp->state == IB_QPS_ERR)) {
630 		flush_cqe(hr_dev, qp);
631 	} else {
632 		struct hns_roce_v2_db sq_db = {};
633 
634 		hr_reg_write(&sq_db, DB_TAG, qp->qpn);
635 		hr_reg_write(&sq_db, DB_CMD, HNS_ROCE_V2_SQ_DB);
636 		hr_reg_write(&sq_db, DB_PI, qp->sq.head);
637 		hr_reg_write(&sq_db, DB_SL, qp->sl);
638 
639 		hns_roce_write64(hr_dev, (__le32 *)&sq_db, qp->sq.db_reg);
640 	}
641 }
642 
643 static inline void update_rq_db(struct hns_roce_dev *hr_dev,
644 				struct hns_roce_qp *qp)
645 {
646 	if (unlikely(qp->state == IB_QPS_ERR)) {
647 		flush_cqe(hr_dev, qp);
648 	} else {
649 		if (likely(qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)) {
650 			*qp->rdb.db_record =
651 					qp->rq.head & V2_DB_PRODUCER_IDX_M;
652 		} else {
653 			struct hns_roce_v2_db rq_db = {};
654 
655 			hr_reg_write(&rq_db, DB_TAG, qp->qpn);
656 			hr_reg_write(&rq_db, DB_CMD, HNS_ROCE_V2_RQ_DB);
657 			hr_reg_write(&rq_db, DB_PI, qp->rq.head);
658 
659 			hns_roce_write64(hr_dev, (__le32 *)&rq_db,
660 					 qp->rq.db_reg);
661 		}
662 	}
663 }
664 
665 static void hns_roce_write512(struct hns_roce_dev *hr_dev, u64 *val,
666 			      u64 __iomem *dest)
667 {
668 #define HNS_ROCE_WRITE_TIMES 8
669 	struct hns_roce_v2_priv *priv = (struct hns_roce_v2_priv *)hr_dev->priv;
670 	struct hnae3_handle *handle = priv->handle;
671 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
672 	int i;
673 
674 	if (!hr_dev->dis_db && !ops->get_hw_reset_stat(handle))
675 		for (i = 0; i < HNS_ROCE_WRITE_TIMES; i++)
676 			writeq_relaxed(*(val + i), dest + i);
677 }
678 
679 static void write_dwqe(struct hns_roce_dev *hr_dev, struct hns_roce_qp *qp,
680 		       void *wqe)
681 {
682 #define HNS_ROCE_SL_SHIFT 2
683 	struct hns_roce_v2_rc_send_wqe *rc_sq_wqe = wqe;
684 
685 	/* All kinds of DirectWQE have the same header field layout */
686 	hr_reg_enable(rc_sq_wqe, RC_SEND_WQE_FLAG);
687 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_DB_SL_L, qp->sl);
688 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_DB_SL_H,
689 		     qp->sl >> HNS_ROCE_SL_SHIFT);
690 	hr_reg_write(rc_sq_wqe, RC_SEND_WQE_WQE_INDEX, qp->sq.head);
691 
692 	hns_roce_write512(hr_dev, wqe, qp->sq.db_reg);
693 }
694 
695 static int hns_roce_v2_post_send(struct ib_qp *ibqp,
696 				 const struct ib_send_wr *wr,
697 				 const struct ib_send_wr **bad_wr)
698 {
699 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
700 	struct ib_device *ibdev = &hr_dev->ib_dev;
701 	struct hns_roce_qp *qp = to_hr_qp(ibqp);
702 	unsigned long flags = 0;
703 	unsigned int owner_bit;
704 	unsigned int sge_idx;
705 	unsigned int wqe_idx;
706 	void *wqe = NULL;
707 	u32 nreq;
708 	int ret;
709 
710 	spin_lock_irqsave(&qp->sq.lock, flags);
711 
712 	ret = check_send_valid(hr_dev, qp);
713 	if (unlikely(ret)) {
714 		*bad_wr = wr;
715 		nreq = 0;
716 		goto out;
717 	}
718 
719 	sge_idx = qp->next_sge;
720 
721 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
722 		if (hns_roce_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
723 			ret = -ENOMEM;
724 			*bad_wr = wr;
725 			goto out;
726 		}
727 
728 		wqe_idx = (qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1);
729 
730 		if (unlikely(wr->num_sge > qp->sq.max_gs)) {
731 			ibdev_err(ibdev, "num_sge = %d > qp->sq.max_gs = %u.\n",
732 				  wr->num_sge, qp->sq.max_gs);
733 			ret = -EINVAL;
734 			*bad_wr = wr;
735 			goto out;
736 		}
737 
738 		wqe = hns_roce_get_send_wqe(qp, wqe_idx);
739 		qp->sq.wrid[wqe_idx] = wr->wr_id;
740 		owner_bit =
741 		       ~(((qp->sq.head + nreq) >> ilog2(qp->sq.wqe_cnt)) & 0x1);
742 
743 		/* Corresponding to the QP type, wqe process separately */
744 		if (ibqp->qp_type == IB_QPT_RC)
745 			ret = set_rc_wqe(qp, wr, wqe, &sge_idx, owner_bit);
746 		else
747 			ret = set_ud_wqe(qp, wr, wqe, &sge_idx, owner_bit);
748 
749 		if (unlikely(ret)) {
750 			*bad_wr = wr;
751 			goto out;
752 		}
753 	}
754 
755 out:
756 	if (likely(nreq)) {
757 		qp->sq.head += nreq;
758 		qp->next_sge = sge_idx;
759 
760 		if (nreq == 1 && (qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE))
761 			write_dwqe(hr_dev, qp, wqe);
762 		else
763 			update_sq_db(hr_dev, qp);
764 	}
765 
766 	spin_unlock_irqrestore(&qp->sq.lock, flags);
767 
768 	return ret;
769 }
770 
771 static int check_recv_valid(struct hns_roce_dev *hr_dev,
772 			    struct hns_roce_qp *hr_qp)
773 {
774 	struct ib_device *ibdev = &hr_dev->ib_dev;
775 	struct ib_qp *ibqp = &hr_qp->ibqp;
776 
777 	if (unlikely(ibqp->qp_type != IB_QPT_RC &&
778 		     ibqp->qp_type != IB_QPT_GSI &&
779 		     ibqp->qp_type != IB_QPT_UD)) {
780 		ibdev_err(ibdev, "unsupported qp type, qp_type = %d.\n",
781 			  ibqp->qp_type);
782 		return -EOPNOTSUPP;
783 	}
784 
785 	if (unlikely(hr_dev->state >= HNS_ROCE_DEVICE_STATE_RST_DOWN))
786 		return -EIO;
787 
788 	if (hr_qp->state == IB_QPS_RESET)
789 		return -EINVAL;
790 
791 	return 0;
792 }
793 
794 static void fill_recv_sge_to_wqe(const struct ib_recv_wr *wr, void *wqe,
795 				 u32 max_sge, bool rsv)
796 {
797 	struct hns_roce_v2_wqe_data_seg *dseg = wqe;
798 	u32 i, cnt;
799 
800 	for (i = 0, cnt = 0; i < wr->num_sge; i++) {
801 		/* Skip zero-length sge */
802 		if (!wr->sg_list[i].length)
803 			continue;
804 		set_data_seg_v2(dseg + cnt, wr->sg_list + i);
805 		cnt++;
806 	}
807 
808 	/* Fill a reserved sge to make hw stop reading remaining segments */
809 	if (rsv) {
810 		dseg[cnt].lkey = cpu_to_le32(HNS_ROCE_INVALID_LKEY);
811 		dseg[cnt].addr = 0;
812 		dseg[cnt].len = cpu_to_le32(HNS_ROCE_INVALID_SGE_LENGTH);
813 	} else {
814 		/* Clear remaining segments to make ROCEE ignore sges */
815 		if (cnt < max_sge)
816 			memset(dseg + cnt, 0,
817 			       (max_sge - cnt) * HNS_ROCE_SGE_SIZE);
818 	}
819 }
820 
821 static void fill_rq_wqe(struct hns_roce_qp *hr_qp, const struct ib_recv_wr *wr,
822 			u32 wqe_idx, u32 max_sge)
823 {
824 	void *wqe = NULL;
825 
826 	wqe = hns_roce_get_recv_wqe(hr_qp, wqe_idx);
827 	fill_recv_sge_to_wqe(wr, wqe, max_sge, hr_qp->rq.rsv_sge);
828 }
829 
830 static int hns_roce_v2_post_recv(struct ib_qp *ibqp,
831 				 const struct ib_recv_wr *wr,
832 				 const struct ib_recv_wr **bad_wr)
833 {
834 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
835 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
836 	struct ib_device *ibdev = &hr_dev->ib_dev;
837 	u32 wqe_idx, nreq, max_sge;
838 	unsigned long flags;
839 	int ret;
840 
841 	spin_lock_irqsave(&hr_qp->rq.lock, flags);
842 
843 	ret = check_recv_valid(hr_dev, hr_qp);
844 	if (unlikely(ret)) {
845 		*bad_wr = wr;
846 		nreq = 0;
847 		goto out;
848 	}
849 
850 	max_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
851 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
852 		if (unlikely(hns_roce_wq_overflow(&hr_qp->rq, nreq,
853 						  hr_qp->ibqp.recv_cq))) {
854 			ret = -ENOMEM;
855 			*bad_wr = wr;
856 			goto out;
857 		}
858 
859 		if (unlikely(wr->num_sge > max_sge)) {
860 			ibdev_err(ibdev, "num_sge = %d >= max_sge = %u.\n",
861 				  wr->num_sge, max_sge);
862 			ret = -EINVAL;
863 			*bad_wr = wr;
864 			goto out;
865 		}
866 
867 		wqe_idx = (hr_qp->rq.head + nreq) & (hr_qp->rq.wqe_cnt - 1);
868 		fill_rq_wqe(hr_qp, wr, wqe_idx, max_sge);
869 		hr_qp->rq.wrid[wqe_idx] = wr->wr_id;
870 	}
871 
872 out:
873 	if (likely(nreq)) {
874 		hr_qp->rq.head += nreq;
875 
876 		update_rq_db(hr_dev, hr_qp);
877 	}
878 	spin_unlock_irqrestore(&hr_qp->rq.lock, flags);
879 
880 	return ret;
881 }
882 
883 static void *get_srq_wqe_buf(struct hns_roce_srq *srq, u32 n)
884 {
885 	return hns_roce_buf_offset(srq->buf_mtr.kmem, n << srq->wqe_shift);
886 }
887 
888 static void *get_idx_buf(struct hns_roce_idx_que *idx_que, u32 n)
889 {
890 	return hns_roce_buf_offset(idx_que->mtr.kmem,
891 				   n << idx_que->entry_shift);
892 }
893 
894 static void hns_roce_free_srq_wqe(struct hns_roce_srq *srq, u32 wqe_index)
895 {
896 	/* always called with interrupts disabled. */
897 	spin_lock(&srq->lock);
898 
899 	bitmap_clear(srq->idx_que.bitmap, wqe_index, 1);
900 	srq->idx_que.tail++;
901 
902 	spin_unlock(&srq->lock);
903 }
904 
905 static int hns_roce_srqwq_overflow(struct hns_roce_srq *srq)
906 {
907 	struct hns_roce_idx_que *idx_que = &srq->idx_que;
908 
909 	return idx_que->head - idx_que->tail >= srq->wqe_cnt;
910 }
911 
912 static int check_post_srq_valid(struct hns_roce_srq *srq, u32 max_sge,
913 				const struct ib_recv_wr *wr)
914 {
915 	struct ib_device *ib_dev = srq->ibsrq.device;
916 
917 	if (unlikely(wr->num_sge > max_sge)) {
918 		ibdev_err(ib_dev,
919 			  "failed to check sge, wr->num_sge = %d, max_sge = %u.\n",
920 			  wr->num_sge, max_sge);
921 		return -EINVAL;
922 	}
923 
924 	if (unlikely(hns_roce_srqwq_overflow(srq))) {
925 		ibdev_err(ib_dev,
926 			  "failed to check srqwq status, srqwq is full.\n");
927 		return -ENOMEM;
928 	}
929 
930 	return 0;
931 }
932 
933 static int get_srq_wqe_idx(struct hns_roce_srq *srq, u32 *wqe_idx)
934 {
935 	struct hns_roce_idx_que *idx_que = &srq->idx_que;
936 	u32 pos;
937 
938 	pos = find_first_zero_bit(idx_que->bitmap, srq->wqe_cnt);
939 	if (unlikely(pos == srq->wqe_cnt))
940 		return -ENOSPC;
941 
942 	bitmap_set(idx_que->bitmap, pos, 1);
943 	*wqe_idx = pos;
944 	return 0;
945 }
946 
947 static void fill_wqe_idx(struct hns_roce_srq *srq, unsigned int wqe_idx)
948 {
949 	struct hns_roce_idx_que *idx_que = &srq->idx_que;
950 	unsigned int head;
951 	__le32 *buf;
952 
953 	head = idx_que->head & (srq->wqe_cnt - 1);
954 
955 	buf = get_idx_buf(idx_que, head);
956 	*buf = cpu_to_le32(wqe_idx);
957 
958 	idx_que->head++;
959 }
960 
961 static void update_srq_db(struct hns_roce_v2_db *db, struct hns_roce_srq *srq)
962 {
963 	hr_reg_write(db, DB_TAG, srq->srqn);
964 	hr_reg_write(db, DB_CMD, HNS_ROCE_V2_SRQ_DB);
965 	hr_reg_write(db, DB_PI, srq->idx_que.head);
966 }
967 
968 static int hns_roce_v2_post_srq_recv(struct ib_srq *ibsrq,
969 				     const struct ib_recv_wr *wr,
970 				     const struct ib_recv_wr **bad_wr)
971 {
972 	struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device);
973 	struct hns_roce_srq *srq = to_hr_srq(ibsrq);
974 	struct hns_roce_v2_db srq_db;
975 	unsigned long flags;
976 	int ret = 0;
977 	u32 max_sge;
978 	u32 wqe_idx;
979 	void *wqe;
980 	u32 nreq;
981 
982 	spin_lock_irqsave(&srq->lock, flags);
983 
984 	max_sge = srq->max_gs - srq->rsv_sge;
985 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
986 		ret = check_post_srq_valid(srq, max_sge, wr);
987 		if (ret) {
988 			*bad_wr = wr;
989 			break;
990 		}
991 
992 		ret = get_srq_wqe_idx(srq, &wqe_idx);
993 		if (unlikely(ret)) {
994 			*bad_wr = wr;
995 			break;
996 		}
997 
998 		wqe = get_srq_wqe_buf(srq, wqe_idx);
999 		fill_recv_sge_to_wqe(wr, wqe, max_sge, srq->rsv_sge);
1000 		fill_wqe_idx(srq, wqe_idx);
1001 		srq->wrid[wqe_idx] = wr->wr_id;
1002 	}
1003 
1004 	if (likely(nreq)) {
1005 		update_srq_db(&srq_db, srq);
1006 
1007 		hns_roce_write64(hr_dev, (__le32 *)&srq_db, srq->db_reg);
1008 	}
1009 
1010 	spin_unlock_irqrestore(&srq->lock, flags);
1011 
1012 	return ret;
1013 }
1014 
1015 static u32 hns_roce_v2_cmd_hw_reseted(struct hns_roce_dev *hr_dev,
1016 				      unsigned long instance_stage,
1017 				      unsigned long reset_stage)
1018 {
1019 	/* When hardware reset has been completed once or more, we should stop
1020 	 * sending mailbox&cmq&doorbell to hardware. If now in .init_instance()
1021 	 * function, we should exit with error. If now at HNAE3_INIT_CLIENT
1022 	 * stage of soft reset process, we should exit with error, and then
1023 	 * HNAE3_INIT_CLIENT related process can rollback the operation like
1024 	 * notifing hardware to free resources, HNAE3_INIT_CLIENT related
1025 	 * process will exit with error to notify NIC driver to reschedule soft
1026 	 * reset process once again.
1027 	 */
1028 	hr_dev->is_reset = true;
1029 	hr_dev->dis_db = true;
1030 
1031 	if (reset_stage == HNS_ROCE_STATE_RST_INIT ||
1032 	    instance_stage == HNS_ROCE_STATE_INIT)
1033 		return CMD_RST_PRC_EBUSY;
1034 
1035 	return CMD_RST_PRC_SUCCESS;
1036 }
1037 
1038 static u32 hns_roce_v2_cmd_hw_resetting(struct hns_roce_dev *hr_dev,
1039 					unsigned long instance_stage,
1040 					unsigned long reset_stage)
1041 {
1042 #define HW_RESET_TIMEOUT_US 1000000
1043 #define HW_RESET_SLEEP_US 1000
1044 
1045 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1046 	struct hnae3_handle *handle = priv->handle;
1047 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1048 	unsigned long val;
1049 	int ret;
1050 
1051 	/* When hardware reset is detected, we should stop sending mailbox&cmq&
1052 	 * doorbell to hardware. If now in .init_instance() function, we should
1053 	 * exit with error. If now at HNAE3_INIT_CLIENT stage of soft reset
1054 	 * process, we should exit with error, and then HNAE3_INIT_CLIENT
1055 	 * related process can rollback the operation like notifing hardware to
1056 	 * free resources, HNAE3_INIT_CLIENT related process will exit with
1057 	 * error to notify NIC driver to reschedule soft reset process once
1058 	 * again.
1059 	 */
1060 	hr_dev->dis_db = true;
1061 
1062 	ret = read_poll_timeout(ops->ae_dev_reset_cnt, val,
1063 				val > hr_dev->reset_cnt, HW_RESET_SLEEP_US,
1064 				HW_RESET_TIMEOUT_US, false, handle);
1065 	if (!ret)
1066 		hr_dev->is_reset = true;
1067 
1068 	if (!hr_dev->is_reset || reset_stage == HNS_ROCE_STATE_RST_INIT ||
1069 	    instance_stage == HNS_ROCE_STATE_INIT)
1070 		return CMD_RST_PRC_EBUSY;
1071 
1072 	return CMD_RST_PRC_SUCCESS;
1073 }
1074 
1075 static u32 hns_roce_v2_cmd_sw_resetting(struct hns_roce_dev *hr_dev)
1076 {
1077 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1078 	struct hnae3_handle *handle = priv->handle;
1079 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1080 
1081 	/* When software reset is detected at .init_instance() function, we
1082 	 * should stop sending mailbox&cmq&doorbell to hardware, and exit
1083 	 * with error.
1084 	 */
1085 	hr_dev->dis_db = true;
1086 	if (ops->ae_dev_reset_cnt(handle) != hr_dev->reset_cnt)
1087 		hr_dev->is_reset = true;
1088 
1089 	return CMD_RST_PRC_EBUSY;
1090 }
1091 
1092 static u32 check_aedev_reset_status(struct hns_roce_dev *hr_dev,
1093 				    struct hnae3_handle *handle)
1094 {
1095 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1096 	unsigned long instance_stage; /* the current instance stage */
1097 	unsigned long reset_stage; /* the current reset stage */
1098 	unsigned long reset_cnt;
1099 	bool sw_resetting;
1100 	bool hw_resetting;
1101 
1102 	/* Get information about reset from NIC driver or RoCE driver itself,
1103 	 * the meaning of the following variables from NIC driver are described
1104 	 * as below:
1105 	 * reset_cnt -- The count value of completed hardware reset.
1106 	 * hw_resetting -- Whether hardware device is resetting now.
1107 	 * sw_resetting -- Whether NIC's software reset process is running now.
1108 	 */
1109 	instance_stage = handle->rinfo.instance_state;
1110 	reset_stage = handle->rinfo.reset_state;
1111 	reset_cnt = ops->ae_dev_reset_cnt(handle);
1112 	if (reset_cnt != hr_dev->reset_cnt)
1113 		return hns_roce_v2_cmd_hw_reseted(hr_dev, instance_stage,
1114 						  reset_stage);
1115 
1116 	hw_resetting = ops->get_cmdq_stat(handle);
1117 	if (hw_resetting)
1118 		return hns_roce_v2_cmd_hw_resetting(hr_dev, instance_stage,
1119 						    reset_stage);
1120 
1121 	sw_resetting = ops->ae_dev_resetting(handle);
1122 	if (sw_resetting && instance_stage == HNS_ROCE_STATE_INIT)
1123 		return hns_roce_v2_cmd_sw_resetting(hr_dev);
1124 
1125 	return CMD_RST_PRC_OTHERS;
1126 }
1127 
1128 static bool check_device_is_in_reset(struct hns_roce_dev *hr_dev)
1129 {
1130 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1131 	struct hnae3_handle *handle = priv->handle;
1132 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1133 
1134 	if (hr_dev->reset_cnt != ops->ae_dev_reset_cnt(handle))
1135 		return true;
1136 
1137 	if (ops->get_hw_reset_stat(handle))
1138 		return true;
1139 
1140 	if (ops->ae_dev_resetting(handle))
1141 		return true;
1142 
1143 	return false;
1144 }
1145 
1146 static bool v2_chk_mbox_is_avail(struct hns_roce_dev *hr_dev, bool *busy)
1147 {
1148 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1149 	u32 status;
1150 
1151 	if (hr_dev->is_reset)
1152 		status = CMD_RST_PRC_SUCCESS;
1153 	else
1154 		status = check_aedev_reset_status(hr_dev, priv->handle);
1155 
1156 	*busy = (status == CMD_RST_PRC_EBUSY);
1157 
1158 	return status == CMD_RST_PRC_OTHERS;
1159 }
1160 
1161 static int hns_roce_alloc_cmq_desc(struct hns_roce_dev *hr_dev,
1162 				   struct hns_roce_v2_cmq_ring *ring)
1163 {
1164 	int size = ring->desc_num * sizeof(struct hns_roce_cmq_desc);
1165 
1166 	ring->desc = dma_alloc_coherent(hr_dev->dev, size,
1167 					&ring->desc_dma_addr, GFP_KERNEL);
1168 	if (!ring->desc)
1169 		return -ENOMEM;
1170 
1171 	return 0;
1172 }
1173 
1174 static void hns_roce_free_cmq_desc(struct hns_roce_dev *hr_dev,
1175 				   struct hns_roce_v2_cmq_ring *ring)
1176 {
1177 	dma_free_coherent(hr_dev->dev,
1178 			  ring->desc_num * sizeof(struct hns_roce_cmq_desc),
1179 			  ring->desc, ring->desc_dma_addr);
1180 
1181 	ring->desc_dma_addr = 0;
1182 }
1183 
1184 static int init_csq(struct hns_roce_dev *hr_dev,
1185 		    struct hns_roce_v2_cmq_ring *csq)
1186 {
1187 	dma_addr_t dma;
1188 	int ret;
1189 
1190 	csq->desc_num = CMD_CSQ_DESC_NUM;
1191 	spin_lock_init(&csq->lock);
1192 	csq->flag = TYPE_CSQ;
1193 	csq->head = 0;
1194 
1195 	ret = hns_roce_alloc_cmq_desc(hr_dev, csq);
1196 	if (ret)
1197 		return ret;
1198 
1199 	dma = csq->desc_dma_addr;
1200 	roce_write(hr_dev, ROCEE_TX_CMQ_BASEADDR_L_REG, lower_32_bits(dma));
1201 	roce_write(hr_dev, ROCEE_TX_CMQ_BASEADDR_H_REG, upper_32_bits(dma));
1202 	roce_write(hr_dev, ROCEE_TX_CMQ_DEPTH_REG,
1203 		   (u32)csq->desc_num >> HNS_ROCE_CMQ_DESC_NUM_S);
1204 
1205 	/* Make sure to write CI first and then PI */
1206 	roce_write(hr_dev, ROCEE_TX_CMQ_CI_REG, 0);
1207 	roce_write(hr_dev, ROCEE_TX_CMQ_PI_REG, 0);
1208 
1209 	return 0;
1210 }
1211 
1212 static int hns_roce_v2_cmq_init(struct hns_roce_dev *hr_dev)
1213 {
1214 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1215 	int ret;
1216 
1217 	priv->cmq.tx_timeout = HNS_ROCE_CMQ_TX_TIMEOUT;
1218 
1219 	ret = init_csq(hr_dev, &priv->cmq.csq);
1220 	if (ret)
1221 		dev_err(hr_dev->dev, "failed to init CSQ, ret = %d.\n", ret);
1222 
1223 	return ret;
1224 }
1225 
1226 static void hns_roce_v2_cmq_exit(struct hns_roce_dev *hr_dev)
1227 {
1228 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1229 
1230 	hns_roce_free_cmq_desc(hr_dev, &priv->cmq.csq);
1231 }
1232 
1233 static void hns_roce_cmq_setup_basic_desc(struct hns_roce_cmq_desc *desc,
1234 					  enum hns_roce_opcode_type opcode,
1235 					  bool is_read)
1236 {
1237 	memset((void *)desc, 0, sizeof(struct hns_roce_cmq_desc));
1238 	desc->opcode = cpu_to_le16(opcode);
1239 	desc->flag = cpu_to_le16(HNS_ROCE_CMD_FLAG_IN);
1240 	if (is_read)
1241 		desc->flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_WR);
1242 	else
1243 		desc->flag &= cpu_to_le16(~HNS_ROCE_CMD_FLAG_WR);
1244 }
1245 
1246 static int hns_roce_cmq_csq_done(struct hns_roce_dev *hr_dev)
1247 {
1248 	u32 tail = roce_read(hr_dev, ROCEE_TX_CMQ_CI_REG);
1249 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1250 
1251 	return tail == priv->cmq.csq.head;
1252 }
1253 
1254 static void update_cmdq_status(struct hns_roce_dev *hr_dev)
1255 {
1256 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1257 	struct hnae3_handle *handle = priv->handle;
1258 
1259 	if (handle->rinfo.reset_state == HNS_ROCE_STATE_RST_INIT ||
1260 	    handle->rinfo.instance_state == HNS_ROCE_STATE_INIT)
1261 		hr_dev->cmd.state = HNS_ROCE_CMDQ_STATE_FATAL_ERR;
1262 }
1263 
1264 static int hns_roce_cmd_err_convert_errno(u16 desc_ret)
1265 {
1266 	struct hns_roce_cmd_errcode errcode_table[] = {
1267 		{CMD_EXEC_SUCCESS, 0},
1268 		{CMD_NO_AUTH, -EPERM},
1269 		{CMD_NOT_EXIST, -EOPNOTSUPP},
1270 		{CMD_CRQ_FULL, -EXFULL},
1271 		{CMD_NEXT_ERR, -ENOSR},
1272 		{CMD_NOT_EXEC, -ENOTBLK},
1273 		{CMD_PARA_ERR, -EINVAL},
1274 		{CMD_RESULT_ERR, -ERANGE},
1275 		{CMD_TIMEOUT, -ETIME},
1276 		{CMD_HILINK_ERR, -ENOLINK},
1277 		{CMD_INFO_ILLEGAL, -ENXIO},
1278 		{CMD_INVALID, -EBADR},
1279 	};
1280 	u16 i;
1281 
1282 	for (i = 0; i < ARRAY_SIZE(errcode_table); i++)
1283 		if (desc_ret == errcode_table[i].return_status)
1284 			return errcode_table[i].errno;
1285 	return -EIO;
1286 }
1287 
1288 static int __hns_roce_cmq_send(struct hns_roce_dev *hr_dev,
1289 			       struct hns_roce_cmq_desc *desc, int num)
1290 {
1291 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1292 	struct hns_roce_v2_cmq_ring *csq = &priv->cmq.csq;
1293 	u32 timeout = 0;
1294 	u16 desc_ret;
1295 	u32 tail;
1296 	int ret;
1297 	int i;
1298 
1299 	spin_lock_bh(&csq->lock);
1300 
1301 	tail = csq->head;
1302 
1303 	for (i = 0; i < num; i++) {
1304 		csq->desc[csq->head++] = desc[i];
1305 		if (csq->head == csq->desc_num)
1306 			csq->head = 0;
1307 	}
1308 
1309 	/* Write to hardware */
1310 	roce_write(hr_dev, ROCEE_TX_CMQ_PI_REG, csq->head);
1311 
1312 	do {
1313 		if (hns_roce_cmq_csq_done(hr_dev))
1314 			break;
1315 		udelay(1);
1316 	} while (++timeout < priv->cmq.tx_timeout);
1317 
1318 	if (hns_roce_cmq_csq_done(hr_dev)) {
1319 		ret = 0;
1320 		for (i = 0; i < num; i++) {
1321 			/* check the result of hardware write back */
1322 			desc[i] = csq->desc[tail++];
1323 			if (tail == csq->desc_num)
1324 				tail = 0;
1325 
1326 			desc_ret = le16_to_cpu(desc[i].retval);
1327 			if (likely(desc_ret == CMD_EXEC_SUCCESS))
1328 				continue;
1329 
1330 			dev_err_ratelimited(hr_dev->dev,
1331 					    "Cmdq IO error, opcode = 0x%x, return = 0x%x.\n",
1332 					    desc->opcode, desc_ret);
1333 			ret = hns_roce_cmd_err_convert_errno(desc_ret);
1334 		}
1335 	} else {
1336 		/* FW/HW reset or incorrect number of desc */
1337 		tail = roce_read(hr_dev, ROCEE_TX_CMQ_CI_REG);
1338 		dev_warn(hr_dev->dev, "CMDQ move tail from %u to %u.\n",
1339 			 csq->head, tail);
1340 		csq->head = tail;
1341 
1342 		update_cmdq_status(hr_dev);
1343 
1344 		ret = -EAGAIN;
1345 	}
1346 
1347 	spin_unlock_bh(&csq->lock);
1348 
1349 	return ret;
1350 }
1351 
1352 static int hns_roce_cmq_send(struct hns_roce_dev *hr_dev,
1353 			     struct hns_roce_cmq_desc *desc, int num)
1354 {
1355 	bool busy;
1356 	int ret;
1357 
1358 	if (hr_dev->cmd.state == HNS_ROCE_CMDQ_STATE_FATAL_ERR)
1359 		return -EIO;
1360 
1361 	if (!v2_chk_mbox_is_avail(hr_dev, &busy))
1362 		return busy ? -EBUSY : 0;
1363 
1364 	ret = __hns_roce_cmq_send(hr_dev, desc, num);
1365 	if (ret) {
1366 		if (!v2_chk_mbox_is_avail(hr_dev, &busy))
1367 			return busy ? -EBUSY : 0;
1368 	}
1369 
1370 	return ret;
1371 }
1372 
1373 static int config_hem_ba_to_hw(struct hns_roce_dev *hr_dev,
1374 			       dma_addr_t base_addr, u8 cmd, unsigned long tag)
1375 {
1376 	struct hns_roce_cmd_mailbox *mbox;
1377 	int ret;
1378 
1379 	mbox = hns_roce_alloc_cmd_mailbox(hr_dev);
1380 	if (IS_ERR(mbox))
1381 		return PTR_ERR(mbox);
1382 
1383 	ret = hns_roce_cmd_mbox(hr_dev, base_addr, mbox->dma, cmd, tag);
1384 	hns_roce_free_cmd_mailbox(hr_dev, mbox);
1385 	return ret;
1386 }
1387 
1388 static int hns_roce_cmq_query_hw_info(struct hns_roce_dev *hr_dev)
1389 {
1390 	struct hns_roce_query_version *resp;
1391 	struct hns_roce_cmq_desc desc;
1392 	int ret;
1393 
1394 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_HW_VER, true);
1395 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1396 	if (ret)
1397 		return ret;
1398 
1399 	resp = (struct hns_roce_query_version *)desc.data;
1400 	hr_dev->hw_rev = le16_to_cpu(resp->rocee_hw_version);
1401 	hr_dev->vendor_id = hr_dev->pci_dev->vendor;
1402 
1403 	return 0;
1404 }
1405 
1406 static void func_clr_hw_resetting_state(struct hns_roce_dev *hr_dev,
1407 					struct hnae3_handle *handle)
1408 {
1409 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1410 	unsigned long end;
1411 
1412 	hr_dev->dis_db = true;
1413 
1414 	dev_warn(hr_dev->dev,
1415 		 "func clear is pending, device in resetting state.\n");
1416 	end = HNS_ROCE_V2_HW_RST_TIMEOUT;
1417 	while (end) {
1418 		if (!ops->get_hw_reset_stat(handle)) {
1419 			hr_dev->is_reset = true;
1420 			dev_info(hr_dev->dev,
1421 				 "func clear success after reset.\n");
1422 			return;
1423 		}
1424 		msleep(HNS_ROCE_V2_HW_RST_COMPLETION_WAIT);
1425 		end -= HNS_ROCE_V2_HW_RST_COMPLETION_WAIT;
1426 	}
1427 
1428 	dev_warn(hr_dev->dev, "func clear failed.\n");
1429 }
1430 
1431 static void func_clr_sw_resetting_state(struct hns_roce_dev *hr_dev,
1432 					struct hnae3_handle *handle)
1433 {
1434 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1435 	unsigned long end;
1436 
1437 	hr_dev->dis_db = true;
1438 
1439 	dev_warn(hr_dev->dev,
1440 		 "func clear is pending, device in resetting state.\n");
1441 	end = HNS_ROCE_V2_HW_RST_TIMEOUT;
1442 	while (end) {
1443 		if (ops->ae_dev_reset_cnt(handle) !=
1444 		    hr_dev->reset_cnt) {
1445 			hr_dev->is_reset = true;
1446 			dev_info(hr_dev->dev,
1447 				 "func clear success after sw reset\n");
1448 			return;
1449 		}
1450 		msleep(HNS_ROCE_V2_HW_RST_COMPLETION_WAIT);
1451 		end -= HNS_ROCE_V2_HW_RST_COMPLETION_WAIT;
1452 	}
1453 
1454 	dev_warn(hr_dev->dev, "func clear failed because of unfinished sw reset\n");
1455 }
1456 
1457 static void hns_roce_func_clr_rst_proc(struct hns_roce_dev *hr_dev, int retval,
1458 				       int flag)
1459 {
1460 	struct hns_roce_v2_priv *priv = hr_dev->priv;
1461 	struct hnae3_handle *handle = priv->handle;
1462 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1463 
1464 	if (ops->ae_dev_reset_cnt(handle) != hr_dev->reset_cnt) {
1465 		hr_dev->dis_db = true;
1466 		hr_dev->is_reset = true;
1467 		dev_info(hr_dev->dev, "func clear success after reset.\n");
1468 		return;
1469 	}
1470 
1471 	if (ops->get_hw_reset_stat(handle)) {
1472 		func_clr_hw_resetting_state(hr_dev, handle);
1473 		return;
1474 	}
1475 
1476 	if (ops->ae_dev_resetting(handle) &&
1477 	    handle->rinfo.instance_state == HNS_ROCE_STATE_INIT) {
1478 		func_clr_sw_resetting_state(hr_dev, handle);
1479 		return;
1480 	}
1481 
1482 	if (retval && !flag)
1483 		dev_warn(hr_dev->dev,
1484 			 "func clear read failed, ret = %d.\n", retval);
1485 
1486 	dev_warn(hr_dev->dev, "func clear failed.\n");
1487 }
1488 
1489 static void __hns_roce_function_clear(struct hns_roce_dev *hr_dev, int vf_id)
1490 {
1491 	bool fclr_write_fail_flag = false;
1492 	struct hns_roce_func_clear *resp;
1493 	struct hns_roce_cmq_desc desc;
1494 	unsigned long end;
1495 	int ret = 0;
1496 
1497 	if (check_device_is_in_reset(hr_dev))
1498 		goto out;
1499 
1500 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_FUNC_CLEAR, false);
1501 	resp = (struct hns_roce_func_clear *)desc.data;
1502 	resp->rst_funcid_en = cpu_to_le32(vf_id);
1503 
1504 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1505 	if (ret) {
1506 		fclr_write_fail_flag = true;
1507 		dev_err(hr_dev->dev, "func clear write failed, ret = %d.\n",
1508 			 ret);
1509 		goto out;
1510 	}
1511 
1512 	msleep(HNS_ROCE_V2_READ_FUNC_CLEAR_FLAG_INTERVAL);
1513 	end = HNS_ROCE_V2_FUNC_CLEAR_TIMEOUT_MSECS;
1514 	while (end) {
1515 		if (check_device_is_in_reset(hr_dev))
1516 			goto out;
1517 		msleep(HNS_ROCE_V2_READ_FUNC_CLEAR_FLAG_FAIL_WAIT);
1518 		end -= HNS_ROCE_V2_READ_FUNC_CLEAR_FLAG_FAIL_WAIT;
1519 
1520 		hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_FUNC_CLEAR,
1521 					      true);
1522 
1523 		resp->rst_funcid_en = cpu_to_le32(vf_id);
1524 		ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1525 		if (ret)
1526 			continue;
1527 
1528 		if (hr_reg_read(resp, FUNC_CLEAR_RST_FUN_DONE)) {
1529 			if (vf_id == 0)
1530 				hr_dev->is_reset = true;
1531 			return;
1532 		}
1533 	}
1534 
1535 out:
1536 	hns_roce_func_clr_rst_proc(hr_dev, ret, fclr_write_fail_flag);
1537 }
1538 
1539 static int hns_roce_free_vf_resource(struct hns_roce_dev *hr_dev, int vf_id)
1540 {
1541 	enum hns_roce_opcode_type opcode = HNS_ROCE_OPC_ALLOC_VF_RES;
1542 	struct hns_roce_cmq_desc desc[2];
1543 	struct hns_roce_cmq_req *req_a;
1544 
1545 	req_a = (struct hns_roce_cmq_req *)desc[0].data;
1546 	hns_roce_cmq_setup_basic_desc(&desc[0], opcode, false);
1547 	desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1548 	hns_roce_cmq_setup_basic_desc(&desc[1], opcode, false);
1549 	hr_reg_write(req_a, FUNC_RES_A_VF_ID, vf_id);
1550 
1551 	return hns_roce_cmq_send(hr_dev, desc, 2);
1552 }
1553 
1554 static void hns_roce_function_clear(struct hns_roce_dev *hr_dev)
1555 {
1556 	int ret;
1557 	int i;
1558 
1559 	if (hr_dev->cmd.state == HNS_ROCE_CMDQ_STATE_FATAL_ERR)
1560 		return;
1561 
1562 	for (i = hr_dev->func_num - 1; i >= 0; i--) {
1563 		__hns_roce_function_clear(hr_dev, i);
1564 
1565 		if (i == 0)
1566 			continue;
1567 
1568 		ret = hns_roce_free_vf_resource(hr_dev, i);
1569 		if (ret)
1570 			ibdev_err(&hr_dev->ib_dev,
1571 				  "failed to free vf resource, vf_id = %d, ret = %d.\n",
1572 				  i, ret);
1573 	}
1574 }
1575 
1576 static int hns_roce_clear_extdb_list_info(struct hns_roce_dev *hr_dev)
1577 {
1578 	struct hns_roce_cmq_desc desc;
1579 	int ret;
1580 
1581 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CLEAR_EXTDB_LIST_INFO,
1582 				      false);
1583 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1584 	if (ret)
1585 		ibdev_err(&hr_dev->ib_dev,
1586 			  "failed to clear extended doorbell info, ret = %d.\n",
1587 			  ret);
1588 
1589 	return ret;
1590 }
1591 
1592 static int hns_roce_query_fw_ver(struct hns_roce_dev *hr_dev)
1593 {
1594 	struct hns_roce_query_fw_info *resp;
1595 	struct hns_roce_cmq_desc desc;
1596 	int ret;
1597 
1598 	hns_roce_cmq_setup_basic_desc(&desc, HNS_QUERY_FW_VER, true);
1599 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1600 	if (ret)
1601 		return ret;
1602 
1603 	resp = (struct hns_roce_query_fw_info *)desc.data;
1604 	hr_dev->caps.fw_ver = (u64)(le32_to_cpu(resp->fw_ver));
1605 
1606 	return 0;
1607 }
1608 
1609 static int hns_roce_query_func_info(struct hns_roce_dev *hr_dev)
1610 {
1611 	struct hns_roce_cmq_desc desc;
1612 	int ret;
1613 
1614 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
1615 		hr_dev->func_num = 1;
1616 		return 0;
1617 	}
1618 
1619 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_FUNC_INFO,
1620 				      true);
1621 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1622 	if (ret) {
1623 		hr_dev->func_num = 1;
1624 		return ret;
1625 	}
1626 
1627 	hr_dev->func_num = le32_to_cpu(desc.func_info.own_func_num);
1628 	hr_dev->cong_algo_tmpl_id = le32_to_cpu(desc.func_info.own_mac_id);
1629 
1630 	return 0;
1631 }
1632 
1633 static int hns_roce_config_global_param(struct hns_roce_dev *hr_dev)
1634 {
1635 	struct hns_roce_cmq_desc desc;
1636 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1637 	u32 clock_cycles_of_1us;
1638 
1639 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GLOBAL_PARAM,
1640 				      false);
1641 
1642 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
1643 		clock_cycles_of_1us = HNS_ROCE_1NS_CFG;
1644 	else
1645 		clock_cycles_of_1us = HNS_ROCE_1US_CFG;
1646 
1647 	hr_reg_write(req, CFG_GLOBAL_PARAM_1US_CYCLES, clock_cycles_of_1us);
1648 	hr_reg_write(req, CFG_GLOBAL_PARAM_UDP_PORT, ROCE_V2_UDP_DPORT);
1649 
1650 	return hns_roce_cmq_send(hr_dev, &desc, 1);
1651 }
1652 
1653 static int load_func_res_caps(struct hns_roce_dev *hr_dev, bool is_vf)
1654 {
1655 	struct hns_roce_cmq_desc desc[2];
1656 	struct hns_roce_cmq_req *r_a = (struct hns_roce_cmq_req *)desc[0].data;
1657 	struct hns_roce_cmq_req *r_b = (struct hns_roce_cmq_req *)desc[1].data;
1658 	struct hns_roce_caps *caps = &hr_dev->caps;
1659 	enum hns_roce_opcode_type opcode;
1660 	u32 func_num;
1661 	int ret;
1662 
1663 	if (is_vf) {
1664 		opcode = HNS_ROCE_OPC_QUERY_VF_RES;
1665 		func_num = 1;
1666 	} else {
1667 		opcode = HNS_ROCE_OPC_QUERY_PF_RES;
1668 		func_num = hr_dev->func_num;
1669 	}
1670 
1671 	hns_roce_cmq_setup_basic_desc(&desc[0], opcode, true);
1672 	desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1673 	hns_roce_cmq_setup_basic_desc(&desc[1], opcode, true);
1674 
1675 	ret = hns_roce_cmq_send(hr_dev, desc, 2);
1676 	if (ret)
1677 		return ret;
1678 
1679 	caps->qpc_bt_num = hr_reg_read(r_a, FUNC_RES_A_QPC_BT_NUM) / func_num;
1680 	caps->srqc_bt_num = hr_reg_read(r_a, FUNC_RES_A_SRQC_BT_NUM) / func_num;
1681 	caps->cqc_bt_num = hr_reg_read(r_a, FUNC_RES_A_CQC_BT_NUM) / func_num;
1682 	caps->mpt_bt_num = hr_reg_read(r_a, FUNC_RES_A_MPT_BT_NUM) / func_num;
1683 	caps->eqc_bt_num = hr_reg_read(r_a, FUNC_RES_A_EQC_BT_NUM) / func_num;
1684 	caps->smac_bt_num = hr_reg_read(r_b, FUNC_RES_B_SMAC_NUM) / func_num;
1685 	caps->sgid_bt_num = hr_reg_read(r_b, FUNC_RES_B_SGID_NUM) / func_num;
1686 	caps->sccc_bt_num = hr_reg_read(r_b, FUNC_RES_B_SCCC_BT_NUM) / func_num;
1687 
1688 	if (is_vf) {
1689 		caps->sl_num = hr_reg_read(r_b, FUNC_RES_V_QID_NUM) / func_num;
1690 		caps->gmv_bt_num = hr_reg_read(r_b, FUNC_RES_V_GMV_BT_NUM) /
1691 					       func_num;
1692 	} else {
1693 		caps->sl_num = hr_reg_read(r_b, FUNC_RES_B_QID_NUM) / func_num;
1694 		caps->gmv_bt_num = hr_reg_read(r_b, FUNC_RES_B_GMV_BT_NUM) /
1695 					       func_num;
1696 	}
1697 
1698 	return 0;
1699 }
1700 
1701 static int load_ext_cfg_caps(struct hns_roce_dev *hr_dev, bool is_vf)
1702 {
1703 	struct hns_roce_cmq_desc desc;
1704 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1705 	struct hns_roce_caps *caps = &hr_dev->caps;
1706 	u32 func_num, qp_num;
1707 	int ret;
1708 
1709 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_EXT_CFG, true);
1710 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1711 	if (ret)
1712 		return ret;
1713 
1714 	func_num = is_vf ? 1 : max_t(u32, 1, hr_dev->func_num);
1715 	qp_num = hr_reg_read(req, EXT_CFG_QP_PI_NUM) / func_num;
1716 	caps->num_pi_qps = round_down(qp_num, HNS_ROCE_QP_BANK_NUM);
1717 
1718 	qp_num = hr_reg_read(req, EXT_CFG_QP_NUM) / func_num;
1719 	caps->num_qps = round_down(qp_num, HNS_ROCE_QP_BANK_NUM);
1720 
1721 	return 0;
1722 }
1723 
1724 static int load_pf_timer_res_caps(struct hns_roce_dev *hr_dev)
1725 {
1726 	struct hns_roce_cmq_desc desc;
1727 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1728 	struct hns_roce_caps *caps = &hr_dev->caps;
1729 	int ret;
1730 
1731 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_PF_TIMER_RES,
1732 				      true);
1733 
1734 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1735 	if (ret)
1736 		return ret;
1737 
1738 	caps->qpc_timer_bt_num = hr_reg_read(req, PF_TIMER_RES_QPC_ITEM_NUM);
1739 	caps->cqc_timer_bt_num = hr_reg_read(req, PF_TIMER_RES_CQC_ITEM_NUM);
1740 
1741 	return 0;
1742 }
1743 
1744 static int query_func_resource_caps(struct hns_roce_dev *hr_dev, bool is_vf)
1745 {
1746 	struct device *dev = hr_dev->dev;
1747 	int ret;
1748 
1749 	ret = load_func_res_caps(hr_dev, is_vf);
1750 	if (ret) {
1751 		dev_err(dev, "failed to load res caps, ret = %d (%s).\n", ret,
1752 			is_vf ? "vf" : "pf");
1753 		return ret;
1754 	}
1755 
1756 	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
1757 		ret = load_ext_cfg_caps(hr_dev, is_vf);
1758 		if (ret)
1759 			dev_err(dev, "failed to load ext cfg, ret = %d (%s).\n",
1760 				ret, is_vf ? "vf" : "pf");
1761 	}
1762 
1763 	return ret;
1764 }
1765 
1766 static int hns_roce_query_pf_resource(struct hns_roce_dev *hr_dev)
1767 {
1768 	struct device *dev = hr_dev->dev;
1769 	int ret;
1770 
1771 	ret = query_func_resource_caps(hr_dev, false);
1772 	if (ret)
1773 		return ret;
1774 
1775 	ret = load_pf_timer_res_caps(hr_dev);
1776 	if (ret)
1777 		dev_err(dev, "failed to load pf timer resource, ret = %d.\n",
1778 			ret);
1779 
1780 	return ret;
1781 }
1782 
1783 static int hns_roce_query_vf_resource(struct hns_roce_dev *hr_dev)
1784 {
1785 	return query_func_resource_caps(hr_dev, true);
1786 }
1787 
1788 static int __hns_roce_set_vf_switch_param(struct hns_roce_dev *hr_dev,
1789 					  u32 vf_id)
1790 {
1791 	struct hns_roce_vf_switch *swt;
1792 	struct hns_roce_cmq_desc desc;
1793 	int ret;
1794 
1795 	swt = (struct hns_roce_vf_switch *)desc.data;
1796 	hns_roce_cmq_setup_basic_desc(&desc, HNS_SWITCH_PARAMETER_CFG, true);
1797 	swt->rocee_sel |= cpu_to_le32(HNS_ICL_SWITCH_CMD_ROCEE_SEL);
1798 	hr_reg_write(swt, VF_SWITCH_VF_ID, vf_id);
1799 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1800 	if (ret)
1801 		return ret;
1802 
1803 	desc.flag = cpu_to_le16(HNS_ROCE_CMD_FLAG_IN);
1804 	desc.flag &= cpu_to_le16(~HNS_ROCE_CMD_FLAG_WR);
1805 	hr_reg_enable(swt, VF_SWITCH_ALW_LPBK);
1806 	hr_reg_clear(swt, VF_SWITCH_ALW_LCL_LPBK);
1807 	hr_reg_enable(swt, VF_SWITCH_ALW_DST_OVRD);
1808 
1809 	return hns_roce_cmq_send(hr_dev, &desc, 1);
1810 }
1811 
1812 static int hns_roce_set_vf_switch_param(struct hns_roce_dev *hr_dev)
1813 {
1814 	u32 vf_id;
1815 	int ret;
1816 
1817 	for (vf_id = 0; vf_id < hr_dev->func_num; vf_id++) {
1818 		ret = __hns_roce_set_vf_switch_param(hr_dev, vf_id);
1819 		if (ret)
1820 			return ret;
1821 	}
1822 	return 0;
1823 }
1824 
1825 static int config_vf_hem_resource(struct hns_roce_dev *hr_dev, int vf_id)
1826 {
1827 	struct hns_roce_cmq_desc desc[2];
1828 	struct hns_roce_cmq_req *r_a = (struct hns_roce_cmq_req *)desc[0].data;
1829 	struct hns_roce_cmq_req *r_b = (struct hns_roce_cmq_req *)desc[1].data;
1830 	enum hns_roce_opcode_type opcode = HNS_ROCE_OPC_ALLOC_VF_RES;
1831 	struct hns_roce_caps *caps = &hr_dev->caps;
1832 
1833 	hns_roce_cmq_setup_basic_desc(&desc[0], opcode, false);
1834 	desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1835 	hns_roce_cmq_setup_basic_desc(&desc[1], opcode, false);
1836 
1837 	hr_reg_write(r_a, FUNC_RES_A_VF_ID, vf_id);
1838 
1839 	hr_reg_write(r_a, FUNC_RES_A_QPC_BT_NUM, caps->qpc_bt_num);
1840 	hr_reg_write(r_a, FUNC_RES_A_QPC_BT_IDX, vf_id * caps->qpc_bt_num);
1841 	hr_reg_write(r_a, FUNC_RES_A_SRQC_BT_NUM, caps->srqc_bt_num);
1842 	hr_reg_write(r_a, FUNC_RES_A_SRQC_BT_IDX, vf_id * caps->srqc_bt_num);
1843 	hr_reg_write(r_a, FUNC_RES_A_CQC_BT_NUM, caps->cqc_bt_num);
1844 	hr_reg_write(r_a, FUNC_RES_A_CQC_BT_IDX, vf_id * caps->cqc_bt_num);
1845 	hr_reg_write(r_a, FUNC_RES_A_MPT_BT_NUM, caps->mpt_bt_num);
1846 	hr_reg_write(r_a, FUNC_RES_A_MPT_BT_IDX, vf_id * caps->mpt_bt_num);
1847 	hr_reg_write(r_a, FUNC_RES_A_EQC_BT_NUM, caps->eqc_bt_num);
1848 	hr_reg_write(r_a, FUNC_RES_A_EQC_BT_IDX, vf_id * caps->eqc_bt_num);
1849 	hr_reg_write(r_b, FUNC_RES_V_QID_NUM, caps->sl_num);
1850 	hr_reg_write(r_b, FUNC_RES_B_QID_IDX, vf_id * caps->sl_num);
1851 	hr_reg_write(r_b, FUNC_RES_B_SCCC_BT_NUM, caps->sccc_bt_num);
1852 	hr_reg_write(r_b, FUNC_RES_B_SCCC_BT_IDX, vf_id * caps->sccc_bt_num);
1853 
1854 	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
1855 		hr_reg_write(r_b, FUNC_RES_V_GMV_BT_NUM, caps->gmv_bt_num);
1856 		hr_reg_write(r_b, FUNC_RES_B_GMV_BT_IDX,
1857 			     vf_id * caps->gmv_bt_num);
1858 	} else {
1859 		hr_reg_write(r_b, FUNC_RES_B_SGID_NUM, caps->sgid_bt_num);
1860 		hr_reg_write(r_b, FUNC_RES_B_SGID_IDX,
1861 			     vf_id * caps->sgid_bt_num);
1862 		hr_reg_write(r_b, FUNC_RES_B_SMAC_NUM, caps->smac_bt_num);
1863 		hr_reg_write(r_b, FUNC_RES_B_SMAC_IDX,
1864 			     vf_id * caps->smac_bt_num);
1865 	}
1866 
1867 	return hns_roce_cmq_send(hr_dev, desc, 2);
1868 }
1869 
1870 static int config_vf_ext_resource(struct hns_roce_dev *hr_dev, u32 vf_id)
1871 {
1872 	struct hns_roce_cmq_desc desc;
1873 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1874 	struct hns_roce_caps *caps = &hr_dev->caps;
1875 
1876 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_EXT_CFG, false);
1877 
1878 	hr_reg_write(req, EXT_CFG_VF_ID, vf_id);
1879 
1880 	hr_reg_write(req, EXT_CFG_QP_PI_NUM, caps->num_pi_qps);
1881 	hr_reg_write(req, EXT_CFG_QP_PI_IDX, vf_id * caps->num_pi_qps);
1882 	hr_reg_write(req, EXT_CFG_QP_NUM, caps->num_qps);
1883 	hr_reg_write(req, EXT_CFG_QP_IDX, vf_id * caps->num_qps);
1884 
1885 	return hns_roce_cmq_send(hr_dev, &desc, 1);
1886 }
1887 
1888 static int hns_roce_alloc_vf_resource(struct hns_roce_dev *hr_dev)
1889 {
1890 	u32 func_num = max_t(u32, 1, hr_dev->func_num);
1891 	u32 vf_id;
1892 	int ret;
1893 
1894 	for (vf_id = 0; vf_id < func_num; vf_id++) {
1895 		ret = config_vf_hem_resource(hr_dev, vf_id);
1896 		if (ret) {
1897 			dev_err(hr_dev->dev,
1898 				"failed to config vf-%u hem res, ret = %d.\n",
1899 				vf_id, ret);
1900 			return ret;
1901 		}
1902 
1903 		if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
1904 			ret = config_vf_ext_resource(hr_dev, vf_id);
1905 			if (ret) {
1906 				dev_err(hr_dev->dev,
1907 					"failed to config vf-%u ext res, ret = %d.\n",
1908 					vf_id, ret);
1909 				return ret;
1910 			}
1911 		}
1912 	}
1913 
1914 	return 0;
1915 }
1916 
1917 static int hns_roce_v2_set_bt(struct hns_roce_dev *hr_dev)
1918 {
1919 	struct hns_roce_cmq_desc desc;
1920 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1921 	struct hns_roce_caps *caps = &hr_dev->caps;
1922 
1923 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_BT_ATTR, false);
1924 
1925 	hr_reg_write(req, CFG_BT_ATTR_QPC_BA_PGSZ,
1926 		     caps->qpc_ba_pg_sz + PG_SHIFT_OFFSET);
1927 	hr_reg_write(req, CFG_BT_ATTR_QPC_BUF_PGSZ,
1928 		     caps->qpc_buf_pg_sz + PG_SHIFT_OFFSET);
1929 	hr_reg_write(req, CFG_BT_ATTR_QPC_HOPNUM,
1930 		     to_hr_hem_hopnum(caps->qpc_hop_num, caps->num_qps));
1931 
1932 	hr_reg_write(req, CFG_BT_ATTR_SRQC_BA_PGSZ,
1933 		     caps->srqc_ba_pg_sz + PG_SHIFT_OFFSET);
1934 	hr_reg_write(req, CFG_BT_ATTR_SRQC_BUF_PGSZ,
1935 		     caps->srqc_buf_pg_sz + PG_SHIFT_OFFSET);
1936 	hr_reg_write(req, CFG_BT_ATTR_SRQC_HOPNUM,
1937 		     to_hr_hem_hopnum(caps->srqc_hop_num, caps->num_srqs));
1938 
1939 	hr_reg_write(req, CFG_BT_ATTR_CQC_BA_PGSZ,
1940 		     caps->cqc_ba_pg_sz + PG_SHIFT_OFFSET);
1941 	hr_reg_write(req, CFG_BT_ATTR_CQC_BUF_PGSZ,
1942 		     caps->cqc_buf_pg_sz + PG_SHIFT_OFFSET);
1943 	hr_reg_write(req, CFG_BT_ATTR_CQC_HOPNUM,
1944 		     to_hr_hem_hopnum(caps->cqc_hop_num, caps->num_cqs));
1945 
1946 	hr_reg_write(req, CFG_BT_ATTR_MPT_BA_PGSZ,
1947 		     caps->mpt_ba_pg_sz + PG_SHIFT_OFFSET);
1948 	hr_reg_write(req, CFG_BT_ATTR_MPT_BUF_PGSZ,
1949 		     caps->mpt_buf_pg_sz + PG_SHIFT_OFFSET);
1950 	hr_reg_write(req, CFG_BT_ATTR_MPT_HOPNUM,
1951 		     to_hr_hem_hopnum(caps->mpt_hop_num, caps->num_mtpts));
1952 
1953 	hr_reg_write(req, CFG_BT_ATTR_SCCC_BA_PGSZ,
1954 		     caps->sccc_ba_pg_sz + PG_SHIFT_OFFSET);
1955 	hr_reg_write(req, CFG_BT_ATTR_SCCC_BUF_PGSZ,
1956 		     caps->sccc_buf_pg_sz + PG_SHIFT_OFFSET);
1957 	hr_reg_write(req, CFG_BT_ATTR_SCCC_HOPNUM,
1958 		     to_hr_hem_hopnum(caps->sccc_hop_num, caps->num_qps));
1959 
1960 	return hns_roce_cmq_send(hr_dev, &desc, 1);
1961 }
1962 
1963 static void calc_pg_sz(u32 obj_num, u32 obj_size, u32 hop_num, u32 ctx_bt_num,
1964 		       u32 *buf_page_size, u32 *bt_page_size, u32 hem_type)
1965 {
1966 	u64 obj_per_chunk;
1967 	u64 bt_chunk_size = PAGE_SIZE;
1968 	u64 buf_chunk_size = PAGE_SIZE;
1969 	u64 obj_per_chunk_default = buf_chunk_size / obj_size;
1970 
1971 	*buf_page_size = 0;
1972 	*bt_page_size = 0;
1973 
1974 	switch (hop_num) {
1975 	case 3:
1976 		obj_per_chunk = ctx_bt_num * (bt_chunk_size / BA_BYTE_LEN) *
1977 				(bt_chunk_size / BA_BYTE_LEN) *
1978 				(bt_chunk_size / BA_BYTE_LEN) *
1979 				 obj_per_chunk_default;
1980 		break;
1981 	case 2:
1982 		obj_per_chunk = ctx_bt_num * (bt_chunk_size / BA_BYTE_LEN) *
1983 				(bt_chunk_size / BA_BYTE_LEN) *
1984 				 obj_per_chunk_default;
1985 		break;
1986 	case 1:
1987 		obj_per_chunk = ctx_bt_num * (bt_chunk_size / BA_BYTE_LEN) *
1988 				obj_per_chunk_default;
1989 		break;
1990 	case HNS_ROCE_HOP_NUM_0:
1991 		obj_per_chunk = ctx_bt_num * obj_per_chunk_default;
1992 		break;
1993 	default:
1994 		pr_err("table %u not support hop_num = %u!\n", hem_type,
1995 		       hop_num);
1996 		return;
1997 	}
1998 
1999 	if (hem_type >= HEM_TYPE_MTT)
2000 		*bt_page_size = ilog2(DIV_ROUND_UP(obj_num, obj_per_chunk));
2001 	else
2002 		*buf_page_size = ilog2(DIV_ROUND_UP(obj_num, obj_per_chunk));
2003 }
2004 
2005 static void set_hem_page_size(struct hns_roce_dev *hr_dev)
2006 {
2007 	struct hns_roce_caps *caps = &hr_dev->caps;
2008 
2009 	/* EQ */
2010 	caps->eqe_ba_pg_sz = 0;
2011 	caps->eqe_buf_pg_sz = 0;
2012 
2013 	/* Link Table */
2014 	caps->llm_buf_pg_sz = 0;
2015 
2016 	/* MR */
2017 	caps->mpt_ba_pg_sz = 0;
2018 	caps->mpt_buf_pg_sz = 0;
2019 	caps->pbl_ba_pg_sz = HNS_ROCE_BA_PG_SZ_SUPPORTED_16K;
2020 	caps->pbl_buf_pg_sz = 0;
2021 	calc_pg_sz(caps->num_mtpts, caps->mtpt_entry_sz, caps->mpt_hop_num,
2022 		   caps->mpt_bt_num, &caps->mpt_buf_pg_sz, &caps->mpt_ba_pg_sz,
2023 		   HEM_TYPE_MTPT);
2024 
2025 	/* QP */
2026 	caps->qpc_ba_pg_sz = 0;
2027 	caps->qpc_buf_pg_sz = 0;
2028 	caps->qpc_timer_ba_pg_sz = 0;
2029 	caps->qpc_timer_buf_pg_sz = 0;
2030 	caps->sccc_ba_pg_sz = 0;
2031 	caps->sccc_buf_pg_sz = 0;
2032 	caps->mtt_ba_pg_sz = 0;
2033 	caps->mtt_buf_pg_sz = 0;
2034 	calc_pg_sz(caps->num_qps, caps->qpc_sz, caps->qpc_hop_num,
2035 		   caps->qpc_bt_num, &caps->qpc_buf_pg_sz, &caps->qpc_ba_pg_sz,
2036 		   HEM_TYPE_QPC);
2037 
2038 	if (caps->flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL)
2039 		calc_pg_sz(caps->num_qps, caps->sccc_sz, caps->sccc_hop_num,
2040 			   caps->sccc_bt_num, &caps->sccc_buf_pg_sz,
2041 			   &caps->sccc_ba_pg_sz, HEM_TYPE_SCCC);
2042 
2043 	/* CQ */
2044 	caps->cqc_ba_pg_sz = 0;
2045 	caps->cqc_buf_pg_sz = 0;
2046 	caps->cqc_timer_ba_pg_sz = 0;
2047 	caps->cqc_timer_buf_pg_sz = 0;
2048 	caps->cqe_ba_pg_sz = HNS_ROCE_BA_PG_SZ_SUPPORTED_256K;
2049 	caps->cqe_buf_pg_sz = 0;
2050 	calc_pg_sz(caps->num_cqs, caps->cqc_entry_sz, caps->cqc_hop_num,
2051 		   caps->cqc_bt_num, &caps->cqc_buf_pg_sz, &caps->cqc_ba_pg_sz,
2052 		   HEM_TYPE_CQC);
2053 	calc_pg_sz(caps->max_cqes, caps->cqe_sz, caps->cqe_hop_num,
2054 		   1, &caps->cqe_buf_pg_sz, &caps->cqe_ba_pg_sz, HEM_TYPE_CQE);
2055 
2056 	/* SRQ */
2057 	if (caps->flags & HNS_ROCE_CAP_FLAG_SRQ) {
2058 		caps->srqc_ba_pg_sz = 0;
2059 		caps->srqc_buf_pg_sz = 0;
2060 		caps->srqwqe_ba_pg_sz = 0;
2061 		caps->srqwqe_buf_pg_sz = 0;
2062 		caps->idx_ba_pg_sz = 0;
2063 		caps->idx_buf_pg_sz = 0;
2064 		calc_pg_sz(caps->num_srqs, caps->srqc_entry_sz,
2065 			   caps->srqc_hop_num, caps->srqc_bt_num,
2066 			   &caps->srqc_buf_pg_sz, &caps->srqc_ba_pg_sz,
2067 			   HEM_TYPE_SRQC);
2068 		calc_pg_sz(caps->num_srqwqe_segs, caps->mtt_entry_sz,
2069 			   caps->srqwqe_hop_num, 1, &caps->srqwqe_buf_pg_sz,
2070 			   &caps->srqwqe_ba_pg_sz, HEM_TYPE_SRQWQE);
2071 		calc_pg_sz(caps->num_idx_segs, caps->idx_entry_sz,
2072 			   caps->idx_hop_num, 1, &caps->idx_buf_pg_sz,
2073 			   &caps->idx_ba_pg_sz, HEM_TYPE_IDX);
2074 	}
2075 
2076 	/* GMV */
2077 	caps->gmv_ba_pg_sz = 0;
2078 	caps->gmv_buf_pg_sz = 0;
2079 }
2080 
2081 /* Apply all loaded caps before setting to hardware */
2082 static void apply_func_caps(struct hns_roce_dev *hr_dev)
2083 {
2084 	struct hns_roce_caps *caps = &hr_dev->caps;
2085 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2086 
2087 	/* The following configurations don't need to be got from firmware. */
2088 	caps->qpc_timer_entry_sz = HNS_ROCE_V2_QPC_TIMER_ENTRY_SZ;
2089 	caps->cqc_timer_entry_sz = HNS_ROCE_V2_CQC_TIMER_ENTRY_SZ;
2090 	caps->mtt_entry_sz = HNS_ROCE_V2_MTT_ENTRY_SZ;
2091 
2092 	caps->pbl_hop_num = HNS_ROCE_PBL_HOP_NUM;
2093 	caps->qpc_timer_hop_num = HNS_ROCE_HOP_NUM_0;
2094 	caps->cqc_timer_hop_num = HNS_ROCE_HOP_NUM_0;
2095 
2096 	caps->num_xrcds = HNS_ROCE_V2_MAX_XRCD_NUM;
2097 	caps->reserved_xrcds = HNS_ROCE_V2_RSV_XRCD_NUM;
2098 
2099 	caps->num_srqwqe_segs = HNS_ROCE_V2_MAX_SRQWQE_SEGS;
2100 	caps->num_idx_segs = HNS_ROCE_V2_MAX_IDX_SEGS;
2101 
2102 	if (!caps->num_comp_vectors)
2103 		caps->num_comp_vectors =
2104 			min_t(u32, caps->eqc_bt_num - HNS_ROCE_V2_AEQE_VEC_NUM,
2105 				(u32)priv->handle->rinfo.num_vectors -
2106 		(HNS_ROCE_V2_AEQE_VEC_NUM + HNS_ROCE_V2_ABNORMAL_VEC_NUM));
2107 
2108 	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
2109 		caps->eqe_hop_num = HNS_ROCE_V3_EQE_HOP_NUM;
2110 		caps->ceqe_size = HNS_ROCE_V3_EQE_SIZE;
2111 		caps->aeqe_size = HNS_ROCE_V3_EQE_SIZE;
2112 
2113 		/* The following configurations will be overwritten */
2114 		caps->qpc_sz = HNS_ROCE_V3_QPC_SZ;
2115 		caps->cqe_sz = HNS_ROCE_V3_CQE_SIZE;
2116 		caps->sccc_sz = HNS_ROCE_V3_SCCC_SZ;
2117 
2118 		/* The following configurations are not got from firmware */
2119 		caps->gmv_entry_sz = HNS_ROCE_V3_GMV_ENTRY_SZ;
2120 
2121 		caps->gmv_hop_num = HNS_ROCE_HOP_NUM_0;
2122 		caps->gid_table_len[0] = caps->gmv_bt_num *
2123 					(HNS_HW_PAGE_SIZE / caps->gmv_entry_sz);
2124 
2125 		caps->gmv_entry_num = caps->gmv_bt_num * (PAGE_SIZE /
2126 							  caps->gmv_entry_sz);
2127 	} else {
2128 		u32 func_num = max_t(u32, 1, hr_dev->func_num);
2129 
2130 		caps->eqe_hop_num = HNS_ROCE_V2_EQE_HOP_NUM;
2131 		caps->ceqe_size = HNS_ROCE_CEQE_SIZE;
2132 		caps->aeqe_size = HNS_ROCE_AEQE_SIZE;
2133 		caps->gid_table_len[0] /= func_num;
2134 	}
2135 
2136 	if (hr_dev->is_vf) {
2137 		caps->default_aeq_arm_st = 0x3;
2138 		caps->default_ceq_arm_st = 0x3;
2139 		caps->default_ceq_max_cnt = 0x1;
2140 		caps->default_ceq_period = 0x10;
2141 		caps->default_aeq_max_cnt = 0x1;
2142 		caps->default_aeq_period = 0x10;
2143 	}
2144 
2145 	set_hem_page_size(hr_dev);
2146 }
2147 
2148 static int hns_roce_query_caps(struct hns_roce_dev *hr_dev)
2149 {
2150 	struct hns_roce_cmq_desc desc[HNS_ROCE_QUERY_PF_CAPS_CMD_NUM];
2151 	struct hns_roce_caps *caps = &hr_dev->caps;
2152 	struct hns_roce_query_pf_caps_a *resp_a;
2153 	struct hns_roce_query_pf_caps_b *resp_b;
2154 	struct hns_roce_query_pf_caps_c *resp_c;
2155 	struct hns_roce_query_pf_caps_d *resp_d;
2156 	struct hns_roce_query_pf_caps_e *resp_e;
2157 	enum hns_roce_opcode_type cmd;
2158 	int ctx_hop_num;
2159 	int pbl_hop_num;
2160 	int ret;
2161 	int i;
2162 
2163 	cmd = hr_dev->is_vf ? HNS_ROCE_OPC_QUERY_VF_CAPS_NUM :
2164 	      HNS_ROCE_OPC_QUERY_PF_CAPS_NUM;
2165 
2166 	for (i = 0; i < HNS_ROCE_QUERY_PF_CAPS_CMD_NUM; i++) {
2167 		hns_roce_cmq_setup_basic_desc(&desc[i], cmd, true);
2168 		if (i < (HNS_ROCE_QUERY_PF_CAPS_CMD_NUM - 1))
2169 			desc[i].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
2170 		else
2171 			desc[i].flag &= ~cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
2172 	}
2173 
2174 	ret = hns_roce_cmq_send(hr_dev, desc, HNS_ROCE_QUERY_PF_CAPS_CMD_NUM);
2175 	if (ret)
2176 		return ret;
2177 
2178 	resp_a = (struct hns_roce_query_pf_caps_a *)desc[0].data;
2179 	resp_b = (struct hns_roce_query_pf_caps_b *)desc[1].data;
2180 	resp_c = (struct hns_roce_query_pf_caps_c *)desc[2].data;
2181 	resp_d = (struct hns_roce_query_pf_caps_d *)desc[3].data;
2182 	resp_e = (struct hns_roce_query_pf_caps_e *)desc[4].data;
2183 
2184 	caps->local_ca_ack_delay = resp_a->local_ca_ack_delay;
2185 	caps->max_sq_sg = le16_to_cpu(resp_a->max_sq_sg);
2186 	caps->max_sq_inline = le16_to_cpu(resp_a->max_sq_inline);
2187 	caps->max_rq_sg = le16_to_cpu(resp_a->max_rq_sg);
2188 	caps->max_rq_sg = roundup_pow_of_two(caps->max_rq_sg);
2189 	caps->max_srq_sges = le16_to_cpu(resp_a->max_srq_sges);
2190 	caps->max_srq_sges = roundup_pow_of_two(caps->max_srq_sges);
2191 	caps->num_aeq_vectors = resp_a->num_aeq_vectors;
2192 	caps->num_other_vectors = resp_a->num_other_vectors;
2193 	caps->max_sq_desc_sz = resp_a->max_sq_desc_sz;
2194 	caps->max_rq_desc_sz = resp_a->max_rq_desc_sz;
2195 
2196 	caps->mtpt_entry_sz = resp_b->mtpt_entry_sz;
2197 	caps->irrl_entry_sz = resp_b->irrl_entry_sz;
2198 	caps->trrl_entry_sz = resp_b->trrl_entry_sz;
2199 	caps->cqc_entry_sz = resp_b->cqc_entry_sz;
2200 	caps->srqc_entry_sz = resp_b->srqc_entry_sz;
2201 	caps->idx_entry_sz = resp_b->idx_entry_sz;
2202 	caps->sccc_sz = resp_b->sccc_sz;
2203 	caps->max_mtu = resp_b->max_mtu;
2204 	caps->min_cqes = resp_b->min_cqes;
2205 	caps->min_wqes = resp_b->min_wqes;
2206 	caps->page_size_cap = le32_to_cpu(resp_b->page_size_cap);
2207 	caps->pkey_table_len[0] = resp_b->pkey_table_len;
2208 	caps->phy_num_uars = resp_b->phy_num_uars;
2209 	ctx_hop_num = resp_b->ctx_hop_num;
2210 	pbl_hop_num = resp_b->pbl_hop_num;
2211 
2212 	caps->num_pds = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_PDS);
2213 
2214 	caps->flags = hr_reg_read(resp_c, PF_CAPS_C_CAP_FLAGS);
2215 	caps->flags |= le16_to_cpu(resp_d->cap_flags_ex) <<
2216 		       HNS_ROCE_CAP_FLAGS_EX_SHIFT;
2217 
2218 	caps->num_cqs = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_CQS);
2219 	caps->gid_table_len[0] = hr_reg_read(resp_c, PF_CAPS_C_MAX_GID);
2220 	caps->max_cqes = 1 << hr_reg_read(resp_c, PF_CAPS_C_CQ_DEPTH);
2221 	caps->num_mtpts = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_MRWS);
2222 	caps->num_qps = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_QPS);
2223 	caps->max_qp_init_rdma = hr_reg_read(resp_c, PF_CAPS_C_MAX_ORD);
2224 	caps->max_qp_dest_rdma = caps->max_qp_init_rdma;
2225 	caps->max_wqes = 1 << le16_to_cpu(resp_c->sq_depth);
2226 
2227 	caps->num_srqs = 1 << hr_reg_read(resp_d, PF_CAPS_D_NUM_SRQS);
2228 	caps->cong_type = hr_reg_read(resp_d, PF_CAPS_D_CONG_TYPE);
2229 	caps->max_srq_wrs = 1 << le16_to_cpu(resp_d->srq_depth);
2230 	caps->ceqe_depth = 1 << hr_reg_read(resp_d, PF_CAPS_D_CEQ_DEPTH);
2231 	caps->num_comp_vectors = hr_reg_read(resp_d, PF_CAPS_D_NUM_CEQS);
2232 	caps->aeqe_depth = 1 << hr_reg_read(resp_d, PF_CAPS_D_AEQ_DEPTH);
2233 	caps->reserved_pds = hr_reg_read(resp_d, PF_CAPS_D_RSV_PDS);
2234 	caps->num_uars = 1 << hr_reg_read(resp_d, PF_CAPS_D_NUM_UARS);
2235 	caps->reserved_qps = hr_reg_read(resp_d, PF_CAPS_D_RSV_QPS);
2236 	caps->reserved_uars = hr_reg_read(resp_d, PF_CAPS_D_RSV_UARS);
2237 
2238 	caps->reserved_mrws = hr_reg_read(resp_e, PF_CAPS_E_RSV_MRWS);
2239 	caps->chunk_sz = 1 << hr_reg_read(resp_e, PF_CAPS_E_CHUNK_SIZE_SHIFT);
2240 	caps->reserved_cqs = hr_reg_read(resp_e, PF_CAPS_E_RSV_CQS);
2241 	caps->reserved_srqs = hr_reg_read(resp_e, PF_CAPS_E_RSV_SRQS);
2242 	caps->reserved_lkey = hr_reg_read(resp_e, PF_CAPS_E_RSV_LKEYS);
2243 
2244 	caps->qpc_hop_num = ctx_hop_num;
2245 	caps->sccc_hop_num = ctx_hop_num;
2246 	caps->srqc_hop_num = ctx_hop_num;
2247 	caps->cqc_hop_num = ctx_hop_num;
2248 	caps->mpt_hop_num = ctx_hop_num;
2249 	caps->mtt_hop_num = pbl_hop_num;
2250 	caps->cqe_hop_num = pbl_hop_num;
2251 	caps->srqwqe_hop_num = pbl_hop_num;
2252 	caps->idx_hop_num = pbl_hop_num;
2253 	caps->wqe_sq_hop_num = hr_reg_read(resp_d, PF_CAPS_D_SQWQE_HOP_NUM);
2254 	caps->wqe_sge_hop_num = hr_reg_read(resp_d, PF_CAPS_D_EX_SGE_HOP_NUM);
2255 	caps->wqe_rq_hop_num = hr_reg_read(resp_d, PF_CAPS_D_RQWQE_HOP_NUM);
2256 
2257 	if (!(caps->page_size_cap & PAGE_SIZE))
2258 		caps->page_size_cap = HNS_ROCE_V2_PAGE_SIZE_SUPPORTED;
2259 
2260 	if (!hr_dev->is_vf) {
2261 		caps->cqe_sz = resp_a->cqe_sz;
2262 		caps->qpc_sz = le16_to_cpu(resp_b->qpc_sz);
2263 		caps->default_aeq_arm_st =
2264 				hr_reg_read(resp_d, PF_CAPS_D_AEQ_ARM_ST);
2265 		caps->default_ceq_arm_st =
2266 				hr_reg_read(resp_d, PF_CAPS_D_CEQ_ARM_ST);
2267 		caps->default_ceq_max_cnt = le16_to_cpu(resp_e->ceq_max_cnt);
2268 		caps->default_ceq_period = le16_to_cpu(resp_e->ceq_period);
2269 		caps->default_aeq_max_cnt = le16_to_cpu(resp_e->aeq_max_cnt);
2270 		caps->default_aeq_period = le16_to_cpu(resp_e->aeq_period);
2271 	}
2272 
2273 	return 0;
2274 }
2275 
2276 static int config_hem_entry_size(struct hns_roce_dev *hr_dev, u32 type, u32 val)
2277 {
2278 	struct hns_roce_cmq_desc desc;
2279 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
2280 
2281 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_ENTRY_SIZE,
2282 				      false);
2283 
2284 	hr_reg_write(req, CFG_HEM_ENTRY_SIZE_TYPE, type);
2285 	hr_reg_write(req, CFG_HEM_ENTRY_SIZE_VALUE, val);
2286 
2287 	return hns_roce_cmq_send(hr_dev, &desc, 1);
2288 }
2289 
2290 static int hns_roce_config_entry_size(struct hns_roce_dev *hr_dev)
2291 {
2292 	struct hns_roce_caps *caps = &hr_dev->caps;
2293 	int ret;
2294 
2295 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
2296 		return 0;
2297 
2298 	ret = config_hem_entry_size(hr_dev, HNS_ROCE_CFG_QPC_SIZE,
2299 				    caps->qpc_sz);
2300 	if (ret) {
2301 		dev_err(hr_dev->dev, "failed to cfg qpc sz, ret = %d.\n", ret);
2302 		return ret;
2303 	}
2304 
2305 	ret = config_hem_entry_size(hr_dev, HNS_ROCE_CFG_SCCC_SIZE,
2306 				    caps->sccc_sz);
2307 	if (ret)
2308 		dev_err(hr_dev->dev, "failed to cfg sccc sz, ret = %d.\n", ret);
2309 
2310 	return ret;
2311 }
2312 
2313 static int hns_roce_v2_vf_profile(struct hns_roce_dev *hr_dev)
2314 {
2315 	struct device *dev = hr_dev->dev;
2316 	int ret;
2317 
2318 	hr_dev->func_num = 1;
2319 
2320 	ret = hns_roce_query_caps(hr_dev);
2321 	if (ret) {
2322 		dev_err(dev, "failed to query VF caps, ret = %d.\n", ret);
2323 		return ret;
2324 	}
2325 
2326 	ret = hns_roce_query_vf_resource(hr_dev);
2327 	if (ret) {
2328 		dev_err(dev, "failed to query VF resource, ret = %d.\n", ret);
2329 		return ret;
2330 	}
2331 
2332 	apply_func_caps(hr_dev);
2333 
2334 	ret = hns_roce_v2_set_bt(hr_dev);
2335 	if (ret)
2336 		dev_err(dev, "failed to config VF BA table, ret = %d.\n", ret);
2337 
2338 	return ret;
2339 }
2340 
2341 static int hns_roce_v2_pf_profile(struct hns_roce_dev *hr_dev)
2342 {
2343 	struct device *dev = hr_dev->dev;
2344 	int ret;
2345 
2346 	ret = hns_roce_query_func_info(hr_dev);
2347 	if (ret) {
2348 		dev_err(dev, "failed to query func info, ret = %d.\n", ret);
2349 		return ret;
2350 	}
2351 
2352 	ret = hns_roce_config_global_param(hr_dev);
2353 	if (ret) {
2354 		dev_err(dev, "failed to config global param, ret = %d.\n", ret);
2355 		return ret;
2356 	}
2357 
2358 	ret = hns_roce_set_vf_switch_param(hr_dev);
2359 	if (ret) {
2360 		dev_err(dev, "failed to set switch param, ret = %d.\n", ret);
2361 		return ret;
2362 	}
2363 
2364 	ret = hns_roce_query_caps(hr_dev);
2365 	if (ret) {
2366 		dev_err(dev, "failed to query PF caps, ret = %d.\n", ret);
2367 		return ret;
2368 	}
2369 
2370 	ret = hns_roce_query_pf_resource(hr_dev);
2371 	if (ret) {
2372 		dev_err(dev, "failed to query pf resource, ret = %d.\n", ret);
2373 		return ret;
2374 	}
2375 
2376 	apply_func_caps(hr_dev);
2377 
2378 	ret = hns_roce_alloc_vf_resource(hr_dev);
2379 	if (ret) {
2380 		dev_err(dev, "failed to alloc vf resource, ret = %d.\n", ret);
2381 		return ret;
2382 	}
2383 
2384 	ret = hns_roce_v2_set_bt(hr_dev);
2385 	if (ret) {
2386 		dev_err(dev, "failed to config BA table, ret = %d.\n", ret);
2387 		return ret;
2388 	}
2389 
2390 	/* Configure the size of QPC, SCCC, etc. */
2391 	return hns_roce_config_entry_size(hr_dev);
2392 }
2393 
2394 static int hns_roce_v2_profile(struct hns_roce_dev *hr_dev)
2395 {
2396 	struct device *dev = hr_dev->dev;
2397 	int ret;
2398 
2399 	ret = hns_roce_cmq_query_hw_info(hr_dev);
2400 	if (ret) {
2401 		dev_err(dev, "failed to query hardware info, ret = %d.\n", ret);
2402 		return ret;
2403 	}
2404 
2405 	ret = hns_roce_query_fw_ver(hr_dev);
2406 	if (ret) {
2407 		dev_err(dev, "failed to query firmware info, ret = %d.\n", ret);
2408 		return ret;
2409 	}
2410 
2411 	hr_dev->vendor_part_id = hr_dev->pci_dev->device;
2412 	hr_dev->sys_image_guid = be64_to_cpu(hr_dev->ib_dev.node_guid);
2413 
2414 	if (hr_dev->is_vf)
2415 		return hns_roce_v2_vf_profile(hr_dev);
2416 	else
2417 		return hns_roce_v2_pf_profile(hr_dev);
2418 }
2419 
2420 static void config_llm_table(struct hns_roce_buf *data_buf, void *cfg_buf)
2421 {
2422 	u32 i, next_ptr, page_num;
2423 	__le64 *entry = cfg_buf;
2424 	dma_addr_t addr;
2425 	u64 val;
2426 
2427 	page_num = data_buf->npages;
2428 	for (i = 0; i < page_num; i++) {
2429 		addr = hns_roce_buf_page(data_buf, i);
2430 		if (i == (page_num - 1))
2431 			next_ptr = 0;
2432 		else
2433 			next_ptr = i + 1;
2434 
2435 		val = HNS_ROCE_EXT_LLM_ENTRY(addr, (u64)next_ptr);
2436 		entry[i] = cpu_to_le64(val);
2437 	}
2438 }
2439 
2440 static int set_llm_cfg_to_hw(struct hns_roce_dev *hr_dev,
2441 			     struct hns_roce_link_table *table)
2442 {
2443 	struct hns_roce_cmq_desc desc[2];
2444 	struct hns_roce_cmq_req *r_a = (struct hns_roce_cmq_req *)desc[0].data;
2445 	struct hns_roce_cmq_req *r_b = (struct hns_roce_cmq_req *)desc[1].data;
2446 	struct hns_roce_buf *buf = table->buf;
2447 	enum hns_roce_opcode_type opcode;
2448 	dma_addr_t addr;
2449 
2450 	opcode = HNS_ROCE_OPC_CFG_EXT_LLM;
2451 	hns_roce_cmq_setup_basic_desc(&desc[0], opcode, false);
2452 	desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
2453 	hns_roce_cmq_setup_basic_desc(&desc[1], opcode, false);
2454 
2455 	hr_reg_write(r_a, CFG_LLM_A_BA_L, lower_32_bits(table->table.map));
2456 	hr_reg_write(r_a, CFG_LLM_A_BA_H, upper_32_bits(table->table.map));
2457 	hr_reg_write(r_a, CFG_LLM_A_DEPTH, buf->npages);
2458 	hr_reg_write(r_a, CFG_LLM_A_PGSZ, to_hr_hw_page_shift(buf->page_shift));
2459 	hr_reg_enable(r_a, CFG_LLM_A_INIT_EN);
2460 
2461 	addr = to_hr_hw_page_addr(hns_roce_buf_page(buf, 0));
2462 	hr_reg_write(r_a, CFG_LLM_A_HEAD_BA_L, lower_32_bits(addr));
2463 	hr_reg_write(r_a, CFG_LLM_A_HEAD_BA_H, upper_32_bits(addr));
2464 	hr_reg_write(r_a, CFG_LLM_A_HEAD_NXTPTR, 1);
2465 	hr_reg_write(r_a, CFG_LLM_A_HEAD_PTR, 0);
2466 
2467 	addr = to_hr_hw_page_addr(hns_roce_buf_page(buf, buf->npages - 1));
2468 	hr_reg_write(r_b, CFG_LLM_B_TAIL_BA_L, lower_32_bits(addr));
2469 	hr_reg_write(r_b, CFG_LLM_B_TAIL_BA_H, upper_32_bits(addr));
2470 	hr_reg_write(r_b, CFG_LLM_B_TAIL_PTR, buf->npages - 1);
2471 
2472 	return hns_roce_cmq_send(hr_dev, desc, 2);
2473 }
2474 
2475 static struct hns_roce_link_table *
2476 alloc_link_table_buf(struct hns_roce_dev *hr_dev)
2477 {
2478 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2479 	struct hns_roce_link_table *link_tbl;
2480 	u32 pg_shift, size, min_size;
2481 
2482 	link_tbl = &priv->ext_llm;
2483 	pg_shift = hr_dev->caps.llm_buf_pg_sz + PAGE_SHIFT;
2484 	size = hr_dev->caps.num_qps * HNS_ROCE_V2_EXT_LLM_ENTRY_SZ;
2485 	min_size = HNS_ROCE_EXT_LLM_MIN_PAGES(hr_dev->caps.sl_num) << pg_shift;
2486 
2487 	/* Alloc data table */
2488 	size = max(size, min_size);
2489 	link_tbl->buf = hns_roce_buf_alloc(hr_dev, size, pg_shift, 0);
2490 	if (IS_ERR(link_tbl->buf))
2491 		return ERR_PTR(-ENOMEM);
2492 
2493 	/* Alloc config table */
2494 	size = link_tbl->buf->npages * sizeof(u64);
2495 	link_tbl->table.buf = dma_alloc_coherent(hr_dev->dev, size,
2496 						 &link_tbl->table.map,
2497 						 GFP_KERNEL);
2498 	if (!link_tbl->table.buf) {
2499 		hns_roce_buf_free(hr_dev, link_tbl->buf);
2500 		return ERR_PTR(-ENOMEM);
2501 	}
2502 
2503 	return link_tbl;
2504 }
2505 
2506 static void free_link_table_buf(struct hns_roce_dev *hr_dev,
2507 				struct hns_roce_link_table *tbl)
2508 {
2509 	if (tbl->buf) {
2510 		u32 size = tbl->buf->npages * sizeof(u64);
2511 
2512 		dma_free_coherent(hr_dev->dev, size, tbl->table.buf,
2513 				  tbl->table.map);
2514 	}
2515 
2516 	hns_roce_buf_free(hr_dev, tbl->buf);
2517 }
2518 
2519 static int hns_roce_init_link_table(struct hns_roce_dev *hr_dev)
2520 {
2521 	struct hns_roce_link_table *link_tbl;
2522 	int ret;
2523 
2524 	link_tbl = alloc_link_table_buf(hr_dev);
2525 	if (IS_ERR(link_tbl))
2526 		return -ENOMEM;
2527 
2528 	if (WARN_ON(link_tbl->buf->npages > HNS_ROCE_V2_EXT_LLM_MAX_DEPTH)) {
2529 		ret = -EINVAL;
2530 		goto err_alloc;
2531 	}
2532 
2533 	config_llm_table(link_tbl->buf, link_tbl->table.buf);
2534 	ret = set_llm_cfg_to_hw(hr_dev, link_tbl);
2535 	if (ret)
2536 		goto err_alloc;
2537 
2538 	return 0;
2539 
2540 err_alloc:
2541 	free_link_table_buf(hr_dev, link_tbl);
2542 	return ret;
2543 }
2544 
2545 static void hns_roce_free_link_table(struct hns_roce_dev *hr_dev)
2546 {
2547 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2548 
2549 	free_link_table_buf(hr_dev, &priv->ext_llm);
2550 }
2551 
2552 static void free_dip_list(struct hns_roce_dev *hr_dev)
2553 {
2554 	struct hns_roce_dip *hr_dip;
2555 	struct hns_roce_dip *tmp;
2556 	unsigned long flags;
2557 
2558 	spin_lock_irqsave(&hr_dev->dip_list_lock, flags);
2559 
2560 	list_for_each_entry_safe(hr_dip, tmp, &hr_dev->dip_list, node) {
2561 		list_del(&hr_dip->node);
2562 		kfree(hr_dip);
2563 	}
2564 
2565 	spin_unlock_irqrestore(&hr_dev->dip_list_lock, flags);
2566 }
2567 
2568 static struct ib_pd *free_mr_init_pd(struct hns_roce_dev *hr_dev)
2569 {
2570 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2571 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2572 	struct ib_device *ibdev = &hr_dev->ib_dev;
2573 	struct hns_roce_pd *hr_pd;
2574 	struct ib_pd *pd;
2575 
2576 	hr_pd = kzalloc(sizeof(*hr_pd), GFP_KERNEL);
2577 	if (ZERO_OR_NULL_PTR(hr_pd))
2578 		return NULL;
2579 	pd = &hr_pd->ibpd;
2580 	pd->device = ibdev;
2581 
2582 	if (hns_roce_alloc_pd(pd, NULL)) {
2583 		ibdev_err(ibdev, "failed to create pd for free mr.\n");
2584 		kfree(hr_pd);
2585 		return NULL;
2586 	}
2587 	free_mr->rsv_pd = to_hr_pd(pd);
2588 	free_mr->rsv_pd->ibpd.device = &hr_dev->ib_dev;
2589 	free_mr->rsv_pd->ibpd.uobject = NULL;
2590 	free_mr->rsv_pd->ibpd.__internal_mr = NULL;
2591 	atomic_set(&free_mr->rsv_pd->ibpd.usecnt, 0);
2592 
2593 	return pd;
2594 }
2595 
2596 static struct ib_cq *free_mr_init_cq(struct hns_roce_dev *hr_dev)
2597 {
2598 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2599 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2600 	struct ib_device *ibdev = &hr_dev->ib_dev;
2601 	struct ib_cq_init_attr cq_init_attr = {};
2602 	struct hns_roce_cq *hr_cq;
2603 	struct ib_cq *cq;
2604 
2605 	cq_init_attr.cqe = HNS_ROCE_FREE_MR_USED_CQE_NUM;
2606 
2607 	hr_cq = kzalloc(sizeof(*hr_cq), GFP_KERNEL);
2608 	if (ZERO_OR_NULL_PTR(hr_cq))
2609 		return NULL;
2610 
2611 	cq = &hr_cq->ib_cq;
2612 	cq->device = ibdev;
2613 
2614 	if (hns_roce_create_cq(cq, &cq_init_attr, NULL)) {
2615 		ibdev_err(ibdev, "failed to create cq for free mr.\n");
2616 		kfree(hr_cq);
2617 		return NULL;
2618 	}
2619 	free_mr->rsv_cq = to_hr_cq(cq);
2620 	free_mr->rsv_cq->ib_cq.device = &hr_dev->ib_dev;
2621 	free_mr->rsv_cq->ib_cq.uobject = NULL;
2622 	free_mr->rsv_cq->ib_cq.comp_handler = NULL;
2623 	free_mr->rsv_cq->ib_cq.event_handler = NULL;
2624 	free_mr->rsv_cq->ib_cq.cq_context = NULL;
2625 	atomic_set(&free_mr->rsv_cq->ib_cq.usecnt, 0);
2626 
2627 	return cq;
2628 }
2629 
2630 static int free_mr_init_qp(struct hns_roce_dev *hr_dev, struct ib_cq *cq,
2631 			   struct ib_qp_init_attr *init_attr, int i)
2632 {
2633 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2634 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2635 	struct ib_device *ibdev = &hr_dev->ib_dev;
2636 	struct hns_roce_qp *hr_qp;
2637 	struct ib_qp *qp;
2638 	int ret;
2639 
2640 	hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL);
2641 	if (ZERO_OR_NULL_PTR(hr_qp))
2642 		return -ENOMEM;
2643 
2644 	qp = &hr_qp->ibqp;
2645 	qp->device = ibdev;
2646 
2647 	ret = hns_roce_create_qp(qp, init_attr, NULL);
2648 	if (ret) {
2649 		ibdev_err(ibdev, "failed to create qp for free mr.\n");
2650 		kfree(hr_qp);
2651 		return ret;
2652 	}
2653 
2654 	free_mr->rsv_qp[i] = hr_qp;
2655 	free_mr->rsv_qp[i]->ibqp.recv_cq = cq;
2656 	free_mr->rsv_qp[i]->ibqp.send_cq = cq;
2657 
2658 	return 0;
2659 }
2660 
2661 static void free_mr_exit(struct hns_roce_dev *hr_dev)
2662 {
2663 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2664 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2665 	struct ib_qp *qp;
2666 	int i;
2667 
2668 	for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
2669 		if (free_mr->rsv_qp[i]) {
2670 			qp = &free_mr->rsv_qp[i]->ibqp;
2671 			hns_roce_v2_destroy_qp(qp, NULL);
2672 			kfree(free_mr->rsv_qp[i]);
2673 			free_mr->rsv_qp[i] = NULL;
2674 		}
2675 	}
2676 
2677 	if (free_mr->rsv_cq) {
2678 		hns_roce_destroy_cq(&free_mr->rsv_cq->ib_cq, NULL);
2679 		kfree(free_mr->rsv_cq);
2680 		free_mr->rsv_cq = NULL;
2681 	}
2682 
2683 	if (free_mr->rsv_pd) {
2684 		hns_roce_dealloc_pd(&free_mr->rsv_pd->ibpd, NULL);
2685 		kfree(free_mr->rsv_pd);
2686 		free_mr->rsv_pd = NULL;
2687 	}
2688 }
2689 
2690 static int free_mr_alloc_res(struct hns_roce_dev *hr_dev)
2691 {
2692 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2693 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2694 	struct ib_qp_init_attr qp_init_attr = {};
2695 	struct ib_pd *pd;
2696 	struct ib_cq *cq;
2697 	int ret;
2698 	int i;
2699 
2700 	pd = free_mr_init_pd(hr_dev);
2701 	if (!pd)
2702 		return -ENOMEM;
2703 
2704 	cq = free_mr_init_cq(hr_dev);
2705 	if (!cq) {
2706 		ret = -ENOMEM;
2707 		goto create_failed_cq;
2708 	}
2709 
2710 	qp_init_attr.qp_type = IB_QPT_RC;
2711 	qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2712 	qp_init_attr.send_cq = cq;
2713 	qp_init_attr.recv_cq = cq;
2714 	for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
2715 		qp_init_attr.cap.max_send_wr = HNS_ROCE_FREE_MR_USED_SQWQE_NUM;
2716 		qp_init_attr.cap.max_send_sge = HNS_ROCE_FREE_MR_USED_SQSGE_NUM;
2717 		qp_init_attr.cap.max_recv_wr = HNS_ROCE_FREE_MR_USED_RQWQE_NUM;
2718 		qp_init_attr.cap.max_recv_sge = HNS_ROCE_FREE_MR_USED_RQSGE_NUM;
2719 
2720 		ret = free_mr_init_qp(hr_dev, cq, &qp_init_attr, i);
2721 		if (ret)
2722 			goto create_failed_qp;
2723 	}
2724 
2725 	return 0;
2726 
2727 create_failed_qp:
2728 	hns_roce_destroy_cq(cq, NULL);
2729 	kfree(cq);
2730 
2731 create_failed_cq:
2732 	hns_roce_dealloc_pd(pd, NULL);
2733 	kfree(pd);
2734 
2735 	return ret;
2736 }
2737 
2738 static int free_mr_modify_rsv_qp(struct hns_roce_dev *hr_dev,
2739 				 struct ib_qp_attr *attr, int sl_num)
2740 {
2741 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2742 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2743 	struct ib_device *ibdev = &hr_dev->ib_dev;
2744 	struct hns_roce_qp *hr_qp;
2745 	int loopback;
2746 	int mask;
2747 	int ret;
2748 
2749 	hr_qp = to_hr_qp(&free_mr->rsv_qp[sl_num]->ibqp);
2750 	hr_qp->free_mr_en = 1;
2751 	hr_qp->ibqp.device = ibdev;
2752 	hr_qp->ibqp.qp_type = IB_QPT_RC;
2753 
2754 	mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT | IB_QP_ACCESS_FLAGS;
2755 	attr->qp_state = IB_QPS_INIT;
2756 	attr->port_num = 1;
2757 	attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
2758 	ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, attr, mask, IB_QPS_INIT,
2759 				    IB_QPS_INIT, NULL);
2760 	if (ret) {
2761 		ibdev_err(ibdev, "failed to modify qp to init, ret = %d.\n",
2762 			  ret);
2763 		return ret;
2764 	}
2765 
2766 	loopback = hr_dev->loop_idc;
2767 	/* Set qpc lbi = 1 incidate loopback IO */
2768 	hr_dev->loop_idc = 1;
2769 
2770 	mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | IB_QP_DEST_QPN |
2771 	       IB_QP_RQ_PSN | IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER;
2772 	attr->qp_state = IB_QPS_RTR;
2773 	attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
2774 	attr->path_mtu = IB_MTU_256;
2775 	attr->dest_qp_num = hr_qp->qpn;
2776 	attr->rq_psn = HNS_ROCE_FREE_MR_USED_PSN;
2777 
2778 	rdma_ah_set_sl(&attr->ah_attr, (u8)sl_num);
2779 
2780 	ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, attr, mask, IB_QPS_INIT,
2781 				    IB_QPS_RTR, NULL);
2782 	hr_dev->loop_idc = loopback;
2783 	if (ret) {
2784 		ibdev_err(ibdev, "failed to modify qp to rtr, ret = %d.\n",
2785 			  ret);
2786 		return ret;
2787 	}
2788 
2789 	mask = IB_QP_STATE | IB_QP_SQ_PSN | IB_QP_RETRY_CNT | IB_QP_TIMEOUT |
2790 	       IB_QP_RNR_RETRY | IB_QP_MAX_QP_RD_ATOMIC;
2791 	attr->qp_state = IB_QPS_RTS;
2792 	attr->sq_psn = HNS_ROCE_FREE_MR_USED_PSN;
2793 	attr->retry_cnt = HNS_ROCE_FREE_MR_USED_QP_RETRY_CNT;
2794 	attr->timeout = HNS_ROCE_FREE_MR_USED_QP_TIMEOUT;
2795 	ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, attr, mask, IB_QPS_RTR,
2796 				    IB_QPS_RTS, NULL);
2797 	if (ret)
2798 		ibdev_err(ibdev, "failed to modify qp to rts, ret = %d.\n",
2799 			  ret);
2800 
2801 	return ret;
2802 }
2803 
2804 static int free_mr_modify_qp(struct hns_roce_dev *hr_dev)
2805 {
2806 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2807 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2808 	struct ib_qp_attr attr = {};
2809 	int ret;
2810 	int i;
2811 
2812 	rdma_ah_set_grh(&attr.ah_attr, NULL, 0, 0, 1, 0);
2813 	rdma_ah_set_static_rate(&attr.ah_attr, 3);
2814 	rdma_ah_set_port_num(&attr.ah_attr, 1);
2815 
2816 	for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
2817 		ret = free_mr_modify_rsv_qp(hr_dev, &attr, i);
2818 		if (ret)
2819 			return ret;
2820 	}
2821 
2822 	return 0;
2823 }
2824 
2825 static int free_mr_init(struct hns_roce_dev *hr_dev)
2826 {
2827 	struct hns_roce_v2_priv *priv = hr_dev->priv;
2828 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2829 	int ret;
2830 
2831 	mutex_init(&free_mr->mutex);
2832 
2833 	ret = free_mr_alloc_res(hr_dev);
2834 	if (ret)
2835 		return ret;
2836 
2837 	ret = free_mr_modify_qp(hr_dev);
2838 	if (ret)
2839 		goto err_modify_qp;
2840 
2841 	return 0;
2842 
2843 err_modify_qp:
2844 	free_mr_exit(hr_dev);
2845 
2846 	return ret;
2847 }
2848 
2849 static int get_hem_table(struct hns_roce_dev *hr_dev)
2850 {
2851 	unsigned int qpc_count;
2852 	unsigned int cqc_count;
2853 	unsigned int gmv_count;
2854 	int ret;
2855 	int i;
2856 
2857 	/* Alloc memory for source address table buffer space chunk */
2858 	for (gmv_count = 0; gmv_count < hr_dev->caps.gmv_entry_num;
2859 	     gmv_count++) {
2860 		ret = hns_roce_table_get(hr_dev, &hr_dev->gmv_table, gmv_count);
2861 		if (ret)
2862 			goto err_gmv_failed;
2863 	}
2864 
2865 	if (hr_dev->is_vf)
2866 		return 0;
2867 
2868 	/* Alloc memory for QPC Timer buffer space chunk */
2869 	for (qpc_count = 0; qpc_count < hr_dev->caps.qpc_timer_bt_num;
2870 	     qpc_count++) {
2871 		ret = hns_roce_table_get(hr_dev, &hr_dev->qpc_timer_table,
2872 					 qpc_count);
2873 		if (ret) {
2874 			dev_err(hr_dev->dev, "QPC Timer get failed\n");
2875 			goto err_qpc_timer_failed;
2876 		}
2877 	}
2878 
2879 	/* Alloc memory for CQC Timer buffer space chunk */
2880 	for (cqc_count = 0; cqc_count < hr_dev->caps.cqc_timer_bt_num;
2881 	     cqc_count++) {
2882 		ret = hns_roce_table_get(hr_dev, &hr_dev->cqc_timer_table,
2883 					 cqc_count);
2884 		if (ret) {
2885 			dev_err(hr_dev->dev, "CQC Timer get failed\n");
2886 			goto err_cqc_timer_failed;
2887 		}
2888 	}
2889 
2890 	return 0;
2891 
2892 err_cqc_timer_failed:
2893 	for (i = 0; i < cqc_count; i++)
2894 		hns_roce_table_put(hr_dev, &hr_dev->cqc_timer_table, i);
2895 
2896 err_qpc_timer_failed:
2897 	for (i = 0; i < qpc_count; i++)
2898 		hns_roce_table_put(hr_dev, &hr_dev->qpc_timer_table, i);
2899 
2900 err_gmv_failed:
2901 	for (i = 0; i < gmv_count; i++)
2902 		hns_roce_table_put(hr_dev, &hr_dev->gmv_table, i);
2903 
2904 	return ret;
2905 }
2906 
2907 static void put_hem_table(struct hns_roce_dev *hr_dev)
2908 {
2909 	int i;
2910 
2911 	for (i = 0; i < hr_dev->caps.gmv_entry_num; i++)
2912 		hns_roce_table_put(hr_dev, &hr_dev->gmv_table, i);
2913 
2914 	if (hr_dev->is_vf)
2915 		return;
2916 
2917 	for (i = 0; i < hr_dev->caps.qpc_timer_bt_num; i++)
2918 		hns_roce_table_put(hr_dev, &hr_dev->qpc_timer_table, i);
2919 
2920 	for (i = 0; i < hr_dev->caps.cqc_timer_bt_num; i++)
2921 		hns_roce_table_put(hr_dev, &hr_dev->cqc_timer_table, i);
2922 }
2923 
2924 static int hns_roce_v2_init(struct hns_roce_dev *hr_dev)
2925 {
2926 	int ret;
2927 
2928 	/* The hns ROCEE requires the extdb info to be cleared before using */
2929 	ret = hns_roce_clear_extdb_list_info(hr_dev);
2930 	if (ret)
2931 		return ret;
2932 
2933 	ret = get_hem_table(hr_dev);
2934 	if (ret)
2935 		return ret;
2936 
2937 	if (hr_dev->is_vf)
2938 		return 0;
2939 
2940 	ret = hns_roce_init_link_table(hr_dev);
2941 	if (ret) {
2942 		dev_err(hr_dev->dev, "failed to init llm, ret = %d.\n", ret);
2943 		goto err_llm_init_failed;
2944 	}
2945 
2946 	return 0;
2947 
2948 err_llm_init_failed:
2949 	put_hem_table(hr_dev);
2950 
2951 	return ret;
2952 }
2953 
2954 static void hns_roce_v2_exit(struct hns_roce_dev *hr_dev)
2955 {
2956 	hns_roce_function_clear(hr_dev);
2957 
2958 	if (!hr_dev->is_vf)
2959 		hns_roce_free_link_table(hr_dev);
2960 
2961 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP09)
2962 		free_dip_list(hr_dev);
2963 }
2964 
2965 static int hns_roce_mbox_post(struct hns_roce_dev *hr_dev,
2966 			      struct hns_roce_mbox_msg *mbox_msg)
2967 {
2968 	struct hns_roce_cmq_desc desc;
2969 	struct hns_roce_post_mbox *mb = (struct hns_roce_post_mbox *)desc.data;
2970 
2971 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_POST_MB, false);
2972 
2973 	mb->in_param_l = cpu_to_le32(mbox_msg->in_param);
2974 	mb->in_param_h = cpu_to_le32(mbox_msg->in_param >> 32);
2975 	mb->out_param_l = cpu_to_le32(mbox_msg->out_param);
2976 	mb->out_param_h = cpu_to_le32(mbox_msg->out_param >> 32);
2977 	mb->cmd_tag = cpu_to_le32(mbox_msg->tag << 8 | mbox_msg->cmd);
2978 	mb->token_event_en = cpu_to_le32(mbox_msg->event_en << 16 |
2979 					 mbox_msg->token);
2980 
2981 	return hns_roce_cmq_send(hr_dev, &desc, 1);
2982 }
2983 
2984 static int v2_wait_mbox_complete(struct hns_roce_dev *hr_dev, u32 timeout,
2985 				 u8 *complete_status)
2986 {
2987 	struct hns_roce_mbox_status *mb_st;
2988 	struct hns_roce_cmq_desc desc;
2989 	unsigned long end;
2990 	int ret = -EBUSY;
2991 	u32 status;
2992 	bool busy;
2993 
2994 	mb_st = (struct hns_roce_mbox_status *)desc.data;
2995 	end = msecs_to_jiffies(timeout) + jiffies;
2996 	while (v2_chk_mbox_is_avail(hr_dev, &busy)) {
2997 		if (hr_dev->cmd.state == HNS_ROCE_CMDQ_STATE_FATAL_ERR)
2998 			return -EIO;
2999 
3000 		status = 0;
3001 		hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_MB_ST,
3002 					      true);
3003 		ret = __hns_roce_cmq_send(hr_dev, &desc, 1);
3004 		if (!ret) {
3005 			status = le32_to_cpu(mb_st->mb_status_hw_run);
3006 			/* No pending message exists in ROCEE mbox. */
3007 			if (!(status & MB_ST_HW_RUN_M))
3008 				break;
3009 		} else if (!v2_chk_mbox_is_avail(hr_dev, &busy)) {
3010 			break;
3011 		}
3012 
3013 		if (time_after(jiffies, end)) {
3014 			dev_err_ratelimited(hr_dev->dev,
3015 					    "failed to wait mbox status 0x%x\n",
3016 					    status);
3017 			return -ETIMEDOUT;
3018 		}
3019 
3020 		cond_resched();
3021 		ret = -EBUSY;
3022 	}
3023 
3024 	if (!ret) {
3025 		*complete_status = (u8)(status & MB_ST_COMPLETE_M);
3026 	} else if (!v2_chk_mbox_is_avail(hr_dev, &busy)) {
3027 		/* Ignore all errors if the mbox is unavailable. */
3028 		ret = 0;
3029 		*complete_status = MB_ST_COMPLETE_M;
3030 	}
3031 
3032 	return ret;
3033 }
3034 
3035 static int v2_post_mbox(struct hns_roce_dev *hr_dev,
3036 			struct hns_roce_mbox_msg *mbox_msg)
3037 {
3038 	u8 status = 0;
3039 	int ret;
3040 
3041 	/* Waiting for the mbox to be idle */
3042 	ret = v2_wait_mbox_complete(hr_dev, HNS_ROCE_V2_GO_BIT_TIMEOUT_MSECS,
3043 				    &status);
3044 	if (unlikely(ret)) {
3045 		dev_err_ratelimited(hr_dev->dev,
3046 				    "failed to check post mbox status = 0x%x, ret = %d.\n",
3047 				    status, ret);
3048 		return ret;
3049 	}
3050 
3051 	/* Post new message to mbox */
3052 	ret = hns_roce_mbox_post(hr_dev, mbox_msg);
3053 	if (ret)
3054 		dev_err_ratelimited(hr_dev->dev,
3055 				    "failed to post mailbox, ret = %d.\n", ret);
3056 
3057 	return ret;
3058 }
3059 
3060 static int v2_poll_mbox_done(struct hns_roce_dev *hr_dev)
3061 {
3062 	u8 status = 0;
3063 	int ret;
3064 
3065 	ret = v2_wait_mbox_complete(hr_dev, HNS_ROCE_CMD_TIMEOUT_MSECS,
3066 				    &status);
3067 	if (!ret) {
3068 		if (status != MB_ST_COMPLETE_SUCC)
3069 			return -EBUSY;
3070 	} else {
3071 		dev_err_ratelimited(hr_dev->dev,
3072 				    "failed to check mbox status = 0x%x, ret = %d.\n",
3073 				    status, ret);
3074 	}
3075 
3076 	return ret;
3077 }
3078 
3079 static void copy_gid(void *dest, const union ib_gid *gid)
3080 {
3081 #define GID_SIZE 4
3082 	const union ib_gid *src = gid;
3083 	__le32 (*p)[GID_SIZE] = dest;
3084 	int i;
3085 
3086 	if (!gid)
3087 		src = &zgid;
3088 
3089 	for (i = 0; i < GID_SIZE; i++)
3090 		(*p)[i] = cpu_to_le32(*(u32 *)&src->raw[i * sizeof(u32)]);
3091 }
3092 
3093 static int config_sgid_table(struct hns_roce_dev *hr_dev,
3094 			     int gid_index, const union ib_gid *gid,
3095 			     enum hns_roce_sgid_type sgid_type)
3096 {
3097 	struct hns_roce_cmq_desc desc;
3098 	struct hns_roce_cfg_sgid_tb *sgid_tb =
3099 				    (struct hns_roce_cfg_sgid_tb *)desc.data;
3100 
3101 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_SGID_TB, false);
3102 
3103 	hr_reg_write(sgid_tb, CFG_SGID_TB_TABLE_IDX, gid_index);
3104 	hr_reg_write(sgid_tb, CFG_SGID_TB_VF_SGID_TYPE, sgid_type);
3105 
3106 	copy_gid(&sgid_tb->vf_sgid_l, gid);
3107 
3108 	return hns_roce_cmq_send(hr_dev, &desc, 1);
3109 }
3110 
3111 static int config_gmv_table(struct hns_roce_dev *hr_dev,
3112 			    int gid_index, const union ib_gid *gid,
3113 			    enum hns_roce_sgid_type sgid_type,
3114 			    const struct ib_gid_attr *attr)
3115 {
3116 	struct hns_roce_cmq_desc desc[2];
3117 	struct hns_roce_cfg_gmv_tb_a *tb_a =
3118 				(struct hns_roce_cfg_gmv_tb_a *)desc[0].data;
3119 	struct hns_roce_cfg_gmv_tb_b *tb_b =
3120 				(struct hns_roce_cfg_gmv_tb_b *)desc[1].data;
3121 
3122 	u16 vlan_id = VLAN_CFI_MASK;
3123 	u8 mac[ETH_ALEN] = {};
3124 	int ret;
3125 
3126 	if (gid) {
3127 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, mac);
3128 		if (ret)
3129 			return ret;
3130 	}
3131 
3132 	hns_roce_cmq_setup_basic_desc(&desc[0], HNS_ROCE_OPC_CFG_GMV_TBL, false);
3133 	desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
3134 
3135 	hns_roce_cmq_setup_basic_desc(&desc[1], HNS_ROCE_OPC_CFG_GMV_TBL, false);
3136 
3137 	copy_gid(&tb_a->vf_sgid_l, gid);
3138 
3139 	hr_reg_write(tb_a, GMV_TB_A_VF_SGID_TYPE, sgid_type);
3140 	hr_reg_write(tb_a, GMV_TB_A_VF_VLAN_EN, vlan_id < VLAN_CFI_MASK);
3141 	hr_reg_write(tb_a, GMV_TB_A_VF_VLAN_ID, vlan_id);
3142 
3143 	tb_b->vf_smac_l = cpu_to_le32(*(u32 *)mac);
3144 
3145 	hr_reg_write(tb_b, GMV_TB_B_SMAC_H, *(u16 *)&mac[4]);
3146 	hr_reg_write(tb_b, GMV_TB_B_SGID_IDX, gid_index);
3147 
3148 	return hns_roce_cmq_send(hr_dev, desc, 2);
3149 }
3150 
3151 static int hns_roce_v2_set_gid(struct hns_roce_dev *hr_dev, int gid_index,
3152 			       const union ib_gid *gid,
3153 			       const struct ib_gid_attr *attr)
3154 {
3155 	enum hns_roce_sgid_type sgid_type = GID_TYPE_FLAG_ROCE_V1;
3156 	int ret;
3157 
3158 	if (gid) {
3159 		if (attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
3160 			if (ipv6_addr_v4mapped((void *)gid))
3161 				sgid_type = GID_TYPE_FLAG_ROCE_V2_IPV4;
3162 			else
3163 				sgid_type = GID_TYPE_FLAG_ROCE_V2_IPV6;
3164 		} else if (attr->gid_type == IB_GID_TYPE_ROCE) {
3165 			sgid_type = GID_TYPE_FLAG_ROCE_V1;
3166 		}
3167 	}
3168 
3169 	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
3170 		ret = config_gmv_table(hr_dev, gid_index, gid, sgid_type, attr);
3171 	else
3172 		ret = config_sgid_table(hr_dev, gid_index, gid, sgid_type);
3173 
3174 	if (ret)
3175 		ibdev_err(&hr_dev->ib_dev, "failed to set gid, ret = %d!\n",
3176 			  ret);
3177 
3178 	return ret;
3179 }
3180 
3181 static int hns_roce_v2_set_mac(struct hns_roce_dev *hr_dev, u8 phy_port,
3182 			       const u8 *addr)
3183 {
3184 	struct hns_roce_cmq_desc desc;
3185 	struct hns_roce_cfg_smac_tb *smac_tb =
3186 				    (struct hns_roce_cfg_smac_tb *)desc.data;
3187 	u16 reg_smac_h;
3188 	u32 reg_smac_l;
3189 
3190 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_SMAC_TB, false);
3191 
3192 	reg_smac_l = *(u32 *)(&addr[0]);
3193 	reg_smac_h = *(u16 *)(&addr[4]);
3194 
3195 	hr_reg_write(smac_tb, CFG_SMAC_TB_IDX, phy_port);
3196 	hr_reg_write(smac_tb, CFG_SMAC_TB_VF_SMAC_H, reg_smac_h);
3197 	smac_tb->vf_smac_l = cpu_to_le32(reg_smac_l);
3198 
3199 	return hns_roce_cmq_send(hr_dev, &desc, 1);
3200 }
3201 
3202 static int set_mtpt_pbl(struct hns_roce_dev *hr_dev,
3203 			struct hns_roce_v2_mpt_entry *mpt_entry,
3204 			struct hns_roce_mr *mr)
3205 {
3206 	u64 pages[HNS_ROCE_V2_MAX_INNER_MTPT_NUM] = { 0 };
3207 	struct ib_device *ibdev = &hr_dev->ib_dev;
3208 	dma_addr_t pbl_ba;
3209 	int i, count;
3210 
3211 	count = hns_roce_mtr_find(hr_dev, &mr->pbl_mtr, 0, pages,
3212 				  min_t(int, ARRAY_SIZE(pages), mr->npages),
3213 				  &pbl_ba);
3214 	if (count < 1) {
3215 		ibdev_err(ibdev, "failed to find PBL mtr, count = %d.\n",
3216 			  count);
3217 		return -ENOBUFS;
3218 	}
3219 
3220 	/* Aligned to the hardware address access unit */
3221 	for (i = 0; i < count; i++)
3222 		pages[i] >>= 6;
3223 
3224 	mpt_entry->pbl_size = cpu_to_le32(mr->npages);
3225 	mpt_entry->pbl_ba_l = cpu_to_le32(pbl_ba >> 3);
3226 	hr_reg_write(mpt_entry, MPT_PBL_BA_H, upper_32_bits(pbl_ba >> 3));
3227 
3228 	mpt_entry->pa0_l = cpu_to_le32(lower_32_bits(pages[0]));
3229 	hr_reg_write(mpt_entry, MPT_PA0_H, upper_32_bits(pages[0]));
3230 
3231 	mpt_entry->pa1_l = cpu_to_le32(lower_32_bits(pages[1]));
3232 	hr_reg_write(mpt_entry, MPT_PA1_H, upper_32_bits(pages[1]));
3233 	hr_reg_write(mpt_entry, MPT_PBL_BUF_PG_SZ,
3234 		     to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.buf_pg_shift));
3235 
3236 	return 0;
3237 }
3238 
3239 static int hns_roce_v2_write_mtpt(struct hns_roce_dev *hr_dev,
3240 				  void *mb_buf, struct hns_roce_mr *mr)
3241 {
3242 	struct hns_roce_v2_mpt_entry *mpt_entry;
3243 
3244 	mpt_entry = mb_buf;
3245 	memset(mpt_entry, 0, sizeof(*mpt_entry));
3246 
3247 	hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_VALID);
3248 	hr_reg_write(mpt_entry, MPT_PD, mr->pd);
3249 
3250 	hr_reg_write_bool(mpt_entry, MPT_BIND_EN,
3251 			  mr->access & IB_ACCESS_MW_BIND);
3252 	hr_reg_write_bool(mpt_entry, MPT_ATOMIC_EN,
3253 			  mr->access & IB_ACCESS_REMOTE_ATOMIC);
3254 	hr_reg_write_bool(mpt_entry, MPT_RR_EN,
3255 			  mr->access & IB_ACCESS_REMOTE_READ);
3256 	hr_reg_write_bool(mpt_entry, MPT_RW_EN,
3257 			  mr->access & IB_ACCESS_REMOTE_WRITE);
3258 	hr_reg_write_bool(mpt_entry, MPT_LW_EN,
3259 			  mr->access & IB_ACCESS_LOCAL_WRITE);
3260 
3261 	mpt_entry->len_l = cpu_to_le32(lower_32_bits(mr->size));
3262 	mpt_entry->len_h = cpu_to_le32(upper_32_bits(mr->size));
3263 	mpt_entry->lkey = cpu_to_le32(mr->key);
3264 	mpt_entry->va_l = cpu_to_le32(lower_32_bits(mr->iova));
3265 	mpt_entry->va_h = cpu_to_le32(upper_32_bits(mr->iova));
3266 
3267 	if (mr->type != MR_TYPE_MR)
3268 		hr_reg_enable(mpt_entry, MPT_PA);
3269 
3270 	if (mr->type == MR_TYPE_DMA)
3271 		return 0;
3272 
3273 	if (mr->pbl_hop_num != HNS_ROCE_HOP_NUM_0)
3274 		hr_reg_write(mpt_entry, MPT_PBL_HOP_NUM, mr->pbl_hop_num);
3275 
3276 	hr_reg_write(mpt_entry, MPT_PBL_BA_PG_SZ,
3277 		     to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.ba_pg_shift));
3278 	hr_reg_enable(mpt_entry, MPT_INNER_PA_VLD);
3279 
3280 	return set_mtpt_pbl(hr_dev, mpt_entry, mr);
3281 }
3282 
3283 static int hns_roce_v2_rereg_write_mtpt(struct hns_roce_dev *hr_dev,
3284 					struct hns_roce_mr *mr, int flags,
3285 					void *mb_buf)
3286 {
3287 	struct hns_roce_v2_mpt_entry *mpt_entry = mb_buf;
3288 	u32 mr_access_flags = mr->access;
3289 	int ret = 0;
3290 
3291 	hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_VALID);
3292 	hr_reg_write(mpt_entry, MPT_PD, mr->pd);
3293 
3294 	if (flags & IB_MR_REREG_ACCESS) {
3295 		hr_reg_write(mpt_entry, MPT_BIND_EN,
3296 			     (mr_access_flags & IB_ACCESS_MW_BIND ? 1 : 0));
3297 		hr_reg_write(mpt_entry, MPT_ATOMIC_EN,
3298 			     mr_access_flags & IB_ACCESS_REMOTE_ATOMIC ? 1 : 0);
3299 		hr_reg_write(mpt_entry, MPT_RR_EN,
3300 			     mr_access_flags & IB_ACCESS_REMOTE_READ ? 1 : 0);
3301 		hr_reg_write(mpt_entry, MPT_RW_EN,
3302 			     mr_access_flags & IB_ACCESS_REMOTE_WRITE ? 1 : 0);
3303 		hr_reg_write(mpt_entry, MPT_LW_EN,
3304 			     mr_access_flags & IB_ACCESS_LOCAL_WRITE ? 1 : 0);
3305 	}
3306 
3307 	if (flags & IB_MR_REREG_TRANS) {
3308 		mpt_entry->va_l = cpu_to_le32(lower_32_bits(mr->iova));
3309 		mpt_entry->va_h = cpu_to_le32(upper_32_bits(mr->iova));
3310 		mpt_entry->len_l = cpu_to_le32(lower_32_bits(mr->size));
3311 		mpt_entry->len_h = cpu_to_le32(upper_32_bits(mr->size));
3312 
3313 		ret = set_mtpt_pbl(hr_dev, mpt_entry, mr);
3314 	}
3315 
3316 	return ret;
3317 }
3318 
3319 static int hns_roce_v2_frmr_write_mtpt(struct hns_roce_dev *hr_dev,
3320 				       void *mb_buf, struct hns_roce_mr *mr)
3321 {
3322 	struct ib_device *ibdev = &hr_dev->ib_dev;
3323 	struct hns_roce_v2_mpt_entry *mpt_entry;
3324 	dma_addr_t pbl_ba = 0;
3325 
3326 	mpt_entry = mb_buf;
3327 	memset(mpt_entry, 0, sizeof(*mpt_entry));
3328 
3329 	if (hns_roce_mtr_find(hr_dev, &mr->pbl_mtr, 0, NULL, 0, &pbl_ba) < 0) {
3330 		ibdev_err(ibdev, "failed to find frmr mtr.\n");
3331 		return -ENOBUFS;
3332 	}
3333 
3334 	hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_FREE);
3335 	hr_reg_write(mpt_entry, MPT_PD, mr->pd);
3336 
3337 	hr_reg_enable(mpt_entry, MPT_RA_EN);
3338 	hr_reg_enable(mpt_entry, MPT_R_INV_EN);
3339 
3340 	hr_reg_enable(mpt_entry, MPT_FRE);
3341 	hr_reg_clear(mpt_entry, MPT_MR_MW);
3342 	hr_reg_enable(mpt_entry, MPT_BPD);
3343 	hr_reg_clear(mpt_entry, MPT_PA);
3344 
3345 	hr_reg_write(mpt_entry, MPT_PBL_HOP_NUM, 1);
3346 	hr_reg_write(mpt_entry, MPT_PBL_BA_PG_SZ,
3347 		     to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.ba_pg_shift));
3348 	hr_reg_write(mpt_entry, MPT_PBL_BUF_PG_SZ,
3349 		     to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.buf_pg_shift));
3350 
3351 	mpt_entry->pbl_size = cpu_to_le32(mr->npages);
3352 
3353 	mpt_entry->pbl_ba_l = cpu_to_le32(lower_32_bits(pbl_ba >> 3));
3354 	hr_reg_write(mpt_entry, MPT_PBL_BA_H, upper_32_bits(pbl_ba >> 3));
3355 
3356 	return 0;
3357 }
3358 
3359 static int hns_roce_v2_mw_write_mtpt(void *mb_buf, struct hns_roce_mw *mw)
3360 {
3361 	struct hns_roce_v2_mpt_entry *mpt_entry;
3362 
3363 	mpt_entry = mb_buf;
3364 	memset(mpt_entry, 0, sizeof(*mpt_entry));
3365 
3366 	hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_FREE);
3367 	hr_reg_write(mpt_entry, MPT_PD, mw->pdn);
3368 
3369 	hr_reg_enable(mpt_entry, MPT_R_INV_EN);
3370 	hr_reg_enable(mpt_entry, MPT_LW_EN);
3371 
3372 	hr_reg_enable(mpt_entry, MPT_MR_MW);
3373 	hr_reg_enable(mpt_entry, MPT_BPD);
3374 	hr_reg_clear(mpt_entry, MPT_PA);
3375 	hr_reg_write(mpt_entry, MPT_BQP,
3376 		     mw->ibmw.type == IB_MW_TYPE_1 ? 0 : 1);
3377 
3378 	mpt_entry->lkey = cpu_to_le32(mw->rkey);
3379 
3380 	hr_reg_write(mpt_entry, MPT_PBL_HOP_NUM,
3381 		     mw->pbl_hop_num == HNS_ROCE_HOP_NUM_0 ? 0 :
3382 							     mw->pbl_hop_num);
3383 	hr_reg_write(mpt_entry, MPT_PBL_BA_PG_SZ,
3384 		     mw->pbl_ba_pg_sz + PG_SHIFT_OFFSET);
3385 	hr_reg_write(mpt_entry, MPT_PBL_BUF_PG_SZ,
3386 		     mw->pbl_buf_pg_sz + PG_SHIFT_OFFSET);
3387 
3388 	return 0;
3389 }
3390 
3391 static int free_mr_post_send_lp_wqe(struct hns_roce_qp *hr_qp)
3392 {
3393 	struct hns_roce_dev *hr_dev = to_hr_dev(hr_qp->ibqp.device);
3394 	struct ib_device *ibdev = &hr_dev->ib_dev;
3395 	const struct ib_send_wr *bad_wr;
3396 	struct ib_rdma_wr rdma_wr = {};
3397 	struct ib_send_wr *send_wr;
3398 	int ret;
3399 
3400 	send_wr = &rdma_wr.wr;
3401 	send_wr->opcode = IB_WR_RDMA_WRITE;
3402 
3403 	ret = hns_roce_v2_post_send(&hr_qp->ibqp, send_wr, &bad_wr);
3404 	if (ret) {
3405 		ibdev_err(ibdev, "failed to post wqe for free mr, ret = %d.\n",
3406 			  ret);
3407 		return ret;
3408 	}
3409 
3410 	return 0;
3411 }
3412 
3413 static int hns_roce_v2_poll_cq(struct ib_cq *ibcq, int num_entries,
3414 			       struct ib_wc *wc);
3415 
3416 static void free_mr_send_cmd_to_hw(struct hns_roce_dev *hr_dev)
3417 {
3418 	struct hns_roce_v2_priv *priv = hr_dev->priv;
3419 	struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
3420 	struct ib_wc wc[ARRAY_SIZE(free_mr->rsv_qp)];
3421 	struct ib_device *ibdev = &hr_dev->ib_dev;
3422 	struct hns_roce_qp *hr_qp;
3423 	unsigned long end;
3424 	int cqe_cnt = 0;
3425 	int npolled;
3426 	int ret;
3427 	int i;
3428 
3429 	/*
3430 	 * If the device initialization is not complete or in the uninstall
3431 	 * process, then there is no need to execute free mr.
3432 	 */
3433 	if (priv->handle->rinfo.reset_state == HNS_ROCE_STATE_RST_INIT ||
3434 	    priv->handle->rinfo.instance_state == HNS_ROCE_STATE_INIT ||
3435 	    hr_dev->state == HNS_ROCE_DEVICE_STATE_UNINIT)
3436 		return;
3437 
3438 	mutex_lock(&free_mr->mutex);
3439 
3440 	for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
3441 		hr_qp = free_mr->rsv_qp[i];
3442 
3443 		ret = free_mr_post_send_lp_wqe(hr_qp);
3444 		if (ret) {
3445 			ibdev_err(ibdev,
3446 				  "failed to send wqe (qp:0x%lx) for free mr, ret = %d.\n",
3447 				  hr_qp->qpn, ret);
3448 			break;
3449 		}
3450 
3451 		cqe_cnt++;
3452 	}
3453 
3454 	end = msecs_to_jiffies(HNS_ROCE_V2_FREE_MR_TIMEOUT) + jiffies;
3455 	while (cqe_cnt) {
3456 		npolled = hns_roce_v2_poll_cq(&free_mr->rsv_cq->ib_cq, cqe_cnt, wc);
3457 		if (npolled < 0) {
3458 			ibdev_err(ibdev,
3459 				  "failed to poll cqe for free mr, remain %d cqe.\n",
3460 				  cqe_cnt);
3461 			goto out;
3462 		}
3463 
3464 		if (time_after(jiffies, end)) {
3465 			ibdev_err(ibdev,
3466 				  "failed to poll cqe for free mr and timeout, remain %d cqe.\n",
3467 				  cqe_cnt);
3468 			goto out;
3469 		}
3470 		cqe_cnt -= npolled;
3471 	}
3472 
3473 out:
3474 	mutex_unlock(&free_mr->mutex);
3475 }
3476 
3477 static void hns_roce_v2_dereg_mr(struct hns_roce_dev *hr_dev)
3478 {
3479 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
3480 		free_mr_send_cmd_to_hw(hr_dev);
3481 }
3482 
3483 static void *get_cqe_v2(struct hns_roce_cq *hr_cq, int n)
3484 {
3485 	return hns_roce_buf_offset(hr_cq->mtr.kmem, n * hr_cq->cqe_size);
3486 }
3487 
3488 static void *get_sw_cqe_v2(struct hns_roce_cq *hr_cq, unsigned int n)
3489 {
3490 	struct hns_roce_v2_cqe *cqe = get_cqe_v2(hr_cq, n & hr_cq->ib_cq.cqe);
3491 
3492 	/* Get cqe when Owner bit is Conversely with the MSB of cons_idx */
3493 	return (hr_reg_read(cqe, CQE_OWNER) ^ !!(n & hr_cq->cq_depth)) ? cqe :
3494 									 NULL;
3495 }
3496 
3497 static inline void update_cq_db(struct hns_roce_dev *hr_dev,
3498 				struct hns_roce_cq *hr_cq)
3499 {
3500 	if (likely(hr_cq->flags & HNS_ROCE_CQ_FLAG_RECORD_DB)) {
3501 		*hr_cq->set_ci_db = hr_cq->cons_index & V2_CQ_DB_CONS_IDX_M;
3502 	} else {
3503 		struct hns_roce_v2_db cq_db = {};
3504 
3505 		hr_reg_write(&cq_db, DB_TAG, hr_cq->cqn);
3506 		hr_reg_write(&cq_db, DB_CMD, HNS_ROCE_V2_CQ_DB);
3507 		hr_reg_write(&cq_db, DB_CQ_CI, hr_cq->cons_index);
3508 		hr_reg_write(&cq_db, DB_CQ_CMD_SN, 1);
3509 
3510 		hns_roce_write64(hr_dev, (__le32 *)&cq_db, hr_cq->db_reg);
3511 	}
3512 }
3513 
3514 static void __hns_roce_v2_cq_clean(struct hns_roce_cq *hr_cq, u32 qpn,
3515 				   struct hns_roce_srq *srq)
3516 {
3517 	struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
3518 	struct hns_roce_v2_cqe *cqe, *dest;
3519 	u32 prod_index;
3520 	int nfreed = 0;
3521 	int wqe_index;
3522 	u8 owner_bit;
3523 
3524 	for (prod_index = hr_cq->cons_index; get_sw_cqe_v2(hr_cq, prod_index);
3525 	     ++prod_index) {
3526 		if (prod_index > hr_cq->cons_index + hr_cq->ib_cq.cqe)
3527 			break;
3528 	}
3529 
3530 	/*
3531 	 * Now backwards through the CQ, removing CQ entries
3532 	 * that match our QP by overwriting them with next entries.
3533 	 */
3534 	while ((int) --prod_index - (int) hr_cq->cons_index >= 0) {
3535 		cqe = get_cqe_v2(hr_cq, prod_index & hr_cq->ib_cq.cqe);
3536 		if (hr_reg_read(cqe, CQE_LCL_QPN) == qpn) {
3537 			if (srq && hr_reg_read(cqe, CQE_S_R)) {
3538 				wqe_index = hr_reg_read(cqe, CQE_WQE_IDX);
3539 				hns_roce_free_srq_wqe(srq, wqe_index);
3540 			}
3541 			++nfreed;
3542 		} else if (nfreed) {
3543 			dest = get_cqe_v2(hr_cq, (prod_index + nfreed) &
3544 					  hr_cq->ib_cq.cqe);
3545 			owner_bit = hr_reg_read(dest, CQE_OWNER);
3546 			memcpy(dest, cqe, hr_cq->cqe_size);
3547 			hr_reg_write(dest, CQE_OWNER, owner_bit);
3548 		}
3549 	}
3550 
3551 	if (nfreed) {
3552 		hr_cq->cons_index += nfreed;
3553 		update_cq_db(hr_dev, hr_cq);
3554 	}
3555 }
3556 
3557 static void hns_roce_v2_cq_clean(struct hns_roce_cq *hr_cq, u32 qpn,
3558 				 struct hns_roce_srq *srq)
3559 {
3560 	spin_lock_irq(&hr_cq->lock);
3561 	__hns_roce_v2_cq_clean(hr_cq, qpn, srq);
3562 	spin_unlock_irq(&hr_cq->lock);
3563 }
3564 
3565 static void hns_roce_v2_write_cqc(struct hns_roce_dev *hr_dev,
3566 				  struct hns_roce_cq *hr_cq, void *mb_buf,
3567 				  u64 *mtts, dma_addr_t dma_handle)
3568 {
3569 	struct hns_roce_v2_cq_context *cq_context;
3570 
3571 	cq_context = mb_buf;
3572 	memset(cq_context, 0, sizeof(*cq_context));
3573 
3574 	hr_reg_write(cq_context, CQC_CQ_ST, V2_CQ_STATE_VALID);
3575 	hr_reg_write(cq_context, CQC_ARM_ST, NO_ARMED);
3576 	hr_reg_write(cq_context, CQC_SHIFT, ilog2(hr_cq->cq_depth));
3577 	hr_reg_write(cq_context, CQC_CEQN, hr_cq->vector);
3578 	hr_reg_write(cq_context, CQC_CQN, hr_cq->cqn);
3579 
3580 	if (hr_cq->cqe_size == HNS_ROCE_V3_CQE_SIZE)
3581 		hr_reg_write(cq_context, CQC_CQE_SIZE, CQE_SIZE_64B);
3582 
3583 	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_STASH)
3584 		hr_reg_enable(cq_context, CQC_STASH);
3585 
3586 	hr_reg_write(cq_context, CQC_CQE_CUR_BLK_ADDR_L,
3587 		     to_hr_hw_page_addr(mtts[0]));
3588 	hr_reg_write(cq_context, CQC_CQE_CUR_BLK_ADDR_H,
3589 		     upper_32_bits(to_hr_hw_page_addr(mtts[0])));
3590 	hr_reg_write(cq_context, CQC_CQE_HOP_NUM, hr_dev->caps.cqe_hop_num ==
3591 		     HNS_ROCE_HOP_NUM_0 ? 0 : hr_dev->caps.cqe_hop_num);
3592 	hr_reg_write(cq_context, CQC_CQE_NEX_BLK_ADDR_L,
3593 		     to_hr_hw_page_addr(mtts[1]));
3594 	hr_reg_write(cq_context, CQC_CQE_NEX_BLK_ADDR_H,
3595 		     upper_32_bits(to_hr_hw_page_addr(mtts[1])));
3596 	hr_reg_write(cq_context, CQC_CQE_BAR_PG_SZ,
3597 		     to_hr_hw_page_shift(hr_cq->mtr.hem_cfg.ba_pg_shift));
3598 	hr_reg_write(cq_context, CQC_CQE_BUF_PG_SZ,
3599 		     to_hr_hw_page_shift(hr_cq->mtr.hem_cfg.buf_pg_shift));
3600 	hr_reg_write(cq_context, CQC_CQE_BA_L, dma_handle >> 3);
3601 	hr_reg_write(cq_context, CQC_CQE_BA_H, (dma_handle >> (32 + 3)));
3602 	hr_reg_write_bool(cq_context, CQC_DB_RECORD_EN,
3603 			  hr_cq->flags & HNS_ROCE_CQ_FLAG_RECORD_DB);
3604 	hr_reg_write(cq_context, CQC_CQE_DB_RECORD_ADDR_L,
3605 		     ((u32)hr_cq->db.dma) >> 1);
3606 	hr_reg_write(cq_context, CQC_CQE_DB_RECORD_ADDR_H,
3607 		     hr_cq->db.dma >> 32);
3608 	hr_reg_write(cq_context, CQC_CQ_MAX_CNT,
3609 		     HNS_ROCE_V2_CQ_DEFAULT_BURST_NUM);
3610 	hr_reg_write(cq_context, CQC_CQ_PERIOD,
3611 		     HNS_ROCE_V2_CQ_DEFAULT_INTERVAL);
3612 }
3613 
3614 static int hns_roce_v2_req_notify_cq(struct ib_cq *ibcq,
3615 				     enum ib_cq_notify_flags flags)
3616 {
3617 	struct hns_roce_dev *hr_dev = to_hr_dev(ibcq->device);
3618 	struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
3619 	struct hns_roce_v2_db cq_db = {};
3620 	u32 notify_flag;
3621 
3622 	/*
3623 	 * flags = 0, then notify_flag : next
3624 	 * flags = 1, then notify flag : solocited
3625 	 */
3626 	notify_flag = (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
3627 		      V2_CQ_DB_REQ_NOT : V2_CQ_DB_REQ_NOT_SOL;
3628 
3629 	hr_reg_write(&cq_db, DB_TAG, hr_cq->cqn);
3630 	hr_reg_write(&cq_db, DB_CMD, HNS_ROCE_V2_CQ_DB_NOTIFY);
3631 	hr_reg_write(&cq_db, DB_CQ_CI, hr_cq->cons_index);
3632 	hr_reg_write(&cq_db, DB_CQ_CMD_SN, hr_cq->arm_sn);
3633 	hr_reg_write(&cq_db, DB_CQ_NOTIFY, notify_flag);
3634 
3635 	hns_roce_write64(hr_dev, (__le32 *)&cq_db, hr_cq->db_reg);
3636 
3637 	return 0;
3638 }
3639 
3640 static int sw_comp(struct hns_roce_qp *hr_qp, struct hns_roce_wq *wq,
3641 		   int num_entries, struct ib_wc *wc)
3642 {
3643 	unsigned int left;
3644 	int npolled = 0;
3645 
3646 	left = wq->head - wq->tail;
3647 	if (left == 0)
3648 		return 0;
3649 
3650 	left = min_t(unsigned int, (unsigned int)num_entries, left);
3651 	while (npolled < left) {
3652 		wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
3653 		wc->status = IB_WC_WR_FLUSH_ERR;
3654 		wc->vendor_err = 0;
3655 		wc->qp = &hr_qp->ibqp;
3656 
3657 		wq->tail++;
3658 		wc++;
3659 		npolled++;
3660 	}
3661 
3662 	return npolled;
3663 }
3664 
3665 static int hns_roce_v2_sw_poll_cq(struct hns_roce_cq *hr_cq, int num_entries,
3666 				  struct ib_wc *wc)
3667 {
3668 	struct hns_roce_qp *hr_qp;
3669 	int npolled = 0;
3670 
3671 	list_for_each_entry(hr_qp, &hr_cq->sq_list, sq_node) {
3672 		npolled += sw_comp(hr_qp, &hr_qp->sq,
3673 				   num_entries - npolled, wc + npolled);
3674 		if (npolled >= num_entries)
3675 			goto out;
3676 	}
3677 
3678 	list_for_each_entry(hr_qp, &hr_cq->rq_list, rq_node) {
3679 		npolled += sw_comp(hr_qp, &hr_qp->rq,
3680 				   num_entries - npolled, wc + npolled);
3681 		if (npolled >= num_entries)
3682 			goto out;
3683 	}
3684 
3685 out:
3686 	return npolled;
3687 }
3688 
3689 static void get_cqe_status(struct hns_roce_dev *hr_dev, struct hns_roce_qp *qp,
3690 			   struct hns_roce_cq *cq, struct hns_roce_v2_cqe *cqe,
3691 			   struct ib_wc *wc)
3692 {
3693 	static const struct {
3694 		u32 cqe_status;
3695 		enum ib_wc_status wc_status;
3696 	} map[] = {
3697 		{ HNS_ROCE_CQE_V2_SUCCESS, IB_WC_SUCCESS },
3698 		{ HNS_ROCE_CQE_V2_LOCAL_LENGTH_ERR, IB_WC_LOC_LEN_ERR },
3699 		{ HNS_ROCE_CQE_V2_LOCAL_QP_OP_ERR, IB_WC_LOC_QP_OP_ERR },
3700 		{ HNS_ROCE_CQE_V2_LOCAL_PROT_ERR, IB_WC_LOC_PROT_ERR },
3701 		{ HNS_ROCE_CQE_V2_WR_FLUSH_ERR, IB_WC_WR_FLUSH_ERR },
3702 		{ HNS_ROCE_CQE_V2_MW_BIND_ERR, IB_WC_MW_BIND_ERR },
3703 		{ HNS_ROCE_CQE_V2_BAD_RESP_ERR, IB_WC_BAD_RESP_ERR },
3704 		{ HNS_ROCE_CQE_V2_LOCAL_ACCESS_ERR, IB_WC_LOC_ACCESS_ERR },
3705 		{ HNS_ROCE_CQE_V2_REMOTE_INVAL_REQ_ERR, IB_WC_REM_INV_REQ_ERR },
3706 		{ HNS_ROCE_CQE_V2_REMOTE_ACCESS_ERR, IB_WC_REM_ACCESS_ERR },
3707 		{ HNS_ROCE_CQE_V2_REMOTE_OP_ERR, IB_WC_REM_OP_ERR },
3708 		{ HNS_ROCE_CQE_V2_TRANSPORT_RETRY_EXC_ERR,
3709 		  IB_WC_RETRY_EXC_ERR },
3710 		{ HNS_ROCE_CQE_V2_RNR_RETRY_EXC_ERR, IB_WC_RNR_RETRY_EXC_ERR },
3711 		{ HNS_ROCE_CQE_V2_REMOTE_ABORT_ERR, IB_WC_REM_ABORT_ERR },
3712 		{ HNS_ROCE_CQE_V2_GENERAL_ERR, IB_WC_GENERAL_ERR}
3713 	};
3714 
3715 	u32 cqe_status = hr_reg_read(cqe, CQE_STATUS);
3716 	int i;
3717 
3718 	wc->status = IB_WC_GENERAL_ERR;
3719 	for (i = 0; i < ARRAY_SIZE(map); i++)
3720 		if (cqe_status == map[i].cqe_status) {
3721 			wc->status = map[i].wc_status;
3722 			break;
3723 		}
3724 
3725 	if (likely(wc->status == IB_WC_SUCCESS ||
3726 		   wc->status == IB_WC_WR_FLUSH_ERR))
3727 		return;
3728 
3729 	ibdev_err(&hr_dev->ib_dev, "error cqe status 0x%x:\n", cqe_status);
3730 	print_hex_dump(KERN_ERR, "", DUMP_PREFIX_NONE, 16, 4, cqe,
3731 		       cq->cqe_size, false);
3732 	wc->vendor_err = hr_reg_read(cqe, CQE_SUB_STATUS);
3733 
3734 	/*
3735 	 * For hns ROCEE, GENERAL_ERR is an error type that is not defined in
3736 	 * the standard protocol, the driver must ignore it and needn't to set
3737 	 * the QP to an error state.
3738 	 */
3739 	if (cqe_status == HNS_ROCE_CQE_V2_GENERAL_ERR)
3740 		return;
3741 
3742 	flush_cqe(hr_dev, qp);
3743 }
3744 
3745 static int get_cur_qp(struct hns_roce_cq *hr_cq, struct hns_roce_v2_cqe *cqe,
3746 		      struct hns_roce_qp **cur_qp)
3747 {
3748 	struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
3749 	struct hns_roce_qp *hr_qp = *cur_qp;
3750 	u32 qpn;
3751 
3752 	qpn = hr_reg_read(cqe, CQE_LCL_QPN);
3753 
3754 	if (!hr_qp || qpn != hr_qp->qpn) {
3755 		hr_qp = __hns_roce_qp_lookup(hr_dev, qpn);
3756 		if (unlikely(!hr_qp)) {
3757 			ibdev_err(&hr_dev->ib_dev,
3758 				  "CQ %06lx with entry for unknown QPN %06x\n",
3759 				  hr_cq->cqn, qpn);
3760 			return -EINVAL;
3761 		}
3762 		*cur_qp = hr_qp;
3763 	}
3764 
3765 	return 0;
3766 }
3767 
3768 /*
3769  * mapped-value = 1 + real-value
3770  * The ib wc opcode's real value is start from 0, In order to distinguish
3771  * between initialized and uninitialized map values, we plus 1 to the actual
3772  * value when defining the mapping, so that the validity can be identified by
3773  * checking whether the mapped value is greater than 0.
3774  */
3775 #define HR_WC_OP_MAP(hr_key, ib_key) \
3776 		[HNS_ROCE_V2_WQE_OP_ ## hr_key] = 1 + IB_WC_ ## ib_key
3777 
3778 static const u32 wc_send_op_map[] = {
3779 	HR_WC_OP_MAP(SEND,			SEND),
3780 	HR_WC_OP_MAP(SEND_WITH_INV,		SEND),
3781 	HR_WC_OP_MAP(SEND_WITH_IMM,		SEND),
3782 	HR_WC_OP_MAP(RDMA_READ,			RDMA_READ),
3783 	HR_WC_OP_MAP(RDMA_WRITE,		RDMA_WRITE),
3784 	HR_WC_OP_MAP(RDMA_WRITE_WITH_IMM,	RDMA_WRITE),
3785 	HR_WC_OP_MAP(ATOM_CMP_AND_SWAP,		COMP_SWAP),
3786 	HR_WC_OP_MAP(ATOM_FETCH_AND_ADD,	FETCH_ADD),
3787 	HR_WC_OP_MAP(ATOM_MSK_CMP_AND_SWAP,	MASKED_COMP_SWAP),
3788 	HR_WC_OP_MAP(ATOM_MSK_FETCH_AND_ADD,	MASKED_FETCH_ADD),
3789 	HR_WC_OP_MAP(FAST_REG_PMR,		REG_MR),
3790 	HR_WC_OP_MAP(BIND_MW,			REG_MR),
3791 };
3792 
3793 static int to_ib_wc_send_op(u32 hr_opcode)
3794 {
3795 	if (hr_opcode >= ARRAY_SIZE(wc_send_op_map))
3796 		return -EINVAL;
3797 
3798 	return wc_send_op_map[hr_opcode] ? wc_send_op_map[hr_opcode] - 1 :
3799 					   -EINVAL;
3800 }
3801 
3802 static const u32 wc_recv_op_map[] = {
3803 	HR_WC_OP_MAP(RDMA_WRITE_WITH_IMM,		WITH_IMM),
3804 	HR_WC_OP_MAP(SEND,				RECV),
3805 	HR_WC_OP_MAP(SEND_WITH_IMM,			WITH_IMM),
3806 	HR_WC_OP_MAP(SEND_WITH_INV,			RECV),
3807 };
3808 
3809 static int to_ib_wc_recv_op(u32 hr_opcode)
3810 {
3811 	if (hr_opcode >= ARRAY_SIZE(wc_recv_op_map))
3812 		return -EINVAL;
3813 
3814 	return wc_recv_op_map[hr_opcode] ? wc_recv_op_map[hr_opcode] - 1 :
3815 					   -EINVAL;
3816 }
3817 
3818 static void fill_send_wc(struct ib_wc *wc, struct hns_roce_v2_cqe *cqe)
3819 {
3820 	u32 hr_opcode;
3821 	int ib_opcode;
3822 
3823 	wc->wc_flags = 0;
3824 
3825 	hr_opcode = hr_reg_read(cqe, CQE_OPCODE);
3826 	switch (hr_opcode) {
3827 	case HNS_ROCE_V2_WQE_OP_RDMA_READ:
3828 		wc->byte_len = le32_to_cpu(cqe->byte_cnt);
3829 		break;
3830 	case HNS_ROCE_V2_WQE_OP_SEND_WITH_IMM:
3831 	case HNS_ROCE_V2_WQE_OP_RDMA_WRITE_WITH_IMM:
3832 		wc->wc_flags |= IB_WC_WITH_IMM;
3833 		break;
3834 	case HNS_ROCE_V2_WQE_OP_ATOM_CMP_AND_SWAP:
3835 	case HNS_ROCE_V2_WQE_OP_ATOM_FETCH_AND_ADD:
3836 	case HNS_ROCE_V2_WQE_OP_ATOM_MSK_CMP_AND_SWAP:
3837 	case HNS_ROCE_V2_WQE_OP_ATOM_MSK_FETCH_AND_ADD:
3838 		wc->byte_len  = 8;
3839 		break;
3840 	default:
3841 		break;
3842 	}
3843 
3844 	ib_opcode = to_ib_wc_send_op(hr_opcode);
3845 	if (ib_opcode < 0)
3846 		wc->status = IB_WC_GENERAL_ERR;
3847 	else
3848 		wc->opcode = ib_opcode;
3849 }
3850 
3851 static int fill_recv_wc(struct ib_wc *wc, struct hns_roce_v2_cqe *cqe)
3852 {
3853 	u32 hr_opcode;
3854 	int ib_opcode;
3855 
3856 	wc->byte_len = le32_to_cpu(cqe->byte_cnt);
3857 
3858 	hr_opcode = hr_reg_read(cqe, CQE_OPCODE);
3859 	switch (hr_opcode) {
3860 	case HNS_ROCE_V2_OPCODE_RDMA_WRITE_IMM:
3861 	case HNS_ROCE_V2_OPCODE_SEND_WITH_IMM:
3862 		wc->wc_flags = IB_WC_WITH_IMM;
3863 		wc->ex.imm_data = cpu_to_be32(le32_to_cpu(cqe->immtdata));
3864 		break;
3865 	case HNS_ROCE_V2_OPCODE_SEND_WITH_INV:
3866 		wc->wc_flags = IB_WC_WITH_INVALIDATE;
3867 		wc->ex.invalidate_rkey = le32_to_cpu(cqe->rkey);
3868 		break;
3869 	default:
3870 		wc->wc_flags = 0;
3871 	}
3872 
3873 	ib_opcode = to_ib_wc_recv_op(hr_opcode);
3874 	if (ib_opcode < 0)
3875 		wc->status = IB_WC_GENERAL_ERR;
3876 	else
3877 		wc->opcode = ib_opcode;
3878 
3879 	wc->sl = hr_reg_read(cqe, CQE_SL);
3880 	wc->src_qp = hr_reg_read(cqe, CQE_RMT_QPN);
3881 	wc->slid = 0;
3882 	wc->wc_flags |= hr_reg_read(cqe, CQE_GRH) ? IB_WC_GRH : 0;
3883 	wc->port_num = hr_reg_read(cqe, CQE_PORTN);
3884 	wc->pkey_index = 0;
3885 
3886 	if (hr_reg_read(cqe, CQE_VID_VLD)) {
3887 		wc->vlan_id = hr_reg_read(cqe, CQE_VID);
3888 		wc->wc_flags |= IB_WC_WITH_VLAN;
3889 	} else {
3890 		wc->vlan_id = 0xffff;
3891 	}
3892 
3893 	wc->network_hdr_type = hr_reg_read(cqe, CQE_PORT_TYPE);
3894 
3895 	return 0;
3896 }
3897 
3898 static int hns_roce_v2_poll_one(struct hns_roce_cq *hr_cq,
3899 				struct hns_roce_qp **cur_qp, struct ib_wc *wc)
3900 {
3901 	struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
3902 	struct hns_roce_qp *qp = *cur_qp;
3903 	struct hns_roce_srq *srq = NULL;
3904 	struct hns_roce_v2_cqe *cqe;
3905 	struct hns_roce_wq *wq;
3906 	int is_send;
3907 	u16 wqe_idx;
3908 	int ret;
3909 
3910 	cqe = get_sw_cqe_v2(hr_cq, hr_cq->cons_index);
3911 	if (!cqe)
3912 		return -EAGAIN;
3913 
3914 	++hr_cq->cons_index;
3915 	/* Memory barrier */
3916 	rmb();
3917 
3918 	ret = get_cur_qp(hr_cq, cqe, &qp);
3919 	if (ret)
3920 		return ret;
3921 
3922 	wc->qp = &qp->ibqp;
3923 	wc->vendor_err = 0;
3924 
3925 	wqe_idx = hr_reg_read(cqe, CQE_WQE_IDX);
3926 
3927 	is_send = !hr_reg_read(cqe, CQE_S_R);
3928 	if (is_send) {
3929 		wq = &qp->sq;
3930 
3931 		/* If sg_signal_bit is set, tail pointer will be updated to
3932 		 * the WQE corresponding to the current CQE.
3933 		 */
3934 		if (qp->sq_signal_bits)
3935 			wq->tail += (wqe_idx - (u16)wq->tail) &
3936 				    (wq->wqe_cnt - 1);
3937 
3938 		wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
3939 		++wq->tail;
3940 
3941 		fill_send_wc(wc, cqe);
3942 	} else {
3943 		if (qp->ibqp.srq) {
3944 			srq = to_hr_srq(qp->ibqp.srq);
3945 			wc->wr_id = srq->wrid[wqe_idx];
3946 			hns_roce_free_srq_wqe(srq, wqe_idx);
3947 		} else {
3948 			wq = &qp->rq;
3949 			wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
3950 			++wq->tail;
3951 		}
3952 
3953 		ret = fill_recv_wc(wc, cqe);
3954 	}
3955 
3956 	get_cqe_status(hr_dev, qp, hr_cq, cqe, wc);
3957 	if (unlikely(wc->status != IB_WC_SUCCESS))
3958 		return 0;
3959 
3960 	return ret;
3961 }
3962 
3963 static int hns_roce_v2_poll_cq(struct ib_cq *ibcq, int num_entries,
3964 			       struct ib_wc *wc)
3965 {
3966 	struct hns_roce_dev *hr_dev = to_hr_dev(ibcq->device);
3967 	struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
3968 	struct hns_roce_qp *cur_qp = NULL;
3969 	unsigned long flags;
3970 	int npolled;
3971 
3972 	spin_lock_irqsave(&hr_cq->lock, flags);
3973 
3974 	/*
3975 	 * When the device starts to reset, the state is RST_DOWN. At this time,
3976 	 * there may still be some valid CQEs in the hardware that are not
3977 	 * polled. Therefore, it is not allowed to switch to the software mode
3978 	 * immediately. When the state changes to UNINIT, CQE no longer exists
3979 	 * in the hardware, and then switch to software mode.
3980 	 */
3981 	if (hr_dev->state == HNS_ROCE_DEVICE_STATE_UNINIT) {
3982 		npolled = hns_roce_v2_sw_poll_cq(hr_cq, num_entries, wc);
3983 		goto out;
3984 	}
3985 
3986 	for (npolled = 0; npolled < num_entries; ++npolled) {
3987 		if (hns_roce_v2_poll_one(hr_cq, &cur_qp, wc + npolled))
3988 			break;
3989 	}
3990 
3991 	if (npolled)
3992 		update_cq_db(hr_dev, hr_cq);
3993 
3994 out:
3995 	spin_unlock_irqrestore(&hr_cq->lock, flags);
3996 
3997 	return npolled;
3998 }
3999 
4000 static int get_op_for_set_hem(struct hns_roce_dev *hr_dev, u32 type,
4001 			      u32 step_idx, u8 *mbox_cmd)
4002 {
4003 	u8 cmd;
4004 
4005 	switch (type) {
4006 	case HEM_TYPE_QPC:
4007 		cmd = HNS_ROCE_CMD_WRITE_QPC_BT0;
4008 		break;
4009 	case HEM_TYPE_MTPT:
4010 		cmd = HNS_ROCE_CMD_WRITE_MPT_BT0;
4011 		break;
4012 	case HEM_TYPE_CQC:
4013 		cmd = HNS_ROCE_CMD_WRITE_CQC_BT0;
4014 		break;
4015 	case HEM_TYPE_SRQC:
4016 		cmd = HNS_ROCE_CMD_WRITE_SRQC_BT0;
4017 		break;
4018 	case HEM_TYPE_SCCC:
4019 		cmd = HNS_ROCE_CMD_WRITE_SCCC_BT0;
4020 		break;
4021 	case HEM_TYPE_QPC_TIMER:
4022 		cmd = HNS_ROCE_CMD_WRITE_QPC_TIMER_BT0;
4023 		break;
4024 	case HEM_TYPE_CQC_TIMER:
4025 		cmd = HNS_ROCE_CMD_WRITE_CQC_TIMER_BT0;
4026 		break;
4027 	default:
4028 		dev_warn(hr_dev->dev, "failed to check hem type %u.\n", type);
4029 		return -EINVAL;
4030 	}
4031 
4032 	*mbox_cmd = cmd + step_idx;
4033 
4034 	return 0;
4035 }
4036 
4037 static int config_gmv_ba_to_hw(struct hns_roce_dev *hr_dev, unsigned long obj,
4038 			       dma_addr_t base_addr)
4039 {
4040 	struct hns_roce_cmq_desc desc;
4041 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
4042 	u32 idx = obj / (HNS_HW_PAGE_SIZE / hr_dev->caps.gmv_entry_sz);
4043 	u64 addr = to_hr_hw_page_addr(base_addr);
4044 
4045 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GMV_BT, false);
4046 
4047 	hr_reg_write(req, CFG_GMV_BT_BA_L, lower_32_bits(addr));
4048 	hr_reg_write(req, CFG_GMV_BT_BA_H, upper_32_bits(addr));
4049 	hr_reg_write(req, CFG_GMV_BT_IDX, idx);
4050 
4051 	return hns_roce_cmq_send(hr_dev, &desc, 1);
4052 }
4053 
4054 static int set_hem_to_hw(struct hns_roce_dev *hr_dev, int obj,
4055 			 dma_addr_t base_addr, u32 hem_type, u32 step_idx)
4056 {
4057 	int ret;
4058 	u8 cmd;
4059 
4060 	if (unlikely(hem_type == HEM_TYPE_GMV))
4061 		return config_gmv_ba_to_hw(hr_dev, obj, base_addr);
4062 
4063 	if (unlikely(hem_type == HEM_TYPE_SCCC && step_idx))
4064 		return 0;
4065 
4066 	ret = get_op_for_set_hem(hr_dev, hem_type, step_idx, &cmd);
4067 	if (ret < 0)
4068 		return ret;
4069 
4070 	return config_hem_ba_to_hw(hr_dev, base_addr, cmd, obj);
4071 }
4072 
4073 static int hns_roce_v2_set_hem(struct hns_roce_dev *hr_dev,
4074 			       struct hns_roce_hem_table *table, int obj,
4075 			       u32 step_idx)
4076 {
4077 	struct hns_roce_hem_iter iter;
4078 	struct hns_roce_hem_mhop mhop;
4079 	struct hns_roce_hem *hem;
4080 	unsigned long mhop_obj = obj;
4081 	int i, j, k;
4082 	int ret = 0;
4083 	u64 hem_idx = 0;
4084 	u64 l1_idx = 0;
4085 	u64 bt_ba = 0;
4086 	u32 chunk_ba_num;
4087 	u32 hop_num;
4088 
4089 	if (!hns_roce_check_whether_mhop(hr_dev, table->type))
4090 		return 0;
4091 
4092 	hns_roce_calc_hem_mhop(hr_dev, table, &mhop_obj, &mhop);
4093 	i = mhop.l0_idx;
4094 	j = mhop.l1_idx;
4095 	k = mhop.l2_idx;
4096 	hop_num = mhop.hop_num;
4097 	chunk_ba_num = mhop.bt_chunk_size / 8;
4098 
4099 	if (hop_num == 2) {
4100 		hem_idx = i * chunk_ba_num * chunk_ba_num + j * chunk_ba_num +
4101 			  k;
4102 		l1_idx = i * chunk_ba_num + j;
4103 	} else if (hop_num == 1) {
4104 		hem_idx = i * chunk_ba_num + j;
4105 	} else if (hop_num == HNS_ROCE_HOP_NUM_0) {
4106 		hem_idx = i;
4107 	}
4108 
4109 	if (table->type == HEM_TYPE_SCCC)
4110 		obj = mhop.l0_idx;
4111 
4112 	if (check_whether_last_step(hop_num, step_idx)) {
4113 		hem = table->hem[hem_idx];
4114 		for (hns_roce_hem_first(hem, &iter);
4115 		     !hns_roce_hem_last(&iter); hns_roce_hem_next(&iter)) {
4116 			bt_ba = hns_roce_hem_addr(&iter);
4117 			ret = set_hem_to_hw(hr_dev, obj, bt_ba, table->type,
4118 					    step_idx);
4119 		}
4120 	} else {
4121 		if (step_idx == 0)
4122 			bt_ba = table->bt_l0_dma_addr[i];
4123 		else if (step_idx == 1 && hop_num == 2)
4124 			bt_ba = table->bt_l1_dma_addr[l1_idx];
4125 
4126 		ret = set_hem_to_hw(hr_dev, obj, bt_ba, table->type, step_idx);
4127 	}
4128 
4129 	return ret;
4130 }
4131 
4132 static int hns_roce_v2_clear_hem(struct hns_roce_dev *hr_dev,
4133 				 struct hns_roce_hem_table *table,
4134 				 int tag, u32 step_idx)
4135 {
4136 	struct hns_roce_cmd_mailbox *mailbox;
4137 	struct device *dev = hr_dev->dev;
4138 	u8 cmd = 0xff;
4139 	int ret;
4140 
4141 	if (!hns_roce_check_whether_mhop(hr_dev, table->type))
4142 		return 0;
4143 
4144 	switch (table->type) {
4145 	case HEM_TYPE_QPC:
4146 		cmd = HNS_ROCE_CMD_DESTROY_QPC_BT0;
4147 		break;
4148 	case HEM_TYPE_MTPT:
4149 		cmd = HNS_ROCE_CMD_DESTROY_MPT_BT0;
4150 		break;
4151 	case HEM_TYPE_CQC:
4152 		cmd = HNS_ROCE_CMD_DESTROY_CQC_BT0;
4153 		break;
4154 	case HEM_TYPE_SRQC:
4155 		cmd = HNS_ROCE_CMD_DESTROY_SRQC_BT0;
4156 		break;
4157 	case HEM_TYPE_SCCC:
4158 	case HEM_TYPE_QPC_TIMER:
4159 	case HEM_TYPE_CQC_TIMER:
4160 	case HEM_TYPE_GMV:
4161 		return 0;
4162 	default:
4163 		dev_warn(dev, "table %u not to be destroyed by mailbox!\n",
4164 			 table->type);
4165 		return 0;
4166 	}
4167 
4168 	cmd += step_idx;
4169 
4170 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
4171 	if (IS_ERR(mailbox))
4172 		return PTR_ERR(mailbox);
4173 
4174 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, cmd, tag);
4175 
4176 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
4177 	return ret;
4178 }
4179 
4180 static int hns_roce_v2_qp_modify(struct hns_roce_dev *hr_dev,
4181 				 struct hns_roce_v2_qp_context *context,
4182 				 struct hns_roce_v2_qp_context *qpc_mask,
4183 				 struct hns_roce_qp *hr_qp)
4184 {
4185 	struct hns_roce_cmd_mailbox *mailbox;
4186 	int qpc_size;
4187 	int ret;
4188 
4189 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
4190 	if (IS_ERR(mailbox))
4191 		return PTR_ERR(mailbox);
4192 
4193 	/* The qpc size of HIP08 is only 256B, which is half of HIP09 */
4194 	qpc_size = hr_dev->caps.qpc_sz;
4195 	memcpy(mailbox->buf, context, qpc_size);
4196 	memcpy(mailbox->buf + qpc_size, qpc_mask, qpc_size);
4197 
4198 	ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0,
4199 				HNS_ROCE_CMD_MODIFY_QPC, hr_qp->qpn);
4200 
4201 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
4202 
4203 	return ret;
4204 }
4205 
4206 static void set_access_flags(struct hns_roce_qp *hr_qp,
4207 			     struct hns_roce_v2_qp_context *context,
4208 			     struct hns_roce_v2_qp_context *qpc_mask,
4209 			     const struct ib_qp_attr *attr, int attr_mask)
4210 {
4211 	u8 dest_rd_atomic;
4212 	u32 access_flags;
4213 
4214 	dest_rd_atomic = (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) ?
4215 			 attr->max_dest_rd_atomic : hr_qp->resp_depth;
4216 
4217 	access_flags = (attr_mask & IB_QP_ACCESS_FLAGS) ?
4218 		       attr->qp_access_flags : hr_qp->atomic_rd_en;
4219 
4220 	if (!dest_rd_atomic)
4221 		access_flags &= IB_ACCESS_REMOTE_WRITE;
4222 
4223 	hr_reg_write_bool(context, QPC_RRE,
4224 			  access_flags & IB_ACCESS_REMOTE_READ);
4225 	hr_reg_clear(qpc_mask, QPC_RRE);
4226 
4227 	hr_reg_write_bool(context, QPC_RWE,
4228 			  access_flags & IB_ACCESS_REMOTE_WRITE);
4229 	hr_reg_clear(qpc_mask, QPC_RWE);
4230 
4231 	hr_reg_write_bool(context, QPC_ATE,
4232 			  access_flags & IB_ACCESS_REMOTE_ATOMIC);
4233 	hr_reg_clear(qpc_mask, QPC_ATE);
4234 	hr_reg_write_bool(context, QPC_EXT_ATE,
4235 			  access_flags & IB_ACCESS_REMOTE_ATOMIC);
4236 	hr_reg_clear(qpc_mask, QPC_EXT_ATE);
4237 }
4238 
4239 static void set_qpc_wqe_cnt(struct hns_roce_qp *hr_qp,
4240 			    struct hns_roce_v2_qp_context *context,
4241 			    struct hns_roce_v2_qp_context *qpc_mask)
4242 {
4243 	hr_reg_write(context, QPC_SGE_SHIFT,
4244 		     to_hr_hem_entries_shift(hr_qp->sge.sge_cnt,
4245 					     hr_qp->sge.sge_shift));
4246 
4247 	hr_reg_write(context, QPC_SQ_SHIFT, ilog2(hr_qp->sq.wqe_cnt));
4248 
4249 	hr_reg_write(context, QPC_RQ_SHIFT, ilog2(hr_qp->rq.wqe_cnt));
4250 }
4251 
4252 static inline int get_cqn(struct ib_cq *ib_cq)
4253 {
4254 	return ib_cq ? to_hr_cq(ib_cq)->cqn : 0;
4255 }
4256 
4257 static inline int get_pdn(struct ib_pd *ib_pd)
4258 {
4259 	return ib_pd ? to_hr_pd(ib_pd)->pdn : 0;
4260 }
4261 
4262 static void modify_qp_reset_to_init(struct ib_qp *ibqp,
4263 				    const struct ib_qp_attr *attr,
4264 				    struct hns_roce_v2_qp_context *context,
4265 				    struct hns_roce_v2_qp_context *qpc_mask)
4266 {
4267 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4268 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4269 
4270 	/*
4271 	 * In v2 engine, software pass context and context mask to hardware
4272 	 * when modifying qp. If software need modify some fields in context,
4273 	 * we should set all bits of the relevant fields in context mask to
4274 	 * 0 at the same time, else set them to 0x1.
4275 	 */
4276 	hr_reg_write(context, QPC_TST, to_hr_qp_type(ibqp->qp_type));
4277 
4278 	hr_reg_write(context, QPC_PD, get_pdn(ibqp->pd));
4279 
4280 	hr_reg_write(context, QPC_RQWS, ilog2(hr_qp->rq.max_gs));
4281 
4282 	set_qpc_wqe_cnt(hr_qp, context, qpc_mask);
4283 
4284 	/* No VLAN need to set 0xFFF */
4285 	hr_reg_write(context, QPC_VLAN_ID, 0xfff);
4286 
4287 	if (ibqp->qp_type == IB_QPT_XRC_TGT) {
4288 		context->qkey_xrcd = cpu_to_le32(hr_qp->xrcdn);
4289 
4290 		hr_reg_enable(context, QPC_XRC_QP_TYPE);
4291 	}
4292 
4293 	if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
4294 		hr_reg_enable(context, QPC_RQ_RECORD_EN);
4295 
4296 	if (hr_qp->en_flags & HNS_ROCE_QP_CAP_OWNER_DB)
4297 		hr_reg_enable(context, QPC_OWNER_MODE);
4298 
4299 	hr_reg_write(context, QPC_RQ_DB_RECORD_ADDR_L,
4300 		     lower_32_bits(hr_qp->rdb.dma) >> 1);
4301 	hr_reg_write(context, QPC_RQ_DB_RECORD_ADDR_H,
4302 		     upper_32_bits(hr_qp->rdb.dma));
4303 
4304 	hr_reg_write(context, QPC_RX_CQN, get_cqn(ibqp->recv_cq));
4305 
4306 	if (ibqp->srq) {
4307 		hr_reg_enable(context, QPC_SRQ_EN);
4308 		hr_reg_write(context, QPC_SRQN, to_hr_srq(ibqp->srq)->srqn);
4309 	}
4310 
4311 	hr_reg_enable(context, QPC_FRE);
4312 
4313 	hr_reg_write(context, QPC_TX_CQN, get_cqn(ibqp->send_cq));
4314 
4315 	if (hr_dev->caps.qpc_sz < HNS_ROCE_V3_QPC_SZ)
4316 		return;
4317 
4318 	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_STASH)
4319 		hr_reg_enable(&context->ext, QPCEX_STASH);
4320 }
4321 
4322 static void modify_qp_init_to_init(struct ib_qp *ibqp,
4323 				   const struct ib_qp_attr *attr,
4324 				   struct hns_roce_v2_qp_context *context,
4325 				   struct hns_roce_v2_qp_context *qpc_mask)
4326 {
4327 	/*
4328 	 * In v2 engine, software pass context and context mask to hardware
4329 	 * when modifying qp. If software need modify some fields in context,
4330 	 * we should set all bits of the relevant fields in context mask to
4331 	 * 0 at the same time, else set them to 0x1.
4332 	 */
4333 	hr_reg_write(context, QPC_TST, to_hr_qp_type(ibqp->qp_type));
4334 	hr_reg_clear(qpc_mask, QPC_TST);
4335 
4336 	hr_reg_write(context, QPC_PD, get_pdn(ibqp->pd));
4337 	hr_reg_clear(qpc_mask, QPC_PD);
4338 
4339 	hr_reg_write(context, QPC_RX_CQN, get_cqn(ibqp->recv_cq));
4340 	hr_reg_clear(qpc_mask, QPC_RX_CQN);
4341 
4342 	hr_reg_write(context, QPC_TX_CQN, get_cqn(ibqp->send_cq));
4343 	hr_reg_clear(qpc_mask, QPC_TX_CQN);
4344 
4345 	if (ibqp->srq) {
4346 		hr_reg_enable(context, QPC_SRQ_EN);
4347 		hr_reg_clear(qpc_mask, QPC_SRQ_EN);
4348 		hr_reg_write(context, QPC_SRQN, to_hr_srq(ibqp->srq)->srqn);
4349 		hr_reg_clear(qpc_mask, QPC_SRQN);
4350 	}
4351 }
4352 
4353 static int config_qp_rq_buf(struct hns_roce_dev *hr_dev,
4354 			    struct hns_roce_qp *hr_qp,
4355 			    struct hns_roce_v2_qp_context *context,
4356 			    struct hns_roce_v2_qp_context *qpc_mask)
4357 {
4358 	u64 mtts[MTT_MIN_COUNT] = { 0 };
4359 	u64 wqe_sge_ba;
4360 	int count;
4361 
4362 	/* Search qp buf's mtts */
4363 	count = hns_roce_mtr_find(hr_dev, &hr_qp->mtr, hr_qp->rq.offset, mtts,
4364 				  MTT_MIN_COUNT, &wqe_sge_ba);
4365 	if (hr_qp->rq.wqe_cnt && count < 1) {
4366 		ibdev_err(&hr_dev->ib_dev,
4367 			  "failed to find RQ WQE, QPN = 0x%lx.\n", hr_qp->qpn);
4368 		return -EINVAL;
4369 	}
4370 
4371 	context->wqe_sge_ba = cpu_to_le32(wqe_sge_ba >> 3);
4372 	qpc_mask->wqe_sge_ba = 0;
4373 
4374 	/*
4375 	 * In v2 engine, software pass context and context mask to hardware
4376 	 * when modifying qp. If software need modify some fields in context,
4377 	 * we should set all bits of the relevant fields in context mask to
4378 	 * 0 at the same time, else set them to 0x1.
4379 	 */
4380 	hr_reg_write(context, QPC_WQE_SGE_BA_H, wqe_sge_ba >> (32 + 3));
4381 	hr_reg_clear(qpc_mask, QPC_WQE_SGE_BA_H);
4382 
4383 	hr_reg_write(context, QPC_SQ_HOP_NUM,
4384 		     to_hr_hem_hopnum(hr_dev->caps.wqe_sq_hop_num,
4385 				      hr_qp->sq.wqe_cnt));
4386 	hr_reg_clear(qpc_mask, QPC_SQ_HOP_NUM);
4387 
4388 	hr_reg_write(context, QPC_SGE_HOP_NUM,
4389 		     to_hr_hem_hopnum(hr_dev->caps.wqe_sge_hop_num,
4390 				      hr_qp->sge.sge_cnt));
4391 	hr_reg_clear(qpc_mask, QPC_SGE_HOP_NUM);
4392 
4393 	hr_reg_write(context, QPC_RQ_HOP_NUM,
4394 		     to_hr_hem_hopnum(hr_dev->caps.wqe_rq_hop_num,
4395 				      hr_qp->rq.wqe_cnt));
4396 
4397 	hr_reg_clear(qpc_mask, QPC_RQ_HOP_NUM);
4398 
4399 	hr_reg_write(context, QPC_WQE_SGE_BA_PG_SZ,
4400 		     to_hr_hw_page_shift(hr_qp->mtr.hem_cfg.ba_pg_shift));
4401 	hr_reg_clear(qpc_mask, QPC_WQE_SGE_BA_PG_SZ);
4402 
4403 	hr_reg_write(context, QPC_WQE_SGE_BUF_PG_SZ,
4404 		     to_hr_hw_page_shift(hr_qp->mtr.hem_cfg.buf_pg_shift));
4405 	hr_reg_clear(qpc_mask, QPC_WQE_SGE_BUF_PG_SZ);
4406 
4407 	context->rq_cur_blk_addr = cpu_to_le32(to_hr_hw_page_addr(mtts[0]));
4408 	qpc_mask->rq_cur_blk_addr = 0;
4409 
4410 	hr_reg_write(context, QPC_RQ_CUR_BLK_ADDR_H,
4411 		     upper_32_bits(to_hr_hw_page_addr(mtts[0])));
4412 	hr_reg_clear(qpc_mask, QPC_RQ_CUR_BLK_ADDR_H);
4413 
4414 	context->rq_nxt_blk_addr = cpu_to_le32(to_hr_hw_page_addr(mtts[1]));
4415 	qpc_mask->rq_nxt_blk_addr = 0;
4416 
4417 	hr_reg_write(context, QPC_RQ_NXT_BLK_ADDR_H,
4418 		     upper_32_bits(to_hr_hw_page_addr(mtts[1])));
4419 	hr_reg_clear(qpc_mask, QPC_RQ_NXT_BLK_ADDR_H);
4420 
4421 	return 0;
4422 }
4423 
4424 static int config_qp_sq_buf(struct hns_roce_dev *hr_dev,
4425 			    struct hns_roce_qp *hr_qp,
4426 			    struct hns_roce_v2_qp_context *context,
4427 			    struct hns_roce_v2_qp_context *qpc_mask)
4428 {
4429 	struct ib_device *ibdev = &hr_dev->ib_dev;
4430 	u64 sge_cur_blk = 0;
4431 	u64 sq_cur_blk = 0;
4432 	int count;
4433 
4434 	/* search qp buf's mtts */
4435 	count = hns_roce_mtr_find(hr_dev, &hr_qp->mtr, 0, &sq_cur_blk, 1, NULL);
4436 	if (count < 1) {
4437 		ibdev_err(ibdev, "failed to find QP(0x%lx) SQ buf.\n",
4438 			  hr_qp->qpn);
4439 		return -EINVAL;
4440 	}
4441 	if (hr_qp->sge.sge_cnt > 0) {
4442 		count = hns_roce_mtr_find(hr_dev, &hr_qp->mtr,
4443 					  hr_qp->sge.offset,
4444 					  &sge_cur_blk, 1, NULL);
4445 		if (count < 1) {
4446 			ibdev_err(ibdev, "failed to find QP(0x%lx) SGE buf.\n",
4447 				  hr_qp->qpn);
4448 			return -EINVAL;
4449 		}
4450 	}
4451 
4452 	/*
4453 	 * In v2 engine, software pass context and context mask to hardware
4454 	 * when modifying qp. If software need modify some fields in context,
4455 	 * we should set all bits of the relevant fields in context mask to
4456 	 * 0 at the same time, else set them to 0x1.
4457 	 */
4458 	hr_reg_write(context, QPC_SQ_CUR_BLK_ADDR_L,
4459 		     lower_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4460 	hr_reg_write(context, QPC_SQ_CUR_BLK_ADDR_H,
4461 		     upper_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4462 	hr_reg_clear(qpc_mask, QPC_SQ_CUR_BLK_ADDR_L);
4463 	hr_reg_clear(qpc_mask, QPC_SQ_CUR_BLK_ADDR_H);
4464 
4465 	hr_reg_write(context, QPC_SQ_CUR_SGE_BLK_ADDR_L,
4466 		     lower_32_bits(to_hr_hw_page_addr(sge_cur_blk)));
4467 	hr_reg_write(context, QPC_SQ_CUR_SGE_BLK_ADDR_H,
4468 		     upper_32_bits(to_hr_hw_page_addr(sge_cur_blk)));
4469 	hr_reg_clear(qpc_mask, QPC_SQ_CUR_SGE_BLK_ADDR_L);
4470 	hr_reg_clear(qpc_mask, QPC_SQ_CUR_SGE_BLK_ADDR_H);
4471 
4472 	hr_reg_write(context, QPC_RX_SQ_CUR_BLK_ADDR_L,
4473 		     lower_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4474 	hr_reg_write(context, QPC_RX_SQ_CUR_BLK_ADDR_H,
4475 		     upper_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4476 	hr_reg_clear(qpc_mask, QPC_RX_SQ_CUR_BLK_ADDR_L);
4477 	hr_reg_clear(qpc_mask, QPC_RX_SQ_CUR_BLK_ADDR_H);
4478 
4479 	return 0;
4480 }
4481 
4482 static inline enum ib_mtu get_mtu(struct ib_qp *ibqp,
4483 				  const struct ib_qp_attr *attr)
4484 {
4485 	if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_UD)
4486 		return IB_MTU_4096;
4487 
4488 	return attr->path_mtu;
4489 }
4490 
4491 static int modify_qp_init_to_rtr(struct ib_qp *ibqp,
4492 				 const struct ib_qp_attr *attr, int attr_mask,
4493 				 struct hns_roce_v2_qp_context *context,
4494 				 struct hns_roce_v2_qp_context *qpc_mask,
4495 				 struct ib_udata *udata)
4496 {
4497 	struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(udata,
4498 					  struct hns_roce_ucontext, ibucontext);
4499 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4500 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4501 	struct ib_device *ibdev = &hr_dev->ib_dev;
4502 	dma_addr_t trrl_ba;
4503 	dma_addr_t irrl_ba;
4504 	enum ib_mtu ib_mtu;
4505 	const u8 *smac;
4506 	u8 lp_pktn_ini;
4507 	u64 *mtts;
4508 	u8 *dmac;
4509 	u32 port;
4510 	int mtu;
4511 	int ret;
4512 
4513 	ret = config_qp_rq_buf(hr_dev, hr_qp, context, qpc_mask);
4514 	if (ret) {
4515 		ibdev_err(ibdev, "failed to config rq buf, ret = %d.\n", ret);
4516 		return ret;
4517 	}
4518 
4519 	/* Search IRRL's mtts */
4520 	mtts = hns_roce_table_find(hr_dev, &hr_dev->qp_table.irrl_table,
4521 				   hr_qp->qpn, &irrl_ba);
4522 	if (!mtts) {
4523 		ibdev_err(ibdev, "failed to find qp irrl_table.\n");
4524 		return -EINVAL;
4525 	}
4526 
4527 	/* Search TRRL's mtts */
4528 	mtts = hns_roce_table_find(hr_dev, &hr_dev->qp_table.trrl_table,
4529 				   hr_qp->qpn, &trrl_ba);
4530 	if (!mtts) {
4531 		ibdev_err(ibdev, "failed to find qp trrl_table.\n");
4532 		return -EINVAL;
4533 	}
4534 
4535 	if (attr_mask & IB_QP_ALT_PATH) {
4536 		ibdev_err(ibdev, "INIT2RTR attr_mask (0x%x) error.\n",
4537 			  attr_mask);
4538 		return -EINVAL;
4539 	}
4540 
4541 	hr_reg_write(context, QPC_TRRL_BA_L, trrl_ba >> 4);
4542 	hr_reg_clear(qpc_mask, QPC_TRRL_BA_L);
4543 	context->trrl_ba = cpu_to_le32(trrl_ba >> (16 + 4));
4544 	qpc_mask->trrl_ba = 0;
4545 	hr_reg_write(context, QPC_TRRL_BA_H, trrl_ba >> (32 + 16 + 4));
4546 	hr_reg_clear(qpc_mask, QPC_TRRL_BA_H);
4547 
4548 	context->irrl_ba = cpu_to_le32(irrl_ba >> 6);
4549 	qpc_mask->irrl_ba = 0;
4550 	hr_reg_write(context, QPC_IRRL_BA_H, irrl_ba >> (32 + 6));
4551 	hr_reg_clear(qpc_mask, QPC_IRRL_BA_H);
4552 
4553 	hr_reg_enable(context, QPC_RMT_E2E);
4554 	hr_reg_clear(qpc_mask, QPC_RMT_E2E);
4555 
4556 	hr_reg_write(context, QPC_SIG_TYPE, hr_qp->sq_signal_bits);
4557 	hr_reg_clear(qpc_mask, QPC_SIG_TYPE);
4558 
4559 	port = (attr_mask & IB_QP_PORT) ? (attr->port_num - 1) : hr_qp->port;
4560 
4561 	smac = (const u8 *)hr_dev->dev_addr[port];
4562 	dmac = (u8 *)attr->ah_attr.roce.dmac;
4563 	/* when dmac equals smac or loop_idc is 1, it should loopback */
4564 	if (ether_addr_equal_unaligned(dmac, smac) ||
4565 	    hr_dev->loop_idc == 0x1) {
4566 		hr_reg_write(context, QPC_LBI, hr_dev->loop_idc);
4567 		hr_reg_clear(qpc_mask, QPC_LBI);
4568 	}
4569 
4570 	if (attr_mask & IB_QP_DEST_QPN) {
4571 		hr_reg_write(context, QPC_DQPN, attr->dest_qp_num);
4572 		hr_reg_clear(qpc_mask, QPC_DQPN);
4573 	}
4574 
4575 	memcpy(&context->dmac, dmac, sizeof(u32));
4576 	hr_reg_write(context, QPC_DMAC_H, *((u16 *)(&dmac[4])));
4577 	qpc_mask->dmac = 0;
4578 	hr_reg_clear(qpc_mask, QPC_DMAC_H);
4579 
4580 	ib_mtu = get_mtu(ibqp, attr);
4581 	hr_qp->path_mtu = ib_mtu;
4582 
4583 	mtu = ib_mtu_enum_to_int(ib_mtu);
4584 	if (WARN_ON(mtu <= 0))
4585 		return -EINVAL;
4586 #define MAX_LP_MSG_LEN 16384
4587 	/* MTU * (2 ^ LP_PKTN_INI) shouldn't be bigger than 16KB */
4588 	lp_pktn_ini = ilog2(MAX_LP_MSG_LEN / mtu);
4589 	if (WARN_ON(lp_pktn_ini >= 0xF))
4590 		return -EINVAL;
4591 
4592 	if (attr_mask & IB_QP_PATH_MTU) {
4593 		hr_reg_write(context, QPC_MTU, ib_mtu);
4594 		hr_reg_clear(qpc_mask, QPC_MTU);
4595 	}
4596 
4597 	hr_reg_write(context, QPC_LP_PKTN_INI, lp_pktn_ini);
4598 	hr_reg_clear(qpc_mask, QPC_LP_PKTN_INI);
4599 
4600 	/* ACK_REQ_FREQ should be larger than or equal to LP_PKTN_INI */
4601 	hr_reg_write(context, QPC_ACK_REQ_FREQ, lp_pktn_ini);
4602 	hr_reg_clear(qpc_mask, QPC_ACK_REQ_FREQ);
4603 
4604 	hr_reg_clear(qpc_mask, QPC_RX_REQ_PSN_ERR);
4605 	hr_reg_clear(qpc_mask, QPC_RX_REQ_MSN);
4606 	hr_reg_clear(qpc_mask, QPC_RX_REQ_LAST_OPTYPE);
4607 
4608 	context->rq_rnr_timer = 0;
4609 	qpc_mask->rq_rnr_timer = 0;
4610 
4611 	hr_reg_clear(qpc_mask, QPC_TRRL_HEAD_MAX);
4612 	hr_reg_clear(qpc_mask, QPC_TRRL_TAIL_MAX);
4613 
4614 	/* rocee send 2^lp_sgen_ini segs every time */
4615 	hr_reg_write(context, QPC_LP_SGEN_INI, 3);
4616 	hr_reg_clear(qpc_mask, QPC_LP_SGEN_INI);
4617 
4618 	if (udata && ibqp->qp_type == IB_QPT_RC &&
4619 	    (uctx->config & HNS_ROCE_RQ_INLINE_FLAGS)) {
4620 		hr_reg_write_bool(context, QPC_RQIE,
4621 				  hr_dev->caps.flags &
4622 				  HNS_ROCE_CAP_FLAG_RQ_INLINE);
4623 		hr_reg_clear(qpc_mask, QPC_RQIE);
4624 	}
4625 
4626 	if (udata &&
4627 	    (ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_XRC_TGT) &&
4628 	    (uctx->config & HNS_ROCE_CQE_INLINE_FLAGS)) {
4629 		hr_reg_write_bool(context, QPC_CQEIE,
4630 				  hr_dev->caps.flags &
4631 				  HNS_ROCE_CAP_FLAG_CQE_INLINE);
4632 		hr_reg_clear(qpc_mask, QPC_CQEIE);
4633 
4634 		hr_reg_write(context, QPC_CQEIS, 0);
4635 		hr_reg_clear(qpc_mask, QPC_CQEIS);
4636 	}
4637 
4638 	return 0;
4639 }
4640 
4641 static int modify_qp_rtr_to_rts(struct ib_qp *ibqp,
4642 				const struct ib_qp_attr *attr, int attr_mask,
4643 				struct hns_roce_v2_qp_context *context,
4644 				struct hns_roce_v2_qp_context *qpc_mask)
4645 {
4646 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4647 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4648 	struct ib_device *ibdev = &hr_dev->ib_dev;
4649 	int ret;
4650 
4651 	/* Not support alternate path and path migration */
4652 	if (attr_mask & (IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE)) {
4653 		ibdev_err(ibdev, "RTR2RTS attr_mask (0x%x)error\n", attr_mask);
4654 		return -EINVAL;
4655 	}
4656 
4657 	ret = config_qp_sq_buf(hr_dev, hr_qp, context, qpc_mask);
4658 	if (ret) {
4659 		ibdev_err(ibdev, "failed to config sq buf, ret = %d.\n", ret);
4660 		return ret;
4661 	}
4662 
4663 	/*
4664 	 * Set some fields in context to zero, Because the default values
4665 	 * of all fields in context are zero, we need not set them to 0 again.
4666 	 * but we should set the relevant fields of context mask to 0.
4667 	 */
4668 	hr_reg_clear(qpc_mask, QPC_IRRL_SGE_IDX);
4669 
4670 	hr_reg_clear(qpc_mask, QPC_RX_ACK_MSN);
4671 
4672 	hr_reg_clear(qpc_mask, QPC_ACK_LAST_OPTYPE);
4673 	hr_reg_clear(qpc_mask, QPC_IRRL_PSN_VLD);
4674 	hr_reg_clear(qpc_mask, QPC_IRRL_PSN);
4675 
4676 	hr_reg_clear(qpc_mask, QPC_IRRL_TAIL_REAL);
4677 
4678 	hr_reg_clear(qpc_mask, QPC_RETRY_MSG_MSN);
4679 
4680 	hr_reg_clear(qpc_mask, QPC_RNR_RETRY_FLAG);
4681 
4682 	hr_reg_clear(qpc_mask, QPC_CHECK_FLG);
4683 
4684 	hr_reg_clear(qpc_mask, QPC_V2_IRRL_HEAD);
4685 
4686 	return 0;
4687 }
4688 
4689 static int get_dip_ctx_idx(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
4690 			   u32 *dip_idx)
4691 {
4692 	const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
4693 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4694 	u32 *spare_idx = hr_dev->qp_table.idx_table.spare_idx;
4695 	u32 *head =  &hr_dev->qp_table.idx_table.head;
4696 	u32 *tail =  &hr_dev->qp_table.idx_table.tail;
4697 	struct hns_roce_dip *hr_dip;
4698 	unsigned long flags;
4699 	int ret = 0;
4700 
4701 	spin_lock_irqsave(&hr_dev->dip_list_lock, flags);
4702 
4703 	spare_idx[*tail] = ibqp->qp_num;
4704 	*tail = (*tail == hr_dev->caps.num_qps - 1) ? 0 : (*tail + 1);
4705 
4706 	list_for_each_entry(hr_dip, &hr_dev->dip_list, node) {
4707 		if (!memcmp(grh->dgid.raw, hr_dip->dgid, 16)) {
4708 			*dip_idx = hr_dip->dip_idx;
4709 			goto out;
4710 		}
4711 	}
4712 
4713 	/* If no dgid is found, a new dip and a mapping between dgid and
4714 	 * dip_idx will be created.
4715 	 */
4716 	hr_dip = kzalloc(sizeof(*hr_dip), GFP_ATOMIC);
4717 	if (!hr_dip) {
4718 		ret = -ENOMEM;
4719 		goto out;
4720 	}
4721 
4722 	memcpy(hr_dip->dgid, grh->dgid.raw, sizeof(grh->dgid.raw));
4723 	hr_dip->dip_idx = *dip_idx = spare_idx[*head];
4724 	*head = (*head == hr_dev->caps.num_qps - 1) ? 0 : (*head + 1);
4725 	list_add_tail(&hr_dip->node, &hr_dev->dip_list);
4726 
4727 out:
4728 	spin_unlock_irqrestore(&hr_dev->dip_list_lock, flags);
4729 	return ret;
4730 }
4731 
4732 enum {
4733 	CONG_DCQCN,
4734 	CONG_WINDOW,
4735 };
4736 
4737 enum {
4738 	UNSUPPORT_CONG_LEVEL,
4739 	SUPPORT_CONG_LEVEL,
4740 };
4741 
4742 enum {
4743 	CONG_LDCP,
4744 	CONG_HC3,
4745 };
4746 
4747 enum {
4748 	DIP_INVALID,
4749 	DIP_VALID,
4750 };
4751 
4752 enum {
4753 	WND_LIMIT,
4754 	WND_UNLIMIT,
4755 };
4756 
4757 static int check_cong_type(struct ib_qp *ibqp,
4758 			   struct hns_roce_congestion_algorithm *cong_alg)
4759 {
4760 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4761 
4762 	/* different congestion types match different configurations */
4763 	switch (hr_dev->caps.cong_type) {
4764 	case CONG_TYPE_DCQCN:
4765 		cong_alg->alg_sel = CONG_DCQCN;
4766 		cong_alg->alg_sub_sel = UNSUPPORT_CONG_LEVEL;
4767 		cong_alg->dip_vld = DIP_INVALID;
4768 		cong_alg->wnd_mode_sel = WND_LIMIT;
4769 		break;
4770 	case CONG_TYPE_LDCP:
4771 		cong_alg->alg_sel = CONG_WINDOW;
4772 		cong_alg->alg_sub_sel = CONG_LDCP;
4773 		cong_alg->dip_vld = DIP_INVALID;
4774 		cong_alg->wnd_mode_sel = WND_UNLIMIT;
4775 		break;
4776 	case CONG_TYPE_HC3:
4777 		cong_alg->alg_sel = CONG_WINDOW;
4778 		cong_alg->alg_sub_sel = CONG_HC3;
4779 		cong_alg->dip_vld = DIP_INVALID;
4780 		cong_alg->wnd_mode_sel = WND_LIMIT;
4781 		break;
4782 	case CONG_TYPE_DIP:
4783 		cong_alg->alg_sel = CONG_DCQCN;
4784 		cong_alg->alg_sub_sel = UNSUPPORT_CONG_LEVEL;
4785 		cong_alg->dip_vld = DIP_VALID;
4786 		cong_alg->wnd_mode_sel = WND_LIMIT;
4787 		break;
4788 	default:
4789 		ibdev_err(&hr_dev->ib_dev,
4790 			  "error type(%u) for congestion selection.\n",
4791 			  hr_dev->caps.cong_type);
4792 		return -EINVAL;
4793 	}
4794 
4795 	return 0;
4796 }
4797 
4798 static int fill_cong_field(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
4799 			   struct hns_roce_v2_qp_context *context,
4800 			   struct hns_roce_v2_qp_context *qpc_mask)
4801 {
4802 	const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
4803 	struct hns_roce_congestion_algorithm cong_field;
4804 	struct ib_device *ibdev = ibqp->device;
4805 	struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
4806 	u32 dip_idx = 0;
4807 	int ret;
4808 
4809 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08 ||
4810 	    grh->sgid_attr->gid_type == IB_GID_TYPE_ROCE)
4811 		return 0;
4812 
4813 	ret = check_cong_type(ibqp, &cong_field);
4814 	if (ret)
4815 		return ret;
4816 
4817 	hr_reg_write(context, QPC_CONG_ALGO_TMPL_ID, hr_dev->cong_algo_tmpl_id +
4818 		     hr_dev->caps.cong_type * HNS_ROCE_CONG_SIZE);
4819 	hr_reg_clear(qpc_mask, QPC_CONG_ALGO_TMPL_ID);
4820 	hr_reg_write(&context->ext, QPCEX_CONG_ALG_SEL, cong_field.alg_sel);
4821 	hr_reg_clear(&qpc_mask->ext, QPCEX_CONG_ALG_SEL);
4822 	hr_reg_write(&context->ext, QPCEX_CONG_ALG_SUB_SEL,
4823 		     cong_field.alg_sub_sel);
4824 	hr_reg_clear(&qpc_mask->ext, QPCEX_CONG_ALG_SUB_SEL);
4825 	hr_reg_write(&context->ext, QPCEX_DIP_CTX_IDX_VLD, cong_field.dip_vld);
4826 	hr_reg_clear(&qpc_mask->ext, QPCEX_DIP_CTX_IDX_VLD);
4827 	hr_reg_write(&context->ext, QPCEX_SQ_RQ_NOT_FORBID_EN,
4828 		     cong_field.wnd_mode_sel);
4829 	hr_reg_clear(&qpc_mask->ext, QPCEX_SQ_RQ_NOT_FORBID_EN);
4830 
4831 	/* if dip is disabled, there is no need to set dip idx */
4832 	if (cong_field.dip_vld == 0)
4833 		return 0;
4834 
4835 	ret = get_dip_ctx_idx(ibqp, attr, &dip_idx);
4836 	if (ret) {
4837 		ibdev_err(ibdev, "failed to fill cong field, ret = %d.\n", ret);
4838 		return ret;
4839 	}
4840 
4841 	hr_reg_write(&context->ext, QPCEX_DIP_CTX_IDX, dip_idx);
4842 	hr_reg_write(&qpc_mask->ext, QPCEX_DIP_CTX_IDX, 0);
4843 
4844 	return 0;
4845 }
4846 
4847 static int hns_roce_v2_set_path(struct ib_qp *ibqp,
4848 				const struct ib_qp_attr *attr,
4849 				int attr_mask,
4850 				struct hns_roce_v2_qp_context *context,
4851 				struct hns_roce_v2_qp_context *qpc_mask)
4852 {
4853 	const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
4854 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4855 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4856 	struct ib_device *ibdev = &hr_dev->ib_dev;
4857 	const struct ib_gid_attr *gid_attr = NULL;
4858 	int is_roce_protocol;
4859 	u16 vlan_id = 0xffff;
4860 	bool is_udp = false;
4861 	u8 ib_port;
4862 	u8 hr_port;
4863 	int ret;
4864 
4865 	/*
4866 	 * If free_mr_en of qp is set, it means that this qp comes from
4867 	 * free mr. This qp will perform the loopback operation.
4868 	 * In the loopback scenario, only sl needs to be set.
4869 	 */
4870 	if (hr_qp->free_mr_en) {
4871 		hr_reg_write(context, QPC_SL, rdma_ah_get_sl(&attr->ah_attr));
4872 		hr_reg_clear(qpc_mask, QPC_SL);
4873 		hr_qp->sl = rdma_ah_get_sl(&attr->ah_attr);
4874 		return 0;
4875 	}
4876 
4877 	ib_port = (attr_mask & IB_QP_PORT) ? attr->port_num : hr_qp->port + 1;
4878 	hr_port = ib_port - 1;
4879 	is_roce_protocol = rdma_cap_eth_ah(&hr_dev->ib_dev, ib_port) &&
4880 			   rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH;
4881 
4882 	if (is_roce_protocol) {
4883 		gid_attr = attr->ah_attr.grh.sgid_attr;
4884 		ret = rdma_read_gid_l2_fields(gid_attr, &vlan_id, NULL);
4885 		if (ret)
4886 			return ret;
4887 
4888 		is_udp = (gid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP);
4889 	}
4890 
4891 	/* Only HIP08 needs to set the vlan_en bits in QPC */
4892 	if (vlan_id < VLAN_N_VID &&
4893 	    hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
4894 		hr_reg_enable(context, QPC_RQ_VLAN_EN);
4895 		hr_reg_clear(qpc_mask, QPC_RQ_VLAN_EN);
4896 		hr_reg_enable(context, QPC_SQ_VLAN_EN);
4897 		hr_reg_clear(qpc_mask, QPC_SQ_VLAN_EN);
4898 	}
4899 
4900 	hr_reg_write(context, QPC_VLAN_ID, vlan_id);
4901 	hr_reg_clear(qpc_mask, QPC_VLAN_ID);
4902 
4903 	if (grh->sgid_index >= hr_dev->caps.gid_table_len[hr_port]) {
4904 		ibdev_err(ibdev, "sgid_index(%u) too large. max is %d\n",
4905 			  grh->sgid_index, hr_dev->caps.gid_table_len[hr_port]);
4906 		return -EINVAL;
4907 	}
4908 
4909 	if (attr->ah_attr.type != RDMA_AH_ATTR_TYPE_ROCE) {
4910 		ibdev_err(ibdev, "ah attr is not RDMA roce type\n");
4911 		return -EINVAL;
4912 	}
4913 
4914 	hr_reg_write(context, QPC_UDPSPN,
4915 		     is_udp ? rdma_get_udp_sport(grh->flow_label, ibqp->qp_num,
4916 						 attr->dest_qp_num) :
4917 				    0);
4918 
4919 	hr_reg_clear(qpc_mask, QPC_UDPSPN);
4920 
4921 	hr_reg_write(context, QPC_GMV_IDX, grh->sgid_index);
4922 
4923 	hr_reg_clear(qpc_mask, QPC_GMV_IDX);
4924 
4925 	hr_reg_write(context, QPC_HOPLIMIT, grh->hop_limit);
4926 	hr_reg_clear(qpc_mask, QPC_HOPLIMIT);
4927 
4928 	ret = fill_cong_field(ibqp, attr, context, qpc_mask);
4929 	if (ret)
4930 		return ret;
4931 
4932 	hr_reg_write(context, QPC_TC, get_tclass(&attr->ah_attr.grh));
4933 	hr_reg_clear(qpc_mask, QPC_TC);
4934 
4935 	hr_reg_write(context, QPC_FL, grh->flow_label);
4936 	hr_reg_clear(qpc_mask, QPC_FL);
4937 	memcpy(context->dgid, grh->dgid.raw, sizeof(grh->dgid.raw));
4938 	memset(qpc_mask->dgid, 0, sizeof(grh->dgid.raw));
4939 
4940 	hr_qp->sl = rdma_ah_get_sl(&attr->ah_attr);
4941 	if (unlikely(hr_qp->sl > MAX_SERVICE_LEVEL)) {
4942 		ibdev_err(ibdev,
4943 			  "failed to fill QPC, sl (%u) shouldn't be larger than %d.\n",
4944 			  hr_qp->sl, MAX_SERVICE_LEVEL);
4945 		return -EINVAL;
4946 	}
4947 
4948 	hr_reg_write(context, QPC_SL, hr_qp->sl);
4949 	hr_reg_clear(qpc_mask, QPC_SL);
4950 
4951 	return 0;
4952 }
4953 
4954 static bool check_qp_state(enum ib_qp_state cur_state,
4955 			   enum ib_qp_state new_state)
4956 {
4957 	static const bool sm[][IB_QPS_ERR + 1] = {
4958 		[IB_QPS_RESET] = { [IB_QPS_RESET] = true,
4959 				   [IB_QPS_INIT] = true },
4960 		[IB_QPS_INIT] = { [IB_QPS_RESET] = true,
4961 				  [IB_QPS_INIT] = true,
4962 				  [IB_QPS_RTR] = true,
4963 				  [IB_QPS_ERR] = true },
4964 		[IB_QPS_RTR] = { [IB_QPS_RESET] = true,
4965 				 [IB_QPS_RTS] = true,
4966 				 [IB_QPS_ERR] = true },
4967 		[IB_QPS_RTS] = { [IB_QPS_RESET] = true,
4968 				 [IB_QPS_RTS] = true,
4969 				 [IB_QPS_ERR] = true },
4970 		[IB_QPS_SQD] = {},
4971 		[IB_QPS_SQE] = {},
4972 		[IB_QPS_ERR] = { [IB_QPS_RESET] = true,
4973 				 [IB_QPS_ERR] = true }
4974 	};
4975 
4976 	return sm[cur_state][new_state];
4977 }
4978 
4979 static int hns_roce_v2_set_abs_fields(struct ib_qp *ibqp,
4980 				      const struct ib_qp_attr *attr,
4981 				      int attr_mask,
4982 				      enum ib_qp_state cur_state,
4983 				      enum ib_qp_state new_state,
4984 				      struct hns_roce_v2_qp_context *context,
4985 				      struct hns_roce_v2_qp_context *qpc_mask,
4986 				      struct ib_udata *udata)
4987 {
4988 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4989 	int ret = 0;
4990 
4991 	if (!check_qp_state(cur_state, new_state)) {
4992 		ibdev_err(&hr_dev->ib_dev, "Illegal state for QP!\n");
4993 		return -EINVAL;
4994 	}
4995 
4996 	if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
4997 		memset(qpc_mask, 0, hr_dev->caps.qpc_sz);
4998 		modify_qp_reset_to_init(ibqp, attr, context, qpc_mask);
4999 	} else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
5000 		modify_qp_init_to_init(ibqp, attr, context, qpc_mask);
5001 	} else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
5002 		ret = modify_qp_init_to_rtr(ibqp, attr, attr_mask, context,
5003 					    qpc_mask, udata);
5004 	} else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
5005 		ret = modify_qp_rtr_to_rts(ibqp, attr, attr_mask, context,
5006 					   qpc_mask);
5007 	}
5008 
5009 	return ret;
5010 }
5011 
5012 static bool check_qp_timeout_cfg_range(struct hns_roce_dev *hr_dev, u8 *timeout)
5013 {
5014 #define QP_ACK_TIMEOUT_MAX_HIP08 20
5015 #define QP_ACK_TIMEOUT_OFFSET 10
5016 #define QP_ACK_TIMEOUT_MAX 31
5017 
5018 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
5019 		if (*timeout > QP_ACK_TIMEOUT_MAX_HIP08) {
5020 			ibdev_warn(&hr_dev->ib_dev,
5021 				   "local ACK timeout shall be 0 to 20.\n");
5022 			return false;
5023 		}
5024 		*timeout += QP_ACK_TIMEOUT_OFFSET;
5025 	} else if (hr_dev->pci_dev->revision > PCI_REVISION_ID_HIP08) {
5026 		if (*timeout > QP_ACK_TIMEOUT_MAX) {
5027 			ibdev_warn(&hr_dev->ib_dev,
5028 				   "local ACK timeout shall be 0 to 31.\n");
5029 			return false;
5030 		}
5031 	}
5032 
5033 	return true;
5034 }
5035 
5036 static int hns_roce_v2_set_opt_fields(struct ib_qp *ibqp,
5037 				      const struct ib_qp_attr *attr,
5038 				      int attr_mask,
5039 				      struct hns_roce_v2_qp_context *context,
5040 				      struct hns_roce_v2_qp_context *qpc_mask)
5041 {
5042 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5043 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5044 	int ret = 0;
5045 	u8 timeout;
5046 
5047 	if (attr_mask & IB_QP_AV) {
5048 		ret = hns_roce_v2_set_path(ibqp, attr, attr_mask, context,
5049 					   qpc_mask);
5050 		if (ret)
5051 			return ret;
5052 	}
5053 
5054 	if (attr_mask & IB_QP_TIMEOUT) {
5055 		timeout = attr->timeout;
5056 		if (check_qp_timeout_cfg_range(hr_dev, &timeout)) {
5057 			hr_reg_write(context, QPC_AT, timeout);
5058 			hr_reg_clear(qpc_mask, QPC_AT);
5059 		}
5060 	}
5061 
5062 	if (attr_mask & IB_QP_RETRY_CNT) {
5063 		hr_reg_write(context, QPC_RETRY_NUM_INIT, attr->retry_cnt);
5064 		hr_reg_clear(qpc_mask, QPC_RETRY_NUM_INIT);
5065 
5066 		hr_reg_write(context, QPC_RETRY_CNT, attr->retry_cnt);
5067 		hr_reg_clear(qpc_mask, QPC_RETRY_CNT);
5068 	}
5069 
5070 	if (attr_mask & IB_QP_RNR_RETRY) {
5071 		hr_reg_write(context, QPC_RNR_NUM_INIT, attr->rnr_retry);
5072 		hr_reg_clear(qpc_mask, QPC_RNR_NUM_INIT);
5073 
5074 		hr_reg_write(context, QPC_RNR_CNT, attr->rnr_retry);
5075 		hr_reg_clear(qpc_mask, QPC_RNR_CNT);
5076 	}
5077 
5078 	if (attr_mask & IB_QP_SQ_PSN) {
5079 		hr_reg_write(context, QPC_SQ_CUR_PSN, attr->sq_psn);
5080 		hr_reg_clear(qpc_mask, QPC_SQ_CUR_PSN);
5081 
5082 		hr_reg_write(context, QPC_SQ_MAX_PSN, attr->sq_psn);
5083 		hr_reg_clear(qpc_mask, QPC_SQ_MAX_PSN);
5084 
5085 		hr_reg_write(context, QPC_RETRY_MSG_PSN_L, attr->sq_psn);
5086 		hr_reg_clear(qpc_mask, QPC_RETRY_MSG_PSN_L);
5087 
5088 		hr_reg_write(context, QPC_RETRY_MSG_PSN_H,
5089 			     attr->sq_psn >> RETRY_MSG_PSN_SHIFT);
5090 		hr_reg_clear(qpc_mask, QPC_RETRY_MSG_PSN_H);
5091 
5092 		hr_reg_write(context, QPC_RETRY_MSG_FPKT_PSN, attr->sq_psn);
5093 		hr_reg_clear(qpc_mask, QPC_RETRY_MSG_FPKT_PSN);
5094 
5095 		hr_reg_write(context, QPC_RX_ACK_EPSN, attr->sq_psn);
5096 		hr_reg_clear(qpc_mask, QPC_RX_ACK_EPSN);
5097 	}
5098 
5099 	if ((attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) &&
5100 	     attr->max_dest_rd_atomic) {
5101 		hr_reg_write(context, QPC_RR_MAX,
5102 			     fls(attr->max_dest_rd_atomic - 1));
5103 		hr_reg_clear(qpc_mask, QPC_RR_MAX);
5104 	}
5105 
5106 	if ((attr_mask & IB_QP_MAX_QP_RD_ATOMIC) && attr->max_rd_atomic) {
5107 		hr_reg_write(context, QPC_SR_MAX, fls(attr->max_rd_atomic - 1));
5108 		hr_reg_clear(qpc_mask, QPC_SR_MAX);
5109 	}
5110 
5111 	if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
5112 		set_access_flags(hr_qp, context, qpc_mask, attr, attr_mask);
5113 
5114 	if (attr_mask & IB_QP_MIN_RNR_TIMER) {
5115 		hr_reg_write(context, QPC_MIN_RNR_TIME,
5116 			    hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08 ?
5117 			    HNS_ROCE_RNR_TIMER_10NS : attr->min_rnr_timer);
5118 		hr_reg_clear(qpc_mask, QPC_MIN_RNR_TIME);
5119 	}
5120 
5121 	if (attr_mask & IB_QP_RQ_PSN) {
5122 		hr_reg_write(context, QPC_RX_REQ_EPSN, attr->rq_psn);
5123 		hr_reg_clear(qpc_mask, QPC_RX_REQ_EPSN);
5124 
5125 		hr_reg_write(context, QPC_RAQ_PSN, attr->rq_psn - 1);
5126 		hr_reg_clear(qpc_mask, QPC_RAQ_PSN);
5127 	}
5128 
5129 	if (attr_mask & IB_QP_QKEY) {
5130 		context->qkey_xrcd = cpu_to_le32(attr->qkey);
5131 		qpc_mask->qkey_xrcd = 0;
5132 		hr_qp->qkey = attr->qkey;
5133 	}
5134 
5135 	return ret;
5136 }
5137 
5138 static void hns_roce_v2_record_opt_fields(struct ib_qp *ibqp,
5139 					  const struct ib_qp_attr *attr,
5140 					  int attr_mask)
5141 {
5142 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5143 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5144 
5145 	if (attr_mask & IB_QP_ACCESS_FLAGS)
5146 		hr_qp->atomic_rd_en = attr->qp_access_flags;
5147 
5148 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
5149 		hr_qp->resp_depth = attr->max_dest_rd_atomic;
5150 	if (attr_mask & IB_QP_PORT) {
5151 		hr_qp->port = attr->port_num - 1;
5152 		hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
5153 	}
5154 }
5155 
5156 static void clear_qp(struct hns_roce_qp *hr_qp)
5157 {
5158 	struct ib_qp *ibqp = &hr_qp->ibqp;
5159 
5160 	if (ibqp->send_cq)
5161 		hns_roce_v2_cq_clean(to_hr_cq(ibqp->send_cq),
5162 				     hr_qp->qpn, NULL);
5163 
5164 	if (ibqp->recv_cq  && ibqp->recv_cq != ibqp->send_cq)
5165 		hns_roce_v2_cq_clean(to_hr_cq(ibqp->recv_cq),
5166 				     hr_qp->qpn, ibqp->srq ?
5167 				     to_hr_srq(ibqp->srq) : NULL);
5168 
5169 	if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
5170 		*hr_qp->rdb.db_record = 0;
5171 
5172 	hr_qp->rq.head = 0;
5173 	hr_qp->rq.tail = 0;
5174 	hr_qp->sq.head = 0;
5175 	hr_qp->sq.tail = 0;
5176 	hr_qp->next_sge = 0;
5177 }
5178 
5179 static void v2_set_flushed_fields(struct ib_qp *ibqp,
5180 				  struct hns_roce_v2_qp_context *context,
5181 				  struct hns_roce_v2_qp_context *qpc_mask)
5182 {
5183 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5184 	unsigned long sq_flag = 0;
5185 	unsigned long rq_flag = 0;
5186 
5187 	if (ibqp->qp_type == IB_QPT_XRC_TGT)
5188 		return;
5189 
5190 	spin_lock_irqsave(&hr_qp->sq.lock, sq_flag);
5191 	hr_reg_write(context, QPC_SQ_PRODUCER_IDX, hr_qp->sq.head);
5192 	hr_reg_clear(qpc_mask, QPC_SQ_PRODUCER_IDX);
5193 	hr_qp->state = IB_QPS_ERR;
5194 	spin_unlock_irqrestore(&hr_qp->sq.lock, sq_flag);
5195 
5196 	if (ibqp->srq || ibqp->qp_type == IB_QPT_XRC_INI) /* no RQ */
5197 		return;
5198 
5199 	spin_lock_irqsave(&hr_qp->rq.lock, rq_flag);
5200 	hr_reg_write(context, QPC_RQ_PRODUCER_IDX, hr_qp->rq.head);
5201 	hr_reg_clear(qpc_mask, QPC_RQ_PRODUCER_IDX);
5202 	spin_unlock_irqrestore(&hr_qp->rq.lock, rq_flag);
5203 }
5204 
5205 static int hns_roce_v2_modify_qp(struct ib_qp *ibqp,
5206 				 const struct ib_qp_attr *attr,
5207 				 int attr_mask, enum ib_qp_state cur_state,
5208 				 enum ib_qp_state new_state, struct ib_udata *udata)
5209 {
5210 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5211 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5212 	struct hns_roce_v2_qp_context ctx[2];
5213 	struct hns_roce_v2_qp_context *context = ctx;
5214 	struct hns_roce_v2_qp_context *qpc_mask = ctx + 1;
5215 	struct ib_device *ibdev = &hr_dev->ib_dev;
5216 	int ret;
5217 
5218 	if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
5219 		return -EOPNOTSUPP;
5220 
5221 	/*
5222 	 * In v2 engine, software pass context and context mask to hardware
5223 	 * when modifying qp. If software need modify some fields in context,
5224 	 * we should set all bits of the relevant fields in context mask to
5225 	 * 0 at the same time, else set them to 0x1.
5226 	 */
5227 	memset(context, 0, hr_dev->caps.qpc_sz);
5228 	memset(qpc_mask, 0xff, hr_dev->caps.qpc_sz);
5229 
5230 	ret = hns_roce_v2_set_abs_fields(ibqp, attr, attr_mask, cur_state,
5231 					 new_state, context, qpc_mask, udata);
5232 	if (ret)
5233 		goto out;
5234 
5235 	/* When QP state is err, SQ and RQ WQE should be flushed */
5236 	if (new_state == IB_QPS_ERR)
5237 		v2_set_flushed_fields(ibqp, context, qpc_mask);
5238 
5239 	/* Configure the optional fields */
5240 	ret = hns_roce_v2_set_opt_fields(ibqp, attr, attr_mask, context,
5241 					 qpc_mask);
5242 	if (ret)
5243 		goto out;
5244 
5245 	hr_reg_write_bool(context, QPC_INV_CREDIT,
5246 			  to_hr_qp_type(hr_qp->ibqp.qp_type) == SERV_TYPE_XRC ||
5247 			  ibqp->srq);
5248 	hr_reg_clear(qpc_mask, QPC_INV_CREDIT);
5249 
5250 	/* Every status migrate must change state */
5251 	hr_reg_write(context, QPC_QP_ST, new_state);
5252 	hr_reg_clear(qpc_mask, QPC_QP_ST);
5253 
5254 	/* SW pass context to HW */
5255 	ret = hns_roce_v2_qp_modify(hr_dev, context, qpc_mask, hr_qp);
5256 	if (ret) {
5257 		ibdev_err(ibdev, "failed to modify QP, ret = %d.\n", ret);
5258 		goto out;
5259 	}
5260 
5261 	hr_qp->state = new_state;
5262 
5263 	hns_roce_v2_record_opt_fields(ibqp, attr, attr_mask);
5264 
5265 	if (new_state == IB_QPS_RESET && !ibqp->uobject)
5266 		clear_qp(hr_qp);
5267 
5268 out:
5269 	return ret;
5270 }
5271 
5272 static int to_ib_qp_st(enum hns_roce_v2_qp_state state)
5273 {
5274 	static const enum ib_qp_state map[] = {
5275 		[HNS_ROCE_QP_ST_RST] = IB_QPS_RESET,
5276 		[HNS_ROCE_QP_ST_INIT] = IB_QPS_INIT,
5277 		[HNS_ROCE_QP_ST_RTR] = IB_QPS_RTR,
5278 		[HNS_ROCE_QP_ST_RTS] = IB_QPS_RTS,
5279 		[HNS_ROCE_QP_ST_SQD] = IB_QPS_SQD,
5280 		[HNS_ROCE_QP_ST_SQER] = IB_QPS_SQE,
5281 		[HNS_ROCE_QP_ST_ERR] = IB_QPS_ERR,
5282 		[HNS_ROCE_QP_ST_SQ_DRAINING] = IB_QPS_SQD
5283 	};
5284 
5285 	return (state < ARRAY_SIZE(map)) ? map[state] : -1;
5286 }
5287 
5288 static int hns_roce_v2_query_qpc(struct hns_roce_dev *hr_dev, u32 qpn,
5289 				 void *buffer)
5290 {
5291 	struct hns_roce_cmd_mailbox *mailbox;
5292 	int ret;
5293 
5294 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5295 	if (IS_ERR(mailbox))
5296 		return PTR_ERR(mailbox);
5297 
5298 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, HNS_ROCE_CMD_QUERY_QPC,
5299 				qpn);
5300 	if (ret)
5301 		goto out;
5302 
5303 	memcpy(buffer, mailbox->buf, hr_dev->caps.qpc_sz);
5304 
5305 out:
5306 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5307 	return ret;
5308 }
5309 
5310 static int hns_roce_v2_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5311 				int qp_attr_mask,
5312 				struct ib_qp_init_attr *qp_init_attr)
5313 {
5314 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5315 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5316 	struct hns_roce_v2_qp_context context = {};
5317 	struct ib_device *ibdev = &hr_dev->ib_dev;
5318 	int tmp_qp_state;
5319 	int state;
5320 	int ret;
5321 
5322 	memset(qp_attr, 0, sizeof(*qp_attr));
5323 	memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5324 
5325 	mutex_lock(&hr_qp->mutex);
5326 
5327 	if (hr_qp->state == IB_QPS_RESET) {
5328 		qp_attr->qp_state = IB_QPS_RESET;
5329 		ret = 0;
5330 		goto done;
5331 	}
5332 
5333 	ret = hns_roce_v2_query_qpc(hr_dev, hr_qp->qpn, &context);
5334 	if (ret) {
5335 		ibdev_err(ibdev, "failed to query QPC, ret = %d.\n", ret);
5336 		ret = -EINVAL;
5337 		goto out;
5338 	}
5339 
5340 	state = hr_reg_read(&context, QPC_QP_ST);
5341 	tmp_qp_state = to_ib_qp_st((enum hns_roce_v2_qp_state)state);
5342 	if (tmp_qp_state == -1) {
5343 		ibdev_err(ibdev, "Illegal ib_qp_state\n");
5344 		ret = -EINVAL;
5345 		goto out;
5346 	}
5347 	hr_qp->state = (u8)tmp_qp_state;
5348 	qp_attr->qp_state = (enum ib_qp_state)hr_qp->state;
5349 	qp_attr->path_mtu = (enum ib_mtu)hr_reg_read(&context, QPC_MTU);
5350 	qp_attr->path_mig_state = IB_MIG_ARMED;
5351 	qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
5352 	if (hr_qp->ibqp.qp_type == IB_QPT_UD)
5353 		qp_attr->qkey = le32_to_cpu(context.qkey_xrcd);
5354 
5355 	qp_attr->rq_psn = hr_reg_read(&context, QPC_RX_REQ_EPSN);
5356 	qp_attr->sq_psn = (u32)hr_reg_read(&context, QPC_SQ_CUR_PSN);
5357 	qp_attr->dest_qp_num = hr_reg_read(&context, QPC_DQPN);
5358 	qp_attr->qp_access_flags =
5359 		((hr_reg_read(&context, QPC_RRE)) << V2_QP_RRE_S) |
5360 		((hr_reg_read(&context, QPC_RWE)) << V2_QP_RWE_S) |
5361 		((hr_reg_read(&context, QPC_ATE)) << V2_QP_ATE_S);
5362 
5363 	if (hr_qp->ibqp.qp_type == IB_QPT_RC ||
5364 	    hr_qp->ibqp.qp_type == IB_QPT_XRC_INI ||
5365 	    hr_qp->ibqp.qp_type == IB_QPT_XRC_TGT) {
5366 		struct ib_global_route *grh =
5367 			rdma_ah_retrieve_grh(&qp_attr->ah_attr);
5368 
5369 		rdma_ah_set_sl(&qp_attr->ah_attr,
5370 			       hr_reg_read(&context, QPC_SL));
5371 		rdma_ah_set_port_num(&qp_attr->ah_attr, hr_qp->port + 1);
5372 		rdma_ah_set_ah_flags(&qp_attr->ah_attr, IB_AH_GRH);
5373 		grh->flow_label = hr_reg_read(&context, QPC_FL);
5374 		grh->sgid_index = hr_reg_read(&context, QPC_GMV_IDX);
5375 		grh->hop_limit = hr_reg_read(&context, QPC_HOPLIMIT);
5376 		grh->traffic_class = hr_reg_read(&context, QPC_TC);
5377 
5378 		memcpy(grh->dgid.raw, context.dgid, sizeof(grh->dgid.raw));
5379 	}
5380 
5381 	qp_attr->port_num = hr_qp->port + 1;
5382 	qp_attr->sq_draining = 0;
5383 	qp_attr->max_rd_atomic = 1 << hr_reg_read(&context, QPC_SR_MAX);
5384 	qp_attr->max_dest_rd_atomic = 1 << hr_reg_read(&context, QPC_RR_MAX);
5385 
5386 	qp_attr->min_rnr_timer = (u8)hr_reg_read(&context, QPC_MIN_RNR_TIME);
5387 	qp_attr->timeout = (u8)hr_reg_read(&context, QPC_AT);
5388 	qp_attr->retry_cnt = hr_reg_read(&context, QPC_RETRY_NUM_INIT);
5389 	qp_attr->rnr_retry = hr_reg_read(&context, QPC_RNR_NUM_INIT);
5390 
5391 done:
5392 	qp_attr->cur_qp_state = qp_attr->qp_state;
5393 	qp_attr->cap.max_recv_wr = hr_qp->rq.wqe_cnt;
5394 	qp_attr->cap.max_recv_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
5395 	qp_attr->cap.max_inline_data = hr_qp->max_inline_data;
5396 
5397 	qp_attr->cap.max_send_wr = hr_qp->sq.wqe_cnt;
5398 	qp_attr->cap.max_send_sge = hr_qp->sq.max_gs;
5399 
5400 	qp_init_attr->qp_context = ibqp->qp_context;
5401 	qp_init_attr->qp_type = ibqp->qp_type;
5402 	qp_init_attr->recv_cq = ibqp->recv_cq;
5403 	qp_init_attr->send_cq = ibqp->send_cq;
5404 	qp_init_attr->srq = ibqp->srq;
5405 	qp_init_attr->cap = qp_attr->cap;
5406 	qp_init_attr->sq_sig_type = hr_qp->sq_signal_bits;
5407 
5408 out:
5409 	mutex_unlock(&hr_qp->mutex);
5410 	return ret;
5411 }
5412 
5413 static inline int modify_qp_is_ok(struct hns_roce_qp *hr_qp)
5414 {
5415 	return ((hr_qp->ibqp.qp_type == IB_QPT_RC ||
5416 		 hr_qp->ibqp.qp_type == IB_QPT_UD ||
5417 		 hr_qp->ibqp.qp_type == IB_QPT_XRC_INI ||
5418 		 hr_qp->ibqp.qp_type == IB_QPT_XRC_TGT) &&
5419 		hr_qp->state != IB_QPS_RESET);
5420 }
5421 
5422 static int hns_roce_v2_destroy_qp_common(struct hns_roce_dev *hr_dev,
5423 					 struct hns_roce_qp *hr_qp,
5424 					 struct ib_udata *udata)
5425 {
5426 	struct ib_device *ibdev = &hr_dev->ib_dev;
5427 	struct hns_roce_cq *send_cq, *recv_cq;
5428 	unsigned long flags;
5429 	int ret = 0;
5430 
5431 	if (modify_qp_is_ok(hr_qp)) {
5432 		/* Modify qp to reset before destroying qp */
5433 		ret = hns_roce_v2_modify_qp(&hr_qp->ibqp, NULL, 0,
5434 					    hr_qp->state, IB_QPS_RESET, udata);
5435 		if (ret)
5436 			ibdev_err(ibdev,
5437 				  "failed to modify QP to RST, ret = %d.\n",
5438 				  ret);
5439 	}
5440 
5441 	send_cq = hr_qp->ibqp.send_cq ? to_hr_cq(hr_qp->ibqp.send_cq) : NULL;
5442 	recv_cq = hr_qp->ibqp.recv_cq ? to_hr_cq(hr_qp->ibqp.recv_cq) : NULL;
5443 
5444 	spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
5445 	hns_roce_lock_cqs(send_cq, recv_cq);
5446 
5447 	if (!udata) {
5448 		if (recv_cq)
5449 			__hns_roce_v2_cq_clean(recv_cq, hr_qp->qpn,
5450 					       (hr_qp->ibqp.srq ?
5451 						to_hr_srq(hr_qp->ibqp.srq) :
5452 						NULL));
5453 
5454 		if (send_cq && send_cq != recv_cq)
5455 			__hns_roce_v2_cq_clean(send_cq, hr_qp->qpn, NULL);
5456 	}
5457 
5458 	hns_roce_qp_remove(hr_dev, hr_qp);
5459 
5460 	hns_roce_unlock_cqs(send_cq, recv_cq);
5461 	spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
5462 
5463 	return ret;
5464 }
5465 
5466 int hns_roce_v2_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
5467 {
5468 	struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5469 	struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5470 	int ret;
5471 
5472 	ret = hns_roce_v2_destroy_qp_common(hr_dev, hr_qp, udata);
5473 	if (ret)
5474 		ibdev_err(&hr_dev->ib_dev,
5475 			  "failed to destroy QP, QPN = 0x%06lx, ret = %d.\n",
5476 			  hr_qp->qpn, ret);
5477 
5478 	hns_roce_qp_destroy(hr_dev, hr_qp, udata);
5479 
5480 	return 0;
5481 }
5482 
5483 static int hns_roce_v2_qp_flow_control_init(struct hns_roce_dev *hr_dev,
5484 					    struct hns_roce_qp *hr_qp)
5485 {
5486 	struct ib_device *ibdev = &hr_dev->ib_dev;
5487 	struct hns_roce_sccc_clr_done *resp;
5488 	struct hns_roce_sccc_clr *clr;
5489 	struct hns_roce_cmq_desc desc;
5490 	int ret, i;
5491 
5492 	if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
5493 		return 0;
5494 
5495 	mutex_lock(&hr_dev->qp_table.scc_mutex);
5496 
5497 	/* set scc ctx clear done flag */
5498 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_RESET_SCCC, false);
5499 	ret =  hns_roce_cmq_send(hr_dev, &desc, 1);
5500 	if (ret) {
5501 		ibdev_err(ibdev, "failed to reset SCC ctx, ret = %d.\n", ret);
5502 		goto out;
5503 	}
5504 
5505 	/* clear scc context */
5506 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CLR_SCCC, false);
5507 	clr = (struct hns_roce_sccc_clr *)desc.data;
5508 	clr->qpn = cpu_to_le32(hr_qp->qpn);
5509 	ret =  hns_roce_cmq_send(hr_dev, &desc, 1);
5510 	if (ret) {
5511 		ibdev_err(ibdev, "failed to clear SCC ctx, ret = %d.\n", ret);
5512 		goto out;
5513 	}
5514 
5515 	/* query scc context clear is done or not */
5516 	resp = (struct hns_roce_sccc_clr_done *)desc.data;
5517 	for (i = 0; i <= HNS_ROCE_CMQ_SCC_CLR_DONE_CNT; i++) {
5518 		hns_roce_cmq_setup_basic_desc(&desc,
5519 					      HNS_ROCE_OPC_QUERY_SCCC, true);
5520 		ret = hns_roce_cmq_send(hr_dev, &desc, 1);
5521 		if (ret) {
5522 			ibdev_err(ibdev, "failed to query clr cmq, ret = %d\n",
5523 				  ret);
5524 			goto out;
5525 		}
5526 
5527 		if (resp->clr_done)
5528 			goto out;
5529 
5530 		msleep(20);
5531 	}
5532 
5533 	ibdev_err(ibdev, "query SCC clr done flag overtime.\n");
5534 	ret = -ETIMEDOUT;
5535 
5536 out:
5537 	mutex_unlock(&hr_dev->qp_table.scc_mutex);
5538 	return ret;
5539 }
5540 
5541 #define DMA_IDX_SHIFT 3
5542 #define DMA_WQE_SHIFT 3
5543 
5544 static int hns_roce_v2_write_srqc_index_queue(struct hns_roce_srq *srq,
5545 					      struct hns_roce_srq_context *ctx)
5546 {
5547 	struct hns_roce_idx_que *idx_que = &srq->idx_que;
5548 	struct ib_device *ibdev = srq->ibsrq.device;
5549 	struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
5550 	u64 mtts_idx[MTT_MIN_COUNT] = {};
5551 	dma_addr_t dma_handle_idx = 0;
5552 	int ret;
5553 
5554 	/* Get physical address of idx que buf */
5555 	ret = hns_roce_mtr_find(hr_dev, &idx_que->mtr, 0, mtts_idx,
5556 				ARRAY_SIZE(mtts_idx), &dma_handle_idx);
5557 	if (ret < 1) {
5558 		ibdev_err(ibdev, "failed to find mtr for SRQ idx, ret = %d.\n",
5559 			  ret);
5560 		return -ENOBUFS;
5561 	}
5562 
5563 	hr_reg_write(ctx, SRQC_IDX_HOP_NUM,
5564 		     to_hr_hem_hopnum(hr_dev->caps.idx_hop_num, srq->wqe_cnt));
5565 
5566 	hr_reg_write(ctx, SRQC_IDX_BT_BA_L, dma_handle_idx >> DMA_IDX_SHIFT);
5567 	hr_reg_write(ctx, SRQC_IDX_BT_BA_H,
5568 		     upper_32_bits(dma_handle_idx >> DMA_IDX_SHIFT));
5569 
5570 	hr_reg_write(ctx, SRQC_IDX_BA_PG_SZ,
5571 		     to_hr_hw_page_shift(idx_que->mtr.hem_cfg.ba_pg_shift));
5572 	hr_reg_write(ctx, SRQC_IDX_BUF_PG_SZ,
5573 		     to_hr_hw_page_shift(idx_que->mtr.hem_cfg.buf_pg_shift));
5574 
5575 	hr_reg_write(ctx, SRQC_IDX_CUR_BLK_ADDR_L,
5576 		     to_hr_hw_page_addr(mtts_idx[0]));
5577 	hr_reg_write(ctx, SRQC_IDX_CUR_BLK_ADDR_H,
5578 		     upper_32_bits(to_hr_hw_page_addr(mtts_idx[0])));
5579 
5580 	hr_reg_write(ctx, SRQC_IDX_NXT_BLK_ADDR_L,
5581 		     to_hr_hw_page_addr(mtts_idx[1]));
5582 	hr_reg_write(ctx, SRQC_IDX_NXT_BLK_ADDR_H,
5583 		     upper_32_bits(to_hr_hw_page_addr(mtts_idx[1])));
5584 
5585 	return 0;
5586 }
5587 
5588 static int hns_roce_v2_write_srqc(struct hns_roce_srq *srq, void *mb_buf)
5589 {
5590 	struct ib_device *ibdev = srq->ibsrq.device;
5591 	struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
5592 	struct hns_roce_srq_context *ctx = mb_buf;
5593 	u64 mtts_wqe[MTT_MIN_COUNT] = {};
5594 	dma_addr_t dma_handle_wqe = 0;
5595 	int ret;
5596 
5597 	memset(ctx, 0, sizeof(*ctx));
5598 
5599 	/* Get the physical address of srq buf */
5600 	ret = hns_roce_mtr_find(hr_dev, &srq->buf_mtr, 0, mtts_wqe,
5601 				ARRAY_SIZE(mtts_wqe), &dma_handle_wqe);
5602 	if (ret < 1) {
5603 		ibdev_err(ibdev, "failed to find mtr for SRQ WQE, ret = %d.\n",
5604 			  ret);
5605 		return -ENOBUFS;
5606 	}
5607 
5608 	hr_reg_write(ctx, SRQC_SRQ_ST, 1);
5609 	hr_reg_write_bool(ctx, SRQC_SRQ_TYPE,
5610 			  srq->ibsrq.srq_type == IB_SRQT_XRC);
5611 	hr_reg_write(ctx, SRQC_PD, to_hr_pd(srq->ibsrq.pd)->pdn);
5612 	hr_reg_write(ctx, SRQC_SRQN, srq->srqn);
5613 	hr_reg_write(ctx, SRQC_XRCD, srq->xrcdn);
5614 	hr_reg_write(ctx, SRQC_XRC_CQN, srq->cqn);
5615 	hr_reg_write(ctx, SRQC_SHIFT, ilog2(srq->wqe_cnt));
5616 	hr_reg_write(ctx, SRQC_RQWS,
5617 		     srq->max_gs <= 0 ? 0 : fls(srq->max_gs - 1));
5618 
5619 	hr_reg_write(ctx, SRQC_WQE_HOP_NUM,
5620 		     to_hr_hem_hopnum(hr_dev->caps.srqwqe_hop_num,
5621 				      srq->wqe_cnt));
5622 
5623 	hr_reg_write(ctx, SRQC_WQE_BT_BA_L, dma_handle_wqe >> DMA_WQE_SHIFT);
5624 	hr_reg_write(ctx, SRQC_WQE_BT_BA_H,
5625 		     upper_32_bits(dma_handle_wqe >> DMA_WQE_SHIFT));
5626 
5627 	hr_reg_write(ctx, SRQC_WQE_BA_PG_SZ,
5628 		     to_hr_hw_page_shift(srq->buf_mtr.hem_cfg.ba_pg_shift));
5629 	hr_reg_write(ctx, SRQC_WQE_BUF_PG_SZ,
5630 		     to_hr_hw_page_shift(srq->buf_mtr.hem_cfg.buf_pg_shift));
5631 
5632 	return hns_roce_v2_write_srqc_index_queue(srq, ctx);
5633 }
5634 
5635 static int hns_roce_v2_modify_srq(struct ib_srq *ibsrq,
5636 				  struct ib_srq_attr *srq_attr,
5637 				  enum ib_srq_attr_mask srq_attr_mask,
5638 				  struct ib_udata *udata)
5639 {
5640 	struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device);
5641 	struct hns_roce_srq *srq = to_hr_srq(ibsrq);
5642 	struct hns_roce_srq_context *srq_context;
5643 	struct hns_roce_srq_context *srqc_mask;
5644 	struct hns_roce_cmd_mailbox *mailbox;
5645 	int ret;
5646 
5647 	/* Resizing SRQs is not supported yet */
5648 	if (srq_attr_mask & IB_SRQ_MAX_WR)
5649 		return -EINVAL;
5650 
5651 	if (srq_attr_mask & IB_SRQ_LIMIT) {
5652 		if (srq_attr->srq_limit > srq->wqe_cnt)
5653 			return -EINVAL;
5654 
5655 		mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5656 		if (IS_ERR(mailbox))
5657 			return PTR_ERR(mailbox);
5658 
5659 		srq_context = mailbox->buf;
5660 		srqc_mask = (struct hns_roce_srq_context *)mailbox->buf + 1;
5661 
5662 		memset(srqc_mask, 0xff, sizeof(*srqc_mask));
5663 
5664 		hr_reg_write(srq_context, SRQC_LIMIT_WL, srq_attr->srq_limit);
5665 		hr_reg_clear(srqc_mask, SRQC_LIMIT_WL);
5666 
5667 		ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0,
5668 					HNS_ROCE_CMD_MODIFY_SRQC, srq->srqn);
5669 		hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5670 		if (ret) {
5671 			ibdev_err(&hr_dev->ib_dev,
5672 				  "failed to handle cmd of modifying SRQ, ret = %d.\n",
5673 				  ret);
5674 			return ret;
5675 		}
5676 	}
5677 
5678 	return 0;
5679 }
5680 
5681 static int hns_roce_v2_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
5682 {
5683 	struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device);
5684 	struct hns_roce_srq *srq = to_hr_srq(ibsrq);
5685 	struct hns_roce_srq_context *srq_context;
5686 	struct hns_roce_cmd_mailbox *mailbox;
5687 	int ret;
5688 
5689 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5690 	if (IS_ERR(mailbox))
5691 		return PTR_ERR(mailbox);
5692 
5693 	srq_context = mailbox->buf;
5694 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma,
5695 				HNS_ROCE_CMD_QUERY_SRQC, srq->srqn);
5696 	if (ret) {
5697 		ibdev_err(&hr_dev->ib_dev,
5698 			  "failed to process cmd of querying SRQ, ret = %d.\n",
5699 			  ret);
5700 		goto out;
5701 	}
5702 
5703 	attr->srq_limit = hr_reg_read(srq_context, SRQC_LIMIT_WL);
5704 	attr->max_wr = srq->wqe_cnt;
5705 	attr->max_sge = srq->max_gs - srq->rsv_sge;
5706 
5707 out:
5708 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5709 	return ret;
5710 }
5711 
5712 static int hns_roce_v2_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
5713 {
5714 	struct hns_roce_dev *hr_dev = to_hr_dev(cq->device);
5715 	struct hns_roce_v2_cq_context *cq_context;
5716 	struct hns_roce_cq *hr_cq = to_hr_cq(cq);
5717 	struct hns_roce_v2_cq_context *cqc_mask;
5718 	struct hns_roce_cmd_mailbox *mailbox;
5719 	int ret;
5720 
5721 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5722 	if (IS_ERR(mailbox))
5723 		return PTR_ERR(mailbox);
5724 
5725 	cq_context = mailbox->buf;
5726 	cqc_mask = (struct hns_roce_v2_cq_context *)mailbox->buf + 1;
5727 
5728 	memset(cqc_mask, 0xff, sizeof(*cqc_mask));
5729 
5730 	hr_reg_write(cq_context, CQC_CQ_MAX_CNT, cq_count);
5731 	hr_reg_clear(cqc_mask, CQC_CQ_MAX_CNT);
5732 
5733 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
5734 		if (cq_period * HNS_ROCE_CLOCK_ADJUST > USHRT_MAX) {
5735 			dev_info(hr_dev->dev,
5736 				 "cq_period(%u) reached the upper limit, adjusted to 65.\n",
5737 				 cq_period);
5738 			cq_period = HNS_ROCE_MAX_CQ_PERIOD;
5739 		}
5740 		cq_period *= HNS_ROCE_CLOCK_ADJUST;
5741 	}
5742 	hr_reg_write(cq_context, CQC_CQ_PERIOD, cq_period);
5743 	hr_reg_clear(cqc_mask, CQC_CQ_PERIOD);
5744 
5745 	ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0,
5746 				HNS_ROCE_CMD_MODIFY_CQC, hr_cq->cqn);
5747 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5748 	if (ret)
5749 		ibdev_err(&hr_dev->ib_dev,
5750 			  "failed to process cmd when modifying CQ, ret = %d.\n",
5751 			  ret);
5752 
5753 	return ret;
5754 }
5755 
5756 static int hns_roce_v2_query_cqc(struct hns_roce_dev *hr_dev, u32 cqn,
5757 				 void *buffer)
5758 {
5759 	struct hns_roce_v2_cq_context *context;
5760 	struct hns_roce_cmd_mailbox *mailbox;
5761 	int ret;
5762 
5763 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5764 	if (IS_ERR(mailbox))
5765 		return PTR_ERR(mailbox);
5766 
5767 	context = mailbox->buf;
5768 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma,
5769 				HNS_ROCE_CMD_QUERY_CQC, cqn);
5770 	if (ret) {
5771 		ibdev_err(&hr_dev->ib_dev,
5772 			  "failed to process cmd when querying CQ, ret = %d.\n",
5773 			  ret);
5774 		goto err_mailbox;
5775 	}
5776 
5777 	memcpy(buffer, context, sizeof(*context));
5778 
5779 err_mailbox:
5780 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5781 
5782 	return ret;
5783 }
5784 
5785 static int hns_roce_v2_query_mpt(struct hns_roce_dev *hr_dev, u32 key,
5786 				 void *buffer)
5787 {
5788 	struct hns_roce_v2_mpt_entry *context;
5789 	struct hns_roce_cmd_mailbox *mailbox;
5790 	int ret;
5791 
5792 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5793 	if (IS_ERR(mailbox))
5794 		return PTR_ERR(mailbox);
5795 
5796 	context = mailbox->buf;
5797 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, HNS_ROCE_CMD_QUERY_MPT,
5798 				key_to_hw_index(key));
5799 	if (ret) {
5800 		ibdev_err(&hr_dev->ib_dev,
5801 			  "failed to process cmd when querying MPT, ret = %d.\n",
5802 			  ret);
5803 		goto err_mailbox;
5804 	}
5805 
5806 	memcpy(buffer, context, sizeof(*context));
5807 
5808 err_mailbox:
5809 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5810 
5811 	return ret;
5812 }
5813 
5814 static void hns_roce_irq_work_handle(struct work_struct *work)
5815 {
5816 	struct hns_roce_work *irq_work =
5817 				container_of(work, struct hns_roce_work, work);
5818 	struct ib_device *ibdev = &irq_work->hr_dev->ib_dev;
5819 
5820 	switch (irq_work->event_type) {
5821 	case HNS_ROCE_EVENT_TYPE_PATH_MIG:
5822 		ibdev_info(ibdev, "path migrated succeeded.\n");
5823 		break;
5824 	case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
5825 		ibdev_warn(ibdev, "path migration failed.\n");
5826 		break;
5827 	case HNS_ROCE_EVENT_TYPE_COMM_EST:
5828 		break;
5829 	case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
5830 		ibdev_warn(ibdev, "send queue drained.\n");
5831 		break;
5832 	case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
5833 		ibdev_err(ibdev, "local work queue 0x%x catast error, sub_event type is: %d\n",
5834 			  irq_work->queue_num, irq_work->sub_type);
5835 		break;
5836 	case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
5837 		ibdev_err(ibdev, "invalid request local work queue 0x%x error.\n",
5838 			  irq_work->queue_num);
5839 		break;
5840 	case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
5841 		ibdev_err(ibdev, "local access violation work queue 0x%x error, sub_event type is: %d\n",
5842 			  irq_work->queue_num, irq_work->sub_type);
5843 		break;
5844 	case HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH:
5845 		ibdev_warn(ibdev, "SRQ limit reach.\n");
5846 		break;
5847 	case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
5848 		ibdev_warn(ibdev, "SRQ last wqe reach.\n");
5849 		break;
5850 	case HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR:
5851 		ibdev_err(ibdev, "SRQ catas error.\n");
5852 		break;
5853 	case HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR:
5854 		ibdev_err(ibdev, "CQ 0x%x access err.\n", irq_work->queue_num);
5855 		break;
5856 	case HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW:
5857 		ibdev_warn(ibdev, "CQ 0x%x overflow\n", irq_work->queue_num);
5858 		break;
5859 	case HNS_ROCE_EVENT_TYPE_DB_OVERFLOW:
5860 		ibdev_warn(ibdev, "DB overflow.\n");
5861 		break;
5862 	case HNS_ROCE_EVENT_TYPE_FLR:
5863 		ibdev_warn(ibdev, "function level reset.\n");
5864 		break;
5865 	case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
5866 		ibdev_err(ibdev, "xrc domain violation error.\n");
5867 		break;
5868 	case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
5869 		ibdev_err(ibdev, "invalid xrceth error.\n");
5870 		break;
5871 	default:
5872 		break;
5873 	}
5874 
5875 	kfree(irq_work);
5876 }
5877 
5878 static void hns_roce_v2_init_irq_work(struct hns_roce_dev *hr_dev,
5879 				      struct hns_roce_eq *eq, u32 queue_num)
5880 {
5881 	struct hns_roce_work *irq_work;
5882 
5883 	irq_work = kzalloc(sizeof(struct hns_roce_work), GFP_ATOMIC);
5884 	if (!irq_work)
5885 		return;
5886 
5887 	INIT_WORK(&irq_work->work, hns_roce_irq_work_handle);
5888 	irq_work->hr_dev = hr_dev;
5889 	irq_work->event_type = eq->event_type;
5890 	irq_work->sub_type = eq->sub_type;
5891 	irq_work->queue_num = queue_num;
5892 	queue_work(hr_dev->irq_workq, &irq_work->work);
5893 }
5894 
5895 static void update_eq_db(struct hns_roce_eq *eq)
5896 {
5897 	struct hns_roce_dev *hr_dev = eq->hr_dev;
5898 	struct hns_roce_v2_db eq_db = {};
5899 
5900 	if (eq->type_flag == HNS_ROCE_AEQ) {
5901 		hr_reg_write(&eq_db, EQ_DB_CMD,
5902 			     eq->arm_st == HNS_ROCE_V2_EQ_ALWAYS_ARMED ?
5903 			     HNS_ROCE_EQ_DB_CMD_AEQ :
5904 			     HNS_ROCE_EQ_DB_CMD_AEQ_ARMED);
5905 	} else {
5906 		hr_reg_write(&eq_db, EQ_DB_TAG, eq->eqn);
5907 
5908 		hr_reg_write(&eq_db, EQ_DB_CMD,
5909 			     eq->arm_st == HNS_ROCE_V2_EQ_ALWAYS_ARMED ?
5910 			     HNS_ROCE_EQ_DB_CMD_CEQ :
5911 			     HNS_ROCE_EQ_DB_CMD_CEQ_ARMED);
5912 	}
5913 
5914 	hr_reg_write(&eq_db, EQ_DB_CI, eq->cons_index);
5915 
5916 	hns_roce_write64(hr_dev, (__le32 *)&eq_db, eq->db_reg);
5917 }
5918 
5919 static struct hns_roce_aeqe *next_aeqe_sw_v2(struct hns_roce_eq *eq)
5920 {
5921 	struct hns_roce_aeqe *aeqe;
5922 
5923 	aeqe = hns_roce_buf_offset(eq->mtr.kmem,
5924 				   (eq->cons_index & (eq->entries - 1)) *
5925 				   eq->eqe_size);
5926 
5927 	return (hr_reg_read(aeqe, AEQE_OWNER) ^
5928 		!!(eq->cons_index & eq->entries)) ? aeqe : NULL;
5929 }
5930 
5931 static irqreturn_t hns_roce_v2_aeq_int(struct hns_roce_dev *hr_dev,
5932 				       struct hns_roce_eq *eq)
5933 {
5934 	struct device *dev = hr_dev->dev;
5935 	struct hns_roce_aeqe *aeqe = next_aeqe_sw_v2(eq);
5936 	irqreturn_t aeqe_found = IRQ_NONE;
5937 	int event_type;
5938 	u32 queue_num;
5939 	int sub_type;
5940 
5941 	while (aeqe) {
5942 		/* Make sure we read AEQ entry after we have checked the
5943 		 * ownership bit
5944 		 */
5945 		dma_rmb();
5946 
5947 		event_type = hr_reg_read(aeqe, AEQE_EVENT_TYPE);
5948 		sub_type = hr_reg_read(aeqe, AEQE_SUB_TYPE);
5949 		queue_num = hr_reg_read(aeqe, AEQE_EVENT_QUEUE_NUM);
5950 
5951 		switch (event_type) {
5952 		case HNS_ROCE_EVENT_TYPE_PATH_MIG:
5953 		case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
5954 		case HNS_ROCE_EVENT_TYPE_COMM_EST:
5955 		case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
5956 		case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
5957 		case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
5958 		case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
5959 		case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
5960 		case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
5961 		case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
5962 			hns_roce_qp_event(hr_dev, queue_num, event_type);
5963 			break;
5964 		case HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH:
5965 		case HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR:
5966 			hns_roce_srq_event(hr_dev, queue_num, event_type);
5967 			break;
5968 		case HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR:
5969 		case HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW:
5970 			hns_roce_cq_event(hr_dev, queue_num, event_type);
5971 			break;
5972 		case HNS_ROCE_EVENT_TYPE_MB:
5973 			hns_roce_cmd_event(hr_dev,
5974 					le16_to_cpu(aeqe->event.cmd.token),
5975 					aeqe->event.cmd.status,
5976 					le64_to_cpu(aeqe->event.cmd.out_param));
5977 			break;
5978 		case HNS_ROCE_EVENT_TYPE_DB_OVERFLOW:
5979 		case HNS_ROCE_EVENT_TYPE_FLR:
5980 			break;
5981 		default:
5982 			dev_err(dev, "unhandled event %d on EQ %d at idx %u.\n",
5983 				event_type, eq->eqn, eq->cons_index);
5984 			break;
5985 		}
5986 
5987 		eq->event_type = event_type;
5988 		eq->sub_type = sub_type;
5989 		++eq->cons_index;
5990 		aeqe_found = IRQ_HANDLED;
5991 
5992 		hns_roce_v2_init_irq_work(hr_dev, eq, queue_num);
5993 
5994 		aeqe = next_aeqe_sw_v2(eq);
5995 	}
5996 
5997 	update_eq_db(eq);
5998 
5999 	return IRQ_RETVAL(aeqe_found);
6000 }
6001 
6002 static struct hns_roce_ceqe *next_ceqe_sw_v2(struct hns_roce_eq *eq)
6003 {
6004 	struct hns_roce_ceqe *ceqe;
6005 
6006 	ceqe = hns_roce_buf_offset(eq->mtr.kmem,
6007 				   (eq->cons_index & (eq->entries - 1)) *
6008 				   eq->eqe_size);
6009 
6010 	return (hr_reg_read(ceqe, CEQE_OWNER) ^
6011 		!!(eq->cons_index & eq->entries)) ? ceqe : NULL;
6012 }
6013 
6014 static irqreturn_t hns_roce_v2_ceq_int(struct hns_roce_dev *hr_dev,
6015 				       struct hns_roce_eq *eq)
6016 {
6017 	struct hns_roce_ceqe *ceqe = next_ceqe_sw_v2(eq);
6018 	irqreturn_t ceqe_found = IRQ_NONE;
6019 	u32 cqn;
6020 
6021 	while (ceqe) {
6022 		/* Make sure we read CEQ entry after we have checked the
6023 		 * ownership bit
6024 		 */
6025 		dma_rmb();
6026 
6027 		cqn = hr_reg_read(ceqe, CEQE_CQN);
6028 
6029 		hns_roce_cq_completion(hr_dev, cqn);
6030 
6031 		++eq->cons_index;
6032 		ceqe_found = IRQ_HANDLED;
6033 
6034 		ceqe = next_ceqe_sw_v2(eq);
6035 	}
6036 
6037 	update_eq_db(eq);
6038 
6039 	return IRQ_RETVAL(ceqe_found);
6040 }
6041 
6042 static irqreturn_t hns_roce_v2_msix_interrupt_eq(int irq, void *eq_ptr)
6043 {
6044 	struct hns_roce_eq *eq = eq_ptr;
6045 	struct hns_roce_dev *hr_dev = eq->hr_dev;
6046 	irqreturn_t int_work;
6047 
6048 	if (eq->type_flag == HNS_ROCE_CEQ)
6049 		/* Completion event interrupt */
6050 		int_work = hns_roce_v2_ceq_int(hr_dev, eq);
6051 	else
6052 		/* Asynchronous event interrupt */
6053 		int_work = hns_roce_v2_aeq_int(hr_dev, eq);
6054 
6055 	return IRQ_RETVAL(int_work);
6056 }
6057 
6058 static irqreturn_t abnormal_interrupt_basic(struct hns_roce_dev *hr_dev,
6059 					    u32 int_st)
6060 {
6061 	struct pci_dev *pdev = hr_dev->pci_dev;
6062 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
6063 	const struct hnae3_ae_ops *ops = ae_dev->ops;
6064 	irqreturn_t int_work = IRQ_NONE;
6065 	u32 int_en;
6066 
6067 	int_en = roce_read(hr_dev, ROCEE_VF_ABN_INT_EN_REG);
6068 
6069 	if (int_st & BIT(HNS_ROCE_V2_VF_INT_ST_AEQ_OVERFLOW_S)) {
6070 		dev_err(hr_dev->dev, "AEQ overflow!\n");
6071 
6072 		roce_write(hr_dev, ROCEE_VF_ABN_INT_ST_REG,
6073 			   1 << HNS_ROCE_V2_VF_INT_ST_AEQ_OVERFLOW_S);
6074 
6075 		/* Set reset level for reset_event() */
6076 		if (ops->set_default_reset_request)
6077 			ops->set_default_reset_request(ae_dev,
6078 						       HNAE3_FUNC_RESET);
6079 		if (ops->reset_event)
6080 			ops->reset_event(pdev, NULL);
6081 
6082 		int_en |= 1 << HNS_ROCE_V2_VF_ABN_INT_EN_S;
6083 		roce_write(hr_dev, ROCEE_VF_ABN_INT_EN_REG, int_en);
6084 
6085 		int_work = IRQ_HANDLED;
6086 	} else {
6087 		dev_err(hr_dev->dev, "there is no basic abn irq found.\n");
6088 	}
6089 
6090 	return IRQ_RETVAL(int_work);
6091 }
6092 
6093 static int fmea_ram_ecc_query(struct hns_roce_dev *hr_dev,
6094 			       struct fmea_ram_ecc *ecc_info)
6095 {
6096 	struct hns_roce_cmq_desc desc;
6097 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
6098 	int ret;
6099 
6100 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_QUERY_RAM_ECC, true);
6101 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
6102 	if (ret)
6103 		return ret;
6104 
6105 	ecc_info->is_ecc_err = hr_reg_read(req, QUERY_RAM_ECC_1BIT_ERR);
6106 	ecc_info->res_type = hr_reg_read(req, QUERY_RAM_ECC_RES_TYPE);
6107 	ecc_info->index = hr_reg_read(req, QUERY_RAM_ECC_TAG);
6108 
6109 	return 0;
6110 }
6111 
6112 static int fmea_recover_gmv(struct hns_roce_dev *hr_dev, u32 idx)
6113 {
6114 	struct hns_roce_cmq_desc desc;
6115 	struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
6116 	u32 addr_upper;
6117 	u32 addr_low;
6118 	int ret;
6119 
6120 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GMV_BT, true);
6121 	hr_reg_write(req, CFG_GMV_BT_IDX, idx);
6122 
6123 	ret = hns_roce_cmq_send(hr_dev, &desc, 1);
6124 	if (ret) {
6125 		dev_err(hr_dev->dev,
6126 			"failed to execute cmd to read gmv, ret = %d.\n", ret);
6127 		return ret;
6128 	}
6129 
6130 	addr_low =  hr_reg_read(req, CFG_GMV_BT_BA_L);
6131 	addr_upper = hr_reg_read(req, CFG_GMV_BT_BA_H);
6132 
6133 	hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GMV_BT, false);
6134 	hr_reg_write(req, CFG_GMV_BT_BA_L, addr_low);
6135 	hr_reg_write(req, CFG_GMV_BT_BA_H, addr_upper);
6136 	hr_reg_write(req, CFG_GMV_BT_IDX, idx);
6137 
6138 	return hns_roce_cmq_send(hr_dev, &desc, 1);
6139 }
6140 
6141 static u64 fmea_get_ram_res_addr(u32 res_type, __le64 *data)
6142 {
6143 	if (res_type == ECC_RESOURCE_QPC_TIMER ||
6144 	    res_type == ECC_RESOURCE_CQC_TIMER ||
6145 	    res_type == ECC_RESOURCE_SCCC)
6146 		return le64_to_cpu(*data);
6147 
6148 	return le64_to_cpu(*data) << PAGE_SHIFT;
6149 }
6150 
6151 static int fmea_recover_others(struct hns_roce_dev *hr_dev, u32 res_type,
6152 			       u32 index)
6153 {
6154 	u8 write_bt0_op = fmea_ram_res[res_type].write_bt0_op;
6155 	u8 read_bt0_op = fmea_ram_res[res_type].read_bt0_op;
6156 	struct hns_roce_cmd_mailbox *mailbox;
6157 	u64 addr;
6158 	int ret;
6159 
6160 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
6161 	if (IS_ERR(mailbox))
6162 		return PTR_ERR(mailbox);
6163 
6164 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, read_bt0_op, index);
6165 	if (ret) {
6166 		dev_err(hr_dev->dev,
6167 			"failed to execute cmd to read fmea ram, ret = %d.\n",
6168 			ret);
6169 		goto out;
6170 	}
6171 
6172 	addr = fmea_get_ram_res_addr(res_type, mailbox->buf);
6173 
6174 	ret = hns_roce_cmd_mbox(hr_dev, addr, 0, write_bt0_op, index);
6175 	if (ret)
6176 		dev_err(hr_dev->dev,
6177 			"failed to execute cmd to write fmea ram, ret = %d.\n",
6178 			ret);
6179 
6180 out:
6181 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
6182 	return ret;
6183 }
6184 
6185 static void fmea_ram_ecc_recover(struct hns_roce_dev *hr_dev,
6186 				 struct fmea_ram_ecc *ecc_info)
6187 {
6188 	u32 res_type = ecc_info->res_type;
6189 	u32 index = ecc_info->index;
6190 	int ret;
6191 
6192 	BUILD_BUG_ON(ARRAY_SIZE(fmea_ram_res) != ECC_RESOURCE_COUNT);
6193 
6194 	if (res_type >= ECC_RESOURCE_COUNT) {
6195 		dev_err(hr_dev->dev, "unsupported fmea ram ecc type %u.\n",
6196 			res_type);
6197 		return;
6198 	}
6199 
6200 	if (res_type == ECC_RESOURCE_GMV)
6201 		ret = fmea_recover_gmv(hr_dev, index);
6202 	else
6203 		ret = fmea_recover_others(hr_dev, res_type, index);
6204 	if (ret)
6205 		dev_err(hr_dev->dev,
6206 			"failed to recover %s, index = %u, ret = %d.\n",
6207 			fmea_ram_res[res_type].name, index, ret);
6208 }
6209 
6210 static void fmea_ram_ecc_work(struct work_struct *ecc_work)
6211 {
6212 	struct hns_roce_dev *hr_dev =
6213 		container_of(ecc_work, struct hns_roce_dev, ecc_work);
6214 	struct fmea_ram_ecc ecc_info = {};
6215 
6216 	if (fmea_ram_ecc_query(hr_dev, &ecc_info)) {
6217 		dev_err(hr_dev->dev, "failed to query fmea ram ecc.\n");
6218 		return;
6219 	}
6220 
6221 	if (!ecc_info.is_ecc_err) {
6222 		dev_err(hr_dev->dev, "there is no fmea ram ecc err found.\n");
6223 		return;
6224 	}
6225 
6226 	fmea_ram_ecc_recover(hr_dev, &ecc_info);
6227 }
6228 
6229 static irqreturn_t hns_roce_v2_msix_interrupt_abn(int irq, void *dev_id)
6230 {
6231 	struct hns_roce_dev *hr_dev = dev_id;
6232 	irqreturn_t int_work = IRQ_NONE;
6233 	u32 int_st;
6234 
6235 	int_st = roce_read(hr_dev, ROCEE_VF_ABN_INT_ST_REG);
6236 
6237 	if (int_st) {
6238 		int_work = abnormal_interrupt_basic(hr_dev, int_st);
6239 	} else if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
6240 		queue_work(hr_dev->irq_workq, &hr_dev->ecc_work);
6241 		int_work = IRQ_HANDLED;
6242 	} else {
6243 		dev_err(hr_dev->dev, "there is no abnormal irq found.\n");
6244 	}
6245 
6246 	return IRQ_RETVAL(int_work);
6247 }
6248 
6249 static void hns_roce_v2_int_mask_enable(struct hns_roce_dev *hr_dev,
6250 					int eq_num, u32 enable_flag)
6251 {
6252 	int i;
6253 
6254 	for (i = 0; i < eq_num; i++)
6255 		roce_write(hr_dev, ROCEE_VF_EVENT_INT_EN_REG +
6256 			   i * EQ_REG_OFFSET, enable_flag);
6257 
6258 	roce_write(hr_dev, ROCEE_VF_ABN_INT_EN_REG, enable_flag);
6259 	roce_write(hr_dev, ROCEE_VF_ABN_INT_CFG_REG, enable_flag);
6260 }
6261 
6262 static void hns_roce_v2_destroy_eqc(struct hns_roce_dev *hr_dev, u32 eqn)
6263 {
6264 	struct device *dev = hr_dev->dev;
6265 	int ret;
6266 	u8 cmd;
6267 
6268 	if (eqn < hr_dev->caps.num_comp_vectors)
6269 		cmd = HNS_ROCE_CMD_DESTROY_CEQC;
6270 	else
6271 		cmd = HNS_ROCE_CMD_DESTROY_AEQC;
6272 
6273 	ret = hns_roce_destroy_hw_ctx(hr_dev, cmd, eqn & HNS_ROCE_V2_EQN_M);
6274 	if (ret)
6275 		dev_err(dev, "[mailbox cmd] destroy eqc(%u) failed.\n", eqn);
6276 }
6277 
6278 static void free_eq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq)
6279 {
6280 	hns_roce_mtr_destroy(hr_dev, &eq->mtr);
6281 }
6282 
6283 static void init_eq_config(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq)
6284 {
6285 	eq->db_reg = hr_dev->reg_base + ROCEE_VF_EQ_DB_CFG0_REG;
6286 	eq->cons_index = 0;
6287 	eq->over_ignore = HNS_ROCE_V2_EQ_OVER_IGNORE_0;
6288 	eq->coalesce = HNS_ROCE_V2_EQ_COALESCE_0;
6289 	eq->arm_st = HNS_ROCE_V2_EQ_ALWAYS_ARMED;
6290 	eq->shift = ilog2((unsigned int)eq->entries);
6291 }
6292 
6293 static int config_eqc(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq,
6294 		      void *mb_buf)
6295 {
6296 	u64 eqe_ba[MTT_MIN_COUNT] = { 0 };
6297 	struct hns_roce_eq_context *eqc;
6298 	u64 bt_ba = 0;
6299 	int count;
6300 
6301 	eqc = mb_buf;
6302 	memset(eqc, 0, sizeof(struct hns_roce_eq_context));
6303 
6304 	init_eq_config(hr_dev, eq);
6305 
6306 	/* if not multi-hop, eqe buffer only use one trunk */
6307 	count = hns_roce_mtr_find(hr_dev, &eq->mtr, 0, eqe_ba, MTT_MIN_COUNT,
6308 				  &bt_ba);
6309 	if (count < 1) {
6310 		dev_err(hr_dev->dev, "failed to find EQE mtr\n");
6311 		return -ENOBUFS;
6312 	}
6313 
6314 	hr_reg_write(eqc, EQC_EQ_ST, HNS_ROCE_V2_EQ_STATE_VALID);
6315 	hr_reg_write(eqc, EQC_EQE_HOP_NUM, eq->hop_num);
6316 	hr_reg_write(eqc, EQC_OVER_IGNORE, eq->over_ignore);
6317 	hr_reg_write(eqc, EQC_COALESCE, eq->coalesce);
6318 	hr_reg_write(eqc, EQC_ARM_ST, eq->arm_st);
6319 	hr_reg_write(eqc, EQC_EQN, eq->eqn);
6320 	hr_reg_write(eqc, EQC_EQE_CNT, HNS_ROCE_EQ_INIT_EQE_CNT);
6321 	hr_reg_write(eqc, EQC_EQE_BA_PG_SZ,
6322 		     to_hr_hw_page_shift(eq->mtr.hem_cfg.ba_pg_shift));
6323 	hr_reg_write(eqc, EQC_EQE_BUF_PG_SZ,
6324 		     to_hr_hw_page_shift(eq->mtr.hem_cfg.buf_pg_shift));
6325 	hr_reg_write(eqc, EQC_EQ_PROD_INDX, HNS_ROCE_EQ_INIT_PROD_IDX);
6326 	hr_reg_write(eqc, EQC_EQ_MAX_CNT, eq->eq_max_cnt);
6327 
6328 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
6329 		if (eq->eq_period * HNS_ROCE_CLOCK_ADJUST > USHRT_MAX) {
6330 			dev_info(hr_dev->dev, "eq_period(%u) reached the upper limit, adjusted to 65.\n",
6331 				 eq->eq_period);
6332 			eq->eq_period = HNS_ROCE_MAX_EQ_PERIOD;
6333 		}
6334 		eq->eq_period *= HNS_ROCE_CLOCK_ADJUST;
6335 	}
6336 
6337 	hr_reg_write(eqc, EQC_EQ_PERIOD, eq->eq_period);
6338 	hr_reg_write(eqc, EQC_EQE_REPORT_TIMER, HNS_ROCE_EQ_INIT_REPORT_TIMER);
6339 	hr_reg_write(eqc, EQC_EQE_BA_L, bt_ba >> 3);
6340 	hr_reg_write(eqc, EQC_EQE_BA_H, bt_ba >> 35);
6341 	hr_reg_write(eqc, EQC_SHIFT, eq->shift);
6342 	hr_reg_write(eqc, EQC_MSI_INDX, HNS_ROCE_EQ_INIT_MSI_IDX);
6343 	hr_reg_write(eqc, EQC_CUR_EQE_BA_L, eqe_ba[0] >> 12);
6344 	hr_reg_write(eqc, EQC_CUR_EQE_BA_M, eqe_ba[0] >> 28);
6345 	hr_reg_write(eqc, EQC_CUR_EQE_BA_H, eqe_ba[0] >> 60);
6346 	hr_reg_write(eqc, EQC_EQ_CONS_INDX, HNS_ROCE_EQ_INIT_CONS_IDX);
6347 	hr_reg_write(eqc, EQC_NEX_EQE_BA_L, eqe_ba[1] >> 12);
6348 	hr_reg_write(eqc, EQC_NEX_EQE_BA_H, eqe_ba[1] >> 44);
6349 	hr_reg_write(eqc, EQC_EQE_SIZE, eq->eqe_size == HNS_ROCE_V3_EQE_SIZE);
6350 
6351 	return 0;
6352 }
6353 
6354 static int alloc_eq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq)
6355 {
6356 	struct hns_roce_buf_attr buf_attr = {};
6357 	int err;
6358 
6359 	if (hr_dev->caps.eqe_hop_num == HNS_ROCE_HOP_NUM_0)
6360 		eq->hop_num = 0;
6361 	else
6362 		eq->hop_num = hr_dev->caps.eqe_hop_num;
6363 
6364 	buf_attr.page_shift = hr_dev->caps.eqe_buf_pg_sz + PAGE_SHIFT;
6365 	buf_attr.region[0].size = eq->entries * eq->eqe_size;
6366 	buf_attr.region[0].hopnum = eq->hop_num;
6367 	buf_attr.region_count = 1;
6368 
6369 	err = hns_roce_mtr_create(hr_dev, &eq->mtr, &buf_attr,
6370 				  hr_dev->caps.eqe_ba_pg_sz + PAGE_SHIFT, NULL,
6371 				  0);
6372 	if (err)
6373 		dev_err(hr_dev->dev, "failed to alloc EQE mtr, err %d\n", err);
6374 
6375 	return err;
6376 }
6377 
6378 static int hns_roce_v2_create_eq(struct hns_roce_dev *hr_dev,
6379 				 struct hns_roce_eq *eq, u8 eq_cmd)
6380 {
6381 	struct hns_roce_cmd_mailbox *mailbox;
6382 	int ret;
6383 
6384 	/* Allocate mailbox memory */
6385 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
6386 	if (IS_ERR(mailbox))
6387 		return PTR_ERR(mailbox);
6388 
6389 	ret = alloc_eq_buf(hr_dev, eq);
6390 	if (ret)
6391 		goto free_cmd_mbox;
6392 
6393 	ret = config_eqc(hr_dev, eq, mailbox->buf);
6394 	if (ret)
6395 		goto err_cmd_mbox;
6396 
6397 	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, eq_cmd, eq->eqn);
6398 	if (ret) {
6399 		dev_err(hr_dev->dev, "[mailbox cmd] create eqc failed.\n");
6400 		goto err_cmd_mbox;
6401 	}
6402 
6403 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
6404 
6405 	return 0;
6406 
6407 err_cmd_mbox:
6408 	free_eq_buf(hr_dev, eq);
6409 
6410 free_cmd_mbox:
6411 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
6412 
6413 	return ret;
6414 }
6415 
6416 static int __hns_roce_request_irq(struct hns_roce_dev *hr_dev, int irq_num,
6417 				  int comp_num, int aeq_num, int other_num)
6418 {
6419 	struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
6420 	int i, j;
6421 	int ret;
6422 
6423 	for (i = 0; i < irq_num; i++) {
6424 		hr_dev->irq_names[i] = kzalloc(HNS_ROCE_INT_NAME_LEN,
6425 					       GFP_KERNEL);
6426 		if (!hr_dev->irq_names[i]) {
6427 			ret = -ENOMEM;
6428 			goto err_kzalloc_failed;
6429 		}
6430 	}
6431 
6432 	/* irq contains: abnormal + AEQ + CEQ */
6433 	for (j = 0; j < other_num; j++)
6434 		snprintf((char *)hr_dev->irq_names[j], HNS_ROCE_INT_NAME_LEN,
6435 			 "hns-abn-%d", j);
6436 
6437 	for (j = other_num; j < (other_num + aeq_num); j++)
6438 		snprintf((char *)hr_dev->irq_names[j], HNS_ROCE_INT_NAME_LEN,
6439 			 "hns-aeq-%d", j - other_num);
6440 
6441 	for (j = (other_num + aeq_num); j < irq_num; j++)
6442 		snprintf((char *)hr_dev->irq_names[j], HNS_ROCE_INT_NAME_LEN,
6443 			 "hns-ceq-%d", j - other_num - aeq_num);
6444 
6445 	for (j = 0; j < irq_num; j++) {
6446 		if (j < other_num)
6447 			ret = request_irq(hr_dev->irq[j],
6448 					  hns_roce_v2_msix_interrupt_abn,
6449 					  0, hr_dev->irq_names[j], hr_dev);
6450 
6451 		else if (j < (other_num + comp_num))
6452 			ret = request_irq(eq_table->eq[j - other_num].irq,
6453 					  hns_roce_v2_msix_interrupt_eq,
6454 					  0, hr_dev->irq_names[j + aeq_num],
6455 					  &eq_table->eq[j - other_num]);
6456 		else
6457 			ret = request_irq(eq_table->eq[j - other_num].irq,
6458 					  hns_roce_v2_msix_interrupt_eq,
6459 					  0, hr_dev->irq_names[j - comp_num],
6460 					  &eq_table->eq[j - other_num]);
6461 		if (ret) {
6462 			dev_err(hr_dev->dev, "request irq error!\n");
6463 			goto err_request_failed;
6464 		}
6465 	}
6466 
6467 	return 0;
6468 
6469 err_request_failed:
6470 	for (j -= 1; j >= 0; j--)
6471 		if (j < other_num)
6472 			free_irq(hr_dev->irq[j], hr_dev);
6473 		else
6474 			free_irq(eq_table->eq[j - other_num].irq,
6475 				 &eq_table->eq[j - other_num]);
6476 
6477 err_kzalloc_failed:
6478 	for (i -= 1; i >= 0; i--)
6479 		kfree(hr_dev->irq_names[i]);
6480 
6481 	return ret;
6482 }
6483 
6484 static void __hns_roce_free_irq(struct hns_roce_dev *hr_dev)
6485 {
6486 	int irq_num;
6487 	int eq_num;
6488 	int i;
6489 
6490 	eq_num = hr_dev->caps.num_comp_vectors + hr_dev->caps.num_aeq_vectors;
6491 	irq_num = eq_num + hr_dev->caps.num_other_vectors;
6492 
6493 	for (i = 0; i < hr_dev->caps.num_other_vectors; i++)
6494 		free_irq(hr_dev->irq[i], hr_dev);
6495 
6496 	for (i = 0; i < eq_num; i++)
6497 		free_irq(hr_dev->eq_table.eq[i].irq, &hr_dev->eq_table.eq[i]);
6498 
6499 	for (i = 0; i < irq_num; i++)
6500 		kfree(hr_dev->irq_names[i]);
6501 }
6502 
6503 static int hns_roce_v2_init_eq_table(struct hns_roce_dev *hr_dev)
6504 {
6505 	struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
6506 	struct device *dev = hr_dev->dev;
6507 	struct hns_roce_eq *eq;
6508 	int other_num;
6509 	int comp_num;
6510 	int aeq_num;
6511 	int irq_num;
6512 	int eq_num;
6513 	u8 eq_cmd;
6514 	int ret;
6515 	int i;
6516 
6517 	other_num = hr_dev->caps.num_other_vectors;
6518 	comp_num = hr_dev->caps.num_comp_vectors;
6519 	aeq_num = hr_dev->caps.num_aeq_vectors;
6520 
6521 	eq_num = comp_num + aeq_num;
6522 	irq_num = eq_num + other_num;
6523 
6524 	eq_table->eq = kcalloc(eq_num, sizeof(*eq_table->eq), GFP_KERNEL);
6525 	if (!eq_table->eq)
6526 		return -ENOMEM;
6527 
6528 	/* create eq */
6529 	for (i = 0; i < eq_num; i++) {
6530 		eq = &eq_table->eq[i];
6531 		eq->hr_dev = hr_dev;
6532 		eq->eqn = i;
6533 		if (i < comp_num) {
6534 			/* CEQ */
6535 			eq_cmd = HNS_ROCE_CMD_CREATE_CEQC;
6536 			eq->type_flag = HNS_ROCE_CEQ;
6537 			eq->entries = hr_dev->caps.ceqe_depth;
6538 			eq->eqe_size = hr_dev->caps.ceqe_size;
6539 			eq->irq = hr_dev->irq[i + other_num + aeq_num];
6540 			eq->eq_max_cnt = HNS_ROCE_CEQ_DEFAULT_BURST_NUM;
6541 			eq->eq_period = HNS_ROCE_CEQ_DEFAULT_INTERVAL;
6542 		} else {
6543 			/* AEQ */
6544 			eq_cmd = HNS_ROCE_CMD_CREATE_AEQC;
6545 			eq->type_flag = HNS_ROCE_AEQ;
6546 			eq->entries = hr_dev->caps.aeqe_depth;
6547 			eq->eqe_size = hr_dev->caps.aeqe_size;
6548 			eq->irq = hr_dev->irq[i - comp_num + other_num];
6549 			eq->eq_max_cnt = HNS_ROCE_AEQ_DEFAULT_BURST_NUM;
6550 			eq->eq_period = HNS_ROCE_AEQ_DEFAULT_INTERVAL;
6551 		}
6552 
6553 		ret = hns_roce_v2_create_eq(hr_dev, eq, eq_cmd);
6554 		if (ret) {
6555 			dev_err(dev, "failed to create eq.\n");
6556 			goto err_create_eq_fail;
6557 		}
6558 	}
6559 
6560 	INIT_WORK(&hr_dev->ecc_work, fmea_ram_ecc_work);
6561 
6562 	hr_dev->irq_workq = alloc_ordered_workqueue("hns_roce_irq_workq", 0);
6563 	if (!hr_dev->irq_workq) {
6564 		dev_err(dev, "failed to create irq workqueue.\n");
6565 		ret = -ENOMEM;
6566 		goto err_create_eq_fail;
6567 	}
6568 
6569 	ret = __hns_roce_request_irq(hr_dev, irq_num, comp_num, aeq_num,
6570 				     other_num);
6571 	if (ret) {
6572 		dev_err(dev, "failed to request irq.\n");
6573 		goto err_request_irq_fail;
6574 	}
6575 
6576 	/* enable irq */
6577 	hns_roce_v2_int_mask_enable(hr_dev, eq_num, EQ_ENABLE);
6578 
6579 	return 0;
6580 
6581 err_request_irq_fail:
6582 	destroy_workqueue(hr_dev->irq_workq);
6583 
6584 err_create_eq_fail:
6585 	for (i -= 1; i >= 0; i--)
6586 		free_eq_buf(hr_dev, &eq_table->eq[i]);
6587 	kfree(eq_table->eq);
6588 
6589 	return ret;
6590 }
6591 
6592 static void hns_roce_v2_cleanup_eq_table(struct hns_roce_dev *hr_dev)
6593 {
6594 	struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
6595 	int eq_num;
6596 	int i;
6597 
6598 	eq_num = hr_dev->caps.num_comp_vectors + hr_dev->caps.num_aeq_vectors;
6599 
6600 	/* Disable irq */
6601 	hns_roce_v2_int_mask_enable(hr_dev, eq_num, EQ_DISABLE);
6602 
6603 	__hns_roce_free_irq(hr_dev);
6604 	destroy_workqueue(hr_dev->irq_workq);
6605 
6606 	for (i = 0; i < eq_num; i++) {
6607 		hns_roce_v2_destroy_eqc(hr_dev, i);
6608 
6609 		free_eq_buf(hr_dev, &eq_table->eq[i]);
6610 	}
6611 
6612 	kfree(eq_table->eq);
6613 }
6614 
6615 static const struct ib_device_ops hns_roce_v2_dev_ops = {
6616 	.destroy_qp = hns_roce_v2_destroy_qp,
6617 	.modify_cq = hns_roce_v2_modify_cq,
6618 	.poll_cq = hns_roce_v2_poll_cq,
6619 	.post_recv = hns_roce_v2_post_recv,
6620 	.post_send = hns_roce_v2_post_send,
6621 	.query_qp = hns_roce_v2_query_qp,
6622 	.req_notify_cq = hns_roce_v2_req_notify_cq,
6623 };
6624 
6625 static const struct ib_device_ops hns_roce_v2_dev_srq_ops = {
6626 	.modify_srq = hns_roce_v2_modify_srq,
6627 	.post_srq_recv = hns_roce_v2_post_srq_recv,
6628 	.query_srq = hns_roce_v2_query_srq,
6629 };
6630 
6631 static const struct hns_roce_hw hns_roce_hw_v2 = {
6632 	.cmq_init = hns_roce_v2_cmq_init,
6633 	.cmq_exit = hns_roce_v2_cmq_exit,
6634 	.hw_profile = hns_roce_v2_profile,
6635 	.hw_init = hns_roce_v2_init,
6636 	.hw_exit = hns_roce_v2_exit,
6637 	.post_mbox = v2_post_mbox,
6638 	.poll_mbox_done = v2_poll_mbox_done,
6639 	.chk_mbox_avail = v2_chk_mbox_is_avail,
6640 	.set_gid = hns_roce_v2_set_gid,
6641 	.set_mac = hns_roce_v2_set_mac,
6642 	.write_mtpt = hns_roce_v2_write_mtpt,
6643 	.rereg_write_mtpt = hns_roce_v2_rereg_write_mtpt,
6644 	.frmr_write_mtpt = hns_roce_v2_frmr_write_mtpt,
6645 	.mw_write_mtpt = hns_roce_v2_mw_write_mtpt,
6646 	.write_cqc = hns_roce_v2_write_cqc,
6647 	.set_hem = hns_roce_v2_set_hem,
6648 	.clear_hem = hns_roce_v2_clear_hem,
6649 	.modify_qp = hns_roce_v2_modify_qp,
6650 	.dereg_mr = hns_roce_v2_dereg_mr,
6651 	.qp_flow_control_init = hns_roce_v2_qp_flow_control_init,
6652 	.init_eq = hns_roce_v2_init_eq_table,
6653 	.cleanup_eq = hns_roce_v2_cleanup_eq_table,
6654 	.write_srqc = hns_roce_v2_write_srqc,
6655 	.query_cqc = hns_roce_v2_query_cqc,
6656 	.query_qpc = hns_roce_v2_query_qpc,
6657 	.query_mpt = hns_roce_v2_query_mpt,
6658 	.hns_roce_dev_ops = &hns_roce_v2_dev_ops,
6659 	.hns_roce_dev_srq_ops = &hns_roce_v2_dev_srq_ops,
6660 };
6661 
6662 static const struct pci_device_id hns_roce_hw_v2_pci_tbl[] = {
6663 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA), 0},
6664 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC), 0},
6665 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA), 0},
6666 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC), 0},
6667 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC), 0},
6668 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_200G_RDMA), 0},
6669 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_RDMA_DCB_PFC_VF),
6670 	 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
6671 	/* required last entry */
6672 	{0, }
6673 };
6674 
6675 MODULE_DEVICE_TABLE(pci, hns_roce_hw_v2_pci_tbl);
6676 
6677 static void hns_roce_hw_v2_get_cfg(struct hns_roce_dev *hr_dev,
6678 				  struct hnae3_handle *handle)
6679 {
6680 	struct hns_roce_v2_priv *priv = hr_dev->priv;
6681 	const struct pci_device_id *id;
6682 	int i;
6683 
6684 	hr_dev->pci_dev = handle->pdev;
6685 	id = pci_match_id(hns_roce_hw_v2_pci_tbl, hr_dev->pci_dev);
6686 	hr_dev->is_vf = id->driver_data;
6687 	hr_dev->dev = &handle->pdev->dev;
6688 	hr_dev->hw = &hns_roce_hw_v2;
6689 	hr_dev->sdb_offset = ROCEE_DB_SQ_L_0_REG;
6690 	hr_dev->odb_offset = hr_dev->sdb_offset;
6691 
6692 	/* Get info from NIC driver. */
6693 	hr_dev->reg_base = handle->rinfo.roce_io_base;
6694 	hr_dev->mem_base = handle->rinfo.roce_mem_base;
6695 	hr_dev->caps.num_ports = 1;
6696 	hr_dev->iboe.netdevs[0] = handle->rinfo.netdev;
6697 	hr_dev->iboe.phy_port[0] = 0;
6698 
6699 	addrconf_addr_eui48((u8 *)&hr_dev->ib_dev.node_guid,
6700 			    hr_dev->iboe.netdevs[0]->dev_addr);
6701 
6702 	for (i = 0; i < handle->rinfo.num_vectors; i++)
6703 		hr_dev->irq[i] = pci_irq_vector(handle->pdev,
6704 						i + handle->rinfo.base_vector);
6705 
6706 	/* cmd issue mode: 0 is poll, 1 is event */
6707 	hr_dev->cmd_mod = 1;
6708 	hr_dev->loop_idc = 0;
6709 
6710 	hr_dev->reset_cnt = handle->ae_algo->ops->ae_dev_reset_cnt(handle);
6711 	priv->handle = handle;
6712 }
6713 
6714 static int __hns_roce_hw_v2_init_instance(struct hnae3_handle *handle)
6715 {
6716 	struct hns_roce_dev *hr_dev;
6717 	int ret;
6718 
6719 	hr_dev = ib_alloc_device(hns_roce_dev, ib_dev);
6720 	if (!hr_dev)
6721 		return -ENOMEM;
6722 
6723 	hr_dev->priv = kzalloc(sizeof(struct hns_roce_v2_priv), GFP_KERNEL);
6724 	if (!hr_dev->priv) {
6725 		ret = -ENOMEM;
6726 		goto error_failed_kzalloc;
6727 	}
6728 
6729 	hns_roce_hw_v2_get_cfg(hr_dev, handle);
6730 
6731 	ret = hns_roce_init(hr_dev);
6732 	if (ret) {
6733 		dev_err(hr_dev->dev, "RoCE Engine init failed!\n");
6734 		goto error_failed_cfg;
6735 	}
6736 
6737 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
6738 		ret = free_mr_init(hr_dev);
6739 		if (ret) {
6740 			dev_err(hr_dev->dev, "failed to init free mr!\n");
6741 			goto error_failed_roce_init;
6742 		}
6743 	}
6744 
6745 	handle->priv = hr_dev;
6746 
6747 	return 0;
6748 
6749 error_failed_roce_init:
6750 	hns_roce_exit(hr_dev);
6751 
6752 error_failed_cfg:
6753 	kfree(hr_dev->priv);
6754 
6755 error_failed_kzalloc:
6756 	ib_dealloc_device(&hr_dev->ib_dev);
6757 
6758 	return ret;
6759 }
6760 
6761 static void __hns_roce_hw_v2_uninit_instance(struct hnae3_handle *handle,
6762 					   bool reset)
6763 {
6764 	struct hns_roce_dev *hr_dev = handle->priv;
6765 
6766 	if (!hr_dev)
6767 		return;
6768 
6769 	handle->priv = NULL;
6770 
6771 	hr_dev->state = HNS_ROCE_DEVICE_STATE_UNINIT;
6772 	hns_roce_handle_device_err(hr_dev);
6773 
6774 	if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
6775 		free_mr_exit(hr_dev);
6776 
6777 	hns_roce_exit(hr_dev);
6778 	kfree(hr_dev->priv);
6779 	ib_dealloc_device(&hr_dev->ib_dev);
6780 }
6781 
6782 static int hns_roce_hw_v2_init_instance(struct hnae3_handle *handle)
6783 {
6784 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
6785 	const struct pci_device_id *id;
6786 	struct device *dev = &handle->pdev->dev;
6787 	int ret;
6788 
6789 	handle->rinfo.instance_state = HNS_ROCE_STATE_INIT;
6790 
6791 	if (ops->ae_dev_resetting(handle) || ops->get_hw_reset_stat(handle)) {
6792 		handle->rinfo.instance_state = HNS_ROCE_STATE_NON_INIT;
6793 		goto reset_chk_err;
6794 	}
6795 
6796 	id = pci_match_id(hns_roce_hw_v2_pci_tbl, handle->pdev);
6797 	if (!id)
6798 		return 0;
6799 
6800 	if (id->driver_data && handle->pdev->revision == PCI_REVISION_ID_HIP08)
6801 		return 0;
6802 
6803 	ret = __hns_roce_hw_v2_init_instance(handle);
6804 	if (ret) {
6805 		handle->rinfo.instance_state = HNS_ROCE_STATE_NON_INIT;
6806 		dev_err(dev, "RoCE instance init failed! ret = %d\n", ret);
6807 		if (ops->ae_dev_resetting(handle) ||
6808 		    ops->get_hw_reset_stat(handle))
6809 			goto reset_chk_err;
6810 		else
6811 			return ret;
6812 	}
6813 
6814 	handle->rinfo.instance_state = HNS_ROCE_STATE_INITED;
6815 
6816 	return 0;
6817 
6818 reset_chk_err:
6819 	dev_err(dev, "Device is busy in resetting state.\n"
6820 		     "please retry later.\n");
6821 
6822 	return -EBUSY;
6823 }
6824 
6825 static void hns_roce_hw_v2_uninit_instance(struct hnae3_handle *handle,
6826 					   bool reset)
6827 {
6828 	if (handle->rinfo.instance_state != HNS_ROCE_STATE_INITED)
6829 		return;
6830 
6831 	handle->rinfo.instance_state = HNS_ROCE_STATE_UNINIT;
6832 
6833 	__hns_roce_hw_v2_uninit_instance(handle, reset);
6834 
6835 	handle->rinfo.instance_state = HNS_ROCE_STATE_NON_INIT;
6836 }
6837 static int hns_roce_hw_v2_reset_notify_down(struct hnae3_handle *handle)
6838 {
6839 	struct hns_roce_dev *hr_dev;
6840 
6841 	if (handle->rinfo.instance_state != HNS_ROCE_STATE_INITED) {
6842 		set_bit(HNS_ROCE_RST_DIRECT_RETURN, &handle->rinfo.state);
6843 		return 0;
6844 	}
6845 
6846 	handle->rinfo.reset_state = HNS_ROCE_STATE_RST_DOWN;
6847 	clear_bit(HNS_ROCE_RST_DIRECT_RETURN, &handle->rinfo.state);
6848 
6849 	hr_dev = handle->priv;
6850 	if (!hr_dev)
6851 		return 0;
6852 
6853 	hr_dev->active = false;
6854 	hr_dev->dis_db = true;
6855 	hr_dev->state = HNS_ROCE_DEVICE_STATE_RST_DOWN;
6856 
6857 	return 0;
6858 }
6859 
6860 static int hns_roce_hw_v2_reset_notify_init(struct hnae3_handle *handle)
6861 {
6862 	struct device *dev = &handle->pdev->dev;
6863 	int ret;
6864 
6865 	if (test_and_clear_bit(HNS_ROCE_RST_DIRECT_RETURN,
6866 			       &handle->rinfo.state)) {
6867 		handle->rinfo.reset_state = HNS_ROCE_STATE_RST_INITED;
6868 		return 0;
6869 	}
6870 
6871 	handle->rinfo.reset_state = HNS_ROCE_STATE_RST_INIT;
6872 
6873 	dev_info(&handle->pdev->dev, "In reset process RoCE client reinit.\n");
6874 	ret = __hns_roce_hw_v2_init_instance(handle);
6875 	if (ret) {
6876 		/* when reset notify type is HNAE3_INIT_CLIENT In reset notify
6877 		 * callback function, RoCE Engine reinitialize. If RoCE reinit
6878 		 * failed, we should inform NIC driver.
6879 		 */
6880 		handle->priv = NULL;
6881 		dev_err(dev, "In reset process RoCE reinit failed %d.\n", ret);
6882 	} else {
6883 		handle->rinfo.reset_state = HNS_ROCE_STATE_RST_INITED;
6884 		dev_info(dev, "reset done, RoCE client reinit finished.\n");
6885 	}
6886 
6887 	return ret;
6888 }
6889 
6890 static int hns_roce_hw_v2_reset_notify_uninit(struct hnae3_handle *handle)
6891 {
6892 	if (test_bit(HNS_ROCE_RST_DIRECT_RETURN, &handle->rinfo.state))
6893 		return 0;
6894 
6895 	handle->rinfo.reset_state = HNS_ROCE_STATE_RST_UNINIT;
6896 	dev_info(&handle->pdev->dev, "In reset process RoCE client uninit.\n");
6897 	msleep(HNS_ROCE_V2_HW_RST_UNINT_DELAY);
6898 	__hns_roce_hw_v2_uninit_instance(handle, false);
6899 
6900 	return 0;
6901 }
6902 
6903 static int hns_roce_hw_v2_reset_notify(struct hnae3_handle *handle,
6904 				       enum hnae3_reset_notify_type type)
6905 {
6906 	int ret = 0;
6907 
6908 	switch (type) {
6909 	case HNAE3_DOWN_CLIENT:
6910 		ret = hns_roce_hw_v2_reset_notify_down(handle);
6911 		break;
6912 	case HNAE3_INIT_CLIENT:
6913 		ret = hns_roce_hw_v2_reset_notify_init(handle);
6914 		break;
6915 	case HNAE3_UNINIT_CLIENT:
6916 		ret = hns_roce_hw_v2_reset_notify_uninit(handle);
6917 		break;
6918 	default:
6919 		break;
6920 	}
6921 
6922 	return ret;
6923 }
6924 
6925 static const struct hnae3_client_ops hns_roce_hw_v2_ops = {
6926 	.init_instance = hns_roce_hw_v2_init_instance,
6927 	.uninit_instance = hns_roce_hw_v2_uninit_instance,
6928 	.reset_notify = hns_roce_hw_v2_reset_notify,
6929 };
6930 
6931 static struct hnae3_client hns_roce_hw_v2_client = {
6932 	.name = "hns_roce_hw_v2",
6933 	.type = HNAE3_CLIENT_ROCE,
6934 	.ops = &hns_roce_hw_v2_ops,
6935 };
6936 
6937 static int __init hns_roce_hw_v2_init(void)
6938 {
6939 	return hnae3_register_client(&hns_roce_hw_v2_client);
6940 }
6941 
6942 static void __exit hns_roce_hw_v2_exit(void)
6943 {
6944 	hnae3_unregister_client(&hns_roce_hw_v2_client);
6945 }
6946 
6947 module_init(hns_roce_hw_v2_init);
6948 module_exit(hns_roce_hw_v2_exit);
6949 
6950 MODULE_LICENSE("Dual BSD/GPL");
6951 MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
6952 MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
6953 MODULE_AUTHOR("Shaobo Xu <xushaobo2@huawei.com>");
6954 MODULE_DESCRIPTION("Hisilicon Hip08 Family RoCE Driver");
6955