linux/drivers/infiniband/hw/cxgb4/cm.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2009-2014 Chelsio, Inc. All rights reserved.
   3 *
   4 * This software is available to you under a choice of one of two
   5 * licenses.  You may choose to be licensed under the terms of the GNU
   6 * General Public License (GPL) Version 2, available from the file
   7 * COPYING in the main directory of this source tree, or the
   8 * OpenIB.org BSD license below:
   9 *
  10 *     Redistribution and use in source and binary forms, with or
  11 *     without modification, are permitted provided that the following
  12 *     conditions are met:
  13 *
  14 *      - Redistributions of source code must retain the above
  15 *        copyright notice, this list of conditions and the following
  16 *        disclaimer.
  17 *
  18 *      - Redistributions in binary form must reproduce the above
  19 *        copyright notice, this list of conditions and the following
  20 *        disclaimer in the documentation and/or other materials
  21 *        provided with the distribution.
  22 *
  23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30 * SOFTWARE.
  31 */
  32#include <linux/module.h>
  33#include <linux/list.h>
  34#include <linux/workqueue.h>
  35#include <linux/skbuff.h>
  36#include <linux/timer.h>
  37#include <linux/notifier.h>
  38#include <linux/inetdevice.h>
  39#include <linux/ip.h>
  40#include <linux/tcp.h>
  41#include <linux/if_vlan.h>
  42
  43#include <net/neighbour.h>
  44#include <net/netevent.h>
  45#include <net/route.h>
  46#include <net/tcp.h>
  47#include <net/ip6_route.h>
  48#include <net/addrconf.h>
  49
  50#include <rdma/ib_addr.h>
  51
  52#include <libcxgb_cm.h>
  53#include "iw_cxgb4.h"
  54#include "clip_tbl.h"
  55
  56static char *states[] = {
  57        "idle",
  58        "listen",
  59        "connecting",
  60        "mpa_wait_req",
  61        "mpa_req_sent",
  62        "mpa_req_rcvd",
  63        "mpa_rep_sent",
  64        "fpdu_mode",
  65        "aborting",
  66        "closing",
  67        "moribund",
  68        "dead",
  69        NULL,
  70};
  71
  72static int nocong;
  73module_param(nocong, int, 0644);
  74MODULE_PARM_DESC(nocong, "Turn of congestion control (default=0)");
  75
  76static int enable_ecn;
  77module_param(enable_ecn, int, 0644);
  78MODULE_PARM_DESC(enable_ecn, "Enable ECN (default=0/disabled)");
  79
  80static int dack_mode = 1;
  81module_param(dack_mode, int, 0644);
  82MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
  83
  84uint c4iw_max_read_depth = 32;
  85module_param(c4iw_max_read_depth, int, 0644);
  86MODULE_PARM_DESC(c4iw_max_read_depth,
  87                 "Per-connection max ORD/IRD (default=32)");
  88
  89static int enable_tcp_timestamps;
  90module_param(enable_tcp_timestamps, int, 0644);
  91MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
  92
  93static int enable_tcp_sack;
  94module_param(enable_tcp_sack, int, 0644);
  95MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
  96
  97static int enable_tcp_window_scaling = 1;
  98module_param(enable_tcp_window_scaling, int, 0644);
  99MODULE_PARM_DESC(enable_tcp_window_scaling,
 100                 "Enable tcp window scaling (default=1)");
 101
 102int c4iw_debug;
 103module_param(c4iw_debug, int, 0644);
 104MODULE_PARM_DESC(c4iw_debug, "obsolete");
 105
 106static int peer2peer = 1;
 107module_param(peer2peer, int, 0644);
 108MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=1)");
 109
 110static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
 111module_param(p2p_type, int, 0644);
 112MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
 113                           "1=RDMA_READ 0=RDMA_WRITE (default 1)");
 114
 115static int ep_timeout_secs = 60;
 116module_param(ep_timeout_secs, int, 0644);
 117MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
 118                                   "in seconds (default=60)");
 119
 120static int mpa_rev = 2;
 121module_param(mpa_rev, int, 0644);
 122MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
 123                "1 is RFC5044 spec compliant, 2 is IETF MPA Peer Connect Draft"
 124                " compliant (default=2)");
 125
 126static int markers_enabled;
 127module_param(markers_enabled, int, 0644);
 128MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
 129
 130static int crc_enabled = 1;
 131module_param(crc_enabled, int, 0644);
 132MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
 133
 134static int rcv_win = 256 * 1024;
 135module_param(rcv_win, int, 0644);
 136MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
 137
 138static int snd_win = 128 * 1024;
 139module_param(snd_win, int, 0644);
 140MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
 141
 142static struct workqueue_struct *workq;
 143
 144static struct sk_buff_head rxq;
 145
 146static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
 147static void ep_timeout(unsigned long arg);
 148static void connect_reply_upcall(struct c4iw_ep *ep, int status);
 149static int sched(struct c4iw_dev *dev, struct sk_buff *skb);
 150
 151static LIST_HEAD(timeout_list);
 152static spinlock_t timeout_lock;
 153
 154static void deref_cm_id(struct c4iw_ep_common *epc)
 155{
 156        epc->cm_id->rem_ref(epc->cm_id);
 157        epc->cm_id = NULL;
 158        set_bit(CM_ID_DEREFED, &epc->history);
 159}
 160
 161static void ref_cm_id(struct c4iw_ep_common *epc)
 162{
 163        set_bit(CM_ID_REFED, &epc->history);
 164        epc->cm_id->add_ref(epc->cm_id);
 165}
 166
 167static void deref_qp(struct c4iw_ep *ep)
 168{
 169        c4iw_qp_rem_ref(&ep->com.qp->ibqp);
 170        clear_bit(QP_REFERENCED, &ep->com.flags);
 171        set_bit(QP_DEREFED, &ep->com.history);
 172}
 173
 174static void ref_qp(struct c4iw_ep *ep)
 175{
 176        set_bit(QP_REFERENCED, &ep->com.flags);
 177        set_bit(QP_REFED, &ep->com.history);
 178        c4iw_qp_add_ref(&ep->com.qp->ibqp);
 179}
 180
 181static void start_ep_timer(struct c4iw_ep *ep)
 182{
 183        pr_debug("%s ep %p\n", __func__, ep);
 184        if (timer_pending(&ep->timer)) {
 185                pr_err("%s timer already started! ep %p\n",
 186                       __func__, ep);
 187                return;
 188        }
 189        clear_bit(TIMEOUT, &ep->com.flags);
 190        c4iw_get_ep(&ep->com);
 191        ep->timer.expires = jiffies + ep_timeout_secs * HZ;
 192        ep->timer.data = (unsigned long)ep;
 193        ep->timer.function = ep_timeout;
 194        add_timer(&ep->timer);
 195}
 196
 197static int stop_ep_timer(struct c4iw_ep *ep)
 198{
 199        pr_debug("%s ep %p stopping\n", __func__, ep);
 200        del_timer_sync(&ep->timer);
 201        if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
 202                c4iw_put_ep(&ep->com);
 203                return 0;
 204        }
 205        return 1;
 206}
 207
 208static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
 209                  struct l2t_entry *l2e)
 210{
 211        int     error = 0;
 212
 213        if (c4iw_fatal_error(rdev)) {
 214                kfree_skb(skb);
 215                pr_debug("%s - device in error state - dropping\n", __func__);
 216                return -EIO;
 217        }
 218        error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
 219        if (error < 0)
 220                kfree_skb(skb);
 221        else if (error == NET_XMIT_DROP)
 222                return -ENOMEM;
 223        return error < 0 ? error : 0;
 224}
 225
 226int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
 227{
 228        int     error = 0;
 229
 230        if (c4iw_fatal_error(rdev)) {
 231                kfree_skb(skb);
 232                pr_debug("%s - device in error state - dropping\n", __func__);
 233                return -EIO;
 234        }
 235        error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
 236        if (error < 0)
 237                kfree_skb(skb);
 238        return error < 0 ? error : 0;
 239}
 240
 241static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
 242{
 243        u32 len = roundup(sizeof(struct cpl_tid_release), 16);
 244
 245        skb = get_skb(skb, len, GFP_KERNEL);
 246        if (!skb)
 247                return;
 248
 249        cxgb_mk_tid_release(skb, len, hwtid, 0);
 250        c4iw_ofld_send(rdev, skb);
 251        return;
 252}
 253
 254static void set_emss(struct c4iw_ep *ep, u16 opt)
 255{
 256        ep->emss = ep->com.dev->rdev.lldi.mtus[TCPOPT_MSS_G(opt)] -
 257                   ((AF_INET == ep->com.remote_addr.ss_family) ?
 258                    sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
 259                   sizeof(struct tcphdr);
 260        ep->mss = ep->emss;
 261        if (TCPOPT_TSTAMP_G(opt))
 262                ep->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
 263        if (ep->emss < 128)
 264                ep->emss = 128;
 265        if (ep->emss & 7)
 266                pr_debug("Warning: misaligned mtu idx %u mss %u emss=%u\n",
 267                         TCPOPT_MSS_G(opt), ep->mss, ep->emss);
 268        pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
 269                 ep->mss, ep->emss);
 270}
 271
 272static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
 273{
 274        enum c4iw_ep_state state;
 275
 276        mutex_lock(&epc->mutex);
 277        state = epc->state;
 278        mutex_unlock(&epc->mutex);
 279        return state;
 280}
 281
 282static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
 283{
 284        epc->state = new;
 285}
 286
 287static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
 288{
 289        mutex_lock(&epc->mutex);
 290        pr_debug("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
 291        __state_set(epc, new);
 292        mutex_unlock(&epc->mutex);
 293        return;
 294}
 295
 296static int alloc_ep_skb_list(struct sk_buff_head *ep_skb_list, int size)
 297{
 298        struct sk_buff *skb;
 299        unsigned int i;
 300        size_t len;
 301
 302        len = roundup(sizeof(union cpl_wr_size), 16);
 303        for (i = 0; i < size; i++) {
 304                skb = alloc_skb(len, GFP_KERNEL);
 305                if (!skb)
 306                        goto fail;
 307                skb_queue_tail(ep_skb_list, skb);
 308        }
 309        return 0;
 310fail:
 311        skb_queue_purge(ep_skb_list);
 312        return -ENOMEM;
 313}
 314
 315static void *alloc_ep(int size, gfp_t gfp)
 316{
 317        struct c4iw_ep_common *epc;
 318
 319        epc = kzalloc(size, gfp);
 320        if (epc) {
 321                kref_init(&epc->kref);
 322                mutex_init(&epc->mutex);
 323                c4iw_init_wr_wait(&epc->wr_wait);
 324        }
 325        pr_debug("%s alloc ep %p\n", __func__, epc);
 326        return epc;
 327}
 328
 329static void remove_ep_tid(struct c4iw_ep *ep)
 330{
 331        unsigned long flags;
 332
 333        spin_lock_irqsave(&ep->com.dev->lock, flags);
 334        _remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid, 0);
 335        if (idr_is_empty(&ep->com.dev->hwtid_idr))
 336                wake_up(&ep->com.dev->wait);
 337        spin_unlock_irqrestore(&ep->com.dev->lock, flags);
 338}
 339
 340static void insert_ep_tid(struct c4iw_ep *ep)
 341{
 342        unsigned long flags;
 343
 344        spin_lock_irqsave(&ep->com.dev->lock, flags);
 345        _insert_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep, ep->hwtid, 0);
 346        spin_unlock_irqrestore(&ep->com.dev->lock, flags);
 347}
 348
 349/*
 350 * Atomically lookup the ep ptr given the tid and grab a reference on the ep.
 351 */
 352static struct c4iw_ep *get_ep_from_tid(struct c4iw_dev *dev, unsigned int tid)
 353{
 354        struct c4iw_ep *ep;
 355        unsigned long flags;
 356
 357        spin_lock_irqsave(&dev->lock, flags);
 358        ep = idr_find(&dev->hwtid_idr, tid);
 359        if (ep)
 360                c4iw_get_ep(&ep->com);
 361        spin_unlock_irqrestore(&dev->lock, flags);
 362        return ep;
 363}
 364
 365/*
 366 * Atomically lookup the ep ptr given the stid and grab a reference on the ep.
 367 */
 368static struct c4iw_listen_ep *get_ep_from_stid(struct c4iw_dev *dev,
 369                                               unsigned int stid)
 370{
 371        struct c4iw_listen_ep *ep;
 372        unsigned long flags;
 373
 374        spin_lock_irqsave(&dev->lock, flags);
 375        ep = idr_find(&dev->stid_idr, stid);
 376        if (ep)
 377                c4iw_get_ep(&ep->com);
 378        spin_unlock_irqrestore(&dev->lock, flags);
 379        return ep;
 380}
 381
 382void _c4iw_free_ep(struct kref *kref)
 383{
 384        struct c4iw_ep *ep;
 385
 386        ep = container_of(kref, struct c4iw_ep, com.kref);
 387        pr_debug("%s ep %p state %s\n", __func__, ep, states[ep->com.state]);
 388        if (test_bit(QP_REFERENCED, &ep->com.flags))
 389                deref_qp(ep);
 390        if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
 391                if (ep->com.remote_addr.ss_family == AF_INET6) {
 392                        struct sockaddr_in6 *sin6 =
 393                                        (struct sockaddr_in6 *)
 394                                        &ep->com.local_addr;
 395
 396                        cxgb4_clip_release(
 397                                        ep->com.dev->rdev.lldi.ports[0],
 398                                        (const u32 *)&sin6->sin6_addr.s6_addr,
 399                                        1);
 400                }
 401                cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid,
 402                                 ep->com.local_addr.ss_family);
 403                dst_release(ep->dst);
 404                cxgb4_l2t_release(ep->l2t);
 405                if (ep->mpa_skb)
 406                        kfree_skb(ep->mpa_skb);
 407        }
 408        if (!skb_queue_empty(&ep->com.ep_skb_list))
 409                skb_queue_purge(&ep->com.ep_skb_list);
 410        kfree(ep);
 411}
 412
 413static void release_ep_resources(struct c4iw_ep *ep)
 414{
 415        set_bit(RELEASE_RESOURCES, &ep->com.flags);
 416
 417        /*
 418         * If we have a hwtid, then remove it from the idr table
 419         * so lookups will no longer find this endpoint.  Otherwise
 420         * we have a race where one thread finds the ep ptr just
 421         * before the other thread is freeing the ep memory.
 422         */
 423        if (ep->hwtid != -1)
 424                remove_ep_tid(ep);
 425        c4iw_put_ep(&ep->com);
 426}
 427
 428static int status2errno(int status)
 429{
 430        switch (status) {
 431        case CPL_ERR_NONE:
 432                return 0;
 433        case CPL_ERR_CONN_RESET:
 434                return -ECONNRESET;
 435        case CPL_ERR_ARP_MISS:
 436                return -EHOSTUNREACH;
 437        case CPL_ERR_CONN_TIMEDOUT:
 438                return -ETIMEDOUT;
 439        case CPL_ERR_TCAM_FULL:
 440                return -ENOMEM;
 441        case CPL_ERR_CONN_EXIST:
 442                return -EADDRINUSE;
 443        default:
 444                return -EIO;
 445        }
 446}
 447
 448/*
 449 * Try and reuse skbs already allocated...
 450 */
 451static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
 452{
 453        if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
 454                skb_trim(skb, 0);
 455                skb_get(skb);
 456                skb_reset_transport_header(skb);
 457        } else {
 458                skb = alloc_skb(len, gfp);
 459        }
 460        t4_set_arp_err_handler(skb, NULL, NULL);
 461        return skb;
 462}
 463
 464static struct net_device *get_real_dev(struct net_device *egress_dev)
 465{
 466        return rdma_vlan_dev_real_dev(egress_dev) ? : egress_dev;
 467}
 468
 469static void arp_failure_discard(void *handle, struct sk_buff *skb)
 470{
 471        pr_err("ARP failure\n");
 472        kfree_skb(skb);
 473}
 474
 475static void mpa_start_arp_failure(void *handle, struct sk_buff *skb)
 476{
 477        pr_err("ARP failure during MPA Negotiation - Closing Connection\n");
 478}
 479
 480enum {
 481        NUM_FAKE_CPLS = 2,
 482        FAKE_CPL_PUT_EP_SAFE = NUM_CPL_CMDS + 0,
 483        FAKE_CPL_PASS_PUT_EP_SAFE = NUM_CPL_CMDS + 1,
 484};
 485
 486static int _put_ep_safe(struct c4iw_dev *dev, struct sk_buff *skb)
 487{
 488        struct c4iw_ep *ep;
 489
 490        ep = *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *)));
 491        release_ep_resources(ep);
 492        kfree_skb(skb);
 493        return 0;
 494}
 495
 496static int _put_pass_ep_safe(struct c4iw_dev *dev, struct sk_buff *skb)
 497{
 498        struct c4iw_ep *ep;
 499
 500        ep = *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *)));
 501        c4iw_put_ep(&ep->parent_ep->com);
 502        release_ep_resources(ep);
 503        kfree_skb(skb);
 504        return 0;
 505}
 506
 507/*
 508 * Fake up a special CPL opcode and call sched() so process_work() will call
 509 * _put_ep_safe() in a safe context to free the ep resources.  This is needed
 510 * because ARP error handlers are called in an ATOMIC context, and
 511 * _c4iw_free_ep() needs to block.
 512 */
 513static void queue_arp_failure_cpl(struct c4iw_ep *ep, struct sk_buff *skb,
 514                                  int cpl)
 515{
 516        struct cpl_act_establish *rpl = cplhdr(skb);
 517
 518        /* Set our special ARP_FAILURE opcode */
 519        rpl->ot.opcode = cpl;
 520
 521        /*
 522         * Save ep in the skb->cb area, after where sched() will save the dev
 523         * ptr.
 524         */
 525        *((struct c4iw_ep **)(skb->cb + 2 * sizeof(void *))) = ep;
 526        sched(ep->com.dev, skb);
 527}
 528
 529/* Handle an ARP failure for an accept */
 530static void pass_accept_rpl_arp_failure(void *handle, struct sk_buff *skb)
 531{
 532        struct c4iw_ep *ep = handle;
 533
 534        pr_err("ARP failure during accept - tid %u - dropping connection\n",
 535               ep->hwtid);
 536
 537        __state_set(&ep->com, DEAD);
 538        queue_arp_failure_cpl(ep, skb, FAKE_CPL_PASS_PUT_EP_SAFE);
 539}
 540
 541/*
 542 * Handle an ARP failure for an active open.
 543 */
 544static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
 545{
 546        struct c4iw_ep *ep = handle;
 547
 548        pr_err("ARP failure during connect\n");
 549        connect_reply_upcall(ep, -EHOSTUNREACH);
 550        __state_set(&ep->com, DEAD);
 551        if (ep->com.remote_addr.ss_family == AF_INET6) {
 552                struct sockaddr_in6 *sin6 =
 553                        (struct sockaddr_in6 *)&ep->com.local_addr;
 554                cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
 555                                   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
 556        }
 557        remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
 558        cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
 559        queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
 560}
 561
 562/*
 563 * Handle an ARP failure for a CPL_ABORT_REQ.  Change it into a no RST variant
 564 * and send it along.
 565 */
 566static void abort_arp_failure(void *handle, struct sk_buff *skb)
 567{
 568        int ret;
 569        struct c4iw_ep *ep = handle;
 570        struct c4iw_rdev *rdev = &ep->com.dev->rdev;
 571        struct cpl_abort_req *req = cplhdr(skb);
 572
 573        pr_debug("%s rdev %p\n", __func__, rdev);
 574        req->cmd = CPL_ABORT_NO_RST;
 575        skb_get(skb);
 576        ret = c4iw_ofld_send(rdev, skb);
 577        if (ret) {
 578                __state_set(&ep->com, DEAD);
 579                queue_arp_failure_cpl(ep, skb, FAKE_CPL_PUT_EP_SAFE);
 580        } else
 581                kfree_skb(skb);
 582}
 583
 584static int send_flowc(struct c4iw_ep *ep)
 585{
 586        struct fw_flowc_wr *flowc;
 587        struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
 588        int i;
 589        u16 vlan = ep->l2t->vlan;
 590        int nparams;
 591
 592        if (WARN_ON(!skb))
 593                return -ENOMEM;
 594
 595        if (vlan == CPL_L2T_VLAN_NONE)
 596                nparams = 8;
 597        else
 598                nparams = 9;
 599
 600        flowc = __skb_put(skb, FLOWC_LEN);
 601
 602        flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
 603                                           FW_FLOWC_WR_NPARAMS_V(nparams));
 604        flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(DIV_ROUND_UP(FLOWC_LEN,
 605                                          16)) | FW_WR_FLOWID_V(ep->hwtid));
 606
 607        flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
 608        flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
 609                                            (ep->com.dev->rdev.lldi.pf));
 610        flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
 611        flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
 612        flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
 613        flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
 614        flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
 615        flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
 616        flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
 617        flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
 618        flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
 619        flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
 620        flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
 621        flowc->mnemval[6].val = cpu_to_be32(ep->snd_win);
 622        flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
 623        flowc->mnemval[7].val = cpu_to_be32(ep->emss);
 624        if (nparams == 9) {
 625                u16 pri;
 626
 627                pri = (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
 628                flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
 629                flowc->mnemval[8].val = cpu_to_be32(pri);
 630        } else {
 631                /* Pad WR to 16 byte boundary */
 632                flowc->mnemval[8].mnemonic = 0;
 633                flowc->mnemval[8].val = 0;
 634        }
 635        for (i = 0; i < 9; i++) {
 636                flowc->mnemval[i].r4[0] = 0;
 637                flowc->mnemval[i].r4[1] = 0;
 638                flowc->mnemval[i].r4[2] = 0;
 639        }
 640
 641        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
 642        return c4iw_ofld_send(&ep->com.dev->rdev, skb);
 643}
 644
 645static int send_halfclose(struct c4iw_ep *ep)
 646{
 647        struct sk_buff *skb = skb_dequeue(&ep->com.ep_skb_list);
 648        u32 wrlen = roundup(sizeof(struct cpl_close_con_req), 16);
 649
 650        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
 651        if (WARN_ON(!skb))
 652                return -ENOMEM;
 653
 654        cxgb_mk_close_con_req(skb, wrlen, ep->hwtid, ep->txq_idx,
 655                              NULL, arp_failure_discard);
 656
 657        return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
 658}
 659
 660static int send_abort(struct c4iw_ep *ep)
 661{
 662        u32 wrlen = roundup(sizeof(struct cpl_abort_req), 16);
 663        struct sk_buff *req_skb = skb_dequeue(&ep->com.ep_skb_list);
 664
 665        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
 666        if (WARN_ON(!req_skb))
 667                return -ENOMEM;
 668
 669        cxgb_mk_abort_req(req_skb, wrlen, ep->hwtid, ep->txq_idx,
 670                          ep, abort_arp_failure);
 671
 672        return c4iw_l2t_send(&ep->com.dev->rdev, req_skb, ep->l2t);
 673}
 674
 675static int send_connect(struct c4iw_ep *ep)
 676{
 677        struct cpl_act_open_req *req = NULL;
 678        struct cpl_t5_act_open_req *t5req = NULL;
 679        struct cpl_t6_act_open_req *t6req = NULL;
 680        struct cpl_act_open_req6 *req6 = NULL;
 681        struct cpl_t5_act_open_req6 *t5req6 = NULL;
 682        struct cpl_t6_act_open_req6 *t6req6 = NULL;
 683        struct sk_buff *skb;
 684        u64 opt0;
 685        u32 opt2;
 686        unsigned int mtu_idx;
 687        u32 wscale;
 688        int win, sizev4, sizev6, wrlen;
 689        struct sockaddr_in *la = (struct sockaddr_in *)
 690                                 &ep->com.local_addr;
 691        struct sockaddr_in *ra = (struct sockaddr_in *)
 692                                 &ep->com.remote_addr;
 693        struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)
 694                                   &ep->com.local_addr;
 695        struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)
 696                                   &ep->com.remote_addr;
 697        int ret;
 698        enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
 699        u32 isn = (prandom_u32() & ~7UL) - 1;
 700        struct net_device *netdev;
 701        u64 params;
 702
 703        netdev = ep->com.dev->rdev.lldi.ports[0];
 704
 705        switch (CHELSIO_CHIP_VERSION(adapter_type)) {
 706        case CHELSIO_T4:
 707                sizev4 = sizeof(struct cpl_act_open_req);
 708                sizev6 = sizeof(struct cpl_act_open_req6);
 709                break;
 710        case CHELSIO_T5:
 711                sizev4 = sizeof(struct cpl_t5_act_open_req);
 712                sizev6 = sizeof(struct cpl_t5_act_open_req6);
 713                break;
 714        case CHELSIO_T6:
 715                sizev4 = sizeof(struct cpl_t6_act_open_req);
 716                sizev6 = sizeof(struct cpl_t6_act_open_req6);
 717                break;
 718        default:
 719                pr_err("T%d Chip is not supported\n",
 720                       CHELSIO_CHIP_VERSION(adapter_type));
 721                return -EINVAL;
 722        }
 723
 724        wrlen = (ep->com.remote_addr.ss_family == AF_INET) ?
 725                        roundup(sizev4, 16) :
 726                        roundup(sizev6, 16);
 727
 728        pr_debug("%s ep %p atid %u\n", __func__, ep, ep->atid);
 729
 730        skb = get_skb(NULL, wrlen, GFP_KERNEL);
 731        if (!skb) {
 732                pr_err("%s - failed to alloc skb\n", __func__);
 733                return -ENOMEM;
 734        }
 735        set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
 736
 737        cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
 738                      enable_tcp_timestamps,
 739                      (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
 740        wscale = cxgb_compute_wscale(rcv_win);
 741
 742        /*
 743         * Specify the largest window that will fit in opt0. The
 744         * remainder will be specified in the rx_data_ack.
 745         */
 746        win = ep->rcv_win >> 10;
 747        if (win > RCV_BUFSIZ_M)
 748                win = RCV_BUFSIZ_M;
 749
 750        opt0 = (nocong ? NO_CONG_F : 0) |
 751               KEEP_ALIVE_F |
 752               DELACK_F |
 753               WND_SCALE_V(wscale) |
 754               MSS_IDX_V(mtu_idx) |
 755               L2T_IDX_V(ep->l2t->idx) |
 756               TX_CHAN_V(ep->tx_chan) |
 757               SMAC_SEL_V(ep->smac_idx) |
 758               DSCP_V(ep->tos >> 2) |
 759               ULP_MODE_V(ULP_MODE_TCPDDP) |
 760               RCV_BUFSIZ_V(win);
 761        opt2 = RX_CHANNEL_V(0) |
 762               CCTRL_ECN_V(enable_ecn) |
 763               RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
 764        if (enable_tcp_timestamps)
 765                opt2 |= TSTAMPS_EN_F;
 766        if (enable_tcp_sack)
 767                opt2 |= SACK_EN_F;
 768        if (wscale && enable_tcp_window_scaling)
 769                opt2 |= WND_SCALE_EN_F;
 770        if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
 771                if (peer2peer)
 772                        isn += 4;
 773
 774                opt2 |= T5_OPT_2_VALID_F;
 775                opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
 776                opt2 |= T5_ISS_F;
 777        }
 778
 779        params = cxgb4_select_ntuple(netdev, ep->l2t);
 780
 781        if (ep->com.remote_addr.ss_family == AF_INET6)
 782                cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
 783                               (const u32 *)&la6->sin6_addr.s6_addr, 1);
 784
 785        t4_set_arp_err_handler(skb, ep, act_open_req_arp_failure);
 786
 787        if (ep->com.remote_addr.ss_family == AF_INET) {
 788                switch (CHELSIO_CHIP_VERSION(adapter_type)) {
 789                case CHELSIO_T4:
 790                        req = skb_put(skb, wrlen);
 791                        INIT_TP_WR(req, 0);
 792                        break;
 793                case CHELSIO_T5:
 794                        t5req = skb_put(skb, wrlen);
 795                        INIT_TP_WR(t5req, 0);
 796                        req = (struct cpl_act_open_req *)t5req;
 797                        break;
 798                case CHELSIO_T6:
 799                        t6req = skb_put(skb, wrlen);
 800                        INIT_TP_WR(t6req, 0);
 801                        req = (struct cpl_act_open_req *)t6req;
 802                        t5req = (struct cpl_t5_act_open_req *)t6req;
 803                        break;
 804                default:
 805                        pr_err("T%d Chip is not supported\n",
 806                               CHELSIO_CHIP_VERSION(adapter_type));
 807                        ret = -EINVAL;
 808                        goto clip_release;
 809                }
 810
 811                OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
 812                                        ((ep->rss_qid<<14) | ep->atid)));
 813                req->local_port = la->sin_port;
 814                req->peer_port = ra->sin_port;
 815                req->local_ip = la->sin_addr.s_addr;
 816                req->peer_ip = ra->sin_addr.s_addr;
 817                req->opt0 = cpu_to_be64(opt0);
 818
 819                if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
 820                        req->params = cpu_to_be32(params);
 821                        req->opt2 = cpu_to_be32(opt2);
 822                } else {
 823                        if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
 824                                t5req->params =
 825                                          cpu_to_be64(FILTER_TUPLE_V(params));
 826                                t5req->rsvd = cpu_to_be32(isn);
 827                        pr_debug("%s snd_isn %u\n", __func__, t5req->rsvd);
 828                                t5req->opt2 = cpu_to_be32(opt2);
 829                        } else {
 830                                t6req->params =
 831                                          cpu_to_be64(FILTER_TUPLE_V(params));
 832                                t6req->rsvd = cpu_to_be32(isn);
 833                        pr_debug("%s snd_isn %u\n", __func__, t6req->rsvd);
 834                                t6req->opt2 = cpu_to_be32(opt2);
 835                        }
 836                }
 837        } else {
 838                switch (CHELSIO_CHIP_VERSION(adapter_type)) {
 839                case CHELSIO_T4:
 840                        req6 = skb_put(skb, wrlen);
 841                        INIT_TP_WR(req6, 0);
 842                        break;
 843                case CHELSIO_T5:
 844                        t5req6 = skb_put(skb, wrlen);
 845                        INIT_TP_WR(t5req6, 0);
 846                        req6 = (struct cpl_act_open_req6 *)t5req6;
 847                        break;
 848                case CHELSIO_T6:
 849                        t6req6 = skb_put(skb, wrlen);
 850                        INIT_TP_WR(t6req6, 0);
 851                        req6 = (struct cpl_act_open_req6 *)t6req6;
 852                        t5req6 = (struct cpl_t5_act_open_req6 *)t6req6;
 853                        break;
 854                default:
 855                        pr_err("T%d Chip is not supported\n",
 856                               CHELSIO_CHIP_VERSION(adapter_type));
 857                        ret = -EINVAL;
 858                        goto clip_release;
 859                }
 860
 861                OPCODE_TID(req6) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
 862                                        ((ep->rss_qid<<14)|ep->atid)));
 863                req6->local_port = la6->sin6_port;
 864                req6->peer_port = ra6->sin6_port;
 865                req6->local_ip_hi = *((__be64 *)(la6->sin6_addr.s6_addr));
 866                req6->local_ip_lo = *((__be64 *)(la6->sin6_addr.s6_addr + 8));
 867                req6->peer_ip_hi = *((__be64 *)(ra6->sin6_addr.s6_addr));
 868                req6->peer_ip_lo = *((__be64 *)(ra6->sin6_addr.s6_addr + 8));
 869                req6->opt0 = cpu_to_be64(opt0);
 870
 871                if (is_t4(ep->com.dev->rdev.lldi.adapter_type)) {
 872                        req6->params = cpu_to_be32(cxgb4_select_ntuple(netdev,
 873                                                                      ep->l2t));
 874                        req6->opt2 = cpu_to_be32(opt2);
 875                } else {
 876                        if (is_t5(ep->com.dev->rdev.lldi.adapter_type)) {
 877                                t5req6->params =
 878                                            cpu_to_be64(FILTER_TUPLE_V(params));
 879                                t5req6->rsvd = cpu_to_be32(isn);
 880                        pr_debug("%s snd_isn %u\n", __func__, t5req6->rsvd);
 881                                t5req6->opt2 = cpu_to_be32(opt2);
 882                        } else {
 883                                t6req6->params =
 884                                            cpu_to_be64(FILTER_TUPLE_V(params));
 885                                t6req6->rsvd = cpu_to_be32(isn);
 886                        pr_debug("%s snd_isn %u\n", __func__, t6req6->rsvd);
 887                                t6req6->opt2 = cpu_to_be32(opt2);
 888                        }
 889
 890                }
 891        }
 892
 893        set_bit(ACT_OPEN_REQ, &ep->com.history);
 894        ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
 895clip_release:
 896        if (ret && ep->com.remote_addr.ss_family == AF_INET6)
 897                cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
 898                                   (const u32 *)&la6->sin6_addr.s6_addr, 1);
 899        return ret;
 900}
 901
 902static int send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
 903                        u8 mpa_rev_to_use)
 904{
 905        int mpalen, wrlen, ret;
 906        struct fw_ofld_tx_data_wr *req;
 907        struct mpa_message *mpa;
 908        struct mpa_v2_conn_params mpa_v2_params;
 909
 910        pr_debug("%s ep %p tid %u pd_len %d\n",
 911                 __func__, ep, ep->hwtid, ep->plen);
 912
 913        BUG_ON(skb_cloned(skb));
 914
 915        mpalen = sizeof(*mpa) + ep->plen;
 916        if (mpa_rev_to_use == 2)
 917                mpalen += sizeof(struct mpa_v2_conn_params);
 918        wrlen = roundup(mpalen + sizeof *req, 16);
 919        skb = get_skb(skb, wrlen, GFP_KERNEL);
 920        if (!skb) {
 921                connect_reply_upcall(ep, -ENOMEM);
 922                return -ENOMEM;
 923        }
 924        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
 925
 926        req = skb_put_zero(skb, wrlen);
 927        req->op_to_immdlen = cpu_to_be32(
 928                FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
 929                FW_WR_COMPL_F |
 930                FW_WR_IMMDLEN_V(mpalen));
 931        req->flowid_len16 = cpu_to_be32(
 932                FW_WR_FLOWID_V(ep->hwtid) |
 933                FW_WR_LEN16_V(wrlen >> 4));
 934        req->plen = cpu_to_be32(mpalen);
 935        req->tunnel_to_proxy = cpu_to_be32(
 936                FW_OFLD_TX_DATA_WR_FLUSH_F |
 937                FW_OFLD_TX_DATA_WR_SHOVE_F);
 938
 939        mpa = (struct mpa_message *)(req + 1);
 940        memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
 941
 942        mpa->flags = 0;
 943        if (crc_enabled)
 944                mpa->flags |= MPA_CRC;
 945        if (markers_enabled) {
 946                mpa->flags |= MPA_MARKERS;
 947                ep->mpa_attr.recv_marker_enabled = 1;
 948        } else {
 949                ep->mpa_attr.recv_marker_enabled = 0;
 950        }
 951        if (mpa_rev_to_use == 2)
 952                mpa->flags |= MPA_ENHANCED_RDMA_CONN;
 953
 954        mpa->private_data_size = htons(ep->plen);
 955        mpa->revision = mpa_rev_to_use;
 956        if (mpa_rev_to_use == 1) {
 957                ep->tried_with_mpa_v1 = 1;
 958                ep->retry_with_mpa_v1 = 0;
 959        }
 960
 961        if (mpa_rev_to_use == 2) {
 962                mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
 963                                               sizeof (struct mpa_v2_conn_params));
 964                pr_debug("%s initiator ird %u ord %u\n", __func__, ep->ird,
 965                         ep->ord);
 966                mpa_v2_params.ird = htons((u16)ep->ird);
 967                mpa_v2_params.ord = htons((u16)ep->ord);
 968
 969                if (peer2peer) {
 970                        mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
 971                        if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
 972                                mpa_v2_params.ord |=
 973                                        htons(MPA_V2_RDMA_WRITE_RTR);
 974                        else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
 975                                mpa_v2_params.ord |=
 976                                        htons(MPA_V2_RDMA_READ_RTR);
 977                }
 978                memcpy(mpa->private_data, &mpa_v2_params,
 979                       sizeof(struct mpa_v2_conn_params));
 980
 981                if (ep->plen)
 982                        memcpy(mpa->private_data +
 983                               sizeof(struct mpa_v2_conn_params),
 984                               ep->mpa_pkt + sizeof(*mpa), ep->plen);
 985        } else
 986                if (ep->plen)
 987                        memcpy(mpa->private_data,
 988                                        ep->mpa_pkt + sizeof(*mpa), ep->plen);
 989
 990        /*
 991         * Reference the mpa skb.  This ensures the data area
 992         * will remain in memory until the hw acks the tx.
 993         * Function fw4_ack() will deref it.
 994         */
 995        skb_get(skb);
 996        t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
 997        BUG_ON(ep->mpa_skb);
 998        ep->mpa_skb = skb;
 999        ret = c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1000        if (ret)
