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