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