2 * Copyright (c) 2016-2017 Hisilicon Limited.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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>
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"
58 enum ecc_resource_type {
64 ECC_RESOURCE_QPC_TIMER,
65 ECC_RESOURCE_CQC_TIMER,
76 HNS_ROCE_CMD_READ_QPC_BT0, HNS_ROCE_CMD_WRITE_QPC_BT0 },
78 HNS_ROCE_CMD_READ_CQC_BT0, HNS_ROCE_CMD_WRITE_CQC_BT0 },
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 */
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 },
94 static inline void set_data_seg_v2(struct hns_roce_v2_wqe_data_seg *dseg,
97 dseg->lkey = cpu_to_le32(sg->lkey);
98 dseg->addr = cpu_to_le64(sg->addr);
99 dseg->len = cpu_to_le32(sg->length);
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.
109 #define HR_OPC_MAP(ib_key, hr_key) \
110 [IB_WR_ ## ib_key] = 1 + HNS_ROCE_V2_WQE_OP_ ## hr_key
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),
126 static u32 to_hr_opcode(u32 ib_opcode)
128 if (ib_opcode >= ARRAY_SIZE(hns_roce_op_code))
129 return HNS_ROCE_V2_WQE_OP_MASK;
131 return hns_roce_op_code[ib_opcode] ? hns_roce_op_code[ib_opcode] - 1 :
132 HNS_ROCE_V2_WQE_OP_MASK;
135 static void set_frmr_seg(struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
136 const struct ib_reg_wr *wr)
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);
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);
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));
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);
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);
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)
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);
176 set_data_seg_v2(dseg, wr->sg_list);
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);
182 aseg->fetchadd_swap_data =
183 cpu_to_le64(atomic_wr(wr)->compare_add);
187 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SGE_NUM, valid_num_sge);
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)
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;
202 if (msg_len > qp->sq.ext_sge_cnt * HNS_ROCE_SGE_SIZE) {
204 "no enough extended sge space for inline data.\n");
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);
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.
219 if (len <= left_len_in_pg) {
220 memcpy(dseg, addr, len);
222 idx += len / HNS_ROCE_SGE_SIZE;
225 if (i >= wr->num_sge)
228 left_len_in_pg -= len;
229 len = wr->sg_list[i].length;
230 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
233 memcpy(dseg, addr, left_len_in_pg);
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;
249 static void set_extend_sge(struct hns_roce_qp *qp, struct ib_sge *sge,
250 unsigned int *sge_ind, unsigned int cnt)
252 struct hns_roce_v2_wqe_data_seg *dseg;
253 unsigned int idx = *sge_ind;
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);
268 static bool check_inl_data_len(struct hns_roce_qp *qp, unsigned int len)
270 struct hns_roce_dev *hr_dev = to_hr_dev(qp->ibqp.device);
271 int mtu = ib_mtu_enum_to_int(qp->path_mtu);
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);
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)
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;
295 if (unlikely(wr->opcode == IB_WR_RDMA_READ)) {
296 ibdev_err(ibdev, "invalid inline parameters!\n");
300 if (!check_inl_data_len(qp, msg_len))
303 dseg += sizeof(struct hns_roce_v2_rc_send_wqe);
305 if (msg_len <= HNS_ROCE_V2_MAX_RC_INL_INN_SZ) {
306 hr_reg_clear(rc_sq_wqe, RC_SEND_WQE_INL_TYPE);
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;
314 hr_reg_enable(rc_sq_wqe, RC_SEND_WQE_INL_TYPE);
316 ret = fill_ext_sge_inl_data(qp, wr, &curr_idx, msg_len);
320 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SGE_NUM, curr_idx - *sge_idx);
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)
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);
339 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_MSG_START_SGE_IDX,
340 (*sge_ind) & (qp->sge.sge_cnt - 1));
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);
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);
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);
363 set_extend_sge(qp, wr->sg_list + i, sge_ind,
364 valid_num_sge - HNS_ROCE_SGE_IN_WQE);
367 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SGE_NUM, valid_num_sge);
372 static int check_send_valid(struct hns_roce_dev *hr_dev,
373 struct hns_roce_qp *hr_qp)
375 struct ib_device *ibdev = &hr_dev->ib_dev;
377 if (unlikely(hr_qp->state == IB_QPS_RESET ||
378 hr_qp->state == IB_QPS_INIT ||
379 hr_qp->state == IB_QPS_RTR)) {
380 ibdev_err(ibdev, "failed to post WQE, QP state %u!\n",
383 } else if (unlikely(hr_dev->state >= HNS_ROCE_DEVICE_STATE_RST_DOWN)) {
384 ibdev_err(ibdev, "failed to post WQE, dev state %d!\n",
392 static unsigned int calc_wr_sge_num(const struct ib_send_wr *wr,
393 unsigned int *sge_len)
395 unsigned int valid_num = 0;
396 unsigned int len = 0;
399 for (i = 0; i < wr->num_sge; i++) {
400 if (likely(wr->sg_list[i].length)) {
401 len += wr->sg_list[i].length;
410 static __le32 get_immtdata(const struct ib_send_wr *wr)
412 switch (wr->opcode) {
413 case IB_WR_SEND_WITH_IMM:
414 case IB_WR_RDMA_WRITE_WITH_IMM:
415 return cpu_to_le32(be32_to_cpu(wr->ex.imm_data));
421 static int set_ud_opcode(struct hns_roce_v2_ud_send_wqe *ud_sq_wqe,
422 const struct ib_send_wr *wr)
424 u32 ib_op = wr->opcode;
426 if (ib_op != IB_WR_SEND && ib_op != IB_WR_SEND_WITH_IMM)
429 ud_sq_wqe->immtdata = get_immtdata(wr);
431 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_OPCODE, to_hr_opcode(ib_op));
436 static int fill_ud_av(struct hns_roce_v2_ud_send_wqe *ud_sq_wqe,
437 struct hns_roce_ah *ah)
439 struct ib_device *ib_dev = ah->ibah.device;
440 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
442 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_UDPSPN, ah->av.udp_sport);
443 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_HOPLIMIT, ah->av.hop_limit);
444 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_TCLASS, ah->av.tclass);
445 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_FLOW_LABEL, ah->av.flowlabel);
447 if (WARN_ON(ah->av.sl > MAX_SERVICE_LEVEL))
450 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_SL, ah->av.sl);
452 ud_sq_wqe->sgid_index = ah->av.gid_index;
454 memcpy(ud_sq_wqe->dmac, ah->av.mac, ETH_ALEN);
455 memcpy(ud_sq_wqe->dgid, ah->av.dgid, GID_LEN_V2);
457 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
460 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_VLAN_EN, ah->av.vlan_en);
461 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_VLAN, ah->av.vlan_id);
466 static inline int set_ud_wqe(struct hns_roce_qp *qp,
467 const struct ib_send_wr *wr,
468 void *wqe, unsigned int *sge_idx,
469 unsigned int owner_bit)
471 struct hns_roce_ah *ah = to_hr_ah(ud_wr(wr)->ah);
472 struct hns_roce_v2_ud_send_wqe *ud_sq_wqe = wqe;
473 unsigned int curr_idx = *sge_idx;
474 unsigned int valid_num_sge;
478 valid_num_sge = calc_wr_sge_num(wr, &msg_len);
480 ret = set_ud_opcode(ud_sq_wqe, wr);
484 ud_sq_wqe->msg_len = cpu_to_le32(msg_len);
486 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_CQE,
487 !!(wr->send_flags & IB_SEND_SIGNALED));
488 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_SE,
489 !!(wr->send_flags & IB_SEND_SOLICITED));
491 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_PD, to_hr_pd(qp->ibqp.pd)->pdn);
492 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_SGE_NUM, valid_num_sge);
493 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_MSG_START_SGE_IDX,
494 curr_idx & (qp->sge.sge_cnt - 1));
496 ud_sq_wqe->qkey = cpu_to_le32(ud_wr(wr)->remote_qkey & 0x80000000 ?
497 qp->qkey : ud_wr(wr)->remote_qkey);
498 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_DQPN, ud_wr(wr)->remote_qpn);
500 ret = fill_ud_av(ud_sq_wqe, ah);
504 qp->sl = to_hr_ah(ud_wr(wr)->ah)->av.sl;
506 set_extend_sge(qp, wr->sg_list, &curr_idx, valid_num_sge);
509 * The pipeline can sequentially post all valid WQEs into WQ buffer,
510 * including new WQEs waiting for the doorbell to update the PI again.
511 * Therefore, the owner bit of WQE MUST be updated after all fields
512 * and extSGEs have been written into DDR instead of cache.
514 if (qp->en_flags & HNS_ROCE_QP_CAP_OWNER_DB)
518 hr_reg_write(ud_sq_wqe, UD_SEND_WQE_OWNER, owner_bit);
523 static int set_rc_opcode(struct hns_roce_dev *hr_dev,
524 struct hns_roce_v2_rc_send_wqe *rc_sq_wqe,
525 const struct ib_send_wr *wr)
527 u32 ib_op = wr->opcode;
530 rc_sq_wqe->immtdata = get_immtdata(wr);
533 case IB_WR_RDMA_READ:
534 case IB_WR_RDMA_WRITE:
535 case IB_WR_RDMA_WRITE_WITH_IMM:
536 rc_sq_wqe->rkey = cpu_to_le32(rdma_wr(wr)->rkey);
537 rc_sq_wqe->va = cpu_to_le64(rdma_wr(wr)->remote_addr);
540 case IB_WR_SEND_WITH_IMM:
542 case IB_WR_ATOMIC_CMP_AND_SWP:
543 case IB_WR_ATOMIC_FETCH_AND_ADD:
544 rc_sq_wqe->rkey = cpu_to_le32(atomic_wr(wr)->rkey);
545 rc_sq_wqe->va = cpu_to_le64(atomic_wr(wr)->remote_addr);
548 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
549 set_frmr_seg(rc_sq_wqe, reg_wr(wr));
553 case IB_WR_SEND_WITH_INV:
554 rc_sq_wqe->inv_key = cpu_to_le32(wr->ex.invalidate_rkey);
563 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_OPCODE, to_hr_opcode(ib_op));
568 static inline int set_rc_wqe(struct hns_roce_qp *qp,
569 const struct ib_send_wr *wr,
570 void *wqe, unsigned int *sge_idx,
571 unsigned int owner_bit)
573 struct hns_roce_dev *hr_dev = to_hr_dev(qp->ibqp.device);
574 struct hns_roce_v2_rc_send_wqe *rc_sq_wqe = wqe;
575 unsigned int curr_idx = *sge_idx;
576 unsigned int valid_num_sge;
580 valid_num_sge = calc_wr_sge_num(wr, &msg_len);
582 rc_sq_wqe->msg_len = cpu_to_le32(msg_len);
584 ret = set_rc_opcode(hr_dev, rc_sq_wqe, wr);
588 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_FENCE,
589 (wr->send_flags & IB_SEND_FENCE) ? 1 : 0);
591 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_SE,
592 (wr->send_flags & IB_SEND_SOLICITED) ? 1 : 0);
594 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_CQE,
595 (wr->send_flags & IB_SEND_SIGNALED) ? 1 : 0);
597 if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
598 wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
599 set_atomic_seg(wr, rc_sq_wqe, valid_num_sge);
600 else if (wr->opcode != IB_WR_REG_MR)
601 ret = set_rwqe_data_seg(&qp->ibqp, wr, rc_sq_wqe,
602 &curr_idx, valid_num_sge);
605 * The pipeline can sequentially post all valid WQEs into WQ buffer,
606 * including new WQEs waiting for the doorbell to update the PI again.
607 * Therefore, the owner bit of WQE MUST be updated after all fields
608 * and extSGEs have been written into DDR instead of cache.
610 if (qp->en_flags & HNS_ROCE_QP_CAP_OWNER_DB)
614 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_OWNER, owner_bit);
619 static inline void update_sq_db(struct hns_roce_dev *hr_dev,
620 struct hns_roce_qp *qp)
622 if (unlikely(qp->state == IB_QPS_ERR)) {
623 flush_cqe(hr_dev, qp);
625 struct hns_roce_v2_db sq_db = {};
627 hr_reg_write(&sq_db, DB_TAG, qp->qpn);
628 hr_reg_write(&sq_db, DB_CMD, HNS_ROCE_V2_SQ_DB);
629 hr_reg_write(&sq_db, DB_PI, qp->sq.head);
630 hr_reg_write(&sq_db, DB_SL, qp->sl);
632 hns_roce_write64(hr_dev, (__le32 *)&sq_db, qp->sq.db_reg);
636 static inline void update_rq_db(struct hns_roce_dev *hr_dev,
637 struct hns_roce_qp *qp)
639 if (unlikely(qp->state == IB_QPS_ERR)) {
640 flush_cqe(hr_dev, qp);
642 if (likely(qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)) {
644 qp->rq.head & V2_DB_PRODUCER_IDX_M;
646 struct hns_roce_v2_db rq_db = {};
648 hr_reg_write(&rq_db, DB_TAG, qp->qpn);
649 hr_reg_write(&rq_db, DB_CMD, HNS_ROCE_V2_RQ_DB);
650 hr_reg_write(&rq_db, DB_PI, qp->rq.head);
652 hns_roce_write64(hr_dev, (__le32 *)&rq_db,
658 static void hns_roce_write512(struct hns_roce_dev *hr_dev, u64 *val,
661 #define HNS_ROCE_WRITE_TIMES 8
662 struct hns_roce_v2_priv *priv = (struct hns_roce_v2_priv *)hr_dev->priv;
663 struct hnae3_handle *handle = priv->handle;
664 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
667 if (!hr_dev->dis_db && !ops->get_hw_reset_stat(handle))
668 for (i = 0; i < HNS_ROCE_WRITE_TIMES; i++)
669 writeq_relaxed(*(val + i), dest + i);
672 static void write_dwqe(struct hns_roce_dev *hr_dev, struct hns_roce_qp *qp,
675 #define HNS_ROCE_SL_SHIFT 2
676 struct hns_roce_v2_rc_send_wqe *rc_sq_wqe = wqe;
678 /* All kinds of DirectWQE have the same header field layout */
679 hr_reg_enable(rc_sq_wqe, RC_SEND_WQE_FLAG);
680 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_DB_SL_L, qp->sl);
681 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_DB_SL_H,
682 qp->sl >> HNS_ROCE_SL_SHIFT);
683 hr_reg_write(rc_sq_wqe, RC_SEND_WQE_WQE_INDEX, qp->sq.head);
685 hns_roce_write512(hr_dev, wqe, qp->sq.db_reg);
688 static int hns_roce_v2_post_send(struct ib_qp *ibqp,
689 const struct ib_send_wr *wr,
690 const struct ib_send_wr **bad_wr)
692 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
693 struct ib_device *ibdev = &hr_dev->ib_dev;
694 struct hns_roce_qp *qp = to_hr_qp(ibqp);
695 unsigned long flags = 0;
696 unsigned int owner_bit;
697 unsigned int sge_idx;
698 unsigned int wqe_idx;
703 spin_lock_irqsave(&qp->sq.lock, flags);
705 ret = check_send_valid(hr_dev, qp);
712 sge_idx = qp->next_sge;
714 for (nreq = 0; wr; ++nreq, wr = wr->next) {
715 if (hns_roce_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
721 wqe_idx = (qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1);
723 if (unlikely(wr->num_sge > qp->sq.max_gs)) {
724 ibdev_err(ibdev, "num_sge = %d > qp->sq.max_gs = %u.\n",
725 wr->num_sge, qp->sq.max_gs);
731 wqe = hns_roce_get_send_wqe(qp, wqe_idx);
732 qp->sq.wrid[wqe_idx] = wr->wr_id;
734 ~(((qp->sq.head + nreq) >> ilog2(qp->sq.wqe_cnt)) & 0x1);
736 /* Corresponding to the QP type, wqe process separately */
737 if (ibqp->qp_type == IB_QPT_RC)
738 ret = set_rc_wqe(qp, wr, wqe, &sge_idx, owner_bit);
740 ret = set_ud_wqe(qp, wr, wqe, &sge_idx, owner_bit);
751 qp->next_sge = sge_idx;
753 if (nreq == 1 && !ret &&
754 (qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE))
755 write_dwqe(hr_dev, qp, wqe);
757 update_sq_db(hr_dev, qp);
760 spin_unlock_irqrestore(&qp->sq.lock, flags);
765 static int check_recv_valid(struct hns_roce_dev *hr_dev,
766 struct hns_roce_qp *hr_qp)
768 if (unlikely(hr_dev->state >= HNS_ROCE_DEVICE_STATE_RST_DOWN))
771 if (hr_qp->state == IB_QPS_RESET)
777 static void fill_recv_sge_to_wqe(const struct ib_recv_wr *wr, void *wqe,
778 u32 max_sge, bool rsv)
780 struct hns_roce_v2_wqe_data_seg *dseg = wqe;
783 for (i = 0, cnt = 0; i < wr->num_sge; i++) {
784 /* Skip zero-length sge */
785 if (!wr->sg_list[i].length)
787 set_data_seg_v2(dseg + cnt, wr->sg_list + i);
791 /* Fill a reserved sge to make hw stop reading remaining segments */
793 dseg[cnt].lkey = cpu_to_le32(HNS_ROCE_INVALID_LKEY);
795 dseg[cnt].len = cpu_to_le32(HNS_ROCE_INVALID_SGE_LENGTH);
797 /* Clear remaining segments to make ROCEE ignore sges */
799 memset(dseg + cnt, 0,
800 (max_sge - cnt) * HNS_ROCE_SGE_SIZE);
804 static void fill_rq_wqe(struct hns_roce_qp *hr_qp, const struct ib_recv_wr *wr,
805 u32 wqe_idx, u32 max_sge)
809 wqe = hns_roce_get_recv_wqe(hr_qp, wqe_idx);
810 fill_recv_sge_to_wqe(wr, wqe, max_sge, hr_qp->rq.rsv_sge);
813 static int hns_roce_v2_post_recv(struct ib_qp *ibqp,
814 const struct ib_recv_wr *wr,
815 const struct ib_recv_wr **bad_wr)
817 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
818 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
819 struct ib_device *ibdev = &hr_dev->ib_dev;
820 u32 wqe_idx, nreq, max_sge;
824 spin_lock_irqsave(&hr_qp->rq.lock, flags);
826 ret = check_recv_valid(hr_dev, hr_qp);
833 max_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
834 for (nreq = 0; wr; ++nreq, wr = wr->next) {
835 if (unlikely(hns_roce_wq_overflow(&hr_qp->rq, nreq,
836 hr_qp->ibqp.recv_cq))) {
842 if (unlikely(wr->num_sge > max_sge)) {
843 ibdev_err(ibdev, "num_sge = %d >= max_sge = %u.\n",
844 wr->num_sge, max_sge);
850 wqe_idx = (hr_qp->rq.head + nreq) & (hr_qp->rq.wqe_cnt - 1);
851 fill_rq_wqe(hr_qp, wr, wqe_idx, max_sge);
852 hr_qp->rq.wrid[wqe_idx] = wr->wr_id;
857 hr_qp->rq.head += nreq;
859 update_rq_db(hr_dev, hr_qp);
861 spin_unlock_irqrestore(&hr_qp->rq.lock, flags);
866 static void *get_srq_wqe_buf(struct hns_roce_srq *srq, u32 n)
868 return hns_roce_buf_offset(srq->buf_mtr.kmem, n << srq->wqe_shift);
871 static void *get_idx_buf(struct hns_roce_idx_que *idx_que, u32 n)
873 return hns_roce_buf_offset(idx_que->mtr.kmem,
874 n << idx_que->entry_shift);
877 static void hns_roce_free_srq_wqe(struct hns_roce_srq *srq, u32 wqe_index)
879 /* always called with interrupts disabled. */
880 spin_lock(&srq->lock);
882 bitmap_clear(srq->idx_que.bitmap, wqe_index, 1);
885 spin_unlock(&srq->lock);
888 static int hns_roce_srqwq_overflow(struct hns_roce_srq *srq)
890 struct hns_roce_idx_que *idx_que = &srq->idx_que;
892 return idx_que->head - idx_que->tail >= srq->wqe_cnt;
895 static int check_post_srq_valid(struct hns_roce_srq *srq, u32 max_sge,
896 const struct ib_recv_wr *wr)
898 struct ib_device *ib_dev = srq->ibsrq.device;
900 if (unlikely(wr->num_sge > max_sge)) {
902 "failed to check sge, wr->num_sge = %d, max_sge = %u.\n",
903 wr->num_sge, max_sge);
907 if (unlikely(hns_roce_srqwq_overflow(srq))) {
909 "failed to check srqwq status, srqwq is full.\n");
916 static int get_srq_wqe_idx(struct hns_roce_srq *srq, u32 *wqe_idx)
918 struct hns_roce_idx_que *idx_que = &srq->idx_que;
921 pos = find_first_zero_bit(idx_que->bitmap, srq->wqe_cnt);
922 if (unlikely(pos == srq->wqe_cnt))
925 bitmap_set(idx_que->bitmap, pos, 1);
930 static void fill_wqe_idx(struct hns_roce_srq *srq, unsigned int wqe_idx)
932 struct hns_roce_idx_que *idx_que = &srq->idx_que;
936 head = idx_que->head & (srq->wqe_cnt - 1);
938 buf = get_idx_buf(idx_que, head);
939 *buf = cpu_to_le32(wqe_idx);
944 static void update_srq_db(struct hns_roce_v2_db *db, struct hns_roce_srq *srq)
946 hr_reg_write(db, DB_TAG, srq->srqn);
947 hr_reg_write(db, DB_CMD, HNS_ROCE_V2_SRQ_DB);
948 hr_reg_write(db, DB_PI, srq->idx_que.head);
951 static int hns_roce_v2_post_srq_recv(struct ib_srq *ibsrq,
952 const struct ib_recv_wr *wr,
953 const struct ib_recv_wr **bad_wr)
955 struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device);
956 struct hns_roce_srq *srq = to_hr_srq(ibsrq);
957 struct hns_roce_v2_db srq_db;
965 spin_lock_irqsave(&srq->lock, flags);
967 max_sge = srq->max_gs - srq->rsv_sge;
968 for (nreq = 0; wr; ++nreq, wr = wr->next) {
969 ret = check_post_srq_valid(srq, max_sge, wr);
975 ret = get_srq_wqe_idx(srq, &wqe_idx);
981 wqe = get_srq_wqe_buf(srq, wqe_idx);
982 fill_recv_sge_to_wqe(wr, wqe, max_sge, srq->rsv_sge);
983 fill_wqe_idx(srq, wqe_idx);
984 srq->wrid[wqe_idx] = wr->wr_id;
988 update_srq_db(&srq_db, srq);
990 hns_roce_write64(hr_dev, (__le32 *)&srq_db, srq->db_reg);
993 spin_unlock_irqrestore(&srq->lock, flags);
998 static u32 hns_roce_v2_cmd_hw_reseted(struct hns_roce_dev *hr_dev,
999 unsigned long instance_stage,
1000 unsigned long reset_stage)
1002 /* When hardware reset has been completed once or more, we should stop
1003 * sending mailbox&cmq&doorbell to hardware. If now in .init_instance()
1004 * function, we should exit with error. If now at HNAE3_INIT_CLIENT
1005 * stage of soft reset process, we should exit with error, and then
1006 * HNAE3_INIT_CLIENT related process can rollback the operation like
1007 * notifing hardware to free resources, HNAE3_INIT_CLIENT related
1008 * process will exit with error to notify NIC driver to reschedule soft
1009 * reset process once again.
1011 hr_dev->is_reset = true;
1012 hr_dev->dis_db = true;
1014 if (reset_stage == HNS_ROCE_STATE_RST_INIT ||
1015 instance_stage == HNS_ROCE_STATE_INIT)
1016 return CMD_RST_PRC_EBUSY;
1018 return CMD_RST_PRC_SUCCESS;
1021 static u32 hns_roce_v2_cmd_hw_resetting(struct hns_roce_dev *hr_dev,
1022 unsigned long instance_stage,
1023 unsigned long reset_stage)
1025 #define HW_RESET_TIMEOUT_US 1000000
1026 #define HW_RESET_SLEEP_US 1000
1028 struct hns_roce_v2_priv *priv = hr_dev->priv;
1029 struct hnae3_handle *handle = priv->handle;
1030 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1034 /* When hardware reset is detected, we should stop sending mailbox&cmq&
1035 * doorbell to hardware. If now in .init_instance() function, we should
1036 * exit with error. If now at HNAE3_INIT_CLIENT stage of soft reset
1037 * process, we should exit with error, and then HNAE3_INIT_CLIENT
1038 * related process can rollback the operation like notifing hardware to
1039 * free resources, HNAE3_INIT_CLIENT related process will exit with
1040 * error to notify NIC driver to reschedule soft reset process once
1043 hr_dev->dis_db = true;
1045 ret = read_poll_timeout(ops->ae_dev_reset_cnt, val,
1046 val > hr_dev->reset_cnt, HW_RESET_SLEEP_US,
1047 HW_RESET_TIMEOUT_US, false, handle);
1049 hr_dev->is_reset = true;
1051 if (!hr_dev->is_reset || reset_stage == HNS_ROCE_STATE_RST_INIT ||
1052 instance_stage == HNS_ROCE_STATE_INIT)
1053 return CMD_RST_PRC_EBUSY;
1055 return CMD_RST_PRC_SUCCESS;
1058 static u32 hns_roce_v2_cmd_sw_resetting(struct hns_roce_dev *hr_dev)
1060 struct hns_roce_v2_priv *priv = hr_dev->priv;
1061 struct hnae3_handle *handle = priv->handle;
1062 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1064 /* When software reset is detected at .init_instance() function, we
1065 * should stop sending mailbox&cmq&doorbell to hardware, and exit
1068 hr_dev->dis_db = true;
1069 if (ops->ae_dev_reset_cnt(handle) != hr_dev->reset_cnt)
1070 hr_dev->is_reset = true;
1072 return CMD_RST_PRC_EBUSY;
1075 static u32 check_aedev_reset_status(struct hns_roce_dev *hr_dev,
1076 struct hnae3_handle *handle)
1078 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1079 unsigned long instance_stage; /* the current instance stage */
1080 unsigned long reset_stage; /* the current reset stage */
1081 unsigned long reset_cnt;
1085 /* Get information about reset from NIC driver or RoCE driver itself,
1086 * the meaning of the following variables from NIC driver are described
1088 * reset_cnt -- The count value of completed hardware reset.
1089 * hw_resetting -- Whether hardware device is resetting now.
1090 * sw_resetting -- Whether NIC's software reset process is running now.
1092 instance_stage = handle->rinfo.instance_state;
1093 reset_stage = handle->rinfo.reset_state;
1094 reset_cnt = ops->ae_dev_reset_cnt(handle);
1095 if (reset_cnt != hr_dev->reset_cnt)
1096 return hns_roce_v2_cmd_hw_reseted(hr_dev, instance_stage,
1099 hw_resetting = ops->get_cmdq_stat(handle);
1101 return hns_roce_v2_cmd_hw_resetting(hr_dev, instance_stage,
1104 sw_resetting = ops->ae_dev_resetting(handle);
1105 if (sw_resetting && instance_stage == HNS_ROCE_STATE_INIT)
1106 return hns_roce_v2_cmd_sw_resetting(hr_dev);
1108 return CMD_RST_PRC_OTHERS;
1111 static bool check_device_is_in_reset(struct hns_roce_dev *hr_dev)
1113 struct hns_roce_v2_priv *priv = hr_dev->priv;
1114 struct hnae3_handle *handle = priv->handle;
1115 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1117 if (hr_dev->reset_cnt != ops->ae_dev_reset_cnt(handle))
1120 if (ops->get_hw_reset_stat(handle))
1123 if (ops->ae_dev_resetting(handle))
1129 static bool v2_chk_mbox_is_avail(struct hns_roce_dev *hr_dev, bool *busy)
1131 struct hns_roce_v2_priv *priv = hr_dev->priv;
1134 if (hr_dev->is_reset)
1135 status = CMD_RST_PRC_SUCCESS;
1137 status = check_aedev_reset_status(hr_dev, priv->handle);
1139 *busy = (status == CMD_RST_PRC_EBUSY);
1141 return status == CMD_RST_PRC_OTHERS;
1144 static int hns_roce_alloc_cmq_desc(struct hns_roce_dev *hr_dev,
1145 struct hns_roce_v2_cmq_ring *ring)
1147 int size = ring->desc_num * sizeof(struct hns_roce_cmq_desc);
1149 ring->desc = dma_alloc_coherent(hr_dev->dev, size,
1150 &ring->desc_dma_addr, GFP_KERNEL);
1157 static void hns_roce_free_cmq_desc(struct hns_roce_dev *hr_dev,
1158 struct hns_roce_v2_cmq_ring *ring)
1160 dma_free_coherent(hr_dev->dev,
1161 ring->desc_num * sizeof(struct hns_roce_cmq_desc),
1162 ring->desc, ring->desc_dma_addr);
1164 ring->desc_dma_addr = 0;
1167 static int init_csq(struct hns_roce_dev *hr_dev,
1168 struct hns_roce_v2_cmq_ring *csq)
1173 csq->desc_num = CMD_CSQ_DESC_NUM;
1174 spin_lock_init(&csq->lock);
1175 csq->flag = TYPE_CSQ;
1178 ret = hns_roce_alloc_cmq_desc(hr_dev, csq);
1182 dma = csq->desc_dma_addr;
1183 roce_write(hr_dev, ROCEE_TX_CMQ_BASEADDR_L_REG, lower_32_bits(dma));
1184 roce_write(hr_dev, ROCEE_TX_CMQ_BASEADDR_H_REG, upper_32_bits(dma));
1185 roce_write(hr_dev, ROCEE_TX_CMQ_DEPTH_REG,
1186 (u32)csq->desc_num >> HNS_ROCE_CMQ_DESC_NUM_S);
1188 /* Make sure to write CI first and then PI */
1189 roce_write(hr_dev, ROCEE_TX_CMQ_CI_REG, 0);
1190 roce_write(hr_dev, ROCEE_TX_CMQ_PI_REG, 0);
1195 static int hns_roce_v2_cmq_init(struct hns_roce_dev *hr_dev)
1197 struct hns_roce_v2_priv *priv = hr_dev->priv;
1200 priv->cmq.tx_timeout = HNS_ROCE_CMQ_TX_TIMEOUT;
1202 ret = init_csq(hr_dev, &priv->cmq.csq);
1204 dev_err(hr_dev->dev, "failed to init CSQ, ret = %d.\n", ret);
1209 static void hns_roce_v2_cmq_exit(struct hns_roce_dev *hr_dev)
1211 struct hns_roce_v2_priv *priv = hr_dev->priv;
1213 hns_roce_free_cmq_desc(hr_dev, &priv->cmq.csq);
1216 static void hns_roce_cmq_setup_basic_desc(struct hns_roce_cmq_desc *desc,
1217 enum hns_roce_opcode_type opcode,
1220 memset((void *)desc, 0, sizeof(struct hns_roce_cmq_desc));
1221 desc->opcode = cpu_to_le16(opcode);
1222 desc->flag = cpu_to_le16(HNS_ROCE_CMD_FLAG_IN);
1224 desc->flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_WR);
1226 desc->flag &= cpu_to_le16(~HNS_ROCE_CMD_FLAG_WR);
1229 static int hns_roce_cmq_csq_done(struct hns_roce_dev *hr_dev)
1231 u32 tail = roce_read(hr_dev, ROCEE_TX_CMQ_CI_REG);
1232 struct hns_roce_v2_priv *priv = hr_dev->priv;
1234 return tail == priv->cmq.csq.head;
1237 static void update_cmdq_status(struct hns_roce_dev *hr_dev)
1239 struct hns_roce_v2_priv *priv = hr_dev->priv;
1240 struct hnae3_handle *handle = priv->handle;
1242 if (handle->rinfo.reset_state == HNS_ROCE_STATE_RST_INIT ||
1243 handle->rinfo.instance_state == HNS_ROCE_STATE_INIT)
1244 hr_dev->cmd.state = HNS_ROCE_CMDQ_STATE_FATAL_ERR;
1247 static int hns_roce_cmd_err_convert_errno(u16 desc_ret)
1249 struct hns_roce_cmd_errcode errcode_table[] = {
1250 {CMD_EXEC_SUCCESS, 0},
1251 {CMD_NO_AUTH, -EPERM},
1252 {CMD_NOT_EXIST, -EOPNOTSUPP},
1253 {CMD_CRQ_FULL, -EXFULL},
1254 {CMD_NEXT_ERR, -ENOSR},
1255 {CMD_NOT_EXEC, -ENOTBLK},
1256 {CMD_PARA_ERR, -EINVAL},
1257 {CMD_RESULT_ERR, -ERANGE},
1258 {CMD_TIMEOUT, -ETIME},
1259 {CMD_HILINK_ERR, -ENOLINK},
1260 {CMD_INFO_ILLEGAL, -ENXIO},
1261 {CMD_INVALID, -EBADR},
1265 for (i = 0; i < ARRAY_SIZE(errcode_table); i++)
1266 if (desc_ret == errcode_table[i].return_status)
1267 return errcode_table[i].errno;
1271 static int __hns_roce_cmq_send(struct hns_roce_dev *hr_dev,
1272 struct hns_roce_cmq_desc *desc, int num)
1274 struct hns_roce_v2_priv *priv = hr_dev->priv;
1275 struct hns_roce_v2_cmq_ring *csq = &priv->cmq.csq;
1282 spin_lock_bh(&csq->lock);
1286 for (i = 0; i < num; i++) {
1287 csq->desc[csq->head++] = desc[i];
1288 if (csq->head == csq->desc_num)
1292 /* Write to hardware */
1293 roce_write(hr_dev, ROCEE_TX_CMQ_PI_REG, csq->head);
1296 if (hns_roce_cmq_csq_done(hr_dev))
1299 } while (++timeout < priv->cmq.tx_timeout);
1301 if (hns_roce_cmq_csq_done(hr_dev)) {
1303 for (i = 0; i < num; i++) {
1304 /* check the result of hardware write back */
1305 desc[i] = csq->desc[tail++];
1306 if (tail == csq->desc_num)
1309 desc_ret = le16_to_cpu(desc[i].retval);
1310 if (likely(desc_ret == CMD_EXEC_SUCCESS))
1313 dev_err_ratelimited(hr_dev->dev,
1314 "Cmdq IO error, opcode = 0x%x, return = 0x%x.\n",
1315 desc->opcode, desc_ret);
1316 ret = hns_roce_cmd_err_convert_errno(desc_ret);
1319 /* FW/HW reset or incorrect number of desc */
1320 tail = roce_read(hr_dev, ROCEE_TX_CMQ_CI_REG);
1321 dev_warn(hr_dev->dev, "CMDQ move tail from %u to %u.\n",
1325 update_cmdq_status(hr_dev);
1330 spin_unlock_bh(&csq->lock);
1335 static int hns_roce_cmq_send(struct hns_roce_dev *hr_dev,
1336 struct hns_roce_cmq_desc *desc, int num)
1341 if (hr_dev->cmd.state == HNS_ROCE_CMDQ_STATE_FATAL_ERR)
1344 if (!v2_chk_mbox_is_avail(hr_dev, &busy))
1345 return busy ? -EBUSY : 0;
1347 ret = __hns_roce_cmq_send(hr_dev, desc, num);
1349 if (!v2_chk_mbox_is_avail(hr_dev, &busy))
1350 return busy ? -EBUSY : 0;
1356 static int config_hem_ba_to_hw(struct hns_roce_dev *hr_dev,
1357 dma_addr_t base_addr, u8 cmd, unsigned long tag)
1359 struct hns_roce_cmd_mailbox *mbox;
1362 mbox = hns_roce_alloc_cmd_mailbox(hr_dev);
1364 return PTR_ERR(mbox);
1366 ret = hns_roce_cmd_mbox(hr_dev, base_addr, mbox->dma, cmd, tag);
1367 hns_roce_free_cmd_mailbox(hr_dev, mbox);
1371 static int hns_roce_cmq_query_hw_info(struct hns_roce_dev *hr_dev)
1373 struct hns_roce_query_version *resp;
1374 struct hns_roce_cmq_desc desc;
1377 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_HW_VER, true);
1378 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1382 resp = (struct hns_roce_query_version *)desc.data;
1383 hr_dev->hw_rev = le16_to_cpu(resp->rocee_hw_version);
1384 hr_dev->vendor_id = hr_dev->pci_dev->vendor;
1389 static void func_clr_hw_resetting_state(struct hns_roce_dev *hr_dev,
1390 struct hnae3_handle *handle)
1392 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1395 hr_dev->dis_db = true;
1397 dev_warn(hr_dev->dev,
1398 "func clear is pending, device in resetting state.\n");
1399 end = HNS_ROCE_V2_HW_RST_TIMEOUT;
1401 if (!ops->get_hw_reset_stat(handle)) {
1402 hr_dev->is_reset = true;
1403 dev_info(hr_dev->dev,
1404 "func clear success after reset.\n");
1407 msleep(HNS_ROCE_V2_HW_RST_COMPLETION_WAIT);
1408 end -= HNS_ROCE_V2_HW_RST_COMPLETION_WAIT;
1411 dev_warn(hr_dev->dev, "func clear failed.\n");
1414 static void func_clr_sw_resetting_state(struct hns_roce_dev *hr_dev,
1415 struct hnae3_handle *handle)
1417 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1420 hr_dev->dis_db = true;
1422 dev_warn(hr_dev->dev,
1423 "func clear is pending, device in resetting state.\n");
1424 end = HNS_ROCE_V2_HW_RST_TIMEOUT;
1426 if (ops->ae_dev_reset_cnt(handle) !=
1427 hr_dev->reset_cnt) {
1428 hr_dev->is_reset = true;
1429 dev_info(hr_dev->dev,
1430 "func clear success after sw reset\n");
1433 msleep(HNS_ROCE_V2_HW_RST_COMPLETION_WAIT);
1434 end -= HNS_ROCE_V2_HW_RST_COMPLETION_WAIT;
1437 dev_warn(hr_dev->dev, "func clear failed because of unfinished sw reset\n");
1440 static void hns_roce_func_clr_rst_proc(struct hns_roce_dev *hr_dev, int retval,
1443 struct hns_roce_v2_priv *priv = hr_dev->priv;
1444 struct hnae3_handle *handle = priv->handle;
1445 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1447 if (ops->ae_dev_reset_cnt(handle) != hr_dev->reset_cnt) {
1448 hr_dev->dis_db = true;
1449 hr_dev->is_reset = true;
1450 dev_info(hr_dev->dev, "func clear success after reset.\n");
1454 if (ops->get_hw_reset_stat(handle)) {
1455 func_clr_hw_resetting_state(hr_dev, handle);
1459 if (ops->ae_dev_resetting(handle) &&
1460 handle->rinfo.instance_state == HNS_ROCE_STATE_INIT) {
1461 func_clr_sw_resetting_state(hr_dev, handle);
1465 if (retval && !flag)
1466 dev_warn(hr_dev->dev,
1467 "func clear read failed, ret = %d.\n", retval);
1469 dev_warn(hr_dev->dev, "func clear failed.\n");
1472 static void __hns_roce_function_clear(struct hns_roce_dev *hr_dev, int vf_id)
1474 bool fclr_write_fail_flag = false;
1475 struct hns_roce_func_clear *resp;
1476 struct hns_roce_cmq_desc desc;
1480 if (check_device_is_in_reset(hr_dev))
1483 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_FUNC_CLEAR, false);
1484 resp = (struct hns_roce_func_clear *)desc.data;
1485 resp->rst_funcid_en = cpu_to_le32(vf_id);
1487 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1489 fclr_write_fail_flag = true;
1490 dev_err(hr_dev->dev, "func clear write failed, ret = %d.\n",
1495 msleep(HNS_ROCE_V2_READ_FUNC_CLEAR_FLAG_INTERVAL);
1496 end = HNS_ROCE_V2_FUNC_CLEAR_TIMEOUT_MSECS;
1498 if (check_device_is_in_reset(hr_dev))
1500 msleep(HNS_ROCE_V2_READ_FUNC_CLEAR_FLAG_FAIL_WAIT);
1501 end -= HNS_ROCE_V2_READ_FUNC_CLEAR_FLAG_FAIL_WAIT;
1503 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_FUNC_CLEAR,
1506 resp->rst_funcid_en = cpu_to_le32(vf_id);
1507 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1511 if (hr_reg_read(resp, FUNC_CLEAR_RST_FUN_DONE)) {
1513 hr_dev->is_reset = true;
1519 hns_roce_func_clr_rst_proc(hr_dev, ret, fclr_write_fail_flag);
1522 static int hns_roce_free_vf_resource(struct hns_roce_dev *hr_dev, int vf_id)
1524 enum hns_roce_opcode_type opcode = HNS_ROCE_OPC_ALLOC_VF_RES;
1525 struct hns_roce_cmq_desc desc[2];
1526 struct hns_roce_cmq_req *req_a;
1528 req_a = (struct hns_roce_cmq_req *)desc[0].data;
1529 hns_roce_cmq_setup_basic_desc(&desc[0], opcode, false);
1530 desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1531 hns_roce_cmq_setup_basic_desc(&desc[1], opcode, false);
1532 hr_reg_write(req_a, FUNC_RES_A_VF_ID, vf_id);
1534 return hns_roce_cmq_send(hr_dev, desc, 2);
1537 static void hns_roce_function_clear(struct hns_roce_dev *hr_dev)
1542 if (hr_dev->cmd.state == HNS_ROCE_CMDQ_STATE_FATAL_ERR)
1545 for (i = hr_dev->func_num - 1; i >= 0; i--) {
1546 __hns_roce_function_clear(hr_dev, i);
1551 ret = hns_roce_free_vf_resource(hr_dev, i);
1553 ibdev_err(&hr_dev->ib_dev,
1554 "failed to free vf resource, vf_id = %d, ret = %d.\n",
1559 static int hns_roce_clear_extdb_list_info(struct hns_roce_dev *hr_dev)
1561 struct hns_roce_cmq_desc desc;
1564 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CLEAR_EXTDB_LIST_INFO,
1566 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1568 ibdev_err(&hr_dev->ib_dev,
1569 "failed to clear extended doorbell info, ret = %d.\n",
1575 static int hns_roce_query_fw_ver(struct hns_roce_dev *hr_dev)
1577 struct hns_roce_query_fw_info *resp;
1578 struct hns_roce_cmq_desc desc;
1581 hns_roce_cmq_setup_basic_desc(&desc, HNS_QUERY_FW_VER, true);
1582 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1586 resp = (struct hns_roce_query_fw_info *)desc.data;
1587 hr_dev->caps.fw_ver = (u64)(le32_to_cpu(resp->fw_ver));
1592 static int hns_roce_query_func_info(struct hns_roce_dev *hr_dev)
1594 struct hns_roce_cmq_desc desc;
1597 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
1598 hr_dev->func_num = 1;
1602 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_FUNC_INFO,
1604 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1606 hr_dev->func_num = 1;
1610 hr_dev->func_num = le32_to_cpu(desc.func_info.own_func_num);
1611 hr_dev->cong_algo_tmpl_id = le32_to_cpu(desc.func_info.own_mac_id);
1616 static int hns_roce_hw_v2_query_counter(struct hns_roce_dev *hr_dev,
1617 u64 *stats, u32 port, int *num_counters)
1619 #define CNT_PER_DESC 3
1620 struct hns_roce_cmq_desc *desc;
1621 int bd_idx, cnt_idx;
1627 if (port > hr_dev->caps.num_ports)
1630 desc_num = DIV_ROUND_UP(HNS_ROCE_HW_CNT_TOTAL, CNT_PER_DESC);
1631 desc = kcalloc(desc_num, sizeof(*desc), GFP_KERNEL);
1635 for (i = 0; i < desc_num; i++) {
1636 hns_roce_cmq_setup_basic_desc(&desc[i],
1637 HNS_ROCE_OPC_QUERY_COUNTER, true);
1638 if (i != desc_num - 1)
1639 desc[i].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1642 ret = hns_roce_cmq_send(hr_dev, desc, desc_num);
1644 ibdev_err(&hr_dev->ib_dev,
1645 "failed to get counter, ret = %d.\n", ret);
1649 for (i = 0; i < HNS_ROCE_HW_CNT_TOTAL && i < *num_counters; i++) {
1650 bd_idx = i / CNT_PER_DESC;
1651 if (!(desc[bd_idx].flag & HNS_ROCE_CMD_FLAG_NEXT) &&
1652 bd_idx != HNS_ROCE_HW_CNT_TOTAL / CNT_PER_DESC)
1655 cnt_data = (__le64 *)&desc[bd_idx].data[0];
1656 cnt_idx = i % CNT_PER_DESC;
1657 stats[i] = le64_to_cpu(cnt_data[cnt_idx]);
1666 static int hns_roce_config_global_param(struct hns_roce_dev *hr_dev)
1668 struct hns_roce_cmq_desc desc;
1669 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1670 u32 clock_cycles_of_1us;
1672 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GLOBAL_PARAM,
1675 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
1676 clock_cycles_of_1us = HNS_ROCE_1NS_CFG;
1678 clock_cycles_of_1us = HNS_ROCE_1US_CFG;
1680 hr_reg_write(req, CFG_GLOBAL_PARAM_1US_CYCLES, clock_cycles_of_1us);
1681 hr_reg_write(req, CFG_GLOBAL_PARAM_UDP_PORT, ROCE_V2_UDP_DPORT);
1683 return hns_roce_cmq_send(hr_dev, &desc, 1);
1686 static int load_func_res_caps(struct hns_roce_dev *hr_dev, bool is_vf)
1688 struct hns_roce_cmq_desc desc[2];
1689 struct hns_roce_cmq_req *r_a = (struct hns_roce_cmq_req *)desc[0].data;
1690 struct hns_roce_cmq_req *r_b = (struct hns_roce_cmq_req *)desc[1].data;
1691 struct hns_roce_caps *caps = &hr_dev->caps;
1692 enum hns_roce_opcode_type opcode;
1697 opcode = HNS_ROCE_OPC_QUERY_VF_RES;
1700 opcode = HNS_ROCE_OPC_QUERY_PF_RES;
1701 func_num = hr_dev->func_num;
1704 hns_roce_cmq_setup_basic_desc(&desc[0], opcode, true);
1705 desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1706 hns_roce_cmq_setup_basic_desc(&desc[1], opcode, true);
1708 ret = hns_roce_cmq_send(hr_dev, desc, 2);
1712 caps->qpc_bt_num = hr_reg_read(r_a, FUNC_RES_A_QPC_BT_NUM) / func_num;
1713 caps->srqc_bt_num = hr_reg_read(r_a, FUNC_RES_A_SRQC_BT_NUM) / func_num;
1714 caps->cqc_bt_num = hr_reg_read(r_a, FUNC_RES_A_CQC_BT_NUM) / func_num;
1715 caps->mpt_bt_num = hr_reg_read(r_a, FUNC_RES_A_MPT_BT_NUM) / func_num;
1716 caps->eqc_bt_num = hr_reg_read(r_a, FUNC_RES_A_EQC_BT_NUM) / func_num;
1717 caps->smac_bt_num = hr_reg_read(r_b, FUNC_RES_B_SMAC_NUM) / func_num;
1718 caps->sgid_bt_num = hr_reg_read(r_b, FUNC_RES_B_SGID_NUM) / func_num;
1719 caps->sccc_bt_num = hr_reg_read(r_b, FUNC_RES_B_SCCC_BT_NUM) / func_num;
1722 caps->sl_num = hr_reg_read(r_b, FUNC_RES_V_QID_NUM) / func_num;
1723 caps->gmv_bt_num = hr_reg_read(r_b, FUNC_RES_V_GMV_BT_NUM) /
1726 caps->sl_num = hr_reg_read(r_b, FUNC_RES_B_QID_NUM) / func_num;
1727 caps->gmv_bt_num = hr_reg_read(r_b, FUNC_RES_B_GMV_BT_NUM) /
1734 static int load_pf_timer_res_caps(struct hns_roce_dev *hr_dev)
1736 struct hns_roce_cmq_desc desc;
1737 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1738 struct hns_roce_caps *caps = &hr_dev->caps;
1741 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_PF_TIMER_RES,
1744 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1748 caps->qpc_timer_bt_num = hr_reg_read(req, PF_TIMER_RES_QPC_ITEM_NUM);
1749 caps->cqc_timer_bt_num = hr_reg_read(req, PF_TIMER_RES_CQC_ITEM_NUM);
1754 static int hns_roce_query_pf_resource(struct hns_roce_dev *hr_dev)
1756 struct device *dev = hr_dev->dev;
1759 ret = load_func_res_caps(hr_dev, false);
1761 dev_err(dev, "failed to load pf res caps, ret = %d.\n", ret);
1765 ret = load_pf_timer_res_caps(hr_dev);
1767 dev_err(dev, "failed to load pf timer resource, ret = %d.\n",
1773 static int hns_roce_query_vf_resource(struct hns_roce_dev *hr_dev)
1775 struct device *dev = hr_dev->dev;
1778 ret = load_func_res_caps(hr_dev, true);
1780 dev_err(dev, "failed to load vf res caps, ret = %d.\n", ret);
1785 static int __hns_roce_set_vf_switch_param(struct hns_roce_dev *hr_dev,
1788 struct hns_roce_vf_switch *swt;
1789 struct hns_roce_cmq_desc desc;
1792 swt = (struct hns_roce_vf_switch *)desc.data;
1793 hns_roce_cmq_setup_basic_desc(&desc, HNS_SWITCH_PARAMETER_CFG, true);
1794 swt->rocee_sel |= cpu_to_le32(HNS_ICL_SWITCH_CMD_ROCEE_SEL);
1795 hr_reg_write(swt, VF_SWITCH_VF_ID, vf_id);
1796 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
1800 desc.flag = cpu_to_le16(HNS_ROCE_CMD_FLAG_IN);
1801 desc.flag &= cpu_to_le16(~HNS_ROCE_CMD_FLAG_WR);
1802 hr_reg_enable(swt, VF_SWITCH_ALW_LPBK);
1803 hr_reg_clear(swt, VF_SWITCH_ALW_LCL_LPBK);
1804 hr_reg_enable(swt, VF_SWITCH_ALW_DST_OVRD);
1806 return hns_roce_cmq_send(hr_dev, &desc, 1);
1809 static int hns_roce_set_vf_switch_param(struct hns_roce_dev *hr_dev)
1814 for (vf_id = 0; vf_id < hr_dev->func_num; vf_id++) {
1815 ret = __hns_roce_set_vf_switch_param(hr_dev, vf_id);
1822 static int config_vf_hem_resource(struct hns_roce_dev *hr_dev, int vf_id)
1824 struct hns_roce_cmq_desc desc[2];
1825 struct hns_roce_cmq_req *r_a = (struct hns_roce_cmq_req *)desc[0].data;
1826 struct hns_roce_cmq_req *r_b = (struct hns_roce_cmq_req *)desc[1].data;
1827 enum hns_roce_opcode_type opcode = HNS_ROCE_OPC_ALLOC_VF_RES;
1828 struct hns_roce_caps *caps = &hr_dev->caps;
1830 hns_roce_cmq_setup_basic_desc(&desc[0], opcode, false);
1831 desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
1832 hns_roce_cmq_setup_basic_desc(&desc[1], opcode, false);
1834 hr_reg_write(r_a, FUNC_RES_A_VF_ID, vf_id);
1836 hr_reg_write(r_a, FUNC_RES_A_QPC_BT_NUM, caps->qpc_bt_num);
1837 hr_reg_write(r_a, FUNC_RES_A_QPC_BT_IDX, vf_id * caps->qpc_bt_num);
1838 hr_reg_write(r_a, FUNC_RES_A_SRQC_BT_NUM, caps->srqc_bt_num);
1839 hr_reg_write(r_a, FUNC_RES_A_SRQC_BT_IDX, vf_id * caps->srqc_bt_num);
1840 hr_reg_write(r_a, FUNC_RES_A_CQC_BT_NUM, caps->cqc_bt_num);
1841 hr_reg_write(r_a, FUNC_RES_A_CQC_BT_IDX, vf_id * caps->cqc_bt_num);
1842 hr_reg_write(r_a, FUNC_RES_A_MPT_BT_NUM, caps->mpt_bt_num);
1843 hr_reg_write(r_a, FUNC_RES_A_MPT_BT_IDX, vf_id * caps->mpt_bt_num);
1844 hr_reg_write(r_a, FUNC_RES_A_EQC_BT_NUM, caps->eqc_bt_num);
1845 hr_reg_write(r_a, FUNC_RES_A_EQC_BT_IDX, vf_id * caps->eqc_bt_num);
1846 hr_reg_write(r_b, FUNC_RES_V_QID_NUM, caps->sl_num);
1847 hr_reg_write(r_b, FUNC_RES_B_QID_IDX, vf_id * caps->sl_num);
1848 hr_reg_write(r_b, FUNC_RES_B_SCCC_BT_NUM, caps->sccc_bt_num);
1849 hr_reg_write(r_b, FUNC_RES_B_SCCC_BT_IDX, vf_id * caps->sccc_bt_num);
1851 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
1852 hr_reg_write(r_b, FUNC_RES_V_GMV_BT_NUM, caps->gmv_bt_num);
1853 hr_reg_write(r_b, FUNC_RES_B_GMV_BT_IDX,
1854 vf_id * caps->gmv_bt_num);
1856 hr_reg_write(r_b, FUNC_RES_B_SGID_NUM, caps->sgid_bt_num);
1857 hr_reg_write(r_b, FUNC_RES_B_SGID_IDX,
1858 vf_id * caps->sgid_bt_num);
1859 hr_reg_write(r_b, FUNC_RES_B_SMAC_NUM, caps->smac_bt_num);
1860 hr_reg_write(r_b, FUNC_RES_B_SMAC_IDX,
1861 vf_id * caps->smac_bt_num);
1864 return hns_roce_cmq_send(hr_dev, desc, 2);
1867 static int hns_roce_alloc_vf_resource(struct hns_roce_dev *hr_dev)
1869 u32 func_num = max_t(u32, 1, hr_dev->func_num);
1873 for (vf_id = 0; vf_id < func_num; vf_id++) {
1874 ret = config_vf_hem_resource(hr_dev, vf_id);
1876 dev_err(hr_dev->dev,
1877 "failed to config vf-%u hem res, ret = %d.\n",
1886 static int hns_roce_v2_set_bt(struct hns_roce_dev *hr_dev)
1888 struct hns_roce_cmq_desc desc;
1889 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
1890 struct hns_roce_caps *caps = &hr_dev->caps;
1892 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_BT_ATTR, false);
1894 hr_reg_write(req, CFG_BT_ATTR_QPC_BA_PGSZ,
1895 caps->qpc_ba_pg_sz + PG_SHIFT_OFFSET);
1896 hr_reg_write(req, CFG_BT_ATTR_QPC_BUF_PGSZ,
1897 caps->qpc_buf_pg_sz + PG_SHIFT_OFFSET);
1898 hr_reg_write(req, CFG_BT_ATTR_QPC_HOPNUM,
1899 to_hr_hem_hopnum(caps->qpc_hop_num, caps->num_qps));
1901 hr_reg_write(req, CFG_BT_ATTR_SRQC_BA_PGSZ,
1902 caps->srqc_ba_pg_sz + PG_SHIFT_OFFSET);
1903 hr_reg_write(req, CFG_BT_ATTR_SRQC_BUF_PGSZ,
1904 caps->srqc_buf_pg_sz + PG_SHIFT_OFFSET);
1905 hr_reg_write(req, CFG_BT_ATTR_SRQC_HOPNUM,
1906 to_hr_hem_hopnum(caps->srqc_hop_num, caps->num_srqs));
1908 hr_reg_write(req, CFG_BT_ATTR_CQC_BA_PGSZ,
1909 caps->cqc_ba_pg_sz + PG_SHIFT_OFFSET);
1910 hr_reg_write(req, CFG_BT_ATTR_CQC_BUF_PGSZ,
1911 caps->cqc_buf_pg_sz + PG_SHIFT_OFFSET);
1912 hr_reg_write(req, CFG_BT_ATTR_CQC_HOPNUM,
1913 to_hr_hem_hopnum(caps->cqc_hop_num, caps->num_cqs));
1915 hr_reg_write(req, CFG_BT_ATTR_MPT_BA_PGSZ,
1916 caps->mpt_ba_pg_sz + PG_SHIFT_OFFSET);
1917 hr_reg_write(req, CFG_BT_ATTR_MPT_BUF_PGSZ,
1918 caps->mpt_buf_pg_sz + PG_SHIFT_OFFSET);
1919 hr_reg_write(req, CFG_BT_ATTR_MPT_HOPNUM,
1920 to_hr_hem_hopnum(caps->mpt_hop_num, caps->num_mtpts));
1922 hr_reg_write(req, CFG_BT_ATTR_SCCC_BA_PGSZ,
1923 caps->sccc_ba_pg_sz + PG_SHIFT_OFFSET);
1924 hr_reg_write(req, CFG_BT_ATTR_SCCC_BUF_PGSZ,
1925 caps->sccc_buf_pg_sz + PG_SHIFT_OFFSET);
1926 hr_reg_write(req, CFG_BT_ATTR_SCCC_HOPNUM,
1927 to_hr_hem_hopnum(caps->sccc_hop_num, caps->num_qps));
1929 return hns_roce_cmq_send(hr_dev, &desc, 1);
1932 static void calc_pg_sz(u32 obj_num, u32 obj_size, u32 hop_num, u32 ctx_bt_num,
1933 u32 *buf_page_size, u32 *bt_page_size, u32 hem_type)
1936 u64 bt_chunk_size = PAGE_SIZE;
1937 u64 buf_chunk_size = PAGE_SIZE;
1938 u64 obj_per_chunk_default = buf_chunk_size / obj_size;
1945 obj_per_chunk = ctx_bt_num * (bt_chunk_size / BA_BYTE_LEN) *
1946 (bt_chunk_size / BA_BYTE_LEN) *
1947 (bt_chunk_size / BA_BYTE_LEN) *
1948 obj_per_chunk_default;
1951 obj_per_chunk = ctx_bt_num * (bt_chunk_size / BA_BYTE_LEN) *
1952 (bt_chunk_size / BA_BYTE_LEN) *
1953 obj_per_chunk_default;
1956 obj_per_chunk = ctx_bt_num * (bt_chunk_size / BA_BYTE_LEN) *
1957 obj_per_chunk_default;
1959 case HNS_ROCE_HOP_NUM_0:
1960 obj_per_chunk = ctx_bt_num * obj_per_chunk_default;
1963 pr_err("table %u not support hop_num = %u!\n", hem_type,
1968 if (hem_type >= HEM_TYPE_MTT)
1969 *bt_page_size = ilog2(DIV_ROUND_UP(obj_num, obj_per_chunk));
1971 *buf_page_size = ilog2(DIV_ROUND_UP(obj_num, obj_per_chunk));
1974 static void set_hem_page_size(struct hns_roce_dev *hr_dev)
1976 struct hns_roce_caps *caps = &hr_dev->caps;
1979 caps->eqe_ba_pg_sz = 0;
1980 caps->eqe_buf_pg_sz = 0;
1983 caps->llm_buf_pg_sz = 0;
1986 caps->mpt_ba_pg_sz = 0;
1987 caps->mpt_buf_pg_sz = 0;
1988 caps->pbl_ba_pg_sz = HNS_ROCE_BA_PG_SZ_SUPPORTED_16K;
1989 caps->pbl_buf_pg_sz = 0;
1990 calc_pg_sz(caps->num_mtpts, caps->mtpt_entry_sz, caps->mpt_hop_num,
1991 caps->mpt_bt_num, &caps->mpt_buf_pg_sz, &caps->mpt_ba_pg_sz,
1995 caps->qpc_ba_pg_sz = 0;
1996 caps->qpc_buf_pg_sz = 0;
1997 caps->qpc_timer_ba_pg_sz = 0;
1998 caps->qpc_timer_buf_pg_sz = 0;
1999 caps->sccc_ba_pg_sz = 0;
2000 caps->sccc_buf_pg_sz = 0;
2001 caps->mtt_ba_pg_sz = 0;
2002 caps->mtt_buf_pg_sz = 0;
2003 calc_pg_sz(caps->num_qps, caps->qpc_sz, caps->qpc_hop_num,
2004 caps->qpc_bt_num, &caps->qpc_buf_pg_sz, &caps->qpc_ba_pg_sz,
2007 if (caps->flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL)
2008 calc_pg_sz(caps->num_qps, caps->sccc_sz, caps->sccc_hop_num,
2009 caps->sccc_bt_num, &caps->sccc_buf_pg_sz,
2010 &caps->sccc_ba_pg_sz, HEM_TYPE_SCCC);
2013 caps->cqc_ba_pg_sz = 0;
2014 caps->cqc_buf_pg_sz = 0;
2015 caps->cqc_timer_ba_pg_sz = 0;
2016 caps->cqc_timer_buf_pg_sz = 0;
2017 caps->cqe_ba_pg_sz = HNS_ROCE_BA_PG_SZ_SUPPORTED_256K;
2018 caps->cqe_buf_pg_sz = 0;
2019 calc_pg_sz(caps->num_cqs, caps->cqc_entry_sz, caps->cqc_hop_num,
2020 caps->cqc_bt_num, &caps->cqc_buf_pg_sz, &caps->cqc_ba_pg_sz,
2022 calc_pg_sz(caps->max_cqes, caps->cqe_sz, caps->cqe_hop_num,
2023 1, &caps->cqe_buf_pg_sz, &caps->cqe_ba_pg_sz, HEM_TYPE_CQE);
2026 if (caps->flags & HNS_ROCE_CAP_FLAG_SRQ) {
2027 caps->srqc_ba_pg_sz = 0;
2028 caps->srqc_buf_pg_sz = 0;
2029 caps->srqwqe_ba_pg_sz = 0;
2030 caps->srqwqe_buf_pg_sz = 0;
2031 caps->idx_ba_pg_sz = 0;
2032 caps->idx_buf_pg_sz = 0;
2033 calc_pg_sz(caps->num_srqs, caps->srqc_entry_sz,
2034 caps->srqc_hop_num, caps->srqc_bt_num,
2035 &caps->srqc_buf_pg_sz, &caps->srqc_ba_pg_sz,
2037 calc_pg_sz(caps->num_srqwqe_segs, caps->mtt_entry_sz,
2038 caps->srqwqe_hop_num, 1, &caps->srqwqe_buf_pg_sz,
2039 &caps->srqwqe_ba_pg_sz, HEM_TYPE_SRQWQE);
2040 calc_pg_sz(caps->num_idx_segs, caps->idx_entry_sz,
2041 caps->idx_hop_num, 1, &caps->idx_buf_pg_sz,
2042 &caps->idx_ba_pg_sz, HEM_TYPE_IDX);
2046 caps->gmv_ba_pg_sz = 0;
2047 caps->gmv_buf_pg_sz = 0;
2050 /* Apply all loaded caps before setting to hardware */
2051 static void apply_func_caps(struct hns_roce_dev *hr_dev)
2053 struct hns_roce_caps *caps = &hr_dev->caps;
2054 struct hns_roce_v2_priv *priv = hr_dev->priv;
2056 /* The following configurations don't need to be got from firmware. */
2057 caps->qpc_timer_entry_sz = HNS_ROCE_V2_QPC_TIMER_ENTRY_SZ;
2058 caps->cqc_timer_entry_sz = HNS_ROCE_V2_CQC_TIMER_ENTRY_SZ;
2059 caps->mtt_entry_sz = HNS_ROCE_V2_MTT_ENTRY_SZ;
2061 caps->pbl_hop_num = HNS_ROCE_PBL_HOP_NUM;
2062 caps->qpc_timer_hop_num = HNS_ROCE_HOP_NUM_0;
2063 caps->cqc_timer_hop_num = HNS_ROCE_HOP_NUM_0;
2065 caps->num_srqwqe_segs = HNS_ROCE_V2_MAX_SRQWQE_SEGS;
2066 caps->num_idx_segs = HNS_ROCE_V2_MAX_IDX_SEGS;
2068 if (!caps->num_comp_vectors)
2069 caps->num_comp_vectors =
2070 min_t(u32, caps->eqc_bt_num - HNS_ROCE_V2_AEQE_VEC_NUM,
2071 (u32)priv->handle->rinfo.num_vectors -
2072 (HNS_ROCE_V2_AEQE_VEC_NUM + HNS_ROCE_V2_ABNORMAL_VEC_NUM));
2074 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
2075 caps->eqe_hop_num = HNS_ROCE_V3_EQE_HOP_NUM;
2076 caps->ceqe_size = HNS_ROCE_V3_EQE_SIZE;
2077 caps->aeqe_size = HNS_ROCE_V3_EQE_SIZE;
2079 /* The following configurations will be overwritten */
2080 caps->qpc_sz = HNS_ROCE_V3_QPC_SZ;
2081 caps->cqe_sz = HNS_ROCE_V3_CQE_SIZE;
2082 caps->sccc_sz = HNS_ROCE_V3_SCCC_SZ;
2084 /* The following configurations are not got from firmware */
2085 caps->gmv_entry_sz = HNS_ROCE_V3_GMV_ENTRY_SZ;
2087 caps->gmv_hop_num = HNS_ROCE_HOP_NUM_0;
2088 caps->gid_table_len[0] = caps->gmv_bt_num *
2089 (HNS_HW_PAGE_SIZE / caps->gmv_entry_sz);
2091 caps->gmv_entry_num = caps->gmv_bt_num * (PAGE_SIZE /
2092 caps->gmv_entry_sz);
2094 u32 func_num = max_t(u32, 1, hr_dev->func_num);
2096 caps->eqe_hop_num = HNS_ROCE_V2_EQE_HOP_NUM;
2097 caps->ceqe_size = HNS_ROCE_CEQE_SIZE;
2098 caps->aeqe_size = HNS_ROCE_AEQE_SIZE;
2099 caps->gid_table_len[0] /= func_num;
2102 if (hr_dev->is_vf) {
2103 caps->default_aeq_arm_st = 0x3;
2104 caps->default_ceq_arm_st = 0x3;
2105 caps->default_ceq_max_cnt = 0x1;
2106 caps->default_ceq_period = 0x10;
2107 caps->default_aeq_max_cnt = 0x1;
2108 caps->default_aeq_period = 0x10;
2111 set_hem_page_size(hr_dev);
2114 static int hns_roce_query_caps(struct hns_roce_dev *hr_dev)
2116 struct hns_roce_cmq_desc desc[HNS_ROCE_QUERY_PF_CAPS_CMD_NUM];
2117 struct hns_roce_caps *caps = &hr_dev->caps;
2118 struct hns_roce_query_pf_caps_a *resp_a;
2119 struct hns_roce_query_pf_caps_b *resp_b;
2120 struct hns_roce_query_pf_caps_c *resp_c;
2121 struct hns_roce_query_pf_caps_d *resp_d;
2122 struct hns_roce_query_pf_caps_e *resp_e;
2123 enum hns_roce_opcode_type cmd;
2129 cmd = hr_dev->is_vf ? HNS_ROCE_OPC_QUERY_VF_CAPS_NUM :
2130 HNS_ROCE_OPC_QUERY_PF_CAPS_NUM;
2132 for (i = 0; i < HNS_ROCE_QUERY_PF_CAPS_CMD_NUM; i++) {
2133 hns_roce_cmq_setup_basic_desc(&desc[i], cmd, true);
2134 if (i < (HNS_ROCE_QUERY_PF_CAPS_CMD_NUM - 1))
2135 desc[i].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
2137 desc[i].flag &= ~cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
2140 ret = hns_roce_cmq_send(hr_dev, desc, HNS_ROCE_QUERY_PF_CAPS_CMD_NUM);
2144 resp_a = (struct hns_roce_query_pf_caps_a *)desc[0].data;
2145 resp_b = (struct hns_roce_query_pf_caps_b *)desc[1].data;
2146 resp_c = (struct hns_roce_query_pf_caps_c *)desc[2].data;
2147 resp_d = (struct hns_roce_query_pf_caps_d *)desc[3].data;
2148 resp_e = (struct hns_roce_query_pf_caps_e *)desc[4].data;
2150 caps->local_ca_ack_delay = resp_a->local_ca_ack_delay;
2151 caps->max_sq_sg = le16_to_cpu(resp_a->max_sq_sg);
2152 caps->max_sq_inline = le16_to_cpu(resp_a->max_sq_inline);
2153 caps->max_rq_sg = le16_to_cpu(resp_a->max_rq_sg);
2154 caps->max_rq_sg = roundup_pow_of_two(caps->max_rq_sg);
2155 caps->max_srq_sges = le16_to_cpu(resp_a->max_srq_sges);
2156 caps->max_srq_sges = roundup_pow_of_two(caps->max_srq_sges);
2157 caps->num_aeq_vectors = resp_a->num_aeq_vectors;
2158 caps->num_other_vectors = resp_a->num_other_vectors;
2159 caps->max_sq_desc_sz = resp_a->max_sq_desc_sz;
2160 caps->max_rq_desc_sz = resp_a->max_rq_desc_sz;
2162 caps->mtpt_entry_sz = resp_b->mtpt_entry_sz;
2163 caps->irrl_entry_sz = resp_b->irrl_entry_sz;
2164 caps->trrl_entry_sz = resp_b->trrl_entry_sz;
2165 caps->cqc_entry_sz = resp_b->cqc_entry_sz;
2166 caps->srqc_entry_sz = resp_b->srqc_entry_sz;
2167 caps->idx_entry_sz = resp_b->idx_entry_sz;
2168 caps->sccc_sz = resp_b->sccc_sz;
2169 caps->max_mtu = resp_b->max_mtu;
2170 caps->min_cqes = resp_b->min_cqes;
2171 caps->min_wqes = resp_b->min_wqes;
2172 caps->page_size_cap = le32_to_cpu(resp_b->page_size_cap);
2173 caps->pkey_table_len[0] = resp_b->pkey_table_len;
2174 caps->phy_num_uars = resp_b->phy_num_uars;
2175 ctx_hop_num = resp_b->ctx_hop_num;
2176 pbl_hop_num = resp_b->pbl_hop_num;
2178 caps->num_pds = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_PDS);
2180 caps->flags = hr_reg_read(resp_c, PF_CAPS_C_CAP_FLAGS);
2181 caps->flags |= le16_to_cpu(resp_d->cap_flags_ex) <<
2182 HNS_ROCE_CAP_FLAGS_EX_SHIFT;
2184 caps->num_cqs = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_CQS);
2185 caps->gid_table_len[0] = hr_reg_read(resp_c, PF_CAPS_C_MAX_GID);
2186 caps->max_cqes = 1 << hr_reg_read(resp_c, PF_CAPS_C_CQ_DEPTH);
2187 caps->num_xrcds = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_XRCDS);
2188 caps->num_mtpts = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_MRWS);
2189 caps->num_qps = 1 << hr_reg_read(resp_c, PF_CAPS_C_NUM_QPS);
2190 caps->max_qp_init_rdma = hr_reg_read(resp_c, PF_CAPS_C_MAX_ORD);
2191 caps->max_qp_dest_rdma = caps->max_qp_init_rdma;
2192 caps->max_wqes = 1 << le16_to_cpu(resp_c->sq_depth);
2194 caps->num_srqs = 1 << hr_reg_read(resp_d, PF_CAPS_D_NUM_SRQS);
2195 caps->cong_type = hr_reg_read(resp_d, PF_CAPS_D_CONG_TYPE);
2196 caps->max_srq_wrs = 1 << le16_to_cpu(resp_d->srq_depth);
2197 caps->ceqe_depth = 1 << hr_reg_read(resp_d, PF_CAPS_D_CEQ_DEPTH);
2198 caps->num_comp_vectors = hr_reg_read(resp_d, PF_CAPS_D_NUM_CEQS);
2199 caps->aeqe_depth = 1 << hr_reg_read(resp_d, PF_CAPS_D_AEQ_DEPTH);
2200 caps->reserved_pds = hr_reg_read(resp_d, PF_CAPS_D_RSV_PDS);
2201 caps->num_uars = 1 << hr_reg_read(resp_d, PF_CAPS_D_NUM_UARS);
2202 caps->reserved_qps = hr_reg_read(resp_d, PF_CAPS_D_RSV_QPS);
2203 caps->reserved_uars = hr_reg_read(resp_d, PF_CAPS_D_RSV_UARS);
2205 caps->reserved_mrws = hr_reg_read(resp_e, PF_CAPS_E_RSV_MRWS);
2206 caps->chunk_sz = 1 << hr_reg_read(resp_e, PF_CAPS_E_CHUNK_SIZE_SHIFT);
2207 caps->reserved_cqs = hr_reg_read(resp_e, PF_CAPS_E_RSV_CQS);
2208 caps->reserved_xrcds = hr_reg_read(resp_e, PF_CAPS_E_RSV_XRCDS);
2209 caps->reserved_srqs = hr_reg_read(resp_e, PF_CAPS_E_RSV_SRQS);
2210 caps->reserved_lkey = hr_reg_read(resp_e, PF_CAPS_E_RSV_LKEYS);
2212 caps->qpc_hop_num = ctx_hop_num;
2213 caps->sccc_hop_num = ctx_hop_num;
2214 caps->srqc_hop_num = ctx_hop_num;
2215 caps->cqc_hop_num = ctx_hop_num;
2216 caps->mpt_hop_num = ctx_hop_num;
2217 caps->mtt_hop_num = pbl_hop_num;
2218 caps->cqe_hop_num = pbl_hop_num;
2219 caps->srqwqe_hop_num = pbl_hop_num;
2220 caps->idx_hop_num = pbl_hop_num;
2221 caps->wqe_sq_hop_num = hr_reg_read(resp_d, PF_CAPS_D_SQWQE_HOP_NUM);
2222 caps->wqe_sge_hop_num = hr_reg_read(resp_d, PF_CAPS_D_EX_SGE_HOP_NUM);
2223 caps->wqe_rq_hop_num = hr_reg_read(resp_d, PF_CAPS_D_RQWQE_HOP_NUM);
2225 if (!(caps->page_size_cap & PAGE_SIZE))
2226 caps->page_size_cap = HNS_ROCE_V2_PAGE_SIZE_SUPPORTED;
2228 if (!hr_dev->is_vf) {
2229 caps->cqe_sz = resp_a->cqe_sz;
2230 caps->qpc_sz = le16_to_cpu(resp_b->qpc_sz);
2231 caps->default_aeq_arm_st =
2232 hr_reg_read(resp_d, PF_CAPS_D_AEQ_ARM_ST);
2233 caps->default_ceq_arm_st =
2234 hr_reg_read(resp_d, PF_CAPS_D_CEQ_ARM_ST);
2235 caps->default_ceq_max_cnt = le16_to_cpu(resp_e->ceq_max_cnt);
2236 caps->default_ceq_period = le16_to_cpu(resp_e->ceq_period);
2237 caps->default_aeq_max_cnt = le16_to_cpu(resp_e->aeq_max_cnt);
2238 caps->default_aeq_period = le16_to_cpu(resp_e->aeq_period);
2244 static int config_hem_entry_size(struct hns_roce_dev *hr_dev, u32 type, u32 val)
2246 struct hns_roce_cmq_desc desc;
2247 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
2249 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_ENTRY_SIZE,
2252 hr_reg_write(req, CFG_HEM_ENTRY_SIZE_TYPE, type);
2253 hr_reg_write(req, CFG_HEM_ENTRY_SIZE_VALUE, val);
2255 return hns_roce_cmq_send(hr_dev, &desc, 1);
2258 static int hns_roce_config_entry_size(struct hns_roce_dev *hr_dev)
2260 struct hns_roce_caps *caps = &hr_dev->caps;
2263 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
2266 ret = config_hem_entry_size(hr_dev, HNS_ROCE_CFG_QPC_SIZE,
2269 dev_err(hr_dev->dev, "failed to cfg qpc sz, ret = %d.\n", ret);
2273 ret = config_hem_entry_size(hr_dev, HNS_ROCE_CFG_SCCC_SIZE,
2276 dev_err(hr_dev->dev, "failed to cfg sccc sz, ret = %d.\n", ret);
2281 static int hns_roce_v2_vf_profile(struct hns_roce_dev *hr_dev)
2283 struct device *dev = hr_dev->dev;
2286 hr_dev->func_num = 1;
2288 ret = hns_roce_query_caps(hr_dev);
2290 dev_err(dev, "failed to query VF caps, ret = %d.\n", ret);
2294 ret = hns_roce_query_vf_resource(hr_dev);
2296 dev_err(dev, "failed to query VF resource, ret = %d.\n", ret);
2300 apply_func_caps(hr_dev);
2302 ret = hns_roce_v2_set_bt(hr_dev);
2304 dev_err(dev, "failed to config VF BA table, ret = %d.\n", ret);
2309 static int hns_roce_v2_pf_profile(struct hns_roce_dev *hr_dev)
2311 struct device *dev = hr_dev->dev;
2314 ret = hns_roce_query_func_info(hr_dev);
2316 dev_err(dev, "failed to query func info, ret = %d.\n", ret);
2320 ret = hns_roce_config_global_param(hr_dev);
2322 dev_err(dev, "failed to config global param, ret = %d.\n", ret);
2326 ret = hns_roce_set_vf_switch_param(hr_dev);
2328 dev_err(dev, "failed to set switch param, ret = %d.\n", ret);
2332 ret = hns_roce_query_caps(hr_dev);
2334 dev_err(dev, "failed to query PF caps, ret = %d.\n", ret);
2338 ret = hns_roce_query_pf_resource(hr_dev);
2340 dev_err(dev, "failed to query pf resource, ret = %d.\n", ret);
2344 apply_func_caps(hr_dev);
2346 ret = hns_roce_alloc_vf_resource(hr_dev);
2348 dev_err(dev, "failed to alloc vf resource, ret = %d.\n", ret);
2352 ret = hns_roce_v2_set_bt(hr_dev);
2354 dev_err(dev, "failed to config BA table, ret = %d.\n", ret);
2358 /* Configure the size of QPC, SCCC, etc. */
2359 return hns_roce_config_entry_size(hr_dev);
2362 static int hns_roce_v2_profile(struct hns_roce_dev *hr_dev)
2364 struct device *dev = hr_dev->dev;
2367 ret = hns_roce_cmq_query_hw_info(hr_dev);
2369 dev_err(dev, "failed to query hardware info, ret = %d.\n", ret);
2373 ret = hns_roce_query_fw_ver(hr_dev);
2375 dev_err(dev, "failed to query firmware info, ret = %d.\n", ret);
2379 hr_dev->vendor_part_id = hr_dev->pci_dev->device;
2380 hr_dev->sys_image_guid = be64_to_cpu(hr_dev->ib_dev.node_guid);
2383 return hns_roce_v2_vf_profile(hr_dev);
2385 return hns_roce_v2_pf_profile(hr_dev);
2388 static void config_llm_table(struct hns_roce_buf *data_buf, void *cfg_buf)
2390 u32 i, next_ptr, page_num;
2391 __le64 *entry = cfg_buf;
2395 page_num = data_buf->npages;
2396 for (i = 0; i < page_num; i++) {
2397 addr = hns_roce_buf_page(data_buf, i);
2398 if (i == (page_num - 1))
2403 val = HNS_ROCE_EXT_LLM_ENTRY(addr, (u64)next_ptr);
2404 entry[i] = cpu_to_le64(val);
2408 static int set_llm_cfg_to_hw(struct hns_roce_dev *hr_dev,
2409 struct hns_roce_link_table *table)
2411 struct hns_roce_cmq_desc desc[2];
2412 struct hns_roce_cmq_req *r_a = (struct hns_roce_cmq_req *)desc[0].data;
2413 struct hns_roce_cmq_req *r_b = (struct hns_roce_cmq_req *)desc[1].data;
2414 struct hns_roce_buf *buf = table->buf;
2415 enum hns_roce_opcode_type opcode;
2418 opcode = HNS_ROCE_OPC_CFG_EXT_LLM;
2419 hns_roce_cmq_setup_basic_desc(&desc[0], opcode, false);
2420 desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
2421 hns_roce_cmq_setup_basic_desc(&desc[1], opcode, false);
2423 hr_reg_write(r_a, CFG_LLM_A_BA_L, lower_32_bits(table->table.map));
2424 hr_reg_write(r_a, CFG_LLM_A_BA_H, upper_32_bits(table->table.map));
2425 hr_reg_write(r_a, CFG_LLM_A_DEPTH, buf->npages);
2426 hr_reg_write(r_a, CFG_LLM_A_PGSZ, to_hr_hw_page_shift(buf->page_shift));
2427 hr_reg_enable(r_a, CFG_LLM_A_INIT_EN);
2429 addr = to_hr_hw_page_addr(hns_roce_buf_page(buf, 0));
2430 hr_reg_write(r_a, CFG_LLM_A_HEAD_BA_L, lower_32_bits(addr));
2431 hr_reg_write(r_a, CFG_LLM_A_HEAD_BA_H, upper_32_bits(addr));
2432 hr_reg_write(r_a, CFG_LLM_A_HEAD_NXTPTR, 1);
2433 hr_reg_write(r_a, CFG_LLM_A_HEAD_PTR, 0);
2435 addr = to_hr_hw_page_addr(hns_roce_buf_page(buf, buf->npages - 1));
2436 hr_reg_write(r_b, CFG_LLM_B_TAIL_BA_L, lower_32_bits(addr));
2437 hr_reg_write(r_b, CFG_LLM_B_TAIL_BA_H, upper_32_bits(addr));
2438 hr_reg_write(r_b, CFG_LLM_B_TAIL_PTR, buf->npages - 1);
2440 return hns_roce_cmq_send(hr_dev, desc, 2);
2443 static struct hns_roce_link_table *
2444 alloc_link_table_buf(struct hns_roce_dev *hr_dev)
2446 struct hns_roce_v2_priv *priv = hr_dev->priv;
2447 struct hns_roce_link_table *link_tbl;
2448 u32 pg_shift, size, min_size;
2450 link_tbl = &priv->ext_llm;
2451 pg_shift = hr_dev->caps.llm_buf_pg_sz + PAGE_SHIFT;
2452 size = hr_dev->caps.num_qps * HNS_ROCE_V2_EXT_LLM_ENTRY_SZ;
2453 min_size = HNS_ROCE_EXT_LLM_MIN_PAGES(hr_dev->caps.sl_num) << pg_shift;
2455 /* Alloc data table */
2456 size = max(size, min_size);
2457 link_tbl->buf = hns_roce_buf_alloc(hr_dev, size, pg_shift, 0);
2458 if (IS_ERR(link_tbl->buf))
2459 return ERR_PTR(-ENOMEM);
2461 /* Alloc config table */
2462 size = link_tbl->buf->npages * sizeof(u64);
2463 link_tbl->table.buf = dma_alloc_coherent(hr_dev->dev, size,
2464 &link_tbl->table.map,
2466 if (!link_tbl->table.buf) {
2467 hns_roce_buf_free(hr_dev, link_tbl->buf);
2468 return ERR_PTR(-ENOMEM);
2474 static void free_link_table_buf(struct hns_roce_dev *hr_dev,
2475 struct hns_roce_link_table *tbl)
2478 u32 size = tbl->buf->npages * sizeof(u64);
2480 dma_free_coherent(hr_dev->dev, size, tbl->table.buf,
2484 hns_roce_buf_free(hr_dev, tbl->buf);
2487 static int hns_roce_init_link_table(struct hns_roce_dev *hr_dev)
2489 struct hns_roce_link_table *link_tbl;
2492 link_tbl = alloc_link_table_buf(hr_dev);
2493 if (IS_ERR(link_tbl))
2496 if (WARN_ON(link_tbl->buf->npages > HNS_ROCE_V2_EXT_LLM_MAX_DEPTH)) {
2501 config_llm_table(link_tbl->buf, link_tbl->table.buf);
2502 ret = set_llm_cfg_to_hw(hr_dev, link_tbl);
2509 free_link_table_buf(hr_dev, link_tbl);
2513 static void hns_roce_free_link_table(struct hns_roce_dev *hr_dev)
2515 struct hns_roce_v2_priv *priv = hr_dev->priv;
2517 free_link_table_buf(hr_dev, &priv->ext_llm);
2520 static void free_dip_list(struct hns_roce_dev *hr_dev)
2522 struct hns_roce_dip *hr_dip;
2523 struct hns_roce_dip *tmp;
2524 unsigned long flags;
2526 spin_lock_irqsave(&hr_dev->dip_list_lock, flags);
2528 list_for_each_entry_safe(hr_dip, tmp, &hr_dev->dip_list, node) {
2529 list_del(&hr_dip->node);
2533 spin_unlock_irqrestore(&hr_dev->dip_list_lock, flags);
2536 static struct ib_pd *free_mr_init_pd(struct hns_roce_dev *hr_dev)
2538 struct hns_roce_v2_priv *priv = hr_dev->priv;
2539 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2540 struct ib_device *ibdev = &hr_dev->ib_dev;
2541 struct hns_roce_pd *hr_pd;
2544 hr_pd = kzalloc(sizeof(*hr_pd), GFP_KERNEL);
2545 if (ZERO_OR_NULL_PTR(hr_pd))
2550 if (hns_roce_alloc_pd(pd, NULL)) {
2551 ibdev_err(ibdev, "failed to create pd for free mr.\n");
2555 free_mr->rsv_pd = to_hr_pd(pd);
2556 free_mr->rsv_pd->ibpd.device = &hr_dev->ib_dev;
2557 free_mr->rsv_pd->ibpd.uobject = NULL;
2558 free_mr->rsv_pd->ibpd.__internal_mr = NULL;
2559 atomic_set(&free_mr->rsv_pd->ibpd.usecnt, 0);
2564 static struct ib_cq *free_mr_init_cq(struct hns_roce_dev *hr_dev)
2566 struct hns_roce_v2_priv *priv = hr_dev->priv;
2567 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2568 struct ib_device *ibdev = &hr_dev->ib_dev;
2569 struct ib_cq_init_attr cq_init_attr = {};
2570 struct hns_roce_cq *hr_cq;
2573 cq_init_attr.cqe = HNS_ROCE_FREE_MR_USED_CQE_NUM;
2575 hr_cq = kzalloc(sizeof(*hr_cq), GFP_KERNEL);
2576 if (ZERO_OR_NULL_PTR(hr_cq))
2582 if (hns_roce_create_cq(cq, &cq_init_attr, NULL)) {
2583 ibdev_err(ibdev, "failed to create cq for free mr.\n");
2587 free_mr->rsv_cq = to_hr_cq(cq);
2588 free_mr->rsv_cq->ib_cq.device = &hr_dev->ib_dev;
2589 free_mr->rsv_cq->ib_cq.uobject = NULL;
2590 free_mr->rsv_cq->ib_cq.comp_handler = NULL;
2591 free_mr->rsv_cq->ib_cq.event_handler = NULL;
2592 free_mr->rsv_cq->ib_cq.cq_context = NULL;
2593 atomic_set(&free_mr->rsv_cq->ib_cq.usecnt, 0);
2598 static int free_mr_init_qp(struct hns_roce_dev *hr_dev, struct ib_cq *cq,
2599 struct ib_qp_init_attr *init_attr, int i)
2601 struct hns_roce_v2_priv *priv = hr_dev->priv;
2602 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2603 struct ib_device *ibdev = &hr_dev->ib_dev;
2604 struct hns_roce_qp *hr_qp;
2608 hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL);
2609 if (ZERO_OR_NULL_PTR(hr_qp))
2615 ret = hns_roce_create_qp(qp, init_attr, NULL);
2617 ibdev_err(ibdev, "failed to create qp for free mr.\n");
2622 free_mr->rsv_qp[i] = hr_qp;
2623 free_mr->rsv_qp[i]->ibqp.recv_cq = cq;
2624 free_mr->rsv_qp[i]->ibqp.send_cq = cq;
2629 static void free_mr_exit(struct hns_roce_dev *hr_dev)
2631 struct hns_roce_v2_priv *priv = hr_dev->priv;
2632 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2636 for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
2637 if (free_mr->rsv_qp[i]) {
2638 qp = &free_mr->rsv_qp[i]->ibqp;
2639 hns_roce_v2_destroy_qp(qp, NULL);
2640 kfree(free_mr->rsv_qp[i]);
2641 free_mr->rsv_qp[i] = NULL;
2645 if (free_mr->rsv_cq) {
2646 hns_roce_destroy_cq(&free_mr->rsv_cq->ib_cq, NULL);
2647 kfree(free_mr->rsv_cq);
2648 free_mr->rsv_cq = NULL;
2651 if (free_mr->rsv_pd) {
2652 hns_roce_dealloc_pd(&free_mr->rsv_pd->ibpd, NULL);
2653 kfree(free_mr->rsv_pd);
2654 free_mr->rsv_pd = NULL;
2658 static int free_mr_alloc_res(struct hns_roce_dev *hr_dev)
2660 struct hns_roce_v2_priv *priv = hr_dev->priv;
2661 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2662 struct ib_qp_init_attr qp_init_attr = {};
2668 pd = free_mr_init_pd(hr_dev);
2672 cq = free_mr_init_cq(hr_dev);
2675 goto create_failed_cq;
2678 qp_init_attr.qp_type = IB_QPT_RC;
2679 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2680 qp_init_attr.send_cq = cq;
2681 qp_init_attr.recv_cq = cq;
2682 for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
2683 qp_init_attr.cap.max_send_wr = HNS_ROCE_FREE_MR_USED_SQWQE_NUM;
2684 qp_init_attr.cap.max_send_sge = HNS_ROCE_FREE_MR_USED_SQSGE_NUM;
2685 qp_init_attr.cap.max_recv_wr = HNS_ROCE_FREE_MR_USED_RQWQE_NUM;
2686 qp_init_attr.cap.max_recv_sge = HNS_ROCE_FREE_MR_USED_RQSGE_NUM;
2688 ret = free_mr_init_qp(hr_dev, cq, &qp_init_attr, i);
2690 goto create_failed_qp;
2696 hns_roce_destroy_cq(cq, NULL);
2700 hns_roce_dealloc_pd(pd, NULL);
2706 static int free_mr_modify_rsv_qp(struct hns_roce_dev *hr_dev,
2707 struct ib_qp_attr *attr, int sl_num)
2709 struct hns_roce_v2_priv *priv = hr_dev->priv;
2710 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2711 struct ib_device *ibdev = &hr_dev->ib_dev;
2712 struct hns_roce_qp *hr_qp;
2717 hr_qp = to_hr_qp(&free_mr->rsv_qp[sl_num]->ibqp);
2718 hr_qp->free_mr_en = 1;
2719 hr_qp->ibqp.device = ibdev;
2720 hr_qp->ibqp.qp_type = IB_QPT_RC;
2722 mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT | IB_QP_ACCESS_FLAGS;
2723 attr->qp_state = IB_QPS_INIT;
2725 attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE;
2726 ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, attr, mask, IB_QPS_INIT,
2729 ibdev_err(ibdev, "failed to modify qp to init, ret = %d.\n",
2734 loopback = hr_dev->loop_idc;
2735 /* Set qpc lbi = 1 incidate loopback IO */
2736 hr_dev->loop_idc = 1;
2738 mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | IB_QP_DEST_QPN |
2739 IB_QP_RQ_PSN | IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER;
2740 attr->qp_state = IB_QPS_RTR;
2741 attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
2742 attr->path_mtu = IB_MTU_256;
2743 attr->dest_qp_num = hr_qp->qpn;
2744 attr->rq_psn = HNS_ROCE_FREE_MR_USED_PSN;
2746 rdma_ah_set_sl(&attr->ah_attr, (u8)sl_num);
2748 ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, attr, mask, IB_QPS_INIT,
2750 hr_dev->loop_idc = loopback;
2752 ibdev_err(ibdev, "failed to modify qp to rtr, ret = %d.\n",
2757 mask = IB_QP_STATE | IB_QP_SQ_PSN | IB_QP_RETRY_CNT | IB_QP_TIMEOUT |
2758 IB_QP_RNR_RETRY | IB_QP_MAX_QP_RD_ATOMIC;
2759 attr->qp_state = IB_QPS_RTS;
2760 attr->sq_psn = HNS_ROCE_FREE_MR_USED_PSN;
2761 attr->retry_cnt = HNS_ROCE_FREE_MR_USED_QP_RETRY_CNT;
2762 attr->timeout = HNS_ROCE_FREE_MR_USED_QP_TIMEOUT;
2763 ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, attr, mask, IB_QPS_RTR,
2766 ibdev_err(ibdev, "failed to modify qp to rts, ret = %d.\n",
2772 static int free_mr_modify_qp(struct hns_roce_dev *hr_dev)
2774 struct hns_roce_v2_priv *priv = hr_dev->priv;
2775 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2776 struct ib_qp_attr attr = {};
2780 rdma_ah_set_grh(&attr.ah_attr, NULL, 0, 0, 1, 0);
2781 rdma_ah_set_static_rate(&attr.ah_attr, 3);
2782 rdma_ah_set_port_num(&attr.ah_attr, 1);
2784 for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
2785 ret = free_mr_modify_rsv_qp(hr_dev, &attr, i);
2793 static int free_mr_init(struct hns_roce_dev *hr_dev)
2795 struct hns_roce_v2_priv *priv = hr_dev->priv;
2796 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
2799 mutex_init(&free_mr->mutex);
2801 ret = free_mr_alloc_res(hr_dev);
2805 ret = free_mr_modify_qp(hr_dev);
2812 free_mr_exit(hr_dev);
2817 static int get_hem_table(struct hns_roce_dev *hr_dev)
2819 unsigned int qpc_count;
2820 unsigned int cqc_count;
2821 unsigned int gmv_count;
2825 /* Alloc memory for source address table buffer space chunk */
2826 for (gmv_count = 0; gmv_count < hr_dev->caps.gmv_entry_num;
2828 ret = hns_roce_table_get(hr_dev, &hr_dev->gmv_table, gmv_count);
2830 goto err_gmv_failed;
2836 /* Alloc memory for QPC Timer buffer space chunk */
2837 for (qpc_count = 0; qpc_count < hr_dev->caps.qpc_timer_bt_num;
2839 ret = hns_roce_table_get(hr_dev, &hr_dev->qpc_timer_table,
2842 dev_err(hr_dev->dev, "QPC Timer get failed\n");
2843 goto err_qpc_timer_failed;
2847 /* Alloc memory for CQC Timer buffer space chunk */
2848 for (cqc_count = 0; cqc_count < hr_dev->caps.cqc_timer_bt_num;
2850 ret = hns_roce_table_get(hr_dev, &hr_dev->cqc_timer_table,
2853 dev_err(hr_dev->dev, "CQC Timer get failed\n");
2854 goto err_cqc_timer_failed;
2860 err_cqc_timer_failed:
2861 for (i = 0; i < cqc_count; i++)
2862 hns_roce_table_put(hr_dev, &hr_dev->cqc_timer_table, i);
2864 err_qpc_timer_failed:
2865 for (i = 0; i < qpc_count; i++)
2866 hns_roce_table_put(hr_dev, &hr_dev->qpc_timer_table, i);
2869 for (i = 0; i < gmv_count; i++)
2870 hns_roce_table_put(hr_dev, &hr_dev->gmv_table, i);
2875 static void put_hem_table(struct hns_roce_dev *hr_dev)
2879 for (i = 0; i < hr_dev->caps.gmv_entry_num; i++)
2880 hns_roce_table_put(hr_dev, &hr_dev->gmv_table, i);
2885 for (i = 0; i < hr_dev->caps.qpc_timer_bt_num; i++)
2886 hns_roce_table_put(hr_dev, &hr_dev->qpc_timer_table, i);
2888 for (i = 0; i < hr_dev->caps.cqc_timer_bt_num; i++)
2889 hns_roce_table_put(hr_dev, &hr_dev->cqc_timer_table, i);
2892 static int hns_roce_v2_init(struct hns_roce_dev *hr_dev)
2896 /* The hns ROCEE requires the extdb info to be cleared before using */
2897 ret = hns_roce_clear_extdb_list_info(hr_dev);
2901 ret = get_hem_table(hr_dev);
2908 ret = hns_roce_init_link_table(hr_dev);
2910 dev_err(hr_dev->dev, "failed to init llm, ret = %d.\n", ret);
2911 goto err_llm_init_failed;
2916 err_llm_init_failed:
2917 put_hem_table(hr_dev);
2922 static void hns_roce_v2_exit(struct hns_roce_dev *hr_dev)
2924 hns_roce_function_clear(hr_dev);
2927 hns_roce_free_link_table(hr_dev);
2929 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP09)
2930 free_dip_list(hr_dev);
2933 static int hns_roce_mbox_post(struct hns_roce_dev *hr_dev,
2934 struct hns_roce_mbox_msg *mbox_msg)
2936 struct hns_roce_cmq_desc desc;
2937 struct hns_roce_post_mbox *mb = (struct hns_roce_post_mbox *)desc.data;
2939 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_POST_MB, false);
2941 mb->in_param_l = cpu_to_le32(mbox_msg->in_param);
2942 mb->in_param_h = cpu_to_le32(mbox_msg->in_param >> 32);
2943 mb->out_param_l = cpu_to_le32(mbox_msg->out_param);
2944 mb->out_param_h = cpu_to_le32(mbox_msg->out_param >> 32);
2945 mb->cmd_tag = cpu_to_le32(mbox_msg->tag << 8 | mbox_msg->cmd);
2946 mb->token_event_en = cpu_to_le32(mbox_msg->event_en << 16 |
2949 return hns_roce_cmq_send(hr_dev, &desc, 1);
2952 static int v2_wait_mbox_complete(struct hns_roce_dev *hr_dev, u32 timeout,
2953 u8 *complete_status)
2955 struct hns_roce_mbox_status *mb_st;
2956 struct hns_roce_cmq_desc desc;
2962 mb_st = (struct hns_roce_mbox_status *)desc.data;
2963 end = msecs_to_jiffies(timeout) + jiffies;
2964 while (v2_chk_mbox_is_avail(hr_dev, &busy)) {
2965 if (hr_dev->cmd.state == HNS_ROCE_CMDQ_STATE_FATAL_ERR)
2969 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_QUERY_MB_ST,
2971 ret = __hns_roce_cmq_send(hr_dev, &desc, 1);
2973 status = le32_to_cpu(mb_st->mb_status_hw_run);
2974 /* No pending message exists in ROCEE mbox. */
2975 if (!(status & MB_ST_HW_RUN_M))
2977 } else if (!v2_chk_mbox_is_avail(hr_dev, &busy)) {
2981 if (time_after(jiffies, end)) {
2982 dev_err_ratelimited(hr_dev->dev,
2983 "failed to wait mbox status 0x%x\n",
2993 *complete_status = (u8)(status & MB_ST_COMPLETE_M);
2994 } else if (!v2_chk_mbox_is_avail(hr_dev, &busy)) {
2995 /* Ignore all errors if the mbox is unavailable. */
2997 *complete_status = MB_ST_COMPLETE_M;
3003 static int v2_post_mbox(struct hns_roce_dev *hr_dev,
3004 struct hns_roce_mbox_msg *mbox_msg)
3009 /* Waiting for the mbox to be idle */
3010 ret = v2_wait_mbox_complete(hr_dev, HNS_ROCE_V2_GO_BIT_TIMEOUT_MSECS,
3012 if (unlikely(ret)) {
3013 dev_err_ratelimited(hr_dev->dev,
3014 "failed to check post mbox status = 0x%x, ret = %d.\n",
3019 /* Post new message to mbox */
3020 ret = hns_roce_mbox_post(hr_dev, mbox_msg);
3022 dev_err_ratelimited(hr_dev->dev,
3023 "failed to post mailbox, ret = %d.\n", ret);
3028 static int v2_poll_mbox_done(struct hns_roce_dev *hr_dev)
3033 ret = v2_wait_mbox_complete(hr_dev, HNS_ROCE_CMD_TIMEOUT_MSECS,
3036 if (status != MB_ST_COMPLETE_SUCC)
3039 dev_err_ratelimited(hr_dev->dev,
3040 "failed to check mbox status = 0x%x, ret = %d.\n",
3047 static void copy_gid(void *dest, const union ib_gid *gid)
3050 const union ib_gid *src = gid;
3051 __le32 (*p)[GID_SIZE] = dest;
3057 for (i = 0; i < GID_SIZE; i++)
3058 (*p)[i] = cpu_to_le32(*(u32 *)&src->raw[i * sizeof(u32)]);
3061 static int config_sgid_table(struct hns_roce_dev *hr_dev,
3062 int gid_index, const union ib_gid *gid,
3063 enum hns_roce_sgid_type sgid_type)
3065 struct hns_roce_cmq_desc desc;
3066 struct hns_roce_cfg_sgid_tb *sgid_tb =
3067 (struct hns_roce_cfg_sgid_tb *)desc.data;
3069 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_SGID_TB, false);
3071 hr_reg_write(sgid_tb, CFG_SGID_TB_TABLE_IDX, gid_index);
3072 hr_reg_write(sgid_tb, CFG_SGID_TB_VF_SGID_TYPE, sgid_type);
3074 copy_gid(&sgid_tb->vf_sgid_l, gid);
3076 return hns_roce_cmq_send(hr_dev, &desc, 1);
3079 static int config_gmv_table(struct hns_roce_dev *hr_dev,
3080 int gid_index, const union ib_gid *gid,
3081 enum hns_roce_sgid_type sgid_type,
3082 const struct ib_gid_attr *attr)
3084 struct hns_roce_cmq_desc desc[2];
3085 struct hns_roce_cfg_gmv_tb_a *tb_a =
3086 (struct hns_roce_cfg_gmv_tb_a *)desc[0].data;
3087 struct hns_roce_cfg_gmv_tb_b *tb_b =
3088 (struct hns_roce_cfg_gmv_tb_b *)desc[1].data;
3090 u16 vlan_id = VLAN_CFI_MASK;
3091 u8 mac[ETH_ALEN] = {};
3095 ret = rdma_read_gid_l2_fields(attr, &vlan_id, mac);
3100 hns_roce_cmq_setup_basic_desc(&desc[0], HNS_ROCE_OPC_CFG_GMV_TBL, false);
3101 desc[0].flag |= cpu_to_le16(HNS_ROCE_CMD_FLAG_NEXT);
3103 hns_roce_cmq_setup_basic_desc(&desc[1], HNS_ROCE_OPC_CFG_GMV_TBL, false);
3105 copy_gid(&tb_a->vf_sgid_l, gid);
3107 hr_reg_write(tb_a, GMV_TB_A_VF_SGID_TYPE, sgid_type);
3108 hr_reg_write(tb_a, GMV_TB_A_VF_VLAN_EN, vlan_id < VLAN_CFI_MASK);
3109 hr_reg_write(tb_a, GMV_TB_A_VF_VLAN_ID, vlan_id);
3111 tb_b->vf_smac_l = cpu_to_le32(*(u32 *)mac);
3113 hr_reg_write(tb_b, GMV_TB_B_SMAC_H, *(u16 *)&mac[4]);
3114 hr_reg_write(tb_b, GMV_TB_B_SGID_IDX, gid_index);
3116 return hns_roce_cmq_send(hr_dev, desc, 2);
3119 static int hns_roce_v2_set_gid(struct hns_roce_dev *hr_dev, int gid_index,
3120 const union ib_gid *gid,
3121 const struct ib_gid_attr *attr)
3123 enum hns_roce_sgid_type sgid_type = GID_TYPE_FLAG_ROCE_V1;
3127 if (attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
3128 if (ipv6_addr_v4mapped((void *)gid))
3129 sgid_type = GID_TYPE_FLAG_ROCE_V2_IPV4;
3131 sgid_type = GID_TYPE_FLAG_ROCE_V2_IPV6;
3132 } else if (attr->gid_type == IB_GID_TYPE_ROCE) {
3133 sgid_type = GID_TYPE_FLAG_ROCE_V1;
3137 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
3138 ret = config_gmv_table(hr_dev, gid_index, gid, sgid_type, attr);
3140 ret = config_sgid_table(hr_dev, gid_index, gid, sgid_type);
3143 ibdev_err(&hr_dev->ib_dev, "failed to set gid, ret = %d!\n",
3149 static int hns_roce_v2_set_mac(struct hns_roce_dev *hr_dev, u8 phy_port,
3152 struct hns_roce_cmq_desc desc;
3153 struct hns_roce_cfg_smac_tb *smac_tb =
3154 (struct hns_roce_cfg_smac_tb *)desc.data;
3158 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_SMAC_TB, false);
3160 reg_smac_l = *(u32 *)(&addr[0]);
3161 reg_smac_h = *(u16 *)(&addr[4]);
3163 hr_reg_write(smac_tb, CFG_SMAC_TB_IDX, phy_port);
3164 hr_reg_write(smac_tb, CFG_SMAC_TB_VF_SMAC_H, reg_smac_h);
3165 smac_tb->vf_smac_l = cpu_to_le32(reg_smac_l);
3167 return hns_roce_cmq_send(hr_dev, &desc, 1);
3170 static int set_mtpt_pbl(struct hns_roce_dev *hr_dev,
3171 struct hns_roce_v2_mpt_entry *mpt_entry,
3172 struct hns_roce_mr *mr)
3174 u64 pages[HNS_ROCE_V2_MAX_INNER_MTPT_NUM] = { 0 };
3175 struct ib_device *ibdev = &hr_dev->ib_dev;
3179 count = hns_roce_mtr_find(hr_dev, &mr->pbl_mtr, 0, pages,
3180 min_t(int, ARRAY_SIZE(pages), mr->npages),
3183 ibdev_err(ibdev, "failed to find PBL mtr, count = %d.\n",
3188 /* Aligned to the hardware address access unit */
3189 for (i = 0; i < count; i++)
3192 mpt_entry->pbl_size = cpu_to_le32(mr->npages);
3193 mpt_entry->pbl_ba_l = cpu_to_le32(pbl_ba >> 3);
3194 hr_reg_write(mpt_entry, MPT_PBL_BA_H, upper_32_bits(pbl_ba >> 3));
3196 mpt_entry->pa0_l = cpu_to_le32(lower_32_bits(pages[0]));
3197 hr_reg_write(mpt_entry, MPT_PA0_H, upper_32_bits(pages[0]));
3199 mpt_entry->pa1_l = cpu_to_le32(lower_32_bits(pages[1]));
3200 hr_reg_write(mpt_entry, MPT_PA1_H, upper_32_bits(pages[1]));
3201 hr_reg_write(mpt_entry, MPT_PBL_BUF_PG_SZ,
3202 to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.buf_pg_shift));
3207 static int hns_roce_v2_write_mtpt(struct hns_roce_dev *hr_dev,
3208 void *mb_buf, struct hns_roce_mr *mr)
3210 struct hns_roce_v2_mpt_entry *mpt_entry;
3213 memset(mpt_entry, 0, sizeof(*mpt_entry));
3215 hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_VALID);
3216 hr_reg_write(mpt_entry, MPT_PD, mr->pd);
3218 hr_reg_write_bool(mpt_entry, MPT_BIND_EN,
3219 mr->access & IB_ACCESS_MW_BIND);
3220 hr_reg_write_bool(mpt_entry, MPT_ATOMIC_EN,
3221 mr->access & IB_ACCESS_REMOTE_ATOMIC);
3222 hr_reg_write_bool(mpt_entry, MPT_RR_EN,
3223 mr->access & IB_ACCESS_REMOTE_READ);
3224 hr_reg_write_bool(mpt_entry, MPT_RW_EN,
3225 mr->access & IB_ACCESS_REMOTE_WRITE);
3226 hr_reg_write_bool(mpt_entry, MPT_LW_EN,
3227 mr->access & IB_ACCESS_LOCAL_WRITE);
3229 mpt_entry->len_l = cpu_to_le32(lower_32_bits(mr->size));
3230 mpt_entry->len_h = cpu_to_le32(upper_32_bits(mr->size));
3231 mpt_entry->lkey = cpu_to_le32(mr->key);
3232 mpt_entry->va_l = cpu_to_le32(lower_32_bits(mr->iova));
3233 mpt_entry->va_h = cpu_to_le32(upper_32_bits(mr->iova));
3235 if (mr->type != MR_TYPE_MR)
3236 hr_reg_enable(mpt_entry, MPT_PA);
3238 if (mr->type == MR_TYPE_DMA)
3241 if (mr->pbl_hop_num != HNS_ROCE_HOP_NUM_0)
3242 hr_reg_write(mpt_entry, MPT_PBL_HOP_NUM, mr->pbl_hop_num);
3244 hr_reg_write(mpt_entry, MPT_PBL_BA_PG_SZ,
3245 to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.ba_pg_shift));
3246 hr_reg_enable(mpt_entry, MPT_INNER_PA_VLD);
3248 return set_mtpt_pbl(hr_dev, mpt_entry, mr);
3251 static int hns_roce_v2_rereg_write_mtpt(struct hns_roce_dev *hr_dev,
3252 struct hns_roce_mr *mr, int flags,
3255 struct hns_roce_v2_mpt_entry *mpt_entry = mb_buf;
3256 u32 mr_access_flags = mr->access;
3259 hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_VALID);
3260 hr_reg_write(mpt_entry, MPT_PD, mr->pd);
3262 if (flags & IB_MR_REREG_ACCESS) {
3263 hr_reg_write(mpt_entry, MPT_BIND_EN,
3264 (mr_access_flags & IB_ACCESS_MW_BIND ? 1 : 0));
3265 hr_reg_write(mpt_entry, MPT_ATOMIC_EN,
3266 mr_access_flags & IB_ACCESS_REMOTE_ATOMIC ? 1 : 0);
3267 hr_reg_write(mpt_entry, MPT_RR_EN,
3268 mr_access_flags & IB_ACCESS_REMOTE_READ ? 1 : 0);
3269 hr_reg_write(mpt_entry, MPT_RW_EN,
3270 mr_access_flags & IB_ACCESS_REMOTE_WRITE ? 1 : 0);
3271 hr_reg_write(mpt_entry, MPT_LW_EN,
3272 mr_access_flags & IB_ACCESS_LOCAL_WRITE ? 1 : 0);
3275 if (flags & IB_MR_REREG_TRANS) {
3276 mpt_entry->va_l = cpu_to_le32(lower_32_bits(mr->iova));
3277 mpt_entry->va_h = cpu_to_le32(upper_32_bits(mr->iova));
3278 mpt_entry->len_l = cpu_to_le32(lower_32_bits(mr->size));
3279 mpt_entry->len_h = cpu_to_le32(upper_32_bits(mr->size));
3281 ret = set_mtpt_pbl(hr_dev, mpt_entry, mr);
3287 static int hns_roce_v2_frmr_write_mtpt(struct hns_roce_dev *hr_dev,
3288 void *mb_buf, struct hns_roce_mr *mr)
3290 struct ib_device *ibdev = &hr_dev->ib_dev;
3291 struct hns_roce_v2_mpt_entry *mpt_entry;
3292 dma_addr_t pbl_ba = 0;
3295 memset(mpt_entry, 0, sizeof(*mpt_entry));
3297 if (hns_roce_mtr_find(hr_dev, &mr->pbl_mtr, 0, NULL, 0, &pbl_ba) < 0) {
3298 ibdev_err(ibdev, "failed to find frmr mtr.\n");
3302 hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_FREE);
3303 hr_reg_write(mpt_entry, MPT_PD, mr->pd);
3305 hr_reg_enable(mpt_entry, MPT_RA_EN);
3306 hr_reg_enable(mpt_entry, MPT_R_INV_EN);
3308 hr_reg_enable(mpt_entry, MPT_FRE);
3309 hr_reg_clear(mpt_entry, MPT_MR_MW);
3310 hr_reg_enable(mpt_entry, MPT_BPD);
3311 hr_reg_clear(mpt_entry, MPT_PA);
3313 hr_reg_write(mpt_entry, MPT_PBL_HOP_NUM, 1);
3314 hr_reg_write(mpt_entry, MPT_PBL_BA_PG_SZ,
3315 to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.ba_pg_shift));
3316 hr_reg_write(mpt_entry, MPT_PBL_BUF_PG_SZ,
3317 to_hr_hw_page_shift(mr->pbl_mtr.hem_cfg.buf_pg_shift));
3319 mpt_entry->pbl_size = cpu_to_le32(mr->npages);
3321 mpt_entry->pbl_ba_l = cpu_to_le32(lower_32_bits(pbl_ba >> 3));
3322 hr_reg_write(mpt_entry, MPT_PBL_BA_H, upper_32_bits(pbl_ba >> 3));
3327 static int hns_roce_v2_mw_write_mtpt(void *mb_buf, struct hns_roce_mw *mw)
3329 struct hns_roce_v2_mpt_entry *mpt_entry;
3332 memset(mpt_entry, 0, sizeof(*mpt_entry));
3334 hr_reg_write(mpt_entry, MPT_ST, V2_MPT_ST_FREE);
3335 hr_reg_write(mpt_entry, MPT_PD, mw->pdn);
3337 hr_reg_enable(mpt_entry, MPT_R_INV_EN);
3338 hr_reg_enable(mpt_entry, MPT_LW_EN);
3340 hr_reg_enable(mpt_entry, MPT_MR_MW);
3341 hr_reg_enable(mpt_entry, MPT_BPD);
3342 hr_reg_clear(mpt_entry, MPT_PA);
3343 hr_reg_write(mpt_entry, MPT_BQP,
3344 mw->ibmw.type == IB_MW_TYPE_1 ? 0 : 1);
3346 mpt_entry->lkey = cpu_to_le32(mw->rkey);
3348 hr_reg_write(mpt_entry, MPT_PBL_HOP_NUM,
3349 mw->pbl_hop_num == HNS_ROCE_HOP_NUM_0 ? 0 :
3351 hr_reg_write(mpt_entry, MPT_PBL_BA_PG_SZ,
3352 mw->pbl_ba_pg_sz + PG_SHIFT_OFFSET);
3353 hr_reg_write(mpt_entry, MPT_PBL_BUF_PG_SZ,
3354 mw->pbl_buf_pg_sz + PG_SHIFT_OFFSET);
3359 static int free_mr_post_send_lp_wqe(struct hns_roce_qp *hr_qp)
3361 struct hns_roce_dev *hr_dev = to_hr_dev(hr_qp->ibqp.device);
3362 struct ib_device *ibdev = &hr_dev->ib_dev;
3363 const struct ib_send_wr *bad_wr;
3364 struct ib_rdma_wr rdma_wr = {};
3365 struct ib_send_wr *send_wr;
3368 send_wr = &rdma_wr.wr;
3369 send_wr->opcode = IB_WR_RDMA_WRITE;
3371 ret = hns_roce_v2_post_send(&hr_qp->ibqp, send_wr, &bad_wr);
3373 ibdev_err(ibdev, "failed to post wqe for free mr, ret = %d.\n",
3381 static int hns_roce_v2_poll_cq(struct ib_cq *ibcq, int num_entries,
3384 static void free_mr_send_cmd_to_hw(struct hns_roce_dev *hr_dev)
3386 struct hns_roce_v2_priv *priv = hr_dev->priv;
3387 struct hns_roce_v2_free_mr *free_mr = &priv->free_mr;
3388 struct ib_wc wc[ARRAY_SIZE(free_mr->rsv_qp)];
3389 struct ib_device *ibdev = &hr_dev->ib_dev;
3390 struct hns_roce_qp *hr_qp;
3398 * If the device initialization is not complete or in the uninstall
3399 * process, then there is no need to execute free mr.
3401 if (priv->handle->rinfo.reset_state == HNS_ROCE_STATE_RST_INIT ||
3402 priv->handle->rinfo.instance_state == HNS_ROCE_STATE_INIT ||
3403 hr_dev->state == HNS_ROCE_DEVICE_STATE_UNINIT)
3406 mutex_lock(&free_mr->mutex);
3408 for (i = 0; i < ARRAY_SIZE(free_mr->rsv_qp); i++) {
3409 hr_qp = free_mr->rsv_qp[i];
3411 ret = free_mr_post_send_lp_wqe(hr_qp);
3414 "failed to send wqe (qp:0x%lx) for free mr, ret = %d.\n",
3422 end = msecs_to_jiffies(HNS_ROCE_V2_FREE_MR_TIMEOUT) + jiffies;
3424 npolled = hns_roce_v2_poll_cq(&free_mr->rsv_cq->ib_cq, cqe_cnt, wc);
3427 "failed to poll cqe for free mr, remain %d cqe.\n",
3432 if (time_after(jiffies, end)) {
3434 "failed to poll cqe for free mr and timeout, remain %d cqe.\n",
3442 mutex_unlock(&free_mr->mutex);
3445 static void hns_roce_v2_dereg_mr(struct hns_roce_dev *hr_dev)
3447 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
3448 free_mr_send_cmd_to_hw(hr_dev);
3451 static void *get_cqe_v2(struct hns_roce_cq *hr_cq, int n)
3453 return hns_roce_buf_offset(hr_cq->mtr.kmem, n * hr_cq->cqe_size);
3456 static void *get_sw_cqe_v2(struct hns_roce_cq *hr_cq, unsigned int n)
3458 struct hns_roce_v2_cqe *cqe = get_cqe_v2(hr_cq, n & hr_cq->ib_cq.cqe);
3460 /* Get cqe when Owner bit is Conversely with the MSB of cons_idx */
3461 return (hr_reg_read(cqe, CQE_OWNER) ^ !!(n & hr_cq->cq_depth)) ? cqe :
3465 static inline void update_cq_db(struct hns_roce_dev *hr_dev,
3466 struct hns_roce_cq *hr_cq)
3468 if (likely(hr_cq->flags & HNS_ROCE_CQ_FLAG_RECORD_DB)) {
3469 *hr_cq->set_ci_db = hr_cq->cons_index & V2_CQ_DB_CONS_IDX_M;
3471 struct hns_roce_v2_db cq_db = {};
3473 hr_reg_write(&cq_db, DB_TAG, hr_cq->cqn);
3474 hr_reg_write(&cq_db, DB_CMD, HNS_ROCE_V2_CQ_DB);
3475 hr_reg_write(&cq_db, DB_CQ_CI, hr_cq->cons_index);
3476 hr_reg_write(&cq_db, DB_CQ_CMD_SN, 1);
3478 hns_roce_write64(hr_dev, (__le32 *)&cq_db, hr_cq->db_reg);
3482 static void __hns_roce_v2_cq_clean(struct hns_roce_cq *hr_cq, u32 qpn,
3483 struct hns_roce_srq *srq)
3485 struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
3486 struct hns_roce_v2_cqe *cqe, *dest;
3492 for (prod_index = hr_cq->cons_index; get_sw_cqe_v2(hr_cq, prod_index);
3494 if (prod_index > hr_cq->cons_index + hr_cq->ib_cq.cqe)
3499 * Now backwards through the CQ, removing CQ entries
3500 * that match our QP by overwriting them with next entries.
3502 while ((int) --prod_index - (int) hr_cq->cons_index >= 0) {
3503 cqe = get_cqe_v2(hr_cq, prod_index & hr_cq->ib_cq.cqe);
3504 if (hr_reg_read(cqe, CQE_LCL_QPN) == qpn) {
3505 if (srq && hr_reg_read(cqe, CQE_S_R)) {
3506 wqe_index = hr_reg_read(cqe, CQE_WQE_IDX);
3507 hns_roce_free_srq_wqe(srq, wqe_index);
3510 } else if (nfreed) {
3511 dest = get_cqe_v2(hr_cq, (prod_index + nfreed) &
3513 owner_bit = hr_reg_read(dest, CQE_OWNER);
3514 memcpy(dest, cqe, hr_cq->cqe_size);
3515 hr_reg_write(dest, CQE_OWNER, owner_bit);
3520 hr_cq->cons_index += nfreed;
3521 update_cq_db(hr_dev, hr_cq);
3525 static void hns_roce_v2_cq_clean(struct hns_roce_cq *hr_cq, u32 qpn,
3526 struct hns_roce_srq *srq)
3528 spin_lock_irq(&hr_cq->lock);
3529 __hns_roce_v2_cq_clean(hr_cq, qpn, srq);
3530 spin_unlock_irq(&hr_cq->lock);
3533 static void hns_roce_v2_write_cqc(struct hns_roce_dev *hr_dev,
3534 struct hns_roce_cq *hr_cq, void *mb_buf,
3535 u64 *mtts, dma_addr_t dma_handle)
3537 struct hns_roce_v2_cq_context *cq_context;
3539 cq_context = mb_buf;
3540 memset(cq_context, 0, sizeof(*cq_context));
3542 hr_reg_write(cq_context, CQC_CQ_ST, V2_CQ_STATE_VALID);
3543 hr_reg_write(cq_context, CQC_ARM_ST, NO_ARMED);
3544 hr_reg_write(cq_context, CQC_SHIFT, ilog2(hr_cq->cq_depth));
3545 hr_reg_write(cq_context, CQC_CEQN, hr_cq->vector);
3546 hr_reg_write(cq_context, CQC_CQN, hr_cq->cqn);
3548 if (hr_cq->cqe_size == HNS_ROCE_V3_CQE_SIZE)
3549 hr_reg_write(cq_context, CQC_CQE_SIZE, CQE_SIZE_64B);
3551 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_STASH)
3552 hr_reg_enable(cq_context, CQC_STASH);
3554 hr_reg_write(cq_context, CQC_CQE_CUR_BLK_ADDR_L,
3555 to_hr_hw_page_addr(mtts[0]));
3556 hr_reg_write(cq_context, CQC_CQE_CUR_BLK_ADDR_H,
3557 upper_32_bits(to_hr_hw_page_addr(mtts[0])));
3558 hr_reg_write(cq_context, CQC_CQE_HOP_NUM, hr_dev->caps.cqe_hop_num ==
3559 HNS_ROCE_HOP_NUM_0 ? 0 : hr_dev->caps.cqe_hop_num);
3560 hr_reg_write(cq_context, CQC_CQE_NEX_BLK_ADDR_L,
3561 to_hr_hw_page_addr(mtts[1]));
3562 hr_reg_write(cq_context, CQC_CQE_NEX_BLK_ADDR_H,
3563 upper_32_bits(to_hr_hw_page_addr(mtts[1])));
3564 hr_reg_write(cq_context, CQC_CQE_BAR_PG_SZ,
3565 to_hr_hw_page_shift(hr_cq->mtr.hem_cfg.ba_pg_shift));
3566 hr_reg_write(cq_context, CQC_CQE_BUF_PG_SZ,
3567 to_hr_hw_page_shift(hr_cq->mtr.hem_cfg.buf_pg_shift));
3568 hr_reg_write(cq_context, CQC_CQE_BA_L, dma_handle >> 3);
3569 hr_reg_write(cq_context, CQC_CQE_BA_H, (dma_handle >> (32 + 3)));
3570 hr_reg_write_bool(cq_context, CQC_DB_RECORD_EN,
3571 hr_cq->flags & HNS_ROCE_CQ_FLAG_RECORD_DB);
3572 hr_reg_write(cq_context, CQC_CQE_DB_RECORD_ADDR_L,
3573 ((u32)hr_cq->db.dma) >> 1);
3574 hr_reg_write(cq_context, CQC_CQE_DB_RECORD_ADDR_H,
3575 hr_cq->db.dma >> 32);
3576 hr_reg_write(cq_context, CQC_CQ_MAX_CNT,
3577 HNS_ROCE_V2_CQ_DEFAULT_BURST_NUM);
3578 hr_reg_write(cq_context, CQC_CQ_PERIOD,
3579 HNS_ROCE_V2_CQ_DEFAULT_INTERVAL);
3582 static int hns_roce_v2_req_notify_cq(struct ib_cq *ibcq,
3583 enum ib_cq_notify_flags flags)
3585 struct hns_roce_dev *hr_dev = to_hr_dev(ibcq->device);
3586 struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
3587 struct hns_roce_v2_db cq_db = {};
3591 * flags = 0, then notify_flag : next
3592 * flags = 1, then notify flag : solocited
3594 notify_flag = (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
3595 V2_CQ_DB_REQ_NOT : V2_CQ_DB_REQ_NOT_SOL;
3597 hr_reg_write(&cq_db, DB_TAG, hr_cq->cqn);
3598 hr_reg_write(&cq_db, DB_CMD, HNS_ROCE_V2_CQ_DB_NOTIFY);
3599 hr_reg_write(&cq_db, DB_CQ_CI, hr_cq->cons_index);
3600 hr_reg_write(&cq_db, DB_CQ_CMD_SN, hr_cq->arm_sn);
3601 hr_reg_write(&cq_db, DB_CQ_NOTIFY, notify_flag);
3603 hns_roce_write64(hr_dev, (__le32 *)&cq_db, hr_cq->db_reg);
3608 static int sw_comp(struct hns_roce_qp *hr_qp, struct hns_roce_wq *wq,
3609 int num_entries, struct ib_wc *wc)
3614 left = wq->head - wq->tail;
3618 left = min_t(unsigned int, (unsigned int)num_entries, left);
3619 while (npolled < left) {
3620 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
3621 wc->status = IB_WC_WR_FLUSH_ERR;
3623 wc->qp = &hr_qp->ibqp;
3633 static int hns_roce_v2_sw_poll_cq(struct hns_roce_cq *hr_cq, int num_entries,
3636 struct hns_roce_qp *hr_qp;
3639 list_for_each_entry(hr_qp, &hr_cq->sq_list, sq_node) {
3640 npolled += sw_comp(hr_qp, &hr_qp->sq,
3641 num_entries - npolled, wc + npolled);
3642 if (npolled >= num_entries)
3646 list_for_each_entry(hr_qp, &hr_cq->rq_list, rq_node) {
3647 npolled += sw_comp(hr_qp, &hr_qp->rq,
3648 num_entries - npolled, wc + npolled);
3649 if (npolled >= num_entries)
3657 static void get_cqe_status(struct hns_roce_dev *hr_dev, struct hns_roce_qp *qp,
3658 struct hns_roce_cq *cq, struct hns_roce_v2_cqe *cqe,
3661 static const struct {
3663 enum ib_wc_status wc_status;
3665 { HNS_ROCE_CQE_V2_SUCCESS, IB_WC_SUCCESS },
3666 { HNS_ROCE_CQE_V2_LOCAL_LENGTH_ERR, IB_WC_LOC_LEN_ERR },
3667 { HNS_ROCE_CQE_V2_LOCAL_QP_OP_ERR, IB_WC_LOC_QP_OP_ERR },
3668 { HNS_ROCE_CQE_V2_LOCAL_PROT_ERR, IB_WC_LOC_PROT_ERR },
3669 { HNS_ROCE_CQE_V2_WR_FLUSH_ERR, IB_WC_WR_FLUSH_ERR },
3670 { HNS_ROCE_CQE_V2_MW_BIND_ERR, IB_WC_MW_BIND_ERR },
3671 { HNS_ROCE_CQE_V2_BAD_RESP_ERR, IB_WC_BAD_RESP_ERR },
3672 { HNS_ROCE_CQE_V2_LOCAL_ACCESS_ERR, IB_WC_LOC_ACCESS_ERR },
3673 { HNS_ROCE_CQE_V2_REMOTE_INVAL_REQ_ERR, IB_WC_REM_INV_REQ_ERR },
3674 { HNS_ROCE_CQE_V2_REMOTE_ACCESS_ERR, IB_WC_REM_ACCESS_ERR },
3675 { HNS_ROCE_CQE_V2_REMOTE_OP_ERR, IB_WC_REM_OP_ERR },
3676 { HNS_ROCE_CQE_V2_TRANSPORT_RETRY_EXC_ERR,
3677 IB_WC_RETRY_EXC_ERR },
3678 { HNS_ROCE_CQE_V2_RNR_RETRY_EXC_ERR, IB_WC_RNR_RETRY_EXC_ERR },
3679 { HNS_ROCE_CQE_V2_REMOTE_ABORT_ERR, IB_WC_REM_ABORT_ERR },
3680 { HNS_ROCE_CQE_V2_GENERAL_ERR, IB_WC_GENERAL_ERR}
3683 u32 cqe_status = hr_reg_read(cqe, CQE_STATUS);
3686 wc->status = IB_WC_GENERAL_ERR;
3687 for (i = 0; i < ARRAY_SIZE(map); i++)
3688 if (cqe_status == map[i].cqe_status) {
3689 wc->status = map[i].wc_status;
3693 if (likely(wc->status == IB_WC_SUCCESS ||
3694 wc->status == IB_WC_WR_FLUSH_ERR))
3697 ibdev_err(&hr_dev->ib_dev, "error cqe status 0x%x:\n", cqe_status);
3698 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_NONE, 16, 4, cqe,
3699 cq->cqe_size, false);
3700 wc->vendor_err = hr_reg_read(cqe, CQE_SUB_STATUS);
3703 * For hns ROCEE, GENERAL_ERR is an error type that is not defined in
3704 * the standard protocol, the driver must ignore it and needn't to set
3705 * the QP to an error state.
3707 if (cqe_status == HNS_ROCE_CQE_V2_GENERAL_ERR)
3710 flush_cqe(hr_dev, qp);
3713 static int get_cur_qp(struct hns_roce_cq *hr_cq, struct hns_roce_v2_cqe *cqe,
3714 struct hns_roce_qp **cur_qp)
3716 struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
3717 struct hns_roce_qp *hr_qp = *cur_qp;
3720 qpn = hr_reg_read(cqe, CQE_LCL_QPN);
3722 if (!hr_qp || qpn != hr_qp->qpn) {
3723 hr_qp = __hns_roce_qp_lookup(hr_dev, qpn);
3724 if (unlikely(!hr_qp)) {
3725 ibdev_err(&hr_dev->ib_dev,
3726 "CQ %06lx with entry for unknown QPN %06x\n",
3737 * mapped-value = 1 + real-value
3738 * The ib wc opcode's real value is start from 0, In order to distinguish
3739 * between initialized and uninitialized map values, we plus 1 to the actual
3740 * value when defining the mapping, so that the validity can be identified by
3741 * checking whether the mapped value is greater than 0.
3743 #define HR_WC_OP_MAP(hr_key, ib_key) \
3744 [HNS_ROCE_V2_WQE_OP_ ## hr_key] = 1 + IB_WC_ ## ib_key
3746 static const u32 wc_send_op_map[] = {
3747 HR_WC_OP_MAP(SEND, SEND),
3748 HR_WC_OP_MAP(SEND_WITH_INV, SEND),
3749 HR_WC_OP_MAP(SEND_WITH_IMM, SEND),
3750 HR_WC_OP_MAP(RDMA_READ, RDMA_READ),
3751 HR_WC_OP_MAP(RDMA_WRITE, RDMA_WRITE),
3752 HR_WC_OP_MAP(RDMA_WRITE_WITH_IMM, RDMA_WRITE),
3753 HR_WC_OP_MAP(ATOM_CMP_AND_SWAP, COMP_SWAP),
3754 HR_WC_OP_MAP(ATOM_FETCH_AND_ADD, FETCH_ADD),
3755 HR_WC_OP_MAP(ATOM_MSK_CMP_AND_SWAP, MASKED_COMP_SWAP),
3756 HR_WC_OP_MAP(ATOM_MSK_FETCH_AND_ADD, MASKED_FETCH_ADD),
3757 HR_WC_OP_MAP(FAST_REG_PMR, REG_MR),
3758 HR_WC_OP_MAP(BIND_MW, REG_MR),
3761 static int to_ib_wc_send_op(u32 hr_opcode)
3763 if (hr_opcode >= ARRAY_SIZE(wc_send_op_map))
3766 return wc_send_op_map[hr_opcode] ? wc_send_op_map[hr_opcode] - 1 :
3770 static const u32 wc_recv_op_map[] = {
3771 HR_WC_OP_MAP(RDMA_WRITE_WITH_IMM, WITH_IMM),
3772 HR_WC_OP_MAP(SEND, RECV),
3773 HR_WC_OP_MAP(SEND_WITH_IMM, WITH_IMM),
3774 HR_WC_OP_MAP(SEND_WITH_INV, RECV),
3777 static int to_ib_wc_recv_op(u32 hr_opcode)
3779 if (hr_opcode >= ARRAY_SIZE(wc_recv_op_map))
3782 return wc_recv_op_map[hr_opcode] ? wc_recv_op_map[hr_opcode] - 1 :
3786 static void fill_send_wc(struct ib_wc *wc, struct hns_roce_v2_cqe *cqe)
3793 hr_opcode = hr_reg_read(cqe, CQE_OPCODE);
3794 switch (hr_opcode) {
3795 case HNS_ROCE_V2_WQE_OP_RDMA_READ:
3796 wc->byte_len = le32_to_cpu(cqe->byte_cnt);
3798 case HNS_ROCE_V2_WQE_OP_SEND_WITH_IMM:
3799 case HNS_ROCE_V2_WQE_OP_RDMA_WRITE_WITH_IMM:
3800 wc->wc_flags |= IB_WC_WITH_IMM;
3802 case HNS_ROCE_V2_WQE_OP_ATOM_CMP_AND_SWAP:
3803 case HNS_ROCE_V2_WQE_OP_ATOM_FETCH_AND_ADD:
3804 case HNS_ROCE_V2_WQE_OP_ATOM_MSK_CMP_AND_SWAP:
3805 case HNS_ROCE_V2_WQE_OP_ATOM_MSK_FETCH_AND_ADD:
3812 ib_opcode = to_ib_wc_send_op(hr_opcode);
3814 wc->status = IB_WC_GENERAL_ERR;
3816 wc->opcode = ib_opcode;
3819 static int fill_recv_wc(struct ib_wc *wc, struct hns_roce_v2_cqe *cqe)
3824 wc->byte_len = le32_to_cpu(cqe->byte_cnt);
3826 hr_opcode = hr_reg_read(cqe, CQE_OPCODE);
3827 switch (hr_opcode) {
3828 case HNS_ROCE_V2_OPCODE_RDMA_WRITE_IMM:
3829 case HNS_ROCE_V2_OPCODE_SEND_WITH_IMM:
3830 wc->wc_flags = IB_WC_WITH_IMM;
3831 wc->ex.imm_data = cpu_to_be32(le32_to_cpu(cqe->immtdata));
3833 case HNS_ROCE_V2_OPCODE_SEND_WITH_INV:
3834 wc->wc_flags = IB_WC_WITH_INVALIDATE;
3835 wc->ex.invalidate_rkey = le32_to_cpu(cqe->rkey);
3841 ib_opcode = to_ib_wc_recv_op(hr_opcode);
3843 wc->status = IB_WC_GENERAL_ERR;
3845 wc->opcode = ib_opcode;
3847 wc->sl = hr_reg_read(cqe, CQE_SL);
3848 wc->src_qp = hr_reg_read(cqe, CQE_RMT_QPN);
3850 wc->wc_flags |= hr_reg_read(cqe, CQE_GRH) ? IB_WC_GRH : 0;
3851 wc->port_num = hr_reg_read(cqe, CQE_PORTN);
3854 if (hr_reg_read(cqe, CQE_VID_VLD)) {
3855 wc->vlan_id = hr_reg_read(cqe, CQE_VID);
3856 wc->wc_flags |= IB_WC_WITH_VLAN;
3858 wc->vlan_id = 0xffff;
3861 wc->network_hdr_type = hr_reg_read(cqe, CQE_PORT_TYPE);
3866 static int hns_roce_v2_poll_one(struct hns_roce_cq *hr_cq,
3867 struct hns_roce_qp **cur_qp, struct ib_wc *wc)
3869 struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
3870 struct hns_roce_qp *qp = *cur_qp;
3871 struct hns_roce_srq *srq = NULL;
3872 struct hns_roce_v2_cqe *cqe;
3873 struct hns_roce_wq *wq;
3878 cqe = get_sw_cqe_v2(hr_cq, hr_cq->cons_index);
3882 ++hr_cq->cons_index;
3883 /* Memory barrier */
3886 ret = get_cur_qp(hr_cq, cqe, &qp);
3893 wqe_idx = hr_reg_read(cqe, CQE_WQE_IDX);
3895 is_send = !hr_reg_read(cqe, CQE_S_R);
3899 /* If sg_signal_bit is set, tail pointer will be updated to
3900 * the WQE corresponding to the current CQE.
3902 if (qp->sq_signal_bits)
3903 wq->tail += (wqe_idx - (u16)wq->tail) &
3906 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
3909 fill_send_wc(wc, cqe);
3912 srq = to_hr_srq(qp->ibqp.srq);
3913 wc->wr_id = srq->wrid[wqe_idx];
3914 hns_roce_free_srq_wqe(srq, wqe_idx);
3917 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
3921 ret = fill_recv_wc(wc, cqe);
3924 get_cqe_status(hr_dev, qp, hr_cq, cqe, wc);
3925 if (unlikely(wc->status != IB_WC_SUCCESS))
3931 static int hns_roce_v2_poll_cq(struct ib_cq *ibcq, int num_entries,
3934 struct hns_roce_dev *hr_dev = to_hr_dev(ibcq->device);
3935 struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
3936 struct hns_roce_qp *cur_qp = NULL;
3937 unsigned long flags;
3940 spin_lock_irqsave(&hr_cq->lock, flags);
3943 * When the device starts to reset, the state is RST_DOWN. At this time,
3944 * there may still be some valid CQEs in the hardware that are not
3945 * polled. Therefore, it is not allowed to switch to the software mode
3946 * immediately. When the state changes to UNINIT, CQE no longer exists
3947 * in the hardware, and then switch to software mode.
3949 if (hr_dev->state == HNS_ROCE_DEVICE_STATE_UNINIT) {
3950 npolled = hns_roce_v2_sw_poll_cq(hr_cq, num_entries, wc);
3954 for (npolled = 0; npolled < num_entries; ++npolled) {
3955 if (hns_roce_v2_poll_one(hr_cq, &cur_qp, wc + npolled))
3960 update_cq_db(hr_dev, hr_cq);
3963 spin_unlock_irqrestore(&hr_cq->lock, flags);
3968 static int get_op_for_set_hem(struct hns_roce_dev *hr_dev, u32 type,
3969 u32 step_idx, u8 *mbox_cmd)
3975 cmd = HNS_ROCE_CMD_WRITE_QPC_BT0;
3978 cmd = HNS_ROCE_CMD_WRITE_MPT_BT0;
3981 cmd = HNS_ROCE_CMD_WRITE_CQC_BT0;
3984 cmd = HNS_ROCE_CMD_WRITE_SRQC_BT0;
3987 cmd = HNS_ROCE_CMD_WRITE_SCCC_BT0;
3989 case HEM_TYPE_QPC_TIMER:
3990 cmd = HNS_ROCE_CMD_WRITE_QPC_TIMER_BT0;
3992 case HEM_TYPE_CQC_TIMER:
3993 cmd = HNS_ROCE_CMD_WRITE_CQC_TIMER_BT0;
3996 dev_warn(hr_dev->dev, "failed to check hem type %u.\n", type);
4000 *mbox_cmd = cmd + step_idx;
4005 static int config_gmv_ba_to_hw(struct hns_roce_dev *hr_dev, unsigned long obj,
4006 dma_addr_t base_addr)
4008 struct hns_roce_cmq_desc desc;
4009 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
4010 u32 idx = obj / (HNS_HW_PAGE_SIZE / hr_dev->caps.gmv_entry_sz);
4011 u64 addr = to_hr_hw_page_addr(base_addr);
4013 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GMV_BT, false);
4015 hr_reg_write(req, CFG_GMV_BT_BA_L, lower_32_bits(addr));
4016 hr_reg_write(req, CFG_GMV_BT_BA_H, upper_32_bits(addr));
4017 hr_reg_write(req, CFG_GMV_BT_IDX, idx);
4019 return hns_roce_cmq_send(hr_dev, &desc, 1);
4022 static int set_hem_to_hw(struct hns_roce_dev *hr_dev, int obj,
4023 dma_addr_t base_addr, u32 hem_type, u32 step_idx)
4028 if (unlikely(hem_type == HEM_TYPE_GMV))
4029 return config_gmv_ba_to_hw(hr_dev, obj, base_addr);
4031 if (unlikely(hem_type == HEM_TYPE_SCCC && step_idx))
4034 ret = get_op_for_set_hem(hr_dev, hem_type, step_idx, &cmd);
4038 return config_hem_ba_to_hw(hr_dev, base_addr, cmd, obj);
4041 static int hns_roce_v2_set_hem(struct hns_roce_dev *hr_dev,
4042 struct hns_roce_hem_table *table, int obj,
4045 struct hns_roce_hem_iter iter;
4046 struct hns_roce_hem_mhop mhop;
4047 struct hns_roce_hem *hem;
4048 unsigned long mhop_obj = obj;
4057 if (!hns_roce_check_whether_mhop(hr_dev, table->type))
4060 hns_roce_calc_hem_mhop(hr_dev, table, &mhop_obj, &mhop);
4064 hop_num = mhop.hop_num;
4065 chunk_ba_num = mhop.bt_chunk_size / 8;
4068 hem_idx = i * chunk_ba_num * chunk_ba_num + j * chunk_ba_num +
4070 l1_idx = i * chunk_ba_num + j;
4071 } else if (hop_num == 1) {
4072 hem_idx = i * chunk_ba_num + j;
4073 } else if (hop_num == HNS_ROCE_HOP_NUM_0) {
4077 if (table->type == HEM_TYPE_SCCC)
4080 if (check_whether_last_step(hop_num, step_idx)) {
4081 hem = table->hem[hem_idx];
4082 for (hns_roce_hem_first(hem, &iter);
4083 !hns_roce_hem_last(&iter); hns_roce_hem_next(&iter)) {
4084 bt_ba = hns_roce_hem_addr(&iter);
4085 ret = set_hem_to_hw(hr_dev, obj, bt_ba, table->type,
4090 bt_ba = table->bt_l0_dma_addr[i];
4091 else if (step_idx == 1 && hop_num == 2)
4092 bt_ba = table->bt_l1_dma_addr[l1_idx];
4094 ret = set_hem_to_hw(hr_dev, obj, bt_ba, table->type, step_idx);
4100 static int hns_roce_v2_clear_hem(struct hns_roce_dev *hr_dev,
4101 struct hns_roce_hem_table *table,
4102 int tag, u32 step_idx)
4104 struct hns_roce_cmd_mailbox *mailbox;
4105 struct device *dev = hr_dev->dev;
4109 if (!hns_roce_check_whether_mhop(hr_dev, table->type))
4112 switch (table->type) {
4114 cmd = HNS_ROCE_CMD_DESTROY_QPC_BT0;
4117 cmd = HNS_ROCE_CMD_DESTROY_MPT_BT0;
4120 cmd = HNS_ROCE_CMD_DESTROY_CQC_BT0;
4123 cmd = HNS_ROCE_CMD_DESTROY_SRQC_BT0;
4126 case HEM_TYPE_QPC_TIMER:
4127 case HEM_TYPE_CQC_TIMER:
4131 dev_warn(dev, "table %u not to be destroyed by mailbox!\n",
4138 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
4139 if (IS_ERR(mailbox))
4140 return PTR_ERR(mailbox);
4142 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, cmd, tag);
4144 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
4148 static int hns_roce_v2_qp_modify(struct hns_roce_dev *hr_dev,
4149 struct hns_roce_v2_qp_context *context,
4150 struct hns_roce_v2_qp_context *qpc_mask,
4151 struct hns_roce_qp *hr_qp)
4153 struct hns_roce_cmd_mailbox *mailbox;
4157 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
4158 if (IS_ERR(mailbox))
4159 return PTR_ERR(mailbox);
4161 /* The qpc size of HIP08 is only 256B, which is half of HIP09 */
4162 qpc_size = hr_dev->caps.qpc_sz;
4163 memcpy(mailbox->buf, context, qpc_size);
4164 memcpy(mailbox->buf + qpc_size, qpc_mask, qpc_size);
4166 ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0,
4167 HNS_ROCE_CMD_MODIFY_QPC, hr_qp->qpn);
4169 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
4174 static void set_access_flags(struct hns_roce_qp *hr_qp,
4175 struct hns_roce_v2_qp_context *context,
4176 struct hns_roce_v2_qp_context *qpc_mask,
4177 const struct ib_qp_attr *attr, int attr_mask)
4182 dest_rd_atomic = (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) ?
4183 attr->max_dest_rd_atomic : hr_qp->resp_depth;
4185 access_flags = (attr_mask & IB_QP_ACCESS_FLAGS) ?
4186 attr->qp_access_flags : hr_qp->atomic_rd_en;
4188 if (!dest_rd_atomic)
4189 access_flags &= IB_ACCESS_REMOTE_WRITE;
4191 hr_reg_write_bool(context, QPC_RRE,
4192 access_flags & IB_ACCESS_REMOTE_READ);
4193 hr_reg_clear(qpc_mask, QPC_RRE);
4195 hr_reg_write_bool(context, QPC_RWE,
4196 access_flags & IB_ACCESS_REMOTE_WRITE);
4197 hr_reg_clear(qpc_mask, QPC_RWE);
4199 hr_reg_write_bool(context, QPC_ATE,
4200 access_flags & IB_ACCESS_REMOTE_ATOMIC);
4201 hr_reg_clear(qpc_mask, QPC_ATE);
4202 hr_reg_write_bool(context, QPC_EXT_ATE,
4203 access_flags & IB_ACCESS_REMOTE_ATOMIC);
4204 hr_reg_clear(qpc_mask, QPC_EXT_ATE);
4207 static void set_qpc_wqe_cnt(struct hns_roce_qp *hr_qp,
4208 struct hns_roce_v2_qp_context *context,
4209 struct hns_roce_v2_qp_context *qpc_mask)
4211 hr_reg_write(context, QPC_SGE_SHIFT,
4212 to_hr_hem_entries_shift(hr_qp->sge.sge_cnt,
4213 hr_qp->sge.sge_shift));
4215 hr_reg_write(context, QPC_SQ_SHIFT, ilog2(hr_qp->sq.wqe_cnt));
4217 hr_reg_write(context, QPC_RQ_SHIFT, ilog2(hr_qp->rq.wqe_cnt));
4220 static inline int get_cqn(struct ib_cq *ib_cq)
4222 return ib_cq ? to_hr_cq(ib_cq)->cqn : 0;
4225 static inline int get_pdn(struct ib_pd *ib_pd)
4227 return ib_pd ? to_hr_pd(ib_pd)->pdn : 0;
4230 static void modify_qp_reset_to_init(struct ib_qp *ibqp,
4231 const struct ib_qp_attr *attr,
4232 struct hns_roce_v2_qp_context *context,
4233 struct hns_roce_v2_qp_context *qpc_mask)
4235 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4236 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4239 * In v2 engine, software pass context and context mask to hardware
4240 * when modifying qp. If software need modify some fields in context,
4241 * we should set all bits of the relevant fields in context mask to
4242 * 0 at the same time, else set them to 0x1.
4244 hr_reg_write(context, QPC_TST, to_hr_qp_type(ibqp->qp_type));
4246 hr_reg_write(context, QPC_PD, get_pdn(ibqp->pd));
4248 hr_reg_write(context, QPC_RQWS, ilog2(hr_qp->rq.max_gs));
4250 set_qpc_wqe_cnt(hr_qp, context, qpc_mask);
4252 /* No VLAN need to set 0xFFF */
4253 hr_reg_write(context, QPC_VLAN_ID, 0xfff);
4255 if (ibqp->qp_type == IB_QPT_XRC_TGT) {
4256 context->qkey_xrcd = cpu_to_le32(hr_qp->xrcdn);
4258 hr_reg_enable(context, QPC_XRC_QP_TYPE);
4261 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
4262 hr_reg_enable(context, QPC_RQ_RECORD_EN);
4264 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_OWNER_DB)
4265 hr_reg_enable(context, QPC_OWNER_MODE);
4267 hr_reg_write(context, QPC_RQ_DB_RECORD_ADDR_L,
4268 lower_32_bits(hr_qp->rdb.dma) >> 1);
4269 hr_reg_write(context, QPC_RQ_DB_RECORD_ADDR_H,
4270 upper_32_bits(hr_qp->rdb.dma));
4272 hr_reg_write(context, QPC_RX_CQN, get_cqn(ibqp->recv_cq));
4275 hr_reg_enable(context, QPC_SRQ_EN);
4276 hr_reg_write(context, QPC_SRQN, to_hr_srq(ibqp->srq)->srqn);
4279 hr_reg_enable(context, QPC_FRE);
4281 hr_reg_write(context, QPC_TX_CQN, get_cqn(ibqp->send_cq));
4283 if (hr_dev->caps.qpc_sz < HNS_ROCE_V3_QPC_SZ)
4286 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_STASH)
4287 hr_reg_enable(&context->ext, QPCEX_STASH);
4290 static void modify_qp_init_to_init(struct ib_qp *ibqp,
4291 const struct ib_qp_attr *attr,
4292 struct hns_roce_v2_qp_context *context,
4293 struct hns_roce_v2_qp_context *qpc_mask)
4296 * In v2 engine, software pass context and context mask to hardware
4297 * when modifying qp. If software need modify some fields in context,
4298 * we should set all bits of the relevant fields in context mask to
4299 * 0 at the same time, else set them to 0x1.
4301 hr_reg_write(context, QPC_TST, to_hr_qp_type(ibqp->qp_type));
4302 hr_reg_clear(qpc_mask, QPC_TST);
4304 hr_reg_write(context, QPC_PD, get_pdn(ibqp->pd));
4305 hr_reg_clear(qpc_mask, QPC_PD);
4307 hr_reg_write(context, QPC_RX_CQN, get_cqn(ibqp->recv_cq));
4308 hr_reg_clear(qpc_mask, QPC_RX_CQN);
4310 hr_reg_write(context, QPC_TX_CQN, get_cqn(ibqp->send_cq));
4311 hr_reg_clear(qpc_mask, QPC_TX_CQN);
4314 hr_reg_enable(context, QPC_SRQ_EN);
4315 hr_reg_clear(qpc_mask, QPC_SRQ_EN);
4316 hr_reg_write(context, QPC_SRQN, to_hr_srq(ibqp->srq)->srqn);
4317 hr_reg_clear(qpc_mask, QPC_SRQN);
4321 static int config_qp_rq_buf(struct hns_roce_dev *hr_dev,
4322 struct hns_roce_qp *hr_qp,
4323 struct hns_roce_v2_qp_context *context,
4324 struct hns_roce_v2_qp_context *qpc_mask)
4326 u64 mtts[MTT_MIN_COUNT] = { 0 };
4330 /* Search qp buf's mtts */
4331 count = hns_roce_mtr_find(hr_dev, &hr_qp->mtr, hr_qp->rq.offset, mtts,
4332 MTT_MIN_COUNT, &wqe_sge_ba);
4333 if (hr_qp->rq.wqe_cnt && count < 1) {
4334 ibdev_err(&hr_dev->ib_dev,
4335 "failed to find RQ WQE, QPN = 0x%lx.\n", hr_qp->qpn);
4339 context->wqe_sge_ba = cpu_to_le32(wqe_sge_ba >> 3);
4340 qpc_mask->wqe_sge_ba = 0;
4343 * In v2 engine, software pass context and context mask to hardware
4344 * when modifying qp. If software need modify some fields in context,
4345 * we should set all bits of the relevant fields in context mask to
4346 * 0 at the same time, else set them to 0x1.
4348 hr_reg_write(context, QPC_WQE_SGE_BA_H, wqe_sge_ba >> (32 + 3));
4349 hr_reg_clear(qpc_mask, QPC_WQE_SGE_BA_H);
4351 hr_reg_write(context, QPC_SQ_HOP_NUM,
4352 to_hr_hem_hopnum(hr_dev->caps.wqe_sq_hop_num,
4353 hr_qp->sq.wqe_cnt));
4354 hr_reg_clear(qpc_mask, QPC_SQ_HOP_NUM);
4356 hr_reg_write(context, QPC_SGE_HOP_NUM,
4357 to_hr_hem_hopnum(hr_dev->caps.wqe_sge_hop_num,
4358 hr_qp->sge.sge_cnt));
4359 hr_reg_clear(qpc_mask, QPC_SGE_HOP_NUM);
4361 hr_reg_write(context, QPC_RQ_HOP_NUM,
4362 to_hr_hem_hopnum(hr_dev->caps.wqe_rq_hop_num,
4363 hr_qp->rq.wqe_cnt));
4365 hr_reg_clear(qpc_mask, QPC_RQ_HOP_NUM);
4367 hr_reg_write(context, QPC_WQE_SGE_BA_PG_SZ,
4368 to_hr_hw_page_shift(hr_qp->mtr.hem_cfg.ba_pg_shift));
4369 hr_reg_clear(qpc_mask, QPC_WQE_SGE_BA_PG_SZ);
4371 hr_reg_write(context, QPC_WQE_SGE_BUF_PG_SZ,
4372 to_hr_hw_page_shift(hr_qp->mtr.hem_cfg.buf_pg_shift));
4373 hr_reg_clear(qpc_mask, QPC_WQE_SGE_BUF_PG_SZ);
4375 context->rq_cur_blk_addr = cpu_to_le32(to_hr_hw_page_addr(mtts[0]));
4376 qpc_mask->rq_cur_blk_addr = 0;
4378 hr_reg_write(context, QPC_RQ_CUR_BLK_ADDR_H,
4379 upper_32_bits(to_hr_hw_page_addr(mtts[0])));
4380 hr_reg_clear(qpc_mask, QPC_RQ_CUR_BLK_ADDR_H);
4382 context->rq_nxt_blk_addr = cpu_to_le32(to_hr_hw_page_addr(mtts[1]));
4383 qpc_mask->rq_nxt_blk_addr = 0;
4385 hr_reg_write(context, QPC_RQ_NXT_BLK_ADDR_H,
4386 upper_32_bits(to_hr_hw_page_addr(mtts[1])));
4387 hr_reg_clear(qpc_mask, QPC_RQ_NXT_BLK_ADDR_H);
4392 static int config_qp_sq_buf(struct hns_roce_dev *hr_dev,
4393 struct hns_roce_qp *hr_qp,
4394 struct hns_roce_v2_qp_context *context,
4395 struct hns_roce_v2_qp_context *qpc_mask)
4397 struct ib_device *ibdev = &hr_dev->ib_dev;
4398 u64 sge_cur_blk = 0;
4402 /* search qp buf's mtts */
4403 count = hns_roce_mtr_find(hr_dev, &hr_qp->mtr, 0, &sq_cur_blk, 1, NULL);
4405 ibdev_err(ibdev, "failed to find QP(0x%lx) SQ buf.\n",
4409 if (hr_qp->sge.sge_cnt > 0) {
4410 count = hns_roce_mtr_find(hr_dev, &hr_qp->mtr,
4412 &sge_cur_blk, 1, NULL);
4414 ibdev_err(ibdev, "failed to find QP(0x%lx) SGE buf.\n",
4421 * In v2 engine, software pass context and context mask to hardware
4422 * when modifying qp. If software need modify some fields in context,
4423 * we should set all bits of the relevant fields in context mask to
4424 * 0 at the same time, else set them to 0x1.
4426 hr_reg_write(context, QPC_SQ_CUR_BLK_ADDR_L,
4427 lower_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4428 hr_reg_write(context, QPC_SQ_CUR_BLK_ADDR_H,
4429 upper_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4430 hr_reg_clear(qpc_mask, QPC_SQ_CUR_BLK_ADDR_L);
4431 hr_reg_clear(qpc_mask, QPC_SQ_CUR_BLK_ADDR_H);
4433 hr_reg_write(context, QPC_SQ_CUR_SGE_BLK_ADDR_L,
4434 lower_32_bits(to_hr_hw_page_addr(sge_cur_blk)));
4435 hr_reg_write(context, QPC_SQ_CUR_SGE_BLK_ADDR_H,
4436 upper_32_bits(to_hr_hw_page_addr(sge_cur_blk)));
4437 hr_reg_clear(qpc_mask, QPC_SQ_CUR_SGE_BLK_ADDR_L);
4438 hr_reg_clear(qpc_mask, QPC_SQ_CUR_SGE_BLK_ADDR_H);
4440 hr_reg_write(context, QPC_RX_SQ_CUR_BLK_ADDR_L,
4441 lower_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4442 hr_reg_write(context, QPC_RX_SQ_CUR_BLK_ADDR_H,
4443 upper_32_bits(to_hr_hw_page_addr(sq_cur_blk)));
4444 hr_reg_clear(qpc_mask, QPC_RX_SQ_CUR_BLK_ADDR_L);
4445 hr_reg_clear(qpc_mask, QPC_RX_SQ_CUR_BLK_ADDR_H);
4450 static inline enum ib_mtu get_mtu(struct ib_qp *ibqp,
4451 const struct ib_qp_attr *attr)
4453 if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_UD)
4456 return attr->path_mtu;
4459 static int modify_qp_init_to_rtr(struct ib_qp *ibqp,
4460 const struct ib_qp_attr *attr, int attr_mask,
4461 struct hns_roce_v2_qp_context *context,
4462 struct hns_roce_v2_qp_context *qpc_mask,
4463 struct ib_udata *udata)
4465 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(udata,
4466 struct hns_roce_ucontext, ibucontext);
4467 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4468 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4469 struct ib_device *ibdev = &hr_dev->ib_dev;
4481 ret = config_qp_rq_buf(hr_dev, hr_qp, context, qpc_mask);
4483 ibdev_err(ibdev, "failed to config rq buf, ret = %d.\n", ret);
4487 /* Search IRRL's mtts */
4488 mtts = hns_roce_table_find(hr_dev, &hr_dev->qp_table.irrl_table,
4489 hr_qp->qpn, &irrl_ba);
4491 ibdev_err(ibdev, "failed to find qp irrl_table.\n");
4495 /* Search TRRL's mtts */
4496 mtts = hns_roce_table_find(hr_dev, &hr_dev->qp_table.trrl_table,
4497 hr_qp->qpn, &trrl_ba);
4499 ibdev_err(ibdev, "failed to find qp trrl_table.\n");
4503 if (attr_mask & IB_QP_ALT_PATH) {
4504 ibdev_err(ibdev, "INIT2RTR attr_mask (0x%x) error.\n",
4509 hr_reg_write(context, QPC_TRRL_BA_L, trrl_ba >> 4);
4510 hr_reg_clear(qpc_mask, QPC_TRRL_BA_L);
4511 context->trrl_ba = cpu_to_le32(trrl_ba >> (16 + 4));
4512 qpc_mask->trrl_ba = 0;
4513 hr_reg_write(context, QPC_TRRL_BA_H, trrl_ba >> (32 + 16 + 4));
4514 hr_reg_clear(qpc_mask, QPC_TRRL_BA_H);
4516 context->irrl_ba = cpu_to_le32(irrl_ba >> 6);
4517 qpc_mask->irrl_ba = 0;
4518 hr_reg_write(context, QPC_IRRL_BA_H, irrl_ba >> (32 + 6));
4519 hr_reg_clear(qpc_mask, QPC_IRRL_BA_H);
4521 hr_reg_enable(context, QPC_RMT_E2E);
4522 hr_reg_clear(qpc_mask, QPC_RMT_E2E);
4524 hr_reg_write(context, QPC_SIG_TYPE, hr_qp->sq_signal_bits);
4525 hr_reg_clear(qpc_mask, QPC_SIG_TYPE);
4527 port = (attr_mask & IB_QP_PORT) ? (attr->port_num - 1) : hr_qp->port;
4529 smac = (const u8 *)hr_dev->dev_addr[port];
4530 dmac = (u8 *)attr->ah_attr.roce.dmac;
4531 /* when dmac equals smac or loop_idc is 1, it should loopback */
4532 if (ether_addr_equal_unaligned(dmac, smac) ||
4533 hr_dev->loop_idc == 0x1) {
4534 hr_reg_write(context, QPC_LBI, hr_dev->loop_idc);
4535 hr_reg_clear(qpc_mask, QPC_LBI);
4538 if (attr_mask & IB_QP_DEST_QPN) {
4539 hr_reg_write(context, QPC_DQPN, attr->dest_qp_num);
4540 hr_reg_clear(qpc_mask, QPC_DQPN);
4543 memcpy(&context->dmac, dmac, sizeof(u32));
4544 hr_reg_write(context, QPC_DMAC_H, *((u16 *)(&dmac[4])));
4546 hr_reg_clear(qpc_mask, QPC_DMAC_H);
4548 ib_mtu = get_mtu(ibqp, attr);
4549 hr_qp->path_mtu = ib_mtu;
4551 mtu = ib_mtu_enum_to_int(ib_mtu);
4552 if (WARN_ON(mtu <= 0))
4554 #define MIN_LP_MSG_LEN 1024
4555 /* mtu * (2 ^ lp_pktn_ini) should be in the range of 1024 to mtu */
4556 lp_pktn_ini = ilog2(max(mtu, MIN_LP_MSG_LEN) / mtu);
4558 if (attr_mask & IB_QP_PATH_MTU) {
4559 hr_reg_write(context, QPC_MTU, ib_mtu);
4560 hr_reg_clear(qpc_mask, QPC_MTU);
4563 hr_reg_write(context, QPC_LP_PKTN_INI, lp_pktn_ini);
4564 hr_reg_clear(qpc_mask, QPC_LP_PKTN_INI);
4566 /* ACK_REQ_FREQ should be larger than or equal to LP_PKTN_INI */
4567 hr_reg_write(context, QPC_ACK_REQ_FREQ, lp_pktn_ini);
4568 hr_reg_clear(qpc_mask, QPC_ACK_REQ_FREQ);
4570 hr_reg_clear(qpc_mask, QPC_RX_REQ_PSN_ERR);
4571 hr_reg_clear(qpc_mask, QPC_RX_REQ_MSN);
4572 hr_reg_clear(qpc_mask, QPC_RX_REQ_LAST_OPTYPE);
4574 context->rq_rnr_timer = 0;
4575 qpc_mask->rq_rnr_timer = 0;
4577 hr_reg_clear(qpc_mask, QPC_TRRL_HEAD_MAX);
4578 hr_reg_clear(qpc_mask, QPC_TRRL_TAIL_MAX);
4580 /* rocee send 2^lp_sgen_ini segs every time */
4581 hr_reg_write(context, QPC_LP_SGEN_INI, 3);
4582 hr_reg_clear(qpc_mask, QPC_LP_SGEN_INI);
4584 if (udata && ibqp->qp_type == IB_QPT_RC &&
4585 (uctx->config & HNS_ROCE_RQ_INLINE_FLAGS)) {
4586 hr_reg_write_bool(context, QPC_RQIE,
4587 hr_dev->caps.flags &
4588 HNS_ROCE_CAP_FLAG_RQ_INLINE);
4589 hr_reg_clear(qpc_mask, QPC_RQIE);
4593 (ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_XRC_TGT) &&
4594 (uctx->config & HNS_ROCE_CQE_INLINE_FLAGS)) {
4595 hr_reg_write_bool(context, QPC_CQEIE,
4596 hr_dev->caps.flags &
4597 HNS_ROCE_CAP_FLAG_CQE_INLINE);
4598 hr_reg_clear(qpc_mask, QPC_CQEIE);
4600 hr_reg_write(context, QPC_CQEIS, 0);
4601 hr_reg_clear(qpc_mask, QPC_CQEIS);
4607 static int modify_qp_rtr_to_rts(struct ib_qp *ibqp,
4608 const struct ib_qp_attr *attr, int attr_mask,
4609 struct hns_roce_v2_qp_context *context,
4610 struct hns_roce_v2_qp_context *qpc_mask)
4612 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4613 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4614 struct ib_device *ibdev = &hr_dev->ib_dev;
4617 /* Not support alternate path and path migration */
4618 if (attr_mask & (IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE)) {
4619 ibdev_err(ibdev, "RTR2RTS attr_mask (0x%x)error\n", attr_mask);
4623 ret = config_qp_sq_buf(hr_dev, hr_qp, context, qpc_mask);
4625 ibdev_err(ibdev, "failed to config sq buf, ret = %d.\n", ret);
4630 * Set some fields in context to zero, Because the default values
4631 * of all fields in context are zero, we need not set them to 0 again.
4632 * but we should set the relevant fields of context mask to 0.
4634 hr_reg_clear(qpc_mask, QPC_IRRL_SGE_IDX);
4636 hr_reg_clear(qpc_mask, QPC_RX_ACK_MSN);
4638 hr_reg_clear(qpc_mask, QPC_ACK_LAST_OPTYPE);
4639 hr_reg_clear(qpc_mask, QPC_IRRL_PSN_VLD);
4640 hr_reg_clear(qpc_mask, QPC_IRRL_PSN);
4642 hr_reg_clear(qpc_mask, QPC_IRRL_TAIL_REAL);
4644 hr_reg_clear(qpc_mask, QPC_RETRY_MSG_MSN);
4646 hr_reg_clear(qpc_mask, QPC_RNR_RETRY_FLAG);
4648 hr_reg_clear(qpc_mask, QPC_CHECK_FLG);
4650 hr_reg_clear(qpc_mask, QPC_V2_IRRL_HEAD);
4655 static int get_dip_ctx_idx(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
4658 const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
4659 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4660 u32 *spare_idx = hr_dev->qp_table.idx_table.spare_idx;
4661 u32 *head = &hr_dev->qp_table.idx_table.head;
4662 u32 *tail = &hr_dev->qp_table.idx_table.tail;
4663 struct hns_roce_dip *hr_dip;
4664 unsigned long flags;
4667 spin_lock_irqsave(&hr_dev->dip_list_lock, flags);
4669 spare_idx[*tail] = ibqp->qp_num;
4670 *tail = (*tail == hr_dev->caps.num_qps - 1) ? 0 : (*tail + 1);
4672 list_for_each_entry(hr_dip, &hr_dev->dip_list, node) {
4673 if (!memcmp(grh->dgid.raw, hr_dip->dgid, 16)) {
4674 *dip_idx = hr_dip->dip_idx;
4679 /* If no dgid is found, a new dip and a mapping between dgid and
4680 * dip_idx will be created.
4682 hr_dip = kzalloc(sizeof(*hr_dip), GFP_ATOMIC);
4688 memcpy(hr_dip->dgid, grh->dgid.raw, sizeof(grh->dgid.raw));
4689 hr_dip->dip_idx = *dip_idx = spare_idx[*head];
4690 *head = (*head == hr_dev->caps.num_qps - 1) ? 0 : (*head + 1);
4691 list_add_tail(&hr_dip->node, &hr_dev->dip_list);
4694 spin_unlock_irqrestore(&hr_dev->dip_list_lock, flags);
4704 UNSUPPORT_CONG_LEVEL,
4723 static int check_cong_type(struct ib_qp *ibqp,
4724 struct hns_roce_congestion_algorithm *cong_alg)
4726 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4728 /* different congestion types match different configurations */
4729 switch (hr_dev->caps.cong_type) {
4730 case CONG_TYPE_DCQCN:
4731 cong_alg->alg_sel = CONG_DCQCN;
4732 cong_alg->alg_sub_sel = UNSUPPORT_CONG_LEVEL;
4733 cong_alg->dip_vld = DIP_INVALID;
4734 cong_alg->wnd_mode_sel = WND_LIMIT;
4736 case CONG_TYPE_LDCP:
4737 cong_alg->alg_sel = CONG_WINDOW;
4738 cong_alg->alg_sub_sel = CONG_LDCP;
4739 cong_alg->dip_vld = DIP_INVALID;
4740 cong_alg->wnd_mode_sel = WND_UNLIMIT;
4743 cong_alg->alg_sel = CONG_WINDOW;
4744 cong_alg->alg_sub_sel = CONG_HC3;
4745 cong_alg->dip_vld = DIP_INVALID;
4746 cong_alg->wnd_mode_sel = WND_LIMIT;
4749 cong_alg->alg_sel = CONG_DCQCN;
4750 cong_alg->alg_sub_sel = UNSUPPORT_CONG_LEVEL;
4751 cong_alg->dip_vld = DIP_VALID;
4752 cong_alg->wnd_mode_sel = WND_LIMIT;
4755 ibdev_err(&hr_dev->ib_dev,
4756 "error type(%u) for congestion selection.\n",
4757 hr_dev->caps.cong_type);
4764 static int fill_cong_field(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
4765 struct hns_roce_v2_qp_context *context,
4766 struct hns_roce_v2_qp_context *qpc_mask)
4768 const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
4769 struct hns_roce_congestion_algorithm cong_field;
4770 struct ib_device *ibdev = ibqp->device;
4771 struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
4775 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08 ||
4776 grh->sgid_attr->gid_type == IB_GID_TYPE_ROCE)
4779 ret = check_cong_type(ibqp, &cong_field);
4783 hr_reg_write(context, QPC_CONG_ALGO_TMPL_ID, hr_dev->cong_algo_tmpl_id +
4784 hr_dev->caps.cong_type * HNS_ROCE_CONG_SIZE);
4785 hr_reg_clear(qpc_mask, QPC_CONG_ALGO_TMPL_ID);
4786 hr_reg_write(&context->ext, QPCEX_CONG_ALG_SEL, cong_field.alg_sel);
4787 hr_reg_clear(&qpc_mask->ext, QPCEX_CONG_ALG_SEL);
4788 hr_reg_write(&context->ext, QPCEX_CONG_ALG_SUB_SEL,
4789 cong_field.alg_sub_sel);
4790 hr_reg_clear(&qpc_mask->ext, QPCEX_CONG_ALG_SUB_SEL);
4791 hr_reg_write(&context->ext, QPCEX_DIP_CTX_IDX_VLD, cong_field.dip_vld);
4792 hr_reg_clear(&qpc_mask->ext, QPCEX_DIP_CTX_IDX_VLD);
4793 hr_reg_write(&context->ext, QPCEX_SQ_RQ_NOT_FORBID_EN,
4794 cong_field.wnd_mode_sel);
4795 hr_reg_clear(&qpc_mask->ext, QPCEX_SQ_RQ_NOT_FORBID_EN);
4797 /* if dip is disabled, there is no need to set dip idx */
4798 if (cong_field.dip_vld == 0)
4801 ret = get_dip_ctx_idx(ibqp, attr, &dip_idx);
4803 ibdev_err(ibdev, "failed to fill cong field, ret = %d.\n", ret);
4807 hr_reg_write(&context->ext, QPCEX_DIP_CTX_IDX, dip_idx);
4808 hr_reg_write(&qpc_mask->ext, QPCEX_DIP_CTX_IDX, 0);
4813 static int hns_roce_v2_set_path(struct ib_qp *ibqp,
4814 const struct ib_qp_attr *attr,
4816 struct hns_roce_v2_qp_context *context,
4817 struct hns_roce_v2_qp_context *qpc_mask)
4819 const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
4820 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4821 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
4822 struct ib_device *ibdev = &hr_dev->ib_dev;
4823 const struct ib_gid_attr *gid_attr = NULL;
4824 int is_roce_protocol;
4825 u16 vlan_id = 0xffff;
4826 bool is_udp = false;
4832 * If free_mr_en of qp is set, it means that this qp comes from
4833 * free mr. This qp will perform the loopback operation.
4834 * In the loopback scenario, only sl needs to be set.
4836 if (hr_qp->free_mr_en) {
4837 hr_reg_write(context, QPC_SL, rdma_ah_get_sl(&attr->ah_attr));
4838 hr_reg_clear(qpc_mask, QPC_SL);
4839 hr_qp->sl = rdma_ah_get_sl(&attr->ah_attr);
4843 ib_port = (attr_mask & IB_QP_PORT) ? attr->port_num : hr_qp->port + 1;
4844 hr_port = ib_port - 1;
4845 is_roce_protocol = rdma_cap_eth_ah(&hr_dev->ib_dev, ib_port) &&
4846 rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH;
4848 if (is_roce_protocol) {
4849 gid_attr = attr->ah_attr.grh.sgid_attr;
4850 ret = rdma_read_gid_l2_fields(gid_attr, &vlan_id, NULL);
4854 is_udp = (gid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP);
4857 /* Only HIP08 needs to set the vlan_en bits in QPC */
4858 if (vlan_id < VLAN_N_VID &&
4859 hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
4860 hr_reg_enable(context, QPC_RQ_VLAN_EN);
4861 hr_reg_clear(qpc_mask, QPC_RQ_VLAN_EN);
4862 hr_reg_enable(context, QPC_SQ_VLAN_EN);
4863 hr_reg_clear(qpc_mask, QPC_SQ_VLAN_EN);
4866 hr_reg_write(context, QPC_VLAN_ID, vlan_id);
4867 hr_reg_clear(qpc_mask, QPC_VLAN_ID);
4869 if (grh->sgid_index >= hr_dev->caps.gid_table_len[hr_port]) {
4870 ibdev_err(ibdev, "sgid_index(%u) too large. max is %d\n",
4871 grh->sgid_index, hr_dev->caps.gid_table_len[hr_port]);
4875 if (attr->ah_attr.type != RDMA_AH_ATTR_TYPE_ROCE) {
4876 ibdev_err(ibdev, "ah attr is not RDMA roce type\n");
4880 hr_reg_write(context, QPC_UDPSPN,
4881 is_udp ? rdma_get_udp_sport(grh->flow_label, ibqp->qp_num,
4882 attr->dest_qp_num) :
4885 hr_reg_clear(qpc_mask, QPC_UDPSPN);
4887 hr_reg_write(context, QPC_GMV_IDX, grh->sgid_index);
4889 hr_reg_clear(qpc_mask, QPC_GMV_IDX);
4891 hr_reg_write(context, QPC_HOPLIMIT, grh->hop_limit);
4892 hr_reg_clear(qpc_mask, QPC_HOPLIMIT);
4894 ret = fill_cong_field(ibqp, attr, context, qpc_mask);
4898 hr_reg_write(context, QPC_TC, get_tclass(&attr->ah_attr.grh));
4899 hr_reg_clear(qpc_mask, QPC_TC);
4901 hr_reg_write(context, QPC_FL, grh->flow_label);
4902 hr_reg_clear(qpc_mask, QPC_FL);
4903 memcpy(context->dgid, grh->dgid.raw, sizeof(grh->dgid.raw));
4904 memset(qpc_mask->dgid, 0, sizeof(grh->dgid.raw));
4906 hr_qp->sl = rdma_ah_get_sl(&attr->ah_attr);
4907 if (unlikely(hr_qp->sl > MAX_SERVICE_LEVEL)) {
4909 "failed to fill QPC, sl (%u) shouldn't be larger than %d.\n",
4910 hr_qp->sl, MAX_SERVICE_LEVEL);
4914 hr_reg_write(context, QPC_SL, hr_qp->sl);
4915 hr_reg_clear(qpc_mask, QPC_SL);
4920 static bool check_qp_state(enum ib_qp_state cur_state,
4921 enum ib_qp_state new_state)
4923 static const bool sm[][IB_QPS_ERR + 1] = {
4924 [IB_QPS_RESET] = { [IB_QPS_RESET] = true,
4925 [IB_QPS_INIT] = true },
4926 [IB_QPS_INIT] = { [IB_QPS_RESET] = true,
4927 [IB_QPS_INIT] = true,
4928 [IB_QPS_RTR] = true,
4929 [IB_QPS_ERR] = true },
4930 [IB_QPS_RTR] = { [IB_QPS_RESET] = true,
4931 [IB_QPS_RTS] = true,
4932 [IB_QPS_ERR] = true },
4933 [IB_QPS_RTS] = { [IB_QPS_RESET] = true,
4934 [IB_QPS_RTS] = true,
4935 [IB_QPS_ERR] = true },
4938 [IB_QPS_ERR] = { [IB_QPS_RESET] = true,
4939 [IB_QPS_ERR] = true }
4942 return sm[cur_state][new_state];
4945 static int hns_roce_v2_set_abs_fields(struct ib_qp *ibqp,
4946 const struct ib_qp_attr *attr,
4948 enum ib_qp_state cur_state,
4949 enum ib_qp_state new_state,
4950 struct hns_roce_v2_qp_context *context,
4951 struct hns_roce_v2_qp_context *qpc_mask,
4952 struct ib_udata *udata)
4954 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
4957 if (!check_qp_state(cur_state, new_state)) {
4958 ibdev_err(&hr_dev->ib_dev, "Illegal state for QP!\n");
4962 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
4963 memset(qpc_mask, 0, hr_dev->caps.qpc_sz);
4964 modify_qp_reset_to_init(ibqp, attr, context, qpc_mask);
4965 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
4966 modify_qp_init_to_init(ibqp, attr, context, qpc_mask);
4967 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
4968 ret = modify_qp_init_to_rtr(ibqp, attr, attr_mask, context,
4970 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
4971 ret = modify_qp_rtr_to_rts(ibqp, attr, attr_mask, context,
4978 static bool check_qp_timeout_cfg_range(struct hns_roce_dev *hr_dev, u8 *timeout)
4980 #define QP_ACK_TIMEOUT_MAX_HIP08 20
4981 #define QP_ACK_TIMEOUT_MAX 31
4983 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
4984 if (*timeout > QP_ACK_TIMEOUT_MAX_HIP08) {
4985 ibdev_warn(&hr_dev->ib_dev,
4986 "local ACK timeout shall be 0 to 20.\n");
4989 *timeout += HNS_ROCE_V2_QP_ACK_TIMEOUT_OFS_HIP08;
4990 } else if (hr_dev->pci_dev->revision > PCI_REVISION_ID_HIP08) {
4991 if (*timeout > QP_ACK_TIMEOUT_MAX) {
4992 ibdev_warn(&hr_dev->ib_dev,
4993 "local ACK timeout shall be 0 to 31.\n");
5001 static int hns_roce_v2_set_opt_fields(struct ib_qp *ibqp,
5002 const struct ib_qp_attr *attr,
5004 struct hns_roce_v2_qp_context *context,
5005 struct hns_roce_v2_qp_context *qpc_mask)
5007 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5008 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5012 if (attr_mask & IB_QP_AV) {
5013 ret = hns_roce_v2_set_path(ibqp, attr, attr_mask, context,
5019 if (attr_mask & IB_QP_TIMEOUT) {
5020 timeout = attr->timeout;
5021 if (check_qp_timeout_cfg_range(hr_dev, &timeout)) {
5022 hr_reg_write(context, QPC_AT, timeout);
5023 hr_reg_clear(qpc_mask, QPC_AT);
5027 if (attr_mask & IB_QP_RETRY_CNT) {
5028 hr_reg_write(context, QPC_RETRY_NUM_INIT, attr->retry_cnt);
5029 hr_reg_clear(qpc_mask, QPC_RETRY_NUM_INIT);
5031 hr_reg_write(context, QPC_RETRY_CNT, attr->retry_cnt);
5032 hr_reg_clear(qpc_mask, QPC_RETRY_CNT);
5035 if (attr_mask & IB_QP_RNR_RETRY) {
5036 hr_reg_write(context, QPC_RNR_NUM_INIT, attr->rnr_retry);
5037 hr_reg_clear(qpc_mask, QPC_RNR_NUM_INIT);
5039 hr_reg_write(context, QPC_RNR_CNT, attr->rnr_retry);
5040 hr_reg_clear(qpc_mask, QPC_RNR_CNT);
5043 if (attr_mask & IB_QP_SQ_PSN) {
5044 hr_reg_write(context, QPC_SQ_CUR_PSN, attr->sq_psn);
5045 hr_reg_clear(qpc_mask, QPC_SQ_CUR_PSN);
5047 hr_reg_write(context, QPC_SQ_MAX_PSN, attr->sq_psn);
5048 hr_reg_clear(qpc_mask, QPC_SQ_MAX_PSN);
5050 hr_reg_write(context, QPC_RETRY_MSG_PSN_L, attr->sq_psn);
5051 hr_reg_clear(qpc_mask, QPC_RETRY_MSG_PSN_L);
5053 hr_reg_write(context, QPC_RETRY_MSG_PSN_H,
5054 attr->sq_psn >> RETRY_MSG_PSN_SHIFT);
5055 hr_reg_clear(qpc_mask, QPC_RETRY_MSG_PSN_H);
5057 hr_reg_write(context, QPC_RETRY_MSG_FPKT_PSN, attr->sq_psn);
5058 hr_reg_clear(qpc_mask, QPC_RETRY_MSG_FPKT_PSN);
5060 hr_reg_write(context, QPC_RX_ACK_EPSN, attr->sq_psn);
5061 hr_reg_clear(qpc_mask, QPC_RX_ACK_EPSN);
5064 if ((attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) &&
5065 attr->max_dest_rd_atomic) {
5066 hr_reg_write(context, QPC_RR_MAX,
5067 fls(attr->max_dest_rd_atomic - 1));
5068 hr_reg_clear(qpc_mask, QPC_RR_MAX);
5071 if ((attr_mask & IB_QP_MAX_QP_RD_ATOMIC) && attr->max_rd_atomic) {
5072 hr_reg_write(context, QPC_SR_MAX, fls(attr->max_rd_atomic - 1));
5073 hr_reg_clear(qpc_mask, QPC_SR_MAX);
5076 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
5077 set_access_flags(hr_qp, context, qpc_mask, attr, attr_mask);
5079 if (attr_mask & IB_QP_MIN_RNR_TIMER) {
5080 hr_reg_write(context, QPC_MIN_RNR_TIME,
5081 hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08 ?
5082 HNS_ROCE_RNR_TIMER_10NS : attr->min_rnr_timer);
5083 hr_reg_clear(qpc_mask, QPC_MIN_RNR_TIME);
5086 if (attr_mask & IB_QP_RQ_PSN) {
5087 hr_reg_write(context, QPC_RX_REQ_EPSN, attr->rq_psn);
5088 hr_reg_clear(qpc_mask, QPC_RX_REQ_EPSN);
5090 hr_reg_write(context, QPC_RAQ_PSN, attr->rq_psn - 1);
5091 hr_reg_clear(qpc_mask, QPC_RAQ_PSN);
5094 if (attr_mask & IB_QP_QKEY) {
5095 context->qkey_xrcd = cpu_to_le32(attr->qkey);
5096 qpc_mask->qkey_xrcd = 0;
5097 hr_qp->qkey = attr->qkey;
5103 static void hns_roce_v2_record_opt_fields(struct ib_qp *ibqp,
5104 const struct ib_qp_attr *attr,
5107 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5108 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5110 if (attr_mask & IB_QP_ACCESS_FLAGS)
5111 hr_qp->atomic_rd_en = attr->qp_access_flags;
5113 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
5114 hr_qp->resp_depth = attr->max_dest_rd_atomic;
5115 if (attr_mask & IB_QP_PORT) {
5116 hr_qp->port = attr->port_num - 1;
5117 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
5121 static void clear_qp(struct hns_roce_qp *hr_qp)
5123 struct ib_qp *ibqp = &hr_qp->ibqp;
5126 hns_roce_v2_cq_clean(to_hr_cq(ibqp->send_cq),
5129 if (ibqp->recv_cq && ibqp->recv_cq != ibqp->send_cq)
5130 hns_roce_v2_cq_clean(to_hr_cq(ibqp->recv_cq),
5131 hr_qp->qpn, ibqp->srq ?
5132 to_hr_srq(ibqp->srq) : NULL);
5134 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
5135 *hr_qp->rdb.db_record = 0;
5141 hr_qp->next_sge = 0;
5144 static void v2_set_flushed_fields(struct ib_qp *ibqp,
5145 struct hns_roce_v2_qp_context *context,
5146 struct hns_roce_v2_qp_context *qpc_mask)
5148 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5149 unsigned long sq_flag = 0;
5150 unsigned long rq_flag = 0;
5152 if (ibqp->qp_type == IB_QPT_XRC_TGT)
5155 spin_lock_irqsave(&hr_qp->sq.lock, sq_flag);
5156 hr_reg_write(context, QPC_SQ_PRODUCER_IDX, hr_qp->sq.head);
5157 hr_reg_clear(qpc_mask, QPC_SQ_PRODUCER_IDX);
5158 hr_qp->state = IB_QPS_ERR;
5159 spin_unlock_irqrestore(&hr_qp->sq.lock, sq_flag);
5161 if (ibqp->srq || ibqp->qp_type == IB_QPT_XRC_INI) /* no RQ */
5164 spin_lock_irqsave(&hr_qp->rq.lock, rq_flag);
5165 hr_reg_write(context, QPC_RQ_PRODUCER_IDX, hr_qp->rq.head);
5166 hr_reg_clear(qpc_mask, QPC_RQ_PRODUCER_IDX);
5167 spin_unlock_irqrestore(&hr_qp->rq.lock, rq_flag);
5170 static int hns_roce_v2_modify_qp(struct ib_qp *ibqp,
5171 const struct ib_qp_attr *attr,
5172 int attr_mask, enum ib_qp_state cur_state,
5173 enum ib_qp_state new_state, struct ib_udata *udata)
5175 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5176 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5177 struct hns_roce_v2_qp_context ctx[2];
5178 struct hns_roce_v2_qp_context *context = ctx;
5179 struct hns_roce_v2_qp_context *qpc_mask = ctx + 1;
5180 struct ib_device *ibdev = &hr_dev->ib_dev;
5183 if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
5187 * In v2 engine, software pass context and context mask to hardware
5188 * when modifying qp. If software need modify some fields in context,
5189 * we should set all bits of the relevant fields in context mask to
5190 * 0 at the same time, else set them to 0x1.
5192 memset(context, 0, hr_dev->caps.qpc_sz);
5193 memset(qpc_mask, 0xff, hr_dev->caps.qpc_sz);
5195 ret = hns_roce_v2_set_abs_fields(ibqp, attr, attr_mask, cur_state,
5196 new_state, context, qpc_mask, udata);
5200 /* When QP state is err, SQ and RQ WQE should be flushed */
5201 if (new_state == IB_QPS_ERR)
5202 v2_set_flushed_fields(ibqp, context, qpc_mask);
5204 /* Configure the optional fields */
5205 ret = hns_roce_v2_set_opt_fields(ibqp, attr, attr_mask, context,
5210 hr_reg_write_bool(context, QPC_INV_CREDIT,
5211 to_hr_qp_type(hr_qp->ibqp.qp_type) == SERV_TYPE_XRC ||
5213 hr_reg_clear(qpc_mask, QPC_INV_CREDIT);
5215 /* Every status migrate must change state */
5216 hr_reg_write(context, QPC_QP_ST, new_state);
5217 hr_reg_clear(qpc_mask, QPC_QP_ST);
5219 /* SW pass context to HW */
5220 ret = hns_roce_v2_qp_modify(hr_dev, context, qpc_mask, hr_qp);
5222 ibdev_err(ibdev, "failed to modify QP, ret = %d.\n", ret);
5226 hr_qp->state = new_state;
5228 hns_roce_v2_record_opt_fields(ibqp, attr, attr_mask);
5230 if (new_state == IB_QPS_RESET && !ibqp->uobject)
5237 static int to_ib_qp_st(enum hns_roce_v2_qp_state state)
5239 static const enum ib_qp_state map[] = {
5240 [HNS_ROCE_QP_ST_RST] = IB_QPS_RESET,
5241 [HNS_ROCE_QP_ST_INIT] = IB_QPS_INIT,
5242 [HNS_ROCE_QP_ST_RTR] = IB_QPS_RTR,
5243 [HNS_ROCE_QP_ST_RTS] = IB_QPS_RTS,
5244 [HNS_ROCE_QP_ST_SQD] = IB_QPS_SQD,
5245 [HNS_ROCE_QP_ST_SQER] = IB_QPS_SQE,
5246 [HNS_ROCE_QP_ST_ERR] = IB_QPS_ERR,
5247 [HNS_ROCE_QP_ST_SQ_DRAINING] = IB_QPS_SQD
5250 return (state < ARRAY_SIZE(map)) ? map[state] : -1;
5253 static int hns_roce_v2_query_qpc(struct hns_roce_dev *hr_dev, u32 qpn,
5256 struct hns_roce_cmd_mailbox *mailbox;
5259 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5260 if (IS_ERR(mailbox))
5261 return PTR_ERR(mailbox);
5263 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, HNS_ROCE_CMD_QUERY_QPC,
5268 memcpy(buffer, mailbox->buf, hr_dev->caps.qpc_sz);
5271 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5275 static u8 get_qp_timeout_attr(struct hns_roce_dev *hr_dev,
5276 struct hns_roce_v2_qp_context *context)
5280 timeout = (u8)hr_reg_read(context, QPC_AT);
5281 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
5282 timeout -= HNS_ROCE_V2_QP_ACK_TIMEOUT_OFS_HIP08;
5287 static int hns_roce_v2_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5289 struct ib_qp_init_attr *qp_init_attr)
5291 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5292 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5293 struct hns_roce_v2_qp_context context = {};
5294 struct ib_device *ibdev = &hr_dev->ib_dev;
5299 memset(qp_attr, 0, sizeof(*qp_attr));
5300 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5302 mutex_lock(&hr_qp->mutex);
5304 if (hr_qp->state == IB_QPS_RESET) {
5305 qp_attr->qp_state = IB_QPS_RESET;
5310 ret = hns_roce_v2_query_qpc(hr_dev, hr_qp->qpn, &context);
5312 ibdev_err(ibdev, "failed to query QPC, ret = %d.\n", ret);
5317 state = hr_reg_read(&context, QPC_QP_ST);
5318 tmp_qp_state = to_ib_qp_st((enum hns_roce_v2_qp_state)state);
5319 if (tmp_qp_state == -1) {
5320 ibdev_err(ibdev, "Illegal ib_qp_state\n");
5324 hr_qp->state = (u8)tmp_qp_state;
5325 qp_attr->qp_state = (enum ib_qp_state)hr_qp->state;
5326 qp_attr->path_mtu = (enum ib_mtu)hr_reg_read(&context, QPC_MTU);
5327 qp_attr->path_mig_state = IB_MIG_ARMED;
5328 qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
5329 if (hr_qp->ibqp.qp_type == IB_QPT_UD)
5330 qp_attr->qkey = le32_to_cpu(context.qkey_xrcd);
5332 qp_attr->rq_psn = hr_reg_read(&context, QPC_RX_REQ_EPSN);
5333 qp_attr->sq_psn = (u32)hr_reg_read(&context, QPC_SQ_CUR_PSN);
5334 qp_attr->dest_qp_num = hr_reg_read(&context, QPC_DQPN);
5335 qp_attr->qp_access_flags =
5336 ((hr_reg_read(&context, QPC_RRE)) << V2_QP_RRE_S) |
5337 ((hr_reg_read(&context, QPC_RWE)) << V2_QP_RWE_S) |
5338 ((hr_reg_read(&context, QPC_ATE)) << V2_QP_ATE_S);
5340 if (hr_qp->ibqp.qp_type == IB_QPT_RC ||
5341 hr_qp->ibqp.qp_type == IB_QPT_XRC_INI ||
5342 hr_qp->ibqp.qp_type == IB_QPT_XRC_TGT) {
5343 struct ib_global_route *grh =
5344 rdma_ah_retrieve_grh(&qp_attr->ah_attr);
5346 rdma_ah_set_sl(&qp_attr->ah_attr,
5347 hr_reg_read(&context, QPC_SL));
5348 rdma_ah_set_port_num(&qp_attr->ah_attr, hr_qp->port + 1);
5349 rdma_ah_set_ah_flags(&qp_attr->ah_attr, IB_AH_GRH);
5350 grh->flow_label = hr_reg_read(&context, QPC_FL);
5351 grh->sgid_index = hr_reg_read(&context, QPC_GMV_IDX);
5352 grh->hop_limit = hr_reg_read(&context, QPC_HOPLIMIT);
5353 grh->traffic_class = hr_reg_read(&context, QPC_TC);
5355 memcpy(grh->dgid.raw, context.dgid, sizeof(grh->dgid.raw));
5358 qp_attr->port_num = hr_qp->port + 1;
5359 qp_attr->sq_draining = 0;
5360 qp_attr->max_rd_atomic = 1 << hr_reg_read(&context, QPC_SR_MAX);
5361 qp_attr->max_dest_rd_atomic = 1 << hr_reg_read(&context, QPC_RR_MAX);
5363 qp_attr->min_rnr_timer = (u8)hr_reg_read(&context, QPC_MIN_RNR_TIME);
5364 qp_attr->timeout = get_qp_timeout_attr(hr_dev, &context);
5365 qp_attr->retry_cnt = hr_reg_read(&context, QPC_RETRY_NUM_INIT);
5366 qp_attr->rnr_retry = hr_reg_read(&context, QPC_RNR_NUM_INIT);
5369 qp_attr->cur_qp_state = qp_attr->qp_state;
5370 qp_attr->cap.max_recv_wr = hr_qp->rq.wqe_cnt;
5371 qp_attr->cap.max_recv_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
5372 qp_attr->cap.max_inline_data = hr_qp->max_inline_data;
5374 qp_attr->cap.max_send_wr = hr_qp->sq.wqe_cnt;
5375 qp_attr->cap.max_send_sge = hr_qp->sq.max_gs;
5377 qp_init_attr->qp_context = ibqp->qp_context;
5378 qp_init_attr->qp_type = ibqp->qp_type;
5379 qp_init_attr->recv_cq = ibqp->recv_cq;
5380 qp_init_attr->send_cq = ibqp->send_cq;
5381 qp_init_attr->srq = ibqp->srq;
5382 qp_init_attr->cap = qp_attr->cap;
5383 qp_init_attr->sq_sig_type = hr_qp->sq_signal_bits;
5386 mutex_unlock(&hr_qp->mutex);
5390 static inline int modify_qp_is_ok(struct hns_roce_qp *hr_qp)
5392 return ((hr_qp->ibqp.qp_type == IB_QPT_RC ||
5393 hr_qp->ibqp.qp_type == IB_QPT_UD ||
5394 hr_qp->ibqp.qp_type == IB_QPT_XRC_INI ||
5395 hr_qp->ibqp.qp_type == IB_QPT_XRC_TGT) &&
5396 hr_qp->state != IB_QPS_RESET);
5399 static int hns_roce_v2_destroy_qp_common(struct hns_roce_dev *hr_dev,
5400 struct hns_roce_qp *hr_qp,
5401 struct ib_udata *udata)
5403 struct ib_device *ibdev = &hr_dev->ib_dev;
5404 struct hns_roce_cq *send_cq, *recv_cq;
5405 unsigned long flags;
5408 if (modify_qp_is_ok(hr_qp)) {
5409 /* Modify qp to reset before destroying qp */
5410 ret = hns_roce_v2_modify_qp(&hr_qp->ibqp, NULL, 0,
5411 hr_qp->state, IB_QPS_RESET, udata);
5414 "failed to modify QP to RST, ret = %d.\n",
5418 send_cq = hr_qp->ibqp.send_cq ? to_hr_cq(hr_qp->ibqp.send_cq) : NULL;
5419 recv_cq = hr_qp->ibqp.recv_cq ? to_hr_cq(hr_qp->ibqp.recv_cq) : NULL;
5421 spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
5422 hns_roce_lock_cqs(send_cq, recv_cq);
5426 __hns_roce_v2_cq_clean(recv_cq, hr_qp->qpn,
5428 to_hr_srq(hr_qp->ibqp.srq) :
5431 if (send_cq && send_cq != recv_cq)
5432 __hns_roce_v2_cq_clean(send_cq, hr_qp->qpn, NULL);
5435 hns_roce_qp_remove(hr_dev, hr_qp);
5437 hns_roce_unlock_cqs(send_cq, recv_cq);
5438 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
5443 int hns_roce_v2_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
5445 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
5446 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
5449 ret = hns_roce_v2_destroy_qp_common(hr_dev, hr_qp, udata);
5451 ibdev_err(&hr_dev->ib_dev,
5452 "failed to destroy QP, QPN = 0x%06lx, ret = %d.\n",
5455 hns_roce_qp_destroy(hr_dev, hr_qp, udata);
5460 static int hns_roce_v2_qp_flow_control_init(struct hns_roce_dev *hr_dev,
5461 struct hns_roce_qp *hr_qp)
5463 struct ib_device *ibdev = &hr_dev->ib_dev;
5464 struct hns_roce_sccc_clr_done *resp;
5465 struct hns_roce_sccc_clr *clr;
5466 struct hns_roce_cmq_desc desc;
5469 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
5472 mutex_lock(&hr_dev->qp_table.scc_mutex);
5474 /* set scc ctx clear done flag */
5475 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_RESET_SCCC, false);
5476 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
5478 ibdev_err(ibdev, "failed to reset SCC ctx, ret = %d.\n", ret);
5482 /* clear scc context */
5483 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CLR_SCCC, false);
5484 clr = (struct hns_roce_sccc_clr *)desc.data;
5485 clr->qpn = cpu_to_le32(hr_qp->qpn);
5486 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
5488 ibdev_err(ibdev, "failed to clear SCC ctx, ret = %d.\n", ret);
5492 /* query scc context clear is done or not */
5493 resp = (struct hns_roce_sccc_clr_done *)desc.data;
5494 for (i = 0; i <= HNS_ROCE_CMQ_SCC_CLR_DONE_CNT; i++) {
5495 hns_roce_cmq_setup_basic_desc(&desc,
5496 HNS_ROCE_OPC_QUERY_SCCC, true);
5497 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
5499 ibdev_err(ibdev, "failed to query clr cmq, ret = %d\n",
5510 ibdev_err(ibdev, "query SCC clr done flag overtime.\n");
5514 mutex_unlock(&hr_dev->qp_table.scc_mutex);
5518 #define DMA_IDX_SHIFT 3
5519 #define DMA_WQE_SHIFT 3
5521 static int hns_roce_v2_write_srqc_index_queue(struct hns_roce_srq *srq,
5522 struct hns_roce_srq_context *ctx)
5524 struct hns_roce_idx_que *idx_que = &srq->idx_que;
5525 struct ib_device *ibdev = srq->ibsrq.device;
5526 struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
5527 u64 mtts_idx[MTT_MIN_COUNT] = {};
5528 dma_addr_t dma_handle_idx = 0;
5531 /* Get physical address of idx que buf */
5532 ret = hns_roce_mtr_find(hr_dev, &idx_que->mtr, 0, mtts_idx,
5533 ARRAY_SIZE(mtts_idx), &dma_handle_idx);
5535 ibdev_err(ibdev, "failed to find mtr for SRQ idx, ret = %d.\n",
5540 hr_reg_write(ctx, SRQC_IDX_HOP_NUM,
5541 to_hr_hem_hopnum(hr_dev->caps.idx_hop_num, srq->wqe_cnt));
5543 hr_reg_write(ctx, SRQC_IDX_BT_BA_L, dma_handle_idx >> DMA_IDX_SHIFT);
5544 hr_reg_write(ctx, SRQC_IDX_BT_BA_H,
5545 upper_32_bits(dma_handle_idx >> DMA_IDX_SHIFT));
5547 hr_reg_write(ctx, SRQC_IDX_BA_PG_SZ,
5548 to_hr_hw_page_shift(idx_que->mtr.hem_cfg.ba_pg_shift));
5549 hr_reg_write(ctx, SRQC_IDX_BUF_PG_SZ,
5550 to_hr_hw_page_shift(idx_que->mtr.hem_cfg.buf_pg_shift));
5552 hr_reg_write(ctx, SRQC_IDX_CUR_BLK_ADDR_L,
5553 to_hr_hw_page_addr(mtts_idx[0]));
5554 hr_reg_write(ctx, SRQC_IDX_CUR_BLK_ADDR_H,
5555 upper_32_bits(to_hr_hw_page_addr(mtts_idx[0])));
5557 hr_reg_write(ctx, SRQC_IDX_NXT_BLK_ADDR_L,
5558 to_hr_hw_page_addr(mtts_idx[1]));
5559 hr_reg_write(ctx, SRQC_IDX_NXT_BLK_ADDR_H,
5560 upper_32_bits(to_hr_hw_page_addr(mtts_idx[1])));
5565 static int hns_roce_v2_write_srqc(struct hns_roce_srq *srq, void *mb_buf)
5567 struct ib_device *ibdev = srq->ibsrq.device;
5568 struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
5569 struct hns_roce_srq_context *ctx = mb_buf;
5570 u64 mtts_wqe[MTT_MIN_COUNT] = {};
5571 dma_addr_t dma_handle_wqe = 0;
5574 memset(ctx, 0, sizeof(*ctx));
5576 /* Get the physical address of srq buf */
5577 ret = hns_roce_mtr_find(hr_dev, &srq->buf_mtr, 0, mtts_wqe,
5578 ARRAY_SIZE(mtts_wqe), &dma_handle_wqe);
5580 ibdev_err(ibdev, "failed to find mtr for SRQ WQE, ret = %d.\n",
5585 hr_reg_write(ctx, SRQC_SRQ_ST, 1);
5586 hr_reg_write_bool(ctx, SRQC_SRQ_TYPE,
5587 srq->ibsrq.srq_type == IB_SRQT_XRC);
5588 hr_reg_write(ctx, SRQC_PD, to_hr_pd(srq->ibsrq.pd)->pdn);
5589 hr_reg_write(ctx, SRQC_SRQN, srq->srqn);
5590 hr_reg_write(ctx, SRQC_XRCD, srq->xrcdn);
5591 hr_reg_write(ctx, SRQC_XRC_CQN, srq->cqn);
5592 hr_reg_write(ctx, SRQC_SHIFT, ilog2(srq->wqe_cnt));
5593 hr_reg_write(ctx, SRQC_RQWS,
5594 srq->max_gs <= 0 ? 0 : fls(srq->max_gs - 1));
5596 hr_reg_write(ctx, SRQC_WQE_HOP_NUM,
5597 to_hr_hem_hopnum(hr_dev->caps.srqwqe_hop_num,
5600 hr_reg_write(ctx, SRQC_WQE_BT_BA_L, dma_handle_wqe >> DMA_WQE_SHIFT);
5601 hr_reg_write(ctx, SRQC_WQE_BT_BA_H,
5602 upper_32_bits(dma_handle_wqe >> DMA_WQE_SHIFT));
5604 hr_reg_write(ctx, SRQC_WQE_BA_PG_SZ,
5605 to_hr_hw_page_shift(srq->buf_mtr.hem_cfg.ba_pg_shift));
5606 hr_reg_write(ctx, SRQC_WQE_BUF_PG_SZ,
5607 to_hr_hw_page_shift(srq->buf_mtr.hem_cfg.buf_pg_shift));
5609 return hns_roce_v2_write_srqc_index_queue(srq, ctx);
5612 static int hns_roce_v2_modify_srq(struct ib_srq *ibsrq,
5613 struct ib_srq_attr *srq_attr,
5614 enum ib_srq_attr_mask srq_attr_mask,
5615 struct ib_udata *udata)
5617 struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device);
5618 struct hns_roce_srq *srq = to_hr_srq(ibsrq);
5619 struct hns_roce_srq_context *srq_context;
5620 struct hns_roce_srq_context *srqc_mask;
5621 struct hns_roce_cmd_mailbox *mailbox;
5624 /* Resizing SRQs is not supported yet */
5625 if (srq_attr_mask & IB_SRQ_MAX_WR)
5628 if (srq_attr_mask & IB_SRQ_LIMIT) {
5629 if (srq_attr->srq_limit > srq->wqe_cnt)
5632 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5633 if (IS_ERR(mailbox))
5634 return PTR_ERR(mailbox);
5636 srq_context = mailbox->buf;
5637 srqc_mask = (struct hns_roce_srq_context *)mailbox->buf + 1;
5639 memset(srqc_mask, 0xff, sizeof(*srqc_mask));
5641 hr_reg_write(srq_context, SRQC_LIMIT_WL, srq_attr->srq_limit);
5642 hr_reg_clear(srqc_mask, SRQC_LIMIT_WL);
5644 ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0,
5645 HNS_ROCE_CMD_MODIFY_SRQC, srq->srqn);
5646 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5648 ibdev_err(&hr_dev->ib_dev,
5649 "failed to handle cmd of modifying SRQ, ret = %d.\n",
5658 static int hns_roce_v2_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
5660 struct hns_roce_dev *hr_dev = to_hr_dev(ibsrq->device);
5661 struct hns_roce_srq *srq = to_hr_srq(ibsrq);
5662 struct hns_roce_srq_context *srq_context;
5663 struct hns_roce_cmd_mailbox *mailbox;
5666 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5667 if (IS_ERR(mailbox))
5668 return PTR_ERR(mailbox);
5670 srq_context = mailbox->buf;
5671 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma,
5672 HNS_ROCE_CMD_QUERY_SRQC, srq->srqn);
5674 ibdev_err(&hr_dev->ib_dev,
5675 "failed to process cmd of querying SRQ, ret = %d.\n",
5680 attr->srq_limit = hr_reg_read(srq_context, SRQC_LIMIT_WL);
5681 attr->max_wr = srq->wqe_cnt;
5682 attr->max_sge = srq->max_gs - srq->rsv_sge;
5685 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5689 static int hns_roce_v2_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
5691 struct hns_roce_dev *hr_dev = to_hr_dev(cq->device);
5692 struct hns_roce_v2_cq_context *cq_context;
5693 struct hns_roce_cq *hr_cq = to_hr_cq(cq);
5694 struct hns_roce_v2_cq_context *cqc_mask;
5695 struct hns_roce_cmd_mailbox *mailbox;
5698 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5699 if (IS_ERR(mailbox))
5700 return PTR_ERR(mailbox);
5702 cq_context = mailbox->buf;
5703 cqc_mask = (struct hns_roce_v2_cq_context *)mailbox->buf + 1;
5705 memset(cqc_mask, 0xff, sizeof(*cqc_mask));
5707 hr_reg_write(cq_context, CQC_CQ_MAX_CNT, cq_count);
5708 hr_reg_clear(cqc_mask, CQC_CQ_MAX_CNT);
5710 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
5711 if (cq_period * HNS_ROCE_CLOCK_ADJUST > USHRT_MAX) {
5712 dev_info(hr_dev->dev,
5713 "cq_period(%u) reached the upper limit, adjusted to 65.\n",
5715 cq_period = HNS_ROCE_MAX_CQ_PERIOD;
5717 cq_period *= HNS_ROCE_CLOCK_ADJUST;
5719 hr_reg_write(cq_context, CQC_CQ_PERIOD, cq_period);
5720 hr_reg_clear(cqc_mask, CQC_CQ_PERIOD);
5722 ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0,
5723 HNS_ROCE_CMD_MODIFY_CQC, hr_cq->cqn);
5724 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5726 ibdev_err(&hr_dev->ib_dev,
5727 "failed to process cmd when modifying CQ, ret = %d.\n",
5733 static int hns_roce_v2_query_cqc(struct hns_roce_dev *hr_dev, u32 cqn,
5736 struct hns_roce_v2_cq_context *context;
5737 struct hns_roce_cmd_mailbox *mailbox;
5740 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5741 if (IS_ERR(mailbox))
5742 return PTR_ERR(mailbox);
5744 context = mailbox->buf;
5745 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma,
5746 HNS_ROCE_CMD_QUERY_CQC, cqn);
5748 ibdev_err(&hr_dev->ib_dev,
5749 "failed to process cmd when querying CQ, ret = %d.\n",
5754 memcpy(buffer, context, sizeof(*context));
5757 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5762 static int hns_roce_v2_query_mpt(struct hns_roce_dev *hr_dev, u32 key,
5765 struct hns_roce_v2_mpt_entry *context;
5766 struct hns_roce_cmd_mailbox *mailbox;
5769 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
5770 if (IS_ERR(mailbox))
5771 return PTR_ERR(mailbox);
5773 context = mailbox->buf;
5774 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, HNS_ROCE_CMD_QUERY_MPT,
5775 key_to_hw_index(key));
5777 ibdev_err(&hr_dev->ib_dev,
5778 "failed to process cmd when querying MPT, ret = %d.\n",
5783 memcpy(buffer, context, sizeof(*context));
5786 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
5791 static void hns_roce_irq_work_handle(struct work_struct *work)
5793 struct hns_roce_work *irq_work =
5794 container_of(work, struct hns_roce_work, work);
5795 struct ib_device *ibdev = &irq_work->hr_dev->ib_dev;
5797 switch (irq_work->event_type) {
5798 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
5799 ibdev_info(ibdev, "path migrated succeeded.\n");
5801 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
5802 ibdev_warn(ibdev, "path migration failed.\n");
5804 case HNS_ROCE_EVENT_TYPE_COMM_EST:
5806 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
5807 ibdev_dbg(ibdev, "send queue drained.\n");
5809 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
5810 ibdev_err(ibdev, "local work queue 0x%x catast error, sub_event type is: %d\n",
5811 irq_work->queue_num, irq_work->sub_type);
5813 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
5814 ibdev_err(ibdev, "invalid request local work queue 0x%x error.\n",
5815 irq_work->queue_num);
5817 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
5818 ibdev_err(ibdev, "local access violation work queue 0x%x error, sub_event type is: %d\n",
5819 irq_work->queue_num, irq_work->sub_type);
5821 case HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH:
5822 ibdev_dbg(ibdev, "SRQ limit reach.\n");
5824 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
5825 ibdev_dbg(ibdev, "SRQ last wqe reach.\n");
5827 case HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR:
5828 ibdev_err(ibdev, "SRQ catas error.\n");
5830 case HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR:
5831 ibdev_err(ibdev, "CQ 0x%x access err.\n", irq_work->queue_num);
5833 case HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW:
5834 ibdev_warn(ibdev, "CQ 0x%x overflow\n", irq_work->queue_num);
5836 case HNS_ROCE_EVENT_TYPE_DB_OVERFLOW:
5837 ibdev_warn(ibdev, "DB overflow.\n");
5839 case HNS_ROCE_EVENT_TYPE_FLR:
5840 ibdev_warn(ibdev, "function level reset.\n");
5842 case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
5843 ibdev_err(ibdev, "xrc domain violation error.\n");
5845 case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
5846 ibdev_err(ibdev, "invalid xrceth error.\n");
5855 static void hns_roce_v2_init_irq_work(struct hns_roce_dev *hr_dev,
5856 struct hns_roce_eq *eq, u32 queue_num)
5858 struct hns_roce_work *irq_work;
5860 irq_work = kzalloc(sizeof(struct hns_roce_work), GFP_ATOMIC);
5864 INIT_WORK(&irq_work->work, hns_roce_irq_work_handle);
5865 irq_work->hr_dev = hr_dev;
5866 irq_work->event_type = eq->event_type;
5867 irq_work->sub_type = eq->sub_type;
5868 irq_work->queue_num = queue_num;
5869 queue_work(hr_dev->irq_workq, &irq_work->work);
5872 static void update_eq_db(struct hns_roce_eq *eq)
5874 struct hns_roce_dev *hr_dev = eq->hr_dev;
5875 struct hns_roce_v2_db eq_db = {};
5877 if (eq->type_flag == HNS_ROCE_AEQ) {
5878 hr_reg_write(&eq_db, EQ_DB_CMD,
5879 eq->arm_st == HNS_ROCE_V2_EQ_ALWAYS_ARMED ?
5880 HNS_ROCE_EQ_DB_CMD_AEQ :
5881 HNS_ROCE_EQ_DB_CMD_AEQ_ARMED);
5883 hr_reg_write(&eq_db, EQ_DB_TAG, eq->eqn);
5885 hr_reg_write(&eq_db, EQ_DB_CMD,
5886 eq->arm_st == HNS_ROCE_V2_EQ_ALWAYS_ARMED ?
5887 HNS_ROCE_EQ_DB_CMD_CEQ :
5888 HNS_ROCE_EQ_DB_CMD_CEQ_ARMED);
5891 hr_reg_write(&eq_db, EQ_DB_CI, eq->cons_index);
5893 hns_roce_write64(hr_dev, (__le32 *)&eq_db, eq->db_reg);
5896 static struct hns_roce_aeqe *next_aeqe_sw_v2(struct hns_roce_eq *eq)
5898 struct hns_roce_aeqe *aeqe;
5900 aeqe = hns_roce_buf_offset(eq->mtr.kmem,
5901 (eq->cons_index & (eq->entries - 1)) *
5904 return (hr_reg_read(aeqe, AEQE_OWNER) ^
5905 !!(eq->cons_index & eq->entries)) ? aeqe : NULL;
5908 static irqreturn_t hns_roce_v2_aeq_int(struct hns_roce_dev *hr_dev,
5909 struct hns_roce_eq *eq)
5911 struct device *dev = hr_dev->dev;
5912 struct hns_roce_aeqe *aeqe = next_aeqe_sw_v2(eq);
5913 irqreturn_t aeqe_found = IRQ_NONE;
5919 /* Make sure we read AEQ entry after we have checked the
5924 event_type = hr_reg_read(aeqe, AEQE_EVENT_TYPE);
5925 sub_type = hr_reg_read(aeqe, AEQE_SUB_TYPE);
5926 queue_num = hr_reg_read(aeqe, AEQE_EVENT_QUEUE_NUM);
5928 switch (event_type) {
5929 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
5930 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
5931 case HNS_ROCE_EVENT_TYPE_COMM_EST:
5932 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
5933 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
5934 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
5935 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
5936 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
5937 case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
5938 case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
5939 hns_roce_qp_event(hr_dev, queue_num, event_type);
5941 case HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH:
5942 case HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR:
5943 hns_roce_srq_event(hr_dev, queue_num, event_type);
5945 case HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR:
5946 case HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW:
5947 hns_roce_cq_event(hr_dev, queue_num, event_type);
5949 case HNS_ROCE_EVENT_TYPE_MB:
5950 hns_roce_cmd_event(hr_dev,
5951 le16_to_cpu(aeqe->event.cmd.token),
5952 aeqe->event.cmd.status,
5953 le64_to_cpu(aeqe->event.cmd.out_param));
5955 case HNS_ROCE_EVENT_TYPE_DB_OVERFLOW:
5956 case HNS_ROCE_EVENT_TYPE_FLR:
5959 dev_err(dev, "unhandled event %d on EQ %d at idx %u.\n",
5960 event_type, eq->eqn, eq->cons_index);
5964 eq->event_type = event_type;
5965 eq->sub_type = sub_type;
5967 aeqe_found = IRQ_HANDLED;
5969 hns_roce_v2_init_irq_work(hr_dev, eq, queue_num);
5971 aeqe = next_aeqe_sw_v2(eq);
5976 return IRQ_RETVAL(aeqe_found);
5979 static struct hns_roce_ceqe *next_ceqe_sw_v2(struct hns_roce_eq *eq)
5981 struct hns_roce_ceqe *ceqe;
5983 ceqe = hns_roce_buf_offset(eq->mtr.kmem,
5984 (eq->cons_index & (eq->entries - 1)) *
5987 return (hr_reg_read(ceqe, CEQE_OWNER) ^
5988 !!(eq->cons_index & eq->entries)) ? ceqe : NULL;
5991 static irqreturn_t hns_roce_v2_ceq_int(struct hns_roce_dev *hr_dev,
5992 struct hns_roce_eq *eq)
5994 struct hns_roce_ceqe *ceqe = next_ceqe_sw_v2(eq);
5995 irqreturn_t ceqe_found = IRQ_NONE;
5999 /* Make sure we read CEQ entry after we have checked the
6004 cqn = hr_reg_read(ceqe, CEQE_CQN);
6006 hns_roce_cq_completion(hr_dev, cqn);
6009 ceqe_found = IRQ_HANDLED;
6011 ceqe = next_ceqe_sw_v2(eq);
6016 return IRQ_RETVAL(ceqe_found);
6019 static irqreturn_t hns_roce_v2_msix_interrupt_eq(int irq, void *eq_ptr)
6021 struct hns_roce_eq *eq = eq_ptr;
6022 struct hns_roce_dev *hr_dev = eq->hr_dev;
6023 irqreturn_t int_work;
6025 if (eq->type_flag == HNS_ROCE_CEQ)
6026 /* Completion event interrupt */
6027 int_work = hns_roce_v2_ceq_int(hr_dev, eq);
6029 /* Asynchronous event interrupt */
6030 int_work = hns_roce_v2_aeq_int(hr_dev, eq);
6032 return IRQ_RETVAL(int_work);
6035 static irqreturn_t abnormal_interrupt_basic(struct hns_roce_dev *hr_dev,
6038 struct pci_dev *pdev = hr_dev->pci_dev;
6039 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
6040 const struct hnae3_ae_ops *ops = ae_dev->ops;
6041 irqreturn_t int_work = IRQ_NONE;
6044 int_en = roce_read(hr_dev, ROCEE_VF_ABN_INT_EN_REG);
6046 if (int_st & BIT(HNS_ROCE_V2_VF_INT_ST_AEQ_OVERFLOW_S)) {
6047 dev_err(hr_dev->dev, "AEQ overflow!\n");
6049 roce_write(hr_dev, ROCEE_VF_ABN_INT_ST_REG,
6050 1 << HNS_ROCE_V2_VF_INT_ST_AEQ_OVERFLOW_S);
6052 /* Set reset level for reset_event() */
6053 if (ops->set_default_reset_request)
6054 ops->set_default_reset_request(ae_dev,
6056 if (ops->reset_event)
6057 ops->reset_event(pdev, NULL);
6059 int_en |= 1 << HNS_ROCE_V2_VF_ABN_INT_EN_S;
6060 roce_write(hr_dev, ROCEE_VF_ABN_INT_EN_REG, int_en);
6062 int_work = IRQ_HANDLED;
6064 dev_err(hr_dev->dev, "there is no basic abn irq found.\n");
6067 return IRQ_RETVAL(int_work);
6070 static int fmea_ram_ecc_query(struct hns_roce_dev *hr_dev,
6071 struct fmea_ram_ecc *ecc_info)
6073 struct hns_roce_cmq_desc desc;
6074 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
6077 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_QUERY_RAM_ECC, true);
6078 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
6082 ecc_info->is_ecc_err = hr_reg_read(req, QUERY_RAM_ECC_1BIT_ERR);
6083 ecc_info->res_type = hr_reg_read(req, QUERY_RAM_ECC_RES_TYPE);
6084 ecc_info->index = hr_reg_read(req, QUERY_RAM_ECC_TAG);
6089 static int fmea_recover_gmv(struct hns_roce_dev *hr_dev, u32 idx)
6091 struct hns_roce_cmq_desc desc;
6092 struct hns_roce_cmq_req *req = (struct hns_roce_cmq_req *)desc.data;
6097 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GMV_BT, true);
6098 hr_reg_write(req, CFG_GMV_BT_IDX, idx);
6100 ret = hns_roce_cmq_send(hr_dev, &desc, 1);
6102 dev_err(hr_dev->dev,
6103 "failed to execute cmd to read gmv, ret = %d.\n", ret);
6107 addr_low = hr_reg_read(req, CFG_GMV_BT_BA_L);
6108 addr_upper = hr_reg_read(req, CFG_GMV_BT_BA_H);
6110 hns_roce_cmq_setup_basic_desc(&desc, HNS_ROCE_OPC_CFG_GMV_BT, false);
6111 hr_reg_write(req, CFG_GMV_BT_BA_L, addr_low);
6112 hr_reg_write(req, CFG_GMV_BT_BA_H, addr_upper);
6113 hr_reg_write(req, CFG_GMV_BT_IDX, idx);
6115 return hns_roce_cmq_send(hr_dev, &desc, 1);
6118 static u64 fmea_get_ram_res_addr(u32 res_type, __le64 *data)
6120 if (res_type == ECC_RESOURCE_QPC_TIMER ||
6121 res_type == ECC_RESOURCE_CQC_TIMER ||
6122 res_type == ECC_RESOURCE_SCCC)
6123 return le64_to_cpu(*data);
6125 return le64_to_cpu(*data) << PAGE_SHIFT;
6128 static int fmea_recover_others(struct hns_roce_dev *hr_dev, u32 res_type,
6131 u8 write_bt0_op = fmea_ram_res[res_type].write_bt0_op;
6132 u8 read_bt0_op = fmea_ram_res[res_type].read_bt0_op;
6133 struct hns_roce_cmd_mailbox *mailbox;
6137 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
6138 if (IS_ERR(mailbox))
6139 return PTR_ERR(mailbox);
6141 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, read_bt0_op, index);
6143 dev_err(hr_dev->dev,
6144 "failed to execute cmd to read fmea ram, ret = %d.\n",
6149 addr = fmea_get_ram_res_addr(res_type, mailbox->buf);
6151 ret = hns_roce_cmd_mbox(hr_dev, addr, 0, write_bt0_op, index);
6153 dev_err(hr_dev->dev,
6154 "failed to execute cmd to write fmea ram, ret = %d.\n",
6158 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
6162 static void fmea_ram_ecc_recover(struct hns_roce_dev *hr_dev,
6163 struct fmea_ram_ecc *ecc_info)
6165 u32 res_type = ecc_info->res_type;
6166 u32 index = ecc_info->index;
6169 BUILD_BUG_ON(ARRAY_SIZE(fmea_ram_res) != ECC_RESOURCE_COUNT);
6171 if (res_type >= ECC_RESOURCE_COUNT) {
6172 dev_err(hr_dev->dev, "unsupported fmea ram ecc type %u.\n",
6177 if (res_type == ECC_RESOURCE_GMV)
6178 ret = fmea_recover_gmv(hr_dev, index);
6180 ret = fmea_recover_others(hr_dev, res_type, index);
6182 dev_err(hr_dev->dev,
6183 "failed to recover %s, index = %u, ret = %d.\n",
6184 fmea_ram_res[res_type].name, index, ret);
6187 static void fmea_ram_ecc_work(struct work_struct *ecc_work)
6189 struct hns_roce_dev *hr_dev =
6190 container_of(ecc_work, struct hns_roce_dev, ecc_work);
6191 struct fmea_ram_ecc ecc_info = {};
6193 if (fmea_ram_ecc_query(hr_dev, &ecc_info)) {
6194 dev_err(hr_dev->dev, "failed to query fmea ram ecc.\n");
6198 if (!ecc_info.is_ecc_err) {
6199 dev_err(hr_dev->dev, "there is no fmea ram ecc err found.\n");
6203 fmea_ram_ecc_recover(hr_dev, &ecc_info);
6206 static irqreturn_t hns_roce_v2_msix_interrupt_abn(int irq, void *dev_id)
6208 struct hns_roce_dev *hr_dev = dev_id;
6209 irqreturn_t int_work = IRQ_NONE;
6212 int_st = roce_read(hr_dev, ROCEE_VF_ABN_INT_ST_REG);
6215 int_work = abnormal_interrupt_basic(hr_dev, int_st);
6216 } else if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) {
6217 queue_work(hr_dev->irq_workq, &hr_dev->ecc_work);
6218 int_work = IRQ_HANDLED;
6220 dev_err(hr_dev->dev, "there is no abnormal irq found.\n");
6223 return IRQ_RETVAL(int_work);
6226 static void hns_roce_v2_int_mask_enable(struct hns_roce_dev *hr_dev,
6227 int eq_num, u32 enable_flag)
6231 for (i = 0; i < eq_num; i++)
6232 roce_write(hr_dev, ROCEE_VF_EVENT_INT_EN_REG +
6233 i * EQ_REG_OFFSET, enable_flag);
6235 roce_write(hr_dev, ROCEE_VF_ABN_INT_EN_REG, enable_flag);
6236 roce_write(hr_dev, ROCEE_VF_ABN_INT_CFG_REG, enable_flag);
6239 static void hns_roce_v2_destroy_eqc(struct hns_roce_dev *hr_dev, u32 eqn)
6241 struct device *dev = hr_dev->dev;
6245 if (eqn < hr_dev->caps.num_comp_vectors)
6246 cmd = HNS_ROCE_CMD_DESTROY_CEQC;
6248 cmd = HNS_ROCE_CMD_DESTROY_AEQC;
6250 ret = hns_roce_destroy_hw_ctx(hr_dev, cmd, eqn & HNS_ROCE_V2_EQN_M);
6252 dev_err(dev, "[mailbox cmd] destroy eqc(%u) failed.\n", eqn);
6255 static void free_eq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq)
6257 hns_roce_mtr_destroy(hr_dev, &eq->mtr);
6260 static void init_eq_config(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq)
6262 eq->db_reg = hr_dev->reg_base + ROCEE_VF_EQ_DB_CFG0_REG;
6264 eq->over_ignore = HNS_ROCE_V2_EQ_OVER_IGNORE_0;
6265 eq->coalesce = HNS_ROCE_V2_EQ_COALESCE_0;
6266 eq->arm_st = HNS_ROCE_V2_EQ_ALWAYS_ARMED;
6267 eq->shift = ilog2((unsigned int)eq->entries);
6270 static int config_eqc(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq,
6273 u64 eqe_ba[MTT_MIN_COUNT] = { 0 };
6274 struct hns_roce_eq_context *eqc;
6279 memset(eqc, 0, sizeof(struct hns_roce_eq_context));
6281 init_eq_config(hr_dev, eq);
6283 /* if not multi-hop, eqe buffer only use one trunk */
6284 count = hns_roce_mtr_find(hr_dev, &eq->mtr, 0, eqe_ba, MTT_MIN_COUNT,
6287 dev_err(hr_dev->dev, "failed to find EQE mtr\n");
6291 hr_reg_write(eqc, EQC_EQ_ST, HNS_ROCE_V2_EQ_STATE_VALID);
6292 hr_reg_write(eqc, EQC_EQE_HOP_NUM, eq->hop_num);
6293 hr_reg_write(eqc, EQC_OVER_IGNORE, eq->over_ignore);
6294 hr_reg_write(eqc, EQC_COALESCE, eq->coalesce);
6295 hr_reg_write(eqc, EQC_ARM_ST, eq->arm_st);
6296 hr_reg_write(eqc, EQC_EQN, eq->eqn);
6297 hr_reg_write(eqc, EQC_EQE_CNT, HNS_ROCE_EQ_INIT_EQE_CNT);
6298 hr_reg_write(eqc, EQC_EQE_BA_PG_SZ,
6299 to_hr_hw_page_shift(eq->mtr.hem_cfg.ba_pg_shift));
6300 hr_reg_write(eqc, EQC_EQE_BUF_PG_SZ,
6301 to_hr_hw_page_shift(eq->mtr.hem_cfg.buf_pg_shift));
6302 hr_reg_write(eqc, EQC_EQ_PROD_INDX, HNS_ROCE_EQ_INIT_PROD_IDX);
6303 hr_reg_write(eqc, EQC_EQ_MAX_CNT, eq->eq_max_cnt);
6305 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
6306 if (eq->eq_period * HNS_ROCE_CLOCK_ADJUST > USHRT_MAX) {
6307 dev_info(hr_dev->dev, "eq_period(%u) reached the upper limit, adjusted to 65.\n",
6309 eq->eq_period = HNS_ROCE_MAX_EQ_PERIOD;
6311 eq->eq_period *= HNS_ROCE_CLOCK_ADJUST;
6314 hr_reg_write(eqc, EQC_EQ_PERIOD, eq->eq_period);
6315 hr_reg_write(eqc, EQC_EQE_REPORT_TIMER, HNS_ROCE_EQ_INIT_REPORT_TIMER);
6316 hr_reg_write(eqc, EQC_EQE_BA_L, bt_ba >> 3);
6317 hr_reg_write(eqc, EQC_EQE_BA_H, bt_ba >> 35);
6318 hr_reg_write(eqc, EQC_SHIFT, eq->shift);
6319 hr_reg_write(eqc, EQC_MSI_INDX, HNS_ROCE_EQ_INIT_MSI_IDX);
6320 hr_reg_write(eqc, EQC_CUR_EQE_BA_L, eqe_ba[0] >> 12);
6321 hr_reg_write(eqc, EQC_CUR_EQE_BA_M, eqe_ba[0] >> 28);
6322 hr_reg_write(eqc, EQC_CUR_EQE_BA_H, eqe_ba[0] >> 60);
6323 hr_reg_write(eqc, EQC_EQ_CONS_INDX, HNS_ROCE_EQ_INIT_CONS_IDX);
6324 hr_reg_write(eqc, EQC_NEX_EQE_BA_L, eqe_ba[1] >> 12);
6325 hr_reg_write(eqc, EQC_NEX_EQE_BA_H, eqe_ba[1] >> 44);
6326 hr_reg_write(eqc, EQC_EQE_SIZE, eq->eqe_size == HNS_ROCE_V3_EQE_SIZE);
6331 static int alloc_eq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_eq *eq)
6333 struct hns_roce_buf_attr buf_attr = {};
6336 if (hr_dev->caps.eqe_hop_num == HNS_ROCE_HOP_NUM_0)
6339 eq->hop_num = hr_dev->caps.eqe_hop_num;
6341 buf_attr.page_shift = hr_dev->caps.eqe_buf_pg_sz + PAGE_SHIFT;
6342 buf_attr.region[0].size = eq->entries * eq->eqe_size;
6343 buf_attr.region[0].hopnum = eq->hop_num;
6344 buf_attr.region_count = 1;
6346 err = hns_roce_mtr_create(hr_dev, &eq->mtr, &buf_attr,
6347 hr_dev->caps.eqe_ba_pg_sz + PAGE_SHIFT, NULL,
6350 dev_err(hr_dev->dev, "failed to alloc EQE mtr, err %d\n", err);
6355 static int hns_roce_v2_create_eq(struct hns_roce_dev *hr_dev,
6356 struct hns_roce_eq *eq, u8 eq_cmd)
6358 struct hns_roce_cmd_mailbox *mailbox;
6361 /* Allocate mailbox memory */
6362 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
6363 if (IS_ERR(mailbox))
6364 return PTR_ERR(mailbox);
6366 ret = alloc_eq_buf(hr_dev, eq);
6370 ret = config_eqc(hr_dev, eq, mailbox->buf);
6374 ret = hns_roce_create_hw_ctx(hr_dev, mailbox, eq_cmd, eq->eqn);
6376 dev_err(hr_dev->dev, "[mailbox cmd] create eqc failed.\n");
6380 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
6385 free_eq_buf(hr_dev, eq);
6388 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
6393 static int __hns_roce_request_irq(struct hns_roce_dev *hr_dev, int irq_num,
6394 int comp_num, int aeq_num, int other_num)
6396 struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
6400 for (i = 0; i < irq_num; i++) {
6401 hr_dev->irq_names[i] = kzalloc(HNS_ROCE_INT_NAME_LEN,
6403 if (!hr_dev->irq_names[i]) {
6405 goto err_kzalloc_failed;
6409 /* irq contains: abnormal + AEQ + CEQ */
6410 for (j = 0; j < other_num; j++)
6411 snprintf((char *)hr_dev->irq_names[j], HNS_ROCE_INT_NAME_LEN,
6414 for (j = other_num; j < (other_num + aeq_num); j++)
6415 snprintf((char *)hr_dev->irq_names[j], HNS_ROCE_INT_NAME_LEN,
6416 "hns-aeq-%d", j - other_num);
6418 for (j = (other_num + aeq_num); j < irq_num; j++)
6419 snprintf((char *)hr_dev->irq_names[j], HNS_ROCE_INT_NAME_LEN,
6420 "hns-ceq-%d", j - other_num - aeq_num);
6422 for (j = 0; j < irq_num; j++) {
6424 ret = request_irq(hr_dev->irq[j],
6425 hns_roce_v2_msix_interrupt_abn,
6426 0, hr_dev->irq_names[j], hr_dev);
6428 else if (j < (other_num + comp_num))
6429 ret = request_irq(eq_table->eq[j - other_num].irq,
6430 hns_roce_v2_msix_interrupt_eq,
6431 0, hr_dev->irq_names[j + aeq_num],
6432 &eq_table->eq[j - other_num]);
6434 ret = request_irq(eq_table->eq[j - other_num].irq,
6435 hns_roce_v2_msix_interrupt_eq,
6436 0, hr_dev->irq_names[j - comp_num],
6437 &eq_table->eq[j - other_num]);
6439 dev_err(hr_dev->dev, "request irq error!\n");
6440 goto err_request_failed;
6447 for (j -= 1; j >= 0; j--)
6449 free_irq(hr_dev->irq[j], hr_dev);
6451 free_irq(eq_table->eq[j - other_num].irq,
6452 &eq_table->eq[j - other_num]);
6455 for (i -= 1; i >= 0; i--)
6456 kfree(hr_dev->irq_names[i]);
6461 static void __hns_roce_free_irq(struct hns_roce_dev *hr_dev)
6467 eq_num = hr_dev->caps.num_comp_vectors + hr_dev->caps.num_aeq_vectors;
6468 irq_num = eq_num + hr_dev->caps.num_other_vectors;
6470 for (i = 0; i < hr_dev->caps.num_other_vectors; i++)
6471 free_irq(hr_dev->irq[i], hr_dev);
6473 for (i = 0; i < eq_num; i++)
6474 free_irq(hr_dev->eq_table.eq[i].irq, &hr_dev->eq_table.eq[i]);
6476 for (i = 0; i < irq_num; i++)
6477 kfree(hr_dev->irq_names[i]);
6480 static int hns_roce_v2_init_eq_table(struct hns_roce_dev *hr_dev)
6482 struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
6483 struct device *dev = hr_dev->dev;
6484 struct hns_roce_eq *eq;
6494 other_num = hr_dev->caps.num_other_vectors;
6495 comp_num = hr_dev->caps.num_comp_vectors;
6496 aeq_num = hr_dev->caps.num_aeq_vectors;
6498 eq_num = comp_num + aeq_num;
6499 irq_num = eq_num + other_num;
6501 eq_table->eq = kcalloc(eq_num, sizeof(*eq_table->eq), GFP_KERNEL);
6506 for (i = 0; i < eq_num; i++) {
6507 eq = &eq_table->eq[i];
6508 eq->hr_dev = hr_dev;
6512 eq_cmd = HNS_ROCE_CMD_CREATE_CEQC;
6513 eq->type_flag = HNS_ROCE_CEQ;
6514 eq->entries = hr_dev->caps.ceqe_depth;
6515 eq->eqe_size = hr_dev->caps.ceqe_size;
6516 eq->irq = hr_dev->irq[i + other_num + aeq_num];
6517 eq->eq_max_cnt = HNS_ROCE_CEQ_DEFAULT_BURST_NUM;
6518 eq->eq_period = HNS_ROCE_CEQ_DEFAULT_INTERVAL;
6521 eq_cmd = HNS_ROCE_CMD_CREATE_AEQC;
6522 eq->type_flag = HNS_ROCE_AEQ;
6523 eq->entries = hr_dev->caps.aeqe_depth;
6524 eq->eqe_size = hr_dev->caps.aeqe_size;
6525 eq->irq = hr_dev->irq[i - comp_num + other_num];
6526 eq->eq_max_cnt = HNS_ROCE_AEQ_DEFAULT_BURST_NUM;
6527 eq->eq_period = HNS_ROCE_AEQ_DEFAULT_INTERVAL;
6530 ret = hns_roce_v2_create_eq(hr_dev, eq, eq_cmd);
6532 dev_err(dev, "failed to create eq.\n");
6533 goto err_create_eq_fail;
6537 INIT_WORK(&hr_dev->ecc_work, fmea_ram_ecc_work);
6539 hr_dev->irq_workq = alloc_ordered_workqueue("hns_roce_irq_workq", 0);
6540 if (!hr_dev->irq_workq) {
6541 dev_err(dev, "failed to create irq workqueue.\n");
6543 goto err_create_eq_fail;
6546 ret = __hns_roce_request_irq(hr_dev, irq_num, comp_num, aeq_num,
6549 dev_err(dev, "failed to request irq.\n");
6550 goto err_request_irq_fail;
6554 hns_roce_v2_int_mask_enable(hr_dev, eq_num, EQ_ENABLE);
6558 err_request_irq_fail:
6559 destroy_workqueue(hr_dev->irq_workq);
6562 for (i -= 1; i >= 0; i--)
6563 free_eq_buf(hr_dev, &eq_table->eq[i]);
6564 kfree(eq_table->eq);
6569 static void hns_roce_v2_cleanup_eq_table(struct hns_roce_dev *hr_dev)
6571 struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
6575 eq_num = hr_dev->caps.num_comp_vectors + hr_dev->caps.num_aeq_vectors;
6578 hns_roce_v2_int_mask_enable(hr_dev, eq_num, EQ_DISABLE);
6580 __hns_roce_free_irq(hr_dev);
6581 destroy_workqueue(hr_dev->irq_workq);
6583 for (i = 0; i < eq_num; i++) {
6584 hns_roce_v2_destroy_eqc(hr_dev, i);
6586 free_eq_buf(hr_dev, &eq_table->eq[i]);
6589 kfree(eq_table->eq);
6592 static const struct ib_device_ops hns_roce_v2_dev_ops = {
6593 .destroy_qp = hns_roce_v2_destroy_qp,
6594 .modify_cq = hns_roce_v2_modify_cq,
6595 .poll_cq = hns_roce_v2_poll_cq,
6596 .post_recv = hns_roce_v2_post_recv,
6597 .post_send = hns_roce_v2_post_send,
6598 .query_qp = hns_roce_v2_query_qp,
6599 .req_notify_cq = hns_roce_v2_req_notify_cq,
6602 static const struct ib_device_ops hns_roce_v2_dev_srq_ops = {
6603 .modify_srq = hns_roce_v2_modify_srq,
6604 .post_srq_recv = hns_roce_v2_post_srq_recv,
6605 .query_srq = hns_roce_v2_query_srq,
6608 static const struct hns_roce_hw hns_roce_hw_v2 = {
6609 .cmq_init = hns_roce_v2_cmq_init,
6610 .cmq_exit = hns_roce_v2_cmq_exit,
6611 .hw_profile = hns_roce_v2_profile,
6612 .hw_init = hns_roce_v2_init,
6613 .hw_exit = hns_roce_v2_exit,
6614 .post_mbox = v2_post_mbox,
6615 .poll_mbox_done = v2_poll_mbox_done,
6616 .chk_mbox_avail = v2_chk_mbox_is_avail,
6617 .set_gid = hns_roce_v2_set_gid,
6618 .set_mac = hns_roce_v2_set_mac,
6619 .write_mtpt = hns_roce_v2_write_mtpt,
6620 .rereg_write_mtpt = hns_roce_v2_rereg_write_mtpt,
6621 .frmr_write_mtpt = hns_roce_v2_frmr_write_mtpt,
6622 .mw_write_mtpt = hns_roce_v2_mw_write_mtpt,
6623 .write_cqc = hns_roce_v2_write_cqc,
6624 .set_hem = hns_roce_v2_set_hem,
6625 .clear_hem = hns_roce_v2_clear_hem,
6626 .modify_qp = hns_roce_v2_modify_qp,
6627 .dereg_mr = hns_roce_v2_dereg_mr,
6628 .qp_flow_control_init = hns_roce_v2_qp_flow_control_init,
6629 .init_eq = hns_roce_v2_init_eq_table,
6630 .cleanup_eq = hns_roce_v2_cleanup_eq_table,
6631 .write_srqc = hns_roce_v2_write_srqc,
6632 .query_cqc = hns_roce_v2_query_cqc,
6633 .query_qpc = hns_roce_v2_query_qpc,
6634 .query_mpt = hns_roce_v2_query_mpt,
6635 .query_hw_counter = hns_roce_hw_v2_query_counter,
6636 .hns_roce_dev_ops = &hns_roce_v2_dev_ops,
6637 .hns_roce_dev_srq_ops = &hns_roce_v2_dev_srq_ops,
6640 static const struct pci_device_id hns_roce_hw_v2_pci_tbl[] = {
6641 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA), 0},
6642 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC), 0},
6643 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA), 0},
6644 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC), 0},
6645 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC), 0},
6646 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_200G_RDMA), 0},
6647 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_RDMA_DCB_PFC_VF),
6648 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
6649 /* required last entry */
6653 MODULE_DEVICE_TABLE(pci, hns_roce_hw_v2_pci_tbl);
6655 static void hns_roce_hw_v2_get_cfg(struct hns_roce_dev *hr_dev,
6656 struct hnae3_handle *handle)
6658 struct hns_roce_v2_priv *priv = hr_dev->priv;
6659 const struct pci_device_id *id;
6662 hr_dev->pci_dev = handle->pdev;
6663 id = pci_match_id(hns_roce_hw_v2_pci_tbl, hr_dev->pci_dev);
6664 hr_dev->is_vf = id->driver_data;
6665 hr_dev->dev = &handle->pdev->dev;
6666 hr_dev->hw = &hns_roce_hw_v2;
6667 hr_dev->sdb_offset = ROCEE_DB_SQ_L_0_REG;
6668 hr_dev->odb_offset = hr_dev->sdb_offset;
6670 /* Get info from NIC driver. */
6671 hr_dev->reg_base = handle->rinfo.roce_io_base;
6672 hr_dev->mem_base = handle->rinfo.roce_mem_base;
6673 hr_dev->caps.num_ports = 1;
6674 hr_dev->iboe.netdevs[0] = handle->rinfo.netdev;
6675 hr_dev->iboe.phy_port[0] = 0;
6677 addrconf_addr_eui48((u8 *)&hr_dev->ib_dev.node_guid,
6678 hr_dev->iboe.netdevs[0]->dev_addr);
6680 for (i = 0; i < handle->rinfo.num_vectors; i++)
6681 hr_dev->irq[i] = pci_irq_vector(handle->pdev,
6682 i + handle->rinfo.base_vector);
6684 /* cmd issue mode: 0 is poll, 1 is event */
6685 hr_dev->cmd_mod = 1;
6686 hr_dev->loop_idc = 0;
6688 hr_dev->reset_cnt = handle->ae_algo->ops->ae_dev_reset_cnt(handle);
6689 priv->handle = handle;
6692 static int __hns_roce_hw_v2_init_instance(struct hnae3_handle *handle)
6694 struct hns_roce_dev *hr_dev;
6697 hr_dev = ib_alloc_device(hns_roce_dev, ib_dev);
6701 hr_dev->priv = kzalloc(sizeof(struct hns_roce_v2_priv), GFP_KERNEL);
6702 if (!hr_dev->priv) {
6704 goto error_failed_kzalloc;
6707 hns_roce_hw_v2_get_cfg(hr_dev, handle);
6709 ret = hns_roce_init(hr_dev);
6711 dev_err(hr_dev->dev, "RoCE Engine init failed!\n");
6712 goto error_failed_roce_init;
6715 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08) {
6716 ret = free_mr_init(hr_dev);
6718 dev_err(hr_dev->dev, "failed to init free mr!\n");
6719 goto error_failed_free_mr_init;
6723 handle->priv = hr_dev;
6727 error_failed_free_mr_init:
6728 hns_roce_exit(hr_dev);
6730 error_failed_roce_init:
6731 kfree(hr_dev->priv);
6733 error_failed_kzalloc:
6734 ib_dealloc_device(&hr_dev->ib_dev);
6739 static void __hns_roce_hw_v2_uninit_instance(struct hnae3_handle *handle,
6742 struct hns_roce_dev *hr_dev = handle->priv;
6747 handle->priv = NULL;
6749 hr_dev->state = HNS_ROCE_DEVICE_STATE_UNINIT;
6750 hns_roce_handle_device_err(hr_dev);
6752 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08)
6753 free_mr_exit(hr_dev);
6755 hns_roce_exit(hr_dev);
6756 kfree(hr_dev->priv);
6757 ib_dealloc_device(&hr_dev->ib_dev);
6760 static int hns_roce_hw_v2_init_instance(struct hnae3_handle *handle)
6762 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
6763 const struct pci_device_id *id;
6764 struct device *dev = &handle->pdev->dev;
6767 handle->rinfo.instance_state = HNS_ROCE_STATE_INIT;
6769 if (ops->ae_dev_resetting(handle) || ops->get_hw_reset_stat(handle)) {
6770 handle->rinfo.instance_state = HNS_ROCE_STATE_NON_INIT;
6774 id = pci_match_id(hns_roce_hw_v2_pci_tbl, handle->pdev);
6778 if (id->driver_data && handle->pdev->revision == PCI_REVISION_ID_HIP08)
6781 ret = __hns_roce_hw_v2_init_instance(handle);
6783 handle->rinfo.instance_state = HNS_ROCE_STATE_NON_INIT;
6784 dev_err(dev, "RoCE instance init failed! ret = %d\n", ret);
6785 if (ops->ae_dev_resetting(handle) ||
6786 ops->get_hw_reset_stat(handle))
6792 handle->rinfo.instance_state = HNS_ROCE_STATE_INITED;
6797 dev_err(dev, "Device is busy in resetting state.\n"
6798 "please retry later.\n");
6803 static void hns_roce_hw_v2_uninit_instance(struct hnae3_handle *handle,
6806 if (handle->rinfo.instance_state != HNS_ROCE_STATE_INITED)
6809 handle->rinfo.instance_state = HNS_ROCE_STATE_UNINIT;
6811 __hns_roce_hw_v2_uninit_instance(handle, reset);
6813 handle->rinfo.instance_state = HNS_ROCE_STATE_NON_INIT;
6815 static int hns_roce_hw_v2_reset_notify_down(struct hnae3_handle *handle)
6817 struct hns_roce_dev *hr_dev;
6819 if (handle->rinfo.instance_state != HNS_ROCE_STATE_INITED) {
6820 set_bit(HNS_ROCE_RST_DIRECT_RETURN, &handle->rinfo.state);
6824 handle->rinfo.reset_state = HNS_ROCE_STATE_RST_DOWN;
6825 clear_bit(HNS_ROCE_RST_DIRECT_RETURN, &handle->rinfo.state);
6827 hr_dev = handle->priv;
6831 hr_dev->active = false;
6832 hr_dev->dis_db = true;
6833 hr_dev->state = HNS_ROCE_DEVICE_STATE_RST_DOWN;
6838 static int hns_roce_hw_v2_reset_notify_init(struct hnae3_handle *handle)
6840 struct device *dev = &handle->pdev->dev;
6843 if (test_and_clear_bit(HNS_ROCE_RST_DIRECT_RETURN,
6844 &handle->rinfo.state)) {
6845 handle->rinfo.reset_state = HNS_ROCE_STATE_RST_INITED;
6849 handle->rinfo.reset_state = HNS_ROCE_STATE_RST_INIT;
6851 dev_info(&handle->pdev->dev, "In reset process RoCE client reinit.\n");
6852 ret = __hns_roce_hw_v2_init_instance(handle);
6854 /* when reset notify type is HNAE3_INIT_CLIENT In reset notify
6855 * callback function, RoCE Engine reinitialize. If RoCE reinit
6856 * failed, we should inform NIC driver.
6858 handle->priv = NULL;
6859 dev_err(dev, "In reset process RoCE reinit failed %d.\n", ret);
6861 handle->rinfo.reset_state = HNS_ROCE_STATE_RST_INITED;
6862 dev_info(dev, "reset done, RoCE client reinit finished.\n");
6868 static int hns_roce_hw_v2_reset_notify_uninit(struct hnae3_handle *handle)
6870 if (test_bit(HNS_ROCE_RST_DIRECT_RETURN, &handle->rinfo.state))
6873 handle->rinfo.reset_state = HNS_ROCE_STATE_RST_UNINIT;
6874 dev_info(&handle->pdev->dev, "In reset process RoCE client uninit.\n");
6875 msleep(HNS_ROCE_V2_HW_RST_UNINT_DELAY);
6876 __hns_roce_hw_v2_uninit_instance(handle, false);
6881 static int hns_roce_hw_v2_reset_notify(struct hnae3_handle *handle,
6882 enum hnae3_reset_notify_type type)
6887 case HNAE3_DOWN_CLIENT:
6888 ret = hns_roce_hw_v2_reset_notify_down(handle);
6890 case HNAE3_INIT_CLIENT:
6891 ret = hns_roce_hw_v2_reset_notify_init(handle);
6893 case HNAE3_UNINIT_CLIENT:
6894 ret = hns_roce_hw_v2_reset_notify_uninit(handle);
6903 static const struct hnae3_client_ops hns_roce_hw_v2_ops = {
6904 .init_instance = hns_roce_hw_v2_init_instance,
6905 .uninit_instance = hns_roce_hw_v2_uninit_instance,
6906 .reset_notify = hns_roce_hw_v2_reset_notify,
6909 static struct hnae3_client hns_roce_hw_v2_client = {
6910 .name = "hns_roce_hw_v2",
6911 .type = HNAE3_CLIENT_ROCE,
6912 .ops = &hns_roce_hw_v2_ops,
6915 static int __init hns_roce_hw_v2_init(void)
6917 return hnae3_register_client(&hns_roce_hw_v2_client);
6920 static void __exit hns_roce_hw_v2_exit(void)
6922 hnae3_unregister_client(&hns_roce_hw_v2_client);
6925 module_init(hns_roce_hw_v2_init);
6926 module_exit(hns_roce_hw_v2_exit);
6928 MODULE_LICENSE("Dual BSD/GPL");
6929 MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
6930 MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
6931 MODULE_AUTHOR("Shaobo Xu <xushaobo2@huawei.com>");
6932 MODULE_DESCRIPTION("Hisilicon Hip08 Family RoCE Driver");