blob: 97ab6b5afeadc27406a856b138758897635ed877 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ursula Braunac713872017-01-09 16:55:13 +01002/*
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 *
Ursula Brauna046d572017-01-09 16:55:16 +010010 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010011 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010012 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010013 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010014 *
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>
Ursula Brauna046d572017-01-09 16:55:16 +010024#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010025#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010026#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020027#include <linux/if_vlan.h>
Ursula Braun4ead9c92019-11-16 17:47:31 +010028#include <linux/rcupdate_wait.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010029
Ursula Braunac713872017-01-09 16:55:13 +010030#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010031#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010032#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020033#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010034
Hans Wippel64e28b52019-02-21 13:01:02 +010035#include <net/net_namespace.h>
36#include <net/netns/generic.h>
37#include "smc_netns.h"
38
Ursula Braunac713872017-01-09 16:55:13 +010039#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010040#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010041#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010042#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010043#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010044#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020045#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010046#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010047#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010048#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010049#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010050
Hans Wippel72a36a82019-02-07 15:56:18 +010051static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
52 * creation on server
53 */
54static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
55 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010056 */
57
Karsten Graul22ef4732020-09-10 18:48:29 +020058struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
59struct workqueue_struct *smc_close_wq; /* wq for close work */
60
Ursula Brauna046d572017-01-09 16:55:16 +010061static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020062static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010063
Ursula Braunac713872017-01-09 16:55:13 +010064static void smc_set_keepalive(struct sock *sk, int val)
65{
66 struct smc_sock *smc = smc_sk(sk);
67
68 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
69}
70
Ursula Braunf16a7dd2017-01-09 16:55:26 +010071static struct smc_hashinfo smc_v4_hashinfo = {
72 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
73};
74
Karsten Graulaaa4d332018-03-16 15:06:41 +010075static struct smc_hashinfo smc_v6_hashinfo = {
76 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
77};
78
Ursula Braunf16a7dd2017-01-09 16:55:26 +010079int smc_hash_sk(struct sock *sk)
80{
81 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
82 struct hlist_head *head;
83
84 head = &h->ht;
85
86 write_lock_bh(&h->lock);
87 sk_add_node(sk, head);
88 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
89 write_unlock_bh(&h->lock);
90
91 return 0;
92}
93EXPORT_SYMBOL_GPL(smc_hash_sk);
94
95void smc_unhash_sk(struct sock *sk)
96{
97 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
98
99 write_lock_bh(&h->lock);
100 if (sk_del_node_init(sk))
101 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
102 write_unlock_bh(&h->lock);
103}
104EXPORT_SYMBOL_GPL(smc_unhash_sk);
105
106struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100107 .name = "SMC",
108 .owner = THIS_MODULE,
109 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100110 .hash = smc_hash_sk,
111 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100112 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100113 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800114 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100115};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100116EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100117
Karsten Graulaaa4d332018-03-16 15:06:41 +0100118struct proto smc_proto6 = {
119 .name = "SMC6",
120 .owner = THIS_MODULE,
121 .keepalive = smc_set_keepalive,
122 .hash = smc_hash_sk,
123 .unhash = smc_unhash_sk,
124 .obj_size = sizeof(struct smc_sock),
125 .h.smc_hash = &smc_v6_hashinfo,
126 .slab_flags = SLAB_TYPESAFE_BY_RCU,
127};
128EXPORT_SYMBOL_GPL(smc_proto6);
129
Ursula Braunf536dff2019-10-23 15:44:05 +0200130static void smc_restore_fallback_changes(struct smc_sock *smc)
131{
Karsten Graul1ad24052020-07-18 15:06:18 +0200132 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
133 smc->clcsock->file->private_data = smc->sk.sk_socket;
134 smc->clcsock->file = NULL;
135 }
Ursula Braunf536dff2019-10-23 15:44:05 +0200136}
137
Ursula Braun39f41f32019-06-27 15:04:52 +0200138static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100139{
Ursula Braun39f41f32019-06-27 15:04:52 +0200140 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100142
Ursula Braun51f1de72018-01-26 09:28:48 +0100143 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100144 rc = smc_close_active(smc);
145 sock_set_flag(sk, SOCK_DEAD);
146 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100147 } else {
148 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
149 sock_put(sk); /* passive closing */
150 if (sk->sk_state == SMC_LISTEN) {
151 /* wake up clcsock accept */
152 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
153 }
154 sk->sk_state = SMC_CLOSED;
155 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200156 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100157 }
Cong Wang26d92e92019-01-05 23:45:26 -0800158
159 sk->sk_prot->unhash(sk);
160
Ursula Braunb03faa12019-02-07 15:56:15 +0100161 if (sk->sk_state == SMC_CLOSED) {
162 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200163 release_sock(sk);
164 smc_clcsock_release(smc);
165 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800166 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100167 if (!smc->use_fallback)
168 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100169 }
Ursula Braunac713872017-01-09 16:55:13 +0100170
Ursula Braun39f41f32019-06-27 15:04:52 +0200171 return rc;
172}
173
174static int smc_release(struct socket *sock)
175{
176 struct sock *sk = sock->sk;
177 struct smc_sock *smc;
178 int rc = 0;
179
180 if (!sk)
181 goto out;
182
Ursula Braun81cf4f42019-10-21 16:13:15 +0200183 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200184 smc = smc_sk(sk);
185
186 /* cleanup for a dangling non-blocking connect */
187 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
188 tcp_abort(smc->clcsock->sk, ECONNABORTED);
189 flush_work(&smc->connect_work);
190
191 if (sk->sk_state == SMC_LISTEN)
192 /* smc_close_non_accepted() is called and acquires
193 * sock lock for child sockets again
194 */
195 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
196 else
197 lock_sock(sk);
198
199 rc = __smc_release(smc);
200
Ursula Braunac713872017-01-09 16:55:13 +0100201 /* detach socket */
202 sock_orphan(sk);
203 sock->sk = NULL;
204 release_sock(sk);
205
Ursula Braun81cf4f42019-10-21 16:13:15 +0200206 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100207 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100208out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100209 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100210}
211
212static void smc_destruct(struct sock *sk)
213{
214 if (sk->sk_state != SMC_CLOSED)
215 return;
216 if (!sock_flag(sk, SOCK_DEAD))
217 return;
218
219 sk_refcnt_debug_dec(sk);
220}
221
Karsten Graulaaa4d332018-03-16 15:06:41 +0100222static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
223 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100224{
225 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100226 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100227 struct sock *sk;
228
Karsten Graulaaa4d332018-03-16 15:06:41 +0100229 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
230 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100231 if (!sk)
232 return NULL;
233
234 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
235 sk->sk_state = SMC_INIT;
236 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100237 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100238 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100239 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200240 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700241 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100242 INIT_LIST_HEAD(&smc->accept_q);
243 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700244 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100245 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100246 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800247 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100248
249 return sk;
250}
251
252static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
253 int addr_len)
254{
255 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
256 struct sock *sk = sock->sk;
257 struct smc_sock *smc;
258 int rc;
259
260 smc = smc_sk(sk);
261
262 /* replicate tests from inet_bind(), to be safe wrt. future changes */
263 rc = -EINVAL;
264 if (addr_len < sizeof(struct sockaddr_in))
265 goto out;
266
267 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100268 if (addr->sin_family != AF_INET &&
269 addr->sin_family != AF_INET6 &&
270 addr->sin_family != AF_UNSPEC)
271 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100272 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100273 if (addr->sin_family == AF_UNSPEC &&
274 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100275 goto out;
276
277 lock_sock(sk);
278
279 /* Check if socket is already active */
280 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200281 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100282 goto out_rel;
283
284 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
285 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
286
287out_rel:
288 release_sock(sk);
289out:
290 return rc;
291}
292
293static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
294 unsigned long mask)
295{
296 /* options we don't get control via setsockopt for */
297 nsk->sk_type = osk->sk_type;
298 nsk->sk_sndbuf = osk->sk_sndbuf;
299 nsk->sk_rcvbuf = osk->sk_rcvbuf;
300 nsk->sk_sndtimeo = osk->sk_sndtimeo;
301 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
302 nsk->sk_mark = osk->sk_mark;
303 nsk->sk_priority = osk->sk_priority;
304 nsk->sk_rcvlowat = osk->sk_rcvlowat;
305 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
306 nsk->sk_err = osk->sk_err;
307
308 nsk->sk_flags &= ~mask;
309 nsk->sk_flags |= osk->sk_flags & mask;
310}
311
312#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
313 (1UL << SOCK_KEEPOPEN) | \
314 (1UL << SOCK_LINGER) | \
315 (1UL << SOCK_BROADCAST) | \
316 (1UL << SOCK_TIMESTAMP) | \
317 (1UL << SOCK_DBG) | \
318 (1UL << SOCK_RCVTSTAMP) | \
319 (1UL << SOCK_RCVTSTAMPNS) | \
320 (1UL << SOCK_LOCALROUTE) | \
321 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
322 (1UL << SOCK_RXQ_OVFL) | \
323 (1UL << SOCK_WIFI_STATUS) | \
324 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800325 (1UL << SOCK_FILTER_LOCKED) | \
326 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100327/* copy only relevant settings and flags of SOL_SOCKET level from smc to
328 * clc socket (since smc is not called for these options from net/core)
329 */
330static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
331{
332 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
333}
334
335#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
336 (1UL << SOCK_KEEPOPEN) | \
337 (1UL << SOCK_LINGER) | \
338 (1UL << SOCK_DBG))
339/* copy only settings and flags relevant for smc from clc to smc socket */
340static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
341{
342 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
343}
344
Karsten Graulb9247542020-04-29 17:10:41 +0200345/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200346static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200347 struct smc_buf_desc *rmb_desc)
348{
Karsten Graul7562a132020-05-01 12:48:01 +0200349 struct smc_link_group *lgr = link->lgr;
350 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200351
Karsten Grauld5500662020-05-01 12:48:05 +0200352 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
353 if (rc)
354 return rc;
355 /* protect against parallel smc_llc_cli_rkey_exchange() and
356 * parallel smcr_link_reg_rmb()
357 */
358 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200359 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200360 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graulb9247542020-04-29 17:10:41 +0200361 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200362 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200363 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200364 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200365 }
Karsten Graul7562a132020-05-01 12:48:01 +0200366
367 /* exchange confirm_rkey msg with peer */
368 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
369 if (rc) {
370 rc = -EFAULT;
371 goto out;
372 }
373 rmb_desc->is_conf_rkey = true;
374out:
Karsten Grauld5500662020-05-01 12:48:05 +0200375 mutex_unlock(&lgr->llc_conf_mutex);
376 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200377 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200378}
379
380static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381{
Karsten Graul387707f2020-04-29 17:10:40 +0200382 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200383 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100384 int rc;
385
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100386 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200387 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
388 SMC_LLC_CONFIRM_LINK);
389 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100390 struct smc_clc_msg_decline dclc;
391
392 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100393 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100394 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100395 }
Karsten Graul649758f2020-05-04 14:18:48 +0200396 smc_llc_save_peer_uid(qentry);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200397 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
398 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
399 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100400 return SMC_CLC_DECL_RMBE_EC;
401
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100402 rc = smc_ib_modify_qp_rts(link);
403 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200404 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100405
406 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200407
Karsten Graul7562a132020-05-01 12:48:01 +0200408 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200409 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200410
Karsten Graul0fb0b022020-04-30 15:55:43 +0200411 /* confirm_rkey is implicit on 1st contact */
412 smc->conn.rmb_desc->is_conf_rkey = true;
413
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100414 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200415 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100416 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200417 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100418
Karsten Graul0fb0b022020-04-30 15:55:43 +0200419 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +0200420 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200421
422 /* optional 2nd link, receive ADD LINK request from server */
423 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
424 SMC_LLC_ADD_LINK);
425 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100426 struct smc_clc_msg_decline dclc;
427
428 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100429 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200430 if (rc == -EAGAIN)
431 rc = 0; /* no DECLINE received, go with one link */
432 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100433 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200434 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200435 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100436 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100437}
438
Hans Wippel41349842018-06-28 19:05:11 +0200439static void smcr_conn_save_peer_info(struct smc_sock *smc,
440 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100441{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200442 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200443
Ursula Braun3d9725a2020-09-10 18:48:23 +0200444 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
445 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200446 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100447 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200448 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100449}
450
Hans Wippel41349842018-06-28 19:05:11 +0200451static void smcd_conn_save_peer_info(struct smc_sock *smc,
452 struct smc_clc_msg_accept_confirm *clc)
453{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200454 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
Hans Wippel41349842018-06-28 19:05:11 +0200455
Ursula Braun3d9725a2020-09-10 18:48:23 +0200456 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
457 smc->conn.peer_token = clc->d0.token;
Hans Wippel41349842018-06-28 19:05:11 +0200458 /* msg header takes up space in the buffer */
459 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
460 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
461 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
462}
463
464static void smc_conn_save_peer_info(struct smc_sock *smc,
465 struct smc_clc_msg_accept_confirm *clc)
466{
467 if (smc->conn.lgr->is_smcd)
468 smcd_conn_save_peer_info(smc, clc);
469 else
470 smcr_conn_save_peer_info(smc, clc);
471}
472
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100473static void smc_link_save_peer_info(struct smc_link *link,
474 struct smc_clc_msg_accept_confirm *clc)
475{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200476 link->peer_qpn = ntoh24(clc->r0.qpn);
477 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
478 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
479 link->peer_psn = ntoh24(clc->r0.psn);
480 link->peer_mtu = clc->r0.qp_mtu;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100481}
482
Ursula Braun07603b22019-04-11 11:17:32 +0200483static void smc_switch_to_fallback(struct smc_sock *smc)
484{
485 smc->use_fallback = true;
486 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
487 smc->clcsock->file = smc->sk.sk_socket->file;
488 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100489 smc->clcsock->wq.fasync_list =
490 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200491 }
492}
493
Hans Wippel3b2dec22018-05-18 09:34:18 +0200494/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200495static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496{
Ursula Braun07603b22019-04-11 11:17:32 +0200497 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200498 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200499 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200500 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200501 if (smc->sk.sk_state == SMC_INIT)
502 smc->sk.sk_state = SMC_ACTIVE;
503 return 0;
504}
505
506/* decline and fall back during connect */
507static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
508{
509 int rc;
510
Ursula Braune1bbdd52018-07-05 16:15:30 +0200511 if (reason_code < 0) { /* error, fallback is not possible */
512 if (smc->sk.sk_state == SMC_INIT)
513 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200514 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200515 }
Karsten Graul603cc142018-07-25 16:35:32 +0200516 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200517 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200518 if (rc < 0) {
519 if (smc->sk.sk_state == SMC_INIT)
520 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200521 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200522 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200523 }
Karsten Graul603cc142018-07-25 16:35:32 +0200524 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200525}
526
527/* abort connecting */
528static int smc_connect_abort(struct smc_sock *smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200529 int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200530{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100531 bool is_smcd = smc->conn.lgr->is_smcd;
532
Ursula Braun5ac54d82020-09-10 18:48:21 +0200533 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100534 smc_lgr_cleanup_early(&smc->conn);
535 else
536 smc_conn_free(&smc->conn);
537 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100538 /* there is only one lgr role for SMC-D; use server lock */
539 mutex_unlock(&smc_server_lgr_pending);
540 else
541 mutex_unlock(&smc_client_lgr_pending);
542
Ursula Braun50717a32019-04-12 12:57:23 +0200543 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200544 return reason_code;
545}
546
547/* check if there is a rdma device available for this connection. */
548/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200549static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200550{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200551 /* PNET table look up: search active ib_device and port
552 * within same PNETID that also contains the ethernet device
553 * used for the internal TCP socket
554 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200555 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200556 if (!ini->ib_dev)
557 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200558 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200559}
560
Hans Wippel41349842018-06-28 19:05:11 +0200561/* check if there is an ISM device available for this connection. */
562/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200563static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200564{
565 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200566 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200567 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200568 return SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun8caaccf2020-09-26 12:44:25 +0200569 else
570 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
Hans Wippel41349842018-06-28 19:05:11 +0200571 return 0;
572}
573
Ursula Braund70bf4f2020-09-26 12:44:27 +0200574/* determine possible V2 ISM devices (either without PNETID or with PNETID plus
575 * PNETID matching net_device)
576 */
577static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
578 struct smc_init_info *ini)
579{
580 int rc = SMC_CLC_DECL_NOSMCDDEV;
581 struct smcd_dev *smcd;
582 int i = 1;
583
584 if (smcd_indicated(ini->smc_type_v1))
585 rc = 0; /* already initialized for V1 */
586 mutex_lock(&smcd_dev_list.mutex);
587 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
588 if (smcd->going_away || smcd == ini->ism_dev[0])
589 continue;
590 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
591 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
592 ini->ism_dev[i] = smcd;
593 ini->ism_chid[i] = smc_ism_get_chid(ini->ism_dev[i]);
594 ini->is_smcd = true;
595 rc = 0;
596 i++;
597 if (i > SMC_MAX_ISM_DEVS)
598 break;
599 }
600 }
601 mutex_unlock(&smcd_dev_list.mutex);
602 ini->ism_offered_cnt = i - 1;
603 if (!ini->ism_dev[0] && !ini->ism_dev[1])
604 ini->smcd_version = 0;
605
606 return rc;
607}
608
Hans Wippel41349842018-06-28 19:05:11 +0200609/* Check for VLAN ID and register it on ISM device just for CLC handshake */
610static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200611 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200612{
Ursula Braun3fc64932020-09-26 12:44:23 +0200613 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200614 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200615 return 0;
616}
617
Ursula Braund70bf4f2020-09-26 12:44:27 +0200618static int smc_find_proposal_devices(struct smc_sock *smc,
619 struct smc_init_info *ini)
620{
621 int rc = 0;
622
623 /* check if there is an ism device available */
624 if (ini->smcd_version & SMC_V1) {
625 if (smc_find_ism_device(smc, ini) ||
626 smc_connect_ism_vlan_setup(smc, ini)) {
627 if (ini->smc_type_v1 == SMC_TYPE_B)
628 ini->smc_type_v1 = SMC_TYPE_R;
629 else
630 ini->smc_type_v1 = SMC_TYPE_N;
631 } /* else ISM V1 is supported for this connection */
632 if (smc_find_rdma_device(smc, ini)) {
633 if (ini->smc_type_v1 == SMC_TYPE_B)
634 ini->smc_type_v1 = SMC_TYPE_D;
635 else
636 ini->smc_type_v1 = SMC_TYPE_N;
637 } /* else RDMA is supported for this connection */
638 }
639 if (smc_ism_v2_capable && smc_find_ism_v2_device_clnt(smc, ini))
640 ini->smc_type_v2 = SMC_TYPE_N;
641
642 /* if neither ISM nor RDMA are supported, fallback */
643 if (!smcr_indicated(ini->smc_type_v1) &&
644 ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
645 rc = SMC_CLC_DECL_NOSMCDEV;
646
647 return rc;
648}
649
Hans Wippel41349842018-06-28 19:05:11 +0200650/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
651 * used, the VLAN ID will be registered again during the connection setup.
652 */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200653static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200654 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200655{
Ursula Braund70bf4f2020-09-26 12:44:27 +0200656 if (!smcd_indicated(ini->smc_type_v1))
Hans Wippel41349842018-06-28 19:05:11 +0200657 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200658 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200659 return SMC_CLC_DECL_CNFERR;
660 return 0;
661}
662
Hans Wippel3b2dec22018-05-18 09:34:18 +0200663/* CLC handshake during connect */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200664static int smc_connect_clc(struct smc_sock *smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200665 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200666 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200667{
668 int rc = 0;
669
670 /* do inband token exchange */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200671 rc = smc_clc_send_proposal(smc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200672 if (rc)
673 return rc;
674 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100675 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
676 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200677}
678
Ursula Brauna046d572017-01-09 16:55:16 +0100679/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200680static int smc_connect_rdma(struct smc_sock *smc,
681 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200682 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200683{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200684 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200685 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200686
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200687 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200688 ini->ib_lcl = &aclc->r0.lcl;
689 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200690 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200691
Hans Wippel72a36a82019-02-07 15:56:18 +0100692 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200693 reason_code = smc_conn_create(smc, ini);
694 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100695 mutex_unlock(&smc_client_lgr_pending);
696 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200697 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200698
699 smc_conn_save_peer_info(smc, aclc);
700
Ursula Braun5ac54d82020-09-10 18:48:21 +0200701 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200702 link = smc->conn.lnk;
703 } else {
704 /* set link that was assigned by server */
705 link = NULL;
706 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
707 struct smc_link *l = &smc->conn.lgr->lnk[i];
708
Ursula Braun3d9725a2020-09-10 18:48:23 +0200709 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
710 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
711 SMC_GID_SIZE) &&
712 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
713 sizeof(l->peer_mac))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200714 link = l;
715 break;
716 }
717 }
718 if (!link)
719 return smc_connect_abort(smc, SMC_CLC_DECL_NOSRVLINK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200720 ini->first_contact_local);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200721 smc->conn.lnk = link;
722 }
723
Hans Wippel3b2dec22018-05-18 09:34:18 +0200724 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200725 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200726 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200727 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200728
Ursula Braun5ac54d82020-09-10 18:48:21 +0200729 if (ini->first_contact_local)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200730 smc_link_save_peer_info(link, aclc);
731
Karsten Graule07d31d2020-04-29 17:10:42 +0200732 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200733 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200734 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200735
736 smc_close_init(smc);
737 smc_rx_init(smc);
738
Ursula Braun5ac54d82020-09-10 18:48:21 +0200739 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200740 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200741 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200742 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200743 } else {
Karsten Graul7562a132020-05-01 12:48:01 +0200744 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200745 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200746 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200747 }
748 smc_rmb_sync_sg_for_device(&smc->conn);
749
750 reason_code = smc_clc_send_confirm(smc);
751 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200752 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200753 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200754
755 smc_tx_init(smc);
756
Ursula Braun5ac54d82020-09-10 18:48:21 +0200757 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200758 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200759 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200760 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200761 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200762 if (reason_code)
763 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200764 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200765 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100766 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200767
768 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200769 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200770 if (smc->sk.sk_state == SMC_INIT)
771 smc->sk.sk_state = SMC_ACTIVE;
772
773 return 0;
774}
775
Hans Wippel41349842018-06-28 19:05:11 +0200776/* setup for ISM connection of client */
777static int smc_connect_ism(struct smc_sock *smc,
778 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200779 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200780{
Hans Wippel41349842018-06-28 19:05:11 +0200781 int rc = 0;
782
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200783 ini->is_smcd = true;
Ursula Braun3fc64932020-09-26 12:44:23 +0200784 ini->ism_peer_gid[0] = aclc->d0.gid;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200785 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200786
Hans Wippel72a36a82019-02-07 15:56:18 +0100787 /* there is only one lgr role for SMC-D; use server lock */
788 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200789 rc = smc_conn_create(smc, ini);
790 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100791 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200792 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100793 }
Hans Wippel41349842018-06-28 19:05:11 +0200794
795 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +0200796 rc = smc_buf_create(smc, true);
797 if (rc)
798 return smc_connect_abort(smc, (rc == -ENOSPC) ?
799 SMC_CLC_DECL_MAX_DMB :
800 SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200801 ini->first_contact_local);
Hans Wippel41349842018-06-28 19:05:11 +0200802
803 smc_conn_save_peer_info(smc, aclc);
804 smc_close_init(smc);
805 smc_rx_init(smc);
806 smc_tx_init(smc);
807
808 rc = smc_clc_send_confirm(smc);
809 if (rc)
Ursula Braun5ac54d82020-09-10 18:48:21 +0200810 return smc_connect_abort(smc, rc, ini->first_contact_local);
Hans Wippel72a36a82019-02-07 15:56:18 +0100811 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200812
813 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200814 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200815 if (smc->sk.sk_state == SMC_INIT)
816 smc->sk.sk_state = SMC_ACTIVE;
817
818 return 0;
819}
820
Ursula Braund70bf4f2020-09-26 12:44:27 +0200821/* check if received accept type and version matches a proposed one */
822static int smc_connect_check_aclc(struct smc_init_info *ini,
823 struct smc_clc_msg_accept_confirm *aclc)
824{
825 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
826 !smcr_indicated(ini->smc_type_v1)) ||
827 (aclc->hdr.typev1 == SMC_TYPE_D &&
828 !smcd_indicated(ini->smc_type_v1)))
829 return SMC_CLC_DECL_MODEUNSUPP;
830
831 return 0;
832}
833
Hans Wippel3b2dec22018-05-18 09:34:18 +0200834/* perform steps before actually connecting */
835static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100836{
837 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +0200838 struct smc_init_info *ini = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +0100839 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100840
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200841 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200842 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200843
Hans Wippel3b2dec22018-05-18 09:34:18 +0200844 /* if peer has not signalled SMC-capability, fall back */
845 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200846 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200847
Ursula Brauna046d572017-01-09 16:55:16 +0100848 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200849 if (using_ipsec(smc))
850 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100851
Ursula Braun3fc64932020-09-26 12:44:23 +0200852 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
853 if (!ini)
854 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM);
855
Ursula Braund70bf4f2020-09-26 12:44:27 +0200856 ini->smcd_version = SMC_V1;
857 ini->smcd_version |= smc_ism_v2_capable ? SMC_V2 : 0;
858 ini->smc_type_v1 = SMC_TYPE_B;
859 ini->smc_type_v2 = smc_ism_v2_capable ? SMC_TYPE_D : SMC_TYPE_N;
860
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200861 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +0200862 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
Ursula Braund70bf4f2020-09-26 12:44:27 +0200863 ini->smcd_version &= ~SMC_V1;
864 ini->smc_type_v1 = SMC_TYPE_N;
865 if (!ini->smcd_version) {
866 rc = SMC_CLC_DECL_GETVLANERR;
867 goto fallback;
868 }
Ursula Braun3fc64932020-09-26 12:44:23 +0200869 }
Hans Wippel41349842018-06-28 19:05:11 +0200870
Ursula Braund70bf4f2020-09-26 12:44:27 +0200871 rc = smc_find_proposal_devices(smc, ini);
872 if (rc)
873 goto fallback;
Ursula Brauna046d572017-01-09 16:55:16 +0100874
Hans Wippel3b2dec22018-05-18 09:34:18 +0200875 /* perform CLC handshake */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200876 rc = smc_connect_clc(smc, &aclc, ini);
877 if (rc)
878 goto vlan_cleanup;
879
880 /* check if smc modes and versions of CLC proposal and accept match */
881 rc = smc_connect_check_aclc(ini, &aclc);
882 if (rc)
883 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100884
Hans Wippel41349842018-06-28 19:05:11 +0200885 /* depending on previous steps, connect using rdma or ism */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200886 if (aclc.hdr.typev1 == SMC_TYPE_R)
Ursula Braun3fc64932020-09-26 12:44:23 +0200887 rc = smc_connect_rdma(smc, &aclc, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200888 else if (aclc.hdr.typev1 == SMC_TYPE_D)
Ursula Braun3fc64932020-09-26 12:44:23 +0200889 rc = smc_connect_ism(smc, &aclc, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200890 if (rc)
891 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100892
Ursula Braund70bf4f2020-09-26 12:44:27 +0200893 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200894 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200895 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200896
897vlan_cleanup:
898 smc_connect_ism_vlan_cleanup(smc, ini);
899fallback:
900 kfree(ini);
901 return smc_connect_decline_fallback(smc, rc);
Ursula Brauna046d572017-01-09 16:55:16 +0100902}
903
Ursula Braun24ac3a02018-06-27 17:59:50 +0200904static void smc_connect_work(struct work_struct *work)
905{
906 struct smc_sock *smc = container_of(work, struct smc_sock,
907 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200908 long timeo = smc->sk.sk_sndtimeo;
909 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200910
Ursula Braun50717a32019-04-12 12:57:23 +0200911 if (!timeo)
912 timeo = MAX_SCHEDULE_TIMEOUT;
913 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200914 if (smc->clcsock->sk->sk_err) {
915 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200916 } else if ((1 << smc->clcsock->sk->sk_state) &
917 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
918 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
919 if ((rc == -EPIPE) &&
920 ((1 << smc->clcsock->sk->sk_state) &
921 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
922 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200923 }
Ursula Braun50717a32019-04-12 12:57:23 +0200924 release_sock(smc->clcsock->sk);
925 lock_sock(&smc->sk);
926 if (rc != 0 || smc->sk.sk_err) {
927 smc->sk.sk_state = SMC_CLOSED;
928 if (rc == -EPIPE || rc == -EAGAIN)
929 smc->sk.sk_err = EPIPE;
930 else if (signal_pending(current))
931 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100932 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200933 goto out;
934 }
935
936 rc = __smc_connect(smc);
937 if (rc < 0)
938 smc->sk.sk_err = -rc;
939
940out:
Ursula Braun07603b22019-04-11 11:17:32 +0200941 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
942 if (smc->sk.sk_err) {
943 smc->sk.sk_state_change(&smc->sk);
944 } else { /* allow polling before and after fallback decision */
945 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
946 smc->sk.sk_write_space(&smc->sk);
947 }
948 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200949 release_sock(&smc->sk);
950}
951
Ursula Braunac713872017-01-09 16:55:13 +0100952static int smc_connect(struct socket *sock, struct sockaddr *addr,
953 int alen, int flags)
954{
955 struct sock *sk = sock->sk;
956 struct smc_sock *smc;
957 int rc = -EINVAL;
958
959 smc = smc_sk(sk);
960
961 /* separate smc parameter checking to be safe */
962 if (alen < sizeof(addr->sa_family))
963 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100964 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100965 goto out_err;
966
967 lock_sock(sk);
968 switch (sk->sk_state) {
969 default:
970 goto out;
971 case SMC_ACTIVE:
972 rc = -EISCONN;
973 goto out;
974 case SMC_INIT:
975 rc = 0;
976 break;
977 }
978
979 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200980 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200981 if (smc->connect_nonblock) {
982 rc = -EALREADY;
983 goto out;
984 }
985 rc = kernel_connect(smc->clcsock, addr, alen, flags);
986 if (rc && rc != -EINPROGRESS)
987 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100988
989 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100990 if (smc->use_fallback)
991 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200992 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +0200993 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +0200994 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200995 rc = -EINPROGRESS;
996 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200997 rc = __smc_connect(smc);
998 if (rc < 0)
999 goto out;
1000 else
1001 rc = 0; /* success cases including fallback */
1002 }
Ursula Braunac713872017-01-09 16:55:13 +01001003
1004out:
1005 release_sock(sk);
1006out_err:
1007 return rc;
1008}
1009
1010static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1011{
Ursula Braun3163c502018-01-24 10:28:12 +01001012 struct socket *new_clcsock = NULL;
1013 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001014 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001015 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001016
Ursula Braun3163c502018-01-24 10:28:12 +01001017 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001018 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001019 if (!new_sk) {
1020 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001021 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001022 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001023 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001024 goto out;
1025 }
1026 *new_smc = smc_sk(new_sk);
1027
Myungho Jung78abe3d2018-12-18 09:02:25 -08001028 mutex_lock(&lsmc->clcsock_release_lock);
1029 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001030 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001031 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001032 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001033 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001034 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001035 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001036 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001037 if (new_clcsock)
1038 sock_release(new_clcsock);
1039 new_sk->sk_state = SMC_CLOSED;
1040 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001041 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001042 *new_smc = NULL;
1043 goto out;
1044 }
1045
Ursula Brauna60a2b12020-09-10 18:48:20 +02001046 /* new clcsock has inherited the smc listen-specific sk_data_ready
1047 * function; switch it back to the original sk_data_ready function
1048 */
1049 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001050 (*new_smc)->clcsock = new_clcsock;
1051out:
1052 return rc;
1053}
1054
Ursula Brauna046d572017-01-09 16:55:16 +01001055/* add a just created sock to the accept queue of the listen sock as
1056 * candidate for a following socket accept call from user space
1057 */
1058static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1059{
1060 struct smc_sock *par = smc_sk(parent);
1061
Ursula Braun51f1de72018-01-26 09:28:48 +01001062 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001063 spin_lock(&par->accept_q_lock);
1064 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1065 spin_unlock(&par->accept_q_lock);
1066 sk_acceptq_added(parent);
1067}
1068
1069/* remove a socket from the accept queue of its parental listening socket */
1070static void smc_accept_unlink(struct sock *sk)
1071{
1072 struct smc_sock *par = smc_sk(sk)->listen_smc;
1073
1074 spin_lock(&par->accept_q_lock);
1075 list_del_init(&smc_sk(sk)->accept_q);
1076 spin_unlock(&par->accept_q_lock);
1077 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001078 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001079}
1080
1081/* remove a sock from the accept queue to bind it to a new socket created
1082 * for a socket accept call from user space
1083 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001084struct sock *smc_accept_dequeue(struct sock *parent,
1085 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001086{
1087 struct smc_sock *isk, *n;
1088 struct sock *new_sk;
1089
1090 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1091 new_sk = (struct sock *)isk;
1092
1093 smc_accept_unlink(new_sk);
1094 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001095 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001096 if (isk->clcsock) {
1097 sock_release(isk->clcsock);
1098 isk->clcsock = NULL;
1099 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001100 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001101 continue;
1102 }
Ursula Braun07603b22019-04-11 11:17:32 +02001103 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001104 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001105 if (isk->use_fallback) {
1106 smc_sk(new_sk)->clcsock->file = new_sock->file;
1107 isk->clcsock->file->private_data = isk->clcsock;
1108 }
1109 }
Ursula Brauna046d572017-01-09 16:55:16 +01001110 return new_sk;
1111 }
1112 return NULL;
1113}
1114
1115/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001116void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001117{
1118 struct smc_sock *smc = smc_sk(sk);
1119
Ursula Braun81cf4f42019-10-21 16:13:15 +02001120 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001121 lock_sock(sk);
1122 if (!sk->sk_lingertime)
1123 /* wait for peer closing */
1124 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001125 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001126 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001127 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001128 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001129}
1130
Karsten Graulb9247542020-04-29 17:10:41 +02001131static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001132{
Karsten Graul387707f2020-04-29 17:10:40 +02001133 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001134 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001135 int rc;
1136
Karsten Graul7562a132020-05-01 12:48:01 +02001137 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001138 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001139
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001140 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001141 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001142 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001143 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001144
1145 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001146 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1147 SMC_LLC_CONFIRM_LINK);
1148 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001149 struct smc_clc_msg_decline dclc;
1150
1151 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001152 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001153 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001154 }
Karsten Graul649758f2020-05-04 14:18:48 +02001155 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001156 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1157 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1158 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001159 return SMC_CLC_DECL_RMBE_EC;
1160
Karsten Graul4667bb42020-04-30 15:55:42 +02001161 /* confirm_rkey is implicit on 1st contact */
1162 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001163
Karsten Graul00a049c2020-04-29 17:10:49 +02001164 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001165 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001166
Karsten Graul4667bb42020-04-30 15:55:42 +02001167 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001168 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001169 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001170}
1171
Hans Wippel3b2dec22018-05-18 09:34:18 +02001172/* listen worker: finish */
1173static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001174{
Ursula Brauna046d572017-01-09 16:55:16 +01001175 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001176 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001177
Ursula Brauna046d572017-01-09 16:55:16 +01001178 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001179 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001180 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001181 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001182 } else { /* no longer listening */
1183 smc_close_non_accepted(newsmcsk);
1184 }
Ursula Brauna046d572017-01-09 16:55:16 +01001185
1186 /* Wake up accept */
1187 lsmc->sk.sk_data_ready(&lsmc->sk);
1188 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001189}
Ursula Brauna046d572017-01-09 16:55:16 +01001190
Hans Wippel3b2dec22018-05-18 09:34:18 +02001191/* listen worker: finish in state connected */
1192static void smc_listen_out_connected(struct smc_sock *new_smc)
1193{
1194 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001195
Hans Wippel3b2dec22018-05-18 09:34:18 +02001196 sk_refcnt_debug_inc(newsmcsk);
1197 if (newsmcsk->sk_state == SMC_INIT)
1198 newsmcsk->sk_state = SMC_ACTIVE;
1199
1200 smc_listen_out(new_smc);
1201}
1202
1203/* listen worker: finish in error state */
1204static void smc_listen_out_err(struct smc_sock *new_smc)
1205{
1206 struct sock *newsmcsk = &new_smc->sk;
1207
Ursula Braun51f1de72018-01-26 09:28:48 +01001208 if (newsmcsk->sk_state == SMC_INIT)
1209 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001210 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001211
1212 smc_listen_out(new_smc);
1213}
1214
1215/* listen worker: decline and fall back if possible */
1216static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001217 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001218{
1219 /* RDMA setup failed, switch back to TCP */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001220 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001221 smc_lgr_cleanup_early(&new_smc->conn);
1222 else
1223 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001224 if (reason_code < 0) { /* error, no fallback possible */
1225 smc_listen_out_err(new_smc);
1226 return;
1227 }
Ursula Braun07603b22019-04-11 11:17:32 +02001228 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001229 new_smc->fallback_rsn = reason_code;
1230 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001231 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1232 smc_listen_out_err(new_smc);
1233 return;
1234 }
1235 }
1236 smc_listen_out_connected(new_smc);
1237}
1238
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001239/* listen worker: version checking */
1240static int smc_listen_v2_check(struct smc_sock *new_smc,
1241 struct smc_clc_msg_proposal *pclc,
1242 struct smc_init_info *ini)
1243{
1244 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1245 struct smc_clc_v2_extension *pclc_v2_ext;
1246
1247 ini->smc_type_v1 = pclc->hdr.typev1;
1248 ini->smc_type_v2 = pclc->hdr.typev2;
1249 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1250 if (pclc->hdr.version > SMC_V1)
1251 ini->smcd_version |=
1252 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1253 if (!smc_ism_v2_capable) {
1254 ini->smcd_version &= ~SMC_V2;
1255 goto out;
1256 }
1257 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1258 if (!pclc_v2_ext) {
1259 ini->smcd_version &= ~SMC_V2;
1260 goto out;
1261 }
1262 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1263 if (!pclc_smcd_v2_ext)
1264 ini->smcd_version &= ~SMC_V2;
1265
1266out:
1267 if (!ini->smcd_version) {
1268 if (pclc->hdr.typev1 == SMC_TYPE_B ||
1269 pclc->hdr.typev2 == SMC_TYPE_B)
1270 return SMC_CLC_DECL_NOSMCDEV;
1271 if (pclc->hdr.typev1 == SMC_TYPE_D ||
1272 pclc->hdr.typev2 == SMC_TYPE_D)
1273 return SMC_CLC_DECL_NOSMCDDEV;
1274 return SMC_CLC_DECL_NOSMCRDEV;
1275 }
1276
1277 return 0;
1278}
1279
Hans Wippel3b2dec22018-05-18 09:34:18 +02001280/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001281static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001282 struct smc_clc_msg_proposal *pclc)
1283{
1284 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1285 struct socket *newclcsock = new_smc->clcsock;
1286
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001287 if (pclc->hdr.typev1 == SMC_TYPE_N)
1288 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001289 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1290 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001291 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001292
1293 return 0;
1294}
1295
1296/* listen worker: initialize connection and buffers */
1297static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001298 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001299{
Karsten Graul7a627252019-04-12 12:57:30 +02001300 int rc;
1301
Hans Wippel3b2dec22018-05-18 09:34:18 +02001302 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001303 rc = smc_conn_create(new_smc, ini);
1304 if (rc)
1305 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001306
1307 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001308 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001309 return SMC_CLC_DECL_MEM;
1310
1311 return 0;
1312}
1313
Hans Wippel41349842018-06-28 19:05:11 +02001314/* listen worker: initialize connection and buffers for SMC-D */
1315static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001316 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001317{
Karsten Graul7a627252019-04-12 12:57:30 +02001318 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001319
Karsten Graul7a627252019-04-12 12:57:30 +02001320 rc = smc_conn_create(new_smc, ini);
1321 if (rc)
1322 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001323
Hans Wippel41349842018-06-28 19:05:11 +02001324 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001325 rc = smc_buf_create(new_smc, true);
1326 if (rc) {
Ursula Braun5ac54d82020-09-10 18:48:21 +02001327 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001328 smc_lgr_cleanup_early(&new_smc->conn);
1329 else
1330 smc_conn_free(&new_smc->conn);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001331 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1332 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001333 }
1334
1335 return 0;
1336}
1337
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001338static bool smc_is_already_selected(struct smcd_dev *smcd,
1339 struct smc_init_info *ini,
1340 int matches)
1341{
1342 int i;
1343
1344 for (i = 0; i < matches; i++)
1345 if (smcd == ini->ism_dev[i])
1346 return true;
1347
1348 return false;
1349}
1350
1351/* check for ISM devices matching proposed ISM devices */
1352static void smc_check_ism_v2_match(struct smc_init_info *ini,
1353 u16 proposed_chid, u64 proposed_gid,
1354 unsigned int *matches)
1355{
1356 struct smcd_dev *smcd;
1357
1358 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1359 if (smcd->going_away)
1360 continue;
1361 if (smc_is_already_selected(smcd, ini, *matches))
1362 continue;
1363 if (smc_ism_get_chid(smcd) == proposed_chid &&
1364 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1365 ini->ism_peer_gid[*matches] = proposed_gid;
1366 ini->ism_dev[*matches] = smcd;
1367 (*matches)++;
1368 break;
1369 }
1370 }
1371}
1372
1373static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1374 struct smc_clc_msg_proposal *pclc,
1375 struct smc_init_info *ini)
1376{
1377 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1378 struct smc_clc_v2_extension *smc_v2_ext;
1379 struct smc_clc_msg_smcd *pclc_smcd;
1380 unsigned int matches = 0;
1381 u8 *eid = NULL;
1382 int i;
1383
1384 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
1385 return;
1386
1387 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1388 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1389 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1390 if (!smcd_v2_ext ||
1391 !smc_v2_ext->hdr.flag.seid) /* no system EID support for SMCD */
1392 goto not_found;
1393
1394 mutex_lock(&smcd_dev_list.mutex);
1395 if (pclc_smcd->ism.chid)
1396 /* check for ISM device matching proposed native ISM device */
1397 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1398 ntohll(pclc_smcd->ism.gid), &matches);
1399 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1400 /* check for ISM devices matching proposed non-native ISM
1401 * devices
1402 */
1403 smc_check_ism_v2_match(ini,
1404 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1405 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1406 &matches);
1407 }
1408 mutex_unlock(&smcd_dev_list.mutex);
1409
1410 if (ini->ism_dev[0]) {
1411 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1412 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1413 goto not_found;
1414 } else {
1415 goto not_found;
1416 }
1417
1418 /* separate - outside the smcd_dev_list.lock */
1419 for (i = 0; i < matches; i++) {
1420 ini->smcd_version = SMC_V2;
1421 ini->is_smcd = true;
1422 ini->ism_selected = i;
1423 if (smc_listen_ism_init(new_smc, ini))
1424 /* try next active ISM device */
1425 continue;
1426 return; /* matching and usable V2 ISM device found */
1427 }
1428
1429not_found:
1430 ini->smcd_version &= ~SMC_V2;
1431 ini->ism_dev[0] = NULL;
1432 ini->is_smcd = false;
1433}
1434
1435static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1436 struct smc_clc_msg_proposal *pclc,
1437 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001438{
1439 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1440
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001441 /* check if ISM V1 is available */
1442 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001443 goto not_found;
1444 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001445 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Ursula Braun7affc802020-09-26 12:44:21 +02001446 if (smc_find_ism_device(new_smc, ini))
1447 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001448 ini->ism_selected = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001449 if (!smc_listen_ism_init(new_smc, ini))
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001450 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001451
1452not_found:
Ursula Braun3fc64932020-09-26 12:44:23 +02001453 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001454 ini->is_smcd = false;
1455}
1456
Hans Wippel3b2dec22018-05-18 09:34:18 +02001457/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001458static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001459{
Karsten Graulb9247542020-04-29 17:10:41 +02001460 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001461
Ursula Braun5ac54d82020-09-10 18:48:21 +02001462 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001463 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001464 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001465 }
1466 smc_rmb_sync_sg_for_device(&new_smc->conn);
1467
1468 return 0;
1469}
1470
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001471static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1472 struct smc_clc_msg_proposal *pclc,
1473 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001474{
1475 int rc;
1476
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001477 if (!smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001478 return SMC_CLC_DECL_NOSMCDEV;
1479
1480 /* prepare RDMA check */
1481 ini->ib_lcl = &pclc->lcl;
1482 rc = smc_find_rdma_device(new_smc, ini);
1483 if (rc) {
1484 /* no RDMA device found */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001485 if (ini->smc_type_v1 == SMC_TYPE_B)
Ursula Braun7affc802020-09-26 12:44:21 +02001486 /* neither ISM nor RDMA device found */
1487 rc = SMC_CLC_DECL_NOSMCDEV;
1488 return rc;
1489 }
1490 rc = smc_listen_rdma_init(new_smc, ini);
1491 if (rc)
1492 return rc;
1493 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1494}
1495
1496/* determine the local device matching to proposal */
1497static int smc_listen_find_device(struct smc_sock *new_smc,
1498 struct smc_clc_msg_proposal *pclc,
1499 struct smc_init_info *ini)
1500{
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001501 int rc;
1502
1503 /* check for ISM device matching V2 proposed device */
1504 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1505 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02001506 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001507
1508 if (!(ini->smcd_version & SMC_V1))
1509 return SMC_CLC_DECL_NOSMCDEV;
1510
1511 /* check for matching IP prefix and subnet length */
1512 rc = smc_listen_prfx_check(new_smc, pclc);
1513 if (rc)
1514 return rc;
1515
1516 /* get vlan id from IP device */
1517 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1518 return SMC_CLC_DECL_GETVLANERR;
1519
1520 /* check for ISM device matching V1 proposed device */
1521 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1522 if (ini->ism_dev[0])
1523 return 0;
1524
Ursula Braun7affc802020-09-26 12:44:21 +02001525 if (pclc->hdr.typev1 == SMC_TYPE_D)
1526 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1527
1528 /* check if RDMA is available */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001529 return smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001530}
1531
Hans Wippel3b2dec22018-05-18 09:34:18 +02001532/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001533static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1534 struct smc_clc_msg_accept_confirm *cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001535 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001536{
Karsten Graul387707f2020-04-29 17:10:40 +02001537 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001538 int reason_code = 0;
1539
Ursula Braun5ac54d82020-09-10 18:48:21 +02001540 if (local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001541 smc_link_save_peer_info(link, cclc);
1542
Ursula Braun0c881ad2020-09-10 18:48:25 +02001543 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1544 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001545
Ursula Braun5ac54d82020-09-10 18:48:21 +02001546 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001547 if (smc_ib_ready_link(link))
1548 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001549 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001550 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001551 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001552 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001553 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001554 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001555}
1556
Ursula Braun7affc802020-09-26 12:44:21 +02001557/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001558static void smc_listen_work(struct work_struct *work)
1559{
1560 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1561 smc_listen_work);
1562 struct socket *newclcsock = new_smc->clcsock;
1563 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001564 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001565 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001566 struct smc_init_info *ini = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001567 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001568
Karsten Graulfd577702019-04-11 11:17:30 +02001569 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1570 return smc_listen_out_err(new_smc);
1571
Hans Wippel3b2dec22018-05-18 09:34:18 +02001572 if (new_smc->use_fallback) {
1573 smc_listen_out_connected(new_smc);
1574 return;
1575 }
1576
1577 /* check if peer is smc capable */
1578 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001579 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001580 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001581 smc_listen_out_connected(new_smc);
1582 return;
1583 }
1584
1585 /* do inband token exchange -
1586 * wait for and receive SMC Proposal CLC message
1587 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001588 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1589 if (!buf) {
1590 rc = SMC_CLC_DECL_MEM;
1591 goto out_decl;
1592 }
1593 pclc = (struct smc_clc_msg_proposal *)buf;
1594 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001595 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001596 if (rc)
1597 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001598
1599 /* IPSec connections opt out of SMC-R optimizations */
1600 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001601 rc = SMC_CLC_DECL_IPSEC;
1602 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001603 }
1604
Ursula Braun3fc64932020-09-26 12:44:23 +02001605 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1606 if (!ini) {
1607 rc = SMC_CLC_DECL_MEM;
1608 goto out_decl;
1609 }
1610
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001611 /* initial version checking */
1612 rc = smc_listen_v2_check(new_smc, pclc, ini);
1613 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001614 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001615
Hans Wippel72a36a82019-02-07 15:56:18 +01001616 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001617 smc_close_init(new_smc);
1618 smc_rx_init(new_smc);
1619 smc_tx_init(new_smc);
1620
Ursula Braun7affc802020-09-26 12:44:21 +02001621 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02001622 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001623 if (rc)
1624 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001625
1626 /* send SMC Accept CLC message */
Ursula Braun3fc64932020-09-26 12:44:23 +02001627 rc = smc_clc_send_accept(new_smc, ini->first_contact_local);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001628 if (rc)
1629 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001630
Hans Wippel62c71392019-02-07 15:56:17 +01001631 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02001632 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01001633 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001634
Hans Wippel3b2dec22018-05-18 09:34:18 +02001635 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001636 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1637 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1638 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02001639 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001640 goto out_unlock;
1641 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001642 }
1643
1644 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02001645 if (!ini->is_smcd) {
Karsten Graul7a627252019-04-12 12:57:30 +02001646 rc = smc_listen_rdma_finish(new_smc, &cclc,
Ursula Braun3fc64932020-09-26 12:44:23 +02001647 ini->first_contact_local);
Hans Wippel62c71392019-02-07 15:56:17 +01001648 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02001649 goto out_unlock;
1650 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001651 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001652 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001653 smc_listen_out_connected(new_smc);
Ursula Braunac679362020-09-17 22:46:02 +02001654 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001655
1656out_unlock:
1657 mutex_unlock(&smc_server_lgr_pending);
1658out_decl:
Ursula Braun3fc64932020-09-26 12:44:23 +02001659 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0);
Ursula Braunac679362020-09-17 22:46:02 +02001660out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02001661 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02001662 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01001663}
1664
1665static void smc_tcp_listen_work(struct work_struct *work)
1666{
1667 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1668 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001669 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001670 struct smc_sock *new_smc;
1671 int rc = 0;
1672
Ursula Braun3163c502018-01-24 10:28:12 +01001673 lock_sock(lsk);
1674 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001675 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001676 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01001677 goto out;
1678 if (!new_smc)
1679 continue;
1680
1681 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001682 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001683 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001684 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001685 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1686 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001687 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1688 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001689 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02001690 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01001691 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001692 }
1693
1694out:
Ursula Braun3163c502018-01-24 10:28:12 +01001695 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001696 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1697}
1698
1699static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1700{
1701 struct smc_sock *lsmc;
1702
1703 lsmc = (struct smc_sock *)
1704 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1705 if (!lsmc)
1706 return;
1707 lsmc->clcsk_data_ready(listen_clcsock);
1708 if (lsmc->sk.sk_state == SMC_LISTEN) {
1709 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02001710 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02001711 sock_put(&lsmc->sk);
1712 }
Ursula Brauna046d572017-01-09 16:55:16 +01001713}
1714
Ursula Braunac713872017-01-09 16:55:13 +01001715static int smc_listen(struct socket *sock, int backlog)
1716{
1717 struct sock *sk = sock->sk;
1718 struct smc_sock *smc;
1719 int rc;
1720
1721 smc = smc_sk(sk);
1722 lock_sock(sk);
1723
1724 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001725 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1726 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001727 goto out;
1728
1729 rc = 0;
1730 if (sk->sk_state == SMC_LISTEN) {
1731 sk->sk_max_ack_backlog = backlog;
1732 goto out;
1733 }
1734 /* some socket options are handled in core, so we could not apply
1735 * them to the clc socket -- copy smc socket options to clc socket
1736 */
1737 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001738 if (!smc->use_fallback)
1739 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001740
Ursula Brauna60a2b12020-09-10 18:48:20 +02001741 /* save original sk_data_ready function and establish
1742 * smc-specific sk_data_ready function
1743 */
1744 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1745 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1746 smc->clcsock->sk->sk_user_data =
1747 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01001748 rc = kernel_listen(smc->clcsock, backlog);
1749 if (rc)
1750 goto out;
1751 sk->sk_max_ack_backlog = backlog;
1752 sk->sk_ack_backlog = 0;
1753 sk->sk_state = SMC_LISTEN;
1754
1755out:
1756 release_sock(sk);
1757 return rc;
1758}
1759
1760static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001761 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001762{
Ursula Brauna046d572017-01-09 16:55:16 +01001763 struct sock *sk = sock->sk, *nsk;
1764 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001765 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001766 long timeo;
1767 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001768
1769 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001770 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001771 lock_sock(sk);
1772
1773 if (lsmc->sk.sk_state != SMC_LISTEN) {
1774 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001775 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001776 goto out;
1777 }
1778
Ursula Brauna046d572017-01-09 16:55:16 +01001779 /* Wait for an incoming connection */
1780 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1781 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1782 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1783 set_current_state(TASK_INTERRUPTIBLE);
1784 if (!timeo) {
1785 rc = -EAGAIN;
1786 break;
1787 }
1788 release_sock(sk);
1789 timeo = schedule_timeout(timeo);
1790 /* wakeup by sk_data_ready in smc_listen_work() */
1791 sched_annotate_sleep();
1792 lock_sock(sk);
1793 if (signal_pending(current)) {
1794 rc = sock_intr_errno(timeo);
1795 break;
1796 }
1797 }
1798 set_current_state(TASK_RUNNING);
1799 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001800
Ursula Brauna046d572017-01-09 16:55:16 +01001801 if (!rc)
1802 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001803 release_sock(sk);
1804 if (rc)
1805 goto out;
1806
1807 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1808 /* wait till data arrives on the socket */
1809 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1810 MSEC_PER_SEC);
1811 if (smc_sk(nsk)->use_fallback) {
1812 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1813
1814 lock_sock(clcsk);
1815 if (skb_queue_empty(&clcsk->sk_receive_queue))
1816 sk_wait_data(clcsk, &timeo, NULL);
1817 release_sock(clcsk);
1818 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1819 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001820 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001821 release_sock(nsk);
1822 }
1823 }
Ursula Braunac713872017-01-09 16:55:13 +01001824
1825out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001826 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001827 return rc;
1828}
1829
1830static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001831 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001832{
1833 struct smc_sock *smc;
1834
Ursula Braunb38d7322017-01-09 16:55:25 +01001835 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1836 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001837 return -ENOTCONN;
1838
1839 smc = smc_sk(sock->sk);
1840
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001841 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001842}
1843
1844static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1845{
1846 struct sock *sk = sock->sk;
1847 struct smc_sock *smc;
1848 int rc = -EPIPE;
1849
1850 smc = smc_sk(sk);
1851 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001852 if ((sk->sk_state != SMC_ACTIVE) &&
1853 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1854 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001855 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001856
1857 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001858 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001859 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001860 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001861 } else {
1862 rc = -EINVAL;
1863 goto out;
1864 }
1865 }
1866
Ursula Braunac713872017-01-09 16:55:13 +01001867 if (smc->use_fallback)
1868 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1869 else
Ursula Braune6727f32017-01-09 16:55:23 +01001870 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001871out:
1872 release_sock(sk);
1873 return rc;
1874}
1875
1876static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1877 int flags)
1878{
1879 struct sock *sk = sock->sk;
1880 struct smc_sock *smc;
1881 int rc = -ENOTCONN;
1882
1883 smc = smc_sk(sk);
1884 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001885 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1886 /* socket was connected before, no more data to read */
1887 rc = 0;
1888 goto out;
1889 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001890 if ((sk->sk_state == SMC_INIT) ||
1891 (sk->sk_state == SMC_LISTEN) ||
1892 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001893 goto out;
1894
Ursula Braunb38d7322017-01-09 16:55:25 +01001895 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1896 rc = 0;
1897 goto out;
1898 }
1899
Stefan Raspl9014db22018-05-03 18:12:39 +02001900 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001901 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001902 } else {
1903 msg->msg_namelen = 0;
1904 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1905 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001906
Ursula Braunac713872017-01-09 16:55:13 +01001907out:
1908 release_sock(sk);
1909 return rc;
1910}
1911
Al Viroade994f2017-07-03 00:01:49 -04001912static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001913{
Ursula Braun8dce2782018-01-26 09:28:47 +01001914 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001915 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001916
Ursula Braun8dce2782018-01-26 09:28:47 +01001917 spin_lock(&isk->accept_q_lock);
1918 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001919 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001920 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001921
Ursula Braun8dce2782018-01-26 09:28:47 +01001922 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001923}
1924
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001925static __poll_t smc_poll(struct file *file, struct socket *sock,
1926 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001927{
1928 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001929 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001930 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001931
Ursula Braun8dce2782018-01-26 09:28:47 +01001932 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001933 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001934
Ursula Braunac713872017-01-09 16:55:13 +01001935 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001936 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001937 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001938 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001939 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001940 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001941 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001942 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001943 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001944 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001945 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1946 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001947 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001948 if (sk->sk_state == SMC_LISTEN) {
1949 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001950 mask |= smc_accept_poll(sk);
1951 } else if (smc->use_fallback) { /* as result of connect_work()*/
1952 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1953 wait);
1954 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001955 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001956 if ((sk->sk_state != SMC_INIT &&
1957 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001958 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001959 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001960 } else {
1961 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1962 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1963 }
1964 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001965 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001966 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001967 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001968 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001969 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001970 if (smc->conn.urg_state == SMC_URG_VALID)
1971 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001972 }
Ursula Braunac713872017-01-09 16:55:13 +01001973 }
1974
1975 return mask;
1976}
1977
1978static int smc_shutdown(struct socket *sock, int how)
1979{
1980 struct sock *sk = sock->sk;
1981 struct smc_sock *smc;
1982 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001983 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001984
1985 smc = smc_sk(sk);
1986
1987 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001988 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001989
1990 lock_sock(sk);
1991
1992 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001993 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001994 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1995 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1996 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1997 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1998 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001999 goto out;
2000 if (smc->use_fallback) {
2001 rc = kernel_sock_shutdown(smc->clcsock, how);
2002 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2003 if (sk->sk_shutdown == SHUTDOWN_MASK)
2004 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002005 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002006 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002007 switch (how) {
2008 case SHUT_RDWR: /* shutdown in both directions */
2009 rc = smc_close_active(smc);
2010 break;
2011 case SHUT_WR:
2012 rc = smc_close_shutdown_write(smc);
2013 break;
2014 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002015 rc = 0;
2016 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002017 break;
2018 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02002019 if (smc->clcsock)
2020 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002021 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2022 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002023
2024out:
2025 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002026 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002027}
2028
2029static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002030 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002031{
2032 struct sock *sk = sock->sk;
2033 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002034 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002035
2036 smc = smc_sk(sk);
2037
2038 /* generic setsockopts reaching us here always apply to the
2039 * CLC socket
2040 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002041 if (unlikely(!smc->clcsock->ops->setsockopt))
2042 rc = -EOPNOTSUPP;
2043 else
2044 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2045 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002046 if (smc->clcsock->sk->sk_err) {
2047 sk->sk_err = smc->clcsock->sk->sk_err;
2048 sk->sk_error_report(sk);
2049 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002050
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002051 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002052 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002053 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002054 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002055
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002056 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002057 if (rc || smc->use_fallback)
2058 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002059 switch (optname) {
2060 case TCP_ULP:
2061 case TCP_FASTOPEN:
2062 case TCP_FASTOPEN_CONNECT:
2063 case TCP_FASTOPEN_KEY:
2064 case TCP_FASTOPEN_NO_COOKIE:
2065 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002066 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02002067 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02002068 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002069 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002070 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002071 }
2072 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002073 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002074 if (sk->sk_state != SMC_INIT &&
2075 sk->sk_state != SMC_LISTEN &&
2076 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002077 if (val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002078 mod_delayed_work(smc->conn.lgr->tx_wq,
2079 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002080 }
2081 break;
2082 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002083 if (sk->sk_state != SMC_INIT &&
2084 sk->sk_state != SMC_LISTEN &&
2085 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002086 if (!val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002087 mod_delayed_work(smc->conn.lgr->tx_wq,
2088 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002089 }
2090 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002091 case TCP_DEFER_ACCEPT:
2092 smc->sockopt_defer_accept = val;
2093 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002094 default:
2095 break;
2096 }
Ursula Braun86434742019-12-12 22:35:58 +01002097out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002098 release_sock(sk);
2099
2100 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002101}
2102
2103static int smc_getsockopt(struct socket *sock, int level, int optname,
2104 char __user *optval, int __user *optlen)
2105{
2106 struct smc_sock *smc;
2107
2108 smc = smc_sk(sock->sk);
2109 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002110 if (unlikely(!smc->clcsock->ops->getsockopt))
2111 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01002112 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2113 optval, optlen);
2114}
2115
2116static int smc_ioctl(struct socket *sock, unsigned int cmd,
2117 unsigned long arg)
2118{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002119 union smc_host_cursor cons, urg;
2120 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002121 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002122 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002123
2124 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002125 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002126 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002127 if (smc->use_fallback) {
2128 if (!smc->clcsock) {
2129 release_sock(&smc->sk);
2130 return -EBADF;
2131 }
2132 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2133 release_sock(&smc->sk);
2134 return answ;
2135 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002136 switch (cmd) {
2137 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002138 if (smc->sk.sk_state == SMC_LISTEN) {
2139 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002140 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002141 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002142 if (smc->sk.sk_state == SMC_INIT ||
2143 smc->sk.sk_state == SMC_CLOSED)
2144 answ = 0;
2145 else
2146 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002147 break;
2148 case SIOCOUTQ:
2149 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002150 if (smc->sk.sk_state == SMC_LISTEN) {
2151 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002152 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002153 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002154 if (smc->sk.sk_state == SMC_INIT ||
2155 smc->sk.sk_state == SMC_CLOSED)
2156 answ = 0;
2157 else
2158 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002159 atomic_read(&smc->conn.sndbuf_space);
2160 break;
2161 case SIOCOUTQNSD:
2162 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002163 if (smc->sk.sk_state == SMC_LISTEN) {
2164 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002165 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002166 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002167 if (smc->sk.sk_state == SMC_INIT ||
2168 smc->sk.sk_state == SMC_CLOSED)
2169 answ = 0;
2170 else
2171 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002172 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002173 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002174 if (smc->sk.sk_state == SMC_LISTEN) {
2175 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002176 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002177 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002178 if (smc->sk.sk_state == SMC_INIT ||
2179 smc->sk.sk_state == SMC_CLOSED) {
2180 answ = 0;
2181 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002182 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2183 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002184 answ = smc_curs_diff(conn->rmb_desc->len,
2185 &cons, &urg) == 1;
2186 }
2187 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002188 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002189 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002190 return -ENOIOCTLCMD;
2191 }
Ursula Braun1992d992018-07-16 13:56:52 +02002192 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002193
2194 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002195}
2196
2197static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2198 int offset, size_t size, int flags)
2199{
2200 struct sock *sk = sock->sk;
2201 struct smc_sock *smc;
2202 int rc = -EPIPE;
2203
2204 smc = smc_sk(sk);
2205 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002206 if (sk->sk_state != SMC_ACTIVE) {
2207 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002208 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002209 }
2210 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002211 if (smc->use_fallback)
2212 rc = kernel_sendpage(smc->clcsock, page, offset,
2213 size, flags);
2214 else
2215 rc = sock_no_sendpage(sock, page, offset, size, flags);
2216
2217out:
Ursula Braunac713872017-01-09 16:55:13 +01002218 return rc;
2219}
2220
Stefan Raspl9014db22018-05-03 18:12:39 +02002221/* Map the affected portions of the rmbe into an spd, note the number of bytes
2222 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2223 * updates till whenever a respective page has been fully processed.
2224 * Note that subsequent recv() calls have to wait till all splice() processing
2225 * completed.
2226 */
Ursula Braunac713872017-01-09 16:55:13 +01002227static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2228 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002229 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002230{
2231 struct sock *sk = sock->sk;
2232 struct smc_sock *smc;
2233 int rc = -ENOTCONN;
2234
2235 smc = smc_sk(sk);
2236 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002237 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2238 /* socket was connected before, no more data to read */
2239 rc = 0;
2240 goto out;
2241 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002242 if (sk->sk_state == SMC_INIT ||
2243 sk->sk_state == SMC_LISTEN ||
2244 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002245 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002246
2247 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2248 rc = 0;
2249 goto out;
2250 }
2251
Ursula Braunac713872017-01-09 16:55:13 +01002252 if (smc->use_fallback) {
2253 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2254 pipe, len, flags);
2255 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002256 if (*ppos) {
2257 rc = -ESPIPE;
2258 goto out;
2259 }
2260 if (flags & SPLICE_F_NONBLOCK)
2261 flags = MSG_DONTWAIT;
2262 else
2263 flags = 0;
2264 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002265 }
2266out:
2267 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002268
Ursula Braunac713872017-01-09 16:55:13 +01002269 return rc;
2270}
2271
2272/* must look like tcp */
2273static const struct proto_ops smc_sock_ops = {
2274 .family = PF_SMC,
2275 .owner = THIS_MODULE,
2276 .release = smc_release,
2277 .bind = smc_bind,
2278 .connect = smc_connect,
2279 .socketpair = sock_no_socketpair,
2280 .accept = smc_accept,
2281 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002282 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002283 .ioctl = smc_ioctl,
2284 .listen = smc_listen,
2285 .shutdown = smc_shutdown,
2286 .setsockopt = smc_setsockopt,
2287 .getsockopt = smc_getsockopt,
2288 .sendmsg = smc_sendmsg,
2289 .recvmsg = smc_recvmsg,
2290 .mmap = sock_no_mmap,
2291 .sendpage = smc_sendpage,
2292 .splice_read = smc_splice_read,
2293};
2294
2295static int smc_create(struct net *net, struct socket *sock, int protocol,
2296 int kern)
2297{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002298 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002299 struct smc_sock *smc;
2300 struct sock *sk;
2301 int rc;
2302
2303 rc = -ESOCKTNOSUPPORT;
2304 if (sock->type != SOCK_STREAM)
2305 goto out;
2306
2307 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002308 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002309 goto out;
2310
2311 rc = -ENOBUFS;
2312 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002313 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002314 if (!sk)
2315 goto out;
2316
2317 /* create internal TCP socket for CLC handshake and fallback */
2318 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002319 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002320 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002321 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2322 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002323 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002324 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002325 goto out;
2326 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002327 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2328 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002329
2330out:
2331 return rc;
2332}
2333
2334static const struct net_proto_family smc_sock_family_ops = {
2335 .family = PF_SMC,
2336 .owner = THIS_MODULE,
2337 .create = smc_create,
2338};
2339
Hans Wippel64e28b52019-02-21 13:01:02 +01002340unsigned int smc_net_id;
2341
2342static __net_init int smc_net_init(struct net *net)
2343{
2344 return smc_pnet_net_init(net);
2345}
2346
2347static void __net_exit smc_net_exit(struct net *net)
2348{
2349 smc_pnet_net_exit(net);
2350}
2351
2352static struct pernet_operations smc_net_ops = {
2353 .init = smc_net_init,
2354 .exit = smc_net_exit,
2355 .id = &smc_net_id,
2356 .size = sizeof(struct smc_net),
2357};
2358
Ursula Braunac713872017-01-09 16:55:13 +01002359static int __init smc_init(void)
2360{
2361 int rc;
2362
Hans Wippel64e28b52019-02-21 13:01:02 +01002363 rc = register_pernet_subsys(&smc_net_ops);
2364 if (rc)
2365 return rc;
2366
Ursula Braun201091e2020-09-26 12:44:24 +02002367 smc_ism_init();
2368
Thomas Richter6812baa2017-01-09 16:55:15 +01002369 rc = smc_pnet_init();
2370 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002371 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002372
Karsten Graul22ef4732020-09-10 18:48:29 +02002373 rc = -ENOMEM;
2374 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2375 if (!smc_hs_wq)
2376 goto out_pnet;
2377
2378 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2379 if (!smc_close_wq)
2380 goto out_alloc_hs_wq;
2381
Ursula Braun6dabd402019-11-16 17:47:29 +01002382 rc = smc_core_init();
2383 if (rc) {
2384 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Karsten Graul22ef4732020-09-10 18:48:29 +02002385 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002386 }
2387
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002388 rc = smc_llc_init();
2389 if (rc) {
2390 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002391 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002392 }
2393
Ursula Braun5f083182017-01-09 16:55:22 +01002394 rc = smc_cdc_init();
2395 if (rc) {
2396 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002397 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002398 }
2399
Ursula Braunac713872017-01-09 16:55:13 +01002400 rc = proto_register(&smc_proto, 1);
2401 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002402 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002403 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002404 }
2405
Karsten Graulaaa4d332018-03-16 15:06:41 +01002406 rc = proto_register(&smc_proto6, 1);
2407 if (rc) {
2408 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2409 goto out_proto;
2410 }
2411
Ursula Braunac713872017-01-09 16:55:13 +01002412 rc = sock_register(&smc_sock_family_ops);
2413 if (rc) {
2414 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002415 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002416 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002417 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002418 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002419
Ursula Brauna4cf0442017-01-09 16:55:14 +01002420 rc = smc_ib_register_client();
2421 if (rc) {
2422 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2423 goto out_sock;
2424 }
2425
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002426 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002427 return 0;
2428
Ursula Brauna4cf0442017-01-09 16:55:14 +01002429out_sock:
2430 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002431out_proto6:
2432 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002433out_proto:
2434 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002435out_core:
2436 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002437out_alloc_wqs:
2438 destroy_workqueue(smc_close_wq);
2439out_alloc_hs_wq:
2440 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002441out_pnet:
2442 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002443out_pernet_subsys:
2444 unregister_pernet_subsys(&smc_net_ops);
2445
Ursula Braunac713872017-01-09 16:55:13 +01002446 return rc;
2447}
2448
2449static void __exit smc_exit(void)
2450{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002451 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002452 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002453 smc_core_exit();
2454 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002455 destroy_workqueue(smc_close_wq);
2456 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002457 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002458 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002459 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002460 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002461 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002462}
2463
2464module_init(smc_init);
2465module_exit(smc_exit);
2466
2467MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2468MODULE_DESCRIPTION("smc socket address family");
2469MODULE_LICENSE("GPL");
2470MODULE_ALIAS_NETPROTO(PF_SMC);