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