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