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