linux/drivers/infiniband/hw/hns/hns_roce_hw_v1.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016 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/platform_device.h>
  34#include <linux/acpi.h>
  35#include <linux/etherdevice.h>
  36#include <linux/interrupt.h>
  37#include <linux/of.h>
  38#include <linux/of_platform.h>
  39#include <rdma/ib_umem.h>
  40#include "hns_roce_common.h"
  41#include "hns_roce_device.h"
  42#include "hns_roce_cmd.h"
  43#include "hns_roce_hem.h"
  44#include "hns_roce_hw_v1.h"
  45
  46static void set_data_seg(struct hns_roce_wqe_data_seg *dseg, struct ib_sge *sg)
  47{
  48        dseg->lkey = cpu_to_le32(sg->lkey);
  49        dseg->addr = cpu_to_le64(sg->addr);
  50        dseg->len  = cpu_to_le32(sg->length);
  51}
  52
  53static void set_raddr_seg(struct hns_roce_wqe_raddr_seg *rseg, u64 remote_addr,
  54                          u32 rkey)
  55{
  56        rseg->raddr = cpu_to_le64(remote_addr);
  57        rseg->rkey  = cpu_to_le32(rkey);
  58        rseg->len   = 0;
  59}
  60
  61static int hns_roce_v1_post_send(struct ib_qp *ibqp,
  62                                 const struct ib_send_wr *wr,
  63                                 const struct ib_send_wr **bad_wr)
  64{
  65        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
  66        struct hns_roce_ah *ah = to_hr_ah(ud_wr(wr)->ah);
  67        struct hns_roce_ud_send_wqe *ud_sq_wqe = NULL;
  68        struct hns_roce_wqe_ctrl_seg *ctrl = NULL;
  69        struct hns_roce_wqe_data_seg *dseg = NULL;
  70        struct hns_roce_qp *qp = to_hr_qp(ibqp);
  71        struct device *dev = &hr_dev->pdev->dev;
  72        struct hns_roce_sq_db sq_db;
  73        int ps_opcode = 0, i = 0;
  74        unsigned long flags = 0;
  75        void *wqe = NULL;
  76        u32 doorbell[2];
  77        int nreq = 0;
  78        u32 ind = 0;
  79        int ret = 0;
  80        u8 *smac;
  81        int loopback;
  82
  83        if (unlikely(ibqp->qp_type != IB_QPT_GSI &&
  84                ibqp->qp_type != IB_QPT_RC)) {
  85                dev_err(dev, "un-supported QP type\n");
  86                *bad_wr = NULL;
  87                return -EOPNOTSUPP;
  88        }
  89
  90        spin_lock_irqsave(&qp->sq.lock, flags);
  91        ind = qp->sq_next_wqe;
  92        for (nreq = 0; wr; ++nreq, wr = wr->next) {
  93                if (hns_roce_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  94                        ret = -ENOMEM;
  95                        *bad_wr = wr;
  96                        goto out;
  97                }
  98
  99                if (unlikely(wr->num_sge > qp->sq.max_gs)) {
 100                        dev_err(dev, "num_sge=%d > qp->sq.max_gs=%d\n",
 101                                wr->num_sge, qp->sq.max_gs);
 102                        ret = -EINVAL;
 103                        *bad_wr = wr;
 104                        goto out;
 105                }
 106
 107                wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
 108                qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] =
 109                                                                      wr->wr_id;
 110
 111                /* Corresponding to the RC and RD type wqe process separately */
 112                if (ibqp->qp_type == IB_QPT_GSI) {
 113                        ud_sq_wqe = wqe;
 114                        roce_set_field(ud_sq_wqe->dmac_h,
 115                                       UD_SEND_WQE_U32_4_DMAC_0_M,
 116                                       UD_SEND_WQE_U32_4_DMAC_0_S,
 117                                       ah->av.mac[0]);
 118                        roce_set_field(ud_sq_wqe->dmac_h,
 119                                       UD_SEND_WQE_U32_4_DMAC_1_M,
 120                                       UD_SEND_WQE_U32_4_DMAC_1_S,
 121                                       ah->av.mac[1]);
 122                        roce_set_field(ud_sq_wqe->dmac_h,
 123                                       UD_SEND_WQE_U32_4_DMAC_2_M,
 124                                       UD_SEND_WQE_U32_4_DMAC_2_S,
 125                                       ah->av.mac[2]);
 126                        roce_set_field(ud_sq_wqe->dmac_h,
 127                                       UD_SEND_WQE_U32_4_DMAC_3_M,
 128                                       UD_SEND_WQE_U32_4_DMAC_3_S,
 129                                       ah->av.mac[3]);
 130
 131                        roce_set_field(ud_sq_wqe->u32_8,
 132                                       UD_SEND_WQE_U32_8_DMAC_4_M,
 133                                       UD_SEND_WQE_U32_8_DMAC_4_S,
 134                                       ah->av.mac[4]);
 135                        roce_set_field(ud_sq_wqe->u32_8,
 136                                       UD_SEND_WQE_U32_8_DMAC_5_M,
 137                                       UD_SEND_WQE_U32_8_DMAC_5_S,
 138                                       ah->av.mac[5]);
 139
 140                        smac = (u8 *)hr_dev->dev_addr[qp->port];
 141                        loopback = ether_addr_equal_unaligned(ah->av.mac,
 142                                                              smac) ? 1 : 0;
 143                        roce_set_bit(ud_sq_wqe->u32_8,
 144                                     UD_SEND_WQE_U32_8_LOOPBACK_INDICATOR_S,
 145                                     loopback);
 146
 147                        roce_set_field(ud_sq_wqe->u32_8,
 148                                       UD_SEND_WQE_U32_8_OPERATION_TYPE_M,
 149                                       UD_SEND_WQE_U32_8_OPERATION_TYPE_S,
 150                                       HNS_ROCE_WQE_OPCODE_SEND);
 151                        roce_set_field(ud_sq_wqe->u32_8,
 152                                       UD_SEND_WQE_U32_8_NUMBER_OF_DATA_SEG_M,
 153                                       UD_SEND_WQE_U32_8_NUMBER_OF_DATA_SEG_S,
 154                                       2);
 155                        roce_set_bit(ud_sq_wqe->u32_8,
 156                                UD_SEND_WQE_U32_8_SEND_GL_ROUTING_HDR_FLAG_S,
 157                                1);
 158
 159                        ud_sq_wqe->u32_8 |= (wr->send_flags & IB_SEND_SIGNALED ?
 160                                cpu_to_le32(HNS_ROCE_WQE_CQ_NOTIFY) : 0) |
 161                                (wr->send_flags & IB_SEND_SOLICITED ?
 162                                cpu_to_le32(HNS_ROCE_WQE_SE) : 0) |
 163                                ((wr->opcode == IB_WR_SEND_WITH_IMM) ?
 164                                cpu_to_le32(HNS_ROCE_WQE_IMM) : 0);
 165
 166                        roce_set_field(ud_sq_wqe->u32_16,
 167                                       UD_SEND_WQE_U32_16_DEST_QP_M,
 168                                       UD_SEND_WQE_U32_16_DEST_QP_S,
 169                                       ud_wr(wr)->remote_qpn);
 170                        roce_set_field(ud_sq_wqe->u32_16,
 171                                       UD_SEND_WQE_U32_16_MAX_STATIC_RATE_M,
 172                                       UD_SEND_WQE_U32_16_MAX_STATIC_RATE_S,
 173                                       ah->av.stat_rate);
 174
 175                        roce_set_field(ud_sq_wqe->u32_36,
 176                                       UD_SEND_WQE_U32_36_FLOW_LABEL_M,
 177                                       UD_SEND_WQE_U32_36_FLOW_LABEL_S,
 178                                       ah->av.sl_tclass_flowlabel &
 179                                       HNS_ROCE_FLOW_LABEL_MASK);
 180                        roce_set_field(ud_sq_wqe->u32_36,
 181                                      UD_SEND_WQE_U32_36_PRIORITY_M,
 182                                      UD_SEND_WQE_U32_36_PRIORITY_S,
 183                                      le32_to_cpu(ah->av.sl_tclass_flowlabel) >>
 184                                      HNS_ROCE_SL_SHIFT);
 185                        roce_set_field(ud_sq_wqe->u32_36,
 186                                       UD_SEND_WQE_U32_36_SGID_INDEX_M,
 187                                       UD_SEND_WQE_U32_36_SGID_INDEX_S,
 188                                       hns_get_gid_index(hr_dev, qp->phy_port,
 189                                                         ah->av.gid_index));
 190
 191                        roce_set_field(ud_sq_wqe->u32_40,
 192                                       UD_SEND_WQE_U32_40_HOP_LIMIT_M,
 193                                       UD_SEND_WQE_U32_40_HOP_LIMIT_S,
 194                                       ah->av.hop_limit);
 195                        roce_set_field(ud_sq_wqe->u32_40,
 196                                       UD_SEND_WQE_U32_40_TRAFFIC_CLASS_M,
 197                                       UD_SEND_WQE_U32_40_TRAFFIC_CLASS_S,
 198                                       ah->av.sl_tclass_flowlabel >>
 199                                       HNS_ROCE_TCLASS_SHIFT);
 200
 201                        memcpy(&ud_sq_wqe->dgid[0], &ah->av.dgid[0], GID_LEN);
 202
 203                        ud_sq_wqe->va0_l =
 204                                       cpu_to_le32((u32)wr->sg_list[0].addr);
 205                        ud_sq_wqe->va0_h =
 206                                       cpu_to_le32((wr->sg_list[0].addr) >> 32);
 207                        ud_sq_wqe->l_key0 =
 208                                       cpu_to_le32(wr->sg_list[0].lkey);
 209
 210                        ud_sq_wqe->va1_l =
 211                                       cpu_to_le32((u32)wr->sg_list[1].addr);
 212                        ud_sq_wqe->va1_h =
 213                                       cpu_to_le32((wr->sg_list[1].addr) >> 32);
 214                        ud_sq_wqe->l_key1 =
 215                                       cpu_to_le32(wr->sg_list[1].lkey);
 216                        ind++;
 217                } else if (ibqp->qp_type == IB_QPT_RC) {
 218                        u32 tmp_len = 0;
 219
 220                        ctrl = wqe;
 221                        memset(ctrl, 0, sizeof(struct hns_roce_wqe_ctrl_seg));
 222                        for (i = 0; i < wr->num_sge; i++)
 223                                tmp_len += wr->sg_list[i].length;
 224
 225                        ctrl->msg_length =
 226                          cpu_to_le32(le32_to_cpu(ctrl->msg_length) + tmp_len);
 227
 228                        ctrl->sgl_pa_h = 0;
 229                        ctrl->flag = 0;
 230
 231                        switch (wr->opcode) {
 232                        case IB_WR_SEND_WITH_IMM:
 233                        case IB_WR_RDMA_WRITE_WITH_IMM:
 234                                ctrl->imm_data = wr->ex.imm_data;
 235                                break;
 236                        case IB_WR_SEND_WITH_INV:
 237                                ctrl->inv_key =
 238                                        cpu_to_le32(wr->ex.invalidate_rkey);
 239                                break;
 240                        default:
 241                                ctrl->imm_data = 0;
 242                                break;
 243                        }
 244
 245                        /*Ctrl field, ctrl set type: sig, solic, imm, fence */
 246                        /* SO wait for conforming application scenarios */
 247                        ctrl->flag |= (wr->send_flags & IB_SEND_SIGNALED ?
 248                                      cpu_to_le32(HNS_ROCE_WQE_CQ_NOTIFY) : 0) |
 249                                      (wr->send_flags & IB_SEND_SOLICITED ?
 250                                      cpu_to_le32(HNS_ROCE_WQE_SE) : 0) |
 251                                      ((wr->opcode == IB_WR_SEND_WITH_IMM ||
 252                                      wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) ?
 253                                      cpu_to_le32(HNS_ROCE_WQE_IMM) : 0) |
 254                                      (wr->send_flags & IB_SEND_FENCE ?
 255                                      (cpu_to_le32(HNS_ROCE_WQE_FENCE)) : 0);
 256
 257                        wqe += sizeof(struct hns_roce_wqe_ctrl_seg);
 258
 259                        switch (wr->opcode) {
 260                        case IB_WR_RDMA_READ:
 261                                ps_opcode = HNS_ROCE_WQE_OPCODE_RDMA_READ;
 262                                set_raddr_seg(wqe,  rdma_wr(wr)->remote_addr,
 263                                               rdma_wr(wr)->rkey);
 264                                break;
 265                        case IB_WR_RDMA_WRITE:
 266                        case IB_WR_RDMA_WRITE_WITH_IMM:
 267                                ps_opcode = HNS_ROCE_WQE_OPCODE_RDMA_WRITE;
 268                                set_raddr_seg(wqe,  rdma_wr(wr)->remote_addr,
 269                                              rdma_wr(wr)->rkey);
 270                                break;
 271                        case IB_WR_SEND:
 272                        case IB_WR_SEND_WITH_INV:
 273                        case IB_WR_SEND_WITH_IMM:
 274                                ps_opcode = HNS_ROCE_WQE_OPCODE_SEND;
 275                                break;
 276                        case IB_WR_LOCAL_INV:
 277                                break;
 278                        case IB_WR_ATOMIC_CMP_AND_SWP:
 279                        case IB_WR_ATOMIC_FETCH_AND_ADD:
 280                        case IB_WR_LSO:
 281                        default:
 282                                ps_opcode = HNS_ROCE_WQE_OPCODE_MASK;
 283                                break;
 284                        }
 285                        ctrl->flag |= cpu_to_le32(ps_opcode);
 286                        wqe += sizeof(struct hns_roce_wqe_raddr_seg);
 287
 288                        dseg = wqe;
 289                        if (wr->send_flags & IB_SEND_INLINE && wr->num_sge) {
 290                                if (le32_to_cpu(ctrl->msg_length) >
 291                                    hr_dev->caps.max_sq_inline) {
 292                                        ret = -EINVAL;
 293                                        *bad_wr = wr;
 294                                        dev_err(dev, "inline len(1-%d)=%d, illegal",
 295                                                ctrl->msg_length,
 296                                                hr_dev->caps.max_sq_inline);
 297                                        goto out;
 298                                }
 299                                for (i = 0; i < wr->num_sge; i++) {
 300                                        memcpy(wqe, ((void *) (uintptr_t)
 301                                               wr->sg_list[i].addr),
 302                                               wr->sg_list[i].length);
 303                                        wqe += wr->sg_list[i].length;
 304                                }
 305                                ctrl->flag |= cpu_to_le32(HNS_ROCE_WQE_INLINE);
 306                        } else {
 307                                /*sqe num is two */
 308                                for (i = 0; i < wr->num_sge; i++)
 309                                        set_data_seg(dseg + i, wr->sg_list + i);
 310
 311                                ctrl->flag |= cpu_to_le32(wr->num_sge <<
 312                                              HNS_ROCE_WQE_SGE_NUM_BIT);
 313                        }
 314                        ind++;
 315                }
 316        }
 317
 318out:
 319        /* Set DB return */
 320        if (likely(nreq)) {
 321                qp->sq.head += nreq;
 322                /* Memory barrier */
 323                wmb();
 324
 325                sq_db.u32_4 = 0;
 326                sq_db.u32_8 = 0;
 327                roce_set_field(sq_db.u32_4, SQ_DOORBELL_U32_4_SQ_HEAD_M,
 328                               SQ_DOORBELL_U32_4_SQ_HEAD_S,
 329                              (qp->sq.head & ((qp->sq.wqe_cnt << 1) - 1)));
 330                roce_set_field(sq_db.u32_4, SQ_DOORBELL_U32_4_SL_M,
 331                               SQ_DOORBELL_U32_4_SL_S, qp->sl);
 332                roce_set_field(sq_db.u32_4, SQ_DOORBELL_U32_4_PORT_M,
 333                               SQ_DOORBELL_U32_4_PORT_S, qp->phy_port);
 334                roce_set_field(sq_db.u32_8, SQ_DOORBELL_U32_8_QPN_M,
 335                               SQ_DOORBELL_U32_8_QPN_S, qp->doorbell_qpn);
 336                roce_set_bit(sq_db.u32_8, SQ_DOORBELL_HW_SYNC_S, 1);
 337
 338                doorbell[0] = le32_to_cpu(sq_db.u32_4);
 339                doorbell[1] = le32_to_cpu(sq_db.u32_8);
 340
 341                hns_roce_write64_k((__le32 *)doorbell, qp->sq.db_reg_l);
 342                qp->sq_next_wqe = ind;
 343        }
 344
 345        spin_unlock_irqrestore(&qp->sq.lock, flags);
 346
 347        return ret;
 348}
 349
 350static int hns_roce_v1_post_recv(struct ib_qp *ibqp,
 351                                 const struct ib_recv_wr *wr,
 352                                 const struct ib_recv_wr **bad_wr)
 353{
 354        int ret = 0;
 355        int nreq = 0;
 356        int ind = 0;
 357        int i = 0;
 358        u32 reg_val;
 359        unsigned long flags = 0;
 360        struct hns_roce_rq_wqe_ctrl *ctrl = NULL;
 361        struct hns_roce_wqe_data_seg *scat = NULL;
 362        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
 363        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
 364        struct device *dev = &hr_dev->pdev->dev;
 365        struct hns_roce_rq_db rq_db;
 366        uint32_t doorbell[2] = {0};
 367
 368        spin_lock_irqsave(&hr_qp->rq.lock, flags);
 369        ind = hr_qp->rq.head & (hr_qp->rq.wqe_cnt - 1);
 370
 371        for (nreq = 0; wr; ++nreq, wr = wr->next) {
 372                if (hns_roce_wq_overflow(&hr_qp->rq, nreq,
 373                        hr_qp->ibqp.recv_cq)) {
 374                        ret = -ENOMEM;
 375                        *bad_wr = wr;
 376                        goto out;
 377                }
 378
 379                if (unlikely(wr->num_sge > hr_qp->rq.max_gs)) {
 380                        dev_err(dev, "rq:num_sge=%d > qp->sq.max_gs=%d\n",
 381                                wr->num_sge, hr_qp->rq.max_gs);
 382                        ret = -EINVAL;
 383                        *bad_wr = wr;
 384                        goto out;
 385                }
 386
 387                ctrl = get_recv_wqe(hr_qp, ind);
 388
 389                roce_set_field(ctrl->rwqe_byte_12,
 390                               RQ_WQE_CTRL_RWQE_BYTE_12_RWQE_SGE_NUM_M,
 391                               RQ_WQE_CTRL_RWQE_BYTE_12_RWQE_SGE_NUM_S,
 392                               wr->num_sge);
 393
 394                scat = (struct hns_roce_wqe_data_seg *)(ctrl + 1);
 395
 396                for (i = 0; i < wr->num_sge; i++)
 397                        set_data_seg(scat + i, wr->sg_list + i);
 398
 399                hr_qp->rq.wrid[ind] = wr->wr_id;
 400
 401                ind = (ind + 1) & (hr_qp->rq.wqe_cnt - 1);
 402        }
 403
 404out:
 405        if (likely(nreq)) {
 406                hr_qp->rq.head += nreq;
 407                /* Memory barrier */
 408                wmb();
 409
 410                if (ibqp->qp_type == IB_QPT_GSI) {
 411                        __le32 tmp;
 412
 413                        /* SW update GSI rq header */
 414                        reg_val = roce_read(to_hr_dev(ibqp->device),
 415                                            ROCEE_QP1C_CFG3_0_REG +
 416                                            QP1C_CFGN_OFFSET * hr_qp->phy_port);
 417                        tmp = cpu_to_le32(reg_val);
 418                        roce_set_field(tmp,
 419                                       ROCEE_QP1C_CFG3_0_ROCEE_QP1C_RQ_HEAD_M,
 420                                       ROCEE_QP1C_CFG3_0_ROCEE_QP1C_RQ_HEAD_S,
 421                                       hr_qp->rq.head);
 422                        reg_val = le32_to_cpu(tmp);
 423                        roce_write(to_hr_dev(ibqp->device),
 424                                   ROCEE_QP1C_CFG3_0_REG +
 425                                   QP1C_CFGN_OFFSET * hr_qp->phy_port, reg_val);
 426                } else {
 427                        rq_db.u32_4 = 0;
 428                        rq_db.u32_8 = 0;
 429
 430                        roce_set_field(rq_db.u32_4, RQ_DOORBELL_U32_4_RQ_HEAD_M,
 431                                       RQ_DOORBELL_U32_4_RQ_HEAD_S,
 432                                       hr_qp->rq.head);
 433                        roce_set_field(rq_db.u32_8, RQ_DOORBELL_U32_8_QPN_M,
 434                                       RQ_DOORBELL_U32_8_QPN_S, hr_qp->qpn);
 435                        roce_set_field(rq_db.u32_8, RQ_DOORBELL_U32_8_CMD_M,
 436                                       RQ_DOORBELL_U32_8_CMD_S, 1);
 437                        roce_set_bit(rq_db.u32_8, RQ_DOORBELL_U32_8_HW_SYNC_S,
 438                                     1);
 439
 440                        doorbell[0] = le32_to_cpu(rq_db.u32_4);
 441                        doorbell[1] = le32_to_cpu(rq_db.u32_8);
 442
 443                        hns_roce_write64_k((__le32 *)doorbell,
 444                                           hr_qp->rq.db_reg_l);
 445                }
 446        }
 447        spin_unlock_irqrestore(&hr_qp->rq.lock, flags);
 448
 449        return ret;
 450}
 451
 452static void hns_roce_set_db_event_mode(struct hns_roce_dev *hr_dev,
 453                                       int sdb_mode, int odb_mode)
 454{
 455        __le32 tmp;
 456        u32 val;
 457
 458        val = roce_read(hr_dev, ROCEE_GLB_CFG_REG);
 459        tmp = cpu_to_le32(val);
 460        roce_set_bit(tmp, ROCEE_GLB_CFG_ROCEE_DB_SQ_MODE_S, sdb_mode);
 461        roce_set_bit(tmp, ROCEE_GLB_CFG_ROCEE_DB_OTH_MODE_S, odb_mode);
 462        val = le32_to_cpu(tmp);
 463        roce_write(hr_dev, ROCEE_GLB_CFG_REG, val);
 464}
 465
 466static void hns_roce_set_db_ext_mode(struct hns_roce_dev *hr_dev, u32 sdb_mode,
 467                                     u32 odb_mode)
 468{
 469        __le32 tmp;
 470        u32 val;
 471
 472        /* Configure SDB/ODB extend mode */
 473        val = roce_read(hr_dev, ROCEE_GLB_CFG_REG);
 474        tmp = cpu_to_le32(val);
 475        roce_set_bit(tmp, ROCEE_GLB_CFG_SQ_EXT_DB_MODE_S, sdb_mode);
 476        roce_set_bit(tmp, ROCEE_GLB_CFG_OTH_EXT_DB_MODE_S, odb_mode);
 477        val = le32_to_cpu(tmp);
 478        roce_write(hr_dev, ROCEE_GLB_CFG_REG, val);
 479}
 480
 481static void hns_roce_set_sdb(struct hns_roce_dev *hr_dev, u32 sdb_alept,
 482                             u32 sdb_alful)
 483{
 484        __le32 tmp;
 485        u32 val;
 486
 487        /* Configure SDB */
 488        val = roce_read(hr_dev, ROCEE_DB_SQ_WL_REG);
 489        tmp = cpu_to_le32(val);
 490        roce_set_field(tmp, ROCEE_DB_SQ_WL_ROCEE_DB_SQ_WL_M,
 491                       ROCEE_DB_SQ_WL_ROCEE_DB_SQ_WL_S, sdb_alful);
 492        roce_set_field(tmp, ROCEE_DB_SQ_WL_ROCEE_DB_SQ_WL_EMPTY_M,
 493                       ROCEE_DB_SQ_WL_ROCEE_DB_SQ_WL_EMPTY_S, sdb_alept);
 494        val = le32_to_cpu(tmp);
 495        roce_write(hr_dev, ROCEE_DB_SQ_WL_REG, val);
 496}
 497
 498static void hns_roce_set_odb(struct hns_roce_dev *hr_dev, u32 odb_alept,
 499                             u32 odb_alful)
 500{
 501        __le32 tmp;
 502        u32 val;
 503
 504        /* Configure ODB */
 505        val = roce_read(hr_dev, ROCEE_DB_OTHERS_WL_REG);
 506        tmp = cpu_to_le32(val);
 507        roce_set_field(tmp, ROCEE_DB_OTHERS_WL_ROCEE_DB_OTH_WL_M,
 508                       ROCEE_DB_OTHERS_WL_ROCEE_DB_OTH_WL_S, odb_alful);
 509        roce_set_field(tmp, ROCEE_DB_OTHERS_WL_ROCEE_DB_OTH_WL_EMPTY_M,
 510                       ROCEE_DB_OTHERS_WL_ROCEE_DB_OTH_WL_EMPTY_S, odb_alept);
 511        val = le32_to_cpu(tmp);
 512        roce_write(hr_dev, ROCEE_DB_OTHERS_WL_REG, val);
 513}
 514
 515static void hns_roce_set_sdb_ext(struct hns_roce_dev *hr_dev, u32 ext_sdb_alept,
 516                                 u32 ext_sdb_alful)
 517{
 518        struct device *dev = &hr_dev->pdev->dev;
 519        struct hns_roce_v1_priv *priv;
 520        struct hns_roce_db_table *db;
 521        dma_addr_t sdb_dma_addr;
 522        __le32 tmp;
 523        u32 val;
 524
 525        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 526        db = &priv->db_table;
 527
 528        /* Configure extend SDB threshold */
 529        roce_write(hr_dev, ROCEE_EXT_DB_SQ_WL_EMPTY_REG, ext_sdb_alept);
 530        roce_write(hr_dev, ROCEE_EXT_DB_SQ_WL_REG, ext_sdb_alful);
 531
 532        /* Configure extend SDB base addr */
 533        sdb_dma_addr = db->ext_db->sdb_buf_list->map;
 534        roce_write(hr_dev, ROCEE_EXT_DB_SQ_REG, (u32)(sdb_dma_addr >> 12));
 535
 536        /* Configure extend SDB depth */
 537        val = roce_read(hr_dev, ROCEE_EXT_DB_SQ_H_REG);
 538        tmp = cpu_to_le32(val);
 539        roce_set_field(tmp, ROCEE_EXT_DB_SQ_H_EXT_DB_SQ_SHIFT_M,
 540                       ROCEE_EXT_DB_SQ_H_EXT_DB_SQ_SHIFT_S,
 541                       db->ext_db->esdb_dep);
 542        /*
 543         * 44 = 32 + 12, When evaluating addr to hardware, shift 12 because of
 544         * using 4K page, and shift more 32 because of
 545         * caculating the high 32 bit value evaluated to hardware.
 546         */
 547        roce_set_field(tmp, ROCEE_EXT_DB_SQ_H_EXT_DB_SQ_BA_H_M,
 548                       ROCEE_EXT_DB_SQ_H_EXT_DB_SQ_BA_H_S, sdb_dma_addr >> 44);
 549        val = le32_to_cpu(tmp);
 550        roce_write(hr_dev, ROCEE_EXT_DB_SQ_H_REG, val);
 551
 552        dev_dbg(dev, "ext SDB depth: 0x%x\n", db->ext_db->esdb_dep);
 553        dev_dbg(dev, "ext SDB threshold: epmty: 0x%x, ful: 0x%x\n",
 554                ext_sdb_alept, ext_sdb_alful);
 555}
 556
 557static void hns_roce_set_odb_ext(struct hns_roce_dev *hr_dev, u32 ext_odb_alept,
 558                                 u32 ext_odb_alful)
 559{
 560        struct device *dev = &hr_dev->pdev->dev;
 561        struct hns_roce_v1_priv *priv;
 562        struct hns_roce_db_table *db;
 563        dma_addr_t odb_dma_addr;
 564        __le32 tmp;
 565        u32 val;
 566
 567        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 568        db = &priv->db_table;
 569
 570        /* Configure extend ODB threshold */
 571        roce_write(hr_dev, ROCEE_EXT_DB_OTHERS_WL_EMPTY_REG, ext_odb_alept);
 572        roce_write(hr_dev, ROCEE_EXT_DB_OTHERS_WL_REG, ext_odb_alful);
 573
 574        /* Configure extend ODB base addr */
 575        odb_dma_addr = db->ext_db->odb_buf_list->map;
 576        roce_write(hr_dev, ROCEE_EXT_DB_OTH_REG, (u32)(odb_dma_addr >> 12));
 577
 578        /* Configure extend ODB depth */
 579        val = roce_read(hr_dev, ROCEE_EXT_DB_OTH_H_REG);
 580        tmp = cpu_to_le32(val);
 581        roce_set_field(tmp, ROCEE_EXT_DB_OTH_H_EXT_DB_OTH_SHIFT_M,
 582                       ROCEE_EXT_DB_OTH_H_EXT_DB_OTH_SHIFT_S,
 583                       db->ext_db->eodb_dep);
 584        roce_set_field(tmp, ROCEE_EXT_DB_SQ_H_EXT_DB_OTH_BA_H_M,
 585                       ROCEE_EXT_DB_SQ_H_EXT_DB_OTH_BA_H_S,
 586                       db->ext_db->eodb_dep);
 587        val = le32_to_cpu(tmp);
 588        roce_write(hr_dev, ROCEE_EXT_DB_OTH_H_REG, val);
 589
 590        dev_dbg(dev, "ext ODB depth: 0x%x\n", db->ext_db->eodb_dep);
 591        dev_dbg(dev, "ext ODB threshold: empty: 0x%x, ful: 0x%x\n",
 592                ext_odb_alept, ext_odb_alful);
 593}
 594
 595static int hns_roce_db_ext_init(struct hns_roce_dev *hr_dev, u32 sdb_ext_mod,
 596                                u32 odb_ext_mod)
 597{
 598        struct device *dev = &hr_dev->pdev->dev;
 599        struct hns_roce_v1_priv *priv;
 600        struct hns_roce_db_table *db;
 601        dma_addr_t sdb_dma_addr;
 602        dma_addr_t odb_dma_addr;
 603        int ret = 0;
 604
 605        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 606        db = &priv->db_table;
 607
 608        db->ext_db = kmalloc(sizeof(*db->ext_db), GFP_KERNEL);
 609        if (!db->ext_db)
 610                return -ENOMEM;
 611
 612        if (sdb_ext_mod) {
 613                db->ext_db->sdb_buf_list = kmalloc(
 614                                sizeof(*db->ext_db->sdb_buf_list), GFP_KERNEL);
 615                if (!db->ext_db->sdb_buf_list) {
 616                        ret = -ENOMEM;
 617                        goto ext_sdb_buf_fail_out;
 618                }
 619
 620                db->ext_db->sdb_buf_list->buf = dma_alloc_coherent(dev,
 621                                                     HNS_ROCE_V1_EXT_SDB_SIZE,
 622                                                     &sdb_dma_addr, GFP_KERNEL);
 623                if (!db->ext_db->sdb_buf_list->buf) {
 624                        ret = -ENOMEM;
 625                        goto alloc_sq_db_buf_fail;
 626                }
 627                db->ext_db->sdb_buf_list->map = sdb_dma_addr;
 628
 629                db->ext_db->esdb_dep = ilog2(HNS_ROCE_V1_EXT_SDB_DEPTH);
 630                hns_roce_set_sdb_ext(hr_dev, HNS_ROCE_V1_EXT_SDB_ALEPT,
 631                                     HNS_ROCE_V1_EXT_SDB_ALFUL);
 632        } else
 633                hns_roce_set_sdb(hr_dev, HNS_ROCE_V1_SDB_ALEPT,
 634                                 HNS_ROCE_V1_SDB_ALFUL);
 635
 636        if (odb_ext_mod) {
 637                db->ext_db->odb_buf_list = kmalloc(
 638                                sizeof(*db->ext_db->odb_buf_list), GFP_KERNEL);
 639                if (!db->ext_db->odb_buf_list) {
 640                        ret = -ENOMEM;
 641                        goto ext_odb_buf_fail_out;
 642                }
 643
 644                db->ext_db->odb_buf_list->buf = dma_alloc_coherent(dev,
 645                                                     HNS_ROCE_V1_EXT_ODB_SIZE,
 646                                                     &odb_dma_addr, GFP_KERNEL);
 647                if (!db->ext_db->odb_buf_list->buf) {
 648                        ret = -ENOMEM;
 649                        goto alloc_otr_db_buf_fail;
 650                }
 651                db->ext_db->odb_buf_list->map = odb_dma_addr;
 652
 653                db->ext_db->eodb_dep = ilog2(HNS_ROCE_V1_EXT_ODB_DEPTH);
 654                hns_roce_set_odb_ext(hr_dev, HNS_ROCE_V1_EXT_ODB_ALEPT,
 655                                     HNS_ROCE_V1_EXT_ODB_ALFUL);
 656        } else
 657                hns_roce_set_odb(hr_dev, HNS_ROCE_V1_ODB_ALEPT,
 658                                 HNS_ROCE_V1_ODB_ALFUL);
 659
 660        hns_roce_set_db_ext_mode(hr_dev, sdb_ext_mod, odb_ext_mod);
 661
 662        return 0;
 663
 664alloc_otr_db_buf_fail:
 665        kfree(db->ext_db->odb_buf_list);
 666
 667ext_odb_buf_fail_out:
 668        if (sdb_ext_mod) {
 669                dma_free_coherent(dev, HNS_ROCE_V1_EXT_SDB_SIZE,
 670                                  db->ext_db->sdb_buf_list->buf,
 671                                  db->ext_db->sdb_buf_list->map);
 672        }
 673
 674alloc_sq_db_buf_fail:
 675        if (sdb_ext_mod)
 676                kfree(db->ext_db->sdb_buf_list);
 677
 678ext_sdb_buf_fail_out:
 679        kfree(db->ext_db);
 680        return ret;
 681}
 682
 683static struct hns_roce_qp *hns_roce_v1_create_lp_qp(struct hns_roce_dev *hr_dev,
 684                                                    struct ib_pd *pd)
 685{
 686        struct device *dev = &hr_dev->pdev->dev;
 687        struct ib_qp_init_attr init_attr;
 688        struct ib_qp *qp;
 689
 690        memset(&init_attr, 0, sizeof(struct ib_qp_init_attr));
 691        init_attr.qp_type               = IB_QPT_RC;
 692        init_attr.sq_sig_type           = IB_SIGNAL_ALL_WR;
 693        init_attr.cap.max_recv_wr       = HNS_ROCE_MIN_WQE_NUM;
 694        init_attr.cap.max_send_wr       = HNS_ROCE_MIN_WQE_NUM;
 695
 696        qp = hns_roce_create_qp(pd, &init_attr, NULL);
 697        if (IS_ERR(qp)) {
 698                dev_err(dev, "Create loop qp for mr free failed!");
 699                return NULL;
 700        }
 701
 702        return to_hr_qp(qp);
 703}
 704
 705static int hns_roce_v1_rsv_lp_qp(struct hns_roce_dev *hr_dev)
 706{
 707        struct hns_roce_caps *caps = &hr_dev->caps;
 708        struct device *dev = &hr_dev->pdev->dev;
 709        struct ib_cq_init_attr cq_init_attr;
 710        struct hns_roce_free_mr *free_mr;
 711        struct ib_qp_attr attr = { 0 };
 712        struct hns_roce_v1_priv *priv;
 713        struct hns_roce_qp *hr_qp;
 714        struct ib_device *ibdev;
 715        struct ib_cq *cq;
 716        struct ib_pd *pd;
 717        union ib_gid dgid;
 718        u64 subnet_prefix;
 719        int attr_mask = 0;
 720        int ret;
 721        int i, j;
 722        u8 queue_en[HNS_ROCE_V1_RESV_QP] = { 0 };
 723        u8 phy_port;
 724        u8 port = 0;
 725        u8 sl;
 726
 727        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 728        free_mr = &priv->free_mr;
 729
 730        /* Reserved cq for loop qp */
 731        cq_init_attr.cqe                = HNS_ROCE_MIN_WQE_NUM * 2;
 732        cq_init_attr.comp_vector        = 0;
 733
 734        ibdev = &hr_dev->ib_dev;
 735        cq = rdma_zalloc_drv_obj(ibdev, ib_cq);
 736        if (!cq)
 737                return -ENOMEM;
 738
 739        ret = hns_roce_ib_create_cq(cq, &cq_init_attr, NULL);
 740        if (ret) {
 741                dev_err(dev, "Create cq for reserved loop qp failed!");
 742                goto alloc_cq_failed;
 743        }
 744        free_mr->mr_free_cq = to_hr_cq(cq);
 745        free_mr->mr_free_cq->ib_cq.device               = &hr_dev->ib_dev;
 746        free_mr->mr_free_cq->ib_cq.uobject              = NULL;
 747        free_mr->mr_free_cq->ib_cq.comp_handler         = NULL;
 748        free_mr->mr_free_cq->ib_cq.event_handler        = NULL;
 749        free_mr->mr_free_cq->ib_cq.cq_context           = NULL;
 750        atomic_set(&free_mr->mr_free_cq->ib_cq.usecnt, 0);
 751
 752        pd = rdma_zalloc_drv_obj(ibdev, ib_pd);
 753        if (pd)
 754                goto alloc_mem_failed;
 755
 756        pd->device  = ibdev;
 757        ret = hns_roce_alloc_pd(pd, NULL);
 758        if (ret)
 759                goto alloc_pd_failed;
 760
 761        free_mr->mr_free_pd = to_hr_pd(pd);
 762        free_mr->mr_free_pd->ibpd.device  = &hr_dev->ib_dev;
 763        free_mr->mr_free_pd->ibpd.uobject = NULL;
 764        free_mr->mr_free_pd->ibpd.__internal_mr = NULL;
 765        atomic_set(&free_mr->mr_free_pd->ibpd.usecnt, 0);
 766
 767        attr.qp_access_flags    = IB_ACCESS_REMOTE_WRITE;
 768        attr.pkey_index         = 0;
 769        attr.min_rnr_timer      = 0;
 770        /* Disable read ability */
 771        attr.max_dest_rd_atomic = 0;
 772        attr.max_rd_atomic      = 0;
 773        /* Use arbitrary values as rq_psn and sq_psn */
 774        attr.rq_psn             = 0x0808;
 775        attr.sq_psn             = 0x0808;
 776        attr.retry_cnt          = 7;
 777        attr.rnr_retry          = 7;
 778        attr.timeout            = 0x12;
 779        attr.path_mtu           = IB_MTU_256;
 780        attr.ah_attr.type       = RDMA_AH_ATTR_TYPE_ROCE;
 781        rdma_ah_set_grh(&attr.ah_attr, NULL, 0, 0, 1, 0);
 782        rdma_ah_set_static_rate(&attr.ah_attr, 3);
 783
 784        subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
 785        for (i = 0; i < HNS_ROCE_V1_RESV_QP; i++) {
 786                phy_port = (i >= HNS_ROCE_MAX_PORTS) ? (i - 2) :
 787                                (i % HNS_ROCE_MAX_PORTS);
 788                sl = i / HNS_ROCE_MAX_PORTS;
 789
 790                for (j = 0; j < caps->num_ports; j++) {
 791                        if (hr_dev->iboe.phy_port[j] == phy_port) {
 792                                queue_en[i] = 1;
 793                                port = j;
 794                                break;
 795                        }
 796                }
 797
 798                if (!queue_en[i])
 799                        continue;
 800
 801                free_mr->mr_free_qp[i] = hns_roce_v1_create_lp_qp(hr_dev, pd);
 802                if (!free_mr->mr_free_qp[i]) {
 803                        dev_err(dev, "Create loop qp failed!\n");
 804                        ret = -ENOMEM;
 805                        goto create_lp_qp_failed;
 806                }
 807                hr_qp = free_mr->mr_free_qp[i];
 808
 809                hr_qp->port             = port;
 810                hr_qp->phy_port         = phy_port;
 811                hr_qp->ibqp.qp_type     = IB_QPT_RC;
 812                hr_qp->ibqp.device      = &hr_dev->ib_dev;
 813                hr_qp->ibqp.uobject     = NULL;
 814                atomic_set(&hr_qp->ibqp.usecnt, 0);
 815                hr_qp->ibqp.pd          = pd;
 816                hr_qp->ibqp.recv_cq     = cq;
 817                hr_qp->ibqp.send_cq     = cq;
 818
 819                rdma_ah_set_port_num(&attr.ah_attr, port + 1);
 820                rdma_ah_set_sl(&attr.ah_attr, sl);
 821                attr.port_num           = port + 1;
 822
 823                attr.dest_qp_num        = hr_qp->qpn;
 824                memcpy(rdma_ah_retrieve_dmac(&attr.ah_attr),
 825                       hr_dev->dev_addr[port],
 826                       MAC_ADDR_OCTET_NUM);
 827
 828                memcpy(&dgid.raw, &subnet_prefix, sizeof(u64));
 829                memcpy(&dgid.raw[8], hr_dev->dev_addr[port], 3);
 830                memcpy(&dgid.raw[13], hr_dev->dev_addr[port] + 3, 3);
 831                dgid.raw[11] = 0xff;
 832                dgid.raw[12] = 0xfe;
 833                dgid.raw[8] ^= 2;
 834                rdma_ah_set_dgid_raw(&attr.ah_attr, dgid.raw);
 835
 836                ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, &attr, attr_mask,
 837                                            IB_QPS_RESET, IB_QPS_INIT);
 838                if (ret) {
 839                        dev_err(dev, "modify qp failed(%d)!\n", ret);
 840                        goto create_lp_qp_failed;
 841                }
 842
 843                ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, &attr, IB_QP_DEST_QPN,
 844                                            IB_QPS_INIT, IB_QPS_RTR);
 845                if (ret) {
 846                        dev_err(dev, "modify qp failed(%d)!\n", ret);
 847                        goto create_lp_qp_failed;
 848                }
 849
 850                ret = hr_dev->hw->modify_qp(&hr_qp->ibqp, &attr, attr_mask,
 851                                            IB_QPS_RTR, IB_QPS_RTS);
 852                if (ret) {
 853                        dev_err(dev, "modify qp failed(%d)!\n", ret);
 854                        goto create_lp_qp_failed;
 855                }
 856        }
 857
 858        return 0;
 859
 860create_lp_qp_failed:
 861        for (i -= 1; i >= 0; i--) {
 862                hr_qp = free_mr->mr_free_qp[i];
 863                if (hns_roce_v1_destroy_qp(&hr_qp->ibqp, NULL))
 864                        dev_err(dev, "Destroy qp %d for mr free failed!\n", i);
 865        }
 866
 867        hns_roce_dealloc_pd(pd, NULL);
 868
 869alloc_pd_failed:
 870        kfree(pd);
 871
 872alloc_mem_failed:
 873        hns_roce_ib_destroy_cq(cq, NULL);
 874alloc_cq_failed:
 875        kfree(cq);
 876        return ret;
 877}
 878
 879static void hns_roce_v1_release_lp_qp(struct hns_roce_dev *hr_dev)
 880{
 881        struct device *dev = &hr_dev->pdev->dev;
 882        struct hns_roce_free_mr *free_mr;
 883        struct hns_roce_v1_priv *priv;
 884        struct hns_roce_qp *hr_qp;
 885        int ret;
 886        int i;
 887
 888        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 889        free_mr = &priv->free_mr;
 890
 891        for (i = 0; i < HNS_ROCE_V1_RESV_QP; i++) {
 892                hr_qp = free_mr->mr_free_qp[i];
 893                if (!hr_qp)
 894                        continue;
 895
 896                ret = hns_roce_v1_destroy_qp(&hr_qp->ibqp, NULL);
 897                if (ret)
 898                        dev_err(dev, "Destroy qp %d for mr free failed(%d)!\n",
 899                                i, ret);
 900        }
 901
 902        hns_roce_ib_destroy_cq(&free_mr->mr_free_cq->ib_cq, NULL);
 903        kfree(&free_mr->mr_free_cq->ib_cq);
 904        hns_roce_dealloc_pd(&free_mr->mr_free_pd->ibpd, NULL);
 905}
 906
 907static int hns_roce_db_init(struct hns_roce_dev *hr_dev)
 908{
 909        struct device *dev = &hr_dev->pdev->dev;
 910        struct hns_roce_v1_priv *priv;
 911        struct hns_roce_db_table *db;
 912        u32 sdb_ext_mod;
 913        u32 odb_ext_mod;
 914        u32 sdb_evt_mod;
 915        u32 odb_evt_mod;
 916        int ret = 0;
 917
 918        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 919        db = &priv->db_table;
 920
 921        memset(db, 0, sizeof(*db));
 922
 923        /* Default DB mode */
 924        sdb_ext_mod = HNS_ROCE_SDB_EXTEND_MODE;
 925        odb_ext_mod = HNS_ROCE_ODB_EXTEND_MODE;
 926        sdb_evt_mod = HNS_ROCE_SDB_NORMAL_MODE;
 927        odb_evt_mod = HNS_ROCE_ODB_POLL_MODE;
 928
 929        db->sdb_ext_mod = sdb_ext_mod;
 930        db->odb_ext_mod = odb_ext_mod;
 931
 932        /* Init extend DB */
 933        ret = hns_roce_db_ext_init(hr_dev, sdb_ext_mod, odb_ext_mod);
 934        if (ret) {
 935                dev_err(dev, "Failed in extend DB configuration.\n");
 936                return ret;
 937        }
 938
 939        hns_roce_set_db_event_mode(hr_dev, sdb_evt_mod, odb_evt_mod);
 940
 941        return 0;
 942}
 943
 944static void hns_roce_v1_recreate_lp_qp_work_fn(struct work_struct *work)
 945{
 946        struct hns_roce_recreate_lp_qp_work *lp_qp_work;
 947        struct hns_roce_dev *hr_dev;
 948
 949        lp_qp_work = container_of(work, struct hns_roce_recreate_lp_qp_work,
 950                                  work);
 951        hr_dev = to_hr_dev(lp_qp_work->ib_dev);
 952
 953        hns_roce_v1_release_lp_qp(hr_dev);
 954
 955        if (hns_roce_v1_rsv_lp_qp(hr_dev))
 956                dev_err(&hr_dev->pdev->dev, "create reserver qp failed\n");
 957
 958        if (lp_qp_work->comp_flag)
 959                complete(lp_qp_work->comp);
 960
 961        kfree(lp_qp_work);
 962}
 963
 964static int hns_roce_v1_recreate_lp_qp(struct hns_roce_dev *hr_dev)
 965{
 966        struct device *dev = &hr_dev->pdev->dev;
 967        struct hns_roce_recreate_lp_qp_work *lp_qp_work;
 968        struct hns_roce_free_mr *free_mr;
 969        struct hns_roce_v1_priv *priv;
 970        struct completion comp;
 971        unsigned long end =
 972          msecs_to_jiffies(HNS_ROCE_V1_RECREATE_LP_QP_TIMEOUT_MSECS) + jiffies;
 973
 974        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
 975        free_mr = &priv->free_mr;
 976
 977        lp_qp_work = kzalloc(sizeof(struct hns_roce_recreate_lp_qp_work),
 978                             GFP_KERNEL);
 979        if (!lp_qp_work)
 980                return -ENOMEM;
 981
 982        INIT_WORK(&(lp_qp_work->work), hns_roce_v1_recreate_lp_qp_work_fn);
 983
 984        lp_qp_work->ib_dev = &(hr_dev->ib_dev);
 985        lp_qp_work->comp = &comp;
 986        lp_qp_work->comp_flag = 1;
 987
 988        init_completion(lp_qp_work->comp);
 989
 990        queue_work(free_mr->free_mr_wq, &(lp_qp_work->work));
 991
 992        while (time_before_eq(jiffies, end)) {
 993                if (try_wait_for_completion(&comp))
 994                        return 0;
 995                msleep(HNS_ROCE_V1_RECREATE_LP_QP_WAIT_VALUE);
 996        }
 997
 998        lp_qp_work->comp_flag = 0;
 999        if (try_wait_for_completion(&comp))
1000                return 0;
1001
1002        dev_warn(dev, "recreate lp qp failed 20s timeout and return failed!\n");
1003        return -ETIMEDOUT;
1004}
1005
1006static int hns_roce_v1_send_lp_wqe(struct hns_roce_qp *hr_qp)
1007{
1008        struct hns_roce_dev *hr_dev = to_hr_dev(hr_qp->ibqp.device);
1009        struct device *dev = &hr_dev->pdev->dev;
1010        struct ib_send_wr send_wr;
1011        const struct ib_send_wr *bad_wr;
1012        int ret;
1013
1014        memset(&send_wr, 0, sizeof(send_wr));
1015        send_wr.next    = NULL;
1016        send_wr.num_sge = 0;
1017        send_wr.send_flags = 0;
1018        send_wr.sg_list = NULL;
1019        send_wr.wr_id   = (unsigned long long)&send_wr;
1020        send_wr.opcode  = IB_WR_RDMA_WRITE;
1021
1022        ret = hns_roce_v1_post_send(&hr_qp->ibqp, &send_wr, &bad_wr);
1023        if (ret) {
1024                dev_err(dev, "Post write wqe for mr free failed(%d)!", ret);
1025                return ret;
1026        }
1027
1028        return 0;
1029}
1030
1031static void hns_roce_v1_mr_free_work_fn(struct work_struct *work)
1032{
1033        struct hns_roce_mr_free_work *mr_work;
1034        struct ib_wc wc[HNS_ROCE_V1_RESV_QP];
1035        struct hns_roce_free_mr *free_mr;
1036        struct hns_roce_cq *mr_free_cq;
1037        struct hns_roce_v1_priv *priv;
1038        struct hns_roce_dev *hr_dev;
1039        struct hns_roce_mr *hr_mr;
1040        struct hns_roce_qp *hr_qp;
1041        struct device *dev;
1042        unsigned long end =
1043                msecs_to_jiffies(HNS_ROCE_V1_FREE_MR_TIMEOUT_MSECS) + jiffies;
1044        int i;
1045        int ret;
1046        int ne = 0;
1047
1048        mr_work = container_of(work, struct hns_roce_mr_free_work, work);
1049        hr_mr = (struct hns_roce_mr *)mr_work->mr;
1050        hr_dev = to_hr_dev(mr_work->ib_dev);
1051        dev = &hr_dev->pdev->dev;
1052
1053        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1054        free_mr = &priv->free_mr;
1055        mr_free_cq = free_mr->mr_free_cq;
1056
1057        for (i = 0; i < HNS_ROCE_V1_RESV_QP; i++) {
1058                hr_qp = free_mr->mr_free_qp[i];
1059                if (!hr_qp)
1060                        continue;
1061                ne++;
1062
1063                ret = hns_roce_v1_send_lp_wqe(hr_qp);
1064                if (ret) {
1065                        dev_err(dev,
1066                             "Send wqe (qp:0x%lx) for mr free failed(%d)!\n",
1067                             hr_qp->qpn, ret);
1068                        goto free_work;
1069                }
1070        }
1071
1072        if (!ne) {
1073                dev_err(dev, "Reserved loop qp is absent!\n");
1074                goto free_work;
1075        }
1076
1077        do {
1078                ret = hns_roce_v1_poll_cq(&mr_free_cq->ib_cq, ne, wc);
1079                if (ret < 0 && hr_qp) {
1080                        dev_err(dev,
1081                           "(qp:0x%lx) starts, Poll cqe failed(%d) for mr 0x%x free! Remain %d cqe\n",
1082                           hr_qp->qpn, ret, hr_mr->key, ne);
1083                        goto free_work;
1084                }
1085                ne -= ret;
1086                usleep_range(HNS_ROCE_V1_FREE_MR_WAIT_VALUE * 1000,
1087                             (1 + HNS_ROCE_V1_FREE_MR_WAIT_VALUE) * 1000);
1088        } while (ne && time_before_eq(jiffies, end));
1089
1090        if (ne != 0)
1091                dev_err(dev,
1092                        "Poll cqe for mr 0x%x free timeout! Remain %d cqe\n",
1093                        hr_mr->key, ne);
1094
1095free_work:
1096        if (mr_work->comp_flag)
1097                complete(mr_work->comp);
1098        kfree(mr_work);
1099}
1100
1101static int hns_roce_v1_dereg_mr(struct hns_roce_dev *hr_dev,
1102                                struct hns_roce_mr *mr, struct ib_udata *udata)
1103{
1104        struct device *dev = &hr_dev->pdev->dev;
1105        struct hns_roce_mr_free_work *mr_work;
1106        struct hns_roce_free_mr *free_mr;
1107        struct hns_roce_v1_priv *priv;
1108        struct completion comp;
1109        unsigned long end =
1110                msecs_to_jiffies(HNS_ROCE_V1_FREE_MR_TIMEOUT_MSECS) + jiffies;
1111        unsigned long start = jiffies;
1112        int npages;
1113        int ret = 0;
1114
1115        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1116        free_mr = &priv->free_mr;
1117
1118        if (mr->enabled) {
1119                if (hns_roce_hw2sw_mpt(hr_dev, NULL, key_to_hw_index(mr->key)
1120                                       & (hr_dev->caps.num_mtpts - 1)))
1121                        dev_warn(dev, "HW2SW_MPT failed!\n");
1122        }
1123
1124        mr_work = kzalloc(sizeof(*mr_work), GFP_KERNEL);
1125        if (!mr_work) {
1126                ret = -ENOMEM;
1127                goto free_mr;
1128        }
1129
1130        INIT_WORK(&(mr_work->work), hns_roce_v1_mr_free_work_fn);
1131
1132        mr_work->ib_dev = &(hr_dev->ib_dev);
1133        mr_work->comp = &comp;
1134        mr_work->comp_flag = 1;
1135        mr_work->mr = (void *)mr;
1136        init_completion(mr_work->comp);
1137
1138        queue_work(free_mr->free_mr_wq, &(mr_work->work));
1139
1140        while (time_before_eq(jiffies, end)) {
1141                if (try_wait_for_completion(&comp))
1142                        goto free_mr;
1143                msleep(HNS_ROCE_V1_FREE_MR_WAIT_VALUE);
1144        }
1145
1146        mr_work->comp_flag = 0;
1147        if (try_wait_for_completion(&comp))
1148                goto free_mr;
1149
1150        dev_warn(dev, "Free mr work 0x%x over 50s and failed!\n", mr->key);
1151        ret = -ETIMEDOUT;
1152
1153free_mr:
1154        dev_dbg(dev, "Free mr 0x%x use 0x%x us.\n",
1155                mr->key, jiffies_to_usecs(jiffies) - jiffies_to_usecs(start));
1156
1157        if (mr->size != ~0ULL) {
1158                npages = ib_umem_page_count(mr->umem);
1159                dma_free_coherent(dev, npages * 8, mr->pbl_buf,
1160                                  mr->pbl_dma_addr);
1161        }
1162
1163        hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap,
1164                             key_to_hw_index(mr->key), 0);
1165
1166        ib_umem_release(mr->umem);
1167
1168        kfree(mr);
1169
1170        return ret;
1171}
1172
1173static void hns_roce_db_free(struct hns_roce_dev *hr_dev)
1174{
1175        struct device *dev = &hr_dev->pdev->dev;
1176        struct hns_roce_v1_priv *priv;
1177        struct hns_roce_db_table *db;
1178
1179        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1180        db = &priv->db_table;
1181
1182        if (db->sdb_ext_mod) {
1183                dma_free_coherent(dev, HNS_ROCE_V1_EXT_SDB_SIZE,
1184                                  db->ext_db->sdb_buf_list->buf,
1185                                  db->ext_db->sdb_buf_list->map);
1186                kfree(db->ext_db->sdb_buf_list);
1187        }
1188
1189        if (db->odb_ext_mod) {
1190                dma_free_coherent(dev, HNS_ROCE_V1_EXT_ODB_SIZE,
1191                                  db->ext_db->odb_buf_list->buf,
1192                                  db->ext_db->odb_buf_list->map);
1193                kfree(db->ext_db->odb_buf_list);
1194        }
1195
1196        kfree(db->ext_db);
1197}
1198
1199static int hns_roce_raq_init(struct hns_roce_dev *hr_dev)
1200{
1201        int ret;
1202        u32 val;
1203        __le32 tmp;
1204        int raq_shift = 0;
1205        dma_addr_t addr;
1206        struct hns_roce_v1_priv *priv;
1207        struct hns_roce_raq_table *raq;
1208        struct device *dev = &hr_dev->pdev->dev;
1209
1210        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1211        raq = &priv->raq_table;
1212
1213        raq->e_raq_buf = kzalloc(sizeof(*(raq->e_raq_buf)), GFP_KERNEL);
1214        if (!raq->e_raq_buf)
1215                return -ENOMEM;
1216
1217        raq->e_raq_buf->buf = dma_alloc_coherent(dev, HNS_ROCE_V1_RAQ_SIZE,
1218                                                 &addr, GFP_KERNEL);
1219        if (!raq->e_raq_buf->buf) {
1220                ret = -ENOMEM;
1221                goto err_dma_alloc_raq;
1222        }
1223        raq->e_raq_buf->map = addr;
1224
1225        /* Configure raq extended address. 48bit 4K align*/
1226        roce_write(hr_dev, ROCEE_EXT_RAQ_REG, raq->e_raq_buf->map >> 12);
1227
1228        /* Configure raq_shift */
1229        raq_shift = ilog2(HNS_ROCE_V1_RAQ_SIZE / HNS_ROCE_V1_RAQ_ENTRY);
1230        val = roce_read(hr_dev, ROCEE_EXT_RAQ_H_REG);
1231        tmp = cpu_to_le32(val);
1232        roce_set_field(tmp, ROCEE_EXT_RAQ_H_EXT_RAQ_SHIFT_M,
1233                       ROCEE_EXT_RAQ_H_EXT_RAQ_SHIFT_S, raq_shift);
1234        /*
1235         * 44 = 32 + 12, When evaluating addr to hardware, shift 12 because of
1236         * using 4K page, and shift more 32 because of
1237         * caculating the high 32 bit value evaluated to hardware.
1238         */
1239        roce_set_field(tmp, ROCEE_EXT_RAQ_H_EXT_RAQ_BA_H_M,
1240                       ROCEE_EXT_RAQ_H_EXT_RAQ_BA_H_S,
1241                       raq->e_raq_buf->map >> 44);
1242        val = le32_to_cpu(tmp);
1243        roce_write(hr_dev, ROCEE_EXT_RAQ_H_REG, val);
1244        dev_dbg(dev, "Configure raq_shift 0x%x.\n", val);
1245
1246        /* Configure raq threshold */
1247        val = roce_read(hr_dev, ROCEE_RAQ_WL_REG);
1248        tmp = cpu_to_le32(val);
1249        roce_set_field(tmp, ROCEE_RAQ_WL_ROCEE_RAQ_WL_M,
1250                       ROCEE_RAQ_WL_ROCEE_RAQ_WL_S,
1251                       HNS_ROCE_V1_EXT_RAQ_WF);
1252        val = le32_to_cpu(tmp);
1253        roce_write(hr_dev, ROCEE_RAQ_WL_REG, val);
1254        dev_dbg(dev, "Configure raq_wl 0x%x.\n", val);
1255
1256        /* Enable extend raq */
1257        val = roce_read(hr_dev, ROCEE_WRMS_POL_TIME_INTERVAL_REG);
1258        tmp = cpu_to_le32(val);
1259        roce_set_field(tmp,
1260                       ROCEE_WRMS_POL_TIME_INTERVAL_WRMS_POL_TIME_INTERVAL_M,
1261                       ROCEE_WRMS_POL_TIME_INTERVAL_WRMS_POL_TIME_INTERVAL_S,
1262                       POL_TIME_INTERVAL_VAL);
1263        roce_set_bit(tmp, ROCEE_WRMS_POL_TIME_INTERVAL_WRMS_EXT_RAQ_MODE, 1);
1264        roce_set_field(tmp,
1265                       ROCEE_WRMS_POL_TIME_INTERVAL_WRMS_RAQ_TIMEOUT_CHK_CFG_M,
1266                       ROCEE_WRMS_POL_TIME_INTERVAL_WRMS_RAQ_TIMEOUT_CHK_CFG_S,
1267                       2);
1268        roce_set_bit(tmp,
1269                     ROCEE_WRMS_POL_TIME_INTERVAL_WRMS_RAQ_TIMEOUT_CHK_EN_S, 1);
1270        val = le32_to_cpu(tmp);
1271        roce_write(hr_dev, ROCEE_WRMS_POL_TIME_INTERVAL_REG, val);
1272        dev_dbg(dev, "Configure WrmsPolTimeInterval 0x%x.\n", val);
1273
1274        /* Enable raq drop */
1275        val = roce_read(hr_dev, ROCEE_GLB_CFG_REG);
1276        tmp = cpu_to_le32(val);
1277        roce_set_bit(tmp, ROCEE_GLB_CFG_TRP_RAQ_DROP_EN_S, 1);
1278        val = le32_to_cpu(tmp);
1279        roce_write(hr_dev, ROCEE_GLB_CFG_REG, val);
1280        dev_dbg(dev, "Configure GlbCfg = 0x%x.\n", val);
1281
1282        return 0;
1283
1284err_dma_alloc_raq:
1285        kfree(raq->e_raq_buf);
1286        return ret;
1287}
1288
1289static void hns_roce_raq_free(struct hns_roce_dev *hr_dev)
1290{
1291        struct device *dev = &hr_dev->pdev->dev;
1292        struct hns_roce_v1_priv *priv;
1293        struct hns_roce_raq_table *raq;
1294
1295        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1296        raq = &priv->raq_table;
1297
1298        dma_free_coherent(dev, HNS_ROCE_V1_RAQ_SIZE, raq->e_raq_buf->buf,
1299                          raq->e_raq_buf->map);
1300        kfree(raq->e_raq_buf);
1301}
1302
1303static void hns_roce_port_enable(struct hns_roce_dev *hr_dev, int enable_flag)
1304{
1305        __le32 tmp;
1306        u32 val;
1307
1308        if (enable_flag) {
1309                val = roce_read(hr_dev, ROCEE_GLB_CFG_REG);
1310                 /* Open all ports */
1311                tmp = cpu_to_le32(val);
1312                roce_set_field(tmp, ROCEE_GLB_CFG_ROCEE_PORT_ST_M,
1313                               ROCEE_GLB_CFG_ROCEE_PORT_ST_S,
1314                               ALL_PORT_VAL_OPEN);
1315                val = le32_to_cpu(tmp);
1316                roce_write(hr_dev, ROCEE_GLB_CFG_REG, val);
1317        } else {
1318                val = roce_read(hr_dev, ROCEE_GLB_CFG_REG);
1319                /* Close all ports */
1320                tmp = cpu_to_le32(val);
1321                roce_set_field(tmp, ROCEE_GLB_CFG_ROCEE_PORT_ST_M,
1322                               ROCEE_GLB_CFG_ROCEE_PORT_ST_S, 0x0);
1323                val = le32_to_cpu(tmp);
1324                roce_write(hr_dev, ROCEE_GLB_CFG_REG, val);
1325        }
1326}
1327
1328static int hns_roce_bt_init(struct hns_roce_dev *hr_dev)
1329{
1330        struct device *dev = &hr_dev->pdev->dev;
1331        struct hns_roce_v1_priv *priv;
1332        int ret;
1333
1334        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1335
1336        priv->bt_table.qpc_buf.buf = dma_alloc_coherent(dev,
1337                HNS_ROCE_BT_RSV_BUF_SIZE, &priv->bt_table.qpc_buf.map,
1338                GFP_KERNEL);
1339        if (!priv->bt_table.qpc_buf.buf)
1340                return -ENOMEM;
1341
1342        priv->bt_table.mtpt_buf.buf = dma_alloc_coherent(dev,
1343                HNS_ROCE_BT_RSV_BUF_SIZE, &priv->bt_table.mtpt_buf.map,
1344                GFP_KERNEL);
1345        if (!priv->bt_table.mtpt_buf.buf) {
1346                ret = -ENOMEM;
1347                goto err_failed_alloc_mtpt_buf;
1348        }
1349
1350        priv->bt_table.cqc_buf.buf = dma_alloc_coherent(dev,
1351                HNS_ROCE_BT_RSV_BUF_SIZE, &priv->bt_table.cqc_buf.map,
1352                GFP_KERNEL);
1353        if (!priv->bt_table.cqc_buf.buf) {
1354                ret = -ENOMEM;
1355                goto err_failed_alloc_cqc_buf;
1356        }
1357
1358        return 0;
1359
1360err_failed_alloc_cqc_buf:
1361        dma_free_coherent(dev, HNS_ROCE_BT_RSV_BUF_SIZE,
1362                priv->bt_table.mtpt_buf.buf, priv->bt_table.mtpt_buf.map);
1363
1364err_failed_alloc_mtpt_buf:
1365        dma_free_coherent(dev, HNS_ROCE_BT_RSV_BUF_SIZE,
1366                priv->bt_table.qpc_buf.buf, priv->bt_table.qpc_buf.map);
1367
1368        return ret;
1369}
1370
1371static void hns_roce_bt_free(struct hns_roce_dev *hr_dev)
1372{
1373        struct device *dev = &hr_dev->pdev->dev;
1374        struct hns_roce_v1_priv *priv;
1375
1376        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1377
1378        dma_free_coherent(dev, HNS_ROCE_BT_RSV_BUF_SIZE,
1379                priv->bt_table.cqc_buf.buf, priv->bt_table.cqc_buf.map);
1380
1381        dma_free_coherent(dev, HNS_ROCE_BT_RSV_BUF_SIZE,
1382                priv->bt_table.mtpt_buf.buf, priv->bt_table.mtpt_buf.map);
1383
1384        dma_free_coherent(dev, HNS_ROCE_BT_RSV_BUF_SIZE,
1385                priv->bt_table.qpc_buf.buf, priv->bt_table.qpc_buf.map);
1386}
1387
1388static int hns_roce_tptr_init(struct hns_roce_dev *hr_dev)
1389{
1390        struct device *dev = &hr_dev->pdev->dev;
1391        struct hns_roce_buf_list *tptr_buf;
1392        struct hns_roce_v1_priv *priv;
1393
1394        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1395        tptr_buf = &priv->tptr_table.tptr_buf;
1396
1397        /*
1398         * This buffer will be used for CQ's tptr(tail pointer), also
1399         * named ci(customer index). Every CQ will use 2 bytes to save
1400         * cqe ci in hip06. Hardware will read this area to get new ci
1401         * when the queue is almost full.
1402         */
1403        tptr_buf->buf = dma_alloc_coherent(dev, HNS_ROCE_V1_TPTR_BUF_SIZE,
1404                                           &tptr_buf->map, GFP_KERNEL);
1405        if (!tptr_buf->buf)
1406                return -ENOMEM;
1407
1408        hr_dev->tptr_dma_addr = tptr_buf->map;
1409        hr_dev->tptr_size = HNS_ROCE_V1_TPTR_BUF_SIZE;
1410
1411        return 0;
1412}
1413
1414static void hns_roce_tptr_free(struct hns_roce_dev *hr_dev)
1415{
1416        struct device *dev = &hr_dev->pdev->dev;
1417        struct hns_roce_buf_list *tptr_buf;
1418        struct hns_roce_v1_priv *priv;
1419
1420        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1421        tptr_buf = &priv->tptr_table.tptr_buf;
1422
1423        dma_free_coherent(dev, HNS_ROCE_V1_TPTR_BUF_SIZE,
1424                          tptr_buf->buf, tptr_buf->map);
1425}
1426
1427static int hns_roce_free_mr_init(struct hns_roce_dev *hr_dev)
1428{
1429        struct device *dev = &hr_dev->pdev->dev;
1430        struct hns_roce_free_mr *free_mr;
1431        struct hns_roce_v1_priv *priv;
1432        int ret = 0;
1433
1434        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1435        free_mr = &priv->free_mr;
1436
1437        free_mr->free_mr_wq = create_singlethread_workqueue("hns_roce_free_mr");
1438        if (!free_mr->free_mr_wq) {
1439                dev_err(dev, "Create free mr workqueue failed!\n");
1440                return -ENOMEM;
1441        }
1442
1443        ret = hns_roce_v1_rsv_lp_qp(hr_dev);
1444        if (ret) {
1445                dev_err(dev, "Reserved loop qp failed(%d)!\n", ret);
1446                flush_workqueue(free_mr->free_mr_wq);
1447                destroy_workqueue(free_mr->free_mr_wq);
1448        }
1449
1450        return ret;
1451}
1452
1453static void hns_roce_free_mr_free(struct hns_roce_dev *hr_dev)
1454{
1455        struct hns_roce_free_mr *free_mr;
1456        struct hns_roce_v1_priv *priv;
1457
1458        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
1459        free_mr = &priv->free_mr;
1460
1461        flush_workqueue(free_mr->free_mr_wq);
1462        destroy_workqueue(free_mr->free_mr_wq);
1463
1464        hns_roce_v1_release_lp_qp(hr_dev);
1465}
1466
1467/**
1468 * hns_roce_v1_reset - reset RoCE
1469 * @hr_dev: RoCE device struct pointer
1470 * @enable: true -- drop reset, false -- reset
1471 * return 0 - success , negative --fail
1472 */
1473static int hns_roce_v1_reset(struct hns_roce_dev *hr_dev, bool dereset)
1474{
1475        struct device_node *dsaf_node;
1476        struct device *dev = &hr_dev->pdev->dev;
1477        struct device_node *np = dev->of_node;
1478        struct fwnode_handle *fwnode;
1479        int ret;
1480
1481        /* check if this is DT/ACPI case */
1482        if (dev_of_node(dev)) {
1483                dsaf_node = of_parse_phandle(np, "dsaf-handle", 0);
1484                if (!dsaf_node) {
1485                        dev_err(dev, "could not find dsaf-handle\n");
1486                        return -EINVAL;
1487                }
1488                fwnode = &dsaf_node->fwnode;
1489        } else if (is_acpi_device_node(dev->fwnode)) {
1490                struct fwnode_reference_args args;
1491
1492                ret = acpi_node_get_property_reference(dev->fwnode,
1493                                                       "dsaf-handle", 0, &args);
1494                if (ret) {
1495                        dev_err(dev, "could not find dsaf-handle\n");
1496                        return ret;
1497                }
1498                fwnode = args.fwnode;
1499        } else {
1500                dev_err(dev, "cannot read data from DT or ACPI\n");
1501                return -ENXIO;
1502        }
1503
1504        ret = hns_dsaf_roce_reset(fwnode, false);
1505        if (ret)
1506                return ret;
1507
1508        if (dereset) {
1509                msleep(SLEEP_TIME_INTERVAL);
1510                ret = hns_dsaf_roce_reset(fwnode, true);
1511        }
1512
1513        return ret;
1514}
1515
1516static int hns_roce_v1_profile(struct hns_roce_dev *hr_dev)
1517{
1518        int i = 0;
1519        struct hns_roce_caps *caps = &hr_dev->caps;
1520
1521        hr_dev->vendor_id = roce_read(hr_dev, ROCEE_VENDOR_ID_REG);
1522        hr_dev->vendor_part_id = roce_read(hr_dev, ROCEE_VENDOR_PART_ID_REG);
1523        hr_dev->sys_image_guid = roce_read(hr_dev, ROCEE_SYS_IMAGE_GUID_L_REG) |
1524                                ((u64)roce_read(hr_dev,
1525                                            ROCEE_SYS_IMAGE_GUID_H_REG) << 32);
1526        hr_dev->hw_rev          = HNS_ROCE_HW_VER1;
1527
1528        caps->num_qps           = HNS_ROCE_V1_MAX_QP_NUM;
1529        caps->max_wqes          = HNS_ROCE_V1_MAX_WQE_NUM;
1530        caps->min_wqes          = HNS_ROCE_MIN_WQE_NUM;
1531        caps->num_cqs           = HNS_ROCE_V1_MAX_CQ_NUM;
1532        caps->min_cqes          = HNS_ROCE_MIN_CQE_NUM;
1533        caps->max_cqes          = HNS_ROCE_V1_MAX_CQE_NUM;
1534        caps->max_sq_sg         = HNS_ROCE_V1_SG_NUM;
1535        caps->max_rq_sg         = HNS_ROCE_V1_SG_NUM;
1536        caps->max_sq_inline     = HNS_ROCE_V1_INLINE_SIZE;
1537        caps->num_uars          = HNS_ROCE_V1_UAR_NUM;
1538        caps->phy_num_uars      = HNS_ROCE_V1_PHY_UAR_NUM;
1539        caps->num_aeq_vectors   = HNS_ROCE_V1_AEQE_VEC_NUM;
1540        caps->num_comp_vectors  = HNS_ROCE_V1_COMP_VEC_NUM;
1541        caps->num_other_vectors = HNS_ROCE_V1_ABNORMAL_VEC_NUM;
1542        caps->num_mtpts         = HNS_ROCE_V1_MAX_MTPT_NUM;
1543        caps->num_mtt_segs      = HNS_ROCE_V1_MAX_MTT_SEGS;
1544        caps->num_pds           = HNS_ROCE_V1_MAX_PD_NUM;
1545        caps->max_qp_init_rdma  = HNS_ROCE_V1_MAX_QP_INIT_RDMA;
1546        caps->max_qp_dest_rdma  = HNS_ROCE_V1_MAX_QP_DEST_RDMA;
1547        caps->max_sq_desc_sz    = HNS_ROCE_V1_MAX_SQ_DESC_SZ;
1548        caps->max_rq_desc_sz    = HNS_ROCE_V1_MAX_RQ_DESC_SZ;
1549        caps->qpc_entry_sz      = HNS_ROCE_V1_QPC_ENTRY_SIZE;
1550        caps->irrl_entry_sz     = HNS_ROCE_V1_IRRL_ENTRY_SIZE;
1551        caps->cqc_entry_sz      = HNS_ROCE_V1_CQC_ENTRY_SIZE;
1552        caps->mtpt_entry_sz     = HNS_ROCE_V1_MTPT_ENTRY_SIZE;
1553        caps->mtt_entry_sz      = HNS_ROCE_V1_MTT_ENTRY_SIZE;
1554        caps->cq_entry_sz       = HNS_ROCE_V1_CQE_ENTRY_SIZE;
1555        caps->page_size_cap     = HNS_ROCE_V1_PAGE_SIZE_SUPPORT;
1556        caps->reserved_lkey     = 0;
1557        caps->reserved_pds      = 0;
1558        caps->reserved_mrws     = 1;
1559        caps->reserved_uars     = 0;
1560        caps->reserved_cqs      = 0;
1561        caps->chunk_sz          = HNS_ROCE_V1_TABLE_CHUNK_SIZE;
1562
1563        for (i = 0; i < caps->num_ports; i++)
1564                caps->pkey_table_len[i] = 1;
1565
1566        for (i = 0; i < caps->num_ports; i++) {
1567                /* Six ports shared 16 GID in v1 engine */
1568                if (i >= (HNS_ROCE_V1_GID_NUM % caps->num_ports))
1569                        caps->gid_table_len[i] = HNS_ROCE_V1_GID_NUM /
1570                                                 caps->num_ports;
1571                else
1572                        caps->gid_table_len[i] = HNS_ROCE_V1_GID_NUM /
1573                                                 caps->num_ports + 1;
1574        }
1575
1576        caps->ceqe_depth = HNS_ROCE_V1_COMP_EQE_NUM;
1577        caps->aeqe_depth = HNS_ROCE_V1_ASYNC_EQE_NUM;
1578        caps->local_ca_ack_delay = roce_read(hr_dev, ROCEE_ACK_DELAY_REG);
1579        caps->max_mtu = IB_MTU_2048;
1580
1581        return 0;
1582}
1583
1584static int hns_roce_v1_init(struct hns_roce_dev *hr_dev)
1585{
1586        int ret;
1587        u32 val;
1588        __le32 tmp;
1589        struct device *dev = &hr_dev->pdev->dev;
1590
1591        /* DMAE user config */
1592        val = roce_read(hr_dev, ROCEE_DMAE_USER_CFG1_REG);
1593        tmp = cpu_to_le32(val);
1594        roce_set_field(tmp, ROCEE_DMAE_USER_CFG1_ROCEE_CACHE_TB_CFG_M,
1595                       ROCEE_DMAE_USER_CFG1_ROCEE_CACHE_TB_CFG_S, 0xf);
1596        roce_set_field(tmp, ROCEE_DMAE_USER_CFG1_ROCEE_STREAM_ID_TB_CFG_M,
1597                       ROCEE_DMAE_USER_CFG1_ROCEE_STREAM_ID_TB_CFG_S,
1598                       1 << PAGES_SHIFT_16);
1599        val = le32_to_cpu(tmp);
1600        roce_write(hr_dev, ROCEE_DMAE_USER_CFG1_REG, val);
1601
1602        val = roce_read(hr_dev, ROCEE_DMAE_USER_CFG2_REG);
1603        tmp = cpu_to_le32(val);
1604        roce_set_field(tmp, ROCEE_DMAE_USER_CFG2_ROCEE_CACHE_PKT_CFG_M,
1605                       ROCEE_DMAE_USER_CFG2_ROCEE_CACHE_PKT_CFG_S, 0xf);
1606        roce_set_field(tmp, ROCEE_DMAE_USER_CFG2_ROCEE_STREAM_ID_PKT_CFG_M,
1607                       ROCEE_DMAE_USER_CFG2_ROCEE_STREAM_ID_PKT_CFG_S,
1608                       1 << PAGES_SHIFT_16);
1609
1610        ret = hns_roce_db_init(hr_dev);
1611        if (ret) {
1612                dev_err(dev, "doorbell init failed!\n");
1613                return ret;
1614        }
1615
1616        ret = hns_roce_raq_init(hr_dev);
1617        if (ret) {
1618                dev_err(dev, "raq init failed!\n");
1619                goto error_failed_raq_init;
1620        }
1621
1622        ret = hns_roce_bt_init(hr_dev);
1623        if (ret) {
1624                dev_err(dev, "bt init failed!\n");
1625                goto error_failed_bt_init;
1626        }
1627
1628        ret = hns_roce_tptr_init(hr_dev);
1629        if (ret) {
1630                dev_err(dev, "tptr init failed!\n");
1631                goto error_failed_tptr_init;
1632        }
1633
1634        ret = hns_roce_free_mr_init(hr_dev);
1635        if (ret) {
1636                dev_err(dev, "free mr init failed!\n");
1637                goto error_failed_free_mr_init;
1638        }
1639
1640        hns_roce_port_enable(hr_dev, HNS_ROCE_PORT_UP);
1641
1642        return 0;
1643
1644error_failed_free_mr_init:
1645        hns_roce_tptr_free(hr_dev);
1646
1647error_failed_tptr_init:
1648        hns_roce_bt_free(hr_dev);
1649
1650error_failed_bt_init:
1651        hns_roce_raq_free(hr_dev);
1652
1653error_failed_raq_init:
1654        hns_roce_db_free(hr_dev);
1655        return ret;
1656}
1657
1658static void hns_roce_v1_exit(struct hns_roce_dev *hr_dev)
1659{
1660        hns_roce_port_enable(hr_dev, HNS_ROCE_PORT_DOWN);
1661        hns_roce_free_mr_free(hr_dev);
1662        hns_roce_tptr_free(hr_dev);
1663        hns_roce_bt_free(hr_dev);
1664        hns_roce_raq_free(hr_dev);
1665        hns_roce_db_free(hr_dev);
1666}
1667
1668static int hns_roce_v1_cmd_pending(struct hns_roce_dev *hr_dev)
1669{
1670        u32 status = readl(hr_dev->reg_base + ROCEE_MB6_REG);
1671
1672        return (!!(status & (1 << HCR_GO_BIT)));
1673}
1674
1675static int hns_roce_v1_post_mbox(struct hns_roce_dev *hr_dev, u64 in_param,
1676                                 u64 out_param, u32 in_modifier, u8 op_modifier,
1677                                 u16 op, u16 token, int event)
1678{
1679        u32 __iomem *hcr = (u32 __iomem *)(hr_dev->reg_base + ROCEE_MB1_REG);
1680        unsigned long end;
1681        u32 val = 0;
1682        __le32 tmp;
1683
1684        end = msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS) + jiffies;
1685        while (hns_roce_v1_cmd_pending(hr_dev)) {
1686                if (time_after(jiffies, end)) {
1687                        dev_err(hr_dev->dev, "jiffies=%d end=%d\n",
1688                                (int)jiffies, (int)end);
1689                        return -EAGAIN;
1690                }
1691                cond_resched();
1692        }
1693
1694        tmp = cpu_to_le32(val);
1695        roce_set_field(tmp, ROCEE_MB6_ROCEE_MB_CMD_M, ROCEE_MB6_ROCEE_MB_CMD_S,
1696                       op);
1697        roce_set_field(tmp, ROCEE_MB6_ROCEE_MB_CMD_MDF_M,
1698                       ROCEE_MB6_ROCEE_MB_CMD_MDF_S, op_modifier);
1699        roce_set_bit(tmp, ROCEE_MB6_ROCEE_MB_EVENT_S, event);
1700        roce_set_bit(tmp, ROCEE_MB6_ROCEE_MB_HW_RUN_S, 1);
1701        roce_set_field(tmp, ROCEE_MB6_ROCEE_MB_TOKEN_M,
1702                       ROCEE_MB6_ROCEE_MB_TOKEN_S, token);
1703
1704        val = le32_to_cpu(tmp);
1705        writeq(in_param, hcr + 0);
1706        writeq(out_param, hcr + 2);
1707        writel(in_modifier, hcr + 4);
1708        /* Memory barrier */
1709        wmb();
1710
1711        writel(val, hcr + 5);
1712
1713        return 0;
1714}
1715
1716static int hns_roce_v1_chk_mbox(struct hns_roce_dev *hr_dev,
1717                                unsigned long timeout)
1718{
1719        u8 __iomem *hcr = hr_dev->reg_base + ROCEE_MB1_REG;
1720        unsigned long end = 0;
1721        u32 status = 0;
1722
1723        end = msecs_to_jiffies(timeout) + jiffies;
1724        while (hns_roce_v1_cmd_pending(hr_dev) && time_before(jiffies, end))
1725                cond_resched();
1726
1727        if (hns_roce_v1_cmd_pending(hr_dev)) {
1728                dev_err(hr_dev->dev, "[cmd_poll]hw run cmd TIMEDOUT!\n");
1729                return -ETIMEDOUT;
1730        }
1731
1732        status = le32_to_cpu((__force __le32)
1733                              __raw_readl(hcr + HCR_STATUS_OFFSET));
1734        if ((status & STATUS_MASK) != 0x1) {
1735                dev_err(hr_dev->dev, "mailbox status 0x%x!\n", status);
1736                return -EBUSY;
1737        }
1738
1739        return 0;
1740}
1741
1742static int hns_roce_v1_set_gid(struct hns_roce_dev *hr_dev, u8 port,
1743                               int gid_index, const union ib_gid *gid,
1744                               const struct ib_gid_attr *attr)
1745{
1746        u32 *p = NULL;
1747        u8 gid_idx = 0;
1748
1749        gid_idx = hns_get_gid_index(hr_dev, port, gid_index);
1750
1751        p = (u32 *)&gid->raw[0];
1752        roce_raw_write(*p, hr_dev->reg_base + ROCEE_PORT_GID_L_0_REG +
1753                       (HNS_ROCE_V1_GID_NUM * gid_idx));
1754
1755        p = (u32 *)&gid->raw[4];
1756        roce_raw_write(*p, hr_dev->reg_base + ROCEE_PORT_GID_ML_0_REG +
1757                       (HNS_ROCE_V1_GID_NUM * gid_idx));
1758
1759        p = (u32 *)&gid->raw[8];
1760        roce_raw_write(*p, hr_dev->reg_base + ROCEE_PORT_GID_MH_0_REG +
1761                       (HNS_ROCE_V1_GID_NUM * gid_idx));
1762
1763        p = (u32 *)&gid->raw[0xc];
1764        roce_raw_write(*p, hr_dev->reg_base + ROCEE_PORT_GID_H_0_REG +
1765                       (HNS_ROCE_V1_GID_NUM * gid_idx));
1766
1767        return 0;
1768}
1769
1770static int hns_roce_v1_set_mac(struct hns_roce_dev *hr_dev, u8 phy_port,
1771                               u8 *addr)
1772{
1773        u32 reg_smac_l;
1774        u16 reg_smac_h;
1775        __le32 tmp;
1776        u16 *p_h;
1777        u32 *p;
1778        u32 val;
1779
1780        /*
1781         * When mac changed, loopback may fail
1782         * because of smac not equal to dmac.
1783         * We Need to release and create reserved qp again.
1784         */
1785        if (hr_dev->hw->dereg_mr) {
1786                int ret;
1787
1788                ret = hns_roce_v1_recreate_lp_qp(hr_dev);
1789                if (ret && ret != -ETIMEDOUT)
1790                        return ret;
1791        }
1792
1793        p = (u32 *)(&addr[0]);
1794        reg_smac_l = *p;
1795        roce_raw_write(reg_smac_l, hr_dev->reg_base + ROCEE_SMAC_L_0_REG +
1796                       PHY_PORT_OFFSET * phy_port);
1797
1798        val = roce_read(hr_dev,
1799                        ROCEE_SMAC_H_0_REG + phy_port * PHY_PORT_OFFSET);
1800        tmp = cpu_to_le32(val);
1801        p_h = (u16 *)(&addr[4]);
1802        reg_smac_h  = *p_h;
1803        roce_set_field(tmp, ROCEE_SMAC_H_ROCEE_SMAC_H_M,
1804                       ROCEE_SMAC_H_ROCEE_SMAC_H_S, reg_smac_h);
1805        val = le32_to_cpu(tmp);
1806        roce_write(hr_dev, ROCEE_SMAC_H_0_REG + phy_port * PHY_PORT_OFFSET,
1807                   val);
1808
1809        return 0;
1810}
1811
1812static void hns_roce_v1_set_mtu(struct hns_roce_dev *hr_dev, u8 phy_port,
1813                                enum ib_mtu mtu)
1814{
1815        __le32 tmp;
1816        u32 val;
1817
1818        val = roce_read(hr_dev,
1819                        ROCEE_SMAC_H_0_REG + phy_port * PHY_PORT_OFFSET);
1820        tmp = cpu_to_le32(val);
1821        roce_set_field(tmp, ROCEE_SMAC_H_ROCEE_PORT_MTU_M,
1822                       ROCEE_SMAC_H_ROCEE_PORT_MTU_S, mtu);
1823        val = le32_to_cpu(tmp);
1824        roce_write(hr_dev, ROCEE_SMAC_H_0_REG + phy_port * PHY_PORT_OFFSET,
1825                   val);
1826}
1827
1828static int hns_roce_v1_write_mtpt(void *mb_buf, struct hns_roce_mr *mr,
1829                                  unsigned long mtpt_idx)
1830{
1831        struct hns_roce_v1_mpt_entry *mpt_entry;
1832        struct sg_dma_page_iter sg_iter;
1833        u64 *pages;
1834        int i;
1835
1836        /* MPT filled into mailbox buf */
1837        mpt_entry = (struct hns_roce_v1_mpt_entry *)mb_buf;
1838        memset(mpt_entry, 0, sizeof(*mpt_entry));
1839
1840        roce_set_field(mpt_entry->mpt_byte_4, MPT_BYTE_4_KEY_STATE_M,
1841                       MPT_BYTE_4_KEY_STATE_S, KEY_VALID);
1842        roce_set_field(mpt_entry->mpt_byte_4, MPT_BYTE_4_KEY_M,
1843                       MPT_BYTE_4_KEY_S, mr->key);
1844        roce_set_field(mpt_entry->mpt_byte_4, MPT_BYTE_4_PAGE_SIZE_M,
1845                       MPT_BYTE_4_PAGE_SIZE_S, MR_SIZE_4K);
1846        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_MW_TYPE_S, 0);
1847        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_MW_BIND_ENABLE_S,
1848                     (mr->access & IB_ACCESS_MW_BIND ? 1 : 0));
1849        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_OWN_S, 0);
1850        roce_set_field(mpt_entry->mpt_byte_4, MPT_BYTE_4_MEMORY_LOCATION_TYPE_M,
1851                       MPT_BYTE_4_MEMORY_LOCATION_TYPE_S, mr->type);
1852        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_REMOTE_ATOMIC_S, 0);
1853        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_LOCAL_WRITE_S,
1854                     (mr->access & IB_ACCESS_LOCAL_WRITE ? 1 : 0));
1855        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_REMOTE_WRITE_S,
1856                     (mr->access & IB_ACCESS_REMOTE_WRITE ? 1 : 0));
1857        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_REMOTE_READ_S,
1858                     (mr->access & IB_ACCESS_REMOTE_READ ? 1 : 0));
1859        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_REMOTE_INVAL_ENABLE_S,
1860                     0);
1861        roce_set_bit(mpt_entry->mpt_byte_4, MPT_BYTE_4_ADDRESS_TYPE_S, 0);
1862
1863        roce_set_field(mpt_entry->mpt_byte_12, MPT_BYTE_12_PBL_ADDR_H_M,
1864                       MPT_BYTE_12_PBL_ADDR_H_S, 0);
1865        roce_set_field(mpt_entry->mpt_byte_12, MPT_BYTE_12_MW_BIND_COUNTER_M,
1866                       MPT_BYTE_12_MW_BIND_COUNTER_S, 0);
1867
1868        mpt_entry->virt_addr_l = cpu_to_le32((u32)mr->iova);
1869        mpt_entry->virt_addr_h = cpu_to_le32((u32)(mr->iova >> 32));
1870        mpt_entry->length = cpu_to_le32((u32)mr->size);
1871
1872        roce_set_field(mpt_entry->mpt_byte_28, MPT_BYTE_28_PD_M,
1873                       MPT_BYTE_28_PD_S, mr->pd);
1874        roce_set_field(mpt_entry->mpt_byte_28, MPT_BYTE_28_L_KEY_IDX_L_M,
1875                       MPT_BYTE_28_L_KEY_IDX_L_S, mtpt_idx);
1876        roce_set_field(mpt_entry->mpt_byte_64, MPT_BYTE_64_L_KEY_IDX_H_M,
1877                       MPT_BYTE_64_L_KEY_IDX_H_S, mtpt_idx >> MTPT_IDX_SHIFT);
1878
1879        /* DMA memory register */
1880        if (mr->type == MR_TYPE_DMA)
1881                return 0;
1882
1883        pages = (u64 *) __get_free_page(GFP_KERNEL);
1884        if (!pages)
1885                return -ENOMEM;
1886
1887        i = 0;
1888        for_each_sg_dma_page(mr->umem->sg_head.sgl, &sg_iter, mr->umem->nmap, 0) {
1889                pages[i] = ((u64)sg_page_iter_dma_address(&sg_iter)) >> 12;
1890
1891                /* Directly record to MTPT table firstly 7 entry */
1892                if (i >= HNS_ROCE_MAX_INNER_MTPT_NUM)
1893                        break;
1894                i++;
1895        }
1896
1897        /* Register user mr */
1898        for (i = 0; i < HNS_ROCE_MAX_INNER_MTPT_NUM; i++) {
1899                switch (i) {
1900                case 0:
1901                        mpt_entry->pa0_l = cpu_to_le32((u32)(pages[i]));
1902                        roce_set_field(mpt_entry->mpt_byte_36,
1903                                MPT_BYTE_36_PA0_H_M,
1904                                MPT_BYTE_36_PA0_H_S,
1905                                (u32)(pages[i] >> PAGES_SHIFT_32));
1906                        break;
1907                case 1:
1908                        roce_set_field(mpt_entry->mpt_byte_36,
1909                                       MPT_BYTE_36_PA1_L_M,
1910                                       MPT_BYTE_36_PA1_L_S, (u32)(pages[i]));
1911                        roce_set_field(mpt_entry->mpt_byte_40,
1912                                MPT_BYTE_40_PA1_H_M,
1913                                MPT_BYTE_40_PA1_H_S,
1914                                (u32)(pages[i] >> PAGES_SHIFT_24));
1915                        break;
1916                case 2:
1917                        roce_set_field(mpt_entry->mpt_byte_40,
1918                                       MPT_BYTE_40_PA2_L_M,
1919                                       MPT_BYTE_40_PA2_L_S, (u32)(pages[i]));
1920                        roce_set_field(mpt_entry->mpt_byte_44,
1921                                MPT_BYTE_44_PA2_H_M,
1922                                MPT_BYTE_44_PA2_H_S,
1923                                (u32)(pages[i] >> PAGES_SHIFT_16));
1924                        break;
1925                case 3:
1926                        roce_set_field(mpt_entry->mpt_byte_44,
1927                                       MPT_BYTE_44_PA3_L_M,
1928                                       MPT_BYTE_44_PA3_L_S, (u32)(pages[i]));
1929                        roce_set_field(mpt_entry->mpt_byte_48,
1930                                MPT_BYTE_48_PA3_H_M,
1931                                MPT_BYTE_48_PA3_H_S,
1932                                (u32)(pages[i] >> PAGES_SHIFT_8));
1933                        break;
1934                case 4:
1935                        mpt_entry->pa4_l = cpu_to_le32((u32)(pages[i]));
1936                        roce_set_field(mpt_entry->mpt_byte_56,
1937                                MPT_BYTE_56_PA4_H_M,
1938                                MPT_BYTE_56_PA4_H_S,
1939                                (u32)(pages[i] >> PAGES_SHIFT_32));
1940                        break;
1941                case 5:
1942                        roce_set_field(mpt_entry->mpt_byte_56,
1943                                       MPT_BYTE_56_PA5_L_M,
1944                                       MPT_BYTE_56_PA5_L_S, (u32)(pages[i]));
1945                        roce_set_field(mpt_entry->mpt_byte_60,
1946                                MPT_BYTE_60_PA5_H_M,
1947                                MPT_BYTE_60_PA5_H_S,
1948                                (u32)(pages[i] >> PAGES_SHIFT_24));
1949                        break;
1950                case 6:
1951                        roce_set_field(mpt_entry->mpt_byte_60,
1952                                       MPT_BYTE_60_PA6_L_M,
1953                                       MPT_BYTE_60_PA6_L_S, (u32)(pages[i]));
1954                        roce_set_field(mpt_entry->mpt_byte_64,
1955                                MPT_BYTE_64_PA6_H_M,
1956                                MPT_BYTE_64_PA6_H_S,
1957                                (u32)(pages[i] >> PAGES_SHIFT_16));
1958                        break;
1959                default:
1960                        break;
1961                }
1962        }
1963
1964        free_page((unsigned long) pages);
1965
1966        mpt_entry->pbl_addr_l = cpu_to_le32((u32)(mr->pbl_dma_addr));
1967
1968        roce_set_field(mpt_entry->mpt_byte_12, MPT_BYTE_12_PBL_ADDR_H_M,
1969                       MPT_BYTE_12_PBL_ADDR_H_S,
1970                       ((u32)(mr->pbl_dma_addr >> 32)));
1971
1972        return 0;
1973}
1974
1975static void *get_cqe(struct hns_roce_cq *hr_cq, int n)
1976{
1977        return hns_roce_buf_offset(&hr_cq->hr_buf.hr_buf,
1978                                   n * HNS_ROCE_V1_CQE_ENTRY_SIZE);
1979}
1980
1981static void *get_sw_cqe(struct hns_roce_cq *hr_cq, int n)
1982{
1983        struct hns_roce_cqe *hr_cqe = get_cqe(hr_cq, n & hr_cq->ib_cq.cqe);
1984
1985        /* Get cqe when Owner bit is Conversely with the MSB of cons_idx */
1986        return (roce_get_bit(hr_cqe->cqe_byte_4, CQE_BYTE_4_OWNER_S) ^
1987                !!(n & (hr_cq->ib_cq.cqe + 1))) ? hr_cqe : NULL;
1988}
1989
1990static struct hns_roce_cqe *next_cqe_sw(struct hns_roce_cq *hr_cq)
1991{
1992        return get_sw_cqe(hr_cq, hr_cq->cons_index);
1993}
1994
1995static void hns_roce_v1_cq_set_ci(struct hns_roce_cq *hr_cq, u32 cons_index)
1996{
1997        __le32 doorbell[2];
1998
1999        doorbell[0] = cpu_to_le32(cons_index & ((hr_cq->cq_depth << 1) - 1));
2000        doorbell[1] = 0;
2001        roce_set_bit(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_HW_SYNS_S, 1);
2002        roce_set_field(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_M,
2003                       ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_S, 3);
2004        roce_set_field(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_MDF_M,
2005                       ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_MDF_S, 0);
2006        roce_set_field(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_INP_H_M,
2007                       ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_INP_H_S, hr_cq->cqn);
2008
2009        hns_roce_write64_k(doorbell, hr_cq->cq_db_l);
2010}
2011
2012static void __hns_roce_v1_cq_clean(struct hns_roce_cq *hr_cq, u32 qpn,
2013                                   struct hns_roce_srq *srq)
2014{
2015        struct hns_roce_cqe *cqe, *dest;
2016        u32 prod_index;
2017        int nfreed = 0;
2018        u8 owner_bit;
2019
2020        for (prod_index = hr_cq->cons_index; get_sw_cqe(hr_cq, prod_index);
2021             ++prod_index) {
2022                if (prod_index == hr_cq->cons_index + hr_cq->ib_cq.cqe)
2023                        break;
2024        }
2025
2026        /*
2027         * Now backwards through the CQ, removing CQ entries
2028         * that match our QP by overwriting them with next entries.
2029         */
2030        while ((int) --prod_index - (int) hr_cq->cons_index >= 0) {
2031                cqe = get_cqe(hr_cq, prod_index & hr_cq->ib_cq.cqe);
2032                if ((roce_get_field(cqe->cqe_byte_16, CQE_BYTE_16_LOCAL_QPN_M,
2033                                     CQE_BYTE_16_LOCAL_QPN_S) &
2034                                     HNS_ROCE_CQE_QPN_MASK) == qpn) {
2035                        /* In v1 engine, not support SRQ */
2036                        ++nfreed;
2037                } else if (nfreed) {
2038                        dest = get_cqe(hr_cq, (prod_index + nfreed) &
2039                                       hr_cq->ib_cq.cqe);
2040                        owner_bit = roce_get_bit(dest->cqe_byte_4,
2041                                                 CQE_BYTE_4_OWNER_S);
2042                        memcpy(dest, cqe, sizeof(*cqe));
2043                        roce_set_bit(dest->cqe_byte_4, CQE_BYTE_4_OWNER_S,
2044                                     owner_bit);
2045                }
2046        }
2047
2048        if (nfreed) {
2049                hr_cq->cons_index += nfreed;
2050                /*
2051                 * Make sure update of buffer contents is done before
2052                 * updating consumer index.
2053                 */
2054                wmb();
2055
2056                hns_roce_v1_cq_set_ci(hr_cq, hr_cq->cons_index);
2057        }
2058}
2059
2060static void hns_roce_v1_cq_clean(struct hns_roce_cq *hr_cq, u32 qpn,
2061                                 struct hns_roce_srq *srq)
2062{
2063        spin_lock_irq(&hr_cq->lock);
2064        __hns_roce_v1_cq_clean(hr_cq, qpn, srq);
2065        spin_unlock_irq(&hr_cq->lock);
2066}
2067
2068static void hns_roce_v1_write_cqc(struct hns_roce_dev *hr_dev,
2069                                  struct hns_roce_cq *hr_cq, void *mb_buf,
2070                                  u64 *mtts, dma_addr_t dma_handle, int nent,
2071                                  u32 vector)
2072{
2073        struct hns_roce_cq_context *cq_context = NULL;
2074        struct hns_roce_buf_list *tptr_buf;
2075        struct hns_roce_v1_priv *priv;
2076        dma_addr_t tptr_dma_addr;
2077        int offset;
2078
2079        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
2080        tptr_buf = &priv->tptr_table.tptr_buf;
2081
2082        cq_context = mb_buf;
2083        memset(cq_context, 0, sizeof(*cq_context));
2084
2085        /* Get the tptr for this CQ. */
2086        offset = hr_cq->cqn * HNS_ROCE_V1_TPTR_ENTRY_SIZE;
2087        tptr_dma_addr = tptr_buf->map + offset;
2088        hr_cq->tptr_addr = (u16 *)(tptr_buf->buf + offset);
2089
2090        /* Register cq_context members */
2091        roce_set_field(cq_context->cqc_byte_4,
2092                       CQ_CONTEXT_CQC_BYTE_4_CQC_STATE_M,
2093                       CQ_CONTEXT_CQC_BYTE_4_CQC_STATE_S, CQ_STATE_VALID);
2094        roce_set_field(cq_context->cqc_byte_4, CQ_CONTEXT_CQC_BYTE_4_CQN_M,
2095                       CQ_CONTEXT_CQC_BYTE_4_CQN_S, hr_cq->cqn);
2096
2097        cq_context->cq_bt_l = cpu_to_le32((u32)dma_handle);
2098
2099        roce_set_field(cq_context->cqc_byte_12,
2100                       CQ_CONTEXT_CQC_BYTE_12_CQ_BT_H_M,
2101                       CQ_CONTEXT_CQC_BYTE_12_CQ_BT_H_S,
2102                       ((u64)dma_handle >> 32));
2103        roce_set_field(cq_context->cqc_byte_12,
2104                       CQ_CONTEXT_CQC_BYTE_12_CQ_CQE_SHIFT_M,
2105                       CQ_CONTEXT_CQC_BYTE_12_CQ_CQE_SHIFT_S,
2106                       ilog2((unsigned int)nent));
2107        roce_set_field(cq_context->cqc_byte_12, CQ_CONTEXT_CQC_BYTE_12_CEQN_M,
2108                       CQ_CONTEXT_CQC_BYTE_12_CEQN_S, vector);
2109
2110        cq_context->cur_cqe_ba0_l = cpu_to_le32((u32)(mtts[0]));
2111
2112        roce_set_field(cq_context->cqc_byte_20,
2113                       CQ_CONTEXT_CQC_BYTE_20_CUR_CQE_BA0_H_M,
2114                       CQ_CONTEXT_CQC_BYTE_20_CUR_CQE_BA0_H_S, (mtts[0]) >> 32);
2115        /* Dedicated hardware, directly set 0 */
2116        roce_set_field(cq_context->cqc_byte_20,
2117                       CQ_CONTEXT_CQC_BYTE_20_CQ_CUR_INDEX_M,
2118                       CQ_CONTEXT_CQC_BYTE_20_CQ_CUR_INDEX_S, 0);
2119        /**
2120         * 44 = 32 + 12, When evaluating addr to hardware, shift 12 because of
2121         * using 4K page, and shift more 32 because of
2122         * caculating the high 32 bit value evaluated to hardware.
2123         */
2124        roce_set_field(cq_context->cqc_byte_20,
2125                       CQ_CONTEXT_CQC_BYTE_20_CQE_TPTR_ADDR_H_M,
2126                       CQ_CONTEXT_CQC_BYTE_20_CQE_TPTR_ADDR_H_S,
2127                       tptr_dma_addr >> 44);
2128
2129        cq_context->cqe_tptr_addr_l = cpu_to_le32((u32)(tptr_dma_addr >> 12));
2130
2131        roce_set_field(cq_context->cqc_byte_32,
2132                       CQ_CONTEXT_CQC_BYTE_32_CUR_CQE_BA1_H_M,
2133                       CQ_CONTEXT_CQC_BYTE_32_CUR_CQE_BA1_H_S, 0);
2134        roce_set_bit(cq_context->cqc_byte_32,
2135                     CQ_CONTEXT_CQC_BYTE_32_SE_FLAG_S, 0);
2136        roce_set_bit(cq_context->cqc_byte_32,
2137                     CQ_CONTEXT_CQC_BYTE_32_CE_FLAG_S, 0);
2138        roce_set_bit(cq_context->cqc_byte_32,
2139                     CQ_CONTEXT_CQC_BYTE_32_NOTIFICATION_FLAG_S, 0);
2140        roce_set_bit(cq_context->cqc_byte_32,
2141                     CQ_CQNTEXT_CQC_BYTE_32_TYPE_OF_COMPLETION_NOTIFICATION_S,
2142                     0);
2143        /* The initial value of cq's ci is 0 */
2144        roce_set_field(cq_context->cqc_byte_32,
2145                       CQ_CONTEXT_CQC_BYTE_32_CQ_CONS_IDX_M,
2146                       CQ_CONTEXT_CQC_BYTE_32_CQ_CONS_IDX_S, 0);
2147}
2148
2149static int hns_roce_v1_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
2150{
2151        return -EOPNOTSUPP;
2152}
2153
2154static int hns_roce_v1_req_notify_cq(struct ib_cq *ibcq,
2155                                     enum ib_cq_notify_flags flags)
2156{
2157        struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
2158        u32 notification_flag;
2159        __le32 doorbell[2];
2160
2161        notification_flag = (flags & IB_CQ_SOLICITED_MASK) ==
2162                            IB_CQ_SOLICITED ? CQ_DB_REQ_NOT : CQ_DB_REQ_NOT_SOL;
2163        /*
2164         * flags = 0; Notification Flag = 1, next
2165         * flags = 1; Notification Flag = 0, solocited
2166         */
2167        doorbell[0] =
2168                cpu_to_le32(hr_cq->cons_index & ((hr_cq->cq_depth << 1) - 1));
2169        roce_set_bit(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_HW_SYNS_S, 1);
2170        roce_set_field(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_M,
2171                       ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_S, 3);
2172        roce_set_field(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_MDF_M,
2173                       ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_CMD_MDF_S, 1);
2174        roce_set_field(doorbell[1], ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_INP_H_M,
2175                       ROCEE_DB_OTHERS_H_ROCEE_DB_OTH_INP_H_S,
2176                       hr_cq->cqn | notification_flag);
2177
2178        hns_roce_write64_k(doorbell, hr_cq->cq_db_l);
2179
2180        return 0;
2181}
2182
2183static int hns_roce_v1_poll_one(struct hns_roce_cq *hr_cq,
2184                                struct hns_roce_qp **cur_qp, struct ib_wc *wc)
2185{
2186        int qpn;
2187        int is_send;
2188        u16 wqe_ctr;
2189        u32 status;
2190        u32 opcode;
2191        struct hns_roce_cqe *cqe;
2192        struct hns_roce_qp *hr_qp;
2193        struct hns_roce_wq *wq;
2194        struct hns_roce_wqe_ctrl_seg *sq_wqe;
2195        struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
2196        struct device *dev = &hr_dev->pdev->dev;
2197
2198        /* Find cqe according consumer index */
2199        cqe = next_cqe_sw(hr_cq);
2200        if (!cqe)
2201                return -EAGAIN;
2202
2203        ++hr_cq->cons_index;
2204        /* Memory barrier */
2205        rmb();
2206        /* 0->SQ, 1->RQ */
2207        is_send  = !(roce_get_bit(cqe->cqe_byte_4, CQE_BYTE_4_SQ_RQ_FLAG_S));
2208
2209        /* Local_qpn in UD cqe is always 1, so it needs to compute new qpn */
2210        if (roce_get_field(cqe->cqe_byte_16, CQE_BYTE_16_LOCAL_QPN_M,
2211                           CQE_BYTE_16_LOCAL_QPN_S) <= 1) {
2212                qpn = roce_get_field(cqe->cqe_byte_20, CQE_BYTE_20_PORT_NUM_M,
2213                                     CQE_BYTE_20_PORT_NUM_S) +
2214                      roce_get_field(cqe->cqe_byte_16, CQE_BYTE_16_LOCAL_QPN_M,
2215                                     CQE_BYTE_16_LOCAL_QPN_S) *
2216                                     HNS_ROCE_MAX_PORTS;
2217        } else {
2218                qpn = roce_get_field(cqe->cqe_byte_16, CQE_BYTE_16_LOCAL_QPN_M,
2219                                     CQE_BYTE_16_LOCAL_QPN_S);
2220        }
2221
2222        if (!*cur_qp || (qpn & HNS_ROCE_CQE_QPN_MASK) != (*cur_qp)->qpn) {
2223                hr_qp = __hns_roce_qp_lookup(hr_dev, qpn);
2224                if (unlikely(!hr_qp)) {
2225                        dev_err(dev, "CQ %06lx with entry for unknown QPN %06x\n",
2226                                hr_cq->cqn, (qpn & HNS_ROCE_CQE_QPN_MASK));
2227                        return -EINVAL;
2228                }
2229
2230                *cur_qp = hr_qp;
2231        }
2232
2233        wc->qp = &(*cur_qp)->ibqp;
2234        wc->vendor_err = 0;
2235
2236        status = roce_get_field(cqe->cqe_byte_4,
2237                                CQE_BYTE_4_STATUS_OF_THE_OPERATION_M,
2238                                CQE_BYTE_4_STATUS_OF_THE_OPERATION_S) &
2239                                HNS_ROCE_CQE_STATUS_MASK;
2240        switch (status) {
2241        case HNS_ROCE_CQE_SUCCESS:
2242                wc->status = IB_WC_SUCCESS;
2243                break;
2244        case HNS_ROCE_CQE_SYNDROME_LOCAL_LENGTH_ERR:
2245                wc->status = IB_WC_LOC_LEN_ERR;
2246                break;
2247        case HNS_ROCE_CQE_SYNDROME_LOCAL_QP_OP_ERR:
2248                wc->status = IB_WC_LOC_QP_OP_ERR;
2249                break;
2250        case HNS_ROCE_CQE_SYNDROME_LOCAL_PROT_ERR:
2251                wc->status = IB_WC_LOC_PROT_ERR;
2252                break;
2253        case HNS_ROCE_CQE_SYNDROME_WR_FLUSH_ERR:
2254                wc->status = IB_WC_WR_FLUSH_ERR;
2255                break;
2256        case HNS_ROCE_CQE_SYNDROME_MEM_MANAGE_OPERATE_ERR:
2257                wc->status = IB_WC_MW_BIND_ERR;
2258                break;
2259        case HNS_ROCE_CQE_SYNDROME_BAD_RESP_ERR:
2260                wc->status = IB_WC_BAD_RESP_ERR;
2261                break;
2262        case HNS_ROCE_CQE_SYNDROME_LOCAL_ACCESS_ERR:
2263                wc->status = IB_WC_LOC_ACCESS_ERR;
2264                break;
2265        case HNS_ROCE_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
2266                wc->status = IB_WC_REM_INV_REQ_ERR;
2267                break;
2268        case HNS_ROCE_CQE_SYNDROME_REMOTE_ACCESS_ERR:
2269                wc->status = IB_WC_REM_ACCESS_ERR;
2270                break;
2271        case HNS_ROCE_CQE_SYNDROME_REMOTE_OP_ERR:
2272                wc->status = IB_WC_REM_OP_ERR;
2273                break;
2274        case HNS_ROCE_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
2275                wc->status = IB_WC_RETRY_EXC_ERR;
2276                break;
2277        case HNS_ROCE_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
2278                wc->status = IB_WC_RNR_RETRY_EXC_ERR;
2279                break;
2280        default:
2281                wc->status = IB_WC_GENERAL_ERR;
2282                break;
2283        }
2284
2285        /* CQE status error, directly return */
2286        if (wc->status != IB_WC_SUCCESS)
2287                return 0;
2288
2289        if (is_send) {
2290                /* SQ conrespond to CQE */
2291                sq_wqe = get_send_wqe(*cur_qp, roce_get_field(cqe->cqe_byte_4,
2292                                                CQE_BYTE_4_WQE_INDEX_M,
2293                                                CQE_BYTE_4_WQE_INDEX_S)&
2294                                                ((*cur_qp)->sq.wqe_cnt-1));
2295                switch (le32_to_cpu(sq_wqe->flag) & HNS_ROCE_WQE_OPCODE_MASK) {
2296                case HNS_ROCE_WQE_OPCODE_SEND:
2297                        wc->opcode = IB_WC_SEND;
2298                        break;
2299                case HNS_ROCE_WQE_OPCODE_RDMA_READ:
2300                        wc->opcode = IB_WC_RDMA_READ;
2301                        wc->byte_len = le32_to_cpu(cqe->byte_cnt);
2302                        break;
2303                case HNS_ROCE_WQE_OPCODE_RDMA_WRITE:
2304                        wc->opcode = IB_WC_RDMA_WRITE;
2305                        break;
2306                case HNS_ROCE_WQE_OPCODE_LOCAL_INV:
2307                        wc->opcode = IB_WC_LOCAL_INV;
2308                        break;
2309                case HNS_ROCE_WQE_OPCODE_UD_SEND:
2310                        wc->opcode = IB_WC_SEND;
2311                        break;
2312                default:
2313                        wc->status = IB_WC_GENERAL_ERR;
2314                        break;
2315                }
2316                wc->wc_flags = (le32_to_cpu(sq_wqe->flag) & HNS_ROCE_WQE_IMM ?
2317                                IB_WC_WITH_IMM : 0);
2318
2319                wq = &(*cur_qp)->sq;
2320                if ((*cur_qp)->sq_signal_bits) {
2321                        /*
2322                         * If sg_signal_bit is 1,
2323                         * firstly tail pointer updated to wqe
2324                         * which current cqe correspond to
2325                         */
2326                        wqe_ctr = (u16)roce_get_field(cqe->cqe_byte_4,
2327                                                      CQE_BYTE_4_WQE_INDEX_M,
2328                                                      CQE_BYTE_4_WQE_INDEX_S);
2329                        wq->tail += (wqe_ctr - (u16)wq->tail) &
2330                                    (wq->wqe_cnt - 1);
2331                }
2332                wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
2333                ++wq->tail;
2334        } else {
2335                /* RQ conrespond to CQE */
2336                wc->byte_len = le32_to_cpu(cqe->byte_cnt);
2337                opcode = roce_get_field(cqe->cqe_byte_4,
2338                                        CQE_BYTE_4_OPERATION_TYPE_M,
2339                                        CQE_BYTE_4_OPERATION_TYPE_S) &
2340                                        HNS_ROCE_CQE_OPCODE_MASK;
2341                switch (opcode) {
2342                case HNS_ROCE_OPCODE_RDMA_WITH_IMM_RECEIVE:
2343                        wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2344                        wc->wc_flags = IB_WC_WITH_IMM;
2345                        wc->ex.imm_data =
2346                                cpu_to_be32(le32_to_cpu(cqe->immediate_data));
2347                        break;
2348                case HNS_ROCE_OPCODE_SEND_DATA_RECEIVE:
2349                        if (roce_get_bit(cqe->cqe_byte_4,
2350                                         CQE_BYTE_4_IMM_INDICATOR_S)) {
2351                                wc->opcode = IB_WC_RECV;
2352                                wc->wc_flags = IB_WC_WITH_IMM;
2353                                wc->ex.imm_data = cpu_to_be32(
2354                                        le32_to_cpu(cqe->immediate_data));
2355                        } else {
2356                                wc->opcode = IB_WC_RECV;
2357                                wc->wc_flags = 0;
2358                        }
2359                        break;
2360                default:
2361                        wc->status = IB_WC_GENERAL_ERR;
2362                        break;
2363                }
2364
2365                /* Update tail pointer, record wr_id */
2366                wq = &(*cur_qp)->rq;
2367                wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
2368                ++wq->tail;
2369                wc->sl = (u8)roce_get_field(cqe->cqe_byte_20, CQE_BYTE_20_SL_M,
2370                                            CQE_BYTE_20_SL_S);
2371                wc->src_qp = (u8)roce_get_field(cqe->cqe_byte_20,
2372                                                CQE_BYTE_20_REMOTE_QPN_M,
2373                                                CQE_BYTE_20_REMOTE_QPN_S);
2374                wc->wc_flags |= (roce_get_bit(cqe->cqe_byte_20,
2375                                              CQE_BYTE_20_GRH_PRESENT_S) ?
2376                                              IB_WC_GRH : 0);
2377                wc->pkey_index = (u16)roce_get_field(cqe->cqe_byte_28,
2378                                                     CQE_BYTE_28_P_KEY_IDX_M,
2379                                                     CQE_BYTE_28_P_KEY_IDX_S);
2380        }
2381
2382        return 0;
2383}
2384
2385int hns_roce_v1_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2386{
2387        struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
2388        struct hns_roce_qp *cur_qp = NULL;
2389        unsigned long flags;
2390        int npolled;
2391        int ret = 0;
2392
2393        spin_lock_irqsave(&hr_cq->lock, flags);
2394
2395        for (npolled = 0; npolled < num_entries; ++npolled) {
2396                ret = hns_roce_v1_poll_one(hr_cq, &cur_qp, wc + npolled);
2397                if (ret)
2398                        break;
2399        }
2400
2401        if (npolled) {
2402                *hr_cq->tptr_addr = hr_cq->cons_index &
2403                        ((hr_cq->cq_depth << 1) - 1);
2404
2405                /* Memroy barrier */
2406                wmb();
2407                hns_roce_v1_cq_set_ci(hr_cq, hr_cq->cons_index);
2408        }
2409
2410        spin_unlock_irqrestore(&hr_cq->lock, flags);
2411
2412        if (ret == 0 || ret == -EAGAIN)
2413                return npolled;
2414        else
2415                return ret;
2416}
2417
2418static int hns_roce_v1_clear_hem(struct hns_roce_dev *hr_dev,
2419                                 struct hns_roce_hem_table *table, int obj,
2420                                 int step_idx)
2421{
2422        struct device *dev = &hr_dev->pdev->dev;
2423        struct hns_roce_v1_priv *priv;
2424        unsigned long end = 0, flags = 0;
2425        __le32 bt_cmd_val[2] = {0};
2426        void __iomem *bt_cmd;
2427        u64 bt_ba = 0;
2428
2429        priv = (struct hns_roce_v1_priv *)hr_dev->priv;
2430
2431        switch (table->type) {
2432        case HEM_TYPE_QPC:
2433                roce_set_field(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_MDF_M,
2434                        ROCEE_BT_CMD_H_ROCEE_BT_CMD_MDF_S, HEM_TYPE_QPC);
2435                bt_ba = priv->bt_table.qpc_buf.map >> 12;
2436                break;
2437        case HEM_TYPE_MTPT:
2438                roce_set_field(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_MDF_M,
2439                        ROCEE_BT_CMD_H_ROCEE_BT_CMD_MDF_S, HEM_TYPE_MTPT);
2440                bt_ba = priv->bt_table.mtpt_buf.map >> 12;
2441                break;
2442        case HEM_TYPE_CQC:
2443                roce_set_field(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_MDF_M,
2444                        ROCEE_BT_CMD_H_ROCEE_BT_CMD_MDF_S, HEM_TYPE_CQC);
2445                bt_ba = priv->bt_table.cqc_buf.map >> 12;
2446                break;
2447        case HEM_TYPE_SRQC:
2448                dev_dbg(dev, "HEM_TYPE_SRQC not support.\n");
2449                return -EINVAL;
2450        default:
2451                return 0;
2452        }
2453        roce_set_field(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_IN_MDF_M,
2454                ROCEE_BT_CMD_H_ROCEE_BT_CMD_IN_MDF_S, obj);
2455        roce_set_bit(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_S, 0);
2456        roce_set_bit(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_HW_SYNS_S, 1);
2457
2458        spin_lock_irqsave(&hr_dev->bt_cmd_lock, flags);
2459
2460        bt_cmd = hr_dev->reg_base + ROCEE_BT_CMD_H_REG;
2461
2462        end = msecs_to_jiffies(HW_SYNC_TIMEOUT_MSECS) + jiffies;
2463        while (1) {
2464                if (readl(bt_cmd) >> BT_CMD_SYNC_SHIFT) {
2465                        if (!(time_before(jiffies, end))) {
2466                                dev_err(dev, "Write bt_cmd err,hw_sync is not zero.\n");
2467                                spin_unlock_irqrestore(&hr_dev->bt_cmd_lock,
2468                                        flags);
2469                                return -EBUSY;
2470                        }
2471                } else {
2472                        break;
2473                }
2474                msleep(HW_SYNC_SLEEP_TIME_INTERVAL);
2475        }
2476
2477        bt_cmd_val[0] = (__le32)bt_ba;
2478        roce_set_field(bt_cmd_val[1], ROCEE_BT_CMD_H_ROCEE_BT_CMD_BA_H_M,
2479                ROCEE_BT_CMD_H_ROCEE_BT_CMD_BA_H_S, bt_ba >> 32);
2480        hns_roce_write64_k(bt_cmd_val, hr_dev->reg_base + ROCEE_BT_CMD_L_REG);
2481
2482        spin_unlock_irqrestore(&hr_dev->bt_cmd_lock, flags);
2483
2484        return 0;
2485}
2486
2487static int hns_roce_v1_qp_modify(struct hns_roce_dev *hr_dev,
2488                                 struct hns_roce_mtt *mtt,
2489                                 enum hns_roce_qp_state cur_state,
2490                                 enum hns_roce_qp_state new_state,
2491                                 struct hns_roce_qp_context *context,
2492                                 struct hns_roce_qp *hr_qp)
2493{
2494        static const u16
2495        op[HNS_ROCE_QP_NUM_STATE][HNS_ROCE_QP_NUM_STATE] = {
2496                [HNS_ROCE_QP_STATE_RST] = {
2497                [HNS_ROCE_QP_STATE_RST] = HNS_ROCE_CMD_2RST_QP,
2498                [HNS_ROCE_QP_STATE_ERR] = HNS_ROCE_CMD_2ERR_QP,
2499                [HNS_ROCE_QP_STATE_INIT] = HNS_ROCE_CMD_RST2INIT_QP,
2500                },
2501                [HNS_ROCE_QP_STATE_INIT] = {
2502                [HNS_ROCE_QP_STATE_RST] = HNS_ROCE_CMD_2RST_QP,
2503                [HNS_ROCE_QP_STATE_ERR] = HNS_ROCE_CMD_2ERR_QP,
2504                /* Note: In v1 engine, HW doesn't support RST2INIT.
2505                 * We use RST2INIT cmd instead of INIT2INIT.
2506                 */
2507                [HNS_ROCE_QP_STATE_INIT] = HNS_ROCE_CMD_RST2INIT_QP,
2508                [HNS_ROCE_QP_STATE_RTR] = HNS_ROCE_CMD_INIT2RTR_QP,
2509                },
2510                [HNS_ROCE_QP_STATE_RTR] = {
2511                [HNS_ROCE_QP_STATE_RST] = HNS_ROCE_CMD_2RST_QP,
2512                [HNS_ROCE_QP_STATE_ERR] = HNS_ROCE_CMD_2ERR_QP,
2513                [HNS_ROCE_QP_STATE_RTS] = HNS_ROCE_CMD_RTR2RTS_QP,
2514                },
2515                [HNS_ROCE_QP_STATE_RTS] = {
2516                [HNS_ROCE_QP_STATE_RST] = HNS_ROCE_CMD_2RST_QP,
2517                [HNS_ROCE_QP_STATE_ERR] = HNS_ROCE_CMD_2ERR_QP,
2518                [HNS_ROCE_QP_STATE_RTS] = HNS_ROCE_CMD_RTS2RTS_QP,
2519                [HNS_ROCE_QP_STATE_SQD] = HNS_ROCE_CMD_RTS2SQD_QP,
2520                },
2521                [HNS_ROCE_QP_STATE_SQD] = {
2522                [HNS_ROCE_QP_STATE_RST] = HNS_ROCE_CMD_2RST_QP,
2523                [HNS_ROCE_QP_STATE_ERR] = HNS_ROCE_CMD_2ERR_QP,
2524                [HNS_ROCE_QP_STATE_RTS] = HNS_ROCE_CMD_SQD2RTS_QP,
2525                [HNS_ROCE_QP_STATE_SQD] = HNS_ROCE_CMD_SQD2SQD_QP,
2526                },
2527                [HNS_ROCE_QP_STATE_ERR] = {
2528                [HNS_ROCE_QP_STATE_RST] = HNS_ROCE_CMD_2RST_QP,
2529                [HNS_ROCE_QP_STATE_ERR] = HNS_ROCE_CMD_2ERR_QP,
2530                }
2531        };
2532
2533        struct hns_roce_cmd_mailbox *mailbox;
2534        struct device *dev = &hr_dev->pdev->dev;
2535        int ret = 0;
2536
2537        if (cur_state >= HNS_ROCE_QP_NUM_STATE ||
2538            new_state >= HNS_ROCE_QP_NUM_STATE ||
2539            !op[cur_state][new_state]) {
2540                dev_err(dev, "[modify_qp]not support state %d to %d\n",
2541                        cur_state, new_state);
2542                return -EINVAL;
2543        }
2544
2545        if (op[cur_state][new_state] == HNS_ROCE_CMD_2RST_QP)
2546                return hns_roce_cmd_mbox(hr_dev, 0, 0, hr_qp->qpn, 2,
2547                                         HNS_ROCE_CMD_2RST_QP,
2548                                         HNS_ROCE_CMD_TIMEOUT_MSECS);
2549
2550        if (op[cur_state][new_state] == HNS_ROCE_CMD_2ERR_QP)
2551                return hns_roce_cmd_mbox(hr_dev, 0, 0, hr_qp->qpn, 2,
2552                                         HNS_ROCE_CMD_2ERR_QP,
2553                                         HNS_ROCE_CMD_TIMEOUT_MSECS);
2554
2555        mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
2556        if (IS_ERR(mailbox))
2557                return PTR_ERR(mailbox);
2558
2559        memcpy(mailbox->buf, context, sizeof(*context));
2560
2561        ret = hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0, hr_qp->qpn, 0,
2562                                op[cur_state][new_state],
2563                                HNS_ROCE_CMD_TIMEOUT_MSECS);
2564
2565        hns_roce_free_cmd_mailbox(hr_dev, mailbox);
2566        return ret;
2567}
2568
2569static int hns_roce_v1_m_sqp(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
2570                             int attr_mask, enum ib_qp_state cur_state,
2571                             enum ib_qp_state new_state)
2572{
2573        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
2574        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
2575        struct hns_roce_sqp_context *context;
2576        struct device *dev = &hr_dev->pdev->dev;
2577        dma_addr_t dma_handle = 0;
2578        u32 __iomem *addr;
2579        int rq_pa_start;
2580        __le32 tmp;
2581        u32 reg_val;
2582        u64 *mtts;
2583
2584        context = kzalloc(sizeof(*context), GFP_KERNEL);
2585        if (!context)
2586                return -ENOMEM;
2587
2588        /* Search QP buf's MTTs */
2589        mtts = hns_roce_table_find(hr_dev, &hr_dev->mr_table.mtt_table,
2590                                   hr_qp->mtt.first_seg, &dma_handle);
2591        if (!mtts) {
2592                dev_err(dev, "qp buf pa find failed\n");
2593                goto out;
2594        }
2595
2596        if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2597                roce_set_field(context->qp1c_bytes_4,
2598                               QP1C_BYTES_4_SQ_WQE_SHIFT_M,
2599                               QP1C_BYTES_4_SQ_WQE_SHIFT_S,
2600                               ilog2((unsigned int)hr_qp->sq.wqe_cnt));
2601                roce_set_field(context->qp1c_bytes_4,
2602                               QP1C_BYTES_4_RQ_WQE_SHIFT_M,
2603                               QP1C_BYTES_4_RQ_WQE_SHIFT_S,
2604                               ilog2((unsigned int)hr_qp->rq.wqe_cnt));
2605                roce_set_field(context->qp1c_bytes_4, QP1C_BYTES_4_PD_M,
2606                               QP1C_BYTES_4_PD_S, to_hr_pd(ibqp->pd)->pdn);
2607
2608                context->sq_rq_bt_l = cpu_to_le32((u32)(dma_handle));
2609                roce_set_field(context->qp1c_bytes_12,
2610                               QP1C_BYTES_12_SQ_RQ_BT_H_M,
2611                               QP1C_BYTES_12_SQ_RQ_BT_H_S,
2612                               ((u32)(dma_handle >> 32)));
2613
2614                roce_set_field(context->qp1c_bytes_16, QP1C_BYTES_16_RQ_HEAD_M,
2615                               QP1C_BYTES_16_RQ_HEAD_S, hr_qp->rq.head);
2616                roce_set_field(context->qp1c_bytes_16, QP1C_BYTES_16_PORT_NUM_M,
2617                               QP1C_BYTES_16_PORT_NUM_S, hr_qp->phy_port);
2618                roce_set_bit(context->qp1c_bytes_16,
2619                             QP1C_BYTES_16_SIGNALING_TYPE_S,
2620                             le32_to_cpu(hr_qp->sq_signal_bits));
2621                roce_set_bit(context->qp1c_bytes_16, QP1C_BYTES_16_RQ_BA_FLG_S,
2622                             1);
2623                roce_set_bit(context->qp1c_bytes_16, QP1C_BYTES_16_SQ_BA_FLG_S,
2624                             1);
2625                roce_set_bit(context->qp1c_bytes_16, QP1C_BYTES_16_QP1_ERR_S,
2626                             0);
2627
2628                roce_set_field(context->qp1c_bytes_20, QP1C_BYTES_20_SQ_HEAD_M,
2629                               QP1C_BYTES_20_SQ_HEAD_S, hr_qp->sq.head);
2630                roce_set_field(context->qp1c_bytes_20, QP1C_BYTES_20_PKEY_IDX_M,
2631                               QP1C_BYTES_20_PKEY_IDX_S, attr->pkey_index);
2632
2633                rq_pa_start = (u32)hr_qp->rq.offset / PAGE_SIZE;
2634                context->cur_rq_wqe_ba_l =
2635                                cpu_to_le32((u32)(mtts[rq_pa_start]));
2636
2637                roce_set_field(context->qp1c_bytes_28,
2638                               QP1C_BYTES_28_CUR_RQ_WQE_BA_H_M,
2639                               QP1C_BYTES_28_CUR_RQ_WQE_BA_H_S,
2640                               (mtts[rq_pa_start]) >> 32);
2641                roce_set_field(context->qp1c_bytes_28,
2642                               QP1C_BYTES_28_RQ_CUR_IDX_M,
2643                               QP1C_BYTES_28_RQ_CUR_IDX_S, 0);
2644
2645                roce_set_field(context->qp1c_bytes_32,
2646                               QP1C_BYTES_32_RX_CQ_NUM_M,
2647                               QP1C_BYTES_32_RX_CQ_NUM_S,
2648                               to_hr_cq(ibqp->recv_cq)->cqn);
2649                roce_set_field(context->qp1c_bytes_32,
2650                               QP1C_BYTES_32_TX_CQ_NUM_M,
2651                               QP1C_BYTES_32_TX_CQ_NUM_S,
2652                               to_hr_cq(ibqp->send_cq)->cqn);
2653
2654                context->cur_sq_wqe_ba_l  = cpu_to_le32((u32)mtts[0]);
2655
2656                roce_set_field(context->qp1c_bytes_40,
2657                               QP1C_BYTES_40_CUR_SQ_WQE_BA_H_M,
2658                               QP1C_BYTES_40_CUR_SQ_WQE_BA_H_S,
2659                               (mtts[0]) >> 32);
2660                roce_set_field(context->qp1c_bytes_40,
2661                               QP1C_BYTES_40_SQ_CUR_IDX_M,
2662                               QP1C_BYTES_40_SQ_CUR_IDX_S, 0);
2663
2664                /* Copy context to QP1C register */
2665                addr = (u32 __iomem *)(hr_dev->reg_base +
2666                                       ROCEE_QP1C_CFG0_0_REG +
2667                                       hr_qp->phy_port * sizeof(*context));
2668
2669                writel(le32_to_cpu(context->qp1c_bytes_4), addr);
2670                writel(le32_to_cpu(context->sq_rq_bt_l), addr + 1);
2671                writel(le32_to_cpu(context->qp1c_bytes_12), addr + 2);
2672                writel(le32_to_cpu(context->qp1c_bytes_16), addr + 3);
2673                writel(le32_to_cpu(context->qp1c_bytes_20), addr + 4);
2674                writel(le32_to_cpu(context->cur_rq_wqe_ba_l), addr + 5);
2675                writel(le32_to_cpu(context->qp1c_bytes_28), addr + 6);
2676                writel(le32_to_cpu(context->qp1c_bytes_32), addr + 7);
2677                writel(le32_to_cpu(context->cur_sq_wqe_ba_l), addr + 8);
2678                writel(le32_to_cpu(context->qp1c_bytes_40), addr + 9);
2679        }
2680
2681        /* Modify QP1C status */
2682        reg_val = roce_read(hr_dev, ROCEE_QP1C_CFG0_0_REG +
2683                            hr_qp->phy_port * sizeof(*context));
2684        tmp = cpu_to_le32(reg_val);
2685        roce_set_field(tmp, ROCEE_QP1C_CFG0_0_ROCEE_QP1C_QP_ST_M,
2686                       ROCEE_QP1C_CFG0_0_ROCEE_QP1C_QP_ST_S, new_state);
2687        reg_val = le32_to_cpu(tmp);
2688        roce_write(hr_dev, ROCEE_QP1C_CFG0_0_REG +
2689                    hr_qp->phy_port * sizeof(*context), reg_val);
2690
2691        hr_qp->state = new_state;
2692        if (new_state == IB_QPS_RESET) {
2693                hns_roce_v1_cq_clean(to_hr_cq(ibqp->recv_cq), hr_qp->qpn,
2694                                     ibqp->srq ? to_hr_srq(ibqp->srq) : NULL);
2695                if (ibqp->send_cq != ibqp->recv_cq)
2696                        hns_roce_v1_cq_clean(to_hr_cq(ibqp->send_cq),
2697                                             hr_qp->qpn, NULL);
2698
2699                hr_qp->rq.head = 0;
2700                hr_qp->rq.tail = 0;
2701                hr_qp->sq.head = 0;
2702                hr_qp->sq.tail = 0;
2703                hr_qp->sq_next_wqe = 0;
2704        }
2705
2706        kfree(context);
2707        return 0;
2708
2709out:
2710        kfree(context);
2711        return -EINVAL;
2712}
2713
2714static int hns_roce_v1_m_qp(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
2715                            int attr_mask, enum ib_qp_state cur_state,
2716                            enum ib_qp_state new_state)
2717{
2718        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
2719        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
2720        struct device *dev = &hr_dev->pdev->dev;
2721        struct hns_roce_qp_context *context;
2722        const struct ib_global_route *grh = rdma_ah_read_grh(&attr->ah_attr);
2723        dma_addr_t dma_handle_2 = 0;
2724        dma_addr_t dma_handle = 0;
2725        __le32 doorbell[2] = {0};
2726        int rq_pa_start = 0;
2727        u64 *mtts_2 = NULL;
2728        int ret = -EINVAL;
2729        u64 *mtts = NULL;
2730        int port;
2731        u8 port_num;
2732        u8 *dmac;
2733        u8 *smac;
2734
2735        context = kzalloc(sizeof(*context), GFP_KERNEL);
2736        if (!context)
2737                return -ENOMEM;
2738
2739        /* Search qp buf's mtts */
2740        mtts = hns_roce_table_find(hr_dev, &hr_dev->mr_table.mtt_table,
2741                                   hr_qp->mtt.first_seg, &dma_handle);
2742        if (mtts == NULL) {
2743                dev_err(dev, "qp buf pa find failed\n");
2744                goto out;
2745        }
2746
2747        /* Search IRRL's mtts */
2748        mtts_2 = hns_roce_table_find(hr_dev, &hr_dev->qp_table.irrl_table,
2749                                     hr_qp->qpn, &dma_handle_2);
2750        if (mtts_2 == NULL) {
2751                dev_err(dev, "qp irrl_table find failed\n");
2752                goto out;
2753        }
2754
2755        /*
2756         * Reset to init
2757         *      Mandatory param:
2758         *      IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT | IB_QP_ACCESS_FLAGS
2759         *      Optional param: NA
2760         */
2761        if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2762                roce_set_field(context->qpc_bytes_4,
2763                               QP_CONTEXT_QPC_BYTES_4_TRANSPORT_SERVICE_TYPE_M,
2764                               QP_CONTEXT_QPC_BYTES_4_TRANSPORT_SERVICE_TYPE_S,
2765                               to_hr_qp_type(hr_qp->ibqp.qp_type));
2766
2767                roce_set_bit(context->qpc_bytes_4,
2768                             QP_CONTEXT_QPC_BYTE_4_ENABLE_FPMR_S, 0);
2769                roce_set_bit(context->qpc_bytes_4,
2770                             QP_CONTEXT_QPC_BYTE_4_RDMA_READ_ENABLE_S,
2771                             !!(attr->qp_access_flags & IB_ACCESS_REMOTE_READ));
2772                roce_set_bit(context->qpc_bytes_4,
2773                             QP_CONTEXT_QPC_BYTE_4_RDMA_WRITE_ENABLE_S,
2774                             !!(attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
2775                             );
2776                roce_set_bit(context->qpc_bytes_4,
2777                             QP_CONTEXT_QPC_BYTE_4_ATOMIC_OPERATION_ENABLE_S,
2778                             !!(attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)
2779                             );
2780                roce_set_bit(context->qpc_bytes_4,
2781                             QP_CONTEXT_QPC_BYTE_4_RDMAR_USE_S, 1);
2782                roce_set_field(context->qpc_bytes_4,
2783                               QP_CONTEXT_QPC_BYTES_4_SQ_WQE_SHIFT_M,
2784                               QP_CONTEXT_QPC_BYTES_4_SQ_WQE_SHIFT_S,
2785                               ilog2((unsigned int)hr_qp->sq.wqe_cnt));
2786                roce_set_field(context->qpc_bytes_4,
2787                               QP_CONTEXT_QPC_BYTES_4_RQ_WQE_SHIFT_M,
2788                               QP_CONTEXT_QPC_BYTES_4_RQ_WQE_SHIFT_S,
2789                               ilog2((unsigned int)hr_qp->rq.wqe_cnt));
2790                roce_set_field(context->qpc_bytes_4,
2791                               QP_CONTEXT_QPC_BYTES_4_PD_M,
2792                               QP_CONTEXT_QPC_BYTES_4_PD_S,
2793                               to_hr_pd(ibqp->pd)->pdn);
2794                hr_qp->access_flags = attr->qp_access_flags;
2795                roce_set_field(context->qpc_bytes_8,
2796                               QP_CONTEXT_QPC_BYTES_8_TX_COMPLETION_M,
2797                               QP_CONTEXT_QPC_BYTES_8_TX_COMPLETION_S,
2798                               to_hr_cq(ibqp->send_cq)->cqn);
2799                roce_set_field(context->qpc_bytes_8,
2800                               QP_CONTEXT_QPC_BYTES_8_RX_COMPLETION_M,
2801                               QP_CONTEXT_QPC_BYTES_8_RX_COMPLETION_S,
2802                               to_hr_cq(ibqp->recv_cq)->cqn);
2803
2804                if (ibqp->srq)
2805                        roce_set_field(context->qpc_bytes_12,
2806                                       QP_CONTEXT_QPC_BYTES_12_SRQ_NUMBER_M,
2807                                       QP_CONTEXT_QPC_BYTES_12_SRQ_NUMBER_S,
2808                                       to_hr_srq(ibqp->srq)->srqn);
2809
2810                roce_set_field(context->qpc_bytes_12,
2811                               QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_M,
2812                               QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_S,
2813                               attr->pkey_index);
2814                hr_qp->pkey_index = attr->pkey_index;
2815                roce_set_field(context->qpc_bytes_16,
2816                               QP_CONTEXT_QPC_BYTES_16_QP_NUM_M,
2817                               QP_CONTEXT_QPC_BYTES_16_QP_NUM_S, hr_qp->qpn);
2818
2819        } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
2820                roce_set_field(context->qpc_bytes_4,
2821                               QP_CONTEXT_QPC_BYTES_4_TRANSPORT_SERVICE_TYPE_M,
2822                               QP_CONTEXT_QPC_BYTES_4_TRANSPORT_SERVICE_TYPE_S,
2823                               to_hr_qp_type(hr_qp->ibqp.qp_type));
2824                roce_set_bit(context->qpc_bytes_4,
2825                             QP_CONTEXT_QPC_BYTE_4_ENABLE_FPMR_S, 0);
2826                if (attr_mask & IB_QP_ACCESS_FLAGS) {
2827                        roce_set_bit(context->qpc_bytes_4,
2828                                     QP_CONTEXT_QPC_BYTE_4_RDMA_READ_ENABLE_S,
2829                                     !!(attr->qp_access_flags &
2830                                     IB_ACCESS_REMOTE_READ));
2831                        roce_set_bit(context->qpc_bytes_4,
2832                                     QP_CONTEXT_QPC_BYTE_4_RDMA_WRITE_ENABLE_S,
2833                                     !!(attr->qp_access_flags &
2834                                     IB_ACCESS_REMOTE_WRITE));
2835                } else {
2836                        roce_set_bit(context->qpc_bytes_4,
2837                                     QP_CONTEXT_QPC_BYTE_4_RDMA_READ_ENABLE_S,
2838                                     !!(hr_qp->access_flags &
2839                                     IB_ACCESS_REMOTE_READ));
2840                        roce_set_bit(context->qpc_bytes_4,
2841                                     QP_CONTEXT_QPC_BYTE_4_RDMA_WRITE_ENABLE_S,
2842                                     !!(hr_qp->access_flags &
2843                                     IB_ACCESS_REMOTE_WRITE));
2844                }
2845
2846                roce_set_bit(context->qpc_bytes_4,
2847                             QP_CONTEXT_QPC_BYTE_4_RDMAR_USE_S, 1);
2848                roce_set_field(context->qpc_bytes_4,
2849                               QP_CONTEXT_QPC_BYTES_4_SQ_WQE_SHIFT_M,
2850                               QP_CONTEXT_QPC_BYTES_4_SQ_WQE_SHIFT_S,
2851                               ilog2((unsigned int)hr_qp->sq.wqe_cnt));
2852                roce_set_field(context->qpc_bytes_4,
2853                               QP_CONTEXT_QPC_BYTES_4_RQ_WQE_SHIFT_M,
2854                               QP_CONTEXT_QPC_BYTES_4_RQ_WQE_SHIFT_S,
2855                               ilog2((unsigned int)hr_qp->rq.wqe_cnt));
2856                roce_set_field(context->qpc_bytes_4,
2857                               QP_CONTEXT_QPC_BYTES_4_PD_M,
2858                               QP_CONTEXT_QPC_BYTES_4_PD_S,
2859                               to_hr_pd(ibqp->pd)->pdn);
2860
2861                roce_set_field(context->qpc_bytes_8,
2862                               QP_CONTEXT_QPC_BYTES_8_TX_COMPLETION_M,
2863                               QP_CONTEXT_QPC_BYTES_8_TX_COMPLETION_S,
2864                               to_hr_cq(ibqp->send_cq)->cqn);
2865                roce_set_field(context->qpc_bytes_8,
2866                               QP_CONTEXT_QPC_BYTES_8_RX_COMPLETION_M,
2867                               QP_CONTEXT_QPC_BYTES_8_RX_COMPLETION_S,
2868                               to_hr_cq(ibqp->recv_cq)->cqn);
2869
2870                if (ibqp->srq)
2871                        roce_set_field(context->qpc_bytes_12,
2872                                       QP_CONTEXT_QPC_BYTES_12_SRQ_NUMBER_M,
2873                                       QP_CONTEXT_QPC_BYTES_12_SRQ_NUMBER_S,
2874                                       to_hr_srq(ibqp->srq)->srqn);
2875                if (attr_mask & IB_QP_PKEY_INDEX)
2876                        roce_set_field(context->qpc_bytes_12,
2877                                       QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_M,
2878                                       QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_S,
2879                                       attr->pkey_index);
2880                else
2881                        roce_set_field(context->qpc_bytes_12,
2882                                       QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_M,
2883                                       QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_S,
2884                                       hr_qp->pkey_index);
2885
2886                roce_set_field(context->qpc_bytes_16,
2887                               QP_CONTEXT_QPC_BYTES_16_QP_NUM_M,
2888                               QP_CONTEXT_QPC_BYTES_16_QP_NUM_S, hr_qp->qpn);
2889        } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
2890                if ((attr_mask & IB_QP_ALT_PATH) ||
2891                    (attr_mask & IB_QP_ACCESS_FLAGS) ||
2892                    (attr_mask & IB_QP_PKEY_INDEX) ||
2893                    (attr_mask & IB_QP_QKEY)) {
2894                        dev_err(dev, "INIT2RTR attr_mask error\n");
2895                        goto out;
2896                }
2897
2898                dmac = (u8 *)attr->ah_attr.roce.dmac;
2899
2900                context->sq_rq_bt_l = cpu_to_le32((u32)(dma_handle));
2901                roce_set_field(context->qpc_bytes_24,
2902                               QP_CONTEXT_QPC_BYTES_24_SQ_RQ_BT_H_M,
2903                               QP_CONTEXT_QPC_BYTES_24_SQ_RQ_BT_H_S,
2904                               ((u32)(dma_handle >> 32)));
2905                roce_set_bit(context->qpc_bytes_24,
2906                             QP_CONTEXT_QPC_BYTE_24_REMOTE_ENABLE_E2E_CREDITS_S,
2907                             1);
2908                roce_set_field(context->qpc_bytes_24,
2909                               QP_CONTEXT_QPC_BYTES_24_MINIMUM_RNR_NAK_TIMER_M,
2910                               QP_CONTEXT_QPC_BYTES_24_MINIMUM_RNR_NAK_TIMER_S,
2911                               attr->min_rnr_timer);
2912                context->irrl_ba_l = cpu_to_le32((u32)(dma_handle_2));
2913                roce_set_field(context->qpc_bytes_32,
2914                               QP_CONTEXT_QPC_BYTES_32_IRRL_BA_H_M,
2915                               QP_CONTEXT_QPC_BYTES_32_IRRL_BA_H_S,
2916                               ((u32)(dma_handle_2 >> 32)) &
2917                                QP_CONTEXT_QPC_BYTES_32_IRRL_BA_H_M);
2918                roce_set_field(context->qpc_bytes_32,
2919                               QP_CONTEXT_QPC_BYTES_32_MIG_STATE_M,
2920                               QP_CONTEXT_QPC_BYTES_32_MIG_STATE_S, 0);
2921                roce_set_bit(context->qpc_bytes_32,
2922                             QP_CONTEXT_QPC_BYTE_32_LOCAL_ENABLE_E2E_CREDITS_S,
2923                             1);
2924                roce_set_bit(context->qpc_bytes_32,
2925                             QP_CONTEXT_QPC_BYTE_32_SIGNALING_TYPE_S,
2926                             le32_to_cpu(hr_qp->sq_signal_bits));
2927
2928                port = (attr_mask & IB_QP_PORT) ? (attr->port_num - 1) :
2929                        hr_qp->port;
2930                smac = (u8 *)hr_dev->dev_addr[port];
2931                /* when dmac equals smac or loop_idc is 1, it should loopback */
2932                if (ether_addr_equal_unaligned(dmac, smac) ||
2933                    hr_dev->loop_idc == 0x1)
2934                        roce_set_bit(context->qpc_bytes_32,
2935                              QP_CONTEXT_QPC_BYTE_32_LOOPBACK_INDICATOR_S, 1);
2936
2937                roce_set_bit(context->qpc_bytes_32,
2938                             QP_CONTEXT_QPC_BYTE_32_GLOBAL_HEADER_S,
2939                             rdma_ah_get_ah_flags(&attr->ah_attr));
2940                roce_set_field(context->qpc_bytes_32,
2941                               QP_CONTEXT_QPC_BYTES_32_RESPONDER_RESOURCES_M,
2942                               QP_CONTEXT_QPC_BYTES_32_RESPONDER_RESOURCES_S,
2943                               ilog2((unsigned int)attr->max_dest_rd_atomic));
2944
2945                if (attr_mask & IB_QP_DEST_QPN)
2946                        roce_set_field(context->qpc_bytes_36,
2947                                       QP_CONTEXT_QPC_BYTES_36_DEST_QP_M,
2948                                       QP_CONTEXT_QPC_BYTES_36_DEST_QP_S,
2949                                       attr->dest_qp_num);
2950
2951                /* Configure GID index */
2952                port_num = rdma_ah_get_port_num(&attr->ah_attr);
2953                roce_set_field(context->qpc_bytes_36,
2954                               QP_CONTEXT_QPC_BYTES_36_SGID_INDEX_M,
2955                               QP_CONTEXT_QPC_BYTES_36_SGID_INDEX_S,
2956                                hns_get_gid_index(hr_dev,
2957                                                  port_num - 1,
2958                                                  grh->sgid_index));
2959
2960                memcpy(&(context->dmac_l), dmac, 4);
2961
2962                roce_set_field(context->qpc_bytes_44,
2963                               QP_CONTEXT_QPC_BYTES_44_DMAC_H_M,
2964                               QP_CONTEXT_QPC_BYTES_44_DMAC_H_S,
2965                               *((u16 *)(&dmac[4])));
2966                roce_set_field(context->qpc_bytes_44,
2967                               QP_CONTEXT_QPC_BYTES_44_MAXIMUM_STATIC_RATE_M,
2968                               QP_CONTEXT_QPC_BYTES_44_MAXIMUM_STATIC_RATE_S,
2969                               rdma_ah_get_static_rate(&attr->ah_attr));
2970                roce_set_field(context->qpc_bytes_44,
2971                               QP_CONTEXT_QPC_BYTES_44_HOPLMT_M,
2972                               QP_CONTEXT_QPC_BYTES_44_HOPLMT_S,
2973                               grh->hop_limit);
2974
2975                roce_set_field(context->qpc_bytes_48,
2976                               QP_CONTEXT_QPC_BYTES_48_FLOWLABEL_M,
2977                               QP_CONTEXT_QPC_BYTES_48_FLOWLABEL_S,
2978                               grh->flow_label);
2979                roce_set_field(context->qpc_bytes_48,
2980                               QP_CONTEXT_QPC_BYTES_48_TCLASS_M,
2981                               QP_CONTEXT_QPC_BYTES_48_TCLASS_S,
2982                               grh->traffic_class);
2983                roce_set_field(context->qpc_bytes_48,
2984                               QP_CONTEXT_QPC_BYTES_48_MTU_M,
2985                               QP_CONTEXT_QPC_BYTES_48_MTU_S, attr->path_mtu);
2986
2987                memcpy(context->dgid, grh->dgid.raw,
2988                       sizeof(grh->dgid.raw));
2989
2990                dev_dbg(dev, "dmac:%x :%lx\n", context->dmac_l,
2991                        roce_get_field(context->qpc_bytes_44,
2992                                       QP_CONTEXT_QPC_BYTES_44_DMAC_H_M,
2993                                       QP_CONTEXT_QPC_BYTES_44_DMAC_H_S));
2994
2995                roce_set_field(context->qpc_bytes_68,
2996                               QP_CONTEXT_QPC_BYTES_68_RQ_HEAD_M,
2997                               QP_CONTEXT_QPC_BYTES_68_RQ_HEAD_S,
2998                               hr_qp->rq.head);
2999                roce_set_field(context->qpc_bytes_68,
3000                               QP_CONTEXT_QPC_BYTES_68_RQ_CUR_INDEX_M,
3001                               QP_CONTEXT_QPC_BYTES_68_RQ_CUR_INDEX_S, 0);
3002
3003                rq_pa_start = (u32)hr_qp->rq.offset / PAGE_SIZE;
3004                context->cur_rq_wqe_ba_l =
3005                                cpu_to_le32((u32)(mtts[rq_pa_start]));
3006
3007                roce_set_field(context->qpc_bytes_76,
3008                        QP_CONTEXT_QPC_BYTES_76_CUR_RQ_WQE_BA_H_M,
3009                        QP_CONTEXT_QPC_BYTES_76_CUR_RQ_WQE_BA_H_S,
3010                        mtts[rq_pa_start] >> 32);
3011                roce_set_field(context->qpc_bytes_76,
3012                               QP_CONTEXT_QPC_BYTES_76_RX_REQ_MSN_M,
3013                               QP_CONTEXT_QPC_BYTES_76_RX_REQ_MSN_S, 0);
3014
3015                context->rx_rnr_time = 0;
3016
3017                roce_set_field(context->qpc_bytes_84,
3018                               QP_CONTEXT_QPC_BYTES_84_LAST_ACK_PSN_M,
3019                               QP_CONTEXT_QPC_BYTES_84_LAST_ACK_PSN_S,
3020                               attr->rq_psn - 1);
3021                roce_set_field(context->qpc_bytes_84,
3022                               QP_CONTEXT_QPC_BYTES_84_TRRL_HEAD_M,
3023                               QP_CONTEXT_QPC_BYTES_84_TRRL_HEAD_S, 0);
3024
3025                roce_set_field(context->qpc_bytes_88,
3026                               QP_CONTEXT_QPC_BYTES_88_RX_REQ_EPSN_M,
3027                               QP_CONTEXT_QPC_BYTES_88_RX_REQ_EPSN_S,
3028                               attr->rq_psn);
3029                roce_set_bit(context->qpc_bytes_88,
3030                             QP_CONTEXT_QPC_BYTES_88_RX_REQ_PSN_ERR_FLAG_S, 0);
3031                roce_set_bit(context->qpc_bytes_88,
3032                             QP_CONTEXT_QPC_BYTES_88_RX_LAST_OPCODE_FLG_S, 0);
3033                roce_set_field(context->qpc_bytes_88,
3034                        QP_CONTEXT_QPC_BYTES_88_RQ_REQ_LAST_OPERATION_TYPE_M,
3035                        QP_CONTEXT_QPC_BYTES_88_RQ_REQ_LAST_OPERATION_TYPE_S,
3036                        0);
3037                roce_set_field(context->qpc_bytes_88,
3038                               QP_CONTEXT_QPC_BYTES_88_RQ_REQ_RDMA_WR_FLAG_M,
3039                               QP_CONTEXT_QPC_BYTES_88_RQ_REQ_RDMA_WR_FLAG_S,
3040                               0);
3041
3042                context->dma_length = 0;
3043                context->r_key = 0;
3044                context->va_l = 0;
3045                context->va_h = 0;
3046
3047                roce_set_field(context->qpc_bytes_108,
3048                               QP_CONTEXT_QPC_BYTES_108_TRRL_SDB_PSN_M,
3049                               QP_CONTEXT_QPC_BYTES_108_TRRL_SDB_PSN_S, 0);
3050                roce_set_bit(context->qpc_bytes_108,
3051                             QP_CONTEXT_QPC_BYTES_108_TRRL_SDB_PSN_FLG_S, 0);
3052                roce_set_bit(context->qpc_bytes_108,
3053                             QP_CONTEXT_QPC_BYTES_108_TRRL_TDB_PSN_FLG_S, 0);
3054
3055                roce_set_field(context->qpc_bytes_112,
3056                               QP_CONTEXT_QPC_BYTES_112_TRRL_TDB_PSN_M,
3057                               QP_CONTEXT_QPC_BYTES_112_TRRL_TDB_PSN_S, 0);
3058                roce_set_field(context->qpc_bytes_112,
3059                               QP_CONTEXT_QPC_BYTES_112_TRRL_TAIL_M,
3060                               QP_CONTEXT_QPC_BYTES_112_TRRL_TAIL_S, 0);
3061
3062                /* For chip resp ack */
3063                roce_set_field(context->qpc_bytes_156,
3064                               QP_CONTEXT_QPC_BYTES_156_PORT_NUM_M,
3065                               QP_CONTEXT_QPC_BYTES_156_PORT_NUM_S,
3066                               hr_qp->phy_port);
3067                roce_set_field(context->qpc_bytes_156,
3068                               QP_CONTEXT_QPC_BYTES_156_SL_M,
3069                               QP_CONTEXT_QPC_BYTES_156_SL_S,
3070                               rdma_ah_get_sl(&attr->ah_attr));
3071                hr_qp->sl = rdma_ah_get_sl(&attr->ah_attr);
3072        } else if (cur_state == IB_QPS_RTR &&
3073                new_state == IB_QPS_RTS) {
3074                /* If exist optional param, return error */
3075                if ((attr_mask & IB_QP_ALT_PATH) ||
3076                    (attr_mask & IB_QP_ACCESS_FLAGS) ||
3077                    (attr_mask & IB_QP_QKEY) ||
3078                    (attr_mask & IB_QP_PATH_MIG_STATE) ||
3079                    (attr_mask & IB_QP_CUR_STATE) ||
3080                    (attr_mask & IB_QP_MIN_RNR_TIMER)) {
3081                        dev_err(dev, "RTR2RTS attr_mask error\n");
3082                        goto out;
3083                }
3084
3085                context->rx_cur_sq_wqe_ba_l = cpu_to_le32((u32)(mtts[0]));
3086
3087                roce_set_field(context->qpc_bytes_120,
3088                               QP_CONTEXT_QPC_BYTES_120_RX_CUR_SQ_WQE_BA_H_M,
3089                               QP_CONTEXT_QPC_BYTES_120_RX_CUR_SQ_WQE_BA_H_S,
3090                               (mtts[0]) >> 32);
3091
3092                roce_set_field(context->qpc_bytes_124,
3093                               QP_CONTEXT_QPC_BYTES_124_RX_ACK_MSN_M,
3094                               QP_CONTEXT_QPC_BYTES_124_RX_ACK_MSN_S, 0);
3095                roce_set_field(context->qpc_bytes_124,
3096                               QP_CONTEXT_QPC_BYTES_124_IRRL_MSG_IDX_M,
3097                               QP_CONTEXT_QPC_BYTES_124_IRRL_MSG_IDX_S, 0);
3098
3099                roce_set_field(context->qpc_bytes_128,
3100                               QP_CONTEXT_QPC_BYTES_128_RX_ACK_EPSN_M,
3101                               QP_CONTEXT_QPC_BYTES_128_RX_ACK_EPSN_S,
3102                               attr->sq_psn);
3103                roce_set_bit(context->qpc_bytes_128,
3104                             QP_CONTEXT_QPC_BYTES_128_RX_ACK_PSN_ERR_FLG_S, 0);
3105                roce_set_field(context->qpc_bytes_128,
3106                             QP_CONTEXT_QPC_BYTES_128_ACK_LAST_OPERATION_TYPE_M,
3107                             QP_CONTEXT_QPC_BYTES_128_ACK_LAST_OPERATION_TYPE_S,
3108                             0);
3109                roce_set_bit(context->qpc_bytes_128,
3110                             QP_CONTEXT_QPC_BYTES_128_IRRL_PSN_VLD_FLG_S, 0);
3111
3112                roce_set_field(context->qpc_bytes_132,
3113                               QP_CONTEXT_QPC_BYTES_132_IRRL_PSN_M,
3114                               QP_CONTEXT_QPC_BYTES_132_IRRL_PSN_S, 0);
3115                roce_set_field(context->qpc_bytes_132,
3116                               QP_CONTEXT_QPC_BYTES_132_IRRL_TAIL_M,
3117                               QP_CONTEXT_QPC_BYTES_132_IRRL_TAIL_S, 0);
3118
3119                roce_set_field(context->qpc_bytes_136,
3120                               QP_CONTEXT_QPC_BYTES_136_RETRY_MSG_PSN_M,
3121                               QP_CONTEXT_QPC_BYTES_136_RETRY_MSG_PSN_S,
3122                               attr->sq_psn);
3123                roce_set_field(context->qpc_bytes_136,
3124                               QP_CONTEXT_QPC_BYTES_136_RETRY_MSG_FPKT_PSN_L_M,
3125                               QP_CONTEXT_QPC_BYTES_136_RETRY_MSG_FPKT_PSN_L_S,
3126                               attr->sq_psn);
3127
3128                roce_set_field(context->qpc_bytes_140,
3129                               QP_CONTEXT_QPC_BYTES_140_RETRY_MSG_FPKT_PSN_H_M,
3130                               QP_CONTEXT_QPC_BYTES_140_RETRY_MSG_FPKT_PSN_H_S,
3131                               (attr->sq_psn >> SQ_PSN_SHIFT));
3132                roce_set_field(context->qpc_bytes_140,
3133                               QP_CONTEXT_QPC_BYTES_140_RETRY_MSG_MSN_M,
3134                               QP_CONTEXT_QPC_BYTES_140_RETRY_MSG_MSN_S, 0);
3135                roce_set_bit(context->qpc_bytes_140,
3136                             QP_CONTEXT_QPC_BYTES_140_RNR_RETRY_FLG_S, 0);
3137
3138                roce_set_field(context->qpc_bytes_148,
3139                               QP_CONTEXT_QPC_BYTES_148_CHECK_FLAG_M,
3140                               QP_CONTEXT_QPC_BYTES_148_CHECK_FLAG_S, 0);
3141                roce_set_field(context->qpc_bytes_148,
3142                               QP_CONTEXT_QPC_BYTES_148_RETRY_COUNT_M,
3143                               QP_CONTEXT_QPC_BYTES_148_RETRY_COUNT_S,
3144                               attr->retry_cnt);
3145                roce_set_field(context->qpc_bytes_148,
3146                               QP_CONTEXT_QPC_BYTES_148_RNR_RETRY_COUNT_M,
3147                               QP_CONTEXT_QPC_BYTES_148_RNR_RETRY_COUNT_S,
3148                               attr->rnr_retry);
3149                roce_set_field(context->qpc_bytes_148,
3150                               QP_CONTEXT_QPC_BYTES_148_LSN_M,
3151                               QP_CONTEXT_QPC_BYTES_148_LSN_S, 0x100);
3152
3153                context->rnr_retry = 0;
3154
3155                roce_set_field(context->qpc_bytes_156,
3156                               QP_CONTEXT_QPC_BYTES_156_RETRY_COUNT_INIT_M,
3157                               QP_CONTEXT_QPC_BYTES_156_RETRY_COUNT_INIT_S,
3158                               attr->retry_cnt);
3159                if (attr->timeout < 0x12) {
3160                        dev_info(dev, "ack timeout value(0x%x) must bigger than 0x12.\n",
3161                                 attr->timeout);
3162                        roce_set_field(context->qpc_bytes_156,
3163                                       QP_CONTEXT_QPC_BYTES_156_ACK_TIMEOUT_M,
3164                                       QP_CONTEXT_QPC_BYTES_156_ACK_TIMEOUT_S,
3165                                       0x12);
3166                } else {
3167                        roce_set_field(context->qpc_bytes_156,
3168                                       QP_CONTEXT_QPC_BYTES_156_ACK_TIMEOUT_M,
3169                                       QP_CONTEXT_QPC_BYTES_156_ACK_TIMEOUT_S,
3170                                       attr->timeout);
3171                }
3172                roce_set_field(context->qpc_bytes_156,
3173                               QP_CONTEXT_QPC_BYTES_156_RNR_RETRY_COUNT_INIT_M,
3174                               QP_CONTEXT_QPC_BYTES_156_RNR_RETRY_COUNT_INIT_S,
3175                               attr->rnr_retry);
3176                roce_set_field(context->qpc_bytes_156,
3177                               QP_CONTEXT_QPC_BYTES_156_PORT_NUM_M,
3178                               QP_CONTEXT_QPC_BYTES_156_PORT_NUM_S,
3179                               hr_qp->phy_port);
3180                roce_set_field(context->qpc_bytes_156,
3181                               QP_CONTEXT_QPC_BYTES_156_SL_M,
3182                               QP_CONTEXT_QPC_BYTES_156_SL_S,
3183                               rdma_ah_get_sl(&attr->ah_attr));
3184                hr_qp->sl = rdma_ah_get_sl(&attr->ah_attr);
3185                roce_set_field(context->qpc_bytes_156,
3186                               QP_CONTEXT_QPC_BYTES_156_INITIATOR_DEPTH_M,
3187                               QP_CONTEXT_QPC_BYTES_156_INITIATOR_DEPTH_S,
3188                               ilog2((unsigned int)attr->max_rd_atomic));
3189                roce_set_field(context->qpc_bytes_156,
3190                               QP_CONTEXT_QPC_BYTES_156_ACK_REQ_IND_M,
3191                               QP_CONTEXT_QPC_BYTES_156_ACK_REQ_IND_S, 0);
3192                context->pkt_use_len = 0;
3193
3194                roce_set_field(context->qpc_bytes_164,
3195                               QP_CONTEXT_QPC_BYTES_164_SQ_PSN_M,
3196                               QP_CONTEXT_QPC_BYTES_164_SQ_PSN_S, attr->sq_psn);
3197                roce_set_field(context->qpc_bytes_164,
3198                               QP_CONTEXT_QPC_BYTES_164_IRRL_HEAD_M,
3199                               QP_CONTEXT_QPC_BYTES_164_IRRL_HEAD_S, 0);
3200
3201                roce_set_field(context->qpc_bytes_168,
3202                               QP_CONTEXT_QPC_BYTES_168_RETRY_SQ_PSN_M,
3203                               QP_CONTEXT_QPC_BYTES_168_RETRY_SQ_PSN_S,
3204                               attr->sq_psn);
3205                roce_set_field(context->qpc_bytes_168,
3206                               QP_CONTEXT_QPC_BYTES_168_SGE_USE_FLA_M,
3207                               QP_CONTEXT_QPC_BYTES_168_SGE_USE_FLA_S, 0);
3208                roce_set_field(context->qpc_bytes_168,
3209                               QP_CONTEXT_QPC_BYTES_168_DB_TYPE_M,
3210                               QP_CONTEXT_QPC_BYTES_168_DB_TYPE_S, 0);
3211                roce_set_bit(context->qpc_bytes_168,
3212                             QP_CONTEXT_QPC_BYTES_168_MSG_LP_IND_S, 0);
3213                roce_set_bit(context->qpc_bytes_168,
3214                             QP_CONTEXT_QPC_BYTES_168_CSDB_LP_IND_S, 0);
3215                roce_set_bit(context->qpc_bytes_168,
3216                             QP_CONTEXT_QPC_BYTES_168_QP_ERR_FLG_S, 0);
3217                context->sge_use_len = 0;
3218
3219                roce_set_field(context->qpc_bytes_176,
3220                               QP_CONTEXT_QPC_BYTES_176_DB_CUR_INDEX_M,
3221                               QP_CONTEXT_QPC_BYTES_176_DB_CUR_INDEX_S, 0);
3222                roce_set_field(context->qpc_bytes_176,
3223                               QP_CONTEXT_QPC_BYTES_176_RETRY_DB_CUR_INDEX_M,
3224                               QP_CONTEXT_QPC_BYTES_176_RETRY_DB_CUR_INDEX_S,
3225                               0);
3226                roce_set_field(context->qpc_bytes_180,
3227                               QP_CONTEXT_QPC_BYTES_180_SQ_CUR_INDEX_M,
3228                               QP_CONTEXT_QPC_BYTES_180_SQ_CUR_INDEX_S, 0);
3229                roce_set_field(context->qpc_bytes_180,
3230                               QP_CONTEXT_QPC_BYTES_180_SQ_HEAD_M,
3231                               QP_CONTEXT_QPC_BYTES_180_SQ_HEAD_S, 0);
3232
3233                context->tx_cur_sq_wqe_ba_l = cpu_to_le32((u32)(mtts[0]));
3234
3235                roce_set_field(context->qpc_bytes_188,
3236                               QP_CONTEXT_QPC_BYTES_188_TX_CUR_SQ_WQE_BA_H_M,
3237                               QP_CONTEXT_QPC_BYTES_188_TX_CUR_SQ_WQE_BA_H_S,
3238                               (mtts[0]) >> 32);
3239                roce_set_bit(context->qpc_bytes_188,
3240                             QP_CONTEXT_QPC_BYTES_188_PKT_RETRY_FLG_S, 0);
3241                roce_set_field(context->qpc_bytes_188,
3242                               QP_CONTEXT_QPC_BYTES_188_TX_RETRY_CUR_INDEX_M,
3243                               QP_CONTEXT_QPC_BYTES_188_TX_RETRY_CUR_INDEX_S,
3244                               0);
3245        } else if (!((cur_state == IB_QPS_INIT && new_state == IB_QPS_RESET) ||
3246                   (cur_state == IB_QPS_INIT && new_state == IB_QPS_ERR) ||
3247                   (cur_state == IB_QPS_RTR && new_state == IB_QPS_RESET) ||
3248                   (cur_state == IB_QPS_RTR && new_state == IB_QPS_ERR) ||
3249                   (cur_state == IB_QPS_RTS && new_state == IB_QPS_RESET) ||
3250                   (cur_state == IB_QPS_RTS && new_state == IB_QPS_ERR) ||
3251                   (cur_state == IB_QPS_ERR && new_state == IB_QPS_RESET) ||
3252                   (cur_state == IB_QPS_ERR && new_state == IB_QPS_ERR))) {
3253                dev_err(dev, "not support this status migration\n");
3254                goto out;
3255        }
3256
3257        /* Every status migrate must change state */
3258        roce_set_field(context->qpc_bytes_144,
3259                       QP_CONTEXT_QPC_BYTES_144_QP_STATE_M,
3260                       QP_CONTEXT_QPC_BYTES_144_QP_STATE_S, new_state);
3261
3262        /* SW pass context to HW */
3263        ret = hns_roce_v1_qp_modify(hr_dev, &hr_qp->mtt,
3264                                    to_hns_roce_state(cur_state),
3265                                    to_hns_roce_state(new_state), context,
3266                                    hr_qp);
3267        if (ret) {
3268                dev_err(dev, "hns_roce_qp_modify failed\n");
3269                goto out;
3270        }
3271
3272        /*
3273         * Use rst2init to instead of init2init with drv,
3274         * need to hw to flash RQ HEAD by DB again
3275         */
3276        if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3277                /* Memory barrier */
3278                wmb();
3279
3280                roce_set_field(doorbell[0], RQ_DOORBELL_U32_4_RQ_HEAD_M,
3281                               RQ_DOORBELL_U32_4_RQ_HEAD_S, hr_qp->rq.head);
3282                roce_set_field(doorbell[1], RQ_DOORBELL_U32_8_QPN_M,
3283                               RQ_DOORBELL_U32_8_QPN_S, hr_qp->qpn);
3284                roce_set_field(doorbell[1], RQ_DOORBELL_U32_8_CMD_M,
3285                               RQ_DOORBELL_U32_8_CMD_S, 1);
3286                roce_set_bit(doorbell[1], RQ_DOORBELL_U32_8_HW_SYNC_S, 1);
3287
3288                if (ibqp->uobject) {
3289                        hr_qp->rq.db_reg_l = hr_dev->reg_base +
3290                                     hr_dev->odb_offset +
3291                                     DB_REG_OFFSET * hr_dev->priv_uar.index;
3292                }
3293
3294                hns_roce_write64_k(doorbell, hr_qp->rq.db_reg_l);
3295        }
3296
3297        hr_qp->state = new_state;
3298
3299        if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
3300                hr_qp->resp_depth = attr->max_dest_rd_atomic;
3301        if (attr_mask & IB_QP_PORT) {
3302                hr_qp->port = attr->port_num - 1;
3303                hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
3304        }
3305
3306        if (new_state == IB_QPS_RESET && !ibqp->uobject) {
3307                hns_roce_v1_cq_clean(to_hr_cq(ibqp->recv_cq), hr_qp->qpn,
3308                                     ibqp->srq ? to_hr_srq(ibqp->srq) : NULL);
3309                if (ibqp->send_cq != ibqp->recv_cq)
3310                        hns_roce_v1_cq_clean(to_hr_cq(ibqp->send_cq),
3311                                             hr_qp->qpn, NULL);
3312
3313                hr_qp->rq.head = 0;
3314                hr_qp->rq.tail = 0;
3315                hr_qp->sq.head = 0;
3316                hr_qp->sq.tail = 0;
3317                hr_qp->sq_next_wqe = 0;
3318        }
3319out:
3320        kfree(context);
3321        return ret;
3322}
3323
3324static int hns_roce_v1_modify_qp(struct ib_qp *ibqp,
3325                                 const struct ib_qp_attr *attr, int attr_mask,
3326                                 enum ib_qp_state cur_state,
3327                                 enum ib_qp_state new_state)
3328{
3329
3330        if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
3331                return hns_roce_v1_m_sqp(ibqp, attr, attr_mask, cur_state,
3332                                         new_state);
3333        else
3334                return hns_roce_v1_m_qp(ibqp, attr, attr_mask, cur_state,
3335                                        new_state);
3336}
3337
3338static enum ib_qp_state to_ib_qp_state(enum hns_roce_qp_state state)
3339{
3340        switch (state) {
3341        case HNS_ROCE_QP_STATE_RST:
3342                return IB_QPS_RESET;
3343        case HNS_ROCE_QP_STATE_INIT:
3344                return IB_QPS_INIT;
3345        case HNS_ROCE_QP_STATE_RTR:
3346                return IB_QPS_RTR;
3347        case HNS_ROCE_QP_STATE_RTS:
3348                return IB_QPS_RTS;
3349        case HNS_ROCE_QP_STATE_SQD:
3350                return IB_QPS_SQD;
3351        case HNS_ROCE_QP_STATE_ERR:
3352                return IB_QPS_ERR;
3353        default:
3354                return IB_QPS_ERR;
3355        }
3356}
3357
3358static int hns_roce_v1_query_qpc(struct hns_roce_dev *hr_dev,
3359                                 struct hns_roce_qp *hr_qp,
3360                                 struct hns_roce_qp_context *hr_context)
3361{
3362        struct hns_roce_cmd_mailbox *mailbox;
3363        int ret;
3364
3365        mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
3366        if (IS_ERR(mailbox))
3367                return PTR_ERR(mailbox);
3368
3369        ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, hr_qp->qpn, 0,
3370                                HNS_ROCE_CMD_QUERY_QP,
3371                                HNS_ROCE_CMD_TIMEOUT_MSECS);
3372        if (!ret)
3373                memcpy(hr_context, mailbox->buf, sizeof(*hr_context));
3374        else
3375                dev_err(&hr_dev->pdev->dev, "QUERY QP cmd process error\n");
3376
3377        hns_roce_free_cmd_mailbox(hr_dev, mailbox);
3378
3379        return ret;
3380}
3381
3382static int hns_roce_v1_q_sqp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
3383                             int qp_attr_mask,
3384                             struct ib_qp_init_attr *qp_init_attr)
3385{
3386        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
3387        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
3388        struct hns_roce_sqp_context context;
3389        u32 addr;
3390
3391        mutex_lock(&hr_qp->mutex);
3392
3393        if (hr_qp->state == IB_QPS_RESET) {
3394                qp_attr->qp_state = IB_QPS_RESET;
3395                goto done;
3396        }
3397
3398        addr = ROCEE_QP1C_CFG0_0_REG +
3399                hr_qp->port * sizeof(struct hns_roce_sqp_context);
3400        context.qp1c_bytes_4 = cpu_to_le32(roce_read(hr_dev, addr));
3401        context.sq_rq_bt_l = cpu_to_le32(roce_read(hr_dev, addr + 1));
3402        context.qp1c_bytes_12 = cpu_to_le32(roce_read(hr_dev, addr + 2));
3403        context.qp1c_bytes_16 = cpu_to_le32(roce_read(hr_dev, addr + 3));
3404        context.qp1c_bytes_20 = cpu_to_le32(roce_read(hr_dev, addr + 4));
3405        context.cur_rq_wqe_ba_l = cpu_to_le32(roce_read(hr_dev, addr + 5));
3406        context.qp1c_bytes_28 = cpu_to_le32(roce_read(hr_dev, addr + 6));
3407        context.qp1c_bytes_32 = cpu_to_le32(roce_read(hr_dev, addr + 7));
3408        context.cur_sq_wqe_ba_l = cpu_to_le32(roce_read(hr_dev, addr + 8));
3409        context.qp1c_bytes_40 = cpu_to_le32(roce_read(hr_dev, addr + 9));
3410
3411        hr_qp->state = roce_get_field(context.qp1c_bytes_4,
3412                                      QP1C_BYTES_4_QP_STATE_M,
3413                                      QP1C_BYTES_4_QP_STATE_S);
3414        qp_attr->qp_state       = hr_qp->state;
3415        qp_attr->path_mtu       = IB_MTU_256;
3416        qp_attr->path_mig_state = IB_MIG_ARMED;
3417        qp_attr->qkey           = QKEY_VAL;
3418        qp_attr->ah_attr.type   = RDMA_AH_ATTR_TYPE_ROCE;
3419        qp_attr->rq_psn         = 0;
3420        qp_attr->sq_psn         = 0;
3421        qp_attr->dest_qp_num    = 1;
3422        qp_attr->qp_access_flags = 6;
3423
3424        qp_attr->pkey_index = roce_get_field(context.qp1c_bytes_20,
3425                                             QP1C_BYTES_20_PKEY_IDX_M,
3426                                             QP1C_BYTES_20_PKEY_IDX_S);
3427        qp_attr->port_num = hr_qp->port + 1;
3428        qp_attr->sq_draining = 0;
3429        qp_attr->max_rd_atomic = 0;
3430        qp_attr->max_dest_rd_atomic = 0;
3431        qp_attr->min_rnr_timer = 0;
3432        qp_attr->timeout = 0;
3433        qp_attr->retry_cnt = 0;
3434        qp_attr->rnr_retry = 0;
3435        qp_attr->alt_timeout = 0;
3436
3437done:
3438        qp_attr->cur_qp_state = qp_attr->qp_state;
3439        qp_attr->cap.max_recv_wr = hr_qp->rq.wqe_cnt;
3440        qp_attr->cap.max_recv_sge = hr_qp->rq.max_gs;
3441        qp_attr->cap.max_send_wr = hr_qp->sq.wqe_cnt;
3442        qp_attr->cap.max_send_sge = hr_qp->sq.max_gs;
3443        qp_attr->cap.max_inline_data = 0;
3444        qp_init_attr->cap = qp_attr->cap;
3445        qp_init_attr->create_flags = 0;
3446
3447        mutex_unlock(&hr_qp->mutex);
3448
3449        return 0;
3450}
3451
3452static int hns_roce_v1_q_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
3453                            int qp_attr_mask,
3454                            struct ib_qp_init_attr *qp_init_attr)
3455{
3456        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
3457        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
3458        struct device *dev = &hr_dev->pdev->dev;
3459        struct hns_roce_qp_context *context;
3460        int tmp_qp_state = 0;
3461        int ret = 0;
3462        int state;
3463
3464        context = kzalloc(sizeof(*context), GFP_KERNEL);
3465        if (!context)
3466                return -ENOMEM;
3467
3468        memset(qp_attr, 0, sizeof(*qp_attr));
3469        memset(qp_init_attr, 0, sizeof(*qp_init_attr));
3470
3471        mutex_lock(&hr_qp->mutex);
3472
3473        if (hr_qp->state == IB_QPS_RESET) {
3474                qp_attr->qp_state = IB_QPS_RESET;
3475                goto done;
3476        }
3477
3478        ret = hns_roce_v1_query_qpc(hr_dev, hr_qp, context);
3479        if (ret) {
3480                dev_err(dev, "query qpc error\n");
3481                ret = -EINVAL;
3482                goto out;
3483        }
3484
3485        state = roce_get_field(context->qpc_bytes_144,
3486                               QP_CONTEXT_QPC_BYTES_144_QP_STATE_M,
3487                               QP_CONTEXT_QPC_BYTES_144_QP_STATE_S);
3488        tmp_qp_state = (int)to_ib_qp_state((enum hns_roce_qp_state)state);
3489        if (tmp_qp_state == -1) {
3490                dev_err(dev, "to_ib_qp_state error\n");
3491                ret = -EINVAL;
3492                goto out;
3493        }
3494        hr_qp->state = (u8)tmp_qp_state;
3495        qp_attr->qp_state = (enum ib_qp_state)hr_qp->state;
3496        qp_attr->path_mtu = (enum ib_mtu)roce_get_field(context->qpc_bytes_48,
3497                                               QP_CONTEXT_QPC_BYTES_48_MTU_M,
3498                                               QP_CONTEXT_QPC_BYTES_48_MTU_S);
3499        qp_attr->path_mig_state = IB_MIG_ARMED;
3500        qp_attr->ah_attr.type   = RDMA_AH_ATTR_TYPE_ROCE;
3501        if (hr_qp->ibqp.qp_type == IB_QPT_UD)
3502                qp_attr->qkey = QKEY_VAL;
3503
3504        qp_attr->rq_psn = roce_get_field(context->qpc_bytes_88,
3505                                         QP_CONTEXT_QPC_BYTES_88_RX_REQ_EPSN_M,
3506                                         QP_CONTEXT_QPC_BYTES_88_RX_REQ_EPSN_S);
3507        qp_attr->sq_psn = (u32)roce_get_field(context->qpc_bytes_164,
3508                                             QP_CONTEXT_QPC_BYTES_164_SQ_PSN_M,
3509                                             QP_CONTEXT_QPC_BYTES_164_SQ_PSN_S);
3510        qp_attr->dest_qp_num = (u8)roce_get_field(context->qpc_bytes_36,
3511                                        QP_CONTEXT_QPC_BYTES_36_DEST_QP_M,
3512                                        QP_CONTEXT_QPC_BYTES_36_DEST_QP_S);
3513        qp_attr->qp_access_flags = ((roce_get_bit(context->qpc_bytes_4,
3514                        QP_CONTEXT_QPC_BYTE_4_RDMA_READ_ENABLE_S)) << 2) |
3515                                   ((roce_get_bit(context->qpc_bytes_4,
3516                        QP_CONTEXT_QPC_BYTE_4_RDMA_WRITE_ENABLE_S)) << 1) |
3517                                   ((roce_get_bit(context->qpc_bytes_4,
3518                        QP_CONTEXT_QPC_BYTE_4_ATOMIC_OPERATION_ENABLE_S)) << 3);
3519
3520        if (hr_qp->ibqp.qp_type == IB_QPT_RC ||
3521            hr_qp->ibqp.qp_type == IB_QPT_UC) {
3522                struct ib_global_route *grh =
3523                        rdma_ah_retrieve_grh(&qp_attr->ah_attr);
3524
3525                rdma_ah_set_sl(&qp_attr->ah_attr,
3526                               roce_get_field(context->qpc_bytes_156,
3527                                              QP_CONTEXT_QPC_BYTES_156_SL_M,
3528                                              QP_CONTEXT_QPC_BYTES_156_SL_S));
3529                rdma_ah_set_ah_flags(&qp_attr->ah_attr, IB_AH_GRH);
3530                grh->flow_label =
3531                        roce_get_field(context->qpc_bytes_48,
3532                                       QP_CONTEXT_QPC_BYTES_48_FLOWLABEL_M,
3533                                       QP_CONTEXT_QPC_BYTES_48_FLOWLABEL_S);
3534                grh->sgid_index =
3535                        roce_get_field(context->qpc_bytes_36,
3536                                       QP_CONTEXT_QPC_BYTES_36_SGID_INDEX_M,
3537                                       QP_CONTEXT_QPC_BYTES_36_SGID_INDEX_S);
3538                grh->hop_limit =
3539                        roce_get_field(context->qpc_bytes_44,
3540                                       QP_CONTEXT_QPC_BYTES_44_HOPLMT_M,
3541                                       QP_CONTEXT_QPC_BYTES_44_HOPLMT_S);
3542                grh->traffic_class =
3543                        roce_get_field(context->qpc_bytes_48,
3544                                       QP_CONTEXT_QPC_BYTES_48_TCLASS_M,
3545                                       QP_CONTEXT_QPC_BYTES_48_TCLASS_S);
3546
3547                memcpy(grh->dgid.raw, context->dgid,
3548                       sizeof(grh->dgid.raw));
3549        }
3550
3551        qp_attr->pkey_index = roce_get_field(context->qpc_bytes_12,
3552                              QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_M,
3553                              QP_CONTEXT_QPC_BYTES_12_P_KEY_INDEX_S);
3554        qp_attr->port_num = hr_qp->port + 1;
3555        qp_attr->sq_draining = 0;
3556        qp_attr->max_rd_atomic = 1 << roce_get_field(context->qpc_bytes_156,
3557                                 QP_CONTEXT_QPC_BYTES_156_INITIATOR_DEPTH_M,
3558                                 QP_CONTEXT_QPC_BYTES_156_INITIATOR_DEPTH_S);
3559        qp_attr->max_dest_rd_atomic = 1 << roce_get_field(context->qpc_bytes_32,
3560                                 QP_CONTEXT_QPC_BYTES_32_RESPONDER_RESOURCES_M,
3561                                 QP_CONTEXT_QPC_BYTES_32_RESPONDER_RESOURCES_S);
3562        qp_attr->min_rnr_timer = (u8)(roce_get_field(context->qpc_bytes_24,
3563                        QP_CONTEXT_QPC_BYTES_24_MINIMUM_RNR_NAK_TIMER_M,
3564                        QP_CONTEXT_QPC_BYTES_24_MINIMUM_RNR_NAK_TIMER_S));
3565        qp_attr->timeout = (u8)(roce_get_field(context->qpc_bytes_156,
3566                            QP_CONTEXT_QPC_BYTES_156_ACK_TIMEOUT_M,
3567                            QP_CONTEXT_QPC_BYTES_156_ACK_TIMEOUT_S));
3568        qp_attr->retry_cnt = roce_get_field(context->qpc_bytes_148,
3569                             QP_CONTEXT_QPC_BYTES_148_RETRY_COUNT_M,
3570                             QP_CONTEXT_QPC_BYTES_148_RETRY_COUNT_S);
3571        qp_attr->rnr_retry = (u8)context->rnr_retry;
3572
3573done:
3574        qp_attr->cur_qp_state = qp_attr->qp_state;
3575        qp_attr->cap.max_recv_wr = hr_qp->rq.wqe_cnt;
3576        qp_attr->cap.max_recv_sge = hr_qp->rq.max_gs;
3577
3578        if (!ibqp->uobject) {
3579                qp_attr->cap.max_send_wr = hr_qp->sq.wqe_cnt;
3580                qp_attr->cap.max_send_sge = hr_qp->sq.max_gs;
3581        } else {
3582                qp_attr->cap.max_send_wr = 0;
3583                qp_attr->cap.max_send_sge = 0;
3584        }
3585
3586        qp_init_attr->cap = qp_attr->cap;
3587
3588out:
3589        mutex_unlock(&hr_qp->mutex);
3590        kfree(context);
3591        return ret;
3592}
3593
3594static int hns_roce_v1_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
3595                                int qp_attr_mask,
3596                                struct ib_qp_init_attr *qp_init_attr)
3597{
3598        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
3599
3600        return hr_qp->doorbell_qpn <= 1 ?
3601                hns_roce_v1_q_sqp(ibqp, qp_attr, qp_attr_mask, qp_init_attr) :
3602                hns_roce_v1_q_qp(ibqp, qp_attr, qp_attr_mask, qp_init_attr);
3603}
3604
3605int hns_roce_v1_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
3606{
3607        struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
3608        struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
3609        struct hns_roce_cq *send_cq, *recv_cq;
3610        int ret;
3611
3612        ret = hns_roce_v1_modify_qp(ibqp, NULL, 0, hr_qp->state, IB_QPS_RESET);
3613        if (ret)
3614                return ret;
3615
3616        send_cq = to_hr_cq(hr_qp->ibqp.send_cq);
3617        recv_cq = to_hr_cq(hr_qp->ibqp.recv_cq);
3618
3619        hns_roce_lock_cqs(send_cq, recv_cq);
3620        if (!udata) {
3621                __hns_roce_v1_cq_clean(recv_cq, hr_qp->qpn, hr_qp->ibqp.srq ?
3622                                       to_hr_srq(hr_qp->ibqp.srq) : NULL);
3623                if (send_cq != recv_cq)
3624                        __hns_roce_v1_cq_clean(send_cq, hr_qp->qpn, NULL);
3625        }
3626        hns_roce_unlock_cqs(send_cq, recv_cq);
3627
3628        hns_roce_qp_remove(hr_dev, hr_qp);
3629        hns_roce_qp_free(hr_dev, hr_qp);
3630
3631        /* RC QP, release QPN */
3632        if (hr_qp->ibqp.qp_type == IB_QPT_RC)
3633                hns_roce_release_range_qp(hr_dev, hr_qp->qpn, 1);
3634
3635        hns_roce_mtt_cleanup(hr_dev, &hr_qp->mtt);
3636
3637        ib_umem_release(hr_qp->umem);
3638        if (!udata) {
3639                kfree(hr_qp->sq.wrid);
3640                kfree(hr_qp->rq.wrid);
3641
3642                hns_roce_buf_free(hr_dev, hr_qp->buff_size, &hr_qp->hr_buf);
3643        }
3644
3645        if (hr_qp->ibqp.qp_type == IB_QPT_RC)
3646                kfree(hr_qp);
3647        else
3648                kfree(hr_to_hr_sqp(hr_qp));
3649        return 0;
3650}
3651
3652static void hns_roce_v1_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
3653{
3654        struct hns_roce_dev *hr_dev = to_hr_dev(ibcq->device);
3655        struct hns_roce_cq *hr_cq = to_hr_cq(ibcq);
3656        struct device *dev = &hr_dev->pdev->dev;
3657        u32 cqe_cnt_ori;
3658        u32 cqe_cnt_cur;
3659        u32 cq_buf_size;
3660        int wait_time = 0;
3661
3662        hns_roce_free_cq(hr_dev, hr_cq);
3663
3664        /*
3665         * Before freeing cq buffer, we need to ensure that the outstanding CQE
3666         * have been written by checking the CQE counter.
3667         */
3668        cqe_cnt_ori = roce_read(hr_dev, ROCEE_SCAEP_WR_CQE_CNT);
3669        while (1) {
3670                if (roce_read(hr_dev, ROCEE_CAEP_CQE_WCMD_EMPTY) &
3671                    HNS_ROCE_CQE_WCMD_EMPTY_BIT)
3672                        break;
3673
3674                cqe_cnt_cur = roce_read(hr_dev, ROCEE_SCAEP_WR_CQE_CNT);
3675                if ((cqe_cnt_cur - cqe_cnt_ori) >= HNS_ROCE_MIN_CQE_CNT)
3676                        break;
3677
3678                msleep(HNS_ROCE_EACH_FREE_CQ_WAIT_MSECS);
3679                if (wait_time > HNS_ROCE_MAX_FREE_CQ_WAIT_CNT) {
3680                        dev_warn(dev, "Destroy cq 0x%lx timeout!\n",
3681                                hr_cq->cqn);
3682                        break;
3683                }
3684                wait_time++;
3685        }
3686
3687        hns_roce_mtt_cleanup(hr_dev, &hr_cq->hr_buf.hr_mtt);
3688
3689        ib_umem_release(hr_cq->umem);
3690        if (!udata) {
3691                /* Free the buff of stored cq */
3692                cq_buf_size = (ibcq->cqe + 1) * hr_dev->caps.cq_entry_sz;
3693                hns_roce_buf_free(hr_dev, cq_buf_size, &hr_cq->hr_buf.hr_buf);
3694        }
3695}
3696
3697static void set_eq_cons_index_v1(struct hns_roce_eq *eq, int req_not)
3698{
3699        roce_raw_write((eq->cons_index & HNS_ROCE_V1_CONS_IDX_M) |
3700                      (req_not << eq->log_entries), eq->doorbell);
3701}
3702
3703static void hns_roce_v1_wq_catas_err_handle(struct hns_roce_dev *hr_dev,
3704                                            struct hns_roce_aeqe *aeqe, int qpn)
3705{
3706        struct device *dev = &hr_dev->pdev->dev;
3707
3708        dev_warn(dev, "Local Work Queue Catastrophic Error.\n");
3709        switch (roce_get_field(aeqe->asyn, HNS_ROCE_AEQE_U32_4_EVENT_SUB_TYPE_M,
3710                               HNS_ROCE_AEQE_U32_4_EVENT_SUB_TYPE_S)) {
3711        case HNS_ROCE_LWQCE_QPC_ERROR:
3712                dev_warn(dev, "QP %d, QPC error.\n", qpn);
3713                break;
3714        case HNS_ROCE_LWQCE_MTU_ERROR:
3715                dev_warn(dev, "QP %d, MTU error.\n", qpn);
3716                break;
3717        case HNS_ROCE_LWQCE_WQE_BA_ADDR_ERROR:
3718                dev_warn(dev, "QP %d, WQE BA addr error.\n", qpn);
3719                break;
3720        case HNS_ROCE_LWQCE_WQE_ADDR_ERROR:
3721                dev_warn(dev, "QP %d, WQE addr error.\n", qpn);
3722                break;
3723        case HNS_ROCE_LWQCE_SQ_WQE_SHIFT_ERROR:
3724                dev_warn(dev, "QP %d, WQE shift error\n", qpn);
3725                break;
3726        case HNS_ROCE_LWQCE_SL_ERROR:
3727                dev_warn(dev, "QP %d, SL error.\n", qpn);
3728                break;
3729        case HNS_ROCE_LWQCE_PORT_ERROR:
3730                dev_warn(dev, "QP %d, port error.\n", qpn);
3731                break;
3732        default:
3733                break;
3734        }
3735}
3736
3737static void hns_roce_v1_local_wq_access_err_handle(struct hns_roce_dev *hr_dev,
3738                                                   struct hns_roce_aeqe *aeqe,
3739                                                   int qpn)
3740{
3741        struct device *dev = &hr_dev->pdev->dev;
3742
3743        dev_warn(dev, "Local Access Violation Work Queue Error.\n");
3744        switch (roce_get_field(aeqe->asyn, HNS_ROCE_AEQE_U32_4_EVENT_SUB_TYPE_M,
3745                               HNS_ROCE_AEQE_U32_4_EVENT_SUB_TYPE_S)) {
3746        case HNS_ROCE_LAVWQE_R_KEY_VIOLATION:
3747                dev_warn(dev, "QP %d, R_key violation.\n", qpn);
3748                break;
3749        case HNS_ROCE_LAVWQE_LENGTH_ERROR:
3750                dev_warn(dev, "QP %d, length error.\n", qpn);
3751                break;
3752        case HNS_ROCE_LAVWQE_VA_ERROR:
3753                dev_warn(dev, "QP %d, VA error.\n", qpn);
3754                break;
3755        case HNS_ROCE_LAVWQE_PD_ERROR:
3756                dev_err(dev, "QP %d, PD error.\n", qpn);
3757                break;
3758        case HNS_ROCE_LAVWQE_RW_ACC_ERROR:
3759                dev_warn(dev, "QP %d, rw acc error.\n", qpn);
3760                break;
3761        case HNS_ROCE_LAVWQE_KEY_STATE_ERROR:
3762                dev_warn(dev, "QP %d, key state error.\n", qpn);
3763                break;
3764        case HNS_ROCE_LAVWQE_MR_OPERATION_ERROR:
3765                dev_warn(dev, "QP %d, MR operation error.\n", qpn);
3766                break;
3767        default:
3768                break;
3769        }
3770}
3771
3772static void hns_roce_v1_qp_err_handle(struct hns_roce_dev *hr_dev,
3773                                      struct hns_roce_aeqe *aeqe,
3774                                      int event_type)
3775{
3776        struct device *dev = &hr_dev->pdev->dev;
3777        int phy_port;
3778        int qpn;
3779
3780        qpn = roce_get_field(aeqe->event.qp_event.qp,
3781                             HNS_ROCE_AEQE_EVENT_QP_EVENT_QP_QPN_M,
3782                             HNS_ROCE_AEQE_EVENT_QP_EVENT_QP_QPN_S);
3783        phy_port = roce_get_field(aeqe->event.qp_event.qp,
3784                                  HNS_ROCE_AEQE_EVENT_QP_EVENT_PORT_NUM_M,
3785                                  HNS_ROCE_AEQE_EVENT_QP_EVENT_PORT_NUM_S);
3786        if (qpn <= 1)
3787                qpn = HNS_ROCE_MAX_PORTS * qpn + phy_port;
3788
3789        switch (event_type) {
3790        case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
3791                dev_warn(dev, "Invalid Req Local Work Queue Error.\n"
3792                         "QP %d, phy_port %d.\n", qpn, phy_port);
3793                break;
3794        case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
3795                hns_roce_v1_wq_catas_err_handle(hr_dev, aeqe, qpn);
3796                break;
3797        case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
3798                hns_roce_v1_local_wq_access_err_handle(hr_dev, aeqe, qpn);
3799                break;
3800        default:
3801                break;
3802        }
3803
3804        hns_roce_qp_event(hr_dev, qpn, event_type);
3805}
3806
3807static void hns_roce_v1_cq_err_handle(struct hns_roce_dev *hr_dev,
3808                                      struct hns_roce_aeqe *aeqe,
3809                                      int event_type)
3810{
3811        struct device *dev = &hr_dev->pdev->dev;
3812        u32 cqn;
3813
3814        cqn = roce_get_field(aeqe->event.cq_event.cq,
3815                          HNS_ROCE_AEQE_EVENT_CQ_EVENT_CQ_CQN_M,
3816                          HNS_ROCE_AEQE_EVENT_CQ_EVENT_CQ_CQN_S);
3817
3818        switch (event_type) {
3819        case HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR:
3820                dev_warn(dev, "CQ 0x%x access err.\n", cqn);
3821                break;
3822        case HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW:
3823                dev_warn(dev, "CQ 0x%x overflow\n", cqn);
3824                break;
3825        case HNS_ROCE_EVENT_TYPE_CQ_ID_INVALID:
3826                dev_warn(dev, "CQ 0x%x ID invalid.\n", cqn);
3827                break;
3828        default:
3829                break;
3830        }
3831
3832        hns_roce_cq_event(hr_dev, cqn, event_type);
3833}
3834
3835static void hns_roce_v1_db_overflow_handle(struct hns_roce_dev *hr_dev,
3836                                           struct hns_roce_aeqe *aeqe)
3837{
3838        struct device *dev = &hr_dev->pdev->dev;
3839
3840        switch (roce_get_field(aeqe->asyn, HNS_ROCE_AEQE_U32_4_EVENT_SUB_TYPE_M,
3841                               HNS_ROCE_AEQE_U32_4_EVENT_SUB_TYPE_S)) {
3842        case HNS_ROCE_DB_SUBTYPE_SDB_OVF:
3843                dev_warn(dev, "SDB overflow.\n");
3844                break;
3845        case HNS_ROCE_DB_SUBTYPE_SDB_ALM_OVF:
3846                dev_warn(dev, "SDB almost overflow.\n");
3847                break;
3848        case HNS_ROCE_DB_SUBTYPE_SDB_ALM_EMP:
3849                dev_warn(dev, "SDB almost empty.\n");
3850                break;
3851        case HNS_ROCE_DB_SUBTYPE_ODB_OVF:
3852                dev_warn(dev, "ODB overflow.\n");
3853                break;
3854        case HNS_ROCE_DB_SUBTYPE_ODB_ALM_OVF:
3855                dev_warn(dev, "ODB almost overflow.\n");
3856                break;
3857        case HNS_ROCE_DB_SUBTYPE_ODB_ALM_EMP:
3858                dev_warn(dev, "SDB almost empty.\n");
3859                break;
3860        default:
3861                break;
3862        }
3863}
3864
3865static struct hns_roce_aeqe *get_aeqe_v1(struct hns_roce_eq *eq, u32 entry)
3866{
3867        unsigned long off = (entry & (eq->entries - 1)) *
3868                             HNS_ROCE_AEQ_ENTRY_SIZE;
3869
3870        return (struct hns_roce_aeqe *)((u8 *)
3871                (eq->buf_list[off / HNS_ROCE_BA_SIZE].buf) +
3872                off % HNS_ROCE_BA_SIZE);
3873}
3874
3875static struct hns_roce_aeqe *next_aeqe_sw_v1(struct hns_roce_eq *eq)
3876{
3877        struct hns_roce_aeqe *aeqe = get_aeqe_v1(eq, eq->cons_index);
3878
3879        return (roce_get_bit(aeqe->asyn, HNS_ROCE_AEQE_U32_4_OWNER_S) ^
3880                !!(eq->cons_index & eq->entries)) ? aeqe : NULL;
3881}
3882
3883static int hns_roce_v1_aeq_int(struct hns_roce_dev *hr_dev,
3884                               struct hns_roce_eq *eq)
3885{
3886        struct device *dev = &hr_dev->pdev->dev;
3887        struct hns_roce_aeqe *aeqe;
3888        int aeqes_found = 0;
3889        int event_type;
3890
3891        while ((aeqe = next_aeqe_sw_v1(eq))) {
3892
3893                /* Make sure we read the AEQ entry after we have checked the
3894                 * ownership bit
3895                 */
3896                dma_rmb();
3897
3898                dev_dbg(dev, "aeqe = %p, aeqe->asyn.event_type = 0x%lx\n", aeqe,
3899                        roce_get_field(aeqe->asyn,
3900                                       HNS_ROCE_AEQE_U32_4_EVENT_TYPE_M,
3901                                       HNS_ROCE_AEQE_U32_4_EVENT_TYPE_S));
3902                event_type = roce_get_field(aeqe->asyn,
3903                                            HNS_ROCE_AEQE_U32_4_EVENT_TYPE_M,
3904                                            HNS_ROCE_AEQE_U32_4_EVENT_TYPE_S);
3905                switch (event_type) {
3906                case HNS_ROCE_EVENT_TYPE_PATH_MIG:
3907                        dev_warn(dev, "PATH MIG not supported\n");
3908                        break;
3909                case HNS_ROCE_EVENT_TYPE_COMM_EST:
3910                        dev_warn(dev, "COMMUNICATION established\n");
3911                        break;
3912                case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
3913                        dev_warn(dev, "SQ DRAINED not supported\n");
3914                        break;
3915                case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
3916                        dev_warn(dev, "PATH MIG failed\n");
3917                        break;
3918                case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
3919                case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
3920                case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
3921                        hns_roce_v1_qp_err_handle(hr_dev, aeqe, event_type);
3922                        break;
3923                case HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH:
3924                case HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR:
3925                case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
3926                        dev_warn(dev, "SRQ not support!\n");
3927                        break;
3928                case HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR:
3929                case HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW:
3930                case HNS_ROCE_EVENT_TYPE_CQ_ID_INVALID:
3931                        hns_roce_v1_cq_err_handle(hr_dev, aeqe, event_type);
3932                        break;
3933                case HNS_ROCE_EVENT_TYPE_PORT_CHANGE:
3934                        dev_warn(dev, "port change.\n");
3935                        break;
3936                case HNS_ROCE_EVENT_TYPE_MB:
3937                        hns_roce_cmd_event(hr_dev,
3938                                           le16_to_cpu(aeqe->event.cmd.token),
3939                                           aeqe->event.cmd.status,
3940                                           le64_to_cpu(aeqe->event.cmd.out_param
3941                                           ));
3942                        break;
3943                case HNS_ROCE_EVENT_TYPE_DB_OVERFLOW:
3944                        hns_roce_v1_db_overflow_handle(hr_dev, aeqe);
3945                        break;
3946                case HNS_ROCE_EVENT_TYPE_CEQ_OVERFLOW:
3947                        dev_warn(dev, "CEQ 0x%lx overflow.\n",
3948                        roce_get_field(aeqe->event.ce_event.ceqe,
3949                                     HNS_ROCE_AEQE_EVENT_CE_EVENT_CEQE_CEQN_M,
3950                                     HNS_ROCE_AEQE_EVENT_CE_EVENT_CEQE_CEQN_S));
3951                        break;
3952                default:
3953                        dev_warn(dev, "Unhandled event %d on EQ %d at idx %u.\n",
3954                                 event_type, eq->eqn, eq->cons_index);
3955                        break;
3956                }
3957
3958                eq->cons_index++;
3959                aeqes_found = 1;
3960
3961                if (eq->cons_index > 2 * hr_dev->caps.aeqe_depth - 1) {
3962                        dev_warn(dev, "cons_index overflow, set back to 0.\n");
3963                        eq->cons_index = 0;
3964                }
3965        }
3966
3967        set_eq_cons_index_v1(eq, 0);
3968
3969        return aeqes_found;
3970}
3971
3972static struct hns_roce_ceqe *get_ceqe_v1(struct hns_roce_eq *eq, u32 entry)
3973{
3974        unsigned long off = (entry & (eq->entries - 1)) *
3975                             HNS_ROCE_CEQ_ENTRY_SIZE;
3976
3977        return (struct hns_roce_ceqe *)((u8 *)
3978                        (eq->buf_list[off / HNS_ROCE_BA_SIZE].buf) +
3979                        off % HNS_ROCE_BA_SIZE);
3980}
3981
3982static struct hns_roce_ceqe *next_ceqe_sw_v1(struct hns_roce_eq *eq)
3983{
3984        struct hns_roce_ceqe *ceqe = get_ceqe_v1(eq, eq->cons_index);
3985
3986        return (!!(roce_get_bit(ceqe->comp,
3987                HNS_ROCE_CEQE_CEQE_COMP_OWNER_S))) ^
3988                (!!(eq->cons_index & eq->entries)) ? ceqe : NULL;
3989}
3990
3991static int hns_roce_v1_ceq_int(struct hns_roce_dev *hr_dev,
3992                               struct hns_roce_eq *eq)
3993{
3994        struct hns_roce_ceqe *ceqe;
3995        int ceqes_found = 0;
3996        u32 cqn;
3997
3998        while ((ceqe = next_ceqe_sw_v1(eq))) {
3999
4000                /* Make sure we read CEQ entry after we have checked the
4001                 * ownership bit
4002                 */
4003                dma_rmb();
4004
4005                cqn = roce_get_field(ceqe->comp,
4006                                     HNS_ROCE_CEQE_CEQE_COMP_CQN_M,
4007                                     HNS_ROCE_CEQE_CEQE_COMP_CQN_S);
4008                hns_roce_cq_completion(hr_dev, cqn);
4009
4010                ++eq->cons_index;
4011                ceqes_found = 1;
4012
4013                if (eq->cons_index > 2 * hr_dev->caps.ceqe_depth - 1) {
4014                        dev_warn(&eq->hr_dev->pdev->dev,
4015                                "cons_index overflow, set back to 0.\n");
4016                        eq->cons_index = 0;
4017                }
4018        }
4019
4020        set_eq_cons_index_v1(eq, 0);
4021
4022        return ceqes_found;
4023}
4024
4025static irqreturn_t hns_roce_v1_msix_interrupt_eq(int irq, void *eq_ptr)
4026{
4027        struct hns_roce_eq  *eq  = eq_ptr;
4028        struct hns_roce_dev *hr_dev = eq->hr_dev;
4029        int int_work = 0;
4030
4031        if (eq->type_flag == HNS_ROCE_CEQ)
4032                /* CEQ irq routine, CEQ is pulse irq, not clear */
4033                int_work = hns_roce_v1_ceq_int(hr_dev, eq);
4034        else
4035                /* AEQ irq routine, AEQ is pulse irq, not clear */
4036                int_work = hns_roce_v1_aeq_int(hr_dev, eq);
4037
4038        return IRQ_RETVAL(int_work);
4039}
4040
4041static irqreturn_t hns_roce_v1_msix_interrupt_abn(int irq, void *dev_id)
4042{
4043        struct hns_roce_dev *hr_dev = dev_id;
4044        struct device *dev = &hr_dev->pdev->dev;
4045        int int_work = 0;
4046        u32 caepaemask_val;
4047        u32 cealmovf_val;
4048        u32 caepaest_val;
4049        u32 aeshift_val;
4050        u32 ceshift_val;
4051        u32 cemask_val;
4052        __le32 tmp;
4053        int i;
4054
4055        /*
4056         * Abnormal interrupt:
4057         * AEQ overflow, ECC multi-bit err, CEQ overflow must clear
4058         * interrupt, mask irq, clear irq, cancel mask operation
4059         */
4060        aeshift_val = roce_read(hr_dev, ROCEE_CAEP_AEQC_AEQE_SHIFT_REG);
4061        tmp = cpu_to_le32(aeshift_val);
4062
4063        /* AEQE overflow */
4064        if (roce_get_bit(tmp,
4065                ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQ_ALM_OVF_INT_ST_S) == 1) {
4066                dev_warn(dev, "AEQ overflow!\n");
4067
4068                /* Set mask */
4069                caepaemask_val = roce_read(hr_dev, ROCEE_CAEP_AE_MASK_REG);
4070                tmp = cpu_to_le32(caepaemask_val);
4071                roce_set_bit(tmp, ROCEE_CAEP_AE_MASK_CAEP_AEQ_ALM_OVF_MASK_S,
4072                             HNS_ROCE_INT_MASK_ENABLE);
4073                caepaemask_val = le32_to_cpu(tmp);
4074                roce_write(hr_dev, ROCEE_CAEP_AE_MASK_REG, caepaemask_val);
4075
4076                /* Clear int state(INT_WC : write 1 clear) */
4077                caepaest_val = roce_read(hr_dev, ROCEE_CAEP_AE_ST_REG);
4078                tmp = cpu_to_le32(caepaest_val);
4079                roce_set_bit(tmp, ROCEE_CAEP_AE_ST_CAEP_AEQ_ALM_OVF_S, 1);
4080                caepaest_val = le32_to_cpu(tmp);
4081                roce_write(hr_dev, ROCEE_CAEP_AE_ST_REG, caepaest_val);
4082
4083                /* Clear mask */
4084                caepaemask_val = roce_read(hr_dev, ROCEE_CAEP_AE_MASK_REG);
4085                tmp = cpu_to_le32(caepaemask_val);
4086                roce_set_bit(tmp, ROCEE_CAEP_AE_MASK_CAEP_AEQ_ALM_OVF_MASK_S,
4087                             HNS_ROCE_INT_MASK_DISABLE);
4088                caepaemask_val = le32_to_cpu(tmp);
4089                roce_write(hr_dev, ROCEE_CAEP_AE_MASK_REG, caepaemask_val);
4090        }
4091
4092        /* CEQ almost overflow */
4093        for (i = 0; i < hr_dev->caps.num_comp_vectors; i++) {
4094                ceshift_val = roce_read(hr_dev, ROCEE_CAEP_CEQC_SHIFT_0_REG +
4095                                        i * CEQ_REG_OFFSET);
4096                tmp = cpu_to_le32(ceshift_val);
4097
4098                if (roce_get_bit(tmp,
4099                        ROCEE_CAEP_CEQC_SHIFT_CAEP_CEQ_ALM_OVF_INT_ST_S) == 1) {
4100                        dev_warn(dev, "CEQ[%d] almost overflow!\n", i);
4101                        int_work++;
4102
4103                        /* Set mask */
4104                        cemask_val = roce_read(hr_dev,
4105                                               ROCEE_CAEP_CE_IRQ_MASK_0_REG +
4106                                               i * CEQ_REG_OFFSET);
4107                        tmp = cpu_to_le32(cemask_val);
4108                        roce_set_bit(tmp,
4109                                ROCEE_CAEP_CE_IRQ_MASK_CAEP_CEQ_ALM_OVF_MASK_S,
4110                                HNS_ROCE_INT_MASK_ENABLE);
4111                        cemask_val = le32_to_cpu(tmp);
4112                        roce_write(hr_dev, ROCEE_CAEP_CE_IRQ_MASK_0_REG +
4113                                   i * CEQ_REG_OFFSET, cemask_val);
4114
4115                        /* Clear int state(INT_WC : write 1 clear) */
4116                        cealmovf_val = roce_read(hr_dev,
4117                                       ROCEE_CAEP_CEQ_ALM_OVF_0_REG +
4118                                       i * CEQ_REG_OFFSET);
4119                        tmp = cpu_to_le32(cealmovf_val);
4120                        roce_set_bit(tmp,
4121                                     ROCEE_CAEP_CEQ_ALM_OVF_CAEP_CEQ_ALM_OVF_S,
4122                                     1);
4123                        cealmovf_val = le32_to_cpu(tmp);
4124                        roce_write(hr_dev, ROCEE_CAEP_CEQ_ALM_OVF_0_REG +
4125                                   i * CEQ_REG_OFFSET, cealmovf_val);
4126
4127                        /* Clear mask */
4128                        cemask_val = roce_read(hr_dev,
4129                                     ROCEE_CAEP_CE_IRQ_MASK_0_REG +
4130                                     i * CEQ_REG_OFFSET);
4131                        tmp = cpu_to_le32(cemask_val);
4132                        roce_set_bit(tmp,
4133                               ROCEE_CAEP_CE_IRQ_MASK_CAEP_CEQ_ALM_OVF_MASK_S,
4134                               HNS_ROCE_INT_MASK_DISABLE);
4135                        cemask_val = le32_to_cpu(tmp);
4136                        roce_write(hr_dev, ROCEE_CAEP_CE_IRQ_MASK_0_REG +
4137                                   i * CEQ_REG_OFFSET, cemask_val);
4138                }
4139        }
4140
4141        /* ECC multi-bit error alarm */
4142        dev_warn(dev, "ECC UCERR ALARM: 0x%x, 0x%x, 0x%x\n",
4143                 roce_read(hr_dev, ROCEE_ECC_UCERR_ALM0_REG),
4144                 roce_read(hr_dev, ROCEE_ECC_UCERR_ALM1_REG),
4145                 roce_read(hr_dev, ROCEE_ECC_UCERR_ALM2_REG));
4146
4147        dev_warn(dev, "ECC CERR ALARM: 0x%x, 0x%x, 0x%x\n",
4148                 roce_read(hr_dev, ROCEE_ECC_CERR_ALM0_REG),
4149                 roce_read(hr_dev, ROCEE_ECC_CERR_ALM1_REG),
4150                 roce_read(hr_dev, ROCEE_ECC_CERR_ALM2_REG));
4151
4152        return IRQ_RETVAL(int_work);
4153}
4154
4155static void hns_roce_v1_int_mask_enable(struct hns_roce_dev *hr_dev)
4156{
4157        u32 aemask_val;
4158        int masken = 0;
4159        __le32 tmp;
4160        int i;
4161
4162        /* AEQ INT */
4163        aemask_val = roce_read(hr_dev, ROCEE_CAEP_AE_MASK_REG);
4164        tmp = cpu_to_le32(aemask_val);
4165        roce_set_bit(tmp, ROCEE_CAEP_AE_MASK_CAEP_AEQ_ALM_OVF_MASK_S,
4166                     masken);
4167        roce_set_bit(tmp, ROCEE_CAEP_AE_MASK_CAEP_AE_IRQ_MASK_S, masken);
4168        aemask_val = le32_to_cpu(tmp);
4169        roce_write(hr_dev, ROCEE_CAEP_AE_MASK_REG, aemask_val);
4170
4171        /* CEQ INT */
4172        for (i = 0; i < hr_dev->caps.num_comp_vectors; i++) {
4173                /* IRQ mask */
4174                roce_write(hr_dev, ROCEE_CAEP_CE_IRQ_MASK_0_REG +
4175                           i * CEQ_REG_OFFSET, masken);
4176        }
4177}
4178
4179static void hns_roce_v1_free_eq(struct hns_roce_dev *hr_dev,
4180                                struct hns_roce_eq *eq)
4181{
4182        int npages = (PAGE_ALIGN(eq->eqe_size * eq->entries) +
4183                      HNS_ROCE_BA_SIZE - 1) / HNS_ROCE_BA_SIZE;
4184        int i;
4185
4186        if (!eq->buf_list)
4187                return;
4188
4189        for (i = 0; i < npages; ++i)
4190                dma_free_coherent(&hr_dev->pdev->dev, HNS_ROCE_BA_SIZE,
4191                                  eq->buf_list[i].buf, eq->buf_list[i].map);
4192
4193        kfree(eq->buf_list);
4194}
4195
4196static void hns_roce_v1_enable_eq(struct hns_roce_dev *hr_dev, int eq_num,
4197                                  int enable_flag)
4198{
4199        void __iomem *eqc = hr_dev->eq_table.eqc_base[eq_num];
4200        __le32 tmp;
4201        u32 val;
4202
4203        val = readl(eqc);
4204        tmp = cpu_to_le32(val);
4205
4206        if (enable_flag)
4207                roce_set_field(tmp,
4208                               ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_STATE_M,
4209                               ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_STATE_S,
4210                               HNS_ROCE_EQ_STAT_VALID);
4211        else
4212                roce_set_field(tmp,
4213                               ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_STATE_M,
4214                               ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_STATE_S,
4215                               HNS_ROCE_EQ_STAT_INVALID);
4216
4217        val = le32_to_cpu(tmp);
4218        writel(val, eqc);
4219}
4220
4221static int hns_roce_v1_create_eq(struct hns_roce_dev *hr_dev,
4222                                 struct hns_roce_eq *eq)
4223{
4224        void __iomem *eqc = hr_dev->eq_table.eqc_base[eq->eqn];
4225        struct device *dev = &hr_dev->pdev->dev;
4226        dma_addr_t tmp_dma_addr;
4227        u32 eqconsindx_val = 0;
4228        u32 eqcuridx_val = 0;
4229        u32 eqshift_val = 0;
4230        __le32 tmp2 = 0;
4231        __le32 tmp1 = 0;
4232        __le32 tmp = 0;
4233        int num_bas;
4234        int ret;
4235        int i;
4236
4237        num_bas = (PAGE_ALIGN(eq->entries * eq->eqe_size) +
4238                   HNS_ROCE_BA_SIZE - 1) / HNS_ROCE_BA_SIZE;
4239
4240        if ((eq->entries * eq->eqe_size) > HNS_ROCE_BA_SIZE) {
4241                dev_err(dev, "[error]eq buf %d gt ba size(%d) need bas=%d\n",
4242                        (eq->entries * eq->eqe_size), HNS_ROCE_BA_SIZE,
4243                        num_bas);
4244                return -EINVAL;
4245        }
4246
4247        eq->buf_list = kcalloc(num_bas, sizeof(*eq->buf_list), GFP_KERNEL);
4248        if (!eq->buf_list)
4249                return -ENOMEM;
4250
4251        for (i = 0; i < num_bas; ++i) {
4252                eq->buf_list[i].buf = dma_alloc_coherent(dev, HNS_ROCE_BA_SIZE,
4253                                                         &tmp_dma_addr,
4254                                                         GFP_KERNEL);
4255                if (!eq->buf_list[i].buf) {
4256                        ret = -ENOMEM;
4257                        goto err_out_free_pages;
4258                }
4259
4260                eq->buf_list[i].map = tmp_dma_addr;
4261        }
4262        eq->cons_index = 0;
4263        roce_set_field(tmp, ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_STATE_M,
4264                       ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_STATE_S,
4265                       HNS_ROCE_EQ_STAT_INVALID);
4266        roce_set_field(tmp, ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_AEQE_SHIFT_M,
4267                       ROCEE_CAEP_AEQC_AEQE_SHIFT_CAEP_AEQC_AEQE_SHIFT_S,
4268                       eq->log_entries);
4269        eqshift_val = le32_to_cpu(tmp);
4270        writel(eqshift_val, eqc);
4271
4272        /* Configure eq extended address 12~44bit */
4273        writel((u32)(eq->buf_list[0].map >> 12), eqc + 4);
4274
4275        /*
4276         * Configure eq extended address 45~49 bit.
4277         * 44 = 32 + 12, When evaluating addr to hardware, shift 12 because of
4278         * using 4K page, and shift more 32 because of
4279         * caculating the high 32 bit value evaluated to hardware.
4280         */
4281        roce_set_field(tmp1, ROCEE_CAEP_AEQE_CUR_IDX_CAEP_AEQ_BT_H_M,
4282                       ROCEE_CAEP_AEQE_CUR_IDX_CAEP_AEQ_BT_H_S,
4283                       eq->buf_list[0].map >> 44);
4284        roce_set_field(tmp1, ROCEE_CAEP_AEQE_CUR_IDX_CAEP_AEQE_CUR_IDX_M,
4285                       ROCEE_CAEP_AEQE_CUR_IDX_CAEP_AEQE_CUR_IDX_S, 0);
4286        eqcuridx_val = le32_to_cpu(tmp1);
4287        writel(eqcuridx_val, eqc + 8);
4288
4289        /* Configure eq consumer index */
4290        roce_set_field(tmp2, ROCEE_CAEP_AEQE_CONS_IDX_CAEP_AEQE_CONS_IDX_M,
4291                       ROCEE_CAEP_AEQE_CONS_IDX_CAEP_AEQE_CONS_IDX_S, 0);
4292        eqconsindx_val = le32_to_cpu(tmp2);
4293        writel(eqconsindx_val, eqc + 0xc);
4294
4295        return 0;
4296
4297err_out_free_pages:
4298        for (i -= 1; i >= 0; i--)
4299                dma_free_coherent(dev, HNS_ROCE_BA_SIZE, eq->buf_list[i].buf,
4300                                  eq->buf_list[i].map);
4301
4302        kfree(eq->buf_list);
4303        return ret;
4304}
4305
4306static int hns_roce_v1_init_eq_table(struct hns_roce_dev *hr_dev)
4307{
4308        struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
4309        struct device *dev = &hr_dev->pdev->dev;
4310        struct hns_roce_eq *eq;
4311        int irq_num;
4312        int eq_num;
4313        int ret;
4314        int i, j;
4315
4316        eq_num = hr_dev->caps.num_comp_vectors + hr_dev->caps.num_aeq_vectors;
4317        irq_num = eq_num + hr_dev->caps.num_other_vectors;
4318
4319        eq_table->eq = kcalloc(eq_num, sizeof(*eq_table->eq), GFP_KERNEL);
4320        if (!eq_table->eq)
4321                return -ENOMEM;
4322
4323        eq_table->eqc_base = kcalloc(eq_num, sizeof(*eq_table->eqc_base),
4324                                     GFP_KERNEL);
4325        if (!eq_table->eqc_base) {
4326                ret = -ENOMEM;
4327                goto err_eqc_base_alloc_fail;
4328        }
4329
4330        for (i = 0; i < eq_num; i++) {
4331                eq = &eq_table->eq[i];
4332                eq->hr_dev = hr_dev;
4333                eq->eqn = i;
4334                eq->irq = hr_dev->irq[i];
4335                eq->log_page_size = PAGE_SHIFT;
4336
4337                if (i < hr_dev->caps.num_comp_vectors) {
4338                        /* CEQ */
4339                        eq_table->eqc_base[i] = hr_dev->reg_base +
4340                                                ROCEE_CAEP_CEQC_SHIFT_0_REG +
4341                                                CEQ_REG_OFFSET * i;
4342                        eq->type_flag = HNS_ROCE_CEQ;
4343                        eq->doorbell = hr_dev->reg_base +
4344                                       ROCEE_CAEP_CEQC_CONS_IDX_0_REG +
4345                                       CEQ_REG_OFFSET * i;
4346                        eq->entries = hr_dev->caps.ceqe_depth;
4347                        eq->log_entries = ilog2(eq->entries);
4348                        eq->eqe_size = HNS_ROCE_CEQ_ENTRY_SIZE;
4349                } else {
4350                        /* AEQ */
4351                        eq_table->eqc_base[i] = hr_dev->reg_base +
4352                                                ROCEE_CAEP_AEQC_AEQE_SHIFT_REG;
4353                        eq->type_flag = HNS_ROCE_AEQ;
4354                        eq->doorbell = hr_dev->reg_base +
4355                                       ROCEE_CAEP_AEQE_CONS_IDX_REG;
4356                        eq->entries = hr_dev->caps.aeqe_depth;
4357                        eq->log_entries = ilog2(eq->entries);
4358                        eq->eqe_size = HNS_ROCE_AEQ_ENTRY_SIZE;
4359                }
4360        }
4361
4362        /* Disable irq */
4363        hns_roce_v1_int_mask_enable(hr_dev);
4364
4365        /* Configure ce int interval */
4366        roce_write(hr_dev, ROCEE_CAEP_CE_INTERVAL_CFG_REG,
4367                   HNS_ROCE_CEQ_DEFAULT_INTERVAL);
4368
4369        /* Configure ce int burst num */
4370        roce_write(hr_dev, ROCEE_CAEP_CE_BURST_NUM_CFG_REG,
4371                   HNS_ROCE_CEQ_DEFAULT_BURST_NUM);
4372
4373        for (i = 0; i < eq_num; i++) {
4374                ret = hns_roce_v1_create_eq(hr_dev, &eq_table->eq[i]);
4375                if (ret) {
4376                        dev_err(dev, "eq create failed\n");
4377                        goto err_create_eq_fail;
4378                }
4379        }
4380
4381        for (j = 0; j < irq_num; j++) {
4382                if (j < eq_num)
4383                        ret = request_irq(hr_dev->irq[j],
4384                                          hns_roce_v1_msix_interrupt_eq, 0,
4385                                          hr_dev->irq_names[j],
4386                                          &eq_table->eq[j]);
4387                else
4388                        ret = request_irq(hr_dev->irq[j],
4389                                          hns_roce_v1_msix_interrupt_abn, 0,
4390                                          hr_dev->irq_names[j], hr_dev);
4391
4392                if (ret) {
4393                        dev_err(dev, "request irq error!\n");
4394                        goto err_request_irq_fail;
4395                }
4396        }
4397
4398        for (i = 0; i < eq_num; i++)
4399                hns_roce_v1_enable_eq(hr_dev, i, EQ_ENABLE);
4400
4401        return 0;
4402
4403err_request_irq_fail:
4404        for (j -= 1; j >= 0; j--)
4405                free_irq(hr_dev->irq[j], &eq_table->eq[j]);
4406
4407err_create_eq_fail:
4408        for (i -= 1; i >= 0; i--)
4409                hns_roce_v1_free_eq(hr_dev, &eq_table->eq[i]);
4410
4411        kfree(eq_table->eqc_base);
4412
4413err_eqc_base_alloc_fail:
4414        kfree(eq_table->eq);
4415
4416        return ret;
4417}
4418
4419static void hns_roce_v1_cleanup_eq_table(struct hns_roce_dev *hr_dev)
4420{
4421        struct hns_roce_eq_table *eq_table = &hr_dev->eq_table;
4422        int irq_num;
4423        int eq_num;
4424        int i;
4425
4426        eq_num = hr_dev->caps.num_comp_vectors + hr_dev->caps.num_aeq_vectors;
4427        irq_num = eq_num + hr_dev->caps.num_other_vectors;
4428        for (i = 0; i < eq_num; i++) {
4429                /* Disable EQ */
4430                hns_roce_v1_enable_eq(hr_dev, i, EQ_DISABLE);
4431
4432                free_irq(hr_dev->irq[i], &eq_table->eq[i]);
4433
4434                hns_roce_v1_free_eq(hr_dev, &eq_table->eq[i]);
4435        }
4436        for (i = eq_num; i < irq_num; i++)
4437                free_irq(hr_dev->irq[i], hr_dev);
4438
4439        kfree(eq_table->eqc_base);
4440        kfree(eq_table->eq);
4441}
4442
4443static const struct ib_device_ops hns_roce_v1_dev_ops = {
4444        .destroy_qp = hns_roce_v1_destroy_qp,
4445        .modify_cq = hns_roce_v1_modify_cq,
4446        .poll_cq = hns_roce_v1_poll_cq,
4447        .post_recv = hns_roce_v1_post_recv,
4448        .post_send = hns_roce_v1_post_send,
4449        .query_qp = hns_roce_v1_query_qp,
4450        .req_notify_cq = hns_roce_v1_req_notify_cq,
4451};
4452
4453static const struct hns_roce_hw hns_roce_hw_v1 = {
4454        .reset = hns_roce_v1_reset,
4455        .hw_profile = hns_roce_v1_profile,
4456        .hw_init = hns_roce_v1_init,
4457        .hw_exit = hns_roce_v1_exit,
4458        .post_mbox = hns_roce_v1_post_mbox,
4459        .chk_mbox = hns_roce_v1_chk_mbox,
4460        .set_gid = hns_roce_v1_set_gid,
4461        .set_mac = hns_roce_v1_set_mac,
4462        .set_mtu = hns_roce_v1_set_mtu,
4463        .write_mtpt = hns_roce_v1_write_mtpt,
4464        .write_cqc = hns_roce_v1_write_cqc,
4465        .modify_cq = hns_roce_v1_modify_cq,
4466        .clear_hem = hns_roce_v1_clear_hem,
4467        .modify_qp = hns_roce_v1_modify_qp,
4468        .query_qp = hns_roce_v1_query_qp,
4469        .destroy_qp = hns_roce_v1_destroy_qp,
4470        .post_send = hns_roce_v1_post_send,
4471        .post_recv = hns_roce_v1_post_recv,
4472        .req_notify_cq = hns_roce_v1_req_notify_cq,
4473        .poll_cq = hns_roce_v1_poll_cq,
4474        .dereg_mr = hns_roce_v1_dereg_mr,
4475        .destroy_cq = hns_roce_v1_destroy_cq,
4476        .init_eq = hns_roce_v1_init_eq_table,
4477        .cleanup_eq = hns_roce_v1_cleanup_eq_table,
4478        .hns_roce_dev_ops = &hns_roce_v1_dev_ops,
4479};
4480
4481static const struct of_device_id hns_roce_of_match[] = {
4482        { .compatible = "hisilicon,hns-roce-v1", .data = &hns_roce_hw_v1, },
4483        {},
4484};
4485MODULE_DEVICE_TABLE(of, hns_roce_of_match);
4486
4487static const struct acpi_device_id hns_roce_acpi_match[] = {
4488        { "HISI00D1", (kernel_ulong_t)&hns_roce_hw_v1 },
4489        {},
4490};
4491MODULE_DEVICE_TABLE(acpi, hns_roce_acpi_match);
4492
4493static int hns_roce_node_match(struct device *dev, const void *fwnode)
4494{
4495        return dev->fwnode == fwnode;
4496}
4497
4498static struct
4499platform_device *hns_roce_find_pdev(struct fwnode_handle *fwnode)
4500{
4501        struct device *dev;
4502
4503        /* get the 'device' corresponding to the matching 'fwnode' */
4504        dev = bus_find_device(&platform_bus_type, NULL,
4505                              fwnode, hns_roce_node_match);
4506        /* get the platform device */
4507        return dev ? to_platform_device(dev) : NULL;
4508}
4509
4510static int hns_roce_get_cfg(struct hns_roce_dev *hr_dev)
4511{
4512        struct device *dev = &hr_dev->pdev->dev;
4513        struct platform_device *pdev = NULL;
4514        struct net_device *netdev = NULL;
4515        struct device_node *net_node;
4516        struct resource *res;
4517        int port_cnt = 0;
4518        u8 phy_port;
4519        int ret;
4520        int i;
4521
4522        /* check if we are compatible with the underlying SoC */
4523        if (dev_of_node(dev)) {
4524                const struct of_device_id *of_id;
4525
4526                of_id = of_match_node(hns_roce_of_match, dev->of_node);
4527                if (!of_id) {
4528                        dev_err(dev, "device is not compatible!\n");
4529                        return -ENXIO;
4530                }
4531                hr_dev->hw = (const struct hns_roce_hw *)of_id->data;
4532                if (!hr_dev->hw) {
4533                        dev_err(dev, "couldn't get H/W specific DT data!\n");
4534                        return -ENXIO;
4535                }
4536        } else if (is_acpi_device_node(dev->fwnode)) {
4537                const struct acpi_device_id *acpi_id;
4538
4539                acpi_id = acpi_match_device(hns_roce_acpi_match, dev);
4540                if (!acpi_id) {
4541                        dev_err(dev, "device is not compatible!\n");
4542                        return -ENXIO;
4543                }
4544                hr_dev->hw = (const struct hns_roce_hw *) acpi_id->driver_data;
4545                if (!hr_dev->hw) {
4546                        dev_err(dev, "couldn't get H/W specific ACPI data!\n");
4547                        return -ENXIO;
4548                }
4549        } else {
4550                dev_err(dev, "can't read compatibility data from DT or ACPI\n");
4551                return -ENXIO;
4552        }
4553
4554        /* get the mapped register base address */
4555        res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
4556        hr_dev->reg_base = devm_ioremap_resource(dev, res);
4557        if (IS_ERR(hr_dev->reg_base))
4558                return PTR_ERR(hr_dev->reg_base);
4559
4560        /* read the node_guid of IB device from the DT or ACPI */
4561        ret = device_property_read_u8_array(dev, "node-guid",
4562                                            (u8 *)&hr_dev->ib_dev.node_guid,
4563                                            GUID_LEN);
4564        if (ret) {
4565                dev_err(dev, "couldn't get node_guid from DT or ACPI!\n");
4566                return ret;
4567        }
4568
4569        /* get the RoCE associated ethernet ports or netdevices */
4570        for (i = 0; i < HNS_ROCE_MAX_PORTS; i++) {
4571                if (dev_of_node(dev)) {
4572                        net_node = of_parse_phandle(dev->of_node, "eth-handle",
4573                                                    i);
4574                        if (!net_node)
4575                                continue;
4576                        pdev = of_find_device_by_node(net_node);
4577                } else if (is_acpi_device_node(dev->fwnode)) {
4578                        struct fwnode_reference_args args;
4579
4580                        ret = acpi_node_get_property_reference(dev->fwnode,
4581                                                               "eth-handle",
4582                                                               i, &args);
4583                        if (ret)
4584                                continue;
4585                        pdev = hns_roce_find_pdev(args.fwnode);
4586                } else {
4587                        dev_err(dev, "cannot read data from DT or ACPI\n");
4588                        return -ENXIO;
4589                }
4590
4591                if (pdev) {
4592                        netdev = platform_get_drvdata(pdev);
4593                        phy_port = (u8)i;
4594                        if (netdev) {
4595                                hr_dev->iboe.netdevs[port_cnt] = netdev;
4596                                hr_dev->iboe.phy_port[port_cnt] = phy_port;
4597                        } else {
4598                                dev_err(dev, "no netdev found with pdev %s\n",
4599                                        pdev->name);
4600                                return -ENODEV;
4601                        }
4602                        port_cnt++;
4603                }
4604        }
4605
4606        if (port_cnt == 0) {
4607                dev_err(dev, "unable to get eth-handle for available ports!\n");
4608                return -EINVAL;
4609        }
4610
4611        hr_dev->caps.num_ports = port_cnt;
4612
4613        /* cmd issue mode: 0 is poll, 1 is event */
4614        hr_dev->cmd_mod = 1;
4615        hr_dev->loop_idc = 0;
4616        hr_dev->sdb_offset = ROCEE_DB_SQ_L_0_REG;
4617        hr_dev->odb_offset = ROCEE_DB_OTHERS_L_0_REG;
4618
4619        /* read the interrupt names from the DT or ACPI */
4620        ret = device_property_read_string_array(dev, "interrupt-names",
4621                                                hr_dev->irq_names,
4622                                                HNS_ROCE_V1_MAX_IRQ_NUM);
4623        if (ret < 0) {
4624                dev_err(dev, "couldn't get interrupt names from DT or ACPI!\n");
4625                return ret;
4626        }
4627
4628        /* fetch the interrupt numbers */
4629        for (i = 0; i < HNS_ROCE_V1_MAX_IRQ_NUM; i++) {
4630                hr_dev->irq[i] = platform_get_irq(hr_dev->pdev, i);
4631                if (hr_dev->irq[i] <= 0) {
4632                        dev_err(dev, "platform get of irq[=%d] failed!\n", i);
4633                        return -EINVAL;
4634                }
4635        }
4636
4637        return 0;
4638}
4639
4640/**
4641 * hns_roce_probe - RoCE driver entrance
4642 * @pdev: pointer to platform device
4643 * Return : int
4644 *
4645 */
4646static int hns_roce_probe(struct platform_device *pdev)
4647{
4648        int ret;
4649        struct hns_roce_dev *hr_dev;
4650        struct device *dev = &pdev->dev;
4651
4652        hr_dev = ib_alloc_device(hns_roce_dev, ib_dev);
4653        if (!hr_dev)
4654                return -ENOMEM;
4655
4656        hr_dev->priv = kzalloc(sizeof(struct hns_roce_v1_priv), GFP_KERNEL);
4657        if (!hr_dev->priv) {
4658                ret = -ENOMEM;
4659                goto error_failed_kzalloc;
4660        }
4661
4662        hr_dev->pdev = pdev;
4663        hr_dev->dev = dev;
4664        platform_set_drvdata(pdev, hr_dev);
4665
4666        if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64ULL)) &&
4667            dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32ULL))) {
4668                dev_err(dev, "Not usable DMA addressing mode\n");
4669                ret = -EIO;
4670                goto error_failed_get_cfg;
4671        }
4672
4673        ret = hns_roce_get_cfg(hr_dev);
4674        if (ret) {
4675                dev_err(dev, "Get Configuration failed!\n");
4676                goto error_failed_get_cfg;
4677        }
4678
4679        ret = hns_roce_init(hr_dev);
4680        if (ret) {
4681                dev_err(dev, "RoCE engine init failed!\n");
4682                goto error_failed_get_cfg;
4683        }
4684
4685        return 0;
4686
4687error_failed_get_cfg:
4688        kfree(hr_dev->priv);
4689
4690error_failed_kzalloc:
4691        ib_dealloc_device(&hr_dev->ib_dev);
4692
4693        return ret;
4694}
4695
4696/**
4697 * hns_roce_remove - remove RoCE device
4698 * @pdev: pointer to platform device
4699 */
4700static int hns_roce_remove(struct platform_device *pdev)
4701{
4702        struct hns_roce_dev *hr_dev = platform_get_drvdata(pdev);
4703
4704        hns_roce_exit(hr_dev);
4705        kfree(hr_dev->priv);
4706        ib_dealloc_device(&hr_dev->ib_dev);
4707
4708        return 0;
4709}
4710
4711static struct platform_driver hns_roce_driver = {
4712        .probe = hns_roce_probe,
4713        .remove = hns_roce_remove,
4714        .driver = {
4715                .name = DRV_NAME,
4716                .of_match_table = hns_roce_of_match,
4717                .acpi_match_table = ACPI_PTR(hns_roce_acpi_match),
4718        },
4719};
4720
4721module_platform_driver(hns_roce_driver);
4722
4723MODULE_LICENSE("Dual BSD/GPL");
4724MODULE_AUTHOR("Wei Hu <xavier.huwei@huawei.com>");
4725MODULE_AUTHOR("Nenglong Zhao <zhaonenglong@hisilicon.com>");
4726MODULE_AUTHOR("Lijun Ou <oulijun@huawei.com>");
4727MODULE_DESCRIPTION("Hisilicon Hip06 Family RoCE Driver");
4728