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