linux/drivers/infiniband/ulp/ipoib/ipoib_cm.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2006 Mellanox Technologies. 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 <rdma/ib_cm.h>
  34#include <net/dst.h>
  35#include <net/icmp.h>
  36#include <linux/icmpv6.h>
  37#include <linux/delay.h>
  38#include <linux/slab.h>
  39#include <linux/vmalloc.h>
  40#include <linux/moduleparam.h>
  41
  42#include "ipoib.h"
  43
  44int ipoib_max_conn_qp = 128;
  45
  46module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
  47MODULE_PARM_DESC(max_nonsrq_conn_qp,
  48                 "Max number of connected-mode QPs per interface "
  49                 "(applied only if shared receive queue is not available)");
  50
  51#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
  52static int data_debug_level;
  53
  54module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
  55MODULE_PARM_DESC(cm_data_debug_level,
  56                 "Enable data path debug tracing for connected mode if > 0");
  57#endif
  58
  59#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
  60
  61#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
  62#define IPOIB_CM_RX_TIMEOUT     (2 * 256 * HZ)
  63#define IPOIB_CM_RX_DELAY       (3 * 256 * HZ)
  64#define IPOIB_CM_RX_UPDATE_MASK (0x3)
  65
  66#define IPOIB_CM_RX_RESERVE     (ALIGN(IPOIB_HARD_LEN, 16) - IPOIB_ENCAP_LEN)
  67
  68static struct ib_qp_attr ipoib_cm_err_attr = {
  69        .qp_state = IB_QPS_ERR
  70};
  71
  72#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
  73
  74static struct ib_send_wr ipoib_cm_rx_drain_wr = {
  75        .opcode = IB_WR_SEND,
  76};
  77
  78static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
  79                               struct ib_cm_event *event);
  80
  81static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
  82                                  u64 mapping[IPOIB_CM_RX_SG])
  83{
  84        int i;
  85
  86        ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
  87
  88        for (i = 0; i < frags; ++i)
  89                ib_dma_unmap_page(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
  90}
  91
  92static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
  93{
  94        struct ipoib_dev_priv *priv = netdev_priv(dev);
  95        struct ib_recv_wr *bad_wr;
  96        int i, ret;
  97
  98        priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
  99
 100        for (i = 0; i < priv->cm.num_frags; ++i)
 101                priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
 102
 103        ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
 104        if (unlikely(ret)) {
 105                ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
 106                ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
 107                                      priv->cm.srq_ring[id].mapping);
 108                dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
 109                priv->cm.srq_ring[id].skb = NULL;
 110        }
 111
 112        return ret;
 113}
 114
 115static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
 116                                        struct ipoib_cm_rx *rx,
 117                                        struct ib_recv_wr *wr,
 118                                        struct ib_sge *sge, int id)
 119{
 120        struct ipoib_dev_priv *priv = netdev_priv(dev);
 121        struct ib_recv_wr *bad_wr;
 122        int i, ret;
 123
 124        wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
 125
 126        for (i = 0; i < IPOIB_CM_RX_SG; ++i)
 127                sge[i].addr = rx->rx_ring[id].mapping[i];
 128
 129        ret = ib_post_recv(rx->qp, wr, &bad_wr);
 130        if (unlikely(ret)) {
 131                ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
 132                ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
 133                                      rx->rx_ring[id].mapping);
 134                dev_kfree_skb_any(rx->rx_ring[id].skb);
 135                rx->rx_ring[id].skb = NULL;
 136        }
 137
 138        return ret;
 139}
 140
 141static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
 142                                             struct ipoib_cm_rx_buf *rx_ring,
 143                                             int id, int frags,
 144                                             u64 mapping[IPOIB_CM_RX_SG],
 145                                             gfp_t gfp)
 146{
 147        struct ipoib_dev_priv *priv = netdev_priv(dev);
 148        struct sk_buff *skb;
 149        int i;
 150
 151        skb = dev_alloc_skb(ALIGN(IPOIB_CM_HEAD_SIZE + IPOIB_PSEUDO_LEN, 16));
 152        if (unlikely(!skb))
 153                return NULL;
 154
 155        /*
 156         * IPoIB adds a IPOIB_ENCAP_LEN byte header, this will align the
 157         * IP header to a multiple of 16.
 158         */
 159        skb_reserve(skb, IPOIB_CM_RX_RESERVE);
 160
 161        mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
 162                                       DMA_FROM_DEVICE);
 163        if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
 164                dev_kfree_skb_any(skb);
 165                return NULL;
 166        }
 167
 168        for (i = 0; i < frags; i++) {
 169                struct page *page = alloc_page(gfp);
 170
 171                if (!page)
 172                        goto partial_error;
 173                skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
 174
 175                mapping[i + 1] = ib_dma_map_page(priv->ca, page,
 176                                                 0, PAGE_SIZE, DMA_FROM_DEVICE);
 177                if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
 178                        goto partial_error;
 179        }
 180
 181        rx_ring[id].skb = skb;
 182        return skb;
 183
 184partial_error:
 185
 186        ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
 187
 188        for (; i > 0; --i)
 189                ib_dma_unmap_page(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
 190
 191        dev_kfree_skb_any(skb);
 192        return NULL;
 193}
 194
 195static void ipoib_cm_free_rx_ring(struct net_device *dev,
 196                                  struct ipoib_cm_rx_buf *rx_ring)
 197{
 198        struct ipoib_dev_priv *priv = netdev_priv(dev);
 199        int i;
 200
 201        for (i = 0; i < ipoib_recvq_size; ++i)
 202                if (rx_ring[i].skb) {
 203                        ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
 204                                              rx_ring[i].mapping);
 205                        dev_kfree_skb_any(rx_ring[i].skb);
 206                }
 207
 208        vfree(rx_ring);
 209}
 210
 211static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
 212{
 213        struct ib_send_wr *bad_wr;
 214        struct ipoib_cm_rx *p;
 215
 216        /* We only reserved 1 extra slot in CQ for drain WRs, so
 217         * make sure we have at most 1 outstanding WR. */
 218        if (list_empty(&priv->cm.rx_flush_list) ||
 219            !list_empty(&priv->cm.rx_drain_list))
 220                return;
 221
 222        /*
 223         * QPs on flush list are error state.  This way, a "flush
 224         * error" WC will be immediately generated for each WR we post.
 225         */
 226        p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
 227        ipoib_cm_rx_drain_wr.wr_id = IPOIB_CM_RX_DRAIN_WRID;
 228        if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
 229                ipoib_warn(priv, "failed to post drain wr\n");
 230
 231        list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
 232}
 233
 234static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
 235{
 236        struct ipoib_cm_rx *p = ctx;
 237        struct ipoib_dev_priv *priv = netdev_priv(p->dev);
 238        unsigned long flags;
 239
 240        if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
 241                return;
 242
 243        spin_lock_irqsave(&priv->lock, flags);
 244        list_move(&p->list, &priv->cm.rx_flush_list);
 245        p->state = IPOIB_CM_RX_FLUSH;
 246        ipoib_cm_start_rx_drain(priv);
 247        spin_unlock_irqrestore(&priv->lock, flags);
 248}
 249
 250static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
 251                                           struct ipoib_cm_rx *p)
 252{
 253        struct ipoib_dev_priv *priv = netdev_priv(dev);
 254        struct ib_qp_init_attr attr = {
 255                .event_handler = ipoib_cm_rx_event_handler,
 256                .send_cq = priv->recv_cq, /* For drain WR */
 257                .recv_cq = priv->recv_cq,
 258                .srq = priv->cm.srq,
 259                .cap.max_send_wr = 1, /* For drain WR */
 260                .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
 261                .sq_sig_type = IB_SIGNAL_ALL_WR,
 262                .qp_type = IB_QPT_RC,
 263                .qp_context = p,
 264        };
 265
 266        if (!ipoib_cm_has_srq(dev)) {
 267                attr.cap.max_recv_wr  = ipoib_recvq_size;
 268                attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
 269        }
 270
 271        return ib_create_qp(priv->pd, &attr);
 272}
 273
 274static int ipoib_cm_modify_rx_qp(struct net_device *dev,
 275                                 struct ib_cm_id *cm_id, struct ib_qp *qp,
 276                                 unsigned psn)
 277{
 278        struct ipoib_dev_priv *priv = netdev_priv(dev);
 279        struct ib_qp_attr qp_attr;
 280        int qp_attr_mask, ret;
 281
 282        qp_attr.qp_state = IB_QPS_INIT;
 283        ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
 284        if (ret) {
 285                ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
 286                return ret;
 287        }
 288        ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
 289        if (ret) {
 290                ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
 291                return ret;
 292        }
 293        qp_attr.qp_state = IB_QPS_RTR;
 294        ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
 295        if (ret) {
 296                ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
 297                return ret;
 298        }
 299        qp_attr.rq_psn = psn;
 300        ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
 301        if (ret) {
 302                ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
 303                return ret;
 304        }
 305
 306        /*
 307         * Current Mellanox HCA firmware won't generate completions
 308         * with error for drain WRs unless the QP has been moved to
 309         * RTS first. This work-around leaves a window where a QP has
 310         * moved to error asynchronously, but this will eventually get
 311         * fixed in firmware, so let's not error out if modify QP
 312         * fails.
 313         */
 314        qp_attr.qp_state = IB_QPS_RTS;
 315        ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
 316        if (ret) {
 317                ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
 318                return 0;
 319        }
 320        ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
 321        if (ret) {
 322                ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
 323                return 0;
 324        }
 325
 326        return 0;
 327}
 328
 329static void ipoib_cm_init_rx_wr(struct net_device *dev,
 330                                struct ib_recv_wr *wr,
 331                                struct ib_sge *sge)
 332{
 333        struct ipoib_dev_priv *priv = netdev_priv(dev);
 334        int i;
 335
 336        for (i = 0; i < priv->cm.num_frags; ++i)
 337                sge[i].lkey = priv->pd->local_dma_lkey;
 338
 339        sge[0].length = IPOIB_CM_HEAD_SIZE;
 340        for (i = 1; i < priv->cm.num_frags; ++i)
 341                sge[i].length = PAGE_SIZE;
 342
 343        wr->next    = NULL;
 344        wr->sg_list = sge;
 345        wr->num_sge = priv->cm.num_frags;
 346}
 347
 348static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
 349                                   struct ipoib_cm_rx *rx)
 350{
 351        struct ipoib_dev_priv *priv = netdev_priv(dev);
 352        struct {
 353                struct ib_recv_wr wr;
 354                struct ib_sge sge[IPOIB_CM_RX_SG];
 355        } *t;
 356        int ret;
 357        int i;
 358
 359        rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
 360        if (!rx->rx_ring) {
 361                printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
 362                       priv->ca->name, ipoib_recvq_size);
 363                return -ENOMEM;
 364        }
 365
 366        t = kmalloc(sizeof *t, GFP_KERNEL);
 367        if (!t) {
 368                ret = -ENOMEM;
 369                goto err_free;
 370        }
 371
 372        ipoib_cm_init_rx_wr(dev, &t->wr, t->sge);
 373
 374        spin_lock_irq(&priv->lock);
 375
 376        if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
 377                spin_unlock_irq(&priv->lock);
 378                ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
 379                ret = -EINVAL;
 380                goto err_free;
 381        } else
 382                ++priv->cm.nonsrq_conn_qp;
 383
 384        spin_unlock_irq(&priv->lock);
 385
 386        for (i = 0; i < ipoib_recvq_size; ++i) {
 387                if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
 388                                           rx->rx_ring[i].mapping,
 389                                           GFP_KERNEL)) {
 390                        ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
 391                        ret = -ENOMEM;
 392                        goto err_count;
 393                }
 394                ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i);
 395                if (ret) {
 396                        ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
 397                                   "failed for buf %d\n", i);
 398                        ret = -EIO;
 399                        goto err_count;
 400                }
 401        }
 402
 403        rx->recv_count = ipoib_recvq_size;
 404
 405        kfree(t);
 406
 407        return 0;
 408
 409err_count:
 410        spin_lock_irq(&priv->lock);
 411        --priv->cm.nonsrq_conn_qp;
 412        spin_unlock_irq(&priv->lock);
 413
 414err_free:
 415        kfree(t);
 416        ipoib_cm_free_rx_ring(dev, rx->rx_ring);
 417
 418        return ret;
 419}
 420
 421static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
 422                             struct ib_qp *qp, struct ib_cm_req_event_param *req,
 423                             unsigned psn)
 424{
 425        struct ipoib_dev_priv *priv = netdev_priv(dev);
 426        struct ipoib_cm_data data = {};
 427        struct ib_cm_rep_param rep = {};
 428
 429        data.qpn = cpu_to_be32(priv->qp->qp_num);
 430        data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
 431
 432        rep.private_data = &data;
 433        rep.private_data_len = sizeof data;
 434        rep.flow_control = 0;
 435        rep.rnr_retry_count = req->rnr_retry_count;
 436        rep.srq = ipoib_cm_has_srq(dev);
 437        rep.qp_num = qp->qp_num;
 438        rep.starting_psn = psn;
 439        return ib_send_cm_rep(cm_id, &rep);
 440}
 441
 442static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
 443{
 444        struct net_device *dev = cm_id->context;
 445        struct ipoib_dev_priv *priv = netdev_priv(dev);
 446        struct ipoib_cm_rx *p;
 447        unsigned psn;
 448        int ret;
 449
 450        ipoib_dbg(priv, "REQ arrived\n");
 451        p = kzalloc(sizeof *p, GFP_KERNEL);
 452        if (!p)
 453                return -ENOMEM;
 454        p->dev = dev;
 455        p->id = cm_id;
 456        cm_id->context = p;
 457        p->state = IPOIB_CM_RX_LIVE;
 458        p->jiffies = jiffies;
 459        INIT_LIST_HEAD(&p->list);
 460
 461        p->qp = ipoib_cm_create_rx_qp(dev, p);
 462        if (IS_ERR(p->qp)) {
 463                ret = PTR_ERR(p->qp);
 464                goto err_qp;
 465        }
 466
 467        psn = prandom_u32() & 0xffffff;
 468        ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
 469        if (ret)
 470                goto err_modify;
 471
 472        if (!ipoib_cm_has_srq(dev)) {
 473                ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
 474                if (ret)
 475                        goto err_modify;
 476        }
 477
 478        spin_lock_irq(&priv->lock);
 479        queue_delayed_work(priv->wq,
 480                           &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
 481        /* Add this entry to passive ids list head, but do not re-add it
 482         * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
 483        p->jiffies = jiffies;
 484        if (p->state == IPOIB_CM_RX_LIVE)
 485                list_move(&p->list, &priv->cm.passive_ids);
 486        spin_unlock_irq(&priv->lock);
 487
 488        ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
 489        if (ret) {
 490                ipoib_warn(priv, "failed to send REP: %d\n", ret);
 491                if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
 492                        ipoib_warn(priv, "unable to move qp to error state\n");
 493        }
 494        return 0;
 495
 496err_modify:
 497        ib_destroy_qp(p->qp);
 498err_qp:
 499        kfree(p);
 500        return ret;
 501}
 502
 503static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
 504                               struct ib_cm_event *event)
 505{
 506        struct ipoib_cm_rx *p;
 507        struct ipoib_dev_priv *priv;
 508
 509        switch (event->event) {
 510        case IB_CM_REQ_RECEIVED:
 511                return ipoib_cm_req_handler(cm_id, event);
 512        case IB_CM_DREQ_RECEIVED:
 513                p = cm_id->context;
 514                ib_send_cm_drep(cm_id, NULL, 0);
 515                /* Fall through */
 516        case IB_CM_REJ_RECEIVED:
 517                p = cm_id->context;
 518                priv = netdev_priv(p->dev);
 519                if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
 520                        ipoib_warn(priv, "unable to move qp to error state\n");
 521                /* Fall through */
 522        default:
 523                return 0;
 524        }
 525}
 526/* Adjust length of skb with fragments to match received data */
 527static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
 528                          unsigned int length, struct sk_buff *toskb)
 529{
 530        int i, num_frags;
 531        unsigned int size;
 532
 533        /* put header into skb */
 534        size = min(length, hdr_space);
 535        skb->tail += size;
 536        skb->len += size;
 537        length -= size;
 538
 539        num_frags = skb_shinfo(skb)->nr_frags;
 540        for (i = 0; i < num_frags; i++) {
 541                skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 542
 543                if (length == 0) {
 544                        /* don't need this page */
 545                        skb_fill_page_desc(toskb, i, skb_frag_page(frag),
 546                                           0, PAGE_SIZE);
 547                        --skb_shinfo(skb)->nr_frags;
 548                } else {
 549                        size = min(length, (unsigned) PAGE_SIZE);
 550
 551                        skb_frag_size_set(frag, size);
 552                        skb->data_len += size;
 553                        skb->truesize += size;
 554                        skb->len += size;
 555                        length -= size;
 556                }
 557        }
 558}
 559
 560void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
 561{
 562        struct ipoib_dev_priv *priv = netdev_priv(dev);
 563        struct ipoib_cm_rx_buf *rx_ring;
 564        unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
 565        struct sk_buff *skb, *newskb;
 566        struct ipoib_cm_rx *p;
 567        unsigned long flags;
 568        u64 mapping[IPOIB_CM_RX_SG];
 569        int frags;
 570        int has_srq;
 571        struct sk_buff *small_skb;
 572
 573        ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
 574                       wr_id, wc->status);
 575
 576        if (unlikely(wr_id >= ipoib_recvq_size)) {
 577                if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
 578                        spin_lock_irqsave(&priv->lock, flags);
 579                        list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
 580                        ipoib_cm_start_rx_drain(priv);
 581                        queue_work(priv->wq, &priv->cm.rx_reap_task);
 582                        spin_unlock_irqrestore(&priv->lock, flags);
 583                } else
 584                        ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
 585                                   wr_id, ipoib_recvq_size);
 586                return;
 587        }
 588
 589        p = wc->qp->qp_context;
 590
 591        has_srq = ipoib_cm_has_srq(dev);
 592        rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
 593
 594        skb = rx_ring[wr_id].skb;
 595
 596        if (unlikely(wc->status != IB_WC_SUCCESS)) {
 597                ipoib_dbg(priv, "cm recv error "
 598                           "(status=%d, wrid=%d vend_err %x)\n",
 599                           wc->status, wr_id, wc->vendor_err);
 600                ++dev->stats.rx_dropped;
 601                if (has_srq)
 602                        goto repost;
 603                else {
 604                        if (!--p->recv_count) {
 605                                spin_lock_irqsave(&priv->lock, flags);
 606                                list_move(&p->list, &priv->cm.rx_reap_list);
 607                                spin_unlock_irqrestore(&priv->lock, flags);
 608                                queue_work(priv->wq, &priv->cm.rx_reap_task);
 609                        }
 610                        return;
 611                }
 612        }
 613
 614        if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
 615                if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
 616                        spin_lock_irqsave(&priv->lock, flags);
 617                        p->jiffies = jiffies;
 618                        /* Move this entry to list head, but do not re-add it
 619                         * if it has been moved out of list. */
 620                        if (p->state == IPOIB_CM_RX_LIVE)
 621                                list_move(&p->list, &priv->cm.passive_ids);
 622                        spin_unlock_irqrestore(&priv->lock, flags);
 623                }
 624        }
 625
 626        if (wc->byte_len < IPOIB_CM_COPYBREAK) {
 627                int dlen = wc->byte_len;
 628
 629                small_skb = dev_alloc_skb(dlen + IPOIB_CM_RX_RESERVE);
 630                if (small_skb) {
 631                        skb_reserve(small_skb, IPOIB_CM_RX_RESERVE);
 632                        ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
 633                                                   dlen, DMA_FROM_DEVICE);
 634                        skb_copy_from_linear_data(skb, small_skb->data, dlen);
 635                        ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
 636                                                      dlen, DMA_FROM_DEVICE);
 637                        skb_put(small_skb, dlen);
 638                        skb = small_skb;
 639                        goto copied;
 640                }
 641        }
 642
 643        frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
 644                                              (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
 645
 646        newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags,
 647                                       mapping, GFP_ATOMIC);
 648        if (unlikely(!newskb)) {
 649                /*
 650                 * If we can't allocate a new RX buffer, dump
 651                 * this packet and reuse the old buffer.
 652                 */
 653                ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
 654                ++dev->stats.rx_dropped;
 655                goto repost;
 656        }
 657
 658        ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
 659        memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
 660
 661        ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
 662                       wc->byte_len, wc->slid);
 663
 664        skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
 665
 666copied:
 667        skb->protocol = ((struct ipoib_header *) skb->data)->proto;
 668        skb_add_pseudo_hdr(skb);
 669
 670        ++dev->stats.rx_packets;
 671        dev->stats.rx_bytes += skb->len;
 672
 673        skb->dev = dev;
 674        /* XXX get correct PACKET_ type here */
 675        skb->pkt_type = PACKET_HOST;
 676        netif_receive_skb(skb);
 677
 678repost:
 679        if (has_srq) {
 680                if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
 681                        ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
 682                                   "for buf %d\n", wr_id);
 683        } else {
 684                if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p,
 685                                                          &priv->cm.rx_wr,
 686                                                          priv->cm.rx_sge,
 687                                                          wr_id))) {
 688                        --p->recv_count;
 689                        ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
 690                                   "for buf %d\n", wr_id);
 691                }
 692        }
 693}
 694
 695static inline int post_send(struct ipoib_dev_priv *priv,
 696                            struct ipoib_cm_tx *tx,
 697                            unsigned int wr_id,
 698                            struct ipoib_tx_buf *tx_req)
 699{
 700        struct ib_send_wr *bad_wr;
 701
 702        ipoib_build_sge(priv, tx_req);
 703
 704        priv->tx_wr.wr.wr_id    = wr_id | IPOIB_OP_CM;
 705
 706        return ib_post_send(tx->qp, &priv->tx_wr.wr, &bad_wr);
 707}
 708
 709void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
 710{
 711        struct ipoib_dev_priv *priv = netdev_priv(dev);
 712        struct ipoib_tx_buf *tx_req;
 713        int rc;
 714        unsigned usable_sge = tx->max_send_sge - !!skb_headlen(skb);
 715
 716        if (unlikely(skb->len > tx->mtu)) {
 717                ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
 718                           skb->len, tx->mtu);
 719                ++dev->stats.tx_dropped;
 720                ++dev->stats.tx_errors;
 721                ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
 722                return;
 723        }
 724        if (skb_shinfo(skb)->nr_frags > usable_sge) {
 725                if (skb_linearize(skb) < 0) {
 726                        ipoib_warn(priv, "skb could not be linearized\n");
 727                        ++dev->stats.tx_dropped;
 728                        ++dev->stats.tx_errors;
 729                        dev_kfree_skb_any(skb);
 730                        return;
 731                }
 732                /* Does skb_linearize return ok without reducing nr_frags? */
 733                if (skb_shinfo(skb)->nr_frags > usable_sge) {
 734                        ipoib_warn(priv, "too many frags after skb linearize\n");
 735                        ++dev->stats.tx_dropped;
 736                        ++dev->stats.tx_errors;
 737                        dev_kfree_skb_any(skb);
 738                        return;
 739                }
 740        }
 741        ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
 742                       tx->tx_head, skb->len, tx->qp->qp_num);
 743
 744        /*
 745         * We put the skb into the tx_ring _before_ we call post_send()
 746         * because it's entirely possible that the completion handler will
 747         * run before we execute anything after the post_send().  That
 748         * means we have to make sure everything is properly recorded and
 749         * our state is consistent before we call post_send().
 750         */
 751        tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
 752        tx_req->skb = skb;
 753
 754        if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
 755                ++dev->stats.tx_errors;
 756                dev_kfree_skb_any(skb);
 757                return;
 758        }
 759
 760        skb_orphan(skb);
 761        skb_dst_drop(skb);
 762
 763        rc = post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1), tx_req);
 764        if (unlikely(rc)) {
 765                ipoib_warn(priv, "post_send failed, error %d\n", rc);
 766                ++dev->stats.tx_errors;
 767                ipoib_dma_unmap_tx(priv, tx_req);
 768                dev_kfree_skb_any(skb);
 769        } else {
 770                netif_trans_update(dev);
 771                ++tx->tx_head;
 772
 773                if (++priv->tx_outstanding == ipoib_sendq_size) {
 774                        ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
 775                                  tx->qp->qp_num);
 776                        netif_stop_queue(dev);
 777                        rc = ib_req_notify_cq(priv->send_cq,
 778                                IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
 779                        if (rc < 0)
 780                                ipoib_warn(priv, "request notify on send CQ failed\n");
 781                        else if (rc)
 782                                ipoib_send_comp_handler(priv->send_cq, dev);
 783                }
 784        }
 785}
 786
 787void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
 788{
 789        struct ipoib_dev_priv *priv = netdev_priv(dev);
 790        struct ipoib_cm_tx *tx = wc->qp->qp_context;
 791        unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
 792        struct ipoib_tx_buf *tx_req;
 793        unsigned long flags;
 794
 795        ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
 796                       wr_id, wc->status);
 797
 798        if (unlikely(wr_id >= ipoib_sendq_size)) {
 799                ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
 800                           wr_id, ipoib_sendq_size);
 801                return;
 802        }
 803
 804        tx_req = &tx->tx_ring[wr_id];
 805
 806        ipoib_dma_unmap_tx(priv, tx_req);
 807
 808        /* FIXME: is this right? Shouldn't we only increment on success? */
 809        ++dev->stats.tx_packets;
 810        dev->stats.tx_bytes += tx_req->skb->len;
 811
 812        dev_kfree_skb_any(tx_req->skb);
 813
 814        netif_tx_lock(dev);
 815
 816        ++tx->tx_tail;
 817        if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
 818            netif_queue_stopped(dev) &&
 819            test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
 820                netif_wake_queue(dev);
 821
 822        if (wc->status != IB_WC_SUCCESS &&
 823            wc->status != IB_WC_WR_FLUSH_ERR) {
 824                struct ipoib_neigh *neigh;
 825
 826                ipoib_dbg(priv, "failed cm send event "
 827                           "(status=%d, wrid=%d vend_err %x)\n",
 828                           wc->status, wr_id, wc->vendor_err);
 829
 830                spin_lock_irqsave(&priv->lock, flags);
 831                neigh = tx->neigh;
 832
 833                if (neigh) {
 834                        neigh->cm = NULL;
 835                        ipoib_neigh_free(neigh);
 836
 837                        tx->neigh = NULL;
 838                }
 839
 840                if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
 841                        list_move(&tx->list, &priv->cm.reap_list);
 842                        queue_work(priv->wq, &priv->cm.reap_task);
 843                }
 844
 845                clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
 846
 847                spin_unlock_irqrestore(&priv->lock, flags);
 848        }
 849
 850        netif_tx_unlock(dev);
 851}
 852
 853int ipoib_cm_dev_open(struct net_device *dev)
 854{
 855        struct ipoib_dev_priv *priv = netdev_priv(dev);
 856        int ret;
 857
 858        if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
 859                return 0;
 860
 861        priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
 862        if (IS_ERR(priv->cm.id)) {
 863                printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
 864                ret = PTR_ERR(priv->cm.id);
 865                goto err_cm;
 866        }
 867
 868        ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
 869                           0);
 870        if (ret) {
 871                printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
 872                       IPOIB_CM_IETF_ID | priv->qp->qp_num);
 873                goto err_listen;
 874        }
 875
 876        return 0;
 877
 878err_listen:
 879        ib_destroy_cm_id(priv->cm.id);
 880err_cm:
 881        priv->cm.id = NULL;
 882        return ret;
 883}
 884
 885static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
 886{
 887        struct ipoib_dev_priv *priv = netdev_priv(dev);
 888        struct ipoib_cm_rx *rx, *n;
 889        LIST_HEAD(list);
 890
 891        spin_lock_irq(&priv->lock);
 892        list_splice_init(&priv->cm.rx_reap_list, &list);
 893        spin_unlock_irq(&priv->lock);
 894
 895        list_for_each_entry_safe(rx, n, &list, list) {
 896                ib_destroy_cm_id(rx->id);
 897                ib_destroy_qp(rx->qp);
 898                if (!ipoib_cm_has_srq(dev)) {
 899                        ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
 900                        spin_lock_irq(&priv->lock);
 901                        --priv->cm.nonsrq_conn_qp;
 902                        spin_unlock_irq(&priv->lock);
 903                }
 904                kfree(rx);
 905        }
 906}
 907
 908void ipoib_cm_dev_stop(struct net_device *dev)
 909{
 910        struct ipoib_dev_priv *priv = netdev_priv(dev);
 911        struct ipoib_cm_rx *p;
 912        unsigned long begin;
 913        int ret;
 914
 915        if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
 916                return;
 917
 918        ib_destroy_cm_id(priv->cm.id);
 919        priv->cm.id = NULL;
 920
 921        spin_lock_irq(&priv->lock);
 922        while (!list_empty(&priv->cm.passive_ids)) {
 923                p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
 924                list_move(&p->list, &priv->cm.rx_error_list);
 925                p->state = IPOIB_CM_RX_ERROR;
 926                spin_unlock_irq(&priv->lock);
 927                ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
 928                if (ret)
 929                        ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
 930                spin_lock_irq(&priv->lock);
 931        }
 932
 933        /* Wait for all RX to be drained */
 934        begin = jiffies;
 935
 936        while (!list_empty(&priv->cm.rx_error_list) ||
 937               !list_empty(&priv->cm.rx_flush_list) ||
 938               !list_empty(&priv->cm.rx_drain_list)) {
 939                if (time_after(jiffies, begin + 5 * HZ)) {
 940                        ipoib_warn(priv, "RX drain timing out\n");
 941
 942                        /*
 943                         * assume the HW is wedged and just free up everything.
 944                         */
 945                        list_splice_init(&priv->cm.rx_flush_list,
 946                                         &priv->cm.rx_reap_list);
 947                        list_splice_init(&priv->cm.rx_error_list,
 948                                         &priv->cm.rx_reap_list);
 949                        list_splice_init(&priv->cm.rx_drain_list,
 950                                         &priv->cm.rx_reap_list);
 951                        break;
 952                }
 953                spin_unlock_irq(&priv->lock);
 954                msleep(1);
 955                ipoib_drain_cq(dev);
 956                spin_lock_irq(&priv->lock);
 957        }
 958
 959        spin_unlock_irq(&priv->lock);
 960
 961        ipoib_cm_free_rx_reap_list(dev);
 962
 963        cancel_delayed_work(&priv->cm.stale_task);
 964}
 965
 966static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
 967{
 968        struct ipoib_cm_tx *p = cm_id->context;
 969        struct ipoib_dev_priv *priv = netdev_priv(p->dev);
 970        struct ipoib_cm_data *data = event->private_data;
 971        struct sk_buff_head skqueue;
 972        struct ib_qp_attr qp_attr;
 973        int qp_attr_mask, ret;
 974        struct sk_buff *skb;
 975
 976        p->mtu = be32_to_cpu(data->mtu);
 977
 978        if (p->mtu <= IPOIB_ENCAP_LEN) {
 979                ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
 980                           p->mtu, IPOIB_ENCAP_LEN);
 981                return -EINVAL;
 982        }
 983
 984        qp_attr.qp_state = IB_QPS_RTR;
 985        ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
 986        if (ret) {
 987                ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
 988                return ret;
 989        }
 990
 991        qp_attr.rq_psn = 0 /* FIXME */;
 992        ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
 993        if (ret) {
 994                ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
 995                return ret;
 996        }
 997
 998        qp_attr.qp_state = IB_QPS_RTS;
 999        ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
