xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/cm.c (revision 5608fd23c27fa1e8ee595d7b678cbfd35d657fbe)
1 /*
2  * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *	  copyright notice, this list of conditions and the following
16  *	  disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *	  copyright notice, this list of conditions and the following
20  *	  disclaimer in the documentation and/or other materials
21  *	  provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
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/neighbour.h>
46 #include <net/route.h>
47 
48 #include <netinet/in_systm.h>
49 #include <netinet/in_pcb.h>
50 #include <netinet/ip.h>
51 #include <netinet/ip_var.h>
52 #include <netinet/tcp_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcpip.h>
55 
56 #include <netinet/toecore.h>
57 
58 struct sge_iq;
59 struct rss_header;
60 #include <linux/types.h>
61 #include "offload.h"
62 #include "tom/t4_tom.h"
63 
64 #define TOEPCB(so)  ((struct toepcb *)(so_sototcpcb((so))->t_toe))
65 
66 #include "iw_cxgbe.h"
67 #include <linux/module.h>
68 #include <linux/workqueue.h>
69 #include <linux/notifier.h>
70 #include <linux/inetdevice.h>
71 #include <linux/if_vlan.h>
72 #include <net/netevent.h>
73 
74 static spinlock_t req_lock;
75 static TAILQ_HEAD(c4iw_ep_list, c4iw_ep_common) req_list;
76 static struct work_struct c4iw_task;
77 static struct workqueue_struct *c4iw_taskq;
78 static LIST_HEAD(timeout_list);
79 static spinlock_t timeout_lock;
80 
81 static void process_req(struct work_struct *ctx);
82 static void start_ep_timer(struct c4iw_ep *ep);
83 static void stop_ep_timer(struct c4iw_ep *ep);
84 static int set_tcpinfo(struct c4iw_ep *ep);
85 static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc);
86 static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state tostate);
87 static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state tostate);
88 static void *alloc_ep(int size, gfp_t flags);
89 void __free_ep(struct c4iw_ep_common *epc);
90 static struct rtentry * find_route(__be32 local_ip, __be32 peer_ip, __be16 local_port,
91 		__be16 peer_port, u8 tos);
92 static int close_socket(struct c4iw_ep_common *epc, int close);
93 static int shutdown_socket(struct c4iw_ep_common *epc);
94 static void abort_socket(struct c4iw_ep *ep);
95 static void 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);
99 static int abort_connection(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 void connect_request_upcall(struct c4iw_ep *ep);
104 static void established_upcall(struct c4iw_ep *ep);
105 static void process_mpa_reply(struct c4iw_ep *ep);
106 static void 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(unsigned long arg);
111 static void init_sock(struct c4iw_ep_common *epc);
112 static void process_data(struct c4iw_ep *ep);
113 static void process_connected(struct c4iw_ep *ep);
114 static struct socket * dequeue_socket(struct socket *head, struct sockaddr_in **remote, struct c4iw_ep *child_ep);
115 static void process_newconn(struct c4iw_ep *parent_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 
120 #define START_EP_TIMER(ep) \
121     do { \
122 	    CTR3(KTR_IW_CXGBE, "start_ep_timer (%s:%d) ep %p", \
123 		__func__, __LINE__, (ep)); \
124 	    start_ep_timer(ep); \
125     } while (0)
126 
127 #define STOP_EP_TIMER(ep) \
128     do { \
129 	    CTR3(KTR_IW_CXGBE, "stop_ep_timer (%s:%d) ep %p", \
130 		__func__, __LINE__, (ep)); \
131 	    stop_ep_timer(ep); \
132     } while (0)
133 
134 #ifdef KTR
135 static char *states[] = {
136 	"idle",
137 	"listen",
138 	"connecting",
139 	"mpa_wait_req",
140 	"mpa_req_sent",
141 	"mpa_req_rcvd",
142 	"mpa_rep_sent",
143 	"fpdu_mode",
144 	"aborting",
145 	"closing",
146 	"moribund",
147 	"dead",
148 	NULL,
149 };
150 #endif
151 
152 static void
153 process_req(struct work_struct *ctx)
154 {
155 	struct c4iw_ep_common *epc;
156 
157 	spin_lock(&req_lock);
158 	while (!TAILQ_EMPTY(&req_list)) {
159 		epc = TAILQ_FIRST(&req_list);
160 		TAILQ_REMOVE(&req_list, epc, entry);
161 		epc->entry.tqe_prev = NULL;
162 		spin_unlock(&req_lock);
163 		if (epc->so)
164 			process_socket_event((struct c4iw_ep *)epc);
165 		c4iw_put_ep(epc);
166 		spin_lock(&req_lock);
167 	}
168 	spin_unlock(&req_lock);
169 }
170 
171 /*
172  * XXX: doesn't belong here in the iWARP driver.
173  * XXX: assumes that the connection was offloaded by cxgbe/t4_tom if TF_TOE is
174  *      set.  Is this a valid assumption for active open?
175  */
176 static int
177 set_tcpinfo(struct c4iw_ep *ep)
178 {
179 	struct socket *so = ep->com.so;
180 	struct inpcb *inp = sotoinpcb(so);
181 	struct tcpcb *tp;
182 	struct toepcb *toep;
183 	int rc = 0;
184 
185 	INP_WLOCK(inp);
186 	tp = intotcpcb(inp);
187 	if ((tp->t_flags & TF_TOE) == 0) {
188 		rc = EINVAL;
189 		log(LOG_ERR, "%s: connection not offloaded (so %p, ep %p)\n",
190 		    __func__, so, ep);
191 		goto done;
192 	}
193 	toep = TOEPCB(so);
194 
195 	ep->hwtid = toep->tid;
196 	ep->snd_seq = tp->snd_nxt;
197 	ep->rcv_seq = tp->rcv_nxt;
198 	ep->emss = max(tp->t_maxseg, 128);
199 done:
200 	INP_WUNLOCK(inp);
201 	return (rc);
202 
203 }
204 
205 static struct rtentry *
206 find_route(__be32 local_ip, __be32 peer_ip, __be16 local_port,
207 		__be16 peer_port, u8 tos)
208 {
209 	struct route iproute;
210 	struct sockaddr_in *dst = (struct sockaddr_in *)&iproute.ro_dst;
211 
212 	CTR5(KTR_IW_CXGBE, "%s:frtB %x, %x, %d, %d", __func__, local_ip,
213 	    peer_ip, ntohs(local_port), ntohs(peer_port));
214 	bzero(&iproute, sizeof iproute);
215 	dst->sin_family = AF_INET;
216 	dst->sin_len = sizeof *dst;
217 	dst->sin_addr.s_addr = peer_ip;
218 
219 	rtalloc(&iproute);
220 	CTR2(KTR_IW_CXGBE, "%s:frtE %p", __func__, (uint64_t)iproute.ro_rt);
221 	return iproute.ro_rt;
222 }
223 
224 static int
225 close_socket(struct c4iw_ep_common *epc, int close)
226 {
227 	struct socket *so = epc->so;
228 	int rc;
229 
230 	CTR4(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s", __func__, epc, so,
231 	    states[epc->state]);
232 
233 	SOCK_LOCK(so);
234 	soupcall_clear(so, SO_RCV);
235 	SOCK_UNLOCK(so);
236 
237 	if (close)
238                 rc = soclose(so);
239         else
240                 rc = soshutdown(so, SHUT_WR | SHUT_RD);
241 	epc->so = NULL;
242 
243 	return (rc);
244 }
245 
246 static int
247 shutdown_socket(struct c4iw_ep_common *epc)
248 {
249 
250 	CTR4(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s", __func__, epc->so, epc,
251 	    states[epc->state]);
252 
253 	return (soshutdown(epc->so, SHUT_WR));
254 }
255 
256 static void
257 abort_socket(struct c4iw_ep *ep)
258 {
259 	struct sockopt sopt;
260 	int rc;
261 	struct linger l;
262 
263 	CTR4(KTR_IW_CXGBE, "%s ep %p so %p state %s", __func__, ep, ep->com.so,
264 	    states[ep->com.state]);
265 
266 	l.l_onoff = 1;
267 	l.l_linger = 0;
268 
269 	/* linger_time of 0 forces RST to be sent */
270 	sopt.sopt_dir = SOPT_SET;
271 	sopt.sopt_level = SOL_SOCKET;
272 	sopt.sopt_name = SO_LINGER;
273 	sopt.sopt_val = (caddr_t)&l;
274 	sopt.sopt_valsize = sizeof l;
275 	sopt.sopt_td = NULL;
276 	rc = sosetopt(ep->com.so, &sopt);
277 	if (rc) {
278 		log(LOG_ERR, "%s: can't set linger to 0, no RST! err %d\n",
279 		    __func__, rc);
280 	}
281 }
282 
283 static void
284 process_peer_close(struct c4iw_ep *ep)
285 {
286 	struct c4iw_qp_attributes attrs;
287 	int disconnect = 1;
288 	int release = 0;
289 
290 	CTR4(KTR_IW_CXGBE, "%s:ppcB ep %p so %p state %s", __func__, ep,
291 	    ep->com.so, states[ep->com.state]);
292 
293 	mutex_lock(&ep->com.mutex);
294 	switch (ep->com.state) {
295 
296 		case MPA_REQ_WAIT:
297 			CTR2(KTR_IW_CXGBE, "%s:ppc1 %p MPA_REQ_WAIT CLOSING",
298 			    __func__, ep);
299 			__state_set(&ep->com, CLOSING);
300 			break;
301 
302 		case MPA_REQ_SENT:
303 			CTR2(KTR_IW_CXGBE, "%s:ppc2 %p MPA_REQ_SENT CLOSING",
304 			    __func__, ep);
305 			__state_set(&ep->com, DEAD);
306 			connect_reply_upcall(ep, -ECONNABORTED);
307 
308 			disconnect = 0;
309 			STOP_EP_TIMER(ep);
310 			close_socket(&ep->com, 0);
311 			ep->com.cm_id->rem_ref(ep->com.cm_id);
312 			ep->com.cm_id = NULL;
313 			ep->com.qp = NULL;
314 			release = 1;
315 			break;
316 
317 		case MPA_REQ_RCVD:
318 
319 			/*
320 			 * We're gonna mark this puppy DEAD, but keep
321 			 * the reference on it until the ULP accepts or
322 			 * rejects the CR.
323 			 */
324 			CTR2(KTR_IW_CXGBE, "%s:ppc3 %p MPA_REQ_RCVD CLOSING",
325 			    __func__, ep);
326 			__state_set(&ep->com, CLOSING);
327 			c4iw_get_ep(&ep->com);
328 			break;
329 
330 		case MPA_REP_SENT:
331 			CTR2(KTR_IW_CXGBE, "%s:ppc4 %p MPA_REP_SENT CLOSING",
332 			    __func__, ep);
333 			__state_set(&ep->com, CLOSING);
334 			break;
335 
336 		case FPDU_MODE:
337 			CTR2(KTR_IW_CXGBE, "%s:ppc5 %p FPDU_MODE CLOSING",
338 			    __func__, ep);
339 			START_EP_TIMER(ep);
340 			__state_set(&ep->com, CLOSING);
341 			attrs.next_state = C4IW_QP_STATE_CLOSING;
342 			c4iw_modify_qp(ep->com.dev, ep->com.qp,
343 					C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
344 			peer_close_upcall(ep);
345 			break;
346 
347 		case ABORTING:
348 			CTR2(KTR_IW_CXGBE, "%s:ppc6 %p ABORTING (disconn)",
349 			    __func__, ep);
350 			disconnect = 0;
351 			break;
352 
353 		case CLOSING:
354 			CTR2(KTR_IW_CXGBE, "%s:ppc7 %p CLOSING MORIBUND",
355 			    __func__, ep);
356 			__state_set(&ep->com, MORIBUND);
357 			disconnect = 0;
358 			break;
359 
360 		case MORIBUND:
361 			CTR2(KTR_IW_CXGBE, "%s:ppc8 %p MORIBUND DEAD", __func__,
362 			    ep);
363 			STOP_EP_TIMER(ep);
364 			if (ep->com.cm_id && ep->com.qp) {
365 				attrs.next_state = C4IW_QP_STATE_IDLE;
366 				c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
367 						C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
368 			}
369 			close_socket(&ep->com, 0);
370 			close_complete_upcall(ep);
371 			__state_set(&ep->com, DEAD);
372 			release = 1;
373 			disconnect = 0;
374 			break;
375 
376 		case DEAD:
377 			CTR2(KTR_IW_CXGBE, "%s:ppc9 %p DEAD (disconn)",
378 			    __func__, ep);
379 			disconnect = 0;
380 			break;
381 
382 		default:
383 			panic("%s: ep %p state %d", __func__, ep,
384 			    ep->com.state);
385 			break;
386 	}
387 
388 	mutex_unlock(&ep->com.mutex);
389 
390 	if (disconnect) {
391 
392 		CTR2(KTR_IW_CXGBE, "%s:ppca %p", __func__, ep);
393 		c4iw_ep_disconnect(ep, 0, M_NOWAIT);
394 	}
395 	if (release) {
396 
397 		CTR2(KTR_IW_CXGBE, "%s:ppcb %p", __func__, ep);
398 		c4iw_put_ep(&ep->com);
399 	}
400 	CTR2(KTR_IW_CXGBE, "%s:ppcE %p", __func__, ep);
401 	return;
402 }
403 
404 static void
405 process_conn_error(struct c4iw_ep *ep)
406 {
407 	struct c4iw_qp_attributes attrs;
408 	int ret;
409 	int state;
410 
411 	state = state_read(&ep->com);
412 	CTR5(KTR_IW_CXGBE, "%s:pceB ep %p so %p so->so_error %u state %s",
413 	    __func__, ep, ep->com.so, ep->com.so->so_error,
414 	    states[ep->com.state]);
415 
416 	switch (state) {
417 
418 		case MPA_REQ_WAIT:
419 			STOP_EP_TIMER(ep);
420 			break;
421 
422 		case MPA_REQ_SENT:
423 			STOP_EP_TIMER(ep);
424 			connect_reply_upcall(ep, -ECONNRESET);
425 			break;
426 
427 		case MPA_REP_SENT:
428 			ep->com.rpl_err = ECONNRESET;
429 			CTR1(KTR_IW_CXGBE, "waking up ep %p", ep);
430 			break;
431 
432 		case MPA_REQ_RCVD:
433 
434 			/*
435 			 * We're gonna mark this puppy DEAD, but keep
436 			 * the reference on it until the ULP accepts or
437 			 * rejects the CR.
438 			 */
439 			c4iw_get_ep(&ep->com);
440 			break;
441 
442 		case MORIBUND:
443 		case CLOSING:
444 			STOP_EP_TIMER(ep);
445 			/*FALLTHROUGH*/
446 		case FPDU_MODE:
447 
448 			if (ep->com.cm_id && ep->com.qp) {
449 
450 				attrs.next_state = C4IW_QP_STATE_ERROR;
451 				ret = c4iw_modify_qp(ep->com.qp->rhp,
452 					ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
453 					&attrs, 1);
454 				if (ret)
455 					log(LOG_ERR,
456 							"%s - qp <- error failed!\n",
457 							__func__);
458 			}
459 			peer_abort_upcall(ep);
460 			break;
461 
462 		case ABORTING:
463 			break;
464 
465 		case DEAD:
466 			CTR2(KTR_IW_CXGBE, "%s so_error %d IN DEAD STATE!!!!",
467 			    __func__, ep->com.so->so_error);
468 			return;
469 
470 		default:
471 			panic("%s: ep %p state %d", __func__, ep, state);
472 			break;
473 	}
474 
475 	if (state != ABORTING) {
476 
477 		CTR2(KTR_IW_CXGBE, "%s:pce1 %p", __func__, ep);
478 		close_socket(&ep->com, 0);
479 		state_set(&ep->com, DEAD);
480 		c4iw_put_ep(&ep->com);
481 	}
482 	CTR2(KTR_IW_CXGBE, "%s:pceE %p", __func__, ep);
483 	return;
484 }
485 
486 static void
487 process_close_complete(struct c4iw_ep *ep)
488 {
489 	struct c4iw_qp_attributes attrs;
490 	int release = 0;
491 
492 	CTR4(KTR_IW_CXGBE, "%s:pccB ep %p so %p state %s", __func__, ep,
493 	    ep->com.so, states[ep->com.state]);
494 
495 	/* The cm_id may be null if we failed to connect */
496 	mutex_lock(&ep->com.mutex);
497 
498 	switch (ep->com.state) {
499 
500 		case CLOSING:
501 			CTR2(KTR_IW_CXGBE, "%s:pcc1 %p CLOSING MORIBUND",
502 			    __func__, ep);
503 			__state_set(&ep->com, MORIBUND);
504 			break;
505 
506 		case MORIBUND:
507 			CTR2(KTR_IW_CXGBE, "%s:pcc1 %p MORIBUND DEAD", __func__,
508 			    ep);
509 			STOP_EP_TIMER(ep);
510 
511 			if ((ep->com.cm_id) && (ep->com.qp)) {
512 
513 				CTR2(KTR_IW_CXGBE, "%s:pcc2 %p QP_STATE_IDLE",
514 				    __func__, ep);
515 				attrs.next_state = C4IW_QP_STATE_IDLE;
516 				c4iw_modify_qp(ep->com.dev,
517 						ep->com.qp,
518 						C4IW_QP_ATTR_NEXT_STATE,
519 						&attrs, 1);
520 			}
521 
522 			if (ep->parent_ep) {
523 
524 				CTR2(KTR_IW_CXGBE, "%s:pcc3 %p", __func__, ep);
525 				close_socket(&ep->com, 1);
526 			}
527 			else {
528 
529 				CTR2(KTR_IW_CXGBE, "%s:pcc4 %p", __func__, ep);
530 				close_socket(&ep->com, 0);
531 			}
532 			close_complete_upcall(ep);
533 			__state_set(&ep->com, DEAD);
534 			release = 1;
535 			break;
536 
537 		case ABORTING:
538 			CTR2(KTR_IW_CXGBE, "%s:pcc5 %p ABORTING", __func__, ep);
539 			break;
540 
541 		case DEAD:
542 		default:
543 			CTR2(KTR_IW_CXGBE, "%s:pcc6 %p DEAD", __func__, ep);
544 			panic("%s:pcc6 %p DEAD", __func__, ep);
545 			break;
546 	}
547 	mutex_unlock(&ep->com.mutex);
548 
549 	if (release) {
550 
551 		CTR2(KTR_IW_CXGBE, "%s:pcc7 %p", __func__, ep);
552 		c4iw_put_ep(&ep->com);
553 	}
554 	CTR2(KTR_IW_CXGBE, "%s:pccE %p", __func__, ep);
555 	return;
556 }
557 
558 static void
559 init_sock(struct c4iw_ep_common *epc)
560 {
561 	int rc;
562 	struct sockopt sopt;
563 	struct socket *so = epc->so;
564 	int on = 1;
565 
566 	SOCK_LOCK(so);
567 	soupcall_set(so, SO_RCV, c4iw_so_upcall, epc);
568 	so->so_state |= SS_NBIO;
569 	SOCK_UNLOCK(so);
570 	sopt.sopt_dir = SOPT_SET;
571 	sopt.sopt_level = IPPROTO_TCP;
572 	sopt.sopt_name = TCP_NODELAY;
573 	sopt.sopt_val = (caddr_t)&on;
574 	sopt.sopt_valsize = sizeof on;
575 	sopt.sopt_td = NULL;
576 	rc = sosetopt(so, &sopt);
577 	if (rc) {
578 		log(LOG_ERR, "%s: can't set TCP_NODELAY on so %p (%d)\n",
579 		    __func__, so, rc);
580 	}
581 }
582 
583 static void
584 process_data(struct c4iw_ep *ep)
585 {
586 	struct sockaddr_in *local, *remote;
587 
588 	CTR5(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s, sb_cc %d", __func__,
589 	    ep->com.so, ep, states[ep->com.state], ep->com.so->so_rcv.sb_cc);
590 
591 	switch (state_read(&ep->com)) {
592 	case MPA_REQ_SENT:
593 		process_mpa_reply(ep);
594 		break;
595 	case MPA_REQ_WAIT:
596 		in_getsockaddr(ep->com.so, (struct sockaddr **)&local);
597 		in_getpeeraddr(ep->com.so, (struct sockaddr **)&remote);
598 		ep->com.local_addr = *local;
599 		ep->com.remote_addr = *remote;
600 		free(local, M_SONAME);
601 		free(remote, M_SONAME);
602 		process_mpa_request(ep);
603 		break;
604 	default:
605 		if (ep->com.so->so_rcv.sb_cc)
606 			log(LOG_ERR, "%s: Unexpected streaming data.  "
607 			    "ep %p, state %d, so %p, so_state 0x%x, sb_cc %u\n",
608 			    __func__, ep, state_read(&ep->com), ep->com.so,
609 			    ep->com.so->so_state, ep->com.so->so_rcv.sb_cc);
610 		break;
611 	}
612 }
613 
614 static void
615 process_connected(struct c4iw_ep *ep)
616 {
617 
618 	if ((ep->com.so->so_state & SS_ISCONNECTED) && !ep->com.so->so_error)
619 		send_mpa_req(ep);
620 	else {
621 		connect_reply_upcall(ep, -ep->com.so->so_error);
622 		close_socket(&ep->com, 0);
623 		state_set(&ep->com, DEAD);
624 		c4iw_put_ep(&ep->com);
625 	}
626 }
627 
628 static struct socket *
629 dequeue_socket(struct socket *head, struct sockaddr_in **remote,
630     struct c4iw_ep *child_ep)
631 {
632 	struct socket *so;
633 
634 	ACCEPT_LOCK();
635 	so = TAILQ_FIRST(&head->so_comp);
636 	if (!so) {
637 		ACCEPT_UNLOCK();
638 		return (NULL);
639 	}
640 	TAILQ_REMOVE(&head->so_comp, so, so_list);
641 	head->so_qlen--;
642 	SOCK_LOCK(so);
643 	so->so_qstate &= ~SQ_COMP;
644 	so->so_head = NULL;
645 	soref(so);
646 	soupcall_set(so, SO_RCV, c4iw_so_upcall, child_ep);
647 	so->so_state |= SS_NBIO;
648 	SOCK_UNLOCK(so);
649 	ACCEPT_UNLOCK();
650 	soaccept(so, (struct sockaddr **)remote);
651 
652 	return (so);
653 }
654 
655 static void
656 process_newconn(struct c4iw_ep *parent_ep)
657 {
658 	struct socket *child_so;
659 	struct c4iw_ep *child_ep;
660 	struct sockaddr_in *remote;
661 
662 	child_ep = alloc_ep(sizeof(*child_ep), M_NOWAIT);
663 	if (!child_ep) {
664 		CTR3(KTR_IW_CXGBE, "%s: parent so %p, parent ep %p, ENOMEM",
665 		    __func__, parent_ep->com.so, parent_ep);
666 		log(LOG_ERR, "%s: failed to allocate ep entry\n", __func__);
667 		return;
668 	}
669 
670 	child_so = dequeue_socket(parent_ep->com.so, &remote, child_ep);
671 	if (!child_so) {
672 		CTR4(KTR_IW_CXGBE,
673 		    "%s: parent so %p, parent ep %p, child ep %p, dequeue err",
674 		    __func__, parent_ep->com.so, parent_ep, child_ep);
675 		log(LOG_ERR, "%s: failed to dequeue child socket\n", __func__);
676 		__free_ep(&child_ep->com);
677 		return;
678 
679 	}
680 
681 	CTR5(KTR_IW_CXGBE,
682 	    "%s: parent so %p, parent ep %p, child so %p, child ep %p",
683 	     __func__, parent_ep->com.so, parent_ep, child_so, child_ep);
684 
685 	child_ep->com.local_addr = parent_ep->com.local_addr;
686 	child_ep->com.remote_addr = *remote;
687 	child_ep->com.dev = parent_ep->com.dev;
688 	child_ep->com.so = child_so;
689 	child_ep->com.cm_id = NULL;
690 	child_ep->com.thread = parent_ep->com.thread;
691 	child_ep->parent_ep = parent_ep;
692 
693 	free(remote, M_SONAME);
694 	c4iw_get_ep(&parent_ep->com);
695 	child_ep->parent_ep = parent_ep;
696 	init_timer(&child_ep->timer);
697 	state_set(&child_ep->com, MPA_REQ_WAIT);
698 	START_EP_TIMER(child_ep);
699 
700 	/* maybe the request has already been queued up on the socket... */
701 	process_mpa_request(child_ep);
702 }
703 
704 static int
705 c4iw_so_upcall(struct socket *so, void *arg, int waitflag)
706 {
707 	struct c4iw_ep *ep = arg;
708 
709 	spin_lock(&req_lock);
710 
711 	CTR6(KTR_IW_CXGBE,
712 	    "%s: so %p, so_state 0x%x, ep %p, ep_state %s, tqe_prev %p",
713 	    __func__, so, so->so_state, ep, states[ep->com.state],
714 	    ep->com.entry.tqe_prev);
715 
716 	if (ep && ep->com.so && !ep->com.entry.tqe_prev) {
717 		KASSERT(ep->com.so == so, ("%s: XXX review.", __func__));
718 		c4iw_get_ep(&ep->com);
719 		TAILQ_INSERT_TAIL(&req_list, &ep->com, entry);
720 		queue_work(c4iw_taskq, &c4iw_task);
721 	}
722 
723 	spin_unlock(&req_lock);
724 	return (SU_OK);
725 }
726 
727 static void
728 process_socket_event(struct c4iw_ep *ep)
729 {
730 	int state = state_read(&ep->com);
731 	struct socket *so = ep->com.so;
732 
733 	CTR6(KTR_IW_CXGBE, "process_socket_event: so %p, so_state 0x%x, "
734 	    "so_err %d, sb_state 0x%x, ep %p, ep_state %s", so, so->so_state,
735 	    so->so_error, so->so_rcv.sb_state, ep, states[state]);
736 
737 	if (state == CONNECTING) {
738 		process_connected(ep);
739 		return;
740 	}
741 
742 	if (state == LISTEN) {
743 		process_newconn(ep);
744 		return;
745 	}
746 
747 	/* connection error */
748 	if (so->so_error) {
749 		process_conn_error(ep);
750 		return;
751 	}
752 
753 	/* peer close */
754 	if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && state < CLOSING) {
755 		process_peer_close(ep);
756 		return;
757 	}
758 
759 	/* close complete */
760 	if (so->so_state & SS_ISDISCONNECTED) {
761 		process_close_complete(ep);
762 		return;
763 	}
764 
765 	/* rx data */
766 	process_data(ep);
767 }
768 
769 SYSCTL_NODE(_hw, OID_AUTO, iw_cxgbe, CTLFLAG_RD, 0, "iw_cxgbe driver parameters");
770 
771 int db_delay_usecs = 1;
772 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, db_delay_usecs, CTLFLAG_RWTUN, &db_delay_usecs, 0,
773 		"Usecs to delay awaiting db fifo to drain");
774 
775 static int dack_mode = 1;
776 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, dack_mode, CTLFLAG_RWTUN, &dack_mode, 0,
777 		"Delayed ack mode (default = 1)");
778 
779 int c4iw_max_read_depth = 8;
780 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_max_read_depth, CTLFLAG_RWTUN, &c4iw_max_read_depth, 0,
781 		"Per-connection max ORD/IRD (default = 8)");
782 
783 static int enable_tcp_timestamps;
784 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_timestamps, CTLFLAG_RWTUN, &enable_tcp_timestamps, 0,
785 		"Enable tcp timestamps (default = 0)");
786 
787 static int enable_tcp_sack;
788 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_sack, CTLFLAG_RWTUN, &enable_tcp_sack, 0,
789 		"Enable tcp SACK (default = 0)");
790 
791 static int enable_tcp_window_scaling = 1;
792 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_window_scaling, CTLFLAG_RWTUN, &enable_tcp_window_scaling, 0,
793 		"Enable tcp window scaling (default = 1)");
794 
795 int c4iw_debug = 1;
796 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_debug, CTLFLAG_RWTUN, &c4iw_debug, 0,
797 		"Enable debug logging (default = 0)");
798 
799 static int peer2peer;
800 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, peer2peer, CTLFLAG_RWTUN, &peer2peer, 0,
801 		"Support peer2peer ULPs (default = 0)");
802 
803 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
804 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, p2p_type, CTLFLAG_RWTUN, &p2p_type, 0,
805 		"RDMAP opcode to use for the RTR message: 1 = RDMA_READ 0 = RDMA_WRITE (default 1)");
806 
807 static int ep_timeout_secs = 60;
808 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, ep_timeout_secs, CTLFLAG_RWTUN, &ep_timeout_secs, 0,
809 		"CM Endpoint operation timeout in seconds (default = 60)");
810 
811 static int mpa_rev = 1;
812 #ifdef IW_CM_MPAV2
813 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
814 		"MPA Revision, 0 supports amso1100, 1 is RFC0544 spec compliant, 2 is IETF MPA Peer Connect Draft compliant (default = 1)");
815 #else
816 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
817 		"MPA Revision, 0 supports amso1100, 1 is RFC0544 spec compliant (default = 1)");
818 #endif
819 
820 static int markers_enabled;
821 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, markers_enabled, CTLFLAG_RWTUN, &markers_enabled, 0,
822 		"Enable MPA MARKERS (default(0) = disabled)");
823 
824 static int crc_enabled = 1;
825 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, crc_enabled, CTLFLAG_RWTUN, &crc_enabled, 0,
826 		"Enable MPA CRC (default(1) = enabled)");
827 
828 static int rcv_win = 256 * 1024;
829 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, rcv_win, CTLFLAG_RWTUN, &rcv_win, 0,
830 		"TCP receive window in bytes (default = 256KB)");
831 
832 static int snd_win = 128 * 1024;
833 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, snd_win, CTLFLAG_RWTUN, &snd_win, 0,
834 		"TCP send window in bytes (default = 128KB)");
835 
836 int db_fc_threshold = 2000;
837 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, db_fc_threshold, CTLFLAG_RWTUN, &db_fc_threshold, 0,
838 		"QP count/threshold that triggers automatic");
839 
840 static void
841 start_ep_timer(struct c4iw_ep *ep)
842 {
843 
844 	if (timer_pending(&ep->timer)) {
845 		CTR2(KTR_IW_CXGBE, "%s: ep %p, already started", __func__, ep);
846 		printk(KERN_ERR "%s timer already started! ep %p\n", __func__,
847 		    ep);
848 		return;
849 	}
850 	clear_bit(TIMEOUT, &ep->com.flags);
851 	c4iw_get_ep(&ep->com);
852 	ep->timer.expires = jiffies + ep_timeout_secs * HZ;
853 	ep->timer.data = (unsigned long)ep;
854 	ep->timer.function = ep_timeout;
855 	add_timer(&ep->timer);
856 }
857 
858 static void
859 stop_ep_timer(struct c4iw_ep *ep)
860 {
861 
862 	del_timer_sync(&ep->timer);
863 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
864 		c4iw_put_ep(&ep->com);
865 	}
866 }
867 
868 static enum
869 c4iw_ep_state state_read(struct c4iw_ep_common *epc)
870 {
871 	enum c4iw_ep_state state;
872 
873 	mutex_lock(&epc->mutex);
874 	state = epc->state;
875 	mutex_unlock(&epc->mutex);
876 
877 	return (state);
878 }
879 
880 static void
881 __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
882 {
883 
884 	epc->state = new;
885 }
886 
887 static void
888 state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
889 {
890 
891 	mutex_lock(&epc->mutex);
892 	__state_set(epc, new);
893 	mutex_unlock(&epc->mutex);
894 }
895 
896 static void *
897 alloc_ep(int size, gfp_t gfp)
898 {
899 	struct c4iw_ep_common *epc;
900 
901 	epc = kzalloc(size, gfp);
902 	if (epc == NULL)
903 		return (NULL);
904 
905 	kref_init(&epc->kref);
906 	mutex_init(&epc->mutex);
907 	c4iw_init_wr_wait(&epc->wr_wait);
908 
909 	return (epc);
910 }
911 
912 void
913 __free_ep(struct c4iw_ep_common *epc)
914 {
915 	CTR2(KTR_IW_CXGBE, "%s:feB %p", __func__, epc);
916 	KASSERT(!epc->so, ("%s warning ep->so %p \n", __func__, epc->so));
917 	KASSERT(!epc->entry.tqe_prev, ("%s epc %p still on req list!\n", __func__, epc));
918 	free(epc, M_DEVBUF);
919 	CTR2(KTR_IW_CXGBE, "%s:feE %p", __func__, epc);
920 }
921 
922 void _c4iw_free_ep(struct kref *kref)
923 {
924 	struct c4iw_ep *ep;
925 	struct c4iw_ep_common *epc;
926 
927 	ep = container_of(kref, struct c4iw_ep, com.kref);
928 	epc = &ep->com;
929 	KASSERT(!epc->so, ("%s ep->so %p", __func__, epc->so));
930 	KASSERT(!epc->entry.tqe_prev, ("%s epc %p still on req list",
931 	    __func__, epc));
932 	kfree(ep);
933 }
934 
935 static void release_ep_resources(struct c4iw_ep *ep)
936 {
937 	CTR2(KTR_IW_CXGBE, "%s:rerB %p", __func__, ep);
938 	set_bit(RELEASE_RESOURCES, &ep->com.flags);
939 	c4iw_put_ep(&ep->com);
940 	CTR2(KTR_IW_CXGBE, "%s:rerE %p", __func__, ep);
941 }
942 
943 static void
944 send_mpa_req(struct c4iw_ep *ep)
945 {
946 	int mpalen;
947 	struct mpa_message *mpa;
948 	struct mpa_v2_conn_params mpa_v2_params;
949 	struct mbuf *m;
950 	char mpa_rev_to_use = mpa_rev;
951 	int err;
952 
953 	if (ep->retry_with_mpa_v1)
954 		mpa_rev_to_use = 1;
955 	mpalen = sizeof(*mpa) + ep->plen;
956 	if (mpa_rev_to_use == 2)
957 		mpalen += sizeof(struct mpa_v2_conn_params);
958 
959 	if (mpalen > MHLEN)
960 		CXGBE_UNIMPLEMENTED(__func__);
961 
962 	m = m_gethdr(M_NOWAIT, MT_DATA);
963 	if (m == NULL) {
964 		connect_reply_upcall(ep, -ENOMEM);
965 		return;
966 	}
967 
968 	mpa = mtod(m, struct mpa_message *);
969 	m->m_len = mpalen;
970 	m->m_pkthdr.len = mpalen;
971 	memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
972 	mpa->flags = (crc_enabled ? MPA_CRC : 0) |
973 		(markers_enabled ? MPA_MARKERS : 0) |
974 		(mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
975 	mpa->private_data_size = htons(ep->plen);
976 	mpa->revision = mpa_rev_to_use;
977 
978 	if (mpa_rev_to_use == 1) {
979 		ep->tried_with_mpa_v1 = 1;
980 		ep->retry_with_mpa_v1 = 0;
981 	}
982 
983 	if (mpa_rev_to_use == 2) {
984 		mpa->private_data_size +=
985 			htons(sizeof(struct mpa_v2_conn_params));
986 		mpa_v2_params.ird = htons((u16)ep->ird);
987 		mpa_v2_params.ord = htons((u16)ep->ord);
988 
989 		if (peer2peer) {
990 			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
991 
992 			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
993 				mpa_v2_params.ord |=
994 				    htons(MPA_V2_RDMA_WRITE_RTR);
995 			} else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
996 				mpa_v2_params.ord |=
997 					htons(MPA_V2_RDMA_READ_RTR);
998 			}
999 		}
1000 		memcpy(mpa->private_data, &mpa_v2_params,
1001 			sizeof(struct mpa_v2_conn_params));
1002 
1003 		if (ep->plen) {
1004 
1005 			memcpy(mpa->private_data +
1006 				sizeof(struct mpa_v2_conn_params),
1007 				ep->mpa_pkt + sizeof(*mpa), ep->plen);
1008 		}
1009 	} else {
1010 
1011 		if (ep->plen)
1012 			memcpy(mpa->private_data,
1013 					ep->mpa_pkt + sizeof(*mpa), ep->plen);
1014 		CTR2(KTR_IW_CXGBE, "%s:smr7 %p", __func__, ep);
1015 	}
1016 
1017 	err = sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT, ep->com.thread);
1018 	if (err) {
1019 		connect_reply_upcall(ep, -ENOMEM);
1020 		return;
1021 	}
1022 
1023 	START_EP_TIMER(ep);
1024 	state_set(&ep->com, MPA_REQ_SENT);
1025 	ep->mpa_attr.initiator = 1;
1026 }
1027 
1028 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
1029 {
1030 	int mpalen ;
1031 	struct mpa_message *mpa;
1032 	struct mpa_v2_conn_params mpa_v2_params;
1033 	struct mbuf *m;
1034 	int err;
1035 
1036 	CTR4(KTR_IW_CXGBE, "%s:smrejB %p %u %d", __func__, ep, ep->hwtid,
1037 	    ep->plen);
1038 
1039 	mpalen = sizeof(*mpa) + plen;
1040 
1041 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1042 
1043 		mpalen += sizeof(struct mpa_v2_conn_params);
1044 		CTR4(KTR_IW_CXGBE, "%s:smrej1 %p %u %d", __func__, ep,
1045 		    ep->mpa_attr.version, mpalen);
1046 	}
1047 
1048 	if (mpalen > MHLEN)
1049 		CXGBE_UNIMPLEMENTED(__func__);
1050 
1051 	m = m_gethdr(M_NOWAIT, MT_DATA);
1052 	if (m == NULL) {
1053 
1054 		printf("%s - cannot alloc mbuf!\n", __func__);
1055 		CTR2(KTR_IW_CXGBE, "%s:smrej2 %p", __func__, ep);
1056 		return (-ENOMEM);
1057 	}
1058 
1059 
1060 	mpa = mtod(m, struct mpa_message *);
1061 	m->m_len = mpalen;
1062 	m->m_pkthdr.len = mpalen;
1063 	memset(mpa, 0, sizeof(*mpa));
1064 	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1065 	mpa->flags = MPA_REJECT;
1066 	mpa->revision = mpa_rev;
1067 	mpa->private_data_size = htons(plen);
1068 
1069 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1070 
1071 		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1072 		mpa->private_data_size +=
1073 			htons(sizeof(struct mpa_v2_conn_params));
1074 		mpa_v2_params.ird = htons(((u16)ep->ird) |
1075 				(peer2peer ? MPA_V2_PEER2PEER_MODEL :
1076 				 0));
1077 		mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
1078 					(p2p_type ==
1079 					 FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
1080 					 MPA_V2_RDMA_WRITE_RTR : p2p_type ==
1081 					 FW_RI_INIT_P2PTYPE_READ_REQ ?
1082 					 MPA_V2_RDMA_READ_RTR : 0) : 0));
1083 		memcpy(mpa->private_data, &mpa_v2_params,
1084 				sizeof(struct mpa_v2_conn_params));
1085 
1086 		if (ep->plen)
1087 			memcpy(mpa->private_data +
1088 					sizeof(struct mpa_v2_conn_params), pdata, plen);
1089 		CTR5(KTR_IW_CXGBE, "%s:smrej3 %p %d %d %d", __func__, ep,
1090 		    mpa_v2_params.ird, mpa_v2_params.ord, ep->plen);
1091 	} else
1092 		if (plen)
1093 			memcpy(mpa->private_data, pdata, plen);
1094 
1095 	err = sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT, ep->com.thread);
1096 	if (!err)
1097 		ep->snd_seq += mpalen;
1098 	CTR4(KTR_IW_CXGBE, "%s:smrejE %p %u %d", __func__, ep, ep->hwtid, err);
1099 	return err;
1100 }
1101 
1102 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
1103 {
1104 	int mpalen;
1105 	struct mpa_message *mpa;
1106 	struct mbuf *m;
1107 	struct mpa_v2_conn_params mpa_v2_params;
1108 	int err;
1109 
1110 	CTR2(KTR_IW_CXGBE, "%s:smrepB %p", __func__, ep);
1111 
1112 	mpalen = sizeof(*mpa) + plen;
1113 
1114 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1115 
1116 		CTR3(KTR_IW_CXGBE, "%s:smrep1 %p %d", __func__, ep,
1117 		    ep->mpa_attr.version);
1118 		mpalen += sizeof(struct mpa_v2_conn_params);
1119 	}
1120 
1121 	if (mpalen > MHLEN)
1122 		CXGBE_UNIMPLEMENTED(__func__);
1123 
1124 	m = m_gethdr(M_NOWAIT, MT_DATA);
1125 	if (m == NULL) {
1126 
1127 		CTR2(KTR_IW_CXGBE, "%s:smrep2 %p", __func__, ep);
1128 		printf("%s - cannot alloc mbuf!\n", __func__);
1129 		return (-ENOMEM);
1130 	}
1131 
1132 
1133 	mpa = mtod(m, struct mpa_message *);
1134 	m->m_len = mpalen;
1135 	m->m_pkthdr.len = mpalen;
1136 	memset(mpa, 0, sizeof(*mpa));
1137 	memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1138 	mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
1139 		(markers_enabled ? MPA_MARKERS : 0);
1140 	mpa->revision = ep->mpa_attr.version;
1141 	mpa->private_data_size = htons(plen);
1142 
1143 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1144 
1145 		mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1146 		mpa->private_data_size +=
1147 			htons(sizeof(struct mpa_v2_conn_params));
1148 		mpa_v2_params.ird = htons((u16)ep->ird);
1149 		mpa_v2_params.ord = htons((u16)ep->ord);
1150 		CTR5(KTR_IW_CXGBE, "%s:smrep3 %p %d %d %d", __func__, ep,
1151 		    ep->mpa_attr.version, mpa_v2_params.ird, mpa_v2_params.ord);
1152 
1153 		if (peer2peer && (ep->mpa_attr.p2p_type !=
1154 			FW_RI_INIT_P2PTYPE_DISABLED)) {
1155 
1156 			mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1157 
1158 			if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
1159 
1160 				mpa_v2_params.ord |=
1161 					htons(MPA_V2_RDMA_WRITE_RTR);
1162 				CTR5(KTR_IW_CXGBE, "%s:smrep4 %p %d %d %d",
1163 				    __func__, ep, p2p_type, mpa_v2_params.ird,
1164 				    mpa_v2_params.ord);
1165 			}
1166 			else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
1167 
1168 				mpa_v2_params.ord |=
1169 					htons(MPA_V2_RDMA_READ_RTR);
1170 				CTR5(KTR_IW_CXGBE, "%s:smrep5 %p %d %d %d",
1171 				    __func__, ep, p2p_type, mpa_v2_params.ird,
1172 				    mpa_v2_params.ord);
1173 			}
1174 		}
1175 
1176 		memcpy(mpa->private_data, &mpa_v2_params,
1177 			sizeof(struct mpa_v2_conn_params));
1178 
1179 		if (ep->plen)
1180 			memcpy(mpa->private_data +
1181 				sizeof(struct mpa_v2_conn_params), pdata, plen);
1182 	} else
1183 		if (plen)
1184 			memcpy(mpa->private_data, pdata, plen);
1185 
1186 	state_set(&ep->com, MPA_REP_SENT);
1187 	ep->snd_seq += mpalen;
1188 	err = sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT,
1189 			ep->com.thread);
1190 	CTR3(KTR_IW_CXGBE, "%s:smrepE %p %d", __func__, ep, err);
1191 	return err;
1192 }
1193 
1194 
1195 
1196 static void close_complete_upcall(struct c4iw_ep *ep)
1197 {
1198 	struct iw_cm_event event;
1199 
1200 	CTR2(KTR_IW_CXGBE, "%s:ccuB %p", __func__, ep);
1201 	memset(&event, 0, sizeof(event));
1202 	event.event = IW_CM_EVENT_CLOSE;
1203 
1204 	if (ep->com.cm_id) {
1205 
1206 		CTR2(KTR_IW_CXGBE, "%s:ccu1 %1", __func__, ep);
1207 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1208 		ep->com.cm_id->rem_ref(ep->com.cm_id);
1209 		ep->com.cm_id = NULL;
1210 		ep->com.qp = NULL;
1211 		set_bit(CLOSE_UPCALL, &ep->com.history);
1212 	}
1213 	CTR2(KTR_IW_CXGBE, "%s:ccuE %p", __func__, ep);
1214 }
1215 
1216 static int abort_connection(struct c4iw_ep *ep)
1217 {
1218 	int err;
1219 
1220 	CTR2(KTR_IW_CXGBE, "%s:abB %p", __func__, ep);
1221 	close_complete_upcall(ep);
1222 	state_set(&ep->com, ABORTING);
1223 	abort_socket(ep);
1224 	err = close_socket(&ep->com, 0);
1225 	set_bit(ABORT_CONN, &ep->com.history);
1226 	CTR2(KTR_IW_CXGBE, "%s:abE %p", __func__, ep);
1227 	return err;
1228 }
1229 
1230 static void peer_close_upcall(struct c4iw_ep *ep)
1231 {
1232 	struct iw_cm_event event;
1233 
1234 	CTR2(KTR_IW_CXGBE, "%s:pcuB %p", __func__, ep);
1235 	memset(&event, 0, sizeof(event));
1236 	event.event = IW_CM_EVENT_DISCONNECT;
1237 
1238 	if (ep->com.cm_id) {
1239 
1240 		CTR2(KTR_IW_CXGBE, "%s:pcu1 %p", __func__, ep);
1241 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1242 		set_bit(DISCONN_UPCALL, &ep->com.history);
1243 	}
1244 	CTR2(KTR_IW_CXGBE, "%s:pcuE %p", __func__, ep);
1245 }
1246 
1247 static void peer_abort_upcall(struct c4iw_ep *ep)
1248 {
1249 	struct iw_cm_event event;
1250 
1251 	CTR2(KTR_IW_CXGBE, "%s:pauB %p", __func__, ep);
1252 	memset(&event, 0, sizeof(event));
1253 	event.event = IW_CM_EVENT_CLOSE;
1254 	event.status = -ECONNRESET;
1255 
1256 	if (ep->com.cm_id) {
1257 
1258 		CTR2(KTR_IW_CXGBE, "%s:pau1 %p", __func__, ep);
1259 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1260 		ep->com.cm_id->rem_ref(ep->com.cm_id);
1261 		ep->com.cm_id = NULL;
1262 		ep->com.qp = NULL;
1263 		set_bit(ABORT_UPCALL, &ep->com.history);
1264 	}
1265 	CTR2(KTR_IW_CXGBE, "%s:pauE %p", __func__, ep);
1266 }
1267 
1268 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
1269 {
1270 	struct iw_cm_event event;
1271 
1272 	CTR3(KTR_IW_CXGBE, "%s:cruB %p", __func__, ep, status);
1273 	memset(&event, 0, sizeof(event));
1274 	event.event = IW_CM_EVENT_CONNECT_REPLY;
1275 	event.status = (status ==-ECONNABORTED)?-ECONNRESET: status;
1276 	event.local_addr = ep->com.local_addr;
1277 	event.remote_addr = ep->com.remote_addr;
1278 
1279 	if ((status == 0) || (status == -ECONNREFUSED)) {
1280 
1281 		if (!ep->tried_with_mpa_v1) {
1282 
1283 			CTR2(KTR_IW_CXGBE, "%s:cru1 %p", __func__, ep);
1284 			/* this means MPA_v2 is used */
1285 			event.private_data_len = ep->plen -
1286 				sizeof(struct mpa_v2_conn_params);
1287 			event.private_data = ep->mpa_pkt +
1288 				sizeof(struct mpa_message) +
1289 				sizeof(struct mpa_v2_conn_params);
1290 		} else {
1291 
1292 			CTR2(KTR_IW_CXGBE, "%s:cru2 %p", __func__, ep);
1293 			/* this means MPA_v1 is used */
1294 			event.private_data_len = ep->plen;
1295 			event.private_data = ep->mpa_pkt +
1296 				sizeof(struct mpa_message);
1297 		}
1298 	}
1299 
1300 	if (ep->com.cm_id) {
1301 
1302 		CTR2(KTR_IW_CXGBE, "%s:cru3 %p", __func__, ep);
1303 		set_bit(CONN_RPL_UPCALL, &ep->com.history);
1304 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1305 	}
1306 
1307 	if(status == -ECONNABORTED) {
1308 
1309 		CTR3(KTR_IW_CXGBE, "%s:cruE %p %d", __func__, ep, status);
1310 		return;
1311 	}
1312 
1313 	if (status < 0) {
1314 
1315 		CTR3(KTR_IW_CXGBE, "%s:cru4 %p %d", __func__, ep, status);
1316 		ep->com.cm_id->rem_ref(ep->com.cm_id);
1317 		ep->com.cm_id = NULL;
1318 		ep->com.qp = NULL;
1319 	}
1320 
1321 	CTR2(KTR_IW_CXGBE, "%s:cruE %p", __func__, ep);
1322 }
1323 
1324 static void connect_request_upcall(struct c4iw_ep *ep)
1325 {
1326 	struct iw_cm_event event;
1327 
1328 	CTR3(KTR_IW_CXGBE, "%s: ep %p, mpa_v1 %d", __func__, ep,
1329 	    ep->tried_with_mpa_v1);
1330 
1331 	memset(&event, 0, sizeof(event));
1332 	event.event = IW_CM_EVENT_CONNECT_REQUEST;
1333 	event.local_addr = ep->com.local_addr;
1334 	event.remote_addr = ep->com.remote_addr;
1335 	event.provider_data = ep;
1336 	event.so = ep->com.so;
1337 
1338 	if (!ep->tried_with_mpa_v1) {
1339 		/* this means MPA_v2 is used */
1340 #ifdef IW_CM_MPAV2
1341 		event.ord = ep->ord;
1342 		event.ird = ep->ird;
1343 #endif
1344 		event.private_data_len = ep->plen -
1345 			sizeof(struct mpa_v2_conn_params);
1346 		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
1347 			sizeof(struct mpa_v2_conn_params);
1348 	} else {
1349 
1350 		/* this means MPA_v1 is used. Send max supported */
1351 #ifdef IW_CM_MPAV2
1352 		event.ord = c4iw_max_read_depth;
1353 		event.ird = c4iw_max_read_depth;
1354 #endif
1355 		event.private_data_len = ep->plen;
1356 		event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
1357 	}
1358 
1359 	c4iw_get_ep(&ep->com);
1360 	ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
1361 	    &event);
1362 	set_bit(CONNREQ_UPCALL, &ep->com.history);
1363 	c4iw_put_ep(&ep->parent_ep->com);
1364 }
1365 
1366 static void established_upcall(struct c4iw_ep *ep)
1367 {
1368 	struct iw_cm_event event;
1369 
1370 	CTR2(KTR_IW_CXGBE, "%s:euB %p", __func__, ep);
1371 	memset(&event, 0, sizeof(event));
1372 	event.event = IW_CM_EVENT_ESTABLISHED;
1373 #ifdef IW_CM_MPAV2
1374 	event.ird = ep->ird;
1375 	event.ord = ep->ord;
1376 #endif
1377 	if (ep->com.cm_id) {
1378 
1379 		CTR2(KTR_IW_CXGBE, "%s:eu1 %p", __func__, ep);
1380 		ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1381 		set_bit(ESTAB_UPCALL, &ep->com.history);
1382 	}
1383 	CTR2(KTR_IW_CXGBE, "%s:euE %p", __func__, ep);
1384 }
1385 
1386 
1387 
1388 static void process_mpa_reply(struct c4iw_ep *ep)
1389 {
1390 	struct mpa_message *mpa;
1391 	struct mpa_v2_conn_params *mpa_v2_params;
1392 	u16 plen;
1393 	u16 resp_ird, resp_ord;
1394 	u8 rtr_mismatch = 0, insuff_ird = 0;
1395 	struct c4iw_qp_attributes attrs;
1396 	enum c4iw_qp_attr_mask mask;
1397 	int err;
1398 	struct mbuf *top, *m;
1399 	int flags = MSG_DONTWAIT;
1400 	struct uio uio;
1401 
1402 	CTR2(KTR_IW_CXGBE, "%s:pmrB %p", __func__, ep);
1403 
1404 	/*
1405 	 * Stop mpa timer.  If it expired, then the state has
1406 	 * changed and we bail since ep_timeout already aborted
1407 	 * the connection.
1408 	 */
1409 	STOP_EP_TIMER(ep);
1410 	if (state_read(&ep->com) != MPA_REQ_SENT)
1411 		return;
1412 
1413 	uio.uio_resid = 1000000;
1414 	uio.uio_td = ep->com.thread;
1415 	err = soreceive(ep->com.so, NULL, &uio, &top, NULL, &flags);
1416 
1417 	if (err) {
1418 
1419 		if (err == EWOULDBLOCK) {
1420 
1421 			CTR2(KTR_IW_CXGBE, "%s:pmr1 %p", __func__, ep);
1422 			START_EP_TIMER(ep);
1423 			return;
1424 		}
1425 		err = -err;
1426 		CTR2(KTR_IW_CXGBE, "%s:pmr2 %p", __func__, ep);
1427 		goto err;
1428 	}
1429 
1430 	if (ep->com.so->so_rcv.sb_mb) {
1431 
1432 		CTR2(KTR_IW_CXGBE, "%s:pmr3 %p", __func__, ep);
1433 		printf("%s data after soreceive called! so %p sb_mb %p top %p\n",
1434 		       __func__, ep->com.so, ep->com.so->so_rcv.sb_mb, top);
1435 	}
1436 
1437 	m = top;
1438 
1439 	do {
1440 
1441 		CTR2(KTR_IW_CXGBE, "%s:pmr4 %p", __func__, ep);
1442 		/*
1443 		 * If we get more than the supported amount of private data
1444 		 * then we must fail this connection.
1445 		 */
1446 		if (ep->mpa_pkt_len + m->m_len > sizeof(ep->mpa_pkt)) {
1447 
1448 			CTR3(KTR_IW_CXGBE, "%s:pmr5 %p %d", __func__, ep,
1449 			    ep->mpa_pkt_len + m->m_len);
1450 			err = (-EINVAL);
1451 			goto err;
1452 		}
1453 
1454 		/*
1455 		 * copy the new data into our accumulation buffer.
1456 		 */
1457 		m_copydata(m, 0, m->m_len, &(ep->mpa_pkt[ep->mpa_pkt_len]));
1458 		ep->mpa_pkt_len += m->m_len;
1459 		if (!m->m_next)
1460 			m = m->m_nextpkt;
1461 		else
1462 			m = m->m_next;
1463 	} while (m);
1464 
1465 	m_freem(top);
1466 	/*
1467 	 * if we don't even have the mpa message, then bail.
1468 	 */
1469 	if (ep->mpa_pkt_len < sizeof(*mpa))
1470 		return;
1471 	mpa = (struct mpa_message *) ep->mpa_pkt;
1472 
1473 	/* Validate MPA header. */
1474 	if (mpa->revision > mpa_rev) {
1475 
1476 		CTR4(KTR_IW_CXGBE, "%s:pmr6 %p %d %d", __func__, ep,
1477 		    mpa->revision, mpa_rev);
1478 		printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d, "
1479 				" Received = %d\n", __func__, mpa_rev, mpa->revision);
1480 		err = -EPROTO;
1481 		goto err;
1482 	}
1483 
1484 	if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1485 
1486 		CTR2(KTR_IW_CXGBE, "%s:pmr7 %p", __func__, ep);
1487 		err = -EPROTO;
1488 		goto err;
1489 	}
1490 
1491 	plen = ntohs(mpa->private_data_size);
1492 
1493 	/*
1494 	 * Fail if there's too much private data.
1495 	 */
1496 	if (plen > MPA_MAX_PRIVATE_DATA) {
1497 
1498 		CTR2(KTR_IW_CXGBE, "%s:pmr8 %p", __func__, ep);
1499 		err = -EPROTO;
1500 		goto err;
1501 	}
1502 
1503 	/*
1504 	 * If plen does not account for pkt size
1505 	 */
1506 	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1507 
1508 		CTR2(KTR_IW_CXGBE, "%s:pmr9 %p", __func__, ep);
1509 		err = -EPROTO;
1510 		goto err;
1511 	}
1512 
1513 	ep->plen = (u8) plen;
1514 
1515 	/*
1516 	 * If we don't have all the pdata yet, then bail.
1517 	 * We'll continue process when more data arrives.
1518 	 */
1519 	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen)) {
1520 
1521 		CTR2(KTR_IW_CXGBE, "%s:pmra %p", __func__, ep);
1522 		return;
1523 	}
1524 
1525 	if (mpa->flags & MPA_REJECT) {
1526 
1527 		CTR2(KTR_IW_CXGBE, "%s:pmrb %p", __func__, ep);
1528 		err = -ECONNREFUSED;
1529 		goto err;
1530 	}
1531 
1532 	/*
1533 	 * If we get here we have accumulated the entire mpa
1534 	 * start reply message including private data. And
1535 	 * the MPA header is valid.
1536 	 */
1537 	state_set(&ep->com, FPDU_MODE);
1538 	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1539 	ep->mpa_attr.recv_marker_enabled = markers_enabled;
1540 	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1541 	ep->mpa_attr.version = mpa->revision;
1542 	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1543 
1544 	if (mpa->revision == 2) {
1545 
1546 		CTR2(KTR_IW_CXGBE, "%s:pmrc %p", __func__, ep);
1547 		ep->mpa_attr.enhanced_rdma_conn =
1548 			mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1549 
1550 		if (ep->mpa_attr.enhanced_rdma_conn) {
1551 
1552 			CTR2(KTR_IW_CXGBE, "%s:pmrd %p", __func__, ep);
1553 			mpa_v2_params = (struct mpa_v2_conn_params *)
1554 				(ep->mpa_pkt + sizeof(*mpa));
1555 			resp_ird = ntohs(mpa_v2_params->ird) &
1556 				MPA_V2_IRD_ORD_MASK;
1557 			resp_ord = ntohs(mpa_v2_params->ord) &
1558 				MPA_V2_IRD_ORD_MASK;
1559 
1560 			/*
1561 			 * This is a double-check. Ideally, below checks are
1562 			 * not required since ird/ord stuff has been taken
1563 			 * care of in c4iw_accept_cr
1564 			 */
1565 			if ((ep->ird < resp_ord) || (ep->ord > resp_ird)) {
1566 
1567 				CTR2(KTR_IW_CXGBE, "%s:pmre %p", __func__, ep);
1568 				err = -ENOMEM;
1569 				ep->ird = resp_ord;
1570 				ep->ord = resp_ird;
1571 				insuff_ird = 1;
1572 			}
1573 
1574 			if (ntohs(mpa_v2_params->ird) &
1575 				MPA_V2_PEER2PEER_MODEL) {
1576 
1577 				CTR2(KTR_IW_CXGBE, "%s:pmrf %p", __func__, ep);
1578 				if (ntohs(mpa_v2_params->ord) &
1579 					MPA_V2_RDMA_WRITE_RTR) {
1580 
1581 					CTR2(KTR_IW_CXGBE, "%s:pmrg %p", __func__, ep);
1582 					ep->mpa_attr.p2p_type =
1583 						FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1584 				}
1585 				else if (ntohs(mpa_v2_params->ord) &
1586 					MPA_V2_RDMA_READ_RTR) {
1587 
1588 					CTR2(KTR_IW_CXGBE, "%s:pmrh %p", __func__, ep);
1589 					ep->mpa_attr.p2p_type =
1590 						FW_RI_INIT_P2PTYPE_READ_REQ;
1591 				}
1592 			}
1593 		}
1594 	} else {
1595 
1596 		CTR2(KTR_IW_CXGBE, "%s:pmri %p", __func__, ep);
1597 
1598 		if (mpa->revision == 1) {
1599 
1600 			CTR2(KTR_IW_CXGBE, "%s:pmrj %p", __func__, ep);
1601 
1602 			if (peer2peer) {
1603 
1604 				CTR2(KTR_IW_CXGBE, "%s:pmrk %p", __func__, ep);
1605 				ep->mpa_attr.p2p_type = p2p_type;
1606 			}
1607 		}
1608 	}
1609 
1610 	if (set_tcpinfo(ep)) {
1611 
1612 		CTR2(KTR_IW_CXGBE, "%s:pmrl %p", __func__, ep);
1613 		printf("%s set_tcpinfo error\n", __func__);
1614 		goto err;
1615 	}
1616 
1617 	CTR6(KTR_IW_CXGBE, "%s - crc_enabled = %d, recv_marker_enabled = %d, "
1618 	    "xmit_marker_enabled = %d, version = %d p2p_type = %d", __func__,
1619 	    ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1620 	    ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1621 	    ep->mpa_attr.p2p_type);
1622 
1623 	/*
1624 	 * If responder's RTR does not match with that of initiator, assign
1625 	 * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1626 	 * generated when moving QP to RTS state.
1627 	 * A TERM message will be sent after QP has moved to RTS state
1628 	 */
1629 	if ((ep->mpa_attr.version == 2) && peer2peer &&
1630 		(ep->mpa_attr.p2p_type != p2p_type)) {
1631 
1632 		CTR2(KTR_IW_CXGBE, "%s:pmrm %p", __func__, ep);
1633 		ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1634 		rtr_mismatch = 1;
1635 	}
1636 
1637 
1638 	//ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
1639 	attrs.mpa_attr = ep->mpa_attr;
1640 	attrs.max_ird = ep->ird;
1641 	attrs.max_ord = ep->ord;
1642 	attrs.llp_stream_handle = ep;
1643 	attrs.next_state = C4IW_QP_STATE_RTS;
1644 
1645 	mask = C4IW_QP_ATTR_NEXT_STATE |
1646 		C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1647 		C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1648 
1649 	/* bind QP and TID with INIT_WR */
1650 	err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
1651 
1652 	if (err) {
1653 
1654 		CTR2(KTR_IW_CXGBE, "%s:pmrn %p", __func__, ep);
1655 		goto err;
1656 	}
1657 
1658 	/*
1659 	 * If responder's RTR requirement did not match with what initiator
1660 	 * supports, generate TERM message
1661 	 */
1662 	if (rtr_mismatch) {
1663 
1664 		CTR2(KTR_IW_CXGBE, "%s:pmro %p", __func__, ep);
1665 		printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
1666 		attrs.layer_etype = LAYER_MPA | DDP_LLP;
1667 		attrs.ecode = MPA_NOMATCH_RTR;
1668 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1669 		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1670 			C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1671 		err = -ENOMEM;
1672 		goto out;
1673 	}
1674 
1675 	/*
1676 	 * Generate TERM if initiator IRD is not sufficient for responder
1677 	 * provided ORD. Currently, we do the same behaviour even when
1678 	 * responder provided IRD is also not sufficient as regards to
1679 	 * initiator ORD.
1680 	 */
1681 	if (insuff_ird) {
1682 
1683 		CTR2(KTR_IW_CXGBE, "%s:pmrp %p", __func__, ep);
1684 		printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
1685 				__func__);
1686 		attrs.layer_etype = LAYER_MPA | DDP_LLP;
1687 		attrs.ecode = MPA_INSUFF_IRD;
1688 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
1689 		err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1690 			C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1691 		err = -ENOMEM;
1692 		goto out;
1693 	}
1694 	goto out;
1695 err:
1696 	state_set(&ep->com, ABORTING);
1697 	abort_connection(ep);
1698 out:
1699 	connect_reply_upcall(ep, err);
1700 	CTR2(KTR_IW_CXGBE, "%s:pmrE %p", __func__, ep);
1701 	return;
1702 }
1703 
1704 static void
1705 process_mpa_request(struct c4iw_ep *ep)
1706 {
1707 	struct mpa_message *mpa;
1708 	u16 plen;
1709 	int flags = MSG_DONTWAIT;
1710 	int rc;
1711 	struct iovec iov;
1712 	struct uio uio;
1713 	enum c4iw_ep_state state = state_read(&ep->com);
1714 
1715 	CTR3(KTR_IW_CXGBE, "%s: ep %p, state %s", __func__, ep, states[state]);
1716 
1717 	if (state != MPA_REQ_WAIT)
1718 		return;
1719 
1720 	iov.iov_base = &ep->mpa_pkt[ep->mpa_pkt_len];
1721 	iov.iov_len = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
1722 	uio.uio_iov = &iov;
1723 	uio.uio_iovcnt = 1;
1724 	uio.uio_offset = 0;
1725 	uio.uio_resid = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
1726 	uio.uio_segflg = UIO_SYSSPACE;
1727 	uio.uio_rw = UIO_READ;
1728 	uio.uio_td = NULL; /* uio.uio_td = ep->com.thread; */
1729 
1730 	rc = soreceive(ep->com.so, NULL, &uio, NULL, NULL, &flags);
1731 	if (rc == EAGAIN)
1732 		return;
1733 	else if (rc) {
1734 abort:
1735 		STOP_EP_TIMER(ep);
1736 		abort_connection(ep);
1737 		return;
1738 	}
1739 	KASSERT(uio.uio_offset > 0, ("%s: sorecieve on so %p read no data",
1740 	    __func__, ep->com.so));
1741 	ep->mpa_pkt_len += uio.uio_offset;
1742 
1743 	/*
1744 	 * If we get more than the supported amount of private data then we must
1745 	 * fail this connection.  XXX: check so_rcv->sb_cc, or peek with another
1746 	 * soreceive, or increase the size of mpa_pkt by 1 and abort if the last
1747 	 * byte is filled by the soreceive above.
1748 	 */
1749 
1750 	/* Don't even have the MPA message.  Wait for more data to arrive. */
1751 	if (ep->mpa_pkt_len < sizeof(*mpa))
1752 		return;
1753 	mpa = (struct mpa_message *) ep->mpa_pkt;
1754 
1755 	/*
1756 	 * Validate MPA Header.
1757 	 */
1758 	if (mpa->revision > mpa_rev) {
1759 		log(LOG_ERR, "%s: MPA version mismatch. Local = %d,"
1760 		    " Received = %d\n", __func__, mpa_rev, mpa->revision);
1761 		goto abort;
1762 	}
1763 
1764 	if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
1765 		goto abort;
1766 
1767 	/*
1768 	 * Fail if there's too much private data.
1769 	 */
1770 	plen = ntohs(mpa->private_data_size);
1771 	if (plen > MPA_MAX_PRIVATE_DATA)
1772 		goto abort;
1773 
1774 	/*
1775 	 * If plen does not account for pkt size
1776 	 */
1777 	if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
1778 		goto abort;
1779 
1780 	ep->plen = (u8) plen;
1781 
1782 	/*
1783 	 * If we don't have all the pdata yet, then bail.
1784 	 */
1785 	if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1786 		return;
1787 
1788 	/*
1789 	 * If we get here we have accumulated the entire mpa
1790 	 * start reply message including private data.
1791 	 */
1792 	ep->mpa_attr.initiator = 0;
1793 	ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1794 	ep->mpa_attr.recv_marker_enabled = markers_enabled;
1795 	ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1796 	ep->mpa_attr.version = mpa->revision;
1797 	if (mpa->revision == 1)
1798 		ep->tried_with_mpa_v1 = 1;
1799 	ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1800 
1801 	if (mpa->revision == 2) {
1802 		ep->mpa_attr.enhanced_rdma_conn =
1803 		    mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1804 		if (ep->mpa_attr.enhanced_rdma_conn) {
1805 			struct mpa_v2_conn_params *mpa_v2_params;
1806 			u16 ird, ord;
1807 
1808 			mpa_v2_params = (void *)&ep->mpa_pkt[sizeof(*mpa)];
1809 			ird = ntohs(mpa_v2_params->ird);
1810 			ord = ntohs(mpa_v2_params->ord);
1811 
1812 			ep->ird = ird & MPA_V2_IRD_ORD_MASK;
1813 			ep->ord = ord & MPA_V2_IRD_ORD_MASK;
1814 			if (ird & MPA_V2_PEER2PEER_MODEL && peer2peer) {
1815 				if (ord & MPA_V2_RDMA_WRITE_RTR) {
1816 					ep->mpa_attr.p2p_type =
1817 					    FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1818 				} else if (ord & MPA_V2_RDMA_READ_RTR) {
1819 					ep->mpa_attr.p2p_type =
1820 					    FW_RI_INIT_P2PTYPE_READ_REQ;
1821 				}
1822 			}
1823 		}
1824 	} else if (mpa->revision == 1 && peer2peer)
1825 		ep->mpa_attr.p2p_type = p2p_type;
1826 
1827 	if (set_tcpinfo(ep))
1828 		goto abort;
1829 
1830 	CTR5(KTR_IW_CXGBE, "%s: crc_enabled = %d, recv_marker_enabled = %d, "
1831 	    "xmit_marker_enabled = %d, version = %d", __func__,
1832 	    ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1833 	    ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1834 
1835 	state_set(&ep->com, MPA_REQ_RCVD);
1836 	STOP_EP_TIMER(ep);
1837 
1838 	/* drive upcall */
1839 	mutex_lock(&ep->parent_ep->com.mutex);
1840 	if (ep->parent_ep->com.state != DEAD)
1841 		connect_request_upcall(ep);
1842 	else
1843 		abort_connection(ep);
1844 	mutex_unlock(&ep->parent_ep->com.mutex);
1845 }
1846 
1847 /*
1848  * Upcall from the adapter indicating data has been transmitted.
1849  * For us its just the single MPA request or reply.  We can now free
1850  * the skb holding the mpa message.
1851  */
1852 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1853 {
1854 	int err;
1855 	struct c4iw_ep *ep = to_ep(cm_id);
1856 	CTR2(KTR_IW_CXGBE, "%s:crcB %p", __func__, ep);
1857 
1858 	if (state_read(&ep->com) == DEAD) {
1859 
1860 		CTR2(KTR_IW_CXGBE, "%s:crc1 %p", __func__, ep);
1861 		c4iw_put_ep(&ep->com);
1862 		return -ECONNRESET;
1863 	}
1864 	set_bit(ULP_REJECT, &ep->com.history);
1865 	BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1866 
1867 	if (mpa_rev == 0) {
1868 
1869 		CTR2(KTR_IW_CXGBE, "%s:crc2 %p", __func__, ep);
1870 		abort_connection(ep);
1871 	}
1872 	else {
1873 
1874 		CTR2(KTR_IW_CXGBE, "%s:crc3 %p", __func__, ep);
1875 		err = send_mpa_reject(ep, pdata, pdata_len);
1876 		err = soshutdown(ep->com.so, 3);
1877 	}
1878 	c4iw_put_ep(&ep->com);
1879 	CTR2(KTR_IW_CXGBE, "%s:crc4 %p", __func__, ep);
1880 	return 0;
1881 }
1882 
1883 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1884 {
1885 	int err;
1886 	struct c4iw_qp_attributes attrs;
1887 	enum c4iw_qp_attr_mask mask;
1888 	struct c4iw_ep *ep = to_ep(cm_id);
1889 	struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
1890 	struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
1891 
1892 	CTR2(KTR_IW_CXGBE, "%s:cacB %p", __func__, ep);
1893 
1894 	if (state_read(&ep->com) == DEAD) {
1895 
1896 		CTR2(KTR_IW_CXGBE, "%s:cac1 %p", __func__, ep);
1897 		err = -ECONNRESET;
1898 		goto err;
1899 	}
1900 
1901 	BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1902 	BUG_ON(!qp);
1903 
1904 	set_bit(ULP_ACCEPT, &ep->com.history);
1905 
1906 	if ((conn_param->ord > c4iw_max_read_depth) ||
1907 		(conn_param->ird > c4iw_max_read_depth)) {
1908 
1909 		CTR2(KTR_IW_CXGBE, "%s:cac2 %p", __func__, ep);
1910 		abort_connection(ep);
1911 		err = -EINVAL;
1912 		goto err;
1913 	}
1914 
1915 	if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1916 
1917 		CTR2(KTR_IW_CXGBE, "%s:cac3 %p", __func__, ep);
1918 
1919 		if (conn_param->ord > ep->ird) {
1920 
1921 			CTR2(KTR_IW_CXGBE, "%s:cac4 %p", __func__, ep);
1922 			ep->ird = conn_param->ird;
1923 			ep->ord = conn_param->ord;
1924 			send_mpa_reject(ep, conn_param->private_data,
1925 					conn_param->private_data_len);
1926 			abort_connection(ep);
1927 			err = -ENOMEM;
1928 			goto err;
1929 		}
1930 
1931 		if (conn_param->ird > ep->ord) {
1932 
1933 			CTR2(KTR_IW_CXGBE, "%s:cac5 %p", __func__, ep);
1934 
1935 			if (!ep->ord) {
1936 
1937 				CTR2(KTR_IW_CXGBE, "%s:cac6 %p", __func__, ep);
1938 				conn_param->ird = 1;
1939 			}
1940 			else {
1941 				CTR2(KTR_IW_CXGBE, "%s:cac7 %p", __func__, ep);
1942 				abort_connection(ep);
1943 				err = -ENOMEM;
1944 				goto err;
1945 			}
1946 		}
1947 
1948 	}
1949 	ep->ird = conn_param->ird;
1950 	ep->ord = conn_param->ord;
1951 
1952 	if (ep->mpa_attr.version != 2) {
1953 
1954 		CTR2(KTR_IW_CXGBE, "%s:cac8 %p", __func__, ep);
1955 
1956 		if (peer2peer && ep->ird == 0) {
1957 
1958 			CTR2(KTR_IW_CXGBE, "%s:cac9 %p", __func__, ep);
1959 			ep->ird = 1;
1960 		}
1961 	}
1962 
1963 
1964 	cm_id->add_ref(cm_id);
1965 	ep->com.cm_id = cm_id;
1966 	ep->com.qp = qp;
1967 	//ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
1968 
1969 	/* bind QP to EP and move to RTS */
1970 	attrs.mpa_attr = ep->mpa_attr;
1971 	attrs.max_ird = ep->ird;
1972 	attrs.max_ord = ep->ord;
1973 	attrs.llp_stream_handle = ep;
1974 	attrs.next_state = C4IW_QP_STATE_RTS;
1975 
1976 	/* bind QP and TID with INIT_WR */
1977 	mask = C4IW_QP_ATTR_NEXT_STATE |
1978 		C4IW_QP_ATTR_LLP_STREAM_HANDLE |
1979 		C4IW_QP_ATTR_MPA_ATTR |
1980 		C4IW_QP_ATTR_MAX_IRD |
1981 		C4IW_QP_ATTR_MAX_ORD;
1982 
1983 	err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
1984 
1985 	if (err) {
1986 
1987 		CTR2(KTR_IW_CXGBE, "%s:caca %p", __func__, ep);
1988 		goto err1;
1989 	}
1990 	err = send_mpa_reply(ep, conn_param->private_data,
1991 			conn_param->private_data_len);
1992 
1993 	if (err) {
1994 
1995 		CTR2(KTR_IW_CXGBE, "%s:caca %p", __func__, ep);
1996 		goto err1;
1997 	}
1998 
1999 	state_set(&ep->com, FPDU_MODE);
2000 	established_upcall(ep);
2001 	c4iw_put_ep(&ep->com);
2002 	CTR2(KTR_IW_CXGBE, "%s:cacE %p", __func__, ep);
2003 	return 0;
2004 err1:
2005 	ep->com.cm_id = NULL;
2006 	ep->com.qp = NULL;
2007 	cm_id->rem_ref(cm_id);
2008 err:
2009 	c4iw_put_ep(&ep->com);
2010 	CTR2(KTR_IW_CXGBE, "%s:cacE err %p", __func__, ep);
2011 	return err;
2012 }
2013 
2014 
2015 
2016 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2017 {
2018 	int err = 0;
2019 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2020 	struct c4iw_ep *ep = NULL;
2021 	struct rtentry *rt;
2022 	struct toedev *tdev;
2023 
2024 	CTR2(KTR_IW_CXGBE, "%s:ccB %p", __func__, cm_id);
2025 
2026 	if ((conn_param->ord > c4iw_max_read_depth) ||
2027 		(conn_param->ird > c4iw_max_read_depth)) {
2028 
2029 		CTR2(KTR_IW_CXGBE, "%s:cc1 %p", __func__, cm_id);
2030 		err = -EINVAL;
2031 		goto out;
2032 	}
2033 	ep = alloc_ep(sizeof(*ep), M_NOWAIT);
2034 
2035 	if (!ep) {
2036 
2037 		CTR2(KTR_IW_CXGBE, "%s:cc2 %p", __func__, cm_id);
2038 		printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2039 		err = -ENOMEM;
2040 		goto out;
2041 	}
2042 	init_timer(&ep->timer);
2043 	ep->plen = conn_param->private_data_len;
2044 
2045 	if (ep->plen) {
2046 
2047 		CTR2(KTR_IW_CXGBE, "%s:cc3 %p", __func__, ep);
2048 		memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
2049 				conn_param->private_data, ep->plen);
2050 	}
2051 	ep->ird = conn_param->ird;
2052 	ep->ord = conn_param->ord;
2053 
2054 	if (peer2peer && ep->ord == 0) {
2055 
2056 		CTR2(KTR_IW_CXGBE, "%s:cc4 %p", __func__, ep);
2057 		ep->ord = 1;
2058 	}
2059 
2060 	cm_id->add_ref(cm_id);
2061 	ep->com.dev = dev;
2062 	ep->com.cm_id = cm_id;
2063 	ep->com.qp = get_qhp(dev, conn_param->qpn);
2064 
2065 	if (!ep->com.qp) {
2066 
2067 		CTR2(KTR_IW_CXGBE, "%s:cc5 %p", __func__, ep);
2068 		err = -EINVAL;
2069 		goto fail2;
2070 	}
2071 	ep->com.thread = curthread;
2072 	ep->com.so = cm_id->so;
2073 
2074 	init_sock(&ep->com);
2075 
2076 	/* find a route */
2077 	rt = find_route(
2078 		cm_id->local_addr.sin_addr.s_addr,
2079 		cm_id->remote_addr.sin_addr.s_addr,
2080 		cm_id->local_addr.sin_port,
2081 		cm_id->remote_addr.sin_port, 0);
2082 
2083 	if (!rt) {
2084 
2085 		CTR2(KTR_IW_CXGBE, "%s:cc7 %p", __func__, ep);
2086 		printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
2087 		err = -EHOSTUNREACH;
2088 		goto fail3;
2089 	}
2090 
2091 
2092 	if (!(rt->rt_ifp->if_flags & IFCAP_TOE)) {
2093 
2094 		CTR2(KTR_IW_CXGBE, "%s:cc8 %p", __func__, ep);
2095 		printf("%s - interface not TOE capable.\n", __func__);
2096 		goto fail3;
2097 	}
2098 	tdev = TOEDEV(rt->rt_ifp);
2099 
2100 	if (tdev == NULL) {
2101 
2102 		CTR2(KTR_IW_CXGBE, "%s:cc9 %p", __func__, ep);
2103 		printf("%s - No toedev for interface.\n", __func__);
2104 		goto fail3;
2105 	}
2106 	RTFREE(rt);
2107 
2108 	state_set(&ep->com, CONNECTING);
2109 	ep->tos = 0;
2110 	ep->com.local_addr = cm_id->local_addr;
2111 	ep->com.remote_addr = cm_id->remote_addr;
2112 	err = soconnect(ep->com.so, (struct sockaddr *)&ep->com.remote_addr,
2113 		ep->com.thread);
2114 
2115 	if (!err) {
2116 
2117 		CTR2(KTR_IW_CXGBE, "%s:cca %p", __func__, ep);
2118 		goto out;
2119 	}
2120 
2121 fail3:
2122 	CTR2(KTR_IW_CXGBE, "%s:ccb %p", __func__, ep);
2123 	RTFREE(rt);
2124 fail2:
2125 	cm_id->rem_ref(cm_id);
2126 	c4iw_put_ep(&ep->com);
2127 out:
2128 	CTR2(KTR_IW_CXGBE, "%s:ccE %p", __func__, ep);
2129 	return err;
2130 }
2131 
2132 /*
2133  * iwcm->create_listen.  Returns -errno on failure.
2134  */
2135 int
2136 c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
2137 {
2138 	int rc;
2139 	struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2140 	struct c4iw_listen_ep *ep;
2141 	struct socket *so = cm_id->so;
2142 
2143 	ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2144 	CTR5(KTR_IW_CXGBE, "%s: cm_id %p, lso %p, ep %p, inp %p", __func__,
2145 	    cm_id, so, ep, so->so_pcb);
2146 	if (ep == NULL) {
2147 		log(LOG_ERR, "%s: failed to alloc memory for endpoint\n",
2148 		    __func__);
2149 		rc = ENOMEM;
2150 		goto failed;
2151 	}
2152 
2153 	cm_id->add_ref(cm_id);
2154 	ep->com.cm_id = cm_id;
2155 	ep->com.dev = dev;
2156 	ep->backlog = backlog;
2157 	ep->com.local_addr = cm_id->local_addr;
2158 	ep->com.thread = curthread;
2159 	state_set(&ep->com, LISTEN);
2160 	ep->com.so = so;
2161 	init_sock(&ep->com);
2162 
2163 	rc = solisten(so, ep->backlog, ep->com.thread);
2164 	if (rc != 0) {
2165 		log(LOG_ERR, "%s: failed to start listener: %d\n", __func__,
2166 		    rc);
2167 		close_socket(&ep->com, 0);
2168 		cm_id->rem_ref(cm_id);
2169 		c4iw_put_ep(&ep->com);
2170 		goto failed;
2171 	}
2172 
2173 	cm_id->provider_data = ep;
2174 	return (0);
2175 
2176 failed:
2177 	CTR3(KTR_IW_CXGBE, "%s: cm_id %p, FAILED (%d)", __func__, cm_id, rc);
2178 	return (-rc);
2179 }
2180 
2181 int
2182 c4iw_destroy_listen(struct iw_cm_id *cm_id)
2183 {
2184 	int rc;
2185 	struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
2186 
2187 	CTR4(KTR_IW_CXGBE, "%s: cm_id %p, so %p, inp %p", __func__, cm_id,
2188 	    cm_id->so, cm_id->so->so_pcb);
2189 
2190 	state_set(&ep->com, DEAD);
2191 	rc = close_socket(&ep->com, 0);
2192 	cm_id->rem_ref(cm_id);
2193 	c4iw_put_ep(&ep->com);
2194 
2195 	return (rc);
2196 }
2197 
2198 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2199 {
2200 	int ret = 0;
2201 	int close = 0;
2202 	int fatal = 0;
2203 	struct c4iw_rdev *rdev;
2204 
2205 	mutex_lock(&ep->com.mutex);
2206 
2207 	CTR2(KTR_IW_CXGBE, "%s:cedB %p", __func__, ep);
2208 
2209 	rdev = &ep->com.dev->rdev;
2210 
2211 	if (c4iw_fatal_error(rdev)) {
2212 
2213 		CTR2(KTR_IW_CXGBE, "%s:ced1 %p", __func__, ep);
2214 		fatal = 1;
2215 		close_complete_upcall(ep);
2216 		ep->com.state = DEAD;
2217 	}
2218 	CTR3(KTR_IW_CXGBE, "%s:ced2 %p %s", __func__, ep,
2219 	    states[ep->com.state]);
2220 
2221 	switch (ep->com.state) {
2222 
2223 		case MPA_REQ_WAIT:
2224 		case MPA_REQ_SENT:
2225 		case MPA_REQ_RCVD:
2226 		case MPA_REP_SENT:
2227 		case FPDU_MODE:
2228 			close = 1;
2229 			if (abrupt)
2230 				ep->com.state = ABORTING;
2231 			else {
2232 				ep->com.state = CLOSING;
2233 				START_EP_TIMER(ep);
2234 			}
2235 			set_bit(CLOSE_SENT, &ep->com.flags);
2236 			break;
2237 
2238 		case CLOSING:
2239 
2240 			if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2241 
2242 				close = 1;
2243 				if (abrupt) {
2244 					STOP_EP_TIMER(ep);
2245 					ep->com.state = ABORTING;
2246 				} else
2247 					ep->com.state = MORIBUND;
2248 			}
2249 			break;
2250 
2251 		case MORIBUND:
2252 		case ABORTING:
2253 		case DEAD:
2254 			CTR3(KTR_IW_CXGBE,
2255 			    "%s ignoring disconnect ep %p state %u", __func__,
2256 			    ep, ep->com.state);
2257 			break;
2258 
2259 		default:
2260 			BUG();
2261 			break;
2262 	}
2263 
2264 	mutex_unlock(&ep->com.mutex);
2265 
2266 	if (close) {
2267 
2268 		CTR2(KTR_IW_CXGBE, "%s:ced3 %p", __func__, ep);
2269 
2270 		if (abrupt) {
2271 
2272 			CTR2(KTR_IW_CXGBE, "%s:ced4 %p", __func__, ep);
2273 			set_bit(EP_DISC_ABORT, &ep->com.history);
2274 			ret = abort_connection(ep);
2275 		} else {
2276 
2277 			CTR2(KTR_IW_CXGBE, "%s:ced5 %p", __func__, ep);
2278 			set_bit(EP_DISC_CLOSE, &ep->com.history);
2279 
2280 			if (!ep->parent_ep)
2281 				__state_set(&ep->com, MORIBUND);
2282 			ret = shutdown_socket(&ep->com);
2283 		}
2284 
2285 		if (ret) {
2286 
2287 			fatal = 1;
2288 		}
2289 	}
2290 
2291 	if (fatal) {
2292 
2293 		release_ep_resources(ep);
2294 		CTR2(KTR_IW_CXGBE, "%s:ced6 %p", __func__, ep);
2295 	}
2296 	CTR2(KTR_IW_CXGBE, "%s:cedE %p", __func__, ep);
2297 	return ret;
2298 }
2299 
2300 #ifdef C4IW_EP_REDIRECT
2301 int c4iw_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2302 		struct l2t_entry *l2t)
2303 {
2304 	struct c4iw_ep *ep = ctx;
2305 
2306 	if (ep->dst != old)
2307 		return 0;
2308 
2309 	PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2310 			l2t);
2311 	dst_hold(new);
2312 	cxgb4_l2t_release(ep->l2t);
2313 	ep->l2t = l2t;
2314 	dst_release(old);
2315 	ep->dst = new;
2316 	return 1;
2317 }
2318 #endif
2319 
2320 
2321 
2322 static void ep_timeout(unsigned long arg)
2323 {
2324 	struct c4iw_ep *ep = (struct c4iw_ep *)arg;
2325 	int kickit = 0;
2326 
2327 	CTR2(KTR_IW_CXGBE, "%s:etB %p", __func__, ep);
2328 	spin_lock(&timeout_lock);
2329 
2330 	if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
2331 
2332 		list_add_tail(&ep->entry, &timeout_list);
2333 		kickit = 1;
2334 	}
2335 	spin_unlock(&timeout_lock);
2336 
2337 	if (kickit) {
2338 
2339 		CTR2(KTR_IW_CXGBE, "%s:et1 %p", __func__, ep);
2340 		queue_work(c4iw_taskq, &c4iw_task);
2341 	}
2342 	CTR2(KTR_IW_CXGBE, "%s:etE %p", __func__, ep);
2343 }
2344 
2345 static int fw6_wr_rpl(struct adapter *sc, const __be64 *rpl)
2346 {
2347 	uint64_t val = be64toh(*rpl);
2348 	int ret;
2349 	struct c4iw_wr_wait *wr_waitp;
2350 
2351 	ret = (int)((val >> 8) & 0xff);
2352 	wr_waitp = (struct c4iw_wr_wait *)rpl[1];
2353 	CTR3(KTR_IW_CXGBE, "%s wr_waitp %p ret %u", __func__, wr_waitp, ret);
2354 	if (wr_waitp)
2355 		c4iw_wake_up(wr_waitp, ret ? -ret : 0);
2356 
2357 	return (0);
2358 }
2359 
2360 static int fw6_cqe_handler(struct adapter *sc, const __be64 *rpl)
2361 {
2362 	struct t4_cqe cqe =*(const struct t4_cqe *)(&rpl[0]);
2363 
2364 	CTR2(KTR_IW_CXGBE, "%s rpl %p", __func__, rpl);
2365 	c4iw_ev_dispatch(sc->iwarp_softc, &cqe);
2366 
2367 	return (0);
2368 }
2369 
2370 static int terminate(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
2371 {
2372 
2373 	struct adapter *sc = iq->adapter;
2374 
2375 	const struct cpl_rdma_terminate *rpl = (const void *)(rss + 1);
2376 	unsigned int tid = GET_TID(rpl);
2377 	struct c4iw_qp_attributes attrs;
2378 	struct toepcb *toep = lookup_tid(sc, tid);
2379 	struct socket *so = inp_inpcbtosocket(toep->inp);
2380 	struct c4iw_ep *ep = so->so_rcv.sb_upcallarg;
2381 
2382 	CTR2(KTR_IW_CXGBE, "%s:tB %p %d", __func__, ep);
2383 
2384 	if (ep && ep->com.qp) {
2385 
2386 		printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
2387 				ep->com.qp->wq.sq.qid);
2388 		attrs.next_state = C4IW_QP_STATE_TERMINATE;
2389 		c4iw_modify_qp(ep->com.dev, ep->com.qp, C4IW_QP_ATTR_NEXT_STATE, &attrs,
2390 				1);
2391 	} else
2392 		printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
2393 	CTR2(KTR_IW_CXGBE, "%s:tE %p %d", __func__, ep);
2394 
2395 	return 0;
2396 }
2397 
2398 	void
2399 c4iw_cm_init_cpl(struct adapter *sc)
2400 {
2401 
2402 	t4_register_cpl_handler(sc, CPL_RDMA_TERMINATE, terminate);
2403 	t4_register_fw_msg_handler(sc, FW6_TYPE_WR_RPL, fw6_wr_rpl);
2404 	t4_register_fw_msg_handler(sc, FW6_TYPE_CQE, fw6_cqe_handler);
2405 	t4_register_an_handler(sc, c4iw_ev_handler);
2406 }
2407 
2408 	void
2409 c4iw_cm_term_cpl(struct adapter *sc)
2410 {
2411 
2412 	t4_register_cpl_handler(sc, CPL_RDMA_TERMINATE, NULL);
2413 	t4_register_fw_msg_handler(sc, FW6_TYPE_WR_RPL, NULL);
2414 	t4_register_fw_msg_handler(sc, FW6_TYPE_CQE, NULL);
2415 }
2416 
2417 int __init c4iw_cm_init(void)
2418 {
2419 
2420 	TAILQ_INIT(&req_list);
2421 	spin_lock_init(&req_lock);
2422 	INIT_LIST_HEAD(&timeout_list);
2423 	spin_lock_init(&timeout_lock);
2424 
2425 	INIT_WORK(&c4iw_task, process_req);
2426 
2427 	c4iw_taskq = create_singlethread_workqueue("iw_cxgbe");
2428 	if (!c4iw_taskq)
2429 		return -ENOMEM;
2430 
2431 
2432 	return 0;
2433 }
2434 
2435 void __exit c4iw_cm_term(void)
2436 {
2437 	WARN_ON(!TAILQ_EMPTY(&req_list));
2438 	WARN_ON(!list_empty(&timeout_list));
2439 	flush_workqueue(c4iw_taskq);
2440 	destroy_workqueue(c4iw_taskq);
2441 }
2442 #endif
2443