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