linux/drivers/infiniband/hw/cxgb4/qp.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
   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/module.h>
  34
  35#include "iw_cxgb4.h"
  36
  37static int db_delay_usecs = 1;
  38module_param(db_delay_usecs, int, 0644);
  39MODULE_PARM_DESC(db_delay_usecs, "Usecs to delay awaiting db fifo to drain");
  40
  41static int ocqp_support = 1;
  42module_param(ocqp_support, int, 0644);
  43MODULE_PARM_DESC(ocqp_support, "Support on-chip SQs (default=1)");
  44
  45int db_fc_threshold = 1000;
  46module_param(db_fc_threshold, int, 0644);
  47MODULE_PARM_DESC(db_fc_threshold,
  48                 "QP count/threshold that triggers"
  49                 " automatic db flow control mode (default = 1000)");
  50
  51int db_coalescing_threshold;
  52module_param(db_coalescing_threshold, int, 0644);
  53MODULE_PARM_DESC(db_coalescing_threshold,
  54                 "QP count/threshold that triggers"
  55                 " disabling db coalescing (default = 0)");
  56
  57static int max_fr_immd = T4_MAX_FR_IMMD;
  58module_param(max_fr_immd, int, 0644);
  59MODULE_PARM_DESC(max_fr_immd, "fastreg threshold for using DSGL instead of immedate");
  60
  61static int alloc_ird(struct c4iw_dev *dev, u32 ird)
  62{
  63        int ret = 0;
  64
  65        spin_lock_irq(&dev->lock);
  66        if (ird <= dev->avail_ird)
  67                dev->avail_ird -= ird;
  68        else
  69                ret = -ENOMEM;
  70        spin_unlock_irq(&dev->lock);
  71
  72        if (ret)
  73                dev_warn(&dev->rdev.lldi.pdev->dev,
  74                         "device IRD resources exhausted\n");
  75
  76        return ret;
  77}
  78
  79static void free_ird(struct c4iw_dev *dev, int ird)
  80{
  81        spin_lock_irq(&dev->lock);
  82        dev->avail_ird += ird;
  83        spin_unlock_irq(&dev->lock);
  84}
  85
  86static void set_state(struct c4iw_qp *qhp, enum c4iw_qp_state state)
  87{
  88        unsigned long flag;
  89        spin_lock_irqsave(&qhp->lock, flag);
  90        qhp->attr.state = state;
  91        spin_unlock_irqrestore(&qhp->lock, flag);
  92}
  93
  94static void dealloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
  95{
  96        c4iw_ocqp_pool_free(rdev, sq->dma_addr, sq->memsize);
  97}
  98
  99static void dealloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
 100{
 101        dma_free_coherent(&(rdev->lldi.pdev->dev), sq->memsize, sq->queue,
 102                          dma_unmap_addr(sq, mapping));
 103}
 104
 105static void dealloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
 106{
 107        if (t4_sq_onchip(sq))
 108                dealloc_oc_sq(rdev, sq);
 109        else
 110                dealloc_host_sq(rdev, sq);
 111}
 112
 113static int alloc_oc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
 114{
 115        if (!ocqp_support || !ocqp_supported(&rdev->lldi))
 116                return -ENOSYS;
 117        sq->dma_addr = c4iw_ocqp_pool_alloc(rdev, sq->memsize);
 118        if (!sq->dma_addr)
 119                return -ENOMEM;
 120        sq->phys_addr = rdev->oc_mw_pa + sq->dma_addr -
 121                        rdev->lldi.vr->ocq.start;
 122        sq->queue = (__force union t4_wr *)(rdev->oc_mw_kva + sq->dma_addr -
 123                                            rdev->lldi.vr->ocq.start);
 124        sq->flags |= T4_SQ_ONCHIP;
 125        return 0;
 126}
 127
 128static int alloc_host_sq(struct c4iw_rdev *rdev, struct t4_sq *sq)
 129{
 130        sq->queue = dma_alloc_coherent(&(rdev->lldi.pdev->dev), sq->memsize,
 131                                       &(sq->dma_addr), GFP_KERNEL);
 132        if (!sq->queue)
 133                return -ENOMEM;
 134        sq->phys_addr = virt_to_phys(sq->queue);
 135        dma_unmap_addr_set(sq, mapping, sq->dma_addr);
 136        return 0;
 137}
 138
 139static int alloc_sq(struct c4iw_rdev *rdev, struct t4_sq *sq, int user)
 140{
 141        int ret = -ENOSYS;
 142        if (user)
 143                ret = alloc_oc_sq(rdev, sq);
 144        if (ret)
 145                ret = alloc_host_sq(rdev, sq);
 146        return ret;
 147}
 148
 149static int destroy_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
 150                      struct c4iw_dev_ucontext *uctx, int has_rq)
 151{
 152        /*
 153         * uP clears EQ contexts when the connection exits rdma mode,
 154         * so no need to post a RESET WR for these EQs.
 155         */
 156        dealloc_sq(rdev, &wq->sq);
 157        kfree(wq->sq.sw_sq);
 158        c4iw_put_qpid(rdev, wq->sq.qid, uctx);
 159
 160        if (has_rq) {
 161                dma_free_coherent(&rdev->lldi.pdev->dev,
 162                                  wq->rq.memsize, wq->rq.queue,
 163                                  dma_unmap_addr(&wq->rq, mapping));
 164                c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
 165                kfree(wq->rq.sw_rq);
 166                c4iw_put_qpid(rdev, wq->rq.qid, uctx);
 167        }
 168        return 0;
 169}
 170
 171/*
 172 * Determine the BAR2 virtual address and qid. If pbar2_pa is not NULL,
 173 * then this is a user mapping so compute the page-aligned physical address
 174 * for mapping.
 175 */
 176void __iomem *c4iw_bar2_addrs(struct c4iw_rdev *rdev, unsigned int qid,
 177                              enum cxgb4_bar2_qtype qtype,
 178                              unsigned int *pbar2_qid, u64 *pbar2_pa)
 179{
 180        u64 bar2_qoffset;
 181        int ret;
 182
 183        ret = cxgb4_bar2_sge_qregs(rdev->lldi.ports[0], qid, qtype,
 184                                   pbar2_pa ? 1 : 0,
 185                                   &bar2_qoffset, pbar2_qid);
 186        if (ret)
 187                return NULL;
 188
 189        if (pbar2_pa)
 190                *pbar2_pa = (rdev->bar2_pa + bar2_qoffset) & PAGE_MASK;
 191
 192        if (is_t4(rdev->lldi.adapter_type))
 193                return NULL;
 194
 195        return rdev->bar2_kva + bar2_qoffset;
 196}
 197
 198static int create_qp(struct c4iw_rdev *rdev, struct t4_wq *wq,
 199                     struct t4_cq *rcq, struct t4_cq *scq,
 200                     struct c4iw_dev_ucontext *uctx,
 201                     struct c4iw_wr_wait *wr_waitp,
 202                     int need_rq)
 203{
 204        int user = (uctx != &rdev->uctx);
 205        struct fw_ri_res_wr *res_wr;
 206        struct fw_ri_res *res;
 207        int wr_len;
 208        struct sk_buff *skb;
 209        int ret = 0;
 210        int eqsize;
 211
 212        wq->sq.qid = c4iw_get_qpid(rdev, uctx);
 213        if (!wq->sq.qid)
 214                return -ENOMEM;
 215
 216        if (need_rq) {
 217                wq->rq.qid = c4iw_get_qpid(rdev, uctx);
 218                if (!wq->rq.qid) {
 219                        ret = -ENOMEM;
 220                        goto free_sq_qid;
 221                }
 222        }
 223
 224        if (!user) {
 225                wq->sq.sw_sq = kcalloc(wq->sq.size, sizeof(*wq->sq.sw_sq),
 226                                       GFP_KERNEL);
 227                if (!wq->sq.sw_sq) {
 228                        ret = -ENOMEM;
 229                        goto free_rq_qid;//FIXME
 230                }
 231
 232                if (need_rq) {
 233                        wq->rq.sw_rq = kcalloc(wq->rq.size,
 234                                               sizeof(*wq->rq.sw_rq),
 235                                               GFP_KERNEL);
 236                        if (!wq->rq.sw_rq) {
 237                                ret = -ENOMEM;
 238                                goto free_sw_sq;
 239                        }
 240                }
 241        }
 242
 243        if (need_rq) {
 244                /*
 245                 * RQT must be a power of 2 and at least 16 deep.
 246                 */
 247                wq->rq.rqt_size =
 248                        roundup_pow_of_two(max_t(u16, wq->rq.size, 16));
 249                wq->rq.rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rq.rqt_size);
 250                if (!wq->rq.rqt_hwaddr) {
 251                        ret = -ENOMEM;
 252                        goto free_sw_rq;
 253                }
 254        }
 255
 256        ret = alloc_sq(rdev, &wq->sq, user);
 257        if (ret)
 258                goto free_hwaddr;
 259        memset(wq->sq.queue, 0, wq->sq.memsize);
 260        dma_unmap_addr_set(&wq->sq, mapping, wq->sq.dma_addr);
 261
 262        if (need_rq) {
 263                wq->rq.queue = dma_alloc_coherent(&rdev->lldi.pdev->dev,
 264                                                  wq->rq.memsize,
 265                                                  &wq->rq.dma_addr,
 266                                                  GFP_KERNEL);
 267                if (!wq->rq.queue) {
 268                        ret = -ENOMEM;
 269                        goto free_sq;
 270                }
 271                pr_debug("sq base va 0x%p pa 0x%llx rq base va 0x%p pa 0x%llx\n",
 272                         wq->sq.queue,
 273                         (unsigned long long)virt_to_phys(wq->sq.queue),
 274                         wq->rq.queue,
 275                         (unsigned long long)virt_to_phys(wq->rq.queue));
 276                memset(wq->rq.queue, 0, wq->rq.memsize);
 277                dma_unmap_addr_set(&wq->rq, mapping, wq->rq.dma_addr);
 278        }
 279
 280        wq->db = rdev->lldi.db_reg;
 281
 282        wq->sq.bar2_va = c4iw_bar2_addrs(rdev, wq->sq.qid,
 283                                         CXGB4_BAR2_QTYPE_EGRESS,
 284                                         &wq->sq.bar2_qid,
 285                                         user ? &wq->sq.bar2_pa : NULL);
 286        if (need_rq)
 287                wq->rq.bar2_va = c4iw_bar2_addrs(rdev, wq->rq.qid,
 288                                                 CXGB4_BAR2_QTYPE_EGRESS,
 289                                                 &wq->rq.bar2_qid,
 290                                                 user ? &wq->rq.bar2_pa : NULL);
 291
 292        /*
 293         * User mode must have bar2 access.
 294         */
 295        if (user && (!wq->sq.bar2_pa || (need_rq && !wq->rq.bar2_pa))) {
 296                pr_warn("%s: sqid %u or rqid %u not in BAR2 range\n",
 297                        pci_name(rdev->lldi.pdev), wq->sq.qid, wq->rq.qid);
 298                goto free_dma;
 299        }
 300
 301        wq->rdev = rdev;
 302        wq->rq.msn = 1;
 303
 304        /* build fw_ri_res_wr */
 305        wr_len = sizeof *res_wr + 2 * sizeof *res;
 306        if (need_rq)
 307                wr_len += sizeof(*res);
 308        skb = alloc_skb(wr_len, GFP_KERNEL);
 309        if (!skb) {
 310                ret = -ENOMEM;
 311                goto free_dma;
 312        }
 313        set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
 314
 315        res_wr = __skb_put_zero(skb, wr_len);
 316        res_wr->op_nres = cpu_to_be32(
 317                        FW_WR_OP_V(FW_RI_RES_WR) |
 318                        FW_RI_RES_WR_NRES_V(need_rq ? 2 : 1) |
 319                        FW_WR_COMPL_F);
 320        res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
 321        res_wr->cookie = (uintptr_t)wr_waitp;
 322        res = res_wr->res;
 323        res->u.sqrq.restype = FW_RI_RES_TYPE_SQ;
 324        res->u.sqrq.op = FW_RI_RES_OP_WRITE;
 325
 326        /*
 327         * eqsize is the number of 64B entries plus the status page size.
 328         */
 329        eqsize = wq->sq.size * T4_SQ_NUM_SLOTS +
 330                rdev->hw_queue.t4_eq_status_entries;
 331
 332        res->u.sqrq.fetchszm_to_iqid = cpu_to_be32(
 333                FW_RI_RES_WR_HOSTFCMODE_V(0) |  /* no host cidx updates */
 334                FW_RI_RES_WR_CPRIO_V(0) |       /* don't keep in chip cache */
 335                FW_RI_RES_WR_PCIECHN_V(0) |     /* set by uP at ri_init time */
 336                (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_ONCHIP_F : 0) |
 337                FW_RI_RES_WR_IQID_V(scq->cqid));
 338        res->u.sqrq.dcaen_to_eqsize = cpu_to_be32(
 339                FW_RI_RES_WR_DCAEN_V(0) |
 340                FW_RI_RES_WR_DCACPU_V(0) |
 341                FW_RI_RES_WR_FBMIN_V(2) |
 342                (t4_sq_onchip(&wq->sq) ? FW_RI_RES_WR_FBMAX_V(2) :
 343                                         FW_RI_RES_WR_FBMAX_V(3)) |
 344                FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
 345                FW_RI_RES_WR_CIDXFTHRESH_V(0) |
 346                FW_RI_RES_WR_EQSIZE_V(eqsize));
 347        res->u.sqrq.eqid = cpu_to_be32(wq->sq.qid);
 348        res->u.sqrq.eqaddr = cpu_to_be64(wq->sq.dma_addr);
 349
 350        if (need_rq) {
 351                res++;
 352                res->u.sqrq.restype = FW_RI_RES_TYPE_RQ;
 353                res->u.sqrq.op = FW_RI_RES_OP_WRITE;
 354
 355                /*
 356                 * eqsize is the number of 64B entries plus the status page size
 357                 */
 358                eqsize = wq->rq.size * T4_RQ_NUM_SLOTS +
 359                        rdev->hw_queue.t4_eq_status_entries;
 360                res->u.sqrq.fetchszm_to_iqid =
 361                        /* no host cidx updates */
 362                        cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
 363                        /* don't keep in chip cache */
 364                        FW_RI_RES_WR_CPRIO_V(0) |
 365                        /* set by uP at ri_init time */
 366                        FW_RI_RES_WR_PCIECHN_V(0) |
 367                        FW_RI_RES_WR_IQID_V(rcq->cqid));
 368                res->u.sqrq.dcaen_to_eqsize =
 369                        cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
 370                        FW_RI_RES_WR_DCACPU_V(0) |
 371                        FW_RI_RES_WR_FBMIN_V(2) |
 372                        FW_RI_RES_WR_FBMAX_V(3) |
 373                        FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
 374                        FW_RI_RES_WR_CIDXFTHRESH_V(0) |
 375                        FW_RI_RES_WR_EQSIZE_V(eqsize));
 376                res->u.sqrq.eqid = cpu_to_be32(wq->rq.qid);
 377                res->u.sqrq.eqaddr = cpu_to_be64(wq->rq.dma_addr);
 378        }
 379
 380        c4iw_init_wr_wait(wr_waitp);
 381        ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->sq.qid, __func__);
 382        if (ret)
 383                goto free_dma;
 384
 385        pr_debug("sqid 0x%x rqid 0x%x kdb 0x%p sq_bar2_addr %p rq_bar2_addr %p\n",
 386                 wq->sq.qid, wq->rq.qid, wq->db,
 387                 wq->sq.bar2_va, wq->rq.bar2_va);
 388
 389        return 0;
 390free_dma:
 391        if (need_rq)
 392                dma_free_coherent(&rdev->lldi.pdev->dev,
 393                                  wq->rq.memsize, wq->rq.queue,
 394                                  dma_unmap_addr(&wq->rq, mapping));
 395free_sq:
 396        dealloc_sq(rdev, &wq->sq);
 397free_hwaddr:
 398        if (need_rq)
 399                c4iw_rqtpool_free(rdev, wq->rq.rqt_hwaddr, wq->rq.rqt_size);
 400free_sw_rq:
 401        if (need_rq)
 402                kfree(wq->rq.sw_rq);
 403free_sw_sq:
 404        kfree(wq->sq.sw_sq);
 405free_rq_qid:
 406        if (need_rq)
 407                c4iw_put_qpid(rdev, wq->rq.qid, uctx);
 408free_sq_qid:
 409        c4iw_put_qpid(rdev, wq->sq.qid, uctx);
 410        return ret;
 411}
 412
 413static int build_immd(struct t4_sq *sq, struct fw_ri_immd *immdp,
 414                      const struct ib_send_wr *wr, int max, u32 *plenp)
 415{
 416        u8 *dstp, *srcp;
 417        u32 plen = 0;
 418        int i;
 419        int rem, len;
 420
 421        dstp = (u8 *)immdp->data;
 422        for (i = 0; i < wr->num_sge; i++) {
 423                if ((plen + wr->sg_list[i].length) > max)
 424                        return -EMSGSIZE;
 425                srcp = (u8 *)(unsigned long)wr->sg_list[i].addr;
 426                plen += wr->sg_list[i].length;
 427                rem = wr->sg_list[i].length;
 428                while (rem) {
 429                        if (dstp == (u8 *)&sq->queue[sq->size])
 430                                dstp = (u8 *)sq->queue;
 431                        if (rem <= (u8 *)&sq->queue[sq->size] - dstp)
 432                                len = rem;
 433                        else
 434                                len = (u8 *)&sq->queue[sq->size] - dstp;
 435                        memcpy(dstp, srcp, len);
 436                        dstp += len;
 437                        srcp += len;
 438                        rem -= len;
 439                }
 440        }
 441        len = roundup(plen + sizeof *immdp, 16) - (plen + sizeof *immdp);
 442        if (len)
 443                memset(dstp, 0, len);
 444        immdp->op = FW_RI_DATA_IMMD;
 445        immdp->r1 = 0;
 446        immdp->r2 = 0;
 447        immdp->immdlen = cpu_to_be32(plen);
 448        *plenp = plen;
 449        return 0;
 450}
 451
 452static int build_isgl(__be64 *queue_start, __be64 *queue_end,
 453                      struct fw_ri_isgl *isglp, struct ib_sge *sg_list,
 454                      int num_sge, u32 *plenp)
 455
 456{
 457        int i;
 458        u32 plen = 0;
 459        __be64 *flitp;
 460
 461        if ((__be64 *)isglp == queue_end)
 462                isglp = (struct fw_ri_isgl *)queue_start;
 463
 464        flitp = (__be64 *)isglp->sge;
 465
 466        for (i = 0; i < num_sge; i++) {
 467                if ((plen + sg_list[i].length) < plen)
 468                        return -EMSGSIZE;
 469                plen += sg_list[i].length;
 470                *flitp = cpu_to_be64(((u64)sg_list[i].lkey << 32) |
 471                                     sg_list[i].length);
 472                if (++flitp == queue_end)
 473                        flitp = queue_start;
 474                *flitp = cpu_to_be64(sg_list[i].addr);
 475                if (++flitp == queue_end)
 476                        flitp = queue_start;
 477        }
 478        *flitp = (__force __be64)0;
 479        isglp->op = FW_RI_DATA_ISGL;
 480        isglp->r1 = 0;
 481        isglp->nsge = cpu_to_be16(num_sge);
 482        isglp->r2 = 0;
 483        if (plenp)
 484                *plenp = plen;
 485        return 0;
 486}
 487
 488static int build_rdma_send(struct t4_sq *sq, union t4_wr *wqe,
 489                           const struct ib_send_wr *wr, u8 *len16)
 490{
 491        u32 plen;
 492        int size;
 493        int ret;
 494
 495        if (wr->num_sge > T4_MAX_SEND_SGE)
 496                return -EINVAL;
 497        switch (wr->opcode) {
 498        case IB_WR_SEND:
 499                if (wr->send_flags & IB_SEND_SOLICITED)
 500                        wqe->send.sendop_pkd = cpu_to_be32(
 501                                FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE));
 502                else
 503                        wqe->send.sendop_pkd = cpu_to_be32(
 504                                FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND));
 505                wqe->send.stag_inv = 0;
 506                break;
 507        case IB_WR_SEND_WITH_INV:
 508                if (wr->send_flags & IB_SEND_SOLICITED)
 509                        wqe->send.sendop_pkd = cpu_to_be32(
 510                                FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_SE_INV));
 511                else
 512                        wqe->send.sendop_pkd = cpu_to_be32(
 513                                FW_RI_SEND_WR_SENDOP_V(FW_RI_SEND_WITH_INV));
 514                wqe->send.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
 515                break;
 516
 517        default:
 518                return -EINVAL;
 519        }
 520        wqe->send.r3 = 0;
 521        wqe->send.r4 = 0;
 522
 523        plen = 0;
 524        if (wr->num_sge) {
 525                if (wr->send_flags & IB_SEND_INLINE) {
 526                        ret = build_immd(sq, wqe->send.u.immd_src, wr,
 527                                         T4_MAX_SEND_INLINE, &plen);
 528                        if (ret)
 529                                return ret;
 530                        size = sizeof wqe->send + sizeof(struct fw_ri_immd) +
 531                               plen;
 532                } else {
 533                        ret = build_isgl((__be64 *)sq->queue,
 534                                         (__be64 *)&sq->queue[sq->size],
 535                                         wqe->send.u.isgl_src,
 536                                         wr->sg_list, wr->num_sge, &plen);
 537                        if (ret)
 538                                return ret;
 539                        size = sizeof wqe->send + sizeof(struct fw_ri_isgl) +
 540                               wr->num_sge * sizeof(struct fw_ri_sge);
 541                }
 542        } else {
 543                wqe->send.u.immd_src[0].op = FW_RI_DATA_IMMD;
 544                wqe->send.u.immd_src[0].r1 = 0;
 545                wqe->send.u.immd_src[0].r2 = 0;
 546                wqe->send.u.immd_src[0].immdlen = 0;
 547                size = sizeof wqe->send + sizeof(struct fw_ri_immd);
 548                plen = 0;
 549        }
 550        *len16 = DIV_ROUND_UP(size, 16);
 551        wqe->send.plen = cpu_to_be32(plen);
 552        return 0;
 553}
 554
 555static int build_rdma_write(struct t4_sq *sq, union t4_wr *wqe,
 556                            const struct ib_send_wr *wr, u8 *len16)
 557{
 558        u32 plen;
 559        int size;
 560        int ret;
 561
 562        if (wr->num_sge > T4_MAX_SEND_SGE)
 563                return -EINVAL;
 564
 565        /*
 566         * iWARP protocol supports 64 bit immediate data but rdma api
 567         * limits it to 32bit.
 568         */
 569        if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
 570                wqe->write.iw_imm_data.ib_imm_data.imm_data32 = wr->ex.imm_data;
 571        else
 572                wqe->write.iw_imm_data.ib_imm_data.imm_data32 = 0;
 573        wqe->write.stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
 574        wqe->write.to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
 575        if (wr->num_sge) {
 576                if (wr->send_flags & IB_SEND_INLINE) {
 577                        ret = build_immd(sq, wqe->write.u.immd_src, wr,
 578                                         T4_MAX_WRITE_INLINE, &plen);
 579                        if (ret)
 580                                return ret;
 581                        size = sizeof wqe->write + sizeof(struct fw_ri_immd) +
 582                               plen;
 583                } else {
 584                        ret = build_isgl((__be64 *)sq->queue,
 585                                         (__be64 *)&sq->queue[sq->size],
 586                                         wqe->write.u.isgl_src,
 587                                         wr->sg_list, wr->num_sge, &plen);
 588                        if (ret)
 589                                return ret;
 590                        size = sizeof wqe->write + sizeof(struct fw_ri_isgl) +
 591                               wr->num_sge * sizeof(struct fw_ri_sge);
 592                }
 593        } else {
 594                wqe->write.u.immd_src[0].op = FW_RI_DATA_IMMD;
 595                wqe->write.u.immd_src[0].r1 = 0;
 596                wqe->write.u.immd_src[0].r2 = 0;
 597                wqe->write.u.immd_src[0].immdlen = 0;
 598                size = sizeof wqe->write + sizeof(struct fw_ri_immd);
 599                plen = 0;
 600        }
 601        *len16 = DIV_ROUND_UP(size, 16);
 602        wqe->write.plen = cpu_to_be32(plen);
 603        return 0;
 604}
 605
 606static void build_immd_cmpl(struct t4_sq *sq, struct fw_ri_immd_cmpl *immdp,
 607                            struct ib_send_wr *wr)
 608{
 609        memcpy((u8 *)immdp->data, (u8 *)(uintptr_t)wr->sg_list->addr, 16);
 610        memset(immdp->r1, 0, 6);
 611        immdp->op = FW_RI_DATA_IMMD;
 612        immdp->immdlen = 16;
 613}
 614
 615static void build_rdma_write_cmpl(struct t4_sq *sq,
 616                                  struct fw_ri_rdma_write_cmpl_wr *wcwr,
 617                                  const struct ib_send_wr *wr, u8 *len16)
 618{
 619        u32 plen;
 620        int size;
 621
 622        /*
 623         * This code assumes the struct fields preceding the write isgl
 624         * fit in one 64B WR slot.  This is because the WQE is built
 625         * directly in the dma queue, and wrapping is only handled
 626         * by the code buildling sgls.  IE the "fixed part" of the wr
 627         * structs must all fit in 64B.  The WQE build code should probably be
 628         * redesigned to avoid this restriction, but for now just add
 629         * the BUILD_BUG_ON() to catch if this WQE struct gets too big.
 630         */
 631        BUILD_BUG_ON(offsetof(struct fw_ri_rdma_write_cmpl_wr, u) > 64);
 632
 633        wcwr->stag_sink = cpu_to_be32(rdma_wr(wr)->rkey);
 634        wcwr->to_sink = cpu_to_be64(rdma_wr(wr)->remote_addr);
 635        if (wr->next->opcode == IB_WR_SEND)
 636                wcwr->stag_inv = 0;
 637        else
 638                wcwr->stag_inv = cpu_to_be32(wr->next->ex.invalidate_rkey);
 639        wcwr->r2 = 0;
 640        wcwr->r3 = 0;
 641
 642        /* SEND_INV SGL */
 643        if (wr->next->send_flags & IB_SEND_INLINE)
 644                build_immd_cmpl(sq, &wcwr->u_cmpl.immd_src, wr->next);
 645        else
 646                build_isgl((__be64 *)sq->queue, (__be64 *)&sq->queue[sq->size],
 647                           &wcwr->u_cmpl.isgl_src, wr->next->sg_list, 1, NULL);
 648
 649        /* WRITE SGL */
 650        build_isgl((__be64 *)sq->queue, (__be64 *)&sq->queue[sq->size],
 651                   wcwr->u.isgl_src, wr->sg_list, wr->num_sge, &plen);
 652
 653        size = sizeof(*wcwr) + sizeof(struct fw_ri_isgl) +
 654                wr->num_sge * sizeof(struct fw_ri_sge);
 655        wcwr->plen = cpu_to_be32(plen);
 656        *len16 = DIV_ROUND_UP(size, 16);
 657}
 658
 659static int build_rdma_read(union t4_wr *wqe, const struct ib_send_wr *wr,
 660                           u8 *len16)
 661{
 662        if (wr->num_sge > 1)
 663                return -EINVAL;
 664        if (wr->num_sge && wr->sg_list[0].length) {
 665                wqe->read.stag_src = cpu_to_be32(rdma_wr(wr)->rkey);
 666                wqe->read.to_src_hi = cpu_to_be32((u32)(rdma_wr(wr)->remote_addr
 667                                                        >> 32));
 668                wqe->read.to_src_lo = cpu_to_be32((u32)rdma_wr(wr)->remote_addr);
 669                wqe->read.stag_sink = cpu_to_be32(wr->sg_list[0].lkey);
 670                wqe->read.plen = cpu_to_be32(wr->sg_list[0].length);
 671                wqe->read.to_sink_hi = cpu_to_be32((u32)(wr->sg_list[0].addr
 672                                                         >> 32));
 673                wqe->read.to_sink_lo = cpu_to_be32((u32)(wr->sg_list[0].addr));
 674        } else {
 675                wqe->read.stag_src = cpu_to_be32(2);
 676                wqe->read.to_src_hi = 0;
 677                wqe->read.to_src_lo = 0;
 678                wqe->read.stag_sink = cpu_to_be32(2);
 679                wqe->read.plen = 0;
 680                wqe->read.to_sink_hi = 0;
 681                wqe->read.to_sink_lo = 0;
 682        }
 683        wqe->read.r2 = 0;
 684        wqe->read.r5 = 0;
 685        *len16 = DIV_ROUND_UP(sizeof wqe->read, 16);
 686        return 0;
 687}
 688
 689static void post_write_cmpl(struct c4iw_qp *qhp, const struct ib_send_wr *wr)
 690{
 691        bool send_signaled = (wr->next->send_flags & IB_SEND_SIGNALED) ||
 692                             qhp->sq_sig_all;
 693        bool write_signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
 694                              qhp->sq_sig_all;
 695        struct t4_swsqe *swsqe;
 696        union t4_wr *wqe;
 697        u16 write_wrid;
 698        u8 len16;
 699        u16 idx;
 700
 701        /*
 702         * The sw_sq entries still look like a WRITE and a SEND and consume
 703         * 2 slots. The FW WR, however, will be a single uber-WR.
 704         */
 705        wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
 706               qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
 707        build_rdma_write_cmpl(&qhp->wq.sq, &wqe->write_cmpl, wr, &len16);
 708
 709        /* WRITE swsqe */
 710        swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
 711        swsqe->opcode = FW_RI_RDMA_WRITE;
 712        swsqe->idx = qhp->wq.sq.pidx;
 713        swsqe->complete = 0;
 714        swsqe->signaled = write_signaled;
 715        swsqe->flushed = 0;
 716        swsqe->wr_id = wr->wr_id;
 717        if (c4iw_wr_log) {
 718                swsqe->sge_ts =
 719                        cxgb4_read_sge_timestamp(qhp->rhp->rdev.lldi.ports[0]);
 720                swsqe->host_time = ktime_get();
 721        }
 722
 723        write_wrid = qhp->wq.sq.pidx;
 724
 725        /* just bump the sw_sq */
 726        qhp->wq.sq.in_use++;
 727        if (++qhp->wq.sq.pidx == qhp->wq.sq.size)
 728                qhp->wq.sq.pidx = 0;
 729
 730        /* SEND_WITH_INV swsqe */
 731        swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
 732        if (wr->next->opcode == IB_WR_SEND)
 733                swsqe->opcode = FW_RI_SEND;
 734        else
 735                swsqe->opcode = FW_RI_SEND_WITH_INV;
 736        swsqe->idx = qhp->wq.sq.pidx;
 737        swsqe->complete = 0;
 738        swsqe->signaled = send_signaled;
 739        swsqe->flushed = 0;
 740        swsqe->wr_id = wr->next->wr_id;
 741        if (c4iw_wr_log) {
 742                swsqe->sge_ts =
 743                        cxgb4_read_sge_timestamp(qhp->rhp->rdev.lldi.ports[0]);
 744                swsqe->host_time = ktime_get();
 745        }
 746
 747        wqe->write_cmpl.flags_send = send_signaled ? FW_RI_COMPLETION_FLAG : 0;
 748        wqe->write_cmpl.wrid_send = qhp->wq.sq.pidx;
 749
 750        init_wr_hdr(wqe, write_wrid, FW_RI_RDMA_WRITE_CMPL_WR,
 751                    write_signaled ? FW_RI_COMPLETION_FLAG : 0, len16);
 752        t4_sq_produce(&qhp->wq, len16);
 753        idx = DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
 754
 755        t4_ring_sq_db(&qhp->wq, idx, wqe);
 756}
 757
 758static int build_rdma_recv(struct c4iw_qp *qhp, union t4_recv_wr *wqe,
 759                           const struct ib_recv_wr *wr, u8 *len16)
 760{
 761        int ret;
 762
 763        ret = build_isgl((__be64 *)qhp->wq.rq.queue,
 764                         (__be64 *)&qhp->wq.rq.queue[qhp->wq.rq.size],
 765                         &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
 766        if (ret)
 767                return ret;
 768        *len16 = DIV_ROUND_UP(sizeof wqe->recv +
 769                              wr->num_sge * sizeof(struct fw_ri_sge), 16);
 770        return 0;
 771}
 772
 773static int build_srq_recv(union t4_recv_wr *wqe, const struct ib_recv_wr *wr,
 774                          u8 *len16)
 775{
 776        int ret;
 777
 778        ret = build_isgl((__be64 *)wqe, (__be64 *)(wqe + 1),
 779                         &wqe->recv.isgl, wr->sg_list, wr->num_sge, NULL);
 780        if (ret)
 781                return ret;
 782        *len16 = DIV_ROUND_UP(sizeof(wqe->recv) +
 783                              wr->num_sge * sizeof(struct fw_ri_sge), 16);
 784        return 0;
 785}
 786
 787static void build_tpte_memreg(struct fw_ri_fr_nsmr_tpte_wr *fr,
 788                              const struct ib_reg_wr *wr, struct c4iw_mr *mhp,
 789                              u8 *len16)
 790{
 791        __be64 *p = (__be64 *)fr->pbl;
 792
 793        fr->r2 = cpu_to_be32(0);
 794        fr->stag = cpu_to_be32(mhp->ibmr.rkey);
 795
 796        fr->tpte.valid_to_pdid = cpu_to_be32(FW_RI_TPTE_VALID_F |
 797                FW_RI_TPTE_STAGKEY_V((mhp->ibmr.rkey & FW_RI_TPTE_STAGKEY_M)) |
 798                FW_RI_TPTE_STAGSTATE_V(1) |
 799                FW_RI_TPTE_STAGTYPE_V(FW_RI_STAG_NSMR) |
 800                FW_RI_TPTE_PDID_V(mhp->attr.pdid));
 801        fr->tpte.locread_to_qpid = cpu_to_be32(
 802                FW_RI_TPTE_PERM_V(c4iw_ib_to_tpt_access(wr->access)) |
 803                FW_RI_TPTE_ADDRTYPE_V(FW_RI_VA_BASED_TO) |
 804                FW_RI_TPTE_PS_V(ilog2(wr->mr->page_size) - 12));
 805        fr->tpte.nosnoop_pbladdr = cpu_to_be32(FW_RI_TPTE_PBLADDR_V(
 806                PBL_OFF(&mhp->rhp->rdev, mhp->attr.pbl_addr)>>3));
 807        fr->tpte.dca_mwbcnt_pstag = cpu_to_be32(0);
 808        fr->tpte.len_hi = cpu_to_be32(0);
 809        fr->tpte.len_lo = cpu_to_be32(mhp->ibmr.length);
 810        fr->tpte.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
 811        fr->tpte.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova & 0xffffffff);
 812
 813        p[0] = cpu_to_be64((u64)mhp->mpl[0]);
 814        p[1] = cpu_to_be64((u64)mhp->mpl[1]);
 815
 816        *len16 = DIV_ROUND_UP(sizeof(*fr), 16);
 817}
 818
 819static int build_memreg(struct t4_sq *sq, union t4_wr *wqe,
 820                        const struct ib_reg_wr *wr, struct c4iw_mr *mhp,
 821                        u8 *len16, bool dsgl_supported)
 822{
 823        struct fw_ri_immd *imdp;
 824        __be64 *p;
 825        int i;
 826        int pbllen = roundup(mhp->mpl_len * sizeof(u64), 32);
 827        int rem;
 828
 829        if (mhp->mpl_len > t4_max_fr_depth(dsgl_supported && use_dsgl))
 830                return -EINVAL;
 831
 832        wqe->fr.qpbinde_to_dcacpu = 0;
 833        wqe->fr.pgsz_shift = ilog2(wr->mr->page_size) - 12;
 834        wqe->fr.addr_type = FW_RI_VA_BASED_TO;
 835        wqe->fr.mem_perms = c4iw_ib_to_tpt_access(wr->access);
 836        wqe->fr.len_hi = 0;
 837        wqe->fr.len_lo = cpu_to_be32(mhp->ibmr.length);
 838        wqe->fr.stag = cpu_to_be32(wr->key);
 839        wqe->fr.va_hi = cpu_to_be32(mhp->ibmr.iova >> 32);
 840        wqe->fr.va_lo_fbo = cpu_to_be32(mhp->ibmr.iova &
 841                                        0xffffffff);
 842
 843        if (dsgl_supported && use_dsgl && (pbllen > max_fr_immd)) {
 844                struct fw_ri_dsgl *sglp;
 845
 846                for (i = 0; i < mhp->mpl_len; i++)
 847                        mhp->mpl[i] = (__force u64)cpu_to_be64((u64)mhp->mpl[i]);
 848
 849                sglp = (struct fw_ri_dsgl *)(&wqe->fr + 1);
 850                sglp->op = FW_RI_DATA_DSGL;
 851                sglp->r1 = 0;
 852                sglp->nsge = cpu_to_be16(1);
 853                sglp->addr0 = cpu_to_be64(mhp->mpl_addr);
 854                sglp->len0 = cpu_to_be32(pbllen);
 855
 856                *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*sglp), 16);
 857        } else {
 858                imdp = (struct fw_ri_immd *)(&wqe->fr + 1);
 859                imdp->op = FW_RI_DATA_IMMD;
 860                imdp->r1 = 0;
 861                imdp->r2 = 0;
 862                imdp->immdlen = cpu_to_be32(pbllen);
 863                p = (__be64 *)(imdp + 1);
 864                rem = pbllen;
 865                for (i = 0; i < mhp->mpl_len; i++) {
 866                        *p = cpu_to_be64((u64)mhp->mpl[i]);
 867                        rem -= sizeof(*p);
 868                        if (++p == (__be64 *)&sq->queue[sq->size])
 869                                p = (__be64 *)sq->queue;
 870                }
 871                while (rem) {
 872                        *p = 0;
 873                        rem -= sizeof(*p);
 874                        if (++p == (__be64 *)&sq->queue[sq->size])
 875                                p = (__be64 *)sq->queue;
 876                }
 877                *len16 = DIV_ROUND_UP(sizeof(wqe->fr) + sizeof(*imdp)
 878                                      + pbllen, 16);
 879        }
 880        return 0;
 881}
 882
 883static int build_inv_stag(union t4_wr *wqe, const struct ib_send_wr *wr,
 884                          u8 *len16)
 885{
 886        wqe->inv.stag_inv = cpu_to_be32(wr->ex.invalidate_rkey);
 887        wqe->inv.r2 = 0;
 888        *len16 = DIV_ROUND_UP(sizeof wqe->inv, 16);
 889        return 0;
 890}
 891
 892static void free_qp_work(struct work_struct *work)
 893{
 894        struct c4iw_ucontext *ucontext;
 895        struct c4iw_qp *qhp;
 896        struct c4iw_dev *rhp;
 897
 898        qhp = container_of(work, struct c4iw_qp, free_work);
 899        ucontext = qhp->ucontext;
 900        rhp = qhp->rhp;
 901
 902        pr_debug("qhp %p ucontext %p\n", qhp, ucontext);
 903        destroy_qp(&rhp->rdev, &qhp->wq,
 904                   ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !qhp->srq);
 905
 906        if (ucontext)
 907                c4iw_put_ucontext(ucontext);
 908        c4iw_put_wr_wait(qhp->wr_waitp);
 909        kfree(qhp);
 910}
 911
 912static void queue_qp_free(struct kref *kref)
 913{
 914        struct c4iw_qp *qhp;
 915
 916        qhp = container_of(kref, struct c4iw_qp, kref);
 917        pr_debug("qhp %p\n", qhp);
 918        queue_work(qhp->rhp->rdev.free_workq, &qhp->free_work);
 919}
 920
 921void c4iw_qp_add_ref(struct ib_qp *qp)
 922{
 923        pr_debug("ib_qp %p\n", qp);
 924        kref_get(&to_c4iw_qp(qp)->kref);
 925}
 926
 927void c4iw_qp_rem_ref(struct ib_qp *qp)
 928{
 929        pr_debug("ib_qp %p\n", qp);
 930        kref_put(&to_c4iw_qp(qp)->kref, queue_qp_free);
 931}
 932
 933static void add_to_fc_list(struct list_head *head, struct list_head *entry)
 934{
 935        if (list_empty(entry))
 936                list_add_tail(entry, head);
 937}
 938
 939static int ring_kernel_sq_db(struct c4iw_qp *qhp, u16 inc)
 940{
 941        unsigned long flags;
 942
 943        spin_lock_irqsave(&qhp->rhp->lock, flags);
 944        spin_lock(&qhp->lock);
 945        if (qhp->rhp->db_state == NORMAL)
 946                t4_ring_sq_db(&qhp->wq, inc, NULL);
 947        else {
 948                add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
 949                qhp->wq.sq.wq_pidx_inc += inc;
 950        }
 951        spin_unlock(&qhp->lock);
 952        spin_unlock_irqrestore(&qhp->rhp->lock, flags);
 953        return 0;
 954}
 955
 956static int ring_kernel_rq_db(struct c4iw_qp *qhp, u16 inc)
 957{
 958        unsigned long flags;
 959
 960        spin_lock_irqsave(&qhp->rhp->lock, flags);
 961        spin_lock(&qhp->lock);
 962        if (qhp->rhp->db_state == NORMAL)
 963                t4_ring_rq_db(&qhp->wq, inc, NULL);
 964        else {
 965                add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
 966                qhp->wq.rq.wq_pidx_inc += inc;
 967        }
 968        spin_unlock(&qhp->lock);
 969        spin_unlock_irqrestore(&qhp->rhp->lock, flags);
 970        return 0;
 971}
 972
 973static int ib_to_fw_opcode(int ib_opcode)
 974{
 975        int opcode;
 976
 977        switch (ib_opcode) {
 978        case IB_WR_SEND_WITH_INV:
 979                opcode = FW_RI_SEND_WITH_INV;
 980                break;
 981        case IB_WR_SEND:
 982                opcode = FW_RI_SEND;
 983                break;
 984        case IB_WR_RDMA_WRITE:
 985                opcode = FW_RI_RDMA_WRITE;
 986                break;
 987        case IB_WR_RDMA_WRITE_WITH_IMM:
 988                opcode = FW_RI_WRITE_IMMEDIATE;
 989                break;
 990        case IB_WR_RDMA_READ:
 991        case IB_WR_RDMA_READ_WITH_INV:
 992                opcode = FW_RI_READ_REQ;
 993                break;
 994        case IB_WR_REG_MR:
 995                opcode = FW_RI_FAST_REGISTER;
 996                break;
 997        case IB_WR_LOCAL_INV:
 998                opcode = FW_RI_LOCAL_INV;
 999                break;
1000        default:
1001                opcode = -EINVAL;
1002        }
1003        return opcode;
1004}
1005
1006static int complete_sq_drain_wr(struct c4iw_qp *qhp,
1007                                const struct ib_send_wr *wr)
1008{
1009        struct t4_cqe cqe = {};
1010        struct c4iw_cq *schp;
1011        unsigned long flag;
1012        struct t4_cq *cq;
1013        int opcode;
1014
1015        schp = to_c4iw_cq(qhp->ibqp.send_cq);
1016        cq = &schp->cq;
1017
1018        opcode = ib_to_fw_opcode(wr->opcode);
1019        if (opcode < 0)
1020                return opcode;
1021
1022        cqe.u.drain_cookie = wr->wr_id;
1023        cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
1024                                 CQE_OPCODE_V(opcode) |
1025                                 CQE_TYPE_V(1) |
1026                                 CQE_SWCQE_V(1) |
1027                                 CQE_DRAIN_V(1) |
1028                                 CQE_QPID_V(qhp->wq.sq.qid));
1029
1030        spin_lock_irqsave(&schp->lock, flag);
1031        cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
1032        cq->sw_queue[cq->sw_pidx] = cqe;
1033        t4_swcq_produce(cq);
1034        spin_unlock_irqrestore(&schp->lock, flag);
1035
1036        if (t4_clear_cq_armed(&schp->cq)) {
1037                spin_lock_irqsave(&schp->comp_handler_lock, flag);
1038                (*schp->ibcq.comp_handler)(&schp->ibcq,
1039                                           schp->ibcq.cq_context);
1040                spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1041        }
1042        return 0;
1043}
1044
1045static int complete_sq_drain_wrs(struct c4iw_qp *qhp,
1046                                 const struct ib_send_wr *wr,
1047                                 const struct ib_send_wr **bad_wr)
1048{
1049        int ret = 0;
1050
1051        while (wr) {
1052                ret = complete_sq_drain_wr(qhp, wr);
1053                if (ret) {
1054                        *bad_wr = wr;
1055                        break;
1056                }
1057                wr = wr->next;
1058        }
1059        return ret;
1060}
1061
1062static void complete_rq_drain_wr(struct c4iw_qp *qhp,
1063                                 const struct ib_recv_wr *wr)
1064{
1065        struct t4_cqe cqe = {};
1066        struct c4iw_cq *rchp;
1067        unsigned long flag;
1068        struct t4_cq *cq;
1069
1070        rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1071        cq = &rchp->cq;
1072
1073        cqe.u.drain_cookie = wr->wr_id;
1074        cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
1075                                 CQE_OPCODE_V(FW_RI_SEND) |
1076                                 CQE_TYPE_V(0) |
1077                                 CQE_SWCQE_V(1) |
1078                                 CQE_DRAIN_V(1) |
1079                                 CQE_QPID_V(qhp->wq.sq.qid));
1080
1081        spin_lock_irqsave(&rchp->lock, flag);
1082        cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
1083        cq->sw_queue[cq->sw_pidx] = cqe;
1084        t4_swcq_produce(cq);
1085        spin_unlock_irqrestore(&rchp->lock, flag);
1086
1087        if (t4_clear_cq_armed(&rchp->cq)) {
1088                spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1089                (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1090                                           rchp->ibcq.cq_context);
1091                spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1092        }
1093}
1094
1095static void complete_rq_drain_wrs(struct c4iw_qp *qhp,
1096                                  const struct ib_recv_wr *wr)
1097{
1098        while (wr) {
1099                complete_rq_drain_wr(qhp, wr);
1100                wr = wr->next;
1101        }
1102}
1103
1104int c4iw_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
1105                   const struct ib_send_wr **bad_wr)
1106{
1107        int err = 0;
1108        u8 len16 = 0;
1109        enum fw_wr_opcodes fw_opcode = 0;
1110        enum fw_ri_wr_flags fw_flags;
1111        struct c4iw_qp *qhp;
1112        struct c4iw_dev *rhp;
1113        union t4_wr *wqe = NULL;
1114        u32 num_wrs;
1115        struct t4_swsqe *swsqe;
1116        unsigned long flag;
1117        u16 idx = 0;
1118
1119        qhp = to_c4iw_qp(ibqp);
1120        rhp = qhp->rhp;
1121        spin_lock_irqsave(&qhp->lock, flag);
1122
1123        /*
1124         * If the qp has been flushed, then just insert a special
1125         * drain cqe.
1126         */
1127        if (qhp->wq.flushed) {
1128                spin_unlock_irqrestore(&qhp->lock, flag);
1129                err = complete_sq_drain_wrs(qhp, wr, bad_wr);
1130                return err;
1131        }
1132        num_wrs = t4_sq_avail(&qhp->wq);
1133        if (num_wrs == 0) {
1134                spin_unlock_irqrestore(&qhp->lock, flag);
1135                *bad_wr = wr;
1136                return -ENOMEM;
1137        }
1138
1139        /*
1140         * Fastpath for NVMe-oF target WRITE + SEND_WITH_INV wr chain which is
1141         * the response for small NVMEe-oF READ requests.  If the chain is
1142         * exactly a WRITE->SEND_WITH_INV or a WRITE->SEND and the sgl depths
1143         * and lengths meet the requirements of the fw_ri_write_cmpl_wr work
1144         * request, then build and post the write_cmpl WR. If any of the tests
1145         * below are not true, then we continue on with the tradtional WRITE
1146         * and SEND WRs.
1147         */
1148        if (qhp->rhp->rdev.lldi.write_cmpl_support &&
1149            CHELSIO_CHIP_VERSION(qhp->rhp->rdev.lldi.adapter_type) >=
1150            CHELSIO_T5 &&
1151            wr && wr->next && !wr->next->next &&
1152            wr->opcode == IB_WR_RDMA_WRITE &&
1153            wr->sg_list[0].length && wr->num_sge <= T4_WRITE_CMPL_MAX_SGL &&
1154            (wr->next->opcode == IB_WR_SEND ||
1155            wr->next->opcode == IB_WR_SEND_WITH_INV) &&
1156            wr->next->sg_list[0].length == T4_WRITE_CMPL_MAX_CQE &&
1157            wr->next->num_sge == 1 && num_wrs >= 2) {
1158                post_write_cmpl(qhp, wr);
1159                spin_unlock_irqrestore(&qhp->lock, flag);
1160                return 0;
1161        }
1162
1163        while (wr) {
1164                if (num_wrs == 0) {
1165                        err = -ENOMEM;
1166                        *bad_wr = wr;
1167                        break;
1168                }
1169                wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
1170                      qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
1171
1172                fw_flags = 0;
1173                if (wr->send_flags & IB_SEND_SOLICITED)
1174                        fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
1175                if (wr->send_flags & IB_SEND_SIGNALED || qhp->sq_sig_all)
1176                        fw_flags |= FW_RI_COMPLETION_FLAG;
1177                swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
1178                switch (wr->opcode) {
1179                case IB_WR_SEND_WITH_INV:
1180                case IB_WR_SEND:
1181                        if (wr->send_flags & IB_SEND_FENCE)
1182                                fw_flags |= FW_RI_READ_FENCE_FLAG;
1183                        fw_opcode = FW_RI_SEND_WR;
1184                        if (wr->opcode == IB_WR_SEND)
1185                                swsqe->opcode = FW_RI_SEND;
1186                        else
1187                                swsqe->opcode = FW_RI_SEND_WITH_INV;
1188                        err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
1189                        break;
1190                case IB_WR_RDMA_WRITE_WITH_IMM:
1191                        if (unlikely(!rhp->rdev.lldi.write_w_imm_support)) {
1192                                err = -EINVAL;
1193                                break;
1194                        }
1195                        fw_flags |= FW_RI_RDMA_WRITE_WITH_IMMEDIATE;
1196                        /*FALLTHROUGH*/
1197                case IB_WR_RDMA_WRITE:
1198                        fw_opcode = FW_RI_RDMA_WRITE_WR;
1199                        swsqe->opcode = FW_RI_RDMA_WRITE;
1200                        err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
1201                        break;
1202                case IB_WR_RDMA_READ:
1203                case IB_WR_RDMA_READ_WITH_INV:
1204                        fw_opcode = FW_RI_RDMA_READ_WR;
1205                        swsqe->opcode = FW_RI_READ_REQ;
1206                        if (wr->opcode == IB_WR_RDMA_READ_WITH_INV) {
1207                                c4iw_invalidate_mr(rhp, wr->sg_list[0].lkey);
1208                                fw_flags = FW_RI_RDMA_READ_INVALIDATE;
1209                        } else {
1210                                fw_flags = 0;
1211                        }
1212                        err = build_rdma_read(wqe, wr, &len16);
1213                        if (err)
1214                                break;
1215                        swsqe->read_len = wr->sg_list[0].length;
1216                        if (!qhp->wq.sq.oldest_read)
1217                                qhp->wq.sq.oldest_read = swsqe;
1218                        break;
1219                case IB_WR_REG_MR: {
1220                        struct c4iw_mr *mhp = to_c4iw_mr(reg_wr(wr)->mr);
1221
1222                        swsqe->opcode = FW_RI_FAST_REGISTER;
1223                        if (rhp->rdev.lldi.fr_nsmr_tpte_wr_support &&
1224                            !mhp->attr.state && mhp->mpl_len <= 2) {
1225                                fw_opcode = FW_RI_FR_NSMR_TPTE_WR;
1226                                build_tpte_memreg(&wqe->fr_tpte, reg_wr(wr),
1227                                                  mhp, &len16);
1228                        } else {
1229                                fw_opcode = FW_RI_FR_NSMR_WR;
1230                                err = build_memreg(&qhp->wq.sq, wqe, reg_wr(wr),
1231                                       mhp, &len16,
1232                                       rhp->rdev.lldi.ulptx_memwrite_dsgl);
1233                                if (err)
1234                                        break;
1235                        }
1236                        mhp->attr.state = 1;
1237                        break;
1238                }
1239                case IB_WR_LOCAL_INV:
1240                        if (wr->send_flags & IB_SEND_FENCE)
1241                                fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
1242                        fw_opcode = FW_RI_INV_LSTAG_WR;
1243                        swsqe->opcode = FW_RI_LOCAL_INV;
1244                        err = build_inv_stag(wqe, wr, &len16);
1245                        c4iw_invalidate_mr(rhp, wr->ex.invalidate_rkey);
1246                        break;
1247                default:
1248                        pr_warn("%s post of type=%d TBD!\n", __func__,
1249                                wr->opcode);
1250                        err = -EINVAL;
1251                }
1252                if (err) {
1253                        *bad_wr = wr;
1254                        break;
1255                }
1256                swsqe->idx = qhp->wq.sq.pidx;
1257                swsqe->complete = 0;
1258                swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
1259                                  qhp->sq_sig_all;
1260                swsqe->flushed = 0;
1261                swsqe->wr_id = wr->wr_id;
1262                if (c4iw_wr_log) {
1263                        swsqe->sge_ts = cxgb4_read_sge_timestamp(
1264                                        rhp->rdev.lldi.ports[0]);
1265                        swsqe->host_time = ktime_get();
1266                }
1267
1268                init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
1269
1270                pr_debug("cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
1271                         (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
1272                         swsqe->opcode, swsqe->read_len);
1273                wr = wr->next;
1274                num_wrs--;
1275                t4_sq_produce(&qhp->wq, len16);
1276                idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
1277        }
1278        if (!rhp->rdev.status_page->db_off) {
1279                t4_ring_sq_db(&qhp->wq, idx, wqe);
1280                spin_unlock_irqrestore(&qhp->lock, flag);
1281        } else {
1282                spin_unlock_irqrestore(&qhp->lock, flag);
1283                ring_kernel_sq_db(qhp, idx);
1284        }
1285        return err;
1286}
1287
1288int c4iw_post_receive(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1289                      const struct ib_recv_wr **bad_wr)
1290{
1291        int err = 0;
1292        struct c4iw_qp *qhp;
1293        union t4_recv_wr *wqe = NULL;
1294        u32 num_wrs;
1295        u8 len16 = 0;
1296        unsigned long flag;
1297        u16 idx = 0;
1298
1299        qhp = to_c4iw_qp(ibqp);
1300        spin_lock_irqsave(&qhp->lock, flag);
1301
1302        /*
1303         * If the qp has been flushed, then just insert a special
1304         * drain cqe.
1305         */
1306        if (qhp->wq.flushed) {
1307                spin_unlock_irqrestore(&qhp->lock, flag);
1308                complete_rq_drain_wrs(qhp, wr);
1309                return err;
1310        }
1311        num_wrs = t4_rq_avail(&qhp->wq);
1312        if (num_wrs == 0) {
1313                spin_unlock_irqrestore(&qhp->lock, flag);
1314                *bad_wr = wr;
1315                return -ENOMEM;
1316        }
1317        while (wr) {
1318                if (wr->num_sge > T4_MAX_RECV_SGE) {
1319                        err = -EINVAL;
1320                        *bad_wr = wr;
1321                        break;
1322                }
1323                wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
1324                                           qhp->wq.rq.wq_pidx *
1325                                           T4_EQ_ENTRY_SIZE);
1326                if (num_wrs)
1327                        err = build_rdma_recv(qhp, wqe, wr, &len16);
1328                else
1329                        err = -ENOMEM;
1330                if (err) {
1331                        *bad_wr = wr;
1332                        break;
1333                }
1334
1335                qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
1336                if (c4iw_wr_log) {
1337                        qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].sge_ts =
1338                                cxgb4_read_sge_timestamp(
1339                                                qhp->rhp->rdev.lldi.ports[0]);
1340                        qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].host_time =
1341                                ktime_get();
1342                }
1343
1344                wqe->recv.opcode = FW_RI_RECV_WR;
1345                wqe->recv.r1 = 0;
1346                wqe->recv.wrid = qhp->wq.rq.pidx;
1347                wqe->recv.r2[0] = 0;
1348                wqe->recv.r2[1] = 0;
1349                wqe->recv.r2[2] = 0;
1350                wqe->recv.len16 = len16;
1351                pr_debug("cookie 0x%llx pidx %u\n",
1352                         (unsigned long long)wr->wr_id, qhp->wq.rq.pidx);
1353                t4_rq_produce(&qhp->wq, len16);
1354                idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
1355                wr = wr->next;
1356                num_wrs--;
1357        }
1358        if (!qhp->rhp->rdev.status_page->db_off) {
1359                t4_ring_rq_db(&qhp->wq, idx, wqe);
1360                spin_unlock_irqrestore(&qhp->lock, flag);
1361        } else {
1362                spin_unlock_irqrestore(&qhp->lock, flag);
1363                ring_kernel_rq_db(qhp, idx);
1364        }
1365        return err;
1366}
1367
1368static void defer_srq_wr(struct t4_srq *srq, union t4_recv_wr *wqe,
1369                         u64 wr_id, u8 len16)
1370{
1371        struct t4_srq_pending_wr *pwr = &srq->pending_wrs[srq->pending_pidx];
1372
1373        pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u ooo_count %u wr_id 0x%llx pending_cidx %u pending_pidx %u pending_in_use %u\n",
1374                 __func__, srq->cidx, srq->pidx, srq->wq_pidx,
1375                 srq->in_use, srq->ooo_count,
1376                 (unsigned long long)wr_id, srq->pending_cidx,
1377                 srq->pending_pidx, srq->pending_in_use);
1378        pwr->wr_id = wr_id;
1379        pwr->len16 = len16;
1380        memcpy(&pwr->wqe, wqe, len16 * 16);
1381        t4_srq_produce_pending_wr(srq);
1382}
1383
1384int c4iw_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
1385                       const struct ib_recv_wr **bad_wr)
1386{
1387        union t4_recv_wr *wqe, lwqe;
1388        struct c4iw_srq *srq;
1389        unsigned long flag;
1390        u8 len16 = 0;
1391        u16 idx = 0;
1392        int err = 0;
1393        u32 num_wrs;
1394
1395        srq = to_c4iw_srq(ibsrq);
1396        spin_lock_irqsave(&srq->lock, flag);
1397        num_wrs = t4_srq_avail(&srq->wq);
1398        if (num_wrs == 0) {
1399                spin_unlock_irqrestore(&srq->lock, flag);
1400                return -ENOMEM;
1401        }
1402        while (wr) {
1403                if (wr->num_sge > T4_MAX_RECV_SGE) {
1404                        err = -EINVAL;
1405                        *bad_wr = wr;
1406                        break;
1407                }
1408                wqe = &lwqe;
1409                if (num_wrs)
1410                        err = build_srq_recv(wqe, wr, &len16);
1411                else
1412                        err = -ENOMEM;
1413                if (err) {
1414                        *bad_wr = wr;
1415                        break;
1416                }
1417
1418                wqe->recv.opcode = FW_RI_RECV_WR;
1419                wqe->recv.r1 = 0;
1420                wqe->recv.wrid = srq->wq.pidx;
1421                wqe->recv.r2[0] = 0;
1422                wqe->recv.r2[1] = 0;
1423                wqe->recv.r2[2] = 0;
1424                wqe->recv.len16 = len16;
1425
1426                if (srq->wq.ooo_count ||
1427                    srq->wq.pending_in_use ||
1428                    srq->wq.sw_rq[srq->wq.pidx].valid) {
1429                        defer_srq_wr(&srq->wq, wqe, wr->wr_id, len16);
1430                } else {
1431                        srq->wq.sw_rq[srq->wq.pidx].wr_id = wr->wr_id;
1432                        srq->wq.sw_rq[srq->wq.pidx].valid = 1;
1433                        c4iw_copy_wr_to_srq(&srq->wq, wqe, len16);
1434                        pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u wr_id 0x%llx\n",
1435                                 __func__, srq->wq.cidx,
1436                                 srq->wq.pidx, srq->wq.wq_pidx,
1437                                 srq->wq.in_use,
1438                                 (unsigned long long)wr->wr_id);
1439                        t4_srq_produce(&srq->wq, len16);
1440                        idx += DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
1441                }
1442                wr = wr->next;
1443                num_wrs--;
1444        }
1445        if (idx)
1446                t4_ring_srq_db(&srq->wq, idx, len16, wqe);
1447        spin_unlock_irqrestore(&srq->lock, flag);
1448        return err;
1449}
1450
1451static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
1452                                    u8 *ecode)
1453{
1454        int status;
1455        int tagged;
1456        int opcode;
1457        int rqtype;
1458        int send_inv;
1459
1460        if (!err_cqe) {
1461                *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1462                *ecode = 0;
1463                return;
1464        }
1465
1466        status = CQE_STATUS(err_cqe);
1467        opcode = CQE_OPCODE(err_cqe);
1468        rqtype = RQ_TYPE(err_cqe);
1469        send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
1470                   (opcode == FW_RI_SEND_WITH_SE_INV);
1471        tagged = (opcode == FW_RI_RDMA_WRITE) ||
1472                 (rqtype && (opcode == FW_RI_READ_RESP));
1473
1474        switch (status) {
1475        case T4_ERR_STAG:
1476                if (send_inv) {
1477                        *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1478                        *ecode = RDMAP_CANT_INV_STAG;
1479                } else {
1480                        *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1481                        *ecode = RDMAP_INV_STAG;
1482                }
1483                break;
1484        case T4_ERR_PDID:
1485                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1486                if ((opcode == FW_RI_SEND_WITH_INV) ||
1487                    (opcode == FW_RI_SEND_WITH_SE_INV))
1488                        *ecode = RDMAP_CANT_INV_STAG;
1489                else
1490                        *ecode = RDMAP_STAG_NOT_ASSOC;
1491                break;
1492        case T4_ERR_QPID:
1493                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1494                *ecode = RDMAP_STAG_NOT_ASSOC;
1495                break;
1496        case T4_ERR_ACCESS:
1497                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1498                *ecode = RDMAP_ACC_VIOL;
1499                break;
1500        case T4_ERR_WRAP:
1501                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1502                *ecode = RDMAP_TO_WRAP;
1503                break;
1504        case T4_ERR_BOUND:
1505                if (tagged) {
1506                        *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1507                        *ecode = DDPT_BASE_BOUNDS;
1508                } else {
1509                        *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1510                        *ecode = RDMAP_BASE_BOUNDS;
1511                }
1512                break;
1513        case T4_ERR_INVALIDATE_SHARED_MR:
1514        case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
1515                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1516                *ecode = RDMAP_CANT_INV_STAG;
1517                break;
1518        case T4_ERR_ECC:
1519        case T4_ERR_ECC_PSTAG:
1520        case T4_ERR_INTERNAL_ERR:
1521                *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
1522                *ecode = 0;
1523                break;
1524        case T4_ERR_OUT_OF_RQE:
1525                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1526                *ecode = DDPU_INV_MSN_NOBUF;
1527                break;
1528        case T4_ERR_PBL_ADDR_BOUND:
1529                *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1530                *ecode = DDPT_BASE_BOUNDS;
1531                break;
1532        case T4_ERR_CRC:
1533                *layer_type = LAYER_MPA|DDP_LLP;
1534                *ecode = MPA_CRC_ERR;
1535                break;
1536        case T4_ERR_MARKER:
1537                *layer_type = LAYER_MPA|DDP_LLP;
1538                *ecode = MPA_MARKER_ERR;
1539                break;
1540        case T4_ERR_PDU_LEN_ERR:
1541                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1542                *ecode = DDPU_MSG_TOOBIG;
1543                break;
1544        case T4_ERR_DDP_VERSION:
1545                if (tagged) {
1546                        *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1547                        *ecode = DDPT_INV_VERS;
1548                } else {
1549                        *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1550                        *ecode = DDPU_INV_VERS;
1551                }
1552                break;
1553        case T4_ERR_RDMA_VERSION:
1554                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1555                *ecode = RDMAP_INV_VERS;
1556                break;
1557        case T4_ERR_OPCODE:
1558                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1559                *ecode = RDMAP_INV_OPCODE;
1560                break;
1561        case T4_ERR_DDP_QUEUE_NUM:
1562                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1563                *ecode = DDPU_INV_QN;
1564                break;
1565        case T4_ERR_MSN:
1566        case T4_ERR_MSN_GAP:
1567        case T4_ERR_MSN_RANGE:
1568        case T4_ERR_IRD_OVERFLOW:
1569                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1570                *ecode = DDPU_INV_MSN_RANGE;
1571                break;
1572        case T4_ERR_TBIT:
1573                *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
1574                *ecode = 0;
1575                break;
1576        case T4_ERR_MO:
1577                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1578                *ecode = DDPU_INV_MO;
1579                break;
1580        default:
1581                *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1582                *ecode = 0;
1583                break;
1584        }
1585}
1586
1587static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
1588                           gfp_t gfp)
1589{
1590        struct fw_ri_wr *wqe;
1591        struct sk_buff *skb;
1592        struct terminate_message *term;
1593
1594        pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid,
1595                 qhp->ep->hwtid);
1596
1597        skb = skb_dequeue(&qhp->ep->com.ep_skb_list);
1598        if (WARN_ON(!skb))
1599                return;
1600
1601        set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1602
1603        wqe = __skb_put_zero(skb, sizeof(*wqe));
1604        wqe->op_compl = cpu_to_be32(FW_WR_OP_V(FW_RI_INIT_WR));
1605        wqe->flowid_len16 = cpu_to_be32(
1606                FW_WR_FLOWID_V(qhp->ep->hwtid) |
1607                FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
1608
1609        wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
1610        wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
1611        term = (struct terminate_message *)wqe->u.terminate.termmsg;
1612        if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
1613                term->layer_etype = qhp->attr.layer_etype;
1614                term->ecode = qhp->attr.ecode;
1615        } else
1616                build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
1617        c4iw_ofld_send(&qhp->rhp->rdev, skb);
1618}
1619
1620/*
1621 * Assumes qhp lock is held.
1622 */
1623static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
1624                       struct c4iw_cq *schp)
1625{
1626        int count;
1627        int rq_flushed = 0, sq_flushed;
1628        unsigned long flag;
1629
1630        pr_debug("qhp %p rchp %p schp %p\n", qhp, rchp, schp);
1631
1632        /* locking hierarchy: cqs lock first, then qp lock. */
1633        spin_lock_irqsave(&rchp->lock, flag);
1634        if (schp != rchp)
1635                spin_lock(&schp->lock);
1636        spin_lock(&qhp->lock);
1637
1638        if (qhp->wq.flushed) {
1639                spin_unlock(&qhp->lock);
1640                if (schp != rchp)
1641                        spin_unlock(&schp->lock);
1642                spin_unlock_irqrestore(&rchp->lock, flag);
1643                return;
1644        }
1645        qhp->wq.flushed = 1;
1646        t4_set_wq_in_error(&qhp->wq, 0);
1647
1648        c4iw_flush_hw_cq(rchp, qhp);
1649        if (!qhp->srq) {
1650                c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
1651                rq_flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
1652        }
1653
1654        if (schp != rchp)
1655                c4iw_flush_hw_cq(schp, qhp);
1656        sq_flushed = c4iw_flush_sq(qhp);
1657
1658        spin_unlock(&qhp->lock);
1659        if (schp != rchp)
1660                spin_unlock(&schp->lock);
1661        spin_unlock_irqrestore(&rchp->lock, flag);
1662
1663        if (schp == rchp) {
1664                if ((rq_flushed || sq_flushed) &&
1665                    t4_clear_cq_armed(&rchp->cq)) {
1666                        spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1667                        (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1668                                                   rchp->ibcq.cq_context);
1669                        spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1670                }
1671        } else {
1672                if (rq_flushed && t4_clear_cq_armed(&rchp->cq)) {
1673                        spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1674                        (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1675                                                   rchp->ibcq.cq_context);
1676                        spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1677                }
1678                if (sq_flushed && t4_clear_cq_armed(&schp->cq)) {
1679                        spin_lock_irqsave(&schp->comp_handler_lock, flag);
1680                        (*schp->ibcq.comp_handler)(&schp->ibcq,
1681                                                   schp->ibcq.cq_context);
1682                        spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1683                }
1684        }
1685}
1686
1687static void flush_qp(struct c4iw_qp *qhp)
1688{
1689        struct c4iw_cq *rchp, *schp;
1690        unsigned long flag;
1691
1692        rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1693        schp = to_c4iw_cq(qhp->ibqp.send_cq);
1694
1695        if (qhp->ibqp.uobject) {
1696
1697                /* for user qps, qhp->wq.flushed is protected by qhp->mutex */
1698                if (qhp->wq.flushed)
1699                        return;
1700
1701                qhp->wq.flushed = 1;
1702                t4_set_wq_in_error(&qhp->wq, 0);
1703                t4_set_cq_in_error(&rchp->cq);
1704                spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1705                (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
1706                spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1707                if (schp != rchp) {
1708                        t4_set_cq_in_error(&schp->cq);
1709                        spin_lock_irqsave(&schp->comp_handler_lock, flag);
1710                        (*schp->ibcq.comp_handler)(&schp->ibcq,
1711                                        schp->ibcq.cq_context);
1712                        spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1713                }
1714                return;
1715        }
1716        __flush_qp(qhp, rchp, schp);
1717}
1718
1719static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1720                     struct c4iw_ep *ep)
1721{
1722        struct fw_ri_wr *wqe;
1723        int ret;
1724        struct sk_buff *skb;
1725
1726        pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid, ep->hwtid);
1727
1728        skb = skb_dequeue(&ep->com.ep_skb_list);
1729        if (WARN_ON(!skb))
1730                return -ENOMEM;
1731
1732        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1733
1734        wqe = __skb_put_zero(skb, sizeof(*wqe));
1735        wqe->op_compl = cpu_to_be32(
1736                FW_WR_OP_V(FW_RI_INIT_WR) |
1737                FW_WR_COMPL_F);
1738        wqe->flowid_len16 = cpu_to_be32(
1739                FW_WR_FLOWID_V(ep->hwtid) |
1740                FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
1741        wqe->cookie = (uintptr_t)ep->com.wr_waitp;
1742
1743        wqe->u.fini.type = FW_RI_TYPE_FINI;
1744
1745        ret = c4iw_ref_send_wait(&rhp->rdev, skb, ep->com.wr_waitp,
1746                                 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
1747
1748        pr_debug("ret %d\n", ret);
1749        return ret;
1750}
1751
1752static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
1753{
1754        pr_debug("p2p_type = %d\n", p2p_type);
1755        memset(&init->u, 0, sizeof init->u);
1756        switch (p2p_type) {
1757        case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
1758                init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
1759                init->u.write.stag_sink = cpu_to_be32(1);
1760                init->u.write.to_sink = cpu_to_be64(1);
1761                init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
1762                init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
1763                                                   sizeof(struct fw_ri_immd),
1764                                                   16);
1765                break;
1766        case FW_RI_INIT_P2PTYPE_READ_REQ:
1767                init->u.write.opcode = FW_RI_RDMA_READ_WR;
1768                init->u.read.stag_src = cpu_to_be32(1);
1769                init->u.read.to_src_lo = cpu_to_be32(1);
1770                init->u.read.stag_sink = cpu_to_be32(1);
1771                init->u.read.to_sink_lo = cpu_to_be32(1);
1772                init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
1773                break;
1774        }
1775}
1776
1777static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
1778{
1779        struct fw_ri_wr *wqe;
1780        int ret;
1781        struct sk_buff *skb;
1782
1783        pr_debug("qhp %p qid 0x%x tid %u ird %u ord %u\n", qhp,
1784                 qhp->wq.sq.qid, qhp->ep->hwtid, qhp->ep->ird, qhp->ep->ord);
1785
1786        skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
1787        if (!skb) {
1788                ret = -ENOMEM;
1789                goto out;
1790        }
1791        ret = alloc_ird(rhp, qhp->attr.max_ird);
1792        if (ret) {
1793                qhp->attr.max_ird = 0;
1794                kfree_skb(skb);
1795                goto out;
1796        }
1797        set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1798
1799        wqe = __skb_put_zero(skb, sizeof(*wqe));
1800        wqe->op_compl = cpu_to_be32(
1801                FW_WR_OP_V(FW_RI_INIT_WR) |
1802                FW_WR_COMPL_F);
1803        wqe->flowid_len16 = cpu_to_be32(
1804                FW_WR_FLOWID_V(qhp->ep->hwtid) |
1805                FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
1806
1807        wqe->cookie = (uintptr_t)qhp->ep->com.wr_waitp;
1808
1809        wqe->u.init.type = FW_RI_TYPE_INIT;
1810        wqe->u.init.mpareqbit_p2ptype =
1811                FW_RI_WR_MPAREQBIT_V(qhp->attr.mpa_attr.initiator) |
1812                FW_RI_WR_P2PTYPE_V(qhp->attr.mpa_attr.p2p_type);
1813        wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
1814        if (qhp->attr.mpa_attr.recv_marker_enabled)
1815                wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
1816        if (qhp->attr.mpa_attr.xmit_marker_enabled)
1817                wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
1818        if (qhp->attr.mpa_attr.crc_enabled)
1819                wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
1820
1821        wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
1822                            FW_RI_QP_RDMA_WRITE_ENABLE |
1823                            FW_RI_QP_BIND_ENABLE;
1824        if (!qhp->ibqp.uobject)
1825                wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
1826                                     FW_RI_QP_STAG0_ENABLE;
1827        wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
1828        wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
1829        wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
1830        wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
1831        if (qhp->srq) {
1832                wqe->u.init.rq_eqid = cpu_to_be32(FW_RI_INIT_RQEQID_SRQ |
1833                                                  qhp->srq->idx);
1834        } else {
1835                wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
1836                wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
1837                wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
1838                                                   rhp->rdev.lldi.vr->rq.start);
1839        }
1840        wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
1841        wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
1842        wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
1843        wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
1844        wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
1845        wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
1846        if (qhp->attr.mpa_attr.initiator)
1847                build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
1848
1849        ret = c4iw_ref_send_wait(&rhp->rdev, skb, qhp->ep->com.wr_waitp,
1850                                 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
1851        if (!ret)
1852                goto out;
1853
1854        free_ird(rhp, qhp->attr.max_ird);
1855out:
1856        pr_debug("ret %d\n", ret);
1857        return ret;
1858}
1859
1860int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1861                   enum c4iw_qp_attr_mask mask,
1862                   struct c4iw_qp_attributes *attrs,
1863                   int internal)
1864{
1865        int ret = 0;
1866        struct c4iw_qp_attributes newattr = qhp->attr;
1867        int disconnect = 0;
1868        int terminate = 0;
1869        int abort = 0;
1870        int free = 0;
1871        struct c4iw_ep *ep = NULL;
1872
1873        pr_debug("qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n",
1874                 qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
1875                 (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
1876
1877        mutex_lock(&qhp->mutex);
1878
1879        /* Process attr changes if in IDLE */
1880        if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
1881                if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
1882                        ret = -EIO;
1883                        goto out;
1884                }
1885                if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
1886                        newattr.enable_rdma_read = attrs->enable_rdma_read;
1887                if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
1888                        newattr.enable_rdma_write = attrs->enable_rdma_write;
1889                if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
1890                        newattr.enable_bind = attrs->enable_bind;
1891                if (mask & C4IW_QP_ATTR_MAX_ORD) {
1892                        if (attrs->max_ord > c4iw_max_read_depth) {
1893                                ret = -EINVAL;
1894                                goto out;
1895                        }
1896                        newattr.max_ord = attrs->max_ord;
1897                }
1898                if (mask & C4IW_QP_ATTR_MAX_IRD) {
1899                        if (attrs->max_ird > cur_max_read_depth(rhp)) {
1900                                ret = -EINVAL;
1901                                goto out;
1902                        }
1903                        newattr.max_ird = attrs->max_ird;
1904                }
1905                qhp->attr = newattr;
1906        }
1907
1908        if (mask & C4IW_QP_ATTR_SQ_DB) {
1909                ret = ring_kernel_sq_db(qhp, attrs->sq_db_inc);
1910                goto out;
1911        }
1912        if (mask & C4IW_QP_ATTR_RQ_DB) {
1913                ret = ring_kernel_rq_db(qhp, attrs->rq_db_inc);
1914                goto out;
1915        }
1916
1917        if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
1918                goto out;
1919        if (qhp->attr.state == attrs->next_state)
1920                goto out;
1921
1922        switch (qhp->attr.state) {
1923        case C4IW_QP_STATE_IDLE:
1924                switch (attrs->next_state) {
1925                case C4IW_QP_STATE_RTS:
1926                        if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
1927                                ret = -EINVAL;
1928                                goto out;
1929                        }
1930                        if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
1931                                ret = -EINVAL;
1932                                goto out;
1933                        }
1934                        qhp->attr.mpa_attr = attrs->mpa_attr;
1935                        qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
1936                        qhp->ep = qhp->attr.llp_stream_handle;
1937                        set_state(qhp, C4IW_QP_STATE_RTS);
1938
1939                        /*
1940                         * Ref the endpoint here and deref when we
1941                         * disassociate the endpoint from the QP.  This
1942                         * happens in CLOSING->IDLE transition or *->ERROR
1943                         * transition.
1944                         */
1945                        c4iw_get_ep(&qhp->ep->com);
1946                        ret = rdma_init(rhp, qhp);
1947                        if (ret)
1948                                goto err;
1949                        break;
1950                case C4IW_QP_STATE_ERROR:
1951                        set_state(qhp, C4IW_QP_STATE_ERROR);
1952                        flush_qp(qhp);
1953                        break;
1954                default:
1955                        ret = -EINVAL;
1956                        goto out;
1957                }
1958                break;
1959        case C4IW_QP_STATE_RTS:
1960                switch (attrs->next_state) {
1961                case C4IW_QP_STATE_CLOSING:
1962                        t4_set_wq_in_error(&qhp->wq, 0);
1963                        set_state(qhp, C4IW_QP_STATE_CLOSING);
1964                        ep = qhp->ep;
1965                        if (!internal) {
1966                                abort = 0;
1967                                disconnect = 1;
1968                                c4iw_get_ep(&qhp->ep->com);
1969                        }
1970                        ret = rdma_fini(rhp, qhp, ep);
1971                        if (ret)
1972                                goto err;
1973                        break;
1974                case C4IW_QP_STATE_TERMINATE:
1975                        t4_set_wq_in_error(&qhp->wq, 0);
1976                        set_state(qhp, C4IW_QP_STATE_TERMINATE);
1977                        qhp->attr.layer_etype = attrs->layer_etype;
1978                        qhp->attr.ecode = attrs->ecode;
1979                        ep = qhp->ep;
1980                        c4iw_get_ep(&ep->com);
1981                        disconnect = 1;
1982                        if (!internal) {
1983                                terminate = 1;
1984                        } else {
1985                                terminate = qhp->attr.send_term;
1986                                ret = rdma_fini(rhp, qhp, ep);
1987                                if (ret)
1988                                        goto err;
1989                        }
1990                        break;
1991                case C4IW_QP_STATE_ERROR:
1992                        t4_set_wq_in_error(&qhp->wq, 0);
1993                        set_state(qhp, C4IW_QP_STATE_ERROR);
1994                        if (!internal) {
1995                                abort = 1;
1996                                disconnect = 1;
1997                                ep = qhp->ep;
1998                                c4iw_get_ep(&qhp->ep->com);
1999                        }
2000                        goto err;
2001                        break;
2002                default:
2003                        ret = -EINVAL;
2004                        goto out;
2005                }
2006                break;
2007        case C4IW_QP_STATE_CLOSING:
2008
2009                /*
2010                 * Allow kernel users to move to ERROR for qp draining.
2011                 */
2012                if (!internal && (qhp->ibqp.uobject || attrs->next_state !=
2013                                  C4IW_QP_STATE_ERROR)) {
2014                        ret = -EINVAL;
2015                        goto out;
2016                }
2017                switch (attrs->next_state) {
2018                case C4IW_QP_STATE_IDLE:
2019                        flush_qp(qhp);
2020                        set_state(qhp, C4IW_QP_STATE_IDLE);
2021                        qhp->attr.llp_stream_handle = NULL;
2022                        c4iw_put_ep(&qhp->ep->com);
2023                        qhp->ep = NULL;
2024                        wake_up(&qhp->wait);
2025                        break;
2026                case C4IW_QP_STATE_ERROR:
2027                        goto err;
2028                default:
2029                        ret = -EINVAL;
2030                        goto err;
2031                }
2032                break;
2033        case C4IW_QP_STATE_ERROR:
2034                if (attrs->next_state != C4IW_QP_STATE_IDLE) {
2035                        ret = -EINVAL;
2036                        goto out;
2037                }
2038                if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
2039                        ret = -EINVAL;
2040                        goto out;
2041                }
2042                set_state(qhp, C4IW_QP_STATE_IDLE);
2043                break;
2044        case C4IW_QP_STATE_TERMINATE:
2045                if (!internal) {
2046                        ret = -EINVAL;
2047                        goto out;
2048                }
2049                goto err;
2050                break;
2051        default:
2052                pr_err("%s in a bad state %d\n", __func__, qhp->attr.state);
2053                ret = -EINVAL;
2054                goto err;
2055                break;
2056        }
2057        goto out;
2058err:
2059        pr_debug("disassociating ep %p qpid 0x%x\n", qhp->ep,
2060                 qhp->wq.sq.qid);
2061
2062        /* disassociate the LLP connection */
2063        qhp->attr.llp_stream_handle = NULL;
2064        if (!ep)
2065                ep = qhp->ep;
2066        qhp->ep = NULL;
2067        set_state(qhp, C4IW_QP_STATE_ERROR);
2068        free = 1;
2069        abort = 1;
2070        flush_qp(qhp);
2071        wake_up(&qhp->wait);
2072out:
2073        mutex_unlock(&qhp->mutex);
2074
2075        if (terminate)
2076                post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
2077
2078        /*
2079         * If disconnect is 1, then we need to initiate a disconnect
2080         * on the EP.  This can be a normal close (RTS->CLOSING) or
2081         * an abnormal close (RTS/CLOSING->ERROR).
2082         */
2083        if (disconnect) {
2084                c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
2085                                                         GFP_KERNEL);
2086                c4iw_put_ep(&ep->com);
2087        }
2088
2089        /*
2090         * If free is 1, then we've disassociated the EP from the QP
2091         * and we need to dereference the EP.
2092         */
2093        if (free)
2094                c4iw_put_ep(&ep->com);
2095        pr_debug("exit state %d\n", qhp->attr.state);
2096        return ret;
2097}
2098
2099int c4iw_destroy_qp(struct ib_qp *ib_qp)
2100{
2101        struct c4iw_dev *rhp;
2102        struct c4iw_qp *qhp;
2103        struct c4iw_qp_attributes attrs;
2104
2105        qhp = to_c4iw_qp(ib_qp);
2106        rhp = qhp->rhp;
2107
2108        attrs.next_state = C4IW_QP_STATE_ERROR;
2109        if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
2110                c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2111        else
2112                c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
2113        wait_event(qhp->wait, !qhp->ep);
2114
2115        remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
2116
2117        spin_lock_irq(&rhp->lock);
2118        if (!list_empty(&qhp->db_fc_entry))
2119                list_del_init(&qhp->db_fc_entry);
2120        spin_unlock_irq(&rhp->lock);
2121        free_ird(rhp, qhp->attr.max_ird);
2122
2123        c4iw_qp_rem_ref(ib_qp);
2124
2125        pr_debug("ib_qp %p qpid 0x%0x\n", ib_qp, qhp->wq.sq.qid);
2126        return 0;
2127}
2128
2129struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
2130                             struct ib_udata *udata)
2131{
2132        struct c4iw_dev *rhp;
2133        struct c4iw_qp *qhp;
2134        struct c4iw_pd *php;
2135        struct c4iw_cq *schp;
2136        struct c4iw_cq *rchp;
2137        struct c4iw_create_qp_resp uresp;
2138        unsigned int sqsize, rqsize = 0;
2139        struct c4iw_ucontext *ucontext;
2140        int ret;
2141        struct c4iw_mm_entry *sq_key_mm, *rq_key_mm = NULL, *sq_db_key_mm;
2142        struct c4iw_mm_entry *rq_db_key_mm = NULL, *ma_sync_key_mm = NULL;
2143
2144        pr_debug("ib_pd %p\n", pd);
2145
2146        if (attrs->qp_type != IB_QPT_RC)
2147                return ERR_PTR(-EINVAL);
2148
2149        php = to_c4iw_pd(pd);
2150        rhp = php->rhp;
2151        schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
2152        rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
2153        if (!schp || !rchp)
2154                return ERR_PTR(-EINVAL);
2155
2156        if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
2157                return ERR_PTR(-EINVAL);
2158
2159        if (!attrs->srq) {
2160                if (attrs->cap.max_recv_wr > rhp->rdev.hw_queue.t4_max_rq_size)
2161                        return ERR_PTR(-E2BIG);
2162                rqsize = attrs->cap.max_recv_wr + 1;
2163                if (rqsize < 8)
2164                        rqsize = 8;
2165        }
2166
2167        if (attrs->cap.max_send_wr > rhp->rdev.hw_queue.t4_max_sq_size)
2168                return ERR_PTR(-E2BIG);
2169        sqsize = attrs->cap.max_send_wr + 1;
2170        if (sqsize < 8)
2171                sqsize = 8;
2172
2173        ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
2174
2175        qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
2176        if (!qhp)
2177                return ERR_PTR(-ENOMEM);
2178
2179        qhp->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
2180        if (!qhp->wr_waitp) {
2181                ret = -ENOMEM;
2182                goto err_free_qhp;
2183        }
2184
2185        qhp->wq.sq.size = sqsize;
2186        qhp->wq.sq.memsize =
2187                (sqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2188                sizeof(*qhp->wq.sq.queue) + 16 * sizeof(__be64);
2189        qhp->wq.sq.flush_cidx = -1;
2190        if (!attrs->srq) {
2191                qhp->wq.rq.size = rqsize;
2192                qhp->wq.rq.memsize =
2193                        (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2194                        sizeof(*qhp->wq.rq.queue);
2195        }
2196
2197        if (ucontext) {
2198                qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
2199                if (!attrs->srq)
2200                        qhp->wq.rq.memsize =
2201                                roundup(qhp->wq.rq.memsize, PAGE_SIZE);
2202        }
2203
2204        ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
2205                        ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2206                        qhp->wr_waitp, !attrs->srq);
2207        if (ret)
2208                goto err_free_wr_wait;
2209
2210        attrs->cap.max_recv_wr = rqsize - 1;
2211        attrs->cap.max_send_wr = sqsize - 1;
2212        attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
2213
2214        qhp->rhp = rhp;
2215        qhp->attr.pd = php->pdid;
2216        qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
2217        qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
2218        qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
2219        qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
2220        qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
2221        if (!attrs->srq) {
2222                qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
2223                qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
2224        }
2225        qhp->attr.state = C4IW_QP_STATE_IDLE;
2226        qhp->attr.next_state = C4IW_QP_STATE_IDLE;
2227        qhp->attr.enable_rdma_read = 1;
2228        qhp->attr.enable_rdma_write = 1;
2229        qhp->attr.enable_bind = 1;
2230        qhp->attr.max_ord = 0;
2231        qhp->attr.max_ird = 0;
2232        qhp->sq_sig_all = attrs->sq_sig_type == IB_SIGNAL_ALL_WR;
2233        spin_lock_init(&qhp->lock);
2234        mutex_init(&qhp->mutex);
2235        init_waitqueue_head(&qhp->wait);
2236        kref_init(&qhp->kref);
2237        INIT_WORK(&qhp->free_work, free_qp_work);
2238
2239        ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
2240        if (ret)
2241                goto err_destroy_qp;
2242
2243        if (udata && ucontext) {
2244                sq_key_mm = kmalloc(sizeof(*sq_key_mm), GFP_KERNEL);
2245                if (!sq_key_mm) {
2246                        ret = -ENOMEM;
2247                        goto err_remove_handle;
2248                }
2249                if (!attrs->srq) {
2250                        rq_key_mm = kmalloc(sizeof(*rq_key_mm), GFP_KERNEL);
2251                        if (!rq_key_mm) {
2252                                ret = -ENOMEM;
2253                                goto err_free_sq_key;
2254                        }
2255                }
2256                sq_db_key_mm = kmalloc(sizeof(*sq_db_key_mm), GFP_KERNEL);
2257                if (!sq_db_key_mm) {
2258                        ret = -ENOMEM;
2259                        goto err_free_rq_key;
2260                }
2261                if (!attrs->srq) {
2262                        rq_db_key_mm =
2263                                kmalloc(sizeof(*rq_db_key_mm), GFP_KERNEL);
2264                        if (!rq_db_key_mm) {
2265                                ret = -ENOMEM;
2266                                goto err_free_sq_db_key;
2267                        }
2268                }
2269                memset(&uresp, 0, sizeof(uresp));
2270                if (t4_sq_onchip(&qhp->wq.sq)) {
2271                        ma_sync_key_mm = kmalloc(sizeof(*ma_sync_key_mm),
2272                                                 GFP_KERNEL);
2273                        if (!ma_sync_key_mm) {
2274                                ret = -ENOMEM;
2275                                goto err_free_rq_db_key;
2276                        }
2277                        uresp.flags = C4IW_QPF_ONCHIP;
2278                }
2279                if (rhp->rdev.lldi.write_w_imm_support)
2280                        uresp.flags |= C4IW_QPF_WRITE_W_IMM;
2281                uresp.qid_mask = rhp->rdev.qpmask;
2282                uresp.sqid = qhp->wq.sq.qid;
2283                uresp.sq_size = qhp->wq.sq.size;
2284                uresp.sq_memsize = qhp->wq.sq.memsize;
2285                if (!attrs->srq) {
2286                        uresp.rqid = qhp->wq.rq.qid;
2287                        uresp.rq_size = qhp->wq.rq.size;
2288                        uresp.rq_memsize = qhp->wq.rq.memsize;
2289                }
2290                spin_lock(&ucontext->mmap_lock);
2291                if (ma_sync_key_mm) {
2292                        uresp.ma_sync_key = ucontext->key;
2293                        ucontext->key += PAGE_SIZE;
2294                }
2295                uresp.sq_key = ucontext->key;
2296                ucontext->key += PAGE_SIZE;
2297                if (!attrs->srq) {
2298                        uresp.rq_key = ucontext->key;
2299                        ucontext->key += PAGE_SIZE;
2300                }
2301                uresp.sq_db_gts_key = ucontext->key;
2302                ucontext->key += PAGE_SIZE;
2303                if (!attrs->srq) {
2304                        uresp.rq_db_gts_key = ucontext->key;
2305                        ucontext->key += PAGE_SIZE;
2306                }
2307                spin_unlock(&ucontext->mmap_lock);
2308                ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
2309                if (ret)
2310                        goto err_free_ma_sync_key;
2311                sq_key_mm->key = uresp.sq_key;
2312                sq_key_mm->addr = qhp->wq.sq.phys_addr;
2313                sq_key_mm->len = PAGE_ALIGN(qhp->wq.sq.memsize);
2314                insert_mmap(ucontext, sq_key_mm);
2315                if (!attrs->srq) {
2316                        rq_key_mm->key = uresp.rq_key;
2317                        rq_key_mm->addr = virt_to_phys(qhp->wq.rq.queue);
2318                        rq_key_mm->len = PAGE_ALIGN(qhp->wq.rq.memsize);
2319                        insert_mmap(ucontext, rq_key_mm);
2320                }
2321                sq_db_key_mm->key = uresp.sq_db_gts_key;
2322                sq_db_key_mm->addr = (u64)(unsigned long)qhp->wq.sq.bar2_pa;
2323                sq_db_key_mm->len = PAGE_SIZE;
2324                insert_mmap(ucontext, sq_db_key_mm);
2325                if (!attrs->srq) {
2326                        rq_db_key_mm->key = uresp.rq_db_gts_key;
2327                        rq_db_key_mm->addr =
2328                                (u64)(unsigned long)qhp->wq.rq.bar2_pa;
2329                        rq_db_key_mm->len = PAGE_SIZE;
2330                        insert_mmap(ucontext, rq_db_key_mm);
2331                }
2332                if (ma_sync_key_mm) {
2333                        ma_sync_key_mm->key = uresp.ma_sync_key;
2334                        ma_sync_key_mm->addr =
2335                                (pci_resource_start(rhp->rdev.lldi.pdev, 0) +
2336                                PCIE_MA_SYNC_A) & PAGE_MASK;
2337                        ma_sync_key_mm->len = PAGE_SIZE;
2338                        insert_mmap(ucontext, ma_sync_key_mm);
2339                }
2340
2341                c4iw_get_ucontext(ucontext);
2342                qhp->ucontext = ucontext;
2343        }
2344        if (!attrs->srq) {
2345                qhp->wq.qp_errp =
2346                        &qhp->wq.rq.queue[qhp->wq.rq.size].status.qp_err;
2347        } else {
2348                qhp->wq.qp_errp =
2349                        &qhp->wq.sq.queue[qhp->wq.sq.size].status.qp_err;
2350                qhp->wq.srqidxp =
2351                        &qhp->wq.sq.queue[qhp->wq.sq.size].status.srqidx;
2352        }
2353
2354        qhp->ibqp.qp_num = qhp->wq.sq.qid;
2355        if (attrs->srq)
2356                qhp->srq = to_c4iw_srq(attrs->srq);
2357        INIT_LIST_HEAD(&qhp->db_fc_entry);
2358        pr_debug("sq id %u size %u memsize %zu num_entries %u rq id %u size %u memsize %zu num_entries %u\n",
2359                 qhp->wq.sq.qid, qhp->wq.sq.size, qhp->wq.sq.memsize,
2360                 attrs->cap.max_send_wr, qhp->wq.rq.qid, qhp->wq.rq.size,
2361                 qhp->wq.rq.memsize, attrs->cap.max_recv_wr);
2362        return &qhp->ibqp;
2363err_free_ma_sync_key:
2364        kfree(ma_sync_key_mm);
2365err_free_rq_db_key:
2366        if (!attrs->srq)
2367                kfree(rq_db_key_mm);
2368err_free_sq_db_key:
2369        kfree(sq_db_key_mm);
2370err_free_rq_key:
2371        if (!attrs->srq)
2372                kfree(rq_key_mm);
2373err_free_sq_key:
2374        kfree(sq_key_mm);
2375err_remove_handle:
2376        remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
2377err_destroy_qp:
2378        destroy_qp(&rhp->rdev, &qhp->wq,
2379                   ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !attrs->srq);
2380err_free_wr_wait:
2381        c4iw_put_wr_wait(qhp->wr_waitp);
2382err_free_qhp:
2383        kfree(qhp);
2384        return ERR_PTR(ret);
2385}
2386
2387int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2388                      int attr_mask, struct ib_udata *udata)
2389{
2390        struct c4iw_dev *rhp;
2391        struct c4iw_qp *qhp;
2392        enum c4iw_qp_attr_mask mask = 0;
2393        struct c4iw_qp_attributes attrs;
2394
2395        pr_debug("ib_qp %p\n", ibqp);
2396
2397        /* iwarp does not support the RTR state */
2398        if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
2399                attr_mask &= ~IB_QP_STATE;
2400
2401        /* Make sure we still have something left to do */
2402        if (!attr_mask)
2403                return 0;
2404
2405        memset(&attrs, 0, sizeof attrs);
2406        qhp = to_c4iw_qp(ibqp);
2407        rhp = qhp->rhp;
2408
2409        attrs.next_state = c4iw_convert_state(attr->qp_state);
2410        attrs.enable_rdma_read = (attr->qp_access_flags &
2411                               IB_ACCESS_REMOTE_READ) ?  1 : 0;
2412        attrs.enable_rdma_write = (attr->qp_access_flags &
2413                                IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
2414        attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
2415
2416
2417        mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
2418        mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
2419                        (C4IW_QP_ATTR_ENABLE_RDMA_READ |
2420                         C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
2421                         C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
2422
2423        /*
2424         * Use SQ_PSN and RQ_PSN to pass in IDX_INC values for
2425         * ringing the queue db when we're in DB_FULL mode.
2426         * Only allow this on T4 devices.
2427         */
2428        attrs.sq_db_inc = attr->sq_psn;
2429        attrs.rq_db_inc = attr->rq_psn;
2430        mask |= (attr_mask & IB_QP_SQ_PSN) ? C4IW_QP_ATTR_SQ_DB : 0;
2431        mask |= (attr_mask & IB_QP_RQ_PSN) ? C4IW_QP_ATTR_RQ_DB : 0;
2432        if (!is_t4(to_c4iw_qp(ibqp)->rhp->rdev.lldi.adapter_type) &&
2433            (mask & (C4IW_QP_ATTR_SQ_DB|C4IW_QP_ATTR_RQ_DB)))
2434                return -EINVAL;
2435
2436        return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
2437}
2438
2439struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
2440{
2441        pr_debug("ib_dev %p qpn 0x%x\n", dev, qpn);
2442        return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
2443}
2444
2445void c4iw_dispatch_srq_limit_reached_event(struct c4iw_srq *srq)
2446{
2447        struct ib_event event = {};
2448
2449        event.device = &srq->rhp->ibdev;
2450        event.element.srq = &srq->ibsrq;
2451        event.event = IB_EVENT_SRQ_LIMIT_REACHED;
2452        ib_dispatch_event(&event);
2453}
2454
2455int c4iw_modify_srq(struct ib_srq *ib_srq, struct ib_srq_attr *attr,
2456                    enum ib_srq_attr_mask srq_attr_mask,
2457                    struct ib_udata *udata)
2458{
2459        struct c4iw_srq *srq = to_c4iw_srq(ib_srq);
2460        int ret = 0;
2461
2462        /*
2463         * XXX 0 mask == a SW interrupt for srq_limit reached...
2464         */
2465        if (udata && !srq_attr_mask) {
2466                c4iw_dispatch_srq_limit_reached_event(srq);
2467                goto out;
2468        }
2469
2470        /* no support for this yet */
2471        if (srq_attr_mask & IB_SRQ_MAX_WR) {
2472                ret = -EINVAL;
2473                goto out;
2474        }
2475
2476        if (!udata && (srq_attr_mask & IB_SRQ_LIMIT)) {
2477                srq->armed = true;
2478                srq->srq_limit = attr->srq_limit;
2479        }
2480out:
2481        return ret;
2482}
2483
2484int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2485                     int attr_mask, struct ib_qp_init_attr *init_attr)
2486{
2487        struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
2488
2489        memset(attr, 0, sizeof *attr);
2490        memset(init_attr, 0, sizeof *init_attr);
2491        attr->qp_state = to_ib_qp_state(qhp->attr.state);
2492        init_attr->cap.max_send_wr = qhp->attr.sq_num_entries;
2493        init_attr->cap.max_recv_wr = qhp->attr.rq_num_entries;
2494        init_attr->cap.max_send_sge = qhp->attr.sq_max_sges;
2495        init_attr->cap.max_recv_sge = qhp->attr.sq_max_sges;
2496        init_attr->cap.max_inline_data = T4_MAX_SEND_INLINE;
2497        init_attr->sq_sig_type = qhp->sq_sig_all ? IB_SIGNAL_ALL_WR : 0;
2498        return 0;
2499}
2500
2501static void free_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
2502                           struct c4iw_wr_wait *wr_waitp)
2503{
2504        struct c4iw_rdev *rdev = &srq->rhp->rdev;
2505        struct sk_buff *skb = srq->destroy_skb;
2506        struct t4_srq *wq = &srq->wq;
2507        struct fw_ri_res_wr *res_wr;
2508        struct fw_ri_res *res;
2509        int wr_len;
2510
2511        wr_len = sizeof(*res_wr) + sizeof(*res);
2512        set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
2513
2514        res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
2515        memset(res_wr, 0, wr_len);
2516        res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
2517                        FW_RI_RES_WR_NRES_V(1) |
2518                        FW_WR_COMPL_F);
2519        res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
2520        res_wr->cookie = (uintptr_t)wr_waitp;
2521        res = res_wr->res;
2522        res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
2523        res->u.srq.op = FW_RI_RES_OP_RESET;
2524        res->u.srq.srqid = cpu_to_be32(srq->idx);
2525        res->u.srq.eqid = cpu_to_be32(wq->qid);
2526
2527        c4iw_init_wr_wait(wr_waitp);
2528        c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, 0, __func__);
2529
2530        dma_free_coherent(&rdev->lldi.pdev->dev,
2531                          wq->memsize, wq->queue,
2532                        dma_unmap_addr(wq, mapping));
2533        c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
2534        kfree(wq->sw_rq);
2535        c4iw_put_qpid(rdev, wq->qid, uctx);
2536}
2537
2538static int alloc_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
2539                           struct c4iw_wr_wait *wr_waitp)
2540{
2541        struct c4iw_rdev *rdev = &srq->rhp->rdev;
2542        int user = (uctx != &rdev->uctx);
2543        struct t4_srq *wq = &srq->wq;
2544        struct fw_ri_res_wr *res_wr;
2545        struct fw_ri_res *res;
2546        struct sk_buff *skb;
2547        int wr_len;
2548        int eqsize;
2549        int ret = -ENOMEM;
2550
2551        wq->qid = c4iw_get_qpid(rdev, uctx);
2552        if (!wq->qid)
2553                goto err;
2554
2555        if (!user) {
2556                wq->sw_rq = kcalloc(wq->size, sizeof(*wq->sw_rq),
2557                                    GFP_KERNEL);
2558                if (!wq->sw_rq)
2559                        goto err_put_qpid;
2560                wq->pending_wrs = kcalloc(srq->wq.size,
2561                                          sizeof(*srq->wq.pending_wrs),
2562                                          GFP_KERNEL);
2563                if (!wq->pending_wrs)
2564                        goto err_free_sw_rq;
2565        }
2566
2567        wq->rqt_size = wq->size;
2568        wq->rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rqt_size);
2569        if (!wq->rqt_hwaddr)
2570                goto err_free_pending_wrs;
2571        wq->rqt_abs_idx = (wq->rqt_hwaddr - rdev->lldi.vr->rq.start) >>
2572                T4_RQT_ENTRY_SHIFT;
2573
2574        wq->queue = dma_zalloc_coherent(&rdev->lldi.pdev->dev,
2575                                       wq->memsize, &wq->dma_addr,
2576                        GFP_KERNEL);
2577        if (!wq->queue)
2578                goto err_free_rqtpool;
2579
2580        dma_unmap_addr_set(wq, mapping, wq->dma_addr);
2581
2582        wq->bar2_va = c4iw_bar2_addrs(rdev, wq->qid, CXGB4_BAR2_QTYPE_EGRESS,
2583                                      &wq->bar2_qid,
2584                        user ? &wq->bar2_pa : NULL);
2585
2586        /*
2587         * User mode must have bar2 access.
2588         */
2589
2590        if (user && !wq->bar2_va) {
2591                pr_warn(MOD "%s: srqid %u not in BAR2 range.\n",
2592                        pci_name(rdev->lldi.pdev), wq->qid);
2593                ret = -EINVAL;
2594                goto err_free_queue;
2595        }
2596
2597        /* build fw_ri_res_wr */
2598        wr_len = sizeof(*res_wr) + sizeof(*res);
2599
2600        skb = alloc_skb(wr_len, GFP_KERNEL);
2601        if (!skb)
2602                goto err_free_queue;
2603        set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
2604
2605        res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
2606        memset(res_wr, 0, wr_len);
2607        res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
2608                        FW_RI_RES_WR_NRES_V(1) |
2609                        FW_WR_COMPL_F);
2610        res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
2611        res_wr->cookie = (uintptr_t)wr_waitp;
2612        res = res_wr->res;
2613        res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
2614        res->u.srq.op = FW_RI_RES_OP_WRITE;
2615
2616        /*
2617         * eqsize is the number of 64B entries plus the status page size.
2618         */
2619        eqsize = wq->size * T4_RQ_NUM_SLOTS +
2620                rdev->hw_queue.t4_eq_status_entries;
2621        res->u.srq.eqid = cpu_to_be32(wq->qid);
2622        res->u.srq.fetchszm_to_iqid =
2623                                                /* no host cidx updates */
2624                cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
2625                FW_RI_RES_WR_CPRIO_V(0) |       /* don't keep in chip cache */
2626                FW_RI_RES_WR_PCIECHN_V(0) |     /* set by uP at ri_init time */
2627                FW_RI_RES_WR_FETCHRO_V(0));     /* relaxed_ordering */
2628        res->u.srq.dcaen_to_eqsize =
2629                cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
2630                FW_RI_RES_WR_DCACPU_V(0) |
2631                FW_RI_RES_WR_FBMIN_V(2) |
2632                FW_RI_RES_WR_FBMAX_V(3) |
2633                FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
2634                FW_RI_RES_WR_CIDXFTHRESH_V(0) |
2635                FW_RI_RES_WR_EQSIZE_V(eqsize));
2636        res->u.srq.eqaddr = cpu_to_be64(wq->dma_addr);
2637        res->u.srq.srqid = cpu_to_be32(srq->idx);
2638        res->u.srq.pdid = cpu_to_be32(srq->pdid);
2639        res->u.srq.hwsrqsize = cpu_to_be32(wq->rqt_size);
2640        res->u.srq.hwsrqaddr = cpu_to_be32(wq->rqt_hwaddr -
2641                        rdev->lldi.vr->rq.start);
2642
2643        c4iw_init_wr_wait(wr_waitp);
2644
2645        ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->qid, __func__);
2646        if (ret)
2647                goto err_free_queue;
2648
2649        pr_debug("%s srq %u eqid %u pdid %u queue va %p pa 0x%llx\n"
2650                        " bar2_addr %p rqt addr 0x%x size %d\n",
2651                        __func__, srq->idx, wq->qid, srq->pdid, wq->queue,
2652                        (u64)virt_to_phys(wq->queue), wq->bar2_va,
2653                        wq->rqt_hwaddr, wq->rqt_size);
2654
2655        return 0;
2656err_free_queue:
2657        dma_free_coherent(&rdev->lldi.pdev->dev,
2658                          wq->memsize, wq->queue,
2659                        dma_unmap_addr(wq, mapping));
2660err_free_rqtpool:
2661        c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
2662err_free_pending_wrs:
2663        if (!user)
2664                kfree(wq->pending_wrs);
2665err_free_sw_rq:
2666        if (!user)
2667                kfree(wq->sw_rq);
2668err_put_qpid:
2669        c4iw_put_qpid(rdev, wq->qid, uctx);
2670err:
2671        return ret;
2672}
2673
2674void c4iw_copy_wr_to_srq(struct t4_srq *srq, union t4_recv_wr *wqe, u8 len16)
2675{
2676        u64 *src, *dst;
2677
2678        src = (u64 *)wqe;
2679        dst = (u64 *)((u8 *)srq->queue + srq->wq_pidx * T4_EQ_ENTRY_SIZE);
2680        while (len16) {
2681                *dst++ = *src++;
2682                if (dst >= (u64 *)&srq->queue[srq->size])
2683                        dst = (u64 *)srq->queue;
2684                *dst++ = *src++;
2685                if (dst >= (u64 *)&srq->queue[srq->size])
2686                        dst = (u64 *)srq->queue;
2687                len16--;
2688        }
2689}
2690
2691struct ib_srq *c4iw_create_srq(struct ib_pd *pd, struct ib_srq_init_attr *attrs,
2692                               struct ib_udata *udata)
2693{
2694        struct c4iw_dev *rhp;
2695        struct c4iw_srq *srq;
2696        struct c4iw_pd *php;
2697        struct c4iw_create_srq_resp uresp;
2698        struct c4iw_ucontext *ucontext;
2699        struct c4iw_mm_entry *srq_key_mm, *srq_db_key_mm;
2700        int rqsize;
2701        int ret;
2702        int wr_len;
2703
2704        pr_debug("%s ib_pd %p\n", __func__, pd);
2705
2706        php = to_c4iw_pd(pd);
2707        rhp = php->rhp;
2708
2709        if (!rhp->rdev.lldi.vr->srq.size)
2710                return ERR_PTR(-EINVAL);
2711        if (attrs->attr.max_wr > rhp->rdev.hw_queue.t4_max_rq_size)
2712                return ERR_PTR(-E2BIG);
2713        if (attrs->attr.max_sge > T4_MAX_RECV_SGE)
2714                return ERR_PTR(-E2BIG);
2715
2716        /*
2717         * SRQ RQT and RQ must be a power of 2 and at least 16 deep.
2718         */
2719        rqsize = attrs->attr.max_wr + 1;
2720        rqsize = roundup_pow_of_two(max_t(u16, rqsize, 16));
2721
2722        ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
2723
2724        srq = kzalloc(sizeof(*srq), GFP_KERNEL);
2725        if (!srq)
2726                return ERR_PTR(-ENOMEM);
2727
2728        srq->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
2729        if (!srq->wr_waitp) {
2730                ret = -ENOMEM;
2731                goto err_free_srq;
2732        }
2733
2734        srq->idx = c4iw_alloc_srq_idx(&rhp->rdev);
2735        if (srq->idx < 0) {
2736                ret = -ENOMEM;
2737                goto err_free_wr_wait;
2738        }
2739
2740        wr_len = sizeof(struct fw_ri_res_wr) + sizeof(struct fw_ri_res);
2741        srq->destroy_skb = alloc_skb(wr_len, GFP_KERNEL);
2742        if (!srq->destroy_skb) {
2743                ret = -ENOMEM;
2744                goto err_free_srq_idx;
2745        }
2746
2747        srq->rhp = rhp;
2748        srq->pdid = php->pdid;
2749
2750        srq->wq.size = rqsize;
2751        srq->wq.memsize =
2752                (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2753                sizeof(*srq->wq.queue);
2754        if (ucontext)
2755                srq->wq.memsize = roundup(srq->wq.memsize, PAGE_SIZE);
2756
2757        ret = alloc_srq_queue(srq, ucontext ? &ucontext->uctx :
2758                        &rhp->rdev.uctx, srq->wr_waitp);
2759        if (ret)
2760                goto err_free_skb;
2761        attrs->attr.max_wr = rqsize - 1;
2762
2763        if (CHELSIO_CHIP_VERSION(rhp->rdev.lldi.adapter_type) > CHELSIO_T6)
2764                srq->flags = T4_SRQ_LIMIT_SUPPORT;
2765
2766        ret = insert_handle(rhp, &rhp->qpidr, srq, srq->wq.qid);
2767        if (ret)
2768                goto err_free_queue;
2769
2770        if (udata) {
2771                srq_key_mm = kmalloc(sizeof(*srq_key_mm), GFP_KERNEL);
2772                if (!srq_key_mm) {
2773                        ret = -ENOMEM;
2774                        goto err_remove_handle;
2775                }
2776                srq_db_key_mm = kmalloc(sizeof(*srq_db_key_mm), GFP_KERNEL);
2777                if (!srq_db_key_mm) {
2778                        ret = -ENOMEM;
2779                        goto err_free_srq_key_mm;
2780                }
2781                memset(&uresp, 0, sizeof(uresp));
2782                uresp.flags = srq->flags;
2783                uresp.qid_mask = rhp->rdev.qpmask;
2784                uresp.srqid = srq->wq.qid;
2785                uresp.srq_size = srq->wq.size;
2786                uresp.srq_memsize = srq->wq.memsize;
2787                uresp.rqt_abs_idx = srq->wq.rqt_abs_idx;
2788                spin_lock(&ucontext->mmap_lock);
2789                uresp.srq_key = ucontext->key;
2790                ucontext->key += PAGE_SIZE;
2791                uresp.srq_db_gts_key = ucontext->key;
2792                ucontext->key += PAGE_SIZE;
2793                spin_unlock(&ucontext->mmap_lock);
2794                ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
2795                if (ret)
2796                        goto err_free_srq_db_key_mm;
2797                srq_key_mm->key = uresp.srq_key;
2798                srq_key_mm->addr = virt_to_phys(srq->wq.queue);
2799                srq_key_mm->len = PAGE_ALIGN(srq->wq.memsize);
2800                insert_mmap(ucontext, srq_key_mm);
2801                srq_db_key_mm->key = uresp.srq_db_gts_key;
2802                srq_db_key_mm->addr = (u64)(unsigned long)srq->wq.bar2_pa;
2803                srq_db_key_mm->len = PAGE_SIZE;
2804                insert_mmap(ucontext, srq_db_key_mm);
2805        }
2806
2807        pr_debug("%s srq qid %u idx %u size %u memsize %lu num_entries %u\n",
2808                 __func__, srq->wq.qid, srq->idx, srq->wq.size,
2809                        (unsigned long)srq->wq.memsize, attrs->attr.max_wr);
2810
2811        spin_lock_init(&srq->lock);
2812        return &srq->ibsrq;
2813err_free_srq_db_key_mm:
2814        kfree(srq_db_key_mm);
2815err_free_srq_key_mm:
2816        kfree(srq_key_mm);
2817err_remove_handle:
2818        remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
2819err_free_queue:
2820        free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2821                       srq->wr_waitp);
2822err_free_skb:
2823        kfree_skb(srq->destroy_skb);
2824err_free_srq_idx:
2825        c4iw_free_srq_idx(&rhp->rdev, srq->idx);
2826err_free_wr_wait:
2827        c4iw_put_wr_wait(srq->wr_waitp);
2828err_free_srq:
2829        kfree(srq);
2830        return ERR_PTR(ret);
2831}
2832
2833int c4iw_destroy_srq(struct ib_srq *ibsrq)
2834{
2835        struct c4iw_dev *rhp;
2836        struct c4iw_srq *srq;
2837        struct c4iw_ucontext *ucontext;
2838
2839        srq = to_c4iw_srq(ibsrq);
2840        rhp = srq->rhp;
2841
2842        pr_debug("%s id %d\n", __func__, srq->wq.qid);
2843
2844        remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
2845        ucontext = ibsrq->uobject ?
2846                to_c4iw_ucontext(ibsrq->uobject->context) : NULL;
2847        free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2848                       srq->wr_waitp);
2849        c4iw_free_srq_idx(&rhp->rdev, srq->idx);
2850        c4iw_put_wr_wait(srq->wr_waitp);
2851        kfree(srq);
2852        return 0;
2853}
2854