xref: /openbmc/linux/net/smc/af_smc.c (revision dc6a81c3)
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 
30 #include <net/sock.h>
31 #include <net/tcp.h>
32 #include <net/smc.h>
33 #include <asm/ioctls.h>
34 
35 #include <net/net_namespace.h>
36 #include <net/netns/generic.h>
37 #include "smc_netns.h"
38 
39 #include "smc.h"
40 #include "smc_clc.h"
41 #include "smc_llc.h"
42 #include "smc_cdc.h"
43 #include "smc_core.h"
44 #include "smc_ib.h"
45 #include "smc_ism.h"
46 #include "smc_pnet.h"
47 #include "smc_tx.h"
48 #include "smc_rx.h"
49 #include "smc_close.h"
50 
51 static DEFINE_MUTEX(smc_server_lgr_pending);	/* serialize link group
52 						 * creation on server
53 						 */
54 static DEFINE_MUTEX(smc_client_lgr_pending);	/* serialize link group
55 						 * creation on client
56 						 */
57 
58 static void smc_tcp_listen_work(struct work_struct *);
59 static void smc_connect_work(struct work_struct *);
60 
61 static void smc_set_keepalive(struct sock *sk, int val)
62 {
63 	struct smc_sock *smc = smc_sk(sk);
64 
65 	smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
66 }
67 
68 static struct smc_hashinfo smc_v4_hashinfo = {
69 	.lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
70 };
71 
72 static struct smc_hashinfo smc_v6_hashinfo = {
73 	.lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
74 };
75 
76 int smc_hash_sk(struct sock *sk)
77 {
78 	struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
79 	struct hlist_head *head;
80 
81 	head = &h->ht;
82 
83 	write_lock_bh(&h->lock);
84 	sk_add_node(sk, head);
85 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
86 	write_unlock_bh(&h->lock);
87 
88 	return 0;
89 }
90 EXPORT_SYMBOL_GPL(smc_hash_sk);
91 
92 void smc_unhash_sk(struct sock *sk)
93 {
94 	struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
95 
96 	write_lock_bh(&h->lock);
97 	if (sk_del_node_init(sk))
98 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
99 	write_unlock_bh(&h->lock);
100 }
101 EXPORT_SYMBOL_GPL(smc_unhash_sk);
102 
103 struct proto smc_proto = {
104 	.name		= "SMC",
105 	.owner		= THIS_MODULE,
106 	.keepalive	= smc_set_keepalive,
107 	.hash		= smc_hash_sk,
108 	.unhash		= smc_unhash_sk,
109 	.obj_size	= sizeof(struct smc_sock),
110 	.h.smc_hash	= &smc_v4_hashinfo,
111 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
112 };
113 EXPORT_SYMBOL_GPL(smc_proto);
114 
115 struct proto smc_proto6 = {
116 	.name		= "SMC6",
117 	.owner		= THIS_MODULE,
118 	.keepalive	= smc_set_keepalive,
119 	.hash		= smc_hash_sk,
120 	.unhash		= smc_unhash_sk,
121 	.obj_size	= sizeof(struct smc_sock),
122 	.h.smc_hash	= &smc_v6_hashinfo,
123 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
124 };
125 EXPORT_SYMBOL_GPL(smc_proto6);
126 
127 static void smc_restore_fallback_changes(struct smc_sock *smc)
128 {
129 	smc->clcsock->file->private_data = smc->sk.sk_socket;
130 	smc->clcsock->file = NULL;
131 }
132 
133 static int __smc_release(struct smc_sock *smc)
134 {
135 	struct sock *sk = &smc->sk;
136 	int rc = 0;
137 
138 	if (!smc->use_fallback) {
139 		rc = smc_close_active(smc);
140 		sock_set_flag(sk, SOCK_DEAD);
141 		sk->sk_shutdown |= SHUTDOWN_MASK;
142 	} else {
143 		if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
144 			sock_put(sk); /* passive closing */
145 		if (sk->sk_state == SMC_LISTEN) {
146 			/* wake up clcsock accept */
147 			rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
148 		}
149 		sk->sk_state = SMC_CLOSED;
150 		sk->sk_state_change(sk);
151 		smc_restore_fallback_changes(smc);
152 	}
153 
154 	sk->sk_prot->unhash(sk);
155 
156 	if (sk->sk_state == SMC_CLOSED) {
157 		if (smc->clcsock) {
158 			release_sock(sk);
159 			smc_clcsock_release(smc);
160 			lock_sock(sk);
161 		}
162 		if (!smc->use_fallback)
163 			smc_conn_free(&smc->conn);
164 	}
165 
166 	return rc;
167 }
168 
169 static int smc_release(struct socket *sock)
170 {
171 	struct sock *sk = sock->sk;
172 	struct smc_sock *smc;
173 	int rc = 0;
174 
175 	if (!sk)
176 		goto out;
177 
178 	sock_hold(sk); /* sock_put below */
179 	smc = smc_sk(sk);
180 
181 	/* cleanup for a dangling non-blocking connect */
182 	if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
183 		tcp_abort(smc->clcsock->sk, ECONNABORTED);
184 	flush_work(&smc->connect_work);
185 
186 	if (sk->sk_state == SMC_LISTEN)
187 		/* smc_close_non_accepted() is called and acquires
188 		 * sock lock for child sockets again
189 		 */
190 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
191 	else
192 		lock_sock(sk);
193 
194 	rc = __smc_release(smc);
195 
196 	/* detach socket */
197 	sock_orphan(sk);
198 	sock->sk = NULL;
199 	release_sock(sk);
200 
201 	sock_put(sk); /* sock_hold above */
202 	sock_put(sk); /* final sock_put */
203 out:
204 	return rc;
205 }
206 
207 static void smc_destruct(struct sock *sk)
208 {
209 	if (sk->sk_state != SMC_CLOSED)
210 		return;
211 	if (!sock_flag(sk, SOCK_DEAD))
212 		return;
213 
214 	sk_refcnt_debug_dec(sk);
215 }
216 
217 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
218 				   int protocol)
219 {
220 	struct smc_sock *smc;
221 	struct proto *prot;
222 	struct sock *sk;
223 
224 	prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
225 	sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
226 	if (!sk)
227 		return NULL;
228 
229 	sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
230 	sk->sk_state = SMC_INIT;
231 	sk->sk_destruct = smc_destruct;
232 	sk->sk_protocol = protocol;
233 	smc = smc_sk(sk);
234 	INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
235 	INIT_WORK(&smc->connect_work, smc_connect_work);
236 	INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
237 	INIT_LIST_HEAD(&smc->accept_q);
238 	spin_lock_init(&smc->accept_q_lock);
239 	spin_lock_init(&smc->conn.send_lock);
240 	sk->sk_prot->hash(sk);
241 	sk_refcnt_debug_inc(sk);
242 	mutex_init(&smc->clcsock_release_lock);
243 
244 	return sk;
245 }
246 
247 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
248 		    int addr_len)
249 {
250 	struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
251 	struct sock *sk = sock->sk;
252 	struct smc_sock *smc;
253 	int rc;
254 
255 	smc = smc_sk(sk);
256 
257 	/* replicate tests from inet_bind(), to be safe wrt. future changes */
258 	rc = -EINVAL;
259 	if (addr_len < sizeof(struct sockaddr_in))
260 		goto out;
261 
262 	rc = -EAFNOSUPPORT;
263 	if (addr->sin_family != AF_INET &&
264 	    addr->sin_family != AF_INET6 &&
265 	    addr->sin_family != AF_UNSPEC)
266 		goto out;
267 	/* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
268 	if (addr->sin_family == AF_UNSPEC &&
269 	    addr->sin_addr.s_addr != htonl(INADDR_ANY))
270 		goto out;
271 
272 	lock_sock(sk);
273 
274 	/* Check if socket is already active */
275 	rc = -EINVAL;
276 	if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
277 		goto out_rel;
278 
279 	smc->clcsock->sk->sk_reuse = sk->sk_reuse;
280 	rc = kernel_bind(smc->clcsock, uaddr, addr_len);
281 
282 out_rel:
283 	release_sock(sk);
284 out:
285 	return rc;
286 }
287 
288 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
289 				   unsigned long mask)
290 {
291 	/* options we don't get control via setsockopt for */
292 	nsk->sk_type = osk->sk_type;
293 	nsk->sk_sndbuf = osk->sk_sndbuf;
294 	nsk->sk_rcvbuf = osk->sk_rcvbuf;
295 	nsk->sk_sndtimeo = osk->sk_sndtimeo;
296 	nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
297 	nsk->sk_mark = osk->sk_mark;
298 	nsk->sk_priority = osk->sk_priority;
299 	nsk->sk_rcvlowat = osk->sk_rcvlowat;
300 	nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
301 	nsk->sk_err = osk->sk_err;
302 
303 	nsk->sk_flags &= ~mask;
304 	nsk->sk_flags |= osk->sk_flags & mask;
305 }
306 
307 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
308 			     (1UL << SOCK_KEEPOPEN) | \
309 			     (1UL << SOCK_LINGER) | \
310 			     (1UL << SOCK_BROADCAST) | \
311 			     (1UL << SOCK_TIMESTAMP) | \
312 			     (1UL << SOCK_DBG) | \
313 			     (1UL << SOCK_RCVTSTAMP) | \
314 			     (1UL << SOCK_RCVTSTAMPNS) | \
315 			     (1UL << SOCK_LOCALROUTE) | \
316 			     (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
317 			     (1UL << SOCK_RXQ_OVFL) | \
318 			     (1UL << SOCK_WIFI_STATUS) | \
319 			     (1UL << SOCK_NOFCS) | \
320 			     (1UL << SOCK_FILTER_LOCKED) | \
321 			     (1UL << SOCK_TSTAMP_NEW))
322 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
323  * clc socket (since smc is not called for these options from net/core)
324  */
325 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
326 {
327 	smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
328 }
329 
330 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
331 			     (1UL << SOCK_KEEPOPEN) | \
332 			     (1UL << SOCK_LINGER) | \
333 			     (1UL << SOCK_DBG))
334 /* copy only settings and flags relevant for smc from clc to smc socket */
335 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
336 {
337 	smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
338 }
339 
340 /* register a new rmb, send confirm_rkey msg to register with peer */
341 static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
342 		       bool conf_rkey)
343 {
344 	if (!rmb_desc->wr_reg) {
345 		/* register memory region for new rmb */
346 		if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
347 			rmb_desc->regerr = 1;
348 			return -EFAULT;
349 		}
350 		rmb_desc->wr_reg = 1;
351 	}
352 	if (!conf_rkey)
353 		return 0;
354 	/* exchange confirm_rkey msg with peer */
355 	if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
356 		rmb_desc->regerr = 1;
357 		return -EFAULT;
358 	}
359 	return 0;
360 }
361 
362 static int smc_clnt_conf_first_link(struct smc_sock *smc)
363 {
364 	struct net *net = sock_net(smc->clcsock->sk);
365 	struct smc_link_group *lgr = smc->conn.lgr;
366 	struct smc_link *link;
367 	int rest;
368 	int rc;
369 
370 	link = &lgr->lnk[SMC_SINGLE_LINK];
371 	/* receive CONFIRM LINK request from server over RoCE fabric */
372 	rest = wait_for_completion_interruptible_timeout(
373 		&link->llc_confirm,
374 		SMC_LLC_WAIT_FIRST_TIME);
375 	if (rest <= 0) {
376 		struct smc_clc_msg_decline dclc;
377 
378 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
379 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
380 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
381 	}
382 
383 	if (link->llc_confirm_rc)
384 		return SMC_CLC_DECL_RMBE_EC;
385 
386 	rc = smc_ib_modify_qp_rts(link);
387 	if (rc)
388 		return SMC_CLC_DECL_ERR_RDYLNK;
389 
390 	smc_wr_remember_qp_attr(link);
391 
392 	if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
393 		return SMC_CLC_DECL_ERR_REGRMB;
394 
395 	/* send CONFIRM LINK response over RoCE fabric */
396 	rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
397 	if (rc < 0)
398 		return SMC_CLC_DECL_TIMEOUT_CL;
399 
400 	/* receive ADD LINK request from server over RoCE fabric */
401 	rest = wait_for_completion_interruptible_timeout(&link->llc_add,
402 							 SMC_LLC_WAIT_TIME);
403 	if (rest <= 0) {
404 		struct smc_clc_msg_decline dclc;
405 
406 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
407 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
408 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
409 	}
410 
411 	/* send add link reject message, only one link supported for now */
412 	rc = smc_llc_send_add_link(link,
413 				   link->smcibdev->mac[link->ibport - 1],
414 				   link->gid, SMC_LLC_RESP);
415 	if (rc < 0)
416 		return SMC_CLC_DECL_TIMEOUT_AL;
417 
418 	smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
419 
420 	return 0;
421 }
422 
423 static void smcr_conn_save_peer_info(struct smc_sock *smc,
424 				     struct smc_clc_msg_accept_confirm *clc)
425 {
426 	int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
427 
428 	smc->conn.peer_rmbe_idx = clc->rmbe_idx;
429 	smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
430 	smc->conn.peer_rmbe_size = bufsize;
431 	atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
432 	smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
433 }
434 
435 static void smcd_conn_save_peer_info(struct smc_sock *smc,
436 				     struct smc_clc_msg_accept_confirm *clc)
437 {
438 	int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
439 
440 	smc->conn.peer_rmbe_idx = clc->dmbe_idx;
441 	smc->conn.peer_token = clc->token;
442 	/* msg header takes up space in the buffer */
443 	smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
444 	atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
445 	smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
446 }
447 
448 static void smc_conn_save_peer_info(struct smc_sock *smc,
449 				    struct smc_clc_msg_accept_confirm *clc)
450 {
451 	if (smc->conn.lgr->is_smcd)
452 		smcd_conn_save_peer_info(smc, clc);
453 	else
454 		smcr_conn_save_peer_info(smc, clc);
455 }
456 
457 static void smc_link_save_peer_info(struct smc_link *link,
458 				    struct smc_clc_msg_accept_confirm *clc)
459 {
460 	link->peer_qpn = ntoh24(clc->qpn);
461 	memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
462 	memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
463 	link->peer_psn = ntoh24(clc->psn);
464 	link->peer_mtu = clc->qp_mtu;
465 }
466 
467 static void smc_switch_to_fallback(struct smc_sock *smc)
468 {
469 	smc->use_fallback = true;
470 	if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
471 		smc->clcsock->file = smc->sk.sk_socket->file;
472 		smc->clcsock->file->private_data = smc->clcsock;
473 	}
474 }
475 
476 /* fall back during connect */
477 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
478 {
479 	smc_switch_to_fallback(smc);
480 	smc->fallback_rsn = reason_code;
481 	smc_copy_sock_settings_to_clc(smc);
482 	smc->connect_nonblock = 0;
483 	if (smc->sk.sk_state == SMC_INIT)
484 		smc->sk.sk_state = SMC_ACTIVE;
485 	return 0;
486 }
487 
488 /* decline and fall back during connect */
489 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
490 {
491 	int rc;
492 
493 	if (reason_code < 0) { /* error, fallback is not possible */
494 		if (smc->sk.sk_state == SMC_INIT)
495 			sock_put(&smc->sk); /* passive closing */
496 		return reason_code;
497 	}
498 	if (reason_code != SMC_CLC_DECL_PEERDECL) {
499 		rc = smc_clc_send_decline(smc, reason_code);
500 		if (rc < 0) {
501 			if (smc->sk.sk_state == SMC_INIT)
502 				sock_put(&smc->sk); /* passive closing */
503 			return rc;
504 		}
505 	}
506 	return smc_connect_fallback(smc, reason_code);
507 }
508 
509 /* abort connecting */
510 static int smc_connect_abort(struct smc_sock *smc, int reason_code,
511 			     int local_contact)
512 {
513 	if (local_contact == SMC_FIRST_CONTACT)
514 		smc_lgr_forget(smc->conn.lgr);
515 	if (smc->conn.lgr->is_smcd)
516 		/* there is only one lgr role for SMC-D; use server lock */
517 		mutex_unlock(&smc_server_lgr_pending);
518 	else
519 		mutex_unlock(&smc_client_lgr_pending);
520 
521 	smc_conn_free(&smc->conn);
522 	smc->connect_nonblock = 0;
523 	return reason_code;
524 }
525 
526 /* check if there is a rdma device available for this connection. */
527 /* called for connect and listen */
528 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
529 {
530 	/* PNET table look up: search active ib_device and port
531 	 * within same PNETID that also contains the ethernet device
532 	 * used for the internal TCP socket
533 	 */
534 	smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
535 	if (!ini->ib_dev)
536 		return SMC_CLC_DECL_NOSMCRDEV;
537 	return 0;
538 }
539 
540 /* check if there is an ISM device available for this connection. */
541 /* called for connect and listen */
542 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
543 {
544 	/* Find ISM device with same PNETID as connecting interface  */
545 	smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
546 	if (!ini->ism_dev)
547 		return SMC_CLC_DECL_NOSMCDDEV;
548 	return 0;
549 }
550 
551 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
552 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
553 				      struct smc_init_info *ini)
554 {
555 	if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
556 		return SMC_CLC_DECL_ISMVLANERR;
557 	return 0;
558 }
559 
560 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
561  * used, the VLAN ID will be registered again during the connection setup.
562  */
563 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
564 					struct smc_init_info *ini)
565 {
566 	if (!is_smcd)
567 		return 0;
568 	if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
569 		return SMC_CLC_DECL_CNFERR;
570 	return 0;
571 }
572 
573 /* CLC handshake during connect */
574 static int smc_connect_clc(struct smc_sock *smc, int smc_type,
575 			   struct smc_clc_msg_accept_confirm *aclc,
576 			   struct smc_init_info *ini)
577 {
578 	int rc = 0;
579 
580 	/* do inband token exchange */
581 	rc = smc_clc_send_proposal(smc, smc_type, ini);
582 	if (rc)
583 		return rc;
584 	/* receive SMC Accept CLC message */
585 	return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
586 				CLC_WAIT_TIME);
587 }
588 
589 /* setup for RDMA connection of client */
590 static int smc_connect_rdma(struct smc_sock *smc,
591 			    struct smc_clc_msg_accept_confirm *aclc,
592 			    struct smc_init_info *ini)
593 {
594 	struct smc_link *link;
595 	int reason_code = 0;
596 
597 	ini->is_smcd = false;
598 	ini->ib_lcl = &aclc->lcl;
599 	ini->ib_clcqpn = ntoh24(aclc->qpn);
600 	ini->srv_first_contact = aclc->hdr.flag;
601 
602 	mutex_lock(&smc_client_lgr_pending);
603 	reason_code = smc_conn_create(smc, ini);
604 	if (reason_code) {
605 		mutex_unlock(&smc_client_lgr_pending);
606 		return reason_code;
607 	}
608 	link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
609 
610 	smc_conn_save_peer_info(smc, aclc);
611 
612 	/* create send buffer and rmb */
613 	if (smc_buf_create(smc, false))
614 		return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
615 					 ini->cln_first_contact);
616 
617 	if (ini->cln_first_contact == SMC_FIRST_CONTACT)
618 		smc_link_save_peer_info(link, aclc);
619 
620 	if (smc_rmb_rtoken_handling(&smc->conn, aclc))
621 		return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
622 					 ini->cln_first_contact);
623 
624 	smc_close_init(smc);
625 	smc_rx_init(smc);
626 
627 	if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
628 		if (smc_ib_ready_link(link))
629 			return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
630 						 ini->cln_first_contact);
631 	} else {
632 		if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
633 			return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
634 						 ini->cln_first_contact);
635 	}
636 	smc_rmb_sync_sg_for_device(&smc->conn);
637 
638 	reason_code = smc_clc_send_confirm(smc);
639 	if (reason_code)
640 		return smc_connect_abort(smc, reason_code,
641 					 ini->cln_first_contact);
642 
643 	smc_tx_init(smc);
644 
645 	if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
646 		/* QP confirmation over RoCE fabric */
647 		reason_code = smc_clnt_conf_first_link(smc);
648 		if (reason_code)
649 			return smc_connect_abort(smc, reason_code,
650 						 ini->cln_first_contact);
651 	}
652 	mutex_unlock(&smc_client_lgr_pending);
653 
654 	smc_copy_sock_settings_to_clc(smc);
655 	smc->connect_nonblock = 0;
656 	if (smc->sk.sk_state == SMC_INIT)
657 		smc->sk.sk_state = SMC_ACTIVE;
658 
659 	return 0;
660 }
661 
662 /* setup for ISM connection of client */
663 static int smc_connect_ism(struct smc_sock *smc,
664 			   struct smc_clc_msg_accept_confirm *aclc,
665 			   struct smc_init_info *ini)
666 {
667 	int rc = 0;
668 
669 	ini->is_smcd = true;
670 	ini->ism_gid = aclc->gid;
671 	ini->srv_first_contact = aclc->hdr.flag;
672 
673 	/* there is only one lgr role for SMC-D; use server lock */
674 	mutex_lock(&smc_server_lgr_pending);
675 	rc = smc_conn_create(smc, ini);
676 	if (rc) {
677 		mutex_unlock(&smc_server_lgr_pending);
678 		return rc;
679 	}
680 
681 	/* Create send and receive buffers */
682 	if (smc_buf_create(smc, true))
683 		return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
684 					 ini->cln_first_contact);
685 
686 	smc_conn_save_peer_info(smc, aclc);
687 	smc_close_init(smc);
688 	smc_rx_init(smc);
689 	smc_tx_init(smc);
690 
691 	rc = smc_clc_send_confirm(smc);
692 	if (rc)
693 		return smc_connect_abort(smc, rc, ini->cln_first_contact);
694 	mutex_unlock(&smc_server_lgr_pending);
695 
696 	smc_copy_sock_settings_to_clc(smc);
697 	smc->connect_nonblock = 0;
698 	if (smc->sk.sk_state == SMC_INIT)
699 		smc->sk.sk_state = SMC_ACTIVE;
700 
701 	return 0;
702 }
703 
704 /* perform steps before actually connecting */
705 static int __smc_connect(struct smc_sock *smc)
706 {
707 	bool ism_supported = false, rdma_supported = false;
708 	struct smc_clc_msg_accept_confirm aclc;
709 	struct smc_init_info ini = {0};
710 	int smc_type;
711 	int rc = 0;
712 
713 	if (smc->use_fallback)
714 		return smc_connect_fallback(smc, smc->fallback_rsn);
715 
716 	/* if peer has not signalled SMC-capability, fall back */
717 	if (!tcp_sk(smc->clcsock->sk)->syn_smc)
718 		return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
719 
720 	/* IPSec connections opt out of SMC-R optimizations */
721 	if (using_ipsec(smc))
722 		return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
723 
724 	/* get vlan id from IP device */
725 	if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
726 		return smc_connect_decline_fallback(smc,
727 						    SMC_CLC_DECL_GETVLANERR);
728 
729 	/* check if there is an ism device available */
730 	if (!smc_find_ism_device(smc, &ini) &&
731 	    !smc_connect_ism_vlan_setup(smc, &ini)) {
732 		/* ISM is supported for this connection */
733 		ism_supported = true;
734 		smc_type = SMC_TYPE_D;
735 	}
736 
737 	/* check if there is a rdma device available */
738 	if (!smc_find_rdma_device(smc, &ini)) {
739 		/* RDMA is supported for this connection */
740 		rdma_supported = true;
741 		if (ism_supported)
742 			smc_type = SMC_TYPE_B; /* both */
743 		else
744 			smc_type = SMC_TYPE_R; /* only RDMA */
745 	}
746 
747 	/* if neither ISM nor RDMA are supported, fallback */
748 	if (!rdma_supported && !ism_supported)
749 		return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
750 
751 	/* perform CLC handshake */
752 	rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
753 	if (rc) {
754 		smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
755 		return smc_connect_decline_fallback(smc, rc);
756 	}
757 
758 	/* depending on previous steps, connect using rdma or ism */
759 	if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
760 		rc = smc_connect_rdma(smc, &aclc, &ini);
761 	else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
762 		rc = smc_connect_ism(smc, &aclc, &ini);
763 	else
764 		rc = SMC_CLC_DECL_MODEUNSUPP;
765 	if (rc) {
766 		smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
767 		return smc_connect_decline_fallback(smc, rc);
768 	}
769 
770 	smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
771 	return 0;
772 }
773 
774 static void smc_connect_work(struct work_struct *work)
775 {
776 	struct smc_sock *smc = container_of(work, struct smc_sock,
777 					    connect_work);
778 	long timeo = smc->sk.sk_sndtimeo;
779 	int rc = 0;
780 
781 	if (!timeo)
782 		timeo = MAX_SCHEDULE_TIMEOUT;
783 	lock_sock(smc->clcsock->sk);
784 	if (smc->clcsock->sk->sk_err) {
785 		smc->sk.sk_err = smc->clcsock->sk->sk_err;
786 	} else if ((1 << smc->clcsock->sk->sk_state) &
787 					(TCPF_SYN_SENT | TCP_SYN_RECV)) {
788 		rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
789 		if ((rc == -EPIPE) &&
790 		    ((1 << smc->clcsock->sk->sk_state) &
791 					(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
792 			rc = 0;
793 	}
794 	release_sock(smc->clcsock->sk);
795 	lock_sock(&smc->sk);
796 	if (rc != 0 || smc->sk.sk_err) {
797 		smc->sk.sk_state = SMC_CLOSED;
798 		if (rc == -EPIPE || rc == -EAGAIN)
799 			smc->sk.sk_err = EPIPE;
800 		else if (signal_pending(current))
801 			smc->sk.sk_err = -sock_intr_errno(timeo);
802 		sock_put(&smc->sk); /* passive closing */
803 		goto out;
804 	}
805 
806 	rc = __smc_connect(smc);
807 	if (rc < 0)
808 		smc->sk.sk_err = -rc;
809 
810 out:
811 	if (!sock_flag(&smc->sk, SOCK_DEAD)) {
812 		if (smc->sk.sk_err) {
813 			smc->sk.sk_state_change(&smc->sk);
814 		} else { /* allow polling before and after fallback decision */
815 			smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
816 			smc->sk.sk_write_space(&smc->sk);
817 		}
818 	}
819 	release_sock(&smc->sk);
820 }
821 
822 static int smc_connect(struct socket *sock, struct sockaddr *addr,
823 		       int alen, int flags)
824 {
825 	struct sock *sk = sock->sk;
826 	struct smc_sock *smc;
827 	int rc = -EINVAL;
828 
829 	smc = smc_sk(sk);
830 
831 	/* separate smc parameter checking to be safe */
832 	if (alen < sizeof(addr->sa_family))
833 		goto out_err;
834 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
835 		goto out_err;
836 
837 	lock_sock(sk);
838 	switch (sk->sk_state) {
839 	default:
840 		goto out;
841 	case SMC_ACTIVE:
842 		rc = -EISCONN;
843 		goto out;
844 	case SMC_INIT:
845 		rc = 0;
846 		break;
847 	}
848 
849 	smc_copy_sock_settings_to_clc(smc);
850 	tcp_sk(smc->clcsock->sk)->syn_smc = 1;
851 	if (smc->connect_nonblock) {
852 		rc = -EALREADY;
853 		goto out;
854 	}
855 	rc = kernel_connect(smc->clcsock, addr, alen, flags);
856 	if (rc && rc != -EINPROGRESS)
857 		goto out;
858 
859 	sock_hold(&smc->sk); /* sock put in passive closing */
860 	if (smc->use_fallback)
861 		goto out;
862 	if (flags & O_NONBLOCK) {
863 		if (schedule_work(&smc->connect_work))
864 			smc->connect_nonblock = 1;
865 		rc = -EINPROGRESS;
866 	} else {
867 		rc = __smc_connect(smc);
868 		if (rc < 0)
869 			goto out;
870 		else
871 			rc = 0; /* success cases including fallback */
872 	}
873 
874 out:
875 	release_sock(sk);
876 out_err:
877 	return rc;
878 }
879 
880 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
881 {
882 	struct socket *new_clcsock = NULL;
883 	struct sock *lsk = &lsmc->sk;
884 	struct sock *new_sk;
885 	int rc = -EINVAL;
886 
887 	release_sock(lsk);
888 	new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
889 	if (!new_sk) {
890 		rc = -ENOMEM;
891 		lsk->sk_err = ENOMEM;
892 		*new_smc = NULL;
893 		lock_sock(lsk);
894 		goto out;
895 	}
896 	*new_smc = smc_sk(new_sk);
897 
898 	mutex_lock(&lsmc->clcsock_release_lock);
899 	if (lsmc->clcsock)
900 		rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
901 	mutex_unlock(&lsmc->clcsock_release_lock);
902 	lock_sock(lsk);
903 	if  (rc < 0)
904 		lsk->sk_err = -rc;
905 	if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
906 		new_sk->sk_prot->unhash(new_sk);
907 		if (new_clcsock)
908 			sock_release(new_clcsock);
909 		new_sk->sk_state = SMC_CLOSED;
910 		sock_set_flag(new_sk, SOCK_DEAD);
911 		sock_put(new_sk); /* final */
912 		*new_smc = NULL;
913 		goto out;
914 	}
915 
916 	(*new_smc)->clcsock = new_clcsock;
917 out:
918 	return rc;
919 }
920 
921 /* add a just created sock to the accept queue of the listen sock as
922  * candidate for a following socket accept call from user space
923  */
924 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
925 {
926 	struct smc_sock *par = smc_sk(parent);
927 
928 	sock_hold(sk); /* sock_put in smc_accept_unlink () */
929 	spin_lock(&par->accept_q_lock);
930 	list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
931 	spin_unlock(&par->accept_q_lock);
932 	sk_acceptq_added(parent);
933 }
934 
935 /* remove a socket from the accept queue of its parental listening socket */
936 static void smc_accept_unlink(struct sock *sk)
937 {
938 	struct smc_sock *par = smc_sk(sk)->listen_smc;
939 
940 	spin_lock(&par->accept_q_lock);
941 	list_del_init(&smc_sk(sk)->accept_q);
942 	spin_unlock(&par->accept_q_lock);
943 	sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
944 	sock_put(sk); /* sock_hold in smc_accept_enqueue */
945 }
946 
947 /* remove a sock from the accept queue to bind it to a new socket created
948  * for a socket accept call from user space
949  */
950 struct sock *smc_accept_dequeue(struct sock *parent,
951 				struct socket *new_sock)
952 {
953 	struct smc_sock *isk, *n;
954 	struct sock *new_sk;
955 
956 	list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
957 		new_sk = (struct sock *)isk;
958 
959 		smc_accept_unlink(new_sk);
960 		if (new_sk->sk_state == SMC_CLOSED) {
961 			new_sk->sk_prot->unhash(new_sk);
962 			if (isk->clcsock) {
963 				sock_release(isk->clcsock);
964 				isk->clcsock = NULL;
965 			}
966 			sock_put(new_sk); /* final */
967 			continue;
968 		}
969 		if (new_sock) {
970 			sock_graft(new_sk, new_sock);
971 			if (isk->use_fallback) {
972 				smc_sk(new_sk)->clcsock->file = new_sock->file;
973 				isk->clcsock->file->private_data = isk->clcsock;
974 			}
975 		}
976 		return new_sk;
977 	}
978 	return NULL;
979 }
980 
981 /* clean up for a created but never accepted sock */
982 void smc_close_non_accepted(struct sock *sk)
983 {
984 	struct smc_sock *smc = smc_sk(sk);
985 
986 	sock_hold(sk); /* sock_put below */
987 	lock_sock(sk);
988 	if (!sk->sk_lingertime)
989 		/* wait for peer closing */
990 		sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
991 	__smc_release(smc);
992 	release_sock(sk);
993 	sock_put(sk); /* sock_hold above */
994 	sock_put(sk); /* final sock_put */
995 }
996 
997 static int smc_serv_conf_first_link(struct smc_sock *smc)
998 {
999 	struct net *net = sock_net(smc->clcsock->sk);
1000 	struct smc_link_group *lgr = smc->conn.lgr;
1001 	struct smc_link *link;
1002 	int rest;
1003 	int rc;
1004 
1005 	link = &lgr->lnk[SMC_SINGLE_LINK];
1006 
1007 	if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
1008 		return SMC_CLC_DECL_ERR_REGRMB;
1009 
1010 	/* send CONFIRM LINK request to client over the RoCE fabric */
1011 	rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1012 	if (rc < 0)
1013 		return SMC_CLC_DECL_TIMEOUT_CL;
1014 
1015 	/* receive CONFIRM LINK response from client over the RoCE fabric */
1016 	rest = wait_for_completion_interruptible_timeout(
1017 		&link->llc_confirm_resp,
1018 		SMC_LLC_WAIT_FIRST_TIME);
1019 	if (rest <= 0) {
1020 		struct smc_clc_msg_decline dclc;
1021 
1022 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1023 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1024 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1025 	}
1026 
1027 	if (link->llc_confirm_resp_rc)
1028 		return SMC_CLC_DECL_RMBE_EC;
1029 
1030 	/* send ADD LINK request to client over the RoCE fabric */
1031 	rc = smc_llc_send_add_link(link,
1032 				   link->smcibdev->mac[link->ibport - 1],
1033 				   link->gid, SMC_LLC_REQ);
1034 	if (rc < 0)
1035 		return SMC_CLC_DECL_TIMEOUT_AL;
1036 
1037 	/* receive ADD LINK response from client over the RoCE fabric */
1038 	rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1039 							 SMC_LLC_WAIT_TIME);
1040 	if (rest <= 0) {
1041 		struct smc_clc_msg_decline dclc;
1042 
1043 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1044 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1045 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
1046 	}
1047 
1048 	smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
1049 
1050 	return 0;
1051 }
1052 
1053 /* listen worker: finish */
1054 static void smc_listen_out(struct smc_sock *new_smc)
1055 {
1056 	struct smc_sock *lsmc = new_smc->listen_smc;
1057 	struct sock *newsmcsk = &new_smc->sk;
1058 
1059 	if (lsmc->sk.sk_state == SMC_LISTEN) {
1060 		lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1061 		smc_accept_enqueue(&lsmc->sk, newsmcsk);
1062 		release_sock(&lsmc->sk);
1063 	} else { /* no longer listening */
1064 		smc_close_non_accepted(newsmcsk);
1065 	}
1066 
1067 	/* Wake up accept */
1068 	lsmc->sk.sk_data_ready(&lsmc->sk);
1069 	sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1070 }
1071 
1072 /* listen worker: finish in state connected */
1073 static void smc_listen_out_connected(struct smc_sock *new_smc)
1074 {
1075 	struct sock *newsmcsk = &new_smc->sk;
1076 
1077 	sk_refcnt_debug_inc(newsmcsk);
1078 	if (newsmcsk->sk_state == SMC_INIT)
1079 		newsmcsk->sk_state = SMC_ACTIVE;
1080 
1081 	smc_listen_out(new_smc);
1082 }
1083 
1084 /* listen worker: finish in error state */
1085 static void smc_listen_out_err(struct smc_sock *new_smc)
1086 {
1087 	struct sock *newsmcsk = &new_smc->sk;
1088 
1089 	if (newsmcsk->sk_state == SMC_INIT)
1090 		sock_put(&new_smc->sk); /* passive closing */
1091 	newsmcsk->sk_state = SMC_CLOSED;
1092 	smc_conn_free(&new_smc->conn);
1093 
1094 	smc_listen_out(new_smc);
1095 }
1096 
1097 /* listen worker: decline and fall back if possible */
1098 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1099 			       int local_contact)
1100 {
1101 	/* RDMA setup failed, switch back to TCP */
1102 	if (local_contact == SMC_FIRST_CONTACT)
1103 		smc_lgr_forget(new_smc->conn.lgr);
1104 	if (reason_code < 0) { /* error, no fallback possible */
1105 		smc_listen_out_err(new_smc);
1106 		return;
1107 	}
1108 	smc_conn_free(&new_smc->conn);
1109 	smc_switch_to_fallback(new_smc);
1110 	new_smc->fallback_rsn = reason_code;
1111 	if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1112 		if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1113 			smc_listen_out_err(new_smc);
1114 			return;
1115 		}
1116 	}
1117 	smc_listen_out_connected(new_smc);
1118 }
1119 
1120 /* listen worker: check prefixes */
1121 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1122 				 struct smc_clc_msg_proposal *pclc)
1123 {
1124 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
1125 	struct socket *newclcsock = new_smc->clcsock;
1126 
1127 	pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1128 	if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1129 		return SMC_CLC_DECL_DIFFPREFIX;
1130 
1131 	return 0;
1132 }
1133 
1134 /* listen worker: initialize connection and buffers */
1135 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1136 				struct smc_init_info *ini)
1137 {
1138 	int rc;
1139 
1140 	/* allocate connection / link group */
1141 	rc = smc_conn_create(new_smc, ini);
1142 	if (rc)
1143 		return rc;
1144 
1145 	/* create send buffer and rmb */
1146 	if (smc_buf_create(new_smc, false))
1147 		return SMC_CLC_DECL_MEM;
1148 
1149 	return 0;
1150 }
1151 
1152 /* listen worker: initialize connection and buffers for SMC-D */
1153 static int smc_listen_ism_init(struct smc_sock *new_smc,
1154 			       struct smc_clc_msg_proposal *pclc,
1155 			       struct smc_init_info *ini)
1156 {
1157 	struct smc_clc_msg_smcd *pclc_smcd;
1158 	int rc;
1159 
1160 	pclc_smcd = smc_get_clc_msg_smcd(pclc);
1161 	ini->ism_gid = pclc_smcd->gid;
1162 	rc = smc_conn_create(new_smc, ini);
1163 	if (rc)
1164 		return rc;
1165 
1166 	/* Check if peer can be reached via ISM device */
1167 	if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1168 			    new_smc->conn.lgr->vlan_id,
1169 			    new_smc->conn.lgr->smcd)) {
1170 		if (ini->cln_first_contact == SMC_FIRST_CONTACT)
1171 			smc_lgr_forget(new_smc->conn.lgr);
1172 		smc_conn_free(&new_smc->conn);
1173 		return SMC_CLC_DECL_SMCDNOTALK;
1174 	}
1175 
1176 	/* Create send and receive buffers */
1177 	if (smc_buf_create(new_smc, true)) {
1178 		if (ini->cln_first_contact == SMC_FIRST_CONTACT)
1179 			smc_lgr_forget(new_smc->conn.lgr);
1180 		smc_conn_free(&new_smc->conn);
1181 		return SMC_CLC_DECL_MEM;
1182 	}
1183 
1184 	return 0;
1185 }
1186 
1187 /* listen worker: register buffers */
1188 static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1189 {
1190 	struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1191 
1192 	if (local_contact != SMC_FIRST_CONTACT) {
1193 		if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1194 			return SMC_CLC_DECL_ERR_REGRMB;
1195 	}
1196 	smc_rmb_sync_sg_for_device(&new_smc->conn);
1197 
1198 	return 0;
1199 }
1200 
1201 /* listen worker: finish RDMA setup */
1202 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1203 				  struct smc_clc_msg_accept_confirm *cclc,
1204 				  int local_contact)
1205 {
1206 	struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1207 	int reason_code = 0;
1208 
1209 	if (local_contact == SMC_FIRST_CONTACT)
1210 		smc_link_save_peer_info(link, cclc);
1211 
1212 	if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
1213 		reason_code = SMC_CLC_DECL_ERR_RTOK;
1214 		goto decline;
1215 	}
1216 
1217 	if (local_contact == SMC_FIRST_CONTACT) {
1218 		if (smc_ib_ready_link(link)) {
1219 			reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1220 			goto decline;
1221 		}
1222 		/* QP confirmation over RoCE fabric */
1223 		reason_code = smc_serv_conf_first_link(new_smc);
1224 		if (reason_code)
1225 			goto decline;
1226 	}
1227 	return 0;
1228 
1229 decline:
1230 	smc_listen_decline(new_smc, reason_code, local_contact);
1231 	return reason_code;
1232 }
1233 
1234 /* setup for RDMA connection of server */
1235 static void smc_listen_work(struct work_struct *work)
1236 {
1237 	struct smc_sock *new_smc = container_of(work, struct smc_sock,
1238 						smc_listen_work);
1239 	struct socket *newclcsock = new_smc->clcsock;
1240 	struct smc_clc_msg_accept_confirm cclc;
1241 	struct smc_clc_msg_proposal *pclc;
1242 	struct smc_init_info ini = {0};
1243 	bool ism_supported = false;
1244 	u8 buf[SMC_CLC_MAX_LEN];
1245 	int rc = 0;
1246 
1247 	if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1248 		return smc_listen_out_err(new_smc);
1249 
1250 	if (new_smc->use_fallback) {
1251 		smc_listen_out_connected(new_smc);
1252 		return;
1253 	}
1254 
1255 	/* check if peer is smc capable */
1256 	if (!tcp_sk(newclcsock->sk)->syn_smc) {
1257 		smc_switch_to_fallback(new_smc);
1258 		new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
1259 		smc_listen_out_connected(new_smc);
1260 		return;
1261 	}
1262 
1263 	/* do inband token exchange -
1264 	 * wait for and receive SMC Proposal CLC message
1265 	 */
1266 	pclc = (struct smc_clc_msg_proposal *)&buf;
1267 	rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1268 			      SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1269 	if (rc)
1270 		goto out_decl;
1271 
1272 	/* IPSec connections opt out of SMC-R optimizations */
1273 	if (using_ipsec(new_smc)) {
1274 		rc = SMC_CLC_DECL_IPSEC;
1275 		goto out_decl;
1276 	}
1277 
1278 	/* check for matching IP prefix and subnet length */
1279 	rc = smc_listen_prfx_check(new_smc, pclc);
1280 	if (rc)
1281 		goto out_decl;
1282 
1283 	/* get vlan id from IP device */
1284 	if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
1285 		rc = SMC_CLC_DECL_GETVLANERR;
1286 		goto out_decl;
1287 	}
1288 
1289 	mutex_lock(&smc_server_lgr_pending);
1290 	smc_close_init(new_smc);
1291 	smc_rx_init(new_smc);
1292 	smc_tx_init(new_smc);
1293 
1294 	/* check if ISM is available */
1295 	if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1296 		ini.is_smcd = true; /* prepare ISM check */
1297 		rc = smc_find_ism_device(new_smc, &ini);
1298 		if (!rc)
1299 			rc = smc_listen_ism_init(new_smc, pclc, &ini);
1300 		if (!rc)
1301 			ism_supported = true;
1302 		else if (pclc->hdr.path == SMC_TYPE_D)
1303 			goto out_unlock; /* skip RDMA and decline */
1304 	}
1305 
1306 	/* check if RDMA is available */
1307 	if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
1308 		/* prepare RDMA check */
1309 		ini.is_smcd = false;
1310 		ini.ism_dev = NULL;
1311 		ini.ib_lcl = &pclc->lcl;
1312 		rc = smc_find_rdma_device(new_smc, &ini);
1313 		if (rc) {
1314 			/* no RDMA device found */
1315 			if (pclc->hdr.path == SMC_TYPE_B)
1316 				/* neither ISM nor RDMA device found */
1317 				rc = SMC_CLC_DECL_NOSMCDEV;
1318 			goto out_unlock;
1319 		}
1320 		rc = smc_listen_rdma_init(new_smc, &ini);
1321 		if (rc)
1322 			goto out_unlock;
1323 		rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
1324 		if (rc)
1325 			goto out_unlock;
1326 	}
1327 
1328 	/* send SMC Accept CLC message */
1329 	rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
1330 	if (rc)
1331 		goto out_unlock;
1332 
1333 	/* SMC-D does not need this lock any more */
1334 	if (ism_supported)
1335 		mutex_unlock(&smc_server_lgr_pending);
1336 
1337 	/* receive SMC Confirm CLC message */
1338 	rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1339 			      SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1340 	if (rc) {
1341 		if (!ism_supported)
1342 			goto out_unlock;
1343 		goto out_decl;
1344 	}
1345 
1346 	/* finish worker */
1347 	if (!ism_supported) {
1348 		rc = smc_listen_rdma_finish(new_smc, &cclc,
1349 					    ini.cln_first_contact);
1350 		mutex_unlock(&smc_server_lgr_pending);
1351 		if (rc)
1352 			return;
1353 	}
1354 	smc_conn_save_peer_info(new_smc, &cclc);
1355 	smc_listen_out_connected(new_smc);
1356 	return;
1357 
1358 out_unlock:
1359 	mutex_unlock(&smc_server_lgr_pending);
1360 out_decl:
1361 	smc_listen_decline(new_smc, rc, ini.cln_first_contact);
1362 }
1363 
1364 static void smc_tcp_listen_work(struct work_struct *work)
1365 {
1366 	struct smc_sock *lsmc = container_of(work, struct smc_sock,
1367 					     tcp_listen_work);
1368 	struct sock *lsk = &lsmc->sk;
1369 	struct smc_sock *new_smc;
1370 	int rc = 0;
1371 
1372 	lock_sock(lsk);
1373 	while (lsk->sk_state == SMC_LISTEN) {
1374 		rc = smc_clcsock_accept(lsmc, &new_smc);
1375 		if (rc)
1376 			goto out;
1377 		if (!new_smc)
1378 			continue;
1379 
1380 		new_smc->listen_smc = lsmc;
1381 		new_smc->use_fallback = lsmc->use_fallback;
1382 		new_smc->fallback_rsn = lsmc->fallback_rsn;
1383 		sock_hold(lsk); /* sock_put in smc_listen_work */
1384 		INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1385 		smc_copy_sock_settings_to_smc(new_smc);
1386 		new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1387 		new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
1388 		sock_hold(&new_smc->sk); /* sock_put in passive closing */
1389 		if (!schedule_work(&new_smc->smc_listen_work))
1390 			sock_put(&new_smc->sk);
1391 	}
1392 
1393 out:
1394 	release_sock(lsk);
1395 	sock_put(&lsmc->sk); /* sock_hold in smc_listen */
1396 }
1397 
1398 static int smc_listen(struct socket *sock, int backlog)
1399 {
1400 	struct sock *sk = sock->sk;
1401 	struct smc_sock *smc;
1402 	int rc;
1403 
1404 	smc = smc_sk(sk);
1405 	lock_sock(sk);
1406 
1407 	rc = -EINVAL;
1408 	if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1409 	    smc->connect_nonblock)
1410 		goto out;
1411 
1412 	rc = 0;
1413 	if (sk->sk_state == SMC_LISTEN) {
1414 		sk->sk_max_ack_backlog = backlog;
1415 		goto out;
1416 	}
1417 	/* some socket options are handled in core, so we could not apply
1418 	 * them to the clc socket -- copy smc socket options to clc socket
1419 	 */
1420 	smc_copy_sock_settings_to_clc(smc);
1421 	if (!smc->use_fallback)
1422 		tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1423 
1424 	rc = kernel_listen(smc->clcsock, backlog);
1425 	if (rc)
1426 		goto out;
1427 	sk->sk_max_ack_backlog = backlog;
1428 	sk->sk_ack_backlog = 0;
1429 	sk->sk_state = SMC_LISTEN;
1430 	sock_hold(sk); /* sock_hold in tcp_listen_worker */
1431 	if (!schedule_work(&smc->tcp_listen_work))
1432 		sock_put(sk);
1433 
1434 out:
1435 	release_sock(sk);
1436 	return rc;
1437 }
1438 
1439 static int smc_accept(struct socket *sock, struct socket *new_sock,
1440 		      int flags, bool kern)
1441 {
1442 	struct sock *sk = sock->sk, *nsk;
1443 	DECLARE_WAITQUEUE(wait, current);
1444 	struct smc_sock *lsmc;
1445 	long timeo;
1446 	int rc = 0;
1447 
1448 	lsmc = smc_sk(sk);
1449 	sock_hold(sk); /* sock_put below */
1450 	lock_sock(sk);
1451 
1452 	if (lsmc->sk.sk_state != SMC_LISTEN) {
1453 		rc = -EINVAL;
1454 		release_sock(sk);
1455 		goto out;
1456 	}
1457 
1458 	/* Wait for an incoming connection */
1459 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1460 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
1461 	while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1462 		set_current_state(TASK_INTERRUPTIBLE);
1463 		if (!timeo) {
1464 			rc = -EAGAIN;
1465 			break;
1466 		}
1467 		release_sock(sk);
1468 		timeo = schedule_timeout(timeo);
1469 		/* wakeup by sk_data_ready in smc_listen_work() */
1470 		sched_annotate_sleep();
1471 		lock_sock(sk);
1472 		if (signal_pending(current)) {
1473 			rc = sock_intr_errno(timeo);
1474 			break;
1475 		}
1476 	}
1477 	set_current_state(TASK_RUNNING);
1478 	remove_wait_queue(sk_sleep(sk), &wait);
1479 
1480 	if (!rc)
1481 		rc = sock_error(nsk);
1482 	release_sock(sk);
1483 	if (rc)
1484 		goto out;
1485 
1486 	if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1487 		/* wait till data arrives on the socket */
1488 		timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1489 								MSEC_PER_SEC);
1490 		if (smc_sk(nsk)->use_fallback) {
1491 			struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1492 
1493 			lock_sock(clcsk);
1494 			if (skb_queue_empty(&clcsk->sk_receive_queue))
1495 				sk_wait_data(clcsk, &timeo, NULL);
1496 			release_sock(clcsk);
1497 		} else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1498 			lock_sock(nsk);
1499 			smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
1500 			release_sock(nsk);
1501 		}
1502 	}
1503 
1504 out:
1505 	sock_put(sk); /* sock_hold above */
1506 	return rc;
1507 }
1508 
1509 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1510 		       int peer)
1511 {
1512 	struct smc_sock *smc;
1513 
1514 	if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1515 	    (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1516 		return -ENOTCONN;
1517 
1518 	smc = smc_sk(sock->sk);
1519 
1520 	return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
1521 }
1522 
1523 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1524 {
1525 	struct sock *sk = sock->sk;
1526 	struct smc_sock *smc;
1527 	int rc = -EPIPE;
1528 
1529 	smc = smc_sk(sk);
1530 	lock_sock(sk);
1531 	if ((sk->sk_state != SMC_ACTIVE) &&
1532 	    (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1533 	    (sk->sk_state != SMC_INIT))
1534 		goto out;
1535 
1536 	if (msg->msg_flags & MSG_FASTOPEN) {
1537 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1538 			smc_switch_to_fallback(smc);
1539 			smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1540 		} else {
1541 			rc = -EINVAL;
1542 			goto out;
1543 		}
1544 	}
1545 
1546 	if (smc->use_fallback)
1547 		rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1548 	else
1549 		rc = smc_tx_sendmsg(smc, msg, len);
1550 out:
1551 	release_sock(sk);
1552 	return rc;
1553 }
1554 
1555 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1556 		       int flags)
1557 {
1558 	struct sock *sk = sock->sk;
1559 	struct smc_sock *smc;
1560 	int rc = -ENOTCONN;
1561 
1562 	smc = smc_sk(sk);
1563 	lock_sock(sk);
1564 	if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1565 		/* socket was connected before, no more data to read */
1566 		rc = 0;
1567 		goto out;
1568 	}
1569 	if ((sk->sk_state == SMC_INIT) ||
1570 	    (sk->sk_state == SMC_LISTEN) ||
1571 	    (sk->sk_state == SMC_CLOSED))
1572 		goto out;
1573 
1574 	if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1575 		rc = 0;
1576 		goto out;
1577 	}
1578 
1579 	if (smc->use_fallback) {
1580 		rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1581 	} else {
1582 		msg->msg_namelen = 0;
1583 		rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1584 	}
1585 
1586 out:
1587 	release_sock(sk);
1588 	return rc;
1589 }
1590 
1591 static __poll_t smc_accept_poll(struct sock *parent)
1592 {
1593 	struct smc_sock *isk = smc_sk(parent);
1594 	__poll_t mask = 0;
1595 
1596 	spin_lock(&isk->accept_q_lock);
1597 	if (!list_empty(&isk->accept_q))
1598 		mask = EPOLLIN | EPOLLRDNORM;
1599 	spin_unlock(&isk->accept_q_lock);
1600 
1601 	return mask;
1602 }
1603 
1604 static __poll_t smc_poll(struct file *file, struct socket *sock,
1605 			     poll_table *wait)
1606 {
1607 	struct sock *sk = sock->sk;
1608 	struct smc_sock *smc;
1609 	__poll_t mask = 0;
1610 
1611 	if (!sk)
1612 		return EPOLLNVAL;
1613 
1614 	smc = smc_sk(sock->sk);
1615 	if (smc->use_fallback) {
1616 		/* delegate to CLC child sock */
1617 		mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1618 		sk->sk_err = smc->clcsock->sk->sk_err;
1619 	} else {
1620 		if (sk->sk_state != SMC_CLOSED)
1621 			sock_poll_wait(file, sock, wait);
1622 		if (sk->sk_err)
1623 			mask |= EPOLLERR;
1624 		if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1625 		    (sk->sk_state == SMC_CLOSED))
1626 			mask |= EPOLLHUP;
1627 		if (sk->sk_state == SMC_LISTEN) {
1628 			/* woken up by sk_data_ready in smc_listen_work() */
1629 			mask |= smc_accept_poll(sk);
1630 		} else if (smc->use_fallback) { /* as result of connect_work()*/
1631 			mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1632 							   wait);
1633 			sk->sk_err = smc->clcsock->sk->sk_err;
1634 		} else {
1635 			if ((sk->sk_state != SMC_INIT &&
1636 			     atomic_read(&smc->conn.sndbuf_space)) ||
1637 			    sk->sk_shutdown & SEND_SHUTDOWN) {
1638 				mask |= EPOLLOUT | EPOLLWRNORM;
1639 			} else {
1640 				sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1641 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1642 			}
1643 			if (atomic_read(&smc->conn.bytes_to_rcv))
1644 				mask |= EPOLLIN | EPOLLRDNORM;
1645 			if (sk->sk_shutdown & RCV_SHUTDOWN)
1646 				mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1647 			if (sk->sk_state == SMC_APPCLOSEWAIT1)
1648 				mask |= EPOLLIN;
1649 			if (smc->conn.urg_state == SMC_URG_VALID)
1650 				mask |= EPOLLPRI;
1651 		}
1652 	}
1653 
1654 	return mask;
1655 }
1656 
1657 static int smc_shutdown(struct socket *sock, int how)
1658 {
1659 	struct sock *sk = sock->sk;
1660 	struct smc_sock *smc;
1661 	int rc = -EINVAL;
1662 	int rc1 = 0;
1663 
1664 	smc = smc_sk(sk);
1665 
1666 	if ((how < SHUT_RD) || (how > SHUT_RDWR))
1667 		return rc;
1668 
1669 	lock_sock(sk);
1670 
1671 	rc = -ENOTCONN;
1672 	if ((sk->sk_state != SMC_ACTIVE) &&
1673 	    (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1674 	    (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1675 	    (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1676 	    (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1677 	    (sk->sk_state != SMC_APPFINCLOSEWAIT))
1678 		goto out;
1679 	if (smc->use_fallback) {
1680 		rc = kernel_sock_shutdown(smc->clcsock, how);
1681 		sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1682 		if (sk->sk_shutdown == SHUTDOWN_MASK)
1683 			sk->sk_state = SMC_CLOSED;
1684 		goto out;
1685 	}
1686 	switch (how) {
1687 	case SHUT_RDWR:		/* shutdown in both directions */
1688 		rc = smc_close_active(smc);
1689 		break;
1690 	case SHUT_WR:
1691 		rc = smc_close_shutdown_write(smc);
1692 		break;
1693 	case SHUT_RD:
1694 		rc = 0;
1695 		/* nothing more to do because peer is not involved */
1696 		break;
1697 	}
1698 	if (smc->clcsock)
1699 		rc1 = kernel_sock_shutdown(smc->clcsock, how);
1700 	/* map sock_shutdown_cmd constants to sk_shutdown value range */
1701 	sk->sk_shutdown |= how + 1;
1702 
1703 out:
1704 	release_sock(sk);
1705 	return rc ? rc : rc1;
1706 }
1707 
1708 static int smc_setsockopt(struct socket *sock, int level, int optname,
1709 			  char __user *optval, unsigned int optlen)
1710 {
1711 	struct sock *sk = sock->sk;
1712 	struct smc_sock *smc;
1713 	int val, rc;
1714 
1715 	smc = smc_sk(sk);
1716 
1717 	/* generic setsockopts reaching us here always apply to the
1718 	 * CLC socket
1719 	 */
1720 	rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1721 					   optval, optlen);
1722 	if (smc->clcsock->sk->sk_err) {
1723 		sk->sk_err = smc->clcsock->sk->sk_err;
1724 		sk->sk_error_report(sk);
1725 	}
1726 
1727 	if (optlen < sizeof(int))
1728 		return -EINVAL;
1729 	if (get_user(val, (int __user *)optval))
1730 		return -EFAULT;
1731 
1732 	lock_sock(sk);
1733 	if (rc || smc->use_fallback)
1734 		goto out;
1735 	switch (optname) {
1736 	case TCP_ULP:
1737 	case TCP_FASTOPEN:
1738 	case TCP_FASTOPEN_CONNECT:
1739 	case TCP_FASTOPEN_KEY:
1740 	case TCP_FASTOPEN_NO_COOKIE:
1741 		/* option not supported by SMC */
1742 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1743 			smc_switch_to_fallback(smc);
1744 			smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1745 		} else {
1746 			rc = -EINVAL;
1747 		}
1748 		break;
1749 	case TCP_NODELAY:
1750 		if (sk->sk_state != SMC_INIT &&
1751 		    sk->sk_state != SMC_LISTEN &&
1752 		    sk->sk_state != SMC_CLOSED) {
1753 			if (val)
1754 				mod_delayed_work(system_wq, &smc->conn.tx_work,
1755 						 0);
1756 		}
1757 		break;
1758 	case TCP_CORK:
1759 		if (sk->sk_state != SMC_INIT &&
1760 		    sk->sk_state != SMC_LISTEN &&
1761 		    sk->sk_state != SMC_CLOSED) {
1762 			if (!val)
1763 				mod_delayed_work(system_wq, &smc->conn.tx_work,
1764 						 0);
1765 		}
1766 		break;
1767 	case TCP_DEFER_ACCEPT:
1768 		smc->sockopt_defer_accept = val;
1769 		break;
1770 	default:
1771 		break;
1772 	}
1773 out:
1774 	release_sock(sk);
1775 
1776 	return rc;
1777 }
1778 
1779 static int smc_getsockopt(struct socket *sock, int level, int optname,
1780 			  char __user *optval, int __user *optlen)
1781 {
1782 	struct smc_sock *smc;
1783 
1784 	smc = smc_sk(sock->sk);
1785 	/* socket options apply to the CLC socket */
1786 	return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1787 					     optval, optlen);
1788 }
1789 
1790 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1791 		     unsigned long arg)
1792 {
1793 	union smc_host_cursor cons, urg;
1794 	struct smc_connection *conn;
1795 	struct smc_sock *smc;
1796 	int answ;
1797 
1798 	smc = smc_sk(sock->sk);
1799 	conn = &smc->conn;
1800 	lock_sock(&smc->sk);
1801 	if (smc->use_fallback) {
1802 		if (!smc->clcsock) {
1803 			release_sock(&smc->sk);
1804 			return -EBADF;
1805 		}
1806 		answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1807 		release_sock(&smc->sk);
1808 		return answ;
1809 	}
1810 	switch (cmd) {
1811 	case SIOCINQ: /* same as FIONREAD */
1812 		if (smc->sk.sk_state == SMC_LISTEN) {
1813 			release_sock(&smc->sk);
1814 			return -EINVAL;
1815 		}
1816 		if (smc->sk.sk_state == SMC_INIT ||
1817 		    smc->sk.sk_state == SMC_CLOSED)
1818 			answ = 0;
1819 		else
1820 			answ = atomic_read(&smc->conn.bytes_to_rcv);
1821 		break;
1822 	case SIOCOUTQ:
1823 		/* output queue size (not send + not acked) */
1824 		if (smc->sk.sk_state == SMC_LISTEN) {
1825 			release_sock(&smc->sk);
1826 			return -EINVAL;
1827 		}
1828 		if (smc->sk.sk_state == SMC_INIT ||
1829 		    smc->sk.sk_state == SMC_CLOSED)
1830 			answ = 0;
1831 		else
1832 			answ = smc->conn.sndbuf_desc->len -
1833 					atomic_read(&smc->conn.sndbuf_space);
1834 		break;
1835 	case SIOCOUTQNSD:
1836 		/* output queue size (not send only) */
1837 		if (smc->sk.sk_state == SMC_LISTEN) {
1838 			release_sock(&smc->sk);
1839 			return -EINVAL;
1840 		}
1841 		if (smc->sk.sk_state == SMC_INIT ||
1842 		    smc->sk.sk_state == SMC_CLOSED)
1843 			answ = 0;
1844 		else
1845 			answ = smc_tx_prepared_sends(&smc->conn);
1846 		break;
1847 	case SIOCATMARK:
1848 		if (smc->sk.sk_state == SMC_LISTEN) {
1849 			release_sock(&smc->sk);
1850 			return -EINVAL;
1851 		}
1852 		if (smc->sk.sk_state == SMC_INIT ||
1853 		    smc->sk.sk_state == SMC_CLOSED) {
1854 			answ = 0;
1855 		} else {
1856 			smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1857 			smc_curs_copy(&urg, &conn->urg_curs, conn);
1858 			answ = smc_curs_diff(conn->rmb_desc->len,
1859 					     &cons, &urg) == 1;
1860 		}
1861 		break;
1862 	default:
1863 		release_sock(&smc->sk);
1864 		return -ENOIOCTLCMD;
1865 	}
1866 	release_sock(&smc->sk);
1867 
1868 	return put_user(answ, (int __user *)arg);
1869 }
1870 
1871 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1872 			    int offset, size_t size, int flags)
1873 {
1874 	struct sock *sk = sock->sk;
1875 	struct smc_sock *smc;
1876 	int rc = -EPIPE;
1877 
1878 	smc = smc_sk(sk);
1879 	lock_sock(sk);
1880 	if (sk->sk_state != SMC_ACTIVE) {
1881 		release_sock(sk);
1882 		goto out;
1883 	}
1884 	release_sock(sk);
1885 	if (smc->use_fallback)
1886 		rc = kernel_sendpage(smc->clcsock, page, offset,
1887 				     size, flags);
1888 	else
1889 		rc = sock_no_sendpage(sock, page, offset, size, flags);
1890 
1891 out:
1892 	return rc;
1893 }
1894 
1895 /* Map the affected portions of the rmbe into an spd, note the number of bytes
1896  * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1897  * updates till whenever a respective page has been fully processed.
1898  * Note that subsequent recv() calls have to wait till all splice() processing
1899  * completed.
1900  */
1901 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1902 			       struct pipe_inode_info *pipe, size_t len,
1903 			       unsigned int flags)
1904 {
1905 	struct sock *sk = sock->sk;
1906 	struct smc_sock *smc;
1907 	int rc = -ENOTCONN;
1908 
1909 	smc = smc_sk(sk);
1910 	lock_sock(sk);
1911 	if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1912 		/* socket was connected before, no more data to read */
1913 		rc = 0;
1914 		goto out;
1915 	}
1916 	if (sk->sk_state == SMC_INIT ||
1917 	    sk->sk_state == SMC_LISTEN ||
1918 	    sk->sk_state == SMC_CLOSED)
1919 		goto out;
1920 
1921 	if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1922 		rc = 0;
1923 		goto out;
1924 	}
1925 
1926 	if (smc->use_fallback) {
1927 		rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1928 						    pipe, len, flags);
1929 	} else {
1930 		if (*ppos) {
1931 			rc = -ESPIPE;
1932 			goto out;
1933 		}
1934 		if (flags & SPLICE_F_NONBLOCK)
1935 			flags = MSG_DONTWAIT;
1936 		else
1937 			flags = 0;
1938 		rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
1939 	}
1940 out:
1941 	release_sock(sk);
1942 
1943 	return rc;
1944 }
1945 
1946 /* must look like tcp */
1947 static const struct proto_ops smc_sock_ops = {
1948 	.family		= PF_SMC,
1949 	.owner		= THIS_MODULE,
1950 	.release	= smc_release,
1951 	.bind		= smc_bind,
1952 	.connect	= smc_connect,
1953 	.socketpair	= sock_no_socketpair,
1954 	.accept		= smc_accept,
1955 	.getname	= smc_getname,
1956 	.poll		= smc_poll,
1957 	.ioctl		= smc_ioctl,
1958 	.listen		= smc_listen,
1959 	.shutdown	= smc_shutdown,
1960 	.setsockopt	= smc_setsockopt,
1961 	.getsockopt	= smc_getsockopt,
1962 	.sendmsg	= smc_sendmsg,
1963 	.recvmsg	= smc_recvmsg,
1964 	.mmap		= sock_no_mmap,
1965 	.sendpage	= smc_sendpage,
1966 	.splice_read	= smc_splice_read,
1967 };
1968 
1969 static int smc_create(struct net *net, struct socket *sock, int protocol,
1970 		      int kern)
1971 {
1972 	int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
1973 	struct smc_sock *smc;
1974 	struct sock *sk;
1975 	int rc;
1976 
1977 	rc = -ESOCKTNOSUPPORT;
1978 	if (sock->type != SOCK_STREAM)
1979 		goto out;
1980 
1981 	rc = -EPROTONOSUPPORT;
1982 	if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
1983 		goto out;
1984 
1985 	rc = -ENOBUFS;
1986 	sock->ops = &smc_sock_ops;
1987 	sk = smc_sock_alloc(net, sock, protocol);
1988 	if (!sk)
1989 		goto out;
1990 
1991 	/* create internal TCP socket for CLC handshake and fallback */
1992 	smc = smc_sk(sk);
1993 	smc->use_fallback = false; /* assume rdma capability first */
1994 	smc->fallback_rsn = 0;
1995 	rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1996 			      &smc->clcsock);
1997 	if (rc) {
1998 		sk_common_release(sk);
1999 		goto out;
2000 	}
2001 	smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2002 	smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
2003 
2004 out:
2005 	return rc;
2006 }
2007 
2008 static const struct net_proto_family smc_sock_family_ops = {
2009 	.family	= PF_SMC,
2010 	.owner	= THIS_MODULE,
2011 	.create	= smc_create,
2012 };
2013 
2014 unsigned int smc_net_id;
2015 
2016 static __net_init int smc_net_init(struct net *net)
2017 {
2018 	return smc_pnet_net_init(net);
2019 }
2020 
2021 static void __net_exit smc_net_exit(struct net *net)
2022 {
2023 	smc_pnet_net_exit(net);
2024 }
2025 
2026 static struct pernet_operations smc_net_ops = {
2027 	.init = smc_net_init,
2028 	.exit = smc_net_exit,
2029 	.id   = &smc_net_id,
2030 	.size = sizeof(struct smc_net),
2031 };
2032 
2033 static int __init smc_init(void)
2034 {
2035 	int rc;
2036 
2037 	rc = register_pernet_subsys(&smc_net_ops);
2038 	if (rc)
2039 		return rc;
2040 
2041 	rc = smc_pnet_init();
2042 	if (rc)
2043 		goto out_pernet_subsys;
2044 
2045 	rc = smc_core_init();
2046 	if (rc) {
2047 		pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2048 		goto out_pnet;
2049 	}
2050 
2051 	rc = smc_llc_init();
2052 	if (rc) {
2053 		pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2054 		goto out_core;
2055 	}
2056 
2057 	rc = smc_cdc_init();
2058 	if (rc) {
2059 		pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2060 		goto out_core;
2061 	}
2062 
2063 	rc = proto_register(&smc_proto, 1);
2064 	if (rc) {
2065 		pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2066 		goto out_core;
2067 	}
2068 
2069 	rc = proto_register(&smc_proto6, 1);
2070 	if (rc) {
2071 		pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2072 		goto out_proto;
2073 	}
2074 
2075 	rc = sock_register(&smc_sock_family_ops);
2076 	if (rc) {
2077 		pr_err("%s: sock_register fails with %d\n", __func__, rc);
2078 		goto out_proto6;
2079 	}
2080 	INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2081 	INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2082 
2083 	rc = smc_ib_register_client();
2084 	if (rc) {
2085 		pr_err("%s: ib_register fails with %d\n", __func__, rc);
2086 		goto out_sock;
2087 	}
2088 
2089 	static_branch_enable(&tcp_have_smc);
2090 	return 0;
2091 
2092 out_sock:
2093 	sock_unregister(PF_SMC);
2094 out_proto6:
2095 	proto_unregister(&smc_proto6);
2096 out_proto:
2097 	proto_unregister(&smc_proto);
2098 out_core:
2099 	smc_core_exit();
2100 out_pnet:
2101 	smc_pnet_exit();
2102 out_pernet_subsys:
2103 	unregister_pernet_subsys(&smc_net_ops);
2104 
2105 	return rc;
2106 }
2107 
2108 static void __exit smc_exit(void)
2109 {
2110 	static_branch_disable(&tcp_have_smc);
2111 	sock_unregister(PF_SMC);
2112 	smc_core_exit();
2113 	smc_ib_unregister_client();
2114 	proto_unregister(&smc_proto6);
2115 	proto_unregister(&smc_proto);
2116 	smc_pnet_exit();
2117 	unregister_pernet_subsys(&smc_net_ops);
2118 	rcu_barrier();
2119 }
2120 
2121 module_init(smc_init);
2122 module_exit(smc_exit);
2123 
2124 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2125 MODULE_DESCRIPTION("smc socket address family");
2126 MODULE_LICENSE("GPL");
2127 MODULE_ALIAS_NETPROTO(PF_SMC);
2128