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