net/smc: call consolidation
[sfrench/cifs-2.6.git] / net / smc / af_smc.c
1 /*
2  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  *  AF_SMC protocol family socket handler keeping the AF_INET sock address type
5  *  applies to SOCK_STREAM sockets only
6  *  offers an alternative communication option for TCP-protocol sockets
7  *  applicable with RoCE-cards only
8  *
9  *  Initial restrictions:
10  *    - support for alternate links postponed
11  *    - partial support for non-blocking sockets only
12  *    - support for urgent data postponed
13  *
14  *  Copyright IBM Corp. 2016, 2018
15  *
16  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
17  *              based on prototype from Frank Blaschka
18  */
19
20 #define KMSG_COMPONENT "smc"
21 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/socket.h>
25 #include <linux/workqueue.h>
26 #include <linux/in.h>
27 #include <linux/sched/signal.h>
28
29 #include <net/sock.h>
30 #include <net/tcp.h>
31 #include <net/smc.h>
32
33 #include "smc.h"
34 #include "smc_clc.h"
35 #include "smc_llc.h"
36 #include "smc_cdc.h"
37 #include "smc_core.h"
38 #include "smc_ib.h"
39 #include "smc_pnet.h"
40 #include "smc_tx.h"
41 #include "smc_rx.h"
42 #include "smc_close.h"
43
44 static DEFINE_MUTEX(smc_create_lgr_pending);    /* serialize link group
45                                                  * creation
46                                                  */
47
48 struct smc_lgr_list smc_lgr_list = {            /* established link groups */
49         .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
50         .list = LIST_HEAD_INIT(smc_lgr_list.list),
51 };
52
53 static void smc_tcp_listen_work(struct work_struct *);
54
55 static void smc_set_keepalive(struct sock *sk, int val)
56 {
57         struct smc_sock *smc = smc_sk(sk);
58
59         smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
60 }
61
62 static struct smc_hashinfo smc_v4_hashinfo = {
63         .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
64 };
65
66 static struct smc_hashinfo smc_v6_hashinfo = {
67         .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
68 };
69
70 int smc_hash_sk(struct sock *sk)
71 {
72         struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
73         struct hlist_head *head;
74
75         head = &h->ht;
76
77         write_lock_bh(&h->lock);
78         sk_add_node(sk, head);
79         sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
80         write_unlock_bh(&h->lock);
81
82         return 0;
83 }
84 EXPORT_SYMBOL_GPL(smc_hash_sk);
85
86 void smc_unhash_sk(struct sock *sk)
87 {
88         struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
89
90         write_lock_bh(&h->lock);
91         if (sk_del_node_init(sk))
92                 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
93         write_unlock_bh(&h->lock);
94 }
95 EXPORT_SYMBOL_GPL(smc_unhash_sk);
96
97 struct proto smc_proto = {
98         .name           = "SMC",
99         .owner          = THIS_MODULE,
100         .keepalive      = smc_set_keepalive,
101         .hash           = smc_hash_sk,
102         .unhash         = smc_unhash_sk,
103         .obj_size       = sizeof(struct smc_sock),
104         .h.smc_hash     = &smc_v4_hashinfo,
105         .slab_flags     = SLAB_TYPESAFE_BY_RCU,
106 };
107 EXPORT_SYMBOL_GPL(smc_proto);
108
109 struct proto smc_proto6 = {
110         .name           = "SMC6",
111         .owner          = THIS_MODULE,
112         .keepalive      = smc_set_keepalive,
113         .hash           = smc_hash_sk,
114         .unhash         = smc_unhash_sk,
115         .obj_size       = sizeof(struct smc_sock),
116         .h.smc_hash     = &smc_v6_hashinfo,
117         .slab_flags     = SLAB_TYPESAFE_BY_RCU,
118 };
119 EXPORT_SYMBOL_GPL(smc_proto6);
120
121 static int smc_release(struct socket *sock)
122 {
123         struct sock *sk = sock->sk;
124         struct smc_sock *smc;
125         int rc = 0;
126
127         if (!sk)
128                 goto out;
129
130         smc = smc_sk(sk);
131         if (sk->sk_state == SMC_LISTEN)
132                 /* smc_close_non_accepted() is called and acquires
133                  * sock lock for child sockets again
134                  */
135                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
136         else
137                 lock_sock(sk);
138
139         if (!smc->use_fallback) {
140                 rc = smc_close_active(smc);
141                 sock_set_flag(sk, SOCK_DEAD);
142                 sk->sk_shutdown |= SHUTDOWN_MASK;
143         }
144         if (smc->clcsock) {
145                 sock_release(smc->clcsock);
146                 smc->clcsock = NULL;
147         }
148         if (smc->use_fallback) {
149                 sock_put(sk); /* passive closing */
150                 sk->sk_state = SMC_CLOSED;
151                 sk->sk_state_change(sk);
152         }
153
154         /* detach socket */
155         sock_orphan(sk);
156         sock->sk = NULL;
157         if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
158                 smc_conn_free(&smc->conn);
159         release_sock(sk);
160
161         sk->sk_prot->unhash(sk);
162         sock_put(sk); /* final sock_put */
163 out:
164         return rc;
165 }
166
167 static void smc_destruct(struct sock *sk)
168 {
169         if (sk->sk_state != SMC_CLOSED)
170                 return;
171         if (!sock_flag(sk, SOCK_DEAD))
172                 return;
173
174         sk_refcnt_debug_dec(sk);
175 }
176
177 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
178                                    int protocol)
179 {
180         struct smc_sock *smc;
181         struct proto *prot;
182         struct sock *sk;
183
184         prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
185         sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
186         if (!sk)
187                 return NULL;
188
189         sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
190         sk->sk_state = SMC_INIT;
191         sk->sk_destruct = smc_destruct;
192         sk->sk_protocol = protocol;
193         smc = smc_sk(sk);
194         INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
195         INIT_LIST_HEAD(&smc->accept_q);
196         spin_lock_init(&smc->accept_q_lock);
197         sk->sk_prot->hash(sk);
198         sk_refcnt_debug_inc(sk);
199
200         return sk;
201 }
202
203 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
204                     int addr_len)
205 {
206         struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
207         struct sock *sk = sock->sk;
208         struct smc_sock *smc;
209         int rc;
210
211         smc = smc_sk(sk);
212
213         /* replicate tests from inet_bind(), to be safe wrt. future changes */
214         rc = -EINVAL;
215         if (addr_len < sizeof(struct sockaddr_in))
216                 goto out;
217
218         rc = -EAFNOSUPPORT;
219         if (addr->sin_family != AF_INET &&
220             addr->sin_family != AF_INET6 &&
221             addr->sin_family != AF_UNSPEC)
222                 goto out;
223         /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
224         if (addr->sin_family == AF_UNSPEC &&
225             addr->sin_addr.s_addr != htonl(INADDR_ANY))
226                 goto out;
227
228         lock_sock(sk);
229
230         /* Check if socket is already active */
231         rc = -EINVAL;
232         if (sk->sk_state != SMC_INIT)
233                 goto out_rel;
234
235         smc->clcsock->sk->sk_reuse = sk->sk_reuse;
236         rc = kernel_bind(smc->clcsock, uaddr, addr_len);
237
238 out_rel:
239         release_sock(sk);
240 out:
241         return rc;
242 }
243
244 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
245                                    unsigned long mask)
246 {
247         /* options we don't get control via setsockopt for */
248         nsk->sk_type = osk->sk_type;
249         nsk->sk_sndbuf = osk->sk_sndbuf;
250         nsk->sk_rcvbuf = osk->sk_rcvbuf;
251         nsk->sk_sndtimeo = osk->sk_sndtimeo;
252         nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
253         nsk->sk_mark = osk->sk_mark;
254         nsk->sk_priority = osk->sk_priority;
255         nsk->sk_rcvlowat = osk->sk_rcvlowat;
256         nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
257         nsk->sk_err = osk->sk_err;
258
259         nsk->sk_flags &= ~mask;
260         nsk->sk_flags |= osk->sk_flags & mask;
261 }
262
263 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
264                              (1UL << SOCK_KEEPOPEN) | \
265                              (1UL << SOCK_LINGER) | \
266                              (1UL << SOCK_BROADCAST) | \
267                              (1UL << SOCK_TIMESTAMP) | \
268                              (1UL << SOCK_DBG) | \
269                              (1UL << SOCK_RCVTSTAMP) | \
270                              (1UL << SOCK_RCVTSTAMPNS) | \
271                              (1UL << SOCK_LOCALROUTE) | \
272                              (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
273                              (1UL << SOCK_RXQ_OVFL) | \
274                              (1UL << SOCK_WIFI_STATUS) | \
275                              (1UL << SOCK_NOFCS) | \
276                              (1UL << SOCK_FILTER_LOCKED))
277 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
278  * clc socket (since smc is not called for these options from net/core)
279  */
280 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
281 {
282         smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
283 }
284
285 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
286                              (1UL << SOCK_KEEPOPEN) | \
287                              (1UL << SOCK_LINGER) | \
288                              (1UL << SOCK_DBG))
289 /* copy only settings and flags relevant for smc from clc to smc socket */
290 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
291 {
292         smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
293 }
294
295 /* register a new rmb */
296 static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
297 {
298         /* register memory region for new rmb */
299         if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK]))
300                 return -EFAULT;
301         return 0;
302 }
303
304 static int smc_clnt_conf_first_link(struct smc_sock *smc)
305 {
306         struct smc_link_group *lgr = smc->conn.lgr;
307         struct smc_link *link;
308         int rest;
309         int rc;
310
311         link = &lgr->lnk[SMC_SINGLE_LINK];
312         /* receive CONFIRM LINK request from server over RoCE fabric */
313         rest = wait_for_completion_interruptible_timeout(
314                 &link->llc_confirm,
315                 SMC_LLC_WAIT_FIRST_TIME);
316         if (rest <= 0) {
317                 struct smc_clc_msg_decline dclc;
318
319                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
320                                       SMC_CLC_DECLINE);
321                 return rc;
322         }
323
324         if (link->llc_confirm_rc)
325                 return SMC_CLC_DECL_RMBE_EC;
326
327         rc = smc_ib_modify_qp_rts(link);
328         if (rc)
329                 return SMC_CLC_DECL_INTERR;
330
331         smc_wr_remember_qp_attr(link);
332
333         if (smc_reg_rmb(link, smc->conn.rmb_desc))
334                 return SMC_CLC_DECL_INTERR;
335
336         /* send CONFIRM LINK response over RoCE fabric */
337         rc = smc_llc_send_confirm_link(link,
338                                        link->smcibdev->mac[link->ibport - 1],
339                                        &link->smcibdev->gid[link->ibport - 1],
340                                        SMC_LLC_RESP);
341         if (rc < 0)
342                 return SMC_CLC_DECL_TCL;
343
344         /* receive ADD LINK request from server over RoCE fabric */
345         rest = wait_for_completion_interruptible_timeout(&link->llc_add,
346                                                          SMC_LLC_WAIT_TIME);
347         if (rest <= 0) {
348                 struct smc_clc_msg_decline dclc;
349
350                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
351                                       SMC_CLC_DECLINE);
352                 return rc;
353         }
354
355         /* send add link reject message, only one link supported for now */
356         rc = smc_llc_send_add_link(link,
357                                    link->smcibdev->mac[link->ibport - 1],
358                                    &link->smcibdev->gid[link->ibport - 1],
359                                    SMC_LLC_RESP);
360         if (rc < 0)
361                 return SMC_CLC_DECL_TCL;
362
363         link->state = SMC_LNK_ACTIVE;
364
365         return 0;
366 }
367
368 static void smc_conn_save_peer_info(struct smc_sock *smc,
369                                     struct smc_clc_msg_accept_confirm *clc)
370 {
371         smc->conn.peer_conn_idx = clc->conn_idx;
372         smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
373         smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
374         atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
375 }
376
377 static void smc_link_save_peer_info(struct smc_link *link,
378                                     struct smc_clc_msg_accept_confirm *clc)
379 {
380         link->peer_qpn = ntoh24(clc->qpn);
381         memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
382         memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
383         link->peer_psn = ntoh24(clc->psn);
384         link->peer_mtu = clc->qp_mtu;
385 }
386
387 /* setup for RDMA connection of client */
388 static int smc_connect_rdma(struct smc_sock *smc)
389 {
390         struct smc_clc_msg_accept_confirm aclc;
391         int local_contact = SMC_FIRST_CONTACT;
392         struct smc_ib_device *smcibdev;
393         struct smc_link *link;
394         u8 srv_first_contact;
395         int reason_code = 0;
396         int rc = 0;
397         u8 ibport;
398
399         sock_hold(&smc->sk); /* sock put in passive closing */
400
401         if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
402                 /* peer has not signalled SMC-capability */
403                 smc->use_fallback = true;
404                 goto out_connected;
405         }
406
407         /* IPSec connections opt out of SMC-R optimizations */
408         if (using_ipsec(smc)) {
409                 reason_code = SMC_CLC_DECL_IPSEC;
410                 goto decline_rdma;
411         }
412
413         /* PNET table look up: search active ib_device and port
414          * within same PNETID that also contains the ethernet device
415          * used for the internal TCP socket
416          */
417         smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
418         if (!smcibdev) {
419                 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
420                 goto decline_rdma;
421         }
422
423         /* do inband token exchange */
424         reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
425         if (reason_code < 0) {
426                 rc = reason_code;
427                 goto out_err;
428         }
429         if (reason_code > 0) /* configuration error */
430                 goto decline_rdma;
431         /* receive SMC Accept CLC message */
432         reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
433                                        SMC_CLC_ACCEPT);
434         if (reason_code < 0) {
435                 rc = reason_code;
436                 goto out_err;
437         }
438         if (reason_code > 0)
439                 goto decline_rdma;
440
441         srv_first_contact = aclc.hdr.flag;
442         mutex_lock(&smc_create_lgr_pending);
443         local_contact = smc_conn_create(smc, smcibdev, ibport, &aclc.lcl,
444                                         srv_first_contact);
445         if (local_contact < 0) {
446                 rc = local_contact;
447                 if (rc == -ENOMEM)
448                         reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
449                 else if (rc == -ENOLINK)
450                         reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
451                 goto decline_rdma_unlock;
452         }
453         link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
454
455         smc_conn_save_peer_info(smc, &aclc);
456
457         /* create send buffer and rmb */
458         rc = smc_buf_create(smc);
459         if (rc) {
460                 reason_code = SMC_CLC_DECL_MEM;
461                 goto decline_rdma_unlock;
462         }
463
464         if (local_contact == SMC_FIRST_CONTACT)
465                 smc_link_save_peer_info(link, &aclc);
466
467         rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
468         if (rc) {
469                 reason_code = SMC_CLC_DECL_INTERR;
470                 goto decline_rdma_unlock;
471         }
472
473         smc_close_init(smc);
474         smc_rx_init(smc);
475
476         if (local_contact == SMC_FIRST_CONTACT) {
477                 rc = smc_ib_ready_link(link);
478                 if (rc) {
479                         reason_code = SMC_CLC_DECL_INTERR;
480                         goto decline_rdma_unlock;
481                 }
482         } else {
483                 if (!smc->conn.rmb_desc->reused) {
484                         if (smc_reg_rmb(link, smc->conn.rmb_desc)) {
485                                 reason_code = SMC_CLC_DECL_INTERR;
486                                 goto decline_rdma_unlock;
487                         }
488                 }
489         }
490         smc_rmb_sync_sg_for_device(&smc->conn);
491
492         rc = smc_clc_send_confirm(smc);
493         if (rc)
494                 goto out_err_unlock;
495
496         if (local_contact == SMC_FIRST_CONTACT) {
497                 /* QP confirmation over RoCE fabric */
498                 reason_code = smc_clnt_conf_first_link(smc);
499                 if (reason_code < 0) {
500                         rc = reason_code;
501                         goto out_err_unlock;
502                 }
503                 if (reason_code > 0)
504                         goto decline_rdma_unlock;
505         }
506
507         mutex_unlock(&smc_create_lgr_pending);
508         smc_tx_init(smc);
509
510 out_connected:
511         smc_copy_sock_settings_to_clc(smc);
512         if (smc->sk.sk_state == SMC_INIT)
513                 smc->sk.sk_state = SMC_ACTIVE;
514
515         return rc ? rc : local_contact;
516
517 decline_rdma_unlock:
518         if (local_contact == SMC_FIRST_CONTACT)
519                 smc_lgr_forget(smc->conn.lgr);
520         mutex_unlock(&smc_create_lgr_pending);
521         smc_conn_free(&smc->conn);
522 decline_rdma:
523         /* RDMA setup failed, switch back to TCP */
524         smc->use_fallback = true;
525         if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
526                 rc = smc_clc_send_decline(smc, reason_code);
527                 if (rc < 0)
528                         goto out_err;
529         }
530         goto out_connected;
531
532 out_err_unlock:
533         if (local_contact == SMC_FIRST_CONTACT)
534                 smc_lgr_forget(smc->conn.lgr);
535         mutex_unlock(&smc_create_lgr_pending);
536         smc_conn_free(&smc->conn);
537 out_err:
538         if (smc->sk.sk_state == SMC_INIT)
539                 sock_put(&smc->sk); /* passive closing */
540         return rc;
541 }
542
543 static int smc_connect(struct socket *sock, struct sockaddr *addr,
544                        int alen, int flags)
545 {
546         struct sock *sk = sock->sk;
547         struct smc_sock *smc;
548         int rc = -EINVAL;
549
550         smc = smc_sk(sk);
551
552         /* separate smc parameter checking to be safe */
553         if (alen < sizeof(addr->sa_family))
554                 goto out_err;
555         if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
556                 goto out_err;
557
558         lock_sock(sk);
559         switch (sk->sk_state) {
560         default:
561                 goto out;
562         case SMC_ACTIVE:
563                 rc = -EISCONN;
564                 goto out;
565         case SMC_INIT:
566                 rc = 0;
567                 break;
568         }
569
570         smc_copy_sock_settings_to_clc(smc);
571         tcp_sk(smc->clcsock->sk)->syn_smc = 1;
572         rc = kernel_connect(smc->clcsock, addr, alen, flags);
573         if (rc)
574                 goto out;
575
576         /* setup RDMA connection */
577         rc = smc_connect_rdma(smc);
578         if (rc < 0)
579                 goto out;
580         else
581                 rc = 0; /* success cases including fallback */
582
583 out:
584         release_sock(sk);
585 out_err:
586         return rc;
587 }
588
589 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
590 {
591         struct socket *new_clcsock = NULL;
592         struct sock *lsk = &lsmc->sk;
593         struct sock *new_sk;
594         int rc;
595
596         release_sock(lsk);
597         new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
598         if (!new_sk) {
599                 rc = -ENOMEM;
600                 lsk->sk_err = ENOMEM;
601                 *new_smc = NULL;
602                 lock_sock(lsk);
603                 goto out;
604         }
605         *new_smc = smc_sk(new_sk);
606
607         rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
608         lock_sock(lsk);
609         if  (rc < 0)
610                 lsk->sk_err = -rc;
611         if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
612                 if (new_clcsock)
613                         sock_release(new_clcsock);
614                 new_sk->sk_state = SMC_CLOSED;
615                 sock_set_flag(new_sk, SOCK_DEAD);
616                 new_sk->sk_prot->unhash(new_sk);
617                 sock_put(new_sk); /* final */
618                 *new_smc = NULL;
619                 goto out;
620         }
621
622         (*new_smc)->clcsock = new_clcsock;
623 out:
624         return rc;
625 }
626
627 /* add a just created sock to the accept queue of the listen sock as
628  * candidate for a following socket accept call from user space
629  */
630 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
631 {
632         struct smc_sock *par = smc_sk(parent);
633
634         sock_hold(sk); /* sock_put in smc_accept_unlink () */
635         spin_lock(&par->accept_q_lock);
636         list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
637         spin_unlock(&par->accept_q_lock);
638         sk_acceptq_added(parent);
639 }
640
641 /* remove a socket from the accept queue of its parental listening socket */
642 static void smc_accept_unlink(struct sock *sk)
643 {
644         struct smc_sock *par = smc_sk(sk)->listen_smc;
645
646         spin_lock(&par->accept_q_lock);
647         list_del_init(&smc_sk(sk)->accept_q);
648         spin_unlock(&par->accept_q_lock);
649         sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
650         sock_put(sk); /* sock_hold in smc_accept_enqueue */
651 }
652
653 /* remove a sock from the accept queue to bind it to a new socket created
654  * for a socket accept call from user space
655  */
656 struct sock *smc_accept_dequeue(struct sock *parent,
657                                 struct socket *new_sock)
658 {
659         struct smc_sock *isk, *n;
660         struct sock *new_sk;
661
662         list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
663                 new_sk = (struct sock *)isk;
664
665                 smc_accept_unlink(new_sk);
666                 if (new_sk->sk_state == SMC_CLOSED) {
667                         if (isk->clcsock) {
668                                 sock_release(isk->clcsock);
669                                 isk->clcsock = NULL;
670                         }
671                         new_sk->sk_prot->unhash(new_sk);
672                         sock_put(new_sk); /* final */
673                         continue;
674                 }
675                 if (new_sock)
676                         sock_graft(new_sk, new_sock);
677                 return new_sk;
678         }
679         return NULL;
680 }
681
682 /* clean up for a created but never accepted sock */
683 void smc_close_non_accepted(struct sock *sk)
684 {
685         struct smc_sock *smc = smc_sk(sk);
686
687         lock_sock(sk);
688         if (!sk->sk_lingertime)
689                 /* wait for peer closing */
690                 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
691         if (!smc->use_fallback) {
692                 smc_close_active(smc);
693                 sock_set_flag(sk, SOCK_DEAD);
694                 sk->sk_shutdown |= SHUTDOWN_MASK;
695         }
696         if (smc->clcsock) {
697                 struct socket *tcp;
698
699                 tcp = smc->clcsock;
700                 smc->clcsock = NULL;
701                 sock_release(tcp);
702         }
703         if (smc->use_fallback) {
704                 sock_put(sk); /* passive closing */
705                 sk->sk_state = SMC_CLOSED;
706         } else {
707                 if (sk->sk_state == SMC_CLOSED)
708                         smc_conn_free(&smc->conn);
709         }
710         release_sock(sk);
711         sk->sk_prot->unhash(sk);
712         sock_put(sk); /* final sock_put */
713 }
714
715 static int smc_serv_conf_first_link(struct smc_sock *smc)
716 {
717         struct smc_link_group *lgr = smc->conn.lgr;
718         struct smc_link *link;
719         int rest;
720         int rc;
721
722         link = &lgr->lnk[SMC_SINGLE_LINK];
723
724         if (smc_reg_rmb(link, smc->conn.rmb_desc))
725                 return SMC_CLC_DECL_INTERR;
726
727         /* send CONFIRM LINK request to client over the RoCE fabric */
728         rc = smc_llc_send_confirm_link(link,
729                                        link->smcibdev->mac[link->ibport - 1],
730                                        &link->smcibdev->gid[link->ibport - 1],
731                                        SMC_LLC_REQ);
732         if (rc < 0)
733                 return SMC_CLC_DECL_TCL;
734
735         /* receive CONFIRM LINK response from client over the RoCE fabric */
736         rest = wait_for_completion_interruptible_timeout(
737                 &link->llc_confirm_resp,
738                 SMC_LLC_WAIT_FIRST_TIME);
739         if (rest <= 0) {
740                 struct smc_clc_msg_decline dclc;
741
742                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
743                                       SMC_CLC_DECLINE);
744                 return rc;
745         }
746
747         if (link->llc_confirm_resp_rc)
748                 return SMC_CLC_DECL_RMBE_EC;
749
750         /* send ADD LINK request to client over the RoCE fabric */
751         rc = smc_llc_send_add_link(link,
752                                    link->smcibdev->mac[link->ibport - 1],
753                                    &link->smcibdev->gid[link->ibport - 1],
754                                    SMC_LLC_REQ);
755         if (rc < 0)
756                 return SMC_CLC_DECL_TCL;
757
758         /* receive ADD LINK response from client over the RoCE fabric */
759         rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
760                                                          SMC_LLC_WAIT_TIME);
761         if (rest <= 0) {
762                 struct smc_clc_msg_decline dclc;
763
764                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
765                                       SMC_CLC_DECLINE);
766                 return rc;
767         }
768
769         link->state = SMC_LNK_ACTIVE;
770
771         return 0;
772 }
773
774 /* setup for RDMA connection of server */
775 static void smc_listen_work(struct work_struct *work)
776 {
777         struct smc_sock *new_smc = container_of(work, struct smc_sock,
778                                                 smc_listen_work);
779         struct smc_clc_msg_proposal_prefix *pclc_prfx;
780         struct socket *newclcsock = new_smc->clcsock;
781         struct smc_sock *lsmc = new_smc->listen_smc;
782         struct smc_clc_msg_accept_confirm cclc;
783         int local_contact = SMC_REUSE_CONTACT;
784         struct sock *newsmcsk = &new_smc->sk;
785         struct smc_clc_msg_proposal *pclc;
786         struct smc_ib_device *smcibdev;
787         u8 buf[SMC_CLC_MAX_LEN];
788         struct smc_link *link;
789         int reason_code = 0;
790         int rc = 0;
791         u8 ibport;
792
793         /* check if peer is smc capable */
794         if (!tcp_sk(newclcsock->sk)->syn_smc) {
795                 new_smc->use_fallback = true;
796                 goto out_connected;
797         }
798
799         /* do inband token exchange -
800          *wait for and receive SMC Proposal CLC message
801          */
802         reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
803                                        SMC_CLC_PROPOSAL);
804         if (reason_code < 0)
805                 goto out_err;
806         if (reason_code > 0)
807                 goto decline_rdma;
808
809         /* IPSec connections opt out of SMC-R optimizations */
810         if (using_ipsec(new_smc)) {
811                 reason_code = SMC_CLC_DECL_IPSEC;
812                 goto decline_rdma;
813         }
814
815         /* PNET table look up: search active ib_device and port
816          * within same PNETID that also contains the ethernet device
817          * used for the internal TCP socket
818          */
819         smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
820         if (!smcibdev) {
821                 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
822                 goto decline_rdma;
823         }
824
825         pclc = (struct smc_clc_msg_proposal *)&buf;
826         pclc_prfx = smc_clc_proposal_get_prefix(pclc);
827
828         rc = smc_clc_prfx_match(newclcsock, pclc_prfx);
829         if (rc) {
830                 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
831                 goto decline_rdma;
832         }
833
834         /* allocate connection / link group */
835         mutex_lock(&smc_create_lgr_pending);
836         local_contact = smc_conn_create(new_smc, smcibdev, ibport, &pclc->lcl,
837                                         0);
838         if (local_contact < 0) {
839                 rc = local_contact;
840                 if (rc == -ENOMEM)
841                         reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
842                 goto decline_rdma_unlock;
843         }
844         link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
845
846         /* create send buffer and rmb */
847         rc = smc_buf_create(new_smc);
848         if (rc) {
849                 reason_code = SMC_CLC_DECL_MEM;
850                 goto decline_rdma_unlock;
851         }
852
853         smc_close_init(new_smc);
854         smc_rx_init(new_smc);
855
856         if (local_contact != SMC_FIRST_CONTACT) {
857                 if (!new_smc->conn.rmb_desc->reused) {
858                         if (smc_reg_rmb(link, new_smc->conn.rmb_desc)) {
859                                 reason_code = SMC_CLC_DECL_INTERR;
860                                 goto decline_rdma_unlock;
861                         }
862                 }
863         }
864         smc_rmb_sync_sg_for_device(&new_smc->conn);
865
866         rc = smc_clc_send_accept(new_smc, local_contact);
867         if (rc)
868                 goto out_err_unlock;
869
870         /* receive SMC Confirm CLC message */
871         reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
872                                        SMC_CLC_CONFIRM);
873         if (reason_code < 0)
874                 goto out_err_unlock;
875         if (reason_code > 0)
876                 goto decline_rdma_unlock;
877         smc_conn_save_peer_info(new_smc, &cclc);
878         if (local_contact == SMC_FIRST_CONTACT)
879                 smc_link_save_peer_info(link, &cclc);
880
881         rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
882         if (rc) {
883                 reason_code = SMC_CLC_DECL_INTERR;
884                 goto decline_rdma_unlock;
885         }
886
887         if (local_contact == SMC_FIRST_CONTACT) {
888                 rc = smc_ib_ready_link(link);
889                 if (rc) {
890                         reason_code = SMC_CLC_DECL_INTERR;
891                         goto decline_rdma_unlock;
892                 }
893                 /* QP confirmation over RoCE fabric */
894                 reason_code = smc_serv_conf_first_link(new_smc);
895                 if (reason_code < 0)
896                         /* peer is not aware of a problem */
897                         goto out_err_unlock;
898                 if (reason_code > 0)
899                         goto decline_rdma_unlock;
900         }
901
902         smc_tx_init(new_smc);
903         mutex_unlock(&smc_create_lgr_pending);
904
905 out_connected:
906         sk_refcnt_debug_inc(newsmcsk);
907         if (newsmcsk->sk_state == SMC_INIT)
908                 newsmcsk->sk_state = SMC_ACTIVE;
909 enqueue:
910         lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
911         if (lsmc->sk.sk_state == SMC_LISTEN) {
912                 smc_accept_enqueue(&lsmc->sk, newsmcsk);
913         } else { /* no longer listening */
914                 smc_close_non_accepted(newsmcsk);
915         }
916         release_sock(&lsmc->sk);
917
918         /* Wake up accept */
919         lsmc->sk.sk_data_ready(&lsmc->sk);
920         sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
921         return;
922
923 decline_rdma_unlock:
924         if (local_contact == SMC_FIRST_CONTACT)
925                 smc_lgr_forget(new_smc->conn.lgr);
926         mutex_unlock(&smc_create_lgr_pending);
927 decline_rdma:
928         /* RDMA setup failed, switch back to TCP */
929         smc_conn_free(&new_smc->conn);
930         new_smc->use_fallback = true;
931         if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
932                 if (smc_clc_send_decline(new_smc, reason_code) < 0)
933                         goto out_err;
934         }
935         goto out_connected;
936
937 out_err_unlock:
938         if (local_contact == SMC_FIRST_CONTACT)
939                 smc_lgr_forget(new_smc->conn.lgr);
940         mutex_unlock(&smc_create_lgr_pending);
941 out_err:
942         if (newsmcsk->sk_state == SMC_INIT)
943                 sock_put(&new_smc->sk); /* passive closing */
944         newsmcsk->sk_state = SMC_CLOSED;
945         smc_conn_free(&new_smc->conn);
946         goto enqueue; /* queue new sock with sk_err set */
947 }
948
949 static void smc_tcp_listen_work(struct work_struct *work)
950 {
951         struct smc_sock *lsmc = container_of(work, struct smc_sock,
952                                              tcp_listen_work);
953         struct sock *lsk = &lsmc->sk;
954         struct smc_sock *new_smc;
955         int rc = 0;
956
957         lock_sock(lsk);
958         while (lsk->sk_state == SMC_LISTEN) {
959                 rc = smc_clcsock_accept(lsmc, &new_smc);
960                 if (rc)
961                         goto out;
962                 if (!new_smc)
963                         continue;
964
965                 new_smc->listen_smc = lsmc;
966                 new_smc->use_fallback = false; /* assume rdma capability first*/
967                 sock_hold(lsk); /* sock_put in smc_listen_work */
968                 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
969                 smc_copy_sock_settings_to_smc(new_smc);
970                 sock_hold(&new_smc->sk); /* sock_put in passive closing */
971                 if (!schedule_work(&new_smc->smc_listen_work))
972                         sock_put(&new_smc->sk);
973         }
974
975 out:
976         release_sock(lsk);
977         sock_put(&lsmc->sk); /* sock_hold in smc_listen */
978 }
979
980 static int smc_listen(struct socket *sock, int backlog)
981 {
982         struct sock *sk = sock->sk;
983         struct smc_sock *smc;
984         int rc;
985
986         smc = smc_sk(sk);
987         lock_sock(sk);
988
989         rc = -EINVAL;
990         if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
991                 goto out;
992
993         rc = 0;
994         if (sk->sk_state == SMC_LISTEN) {
995                 sk->sk_max_ack_backlog = backlog;
996                 goto out;
997         }
998         /* some socket options are handled in core, so we could not apply
999          * them to the clc socket -- copy smc socket options to clc socket
1000          */
1001         smc_copy_sock_settings_to_clc(smc);
1002         tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1003
1004         rc = kernel_listen(smc->clcsock, backlog);
1005         if (rc)
1006                 goto out;
1007         sk->sk_max_ack_backlog = backlog;
1008         sk->sk_ack_backlog = 0;
1009         sk->sk_state = SMC_LISTEN;
1010         INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
1011         sock_hold(sk); /* sock_hold in tcp_listen_worker */
1012         if (!schedule_work(&smc->tcp_listen_work))
1013                 sock_put(sk);
1014
1015 out:
1016         release_sock(sk);
1017         return rc;
1018 }
1019
1020 static int smc_accept(struct socket *sock, struct socket *new_sock,
1021                       int flags, bool kern)
1022 {
1023         struct sock *sk = sock->sk, *nsk;
1024         DECLARE_WAITQUEUE(wait, current);
1025         struct smc_sock *lsmc;
1026         long timeo;
1027         int rc = 0;
1028
1029         lsmc = smc_sk(sk);
1030         sock_hold(sk); /* sock_put below */
1031         lock_sock(sk);
1032
1033         if (lsmc->sk.sk_state != SMC_LISTEN) {
1034                 rc = -EINVAL;
1035                 goto out;
1036         }
1037
1038         /* Wait for an incoming connection */
1039         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1040         add_wait_queue_exclusive(sk_sleep(sk), &wait);
1041         while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1042                 set_current_state(TASK_INTERRUPTIBLE);
1043                 if (!timeo) {
1044                         rc = -EAGAIN;
1045                         break;
1046                 }
1047                 release_sock(sk);
1048                 timeo = schedule_timeout(timeo);
1049                 /* wakeup by sk_data_ready in smc_listen_work() */
1050                 sched_annotate_sleep();
1051                 lock_sock(sk);
1052                 if (signal_pending(current)) {
1053                         rc = sock_intr_errno(timeo);
1054                         break;
1055                 }
1056         }
1057         set_current_state(TASK_RUNNING);
1058         remove_wait_queue(sk_sleep(sk), &wait);
1059
1060         if (!rc)
1061                 rc = sock_error(nsk);
1062
1063 out:
1064         release_sock(sk);
1065         sock_put(sk); /* sock_hold above */
1066         return rc;
1067 }
1068
1069 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1070                        int peer)
1071 {
1072         struct smc_sock *smc;
1073
1074         if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1075             (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1076                 return -ENOTCONN;
1077
1078         smc = smc_sk(sock->sk);
1079
1080         return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
1081 }
1082
1083 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1084 {
1085         struct sock *sk = sock->sk;
1086         struct smc_sock *smc;
1087         int rc = -EPIPE;
1088
1089         smc = smc_sk(sk);
1090         lock_sock(sk);
1091         if ((sk->sk_state != SMC_ACTIVE) &&
1092             (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1093             (sk->sk_state != SMC_INIT))
1094                 goto out;
1095         if (smc->use_fallback)
1096                 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1097         else
1098                 rc = smc_tx_sendmsg(smc, msg, len);
1099 out:
1100         release_sock(sk);
1101         return rc;
1102 }
1103
1104 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1105                        int flags)
1106 {
1107         struct sock *sk = sock->sk;
1108         struct smc_sock *smc;
1109         int rc = -ENOTCONN;
1110
1111         smc = smc_sk(sk);
1112         lock_sock(sk);
1113         if ((sk->sk_state == SMC_INIT) ||
1114             (sk->sk_state == SMC_LISTEN) ||
1115             (sk->sk_state == SMC_CLOSED))
1116                 goto out;
1117
1118         if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1119                 rc = 0;
1120                 goto out;
1121         }
1122
1123         if (smc->use_fallback)
1124                 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1125         else
1126                 rc = smc_rx_recvmsg(smc, msg, len, flags);
1127
1128 out:
1129         release_sock(sk);
1130         return rc;
1131 }
1132
1133 static __poll_t smc_accept_poll(struct sock *parent)
1134 {
1135         struct smc_sock *isk = smc_sk(parent);
1136         __poll_t mask = 0;
1137
1138         spin_lock(&isk->accept_q_lock);
1139         if (!list_empty(&isk->accept_q))
1140                 mask = EPOLLIN | EPOLLRDNORM;
1141         spin_unlock(&isk->accept_q_lock);
1142
1143         return mask;
1144 }
1145
1146 static __poll_t smc_poll(struct file *file, struct socket *sock,
1147                              poll_table *wait)
1148 {
1149         struct sock *sk = sock->sk;
1150         __poll_t mask = 0;
1151         struct smc_sock *smc;
1152         int rc;
1153
1154         if (!sk)
1155                 return EPOLLNVAL;
1156
1157         smc = smc_sk(sock->sk);
1158         sock_hold(sk);
1159         lock_sock(sk);
1160         if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1161                 /* delegate to CLC child sock */
1162                 release_sock(sk);
1163                 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1164                 lock_sock(sk);
1165                 sk->sk_err = smc->clcsock->sk->sk_err;
1166                 if (sk->sk_err) {
1167                         mask |= EPOLLERR;
1168                 } else {
1169                         /* if non-blocking connect finished ... */
1170                         if (sk->sk_state == SMC_INIT &&
1171                             mask & EPOLLOUT &&
1172                             smc->clcsock->sk->sk_state != TCP_CLOSE) {
1173                                 rc = smc_connect_rdma(smc);
1174                                 if (rc < 0)
1175                                         mask |= EPOLLERR;
1176                                 /* success cases including fallback */
1177                                 mask |= EPOLLOUT | EPOLLWRNORM;
1178                         }
1179                 }
1180         } else {
1181                 if (sk->sk_state != SMC_CLOSED) {
1182                         release_sock(sk);
1183                         sock_poll_wait(file, sk_sleep(sk), wait);
1184                         lock_sock(sk);
1185                 }
1186                 if (sk->sk_err)
1187                         mask |= EPOLLERR;
1188                 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1189                     (sk->sk_state == SMC_CLOSED))
1190                         mask |= EPOLLHUP;
1191                 if (sk->sk_state == SMC_LISTEN) {
1192                         /* woken up by sk_data_ready in smc_listen_work() */
1193                         mask = smc_accept_poll(sk);
1194                 } else {
1195                         if (atomic_read(&smc->conn.sndbuf_space) ||
1196                             sk->sk_shutdown & SEND_SHUTDOWN) {
1197                                 mask |= EPOLLOUT | EPOLLWRNORM;
1198                         } else {
1199                                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1200                                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1201                         }
1202                         if (atomic_read(&smc->conn.bytes_to_rcv))
1203                                 mask |= EPOLLIN | EPOLLRDNORM;
1204                         if (sk->sk_shutdown & RCV_SHUTDOWN)
1205                                 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1206                         if (sk->sk_state == SMC_APPCLOSEWAIT1)
1207                                 mask |= EPOLLIN;
1208                 }
1209
1210         }
1211         release_sock(sk);
1212         sock_put(sk);
1213
1214         return mask;
1215 }
1216
1217 static int smc_shutdown(struct socket *sock, int how)
1218 {
1219         struct sock *sk = sock->sk;
1220         struct smc_sock *smc;
1221         int rc = -EINVAL;
1222         int rc1 = 0;
1223
1224         smc = smc_sk(sk);
1225
1226         if ((how < SHUT_RD) || (how > SHUT_RDWR))
1227                 return rc;
1228
1229         lock_sock(sk);
1230
1231         rc = -ENOTCONN;
1232         if ((sk->sk_state != SMC_LISTEN) &&
1233             (sk->sk_state != SMC_ACTIVE) &&
1234             (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1235             (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1236             (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1237             (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1238             (sk->sk_state != SMC_APPFINCLOSEWAIT))
1239                 goto out;
1240         if (smc->use_fallback) {
1241                 rc = kernel_sock_shutdown(smc->clcsock, how);
1242                 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1243                 if (sk->sk_shutdown == SHUTDOWN_MASK)
1244                         sk->sk_state = SMC_CLOSED;
1245                 goto out;
1246         }
1247         switch (how) {
1248         case SHUT_RDWR:         /* shutdown in both directions */
1249                 rc = smc_close_active(smc);
1250                 break;
1251         case SHUT_WR:
1252                 rc = smc_close_shutdown_write(smc);
1253                 break;
1254         case SHUT_RD:
1255                 rc = 0;
1256                 /* nothing more to do because peer is not involved */
1257                 break;
1258         }
1259         if (smc->clcsock)
1260                 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1261         /* map sock_shutdown_cmd constants to sk_shutdown value range */
1262         sk->sk_shutdown |= how + 1;
1263
1264 out:
1265         release_sock(sk);
1266         return rc ? rc : rc1;
1267 }
1268
1269 static int smc_setsockopt(struct socket *sock, int level, int optname,
1270                           char __user *optval, unsigned int optlen)
1271 {
1272         struct sock *sk = sock->sk;
1273         struct smc_sock *smc;
1274
1275         smc = smc_sk(sk);
1276
1277         /* generic setsockopts reaching us here always apply to the
1278          * CLC socket
1279          */
1280         return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1281                                              optval, optlen);
1282 }
1283
1284 static int smc_getsockopt(struct socket *sock, int level, int optname,
1285                           char __user *optval, int __user *optlen)
1286 {
1287         struct smc_sock *smc;
1288
1289         smc = smc_sk(sock->sk);
1290         /* socket options apply to the CLC socket */
1291         return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1292                                              optval, optlen);
1293 }
1294
1295 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1296                      unsigned long arg)
1297 {
1298         struct smc_sock *smc;
1299
1300         smc = smc_sk(sock->sk);
1301         if (smc->use_fallback)
1302                 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1303         else
1304                 return sock_no_ioctl(sock, cmd, arg);
1305 }
1306
1307 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1308                             int offset, size_t size, int flags)
1309 {
1310         struct sock *sk = sock->sk;
1311         struct smc_sock *smc;
1312         int rc = -EPIPE;
1313
1314         smc = smc_sk(sk);
1315         lock_sock(sk);
1316         if (sk->sk_state != SMC_ACTIVE)
1317                 goto out;
1318         if (smc->use_fallback)
1319                 rc = kernel_sendpage(smc->clcsock, page, offset,
1320                                      size, flags);
1321         else
1322                 rc = sock_no_sendpage(sock, page, offset, size, flags);
1323
1324 out:
1325         release_sock(sk);
1326         return rc;
1327 }
1328
1329 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1330                                struct pipe_inode_info *pipe, size_t len,
1331                                     unsigned int flags)
1332 {
1333         struct sock *sk = sock->sk;
1334         struct smc_sock *smc;
1335         int rc = -ENOTCONN;
1336
1337         smc = smc_sk(sk);
1338         lock_sock(sk);
1339         if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1340                 goto out;
1341         if (smc->use_fallback) {
1342                 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1343                                                     pipe, len, flags);
1344         } else {
1345                 rc = -EOPNOTSUPP;
1346         }
1347 out:
1348         release_sock(sk);
1349         return rc;
1350 }
1351
1352 /* must look like tcp */
1353 static const struct proto_ops smc_sock_ops = {
1354         .family         = PF_SMC,
1355         .owner          = THIS_MODULE,
1356         .release        = smc_release,
1357         .bind           = smc_bind,
1358         .connect        = smc_connect,
1359         .socketpair     = sock_no_socketpair,
1360         .accept         = smc_accept,
1361         .getname        = smc_getname,
1362         .poll           = smc_poll,
1363         .ioctl          = smc_ioctl,
1364         .listen         = smc_listen,
1365         .shutdown       = smc_shutdown,
1366         .setsockopt     = smc_setsockopt,
1367         .getsockopt     = smc_getsockopt,
1368         .sendmsg        = smc_sendmsg,
1369         .recvmsg        = smc_recvmsg,
1370         .mmap           = sock_no_mmap,
1371         .sendpage       = smc_sendpage,
1372         .splice_read    = smc_splice_read,
1373 };
1374
1375 static int smc_create(struct net *net, struct socket *sock, int protocol,
1376                       int kern)
1377 {
1378         int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
1379         struct smc_sock *smc;
1380         struct sock *sk;
1381         int rc;
1382
1383         rc = -ESOCKTNOSUPPORT;
1384         if (sock->type != SOCK_STREAM)
1385                 goto out;
1386
1387         rc = -EPROTONOSUPPORT;
1388         if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
1389                 goto out;
1390
1391         rc = -ENOBUFS;
1392         sock->ops = &smc_sock_ops;
1393         sk = smc_sock_alloc(net, sock, protocol);
1394         if (!sk)
1395                 goto out;
1396
1397         /* create internal TCP socket for CLC handshake and fallback */
1398         smc = smc_sk(sk);
1399         smc->use_fallback = false; /* assume rdma capability first */
1400         rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1401                               &smc->clcsock);
1402         if (rc) {
1403                 sk_common_release(sk);
1404                 goto out;
1405         }
1406         smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1407         smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1408
1409 out:
1410         return rc;
1411 }
1412
1413 static const struct net_proto_family smc_sock_family_ops = {
1414         .family = PF_SMC,
1415         .owner  = THIS_MODULE,
1416         .create = smc_create,
1417 };
1418
1419 static int __init smc_init(void)
1420 {
1421         int rc;
1422
1423         rc = smc_pnet_init();
1424         if (rc)
1425                 return rc;
1426
1427         rc = smc_llc_init();
1428         if (rc) {
1429                 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1430                 goto out_pnet;
1431         }
1432
1433         rc = smc_cdc_init();
1434         if (rc) {
1435                 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1436                 goto out_pnet;
1437         }
1438
1439         rc = proto_register(&smc_proto, 1);
1440         if (rc) {
1441                 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
1442                 goto out_pnet;
1443         }
1444
1445         rc = proto_register(&smc_proto6, 1);
1446         if (rc) {
1447                 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1448                 goto out_proto;
1449         }
1450
1451         rc = sock_register(&smc_sock_family_ops);
1452         if (rc) {
1453                 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1454                 goto out_proto6;
1455         }
1456         INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
1457         INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
1458
1459         rc = smc_ib_register_client();
1460         if (rc) {
1461                 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1462                 goto out_sock;
1463         }
1464
1465         static_branch_enable(&tcp_have_smc);
1466         return 0;
1467
1468 out_sock:
1469         sock_unregister(PF_SMC);
1470 out_proto6:
1471         proto_unregister(&smc_proto6);
1472 out_proto:
1473         proto_unregister(&smc_proto);
1474 out_pnet:
1475         smc_pnet_exit();
1476         return rc;
1477 }
1478
1479 static void __exit smc_exit(void)
1480 {
1481         struct smc_link_group *lgr, *lg;
1482         LIST_HEAD(lgr_freeing_list);
1483
1484         spin_lock_bh(&smc_lgr_list.lock);
1485         if (!list_empty(&smc_lgr_list.list))
1486                 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1487         spin_unlock_bh(&smc_lgr_list.lock);
1488         list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1489                 list_del_init(&lgr->list);
1490                 cancel_delayed_work_sync(&lgr->free_work);
1491                 smc_lgr_free(lgr); /* free link group */
1492         }
1493         static_branch_disable(&tcp_have_smc);
1494         smc_ib_unregister_client();
1495         sock_unregister(PF_SMC);
1496         proto_unregister(&smc_proto6);
1497         proto_unregister(&smc_proto);
1498         smc_pnet_exit();
1499 }
1500
1501 module_init(smc_init);
1502 module_exit(smc_exit);
1503
1504 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1505 MODULE_DESCRIPTION("smc socket address family");
1506 MODULE_LICENSE("GPL");
1507 MODULE_ALIAS_NETPROTO(PF_SMC);