xref: /openbmc/linux/drivers/infiniband/sw/siw/siw_cm.c (revision e724e7aa)
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 		siw_cep_put(cep);
1505 
1506 		qp->cep = NULL;
1507 		siw_cep_put(cep);
1508 
1509 		cep->state = SIW_EPSTATE_CLOSED;
1510 
1511 		siw_cep_set_free(cep);
1512 
1513 		siw_cep_put(cep);
1514 
1515 	} else if (s) {
1516 		sock_release(s);
1517 	}
1518 	if (qp)
1519 		siw_qp_put(qp);
1520 
1521 	return rv;
1522 }
1523 
1524 /*
1525  * siw_accept - Let SoftiWARP accept an RDMA connection request
1526  *
1527  * @id:		New connection management id to be used for accepted
1528  *		connection request
1529  * @params:	Connection parameters provided by ULP for accepting connection
1530  *
1531  * Transition QP to RTS state, associate new CM id @id with accepted CEP
1532  * and get prepared for TCP input by installing socket callbacks.
1533  * Then send MPA Reply and generate the "connection established" event.
1534  * Socket callbacks must be installed before sending MPA Reply, because
1535  * the latter may cause a first RDMA message to arrive from the RDMA Initiator
1536  * side very quickly, at which time the socket callbacks must be ready.
1537  */
1538 int siw_accept(struct iw_cm_id *id, struct iw_cm_conn_param *params)
1539 {
1540 	struct siw_device *sdev = to_siw_dev(id->device);
1541 	struct siw_cep *cep = (struct siw_cep *)id->provider_data;
1542 	struct siw_qp *qp;
1543 	struct siw_qp_attrs qp_attrs;
1544 	int rv, max_priv_data = MPA_MAX_PRIVDATA;
1545 	bool wait_for_peer_rts = false;
1546 
1547 	siw_cep_set_inuse(cep);
1548 	siw_cep_put(cep);
1549 
1550 	/* Free lingering inbound private data */
1551 	if (cep->mpa.hdr.params.pd_len) {
1552 		cep->mpa.hdr.params.pd_len = 0;
1553 		kfree(cep->mpa.pdata);
1554 		cep->mpa.pdata = NULL;
1555 	}
1556 	siw_cancel_mpatimer(cep);
1557 
1558 	if (cep->state != SIW_EPSTATE_RECVD_MPAREQ) {
1559 		siw_dbg_cep(cep, "out of state\n");
1560 
1561 		siw_cep_set_free(cep);
1562 		siw_cep_put(cep);
1563 
1564 		return -ECONNRESET;
1565 	}
1566 	qp = siw_qp_id2obj(sdev, params->qpn);
1567 	if (!qp) {
1568 		WARN(1, "[QP %d] does not exist\n", params->qpn);
1569 		siw_cep_set_free(cep);
1570 		siw_cep_put(cep);
1571 
1572 		return -EINVAL;
1573 	}
1574 	down_write(&qp->state_lock);
1575 	if (qp->attrs.state > SIW_QP_STATE_RTR) {
1576 		rv = -EINVAL;
1577 		up_write(&qp->state_lock);
1578 		goto error;
1579 	}
1580 	siw_dbg_cep(cep, "[QP %d]\n", params->qpn);
1581 
1582 	if (try_gso && cep->mpa.hdr.params.bits & MPA_RR_FLAG_GSO_EXP) {
1583 		siw_dbg_cep(cep, "peer allows GSO on TX\n");
1584 		qp->tx_ctx.gso_seg_limit = 0;
1585 	}
1586 	if (params->ord > sdev->attrs.max_ord ||
1587 	    params->ird > sdev->attrs.max_ird) {
1588 		siw_dbg_cep(
1589 			cep,
1590 			"[QP %u]: ord %d (max %d), ird %d (max %d)\n",
1591 			qp_id(qp), params->ord, sdev->attrs.max_ord,
1592 			params->ird, sdev->attrs.max_ird);
1593 		rv = -EINVAL;
1594 		up_write(&qp->state_lock);
1595 		goto error;
1596 	}
1597 	if (cep->enhanced_rdma_conn_est)
1598 		max_priv_data -= sizeof(struct mpa_v2_data);
1599 
1600 	if (params->private_data_len > max_priv_data) {
1601 		siw_dbg_cep(
1602 			cep,
1603 			"[QP %u]: private data length: %d (max %d)\n",
1604 			qp_id(qp), params->private_data_len, max_priv_data);
1605 		rv = -EINVAL;
1606 		up_write(&qp->state_lock);
1607 		goto error;
1608 	}
1609 	if (cep->enhanced_rdma_conn_est) {
1610 		if (params->ord > cep->ord) {
1611 			if (relaxed_ird_negotiation) {
1612 				params->ord = cep->ord;
1613 			} else {
1614 				cep->ird = params->ird;
1615 				cep->ord = params->ord;
1616 				rv = -EINVAL;
1617 				up_write(&qp->state_lock);
1618 				goto error;
1619 			}
1620 		}
1621 		if (params->ird < cep->ird) {
1622 			if (relaxed_ird_negotiation &&
1623 			    cep->ird <= sdev->attrs.max_ird)
1624 				params->ird = cep->ird;
1625 			else {
1626 				rv = -ENOMEM;
1627 				up_write(&qp->state_lock);
1628 				goto error;
1629 			}
1630 		}
1631 		if (cep->mpa.v2_ctrl.ord &
1632 		    (MPA_V2_RDMA_WRITE_RTR | MPA_V2_RDMA_READ_RTR))
1633 			wait_for_peer_rts = true;
1634 		/*
1635 		 * Signal back negotiated IRD and ORD values
1636 		 */
1637 		cep->mpa.v2_ctrl.ord =
1638 			htons(params->ord & MPA_IRD_ORD_MASK) |
1639 			(cep->mpa.v2_ctrl.ord & ~MPA_V2_MASK_IRD_ORD);
1640 		cep->mpa.v2_ctrl.ird =
1641 			htons(params->ird & MPA_IRD_ORD_MASK) |
1642 			(cep->mpa.v2_ctrl.ird & ~MPA_V2_MASK_IRD_ORD);
1643 	}
1644 	cep->ird = params->ird;
1645 	cep->ord = params->ord;
1646 
1647 	cep->cm_id = id;
1648 	id->add_ref(id);
1649 
1650 	memset(&qp_attrs, 0, sizeof(qp_attrs));
1651 	qp_attrs.orq_size = cep->ord;
1652 	qp_attrs.irq_size = cep->ird;
1653 	qp_attrs.sk = cep->sock;
1654 	if (cep->mpa.hdr.params.bits & MPA_RR_FLAG_CRC)
1655 		qp_attrs.flags = SIW_MPA_CRC;
1656 	qp_attrs.state = SIW_QP_STATE_RTS;
1657 
1658 	siw_dbg_cep(cep, "[QP%u]: moving to rts\n", qp_id(qp));
1659 
1660 	/* Associate QP with CEP */
1661 	siw_cep_get(cep);
1662 	qp->cep = cep;
1663 
1664 	/* siw_qp_get(qp) already done by QP lookup */
1665 	cep->qp = qp;
1666 
1667 	cep->state = SIW_EPSTATE_RDMA_MODE;
1668 
1669 	/* Move socket RX/TX under QP control */
1670 	rv = siw_qp_modify(qp, &qp_attrs,
1671 			   SIW_QP_ATTR_STATE | SIW_QP_ATTR_LLP_HANDLE |
1672 				   SIW_QP_ATTR_ORD | SIW_QP_ATTR_IRD |
1673 				   SIW_QP_ATTR_MPA);
1674 	up_write(&qp->state_lock);
1675 
1676 	if (rv)
1677 		goto error;
1678 
1679 	siw_dbg_cep(cep, "[QP %u]: send mpa reply, %d byte pdata\n",
1680 		    qp_id(qp), params->private_data_len);
1681 
1682 	rv = siw_send_mpareqrep(cep, params->private_data,
1683 				params->private_data_len);
1684 	if (rv != 0)
1685 		goto error;
1686 
1687 	if (wait_for_peer_rts) {
1688 		siw_sk_assign_rtr_upcalls(cep);
1689 	} else {
1690 		siw_qp_socket_assoc(cep, qp);
1691 		rv = siw_cm_upcall(cep, IW_CM_EVENT_ESTABLISHED, 0);
1692 		if (rv)
1693 			goto error;
1694 	}
1695 	siw_cep_set_free(cep);
1696 
1697 	return 0;
1698 error:
1699 	siw_socket_disassoc(cep->sock);
1700 	sock_release(cep->sock);
1701 	cep->sock = NULL;
1702 
1703 	cep->state = SIW_EPSTATE_CLOSED;
1704 
1705 	if (cep->cm_id) {
1706 		cep->cm_id->rem_ref(id);
1707 		cep->cm_id = NULL;
1708 	}
1709 	if (qp->cep) {
1710 		siw_cep_put(cep);
1711 		qp->cep = NULL;
1712 	}
1713 	cep->qp = NULL;
1714 	siw_qp_put(qp);
1715 
1716 	siw_cep_set_free(cep);
1717 	siw_cep_put(cep);
1718 
1719 	return rv;
1720 }
1721 
1722 /*
1723  * siw_reject()
1724  *
1725  * Local connection reject case. Send private data back to peer,
1726  * close connection and dereference connection id.
1727  */
1728 int siw_reject(struct iw_cm_id *id, const void *pdata, u8 pd_len)
1729 {
1730 	struct siw_cep *cep = (struct siw_cep *)id->provider_data;
1731 
1732 	siw_cep_set_inuse(cep);
1733 	siw_cep_put(cep);
1734 
1735 	siw_cancel_mpatimer(cep);
1736 
1737 	if (cep->state != SIW_EPSTATE_RECVD_MPAREQ) {
1738 		siw_dbg_cep(cep, "out of state\n");
1739 
1740 		siw_cep_set_free(cep);
1741 		siw_cep_put(cep); /* put last reference */
1742 
1743 		return -ECONNRESET;
1744 	}
1745 	siw_dbg_cep(cep, "cep->state %d, pd_len %d\n", cep->state,
1746 		    pd_len);
1747 
1748 	if (__mpa_rr_revision(cep->mpa.hdr.params.bits) >= MPA_REVISION_1) {
1749 		cep->mpa.hdr.params.bits |= MPA_RR_FLAG_REJECT; /* reject */
1750 		siw_send_mpareqrep(cep, pdata, pd_len);
1751 	}
1752 	siw_socket_disassoc(cep->sock);
1753 	sock_release(cep->sock);
1754 	cep->sock = NULL;
1755 
1756 	cep->state = SIW_EPSTATE_CLOSED;
1757 
1758 	siw_cep_set_free(cep);
1759 	siw_cep_put(cep);
1760 
1761 	return 0;
1762 }
1763 
1764 /*
1765  * siw_create_listen - Create resources for a listener's IWCM ID @id
1766  *
1767  * Starts listen on the socket address id->local_addr.
1768  *
1769  */
1770 int siw_create_listen(struct iw_cm_id *id, int backlog)
1771 {
1772 	struct socket *s;
1773 	struct siw_cep *cep = NULL;
1774 	struct siw_device *sdev = to_siw_dev(id->device);
1775 	int addr_family = id->local_addr.ss_family;
1776 	int rv = 0;
1777 
1778 	if (addr_family != AF_INET && addr_family != AF_INET6)
1779 		return -EAFNOSUPPORT;
1780 
1781 	rv = sock_create(addr_family, SOCK_STREAM, IPPROTO_TCP, &s);
1782 	if (rv < 0)
1783 		return rv;
1784 
1785 	/*
1786 	 * Allow binding local port when still in TIME_WAIT from last close.
1787 	 */
1788 	sock_set_reuseaddr(s->sk);
1789 
1790 	if (addr_family == AF_INET) {
1791 		struct sockaddr_in *laddr = &to_sockaddr_in(id->local_addr);
1792 
1793 		/* For wildcard addr, limit binding to current device only */
1794 		if (ipv4_is_zeronet(laddr->sin_addr.s_addr))
1795 			s->sk->sk_bound_dev_if = sdev->netdev->ifindex;
1796 
1797 		rv = s->ops->bind(s, (struct sockaddr *)laddr,
1798 				  sizeof(struct sockaddr_in));
1799 	} else {
1800 		struct sockaddr_in6 *laddr = &to_sockaddr_in6(id->local_addr);
1801 
1802 		if (id->afonly) {
1803 			rv = ip6_sock_set_v6only(s->sk);
1804 			if (rv) {
1805 				siw_dbg(id->device,
1806 					"ip6_sock_set_v6only erro: %d\n", rv);
1807 				goto error;
1808 			}
1809 		}
1810 
1811 		/* For wildcard addr, limit binding to current device only */
1812 		if (ipv6_addr_any(&laddr->sin6_addr))
1813 			s->sk->sk_bound_dev_if = sdev->netdev->ifindex;
1814 
1815 		rv = s->ops->bind(s, (struct sockaddr *)laddr,
1816 				  sizeof(struct sockaddr_in6));
1817 	}
1818 	if (rv) {
1819 		siw_dbg(id->device, "socket bind error: %d\n", rv);
1820 		goto error;
1821 	}
1822 	cep = siw_cep_alloc(sdev);
1823 	if (!cep) {
1824 		rv = -ENOMEM;
1825 		goto error;
1826 	}
1827 	siw_cep_socket_assoc(cep, s);
1828 
1829 	rv = siw_cm_alloc_work(cep, backlog);
1830 	if (rv) {
1831 		siw_dbg(id->device,
1832 			"alloc_work error %d, backlog %d\n",
1833 			rv, backlog);
1834 		goto error;
1835 	}
1836 	rv = s->ops->listen(s, backlog);
1837 	if (rv) {
1838 		siw_dbg(id->device, "listen error %d\n", rv);
1839 		goto error;
1840 	}
1841 	cep->cm_id = id;
1842 	id->add_ref(id);
1843 
1844 	/*
1845 	 * In case of a wildcard rdma_listen on a multi-homed device,
1846 	 * a listener's IWCM id is associated with more than one listening CEP.
1847 	 *
1848 	 * We currently use id->provider_data in three different ways:
1849 	 *
1850 	 * o For a listener's IWCM id, id->provider_data points to
1851 	 *   the list_head of the list of listening CEPs.
1852 	 *   Uses: siw_create_listen(), siw_destroy_listen()
1853 	 *
1854 	 * o For each accepted passive-side IWCM id, id->provider_data
1855 	 *   points to the CEP itself. This is a consequence of
1856 	 *   - siw_cm_upcall() setting event.provider_data = cep and
1857 	 *   - the IWCM's cm_conn_req_handler() setting provider_data of the
1858 	 *     new passive-side IWCM id equal to event.provider_data
1859 	 *   Uses: siw_accept(), siw_reject()
1860 	 *
1861 	 * o For an active-side IWCM id, id->provider_data is not used at all.
1862 	 *
1863 	 */
1864 	if (!id->provider_data) {
1865 		id->provider_data =
1866 			kmalloc(sizeof(struct list_head), GFP_KERNEL);
1867 		if (!id->provider_data) {
1868 			rv = -ENOMEM;
1869 			goto error;
1870 		}
1871 		INIT_LIST_HEAD((struct list_head *)id->provider_data);
1872 	}
1873 	list_add_tail(&cep->listenq, (struct list_head *)id->provider_data);
1874 	cep->state = SIW_EPSTATE_LISTENING;
1875 
1876 	siw_dbg(id->device, "Listen at laddr %pISp\n", &id->local_addr);
1877 
1878 	return 0;
1879 
1880 error:
1881 	siw_dbg(id->device, "failed: %d\n", rv);
1882 
1883 	if (cep) {
1884 		siw_cep_set_inuse(cep);
1885 
1886 		if (cep->cm_id) {
1887 			cep->cm_id->rem_ref(cep->cm_id);
1888 			cep->cm_id = NULL;
1889 		}
1890 		cep->sock = NULL;
1891 		siw_socket_disassoc(s);
1892 		cep->state = SIW_EPSTATE_CLOSED;
1893 
1894 		siw_cep_set_free(cep);
1895 		siw_cep_put(cep);
1896 	}
1897 	sock_release(s);
1898 
1899 	return rv;
1900 }
1901 
1902 static void siw_drop_listeners(struct iw_cm_id *id)
1903 {
1904 	struct list_head *p, *tmp;
1905 
1906 	/*
1907 	 * In case of a wildcard rdma_listen on a multi-homed device,
1908 	 * a listener's IWCM id is associated with more than one listening CEP.
1909 	 */
1910 	list_for_each_safe(p, tmp, (struct list_head *)id->provider_data) {
1911 		struct siw_cep *cep = list_entry(p, struct siw_cep, listenq);
1912 
1913 		list_del(p);
1914 
1915 		siw_dbg_cep(cep, "drop cep, state %d\n", cep->state);
1916 
1917 		siw_cep_set_inuse(cep);
1918 
1919 		if (cep->cm_id) {
1920 			cep->cm_id->rem_ref(cep->cm_id);
1921 			cep->cm_id = NULL;
1922 		}
1923 		if (cep->sock) {
1924 			siw_socket_disassoc(cep->sock);
1925 			sock_release(cep->sock);
1926 			cep->sock = NULL;
1927 		}
1928 		cep->state = SIW_EPSTATE_CLOSED;
1929 		siw_cep_set_free(cep);
1930 		siw_cep_put(cep);
1931 	}
1932 }
1933 
1934 int siw_destroy_listen(struct iw_cm_id *id)
1935 {
1936 	if (!id->provider_data) {
1937 		siw_dbg(id->device, "no cep(s)\n");
1938 		return 0;
1939 	}
1940 	siw_drop_listeners(id);
1941 	kfree(id->provider_data);
1942 	id->provider_data = NULL;
1943 
1944 	return 0;
1945 }
1946 
1947 int siw_cm_init(void)
1948 {
1949 	/*
1950 	 * create_single_workqueue for strict ordering
1951 	 */
1952 	siw_cm_wq = create_singlethread_workqueue("siw_cm_wq");
1953 	if (!siw_cm_wq)
1954 		return -ENOMEM;
1955 
1956 	return 0;
1957 }
1958 
1959 void siw_cm_exit(void)
1960 {
1961 	if (siw_cm_wq)
1962 		destroy_workqueue(siw_cm_wq);
1963 }
1964