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