root/net/smc/af_smc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. smc_set_keepalive
  2. smc_hash_sk
  3. smc_unhash_sk
  4. smc_restore_fallback_changes
  5. __smc_release
  6. smc_release
  7. smc_destruct
  8. smc_sock_alloc
  9. smc_bind
  10. smc_copy_sock_settings
  11. smc_copy_sock_settings_to_clc
  12. smc_copy_sock_settings_to_smc
  13. smc_reg_rmb
  14. smc_clnt_conf_first_link
  15. smcr_conn_save_peer_info
  16. smcd_conn_save_peer_info
  17. smc_conn_save_peer_info
  18. smc_link_save_peer_info
  19. smc_switch_to_fallback
  20. smc_connect_fallback
  21. smc_connect_decline_fallback
  22. smc_connect_abort
  23. smc_find_rdma_device
  24. smc_find_ism_device
  25. smc_connect_ism_vlan_setup
  26. smc_connect_ism_vlan_cleanup
  27. smc_connect_clc
  28. smc_connect_rdma
  29. smc_connect_ism
  30. __smc_connect
  31. smc_connect_work
  32. smc_connect
  33. smc_clcsock_accept
  34. smc_accept_enqueue
  35. smc_accept_unlink
  36. smc_accept_dequeue
  37. smc_close_non_accepted
  38. smc_serv_conf_first_link
  39. smc_listen_out
  40. smc_listen_out_connected
  41. smc_listen_out_err
  42. smc_listen_decline
  43. smc_listen_prfx_check
  44. smc_listen_rdma_init
  45. smc_listen_ism_init
  46. smc_listen_rdma_reg
  47. smc_listen_rdma_finish
  48. smc_listen_work
  49. smc_tcp_listen_work
  50. smc_listen
  51. smc_accept
  52. smc_getname
  53. smc_sendmsg
  54. smc_recvmsg
  55. smc_accept_poll
  56. smc_poll
  57. smc_shutdown
  58. smc_setsockopt
  59. smc_getsockopt
  60. smc_ioctl
  61. smc_sendpage
  62. smc_splice_read
  63. smc_create
  64. smc_net_init
  65. smc_net_exit
  66. smc_init
  67. smc_exit

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
   4  *
   5  *  AF_SMC protocol family socket handler keeping the AF_INET sock address type
   6  *  applies to SOCK_STREAM sockets only
   7  *  offers an alternative communication option for TCP-protocol sockets
   8  *  applicable with RoCE-cards only
   9  *
  10  *  Initial restrictions:
  11  *    - support for alternate links postponed
  12  *
  13  *  Copyright IBM Corp. 2016, 2018
  14  *
  15  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
  16  *              based on prototype from Frank Blaschka
  17  */
  18 
  19 #define KMSG_COMPONENT "smc"
  20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  21 
  22 #include <linux/module.h>
  23 #include <linux/socket.h>
  24 #include <linux/workqueue.h>
  25 #include <linux/in.h>
  26 #include <linux/sched/signal.h>
  27 #include <linux/if_vlan.h>
  28 
  29 #include <net/sock.h>
  30 #include <net/tcp.h>
  31 #include <net/smc.h>
  32 #include <asm/ioctls.h>
  33 
  34 #include <net/net_namespace.h>
  35 #include <net/netns/generic.h>
  36 #include "smc_netns.h"
  37 
  38 #include "smc.h"
  39 #include "smc_clc.h"
  40 #include "smc_llc.h"
  41 #include "smc_cdc.h"
  42 #include "smc_core.h"
  43 #include "smc_ib.h"
  44 #include "smc_ism.h"
  45 #include "smc_pnet.h"
  46 #include "smc_tx.h"
  47 #include "smc_rx.h"
  48 #include "smc_close.h"
  49 
  50 static DEFINE_MUTEX(smc_server_lgr_pending);    /* serialize link group
  51                                                  * creation on server
  52                                                  */
  53 static DEFINE_MUTEX(smc_client_lgr_pending);    /* serialize link group
  54                                                  * creation on client
  55                                                  */
  56 
  57 static void smc_tcp_listen_work(struct work_struct *);
  58 static void smc_connect_work(struct work_struct *);
  59 
  60 static void smc_set_keepalive(struct sock *sk, int val)
  61 {
  62         struct smc_sock *smc = smc_sk(sk);
  63 
  64         smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
  65 }
  66 
  67 static struct smc_hashinfo smc_v4_hashinfo = {
  68         .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
  69 };
  70 
  71 static struct smc_hashinfo smc_v6_hashinfo = {
  72         .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
  73 };
  74 
  75 int smc_hash_sk(struct sock *sk)
  76 {
  77         struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
  78         struct hlist_head *head;
  79 
  80         head = &h->ht;
  81 
  82         write_lock_bh(&h->lock);
  83         sk_add_node(sk, head);
  84         sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  85         write_unlock_bh(&h->lock);
  86 
  87         return 0;
  88 }
  89 EXPORT_SYMBOL_GPL(smc_hash_sk);
  90 
  91 void smc_unhash_sk(struct sock *sk)
  92 {
  93         struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
  94 
  95         write_lock_bh(&h->lock);
  96         if (sk_del_node_init(sk))
  97                 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  98         write_unlock_bh(&h->lock);
  99 }
 100 EXPORT_SYMBOL_GPL(smc_unhash_sk);
 101 
 102 struct proto smc_proto = {
 103         .name           = "SMC",
 104         .owner          = THIS_MODULE,
 105         .keepalive      = smc_set_keepalive,
 106         .hash           = smc_hash_sk,
 107         .unhash         = smc_unhash_sk,
 108         .obj_size       = sizeof(struct smc_sock),
 109         .h.smc_hash     = &smc_v4_hashinfo,
 110         .slab_flags     = SLAB_TYPESAFE_BY_RCU,
 111 };
 112 EXPORT_SYMBOL_GPL(smc_proto);
 113 
 114 struct proto smc_proto6 = {
 115         .name           = "SMC6",
 116         .owner          = THIS_MODULE,
 117         .keepalive      = smc_set_keepalive,
 118         .hash           = smc_hash_sk,
 119         .unhash         = smc_unhash_sk,
 120         .obj_size       = sizeof(struct smc_sock),
 121         .h.smc_hash     = &smc_v6_hashinfo,
 122         .slab_flags     = SLAB_TYPESAFE_BY_RCU,
 123 };
 124 EXPORT_SYMBOL_GPL(smc_proto6);
 125 
 126 static void smc_restore_fallback_changes(struct smc_sock *smc)
 127 {
 128         smc->clcsock->file->private_data = smc->sk.sk_socket;
 129         smc->clcsock->file = NULL;
 130 }
 131 
 132 static int __smc_release(struct smc_sock *smc)
 133 {
 134         struct sock *sk = &smc->sk;
 135         int rc = 0;
 136 
 137         if (!smc->use_fallback) {
 138                 rc = smc_close_active(smc);
 139                 sock_set_flag(sk, SOCK_DEAD);
 140                 sk->sk_shutdown |= SHUTDOWN_MASK;
 141         } else {
 142                 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
 143                         sock_put(sk); /* passive closing */
 144                 if (sk->sk_state == SMC_LISTEN) {
 145                         /* wake up clcsock accept */
 146                         rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
 147                 }
 148                 sk->sk_state = SMC_CLOSED;
 149                 sk->sk_state_change(sk);
 150                 smc_restore_fallback_changes(smc);
 151         }
 152 
 153         sk->sk_prot->unhash(sk);
 154 
 155         if (sk->sk_state == SMC_CLOSED) {
 156                 if (smc->clcsock) {
 157                         release_sock(sk);
 158                         smc_clcsock_release(smc);
 159                         lock_sock(sk);
 160                 }
 161                 if (!smc->use_fallback)
 162                         smc_conn_free(&smc->conn);
 163         }
 164 
 165         return rc;
 166 }
 167 
 168 static int smc_release(struct socket *sock)
 169 {
 170         struct sock *sk = sock->sk;
 171         struct smc_sock *smc;
 172         int rc = 0;
 173 
 174         if (!sk)
 175                 goto out;
 176 
 177         smc = smc_sk(sk);
 178 
 179         /* cleanup for a dangling non-blocking connect */
 180         if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
 181                 tcp_abort(smc->clcsock->sk, ECONNABORTED);
 182         flush_work(&smc->connect_work);
 183 
 184         if (sk->sk_state == SMC_LISTEN)
 185                 /* smc_close_non_accepted() is called and acquires
 186                  * sock lock for child sockets again
 187                  */
 188                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
 189         else
 190                 lock_sock(sk);
 191 
 192         rc = __smc_release(smc);
 193 
 194         /* detach socket */
 195         sock_orphan(sk);
 196         sock->sk = NULL;
 197         release_sock(sk);
 198 
 199         sock_put(sk); /* final sock_put */
 200 out:
 201         return rc;
 202 }
 203 
 204 static void smc_destruct(struct sock *sk)
 205 {
 206         if (sk->sk_state != SMC_CLOSED)
 207                 return;
 208         if (!sock_flag(sk, SOCK_DEAD))
 209                 return;
 210 
 211         sk_refcnt_debug_dec(sk);
 212 }
 213 
 214 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
 215                                    int protocol)
 216 {
 217         struct smc_sock *smc;
 218         struct proto *prot;
 219         struct sock *sk;
 220 
 221         prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
 222         sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
 223         if (!sk)
 224                 return NULL;
 225 
 226         sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
 227         sk->sk_state = SMC_INIT;
 228         sk->sk_destruct = smc_destruct;
 229         sk->sk_protocol = protocol;
 230         smc = smc_sk(sk);
 231         INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
 232         INIT_WORK(&smc->connect_work, smc_connect_work);
 233         INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
 234         INIT_LIST_HEAD(&smc->accept_q);
 235         spin_lock_init(&smc->accept_q_lock);
 236         spin_lock_init(&smc->conn.send_lock);
 237         sk->sk_prot->hash(sk);
 238         sk_refcnt_debug_inc(sk);
 239         mutex_init(&smc->clcsock_release_lock);
 240 
 241         return sk;
 242 }
 243 
 244 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
 245                     int addr_len)
 246 {
 247         struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
 248         struct sock *sk = sock->sk;
 249         struct smc_sock *smc;
 250         int rc;
 251 
 252         smc = smc_sk(sk);
 253 
 254         /* replicate tests from inet_bind(), to be safe wrt. future changes */
 255         rc = -EINVAL;
 256         if (addr_len < sizeof(struct sockaddr_in))
 257                 goto out;
 258 
 259         rc = -EAFNOSUPPORT;
 260         if (addr->sin_family != AF_INET &&
 261             addr->sin_family != AF_INET6 &&
 262             addr->sin_family != AF_UNSPEC)
 263                 goto out;
 264         /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
 265         if (addr->sin_family == AF_UNSPEC &&
 266             addr->sin_addr.s_addr != htonl(INADDR_ANY))
 267                 goto out;
 268 
 269         lock_sock(sk);
 270 
 271         /* Check if socket is already active */
 272         rc = -EINVAL;
 273         if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
 274                 goto out_rel;
 275 
 276         smc->clcsock->sk->sk_reuse = sk->sk_reuse;
 277         rc = kernel_bind(smc->clcsock, uaddr, addr_len);
 278 
 279 out_rel:
 280         release_sock(sk);
 281 out:
 282         return rc;
 283 }
 284 
 285 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
 286                                    unsigned long mask)
 287 {
 288         /* options we don't get control via setsockopt for */
 289         nsk->sk_type = osk->sk_type;
 290         nsk->sk_sndbuf = osk->sk_sndbuf;
 291         nsk->sk_rcvbuf = osk->sk_rcvbuf;
 292         nsk->sk_sndtimeo = osk->sk_sndtimeo;
 293         nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
 294         nsk->sk_mark = osk->sk_mark;
 295         nsk->sk_priority = osk->sk_priority;
 296         nsk->sk_rcvlowat = osk->sk_rcvlowat;
 297         nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
 298         nsk->sk_err = osk->sk_err;
 299 
 300         nsk->sk_flags &= ~mask;
 301         nsk->sk_flags |= osk->sk_flags & mask;
 302 }
 303 
 304 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
 305                              (1UL << SOCK_KEEPOPEN) | \
 306                              (1UL << SOCK_LINGER) | \
 307                              (1UL << SOCK_BROADCAST) | \
 308                              (1UL << SOCK_TIMESTAMP) | \
 309                              (1UL << SOCK_DBG) | \
 310                              (1UL << SOCK_RCVTSTAMP) | \
 311                              (1UL << SOCK_RCVTSTAMPNS) | \
 312                              (1UL << SOCK_LOCALROUTE) | \
 313                              (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
 314                              (1UL << SOCK_RXQ_OVFL) | \
 315                              (1UL << SOCK_WIFI_STATUS) | \
 316                              (1UL << SOCK_NOFCS) | \
 317                              (1UL << SOCK_FILTER_LOCKED) | \
 318                              (1UL << SOCK_TSTAMP_NEW))
 319 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
 320  * clc socket (since smc is not called for these options from net/core)
 321  */
 322 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
 323 {
 324         smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
 325 }
 326 
 327 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
 328                              (1UL << SOCK_KEEPOPEN) | \
 329                              (1UL << SOCK_LINGER) | \
 330                              (1UL << SOCK_DBG))
 331 /* copy only settings and flags relevant for smc from clc to smc socket */
 332 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
 333 {
 334         smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
 335 }
 336 
 337 /* register a new rmb, send confirm_rkey msg to register with peer */
 338 static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
 339                        bool conf_rkey)
 340 {
 341         if (!rmb_desc->wr_reg) {
 342                 /* register memory region for new rmb */
 343                 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
 344                         rmb_desc->regerr = 1;
 345                         return -EFAULT;
 346                 }
 347                 rmb_desc->wr_reg = 1;
 348         }
 349         if (!conf_rkey)
 350                 return 0;
 351         /* exchange confirm_rkey msg with peer */
 352         if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
 353                 rmb_desc->regerr = 1;
 354                 return -EFAULT;
 355         }
 356         return 0;
 357 }
 358 
 359 static int smc_clnt_conf_first_link(struct smc_sock *smc)
 360 {
 361         struct net *net = sock_net(smc->clcsock->sk);
 362         struct smc_link_group *lgr = smc->conn.lgr;
 363         struct smc_link *link;
 364         int rest;
 365         int rc;
 366 
 367         link = &lgr->lnk[SMC_SINGLE_LINK];
 368         /* receive CONFIRM LINK request from server over RoCE fabric */
 369         rest = wait_for_completion_interruptible_timeout(
 370                 &link->llc_confirm,
 371                 SMC_LLC_WAIT_FIRST_TIME);
 372         if (rest <= 0) {
 373                 struct smc_clc_msg_decline dclc;
 374 
 375                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
 376                                       SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
 377                 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
 378         }
 379 
 380         if (link->llc_confirm_rc)
 381                 return SMC_CLC_DECL_RMBE_EC;
 382 
 383         rc = smc_ib_modify_qp_rts(link);
 384         if (rc)
 385                 return SMC_CLC_DECL_ERR_RDYLNK;
 386 
 387         smc_wr_remember_qp_attr(link);
 388 
 389         if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
 390                 return SMC_CLC_DECL_ERR_REGRMB;
 391 
 392         /* send CONFIRM LINK response over RoCE fabric */
 393         rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
 394         if (rc < 0)
 395                 return SMC_CLC_DECL_TIMEOUT_CL;
 396 
 397         /* receive ADD LINK request from server over RoCE fabric */
 398         rest = wait_for_completion_interruptible_timeout(&link->llc_add,
 399                                                          SMC_LLC_WAIT_TIME);
 400         if (rest <= 0) {
 401                 struct smc_clc_msg_decline dclc;
 402 
 403                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
 404                                       SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
 405                 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
 406         }
 407 
 408         /* send add link reject message, only one link supported for now */
 409         rc = smc_llc_send_add_link(link,
 410                                    link->smcibdev->mac[link->ibport - 1],
 411                                    link->gid, SMC_LLC_RESP);
 412         if (rc < 0)
 413                 return SMC_CLC_DECL_TIMEOUT_AL;
 414 
 415         smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
 416 
 417         return 0;
 418 }
 419 
 420 static void smcr_conn_save_peer_info(struct smc_sock *smc,
 421                                      struct smc_clc_msg_accept_confirm *clc)
 422 {
 423         int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
 424 
 425         smc->conn.peer_rmbe_idx = clc->rmbe_idx;
 426         smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
 427         smc->conn.peer_rmbe_size = bufsize;
 428         atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
 429         smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
 430 }
 431 
 432 static void smcd_conn_save_peer_info(struct smc_sock *smc,
 433                                      struct smc_clc_msg_accept_confirm *clc)
 434 {
 435         int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
 436 
 437         smc->conn.peer_rmbe_idx = clc->dmbe_idx;
 438         smc->conn.peer_token = clc->token;
 439         /* msg header takes up space in the buffer */
 440         smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
 441         atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
 442         smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
 443 }
 444 
 445 static void smc_conn_save_peer_info(struct smc_sock *smc,
 446                                     struct smc_clc_msg_accept_confirm *clc)
 447 {
 448         if (smc->conn.lgr->is_smcd)
 449                 smcd_conn_save_peer_info(smc, clc);
 450         else
 451                 smcr_conn_save_peer_info(smc, clc);
 452 }
 453 
 454 static void smc_link_save_peer_info(struct smc_link *link,
 455                                     struct smc_clc_msg_accept_confirm *clc)
 456 {
 457         link->peer_qpn = ntoh24(clc->qpn);
 458         memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
 459         memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
 460         link->peer_psn = ntoh24(clc->psn);
 461         link->peer_mtu = clc->qp_mtu;
 462 }
 463 
 464 static void smc_switch_to_fallback(struct smc_sock *smc)
 465 {
 466         smc->use_fallback = true;
 467         if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
 468                 smc->clcsock->file = smc->sk.sk_socket->file;
 469                 smc->clcsock->file->private_data = smc->clcsock;
 470                 smc->clcsock->wq.fasync_list =
 471                         smc->sk.sk_socket->wq.fasync_list;
 472         }
 473 }
 474 
 475 /* fall back during connect */
 476 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
 477 {
 478         smc_switch_to_fallback(smc);
 479         smc->fallback_rsn = reason_code;
 480         smc_copy_sock_settings_to_clc(smc);
 481         smc->connect_nonblock = 0;
 482         if (smc->sk.sk_state == SMC_INIT)
 483                 smc->sk.sk_state = SMC_ACTIVE;
 484         return 0;
 485 }
 486 
 487 /* decline and fall back during connect */
 488 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
 489 {
 490         int rc;
 491 
 492         if (reason_code < 0) { /* error, fallback is not possible */
 493                 if (smc->sk.sk_state == SMC_INIT)
 494                         sock_put(&smc->sk); /* passive closing */
 495                 return reason_code;
 496         }
 497         if (reason_code != SMC_CLC_DECL_PEERDECL) {
 498                 rc = smc_clc_send_decline(smc, reason_code);
 499                 if (rc < 0) {
 500                         if (smc->sk.sk_state == SMC_INIT)
 501                                 sock_put(&smc->sk); /* passive closing */
 502                         return rc;
 503                 }
 504         }
 505         return smc_connect_fallback(smc, reason_code);
 506 }
 507 
 508 /* abort connecting */
 509 static int smc_connect_abort(struct smc_sock *smc, int reason_code,
 510                              int local_contact)
 511 {
 512         if (local_contact == SMC_FIRST_CONTACT)
 513                 smc_lgr_forget(smc->conn.lgr);
 514         if (smc->conn.lgr->is_smcd)
 515                 /* there is only one lgr role for SMC-D; use server lock */
 516                 mutex_unlock(&smc_server_lgr_pending);
 517         else
 518                 mutex_unlock(&smc_client_lgr_pending);
 519 
 520         smc_conn_free(&smc->conn);
 521         smc->connect_nonblock = 0;
 522         return reason_code;
 523 }
 524 
 525 /* check if there is a rdma device available for this connection. */
 526 /* called for connect and listen */
 527 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
 528 {
 529         /* PNET table look up: search active ib_device and port
 530          * within same PNETID that also contains the ethernet device
 531          * used for the internal TCP socket
 532          */
 533         smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
 534         if (!ini->ib_dev)
 535                 return SMC_CLC_DECL_NOSMCRDEV;
 536         return 0;
 537 }
 538 
 539 /* check if there is an ISM device available for this connection. */
 540 /* called for connect and listen */
 541 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
 542 {
 543         /* Find ISM device with same PNETID as connecting interface  */
 544         smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
 545         if (!ini->ism_dev)
 546                 return SMC_CLC_DECL_NOSMCDDEV;
 547         return 0;
 548 }
 549 
 550 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
 551 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
 552                                       struct smc_init_info *ini)
 553 {
 554         if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
 555                 return SMC_CLC_DECL_ISMVLANERR;
 556         return 0;
 557 }
 558 
 559 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
 560  * used, the VLAN ID will be registered again during the connection setup.
 561  */
 562 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
 563                                         struct smc_init_info *ini)
 564 {
 565         if (!is_smcd)
 566                 return 0;
 567         if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
 568                 return SMC_CLC_DECL_CNFERR;
 569         return 0;
 570 }
 571 
 572 /* CLC handshake during connect */
 573 static int smc_connect_clc(struct smc_sock *smc, int smc_type,
 574                            struct smc_clc_msg_accept_confirm *aclc,
 575                            struct smc_init_info *ini)
 576 {
 577         int rc = 0;
 578 
 579         /* do inband token exchange */
 580         rc = smc_clc_send_proposal(smc, smc_type, ini);
 581         if (rc)
 582                 return rc;
 583         /* receive SMC Accept CLC message */
 584         return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
 585                                 CLC_WAIT_TIME);
 586 }
 587 
 588 /* setup for RDMA connection of client */
 589 static int smc_connect_rdma(struct smc_sock *smc,
 590                             struct smc_clc_msg_accept_confirm *aclc,
 591                             struct smc_init_info *ini)
 592 {
 593         struct smc_link *link;
 594         int reason_code = 0;
 595 
 596         ini->is_smcd = false;
 597         ini->ib_lcl = &aclc->lcl;
 598         ini->ib_clcqpn = ntoh24(aclc->qpn);
 599         ini->srv_first_contact = aclc->hdr.flag;
 600 
 601         mutex_lock(&smc_client_lgr_pending);
 602         reason_code = smc_conn_create(smc, ini);
 603         if (reason_code) {
 604                 mutex_unlock(&smc_client_lgr_pending);
 605                 return reason_code;
 606         }
 607         link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
 608 
 609         smc_conn_save_peer_info(smc, aclc);
 610 
 611         /* create send buffer and rmb */
 612         if (smc_buf_create(smc, false))
 613                 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
 614                                          ini->cln_first_contact);
 615 
 616         if (ini->cln_first_contact == SMC_FIRST_CONTACT)
 617                 smc_link_save_peer_info(link, aclc);
 618 
 619         if (smc_rmb_rtoken_handling(&smc->conn, aclc))
 620                 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
 621                                          ini->cln_first_contact);
 622 
 623         smc_close_init(smc);
 624         smc_rx_init(smc);
 625 
 626         if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
 627                 if (smc_ib_ready_link(link))
 628                         return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
 629                                                  ini->cln_first_contact);
 630         } else {
 631                 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
 632                         return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
 633                                                  ini->cln_first_contact);
 634         }
 635         smc_rmb_sync_sg_for_device(&smc->conn);
 636 
 637         reason_code = smc_clc_send_confirm(smc);
 638         if (reason_code)
 639                 return smc_connect_abort(smc, reason_code,
 640                                          ini->cln_first_contact);
 641 
 642         smc_tx_init(smc);
 643 
 644         if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
 645                 /* QP confirmation over RoCE fabric */
 646                 reason_code = smc_clnt_conf_first_link(smc);
 647                 if (reason_code)
 648                         return smc_connect_abort(smc, reason_code,
 649                                                  ini->cln_first_contact);
 650         }
 651         mutex_unlock(&smc_client_lgr_pending);
 652 
 653         smc_copy_sock_settings_to_clc(smc);
 654         smc->connect_nonblock = 0;
 655         if (smc->sk.sk_state == SMC_INIT)
 656                 smc->sk.sk_state = SMC_ACTIVE;
 657 
 658         return 0;
 659 }
 660 
 661 /* setup for ISM connection of client */
 662 static int smc_connect_ism(struct smc_sock *smc,
 663                            struct smc_clc_msg_accept_confirm *aclc,
 664                            struct smc_init_info *ini)
 665 {
 666         int rc = 0;
 667 
 668         ini->is_smcd = true;
 669         ini->ism_gid = aclc->gid;
 670         ini->srv_first_contact = aclc->hdr.flag;
 671 
 672         /* there is only one lgr role for SMC-D; use server lock */
 673         mutex_lock(&smc_server_lgr_pending);
 674         rc = smc_conn_create(smc, ini);
 675         if (rc) {
 676                 mutex_unlock(&smc_server_lgr_pending);
 677                 return rc;
 678         }
 679 
 680         /* Create send and receive buffers */
 681         if (smc_buf_create(smc, true))
 682                 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
 683                                          ini->cln_first_contact);
 684 
 685         smc_conn_save_peer_info(smc, aclc);
 686         smc_close_init(smc);
 687         smc_rx_init(smc);
 688         smc_tx_init(smc);
 689 
 690         rc = smc_clc_send_confirm(smc);
 691         if (rc)
 692                 return smc_connect_abort(smc, rc, ini->cln_first_contact);
 693         mutex_unlock(&smc_server_lgr_pending);
 694 
 695         smc_copy_sock_settings_to_clc(smc);
 696         smc->connect_nonblock = 0;
 697         if (smc->sk.sk_state == SMC_INIT)
 698                 smc->sk.sk_state = SMC_ACTIVE;
 699 
 700         return 0;
 701 }
 702 
 703 /* perform steps before actually connecting */
 704 static int __smc_connect(struct smc_sock *smc)
 705 {
 706         bool ism_supported = false, rdma_supported = false;
 707         struct smc_clc_msg_accept_confirm aclc;
 708         struct smc_init_info ini = {0};
 709         int smc_type;
 710         int rc = 0;
 711 
 712         if (smc->use_fallback)
 713                 return smc_connect_fallback(smc, smc->fallback_rsn);
 714 
 715         /* if peer has not signalled SMC-capability, fall back */
 716         if (!tcp_sk(smc->clcsock->sk)->syn_smc)
 717                 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
 718 
 719         /* IPSec connections opt out of SMC-R optimizations */
 720         if (using_ipsec(smc))
 721                 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
 722 
 723         /* get vlan id from IP device */
 724         if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
 725                 return smc_connect_decline_fallback(smc,
 726                                                     SMC_CLC_DECL_GETVLANERR);
 727 
 728         /* check if there is an ism device available */
 729         if (!smc_find_ism_device(smc, &ini) &&
 730             !smc_connect_ism_vlan_setup(smc, &ini)) {
 731                 /* ISM is supported for this connection */
 732                 ism_supported = true;
 733                 smc_type = SMC_TYPE_D;
 734         }
 735 
 736         /* check if there is a rdma device available */
 737         if (!smc_find_rdma_device(smc, &ini)) {
 738                 /* RDMA is supported for this connection */
 739                 rdma_supported = true;
 740                 if (ism_supported)
 741                         smc_type = SMC_TYPE_B; /* both */
 742                 else
 743                         smc_type = SMC_TYPE_R; /* only RDMA */
 744         }
 745 
 746         /* if neither ISM nor RDMA are supported, fallback */
 747         if (!rdma_supported && !ism_supported)
 748                 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
 749 
 750         /* perform CLC handshake */
 751         rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
 752         if (rc) {
 753                 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
 754                 return smc_connect_decline_fallback(smc, rc);
 755         }
 756 
 757         /* depending on previous steps, connect using rdma or ism */
 758         if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
 759                 rc = smc_connect_rdma(smc, &aclc, &ini);
 760         else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
 761                 rc = smc_connect_ism(smc, &aclc, &ini);
 762         else
 763                 rc = SMC_CLC_DECL_MODEUNSUPP;
 764         if (rc) {
 765                 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
 766                 return smc_connect_decline_fallback(smc, rc);
 767         }
 768 
 769         smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
 770         return 0;
 771 }
 772 
 773 static void smc_connect_work(struct work_struct *work)
 774 {
 775         struct smc_sock *smc = container_of(work, struct smc_sock,
 776                                             connect_work);
 777         long timeo = smc->sk.sk_sndtimeo;
 778         int rc = 0;
 779 
 780         if (!timeo)
 781                 timeo = MAX_SCHEDULE_TIMEOUT;
 782         lock_sock(smc->clcsock->sk);
 783         if (smc->clcsock->sk->sk_err) {
 784                 smc->sk.sk_err = smc->clcsock->sk->sk_err;
 785         } else if ((1 << smc->clcsock->sk->sk_state) &
 786                                         (TCPF_SYN_SENT | TCP_SYN_RECV)) {
 787                 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
 788                 if ((rc == -EPIPE) &&
 789                     ((1 << smc->clcsock->sk->sk_state) &
 790                                         (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
 791                         rc = 0;
 792         }
 793         release_sock(smc->clcsock->sk);
 794         lock_sock(&smc->sk);
 795         if (rc != 0 || smc->sk.sk_err) {
 796                 smc->sk.sk_state = SMC_CLOSED;
 797                 if (rc == -EPIPE || rc == -EAGAIN)
 798                         smc->sk.sk_err = EPIPE;
 799                 else if (signal_pending(current))
 800                         smc->sk.sk_err = -sock_intr_errno(timeo);
 801                 sock_put(&smc->sk); /* passive closing */
 802                 goto out;
 803         }
 804 
 805         rc = __smc_connect(smc);
 806         if (rc < 0)
 807                 smc->sk.sk_err = -rc;
 808 
 809 out:
 810         if (!sock_flag(&smc->sk, SOCK_DEAD)) {
 811                 if (smc->sk.sk_err) {
 812                         smc->sk.sk_state_change(&smc->sk);
 813                 } else { /* allow polling before and after fallback decision */
 814                         smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
 815                         smc->sk.sk_write_space(&smc->sk);
 816                 }
 817         }
 818         release_sock(&smc->sk);
 819 }
 820 
 821 static int smc_connect(struct socket *sock, struct sockaddr *addr,
 822                        int alen, int flags)
 823 {
 824         struct sock *sk = sock->sk;
 825         struct smc_sock *smc;
 826         int rc = -EINVAL;
 827 
 828         smc = smc_sk(sk);
 829 
 830         /* separate smc parameter checking to be safe */
 831         if (alen < sizeof(addr->sa_family))
 832                 goto out_err;
 833         if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
 834                 goto out_err;
 835 
 836         lock_sock(sk);
 837         switch (sk->sk_state) {
 838         default:
 839                 goto out;
 840         case SMC_ACTIVE:
 841                 rc = -EISCONN;
 842                 goto out;
 843         case SMC_INIT:
 844                 rc = 0;
 845                 break;
 846         }
 847 
 848         smc_copy_sock_settings_to_clc(smc);
 849         tcp_sk(smc->clcsock->sk)->syn_smc = 1;
 850         if (smc->connect_nonblock) {
 851                 rc = -EALREADY;
 852                 goto out;
 853         }
 854         rc = kernel_connect(smc->clcsock, addr, alen, flags);
 855         if (rc && rc != -EINPROGRESS)
 856                 goto out;
 857 
 858         sock_hold(&smc->sk); /* sock put in passive closing */
 859         if (smc->use_fallback)
 860                 goto out;
 861         if (flags & O_NONBLOCK) {
 862                 if (schedule_work(&smc->connect_work))
 863                         smc->connect_nonblock = 1;
 864                 rc = -EINPROGRESS;
 865         } else {
 866                 rc = __smc_connect(smc);
 867                 if (rc < 0)
 868                         goto out;
 869                 else
 870                         rc = 0; /* success cases including fallback */
 871         }
 872 
 873 out:
 874         release_sock(sk);
 875 out_err:
 876         return rc;
 877 }
 878 
 879 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 880 {
 881         struct socket *new_clcsock = NULL;
 882         struct sock *lsk = &lsmc->sk;
 883         struct sock *new_sk;
 884         int rc = -EINVAL;
 885 
 886         release_sock(lsk);
 887         new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
 888         if (!new_sk) {
 889                 rc = -ENOMEM;
 890                 lsk->sk_err = ENOMEM;
 891                 *new_smc = NULL;
 892                 lock_sock(lsk);
 893                 goto out;
 894         }
 895         *new_smc = smc_sk(new_sk);
 896 
 897         mutex_lock(&lsmc->clcsock_release_lock);
 898         if (lsmc->clcsock)
 899                 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
 900         mutex_unlock(&lsmc->clcsock_release_lock);
 901         lock_sock(lsk);
 902         if  (rc < 0)
 903                 lsk->sk_err = -rc;
 904         if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
 905                 new_sk->sk_prot->unhash(new_sk);
 906                 if (new_clcsock)
 907                         sock_release(new_clcsock);
 908                 new_sk->sk_state = SMC_CLOSED;
 909                 sock_set_flag(new_sk, SOCK_DEAD);
 910                 sock_put(new_sk); /* final */
 911                 *new_smc = NULL;
 912                 goto out;
 913         }
 914 
 915         (*new_smc)->clcsock = new_clcsock;
 916 out:
 917         return rc;
 918 }
 919 
 920 /* add a just created sock to the accept queue of the listen sock as
 921  * candidate for a following socket accept call from user space
 922  */
 923 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
 924 {
 925         struct smc_sock *par = smc_sk(parent);
 926 
 927         sock_hold(sk); /* sock_put in smc_accept_unlink () */
 928         spin_lock(&par->accept_q_lock);
 929         list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
 930         spin_unlock(&par->accept_q_lock);
 931         sk_acceptq_added(parent);
 932 }
 933 
 934 /* remove a socket from the accept queue of its parental listening socket */
 935 static void smc_accept_unlink(struct sock *sk)
 936 {
 937         struct smc_sock *par = smc_sk(sk)->listen_smc;
 938 
 939         spin_lock(&par->accept_q_lock);
 940         list_del_init(&smc_sk(sk)->accept_q);
 941         spin_unlock(&par->accept_q_lock);
 942         sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
 943         sock_put(sk); /* sock_hold in smc_accept_enqueue */
 944 }
 945 
 946 /* remove a sock from the accept queue to bind it to a new socket created
 947  * for a socket accept call from user space
 948  */
 949 struct sock *smc_accept_dequeue(struct sock *parent,
 950                                 struct socket *new_sock)
 951 {
 952         struct smc_sock *isk, *n;
 953         struct sock *new_sk;
 954 
 955         list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
 956                 new_sk = (struct sock *)isk;
 957 
 958                 smc_accept_unlink(new_sk);
 959                 if (new_sk->sk_state == SMC_CLOSED) {
 960                         new_sk->sk_prot->unhash(new_sk);
 961                         if (isk->clcsock) {
 962                                 sock_release(isk->clcsock);
 963                                 isk->clcsock = NULL;
 964                         }
 965                         sock_put(new_sk); /* final */
 966                         continue;
 967                 }
 968                 if (new_sock) {
 969                         sock_graft(new_sk, new_sock);
 970                         if (isk->use_fallback) {
 971                                 smc_sk(new_sk)->clcsock->file = new_sock->file;
 972                                 isk->clcsock->file->private_data = isk->clcsock;
 973                         }
 974                 }
 975                 return new_sk;
 976         }
 977         return NULL;
 978 }
 979 
 980 /* clean up for a created but never accepted sock */
 981 void smc_close_non_accepted(struct sock *sk)
 982 {
 983         struct smc_sock *smc = smc_sk(sk);
 984 
 985         lock_sock(sk);
 986         if (!sk->sk_lingertime)
 987                 /* wait for peer closing */
 988                 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
 989         __smc_release(smc);
 990         release_sock(sk);
 991         sock_put(sk); /* final sock_put */
 992 }
 993 
 994 static int smc_serv_conf_first_link(struct smc_sock *smc)
 995 {
 996         struct net *net = sock_net(smc->clcsock->sk);
 997         struct smc_link_group *lgr = smc->conn.lgr;
 998         struct smc_link *link;
 999         int rest;
1000         int rc;
1001 
1002         link = &lgr->lnk[SMC_SINGLE_LINK];
1003 
1004         if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
1005                 return SMC_CLC_DECL_ERR_REGRMB;
1006 
1007         /* send CONFIRM LINK request to client over the RoCE fabric */
1008         rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1009         if (rc < 0)
1010                 return SMC_CLC_DECL_TIMEOUT_CL;
1011 
1012         /* receive CONFIRM LINK response from client over the RoCE fabric */
1013         rest = wait_for_completion_interruptible_timeout(
1014                 &link->llc_confirm_resp,
1015                 SMC_LLC_WAIT_FIRST_TIME);
1016         if (rest <= 0) {
1017                 struct smc_clc_msg_decline dclc;
1018 
1019                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1020                                       SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1021                 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1022         }
1023 
1024         if (link->llc_confirm_resp_rc)
1025                 return SMC_CLC_DECL_RMBE_EC;
1026 
1027         /* send ADD LINK request to client over the RoCE fabric */
1028         rc = smc_llc_send_add_link(link,
1029                                    link->smcibdev->mac[link->ibport - 1],
1030                                    link->gid, SMC_LLC_REQ);
1031         if (rc < 0)
1032                 return SMC_CLC_DECL_TIMEOUT_AL;
1033 
1034         /* receive ADD LINK response from client over the RoCE fabric */
1035         rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1036                                                          SMC_LLC_WAIT_TIME);
1037         if (rest <= 0) {
1038                 struct smc_clc_msg_decline dclc;
1039 
1040                 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1041                                       SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1042                 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
1043         }
1044 
1045         smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
1046 
1047         return 0;
1048 }
1049 
1050 /* listen worker: finish */
1051 static void smc_listen_out(struct smc_sock *new_smc)
1052 {
1053         struct smc_sock *lsmc = new_smc->listen_smc;
1054         struct sock *newsmcsk = &new_smc->sk;
1055 
1056         if (lsmc->sk.sk_state == SMC_LISTEN) {
1057                 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1058                 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1059                 release_sock(&lsmc->sk);
1060         } else { /* no longer listening */
1061                 smc_close_non_accepted(newsmcsk);
1062         }
1063 
1064         /* Wake up accept */
1065         lsmc->sk.sk_data_ready(&lsmc->sk);
1066         sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1067 }
1068 
1069 /* listen worker: finish in state connected */
1070 static void smc_listen_out_connected(struct smc_sock *new_smc)
1071 {
1072         struct sock *newsmcsk = &new_smc->sk;
1073 
1074         sk_refcnt_debug_inc(newsmcsk);
1075         if (newsmcsk->sk_state == SMC_INIT)
1076                 newsmcsk->sk_state = SMC_ACTIVE;
1077 
1078         smc_listen_out(new_smc);
1079 }
1080 
1081 /* listen worker: finish in error state */
1082 static void smc_listen_out_err(struct smc_sock *new_smc)
1083 {
1084         struct sock *newsmcsk = &new_smc->sk;
1085 
1086         if (newsmcsk->sk_state == SMC_INIT)
1087                 sock_put(&new_smc->sk); /* passive closing */
1088         newsmcsk->sk_state = SMC_CLOSED;
1089         smc_conn_free(&new_smc->conn);
1090 
1091         smc_listen_out(new_smc);
1092 }
1093 
1094 /* listen worker: decline and fall back if possible */
1095 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1096                                int local_contact)
1097 {
1098         /* RDMA setup failed, switch back to TCP */
1099         if (local_contact == SMC_FIRST_CONTACT)
1100                 smc_lgr_forget(new_smc->conn.lgr);
1101         if (reason_code < 0) { /* error, no fallback possible */
1102                 smc_listen_out_err(new_smc);
1103                 return;
1104         }
1105         smc_conn_free(&new_smc->conn);
1106         smc_switch_to_fallback(new_smc);
1107         new_smc->fallback_rsn = reason_code;
1108         if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1109                 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1110                         smc_listen_out_err(new_smc);
1111                         return;
1112                 }
1113         }
1114         smc_listen_out_connected(new_smc);
1115 }
1116 
1117 /* listen worker: check prefixes */
1118 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1119                                  struct smc_clc_msg_proposal *pclc)
1120 {
1121         struct smc_clc_msg_proposal_prefix *pclc_prfx;
1122         struct socket *newclcsock = new_smc->clcsock;
1123 
1124         pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1125         if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1126                 return SMC_CLC_DECL_DIFFPREFIX;
1127 
1128         return 0;
1129 }
1130 
1131 /* listen worker: initialize connection and buffers */
1132 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1133                                 struct smc_init_info *ini)
1134 {
1135         int rc;
1136 
1137         /* allocate connection / link group */
1138         rc = smc_conn_create(new_smc, ini);
1139         if (rc)
1140                 return rc;
1141 
1142         /* create send buffer and rmb */
1143         if (smc_buf_create(new_smc, false))
1144                 return SMC_CLC_DECL_MEM;
1145 
1146         return 0;
1147 }
1148 
1149 /* listen worker: initialize connection and buffers for SMC-D */
1150 static int smc_listen_ism_init(struct smc_sock *new_smc,
1151                                struct smc_clc_msg_proposal *pclc,
1152                                struct smc_init_info *ini)
1153 {
1154         struct smc_clc_msg_smcd *pclc_smcd;
1155         int rc;
1156 
1157         pclc_smcd = smc_get_clc_msg_smcd(pclc);
1158         ini->ism_gid = pclc_smcd->gid;
1159         rc = smc_conn_create(new_smc, ini);
1160         if (rc)
1161                 return rc;
1162 
1163         /* Check if peer can be reached via ISM device */
1164         if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1165                             new_smc->conn.lgr->vlan_id,
1166                             new_smc->conn.lgr->smcd)) {
1167                 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
1168                         smc_lgr_forget(new_smc->conn.lgr);
1169                 smc_conn_free(&new_smc->conn);
1170                 return SMC_CLC_DECL_SMCDNOTALK;
1171         }
1172 
1173         /* Create send and receive buffers */
1174         if (smc_buf_create(new_smc, true)) {
1175                 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
1176                         smc_lgr_forget(new_smc->conn.lgr);
1177                 smc_conn_free(&new_smc->conn);
1178                 return SMC_CLC_DECL_MEM;
1179         }
1180 
1181         return 0;
1182 }
1183 
1184 /* listen worker: register buffers */
1185 static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1186 {
1187         struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1188 
1189         if (local_contact != SMC_FIRST_CONTACT) {
1190                 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1191                         return SMC_CLC_DECL_ERR_REGRMB;
1192         }
1193         smc_rmb_sync_sg_for_device(&new_smc->conn);
1194 
1195         return 0;
1196 }
1197 
1198 /* listen worker: finish RDMA setup */
1199 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1200                                   struct smc_clc_msg_accept_confirm *cclc,
1201                                   int local_contact)
1202 {
1203         struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1204         int reason_code = 0;
1205 
1206         if (local_contact == SMC_FIRST_CONTACT)
1207                 smc_link_save_peer_info(link, cclc);
1208 
1209         if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
1210                 reason_code = SMC_CLC_DECL_ERR_RTOK;
1211                 goto decline;
1212         }
1213 
1214         if (local_contact == SMC_FIRST_CONTACT) {
1215                 if (smc_ib_ready_link(link)) {
1216                         reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1217                         goto decline;
1218                 }
1219                 /* QP confirmation over RoCE fabric */
1220                 reason_code = smc_serv_conf_first_link(new_smc);
1221                 if (reason_code)
1222                         goto decline;
1223         }
1224         return 0;
1225 
1226 decline:
1227         smc_listen_decline(new_smc, reason_code, local_contact);
1228         return reason_code;
1229 }
1230 
1231 /* setup for RDMA connection of server */
1232 static void smc_listen_work(struct work_struct *work)
1233 {
1234         struct smc_sock *new_smc = container_of(work, struct smc_sock,
1235                                                 smc_listen_work);
1236         struct socket *newclcsock = new_smc->clcsock;
1237         struct smc_clc_msg_accept_confirm cclc;
1238         struct smc_clc_msg_proposal *pclc;
1239         struct smc_init_info ini = {0};
1240         bool ism_supported = false;
1241         u8 buf[SMC_CLC_MAX_LEN];
1242         int rc = 0;
1243 
1244         if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1245                 return smc_listen_out_err(new_smc);
1246 
1247         if (new_smc->use_fallback) {
1248                 smc_listen_out_connected(new_smc);
1249                 return;
1250         }
1251 
1252         /* check if peer is smc capable */
1253         if (!tcp_sk(newclcsock->sk)->syn_smc) {
1254                 smc_switch_to_fallback(new_smc);
1255                 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
1256                 smc_listen_out_connected(new_smc);
1257                 return;
1258         }
1259 
1260         /* do inband token exchange -
1261          * wait for and receive SMC Proposal CLC message
1262          */
1263         pclc = (struct smc_clc_msg_proposal *)&buf;
1264         rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1265                               SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1266         if (rc)
1267                 goto out_decl;
1268 
1269         /* IPSec connections opt out of SMC-R optimizations */
1270         if (using_ipsec(new_smc)) {
1271                 rc = SMC_CLC_DECL_IPSEC;
1272                 goto out_decl;
1273         }
1274 
1275         /* check for matching IP prefix and subnet length */
1276         rc = smc_listen_prfx_check(new_smc, pclc);
1277         if (rc)
1278                 goto out_decl;
1279 
1280         /* get vlan id from IP device */
1281         if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
1282                 rc = SMC_CLC_DECL_GETVLANERR;
1283                 goto out_decl;
1284         }
1285 
1286         mutex_lock(&smc_server_lgr_pending);
1287         smc_close_init(new_smc);
1288         smc_rx_init(new_smc);
1289         smc_tx_init(new_smc);
1290 
1291         /* check if ISM is available */
1292         if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1293                 ini.is_smcd = true; /* prepare ISM check */
1294                 rc = smc_find_ism_device(new_smc, &ini);
1295                 if (!rc)
1296                         rc = smc_listen_ism_init(new_smc, pclc, &ini);
1297                 if (!rc)
1298                         ism_supported = true;
1299                 else if (pclc->hdr.path == SMC_TYPE_D)
1300                         goto out_unlock; /* skip RDMA and decline */
1301         }
1302 
1303         /* check if RDMA is available */
1304         if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
1305                 /* prepare RDMA check */
1306                 ini.is_smcd = false;
1307                 ini.ism_dev = NULL;
1308                 ini.ib_lcl = &pclc->lcl;
1309                 rc = smc_find_rdma_device(new_smc, &ini);
1310                 if (rc) {
1311                         /* no RDMA device found */
1312                         if (pclc->hdr.path == SMC_TYPE_B)
1313                                 /* neither ISM nor RDMA device found */
1314                                 rc = SMC_CLC_DECL_NOSMCDEV;
1315                         goto out_unlock;
1316                 }
1317                 rc = smc_listen_rdma_init(new_smc, &ini);
1318                 if (rc)
1319                         goto out_unlock;
1320                 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
1321                 if (rc)
1322                         goto out_unlock;
1323         }
1324 
1325         /* send SMC Accept CLC message */
1326         rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
1327         if (rc)
1328                 goto out_unlock;
1329 
1330         /* SMC-D does not need this lock any more */
1331         if (ism_supported)
1332                 mutex_unlock(&smc_server_lgr_pending);
1333 
1334         /* receive SMC Confirm CLC message */
1335         rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1336                               SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1337         if (rc) {
1338                 if (!ism_supported)
1339                         goto out_unlock;
1340                 goto out_decl;
1341         }
1342 
1343         /* finish worker */
1344         if (!ism_supported) {
1345                 rc = smc_listen_rdma_finish(new_smc, &cclc,
1346                                             ini.cln_first_contact);
1347                 mutex_unlock(&smc_server_lgr_pending);
1348                 if (rc)
1349                         return;
1350         }
1351         smc_conn_save_peer_info(new_smc, &cclc);
1352         smc_listen_out_connected(new_smc);
1353         return;
1354 
1355 out_unlock:
1356         mutex_unlock(&smc_server_lgr_pending);
1357 out_decl:
1358         smc_listen_decline(new_smc, rc, ini.cln_first_contact);
1359 }
1360 
1361 static void smc_tcp_listen_work(struct work_struct *work)
1362 {
1363         struct smc_sock *lsmc = container_of(work, struct smc_sock,
1364                                              tcp_listen_work);
1365         struct sock *lsk = &lsmc->sk;
1366         struct smc_sock *new_smc;
1367         int rc = 0;
1368 
1369         lock_sock(lsk);
1370         while (lsk->sk_state == SMC_LISTEN) {
1371                 rc = smc_clcsock_accept(lsmc, &new_smc);
1372                 if (rc)
1373                         goto out;
1374                 if (!new_smc)
1375                         continue;
1376 
1377                 new_smc->listen_smc = lsmc;
1378                 new_smc->use_fallback = lsmc->use_fallback;
1379                 new_smc->fallback_rsn = lsmc->fallback_rsn;
1380                 sock_hold(lsk); /* sock_put in smc_listen_work */
1381                 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1382                 smc_copy_sock_settings_to_smc(new_smc);
1383                 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1384                 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
1385                 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1386                 if (!schedule_work(&new_smc->smc_listen_work))
1387                         sock_put(&new_smc->sk);
1388         }
1389 
1390 out:
1391         release_sock(lsk);
1392         sock_put(&lsmc->sk); /* sock_hold in smc_listen */
1393 }
1394 
1395 static int smc_listen(struct socket *sock, int backlog)
1396 {
1397         struct sock *sk = sock->sk;
1398         struct smc_sock *smc;
1399         int rc;
1400 
1401         smc = smc_sk(sk);
1402         lock_sock(sk);
1403 
1404         rc = -EINVAL;
1405         if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1406             smc->connect_nonblock)
1407                 goto out;
1408 
1409         rc = 0;
1410         if (sk->sk_state == SMC_LISTEN) {
1411                 sk->sk_max_ack_backlog = backlog;
1412                 goto out;
1413         }
1414         /* some socket options are handled in core, so we could not apply
1415          * them to the clc socket -- copy smc socket options to clc socket
1416          */
1417         smc_copy_sock_settings_to_clc(smc);
1418         if (!smc->use_fallback)
1419                 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1420 
1421         rc = kernel_listen(smc->clcsock, backlog);
1422         if (rc)
1423                 goto out;
1424         sk->sk_max_ack_backlog = backlog;
1425         sk->sk_ack_backlog = 0;
1426         sk->sk_state = SMC_LISTEN;
1427         sock_hold(sk); /* sock_hold in tcp_listen_worker */
1428         if (!schedule_work(&smc->tcp_listen_work))
1429                 sock_put(sk);
1430 
1431 out:
1432         release_sock(sk);
1433         return rc;
1434 }
1435 
1436 static int smc_accept(struct socket *sock, struct socket *new_sock,
1437                       int flags, bool kern)
1438 {
1439         struct sock *sk = sock->sk, *nsk;
1440         DECLARE_WAITQUEUE(wait, current);
1441         struct smc_sock *lsmc;
1442         long timeo;
1443         int rc = 0;
1444 
1445         lsmc = smc_sk(sk);
1446         sock_hold(sk); /* sock_put below */
1447         lock_sock(sk);
1448 
1449         if (lsmc->sk.sk_state != SMC_LISTEN) {
1450                 rc = -EINVAL;
1451                 release_sock(sk);
1452                 goto out;
1453         }
1454 
1455         /* Wait for an incoming connection */
1456         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1457         add_wait_queue_exclusive(sk_sleep(sk), &wait);
1458         while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1459                 set_current_state(TASK_INTERRUPTIBLE);
1460                 if (!timeo) {
1461                         rc = -EAGAIN;
1462                         break;
1463                 }
1464                 release_sock(sk);
1465                 timeo = schedule_timeout(timeo);
1466                 /* wakeup by sk_data_ready in smc_listen_work() */
1467                 sched_annotate_sleep();
1468                 lock_sock(sk);
1469                 if (signal_pending(current)) {
1470                         rc = sock_intr_errno(timeo);
1471                         break;
1472                 }
1473         }
1474         set_current_state(TASK_RUNNING);
1475         remove_wait_queue(sk_sleep(sk), &wait);
1476 
1477         if (!rc)
1478                 rc = sock_error(nsk);
1479         release_sock(sk);
1480         if (rc)
1481                 goto out;
1482 
1483         if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1484                 /* wait till data arrives on the socket */
1485                 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1486                                                                 MSEC_PER_SEC);
1487                 if (smc_sk(nsk)->use_fallback) {
1488                         struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1489 
1490                         lock_sock(clcsk);
1491                         if (skb_queue_empty(&clcsk->sk_receive_queue))
1492                                 sk_wait_data(clcsk, &timeo, NULL);
1493                         release_sock(clcsk);
1494                 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1495                         lock_sock(nsk);
1496                         smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
1497                         release_sock(nsk);
1498                 }
1499         }
1500 
1501 out:
1502         sock_put(sk); /* sock_hold above */
1503         return rc;
1504 }
1505 
1506 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1507                        int peer)
1508 {
1509         struct smc_sock *smc;
1510 
1511         if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1512             (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1513                 return -ENOTCONN;
1514 
1515         smc = smc_sk(sock->sk);
1516 
1517         return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
1518 }
1519 
1520 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1521 {
1522         struct sock *sk = sock->sk;
1523         struct smc_sock *smc;
1524         int rc = -EPIPE;
1525 
1526         smc = smc_sk(sk);
1527         lock_sock(sk);
1528         if ((sk->sk_state != SMC_ACTIVE) &&
1529             (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1530             (sk->sk_state != SMC_INIT))
1531                 goto out;
1532 
1533         if (msg->msg_flags & MSG_FASTOPEN) {
1534                 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1535                         smc_switch_to_fallback(smc);
1536                         smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1537                 } else {
1538                         rc = -EINVAL;
1539                         goto out;
1540                 }
1541         }
1542 
1543         if (smc->use_fallback)
1544                 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1545         else
1546                 rc = smc_tx_sendmsg(smc, msg, len);
1547 out:
1548         release_sock(sk);
1549         return rc;
1550 }
1551 
1552 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1553                        int flags)
1554 {
1555         struct sock *sk = sock->sk;
1556         struct smc_sock *smc;
1557         int rc = -ENOTCONN;
1558 
1559         smc = smc_sk(sk);
1560         lock_sock(sk);
1561         if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1562                 /* socket was connected before, no more data to read */
1563                 rc = 0;
1564                 goto out;
1565         }
1566         if ((sk->sk_state == SMC_INIT) ||
1567             (sk->sk_state == SMC_LISTEN) ||
1568             (sk->sk_state == SMC_CLOSED))
1569                 goto out;
1570 
1571         if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1572                 rc = 0;
1573                 goto out;
1574         }
1575 
1576         if (smc->use_fallback) {
1577                 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1578         } else {
1579                 msg->msg_namelen = 0;
1580                 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1581         }
1582 
1583 out:
1584         release_sock(sk);
1585         return rc;
1586 }
1587 
1588 static __poll_t smc_accept_poll(struct sock *parent)
1589 {
1590         struct smc_sock *isk = smc_sk(parent);
1591         __poll_t mask = 0;
1592 
1593         spin_lock(&isk->accept_q_lock);
1594         if (!list_empty(&isk->accept_q))
1595                 mask = EPOLLIN | EPOLLRDNORM;
1596         spin_unlock(&isk->accept_q_lock);
1597 
1598         return mask;
1599 }
1600 
1601 static __poll_t smc_poll(struct file *file, struct socket *sock,
1602                              poll_table *wait)
1603 {
1604         struct sock *sk = sock->sk;
1605         struct smc_sock *smc;
1606         __poll_t mask = 0;
1607 
1608         if (!sk)
1609                 return EPOLLNVAL;
1610 
1611         smc = smc_sk(sock->sk);
1612         if (smc->use_fallback) {
1613                 /* delegate to CLC child sock */
1614                 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1615                 sk->sk_err = smc->clcsock->sk->sk_err;
1616         } else {
1617                 if (sk->sk_state != SMC_CLOSED)
1618                         sock_poll_wait(file, sock, wait);
1619                 if (sk->sk_err)
1620                         mask |= EPOLLERR;
1621                 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1622                     (sk->sk_state == SMC_CLOSED))
1623                         mask |= EPOLLHUP;
1624                 if (sk->sk_state == SMC_LISTEN) {
1625                         /* woken up by sk_data_ready in smc_listen_work() */
1626                         mask |= smc_accept_poll(sk);
1627                 } else if (smc->use_fallback) { /* as result of connect_work()*/
1628                         mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1629                                                            wait);
1630                         sk->sk_err = smc->clcsock->sk->sk_err;
1631                 } else {
1632                         if ((sk->sk_state != SMC_INIT &&
1633                              atomic_read(&smc->conn.sndbuf_space)) ||
1634                             sk->sk_shutdown & SEND_SHUTDOWN) {
1635                                 mask |= EPOLLOUT | EPOLLWRNORM;
1636                         } else {
1637                                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1638                                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1639                         }
1640                         if (atomic_read(&smc->conn.bytes_to_rcv))
1641                                 mask |= EPOLLIN | EPOLLRDNORM;
1642                         if (sk->sk_shutdown & RCV_SHUTDOWN)
1643                                 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1644                         if (sk->sk_state == SMC_APPCLOSEWAIT1)
1645                                 mask |= EPOLLIN;
1646                         if (smc->conn.urg_state == SMC_URG_VALID)
1647                                 mask |= EPOLLPRI;
1648                 }
1649         }
1650 
1651         return mask;
1652 }
1653 
1654 static int smc_shutdown(struct socket *sock, int how)
1655 {
1656         struct sock *sk = sock->sk;
1657         struct smc_sock *smc;
1658         int rc = -EINVAL;
1659         int rc1 = 0;
1660 
1661         smc = smc_sk(sk);
1662 
1663         if ((how < SHUT_RD) || (how > SHUT_RDWR))
1664                 return rc;
1665 
1666         lock_sock(sk);
1667 
1668         rc = -ENOTCONN;
1669         if ((sk->sk_state != SMC_ACTIVE) &&
1670             (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1671             (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1672             (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1673             (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1674             (sk->sk_state != SMC_APPFINCLOSEWAIT))
1675                 goto out;
1676         if (smc->use_fallback) {
1677                 rc = kernel_sock_shutdown(smc->clcsock, how);
1678                 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1679                 if (sk->sk_shutdown == SHUTDOWN_MASK)
1680                         sk->sk_state = SMC_CLOSED;
1681                 goto out;
1682         }
1683         switch (how) {
1684         case SHUT_RDWR:         /* shutdown in both directions */
1685                 rc = smc_close_active(smc);
1686                 break;
1687         case SHUT_WR:
1688                 rc = smc_close_shutdown_write(smc);
1689                 break;
1690         case SHUT_RD:
1691                 rc = 0;
1692                 /* nothing more to do because peer is not involved */
1693                 break;
1694         }
1695         if (smc->clcsock)
1696                 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1697         /* map sock_shutdown_cmd constants to sk_shutdown value range */
1698         sk->sk_shutdown |= how + 1;
1699 
1700 out:
1701         release_sock(sk);
1702         return rc ? rc : rc1;
1703 }
1704 
1705 static int smc_setsockopt(struct socket *sock, int level, int optname,
1706                           char __user *optval, unsigned int optlen)
1707 {
1708         struct sock *sk = sock->sk;
1709         struct smc_sock *smc;
1710         int val, rc;
1711 
1712         smc = smc_sk(sk);
1713 
1714         /* generic setsockopts reaching us here always apply to the
1715          * CLC socket
1716          */
1717         rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1718                                            optval, optlen);
1719         if (smc->clcsock->sk->sk_err) {
1720                 sk->sk_err = smc->clcsock->sk->sk_err;
1721                 sk->sk_error_report(sk);
1722         }
1723 
1724         if (optlen < sizeof(int))
1725                 return -EINVAL;
1726         if (get_user(val, (int __user *)optval))
1727                 return -EFAULT;
1728 
1729         lock_sock(sk);
1730         if (rc || smc->use_fallback)
1731                 goto out;
1732         switch (optname) {
1733         case TCP_ULP:
1734         case TCP_FASTOPEN:
1735         case TCP_FASTOPEN_CONNECT:
1736         case TCP_FASTOPEN_KEY:
1737         case TCP_FASTOPEN_NO_COOKIE:
1738                 /* option not supported by SMC */
1739                 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1740                         smc_switch_to_fallback(smc);
1741                         smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1742                 } else {
1743                         rc = -EINVAL;
1744                 }
1745                 break;
1746         case TCP_NODELAY:
1747                 if (sk->sk_state != SMC_INIT &&
1748                     sk->sk_state != SMC_LISTEN &&
1749                     sk->sk_state != SMC_CLOSED) {
1750                         if (val)
1751                                 mod_delayed_work(system_wq, &smc->conn.tx_work,
1752                                                  0);
1753                 }
1754                 break;
1755         case TCP_CORK:
1756                 if (sk->sk_state != SMC_INIT &&
1757                     sk->sk_state != SMC_LISTEN &&
1758                     sk->sk_state != SMC_CLOSED) {
1759                         if (!val)
1760                                 mod_delayed_work(system_wq, &smc->conn.tx_work,
1761                                                  0);
1762                 }
1763                 break;
1764         case TCP_DEFER_ACCEPT:
1765                 smc->sockopt_defer_accept = val;
1766                 break;
1767         default:
1768                 break;
1769         }
1770 out:
1771         release_sock(sk);
1772 
1773         return rc;
1774 }
1775 
1776 static int smc_getsockopt(struct socket *sock, int level, int optname,
1777                           char __user *optval, int __user *optlen)
1778 {
1779         struct smc_sock *smc;
1780 
1781         smc = smc_sk(sock->sk);
1782         /* socket options apply to the CLC socket */
1783         return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1784                                              optval, optlen);
1785 }
1786 
1787 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1788                      unsigned long arg)
1789 {
1790         union smc_host_cursor cons, urg;
1791         struct smc_connection *conn;
1792         struct smc_sock *smc;
1793         int answ;
1794 
1795         smc = smc_sk(sock->sk);
1796         conn = &smc->conn;
1797         lock_sock(&smc->sk);
1798         if (smc->use_fallback) {
1799                 if (!smc->clcsock) {
1800                         release_sock(&smc->sk);
1801                         return -EBADF;
1802                 }
1803                 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1804                 release_sock(&smc->sk);
1805                 return answ;
1806         }
1807         switch (cmd) {
1808         case SIOCINQ: /* same as FIONREAD */
1809                 if (smc->sk.sk_state == SMC_LISTEN) {
1810                         release_sock(&smc->sk);
1811                         return -EINVAL;
1812                 }
1813                 if (smc->sk.sk_state == SMC_INIT ||
1814                     smc->sk.sk_state == SMC_CLOSED)
1815                         answ = 0;
1816                 else
1817                         answ = atomic_read(&smc->conn.bytes_to_rcv);
1818                 break;
1819         case SIOCOUTQ:
1820                 /* output queue size (not send + not acked) */
1821                 if (smc->sk.sk_state == SMC_LISTEN) {
1822                         release_sock(&smc->sk);
1823                         return -EINVAL;
1824                 }
1825                 if (smc->sk.sk_state == SMC_INIT ||
1826                     smc->sk.sk_state == SMC_CLOSED)
1827                         answ = 0;
1828                 else
1829                         answ = smc->conn.sndbuf_desc->len -
1830                                         atomic_read(&smc->conn.sndbuf_space);
1831                 break;
1832         case SIOCOUTQNSD:
1833                 /* output queue size (not send only) */
1834                 if (smc->sk.sk_state == SMC_LISTEN) {
1835                         release_sock(&smc->sk);
1836                         return -EINVAL;
1837                 }
1838                 if (smc->sk.sk_state == SMC_INIT ||
1839                     smc->sk.sk_state == SMC_CLOSED)
1840                         answ = 0;
1841                 else
1842                         answ = smc_tx_prepared_sends(&smc->conn);
1843                 break;
1844         case SIOCATMARK:
1845                 if (smc->sk.sk_state == SMC_LISTEN) {
1846                         release_sock(&smc->sk);
1847                         return -EINVAL;
1848                 }
1849                 if (smc->sk.sk_state == SMC_INIT ||
1850                     smc->sk.sk_state == SMC_CLOSED) {
1851                         answ = 0;
1852                 } else {
1853                         smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1854                         smc_curs_copy(&urg, &conn->urg_curs, conn);
1855                         answ = smc_curs_diff(conn->rmb_desc->len,
1856                                              &cons, &urg) == 1;
1857                 }
1858                 break;
1859         default:
1860                 release_sock(&smc->sk);
1861                 return -ENOIOCTLCMD;
1862         }
1863         release_sock(&smc->sk);
1864 
1865         return put_user(answ, (int __user *)arg);
1866 }
1867 
1868 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1869                             int offset, size_t size, int flags)
1870 {
1871         struct sock *sk = sock->sk;
1872         struct smc_sock *smc;
1873         int rc = -EPIPE;
1874 
1875         smc = smc_sk(sk);
1876         lock_sock(sk);
1877         if (sk->sk_state != SMC_ACTIVE) {
1878                 release_sock(sk);
1879                 goto out;
1880         }
1881         release_sock(sk);
1882         if (smc->use_fallback)
1883                 rc = kernel_sendpage(smc->clcsock, page, offset,
1884                                      size, flags);
1885         else
1886                 rc = sock_no_sendpage(sock, page, offset, size, flags);
1887 
1888 out:
1889         return rc;
1890 }
1891 
1892 /* Map the affected portions of the rmbe into an spd, note the number of bytes
1893  * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1894  * updates till whenever a respective page has been fully processed.
1895  * Note that subsequent recv() calls have to wait till all splice() processing
1896  * completed.
1897  */
1898 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1899                                struct pipe_inode_info *pipe, size_t len,
1900                                unsigned int flags)
1901 {
1902         struct sock *sk = sock->sk;
1903         struct smc_sock *smc;
1904         int rc = -ENOTCONN;
1905 
1906         smc = smc_sk(sk);
1907         lock_sock(sk);
1908         if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1909                 /* socket was connected before, no more data to read */
1910                 rc = 0;
1911                 goto out;
1912         }
1913         if (sk->sk_state == SMC_INIT ||
1914             sk->sk_state == SMC_LISTEN ||
1915             sk->sk_state == SMC_CLOSED)
1916                 goto out;
1917 
1918         if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1919                 rc = 0;
1920                 goto out;
1921         }
1922 
1923         if (smc->use_fallback) {
1924                 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1925                                                     pipe, len, flags);
1926         } else {
1927                 if (*ppos) {
1928                         rc = -ESPIPE;
1929                         goto out;
1930                 }
1931                 if (flags & SPLICE_F_NONBLOCK)
1932                         flags = MSG_DONTWAIT;
1933                 else
1934                         flags = 0;
1935                 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
1936         }
1937 out:
1938         release_sock(sk);
1939 
1940         return rc;
1941 }
1942 
1943 /* must look like tcp */
1944 static const struct proto_ops smc_sock_ops = {
1945         .family         = PF_SMC,
1946         .owner          = THIS_MODULE,
1947         .release        = smc_release,
1948         .bind           = smc_bind,
1949         .connect        = smc_connect,
1950         .socketpair     = sock_no_socketpair,
1951         .accept         = smc_accept,
1952         .getname        = smc_getname,
1953         .poll           = smc_poll,
1954         .ioctl          = smc_ioctl,
1955         .listen         = smc_listen,
1956         .shutdown       = smc_shutdown,
1957         .setsockopt     = smc_setsockopt,
1958         .getsockopt     = smc_getsockopt,
1959         .sendmsg        = smc_sendmsg,
1960         .recvmsg        = smc_recvmsg,
1961         .mmap           = sock_no_mmap,
1962         .sendpage       = smc_sendpage,
1963         .splice_read    = smc_splice_read,
1964 };
1965 
1966 static int smc_create(struct net *net, struct socket *sock, int protocol,
1967                       int kern)
1968 {
1969         int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
1970         struct smc_sock *smc;
1971         struct sock *sk;
1972         int rc;
1973 
1974         rc = -ESOCKTNOSUPPORT;
1975         if (sock->type != SOCK_STREAM)
1976                 goto out;
1977 
1978         rc = -EPROTONOSUPPORT;
1979         if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
1980                 goto out;
1981 
1982         rc = -ENOBUFS;
1983         sock->ops = &smc_sock_ops;
1984         sk = smc_sock_alloc(net, sock, protocol);
1985         if (!sk)
1986                 goto out;
1987 
1988         /* create internal TCP socket for CLC handshake and fallback */
1989         smc = smc_sk(sk);
1990         smc->use_fallback = false; /* assume rdma capability first */
1991         smc->fallback_rsn = 0;
1992         rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1993                               &smc->clcsock);
1994         if (rc) {
1995                 sk_common_release(sk);
1996                 goto out;
1997         }
1998         smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1999         smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
2000 
2001 out:
2002         return rc;
2003 }
2004 
2005 static const struct net_proto_family smc_sock_family_ops = {
2006         .family = PF_SMC,
2007         .owner  = THIS_MODULE,
2008         .create = smc_create,
2009 };
2010 
2011 unsigned int smc_net_id;
2012 
2013 static __net_init int smc_net_init(struct net *net)
2014 {
2015         return smc_pnet_net_init(net);
2016 }
2017 
2018 static void __net_exit smc_net_exit(struct net *net)
2019 {
2020         smc_pnet_net_exit(net);
2021 }
2022 
2023 static struct pernet_operations smc_net_ops = {
2024         .init = smc_net_init,
2025         .exit = smc_net_exit,
2026         .id   = &smc_net_id,
2027         .size = sizeof(struct smc_net),
2028 };
2029 
2030 static int __init smc_init(void)
2031 {
2032         int rc;
2033 
2034         rc = register_pernet_subsys(&smc_net_ops);
2035         if (rc)
2036                 return rc;
2037 
2038         rc = smc_pnet_init();
2039         if (rc)
2040                 goto out_pernet_subsys;
2041 
2042         rc = smc_llc_init();
2043         if (rc) {
2044                 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2045                 goto out_pnet;
2046         }
2047 
2048         rc = smc_cdc_init();
2049         if (rc) {
2050                 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2051                 goto out_pnet;
2052         }
2053 
2054         rc = proto_register(&smc_proto, 1);
2055         if (rc) {
2056                 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2057                 goto out_pnet;
2058         }
2059 
2060         rc = proto_register(&smc_proto6, 1);
2061         if (rc) {
2062                 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2063                 goto out_proto;
2064         }
2065 
2066         rc = sock_register(&smc_sock_family_ops);
2067         if (rc) {
2068                 pr_err("%s: sock_register fails with %d\n", __func__, rc);
2069                 goto out_proto6;
2070         }
2071         INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2072         INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2073 
2074         rc = smc_ib_register_client();
2075         if (rc) {
2076                 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2077                 goto out_sock;
2078         }
2079 
2080         static_branch_enable(&tcp_have_smc);
2081         return 0;
2082 
2083 out_sock:
2084         sock_unregister(PF_SMC);
2085 out_proto6:
2086         proto_unregister(&smc_proto6);
2087 out_proto:
2088         proto_unregister(&smc_proto);
2089 out_pnet:
2090         smc_pnet_exit();
2091 out_pernet_subsys:
2092         unregister_pernet_subsys(&smc_net_ops);
2093 
2094         return rc;
2095 }
2096 
2097 static void __exit smc_exit(void)
2098 {
2099         smc_core_exit();
2100         static_branch_disable(&tcp_have_smc);
2101         smc_ib_unregister_client();
2102         sock_unregister(PF_SMC);
2103         proto_unregister(&smc_proto6);
2104         proto_unregister(&smc_proto);
2105         smc_pnet_exit();
2106         unregister_pernet_subsys(&smc_net_ops);
2107 }
2108 
2109 module_init(smc_init);
2110 module_exit(smc_exit);
2111 
2112 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2113 MODULE_DESCRIPTION("smc socket address family");
2114 MODULE_LICENSE("GPL");
2115 MODULE_ALIAS_NETPROTO(PF_SMC);

/* [<][>][^][v][top][bottom][index][help] */