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