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