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