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