xref: /openbmc/linux/drivers/infiniband/sw/siw/siw_cm.c (revision 8957261c)
1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2 
3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4 /*          Fredy Neeser */
5 /*          Greg Joyce <greg@opengridcomputing.com> */
6 /* Copyright (c) 2008-2019, IBM Corporation */
7 /* Copyright (c) 2017, Open Grid Computing, Inc. */
8 
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/net.h>
12 #include <linux/inetdevice.h>
13 #include <net/addrconf.h>
14 #include <linux/workqueue.h>
15 #include <net/sock.h>
16 #include <net/tcp.h>
17 #include <linux/inet.h>
18 #include <linux/tcp.h>
19 #include <trace/events/sock.h>
20 
21 #include <rdma/iw_cm.h>
22 #include <rdma/ib_verbs.h>
23 #include <rdma/ib_user_verbs.h>
24 
25 #include "siw.h"
26 #include "siw_cm.h"
27 
28 /*
29  * Set to any combination of
30  * MPA_V2_RDMA_NO_RTR, MPA_V2_RDMA_READ_RTR, MPA_V2_RDMA_WRITE_RTR
31  */
32 static __be16 rtr_type = MPA_V2_RDMA_READ_RTR | MPA_V2_RDMA_WRITE_RTR;
33 static const bool relaxed_ird_negotiation = true;
34 
35 static void siw_cm_llp_state_change(struct sock *s);
36 static void siw_cm_llp_data_ready(struct sock *s);
37 static void siw_cm_llp_write_space(struct sock *s);
38 static void siw_cm_llp_error_report(struct sock *s);
39 static int siw_cm_upcall(struct siw_cep *cep, enum iw_cm_event_type reason,
40 			 int status);
41 
42 static void siw_sk_assign_cm_upcalls(struct sock *sk)
43 {
44 	write_lock_bh(&sk->sk_callback_lock);
45 	sk->sk_state_change = siw_cm_llp_state_change;
46 	sk->sk_data_ready = siw_cm_llp_data_ready;
47 	sk->sk_write_space = siw_cm_llp_write_space;
48 	sk->sk_error_report = siw_cm_llp_error_report;
49 	write_unlock_bh(&sk->sk_callback_lock);
50 }
51 
52 static void siw_sk_save_upcalls(struct sock *sk)
53 {
54 	struct siw_cep *cep = sk_to_cep(sk);
55 
56 	write_lock_bh(&sk->sk_callback_lock);
57 	cep->sk_state_change = sk->sk_state_change;
58 	cep->sk_data_ready = sk->sk_data_ready;
59 	cep->sk_write_space = sk->sk_write_space;
60 	cep->sk_error_report = sk->sk_error_report;
61 	write_unlock_bh(&sk->sk_callback_lock);
62 }
63 
64 static void siw_sk_restore_upcalls(struct sock *sk, struct siw_cep *cep)
65 {
66 	sk->sk_state_change = cep->sk_state_change;
67 	sk->sk_data_ready = cep->sk_data_ready;
68 	sk->sk_write_space = cep->sk_write_space;
69 	sk->sk_error_report = cep->sk_error_report;
70 	sk->sk_user_data = NULL;
71 }
72 
73 static void siw_qp_socket_assoc(struct siw_cep *cep, struct siw_qp *qp)
74 {
75 	struct socket *s = cep->sock;
76 	struct sock *sk = s->sk;
77 
78 	write_lock_bh(&sk->sk_callback_lock);
79 
80 	qp->attrs.sk = s;
81 	sk->sk_data_ready = siw_qp_llp_data_ready;
82 	sk->sk_write_space = siw_qp_llp_write_space;
83 
84 	write_unlock_bh(&sk->sk_callback_lock);
85 }
86 
87 static void siw_socket_disassoc(struct socket *s)
88 {
89 	struct sock *sk = s->sk;
90 	struct siw_cep *cep;
91 
92 	if (sk) {
93 		write_lock_bh(&sk->sk_callback_lock);
94 		cep = sk_to_cep(sk);
95 		if (cep) {
96 			siw_sk_restore_upcalls(sk, cep);
97 			siw_cep_put(cep);
98 		} else {
99 			pr_warn("siw: cannot restore sk callbacks: no ep\n");
100 		}
101 		write_unlock_bh(&sk->sk_callback_lock);
102 	} else {
103 		pr_warn("siw: cannot restore sk callbacks: no sk\n");
104 	}
105 }
106 
107 static void siw_rtr_data_ready(struct sock *sk)
108 {
109 	struct siw_cep *cep;
110 	struct siw_qp *qp = NULL;
111 	read_descriptor_t rd_desc;
112 
113 	trace_sk_data_ready(sk);
114 
115 	read_lock(&sk->sk_callback_lock);
116 
117 	cep = sk_to_cep(sk);
118 	if (!cep) {
119 		WARN(1, "No connection endpoint\n");
120 		goto out;
121 	}
122 	qp = sk_to_qp(sk);
123 
124 	memset(&rd_desc, 0, sizeof(rd_desc));
125 	rd_desc.arg.data = qp;
126 	rd_desc.count = 1;
127 
128 	tcp_read_sock(sk, &rd_desc, siw_tcp_rx_data);
129 	/*
130 	 * Check if first frame was successfully processed.
131 	 * Signal connection full establishment if yes.
132 	 * Failed data processing would have already scheduled
133 	 * connection drop.
134 	 */
135 	if (!qp->rx_stream.rx_suspend)
136 		siw_cm_upcall(cep, IW_CM_EVENT_ESTABLISHED, 0);
137 out:
138 	read_unlock(&sk->sk_callback_lock);
139 	if (qp)
140 		siw_qp_socket_assoc(cep, qp);
141 }
142 
143 static void siw_sk_assign_rtr_upcalls(struct siw_cep *cep)
144 {
145 	struct sock *sk = cep->sock->sk;
146 
147 	write_lock_bh(&sk->sk_callback_lock);
148 	sk->sk_data_ready = siw_rtr_data_ready;
149 	sk->sk_write_space = siw_qp_llp_write_space;
150 	write_unlock_bh(&sk->sk_callback_lock);
151 }
152 
153 static void siw_cep_socket_assoc(struct siw_cep *cep, struct socket *s)
154 {
155 	cep->sock = s;
156 	siw_cep_get(cep);
157 	s->sk->sk_user_data = cep;
158 
159 	siw_sk_save_upcalls(s->sk);
160 	siw_sk_assign_cm_upcalls(s->sk);
161 }
162 
163 static struct siw_cep *siw_cep_alloc(struct siw_device *sdev)
164 {
165 	struct siw_cep *cep = kzalloc(sizeof(*cep), GFP_KERNEL);
166 	unsigned long flags;
167 
168 	if (!cep)
169 		return NULL;
170 
171 	INIT_LIST_HEAD(&cep->listenq);
172 	INIT_LIST_HEAD(&cep->devq);
173 	INIT_LIST_HEAD(&cep->work_freelist);
174 
175 	kref_init(&cep->ref);
176 	cep->state = SIW_EPSTATE_IDLE;
177 	init_waitqueue_head(&cep->waitq);
178 	spin_lock_init(&cep->lock);
179 	cep->sdev = sdev;
180 	cep->enhanced_rdma_conn_est = false;
181 
182 	spin_lock_irqsave(&sdev->lock, flags);
183 	list_add_tail(&cep->devq, &sdev->cep_list);
184 	spin_unlock_irqrestore(&sdev->lock, flags);
185 
186 	siw_dbg_cep(cep, "new endpoint\n");
187 	return cep;
188 }
189 
190 static void siw_cm_free_work(struct siw_cep *cep)
191 {
192 	struct list_head *w, *tmp;
193 	struct siw_cm_work *work;
194 
195 	list_for_each_safe(w, tmp, &cep->work_freelist) {
196 		work = list_entry(w, struct siw_cm_work, list);
197 		list_del(&work->list);
198 		kfree(work);
199 	}
200 }
201 
202 static void siw_cancel_mpatimer(struct siw_cep *cep)
203 {
204 	spin_lock_bh(&cep->lock);
205 	if (cep->mpa_timer) {
206 		if (cancel_delayed_work(&cep->mpa_timer->work)) {
207 			siw_cep_put(cep);
208 			kfree(cep->mpa_timer); /* not needed again */
209 		}
210 		cep->mpa_timer = NULL;
211 	}
212 	spin_unlock_bh(&cep->lock);
213 }
214 
215 static void siw_put_work(struct siw_cm_work *work)
216 {
217 	INIT_LIST_HEAD(&work->list);
218 	spin_lock_bh(&work->cep->lock);
219 	list_add(&work->list, &work->cep->work_freelist);
220 	spin_unlock_bh(&work->cep->lock);
221 }
222 
223 static void siw_cep_set_inuse(struct siw_cep *cep)
224 {
225 	unsigned long flags;
226 retry:
227 	spin_lock_irqsave(&cep->lock, flags);
228 
229 	if (cep->in_use) {
230 		spin_unlock_irqrestore(&cep->lock, flags);
231 		wait_event_interruptible(cep->waitq, !cep->in_use);
232 		if (signal_pending(current))
233 			flush_signals(current);
234 		goto retry;
235 	} else {
236 		cep->in_use = 1;
237 		spin_unlock_irqrestore(&cep->lock, flags);
238 	}
239 }
240 
241 static void siw_cep_set_free(struct siw_cep *cep)
242 {
243 	unsigned long flags;
244 
245 	spin_lock_irqsave(&cep->lock, flags);
246 	cep->in_use = 0;
247 	spin_unlock_irqrestore(&cep->lock, flags);
248 
249 	wake_up(&cep->waitq);
250 }
251 
252 static void __siw_cep_dealloc(struct kref *ref)
253 {
254 	struct siw_cep *cep = container_of(ref, struct siw_cep, ref);
255 	struct siw_device *sdev = cep->sdev;
256 	unsigned long flags;
257 
258 	WARN_ON(cep->listen_cep);
259 
260 	/* kfree(NULL) is safe */
261 	kfree(cep->mpa.pdata);
262 	spin_lock_bh(&cep->lock);
263 	if (!list_empty(&cep->work_freelist))
264 		siw_cm_free_work(cep);
265 	spin_unlock_bh(&cep->lock);
266 
267 	spin_lock_irqsave(&sdev->lock, flags);
268 	list_del(&cep->devq);
269 	spin_unlock_irqrestore(&sdev->lock, flags);
270 
271 	siw_dbg_cep(cep, "free endpoint\n");
272 	kfree(cep);
273 }
274 
275 static struct siw_cm_work *siw_get_work(struct siw_cep *cep)
276 {
277 	struct siw_cm_work *work = NULL;
278 
279 	spin_lock_bh(&cep->lock);
280 	if (!list_empty(&cep->work_freelist)) {
281 		work = list_entry(cep->work_freelist.next, struct siw_cm_work,
282 				  list);
283 		list_del_init(&work->list);
284 	}
285 	spin_unlock_bh(&cep->lock);
286 	return work;
287 }
288 
289 static int siw_cm_alloc_work(struct siw_cep *cep, int num)
290 {
291 	struct siw_cm_work *work;
292 
293 	while (num--) {
294 		work = kmalloc(sizeof(*work), GFP_KERNEL);
295 		if (!work) {
296 			if (!(list_empty(&cep->work_freelist)))
297 				siw_cm_free_work(cep);
298 			return -ENOMEM;
299 		}
300 		work->cep = cep;
301 		INIT_LIST_HEAD(&work->list);
302 		list_add(&work->list, &cep->work_freelist);
303 	}
304 	return 0;
305 }
306 
307 /*
308  * siw_cm_upcall()
309  *
310  * Upcall to IWCM to inform about async connection events
311  */
312 static int siw_cm_upcall(struct siw_cep *cep, enum iw_cm_event_type reason,
313 			 int status)
314 {
315 	struct iw_cm_event event;
316 	struct iw_cm_id *id;
317 
318 	memset(&event, 0, sizeof(event));
319 	event.status = status;
320 	event.event = reason;
321 
322 	if (reason == IW_CM_EVENT_CONNECT_REQUEST) {
323 		event.provider_data = cep;
324 		id = cep->listen_cep->cm_id;
325 	} else {
326 		id = cep->cm_id;
327 	}
328 	/* Signal IRD and ORD */
329 	if (reason == IW_CM_EVENT_ESTABLISHED ||
330 	    reason == IW_CM_EVENT_CONNECT_REPLY) {
331 		/* Signal negotiated IRD/ORD values we will use */
332 		event.ird = cep->ird;
333 		event.ord = cep->ord;
334 	} else if (reason == IW_CM_EVENT_CONNECT_REQUEST) {
335 		event.ird = cep->ord;
336 		event.ord = cep->ird;
337 	}
338 	/* Signal private data and address information */
339 	if (reason == IW_CM_EVENT_CONNECT_REQUEST ||
340 	    reason == IW_CM_EVENT_CONNECT_REPLY) {
341 		u16 pd_len = be16_to_cpu(cep->mpa.hdr.params.pd_len);
342 
343 		if (pd_len) {
344 			/*
345 			 * hand over MPA private data
346 			 */
347 			event.private_data_len = pd_len;
348 			event.private_data = cep->mpa.pdata;
349 
350 			/* Hide MPA V2 IRD/ORD control */
351 			if (cep->enhanced_rdma_conn_est) {
352 				event.private_data_len -=
353 					sizeof(struct mpa_v2_data);
354 				event.private_data +=
355 					sizeof(struct mpa_v2_data);
356 			}
357 		}
358 		getname_local(cep->sock, &event.local_addr);
359 		getname_peer(cep->sock, &event.remote_addr);
360 	}
361 	siw_dbg_cep(cep, "[QP %u]: reason=%d, status=%d\n",
362 		    cep->qp ? qp_id(cep->qp) : UINT_MAX, reason, status);
363 
364 	return id->event_handler(id, &event);
365 }
366 
367 /*
368  * siw_qp_cm_drop()
369  *
370  * Drops established LLP connection if present and not already
371  * scheduled for dropping. Called from user context, SQ workqueue
372  * or receive IRQ. Caller signals if socket can be immediately
373  * closed (basically, if not in IRQ).
374  */
375 void siw_qp_cm_drop(struct siw_qp *qp, int schedule)
376 {
377 	struct siw_cep *cep = qp->cep;
378 
379 	qp->rx_stream.rx_suspend = 1;
380 	qp->tx_ctx.tx_suspend = 1;
381 
382 	if (!qp->cep)
383 		return;
384 
385 	if (schedule) {
386 		siw_cm_queue_work(cep, SIW_CM_WORK_CLOSE_LLP);
387 	} else {
388 		siw_cep_set_inuse(cep);
389 
390 		if (cep->state == SIW_EPSTATE_CLOSED) {
391 			siw_dbg_cep(cep, "already closed\n");
392 			goto out;
393 		}
394 		siw_dbg_cep(cep, "immediate close, state %d\n", cep->state);
395 
396 		if (qp->term_info.valid)
397 			siw_send_terminate(qp);
398 
399 		if (cep->cm_id) {
400 			switch (cep->state) {
401 			case SIW_EPSTATE_AWAIT_MPAREP:
402 				siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
403 					      -EINVAL);
404 				break;
405 
406 			case SIW_EPSTATE_RDMA_MODE:
407 				siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
408 				break;
409 
410 			case SIW_EPSTATE_IDLE:
411 			case SIW_EPSTATE_LISTENING:
412 			case SIW_EPSTATE_CONNECTING:
413 			case SIW_EPSTATE_AWAIT_MPAREQ:
414 			case SIW_EPSTATE_RECVD_MPAREQ:
415 			case SIW_EPSTATE_CLOSED:
416 			default:
417 				break;
418 			}
419 			cep->cm_id->rem_ref(cep->cm_id);
420 			cep->cm_id = NULL;
421 			siw_cep_put(cep);
422 		}
423 		cep->state = SIW_EPSTATE_CLOSED;
424 
425 		if (cep->sock) {
426 			siw_socket_disassoc(cep->sock);
427 			/*
428 			 * Immediately close socket
429 			 */
430 			sock_release(cep->sock);
431 			cep->sock = NULL;
432 		}
433 		if (cep->qp) {
434 			cep->qp = NULL;
435 			siw_qp_put(qp);
436 		}
437 out:
438 		siw_cep_set_free(cep);
439 	}
440 }
441 
442 void siw_cep_put(struct siw_cep *cep)
443 {
444 	WARN_ON(kref_read(&cep->ref) < 1);
445 	kref_put(&cep->ref, __siw_cep_dealloc);
446 }
447 
448 void siw_cep_get(struct siw_cep *cep)
449 {
450 	kref_get(&cep->ref);
451 }
452 
453 /*
454  * Expects params->pd_len in host byte order
455  */
456 static int siw_send_mpareqrep(struct siw_cep *cep, const void *pdata, u8 pd_len)
457 {
458 	struct socket *s = cep->sock;
459 	struct mpa_rr *rr = &cep->mpa.hdr;
460 	struct kvec iov[3];
461 	struct msghdr msg;
462 	int rv;
463 	int iovec_num = 0;
464 	int mpa_len;
465 
466 	memset(&msg, 0, sizeof(msg));
467 
468 	iov[iovec_num].iov_base = rr;
469 	iov[iovec_num].iov_len = sizeof(*rr);
470 	mpa_len = sizeof(*rr);
471 
472 	if (cep->enhanced_rdma_conn_est) {
473 		iovec_num++;
474 		iov[iovec_num].iov_base = &cep->mpa.v2_ctrl;
475 		iov[iovec_num].iov_len = sizeof(cep->mpa.v2_ctrl);
476 		mpa_len += sizeof(cep->mpa.v2_ctrl);
477 	}
478 	if (pd_len) {
479 		iovec_num++;
480 		iov[iovec_num].iov_base = (char *)pdata;
481 		iov[iovec_num].iov_len = pd_len;
482 		mpa_len += pd_len;
483 	}
484 	if (cep->enhanced_rdma_conn_est)
485 		pd_len += sizeof(cep->mpa.v2_ctrl);
486 
487 	rr->params.pd_len = cpu_to_be16(pd_len);
488 
489 	rv = kernel_sendmsg(s, &msg, iov, iovec_num + 1, mpa_len);
490 
491 	return rv < 0 ? rv : 0;
492 }
493 
494 /*
495  * Receive MPA Request/Reply header.
496  *
497  * Returns 0 if complete MPA Request/Reply header including
498  * eventual private data was received. Returns -EAGAIN if
499  * header was partially received or negative error code otherwise.
500  *
501  * Context: May be called in process context only
502  */
503 static int siw_recv_mpa_rr(struct siw_cep *cep)
504 {
505 	struct mpa_rr *hdr = &cep->mpa.hdr;
506 	struct socket *s = cep->sock;
507 	u16 pd_len;
508 	int rcvd, to_rcv;
509 
510 	if (cep->mpa.bytes_rcvd < sizeof(struct mpa_rr)) {
511 		rcvd = ksock_recv(s, (char *)hdr + cep->mpa.bytes_rcvd,
512 				  sizeof(struct mpa_rr) - cep->mpa.bytes_rcvd,
513 				  0);
514 		if (rcvd <= 0)
515 			return -ECONNABORTED;
516 
517 		cep->mpa.bytes_rcvd += rcvd;
518 
519 		if (cep->mpa.bytes_rcvd < sizeof(struct mpa_rr))
520 			return -EAGAIN;
521 
522 		if (be16_to_cpu(hdr->params.pd_len) > MPA_MAX_PRIVDATA)
523 			return -EPROTO;
524 	}
525 	pd_len = be16_to_cpu(hdr->params.pd_len);
526 
527 	/*
528 	 * At least the MPA Request/Reply header (frame not including
529 	 * private data) has been received.
530 	 * Receive (or continue receiving) any private data.
531 	 */
532 	to_rcv = pd_len - (cep->mpa.bytes_rcvd - sizeof(struct mpa_rr));
533 
534 	if (!to_rcv) {
535 		/*
536 		 * We must have hdr->params.pd_len == 0 and thus received a
537 		 * complete MPA Request/Reply frame.
538 		 * Check against peer protocol violation.
539 		 */
540 		u32 word;
541 
542 		rcvd = ksock_recv(s, (char *)&word, sizeof(word), MSG_DONTWAIT);
543 		if (rcvd == -EAGAIN)
544 			return 0;
545 
546 		if (rcvd == 0) {
547 			siw_dbg_cep(cep, "peer EOF\n");
548 			return -EPIPE;
549 		}
550 		if (rcvd < 0) {
551 			siw_dbg_cep(cep, "error: %d\n", rcvd);
552 			return rcvd;
553 		}
554 		siw_dbg_cep(cep, "peer sent extra data: %d\n", rcvd);
555 
556 		return -EPROTO;
557 	}
558 
559 	/*
560 	 * At this point, we must have hdr->params.pd_len != 0.
561 	 * A private data buffer gets allocated if hdr->params.pd_len != 0.
562 	 */
563 	if (!cep->mpa.pdata) {
564 		cep->mpa.pdata = kmalloc(pd_len + 4, GFP_KERNEL);
565 		if (!cep->mpa.pdata)
566 			return -ENOMEM;
567 	}
568 	rcvd = ksock_recv(
569 		s, cep->mpa.pdata + cep->mpa.bytes_rcvd - sizeof(struct mpa_rr),
570 		to_rcv + 4, MSG_DONTWAIT);
571 
572 	if (rcvd < 0)
573 		return rcvd;
574 
575 	if (rcvd > to_rcv)
576 		return -EPROTO;
577 
578 	cep->mpa.bytes_rcvd += rcvd;
579 
580 	if (to_rcv == rcvd) {
581 		siw_dbg_cep(cep, "%d bytes private data received\n", pd_len);
582 		return 0;
583 	}
584 	return -EAGAIN;
585 }
586 
587 /*
588  * siw_proc_mpareq()
589  *
590  * Read MPA Request from socket and signal new connection to IWCM
591  * if success. Caller must hold lock on corresponding listening CEP.
592  */
593 static int siw_proc_mpareq(struct siw_cep *cep)
594 {
595 	struct mpa_rr *req;
596 	int version, rv;
597 	u16 pd_len;
598 
599 	rv = siw_recv_mpa_rr(cep);
600 	if (rv)
601 		return rv;
602 
603 	req = &cep->mpa.hdr;
604 
605 	version = __mpa_rr_revision(req->params.bits);
606 	pd_len = be16_to_cpu(req->params.pd_len);
607 
608 	if (version > MPA_REVISION_2)
609 		/* allow for 0, 1, and 2 only */
610 		return -EPROTO;
611 
612 	if (memcmp(req->key, MPA_KEY_REQ, 16))
613 		return -EPROTO;
614 
615 	/* Prepare for sending MPA reply */
616 	memcpy(req->key, MPA_KEY_REP, 16);
617 
618 	if (version == MPA_REVISION_2 &&
619 	    (req->params.bits & MPA_RR_FLAG_ENHANCED)) {
620 		/*
621 		 * MPA version 2 must signal IRD/ORD values and P2P mode
622 		 * in private data if header flag MPA_RR_FLAG_ENHANCED
623 		 * is set.
624 		 */
625 		if (pd_len < sizeof(struct mpa_v2_data))
626 			goto reject_conn;
627 
628 		cep->enhanced_rdma_conn_est = true;
629 	}
630 
631 	/* MPA Markers: currently not supported. Marker TX to be added. */
632 	if (req->params.bits & MPA_RR_FLAG_MARKERS)
633 		goto reject_conn;
634 
635 	if (req->params.bits & MPA_RR_FLAG_CRC) {
636 		/*
637 		 * RFC 5044, page 27: CRC MUST be used if peer requests it.
638 		 * siw specific: 'mpa_crc_strict' parameter to reject
639 		 * connection with CRC if local CRC off enforced by
640 		 * 'mpa_crc_strict' module parameter.
641 		 */
642 		if (!mpa_crc_required && mpa_crc_strict)
643 			goto reject_conn;
644 
645 		/* Enable CRC if requested by module parameter */
646 		if (mpa_crc_required)
647 			req->params.bits |= MPA_RR_FLAG_CRC;
648 	}
649 	if (cep->enhanced_rdma_conn_est) {
650 		struct mpa_v2_data *v2 = (struct mpa_v2_data *)cep->mpa.pdata;
651 
652 		/*
653 		 * Peer requested ORD becomes requested local IRD,
654 		 * peer requested IRD becomes requested local ORD.
655 		 * IRD and ORD get limited by global maximum values.
656 		 */
657 		cep->ord = ntohs(v2->ird) & MPA_IRD_ORD_MASK;
658 		cep->ord = min(cep->ord, SIW_MAX_ORD_QP);
659 		cep->ird = ntohs(v2->ord) & MPA_IRD_ORD_MASK;
660 		cep->ird = min(cep->ird, SIW_MAX_IRD_QP);
661 
662 		/* May get overwritten by locally negotiated values */
663 		cep->mpa.v2_ctrl.ird = htons(cep->ird);
664 		cep->mpa.v2_ctrl.ord = htons(cep->ord);
665 
666 		/*
667 		 * Support for peer sent zero length Write or Read to
668 		 * let local side enter RTS. Writes are preferred.
669 		 * Sends would require pre-posting a Receive and are
670 		 * not supported.
671 		 * Propose zero length Write if none of Read and Write
672 		 * is indicated.
673 		 */
674 		if (v2->ird & MPA_V2_PEER_TO_PEER) {
675 			cep->mpa.v2_ctrl.ird |= MPA_V2_PEER_TO_PEER;
676 
677 			if (v2->ord & MPA_V2_RDMA_WRITE_RTR)
678 				cep->mpa.v2_ctrl.ord |= MPA_V2_RDMA_WRITE_RTR;
679 			else if (v2->ord & MPA_V2_RDMA_READ_RTR)
680 				cep->mpa.v2_ctrl.ord |= MPA_V2_RDMA_READ_RTR;
681 			else
682 				cep->mpa.v2_ctrl.ord |= MPA_V2_RDMA_WRITE_RTR;
683 		}
684 	}
685 
686 	cep->state = SIW_EPSTATE_RECVD_MPAREQ;
687 
688 	/* Keep reference until IWCM accepts/rejects */
689 	siw_cep_get(cep);
690 	rv = siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REQUEST, 0);
691 	if (rv)
692 		siw_cep_put(cep);
693 
694 	return rv;
695 
696 reject_conn:
697 	siw_dbg_cep(cep, "reject: crc %d:%d:%d, m %d:%d\n",
698 		    req->params.bits & MPA_RR_FLAG_CRC ? 1 : 0,
699 		    mpa_crc_required, mpa_crc_strict,
700 		    req->params.bits & MPA_RR_FLAG_MARKERS ? 1 : 0, 0);
701 
702 	req->params.bits &= ~MPA_RR_FLAG_MARKERS;
703 	req->params.bits |= MPA_RR_FLAG_REJECT;
704 
705 	if (!mpa_crc_required && mpa_crc_strict)
706 		req->params.bits &= ~MPA_RR_FLAG_CRC;
707 
708 	if (pd_len)
709 		kfree(cep->mpa.pdata);
710 
711 	cep->mpa.pdata = NULL;
712 
713 	siw_send_mpareqrep(cep, NULL, 0);
714 
715 	return -EOPNOTSUPP;
716 }
717 
718 static int siw_proc_mpareply(struct siw_cep *cep)
719 {
720 	struct siw_qp_attrs qp_attrs;
721 	enum siw_qp_attr_mask qp_attr_mask;
722 	struct siw_qp *qp = cep->qp;
723 	struct mpa_rr *rep;
724 	int rv;
725 	u16 rep_ord;
726 	u16 rep_ird;
727 	bool ird_insufficient = false;
728 	enum mpa_v2_ctrl mpa_p2p_mode = MPA_V2_RDMA_NO_RTR;
729 
730 	rv = siw_recv_mpa_rr(cep);
731 	if (rv)
732 		goto out_err;
733 
734 	siw_cancel_mpatimer(cep);
735 
736 	rep = &cep->mpa.hdr;
737 
738 	if (__mpa_rr_revision(rep->params.bits) > MPA_REVISION_2) {
739 		/* allow for 0, 1,  and 2 only */
740 		rv = -EPROTO;
741 		goto out_err;
742 	}
743 	if (memcmp(rep->key, MPA_KEY_REP, 16)) {
744 		siw_init_terminate(qp, TERM_ERROR_LAYER_LLP, LLP_ETYPE_MPA,
745 				   LLP_ECODE_INVALID_REQ_RESP, 0);
746 		siw_send_terminate(qp);
747 		rv = -EPROTO;
748 		goto out_err;
749 	}
750 	if (rep->params.bits & MPA_RR_FLAG_REJECT) {
751 		siw_dbg_cep(cep, "got mpa reject\n");
752 		siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, -ECONNRESET);
753 
754 		return -ECONNRESET;
755 	}
756 	if (try_gso && rep->params.bits & MPA_RR_FLAG_GSO_EXP) {
757 		siw_dbg_cep(cep, "peer allows GSO on TX\n");
758 		qp->tx_ctx.gso_seg_limit = 0;
759 	}
760 	if ((rep->params.bits & MPA_RR_FLAG_MARKERS) ||
761 	    (mpa_crc_required && !(rep->params.bits & MPA_RR_FLAG_CRC)) ||
762 	    (mpa_crc_strict && !mpa_crc_required &&
763 	     (rep->params.bits & MPA_RR_FLAG_CRC))) {
764 		siw_dbg_cep(cep, "reply unsupp: crc %d:%d:%d, m %d:%d\n",
765 			    rep->params.bits & MPA_RR_FLAG_CRC ? 1 : 0,
766 			    mpa_crc_required, mpa_crc_strict,
767 			    rep->params.bits & MPA_RR_FLAG_MARKERS ? 1 : 0, 0);
768 
769 		siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, -ECONNREFUSED);
770 
771 		return -EINVAL;
772 	}
773 	if (cep->enhanced_rdma_conn_est) {
774 		struct mpa_v2_data *v2;
775 
776 		if (__mpa_rr_revision(rep->params.bits) < MPA_REVISION_2 ||
777 		    !(rep->params.bits & MPA_RR_FLAG_ENHANCED)) {
778 			/*
779 			 * Protocol failure: The responder MUST reply with
780 			 * MPA version 2 and MUST set MPA_RR_FLAG_ENHANCED.
781 			 */
782 			siw_dbg_cep(cep, "mpa reply error: vers %d, enhcd %d\n",
783 				    __mpa_rr_revision(rep->params.bits),
784 				    rep->params.bits & MPA_RR_FLAG_ENHANCED ?
785 					    1 :
786 					    0);
787 
788 			siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
789 				      -ECONNRESET);
790 			return -EINVAL;
791 		}
792 		v2 = (struct mpa_v2_data *)cep->mpa.pdata;
793 		rep_ird = ntohs(v2->ird) & MPA_IRD_ORD_MASK;
794 		rep_ord = ntohs(v2->ord) & MPA_IRD_ORD_MASK;
795 
796 		if (cep->ird < rep_ord &&
797 		    (relaxed_ird_negotiation == false ||
798 		     rep_ord > cep->sdev->attrs.max_ird)) {
799 			siw_dbg_cep(cep, "ird %d, rep_ord %d, max_ord %d\n",
800 				    cep->ird, rep_ord,
801 				    cep->sdev->attrs.max_ord);
802 			ird_insufficient = true;
803 		}
804 		if (cep->ord > rep_ird && relaxed_ird_negotiation == false) {
805 			siw_dbg_cep(cep, "ord %d, rep_ird %d\n", cep->ord,
806 				    rep_ird);
807 			ird_insufficient = true;
808 		}
809 		/*
810 		 * Always report negotiated peer values to user,
811 		 * even if IRD/ORD negotiation failed
812 		 */
813 		cep->ird = rep_ord;
814 		cep->ord = rep_ird;
815 
816 		if (ird_insufficient) {
817 			/*
818 			 * If the initiator IRD is insuffient for the
819 			 * responder ORD, send a TERM.
820 			 */
821 			siw_init_terminate(qp, TERM_ERROR_LAYER_LLP,
822 					   LLP_ETYPE_MPA,
823 					   LLP_ECODE_INSUFFICIENT_IRD, 0);
824 			siw_send_terminate(qp);
825 			rv = -ENOMEM;
826 			goto out_err;
827 		}
828 		if (cep->mpa.v2_ctrl_req.ird & MPA_V2_PEER_TO_PEER)
829 			mpa_p2p_mode =
830 				cep->mpa.v2_ctrl_req.ord &
831 				(MPA_V2_RDMA_WRITE_RTR | MPA_V2_RDMA_READ_RTR);
832 
833 		/*
834 		 * Check if we requested P2P mode, and if peer agrees
835 		 */
836 		if (mpa_p2p_mode != MPA_V2_RDMA_NO_RTR) {
837 			if ((mpa_p2p_mode & v2->ord) == 0) {
838 				/*
839 				 * We requested RTR mode(s), but the peer
840 				 * did not pick any mode we support.
841 				 */
842 				siw_dbg_cep(cep,
843 					    "rtr mode:  req %2x, got %2x\n",
844 					    mpa_p2p_mode,
845 					    v2->ord & (MPA_V2_RDMA_WRITE_RTR |
846 						       MPA_V2_RDMA_READ_RTR));
847 
848 				siw_init_terminate(qp, TERM_ERROR_LAYER_LLP,
849 						   LLP_ETYPE_MPA,
850 						   LLP_ECODE_NO_MATCHING_RTR,
851 						   0);
852 				siw_send_terminate(qp);
853 				rv = -EPROTO;
854 				goto out_err;
855 			}
856 			mpa_p2p_mode = v2->ord & (MPA_V2_RDMA_WRITE_RTR |
857 						  MPA_V2_RDMA_READ_RTR);
858 		}
859 	}
860 	memset(&qp_attrs, 0, sizeof(qp_attrs));
861 
862 	if (rep->params.bits & MPA_RR_FLAG_CRC)
863 		qp_attrs.flags = SIW_MPA_CRC;
864 
865 	qp_attrs.irq_size = cep->ird;
866 	qp_attrs.orq_size = cep->ord;
867 	qp_attrs.sk = cep->sock;
868 	qp_attrs.state = SIW_QP_STATE_RTS;
869 
870 	qp_attr_mask = SIW_QP_ATTR_STATE | SIW_QP_ATTR_LLP_HANDLE |
871 		       SIW_QP_ATTR_ORD | SIW_QP_ATTR_IRD | SIW_QP_ATTR_MPA;
872 
873 	/* Move socket RX/TX under QP control */
874 	down_write(&qp->state_lock);
875 	if (qp->attrs.state > SIW_QP_STATE_RTR) {
876 		rv = -EINVAL;
877 		up_write(&qp->state_lock);
878 		goto out_err;
879 	}
880 	rv = siw_qp_modify(qp, &qp_attrs, qp_attr_mask);
881 
882 	siw_qp_socket_assoc(cep, qp);
883 
884 	up_write(&qp->state_lock);
885 
886 	/* Send extra RDMA frame to trigger peer RTS if negotiated */
887 	if (mpa_p2p_mode != MPA_V2_RDMA_NO_RTR) {
888 		rv = siw_qp_mpa_rts(qp, mpa_p2p_mode);
889 		if (rv)
890 			goto out_err;
891 	}
892 	if (!rv) {
893 		rv = siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, 0);
894 		if (!rv)
895 			cep->state = SIW_EPSTATE_RDMA_MODE;
896 
897 		return 0;
898 	}
899 
900 out_err:
901 	if (rv != -EAGAIN)
902 		siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, -EINVAL);
903 
904 	return rv;
905 }
906 
907 /*
908  * siw_accept_newconn - accept an incoming pending connection
909  *
910  */
911 static void siw_accept_newconn(struct siw_cep *cep)
912 {
913 	struct socket *s = cep->sock;
914 	struct socket *new_s = NULL;
915 	struct siw_cep *new_cep = NULL;
916 	int rv = 0; /* debug only. should disappear */
917 
918 	if (cep->state != SIW_EPSTATE_LISTENING)
919 		goto error;
920 
921 	new_cep = siw_cep_alloc(cep->sdev);
922 	if (!new_cep)
923 		goto error;
924 
925 	/*
926 	 * 4: Allocate a sufficient number of work elements
927 	 * to allow concurrent handling of local + peer close
928 	 * events, MPA header processing + MPA timeout.
929 	 */
930 	if (siw_cm_alloc_work(new_cep, 4) != 0)
931 		goto error;
932 
933 	/*
934 	 * Copy saved socket callbacks from listening CEP
935 	 * and assign new socket with new CEP
936 	 */
937 	new_cep->sk_state_change = cep->sk_state_change;
938 	new_cep->sk_data_ready = cep->sk_data_ready;
939 	new_cep->sk_write_space = cep->sk_write_space;
940 	new_cep->sk_error_report = cep->sk_error_report;
941 
942 	rv = kernel_accept(s, &new_s, O_NONBLOCK);
943 	if (rv != 0) {
944 		/*
945 		 * Connection already aborted by peer..?
946 		 */
947 		siw_dbg_cep(cep, "kernel_accept() error: %d\n", rv);
948 		goto error;
949 	}
950 	new_cep->sock = new_s;
951 	siw_cep_get(new_cep);
952 	new_s->sk->sk_user_data = new_cep;
953 
954 	if (siw_tcp_nagle == false)
955 		tcp_sock_set_nodelay(new_s->sk);
956 	new_cep->state = SIW_EPSTATE_AWAIT_MPAREQ;
957 
958 	rv = siw_cm_queue_work(new_cep, SIW_CM_WORK_MPATIMEOUT);
959 	if (rv)
960 		goto error;
961 	/*
962 	 * See siw_proc_mpareq() etc. for the use of new_cep->listen_cep.
963 	 */
964 	new_cep->listen_cep = cep;
965 	siw_cep_get(cep);
966 
967 	if (atomic_read(&new_s->sk->sk_rmem_alloc)) {
968 		/*
969 		 * MPA REQ already queued
970 		 */
971 		siw_dbg_cep(cep, "immediate mpa request\n");
972 
973 		siw_cep_set_inuse(new_cep);
974 		rv = siw_proc_mpareq(new_cep);
975 		if (rv != -EAGAIN) {
976 			siw_cep_put(cep);
977 			new_cep->listen_cep = NULL;
978 			if (rv) {
979 				siw_cep_set_free(new_cep);
980 				goto error;
981 			}
982 		}
983 		siw_cep_set_free(new_cep);
984 	}
985 	return;
986 
987 error:
988 	if (new_cep)
989 		siw_cep_put(new_cep);
990 
991 	if (new_s) {
992 		siw_socket_disassoc(new_s);
993 		sock_release(new_s);
994 		new_cep->sock = NULL;
995 	}
996 	siw_dbg_cep(cep, "error %d\n", rv);
997 }
998 
999 static void siw_cm_work_handler(struct work_struct *w)
1000 {
1001 	struct siw_cm_work *work;
1002 	struct siw_cep *cep;
1003 	int release_cep = 0, rv = 0;
1004 
1005 	work = container_of(w, struct siw_cm_work, work.work);
1006 	cep = work->cep;
1007 
1008 	siw_dbg_cep(cep, "[QP %u]: work type: %d, state %d\n",
1009 		    cep->qp ? qp_id(cep->qp) : UINT_MAX,
1010 		    work->type, cep->state);
1011 
1012 	siw_cep_set_inuse(cep);
1013 
1014 	switch (work->type) {
1015 	case SIW_CM_WORK_ACCEPT:
1016 		siw_accept_newconn(cep);
1017 		break;
1018 
1019 	case SIW_CM_WORK_READ_MPAHDR:
1020 		if (cep->state == SIW_EPSTATE_AWAIT_MPAREQ) {
1021 			if (cep->listen_cep) {
1022 				siw_cep_set_inuse(cep->listen_cep);
1023 
1024 				if (cep->listen_cep->state ==
1025 				    SIW_EPSTATE_LISTENING)
1026 					rv = siw_proc_mpareq(cep);
1027 				else
1028 					rv = -EFAULT;
1029 
1030 				siw_cep_set_free(cep->listen_cep);
1031 
1032 				if (rv != -EAGAIN) {
1033 					siw_cep_put(cep->listen_cep);
1034 					cep->listen_cep = NULL;
1035 					if (rv)
1036 						siw_cep_put(cep);
1037 				}
1038 			}
1039 		} else if (cep->state == SIW_EPSTATE_AWAIT_MPAREP) {
1040 			rv = siw_proc_mpareply(cep);
1041 		} else {
1042 			/*
1043 			 * CEP already moved out of MPA handshake.
1044 			 * any connection management already done.
1045 			 * silently ignore the mpa packet.
1046 			 */
1047 			if (cep->state == SIW_EPSTATE_RDMA_MODE) {
1048 				cep->sock->sk->sk_data_ready(cep->sock->sk);
1049 				siw_dbg_cep(cep, "already in RDMA mode");
1050 			} else {
1051 				siw_dbg_cep(cep, "out of state: %d\n",
1052 					    cep->state);
1053 			}
1054 		}
1055 		if (rv && rv != -EAGAIN)
1056 			release_cep = 1;
1057 		break;
1058 
1059 	case SIW_CM_WORK_CLOSE_LLP:
1060 		/*
1061 		 * QP scheduled LLP close
1062 		 */
1063 		if (cep->qp && cep->qp->term_info.valid)
1064 			siw_send_terminate(cep->qp);
1065 
1066 		if (cep->cm_id)
1067 			siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
1068 
1069 		release_cep = 1;
1070 		break;
1071 
1072 	case SIW_CM_WORK_PEER_CLOSE:
1073 		if (cep->cm_id) {
1074 			if (cep->state == SIW_EPSTATE_AWAIT_MPAREP) {
1075 				/*
1076 				 * MPA reply not received, but connection drop
1077 				 */
1078 				siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
1079 					      -ECONNRESET);
1080 			} else if (cep->state == SIW_EPSTATE_RDMA_MODE) {
1081 				/*
1082 				 * NOTE: IW_CM_EVENT_DISCONNECT is given just
1083 				 *       to transition IWCM into CLOSING.
1084 				 */
1085 				siw_cm_upcall(cep, IW_CM_EVENT_DISCONNECT, 0);
1086 				siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
1087 			}
1088 			/*
1089 			 * for other states there is no connection
1090 			 * known to the IWCM.
1091 			 */
1092 		} else {
1093 			if (cep->state == SIW_EPSTATE_RECVD_MPAREQ) {
1094 				/*
1095 				 * Wait for the ulp/CM to call accept/reject
1096 				 */
1097 				siw_dbg_cep(cep,
1098 					    "mpa req recvd, wait for ULP\n");
1099 			} else if (cep->state == SIW_EPSTATE_AWAIT_MPAREQ) {
1100 				/*
1101 				 * Socket close before MPA request received.
1102 				 */
1103 				siw_dbg_cep(cep, "no mpareq: drop listener\n");
1104 				siw_cep_put(cep->listen_cep);
1105 				cep->listen_cep = NULL;
1106 			}
1107 		}
1108 		release_cep = 1;
1109 		break;
1110 
1111 	case SIW_CM_WORK_MPATIMEOUT:
1112 		cep->mpa_timer = NULL;
1113 
1114 		if (cep->state == SIW_EPSTATE_AWAIT_MPAREP) {
1115 			/*
1116 			 * MPA request timed out:
1117 			 * Hide any partially received private data and signal
1118 			 * timeout
1119 			 */
1120 			cep->mpa.hdr.params.pd_len = 0;
1121 
1122 			if (cep->cm_id)
1123 				siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
1124 					      -ETIMEDOUT);
1125 			release_cep = 1;
1126 
1127 		} else if (cep->state == SIW_EPSTATE_AWAIT_MPAREQ) {
1128 			/*
1129 			 * No MPA request received after peer TCP stream setup.
1130 			 */
1131 			if (cep->listen_cep) {
1132 				siw_cep_put(cep->listen_cep);
1133 				cep->listen_cep = NULL;
1134 			}
1135 			release_cep = 1;
1136 		}
1137 		break;
1138 
1139 	default:
1140 		WARN(1, "Undefined CM work type: %d\n", work->type);
1141 	}
1142 	if (release_cep) {
1143 		siw_dbg_cep(cep,
1144 			    "release: timer=%s, QP[%u]\n",
1145 			    cep->mpa_timer ? "y" : "n",
1146 			    cep->qp ? qp_id(cep->qp) : UINT_MAX);
1147 
1148 		siw_cancel_mpatimer(cep);
1149 
1150 		cep->state = SIW_EPSTATE_CLOSED;
1151 
1152 		if (cep->qp) {
1153 			struct siw_qp *qp = cep->qp;
1154 			/*
1155 			 * Serialize a potential race with application
1156 			 * closing the QP and calling siw_qp_cm_drop()
1157 			 */
1158 			siw_qp_get(qp);
1159 			siw_cep_set_free(cep);
1160 
1161 			siw_qp_llp_close(qp);
1162 			siw_qp_put(qp);
1163 
1164 			siw_cep_set_inuse(cep);
1165 			cep->qp = NULL;
1166 			siw_qp_put(qp);
1167 		}
1168 		if (cep->sock) {
1169 			siw_socket_disassoc(cep->sock);
1170 			sock_release(cep->sock);
1171 			cep->sock = NULL;
1172 		}
1173 		if (cep->cm_id) {
1174 			cep->cm_id->rem_ref(cep->cm_id);
1175 			cep->cm_id = NULL;
1176 			siw_cep_put(cep);
1177 		}
1178 	}
1179 	siw_cep_set_free(cep);
1180 	siw_put_work(work);
1181 	siw_cep_put(cep);
1182 }
1183 
1184 static struct workqueue_struct *siw_cm_wq;
1185 
1186 int siw_cm_queue_work(struct siw_cep *cep, enum siw_work_type type)
1187 {
1188 	struct siw_cm_work *work = siw_get_work(cep);
1189 	unsigned long delay = 0;
1190 
1191 	if (!work) {
1192 		siw_dbg_cep(cep, "failed with no work available\n");
1193 		return -ENOMEM;
1194 	}
1195 	work->type = type;
1196 	work->cep = cep;
1197 
1198 	siw_cep_get(cep);
1199 
1200 	INIT_DELAYED_WORK(&work->work, siw_cm_work_handler);
1201 
1202 	if (type == SIW_CM_WORK_MPATIMEOUT) {
1203 		cep->mpa_timer = work;
1204 
1205 		if (cep->state == SIW_EPSTATE_AWAIT_MPAREP)
1206 			delay = MPAREQ_TIMEOUT;
1207 		else
1208 			delay = MPAREP_TIMEOUT;
1209 	}
1210 	siw_dbg_cep(cep, "[QP %u]: work type: %d, timeout %lu\n",
1211 		    cep->qp ? qp_id(cep->qp) : -1, type, delay);
1212 
1213 	queue_delayed_work(siw_cm_wq, &work->work, delay);
1214 
1215 	return 0;
1216 }
1217 
1218 static void siw_cm_llp_data_ready(struct sock *sk)
1219 {
1220 	struct siw_cep *cep;
1221 
1222 	trace_sk_data_ready(sk);
1223 
1224 	read_lock(&sk->sk_callback_lock);
1225 
1226 	cep = sk_to_cep(sk);
1227 	if (!cep)
1228 		goto out;
1229 
1230 	siw_dbg_cep(cep, "state: %d\n", cep->state);
1231 
1232 	switch (cep->state) {
1233 	case SIW_EPSTATE_RDMA_MODE:
1234 	case SIW_EPSTATE_LISTENING:
1235 		break;
1236 
1237 	case SIW_EPSTATE_AWAIT_MPAREQ:
1238 	case SIW_EPSTATE_AWAIT_MPAREP:
1239 		siw_cm_queue_work(cep, SIW_CM_WORK_READ_MPAHDR);
1240 		break;
1241 
1242 	default:
1243 		siw_dbg_cep(cep, "unexpected data, state %d\n", cep->state);
1244 		break;
1245 	}
1246 out:
1247 	read_unlock(&sk->sk_callback_lock);
1248 }
1249 
1250 static void siw_cm_llp_write_space(struct sock *sk)
1251 {
1252 	struct siw_cep *cep = sk_to_cep(sk);
1253 
1254 	if (cep)
1255 		siw_dbg_cep(cep, "state: %d\n", cep->state);
1256 }
1257 
1258 static void siw_cm_llp_error_report(struct sock *sk)
1259 {
1260 	struct siw_cep *cep = sk_to_cep(sk);
1261 
1262 	if (cep) {
1263 		siw_dbg_cep(cep, "error %d, socket state: %d, cep state: %d\n",
1264 			    sk->sk_err, sk->sk_state, cep->state);
1265 		cep->sk_error_report(sk);
1266 	}
1267 }
1268 
1269 static void siw_cm_llp_state_change(struct sock *sk)
1270 {
1271 	struct siw_cep *cep;
1272 	void (*orig_state_change)(struct sock *s);
1273 
1274 	read_lock(&sk->sk_callback_lock);
1275 
1276 	cep = sk_to_cep(sk);
1277 	if (!cep) {
1278 		/* endpoint already disassociated */
1279 		read_unlock(&sk->sk_callback_lock);
1280 		return;
1281 	}
1282 	orig_state_change = cep->sk_state_change;
1283 
1284 	siw_dbg_cep(cep, "state: %d\n", cep->state);
1285 
1286 	switch (sk->sk_state) {
1287 	case TCP_ESTABLISHED:
1288 		/*
1289 		 * handle accepting socket as special case where only
1290 		 * new connection is possible
1291 		 */
1292 		siw_cm_queue_work(cep, SIW_CM_WORK_ACCEPT);
1293 		break;
1294 
1295 	case TCP_CLOSE:
1296 	case TCP_CLOSE_WAIT:
1297 		if (cep->qp)
1298 			cep->qp->tx_ctx.tx_suspend = 1;
1299 		siw_cm_queue_work(cep, SIW_CM_WORK_PEER_CLOSE);
1300 		break;
1301 
1302 	default:
1303 		siw_dbg_cep(cep, "unexpected socket state %d\n", sk->sk_state);
1304 	}
1305 	read_unlock(&sk->sk_callback_lock);
1306 	orig_state_change(sk);
1307 }
1308 
1309 static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr,
1310 			      struct sockaddr *raddr, bool afonly)
1311 {
1312 	int rv, flags = 0;
1313 	size_t size = laddr->sa_family == AF_INET ?
1314 		sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
1315 
1316 	/*
1317 	 * Make address available again asap.
1318 	 */
1319 	sock_set_reuseaddr(s->sk);
1320 
1321 	if (afonly) {
1322 		rv = ip6_sock_set_v6only(s->sk);
1323 		if (rv)
1324 			return rv;
1325 	}
1326 
1327 	rv = s->ops->bind(s, laddr, size);
1328 	if (rv < 0)
1329 		return rv;
1330 
1331 	rv = s->ops->connect(s, raddr, size, flags);
1332 
1333 	return rv < 0 ? rv : 0;
1334 }
1335 
1336 int siw_connect(struct iw_cm_id *id, struct iw_cm_conn_param *params)
1337 {
1338 	struct siw_device *sdev = to_siw_dev(id->device);
1339 	struct siw_qp *qp;
1340 	struct siw_cep *cep = NULL;
1341 	struct socket *s = NULL;
1342 	struct sockaddr *laddr = (struct sockaddr *)&id->local_addr,
1343 			*raddr = (struct sockaddr *)&id->remote_addr;
1344 	bool p2p_mode = peer_to_peer, v4 = true;
1345 	u16 pd_len = params->private_data_len;
1346 	int version = mpa_version, rv;
1347 
1348 	if (pd_len > MPA_MAX_PRIVDATA)
1349 		return -EINVAL;
1350 
1351 	if (params->ird > sdev->attrs.max_ird ||
1352 	    params->ord > sdev->attrs.max_ord)
1353 		return -ENOMEM;
1354 
1355 	if (laddr->sa_family == AF_INET6)
1356 		v4 = false;
1357 	else if (laddr->sa_family != AF_INET)
1358 		return -EAFNOSUPPORT;
1359 
1360 	/*
1361 	 * Respect any iwarp port mapping: Use mapped remote address
1362 	 * if valid. Local address must not be mapped, since siw
1363 	 * uses kernel TCP stack.
1364 	 */
1365 	if ((v4 && to_sockaddr_in(id->remote_addr).sin_port != 0) ||
1366 	     to_sockaddr_in6(id->remote_addr).sin6_port != 0)
1367 		raddr = (struct sockaddr *)&id->m_remote_addr;
1368 
1369 	qp = siw_qp_id2obj(sdev, params->qpn);
1370 	if (!qp) {
1371 		WARN(1, "[QP %u] does not exist\n", params->qpn);
1372 		rv = -EINVAL;
1373 		goto error;
1374 	}
1375 	siw_dbg_qp(qp, "pd_len %d, laddr %pISp, raddr %pISp\n", pd_len, laddr,
1376 		   raddr);
1377 
1378 	rv = sock_create(v4 ? AF_INET : AF_INET6, SOCK_STREAM, IPPROTO_TCP, &s);
1379 	if (rv < 0)
1380 		goto error;
1381 
1382 	/*
1383 	 * NOTE: For simplification, connect() is called in blocking
1384 	 * mode. Might be reconsidered for async connection setup at
1385 	 * TCP level.
1386 	 */
1387 	rv = kernel_bindconnect(s, laddr, raddr, id->afonly);
1388 	if (rv != 0) {
1389 		siw_dbg_qp(qp, "kernel_bindconnect: error %d\n", rv);
1390 		goto error;
1391 	}
1392 	if (siw_tcp_nagle == false)
1393 		tcp_sock_set_nodelay(s->sk);
1394 	cep = siw_cep_alloc(sdev);
1395 	if (!cep) {
1396 		rv = -ENOMEM;
1397 		goto error;
1398 	}
1399 	siw_cep_set_inuse(cep);
1400 
1401 	/* Associate QP with CEP */
1402 	siw_cep_get(cep);
1403 	qp->cep = cep;
1404 
1405 	/* siw_qp_get(qp) already done by QP lookup */
1406 	cep->qp = qp;
1407 
1408 	id->add_ref(id);
1409 	cep->cm_id = id;
1410 
1411 	/*
1412 	 * 4: Allocate a sufficient number of work elements
1413 	 * to allow concurrent handling of local + peer close
1414 	 * events, MPA header processing + MPA timeout.
1415 	 */
1416 	rv = siw_cm_alloc_work(cep, 4);
1417 	if (rv != 0) {
1418 		rv = -ENOMEM;
1419 		goto error;
1420 	}
1421 	cep->ird = params->ird;
1422 	cep->ord = params->ord;
1423 
1424 	if (p2p_mode && cep->ord == 0)
1425 		cep->ord = 1;
1426 
1427 	cep->state = SIW_EPSTATE_CONNECTING;
1428 
1429 	/*
1430 	 * Associate CEP with socket
1431 	 */
1432 	siw_cep_socket_assoc(cep, s);
1433 
1434 	cep->state = SIW_EPSTATE_AWAIT_MPAREP;
1435 
1436 	/*
1437 	 * Set MPA Request bits: CRC if required, no MPA Markers,
1438 	 * MPA Rev. according to module parameter 'mpa_version', Key 'Request'.
1439 	 */
1440 	cep->mpa.hdr.params.bits = 0;
1441 	if (version > MPA_REVISION_2) {
1442 		pr_warn("Setting MPA version to %u\n", MPA_REVISION_2);
1443 		version = MPA_REVISION_2;
1444 		/* Adjust also module parameter */
1445 		mpa_version = MPA_REVISION_2;
1446 	}
1447 	__mpa_rr_set_revision(&cep->mpa.hdr.params.bits, version);
1448 
1449 	if (try_gso)
1450 		cep->mpa.hdr.params.bits |= MPA_RR_FLAG_GSO_EXP;
1451 
1452 	if (mpa_crc_required)
1453 		cep->mpa.hdr.params.bits |= MPA_RR_FLAG_CRC;
1454 
1455 	/*
1456 	 * If MPA version == 2:
1457 	 * o Include ORD and IRD.
1458 	 * o Indicate peer-to-peer mode, if required by module
1459 	 *   parameter 'peer_to_peer'.
1460 	 */
1461 	if (version == MPA_REVISION_2) {
1462 		cep->enhanced_rdma_conn_est = true;
1463 		cep->mpa.hdr.params.bits |= MPA_RR_FLAG_ENHANCED;
1464 
1465 		cep->mpa.v2_ctrl.ird = htons(cep->ird);
1466 		cep->mpa.v2_ctrl.ord = htons(cep->ord);
1467 
1468 		if (p2p_mode) {
1469 			cep->mpa.v2_ctrl.ird |= MPA_V2_PEER_TO_PEER;
1470 			cep->mpa.v2_ctrl.ord |= rtr_type;
1471 		}
1472 		/* Remember own P2P mode requested */
1473 		cep->mpa.v2_ctrl_req.ird = cep->mpa.v2_ctrl.ird;
1474 		cep->mpa.v2_ctrl_req.ord = cep->mpa.v2_ctrl.ord;
1475 	}
1476 	memcpy(cep->mpa.hdr.key, MPA_KEY_REQ, 16);
1477 
1478 	rv = siw_send_mpareqrep(cep, params->private_data, pd_len);
1479 	/*
1480 	 * Reset private data.
1481 	 */
1482 	cep->mpa.hdr.params.pd_len = 0;
1483 
1484 	if (rv >= 0) {
1485 		rv = siw_cm_queue_work(cep, SIW_CM_WORK_MPATIMEOUT);
1486 		if (!rv) {
1487 			siw_dbg_cep(cep, "[QP %u]: exit\n", qp_id(qp));
1488 			siw_cep_set_free(cep);
1489 			return 0;
1490 		}
1491 	}
1492 error:
1493 	siw_dbg(id->device, "failed: %d\n", rv);
1494 
1495 	if (cep) {
1496 		siw_socket_disassoc(s);
1497 		sock_release(s);
1498 		cep->sock = NULL;
1499 
1500 		cep->qp = NULL;
1501 
1502 		cep->cm_id = NULL;
1503 		id->rem_ref(id);
1504 
1505 		qp->cep = NULL;
1506 		siw_cep_put(cep);
1507 
1508 		cep->state = SIW_EPSTATE_CLOSED;
1509 
1510 		siw_cep_set_free(cep);
1511 
1512 		siw_cep_put(cep);
1513 
1514 	} else if (s) {
1515 		sock_release(s);
1516 	}
1517 	if (qp)
1518 		siw_qp_put(qp);
1519 
1520 	return rv;
1521 }
1522 
1523 /*
1524  * siw_accept - Let SoftiWARP accept an RDMA connection request
1525  *
1526  * @id:		New connection management id to be used for accepted
1527  *		connection request
1528  * @params:	Connection parameters provided by ULP for accepting connection
1529  *
1530  * Transition QP to RTS state, associate new CM id @id with accepted CEP
1531  * and get prepared for TCP input by installing socket callbacks.
1532  * Then send MPA Reply and generate the "connection established" event.
1533  * Socket callbacks must be installed before sending MPA Reply, because
1534  * the latter may cause a first RDMA message to arrive from the RDMA Initiator
1535  * side very quickly, at which time the socket callbacks must be ready.
1536  */
1537 int siw_accept(struct iw_cm_id *id, struct iw_cm_conn_param *params)
1538 {
1539 	struct siw_device *sdev = to_siw_dev(id->device);
1540 	struct siw_cep *cep = (struct siw_cep *)id->provider_data;
1541 	struct siw_qp *qp;
1542 	struct siw_qp_attrs qp_attrs;
1543 	int rv, max_priv_data = MPA_MAX_PRIVDATA;
1544 	bool wait_for_peer_rts = false;
1545 
1546 	siw_cep_set_inuse(cep);
1547 	siw_cep_put(cep);
1548 
1549 	/* Free lingering inbound private data */
1550 	if (cep->mpa.hdr.params.pd_len) {
1551 		cep->mpa.hdr.params.pd_len = 0;
1552 		kfree(cep->mpa.pdata);
1553 		cep->mpa.pdata = NULL;
1554 	}
1555 	siw_cancel_mpatimer(cep);
1556 
1557 	if (cep->state != SIW_EPSTATE_RECVD_MPAREQ) {
1558 		siw_dbg_cep(cep, "out of state\n");
1559 
1560 		siw_cep_set_free(cep);
1561 		siw_cep_put(cep);
1562 
1563 		return -ECONNRESET;
1564 	}
1565 	qp = siw_qp_id2obj(sdev, params->qpn);
1566 	if (!qp) {
1567 		WARN(1, "[QP %d] does not exist\n", params->qpn);
1568 		siw_cep_set_free(cep);
1569 		siw_cep_put(cep);
1570 
1571 		return -EINVAL;
1572 	}
1573 	down_write(&qp->state_lock);
1574 	if (qp->attrs.state > SIW_QP_STATE_RTR) {
1575 		rv = -EINVAL;
1576 		up_write(&qp->state_lock);
1577 		goto error;
1578 	}
1579 	siw_dbg_cep(cep, "[QP %d]\n", params->qpn);
1580 
1581 	if (try_gso && cep->mpa.hdr.params.bits & MPA_RR_FLAG_GSO_EXP) {
1582 		siw_dbg_cep(cep, "peer allows GSO on TX\n");
1583 		qp->tx_ctx.gso_seg_limit = 0;
1584 	}
1585 	if (params->ord > sdev->attrs.max_ord ||
1586 	    params->ird > sdev->attrs.max_ird) {
1587 		siw_dbg_cep(
1588 			cep,
1589 			"[QP %u]: ord %d (max %d), ird %d (max %d)\n",
1590 			qp_id(qp), params->ord, sdev->attrs.max_ord,
1591 			params->ird, sdev->attrs.max_ird);
1592 		rv = -EINVAL;
1593 		up_write(&qp->state_lock);
1594 		goto error;
1595 	}
1596 	if (cep->enhanced_rdma_conn_est)
1597 		max_priv_data -= sizeof(struct mpa_v2_data);
1598 
1599 	if (params->private_data_len > max_priv_data) {
1600 		siw_dbg_cep(
1601 			cep,
1602 			"[QP %u]: private data length: %d (max %d)\n",
1603 			qp_id(qp), params->private_data_len, max_priv_data);
1604 		rv = -EINVAL;
1605 		up_write(&qp->state_lock);
1606 		goto error;
1607 	}
1608 	if (cep->enhanced_rdma_conn_est) {
1609 		if (params->ord > cep->ord) {
1610 			if (relaxed_ird_negotiation) {
1611 				params->ord = cep->ord;
1612 			} else {
1613 				cep->ird = params->ird;
1614 				cep->ord = params->ord;
1615 				rv = -EINVAL;
1616 				up_write(&qp->state_lock);
1617 				goto error;
1618 			}
1619 		}
1620 		if (params->ird < cep->ird) {
1621 			if (relaxed_ird_negotiation &&
1622 			    cep->ird <= sdev->attrs.max_ird)
1623 				params->ird = cep->ird;
1624 			else {
1625 				rv = -ENOMEM;
1626 				up_write(&qp->state_lock);
1627 				goto error;
1628 			}
1629 		}
1630 		if (cep->mpa.v2_ctrl.ord &
1631 		    (MPA_V2_RDMA_WRITE_RTR | MPA_V2_RDMA_READ_RTR))
1632 			wait_for_peer_rts = true;
1633 		/*
1634 		 * Signal back negotiated IRD and ORD values
1635 		 */
1636 		cep->mpa.v2_ctrl.ord =
1637 			htons(params->ord & MPA_IRD_ORD_MASK) |
1638 			(cep->mpa.v2_ctrl.ord & ~MPA_V2_MASK_IRD_ORD);
1639 		cep->mpa.v2_ctrl.ird =
1640 			htons(params->ird & MPA_IRD_ORD_MASK) |
1641 			(cep->mpa.v2_ctrl.ird & ~MPA_V2_MASK_IRD_ORD);
1642 	}
1643 	cep->ird = params->ird;
1644 	cep->ord = params->ord;
1645 
1646 	cep->cm_id = id;
1647 	id->add_ref(id);
1648 
1649 	memset(&qp_attrs, 0, sizeof(qp_attrs));
1650 	qp_attrs.orq_size = cep->ord;
1651 	qp_attrs.irq_size = cep->ird;
1652 	qp_attrs.sk = cep->sock;
1653 	if (cep->mpa.hdr.params.bits & MPA_RR_FLAG_CRC)
1654 		qp_attrs.flags = SIW_MPA_CRC;
1655 	qp_attrs.state = SIW_QP_STATE_RTS;
1656 
1657 	siw_dbg_cep(cep, "[QP%u]: moving to rts\n", qp_id(qp));
1658 
1659 	/* Associate QP with CEP */
1660 	siw_cep_get(cep);
1661 	qp->cep = cep;
1662 
1663 	/* siw_qp_get(qp) already done by QP lookup */
1664 	cep->qp = qp;
1665 
1666 	cep->state = SIW_EPSTATE_RDMA_MODE;
1667 
1668 	/* Move socket RX/TX under QP control */
1669 	rv = siw_qp_modify(qp, &qp_attrs,
1670 			   SIW_QP_ATTR_STATE | SIW_QP_ATTR_LLP_HANDLE |
1671 				   SIW_QP_ATTR_ORD | SIW_QP_ATTR_IRD |
1672 				   SIW_QP_ATTR_MPA);
1673 	up_write(&qp->state_lock);
1674 
1675 	if (rv)
1676 		goto error;
1677 
1678 	siw_dbg_cep(cep, "[QP %u]: send mpa reply, %d byte pdata\n",
1679 		    qp_id(qp), params->private_data_len);
1680 
1681 	rv = siw_send_mpareqrep(cep, params->private_data,
1682 				params->private_data_len);
1683 	if (rv != 0)
1684 		goto error;
1685 
1686 	if (wait_for_peer_rts) {
1687 		siw_sk_assign_rtr_upcalls(cep);
1688 	} else {
1689 		siw_qp_socket_assoc(cep, qp);
1690 		rv = siw_cm_upcall(cep, IW_CM_EVENT_ESTABLISHED, 0);
1691 		if (rv)
1692 			goto error;
1693 	}
1694 	siw_cep_set_free(cep);
1695 
1696 	return 0;
1697 error:
1698 	siw_socket_disassoc(cep->sock);
1699 	sock_release(cep->sock);
1700 	cep->sock = NULL;
1701 
1702 	cep->state = SIW_EPSTATE_CLOSED;
1703 
1704 	if (cep->cm_id) {
1705 		cep->cm_id->rem_ref(id);
1706 		cep->cm_id = NULL;
1707 	}
1708 	if (qp->cep) {
1709 		siw_cep_put(cep);
1710 		qp->cep = NULL;
1711 	}
1712 	cep->qp = NULL;
1713 	siw_qp_put(qp);
1714 
1715 	siw_cep_set_free(cep);
1716 	siw_cep_put(cep);
1717 
1718 	return rv;
1719 }
1720 
1721 /*
1722  * siw_reject()
1723  *
1724  * Local connection reject case. Send private data back to peer,
1725  * close connection and dereference connection id.
1726  */
1727 int siw_reject(struct iw_cm_id *id, const void *pdata, u8 pd_len)
1728 {
1729 	struct siw_cep *cep = (struct siw_cep *)id->provider_data;
1730 
1731 	siw_cep_set_inuse(cep);
1732 	siw_cep_put(cep);
1733 
1734 	siw_cancel_mpatimer(cep);
1735 
1736 	if (cep->state != SIW_EPSTATE_RECVD_MPAREQ) {
1737 		siw_dbg_cep(cep, "out of state\n");
1738 
1739 		siw_cep_set_free(cep);
1740 		siw_cep_put(cep); /* put last reference */
1741 
1742 		return -ECONNRESET;
1743 	}
1744 	siw_dbg_cep(cep, "cep->state %d, pd_len %d\n", cep->state,
1745 		    pd_len);
1746 
1747 	if (__mpa_rr_revision(cep->mpa.hdr.params.bits) >= MPA_REVISION_1) {
1748 		cep->mpa.hdr.params.bits |= MPA_RR_FLAG_REJECT; /* reject */
1749 		siw_send_mpareqrep(cep, pdata, pd_len);
1750 	}
1751 	siw_socket_disassoc(cep->sock);
1752 	sock_release(cep->sock);
1753 	cep->sock = NULL;
1754 
1755 	cep->state = SIW_EPSTATE_CLOSED;
1756 
1757 	siw_cep_set_free(cep);
1758 	siw_cep_put(cep);
1759 
1760 	return 0;
1761 }
1762 
1763 /*
1764  * siw_create_listen - Create resources for a listener's IWCM ID @id
1765  *
1766  * Starts listen on the socket address id->local_addr.
1767  *
1768  */
1769 int siw_create_listen(struct iw_cm_id *id, int backlog)
1770 {
1771 	struct socket *s;
1772 	struct siw_cep *cep = NULL;
1773 	struct siw_device *sdev = to_siw_dev(id->device);
1774 	int addr_family = id->local_addr.ss_family;
1775 	int rv = 0;
1776 
1777 	if (addr_family != AF_INET && addr_family != AF_INET6)
1778 		return -EAFNOSUPPORT;
1779 
1780 	rv = sock_create(addr_family, SOCK_STREAM, IPPROTO_TCP, &s);
1781 	if (rv < 0)
1782 		return rv;
1783 
1784 	/*
1785 	 * Allow binding local port when still in TIME_WAIT from last close.
1786 	 */
1787 	sock_set_reuseaddr(s->sk);
1788 
1789 	if (addr_family == AF_INET) {
1790 		struct sockaddr_in *laddr = &to_sockaddr_in(id->local_addr);
1791 
1792 		/* For wildcard addr, limit binding to current device only */
1793 		if (ipv4_is_zeronet(laddr->sin_addr.s_addr))
1794 			s->sk->sk_bound_dev_if = sdev->netdev->ifindex;
1795 
1796 		rv = s->ops->bind(s, (struct sockaddr *)laddr,
1797 				  sizeof(struct sockaddr_in));
1798 	} else {
1799 		struct sockaddr_in6 *laddr = &to_sockaddr_in6(id->local_addr);
1800 
1801 		if (id->afonly) {
1802 			rv = ip6_sock_set_v6only(s->sk);
1803 			if (rv) {
1804 				siw_dbg(id->device,
1805 					"ip6_sock_set_v6only erro: %d\n", rv);
1806 				goto error;
1807 			}
1808 		}
1809 
1810 		/* For wildcard addr, limit binding to current device only */
1811 		if (ipv6_addr_any(&laddr->sin6_addr))
1812 			s->sk->sk_bound_dev_if = sdev->netdev->ifindex;
1813 
1814 		rv = s->ops->bind(s, (struct sockaddr *)laddr,
1815 				  sizeof(struct sockaddr_in6));
1816 	}
1817 	if (rv) {
1818 		siw_dbg(id->device, "socket bind error: %d\n", rv);
1819 		goto error;
1820 	}
1821 	cep = siw_cep_alloc(sdev);
1822 	if (!cep) {
1823 		rv = -ENOMEM;
1824 		goto error;
1825 	}
1826 	siw_cep_socket_assoc(cep, s);
1827 
1828 	rv = siw_cm_alloc_work(cep, backlog);
1829 	if (rv) {
1830 		siw_dbg(id->device,
1831 			"alloc_work error %d, backlog %d\n",
1832 			rv, backlog);
1833 		goto error;
1834 	}
1835 	rv = s->ops->listen(s, backlog);
1836 	if (rv) {
1837 		siw_dbg(id->device, "listen error %d\n", rv);
1838 		goto error;
1839 	}
1840 	cep->cm_id = id;
1841 	id->add_ref(id);
1842 
1843 	/*
1844 	 * In case of a wildcard rdma_listen on a multi-homed device,
1845 	 * a listener's IWCM id is associated with more than one listening CEP.
1846 	 *
1847 	 * We currently use id->provider_data in three different ways:
1848 	 *
1849 	 * o For a listener's IWCM id, id->provider_data points to
1850 	 *   the list_head of the list of listening CEPs.
1851 	 *   Uses: siw_create_listen(), siw_destroy_listen()
1852 	 *
1853 	 * o For each accepted passive-side IWCM id, id->provider_data
1854 	 *   points to the CEP itself. This is a consequence of
1855 	 *   - siw_cm_upcall() setting event.provider_data = cep and
1856 	 *   - the IWCM's cm_conn_req_handler() setting provider_data of the
1857 	 *     new passive-side IWCM id equal to event.provider_data
1858 	 *   Uses: siw_accept(), siw_reject()
1859 	 *
1860 	 * o For an active-side IWCM id, id->provider_data is not used at all.
1861 	 *
1862 	 */
1863 	if (!id->provider_data) {
1864 		id->provider_data =
1865 			kmalloc(sizeof(struct list_head), GFP_KERNEL);
1866 		if (!id->provider_data) {
1867 			rv = -ENOMEM;
1868 			goto error;
1869 		}
1870 		INIT_LIST_HEAD((struct list_head *)id->provider_data);
1871 	}
1872 	list_add_tail(&cep->listenq, (struct list_head *)id->provider_data);
1873 	cep->state = SIW_EPSTATE_LISTENING;
1874 
1875 	siw_dbg(id->device, "Listen at laddr %pISp\n", &id->local_addr);
1876 
1877 	return 0;
1878 
1879 error:
1880 	siw_dbg(id->device, "failed: %d\n", rv);
1881 
1882 	if (cep) {
1883 		siw_cep_set_inuse(cep);
1884 
1885 		if (cep->cm_id) {
1886 			cep->cm_id->rem_ref(cep->cm_id);
1887 			cep->cm_id = NULL;
1888 		}
1889 		cep->sock = NULL;
1890 		siw_socket_disassoc(s);
1891 		cep->state = SIW_EPSTATE_CLOSED;
1892 
1893 		siw_cep_set_free(cep);
1894 		siw_cep_put(cep);
1895 	}
1896 	sock_release(s);
1897 
1898 	return rv;
1899 }
1900 
1901 static void siw_drop_listeners(struct iw_cm_id *id)
1902 {
1903 	struct list_head *p, *tmp;
1904 
1905 	/*
1906 	 * In case of a wildcard rdma_listen on a multi-homed device,
1907 	 * a listener's IWCM id is associated with more than one listening CEP.
1908 	 */
1909 	list_for_each_safe(p, tmp, (struct list_head *)id->provider_data) {
1910 		struct siw_cep *cep = list_entry(p, struct siw_cep, listenq);
1911 
1912 		list_del(p);
1913 
1914 		siw_dbg_cep(cep, "drop cep, state %d\n", cep->state);
1915 
1916 		siw_cep_set_inuse(cep);
1917 
1918 		if (cep->cm_id) {
1919 			cep->cm_id->rem_ref(cep->cm_id);
1920 			cep->cm_id = NULL;
1921 		}
1922 		if (cep->sock) {
1923 			siw_socket_disassoc(cep->sock);
1924 			sock_release(cep->sock);
1925 			cep->sock = NULL;
1926 		}
1927 		cep->state = SIW_EPSTATE_CLOSED;
1928 		siw_cep_set_free(cep);
1929 		siw_cep_put(cep);
1930 	}
1931 }
1932 
1933 int siw_destroy_listen(struct iw_cm_id *id)
1934 {
1935 	if (!id->provider_data) {
1936 		siw_dbg(id->device, "no cep(s)\n");
1937 		return 0;
1938 	}
1939 	siw_drop_listeners(id);
1940 	kfree(id->provider_data);
1941 	id->provider_data = NULL;
1942 
1943 	return 0;
1944 }
1945 
1946 int siw_cm_init(void)
1947 {
1948 	/*
1949 	 * create_single_workqueue for strict ordering
1950 	 */
1951 	siw_cm_wq = create_singlethread_workqueue("siw_cm_wq");
1952 	if (!siw_cm_wq)
1953 		return -ENOMEM;
1954 
1955 	return 0;
1956 }
1957 
1958 void siw_cm_exit(void)
1959 {
1960 	if (siw_cm_wq)
1961 		destroy_workqueue(siw_cm_wq);
1962 }
1963