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