xref: /linux/net/smc/af_smc.c (revision 4ae68b26c3ab5a82aa271e6e9fc9b1a06e1d6b40)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  AF_SMC protocol family socket handler keeping the AF_INET sock address type
6  *  applies to SOCK_STREAM sockets only
7  *  offers an alternative communication option for TCP-protocol sockets
8  *  applicable with RoCE-cards only
9  *
10  *  Initial restrictions:
11  *    - support for alternate links postponed
12  *
13  *  Copyright IBM Corp. 2016, 2018
14  *
15  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
16  *              based on prototype from Frank Blaschka
17  */
18 
19 #define KMSG_COMPONENT "smc"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21 
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/workqueue.h>
25 #include <linux/in.h>
26 #include <linux/sched/signal.h>
27 #include <linux/if_vlan.h>
28 #include <linux/rcupdate_wait.h>
29 #include <linux/ctype.h>
30 #include <linux/splice.h>
31 
32 #include <net/sock.h>
33 #include <net/tcp.h>
34 #include <net/smc.h>
35 #include <asm/ioctls.h>
36 
37 #include <net/net_namespace.h>
38 #include <net/netns/generic.h>
39 #include "smc_netns.h"
40 
41 #include "smc.h"
42 #include "smc_clc.h"
43 #include "smc_llc.h"
44 #include "smc_cdc.h"
45 #include "smc_core.h"
46 #include "smc_ib.h"
47 #include "smc_ism.h"
48 #include "smc_pnet.h"
49 #include "smc_netlink.h"
50 #include "smc_tx.h"
51 #include "smc_rx.h"
52 #include "smc_close.h"
53 #include "smc_stats.h"
54 #include "smc_tracepoint.h"
55 #include "smc_sysctl.h"
56 
57 static DEFINE_MUTEX(smc_server_lgr_pending);	/* serialize link group
58 						 * creation on server
59 						 */
60 static DEFINE_MUTEX(smc_client_lgr_pending);	/* serialize link group
61 						 * creation on client
62 						 */
63 
64 static struct workqueue_struct	*smc_tcp_ls_wq;	/* wq for tcp listen work */
65 struct workqueue_struct	*smc_hs_wq;	/* wq for handshake work */
66 struct workqueue_struct	*smc_close_wq;	/* wq for close work */
67 
68 static void smc_tcp_listen_work(struct work_struct *);
69 static void smc_connect_work(struct work_struct *);
70 
71 int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb)
72 {
73 	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
74 	void *hdr;
75 
76 	if (cb_ctx->pos[0])
77 		goto out;
78 
79 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
80 			  &smc_gen_nl_family, NLM_F_MULTI,
81 			  SMC_NETLINK_DUMP_HS_LIMITATION);
82 	if (!hdr)
83 		return -ENOMEM;
84 
85 	if (nla_put_u8(skb, SMC_NLA_HS_LIMITATION_ENABLED,
86 		       sock_net(skb->sk)->smc.limit_smc_hs))
87 		goto err;
88 
89 	genlmsg_end(skb, hdr);
90 	cb_ctx->pos[0] = 1;
91 out:
92 	return skb->len;
93 err:
94 	genlmsg_cancel(skb, hdr);
95 	return -EMSGSIZE;
96 }
97 
98 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
99 {
100 	sock_net(skb->sk)->smc.limit_smc_hs = true;
101 	return 0;
102 }
103 
104 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
105 {
106 	sock_net(skb->sk)->smc.limit_smc_hs = false;
107 	return 0;
108 }
109 
110 static void smc_set_keepalive(struct sock *sk, int val)
111 {
112 	struct smc_sock *smc = smc_sk(sk);
113 
114 	smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
115 }
116 
117 static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
118 					  struct sk_buff *skb,
119 					  struct request_sock *req,
120 					  struct dst_entry *dst,
121 					  struct request_sock *req_unhash,
122 					  bool *own_req)
123 {
124 	struct smc_sock *smc;
125 	struct sock *child;
126 
127 	smc = smc_clcsock_user_data(sk);
128 
129 	if (READ_ONCE(sk->sk_ack_backlog) + atomic_read(&smc->queued_smc_hs) >
130 				sk->sk_max_ack_backlog)
131 		goto drop;
132 
133 	if (sk_acceptq_is_full(&smc->sk)) {
134 		NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
135 		goto drop;
136 	}
137 
138 	/* passthrough to original syn recv sock fct */
139 	child = smc->ori_af_ops->syn_recv_sock(sk, skb, req, dst, req_unhash,
140 					       own_req);
141 	/* child must not inherit smc or its ops */
142 	if (child) {
143 		rcu_assign_sk_user_data(child, NULL);
144 
145 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
146 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
147 			inet_csk(child)->icsk_af_ops = smc->ori_af_ops;
148 	}
149 	return child;
150 
151 drop:
152 	dst_release(dst);
153 	tcp_listendrop(sk);
154 	return NULL;
155 }
156 
157 static bool smc_hs_congested(const struct sock *sk)
158 {
159 	const struct smc_sock *smc;
160 
161 	smc = smc_clcsock_user_data(sk);
162 
163 	if (!smc)
164 		return true;
165 
166 	if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
167 		return true;
168 
169 	return false;
170 }
171 
172 static struct smc_hashinfo smc_v4_hashinfo = {
173 	.lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
174 };
175 
176 static struct smc_hashinfo smc_v6_hashinfo = {
177 	.lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
178 };
179 
180 int smc_hash_sk(struct sock *sk)
181 {
182 	struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
183 	struct hlist_head *head;
184 
185 	head = &h->ht;
186 
187 	write_lock_bh(&h->lock);
188 	sk_add_node(sk, head);
189 	write_unlock_bh(&h->lock);
190 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
191 
192 	return 0;
193 }
194 EXPORT_SYMBOL_GPL(smc_hash_sk);
195 
196 void smc_unhash_sk(struct sock *sk)
197 {
198 	struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
199 
200 	write_lock_bh(&h->lock);
201 	if (sk_del_node_init(sk))
202 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
203 	write_unlock_bh(&h->lock);
204 }
205 EXPORT_SYMBOL_GPL(smc_unhash_sk);
206 
207 /* This will be called before user really release sock_lock. So do the
208  * work which we didn't do because of user hold the sock_lock in the
209  * BH context
210  */
211 static void smc_release_cb(struct sock *sk)
212 {
213 	struct smc_sock *smc = smc_sk(sk);
214 
215 	if (smc->conn.tx_in_release_sock) {
216 		smc_tx_pending(&smc->conn);
217 		smc->conn.tx_in_release_sock = false;
218 	}
219 }
220 
221 struct proto smc_proto = {
222 	.name		= "SMC",
223 	.owner		= THIS_MODULE,
224 	.keepalive	= smc_set_keepalive,
225 	.hash		= smc_hash_sk,
226 	.unhash		= smc_unhash_sk,
227 	.release_cb	= smc_release_cb,
228 	.obj_size	= sizeof(struct smc_sock),
229 	.h.smc_hash	= &smc_v4_hashinfo,
230 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
231 };
232 EXPORT_SYMBOL_GPL(smc_proto);
233 
234 struct proto smc_proto6 = {
235 	.name		= "SMC6",
236 	.owner		= THIS_MODULE,
237 	.keepalive	= smc_set_keepalive,
238 	.hash		= smc_hash_sk,
239 	.unhash		= smc_unhash_sk,
240 	.release_cb	= smc_release_cb,
241 	.obj_size	= sizeof(struct smc_sock),
242 	.h.smc_hash	= &smc_v6_hashinfo,
243 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
244 };
245 EXPORT_SYMBOL_GPL(smc_proto6);
246 
247 static void smc_fback_restore_callbacks(struct smc_sock *smc)
248 {
249 	struct sock *clcsk = smc->clcsock->sk;
250 
251 	write_lock_bh(&clcsk->sk_callback_lock);
252 	clcsk->sk_user_data = NULL;
253 
254 	smc_clcsock_restore_cb(&clcsk->sk_state_change, &smc->clcsk_state_change);
255 	smc_clcsock_restore_cb(&clcsk->sk_data_ready, &smc->clcsk_data_ready);
256 	smc_clcsock_restore_cb(&clcsk->sk_write_space, &smc->clcsk_write_space);
257 	smc_clcsock_restore_cb(&clcsk->sk_error_report, &smc->clcsk_error_report);
258 
259 	write_unlock_bh(&clcsk->sk_callback_lock);
260 }
261 
262 static void smc_restore_fallback_changes(struct smc_sock *smc)
263 {
264 	if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
265 		smc->clcsock->file->private_data = smc->sk.sk_socket;
266 		smc->clcsock->file = NULL;
267 		smc_fback_restore_callbacks(smc);
268 	}
269 }
270 
271 static int __smc_release(struct smc_sock *smc)
272 {
273 	struct sock *sk = &smc->sk;
274 	int rc = 0;
275 
276 	if (!smc->use_fallback) {
277 		rc = smc_close_active(smc);
278 		sock_set_flag(sk, SOCK_DEAD);
279 		sk->sk_shutdown |= SHUTDOWN_MASK;
280 	} else {
281 		if (sk->sk_state != SMC_CLOSED) {
282 			if (sk->sk_state != SMC_LISTEN &&
283 			    sk->sk_state != SMC_INIT)
284 				sock_put(sk); /* passive closing */
285 			if (sk->sk_state == SMC_LISTEN) {
286 				/* wake up clcsock accept */
287 				rc = kernel_sock_shutdown(smc->clcsock,
288 							  SHUT_RDWR);
289 			}
290 			sk->sk_state = SMC_CLOSED;
291 			sk->sk_state_change(sk);
292 		}
293 		smc_restore_fallback_changes(smc);
294 	}
295 
296 	sk->sk_prot->unhash(sk);
297 
298 	if (sk->sk_state == SMC_CLOSED) {
299 		if (smc->clcsock) {
300 			release_sock(sk);
301 			smc_clcsock_release(smc);
302 			lock_sock(sk);
303 		}
304 		if (!smc->use_fallback)
305 			smc_conn_free(&smc->conn);
306 	}
307 
308 	return rc;
309 }
310 
311 static int smc_release(struct socket *sock)
312 {
313 	struct sock *sk = sock->sk;
314 	struct smc_sock *smc;
315 	int old_state, rc = 0;
316 
317 	if (!sk)
318 		goto out;
319 
320 	sock_hold(sk); /* sock_put below */
321 	smc = smc_sk(sk);
322 
323 	old_state = sk->sk_state;
324 
325 	/* cleanup for a dangling non-blocking connect */
326 	if (smc->connect_nonblock && old_state == SMC_INIT)
327 		tcp_abort(smc->clcsock->sk, ECONNABORTED);
328 
329 	if (cancel_work_sync(&smc->connect_work))
330 		sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
331 
332 	if (sk->sk_state == SMC_LISTEN)
333 		/* smc_close_non_accepted() is called and acquires
334 		 * sock lock for child sockets again
335 		 */
336 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
337 	else
338 		lock_sock(sk);
339 
340 	if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE &&
341 	    !smc->use_fallback)
342 		smc_close_active_abort(smc);
343 
344 	rc = __smc_release(smc);
345 
346 	/* detach socket */
347 	sock_orphan(sk);
348 	sock->sk = NULL;
349 	release_sock(sk);
350 
351 	sock_put(sk); /* sock_hold above */
352 	sock_put(sk); /* final sock_put */
353 out:
354 	return rc;
355 }
356 
357 static void smc_destruct(struct sock *sk)
358 {
359 	if (sk->sk_state != SMC_CLOSED)
360 		return;
361 	if (!sock_flag(sk, SOCK_DEAD))
362 		return;
363 }
364 
365 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
366 				   int protocol)
367 {
368 	struct smc_sock *smc;
369 	struct proto *prot;
370 	struct sock *sk;
371 
372 	prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
373 	sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
374 	if (!sk)
375 		return NULL;
376 
377 	sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
378 	sk->sk_state = SMC_INIT;
379 	sk->sk_destruct = smc_destruct;
380 	sk->sk_protocol = protocol;
381 	WRITE_ONCE(sk->sk_sndbuf, 2 * READ_ONCE(net->smc.sysctl_wmem));
382 	WRITE_ONCE(sk->sk_rcvbuf, 2 * READ_ONCE(net->smc.sysctl_rmem));
383 	smc = smc_sk(sk);
384 	INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
385 	INIT_WORK(&smc->connect_work, smc_connect_work);
386 	INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
387 	INIT_LIST_HEAD(&smc->accept_q);
388 	spin_lock_init(&smc->accept_q_lock);
389 	spin_lock_init(&smc->conn.send_lock);
390 	sk->sk_prot->hash(sk);
391 	mutex_init(&smc->clcsock_release_lock);
392 	smc_init_saved_callbacks(smc);
393 
394 	return sk;
395 }
396 
397 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
398 		    int addr_len)
399 {
400 	struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
401 	struct sock *sk = sock->sk;
402 	struct smc_sock *smc;
403 	int rc;
404 
405 	smc = smc_sk(sk);
406 
407 	/* replicate tests from inet_bind(), to be safe wrt. future changes */
408 	rc = -EINVAL;
409 	if (addr_len < sizeof(struct sockaddr_in))
410 		goto out;
411 
412 	rc = -EAFNOSUPPORT;
413 	if (addr->sin_family != AF_INET &&
414 	    addr->sin_family != AF_INET6 &&
415 	    addr->sin_family != AF_UNSPEC)
416 		goto out;
417 	/* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
418 	if (addr->sin_family == AF_UNSPEC &&
419 	    addr->sin_addr.s_addr != htonl(INADDR_ANY))
420 		goto out;
421 
422 	lock_sock(sk);
423 
424 	/* Check if socket is already active */
425 	rc = -EINVAL;
426 	if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
427 		goto out_rel;
428 
429 	smc->clcsock->sk->sk_reuse = sk->sk_reuse;
430 	smc->clcsock->sk->sk_reuseport = sk->sk_reuseport;
431 	rc = kernel_bind(smc->clcsock, uaddr, addr_len);
432 
433 out_rel:
434 	release_sock(sk);
435 out:
436 	return rc;
437 }
438 
439 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
440  * clc socket (since smc is not called for these options from net/core)
441  */
442 
443 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
444 			     (1UL << SOCK_KEEPOPEN) | \
445 			     (1UL << SOCK_LINGER) | \
446 			     (1UL << SOCK_BROADCAST) | \
447 			     (1UL << SOCK_TIMESTAMP) | \
448 			     (1UL << SOCK_DBG) | \
449 			     (1UL << SOCK_RCVTSTAMP) | \
450 			     (1UL << SOCK_RCVTSTAMPNS) | \
451 			     (1UL << SOCK_LOCALROUTE) | \
452 			     (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
453 			     (1UL << SOCK_RXQ_OVFL) | \
454 			     (1UL << SOCK_WIFI_STATUS) | \
455 			     (1UL << SOCK_NOFCS) | \
456 			     (1UL << SOCK_FILTER_LOCKED) | \
457 			     (1UL << SOCK_TSTAMP_NEW))
458 
459 /* if set, use value set by setsockopt() - else use IPv4 or SMC sysctl value */
460 static void smc_adjust_sock_bufsizes(struct sock *nsk, struct sock *osk,
461 				     unsigned long mask)
462 {
463 	struct net *nnet = sock_net(nsk);
464 
465 	nsk->sk_userlocks = osk->sk_userlocks;
466 	if (osk->sk_userlocks & SOCK_SNDBUF_LOCK) {
467 		nsk->sk_sndbuf = osk->sk_sndbuf;
468 	} else {
469 		if (mask == SK_FLAGS_SMC_TO_CLC)
470 			WRITE_ONCE(nsk->sk_sndbuf,
471 				   READ_ONCE(nnet->ipv4.sysctl_tcp_wmem[1]));
472 		else
473 			WRITE_ONCE(nsk->sk_sndbuf,
474 				   2 * READ_ONCE(nnet->smc.sysctl_wmem));
475 	}
476 	if (osk->sk_userlocks & SOCK_RCVBUF_LOCK) {
477 		nsk->sk_rcvbuf = osk->sk_rcvbuf;
478 	} else {
479 		if (mask == SK_FLAGS_SMC_TO_CLC)
480 			WRITE_ONCE(nsk->sk_rcvbuf,
481 				   READ_ONCE(nnet->ipv4.sysctl_tcp_rmem[1]));
482 		else
483 			WRITE_ONCE(nsk->sk_rcvbuf,
484 				   2 * READ_ONCE(nnet->smc.sysctl_rmem));
485 	}
486 }
487 
488 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
489 				   unsigned long mask)
490 {
491 	/* options we don't get control via setsockopt for */
492 	nsk->sk_type = osk->sk_type;
493 	nsk->sk_sndtimeo = osk->sk_sndtimeo;
494 	nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
495 	nsk->sk_mark = READ_ONCE(osk->sk_mark);
496 	nsk->sk_priority = osk->sk_priority;
497 	nsk->sk_rcvlowat = osk->sk_rcvlowat;
498 	nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
499 	nsk->sk_err = osk->sk_err;
500 
501 	nsk->sk_flags &= ~mask;
502 	nsk->sk_flags |= osk->sk_flags & mask;
503 
504 	smc_adjust_sock_bufsizes(nsk, osk, mask);
505 }
506 
507 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
508 {
509 	smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
510 }
511 
512 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
513 			     (1UL << SOCK_KEEPOPEN) | \
514 			     (1UL << SOCK_LINGER) | \
515 			     (1UL << SOCK_DBG))
516 /* copy only settings and flags relevant for smc from clc to smc socket */
517 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
518 {
519 	smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
520 }
521 
522 /* register the new vzalloced sndbuf on all links */
523 static int smcr_lgr_reg_sndbufs(struct smc_link *link,
524 				struct smc_buf_desc *snd_desc)
525 {
526 	struct smc_link_group *lgr = link->lgr;
527 	int i, rc = 0;
528 
529 	if (!snd_desc->is_vm)
530 		return -EINVAL;
531 
532 	/* protect against parallel smcr_link_reg_buf() */
533 	down_write(&lgr->llc_conf_mutex);
534 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
535 		if (!smc_link_active(&lgr->lnk[i]))
536 			continue;
537 		rc = smcr_link_reg_buf(&lgr->lnk[i], snd_desc);
538 		if (rc)
539 			break;
540 	}
541 	up_write(&lgr->llc_conf_mutex);
542 	return rc;
543 }
544 
545 /* register the new rmb on all links */
546 static int smcr_lgr_reg_rmbs(struct smc_link *link,
547 			     struct smc_buf_desc *rmb_desc)
548 {
549 	struct smc_link_group *lgr = link->lgr;
550 	bool do_slow = false;
551 	int i, rc = 0;
552 
553 	rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
554 	if (rc)
555 		return rc;
556 
557 	down_read(&lgr->llc_conf_mutex);
558 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
559 		if (!smc_link_active(&lgr->lnk[i]))
560 			continue;
561 		if (!rmb_desc->is_reg_mr[link->link_idx]) {
562 			up_read(&lgr->llc_conf_mutex);
563 			goto slow_path;
564 		}
565 	}
566 	/* mr register already */
567 	goto fast_path;
568 slow_path:
569 	do_slow = true;
570 	/* protect against parallel smc_llc_cli_rkey_exchange() and
571 	 * parallel smcr_link_reg_buf()
572 	 */
573 	down_write(&lgr->llc_conf_mutex);
574 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
575 		if (!smc_link_active(&lgr->lnk[i]))
576 			continue;
577 		rc = smcr_link_reg_buf(&lgr->lnk[i], rmb_desc);
578 		if (rc)
579 			goto out;
580 	}
581 fast_path:
582 	/* exchange confirm_rkey msg with peer */
583 	rc = smc_llc_do_confirm_rkey(link, rmb_desc);
584 	if (rc) {
585 		rc = -EFAULT;
586 		goto out;
587 	}
588 	rmb_desc->is_conf_rkey = true;
589 out:
590 	do_slow ? up_write(&lgr->llc_conf_mutex) : up_read(&lgr->llc_conf_mutex);
591 	smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
592 	return rc;
593 }
594 
595 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
596 {
597 	struct smc_link *link = smc->conn.lnk;
598 	struct smc_llc_qentry *qentry;
599 	int rc;
600 
601 	/* receive CONFIRM LINK request from server over RoCE fabric */
602 	qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
603 			      SMC_LLC_CONFIRM_LINK);
604 	if (!qentry) {
605 		struct smc_clc_msg_decline dclc;
606 
607 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
608 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
609 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
610 	}
611 	smc_llc_save_peer_uid(qentry);
612 	rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
613 	smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
614 	if (rc)
615 		return SMC_CLC_DECL_RMBE_EC;
616 
617 	rc = smc_ib_modify_qp_rts(link);
618 	if (rc)
619 		return SMC_CLC_DECL_ERR_RDYLNK;
620 
621 	smc_wr_remember_qp_attr(link);
622 
623 	/* reg the sndbuf if it was vzalloced */
624 	if (smc->conn.sndbuf_desc->is_vm) {
625 		if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
626 			return SMC_CLC_DECL_ERR_REGBUF;
627 	}
628 
629 	/* reg the rmb */
630 	if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
631 		return SMC_CLC_DECL_ERR_REGBUF;
632 
633 	/* confirm_rkey is implicit on 1st contact */
634 	smc->conn.rmb_desc->is_conf_rkey = true;
635 
636 	/* send CONFIRM LINK response over RoCE fabric */
637 	rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
638 	if (rc < 0)
639 		return SMC_CLC_DECL_TIMEOUT_CL;
640 
641 	smc_llc_link_active(link);
642 	smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
643 
644 	/* optional 2nd link, receive ADD LINK request from server */
645 	qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
646 			      SMC_LLC_ADD_LINK);
647 	if (!qentry) {
648 		struct smc_clc_msg_decline dclc;
649 
650 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
651 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
652 		if (rc == -EAGAIN)
653 			rc = 0; /* no DECLINE received, go with one link */
654 		return rc;
655 	}
656 	smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
657 	smc_llc_cli_add_link(link, qentry);
658 	return 0;
659 }
660 
661 static bool smc_isascii(char *hostname)
662 {
663 	int i;
664 
665 	for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
666 		if (!isascii(hostname[i]))
667 			return false;
668 	return true;
669 }
670 
671 static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
672 					struct smc_clc_msg_accept_confirm *clc)
673 {
674 	struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
675 		(struct smc_clc_msg_accept_confirm_v2 *)clc;
676 	struct smc_clc_first_contact_ext *fce;
677 	int clc_v2_len;
678 
679 	if (clc->hdr.version == SMC_V1 ||
680 	    !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
681 		return;
682 
683 	if (smc->conn.lgr->is_smcd) {
684 		memcpy(smc->conn.lgr->negotiated_eid, clc_v2->d1.eid,
685 		       SMC_MAX_EID_LEN);
686 		clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
687 					 d1);
688 	} else {
689 		memcpy(smc->conn.lgr->negotiated_eid, clc_v2->r1.eid,
690 		       SMC_MAX_EID_LEN);
691 		clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
692 					 r1);
693 	}
694 	fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) + clc_v2_len);
695 	smc->conn.lgr->peer_os = fce->os_type;
696 	smc->conn.lgr->peer_smc_release = fce->release;
697 	if (smc_isascii(fce->hostname))
698 		memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
699 		       SMC_MAX_HOSTNAME_LEN);
700 }
701 
702 static void smcr_conn_save_peer_info(struct smc_sock *smc,
703 				     struct smc_clc_msg_accept_confirm *clc)
704 {
705 	int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
706 
707 	smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
708 	smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
709 	smc->conn.peer_rmbe_size = bufsize;
710 	atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
711 	smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
712 }
713 
714 static void smcd_conn_save_peer_info(struct smc_sock *smc,
715 				     struct smc_clc_msg_accept_confirm *clc)
716 {
717 	int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
718 
719 	smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
720 	smc->conn.peer_token = clc->d0.token;
721 	/* msg header takes up space in the buffer */
722 	smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
723 	atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
724 	smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
725 }
726 
727 static void smc_conn_save_peer_info(struct smc_sock *smc,
728 				    struct smc_clc_msg_accept_confirm *clc)
729 {
730 	if (smc->conn.lgr->is_smcd)
731 		smcd_conn_save_peer_info(smc, clc);
732 	else
733 		smcr_conn_save_peer_info(smc, clc);
734 	smc_conn_save_peer_info_fce(smc, clc);
735 }
736 
737 static void smc_link_save_peer_info(struct smc_link *link,
738 				    struct smc_clc_msg_accept_confirm *clc,
739 				    struct smc_init_info *ini)
740 {
741 	link->peer_qpn = ntoh24(clc->r0.qpn);
742 	memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
743 	memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
744 	link->peer_psn = ntoh24(clc->r0.psn);
745 	link->peer_mtu = clc->r0.qp_mtu;
746 }
747 
748 static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
749 				       struct smc_stats_fback *fback_arr)
750 {
751 	int cnt;
752 
753 	for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
754 		if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
755 			fback_arr[cnt].count++;
756 			break;
757 		}
758 		if (!fback_arr[cnt].fback_code) {
759 			fback_arr[cnt].fback_code = smc->fallback_rsn;
760 			fback_arr[cnt].count++;
761 			break;
762 		}
763 	}
764 }
765 
766 static void smc_stat_fallback(struct smc_sock *smc)
767 {
768 	struct net *net = sock_net(&smc->sk);
769 
770 	mutex_lock(&net->smc.mutex_fback_rsn);
771 	if (smc->listen_smc) {
772 		smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
773 		net->smc.fback_rsn->srv_fback_cnt++;
774 	} else {
775 		smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
776 		net->smc.fback_rsn->clnt_fback_cnt++;
777 	}
778 	mutex_unlock(&net->smc.mutex_fback_rsn);
779 }
780 
781 /* must be called under rcu read lock */
782 static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key)
783 {
784 	struct socket_wq *wq;
785 	__poll_t flags;
786 
787 	wq = rcu_dereference(smc->sk.sk_wq);
788 	if (!skwq_has_sleeper(wq))
789 		return;
790 
791 	/* wake up smc sk->sk_wq */
792 	if (!key) {
793 		/* sk_state_change */
794 		wake_up_interruptible_all(&wq->wait);
795 	} else {
796 		flags = key_to_poll(key);
797 		if (flags & (EPOLLIN | EPOLLOUT))
798 			/* sk_data_ready or sk_write_space */
799 			wake_up_interruptible_sync_poll(&wq->wait, flags);
800 		else if (flags & EPOLLERR)
801 			/* sk_error_report */
802 			wake_up_interruptible_poll(&wq->wait, flags);
803 	}
804 }
805 
806 static int smc_fback_mark_woken(wait_queue_entry_t *wait,
807 				unsigned int mode, int sync, void *key)
808 {
809 	struct smc_mark_woken *mark =
810 		container_of(wait, struct smc_mark_woken, wait_entry);
811 
812 	mark->woken = true;
813 	mark->key = key;
814 	return 0;
815 }
816 
817 static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk,
818 				     void (*clcsock_callback)(struct sock *sk))
819 {
820 	struct smc_mark_woken mark = { .woken = false };
821 	struct socket_wq *wq;
822 
823 	init_waitqueue_func_entry(&mark.wait_entry,
824 				  smc_fback_mark_woken);
825 	rcu_read_lock();
826 	wq = rcu_dereference(clcsk->sk_wq);
827 	if (!wq)
828 		goto out;
829 	add_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
830 	clcsock_callback(clcsk);
831 	remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
832 
833 	if (mark.woken)
834 		smc_fback_wakeup_waitqueue(smc, mark.key);
835 out:
836 	rcu_read_unlock();
837 }
838 
839 static void smc_fback_state_change(struct sock *clcsk)
840 {
841 	struct smc_sock *smc;
842 
843 	read_lock_bh(&clcsk->sk_callback_lock);
844 	smc = smc_clcsock_user_data(clcsk);
845 	if (smc)
846 		smc_fback_forward_wakeup(smc, clcsk,
847 					 smc->clcsk_state_change);
848 	read_unlock_bh(&clcsk->sk_callback_lock);
849 }
850 
851 static void smc_fback_data_ready(struct sock *clcsk)
852 {
853 	struct smc_sock *smc;
854 
855 	read_lock_bh(&clcsk->sk_callback_lock);
856 	smc = smc_clcsock_user_data(clcsk);
857 	if (smc)
858 		smc_fback_forward_wakeup(smc, clcsk,
859 					 smc->clcsk_data_ready);
860 	read_unlock_bh(&clcsk->sk_callback_lock);
861 }
862 
863 static void smc_fback_write_space(struct sock *clcsk)
864 {
865 	struct smc_sock *smc;
866 
867 	read_lock_bh(&clcsk->sk_callback_lock);
868 	smc = smc_clcsock_user_data(clcsk);
869 	if (smc)
870 		smc_fback_forward_wakeup(smc, clcsk,
871 					 smc->clcsk_write_space);
872 	read_unlock_bh(&clcsk->sk_callback_lock);
873 }
874 
875 static void smc_fback_error_report(struct sock *clcsk)
876 {
877 	struct smc_sock *smc;
878 
879 	read_lock_bh(&clcsk->sk_callback_lock);
880 	smc = smc_clcsock_user_data(clcsk);
881 	if (smc)
882 		smc_fback_forward_wakeup(smc, clcsk,
883 					 smc->clcsk_error_report);
884 	read_unlock_bh(&clcsk->sk_callback_lock);
885 }
886 
887 static void smc_fback_replace_callbacks(struct smc_sock *smc)
888 {
889 	struct sock *clcsk = smc->clcsock->sk;
890 
891 	write_lock_bh(&clcsk->sk_callback_lock);
892 	clcsk->sk_user_data = (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
893 
894 	smc_clcsock_replace_cb(&clcsk->sk_state_change, smc_fback_state_change,
895 			       &smc->clcsk_state_change);
896 	smc_clcsock_replace_cb(&clcsk->sk_data_ready, smc_fback_data_ready,
897 			       &smc->clcsk_data_ready);
898 	smc_clcsock_replace_cb(&clcsk->sk_write_space, smc_fback_write_space,
899 			       &smc->clcsk_write_space);
900 	smc_clcsock_replace_cb(&clcsk->sk_error_report, smc_fback_error_report,
901 			       &smc->clcsk_error_report);
902 
903 	write_unlock_bh(&clcsk->sk_callback_lock);
904 }
905 
906 static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
907 {
908 	int rc = 0;
909 
910 	mutex_lock(&smc->clcsock_release_lock);
911 	if (!smc->clcsock) {
912 		rc = -EBADF;
913 		goto out;
914 	}
915 
916 	smc->use_fallback = true;
917 	smc->fallback_rsn = reason_code;
918 	smc_stat_fallback(smc);
919 	trace_smc_switch_to_fallback(smc, reason_code);
920 	if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
921 		smc->clcsock->file = smc->sk.sk_socket->file;
922 		smc->clcsock->file->private_data = smc->clcsock;
923 		smc->clcsock->wq.fasync_list =
924 			smc->sk.sk_socket->wq.fasync_list;
925 
926 		/* There might be some wait entries remaining
927 		 * in smc sk->sk_wq and they should be woken up
928 		 * as clcsock's wait queue is woken up.
929 		 */
930 		smc_fback_replace_callbacks(smc);
931 	}
932 out:
933 	mutex_unlock(&smc->clcsock_release_lock);
934 	return rc;
935 }
936 
937 /* fall back during connect */
938 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
939 {
940 	struct net *net = sock_net(&smc->sk);
941 	int rc = 0;
942 
943 	rc = smc_switch_to_fallback(smc, reason_code);
944 	if (rc) { /* fallback fails */
945 		this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
946 		if (smc->sk.sk_state == SMC_INIT)
947 			sock_put(&smc->sk); /* passive closing */
948 		return rc;
949 	}
950 	smc_copy_sock_settings_to_clc(smc);
951 	smc->connect_nonblock = 0;
952 	if (smc->sk.sk_state == SMC_INIT)
953 		smc->sk.sk_state = SMC_ACTIVE;
954 	return 0;
955 }
956 
957 /* decline and fall back during connect */
958 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
959 					u8 version)
960 {
961 	struct net *net = sock_net(&smc->sk);
962 	int rc;
963 
964 	if (reason_code < 0) { /* error, fallback is not possible */
965 		this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
966 		if (smc->sk.sk_state == SMC_INIT)
967 			sock_put(&smc->sk); /* passive closing */
968 		return reason_code;
969 	}
970 	if (reason_code != SMC_CLC_DECL_PEERDECL) {
971 		rc = smc_clc_send_decline(smc, reason_code, version);
972 		if (rc < 0) {
973 			this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
974 			if (smc->sk.sk_state == SMC_INIT)
975 				sock_put(&smc->sk); /* passive closing */
976 			return rc;
977 		}
978 	}
979 	return smc_connect_fallback(smc, reason_code);
980 }
981 
982 static void smc_conn_abort(struct smc_sock *smc, int local_first)
983 {
984 	struct smc_connection *conn = &smc->conn;
985 	struct smc_link_group *lgr = conn->lgr;
986 	bool lgr_valid = false;
987 
988 	if (smc_conn_lgr_valid(conn))
989 		lgr_valid = true;
990 
991 	smc_conn_free(conn);
992 	if (local_first && lgr_valid)
993 		smc_lgr_cleanup_early(lgr);
994 }
995 
996 /* check if there is a rdma device available for this connection. */
997 /* called for connect and listen */
998 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
999 {
1000 	/* PNET table look up: search active ib_device and port
1001 	 * within same PNETID that also contains the ethernet device
1002 	 * used for the internal TCP socket
1003 	 */
1004 	smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
1005 	if (!ini->check_smcrv2 && !ini->ib_dev)
1006 		return SMC_CLC_DECL_NOSMCRDEV;
1007 	if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
1008 		return SMC_CLC_DECL_NOSMCRDEV;
1009 	return 0;
1010 }
1011 
1012 /* check if there is an ISM device available for this connection. */
1013 /* called for connect and listen */
1014 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
1015 {
1016 	/* Find ISM device with same PNETID as connecting interface  */
1017 	smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
1018 	if (!ini->ism_dev[0])
1019 		return SMC_CLC_DECL_NOSMCDDEV;
1020 	else
1021 		ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
1022 	return 0;
1023 }
1024 
1025 /* is chid unique for the ism devices that are already determined? */
1026 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
1027 					   int cnt)
1028 {
1029 	int i = (!ini->ism_dev[0]) ? 1 : 0;
1030 
1031 	for (; i < cnt; i++)
1032 		if (ini->ism_chid[i] == chid)
1033 			return false;
1034 	return true;
1035 }
1036 
1037 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
1038  * PNETID matching net_device)
1039  */
1040 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
1041 				       struct smc_init_info *ini)
1042 {
1043 	int rc = SMC_CLC_DECL_NOSMCDDEV;
1044 	struct smcd_dev *smcd;
1045 	int i = 1;
1046 	u16 chid;
1047 
1048 	if (smcd_indicated(ini->smc_type_v1))
1049 		rc = 0;		/* already initialized for V1 */
1050 	mutex_lock(&smcd_dev_list.mutex);
1051 	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1052 		if (smcd->going_away || smcd == ini->ism_dev[0])
1053 			continue;
1054 		chid = smc_ism_get_chid(smcd);
1055 		if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
1056 			continue;
1057 		if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
1058 		    smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
1059 			ini->ism_dev[i] = smcd;
1060 			ini->ism_chid[i] = chid;
1061 			ini->is_smcd = true;
1062 			rc = 0;
1063 			i++;
1064 			if (i > SMC_MAX_ISM_DEVS)
1065 				break;
1066 		}
1067 	}
1068 	mutex_unlock(&smcd_dev_list.mutex);
1069 	ini->ism_offered_cnt = i - 1;
1070 	if (!ini->ism_dev[0] && !ini->ism_dev[1])
1071 		ini->smcd_version = 0;
1072 
1073 	return rc;
1074 }
1075 
1076 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
1077 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
1078 				      struct smc_init_info *ini)
1079 {
1080 	if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
1081 		return SMC_CLC_DECL_ISMVLANERR;
1082 	return 0;
1083 }
1084 
1085 static int smc_find_proposal_devices(struct smc_sock *smc,
1086 				     struct smc_init_info *ini)
1087 {
1088 	int rc = 0;
1089 
1090 	/* check if there is an ism device available */
1091 	if (!(ini->smcd_version & SMC_V1) ||
1092 	    smc_find_ism_device(smc, ini) ||
1093 	    smc_connect_ism_vlan_setup(smc, ini))
1094 		ini->smcd_version &= ~SMC_V1;
1095 	/* else ISM V1 is supported for this connection */
1096 
1097 	/* check if there is an rdma device available */
1098 	if (!(ini->smcr_version & SMC_V1) ||
1099 	    smc_find_rdma_device(smc, ini))
1100 		ini->smcr_version &= ~SMC_V1;
1101 	/* else RDMA is supported for this connection */
1102 
1103 	ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
1104 					      ini->smcr_version & SMC_V1);
1105 
1106 	/* check if there is an ism v2 device available */
1107 	if (!(ini->smcd_version & SMC_V2) ||
1108 	    !smc_ism_is_v2_capable() ||
1109 	    smc_find_ism_v2_device_clnt(smc, ini))
1110 		ini->smcd_version &= ~SMC_V2;
1111 
1112 	/* check if there is an rdma v2 device available */
1113 	ini->check_smcrv2 = true;
1114 	ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
1115 	if (!(ini->smcr_version & SMC_V2) ||
1116 	    smc->clcsock->sk->sk_family != AF_INET ||
1117 	    !smc_clc_ueid_count() ||
1118 	    smc_find_rdma_device(smc, ini))
1119 		ini->smcr_version &= ~SMC_V2;
1120 	ini->check_smcrv2 = false;
1121 
1122 	ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
1123 					      ini->smcr_version & SMC_V2);
1124 
1125 	/* if neither ISM nor RDMA are supported, fallback */
1126 	if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
1127 		rc = SMC_CLC_DECL_NOSMCDEV;
1128 
1129 	return rc;
1130 }
1131 
1132 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
1133  * used, the VLAN ID will be registered again during the connection setup.
1134  */
1135 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
1136 					struct smc_init_info *ini)
1137 {
1138 	if (!smcd_indicated(ini->smc_type_v1))
1139 		return 0;
1140 	if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
1141 		return SMC_CLC_DECL_CNFERR;
1142 	return 0;
1143 }
1144 
1145 #define SMC_CLC_MAX_ACCEPT_LEN \
1146 	(sizeof(struct smc_clc_msg_accept_confirm_v2) + \
1147 	 sizeof(struct smc_clc_first_contact_ext) + \
1148 	 sizeof(struct smc_clc_msg_trail))
1149 
1150 /* CLC handshake during connect */
1151 static int smc_connect_clc(struct smc_sock *smc,
1152 			   struct smc_clc_msg_accept_confirm_v2 *aclc2,
1153 			   struct smc_init_info *ini)
1154 {
1155 	int rc = 0;
1156 
1157 	/* do inband token exchange */
1158 	rc = smc_clc_send_proposal(smc, ini);
1159 	if (rc)
1160 		return rc;
1161 	/* receive SMC Accept CLC message */
1162 	return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
1163 				SMC_CLC_ACCEPT, CLC_WAIT_TIME);
1164 }
1165 
1166 void smc_fill_gid_list(struct smc_link_group *lgr,
1167 		       struct smc_gidlist *gidlist,
1168 		       struct smc_ib_device *known_dev, u8 *known_gid)
1169 {
1170 	struct smc_init_info *alt_ini = NULL;
1171 
1172 	memset(gidlist, 0, sizeof(*gidlist));
1173 	memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
1174 
1175 	alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
1176 	if (!alt_ini)
1177 		goto out;
1178 
1179 	alt_ini->vlan_id = lgr->vlan_id;
1180 	alt_ini->check_smcrv2 = true;
1181 	alt_ini->smcrv2.saddr = lgr->saddr;
1182 	smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
1183 
1184 	if (!alt_ini->smcrv2.ib_dev_v2)
1185 		goto out;
1186 
1187 	memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
1188 	       SMC_GID_SIZE);
1189 
1190 out:
1191 	kfree(alt_ini);
1192 }
1193 
1194 static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
1195 				       struct smc_clc_msg_accept_confirm *aclc,
1196 				       struct smc_init_info *ini)
1197 {
1198 	struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1199 		(struct smc_clc_msg_accept_confirm_v2 *)aclc;
1200 	struct smc_clc_first_contact_ext *fce =
1201 		(struct smc_clc_first_contact_ext *)
1202 			(((u8 *)clc_v2) + sizeof(*clc_v2));
1203 
1204 	if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
1205 		return 0;
1206 
1207 	if (fce->v2_direct) {
1208 		memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
1209 		ini->smcrv2.uses_gateway = false;
1210 	} else {
1211 		if (smc_ib_find_route(smc->clcsock->sk->sk_rcv_saddr,
1212 				      smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
1213 				      ini->smcrv2.nexthop_mac,
1214 				      &ini->smcrv2.uses_gateway))
1215 			return SMC_CLC_DECL_NOROUTE;
1216 		if (!ini->smcrv2.uses_gateway) {
1217 			/* mismatch: peer claims indirect, but its direct */
1218 			return SMC_CLC_DECL_NOINDIRECT;
1219 		}
1220 	}
1221 	return 0;
1222 }
1223 
1224 /* setup for RDMA connection of client */
1225 static int smc_connect_rdma(struct smc_sock *smc,
1226 			    struct smc_clc_msg_accept_confirm *aclc,
1227 			    struct smc_init_info *ini)
1228 {
1229 	int i, reason_code = 0;
1230 	struct smc_link *link;
1231 	u8 *eid = NULL;
1232 
1233 	ini->is_smcd = false;
1234 	ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
1235 	ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1236 	memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
1237 	memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
1238 	memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
1239 
1240 	reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
1241 	if (reason_code)
1242 		return reason_code;
1243 
1244 	mutex_lock(&smc_client_lgr_pending);
1245 	reason_code = smc_conn_create(smc, ini);
1246 	if (reason_code) {
1247 		mutex_unlock(&smc_client_lgr_pending);
1248 		return reason_code;
1249 	}
1250 
1251 	smc_conn_save_peer_info(smc, aclc);
1252 
1253 	if (ini->first_contact_local) {
1254 		link = smc->conn.lnk;
1255 	} else {
1256 		/* set link that was assigned by server */
1257 		link = NULL;
1258 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1259 			struct smc_link *l = &smc->conn.lgr->lnk[i];
1260 
1261 			if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
1262 			    !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
1263 				    SMC_GID_SIZE) &&
1264 			    (aclc->hdr.version > SMC_V1 ||
1265 			     !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
1266 				     sizeof(l->peer_mac)))) {
1267 				link = l;
1268 				break;
1269 			}
1270 		}
1271 		if (!link) {
1272 			reason_code = SMC_CLC_DECL_NOSRVLINK;
1273 			goto connect_abort;
1274 		}
1275 		smc_switch_link_and_count(&smc->conn, link);
1276 	}
1277 
1278 	/* create send buffer and rmb */
1279 	if (smc_buf_create(smc, false)) {
1280 		reason_code = SMC_CLC_DECL_MEM;
1281 		goto connect_abort;
1282 	}
1283 
1284 	if (ini->first_contact_local)
1285 		smc_link_save_peer_info(link, aclc, ini);
1286 
1287 	if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
1288 		reason_code = SMC_CLC_DECL_ERR_RTOK;
1289 		goto connect_abort;
1290 	}
1291 
1292 	smc_close_init(smc);
1293 	smc_rx_init(smc);
1294 
1295 	if (ini->first_contact_local) {
1296 		if (smc_ib_ready_link(link)) {
1297 			reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1298 			goto connect_abort;
1299 		}
1300 	} else {
1301 		/* reg sendbufs if they were vzalloced */
1302 		if (smc->conn.sndbuf_desc->is_vm) {
1303 			if (smcr_lgr_reg_sndbufs(link, smc->conn.sndbuf_desc)) {
1304 				reason_code = SMC_CLC_DECL_ERR_REGBUF;
1305 				goto connect_abort;
1306 			}
1307 		}
1308 		if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
1309 			reason_code = SMC_CLC_DECL_ERR_REGBUF;
1310 			goto connect_abort;
1311 		}
1312 	}
1313 
1314 	if (aclc->hdr.version > SMC_V1) {
1315 		struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1316 			(struct smc_clc_msg_accept_confirm_v2 *)aclc;
1317 
1318 		eid = clc_v2->r1.eid;
1319 		if (ini->first_contact_local)
1320 			smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
1321 					  link->smcibdev, link->gid);
1322 	}
1323 
1324 	reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
1325 					   aclc->hdr.version, eid, ini);
1326 	if (reason_code)
1327 		goto connect_abort;
1328 
1329 	smc_tx_init(smc);
1330 
1331 	if (ini->first_contact_local) {
1332 		/* QP confirmation over RoCE fabric */
1333 		smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1334 		reason_code = smcr_clnt_conf_first_link(smc);
1335 		smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1336 		if (reason_code)
1337 			goto connect_abort;
1338 	}
1339 	mutex_unlock(&smc_client_lgr_pending);
1340 
1341 	smc_copy_sock_settings_to_clc(smc);
1342 	smc->connect_nonblock = 0;
1343 	if (smc->sk.sk_state == SMC_INIT)
1344 		smc->sk.sk_state = SMC_ACTIVE;
1345 
1346 	return 0;
1347 connect_abort:
1348 	smc_conn_abort(smc, ini->first_contact_local);
1349 	mutex_unlock(&smc_client_lgr_pending);
1350 	smc->connect_nonblock = 0;
1351 
1352 	return reason_code;
1353 }
1354 
1355 /* The server has chosen one of the proposed ISM devices for the communication.
1356  * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
1357  */
1358 static int
1359 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
1360 			       struct smc_init_info *ini)
1361 {
1362 	int i;
1363 
1364 	for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
1365 		if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
1366 			ini->ism_selected = i;
1367 			return 0;
1368 		}
1369 	}
1370 
1371 	return -EPROTO;
1372 }
1373 
1374 /* setup for ISM connection of client */
1375 static int smc_connect_ism(struct smc_sock *smc,
1376 			   struct smc_clc_msg_accept_confirm *aclc,
1377 			   struct smc_init_info *ini)
1378 {
1379 	u8 *eid = NULL;
1380 	int rc = 0;
1381 
1382 	ini->is_smcd = true;
1383 	ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1384 
1385 	if (aclc->hdr.version == SMC_V2) {
1386 		struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1387 			(struct smc_clc_msg_accept_confirm_v2 *)aclc;
1388 
1389 		rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1390 		if (rc)
1391 			return rc;
1392 	}
1393 	ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
1394 
1395 	/* there is only one lgr role for SMC-D; use server lock */
1396 	mutex_lock(&smc_server_lgr_pending);
1397 	rc = smc_conn_create(smc, ini);
1398 	if (rc) {
1399 		mutex_unlock(&smc_server_lgr_pending);
1400 		return rc;
1401 	}
1402 
1403 	/* Create send and receive buffers */
1404 	rc = smc_buf_create(smc, true);
1405 	if (rc) {
1406 		rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1407 		goto connect_abort;
1408 	}
1409 
1410 	smc_conn_save_peer_info(smc, aclc);
1411 	smc_close_init(smc);
1412 	smc_rx_init(smc);
1413 	smc_tx_init(smc);
1414 
1415 	if (aclc->hdr.version > SMC_V1) {
1416 		struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1417 			(struct smc_clc_msg_accept_confirm_v2 *)aclc;
1418 
1419 		eid = clc_v2->d1.eid;
1420 	}
1421 
1422 	rc = smc_clc_send_confirm(smc, ini->first_contact_local,
1423 				  aclc->hdr.version, eid, NULL);
1424 	if (rc)
1425 		goto connect_abort;
1426 	mutex_unlock(&smc_server_lgr_pending);
1427 
1428 	smc_copy_sock_settings_to_clc(smc);
1429 	smc->connect_nonblock = 0;
1430 	if (smc->sk.sk_state == SMC_INIT)
1431 		smc->sk.sk_state = SMC_ACTIVE;
1432 
1433 	return 0;
1434 connect_abort:
1435 	smc_conn_abort(smc, ini->first_contact_local);
1436 	mutex_unlock(&smc_server_lgr_pending);
1437 	smc->connect_nonblock = 0;
1438 
1439 	return rc;
1440 }
1441 
1442 /* check if received accept type and version matches a proposed one */
1443 static int smc_connect_check_aclc(struct smc_init_info *ini,
1444 				  struct smc_clc_msg_accept_confirm *aclc)
1445 {
1446 	if (aclc->hdr.typev1 != SMC_TYPE_R &&
1447 	    aclc->hdr.typev1 != SMC_TYPE_D)
1448 		return SMC_CLC_DECL_MODEUNSUPP;
1449 
1450 	if (aclc->hdr.version >= SMC_V2) {
1451 		if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1452 		     !smcr_indicated(ini->smc_type_v2)) ||
1453 		    (aclc->hdr.typev1 == SMC_TYPE_D &&
1454 		     !smcd_indicated(ini->smc_type_v2)))
1455 			return SMC_CLC_DECL_MODEUNSUPP;
1456 	} else {
1457 		if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1458 		     !smcr_indicated(ini->smc_type_v1)) ||
1459 		    (aclc->hdr.typev1 == SMC_TYPE_D &&
1460 		     !smcd_indicated(ini->smc_type_v1)))
1461 			return SMC_CLC_DECL_MODEUNSUPP;
1462 	}
1463 
1464 	return 0;
1465 }
1466 
1467 /* perform steps before actually connecting */
1468 static int __smc_connect(struct smc_sock *smc)
1469 {
1470 	u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
1471 	struct smc_clc_msg_accept_confirm_v2 *aclc2;
1472 	struct smc_clc_msg_accept_confirm *aclc;
1473 	struct smc_init_info *ini = NULL;
1474 	u8 *buf = NULL;
1475 	int rc = 0;
1476 
1477 	if (smc->use_fallback)
1478 		return smc_connect_fallback(smc, smc->fallback_rsn);
1479 
1480 	/* if peer has not signalled SMC-capability, fall back */
1481 	if (!tcp_sk(smc->clcsock->sk)->syn_smc)
1482 		return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
1483 
1484 	/* IPSec connections opt out of SMC optimizations */
1485 	if (using_ipsec(smc))
1486 		return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1487 						    version);
1488 
1489 	ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1490 	if (!ini)
1491 		return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1492 						    version);
1493 
1494 	ini->smcd_version = SMC_V1 | SMC_V2;
1495 	ini->smcr_version = SMC_V1 | SMC_V2;
1496 	ini->smc_type_v1 = SMC_TYPE_B;
1497 	ini->smc_type_v2 = SMC_TYPE_B;
1498 
1499 	/* get vlan id from IP device */
1500 	if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
1501 		ini->smcd_version &= ~SMC_V1;
1502 		ini->smcr_version = 0;
1503 		ini->smc_type_v1 = SMC_TYPE_N;
1504 		if (!ini->smcd_version) {
1505 			rc = SMC_CLC_DECL_GETVLANERR;
1506 			goto fallback;
1507 		}
1508 	}
1509 
1510 	rc = smc_find_proposal_devices(smc, ini);
1511 	if (rc)
1512 		goto fallback;
1513 
1514 	buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1515 	if (!buf) {
1516 		rc = SMC_CLC_DECL_MEM;
1517 		goto fallback;
1518 	}
1519 	aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1520 	aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1521 
1522 	/* perform CLC handshake */
1523 	rc = smc_connect_clc(smc, aclc2, ini);
1524 	if (rc) {
1525 		/* -EAGAIN on timeout, see tcp_recvmsg() */
1526 		if (rc == -EAGAIN) {
1527 			rc = -ETIMEDOUT;
1528 			smc->sk.sk_err = ETIMEDOUT;
1529 		}
1530 		goto vlan_cleanup;
1531 	}
1532 
1533 	/* check if smc modes and versions of CLC proposal and accept match */
1534 	rc = smc_connect_check_aclc(ini, aclc);
1535 	version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1536 	if (rc)
1537 		goto vlan_cleanup;
1538 
1539 	/* depending on previous steps, connect using rdma or ism */
1540 	if (aclc->hdr.typev1 == SMC_TYPE_R) {
1541 		ini->smcr_version = version;
1542 		rc = smc_connect_rdma(smc, aclc, ini);
1543 	} else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1544 		ini->smcd_version = version;
1545 		rc = smc_connect_ism(smc, aclc, ini);
1546 	}
1547 	if (rc)
1548 		goto vlan_cleanup;
1549 
1550 	SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
1551 	smc_connect_ism_vlan_cleanup(smc, ini);
1552 	kfree(buf);
1553 	kfree(ini);
1554 	return 0;
1555 
1556 vlan_cleanup:
1557 	smc_connect_ism_vlan_cleanup(smc, ini);
1558 	kfree(buf);
1559 fallback:
1560 	kfree(ini);
1561 	return smc_connect_decline_fallback(smc, rc, version);
1562 }
1563 
1564 static void smc_connect_work(struct work_struct *work)
1565 {
1566 	struct smc_sock *smc = container_of(work, struct smc_sock,
1567 					    connect_work);
1568 	long timeo = smc->sk.sk_sndtimeo;
1569 	int rc = 0;
1570 
1571 	if (!timeo)
1572 		timeo = MAX_SCHEDULE_TIMEOUT;
1573 	lock_sock(smc->clcsock->sk);
1574 	if (smc->clcsock->sk->sk_err) {
1575 		smc->sk.sk_err = smc->clcsock->sk->sk_err;
1576 	} else if ((1 << smc->clcsock->sk->sk_state) &
1577 					(TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1578 		rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1579 		if ((rc == -EPIPE) &&
1580 		    ((1 << smc->clcsock->sk->sk_state) &
1581 					(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1582 			rc = 0;
1583 	}
1584 	release_sock(smc->clcsock->sk);
1585 	lock_sock(&smc->sk);
1586 	if (rc != 0 || smc->sk.sk_err) {
1587 		smc->sk.sk_state = SMC_CLOSED;
1588 		if (rc == -EPIPE || rc == -EAGAIN)
1589 			smc->sk.sk_err = EPIPE;
1590 		else if (rc == -ECONNREFUSED)
1591 			smc->sk.sk_err = ECONNREFUSED;
1592 		else if (signal_pending(current))
1593 			smc->sk.sk_err = -sock_intr_errno(timeo);
1594 		sock_put(&smc->sk); /* passive closing */
1595 		goto out;
1596 	}
1597 
1598 	rc = __smc_connect(smc);
1599 	if (rc < 0)
1600 		smc->sk.sk_err = -rc;
1601 
1602 out:
1603 	if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1604 		if (smc->sk.sk_err) {
1605 			smc->sk.sk_state_change(&smc->sk);
1606 		} else { /* allow polling before and after fallback decision */
1607 			smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1608 			smc->sk.sk_write_space(&smc->sk);
1609 		}
1610 	}
1611 	release_sock(&smc->sk);
1612 }
1613 
1614 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1615 		       int alen, int flags)
1616 {
1617 	struct sock *sk = sock->sk;
1618 	struct smc_sock *smc;
1619 	int rc = -EINVAL;
1620 
1621 	smc = smc_sk(sk);
1622 
1623 	/* separate smc parameter checking to be safe */
1624 	if (alen < sizeof(addr->sa_family))
1625 		goto out_err;
1626 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1627 		goto out_err;
1628 
1629 	lock_sock(sk);
1630 	switch (sock->state) {
1631 	default:
1632 		rc = -EINVAL;
1633 		goto out;
1634 	case SS_CONNECTED:
1635 		rc = sk->sk_state == SMC_ACTIVE ? -EISCONN : -EINVAL;
1636 		goto out;
1637 	case SS_CONNECTING:
1638 		if (sk->sk_state == SMC_ACTIVE)
1639 			goto connected;
1640 		break;
1641 	case SS_UNCONNECTED:
1642 		sock->state = SS_CONNECTING;
1643 		break;
1644 	}
1645 
1646 	switch (sk->sk_state) {
1647 	default:
1648 		goto out;
1649 	case SMC_CLOSED:
1650 		rc = sock_error(sk) ? : -ECONNABORTED;
1651 		sock->state = SS_UNCONNECTED;
1652 		goto out;
1653 	case SMC_ACTIVE:
1654 		rc = -EISCONN;
1655 		goto out;
1656 	case SMC_INIT:
1657 		break;
1658 	}
1659 
1660 	smc_copy_sock_settings_to_clc(smc);
1661 	tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1662 	if (smc->connect_nonblock) {
1663 		rc = -EALREADY;
1664 		goto out;
1665 	}
1666 	rc = kernel_connect(smc->clcsock, addr, alen, flags);
1667 	if (rc && rc != -EINPROGRESS)
1668 		goto out;
1669 
1670 	if (smc->use_fallback) {
1671 		sock->state = rc ? SS_CONNECTING : SS_CONNECTED;
1672 		goto out;
1673 	}
1674 	sock_hold(&smc->sk); /* sock put in passive closing */
1675 	if (flags & O_NONBLOCK) {
1676 		if (queue_work(smc_hs_wq, &smc->connect_work))
1677 			smc->connect_nonblock = 1;
1678 		rc = -EINPROGRESS;
1679 		goto out;
1680 	} else {
1681 		rc = __smc_connect(smc);
1682 		if (rc < 0)
1683 			goto out;
1684 	}
1685 
1686 connected:
1687 	rc = 0;
1688 	sock->state = SS_CONNECTED;
1689 out:
1690 	release_sock(sk);
1691 out_err:
1692 	return rc;
1693 }
1694 
1695 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1696 {
1697 	struct socket *new_clcsock = NULL;
1698 	struct sock *lsk = &lsmc->sk;
1699 	struct sock *new_sk;
1700 	int rc = -EINVAL;
1701 
1702 	release_sock(lsk);
1703 	new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1704 	if (!new_sk) {
1705 		rc = -ENOMEM;
1706 		lsk->sk_err = ENOMEM;
1707 		*new_smc = NULL;
1708 		lock_sock(lsk);
1709 		goto out;
1710 	}
1711 	*new_smc = smc_sk(new_sk);
1712 
1713 	mutex_lock(&lsmc->clcsock_release_lock);
1714 	if (lsmc->clcsock)
1715 		rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1716 	mutex_unlock(&lsmc->clcsock_release_lock);
1717 	lock_sock(lsk);
1718 	if  (rc < 0 && rc != -EAGAIN)
1719 		lsk->sk_err = -rc;
1720 	if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1721 		new_sk->sk_prot->unhash(new_sk);
1722 		if (new_clcsock)
1723 			sock_release(new_clcsock);
1724 		new_sk->sk_state = SMC_CLOSED;
1725 		sock_set_flag(new_sk, SOCK_DEAD);
1726 		sock_put(new_sk); /* final */
1727 		*new_smc = NULL;
1728 		goto out;
1729 	}
1730 
1731 	/* new clcsock has inherited the smc listen-specific sk_data_ready
1732 	 * function; switch it back to the original sk_data_ready function
1733 	 */
1734 	new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1735 
1736 	/* if new clcsock has also inherited the fallback-specific callback
1737 	 * functions, switch them back to the original ones.
1738 	 */
1739 	if (lsmc->use_fallback) {
1740 		if (lsmc->clcsk_state_change)
1741 			new_clcsock->sk->sk_state_change = lsmc->clcsk_state_change;
1742 		if (lsmc->clcsk_write_space)
1743 			new_clcsock->sk->sk_write_space = lsmc->clcsk_write_space;
1744 		if (lsmc->clcsk_error_report)
1745 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
1746 	}
1747 
1748 	(*new_smc)->clcsock = new_clcsock;
1749 out:
1750 	return rc;
1751 }
1752 
1753 /* add a just created sock to the accept queue of the listen sock as
1754  * candidate for a following socket accept call from user space
1755  */
1756 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1757 {
1758 	struct smc_sock *par = smc_sk(parent);
1759 
1760 	sock_hold(sk); /* sock_put in smc_accept_unlink () */
1761 	spin_lock(&par->accept_q_lock);
1762 	list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1763 	spin_unlock(&par->accept_q_lock);
1764 	sk_acceptq_added(parent);
1765 }
1766 
1767 /* remove a socket from the accept queue of its parental listening socket */
1768 static void smc_accept_unlink(struct sock *sk)
1769 {
1770 	struct smc_sock *par = smc_sk(sk)->listen_smc;
1771 
1772 	spin_lock(&par->accept_q_lock);
1773 	list_del_init(&smc_sk(sk)->accept_q);
1774 	spin_unlock(&par->accept_q_lock);
1775 	sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1776 	sock_put(sk); /* sock_hold in smc_accept_enqueue */
1777 }
1778 
1779 /* remove a sock from the accept queue to bind it to a new socket created
1780  * for a socket accept call from user space
1781  */
1782 struct sock *smc_accept_dequeue(struct sock *parent,
1783 				struct socket *new_sock)
1784 {
1785 	struct smc_sock *isk, *n;
1786 	struct sock *new_sk;
1787 
1788 	list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1789 		new_sk = (struct sock *)isk;
1790 
1791 		smc_accept_unlink(new_sk);
1792 		if (new_sk->sk_state == SMC_CLOSED) {
1793 			new_sk->sk_prot->unhash(new_sk);
1794 			if (isk->clcsock) {
1795 				sock_release(isk->clcsock);
1796 				isk->clcsock = NULL;
1797 			}
1798 			sock_put(new_sk); /* final */
1799 			continue;
1800 		}
1801 		if (new_sock) {
1802 			sock_graft(new_sk, new_sock);
1803 			new_sock->state = SS_CONNECTED;
1804 			if (isk->use_fallback) {
1805 				smc_sk(new_sk)->clcsock->file = new_sock->file;
1806 				isk->clcsock->file->private_data = isk->clcsock;
1807 			}
1808 		}
1809 		return new_sk;
1810 	}
1811 	return NULL;
1812 }
1813 
1814 /* clean up for a created but never accepted sock */
1815 void smc_close_non_accepted(struct sock *sk)
1816 {
1817 	struct smc_sock *smc = smc_sk(sk);
1818 
1819 	sock_hold(sk); /* sock_put below */
1820 	lock_sock(sk);
1821 	if (!sk->sk_lingertime)
1822 		/* wait for peer closing */
1823 		sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
1824 	__smc_release(smc);
1825 	release_sock(sk);
1826 	sock_put(sk); /* sock_hold above */
1827 	sock_put(sk); /* final sock_put */
1828 }
1829 
1830 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1831 {
1832 	struct smc_link *link = smc->conn.lnk;
1833 	struct smc_llc_qentry *qentry;
1834 	int rc;
1835 
1836 	/* reg the sndbuf if it was vzalloced*/
1837 	if (smc->conn.sndbuf_desc->is_vm) {
1838 		if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
1839 			return SMC_CLC_DECL_ERR_REGBUF;
1840 	}
1841 
1842 	/* reg the rmb */
1843 	if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
1844 		return SMC_CLC_DECL_ERR_REGBUF;
1845 
1846 	/* send CONFIRM LINK request to client over the RoCE fabric */
1847 	rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1848 	if (rc < 0)
1849 		return SMC_CLC_DECL_TIMEOUT_CL;
1850 
1851 	/* receive CONFIRM LINK response from client over the RoCE fabric */
1852 	qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1853 			      SMC_LLC_CONFIRM_LINK);
1854 	if (!qentry) {
1855 		struct smc_clc_msg_decline dclc;
1856 
1857 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1858 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1859 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1860 	}
1861 	smc_llc_save_peer_uid(qentry);
1862 	rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1863 	smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1864 	if (rc)
1865 		return SMC_CLC_DECL_RMBE_EC;
1866 
1867 	/* confirm_rkey is implicit on 1st contact */
1868 	smc->conn.rmb_desc->is_conf_rkey = true;
1869 
1870 	smc_llc_link_active(link);
1871 	smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1872 
1873 	down_write(&link->lgr->llc_conf_mutex);
1874 	/* initial contact - try to establish second link */
1875 	smc_llc_srv_add_link(link, NULL);
1876 	up_write(&link->lgr->llc_conf_mutex);
1877 	return 0;
1878 }
1879 
1880 /* listen worker: finish */
1881 static void smc_listen_out(struct smc_sock *new_smc)
1882 {
1883 	struct smc_sock *lsmc = new_smc->listen_smc;
1884 	struct sock *newsmcsk = &new_smc->sk;
1885 
1886 	if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
1887 		atomic_dec(&lsmc->queued_smc_hs);
1888 
1889 	if (lsmc->sk.sk_state == SMC_LISTEN) {
1890 		lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1891 		smc_accept_enqueue(&lsmc->sk, newsmcsk);
1892 		release_sock(&lsmc->sk);
1893 	} else { /* no longer listening */
1894 		smc_close_non_accepted(newsmcsk);
1895 	}
1896 
1897 	/* Wake up accept */
1898 	lsmc->sk.sk_data_ready(&lsmc->sk);
1899 	sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1900 }
1901 
1902 /* listen worker: finish in state connected */
1903 static void smc_listen_out_connected(struct smc_sock *new_smc)
1904 {
1905 	struct sock *newsmcsk = &new_smc->sk;
1906 
1907 	if (newsmcsk->sk_state == SMC_INIT)
1908 		newsmcsk->sk_state = SMC_ACTIVE;
1909 
1910 	smc_listen_out(new_smc);
1911 }
1912 
1913 /* listen worker: finish in error state */
1914 static void smc_listen_out_err(struct smc_sock *new_smc)
1915 {
1916 	struct sock *newsmcsk = &new_smc->sk;
1917 	struct net *net = sock_net(newsmcsk);
1918 
1919 	this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
1920 	if (newsmcsk->sk_state == SMC_INIT)
1921 		sock_put(&new_smc->sk); /* passive closing */
1922 	newsmcsk->sk_state = SMC_CLOSED;
1923 
1924 	smc_listen_out(new_smc);
1925 }
1926 
1927 /* listen worker: decline and fall back if possible */
1928 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1929 			       int local_first, u8 version)
1930 {
1931 	/* RDMA setup failed, switch back to TCP */
1932 	smc_conn_abort(new_smc, local_first);
1933 	if (reason_code < 0 ||
1934 	    smc_switch_to_fallback(new_smc, reason_code)) {
1935 		/* error, no fallback possible */
1936 		smc_listen_out_err(new_smc);
1937 		return;
1938 	}
1939 	if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1940 		if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1941 			smc_listen_out_err(new_smc);
1942 			return;
1943 		}
1944 	}
1945 	smc_listen_out_connected(new_smc);
1946 }
1947 
1948 /* listen worker: version checking */
1949 static int smc_listen_v2_check(struct smc_sock *new_smc,
1950 			       struct smc_clc_msg_proposal *pclc,
1951 			       struct smc_init_info *ini)
1952 {
1953 	struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1954 	struct smc_clc_v2_extension *pclc_v2_ext;
1955 	int rc = SMC_CLC_DECL_PEERNOSMC;
1956 
1957 	ini->smc_type_v1 = pclc->hdr.typev1;
1958 	ini->smc_type_v2 = pclc->hdr.typev2;
1959 	ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1960 	ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1961 	if (pclc->hdr.version > SMC_V1) {
1962 		if (smcd_indicated(ini->smc_type_v2))
1963 			ini->smcd_version |= SMC_V2;
1964 		if (smcr_indicated(ini->smc_type_v2))
1965 			ini->smcr_version |= SMC_V2;
1966 	}
1967 	if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1968 		rc = SMC_CLC_DECL_PEERNOSMC;
1969 		goto out;
1970 	}
1971 	pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1972 	if (!pclc_v2_ext) {
1973 		ini->smcd_version &= ~SMC_V2;
1974 		ini->smcr_version &= ~SMC_V2;
1975 		rc = SMC_CLC_DECL_NOV2EXT;
1976 		goto out;
1977 	}
1978 	pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1979 	if (ini->smcd_version & SMC_V2) {
1980 		if (!smc_ism_is_v2_capable()) {
1981 			ini->smcd_version &= ~SMC_V2;
1982 			rc = SMC_CLC_DECL_NOISM2SUPP;
1983 		} else if (!pclc_smcd_v2_ext) {
1984 			ini->smcd_version &= ~SMC_V2;
1985 			rc = SMC_CLC_DECL_NOV2DEXT;
1986 		} else if (!pclc_v2_ext->hdr.eid_cnt &&
1987 			   !pclc_v2_ext->hdr.flag.seid) {
1988 			ini->smcd_version &= ~SMC_V2;
1989 			rc = SMC_CLC_DECL_NOUEID;
1990 		}
1991 	}
1992 	if (ini->smcr_version & SMC_V2) {
1993 		if (!pclc_v2_ext->hdr.eid_cnt) {
1994 			ini->smcr_version &= ~SMC_V2;
1995 			rc = SMC_CLC_DECL_NOUEID;
1996 		}
1997 	}
1998 
1999 out:
2000 	if (!ini->smcd_version && !ini->smcr_version)
2001 		return rc;
2002 
2003 	return 0;
2004 }
2005 
2006 /* listen worker: check prefixes */
2007 static int smc_listen_prfx_check(struct smc_sock *new_smc,
2008 				 struct smc_clc_msg_proposal *pclc)
2009 {
2010 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
2011 	struct socket *newclcsock = new_smc->clcsock;
2012 
2013 	if (pclc->hdr.typev1 == SMC_TYPE_N)
2014 		return 0;
2015 	pclc_prfx = smc_clc_proposal_get_prefix(pclc);
2016 	if (smc_clc_prfx_match(newclcsock, pclc_prfx))
2017 		return SMC_CLC_DECL_DIFFPREFIX;
2018 
2019 	return 0;
2020 }
2021 
2022 /* listen worker: initialize connection and buffers */
2023 static int smc_listen_rdma_init(struct smc_sock *new_smc,
2024 				struct smc_init_info *ini)
2025 {
2026 	int rc;
2027 
2028 	/* allocate connection / link group */
2029 	rc = smc_conn_create(new_smc, ini);
2030 	if (rc)
2031 		return rc;
2032 
2033 	/* create send buffer and rmb */
2034 	if (smc_buf_create(new_smc, false)) {
2035 		smc_conn_abort(new_smc, ini->first_contact_local);
2036 		return SMC_CLC_DECL_MEM;
2037 	}
2038 
2039 	return 0;
2040 }
2041 
2042 /* listen worker: initialize connection and buffers for SMC-D */
2043 static int smc_listen_ism_init(struct smc_sock *new_smc,
2044 			       struct smc_init_info *ini)
2045 {
2046 	int rc;
2047 
2048 	rc = smc_conn_create(new_smc, ini);
2049 	if (rc)
2050 		return rc;
2051 
2052 	/* Create send and receive buffers */
2053 	rc = smc_buf_create(new_smc, true);
2054 	if (rc) {
2055 		smc_conn_abort(new_smc, ini->first_contact_local);
2056 		return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
2057 					 SMC_CLC_DECL_MEM;
2058 	}
2059 
2060 	return 0;
2061 }
2062 
2063 static bool smc_is_already_selected(struct smcd_dev *smcd,
2064 				    struct smc_init_info *ini,
2065 				    int matches)
2066 {
2067 	int i;
2068 
2069 	for (i = 0; i < matches; i++)
2070 		if (smcd == ini->ism_dev[i])
2071 			return true;
2072 
2073 	return false;
2074 }
2075 
2076 /* check for ISM devices matching proposed ISM devices */
2077 static void smc_check_ism_v2_match(struct smc_init_info *ini,
2078 				   u16 proposed_chid, u64 proposed_gid,
2079 				   unsigned int *matches)
2080 {
2081 	struct smcd_dev *smcd;
2082 
2083 	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2084 		if (smcd->going_away)
2085 			continue;
2086 		if (smc_is_already_selected(smcd, ini, *matches))
2087 			continue;
2088 		if (smc_ism_get_chid(smcd) == proposed_chid &&
2089 		    !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
2090 			ini->ism_peer_gid[*matches] = proposed_gid;
2091 			ini->ism_dev[*matches] = smcd;
2092 			(*matches)++;
2093 			break;
2094 		}
2095 	}
2096 }
2097 
2098 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
2099 {
2100 	if (!ini->rc)
2101 		ini->rc = rc;
2102 }
2103 
2104 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
2105 					struct smc_clc_msg_proposal *pclc,
2106 					struct smc_init_info *ini)
2107 {
2108 	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
2109 	struct smc_clc_v2_extension *smc_v2_ext;
2110 	struct smc_clc_msg_smcd *pclc_smcd;
2111 	unsigned int matches = 0;
2112 	u8 smcd_version;
2113 	u8 *eid = NULL;
2114 	int i, rc;
2115 
2116 	if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
2117 		goto not_found;
2118 
2119 	pclc_smcd = smc_get_clc_msg_smcd(pclc);
2120 	smc_v2_ext = smc_get_clc_v2_ext(pclc);
2121 	smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
2122 
2123 	mutex_lock(&smcd_dev_list.mutex);
2124 	if (pclc_smcd->ism.chid)
2125 		/* check for ISM device matching proposed native ISM device */
2126 		smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
2127 				       ntohll(pclc_smcd->ism.gid), &matches);
2128 	for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
2129 		/* check for ISM devices matching proposed non-native ISM
2130 		 * devices
2131 		 */
2132 		smc_check_ism_v2_match(ini,
2133 				       ntohs(smcd_v2_ext->gidchid[i - 1].chid),
2134 				       ntohll(smcd_v2_ext->gidchid[i - 1].gid),
2135 				       &matches);
2136 	}
2137 	mutex_unlock(&smcd_dev_list.mutex);
2138 
2139 	if (!ini->ism_dev[0]) {
2140 		smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
2141 		goto not_found;
2142 	}
2143 
2144 	smc_ism_get_system_eid(&eid);
2145 	if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
2146 			       smcd_v2_ext->system_eid, eid))
2147 		goto not_found;
2148 
2149 	/* separate - outside the smcd_dev_list.lock */
2150 	smcd_version = ini->smcd_version;
2151 	for (i = 0; i < matches; i++) {
2152 		ini->smcd_version = SMC_V2;
2153 		ini->is_smcd = true;
2154 		ini->ism_selected = i;
2155 		rc = smc_listen_ism_init(new_smc, ini);
2156 		if (rc) {
2157 			smc_find_ism_store_rc(rc, ini);
2158 			/* try next active ISM device */
2159 			continue;
2160 		}
2161 		return; /* matching and usable V2 ISM device found */
2162 	}
2163 	/* no V2 ISM device could be initialized */
2164 	ini->smcd_version = smcd_version;	/* restore original value */
2165 	ini->negotiated_eid[0] = 0;
2166 
2167 not_found:
2168 	ini->smcd_version &= ~SMC_V2;
2169 	ini->ism_dev[0] = NULL;
2170 	ini->is_smcd = false;
2171 }
2172 
2173 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
2174 					struct smc_clc_msg_proposal *pclc,
2175 					struct smc_init_info *ini)
2176 {
2177 	struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
2178 	int rc = 0;
2179 
2180 	/* check if ISM V1 is available */
2181 	if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
2182 		goto not_found;
2183 	ini->is_smcd = true; /* prepare ISM check */
2184 	ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
2185 	rc = smc_find_ism_device(new_smc, ini);
2186 	if (rc)
2187 		goto not_found;
2188 	ini->ism_selected = 0;
2189 	rc = smc_listen_ism_init(new_smc, ini);
2190 	if (!rc)
2191 		return;		/* V1 ISM device found */
2192 
2193 not_found:
2194 	smc_find_ism_store_rc(rc, ini);
2195 	ini->smcd_version &= ~SMC_V1;
2196 	ini->ism_dev[0] = NULL;
2197 	ini->is_smcd = false;
2198 }
2199 
2200 /* listen worker: register buffers */
2201 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
2202 {
2203 	struct smc_connection *conn = &new_smc->conn;
2204 
2205 	if (!local_first) {
2206 		/* reg sendbufs if they were vzalloced */
2207 		if (conn->sndbuf_desc->is_vm) {
2208 			if (smcr_lgr_reg_sndbufs(conn->lnk,
2209 						 conn->sndbuf_desc))
2210 				return SMC_CLC_DECL_ERR_REGBUF;
2211 		}
2212 		if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
2213 			return SMC_CLC_DECL_ERR_REGBUF;
2214 	}
2215 
2216 	return 0;
2217 }
2218 
2219 static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
2220 					 struct smc_clc_msg_proposal *pclc,
2221 					 struct smc_init_info *ini)
2222 {
2223 	struct smc_clc_v2_extension *smc_v2_ext;
2224 	u8 smcr_version;
2225 	int rc;
2226 
2227 	if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
2228 		goto not_found;
2229 
2230 	smc_v2_ext = smc_get_clc_v2_ext(pclc);
2231 	if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
2232 		goto not_found;
2233 
2234 	/* prepare RDMA check */
2235 	memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2236 	memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
2237 	memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2238 	ini->check_smcrv2 = true;
2239 	ini->smcrv2.clc_sk = new_smc->clcsock->sk;
2240 	ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
2241 	ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
2242 	rc = smc_find_rdma_device(new_smc, ini);
2243 	if (rc) {
2244 		smc_find_ism_store_rc(rc, ini);
2245 		goto not_found;
2246 	}
2247 	if (!ini->smcrv2.uses_gateway)
2248 		memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
2249 
2250 	smcr_version = ini->smcr_version;
2251 	ini->smcr_version = SMC_V2;
2252 	rc = smc_listen_rdma_init(new_smc, ini);
2253 	if (!rc) {
2254 		rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2255 		if (rc)
2256 			smc_conn_abort(new_smc, ini->first_contact_local);
2257 	}
2258 	if (!rc)
2259 		return;
2260 	ini->smcr_version = smcr_version;
2261 	smc_find_ism_store_rc(rc, ini);
2262 
2263 not_found:
2264 	ini->smcr_version &= ~SMC_V2;
2265 	ini->smcrv2.ib_dev_v2 = NULL;
2266 	ini->check_smcrv2 = false;
2267 }
2268 
2269 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
2270 					struct smc_clc_msg_proposal *pclc,
2271 					struct smc_init_info *ini)
2272 {
2273 	int rc;
2274 
2275 	if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
2276 		return SMC_CLC_DECL_NOSMCDEV;
2277 
2278 	/* prepare RDMA check */
2279 	memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2280 	memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
2281 	memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2282 	rc = smc_find_rdma_device(new_smc, ini);
2283 	if (rc) {
2284 		/* no RDMA device found */
2285 		return SMC_CLC_DECL_NOSMCDEV;
2286 	}
2287 	rc = smc_listen_rdma_init(new_smc, ini);
2288 	if (rc)
2289 		return rc;
2290 	return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2291 }
2292 
2293 /* determine the local device matching to proposal */
2294 static int smc_listen_find_device(struct smc_sock *new_smc,
2295 				  struct smc_clc_msg_proposal *pclc,
2296 				  struct smc_init_info *ini)
2297 {
2298 	int prfx_rc;
2299 
2300 	/* check for ISM device matching V2 proposed device */
2301 	smc_find_ism_v2_device_serv(new_smc, pclc, ini);
2302 	if (ini->ism_dev[0])
2303 		return 0;
2304 
2305 	/* check for matching IP prefix and subnet length (V1) */
2306 	prfx_rc = smc_listen_prfx_check(new_smc, pclc);
2307 	if (prfx_rc)
2308 		smc_find_ism_store_rc(prfx_rc, ini);
2309 
2310 	/* get vlan id from IP device */
2311 	if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
2312 		return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
2313 
2314 	/* check for ISM device matching V1 proposed device */
2315 	if (!prfx_rc)
2316 		smc_find_ism_v1_device_serv(new_smc, pclc, ini);
2317 	if (ini->ism_dev[0])
2318 		return 0;
2319 
2320 	if (!smcr_indicated(pclc->hdr.typev1) &&
2321 	    !smcr_indicated(pclc->hdr.typev2))
2322 		/* skip RDMA and decline */
2323 		return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
2324 
2325 	/* check if RDMA V2 is available */
2326 	smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
2327 	if (ini->smcrv2.ib_dev_v2)
2328 		return 0;
2329 
2330 	/* check if RDMA V1 is available */
2331 	if (!prfx_rc) {
2332 		int rc;
2333 
2334 		rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
2335 		smc_find_ism_store_rc(rc, ini);
2336 		return (!rc) ? 0 : ini->rc;
2337 	}
2338 	return SMC_CLC_DECL_NOSMCDEV;
2339 }
2340 
2341 /* listen worker: finish RDMA setup */
2342 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
2343 				  struct smc_clc_msg_accept_confirm *cclc,
2344 				  bool local_first,
2345 				  struct smc_init_info *ini)
2346 {
2347 	struct smc_link *link = new_smc->conn.lnk;
2348 	int reason_code = 0;
2349 
2350 	if (local_first)
2351 		smc_link_save_peer_info(link, cclc, ini);
2352 
2353 	if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
2354 		return SMC_CLC_DECL_ERR_RTOK;
2355 
2356 	if (local_first) {
2357 		if (smc_ib_ready_link(link))
2358 			return SMC_CLC_DECL_ERR_RDYLNK;
2359 		/* QP confirmation over RoCE fabric */
2360 		smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
2361 		reason_code = smcr_serv_conf_first_link(new_smc);
2362 		smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
2363 	}
2364 	return reason_code;
2365 }
2366 
2367 /* setup for connection of server */
2368 static void smc_listen_work(struct work_struct *work)
2369 {
2370 	struct smc_sock *new_smc = container_of(work, struct smc_sock,
2371 						smc_listen_work);
2372 	struct socket *newclcsock = new_smc->clcsock;
2373 	struct smc_clc_msg_accept_confirm *cclc;
2374 	struct smc_clc_msg_proposal_area *buf;
2375 	struct smc_clc_msg_proposal *pclc;
2376 	struct smc_init_info *ini = NULL;
2377 	u8 proposal_version = SMC_V1;
2378 	u8 accept_version;
2379 	int rc = 0;
2380 
2381 	if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
2382 		return smc_listen_out_err(new_smc);
2383 
2384 	if (new_smc->use_fallback) {
2385 		smc_listen_out_connected(new_smc);
2386 		return;
2387 	}
2388 
2389 	/* check if peer is smc capable */
2390 	if (!tcp_sk(newclcsock->sk)->syn_smc) {
2391 		rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
2392 		if (rc)
2393 			smc_listen_out_err(new_smc);
2394 		else
2395 			smc_listen_out_connected(new_smc);
2396 		return;
2397 	}
2398 
2399 	/* do inband token exchange -
2400 	 * wait for and receive SMC Proposal CLC message
2401 	 */
2402 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
2403 	if (!buf) {
2404 		rc = SMC_CLC_DECL_MEM;
2405 		goto out_decl;
2406 	}
2407 	pclc = (struct smc_clc_msg_proposal *)buf;
2408 	rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
2409 			      SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
2410 	if (rc)
2411 		goto out_decl;
2412 
2413 	if (pclc->hdr.version > SMC_V1)
2414 		proposal_version = SMC_V2;
2415 
2416 	/* IPSec connections opt out of SMC optimizations */
2417 	if (using_ipsec(new_smc)) {
2418 		rc = SMC_CLC_DECL_IPSEC;
2419 		goto out_decl;
2420 	}
2421 
2422 	ini = kzalloc(sizeof(*ini), GFP_KERNEL);
2423 	if (!ini) {
2424 		rc = SMC_CLC_DECL_MEM;
2425 		goto out_decl;
2426 	}
2427 
2428 	/* initial version checking */
2429 	rc = smc_listen_v2_check(new_smc, pclc, ini);
2430 	if (rc)
2431 		goto out_decl;
2432 
2433 	mutex_lock(&smc_server_lgr_pending);
2434 	smc_close_init(new_smc);
2435 	smc_rx_init(new_smc);
2436 	smc_tx_init(new_smc);
2437 
2438 	/* determine ISM or RoCE device used for connection */
2439 	rc = smc_listen_find_device(new_smc, pclc, ini);
2440 	if (rc)
2441 		goto out_unlock;
2442 
2443 	/* send SMC Accept CLC message */
2444 	accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
2445 	rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
2446 				 accept_version, ini->negotiated_eid);
2447 	if (rc)
2448 		goto out_unlock;
2449 
2450 	/* SMC-D does not need this lock any more */
2451 	if (ini->is_smcd)
2452 		mutex_unlock(&smc_server_lgr_pending);
2453 
2454 	/* receive SMC Confirm CLC message */
2455 	memset(buf, 0, sizeof(*buf));
2456 	cclc = (struct smc_clc_msg_accept_confirm *)buf;
2457 	rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
2458 			      SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2459 	if (rc) {
2460 		if (!ini->is_smcd)
2461 			goto out_unlock;
2462 		goto out_decl;
2463 	}
2464 
2465 	/* finish worker */
2466 	if (!ini->is_smcd) {
2467 		rc = smc_listen_rdma_finish(new_smc, cclc,
2468 					    ini->first_contact_local, ini);
2469 		if (rc)
2470 			goto out_unlock;
2471 		mutex_unlock(&smc_server_lgr_pending);
2472 	}
2473 	smc_conn_save_peer_info(new_smc, cclc);
2474 	smc_listen_out_connected(new_smc);
2475 	SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
2476 	goto out_free;
2477 
2478 out_unlock:
2479 	mutex_unlock(&smc_server_lgr_pending);
2480 out_decl:
2481 	smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
2482 			   proposal_version);
2483 out_free:
2484 	kfree(ini);
2485 	kfree(buf);
2486 }
2487 
2488 static void smc_tcp_listen_work(struct work_struct *work)
2489 {
2490 	struct smc_sock *lsmc = container_of(work, struct smc_sock,
2491 					     tcp_listen_work);
2492 	struct sock *lsk = &lsmc->sk;
2493 	struct smc_sock *new_smc;
2494 	int rc = 0;
2495 
2496 	lock_sock(lsk);
2497 	while (lsk->sk_state == SMC_LISTEN) {
2498 		rc = smc_clcsock_accept(lsmc, &new_smc);
2499 		if (rc) /* clcsock accept queue empty or error */
2500 			goto out;
2501 		if (!new_smc)
2502 			continue;
2503 
2504 		if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
2505 			atomic_inc(&lsmc->queued_smc_hs);
2506 
2507 		new_smc->listen_smc = lsmc;
2508 		new_smc->use_fallback = lsmc->use_fallback;
2509 		new_smc->fallback_rsn = lsmc->fallback_rsn;
2510 		sock_hold(lsk); /* sock_put in smc_listen_work */
2511 		INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2512 		smc_copy_sock_settings_to_smc(new_smc);
2513 		sock_hold(&new_smc->sk); /* sock_put in passive closing */
2514 		if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
2515 			sock_put(&new_smc->sk);
2516 	}
2517 
2518 out:
2519 	release_sock(lsk);
2520 	sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2521 }
2522 
2523 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2524 {
2525 	struct smc_sock *lsmc;
2526 
2527 	read_lock_bh(&listen_clcsock->sk_callback_lock);
2528 	lsmc = smc_clcsock_user_data(listen_clcsock);
2529 	if (!lsmc)
2530 		goto out;
2531 	lsmc->clcsk_data_ready(listen_clcsock);
2532 	if (lsmc->sk.sk_state == SMC_LISTEN) {
2533 		sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
2534 		if (!queue_work(smc_tcp_ls_wq, &lsmc->tcp_listen_work))
2535 			sock_put(&lsmc->sk);
2536 	}
2537 out:
2538 	read_unlock_bh(&listen_clcsock->sk_callback_lock);
2539 }
2540 
2541 static int smc_listen(struct socket *sock, int backlog)
2542 {
2543 	struct sock *sk = sock->sk;
2544 	struct smc_sock *smc;
2545 	int rc;
2546 
2547 	smc = smc_sk(sk);
2548 	lock_sock(sk);
2549 
2550 	rc = -EINVAL;
2551 	if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2552 	    smc->connect_nonblock || sock->state != SS_UNCONNECTED)
2553 		goto out;
2554 
2555 	rc = 0;
2556 	if (sk->sk_state == SMC_LISTEN) {
2557 		sk->sk_max_ack_backlog = backlog;
2558 		goto out;
2559 	}
2560 	/* some socket options are handled in core, so we could not apply
2561 	 * them to the clc socket -- copy smc socket options to clc socket
2562 	 */
2563 	smc_copy_sock_settings_to_clc(smc);
2564 	if (!smc->use_fallback)
2565 		tcp_sk(smc->clcsock->sk)->syn_smc = 1;
2566 
2567 	/* save original sk_data_ready function and establish
2568 	 * smc-specific sk_data_ready function
2569 	 */
2570 	write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2571 	smc->clcsock->sk->sk_user_data =
2572 		(void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
2573 	smc_clcsock_replace_cb(&smc->clcsock->sk->sk_data_ready,
2574 			       smc_clcsock_data_ready, &smc->clcsk_data_ready);
2575 	write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2576 
2577 	/* save original ops */
2578 	smc->ori_af_ops = inet_csk(smc->clcsock->sk)->icsk_af_ops;
2579 
2580 	smc->af_ops = *smc->ori_af_ops;
2581 	smc->af_ops.syn_recv_sock = smc_tcp_syn_recv_sock;
2582 
2583 	inet_csk(smc->clcsock->sk)->icsk_af_ops = &smc->af_ops;
2584 
2585 	if (smc->limit_smc_hs)
2586 		tcp_sk(smc->clcsock->sk)->smc_hs_congested = smc_hs_congested;
2587 
2588 	rc = kernel_listen(smc->clcsock, backlog);
2589 	if (rc) {
2590 		write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2591 		smc_clcsock_restore_cb(&smc->clcsock->sk->sk_data_ready,
2592 				       &smc->clcsk_data_ready);
2593 		smc->clcsock->sk->sk_user_data = NULL;
2594 		write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2595 		goto out;
2596 	}
2597 	sk->sk_max_ack_backlog = backlog;
2598 	sk->sk_ack_backlog = 0;
2599 	sk->sk_state = SMC_LISTEN;
2600 
2601 out:
2602 	release_sock(sk);
2603 	return rc;
2604 }
2605 
2606 static int smc_accept(struct socket *sock, struct socket *new_sock,
2607 		      int flags, bool kern)
2608 {
2609 	struct sock *sk = sock->sk, *nsk;
2610 	DECLARE_WAITQUEUE(wait, current);
2611 	struct smc_sock *lsmc;
2612 	long timeo;
2613 	int rc = 0;
2614 
2615 	lsmc = smc_sk(sk);
2616 	sock_hold(sk); /* sock_put below */
2617 	lock_sock(sk);
2618 
2619 	if (lsmc->sk.sk_state != SMC_LISTEN) {
2620 		rc = -EINVAL;
2621 		release_sock(sk);
2622 		goto out;
2623 	}
2624 
2625 	/* Wait for an incoming connection */
2626 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2627 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
2628 	while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2629 		set_current_state(TASK_INTERRUPTIBLE);
2630 		if (!timeo) {
2631 			rc = -EAGAIN;
2632 			break;
2633 		}
2634 		release_sock(sk);
2635 		timeo = schedule_timeout(timeo);
2636 		/* wakeup by sk_data_ready in smc_listen_work() */
2637 		sched_annotate_sleep();
2638 		lock_sock(sk);
2639 		if (signal_pending(current)) {
2640 			rc = sock_intr_errno(timeo);
2641 			break;
2642 		}
2643 	}
2644 	set_current_state(TASK_RUNNING);
2645 	remove_wait_queue(sk_sleep(sk), &wait);
2646 
2647 	if (!rc)
2648 		rc = sock_error(nsk);
2649 	release_sock(sk);
2650 	if (rc)
2651 		goto out;
2652 
2653 	if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2654 		/* wait till data arrives on the socket */
2655 		timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2656 								MSEC_PER_SEC);
2657 		if (smc_sk(nsk)->use_fallback) {
2658 			struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2659 
2660 			lock_sock(clcsk);
2661 			if (skb_queue_empty(&clcsk->sk_receive_queue))
2662 				sk_wait_data(clcsk, &timeo, NULL);
2663 			release_sock(clcsk);
2664 		} else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2665 			lock_sock(nsk);
2666 			smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
2667 			release_sock(nsk);
2668 		}
2669 	}
2670 
2671 out:
2672 	sock_put(sk); /* sock_hold above */
2673 	return rc;
2674 }
2675 
2676 static int smc_getname(struct socket *sock, struct sockaddr *addr,
2677 		       int peer)
2678 {
2679 	struct smc_sock *smc;
2680 
2681 	if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2682 	    (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
2683 		return -ENOTCONN;
2684 
2685 	smc = smc_sk(sock->sk);
2686 
2687 	return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
2688 }
2689 
2690 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2691 {
2692 	struct sock *sk = sock->sk;
2693 	struct smc_sock *smc;
2694 	int rc;
2695 
2696 	smc = smc_sk(sk);
2697 	lock_sock(sk);
2698 
2699 	/* SMC does not support connect with fastopen */
2700 	if (msg->msg_flags & MSG_FASTOPEN) {
2701 		/* not connected yet, fallback */
2702 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2703 			rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2704 			if (rc)
2705 				goto out;
2706 		} else {
2707 			rc = -EINVAL;
2708 			goto out;
2709 		}
2710 	} else if ((sk->sk_state != SMC_ACTIVE) &&
2711 		   (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2712 		   (sk->sk_state != SMC_INIT)) {
2713 		rc = -EPIPE;
2714 		goto out;
2715 	}
2716 
2717 	if (smc->use_fallback) {
2718 		rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2719 	} else {
2720 		rc = smc_tx_sendmsg(smc, msg, len);
2721 		SMC_STAT_TX_PAYLOAD(smc, len, rc);
2722 	}
2723 out:
2724 	release_sock(sk);
2725 	return rc;
2726 }
2727 
2728 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2729 		       int flags)
2730 {
2731 	struct sock *sk = sock->sk;
2732 	struct smc_sock *smc;
2733 	int rc = -ENOTCONN;
2734 
2735 	smc = smc_sk(sk);
2736 	lock_sock(sk);
2737 	if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2738 		/* socket was connected before, no more data to read */
2739 		rc = 0;
2740 		goto out;
2741 	}
2742 	if ((sk->sk_state == SMC_INIT) ||
2743 	    (sk->sk_state == SMC_LISTEN) ||
2744 	    (sk->sk_state == SMC_CLOSED))
2745 		goto out;
2746 
2747 	if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2748 		rc = 0;
2749 		goto out;
2750 	}
2751 
2752 	if (smc->use_fallback) {
2753 		rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2754 	} else {
2755 		msg->msg_namelen = 0;
2756 		rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2757 		SMC_STAT_RX_PAYLOAD(smc, rc, rc);
2758 	}
2759 
2760 out:
2761 	release_sock(sk);
2762 	return rc;
2763 }
2764 
2765 static __poll_t smc_accept_poll(struct sock *parent)
2766 {
2767 	struct smc_sock *isk = smc_sk(parent);
2768 	__poll_t mask = 0;
2769 
2770 	spin_lock(&isk->accept_q_lock);
2771 	if (!list_empty(&isk->accept_q))
2772 		mask = EPOLLIN | EPOLLRDNORM;
2773 	spin_unlock(&isk->accept_q_lock);
2774 
2775 	return mask;
2776 }
2777 
2778 static __poll_t smc_poll(struct file *file, struct socket *sock,
2779 			     poll_table *wait)
2780 {
2781 	struct sock *sk = sock->sk;
2782 	struct smc_sock *smc;
2783 	__poll_t mask = 0;
2784 
2785 	if (!sk)
2786 		return EPOLLNVAL;
2787 
2788 	smc = smc_sk(sock->sk);
2789 	if (smc->use_fallback) {
2790 		/* delegate to CLC child sock */
2791 		mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2792 		sk->sk_err = smc->clcsock->sk->sk_err;
2793 	} else {
2794 		if (sk->sk_state != SMC_CLOSED)
2795 			sock_poll_wait(file, sock, wait);
2796 		if (sk->sk_err)
2797 			mask |= EPOLLERR;
2798 		if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2799 		    (sk->sk_state == SMC_CLOSED))
2800 			mask |= EPOLLHUP;
2801 		if (sk->sk_state == SMC_LISTEN) {
2802 			/* woken up by sk_data_ready in smc_listen_work() */
2803 			mask |= smc_accept_poll(sk);
2804 		} else if (smc->use_fallback) { /* as result of connect_work()*/
2805 			mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2806 							   wait);
2807 			sk->sk_err = smc->clcsock->sk->sk_err;
2808 		} else {
2809 			if ((sk->sk_state != SMC_INIT &&
2810 			     atomic_read(&smc->conn.sndbuf_space)) ||
2811 			    sk->sk_shutdown & SEND_SHUTDOWN) {
2812 				mask |= EPOLLOUT | EPOLLWRNORM;
2813 			} else {
2814 				sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2815 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2816 			}
2817 			if (atomic_read(&smc->conn.bytes_to_rcv))
2818 				mask |= EPOLLIN | EPOLLRDNORM;
2819 			if (sk->sk_shutdown & RCV_SHUTDOWN)
2820 				mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2821 			if (sk->sk_state == SMC_APPCLOSEWAIT1)
2822 				mask |= EPOLLIN;
2823 			if (smc->conn.urg_state == SMC_URG_VALID)
2824 				mask |= EPOLLPRI;
2825 		}
2826 	}
2827 
2828 	return mask;
2829 }
2830 
2831 static int smc_shutdown(struct socket *sock, int how)
2832 {
2833 	struct sock *sk = sock->sk;
2834 	bool do_shutdown = true;
2835 	struct smc_sock *smc;
2836 	int rc = -EINVAL;
2837 	int old_state;
2838 	int rc1 = 0;
2839 
2840 	smc = smc_sk(sk);
2841 
2842 	if ((how < SHUT_RD) || (how > SHUT_RDWR))
2843 		return rc;
2844 
2845 	lock_sock(sk);
2846 
2847 	if (sock->state == SS_CONNECTING) {
2848 		if (sk->sk_state == SMC_ACTIVE)
2849 			sock->state = SS_CONNECTED;
2850 		else if (sk->sk_state == SMC_PEERCLOSEWAIT1 ||
2851 			 sk->sk_state == SMC_PEERCLOSEWAIT2 ||
2852 			 sk->sk_state == SMC_APPCLOSEWAIT1 ||
2853 			 sk->sk_state == SMC_APPCLOSEWAIT2 ||
2854 			 sk->sk_state == SMC_APPFINCLOSEWAIT)
2855 			sock->state = SS_DISCONNECTING;
2856 	}
2857 
2858 	rc = -ENOTCONN;
2859 	if ((sk->sk_state != SMC_ACTIVE) &&
2860 	    (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2861 	    (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2862 	    (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2863 	    (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2864 	    (sk->sk_state != SMC_APPFINCLOSEWAIT))
2865 		goto out;
2866 	if (smc->use_fallback) {
2867 		rc = kernel_sock_shutdown(smc->clcsock, how);
2868 		sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2869 		if (sk->sk_shutdown == SHUTDOWN_MASK) {
2870 			sk->sk_state = SMC_CLOSED;
2871 			sk->sk_socket->state = SS_UNCONNECTED;
2872 			sock_put(sk);
2873 		}
2874 		goto out;
2875 	}
2876 	switch (how) {
2877 	case SHUT_RDWR:		/* shutdown in both directions */
2878 		old_state = sk->sk_state;
2879 		rc = smc_close_active(smc);
2880 		if (old_state == SMC_ACTIVE &&
2881 		    sk->sk_state == SMC_PEERCLOSEWAIT1)
2882 			do_shutdown = false;
2883 		break;
2884 	case SHUT_WR:
2885 		rc = smc_close_shutdown_write(smc);
2886 		break;
2887 	case SHUT_RD:
2888 		rc = 0;
2889 		/* nothing more to do because peer is not involved */
2890 		break;
2891 	}
2892 	if (do_shutdown && smc->clcsock)
2893 		rc1 = kernel_sock_shutdown(smc->clcsock, how);
2894 	/* map sock_shutdown_cmd constants to sk_shutdown value range */
2895 	sk->sk_shutdown |= how + 1;
2896 
2897 	if (sk->sk_state == SMC_CLOSED)
2898 		sock->state = SS_UNCONNECTED;
2899 	else
2900 		sock->state = SS_DISCONNECTING;
2901 out:
2902 	release_sock(sk);
2903 	return rc ? rc : rc1;
2904 }
2905 
2906 static int __smc_getsockopt(struct socket *sock, int level, int optname,
2907 			    char __user *optval, int __user *optlen)
2908 {
2909 	struct smc_sock *smc;
2910 	int val, len;
2911 
2912 	smc = smc_sk(sock->sk);
2913 
2914 	if (get_user(len, optlen))
2915 		return -EFAULT;
2916 
2917 	len = min_t(int, len, sizeof(int));
2918 
2919 	if (len < 0)
2920 		return -EINVAL;
2921 
2922 	switch (optname) {
2923 	case SMC_LIMIT_HS:
2924 		val = smc->limit_smc_hs;
2925 		break;
2926 	default:
2927 		return -EOPNOTSUPP;
2928 	}
2929 
2930 	if (put_user(len, optlen))
2931 		return -EFAULT;
2932 	if (copy_to_user(optval, &val, len))
2933 		return -EFAULT;
2934 
2935 	return 0;
2936 }
2937 
2938 static int __smc_setsockopt(struct socket *sock, int level, int optname,
2939 			    sockptr_t optval, unsigned int optlen)
2940 {
2941 	struct sock *sk = sock->sk;
2942 	struct smc_sock *smc;
2943 	int val, rc;
2944 
2945 	smc = smc_sk(sk);
2946 
2947 	lock_sock(sk);
2948 	switch (optname) {
2949 	case SMC_LIMIT_HS:
2950 		if (optlen < sizeof(int)) {
2951 			rc = -EINVAL;
2952 			break;
2953 		}
2954 		if (copy_from_sockptr(&val, optval, sizeof(int))) {
2955 			rc = -EFAULT;
2956 			break;
2957 		}
2958 
2959 		smc->limit_smc_hs = !!val;
2960 		rc = 0;
2961 		break;
2962 	default:
2963 		rc = -EOPNOTSUPP;
2964 		break;
2965 	}
2966 	release_sock(sk);
2967 
2968 	return rc;
2969 }
2970 
2971 static int smc_setsockopt(struct socket *sock, int level, int optname,
2972 			  sockptr_t optval, unsigned int optlen)
2973 {
2974 	struct sock *sk = sock->sk;
2975 	struct smc_sock *smc;
2976 	int val, rc;
2977 
2978 	if (level == SOL_TCP && optname == TCP_ULP)
2979 		return -EOPNOTSUPP;
2980 	else if (level == SOL_SMC)
2981 		return __smc_setsockopt(sock, level, optname, optval, optlen);
2982 
2983 	smc = smc_sk(sk);
2984 
2985 	/* generic setsockopts reaching us here always apply to the
2986 	 * CLC socket
2987 	 */
2988 	mutex_lock(&smc->clcsock_release_lock);
2989 	if (!smc->clcsock) {
2990 		mutex_unlock(&smc->clcsock_release_lock);
2991 		return -EBADF;
2992 	}
2993 	if (unlikely(!smc->clcsock->ops->setsockopt))
2994 		rc = -EOPNOTSUPP;
2995 	else
2996 		rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2997 						   optval, optlen);
2998 	if (smc->clcsock->sk->sk_err) {
2999 		sk->sk_err = smc->clcsock->sk->sk_err;
3000 		sk_error_report(sk);
3001 	}
3002 	mutex_unlock(&smc->clcsock_release_lock);
3003 
3004 	if (optlen < sizeof(int))
3005 		return -EINVAL;
3006 	if (copy_from_sockptr(&val, optval, sizeof(int)))
3007 		return -EFAULT;
3008 
3009 	lock_sock(sk);
3010 	if (rc || smc->use_fallback)
3011 		goto out;
3012 	switch (optname) {
3013 	case TCP_FASTOPEN:
3014 	case TCP_FASTOPEN_CONNECT:
3015 	case TCP_FASTOPEN_KEY:
3016 	case TCP_FASTOPEN_NO_COOKIE:
3017 		/* option not supported by SMC */
3018 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
3019 			rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
3020 		} else {
3021 			rc = -EINVAL;
3022 		}
3023 		break;
3024 	case TCP_NODELAY:
3025 		if (sk->sk_state != SMC_INIT &&
3026 		    sk->sk_state != SMC_LISTEN &&
3027 		    sk->sk_state != SMC_CLOSED) {
3028 			if (val) {
3029 				SMC_STAT_INC(smc, ndly_cnt);
3030 				smc_tx_pending(&smc->conn);
3031 				cancel_delayed_work(&smc->conn.tx_work);
3032 			}
3033 		}
3034 		break;
3035 	case TCP_CORK:
3036 		if (sk->sk_state != SMC_INIT &&
3037 		    sk->sk_state != SMC_LISTEN &&
3038 		    sk->sk_state != SMC_CLOSED) {
3039 			if (!val) {
3040 				SMC_STAT_INC(smc, cork_cnt);
3041 				smc_tx_pending(&smc->conn);
3042 				cancel_delayed_work(&smc->conn.tx_work);
3043 			}
3044 		}
3045 		break;
3046 	case TCP_DEFER_ACCEPT:
3047 		smc->sockopt_defer_accept = val;
3048 		break;
3049 	default:
3050 		break;
3051 	}
3052 out:
3053 	release_sock(sk);
3054 
3055 	return rc;
3056 }
3057 
3058 static int smc_getsockopt(struct socket *sock, int level, int optname,
3059 			  char __user *optval, int __user *optlen)
3060 {
3061 	struct smc_sock *smc;
3062 	int rc;
3063 
3064 	if (level == SOL_SMC)
3065 		return __smc_getsockopt(sock, level, optname, optval, optlen);
3066 
3067 	smc = smc_sk(sock->sk);
3068 	mutex_lock(&smc->clcsock_release_lock);
3069 	if (!smc->clcsock) {
3070 		mutex_unlock(&smc->clcsock_release_lock);
3071 		return -EBADF;
3072 	}
3073 	/* socket options apply to the CLC socket */
3074 	if (unlikely(!smc->clcsock->ops->getsockopt)) {
3075 		mutex_unlock(&smc->clcsock_release_lock);
3076 		return -EOPNOTSUPP;
3077 	}
3078 	rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
3079 					   optval, optlen);
3080 	mutex_unlock(&smc->clcsock_release_lock);
3081 	return rc;
3082 }
3083 
3084 static int smc_ioctl(struct socket *sock, unsigned int cmd,
3085 		     unsigned long arg)
3086 {
3087 	union smc_host_cursor cons, urg;
3088 	struct smc_connection *conn;
3089 	struct smc_sock *smc;
3090 	int answ;
3091 
3092 	smc = smc_sk(sock->sk);
3093 	conn = &smc->conn;
3094 	lock_sock(&smc->sk);
3095 	if (smc->use_fallback) {
3096 		if (!smc->clcsock) {
3097 			release_sock(&smc->sk);
3098 			return -EBADF;
3099 		}
3100 		answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
3101 		release_sock(&smc->sk);
3102 		return answ;
3103 	}
3104 	switch (cmd) {
3105 	case SIOCINQ: /* same as FIONREAD */
3106 		if (smc->sk.sk_state == SMC_LISTEN) {
3107 			release_sock(&smc->sk);
3108 			return -EINVAL;
3109 		}
3110 		if (smc->sk.sk_state == SMC_INIT ||
3111 		    smc->sk.sk_state == SMC_CLOSED)
3112 			answ = 0;
3113 		else
3114 			answ = atomic_read(&smc->conn.bytes_to_rcv);
3115 		break;
3116 	case SIOCOUTQ:
3117 		/* output queue size (not send + not acked) */
3118 		if (smc->sk.sk_state == SMC_LISTEN) {
3119 			release_sock(&smc->sk);
3120 			return -EINVAL;
3121 		}
3122 		if (smc->sk.sk_state == SMC_INIT ||
3123 		    smc->sk.sk_state == SMC_CLOSED)
3124 			answ = 0;
3125 		else
3126 			answ = smc->conn.sndbuf_desc->len -
3127 					atomic_read(&smc->conn.sndbuf_space);
3128 		break;
3129 	case SIOCOUTQNSD:
3130 		/* output queue size (not send only) */
3131 		if (smc->sk.sk_state == SMC_LISTEN) {
3132 			release_sock(&smc->sk);
3133 			return -EINVAL;
3134 		}
3135 		if (smc->sk.sk_state == SMC_INIT ||
3136 		    smc->sk.sk_state == SMC_CLOSED)
3137 			answ = 0;
3138 		else
3139 			answ = smc_tx_prepared_sends(&smc->conn);
3140 		break;
3141 	case SIOCATMARK:
3142 		if (smc->sk.sk_state == SMC_LISTEN) {
3143 			release_sock(&smc->sk);
3144 			return -EINVAL;
3145 		}
3146 		if (smc->sk.sk_state == SMC_INIT ||
3147 		    smc->sk.sk_state == SMC_CLOSED) {
3148 			answ = 0;
3149 		} else {
3150 			smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
3151 			smc_curs_copy(&urg, &conn->urg_curs, conn);
3152 			answ = smc_curs_diff(conn->rmb_desc->len,
3153 					     &cons, &urg) == 1;
3154 		}
3155 		break;
3156 	default:
3157 		release_sock(&smc->sk);
3158 		return -ENOIOCTLCMD;
3159 	}
3160 	release_sock(&smc->sk);
3161 
3162 	return put_user(answ, (int __user *)arg);
3163 }
3164 
3165 /* Map the affected portions of the rmbe into an spd, note the number of bytes
3166  * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
3167  * updates till whenever a respective page has been fully processed.
3168  * Note that subsequent recv() calls have to wait till all splice() processing
3169  * completed.
3170  */
3171 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
3172 			       struct pipe_inode_info *pipe, size_t len,
3173 			       unsigned int flags)
3174 {
3175 	struct sock *sk = sock->sk;
3176 	struct smc_sock *smc;
3177 	int rc = -ENOTCONN;
3178 
3179 	smc = smc_sk(sk);
3180 	lock_sock(sk);
3181 	if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
3182 		/* socket was connected before, no more data to read */
3183 		rc = 0;
3184 		goto out;
3185 	}
3186 	if (sk->sk_state == SMC_INIT ||
3187 	    sk->sk_state == SMC_LISTEN ||
3188 	    sk->sk_state == SMC_CLOSED)
3189 		goto out;
3190 
3191 	if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
3192 		rc = 0;
3193 		goto out;
3194 	}
3195 
3196 	if (smc->use_fallback) {
3197 		rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
3198 						    pipe, len, flags);
3199 	} else {
3200 		if (*ppos) {
3201 			rc = -ESPIPE;
3202 			goto out;
3203 		}
3204 		if (flags & SPLICE_F_NONBLOCK)
3205 			flags = MSG_DONTWAIT;
3206 		else
3207 			flags = 0;
3208 		SMC_STAT_INC(smc, splice_cnt);
3209 		rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
3210 	}
3211 out:
3212 	release_sock(sk);
3213 
3214 	return rc;
3215 }
3216 
3217 /* must look like tcp */
3218 static const struct proto_ops smc_sock_ops = {
3219 	.family		= PF_SMC,
3220 	.owner		= THIS_MODULE,
3221 	.release	= smc_release,
3222 	.bind		= smc_bind,
3223 	.connect	= smc_connect,
3224 	.socketpair	= sock_no_socketpair,
3225 	.accept		= smc_accept,
3226 	.getname	= smc_getname,
3227 	.poll		= smc_poll,
3228 	.ioctl		= smc_ioctl,
3229 	.listen		= smc_listen,
3230 	.shutdown	= smc_shutdown,
3231 	.setsockopt	= smc_setsockopt,
3232 	.getsockopt	= smc_getsockopt,
3233 	.sendmsg	= smc_sendmsg,
3234 	.recvmsg	= smc_recvmsg,
3235 	.mmap		= sock_no_mmap,
3236 	.splice_read	= smc_splice_read,
3237 };
3238 
3239 static int __smc_create(struct net *net, struct socket *sock, int protocol,
3240 			int kern, struct socket *clcsock)
3241 {
3242 	int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
3243 	struct smc_sock *smc;
3244 	struct sock *sk;
3245 	int rc;
3246 
3247 	rc = -ESOCKTNOSUPPORT;
3248 	if (sock->type != SOCK_STREAM)
3249 		goto out;
3250 
3251 	rc = -EPROTONOSUPPORT;
3252 	if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
3253 		goto out;
3254 
3255 	rc = -ENOBUFS;
3256 	sock->ops = &smc_sock_ops;
3257 	sock->state = SS_UNCONNECTED;
3258 	sk = smc_sock_alloc(net, sock, protocol);
3259 	if (!sk)
3260 		goto out;
3261 
3262 	/* create internal TCP socket for CLC handshake and fallback */
3263 	smc = smc_sk(sk);
3264 	smc->use_fallback = false; /* assume rdma capability first */
3265 	smc->fallback_rsn = 0;
3266 
3267 	/* default behavior from limit_smc_hs in every net namespace */
3268 	smc->limit_smc_hs = net->smc.limit_smc_hs;
3269 
3270 	rc = 0;
3271 	if (!clcsock) {
3272 		rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
3273 				      &smc->clcsock);
3274 		if (rc) {
3275 			sk_common_release(sk);
3276 			goto out;
3277 		}
3278 
3279 		/* smc_clcsock_release() does not wait smc->clcsock->sk's
3280 		 * destruction;  its sk_state might not be TCP_CLOSE after
3281 		 * smc->sk is close()d, and TCP timers can be fired later,
3282 		 * which need net ref.
3283 		 */
3284 		sk = smc->clcsock->sk;
3285 		__netns_tracker_free(net, &sk->ns_tracker, false);
3286 		sk->sk_net_refcnt = 1;
3287 		get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
3288 		sock_inuse_add(net, 1);
3289 	} else {
3290 		smc->clcsock = clcsock;
3291 	}
3292 
3293 out:
3294 	return rc;
3295 }
3296 
3297 static int smc_create(struct net *net, struct socket *sock, int protocol,
3298 		      int kern)
3299 {
3300 	return __smc_create(net, sock, protocol, kern, NULL);
3301 }
3302 
3303 static const struct net_proto_family smc_sock_family_ops = {
3304 	.family	= PF_SMC,
3305 	.owner	= THIS_MODULE,
3306 	.create	= smc_create,
3307 };
3308 
3309 static int smc_ulp_init(struct sock *sk)
3310 {
3311 	struct socket *tcp = sk->sk_socket;
3312 	struct net *net = sock_net(sk);
3313 	struct socket *smcsock;
3314 	int protocol, ret;
3315 
3316 	/* only TCP can be replaced */
3317 	if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP ||
3318 	    (sk->sk_family != AF_INET && sk->sk_family != AF_INET6))
3319 		return -ESOCKTNOSUPPORT;
3320 	/* don't handle wq now */
3321 	if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list)
3322 		return -ENOTCONN;
3323 
3324 	if (sk->sk_family == AF_INET)
3325 		protocol = SMCPROTO_SMC;
3326 	else
3327 		protocol = SMCPROTO_SMC6;
3328 
3329 	smcsock = sock_alloc();
3330 	if (!smcsock)
3331 		return -ENFILE;
3332 
3333 	smcsock->type = SOCK_STREAM;
3334 	__module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */
3335 	ret = __smc_create(net, smcsock, protocol, 1, tcp);
3336 	if (ret) {
3337 		sock_release(smcsock); /* module_put() which ops won't be NULL */
3338 		return ret;
3339 	}
3340 
3341 	/* replace tcp socket to smc */
3342 	smcsock->file = tcp->file;
3343 	smcsock->file->private_data = smcsock;
3344 	smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */
3345 	smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */
3346 	tcp->file = NULL;
3347 
3348 	return ret;
3349 }
3350 
3351 static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk,
3352 			  const gfp_t priority)
3353 {
3354 	struct inet_connection_sock *icsk = inet_csk(newsk);
3355 
3356 	/* don't inherit ulp ops to child when listen */
3357 	icsk->icsk_ulp_ops = NULL;
3358 }
3359 
3360 static struct tcp_ulp_ops smc_ulp_ops __read_mostly = {
3361 	.name		= "smc",
3362 	.owner		= THIS_MODULE,
3363 	.init		= smc_ulp_init,
3364 	.clone		= smc_ulp_clone,
3365 };
3366 
3367 unsigned int smc_net_id;
3368 
3369 static __net_init int smc_net_init(struct net *net)
3370 {
3371 	int rc;
3372 
3373 	rc = smc_sysctl_net_init(net);
3374 	if (rc)
3375 		return rc;
3376 	return smc_pnet_net_init(net);
3377 }
3378 
3379 static void __net_exit smc_net_exit(struct net *net)
3380 {
3381 	smc_sysctl_net_exit(net);
3382 	smc_pnet_net_exit(net);
3383 }
3384 
3385 static __net_init int smc_net_stat_init(struct net *net)
3386 {
3387 	return smc_stats_init(net);
3388 }
3389 
3390 static void __net_exit smc_net_stat_exit(struct net *net)
3391 {
3392 	smc_stats_exit(net);
3393 }
3394 
3395 static struct pernet_operations smc_net_ops = {
3396 	.init = smc_net_init,
3397 	.exit = smc_net_exit,
3398 	.id   = &smc_net_id,
3399 	.size = sizeof(struct smc_net),
3400 };
3401 
3402 static struct pernet_operations smc_net_stat_ops = {
3403 	.init = smc_net_stat_init,
3404 	.exit = smc_net_stat_exit,
3405 };
3406 
3407 static int __init smc_init(void)
3408 {
3409 	int rc;
3410 
3411 	rc = register_pernet_subsys(&smc_net_ops);
3412 	if (rc)
3413 		return rc;
3414 
3415 	rc = register_pernet_subsys(&smc_net_stat_ops);
3416 	if (rc)
3417 		goto out_pernet_subsys;
3418 
3419 	rc = smc_ism_init();
3420 	if (rc)
3421 		goto out_pernet_subsys_stat;
3422 	smc_clc_init();
3423 
3424 	rc = smc_nl_init();
3425 	if (rc)
3426 		goto out_ism;
3427 
3428 	rc = smc_pnet_init();
3429 	if (rc)
3430 		goto out_nl;
3431 
3432 	rc = -ENOMEM;
3433 
3434 	smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", 0, 0);
3435 	if (!smc_tcp_ls_wq)
3436 		goto out_pnet;
3437 
3438 	smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
3439 	if (!smc_hs_wq)
3440 		goto out_alloc_tcp_ls_wq;
3441 
3442 	smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
3443 	if (!smc_close_wq)
3444 		goto out_alloc_hs_wq;
3445 
3446 	rc = smc_core_init();
3447 	if (rc) {
3448 		pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
3449 		goto out_alloc_wqs;
3450 	}
3451 
3452 	rc = smc_llc_init();
3453 	if (rc) {
3454 		pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
3455 		goto out_core;
3456 	}
3457 
3458 	rc = smc_cdc_init();
3459 	if (rc) {
3460 		pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
3461 		goto out_core;
3462 	}
3463 
3464 	rc = proto_register(&smc_proto, 1);
3465 	if (rc) {
3466 		pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
3467 		goto out_core;
3468 	}
3469 
3470 	rc = proto_register(&smc_proto6, 1);
3471 	if (rc) {
3472 		pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
3473 		goto out_proto;
3474 	}
3475 
3476 	rc = sock_register(&smc_sock_family_ops);
3477 	if (rc) {
3478 		pr_err("%s: sock_register fails with %d\n", __func__, rc);
3479 		goto out_proto6;
3480 	}
3481 	INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
3482 	INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
3483 
3484 	rc = smc_ib_register_client();
3485 	if (rc) {
3486 		pr_err("%s: ib_register fails with %d\n", __func__, rc);
3487 		goto out_sock;
3488 	}
3489 
3490 	rc = tcp_register_ulp(&smc_ulp_ops);
3491 	if (rc) {
3492 		pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc);
3493 		goto out_ib;
3494 	}
3495 
3496 	static_branch_enable(&tcp_have_smc);
3497 	return 0;
3498 
3499 out_ib:
3500 	smc_ib_unregister_client();
3501 out_sock:
3502 	sock_unregister(PF_SMC);
3503 out_proto6:
3504 	proto_unregister(&smc_proto6);
3505 out_proto:
3506 	proto_unregister(&smc_proto);
3507 out_core:
3508 	smc_core_exit();
3509 out_alloc_wqs:
3510 	destroy_workqueue(smc_close_wq);
3511 out_alloc_hs_wq:
3512 	destroy_workqueue(smc_hs_wq);
3513 out_alloc_tcp_ls_wq:
3514 	destroy_workqueue(smc_tcp_ls_wq);
3515 out_pnet:
3516 	smc_pnet_exit();
3517 out_nl:
3518 	smc_nl_exit();
3519 out_ism:
3520 	smc_clc_exit();
3521 	smc_ism_exit();
3522 out_pernet_subsys_stat:
3523 	unregister_pernet_subsys(&smc_net_stat_ops);
3524 out_pernet_subsys:
3525 	unregister_pernet_subsys(&smc_net_ops);
3526 
3527 	return rc;
3528 }
3529 
3530 static void __exit smc_exit(void)
3531 {
3532 	static_branch_disable(&tcp_have_smc);
3533 	tcp_unregister_ulp(&smc_ulp_ops);
3534 	sock_unregister(PF_SMC);
3535 	smc_core_exit();
3536 	smc_ib_unregister_client();
3537 	smc_ism_exit();
3538 	destroy_workqueue(smc_close_wq);
3539 	destroy_workqueue(smc_tcp_ls_wq);
3540 	destroy_workqueue(smc_hs_wq);
3541 	proto_unregister(&smc_proto6);
3542 	proto_unregister(&smc_proto);
3543 	smc_pnet_exit();
3544 	smc_nl_exit();
3545 	smc_clc_exit();
3546 	unregister_pernet_subsys(&smc_net_stat_ops);
3547 	unregister_pernet_subsys(&smc_net_ops);
3548 	rcu_barrier();
3549 }
3550 
3551 module_init(smc_init);
3552 module_exit(smc_exit);
3553 
3554 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
3555 MODULE_DESCRIPTION("smc socket address family");
3556 MODULE_LICENSE("GPL");
3557 MODULE_ALIAS_NETPROTO(PF_SMC);
3558 MODULE_ALIAS_TCP_ULP("smc");
3559 MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME);
3560