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