linux/drivers/infiniband/hw/i40iw/i40iw_cm.c
<<
>>
Prefs
   1/*******************************************************************************
   2*
   3* Copyright (c) 2015-2016 Intel Corporation.  All rights reserved.
   4*
   5* This software is available to you under a choice of one of two
   6* licenses.  You may choose to be licensed under the terms of the GNU
   7* General Public License (GPL) Version 2, available from the file
   8* COPYING in the main directory of this source tree, or the
   9* OpenFabrics.org BSD license below:
  10*
  11*   Redistribution and use in source and binary forms, with or
  12*   without modification, are permitted provided that the following
  13*   conditions are met:
  14*
  15*    - Redistributions of source code must retain the above
  16*       copyright notice, this list of conditions and the following
  17*       disclaimer.
  18*
  19*    - Redistributions in binary form must reproduce the above
  20*       copyright notice, this list of conditions and the following
  21*       disclaimer in the documentation and/or other materials
  22*       provided with the distribution.
  23*
  24* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31* SOFTWARE.
  32*
  33*******************************************************************************/
  34
  35#include <linux/atomic.h>
  36#include <linux/ip.h>
  37#include <linux/tcp.h>
  38#include <linux/init.h>
  39#include <linux/if_arp.h>
  40#include <linux/if_vlan.h>
  41#include <linux/notifier.h>
  42#include <linux/net.h>
  43#include <linux/types.h>
  44#include <linux/timer.h>
  45#include <linux/time.h>
  46#include <linux/delay.h>
  47#include <linux/etherdevice.h>
  48#include <linux/netdevice.h>
  49#include <linux/random.h>
  50#include <linux/list.h>
  51#include <linux/threads.h>
  52#include <linux/highmem.h>
  53#include <net/arp.h>
  54#include <net/ndisc.h>
  55#include <net/neighbour.h>
  56#include <net/route.h>
  57#include <net/addrconf.h>
  58#include <net/ip6_route.h>
  59#include <net/ip_fib.h>
  60#include <net/tcp.h>
  61#include <asm/checksum.h>
  62
  63#include "i40iw.h"
  64
  65static void i40iw_rem_ref_cm_node(struct i40iw_cm_node *);
  66static void i40iw_cm_post_event(struct i40iw_cm_event *event);
  67static void i40iw_disconnect_worker(struct work_struct *work);
  68
  69/**
  70 * i40iw_free_sqbuf - put back puda buffer if refcount = 0
  71 * @dev: FPK device
  72 * @buf: puda buffer to free
  73 */
  74void i40iw_free_sqbuf(struct i40iw_sc_dev *dev, void *bufp)
  75{
  76        struct i40iw_puda_buf *buf = (struct i40iw_puda_buf *)bufp;
  77        struct i40iw_puda_rsrc *ilq = dev->ilq;
  78
  79        if (!atomic_dec_return(&buf->refcount))
  80                i40iw_puda_ret_bufpool(ilq, buf);
  81}
  82
  83/**
  84 * i40iw_derive_hw_ird_setting - Calculate IRD
  85 *
  86 * @cm_ird: IRD of connection's node
  87 *
  88 * The ird from the connection is rounded to a supported HW
  89 * setting (2,8,32,64) and then encoded for ird_size field of
  90 * qp_ctx
  91 */
  92static u8 i40iw_derive_hw_ird_setting(u16 cm_ird)
  93{
  94        u8 encoded_ird_size;
  95        u8 pof2_cm_ird = 1;
  96
  97        /* round-off to next powerof2 */
  98        while (pof2_cm_ird < cm_ird)
  99                pof2_cm_ird *= 2;
 100
 101        /* ird_size field is encoded in qp_ctx */
 102        switch (pof2_cm_ird) {
 103        case I40IW_HW_IRD_SETTING_64:
 104                encoded_ird_size = 3;
 105                break;
 106        case I40IW_HW_IRD_SETTING_32:
 107        case I40IW_HW_IRD_SETTING_16:
 108                encoded_ird_size = 2;
 109                break;
 110        case I40IW_HW_IRD_SETTING_8:
 111        case I40IW_HW_IRD_SETTING_4:
 112                encoded_ird_size = 1;
 113                break;
 114        case I40IW_HW_IRD_SETTING_2:
 115        default:
 116                encoded_ird_size = 0;
 117                break;
 118        }
 119        return encoded_ird_size;
 120}
 121
 122/**
 123 * i40iw_record_ird_ord - Record IRD/ORD passed in
 124 * @cm_node: connection's node
 125 * @conn_ird: connection IRD
 126 * @conn_ord: connection ORD
 127 */
 128static void i40iw_record_ird_ord(struct i40iw_cm_node *cm_node, u16 conn_ird, u16 conn_ord)
 129{
 130        if (conn_ird > I40IW_MAX_IRD_SIZE)
 131                conn_ird = I40IW_MAX_IRD_SIZE;
 132
 133        if (conn_ord > I40IW_MAX_ORD_SIZE)
 134                conn_ord = I40IW_MAX_ORD_SIZE;
 135
 136        cm_node->ird_size = conn_ird;
 137        cm_node->ord_size = conn_ord;
 138}
 139
 140/**
 141 * i40iw_copy_ip_ntohl - change network to host ip
 142 * @dst: host ip
 143 * @src: big endian
 144 */
 145void i40iw_copy_ip_ntohl(u32 *dst, __be32 *src)
 146{
 147        *dst++ = ntohl(*src++);
 148        *dst++ = ntohl(*src++);
 149        *dst++ = ntohl(*src++);
 150        *dst = ntohl(*src);
 151}
 152
 153/**
 154 * i40iw_copy_ip_htonl - change host addr to network ip
 155 * @dst: host ip
 156 * @src: little endian
 157 */
 158static inline void i40iw_copy_ip_htonl(__be32 *dst, u32 *src)
 159{
 160        *dst++ = htonl(*src++);
 161        *dst++ = htonl(*src++);
 162        *dst++ = htonl(*src++);
 163        *dst = htonl(*src);
 164}
 165
 166/**
 167 * i40iw_fill_sockaddr4 - get addr info for passive connection
 168 * @cm_node: connection's node
 169 * @event: upper layer's cm event
 170 */
 171static inline void i40iw_fill_sockaddr4(struct i40iw_cm_node *cm_node,
 172                                        struct iw_cm_event *event)
 173{
 174        struct sockaddr_in *laddr = (struct sockaddr_in *)&event->local_addr;
 175        struct sockaddr_in *raddr = (struct sockaddr_in *)&event->remote_addr;
 176
 177        laddr->sin_family = AF_INET;
 178        raddr->sin_family = AF_INET;
 179
 180        laddr->sin_port = htons(cm_node->loc_port);
 181        raddr->sin_port = htons(cm_node->rem_port);
 182
 183        laddr->sin_addr.s_addr = htonl(cm_node->loc_addr[0]);
 184        raddr->sin_addr.s_addr = htonl(cm_node->rem_addr[0]);
 185}
 186
 187/**
 188 * i40iw_fill_sockaddr6 - get ipv6 addr info for passive side
 189 * @cm_node: connection's node
 190 * @event: upper layer's cm event
 191 */
 192static inline void i40iw_fill_sockaddr6(struct i40iw_cm_node *cm_node,
 193                                        struct iw_cm_event *event)
 194{
 195        struct sockaddr_in6 *laddr6 = (struct sockaddr_in6 *)&event->local_addr;
 196        struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)&event->remote_addr;
 197
 198        laddr6->sin6_family = AF_INET6;
 199        raddr6->sin6_family = AF_INET6;
 200
 201        laddr6->sin6_port = htons(cm_node->loc_port);
 202        raddr6->sin6_port = htons(cm_node->rem_port);
 203
 204        i40iw_copy_ip_htonl(laddr6->sin6_addr.in6_u.u6_addr32,
 205                            cm_node->loc_addr);
 206        i40iw_copy_ip_htonl(raddr6->sin6_addr.in6_u.u6_addr32,
 207                            cm_node->rem_addr);
 208}
 209
 210/**
 211 * i40iw_get_addr_info
 212 * @cm_node: contains ip/tcp info
 213 * @cm_info: to get a copy of the cm_node ip/tcp info
 214*/
 215static void i40iw_get_addr_info(struct i40iw_cm_node *cm_node,
 216                                struct i40iw_cm_info *cm_info)
 217{
 218        cm_info->ipv4 = cm_node->ipv4;
 219        cm_info->vlan_id = cm_node->vlan_id;
 220        memcpy(cm_info->loc_addr, cm_node->loc_addr, sizeof(cm_info->loc_addr));
 221        memcpy(cm_info->rem_addr, cm_node->rem_addr, sizeof(cm_info->rem_addr));
 222        cm_info->loc_port = cm_node->loc_port;
 223        cm_info->rem_port = cm_node->rem_port;
 224}
 225
 226/**
 227 * i40iw_get_cmevent_info - for cm event upcall
 228 * @cm_node: connection's node
 229 * @cm_id: upper layers cm struct for the event
 230 * @event: upper layer's cm event
 231 */
 232static inline void i40iw_get_cmevent_info(struct i40iw_cm_node *cm_node,
 233                                          struct iw_cm_id *cm_id,
 234                                          struct iw_cm_event *event)
 235{
 236        memcpy(&event->local_addr, &cm_id->m_local_addr,
 237               sizeof(event->local_addr));
 238        memcpy(&event->remote_addr, &cm_id->m_remote_addr,
 239               sizeof(event->remote_addr));
 240        if (cm_node) {
 241                event->private_data = (void *)cm_node->pdata_buf;
 242                event->private_data_len = (u8)cm_node->pdata.size;
 243                event->ird = cm_node->ird_size;
 244                event->ord = cm_node->ord_size;
 245        }
 246}
 247
 248/**
 249 * i40iw_send_cm_event - upcall cm's event handler
 250 * @cm_node: connection's node
 251 * @cm_id: upper layer's cm info struct
 252 * @type: Event type to indicate
 253 * @status: status for the event type
 254 */
 255static int i40iw_send_cm_event(struct i40iw_cm_node *cm_node,
 256                               struct iw_cm_id *cm_id,
 257                               enum iw_cm_event_type type,
 258                               int status)
 259{
 260        struct iw_cm_event event;
 261
 262        memset(&event, 0, sizeof(event));
 263        event.event = type;
 264        event.status = status;
 265        switch (type) {
 266        case IW_CM_EVENT_CONNECT_REQUEST:
 267                if (cm_node->ipv4)
 268                        i40iw_fill_sockaddr4(cm_node, &event);
 269                else
 270                        i40iw_fill_sockaddr6(cm_node, &event);
 271                event.provider_data = (void *)cm_node;
 272                event.private_data = (void *)cm_node->pdata_buf;
 273                event.private_data_len = (u8)cm_node->pdata.size;
 274                break;
 275        case IW_CM_EVENT_CONNECT_REPLY:
 276                i40iw_get_cmevent_info(cm_node, cm_id, &event);
 277                break;
 278        case IW_CM_EVENT_ESTABLISHED:
 279                event.ird = cm_node->ird_size;
 280                event.ord = cm_node->ord_size;
 281                break;
 282        case IW_CM_EVENT_DISCONNECT:
 283                break;
 284        case IW_CM_EVENT_CLOSE:
 285                break;
 286        default:
 287                i40iw_pr_err("event type received type = %d\n", type);
 288                return -1;
 289        }
 290        return cm_id->event_handler(cm_id, &event);
 291}
 292
 293/**
 294 * i40iw_create_event - create cm event
 295 * @cm_node: connection's node
 296 * @type: Event type to generate
 297 */
 298static struct i40iw_cm_event *i40iw_create_event(struct i40iw_cm_node *cm_node,
 299                                                 enum i40iw_cm_event_type type)
 300{
 301        struct i40iw_cm_event *event;
 302
 303        if (!cm_node->cm_id)
 304                return NULL;
 305
 306        event = kzalloc(sizeof(*event), GFP_ATOMIC);
 307
 308        if (!event)
 309                return NULL;
 310
 311        event->type = type;
 312        event->cm_node = cm_node;
 313        memcpy(event->cm_info.rem_addr, cm_node->rem_addr, sizeof(event->cm_info.rem_addr));
 314        memcpy(event->cm_info.loc_addr, cm_node->loc_addr, sizeof(event->cm_info.loc_addr));
 315        event->cm_info.rem_port = cm_node->rem_port;
 316        event->cm_info.loc_port = cm_node->loc_port;
 317        event->cm_info.cm_id = cm_node->cm_id;
 318
 319        i40iw_debug(cm_node->dev,
 320                    I40IW_DEBUG_CM,
 321                    "node=%p event=%p type=%u dst=%pI4 src=%pI4\n",
 322                    cm_node,
 323                    event,
 324                    type,
 325                    event->cm_info.loc_addr,
 326                    event->cm_info.rem_addr);
 327
 328        i40iw_cm_post_event(event);
 329        return event;
 330}
 331
 332/**
 333 * i40iw_free_retrans_entry - free send entry
 334 * @cm_node: connection's node
 335 */
 336static void i40iw_free_retrans_entry(struct i40iw_cm_node *cm_node)
 337{
 338        struct i40iw_sc_dev *dev = cm_node->dev;
 339        struct i40iw_timer_entry *send_entry;
 340
 341        send_entry = cm_node->send_entry;
 342        if (send_entry) {
 343                cm_node->send_entry = NULL;
 344                i40iw_free_sqbuf(dev, (void *)send_entry->sqbuf);
 345                kfree(send_entry);
 346                atomic_dec(&cm_node->ref_count);
 347        }
 348}
 349
 350/**
 351 * i40iw_cleanup_retrans_entry - free send entry with lock
 352 * @cm_node: connection's node
 353 */
 354static void i40iw_cleanup_retrans_entry(struct i40iw_cm_node *cm_node)
 355{
 356        unsigned long flags;
 357
 358        spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
 359        i40iw_free_retrans_entry(cm_node);
 360        spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
 361}
 362
 363static bool is_remote_ne020_or_chelsio(struct i40iw_cm_node *cm_node)
 364{
 365        if ((cm_node->rem_mac[0] == 0x0) &&
 366            (((cm_node->rem_mac[1] == 0x12) && (cm_node->rem_mac[2] == 0x55)) ||
 367             ((cm_node->rem_mac[1] == 0x07 && (cm_node->rem_mac[2] == 0x43)))))
 368                return true;
 369        return false;
 370}
 371
 372/**
 373 * i40iw_form_cm_frame - get a free packet and build frame
 374 * @cm_node: connection's node ionfo to use in frame
 375 * @options: pointer to options info
 376 * @hdr: pointer mpa header
 377 * @pdata: pointer to private data
 378 * @flags:  indicates FIN or ACK
 379 */
 380static struct i40iw_puda_buf *i40iw_form_cm_frame(struct i40iw_cm_node *cm_node,
 381                                                  struct i40iw_kmem_info *options,
 382                                                  struct i40iw_kmem_info *hdr,
 383                                                  struct i40iw_kmem_info *pdata,
 384                                                  u8 flags)
 385{
 386        struct i40iw_puda_buf *sqbuf;
 387        struct i40iw_sc_dev *dev = cm_node->dev;
 388        u8 *buf;
 389
 390        struct tcphdr *tcph;
 391        struct iphdr *iph;
 392        struct ipv6hdr *ip6h;
 393        struct ethhdr *ethh;
 394        u16 packetsize;
 395        u16 eth_hlen = ETH_HLEN;
 396        u32 opts_len = 0;
 397        u32 pd_len = 0;
 398        u32 hdr_len = 0;
 399
 400        sqbuf = i40iw_puda_get_bufpool(dev->ilq);
 401        if (!sqbuf)
 402                return NULL;
 403        buf = sqbuf->mem.va;
 404
 405        if (options)
 406                opts_len = (u32)options->size;
 407
 408        if (hdr)
 409                hdr_len = hdr->size;
 410
 411        if (pdata) {
 412                pd_len = pdata->size;
 413                if (!is_remote_ne020_or_chelsio(cm_node))
 414                        pd_len += MPA_ZERO_PAD_LEN;
 415        }
 416
 417        if (cm_node->vlan_id < VLAN_TAG_PRESENT)
 418                eth_hlen += 4;
 419
 420        if (cm_node->ipv4)
 421                packetsize = sizeof(*iph) + sizeof(*tcph);
 422        else
 423                packetsize = sizeof(*ip6h) + sizeof(*tcph);
 424        packetsize += opts_len + hdr_len + pd_len;
 425
 426        memset(buf, 0x00, eth_hlen + packetsize);
 427
 428        sqbuf->totallen = packetsize + eth_hlen;
 429        sqbuf->maclen = eth_hlen;
 430        sqbuf->tcphlen = sizeof(*tcph) + opts_len;
 431        sqbuf->scratch = (void *)cm_node;
 432
 433        ethh = (struct ethhdr *)buf;
 434        buf += eth_hlen;
 435
 436        if (cm_node->ipv4) {
 437                sqbuf->ipv4 = true;
 438
 439                iph = (struct iphdr *)buf;
 440                buf += sizeof(*iph);
 441                tcph = (struct tcphdr *)buf;
 442                buf += sizeof(*tcph);
 443
 444                ether_addr_copy(ethh->h_dest, cm_node->rem_mac);
 445                ether_addr_copy(ethh->h_source, cm_node->loc_mac);
 446                if (cm_node->vlan_id < VLAN_TAG_PRESENT) {
 447                        ((struct vlan_ethhdr *)ethh)->h_vlan_proto = htons(ETH_P_8021Q);
 448                        ((struct vlan_ethhdr *)ethh)->h_vlan_TCI = htons(cm_node->vlan_id);
 449
 450                        ((struct vlan_ethhdr *)ethh)->h_vlan_encapsulated_proto = htons(ETH_P_IP);
 451                } else {
 452                        ethh->h_proto = htons(ETH_P_IP);
 453                }
 454
 455                iph->version = IPVERSION;
 456                iph->ihl = 5;   /* 5 * 4Byte words, IP headr len */
 457                iph->tos = 0;
 458                iph->tot_len = htons(packetsize);
 459                iph->id = htons(++cm_node->tcp_cntxt.loc_id);
 460
 461                iph->frag_off = htons(0x4000);
 462                iph->ttl = 0x40;
 463                iph->protocol = IPPROTO_TCP;
 464                iph->saddr = htonl(cm_node->loc_addr[0]);
 465                iph->daddr = htonl(cm_node->rem_addr[0]);
 466        } else {
 467                sqbuf->ipv4 = false;
 468                ip6h = (struct ipv6hdr *)buf;
 469                buf += sizeof(*ip6h);
 470                tcph = (struct tcphdr *)buf;
 471                buf += sizeof(*tcph);
 472
 473                ether_addr_copy(ethh->h_dest, cm_node->rem_mac);
 474                ether_addr_copy(ethh->h_source, cm_node->loc_mac);
 475                if (cm_node->vlan_id < VLAN_TAG_PRESENT) {
 476                        ((struct vlan_ethhdr *)ethh)->h_vlan_proto = htons(ETH_P_8021Q);
 477                        ((struct vlan_ethhdr *)ethh)->h_vlan_TCI = htons(cm_node->vlan_id);
 478                        ((struct vlan_ethhdr *)ethh)->h_vlan_encapsulated_proto = htons(ETH_P_IPV6);
 479                } else {
 480                        ethh->h_proto = htons(ETH_P_IPV6);
 481                }
 482                ip6h->version = 6;
 483                ip6h->flow_lbl[0] = 0;
 484                ip6h->flow_lbl[1] = 0;
 485                ip6h->flow_lbl[2] = 0;
 486                ip6h->payload_len = htons(packetsize - sizeof(*ip6h));
 487                ip6h->nexthdr = 6;
 488                ip6h->hop_limit = 128;
 489                i40iw_copy_ip_htonl(ip6h->saddr.in6_u.u6_addr32,
 490                                    cm_node->loc_addr);
 491                i40iw_copy_ip_htonl(ip6h->daddr.in6_u.u6_addr32,
 492                                    cm_node->rem_addr);
 493        }
 494
 495        tcph->source = htons(cm_node->loc_port);
 496        tcph->dest = htons(cm_node->rem_port);
 497
 498        tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
 499
 500        if (flags & SET_ACK) {
 501                cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
 502                tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
 503                tcph->ack = 1;
 504        } else {
 505                tcph->ack_seq = 0;
 506        }
 507
 508        if (flags & SET_SYN) {
 509                cm_node->tcp_cntxt.loc_seq_num++;
 510                tcph->syn = 1;
 511        } else {
 512                cm_node->tcp_cntxt.loc_seq_num += hdr_len + pd_len;
 513        }
 514
 515        if (flags & SET_FIN) {
 516                cm_node->tcp_cntxt.loc_seq_num++;
 517                tcph->fin = 1;
 518        }
 519
 520        if (flags & SET_RST)
 521                tcph->rst = 1;
 522
 523        tcph->doff = (u16)((sizeof(*tcph) + opts_len + 3) >> 2);
 524        sqbuf->tcphlen = tcph->doff << 2;
 525        tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
 526        tcph->urg_ptr = 0;
 527
 528        if (opts_len) {
 529                memcpy(buf, options->addr, opts_len);
 530                buf += opts_len;
 531        }
 532
 533        if (hdr_len) {
 534                memcpy(buf, hdr->addr, hdr_len);
 535                buf += hdr_len;
 536        }
 537
 538        if (pd_len)
 539                memcpy(buf, pdata->addr, pd_len);
 540
 541        atomic_set(&sqbuf->refcount, 1);
 542
 543        return sqbuf;
 544}
 545
 546/**
 547 * i40iw_send_reset - Send RST packet
 548 * @cm_node: connection's node
 549 */
 550static int i40iw_send_reset(struct i40iw_cm_node *cm_node)
 551{
 552        struct i40iw_puda_buf *sqbuf;
 553        int flags = SET_RST | SET_ACK;
 554
 555        sqbuf = i40iw_form_cm_frame(cm_node, NULL, NULL, NULL, flags);
 556        if (!sqbuf) {
 557                i40iw_pr_err("no sqbuf\n");
 558                return -1;
 559        }
 560
 561        return i40iw_schedule_cm_timer(cm_node, sqbuf, I40IW_TIMER_TYPE_SEND, 0, 1);
 562}
 563
 564/**
 565 * i40iw_active_open_err - send event for active side cm error
 566 * @cm_node: connection's node
 567 * @reset: Flag to send reset or not
 568 */
 569static void i40iw_active_open_err(struct i40iw_cm_node *cm_node, bool reset)
 570{
 571        i40iw_cleanup_retrans_entry(cm_node);
 572        cm_node->cm_core->stats_connect_errs++;
 573        if (reset) {
 574                i40iw_debug(cm_node->dev,
 575                            I40IW_DEBUG_CM,
 576                            "%s cm_node=%p state=%d\n",
 577                            __func__,
 578                            cm_node,
 579                            cm_node->state);
 580                atomic_inc(&cm_node->ref_count);
 581                i40iw_send_reset(cm_node);
 582        }
 583
 584        cm_node->state = I40IW_CM_STATE_CLOSED;
 585        i40iw_create_event(cm_node, I40IW_CM_EVENT_ABORTED);
 586}
 587
 588/**
 589 * i40iw_passive_open_err - handle passive side cm error
 590 * @cm_node: connection's node
 591 * @reset: send reset or just free cm_node
 592 */
 593static void i40iw_passive_open_err(struct i40iw_cm_node *cm_node, bool reset)
 594{
 595        i40iw_cleanup_retrans_entry(cm_node);
 596        cm_node->cm_core->stats_passive_errs++;
 597        cm_node->state = I40IW_CM_STATE_CLOSED;
 598        i40iw_debug(cm_node->dev,
 599                    I40IW_DEBUG_CM,
 600                    "%s cm_node=%p state =%d\n",
 601                    __func__,
 602                    cm_node,
 603                    cm_node->state);
 604        if (reset)
 605                i40iw_send_reset(cm_node);
 606        else
 607                i40iw_rem_ref_cm_node(cm_node);
 608}
 609
 610/**
 611 * i40iw_event_connect_error - to create connect error event
 612 * @event: cm information for connect event
 613 */
 614static void i40iw_event_connect_error(struct i40iw_cm_event *event)
 615{
 616        struct i40iw_qp *iwqp;
 617        struct iw_cm_id *cm_id;
 618
 619        cm_id = event->cm_node->cm_id;
 620        if (!cm_id)
 621                return;
 622
 623        iwqp = cm_id->provider_data;
 624
 625        if (!iwqp || !iwqp->iwdev)
 626                return;
 627
 628        iwqp->cm_id = NULL;
 629        cm_id->provider_data = NULL;
 630        i40iw_send_cm_event(event->cm_node, cm_id,
 631                            IW_CM_EVENT_CONNECT_REPLY,
 632                            -ECONNRESET);
 633        cm_id->rem_ref(cm_id);
 634        i40iw_rem_ref_cm_node(event->cm_node);
 635}
 636
 637/**
 638 * i40iw_process_options
 639 * @cm_node: connection's node
 640 * @optionsloc: point to start of options
 641 * @optionsize: size of all options
 642 * @syn_packet: flag if syn packet
 643 */
 644static int i40iw_process_options(struct i40iw_cm_node *cm_node,
 645                                 u8 *optionsloc,
 646                                 u32 optionsize,
 647                                 u32 syn_packet)
 648{
 649        u32 tmp;
 650        u32 offset = 0;
 651        union all_known_options *all_options;
 652        char got_mss_option = 0;
 653
 654        while (offset < optionsize) {
 655                all_options = (union all_known_options *)(optionsloc + offset);
 656                switch (all_options->as_base.optionnum) {
 657                case OPTION_NUMBER_END:
 658                        offset = optionsize;
 659                        break;
 660                case OPTION_NUMBER_NONE:
 661                        offset += 1;
 662                        continue;
 663                case OPTION_NUMBER_MSS:
 664                        i40iw_debug(cm_node->dev,
 665                                    I40IW_DEBUG_CM,
 666                                    "%s: MSS Length: %d Offset: %d Size: %d\n",
 667                                    __func__,
 668                                    all_options->as_mss.length,
 669                                    offset,
 670                                    optionsize);
 671                        got_mss_option = 1;
 672                        if (all_options->as_mss.length != 4)
 673                                return -1;
 674                        tmp = ntohs(all_options->as_mss.mss);
 675                        if (tmp > 0 && tmp < cm_node->tcp_cntxt.mss)
 676                                cm_node->tcp_cntxt.mss = tmp;
 677                        break;
 678                case OPTION_NUMBER_WINDOW_SCALE:
 679                        cm_node->tcp_cntxt.snd_wscale =
 680                            all_options->as_windowscale.shiftcount;
 681                        break;
 682                default:
 683                        i40iw_debug(cm_node->dev,
 684                                    I40IW_DEBUG_CM,
 685                                    "TCP Option not understood: %x\n",
 686                                    all_options->as_base.optionnum);
 687                        break;
 688                }
 689                offset += all_options->as_base.length;
 690        }
 691        if (!got_mss_option && syn_packet)
 692                cm_node->tcp_cntxt.mss = I40IW_CM_DEFAULT_MSS;
 693        return 0;
 694}
 695
 696/**
 697 * i40iw_handle_tcp_options -
 698 * @cm_node: connection's node
 699 * @tcph: pointer tcp header
 700 * @optionsize: size of options rcvd
 701 * @passive: active or passive flag
 702 */
 703static int i40iw_handle_tcp_options(struct i40iw_cm_node *cm_node,
 704                                    struct tcphdr *tcph,
 705                                    int optionsize,
 706                                    int passive)
 707{
 708        u8 *optionsloc = (u8 *)&tcph[1];
 709
 710        if (optionsize) {
 711                if (i40iw_process_options(cm_node,
 712                                          optionsloc,
 713                                          optionsize,
 714                                          (u32)tcph->syn)) {
 715                        i40iw_debug(cm_node->dev,
 716                                    I40IW_DEBUG_CM,
 717                                    "%s: Node %p, Sending RESET\n",
 718                                    __func__,
 719                                    cm_node);
 720                        if (passive)
 721                                i40iw_passive_open_err(cm_node, true);
 722                        else
 723                                i40iw_active_open_err(cm_node, true);
 724                        return -1;
 725                }
 726        }
 727
 728        cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
 729            cm_node->tcp_cntxt.snd_wscale;
 730
 731        if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
 732                cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
 733        return 0;
 734}
 735
 736/**
 737 * i40iw_build_mpa_v1 - build a MPA V1 frame
 738 * @cm_node: connection's node
 739 * @mpa_key: to do read0 or write0
 740 */
 741static void i40iw_build_mpa_v1(struct i40iw_cm_node *cm_node,
 742                               void *start_addr,
 743                               u8 mpa_key)
 744{
 745        struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
 746
 747        switch (mpa_key) {
 748        case MPA_KEY_REQUEST:
 749                memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
 750                break;
 751        case MPA_KEY_REPLY:
 752                memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
 753                break;
 754        default:
 755                break;
 756        }
 757        mpa_frame->flags = IETF_MPA_FLAGS_CRC;
 758        mpa_frame->rev = cm_node->mpa_frame_rev;
 759        mpa_frame->priv_data_len = htons(cm_node->pdata.size);
 760}
 761
 762/**
 763 * i40iw_build_mpa_v2 - build a MPA V2 frame
 764 * @cm_node: connection's node
 765 * @start_addr: buffer start address
 766 * @mpa_key: to do read0 or write0
 767 */
 768static void i40iw_build_mpa_v2(struct i40iw_cm_node *cm_node,
 769                               void *start_addr,
 770                               u8 mpa_key)
 771{
 772        struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
 773        struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
 774
 775        /* initialize the upper 5 bytes of the frame */
 776        i40iw_build_mpa_v1(cm_node, start_addr, mpa_key);
 777        mpa_frame->flags |= IETF_MPA_V2_FLAG;
 778        mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
 779
 780        /* initialize RTR msg */
 781        if (cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
 782                rtr_msg->ctrl_ird = IETF_NO_IRD_ORD;
 783                rtr_msg->ctrl_ord = IETF_NO_IRD_ORD;
 784        } else {
 785                rtr_msg->ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
 786                        IETF_NO_IRD_ORD : cm_node->ird_size;
 787                rtr_msg->ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
 788                        IETF_NO_IRD_ORD : cm_node->ord_size;
 789        }
 790
 791        rtr_msg->ctrl_ird |= IETF_PEER_TO_PEER;
 792        rtr_msg->ctrl_ird |= IETF_FLPDU_ZERO_LEN;
 793
 794        switch (mpa_key) {
 795        case MPA_KEY_REQUEST:
 796                rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
 797                rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
 798                break;
 799        case MPA_KEY_REPLY:
 800                switch (cm_node->send_rdma0_op) {
 801                case SEND_RDMA_WRITE_ZERO:
 802                        rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
 803                        break;
 804                case SEND_RDMA_READ_ZERO:
 805                        rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
 806                        break;
 807                }
 808                break;
 809        default:
 810                break;
 811        }
 812        rtr_msg->ctrl_ird = htons(rtr_msg->ctrl_ird);
 813        rtr_msg->ctrl_ord = htons(rtr_msg->ctrl_ord);
 814}
 815
 816/**
 817 * i40iw_cm_build_mpa_frame - build mpa frame for mpa version 1 or version 2
 818 * @cm_node: connection's node
 819 * @mpa: mpa: data buffer
 820 * @mpa_key: to do read0 or write0
 821 */
 822static int i40iw_cm_build_mpa_frame(struct i40iw_cm_node *cm_node,
 823                                    struct i40iw_kmem_info *mpa,
 824                                    u8 mpa_key)
 825{
 826        int hdr_len = 0;
 827
 828        switch (cm_node->mpa_frame_rev) {
 829        case IETF_MPA_V1:
 830                hdr_len = sizeof(struct ietf_mpa_v1);
 831                i40iw_build_mpa_v1(cm_node, mpa->addr, mpa_key);
 832                break;
 833        case IETF_MPA_V2:
 834                hdr_len = sizeof(struct ietf_mpa_v2);
 835                i40iw_build_mpa_v2(cm_node, mpa->addr, mpa_key);
 836                break;
 837        default:
 838                break;
 839        }
 840
 841        return hdr_len;
 842}
 843
 844/**
 845 * i40iw_send_mpa_request - active node send mpa request to passive node
 846 * @cm_node: connection's node
 847 */
 848static int i40iw_send_mpa_request(struct i40iw_cm_node *cm_node)
 849{
 850        struct i40iw_puda_buf *sqbuf;
 851
 852        if (!cm_node) {
 853                i40iw_pr_err("cm_node == NULL\n");
 854                return -1;
 855        }
 856
 857        cm_node->mpa_hdr.addr = &cm_node->mpa_frame;
 858        cm_node->mpa_hdr.size = i40iw_cm_build_mpa_frame(cm_node,
 859                                                         &cm_node->mpa_hdr,
 860                                                         MPA_KEY_REQUEST);
 861        if (!cm_node->mpa_hdr.size) {
 862                i40iw_pr_err("mpa size = %d\n", cm_node->mpa_hdr.size);
 863                return -1;
 864        }
 865
 866        sqbuf = i40iw_form_cm_frame(cm_node,
 867                                    NULL,
 868                                    &cm_node->mpa_hdr,
 869                                    &cm_node->pdata,
 870                                    SET_ACK);
 871        if (!sqbuf) {
 872                i40iw_pr_err("sq_buf == NULL\n");
 873                return -1;
 874        }
 875        return i40iw_schedule_cm_timer(cm_node, sqbuf, I40IW_TIMER_TYPE_SEND, 1, 0);
 876}
 877
 878/**
 879 * i40iw_send_mpa_reject -
 880 * @cm_node: connection's node
 881 * @pdata: reject data for connection
 882 * @plen: length of reject data
 883 */
 884static int i40iw_send_mpa_reject(struct i40iw_cm_node *cm_node,
 885                                 const void *pdata,
 886                                 u8 plen)
 887{
 888        struct i40iw_puda_buf *sqbuf;
 889        struct i40iw_kmem_info priv_info;
 890
 891        cm_node->mpa_hdr.addr = &cm_node->mpa_frame;
 892        cm_node->mpa_hdr.size = i40iw_cm_build_mpa_frame(cm_node,
 893                                                         &cm_node->mpa_hdr,
 894                                                         MPA_KEY_REPLY);
 895
 896        cm_node->mpa_frame.flags |= IETF_MPA_FLAGS_REJECT;
 897        priv_info.addr = (void *)pdata;
 898        priv_info.size = plen;
 899
 900        sqbuf = i40iw_form_cm_frame(cm_node,
 901                                    NULL,
 902                                    &cm_node->mpa_hdr,
 903                                    &priv_info,
 904                                    SET_ACK | SET_FIN);
 905        if (!sqbuf) {
 906                i40iw_pr_err("no sqbuf\n");
 907                return -ENOMEM;
 908        }
 909        cm_node->state = I40IW_CM_STATE_FIN_WAIT1;
 910        return i40iw_schedule_cm_timer(cm_node, sqbuf, I40IW_TIMER_TYPE_SEND, 1, 0);
 911}
 912
 913/**
 914 * recv_mpa - process an IETF MPA frame
 915 * @cm_node: connection's node
 916 * @buffer: Data pointer
 917 * @type: to return accept or reject
 918 * @len: Len of mpa buffer
 919 */
 920static int i40iw_parse_mpa(struct i40iw_cm_node *cm_node, u8 *buffer, u32 *type, u32 len)
 921{
 922        struct ietf_mpa_v1 *mpa_frame;
 923        struct ietf_mpa_v2 *mpa_v2_frame;
 924        struct ietf_rtr_msg *rtr_msg;
 925        int mpa_hdr_len;
 926        int priv_data_len;
 927
 928        *type = I40IW_MPA_REQUEST_ACCEPT;
 929
 930        if (len < sizeof(struct ietf_mpa_v1)) {
 931                i40iw_pr_err("ietf buffer small (%x)\n", len);
 932                return -1;
 933        }
 934
 935        mpa_frame = (struct ietf_mpa_v1 *)buffer;
 936        mpa_hdr_len = sizeof(struct ietf_mpa_v1);
 937        priv_data_len = ntohs(mpa_frame->priv_data_len);
 938
 939        if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
 940                i40iw_pr_err("large pri_data %d\n", priv_data_len);
 941                return -1;
 942        }
 943        if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
 944                i40iw_pr_err("unsupported mpa rev = %d\n", mpa_frame->rev);
 945                return -1;
 946        }
 947        if (mpa_frame->rev > cm_node->mpa_frame_rev) {
 948                i40iw_pr_err("rev %d\n", mpa_frame->rev);
 949                return -1;
 950        }
 951        cm_node->mpa_frame_rev = mpa_frame->rev;
 952
 953        if (cm_node->state != I40IW_CM_STATE_MPAREQ_SENT) {
 954                if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
 955                        i40iw_pr_err("Unexpected MPA Key received\n");
 956                        return -1;
 957                }
 958        } else {
 959                if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
 960                        i40iw_pr_err("Unexpected MPA Key received\n");
 961                        return -1;
 962                }
 963        }
 964
 965        if (priv_data_len + mpa_hdr_len > len) {
 966                i40iw_pr_err("ietf buffer len(%x + %x != %x)\n",
 967                             priv_data_len, mpa_hdr_len, len);
 968                return -1;
 969        }
 970        if (len > MAX_CM_BUFFER) {
 971                i40iw_pr_err("ietf buffer large len = %d\n", len);
 972                return -1;
 973        }
 974
 975        switch (mpa_frame->rev) {
 976        case IETF_MPA_V2:{
 977                        u16 ird_size;
 978                        u16 ord_size;
 979                        u16 ctrl_ord;
 980                        u16 ctrl_ird;
 981
 982                        mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
 983                        mpa_hdr_len += IETF_RTR_MSG_SIZE;
 984                        rtr_msg = &mpa_v2_frame->rtr_msg;
 985
 986                        /* parse rtr message */
 987                        ctrl_ord = ntohs(rtr_msg->ctrl_ord);
 988                        ctrl_ird = ntohs(rtr_msg->ctrl_ird);
 989                        ird_size = ctrl_ird & IETF_NO_IRD_ORD;
 990                        ord_size = ctrl_ord & IETF_NO_IRD_ORD;
 991
 992                        if (!(ctrl_ird & IETF_PEER_TO_PEER))
 993                                return -1;
 994
 995                        if (ird_size == IETF_NO_IRD_ORD || ord_size == IETF_NO_IRD_ORD) {
 996                                cm_node->mpav2_ird_ord = IETF_NO_IRD_ORD;
 997                                goto negotiate_done;
 998                        }
 999
1000                        if (cm_node->state != I40IW_CM_STATE_MPAREQ_SENT) {
1001                                /* responder */
1002                                if (!ord_size && (ctrl_ord & IETF_RDMA0_READ))
1003                                        cm_node->ird_size = 1;
1004                                if (cm_node->ord_size > ird_size)
1005                                        cm_node->ord_size = ird_size;
1006                        } else {
1007                                /* initiator */
1008                                if (!ird_size && (ctrl_ord & IETF_RDMA0_READ))
1009                                        return -1;
1010                                if (cm_node->ord_size > ird_size)
1011                                        cm_node->ord_size = ird_size;
1012
1013                                if (cm_node->ird_size < ord_size)
1014                                        /* no resources available */
1015                                        return -1;
1016                        }
1017
1018negotiate_done:
1019                        if (ctrl_ord & IETF_RDMA0_READ)
1020                                cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1021                        else if (ctrl_ord & IETF_RDMA0_WRITE)
1022                                cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
1023                        else    /* Not supported RDMA0 operation */
1024                                return -1;
1025                        i40iw_debug(cm_node->dev, I40IW_DEBUG_CM,
1026                                    "MPAV2: Negotiated ORD: %d, IRD: %d\n",
1027                                    cm_node->ord_size, cm_node->ird_size);
1028                        break;
1029                }
1030                break;
1031        case IETF_MPA_V1:
1032        default:
1033                break;
1034        }
1035
1036        memcpy(cm_node->pdata_buf, buffer + mpa_hdr_len, priv_data_len);
1037        cm_node->pdata.size = priv_data_len;
1038
1039        if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
1040                *type = I40IW_MPA_REQUEST_REJECT;
1041
1042        if (mpa_frame->flags & IETF_MPA_FLAGS_MARKERS)
1043                cm_node->snd_mark_en = true;
1044
1045        return 0;
1046}
1047
1048/**
1049 * i40iw_schedule_cm_timer
1050 * @@cm_node: connection's node
1051 * @sqbuf: buffer to send
1052 * @type: if it es send ot close
1053 * @send_retrans: if rexmits to be done
1054 * @close_when_complete: is cm_node to be removed
1055 *
1056 * note - cm_node needs to be protected before calling this. Encase in:
1057 *              i40iw_rem_ref_cm_node(cm_core, cm_node);
1058 *              i40iw_schedule_cm_timer(...)
1059 *              atomic_inc(&cm_node->ref_count);
1060 */
1061int i40iw_schedule_cm_timer(struct i40iw_cm_node *cm_node,
1062                            struct i40iw_puda_buf *sqbuf,
1063                            enum i40iw_timer_type type,
1064                            int send_retrans,
1065                            int close_when_complete)
1066{
1067        struct i40iw_sc_dev *dev = cm_node->dev;
1068        struct i40iw_cm_core *cm_core = cm_node->cm_core;
1069        struct i40iw_timer_entry *new_send;
1070        int ret = 0;
1071        u32 was_timer_set;
1072        unsigned long flags;
1073
1074        new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
1075        if (!new_send) {
1076                i40iw_free_sqbuf(cm_node->dev, (void *)sqbuf);
1077                return -ENOMEM;
1078        }
1079        new_send->retrycount = I40IW_DEFAULT_RETRYS;
1080        new_send->retranscount = I40IW_DEFAULT_RETRANS;
1081        new_send->sqbuf = sqbuf;
1082        new_send->timetosend = jiffies;
1083        new_send->type = type;
1084        new_send->send_retrans = send_retrans;
1085        new_send->close_when_complete = close_when_complete;
1086
1087        if (type == I40IW_TIMER_TYPE_CLOSE) {
1088                new_send->timetosend += (HZ / 10);
1089                if (cm_node->close_entry) {
1090                        kfree(new_send);
1091                        i40iw_free_sqbuf(cm_node->dev, (void *)sqbuf);
1092                        i40iw_pr_err("already close entry\n");
1093                        return -EINVAL;
1094                }
1095                cm_node->close_entry = new_send;
1096        }
1097
1098        if (type == I40IW_TIMER_TYPE_SEND) {
1099                spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1100                cm_node->send_entry = new_send;
1101                atomic_inc(&cm_node->ref_count);
1102                spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1103                new_send->timetosend = jiffies + I40IW_RETRY_TIMEOUT;
1104
1105                atomic_inc(&sqbuf->refcount);
1106                i40iw_puda_send_buf(dev->ilq, sqbuf);
1107                if (!send_retrans) {
1108                        i40iw_cleanup_retrans_entry(cm_node);
1109                        if (close_when_complete)
1110                                i40iw_rem_ref_cm_node(cm_node);
1111                        return ret;
1112                }
1113        }
1114
1115        spin_lock_irqsave(&cm_core->ht_lock, flags);
1116        was_timer_set = timer_pending(&cm_core->tcp_timer);
1117
1118        if (!was_timer_set) {
1119                cm_core->tcp_timer.expires = new_send->timetosend;
1120                add_timer(&cm_core->tcp_timer);
1121        }
1122        spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1123
1124        return ret;
1125}
1126
1127/**
1128 * i40iw_retrans_expired - Could not rexmit the packet
1129 * @cm_node: connection's node
1130 */
1131static void i40iw_retrans_expired(struct i40iw_cm_node *cm_node)
1132{
1133        struct iw_cm_id *cm_id = cm_node->cm_id;
1134        enum i40iw_cm_node_state state = cm_node->state;
1135
1136        cm_node->state = I40IW_CM_STATE_CLOSED;
1137        switch (state) {
1138        case I40IW_CM_STATE_SYN_RCVD:
1139        case I40IW_CM_STATE_CLOSING:
1140                i40iw_rem_ref_cm_node(cm_node);
1141                break;
1142        case I40IW_CM_STATE_FIN_WAIT1:
1143        case I40IW_CM_STATE_LAST_ACK:
1144                if (cm_node->cm_id)
1145                        cm_id->rem_ref(cm_id);
1146                i40iw_send_reset(cm_node);
1147                break;
1148        default:
1149                atomic_inc(&cm_node->ref_count);
1150                i40iw_send_reset(cm_node);
1151                i40iw_create_event(cm_node, I40IW_CM_EVENT_ABORTED);
1152                break;
1153        }
1154}
1155
1156/**
1157 * i40iw_handle_close_entry - for handling retry/timeouts
1158 * @cm_node: connection's node
1159 * @rem_node: flag for remove cm_node
1160 */
1161static void i40iw_handle_close_entry(struct i40iw_cm_node *cm_node, u32 rem_node)
1162{
1163        struct i40iw_timer_entry *close_entry = cm_node->close_entry;
1164        struct iw_cm_id *cm_id = cm_node->cm_id;
1165        struct i40iw_qp *iwqp;
1166        unsigned long flags;
1167
1168        if (!close_entry)
1169                return;
1170        iwqp = (struct i40iw_qp *)close_entry->sqbuf;
1171        if (iwqp) {
1172                spin_lock_irqsave(&iwqp->lock, flags);
1173                if (iwqp->cm_id) {
1174                        iwqp->hw_tcp_state = I40IW_TCP_STATE_CLOSED;
1175                        iwqp->hw_iwarp_state = I40IW_QP_STATE_ERROR;
1176                        iwqp->last_aeq = I40IW_AE_RESET_SENT;
1177                        iwqp->ibqp_state = IB_QPS_ERR;
1178                        spin_unlock_irqrestore(&iwqp->lock, flags);
1179                        i40iw_cm_disconn(iwqp);
1180                } else {
1181                        spin_unlock_irqrestore(&iwqp->lock, flags);
1182                }
1183        } else if (rem_node) {
1184                /* TIME_WAIT state */
1185                i40iw_rem_ref_cm_node(cm_node);
1186        }
1187        if (cm_id)
1188                cm_id->rem_ref(cm_id);
1189        kfree(close_entry);
1190        cm_node->close_entry = NULL;
1191}
1192
1193/**
1194 * i40iw_cm_timer_tick - system's timer expired callback
1195 * @pass: Pointing to cm_core
1196 */
1197static void i40iw_cm_timer_tick(unsigned long pass)
1198{
1199        unsigned long nexttimeout = jiffies + I40IW_LONG_TIME;
1200        struct i40iw_cm_node *cm_node;
1201        struct i40iw_timer_entry *send_entry, *close_entry;
1202        struct list_head *list_core_temp;
1203        struct list_head *list_node;
1204        struct i40iw_cm_core *cm_core = (struct i40iw_cm_core *)pass;
1205        u32 settimer = 0;
1206        unsigned long timetosend;
1207        struct i40iw_sc_dev *dev;
1208        unsigned long flags;
1209
1210        struct list_head timer_list;
1211
1212        INIT_LIST_HEAD(&timer_list);
1213        spin_lock_irqsave(&cm_core->ht_lock, flags);
1214
1215        list_for_each_safe(list_node, list_core_temp, &cm_core->connected_nodes) {
1216                cm_node = container_of(list_node, struct i40iw_cm_node, list);
1217                if (cm_node->close_entry || cm_node->send_entry) {
1218                        atomic_inc(&cm_node->ref_count);
1219                        list_add(&cm_node->timer_entry, &timer_list);
1220                }
1221        }
1222        spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1223
1224        list_for_each_safe(list_node, list_core_temp, &timer_list) {
1225                cm_node = container_of(list_node,
1226                                       struct i40iw_cm_node,
1227                                       timer_entry);
1228                close_entry = cm_node->close_entry;
1229
1230                if (close_entry) {
1231                        if (time_after(close_entry->timetosend, jiffies)) {
1232                                if (nexttimeout > close_entry->timetosend ||
1233                                    !settimer) {
1234                                        nexttimeout = close_entry->timetosend;
1235                                        settimer = 1;
1236                                }
1237                        } else {
1238                                i40iw_handle_close_entry(cm_node, 1);
1239                        }
1240                }
1241
1242                spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1243
1244                send_entry = cm_node->send_entry;
1245                if (!send_entry)
1246                        goto done;
1247                if (time_after(send_entry->timetosend, jiffies)) {
1248                        if (cm_node->state != I40IW_CM_STATE_OFFLOADED) {
1249                                if ((nexttimeout > send_entry->timetosend) ||
1250                                    !settimer) {
1251                                        nexttimeout = send_entry->timetosend;
1252                                        settimer = 1;
1253                                }
1254                        } else {
1255                                i40iw_free_retrans_entry(cm_node);
1256                        }
1257                        goto done;
1258                }
1259
1260                if ((cm_node->state == I40IW_CM_STATE_OFFLOADED) ||
1261                    (cm_node->state == I40IW_CM_STATE_CLOSED)) {
1262                        i40iw_free_retrans_entry(cm_node);
1263                        goto done;
1264                }
1265
1266                if (!send_entry->retranscount || !send_entry->retrycount) {
1267                        i40iw_free_retrans_entry(cm_node);
1268
1269                        spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1270                        i40iw_retrans_expired(cm_node);
1271                        cm_node->state = I40IW_CM_STATE_CLOSED;
1272                        spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1273                        goto done;
1274                }
1275                cm_node->cm_core->stats_pkt_retrans++;
1276                spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1277
1278                dev = cm_node->dev;
1279                atomic_inc(&send_entry->sqbuf->refcount);
1280                i40iw_puda_send_buf(dev->ilq, send_entry->sqbuf);
1281                spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1282                if (send_entry->send_retrans) {
1283                        send_entry->retranscount--;
1284                        timetosend = (I40IW_RETRY_TIMEOUT <<
1285                                      (I40IW_DEFAULT_RETRANS -
1286                                       send_entry->retranscount));
1287
1288                        send_entry->timetosend = jiffies +
1289                            min(timetosend, I40IW_MAX_TIMEOUT);
1290                        if (nexttimeout > send_entry->timetosend || !settimer) {
1291                                nexttimeout = send_entry->timetosend;
1292                                settimer = 1;
1293                        }
1294                } else {
1295                        int close_when_complete;
1296
1297                        close_when_complete = send_entry->close_when_complete;
1298                        i40iw_debug(cm_node->dev,
1299                                    I40IW_DEBUG_CM,
1300                                    "cm_node=%p state=%d\n",
1301                                    cm_node,
1302                                    cm_node->state);
1303                        i40iw_free_retrans_entry(cm_node);
1304                        if (close_when_complete)
1305                                i40iw_rem_ref_cm_node(cm_node);
1306                }
1307done:
1308                spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1309                i40iw_rem_ref_cm_node(cm_node);
1310        }
1311
1312        if (settimer) {
1313                spin_lock_irqsave(&cm_core->ht_lock, flags);
1314                if (!timer_pending(&cm_core->tcp_timer)) {
1315                        cm_core->tcp_timer.expires = nexttimeout;
1316                        add_timer(&cm_core->tcp_timer);
1317                }
1318                spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1319        }
1320}
1321
1322/**
1323 * i40iw_send_syn - send SYN packet
1324 * @cm_node: connection's node
1325 * @sendack: flag to set ACK bit or not
1326 */
1327int i40iw_send_syn(struct i40iw_cm_node *cm_node, u32 sendack)
1328{
1329        struct i40iw_puda_buf *sqbuf;
1330        int flags = SET_SYN;
1331        char optionsbuffer[sizeof(struct option_mss) +
1332                           sizeof(struct option_windowscale) +
1333                           sizeof(struct option_base) + TCP_OPTIONS_PADDING];
1334        struct i40iw_kmem_info opts;
1335
1336        int optionssize = 0;
1337        /* Sending MSS option */
1338        union all_known_options *options;
1339
1340        opts.addr = optionsbuffer;
1341        if (!cm_node) {
1342                i40iw_pr_err("no cm_node\n");
1343                return -EINVAL;
1344        }
1345
1346        options = (union all_known_options *)&optionsbuffer[optionssize];
1347        options->as_mss.optionnum = OPTION_NUMBER_MSS;
1348        options->as_mss.length = sizeof(struct option_mss);
1349        options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
1350        optionssize += sizeof(struct option_mss);
1351
1352        options = (union all_known_options *)&optionsbuffer[optionssize];
1353        options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
1354        options->as_windowscale.length = sizeof(struct option_windowscale);
1355        options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1356        optionssize += sizeof(struct option_windowscale);
1357        options = (union all_known_options *)&optionsbuffer[optionssize];
1358        options->as_end = OPTION_NUMBER_END;
1359        optionssize += 1;
1360
1361        if (sendack)
1362                flags |= SET_ACK;
1363
1364        opts.size = optionssize;
1365
1366        sqbuf = i40iw_form_cm_frame(cm_node, &opts, NULL, NULL, flags);
1367        if (!sqbuf) {
1368                i40iw_pr_err("no sqbuf\n");
1369                return -1;
1370        }
1371        return i40iw_schedule_cm_timer(cm_node, sqbuf, I40IW_TIMER_TYPE_SEND, 1, 0);
1372}
1373
1374/**
1375 * i40iw_send_ack - Send ACK packet
1376 * @cm_node: connection's node
1377 */
1378static void i40iw_send_ack(struct i40iw_cm_node *cm_node)
1379{
1380        struct i40iw_puda_buf *sqbuf;
1381
1382        sqbuf = i40iw_form_cm_frame(cm_node, NULL, NULL, NULL, SET_ACK);
1383        if (sqbuf)
1384                i40iw_puda_send_buf(cm_node->dev->ilq, sqbuf);
1385        else
1386                i40iw_pr_err("no sqbuf\n");
1387}
1388
1389/**
1390 * i40iw_send_fin - Send FIN pkt
1391 * @cm_node: connection's node
1392 */
1393static int i40iw_send_fin(struct i40iw_cm_node *cm_node)
1394{
1395        struct i40iw_puda_buf *sqbuf;
1396
1397        sqbuf = i40iw_form_cm_frame(cm_node, NULL, NULL, NULL, SET_ACK | SET_FIN);
1398        if (!sqbuf) {
1399                i40iw_pr_err("no sqbuf\n");
1400                return -1;
1401        }
1402        return i40iw_schedule_cm_timer(cm_node, sqbuf, I40IW_TIMER_TYPE_SEND, 1, 0);
1403}
1404
1405/**
1406 * i40iw_find_node - find a cm node that matches the reference cm node
1407 * @cm_core: cm's core
1408 * @rem_port: remote tcp port num
1409 * @rem_addr: remote ip addr
1410 * @loc_port: local tcp port num
1411 * @loc_addr: loc ip addr
1412 * @add_refcnt: flag to increment refcount of cm_node
1413 */
1414struct i40iw_cm_node *i40iw_find_node(struct i40iw_cm_core *cm_core,
1415                                      u16 rem_port,
1416                                      u32 *rem_addr,
1417                                      u16 loc_port,
1418                                      u32 *loc_addr,
1419                                      bool add_refcnt)
1420{
1421        struct list_head *hte;
1422        struct i40iw_cm_node *cm_node;
1423        unsigned long flags;
1424
1425        hte = &cm_core->connected_nodes;
1426
1427        /* walk list and find cm_node associated with this session ID */
1428        spin_lock_irqsave(&cm_core->ht_lock, flags);
1429        list_for_each_entry(cm_node, hte, list) {
1430                if (!memcmp(cm_node->loc_addr, loc_addr, sizeof(cm_node->loc_addr)) &&
1431                    (cm_node->loc_port == loc_port) &&
1432                    !memcmp(cm_node->rem_addr, rem_addr, sizeof(cm_node->rem_addr)) &&
1433                    (cm_node->rem_port == rem_port)) {
1434                        if (add_refcnt)
1435                                atomic_inc(&cm_node->ref_count);
1436                        spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1437                        return cm_node;
1438                }
1439        }
1440        spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1441
1442        /* no owner node */
1443        return NULL;
1444}
1445
1446/**
1447 * i40iw_find_listener - find a cm node listening on this addr-port pair
1448 * @cm_core: cm's core
1449 * @dst_port: listener tcp port num
1450 * @dst_addr: listener ip addr
1451 * @listener_state: state to match with listen node's
1452 */
1453static struct i40iw_cm_listener *i40iw_find_listener(
1454                                                     struct i40iw_cm_core *cm_core,
1455                                                     u32 *dst_addr,
1456                                                     u16 dst_port,
1457                                                     u16 vlan_id,
1458                                                     enum i40iw_cm_listener_state
1459                                                     listener_state)
1460{
1461        struct i40iw_cm_listener *listen_node;
1462        static const u32 ip_zero[4] = { 0, 0, 0, 0 };
1463        u32 listen_addr[4];
1464        u16 listen_port;
1465        unsigned long flags;
1466
1467        /* walk list and find cm_node associated with this session ID */
1468        spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1469        list_for_each_entry(listen_node, &cm_core->listen_nodes, list) {
1470                memcpy(listen_addr, listen_node->loc_addr, sizeof(listen_addr));
1471                listen_port = listen_node->loc_port;
1472                /* compare node pair, return node handle if a match */
1473                if ((!memcmp(listen_addr, dst_addr, sizeof(listen_addr)) ||
1474                     !memcmp(listen_addr, ip_zero, sizeof(listen_addr))) &&
1475                     (listen_port == dst_port) &&
1476                     (listener_state & listen_node->listener_state)) {
1477                        atomic_inc(&listen_node->ref_count);
1478                        spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1479                        return listen_node;
1480                }
1481        }
1482        spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1483        return NULL;
1484}
1485
1486/**
1487 * i40iw_add_hte_node - add a cm node to the hash table
1488 * @cm_core: cm's core
1489 * @cm_node: connection's node
1490 */
1491static void i40iw_add_hte_node(struct i40iw_cm_core *cm_core,
1492                               struct i40iw_cm_node *cm_node)
1493{
1494        struct list_head *hte;
1495        unsigned long flags;
1496
1497        if (!cm_node || !cm_core) {
1498                i40iw_pr_err("cm_node or cm_core == NULL\n");
1499                return;
1500        }
1501        spin_lock_irqsave(&cm_core->ht_lock, flags);
1502
1503        /* get a handle on the hash table element (list head for this slot) */
1504        hte = &cm_core->connected_nodes;
1505        list_add_tail(&cm_node->list, hte);
1506        spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1507}
1508
1509/**
1510 * listen_port_in_use - determine if port is in use
1511 * @port: Listen port number
1512 */
1513static bool i40iw_listen_port_in_use(struct i40iw_cm_core *cm_core, u16 port)
1514{
1515        struct i40iw_cm_listener *listen_node;
1516        unsigned long flags;
1517        bool ret = false;
1518
1519        spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1520        list_for_each_entry(listen_node, &cm_core->listen_nodes, list) {
1521                if (listen_node->loc_port == port) {
1522                        ret = true;
1523                        break;
1524                }
1525        }
1526        spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1527        return ret;
1528}
1529
1530/**
1531 * i40iw_del_multiple_qhash - Remove qhash and child listens
1532 * @iwdev: iWarp device
1533 * @cm_info: CM info for parent listen node
1534 * @cm_parent_listen_node: The parent listen node
1535 */
1536static enum i40iw_status_code i40iw_del_multiple_qhash(
1537                                                       struct i40iw_device *iwdev,
1538                                                       struct i40iw_cm_info *cm_info,
1539                                                       struct i40iw_cm_listener *cm_parent_listen_node)
1540{
1541        struct i40iw_cm_listener *child_listen_node;
1542        enum i40iw_status_code ret = I40IW_ERR_CONFIG;
1543        struct list_head *pos, *tpos;
1544        unsigned long flags;
1545
1546        spin_lock_irqsave(&iwdev->cm_core.listen_list_lock, flags);
1547        list_for_each_safe(pos, tpos, &cm_parent_listen_node->child_listen_list) {
1548                child_listen_node = list_entry(pos, struct i40iw_cm_listener, child_listen_list);
1549                if (child_listen_node->ipv4)
1550                        i40iw_debug(&iwdev->sc_dev,
1551                                    I40IW_DEBUG_CM,
1552                                    "removing child listen for IP=%pI4, port=%d, vlan=%d\n",
1553                                    child_listen_node->loc_addr,
1554                                    child_listen_node->loc_port,
1555                                    child_listen_node->vlan_id);
1556                else
1557                        i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_CM,
1558                                    "removing child listen for IP=%pI6, port=%d, vlan=%d\n",
1559                                    child_listen_node->loc_addr,
1560                                    child_listen_node->loc_port,
1561                                    child_listen_node->vlan_id);
1562                list_del(pos);
1563                memcpy(cm_info->loc_addr, child_listen_node->loc_addr,
1564                       sizeof(cm_info->loc_addr));
1565                cm_info->vlan_id = child_listen_node->vlan_id;
1566                ret = i40iw_manage_qhash(iwdev, cm_info,
1567                                         I40IW_QHASH_TYPE_TCP_SYN,
1568                                         I40IW_QHASH_MANAGE_TYPE_DELETE, NULL, false);
1569                kfree(child_listen_node);
1570                cm_parent_listen_node->cm_core->stats_listen_nodes_destroyed++;
1571                i40iw_debug(&iwdev->sc_dev,
1572                            I40IW_DEBUG_CM,
1573                            "freed pointer = %p\n",
1574                            child_listen_node);
1575        }
1576        spin_unlock_irqrestore(&iwdev->cm_core.listen_list_lock, flags);
1577
1578        return ret;
1579}
1580
1581/**
1582 * i40iw_netdev_vlan_ipv6 - Gets the netdev and mac
1583 * @addr: local IPv6 address
1584 * @vlan_id: vlan id for the given IPv6 address
1585 * @mac: mac address for the given IPv6 address
1586 *
1587 * Returns the net_device of the IPv6 address and also sets the
1588 * vlan id and mac for that address.
1589 */
1590static struct net_device *i40iw_netdev_vlan_ipv6(u32 *addr, u16 *vlan_id, u8 *mac)
1591{
1592        struct net_device *ip_dev = NULL;
1593#if IS_ENABLED(CONFIG_IPV6)
1594        struct in6_addr laddr6;
1595
1596        i40iw_copy_ip_htonl(laddr6.in6_u.u6_addr32, addr);
1597        if (vlan_id)
1598                *vlan_id = I40IW_NO_VLAN;
1599        if (mac)
1600                eth_zero_addr(mac);
1601        rcu_read_lock();
1602        for_each_netdev_rcu(&init_net, ip_dev) {
1603                if (ipv6_chk_addr(&init_net, &laddr6, ip_dev, 1)) {
1604                        if (vlan_id)
1605                                *vlan_id = rdma_vlan_dev_vlan_id(ip_dev);
1606                        if (ip_dev->dev_addr && mac)
1607                                ether_addr_copy(mac, ip_dev->dev_addr);
1608                        break;
1609                }
1610        }
1611        rcu_read_unlock();
1612#endif
1613        return ip_dev;
1614}
1615
1616/**
1617 * i40iw_get_vlan_ipv4 - Returns the vlan_id for IPv4 address
1618 * @addr: local IPv4 address
1619 */
1620static u16 i40iw_get_vlan_ipv4(u32 *addr)
1621{
1622        struct net_device *netdev;
1623        u16 vlan_id = I40IW_NO_VLAN;
1624
1625        netdev = ip_dev_find(&init_net, htonl(addr[0]));
1626        if (netdev) {
1627                vlan_id = rdma_vlan_dev_vlan_id(netdev);
1628                dev_put(netdev);
1629        }
1630        return vlan_id;
1631}
1632
1633/**
1634 * i40iw_add_mqh_6 - Adds multiple qhashes for IPv6
1635 * @iwdev: iWarp device
1636 * @cm_info: CM info for parent listen node
1637 * @cm_parent_listen_node: The parent listen node
1638 *
1639 * Adds a qhash and a child listen node for every IPv6 address
1640 * on the adapter and adds the associated qhash filter
1641 */
1642static enum i40iw_status_code i40iw_add_mqh_6(struct i40iw_device *iwdev,
1643                                              struct i40iw_cm_info *cm_info,
1644                                              struct i40iw_cm_listener *cm_parent_listen_node)
1645{
1646        struct net_device *ip_dev;
1647        struct inet6_dev *idev;
1648        struct inet6_ifaddr *ifp;
1649        enum i40iw_status_code ret = 0;
1650        struct i40iw_cm_listener *child_listen_node;
1651        unsigned long flags;
1652
1653        rtnl_lock();
1654        for_each_netdev_rcu(&init_net, ip_dev) {
1655                if ((((rdma_vlan_dev_vlan_id(ip_dev) < I40IW_NO_VLAN) &&
1656                      (rdma_vlan_dev_real_dev(ip_dev) == iwdev->netdev)) ||
1657                     (ip_dev == iwdev->netdev)) && (ip_dev->flags & IFF_UP)) {
1658                        idev = __in6_dev_get(ip_dev);
1659                        if (!idev) {
1660                                i40iw_pr_err("idev == NULL\n");
1661                                break;
1662                        }
1663                        list_for_each_entry(ifp, &idev->addr_list, if_list) {
1664                                i40iw_debug(&iwdev->sc_dev,
1665                                            I40IW_DEBUG_CM,
1666                                            "IP=%pI6, vlan_id=%d, MAC=%pM\n",
1667                                            &ifp->addr,
1668                                            rdma_vlan_dev_vlan_id(ip_dev),
1669                                            ip_dev->dev_addr);
1670                                child_listen_node =
1671                                        kzalloc(sizeof(*child_listen_node), GFP_ATOMIC);
1672                                i40iw_debug(&iwdev->sc_dev,
1673                                            I40IW_DEBUG_CM,
1674                                            "Allocating child listener %p\n",
1675                                            child_listen_node);
1676                                if (!child_listen_node) {
1677                                        i40iw_pr_err("listener memory allocation\n");
1678                                        ret = I40IW_ERR_NO_MEMORY;
1679                                        goto exit;
1680                                }
1681                                cm_info->vlan_id = rdma_vlan_dev_vlan_id(ip_dev);
1682                                cm_parent_listen_node->vlan_id = cm_info->vlan_id;
1683
1684                                memcpy(child_listen_node, cm_parent_listen_node,
1685                                       sizeof(*child_listen_node));
1686
1687                                i40iw_copy_ip_ntohl(child_listen_node->loc_addr,
1688                                                    ifp->addr.in6_u.u6_addr32);
1689                                memcpy(cm_info->loc_addr, child_listen_node->loc_addr,
1690                                       sizeof(cm_info->loc_addr));
1691
1692                                ret = i40iw_manage_qhash(iwdev, cm_info,
1693                                                         I40IW_QHASH_TYPE_TCP_SYN,
1694                                                         I40IW_QHASH_MANAGE_TYPE_ADD,
1695                                                         NULL, true);
1696                                if (!ret) {
1697                                        spin_lock_irqsave(&iwdev->cm_core.listen_list_lock, flags);
1698                                        list_add(&child_listen_node->child_listen_list,
1699                                                 &cm_parent_listen_node->child_listen_list);
1700                                        spin_unlock_irqrestore(&iwdev->cm_core.listen_list_lock, flags);
1701                                        cm_parent_listen_node->cm_core->stats_listen_nodes_created++;
1702                                } else {
1703                                        kfree(child_listen_node);
1704                                }
1705                        }
1706                }
1707        }
1708exit:
1709        rtnl_unlock();
1710        return ret;
1711}
1712
1713/**
1714 * i40iw_add_mqh_4 - Adds multiple qhashes for IPv4
1715 * @iwdev: iWarp device
1716 * @cm_info: CM info for parent listen node
1717 * @cm_parent_listen_node: The parent listen node
1718 *
1719 * Adds a qhash and a child listen node for every IPv4 address
1720 * on the adapter and adds the associated qhash filter
1721 */
1722static enum i40iw_status_code i40iw_add_mqh_4(
1723                                struct i40iw_device *iwdev,
1724                                struct i40iw_cm_info *cm_info,
1725                                struct i40iw_cm_listener *cm_parent_listen_node)
1726{
1727        struct net_device *dev;
1728        struct in_device *idev;
1729        struct i40iw_cm_listener *child_listen_node;
1730        enum i40iw_status_code ret = 0;
1731        unsigned long flags;
1732
1733        rtnl_lock();
1734        for_each_netdev(&init_net, dev) {
1735                if ((((rdma_vlan_dev_vlan_id(dev) < I40IW_NO_VLAN) &&
1736                      (rdma_vlan_dev_real_dev(dev) == iwdev->netdev)) ||
1737                    (dev == iwdev->netdev)) && (dev->flags & IFF_UP)) {
1738                        idev = in_dev_get(dev);
1739                        for_ifa(idev) {
1740                                i40iw_debug(&iwdev->sc_dev,
1741                                            I40IW_DEBUG_CM,
1742                                            "Allocating child CM Listener forIP=%pI4, vlan_id=%d, MAC=%pM\n",
1743                                            &ifa->ifa_address,
1744                                            rdma_vlan_dev_vlan_id(dev),
1745                                            dev->dev_addr);
1746                                child_listen_node = kzalloc(sizeof(*child_listen_node), GFP_ATOMIC);
1747                                cm_parent_listen_node->cm_core->stats_listen_nodes_created++;
1748                                i40iw_debug(&iwdev->sc_dev,
1749                                            I40IW_DEBUG_CM,
1750                                            "Allocating child listener %p\n",
1751                                            child_listen_node);
1752                                if (!child_listen_node) {
1753                                        i40iw_pr_err("listener memory allocation\n");
1754                                        in_dev_put(idev);
1755                                        ret = I40IW_ERR_NO_MEMORY;
1756                                        goto exit;
1757                                }
1758                                cm_info->vlan_id = rdma_vlan_dev_vlan_id(dev);
1759                                cm_parent_listen_node->vlan_id = cm_info->vlan_id;
1760                                memcpy(child_listen_node,
1761                                       cm_parent_listen_node,
1762                                       sizeof(*child_listen_node));
1763
1764                                child_listen_node->loc_addr[0] = ntohl(ifa->ifa_address);
1765                                memcpy(cm_info->loc_addr, child_listen_node->loc_addr,
1766                                       sizeof(cm_info->loc_addr));
1767
1768                                ret = i40iw_manage_qhash(iwdev,
1769                                                         cm_info,
1770                                                         I40IW_QHASH_TYPE_TCP_SYN,
1771                                                         I40IW_QHASH_MANAGE_TYPE_ADD,
1772                                                         NULL,
1773                                                         true);
1774                                if (!ret) {
1775                                        spin_lock_irqsave(&iwdev->cm_core.listen_list_lock, flags);
1776                                        list_add(&child_listen_node->child_listen_list,
1777                                                 &cm_parent_listen_node->child_listen_list);
1778                                        spin_unlock_irqrestore(&iwdev->cm_core.listen_list_lock, flags);
1779                                } else {
1780                                        kfree(child_listen_node);
1781                                        cm_parent_listen_node->cm_core->stats_listen_nodes_created--;
1782                                }
1783                        }
1784                        endfor_ifa(idev);
1785                        in_dev_put(idev);
1786                }
1787        }
1788exit:
1789        rtnl_unlock();
1790        return ret;
1791}
1792
1793/**
1794 * i40iw_dec_refcnt_listen - delete listener and associated cm nodes
1795 * @cm_core: cm's core
1796 * @free_hanging_nodes: to free associated cm_nodes
1797 * @apbvt_del: flag to delete the apbvt
1798 */
1799static int i40iw_dec_refcnt_listen(struct i40iw_cm_core *cm_core,
1800                                   struct i40iw_cm_listener *listener,
1801                                   int free_hanging_nodes, bool apbvt_del)
1802{
1803        int ret = -EINVAL;
1804        int err = 0;
1805        struct list_head *list_pos;
1806        struct list_head *list_temp;
1807        struct i40iw_cm_node *cm_node;
1808        struct list_head reset_list;
1809        struct i40iw_cm_info nfo;
1810        struct i40iw_cm_node *loopback;
1811        enum i40iw_cm_node_state old_state;
1812        unsigned long flags;
1813
1814        /* free non-accelerated child nodes for this listener */
1815        INIT_LIST_HEAD(&reset_list);
1816        if (free_hanging_nodes) {
1817                spin_lock_irqsave(&cm_core->ht_lock, flags);
1818                list_for_each_safe(list_pos, list_temp, &cm_core->connected_nodes) {
1819                        cm_node = container_of(list_pos, struct i40iw_cm_node, list);
1820                        if ((cm_node->listener == listener) && !cm_node->accelerated) {
1821                                atomic_inc(&cm_node->ref_count);
1822                                list_add(&cm_node->reset_entry, &reset_list);
1823                        }
1824                }
1825                spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1826        }
1827
1828        list_for_each_safe(list_pos, list_temp, &reset_list) {
1829                cm_node = container_of(list_pos, struct i40iw_cm_node, reset_entry);
1830                loopback = cm_node->loopbackpartner;
1831                if (cm_node->state >= I40IW_CM_STATE_FIN_WAIT1) {
1832                        i40iw_rem_ref_cm_node(cm_node);
1833                } else {
1834                        if (!loopback) {
1835                                i40iw_cleanup_retrans_entry(cm_node);
1836                                err = i40iw_send_reset(cm_node);
1837                                if (err) {
1838                                        cm_node->state = I40IW_CM_STATE_CLOSED;
1839                                        i40iw_pr_err("send reset\n");
1840                                } else {
1841                                        old_state = cm_node->state;
1842                                        cm_node->state = I40IW_CM_STATE_LISTENER_DESTROYED;
1843                                        if (old_state != I40IW_CM_STATE_MPAREQ_RCVD)
1844                                                i40iw_rem_ref_cm_node(cm_node);
1845                                }
1846                        } else {
1847                                struct i40iw_cm_event event;
1848
1849                                event.cm_node = loopback;
1850                                memcpy(event.cm_info.rem_addr,
1851                                       loopback->rem_addr, sizeof(event.cm_info.rem_addr));
1852                                memcpy(event.cm_info.loc_addr,
1853                                       loopback->loc_addr, sizeof(event.cm_info.loc_addr));
1854                                event.cm_info.rem_port = loopback->rem_port;
1855                                event.cm_info.loc_port = loopback->loc_port;
1856                                event.cm_info.cm_id = loopback->cm_id;
1857                                event.cm_info.ipv4 = loopback->ipv4;
1858                                atomic_inc(&loopback->ref_count);
1859                                loopback->state = I40IW_CM_STATE_CLOSED;
1860                                i40iw_event_connect_error(&event);
1861                                cm_node->state = I40IW_CM_STATE_LISTENER_DESTROYED;
1862                                i40iw_rem_ref_cm_node(cm_node);
1863                        }
1864                }
1865        }
1866
1867        if (!atomic_dec_return(&listener->ref_count)) {
1868                spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1869                list_del(&listener->list);
1870                spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1871
1872                if (listener->iwdev) {
1873                        if (apbvt_del && !i40iw_listen_port_in_use(cm_core, listener->loc_port))
1874                                i40iw_manage_apbvt(listener->iwdev,
1875                                                   listener->loc_port,
1876                                                   I40IW_MANAGE_APBVT_DEL);
1877
1878                        memcpy(nfo.loc_addr, listener->loc_addr, sizeof(nfo.loc_addr));
1879                        nfo.loc_port = listener->loc_port;
1880                        nfo.ipv4 = listener->ipv4;
1881                        nfo.vlan_id = listener->vlan_id;
1882
1883                        if (!list_empty(&listener->child_listen_list)) {
1884                                i40iw_del_multiple_qhash(listener->iwdev, &nfo, listener);
1885                        } else {
1886                                if (listener->qhash_set)
1887                                        i40iw_manage_qhash(listener->iwdev,
1888                                                           &nfo,
1889                                                           I40IW_QHASH_TYPE_TCP_SYN,
1890                                                           I40IW_QHASH_MANAGE_TYPE_DELETE,
1891                                                           NULL,
1892                                                           false);
1893                        }
1894                }
1895
1896                cm_core->stats_listen_destroyed++;
1897                kfree(listener);
1898                cm_core->stats_listen_nodes_destroyed++;
1899                listener = NULL;
1900                ret = 0;
1901        }
1902
1903        if (listener) {
1904                if (atomic_read(&listener->pend_accepts_cnt) > 0)
1905                        i40iw_debug(cm_core->dev,
1906                                    I40IW_DEBUG_CM,
1907                                    "%s: listener (%p) pending accepts=%u\n",
1908                                    __func__,
1909                                    listener,
1910                                    atomic_read(&listener->pend_accepts_cnt));
1911        }
1912
1913        return ret;
1914}
1915
1916/**
1917 * i40iw_cm_del_listen - delete a linstener
1918 * @cm_core: cm's core
1919  * @listener: passive connection's listener
1920 * @apbvt_del: flag to delete apbvt
1921 */
1922static int i40iw_cm_del_listen(struct i40iw_cm_core *cm_core,
1923                               struct i40iw_cm_listener *listener,
1924                               bool apbvt_del)
1925{
1926        listener->listener_state = I40IW_CM_LISTENER_PASSIVE_STATE;
1927        listener->cm_id = NULL; /* going to be destroyed pretty soon */
1928        return i40iw_dec_refcnt_listen(cm_core, listener, 1, apbvt_del);
1929}
1930
1931/**
1932 * i40iw_addr_resolve_neigh - resolve neighbor address
1933 * @iwdev: iwarp device structure
1934 * @src_ip: local ip address
1935 * @dst_ip: remote ip address
1936 * @arpindex: if there is an arp entry
1937 */
1938static int i40iw_addr_resolve_neigh(struct i40iw_device *iwdev,
1939                                    u32 src_ip,
1940                                    u32 dst_ip,
1941                                    int arpindex)
1942{
1943        struct rtable *rt;
1944        struct neighbour *neigh;
1945        int rc = arpindex;
1946        struct net_device *netdev = iwdev->netdev;
1947        __be32 dst_ipaddr = htonl(dst_ip);
1948        __be32 src_ipaddr = htonl(src_ip);
1949
1950        rt = ip_route_output(&init_net, dst_ipaddr, src_ipaddr, 0, 0);
1951        if (IS_ERR(rt)) {
1952                i40iw_pr_err("ip_route_output\n");
1953                return rc;
1954        }
1955
1956        if (netif_is_bond_slave(netdev))
1957                netdev = netdev_master_upper_dev_get(netdev);
1958
1959        neigh = dst_neigh_lookup(&rt->dst, &dst_ipaddr);
1960
1961        rcu_read_lock();
1962        if (neigh) {
1963                if (neigh->nud_state & NUD_VALID) {
1964                        if (arpindex >= 0) {
1965                                if (ether_addr_equal(iwdev->arp_table[arpindex].mac_addr,
1966                                                     neigh->ha))
1967                                        /* Mac address same as arp table */
1968                                        goto resolve_neigh_exit;
1969                                i40iw_manage_arp_cache(iwdev,
1970                                                       iwdev->arp_table[arpindex].mac_addr,
1971                                                       &dst_ip,
1972                                                       true,
1973                                                       I40IW_ARP_DELETE);
1974                        }
1975
1976                        i40iw_manage_arp_cache(iwdev, neigh->ha, &dst_ip, true, I40IW_ARP_ADD);
1977                        rc = i40iw_arp_table(iwdev, &dst_ip, true, NULL, I40IW_ARP_RESOLVE);
1978                } else {
1979                        neigh_event_send(neigh, NULL);
1980                }
1981        }
1982 resolve_neigh_exit:
1983
1984        rcu_read_unlock();
1985        if (neigh)
1986                neigh_release(neigh);
1987
1988        ip_rt_put(rt);
1989        return rc;
1990}
1991
1992/**
1993 * i40iw_get_dst_ipv6
1994 */
1995static struct dst_entry *i40iw_get_dst_ipv6(struct sockaddr_in6 *src_addr,
1996                                            struct sockaddr_in6 *dst_addr)
1997{
1998        struct dst_entry *dst;
1999        struct flowi6 fl6;
2000
2001        memset(&fl6, 0, sizeof(fl6));
2002        fl6.daddr = dst_addr->sin6_addr;
2003        fl6.saddr = src_addr->sin6_addr;
2004        if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
2005                fl6.flowi6_oif = dst_addr->sin6_scope_id;
2006
2007        dst = ip6_route_output(&init_net, NULL, &fl6);
2008        return dst;
2009}
2010
2011/**
2012 * i40iw_addr_resolve_neigh_ipv6 - resolve neighbor ipv6 address
2013 * @iwdev: iwarp device structure
2014 * @dst_ip: remote ip address
2015 * @arpindex: if there is an arp entry
2016 */
2017static int i40iw_addr_resolve_neigh_ipv6(struct i40iw_device *iwdev,
2018                                         u32 *src,
2019                                         u32 *dest,
2020                                         int arpindex)
2021{
2022        struct neighbour *neigh;
2023        int rc = arpindex;
2024        struct net_device *netdev = iwdev->netdev;
2025        struct dst_entry *dst;
2026        struct sockaddr_in6 dst_addr;
2027        struct sockaddr_in6 src_addr;
2028
2029        memset(&dst_addr, 0, sizeof(dst_addr));
2030        dst_addr.sin6_family = AF_INET6;
2031        i40iw_copy_ip_htonl(dst_addr.sin6_addr.in6_u.u6_addr32, dest);
2032        memset(&src_addr, 0, sizeof(src_addr));
2033        src_addr.sin6_family = AF_INET6;
2034        i40iw_copy_ip_htonl(src_addr.sin6_addr.in6_u.u6_addr32, src);
2035        dst = i40iw_get_dst_ipv6(&src_addr, &dst_addr);
2036        if (!dst || dst->error) {
2037                if (dst) {
2038                        dst_release(dst);
2039                        i40iw_pr_err("ip6_route_output returned dst->error = %d\n",
2040                                     dst->error);
2041                }
2042                return rc;
2043        }
2044
2045        if (netif_is_bond_slave(netdev))
2046                netdev = netdev_master_upper_dev_get(netdev);
2047
2048        neigh = dst_neigh_lookup(dst, &dst_addr);
2049
2050        rcu_read_lock();
2051        if (neigh) {
2052                i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_CM, "dst_neigh_lookup MAC=%pM\n", neigh->ha);
2053                if (neigh->nud_state & NUD_VALID) {
2054                        if (arpindex >= 0) {
2055                                if (ether_addr_equal
2056                                    (iwdev->arp_table[arpindex].mac_addr,
2057                                     neigh->ha)) {
2058                                        /* Mac address same as in arp table */
2059                                        goto resolve_neigh_exit6;
2060                                }
2061                                i40iw_manage_arp_cache(iwdev,
2062                                                       iwdev->arp_table[arpindex].mac_addr,
2063                                                       dest,
2064                                                       false,
2065                                                       I40IW_ARP_DELETE);
2066                        }
2067                        i40iw_manage_arp_cache(iwdev,
2068                                               neigh->ha,
2069                                               dest,
2070                                               false,
2071                                               I40IW_ARP_ADD);
2072                        rc = i40iw_arp_table(iwdev,
2073                                             dest,
2074                                             false,
2075                                             NULL,
2076                                             I40IW_ARP_RESOLVE);
2077                } else {
2078                        neigh_event_send(neigh, NULL);
2079                }
2080        }
2081
2082 resolve_neigh_exit6:
2083        rcu_read_unlock();
2084        if (neigh)
2085                neigh_release(neigh);
2086        dst_release(dst);
2087        return rc;
2088}
2089
2090/**
2091 * i40iw_ipv4_is_loopback - check if loopback
2092 * @loc_addr: local addr to compare
2093 * @rem_addr: remote address
2094 */
2095static bool i40iw_ipv4_is_loopback(u32 loc_addr, u32 rem_addr)
2096{
2097        return ipv4_is_loopback(htonl(rem_addr)) || (loc_addr == rem_addr);
2098}
2099
2100/**
2101 * i40iw_ipv6_is_loopback - check if loopback
2102 * @loc_addr: local addr to compare
2103 * @rem_addr: remote address
2104 */
2105static bool i40iw_ipv6_is_loopback(u32 *loc_addr, u32 *rem_addr)
2106{
2107        struct in6_addr raddr6;
2108
2109        i40iw_copy_ip_htonl(raddr6.in6_u.u6_addr32, rem_addr);
2110        return (!memcmp(loc_addr, rem_addr, 16) || ipv6_addr_loopback(&raddr6));
2111}
2112
2113/**
2114 * i40iw_make_cm_node - create a new instance of a cm node
2115 * @cm_core: cm's core
2116 * @iwdev: iwarp device structure
2117 * @cm_info: quad info for connection
2118 * @listener: passive connection's listener
2119 */
2120static struct i40iw_cm_node *i40iw_make_cm_node(
2121                                   struct i40iw_cm_core *cm_core,
2122                                   struct i40iw_device *iwdev,
2123                                   struct i40iw_cm_info *cm_info,
2124                                   struct i40iw_cm_listener *listener)
2125{
2126        struct i40iw_cm_node *cm_node;
2127        struct timespec ts;
2128        int oldarpindex;
2129        int arpindex;
2130        struct net_device *netdev = iwdev->netdev;
2131
2132        /* create an hte and cm_node for this instance */
2133        cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
2134        if (!cm_node)
2135                return NULL;
2136
2137        /* set our node specific transport info */
2138        cm_node->ipv4 = cm_info->ipv4;
2139        cm_node->vlan_id = cm_info->vlan_id;
2140        memcpy(cm_node->loc_addr, cm_info->loc_addr, sizeof(cm_node->loc_addr));
2141        memcpy(cm_node->rem_addr, cm_info->rem_addr, sizeof(cm_node->rem_addr));
2142        cm_node->loc_port = cm_info->loc_port;
2143        cm_node->rem_port = cm_info->rem_port;
2144
2145        cm_node->mpa_frame_rev = iwdev->mpa_version;
2146        cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
2147        cm_node->ird_size = I40IW_MAX_IRD_SIZE;
2148        cm_node->ord_size = I40IW_MAX_ORD_SIZE;
2149
2150        cm_node->listener = listener;
2151        cm_node->cm_id = cm_info->cm_id;
2152        ether_addr_copy(cm_node->loc_mac, netdev->dev_addr);
2153        spin_lock_init(&cm_node->retrans_list_lock);
2154
2155        atomic_set(&cm_node->ref_count, 1);
2156        /* associate our parent CM core */
2157        cm_node->cm_core = cm_core;
2158        cm_node->tcp_cntxt.loc_id = I40IW_CM_DEF_LOCAL_ID;
2159        cm_node->tcp_cntxt.rcv_wscale = I40IW_CM_DEFAULT_RCV_WND_SCALE;
2160        cm_node->tcp_cntxt.rcv_wnd =
2161                        I40IW_CM_DEFAULT_RCV_WND_SCALED >> I40IW_CM_DEFAULT_RCV_WND_SCALE;
2162        ts = current_kernel_time();
2163        cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
2164        cm_node->tcp_cntxt.mss = iwdev->mss;
2165
2166        cm_node->iwdev = iwdev;
2167        cm_node->dev = &iwdev->sc_dev;
2168
2169        if ((cm_node->ipv4 &&
2170             i40iw_ipv4_is_loopback(cm_node->loc_addr[0], cm_node->rem_addr[0])) ||
2171             (!cm_node->ipv4 && i40iw_ipv6_is_loopback(cm_node->loc_addr,
2172                                                       cm_node->rem_addr))) {
2173                arpindex = i40iw_arp_table(iwdev,
2174                                           cm_node->rem_addr,
2175                                           false,
2176                                           NULL,
2177                                           I40IW_ARP_RESOLVE);
2178        } else {
2179                oldarpindex = i40iw_arp_table(iwdev,
2180                                              cm_node->rem_addr,
2181                                              false,
2182                                              NULL,
2183                                              I40IW_ARP_RESOLVE);
2184                if (cm_node->ipv4)
2185                        arpindex = i40iw_addr_resolve_neigh(iwdev,
2186                                                            cm_info->loc_addr[0],
2187                                                            cm_info->rem_addr[0],
2188                                                            oldarpindex);
2189                else if (IS_ENABLED(CONFIG_IPV6))
2190                        arpindex = i40iw_addr_resolve_neigh_ipv6(iwdev,
2191                                                                 cm_info->loc_addr,
2192                                                                 cm_info->rem_addr,
2193                                                                 oldarpindex);
2194                else
2195                        arpindex = -EINVAL;
2196        }
2197        if (arpindex < 0) {
2198                i40iw_pr_err("cm_node arpindex\n");
2199                kfree(cm_node);
2200                return NULL;
2201        }
2202        ether_addr_copy(cm_node->rem_mac, iwdev->arp_table[arpindex].mac_addr);
2203        i40iw_add_hte_node(cm_core, cm_node);
2204        cm_core->stats_nodes_created++;
2205        return cm_node;
2206}
2207
2208/**
2209 * i40iw_rem_ref_cm_node - destroy an instance of a cm node
2210 * @cm_node: connection's node
2211 */
2212static void i40iw_rem_ref_cm_node(struct i40iw_cm_node *cm_node)
2213{
2214        struct i40iw_cm_core *cm_core = cm_node->cm_core;
2215        struct i40iw_qp *iwqp;
2216        struct i40iw_cm_info nfo;
2217        unsigned long flags;
2218
2219        spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
2220        if (atomic_dec_return(&cm_node->ref_count)) {
2221                spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
2222                return;
2223        }
2224        list_del(&cm_node->list);
2225        spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
2226
2227        /* if the node is destroyed before connection was accelerated */
2228        if (!cm_node->accelerated && cm_node->accept_pend) {
2229                pr_err("node destroyed before established\n");
2230                atomic_dec(&cm_node->listener->pend_accepts_cnt);
2231        }
2232        if (cm_node->close_entry)
2233                i40iw_handle_close_entry(cm_node, 0);
2234        if (cm_node->listener) {
2235                i40iw_dec_refcnt_listen(cm_core, cm_node->listener, 0, true);
2236        } else {
2237                if (!i40iw_listen_port_in_use(cm_core, htons(cm_node->loc_port)) &&
2238                    cm_node->apbvt_set && cm_node->iwdev) {
2239                        i40iw_manage_apbvt(cm_node->iwdev,
2240                                           cm_node->loc_port,
2241                                           I40IW_MANAGE_APBVT_DEL);
2242                        i40iw_get_addr_info(cm_node, &nfo);
2243                        if (cm_node->qhash_set) {
2244                                i40iw_manage_qhash(cm_node->iwdev,
2245                                                   &nfo,
2246                                                   I40IW_QHASH_TYPE_TCP_ESTABLISHED,
2247                                                   I40IW_QHASH_MANAGE_TYPE_DELETE,
2248                                                   NULL,
2249                                                   false);
2250                                cm_node->qhash_set = 0;
2251                        }
2252                }
2253        }
2254
2255        iwqp = cm_node->iwqp;
2256        if (iwqp) {
2257                iwqp->cm_node = NULL;
2258                i40iw_rem_ref(&iwqp->ibqp);
2259                cm_node->iwqp = NULL;
2260        } else if (cm_node->qhash_set) {
2261                i40iw_get_addr_info(cm_node, &nfo);
2262                i40iw_manage_qhash(cm_node->iwdev,
2263                                   &nfo,
2264                                   I40IW_QHASH_TYPE_TCP_ESTABLISHED,
2265                                   I40IW_QHASH_MANAGE_TYPE_DELETE,
2266                                   NULL,
2267                                   false);
2268                cm_node->qhash_set = 0;
2269        }
2270
2271        cm_node->cm_core->stats_nodes_destroyed++;
2272        kfree(cm_node);
2273}
2274
2275/**
2276 * i40iw_handle_fin_pkt - FIN packet received
2277 * @cm_node: connection's node
2278 */
2279static void i40iw_handle_fin_pkt(struct i40iw_cm_node *cm_node)
2280{
2281        u32 ret;
2282
2283        switch (cm_node->state) {
2284        case I40IW_CM_STATE_SYN_RCVD:
2285        case I40IW_CM_STATE_SYN_SENT:
2286        case I40IW_CM_STATE_ESTABLISHED:
2287        case I40IW_CM_STATE_MPAREJ_RCVD:
2288                cm_node->tcp_cntxt.rcv_nxt++;
2289                i40iw_cleanup_retrans_entry(cm_node);
2290                cm_node->state = I40IW_CM_STATE_LAST_ACK;
2291                i40iw_send_fin(cm_node);
2292                break;
2293        case I40IW_CM_STATE_MPAREQ_SENT:
2294                i40iw_create_event(cm_node, I40IW_CM_EVENT_ABORTED);
2295                cm_node->tcp_cntxt.rcv_nxt++;
2296                i40iw_cleanup_retrans_entry(cm_node);
2297                cm_node->state = I40IW_CM_STATE_CLOSED;
2298                atomic_inc(&cm_node->ref_count);
2299                i40iw_send_reset(cm_node);
2300                break;
2301        case I40IW_CM_STATE_FIN_WAIT1:
2302                cm_node->tcp_cntxt.rcv_nxt++;
2303                i40iw_cleanup_retrans_entry(cm_node);
2304                cm_node->state = I40IW_CM_STATE_CLOSING;
2305                i40iw_send_ack(cm_node);
2306                /*
2307                 * Wait for ACK as this is simultaneous close.
2308                 * After we receive ACK, do not send anything.
2309                 * Just rm the node.
2310                 */
2311                break;
2312        case I40IW_CM_STATE_FIN_WAIT2:
2313                cm_node->tcp_cntxt.rcv_nxt++;
2314                i40iw_cleanup_retrans_entry(cm_node);
2315                cm_node->state = I40IW_CM_STATE_TIME_WAIT;
2316                i40iw_send_ack(cm_node);
2317                ret =
2318                    i40iw_schedule_cm_timer(cm_node, NULL, I40IW_TIMER_TYPE_CLOSE, 1, 0);
2319                if (ret)
2320                        i40iw_pr_err("node %p state = %d\n", cm_node, cm_node->state);
2321                break;
2322        case I40IW_CM_STATE_TIME_WAIT:
2323                cm_node->tcp_cntxt.rcv_nxt++;
2324                i40iw_cleanup_retrans_entry(cm_node);
2325                cm_node->state = I40IW_CM_STATE_CLOSED;
2326                i40iw_rem_ref_cm_node(cm_node);
2327                break;
2328        case I40IW_CM_STATE_OFFLOADED:
2329        default:
2330                i40iw_pr_err("bad state node %p state = %d\n", cm_node, cm_node->state);
2331                break;
2332        }
2333}
2334
2335/**
2336 * i40iw_handle_rst_pkt - process received RST packet
2337 * @cm_node: connection's node
2338 * @rbuf: receive buffer
2339 */
2340static void i40iw_handle_rst_pkt(struct i40iw_cm_node *cm_node,
2341                                 struct i40iw_puda_buf *rbuf)
2342{
2343        i40iw_cleanup_retrans_entry(cm_node);
2344        switch (cm_node->state) {
2345        case I40IW_CM_STATE_SYN_SENT:
2346        case I40IW_CM_STATE_MPAREQ_SENT:
2347                switch (cm_node->mpa_frame_rev) {
2348                case IETF_MPA_V2:
2349                        cm_node->mpa_frame_rev = IETF_MPA_V1;
2350                        /* send a syn and goto syn sent state */
2351                        cm_node->state = I40IW_CM_STATE_SYN_SENT;
2352                        if (i40iw_send_syn(cm_node, 0))
2353                                i40iw_active_open_err(cm_node, false);
2354                        break;
2355                case IETF_MPA_V1:
2356                default:
2357                        i40iw_active_open_err(cm_node, false);
2358                        break;
2359                }
2360                break;
2361        case I40IW_CM_STATE_MPAREQ_RCVD:
2362                atomic_add_return(1, &cm_node->passive_state);
2363                break;
2364        case I40IW_CM_STATE_ESTABLISHED:
2365        case I40IW_CM_STATE_SYN_RCVD:
2366        case I40IW_CM_STATE_LISTENING:
2367                i40iw_pr_err("Bad state state = %d\n", cm_node->state);
2368                i40iw_passive_open_err(cm_node, false);
2369                break;
2370        case I40IW_CM_STATE_OFFLOADED:
2371                i40iw_active_open_err(cm_node, false);
2372                break;
2373        case I40IW_CM_STATE_CLOSED:
2374                break;
2375        case I40IW_CM_STATE_FIN_WAIT2:
2376        case I40IW_CM_STATE_FIN_WAIT1:
2377        case I40IW_CM_STATE_LAST_ACK:
2378                cm_node->cm_id->rem_ref(cm_node->cm_id);
2379        case I40IW_CM_STATE_TIME_WAIT:
2380                cm_node->state = I40IW_CM_STATE_CLOSED;
2381                i40iw_rem_ref_cm_node(cm_node);
2382                break;
2383        default:
2384                break;
2385        }
2386}
2387
2388/**
2389 * i40iw_handle_rcv_mpa - Process a recv'd mpa buffer
2390 * @cm_node: connection's node
2391 * @rbuf: receive buffer
2392 */
2393static void i40iw_handle_rcv_mpa(struct i40iw_cm_node *cm_node,
2394                                 struct i40iw_puda_buf *rbuf)
2395{
2396        int ret;
2397        int datasize = rbuf->datalen;
2398        u8 *dataloc = rbuf->data;
2399
2400        enum i40iw_cm_event_type type = I40IW_CM_EVENT_UNKNOWN;
2401        u32 res_type;
2402
2403        ret = i40iw_parse_mpa(cm_node, dataloc, &res_type, datasize);
2404        if (ret) {
2405                if (cm_node->state == I40IW_CM_STATE_MPAREQ_SENT)
2406                        i40iw_active_open_err(cm_node, true);
2407                else
2408                        i40iw_passive_open_err(cm_node, true);
2409                return;
2410        }
2411
2412        switch (cm_node->state) {
2413        case I40IW_CM_STATE_ESTABLISHED:
2414                if (res_type == I40IW_MPA_REQUEST_REJECT)
2415                        i40iw_pr_err("state for reject\n");
2416                cm_node->state = I40IW_CM_STATE_MPAREQ_RCVD;
2417                type = I40IW_CM_EVENT_MPA_REQ;
2418                i40iw_send_ack(cm_node);        /* ACK received MPA request */
2419                atomic_set(&cm_node->passive_state,
2420                           I40IW_PASSIVE_STATE_INDICATED);
2421                break;
2422        case I40IW_CM_STATE_MPAREQ_SENT:
2423                i40iw_cleanup_retrans_entry(cm_node);
2424                if (res_type == I40IW_MPA_REQUEST_REJECT) {
2425                        type = I40IW_CM_EVENT_MPA_REJECT;
2426                        cm_node->state = I40IW_CM_STATE_MPAREJ_RCVD;
2427                } else {
2428                        type = I40IW_CM_EVENT_CONNECTED;
2429                        cm_node->state = I40IW_CM_STATE_OFFLOADED;
2430                        i40iw_send_ack(cm_node);
2431                }
2432                break;
2433        default:
2434                pr_err("%s wrong cm_node state =%d\n", __func__, cm_node->state);
2435                break;
2436        }
2437        i40iw_create_event(cm_node, type);
2438}
2439
2440/**
2441 * i40iw_indicate_pkt_err - Send up err event to cm
2442 * @cm_node: connection's node
2443 */
2444static void i40iw_indicate_pkt_err(struct i40iw_cm_node *cm_node)
2445{
2446        switch (cm_node->state) {
2447        case I40IW_CM_STATE_SYN_SENT:
2448        case I40IW_CM_STATE_MPAREQ_SENT:
2449                i40iw_active_open_err(cm_node, true);
2450                break;
2451        case I40IW_CM_STATE_ESTABLISHED:
2452        case I40IW_CM_STATE_SYN_RCVD:
2453                i40iw_passive_open_err(cm_node, true);
2454                break;
2455        case I40IW_CM_STATE_OFFLOADED:
2456        default:
2457                break;
2458        }
2459}
2460
2461/**
2462 * i40iw_check_syn - Check for error on received syn ack
2463 * @cm_node: connection's node
2464 * @tcph: pointer tcp header
2465 */
2466static int i40iw_check_syn(struct i40iw_cm_node *cm_node, struct tcphdr *tcph)
2467{
2468        int err = 0;
2469
2470        if (ntohl(tcph->ack_seq) != cm_node->tcp_cntxt.loc_seq_num) {
2471                err = 1;
2472                i40iw_active_open_err(cm_node, true);
2473        }
2474        return err;
2475}
2476
2477/**
2478 * i40iw_check_seq - check seq numbers if OK
2479 * @cm_node: connection's node
2480 * @tcph: pointer tcp header
2481 */
2482static int i40iw_check_seq(struct i40iw_cm_node *cm_node, struct tcphdr *tcph)
2483{
2484        int err = 0;
2485        u32 seq;
2486        u32 ack_seq;
2487        u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
2488        u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
2489        u32 rcv_wnd;
2490
2491        seq = ntohl(tcph->seq);
2492        ack_seq = ntohl(tcph->ack_seq);
2493        rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
2494        if (ack_seq != loc_seq_num)
2495                err = -1;
2496        else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
2497                err = -1;
2498        if (err) {
2499                i40iw_pr_err("seq number\n");
2500                i40iw_indicate_pkt_err(cm_node);
2501        }
2502        return err;
2503}
2504
2505/**
2506 * i40iw_handle_syn_pkt - is for Passive node
2507 * @cm_node: connection's node
2508 * @rbuf: receive buffer
2509 */
2510static void i40iw_handle_syn_pkt(struct i40iw_cm_node *cm_node,
2511                                 struct i40iw_puda_buf *rbuf)
2512{
2513        struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2514        int ret;
2515        u32 inc_sequence;
2516        int optionsize;
2517        struct i40iw_cm_info nfo;
2518
2519        optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
2520        inc_sequence = ntohl(tcph->seq);
2521
2522        switch (cm_node->state) {
2523        case I40IW_CM_STATE_SYN_SENT:
2524        case I40IW_CM_STATE_MPAREQ_SENT:
2525                /* Rcvd syn on active open connection */
2526                i40iw_active_open_err(cm_node, 1);
2527                break;
2528        case I40IW_CM_STATE_LISTENING:
2529                /* Passive OPEN */
2530                if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
2531                    cm_node->listener->backlog) {
2532                        cm_node->cm_core->stats_backlog_drops++;
2533                        i40iw_passive_open_err(cm_node, false);
2534                        break;
2535                }
2536                ret = i40iw_handle_tcp_options(cm_node, tcph, optionsize, 1);
2537                if (ret) {
2538                        i40iw_passive_open_err(cm_node, false);
2539                        /* drop pkt */
2540                        break;
2541                }
2542                cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2543                cm_node->accept_pend = 1;
2544                atomic_inc(&cm_node->listener->pend_accepts_cnt);
2545
2546                cm_node->state = I40IW_CM_STATE_SYN_RCVD;
2547                i40iw_get_addr_info(cm_node, &nfo);
2548                ret = i40iw_manage_qhash(cm_node->iwdev,
2549                                         &nfo,
2550                                         I40IW_QHASH_TYPE_TCP_ESTABLISHED,
2551                                         I40IW_QHASH_MANAGE_TYPE_ADD,
2552                                         (void *)cm_node,
2553                                         false);
2554                cm_node->qhash_set = true;
2555                break;
2556        case I40IW_CM_STATE_CLOSED:
2557                i40iw_cleanup_retrans_entry(cm_node);
2558                atomic_inc(&cm_node->ref_count);
2559                i40iw_send_reset(cm_node);
2560                break;
2561        case I40IW_CM_STATE_OFFLOADED:
2562        case I40IW_CM_STATE_ESTABLISHED:
2563        case I40IW_CM_STATE_FIN_WAIT1:
2564        case I40IW_CM_STATE_FIN_WAIT2:
2565        case I40IW_CM_STATE_MPAREQ_RCVD:
2566        case I40IW_CM_STATE_LAST_ACK:
2567        case I40IW_CM_STATE_CLOSING:
2568        case I40IW_CM_STATE_UNKNOWN:
2569        default:
2570                break;
2571        }
2572}
2573
2574/**
2575 * i40iw_handle_synack_pkt - Process SYN+ACK packet (active side)
2576 * @cm_node: connection's node
2577 * @rbuf: receive buffer
2578 */
2579static void i40iw_handle_synack_pkt(struct i40iw_cm_node *cm_node,
2580                                    struct i40iw_puda_buf *rbuf)
2581{
2582        struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2583        int ret;
2584        u32 inc_sequence;
2585        int optionsize;
2586
2587        optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
2588        inc_sequence = ntohl(tcph->seq);
2589        switch (cm_node->state) {
2590        case I40IW_CM_STATE_SYN_SENT:
2591                i40iw_cleanup_retrans_entry(cm_node);
2592                /* active open */
2593                if (i40iw_check_syn(cm_node, tcph)) {
2594                        i40iw_pr_err("check syn fail\n");
2595                        return;
2596                }
2597                cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2598                /* setup options */
2599                ret = i40iw_handle_tcp_options(cm_node, tcph, optionsize, 0);
2600                if (ret) {
2601                        i40iw_debug(cm_node->dev,
2602                                    I40IW_DEBUG_CM,
2603                                    "cm_node=%p tcp_options failed\n",
2604                                    cm_node);
2605                        break;
2606                }
2607                i40iw_cleanup_retrans_entry(cm_node);
2608                cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2609                i40iw_send_ack(cm_node);        /* ACK  for the syn_ack */
2610                ret = i40iw_send_mpa_request(cm_node);
2611                if (ret) {
2612                        i40iw_debug(cm_node->dev,
2613                                    I40IW_DEBUG_CM,
2614                                    "cm_node=%p i40iw_send_mpa_request failed\n",
2615                                    cm_node);
2616                        break;
2617                }
2618                cm_node->state = I40IW_CM_STATE_MPAREQ_SENT;
2619                break;
2620        case I40IW_CM_STATE_MPAREQ_RCVD:
2621                i40iw_passive_open_err(cm_node, true);
2622                break;
2623        case I40IW_CM_STATE_LISTENING:
2624                cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2625                i40iw_cleanup_retrans_entry(cm_node);
2626                cm_node->state = I40IW_CM_STATE_CLOSED;
2627                i40iw_send_reset(cm_node);
2628                break;
2629        case I40IW_CM_STATE_CLOSED:
2630                cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2631                i40iw_cleanup_retrans_entry(cm_node);
2632                atomic_inc(&cm_node->ref_count);
2633                i40iw_send_reset(cm_node);
2634                break;
2635        case I40IW_CM_STATE_ESTABLISHED:
2636        case I40IW_CM_STATE_FIN_WAIT1:
2637        case I40IW_CM_STATE_FIN_WAIT2:
2638        case I40IW_CM_STATE_LAST_ACK:
2639        case I40IW_CM_STATE_OFFLOADED:
2640        case I40IW_CM_STATE_CLOSING:
2641        case I40IW_CM_STATE_UNKNOWN:
2642        case I40IW_CM_STATE_MPAREQ_SENT:
2643        default:
2644                break;
2645        }
2646}
2647
2648/**
2649 * i40iw_handle_ack_pkt - process packet with ACK
2650 * @cm_node: connection's node
2651 * @rbuf: receive buffer
2652 */
2653static int i40iw_handle_ack_pkt(struct i40iw_cm_node *cm_node,
2654                                struct i40iw_puda_buf *rbuf)
2655{
2656        struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2657        u32 inc_sequence;
2658        int ret = 0;
2659        int optionsize;
2660        u32 datasize = rbuf->datalen;
2661
2662        optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
2663
2664        if (i40iw_check_seq(cm_node, tcph))
2665                return -EINVAL;
2666
2667        inc_sequence = ntohl(tcph->seq);
2668        switch (cm_node->state) {
2669        case I40IW_CM_STATE_SYN_RCVD:
2670                i40iw_cleanup_retrans_entry(cm_node);
2671                ret = i40iw_handle_tcp_options(cm_node, tcph, optionsize, 1);
2672                if (ret)
2673                        break;
2674                cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2675                cm_node->state = I40IW_CM_STATE_ESTABLISHED;
2676                if (datasize) {
2677                        cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2678                        i40iw_handle_rcv_mpa(cm_node, rbuf);
2679                }
2680                break;
2681        case I40IW_CM_STATE_ESTABLISHED:
2682                i40iw_cleanup_retrans_entry(cm_node);
2683                if (datasize) {
2684                        cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2685                        i40iw_handle_rcv_mpa(cm_node, rbuf);
2686                }
2687                break;
2688        case I40IW_CM_STATE_MPAREQ_SENT:
2689                cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2690                if (datasize) {
2691                        cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2692                        i40iw_handle_rcv_mpa(cm_node, rbuf);
2693                }
2694                break;
2695        case I40IW_CM_STATE_LISTENING:
2696                i40iw_cleanup_retrans_entry(cm_node);
2697                cm_node->state = I40IW_CM_STATE_CLOSED;
2698                i40iw_send_reset(cm_node);
2699                break;
2700        case I40IW_CM_STATE_CLOSED:
2701                i40iw_cleanup_retrans_entry(cm_node);
2702                atomic_inc(&cm_node->ref_count);
2703                i40iw_send_reset(cm_node);
2704                break;
2705        case I40IW_CM_STATE_LAST_ACK:
2706        case I40IW_CM_STATE_CLOSING:
2707                i40iw_cleanup_retrans_entry(cm_node);
2708                cm_node->state = I40IW_CM_STATE_CLOSED;
2709                if (!cm_node->accept_pend)
2710                        cm_node->cm_id->rem_ref(cm_node->cm_id);
2711                i40iw_rem_ref_cm_node(cm_node);
2712                break;
2713        case I40IW_CM_STATE_FIN_WAIT1:
2714                i40iw_cleanup_retrans_entry(cm_node);
2715                cm_node->state = I40IW_CM_STATE_FIN_WAIT2;
2716                break;
2717        case I40IW_CM_STATE_SYN_SENT:
2718        case I40IW_CM_STATE_FIN_WAIT2:
2719        case I40IW_CM_STATE_OFFLOADED:
2720        case I40IW_CM_STATE_MPAREQ_RCVD:
2721        case I40IW_CM_STATE_UNKNOWN:
2722        default:
2723                i40iw_cleanup_retrans_entry(cm_node);
2724                break;
2725        }
2726        return ret;
2727}
2728
2729/**
2730 * i40iw_process_packet - process cm packet
2731 * @cm_node: connection's node
2732 * @rbuf: receive buffer
2733 */
2734static void i40iw_process_packet(struct i40iw_cm_node *cm_node,
2735                                 struct i40iw_puda_buf *rbuf)
2736{
2737        enum i40iw_tcpip_pkt_type pkt_type = I40IW_PKT_TYPE_UNKNOWN;
2738        struct tcphdr *tcph = (struct tcphdr *)rbuf->tcph;
2739        u32 fin_set = 0;
2740        int ret;
2741
2742        if (tcph->rst) {
2743                pkt_type = I40IW_PKT_TYPE_RST;
2744        } else if (tcph->syn) {
2745                pkt_type = I40IW_PKT_TYPE_SYN;
2746                if (tcph->ack)
2747                        pkt_type = I40IW_PKT_TYPE_SYNACK;
2748        } else if (tcph->ack) {
2749                pkt_type = I40IW_PKT_TYPE_ACK;
2750        }
2751        if (tcph->fin)
2752                fin_set = 1;
2753
2754        switch (pkt_type) {
2755        case I40IW_PKT_TYPE_SYN:
2756                i40iw_handle_syn_pkt(cm_node, rbuf);
2757                break;
2758        case I40IW_PKT_TYPE_SYNACK:
2759                i40iw_handle_synack_pkt(cm_node, rbuf);
2760                break;
2761        case I40IW_PKT_TYPE_ACK:
2762                ret = i40iw_handle_ack_pkt(cm_node, rbuf);
2763                if (fin_set && !ret)
2764                        i40iw_handle_fin_pkt(cm_node);
2765                break;
2766        case I40IW_PKT_TYPE_RST:
2767                i40iw_handle_rst_pkt(cm_node, rbuf);
2768                break;
2769        default:
2770                if (fin_set &&
2771                    (!i40iw_check_seq(cm_node, (struct tcphdr *)rbuf->tcph)))
2772                        i40iw_handle_fin_pkt(cm_node);
2773                break;
2774        }
2775}
2776
2777/**
2778 * i40iw_make_listen_node - create a listen node with params
2779 * @cm_core: cm's core
2780 * @iwdev: iwarp device structure
2781 * @cm_info: quad info for connection
2782 */
2783static struct i40iw_cm_listener *i40iw_make_listen_node(
2784                                        struct i40iw_cm_core *cm_core,
2785                                        struct i40iw_device *iwdev,
2786                                        struct i40iw_cm_info *cm_info)
2787{
2788        struct i40iw_cm_listener *listener;
2789        unsigned long flags;
2790
2791        /* cannot have multiple matching listeners */
2792        listener = i40iw_find_listener(cm_core, cm_info->loc_addr,
2793                                       cm_info->loc_port,
2794                                       cm_info->vlan_id,
2795                                       I40IW_CM_LISTENER_EITHER_STATE);
2796        if (listener &&
2797            (listener->listener_state == I40IW_CM_LISTENER_ACTIVE_STATE)) {
2798                atomic_dec(&listener->ref_count);
2799                i40iw_debug(cm_core->dev,
2800                            I40IW_DEBUG_CM,
2801                            "Not creating listener since it already exists\n");
2802                return NULL;
2803        }
2804
2805        if (!listener) {
2806                /* create a CM listen node (1/2 node to compare incoming traffic to) */
2807                listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2808                if (!listener)
2809                        return NULL;
2810                cm_core->stats_listen_nodes_created++;
2811                memcpy(listener->loc_addr, cm_info->loc_addr, sizeof(listener->loc_addr));
2812                listener->loc_port = cm_info->loc_port;
2813
2814                INIT_LIST_HEAD(&listener->child_listen_list);
2815
2816                atomic_set(&listener->ref_count, 1);
2817        } else {
2818                listener->reused_node = 1;
2819        }
2820
2821        listener->cm_id = cm_info->cm_id;
2822        listener->ipv4 = cm_info->ipv4;
2823        listener->vlan_id = cm_info->vlan_id;
2824        atomic_set(&listener->pend_accepts_cnt, 0);
2825        listener->cm_core = cm_core;
2826        listener->iwdev = iwdev;
2827
2828        listener->backlog = cm_info->backlog;
2829        listener->listener_state = I40IW_CM_LISTENER_ACTIVE_STATE;
2830
2831        if (!listener->reused_node) {
2832                spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2833                list_add(&listener->list, &cm_core->listen_nodes);
2834                spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2835        }
2836
2837        return listener;
2838}
2839
2840/**
2841 * i40iw_create_cm_node - make a connection node with params
2842 * @cm_core: cm's core
2843 * @iwdev: iwarp device structure
2844 * @private_data_len: len to provate data for mpa request
2845 * @private_data: pointer to private data for connection
2846 * @cm_info: quad info for connection
2847 */
2848static struct i40iw_cm_node *i40iw_create_cm_node(
2849                                        struct i40iw_cm_core *cm_core,
2850                                        struct i40iw_device *iwdev,
2851                                        u16 private_data_len,
2852                                        void *private_data,
2853                                        struct i40iw_cm_info *cm_info)
2854{
2855        int ret;
2856        struct i40iw_cm_node *cm_node;
2857        struct i40iw_cm_listener *loopback_remotelistener;
2858        struct i40iw_cm_node *loopback_remotenode;
2859        struct i40iw_cm_info loopback_cm_info;
2860
2861        /* create a CM connection node */
2862        cm_node = i40iw_make_cm_node(cm_core, iwdev, cm_info, NULL);
2863        if (!cm_node)
2864                return NULL;
2865        /* set our node side to client (active) side */
2866        cm_node->tcp_cntxt.client = 1;
2867        cm_node->tcp_cntxt.rcv_wscale = I40IW_CM_DEFAULT_RCV_WND_SCALE;
2868
2869        if (!memcmp(cm_info->loc_addr, cm_info->rem_addr, sizeof(cm_info->loc_addr))) {
2870                loopback_remotelistener = i40iw_find_listener(
2871                                                cm_core,
2872                                                cm_info->rem_addr,
2873                                                cm_node->rem_port,
2874                                                cm_node->vlan_id,
2875                                                I40IW_CM_LISTENER_ACTIVE_STATE);
2876                if (!loopback_remotelistener) {
2877                        i40iw_create_event(cm_node, I40IW_CM_EVENT_ABORTED);
2878                } else {
2879                        loopback_cm_info = *cm_info;
2880                        loopback_cm_info.loc_port = cm_info->rem_port;
2881                        loopback_cm_info.rem_port = cm_info->loc_port;
2882                        loopback_cm_info.cm_id = loopback_remotelistener->cm_id;
2883                        loopback_cm_info.ipv4 = cm_info->ipv4;
2884                        loopback_remotenode = i40iw_make_cm_node(cm_core,
2885                                                                 iwdev,
2886                                                                 &loopback_cm_info,
2887                                                                 loopback_remotelistener);
2888                        if (!loopback_remotenode) {
2889                                i40iw_rem_ref_cm_node(cm_node);
2890                                return NULL;
2891                        }
2892                        cm_core->stats_loopbacks++;
2893                        loopback_remotenode->loopbackpartner = cm_node;
2894                        loopback_remotenode->tcp_cntxt.rcv_wscale =
2895                                I40IW_CM_DEFAULT_RCV_WND_SCALE;
2896                        cm_node->loopbackpartner = loopback_remotenode;
2897                        memcpy(loopback_remotenode->pdata_buf, private_data,
2898                               private_data_len);
2899                        loopback_remotenode->pdata.size = private_data_len;
2900
2901                        cm_node->state = I40IW_CM_STATE_OFFLOADED;
2902                        cm_node->tcp_cntxt.rcv_nxt =
2903                                loopback_remotenode->tcp_cntxt.loc_seq_num;
2904                        loopback_remotenode->tcp_cntxt.rcv_nxt =
2905                                cm_node->tcp_cntxt.loc_seq_num;
2906                        cm_node->tcp_cntxt.max_snd_wnd =
2907                                loopback_remotenode->tcp_cntxt.rcv_wnd;
2908                        loopback_remotenode->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
2909                        cm_node->tcp_cntxt.snd_wnd = loopback_remotenode->tcp_cntxt.rcv_wnd;
2910                        loopback_remotenode->tcp_cntxt.snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
2911                        cm_node->tcp_cntxt.snd_wscale = loopback_remotenode->tcp_cntxt.rcv_wscale;
2912                        loopback_remotenode->tcp_cntxt.snd_wscale = cm_node->tcp_cntxt.rcv_wscale;
2913                        loopback_remotenode->state = I40IW_CM_STATE_MPAREQ_RCVD;
2914                        i40iw_create_event(loopback_remotenode, I40IW_CM_EVENT_MPA_REQ);
2915                }
2916                return cm_node;
2917        }
2918
2919        cm_node->pdata.size = private_data_len;
2920        cm_node->pdata.addr = cm_node->pdata_buf;
2921
2922        memcpy(cm_node->pdata_buf, private_data, private_data_len);
2923
2924        cm_node->state = I40IW_CM_STATE_SYN_SENT;
2925        ret = i40iw_send_syn(cm_node, 0);
2926
2927        if (ret) {
2928                if (cm_node->ipv4)
2929                        i40iw_debug(cm_node->dev,
2930                                    I40IW_DEBUG_CM,
2931                                    "Api - connect() FAILED: dest addr=%pI4",
2932                                    cm_node->rem_addr);
2933                else
2934                        i40iw_debug(cm_node->dev, I40IW_DEBUG_CM,
2935                                    "Api - connect() FAILED: dest addr=%pI6",
2936                                    cm_node->rem_addr);
2937                i40iw_rem_ref_cm_node(cm_node);
2938                cm_node = NULL;
2939        }
2940
2941        if (cm_node)
2942                i40iw_debug(cm_node->dev,
2943                            I40IW_DEBUG_CM,
2944                            "Api - connect(): port=0x%04x, cm_node=%p, cm_id = %p.\n",
2945                            cm_node->rem_port,
2946                            cm_node,
2947                            cm_node->cm_id);
2948
2949        return cm_node;
2950}
2951
2952/**
2953 * i40iw_cm_reject - reject and teardown a connection
2954 * @cm_node: connection's node
2955 * @pdate: ptr to private data for reject
2956 * @plen: size of private data
2957 */
2958static int i40iw_cm_reject(struct i40iw_cm_node *cm_node, const void *pdata, u8 plen)
2959{
2960        int ret = 0;
2961        int err;
2962        int passive_state;
2963        struct iw_cm_id *cm_id = cm_node->cm_id;
2964        struct i40iw_cm_node *loopback = cm_node->loopbackpartner;
2965
2966        if (cm_node->tcp_cntxt.client)
2967                return ret;
2968        i40iw_cleanup_retrans_entry(cm_node);
2969
2970        if (!loopback) {
2971                passive_state = atomic_add_return(1, &cm_node->passive_state);
2972                if (passive_state == I40IW_SEND_RESET_EVENT) {
2973                        cm_node->state = I40IW_CM_STATE_CLOSED;
2974                        i40iw_rem_ref_cm_node(cm_node);
2975                } else {
2976                        if (cm_node->state == I40IW_CM_STATE_LISTENER_DESTROYED) {
2977                                i40iw_rem_ref_cm_node(cm_node);
2978                        } else {
2979                                ret = i40iw_send_mpa_reject(cm_node, pdata, plen);
2980                                if (ret) {
2981                                        cm_node->state = I40IW_CM_STATE_CLOSED;
2982                                        err = i40iw_send_reset(cm_node);
2983                                        if (err)
2984                                                i40iw_pr_err("send reset failed\n");
2985                                } else {
2986                                        cm_id->add_ref(cm_id);
2987                                }
2988                        }
2989                }
2990        } else {
2991                cm_node->cm_id = NULL;
2992                if (cm_node->state == I40IW_CM_STATE_LISTENER_DESTROYED) {
2993                        i40iw_rem_ref_cm_node(cm_node);
2994                        i40iw_rem_ref_cm_node(loopback);
2995                } else {
2996                        ret = i40iw_send_cm_event(loopback,
2997                                                  loopback->cm_id,
2998                                                  IW_CM_EVENT_CONNECT_REPLY,
2999                                                  -ECONNREFUSED);
3000                        i40iw_rem_ref_cm_node(cm_node);
3001                        loopback->state = I40IW_CM_STATE_CLOSING;
3002
3003                        cm_id = loopback->cm_id;
3004                        i40iw_rem_ref_cm_node(loopback);
3005                        cm_id->rem_ref(cm_id);
3006                }
3007        }
3008
3009        return ret;
3010}
3011
3012/**
3013 * i40iw_cm_close - close of cm connection
3014 * @cm_node: connection's node
3015 */
3016static int i40iw_cm_close(struct i40iw_cm_node *cm_node)
3017{
3018        int ret = 0;
3019
3020        if (!cm_node)
3021                return -EINVAL;
3022
3023        switch (cm_node->state) {
3024        case I40IW_CM_STATE_SYN_RCVD:
3025        case I40IW_CM_STATE_SYN_SENT:
3026        case I40IW_CM_STATE_ONE_SIDE_ESTABLISHED:
3027        case I40IW_CM_STATE_ESTABLISHED:
3028        case I40IW_CM_STATE_ACCEPTING:
3029        case I40IW_CM_STATE_MPAREQ_SENT:
3030        case I40IW_CM_STATE_MPAREQ_RCVD:
3031                i40iw_cleanup_retrans_entry(cm_node);
3032                i40iw_send_reset(cm_node);
3033                break;
3034        case I40IW_CM_STATE_CLOSE_WAIT:
3035                cm_node->state = I40IW_CM_STATE_LAST_ACK;
3036                i40iw_send_fin(cm_node);
3037                break;
3038        case I40IW_CM_STATE_FIN_WAIT1:
3039        case I40IW_CM_STATE_FIN_WAIT2:
3040        case I40IW_CM_STATE_LAST_ACK:
3041        case I40IW_CM_STATE_TIME_WAIT:
3042        case I40IW_CM_STATE_CLOSING:
3043                ret = -1;
3044                break;
3045        case I40IW_CM_STATE_LISTENING:
3046                i40iw_cleanup_retrans_entry(cm_node);
3047                i40iw_send_reset(cm_node);
3048                break;
3049        case I40IW_CM_STATE_MPAREJ_RCVD:
3050        case I40IW_CM_STATE_UNKNOWN:
3051        case I40IW_CM_STATE_INITED:
3052        case I40IW_CM_STATE_CLOSED:
3053        case I40IW_CM_STATE_LISTENER_DESTROYED:
3054                i40iw_rem_ref_cm_node(cm_node);
3055                break;
3056        case I40IW_CM_STATE_OFFLOADED:
3057                if (cm_node->send_entry)
3058                        i40iw_pr_err("send_entry\n");
3059                i40iw_rem_ref_cm_node(cm_node);
3060                break;
3061        }
3062        return ret;
3063}
3064
3065/**
3066 * i40iw_receive_ilq - recv an ETHERNET packet, and process it
3067 * through CM
3068 * @dev: FPK dev struct
3069 * @rbuf: receive buffer
3070 */
3071void i40iw_receive_ilq(struct i40iw_sc_dev *dev, struct i40iw_puda_buf *rbuf)
3072{
3073        struct i40iw_cm_node *cm_node;
3074        struct i40iw_cm_listener *listener;
3075        struct iphdr *iph;
3076        struct ipv6hdr *ip6h;
3077        struct tcphdr *tcph;
3078        struct i40iw_cm_info cm_info;
3079        struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
3080        struct i40iw_cm_core *cm_core = &iwdev->cm_core;
3081        struct vlan_ethhdr *ethh;
3082
3083        /* if vlan, then maclen = 18 else 14 */
3084        iph = (struct iphdr *)rbuf->iph;
3085        memset(&cm_info, 0, sizeof(cm_info));
3086
3087        i40iw_debug_buf(dev,
3088                        I40IW_DEBUG_ILQ,
3089                        "RECEIVE ILQ BUFFER",
3090                        rbuf->mem.va,
3091                        rbuf->totallen);
3092        ethh = (struct vlan_ethhdr *)rbuf->mem.va;
3093
3094        if (ethh->h_vlan_proto == htons(ETH_P_8021Q)) {
3095                cm_info.vlan_id = ntohs(ethh->h_vlan_TCI) & VLAN_VID_MASK;
3096                i40iw_debug(cm_core->dev,
3097                            I40IW_DEBUG_CM,
3098                            "%s vlan_id=%d\n",
3099                            __func__,
3100                            cm_info.vlan_id);
3101        } else {
3102                cm_info.vlan_id = I40IW_NO_VLAN;
3103        }
3104        tcph = (struct tcphdr *)rbuf->tcph;
3105
3106        if (rbuf->ipv4) {
3107                cm_info.loc_addr[0] = ntohl(iph->daddr);
3108                cm_info.rem_addr[0] = ntohl(iph->saddr);
3109                cm_info.ipv4 = true;
3110        } else {
3111                ip6h = (struct ipv6hdr *)rbuf->iph;
3112                i40iw_copy_ip_ntohl(cm_info.loc_addr,
3113                                    ip6h->daddr.in6_u.u6_addr32);
3114                i40iw_copy_ip_ntohl(cm_info.rem_addr,
3115                                    ip6h->saddr.in6_u.u6_addr32);
3116                cm_info.ipv4 = false;
3117        }
3118        cm_info.loc_port = ntohs(tcph->dest);
3119        cm_info.rem_port = ntohs(tcph->source);
3120        cm_node = i40iw_find_node(cm_core,
3121                                  cm_info.rem_port,
3122                                  cm_info.rem_addr,
3123                                  cm_info.loc_port,
3124                                  cm_info.loc_addr,
3125                                  true);
3126
3127        if (!cm_node) {
3128                /* Only type of packet accepted are for */
3129                /* the PASSIVE open (syn only) */
3130                if (!tcph->syn || tcph->ack)
3131                        return;
3132                listener =
3133                    i40iw_find_listener(cm_core,
3134                                        cm_info.loc_addr,
3135                                        cm_info.loc_port,
3136                                        cm_info.vlan_id,
3137                                        I40IW_CM_LISTENER_ACTIVE_STATE);
3138                if (!listener) {
3139                        cm_info.cm_id = NULL;
3140                        i40iw_debug(cm_core->dev,
3141                                    I40IW_DEBUG_CM,
3142                                    "%s no listener found\n",
3143                                    __func__);
3144                        return;
3145                }
3146                cm_info.cm_id = listener->cm_id;
3147                cm_node = i40iw_make_cm_node(cm_core, iwdev, &cm_info, listener);
3148                if (!cm_node) {
3149                        i40iw_debug(cm_core->dev,
3150                                    I40IW_DEBUG_CM,
3151                                    "%s allocate node failed\n",
3152                                    __func__);
3153                        atomic_dec(&listener->ref_count);
3154                        return;
3155                }
3156                if (!tcph->rst && !tcph->fin) {
3157                        cm_node->state = I40IW_CM_STATE_LISTENING;
3158                } else {
3159                        i40iw_rem_ref_cm_node(cm_node);
3160                        return;
3161                }
3162                atomic_inc(&cm_node->ref_count);
3163        } else if (cm_node->state == I40IW_CM_STATE_OFFLOADED) {
3164                i40iw_rem_ref_cm_node(cm_node);
3165                return;
3166        }
3167        i40iw_process_packet(cm_node, rbuf);
3168        i40iw_rem_ref_cm_node(cm_node);
3169}
3170
3171/**
3172 * i40iw_setup_cm_core - allocate a top level instance of a cm
3173 * core
3174 * @iwdev: iwarp device structure
3175 */
3176void i40iw_setup_cm_core(struct i40iw_device *iwdev)
3177{
3178        struct i40iw_cm_core *cm_core = &iwdev->cm_core;
3179
3180        cm_core->iwdev = iwdev;
3181        cm_core->dev = &iwdev->sc_dev;
3182
3183        INIT_LIST_HEAD(&cm_core->connected_nodes);
3184        INIT_LIST_HEAD(&cm_core->listen_nodes);
3185
3186        init_timer(&cm_core->tcp_timer);
3187        cm_core->tcp_timer.function = i40iw_cm_timer_tick;
3188        cm_core->tcp_timer.data = (unsigned long)cm_core;
3189
3190        spin_lock_init(&cm_core->ht_lock);
3191        spin_lock_init(&cm_core->listen_list_lock);
3192
3193        cm_core->event_wq = create_singlethread_workqueue("iwewq");
3194        cm_core->disconn_wq = create_singlethread_workqueue("iwdwq");
3195}
3196
3197/**
3198 * i40iw_cleanup_cm_core - deallocate a top level instance of a
3199 * cm core
3200 * @cm_core: cm's core
3201 */
3202void i40iw_cleanup_cm_core(struct i40iw_cm_core *cm_core)
3203{
3204        unsigned long flags;
3205
3206        if (!cm_core)
3207                return;
3208
3209        spin_lock_irqsave(&cm_core->ht_lock, flags);
3210        if (timer_pending(&cm_core->tcp_timer))
3211                del_timer_sync(&cm_core->tcp_timer);
3212        spin_unlock_irqrestore(&cm_core->ht_lock, flags);
3213
3214        destroy_workqueue(cm_core->event_wq);
3215        destroy_workqueue(cm_core->disconn_wq);
3216}
3217
3218/**
3219 * i40iw_init_tcp_ctx - setup qp context
3220 * @cm_node: connection's node
3221 * @tcp_info: offload info for tcp
3222 * @iwqp: associate qp for the connection
3223 */
3224static void i40iw_init_tcp_ctx(struct i40iw_cm_node *cm_node,
3225                               struct i40iw_tcp_offload_info *tcp_info,
3226                               struct i40iw_qp *iwqp)
3227{
3228        tcp_info->ipv4 = cm_node->ipv4;
3229        tcp_info->drop_ooo_seg = true;
3230        tcp_info->wscale = true;
3231        tcp_info->ignore_tcp_opt = true;
3232        tcp_info->ignore_tcp_uns_opt = true;
3233        tcp_info->no_nagle = false;
3234
3235        tcp_info->ttl = I40IW_DEFAULT_TTL;
3236        tcp_info->rtt_var = cpu_to_le32(I40IW_DEFAULT_RTT_VAR);
3237        tcp_info->ss_thresh = cpu_to_le32(I40IW_DEFAULT_SS_THRESH);
3238        tcp_info->rexmit_thresh = I40IW_DEFAULT_REXMIT_THRESH;
3239
3240        tcp_info->tcp_state = I40IW_TCP_STATE_ESTABLISHED;
3241        tcp_info->snd_wscale = cm_node->tcp_cntxt.snd_wscale;
3242        tcp_info->rcv_wscale = cm_node->tcp_cntxt.rcv_wscale;
3243
3244        tcp_info->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
3245        tcp_info->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
3246        tcp_info->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
3247        tcp_info->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
3248
3249        tcp_info->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
3250        tcp_info->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
3251        tcp_info->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
3252        tcp_info->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
3253        tcp_info->max_snd_window = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
3254        tcp_info->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
3255                                        cm_node->tcp_cntxt.rcv_wscale);
3256
3257        tcp_info->flow_label = 0;
3258        tcp_info->snd_mss = cpu_to_le32(((u32)cm_node->tcp_cntxt.mss));
3259        if (cm_node->vlan_id < VLAN_TAG_PRESENT) {
3260                tcp_info->insert_vlan_tag = true;
3261                tcp_info->vlan_tag = cpu_to_le16(cm_node->vlan_id);
3262        }
3263        if (cm_node->ipv4) {
3264                tcp_info->src_port = cpu_to_le16(cm_node->loc_port);
3265                tcp_info->dst_port = cpu_to_le16(cm_node->rem_port);
3266
3267                tcp_info->dest_ip_addr3 = cpu_to_le32(cm_node->rem_addr[0]);
3268                tcp_info->local_ipaddr3 = cpu_to_le32(cm_node->loc_addr[0]);
3269                tcp_info->arp_idx = cpu_to_le32(i40iw_arp_table(iwqp->iwdev,
3270                                                                &tcp_info->dest_ip_addr3,
3271                                                                true,
3272                                                                NULL,
3273                                                                I40IW_ARP_RESOLVE));
3274        } else {
3275                tcp_info->src_port = cpu_to_le16(cm_node->loc_port);
3276                tcp_info->dst_port = cpu_to_le16(cm_node->rem_port);
3277                tcp_info->dest_ip_addr0 = cpu_to_le32(cm_node->rem_addr[0]);
3278                tcp_info->dest_ip_addr1 = cpu_to_le32(cm_node->rem_addr[1]);
3279                tcp_info->dest_ip_addr2 = cpu_to_le32(cm_node->rem_addr[2]);
3280                tcp_info->dest_ip_addr3 = cpu_to_le32(cm_node->rem_addr[3]);
3281                tcp_info->local_ipaddr0 = cpu_to_le32(cm_node->loc_addr[0]);
3282                tcp_info->local_ipaddr1 = cpu_to_le32(cm_node->loc_addr[1]);
3283                tcp_info->local_ipaddr2 = cpu_to_le32(cm_node->loc_addr[2]);
3284                tcp_info->local_ipaddr3 = cpu_to_le32(cm_node->loc_addr[3]);
3285                tcp_info->arp_idx = cpu_to_le32(i40iw_arp_table(
3286                                                        iwqp->iwdev,
3287                                                        &tcp_info->dest_ip_addr0,
3288                                                        false,
3289                                                        NULL,
3290                                                        I40IW_ARP_RESOLVE));
3291        }
3292}
3293
3294/**
3295 * i40iw_cm_init_tsa_conn - setup qp for RTS
3296 * @iwqp: associate qp for the connection
3297 * @cm_node: connection's node
3298 */
3299static void i40iw_cm_init_tsa_conn(struct i40iw_qp *iwqp,
3300                                   struct i40iw_cm_node *cm_node)
3301{
3302        struct i40iw_tcp_offload_info tcp_info;
3303        struct i40iwarp_offload_info *iwarp_info;
3304        struct i40iw_qp_host_ctx_info *ctx_info;
3305        struct i40iw_device *iwdev = iwqp->iwdev;
3306        struct i40iw_sc_dev *dev = &iwqp->iwdev->sc_dev;
3307
3308        memset(&tcp_info, 0x00, sizeof(struct i40iw_tcp_offload_info));
3309        iwarp_info = &iwqp->iwarp_info;
3310        ctx_info = &iwqp->ctx_info;
3311
3312        ctx_info->tcp_info = &tcp_info;
3313        ctx_info->send_cq_num = iwqp->iwscq->sc_cq.cq_uk.cq_id;
3314        ctx_info->rcv_cq_num = iwqp->iwrcq->sc_cq.cq_uk.cq_id;
3315
3316        iwarp_info->ord_size = cm_node->ord_size;
3317        iwarp_info->ird_size = i40iw_derive_hw_ird_setting(cm_node->ird_size);
3318
3319        if (iwarp_info->ord_size == 1)
3320                iwarp_info->ord_size = 2;
3321
3322        iwarp_info->rd_enable = true;
3323        iwarp_info->rdmap_ver = 1;
3324        iwarp_info->ddp_ver = 1;
3325
3326        iwarp_info->pd_id = iwqp->iwpd->sc_pd.pd_id;
3327
3328        ctx_info->tcp_info_valid = true;
3329        ctx_info->iwarp_info_valid = true;
3330
3331        i40iw_init_tcp_ctx(cm_node, &tcp_info, iwqp);
3332        if (cm_node->snd_mark_en) {
3333                iwarp_info->snd_mark_en = true;
3334                iwarp_info->snd_mark_offset = (tcp_info.snd_nxt &
3335                                SNDMARKER_SEQNMASK) + cm_node->lsmm_size;
3336        }
3337
3338        cm_node->state = I40IW_CM_STATE_OFFLOADED;
3339        tcp_info.tcp_state = I40IW_TCP_STATE_ESTABLISHED;
3340        tcp_info.src_mac_addr_idx = iwdev->mac_ip_table_idx;
3341
3342        dev->iw_priv_qp_ops->qp_setctx(&iwqp->sc_qp, (u64 *)(iwqp->host_ctx.va), ctx_info);
3343
3344        /* once tcp_info is set, no need to do it again */
3345        ctx_info->tcp_info_valid = false;
3346        ctx_info->iwarp_info_valid = false;
3347}
3348
3349/**
3350 * i40iw_cm_disconn - when a connection is being closed
3351 * @iwqp: associate qp for the connection
3352 */
3353int i40iw_cm_disconn(struct i40iw_qp *iwqp)
3354{
3355        struct disconn_work *work;
3356        struct i40iw_device *iwdev = iwqp->iwdev;
3357        struct i40iw_cm_core *cm_core = &iwdev->cm_core;
3358
3359        work = kzalloc(sizeof(*work), GFP_ATOMIC);
3360        if (!work)
3361                return -ENOMEM; /* Timer will clean up */
3362
3363        i40iw_add_ref(&iwqp->ibqp);
3364        work->iwqp = iwqp;
3365        INIT_WORK(&work->work, i40iw_disconnect_worker);
3366        queue_work(cm_core->disconn_wq, &work->work);
3367        return 0;
3368}
3369
3370/**
3371 * i40iw_loopback_nop - Send a nop
3372 * @qp: associated hw qp
3373 */
3374static void i40iw_loopback_nop(struct i40iw_sc_qp *qp)
3375{
3376        u64 *wqe;
3377        u64 header;
3378
3379        wqe = qp->qp_uk.sq_base->elem;
3380        set_64bit_val(wqe, 0, 0);
3381        set_64bit_val(wqe, 8, 0);
3382        set_64bit_val(wqe, 16, 0);
3383
3384        header = LS_64(I40IWQP_OP_NOP, I40IWQPSQ_OPCODE) |
3385            LS_64(0, I40IWQPSQ_SIGCOMPL) |
3386            LS_64(qp->qp_uk.swqe_polarity, I40IWQPSQ_VALID);
3387        set_64bit_val(wqe, 24, header);
3388}
3389
3390/**
3391 * i40iw_qp_disconnect - free qp and close cm
3392 * @iwqp: associate qp for the connection
3393 */
3394static void i40iw_qp_disconnect(struct i40iw_qp *iwqp)
3395{
3396        struct i40iw_device *iwdev;
3397        struct i40iw_ib_device *iwibdev;
3398
3399        iwdev = to_iwdev(iwqp->ibqp.device);
3400        if (!iwdev) {
3401                i40iw_pr_err("iwdev == NULL\n");
3402                return;
3403        }
3404
3405        iwibdev = iwdev->iwibdev;
3406
3407        if (iwqp->active_conn) {
3408                /* indicate this connection is NOT active */
3409                iwqp->active_conn = 0;
3410        } else {
3411                /* Need to free the Last Streaming Mode Message */
3412                if (iwqp->ietf_mem.va) {
3413                        if (iwqp->lsmm_mr)
3414                                iwibdev->ibdev.dereg_mr(iwqp->lsmm_mr);
3415                        i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwqp->ietf_mem);
3416                }
3417        }
3418
3419        /* close the CM node down if it is still active */
3420        if (iwqp->cm_node) {
3421                i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_CM, "%s Call close API\n", __func__);
3422                i40iw_cm_close(iwqp->cm_node);
3423        }
3424}
3425
3426/**
3427 * i40iw_cm_disconn_true - called by worker thread to disconnect qp
3428 * @iwqp: associate qp for the connection
3429 */
3430static void i40iw_cm_disconn_true(struct i40iw_qp *iwqp)
3431{
3432        struct iw_cm_id *cm_id;
3433        struct i40iw_device *iwdev;
3434        struct i40iw_sc_qp *qp = &iwqp->sc_qp;
3435        u16 last_ae;
3436        u8 original_hw_tcp_state;
3437        u8 original_ibqp_state;
3438        int disconn_status = 0;
3439        int issue_disconn = 0;
3440        int issue_close = 0;
3441        int issue_flush = 0;
3442        struct ib_event ibevent;
3443        unsigned long flags;
3444        int ret;
3445
3446        if (!iwqp) {
3447                i40iw_pr_err("iwqp == NULL\n");
3448                return;
3449        }
3450
3451        spin_lock_irqsave(&iwqp->lock, flags);
3452        cm_id = iwqp->cm_id;
3453        /* make sure we havent already closed this connection */
3454        if (!cm_id) {
3455                spin_unlock_irqrestore(&iwqp->lock, flags);
3456                return;
3457        }
3458
3459        iwdev = to_iwdev(iwqp->ibqp.device);
3460
3461        original_hw_tcp_state = iwqp->hw_tcp_state;
3462        original_ibqp_state = iwqp->ibqp_state;
3463        last_ae = iwqp->last_aeq;
3464
3465        if (qp->term_flags) {
3466                issue_disconn = 1;
3467                issue_close = 1;
3468                iwqp->cm_id = NULL;
3469                /*When term timer expires after cm_timer, don't want
3470                 *terminate-handler to issue cm_disconn which can re-free
3471                 *a QP even after its refcnt=0.
3472                 */
3473                del_timer(&iwqp->terminate_timer);
3474                if (!iwqp->flush_issued) {
3475                        iwqp->flush_issued = 1;
3476                        issue_flush = 1;
3477                }
3478        } else if ((original_hw_tcp_state == I40IW_TCP_STATE_CLOSE_WAIT) ||
3479                   ((original_ibqp_state == IB_QPS_RTS) &&
3480                    (last_ae == I40IW_AE_LLP_CONNECTION_RESET))) {
3481                issue_disconn = 1;
3482                if (last_ae == I40IW_AE_LLP_CONNECTION_RESET)
3483                        disconn_status = -ECONNRESET;
3484        }
3485
3486        if (((original_hw_tcp_state == I40IW_TCP_STATE_CLOSED) ||
3487             (original_hw_tcp_state == I40IW_TCP_STATE_TIME_WAIT) ||
3488             (last_ae == I40IW_AE_RDMAP_ROE_BAD_LLP_CLOSE) ||
3489             (last_ae == I40IW_AE_LLP_CONNECTION_RESET))) {
3490                issue_close = 1;
3491                iwqp->cm_id = NULL;
3492                if (!iwqp->flush_issued) {
3493                        iwqp->flush_issued = 1;
3494                        issue_flush = 1;
3495                }
3496        }
3497
3498        spin_unlock_irqrestore(&iwqp->lock, flags);
3499        if (issue_flush && !iwqp->destroyed) {
3500                /* Flush the queues */
3501                i40iw_flush_wqes(iwdev, iwqp);
3502
3503                if (qp->term_flags) {
3504                        ibevent.device = iwqp->ibqp.device;
3505                        ibevent.event = (qp->eventtype == TERM_EVENT_QP_FATAL) ?
3506                                        IB_EVENT_QP_FATAL : IB_EVENT_QP_ACCESS_ERR;
3507                        ibevent.element.qp = &iwqp->ibqp;
3508                        iwqp->ibqp.event_handler(&ibevent, iwqp->ibqp.qp_context);
3509                }
3510        }
3511
3512        if (cm_id && cm_id->event_handler) {
3513                if (issue_disconn) {
3514                        ret = i40iw_send_cm_event(NULL,
3515                                                  cm_id,
3516                                                  IW_CM_EVENT_DISCONNECT,
3517                                                  disconn_status);
3518
3519                        if (ret)
3520                                i40iw_debug(&iwdev->sc_dev,
3521                                            I40IW_DEBUG_CM,
3522                                            "disconnect event failed %s: - cm_id = %p\n",
3523                                            __func__, cm_id);
3524                }
3525                if (issue_close) {
3526                        i40iw_qp_disconnect(iwqp);
3527                        cm_id->provider_data = iwqp;
3528                        ret = i40iw_send_cm_event(NULL, cm_id, IW_CM_EVENT_CLOSE, 0);
3529                        if (ret)
3530                                i40iw_debug(&iwdev->sc_dev,
3531                                            I40IW_DEBUG_CM,
3532                                            "close event failed %s: - cm_id = %p\n",
3533                                            __func__, cm_id);
3534                        cm_id->rem_ref(cm_id);
3535                }
3536        }
3537}
3538
3539/**
3540 * i40iw_disconnect_worker - worker for connection close
3541 * @work: points or disconn structure
3542 */
3543static void i40iw_disconnect_worker(struct work_struct *work)
3544{
3545        struct disconn_work *dwork = container_of(work, struct disconn_work, work);
3546        struct i40iw_qp *iwqp = dwork->iwqp;
3547
3548        kfree(dwork);
3549        i40iw_cm_disconn_true(iwqp);
3550        i40iw_rem_ref(&iwqp->ibqp);
3551}
3552
3553/**
3554 * i40iw_accept - registered call for connection to be accepted
3555 * @cm_id: cm information for passive connection
3556 * @conn_param: accpet parameters
3557 */
3558int i40iw_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3559{
3560        struct ib_qp *ibqp;
3561        struct i40iw_qp *iwqp;
3562        struct i40iw_device *iwdev;
3563        struct i40iw_sc_dev *dev;
3564        struct i40iw_cm_node *cm_node;
3565        struct ib_qp_attr attr;
3566        int passive_state;
3567        struct i40iw_ib_device *iwibdev;
3568        struct ib_mr *ibmr;
3569        struct i40iw_pd *iwpd;
3570        u16 buf_len = 0;
3571        struct i40iw_kmem_info accept;
3572        enum i40iw_status_code status;
3573        u64 tagged_offset;
3574
3575        memset(&attr, 0, sizeof(attr));
3576        ibqp = i40iw_get_qp(cm_id->device, conn_param->qpn);
3577        if (!ibqp)
3578                return -EINVAL;
3579
3580        iwqp = to_iwqp(ibqp);
3581        iwdev = iwqp->iwdev;
3582        dev = &iwdev->sc_dev;
3583        cm_node = (struct i40iw_cm_node *)cm_id->provider_data;
3584
3585        if (((struct sockaddr_in *)&cm_id->local_addr)->sin_family == AF_INET) {
3586                cm_node->ipv4 = true;
3587                cm_node->vlan_id = i40iw_get_vlan_ipv4(cm_node->loc_addr);
3588        } else {
3589                cm_node->ipv4 = false;
3590                i40iw_netdev_vlan_ipv6(cm_node->loc_addr, &cm_node->vlan_id, NULL);
3591        }
3592        i40iw_debug(cm_node->dev,
3593                    I40IW_DEBUG_CM,
3594                    "Accept vlan_id=%d\n",
3595                    cm_node->vlan_id);
3596        if (cm_node->state == I40IW_CM_STATE_LISTENER_DESTROYED) {
3597                if (cm_node->loopbackpartner)
3598                        i40iw_rem_ref_cm_node(cm_node->loopbackpartner);
3599                i40iw_rem_ref_cm_node(cm_node);
3600                return -EINVAL;
3601        }
3602
3603        passive_state = atomic_add_return(1, &cm_node->passive_state);
3604        if (passive_state == I40IW_SEND_RESET_EVENT) {
3605                i40iw_rem_ref_cm_node(cm_node);
3606                return -ECONNRESET;
3607        }
3608
3609        cm_node->cm_core->stats_accepts++;
3610        iwqp->cm_node = (void *)cm_node;
3611        cm_node->iwqp = iwqp;
3612
3613        buf_len = conn_param->private_data_len + I40IW_MAX_IETF_SIZE + MPA_ZERO_PAD_LEN;
3614
3615        status = i40iw_allocate_dma_mem(dev->hw, &iwqp->ietf_mem, buf_len, 1);
3616
3617        if (status)
3618                return -ENOMEM;
3619        cm_node->pdata.size = conn_param->private_data_len;
3620        accept.addr = iwqp->ietf_mem.va;
3621        accept.size = i40iw_cm_build_mpa_frame(cm_node, &accept, MPA_KEY_REPLY);
3622        memcpy(accept.addr + accept.size, conn_param->private_data,
3623               conn_param->private_data_len);
3624
3625        /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3626        if ((cm_node->ipv4 &&
3627             !i40iw_ipv4_is_loopback(cm_node->loc_addr[0], cm_node->rem_addr[0])) ||
3628            (!cm_node->ipv4 &&
3629             !i40iw_ipv6_is_loopback(cm_node->loc_addr, cm_node->rem_addr))) {
3630                iwibdev = iwdev->iwibdev;
3631                iwpd = iwqp->iwpd;
3632                tagged_offset = (uintptr_t)iwqp->ietf_mem.va;
3633                ibmr = i40iw_reg_phys_mr(&iwpd->ibpd,
3634                                         iwqp->ietf_mem.pa,
3635                                         buf_len,
3636                                         IB_ACCESS_LOCAL_WRITE,
3637                                         &tagged_offset);
3638                if (IS_ERR(ibmr)) {
3639                        i40iw_free_dma_mem(dev->hw, &iwqp->ietf_mem);
3640                        return -ENOMEM;
3641                }
3642
3643                ibmr->pd = &iwpd->ibpd;
3644                ibmr->device = iwpd->ibpd.device;
3645                iwqp->lsmm_mr = ibmr;
3646                if (iwqp->page)
3647                        iwqp->sc_qp.qp_uk.sq_base = kmap(iwqp->page);
3648                if (is_remote_ne020_or_chelsio(cm_node))
3649                        dev->iw_priv_qp_ops->qp_send_lsmm(
3650                                                        &iwqp->sc_qp,
3651                                                        iwqp->ietf_mem.va,
3652                                                        (accept.size + conn_param->private_data_len),
3653                                                        ibmr->lkey);
3654                else
3655                        dev->iw_priv_qp_ops->qp_send_lsmm(
3656                                                        &iwqp->sc_qp,
3657                                                        iwqp->ietf_mem.va,
3658                                                        (accept.size + conn_param->private_data_len + MPA_ZERO_PAD_LEN),
3659                                                        ibmr->lkey);
3660
3661        } else {
3662                if (iwqp->page)
3663                        iwqp->sc_qp.qp_uk.sq_base = kmap(iwqp->page);
3664                i40iw_loopback_nop(&iwqp->sc_qp);
3665        }
3666
3667        if (iwqp->page)
3668                kunmap(iwqp->page);
3669
3670        iwqp->cm_id = cm_id;
3671        cm_node->cm_id = cm_id;
3672
3673        cm_id->provider_data = (void *)iwqp;
3674        iwqp->active_conn = 0;
3675
3676        cm_node->lsmm_size = accept.size + conn_param->private_data_len;
3677        i40iw_cm_init_tsa_conn(iwqp, cm_node);
3678        cm_id->add_ref(cm_id);
3679        i40iw_add_ref(&iwqp->ibqp);
3680
3681        i40iw_send_cm_event(cm_node, cm_id, IW_CM_EVENT_ESTABLISHED, 0);
3682
3683        attr.qp_state = IB_QPS_RTS;
3684        cm_node->qhash_set = false;
3685        i40iw_modify_qp(&iwqp->ibqp, &attr, IB_QP_STATE, NULL);
3686        if (cm_node->loopbackpartner) {
3687                cm_node->loopbackpartner->pdata.size = conn_param->private_data_len;
3688
3689                /* copy entire MPA frame to our cm_node's frame */
3690                memcpy(cm_node->loopbackpartner->pdata_buf,
3691                       conn_param->private_data,
3692                       conn_param->private_data_len);
3693                i40iw_create_event(cm_node->loopbackpartner, I40IW_CM_EVENT_CONNECTED);
3694        }
3695
3696        cm_node->accelerated = 1;
3697        if (cm_node->accept_pend) {
3698                if (!cm_node->listener)
3699                        i40iw_pr_err("cm_node->listener NULL for passive node\n");
3700                atomic_dec(&cm_node->listener->pend_accepts_cnt);
3701                cm_node->accept_pend = 0;
3702        }
3703        return 0;
3704}
3705
3706/**
3707 * i40iw_reject - registered call for connection to be rejected
3708 * @cm_id: cm information for passive connection
3709 * @pdata: private data to be sent
3710 * @pdata_len: private data length
3711 */
3712int i40iw_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3713{
3714        struct i40iw_device *iwdev;
3715        struct i40iw_cm_node *cm_node;
3716        struct i40iw_cm_node *loopback;
3717
3718        cm_node = (struct i40iw_cm_node *)cm_id->provider_data;
3719        loopback = cm_node->loopbackpartner;
3720        cm_node->cm_id = cm_id;
3721        cm_node->pdata.size = pdata_len;
3722
3723        iwdev = to_iwdev(cm_id->device);
3724        if (!iwdev)
3725                return -EINVAL;
3726        cm_node->cm_core->stats_rejects++;
3727
3728        if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
3729                return -EINVAL;
3730
3731        if (loopback) {
3732                memcpy(&loopback->pdata_buf, pdata, pdata_len);
3733                loopback->pdata.size = pdata_len;
3734        }
3735
3736        return i40iw_cm_reject(cm_node, pdata, pdata_len);
3737}
3738
3739/**
3740 * i40iw_connect - registered call for connection to be established
3741 * @cm_id: cm information for passive connection
3742 * @conn_param: Information about the connection
3743 */
3744int i40iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3745{
3746        struct ib_qp *ibqp;
3747        struct i40iw_qp *iwqp;
3748        struct i40iw_device *iwdev;
3749        struct i40iw_cm_node *cm_node;
3750        struct i40iw_cm_info cm_info;
3751        struct sockaddr_in *laddr;
3752        struct sockaddr_in *raddr;
3753        struct sockaddr_in6 *laddr6;
3754        struct sockaddr_in6 *raddr6;
3755        int apbvt_set = 0;
3756        enum i40iw_status_code status;
3757
3758        ibqp = i40iw_get_qp(cm_id->device, conn_param->qpn);
3759        if (!ibqp)
3760                return -EINVAL;
3761        iwqp = to_iwqp(ibqp);
3762        if (!iwqp)
3763                return -EINVAL;
3764        iwdev = to_iwdev(iwqp->ibqp.device);
3765        if (!iwdev)
3766                return -EINVAL;
3767
3768        laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3769        raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
3770        laddr6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
3771        raddr6 = (struct sockaddr_in6 *)&cm_id->m_remote_addr;
3772
3773        if (!(laddr->sin_port) || !(raddr->sin_port))
3774                return -EINVAL;
3775
3776        iwqp->active_conn = 1;
3777        iwqp->cm_id = NULL;
3778        cm_id->provider_data = iwqp;
3779
3780        /* set up the connection params for the node */
3781        if (cm_id->remote_addr.ss_family == AF_INET) {
3782                cm_info.ipv4 = true;
3783                memset(cm_info.loc_addr, 0, sizeof(cm_info.loc_addr));
3784                memset(cm_info.rem_addr, 0, sizeof(cm_info.rem_addr));
3785                cm_info.loc_addr[0] = ntohl(laddr->sin_addr.s_addr);
3786                cm_info.rem_addr[0] = ntohl(raddr->sin_addr.s_addr);
3787                cm_info.loc_port = ntohs(laddr->sin_port);
3788                cm_info.rem_port = ntohs(raddr->sin_port);
3789                cm_info.vlan_id = i40iw_get_vlan_ipv4(cm_info.loc_addr);
3790        } else {
3791                cm_info.ipv4 = false;
3792                i40iw_copy_ip_ntohl(cm_info.loc_addr,
3793                                    laddr6->sin6_addr.in6_u.u6_addr32);
3794                i40iw_copy_ip_ntohl(cm_info.rem_addr,
3795                                    raddr6->sin6_addr.in6_u.u6_addr32);
3796                cm_info.loc_port = ntohs(laddr6->sin6_port);
3797                cm_info.rem_port = ntohs(raddr6->sin6_port);
3798                i40iw_netdev_vlan_ipv6(cm_info.loc_addr, &cm_info.vlan_id, NULL);
3799        }
3800        cm_info.cm_id = cm_id;
3801        if ((cm_info.ipv4 && (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr)) ||
3802            (!cm_info.ipv4 && memcmp(laddr6->sin6_addr.in6_u.u6_addr32,
3803                                     raddr6->sin6_addr.in6_u.u6_addr32,
3804                                     sizeof(laddr6->sin6_addr.in6_u.u6_addr32)))) {
3805                status = i40iw_manage_qhash(iwdev,
3806                                            &cm_info,
3807                                            I40IW_QHASH_TYPE_TCP_ESTABLISHED,
3808                                            I40IW_QHASH_MANAGE_TYPE_ADD,
3809                                            NULL,
3810                                            true);
3811                if (status)
3812                        return -EINVAL;
3813        }
3814        status = i40iw_manage_apbvt(iwdev, cm_info.loc_port, I40IW_MANAGE_APBVT_ADD);
3815        if (status) {
3816                i40iw_manage_qhash(iwdev,
3817                                   &cm_info,
3818                                   I40IW_QHASH_TYPE_TCP_ESTABLISHED,
3819                                   I40IW_QHASH_MANAGE_TYPE_DELETE,
3820                                   NULL,
3821                                   false);
3822                return -EINVAL;
3823        }
3824
3825        apbvt_set = 1;
3826        cm_id->add_ref(cm_id);
3827        cm_node = i40iw_create_cm_node(&iwdev->cm_core, iwdev,
3828                                       conn_param->private_data_len,
3829                                       (void *)conn_param->private_data,
3830                                       &cm_info);
3831        if (!cm_node) {
3832                i40iw_manage_qhash(iwdev,
3833                                   &cm_info,
3834                                   I40IW_QHASH_TYPE_TCP_ESTABLISHED,
3835                                   I40IW_QHASH_MANAGE_TYPE_DELETE,
3836                                   NULL,
3837                                   false);
3838
3839                if (apbvt_set && !i40iw_listen_port_in_use(&iwdev->cm_core,
3840                                                           cm_info.loc_port))
3841                        i40iw_manage_apbvt(iwdev,
3842                                           cm_info.loc_port,
3843                                           I40IW_MANAGE_APBVT_DEL);
3844                cm_id->rem_ref(cm_id);
3845                iwdev->cm_core.stats_connect_errs++;
3846                return -ENOMEM;
3847        }
3848
3849        i40iw_record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3850        if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
3851            !cm_node->ord_size)
3852                cm_node->ord_size = 1;
3853
3854        cm_node->apbvt_set = apbvt_set;
3855        cm_node->qhash_set = true;
3856        iwqp->cm_node = cm_node;
3857        cm_node->iwqp = iwqp;
3858        iwqp->cm_id = cm_id;
3859        i40iw_add_ref(&iwqp->ibqp);
3860        return 0;
3861}
3862
3863/**
3864 * i40iw_create_listen - registered call creating listener
3865 * @cm_id: cm information for passive connection
3866 * @backlog: to max accept pending count
3867 */
3868int i40iw_create_listen(struct iw_cm_id *cm_id, int backlog)
3869{
3870        struct i40iw_device *iwdev;
3871        struct i40iw_cm_listener *cm_listen_node;
3872        struct i40iw_cm_info cm_info;
3873        enum i40iw_status_code ret;
3874        struct sockaddr_in *laddr;
3875        struct sockaddr_in6 *laddr6;
3876        bool wildcard = false;
3877
3878        iwdev = to_iwdev(cm_id->device);
3879        if (!iwdev)
3880                return -EINVAL;
3881
3882        laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3883        laddr6 = (struct sockaddr_in6 *)&cm_id->m_local_addr;
3884        memset(&cm_info, 0, sizeof(cm_info));
3885        if (laddr->sin_family == AF_INET) {
3886                cm_info.ipv4 = true;
3887                cm_info.loc_addr[0] = ntohl(laddr->sin_addr.s_addr);
3888                cm_info.loc_port = ntohs(laddr->sin_port);
3889
3890                if (laddr->sin_addr.s_addr != INADDR_ANY)
3891                        cm_info.vlan_id = i40iw_get_vlan_ipv4(cm_info.loc_addr);
3892                else
3893                        wildcard = true;
3894
3895        } else {
3896                cm_info.ipv4 = false;
3897                i40iw_copy_ip_ntohl(cm_info.loc_addr,
3898                                    laddr6->sin6_addr.in6_u.u6_addr32);
3899                cm_info.loc_port = ntohs(laddr6->sin6_port);
3900                if (ipv6_addr_type(&laddr6->sin6_addr) != IPV6_ADDR_ANY)
3901                        i40iw_netdev_vlan_ipv6(cm_info.loc_addr,
3902                                               &cm_info.vlan_id,
3903                                               NULL);
3904                else
3905                        wildcard = true;
3906        }
3907        cm_info.backlog = backlog;
3908        cm_info.cm_id = cm_id;
3909
3910        cm_listen_node = i40iw_make_listen_node(&iwdev->cm_core, iwdev, &cm_info);
3911        if (!cm_listen_node) {
3912                i40iw_pr_err("cm_listen_node == NULL\n");
3913                return -ENOMEM;
3914        }
3915
3916        cm_id->provider_data = cm_listen_node;
3917
3918        if (!cm_listen_node->reused_node) {
3919                if (wildcard) {
3920                        if (cm_info.ipv4)
3921                                ret = i40iw_add_mqh_4(iwdev,
3922                                                      &cm_info,
3923                                                      cm_listen_node);
3924                        else
3925                                ret = i40iw_add_mqh_6(iwdev,
3926                                                      &cm_info,
3927                                                      cm_listen_node);
3928                        if (ret)
3929                                goto error;
3930
3931                        ret = i40iw_manage_apbvt(iwdev,
3932                                                 cm_info.loc_port,
3933                                                 I40IW_MANAGE_APBVT_ADD);
3934
3935                        if (ret)
3936                                goto error;
3937                } else {
3938                        ret = i40iw_manage_qhash(iwdev,
3939                                                 &cm_info,
3940                                                 I40IW_QHASH_TYPE_TCP_SYN,
3941                                                 I40IW_QHASH_MANAGE_TYPE_ADD,
3942                                                 NULL,
3943                                                 true);
3944                        if (ret)
3945                                goto error;
3946                        cm_listen_node->qhash_set = true;
3947                        ret = i40iw_manage_apbvt(iwdev,
3948                                                 cm_info.loc_port,
3949                                                 I40IW_MANAGE_APBVT_ADD);
3950                        if (ret)
3951                                goto error;
3952                }
3953        }
3954        cm_id->add_ref(cm_id);
3955        cm_listen_node->cm_core->stats_listen_created++;
3956        return 0;
3957 error:
3958        i40iw_cm_del_listen(&iwdev->cm_core, (void *)cm_listen_node, false);
3959        return -EINVAL;
3960}
3961
3962/**
3963 * i40iw_destroy_listen - registered call to destroy listener
3964 * @cm_id: cm information for passive connection
3965 */
3966int i40iw_destroy_listen(struct iw_cm_id *cm_id)
3967{
3968        struct i40iw_device *iwdev;
3969
3970        iwdev = to_iwdev(cm_id->device);
3971        if (cm_id->provider_data)
3972                i40iw_cm_del_listen(&iwdev->cm_core, cm_id->provider_data, true);
3973        else
3974                i40iw_pr_err("cm_id->provider_data was NULL\n");
3975
3976        cm_id->rem_ref(cm_id);
3977
3978        return 0;
3979}
3980
3981/**
3982 * i40iw_cm_event_connected - handle connected active node
3983 * @event: the info for cm_node of connection
3984 */
3985static void i40iw_cm_event_connected(struct i40iw_cm_event *event)
3986{
3987        struct i40iw_qp *iwqp;
3988        struct i40iw_device *iwdev;
3989        struct i40iw_cm_node *cm_node;
3990        struct i40iw_sc_dev *dev;
3991        struct ib_qp_attr attr;
3992        struct iw_cm_id *cm_id;
3993        int status;
3994        bool read0;
3995
3996        cm_node = event->cm_node;
3997        cm_id = cm_node->cm_id;
3998        iwqp = (struct i40iw_qp *)cm_id->provider_data;
3999        iwdev = to_iwdev(iwqp->ibqp.device);
4000        dev = &iwdev->sc_dev;
4001
4002        if (iwqp->destroyed) {
4003                status = -ETIMEDOUT;
4004                goto error;
4005        }
4006        i40iw_cm_init_tsa_conn(iwqp, cm_node);
4007        read0 = (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO);
4008        if (iwqp->page)
4009                iwqp->sc_qp.qp_uk.sq_base = kmap(iwqp->page);
4010        dev->iw_priv_qp_ops->qp_send_rtt(&iwqp->sc_qp, read0);
4011        if (iwqp->page)
4012                kunmap(iwqp->page);
4013        status = i40iw_send_cm_event(cm_node, cm_id, IW_CM_EVENT_CONNECT_REPLY, 0);
4014        if (status)
4015                i40iw_pr_err("send cm event\n");
4016
4017        memset(&attr, 0, sizeof(attr));
4018        attr.qp_state = IB_QPS_RTS;
4019        cm_node->qhash_set = false;
4020        i40iw_modify_qp(&iwqp->ibqp, &attr, IB_QP_STATE, NULL);
4021
4022        cm_node->accelerated = 1;
4023        if (cm_node->accept_pend) {
4024                if (!cm_node->listener)
4025                        i40iw_pr_err("listener is null for passive node\n");
4026                atomic_dec(&cm_node->listener->pend_accepts_cnt);
4027                cm_node->accept_pend = 0;
4028        }
4029        return;
4030
4031error:
4032        iwqp->cm_id = NULL;
4033        cm_id->provider_data = NULL;
4034        i40iw_send_cm_event(event->cm_node,
4035                            cm_id,
4036                            IW_CM_EVENT_CONNECT_REPLY,
4037                            status);
4038        cm_id->rem_ref(cm_id);
4039        i40iw_rem_ref_cm_node(event->cm_node);
4040}
4041
4042/**
4043 * i40iw_cm_event_reset - handle reset
4044 * @event: the info for cm_node of connection
4045 */
4046static void i40iw_cm_event_reset(struct i40iw_cm_event *event)
4047{
4048        struct i40iw_cm_node *cm_node = event->cm_node;
4049        struct iw_cm_id   *cm_id = cm_node->cm_id;
4050        struct i40iw_qp *iwqp;
4051
4052        if (!cm_id)
4053                return;
4054
4055        iwqp = cm_id->provider_data;
4056        if (!iwqp)
4057                return;
4058
4059        i40iw_debug(cm_node->dev,
4060                    I40IW_DEBUG_CM,
4061                    "reset event %p - cm_id = %p\n",
4062                     event->cm_node, cm_id);
4063        iwqp->cm_id = NULL;
4064
4065        i40iw_send_cm_event(cm_node, cm_node->cm_id, IW_CM_EVENT_DISCONNECT, -ECONNRESET);
4066        i40iw_send_cm_event(cm_node, cm_node->cm_id, IW_CM_EVENT_CLOSE, 0);
4067}
4068
4069/**
4070 * i40iw_cm_event_handler - worker thread callback to send event to cm upper layer
4071 * @work: pointer of cm event info.
4072 */
4073static void i40iw_cm_event_handler(struct work_struct *work)
4074{
4075        struct i40iw_cm_event *event = container_of(work,
4076                                                    struct i40iw_cm_event,
4077                                                    event_work);
4078        struct i40iw_cm_node *cm_node;
4079
4080        if (!event || !event->cm_node || !event->cm_node->cm_core)
4081                return;
4082
4083        cm_node = event->cm_node;
4084
4085        switch (event->type) {
4086        case I40IW_CM_EVENT_MPA_REQ:
4087                i40iw_send_cm_event(cm_node,
4088                                    cm_node->cm_id,
4089                                    IW_CM_EVENT_CONNECT_REQUEST,
4090                                    0);
4091                break;
4092        case I40IW_CM_EVENT_RESET:
4093                i40iw_cm_event_reset(event);
4094                break;
4095        case I40IW_CM_EVENT_CONNECTED:
4096                if (!event->cm_node->cm_id ||
4097                    (event->cm_node->state != I40IW_CM_STATE_OFFLOADED))
4098                        break;
4099                i40iw_cm_event_connected(event);
4100                break;
4101        case I40IW_CM_EVENT_MPA_REJECT:
4102                if (!event->cm_node->cm_id ||
4103                    (cm_node->state == I40IW_CM_STATE_OFFLOADED))
4104                        break;
4105                i40iw_send_cm_event(cm_node,
4106                                    cm_node->cm_id,
4107                                    IW_CM_EVENT_CONNECT_REPLY,
4108                                    -ECONNREFUSED);
4109                break;
4110        case I40IW_CM_EVENT_ABORTED:
4111                if (!event->cm_node->cm_id ||
4112                    (event->cm_node->state == I40IW_CM_STATE_OFFLOADED))
4113                        break;
4114                i40iw_event_connect_error(event);
4115                break;
4116        default:
4117                i40iw_pr_err("event type = %d\n", event->type);
4118                break;
4119        }
4120
4121        event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
4122        i40iw_rem_ref_cm_node(event->cm_node);
4123        kfree(event);
4124}
4125
4126/**
4127 * i40iw_cm_post_event - queue event request for worker thread
4128 * @event: cm node's info for up event call
4129 */
4130static void i40iw_cm_post_event(struct i40iw_cm_event *event)
4131{
4132        atomic_inc(&event->cm_node->ref_count);
4133        event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
4134        INIT_WORK(&event->event_work, i40iw_cm_event_handler);
4135
4136        queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
4137}
4138