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