1001                return ret;
1002        start_ep_timer(ep);
1003        __state_set(&ep->com, MPA_REQ_SENT);
1004        ep->mpa_attr.initiator = 1;
1005        ep->snd_seq += mpalen;
1006        return ret;
1007}
1008
1009static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
1010{
1011        int mpalen, wrlen;
1012        struct fw_ofld_tx_data_wr *req;
1013        struct mpa_message *mpa;
1014        struct sk_buff *skb;
1015        struct mpa_v2_conn_params mpa_v2_params;
1016
1017        pr_debug("%s ep %p tid %u pd_len %d\n",
1018                 __func__, ep, ep->hwtid, ep->plen);
1019
1020        mpalen = sizeof(*mpa) + plen;
1021        if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
1022                mpalen += sizeof(struct mpa_v2_conn_params);
1023        wrlen = roundup(mpalen + sizeof *req, 16);
1024
1025        skb = get_skb(NULL, wrlen, GFP_KERNEL);
1026        if (!skb) {
1027                pr_err("%s - cannot alloc skb!\n", __func__);
1028                return -ENOMEM;
1029        }
1030        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1031
1032        req = skb_put_zero(skb, wrlen);
1033        req->op_to_immdlen = cpu_to_be32(
1034                FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
1035                FW_WR_COMPL_F |
1036                FW_WR_IMMDLEN_V(mpalen));
1037        req->flowid_len16 = cpu_to_be32(
1038                FW_WR_FLOWID_V(ep->hwtid) |
1039                FW_WR_LEN16_V(wrlen >> 4));
1040        req->plen = cpu_to_be32(mpalen);
1041        req->tunnel_to_proxy = cpu_to_be32(
1042                FW_OFLD_TX_DATA_WR_FLUSH_F |
1043                FW_OFLD_TX_DATA_WR_SHOVE_F);
1044
1045        mpa = (struct mpa_message *)(req + 1);
1046        memset(mpa, 0, sizeof(*mpa));
1047        memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1048        mpa->flags = MPA_REJECT;
1049        mpa->revision = ep->mpa_attr.version;
1050        mpa->private_data_size = htons(plen);
1051
1052        if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1053                mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1054                mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1055                                               sizeof (struct mpa_v2_conn_params));
1056                mpa_v2_params.ird = htons(((u16)ep->ird) |
1057                                          (peer2peer ? MPA_V2_PEER2PEER_MODEL :
1058                                           0));
1059                mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
1060                                          (p2p_type ==
1061                                           FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
1062                                           MPA_V2_RDMA_WRITE_RTR : p2p_type ==
1063                                           FW_RI_INIT_P2PTYPE_READ_REQ ?
1064                                           MPA_V2_RDMA_READ_RTR : 0) : 0));
1065                memcpy(mpa->private_data, &mpa_v2_params,
1066                       sizeof(struct mpa_v2_conn_params));
1067
1068                if (ep->plen)
1069                        memcpy(mpa->private_data +
1070                               sizeof(struct mpa_v2_conn_params), pdata, plen);
1071        } else
1072                if (plen)
1073                        memcpy(mpa->private_data, pdata, plen);
1074
1075        /*
1076         * Reference the mpa skb again.  This ensures the data area
1077         * will remain in memory until the hw acks the tx.
1078         * Function fw4_ack() will deref it.
1079         */
1080        skb_get(skb);
1081        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1082        t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1083        BUG_ON(ep->mpa_skb);
1084        ep->mpa_skb = skb;
1085        ep->snd_seq += mpalen;
1086        return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1087}
1088
1089static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
1090{
1091        int mpalen, wrlen;
1092        struct fw_ofld_tx_data_wr *req;
1093        struct mpa_message *mpa;
1094        struct sk_buff *skb;
1095        struct mpa_v2_conn_params mpa_v2_params;
1096
1097        pr_debug("%s ep %p tid %u pd_len %d\n",
1098                 __func__, ep, ep->hwtid, ep->plen);
1099
1100        mpalen = sizeof(*mpa) + plen;
1101        if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
1102                mpalen += sizeof(struct mpa_v2_conn_params);
1103        wrlen = roundup(mpalen + sizeof *req, 16);
1104
1105        skb = get_skb(NULL, wrlen, GFP_KERNEL);
1106        if (!skb) {
1107                pr_err("%s - cannot alloc skb!\n", __func__);
1108                return -ENOMEM;
1109        }
1110        set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
1111
1112        req = skb_put_zero(skb, wrlen);
1113        req->op_to_immdlen = cpu_to_be32(
1114                FW_WR_OP_V(FW_OFLD_TX_DATA_WR) |
1115                FW_WR_COMPL_F |
1116                FW_WR_IMMDLEN_V(mpalen));
1117        req->flowid_len16 = cpu_to_be32(
1118                FW_WR_FLOWID_V(ep->hwtid) |
1119                FW_WR_LEN16_V(wrlen >> 4));
1120        req->plen = cpu_to_be32(mpalen);
1121        req->tunnel_to_proxy = cpu_to_be32(
1122                FW_OFLD_TX_DATA_WR_FLUSH_F |
1123                FW_OFLD_TX_DATA_WR_SHOVE_F);
1124
1125        mpa = (struct mpa_message *)(req + 1);
1126        memset(mpa, 0, sizeof(*mpa));
1127        memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1128        mpa->flags = 0;
1129        if (ep->mpa_attr.crc_enabled)
1130                mpa->flags |= MPA_CRC;
1131        if (ep->mpa_attr.recv_marker_enabled)
1132                mpa->flags |= MPA_MARKERS;
1133        mpa->revision = ep->mpa_attr.version;
1134        mpa->private_data_size = htons(plen);
1135
1136        if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1137                mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1138                mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1139                                               sizeof (struct mpa_v2_conn_params));
1140                mpa_v2_params.ird = htons((u16)ep->ird);
1141                mpa_v2_params.ord = htons((u16)ep->ord);
1142                if (peer2peer && (ep->mpa_attr.p2p_type !=
1143                                        FW_RI_INIT_P2PTYPE_DISABLED)) {
1144                        mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1145
1146                        if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
1147                                mpa_v2_params.ord |=
1148                                        htons(MPA_V2_RDMA_WRITE_RTR);
1149                        else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
1150                                mpa_v2_params.ord |=
1151                                        htons(MPA_V2_RDMA_READ_RTR);
1152                }
1153
1154                memcpy(mpa->private_data, &mpa_v2_params,
1155                       sizeof(struct mpa_v2_conn_params));
1156
1157                if (ep->plen)
1158                        memcpy(mpa->private_data +
1159                               sizeof(struct mpa_v2_conn_params), pdata, plen);
1160        } else
1161                if (plen)
1162                        memcpy(mpa->private_data, pdata, plen);
1163
1164        /*
1165         * Reference the mpa skb.  This ensures the data area
1166         * will remain in memory until the hw acks the tx.
1167         * Function fw4_ack() will deref it.
1168         */
1169        skb_get(skb);
1170        t4_set_arp_err_handler(skb, NULL, mpa_start_arp_failure);
1171        ep->mpa_skb = skb;
1172        __state_set(&ep->com, MPA_REP_SENT);
1173        ep->snd_seq += mpalen;
1174        return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1175}
1176
1177static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
1178{
1179        struct c4iw_ep *ep;
1180        struct cpl_act_establish *req = cplhdr(skb);
1181        unsigned int tid = GET_TID(req);
1182        unsigned int atid = TID_TID_G(ntohl(req->tos_atid));
1183        struct tid_info *t = dev->rdev.lldi.tids;
1184        int ret;
1185
1186        ep = lookup_atid(t, atid);
1187
1188        pr_debug("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
1189                 be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
1190
1191        mutex_lock(&ep->com.mutex);
1192        dst_confirm(ep->dst);
1193
1194        /* setup the hwtid for this connection */
1195        ep->hwtid = tid;
1196        cxgb4_insert_tid(t, ep, tid, ep->com.local_addr.ss_family);
1197        insert_ep_tid(ep);
1198
1199        ep->snd_seq = be32_to_cpu(req->snd_isn);
1200        ep->rcv_seq = be32_to_cpu(req->rcv_isn);
1201
1202        set_emss(ep, ntohs(req->tcp_opt));
1203
1204        /* dealloc the atid */
1205        remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
1206        cxgb4_free_atid(t, atid);
1207        set_bit(ACT_ESTAB, &ep->com.history);
1208
1209        /* start MPA negotiation */
1210        ret = send_flowc(ep);
1211        if (ret)
1212                goto err;
1213        if (ep->retry_with_mpa_v1)
1214                ret = send_mpa_req(ep, skb, 1);
1215        else
1216                ret = send_mpa_req(ep, skb, mpa_rev);
1217        if (ret)
1218                goto err;
1219        mutex_unlock(&ep->com.mutex);
1220        return 0;
1221err:
1222        mutex_unlock(&ep->com.mutex);
1223        connect_reply_upcall(ep, -ENOMEM);
1224        c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1225        return 0;
1226}
1227
1228static void close_complete_upcall(struct c4iw_ep *ep, int status)
1229{
1230        struct iw_cm_event event;
1231
1232        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1233        memset(&event, 0, sizeof(event));
1234        event.event = IW_CM_EVENT_CLOSE;
1235        event.status = status;
1236        if (ep->com.cm_id) {
1237                pr_debug("close complete delivered ep %p cm_id %p tid %u\n",
1238                         ep, ep->com.cm_id, ep->hwtid);
1239                ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1240                deref_cm_id(&ep->com);
1241                set_bit(CLOSE_UPCALL, &ep->com.history);
1242        }
1243}
1244
1245static void peer_close_upcall(struct c4iw_ep *ep)
1246{
1247        struct iw_cm_event event;
1248
1249        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1250        memset(&event, 0, sizeof(event));
1251        event.event = IW_CM_EVENT_DISCONNECT;
1252        if (ep->com.cm_id) {
1253                pr_debug("peer close delivered ep %p cm_id %p tid %u\n",
1254                         ep, ep->com.cm_id, ep->hwtid);
1255                ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1256                set_bit(DISCONN_UPCALL, &ep->com.history);
1257        }
1258}
1259
1260static void peer_abort_upcall(struct c4iw_ep *ep)
1261{
1262        struct iw_cm_event event;
1263
1264        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1265        memset(&event, 0, sizeof(event));
1266        event.event = IW_CM_EVENT_CLOSE;
1267        event.status = -ECONNRESET;
1268        if (ep->com.cm_id) {
1269                pr_debug("abort delivered ep %p cm_id %p tid %u\n", ep,
1270                         ep->com.cm_id, ep->hwtid);
1271                ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1272                deref_cm_id(&ep->com);
1273                set_bit(ABORT_UPCALL, &ep->com.history);
1274        }
1275}
1276
1277static void connect_reply_upcall(struct c4iw_ep *ep, int status)
1278{
1279        struct iw_cm_event event;
1280
1281        pr_debug("%s ep %p tid %u status %d\n",
1282                 __func__, ep, ep->hwtid, status);
1283        memset(&event, 0, sizeof(event));
1284        event.event = IW_CM_EVENT_CONNECT_REPLY;
1285        event.status = status;
1286        memcpy(&event.local_addr, &ep->com.local_addr,
1287               sizeof(ep->com.local_addr));
1288        memcpy(&event.remote_addr, &ep->com.remote_addr,
1289               sizeof(ep->com.remote_addr));
1290
1291        if ((status == 0) || (status == -ECONNREFUSED)) {
1292                if (!ep->tried_with_mpa_v1) {
1293                        /* this means MPA_v2 is used */
1294                        event.ord = ep->ird;
1295                        event.ird = ep->ord;
1296                        event.private_data_len = ep->plen -
1297                                sizeof(struct mpa_v2_conn_params);
1298                        event.private_data = ep->mpa_pkt +
1299                                sizeof(struct mpa_message) +
1300                                sizeof(struct mpa_v2_conn_params);
1301                } else {
1302                        /* this means MPA_v1 is used */
1303                        event.ord = cur_max_read_depth(ep->com.dev);
1304                        event.ird = cur_max_read_depth(ep->com.dev);
1305                        event.private_data_len = ep->plen;
1306                        event.private_data = ep->mpa_pkt +
1307                                sizeof(struct mpa_message);
1308                }
1309        }
1310
1311        pr_debug("%s ep %p tid %u status %d\n", __func__, ep,
1312                 ep->hwtid, status);
1313        set_bit(CONN_RPL_UPCALL, &ep->com.history);
1314        ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1315
1316        if (status < 0)
1317                deref_cm_id(&ep->com);
1318}
1319
1320static int connect_request_upcall(struct c4iw_ep *ep)
1321{
1322        struct iw_cm_event event;
1323        int ret;
1324
1325        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1326        memset(&event, 0, sizeof(event));
1327        event.event = IW_CM_EVENT_CONNECT_REQUEST;
1328        memcpy(&event.local_addr, &ep->com.local_addr,
1329               sizeof(ep->com.local_addr));
1330        memcpy(&event.remote_addr, &ep->com.remote_addr,
1331               sizeof(ep->com.remote_addr));
1332        event.provider_data = ep;
1333        if (!ep->tried_with_mpa_v1) {
1334                /* this means MPA_v2 is used */
1335                event.ord = ep->ord;
1336                event.ird = ep->ird;
1337                event.private_data_len = ep->plen -
1338                        sizeof(struct mpa_v2_conn_params);
1339                event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
1340                        sizeof(struct mpa_v2_conn_params);
1341        } else {
1342                /* this means MPA_v1 is used. Send max supported */
1343                event.ord = cur_max_read_depth(ep->com.dev);
1344                event.ird = cur_max_read_depth(ep->com.dev);
1345                event.private_data_len = ep->plen;
1346                event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
1347        }
1348        c4iw_get_ep(&ep->com);
1349        ret = ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
1350                                                      &event);
1351        if (ret)
1352                c4iw_put_ep(&ep->com);
1353        set_bit(CONNREQ_UPCALL, &ep->com.history);
1354        c4iw_put_ep(&ep->parent_ep->com);
1355        return ret;
1356}
1357
1358static void established_upcall(struct c4iw_ep *ep)
1359{
1360        struct iw_cm_event event;
1361
1362        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1363        memset(&event, 0, sizeof(event));
1364        event.event = IW_CM_EVENT_ESTABLISHED;
1365        event.ird = ep->ord;
1366        event.ord = ep->ird;
1367        if (ep->com.cm_id) {
1368                pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1369                ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1370                set_bit(ESTAB_UPCALL, &ep->com.history);
1371        }
1372}
1373
1374static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
1375{
1376        struct sk_buff *skb;
1377        u32 wrlen = roundup(sizeof(struct cpl_rx_data_ack), 16);
1378        u32 credit_dack;
1379
1380        pr_debug("%s ep %p tid %u credits %u\n",
1381                 __func__, ep, ep->hwtid, credits);
1382        skb = get_skb(NULL, wrlen, GFP_KERNEL);
1383        if (!skb) {
1384                pr_err("update_rx_credits - cannot alloc skb!\n");
1385                return 0;
1386        }
1387
1388        /*
1389         * If we couldn't specify the entire rcv window at connection setup
1390         * due to the limit in the number of bits in the RCV_BUFSIZ field,
1391         * then add the overage in to the credits returned.
1392         */
1393        if (ep->rcv_win > RCV_BUFSIZ_M * 1024)
1394                credits += ep->rcv_win - RCV_BUFSIZ_M * 1024;
1395
1396        credit_dack = credits | RX_FORCE_ACK_F | RX_DACK_CHANGE_F |
1397                      RX_DACK_MODE_V(dack_mode);
1398
1399        cxgb_mk_rx_data_ack(skb, wrlen, ep->hwtid, ep->ctrlq_idx,
1400                            credit_dack);
1401
1402        c4iw_ofld_send(&ep->com.dev->rdev, skb);
1403        return credits;
1404}
1405
1406#define RELAXED_IRD_NEGOTIATION 1
1407
1408/*
1409 * process_mpa_reply - process streaming mode MPA reply
1410 *
1411 * Returns:
1412 *
1413 * 0 upon success indicating a connect request was delivered to the ULP
1414 * or the mpa request is incomplete but valid so far.
1415 *
1416 * 1 if a failure requires the caller to close the connection.
1417 *
1418 * 2 if a failure requires the caller to abort the connection.
1419 */
1420static int process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
1421{
1422        struct mpa_message *mpa;
1423        struct mpa_v2_conn_params *mpa_v2_params;
1424        u16 plen;
1425        u16 resp_ird, resp_ord;
1426        u8 rtr_mismatch = 0, insuff_ird = 0;
1427        struct c4iw_qp_attributes attrs;
1428        enum c4iw_qp_attr_mask mask;
1429        int err;
1430        int disconnect = 0;
1431
1432        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1433
1434        /*
1435         * If we get more than the supported amount of private data
1436         * then we must fail this connection.
1437         */
1438        if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1439                err = -EINVAL;
1440                goto err_stop_timer;
1441        }
1442
1443        /*
1444         * copy the new data into our accumulation buffer.
1445         */
1446        skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1447                                  skb->len);
1448        ep->mpa_pkt_len += skb->len;
1449
1450        /*
1451         * if we don't even have the mpa message, then bail.
1452         */
1453        if (ep->mpa_pkt_len < sizeof(*mpa))
1454                return 0;
1455        mpa = (struct mpa_message *) ep->mpa_pkt;
1456
1457        /* Validate MPA header. */
1458        if (mpa->revision > mpa_rev) {
1459                pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
1460                       __func__, mpa_rev, mpa->revision);
1461                err = -EPROTO;
1462                goto err_stop_timer;
1463        }
1464        if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1465                err = -EPROTO;
1466                goto err_stop_timer;
1467        }
1468
1469        plen = ntohs(mpa->private_data_size);
1470
1471        /*
1472         * Fail if there's too much private data.
1473         */
1474        if (plen > MPA_MAX_PRIVATE_DATA) {
1475                err = -EPROTO;
1476                goto err_stop_timer;
1477        }
1478
1479        /*
1480         * If plen does not account for pkt size
1481         */
1482        if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1483                err = -EPROTO;
1484                goto err_stop_timer;
1485        }
1486
1487        ep->plen = (u8) plen;
1488
1489        /*
1490         * If we don't have all the pdata yet, then bail.
1491         * We'll continue process when more data arrives.
1492         */
1493        if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1494                return 0;
1495
1496        if (mpa->flags & MPA_REJECT) {
1497                err = -ECONNREFUSED;
1498                goto err_stop_timer;
1499        }
1500
1501        /*
1502         * Stop mpa timer.  If it expired, then
1503         * we ignore the MPA reply.  process_timeout()
1504         * will abort the connection.
1505         */
1506        if (stop_ep_timer(ep))
1507                return 0;
1508
1509        /*
1510         * If we get here we have accumulated the entire mpa
1511         * start reply message including private data. And
1512         * the MPA header is valid.
1513         */
1514        __state_set(&ep->com, FPDU_MODE);
1515        ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1516        ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1517        ep->mpa_attr.version = mpa->revision;
1518        ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1519
1520        if (mpa->revision == 2) {
1521                ep->mpa_attr.enhanced_rdma_conn =
1522                        mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1523                if (ep->mpa_attr.enhanced_rdma_conn) {
1524                        mpa_v2_params = (struct mpa_v2_conn_params *)
1525                                (ep->mpa_pkt + sizeof(*mpa));
1526                        resp_ird = ntohs(mpa_v2_params->ird) &
1527                                MPA_V2_IRD_ORD_MASK;
1528                        resp_ord = ntohs(mpa_v2_params->ord) &
1529                                MPA_V2_IRD_ORD_MASK;
1530                        pr_debug("%s responder ird %u ord %u ep ird %u ord %u\n",
1531                                 __func__,
1532                                 resp_ird, resp_ord, ep->ird, ep->ord);
1533
1534                        /*
1535                         * This is a double-check. Ideally, below checks are
1536                         * not required since ird/ord stuff has been taken
1537                         * care of in c4iw_accept_cr
1538                         */
1539                        if (ep->ird < resp_ord) {
1540                                if (RELAXED_IRD_NEGOTIATION && resp_ord <=
1541                                    ep->com.dev->rdev.lldi.max_ordird_qp)
1542                                        ep->ird = resp_ord;
1543                                else
1544                                        insuff_ird = 1;
1545                        } else if (ep->ird > resp_ord) {
1546                                ep->ird = resp_ord;
1547                        }
1548                        if (ep->ord > resp_ird) {
1549                                if (RELAXED_IRD_NEGOTIATION)
1550                                        ep->ord = resp_ird;
1551                                else
1552                                        insuff_ird = 1;
1553                        }
1554                        if (insuff_ird) {
1555                                err = -ENOMEM;
1556                                ep->ird = resp_ord;
1557                                ep->ord = resp_ird;
1558                        }
1559
1560                        if (ntohs(mpa_v2_params->ird) &
1561                                        MPA_V2_PEER2PEER_MODEL) {
1562                                if (ntohs(mpa_v2_params->ord) &
1563                                                MPA_V2_RDMA_WRITE_RTR)
1564                                        ep->mpa_attr.p2p_type =
1565                                                FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1566                                else if (ntohs(mpa_v2_params->ord) &
1567                                                MPA_V2_RDMA_READ_RTR)
1568                                        ep->mpa_attr.p2p_type =
1569                                                FW_RI_INIT_P2PTYPE_READ_REQ;
1570                        }
1571                }
1572        } else if (mpa->revision == 1)
1573                if (peer2peer)
1574                        ep->mpa_attr.p2p_type = p2p_type;
1575
1576        pr_debug("%s - crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = %d\n",
1577                 __func__, ep->mpa_attr.crc_enabled,
1578                 ep->mpa_attr.recv_marker_enabled,
1579                 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1580                 ep->mpa_attr.p2p_type, p2p_type);
1581
1582        /*
1583         * If responder's RTR does not match with that of initiator, assign
1584         * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1585         * generated when moving QP to RTS state.
1586         * A TERM message will be sent after QP has moved to RTS state
1587         */
1588        if ((ep->mpa_attr.version == 2) && peer2peer &&
1589                        (ep->mpa_attr.p2p_type != p2p_type)) {
1590                ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1591                rtr_mismatch = 1;
1592        }
1593
1594        attrs.mpa_attr = ep->mpa_attr;
1595        attrs.max_ird = ep->ird;
1596        attrs.max_ord = ep->ord;
1597        attrs.llp_stream_handle = ep;
1598        attrs.next_state = C4IW_QP_STATE_RTS;
1599
1600        mask = C4IW_QP_ATTR_NEXT_STATE |
1601            C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1602            C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1603
1604        /* bind QP and TID with INIT_WR */
1605        err = c4iw_modify_qp(ep->com.qp->rhp,
1606                             ep->com.qp, mask, &attrs, 1);
1607        if (err)
1608                goto err;
1609
1610        /*
1611         * If responder's RTR requirement did not match with what initiator
1612         * supports, generate TERM message
1613         */
1614        if (rtr_mismatch) {
1615                pr_err("%s: RTR mismatch, sending TERM\n", __func__);
1616                attrs.layer_etype = LAYER_MPA | DDP_LLP;
1617                attrs.ecode = MPA_NOMATCH_RTR;
1618                attrs.next_state = C4IW_QP_STATE_TERMINATE;
1619                attrs.send_term = 1;
1620                err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1621                                C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1622                err = -ENOMEM;
1623                disconnect = 1;
1624                goto out;
1625        }
1626
1627        /*
1628         * Generate TERM if initiator IRD is not sufficient for responder
1629         * provided ORD. Currently, we do the same behaviour even when
1630         * responder provided IRD is also not sufficient as regards to
1631         * initiator ORD.
1632         */
1633        if (insuff_ird) {
1634                pr_err("%s: Insufficient IRD, sending TERM\n", __func__);
1635                attrs.layer_etype = LAYER_MPA | DDP_LLP;
1636                attrs.ecode = MPA_INSUFF_IRD;
1637                attrs.next_state = C4IW_QP_STATE_TERMINATE;
1638                attrs.send_term = 1;
1639                err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1640                                C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1641                err = -ENOMEM;
1642                disconnect = 1;
1643                goto out;
1644        }
1645        goto out;
1646err_stop_timer:
1647        stop_ep_timer(ep);
1648err:
1649        disconnect = 2;
1650out:
1651        connect_reply_upcall(ep, err);
1652        return disconnect;
1653}
1654
1655/*
1656 * process_mpa_request - process streaming mode MPA request
1657 *
1658 * Returns:
1659 *
1660 * 0 upon success indicating a connect request was delivered to the ULP
1661 * or the mpa request is incomplete but valid so far.
1662 *
1663 * 1 if a failure requires the caller to close the connection.
1664 *
1665 * 2 if a failure requires the caller to abort the connection.
1666 */
1667static int process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
1668{
1669        struct mpa_message *mpa;
1670        struct mpa_v2_conn_params *mpa_v2_params;
1671        u16 plen;
1672
1673        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1674
1675        /*
1676         * If we get more than the supported amount of private data
1677         * then we must fail this connection.
1678         */
1679        if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt))
1680                goto err_stop_timer;
1681
1682        pr_debug("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1683
1684        /*
1685         * Copy the new data into our accumulation buffer.
1686         */
1687        skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1688                                  skb->len);
1689        ep->mpa_pkt_len += skb->len;
1690
1691        /*
1692         * If we don't even have the mpa message, then bail.
1693         * We'll continue process when more data arrives.
1694         */
1695        if (ep->mpa_pkt_len < sizeof(*mpa))
1696                return 0;
1697
1698        pr_debug("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1699        mpa = (struct mpa_message *) ep->mpa_pkt;
1700
1701        /*
1702         * Validate MPA Header.
1703         */
1704        if (mpa->revision > mpa_rev) {
1705                pr_err("%s MPA version mismatch. Local = %d, Received = %d\n",
1706                       __func__, mpa_rev, mpa->revision);
1707                goto err_stop_timer;
1708        }
1709
1710        if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
1711                goto err_stop_timer;
1712
1713        plen = ntohs(mpa->private_data_size);
1714
1715        /*
1716         * Fail if there's too much private data.
1717         */
1718        if (plen > MPA_MAX_PRIVATE_DATA)
1719                goto err_stop_timer;
1720
1721        /*
1722         * If plen does not account for pkt size
1723         */
1724        if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
1725                goto err_stop_timer;
1726        ep->plen = (u8) plen;
1727
1728        /*
1729         * If we don't have all the pdata yet, then bail.
1730         */
1731        if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1732                return 0;
1733
1734        /*
1735         * If we get here we have accumulated the entire mpa
1736         * start reply message including private data.
1737         */
1738        ep->mpa_attr.initiator = 0;
1739        ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1740        ep->mpa_attr.recv_marker_enabled = markers_enabled;
1741        ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1742        ep->mpa_attr.version = mpa->revision;
1743        if (mpa->revision == 1)
1744                ep->tried_with_mpa_v1 = 1;
1745        ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1746
1747        if (mpa->revision == 2) {
1748                ep->mpa_attr.enhanced_rdma_conn =
1749                        mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1750                if (ep->mpa_attr.enhanced_rdma_conn) {
1751                        mpa_v2_params = (struct mpa_v2_conn_params *)
1752                                (ep->mpa_pkt + sizeof(*mpa));
1753                        ep->ird = ntohs(mpa_v2_params->ird) &
1754                                MPA_V2_IRD_ORD_MASK;
1755                        ep->ird = min_t(u32, ep->ird,
1756                                        cur_max_read_depth(ep->com.dev));
1757                        ep->ord = ntohs(mpa_v2_params->ord) &
1758                                MPA_V2_IRD_ORD_MASK;
1759                        ep->ord = min_t(u32, ep->ord,
1760                                        cur_max_read_depth(ep->com.dev));
1761                        pr_debug("%s initiator ird %u ord %u\n",
1762                                 __func__, ep->ird, ep->ord);
1763                        if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
1764                                if (peer2peer) {
1765                                        if (ntohs(mpa_v2_params->ord) &
1766                                                        MPA_V2_RDMA_WRITE_RTR)
1767                                                ep->mpa_attr.p2p_type =
1768                                                FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1769                                        else if (ntohs(mpa_v2_params->ord) &
1770                                                        MPA_V2_RDMA_READ_RTR)
1771                                                ep->mpa_attr.p2p_type =
1772                                                FW_RI_INIT_P2PTYPE_READ_REQ;
1773                                }
1774                }
1775        } else if (mpa->revision == 1)
1776                if (peer2peer)
1777                        ep->mpa_attr.p2p_type = p2p_type;
1778
1779        pr_debug("%s - crc_enabled=%d, recv_marker_enabled=%d, xmit_marker_enabled=%d, version=%d p2p_type=%d\n",
1780                 __func__,
1781                 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1782                 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1783                 ep->mpa_attr.p2p_type);
1784
1785        __state_set(&ep->com, MPA_REQ_RCVD);
1786
1787        /* drive upcall */
1788        mutex_lock_nested(&ep->parent_ep->com.mutex, SINGLE_DEPTH_NESTING);
1789        if (ep->parent_ep->com.state != DEAD) {
1790                if (connect_request_upcall(ep))
1791                        goto err_unlock_parent;
1792        } else {
1793                goto err_unlock_parent;
1794        }
1795        mutex_unlock(&ep->parent_ep->com.mutex);
1796        return 0;
1797
1798err_unlock_parent:
1799        mutex_unlock(&ep->parent_ep->com.mutex);
1800        goto err_out;
1801err_stop_timer:
1802        (void)stop_ep_timer(ep);
1803err_out:
1804        return 2;
1805}
1806
1807static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
1808{
1809        struct c4iw_ep *ep;
1810        struct cpl_rx_data *hdr = cplhdr(skb);
1811        unsigned int dlen = ntohs(hdr->len);
1812        unsigned int tid = GET_TID(hdr);
1813        __u8 status = hdr->status;
1814        int disconnect = 0;
1815
1816        ep = get_ep_from_tid(dev, tid);
1817        if (!ep)
1818                return 0;
1819        pr_debug("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
1820        skb_pull(skb, sizeof(*hdr));
1821        skb_trim(skb, dlen);
1822        mutex_lock(&ep->com.mutex);
1823
1824        switch (ep->com.state) {
1825        case MPA_REQ_SENT:
1826                update_rx_credits(ep, dlen);
1827                ep->rcv_seq += dlen;
1828                disconnect = process_mpa_reply(ep, skb);
1829                break;
1830        case MPA_REQ_WAIT:
1831                update_rx_credits(ep, dlen);
1832                ep->rcv_seq += dlen;
1833                disconnect = process_mpa_request(ep, skb);
1834                break;
1835        case FPDU_MODE: {
1836                struct c4iw_qp_attributes attrs;
1837
1838                update_rx_credits(ep, dlen);
1839                BUG_ON(!ep->com.qp);
1840                if (status)
1841                        pr_err("%s Unexpected streaming data." \
1842                               " qpid %u ep %p state %d tid %u status %d\n",
1843                               __func__, ep->com.qp->wq.sq.qid, ep,
1844                               ep->com.state, ep->hwtid, status);
1845                attrs.next_state = C4IW_QP_STATE_TERMINATE;
1846                c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1847                               C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1848                disconnect = 1;
1849                break;
1850        }
1851        default:
1852                break;
1853        }
1854        mutex_unlock(&ep->com.mutex);
1855        if (disconnect)
1856                c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
1857        c4iw_put_ep(&ep->com);
1858        return 0;
1859}
1860
1861static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1862{
1863        struct c4iw_ep *ep;
1864        struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1865        int release = 0;
1866        unsigned int tid = GET_TID(rpl);
1867
1868        ep = get_ep_from_tid(dev, tid);
1869        if (!ep) {
1870                pr_warn("Abort rpl to freed endpoint\n");
1871                return 0;
1872        }
1873        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1874        mutex_lock(&ep->com.mutex);
1875        switch (ep->com.state) {
1876        case ABORTING:
1877                c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1878                __state_set(&ep->com, DEAD);
1879                release = 1;
1880                break;
1881        default:
1882                pr_err("%s ep %p state %d\n", __func__, ep, ep->com.state);
1883                break;
1884        }
1885        mutex_unlock(&ep->com.mutex);
1886
1887        if (release)
1888                release_ep_resources(ep);
1889        c4iw_put_ep(&ep->com);
1890        return 0;
1891}
1892
1893static int send_fw_act_open_req(struct c4iw_ep *ep, unsigned int atid)
1894{
1895        struct sk_buff *skb;
1896        struct fw_ofld_connection_wr *req;
1897        unsigned int mtu_idx;
1898        u32 wscale;
1899        struct sockaddr_in *sin;
1900        int win;
1901
1902        skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1903        req = __skb_put_zero(skb, sizeof(*req));
1904        req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR));
1905        req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
1906        req->le.filter = cpu_to_be32(cxgb4_select_ntuple(
1907                                     ep->com.dev->rdev.lldi.ports[0],
1908                                     ep->l2t));
1909        sin = (struct sockaddr_in *)&ep->com.local_addr;
1910        req->le.lport = sin->sin_port;
1911        req->le.u.ipv4.lip = sin->sin_addr.s_addr;
1912        sin = (struct sockaddr_in *)&ep->com.remote_addr;
1913        req->le.pport = sin->sin_port;
1914        req->le.u.ipv4.pip = sin->sin_addr.s_addr;
1915        req->tcb.t_state_to_astid =
1916                        htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_SENT) |
1917                        FW_OFLD_CONNECTION_WR_ASTID_V(atid));
1918        req->tcb.cplrxdataack_cplpassacceptrpl =
1919                        htons(FW_OFLD_CONNECTION_WR_CPLRXDATAACK_F);
1920        req->tcb.tx_max = (__force __be32) jiffies;
1921        req->tcb.rcv_adv = htons(1);
1922        cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
1923                      enable_tcp_timestamps,
1924                      (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1925        wscale = cxgb_compute_wscale(rcv_win);
1926
1927        /*
1928         * Specify the largest window that will fit in opt0. The
1929         * remainder will be specified in the rx_data_ack.
1930         */
1931        win = ep->rcv_win >> 10;
1932        if (win > RCV_BUFSIZ_M)
1933                win = RCV_BUFSIZ_M;
1934
1935        req->tcb.opt0 = (__force __be64) (TCAM_BYPASS_F |
1936                (nocong ? NO_CONG_F : 0) |
1937                KEEP_ALIVE_F |
1938                DELACK_F |
1939                WND_SCALE_V(wscale) |
1940                MSS_IDX_V(mtu_idx) |
1941                L2T_IDX_V(ep->l2t->idx) |
1942                TX_CHAN_V(ep->tx_chan) |
1943                SMAC_SEL_V(ep->smac_idx) |
1944                DSCP_V(ep->tos >> 2) |
1945                ULP_MODE_V(ULP_MODE_TCPDDP) |
1946                RCV_BUFSIZ_V(win));
1947        req->tcb.opt2 = (__force __be32) (PACE_V(1) |
1948                TX_QUEUE_V(ep->com.dev->rdev.lldi.tx_modq[ep->tx_chan]) |
1949                RX_CHANNEL_V(0) |
1950                CCTRL_ECN_V(enable_ecn) |
1951                RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid));
1952        if (enable_tcp_timestamps)
1953                req->tcb.opt2 |= (__force __be32)TSTAMPS_EN_F;
1954        if (enable_tcp_sack)
1955                req->tcb.opt2 |= (__force __be32)SACK_EN_F;
1956        if (wscale && enable_tcp_window_scaling)
1957                req->tcb.opt2 |= (__force __be32)WND_SCALE_EN_F;
1958        req->tcb.opt0 = cpu_to_be64((__force u64)req->tcb.opt0);
1959        req->tcb.opt2 = cpu_to_be32((__force u32)req->tcb.opt2);
1960        set_wr_txq(skb, CPL_PRIORITY_CONTROL, ep->ctrlq_idx);
1961        set_bit(ACT_OFLD_CONN, &ep->com.history);
1962        return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1963}
1964
1965/*
1966 * Some of the error codes above implicitly indicate that there is no TID
1967 * allocated with the result of an ACT_OPEN.  We use this predicate to make
1968 * that explicit.
1969 */
1970static inline int act_open_has_tid(int status)
1971{
1972        return (status != CPL_ERR_TCAM_PARITY &&
1973                status != CPL_ERR_TCAM_MISS &&
1974                status != CPL_ERR_TCAM_FULL &&
1975                status != CPL_ERR_CONN_EXIST_SYNRECV &&
1976                status != CPL_ERR_CONN_EXIST);
1977}
1978
1979static char *neg_adv_str(unsigned int status)
1980{
1981        switch (status) {
1982        case CPL_ERR_RTX_NEG_ADVICE:
1983                return "Retransmit timeout";
1984        case CPL_ERR_PERSIST_NEG_ADVICE:
1985                return "Persist timeout";
1986        case CPL_ERR_KEEPALV_NEG_ADVICE:
1987                return "Keepalive timeout";
1988        default:
1989                return "Unknown";
1990        }
1991}
1992
1993static void set_tcp_window(struct c4iw_ep *ep, struct port_info *pi)
1994{
1995        ep->snd_win = snd_win;
1996        ep->rcv_win = rcv_win;
1997        pr_debug("%s snd_win %d rcv_win %d\n",
1998                 __func__, ep->snd_win, ep->rcv_win);
1999}
2000
2001#define ACT_OPEN_RETRY_COUNT 2
2002
2003static int import_ep(struct c4iw_ep *ep, int iptype, __u8 *peer_ip,
2004                     struct dst_entry *dst, struct c4iw_dev *cdev,
2005                     bool clear_mpa_v1, enum chip_type adapter_type, u8 tos)
2006{
2007        struct neighbour *n;
2008        int err, step;
2009        struct net_device *pdev;
2010
2011        n = dst_neigh_lookup(dst, peer_ip);
2012        if (!n)
2013                return -ENODEV;
2014
2015        rcu_read_lock();
2016        err = -ENOMEM;
2017        if (n->dev->flags & IFF_LOOPBACK) {
2018                if (iptype == 4)
2019                        pdev = ip_dev_find(&init_net, *(__be32 *)peer_ip);
2020                else if (IS_ENABLED(CONFIG_IPV6))
2021                        for_each_netdev(&init_net, pdev) {
2022                                if (ipv6_chk_addr(&init_net,
2023                                                  (struct in6_addr *)peer_ip,
2024                                                  pdev, 1))
2025                                        break;
2026                        }
2027                else
2028                        pdev = NULL;
2029
2030                if (!pdev) {
2031                        err = -ENODEV;
2032                        goto out;
2033                }
2034                ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
2035                                        n, pdev, rt_tos2priority(tos));
2036                if (!ep->l2t) {
2037                        dev_put(pdev);
2038                        goto out;
2039                }
2040                ep->mtu = pdev->mtu;
2041                ep->tx_chan = cxgb4_port_chan(pdev);
2042                ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
2043                                                cxgb4_port_viid(pdev));
2044                step = cdev->rdev.lldi.ntxq /
2045                        cdev->rdev.lldi.nchan;
2046                ep->txq_idx = cxgb4_port_idx(pdev) * step;
2047                step = cdev->rdev.lldi.nrxq /
2048                        cdev->rdev.lldi.nchan;
2049                ep->ctrlq_idx = cxgb4_port_idx(pdev);
2050                ep->rss_qid = cdev->rdev.lldi.rxq_ids[
2051                        cxgb4_port_idx(pdev) * step];
2052                set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2053                dev_put(pdev);
2054        } else {
2055                pdev = get_real_dev(n->dev);
2056                ep->l2t = cxgb4_l2t_get(cdev->rdev.lldi.l2t,
2057                                        n, pdev, 0);
2058                if (!ep->l2t)
2059                        goto out;
2060                ep->mtu = dst_mtu(dst);
2061                ep->tx_chan = cxgb4_port_chan(pdev);
2062                ep->smac_idx = cxgb4_tp_smt_idx(adapter_type,
2063                                                cxgb4_port_viid(pdev));
2064                step = cdev->rdev.lldi.ntxq /
2065                        cdev->rdev.lldi.nchan;
2066                ep->txq_idx = cxgb4_port_idx(pdev) * step;
2067                ep->ctrlq_idx = cxgb4_port_idx(pdev);
2068                step = cdev->rdev.lldi.nrxq /
2069                        cdev->rdev.lldi.nchan;
2070                ep->rss_qid = cdev->rdev.lldi.rxq_ids[
2071                        cxgb4_port_idx(pdev) * step];
2072                set_tcp_window(ep, (struct port_info *)netdev_priv(pdev));
2073
2074                if (clear_mpa_v1) {
2075                        ep->retry_with_mpa_v1 = 0;
2076                        ep->tried_with_mpa_v1 = 0;
2077                }
2078        }
2079        err = 0;
2080out:
2081        rcu_read_unlock();
2082
2083        neigh_release(n);
2084
2085        return err;
2086}
2087
2088static int c4iw_reconnect(struct c4iw_ep *ep)
2089{
2090        int err = 0;
2091        int size = 0;
2092        struct sockaddr_in *laddr = (struct sockaddr_in *)
2093                                    &ep->com.cm_id->m_local_addr;
2094        struct sockaddr_in *raddr = (struct sockaddr_in *)
2095                                    &ep->com.cm_id->m_remote_addr;
2096        struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)
2097                                      &ep->com.cm_id->m_local_addr;
2098        struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)
2099                                      &ep->com.cm_id->m_remote_addr;
2100        int iptype;
2101        __u8 *ra;
2102
2103        pr_debug("%s qp %p cm_id %p\n", __func__, ep->com.qp, ep->com.cm_id);
2104        init_timer(&ep->timer);
2105        c4iw_init_wr_wait(&ep->com.wr_wait);
2106
2107        /* When MPA revision is different on nodes, the node with MPA_rev=2
2108         * tries to reconnect with MPA_rev 1 for the same EP through
2109         * c4iw_reconnect(), where the same EP is assigned with new tid for
2110         * further connection establishment. As we are using the same EP pointer
2111         * for reconnect, few skbs are used during the previous c4iw_connect(),
2112         * which leaves the EP with inadequate skbs for further
2113         * c4iw_reconnect(), Further causing an assert BUG_ON() due to empty
2114         * skb_list() during peer_abort(). Allocate skbs which is already used.
2115         */
2116        size = (CN_MAX_CON_BUF - skb_queue_len(&ep->com.ep_skb_list));
2117        if (alloc_ep_skb_list(&ep->com.ep_skb_list, size)) {
2118                err = -ENOMEM;
2119                goto fail1;
2120        }
2121
2122        /*
2123         * Allocate an active TID to initiate a TCP connection.
2124         */
2125        ep->atid = cxgb4_alloc_atid(ep->com.dev->rdev.lldi.tids, ep);
2126        if (ep->atid == -1) {
2127                pr_err("%s - cannot alloc atid\n", __func__);
2128                err = -ENOMEM;
2129                goto fail2;
2130        }
2131        insert_handle(ep->com.dev, &ep->com.dev->atid_idr, ep, ep->atid);
2132
2133        /* find a route */
2134        if (ep->com.cm_id->m_local_addr.ss_family == AF_INET) {
2135                ep->dst = cxgb_find_route(&ep->com.dev->rdev.lldi, get_real_dev,
2136                                          laddr->sin_addr.s_addr,
2137                                          raddr->sin_addr.s_addr,
2138                                          laddr->sin_port,
2139                                          raddr->sin_port, ep->com.cm_id->tos);
2140                iptype = 4;
2141                ra = (__u8 *)&raddr->sin_addr;
2142        } else {
2143                ep->dst = cxgb_find_route6(&ep->com.dev->rdev.lldi,
2144                                           get_real_dev,
2145                                           laddr6->sin6_addr.s6_addr,
2146                                           raddr6->sin6_addr.s6_addr,
2147                                           laddr6->sin6_port,
2148                                           raddr6->sin6_port, 0,
2149                                           raddr6->sin6_scope_id);
2150                iptype = 6;
2151                ra = (__u8 *)&raddr6->sin6_addr;
2152        }
2153        if (!ep->dst) {
2154                pr_err("%s - cannot find route\n", __func__);
2155                err = -EHOSTUNREACH;
2156                goto fail3;
2157        }
2158        err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, false,
2159                        ep->com.dev->rdev.lldi.adapter_type,
2160                        ep->com.cm_id->tos);
2161        if (err) {
2162                pr_err("%s - cannot alloc l2e\n", __func__);
2163                goto fail4;
2164        }
2165
2166        pr_debug("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
2167                 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
2168                 ep->l2t->idx);
2169
2170        state_set(&ep->com, CONNECTING);
2171        ep->tos = ep->com.cm_id->tos;
2172
2173        /* send connect request to rnic */
2174        err = send_connect(ep);
2175        if (!err)
2176                goto out;
2177
2178        cxgb4_l2t_release(ep->l2t);
2179fail4:
2180        dst_release(ep->dst);
2181fail3:
2182        remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
2183        cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
2184fail2:
2185        /*
2186         * remember to send notification to upper layer.
2187         * We are in here so the upper layer is not aware that this is
2188         * re-connect attempt and so, upper layer is still waiting for
2189         * response of 1st connect request.
2190         */
2191        connect_reply_upcall(ep, -ECONNRESET);
2192fail1:
2193        c4iw_put_ep(&ep->com);
2194out:
2195        return err;
2196}
2197
2198static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2199{
2200        struct c4iw_ep *ep;
2201        struct cpl_act_open_rpl *rpl = cplhdr(skb);
2202        unsigned int atid = TID_TID_G(AOPEN_ATID_G(
2203                                      ntohl(rpl->atid_status)));
2204        struct tid_info *t = dev->rdev.lldi.tids;
2205        int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
2206        struct sockaddr_in *la;
2207        struct sockaddr_in *ra;
2208        struct sockaddr_in6 *la6;
2209        struct sockaddr_in6 *ra6;
2210        int ret = 0;
2211
2212        ep = lookup_atid(t, atid);
2213        la = (struct sockaddr_in *)&ep->com.local_addr;
2214        ra = (struct sockaddr_in *)&ep->com.remote_addr;
2215        la6 = (struct sockaddr_in6 *)&ep->com.local_addr;
2216        ra6 = (struct sockaddr_in6 *)&ep->com.remote_addr;
2217
2218        pr_debug("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
2219                 status, status2errno(status));
2220
2221        if (cxgb_is_neg_adv(status)) {
2222                pr_debug("%s Connection problems for atid %u status %u (%s)\n",
2223                         __func__, atid, status, neg_adv_str(status));
2224                ep->stats.connect_neg_adv++;
2225                mutex_lock(&dev->rdev.stats.lock);
2226                dev->rdev.stats.neg_adv++;
2227                mutex_unlock(&dev->rdev.stats.lock);
2228                return 0;
2229        }
2230
2231        set_bit(ACT_OPEN_RPL, &ep->com.history);
2232
2233        /*
2234         * Log interesting failures.
2235         */
2236        switch (status) {
2237        case CPL_ERR_CONN_RESET:
2238        case CPL_ERR_CONN_TIMEDOUT:
2239                break;
2240        case CPL_ERR_TCAM_FULL:
2241                mutex_lock(&dev->rdev.stats.lock);
2242                dev->rdev.stats.tcam_full++;
2243                mutex_unlock(&dev->rdev.stats.lock);
2244                if (ep->com.local_addr.ss_family == AF_INET &&
2245                    dev->rdev.lldi.enable_fw_ofld_conn) {
2246                        ret = send_fw_act_open_req(ep, TID_TID_G(AOPEN_ATID_G(
2247                                                   ntohl(rpl->atid_status))));
2248                        if (ret)
2249                                goto fail;
2250                        return 0;
2251                }
2252                break;
2253        case CPL_ERR_CONN_EXIST:
2254                if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
2255                        set_bit(ACT_RETRY_INUSE, &ep->com.history);
2256                        if (ep->com.remote_addr.ss_family == AF_INET6) {
2257                                struct sockaddr_in6 *sin6 =
2258                                                (struct sockaddr_in6 *)
2259                                                &ep->com.local_addr;
2260                                cxgb4_clip_release(
2261                                                ep->com.dev->rdev.lldi.ports[0],
2262                                                (const u32 *)
2263                                                &sin6->sin6_addr.s6_addr, 1);
2264                        }
2265                        remove_handle(ep->com.dev, &ep->com.dev->atid_idr,
2266                                        atid);
2267                        cxgb4_free_atid(t, atid);
2268                        dst_release(ep->dst);
2269                        cxgb4_l2t_release(ep->l2t);
2270                        c4iw_reconnect(ep);
2271                        return 0;
2272                }
2273                break;
2274        default:
2275                if (ep->com.local_addr.ss_family == AF_INET) {
2276                        pr_info("Active open failure - atid %u status %u errno %d %pI4:%u->%pI4:%u\n",
2277                                atid, status, status2errno(status),
2278                                &la->sin_addr.s_addr, ntohs(la->sin_port),
2279                                &ra->sin_addr.s_addr, ntohs(ra->sin_port));
2280                } else {
2281                        pr_info("Active open failure - atid %u status %u errno %d %pI6:%u->%pI6:%u\n",
2282                                atid, status, status2errno(status),
2283                                la6->sin6_addr.s6_addr, ntohs(la6->sin6_port),
2284                                ra6->sin6_addr.s6_addr, ntohs(ra6->sin6_port));
2285                }
2286                break;
2287        }
2288
2289fail:
2290        connect_reply_upcall(ep, status2errno(status));
2291        state_set(&ep->com, DEAD);
2292
2293        if (ep->com.remote_addr.ss_family == AF_INET6) {
2294                struct sockaddr_in6 *sin6 =
2295                        (struct sockaddr_in6 *)&ep->com.local_addr;
2296                cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
2297                                   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
2298        }
2299        if (status && act_open_has_tid(status))
2300                cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl),
2301                                 ep->com.local_addr.ss_family);
2302
2303        remove_handle(ep->com.dev, &ep->com.dev->atid_idr, atid);
2304        cxgb4_free_atid(t, atid);
2305        dst_release(ep->dst);
2306        cxgb4_l2t_release(ep->l2t);
2307        c4iw_put_ep(&ep->com);
2308
2309        return 0;
2310}
2311
2312static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2313{
2314        struct cpl_pass_open_rpl *rpl = cplhdr(skb);
2315        unsigned int stid = GET_TID(rpl);
2316        struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2317
2318        if (!ep) {
2319                pr_debug("%s stid %d lookup failure!\n", __func__, stid);
2320                goto out;
2321        }
2322        pr_debug("%s ep %p status %d error %d\n", __func__, ep,
2323                 rpl->status, status2errno(rpl->status));
2324        c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2325        c4iw_put_ep(&ep->com);
2326out:
2327        return 0;
2328}
2329
2330static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2331{
2332        struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
2333        unsigned int stid = GET_TID(rpl);
2334        struct c4iw_listen_ep *ep = get_ep_from_stid(dev, stid);
2335
2336        if (!ep) {
2337                pr_debug("%s stid %d lookup failure!\n", __func__, stid);
2338                goto out;
2339        }
2340        pr_debug("%s ep %p\n", __func__, ep);
2341        c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
2342        c4iw_put_ep(&ep->com);
2343out:
2344        return 0;
2345}
2346
2347static int accept_cr(struct c4iw_ep *ep, struct sk_buff *skb,
2348                     struct cpl_pass_accept_req *req)
2349{
2350        struct cpl_pass_accept_rpl *rpl;
2351        unsigned int mtu_idx;
2352        u64 opt0;
2353        u32 opt2;
2354        u32 wscale;
2355        struct cpl_t5_pass_accept_rpl *rpl5 = NULL;
2356        int win;
2357        enum chip_type adapter_type = ep->com.dev->rdev.lldi.adapter_type;
2358
2359        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2360        BUG_ON(skb_cloned(skb));
2361
2362        skb_get(skb);
2363        rpl = cplhdr(skb);
2364        if (!is_t4(adapter_type)) {
2365                skb_trim(skb, roundup(sizeof(*rpl5), 16));
2366                rpl5 = (void *)rpl;
2367                INIT_TP_WR(rpl5, ep->hwtid);
2368        } else {
2369                skb_trim(skb, sizeof(*rpl));
2370                INIT_TP_WR(rpl, ep->hwtid);
2371        }
2372        OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
2373                                                    ep->hwtid));
2374
2375        cxgb_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx,
2376                      enable_tcp_timestamps && req->tcpopt.tstamp,
2377                      (ep->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
2378        wscale = cxgb_compute_wscale(rcv_win);
2379
2380        /*
2381         * Specify the largest window that will fit in opt0. The
2382         * remainder will be specified in the rx_data_ack.
2383         */
2384        win = ep->rcv_win >> 10;
2385        if (win > RCV_BUFSIZ_M)
2386                win = RCV_BUFSIZ_M;
2387        opt0 = (nocong ? NO_CONG_F : 0) |
2388               KEEP_ALIVE_F |
2389               DELACK_F |
2390               WND_SCALE_V(wscale) |
2391               MSS_IDX_V(mtu_idx) |
2392               L2T_IDX_V(ep->l2t->idx) |
2393               TX_CHAN_V(ep->tx_chan) |
2394               SMAC_SEL_V(ep->smac_idx) |
2395               DSCP_V(ep->tos >> 2) |
2396               ULP_MODE_V(ULP_MODE_TCPDDP) |
2397               RCV_BUFSIZ_V(win);
2398        opt2 = RX_CHANNEL_V(0) |
2399               RSS_QUEUE_VALID_F | RSS_QUEUE_V(ep->rss_qid);
2400
2401        if (enable_tcp_timestamps && req->tcpopt.tstamp)
2402                opt2 |= TSTAMPS_EN_F;
2403        if (enable_tcp_sack && req->tcpopt.sack)
2404                opt2 |= SACK_EN_F;
2405        if (wscale && enable_tcp_window_scaling)
2406                opt2 |= WND_SCALE_EN_F;
2407        if (enable_ecn) {
2408                const struct tcphdr *tcph;
2409                u32 hlen = ntohl(req->hdr_len);
2410
2411                if (CHELSIO_CHIP_VERSION(adapter_type) <= CHELSIO_T5)
2412                        tcph = (const void *)(req + 1) + ETH_HDR_LEN_G(hlen) +
2413                                IP_HDR_LEN_G(hlen);
2414                else
2415                        tcph = (const void *)(req + 1) +
2416                                T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen);
2417                if (tcph->ece && tcph->cwr)
2418                        opt2 |= CCTRL_ECN_V(1);
2419        }
2420        if (CHELSIO_CHIP_VERSION(adapter_type) > CHELSIO_T4) {
2421                u32 isn = (prandom_u32() & ~7UL) - 1;
2422                opt2 |= T5_OPT_2_VALID_F;
2423                opt2 |= CONG_CNTRL_V(CONG_ALG_TAHOE);
2424                opt2 |= T5_ISS_F;
2425                rpl5 = (void *)rpl;
2426                memset(&rpl5->iss, 0, roundup(sizeof(*rpl5)-sizeof(*rpl), 16));
2427                if (peer2peer)
2428                        isn += 4;
2429                rpl5->iss = cpu_to_be32(isn);
2430                pr_debug("%s iss %u\n", __func__, be32_to_cpu(rpl5->iss));
2431        }
2432
2433        rpl->opt0 = cpu_to_be64(opt0);
2434        rpl->opt2 = cpu_to_be32(opt2);
2435        set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
2436        t4_set_arp_err_handler(skb, ep, pass_accept_rpl_arp_failure);
2437
2438        return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
2439}
2440
2441static void reject_cr(struct c4iw_dev *dev, u32 hwtid, struct sk_buff *skb)
2442{
2443        pr_debug("%s c4iw_dev %p tid %u\n", __func__, dev, hwtid);
2444        BUG_ON(skb_cloned(skb));
2445        skb_trim(skb, sizeof(struct cpl_tid_release));
2446        release_tid(&dev->rdev, hwtid, skb);
2447        return;
2448}
2449
2450static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
2451{
2452        struct c4iw_ep *child_ep = NULL, *parent_ep;
2453        struct cpl_pass_accept_req *req = cplhdr(skb);
2454        unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
2455        struct tid_info *t = dev->rdev.lldi.tids;
2456        unsigned int hwtid = GET_TID(req);
2457        struct dst_entry *dst;
2458        __u8 local_ip[16], peer_ip[16];
2459        __be16 local_port, peer_port;
2460        struct sockaddr_in6 *sin6;
2461        int err;
2462        u16 peer_mss = ntohs(req->tcpopt.mss);
2463        int iptype;
2464        unsigned short hdrs;
2465        u8 tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
2466
2467        parent_ep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
2468        if (!parent_ep) {
2469                pr_debug("%s connect request on invalid stid %d\n",
2470                         __func__, stid);
2471                goto reject;
2472        }
2473
2474        if (state_read(&parent_ep->com) != LISTEN) {
2475                pr_debug("%s - listening ep not in LISTEN\n", __func__);
2476                goto reject;
2477        }
2478
2479        cxgb_get_4tuple(req, parent_ep->com.dev->rdev.lldi.adapter_type,
2480                        &iptype, local_ip, peer_ip, &local_port, &peer_port);
2481
2482        /* Find output route */
2483        if (iptype == 4)  {
2484                pr_debug("%s parent ep %p hwtid %u laddr %pI4 raddr %pI4 lport %d rport %d peer_mss %d\n"
2485                         , __func__, parent_ep, hwtid,
2486                         local_ip, peer_ip, ntohs(local_port),
2487                         ntohs(peer_port), peer_mss);
2488                dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
2489                                      *(__be32 *)local_ip, *(__be32 *)peer_ip,
2490                                      local_port, peer_port, tos);
2491        } else {
2492                pr_debug("%s parent ep %p hwtid %u laddr %pI6 raddr %pI6 lport %d rport %d peer_mss %d\n"
2493                         , __func__, parent_ep, hwtid,
2494                         local_ip, peer_ip, ntohs(local_port),
2495                         ntohs(peer_port), peer_mss);
2496                dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
2497                                local_ip, peer_ip, local_port, peer_port,
2498                                PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
2499                                ((struct sockaddr_in6 *)
2500                                 &parent_ep->com.local_addr)->sin6_scope_id);
2501        }
2502        if (!dst) {
2503                pr_err("%s - failed to find dst entry!\n", __func__);
2504                goto reject;
2505        }
2506
2507        child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
2508        if (!child_ep) {
2509                pr_err("%s - failed to allocate ep entry!\n", __func__);
2510                dst_release(dst);
2511                goto reject;
2512        }
2513
2514        err = import_ep(child_ep, iptype, peer_ip, dst, dev, false,
2515                        parent_ep->com.dev->rdev.lldi.adapter_type, tos);
2516        if (err) {
2517                pr_err("%s - failed to allocate l2t entry!\n", __func__);
2518                dst_release(dst);
2519                kfree(child_ep);
2520                goto reject;
2521        }
2522
2523        hdrs = ((iptype == 4) ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
2524               sizeof(struct tcphdr) +
2525               ((enable_tcp_timestamps && req->tcpopt.tstamp) ? 12 : 0);
2526        if (peer_mss && child_ep->mtu > (peer_mss + hdrs))
2527                child_ep->mtu = peer_mss + hdrs;
2528
2529        skb_queue_head_init(&child_ep->com.ep_skb_list);
2530        if (alloc_ep_skb_list(&child_ep->com.ep_skb_list, CN_MAX_CON_BUF))
2531                goto fail;
2532
2533        state_set(&child_ep->com, CONNECTING);
2534        child_ep->com.dev = dev;
2535        child_ep->com.cm_id = NULL;
2536
2537        if (iptype == 4) {
2538                struct sockaddr_in *sin = (struct sockaddr_in *)
2539                        &child_ep->com.local_addr;
2540
2541                sin->sin_family = AF_INET;
2542                sin->sin_port = local_port;
2543                sin->sin_addr.s_addr = *(__be32 *)local_ip;
2544
2545                sin = (struct sockaddr_in *)&child_ep->com.local_addr;
2546                sin->sin_family = AF_INET;
2547                sin->sin_port = ((struct sockaddr_in *)
2548                                 &parent_ep->com.local_addr)->sin_port;
2549                sin->sin_addr.s_addr = *(__be32 *)local_ip;
2550
2551                sin = (struct sockaddr_in *)&child_ep->com.remote_addr;
2552                sin->sin_family = AF_INET;
2553                sin->sin_port = peer_port;
2554                sin->sin_addr.s_addr = *(__be32 *)peer_ip;
2555        } else {
2556                sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2557                sin6->sin6_family = PF_INET6;
2558                sin6->sin6_port = local_port;
2559                memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2560
2561                sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2562                sin6->sin6_family = PF_INET6;
2563                sin6->sin6_port = ((struct sockaddr_in6 *)
2564                                   &parent_ep->com.local_addr)->sin6_port;
2565                memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
2566
2567                sin6 = (struct sockaddr_in6 *)&child_ep->com.remote_addr;
2568                sin6->sin6_family = PF_INET6;
2569                sin6->sin6_port = peer_port;
2570                memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
2571        }
2572
2573        c4iw_get_ep(&parent_ep->com);
2574        child_ep->parent_ep = parent_ep;
2575        child_ep->tos = tos;
2576        child_ep->dst = dst;
2577        child_ep->hwtid = hwtid;
2578
2579        pr_debug("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
2580                 child_ep->tx_chan, child_ep->smac_idx, child_ep->rss_qid);
2581
2582        init_timer(&child_ep->timer);
2583        cxgb4_insert_tid(t, child_ep, hwtid,
2584                         child_ep->com.local_addr.ss_family);
2585        insert_ep_tid(child_ep);
2586        if (accept_cr(child_ep, skb, req)) {
2587                c4iw_put_ep(&parent_ep->com);
2588                release_ep_resources(child_ep);
2589        } else {
2590                set_bit(PASS_ACCEPT_REQ, &child_ep->com.history);
2591        }
2592        if (iptype == 6) {
2593                sin6 = (struct sockaddr_in6 *)&child_ep->com.local_addr;
2594                cxgb4_clip_get(child_ep->com.dev->rdev.lldi.ports[0],
2595                               (const u32 *)&sin6->sin6_addr.s6_addr, 1);
2596        }
2597        goto out;
2598fail:
2599        c4iw_put_ep(&child_ep->com);
2600reject:
2601        reject_cr(dev, hwtid, skb);
2602out:
2603        if (parent_ep)
2604                c4iw_put_ep(&parent_ep->com);
2605        return 0;
2606}
2607
2608static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
2609{
2610        struct c4iw_ep *ep;
2611        struct cpl_pass_establish *req = cplhdr(skb);
2612        unsigned int tid = GET_TID(req);
2613        int ret;
2614
2615        ep = get_ep_from_tid(dev, tid);
2616        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2617        ep->snd_seq = be32_to_cpu(req->snd_isn);
2618        ep->rcv_seq = be32_to_cpu(req->rcv_isn);
2619
2620        pr_debug("%s ep %p hwtid %u tcp_opt 0x%02x\n", __func__, ep, tid,
2621                 ntohs(req->tcp_opt));
2622
2623        set_emss(ep, ntohs(req->tcp_opt));
2624
2625        dst_confirm(ep->dst);
2626        mutex_lock(&ep->com.mutex);
2627        ep->com.state = MPA_REQ_WAIT;
2628        start_ep_timer(ep);
2629        set_bit(PASS_ESTAB, &ep->com.history);
2630        ret = send_flowc(ep);
2631        mutex_unlock(&ep->com.mutex);
2632        if (ret)
2633                c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2634        c4iw_put_ep(&ep->com);
2635
2636        return 0;
2637}
2638
2639static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
2640{
2641        struct cpl_peer_close *hdr = cplhdr(skb);
2642        struct c4iw_ep *ep;
2643        struct c4iw_qp_attributes attrs;
2644        int disconnect = 1;
2645        int release = 0;
2646        unsigned int tid = GET_TID(hdr);
2647        int ret;
2648
2649        ep = get_ep_from_tid(dev, tid);
2650        if (!ep)
2651                return 0;
2652
2653        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2654        dst_confirm(ep->dst);
2655
2656        set_bit(PEER_CLOSE, &ep->com.history);
2657        mutex_lock(&ep->com.mutex);
2658        switch (ep->com.state) {
2659        case MPA_REQ_WAIT:
2660                __state_set(&ep->com, CLOSING);
2661                break;
2662        case MPA_REQ_SENT:
2663                __state_set(&ep->com, CLOSING);
2664                connect_reply_upcall(ep, -ECONNRESET);
2665                break;
2666        case MPA_REQ_RCVD:
2667
2668                /*
2669                 * We're gonna mark this puppy DEAD, but keep
2670                 * the reference on it until the ULP accepts or
2671                 * rejects the CR. Also wake up anyone waiting
2672                 * in rdma connection migration (see c4iw_accept_cr()).
2673                 */
2674                __state_set(&ep->com, CLOSING);
2675                pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2676                c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2677                break;
2678        case MPA_REP_SENT:
2679                __state_set(&ep->com, CLOSING);
2680                pr_debug("waking up ep %p tid %u\n", ep, ep->hwtid);
2681                c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2682                break;
2683        case FPDU_MODE:
2684                start_ep_timer(ep);
2685                __state_set(&ep->com, CLOSING);
2686                attrs.next_state = C4IW_QP_STATE_CLOSING;
2687                ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2688                                       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2689                if (ret != -ECONNRESET) {
2690                        peer_close_upcall(ep);
2691                        disconnect = 1;
2692                }
2693                break;
2694        case ABORTING:
2695                disconnect = 0;
2696                break;
2697        case CLOSING:
2698                __state_set(&ep->com, MORIBUND);
2699                disconnect = 0;
2700                break;
2701        case MORIBUND:
2702                (void)stop_ep_timer(ep);
2703                if (ep->com.cm_id && ep->com.qp) {
2704                        attrs.next_state = C4IW_QP_STATE_IDLE;
2705                        c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2706                                       C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2707                }
2708                close_complete_upcall(ep, 0);
2709                __state_set(&ep->com, DEAD);
2710                release = 1;
2711                disconnect = 0;
2712                break;
2713        case DEAD:
2714                disconnect = 0;
2715                break;
2716        default:
2717                BUG_ON(1);
2718        }
2719        mutex_unlock(&ep->com.mutex);
2720        if (disconnect)
2721                c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2722        if (release)
2723                release_ep_resources(ep);
2724        c4iw_put_ep(&ep->com);
2725        return 0;
2726}
2727
2728static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
2729{
2730        struct cpl_abort_req_rss *req = cplhdr(skb);
2731        struct c4iw_ep *ep;
2732        struct sk_buff *rpl_skb;
2733        struct c4iw_qp_attributes attrs;
2734        int ret;
2735        int release = 0;
2736        unsigned int tid = GET_TID(req);
2737        u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
2738
2739        ep = get_ep_from_tid(dev, tid);
2740        if (!ep)
2741                return 0;
2742
2743        if (cxgb_is_neg_adv(req->status)) {
2744                pr_debug("%s Negative advice on abort- tid %u status %d (%s)\n",
2745                         __func__, ep->hwtid, req->status,
2746                         neg_adv_str(req->status));
2747                ep->stats.abort_neg_adv++;
2748                mutex_lock(&dev->rdev.stats.lock);
2749                dev->rdev.stats.neg_adv++;
2750                mutex_unlock(&dev->rdev.stats.lock);
2751                goto deref_ep;
2752        }
2753        pr_debug("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
2754                 ep->com.state);
2755        set_bit(PEER_ABORT, &ep->com.history);
2756
2757        /*
2758         * Wake up any threads in rdma_init() or rdma_fini().
2759         * However, this is not needed if com state is just
2760         * MPA_REQ_SENT
2761         */
2762        if (ep->com.state != MPA_REQ_SENT)
2763                c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2764
2765        mutex_lock(&ep->com.mutex);
2766        switch (ep->com.state) {
2767        case CONNECTING:
2768                c4iw_put_ep(&ep->parent_ep->com);
2769                break;
2770        case MPA_REQ_WAIT:
2771                (void)stop_ep_timer(ep);
2772                break;
2773        case MPA_REQ_SENT:
2774                (void)stop_ep_timer(ep);
2775                if (mpa_rev == 1 || (mpa_rev == 2 && ep->tried_with_mpa_v1))
2776                        connect_reply_upcall(ep, -ECONNRESET);
2777                else {
2778                        /*
2779                         * we just don't send notification upwards because we
2780                         * want to retry with mpa_v1 without upper layers even
2781                         * knowing it.
2782                         *
2783                         * do some housekeeping so as to re-initiate the
2784                         * connection
2785                         */
2786                        pr_debug("%s: mpa_rev=%d. Retrying with mpav1\n",
2787                                 __func__, mpa_rev);
2788                        ep->retry_with_mpa_v1 = 1;
2789                }
2790                break;
2791        case MPA_REP_SENT:
2792                break;
2793        case MPA_REQ_RCVD:
2794                break;
2795        case MORIBUND:
2796        case CLOSING:
2797                stop_ep_timer(ep);
2798                /*FALLTHROUGH*/
2799        case FPDU_MODE:
2800                if (ep->com.cm_id && ep->com.qp) {
2801                        attrs.next_state = C4IW_QP_STATE_ERROR;
2802                        ret = c4iw_modify_qp(ep->com.qp->rhp,
2803                                     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
2804                                     &attrs, 1);
2805                        if (ret)
2806                                pr_err("%s - qp <- error failed!\n", __func__);
2807                }
2808                peer_abort_upcall(ep);
2809                break;
2810        case ABORTING:
2811                break;
2812        case DEAD:
2813                pr_debug("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
2814                mutex_unlock(&ep->com.mutex);
2815                goto deref_ep;
2816        default:
2817                BUG_ON(1);
2818                break;
2819        }
2820        dst_confirm(ep->dst);
2821        if (ep->com.state != ABORTING) {
2822                __state_set(&ep->com, DEAD);
2823                /* we don't release if we want to retry with mpa_v1 */
2824                if (!ep->retry_with_mpa_v1)
2825                        release = 1;
2826        }
2827        mutex_unlock(&ep->com.mutex);
2828
2829        rpl_skb = skb_dequeue(&ep->com.ep_skb_list);
2830        if (WARN_ON(!rpl_skb)) {
2831                release = 1;
2832                goto out;
2833        }
2834
2835        cxgb_mk_abort_rpl(rpl_skb, len, ep->hwtid, ep->txq_idx);
2836
2837        c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
2838out:
2839        if (release)
2840                release_ep_resources(ep);
2841        else if (ep->retry_with_mpa_v1) {
2842                if (ep->com.remote_addr.ss_family == AF_INET6) {
2843                        struct sockaddr_in6 *sin6 =
2844                                        (struct sockaddr_in6 *)
2845                                        &ep->com.local_addr;
2846                        cxgb4_clip_release(
2847                                        ep->com.dev->rdev.lldi.ports[0],
2848                                        (const u32 *)&sin6->sin6_addr.s6_addr,
2849                                        1);
2850                }
2851                remove_handle(ep->com.dev, &ep->com.dev->hwtid_idr, ep->hwtid);
2852                cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid,
2853                                 ep->com.local_addr.ss_family);
2854                dst_release(ep->dst);
2855                cxgb4_l2t_release(ep->l2t);
2856                c4iw_reconnect(ep);
2857        }
2858
2859deref_ep:
2860        c4iw_put_ep(&ep->com);
2861        /* Dereferencing ep, referenced in peer_abort_intr() */
2862        c4iw_put_ep(&ep->com);
2863        return 0;
2864}
2865
2866static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2867{
2868        struct c4iw_ep *ep;
2869        struct c4iw_qp_attributes attrs;
2870        struct cpl_close_con_rpl *rpl = cplhdr(skb);
2871        int release = 0;
2872        unsigned int tid = GET_TID(rpl);
2873
2874        ep = get_ep_from_tid(dev, tid);
2875        if (!ep)
2876                return 0;
2877
2878        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2879
2880        /* The cm_id may be null if we failed to connect */
2881        mutex_lock(&ep->com.mutex);
2882        set_bit(CLOSE_CON_RPL, &ep->com.history);
2883        switch (ep->com.state) {
2884        case CLOSING:
2885                __state_set(&ep->com, MORIBUND);
2886                break;
2887        case MORIBUND:
2888                (void)stop_ep_timer(ep);
2889                if ((ep->com.cm_id) && (ep->com.qp)) {
2890                        attrs.next_state = C4IW_QP_STATE_IDLE;
2891                        c4iw_modify_qp(ep->com.qp->rhp,
2892                                             ep->com.qp,
2893                                             C4IW_QP_ATTR_NEXT_STATE,
2894                                             &attrs, 1);
2895                }
2896                close_complete_upcall(ep, 0);
2897                __state_set(&ep->com, DEAD);
2898                release = 1;
2899                break;
2900        case ABORTING:
2901        case DEAD:
2902                break;
2903        default:
2904                BUG_ON(1);
2905                break;
2906        }
2907        mutex_unlock(&ep->com.mutex);
2908        if (release)
2909                release_ep_resources(ep);
2910        c4iw_put_ep(&ep->com);
2911        return 0;
2912}
2913
2914static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
2915{
2916        struct cpl_rdma_terminate *rpl = cplhdr(skb);
2917        unsigned int tid = GET_TID(rpl);
2918        struct c4iw_ep *ep;
2919        struct c4iw_qp_attributes attrs;
2920
2921        ep = get_ep_from_tid(dev, tid);
2922        BUG_ON(!ep);
2923
2924        if (ep && ep->com.qp) {
2925                pr_warn("TERM received tid %u qpid %u\n",
2926                        tid, ep->com.qp->wq.sq.qid);
2927                attrs.next_state = C4IW_QP_STATE_TERMINATE;
2928                c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2929                               C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2930        } else
2931                pr_warn("TERM received tid %u no ep/qp\n", tid);
2932        c4iw_put_ep(&ep->com);
2933
2934        return 0;
2935}
2936
2937/*
2938 * Upcall from the adapter indicating data has been transmitted.
2939 * For us its just the single MPA request or reply.  We can now free
2940 * the skb holding the mpa message.
2941 */
2942static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
2943{
2944        struct c4iw_ep *ep;
2945        struct cpl_fw4_ack *hdr = cplhdr(skb);
2946        u8 credits = hdr->credits;
2947        unsigned int tid = GET_TID(hdr);
2948
2949
2950        ep = get_ep_from_tid(dev, tid);
2951        if (!ep)
2952                return 0;
2953        pr_debug("%s ep %p tid %u credits %u\n",
2954                 __func__, ep, ep->hwtid, credits);
2955        if (credits == 0) {
2956                pr_debug("%s 0 credit ack ep %p tid %u state %u\n",
2957                         __func__, ep, ep->hwtid, state_read(&ep->com));
2958                goto out;
2959        }
2960
2961        dst_confirm(ep->dst);
2962        if (ep->mpa_skb) {
2963                pr_debug("%s last streaming msg ack ep %p tid %u state %u initiator %u freeing skb\n",
2964                         __func__, ep, ep->hwtid,
2965                         state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
2966                mutex_lock(&ep->com.mutex);
2967                kfree_skb(ep->mpa_skb);
2968                ep->mpa_skb = NULL;
2969                if (test_bit(STOP_MPA_TIMER, &ep->com.flags))
2970                        stop_ep_timer(ep);
2971                mutex_unlock(&ep->com.mutex);
2972        }
2973out:
2974        c4iw_put_ep(&ep->com);
2975        return 0;
2976}
2977
2978int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2979{
2980        int abort;
2981        struct c4iw_ep *ep = to_ep(cm_id);
2982
2983        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2984
2985        mutex_lock(&ep->com.mutex);
2986        if (ep->com.state != MPA_REQ_RCVD) {
2987                mutex_unlock(&ep->com.mutex);
2988                c4iw_put_ep(&ep->com);
2989                return -ECONNRESET;
2990        }
2991        set_bit(ULP_REJECT, &ep->com.history);
2992        if (mpa_rev == 0)
2993                abort = 1;
2994        else
2995                abort = send_mpa_reject(ep, pdata, pdata_len);
2996        mutex_unlock(&ep->com.mutex);
2997
2998        stop_ep_timer(ep);
2999        c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
3000        c4iw_put_ep(&ep->com);
3001        return 0;
3002}
3003
3004int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3005{
3006        int err;
3007        struct c4iw_qp_attributes attrs;
3008        enum c4iw_qp_attr_mask mask;
3009        struct c4iw_ep *ep = to_ep(cm_id);
3010        struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
3011        struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
3012        int abort = 0;
3013
3014        pr_debug("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
3015
3016        mutex_lock(&ep->com.mutex);
3017        if (ep->com.state != MPA_REQ_RCVD) {
3018                err = -ECONNRESET;
3019                goto err_out;
3020        }
3021
3022        BUG_ON(!qp);
3023
3024        set_bit(ULP_ACCEPT, &ep->com.history);
3025        if ((conn_param->ord > cur_max_read_depth(ep->com.dev)) ||
3026            (conn_param->ird > cur_max_read_depth(ep->com.dev))) {
3027                err = -EINVAL;
3028                goto err_abort;
3029        }
3030
3031        if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
3032                if (conn_param->ord > ep->ird) {
3033                        if (RELAXED_IRD_NEGOTIATION) {
3034                                conn_param->ord = ep->ird;
3035                        } else {
3036                                ep->ird = conn_param->ird;
3037                                ep->ord = conn_param->ord;
3038                                send_mpa_reject(ep, conn_param->private_data,
3039                                                conn_param->private_data_len);
3040                                err = -ENOMEM;
3041                                goto err_abort;
3042                        }
3043                }
3044                if (conn_param->ird < ep->ord) {
3045                        if (RELAXED_IRD_NEGOTIATION &&
3046                            ep->ord <= h->rdev.lldi.max_ordird_qp) {
3047                                conn_param->ird = ep->ord;
3048                        } else {
3049                                err = -ENOMEM;
3050                                goto err_abort;
3051                        }
3052                }
3053        }
3054        ep->ird = conn_param->ird;
3055        ep->ord = conn_param->ord;
3056
3057        if (ep->mpa_attr.version == 1) {
3058                if (peer2peer && ep->ird == 0)
3059                        ep->ird = 1;
3060        } else {
3061                if (peer2peer &&
3062                    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
3063                    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
3064                        ep->ird = 1;
3065        }
3066
3067        pr_debug("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
3068
3069        ep->com.cm_id = cm_id;
3070        ref_cm_id(&ep->com);
3071        ep->com.qp = qp;
3072        ref_qp(ep);
3073
3074        /* bind QP to EP and move to RTS */
3075        attrs.mpa_attr = ep->mpa_attr;
3076        attrs.max_ird = ep->ird;
3077        attrs.max_ord = ep->ord;
3078        attrs.llp_stream_handle = ep;
3079        attrs.next_state = C4IW_QP_STATE_RTS;
3080
3081        /* bind QP and TID with INIT_WR */
3082        mask = C4IW_QP_ATTR_NEXT_STATE |
3083                             C4IW_QP_ATTR_LLP_STREAM_HANDLE |
3084                             C4IW_QP_ATTR_MPA_ATTR |
3085                             C4IW_QP_ATTR_MAX_IRD |
3086                             C4IW_QP_ATTR_MAX_ORD;
3087
3088        err = c4iw_modify_qp(ep->com.qp->rhp,
3089                             ep->com.qp, mask, &attrs, 1);
3090        if (err)
3091                goto err_deref_cm_id;
3092
3093        set_bit(STOP_MPA_TIMER, &ep->com.flags);
3094        err = send_mpa_reply(ep, conn_param->private_data,
3095                             conn_param->private_data_len);
3096        if (err)
3097                goto err_deref_cm_id;
3098
3099        __state_set(&ep->com, FPDU_MODE);
3100        established_upcall(ep);
3101        mutex_unlock(&ep->com.mutex);
3102        c4iw_put_ep(&ep->com);
3103        return 0;
3104err_deref_cm_id:
3105        deref_cm_id(&ep->com);
3106err_abort:
3107        abort = 1;
3108err_out:
3109        mutex_unlock(&ep->com.mutex);
3110        if (abort)
3111                c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
3112        c4iw_put_ep(&ep->com);
3113        return err;
3114}
3115
3116static int pick_local_ipaddrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
3117{
3118        struct in_device *ind;
3119        int found = 0;
3120        struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3121        struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3122
3123        ind = in_dev_get(dev->rdev.lldi.ports[0]);
3124        if (!ind)
3125                return -EADDRNOTAVAIL;
3126        for_primary_ifa(ind) {
3127                laddr->sin_addr.s_addr = ifa->ifa_address;
3128                raddr->sin_addr.s_addr = ifa->ifa_address;
3129                found = 1;
3130                break;
3131        }
3132        endfor_ifa(ind);
3133        in_dev_put(ind);
3134        return found ? 0 : -EADDRNOTAVAIL;
3135}
3136
3137static int get_lladdr(struct net_device *dev, struct in6_addr *addr,
3138                      unsigned char banned_flags)
3139{
3140        struct inet6_dev *idev;
3141        int err = -EADDRNOTAVAIL;
3142
3143        rcu_read_lock();
3144        idev = __in6_dev_get(dev);
3145        if (idev != NULL) {
3146                struct inet6_ifaddr *ifp;
3147
3148                read_lock_bh(&idev->lock);
3149                list_for_each_entry(ifp, &idev->addr_list, if_list) {
3150                        if (ifp->scope == IFA_LINK &&
3151                            !(ifp->flags & banned_flags)) {
3152                                memcpy(addr, &ifp->addr, 16);
3153                                err = 0;
3154                                break;
3155                        }
3156                }
3157                read_unlock_bh(&idev->lock);
3158        }
3159        rcu_read_unlock();
3160        return err;
3161}
3162
3163static int pick_local_ip6addrs(struct c4iw_dev *dev, struct iw_cm_id *cm_id)
3164{
3165        struct in6_addr uninitialized_var(addr);
3166        struct sockaddr_in6 *la6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
3167        struct sockaddr_in6 *ra6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3168
3169        if (!get_lladdr(dev->rdev.lldi.ports[0], &addr, IFA_F_TENTATIVE)) {
3170                memcpy(la6->sin6_addr.s6_addr, &addr, 16);
3171                memcpy(ra6->sin6_addr.s6_addr, &addr, 16);
3172                return 0;
3173        }
3174        return -EADDRNOTAVAIL;
3175}
3176
3177int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3178{
3179        struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
3180        struct c4iw_ep *ep;
3181        int err = 0;
3182        struct sockaddr_in *laddr;
3183        struct sockaddr_in *raddr;
3184        struct sockaddr_in6 *laddr6;
3185        struct sockaddr_in6 *raddr6;
3186        __u8 *ra;
3187        int iptype;
3188
3189        if ((conn_param->ord > cur_max_read_depth(dev)) ||
3190            (conn_param->ird > cur_max_read_depth(dev))) {
3191                err = -EINVAL;
3192                goto out;
3193        }
3194        ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
3195        if (!ep) {
3196                pr_err("%s - cannot alloc ep\n", __func__);
3197                err = -ENOMEM;
3198                goto out;
3199        }
3200
3201        skb_queue_head_init(&ep->com.ep_skb_list);
3202        if (alloc_ep_skb_list(&ep->com.ep_skb_list, CN_MAX_CON_BUF)) {
3203                err = -ENOMEM;
3204                goto fail1;
3205        }
3206
3207        init_timer(&ep->timer);
3208        ep->plen = conn_param->private_data_len;
3209        if (ep->plen)
3210                memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
3211                       conn_param->private_data, ep->plen);
3212        ep->ird = conn_param->ird;
3213        ep->ord = conn_param->ord;
3214
3215        if (peer2peer && ep->ord == 0)
3216                ep->ord = 1;
3217
3218        ep->com.cm_id = cm_id;
3219        ref_cm_id(&ep->com);
3220        ep->com.dev = dev;
3221        ep->com.qp = get_qhp(dev, conn_param->qpn);
3222        if (!ep->com.qp) {
3223                pr_debug("%s qpn 0x%x not found!\n", __func__, conn_param->qpn);
3224                err = -EINVAL;
3225                goto fail2;
3226        }
3227        ref_qp(ep);
3228        pr_debug("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
3229                 ep->com.qp, cm_id);
3230
3231        /*
3232         * Allocate an active TID to initiate a TCP connection.
3233         */
3234        ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
3235        if (ep->atid == -1) {
3236                pr_err("%s - cannot alloc atid\n", __func__);
3237                err = -ENOMEM;
3238                goto fail2;
3239        }
3240        insert_handle(dev, &dev->atid_idr, ep, ep->atid);
3241
3242        memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3243               sizeof(ep->com.local_addr));
3244        memcpy(&ep->com.remote_addr, &cm_id->m_remote_addr,
3245               sizeof(ep->com.remote_addr));
3246
3247        laddr = (struct sockaddr_in *)&ep->com.local_addr;
3248        raddr = (struct sockaddr_in *)&ep->com.remote_addr;
3249        laddr6 = (struct sockaddr_in6 *)&ep->com.local_addr;
3250        raddr6 = (struct sockaddr_in6 *) &ep->com.remote_addr;
3251
3252        if (cm_id->m_remote_addr.ss_family == AF_INET) {
3253                iptype = 4;
3254                ra = (__u8 *)&raddr->sin_addr;
3255
3256                /*
3257                 * Handle loopback requests to INADDR_ANY.
3258                 */
3259                if (raddr->sin_addr.s_addr == htonl(INADDR_ANY)) {
3260                        err = pick_local_ipaddrs(dev, cm_id);
3261                        if (err)
3262                                goto fail2;
3263                }
3264
3265                /* find a route */
3266                pr_debug("%s saddr %pI4 sport 0x%x raddr %pI4 rport 0x%x\n",
3267                         __func__, &laddr->sin_addr, ntohs(laddr->sin_port),
3268                         ra, ntohs(raddr->sin_port));
3269                ep->dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
3270                                          laddr->sin_addr.s_addr,
3271                                          raddr->sin_addr.s_addr,
3272                                          laddr->sin_port,
3273                                          raddr->sin_port, cm_id->tos);
3274        } else {
3275                iptype = 6;
3276                ra = (__u8 *)&raddr6->sin6_addr;
3277
3278                /*
3279                 * Handle loopback requests to INADDR_ANY.
3280                 */
3281                if (ipv6_addr_type(&raddr6->sin6_addr) == IPV6_ADDR_ANY) {
3282                        err = pick_local_ip6addrs(dev, cm_id);
3283                        if (err)
3284                                goto fail2;
3285                }
3286
3287                /* find a route */
3288                pr_debug("%s saddr %pI6 sport 0x%x raddr %pI6 rport 0x%x\n",
3289                         __func__, laddr6->sin6_addr.s6_addr,
3290                         ntohs(laddr6->sin6_port),
3291                         raddr6->sin6_addr.s6_addr, ntohs(raddr6->sin6_port));
3292                ep->dst = cxgb_find_route6(&dev->rdev.lldi, get_real_dev,
3293                                           laddr6->sin6_addr.s6_addr,
3294                                           raddr6->sin6_addr.s6_addr,
3295                                           laddr6->sin6_port,
3296                                           raddr6->sin6_port, 0,
3297                                           raddr6->sin6_scope_id);
3298        }
3299        if (!ep->dst) {
3300                pr_err("%s - cannot find route\n", __func__);
3301                err = -EHOSTUNREACH;
3302                goto fail3;
3303        }
3304
3305        err = import_ep(ep, iptype, ra, ep->dst, ep->com.dev, true,
3306                        ep->com.dev->rdev.lldi.adapter_type, cm_id->tos);
3307        if (err) {
3308                pr_err("%s - cannot alloc l2e\n", __func__);
3309                goto fail4;
3310        }
3311
3312        pr_debug("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
3313                 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
3314                 ep->l2t->idx);
3315
3316        state_set(&ep->com, CONNECTING);
3317        ep->tos = cm_id->tos;
3318
3319        /* send connect request to rnic */
3320        err = send_connect(ep);
3321        if (!err)
3322                goto out;
3323
3324        cxgb4_l2t_release(ep->l2t);
3325fail4:
3326        dst_release(ep->dst);
3327fail3:
3328        remove_handle(ep->com.dev, &ep->com.dev->atid_idr, ep->atid);
3329        cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
3330fail2:
3331        skb_queue_purge(&ep->com.ep_skb_list);
3332        deref_cm_id(&ep->com);
3333fail1:
3334        c4iw_put_ep(&ep->com);
3335out:
3336        return err;
3337}
3338
3339static int create_server6(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
3340{
3341        int err;
3342        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
3343                                    &ep->com.local_addr;
3344
3345        if (ipv6_addr_type(&sin6->sin6_addr) != IPV6_ADDR_ANY) {
3346                err = cxgb4_clip_get(ep->com.dev->rdev.lldi.ports[0],
3347                                     (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3348                if (err)
3349                        return err;
3350        }
3351        c4iw_init_wr_wait(&ep->com.wr_wait);
3352        err = cxgb4_create_server6(ep->com.dev->rdev.lldi.ports[0],
3353                                   ep->stid, &sin6->sin6_addr,
3354                                   sin6->sin6_port,
3355                                   ep->com.dev->rdev.lldi.rxq_ids[0]);
3356        if (!err)
3357                err = c4iw_wait_for_reply(&ep->com.dev->rdev,
3358                                          &ep->com.wr_wait,
3359                                          0, 0, __func__);
3360        else if (err > 0)
3361                err = net_xmit_errno(err);
3362        if (err) {
3363                cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3364                                   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3365                pr_err("cxgb4_create_server6/filter failed err %d stid %d laddr %pI6 lport %d\n",
3366                       err, ep->stid,
3367                       sin6->sin6_addr.s6_addr, ntohs(sin6->sin6_port));
3368        }
3369        return err;
3370}
3371
3372static int create_server4(struct c4iw_dev *dev, struct c4iw_listen_ep *ep)
3373{
3374        int err;
3375        struct sockaddr_in *sin = (struct sockaddr_in *)
3376                                  &ep->com.local_addr;
3377
3378        if (dev->rdev.lldi.enable_fw_ofld_conn) {
3379                do {
3380                        err = cxgb4_create_server_filter(
3381                                ep->com.dev->rdev.lldi.ports[0], ep->stid,
3382                                sin->sin_addr.s_addr, sin->sin_port, 0,
3383                                ep->com.dev->rdev.lldi.rxq_ids[0], 0, 0);
3384                        if (err == -EBUSY) {
3385                                if (c4iw_fatal_error(&ep->com.dev->rdev)) {
3386                                        err = -EIO;
3387                                        break;
3388                                }
3389                                set_current_state(TASK_UNINTERRUPTIBLE);
3390                                schedule_timeout(usecs_to_jiffies(100));
3391                        }
3392                } while (err == -EBUSY);
3393        } else {
3394                c4iw_init_wr_wait(&ep->com.wr_wait);
3395                err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0],
3396                                ep->stid, sin->sin_addr.s_addr, sin->sin_port,
3397                                0, ep->com.dev->rdev.lldi.rxq_ids[0]);
3398                if (!err)
3399                        err = c4iw_wait_for_reply(&ep->com.dev->rdev,
3400                                                  &ep->com.wr_wait,
3401                                                  0, 0, __func__);
3402                else if (err > 0)
3403                        err = net_xmit_errno(err);
3404        }
3405        if (err)
3406                pr_err("cxgb4_create_server/filter failed err %d stid %d laddr %pI4 lport %d\n"
3407                       , err, ep->stid,
3408                       &sin->sin_addr, ntohs(sin->sin_port));
3409        return err;
3410}
3411
3412int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
3413{
3414        int err = 0;
3415        struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
3416        struct c4iw_listen_ep *ep;
3417
3418        might_sleep();
3419
3420        ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
3421        if (!ep) {
3422                pr_err("%s - cannot alloc ep\n", __func__);
3423                err = -ENOMEM;
3424                goto fail1;
3425        }
3426        skb_queue_head_init(&ep->com.ep_skb_list);
3427        pr_debug("%s ep %p\n", __func__, ep);
3428        ep->com.cm_id = cm_id;
3429        ref_cm_id(&ep->com);
3430        ep->com.dev = dev;
3431        ep->backlog = backlog;
3432        memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3433               sizeof(ep->com.local_addr));
3434
3435        /*
3436         * Allocate a server TID.
3437         */
3438        if (dev->rdev.lldi.enable_fw_ofld_conn &&
3439            ep->com.local_addr.ss_family == AF_INET)
3440                ep->stid = cxgb4_alloc_sftid(dev->rdev.lldi.tids,
3441                                             cm_id->m_local_addr.ss_family, ep);
3442        else
3443                ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids,
3444                                            cm_id->m_local_addr.ss_family, ep);
3445
3446        if (ep->stid == -1) {
3447                pr_err("%s - cannot alloc stid\n", __func__);
3448                err = -ENOMEM;
3449                goto fail2;
3450        }
3451        insert_handle(dev, &dev->stid_idr, ep, ep->stid);
3452
3453        memcpy(&ep->com.local_addr, &cm_id->m_local_addr,
3454               sizeof(ep->com.local_addr));
3455
3456        state_set(&ep->com, LISTEN);
3457        if (ep->com.local_addr.ss_family == AF_INET)
3458                err = create_server4(dev, ep);
3459        else
3460                err = create_server6(dev, ep);
3461        if (!err) {
3462                cm_id->provider_data = ep;
3463                goto out;
3464        }
3465        remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3466        cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
3467                        ep->com.local_addr.ss_family);
3468fail2:
3469        deref_cm_id(&ep->com);
3470        c4iw_put_ep(&ep->com);
3471fail1:
3472out:
3473        return err;
3474}
3475
3476int c4iw_destroy_listen(struct iw_cm_id *cm_id)
3477{
3478        int err;
3479        struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
3480
3481        pr_debug("%s ep %p\n", __func__, ep);
3482
3483        might_sleep();
3484        state_set(&ep->com, DEAD);
3485        if (ep->com.dev->rdev.lldi.enable_fw_ofld_conn &&
3486            ep->com.local_addr.ss_family == AF_INET) {
3487                err = cxgb4_remove_server_filter(
3488                        ep->com.dev->rdev.lldi.ports[0], ep->stid,
3489                        ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3490        } else {
3491                struct sockaddr_in6 *sin6;
3492                c4iw_init_wr_wait(&ep->com.wr_wait);
3493                err = cxgb4_remove_server(
3494                                ep->com.dev->rdev.lldi.ports[0], ep->stid,
3495                                ep->com.dev->rdev.lldi.rxq_ids[0], 0);
3496                if (err)
3497                        goto done;
3498                err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait,
3499                                          0, 0, __func__);
3500                sin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
3501                cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3502                                   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3503        }
3504        remove_handle(ep->com.dev, &ep->com.dev->stid_idr, ep->stid);
3505        cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid,
3506                        ep->com.local_addr.ss_family);
3507done:
3508        deref_cm_id(&ep->com);
3509        c4iw_put_ep(&ep->com);
3510        return err;
3511}
3512
3513int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
3514{
3515        int ret = 0;
3516        int close = 0;
3517        int fatal = 0;
3518        struct c4iw_rdev *rdev;
3519
3520        mutex_lock(&ep->com.mutex);
3521
3522        pr_debug("%s ep %p state %s, abrupt %d\n", __func__, ep,
3523                 states[ep->com.state], abrupt);
3524
3525        /*
3526         * Ref the ep here in case we have fatal errors causing the
3527         * ep to be released and freed.
3528         */
3529        c4iw_get_ep(&ep->com);
3530
3531        rdev = &ep->com.dev->rdev;
3532        if (c4iw_fatal_error(rdev)) {
3533                fatal = 1;
3534                close_complete_upcall(ep, -EIO);
3535                ep->com.state = DEAD;
3536        }
3537        switch (ep->com.state) {
3538        case MPA_REQ_WAIT:
3539        case MPA_REQ_SENT:
3540        case MPA_REQ_RCVD:
3541        case MPA_REP_SENT:
3542        case FPDU_MODE:
3543        case CONNECTING:
3544                close = 1;
3545                if (abrupt)
3546                        ep->com.state = ABORTING;
3547                else {
3548                        ep->com.state = CLOSING;
3549
3550                        /*
3551                         * if we close before we see the fw4_ack() then we fix
3552                         * up the timer state since we're reusing it.
3553                         */
3554                        if (ep->mpa_skb &&
3555                            test_bit(STOP_MPA_TIMER, &ep->com.flags)) {
3556                                clear_bit(STOP_MPA_TIMER, &ep->com.flags);
3557                                stop_ep_timer(ep);
3558                        }
3559                        start_ep_timer(ep);
3560                }
3561                set_bit(CLOSE_SENT, &ep->com.flags);
3562                break;
3563        case CLOSING:
3564                if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
3565                        close = 1;
3566                        if (abrupt) {
3567                                (void)stop_ep_timer(ep);
3568                                ep->com.state = ABORTING;
3569                        } else
3570                                ep->com.state = MORIBUND;
3571                }
3572                break;
3573        case MORIBUND:
3574        case ABORTING:
3575        case DEAD:
3576                pr_debug("%s ignoring disconnect ep %p state %u\n",
3577                         __func__, ep, ep->com.state);
3578                break;
3579        default:
3580                BUG();
3581                break;
3582        }
3583
3584        if (close) {
3585                if (abrupt) {
3586                        set_bit(EP_DISC_ABORT, &ep->com.history);
3587                        close_complete_upcall(ep, -ECONNRESET);
3588                        ret = send_abort(ep);
3589                } else {
3590                        set_bit(EP_DISC_CLOSE, &ep->com.history);
3591                        ret = send_halfclose(ep);
3592                }
3593                if (ret) {
3594                        set_bit(EP_DISC_FAIL, &ep->com.history);
3595                        if (!abrupt) {
3596                                stop_ep_timer(ep);
3597                                close_complete_upcall(ep, -EIO);
3598                        }
3599                        if (ep->com.qp) {
3600                                struct c4iw_qp_attributes attrs;
3601
3602                                attrs.next_state = C4IW_QP_STATE_ERROR;
3603                                ret = c4iw_modify_qp(ep->com.qp->rhp,
3604                                                     ep->com.qp,
3605                                                     C4IW_QP_ATTR_NEXT_STATE,
3606                                                     &attrs, 1);
3607                                if (ret)
3608                                        pr_err("%s - qp <- error failed!\n",
3609                                               __func__);
3610                        }
3611                        fatal = 1;
3612                }
3613        }
3614        mutex_unlock(&ep->com.mutex);
3615        c4iw_put_ep(&ep->com);
3616        if (fatal)
3617                release_ep_resources(ep);
3618        return ret;
3619}
3620
3621static void active_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
3622                        struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
3623{
3624        struct c4iw_ep *ep;
3625        int atid = be32_to_cpu(req->tid);
3626
3627        ep = (struct c4iw_ep *)lookup_atid(dev->rdev.lldi.tids,
3628                                           (__force u32) req->tid);
3629        if (!ep)
3630                return;
3631
3632        switch (req->retval) {
3633        case FW_ENOMEM:
3634                set_bit(ACT_RETRY_NOMEM, &ep->com.history);
3635                if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
3636                        send_fw_act_open_req(ep, atid);
3637                        return;
3638                }
3639        case FW_EADDRINUSE:
3640                set_bit(ACT_RETRY_INUSE, &ep->com.history);
3641                if (ep->retry_count++ < ACT_OPEN_RETRY_COUNT) {
3642                        send_fw_act_open_req(ep, atid);
3643                        return;
3644                }
3645                break;
3646        default:
3647                pr_info("%s unexpected ofld conn wr retval %d\n",
3648                       __func__, req->retval);
3649                break;
3650        }
3651        pr_err("active ofld_connect_wr failure %d atid %d\n",
3652               req->retval, atid);
3653        mutex_lock(&dev->rdev.stats.lock);
3654        dev->rdev.stats.act_ofld_conn_fails++;
3655        mutex_unlock(&dev->rdev.stats.lock);
3656        connect_reply_upcall(ep, status2errno(req->retval));
3657        state_set(&ep->com, DEAD);
3658        if (ep->com.remote_addr.ss_family == AF_INET6) {
3659                struct sockaddr_in6 *sin6 =
3660                        (struct sockaddr_in6 *)&ep->com.local_addr;
3661                cxgb4_clip_release(ep->com.dev->rdev.lldi.ports[0],
3662                                   (const u32 *)&sin6->sin6_addr.s6_addr, 1);
3663        }
3664        remove_handle(dev, &dev->atid_idr, atid);
3665        cxgb4_free_atid(dev->rdev.lldi.tids, atid);
3666        dst_release(ep->dst);
3667        cxgb4_l2t_release(ep->l2t);
3668        c4iw_put_ep(&ep->com);
3669}
3670
3671static void passive_ofld_conn_reply(struct c4iw_dev *dev, struct sk_buff *skb,
3672                        struct cpl_fw6_msg_ofld_connection_wr_rpl *req)
3673{
3674        struct sk_buff *rpl_skb;
3675        struct cpl_pass_accept_req *cpl;
3676        int ret;
3677
3678        rpl_skb = (struct sk_buff *)(unsigned long)req->cookie;
3679        BUG_ON(!rpl_skb);
3680        if (req->retval) {
3681                pr_debug("%s passive open failure %d\n", __func__, req->retval);
3682                mutex_lock(&dev->rdev.stats.lock);
3683                dev->rdev.stats.pas_ofld_conn_fails++;
3684                mutex_unlock(&dev->rdev.stats.lock);
3685                kfree_skb(rpl_skb);
3686        } else {
3687                cpl = (struct cpl_pass_accept_req *)cplhdr(rpl_skb);
3688                OPCODE_TID(cpl) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ,
3689                                        (__force u32) htonl(
3690                                        (__force u32) req->tid)));
3691                ret = pass_accept_req(dev, rpl_skb);
3692                if (!ret)
3693                        kfree_skb(rpl_skb);
3694        }
3695        return;
3696}
3697
3698static int deferred_fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
3699{
3700        struct cpl_fw6_msg *rpl = cplhdr(skb);
3701        struct cpl_fw6_msg_ofld_connection_wr_rpl *req;
3702
3703        switch (rpl->type) {
3704        case FW6_TYPE_CQE:
3705                c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
3706                break;
3707        case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
3708                req = (struct cpl_fw6_msg_ofld_connection_wr_rpl *)rpl->data;
3709                switch (req->t_state) {
3710                case TCP_SYN_SENT:
3711                        active_ofld_conn_reply(dev, skb, req);
3712                        break;
3713                case TCP_SYN_RECV:
3714                        passive_ofld_conn_reply(dev, skb, req);
3715                        break;
3716                default:
3717                        pr_err("%s unexpected ofld conn wr state %d\n",
3718                               __func__, req->t_state);
3719                        break;
3720                }
3721                break;
3722        }
3723        return 0;
3724}
3725
3726static void build_cpl_pass_accept_req(struct sk_buff *skb, int stid , u8 tos)
3727{
3728        __be32 l2info;
3729        __be16 hdr_len, vlantag, len;
3730        u16 eth_hdr_len;
3731        int tcp_hdr_len, ip_hdr_len;
3732        u8 intf;
3733        struct cpl_rx_pkt *cpl = cplhdr(skb);
3734        struct cpl_pass_accept_req *req;
3735        struct tcp_options_received tmp_opt;
3736        struct c4iw_dev *dev;
3737        enum chip_type type;
3738
3739        dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
3740        /* Store values from cpl_rx_pkt in temporary location. */
3741        vlantag = cpl->vlan;
3742        len = cpl->len;
3743        l2info  = cpl->l2info;
3744        hdr_len = cpl->hdr_len;
3745        intf = cpl->iff;
3746
3747        __skb_pull(skb, sizeof(*req) + sizeof(struct rss_header));
3748
3749        /*
3750         * We need to parse the TCP options from SYN packet.
3751         * to generate cpl_pass_accept_req.
3752         */
3753        memset(&tmp_opt, 0, sizeof(tmp_opt));
3754        tcp_clear_options(&tmp_opt);
3755        tcp_parse_options(&init_net, skb, &tmp_opt, 0, NULL);
3756
3757        req = __skb_push(skb, sizeof(*req));
3758        memset(req, 0, sizeof(*req));
3759        req->l2info = cpu_to_be16(SYN_INTF_V(intf) |
3760                         SYN_MAC_IDX_V(RX_MACIDX_G(
3761                         be32_to_cpu(l2info))) |
3762                         SYN_XACT_MATCH_F);
3763        type = dev->rdev.lldi.adapter_type;
3764        tcp_hdr_len = RX_TCPHDR_LEN_G(be16_to_cpu(hdr_len));
3765        ip_hdr_len = RX_IPHDR_LEN_G(be16_to_cpu(hdr_len));
3766        req->hdr_len =
3767                cpu_to_be32(SYN_RX_CHAN_V(RX_CHAN_G(be32_to_cpu(l2info))));
3768        if (CHELSIO_CHIP_VERSION(type) <= CHELSIO_T5) {
3769                eth_hdr_len = is_t4(type) ?
3770                                RX_ETHHDR_LEN_G(be32_to_cpu(l2info)) :
3771                                RX_T5_ETHHDR_LEN_G(be32_to_cpu(l2info));
3772                req->hdr_len |= cpu_to_be32(TCP_HDR_LEN_V(tcp_hdr_len) |
3773                                            IP_HDR_LEN_V(ip_hdr_len) |
3774                                            ETH_HDR_LEN_V(eth_hdr_len));
3775        } else { /* T6 and later */
3776                eth_hdr_len = RX_T6_ETHHDR_LEN_G(be32_to_cpu(l2info));
3777                req->hdr_len |= cpu_to_be32(T6_TCP_HDR_LEN_V(tcp_hdr_len) |
3778                                            T6_IP_HDR_LEN_V(ip_hdr_len) |
3779                                            T6_ETH_HDR_LEN_V(eth_hdr_len));
3780        }
3781        req->vlan = vlantag;
3782        req->len = len;
3783        req->tos_stid = cpu_to_be32(PASS_OPEN_TID_V(stid) |
3784                                    PASS_OPEN_TOS_V(tos));
3785        req->tcpopt.mss = htons(tmp_opt.mss_clamp);
3786        if (tmp_opt.wscale_ok)
3787                req->tcpopt.wsf = tmp_opt.snd_wscale;
3788        req->tcpopt.tstamp = tmp_opt.saw_tstamp;
3789        if (tmp_opt.sack_ok)
3790                req->tcpopt.sack = 1;
3791        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_ACCEPT_REQ, 0));
3792        return;
3793}
3794
3795static void send_fw_pass_open_req(struct c4iw_dev *dev, struct sk_buff *skb,
3796                                  __be32 laddr, __be16 lport,
3797                                  __be32 raddr, __be16 rport,
3798                                  u32 rcv_isn, u32 filter, u16 window,
3799                                  u32 rss_qid, u8 port_id)
3800{
3801        struct sk_buff *req_skb;
3802        struct fw_ofld_connection_wr *req;
3803        struct cpl_pass_accept_req *cpl = cplhdr(skb);
3804        int ret;
3805
3806        req_skb = alloc_skb(sizeof(struct fw_ofld_connection_wr), GFP_KERNEL);
3807        if (!req_skb)
3808                return;
3809        req = __skb_put_zero(req_skb, sizeof(*req));
3810        req->op_compl = htonl(WR_OP_V(FW_OFLD_CONNECTION_WR) | FW_WR_COMPL_F);
3811        req->len16_pkd = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)));
3812        req->le.version_cpl = htonl(FW_OFLD_CONNECTION_WR_CPL_F);
3813        req->le.filter = (__force __be32) filter;
3814        req->le.lport = lport;
3815        req->le.pport = rport;
3816        req->le.u.ipv4.lip = laddr;
3817        req->le.u.ipv4.pip = raddr;
3818        req->tcb.rcv_nxt = htonl(rcv_isn + 1);
3819        req->tcb.rcv_adv = htons(window);
3820        req->tcb.t_state_to_astid =
3821                 htonl(FW_OFLD_CONNECTION_WR_T_STATE_V(TCP_SYN_RECV) |
3822                        FW_OFLD_CONNECTION_WR_RCV_SCALE_V(cpl->tcpopt.wsf) |
3823                        FW_OFLD_CONNECTION_WR_ASTID_V(
3824                        PASS_OPEN_TID_G(ntohl(cpl->tos_stid))));
3825
3826        /*
3827         * We store the qid in opt2 which will be used by the firmware
3828         * to send us the wr response.
3829         */
3830        req->tcb.opt2 = htonl(RSS_QUEUE_V(rss_qid));
3831
3832        /*
3833         * We initialize the MSS index in TCB to 0xF.
3834         * So that when driver sends cpl_pass_accept_rpl
3835         * TCB picks up the correct value. If this was 0
3836         * TP will ignore any value > 0 for MSS index.
3837         */
3838        req->tcb.opt0 = cpu_to_be64(MSS_IDX_V(0xF));
3839        req->cookie = (uintptr_t)skb;
3840
3841        set_wr_txq(req_skb, CPL_PRIORITY_CONTROL, port_id);
3842        ret = cxgb4_ofld_send(dev->rdev.lldi.ports[0], req_skb);
3843        if (ret < 0) {
3844                pr_err("%s - cxgb4_ofld_send error %d - dropping\n", __func__,
3845                       ret);
3846                kfree_skb(skb);
3847                kfree_skb(req_skb);
3848        }
3849}
3850
3851/*
3852 * Handler for CPL_RX_PKT message. Need to handle cpl_rx_pkt
3853 * messages when a filter is being used instead of server to
3854 * redirect a syn packet. When packets hit filter they are redirected
3855 * to the offload queue and driver tries to establish the connection
3856 * using firmware work request.
3857 */
3858static int rx_pkt(struct c4iw_dev *dev, struct sk_buff *skb)
3859{
3860        int stid;
3861        unsigned int filter;
3862        struct ethhdr *eh = NULL;
3863        struct vlan_ethhdr *vlan_eh = NULL;
3864        struct iphdr *iph;
3865        struct tcphdr *tcph;
3866        struct rss_header *rss = (void *)skb->data;
3867        struct cpl_rx_pkt *cpl = (void *)skb->data;
3868        struct cpl_pass_accept_req *req = (void *)(rss + 1);
3869        struct l2t_entry *e;
3870        struct dst_entry *dst;
3871        struct c4iw_ep *lep = NULL;
3872        u16 window;
3873        struct port_info *pi;
3874        struct net_device *pdev;
3875        u16 rss_qid, eth_hdr_len;
3876        int step;
3877        u32 tx_chan;
3878        struct neighbour *neigh;
3879
3880        /* Drop all non-SYN packets */
3881        if (!(cpl->l2info & cpu_to_be32(RXF_SYN_F)))
3882                goto reject;
3883
3884        /*
3885         * Drop all packets which did not hit the filter.
3886         * Unlikely to happen.
3887         */
3888        if (!(rss->filter_hit && rss->filter_tid))
3889                goto reject;
3890
3891        /*
3892         * Calculate the server tid from filter hit index from cpl_rx_pkt.
3893         */
3894        stid = (__force int) cpu_to_be32((__force u32) rss->hash_val);
3895
3896        lep = (struct c4iw_ep *)get_ep_from_stid(dev, stid);
3897        if (!lep) {
3898                pr_debug("%s connect request on invalid stid %d\n",
3899                         __func__, stid);
3900                goto reject;
3901        }
3902
3903        switch (CHELSIO_CHIP_VERSION(dev->rdev.lldi.adapter_type)) {
3904        case CHELSIO_T4:
3905                eth_hdr_len = RX_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
3906                break;
3907        case CHELSIO_T5:
3908                eth_hdr_len = RX_T5_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
3909                break;
3910        case CHELSIO_T6:
3911                eth_hdr_len = RX_T6_ETHHDR_LEN_G(be32_to_cpu(cpl->l2info));
3912                break;
3913        default:
3914                pr_err("T%d Chip is not supported\n",
3915                       CHELSIO_CHIP_VERSION(dev->rdev.lldi.adapter_type));
3916                goto reject;
3917        }
3918
3919        if (eth_hdr_len == ETH_HLEN) {
3920                eh = (struct ethhdr *)(req + 1);
3921                iph = (struct iphdr *)(eh + 1);
3922        } else {
3923                vlan_eh = (struct vlan_ethhdr *)(req + 1);
3924                iph = (struct iphdr *)(vlan_eh + 1);
3925                skb->vlan_tci = ntohs(cpl->vlan);
3926        }
3927
3928        if (iph->version != 0x4)
3929                goto reject;
3930
3931        tcph = (struct tcphdr *)(iph + 1);
3932        skb_set_network_header(skb, (void *)iph - (void *)rss);
3933        skb_set_transport_header(skb, (void *)tcph - (void *)rss);
3934        skb_get(skb);
3935
3936        pr_debug("%s lip 0x%x lport %u pip 0x%x pport %u tos %d\n", __func__,
3937                 ntohl(iph->daddr), ntohs(tcph->dest), ntohl(iph->saddr),
3938                 ntohs(tcph->source), iph->tos);
3939
3940        dst = cxgb_find_route(&dev->rdev.lldi, get_real_dev,
3941                              iph->daddr, iph->saddr, tcph->dest,
3942                              tcph->source, iph->tos);
3943        if (!dst) {
3944                pr_err("%s - failed to find dst entry!\n",
3945                       __func__);
3946                goto reject;
3947        }
3948        neigh = dst_neigh_lookup_skb(dst, skb);
3949
3950        if (!neigh) {
3951                pr_err("%s - failed to allocate neigh!\n",
3952                       __func__);
3953                goto free_dst;
3954        }
3955
3956        if (neigh->dev->flags & IFF_LOOPBACK) {
3957                pdev = ip_dev_find(&init_net, iph->daddr);
3958                e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3959                                    pdev, 0);
3960                pi = (struct port_info *)netdev_priv(pdev);
3961                tx_chan = cxgb4_port_chan(pdev);
3962                dev_put(pdev);
3963        } else {
3964                pdev = get_real_dev(neigh->dev);
3965                e = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh,
3966                                        pdev, 0);
3967                pi = (struct port_info *)netdev_priv(pdev);
3968                tx_chan = cxgb4_port_chan(pdev);
3969        }
3970        neigh_release(neigh);
3971        if (!e) {
3972                pr_err("%s - failed to allocate l2t entry!\n",
3973                       __func__);
3974                goto free_dst;
3975        }
3976
3977        step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
3978        rss_qid = dev->rdev.lldi.rxq_ids[pi->port_id * step];
3979        window = (__force u16) htons((__force u16)tcph->window);
3980
3981        /* Calcuate filter portion for LE region. */
3982        filter = (__force unsigned int) cpu_to_be32(cxgb4_select_ntuple(
3983                                                    dev->rdev.lldi.ports[0],
3984                                                    e));
3985
3986        /*
3987         * Synthesize the cpl_pass_accept_req. We have everything except the
3988         * TID. Once firmware sends a reply with TID we update the TID field
3989         * in cpl and pass it through the regular cpl_pass_accept_req path.
3990         */
3991        build_cpl_pass_accept_req(skb, stid, iph->tos);
3992        send_fw_pass_open_req(dev, skb, iph->daddr, tcph->dest, iph->saddr,
3993                              tcph->source, ntohl(tcph->seq), filter, window,
3994                              rss_qid, pi->port_id);
3995        cxgb4_l2t_release(e);
3996free_dst:
3997        dst_release(dst);
3998reject:
3999        if (lep)
4000                c4iw_put_ep(&lep->com);
4001        return 0;
4002}
4003
4004/*
4005 * These are the real handlers that are called from a
4006 * work queue.
4007 */
4008static c4iw_handler_func work_handlers[NUM_CPL_CMDS + NUM_FAKE_CPLS] = {
4009        [CPL_ACT_ESTABLISH] = act_establish,
4010        [CPL_ACT_OPEN_RPL] = act_open_rpl,
4011        [CPL_RX_DATA] = rx_data,
4012        [CPL_ABORT_RPL_RSS] = abort_rpl,
4013        [CPL_ABORT_RPL] = abort_rpl,
4014        [CPL_PASS_OPEN_RPL] = pass_open_rpl,
4015        [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
4016        [CPL_PASS_ACCEPT_REQ] = pass_accept_req,
4017        [CPL_PASS_ESTABLISH] = pass_establish,
4018        [CPL_PEER_CLOSE] = peer_close,
4019        [CPL_ABORT_REQ_RSS] = peer_abort,
4020        [CPL_CLOSE_CON_RPL] = close_con_rpl,
4021        [CPL_RDMA_TERMINATE] = terminate,
4022        [CPL_FW4_ACK] = fw4_ack,
4023        [CPL_FW6_MSG] = deferred_fw6_msg,
4024        [CPL_RX_PKT] = rx_pkt,
4025        [FAKE_CPL_PUT_EP_SAFE] = _put_ep_safe,
4026        [FAKE_CPL_PASS_PUT_EP_SAFE] = _put_pass_ep_safe
4027};
4028
4029static void process_timeout(struct c4iw_ep *ep)
4030{
4031        struct c4iw_qp_attributes attrs;
4032        int abort = 1;
4033
4034        mutex_lock(&ep->com.mutex);
4035        pr_debug("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
4036                 ep->com.state);
4037        set_bit(TIMEDOUT, &ep->com.history);
4038        switch (ep->com.state) {
4039        case MPA_REQ_SENT:
4040                connect_reply_upcall(ep, -ETIMEDOUT);
4041                break;
4042        case MPA_REQ_WAIT:
4043        case MPA_REQ_RCVD:
4044        case MPA_REP_SENT:
4045        case FPDU_MODE:
4046                break;
4047        case CLOSING:
4048        case MORIBUND:
4049                if (ep->com.cm_id && ep->com.qp) {
4050                        attrs.next_state = C4IW_QP_STATE_ERROR;
4051                        c4iw_modify_qp(ep->com.qp->rhp,
4052                                     ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
4053                                     &attrs, 1);
4054                }
4055                close_complete_upcall(ep, -ETIMEDOUT);
4056                break;
4057        case ABORTING:
4058        case DEAD:
4059
4060                /*
4061                 * These states are expected if the ep timed out at the same
4062                 * time as another thread was calling stop_ep_timer().
4063                 * So we silently do nothing for these states.
4064                 */
4065                abort = 0;
4066                break;
4067        default:
4068                WARN(1, "%s unexpected state ep %p tid %u state %u\n",
4069                        __func__, ep, ep->hwtid, ep->com.state);
4070                abort = 0;
4071        }
4072        mutex_unlock(&ep->com.mutex);
4073        if (abort)
4074                c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
4075        c4iw_put_ep(&ep->com);
4076}
4077
4078static void process_timedout_eps(void)
4079{
4080        struct c4iw_ep *ep;
4081
4082        spin_lock_irq(&timeout_lock);
4083        while (!list_empty(&timeout_list)) {
4084                struct list_head *tmp;
4085
4086                tmp = timeout_list.next;
4087                list_del(tmp);
4088                tmp->next = NULL;
4089                tmp->prev = NULL;
4090                spin_unlock_irq(&timeout_lock);
4091                ep = list_entry(tmp, struct c4iw_ep, entry);
4092                process_timeout(ep);
4093                spin_lock_irq(&timeout_lock);
4094        }
4095        spin_unlock_irq(&timeout_lock);
4096}
4097
4098static void process_work(struct work_struct *work)
4099{
4100        struct sk_buff *skb = NULL;
4101        struct c4iw_dev *dev;
4102        struct cpl_act_establish *rpl;
4103        unsigned int opcode;
4104        int ret;
4105
4106        process_timedout_eps();
4107        while ((skb = skb_dequeue(&rxq))) {
4108                rpl = cplhdr(skb);
4109                dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
4110                opcode = rpl->ot.opcode;
4111
4112                BUG_ON(!work_handlers[opcode]);
4113                ret = work_handlers[opcode](dev, skb);
4114                if (!ret)
4115                        kfree_skb(skb);
4116                process_timedout_eps();
4117        }
4118}
4119
4120static DECLARE_WORK(skb_work, process_work);
4121
4122static void ep_timeout(unsigned long arg)
4123{
4124        struct c4iw_ep *ep = (struct c4iw_ep *)arg;
4125        int kickit = 0;
4126
4127        spin_lock(&timeout_lock);
4128        if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
4129                /*
4130                 * Only insert if it is not already on the list.
4131                 */
4132                if (!ep->entry.next) {
4133                        list_add_tail(&ep->entry, &timeout_list);
4134                        kickit = 1;
4135                }
4136        }
4137        spin_unlock(&timeout_lock);
4138        if (kickit)
4139                queue_work(workq, &skb_work);
4140}
4141
4142/*
4143 * All the CM events are handled on a work queue to have a safe context.
4144 */
4145static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
4146{
4147
4148        /*
4149         * Save dev in the skb->cb area.
4150         */
4151        *((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
4152
4153        /*
4154         * Queue the skb and schedule the worker thread.
4155         */
4156        skb_queue_tail(&rxq, skb);
4157        queue_work(workq, &skb_work);
4158        return 0;
4159}
4160
4161static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
4162{
4163        struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
4164
4165        if (rpl->status != CPL_ERR_NONE) {
4166                pr_err("Unexpected SET_TCB_RPL status %u for tid %u\n",
4167                       rpl->status, GET_TID(rpl));
4168        }
4169        kfree_skb(skb);
4170        return 0;
4171}
4172
4173static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
4174{
4175        struct cpl_fw6_msg *rpl = cplhdr(skb);
4176        struct c4iw_wr_wait *wr_waitp;
4177        int ret;
4178
4179        pr_debug("%s type %u\n", __func__, rpl->type);
4180
4181        switch (rpl->type) {
4182        case FW6_TYPE_WR_RPL:
4183                ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
4184                wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
4185                pr_debug("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
4186                if (wr_waitp)
4187                        c4iw_wake_up(wr_waitp, ret ? -ret : 0);
4188                kfree_skb(skb);
4189                break;
4190        case FW6_TYPE_CQE:
4191        case FW6_TYPE_OFLD_CONNECTION_WR_RPL:
4192                sched(dev, skb);
4193                break;
4194        default:
4195                pr_err("%s unexpected fw6 msg type %u\n",
4196                       __func__, rpl->type);
4197                kfree_skb(skb);
4198                break;
4199        }
4200        return 0;
4201}
4202
4203static int peer_abort_intr(struct c4iw_dev *dev, struct sk_buff *skb)
4204{
4205        struct cpl_abort_req_rss *req = cplhdr(skb);
4206        struct c4iw_ep *ep;
4207        unsigned int tid = GET_TID(req);
4208
4209        ep = get_ep_from_tid(dev, tid);
4210        /* This EP will be dereferenced in peer_abort() */
4211        if (!ep) {
4212                pr_warn("Abort on non-existent endpoint, tid %d\n", tid);
4213                kfree_skb(skb);
4214                return 0;
4215        }
4216        if (cxgb_is_neg_adv(req->status)) {
4217                pr_debug("%s Negative advice on abort- tid %u status %d (%s)\n",
4218                         __func__, ep->hwtid, req->status,
4219                         neg_adv_str(req->status));
4220                goto out;
4221        }
4222        pr_debug("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
4223                 ep->com.state);
4224
4225        c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
4226out:
4227        sched(dev, skb);
4228        return 0;
4229}
4230
4231/*
4232 * Most upcalls from the T4 Core go to sched() to
4233 * schedule the processing on a work queue.
4234 */
4235c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
4236        [CPL_ACT_ESTABLISH] = sched,
4237        [CPL_ACT_OPEN_RPL] = sched,
4238        [CPL_RX_DATA] = sched,
4239        [CPL_ABORT_RPL_RSS] = sched,
4240        [CPL_ABORT_RPL] = sched,
4241        [CPL_PASS_OPEN_RPL] = sched,
4242        [CPL_CLOSE_LISTSRV_RPL] = sched,
4243        [CPL_PASS_ACCEPT_REQ] = sched,
4244        [CPL_PASS_ESTABLISH] = sched,
4245        [CPL_PEER_CLOSE] = sched,
4246        [CPL_CLOSE_CON_RPL] = sched,
4247        [CPL_ABORT_REQ_RSS] = peer_abort_intr,
4248        [CPL_RDMA_TERMINATE] = sched,
4249        [CPL_FW4_ACK] = sched,
4250        [CPL_SET_TCB_RPL] = set_tcb_rpl,
4251        [CPL_FW6_MSG] = fw6_msg,
4252        [CPL_RX_PKT] = sched
4253};
4254
4255int __init c4iw_cm_init(void)
4256{
4257        spin_lock_init(&timeout_lock);
4258        skb_queue_head_init(&rxq);
4259
4260        workq = alloc_ordered_workqueue("iw_cxgb4", WQ_MEM_RECLAIM);
4261        if (!workq)
4262                return -ENOMEM;
4263
4264        return 0;
4265}
4266
4267void c4iw_cm_term(void)
4268{
4269        WARN_ON(!list_empty(&timeout_list));
4270        flush_workqueue(workq);
4271        destroy_workqueue(workq);
4272}
4273