1000        if (ret) {
1001                ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
1002                return ret;
1003        }
1004        ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
1005        if (ret) {
1006                ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
1007                return ret;
1008        }
1009
1010        skb_queue_head_init(&skqueue);
1011
1012        spin_lock_irq(&priv->lock);
1013        set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
1014        if (p->neigh)
1015                while ((skb = __skb_dequeue(&p->neigh->queue)))
1016                        __skb_queue_tail(&skqueue, skb);
1017        spin_unlock_irq(&priv->lock);
1018
1019        while ((skb = __skb_dequeue(&skqueue))) {
1020                skb->dev = p->dev;
1021                if (dev_queue_xmit(skb))
1022                        ipoib_warn(priv, "dev_queue_xmit failed "
1023                                   "to requeue packet\n");
1024        }
1025
1026        ret = ib_send_cm_rtu(cm_id, NULL, 0);
1027        if (ret) {
1028                ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1029                return ret;
1030        }
1031        return 0;
1032}
1033
1034static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
1035{
1036        struct ipoib_dev_priv *priv = netdev_priv(dev);
1037        struct ib_qp_init_attr attr = {
1038                .send_cq                = priv->recv_cq,
1039                .recv_cq                = priv->recv_cq,
1040                .srq                    = priv->cm.srq,
1041                .cap.max_send_wr        = ipoib_sendq_size,
1042                .cap.max_send_sge       = 1,
1043                .sq_sig_type            = IB_SIGNAL_ALL_WR,
1044                .qp_type                = IB_QPT_RC,
1045                .qp_context             = tx,
1046                .create_flags           = IB_QP_CREATE_USE_GFP_NOIO
1047        };
1048
1049        struct ib_qp *tx_qp;
1050
1051        if (dev->features & NETIF_F_SG)
1052                attr.cap.max_send_sge =
1053                        min_t(u32, priv->ca->attrs.max_sge, MAX_SKB_FRAGS + 1);
1054
1055        tx_qp = ib_create_qp(priv->pd, &attr);
1056        if (PTR_ERR(tx_qp) == -EINVAL) {
1057                ipoib_warn(priv, "can't use GFP_NOIO for QPs on device %s, using GFP_KERNEL\n",
1058                           priv->ca->name);
1059                attr.create_flags &= ~IB_QP_CREATE_USE_GFP_NOIO;
1060                tx_qp = ib_create_qp(priv->pd, &attr);
1061        }
1062        tx->max_send_sge = attr.cap.max_send_sge;
1063        return tx_qp;
1064}
1065
1066static int ipoib_cm_send_req(struct net_device *dev,
1067                             struct ib_cm_id *id, struct ib_qp *qp,
1068                             u32 qpn,
1069                             struct ib_sa_path_rec *pathrec)
1070{
1071        struct ipoib_dev_priv *priv = netdev_priv(dev);
1072        struct ipoib_cm_data data = {};
1073        struct ib_cm_req_param req = {};
1074
1075        data.qpn = cpu_to_be32(priv->qp->qp_num);
1076        data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1077
1078        req.primary_path                = pathrec;
1079        req.alternate_path              = NULL;
1080        req.service_id                  = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1081        req.qp_num                      = qp->qp_num;
1082        req.qp_type                     = qp->qp_type;
1083        req.private_data                = &data;
1084        req.private_data_len            = sizeof data;
1085        req.flow_control                = 0;
1086
1087        req.starting_psn                = 0; /* FIXME */
1088
1089        /*
1090         * Pick some arbitrary defaults here; we could make these
1091         * module parameters if anyone cared about setting them.
1092         */
1093        req.responder_resources         = 4;
1094        req.remote_cm_response_timeout  = 20;
1095        req.local_cm_response_timeout   = 20;
1096        req.retry_count                 = 0; /* RFC draft warns against retries */
1097        req.rnr_retry_count             = 0; /* RFC draft warns against retries */
1098        req.max_cm_retries              = 15;
1099        req.srq                         = ipoib_cm_has_srq(dev);
1100        return ib_send_cm_req(id, &req);
1101}
1102
1103static int ipoib_cm_modify_tx_init(struct net_device *dev,
1104                                  struct ib_cm_id *cm_id, struct ib_qp *qp)
1105{
1106        struct ipoib_dev_priv *priv = netdev_priv(dev);
1107        struct ib_qp_attr qp_attr;
1108        int qp_attr_mask, ret;
1109        ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
1110        if (ret) {
1111                ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
1112                return ret;
1113        }
1114
1115        qp_attr.qp_state = IB_QPS_INIT;
1116        qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1117        qp_attr.port_num = priv->port;
1118        qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1119
1120        ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1121        if (ret) {
1122                ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1123                return ret;
1124        }
1125        return 0;
1126}
1127
1128static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1129                            struct ib_sa_path_rec *pathrec)
1130{
1131        struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1132        int ret;
1133
1134        p->tx_ring = __vmalloc(ipoib_sendq_size * sizeof *p->tx_ring,
1135                               GFP_NOIO, PAGE_KERNEL);
1136        if (!p->tx_ring) {
1137                ipoib_warn(priv, "failed to allocate tx ring\n");
1138                ret = -ENOMEM;
1139                goto err_tx;
1140        }
1141        memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
1142
1143        p->qp = ipoib_cm_create_tx_qp(p->dev, p);
1144        if (IS_ERR(p->qp)) {
1145                ret = PTR_ERR(p->qp);
1146                ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1147                goto err_qp;
1148        }
1149
1150        p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1151        if (IS_ERR(p->id)) {
1152                ret = PTR_ERR(p->id);
1153                ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1154                goto err_id;
1155        }
1156
1157        ret = ipoib_cm_modify_tx_init(p->dev, p->id,  p->qp);
1158        if (ret) {
1159                ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1160                goto err_modify;
1161        }
1162
1163        ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1164        if (ret) {
1165                ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1166                goto err_send_cm;
1167        }
1168
1169        ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1170                  p->qp->qp_num, pathrec->dgid.raw, qpn);
1171
1172        return 0;
1173
1174err_send_cm:
1175err_modify:
1176        ib_destroy_cm_id(p->id);
1177err_id:
1178        p->id = NULL;
1179        ib_destroy_qp(p->qp);
1180err_qp:
1181        p->qp = NULL;
1182        vfree(p->tx_ring);
1183err_tx:
1184        return ret;
1185}
1186
1187static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1188{
1189        struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1190        struct ipoib_tx_buf *tx_req;
1191        unsigned long begin;
1192
1193        ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1194                  p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1195
1196        if (p->id)
1197                ib_destroy_cm_id(p->id);
1198
1199        if (p->tx_ring) {
1200                /* Wait for all sends to complete */
1201                begin = jiffies;
1202                while ((int) p->tx_tail - (int) p->tx_head < 0) {
1203                        if (time_after(jiffies, begin + 5 * HZ)) {
1204                                ipoib_warn(priv, "timing out; %d sends not completed\n",
1205                                           p->tx_head - p->tx_tail);
1206                                goto timeout;
1207                        }
1208
1209                        msleep(1);
1210                }
1211        }
1212
1213timeout:
1214
1215        while ((int) p->tx_tail - (int) p->tx_head < 0) {
1216                tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
1217                ipoib_dma_unmap_tx(priv, tx_req);
1218                dev_kfree_skb_any(tx_req->skb);
1219                ++p->tx_tail;
1220                netif_tx_lock_bh(p->dev);
1221                if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1222                    netif_queue_stopped(p->dev) &&
1223                    test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1224                        netif_wake_queue(p->dev);
1225                netif_tx_unlock_bh(p->dev);
1226        }
1227
1228        if (p->qp)
1229                ib_destroy_qp(p->qp);
1230
1231        vfree(p->tx_ring);
1232        kfree(p);
1233}
1234
1235static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1236                               struct ib_cm_event *event)
1237{
1238        struct ipoib_cm_tx *tx = cm_id->context;
1239        struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1240        struct net_device *dev = priv->dev;
1241        struct ipoib_neigh *neigh;
1242        unsigned long flags;
1243        int ret;
1244
1245        switch (event->event) {
1246        case IB_CM_DREQ_RECEIVED:
1247                ipoib_dbg(priv, "DREQ received.\n");
1248                ib_send_cm_drep(cm_id, NULL, 0);
1249                break;
1250        case IB_CM_REP_RECEIVED:
1251                ipoib_dbg(priv, "REP received.\n");
1252                ret = ipoib_cm_rep_handler(cm_id, event);
1253                if (ret)
1254                        ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1255                                       NULL, 0, NULL, 0);
1256                break;
1257        case IB_CM_REQ_ERROR:
1258        case IB_CM_REJ_RECEIVED:
1259        case IB_CM_TIMEWAIT_EXIT:
1260                ipoib_dbg(priv, "CM error %d.\n", event->event);
1261                netif_tx_lock_bh(dev);
1262                spin_lock_irqsave(&priv->lock, flags);
1263                neigh = tx->neigh;
1264
1265                if (neigh) {
1266                        neigh->cm = NULL;
1267                        ipoib_neigh_free(neigh);
1268
1269                        tx->neigh = NULL;
1270                }
1271
1272                if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1273                        list_move(&tx->list, &priv->cm.reap_list);
1274                        queue_work(priv->wq, &priv->cm.reap_task);
1275                }
1276
1277                spin_unlock_irqrestore(&priv->lock, flags);
1278                netif_tx_unlock_bh(dev);
1279                break;
1280        default:
1281                break;
1282        }
1283
1284        return 0;
1285}
1286
1287struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1288                                       struct ipoib_neigh *neigh)
1289{
1290        struct ipoib_dev_priv *priv = netdev_priv(dev);
1291        struct ipoib_cm_tx *tx;
1292
1293        tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1294        if (!tx)
1295                return NULL;
1296
1297        neigh->cm = tx;
1298        tx->neigh = neigh;
1299        tx->path = path;
1300        tx->dev = dev;
1301        list_add(&tx->list, &priv->cm.start_list);
1302        set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1303        queue_work(priv->wq, &priv->cm.start_task);
1304        return tx;
1305}
1306
1307void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1308{
1309        struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1310        unsigned long flags;
1311        if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1312                spin_lock_irqsave(&priv->lock, flags);
1313                list_move(&tx->list, &priv->cm.reap_list);
1314                queue_work(priv->wq, &priv->cm.reap_task);
1315                ipoib_dbg(priv, "Reap connection for gid %pI6\n",
1316                          tx->neigh->daddr + 4);
1317                tx->neigh = NULL;
1318                spin_unlock_irqrestore(&priv->lock, flags);
1319        }
1320}
1321
1322#define QPN_AND_OPTIONS_OFFSET  4
1323
1324static void ipoib_cm_tx_start(struct work_struct *work)
1325{
1326        struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1327                                                   cm.start_task);
1328        struct net_device *dev = priv->dev;
1329        struct ipoib_neigh *neigh;
1330        struct ipoib_cm_tx *p;
1331        unsigned long flags;
1332        struct ipoib_path *path;
1333        int ret;
1334
1335        struct ib_sa_path_rec pathrec;
1336        u32 qpn;
1337
1338        netif_tx_lock_bh(dev);
1339        spin_lock_irqsave(&priv->lock, flags);
1340
1341        while (!list_empty(&priv->cm.start_list)) {
1342                p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1343                list_del_init(&p->list);
1344                neigh = p->neigh;
1345
1346                qpn = IPOIB_QPN(neigh->daddr);
1347                /*
1348                 * As long as the search is with these 2 locks,
1349                 * path existence indicates its validity.
1350                 */
1351                path = __path_find(dev, neigh->daddr + QPN_AND_OPTIONS_OFFSET);
1352                if (!path) {
1353                        pr_info("%s ignore not valid path %pI6\n",
1354                                __func__,
1355                                neigh->daddr + QPN_AND_OPTIONS_OFFSET);
1356                        goto free_neigh;
1357                }
1358                memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1359
1360                spin_unlock_irqrestore(&priv->lock, flags);
1361                netif_tx_unlock_bh(dev);
1362
1363                ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1364
1365                netif_tx_lock_bh(dev);
1366                spin_lock_irqsave(&priv->lock, flags);
1367
1368                if (ret) {
1369free_neigh:
1370                        neigh = p->neigh;
1371                        if (neigh) {
1372                                neigh->cm = NULL;
1373                                ipoib_neigh_free(neigh);
1374                        }
1375                        list_del(&p->list);
1376                        kfree(p);
1377                }
1378        }
1379
1380        spin_unlock_irqrestore(&priv->lock, flags);
1381        netif_tx_unlock_bh(dev);
1382}
1383
1384static void ipoib_cm_tx_reap(struct work_struct *work)
1385{
1386        struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1387                                                   cm.reap_task);
1388        struct net_device *dev = priv->dev;
1389        struct ipoib_cm_tx *p;
1390        unsigned long flags;
1391
1392        netif_tx_lock_bh(dev);
1393        spin_lock_irqsave(&priv->lock, flags);
1394
1395        while (!list_empty(&priv->cm.reap_list)) {
1396                p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1397                list_del(&p->list);
1398                spin_unlock_irqrestore(&priv->lock, flags);
1399                netif_tx_unlock_bh(dev);
1400                ipoib_cm_tx_destroy(p);
1401                netif_tx_lock_bh(dev);
1402                spin_lock_irqsave(&priv->lock, flags);
1403        }
1404
1405        spin_unlock_irqrestore(&priv->lock, flags);
1406        netif_tx_unlock_bh(dev);
1407}
1408
1409static void ipoib_cm_skb_reap(struct work_struct *work)
1410{
1411        struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1412                                                   cm.skb_task);
1413        struct net_device *dev = priv->dev;
1414        struct sk_buff *skb;
1415        unsigned long flags;
1416        unsigned mtu = priv->mcast_mtu;
1417
1418        netif_tx_lock_bh(dev);
1419        spin_lock_irqsave(&priv->lock, flags);
1420
1421        while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1422                spin_unlock_irqrestore(&priv->lock, flags);
1423                netif_tx_unlock_bh(dev);
1424
1425                if (skb->protocol == htons(ETH_P_IP))
1426                        icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1427#if IS_ENABLED(CONFIG_IPV6)
1428                else if (skb->protocol == htons(ETH_P_IPV6))
1429                        icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1430#endif
1431                dev_kfree_skb_any(skb);
1432
1433                netif_tx_lock_bh(dev);
1434                spin_lock_irqsave(&priv->lock, flags);
1435        }
1436
1437        spin_unlock_irqrestore(&priv->lock, flags);
1438        netif_tx_unlock_bh(dev);
1439}
1440
1441void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
1442                           unsigned int mtu)
1443{
1444        struct ipoib_dev_priv *priv = netdev_priv(dev);
1445        int e = skb_queue_empty(&priv->cm.skb_queue);
1446
1447        if (skb_dst(skb))
1448                skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1449
1450        skb_queue_tail(&priv->cm.skb_queue, skb);
1451        if (e)
1452                queue_work(priv->wq, &priv->cm.skb_task);
1453}
1454
1455static void ipoib_cm_rx_reap(struct work_struct *work)
1456{
1457        ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1458                                                cm.rx_reap_task)->dev);
1459}
1460
1461static void ipoib_cm_stale_task(struct work_struct *work)
1462{
1463        struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1464                                                   cm.stale_task.work);
1465        struct ipoib_cm_rx *p;
1466        int ret;
1467
1468        spin_lock_irq(&priv->lock);
1469        while (!list_empty(&priv->cm.passive_ids)) {
1470                /* List is sorted by LRU, start from tail,
1471                 * stop when we see a recently used entry */
1472                p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1473                if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1474                        break;
1475                list_move(&p->list, &priv->cm.rx_error_list);
1476                p->state = IPOIB_CM_RX_ERROR;
1477                spin_unlock_irq(&priv->lock);
1478                ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1479                if (ret)
1480                        ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1481                spin_lock_irq(&priv->lock);
1482        }
1483
1484        if (!list_empty(&priv->cm.passive_ids))
1485                queue_delayed_work(priv->wq,
1486                                   &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1487        spin_unlock_irq(&priv->lock);
1488}
1489
1490static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1491                         char *buf)
1492{
1493        struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1494
1495        if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1496                return sprintf(buf, "connected\n");
1497        else
1498                return sprintf(buf, "datagram\n");
1499}
1500
1501static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1502                        const char *buf, size_t count)
1503{
1504        struct net_device *dev = to_net_dev(d);
1505        int ret;
1506        struct ipoib_dev_priv *priv = netdev_priv(dev);
1507
1508        if (test_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags))
1509                return -EPERM;
1510
1511        if (!rtnl_trylock())
1512                return restart_syscall();
1513
1514        ret = ipoib_set_mode(dev, buf);
1515
1516        rtnl_unlock();
1517
1518        if (!ret)
1519                return count;
1520
1521        return ret;
1522}
1523
1524static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1525
1526int ipoib_cm_add_mode_attr(struct net_device *dev)
1527{
1528        return device_create_file(&dev->dev, &dev_attr_mode);
1529}
1530
1531static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
1532{
1533        struct ipoib_dev_priv *priv = netdev_priv(dev);
1534        struct ib_srq_init_attr srq_init_attr = {
1535                .srq_type = IB_SRQT_BASIC,
1536                .attr = {
1537                        .max_wr  = ipoib_recvq_size,
1538                        .max_sge = max_sge
1539                }
1540        };
1541
1542        priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1543        if (IS_ERR(priv->cm.srq)) {
1544                if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1545                        printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1546                               priv->ca->name, PTR_ERR(priv->cm.srq));
1547                priv->cm.srq = NULL;
1548                return;
1549        }
1550
1551        priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1552        if (!priv->cm.srq_ring) {
1553                printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1554                       priv->ca->name, ipoib_recvq_size);
1555                ib_destroy_srq(priv->cm.srq);
1556                priv->cm.srq = NULL;
1557                return;
1558        }
1559
1560}
1561
1562int ipoib_cm_dev_init(struct net_device *dev)
1563{
1564        struct ipoib_dev_priv *priv = netdev_priv(dev);
1565        int max_srq_sge, i;
1566
1567        INIT_LIST_HEAD(&priv->cm.passive_ids);
1568        INIT_LIST_HEAD(&priv->cm.reap_list);
1569        INIT_LIST_HEAD(&priv->cm.start_list);
1570        INIT_LIST_HEAD(&priv->cm.rx_error_list);
1571        INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1572        INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1573        INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1574        INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1575        INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1576        INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1577        INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1578        INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1579
1580        skb_queue_head_init(&priv->cm.skb_queue);
1581
1582        ipoib_dbg(priv, "max_srq_sge=%d\n", priv->ca->attrs.max_srq_sge);
1583
1584        max_srq_sge = min_t(int, IPOIB_CM_RX_SG, priv->ca->attrs.max_srq_sge);
1585        ipoib_cm_create_srq(dev, max_srq_sge);
1586        if (ipoib_cm_has_srq(dev)) {
1587                priv->cm.max_cm_mtu = max_srq_sge * PAGE_SIZE - 0x10;
1588                priv->cm.num_frags  = max_srq_sge;
1589                ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1590                          priv->cm.max_cm_mtu, priv->cm.num_frags);
1591        } else {
1592                priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1593                priv->cm.num_frags  = IPOIB_CM_RX_SG;
1594        }
1595
1596        ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
1597
1598        if (ipoib_cm_has_srq(dev)) {
1599                for (i = 0; i < ipoib_recvq_size; ++i) {
1600                        if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
1601                                                   priv->cm.num_frags - 1,
1602                                                   priv->cm.srq_ring[i].mapping,
1603                                                   GFP_KERNEL)) {
1604                                ipoib_warn(priv, "failed to allocate "
1605                                           "receive buffer %d\n", i);
1606                                ipoib_cm_dev_cleanup(dev);
1607                                return -ENOMEM;
1608                        }
1609
1610                        if (ipoib_cm_post_receive_srq(dev, i)) {
1611                                ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1612                                           "failed for buf %d\n", i);
1613                                ipoib_cm_dev_cleanup(dev);
1614                                return -EIO;
1615                        }
1616                }
1617        }
1618
1619        priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1620        return 0;
1621}
1622
1623void ipoib_cm_dev_cleanup(struct net_device *dev)
1624{
1625        struct ipoib_dev_priv *priv = netdev_priv(dev);
1626        int ret;
1627
1628        if (!priv->cm.srq)
1629                return;
1630
1631        ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1632
1633        ret = ib_destroy_srq(priv->cm.srq);
1634        if (ret)
1635                ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1636
1637        priv->cm.srq = NULL;
1638        if (!priv->cm.srq_ring)
1639                return;
1640
1641        ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
1642        priv->cm.srq_ring = NULL;
1643}
1644