linux/net/sunrpc/xprtrdma/svc_rdma_transport.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
   3 * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
   4 *
   5 * This software is available to you under a choice of one of two
   6 * licenses.  You may choose to be licensed under the terms of the GNU
   7 * General Public License (GPL) Version 2, available from the file
   8 * COPYING in the main directory of this source tree, or the BSD-type
   9 * license below:
  10 *
  11 * Redistribution and use in source and binary forms, with or without
  12 * modification, are permitted provided that the following conditions
  13 * are met:
  14 *
  15 *      Redistributions of source code must retain the above copyright
  16 *      notice, this list of conditions and the following 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 provided
  21 *      with the distribution.
  22 *
  23 *      Neither the name of the Network Appliance, Inc. nor the names of
  24 *      its contributors may be used to endorse or promote products
  25 *      derived from this software without specific prior written
  26 *      permission.
  27 *
  28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39 *
  40 * Author: Tom Tucker <tom@opengridcomputing.com>
  41 */
  42
  43#include <linux/sunrpc/svc_xprt.h>
  44#include <linux/sunrpc/debug.h>
  45#include <linux/sunrpc/rpc_rdma.h>
  46#include <linux/interrupt.h>
  47#include <linux/sched.h>
  48#include <linux/slab.h>
  49#include <linux/spinlock.h>
  50#include <linux/workqueue.h>
  51#include <rdma/ib_verbs.h>
  52#include <rdma/rdma_cm.h>
  53#include <linux/sunrpc/svc_rdma.h>
  54#include <linux/export.h>
  55#include "xprt_rdma.h"
  56
  57#define RPCDBG_FACILITY RPCDBG_SVCXPRT
  58
  59static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
  60                                        struct net *net,
  61                                        struct sockaddr *sa, int salen,
  62                                        int flags);
  63static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
  64static void svc_rdma_release_rqst(struct svc_rqst *);
  65static void dto_tasklet_func(unsigned long data);
  66static void svc_rdma_detach(struct svc_xprt *xprt);
  67static void svc_rdma_free(struct svc_xprt *xprt);
  68static int svc_rdma_has_wspace(struct svc_xprt *xprt);
  69static int svc_rdma_secure_port(struct svc_rqst *);
  70static void rq_cq_reap(struct svcxprt_rdma *xprt);
  71static void sq_cq_reap(struct svcxprt_rdma *xprt);
  72
  73static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
  74static DEFINE_SPINLOCK(dto_lock);
  75static LIST_HEAD(dto_xprt_q);
  76
  77static struct svc_xprt_ops svc_rdma_ops = {
  78        .xpo_create = svc_rdma_create,
  79        .xpo_recvfrom = svc_rdma_recvfrom,
  80        .xpo_sendto = svc_rdma_sendto,
  81        .xpo_release_rqst = svc_rdma_release_rqst,
  82        .xpo_detach = svc_rdma_detach,
  83        .xpo_free = svc_rdma_free,
  84        .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
  85        .xpo_has_wspace = svc_rdma_has_wspace,
  86        .xpo_accept = svc_rdma_accept,
  87        .xpo_secure_port = svc_rdma_secure_port,
  88};
  89
  90struct svc_xprt_class svc_rdma_class = {
  91        .xcl_name = "rdma",
  92        .xcl_owner = THIS_MODULE,
  93        .xcl_ops = &svc_rdma_ops,
  94        .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
  95};
  96
  97struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
  98{
  99        struct svc_rdma_op_ctxt *ctxt;
 100
 101        while (1) {
 102                ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
 103                if (ctxt)
 104                        break;
 105                schedule_timeout_uninterruptible(msecs_to_jiffies(500));
 106        }
 107        ctxt->xprt = xprt;
 108        INIT_LIST_HEAD(&ctxt->dto_q);
 109        ctxt->count = 0;
 110        ctxt->frmr = NULL;
 111        atomic_inc(&xprt->sc_ctxt_used);
 112        return ctxt;
 113}
 114
 115void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
 116{
 117        struct svcxprt_rdma *xprt = ctxt->xprt;
 118        int i;
 119        for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
 120                /*
 121                 * Unmap the DMA addr in the SGE if the lkey matches
 122                 * the sc_dma_lkey, otherwise, ignore it since it is
 123                 * an FRMR lkey and will be unmapped later when the
 124                 * last WR that uses it completes.
 125                 */
 126                if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
 127                        atomic_dec(&xprt->sc_dma_used);
 128                        ib_dma_unmap_page(xprt->sc_cm_id->device,
 129                                            ctxt->sge[i].addr,
 130                                            ctxt->sge[i].length,
 131                                            ctxt->direction);
 132                }
 133        }
 134}
 135
 136void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
 137{
 138        struct svcxprt_rdma *xprt;
 139        int i;
 140
 141        BUG_ON(!ctxt);
 142        xprt = ctxt->xprt;
 143        if (free_pages)
 144                for (i = 0; i < ctxt->count; i++)
 145                        put_page(ctxt->pages[i]);
 146
 147        kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
 148        atomic_dec(&xprt->sc_ctxt_used);
 149}
 150
 151/*
 152 * Temporary NFS req mappings are shared across all transport
 153 * instances. These are short lived and should be bounded by the number
 154 * of concurrent server threads * depth of the SQ.
 155 */
 156struct svc_rdma_req_map *svc_rdma_get_req_map(void)
 157{
 158        struct svc_rdma_req_map *map;
 159        while (1) {
 160                map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
 161                if (map)
 162                        break;
 163                schedule_timeout_uninterruptible(msecs_to_jiffies(500));
 164        }
 165        map->count = 0;
 166        return map;
 167}
 168
 169void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
 170{
 171        kmem_cache_free(svc_rdma_map_cachep, map);
 172}
 173
 174/* ib_cq event handler */
 175static void cq_event_handler(struct ib_event *event, void *context)
 176{
 177        struct svc_xprt *xprt = context;
 178        dprintk("svcrdma: received CQ event id=%d, context=%p\n",
 179                event->event, context);
 180        set_bit(XPT_CLOSE, &xprt->xpt_flags);
 181}
 182
 183/* QP event handler */
 184static void qp_event_handler(struct ib_event *event, void *context)
 185{
 186        struct svc_xprt *xprt = context;
 187
 188        switch (event->event) {
 189        /* These are considered benign events */
 190        case IB_EVENT_PATH_MIG:
 191        case IB_EVENT_COMM_EST:
 192        case IB_EVENT_SQ_DRAINED:
 193        case IB_EVENT_QP_LAST_WQE_REACHED:
 194                dprintk("svcrdma: QP event %d received for QP=%p\n",
 195                        event->event, event->element.qp);
 196                break;
 197        /* These are considered fatal events */
 198        case IB_EVENT_PATH_MIG_ERR:
 199        case IB_EVENT_QP_FATAL:
 200        case IB_EVENT_QP_REQ_ERR:
 201        case IB_EVENT_QP_ACCESS_ERR:
 202        case IB_EVENT_DEVICE_FATAL:
 203        default:
 204                dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
 205                        "closing transport\n",
 206                        event->event, event->element.qp);
 207                set_bit(XPT_CLOSE, &xprt->xpt_flags);
 208                break;
 209        }
 210}
 211
 212/*
 213 * Data Transfer Operation Tasklet
 214 *
 215 * Walks a list of transports with I/O pending, removing entries as
 216 * they are added to the server's I/O pending list. Two bits indicate
 217 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
 218 * spinlock that serializes access to the transport list with the RQ
 219 * and SQ interrupt handlers.
 220 */
 221static void dto_tasklet_func(unsigned long data)
 222{
 223        struct svcxprt_rdma *xprt;
 224        unsigned long flags;
 225
 226        spin_lock_irqsave(&dto_lock, flags);
 227        while (!list_empty(&dto_xprt_q)) {
 228                xprt = list_entry(dto_xprt_q.next,
 229                                  struct svcxprt_rdma, sc_dto_q);
 230                list_del_init(&xprt->sc_dto_q);
 231                spin_unlock_irqrestore(&dto_lock, flags);
 232
 233                rq_cq_reap(xprt);
 234                sq_cq_reap(xprt);
 235
 236                svc_xprt_put(&xprt->sc_xprt);
 237                spin_lock_irqsave(&dto_lock, flags);
 238        }
 239        spin_unlock_irqrestore(&dto_lock, flags);
 240}
 241
 242/*
 243 * Receive Queue Completion Handler
 244 *
 245 * Since an RQ completion handler is called on interrupt context, we
 246 * need to defer the handling of the I/O to a tasklet
 247 */
 248static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
 249{
 250        struct svcxprt_rdma *xprt = cq_context;
 251        unsigned long flags;
 252
 253        /* Guard against unconditional flush call for destroyed QP */
 254        if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
 255                return;
 256
 257        /*
 258         * Set the bit regardless of whether or not it's on the list
 259         * because it may be on the list already due to an SQ
 260         * completion.
 261         */
 262        set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
 263
 264        /*
 265         * If this transport is not already on the DTO transport queue,
 266         * add it
 267         */
 268        spin_lock_irqsave(&dto_lock, flags);
 269        if (list_empty(&xprt->sc_dto_q)) {
 270                svc_xprt_get(&xprt->sc_xprt);
 271                list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
 272        }
 273        spin_unlock_irqrestore(&dto_lock, flags);
 274
 275        /* Tasklet does all the work to avoid irqsave locks. */
 276        tasklet_schedule(&dto_tasklet);
 277}
 278
 279/*
 280 * rq_cq_reap - Process the RQ CQ.
 281 *
 282 * Take all completing WC off the CQE and enqueue the associated DTO
 283 * context on the dto_q for the transport.
 284 *
 285 * Note that caller must hold a transport reference.
 286 */
 287static void rq_cq_reap(struct svcxprt_rdma *xprt)
 288{
 289        int ret;
 290        struct ib_wc wc;
 291        struct svc_rdma_op_ctxt *ctxt = NULL;
 292
 293        if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
 294                return;
 295
 296        ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
 297        atomic_inc(&rdma_stat_rq_poll);
 298
 299        while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
 300                ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
 301                ctxt->wc_status = wc.status;
 302                ctxt->byte_len = wc.byte_len;
 303                svc_rdma_unmap_dma(ctxt);
 304                if (wc.status != IB_WC_SUCCESS) {
 305                        /* Close the transport */
 306                        dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
 307                        set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
 308                        svc_rdma_put_context(ctxt, 1);
 309                        svc_xprt_put(&xprt->sc_xprt);
 310                        continue;
 311                }
 312                spin_lock_bh(&xprt->sc_rq_dto_lock);
 313                list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
 314                spin_unlock_bh(&xprt->sc_rq_dto_lock);
 315                svc_xprt_put(&xprt->sc_xprt);
 316        }
 317
 318        if (ctxt)
 319                atomic_inc(&rdma_stat_rq_prod);
 320
 321        set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
 322        /*
 323         * If data arrived before established event,
 324         * don't enqueue. This defers RPC I/O until the
 325         * RDMA connection is complete.
 326         */
 327        if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
 328                svc_xprt_enqueue(&xprt->sc_xprt);
 329}
 330
 331/*
 332 * Process a completion context
 333 */
 334static void process_context(struct svcxprt_rdma *xprt,
 335                            struct svc_rdma_op_ctxt *ctxt)
 336{
 337        svc_rdma_unmap_dma(ctxt);
 338
 339        switch (ctxt->wr_op) {
 340        case IB_WR_SEND:
 341                BUG_ON(ctxt->frmr);
 342                svc_rdma_put_context(ctxt, 1);
 343                break;
 344
 345        case IB_WR_RDMA_WRITE:
 346                BUG_ON(ctxt->frmr);
 347                svc_rdma_put_context(ctxt, 0);
 348                break;
 349
 350        case IB_WR_RDMA_READ:
 351        case IB_WR_RDMA_READ_WITH_INV:
 352                svc_rdma_put_frmr(xprt, ctxt->frmr);
 353                if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
 354                        struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
 355                        BUG_ON(!read_hdr);
 356                        spin_lock_bh(&xprt->sc_rq_dto_lock);
 357                        set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
 358                        list_add_tail(&read_hdr->dto_q,
 359                                      &xprt->sc_read_complete_q);
 360                        spin_unlock_bh(&xprt->sc_rq_dto_lock);
 361                        svc_xprt_enqueue(&xprt->sc_xprt);
 362                }
 363                svc_rdma_put_context(ctxt, 0);
 364                break;
 365
 366        default:
 367                BUG_ON(1);
 368                printk(KERN_ERR "svcrdma: unexpected completion type, "
 369                       "opcode=%d\n",
 370                       ctxt->wr_op);
 371                break;
 372        }
 373}
 374
 375/*
 376 * Send Queue Completion Handler - potentially called on interrupt context.
 377 *
 378 * Note that caller must hold a transport reference.
 379 */
 380static void sq_cq_reap(struct svcxprt_rdma *xprt)
 381{
 382        struct svc_rdma_op_ctxt *ctxt = NULL;
 383        struct ib_wc wc_a[6];
 384        struct ib_wc *wc;
 385        struct ib_cq *cq = xprt->sc_sq_cq;
 386        int ret;
 387
 388        memset(wc_a, 0, sizeof(wc_a));
 389
 390        if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
 391                return;
 392
 393        ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
 394        atomic_inc(&rdma_stat_sq_poll);
 395        while ((ret = ib_poll_cq(cq, ARRAY_SIZE(wc_a), wc_a)) > 0) {
 396                int i;
 397
 398                for (i = 0; i < ret; i++) {
 399                        wc = &wc_a[i];
 400                        if (wc->status != IB_WC_SUCCESS) {
 401                                dprintk("svcrdma: sq wc err status %d\n",
 402                                        wc->status);
 403
 404                                /* Close the transport */
 405                                set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
 406                        }
 407
 408                        /* Decrement used SQ WR count */
 409                        atomic_dec(&xprt->sc_sq_count);
 410                        wake_up(&xprt->sc_send_wait);
 411
 412                        ctxt = (struct svc_rdma_op_ctxt *)
 413                                (unsigned long)wc->wr_id;
 414                        if (ctxt)
 415                                process_context(xprt, ctxt);
 416
 417                        svc_xprt_put(&xprt->sc_xprt);
 418                }
 419        }
 420
 421        if (ctxt)
 422                atomic_inc(&rdma_stat_sq_prod);
 423}
 424
 425static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
 426{
 427        struct svcxprt_rdma *xprt = cq_context;
 428        unsigned long flags;
 429
 430        /* Guard against unconditional flush call for destroyed QP */
 431        if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
 432                return;
 433
 434        /*
 435         * Set the bit regardless of whether or not it's on the list
 436         * because it may be on the list already due to an RQ
 437         * completion.
 438         */
 439        set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
 440
 441        /*
 442         * If this transport is not already on the DTO transport queue,
 443         * add it
 444         */
 445        spin_lock_irqsave(&dto_lock, flags);
 446        if (list_empty(&xprt->sc_dto_q)) {
 447                svc_xprt_get(&xprt->sc_xprt);
 448                list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
 449        }
 450        spin_unlock_irqrestore(&dto_lock, flags);
 451
 452        /* Tasklet does all the work to avoid irqsave locks. */
 453        tasklet_schedule(&dto_tasklet);
 454}
 455
 456static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
 457                                             int listener)
 458{
 459        struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
 460
 461        if (!cma_xprt)
 462                return NULL;
 463        svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
 464        INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
 465        INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
 466        INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
 467        INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
 468        INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
 469        init_waitqueue_head(&cma_xprt->sc_send_wait);
 470
 471        spin_lock_init(&cma_xprt->sc_lock);
 472        spin_lock_init(&cma_xprt->sc_rq_dto_lock);
 473        spin_lock_init(&cma_xprt->sc_frmr_q_lock);
 474
 475        cma_xprt->sc_ord = svcrdma_ord;
 476
 477        cma_xprt->sc_max_req_size = svcrdma_max_req_size;
 478        cma_xprt->sc_max_requests = svcrdma_max_requests;
 479        cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
 480        atomic_set(&cma_xprt->sc_sq_count, 0);
 481        atomic_set(&cma_xprt->sc_ctxt_used, 0);
 482
 483        if (listener)
 484                set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
 485
 486        return cma_xprt;
 487}
 488
 489struct page *svc_rdma_get_page(void)
 490{
 491        struct page *page;
 492
 493        while ((page = alloc_page(GFP_KERNEL)) == NULL) {
 494                /* If we can't get memory, wait a bit and try again */
 495                printk(KERN_INFO "svcrdma: out of memory...retrying in 1s\n");
 496                schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
 497        }
 498        return page;
 499}
 500
 501int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
 502{
 503        struct ib_recv_wr recv_wr, *bad_recv_wr;
 504        struct svc_rdma_op_ctxt *ctxt;
 505        struct page *page;
 506        dma_addr_t pa;
 507        int sge_no;
 508        int buflen;
 509        int ret;
 510
 511        ctxt = svc_rdma_get_context(xprt);
 512        buflen = 0;
 513        ctxt->direction = DMA_FROM_DEVICE;
 514        for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
 515                BUG_ON(sge_no >= xprt->sc_max_sge);
 516                page = svc_rdma_get_page();
 517                ctxt->pages[sge_no] = page;
 518                pa = ib_dma_map_page(xprt->sc_cm_id->device,
 519                                     page, 0, PAGE_SIZE,
 520                                     DMA_FROM_DEVICE);
 521                if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
 522                        goto err_put_ctxt;
 523                atomic_inc(&xprt->sc_dma_used);
 524                ctxt->sge[sge_no].addr = pa;
 525                ctxt->sge[sge_no].length = PAGE_SIZE;
 526                ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
 527                ctxt->count = sge_no + 1;
 528                buflen += PAGE_SIZE;
 529        }
 530        recv_wr.next = NULL;
 531        recv_wr.sg_list = &ctxt->sge[0];
 532        recv_wr.num_sge = ctxt->count;
 533        recv_wr.wr_id = (u64)(unsigned long)ctxt;
 534
 535        svc_xprt_get(&xprt->sc_xprt);
 536        ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
 537        if (ret) {
 538                svc_rdma_unmap_dma(ctxt);
 539                svc_rdma_put_context(ctxt, 1);
 540                svc_xprt_put(&xprt->sc_xprt);
 541        }
 542        return ret;
 543
 544 err_put_ctxt:
 545        svc_rdma_unmap_dma(ctxt);
 546        svc_rdma_put_context(ctxt, 1);
 547        return -ENOMEM;
 548}
 549
 550/*
 551 * This function handles the CONNECT_REQUEST event on a listening
 552 * endpoint. It is passed the cma_id for the _new_ connection. The context in
 553 * this cma_id is inherited from the listening cma_id and is the svc_xprt
 554 * structure for the listening endpoint.
 555 *
 556 * This function creates a new xprt for the new connection and enqueues it on
 557 * the accept queue for the listent xprt. When the listen thread is kicked, it
 558 * will call the recvfrom method on the listen xprt which will accept the new
 559 * connection.
 560 */
 561static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
 562{
 563        struct svcxprt_rdma *listen_xprt = new_cma_id->context;
 564        struct svcxprt_rdma *newxprt;
 565        struct sockaddr *sa;
 566
 567        /* Create a new transport */
 568        newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
 569        if (!newxprt) {
 570                dprintk("svcrdma: failed to create new transport\n");
 571                return;
 572        }
 573        newxprt->sc_cm_id = new_cma_id;
 574        new_cma_id->context = newxprt;
 575        dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
 576                newxprt, newxprt->sc_cm_id, listen_xprt);
 577
 578        /* Save client advertised inbound read limit for use later in accept. */
 579        newxprt->sc_ord = client_ird;
 580
 581        /* Set the local and remote addresses in the transport */
 582        sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
 583        svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
 584        sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
 585        svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
 586
 587        /*
 588         * Enqueue the new transport on the accept queue of the listening
 589         * transport
 590         */
 591        spin_lock_bh(&listen_xprt->sc_lock);
 592        list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
 593        spin_unlock_bh(&listen_xprt->sc_lock);
 594
 595        set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
 596        svc_xprt_enqueue(&listen_xprt->sc_xprt);
 597}
 598
 599/*
 600 * Handles events generated on the listening endpoint. These events will be
 601 * either be incoming connect requests or adapter removal  events.
 602 */
 603static int rdma_listen_handler(struct rdma_cm_id *cma_id,
 604                               struct rdma_cm_event *event)
 605{
 606        struct svcxprt_rdma *xprt = cma_id->context;
 607        int ret = 0;
 608
 609        switch (event->event) {
 610        case RDMA_CM_EVENT_CONNECT_REQUEST:
 611                dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
 612                        "event=%d\n", cma_id, cma_id->context, event->event);
 613                handle_connect_req(cma_id,
 614                                   event->param.conn.initiator_depth);
 615                break;
 616
 617        case RDMA_CM_EVENT_ESTABLISHED:
 618                /* Accept complete */
 619                dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
 620                        "cm_id=%p\n", xprt, cma_id);
 621                break;
 622
 623        case RDMA_CM_EVENT_DEVICE_REMOVAL:
 624                dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
 625                        xprt, cma_id);
 626                if (xprt)
 627                        set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
 628                break;
 629
 630        default:
 631                dprintk("svcrdma: Unexpected event on listening endpoint %p, "
 632                        "event=%d\n", cma_id, event->event);
 633                break;
 634        }
 635
 636        return ret;
 637}
 638
 639static int rdma_cma_handler(struct rdma_cm_id *cma_id,
 640                            struct rdma_cm_event *event)
 641{
 642        struct svc_xprt *xprt = cma_id->context;
 643        struct svcxprt_rdma *rdma =
 644                container_of(xprt, struct svcxprt_rdma, sc_xprt);
 645        switch (event->event) {
 646        case RDMA_CM_EVENT_ESTABLISHED:
 647                /* Accept complete */
 648                svc_xprt_get(xprt);
 649                dprintk("svcrdma: Connection completed on DTO xprt=%p, "
 650                        "cm_id=%p\n", xprt, cma_id);
 651                clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
 652                svc_xprt_enqueue(xprt);
 653                break;
 654        case RDMA_CM_EVENT_DISCONNECTED:
 655                dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
 656                        xprt, cma_id);
 657                if (xprt) {
 658                        set_bit(XPT_CLOSE, &xprt->xpt_flags);
 659                        svc_xprt_enqueue(xprt);
 660                        svc_xprt_put(xprt);
 661                }
 662                break;
 663        case RDMA_CM_EVENT_DEVICE_REMOVAL:
 664                dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
 665                        "event=%d\n", cma_id, xprt, event->event);
 666                if (xprt) {
 667                        set_bit(XPT_CLOSE, &xprt->xpt_flags);
 668                        svc_xprt_enqueue(xprt);
 669                }
 670                break;
 671        default:
 672                dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
 673                        "event=%d\n", cma_id, event->event);
 674                break;
 675        }
 676        return 0;
 677}
 678
 679/*
 680 * Create a listening RDMA service endpoint.
 681 */
 682static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
 683                                        struct net *net,
 684                                        struct sockaddr *sa, int salen,
 685                                        int flags)
 686{
 687        struct rdma_cm_id *listen_id;
 688        struct svcxprt_rdma *cma_xprt;
 689        struct svc_xprt *xprt;
 690        int ret;
 691
 692        dprintk("svcrdma: Creating RDMA socket\n");
 693        if (sa->sa_family != AF_INET) {
 694                dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
 695                return ERR_PTR(-EAFNOSUPPORT);
 696        }
 697        cma_xprt = rdma_create_xprt(serv, 1);
 698        if (!cma_xprt)
 699                return ERR_PTR(-ENOMEM);
 700        xprt = &cma_xprt->sc_xprt;
 701
 702        listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP,
 703                                   IB_QPT_RC);
 704        if (IS_ERR(listen_id)) {
 705                ret = PTR_ERR(listen_id);
 706                dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
 707                goto err0;
 708        }
 709
 710        ret = rdma_bind_addr(listen_id, sa);
 711        if (ret) {
 712                dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
 713                goto err1;
 714        }
 715        cma_xprt->sc_cm_id = listen_id;
 716
 717        ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
 718        if (ret) {
 719                dprintk("svcrdma: rdma_listen failed = %d\n", ret);
 720                goto err1;
 721        }
 722
 723        /*
 724         * We need to use the address from the cm_id in case the
 725         * caller specified 0 for the port number.
 726         */
 727        sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
 728        svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
 729
 730        return &cma_xprt->sc_xprt;
 731
 732 err1:
 733        rdma_destroy_id(listen_id);
 734 err0:
 735        kfree(cma_xprt);
 736        return ERR_PTR(ret);
 737}
 738
 739static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
 740{
 741        struct ib_mr *mr;
 742        struct ib_fast_reg_page_list *pl;
 743        struct svc_rdma_fastreg_mr *frmr;
 744
 745        frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
 746        if (!frmr)
 747                goto err;
 748
 749        mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
 750        if (IS_ERR(mr))
 751                goto err_free_frmr;
 752
 753        pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
 754                                         RPCSVC_MAXPAGES);
 755        if (IS_ERR(pl))
 756                goto err_free_mr;
 757
 758        frmr->mr = mr;
 759        frmr->page_list = pl;
 760        INIT_LIST_HEAD(&frmr->frmr_list);
 761        return frmr;
 762
 763 err_free_mr:
 764        ib_dereg_mr(mr);
 765 err_free_frmr:
 766        kfree(frmr);
 767 err:
 768        return ERR_PTR(-ENOMEM);
 769}
 770
 771static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
 772{
 773        struct svc_rdma_fastreg_mr *frmr;
 774
 775        while (!list_empty(&xprt->sc_frmr_q)) {
 776                frmr = list_entry(xprt->sc_frmr_q.next,
 777                                  struct svc_rdma_fastreg_mr, frmr_list);
 778                list_del_init(&frmr->frmr_list);
 779                ib_dereg_mr(frmr->mr);
 780                ib_free_fast_reg_page_list(frmr->page_list);
 781                kfree(frmr);
 782        }
 783}
 784
 785struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
 786{
 787        struct svc_rdma_fastreg_mr *frmr = NULL;
 788
 789        spin_lock_bh(&rdma->sc_frmr_q_lock);
 790        if (!list_empty(&rdma->sc_frmr_q)) {
 791                frmr = list_entry(rdma->sc_frmr_q.next,
 792                                  struct svc_rdma_fastreg_mr, frmr_list);
 793                list_del_init(&frmr->frmr_list);
 794                frmr->map_len = 0;
 795                frmr->page_list_len = 0;
 796        }
 797        spin_unlock_bh(&rdma->sc_frmr_q_lock);
 798        if (frmr)
 799                return frmr;
 800
 801        return rdma_alloc_frmr(rdma);
 802}
 803
 804static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
 805                           struct svc_rdma_fastreg_mr *frmr)
 806{
 807        int page_no;
 808        for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
 809                dma_addr_t addr = frmr->page_list->page_list[page_no];
 810                if (ib_dma_mapping_error(frmr->mr->device, addr))
 811                        continue;
 812                atomic_dec(&xprt->sc_dma_used);
 813                ib_dma_unmap_page(frmr->mr->device, addr, PAGE_SIZE,
 814                                  frmr->direction);
 815        }
 816}
 817
 818void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
 819                       struct svc_rdma_fastreg_mr *frmr)
 820{
 821        if (frmr) {
 822                frmr_unmap_dma(rdma, frmr);
 823                spin_lock_bh(&rdma->sc_frmr_q_lock);
 824                BUG_ON(!list_empty(&frmr->frmr_list));
 825                list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
 826                spin_unlock_bh(&rdma->sc_frmr_q_lock);
 827        }
 828}
 829
 830/*
 831 * This is the xpo_recvfrom function for listening endpoints. Its
 832 * purpose is to accept incoming connections. The CMA callback handler
 833 * has already created a new transport and attached it to the new CMA
 834 * ID.
 835 *
 836 * There is a queue of pending connections hung on the listening
 837 * transport. This queue contains the new svc_xprt structure. This
 838 * function takes svc_xprt structures off the accept_q and completes
 839 * the connection.
 840 */
 841static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
 842{
 843        struct svcxprt_rdma *listen_rdma;
 844        struct svcxprt_rdma *newxprt = NULL;
 845        struct rdma_conn_param conn_param;
 846        struct ib_qp_init_attr qp_attr;
 847        struct ib_device_attr devattr;
 848        int uninitialized_var(dma_mr_acc);
 849        int need_dma_mr;
 850        int ret;
 851        int i;
 852
 853        listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
 854        clear_bit(XPT_CONN, &xprt->xpt_flags);
 855        /* Get the next entry off the accept list */
 856        spin_lock_bh(&listen_rdma->sc_lock);
 857        if (!list_empty(&listen_rdma->sc_accept_q)) {
 858                newxprt = list_entry(listen_rdma->sc_accept_q.next,
 859                                     struct svcxprt_rdma, sc_accept_q);
 860                list_del_init(&newxprt->sc_accept_q);
 861        }
 862        if (!list_empty(&listen_rdma->sc_accept_q))
 863                set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
 864        spin_unlock_bh(&listen_rdma->sc_lock);
 865        if (!newxprt)
 866                return NULL;
 867
 868        dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
 869                newxprt, newxprt->sc_cm_id);
 870
 871        ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
 872        if (ret) {
 873                dprintk("svcrdma: could not query device attributes on "
 874                        "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
 875                goto errout;
 876        }
 877
 878        /* Qualify the transport resource defaults with the
 879         * capabilities of this particular device */
 880        newxprt->sc_max_sge = min((size_t)devattr.max_sge,
 881                                  (size_t)RPCSVC_MAXPAGES);
 882        newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
 883                                   (size_t)svcrdma_max_requests);
 884        newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
 885
 886        /*
 887         * Limit ORD based on client limit, local device limit, and
 888         * configured svcrdma limit.
 889         */
 890        newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
 891        newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
 892
 893        newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
 894        if (IS_ERR(newxprt->sc_pd)) {
 895                dprintk("svcrdma: error creating PD for connect request\n");
 896                goto errout;
 897        }
 898        newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
 899                                         sq_comp_handler,
 900                                         cq_event_handler,
 901                                         newxprt,
 902                                         newxprt->sc_sq_depth,
 903                                         0);
 904        if (IS_ERR(newxprt->sc_sq_cq)) {
 905                dprintk("svcrdma: error creating SQ CQ for connect request\n");
 906                goto errout;
 907        }
 908        newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
 909                                         rq_comp_handler,
 910                                         cq_event_handler,
 911                                         newxprt,
 912                                         newxprt->sc_max_requests,
 913                                         0);
 914        if (IS_ERR(newxprt->sc_rq_cq)) {
 915                dprintk("svcrdma: error creating RQ CQ for connect request\n");
 916                goto errout;
 917        }
 918
 919        memset(&qp_attr, 0, sizeof qp_attr);
 920        qp_attr.event_handler = qp_event_handler;
 921        qp_attr.qp_context = &newxprt->sc_xprt;
 922        qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
 923        qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
 924        qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
 925        qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
 926        qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
 927        qp_attr.qp_type = IB_QPT_RC;
 928        qp_attr.send_cq = newxprt->sc_sq_cq;
 929        qp_attr.recv_cq = newxprt->sc_rq_cq;
 930        dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
 931                "    cm_id->device=%p, sc_pd->device=%p\n"
 932                "    cap.max_send_wr = %d\n"
 933                "    cap.max_recv_wr = %d\n"
 934                "    cap.max_send_sge = %d\n"
 935                "    cap.max_recv_sge = %d\n",
 936                newxprt->sc_cm_id, newxprt->sc_pd,
 937                newxprt->sc_cm_id->device, newxprt->sc_pd->device,
 938                qp_attr.cap.max_send_wr,
 939                qp_attr.cap.max_recv_wr,
 940                qp_attr.cap.max_send_sge,
 941                qp_attr.cap.max_recv_sge);
 942
 943        ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
 944        if (ret) {
 945                /*
 946                 * XXX: This is a hack. We need a xx_request_qp interface
 947                 * that will adjust the qp_attr's with a best-effort
 948                 * number
 949                 */
 950                qp_attr.cap.max_send_sge -= 2;
 951                qp_attr.cap.max_recv_sge -= 2;
 952                ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
 953                                     &qp_attr);
 954                if (ret) {
 955                        dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
 956                        goto errout;
 957                }
 958                newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
 959                newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
 960                newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
 961                newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
 962        }
 963        newxprt->sc_qp = newxprt->sc_cm_id->qp;
 964
 965        /*
 966         * Use the most secure set of MR resources based on the
 967         * transport type and available memory management features in
 968         * the device. Here's the table implemented below:
 969         *
 970         *              Fast    Global  DMA     Remote WR
 971         *              Reg     LKEY    MR      Access
 972         *              Sup'd   Sup'd   Needed  Needed
 973         *
 974         * IWARP        N       N       Y       Y
 975         *              N       Y       Y       Y
 976         *              Y       N       Y       N
 977         *              Y       Y       N       -
 978         *
 979         * IB           N       N       Y       N
 980         *              N       Y       N       -
 981         *              Y       N       Y       N
 982         *              Y       Y       N       -
 983         *
 984         * NB:  iWARP requires remote write access for the data sink
 985         *      of an RDMA_READ. IB does not.
 986         */
 987        if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
 988                newxprt->sc_frmr_pg_list_len =
 989                        devattr.max_fast_reg_page_list_len;
 990                newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
 991        }
 992
 993        /*
 994         * Determine if a DMA MR is required and if so, what privs are required
 995         */
 996        switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
 997        case RDMA_TRANSPORT_IWARP:
 998                newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
 999                if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
