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