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