1000                        need_dma_mr = 1;
1001                        dma_mr_acc =
1002                                (IB_ACCESS_LOCAL_WRITE |
1003                                 IB_ACCESS_REMOTE_WRITE);
1004                } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
1005                        need_dma_mr = 1;
1006                        dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1007                } else
1008                        need_dma_mr = 0;
1009                break;
1010        case RDMA_TRANSPORT_IB:
1011                if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
1012                        need_dma_mr = 1;
1013                        dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1014                } else if (!(devattr.device_cap_flags &
1015                             IB_DEVICE_LOCAL_DMA_LKEY)) {
1016                        need_dma_mr = 1;
1017                        dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1018                } else
1019                        need_dma_mr = 0;
1020                break;
1021        default:
1022                goto errout;
1023        }
1024
1025        /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1026        if (need_dma_mr) {
1027                /* Register all of physical memory */
1028                newxprt->sc_phys_mr =
1029                        ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1030                if (IS_ERR(newxprt->sc_phys_mr)) {
1031                        dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1032                                ret);
1033                        goto errout;
1034                }
1035                newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1036        } else
1037                newxprt->sc_dma_lkey =
1038                        newxprt->sc_cm_id->device->local_dma_lkey;
1039
1040        /* Post receive buffers */
1041        for (i = 0; i < newxprt->sc_max_requests; i++) {
1042                ret = svc_rdma_post_recv(newxprt);
1043                if (ret) {
1044                        dprintk("svcrdma: failure posting receive buffers\n");
1045                        goto errout;
1046                }
1047        }
1048
1049        /* Swap out the handler */
1050        newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1051
1052        /*
1053         * Arm the CQs for the SQ and RQ before accepting so we can't
1054         * miss the first message
1055         */
1056        ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1057        ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1058
1059        /* Accept Connection */
1060        set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1061        memset(&conn_param, 0, sizeof conn_param);
1062        conn_param.responder_resources = 0;
1063        conn_param.initiator_depth = newxprt->sc_ord;
1064        ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1065        if (ret) {
1066                dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1067                       ret);
1068                goto errout;
1069        }
1070
1071        dprintk("svcrdma: new connection %p accepted with the following "
1072                "attributes:\n"
1073                "    local_ip        : %pI4\n"
1074                "    local_port      : %d\n"
1075                "    remote_ip       : %pI4\n"
1076                "    remote_port     : %d\n"
1077                "    max_sge         : %d\n"
1078                "    sq_depth        : %d\n"
1079                "    max_requests    : %d\n"
1080                "    ord             : %d\n",
1081                newxprt,
1082                &((struct sockaddr_in *)&newxprt->sc_cm_id->
1083                         route.addr.src_addr)->sin_addr.s_addr,
1084                ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1085                       route.addr.src_addr)->sin_port),
1086                &((struct sockaddr_in *)&newxprt->sc_cm_id->
1087                         route.addr.dst_addr)->sin_addr.s_addr,
1088                ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1089                       route.addr.dst_addr)->sin_port),
1090                newxprt->sc_max_sge,
1091                newxprt->sc_sq_depth,
1092                newxprt->sc_max_requests,
1093                newxprt->sc_ord);
1094
1095        return &newxprt->sc_xprt;
1096
1097 errout:
1098        dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1099        /* Take a reference in case the DTO handler runs */
1100        svc_xprt_get(&newxprt->sc_xprt);
1101        if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1102                ib_destroy_qp(newxprt->sc_qp);
1103        rdma_destroy_id(newxprt->sc_cm_id);
1104        /* This call to put will destroy the transport */
1105        svc_xprt_put(&newxprt->sc_xprt);
1106        return NULL;
1107}
1108
1109static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1110{
1111}
1112
1113/*
1114 * When connected, an svc_xprt has at least two references:
1115 *
1116 * - A reference held by the cm_id between the ESTABLISHED and
1117 *   DISCONNECTED events. If the remote peer disconnected first, this
1118 *   reference could be gone.
1119 *
1120 * - A reference held by the svc_recv code that called this function
1121 *   as part of close processing.
1122 *
1123 * At a minimum one references should still be held.
1124 */
1125static void svc_rdma_detach(struct svc_xprt *xprt)
1126{
1127        struct svcxprt_rdma *rdma =
1128                container_of(xprt, struct svcxprt_rdma, sc_xprt);
1129        dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1130
1131        /* Disconnect and flush posted WQE */
1132        rdma_disconnect(rdma->sc_cm_id);
1133}
1134
1135static void __svc_rdma_free(struct work_struct *work)
1136{
1137        struct svcxprt_rdma *rdma =
1138                container_of(work, struct svcxprt_rdma, sc_work);
1139        dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1140
1141        /* We should only be called from kref_put */
1142        BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1143
1144        /*
1145         * Destroy queued, but not processed read completions. Note
1146         * that this cleanup has to be done before destroying the
1147         * cm_id because the device ptr is needed to unmap the dma in
1148         * svc_rdma_put_context.
1149         */
1150        while (!list_empty(&rdma->sc_read_complete_q)) {
1151                struct svc_rdma_op_ctxt *ctxt;
1152                ctxt = list_entry(rdma->sc_read_complete_q.next,
1153                                  struct svc_rdma_op_ctxt,
1154                                  dto_q);
1155                list_del_init(&ctxt->dto_q);
1156                svc_rdma_put_context(ctxt, 1);
1157        }
1158
1159        /* Destroy queued, but not processed recv completions */
1160        while (!list_empty(&rdma->sc_rq_dto_q)) {
1161                struct svc_rdma_op_ctxt *ctxt;
1162                ctxt = list_entry(rdma->sc_rq_dto_q.next,
1163                                  struct svc_rdma_op_ctxt,
1164                                  dto_q);
1165                list_del_init(&ctxt->dto_q);
1166                svc_rdma_put_context(ctxt, 1);
1167        }
1168
1169        /* Warn if we leaked a resource or under-referenced */
1170        WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1171        WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1172
1173        /* De-allocate fastreg mr */
1174        rdma_dealloc_frmr_q(rdma);
1175
1176        /* Destroy the QP if present (not a listener) */
1177        if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1178                ib_destroy_qp(rdma->sc_qp);
1179
1180        if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1181                ib_destroy_cq(rdma->sc_sq_cq);
1182
1183        if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1184                ib_destroy_cq(rdma->sc_rq_cq);
1185
1186        if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1187                ib_dereg_mr(rdma->sc_phys_mr);
1188
1189        if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1190                ib_dealloc_pd(rdma->sc_pd);
1191
1192        /* Destroy the CM ID */
1193        rdma_destroy_id(rdma->sc_cm_id);
1194
1195        kfree(rdma);
1196}
1197
1198static void svc_rdma_free(struct svc_xprt *xprt)
1199{
1200        struct svcxprt_rdma *rdma =
1201                container_of(xprt, struct svcxprt_rdma, sc_xprt);
1202        INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1203        queue_work(svc_rdma_wq, &rdma->sc_work);
1204}
1205
1206static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1207{
1208        struct svcxprt_rdma *rdma =
1209                container_of(xprt, struct svcxprt_rdma, sc_xprt);
1210
1211        /*
1212         * If there are already waiters on the SQ,
1213         * return false.
1214         */
1215        if (waitqueue_active(&rdma->sc_send_wait))
1216                return 0;
1217
1218        /* Otherwise return true. */
1219        return 1;
1220}
1221
1222static int svc_rdma_secure_port(struct svc_rqst *rqstp)
1223{
1224        return 1;
1225}
1226
1227/*
1228 * Attempt to register the kvec representing the RPC memory with the
1229 * device.
1230 *
1231 * Returns:
1232 *  NULL : The device does not support fastreg or there were no more
1233 *         fastreg mr.
1234 *  frmr : The kvec register request was successfully posted.
1235 *    <0 : An error was encountered attempting to register the kvec.
1236 */
1237int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1238                     struct svc_rdma_fastreg_mr *frmr)
1239{
1240        struct ib_send_wr fastreg_wr;
1241        u8 key;
1242
1243        /* Bump the key */
1244        key = (u8)(frmr->mr->lkey & 0x000000FF);
1245        ib_update_fast_reg_key(frmr->mr, ++key);
1246
1247        /* Prepare FASTREG WR */
1248        memset(&fastreg_wr, 0, sizeof fastreg_wr);
1249        fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1250        fastreg_wr.send_flags = IB_SEND_SIGNALED;
1251        fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1252        fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1253        fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1254        fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1255        fastreg_wr.wr.fast_reg.length = frmr->map_len;
1256        fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1257        fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1258        return svc_rdma_send(xprt, &fastreg_wr);
1259}
1260
1261int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1262{
1263        struct ib_send_wr *bad_wr, *n_wr;
1264        int wr_count;
1265        int i;
1266        int ret;
1267
1268        if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1269                return -ENOTCONN;
1270
1271        BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1272        wr_count = 1;
1273        for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1274                wr_count++;
1275
1276        /* If the SQ is full, wait until an SQ entry is available */
1277        while (1) {
1278                spin_lock_bh(&xprt->sc_lock);
1279                if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
1280                        spin_unlock_bh(&xprt->sc_lock);
1281                        atomic_inc(&rdma_stat_sq_starve);
1282
1283                        /* See if we can opportunistically reap SQ WR to make room */
1284                        sq_cq_reap(xprt);
1285
1286                        /* Wait until SQ WR available if SQ still full */
1287                        wait_event(xprt->sc_send_wait,
1288                                   atomic_read(&xprt->sc_sq_count) <
1289                                   xprt->sc_sq_depth);
1290                        if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1291                                return -ENOTCONN;
1292                        continue;
1293                }
1294                /* Take a transport ref for each WR posted */
1295                for (i = 0; i < wr_count; i++)
1296                        svc_xprt_get(&xprt->sc_xprt);
1297
1298                /* Bump used SQ WR count and post */
1299                atomic_add(wr_count, &xprt->sc_sq_count);
1300                ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1301                if (ret) {
1302                        set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1303                        atomic_sub(wr_count, &xprt->sc_sq_count);
1304                        for (i = 0; i < wr_count; i ++)
1305                                svc_xprt_put(&xprt->sc_xprt);
1306                        dprintk("svcrdma: failed to post SQ WR rc=%d, "
1307                               "sc_sq_count=%d, sc_sq_depth=%d\n",
1308                               ret, atomic_read(&xprt->sc_sq_count),
1309                               xprt->sc_sq_depth);
1310                }
1311                spin_unlock_bh(&xprt->sc_lock);
1312                if (ret)
1313                        wake_up(&xprt->sc_send_wait);
1314                break;
1315        }
1316        return ret;
1317}
1318
1319void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1320                         enum rpcrdma_errcode err)
1321{
1322        struct ib_send_wr err_wr;
1323        struct page *p;
1324        struct svc_rdma_op_ctxt *ctxt;
1325        u32 *va;
1326        int length;
1327        int ret;
1328
1329        p = svc_rdma_get_page();
1330        va = page_address(p);
1331
1332        /* XDR encode error */
1333        length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1334
1335        ctxt = svc_rdma_get_context(xprt);
1336        ctxt->direction = DMA_FROM_DEVICE;
1337        ctxt->count = 1;
1338        ctxt->pages[0] = p;
1339
1340        /* Prepare SGE for local address */
1341        ctxt->sge[0].addr = ib_dma_map_page(xprt->sc_cm_id->device,
1342                                            p, 0, length, DMA_FROM_DEVICE);
1343        if (ib_dma_mapping_error(xprt->sc_cm_id->device, ctxt->sge[0].addr)) {
1344                put_page(p);
1345                svc_rdma_put_context(ctxt, 1);
1346                return;
1347        }
1348        atomic_inc(&xprt->sc_dma_used);
1349        ctxt->sge[0].lkey = xprt->sc_dma_lkey;
1350        ctxt->sge[0].length = length;
1351
1352        /* Prepare SEND WR */
1353        memset(&err_wr, 0, sizeof err_wr);
1354        ctxt->wr_op = IB_WR_SEND;
1355        err_wr.wr_id = (unsigned long)ctxt;
1356        err_wr.sg_list = ctxt->sge;
1357        err_wr.num_sge = 1;
1358        err_wr.opcode = IB_WR_SEND;
1359        err_wr.send_flags = IB_SEND_SIGNALED;
1360
1361        /* Post It */
1362        ret = svc_rdma_send(xprt, &err_wr);
1363        if (ret) {
1364                dprintk("svcrdma: Error %d posting send for protocol error\n",
1365                        ret);
1366                svc_rdma_unmap_dma(ctxt);
1367                svc_rdma_put_context(ctxt, 1);
1368        }
1369}
1370