xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/cm.c (revision 8a1b8cf4)
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 *)(so_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 IN_ZERONET(
942 			ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
943 	else {
944 		ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
945 		return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
946 				ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
947 	}
948 }
949 
950 static inline int c4iw_loopback_addr(struct sockaddr *addr)
951 {
952 	if (addr->sa_family == AF_INET)
953 		return IN_LOOPBACK(
954 			ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr));
955 	else
956 		return IN6_IS_ADDR_LOOPBACK(
957 				&((struct sockaddr_in6 *) addr)->sin6_addr);
958 }
959 
960 static inline int c4iw_any_addr(struct sockaddr *addr)
961 {
962 	return c4iw_zero_addr(addr) || c4iw_loopback_addr(addr);
963 }
964 
965 static void
966 process_newconn(struct c4iw_listen_ep *master_lep, struct socket *new_so)
967 {
968 	struct c4iw_listen_ep *real_lep = NULL;
969 	struct c4iw_ep *new_ep = NULL;
970 	struct sockaddr_in *remote = NULL;
971 	int ret = 0;
972 
973 	MPASS(new_so != NULL);
974 
975 	if (c4iw_any_addr((struct sockaddr *)&master_lep->com.local_addr)) {
976 		/* Here we need to find the 'real_lep' that belongs to the
977 		 * incomming socket's network interface, such that the newly
978 		 * created 'ep' can be attached to the real 'lep'.
979 		 */
980 		real_lep = find_real_listen_ep(master_lep, new_so);
981 		if (real_lep == NULL) {
982 			CTR2(KTR_IW_CXGBE, "%s: Could not find the real listen "
983 					"ep for sock: %p", __func__, new_so);
984 			log(LOG_ERR,"%s: Could not find the real listen ep for "
985 					"sock: %p\n", __func__, new_so);
986 			/* FIXME: properly free the 'new_so' in failure case.
987 			 * Use of soabort() and  soclose() are not legal
988 			 * here(before soaccept()).
989 			 */
990 			return;
991 		}
992 	} else /* for Non-Wildcard address, master_lep is always the real_lep */
993 		real_lep = master_lep;
994 
995 	new_ep = alloc_ep(sizeof(*new_ep), GFP_KERNEL);
996 
997 	CTR6(KTR_IW_CXGBE, "%s: master_lep %p, real_lep: %p, new ep %p, "
998 	    "listening so %p, new so %p", __func__, master_lep, real_lep,
999 	    new_ep, master_lep->com.so, new_so);
1000 
1001 	new_ep->com.dev = real_lep->com.dev;
1002 	new_ep->com.so = new_so;
1003 	new_ep->com.cm_id = NULL;
1004 	new_ep->com.thread = real_lep->com.thread;
1005 	new_ep->parent_ep = real_lep;
1006 
1007 	GET_LOCAL_ADDR(&new_ep->com.local_addr, new_so);
1008 	GET_REMOTE_ADDR(&new_ep->com.remote_addr, new_so);
1009 	c4iw_get_ep(&real_lep->com);
1010 	init_timer(&new_ep->timer);
1011 	new_ep->com.state = MPA_REQ_WAIT;
1012 
1013 	setiwsockopt(new_so);
1014 	ret = soaccept(new_so, (struct sockaddr **)&remote);
1015 	if (ret != 0) {
1016 		CTR4(KTR_IW_CXGBE,
1017 				"%s:listen sock:%p, new sock:%p, ret:%d",
1018 				__func__, master_lep->com.so, new_so, ret);
1019 		if (remote != NULL)
1020 			free(remote, M_SONAME);
1021 		soclose(new_so);
1022 		c4iw_put_ep(&new_ep->com);
1023 		c4iw_put_ep(&real_lep->com);
1024 		return;
1025 	}
1026 	free(remote, M_SONAME);
1027 
1028 	START_EP_TIMER(new_ep);
1029 
1030 	/* MPA request might have been queued up on the socket already, so we
1031 	 * initialize the socket/upcall_handler under lock to prevent processing
1032 	 * MPA request on another thread(via process_req()) simultaneously.
1033 	 */
1034 	c4iw_get_ep(&new_ep->com); /* Dereferenced at the end below, this is to
1035 				      avoid freeing of ep before ep unlock. */
1036 	mutex_lock(&new_ep->com.mutex);
1037 	init_iwarp_socket(new_so, &new_ep->com);
1038 
1039 	ret = process_mpa_request(new_ep);
1040 	if (ret) {
1041 		/* ABORT */
1042 		c4iw_ep_disconnect(new_ep, 1, GFP_KERNEL);
1043 		c4iw_put_ep(&real_lep->com);
1044 	}
1045 	mutex_unlock(&new_ep->com.mutex);
1046 	c4iw_put_ep(&new_ep->com);
1047 	return;
1048 }
1049 
1050 static int
1051 add_ep_to_req_list(struct c4iw_ep *ep, int new_ep_event)
1052 {
1053 	unsigned long flag;
1054 
1055 	spin_lock_irqsave(&req_lock, flag);
1056 	if (ep && ep->com.so) {
1057 		ep->com.ep_events |= new_ep_event;
1058 		if (!ep->com.entry.tqe_prev) {
1059 			c4iw_get_ep(&ep->com);
1060 			TAILQ_INSERT_TAIL(&req_list, &ep->com, entry);
1061 			queue_work(c4iw_taskq, &c4iw_task);
1062 		}
1063 	}
1064 	spin_unlock_irqrestore(&req_lock, flag);
1065 
1066 	return (0);
1067 }
1068 
1069 static int
1070 c4iw_so_upcall(struct socket *so, void *arg, int waitflag)
1071 {
1072 	struct c4iw_ep *ep = arg;
1073 
1074 	CTR6(KTR_IW_CXGBE,
1075 	    "%s: so %p, so_state 0x%x, ep %p, ep_state %s, tqe_prev %p",
1076 	    __func__, so, so->so_state, ep, states[ep->com.state],
1077 	    ep->com.entry.tqe_prev);
1078 
1079 	MPASS(ep->com.so == so);
1080 	/*
1081 	 * Wake up any threads waiting in rdma_init()/rdma_fini(),
1082 	 * with locks held.
1083 	 */
1084 	if (so->so_error || (ep->com.dev->rdev.flags & T4_FATAL_ERROR))
1085 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1086 	add_ep_to_req_list(ep, C4IW_EVENT_SOCKET);
1087 
1088 	return (SU_OK);
1089 }
1090 
1091 
1092 static int
1093 terminate(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1094 {
1095 	struct adapter *sc = iq->adapter;
1096 	const struct cpl_rdma_terminate *cpl = mtod(m, const void *);
1097 	unsigned int tid = GET_TID(cpl);
1098 	struct toepcb *toep = lookup_tid(sc, tid);
1099 	struct socket *so;
1100 	struct c4iw_ep *ep;
1101 
1102 	INP_WLOCK(toep->inp);
1103 	so = inp_inpcbtosocket(toep->inp);
1104 	ep = so->so_rcv.sb_upcallarg;
1105 	INP_WUNLOCK(toep->inp);
1106 
1107 	CTR3(KTR_IW_CXGBE, "%s: so %p, ep %p", __func__, so, ep);
1108 	add_ep_to_req_list(ep, C4IW_EVENT_TERM);
1109 
1110 	return 0;
1111 }
1112 
1113 static void
1114 process_socket_event(struct c4iw_ep *ep)
1115 {
1116 	int state = ep->com.state;
1117 	struct socket *so = ep->com.so;
1118 
1119 	if (ep->com.state == DEAD) {
1120 		CTR3(KTR_IW_CXGBE, "%s: Pending socket event discarded "
1121 			"ep %p ep_state %s", __func__, ep, states[state]);
1122 		return;
1123 	}
1124 
1125 	CTR6(KTR_IW_CXGBE, "process_socket_event: so %p, so_state 0x%x, "
1126 	    "so_err %d, sb_state 0x%x, ep %p, ep_state %s", so, so->so_state,
1127 	    so->so_error, so->so_rcv.sb_state, ep, states[state]);
1128 
1129 	if (state == CONNECTING) {
1130 		process_connected(ep);
1131 		return;
1132 	}
1133 
1134 	if (state == LISTEN) {
1135 		struct c4iw_listen_ep *lep = (struct c4iw_listen_ep *)ep;
1136 		struct socket *listen_so = so, *new_so = NULL;
1137 		int error = 0;
1138 
1139 		SOLISTEN_LOCK(listen_so);
1140 		do {
1141 			error = solisten_dequeue(listen_so, &new_so,
1142 						SOCK_NONBLOCK);
1143 			if (error) {
1144 				CTR4(KTR_IW_CXGBE, "%s: lep %p listen_so %p "
1145 					"error %d", __func__, lep, listen_so,
1146 					error);
1147 				return;
1148 			}
1149 			process_newconn(lep, new_so);
1150 
1151 			/* solisten_dequeue() unlocks while return, so aquire
1152 			 * lock again for sol_qlen and also for next iteration.
1153 			 */
1154 			SOLISTEN_LOCK(listen_so);
1155 		} while (listen_so->sol_qlen);
1156 		SOLISTEN_UNLOCK(listen_so);
1157 
1158 		return;
1159 	}
1160 
1161 	/* connection error */
1162 	if (so->so_error) {
1163 		process_conn_error(ep);
1164 		return;
1165 	}
1166 
1167 	/* peer close */
1168 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && state <= CLOSING) {
1169 		process_peer_close(ep);
1170 		/*
1171 		 * check whether socket disconnect event is pending before
1172 		 * returning. Fallthrough if yes.
1173 		 */
1174 		if (!(so->so_state & SS_ISDISCONNECTED))
1175 			return;
1176 	}
1177 
1178 	/* close complete */
1179 	if (so->so_state & SS_ISDISCONNECTED) {
1180 		process_close_complete(ep);
1181 		return;
1182 	}
1183 
1184 	/* rx data */
1185 	if (sbused(&ep->com.so->so_rcv)) {
1186 		process_data(ep);
1187 		return;
1188 	}
1189 
1190 	/* Socket events for 'MPA Request Received' and 'Close Complete'
1191 	 * were already processed earlier in their previous events handlers.
1192 	 * Hence, these socket events are skipped.
1193 	 * And any other socket events must have handled above.
1194 	 */
1195 	MPASS((ep->com.state == MPA_REQ_RCVD) || (ep->com.state == MORIBUND));
1196 
1197 	if ((ep->com.state != MPA_REQ_RCVD) && (ep->com.state != MORIBUND))
1198 		log(LOG_ERR, "%s: Unprocessed socket event so %p, "
1199 		"so_state 0x%x, so_err %d, sb_state 0x%x, ep %p, ep_state %s\n",
1200 		__func__, so, so->so_state, so->so_error, so->so_rcv.sb_state,
1201 			ep, states[state]);
1202 
1203 }
1204 
1205 SYSCTL_NODE(_hw, OID_AUTO, iw_cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1206     "iw_cxgbe driver parameters");
1207 
1208 static int dack_mode = 0;
1209 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, dack_mode, CTLFLAG_RWTUN, &dack_mode, 0,
1210 		"Delayed ack mode (default = 0)");
1211 
1212 int c4iw_max_read_depth = 8;
1213 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_max_read_depth, CTLFLAG_RWTUN, &c4iw_max_read_depth, 0,
1214 		"Per-connection max ORD/IRD (default = 8)");
1215 
1216 static int enable_tcp_timestamps;
1217 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_timestamps, CTLFLAG_RWTUN, &enable_tcp_timestamps, 0,
1218 		"Enable tcp timestamps (default = 0)");
1219 
1220 static int enable_tcp_sack;
1221 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_sack, CTLFLAG_RWTUN, &enable_tcp_sack, 0,
1222 		"Enable tcp SACK (default = 0)");
1223 
1224 static int enable_tcp_window_scaling = 1;
1225 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_window_scaling, CTLFLAG_RWTUN, &enable_tcp_window_scaling, 0,
1226 		"Enable tcp window scaling (default = 1)");
1227 
1228 int c4iw_debug = 0;
1229 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_debug, CTLFLAG_RWTUN, &c4iw_debug, 0,
1230 		"Enable debug logging (default = 0)");
1231 
1232 static int peer2peer = 1;
1233 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, peer2peer, CTLFLAG_RWTUN, &peer2peer, 0,
1234 		"Support peer2peer ULPs (default = 1)");
1235 
1236 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
1237 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, p2p_type, CTLFLAG_RWTUN, &p2p_type, 0,
1238 		"RDMAP opcode to use for the RTR message: 1 = RDMA_READ 0 = RDMA_WRITE (default 1)");
1239 
1240 static int ep_timeout_secs = 60;
1241 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, ep_timeout_secs, CTLFLAG_RWTUN, &ep_timeout_secs, 0,
1242 		"CM Endpoint operation timeout in seconds (default = 60)");
1243 
1244 static int mpa_rev = 1;
1245 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
1246 		"MPA Revision, 0 supports amso1100, 1 is RFC5044 spec compliant, 2 is IETF MPA Peer Connect Draft compliant (default = 1)");
1247 
1248 static int markers_enabled;
1249 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, markers_enabled, CTLFLAG_RWTUN, &markers_enabled, 0,
1250 		"Enable MPA MARKERS (default(0) = disabled)");
1251 
1252 static int crc_enabled = 1;
1253 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, crc_enabled, CTLFLAG_RWTUN, &crc_enabled, 0,
1254 		"Enable MPA CRC (default(1) = enabled)");
1255 
1256 static int rcv_win = 256 * 1024;
1257 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, rcv_win, CTLFLAG_RWTUN, &rcv_win, 0,
1258 		"TCP receive window in bytes (default = 256KB)");
1259 
1260 static int snd_win = 128 * 1024;
1261 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, snd_win, CTLFLAG_RWTUN, &snd_win, 0,
1262 		"TCP send window in bytes (default = 128KB)");
1263 
1264 int use_dsgl = 1;
1265 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, use_dsgl, CTLFLAG_RWTUN, &use_dsgl, 0,
1266 		"Use DSGL for PBL/FastReg (default=1)");
1267 
1268 int inline_threshold = 128;
1269 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, inline_threshold, CTLFLAG_RWTUN, &inline_threshold, 0,
1270 		"inline vs dsgl threshold (default=128)");
1271 
1272 static int reuseaddr = 0;
1273 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, reuseaddr, CTLFLAG_RWTUN, &reuseaddr, 0,
1274 		"Enable SO_REUSEADDR & SO_REUSEPORT socket options on all iWARP client connections(default = 0)");
1275 
1276 static void
1277 start_ep_timer(struct c4iw_ep *ep)
1278 {
1279 
1280 	if (timer_pending(&ep->timer)) {
1281 		CTR2(KTR_IW_CXGBE, "%s: ep %p, already started", __func__, ep);
1282 		printk(KERN_ERR "%s timer already started! ep %p\n", __func__,
1283 		    ep);
1284 		return;
1285 	}
1286 	clear_bit(TIMEOUT, &ep->com.flags);
1287 	c4iw_get_ep(&ep->com);
1288 	ep->timer.expires = jiffies + ep_timeout_secs * HZ;
1289 	ep->timer.data = (unsigned long)ep;
1290 	ep->timer.function = ep_timeout;
1291 	add_timer(&ep->timer);
1292 }
1293 
1294 static int
1295 stop_ep_timer(struct c4iw_ep *ep)
1296 {
1297 
1298 	del_timer_sync(&ep->timer);
1299 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
1300 		c4iw_put_ep(&ep->com);
1301 		return 0;
1302 	}
1303 	return 1;
1304 }
1305 
1306 static void *
1307 alloc_ep(int size, gfp_t gfp)
1308 {
1309 	struct c4iw_ep_common *epc;
1310 
1311 	epc = kzalloc(size, gfp);
1312 	if (epc == NULL)
1313 		return (NULL);
1314 
1315 	kref_init(&epc->kref);
1316 	mutex_init(&epc->mutex);
1317 	c4iw_init_wr_wait(&epc->wr_wait);
1318 
1319 	return (epc);
1320 }
1321 
1322 void _c4iw_free_ep(struct kref *kref)
1323 {
1324 	struct c4iw_ep *ep;
1325 #if defined(KTR) || defined(INVARIANTS)
1326 	struct c4iw_ep_common *epc;
1327 #endif
1328 
1329 	ep = container_of(kref, struct c4iw_ep, com.kref);
1330 #if defined(KTR) || defined(INVARIANTS)
1331 	epc = &ep->com;
1332 #endif
1333 	KASSERT(!epc->entry.tqe_prev, ("%s epc %p still on req list",
1334 	    __func__, epc));
1335 	if (test_bit(QP_REFERENCED, &ep->com.flags))
1336 		deref_qp(ep);
1337 	CTR4(KTR_IW_CXGBE, "%s: ep %p, history 0x%lx, flags 0x%lx",
1338 	    __func__, ep, epc->history, epc->flags);
1339 	kfree(ep);
1340 }
1341 
1342 static void release_ep_resources(struct c4iw_ep *ep)
1343 {
1344 	CTR2(KTR_IW_CXGBE, "%s:rerB %p", __func__, ep);
1345 	set_bit(RELEASE_RESOURCES, &ep->com.flags);
1346 	c4iw_put_ep(&ep->com);
1347 	CTR2(KTR_IW_CXGBE, "%s:rerE %p", __func__, ep);
1348 }
1349 
1350 static int
1351 send_mpa_req(struct c4iw_ep *ep)
1352 {
1353 	int mpalen;
1354 	struct mpa_message *mpa;
1355 	struct mpa_v2_conn_params mpa_v2_params;
1356 	struct mbuf *m;
1357 	char mpa_rev_to_use = mpa_rev;
1358 	int err = 0;
1359 
1360 	if (ep->retry_with_mpa_v1)
1361 		mpa_rev_to_use = 1;
1362 	mpalen = sizeof(*mpa) + ep->plen;
1363 	if (mpa_rev_to_use == 2)
1364 		mpalen += sizeof(struct mpa_v2_conn_params);
1365 
1366 	mpa = malloc(mpalen, M_CXGBE, M_NOWAIT);
1367 	if (mpa == NULL) {
1368 		err = -ENOMEM;
1369 		CTR3(KTR_IW_CXGBE, "%s:smr1 ep: %p , error: %d",
1370 				__func__, ep, err);
1371 		goto err;
1372 	}
1373 
1374 	memset(mpa, 0, mpalen);
1375 	memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
1376 	mpa->flags = (crc_enabled ? MPA_CRC : 0) |
1377 		(markers_enabled ? MPA_MARKERS : 0) |
1378 		(mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
1379 	mpa->private_data_size = htons(ep->plen);
1380 	mpa->revision = mpa_rev_to_use;
1381 
1382 	if (mpa_rev_to_use == 1) {
1383 		ep->tried_with_mpa_v1 = 1;
1384 		ep->retry_with_mpa_v1 = 0;
1385 	}
1386 
1387 	if (mpa_rev_to_use == 2) {
1388 		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1389 					    sizeof(struct mpa_v2_conn_params));
1390 		mpa_v2_params.ird = htons((u16)ep->ird);
1391 		mpa_v2_params.ord = htons((u16)ep->ord);
1392 
1393 		if (peer2peer) {
1394 			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1395 
1396 			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
1397 				mpa_v2_params.ord |=
1398 				    htons(MPA_V2_RDMA_WRITE_RTR);
1399 			} else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
1400 				mpa_v2_params.ord |=
1401 					htons(MPA_V2_RDMA_READ_RTR);
1402 			}
1403 		}
1404 		memcpy(mpa->private_data, &mpa_v2_params,
1405 			sizeof(struct mpa_v2_conn_params));
1406 
1407 		if (ep->plen) {
1408 
1409 			memcpy(mpa->private_data +
1410 				sizeof(struct mpa_v2_conn_params),
1411 				ep->mpa_pkt + sizeof(*mpa), ep->plen);
1412 		}
1413 	} else {
1414 
1415 		if (ep->plen)
1416 			memcpy(mpa->private_data,
1417 					ep->mpa_pkt + sizeof(*mpa), ep->plen);
1418 		CTR2(KTR_IW_CXGBE, "%s:smr7 %p", __func__, ep);
1419 	}
1420 
1421 	m = m_getm(NULL, mpalen, M_NOWAIT, MT_DATA);
1422 	if (m == NULL) {
1423 		err = -ENOMEM;
1424 		CTR3(KTR_IW_CXGBE, "%s:smr2 ep: %p , error: %d",
1425 				__func__, ep, err);
1426 		free(mpa, M_CXGBE);
1427 		goto err;
1428 	}
1429 	m_copyback(m, 0, mpalen, (void *)mpa);
1430 	free(mpa, M_CXGBE);
1431 
1432 	err = -sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT,
1433 			ep->com.thread);
1434 	if (err) {
1435 		CTR3(KTR_IW_CXGBE, "%s:smr3 ep: %p , error: %d",
1436 				__func__, ep, err);
1437 		goto err;
1438 	}
1439 
1440 	START_EP_TIMER(ep);
1441 	ep->com.state = MPA_REQ_SENT;
1442 	ep->mpa_attr.initiator = 1;
1443 	CTR3(KTR_IW_CXGBE, "%s:smrE %p, error: %d", __func__, ep, err);
1444 	return 0;
1445 err:
1446 	connect_reply_upcall(ep, err);
1447 	CTR3(KTR_IW_CXGBE, "%s:smrE %p, error: %d", __func__, ep, err);
1448 	return err;
1449 }
1450 
1451 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
1452 {
1453 	int mpalen ;
1454 	struct mpa_message *mpa;
1455 	struct mpa_v2_conn_params mpa_v2_params;
1456 	struct mbuf *m;
1457 	int err;
1458 
1459 	CTR4(KTR_IW_CXGBE, "%s:smrejB %p %u %d", __func__, ep, ep->hwtid,
1460 	    ep->plen);
1461 
1462 	mpalen = sizeof(*mpa) + plen;
1463 
1464 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1465 
1466 		mpalen += sizeof(struct mpa_v2_conn_params);
1467 		CTR4(KTR_IW_CXGBE, "%s:smrej1 %p %u %d", __func__, ep,
1468 		    ep->mpa_attr.version, mpalen);
1469 	}
1470 
1471 	mpa = malloc(mpalen, M_CXGBE, M_NOWAIT);
1472 	if (mpa == NULL)
1473 		return (-ENOMEM);
1474 
1475 	memset(mpa, 0, mpalen);
1476 	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1477 	mpa->flags = MPA_REJECT;
1478 	mpa->revision = mpa_rev;
1479 	mpa->private_data_size = htons(plen);
1480 
1481 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1482 
1483 		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1484 		mpa->private_data_size = htons(ntohs(mpa->private_data_size) +
1485 					    sizeof(struct mpa_v2_conn_params));
1486 		mpa_v2_params.ird = htons(((u16)ep->ird) |
1487 				(peer2peer ? MPA_V2_PEER2PEER_MODEL :
1488 				 0));
1489 		mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
1490 					(p2p_type ==
1491 					 FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
1492 					 MPA_V2_RDMA_WRITE_RTR : p2p_type ==
1493 					 FW_RI_INIT_P2PTYPE_READ_REQ ?
1494 					 MPA_V2_RDMA_READ_RTR : 0) : 0));
1495 		memcpy(mpa->private_data, &mpa_v2_params,
1496 				sizeof(struct mpa_v2_conn_params));
1497 
1498 		if (ep->plen)
1499 			memcpy(mpa->private_data +
1500 				sizeof(struct mpa_v2_conn_params), pdata, plen);
1501 		CTR5(KTR_IW_CXGBE, "%s:smrej3 %p %d %d %d", __func__, ep,
1502 		    mpa_v2_params.ird, mpa_v2_params.ord, ep->plen);
1503 	} else
1504 		if (plen)
1505 			memcpy(mpa->private_data, pdata, plen);
1506 
1507 	m = m_getm(NULL, mpalen, M_NOWAIT, MT_DATA);
1508 	if (m == NULL) {
1509 		free(mpa, M_CXGBE);
1510 		return (-ENOMEM);
1511 	}
1512 	m_copyback(m, 0, mpalen, (void *)mpa);
1513 	free(mpa, M_CXGBE);
1514 
1515 	err = -sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT, ep->com.thread);
1516 	if (!err)
1517 		ep->snd_seq += mpalen;
1518 	CTR4(KTR_IW_CXGBE, "%s:smrejE %p %u %d", __func__, ep, ep->hwtid, err);
1519 	return err;
1520 }
1521 
1522 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
1523 {
1524 	int mpalen;
1525 	struct mpa_message *mpa;
1526 	struct mbuf *m;
1527 	struct mpa_v2_conn_params mpa_v2_params;
1528 	int err;
1529 
1530 	CTR2(KTR_IW_CXGBE, "%s:smrepB %p", __func__, ep);
1531 
1532 	mpalen = sizeof(*mpa) + plen;
1533 
1534 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1535 
1536 		CTR3(KTR_IW_CXGBE, "%s:smrep1 %p %d", __func__, ep,
1537 		    ep->mpa_attr.version);
1538 		mpalen += sizeof(struct mpa_v2_conn_params);
1539 	}
1540 
1541 	mpa = malloc(mpalen, M_CXGBE, M_NOWAIT);
1542 	if (mpa == NULL)
1543 		return (-ENOMEM);
1544 
1545 	memset(mpa, 0, sizeof(*mpa));
1546 	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1547 	mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
1548 		(markers_enabled ? MPA_MARKERS : 0);
1549 	mpa->revision = ep->mpa_attr.version;
1550 	mpa->private_data_size = htons(plen);
1551 
1552 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1553 
1554 		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1555 		mpa->private_data_size +=
1556 			htons(sizeof(struct mpa_v2_conn_params));
1557 		mpa_v2_params.ird = htons((u16)ep->ird);
1558 		mpa_v2_params.ord = htons((u16)ep->ord);
1559 		CTR5(KTR_IW_CXGBE, "%s:smrep3 %p %d %d %d", __func__, ep,
1560 		    ep->mpa_attr.version, mpa_v2_params.ird, mpa_v2_params.ord);
1561 
1562 		if (peer2peer && (ep->mpa_attr.p2p_type !=
1563 			FW_RI_INIT_P2PTYPE_DISABLED)) {
1564 
1565 			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1566 
1567 			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
1568 
1569 				mpa_v2_params.ord |=
1570 					htons(MPA_V2_RDMA_WRITE_RTR);
1571 				CTR5(KTR_IW_CXGBE, "%s:smrep4 %p %d %d %d",
1572 				    __func__, ep, p2p_type, mpa_v2_params.ird,
1573 				    mpa_v2_params.ord);
1574 			}
1575 			else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
1576 
1577 				mpa_v2_params.ord |=
1578 					htons(MPA_V2_RDMA_READ_RTR);
1579 				CTR5(KTR_IW_CXGBE, "%s:smrep5 %p %d %d %d",
1580 				    __func__, ep, p2p_type, mpa_v2_params.ird,
1581 				    mpa_v2_params.ord);
1582 			}
1583 		}
1584 
1585 		memcpy(mpa->private_data, &mpa_v2_params,
1586 			sizeof(struct mpa_v2_conn_params));
1587 
1588 		if (ep->plen)
1589 			memcpy(mpa->private_data +
1590 				sizeof(struct mpa_v2_conn_params), pdata, plen);
1591 	} else
1592 		if (plen)
1593 			memcpy(mpa->private_data, pdata, plen);
1594 
1595 	m = m_getm(NULL, mpalen, M_NOWAIT, MT_DATA);
1596 	if (m == NULL) {
1597 		free(mpa, M_CXGBE);
1598 		return (-ENOMEM);
1599 	}
1600 	m_copyback(m, 0, mpalen, (void *)mpa);
1601 	free(mpa, M_CXGBE);
1602 
1603 
1604 	ep->com.state = MPA_REP_SENT;
1605 	ep->snd_seq += mpalen;
1606 	err = -sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT,
1607 			ep->com.thread);
1608 	CTR3(KTR_IW_CXGBE, "%s:smrepE %p %d", __func__, ep, err);
1609 	return err;
1610 }
1611 
1612 
1613 
1614 static void close_complete_upcall(struct c4iw_ep *ep, int status)
1615 {
1616 	struct iw_cm_event event;
1617 
1618 	CTR2(KTR_IW_CXGBE, "%s:ccuB %p", __func__, ep);
1619 	memset(&event, 0, sizeof(event));
1620 	event.event = IW_CM_EVENT_CLOSE;
1621 	event.status = status;
1622 
1623 	if (ep->com.cm_id) {
1624 
1625 		CTR2(KTR_IW_CXGBE, "%s:ccu1 %1", __func__, ep);
1626 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1627 		deref_cm_id(&ep->com);
1628 		set_bit(CLOSE_UPCALL, &ep->com.history);
1629 	}
1630 	CTR2(KTR_IW_CXGBE, "%s:ccuE %p", __func__, ep);
1631 }
1632 
1633 static int
1634 send_abort(struct c4iw_ep *ep)
1635 {
1636 	struct socket *so = ep->com.so;
1637 	struct sockopt sopt;
1638 	int rc;
1639 	struct linger l;
1640 
1641 	CTR5(KTR_IW_CXGBE, "%s ep %p so %p state %s tid %d", __func__, ep, so,
1642 	    states[ep->com.state], ep->hwtid);
1643 
1644 	l.l_onoff = 1;
1645 	l.l_linger = 0;
1646 
1647 	/* linger_time of 0 forces RST to be sent */
1648 	sopt.sopt_dir = SOPT_SET;
1649 	sopt.sopt_level = SOL_SOCKET;
1650 	sopt.sopt_name = SO_LINGER;
1651 	sopt.sopt_val = (caddr_t)&l;
1652 	sopt.sopt_valsize = sizeof l;
1653 	sopt.sopt_td = NULL;
1654 	rc = -sosetopt(so, &sopt);
1655 	if (rc != 0) {
1656 		log(LOG_ERR, "%s: sosetopt(%p, linger = 0) failed with %d.\n",
1657 		    __func__, so, rc);
1658 	}
1659 
1660 	uninit_iwarp_socket(so);
1661 	soclose(so);
1662 	set_bit(ABORT_CONN, &ep->com.history);
1663 
1664 	/*
1665 	 * TBD: iw_cxgbe driver should receive ABORT reply for every ABORT
1666 	 * request it has sent. But the current TOE driver is not propagating
1667 	 * this ABORT reply event (via do_abort_rpl) to iw_cxgbe. So as a work-
1668 	 * around de-refererece 'ep' here instead of doing it in abort_rpl()
1669 	 * handler(not yet implemented) of iw_cxgbe driver.
1670 	 */
1671 	release_ep_resources(ep);
1672 	ep->com.state = DEAD;
1673 
1674 	return (0);
1675 }
1676 
1677 static void peer_close_upcall(struct c4iw_ep *ep)
1678 {
1679 	struct iw_cm_event event;
1680 
1681 	CTR2(KTR_IW_CXGBE, "%s:pcuB %p", __func__, ep);
1682 	memset(&event, 0, sizeof(event));
1683 	event.event = IW_CM_EVENT_DISCONNECT;
1684 
1685 	if (ep->com.cm_id) {
1686 
1687 		CTR2(KTR_IW_CXGBE, "%s:pcu1 %p", __func__, ep);
1688 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1689 		set_bit(DISCONN_UPCALL, &ep->com.history);
1690 	}
1691 	CTR2(KTR_IW_CXGBE, "%s:pcuE %p", __func__, ep);
1692 }
1693 
1694 static void peer_abort_upcall(struct c4iw_ep *ep)
1695 {
1696 	struct iw_cm_event event;
1697 
1698 	CTR2(KTR_IW_CXGBE, "%s:pauB %p", __func__, ep);
1699 	memset(&event, 0, sizeof(event));
1700 	event.event = IW_CM_EVENT_CLOSE;
1701 	event.status = -ECONNRESET;
1702 
1703 	if (ep->com.cm_id) {
1704 
1705 		CTR2(KTR_IW_CXGBE, "%s:pau1 %p", __func__, ep);
1706 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1707 		deref_cm_id(&ep->com);
1708 		set_bit(ABORT_UPCALL, &ep->com.history);
1709 	}
1710 	CTR2(KTR_IW_CXGBE, "%s:pauE %p", __func__, ep);
1711 }
1712 
1713 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
1714 {
1715 	struct iw_cm_event event;
1716 
1717 	CTR3(KTR_IW_CXGBE, "%s:cruB %p, status: %d", __func__, ep, status);
1718 	memset(&event, 0, sizeof(event));
1719 	event.event = IW_CM_EVENT_CONNECT_REPLY;
1720 	event.status = ((status == -ECONNABORTED) || (status == -EPIPE)) ?
1721 					-ECONNRESET : status;
1722 	event.local_addr = ep->com.local_addr;
1723 	event.remote_addr = ep->com.remote_addr;
1724 
1725 	if ((status == 0) || (status == -ECONNREFUSED)) {
1726 
1727 		if (!ep->tried_with_mpa_v1) {
1728 
1729 			CTR2(KTR_IW_CXGBE, "%s:cru1 %p", __func__, ep);
1730 			/* this means MPA_v2 is used */
1731 			event.ord = ep->ird;
1732 			event.ird = ep->ord;
1733 			event.private_data_len = ep->plen -
1734 				sizeof(struct mpa_v2_conn_params);
1735 			event.private_data = ep->mpa_pkt +
1736 				sizeof(struct mpa_message) +
1737 				sizeof(struct mpa_v2_conn_params);
1738 		} else {
1739 
1740 			CTR2(KTR_IW_CXGBE, "%s:cru2 %p", __func__, ep);
1741 			/* this means MPA_v1 is used */
1742 			event.ord = c4iw_max_read_depth;
1743 			event.ird = c4iw_max_read_depth;
1744 			event.private_data_len = ep->plen;
1745 			event.private_data = ep->mpa_pkt +
1746 				sizeof(struct mpa_message);
1747 		}
1748 	}
1749 
1750 	if (ep->com.cm_id) {
1751 
1752 		CTR2(KTR_IW_CXGBE, "%s:cru3 %p", __func__, ep);
1753 		set_bit(CONN_RPL_UPCALL, &ep->com.history);
1754 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1755 	}
1756 
1757 	if(status == -ECONNABORTED) {
1758 
1759 		CTR3(KTR_IW_CXGBE, "%s:cruE %p %d", __func__, ep, status);
1760 		return;
1761 	}
1762 
1763 	if (status < 0) {
1764 
1765 		CTR3(KTR_IW_CXGBE, "%s:cru4 %p %d", __func__, ep, status);
1766 		deref_cm_id(&ep->com);
1767 	}
1768 
1769 	CTR2(KTR_IW_CXGBE, "%s:cruE %p", __func__, ep);
1770 }
1771 
1772 static int connect_request_upcall(struct c4iw_ep *ep)
1773 {
1774 	struct iw_cm_event event;
1775 	int ret;
1776 
1777 	CTR3(KTR_IW_CXGBE, "%s: ep %p, mpa_v1 %d", __func__, ep,
1778 	    ep->tried_with_mpa_v1);
1779 
1780 	memset(&event, 0, sizeof(event));
1781 	event.event = IW_CM_EVENT_CONNECT_REQUEST;
1782 	event.local_addr = ep->com.local_addr;
1783 	event.remote_addr = ep->com.remote_addr;
1784 	event.provider_data = ep;
1785 
1786 	if (!ep->tried_with_mpa_v1) {
1787 		/* this means MPA_v2 is used */
1788 		event.ord = ep->ord;
1789 		event.ird = ep->ird;
1790 		event.private_data_len = ep->plen -
1791 			sizeof(struct mpa_v2_conn_params);
1792 		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
1793 			sizeof(struct mpa_v2_conn_params);
1794 	} else {
1795 
1796 		/* this means MPA_v1 is used. Send max supported */
1797 		event.ord = c4iw_max_read_depth;
1798 		event.ird = c4iw_max_read_depth;
1799 		event.private_data_len = ep->plen;
1800 		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
1801 	}
1802 
1803 	c4iw_get_ep(&ep->com);
1804 	ret = ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
1805 	    &event);
1806 	if(ret) {
1807 		CTR3(KTR_IW_CXGBE, "%s: ep %p, Failure while notifying event to"
1808 			" IWCM, err:%d", __func__, ep, ret);
1809 		c4iw_put_ep(&ep->com);
1810 	} else
1811 		/* Dereference parent_ep only in success case.
1812 		 * In case of failure, parent_ep is dereferenced by the caller
1813 		 * of process_mpa_request().
1814 		 */
1815 		c4iw_put_ep(&ep->parent_ep->com);
1816 
1817 	set_bit(CONNREQ_UPCALL, &ep->com.history);
1818 	return ret;
1819 }
1820 
1821 static void established_upcall(struct c4iw_ep *ep)
1822 {
1823 	struct iw_cm_event event;
1824 
1825 	CTR2(KTR_IW_CXGBE, "%s:euB %p", __func__, ep);
1826 	memset(&event, 0, sizeof(event));
1827 	event.event = IW_CM_EVENT_ESTABLISHED;
1828 	event.ird = ep->ord;
1829 	event.ord = ep->ird;
1830 
1831 	if (ep->com.cm_id) {
1832 
1833 		CTR2(KTR_IW_CXGBE, "%s:eu1 %p", __func__, ep);
1834 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1835 		set_bit(ESTAB_UPCALL, &ep->com.history);
1836 	}
1837 	CTR2(KTR_IW_CXGBE, "%s:euE %p", __func__, ep);
1838 }
1839 
1840 
1841 #define RELAXED_IRD_NEGOTIATION 1
1842 
1843 /*
1844  * process_mpa_reply - process streaming mode MPA reply
1845  *
1846  * Returns:
1847  *
1848  * 0 upon success indicating a connect request was delivered to the ULP
1849  * or the mpa request is incomplete but valid so far.
1850  *
1851  * 1 if a failure requires the caller to close the connection.
1852  *
1853  * 2 if a failure requires the caller to abort the connection.
1854  */
1855 static int process_mpa_reply(struct c4iw_ep *ep)
1856 {
1857 	struct mpa_message *mpa;
1858 	struct mpa_v2_conn_params *mpa_v2_params;
1859 	u16 plen;
1860 	u16 resp_ird, resp_ord;
1861 	u8 rtr_mismatch = 0, insuff_ird = 0;
1862 	struct c4iw_qp_attributes attrs = {0};
1863 	enum c4iw_qp_attr_mask mask;
1864 	int err;
1865 	struct mbuf *top, *m;
1866 	int flags = MSG_DONTWAIT;
1867 	struct uio uio;
1868 	int disconnect = 0;
1869 
1870 	CTR2(KTR_IW_CXGBE, "%s:pmrB %p", __func__, ep);
1871 
1872 	/*
1873 	 * Stop mpa timer.  If it expired, then
1874 	 * we ignore the MPA reply.  process_timeout()
1875 	 * will abort the connection.
1876 	 */
1877 	if (STOP_EP_TIMER(ep))
1878 		return 0;
1879 
1880 	uio.uio_resid = 1000000;
1881 	uio.uio_td = ep->com.thread;
1882 	err = soreceive(ep->com.so, NULL, &uio, &top, NULL, &flags);
1883 
1884 	if (err) {
1885 
1886 		if (err == EWOULDBLOCK) {
1887 
1888 			CTR2(KTR_IW_CXGBE, "%s:pmr1 %p", __func__, ep);
1889 			START_EP_TIMER(ep);
1890 			return 0;
1891 		}
1892 		err = -err;
1893 		CTR2(KTR_IW_CXGBE, "%s:pmr2 %p", __func__, ep);
1894 		goto err;
1895 	}
1896 
1897 	if (ep->com.so->so_rcv.sb_mb) {
1898 
1899 		CTR2(KTR_IW_CXGBE, "%s:pmr3 %p", __func__, ep);
1900 		printf("%s data after soreceive called! so %p sb_mb %p top %p\n",
1901 		       __func__, ep->com.so, ep->com.so->so_rcv.sb_mb, top);
1902 	}
1903 
1904 	m = top;
1905 
1906 	do {
1907 
1908 		CTR2(KTR_IW_CXGBE, "%s:pmr4 %p", __func__, ep);
1909 		/*
1910 		 * If we get more than the supported amount of private data
1911 		 * then we must fail this connection.
1912 		 */
1913 		if (ep->mpa_pkt_len + m->m_len > sizeof(ep->mpa_pkt)) {
1914 
1915 			CTR3(KTR_IW_CXGBE, "%s:pmr5 %p %d", __func__, ep,
1916 			    ep->mpa_pkt_len + m->m_len);
1917 			err = (-EINVAL);
1918 			goto err_stop_timer;
1919 		}
1920 
1921 		/*
1922 		 * copy the new data into our accumulation buffer.
1923 		 */
1924 		m_copydata(m, 0, m->m_len, &(ep->mpa_pkt[ep->mpa_pkt_len]));
1925 		ep->mpa_pkt_len += m->m_len;
1926 		if (!m->m_next)
1927 			m = m->m_nextpkt;
1928 		else
1929 			m = m->m_next;
1930 	} while (m);
1931 
1932 	m_freem(top);
1933 	/*
1934 	 * if we don't even have the mpa message, then bail.
1935 	 */
1936 	if (ep->mpa_pkt_len < sizeof(*mpa)) {
1937 		return 0;
1938 	}
1939 	mpa = (struct mpa_message *) ep->mpa_pkt;
1940 
1941 	/* Validate MPA header. */
1942 	if (mpa->revision > mpa_rev) {
1943 
1944 		CTR4(KTR_IW_CXGBE, "%s:pmr6 %p %d %d", __func__, ep,
1945 		    mpa->revision, mpa_rev);
1946 		printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d, "
1947 				" Received = %d\n", __func__, mpa_rev, mpa->revision);
1948 		err = -EPROTO;
1949 		goto err_stop_timer;
1950 	}
1951 
1952 	if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1953 
1954 		CTR2(KTR_IW_CXGBE, "%s:pmr7 %p", __func__, ep);
1955 		err = -EPROTO;
1956 		goto err_stop_timer;
1957 	}
1958 
1959 	plen = ntohs(mpa->private_data_size);
1960 
1961 	/*
1962 	 * Fail if there's too much private data.
1963 	 */
1964 	if (plen > MPA_MAX_PRIVATE_DATA) {
1965 
1966 		CTR2(KTR_IW_CXGBE, "%s:pmr8 %p", __func__, ep);
1967 		err = -EPROTO;
1968 		goto err_stop_timer;
1969 	}
1970 
1971 	/*
1972 	 * If plen does not account for pkt size
1973 	 */
1974 	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1975 
1976 		CTR2(KTR_IW_CXGBE, "%s:pmr9 %p", __func__, ep);
1977 		STOP_EP_TIMER(ep);
1978 		err = -EPROTO;
1979 		goto err_stop_timer;
1980 	}
1981 
1982 	ep->plen = (u8) plen;
1983 
1984 	/*
1985 	 * If we don't have all the pdata yet, then bail.
1986 	 * We'll continue process when more data arrives.
1987 	 */
1988 	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen)) {
1989 
1990 		CTR2(KTR_IW_CXGBE, "%s:pmra %p", __func__, ep);
1991 		return 0;
1992 	}
1993 
1994 	if (mpa->flags & MPA_REJECT) {
1995 
1996 		CTR2(KTR_IW_CXGBE, "%s:pmrb %p", __func__, ep);
1997 		err = -ECONNREFUSED;
1998 		goto err_stop_timer;
1999 	}
2000 
2001 	/*
2002 	 * If we get here we have accumulated the entire mpa
2003 	 * start reply message including private data. And
2004 	 * the MPA header is valid.
2005 	 */
2006 	ep->com.state = FPDU_MODE;
2007 	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
2008 	ep->mpa_attr.recv_marker_enabled = markers_enabled;
2009 	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
2010 	ep->mpa_attr.version = mpa->revision;
2011 	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
2012 
2013 	if (mpa->revision == 2) {
2014 
2015 		CTR2(KTR_IW_CXGBE, "%s:pmrc %p", __func__, ep);
2016 		ep->mpa_attr.enhanced_rdma_conn =
2017 			mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
2018 
2019 		if (ep->mpa_attr.enhanced_rdma_conn) {
2020 
2021 			CTR2(KTR_IW_CXGBE, "%s:pmrd %p", __func__, ep);
2022 			mpa_v2_params = (struct mpa_v2_conn_params *)
2023 				(ep->mpa_pkt + sizeof(*mpa));
2024 			resp_ird = ntohs(mpa_v2_params->ird) &
2025 				MPA_V2_IRD_ORD_MASK;
2026 			resp_ord = ntohs(mpa_v2_params->ord) &
2027 				MPA_V2_IRD_ORD_MASK;
2028 
2029 			/*
2030 			 * This is a double-check. Ideally, below checks are
2031 			 * not required since ird/ord stuff has been taken
2032 			 * care of in c4iw_accept_cr
2033 			 */
2034 			if (ep->ird < resp_ord) {
2035 				if (RELAXED_IRD_NEGOTIATION && resp_ord <=
2036 				   ep->com.dev->rdev.adap->params.max_ordird_qp)
2037 					ep->ird = resp_ord;
2038 				else
2039 					insuff_ird = 1;
2040 			} else if (ep->ird > resp_ord) {
2041 				ep->ird = resp_ord;
2042 			}
2043 			if (ep->ord > resp_ird) {
2044 				if (RELAXED_IRD_NEGOTIATION)
2045 					ep->ord = resp_ird;
2046 				else
2047 					insuff_ird = 1;
2048 			}
2049 			if (insuff_ird) {
2050 				err = -ENOMEM;
2051 				ep->ird = resp_ord;
2052 				ep->ord = resp_ird;
2053 			}
2054 
2055 			if (ntohs(mpa_v2_params->ird) &
2056 				MPA_V2_PEER2PEER_MODEL) {
2057 
2058 				CTR2(KTR_IW_CXGBE, "%s:pmrf %p", __func__, ep);
2059 				if (ntohs(mpa_v2_params->ord) &
2060 					MPA_V2_RDMA_WRITE_RTR) {
2061 
2062 					CTR2(KTR_IW_CXGBE, "%s:pmrg %p", __func__, ep);
2063 					ep->mpa_attr.p2p_type =
2064 						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
2065 				}
2066 				else if (ntohs(mpa_v2_params->ord) &
2067 					MPA_V2_RDMA_READ_RTR) {
2068 
2069 					CTR2(KTR_IW_CXGBE, "%s:pmrh %p", __func__, ep);
2070 					ep->mpa_attr.p2p_type =
2071 						FW_RI_INIT_P2PTYPE_READ_REQ;
2072 				}
2073 			}
2074 		}
2075 	} else {
2076 
2077 		CTR2(KTR_IW_CXGBE, "%s:pmri %p", __func__, ep);
2078 
2079 		if (mpa->revision == 1) {
2080 
2081 			CTR2(KTR_IW_CXGBE, "%s:pmrj %p", __func__, ep);
2082 
2083 			if (peer2peer) {
2084 
2085 				CTR2(KTR_IW_CXGBE, "%s:pmrk %p", __func__, ep);
2086 				ep->mpa_attr.p2p_type = p2p_type;
2087 			}
2088 		}
2089 	}
2090 
2091 	if (set_tcpinfo(ep)) {
2092 
2093 		CTR2(KTR_IW_CXGBE, "%s:pmrl %p", __func__, ep);
2094 		printf("%s set_tcpinfo error\n", __func__);
2095 		err = -ECONNRESET;
2096 		goto err;
2097 	}
2098 
2099 	CTR6(KTR_IW_CXGBE, "%s - crc_enabled = %d, recv_marker_enabled = %d, "
2100 	    "xmit_marker_enabled = %d, version = %d p2p_type = %d", __func__,
2101 	    ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
2102 	    ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
2103 	    ep->mpa_attr.p2p_type);
2104 
2105 	/*
2106 	 * If responder's RTR does not match with that of initiator, assign
2107 	 * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
2108 	 * generated when moving QP to RTS state.
2109 	 * A TERM message will be sent after QP has moved to RTS state
2110 	 */
2111 	if ((ep->mpa_attr.version == 2) && peer2peer &&
2112 		(ep->mpa_attr.p2p_type != p2p_type)) {
2113 
2114 		CTR2(KTR_IW_CXGBE, "%s:pmrm %p", __func__, ep);
2115 		ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
2116 		rtr_mismatch = 1;
2117 	}
2118 
2119 
2120 	//ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
2121 	attrs.mpa_attr = ep->mpa_attr;
2122 	attrs.max_ird = ep->ird;
2123 	attrs.max_ord = ep->ord;
2124 	attrs.llp_stream_handle = ep;
2125 	attrs.next_state = C4IW_QP_STATE_RTS;
2126 
2127 	mask = C4IW_QP_ATTR_NEXT_STATE |
2128 		C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
2129 		C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
2130 
2131 	/* bind QP and TID with INIT_WR */
2132 	err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
2133 
2134 	if (err) {
2135 
2136 		CTR2(KTR_IW_CXGBE, "%s:pmrn %p", __func__, ep);
2137 		goto err;
2138 	}
2139 
2140 	/*
2141 	 * If responder's RTR requirement did not match with what initiator
2142 	 * supports, generate TERM message
2143 	 */
2144 	if (rtr_mismatch) {
2145 
2146 		CTR2(KTR_IW_CXGBE, "%s:pmro %p", __func__, ep);
2147 		printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
2148 		attrs.layer_etype = LAYER_MPA | DDP_LLP;
2149 		attrs.ecode = MPA_NOMATCH_RTR;
2150 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
2151 		attrs.send_term = 1;
2152 		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2153 			C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2154 		err = -ENOMEM;
2155 		disconnect = 1;
2156 		goto out;
2157 	}
2158 
2159 	/*
2160 	 * Generate TERM if initiator IRD is not sufficient for responder
2161 	 * provided ORD. Currently, we do the same behaviour even when
2162 	 * responder provided IRD is also not sufficient as regards to
2163 	 * initiator ORD.
2164 	 */
2165 	if (insuff_ird) {
2166 
2167 		CTR2(KTR_IW_CXGBE, "%s:pmrp %p", __func__, ep);
2168 		printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
2169 				__func__);
2170 		attrs.layer_etype = LAYER_MPA | DDP_LLP;
2171 		attrs.ecode = MPA_INSUFF_IRD;
2172 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
2173 		attrs.send_term = 1;
2174 		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2175 			C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2176 		err = -ENOMEM;
2177 		disconnect = 1;
2178 		goto out;
2179 	}
2180 	goto out;
2181 err_stop_timer:
2182 	STOP_EP_TIMER(ep);
2183 err:
2184 	disconnect = 2;
2185 out:
2186 	connect_reply_upcall(ep, err);
2187 	CTR2(KTR_IW_CXGBE, "%s:pmrE %p", __func__, ep);
2188 	return disconnect;
2189 }
2190 
2191 /*
2192  * process_mpa_request - process streaming mode MPA request
2193  *
2194  * Returns:
2195  *
2196  * 0 upon success indicating a connect request was delivered to the ULP
2197  * or the mpa request is incomplete but valid so far.
2198  *
2199  * 1 if a failure requires the caller to close the connection.
2200  *
2201  * 2 if a failure requires the caller to abort the connection.
2202  */
2203 static int
2204 process_mpa_request(struct c4iw_ep *ep)
2205 {
2206 	struct mpa_message *mpa;
2207 	struct mpa_v2_conn_params *mpa_v2_params;
2208 	u16 plen;
2209 	int flags = MSG_DONTWAIT;
2210 	int rc;
2211 	struct iovec iov;
2212 	struct uio uio;
2213 	enum c4iw_ep_state state = ep->com.state;
2214 
2215 	CTR3(KTR_IW_CXGBE, "%s: ep %p, state %s", __func__, ep, states[state]);
2216 
2217 	if (state != MPA_REQ_WAIT)
2218 		return 0;
2219 
2220 	iov.iov_base = &ep->mpa_pkt[ep->mpa_pkt_len];
2221 	iov.iov_len = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
2222 	uio.uio_iov = &iov;
2223 	uio.uio_iovcnt = 1;
2224 	uio.uio_offset = 0;
2225 	uio.uio_resid = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
2226 	uio.uio_segflg = UIO_SYSSPACE;
2227 	uio.uio_rw = UIO_READ;
2228 	uio.uio_td = NULL; /* uio.uio_td = ep->com.thread; */
2229 
2230 	rc = soreceive(ep->com.so, NULL, &uio, NULL, NULL, &flags);
2231 	if (rc == EAGAIN)
2232 		return 0;
2233 	else if (rc)
2234 		goto err_stop_timer;
2235 
2236 	KASSERT(uio.uio_offset > 0, ("%s: sorecieve on so %p read no data",
2237 	    __func__, ep->com.so));
2238 	ep->mpa_pkt_len += uio.uio_offset;
2239 
2240 	/*
2241 	 * If we get more than the supported amount of private data then we must
2242 	 * fail this connection.  XXX: check so_rcv->sb_cc, or peek with another
2243 	 * soreceive, or increase the size of mpa_pkt by 1 and abort if the last
2244 	 * byte is filled by the soreceive above.
2245 	 */
2246 
2247 	/* Don't even have the MPA message.  Wait for more data to arrive. */
2248 	if (ep->mpa_pkt_len < sizeof(*mpa))
2249 		return 0;
2250 	mpa = (struct mpa_message *) ep->mpa_pkt;
2251 
2252 	/*
2253 	 * Validate MPA Header.
2254 	 */
2255 	if (mpa->revision > mpa_rev) {
2256 		log(LOG_ERR, "%s: MPA version mismatch. Local = %d,"
2257 		    " Received = %d\n", __func__, mpa_rev, mpa->revision);
2258 		goto err_stop_timer;
2259 	}
2260 
2261 	if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
2262 		goto err_stop_timer;
2263 
2264 	/*
2265 	 * Fail if there's too much private data.
2266 	 */
2267 	plen = ntohs(mpa->private_data_size);
2268 	if (plen > MPA_MAX_PRIVATE_DATA)
2269 		goto err_stop_timer;
2270 
2271 	/*
2272 	 * If plen does not account for pkt size
2273 	 */
2274 	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
2275 		goto err_stop_timer;
2276 
2277 	ep->plen = (u8) plen;
2278 
2279 	/*
2280 	 * If we don't have all the pdata yet, then bail.
2281 	 */
2282 	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
2283 		return 0;
2284 
2285 	/*
2286 	 * If we get here we have accumulated the entire mpa
2287 	 * start reply message including private data.
2288 	 */
2289 	ep->mpa_attr.initiator = 0;
2290 	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
2291 	ep->mpa_attr.recv_marker_enabled = markers_enabled;
2292 	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
2293 	ep->mpa_attr.version = mpa->revision;
2294 	if (mpa->revision == 1)
2295 		ep->tried_with_mpa_v1 = 1;
2296 	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
2297 
2298 	if (mpa->revision == 2) {
2299 		ep->mpa_attr.enhanced_rdma_conn =
2300 		    mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
2301 		if (ep->mpa_attr.enhanced_rdma_conn) {
2302 			mpa_v2_params = (struct mpa_v2_conn_params *)
2303 				(ep->mpa_pkt + sizeof(*mpa));
2304 			ep->ird = ntohs(mpa_v2_params->ird) &
2305 				MPA_V2_IRD_ORD_MASK;
2306 			ep->ird = min_t(u32, ep->ird,
2307 					cur_max_read_depth(ep->com.dev));
2308 			ep->ord = ntohs(mpa_v2_params->ord) &
2309 				MPA_V2_IRD_ORD_MASK;
2310 			ep->ord = min_t(u32, ep->ord,
2311 					cur_max_read_depth(ep->com.dev));
2312 			CTR3(KTR_IW_CXGBE, "%s initiator ird %u ord %u",
2313 				 __func__, ep->ird, ep->ord);
2314 			if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
2315 				if (peer2peer) {
2316 					if (ntohs(mpa_v2_params->ord) &
2317 							MPA_V2_RDMA_WRITE_RTR)
2318 						ep->mpa_attr.p2p_type =
2319 						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
2320 					else if (ntohs(mpa_v2_params->ord) &
2321 							MPA_V2_RDMA_READ_RTR)
2322 						ep->mpa_attr.p2p_type =
2323 						FW_RI_INIT_P2PTYPE_READ_REQ;
2324 				}
2325 		}
2326 	} else if (mpa->revision == 1 && peer2peer)
2327 		ep->mpa_attr.p2p_type = p2p_type;
2328 
2329 	if (set_tcpinfo(ep))
2330 		goto err_stop_timer;
2331 
2332 	CTR5(KTR_IW_CXGBE, "%s: crc_enabled = %d, recv_marker_enabled = %d, "
2333 	    "xmit_marker_enabled = %d, version = %d", __func__,
2334 	    ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
2335 	    ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
2336 
2337 	ep->com.state = MPA_REQ_RCVD;
2338 	STOP_EP_TIMER(ep);
2339 
2340 	/* drive upcall */
2341 	if (ep->parent_ep->com.state != DEAD)
2342 		if (connect_request_upcall(ep))
2343 			goto err_out;
2344 	return 0;
2345 
2346 err_stop_timer:
2347 	STOP_EP_TIMER(ep);
2348 err_out:
2349 	return 2;
2350 }
2351 
2352 /*
2353  * Upcall from the adapter indicating data has been transmitted.
2354  * For us its just the single MPA request or reply.  We can now free
2355  * the skb holding the mpa message.
2356  */
2357 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2358 {
2359 #ifdef KTR
2360 	int err;
2361 #endif
2362 	struct c4iw_ep *ep = to_ep(cm_id);
2363 	int abort = 0;
2364 
2365 	mutex_lock(&ep->com.mutex);
2366 	CTR2(KTR_IW_CXGBE, "%s:crcB %p", __func__, ep);
2367 
2368 	if ((ep->com.state == DEAD) ||
2369 			(ep->com.state != MPA_REQ_RCVD)) {
2370 
2371 		CTR2(KTR_IW_CXGBE, "%s:crc1 %p", __func__, ep);
2372 		mutex_unlock(&ep->com.mutex);
2373 		c4iw_put_ep(&ep->com);
2374 		return -ECONNRESET;
2375 	}
2376 	set_bit(ULP_REJECT, &ep->com.history);
2377 
2378 	if (mpa_rev == 0) {
2379 
2380 		CTR2(KTR_IW_CXGBE, "%s:crc2 %p", __func__, ep);
2381 		abort = 1;
2382 	}
2383 	else {
2384 
2385 		CTR2(KTR_IW_CXGBE, "%s:crc3 %p", __func__, ep);
2386 		abort = send_mpa_reject(ep, pdata, pdata_len);
2387 	}
2388 	STOP_EP_TIMER(ep);
2389 #ifdef KTR
2390 	err = c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
2391 #else
2392 	c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
2393 #endif
2394 	mutex_unlock(&ep->com.mutex);
2395 	c4iw_put_ep(&ep->com);
2396 	CTR3(KTR_IW_CXGBE, "%s:crc4 %p, err: %d", __func__, ep, err);
2397 	return 0;
2398 }
2399 
2400 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2401 {
2402 	int err;
2403 	struct c4iw_qp_attributes attrs = {0};
2404 	enum c4iw_qp_attr_mask mask;
2405 	struct c4iw_ep *ep = to_ep(cm_id);
2406 	struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
2407 	struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
2408 	int abort = 0;
2409 
2410 	mutex_lock(&ep->com.mutex);
2411 	CTR2(KTR_IW_CXGBE, "%s:cacB %p", __func__, ep);
2412 
2413 	if ((ep->com.state == DEAD) ||
2414 			(ep->com.state != MPA_REQ_RCVD)) {
2415 
2416 		CTR2(KTR_IW_CXGBE, "%s:cac1 %p", __func__, ep);
2417 		err = -ECONNRESET;
2418 		goto err_out;
2419 	}
2420 
2421 	BUG_ON(!qp);
2422 
2423 	set_bit(ULP_ACCEPT, &ep->com.history);
2424 
2425 	if ((conn_param->ord > c4iw_max_read_depth) ||
2426 		(conn_param->ird > c4iw_max_read_depth)) {
2427 
2428 		CTR2(KTR_IW_CXGBE, "%s:cac2 %p", __func__, ep);
2429 		err = -EINVAL;
2430 		goto err_abort;
2431 	}
2432 
2433 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
2434 
2435 		CTR2(KTR_IW_CXGBE, "%s:cac3 %p", __func__, ep);
2436 
2437 		if (conn_param->ord > ep->ird) {
2438 			if (RELAXED_IRD_NEGOTIATION) {
2439 				conn_param->ord = ep->ird;
2440 			} else {
2441 				ep->ird = conn_param->ird;
2442 				ep->ord = conn_param->ord;
2443 				send_mpa_reject(ep, conn_param->private_data,
2444 						conn_param->private_data_len);
2445 				err = -ENOMEM;
2446 				goto err_abort;
2447 			}
2448 		}
2449 		if (conn_param->ird < ep->ord) {
2450 			if (RELAXED_IRD_NEGOTIATION &&
2451 			    ep->ord <= h->rdev.adap->params.max_ordird_qp) {
2452 				conn_param->ird = ep->ord;
2453 			} else {
2454 				err = -ENOMEM;
2455 				goto err_abort;
2456 			}
2457 		}
2458 	}
2459 	ep->ird = conn_param->ird;
2460 	ep->ord = conn_param->ord;
2461 
2462 	if (ep->mpa_attr.version == 1) {
2463 		if (peer2peer && ep->ird == 0)
2464 			ep->ird = 1;
2465 	} else {
2466 		if (peer2peer &&
2467 		    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
2468 		    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
2469 			ep->ird = 1;
2470 	}
2471 
2472 	CTR4(KTR_IW_CXGBE, "%s %d ird %d ord %d", __func__, __LINE__,
2473 			ep->ird, ep->ord);
2474 
2475 	ep->com.cm_id = cm_id;
2476 	ref_cm_id(&ep->com);
2477 	ep->com.qp = qp;
2478 	ref_qp(ep);
2479 	//ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
2480 
2481 	/* bind QP to EP and move to RTS */
2482 	attrs.mpa_attr = ep->mpa_attr;
2483 	attrs.max_ird = ep->ird;
2484 	attrs.max_ord = ep->ord;
2485 	attrs.llp_stream_handle = ep;
2486 	attrs.next_state = C4IW_QP_STATE_RTS;
2487 
2488 	/* bind QP and TID with INIT_WR */
2489 	mask = C4IW_QP_ATTR_NEXT_STATE |
2490 		C4IW_QP_ATTR_LLP_STREAM_HANDLE |
2491 		C4IW_QP_ATTR_MPA_ATTR |
2492 		C4IW_QP_ATTR_MAX_IRD |
2493 		C4IW_QP_ATTR_MAX_ORD;
2494 
2495 	err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
2496 	if (err) {
2497 		CTR3(KTR_IW_CXGBE, "%s:caca %p, err: %d", __func__, ep, err);
2498 		goto err_defef_cm_id;
2499 	}
2500 
2501 	err = send_mpa_reply(ep, conn_param->private_data,
2502 			conn_param->private_data_len);
2503 	if (err) {
2504 		CTR3(KTR_IW_CXGBE, "%s:cacb %p, err: %d", __func__, ep, err);
2505 		goto err_defef_cm_id;
2506 	}
2507 
2508 	ep->com.state = FPDU_MODE;
2509 	established_upcall(ep);
2510 	mutex_unlock(&ep->com.mutex);
2511 	c4iw_put_ep(&ep->com);
2512 	CTR2(KTR_IW_CXGBE, "%s:cacE %p", __func__, ep);
2513 	return 0;
2514 err_defef_cm_id:
2515 	deref_cm_id(&ep->com);
2516 err_abort:
2517 	abort = 1;
2518 err_out:
2519 	if (abort)
2520 		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2521 	mutex_unlock(&ep->com.mutex);
2522 	c4iw_put_ep(&ep->com);
2523 	CTR2(KTR_IW_CXGBE, "%s:cacE err %p", __func__, ep);
2524 	return err;
2525 }
2526 
2527 static int
2528 c4iw_sock_create(struct sockaddr_storage *laddr, struct socket **so)
2529 {
2530 	int ret;
2531 	int size, on;
2532 	struct socket *sock = NULL;
2533 	struct sockopt sopt;
2534 
2535 	ret = sock_create_kern(laddr->ss_family,
2536 			SOCK_STREAM, IPPROTO_TCP, &sock);
2537 	if (ret) {
2538 		CTR2(KTR_IW_CXGBE, "%s:Failed to create TCP socket. err %d",
2539 				__func__, ret);
2540 		return ret;
2541 	}
2542 
2543 	if (reuseaddr) {
2544 		bzero(&sopt, sizeof(struct sockopt));
2545 		sopt.sopt_dir = SOPT_SET;
2546 		sopt.sopt_level = SOL_SOCKET;
2547 		sopt.sopt_name = SO_REUSEADDR;
2548 		on = 1;
2549 		sopt.sopt_val = &on;
2550 		sopt.sopt_valsize = sizeof(on);
2551 		ret = -sosetopt(sock, &sopt);
2552 		if (ret != 0) {
2553 			log(LOG_ERR, "%s: sosetopt(%p, SO_REUSEADDR) "
2554 				"failed with %d.\n", __func__, sock, ret);
2555 		}
2556 		bzero(&sopt, sizeof(struct sockopt));
2557 		sopt.sopt_dir = SOPT_SET;
2558 		sopt.sopt_level = SOL_SOCKET;
2559 		sopt.sopt_name = SO_REUSEPORT;
2560 		on = 1;
2561 		sopt.sopt_val = &on;
2562 		sopt.sopt_valsize = sizeof(on);
2563 		ret = -sosetopt(sock, &sopt);
2564 		if (ret != 0) {
2565 			log(LOG_ERR, "%s: sosetopt(%p, SO_REUSEPORT) "
2566 				"failed with %d.\n", __func__, sock, ret);
2567 		}
2568 	}
2569 
2570 	ret = -sobind(sock, (struct sockaddr *)laddr, curthread);
2571 	if (ret) {
2572 		CTR2(KTR_IW_CXGBE, "%s:Failed to bind socket. err %p",
2573 				__func__, ret);
2574 		sock_release(sock);
2575 		return ret;
2576 	}
2577 
2578 	size = laddr->ss_family == AF_INET6 ?
2579 		sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
2580 	ret = sock_getname(sock, (struct sockaddr *)laddr, &size, 0);
2581 	if (ret) {
2582 		CTR2(KTR_IW_CXGBE, "%s:sock_getname failed. err %p",
2583 				__func__, ret);
2584 		sock_release(sock);
2585 		return ret;
2586 	}
2587 
2588 	*so = sock;
2589 	return 0;
2590 }
2591 
2592 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2593 {
2594 	int err = 0;
2595 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2596 	struct c4iw_ep *ep = NULL;
2597 	struct ifnet    *nh_ifp;        /* Logical egress interface */
2598 	struct epoch_tracker et;
2599 #ifdef VIMAGE
2600 	struct rdma_cm_id *rdma_id = (struct rdma_cm_id*)cm_id->context;
2601 	struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
2602 #endif
2603 
2604 	CTR2(KTR_IW_CXGBE, "%s:ccB %p", __func__, cm_id);
2605 
2606 
2607 	if ((conn_param->ord > c4iw_max_read_depth) ||
2608 		(conn_param->ird > c4iw_max_read_depth)) {
2609 
2610 		CTR2(KTR_IW_CXGBE, "%s:cc1 %p", __func__, cm_id);
2611 		err = -EINVAL;
2612 		goto out;
2613 	}
2614 	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2615 	cm_id->provider_data = ep;
2616 
2617 	init_timer(&ep->timer);
2618 	ep->plen = conn_param->private_data_len;
2619 
2620 	if (ep->plen) {
2621 
2622 		CTR2(KTR_IW_CXGBE, "%s:cc3 %p", __func__, ep);
2623 		memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
2624 				conn_param->private_data, ep->plen);
2625 	}
2626 	ep->ird = conn_param->ird;
2627 	ep->ord = conn_param->ord;
2628 
2629 	if (peer2peer && ep->ord == 0) {
2630 
2631 		CTR2(KTR_IW_CXGBE, "%s:cc4 %p", __func__, ep);
2632 		ep->ord = 1;
2633 	}
2634 
2635 	ep->com.dev = dev;
2636 	ep->com.cm_id = cm_id;
2637 	ref_cm_id(&ep->com);
2638 	ep->com.qp = get_qhp(dev, conn_param->qpn);
2639 
2640 	if (!ep->com.qp) {
2641 
2642 		CTR2(KTR_IW_CXGBE, "%s:cc5 %p", __func__, ep);
2643 		err = -EINVAL;
2644 		goto fail;
2645 	}
2646 	ref_qp(ep);
2647 	ep->com.thread = curthread;
2648 
2649 	NET_EPOCH_ENTER(et);
2650 	CURVNET_SET(vnet);
2651 	err = get_ifnet_from_raddr(&cm_id->remote_addr, &nh_ifp);
2652 	CURVNET_RESTORE();
2653 	NET_EPOCH_EXIT(et);
2654 
2655 	if (err) {
2656 
2657 		CTR2(KTR_IW_CXGBE, "%s:cc7 %p", __func__, ep);
2658 		printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
2659 		err = EHOSTUNREACH;
2660 		return err;
2661 	}
2662 
2663 	if (!(nh_ifp->if_capenable & IFCAP_TOE) ||
2664 	    TOEDEV(nh_ifp) == NULL) {
2665 		err = -ENOPROTOOPT;
2666 		goto fail;
2667 	}
2668 	ep->com.state = CONNECTING;
2669 	ep->tos = 0;
2670 	ep->com.local_addr = cm_id->local_addr;
2671 	ep->com.remote_addr = cm_id->remote_addr;
2672 
2673 	err = c4iw_sock_create(&cm_id->local_addr, &ep->com.so);
2674 	if (err)
2675 		goto fail;
2676 
2677 	setiwsockopt(ep->com.so);
2678 	init_iwarp_socket(ep->com.so, &ep->com);
2679 	err = -soconnect(ep->com.so, (struct sockaddr *)&ep->com.remote_addr,
2680 		ep->com.thread);
2681 	if (err)
2682 		goto fail_free_so;
2683 	CTR2(KTR_IW_CXGBE, "%s:ccE, ep %p", __func__, ep);
2684 	return 0;
2685 
2686 fail_free_so:
2687 	uninit_iwarp_socket(ep->com.so);
2688 	ep->com.state = DEAD;
2689 	sock_release(ep->com.so);
2690 fail:
2691 	deref_cm_id(&ep->com);
2692 	c4iw_put_ep(&ep->com);
2693 	ep = NULL;
2694 out:
2695 	CTR2(KTR_IW_CXGBE, "%s:ccE Error %d", __func__, err);
2696 	return err;
2697 }
2698 
2699 /*
2700  * iwcm->create_listen.  Returns -errno on failure.
2701  */
2702 int
2703 c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
2704 {
2705 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2706 	struct c4iw_listen_ep *lep = NULL;
2707 	struct listen_port_info *port_info = NULL;
2708 	int rc = 0;
2709 
2710 	CTR3(KTR_IW_CXGBE, "%s: cm_id %p, backlog %s", __func__, cm_id,
2711 			backlog);
2712 	if (c4iw_fatal_error(&dev->rdev)) {
2713 		CTR2(KTR_IW_CXGBE, "%s: cm_id %p, fatal error", __func__,
2714 			       cm_id);
2715 		return -EIO;
2716 	}
2717 	lep = alloc_ep(sizeof(*lep), GFP_KERNEL);
2718 	lep->com.cm_id = cm_id;
2719 	ref_cm_id(&lep->com);
2720 	lep->com.dev = dev;
2721 	lep->backlog = backlog;
2722 	lep->com.local_addr = cm_id->local_addr;
2723 	lep->com.thread = curthread;
2724 	cm_id->provider_data = lep;
2725 	lep->com.state = LISTEN;
2726 
2727 	/* In case of INDADDR_ANY, ibcore creates cmid for each device and
2728 	 * invokes iw_cxgbe listener callbacks assuming that iw_cxgbe creates
2729 	 * HW listeners for each device seperately. But toecore expects single
2730 	 * solisten() call with INADDR_ANY address to create HW listeners on
2731 	 * all devices for a given port number. So iw_cxgbe driver calls
2732 	 * solisten() only once for INADDR_ANY(usually done at first time
2733 	 * listener callback from ibcore). And all the subsequent INADDR_ANY
2734 	 * listener callbacks from ibcore(for the same port address) do not
2735 	 * invoke solisten() as first listener callback has already created
2736 	 * listeners for all other devices(via solisten).
2737 	 */
2738 	if (c4iw_any_addr((struct sockaddr *)&lep->com.local_addr)) {
2739 		port_info = add_ep_to_listenlist(lep);
2740 		/* skip solisten() if refcnt > 1, as the listeners were
2741 		 * alredy created by 'Master lep'
2742 		 */
2743 		if (port_info->refcnt > 1) {
2744 			/* As there will be only one listener socket for a TCP
2745 			 * port, copy Master lep's socket pointer to other lep's
2746 			 * that are belonging to same TCP port.
2747 			 */
2748 			struct c4iw_listen_ep *head_lep =
2749 					container_of(port_info->lep_list.next,
2750 					struct c4iw_listen_ep, listen_ep_list);
2751 			lep->com.so =  head_lep->com.so;
2752 			goto out;
2753 		}
2754 	}
2755 	rc = c4iw_sock_create(&cm_id->local_addr, &lep->com.so);
2756 	if (rc) {
2757 		CTR2(KTR_IW_CXGBE, "%s:Failed to create socket. err %d",
2758 				__func__, rc);
2759 		goto fail;
2760 	}
2761 
2762 	rc = -solisten(lep->com.so, backlog, curthread);
2763 	if (rc) {
2764 		CTR3(KTR_IW_CXGBE, "%s:Failed to listen on sock:%p. err %d",
2765 				__func__, lep->com.so, rc);
2766 		goto fail_free_so;
2767 	}
2768 	init_iwarp_socket(lep->com.so, &lep->com);
2769 out:
2770 	return 0;
2771 
2772 fail_free_so:
2773 	sock_release(lep->com.so);
2774 fail:
2775 	if (port_info)
2776 		rem_ep_from_listenlist(lep);
2777 	deref_cm_id(&lep->com);
2778 	c4iw_put_ep(&lep->com);
2779 	return rc;
2780 }
2781 
2782 int
2783 c4iw_destroy_listen(struct iw_cm_id *cm_id)
2784 {
2785 	struct c4iw_listen_ep *lep = to_listen_ep(cm_id);
2786 
2787 	mutex_lock(&lep->com.mutex);
2788 	CTR3(KTR_IW_CXGBE, "%s: cm_id %p, state %s", __func__, cm_id,
2789 	    states[lep->com.state]);
2790 
2791 	lep->com.state = DEAD;
2792 	if (c4iw_any_addr((struct sockaddr *)&lep->com.local_addr)) {
2793 		/* if no refcount then close listen socket */
2794 		if (!rem_ep_from_listenlist(lep))
2795 			close_socket(lep->com.so);
2796 	} else
2797 		close_socket(lep->com.so);
2798 	deref_cm_id(&lep->com);
2799 	mutex_unlock(&lep->com.mutex);
2800 	c4iw_put_ep(&lep->com);
2801 	return 0;
2802 }
2803 
2804 int __c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2805 {
2806 	int ret;
2807 	mutex_lock(&ep->com.mutex);
2808 	ret = c4iw_ep_disconnect(ep, abrupt, gfp);
2809 	mutex_unlock(&ep->com.mutex);
2810 	return ret;
2811 }
2812 
2813 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2814 {
2815 	int ret = 0;
2816 	int close = 0;
2817 	struct c4iw_rdev *rdev;
2818 
2819 
2820 	CTR2(KTR_IW_CXGBE, "%s:cedB %p", __func__, ep);
2821 
2822 	rdev = &ep->com.dev->rdev;
2823 
2824 	if (c4iw_fatal_error(rdev)) {
2825 		CTR3(KTR_IW_CXGBE, "%s:ced1 fatal error %p %s", __func__, ep,
2826 					states[ep->com.state]);
2827 		if (ep->com.state != DEAD) {
2828 			send_abort(ep);
2829 			ep->com.state = DEAD;
2830 		}
2831 		close_complete_upcall(ep, -ECONNRESET);
2832 		return ECONNRESET;
2833 	}
2834 	CTR3(KTR_IW_CXGBE, "%s:ced2 %p %s", __func__, ep,
2835 	    states[ep->com.state]);
2836 
2837 	/*
2838 	 * Ref the ep here in case we have fatal errors causing the
2839 	 * ep to be released and freed.
2840 	 */
2841 	c4iw_get_ep(&ep->com);
2842 	switch (ep->com.state) {
2843 
2844 		case MPA_REQ_WAIT:
2845 		case MPA_REQ_SENT:
2846 		case MPA_REQ_RCVD:
2847 		case MPA_REP_SENT:
2848 		case FPDU_MODE:
2849 			close = 1;
2850 			if (abrupt)
2851 				ep->com.state = ABORTING;
2852 			else {
2853 				ep->com.state = CLOSING;
2854 				START_EP_TIMER(ep);
2855 			}
2856 			set_bit(CLOSE_SENT, &ep->com.flags);
2857 			break;
2858 
2859 		case CLOSING:
2860 
2861 			if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2862 
2863 				close = 1;
2864 				if (abrupt) {
2865 					STOP_EP_TIMER(ep);
2866 					ep->com.state = ABORTING;
2867 				} else
2868 					ep->com.state = MORIBUND;
2869 			}
2870 			break;
2871 
2872 		case MORIBUND:
2873 		case ABORTING:
2874 		case DEAD:
2875 			CTR3(KTR_IW_CXGBE,
2876 			    "%s ignoring disconnect ep %p state %u", __func__,
2877 			    ep, ep->com.state);
2878 			break;
2879 
2880 		default:
2881 			BUG();
2882 			break;
2883 	}
2884 
2885 
2886 	if (close) {
2887 
2888 		CTR2(KTR_IW_CXGBE, "%s:ced3 %p", __func__, ep);
2889 
2890 		if (abrupt) {
2891 
2892 			CTR2(KTR_IW_CXGBE, "%s:ced4 %p", __func__, ep);
2893 			set_bit(EP_DISC_ABORT, &ep->com.history);
2894 			close_complete_upcall(ep, -ECONNRESET);
2895 			send_abort(ep);
2896 		} else {
2897 
2898 			CTR2(KTR_IW_CXGBE, "%s:ced5 %p", __func__, ep);
2899 			set_bit(EP_DISC_CLOSE, &ep->com.history);
2900 
2901 			if (!ep->parent_ep)
2902 				ep->com.state = MORIBUND;
2903 
2904 			CURVNET_SET(ep->com.so->so_vnet);
2905 			ret = sodisconnect(ep->com.so);
2906 			CURVNET_RESTORE();
2907 			if (ret) {
2908 				CTR2(KTR_IW_CXGBE, "%s:ced6 %p", __func__, ep);
2909 				STOP_EP_TIMER(ep);
2910 				send_abort(ep);
2911 				ep->com.state = DEAD;
2912 				close_complete_upcall(ep, -ECONNRESET);
2913 				set_bit(EP_DISC_FAIL, &ep->com.history);
2914 				if (ep->com.qp) {
2915 					struct c4iw_qp_attributes attrs = {0};
2916 
2917 					attrs.next_state = C4IW_QP_STATE_ERROR;
2918 					ret = c4iw_modify_qp(
2919 							ep->com.dev, ep->com.qp,
2920 							C4IW_QP_ATTR_NEXT_STATE,
2921 							&attrs, 1);
2922 					CTR3(KTR_IW_CXGBE, "%s:ced7 %p ret %d",
2923 						__func__, ep, ret);
2924 				}
2925 			}
2926 		}
2927 	}
2928 	c4iw_put_ep(&ep->com);
2929 	CTR2(KTR_IW_CXGBE, "%s:cedE %p", __func__, ep);
2930 	return ret;
2931 }
2932 
2933 #ifdef C4IW_EP_REDIRECT
2934 int c4iw_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2935 		struct l2t_entry *l2t)
2936 {
2937 	struct c4iw_ep *ep = ctx;
2938 
2939 	if (ep->dst != old)
2940 		return 0;
2941 
2942 	PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2943 			l2t);
2944 	dst_hold(new);
2945 	cxgb4_l2t_release(ep->l2t);
2946 	ep->l2t = l2t;
2947 	dst_release(old);
2948 	ep->dst = new;
2949 	return 1;
2950 }
2951 #endif
2952 
2953 
2954 
2955 static void ep_timeout(unsigned long arg)
2956 {
2957 	struct c4iw_ep *ep = (struct c4iw_ep *)arg;
2958 
2959 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
2960 
2961 		/*
2962 		 * Only insert if it is not already on the list.
2963 		 */
2964 		if (!(ep->com.ep_events & C4IW_EVENT_TIMEOUT)) {
2965 			CTR2(KTR_IW_CXGBE, "%s:et1 %p", __func__, ep);
2966 			add_ep_to_req_list(ep, C4IW_EVENT_TIMEOUT);
2967 		}
2968 	}
2969 }
2970 
2971 static int fw6_wr_rpl(struct adapter *sc, const __be64 *rpl)
2972 {
2973 	uint64_t val = be64toh(*rpl);
2974 	int ret;
2975 	struct c4iw_wr_wait *wr_waitp;
2976 
2977 	ret = (int)((val >> 8) & 0xff);
2978 	wr_waitp = (struct c4iw_wr_wait *)rpl[1];
2979 	CTR3(KTR_IW_CXGBE, "%s wr_waitp %p ret %u", __func__, wr_waitp, ret);
2980 	if (wr_waitp)
2981 		c4iw_wake_up(wr_waitp, ret ? -ret : 0);
2982 
2983 	return (0);
2984 }
2985 
2986 static int fw6_cqe_handler(struct adapter *sc, const __be64 *rpl)
2987 {
2988 	struct cqe_list_entry *cle;
2989 	unsigned long flag;
2990 
2991 	cle = malloc(sizeof(*cle), M_CXGBE, M_NOWAIT);
2992 	cle->rhp = sc->iwarp_softc;
2993 	cle->err_cqe = *(const struct t4_cqe *)(&rpl[0]);
2994 
2995 	spin_lock_irqsave(&err_cqe_lock, flag);
2996 	list_add_tail(&cle->entry, &err_cqe_list);
2997 	queue_work(c4iw_taskq, &c4iw_task);
2998 	spin_unlock_irqrestore(&err_cqe_lock, flag);
2999 
3000 	return (0);
3001 }
3002 
3003 static int
3004 process_terminate(struct c4iw_ep *ep)
3005 {
3006 	struct c4iw_qp_attributes attrs = {0};
3007 
3008 	CTR2(KTR_IW_CXGBE, "%s:tB %p %d", __func__, ep);
3009 
3010 	if (ep && ep->com.qp) {
3011 
3012 		printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n",
3013 				ep->hwtid, ep->com.qp->wq.sq.qid);
3014 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
3015 		c4iw_modify_qp(ep->com.dev, ep->com.qp, C4IW_QP_ATTR_NEXT_STATE, &attrs,
3016 				1);
3017 	} else
3018 		printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n",
3019 								ep->hwtid);
3020 	CTR2(KTR_IW_CXGBE, "%s:tE %p %d", __func__, ep);
3021 
3022 	return 0;
3023 }
3024 
3025 int __init c4iw_cm_init(void)
3026 {
3027 
3028 	t4_register_cpl_handler(CPL_RDMA_TERMINATE, terminate);
3029 	t4_register_fw_msg_handler(FW6_TYPE_WR_RPL, fw6_wr_rpl);
3030 	t4_register_fw_msg_handler(FW6_TYPE_CQE, fw6_cqe_handler);
3031 	t4_register_an_handler(c4iw_ev_handler);
3032 
3033 	TAILQ_INIT(&req_list);
3034 	spin_lock_init(&req_lock);
3035 	INIT_LIST_HEAD(&err_cqe_list);
3036 	spin_lock_init(&err_cqe_lock);
3037 
3038 	INIT_WORK(&c4iw_task, process_req);
3039 
3040 	c4iw_taskq = create_singlethread_workqueue("iw_cxgbe");
3041 	if (!c4iw_taskq)
3042 		return -ENOMEM;
3043 
3044 	return 0;
3045 }
3046 
3047 void __exit c4iw_cm_term(void)
3048 {
3049 	WARN_ON(!TAILQ_EMPTY(&req_list));
3050 	WARN_ON(!list_empty(&err_cqe_list));
3051 	flush_workqueue(c4iw_taskq);
3052 	destroy_workqueue(c4iw_taskq);
3053 
3054 	t4_register_cpl_handler(CPL_RDMA_TERMINATE, NULL);
3055 	t4_register_fw_msg_handler(FW6_TYPE_WR_RPL, NULL);
3056 	t4_register_fw_msg_handler(FW6_TYPE_CQE, NULL);
3057 	t4_register_an_handler(NULL);
3058 }
3059 #endif
3060