xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/cm.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
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 
49 #include <netinet/in_systm.h>
50 #include <netinet/in_pcb.h>
51 #include <netinet6/in6_pcb.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_fib.h>
54 #include <netinet6/in6_fib.h>
55 #include <netinet6/scope6_var.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp_var.h>
58 #include <netinet/tcp.h>
59 #include <netinet/tcpip.h>
60 
61 #include <netinet/toecore.h>
62 
63 struct sge_iq;
64 struct rss_header;
65 struct cpl_set_tcb_rpl;
66 #include <linux/types.h>
67 #include "offload.h"
68 #include "tom/t4_tom.h"
69 
70 #define TOEPCB(so)  ((struct toepcb *)(so_sototcpcb((so))->t_toe))
71 
72 #include "iw_cxgbe.h"
73 #include <linux/module.h>
74 #include <linux/workqueue.h>
75 #include <linux/notifier.h>
76 #include <linux/inetdevice.h>
77 #include <linux/if_vlan.h>
78 #include <net/netevent.h>
79 #include <rdma/rdma_cm.h>
80 
81 static spinlock_t req_lock;
82 static TAILQ_HEAD(c4iw_ep_list, c4iw_ep_common) req_list;
83 static struct work_struct c4iw_task;
84 static struct workqueue_struct *c4iw_taskq;
85 static LIST_HEAD(err_cqe_list);
86 static spinlock_t err_cqe_lock;
87 static LIST_HEAD(listen_port_list);
88 static DEFINE_MUTEX(listen_port_mutex);
89 
90 static void process_req(struct work_struct *ctx);
91 static void start_ep_timer(struct c4iw_ep *ep);
92 static int stop_ep_timer(struct c4iw_ep *ep);
93 static int set_tcpinfo(struct c4iw_ep *ep);
94 static void process_timeout(struct c4iw_ep *ep);
95 static void process_err_cqes(void);
96 static void *alloc_ep(int size, gfp_t flags);
97 static void close_socket(struct socket *so);
98 static int send_mpa_req(struct c4iw_ep *ep);
99 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen);
100 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen);
101 static void close_complete_upcall(struct c4iw_ep *ep, int status);
102 static int send_abort(struct c4iw_ep *ep);
103 static void peer_close_upcall(struct c4iw_ep *ep);
104 static void peer_abort_upcall(struct c4iw_ep *ep);
105 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
106 static int connect_request_upcall(struct c4iw_ep *ep);
107 static void established_upcall(struct c4iw_ep *ep);
108 static int process_mpa_reply(struct c4iw_ep *ep);
109 static int process_mpa_request(struct c4iw_ep *ep);
110 static void process_peer_close(struct c4iw_ep *ep);
111 static void process_conn_error(struct c4iw_ep *ep);
112 static void process_close_complete(struct c4iw_ep *ep);
113 static void ep_timeout(unsigned long arg);
114 static void setiwsockopt(struct socket *so);
115 static void init_iwarp_socket(struct socket *so, void *arg);
116 static void uninit_iwarp_socket(struct socket *so);
117 static void process_data(struct c4iw_ep *ep);
118 static void process_connected(struct c4iw_ep *ep);
119 static int c4iw_so_upcall(struct socket *so, void *arg, int waitflag);
120 static void process_socket_event(struct c4iw_ep *ep);
121 static void release_ep_resources(struct c4iw_ep *ep);
122 static int process_terminate(struct c4iw_ep *ep);
123 static int terminate(struct sge_iq *iq, const struct rss_header *rss,
124     struct mbuf *m);
125 static int add_ep_to_req_list(struct c4iw_ep *ep, int ep_events);
126 static struct listen_port_info *
127 add_ep_to_listenlist(struct c4iw_listen_ep *lep);
128 static int rem_ep_from_listenlist(struct c4iw_listen_ep *lep);
129 static struct c4iw_listen_ep *
130 find_real_listen_ep(struct c4iw_listen_ep *master_lep, struct socket *so);
131 static int get_ifnet_from_raddr(struct sockaddr_storage *raddr,
132 		struct ifnet **ifp);
133 static void process_newconn(struct c4iw_listen_ep *master_lep,
134 		struct socket *new_so);
135 #define START_EP_TIMER(ep) \
136     do { \
137 	    CTR3(KTR_IW_CXGBE, "start_ep_timer (%s:%d) ep %p", \
138 		__func__, __LINE__, (ep)); \
139 	    start_ep_timer(ep); \
140     } while (0)
141 
142 #define STOP_EP_TIMER(ep) \
143     ({ \
144 	    CTR3(KTR_IW_CXGBE, "stop_ep_timer (%s:%d) ep %p", \
145 		__func__, __LINE__, (ep)); \
146 	    stop_ep_timer(ep); \
147     })
148 
149 #define GET_LOCAL_ADDR(pladdr, so) \
150 	do { \
151 		struct sockaddr_storage *__a = NULL; \
152 		struct  inpcb *__inp = sotoinpcb(so); \
153 		KASSERT(__inp != NULL, \
154 		   ("GET_LOCAL_ADDR(%s):so:%p, inp = NULL", __func__, so)); \
155 		if (__inp->inp_vflag & INP_IPV4) \
156 			in_getsockaddr(so, (struct sockaddr **)&__a); \
157 		else \
158 			in6_getsockaddr(so, (struct sockaddr **)&__a); \
159 		*(pladdr) = *__a; \
160 		free(__a, M_SONAME); \
161 	} while (0)
162 
163 #define GET_REMOTE_ADDR(praddr, so) \
164 	do { \
165 		struct sockaddr_storage *__a = NULL; \
166 		struct  inpcb *__inp = sotoinpcb(so); \
167 		KASSERT(__inp != NULL, \
168 		   ("GET_REMOTE_ADDR(%s):so:%p, inp = NULL", __func__, so)); \
169 		if (__inp->inp_vflag & INP_IPV4) \
170 			in_getpeeraddr(so, (struct sockaddr **)&__a); \
171 		else \
172 			in6_getpeeraddr(so, (struct sockaddr **)&__a); \
173 		*(praddr) = *__a; \
174 		free(__a, M_SONAME); \
175 	} while (0)
176 
177 static char *states[] = {
178 	"idle",
179 	"listen",
180 	"connecting",
181 	"mpa_wait_req",
182 	"mpa_req_sent",
183 	"mpa_req_rcvd",
184 	"mpa_rep_sent",
185 	"fpdu_mode",
186 	"aborting",
187 	"closing",
188 	"moribund",
189 	"dead",
190 	NULL,
191 };
192 
193 static void deref_cm_id(struct c4iw_ep_common *epc)
194 {
195       epc->cm_id->rem_ref(epc->cm_id);
196       epc->cm_id = NULL;
197       set_bit(CM_ID_DEREFED, &epc->history);
198 }
199 
200 static void ref_cm_id(struct c4iw_ep_common *epc)
201 {
202       set_bit(CM_ID_REFED, &epc->history);
203       epc->cm_id->add_ref(epc->cm_id);
204 }
205 
206 static void deref_qp(struct c4iw_ep *ep)
207 {
208 	c4iw_qp_rem_ref(&ep->com.qp->ibqp);
209 	clear_bit(QP_REFERENCED, &ep->com.flags);
210 	set_bit(QP_DEREFED, &ep->com.history);
211 }
212 
213 static void ref_qp(struct c4iw_ep *ep)
214 {
215 	set_bit(QP_REFERENCED, &ep->com.flags);
216 	set_bit(QP_REFED, &ep->com.history);
217 	c4iw_qp_add_ref(&ep->com.qp->ibqp);
218 }
219 /* allocated per TCP port while listening */
220 struct listen_port_info {
221 	uint16_t port_num; /* TCP port address */
222 	struct list_head list; /* belongs to listen_port_list */
223 	struct list_head lep_list; /* per port lep list */
224 	uint32_t refcnt; /* number of lep's listening */
225 };
226 
227 /*
228  * Following two lists are used to manage INADDR_ANY listeners:
229  * 1)listen_port_list
230  * 2)lep_list
231  *
232  * Below is the INADDR_ANY listener lists overview on a system with a two port
233  * adapter:
234  *   |------------------|
235  *   |listen_port_list  |
236  *   |------------------|
237  *            |
238  *            |              |-----------|       |-----------|
239  *            |              | port_num:X|       | port_num:X|
240  *            |--------------|-list------|-------|-list------|-------....
241  *                           | lep_list----|     | lep_list----|
242  *                           | refcnt    | |     | refcnt    | |
243  *                           |           | |     |           | |
244  *                           |           | |     |           | |
245  *                           |-----------| |     |-----------| |
246  *                                         |                   |
247  *                                         |                   |
248  *                                         |                   |
249  *                                         |                   |         lep1                  lep2
250  *                                         |                   |    |----------------|    |----------------|
251  *                                         |                   |----| listen_ep_list |----| listen_ep_list |
252  *                                         |                        |----------------|    |----------------|
253  *                                         |
254  *                                         |
255  *                                         |        lep1                  lep2
256  *                                         |   |----------------|    |----------------|
257  *                                         |---| listen_ep_list |----| listen_ep_list |
258  *                                             |----------------|    |----------------|
259  *
260  * Because of two port adapter, the number of lep's are two(lep1 & lep2) for
261  * each TCP port number.
262  *
263  * Here 'lep1' is always marked as Master lep, because solisten() is always
264  * called through first lep.
265  *
266  */
267 static struct listen_port_info *
268 add_ep_to_listenlist(struct c4iw_listen_ep *lep)
269 {
270 	uint16_t port;
271 	struct listen_port_info *port_info = NULL;
272 	struct sockaddr_storage *laddr = &lep->com.local_addr;
273 
274 	port = (laddr->ss_family == AF_INET) ?
275 		((struct sockaddr_in *)laddr)->sin_port :
276 		((struct sockaddr_in6 *)laddr)->sin6_port;
277 
278 	mutex_lock(&listen_port_mutex);
279 
280 	list_for_each_entry(port_info, &listen_port_list, list)
281 		if (port_info->port_num == port)
282 			goto found_port;
283 
284 	port_info = malloc(sizeof(*port_info), M_CXGBE, M_WAITOK);
285 	port_info->port_num = port;
286 	port_info->refcnt    = 0;
287 
288 	list_add_tail(&port_info->list, &listen_port_list);
289 	INIT_LIST_HEAD(&port_info->lep_list);
290 
291 found_port:
292 	port_info->refcnt++;
293 	list_add_tail(&lep->listen_ep_list, &port_info->lep_list);
294 	mutex_unlock(&listen_port_mutex);
295 	return port_info;
296 }
297 
298 static int
299 rem_ep_from_listenlist(struct c4iw_listen_ep *lep)
300 {
301 	uint16_t port;
302 	struct listen_port_info *port_info = NULL;
303 	struct sockaddr_storage *laddr = &lep->com.local_addr;
304 	int refcnt = 0;
305 
306 	port = (laddr->ss_family == AF_INET) ?
307 		((struct sockaddr_in *)laddr)->sin_port :
308 		((struct sockaddr_in6 *)laddr)->sin6_port;
309 
310 	mutex_lock(&listen_port_mutex);
311 
312 	/* get the port_info structure based on the lep's port address */
313 	list_for_each_entry(port_info, &listen_port_list, list) {
314 		if (port_info->port_num == port) {
315 			port_info->refcnt--;
316 			refcnt = port_info->refcnt;
317 			/* remove the current lep from the listen list */
318 			list_del(&lep->listen_ep_list);
319 			if (port_info->refcnt == 0) {
320 				/* Remove this entry from the list as there
321 				 * are no more listeners for this port_num.
322 				 */
323 				list_del(&port_info->list);
324 				kfree(port_info);
325 			}
326 			break;
327 		}
328 	}
329 	mutex_unlock(&listen_port_mutex);
330 	return refcnt;
331 }
332 
333 /*
334  * Find the lep that belongs to the ifnet on which the SYN frame was received.
335  */
336 struct c4iw_listen_ep *
337 find_real_listen_ep(struct c4iw_listen_ep *master_lep, struct socket *so)
338 {
339 	struct adapter *adap = NULL;
340 	struct c4iw_listen_ep *lep = NULL;
341 	struct ifnet *ifp = NULL, *hw_ifp = NULL;
342 	struct listen_port_info *port_info = NULL;
343 	int i = 0, found_portinfo = 0, found_lep = 0;
344 	uint16_t port;
345 
346 	/*
347 	 * STEP 1: Figure out 'ifp' of the physical interface, not pseudo
348 	 * interfaces like vlan, lagg, etc..
349 	 * TBD: lagg support, lagg + vlan support.
350 	 */
351 	ifp = TOEPCB(so)->l2te->ifp;
352 	if (ifp->if_type == IFT_L2VLAN) {
353 		hw_ifp = VLAN_TRUNKDEV(ifp);
354 		if (hw_ifp == NULL) {
355 			CTR4(KTR_IW_CXGBE, "%s: Failed to get parent ifnet of "
356 				"vlan ifnet %p, sock %p, master_lep %p",
357 				__func__, ifp, so, master_lep);
358 			return (NULL);
359 		}
360 	} else
361 		hw_ifp = ifp;
362 
363 	/* STEP 2: Find 'port_info' with listener local port address. */
364 	port = (master_lep->com.local_addr.ss_family == AF_INET) ?
365 		((struct sockaddr_in *)&master_lep->com.local_addr)->sin_port :
366 		((struct sockaddr_in6 *)&master_lep->com.local_addr)->sin6_port;
367 
368 
369 	mutex_lock(&listen_port_mutex);
370 	list_for_each_entry(port_info, &listen_port_list, list)
371 		if (port_info->port_num == port) {
372 			found_portinfo =1;
373 			break;
374 		}
375 	if (!found_portinfo)
376 		goto out;
377 
378 	/* STEP 3: Traverse through list of lep's that are bound to the current
379 	 * TCP port address and find the lep that belongs to the ifnet on which
380 	 * the SYN frame was received.
381 	 */
382 	list_for_each_entry(lep, &port_info->lep_list, listen_ep_list) {
383 		adap = lep->com.dev->rdev.adap;
384 		for_each_port(adap, i) {
385 			if (hw_ifp == adap->port[i]->vi[0].ifp) {
386 				found_lep =1;
387 				goto out;
388 			}
389 		}
390 	}
391 out:
392 	mutex_unlock(&listen_port_mutex);
393 	return found_lep ? lep : (NULL);
394 }
395 
396 static void process_timeout(struct c4iw_ep *ep)
397 {
398 	struct c4iw_qp_attributes attrs = {0};
399 	int abort = 1;
400 
401 	CTR4(KTR_IW_CXGBE, "%s ep :%p, tid:%u, state %d", __func__,
402 			ep, ep->hwtid, ep->com.state);
403 	set_bit(TIMEDOUT, &ep->com.history);
404 	switch (ep->com.state) {
405 	case MPA_REQ_SENT:
406 		connect_reply_upcall(ep, -ETIMEDOUT);
407 		break;
408 	case MPA_REQ_WAIT:
409 	case MPA_REQ_RCVD:
410 	case MPA_REP_SENT:
411 	case FPDU_MODE:
412 		break;
413 	case CLOSING:
414 	case MORIBUND:
415 		if (ep->com.cm_id && ep->com.qp) {
416 			attrs.next_state = C4IW_QP_STATE_ERROR;
417 			c4iw_modify_qp(ep->com.dev, ep->com.qp,
418 					C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
419 		}
420 		close_complete_upcall(ep, -ETIMEDOUT);
421 		break;
422 	case ABORTING:
423 	case DEAD:
424 		/*
425 		 * These states are expected if the ep timed out at the same
426 		 * time as another thread was calling stop_ep_timer().
427 		 * So we silently do nothing for these states.
428 		 */
429 		abort = 0;
430 		break;
431 	default:
432 		CTR4(KTR_IW_CXGBE, "%s unexpected state ep %p tid %u state %u"
433 				, __func__, ep, ep->hwtid, ep->com.state);
434 		abort = 0;
435 	}
436 	if (abort)
437 		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
438 	c4iw_put_ep(&ep->com);
439 	return;
440 }
441 
442 struct cqe_list_entry {
443 	struct list_head entry;
444 	struct c4iw_dev *rhp;
445 	struct t4_cqe err_cqe;
446 };
447 
448 static void
449 process_err_cqes(void)
450 {
451 	unsigned long flag;
452 	struct cqe_list_entry *cle;
453 
454 	spin_lock_irqsave(&err_cqe_lock, flag);
455 	while (!list_empty(&err_cqe_list)) {
456 		struct list_head *tmp;
457 		tmp = err_cqe_list.next;
458 		list_del(tmp);
459 		tmp->next = tmp->prev = NULL;
460 		spin_unlock_irqrestore(&err_cqe_lock, flag);
461 		cle = list_entry(tmp, struct cqe_list_entry, entry);
462 		c4iw_ev_dispatch(cle->rhp, &cle->err_cqe);
463 		free(cle, M_CXGBE);
464 		spin_lock_irqsave(&err_cqe_lock, flag);
465 	}
466 	spin_unlock_irqrestore(&err_cqe_lock, flag);
467 
468 	return;
469 }
470 
471 static void
472 process_req(struct work_struct *ctx)
473 {
474 	struct c4iw_ep_common *epc;
475 	unsigned long flag;
476 	int ep_events;
477 
478 	process_err_cqes();
479 	spin_lock_irqsave(&req_lock, flag);
480 	while (!TAILQ_EMPTY(&req_list)) {
481 		epc = TAILQ_FIRST(&req_list);
482 		TAILQ_REMOVE(&req_list, epc, entry);
483 		epc->entry.tqe_prev = NULL;
484 		ep_events = epc->ep_events;
485 		epc->ep_events = 0;
486 		spin_unlock_irqrestore(&req_lock, flag);
487 		mutex_lock(&epc->mutex);
488 		CTR5(KTR_IW_CXGBE, "%s: so %p, ep %p, ep_state %s events 0x%x",
489 		    __func__, epc->so, epc, states[epc->state], ep_events);
490 		if (ep_events & C4IW_EVENT_TERM)
491 			process_terminate((struct c4iw_ep *)epc);
492 		if (ep_events & C4IW_EVENT_TIMEOUT)
493 			process_timeout((struct c4iw_ep *)epc);
494 		if (ep_events & C4IW_EVENT_SOCKET)
495 			process_socket_event((struct c4iw_ep *)epc);
496 		mutex_unlock(&epc->mutex);
497 		c4iw_put_ep(epc);
498 		process_err_cqes();
499 		spin_lock_irqsave(&req_lock, flag);
500 	}
501 	spin_unlock_irqrestore(&req_lock, flag);
502 }
503 
504 /*
505  * XXX: doesn't belong here in the iWARP driver.
506  * XXX: assumes that the connection was offloaded by cxgbe/t4_tom if TF_TOE is
507  *      set.  Is this a valid assumption for active open?
508  */
509 static int
510 set_tcpinfo(struct c4iw_ep *ep)
511 {
512 	struct socket *so = ep->com.so;
513 	struct inpcb *inp = sotoinpcb(so);
514 	struct tcpcb *tp;
515 	struct toepcb *toep;
516 	int rc = 0;
517 
518 	INP_WLOCK(inp);
519 	tp = intotcpcb(inp);
520 	if ((tp->t_flags & TF_TOE) == 0) {
521 		rc = EINVAL;
522 		log(LOG_ERR, "%s: connection not offloaded (so %p, ep %p)\n",
523 		    __func__, so, ep);
524 		goto done;
525 	}
526 	toep = TOEPCB(so);
527 
528 	ep->hwtid = toep->tid;
529 	ep->snd_seq = tp->snd_nxt;
530 	ep->rcv_seq = tp->rcv_nxt;
531 	ep->emss = max(tp->t_maxseg, 128);
532 done:
533 	INP_WUNLOCK(inp);
534 	return (rc);
535 
536 }
537 static int
538 get_ifnet_from_raddr(struct sockaddr_storage *raddr, struct ifnet **ifp)
539 {
540 	int err = 0;
541 
542 	if (raddr->ss_family == AF_INET) {
543 		struct sockaddr_in *raddr4 = (struct sockaddr_in *)raddr;
544 		struct nhop4_extended nh4 = {0};
545 
546 		err = fib4_lookup_nh_ext(RT_DEFAULT_FIB, raddr4->sin_addr,
547 				NHR_REF, 0, &nh4);
548 		*ifp = nh4.nh_ifp;
549 		if (err)
550 			fib4_free_nh_ext(RT_DEFAULT_FIB, &nh4);
551 	} else {
552 		struct sockaddr_in6 *raddr6 = (struct sockaddr_in6 *)raddr;
553 		struct nhop6_extended nh6 = {0};
554 		struct in6_addr addr6;
555 		uint32_t scopeid;
556 
557 		memset(&addr6, 0, sizeof(addr6));
558 		in6_splitscope((struct in6_addr *)&raddr6->sin6_addr,
559 					&addr6, &scopeid);
560 		err = fib6_lookup_nh_ext(RT_DEFAULT_FIB, &addr6, scopeid,
561 				NHR_REF, 0, &nh6);
562 		*ifp = nh6.nh_ifp;
563 		if (err)
564 			fib6_free_nh_ext(RT_DEFAULT_FIB, &nh6);
565 	}
566 
567 	CTR2(KTR_IW_CXGBE, "%s: return: %d", __func__, err);
568 	return err;
569 }
570 
571 static void
572 close_socket(struct socket *so)
573 {
574 	uninit_iwarp_socket(so);
575 	soclose(so);
576 }
577 
578 static void
579 process_peer_close(struct c4iw_ep *ep)
580 {
581 	struct c4iw_qp_attributes attrs = {0};
582 	int disconnect = 1;
583 	int release = 0;
584 
585 	CTR4(KTR_IW_CXGBE, "%s:ppcB ep %p so %p state %s", __func__, ep,
586 	    ep->com.so, states[ep->com.state]);
587 
588 	switch (ep->com.state) {
589 
590 		case MPA_REQ_WAIT:
591 			CTR2(KTR_IW_CXGBE, "%s:ppc1 %p MPA_REQ_WAIT DEAD",
592 			    __func__, ep);
593 			/* Fallthrough */
594 		case MPA_REQ_SENT:
595 			CTR2(KTR_IW_CXGBE, "%s:ppc2 %p MPA_REQ_SENT DEAD",
596 			    __func__, ep);
597 			ep->com.state = DEAD;
598 			connect_reply_upcall(ep, -ECONNABORTED);
599 
600 			disconnect = 0;
601 			STOP_EP_TIMER(ep);
602 			close_socket(ep->com.so);
603 			deref_cm_id(&ep->com);
604 			release = 1;
605 			break;
606 
607 		case MPA_REQ_RCVD:
608 
609 			/*
610 			 * We're gonna mark this puppy DEAD, but keep
611 			 * the reference on it until the ULP accepts or
612 			 * rejects the CR.
613 			 */
614 			CTR2(KTR_IW_CXGBE, "%s:ppc3 %p MPA_REQ_RCVD CLOSING",
615 			    __func__, ep);
616 			ep->com.state = CLOSING;
617 			break;
618 
619 		case MPA_REP_SENT:
620 			CTR2(KTR_IW_CXGBE, "%s:ppc4 %p MPA_REP_SENT CLOSING",
621 			    __func__, ep);
622 			ep->com.state = CLOSING;
623 			break;
624 
625 		case FPDU_MODE:
626 			CTR2(KTR_IW_CXGBE, "%s:ppc5 %p FPDU_MODE CLOSING",
627 			    __func__, ep);
628 			START_EP_TIMER(ep);
629 			ep->com.state = CLOSING;
630 			attrs.next_state = C4IW_QP_STATE_CLOSING;
631 			c4iw_modify_qp(ep->com.dev, ep->com.qp,
632 					C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
633 			peer_close_upcall(ep);
634 			break;
635 
636 		case ABORTING:
637 			CTR2(KTR_IW_CXGBE, "%s:ppc6 %p ABORTING (disconn)",
638 			    __func__, ep);
639 			disconnect = 0;
640 			break;
641 
642 		case CLOSING:
643 			CTR2(KTR_IW_CXGBE, "%s:ppc7 %p CLOSING MORIBUND",
644 			    __func__, ep);
645 			ep->com.state = MORIBUND;
646 			disconnect = 0;
647 			break;
648 
649 		case MORIBUND:
650 			CTR2(KTR_IW_CXGBE, "%s:ppc8 %p MORIBUND DEAD", __func__,
651 			    ep);
652 			STOP_EP_TIMER(ep);
653 			if (ep->com.cm_id && ep->com.qp) {
654 				attrs.next_state = C4IW_QP_STATE_IDLE;
655 				c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
656 						C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
657 			}
658 			close_socket(ep->com.so);
659 			close_complete_upcall(ep, 0);
660 			ep->com.state = DEAD;
661 			release = 1;
662 			disconnect = 0;
663 			break;
664 
665 		case DEAD:
666 			CTR2(KTR_IW_CXGBE, "%s:ppc9 %p DEAD (disconn)",
667 			    __func__, ep);
668 			disconnect = 0;
669 			break;
670 
671 		default:
672 			panic("%s: ep %p state %d", __func__, ep,
673 			    ep->com.state);
674 			break;
675 	}
676 
677 
678 	if (disconnect) {
679 
680 		CTR2(KTR_IW_CXGBE, "%s:ppca %p", __func__, ep);
681 		c4iw_ep_disconnect(ep, 0, M_NOWAIT);
682 	}
683 	if (release) {
684 
685 		CTR2(KTR_IW_CXGBE, "%s:ppcb %p", __func__, ep);
686 		c4iw_put_ep(&ep->com);
687 	}
688 	CTR2(KTR_IW_CXGBE, "%s:ppcE %p", __func__, ep);
689 	return;
690 }
691 
692 static void
693 process_conn_error(struct c4iw_ep *ep)
694 {
695 	struct c4iw_qp_attributes attrs = {0};
696 	int ret;
697 	int state;
698 
699 	state = ep->com.state;
700 	CTR5(KTR_IW_CXGBE, "%s:pceB ep %p so %p so->so_error %u state %s",
701 	    __func__, ep, ep->com.so, ep->com.so->so_error,
702 	    states[ep->com.state]);
703 
704 	switch (state) {
705 
706 		case MPA_REQ_WAIT:
707 			STOP_EP_TIMER(ep);
708 			c4iw_put_ep(&ep->parent_ep->com);
709 			break;
710 
711 		case MPA_REQ_SENT:
712 			STOP_EP_TIMER(ep);
713 			connect_reply_upcall(ep, -ECONNRESET);
714 			break;
715 
716 		case MPA_REP_SENT:
717 			ep->com.rpl_err = ECONNRESET;
718 			CTR1(KTR_IW_CXGBE, "waking up ep %p", ep);
719 			break;
720 
721 		case MPA_REQ_RCVD:
722 			break;
723 
724 		case MORIBUND:
725 		case CLOSING:
726 			STOP_EP_TIMER(ep);
727 			/*FALLTHROUGH*/
728 		case FPDU_MODE:
729 
730 			if (ep->com.cm_id && ep->com.qp) {
731 
732 				attrs.next_state = C4IW_QP_STATE_ERROR;
733 				ret = c4iw_modify_qp(ep->com.qp->rhp,
734 					ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
735 					&attrs, 1);
736 				if (ret)
737 					log(LOG_ERR,
738 							"%s - qp <- error failed!\n",
739 							__func__);
740 			}
741 			peer_abort_upcall(ep);
742 			break;
743 
744 		case ABORTING:
745 			break;
746 
747 		case DEAD:
748 			CTR2(KTR_IW_CXGBE, "%s so_error %d IN DEAD STATE!!!!",
749 			    __func__, ep->com.so->so_error);
750 			return;
751 
752 		default:
753 			panic("%s: ep %p state %d", __func__, ep, state);
754 			break;
755 	}
756 
757 	if (state != ABORTING) {
758 		close_socket(ep->com.so);
759 		ep->com.state = DEAD;
760 		c4iw_put_ep(&ep->com);
761 	}
762 	CTR2(KTR_IW_CXGBE, "%s:pceE %p", __func__, ep);
763 	return;
764 }
765 
766 static void
767 process_close_complete(struct c4iw_ep *ep)
768 {
769 	struct c4iw_qp_attributes attrs = {0};
770 	int release = 0;
771 
772 	CTR4(KTR_IW_CXGBE, "%s:pccB ep %p so %p state %s", __func__, ep,
773 	    ep->com.so, states[ep->com.state]);
774 
775 	/* The cm_id may be null if we failed to connect */
776 	set_bit(CLOSE_CON_RPL, &ep->com.history);
777 
778 	switch (ep->com.state) {
779 
780 		case CLOSING:
781 			CTR2(KTR_IW_CXGBE, "%s:pcc1 %p CLOSING MORIBUND",
782 			    __func__, ep);
783 			ep->com.state = MORIBUND;
784 			break;
785 
786 		case MORIBUND:
787 			CTR2(KTR_IW_CXGBE, "%s:pcc1 %p MORIBUND DEAD", __func__,
788 			    ep);
789 			STOP_EP_TIMER(ep);
790 
791 			if ((ep->com.cm_id) && (ep->com.qp)) {
792 
793 				CTR2(KTR_IW_CXGBE, "%s:pcc2 %p QP_STATE_IDLE",
794 				    __func__, ep);
795 				attrs.next_state = C4IW_QP_STATE_IDLE;
796 				c4iw_modify_qp(ep->com.dev,
797 						ep->com.qp,
798 						C4IW_QP_ATTR_NEXT_STATE,
799 						&attrs, 1);
800 			}
801 
802 			close_socket(ep->com.so);
803 			close_complete_upcall(ep, 0);
804 			ep->com.state = DEAD;
805 			release = 1;
806 			break;
807 
808 		case ABORTING:
809 			CTR2(KTR_IW_CXGBE, "%s:pcc5 %p ABORTING", __func__, ep);
810 			break;
811 
812 		case DEAD:
813 			CTR2(KTR_IW_CXGBE, "%s:pcc6 %p DEAD", __func__, ep);
814 			break;
815 		default:
816 			CTR2(KTR_IW_CXGBE, "%s:pcc7 %p unknown ep state",
817 					__func__, ep);
818 			panic("%s:pcc6 %p unknown ep state", __func__, ep);
819 			break;
820 	}
821 
822 	if (release) {
823 
824 		CTR2(KTR_IW_CXGBE, "%s:pcc8 %p", __func__, ep);
825 		release_ep_resources(ep);
826 	}
827 	CTR2(KTR_IW_CXGBE, "%s:pccE %p", __func__, ep);
828 	return;
829 }
830 
831 static void
832 setiwsockopt(struct socket *so)
833 {
834 	int rc;
835 	struct sockopt sopt;
836 	int on = 1;
837 
838 	sopt.sopt_dir = SOPT_SET;
839 	sopt.sopt_level = IPPROTO_TCP;
840 	sopt.sopt_name = TCP_NODELAY;
841 	sopt.sopt_val = (caddr_t)&on;
842 	sopt.sopt_valsize = sizeof on;
843 	sopt.sopt_td = NULL;
844 	rc = -sosetopt(so, &sopt);
845 	if (rc) {
846 		log(LOG_ERR, "%s: can't set TCP_NODELAY on so %p (%d)\n",
847 		    __func__, so, rc);
848 	}
849 }
850 
851 static void
852 init_iwarp_socket(struct socket *so, void *arg)
853 {
854 	if (SOLISTENING(so)) {
855 		SOLISTEN_LOCK(so);
856 		solisten_upcall_set(so, c4iw_so_upcall, arg);
857 		so->so_state |= SS_NBIO;
858 		SOLISTEN_UNLOCK(so);
859 	} else {
860 		SOCKBUF_LOCK(&so->so_rcv);
861 		soupcall_set(so, SO_RCV, c4iw_so_upcall, arg);
862 		so->so_state |= SS_NBIO;
863 		SOCKBUF_UNLOCK(&so->so_rcv);
864 	}
865 }
866 
867 static void
868 uninit_iwarp_socket(struct socket *so)
869 {
870 	if (SOLISTENING(so)) {
871 		SOLISTEN_LOCK(so);
872 		solisten_upcall_set(so, NULL, NULL);
873 		SOLISTEN_UNLOCK(so);
874 	} else {
875 		SOCKBUF_LOCK(&so->so_rcv);
876 		soupcall_clear(so, SO_RCV);
877 		SOCKBUF_UNLOCK(&so->so_rcv);
878 	}
879 }
880 
881 static void
882 process_data(struct c4iw_ep *ep)
883 {
884 	int ret = 0;
885 	int disconnect = 0;
886 	struct c4iw_qp_attributes attrs = {0};
887 
888 	CTR5(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s, sbused %d", __func__,
889 	    ep->com.so, ep, states[ep->com.state], sbused(&ep->com.so->so_rcv));
890 
891 	switch (ep->com.state) {
892 	case MPA_REQ_SENT:
893 		disconnect = process_mpa_reply(ep);
894 		break;
895 	case MPA_REQ_WAIT:
896 		disconnect = process_mpa_request(ep);
897 		if (disconnect)
898 			/* Refered in process_newconn() */
899 			c4iw_put_ep(&ep->parent_ep->com);
900 		break;
901 	case FPDU_MODE:
902 		MPASS(ep->com.qp != NULL);
903 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
904 		ret = c4iw_modify_qp(ep->com.dev, ep->com.qp,
905 					C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
906 		if (ret != -EINPROGRESS)
907 			disconnect = 1;
908 		break;
909 	default:
910 		log(LOG_ERR, "%s: Unexpected streaming data. ep %p, "
911 			    "state %d, so %p, so_state 0x%x, sbused %u\n",
912 			    __func__, ep, ep->com.state, ep->com.so,
913 			    ep->com.so->so_state, sbused(&ep->com.so->so_rcv));
914 		break;
915 	}
916 	if (disconnect)
917 		c4iw_ep_disconnect(ep, disconnect == 2, GFP_KERNEL);
918 
919 }
920 
921 static void
922 process_connected(struct c4iw_ep *ep)
923 {
924 	struct socket *so = ep->com.so;
925 
926 	if ((so->so_state & SS_ISCONNECTED) && !so->so_error) {
927 		if (send_mpa_req(ep))
928 			goto err;
929 	} else {
930 		connect_reply_upcall(ep, -so->so_error);
931 		goto err;
932 	}
933 	return;
934 err:
935 	close_socket(so);
936 	ep->com.state = DEAD;
937 	c4iw_put_ep(&ep->com);
938 	return;
939 }
940 
941 static inline int c4iw_zero_addr(struct sockaddr *addr)
942 {
943 	struct in6_addr *ip6;
944 
945 	if (addr->sa_family == AF_INET)
946 		return IN_ZERONET(
947 			ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
948 	else {
949 		ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
950 		return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
951 				ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
952 	}
953 }
954 
955 static inline int c4iw_loopback_addr(struct sockaddr *addr)
956 {
957 	if (addr->sa_family == AF_INET)
958 		return IN_LOOPBACK(
959 			ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr));
960 	else
961 		return IN6_IS_ADDR_LOOPBACK(
962 				&((struct sockaddr_in6 *) addr)->sin6_addr);
963 }
964 
965 static inline int c4iw_any_addr(struct sockaddr *addr)
966 {
967 	return c4iw_zero_addr(addr) || c4iw_loopback_addr(addr);
968 }
969 
970 static void
971 process_newconn(struct c4iw_listen_ep *master_lep, struct socket *new_so)
972 {
973 	struct c4iw_listen_ep *real_lep = NULL;
974 	struct c4iw_ep *new_ep = NULL;
975 	struct sockaddr_in *remote = NULL;
976 	int ret = 0;
977 
978 	MPASS(new_so != NULL);
979 
980 	if (c4iw_any_addr((struct sockaddr *)&master_lep->com.local_addr)) {
981 		/* Here we need to find the 'real_lep' that belongs to the
982 		 * incomming socket's network interface, such that the newly
983 		 * created 'ep' can be attached to the real 'lep'.
984 		 */
985 		real_lep = find_real_listen_ep(master_lep, new_so);
986 		if (real_lep == NULL) {
987 			CTR2(KTR_IW_CXGBE, "%s: Could not find the real listen "
988 					"ep for sock: %p", __func__, new_so);
989 			log(LOG_ERR,"%s: Could not find the real listen ep for "
990 					"sock: %p\n", __func__, new_so);
991 			/* FIXME: properly free the 'new_so' in failure case.
992 			 * Use of soabort() and  soclose() are not legal
993 			 * here(before soaccept()).
994 			 */
995 			return;
996 		}
997 	} else /* for Non-Wildcard address, master_lep is always the real_lep */
998 		real_lep = master_lep;
999 
1000 	new_ep = alloc_ep(sizeof(*new_ep), GFP_KERNEL);
1001 
1002 	CTR6(KTR_IW_CXGBE, "%s: master_lep %p, real_lep: %p, new ep %p, "
1003 	    "listening so %p, new so %p", __func__, master_lep, real_lep,
1004 	    new_ep, master_lep->com.so, new_so);
1005 
1006 	new_ep->com.dev = real_lep->com.dev;
1007 	new_ep->com.so = new_so;
1008 	new_ep->com.cm_id = NULL;
1009 	new_ep->com.thread = real_lep->com.thread;
1010 	new_ep->parent_ep = real_lep;
1011 
1012 	GET_LOCAL_ADDR(&new_ep->com.local_addr, new_so);
1013 	GET_REMOTE_ADDR(&new_ep->com.remote_addr, new_so);
1014 	c4iw_get_ep(&real_lep->com);
1015 	init_timer(&new_ep->timer);
1016 	new_ep->com.state = MPA_REQ_WAIT;
1017 	START_EP_TIMER(new_ep);
1018 
1019 	setiwsockopt(new_so);
1020 	ret = soaccept(new_so, (struct sockaddr **)&remote);
1021 	if (ret != 0) {
1022 		CTR4(KTR_IW_CXGBE,
1023 				"%s:listen sock:%p, new sock:%p, ret:%d",
1024 				__func__, master_lep->com.so, new_so, ret);
1025 		if (remote != NULL)
1026 			free(remote, M_SONAME);
1027 		uninit_iwarp_socket(new_so);
1028 		soclose(new_so);
1029 		c4iw_put_ep(&new_ep->com);
1030 		c4iw_put_ep(&real_lep->com);
1031 		return;
1032 	}
1033 	free(remote, M_SONAME);
1034 
1035 	/* MPA request might have been queued up on the socket already, so we
1036 	 * initialize the socket/upcall_handler under lock to prevent processing
1037 	 * MPA request on another thread(via process_req()) simultaniously.
1038 	 */
1039 	c4iw_get_ep(&new_ep->com); /* Dereferenced at the end below, this is to
1040 				      avoid freeing of ep before ep unlock. */
1041 	mutex_lock(&new_ep->com.mutex);
1042 	init_iwarp_socket(new_so, &new_ep->com);
1043 
1044 	ret = process_mpa_request(new_ep);
1045 	if (ret) {
1046 		/* ABORT */
1047 		c4iw_ep_disconnect(new_ep, 1, GFP_KERNEL);
1048 		c4iw_put_ep(&real_lep->com);
1049 	}
1050 	mutex_unlock(&new_ep->com.mutex);
1051 	c4iw_put_ep(&new_ep->com);
1052 	return;
1053 }
1054 
1055 static int
1056 add_ep_to_req_list(struct c4iw_ep *ep, int new_ep_event)
1057 {
1058 	unsigned long flag;
1059 
1060 	spin_lock_irqsave(&req_lock, flag);
1061 	if (ep && ep->com.so) {
1062 		ep->com.ep_events |= new_ep_event;
1063 		if (!ep->com.entry.tqe_prev) {
1064 			c4iw_get_ep(&ep->com);
1065 			TAILQ_INSERT_TAIL(&req_list, &ep->com, entry);
1066 			queue_work(c4iw_taskq, &c4iw_task);
1067 		}
1068 	}
1069 	spin_unlock_irqrestore(&req_lock, flag);
1070 
1071 	return (0);
1072 }
1073 
1074 static int
1075 c4iw_so_upcall(struct socket *so, void *arg, int waitflag)
1076 {
1077 	struct c4iw_ep *ep = arg;
1078 
1079 	CTR6(KTR_IW_CXGBE,
1080 	    "%s: so %p, so_state 0x%x, ep %p, ep_state %s, tqe_prev %p",
1081 	    __func__, so, so->so_state, ep, states[ep->com.state],
1082 	    ep->com.entry.tqe_prev);
1083 
1084 	MPASS(ep->com.so == so);
1085 	/*
1086 	 * Wake up any threads waiting in rdma_init()/rdma_fini(),
1087 	 * with locks held.
1088 	 */
1089 	if (so->so_error)
1090 		c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1091 	add_ep_to_req_list(ep, C4IW_EVENT_SOCKET);
1092 
1093 	return (SU_OK);
1094 }
1095 
1096 
1097 static int
1098 terminate(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1099 {
1100 	struct adapter *sc = iq->adapter;
1101 	const struct cpl_rdma_terminate *cpl = mtod(m, const void *);
1102 	unsigned int tid = GET_TID(cpl);
1103 	struct toepcb *toep = lookup_tid(sc, tid);
1104 	struct socket *so;
1105 	struct c4iw_ep *ep;
1106 
1107 	INP_WLOCK(toep->inp);
1108 	so = inp_inpcbtosocket(toep->inp);
1109 	ep = so->so_rcv.sb_upcallarg;
1110 	INP_WUNLOCK(toep->inp);
1111 
1112 	CTR3(KTR_IW_CXGBE, "%s: so %p, ep %p", __func__, so, ep);
1113 	add_ep_to_req_list(ep, C4IW_EVENT_TERM);
1114 
1115 	return 0;
1116 }
1117 
1118 static void
1119 process_socket_event(struct c4iw_ep *ep)
1120 {
1121 	int state = ep->com.state;
1122 	struct socket *so = ep->com.so;
1123 
1124 	if (ep->com.state == DEAD) {
1125 		CTR3(KTR_IW_CXGBE, "%s: Pending socket event discarded "
1126 			"ep %p ep_state %s", __func__, ep, states[state]);
1127 		return;
1128 	}
1129 
1130 	CTR6(KTR_IW_CXGBE, "process_socket_event: so %p, so_state 0x%x, "
1131 	    "so_err %d, sb_state 0x%x, ep %p, ep_state %s", so, so->so_state,
1132 	    so->so_error, so->so_rcv.sb_state, ep, states[state]);
1133 
1134 	if (state == CONNECTING) {
1135 		process_connected(ep);
1136 		return;
1137 	}
1138 
1139 	if (state == LISTEN) {
1140 		struct c4iw_listen_ep *lep = (struct c4iw_listen_ep *)ep;
1141 		struct socket *listen_so = so, *new_so = NULL;
1142 		int error = 0;
1143 
1144 		SOLISTEN_LOCK(listen_so);
1145 		do {
1146 			error = solisten_dequeue(listen_so, &new_so,
1147 						SOCK_NONBLOCK);
1148 			if (error) {
1149 				CTR4(KTR_IW_CXGBE, "%s: lep %p listen_so %p "
1150 					"error %d", __func__, lep, listen_so,
1151 					error);
1152 				return;
1153 			}
1154 			process_newconn(lep, new_so);
1155 
1156 			/* solisten_dequeue() unlocks while return, so aquire
1157 			 * lock again for sol_qlen and also for next iteration.
1158 			 */
1159 			SOLISTEN_LOCK(listen_so);
1160 		} while (listen_so->sol_qlen);
1161 		SOLISTEN_UNLOCK(listen_so);
1162 
1163 		return;
1164 	}
1165 
1166 	/* connection error */
1167 	if (so->so_error) {
1168 		process_conn_error(ep);
1169 		return;
1170 	}
1171 
1172 	/* peer close */
1173 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && state <= CLOSING) {
1174 		process_peer_close(ep);
1175 		/*
1176 		 * check whether socket disconnect event is pending before
1177 		 * returning. Fallthrough if yes.
1178 		 */
1179 		if (!(so->so_state & SS_ISDISCONNECTED))
1180 			return;
1181 	}
1182 
1183 	/* close complete */
1184 	if (so->so_state & SS_ISDISCONNECTED) {
1185 		process_close_complete(ep);
1186 		return;
1187 	}
1188 
1189 	/* rx data */
1190 	if (sbused(&ep->com.so->so_rcv)) {
1191 		process_data(ep);
1192 		return;
1193 	}
1194 
1195 	/* Socket events for 'MPA Request Received' and 'Close Complete'
1196 	 * were already processed earlier in their previous events handlers.
1197 	 * Hence, these socket events are skipped.
1198 	 * And any other socket events must have handled above.
1199 	 */
1200 	MPASS((ep->com.state == MPA_REQ_RCVD) || (ep->com.state == MORIBUND));
1201 
1202 	if ((ep->com.state != MPA_REQ_RCVD) && (ep->com.state != MORIBUND))
1203 		log(LOG_ERR, "%s: Unprocessed socket event so %p, "
1204 		"so_state 0x%x, so_err %d, sb_state 0x%x, ep %p, ep_state %s\n",
1205 		__func__, so, so->so_state, so->so_error, so->so_rcv.sb_state,
1206 			ep, states[state]);
1207 
1208 }
1209 
1210 SYSCTL_NODE(_hw, OID_AUTO, iw_cxgbe, CTLFLAG_RD, 0, "iw_cxgbe driver parameters");
1211 
1212 static int dack_mode = 0;
1213 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, dack_mode, CTLFLAG_RWTUN, &dack_mode, 0,
1214 		"Delayed ack mode (default = 0)");
1215 
1216 int c4iw_max_read_depth = 8;
1217 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_max_read_depth, CTLFLAG_RWTUN, &c4iw_max_read_depth, 0,
1218 		"Per-connection max ORD/IRD (default = 8)");
1219 
1220 static int enable_tcp_timestamps;
1221 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_timestamps, CTLFLAG_RWTUN, &enable_tcp_timestamps, 0,
1222 		"Enable tcp timestamps (default = 0)");
1223 
1224 static int enable_tcp_sack;
1225 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_sack, CTLFLAG_RWTUN, &enable_tcp_sack, 0,
1226 		"Enable tcp SACK (default = 0)");
1227 
1228 static int enable_tcp_window_scaling = 1;
1229 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_window_scaling, CTLFLAG_RWTUN, &enable_tcp_window_scaling, 0,
1230 		"Enable tcp window scaling (default = 1)");
1231 
1232 int c4iw_debug = 0;
1233 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_debug, CTLFLAG_RWTUN, &c4iw_debug, 0,
1234 		"Enable debug logging (default = 0)");
1235 
1236 static int peer2peer = 1;
1237 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, peer2peer, CTLFLAG_RWTUN, &peer2peer, 0,
1238 		"Support peer2peer ULPs (default = 1)");
1239 
1240 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
1241 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, p2p_type, CTLFLAG_RWTUN, &p2p_type, 0,
1242 		"RDMAP opcode to use for the RTR message: 1 = RDMA_READ 0 = RDMA_WRITE (default 1)");
1243 
1244 static int ep_timeout_secs = 60;
1245 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, ep_timeout_secs, CTLFLAG_RWTUN, &ep_timeout_secs, 0,
1246 		"CM Endpoint operation timeout in seconds (default = 60)");
1247 
1248 static int mpa_rev = 1;
1249 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
1250 		"MPA Revision, 0 supports amso1100, 1 is RFC5044 spec compliant, 2 is IETF MPA Peer Connect Draft compliant (default = 1)");
1251 
1252 static int markers_enabled;
1253 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, markers_enabled, CTLFLAG_RWTUN, &markers_enabled, 0,
1254 		"Enable MPA MARKERS (default(0) = disabled)");
1255 
1256 static int crc_enabled = 1;
1257 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, crc_enabled, CTLFLAG_RWTUN, &crc_enabled, 0,
1258 		"Enable MPA CRC (default(1) = enabled)");
1259 
1260 static int rcv_win = 256 * 1024;
1261 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, rcv_win, CTLFLAG_RWTUN, &rcv_win, 0,
1262 		"TCP receive window in bytes (default = 256KB)");
1263 
1264 static int snd_win = 128 * 1024;
1265 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, snd_win, CTLFLAG_RWTUN, &snd_win, 0,
1266 		"TCP send window in bytes (default = 128KB)");
1267 
1268 int use_dsgl = 1;
1269 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, use_dsgl, CTLFLAG_RWTUN, &use_dsgl, 0,
1270 		"Use DSGL for PBL/FastReg (default=1)");
1271 
1272 int inline_threshold = 128;
1273 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, inline_threshold, CTLFLAG_RWTUN, &inline_threshold, 0,
1274 		"inline vs dsgl threshold (default=128)");
1275 
1276 static int reuseaddr = 0;
1277 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, reuseaddr, CTLFLAG_RWTUN, &reuseaddr, 0,
1278 		"Enable SO_REUSEADDR & SO_REUSEPORT socket options on all iWARP client connections(default = 0)");
1279 
1280 static void
1281 start_ep_timer(struct c4iw_ep *ep)
1282 {
1283 
1284 	if (timer_pending(&ep->timer)) {
1285 		CTR2(KTR_IW_CXGBE, "%s: ep %p, already started", __func__, ep);
1286 		printk(KERN_ERR "%s timer already started! ep %p\n", __func__,
1287 		    ep);
1288 		return;
1289 	}
1290 	clear_bit(TIMEOUT, &ep->com.flags);
1291 	c4iw_get_ep(&ep->com);
1292 	ep->timer.expires = jiffies + ep_timeout_secs * HZ;
1293 	ep->timer.data = (unsigned long)ep;
1294 	ep->timer.function = ep_timeout;
1295 	add_timer(&ep->timer);
1296 }
1297 
1298 static int
1299 stop_ep_timer(struct c4iw_ep *ep)
1300 {
1301 
1302 	del_timer_sync(&ep->timer);
1303 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
1304 		c4iw_put_ep(&ep->com);
1305 		return 0;
1306 	}
1307 	return 1;
1308 }
1309 
1310 static void *
1311 alloc_ep(int size, gfp_t gfp)
1312 {
1313 	struct c4iw_ep_common *epc;
1314 
1315 	epc = kzalloc(size, gfp);
1316 	if (epc == NULL)
1317 		return (NULL);
1318 
1319 	kref_init(&epc->kref);
1320 	mutex_init(&epc->mutex);
1321 	c4iw_init_wr_wait(&epc->wr_wait);
1322 
1323 	return (epc);
1324 }
1325 
1326 void _c4iw_free_ep(struct kref *kref)
1327 {
1328 	struct c4iw_ep *ep;
1329 	struct c4iw_ep_common *epc;
1330 
1331 	ep = container_of(kref, struct c4iw_ep, com.kref);
1332 	epc = &ep->com;
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 	int err;
2360 	struct c4iw_ep *ep = to_ep(cm_id);
2361 	int abort = 0;
2362 
2363 	mutex_lock(&ep->com.mutex);
2364 	CTR2(KTR_IW_CXGBE, "%s:crcB %p", __func__, ep);
2365 
2366 	if ((ep->com.state == DEAD) ||
2367 			(ep->com.state != MPA_REQ_RCVD)) {
2368 
2369 		CTR2(KTR_IW_CXGBE, "%s:crc1 %p", __func__, ep);
2370 		mutex_unlock(&ep->com.mutex);
2371 		c4iw_put_ep(&ep->com);
2372 		return -ECONNRESET;
2373 	}
2374 	set_bit(ULP_REJECT, &ep->com.history);
2375 
2376 	if (mpa_rev == 0) {
2377 
2378 		CTR2(KTR_IW_CXGBE, "%s:crc2 %p", __func__, ep);
2379 		abort = 1;
2380 	}
2381 	else {
2382 
2383 		CTR2(KTR_IW_CXGBE, "%s:crc3 %p", __func__, ep);
2384 		abort = send_mpa_reject(ep, pdata, pdata_len);
2385 	}
2386 	STOP_EP_TIMER(ep);
2387 	err = c4iw_ep_disconnect(ep, abort != 0, GFP_KERNEL);
2388 	mutex_unlock(&ep->com.mutex);
2389 	c4iw_put_ep(&ep->com);
2390 	CTR3(KTR_IW_CXGBE, "%s:crc4 %p, err: %d", __func__, ep, err);
2391 	return 0;
2392 }
2393 
2394 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2395 {
2396 	int err;
2397 	struct c4iw_qp_attributes attrs = {0};
2398 	enum c4iw_qp_attr_mask mask;
2399 	struct c4iw_ep *ep = to_ep(cm_id);
2400 	struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
2401 	struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
2402 	int abort = 0;
2403 
2404 	mutex_lock(&ep->com.mutex);
2405 	CTR2(KTR_IW_CXGBE, "%s:cacB %p", __func__, ep);
2406 
2407 	if ((ep->com.state == DEAD) ||
2408 			(ep->com.state != MPA_REQ_RCVD)) {
2409 
2410 		CTR2(KTR_IW_CXGBE, "%s:cac1 %p", __func__, ep);
2411 		err = -ECONNRESET;
2412 		goto err_out;
2413 	}
2414 
2415 	BUG_ON(!qp);
2416 
2417 	set_bit(ULP_ACCEPT, &ep->com.history);
2418 
2419 	if ((conn_param->ord > c4iw_max_read_depth) ||
2420 		(conn_param->ird > c4iw_max_read_depth)) {
2421 
2422 		CTR2(KTR_IW_CXGBE, "%s:cac2 %p", __func__, ep);
2423 		err = -EINVAL;
2424 		goto err_abort;
2425 	}
2426 
2427 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
2428 
2429 		CTR2(KTR_IW_CXGBE, "%s:cac3 %p", __func__, ep);
2430 
2431 		if (conn_param->ord > ep->ird) {
2432 			if (RELAXED_IRD_NEGOTIATION) {
2433 				conn_param->ord = ep->ird;
2434 			} else {
2435 				ep->ird = conn_param->ird;
2436 				ep->ord = conn_param->ord;
2437 				send_mpa_reject(ep, conn_param->private_data,
2438 						conn_param->private_data_len);
2439 				err = -ENOMEM;
2440 				goto err_abort;
2441 			}
2442 		}
2443 		if (conn_param->ird < ep->ord) {
2444 			if (RELAXED_IRD_NEGOTIATION &&
2445 			    ep->ord <= h->rdev.adap->params.max_ordird_qp) {
2446 				conn_param->ird = ep->ord;
2447 			} else {
2448 				err = -ENOMEM;
2449 				goto err_abort;
2450 			}
2451 		}
2452 	}
2453 	ep->ird = conn_param->ird;
2454 	ep->ord = conn_param->ord;
2455 
2456 	if (ep->mpa_attr.version == 1) {
2457 		if (peer2peer && ep->ird == 0)
2458 			ep->ird = 1;
2459 	} else {
2460 		if (peer2peer &&
2461 		    (ep->mpa_attr.p2p_type != FW_RI_INIT_P2PTYPE_DISABLED) &&
2462 		    (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) && ep->ird == 0)
2463 			ep->ird = 1;
2464 	}
2465 
2466 	CTR4(KTR_IW_CXGBE, "%s %d ird %d ord %d", __func__, __LINE__,
2467 			ep->ird, ep->ord);
2468 
2469 	ep->com.cm_id = cm_id;
2470 	ref_cm_id(&ep->com);
2471 	ep->com.qp = qp;
2472 	ref_qp(ep);
2473 	//ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
2474 
2475 	/* bind QP to EP and move to RTS */
2476 	attrs.mpa_attr = ep->mpa_attr;
2477 	attrs.max_ird = ep->ird;
2478 	attrs.max_ord = ep->ord;
2479 	attrs.llp_stream_handle = ep;
2480 	attrs.next_state = C4IW_QP_STATE_RTS;
2481 
2482 	/* bind QP and TID with INIT_WR */
2483 	mask = C4IW_QP_ATTR_NEXT_STATE |
2484 		C4IW_QP_ATTR_LLP_STREAM_HANDLE |
2485 		C4IW_QP_ATTR_MPA_ATTR |
2486 		C4IW_QP_ATTR_MAX_IRD |
2487 		C4IW_QP_ATTR_MAX_ORD;
2488 
2489 	err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
2490 	if (err) {
2491 		CTR3(KTR_IW_CXGBE, "%s:caca %p, err: %d", __func__, ep, err);
2492 		goto err_defef_cm_id;
2493 	}
2494 
2495 	err = send_mpa_reply(ep, conn_param->private_data,
2496 			conn_param->private_data_len);
2497 	if (err) {
2498 		CTR3(KTR_IW_CXGBE, "%s:cacb %p, err: %d", __func__, ep, err);
2499 		goto err_defef_cm_id;
2500 	}
2501 
2502 	ep->com.state = FPDU_MODE;
2503 	established_upcall(ep);
2504 	mutex_unlock(&ep->com.mutex);
2505 	c4iw_put_ep(&ep->com);
2506 	CTR2(KTR_IW_CXGBE, "%s:cacE %p", __func__, ep);
2507 	return 0;
2508 err_defef_cm_id:
2509 	deref_cm_id(&ep->com);
2510 err_abort:
2511 	abort = 1;
2512 err_out:
2513 	if (abort)
2514 		c4iw_ep_disconnect(ep, 1, GFP_KERNEL);
2515 	mutex_unlock(&ep->com.mutex);
2516 	c4iw_put_ep(&ep->com);
2517 	CTR2(KTR_IW_CXGBE, "%s:cacE err %p", __func__, ep);
2518 	return err;
2519 }
2520 
2521 static int
2522 c4iw_sock_create(struct sockaddr_storage *laddr, struct socket **so)
2523 {
2524 	int ret;
2525 	int size, on;
2526 	struct socket *sock = NULL;
2527 	struct sockopt sopt;
2528 
2529 	ret = sock_create_kern(laddr->ss_family,
2530 			SOCK_STREAM, IPPROTO_TCP, &sock);
2531 	if (ret) {
2532 		CTR2(KTR_IW_CXGBE, "%s:Failed to create TCP socket. err %d",
2533 				__func__, ret);
2534 		return ret;
2535 	}
2536 
2537 	if (reuseaddr) {
2538 		bzero(&sopt, sizeof(struct sockopt));
2539 		sopt.sopt_dir = SOPT_SET;
2540 		sopt.sopt_level = SOL_SOCKET;
2541 		sopt.sopt_name = SO_REUSEADDR;
2542 		on = 1;
2543 		sopt.sopt_val = &on;
2544 		sopt.sopt_valsize = sizeof(on);
2545 		ret = -sosetopt(sock, &sopt);
2546 		if (ret != 0) {
2547 			log(LOG_ERR, "%s: sosetopt(%p, SO_REUSEADDR) "
2548 				"failed with %d.\n", __func__, sock, ret);
2549 		}
2550 		bzero(&sopt, sizeof(struct sockopt));
2551 		sopt.sopt_dir = SOPT_SET;
2552 		sopt.sopt_level = SOL_SOCKET;
2553 		sopt.sopt_name = SO_REUSEPORT;
2554 		on = 1;
2555 		sopt.sopt_val = &on;
2556 		sopt.sopt_valsize = sizeof(on);
2557 		ret = -sosetopt(sock, &sopt);
2558 		if (ret != 0) {
2559 			log(LOG_ERR, "%s: sosetopt(%p, SO_REUSEPORT) "
2560 				"failed with %d.\n", __func__, sock, ret);
2561 		}
2562 	}
2563 
2564 	ret = -sobind(sock, (struct sockaddr *)laddr, curthread);
2565 	if (ret) {
2566 		CTR2(KTR_IW_CXGBE, "%s:Failed to bind socket. err %p",
2567 				__func__, ret);
2568 		sock_release(sock);
2569 		return ret;
2570 	}
2571 
2572 	size = laddr->ss_family == AF_INET6 ?
2573 		sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
2574 	ret = sock_getname(sock, (struct sockaddr *)laddr, &size, 0);
2575 	if (ret) {
2576 		CTR2(KTR_IW_CXGBE, "%s:sock_getname failed. err %p",
2577 				__func__, ret);
2578 		sock_release(sock);
2579 		return ret;
2580 	}
2581 
2582 	*so = sock;
2583 	return 0;
2584 }
2585 
2586 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2587 {
2588 	int err = 0;
2589 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2590 	struct c4iw_ep *ep = NULL;
2591 	struct ifnet    *nh_ifp;        /* Logical egress interface */
2592 #ifdef VIMAGE
2593 	struct rdma_cm_id *rdma_id = (struct rdma_cm_id*)cm_id->context;
2594 	struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
2595 #endif
2596 
2597 	CTR2(KTR_IW_CXGBE, "%s:ccB %p", __func__, cm_id);
2598 
2599 
2600 	if ((conn_param->ord > c4iw_max_read_depth) ||
2601 		(conn_param->ird > c4iw_max_read_depth)) {
2602 
2603 		CTR2(KTR_IW_CXGBE, "%s:cc1 %p", __func__, cm_id);
2604 		err = -EINVAL;
2605 		goto out;
2606 	}
2607 	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2608 	cm_id->provider_data = ep;
2609 
2610 	init_timer(&ep->timer);
2611 	ep->plen = conn_param->private_data_len;
2612 
2613 	if (ep->plen) {
2614 
2615 		CTR2(KTR_IW_CXGBE, "%s:cc3 %p", __func__, ep);
2616 		memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
2617 				conn_param->private_data, ep->plen);
2618 	}
2619 	ep->ird = conn_param->ird;
2620 	ep->ord = conn_param->ord;
2621 
2622 	if (peer2peer && ep->ord == 0) {
2623 
2624 		CTR2(KTR_IW_CXGBE, "%s:cc4 %p", __func__, ep);
2625 		ep->ord = 1;
2626 	}
2627 
2628 	ep->com.dev = dev;
2629 	ep->com.cm_id = cm_id;
2630 	ref_cm_id(&ep->com);
2631 	ep->com.qp = get_qhp(dev, conn_param->qpn);
2632 
2633 	if (!ep->com.qp) {
2634 
2635 		CTR2(KTR_IW_CXGBE, "%s:cc5 %p", __func__, ep);
2636 		err = -EINVAL;
2637 		goto fail;
2638 	}
2639 	ref_qp(ep);
2640 	ep->com.thread = curthread;
2641 
2642 	CURVNET_SET(vnet);
2643 	err = get_ifnet_from_raddr(&cm_id->remote_addr, &nh_ifp);
2644 	CURVNET_RESTORE();
2645 
2646 	if (err) {
2647 
2648 		CTR2(KTR_IW_CXGBE, "%s:cc7 %p", __func__, ep);
2649 		printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
2650 		err = EHOSTUNREACH;
2651 		return err;
2652 	}
2653 
2654 	if (!(nh_ifp->if_capenable & IFCAP_TOE) ||
2655 	    TOEDEV(nh_ifp) == NULL) {
2656 		err = -ENOPROTOOPT;
2657 		goto fail;
2658 	}
2659 	ep->com.state = CONNECTING;
2660 	ep->tos = 0;
2661 	ep->com.local_addr = cm_id->local_addr;
2662 	ep->com.remote_addr = cm_id->remote_addr;
2663 
2664 	err = c4iw_sock_create(&cm_id->local_addr, &ep->com.so);
2665 	if (err)
2666 		goto fail;
2667 
2668 	setiwsockopt(ep->com.so);
2669 	init_iwarp_socket(ep->com.so, &ep->com);
2670 	err = -soconnect(ep->com.so, (struct sockaddr *)&ep->com.remote_addr,
2671 		ep->com.thread);
2672 	if (err)
2673 		goto fail_free_so;
2674 	CTR2(KTR_IW_CXGBE, "%s:ccE, ep %p", __func__, ep);
2675 	return 0;
2676 
2677 fail_free_so:
2678 	uninit_iwarp_socket(ep->com.so);
2679 	ep->com.state = DEAD;
2680 	sock_release(ep->com.so);
2681 fail:
2682 	deref_cm_id(&ep->com);
2683 	c4iw_put_ep(&ep->com);
2684 	ep = NULL;
2685 out:
2686 	CTR2(KTR_IW_CXGBE, "%s:ccE Error %d", __func__, err);
2687 	return err;
2688 }
2689 
2690 /*
2691  * iwcm->create_listen.  Returns -errno on failure.
2692  */
2693 int
2694 c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
2695 {
2696 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2697 	struct c4iw_listen_ep *lep = NULL;
2698 	struct listen_port_info *port_info = NULL;
2699 	int rc = 0;
2700 
2701 	CTR3(KTR_IW_CXGBE, "%s: cm_id %p, backlog %s", __func__, cm_id,
2702 			backlog);
2703 	lep = alloc_ep(sizeof(*lep), GFP_KERNEL);
2704 	lep->com.cm_id = cm_id;
2705 	ref_cm_id(&lep->com);
2706 	lep->com.dev = dev;
2707 	lep->backlog = backlog;
2708 	lep->com.local_addr = cm_id->local_addr;
2709 	lep->com.thread = curthread;
2710 	cm_id->provider_data = lep;
2711 	lep->com.state = LISTEN;
2712 
2713 	/* In case of INDADDR_ANY, ibcore creates cmid for each device and
2714 	 * invokes iw_cxgbe listener callbacks assuming that iw_cxgbe creates
2715 	 * HW listeners for each device seperately. But toecore expects single
2716 	 * solisten() call with INADDR_ANY address to create HW listeners on
2717 	 * all devices for a given port number. So iw_cxgbe driver calls
2718 	 * solisten() only once for INADDR_ANY(usually done at first time
2719 	 * listener callback from ibcore). And all the subsequent INADDR_ANY
2720 	 * listener callbacks from ibcore(for the same port address) do not
2721 	 * invoke solisten() as first listener callback has already created
2722 	 * listeners for all other devices(via solisten).
2723 	 */
2724 	if (c4iw_any_addr((struct sockaddr *)&lep->com.local_addr)) {
2725 		port_info = add_ep_to_listenlist(lep);
2726 		/* skip solisten() if refcnt > 1, as the listeners were
2727 		 * alredy created by 'Master lep'
2728 		 */
2729 		if (port_info->refcnt > 1) {
2730 			/* As there will be only one listener socket for a TCP
2731 			 * port, copy Master lep's socket pointer to other lep's
2732 			 * that are belonging to same TCP port.
2733 			 */
2734 			struct c4iw_listen_ep *head_lep =
2735 					container_of(port_info->lep_list.next,
2736 					struct c4iw_listen_ep, listen_ep_list);
2737 			lep->com.so =  head_lep->com.so;
2738 			goto out;
2739 		}
2740 	}
2741 	rc = c4iw_sock_create(&cm_id->local_addr, &lep->com.so);
2742 	if (rc) {
2743 		CTR2(KTR_IW_CXGBE, "%s:Failed to create socket. err %d",
2744 				__func__, rc);
2745 		goto fail;
2746 	}
2747 
2748 	rc = -solisten(lep->com.so, backlog, curthread);
2749 	if (rc) {
2750 		CTR3(KTR_IW_CXGBE, "%s:Failed to listen on sock:%p. err %d",
2751 				__func__, lep->com.so, rc);
2752 		goto fail_free_so;
2753 	}
2754 	init_iwarp_socket(lep->com.so, &lep->com);
2755 out:
2756 	return 0;
2757 
2758 fail_free_so:
2759 	sock_release(lep->com.so);
2760 fail:
2761 	if (port_info)
2762 		rem_ep_from_listenlist(lep);
2763 	deref_cm_id(&lep->com);
2764 	c4iw_put_ep(&lep->com);
2765 	return rc;
2766 }
2767 
2768 int
2769 c4iw_destroy_listen(struct iw_cm_id *cm_id)
2770 {
2771 	struct c4iw_listen_ep *lep = to_listen_ep(cm_id);
2772 
2773 	mutex_lock(&lep->com.mutex);
2774 	CTR3(KTR_IW_CXGBE, "%s: cm_id %p, state %s", __func__, cm_id,
2775 	    states[lep->com.state]);
2776 
2777 	lep->com.state = DEAD;
2778 	if (c4iw_any_addr((struct sockaddr *)&lep->com.local_addr)) {
2779 		/* if no refcount then close listen socket */
2780 		if (!rem_ep_from_listenlist(lep))
2781 			close_socket(lep->com.so);
2782 	} else
2783 		close_socket(lep->com.so);
2784 	deref_cm_id(&lep->com);
2785 	mutex_unlock(&lep->com.mutex);
2786 	c4iw_put_ep(&lep->com);
2787 	return 0;
2788 }
2789 
2790 int __c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2791 {
2792 	int ret;
2793 	mutex_lock(&ep->com.mutex);
2794 	ret = c4iw_ep_disconnect(ep, abrupt, gfp);
2795 	mutex_unlock(&ep->com.mutex);
2796 	return ret;
2797 }
2798 
2799 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2800 {
2801 	int ret = 0;
2802 	int close = 0;
2803 	int fatal = 0;
2804 	struct c4iw_rdev *rdev;
2805 
2806 
2807 	CTR2(KTR_IW_CXGBE, "%s:cedB %p", __func__, ep);
2808 
2809 	rdev = &ep->com.dev->rdev;
2810 
2811 	if (c4iw_fatal_error(rdev)) {
2812 
2813 		CTR2(KTR_IW_CXGBE, "%s:ced1 %p", __func__, ep);
2814 		fatal = 1;
2815 		close_complete_upcall(ep, -ECONNRESET);
2816 		send_abort(ep);
2817 		ep->com.state = DEAD;
2818 	}
2819 	CTR3(KTR_IW_CXGBE, "%s:ced2 %p %s", __func__, ep,
2820 	    states[ep->com.state]);
2821 
2822 	/*
2823 	 * Ref the ep here in case we have fatal errors causing the
2824 	 * ep to be released and freed.
2825 	 */
2826 	c4iw_get_ep(&ep->com);
2827 	switch (ep->com.state) {
2828 
2829 		case MPA_REQ_WAIT:
2830 		case MPA_REQ_SENT:
2831 		case MPA_REQ_RCVD:
2832 		case MPA_REP_SENT:
2833 		case FPDU_MODE:
2834 			close = 1;
2835 			if (abrupt)
2836 				ep->com.state = ABORTING;
2837 			else {
2838 				ep->com.state = CLOSING;
2839 				START_EP_TIMER(ep);
2840 			}
2841 			set_bit(CLOSE_SENT, &ep->com.flags);
2842 			break;
2843 
2844 		case CLOSING:
2845 
2846 			if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2847 
2848 				close = 1;
2849 				if (abrupt) {
2850 					STOP_EP_TIMER(ep);
2851 					ep->com.state = ABORTING;
2852 				} else
2853 					ep->com.state = MORIBUND;
2854 			}
2855 			break;
2856 
2857 		case MORIBUND:
2858 		case ABORTING:
2859 		case DEAD:
2860 			CTR3(KTR_IW_CXGBE,
2861 			    "%s ignoring disconnect ep %p state %u", __func__,
2862 			    ep, ep->com.state);
2863 			break;
2864 
2865 		default:
2866 			BUG();
2867 			break;
2868 	}
2869 
2870 
2871 	if (close) {
2872 
2873 		CTR2(KTR_IW_CXGBE, "%s:ced3 %p", __func__, ep);
2874 
2875 		if (abrupt) {
2876 
2877 			CTR2(KTR_IW_CXGBE, "%s:ced4 %p", __func__, ep);
2878 			set_bit(EP_DISC_ABORT, &ep->com.history);
2879 			close_complete_upcall(ep, -ECONNRESET);
2880 			ret = send_abort(ep);
2881 			if (ret)
2882 				fatal = 1;
2883 		} else {
2884 
2885 			CTR2(KTR_IW_CXGBE, "%s:ced5 %p", __func__, ep);
2886 			set_bit(EP_DISC_CLOSE, &ep->com.history);
2887 
2888 			if (!ep->parent_ep)
2889 				ep->com.state = MORIBUND;
2890 
2891 			CURVNET_SET(ep->com.so->so_vnet);
2892 			sodisconnect(ep->com.so);
2893 			CURVNET_RESTORE();
2894 		}
2895 
2896 	}
2897 
2898 	if (fatal) {
2899 		set_bit(EP_DISC_FAIL, &ep->com.history);
2900 		if (!abrupt) {
2901 			STOP_EP_TIMER(ep);
2902 			close_complete_upcall(ep, -EIO);
2903 		}
2904 		if (ep->com.qp) {
2905 			struct c4iw_qp_attributes attrs = {0};
2906 
2907 			attrs.next_state = C4IW_QP_STATE_ERROR;
2908 			ret = c4iw_modify_qp(ep->com.dev, ep->com.qp,
2909 						C4IW_QP_ATTR_NEXT_STATE,
2910 						&attrs, 1);
2911 			if (ret) {
2912 				CTR2(KTR_IW_CXGBE, "%s:ced7 %p", __func__, ep);
2913 				printf("%s - qp <- error failed!\n", __func__);
2914 			}
2915 		}
2916 		release_ep_resources(ep);
2917 		ep->com.state = DEAD;
2918 		CTR2(KTR_IW_CXGBE, "%s:ced6 %p", __func__, ep);
2919 	}
2920 	c4iw_put_ep(&ep->com);
2921 	CTR2(KTR_IW_CXGBE, "%s:cedE %p", __func__, ep);
2922 	return ret;
2923 }
2924 
2925 #ifdef C4IW_EP_REDIRECT
2926 int c4iw_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2927 		struct l2t_entry *l2t)
2928 {
2929 	struct c4iw_ep *ep = ctx;
2930 
2931 	if (ep->dst != old)
2932 		return 0;
2933 
2934 	PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2935 			l2t);
2936 	dst_hold(new);
2937 	cxgb4_l2t_release(ep->l2t);
2938 	ep->l2t = l2t;
2939 	dst_release(old);
2940 	ep->dst = new;
2941 	return 1;
2942 }
2943 #endif
2944 
2945 
2946 
2947 static void ep_timeout(unsigned long arg)
2948 {
2949 	struct c4iw_ep *ep = (struct c4iw_ep *)arg;
2950 
2951 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
2952 
2953 		/*
2954 		 * Only insert if it is not already on the list.
2955 		 */
2956 		if (!(ep->com.ep_events & C4IW_EVENT_TIMEOUT)) {
2957 			CTR2(KTR_IW_CXGBE, "%s:et1 %p", __func__, ep);
2958 			add_ep_to_req_list(ep, C4IW_EVENT_TIMEOUT);
2959 		}
2960 	}
2961 }
2962 
2963 static int fw6_wr_rpl(struct adapter *sc, const __be64 *rpl)
2964 {
2965 	uint64_t val = be64toh(*rpl);
2966 	int ret;
2967 	struct c4iw_wr_wait *wr_waitp;
2968 
2969 	ret = (int)((val >> 8) & 0xff);
2970 	wr_waitp = (struct c4iw_wr_wait *)rpl[1];
2971 	CTR3(KTR_IW_CXGBE, "%s wr_waitp %p ret %u", __func__, wr_waitp, ret);
2972 	if (wr_waitp)
2973 		c4iw_wake_up(wr_waitp, ret ? -ret : 0);
2974 
2975 	return (0);
2976 }
2977 
2978 static int fw6_cqe_handler(struct adapter *sc, const __be64 *rpl)
2979 {
2980 	struct cqe_list_entry *cle;
2981 	unsigned long flag;
2982 
2983 	cle = malloc(sizeof(*cle), M_CXGBE, M_NOWAIT);
2984 	cle->rhp = sc->iwarp_softc;
2985 	cle->err_cqe = *(const struct t4_cqe *)(&rpl[0]);
2986 
2987 	spin_lock_irqsave(&err_cqe_lock, flag);
2988 	list_add_tail(&cle->entry, &err_cqe_list);
2989 	queue_work(c4iw_taskq, &c4iw_task);
2990 	spin_unlock_irqrestore(&err_cqe_lock, flag);
2991 
2992 	return (0);
2993 }
2994 
2995 static int
2996 process_terminate(struct c4iw_ep *ep)
2997 {
2998 	struct c4iw_qp_attributes attrs = {0};
2999 
3000 	CTR2(KTR_IW_CXGBE, "%s:tB %p %d", __func__, ep);
3001 
3002 	if (ep && ep->com.qp) {
3003 
3004 		printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n",
3005 				ep->hwtid, ep->com.qp->wq.sq.qid);
3006 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
3007 		c4iw_modify_qp(ep->com.dev, ep->com.qp, C4IW_QP_ATTR_NEXT_STATE, &attrs,
3008 				1);
3009 	} else
3010 		printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n",
3011 								ep->hwtid);
3012 	CTR2(KTR_IW_CXGBE, "%s:tE %p %d", __func__, ep);
3013 
3014 	return 0;
3015 }
3016 
3017 int __init c4iw_cm_init(void)
3018 {
3019 
3020 	t4_register_cpl_handler(CPL_RDMA_TERMINATE, terminate);
3021 	t4_register_fw_msg_handler(FW6_TYPE_WR_RPL, fw6_wr_rpl);
3022 	t4_register_fw_msg_handler(FW6_TYPE_CQE, fw6_cqe_handler);
3023 	t4_register_an_handler(c4iw_ev_handler);
3024 
3025 	TAILQ_INIT(&req_list);
3026 	spin_lock_init(&req_lock);
3027 	INIT_LIST_HEAD(&err_cqe_list);
3028 	spin_lock_init(&err_cqe_lock);
3029 
3030 	INIT_WORK(&c4iw_task, process_req);
3031 
3032 	c4iw_taskq = create_singlethread_workqueue("iw_cxgbe");
3033 	if (!c4iw_taskq)
3034 		return -ENOMEM;
3035 
3036 	return 0;
3037 }
3038 
3039 void __exit c4iw_cm_term(void)
3040 {
3041 	WARN_ON(!TAILQ_EMPTY(&req_list));
3042 	WARN_ON(!list_empty(&err_cqe_list));
3043 	flush_workqueue(c4iw_taskq);
3044 	destroy_workqueue(c4iw_taskq);
3045 
3046 	t4_register_cpl_handler(CPL_RDMA_TERMINATE, NULL);
3047 	t4_register_fw_msg_handler(FW6_TYPE_WR_RPL, NULL);
3048 	t4_register_fw_msg_handler(FW6_TYPE_CQE, NULL);
3049 	t4_register_an_handler(NULL);
3050 }
3051 #endif
3052