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