The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/cxgbe/iw_cxgbe/cm.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2009-2013, 2016 Chelsio, Inc. All rights reserved.
    5  *
    6  * This software is available to you under a choice of one of two
    7  * licenses.  You may choose to be licensed under the terms of the GNU
    8  * General Public License (GPL) Version 2, available from the file
    9  * COPYING in the main directory of this source tree, or the
   10  * OpenIB.org BSD license below:
   11  *
   12  *     Redistribution and use in source and binary forms, with or
   13  *     without modification, are permitted provided that the following
   14  *     conditions are met:
   15  *
   16  *      - Redistributions of source code must retain the above
   17  *        copyright notice, this list of conditions and the following
   18  *        disclaimer.
   19  *
   20  *      - Redistributions in binary form must reproduce the above
   21  *        copyright notice, this list of conditions and the following
   22  *        disclaimer in the documentation and/or other materials
   23  *        provided with the distribution.
   24  *
   25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   32  * SOFTWARE.
   33  */
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD$");
   36 
   37 #include "opt_inet.h"
   38 
   39 #ifdef TCP_OFFLOAD
   40 #include <sys/types.h>
   41 #include <sys/malloc.h>
   42 #include <sys/socket.h>
   43 #include <sys/socketvar.h>
   44 #include <sys/sockio.h>
   45 #include <sys/taskqueue.h>
   46 #include <netinet/in.h>
   47 #include <net/route.h>
   48 #include <net/route/nhop.h>
   49 
   50 #include <netinet/in_systm.h>
   51 #include <netinet/in_pcb.h>
   52 #include <netinet6/in6_pcb.h>
   53 #include <netinet/ip.h>
   54 #include <netinet/in_fib.h>
   55 #include <netinet6/in6_fib.h>
   56 #include <netinet6/scope6_var.h>
   57 #include <netinet/ip_var.h>
   58 #include <netinet/tcp_var.h>
   59 #include <netinet/tcp.h>
   60 #include <netinet/tcpip.h>
   61 
   62 #include <netinet/toecore.h>
   63 
   64 struct sge_iq;
   65 struct rss_header;
   66 struct cpl_set_tcb_rpl;
   67 #include <linux/types.h>
   68 #include "offload.h"
   69 #include "tom/t4_tom.h"
   70 
   71 #define TOEPCB(so)  ((struct toepcb *)(sototcpcb((so))->t_toe))
   72 
   73 #include "iw_cxgbe.h"
   74 #include <linux/module.h>
   75 #include <linux/workqueue.h>
   76 #include <linux/if_vlan.h>
   77 #include <net/netevent.h>
   78 #include <rdma/rdma_cm.h>
   79 
   80 static spinlock_t req_lock;
   81 static TAILQ_HEAD(c4iw_ep_list, c4iw_ep_common) req_list;
   82 static struct work_struct c4iw_task;
   83 static struct workqueue_struct *c4iw_taskq;
   84 static LIST_HEAD(err_cqe_list);
   85 static spinlock_t err_cqe_lock;
   86 static LIST_HEAD(listen_port_list);
   87 static DEFINE_MUTEX(listen_port_mutex);
   88 
   89 static void process_req(struct work_struct *ctx);
   90 static void start_ep_timer(struct c4iw_ep *ep);
   91 static int stop_ep_timer(struct c4iw_ep *ep);
   92 static int set_tcpinfo(struct c4iw_ep *ep);
   93 static void process_timeout(struct c4iw_ep *ep);
   94 static void process_err_cqes(void);
   95 static void *alloc_ep(int size, gfp_t flags);
   96 static void close_socket(struct socket *so);
   97 static int send_mpa_req(struct c4iw_ep *ep);
   98 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen);
   99 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen);
  100 static void close_complete_upcall(struct c4iw_ep *ep, int status);
  101 static int send_abort(struct c4iw_ep *ep);
  102 static void peer_close_upcall(struct c4iw_ep *ep);
  103 static void peer_abort_upcall(struct c4iw_ep *ep);
  104 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
  105 static int connect_request_upcall(struct c4iw_ep *ep);
  106 static void established_upcall(struct c4iw_ep *ep);
  107 static int process_mpa_reply(struct c4iw_ep *ep);
  108 static int process_mpa_request(struct c4iw_ep *ep);
  109 static void process_peer_close(struct c4iw_ep *ep);
  110 static void process_conn_error(struct c4iw_ep *ep);
  111 static void process_close_complete(struct c4iw_ep *ep);
  112 static void ep_timeout(unsigned long arg);
  113 static void setiwsockopt(struct socket *so);
  114 static void init_iwarp_socket(struct socket *so, void *arg);
  115 static void uninit_iwarp_socket(struct socket *so);
  116 static void process_data(struct c4iw_ep *ep);
  117 static void process_connected(struct c4iw_ep *ep);
  118 static int c4iw_so_upcall(struct socket *so, void *arg, int waitflag);
  119 static void process_socket_event(struct c4iw_ep *ep);
  120 static void release_ep_resources(struct c4iw_ep *ep);
  121 static int process_terminate(struct c4iw_ep *ep);
  122 static int terminate(struct sge_iq *iq, const struct rss_header *rss,
  123     struct mbuf *m);
  124 static int add_ep_to_req_list(struct c4iw_ep *ep, int ep_events);
  125 static struct listen_port_info *
  126 add_ep_to_listenlist(struct c4iw_listen_ep *lep);
  127 static int rem_ep_from_listenlist(struct c4iw_listen_ep *lep);
  128 static struct c4iw_listen_ep *
  129 find_real_listen_ep(struct c4iw_listen_ep *master_lep, struct socket *so);
  130 static int get_ifnet_from_raddr(struct sockaddr_storage *raddr,
  131                 struct ifnet **ifp);
  132 static void process_newconn(struct c4iw_listen_ep *master_lep,
  133                 struct socket *new_so);
  134 #define START_EP_TIMER(ep) \
  135     do { \
  136             CTR3(KTR_IW_CXGBE, "start_ep_timer (%s:%d) ep %p", \
  137                 __func__, __LINE__, (ep)); \
  138             start_ep_timer(ep); \
  139     } while (0)
  140 
  141 #define STOP_EP_TIMER(ep) \
  142     ({ \
  143             CTR3(KTR_IW_CXGBE, "stop_ep_timer (%s:%d) ep %p", \
  144                 __func__, __LINE__, (ep)); \
  145             stop_ep_timer(ep); \
  146     })
  147 
  148 #define GET_LOCAL_ADDR(pladdr, so) \
  149         do { \
  150                 struct sockaddr_storage *__a = NULL; \
  151                 struct  inpcb *__inp = sotoinpcb(so); \
  152                 KASSERT(__inp != NULL, \
  153                    ("GET_LOCAL_ADDR(%s):so:%p, inp = NULL", __func__, so)); \
  154                 if (__inp->inp_vflag & INP_IPV4) \
  155                         in_getsockaddr(so, (struct sockaddr **)&__a); \
  156                 else \
  157                         in6_getsockaddr(so, (struct sockaddr **)&__a); \
  158                 *(pladdr) = *__a; \
  159                 free(__a, M_SONAME); \
  160         } while (0)
  161 
  162 #define GET_REMOTE_ADDR(praddr, so) \
  163         do { \
  164                 struct sockaddr_storage *__a = NULL; \
  165                 struct  inpcb *__inp = sotoinpcb(so); \
  166                 KASSERT(__inp != NULL, \
  167                    ("GET_REMOTE_ADDR(%s):so:%p, inp = NULL", __func__, so)); \
  168                 if (__inp->inp_vflag & INP_IPV4) \
  169                         in_getpeeraddr(so, (struct sockaddr **)&__a); \
  170                 else \
  171                         in6_getpeeraddr(so, (struct sockaddr **)&__a); \
  172                 *(praddr) = *__a; \
  173                 free(__a, M_SONAME); \
  174         } while (0)
  175 
  176 static char *states[] = {
  177         "idle",
  178         "listen",
  179         "connecting",
  180         "mpa_wait_req",
  181         "mpa_req_sent",
  182         "mpa_req_rcvd",
  183         "mpa_rep_sent",
  184         "fpdu_mode",
  185         "aborting",
  186         "closing",
  187         "moribund",
  188         "dead",
  189         NULL,
  190 };
  191 
  192 static void deref_cm_id(struct c4iw_ep_common *epc)
  193 {
  194       epc->cm_id->rem_ref(epc->cm_id);
  195       epc->cm_id = NULL;
  196       set_bit(CM_ID_DEREFED, &epc->history);
  197 }
  198 
  199 static void ref_cm_id(struct c4iw_ep_common *epc)
  200 {
  201       set_bit(CM_ID_REFED, &epc->history);
  202       epc->cm_id->add_ref(epc->cm_id);
  203 }
  204 
  205 static void deref_qp(struct c4iw_ep *ep)
  206 {
  207         c4iw_qp_rem_ref(&ep->com.qp->ibqp);
  208         clear_bit(QP_REFERENCED, &ep->com.flags);
  209         set_bit(QP_DEREFED, &ep->com.history);
  210 }
  211 
  212 static void ref_qp(struct c4iw_ep *ep)
  213 {
  214         set_bit(QP_REFERENCED, &ep->com.flags);
  215         set_bit(QP_REFED, &ep->com.history);
  216         c4iw_qp_add_ref(&ep->com.qp->ibqp);
  217 }
  218 /* allocated per TCP port while listening */
  219 struct listen_port_info {
  220         uint16_t port_num; /* TCP port address */
  221         struct list_head list; /* belongs to listen_port_list */
  222         struct list_head lep_list; /* per port lep list */
  223         uint32_t refcnt; /* number of lep's listening */
  224 };
  225 
  226 /*
  227  * Following two lists are used to manage INADDR_ANY listeners:
  228  * 1)listen_port_list
  229  * 2)lep_list
  230  *
  231  * Below is the INADDR_ANY listener lists overview on a system with a two port
  232  * adapter:
  233  *   |------------------|
  234  *   |listen_port_list  |
  235  *   |------------------|
  236  *            |
  237  *            |              |-----------|       |-----------|
  238  *            |              | port_num:X|       | port_num:X|
  239  *            |--------------|-list------|-------|-list------|-------....
  240  *                           | lep_list----|     | lep_list----|
  241  *                           | refcnt    | |     | refcnt    | |
  242  *                           |           | |     |           | |
  243  *                           |           | |     |           | |
  244  *                           |-----------| |     |-----------| |
  245  *                                         |                   |
  246  *                                         |                   |
  247  *                                         |                   |
  248  *                                         |                   |         lep1                  lep2
  249  *                                         |                   |    |----------------|    |----------------|
  250  *                                         |                   |----| listen_ep_list |----| listen_ep_list |
  251  *                                         |                        |----------------|    |----------------|
  252  *                                         |
  253  *                                         |
  254  *                                         |        lep1                  lep2
  255  *                                         |   |----------------|    |----------------|
  256  *                                         |---| listen_ep_list |----| listen_ep_list |
  257  *                                             |----------------|    |----------------|
  258  *
  259  * Because of two port adapter, the number of lep's are two(lep1 & lep2) for
  260  * each TCP port number.
  261  *
  262  * Here 'lep1' is always marked as Master lep, because solisten() is always
  263  * called through first lep.
  264  *
  265  */
  266 static struct listen_port_info *
  267 add_ep_to_listenlist(struct c4iw_listen_ep *lep)
  268 {
  269         uint16_t port;
  270         struct listen_port_info *port_info = NULL;
  271         struct sockaddr_storage *laddr = &lep->com.local_addr;
  272 
  273         port = (laddr->ss_family == AF_INET) ?
  274                 ((struct sockaddr_in *)laddr)->sin_port :
  275                 ((struct sockaddr_in6 *)laddr)->sin6_port;
  276 
  277         mutex_lock(&listen_port_mutex);
  278 
  279         list_for_each_entry(port_info, &listen_port_list, list)
  280                 if (port_info->port_num == port)
  281                         goto found_port;
  282 
  283         port_info = malloc(sizeof(*port_info), M_CXGBE, M_WAITOK);
  284         port_info->port_num = port;
  285         port_info->refcnt    = 0;
  286 
  287         list_add_tail(&port_info->list, &listen_port_list);
  288         INIT_LIST_HEAD(&port_info->lep_list);
  289 
  290 found_port:
  291         port_info->refcnt++;
  292         list_add_tail(&lep->listen_ep_list, &port_info->lep_list);
  293         mutex_unlock(&listen_port_mutex);
  294         return port_info;
  295 }
  296 
  297 static int
  298 rem_ep_from_listenlist(struct c4iw_listen_ep *lep)
  299 {
  300         uint16_t port;
  301         struct listen_port_info *port_info = NULL;
  302         struct sockaddr_storage *laddr = &lep->com.local_addr;
  303         int refcnt = 0;
  304 
  305         port = (laddr->ss_family == AF_INET) ?
  306                 ((struct sockaddr_in *)laddr)->sin_port :
  307                 ((struct sockaddr_in6 *)laddr)->sin6_port;
  308 
  309         mutex_lock(&listen_port_mutex);
  310 
  311         /* get the port_info structure based on the lep's port address */
  312         list_for_each_entry(port_info, &listen_port_list, list) {
  313                 if (port_info->port_num == port) {
  314                         port_info->refcnt--;
  315                         refcnt = port_info->refcnt;
  316                         /* remove the current lep from the listen list */
  317                         list_del(&lep->listen_ep_list);
  318                         if (port_info->refcnt == 0) {
  319                                 /* Remove this entry from the list as there
  320                                  * are no more listeners for this port_num.
  321                                  */
  322                                 list_del(&port_info->list);
  323                                 kfree(port_info);
  324                         }
  325                         break;
  326                 }
  327         }
  328         mutex_unlock(&listen_port_mutex);
  329         return refcnt;
  330 }
  331 
  332 /*
  333  * Find the lep that belongs to the ifnet on which the SYN frame was received.
  334  */
  335 struct c4iw_listen_ep *
  336 find_real_listen_ep(struct c4iw_listen_ep *master_lep, struct socket *so)
  337 {
  338         struct adapter *adap = NULL;
  339         struct c4iw_listen_ep *lep = NULL;
  340         struct ifnet *ifp = NULL, *hw_ifp = NULL;
  341         struct listen_port_info *port_info = NULL;
  342         int i = 0, found_portinfo = 0, found_lep = 0;
  343         uint16_t port;
  344 
  345         /*
  346          * STEP 1: Figure out 'ifp' of the physical interface, not pseudo
  347          * interfaces like vlan, lagg, etc..
  348          * TBD: lagg support, lagg + vlan support.
  349          */
  350         ifp = TOEPCB(so)->l2te->ifp;
  351         if (ifp->if_type == IFT_L2VLAN) {
  352                 hw_ifp = VLAN_TRUNKDEV(ifp);
  353                 if (hw_ifp == NULL) {
  354                         CTR4(KTR_IW_CXGBE, "%s: Failed to get parent ifnet of "
  355                                 "vlan ifnet %p, sock %p, master_lep %p",
  356                                 __func__, ifp, so, master_lep);
  357                         return (NULL);
  358                 }
  359         } else
  360                 hw_ifp = ifp;
  361 
  362         /* STEP 2: Find 'port_info' with listener local port address. */
  363         port = (master_lep->com.local_addr.ss_family == AF_INET) ?
  364                 ((struct sockaddr_in *)&master_lep->com.local_addr)->sin_port :
  365                 ((struct sockaddr_in6 *)&master_lep->com.local_addr)->sin6_port;
  366 
  367 
  368         mutex_lock(&listen_port_mutex);
  369         list_for_each_entry(port_info, &listen_port_list, list)
  370                 if (port_info->port_num == port) {
  371                         found_portinfo =1;
  372                         break;
  373                 }
  374         if (!found_portinfo)
  375                 goto out;
  376 
  377         /* STEP 3: Traverse through list of lep's that are bound to the current
  378          * TCP port address and find the lep that belongs to the ifnet on which
  379          * the SYN frame was received.
  380          */
  381         list_for_each_entry(lep, &port_info->lep_list, listen_ep_list) {
  382                 adap = lep->com.dev->rdev.adap;
  383                 for_each_port(adap, i) {
  384                         if (hw_ifp == adap->port[i]->vi[0].ifp) {
  385                                 found_lep =1;
  386                                 goto out;
  387                         }
  388                 }
  389         }
  390 out:
  391         mutex_unlock(&listen_port_mutex);
  392         return found_lep ? lep : (NULL);
  393 }
  394 
  395 static void process_timeout(struct c4iw_ep *ep)
  396 {
  397         struct c4iw_qp_attributes attrs = {0};
  398         int abort = 1;
  399 
  400         CTR4(KTR_IW_CXGBE, "%s ep :%p, tid:%u, state %d", __func__,
  401                         ep, ep->hwtid, ep->com.state);
  402         set_bit(TIMEDOUT, &ep->com.history);
  403         switch (ep->com.state) {
  404         case MPA_REQ_SENT:
  405                 connect_reply_upcall(ep, -ETIMEDOUT);
  406                 break;
  407         case MPA_REQ_WAIT:
  408         case MPA_REQ_RCVD:
  409         case MPA_REP_SENT:
  410         case FPDU_MODE:
  411                 break;
  412         case CLOSING:
  413         case MORIBUND:
  414                 if (ep->com.cm_id && ep->com.qp) {
  415                         attrs.next_state = C4IW_QP_STATE_ERROR;
  416                         c4iw_modify_qp(ep->com.dev, ep->com.qp,
  417                                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  418                 }
  419                 close_complete_upcall(ep, -ETIMEDOUT);
  420                 break;
  421         case ABORTING:
  422         case DEAD:
  423                 /*
  424                  * These states are expected if the ep timed out at the same
  425                  * time as another thread was calling stop_ep_timer().
  426                  * So we silently do nothing for these states.
  427                  */
  428                 abort = 0;
  429                 break;
  430         default:
  431                 CTR4(KTR_IW_CXGBE, "%s unexpected state ep %p tid %u state %u"
  432                                 , __func__, ep, ep->hwtid, ep->com.state);
  433                 abort = 0;
  434         }
  435         if (abort)
  436                 c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
  437         c4iw_put_ep(&ep->com);
  438         return;
  439 }
  440 
  441 struct cqe_list_entry {
  442         struct list_head entry;
  443         struct c4iw_dev *rhp;
  444         struct t4_cqe err_cqe;
  445 };
  446 
  447 static void
  448 process_err_cqes(void)
  449 {
  450         unsigned long flag;
  451         struct cqe_list_entry *cle;
  452 
  453         spin_lock_irqsave(&err_cqe_lock, flag);
  454         while (!list_empty(&err_cqe_list)) {
  455                 struct list_head *tmp;
  456                 tmp = err_cqe_list.next;
  457                 list_del(tmp);
  458                 tmp->next = tmp->prev = NULL;
  459                 spin_unlock_irqrestore(&err_cqe_lock, flag);
  460                 cle = list_entry(tmp, struct cqe_list_entry, entry);
  461                 c4iw_ev_dispatch(cle->rhp, &cle->err_cqe);
  462                 free(cle, M_CXGBE);
  463                 spin_lock_irqsave(&err_cqe_lock, flag);
  464         }
  465         spin_unlock_irqrestore(&err_cqe_lock, flag);
  466 
  467         return;
  468 }
  469 
  470 static void
  471 process_req(struct work_struct *ctx)
  472 {
  473         struct c4iw_ep_common *epc;
  474         unsigned long flag;
  475         int ep_events;
  476 
  477         process_err_cqes();
  478         spin_lock_irqsave(&req_lock, flag);
  479         while (!TAILQ_EMPTY(&req_list)) {
  480                 epc = TAILQ_FIRST(&req_list);
  481                 TAILQ_REMOVE(&req_list, epc, entry);
  482                 epc->entry.tqe_prev = NULL;
  483                 ep_events = epc->ep_events;
  484                 epc->ep_events = 0;
  485                 spin_unlock_irqrestore(&req_lock, flag);
  486                 mutex_lock(&epc->mutex);
  487                 CTR5(KTR_IW_CXGBE, "%s: so %p, ep %p, ep_state %s events 0x%x",
  488                     __func__, epc->so, epc, states[epc->state], ep_events);
  489                 if (ep_events & C4IW_EVENT_TERM)
  490                         process_terminate((struct c4iw_ep *)epc);
  491                 if (ep_events & C4IW_EVENT_TIMEOUT)
  492                         process_timeout((struct c4iw_ep *)epc);
  493                 if (ep_events & C4IW_EVENT_SOCKET)
  494                         process_socket_event((struct c4iw_ep *)epc);
  495                 mutex_unlock(&epc->mutex);
  496                 c4iw_put_ep(epc);
  497                 process_err_cqes();
  498                 spin_lock_irqsave(&req_lock, flag);
  499         }
  500         spin_unlock_irqrestore(&req_lock, flag);
  501 }
  502 
  503 /*
  504  * XXX: doesn't belong here in the iWARP driver.
  505  * XXX: assumes that the connection was offloaded by cxgbe/t4_tom if TF_TOE is
  506  *      set.  Is this a valid assumption for active open?
  507  */
  508 static int
  509 set_tcpinfo(struct c4iw_ep *ep)
  510 {
  511         struct socket *so = ep->com.so;
  512         struct inpcb *inp = sotoinpcb(so);
  513         struct tcpcb *tp;
  514         struct toepcb *toep;
  515         int rc = 0;
  516 
  517         INP_WLOCK(inp);
  518         tp = intotcpcb(inp);
  519         if ((tp->t_flags & TF_TOE) == 0) {
  520                 rc = EINVAL;
  521                 log(LOG_ERR, "%s: connection not offloaded (so %p, ep %p)\n",
  522                     __func__, so, ep);
  523                 goto done;
  524         }
  525         toep = TOEPCB(so);
  526 
  527         ep->hwtid = toep->tid;
  528         ep->snd_seq = tp->snd_nxt;
  529         ep->rcv_seq = tp->rcv_nxt;
  530 done:
  531         INP_WUNLOCK(inp);
  532         return (rc);
  533 
  534 }
  535 static int
  536 get_ifnet_from_raddr(struct sockaddr_storage *raddr, struct ifnet **ifp)
  537 {
  538         int err = 0;
  539         struct nhop_object *nh;
  540 
  541         if (raddr->ss_family == AF_INET) {
  542                 struct sockaddr_in *raddr4 = (struct sockaddr_in *)raddr;
  543 
  544                 nh = fib4_lookup(RT_DEFAULT_FIB, raddr4->sin_addr, 0,
  545                                 NHR_NONE, 0);
  546         } else {
  547                 struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)raddr;
  548                 struct in6_addr addr6;
  549                 uint32_t scopeid;
  550 
  551                 memset(&addr6, 0, sizeof(addr6));
  552                 in6_splitscope((struct in6_addr *)&raddr6->sin6_addr,
  553                                         &addr6, &scopeid);
  554                 nh = fib6_lookup(RT_DEFAULT_FIB, &addr6, scopeid,
  555                                 NHR_NONE, 0);
  556         }
  557 
  558         if (nh == NULL)
  559                 err = EHOSTUNREACH;
  560         else
  561                 *ifp = nh->nh_ifp;
  562         CTR2(KTR_IW_CXGBE, "%s: return: %d", __func__, err);
  563         return err;
  564 }
  565 
  566 static void
  567 close_socket(struct socket *so)
  568 {
  569         uninit_iwarp_socket(so);
  570         soclose(so);
  571 }
  572 
  573 static void
  574 process_peer_close(struct c4iw_ep *ep)
  575 {
  576         struct c4iw_qp_attributes attrs = {0};
  577         int disconnect = 1;
  578         int release = 0;
  579 
  580         CTR4(KTR_IW_CXGBE, "%s:ppcB ep %p so %p state %s", __func__, ep,
  581             ep->com.so, states[ep->com.state]);
  582 
  583         switch (ep->com.state) {
  584 
  585                 case MPA_REQ_WAIT:
  586                         CTR2(KTR_IW_CXGBE, "%s:ppc1 %p MPA_REQ_WAIT DEAD",
  587                             __func__, ep);
  588                         /* Fallthrough */
  589                 case MPA_REQ_SENT:
  590                         CTR2(KTR_IW_CXGBE, "%s:ppc2 %p MPA_REQ_SENT DEAD",
  591                             __func__, ep);
  592                         ep->com.state = DEAD;
  593                         connect_reply_upcall(ep, -ECONNABORTED);
  594 
  595                         disconnect = 0;
  596                         STOP_EP_TIMER(ep);
  597                         close_socket(ep->com.so);
  598                         deref_cm_id(&ep->com);
  599                         release = 1;
  600                         break;
  601 
  602                 case MPA_REQ_RCVD:
  603 
  604                         /*
  605                          * We're gonna mark this puppy DEAD, but keep
  606                          * the reference on it until the ULP accepts or
  607                          * rejects the CR.
  608                          */
  609                         CTR2(KTR_IW_CXGBE, "%s:ppc3 %p MPA_REQ_RCVD CLOSING",
  610                             __func__, ep);
  611                         ep->com.state = CLOSING;
  612                         break;
  613 
  614                 case MPA_REP_SENT:
  615                         CTR2(KTR_IW_CXGBE, "%s:ppc4 %p MPA_REP_SENT CLOSING",
  616                             __func__, ep);
  617                         ep->com.state = CLOSING;
  618                         break;
  619 
  620                 case FPDU_MODE:
  621                         CTR2(KTR_IW_CXGBE, "%s:ppc5 %p FPDU_MODE CLOSING",
  622                             __func__, ep);
  623                         START_EP_TIMER(ep);
  624                         ep->com.state = CLOSING;
  625                         attrs.next_state = C4IW_QP_STATE_CLOSING;
  626                         c4iw_modify_qp(ep->com.dev, ep->com.qp,
  627                                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  628                         peer_close_upcall(ep);
  629                         break;
  630 
  631                 case ABORTING:
  632                         CTR2(KTR_IW_CXGBE, "%s:ppc6 %p ABORTING (disconn)",
  633                             __func__, ep);
  634                         disconnect = 0;
  635                         break;
  636 
  637                 case CLOSING:
  638                         CTR2(KTR_IW_CXGBE, "%s:ppc7 %p CLOSING MORIBUND",
  639                             __func__, ep);
  640                         ep->com.state = MORIBUND;
  641                         disconnect = 0;
  642                         break;
  643 
  644                 case MORIBUND:
  645                         CTR2(KTR_IW_CXGBE, "%s:ppc8 %p MORIBUND DEAD", __func__,
  646                             ep);
  647                         STOP_EP_TIMER(ep);
  648                         if (ep->com.cm_id && ep->com.qp) {
  649                                 attrs.next_state = C4IW_QP_STATE_IDLE;
  650                                 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
  651                                                 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  652                         }
  653                         close_socket(ep->com.so);
  654                         close_complete_upcall(ep, 0);
  655                         ep->com.state = DEAD;
  656                         release = 1;
  657                         disconnect = 0;
  658                         break;
  659 
  660                 case DEAD:
  661                         CTR2(KTR_IW_CXGBE, "%s:ppc9 %p DEAD (disconn)",
  662                             __func__, ep);
  663                         disconnect = 0;
  664                         break;
  665 
  666                 default:
  667                         panic("%s: ep %p state %d", __func__, ep,
  668                             ep->com.state);
  669                         break;
  670         }
  671 
  672 
  673         if (disconnect) {
  674 
  675                 CTR2(KTR_IW_CXGBE, "%s:ppca %p", __func__, ep);
  676                 c4iw_ep_disconnect(ep, 0, M_NOWAIT);
  677         }
  678         if (release) {
  679 
  680                 CTR2(KTR_IW_CXGBE, "%s:ppcb %p", __func__, ep);
  681                 c4iw_put_ep(&ep->com);
  682         }
  683         CTR2(KTR_IW_CXGBE, "%s:ppcE %p", __func__, ep);
  684         return;
  685 }
  686 
  687 static void
  688 process_conn_error(struct c4iw_ep *ep)
  689 {
  690         struct c4iw_qp_attributes attrs = {0};
  691         int ret;
  692         int state;
  693 
  694         state = ep->com.state;
  695         CTR5(KTR_IW_CXGBE, "%s:pceB ep %p so %p so->so_error %u state %s",
  696             __func__, ep, ep->com.so, ep->com.so->so_error,
  697             states[ep->com.state]);
  698 
  699         switch (state) {
  700 
  701                 case MPA_REQ_WAIT:
  702                         STOP_EP_TIMER(ep);
  703                         c4iw_put_ep(&ep->parent_ep->com);
  704                         break;
  705 
  706                 case MPA_REQ_SENT:
  707                         STOP_EP_TIMER(ep);
  708                         connect_reply_upcall(ep, -ECONNRESET);
  709                         break;
  710 
  711                 case MPA_REP_SENT:
  712                         ep->com.rpl_err = ECONNRESET;
  713                         CTR1(KTR_IW_CXGBE, "waking up ep %p", ep);
  714                         break;
  715 
  716                 case MPA_REQ_RCVD:
  717                         break;
  718 
  719                 case MORIBUND:
  720                 case CLOSING:
  721                         STOP_EP_TIMER(ep);
  722                         /*FALLTHROUGH*/
  723                 case FPDU_MODE:
  724 
  725                         if (ep->com.cm_id && ep->com.qp) {
  726 
  727                                 attrs.next_state = C4IW_QP_STATE_ERROR;
  728                                 ret = c4iw_modify_qp(ep->com.qp->rhp,
  729                                         ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
  730                                         &attrs, 1);
  731                                 if (ret)
  732                                         log(LOG_ERR,
  733                                                         "%s - qp <- error failed!\n",
  734                                                         __func__);
  735                         }
  736                         peer_abort_upcall(ep);
  737                         break;
  738 
  739                 case ABORTING:
  740                         break;
  741 
  742                 case DEAD:
  743                         CTR2(KTR_IW_CXGBE, "%s so_error %d IN DEAD STATE!!!!",
  744                             __func__, ep->com.so->so_error);
  745                         return;
  746 
  747                 default:
  748                         panic("%s: ep %p state %d", __func__, ep, state);
  749                         break;
  750         }
  751 
  752         if (state != ABORTING) {
  753                 close_socket(ep->com.so);
  754                 ep->com.state = DEAD;
  755                 c4iw_put_ep(&ep->com);
  756         }
  757         CTR2(KTR_IW_CXGBE, "%s:pceE %p", __func__, ep);
  758         return;
  759 }
  760 
  761 static void
  762 process_close_complete(struct c4iw_ep *ep)
  763 {
  764         struct c4iw_qp_attributes attrs = {0};
  765         int release = 0;
  766 
  767         CTR4(KTR_IW_CXGBE, "%s:pccB ep %p so %p state %s", __func__, ep,
  768             ep->com.so, states[ep->com.state]);
  769 
  770         /* The cm_id may be null if we failed to connect */
  771         set_bit(CLOSE_CON_RPL, &ep->com.history);
  772 
  773         switch (ep->com.state) {
  774 
  775                 case CLOSING:
  776                         CTR2(KTR_IW_CXGBE, "%s:pcc1 %p CLOSING MORIBUND",
  777                             __func__, ep);
  778                         ep->com.state = MORIBUND;
  779                         break;
  780 
  781                 case MORIBUND:
  782                         CTR2(KTR_IW_CXGBE, "%s:pcc1 %p MORIBUND DEAD", __func__,
  783                             ep);
  784                         STOP_EP_TIMER(ep);
  785 
  786                         if ((ep->com.cm_id) && (ep->com.qp)) {
  787 
  788                                 CTR2(KTR_IW_CXGBE, "%s:pcc2 %p QP_STATE_IDLE",
  789                                     __func__, ep);
  790                                 attrs.next_state = C4IW_QP_STATE_IDLE;
  791                                 c4iw_modify_qp(ep->com.dev,
  792                                                 ep->com.qp,
  793                                                 C4IW_QP_ATTR_NEXT_STATE,
  794                                                 &attrs, 1);
  795                         }
  796 
  797                         close_socket(ep->com.so);
  798                         close_complete_upcall(ep, 0);
  799                         ep->com.state = DEAD;
  800                         release = 1;
  801                         break;
  802 
  803                 case ABORTING:
  804                         CTR2(KTR_IW_CXGBE, "%s:pcc5 %p ABORTING", __func__, ep);
  805                         break;
  806 
  807                 case DEAD:
  808                         CTR2(KTR_IW_CXGBE, "%s:pcc6 %p DEAD", __func__, ep);
  809                         break;
  810                 default:
  811                         CTR2(KTR_IW_CXGBE, "%s:pcc7 %p unknown ep state",
  812                                         __func__, ep);
  813                         panic("%s:pcc6 %p unknown ep state", __func__, ep);
  814                         break;
  815         }
  816 
  817         if (release) {
  818 
  819                 CTR2(KTR_IW_CXGBE, "%s:pcc8 %p", __func__, ep);
  820                 release_ep_resources(ep);
  821         }
  822         CTR2(KTR_IW_CXGBE, "%s:pccE %p", __func__, ep);
  823         return;
  824 }
  825 
  826 static void
  827 setiwsockopt(struct socket *so)
  828 {
  829         int rc;
  830         struct sockopt sopt;
  831         int on = 1;
  832 
  833         sopt.sopt_dir = SOPT_SET;
  834         sopt.sopt_level = IPPROTO_TCP;
  835         sopt.sopt_name = TCP_NODELAY;
  836         sopt.sopt_val = (caddr_t)&on;
  837         sopt.sopt_valsize = sizeof on;
  838         sopt.sopt_td = NULL;
  839         rc = -sosetopt(so, &sopt);
  840         if (rc) {
  841                 log(LOG_ERR, "%s: can't set TCP_NODELAY on so %p (%d)\n",
  842                     __func__, so, rc);
  843         }
  844 }
  845 
  846 static void
  847 init_iwarp_socket(struct socket *so, void *arg)
  848 {
  849         if (SOLISTENING(so)) {
  850                 SOLISTEN_LOCK(so);
  851                 solisten_upcall_set(so, c4iw_so_upcall, arg);
  852                 so->so_state |= SS_NBIO;
  853                 SOLISTEN_UNLOCK(so);
  854         } else {
  855                 SOCKBUF_LOCK(&so->so_rcv);
  856                 soupcall_set(so, SO_RCV, c4iw_so_upcall, arg);
  857                 so->so_state |= SS_NBIO;
  858                 SOCKBUF_UNLOCK(&so->so_rcv);
  859         }
  860 }
  861 
  862 static void
  863 uninit_iwarp_socket(struct socket *so)
  864 {
  865         if (SOLISTENING(so)) {
  866                 SOLISTEN_LOCK(so);
  867                 solisten_upcall_set(so, NULL, NULL);
  868                 SOLISTEN_UNLOCK(so);
  869         } else {
  870                 SOCKBUF_LOCK(&so->so_rcv);
  871                 soupcall_clear(so, SO_RCV);
  872                 SOCKBUF_UNLOCK(&so->so_rcv);
  873         }
  874 }
  875 
  876 static void
  877 process_data(struct c4iw_ep *ep)
  878 {
  879         int ret = 0;
  880         int disconnect = 0;
  881         struct c4iw_qp_attributes attrs = {0};
  882 
  883         CTR5(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s, sbused %d", __func__,
  884             ep->com.so, ep, states[ep->com.state], sbused(&ep->com.so->so_rcv));
  885 
  886         switch (ep->com.state) {
  887         case MPA_REQ_SENT:
  888                 disconnect = process_mpa_reply(ep);
  889                 break;
  890         case MPA_REQ_WAIT:
  891                 disconnect = process_mpa_request(ep);
  892                 if (disconnect)
  893                         /* Refered in process_newconn() */
  894                         c4iw_put_ep(&ep->parent_ep->com);
  895                 break;
  896         case FPDU_MODE:
  897                 MPASS(ep->com.qp != NULL);
  898                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
  899                 ret = c4iw_modify_qp(ep->com.dev, ep->com.qp,
  900                                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
  901                 if (ret != -EINPROGRESS)
  902                         disconnect = 1;
  903                 break;
  904         default:
  905                 log(LOG_ERR, "%s: Unexpected streaming data. ep %p, "
  906                             "state %d, so %p, so_state 0x%x, sbused %u\n",
  907                             __func__, ep, ep->com.state, ep->com.so,
  908                             ep->com.so->so_state, sbused(&ep->com.so->so_rcv));
  909                 break;
  910         }
  911         if (disconnect)
  912                 c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
  913 
  914 }
  915 
  916 static void
  917 process_connected(struct c4iw_ep *ep)
  918 {
  919         struct socket *so = ep->com.so;
  920 
  921         if ((so->so_state & SS_ISCONNECTED) && !so->so_error) {
  922                 if (send_mpa_req(ep))
  923                         goto err;
  924         } else {
  925                 connect_reply_upcall(ep, -so->so_error);
  926                 goto err;
  927         }
  928         return;
  929 err:
  930         close_socket(so);
  931         ep->com.state = DEAD;
  932         c4iw_put_ep(&ep->com);
  933         return;
  934 }
  935 
  936 static inline int c4iw_zero_addr(struct sockaddr *addr)
  937 {
  938         struct in6_addr *ip6;
  939 
  940         if (addr->sa_family == AF_INET)
  941                 return (((struct sockaddr_in *)addr)->sin_addr.s_addr == 0);
  942         else {
  943                 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
  944                 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
  945                                 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
  946         }
  947 }
  948 
  949 static inline int c4iw_loopback_addr(struct sockaddr *addr)
  950 {
  951         if (addr->sa_family == AF_INET)
  952                 return IN_LOOPBACK(
  953                         ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr));
  954         else
  955                 return IN6_IS_ADDR_LOOPBACK(
  956                                 &((struct sockaddr_in6 *) addr)->sin6_addr);
  957 }
  958 
  959 static inline int c4iw_any_addr(struct sockaddr *addr)
  960 {
  961         return c4iw_zero_addr(addr) || c4iw_loopback_addr(addr);
  962 }
  963 
  964 static void
  965 process_newconn(struct c4iw_listen_ep *master_lep, struct socket *new_so)
  966 {
  967         struct c4iw_listen_ep *real_lep = NULL;
  968         struct c4iw_ep *new_ep = NULL;
  969         struct sockaddr_in *remote = NULL;
  970         int ret = 0;
  971 
  972         MPASS(new_so != NULL);
  973 
  974         if (c4iw_any_addr((struct sockaddr *)&master_lep->com.local_addr)) {
  975                 /* Here we need to find the 'real_lep' that belongs to the
  976                  * incomming socket's network interface, such that the newly
  977                  * created 'ep' can be attached to the real 'lep'.
  978                  */
  979                 real_lep = find_real_listen_ep(master_lep, new_so);
  980                 if (real_lep == NULL) {
  981                         CTR2(KTR_IW_CXGBE, "%s: Could not find the real listen "
  982                                         "ep for sock: %p", __func__, new_so);
  983                         log(LOG_ERR,"%s: Could not find the real listen ep for "
  984                                         "sock: %p\n", __func__, new_so);
  985                         /* FIXME: properly free the 'new_so' in failure case.
  986                          * Use of soabort() and  soclose() are not legal
  987                          * here(before soaccept()).
  988                          */
  989                         return;
  990                 }
  991         } else /* for Non-Wildcard address, master_lep is always the real_lep */
  992                 real_lep = master_lep;
  993 
  994         new_ep = alloc_ep(sizeof(*new_ep), GFP_KERNEL);
  995 
  996         CTR6(KTR_IW_CXGBE, "%s: master_lep %p, real_lep: %p, new ep %p, "
  997             "listening so %p, new so %p", __func__, master_lep, real_lep,
  998             new_ep, master_lep->com.so, new_so);
  999 
 1000         new_ep->com.dev = real_lep->com.dev;
 1001         new_ep->com.so = new_so;
 1002         new_ep->com.cm_id = NULL;
 1003         new_ep->com.thread = real_lep->com.thread;
 1004         new_ep->parent_ep = real_lep;
 1005 
 1006         GET_LOCAL_ADDR(&new_ep->com.local_addr, new_so);
 1007         GET_REMOTE_ADDR(&new_ep->com.remote_addr, new_so);
 1008         c4iw_get_ep(&real_lep->com);
 1009         init_timer(&new_ep->timer);
 1010         new_ep->com.state = MPA_REQ_WAIT;
 1011 
 1012         setiwsockopt(new_so);
 1013         ret = soaccept(new_so, (struct sockaddr **)&remote);
 1014         if (ret != 0) {
 1015                 CTR4(KTR_IW_CXGBE,
 1016                                 "%s:listen sock:%p, new sock:%p, ret:%d",
 1017                                 __func__, master_lep->com.so, new_so, ret);
 1018                 if (remote != NULL)
 1019                         free(remote, M_SONAME);
 1020                 soclose(new_so);
 1021                 c4iw_put_ep(&new_ep->com);
 1022                 c4iw_put_ep(&real_lep->com);
 1023                 return;
 1024         }
 1025         free(remote, M_SONAME);
 1026 
 1027         START_EP_TIMER(new_ep);
 1028 
 1029         /* MPA request might have been queued up on the socket already, so we
 1030          * initialize the socket/upcall_handler under lock to prevent processing
 1031          * MPA request on another thread(via process_req()) simultaneously.
 1032          */
 1033         c4iw_get_ep(&new_ep->com); /* Dereferenced at the end below, this is to
 1034                                       avoid freeing of ep before ep unlock. */
 1035         mutex_lock(&new_ep->com.mutex);
 1036         init_iwarp_socket(new_so, &new_ep->com);
 1037 
 1038         ret = process_mpa_request(new_ep);
 1039         if (ret) {
 1040                 /* ABORT */
 1041                 c4iw_ep_disconnect(new_ep, 1, GFP_KERNEL);
 1042                 c4iw_put_ep(&real_lep->com);
 1043         }
 1044         mutex_unlock(&new_ep->com.mutex);
 1045         c4iw_put_ep(&new_ep->com);
 1046         return;
 1047 }
 1048 
 1049 static int
 1050 add_ep_to_req_list(struct c4iw_ep *ep, int new_ep_event)
 1051 {
 1052         unsigned long flag;
 1053 
 1054         spin_lock_irqsave(&req_lock, flag);
 1055         if (ep && ep->com.so) {
 1056                 ep->com.ep_events |= new_ep_event;
 1057                 if (!ep->com.entry.tqe_prev) {
 1058                         c4iw_get_ep(&ep->com);
 1059                         TAILQ_INSERT_TAIL(&req_list, &ep->com, entry);
 1060                         queue_work(c4iw_taskq, &c4iw_task);
 1061                 }
 1062         }
 1063         spin_unlock_irqrestore(&req_lock, flag);
 1064 
 1065         return (0);
 1066 }
 1067 
 1068 static int
 1069 c4iw_so_upcall(struct socket *so, void *arg, int waitflag)
 1070 {
 1071         struct c4iw_ep *ep = arg;
 1072 
 1073         CTR6(KTR_IW_CXGBE,
 1074             "%s: so %p, so_state 0x%x, ep %p, ep_state %s, tqe_prev %p",
 1075             __func__, so, so->so_state, ep, states[ep->com.state],
 1076             ep->com.entry.tqe_prev);
 1077 
 1078         MPASS(ep->com.so == so);
 1079         /*
 1080          * Wake up any threads waiting in rdma_init()/rdma_fini(),
 1081          * with locks held.
 1082          */
 1083         if (so->so_error || (ep->com.dev->rdev.flags & T4_FATAL_ERROR))
 1084                 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
 1085         add_ep_to_req_list(ep, C4IW_EVENT_SOCKET);
 1086 
 1087         return (SU_OK);
 1088 }
 1089 
 1090 
 1091 static int
 1092 terminate(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
 1093 {
 1094         struct adapter *sc = iq->adapter;
 1095         const struct cpl_rdma_terminate *cpl = mtod(m, const void *);
 1096         unsigned int tid = GET_TID(cpl);
 1097         struct toepcb *toep = lookup_tid(sc, tid);
 1098         struct socket *so;
 1099         struct c4iw_ep *ep;
 1100 
 1101         INP_WLOCK(toep->inp);
 1102         so = inp_inpcbtosocket(toep->inp);
 1103         ep = so->so_rcv.sb_upcallarg;
 1104         INP_WUNLOCK(toep->inp);
 1105 
 1106         CTR3(KTR_IW_CXGBE, "%s: so %p, ep %p", __func__, so, ep);
 1107         add_ep_to_req_list(ep, C4IW_EVENT_TERM);
 1108 
 1109         return 0;
 1110 }
 1111 
 1112 static void
 1113 process_socket_event(struct c4iw_ep *ep)
 1114 {
 1115         int state = ep->com.state;
 1116         struct socket *so = ep->com.so;
 1117 
 1118         if (ep->com.state == DEAD) {
 1119                 CTR3(KTR_IW_CXGBE, "%s: Pending socket event discarded "
 1120                     "ep %p ep_state %s", __func__, ep, states[state]);
 1121                 return;
 1122         }
 1123 
 1124         CTR6(KTR_IW_CXGBE, "process_socket_event: so %p, so_state 0x%x, "
 1125             "so_err %d, sb_state 0x%x, ep %p, ep_state %s", so, so->so_state,
 1126             so->so_error, so->so_rcv.sb_state, ep, states[state]);
 1127 
 1128         if (state == CONNECTING) {
 1129                 process_connected(ep);
 1130                 return;
 1131         }
 1132 
 1133         if (state == LISTEN) {
 1134                 struct c4iw_listen_ep *lep = (struct c4iw_listen_ep *)ep;
 1135                 struct socket *listen_so = so, *new_so = NULL;
 1136                 int error = 0;
 1137 
 1138                 SOLISTEN_LOCK(listen_so);
 1139                 do {
 1140                         error = solisten_dequeue(listen_so, &new_so,
 1141                                                 SOCK_NONBLOCK);
 1142                         if (error) {
 1143                                 CTR4(KTR_IW_CXGBE, "%s: lep %p listen_so %p "
 1144                                         "error %d", __func__, lep, listen_so,
 1145                                         error);
 1146                                 return;
 1147                         }
 1148                         process_newconn(lep, new_so);
 1149 
 1150                         /* solisten_dequeue() unlocks while return, so aquire
 1151                          * lock again for sol_qlen and also for next iteration.
 1152                          */
 1153                         SOLISTEN_LOCK(listen_so);
 1154                 } while (listen_so->sol_qlen);
 1155                 SOLISTEN_UNLOCK(listen_so);
 1156 
 1157                 return;
 1158         }
 1159 
 1160         /* connection error */
 1161         if (so->so_error) {
 1162                 process_conn_error(ep);
 1163                 return;
 1164         }
 1165 
 1166         /* peer close */
 1167         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && state <= CLOSING) {
 1168                 process_peer_close(ep);
 1169                 /*
 1170                  * check whether socket disconnect event is pending before
 1171                  * returning. Fallthrough if yes.
 1172                  */
 1173                 if (!(so->so_state & SS_ISDISCONNECTED))
 1174                         return;
 1175         }
 1176 
 1177         /* close complete */
 1178         if (so->so_state & SS_ISDISCONNECTED) {
 1179                 process_close_complete(ep);
 1180                 return;
 1181         }
 1182 
 1183         /* rx data */
 1184         if (sbused(&ep->com.so->so_rcv)) {
 1185                 process_data(ep);
 1186                 return;
 1187         }
 1188 
 1189         /* Socket events for 'MPA Request Received' and 'Close Complete'
 1190          * were already processed earlier in their previous events handlers.
 1191          * Hence, these socket events are skipped.
 1192          * And any other socket events must have handled above.
 1193          */
 1194         MPASS((ep->com.state == MPA_REQ_RCVD) || (ep->com.state == MORIBUND));
 1195 
 1196         if ((ep->com.state != MPA_REQ_RCVD) && (ep->com.state != MORIBUND))
 1197                 log(LOG_ERR, "%s: Unprocessed socket event so %p, "
 1198                 "so_state 0x%x, so_err %d, sb_state 0x%x, ep %p, ep_state %s\n",
 1199                 __func__, so, so->so_state, so->so_error, so->so_rcv.sb_state,
 1200                         ep, states[state]);
 1201 
 1202 }
 1203 
 1204 SYSCTL_NODE(_hw, OID_AUTO, iw_cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
 1205     "iw_cxgbe driver parameters");
 1206 
 1207 static int dack_mode = 0;
 1208 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, dack_mode, CTLFLAG_RWTUN, &dack_mode, 0,
 1209                 "Delayed ack mode (default = 0)");
 1210 
 1211 int c4iw_max_read_depth = 8;
 1212 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_max_read_depth, CTLFLAG_RWTUN, &c4iw_max_read_depth, 0,
 1213                 "Per-connection max ORD/IRD (default = 8)");
 1214 
 1215 static int enable_tcp_timestamps;
 1216 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_timestamps, CTLFLAG_RWTUN, &enable_tcp_timestamps, 0,
 1217                 "Enable tcp timestamps (default = 0)");
 1218 
 1219 static int enable_tcp_sack;
 1220 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_sack, CTLFLAG_RWTUN, &enable_tcp_sack, 0,
 1221                 "Enable tcp SACK (default = 0)");
 1222 
 1223 static int enable_tcp_window_scaling = 1;
 1224 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_window_scaling, CTLFLAG_RWTUN, &enable_tcp_window_scaling, 0,
 1225                 "Enable tcp window scaling (default = 1)");
 1226 
 1227 int c4iw_debug = 0;
 1228 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_debug, CTLFLAG_RWTUN, &c4iw_debug, 0,
 1229                 "Enable debug logging (default = 0)");
 1230 
 1231 static int peer2peer = 1;
 1232 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, peer2peer, CTLFLAG_RWTUN, &peer2peer, 0,
 1233                 "Support peer2peer ULPs (default = 1)");
 1234 
 1235 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
 1236 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, p2p_type, CTLFLAG_RWTUN, &p2p_type, 0,
 1237                 "RDMAP opcode to use for the RTR message: 1 = RDMA_READ 0 = RDMA_WRITE (default 1)");
 1238 
 1239 static int ep_timeout_secs = 60;
 1240 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, ep_timeout_secs, CTLFLAG_RWTUN, &ep_timeout_secs, 0,
 1241                 "CM Endpoint operation timeout in seconds (default = 60)");
 1242 
 1243 static int mpa_rev = 1;
 1244 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
 1245                 "MPA Revision, 0 supports amso1100, 1 is RFC5044 spec compliant, 2 is IETF MPA Peer Connect Draft compliant (default = 1)");
 1246 
 1247 static int markers_enabled;
 1248 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, markers_enabled, CTLFLAG_RWTUN, &markers_enabled, 0,
 1249                 "Enable MPA MARKERS (default(0) = disabled)");
 1250 
 1251 static int crc_enabled = 1;
 1252 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, crc_enabled, CTLFLAG_RWTUN, &crc_enabled, 0,
 1253                 "Enable MPA CRC (default(1) = enabled)");
 1254 
 1255 static int rcv_win = 256 * 1024;
 1256 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, rcv_win, CTLFLAG_RWTUN, &rcv_win, 0,
 1257                 "TCP receive window in bytes (default = 256KB)");
 1258 
 1259 static int snd_win = 128 * 1024;
 1260 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, snd_win, CTLFLAG_RWTUN, &snd_win, 0,
 1261                 "TCP send window in bytes (default = 128KB)");
 1262 
 1263 int use_dsgl = 1;
 1264 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, use_dsgl, CTLFLAG_RWTUN, &use_dsgl, 0,
 1265                 "Use DSGL for PBL/FastReg (default=1)");
 1266 
 1267 int inline_threshold = 128;
 1268 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, inline_threshold, CTLFLAG_RWTUN, &inline_threshold, 0,
 1269                 "inline vs dsgl threshold (default=128)");
 1270 
 1271 static int reuseaddr = 0;
 1272 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, reuseaddr, CTLFLAG_RWTUN, &reuseaddr, 0,
 1273                 "Enable SO_REUSEADDR & SO_REUSEPORT socket options on all iWARP client connections(default = 0)");
 1274 
 1275 static void
 1276 start_ep_timer(struct c4iw_ep *ep)
 1277 {
 1278 
 1279         if (timer_pending(&ep->timer)) {
 1280                 CTR2(KTR_IW_CXGBE, "%s: ep %p, already started", __func__, ep);
 1281                 printk(KERN_ERR "%s timer already started! ep %p\n", __func__,
 1282                     ep);
 1283                 return;
 1284         }
 1285         clear_bit(TIMEOUT, &ep->com.flags);
 1286         c4iw_get_ep(&ep->com);
 1287         ep->timer.expires = jiffies + ep_timeout_secs * HZ;
 1288         ep->timer.data = (unsigned long)ep;
 1289         ep->timer.function = ep_timeout;
 1290         add_timer(&ep->timer);
 1291 }
 1292 
 1293 static int
 1294 stop_ep_timer(struct c4iw_ep *ep)
 1295 {
 1296 
 1297         del_timer_sync(&ep->timer);
 1298         if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
 1299                 c4iw_put_ep(&ep->com);
 1300                 return 0;
 1301         }
 1302         return 1;
 1303 }
 1304 
 1305 static void *
 1306 alloc_ep(int size, gfp_t gfp)
 1307 {
 1308         struct c4iw_ep_common *epc;
 1309 
 1310         epc = kzalloc(size, gfp);
 1311         if (epc == NULL)
 1312                 return (NULL);
 1313 
 1314         kref_init(&epc->kref);
 1315         mutex_init(&epc->mutex);
 1316         c4iw_init_wr_wait(&epc->wr_wait);
 1317 
 1318         return (epc);
 1319 }
 1320 
 1321 void _c4iw_free_ep(struct kref *kref)
 1322 {
 1323         struct c4iw_ep *ep;
 1324 #if defined(KTR) || defined(INVARIANTS)
 1325         struct c4iw_ep_common *epc;
 1326 #endif
 1327 
 1328         ep = container_of(kref, struct c4iw_ep, com.kref);
 1329 #if defined(KTR) || defined(INVARIANTS)
 1330         epc = &ep->com;
 1331 #endif
 1332         KASSERT(!epc->entry.tqe_prev, ("%s epc %p still on req list",
 1333             __func__, epc));
 1334         if (test_bit(QP_REFERENCED, &ep->com.flags))
 1335                 deref_qp(ep);
 1336         CTR4(KTR_IW_CXGBE, "%s: ep %p, history 0x%lx, flags 0x%lx",
 1337             __func__, ep, epc->history, epc->flags);
 1338         kfree(ep);
 1339 }
 1340 
 1341 static void release_ep_resources(struct c4iw_ep *ep)
 1342 {
 1343         CTR2(KTR_IW_CXGBE, "%s:rerB %p", __func__, ep);
 1344         set_bit(RELEASE_RESOURCES, &ep->com.flags);
 1345         c4iw_put_ep(&ep->com);
 1346         CTR2(KTR_IW_CXGBE, "%s:rerE %p", __func__, ep);
 1347 }
 1348 
 1349 static int
 1350 send_mpa_req(struct c4iw_ep *ep)
 1351 {
 1352         int mpalen;
 1353         struct mpa_message *mpa;
 1354         struct mpa_v2_conn_params mpa_v2_params;
 1355         struct mbuf *m;
 1356         char mpa_rev_to_use = mpa_rev;
 1357         int err = 0;
 1358 
 1359         if (ep->retry_with_mpa_v1)
 1360                 mpa_rev_to_use = 1;
 1361         mpalen = sizeof(*mpa) + ep->plen;
 1362         if (mpa_rev_to_use == 2)
 1363                 mpalen += sizeof(struct mpa_v2_conn_params);
 1364 
 1365         mpa = malloc(mpalen, M_CXGBE, M_NOWAIT);
 1366         if (mpa == NULL) {
 1367                 err = -ENOMEM;
 1368                 CTR3(KTR_IW_CXGBE, "%s:smr1 ep: %p , error: %d",
 1369                                 __func__, ep, err);
 1370                 goto err;
 1371         }
 1372 
 1373         memset(mpa, 0, mpalen);
 1374         memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
 1375         mpa->flags = (crc_enabled ? MPA_CRC : 0) |
 1376                 (markers_enabled ? MPA_MARKERS : 0) |
 1377                 (mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
 1378         mpa->private_data_size = htons(ep->plen);
 1379         mpa->revision = mpa_rev_to_use;
 1380 
 1381         if (mpa_rev_to_use == 1) {
 1382                 ep->tried_with_mpa_v1 = 1;
 1383                 ep->retry_with_mpa_v1 = 0;
 1384         }
 1385 
 1386         if (mpa_rev_to_use == 2) {
 1387                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
 1388                                             sizeof(struct mpa_v2_conn_params));
 1389                 mpa_v2_params.ird = htons((u16)ep->ird);
 1390                 mpa_v2_params.ord = htons((u16)ep->ord);
 1391 
 1392                 if (peer2peer) {
 1393                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
 1394 
 1395                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
 1396                                 mpa_v2_params.ord |=
 1397                                     htons(MPA_V2_RDMA_WRITE_RTR);
 1398                         } else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
 1399                                 mpa_v2_params.ord |=
 1400                                         htons(MPA_V2_RDMA_READ_RTR);
 1401                         }
 1402                 }
 1403                 memcpy(mpa->private_data, &mpa_v2_params,
 1404                         sizeof(struct mpa_v2_conn_params));
 1405 
 1406                 if (ep->plen) {
 1407 
 1408                         memcpy(mpa->private_data +
 1409                                 sizeof(struct mpa_v2_conn_params),
 1410                                 ep->mpa_pkt + sizeof(*mpa), ep->plen);
 1411                 }
 1412         } else {
 1413 
 1414                 if (ep->plen)
 1415                         memcpy(mpa->private_data,
 1416                                         ep->mpa_pkt + sizeof(*mpa), ep->plen);
 1417                 CTR2(KTR_IW_CXGBE, "%s:smr7 %p", __func__, ep);
 1418         }
 1419 
 1420         m = m_getm(NULL, mpalen, M_NOWAIT, MT_DATA);
 1421         if (m == NULL) {
 1422                 err = -ENOMEM;
 1423                 CTR3(KTR_IW_CXGBE, "%s:smr2 ep: %p , error: %d",
 1424                                 __func__, ep, err);
 1425                 free(mpa, M_CXGBE);
 1426                 goto err;
 1427         }
 1428         m_copyback(m, 0, mpalen, (void *)mpa);
 1429         free(mpa, M_CXGBE);
 1430 
 1431         err = -sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT,
 1432                         ep->com.thread);
 1433         if (err) {
 1434                 CTR3(KTR_IW_CXGBE, "%s:smr3 ep: %p , error: %d",
 1435                                 __func__, ep, err);
 1436                 goto err;
 1437         }
 1438 
 1439         START_EP_TIMER(ep);
 1440         ep->com.state = MPA_REQ_SENT;
 1441         ep->mpa_attr.initiator = 1;
 1442         CTR3(KTR_IW_CXGBE, "%s:smrE %p, error: %d", __func__, ep, err);
 1443         return 0;
 1444 err:
 1445         connect_reply_upcall(ep, err);
 1446         CTR3(KTR_IW_CXGBE, "%s:smrE %p, error: %d", __func__, ep, err);
 1447         return err;
 1448 }
 1449 
 1450 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
 1451 {
 1452         int mpalen ;
 1453         struct mpa_message *mpa;
 1454         struct mpa_v2_conn_params mpa_v2_params;
 1455         struct mbuf *m;
 1456         int err;
 1457 
 1458         CTR4(KTR_IW_CXGBE, "%s:smrejB %p %u %d", __func__, ep, ep->hwtid,
 1459             ep->plen);
 1460 
 1461         mpalen = sizeof(*mpa) + plen;
 1462 
 1463         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
 1464 
 1465                 mpalen += sizeof(struct mpa_v2_conn_params);
 1466                 CTR4(KTR_IW_CXGBE, "%s:smrej1 %p %u %d", __func__, ep,
 1467                     ep->mpa_attr.version, mpalen);
 1468         }
 1469 
 1470         mpa = malloc(mpalen, M_CXGBE, M_NOWAIT);
 1471         if (mpa == NULL)
 1472                 return (-ENOMEM);
 1473 
 1474         memset(mpa, 0, mpalen);
 1475         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
 1476         mpa->flags = MPA_REJECT;
 1477         mpa->revision = mpa_rev;
 1478         mpa->private_data_size = htons(plen);
 1479 
 1480         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
 1481 
 1482                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
 1483                 mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
 1484                                             sizeof(struct mpa_v2_conn_params));
 1485                 mpa_v2_params.ird = htons(((u16)ep->ird) |
 1486                                 (peer2peer ? MPA_V2_PEER2PEER_MODEL :
 1487                                  0));
 1488                 mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
 1489                                         (p2p_type ==
 1490                                          FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
 1491                                          MPA_V2_RDMA_WRITE_RTR : p2p_type ==
 1492                                          FW_RI_INIT_P2PTYPE_READ_REQ ?
 1493                                          MPA_V2_RDMA_READ_RTR : 0) : 0));
 1494                 memcpy(mpa->private_data, &mpa_v2_params,
 1495                                 sizeof(struct mpa_v2_conn_params));
 1496 
 1497                 if (ep->plen)
 1498                         memcpy(mpa->private_data +
 1499                                 sizeof(struct mpa_v2_conn_params), pdata, plen);
 1500                 CTR5(KTR_IW_CXGBE, "%s:smrej3 %p %d %d %d", __func__, ep,
 1501                     mpa_v2_params.ird, mpa_v2_params.ord, ep->plen);
 1502         } else
 1503                 if (plen)
 1504                         memcpy(mpa->private_data, pdata, plen);
 1505 
 1506         m = m_getm(NULL, mpalen, M_NOWAIT, MT_DATA);
 1507         if (m == NULL) {
 1508                 free(mpa, M_CXGBE);
 1509                 return (-ENOMEM);
 1510         }
 1511         m_copyback(m, 0, mpalen, (void *)mpa);
 1512         free(mpa, M_CXGBE);
 1513 
 1514         err = -sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT, ep->com.thread);
 1515         if (!err)
 1516                 ep->snd_seq += mpalen;
 1517         CTR4(KTR_IW_CXGBE, "%s:smrejE %p %u %d", __func__, ep, ep->hwtid, err);
 1518         return err;
 1519 }
 1520 
 1521 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
 1522 {
 1523         int mpalen;
 1524         struct mpa_message *mpa;
 1525         struct mbuf *m;
 1526         struct mpa_v2_conn_params mpa_v2_params;
 1527         int err;
 1528 
 1529         CTR2(KTR_IW_CXGBE, "%s:smrepB %p", __func__, ep);
 1530 
 1531         mpalen = sizeof(*mpa) + plen;
 1532 
 1533         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
 1534 
 1535                 CTR3(KTR_IW_CXGBE, "%s:smrep1 %p %d", __func__, ep,
 1536                     ep->mpa_attr.version);
 1537                 mpalen += sizeof(struct mpa_v2_conn_params);
 1538         }
 1539 
 1540         mpa = malloc(mpalen, M_CXGBE, M_NOWAIT);
 1541         if (mpa == NULL)
 1542                 return (-ENOMEM);
 1543 
 1544         memset(mpa, 0, sizeof(*mpa));
 1545         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
 1546         mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
 1547                 (markers_enabled ? MPA_MARKERS : 0);
 1548         mpa->revision = ep->mpa_attr.version;
 1549         mpa->private_data_size = htons(plen);
 1550 
 1551         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
 1552 
 1553                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
 1554                 mpa->private_data_size +=
 1555                         htons(sizeof(struct mpa_v2_conn_params));
 1556                 mpa_v2_params.ird = htons((u16)ep->ird);
 1557                 mpa_v2_params.ord = htons((u16)ep->ord);
 1558                 CTR5(KTR_IW_CXGBE, "%s:smrep3 %p %d %d %d", __func__, ep,
 1559                     ep->mpa_attr.version, mpa_v2_params.ird, mpa_v2_params.ord);
 1560 
 1561                 if (peer2peer && (ep->mpa_attr.p2p_type !=
 1562                         FW_RI_INIT_P2PTYPE_DISABLED)) {
 1563 
 1564                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
 1565 
 1566                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
 1567 
 1568                                 mpa_v2_params.ord |=
 1569                                         htons(MPA_V2_RDMA_WRITE_RTR);
 1570                                 CTR5(KTR_IW_CXGBE, "%s:smrep4 %p %d %d %d",
 1571                                     __func__, ep, p2p_type, mpa_v2_params.ird,
 1572                                     mpa_v2_params.ord);
 1573                         }
 1574                         else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
 1575 
 1576                                 mpa_v2_params.ord |=
 1577                                         htons(MPA_V2_RDMA_READ_RTR);
 1578                                 CTR5(KTR_IW_CXGBE, "%s:smrep5 %p %d %d %d",
 1579                                     __func__, ep, p2p_type, mpa_v2_params.ird,
 1580                                     mpa_v2_params.ord);
 1581                         }
 1582                 }
 1583 
 1584                 memcpy(mpa->private_data, &mpa_v2_params,
 1585                         sizeof(struct mpa_v2_conn_params));
 1586 
 1587                 if (ep->plen)
 1588                         memcpy(mpa->private_data +
 1589                                 sizeof(struct mpa_v2_conn_params), pdata, plen);
 1590         } else
 1591                 if (plen)
 1592                         memcpy(mpa->private_data, pdata, plen);
 1593 
 1594         m = m_getm(NULL, mpalen, M_NOWAIT, MT_DATA);
 1595         if (m == NULL) {
 1596                 free(mpa, M_CXGBE);
 1597                 return (-ENOMEM);
 1598         }
 1599         m_copyback(m, 0, mpalen, (void *)mpa);
 1600         free(mpa, M_CXGBE);
 1601 
 1602 
 1603         ep->com.state = MPA_REP_SENT;
 1604         ep->snd_seq += mpalen;
 1605         err = -sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT,
 1606                         ep->com.thread);
 1607         CTR3(KTR_IW_CXGBE, "%s:smrepE %p %d", __func__, ep, err);
 1608         return err;
 1609 }
 1610 
 1611 
 1612 
 1613 static void close_complete_upcall(struct c4iw_ep *ep, int status)
 1614 {
 1615         struct iw_cm_event event;
 1616 
 1617         CTR2(KTR_IW_CXGBE, "%s:ccuB %p", __func__, ep);
 1618         memset(&event, 0, sizeof(event));
 1619         event.event = IW_CM_EVENT_CLOSE;
 1620         event.status = status;
 1621 
 1622         if (ep->com.cm_id) {
 1623 
 1624                 CTR2(KTR_IW_CXGBE, "%s:ccu1 %1", __func__, ep);
 1625                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
 1626                 deref_cm_id(&ep->com);
 1627                 set_bit(CLOSE_UPCALL, &ep->com.history);
 1628         }
 1629         CTR2(KTR_IW_CXGBE, "%s:ccuE %p", __func__, ep);
 1630 }
 1631 
 1632 static int
 1633 send_abort(struct c4iw_ep *ep)
 1634 {
 1635         struct socket *so = ep->com.so;
 1636         struct sockopt sopt;
 1637         int rc;
 1638         struct linger l;
 1639 
 1640         CTR5(KTR_IW_CXGBE, "%s ep %p so %p state %s tid %d", __func__, ep, so,
 1641             states[ep->com.state], ep->hwtid);
 1642 
 1643         l.l_onoff = 1;
 1644         l.l_linger = 0;
 1645 
 1646         /* linger_time of 0 forces RST to be sent */
 1647         sopt.sopt_dir = SOPT_SET;
 1648         sopt.sopt_level = SOL_SOCKET;
 1649         sopt.sopt_name = SO_LINGER;
 1650         sopt.sopt_val = (caddr_t)&l;
 1651         sopt.sopt_valsize = sizeof l;
 1652         sopt.sopt_td = NULL;
 1653         rc = -sosetopt(so, &sopt);
 1654         if (rc != 0) {
 1655                 log(LOG_ERR, "%s: sosetopt(%p, linger = 0) failed with %d.\n",
 1656                     __func__, so, rc);
 1657         }
 1658 
 1659         uninit_iwarp_socket(so);
 1660         soclose(so);
 1661         set_bit(ABORT_CONN, &ep->com.history);
 1662 
 1663         /*
 1664          * TBD: iw_cxgbe driver should receive ABORT reply for every ABORT
 1665          * request it has sent. But the current TOE driver is not propagating
 1666          * this ABORT reply event (via do_abort_rpl) to iw_cxgbe. So as a work-
 1667          * around de-refererece 'ep' here instead of doing it in abort_rpl()
 1668          * handler(not yet implemented) of iw_cxgbe driver.
 1669          */
 1670         release_ep_resources(ep);
 1671         ep->com.state = DEAD;
 1672 
 1673         return (0);
 1674 }
 1675 
 1676 static void peer_close_upcall(struct c4iw_ep *ep)
 1677 {
 1678         struct iw_cm_event event;
 1679 
 1680         CTR2(KTR_IW_CXGBE, "%s:pcuB %p", __func__, ep);
 1681         memset(&event, 0, sizeof(event));
 1682         event.event = IW_CM_EVENT_DISCONNECT;
 1683 
 1684         if (ep->com.cm_id) {
 1685 
 1686                 CTR2(KTR_IW_CXGBE, "%s:pcu1 %p", __func__, ep);
 1687                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
 1688                 set_bit(DISCONN_UPCALL, &ep->com.history);
 1689         }
 1690         CTR2(KTR_IW_CXGBE, "%s:pcuE %p", __func__, ep);
 1691 }
 1692 
 1693 static void peer_abort_upcall(struct c4iw_ep *ep)
 1694 {
 1695         struct iw_cm_event event;
 1696 
 1697         CTR2(KTR_IW_CXGBE, "%s:pauB %p", __func__, ep);
 1698         memset(&event, 0, sizeof(event));
 1699         event.event = IW_CM_EVENT_CLOSE;
 1700         event.status = -ECONNRESET;
 1701 
 1702         if (ep->com.cm_id) {
 1703 
 1704                 CTR2(KTR_IW_CXGBE, "%s:pau1 %p", __func__, ep);
 1705                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
 1706                 deref_cm_id(&ep->com);
 1707                 set_bit(ABORT_UPCALL, &ep->com.history);
 1708         }
 1709         CTR2(KTR_IW_CXGBE, "%s:pauE %p", __func__, ep);
 1710 }
 1711 
 1712 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
 1713 {
 1714         struct iw_cm_event event;
 1715 
 1716         CTR3(KTR_IW_CXGBE, "%s:cruB %p, status: %d", __func__, ep, status);
 1717         memset(&event, 0, sizeof(event));
 1718         event.event = IW_CM_EVENT_CONNECT_REPLY;
 1719         event.status = ((status == -ECONNABORTED) || (status == -EPIPE)) ?
 1720                                         -ECONNRESET : status;
 1721         event.local_addr = ep->com.local_addr;
 1722         event.remote_addr = ep->com.remote_addr;
 1723 
 1724         if ((status == 0) || (status == -ECONNREFUSED)) {
 1725 
 1726                 if (!ep->tried_with_mpa_v1) {
 1727 
 1728                         CTR2(KTR_IW_CXGBE, "%s:cru1 %p", __func__, ep);
 1729                         /* this means MPA_v2 is used */
 1730                         event.ord = ep->ird;
 1731                         event.ird = ep->ord;
 1732                         event.private_data_len = ep->plen -
 1733                                 sizeof(struct mpa_v2_conn_params);
 1734                         event.private_data = ep->mpa_pkt +
 1735                                 sizeof(struct mpa_message) +
 1736                                 sizeof(struct mpa_v2_conn_params);
 1737                 } else {
 1738 
 1739                         CTR2(KTR_IW_CXGBE, "%s:cru2 %p", __func__, ep);
 1740                         /* this means MPA_v1 is used */
 1741                         event.ord = c4iw_max_read_depth;
 1742                         event.ird = c4iw_max_read_depth;
 1743                         event.private_data_len = ep->plen;
 1744                         event.private_data = ep->mpa_pkt +
 1745                                 sizeof(struct mpa_message);
 1746                 }
 1747         }
 1748 
 1749         if (ep->com.cm_id) {
 1750 
 1751                 CTR2(KTR_IW_CXGBE, "%s:cru3 %p", __func__, ep);
 1752                 set_bit(CONN_RPL_UPCALL, &ep->com.history);
 1753                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
 1754         }
 1755 
 1756         if(status == -ECONNABORTED) {
 1757 
 1758                 CTR3(KTR_IW_CXGBE, "%s:cruE %p %d", __func__, ep, status);
 1759                 return;
 1760         }
 1761 
 1762         if (status < 0) {
 1763 
 1764                 CTR3(KTR_IW_CXGBE, "%s:cru4 %p %d", __func__, ep, status);
 1765                 deref_cm_id(&ep->com);
 1766         }
 1767 
 1768         CTR2(KTR_IW_CXGBE, "%s:cruE %p", __func__, ep);
 1769 }
 1770 
 1771 static int connect_request_upcall(struct c4iw_ep *ep)
 1772 {
 1773         struct iw_cm_event event;
 1774         int ret;
 1775 
 1776         CTR3(KTR_IW_CXGBE, "%s: ep %p, mpa_v1 %d", __func__, ep,
 1777             ep->tried_with_mpa_v1);
 1778 
 1779         memset(&event, 0, sizeof(event));
 1780         event.event = IW_CM_EVENT_CONNECT_REQUEST;
 1781         event.local_addr = ep->com.local_addr;
 1782         event.remote_addr = ep->com.remote_addr;
 1783         event.provider_data = ep;
 1784 
 1785         if (!ep->tried_with_mpa_v1) {
 1786                 /* this means MPA_v2 is used */
 1787                 event.ord = ep->ord;
 1788                 event.ird = ep->ird;
 1789                 event.private_data_len = ep->plen -
 1790                         sizeof(struct mpa_v2_conn_params);
 1791                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
 1792                         sizeof(struct mpa_v2_conn_params);
 1793         } else {
 1794 
 1795                 /* this means MPA_v1 is used. Send max supported */
 1796                 event.ord = c4iw_max_read_depth;
 1797                 event.ird = c4iw_max_read_depth;
 1798                 event.private_data_len = ep->plen;
 1799                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
 1800         }
 1801 
 1802         c4iw_get_ep(&ep->com);
 1803         ret = ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
 1804             &event);
 1805         if(ret) {
 1806                 CTR3(KTR_IW_CXGBE, "%s: ep %p, Failure while notifying event to"
 1807                         " IWCM, err:%d", __func__, ep, ret);
 1808                 c4iw_put_ep(&ep->com);
 1809         } else
 1810                 /* Dereference parent_ep only in success case.
 1811                  * In case of failure, parent_ep is dereferenced by the caller
 1812                  * of process_mpa_request().
 1813                  */
 1814                 c4iw_put_ep(&ep->parent_ep->com);
 1815 
 1816         set_bit(CONNREQ_UPCALL, &ep->com.history);
 1817         return ret;
 1818 }
 1819 
 1820 static void established_upcall(struct c4iw_ep *ep)
 1821 {
 1822         struct iw_cm_event event;
 1823 
 1824         CTR2(KTR_IW_CXGBE, "%s:euB %p", __func__, ep);
 1825         memset(&event, 0, sizeof(event));
 1826         event.event = IW_CM_EVENT_ESTABLISHED;
 1827         event.ird = ep->ord;
 1828         event.ord = ep->ird;
 1829 
 1830         if (ep->com.cm_id) {
 1831 
 1832                 CTR2(KTR_IW_CXGBE, "%s:eu1 %p", __func__, ep);
 1833                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
 1834                 set_bit(ESTAB_UPCALL, &ep->com.history);
 1835         }
 1836         CTR2(KTR_IW_CXGBE, "%s:euE %p", __func__, ep);
 1837 }
 1838 
 1839 
 1840 #define RELAXED_IRD_NEGOTIATION 1
 1841 
 1842 /*
 1843  * process_mpa_reply - process streaming mode MPA reply
 1844  *
 1845  * Returns:
 1846  *
 1847  * 0 upon success indicating a connect request was delivered to the ULP
 1848  * or the mpa request is incomplete but valid so far.
 1849  *
 1850  * 1 if a failure requires the caller to close the connection.
 1851  *
 1852  * 2 if a failure requires the caller to abort the connection.
 1853  */
 1854 static int process_mpa_reply(struct c4iw_ep *ep)
 1855 {
 1856         struct mpa_message *mpa;
 1857         struct mpa_v2_conn_params *mpa_v2_params;
 1858         u16 plen;
 1859         u16 resp_ird, resp_ord;
 1860         u8 rtr_mismatch = 0, insuff_ird = 0;
 1861         struct c4iw_qp_attributes attrs = {0};
 1862         enum c4iw_qp_attr_mask mask;
 1863         int err;
 1864         struct mbuf *top, *m;
 1865         int flags = MSG_DONTWAIT;
 1866         struct uio uio;
 1867         int disconnect = 0;
 1868 
 1869         CTR2(KTR_IW_CXGBE, "%s:pmrB %p", __func__, ep);
 1870 
 1871         /*
 1872          * Stop mpa timer.  If it expired, then
 1873          * we ignore the MPA reply.  process_timeout()
 1874          * will abort the connection.
 1875          */
 1876         if (STOP_EP_TIMER(ep))
 1877                 return 0;
 1878 
 1879         uio.uio_resid = 1000000;
 1880         uio.uio_td = ep->com.thread;
 1881         err = soreceive(ep->com.so, NULL, &uio, &top, NULL, &flags);
 1882 
 1883         if (err) {
 1884 
 1885                 if (err == EWOULDBLOCK) {
 1886 
 1887                         CTR2(KTR_IW_CXGBE, "%s:pmr1 %p", __func__, ep);
 1888                         START_EP_TIMER(ep);
 1889                         return 0;
 1890                 }
 1891                 err = -err;
 1892                 CTR2(KTR_IW_CXGBE, "%s:pmr2 %p", __func__, ep);
 1893                 goto err;
 1894         }
 1895 
 1896         if (ep->com.so->so_rcv.sb_mb) {
 1897 
 1898                 CTR2(KTR_IW_CXGBE, "%s:pmr3 %p", __func__, ep);
 1899                 printf("%s data after soreceive called! so %p sb_mb %p top %p\n",
 1900                        __func__, ep->com.so, ep->com.so->so_rcv.sb_mb, top);
 1901         }
 1902 
 1903         m = top;
 1904 
 1905         do {
 1906 
 1907                 CTR2(KTR_IW_CXGBE, "%s:pmr4 %p", __func__, ep);
 1908                 /*
 1909                  * If we get more than the supported amount of private data
 1910                  * then we must fail this connection.
 1911                  */
 1912                 if (ep->mpa_pkt_len + m->m_len > sizeof(ep->mpa_pkt)) {
 1913 
 1914                         CTR3(KTR_IW_CXGBE, "%s:pmr5 %p %d", __func__, ep,
 1915                             ep->mpa_pkt_len + m->m_len);
 1916                         err = (-EINVAL);
 1917                         goto err_stop_timer;
 1918                 }
 1919 
 1920                 /*
 1921                  * copy the new data into our accumulation buffer.
 1922                  */
 1923                 m_copydata(m, 0, m->m_len, &(ep->mpa_pkt[ep->mpa_pkt_len]));
 1924                 ep->mpa_pkt_len += m->m_len;
 1925                 if (!m->m_next)
 1926                         m = m->m_nextpkt;
 1927                 else
 1928                         m = m->m_next;
 1929         } while (m);
 1930 
 1931         m_freem(top);
 1932         /*
 1933          * if we don't even have the mpa message, then bail.
 1934          */
 1935         if (ep->mpa_pkt_len < sizeof(*mpa)) {
 1936                 return 0;
 1937         }
 1938         mpa = (struct mpa_message *) ep->mpa_pkt;
 1939 
 1940         /* Validate MPA header. */
 1941         if (mpa->revision > mpa_rev) {
 1942 
 1943                 CTR4(KTR_IW_CXGBE, "%s:pmr6 %p %d %d", __func__, ep,
 1944                     mpa->revision, mpa_rev);
 1945                 printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d, "
 1946                                 " Received = %d\n", __func__, mpa_rev, mpa->revision);
 1947                 err = -EPROTO;
 1948                 goto err_stop_timer;
 1949         }
 1950 
 1951         if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
 1952 
 1953                 CTR2(KTR_IW_CXGBE, "%s:pmr7 %p", __func__, ep);
 1954                 err = -EPROTO;
 1955                 goto err_stop_timer;
 1956         }
 1957 
 1958         plen = ntohs(mpa->private_data_size);
 1959 
 1960         /*
 1961          * Fail if there's too much private data.
 1962          */
 1963         if (plen > MPA_MAX_PRIVATE_DATA) {
 1964 
 1965                 CTR2(KTR_IW_CXGBE, "%s:pmr8 %p", __func__, ep);
 1966                 err = -EPROTO;
 1967                 goto err_stop_timer;
 1968         }
 1969 
 1970         /*
 1971          * If plen does not account for pkt size
 1972          */
 1973         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
 1974 
 1975                 CTR2(KTR_IW_CXGBE, "%s:pmr9 %p", __func__, ep);
 1976                 STOP_EP_TIMER(ep);
 1977                 err = -EPROTO;
 1978                 goto err_stop_timer;
 1979         }
 1980 
 1981         ep->plen = (u8) plen;
 1982 
 1983         /*
 1984          * If we don't have all the pdata yet, then bail.
 1985          * We'll continue process when more data arrives.
 1986          */
 1987         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen)) {
 1988 
 1989                 CTR2(KTR_IW_CXGBE, "%s:pmra %p", __func__, ep);
 1990                 return 0;
 1991         }
 1992 
 1993         if (mpa->flags & MPA_REJECT) {
 1994 
 1995                 CTR2(KTR_IW_CXGBE, "%s:pmrb %p", __func__, ep);
 1996                 err = -ECONNREFUSED;
 1997                 goto err_stop_timer;
 1998         }
 1999 
 2000         /*
 2001          * If we get here we have accumulated the entire mpa
 2002          * start reply message including private data. And
 2003          * the MPA header is valid.
 2004          */
 2005         ep->com.state = FPDU_MODE;
 2006         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
 2007         ep->mpa_attr.recv_marker_enabled = markers_enabled;
 2008         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
 2009         ep->mpa_attr.version = mpa->revision;
 2010         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
 2011 
 2012         if (mpa->revision == 2) {
 2013 
 2014                 CTR2(KTR_IW_CXGBE, "%s:pmrc %p", __func__, ep);
 2015                 ep->mpa_attr.enhanced_rdma_conn =
 2016                         mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
 2017 
 2018                 if (ep->mpa_attr.enhanced_rdma_conn) {
 2019 
 2020                         CTR2(KTR_IW_CXGBE, "%s:pmrd %p", __func__, ep);
 2021                         mpa_v2_params = (struct mpa_v2_conn_params *)
 2022                                 (ep->mpa_pkt + sizeof(*mpa));
 2023                         resp_ird = ntohs(mpa_v2_params->ird) &
 2024                                 MPA_V2_IRD_ORD_MASK;
 2025                         resp_ord = ntohs(mpa_v2_params->ord) &
 2026                                 MPA_V2_IRD_ORD_MASK;
 2027 
 2028                         /*
 2029                          * This is a double-check. Ideally, below checks are
 2030                          * not required since ird/ord stuff has been taken
 2031                          * care of in c4iw_accept_cr
 2032                          */
 2033                         if (ep->ird < resp_ord) {
 2034                                 if (RELAXED_IRD_NEGOTIATION && resp_ord <=
 2035                                    ep->com.dev->rdev.adap->params.max_ordird_qp)
 2036                                         ep->ird = resp_ord;
 2037                                 else
 2038                                         insuff_ird = 1;
 2039                         } else if (ep->ird > resp_ord) {
 2040                                 ep->ird = resp_ord;
 2041                         }
 2042                         if (ep->ord > resp_ird) {
 2043                                 if (RELAXED_IRD_NEGOTIATION)
 2044                                         ep->ord = resp_ird;
 2045                                 else
 2046                                         insuff_ird = 1;
 2047                         }
 2048                         if (insuff_ird) {
 2049                                 err = -ENOMEM;
 2050                                 ep->ird = resp_ord;
 2051                                 ep->ord = resp_ird;
 2052                         }
 2053 
 2054                         if (ntohs(mpa_v2_params->ird) &
 2055                                 MPA_V2_PEER2PEER_MODEL) {
 2056 
 2057                                 CTR2(KTR_IW_CXGBE, "%s:pmrf %p", __func__, ep);
 2058                                 if (ntohs(mpa_v2_params->ord) &
 2059                                         MPA_V2_RDMA_WRITE_RTR) {
 2060 
 2061                                         CTR2(KTR_IW_CXGBE, "%s:pmrg %p", __func__, ep);
 2062                                         ep->mpa_attr.p2p_type =
 2063                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
 2064                                 }
 2065                                 else if (ntohs(mpa_v2_params->ord) &
 2066                                         MPA_V2_RDMA_READ_RTR) {
 2067 
 2068                                         CTR2(KTR_IW_CXGBE, "%s:pmrh %p", __func__, ep);
 2069                                         ep->mpa_attr.p2p_type =
 2070                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
 2071                                 }
 2072                         }
 2073                 }
 2074         } else {
 2075 
 2076                 CTR2(KTR_IW_CXGBE, "%s:pmri %p", __func__, ep);
 2077 
 2078                 if (mpa->revision == 1) {
 2079 
 2080                         CTR2(KTR_IW_CXGBE, "%s:pmrj %p", __func__, ep);
 2081 
 2082                         if (peer2peer) {
 2083 
 2084                                 CTR2(KTR_IW_CXGBE, "%s:pmrk %p", __func__, ep);
 2085                                 ep->mpa_attr.p2p_type = p2p_type;
 2086                         }
 2087                 }
 2088         }
 2089 
 2090         if (set_tcpinfo(ep)) {
 2091 
 2092                 CTR2(KTR_IW_CXGBE, "%s:pmrl %p", __func__, ep);
 2093                 printf("%s set_tcpinfo error\n", __func__);
 2094                 err = -ECONNRESET;
 2095                 goto err;
 2096         }
 2097 
 2098         CTR6(KTR_IW_CXGBE, "%s - crc_enabled = %d, recv_marker_enabled = %d, "
 2099             "xmit_marker_enabled = %d, version = %d p2p_type = %d", __func__,
 2100             ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
 2101             ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
 2102             ep->mpa_attr.p2p_type);
 2103 
 2104         /*
 2105          * If responder's RTR does not match with that of initiator, assign
 2106          * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
 2107          * generated when moving QP to RTS state.
 2108          * A TERM message will be sent after QP has moved to RTS state
 2109          */
 2110         if ((ep->mpa_attr.version == 2) && peer2peer &&
 2111                 (ep->mpa_attr.p2p_type != p2p_type)) {
 2112 
 2113                 CTR2(KTR_IW_CXGBE, "%s:pmrm %p", __func__, ep);
 2114                 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
 2115                 rtr_mismatch = 1;
 2116         }
 2117 
 2118 
 2119         //ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
 2120         attrs.mpa_attr = ep->mpa_attr;
 2121         attrs.max_ird = ep->ird;
 2122         attrs.max_ord = ep->ord;
 2123         attrs.llp_stream_handle = ep;
 2124         attrs.next_state = C4IW_QP_STATE_RTS;
 2125 
 2126         mask = C4IW_QP_ATTR_NEXT_STATE |
 2127                 C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
 2128                 C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
 2129 
 2130         /* bind QP and TID with INIT_WR */
 2131         err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
 2132 
 2133         if (err) {
 2134 
 2135                 CTR2(KTR_IW_CXGBE, "%s:pmrn %p", __func__, ep);
 2136                 goto err;
 2137         }
 2138 
 2139         /*
 2140          * If responder's RTR requirement did not match with what initiator
 2141          * supports, generate TERM message
 2142          */
 2143         if (rtr_mismatch) {
 2144 
 2145                 CTR2(KTR_IW_CXGBE, "%s:pmro %p", __func__, ep);
 2146                 printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
 2147                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
 2148                 attrs.ecode = MPA_NOMATCH_RTR;
 2149                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
 2150                 attrs.send_term = 1;
 2151                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
 2152                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
 2153                 err = -ENOMEM;
 2154                 disconnect = 1;
 2155                 goto out;
 2156         }
 2157 
 2158         /*
 2159          * Generate TERM if initiator IRD is not sufficient for responder
 2160          * provided ORD. Currently, we do the same behaviour even when
 2161          * responder provided IRD is also not sufficient as regards to
 2162          * initiator ORD.
 2163          */
 2164         if (insuff_ird) {
 2165 
 2166                 CTR2(KTR_IW_CXGBE, "%s:pmrp %p", __func__, ep);
 2167                 printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
 2168                                 __func__);
 2169                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
 2170                 attrs.ecode = MPA_INSUFF_IRD;
 2171                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
 2172                 attrs.send_term = 1;
 2173                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
 2174                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
 2175                 err = -ENOMEM;
 2176                 disconnect = 1;
 2177                 goto out;
 2178         }
 2179         goto out;
 2180 err_stop_timer:
 2181         STOP_EP_TIMER(ep);
 2182 err:
 2183         disconnect = 2;
 2184 out:
 2185         connect_reply_upcall(ep, err);
 2186         CTR2(KTR_IW_CXGBE, "%s:pmrE %p", __func__, ep);
 2187         return disconnect;
 2188 }
 2189 
 2190 /*
 2191  * process_mpa_request - process streaming mode MPA request
 2192  *
 2193  * Returns:
 2194  *
 2195  * 0 upon success indicating a connect request was delivered to the ULP
 2196  * or the mpa request is incomplete but valid so far.
 2197  *
 2198  * 1 if a failure requires the caller to close the connection.
 2199  *
 2200  * 2 if a failure requires the caller to abort the connection.
 2201  */
 2202 static int
 2203 process_mpa_request(struct c4iw_ep *ep)
 2204 {
 2205         struct mpa_message *mpa;
 2206         struct mpa_v2_conn_params *mpa_v2_params;
 2207         u16 plen;
 2208         int flags = MSG_DONTWAIT;
 2209         int rc;
 2210         struct iovec iov;
 2211         struct uio uio;
 2212         enum c4iw_ep_state state = ep->com.state;
 2213 
 2214         CTR3(KTR_IW_CXGBE, "%s: ep %p, state %s", __func__, ep, states[state]);
 2215 
 2216         if (state != MPA_REQ_WAIT)
 2217                 return 0;
 2218 
 2219         iov.iov_base = &ep->mpa_pkt[ep->mpa_pkt_len];
 2220         iov.iov_len = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
 2221         uio.uio_iov = &iov;
 2222         uio.uio_iovcnt = 1;
 2223         uio.uio_offset = 0;
 2224         uio.uio_resid = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
 2225         uio.uio_segflg = UIO_SYSSPACE;
 2226         uio.uio_rw = UIO_READ;
 2227         uio.uio_td = NULL; /* uio.uio_td = ep->com.thread; */
 2228 
 2229         rc = soreceive(ep->com.so, NULL, &uio, NULL, NULL, &flags);
 2230         if (rc == EAGAIN)
 2231                 return 0;
 2232         else if (rc)
 2233                 goto err_stop_timer;
 2234 
 2235         KASSERT(uio.uio_offset > 0, ("%s: sorecieve on so %p read no data",
 2236             __func__, ep->com.so));
 2237         ep->mpa_pkt_len += uio.uio_offset;
 2238 
 2239         /*
 2240          * If we get more than the supported amount of private data then we must
 2241          * fail this connection.  XXX: check so_rcv->sb_cc, or peek with another
 2242          * soreceive, or increase the size of mpa_pkt by 1 and abort if the last
 2243          * byte is filled by the soreceive above.
 2244          */
 2245 
 2246         /* Don't even have the MPA message.  Wait for more data to arrive. */
 2247         if (ep->mpa_pkt_len < sizeof(*mpa))
 2248                 return 0;
 2249         mpa = (struct mpa_message *) ep->mpa_pkt;
 2250 
 2251         /*
 2252          * Validate MPA Header.
 2253          */
 2254         if (mpa->revision > mpa_rev) {
 2255                 log(LOG_ERR, "%s: MPA version mismatch. Local = %d,"
 2256                     " Received = %d\n", __func__, mpa_rev, mpa->revision);
 2257                 goto err_stop_timer;
 2258         }
 2259 
 2260         if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
 2261                 goto err_stop_timer;
 2262 
 2263         /*
 2264          * Fail if there's too much private data.
 2265          */
 2266         plen = ntohs(mpa->private_data_size);
 2267         if (plen > MPA_MAX_PRIVATE_DATA)
 2268                 goto err_stop_timer;
 2269 
 2270         /*
 2271          * If plen does not account for pkt size
 2272          */
 2273         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
 2274                 goto err_stop_timer;
 2275 
 2276         ep->plen = (u8) plen;
 2277 
 2278         /*
 2279          * If we don't have all the pdata yet, then bail.
 2280          */
 2281         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
 2282                 return 0;
 2283 
 2284         /*
 2285          * If we get here we have accumulated the entire mpa
 2286          * start reply message including private data.
 2287          */
 2288         ep->mpa_attr.initiator = 0;
 2289         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
 2290         ep->mpa_attr.recv_marker_enabled = markers_enabled;
 2291         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
 2292         ep->mpa_attr.version = mpa->revision;
 2293         if (mpa->revision == 1)
 2294                 ep->tried_with_mpa_v1 = 1;
 2295         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
 2296 
 2297         if (mpa->revision == 2) {
 2298                 ep->mpa_attr.enhanced_rdma_conn =
 2299                     mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
 2300                 if (ep->mpa_attr.enhanced_rdma_conn) {
 2301                         mpa_v2_params = (struct mpa_v2_conn_params *)
 2302                                 (ep->mpa_pkt + sizeof(*mpa));
 2303                         ep->ird = ntohs(mpa_v2_params->ird) &
 2304                                 MPA_V2_IRD_ORD_MASK;
 2305                         ep->ird = min_t(u32, ep->ird,
 2306                                         cur_max_read_depth(ep->com.dev));
 2307                         ep->ord = ntohs(mpa_v2_params->ord) &
 2308                                 MPA_V2_IRD_ORD_MASK;
 2309                         ep->ord = min_t(u32, ep->ord,
 2310                                         cur_max_read_depth(ep->com.dev));
 2311                         CTR3(KTR_IW_CXGBE, "%s initiator ird %u ord %u",
 2312                                  __func__, ep->ird, ep->ord);
 2313                         if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
 2314                                 if (peer2peer) {
 2315                                         if (ntohs(mpa_v2_params->ord) &
 2316                                                         MPA_V2_RDMA_WRITE_RTR)
 2317                                                 ep->mpa_attr.p2p_type =
 2318                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
 2319                                         else if (ntohs(mpa_v2_params->ord) &
 2320                                                         MPA_V2_RDMA_READ_RTR)
 2321                                                 ep->mpa_attr.p2p_type =
 2322                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
 2323                                 }
 2324                 }
 2325         } else if (mpa->revision == 1 && peer2peer)
 2326                 ep->mpa_attr.p2p_type = p2p_type;
 2327 
 2328         if (set_tcpinfo(ep))
 2329                 goto err_stop_timer;
 2330 
 2331         CTR5(KTR_IW_CXGBE, "%s: crc_enabled = %d, recv_marker_enabled = %d, "
 2332             "xmit_marker_enabled = %d, version = %d", __func__,
 2333             ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
 2334             ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
 2335 
 2336         ep->com.state = MPA_REQ_RCVD;
 2337         STOP_EP_TIMER(ep);
 2338 
 2339         /* drive upcall */
 2340         if (ep->parent_ep->com.state != DEAD)
 2341                 if (connect_request_upcall(ep))
 2342                         goto err_out;
 2343         return 0;
 2344 
 2345 err_stop_timer:
 2346         STOP_EP_TIMER(ep);
 2347 err_out:
 2348         return 2;
 2349 }
 2350 
 2351 /*
 2352  * Upcall from the adapter indicating data has been transmitted.
 2353  * For us its just the single MPA request or reply.  We can now free
 2354  * the skb holding the mpa message.
 2355  */
 2356 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
 2357 {
 2358 #ifdef KTR
 2359         int err;
 2360 #endif
 2361         struct c4iw_ep *ep = to_ep(cm_id);
 2362         int abort = 0;
 2363 
 2364         mutex_lock(&ep->com.mutex);
 2365         CTR2(KTR_IW_CXGBE, "%s:crcB %p", __func__, ep);
 2366 
 2367         if ((ep->com.state == DEAD) ||
 2368                         (ep->com.state != MPA_REQ_RCVD)) {
 2369 
 2370                 CTR2(KTR_IW_CXGBE, "%s:crc1 %p", __func__, ep);
 2371                 mutex_unlock(&ep->com.mutex);
 2372                 c4iw_put_ep(&ep->com);
 2373                 return -ECONNRESET;
 2374         }
 2375         set_bit(ULP_REJECT, &ep->com.history);
 2376 
 2377         if (mpa_rev == 0) {
 2378 
 2379                 CTR2(KTR_IW_CXGBE, "%s:crc2 %p", __func__, ep);
 2380                 abort = 1;
 2381         }
 2382         else {
 2383 
 2384                 CTR2(KTR_IW_CXGBE, "%s:crc3 %p", __func__, ep);
 2385                 abort = send_mpa_reject(ep, pdata, pdata_len);
 2386         }
 2387         STOP_EP_TIMER(ep);
 2388 #ifdef KTR
 2389         err = c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
 2390 #else
 2391         c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
 2392 #endif
 2393         mutex_unlock(&ep->com.mutex);
 2394         c4iw_put_ep(&ep->com);
 2395         CTR3(KTR_IW_CXGBE, "%s:crc4 %p, err: %d", __func__, ep, err);
 2396         return 0;
 2397 }
 2398 
 2399 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
 2400 {
 2401         int err;
 2402         struct c4iw_qp_attributes attrs = {0};
 2403         enum c4iw_qp_attr_mask mask;
 2404         struct c4iw_ep *ep = to_ep(cm_id);
 2405         struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
 2406         struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
 2407         int abort = 0;
 2408 
 2409         mutex_lock(&ep->com.mutex);
 2410         CTR2(KTR_IW_CXGBE, "%s:cacB %p", __func__, ep);
 2411 
 2412         if ((ep->com.state == DEAD) ||
 2413                         (ep->com.state != MPA_REQ_RCVD)) {
 2414 
 2415                 CTR2(KTR_IW_CXGBE, "%s:cac1 %p", __func__, ep);
 2416                 err = -ECONNRESET;
 2417                 goto err_out;
 2418         }
 2419 
 2420         BUG_ON(!qp);
 2421 
 2422         set_bit(ULP_ACCEPT, &ep->com.history);
 2423 
 2424         if ((conn_param->ord > c4iw_max_read_depth) ||
 2425                 (conn_param->ird > c4iw_max_read_depth)) {
 2426 
 2427                 CTR2(KTR_IW_CXGBE, "%s:cac2 %p", __func__, ep);
 2428                 err = -EINVAL;
 2429                 goto err_abort;
 2430         }
 2431 
 2432         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
 2433 
 2434                 CTR2(KTR_IW_CXGBE, "%s:cac3 %p", __func__, ep);
 2435 
 2436                 if (conn_param->ord > ep->ird) {
 2437                         if (RELAXED_IRD_NEGOTIATION) {
 2438                                 conn_param->ord = ep->ird;
 2439                         } else {
 2440                                 ep->ird = conn_param->ird;
 2441                                 ep->ord = conn_param->ord;
 2442                                 send_mpa_reject(ep, conn_param->private_data,
 2443                                                 conn_param->private_data_len);
 2444                                 err = -ENOMEM;
 2445                                 goto err_abort;
 2446                         }
 2447                 }
 2448                 if (conn_param->ird < ep->ord) {
 2449                         if (RELAXED_IRD_NEGOTIATION &&
 2450                             ep->ord <= h->rdev.adap->params.max_ordird_qp) {
 2451                                 conn_param->ird = ep->ord;
 2452                         } else {
 2453                                 err = -ENOMEM;
 2454                                 goto err_abort;
 2455                         }
 2456                 }
 2457         }
 2458         ep->ird = conn_param->ird;
 2459         ep->ord = conn_param->ord;
 2460 
 2461         if (ep->mpa_attr.version == 1) {
 2462                 if (peer2peer && ep->ird == 0)
 2463                         ep->ird = 1;
 2464         } else {
 2465                 if (peer2peer &&
 2466                     (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
 2467                     (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
 2468                         ep->ird = 1;
 2469         }
 2470 
 2471         CTR4(KTR_IW_CXGBE, "%s %d ird %d ord %d", __func__, __LINE__,
 2472                         ep->ird, ep->ord);
 2473 
 2474         ep->com.cm_id = cm_id;
 2475         ref_cm_id(&ep->com);
 2476         ep->com.qp = qp;
 2477         ref_qp(ep);
 2478         //ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
 2479 
 2480         /* bind QP to EP and move to RTS */
 2481         attrs.mpa_attr = ep->mpa_attr;
 2482         attrs.max_ird = ep->ird;
 2483         attrs.max_ord = ep->ord;
 2484         attrs.llp_stream_handle = ep;
 2485         attrs.next_state = C4IW_QP_STATE_RTS;
 2486 
 2487         /* bind QP and TID with INIT_WR */
 2488         mask = C4IW_QP_ATTR_NEXT_STATE |
 2489                 C4IW_QP_ATTR_LLP_STREAM_HANDLE |
 2490                 C4IW_QP_ATTR_MPA_ATTR |
 2491                 C4IW_QP_ATTR_MAX_IRD |
 2492                 C4IW_QP_ATTR_MAX_ORD;
 2493 
 2494         err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
 2495         if (err) {
 2496                 CTR3(KTR_IW_CXGBE, "%s:caca %p, err: %d", __func__, ep, err);
 2497                 goto err_defef_cm_id;
 2498         }
 2499 
 2500         err = send_mpa_reply(ep, conn_param->private_data,
 2501                         conn_param->private_data_len);
 2502         if (err) {
 2503                 CTR3(KTR_IW_CXGBE, "%s:cacb %p, err: %d", __func__, ep, err);
 2504                 goto err_defef_cm_id;
 2505         }
 2506 
 2507         ep->com.state = FPDU_MODE;
 2508         established_upcall(ep);
 2509         mutex_unlock(&ep->com.mutex);
 2510         c4iw_put_ep(&ep->com);
 2511         CTR2(KTR_IW_CXGBE, "%s:cacE %p", __func__, ep);
 2512         return 0;
 2513 err_defef_cm_id:
 2514         deref_cm_id(&ep->com);
 2515 err_abort:
 2516         abort = 1;
 2517 err_out:
 2518         if (abort)
 2519                 c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
 2520         mutex_unlock(&ep->com.mutex);
 2521         c4iw_put_ep(&ep->com);
 2522         CTR2(KTR_IW_CXGBE, "%s:cacE err %p", __func__, ep);
 2523         return err;
 2524 }
 2525 
 2526 static int
 2527 c4iw_sock_create(struct sockaddr_storage *laddr, struct socket **so)
 2528 {
 2529         int ret;
 2530         int size, on;
 2531         struct socket *sock = NULL;
 2532         struct sockopt sopt;
 2533 
 2534         ret = sock_create_kern(laddr->ss_family,
 2535                         SOCK_STREAM, IPPROTO_TCP, &sock);
 2536         if (ret) {
 2537                 CTR2(KTR_IW_CXGBE, "%s:Failed to create TCP socket. err %d",
 2538                                 __func__, ret);
 2539                 return ret;
 2540         }
 2541 
 2542         if (reuseaddr) {
 2543                 bzero(&sopt, sizeof(struct sockopt));
 2544                 sopt.sopt_dir = SOPT_SET;
 2545                 sopt.sopt_level = SOL_SOCKET;
 2546                 sopt.sopt_name = SO_REUSEADDR;
 2547                 on = 1;
 2548                 sopt.sopt_val = &on;
 2549                 sopt.sopt_valsize = sizeof(on);
 2550                 ret = -sosetopt(sock, &sopt);
 2551                 if (ret != 0) {
 2552                         log(LOG_ERR, "%s: sosetopt(%p, SO_REUSEADDR) "
 2553                                 "failed with %d.\n", __func__, sock, ret);
 2554                 }
 2555                 bzero(&sopt, sizeof(struct sockopt));
 2556                 sopt.sopt_dir = SOPT_SET;
 2557                 sopt.sopt_level = SOL_SOCKET;
 2558                 sopt.sopt_name = SO_REUSEPORT;
 2559                 on = 1;
 2560                 sopt.sopt_val = &on;
 2561                 sopt.sopt_valsize = sizeof(on);
 2562                 ret = -sosetopt(sock, &sopt);
 2563                 if (ret != 0) {
 2564                         log(LOG_ERR, "%s: sosetopt(%p, SO_REUSEPORT) "
 2565                                 "failed with %d.\n", __func__, sock, ret);
 2566                 }
 2567         }
 2568 
 2569         ret = -sobind(sock, (struct sockaddr *)laddr, curthread);
 2570         if (ret) {
 2571                 CTR2(KTR_IW_CXGBE, "%s:Failed to bind socket. err %p",
 2572                                 __func__, ret);
 2573                 sock_release(sock);
 2574                 return ret;
 2575         }
 2576 
 2577         size = laddr->ss_family == AF_INET6 ?
 2578                 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
 2579         ret = sock_getname(sock, (struct sockaddr *)laddr, &size, 0);
 2580         if (ret) {
 2581                 CTR2(KTR_IW_CXGBE, "%s:sock_getname failed. err %p",
 2582                                 __func__, ret);
 2583                 sock_release(sock);
 2584                 return ret;
 2585         }
 2586 
 2587         *so = sock;
 2588         return 0;
 2589 }
 2590 
 2591 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
 2592 {
 2593         int err = 0;
 2594         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
 2595         struct c4iw_ep *ep = NULL;
 2596         struct ifnet    *nh_ifp;        /* Logical egress interface */
 2597         struct epoch_tracker et;
 2598 #ifdef VIMAGE
 2599         struct rdma_cm_id *rdma_id = (struct rdma_cm_id*)cm_id->context;
 2600         struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
 2601 #endif
 2602 
 2603         CTR2(KTR_IW_CXGBE, "%s:ccB %p", __func__, cm_id);
 2604 
 2605 
 2606         if ((conn_param->ord > c4iw_max_read_depth) ||
 2607                 (conn_param->ird > c4iw_max_read_depth)) {
 2608 
 2609                 CTR2(KTR_IW_CXGBE, "%s:cc1 %p", __func__, cm_id);
 2610                 err = -EINVAL;
 2611                 goto out;
 2612         }
 2613         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
 2614         cm_id->provider_data = ep;
 2615 
 2616         init_timer(&ep->timer);
 2617         ep->plen = conn_param->private_data_len;
 2618 
 2619         if (ep->plen) {
 2620 
 2621                 CTR2(KTR_IW_CXGBE, "%s:cc3 %p", __func__, ep);
 2622                 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
 2623                                 conn_param->private_data, ep->plen);
 2624         }
 2625         ep->ird = conn_param->ird;
 2626         ep->ord = conn_param->ord;
 2627 
 2628         if (peer2peer && ep->ord == 0) {
 2629 
 2630                 CTR2(KTR_IW_CXGBE, "%s:cc4 %p", __func__, ep);
 2631                 ep->ord = 1;
 2632         }
 2633 
 2634         ep->com.dev = dev;
 2635         ep->com.cm_id = cm_id;
 2636         ref_cm_id(&ep->com);
 2637         ep->com.qp = get_qhp(dev, conn_param->qpn);
 2638 
 2639         if (!ep->com.qp) {
 2640 
 2641                 CTR2(KTR_IW_CXGBE, "%s:cc5 %p", __func__, ep);
 2642                 err = -EINVAL;
 2643                 goto fail;
 2644         }
 2645         ref_qp(ep);
 2646         ep->com.thread = curthread;
 2647 
 2648         NET_EPOCH_ENTER(et);
 2649         CURVNET_SET(vnet);
 2650         err = get_ifnet_from_raddr(&cm_id->remote_addr, &nh_ifp);
 2651         CURVNET_RESTORE();
 2652         NET_EPOCH_EXIT(et);
 2653 
 2654         if (err) {
 2655 
 2656                 CTR2(KTR_IW_CXGBE, "%s:cc7 %p", __func__, ep);
 2657                 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
 2658                 err = EHOSTUNREACH;
 2659                 return err;
 2660         }
 2661 
 2662         if (!(nh_ifp->if_capenable & IFCAP_TOE) ||
 2663             TOEDEV(nh_ifp) == NULL) {
 2664                 err = -ENOPROTOOPT;
 2665                 goto fail;
 2666         }
 2667         ep->com.state = CONNECTING;
 2668         ep->tos = 0;
 2669         ep->com.local_addr = cm_id->local_addr;
 2670         ep->com.remote_addr = cm_id->remote_addr;
 2671 
 2672         err = c4iw_sock_create(&cm_id->local_addr, &ep->com.so);
 2673         if (err)
 2674                 goto fail;
 2675 
 2676         setiwsockopt(ep->com.so);
 2677         init_iwarp_socket(ep->com.so, &ep->com);
 2678         err = -soconnect(ep->com.so, (struct sockaddr *)&ep->com.remote_addr,
 2679                 ep->com.thread);
 2680         if (err)
 2681                 goto fail_free_so;
 2682         CTR2(KTR_IW_CXGBE, "%s:ccE, ep %p", __func__, ep);
 2683         return 0;
 2684 
 2685 fail_free_so:
 2686         uninit_iwarp_socket(ep->com.so);
 2687         ep->com.state = DEAD;
 2688         sock_release(ep->com.so);
 2689 fail:
 2690         deref_cm_id(&ep->com);
 2691         c4iw_put_ep(&ep->com);
 2692         ep = NULL;
 2693 out:
 2694         CTR2(KTR_IW_CXGBE, "%s:ccE Error %d", __func__, err);
 2695         return err;
 2696 }
 2697 
 2698 /*
 2699  * iwcm->create_listen.  Returns -errno on failure.
 2700  */
 2701 int
 2702 c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
 2703 {
 2704         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
 2705         struct c4iw_listen_ep *lep = NULL;
 2706         struct listen_port_info *port_info = NULL;
 2707         int rc = 0;
 2708 
 2709         CTR3(KTR_IW_CXGBE, "%s: cm_id %p, backlog %s", __func__, cm_id,
 2710                         backlog);
 2711         if (c4iw_fatal_error(&dev->rdev)) {
 2712                 CTR2(KTR_IW_CXGBE, "%s: cm_id %p, fatal error", __func__,
 2713                                cm_id);
 2714                 return -EIO;
 2715         }
 2716         lep = alloc_ep(sizeof(*lep), GFP_KERNEL);
 2717         lep->com.cm_id = cm_id;
 2718         ref_cm_id(&lep->com);
 2719         lep->com.dev = dev;
 2720         lep->backlog = backlog;
 2721         lep->com.local_addr = cm_id->local_addr;
 2722         lep->com.thread = curthread;
 2723         cm_id->provider_data = lep;
 2724         lep->com.state = LISTEN;
 2725 
 2726         /* In case of INDADDR_ANY, ibcore creates cmid for each device and
 2727          * invokes iw_cxgbe listener callbacks assuming that iw_cxgbe creates
 2728          * HW listeners for each device seperately. But toecore expects single
 2729          * solisten() call with INADDR_ANY address to create HW listeners on
 2730          * all devices for a given port number. So iw_cxgbe driver calls
 2731          * solisten() only once for INADDR_ANY(usually done at first time
 2732          * listener callback from ibcore). And all the subsequent INADDR_ANY
 2733          * listener callbacks from ibcore(for the same port address) do not
 2734          * invoke solisten() as first listener callback has already created
 2735          * listeners for all other devices(via solisten).
 2736          */
 2737         if (c4iw_any_addr((struct sockaddr *)&lep->com.local_addr)) {
 2738                 port_info = add_ep_to_listenlist(lep);
 2739                 /* skip solisten() if refcnt > 1, as the listeners were
 2740                  * already created by 'Master lep'
 2741                  */
 2742                 if (port_info->refcnt > 1) {
 2743                         /* As there will be only one listener socket for a TCP
 2744                          * port, copy Master lep's socket pointer to other lep's
 2745                          * that are belonging to same TCP port.
 2746                          */
 2747                         struct c4iw_listen_ep *head_lep =
 2748                                         container_of(port_info->lep_list.next,
 2749                                         struct c4iw_listen_ep, listen_ep_list);
 2750                         lep->com.so =  head_lep->com.so;
 2751                         goto out;
 2752                 }
 2753         }
 2754         rc = c4iw_sock_create(&cm_id->local_addr, &lep->com.so);
 2755         if (rc) {
 2756                 CTR2(KTR_IW_CXGBE, "%s:Failed to create socket. err %d",
 2757                                 __func__, rc);
 2758                 goto fail;
 2759         }
 2760 
 2761         rc = -solisten(lep->com.so, backlog, curthread);
 2762         if (rc) {
 2763                 CTR3(KTR_IW_CXGBE, "%s:Failed to listen on sock:%p. err %d",
 2764                                 __func__, lep->com.so, rc);
 2765                 goto fail_free_so;
 2766         }
 2767         init_iwarp_socket(lep->com.so, &lep->com);
 2768 out:
 2769         return 0;
 2770 
 2771 fail_free_so:
 2772         sock_release(lep->com.so);
 2773 fail:
 2774         if (port_info)
 2775                 rem_ep_from_listenlist(lep);
 2776         deref_cm_id(&lep->com);
 2777         c4iw_put_ep(&lep->com);
 2778         return rc;
 2779 }
 2780 
 2781 int
 2782 c4iw_destroy_listen(struct iw_cm_id *cm_id)
 2783 {
 2784         struct c4iw_listen_ep *lep = to_listen_ep(cm_id);
 2785 
 2786         mutex_lock(&lep->com.mutex);
 2787         CTR3(KTR_IW_CXGBE, "%s: cm_id %p, state %s", __func__, cm_id,
 2788             states[lep->com.state]);
 2789 
 2790         lep->com.state = DEAD;
 2791         if (c4iw_any_addr((struct sockaddr *)&lep->com.local_addr)) {
 2792                 /* if no refcount then close listen socket */
 2793                 if (!rem_ep_from_listenlist(lep))
 2794                         close_socket(lep->com.so);
 2795         } else
 2796                 close_socket(lep->com.so);
 2797         deref_cm_id(&lep->com);
 2798         mutex_unlock(&lep->com.mutex);
 2799         c4iw_put_ep(&lep->com);
 2800         return 0;
 2801 }
 2802 
 2803 int __c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
 2804 {
 2805         int ret;
 2806         mutex_lock(&ep->com.mutex);
 2807         ret = c4iw_ep_disconnect(ep, abrupt, gfp);
 2808         mutex_unlock(&ep->com.mutex);
 2809         return ret;
 2810 }
 2811 
 2812 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
 2813 {
 2814         int ret = 0;
 2815         int close = 0;
 2816         struct c4iw_rdev *rdev;
 2817 
 2818 
 2819         CTR2(KTR_IW_CXGBE, "%s:cedB %p", __func__, ep);
 2820 
 2821         rdev = &ep->com.dev->rdev;
 2822 
 2823         if (c4iw_fatal_error(rdev)) {
 2824                 CTR3(KTR_IW_CXGBE, "%s:ced1 fatal error %p %s", __func__, ep,
 2825                                         states[ep->com.state]);
 2826                 if (ep->com.state != DEAD) {
 2827                         send_abort(ep);
 2828                         ep->com.state = DEAD;
 2829                 }
 2830                 close_complete_upcall(ep, -ECONNRESET);
 2831                 return ECONNRESET;
 2832         }
 2833         CTR3(KTR_IW_CXGBE, "%s:ced2 %p %s", __func__, ep,
 2834             states[ep->com.state]);
 2835 
 2836         /*
 2837          * Ref the ep here in case we have fatal errors causing the
 2838          * ep to be released and freed.
 2839          */
 2840         c4iw_get_ep(&ep->com);
 2841         switch (ep->com.state) {
 2842 
 2843                 case MPA_REQ_WAIT:
 2844                 case MPA_REQ_SENT:
 2845                 case MPA_REQ_RCVD:
 2846                 case MPA_REP_SENT:
 2847                 case FPDU_MODE:
 2848                         close = 1;
 2849                         if (abrupt)
 2850                                 ep->com.state = ABORTING;
 2851                         else {
 2852                                 ep->com.state = CLOSING;
 2853                                 START_EP_TIMER(ep);
 2854                         }
 2855                         set_bit(CLOSE_SENT, &ep->com.flags);
 2856                         break;
 2857 
 2858                 case CLOSING:
 2859 
 2860                         if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
 2861 
 2862                                 close = 1;
 2863                                 if (abrupt) {
 2864                                         STOP_EP_TIMER(ep);
 2865                                         ep->com.state = ABORTING;
 2866                                 } else
 2867                                         ep->com.state = MORIBUND;
 2868                         }
 2869                         break;
 2870 
 2871                 case MORIBUND:
 2872                 case ABORTING:
 2873                 case DEAD:
 2874                         CTR3(KTR_IW_CXGBE,
 2875                             "%s ignoring disconnect ep %p state %u", __func__,
 2876                             ep, ep->com.state);
 2877                         break;
 2878 
 2879                 default:
 2880                         BUG();
 2881                         break;
 2882         }
 2883 
 2884 
 2885         if (close) {
 2886 
 2887                 CTR2(KTR_IW_CXGBE, "%s:ced3 %p", __func__, ep);
 2888 
 2889                 if (abrupt) {
 2890 
 2891                         CTR2(KTR_IW_CXGBE, "%s:ced4 %p", __func__, ep);
 2892                         set_bit(EP_DISC_ABORT, &ep->com.history);
 2893                         close_complete_upcall(ep, -ECONNRESET);
 2894                         send_abort(ep);
 2895                 } else {
 2896 
 2897                         CTR2(KTR_IW_CXGBE, "%s:ced5 %p", __func__, ep);
 2898                         set_bit(EP_DISC_CLOSE, &ep->com.history);
 2899 
 2900                         if (!ep->parent_ep)
 2901                                 ep->com.state = MORIBUND;
 2902 
 2903                         CURVNET_SET(ep->com.so->so_vnet);
 2904                         ret = sodisconnect(ep->com.so);
 2905                         CURVNET_RESTORE();
 2906                         if (ret) {
 2907                                 CTR2(KTR_IW_CXGBE, "%s:ced6 %p", __func__, ep);
 2908                                 STOP_EP_TIMER(ep);
 2909                                 send_abort(ep);
 2910                                 ep->com.state = DEAD;
 2911                                 close_complete_upcall(ep, -ECONNRESET);
 2912                                 set_bit(EP_DISC_FAIL, &ep->com.history);
 2913                                 if (ep->com.qp) {
 2914                                         struct c4iw_qp_attributes attrs = {0};
 2915 
 2916                                         attrs.next_state = C4IW_QP_STATE_ERROR;
 2917                                         ret = c4iw_modify_qp(
 2918                                                         ep->com.dev, ep->com.qp,
 2919                                                         C4IW_QP_ATTR_NEXT_STATE,
 2920                                                         &attrs, 1);
 2921                                         CTR3(KTR_IW_CXGBE, "%s:ced7 %p ret %d",
 2922                                                 __func__, ep, ret);
 2923                                 }
 2924                         }
 2925                 }
 2926         }
 2927         c4iw_put_ep(&ep->com);
 2928         CTR2(KTR_IW_CXGBE, "%s:cedE %p", __func__, ep);
 2929         return ret;
 2930 }
 2931 
 2932 #ifdef C4IW_EP_REDIRECT
 2933 int c4iw_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
 2934                 struct l2t_entry *l2t)
 2935 {
 2936         struct c4iw_ep *ep = ctx;
 2937 
 2938         if (ep->dst != old)
 2939                 return 0;
 2940 
 2941         PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
 2942                         l2t);
 2943         dst_hold(new);
 2944         cxgb4_l2t_release(ep->l2t);
 2945         ep->l2t = l2t;
 2946         dst_release(old);
 2947         ep->dst = new;
 2948         return 1;
 2949 }
 2950 #endif
 2951 
 2952 
 2953 
 2954 static void ep_timeout(unsigned long arg)
 2955 {
 2956         struct c4iw_ep *ep = (struct c4iw_ep *)arg;
 2957 
 2958         if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
 2959 
 2960                 /*
 2961                  * Only insert if it is not already on the list.
 2962                  */
 2963                 if (!(ep->com.ep_events & C4IW_EVENT_TIMEOUT)) {
 2964                         CTR2(KTR_IW_CXGBE, "%s:et1 %p", __func__, ep);
 2965                         add_ep_to_req_list(ep, C4IW_EVENT_TIMEOUT);
 2966                 }
 2967         }
 2968 }
 2969 
 2970 static int fw6_wr_rpl(struct adapter *sc, const __be64 *rpl)
 2971 {
 2972         uint64_t val = be64toh(*rpl);
 2973         int ret;
 2974         struct c4iw_wr_wait *wr_waitp;
 2975 
 2976         ret = (int)((val >> 8) & 0xff);
 2977         wr_waitp = (struct c4iw_wr_wait *)rpl[1];
 2978         CTR3(KTR_IW_CXGBE, "%s wr_waitp %p ret %u", __func__, wr_waitp, ret);
 2979         if (wr_waitp)
 2980                 c4iw_wake_up(wr_waitp, ret ? -ret : 0);
 2981 
 2982         return (0);
 2983 }
 2984 
 2985 static int fw6_cqe_handler(struct adapter *sc, const __be64 *rpl)
 2986 {
 2987         struct cqe_list_entry *cle;
 2988         unsigned long flag;
 2989 
 2990         cle = malloc(sizeof(*cle), M_CXGBE, M_NOWAIT);
 2991         cle->rhp = sc->iwarp_softc;
 2992         cle->err_cqe = *(const struct t4_cqe *)(&rpl[0]);
 2993 
 2994         spin_lock_irqsave(&err_cqe_lock, flag);
 2995         list_add_tail(&cle->entry, &err_cqe_list);
 2996         queue_work(c4iw_taskq, &c4iw_task);
 2997         spin_unlock_irqrestore(&err_cqe_lock, flag);
 2998 
 2999         return (0);
 3000 }
 3001 
 3002 static int
 3003 process_terminate(struct c4iw_ep *ep)
 3004 {
 3005         struct c4iw_qp_attributes attrs = {0};
 3006 
 3007         CTR2(KTR_IW_CXGBE, "%s:tB %p %d", __func__, ep);
 3008 
 3009         if (ep && ep->com.qp) {
 3010 
 3011                 printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n",
 3012                                 ep->hwtid, ep->com.qp->wq.sq.qid);
 3013                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
 3014                 c4iw_modify_qp(ep->com.dev, ep->com.qp, C4IW_QP_ATTR_NEXT_STATE, &attrs,
 3015                                 1);
 3016         } else
 3017                 printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n",
 3018                                                                 ep->hwtid);
 3019         CTR2(KTR_IW_CXGBE, "%s:tE %p %d", __func__, ep);
 3020 
 3021         return 0;
 3022 }
 3023 
 3024 int __init c4iw_cm_init(void)
 3025 {
 3026 
 3027         t4_register_cpl_handler(CPL_RDMA_TERMINATE, terminate);
 3028         t4_register_fw_msg_handler(FW6_TYPE_WR_RPL, fw6_wr_rpl);
 3029         t4_register_fw_msg_handler(FW6_TYPE_CQE, fw6_cqe_handler);
 3030         t4_register_an_handler(c4iw_ev_handler);
 3031 
 3032         TAILQ_INIT(&req_list);
 3033         spin_lock_init(&req_lock);
 3034         INIT_LIST_HEAD(&err_cqe_list);
 3035         spin_lock_init(&err_cqe_lock);
 3036 
 3037         INIT_WORK(&c4iw_task, process_req);
 3038 
 3039         c4iw_taskq = create_singlethread_workqueue("iw_cxgbe");
 3040         if (!c4iw_taskq)
 3041                 return -ENOMEM;
 3042 
 3043         return 0;
 3044 }
 3045 
 3046 void __exit c4iw_cm_term(void)
 3047 {
 3048         WARN_ON(!TAILQ_EMPTY(&req_list));
 3049         WARN_ON(!list_empty(&err_cqe_list));
 3050         flush_workqueue(c4iw_taskq);
 3051         destroy_workqueue(c4iw_taskq);
 3052 
 3053         t4_register_cpl_handler(CPL_RDMA_TERMINATE, NULL);
 3054         t4_register_fw_msg_handler(FW6_TYPE_WR_RPL, NULL);
 3055         t4_register_fw_msg_handler(FW6_TYPE_CQE, NULL);
 3056         t4_register_an_handler(NULL);
 3057 }
 3058 #endif

Cache object: 31cbe126f252e574c1884eecce1e4b30


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.