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        c4iw_put_wr_wait(qhp->wr_waitp);
 907        kfree(qhp);
 908}
 909
 910static void queue_qp_free(struct kref *kref)
 911{
 912        struct c4iw_qp *qhp;
 913
 914        qhp = container_of(kref, struct c4iw_qp, kref);
 915        pr_debug("qhp %p\n", qhp);
 916        queue_work(qhp->rhp->rdev.free_workq, &qhp->free_work);
 917}
 918
 919void c4iw_qp_add_ref(struct ib_qp *qp)
 920{
 921        pr_debug("ib_qp %p\n", qp);
 922        kref_get(&to_c4iw_qp(qp)->kref);
 923}
 924
 925void c4iw_qp_rem_ref(struct ib_qp *qp)
 926{
 927        pr_debug("ib_qp %p\n", qp);
 928        kref_put(&to_c4iw_qp(qp)->kref, queue_qp_free);
 929}
 930
 931static void add_to_fc_list(struct list_head *head, struct list_head *entry)
 932{
 933        if (list_empty(entry))
 934                list_add_tail(entry, head);
 935}
 936
 937static int ring_kernel_sq_db(struct c4iw_qp *qhp, u16 inc)
 938{
 939        unsigned long flags;
 940
 941        spin_lock_irqsave(&qhp->rhp->lock, flags);
 942        spin_lock(&qhp->lock);
 943        if (qhp->rhp->db_state == NORMAL)
 944                t4_ring_sq_db(&qhp->wq, inc, NULL);
 945        else {
 946                add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
 947                qhp->wq.sq.wq_pidx_inc += inc;
 948        }
 949        spin_unlock(&qhp->lock);
 950        spin_unlock_irqrestore(&qhp->rhp->lock, flags);
 951        return 0;
 952}
 953
 954static int ring_kernel_rq_db(struct c4iw_qp *qhp, u16 inc)
 955{
 956        unsigned long flags;
 957
 958        spin_lock_irqsave(&qhp->rhp->lock, flags);
 959        spin_lock(&qhp->lock);
 960        if (qhp->rhp->db_state == NORMAL)
 961                t4_ring_rq_db(&qhp->wq, inc, NULL);
 962        else {
 963                add_to_fc_list(&qhp->rhp->db_fc_list, &qhp->db_fc_entry);
 964                qhp->wq.rq.wq_pidx_inc += inc;
 965        }
 966        spin_unlock(&qhp->lock);
 967        spin_unlock_irqrestore(&qhp->rhp->lock, flags);
 968        return 0;
 969}
 970
 971static int ib_to_fw_opcode(int ib_opcode)
 972{
 973        int opcode;
 974
 975        switch (ib_opcode) {
 976        case IB_WR_SEND_WITH_INV:
 977                opcode = FW_RI_SEND_WITH_INV;
 978                break;
 979        case IB_WR_SEND:
 980                opcode = FW_RI_SEND;
 981                break;
 982        case IB_WR_RDMA_WRITE:
 983                opcode = FW_RI_RDMA_WRITE;
 984                break;
 985        case IB_WR_RDMA_WRITE_WITH_IMM:
 986                opcode = FW_RI_WRITE_IMMEDIATE;
 987                break;
 988        case IB_WR_RDMA_READ:
 989        case IB_WR_RDMA_READ_WITH_INV:
 990                opcode = FW_RI_READ_REQ;
 991                break;
 992        case IB_WR_REG_MR:
 993                opcode = FW_RI_FAST_REGISTER;
 994                break;
 995        case IB_WR_LOCAL_INV:
 996                opcode = FW_RI_LOCAL_INV;
 997                break;
 998        default:
 999                opcode = -EINVAL;
1000        }
1001        return opcode;
1002}
1003
1004static int complete_sq_drain_wr(struct c4iw_qp *qhp,
1005                                const struct ib_send_wr *wr)
1006{
1007        struct t4_cqe cqe = {};
1008        struct c4iw_cq *schp;
1009        unsigned long flag;
1010        struct t4_cq *cq;
1011        int opcode;
1012
1013        schp = to_c4iw_cq(qhp->ibqp.send_cq);
1014        cq = &schp->cq;
1015
1016        opcode = ib_to_fw_opcode(wr->opcode);
1017        if (opcode < 0)
1018                return opcode;
1019
1020        cqe.u.drain_cookie = wr->wr_id;
1021        cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
1022                                 CQE_OPCODE_V(opcode) |
1023                                 CQE_TYPE_V(1) |
1024                                 CQE_SWCQE_V(1) |
1025                                 CQE_DRAIN_V(1) |
1026                                 CQE_QPID_V(qhp->wq.sq.qid));
1027
1028        spin_lock_irqsave(&schp->lock, flag);
1029        cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
1030        cq->sw_queue[cq->sw_pidx] = cqe;
1031        t4_swcq_produce(cq);
1032        spin_unlock_irqrestore(&schp->lock, flag);
1033
1034        if (t4_clear_cq_armed(&schp->cq)) {
1035                spin_lock_irqsave(&schp->comp_handler_lock, flag);
1036                (*schp->ibcq.comp_handler)(&schp->ibcq,
1037                                           schp->ibcq.cq_context);
1038                spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1039        }
1040        return 0;
1041}
1042
1043static int complete_sq_drain_wrs(struct c4iw_qp *qhp,
1044                                 const struct ib_send_wr *wr,
1045                                 const struct ib_send_wr **bad_wr)
1046{
1047        int ret = 0;
1048
1049        while (wr) {
1050                ret = complete_sq_drain_wr(qhp, wr);
1051                if (ret) {
1052                        *bad_wr = wr;
1053                        break;
1054                }
1055                wr = wr->next;
1056        }
1057        return ret;
1058}
1059
1060static void complete_rq_drain_wr(struct c4iw_qp *qhp,
1061                                 const struct ib_recv_wr *wr)
1062{
1063        struct t4_cqe cqe = {};
1064        struct c4iw_cq *rchp;
1065        unsigned long flag;
1066        struct t4_cq *cq;
1067
1068        rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1069        cq = &rchp->cq;
1070
1071        cqe.u.drain_cookie = wr->wr_id;
1072        cqe.header = cpu_to_be32(CQE_STATUS_V(T4_ERR_SWFLUSH) |
1073                                 CQE_OPCODE_V(FW_RI_SEND) |
1074                                 CQE_TYPE_V(0) |
1075                                 CQE_SWCQE_V(1) |
1076                                 CQE_DRAIN_V(1) |
1077                                 CQE_QPID_V(qhp->wq.sq.qid));
1078
1079        spin_lock_irqsave(&rchp->lock, flag);
1080        cqe.bits_type_ts = cpu_to_be64(CQE_GENBIT_V((u64)cq->gen));
1081        cq->sw_queue[cq->sw_pidx] = cqe;
1082        t4_swcq_produce(cq);
1083        spin_unlock_irqrestore(&rchp->lock, flag);
1084
1085        if (t4_clear_cq_armed(&rchp->cq)) {
1086                spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1087                (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1088                                           rchp->ibcq.cq_context);
1089                spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1090        }
1091}
1092
1093static void complete_rq_drain_wrs(struct c4iw_qp *qhp,
1094                                  const struct ib_recv_wr *wr)
1095{
1096        while (wr) {
1097                complete_rq_drain_wr(qhp, wr);
1098                wr = wr->next;
1099        }
1100}
1101
1102int c4iw_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
1103                   const struct ib_send_wr **bad_wr)
1104{
1105        int err = 0;
1106        u8 len16 = 0;
1107        enum fw_wr_opcodes fw_opcode = 0;
1108        enum fw_ri_wr_flags fw_flags;
1109        struct c4iw_qp *qhp;
1110        struct c4iw_dev *rhp;
1111        union t4_wr *wqe = NULL;
1112        u32 num_wrs;
1113        struct t4_swsqe *swsqe;
1114        unsigned long flag;
1115        u16 idx = 0;
1116
1117        qhp = to_c4iw_qp(ibqp);
1118        rhp = qhp->rhp;
1119        spin_lock_irqsave(&qhp->lock, flag);
1120
1121        /*
1122         * If the qp has been flushed, then just insert a special
1123         * drain cqe.
1124         */
1125        if (qhp->wq.flushed) {
1126                spin_unlock_irqrestore(&qhp->lock, flag);
1127                err = complete_sq_drain_wrs(qhp, wr, bad_wr);
1128                return err;
1129        }
1130        num_wrs = t4_sq_avail(&qhp->wq);
1131        if (num_wrs == 0) {
1132                spin_unlock_irqrestore(&qhp->lock, flag);
1133                *bad_wr = wr;
1134                return -ENOMEM;
1135        }
1136
1137        /*
1138         * Fastpath for NVMe-oF target WRITE + SEND_WITH_INV wr chain which is
1139         * the response for small NVMEe-oF READ requests.  If the chain is
1140         * exactly a WRITE->SEND_WITH_INV or a WRITE->SEND and the sgl depths
1141         * and lengths meet the requirements of the fw_ri_write_cmpl_wr work
1142         * request, then build and post the write_cmpl WR. If any of the tests
1143         * below are not true, then we continue on with the tradtional WRITE
1144         * and SEND WRs.
1145         */
1146        if (qhp->rhp->rdev.lldi.write_cmpl_support &&
1147            CHELSIO_CHIP_VERSION(qhp->rhp->rdev.lldi.adapter_type) >=
1148            CHELSIO_T5 &&
1149            wr && wr->next && !wr->next->next &&
1150            wr->opcode == IB_WR_RDMA_WRITE &&
1151            wr->sg_list[0].length && wr->num_sge <= T4_WRITE_CMPL_MAX_SGL &&
1152            (wr->next->opcode == IB_WR_SEND ||
1153            wr->next->opcode == IB_WR_SEND_WITH_INV) &&
1154            wr->next->sg_list[0].length == T4_WRITE_CMPL_MAX_CQE &&
1155            wr->next->num_sge == 1 && num_wrs >= 2) {
1156                post_write_cmpl(qhp, wr);
1157                spin_unlock_irqrestore(&qhp->lock, flag);
1158                return 0;
1159        }
1160
1161        while (wr) {
1162                if (num_wrs == 0) {
1163                        err = -ENOMEM;
1164                        *bad_wr = wr;
1165                        break;
1166                }
1167                wqe = (union t4_wr *)((u8 *)qhp->wq.sq.queue +
1168                      qhp->wq.sq.wq_pidx * T4_EQ_ENTRY_SIZE);
1169
1170                fw_flags = 0;
1171                if (wr->send_flags & IB_SEND_SOLICITED)
1172                        fw_flags |= FW_RI_SOLICITED_EVENT_FLAG;
1173                if (wr->send_flags & IB_SEND_SIGNALED || qhp->sq_sig_all)
1174                        fw_flags |= FW_RI_COMPLETION_FLAG;
1175                swsqe = &qhp->wq.sq.sw_sq[qhp->wq.sq.pidx];
1176                switch (wr->opcode) {
1177                case IB_WR_SEND_WITH_INV:
1178                case IB_WR_SEND:
1179                        if (wr->send_flags & IB_SEND_FENCE)
1180                                fw_flags |= FW_RI_READ_FENCE_FLAG;
1181                        fw_opcode = FW_RI_SEND_WR;
1182                        if (wr->opcode == IB_WR_SEND)
1183                                swsqe->opcode = FW_RI_SEND;
1184                        else
1185                                swsqe->opcode = FW_RI_SEND_WITH_INV;
1186                        err = build_rdma_send(&qhp->wq.sq, wqe, wr, &len16);
1187                        break;
1188                case IB_WR_RDMA_WRITE_WITH_IMM:
1189                        if (unlikely(!rhp->rdev.lldi.write_w_imm_support)) {
1190                                err = -EINVAL;
1191                                break;
1192                        }
1193                        fw_flags |= FW_RI_RDMA_WRITE_WITH_IMMEDIATE;
1194                        /*FALLTHROUGH*/
1195                case IB_WR_RDMA_WRITE:
1196                        fw_opcode = FW_RI_RDMA_WRITE_WR;
1197                        swsqe->opcode = FW_RI_RDMA_WRITE;
1198                        err = build_rdma_write(&qhp->wq.sq, wqe, wr, &len16);
1199                        break;
1200                case IB_WR_RDMA_READ:
1201                case IB_WR_RDMA_READ_WITH_INV:
1202                        fw_opcode = FW_RI_RDMA_READ_WR;
1203                        swsqe->opcode = FW_RI_READ_REQ;
1204                        if (wr->opcode == IB_WR_RDMA_READ_WITH_INV) {
1205                                c4iw_invalidate_mr(rhp, wr->sg_list[0].lkey);
1206                                fw_flags = FW_RI_RDMA_READ_INVALIDATE;
1207                        } else {
1208                                fw_flags = 0;
1209                        }
1210                        err = build_rdma_read(wqe, wr, &len16);
1211                        if (err)
1212                                break;
1213                        swsqe->read_len = wr->sg_list[0].length;
1214                        if (!qhp->wq.sq.oldest_read)
1215                                qhp->wq.sq.oldest_read = swsqe;
1216                        break;
1217                case IB_WR_REG_MR: {
1218                        struct c4iw_mr *mhp = to_c4iw_mr(reg_wr(wr)->mr);
1219
1220                        swsqe->opcode = FW_RI_FAST_REGISTER;
1221                        if (rhp->rdev.lldi.fr_nsmr_tpte_wr_support &&
1222                            !mhp->attr.state && mhp->mpl_len <= 2) {
1223                                fw_opcode = FW_RI_FR_NSMR_TPTE_WR;
1224                                build_tpte_memreg(&wqe->fr_tpte, reg_wr(wr),
1225                                                  mhp, &len16);
1226                        } else {
1227                                fw_opcode = FW_RI_FR_NSMR_WR;
1228                                err = build_memreg(&qhp->wq.sq, wqe, reg_wr(wr),
1229                                       mhp, &len16,
1230                                       rhp->rdev.lldi.ulptx_memwrite_dsgl);
1231                                if (err)
1232                                        break;
1233                        }
1234                        mhp->attr.state = 1;
1235                        break;
1236                }
1237                case IB_WR_LOCAL_INV:
1238                        if (wr->send_flags & IB_SEND_FENCE)
1239                                fw_flags |= FW_RI_LOCAL_FENCE_FLAG;
1240                        fw_opcode = FW_RI_INV_LSTAG_WR;
1241                        swsqe->opcode = FW_RI_LOCAL_INV;
1242                        err = build_inv_stag(wqe, wr, &len16);
1243                        c4iw_invalidate_mr(rhp, wr->ex.invalidate_rkey);
1244                        break;
1245                default:
1246                        pr_warn("%s post of type=%d TBD!\n", __func__,
1247                                wr->opcode);
1248                        err = -EINVAL;
1249                }
1250                if (err) {
1251                        *bad_wr = wr;
1252                        break;
1253                }
1254                swsqe->idx = qhp->wq.sq.pidx;
1255                swsqe->complete = 0;
1256                swsqe->signaled = (wr->send_flags & IB_SEND_SIGNALED) ||
1257                                  qhp->sq_sig_all;
1258                swsqe->flushed = 0;
1259                swsqe->wr_id = wr->wr_id;
1260                if (c4iw_wr_log) {
1261                        swsqe->sge_ts = cxgb4_read_sge_timestamp(
1262                                        rhp->rdev.lldi.ports[0]);
1263                        swsqe->host_time = ktime_get();
1264                }
1265
1266                init_wr_hdr(wqe, qhp->wq.sq.pidx, fw_opcode, fw_flags, len16);
1267
1268                pr_debug("cookie 0x%llx pidx 0x%x opcode 0x%x read_len %u\n",
1269                         (unsigned long long)wr->wr_id, qhp->wq.sq.pidx,
1270                         swsqe->opcode, swsqe->read_len);
1271                wr = wr->next;
1272                num_wrs--;
1273                t4_sq_produce(&qhp->wq, len16);
1274                idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
1275        }
1276        if (!rhp->rdev.status_page->db_off) {
1277                t4_ring_sq_db(&qhp->wq, idx, wqe);
1278                spin_unlock_irqrestore(&qhp->lock, flag);
1279        } else {
1280                spin_unlock_irqrestore(&qhp->lock, flag);
1281                ring_kernel_sq_db(qhp, idx);
1282        }
1283        return err;
1284}
1285
1286int c4iw_post_receive(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1287                      const struct ib_recv_wr **bad_wr)
1288{
1289        int err = 0;
1290        struct c4iw_qp *qhp;
1291        union t4_recv_wr *wqe = NULL;
1292        u32 num_wrs;
1293        u8 len16 = 0;
1294        unsigned long flag;
1295        u16 idx = 0;
1296
1297        qhp = to_c4iw_qp(ibqp);
1298        spin_lock_irqsave(&qhp->lock, flag);
1299
1300        /*
1301         * If the qp has been flushed, then just insert a special
1302         * drain cqe.
1303         */
1304        if (qhp->wq.flushed) {
1305                spin_unlock_irqrestore(&qhp->lock, flag);
1306                complete_rq_drain_wrs(qhp, wr);
1307                return err;
1308        }
1309        num_wrs = t4_rq_avail(&qhp->wq);
1310        if (num_wrs == 0) {
1311                spin_unlock_irqrestore(&qhp->lock, flag);
1312                *bad_wr = wr;
1313                return -ENOMEM;
1314        }
1315        while (wr) {
1316                if (wr->num_sge > T4_MAX_RECV_SGE) {
1317                        err = -EINVAL;
1318                        *bad_wr = wr;
1319                        break;
1320                }
1321                wqe = (union t4_recv_wr *)((u8 *)qhp->wq.rq.queue +
1322                                           qhp->wq.rq.wq_pidx *
1323                                           T4_EQ_ENTRY_SIZE);
1324                if (num_wrs)
1325                        err = build_rdma_recv(qhp, wqe, wr, &len16);
1326                else
1327                        err = -ENOMEM;
1328                if (err) {
1329                        *bad_wr = wr;
1330                        break;
1331                }
1332
1333                qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].wr_id = wr->wr_id;
1334                if (c4iw_wr_log) {
1335                        qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].sge_ts =
1336                                cxgb4_read_sge_timestamp(
1337                                                qhp->rhp->rdev.lldi.ports[0]);
1338                        qhp->wq.rq.sw_rq[qhp->wq.rq.pidx].host_time =
1339                                ktime_get();
1340                }
1341
1342                wqe->recv.opcode = FW_RI_RECV_WR;
1343                wqe->recv.r1 = 0;
1344                wqe->recv.wrid = qhp->wq.rq.pidx;
1345                wqe->recv.r2[0] = 0;
1346                wqe->recv.r2[1] = 0;
1347                wqe->recv.r2[2] = 0;
1348                wqe->recv.len16 = len16;
1349                pr_debug("cookie 0x%llx pidx %u\n",
1350                         (unsigned long long)wr->wr_id, qhp->wq.rq.pidx);
1351                t4_rq_produce(&qhp->wq, len16);
1352                idx += DIV_ROUND_UP(len16*16, T4_EQ_ENTRY_SIZE);
1353                wr = wr->next;
1354                num_wrs--;
1355        }
1356        if (!qhp->rhp->rdev.status_page->db_off) {
1357                t4_ring_rq_db(&qhp->wq, idx, wqe);
1358                spin_unlock_irqrestore(&qhp->lock, flag);
1359        } else {
1360                spin_unlock_irqrestore(&qhp->lock, flag);
1361                ring_kernel_rq_db(qhp, idx);
1362        }
1363        return err;
1364}
1365
1366
1367static void defer_srq_wr(struct t4_srq *srq, union t4_recv_wr *wqe,
1368                         u64 wr_id, u8 len16)
1369{
1370        struct t4_srq_pending_wr *pwr = &srq->pending_wrs[srq->pending_pidx];
1371
1372        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",
1373                 __func__, srq->cidx, srq->pidx, srq->wq_pidx,
1374                 srq->in_use, srq->ooo_count,
1375                 (unsigned long long)wr_id, srq->pending_cidx,
1376                 srq->pending_pidx, srq->pending_in_use);
1377        pwr->wr_id = wr_id;
1378        pwr->len16 = len16;
1379        memcpy(&pwr->wqe, wqe, len16 * 16);
1380        t4_srq_produce_pending_wr(srq);
1381}
1382
1383int c4iw_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
1384                       const struct ib_recv_wr **bad_wr)
1385{
1386        union t4_recv_wr *wqe, lwqe;
1387        struct c4iw_srq *srq;
1388        unsigned long flag;
1389        u8 len16 = 0;
1390        u16 idx = 0;
1391        int err = 0;
1392        u32 num_wrs;
1393
1394        srq = to_c4iw_srq(ibsrq);
1395        spin_lock_irqsave(&srq->lock, flag);
1396        num_wrs = t4_srq_avail(&srq->wq);
1397        if (num_wrs == 0) {
1398                spin_unlock_irqrestore(&srq->lock, flag);
1399                return -ENOMEM;
1400        }
1401        while (wr) {
1402                if (wr->num_sge > T4_MAX_RECV_SGE) {
1403                        err = -EINVAL;
1404                        *bad_wr = wr;
1405                        break;
1406                }
1407                wqe = &lwqe;
1408                if (num_wrs)
1409                        err = build_srq_recv(wqe, wr, &len16);
1410                else
1411                        err = -ENOMEM;
1412                if (err) {
1413                        *bad_wr = wr;
1414                        break;
1415                }
1416
1417                wqe->recv.opcode = FW_RI_RECV_WR;
1418                wqe->recv.r1 = 0;
1419                wqe->recv.wrid = srq->wq.pidx;
1420                wqe->recv.r2[0] = 0;
1421                wqe->recv.r2[1] = 0;
1422                wqe->recv.r2[2] = 0;
1423                wqe->recv.len16 = len16;
1424
1425                if (srq->wq.ooo_count ||
1426                    srq->wq.pending_in_use ||
1427                    srq->wq.sw_rq[srq->wq.pidx].valid) {
1428                        defer_srq_wr(&srq->wq, wqe, wr->wr_id, len16);
1429                } else {
1430                        srq->wq.sw_rq[srq->wq.pidx].wr_id = wr->wr_id;
1431                        srq->wq.sw_rq[srq->wq.pidx].valid = 1;
1432                        c4iw_copy_wr_to_srq(&srq->wq, wqe, len16);
1433                        pr_debug("%s cidx %u pidx %u wq_pidx %u in_use %u wr_id 0x%llx\n",
1434                                 __func__, srq->wq.cidx,
1435                                 srq->wq.pidx, srq->wq.wq_pidx,
1436                                 srq->wq.in_use,
1437                                 (unsigned long long)wr->wr_id);
1438                        t4_srq_produce(&srq->wq, len16);
1439                        idx += DIV_ROUND_UP(len16 * 16, T4_EQ_ENTRY_SIZE);
1440                }
1441                wr = wr->next;
1442                num_wrs--;
1443        }
1444        if (idx)
1445                t4_ring_srq_db(&srq->wq, idx, len16, wqe);
1446        spin_unlock_irqrestore(&srq->lock, flag);
1447        return err;
1448}
1449
1450static inline void build_term_codes(struct t4_cqe *err_cqe, u8 *layer_type,
1451                                    u8 *ecode)
1452{
1453        int status;
1454        int tagged;
1455        int opcode;
1456        int rqtype;
1457        int send_inv;
1458
1459        if (!err_cqe) {
1460                *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1461                *ecode = 0;
1462                return;
1463        }
1464
1465        status = CQE_STATUS(err_cqe);
1466        opcode = CQE_OPCODE(err_cqe);
1467        rqtype = RQ_TYPE(err_cqe);
1468        send_inv = (opcode == FW_RI_SEND_WITH_INV) ||
1469                   (opcode == FW_RI_SEND_WITH_SE_INV);
1470        tagged = (opcode == FW_RI_RDMA_WRITE) ||
1471                 (rqtype && (opcode == FW_RI_READ_RESP));
1472
1473        switch (status) {
1474        case T4_ERR_STAG:
1475                if (send_inv) {
1476                        *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1477                        *ecode = RDMAP_CANT_INV_STAG;
1478                } else {
1479                        *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1480                        *ecode = RDMAP_INV_STAG;
1481                }
1482                break;
1483        case T4_ERR_PDID:
1484                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1485                if ((opcode == FW_RI_SEND_WITH_INV) ||
1486                    (opcode == FW_RI_SEND_WITH_SE_INV))
1487                        *ecode = RDMAP_CANT_INV_STAG;
1488                else
1489                        *ecode = RDMAP_STAG_NOT_ASSOC;
1490                break;
1491        case T4_ERR_QPID:
1492                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1493                *ecode = RDMAP_STAG_NOT_ASSOC;
1494                break;
1495        case T4_ERR_ACCESS:
1496                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1497                *ecode = RDMAP_ACC_VIOL;
1498                break;
1499        case T4_ERR_WRAP:
1500                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1501                *ecode = RDMAP_TO_WRAP;
1502                break;
1503        case T4_ERR_BOUND:
1504                if (tagged) {
1505                        *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1506                        *ecode = DDPT_BASE_BOUNDS;
1507                } else {
1508                        *layer_type = LAYER_RDMAP|RDMAP_REMOTE_PROT;
1509                        *ecode = RDMAP_BASE_BOUNDS;
1510                }
1511                break;
1512        case T4_ERR_INVALIDATE_SHARED_MR:
1513        case T4_ERR_INVALIDATE_MR_WITH_MW_BOUND:
1514                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1515                *ecode = RDMAP_CANT_INV_STAG;
1516                break;
1517        case T4_ERR_ECC:
1518        case T4_ERR_ECC_PSTAG:
1519        case T4_ERR_INTERNAL_ERR:
1520                *layer_type = LAYER_RDMAP|RDMAP_LOCAL_CATA;
1521                *ecode = 0;
1522                break;
1523        case T4_ERR_OUT_OF_RQE:
1524                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1525                *ecode = DDPU_INV_MSN_NOBUF;
1526                break;
1527        case T4_ERR_PBL_ADDR_BOUND:
1528                *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1529                *ecode = DDPT_BASE_BOUNDS;
1530                break;
1531        case T4_ERR_CRC:
1532                *layer_type = LAYER_MPA|DDP_LLP;
1533                *ecode = MPA_CRC_ERR;
1534                break;
1535        case T4_ERR_MARKER:
1536                *layer_type = LAYER_MPA|DDP_LLP;
1537                *ecode = MPA_MARKER_ERR;
1538                break;
1539        case T4_ERR_PDU_LEN_ERR:
1540                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1541                *ecode = DDPU_MSG_TOOBIG;
1542                break;
1543        case T4_ERR_DDP_VERSION:
1544                if (tagged) {
1545                        *layer_type = LAYER_DDP|DDP_TAGGED_ERR;
1546                        *ecode = DDPT_INV_VERS;
1547                } else {
1548                        *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1549                        *ecode = DDPU_INV_VERS;
1550                }
1551                break;
1552        case T4_ERR_RDMA_VERSION:
1553                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1554                *ecode = RDMAP_INV_VERS;
1555                break;
1556        case T4_ERR_OPCODE:
1557                *layer_type = LAYER_RDMAP|RDMAP_REMOTE_OP;
1558                *ecode = RDMAP_INV_OPCODE;
1559                break;
1560        case T4_ERR_DDP_QUEUE_NUM:
1561                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1562                *ecode = DDPU_INV_QN;
1563                break;
1564        case T4_ERR_MSN:
1565        case T4_ERR_MSN_GAP:
1566        case T4_ERR_MSN_RANGE:
1567        case T4_ERR_IRD_OVERFLOW:
1568                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1569                *ecode = DDPU_INV_MSN_RANGE;
1570                break;
1571        case T4_ERR_TBIT:
1572                *layer_type = LAYER_DDP|DDP_LOCAL_CATA;
1573                *ecode = 0;
1574                break;
1575        case T4_ERR_MO:
1576                *layer_type = LAYER_DDP|DDP_UNTAGGED_ERR;
1577                *ecode = DDPU_INV_MO;
1578                break;
1579        default:
1580                *layer_type = LAYER_RDMAP|DDP_LOCAL_CATA;
1581                *ecode = 0;
1582                break;
1583        }
1584}
1585
1586static void post_terminate(struct c4iw_qp *qhp, struct t4_cqe *err_cqe,
1587                           gfp_t gfp)
1588{
1589        struct fw_ri_wr *wqe;
1590        struct sk_buff *skb;
1591        struct terminate_message *term;
1592
1593        pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid,
1594                 qhp->ep->hwtid);
1595
1596        skb = skb_dequeue(&qhp->ep->com.ep_skb_list);
1597        if (WARN_ON(!skb))
1598                return;
1599
1600        set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1601
1602        wqe = __skb_put_zero(skb, sizeof(*wqe));
1603        wqe->op_compl = cpu_to_be32(FW_WR_OP_V(FW_RI_INIT_WR));
1604        wqe->flowid_len16 = cpu_to_be32(
1605                FW_WR_FLOWID_V(qhp->ep->hwtid) |
1606                FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
1607
1608        wqe->u.terminate.type = FW_RI_TYPE_TERMINATE;
1609        wqe->u.terminate.immdlen = cpu_to_be32(sizeof *term);
1610        term = (struct terminate_message *)wqe->u.terminate.termmsg;
1611        if (qhp->attr.layer_etype == (LAYER_MPA|DDP_LLP)) {
1612                term->layer_etype = qhp->attr.layer_etype;
1613                term->ecode = qhp->attr.ecode;
1614        } else
1615                build_term_codes(err_cqe, &term->layer_etype, &term->ecode);
1616        c4iw_ofld_send(&qhp->rhp->rdev, skb);
1617}
1618
1619/*
1620 * Assumes qhp lock is held.
1621 */
1622static void __flush_qp(struct c4iw_qp *qhp, struct c4iw_cq *rchp,
1623                       struct c4iw_cq *schp)
1624{
1625        int count;
1626        int rq_flushed = 0, sq_flushed;
1627        unsigned long flag;
1628
1629        pr_debug("qhp %p rchp %p schp %p\n", qhp, rchp, schp);
1630
1631        /* locking hierarchy: cqs lock first, then qp lock. */
1632        spin_lock_irqsave(&rchp->lock, flag);
1633        if (schp != rchp)
1634                spin_lock(&schp->lock);
1635        spin_lock(&qhp->lock);
1636
1637        if (qhp->wq.flushed) {
1638                spin_unlock(&qhp->lock);
1639                if (schp != rchp)
1640                        spin_unlock(&schp->lock);
1641                spin_unlock_irqrestore(&rchp->lock, flag);
1642                return;
1643        }
1644        qhp->wq.flushed = 1;
1645        t4_set_wq_in_error(&qhp->wq, 0);
1646
1647        c4iw_flush_hw_cq(rchp, qhp);
1648        if (!qhp->srq) {
1649                c4iw_count_rcqes(&rchp->cq, &qhp->wq, &count);
1650                rq_flushed = c4iw_flush_rq(&qhp->wq, &rchp->cq, count);
1651        }
1652
1653        if (schp != rchp)
1654                c4iw_flush_hw_cq(schp, qhp);
1655        sq_flushed = c4iw_flush_sq(qhp);
1656
1657        spin_unlock(&qhp->lock);
1658        if (schp != rchp)
1659                spin_unlock(&schp->lock);
1660        spin_unlock_irqrestore(&rchp->lock, flag);
1661
1662        if (schp == rchp) {
1663                if ((rq_flushed || sq_flushed) &&
1664                    t4_clear_cq_armed(&rchp->cq)) {
1665                        spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1666                        (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1667                                                   rchp->ibcq.cq_context);
1668                        spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1669                }
1670        } else {
1671                if (rq_flushed && t4_clear_cq_armed(&rchp->cq)) {
1672                        spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1673                        (*rchp->ibcq.comp_handler)(&rchp->ibcq,
1674                                                   rchp->ibcq.cq_context);
1675                        spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1676                }
1677                if (sq_flushed && t4_clear_cq_armed(&schp->cq)) {
1678                        spin_lock_irqsave(&schp->comp_handler_lock, flag);
1679                        (*schp->ibcq.comp_handler)(&schp->ibcq,
1680                                                   schp->ibcq.cq_context);
1681                        spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1682                }
1683        }
1684}
1685
1686static void flush_qp(struct c4iw_qp *qhp)
1687{
1688        struct c4iw_cq *rchp, *schp;
1689        unsigned long flag;
1690
1691        rchp = to_c4iw_cq(qhp->ibqp.recv_cq);
1692        schp = to_c4iw_cq(qhp->ibqp.send_cq);
1693
1694        if (qhp->ibqp.uobject) {
1695
1696                /* for user qps, qhp->wq.flushed is protected by qhp->mutex */
1697                if (qhp->wq.flushed)
1698                        return;
1699
1700                qhp->wq.flushed = 1;
1701                t4_set_wq_in_error(&qhp->wq, 0);
1702                t4_set_cq_in_error(&rchp->cq);
1703                spin_lock_irqsave(&rchp->comp_handler_lock, flag);
1704                (*rchp->ibcq.comp_handler)(&rchp->ibcq, rchp->ibcq.cq_context);
1705                spin_unlock_irqrestore(&rchp->comp_handler_lock, flag);
1706                if (schp != rchp) {
1707                        t4_set_cq_in_error(&schp->cq);
1708                        spin_lock_irqsave(&schp->comp_handler_lock, flag);
1709                        (*schp->ibcq.comp_handler)(&schp->ibcq,
1710                                        schp->ibcq.cq_context);
1711                        spin_unlock_irqrestore(&schp->comp_handler_lock, flag);
1712                }
1713                return;
1714        }
1715        __flush_qp(qhp, rchp, schp);
1716}
1717
1718static int rdma_fini(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1719                     struct c4iw_ep *ep)
1720{
1721        struct fw_ri_wr *wqe;
1722        int ret;
1723        struct sk_buff *skb;
1724
1725        pr_debug("qhp %p qid 0x%x tid %u\n", qhp, qhp->wq.sq.qid, ep->hwtid);
1726
1727        skb = skb_dequeue(&ep->com.ep_skb_list);
1728        if (WARN_ON(!skb))
1729                return -ENOMEM;
1730
1731        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1732
1733        wqe = __skb_put_zero(skb, sizeof(*wqe));
1734        wqe->op_compl = cpu_to_be32(
1735                FW_WR_OP_V(FW_RI_INIT_WR) |
1736                FW_WR_COMPL_F);
1737        wqe->flowid_len16 = cpu_to_be32(
1738                FW_WR_FLOWID_V(ep->hwtid) |
1739                FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
1740        wqe->cookie = (uintptr_t)ep->com.wr_waitp;
1741
1742        wqe->u.fini.type = FW_RI_TYPE_FINI;
1743
1744        ret = c4iw_ref_send_wait(&rhp->rdev, skb, ep->com.wr_waitp,
1745                                 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
1746
1747        pr_debug("ret %d\n", ret);
1748        return ret;
1749}
1750
1751static void build_rtr_msg(u8 p2p_type, struct fw_ri_init *init)
1752{
1753        pr_debug("p2p_type = %d\n", p2p_type);
1754        memset(&init->u, 0, sizeof init->u);
1755        switch (p2p_type) {
1756        case FW_RI_INIT_P2PTYPE_RDMA_WRITE:
1757                init->u.write.opcode = FW_RI_RDMA_WRITE_WR;
1758                init->u.write.stag_sink = cpu_to_be32(1);
1759                init->u.write.to_sink = cpu_to_be64(1);
1760                init->u.write.u.immd_src[0].op = FW_RI_DATA_IMMD;
1761                init->u.write.len16 = DIV_ROUND_UP(sizeof init->u.write +
1762                                                   sizeof(struct fw_ri_immd),
1763                                                   16);
1764                break;
1765        case FW_RI_INIT_P2PTYPE_READ_REQ:
1766                init->u.write.opcode = FW_RI_RDMA_READ_WR;
1767                init->u.read.stag_src = cpu_to_be32(1);
1768                init->u.read.to_src_lo = cpu_to_be32(1);
1769                init->u.read.stag_sink = cpu_to_be32(1);
1770                init->u.read.to_sink_lo = cpu_to_be32(1);
1771                init->u.read.len16 = DIV_ROUND_UP(sizeof init->u.read, 16);
1772                break;
1773        }
1774}
1775
1776static int rdma_init(struct c4iw_dev *rhp, struct c4iw_qp *qhp)
1777{
1778        struct fw_ri_wr *wqe;
1779        int ret;
1780        struct sk_buff *skb;
1781
1782        pr_debug("qhp %p qid 0x%x tid %u ird %u ord %u\n", qhp,
1783                 qhp->wq.sq.qid, qhp->ep->hwtid, qhp->ep->ird, qhp->ep->ord);
1784
1785        skb = alloc_skb(sizeof *wqe, GFP_KERNEL);
1786        if (!skb) {
1787                ret = -ENOMEM;
1788                goto out;
1789        }
1790        ret = alloc_ird(rhp, qhp->attr.max_ird);
1791        if (ret) {
1792                qhp->attr.max_ird = 0;
1793                kfree_skb(skb);
1794                goto out;
1795        }
1796        set_wr_txq(skb, CPL_PRIORITY_DATA, qhp->ep->txq_idx);
1797
1798        wqe = __skb_put_zero(skb, sizeof(*wqe));
1799        wqe->op_compl = cpu_to_be32(
1800                FW_WR_OP_V(FW_RI_INIT_WR) |
1801                FW_WR_COMPL_F);
1802        wqe->flowid_len16 = cpu_to_be32(
1803                FW_WR_FLOWID_V(qhp->ep->hwtid) |
1804                FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*wqe), 16)));
1805
1806        wqe->cookie = (uintptr_t)qhp->ep->com.wr_waitp;
1807
1808        wqe->u.init.type = FW_RI_TYPE_INIT;
1809        wqe->u.init.mpareqbit_p2ptype =
1810                FW_RI_WR_MPAREQBIT_V(qhp->attr.mpa_attr.initiator) |
1811                FW_RI_WR_P2PTYPE_V(qhp->attr.mpa_attr.p2p_type);
1812        wqe->u.init.mpa_attrs = FW_RI_MPA_IETF_ENABLE;
1813        if (qhp->attr.mpa_attr.recv_marker_enabled)
1814                wqe->u.init.mpa_attrs |= FW_RI_MPA_RX_MARKER_ENABLE;
1815        if (qhp->attr.mpa_attr.xmit_marker_enabled)
1816                wqe->u.init.mpa_attrs |= FW_RI_MPA_TX_MARKER_ENABLE;
1817        if (qhp->attr.mpa_attr.crc_enabled)
1818                wqe->u.init.mpa_attrs |= FW_RI_MPA_CRC_ENABLE;
1819
1820        wqe->u.init.qp_caps = FW_RI_QP_RDMA_READ_ENABLE |
1821                            FW_RI_QP_RDMA_WRITE_ENABLE |
1822                            FW_RI_QP_BIND_ENABLE;
1823        if (!qhp->ibqp.uobject)
1824                wqe->u.init.qp_caps |= FW_RI_QP_FAST_REGISTER_ENABLE |
1825                                     FW_RI_QP_STAG0_ENABLE;
1826        wqe->u.init.nrqe = cpu_to_be16(t4_rqes_posted(&qhp->wq));
1827        wqe->u.init.pdid = cpu_to_be32(qhp->attr.pd);
1828        wqe->u.init.qpid = cpu_to_be32(qhp->wq.sq.qid);
1829        wqe->u.init.sq_eqid = cpu_to_be32(qhp->wq.sq.qid);
1830        if (qhp->srq) {
1831                wqe->u.init.rq_eqid = cpu_to_be32(FW_RI_INIT_RQEQID_SRQ |
1832                                                  qhp->srq->idx);
1833        } else {
1834                wqe->u.init.rq_eqid = cpu_to_be32(qhp->wq.rq.qid);
1835                wqe->u.init.hwrqsize = cpu_to_be32(qhp->wq.rq.rqt_size);
1836                wqe->u.init.hwrqaddr = cpu_to_be32(qhp->wq.rq.rqt_hwaddr -
1837                                                   rhp->rdev.lldi.vr->rq.start);
1838        }
1839        wqe->u.init.scqid = cpu_to_be32(qhp->attr.scq);
1840        wqe->u.init.rcqid = cpu_to_be32(qhp->attr.rcq);
1841        wqe->u.init.ord_max = cpu_to_be32(qhp->attr.max_ord);
1842        wqe->u.init.ird_max = cpu_to_be32(qhp->attr.max_ird);
1843        wqe->u.init.iss = cpu_to_be32(qhp->ep->snd_seq);
1844        wqe->u.init.irs = cpu_to_be32(qhp->ep->rcv_seq);
1845        if (qhp->attr.mpa_attr.initiator)
1846                build_rtr_msg(qhp->attr.mpa_attr.p2p_type, &wqe->u.init);
1847
1848        ret = c4iw_ref_send_wait(&rhp->rdev, skb, qhp->ep->com.wr_waitp,
1849                                 qhp->ep->hwtid, qhp->wq.sq.qid, __func__);
1850        if (!ret)
1851                goto out;
1852
1853        free_ird(rhp, qhp->attr.max_ird);
1854out:
1855        pr_debug("ret %d\n", ret);
1856        return ret;
1857}
1858
1859int c4iw_modify_qp(struct c4iw_dev *rhp, struct c4iw_qp *qhp,
1860                   enum c4iw_qp_attr_mask mask,
1861                   struct c4iw_qp_attributes *attrs,
1862                   int internal)
1863{
1864        int ret = 0;
1865        struct c4iw_qp_attributes newattr = qhp->attr;
1866        int disconnect = 0;
1867        int terminate = 0;
1868        int abort = 0;
1869        int free = 0;
1870        struct c4iw_ep *ep = NULL;
1871
1872        pr_debug("qhp %p sqid 0x%x rqid 0x%x ep %p state %d -> %d\n",
1873                 qhp, qhp->wq.sq.qid, qhp->wq.rq.qid, qhp->ep, qhp->attr.state,
1874                 (mask & C4IW_QP_ATTR_NEXT_STATE) ? attrs->next_state : -1);
1875
1876        mutex_lock(&qhp->mutex);
1877
1878        /* Process attr changes if in IDLE */
1879        if (mask & C4IW_QP_ATTR_VALID_MODIFY) {
1880                if (qhp->attr.state != C4IW_QP_STATE_IDLE) {
1881                        ret = -EIO;
1882                        goto out;
1883                }
1884                if (mask & C4IW_QP_ATTR_ENABLE_RDMA_READ)
1885                        newattr.enable_rdma_read = attrs->enable_rdma_read;
1886                if (mask & C4IW_QP_ATTR_ENABLE_RDMA_WRITE)
1887                        newattr.enable_rdma_write = attrs->enable_rdma_write;
1888                if (mask & C4IW_QP_ATTR_ENABLE_RDMA_BIND)
1889                        newattr.enable_bind = attrs->enable_bind;
1890                if (mask & C4IW_QP_ATTR_MAX_ORD) {
1891                        if (attrs->max_ord > c4iw_max_read_depth) {
1892                                ret = -EINVAL;
1893                                goto out;
1894                        }
1895                        newattr.max_ord = attrs->max_ord;
1896                }
1897                if (mask & C4IW_QP_ATTR_MAX_IRD) {
1898                        if (attrs->max_ird > cur_max_read_depth(rhp)) {
1899                                ret = -EINVAL;
1900                                goto out;
1901                        }
1902                        newattr.max_ird = attrs->max_ird;
1903                }
1904                qhp->attr = newattr;
1905        }
1906
1907        if (mask & C4IW_QP_ATTR_SQ_DB) {
1908                ret = ring_kernel_sq_db(qhp, attrs->sq_db_inc);
1909                goto out;
1910        }
1911        if (mask & C4IW_QP_ATTR_RQ_DB) {
1912                ret = ring_kernel_rq_db(qhp, attrs->rq_db_inc);
1913                goto out;
1914        }
1915
1916        if (!(mask & C4IW_QP_ATTR_NEXT_STATE))
1917                goto out;
1918        if (qhp->attr.state == attrs->next_state)
1919                goto out;
1920
1921        switch (qhp->attr.state) {
1922        case C4IW_QP_STATE_IDLE:
1923                switch (attrs->next_state) {
1924                case C4IW_QP_STATE_RTS:
1925                        if (!(mask & C4IW_QP_ATTR_LLP_STREAM_HANDLE)) {
1926                                ret = -EINVAL;
1927                                goto out;
1928                        }
1929                        if (!(mask & C4IW_QP_ATTR_MPA_ATTR)) {
1930                                ret = -EINVAL;
1931                                goto out;
1932                        }
1933                        qhp->attr.mpa_attr = attrs->mpa_attr;
1934                        qhp->attr.llp_stream_handle = attrs->llp_stream_handle;
1935                        qhp->ep = qhp->attr.llp_stream_handle;
1936                        set_state(qhp, C4IW_QP_STATE_RTS);
1937
1938                        /*
1939                         * Ref the endpoint here and deref when we
1940                         * disassociate the endpoint from the QP.  This
1941                         * happens in CLOSING->IDLE transition or *->ERROR
1942                         * transition.
1943                         */
1944                        c4iw_get_ep(&qhp->ep->com);
1945                        ret = rdma_init(rhp, qhp);
1946                        if (ret)
1947                                goto err;
1948                        break;
1949                case C4IW_QP_STATE_ERROR:
1950                        set_state(qhp, C4IW_QP_STATE_ERROR);
1951                        flush_qp(qhp);
1952                        break;
1953                default:
1954                        ret = -EINVAL;
1955                        goto out;
1956                }
1957                break;
1958        case C4IW_QP_STATE_RTS:
1959                switch (attrs->next_state) {
1960                case C4IW_QP_STATE_CLOSING:
1961                        t4_set_wq_in_error(&qhp->wq, 0);
1962                        set_state(qhp, C4IW_QP_STATE_CLOSING);
1963                        ep = qhp->ep;
1964                        if (!internal) {
1965                                abort = 0;
1966                                disconnect = 1;
1967                                c4iw_get_ep(&qhp->ep->com);
1968                        }
1969                        ret = rdma_fini(rhp, qhp, ep);
1970                        if (ret)
1971                                goto err;
1972                        break;
1973                case C4IW_QP_STATE_TERMINATE:
1974                        t4_set_wq_in_error(&qhp->wq, 0);
1975                        set_state(qhp, C4IW_QP_STATE_TERMINATE);
1976                        qhp->attr.layer_etype = attrs->layer_etype;
1977                        qhp->attr.ecode = attrs->ecode;
1978                        ep = qhp->ep;
1979                        c4iw_get_ep(&ep->com);
1980                        disconnect = 1;
1981                        if (!internal) {
1982                                terminate = 1;
1983                        } else {
1984                                terminate = qhp->attr.send_term;
1985                                ret = rdma_fini(rhp, qhp, ep);
1986                                if (ret)
1987                                        goto err;
1988                        }
1989                        break;
1990                case C4IW_QP_STATE_ERROR:
1991                        t4_set_wq_in_error(&qhp->wq, 0);
1992                        set_state(qhp, C4IW_QP_STATE_ERROR);
1993                        if (!internal) {
1994                                abort = 1;
1995                                disconnect = 1;
1996                                ep = qhp->ep;
1997                                c4iw_get_ep(&qhp->ep->com);
1998                        }
1999                        goto err;
2000                        break;
2001                default:
2002                        ret = -EINVAL;
2003                        goto out;
2004                }
2005                break;
2006        case C4IW_QP_STATE_CLOSING:
2007
2008                /*
2009                 * Allow kernel users to move to ERROR for qp draining.
2010                 */
2011                if (!internal && (qhp->ibqp.uobject || attrs->next_state !=
2012                                  C4IW_QP_STATE_ERROR)) {
2013                        ret = -EINVAL;
2014                        goto out;
2015                }
2016                switch (attrs->next_state) {
2017                case C4IW_QP_STATE_IDLE:
2018                        flush_qp(qhp);
2019                        set_state(qhp, C4IW_QP_STATE_IDLE);
2020                        qhp->attr.llp_stream_handle = NULL;
2021                        c4iw_put_ep(&qhp->ep->com);
2022                        qhp->ep = NULL;
2023                        wake_up(&qhp->wait);
2024                        break;
2025                case C4IW_QP_STATE_ERROR:
2026                        goto err;
2027                default:
2028                        ret = -EINVAL;
2029                        goto err;
2030                }
2031                break;
2032        case C4IW_QP_STATE_ERROR:
2033                if (attrs->next_state != C4IW_QP_STATE_IDLE) {
2034                        ret = -EINVAL;
2035                        goto out;
2036                }
2037                if (!t4_sq_empty(&qhp->wq) || !t4_rq_empty(&qhp->wq)) {
2038                        ret = -EINVAL;
2039                        goto out;
2040                }
2041                set_state(qhp, C4IW_QP_STATE_IDLE);
2042                break;
2043        case C4IW_QP_STATE_TERMINATE:
2044                if (!internal) {
2045                        ret = -EINVAL;
2046                        goto out;
2047                }
2048                goto err;
2049                break;
2050        default:
2051                pr_err("%s in a bad state %d\n", __func__, qhp->attr.state);
2052                ret = -EINVAL;
2053                goto err;
2054                break;
2055        }
2056        goto out;
2057err:
2058        pr_debug("disassociating ep %p qpid 0x%x\n", qhp->ep,
2059                 qhp->wq.sq.qid);
2060
2061        /* disassociate the LLP connection */
2062        qhp->attr.llp_stream_handle = NULL;
2063        if (!ep)
2064                ep = qhp->ep;
2065        qhp->ep = NULL;
2066        set_state(qhp, C4IW_QP_STATE_ERROR);
2067        free = 1;
2068        abort = 1;
2069        flush_qp(qhp);
2070        wake_up(&qhp->wait);
2071out:
2072        mutex_unlock(&qhp->mutex);
2073
2074        if (terminate)
2075                post_terminate(qhp, NULL, internal ? GFP_ATOMIC : GFP_KERNEL);
2076
2077        /*
2078         * If disconnect is 1, then we need to initiate a disconnect
2079         * on the EP.  This can be a normal close (RTS->CLOSING) or
2080         * an abnormal close (RTS/CLOSING->ERROR).
2081         */
2082        if (disconnect) {
2083                c4iw_ep_disconnect(ep, abort, internal ? GFP_ATOMIC :
2084                                                         GFP_KERNEL);
2085                c4iw_put_ep(&ep->com);
2086        }
2087
2088        /*
2089         * If free is 1, then we've disassociated the EP from the QP
2090         * and we need to dereference the EP.
2091         */
2092        if (free)
2093                c4iw_put_ep(&ep->com);
2094        pr_debug("exit state %d\n", qhp->attr.state);
2095        return ret;
2096}
2097
2098int c4iw_destroy_qp(struct ib_qp *ib_qp)
2099{
2100        struct c4iw_dev *rhp;
2101        struct c4iw_qp *qhp;
2102        struct c4iw_qp_attributes attrs;
2103
2104        qhp = to_c4iw_qp(ib_qp);
2105        rhp = qhp->rhp;
2106
2107        attrs.next_state = C4IW_QP_STATE_ERROR;
2108        if (qhp->attr.state == C4IW_QP_STATE_TERMINATE)
2109                c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2110        else
2111                c4iw_modify_qp(rhp, qhp, C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
2112        wait_event(qhp->wait, !qhp->ep);
2113
2114        remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
2115
2116        spin_lock_irq(&rhp->lock);
2117        if (!list_empty(&qhp->db_fc_entry))
2118                list_del_init(&qhp->db_fc_entry);
2119        spin_unlock_irq(&rhp->lock);
2120        free_ird(rhp, qhp->attr.max_ird);
2121
2122        c4iw_qp_rem_ref(ib_qp);
2123
2124        pr_debug("ib_qp %p qpid 0x%0x\n", ib_qp, qhp->wq.sq.qid);
2125        return 0;
2126}
2127
2128struct ib_qp *c4iw_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
2129                             struct ib_udata *udata)
2130{
2131        struct c4iw_dev *rhp;
2132        struct c4iw_qp *qhp;
2133        struct c4iw_pd *php;
2134        struct c4iw_cq *schp;
2135        struct c4iw_cq *rchp;
2136        struct c4iw_create_qp_resp uresp;
2137        unsigned int sqsize, rqsize = 0;
2138        struct c4iw_ucontext *ucontext;
2139        int ret;
2140        struct c4iw_mm_entry *sq_key_mm, *rq_key_mm = NULL, *sq_db_key_mm;
2141        struct c4iw_mm_entry *rq_db_key_mm = NULL, *ma_sync_key_mm = NULL;
2142
2143        pr_debug("ib_pd %p\n", pd);
2144
2145        if (attrs->qp_type != IB_QPT_RC)
2146                return ERR_PTR(-EINVAL);
2147
2148        php = to_c4iw_pd(pd);
2149        rhp = php->rhp;
2150        schp = get_chp(rhp, ((struct c4iw_cq *)attrs->send_cq)->cq.cqid);
2151        rchp = get_chp(rhp, ((struct c4iw_cq *)attrs->recv_cq)->cq.cqid);
2152        if (!schp || !rchp)
2153                return ERR_PTR(-EINVAL);
2154
2155        if (attrs->cap.max_inline_data > T4_MAX_SEND_INLINE)
2156                return ERR_PTR(-EINVAL);
2157
2158        if (!attrs->srq) {
2159                if (attrs->cap.max_recv_wr > rhp->rdev.hw_queue.t4_max_rq_size)
2160                        return ERR_PTR(-E2BIG);
2161                rqsize = attrs->cap.max_recv_wr + 1;
2162                if (rqsize < 8)
2163                        rqsize = 8;
2164        }
2165
2166        if (attrs->cap.max_send_wr > rhp->rdev.hw_queue.t4_max_sq_size)
2167                return ERR_PTR(-E2BIG);
2168        sqsize = attrs->cap.max_send_wr + 1;
2169        if (sqsize < 8)
2170                sqsize = 8;
2171
2172        ucontext = udata ? to_c4iw_ucontext(pd->uobject->context) : NULL;
2173
2174        qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
2175        if (!qhp)
2176                return ERR_PTR(-ENOMEM);
2177
2178        qhp->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
2179        if (!qhp->wr_waitp) {
2180                ret = -ENOMEM;
2181                goto err_free_qhp;
2182        }
2183
2184        qhp->wq.sq.size = sqsize;
2185        qhp->wq.sq.memsize =
2186                (sqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2187                sizeof(*qhp->wq.sq.queue) + 16 * sizeof(__be64);
2188        qhp->wq.sq.flush_cidx = -1;
2189        if (!attrs->srq) {
2190                qhp->wq.rq.size = rqsize;
2191                qhp->wq.rq.memsize =
2192                        (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2193                        sizeof(*qhp->wq.rq.queue);
2194        }
2195
2196        if (ucontext) {
2197                qhp->wq.sq.memsize = roundup(qhp->wq.sq.memsize, PAGE_SIZE);
2198                if (!attrs->srq)
2199                        qhp->wq.rq.memsize =
2200                                roundup(qhp->wq.rq.memsize, PAGE_SIZE);
2201        }
2202
2203        ret = create_qp(&rhp->rdev, &qhp->wq, &schp->cq, &rchp->cq,
2204                        ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2205                        qhp->wr_waitp, !attrs->srq);
2206        if (ret)
2207                goto err_free_wr_wait;
2208
2209        attrs->cap.max_recv_wr = rqsize - 1;
2210        attrs->cap.max_send_wr = sqsize - 1;
2211        attrs->cap.max_inline_data = T4_MAX_SEND_INLINE;
2212
2213        qhp->rhp = rhp;
2214        qhp->attr.pd = php->pdid;
2215        qhp->attr.scq = ((struct c4iw_cq *) attrs->send_cq)->cq.cqid;
2216        qhp->attr.rcq = ((struct c4iw_cq *) attrs->recv_cq)->cq.cqid;
2217        qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
2218        qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
2219        qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
2220        if (!attrs->srq) {
2221                qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
2222                qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
2223        }
2224        qhp->attr.state = C4IW_QP_STATE_IDLE;
2225        qhp->attr.next_state = C4IW_QP_STATE_IDLE;
2226        qhp->attr.enable_rdma_read = 1;
2227        qhp->attr.enable_rdma_write = 1;
2228        qhp->attr.enable_bind = 1;
2229        qhp->attr.max_ord = 0;
2230        qhp->attr.max_ird = 0;
2231        qhp->sq_sig_all = attrs->sq_sig_type == IB_SIGNAL_ALL_WR;
2232        spin_lock_init(&qhp->lock);
2233        mutex_init(&qhp->mutex);
2234        init_waitqueue_head(&qhp->wait);
2235        kref_init(&qhp->kref);
2236        INIT_WORK(&qhp->free_work, free_qp_work);
2237
2238        ret = insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.sq.qid);
2239        if (ret)
2240                goto err_destroy_qp;
2241
2242        if (udata && ucontext) {
2243                sq_key_mm = kmalloc(sizeof(*sq_key_mm), GFP_KERNEL);
2244                if (!sq_key_mm) {
2245                        ret = -ENOMEM;
2246                        goto err_remove_handle;
2247                }
2248                if (!attrs->srq) {
2249                        rq_key_mm = kmalloc(sizeof(*rq_key_mm), GFP_KERNEL);
2250                        if (!rq_key_mm) {
2251                                ret = -ENOMEM;
2252                                goto err_free_sq_key;
2253                        }
2254                }
2255                sq_db_key_mm = kmalloc(sizeof(*sq_db_key_mm), GFP_KERNEL);
2256                if (!sq_db_key_mm) {
2257                        ret = -ENOMEM;
2258                        goto err_free_rq_key;
2259                }
2260                if (!attrs->srq) {
2261                        rq_db_key_mm =
2262                                kmalloc(sizeof(*rq_db_key_mm), GFP_KERNEL);
2263                        if (!rq_db_key_mm) {
2264                                ret = -ENOMEM;
2265                                goto err_free_sq_db_key;
2266                        }
2267                }
2268                memset(&uresp, 0, sizeof(uresp));
2269                if (t4_sq_onchip(&qhp->wq.sq)) {
2270                        ma_sync_key_mm = kmalloc(sizeof(*ma_sync_key_mm),
2271                                                 GFP_KERNEL);
2272                        if (!ma_sync_key_mm) {
2273                                ret = -ENOMEM;
2274                                goto err_free_rq_db_key;
2275                        }
2276                        uresp.flags = C4IW_QPF_ONCHIP;
2277                }
2278                if (rhp->rdev.lldi.write_w_imm_support)
2279                        uresp.flags |= C4IW_QPF_WRITE_W_IMM;
2280                uresp.qid_mask = rhp->rdev.qpmask;
2281                uresp.sqid = qhp->wq.sq.qid;
2282                uresp.sq_size = qhp->wq.sq.size;
2283                uresp.sq_memsize = qhp->wq.sq.memsize;
2284                if (!attrs->srq) {
2285                        uresp.rqid = qhp->wq.rq.qid;
2286                        uresp.rq_size = qhp->wq.rq.size;
2287                        uresp.rq_memsize = qhp->wq.rq.memsize;
2288                }
2289                spin_lock(&ucontext->mmap_lock);
2290                if (ma_sync_key_mm) {
2291                        uresp.ma_sync_key = ucontext->key;
2292                        ucontext->key += PAGE_SIZE;
2293                }
2294                uresp.sq_key = ucontext->key;
2295                ucontext->key += PAGE_SIZE;
2296                if (!attrs->srq) {
2297                        uresp.rq_key = ucontext->key;
2298                        ucontext->key += PAGE_SIZE;
2299                }
2300                uresp.sq_db_gts_key = ucontext->key;
2301                ucontext->key += PAGE_SIZE;
2302                if (!attrs->srq) {
2303                        uresp.rq_db_gts_key = ucontext->key;
2304                        ucontext->key += PAGE_SIZE;
2305                }
2306                spin_unlock(&ucontext->mmap_lock);
2307                ret = ib_copy_to_udata(udata, &uresp, sizeof uresp);
2308                if (ret)
2309                        goto err_free_ma_sync_key;
2310                sq_key_mm->key = uresp.sq_key;
2311                sq_key_mm->addr = qhp->wq.sq.phys_addr;
2312                sq_key_mm->len = PAGE_ALIGN(qhp->wq.sq.memsize);
2313                insert_mmap(ucontext, sq_key_mm);
2314                if (!attrs->srq) {
2315                        rq_key_mm->key = uresp.rq_key;
2316                        rq_key_mm->addr = virt_to_phys(qhp->wq.rq.queue);
2317                        rq_key_mm->len = PAGE_ALIGN(qhp->wq.rq.memsize);
2318                        insert_mmap(ucontext, rq_key_mm);
2319                }
2320                sq_db_key_mm->key = uresp.sq_db_gts_key;
2321                sq_db_key_mm->addr = (u64)(unsigned long)qhp->wq.sq.bar2_pa;
2322                sq_db_key_mm->len = PAGE_SIZE;
2323                insert_mmap(ucontext, sq_db_key_mm);
2324                if (!attrs->srq) {
2325                        rq_db_key_mm->key = uresp.rq_db_gts_key;
2326                        rq_db_key_mm->addr =
2327                                (u64)(unsigned long)qhp->wq.rq.bar2_pa;
2328                        rq_db_key_mm->len = PAGE_SIZE;
2329                        insert_mmap(ucontext, rq_db_key_mm);
2330                }
2331                if (ma_sync_key_mm) {
2332                        ma_sync_key_mm->key = uresp.ma_sync_key;
2333                        ma_sync_key_mm->addr =
2334                                (pci_resource_start(rhp->rdev.lldi.pdev, 0) +
2335                                PCIE_MA_SYNC_A) & PAGE_MASK;
2336                        ma_sync_key_mm->len = PAGE_SIZE;
2337                        insert_mmap(ucontext, ma_sync_key_mm);
2338                }
2339
2340                qhp->ucontext = ucontext;
2341        }
2342        if (!attrs->srq) {
2343                qhp->wq.qp_errp =
2344                        &qhp->wq.rq.queue[qhp->wq.rq.size].status.qp_err;
2345        } else {
2346                qhp->wq.qp_errp =
2347                        &qhp->wq.sq.queue[qhp->wq.sq.size].status.qp_err;
2348                qhp->wq.srqidxp =
2349                        &qhp->wq.sq.queue[qhp->wq.sq.size].status.srqidx;
2350        }
2351
2352        qhp->ibqp.qp_num = qhp->wq.sq.qid;
2353        if (attrs->srq)
2354                qhp->srq = to_c4iw_srq(attrs->srq);
2355        INIT_LIST_HEAD(&qhp->db_fc_entry);
2356        pr_debug("sq id %u size %u memsize %zu num_entries %u rq id %u size %u memsize %zu num_entries %u\n",
2357                 qhp->wq.sq.qid, qhp->wq.sq.size, qhp->wq.sq.memsize,
2358                 attrs->cap.max_send_wr, qhp->wq.rq.qid, qhp->wq.rq.size,
2359                 qhp->wq.rq.memsize, attrs->cap.max_recv_wr);
2360        return &qhp->ibqp;
2361err_free_ma_sync_key:
2362        kfree(ma_sync_key_mm);
2363err_free_rq_db_key:
2364        if (!attrs->srq)
2365                kfree(rq_db_key_mm);
2366err_free_sq_db_key:
2367        kfree(sq_db_key_mm);
2368err_free_rq_key:
2369        if (!attrs->srq)
2370                kfree(rq_key_mm);
2371err_free_sq_key:
2372        kfree(sq_key_mm);
2373err_remove_handle:
2374        remove_handle(rhp, &rhp->qpidr, qhp->wq.sq.qid);
2375err_destroy_qp:
2376        destroy_qp(&rhp->rdev, &qhp->wq,
2377                   ucontext ? &ucontext->uctx : &rhp->rdev.uctx, !attrs->srq);
2378err_free_wr_wait:
2379        c4iw_put_wr_wait(qhp->wr_waitp);
2380err_free_qhp:
2381        kfree(qhp);
2382        return ERR_PTR(ret);
2383}
2384
2385int c4iw_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2386                      int attr_mask, struct ib_udata *udata)
2387{
2388        struct c4iw_dev *rhp;
2389        struct c4iw_qp *qhp;
2390        enum c4iw_qp_attr_mask mask = 0;
2391        struct c4iw_qp_attributes attrs;
2392
2393        pr_debug("ib_qp %p\n", ibqp);
2394
2395        /* iwarp does not support the RTR state */
2396        if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
2397                attr_mask &= ~IB_QP_STATE;
2398
2399        /* Make sure we still have something left to do */
2400        if (!attr_mask)
2401                return 0;
2402
2403        memset(&attrs, 0, sizeof attrs);
2404        qhp = to_c4iw_qp(ibqp);
2405        rhp = qhp->rhp;
2406
2407        attrs.next_state = c4iw_convert_state(attr->qp_state);
2408        attrs.enable_rdma_read = (attr->qp_access_flags &
2409                               IB_ACCESS_REMOTE_READ) ?  1 : 0;
2410        attrs.enable_rdma_write = (attr->qp_access_flags &
2411                                IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
2412        attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
2413
2414
2415        mask |= (attr_mask & IB_QP_STATE) ? C4IW_QP_ATTR_NEXT_STATE : 0;
2416        mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
2417                        (C4IW_QP_ATTR_ENABLE_RDMA_READ |
2418                         C4IW_QP_ATTR_ENABLE_RDMA_WRITE |
2419                         C4IW_QP_ATTR_ENABLE_RDMA_BIND) : 0;
2420
2421        /*
2422         * Use SQ_PSN and RQ_PSN to pass in IDX_INC values for
2423         * ringing the queue db when we're in DB_FULL mode.
2424         * Only allow this on T4 devices.
2425         */
2426        attrs.sq_db_inc = attr->sq_psn;
2427        attrs.rq_db_inc = attr->rq_psn;
2428        mask |= (attr_mask & IB_QP_SQ_PSN) ? C4IW_QP_ATTR_SQ_DB : 0;
2429        mask |= (attr_mask & IB_QP_RQ_PSN) ? C4IW_QP_ATTR_RQ_DB : 0;
2430        if (!is_t4(to_c4iw_qp(ibqp)->rhp->rdev.lldi.adapter_type) &&
2431            (mask & (C4IW_QP_ATTR_SQ_DB|C4IW_QP_ATTR_RQ_DB)))
2432                return -EINVAL;
2433
2434        return c4iw_modify_qp(rhp, qhp, mask, &attrs, 0);
2435}
2436
2437struct ib_qp *c4iw_get_qp(struct ib_device *dev, int qpn)
2438{
2439        pr_debug("ib_dev %p qpn 0x%x\n", dev, qpn);
2440        return (struct ib_qp *)get_qhp(to_c4iw_dev(dev), qpn);
2441}
2442
2443void c4iw_dispatch_srq_limit_reached_event(struct c4iw_srq *srq)
2444{
2445        struct ib_event event = {};
2446
2447        event.device = &srq->rhp->ibdev;
2448        event.element.srq = &srq->ibsrq;
2449        event.event = IB_EVENT_SRQ_LIMIT_REACHED;
2450        ib_dispatch_event(&event);
2451}
2452
2453int c4iw_modify_srq(struct ib_srq *ib_srq, struct ib_srq_attr *attr,
2454                    enum ib_srq_attr_mask srq_attr_mask,
2455                    struct ib_udata *udata)
2456{
2457        struct c4iw_srq *srq = to_c4iw_srq(ib_srq);
2458        int ret = 0;
2459
2460        /*
2461         * XXX 0 mask == a SW interrupt for srq_limit reached...
2462         */
2463        if (udata && !srq_attr_mask) {
2464                c4iw_dispatch_srq_limit_reached_event(srq);
2465                goto out;
2466        }
2467
2468        /* no support for this yet */
2469        if (srq_attr_mask & IB_SRQ_MAX_WR) {
2470                ret = -EINVAL;
2471                goto out;
2472        }
2473
2474        if (!udata && (srq_attr_mask & IB_SRQ_LIMIT)) {
2475                srq->armed = true;
2476                srq->srq_limit = attr->srq_limit;
2477        }
2478out:
2479        return ret;
2480}
2481
2482int c4iw_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2483                     int attr_mask, struct ib_qp_init_attr *init_attr)
2484{
2485        struct c4iw_qp *qhp = to_c4iw_qp(ibqp);
2486
2487        memset(attr, 0, sizeof *attr);
2488        memset(init_attr, 0, sizeof *init_attr);
2489        attr->qp_state = to_ib_qp_state(qhp->attr.state);
2490        init_attr->cap.max_send_wr = qhp->attr.sq_num_entries;
2491        init_attr->cap.max_recv_wr = qhp->attr.rq_num_entries;
2492        init_attr->cap.max_send_sge = qhp->attr.sq_max_sges;
2493        init_attr->cap.max_recv_sge = qhp->attr.sq_max_sges;
2494        init_attr->cap.max_inline_data = T4_MAX_SEND_INLINE;
2495        init_attr->sq_sig_type = qhp->sq_sig_all ? IB_SIGNAL_ALL_WR : 0;
2496        return 0;
2497}
2498
2499static void free_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
2500                           struct c4iw_wr_wait *wr_waitp)
2501{
2502        struct c4iw_rdev *rdev = &srq->rhp->rdev;
2503        struct sk_buff *skb = srq->destroy_skb;
2504        struct t4_srq *wq = &srq->wq;
2505        struct fw_ri_res_wr *res_wr;
2506        struct fw_ri_res *res;
2507        int wr_len;
2508
2509        wr_len = sizeof(*res_wr) + sizeof(*res);
2510        set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
2511
2512        res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
2513        memset(res_wr, 0, wr_len);
2514        res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
2515                        FW_RI_RES_WR_NRES_V(1) |
2516                        FW_WR_COMPL_F);
2517        res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
2518        res_wr->cookie = (uintptr_t)wr_waitp;
2519        res = res_wr->res;
2520        res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
2521        res->u.srq.op = FW_RI_RES_OP_RESET;
2522        res->u.srq.srqid = cpu_to_be32(srq->idx);
2523        res->u.srq.eqid = cpu_to_be32(wq->qid);
2524
2525        c4iw_init_wr_wait(wr_waitp);
2526        c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, 0, __func__);
2527
2528        dma_free_coherent(&rdev->lldi.pdev->dev,
2529                          wq->memsize, wq->queue,
2530                        dma_unmap_addr(wq, mapping));
2531        c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
2532        kfree(wq->sw_rq);
2533        c4iw_put_qpid(rdev, wq->qid, uctx);
2534}
2535
2536static int alloc_srq_queue(struct c4iw_srq *srq, struct c4iw_dev_ucontext *uctx,
2537                           struct c4iw_wr_wait *wr_waitp)
2538{
2539        struct c4iw_rdev *rdev = &srq->rhp->rdev;
2540        int user = (uctx != &rdev->uctx);
2541        struct t4_srq *wq = &srq->wq;
2542        struct fw_ri_res_wr *res_wr;
2543        struct fw_ri_res *res;
2544        struct sk_buff *skb;
2545        int wr_len;
2546        int eqsize;
2547        int ret = -ENOMEM;
2548
2549        wq->qid = c4iw_get_qpid(rdev, uctx);
2550        if (!wq->qid)
2551                goto err;
2552
2553        if (!user) {
2554                wq->sw_rq = kcalloc(wq->size, sizeof(*wq->sw_rq),
2555                                    GFP_KERNEL);
2556                if (!wq->sw_rq)
2557                        goto err_put_qpid;
2558                wq->pending_wrs = kcalloc(srq->wq.size,
2559                                          sizeof(*srq->wq.pending_wrs),
2560                                          GFP_KERNEL);
2561                if (!wq->pending_wrs)
2562                        goto err_free_sw_rq;
2563        }
2564
2565        wq->rqt_size = wq->size;
2566        wq->rqt_hwaddr = c4iw_rqtpool_alloc(rdev, wq->rqt_size);
2567        if (!wq->rqt_hwaddr)
2568                goto err_free_pending_wrs;
2569        wq->rqt_abs_idx = (wq->rqt_hwaddr - rdev->lldi.vr->rq.start) >>
2570                T4_RQT_ENTRY_SHIFT;
2571
2572        wq->queue = dma_alloc_coherent(&rdev->lldi.pdev->dev,
2573                                       wq->memsize, &wq->dma_addr,
2574                        GFP_KERNEL);
2575        if (!wq->queue)
2576                goto err_free_rqtpool;
2577
2578        memset(wq->queue, 0, wq->memsize);
2579        dma_unmap_addr_set(wq, mapping, wq->dma_addr);
2580
2581        wq->bar2_va = c4iw_bar2_addrs(rdev, wq->qid, CXGB4_BAR2_QTYPE_EGRESS,
2582                                      &wq->bar2_qid,
2583                        user ? &wq->bar2_pa : NULL);
2584
2585        /*
2586         * User mode must have bar2 access.
2587         */
2588
2589        if (user && !wq->bar2_va) {
2590                pr_warn(MOD "%s: srqid %u not in BAR2 range.\n",
2591                        pci_name(rdev->lldi.pdev), wq->qid);
2592                ret = -EINVAL;
2593                goto err_free_queue;
2594        }
2595
2596        /* build fw_ri_res_wr */
2597        wr_len = sizeof(*res_wr) + sizeof(*res);
2598
2599        skb = alloc_skb(wr_len, GFP_KERNEL);
2600        if (!skb)
2601                goto err_free_queue;
2602        set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
2603
2604        res_wr = (struct fw_ri_res_wr *)__skb_put(skb, wr_len);
2605        memset(res_wr, 0, wr_len);
2606        res_wr->op_nres = cpu_to_be32(FW_WR_OP_V(FW_RI_RES_WR) |
2607                        FW_RI_RES_WR_NRES_V(1) |
2608                        FW_WR_COMPL_F);
2609        res_wr->len16_pkd = cpu_to_be32(DIV_ROUND_UP(wr_len, 16));
2610        res_wr->cookie = (uintptr_t)wr_waitp;
2611        res = res_wr->res;
2612        res->u.srq.restype = FW_RI_RES_TYPE_SRQ;
2613        res->u.srq.op = FW_RI_RES_OP_WRITE;
2614
2615        /*
2616         * eqsize is the number of 64B entries plus the status page size.
2617         */
2618        eqsize = wq->size * T4_RQ_NUM_SLOTS +
2619                rdev->hw_queue.t4_eq_status_entries;
2620        res->u.srq.eqid = cpu_to_be32(wq->qid);
2621        res->u.srq.fetchszm_to_iqid =
2622                                                /* no host cidx updates */
2623                cpu_to_be32(FW_RI_RES_WR_HOSTFCMODE_V(0) |
2624                FW_RI_RES_WR_CPRIO_V(0) |       /* don't keep in chip cache */
2625                FW_RI_RES_WR_PCIECHN_V(0) |     /* set by uP at ri_init time */
2626                FW_RI_RES_WR_FETCHRO_V(0));     /* relaxed_ordering */
2627        res->u.srq.dcaen_to_eqsize =
2628                cpu_to_be32(FW_RI_RES_WR_DCAEN_V(0) |
2629                FW_RI_RES_WR_DCACPU_V(0) |
2630                FW_RI_RES_WR_FBMIN_V(2) |
2631                FW_RI_RES_WR_FBMAX_V(3) |
2632                FW_RI_RES_WR_CIDXFTHRESHO_V(0) |
2633                FW_RI_RES_WR_CIDXFTHRESH_V(0) |
2634                FW_RI_RES_WR_EQSIZE_V(eqsize));
2635        res->u.srq.eqaddr = cpu_to_be64(wq->dma_addr);
2636        res->u.srq.srqid = cpu_to_be32(srq->idx);
2637        res->u.srq.pdid = cpu_to_be32(srq->pdid);
2638        res->u.srq.hwsrqsize = cpu_to_be32(wq->rqt_size);
2639        res->u.srq.hwsrqaddr = cpu_to_be32(wq->rqt_hwaddr -
2640                        rdev->lldi.vr->rq.start);
2641
2642        c4iw_init_wr_wait(wr_waitp);
2643
2644        ret = c4iw_ref_send_wait(rdev, skb, wr_waitp, 0, wq->qid, __func__);
2645        if (ret)
2646                goto err_free_queue;
2647
2648        pr_debug("%s srq %u eqid %u pdid %u queue va %p pa 0x%llx\n"
2649                        " bar2_addr %p rqt addr 0x%x size %d\n",
2650                        __func__, srq->idx, wq->qid, srq->pdid, wq->queue,
2651                        (u64)virt_to_phys(wq->queue), wq->bar2_va,
2652                        wq->rqt_hwaddr, wq->rqt_size);
2653
2654        return 0;
2655err_free_queue:
2656        dma_free_coherent(&rdev->lldi.pdev->dev,
2657                          wq->memsize, wq->queue,
2658                        dma_unmap_addr(wq, mapping));
2659err_free_rqtpool:
2660        c4iw_rqtpool_free(rdev, wq->rqt_hwaddr, wq->rqt_size);
2661err_free_pending_wrs:
2662        if (!user)
2663                kfree(wq->pending_wrs);
2664err_free_sw_rq:
2665        if (!user)
2666                kfree(wq->sw_rq);
2667err_put_qpid:
2668        c4iw_put_qpid(rdev, wq->qid, uctx);
2669err:
2670        return ret;
2671}
2672
2673void c4iw_copy_wr_to_srq(struct t4_srq *srq, union t4_recv_wr *wqe, u8 len16)
2674{
2675        u64 *src, *dst;
2676
2677        src = (u64 *)wqe;
2678        dst = (u64 *)((u8 *)srq->queue + srq->wq_pidx * T4_EQ_ENTRY_SIZE);
2679        while (len16) {
2680                *dst++ = *src++;
2681                if (dst >= (u64 *)&srq->queue[srq->size])
2682                        dst = (u64 *)srq->queue;
2683                *dst++ = *src++;
2684                if (dst >= (u64 *)&srq->queue[srq->size])
2685                        dst = (u64 *)srq->queue;
2686                len16--;
2687        }
2688}
2689
2690struct ib_srq *c4iw_create_srq(struct ib_pd *pd, struct ib_srq_init_attr *attrs,
2691                               struct ib_udata *udata)
2692{
2693        struct c4iw_dev *rhp;
2694        struct c4iw_srq *srq;
2695        struct c4iw_pd *php;
2696        struct c4iw_create_srq_resp uresp;
2697        struct c4iw_ucontext *ucontext;
2698        struct c4iw_mm_entry *srq_key_mm, *srq_db_key_mm;
2699        int rqsize;
2700        int ret;
2701        int wr_len;
2702
2703        pr_debug("%s ib_pd %p\n", __func__, pd);
2704
2705        php = to_c4iw_pd(pd);
2706        rhp = php->rhp;
2707
2708        if (!rhp->rdev.lldi.vr->srq.size)
2709                return ERR_PTR(-EINVAL);
2710        if (attrs->attr.max_wr > rhp->rdev.hw_queue.t4_max_rq_size)
2711                return ERR_PTR(-E2BIG);
2712        if (attrs->attr.max_sge > T4_MAX_RECV_SGE)
2713                return ERR_PTR(-E2BIG);
2714
2715        /*
2716         * SRQ RQT and RQ must be a power of 2 and at least 16 deep.
2717         */
2718        rqsize = attrs->attr.max_wr + 1;
2719        rqsize = roundup_pow_of_two(max_t(u16, rqsize, 16));
2720
2721        ucontext = pd->uobject ? to_c4iw_ucontext(pd->uobject->context) : NULL;
2722
2723        srq = kzalloc(sizeof(*srq), GFP_KERNEL);
2724        if (!srq)
2725                return ERR_PTR(-ENOMEM);
2726
2727        srq->wr_waitp = c4iw_alloc_wr_wait(GFP_KERNEL);
2728        if (!srq->wr_waitp) {
2729                ret = -ENOMEM;
2730                goto err_free_srq;
2731        }
2732
2733        srq->idx = c4iw_alloc_srq_idx(&rhp->rdev);
2734        if (srq->idx < 0) {
2735                ret = -ENOMEM;
2736                goto err_free_wr_wait;
2737        }
2738
2739        wr_len = sizeof(struct fw_ri_res_wr) + sizeof(struct fw_ri_res);
2740        srq->destroy_skb = alloc_skb(wr_len, GFP_KERNEL);
2741        if (!srq->destroy_skb) {
2742                ret = -ENOMEM;
2743                goto err_free_srq_idx;
2744        }
2745
2746        srq->rhp = rhp;
2747        srq->pdid = php->pdid;
2748
2749        srq->wq.size = rqsize;
2750        srq->wq.memsize =
2751                (rqsize + rhp->rdev.hw_queue.t4_eq_status_entries) *
2752                sizeof(*srq->wq.queue);
2753        if (ucontext)
2754                srq->wq.memsize = roundup(srq->wq.memsize, PAGE_SIZE);
2755
2756        ret = alloc_srq_queue(srq, ucontext ? &ucontext->uctx :
2757                        &rhp->rdev.uctx, srq->wr_waitp);
2758        if (ret)
2759                goto err_free_skb;
2760        attrs->attr.max_wr = rqsize - 1;
2761
2762        if (CHELSIO_CHIP_VERSION(rhp->rdev.lldi.adapter_type) > CHELSIO_T6)
2763                srq->flags = T4_SRQ_LIMIT_SUPPORT;
2764
2765        ret = insert_handle(rhp, &rhp->qpidr, srq, srq->wq.qid);
2766        if (ret)
2767                goto err_free_queue;
2768
2769        if (udata) {
2770                srq_key_mm = kmalloc(sizeof(*srq_key_mm), GFP_KERNEL);
2771                if (!srq_key_mm) {
2772                        ret = -ENOMEM;
2773                        goto err_remove_handle;
2774                }
2775                srq_db_key_mm = kmalloc(sizeof(*srq_db_key_mm), GFP_KERNEL);
2776                if (!srq_db_key_mm) {
2777                        ret = -ENOMEM;
2778                        goto err_free_srq_key_mm;
2779                }
2780                uresp.flags = srq->flags;
2781                uresp.qid_mask = rhp->rdev.qpmask;
2782                uresp.srqid = srq->wq.qid;
2783                uresp.srq_size = srq->wq.size;
2784                uresp.srq_memsize = srq->wq.memsize;
2785                uresp.rqt_abs_idx = srq->wq.rqt_abs_idx;
2786                spin_lock(&ucontext->mmap_lock);
2787                uresp.srq_key = ucontext->key;
2788                ucontext->key += PAGE_SIZE;
2789                uresp.srq_db_gts_key = ucontext->key;
2790                ucontext->key += PAGE_SIZE;
2791                spin_unlock(&ucontext->mmap_lock);
2792                ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
2793                if (ret)
2794                        goto err_free_srq_db_key_mm;
2795                srq_key_mm->key = uresp.srq_key;
2796                srq_key_mm->addr = virt_to_phys(srq->wq.queue);
2797                srq_key_mm->len = PAGE_ALIGN(srq->wq.memsize);
2798                insert_mmap(ucontext, srq_key_mm);
2799                srq_db_key_mm->key = uresp.srq_db_gts_key;
2800                srq_db_key_mm->addr = (u64)(unsigned long)srq->wq.bar2_pa;
2801                srq_db_key_mm->len = PAGE_SIZE;
2802                insert_mmap(ucontext, srq_db_key_mm);
2803        }
2804
2805        pr_debug("%s srq qid %u idx %u size %u memsize %lu num_entries %u\n",
2806                 __func__, srq->wq.qid, srq->idx, srq->wq.size,
2807                        (unsigned long)srq->wq.memsize, attrs->attr.max_wr);
2808
2809        spin_lock_init(&srq->lock);
2810        return &srq->ibsrq;
2811err_free_srq_db_key_mm:
2812        kfree(srq_db_key_mm);
2813err_free_srq_key_mm:
2814        kfree(srq_key_mm);
2815err_remove_handle:
2816        remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
2817err_free_queue:
2818        free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2819                       srq->wr_waitp);
2820err_free_skb:
2821        kfree_skb(srq->destroy_skb);
2822err_free_srq_idx:
2823        c4iw_free_srq_idx(&rhp->rdev, srq->idx);
2824err_free_wr_wait:
2825        c4iw_put_wr_wait(srq->wr_waitp);
2826err_free_srq:
2827        kfree(srq);
2828        return ERR_PTR(ret);
2829}
2830
2831int c4iw_destroy_srq(struct ib_srq *ibsrq)
2832{
2833        struct c4iw_dev *rhp;
2834        struct c4iw_srq *srq;
2835        struct c4iw_ucontext *ucontext;
2836
2837        srq = to_c4iw_srq(ibsrq);
2838        rhp = srq->rhp;
2839
2840        pr_debug("%s id %d\n", __func__, srq->wq.qid);
2841
2842        remove_handle(rhp, &rhp->qpidr, srq->wq.qid);
2843        ucontext = ibsrq->uobject ?
2844                to_c4iw_ucontext(ibsrq->uobject->context) : NULL;
2845        free_srq_queue(srq, ucontext ? &ucontext->uctx : &rhp->rdev.uctx,
2846                       srq->wr_waitp);
2847        c4iw_free_srq_idx(&rhp->rdev, srq->idx);
2848        c4iw_put_wr_wait(srq->wr_waitp);
2849        kfree(srq);
2850        return 0;
2851}
2852