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