1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Shared Memory Communications over RDMA (SMC-R) and RoCE 4 * 5 * Basic Transport Functions exploiting Infiniband API 6 * 7 * Copyright IBM Corp. 2016 8 * 9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> 10 */ 11 12 #include <linux/socket.h> 13 #include <linux/if_vlan.h> 14 #include <linux/random.h> 15 #include <linux/workqueue.h> 16 #include <net/tcp.h> 17 #include <net/sock.h> 18 #include <rdma/ib_verbs.h> 19 #include <rdma/ib_cache.h> 20 21 #include "smc.h" 22 #include "smc_clc.h" 23 #include "smc_core.h" 24 #include "smc_ib.h" 25 #include "smc_wr.h" 26 #include "smc_llc.h" 27 #include "smc_cdc.h" 28 #include "smc_close.h" 29 #include "smc_ism.h" 30 31 #define SMC_LGR_NUM_INCR 256 32 #define SMC_LGR_FREE_DELAY_SERV (600 * HZ) 33 #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10 * HZ) 34 #define SMC_LGR_FREE_DELAY_FAST (8 * HZ) 35 36 static struct smc_lgr_list smc_lgr_list = { /* established link groups */ 37 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock), 38 .list = LIST_HEAD_INIT(smc_lgr_list.list), 39 .num = 0, 40 }; 41 42 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb, 43 struct smc_buf_desc *buf_desc); 44 45 static void smc_lgr_schedule_free_work(struct smc_link_group *lgr) 46 { 47 /* client link group creation always follows the server link group 48 * creation. For client use a somewhat higher removal delay time, 49 * otherwise there is a risk of out-of-sync link groups. 50 */ 51 mod_delayed_work(system_wq, &lgr->free_work, 52 (!lgr->is_smcd && lgr->role == SMC_CLNT) ? 53 SMC_LGR_FREE_DELAY_CLNT : SMC_LGR_FREE_DELAY_SERV); 54 } 55 56 void smc_lgr_schedule_free_work_fast(struct smc_link_group *lgr) 57 { 58 mod_delayed_work(system_wq, &lgr->free_work, SMC_LGR_FREE_DELAY_FAST); 59 } 60 61 /* Register connection's alert token in our lookup structure. 62 * To use rbtrees we have to implement our own insert core. 63 * Requires @conns_lock 64 * @smc connection to register 65 * Returns 0 on success, != otherwise. 66 */ 67 static void smc_lgr_add_alert_token(struct smc_connection *conn) 68 { 69 struct rb_node **link, *parent = NULL; 70 u32 token = conn->alert_token_local; 71 72 link = &conn->lgr->conns_all.rb_node; 73 while (*link) { 74 struct smc_connection *cur = rb_entry(*link, 75 struct smc_connection, alert_node); 76 77 parent = *link; 78 if (cur->alert_token_local > token) 79 link = &parent->rb_left; 80 else 81 link = &parent->rb_right; 82 } 83 /* Put the new node there */ 84 rb_link_node(&conn->alert_node, parent, link); 85 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all); 86 } 87 88 /* Register connection in link group by assigning an alert token 89 * registered in a search tree. 90 * Requires @conns_lock 91 * Note that '0' is a reserved value and not assigned. 92 */ 93 static void smc_lgr_register_conn(struct smc_connection *conn) 94 { 95 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 96 static atomic_t nexttoken = ATOMIC_INIT(0); 97 98 /* find a new alert_token_local value not yet used by some connection 99 * in this link group 100 */ 101 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */ 102 while (!conn->alert_token_local) { 103 conn->alert_token_local = atomic_inc_return(&nexttoken); 104 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr)) 105 conn->alert_token_local = 0; 106 } 107 smc_lgr_add_alert_token(conn); 108 conn->lgr->conns_num++; 109 } 110 111 /* Unregister connection and reset the alert token of the given connection< 112 */ 113 static void __smc_lgr_unregister_conn(struct smc_connection *conn) 114 { 115 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 116 struct smc_link_group *lgr = conn->lgr; 117 118 rb_erase(&conn->alert_node, &lgr->conns_all); 119 lgr->conns_num--; 120 conn->alert_token_local = 0; 121 conn->lgr = NULL; 122 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */ 123 } 124 125 /* Unregister connection from lgr 126 */ 127 static void smc_lgr_unregister_conn(struct smc_connection *conn) 128 { 129 struct smc_link_group *lgr = conn->lgr; 130 131 write_lock_bh(&lgr->conns_lock); 132 if (conn->alert_token_local) { 133 __smc_lgr_unregister_conn(conn); 134 } 135 write_unlock_bh(&lgr->conns_lock); 136 } 137 138 /* Send delete link, either as client to request the initiation 139 * of the DELETE LINK sequence from server; or as server to 140 * initiate the delete processing. See smc_llc_rx_delete_link(). 141 */ 142 static int smc_link_send_delete(struct smc_link *lnk) 143 { 144 if (lnk->state == SMC_LNK_ACTIVE && 145 !smc_llc_send_delete_link(lnk, SMC_LLC_REQ, true)) { 146 smc_llc_link_deleting(lnk); 147 return 0; 148 } 149 return -ENOTCONN; 150 } 151 152 static void smc_lgr_free_work(struct work_struct *work) 153 { 154 struct smc_link_group *lgr = container_of(to_delayed_work(work), 155 struct smc_link_group, 156 free_work); 157 bool conns; 158 159 spin_lock_bh(&smc_lgr_list.lock); 160 if (list_empty(&lgr->list)) 161 goto free; 162 read_lock_bh(&lgr->conns_lock); 163 conns = RB_EMPTY_ROOT(&lgr->conns_all); 164 read_unlock_bh(&lgr->conns_lock); 165 if (!conns) { /* number of lgr connections is no longer zero */ 166 spin_unlock_bh(&smc_lgr_list.lock); 167 return; 168 } 169 list_del_init(&lgr->list); /* remove from smc_lgr_list */ 170 free: 171 spin_unlock_bh(&smc_lgr_list.lock); 172 173 if (!lgr->is_smcd && !lgr->terminating) { 174 /* try to send del link msg, on error free lgr immediately */ 175 if (!smc_link_send_delete(&lgr->lnk[SMC_SINGLE_LINK])) { 176 /* reschedule in case we never receive a response */ 177 smc_lgr_schedule_free_work(lgr); 178 return; 179 } 180 } 181 182 if (!delayed_work_pending(&lgr->free_work)) { 183 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; 184 185 if (!lgr->is_smcd && lnk->state != SMC_LNK_INACTIVE) 186 smc_llc_link_inactive(lnk); 187 smc_lgr_free(lgr); 188 } 189 } 190 191 /* create a new SMC link group */ 192 static int smc_lgr_create(struct smc_sock *smc, bool is_smcd, 193 struct smc_ib_device *smcibdev, u8 ibport, 194 char *peer_systemid, unsigned short vlan_id, 195 struct smcd_dev *smcismdev, u64 peer_gid) 196 { 197 struct smc_link_group *lgr; 198 struct smc_link *lnk; 199 u8 rndvec[3]; 200 int rc = 0; 201 int i; 202 203 if (is_smcd && vlan_id) { 204 rc = smc_ism_get_vlan(smcismdev, vlan_id); 205 if (rc) 206 goto out; 207 } 208 209 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL); 210 if (!lgr) { 211 rc = -ENOMEM; 212 goto out; 213 } 214 lgr->is_smcd = is_smcd; 215 lgr->sync_err = 0; 216 lgr->vlan_id = vlan_id; 217 rwlock_init(&lgr->sndbufs_lock); 218 rwlock_init(&lgr->rmbs_lock); 219 rwlock_init(&lgr->conns_lock); 220 for (i = 0; i < SMC_RMBE_SIZES; i++) { 221 INIT_LIST_HEAD(&lgr->sndbufs[i]); 222 INIT_LIST_HEAD(&lgr->rmbs[i]); 223 } 224 smc_lgr_list.num += SMC_LGR_NUM_INCR; 225 memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE); 226 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work); 227 lgr->conns_all = RB_ROOT; 228 if (is_smcd) { 229 /* SMC-D specific settings */ 230 lgr->peer_gid = peer_gid; 231 lgr->smcd = smcismdev; 232 } else { 233 /* SMC-R specific settings */ 234 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT; 235 memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN); 236 237 lnk = &lgr->lnk[SMC_SINGLE_LINK]; 238 /* initialize link */ 239 lnk->state = SMC_LNK_ACTIVATING; 240 lnk->link_id = SMC_SINGLE_LINK; 241 lnk->smcibdev = smcibdev; 242 lnk->ibport = ibport; 243 lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu; 244 if (!smcibdev->initialized) 245 smc_ib_setup_per_ibdev(smcibdev); 246 get_random_bytes(rndvec, sizeof(rndvec)); 247 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + 248 (rndvec[2] << 16); 249 rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport, 250 vlan_id, lnk->gid, &lnk->sgid_index); 251 if (rc) 252 goto free_lgr; 253 rc = smc_llc_link_init(lnk); 254 if (rc) 255 goto free_lgr; 256 rc = smc_wr_alloc_link_mem(lnk); 257 if (rc) 258 goto clear_llc_lnk; 259 rc = smc_ib_create_protection_domain(lnk); 260 if (rc) 261 goto free_link_mem; 262 rc = smc_ib_create_queue_pair(lnk); 263 if (rc) 264 goto dealloc_pd; 265 rc = smc_wr_create_link(lnk); 266 if (rc) 267 goto destroy_qp; 268 } 269 smc->conn.lgr = lgr; 270 spin_lock_bh(&smc_lgr_list.lock); 271 list_add(&lgr->list, &smc_lgr_list.list); 272 spin_unlock_bh(&smc_lgr_list.lock); 273 return 0; 274 275 destroy_qp: 276 smc_ib_destroy_queue_pair(lnk); 277 dealloc_pd: 278 smc_ib_dealloc_protection_domain(lnk); 279 free_link_mem: 280 smc_wr_free_link_mem(lnk); 281 clear_llc_lnk: 282 smc_llc_link_clear(lnk); 283 free_lgr: 284 kfree(lgr); 285 out: 286 return rc; 287 } 288 289 static void smc_buf_unuse(struct smc_connection *conn, 290 struct smc_link_group *lgr) 291 { 292 if (conn->sndbuf_desc) 293 conn->sndbuf_desc->used = 0; 294 if (conn->rmb_desc) { 295 if (!conn->rmb_desc->regerr) { 296 conn->rmb_desc->reused = 1; 297 conn->rmb_desc->used = 0; 298 } else { 299 /* buf registration failed, reuse not possible */ 300 write_lock_bh(&lgr->rmbs_lock); 301 list_del(&conn->rmb_desc->list); 302 write_unlock_bh(&lgr->rmbs_lock); 303 304 smc_buf_free(lgr, true, conn->rmb_desc); 305 } 306 } 307 } 308 309 /* remove a finished connection from its link group */ 310 void smc_conn_free(struct smc_connection *conn) 311 { 312 struct smc_link_group *lgr = conn->lgr; 313 314 if (!lgr) 315 return; 316 if (lgr->is_smcd) { 317 smc_ism_unset_conn(conn); 318 tasklet_kill(&conn->rx_tsklet); 319 } else { 320 smc_cdc_tx_dismiss_slots(conn); 321 } 322 smc_lgr_unregister_conn(conn); /* unsets conn->lgr */ 323 smc_buf_unuse(conn, lgr); /* allow buffer reuse */ 324 325 if (!lgr->conns_num) 326 smc_lgr_schedule_free_work(lgr); 327 } 328 329 static void smc_link_clear(struct smc_link *lnk) 330 { 331 lnk->peer_qpn = 0; 332 smc_llc_link_clear(lnk); 333 smc_ib_modify_qp_reset(lnk); 334 smc_wr_free_link(lnk); 335 smc_ib_destroy_queue_pair(lnk); 336 smc_ib_dealloc_protection_domain(lnk); 337 smc_wr_free_link_mem(lnk); 338 } 339 340 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb, 341 struct smc_buf_desc *buf_desc) 342 { 343 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; 344 345 if (is_rmb) { 346 if (buf_desc->mr_rx[SMC_SINGLE_LINK]) 347 smc_ib_put_memory_region( 348 buf_desc->mr_rx[SMC_SINGLE_LINK]); 349 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc, 350 DMA_FROM_DEVICE); 351 } else { 352 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc, 353 DMA_TO_DEVICE); 354 } 355 sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]); 356 if (buf_desc->pages) 357 __free_pages(buf_desc->pages, buf_desc->order); 358 kfree(buf_desc); 359 } 360 361 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb, 362 struct smc_buf_desc *buf_desc) 363 { 364 if (is_dmb) { 365 /* restore original buf len */ 366 buf_desc->len += sizeof(struct smcd_cdc_msg); 367 smc_ism_unregister_dmb(lgr->smcd, buf_desc); 368 } else { 369 kfree(buf_desc->cpu_addr); 370 } 371 kfree(buf_desc); 372 } 373 374 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb, 375 struct smc_buf_desc *buf_desc) 376 { 377 if (lgr->is_smcd) 378 smcd_buf_free(lgr, is_rmb, buf_desc); 379 else 380 smcr_buf_free(lgr, is_rmb, buf_desc); 381 } 382 383 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb) 384 { 385 struct smc_buf_desc *buf_desc, *bf_desc; 386 struct list_head *buf_list; 387 int i; 388 389 for (i = 0; i < SMC_RMBE_SIZES; i++) { 390 if (is_rmb) 391 buf_list = &lgr->rmbs[i]; 392 else 393 buf_list = &lgr->sndbufs[i]; 394 list_for_each_entry_safe(buf_desc, bf_desc, buf_list, 395 list) { 396 list_del(&buf_desc->list); 397 smc_buf_free(lgr, is_rmb, buf_desc); 398 } 399 } 400 } 401 402 static void smc_lgr_free_bufs(struct smc_link_group *lgr) 403 { 404 /* free send buffers */ 405 __smc_lgr_free_bufs(lgr, false); 406 /* free rmbs */ 407 __smc_lgr_free_bufs(lgr, true); 408 } 409 410 /* remove a link group */ 411 void smc_lgr_free(struct smc_link_group *lgr) 412 { 413 smc_lgr_free_bufs(lgr); 414 if (lgr->is_smcd) 415 smc_ism_put_vlan(lgr->smcd, lgr->vlan_id); 416 else 417 smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]); 418 kfree(lgr); 419 } 420 421 void smc_lgr_forget(struct smc_link_group *lgr) 422 { 423 spin_lock_bh(&smc_lgr_list.lock); 424 /* do not use this link group for new connections */ 425 if (!list_empty(&lgr->list)) 426 list_del_init(&lgr->list); 427 spin_unlock_bh(&smc_lgr_list.lock); 428 } 429 430 /* terminate linkgroup abnormally */ 431 static void __smc_lgr_terminate(struct smc_link_group *lgr) 432 { 433 struct smc_connection *conn; 434 struct smc_sock *smc; 435 struct rb_node *node; 436 437 if (lgr->terminating) 438 return; /* lgr already terminating */ 439 lgr->terminating = 1; 440 if (!list_empty(&lgr->list)) /* forget lgr */ 441 list_del_init(&lgr->list); 442 if (!lgr->is_smcd) 443 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]); 444 445 write_lock_bh(&lgr->conns_lock); 446 node = rb_first(&lgr->conns_all); 447 while (node) { 448 conn = rb_entry(node, struct smc_connection, alert_node); 449 smc = container_of(conn, struct smc_sock, conn); 450 sock_hold(&smc->sk); /* sock_put in close work */ 451 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; 452 __smc_lgr_unregister_conn(conn); 453 write_unlock_bh(&lgr->conns_lock); 454 if (!schedule_work(&conn->close_work)) 455 sock_put(&smc->sk); 456 write_lock_bh(&lgr->conns_lock); 457 node = rb_first(&lgr->conns_all); 458 } 459 write_unlock_bh(&lgr->conns_lock); 460 if (!lgr->is_smcd) 461 wake_up(&lgr->lnk[SMC_SINGLE_LINK].wr_reg_wait); 462 smc_lgr_schedule_free_work(lgr); 463 } 464 465 void smc_lgr_terminate(struct smc_link_group *lgr) 466 { 467 spin_lock_bh(&smc_lgr_list.lock); 468 __smc_lgr_terminate(lgr); 469 spin_unlock_bh(&smc_lgr_list.lock); 470 } 471 472 /* Called when IB port is terminated */ 473 void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport) 474 { 475 struct smc_link_group *lgr, *l; 476 477 spin_lock_bh(&smc_lgr_list.lock); 478 list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) { 479 if (!lgr->is_smcd && 480 lgr->lnk[SMC_SINGLE_LINK].smcibdev == smcibdev && 481 lgr->lnk[SMC_SINGLE_LINK].ibport == ibport) 482 __smc_lgr_terminate(lgr); 483 } 484 spin_unlock_bh(&smc_lgr_list.lock); 485 } 486 487 /* Called when SMC-D device is terminated or peer is lost */ 488 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid) 489 { 490 struct smc_link_group *lgr, *l; 491 LIST_HEAD(lgr_free_list); 492 493 /* run common cleanup function and build free list */ 494 spin_lock_bh(&smc_lgr_list.lock); 495 list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) { 496 if (lgr->is_smcd && lgr->smcd == dev && 497 (!peer_gid || lgr->peer_gid == peer_gid) && 498 !list_empty(&lgr->list)) { 499 __smc_lgr_terminate(lgr); 500 list_move(&lgr->list, &lgr_free_list); 501 } 502 } 503 spin_unlock_bh(&smc_lgr_list.lock); 504 505 /* cancel the regular free workers and actually free lgrs */ 506 list_for_each_entry_safe(lgr, l, &lgr_free_list, list) { 507 list_del_init(&lgr->list); 508 cancel_delayed_work_sync(&lgr->free_work); 509 smc_lgr_free(lgr); 510 } 511 } 512 513 /* Determine vlan of internal TCP socket. 514 * @vlan_id: address to store the determined vlan id into 515 */ 516 int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id) 517 { 518 struct dst_entry *dst = sk_dst_get(clcsock->sk); 519 struct net_device *ndev; 520 int i, nest_lvl, rc = 0; 521 522 *vlan_id = 0; 523 if (!dst) { 524 rc = -ENOTCONN; 525 goto out; 526 } 527 if (!dst->dev) { 528 rc = -ENODEV; 529 goto out_rel; 530 } 531 532 ndev = dst->dev; 533 if (is_vlan_dev(ndev)) { 534 *vlan_id = vlan_dev_vlan_id(ndev); 535 goto out_rel; 536 } 537 538 rtnl_lock(); 539 nest_lvl = dev_get_nest_level(ndev); 540 for (i = 0; i < nest_lvl; i++) { 541 struct list_head *lower = &ndev->adj_list.lower; 542 543 if (list_empty(lower)) 544 break; 545 lower = lower->next; 546 ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower); 547 if (is_vlan_dev(ndev)) { 548 *vlan_id = vlan_dev_vlan_id(ndev); 549 break; 550 } 551 } 552 rtnl_unlock(); 553 554 out_rel: 555 dst_release(dst); 556 out: 557 return rc; 558 } 559 560 static bool smcr_lgr_match(struct smc_link_group *lgr, 561 struct smc_clc_msg_local *lcl, 562 enum smc_lgr_role role) 563 { 564 return !memcmp(lgr->peer_systemid, lcl->id_for_peer, 565 SMC_SYSTEMID_LEN) && 566 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid, 567 SMC_GID_SIZE) && 568 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac, 569 sizeof(lcl->mac)) && 570 lgr->role == role; 571 } 572 573 static bool smcd_lgr_match(struct smc_link_group *lgr, 574 struct smcd_dev *smcismdev, u64 peer_gid) 575 { 576 return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev; 577 } 578 579 /* create a new SMC connection (and a new link group if necessary) */ 580 int smc_conn_create(struct smc_sock *smc, bool is_smcd, int srv_first_contact, 581 struct smc_ib_device *smcibdev, u8 ibport, 582 struct smc_clc_msg_local *lcl, struct smcd_dev *smcd, 583 u64 peer_gid) 584 { 585 struct smc_connection *conn = &smc->conn; 586 int local_contact = SMC_FIRST_CONTACT; 587 struct smc_link_group *lgr; 588 unsigned short vlan_id; 589 enum smc_lgr_role role; 590 int rc = 0; 591 592 role = smc->listen_smc ? SMC_SERV : SMC_CLNT; 593 rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id); 594 if (rc) 595 return rc; 596 597 if ((role == SMC_CLNT) && srv_first_contact) 598 /* create new link group as well */ 599 goto create; 600 601 /* determine if an existing link group can be reused */ 602 spin_lock_bh(&smc_lgr_list.lock); 603 list_for_each_entry(lgr, &smc_lgr_list.list, list) { 604 write_lock_bh(&lgr->conns_lock); 605 if ((is_smcd ? smcd_lgr_match(lgr, smcd, peer_gid) : 606 smcr_lgr_match(lgr, lcl, role)) && 607 !lgr->sync_err && 608 lgr->vlan_id == vlan_id && 609 (role == SMC_CLNT || 610 lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) { 611 /* link group found */ 612 local_contact = SMC_REUSE_CONTACT; 613 conn->lgr = lgr; 614 smc_lgr_register_conn(conn); /* add smc conn to lgr */ 615 write_unlock_bh(&lgr->conns_lock); 616 break; 617 } 618 write_unlock_bh(&lgr->conns_lock); 619 } 620 spin_unlock_bh(&smc_lgr_list.lock); 621 622 if (role == SMC_CLNT && !srv_first_contact && 623 (local_contact == SMC_FIRST_CONTACT)) { 624 /* Server reuses a link group, but Client wants to start 625 * a new one 626 * send out_of_sync decline, reason synchr. error 627 */ 628 return -ENOLINK; 629 } 630 631 create: 632 if (local_contact == SMC_FIRST_CONTACT) { 633 rc = smc_lgr_create(smc, is_smcd, smcibdev, ibport, 634 lcl->id_for_peer, vlan_id, smcd, peer_gid); 635 if (rc) 636 goto out; 637 smc_lgr_register_conn(conn); /* add smc conn to lgr */ 638 } 639 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE; 640 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE; 641 conn->urg_state = SMC_URG_READ; 642 if (is_smcd) { 643 conn->rx_off = sizeof(struct smcd_cdc_msg); 644 smcd_cdc_rx_init(conn); /* init tasklet for this conn */ 645 } 646 #ifndef KERNEL_HAS_ATOMIC64 647 spin_lock_init(&conn->acurs_lock); 648 #endif 649 650 out: 651 return rc ? rc : local_contact; 652 } 653 654 /* convert the RMB size into the compressed notation - minimum 16K. 655 * In contrast to plain ilog2, this rounds towards the next power of 2, 656 * so the socket application gets at least its desired sndbuf / rcvbuf size. 657 */ 658 static u8 smc_compress_bufsize(int size) 659 { 660 u8 compressed; 661 662 if (size <= SMC_BUF_MIN_SIZE) 663 return 0; 664 665 size = (size - 1) >> 14; 666 compressed = ilog2(size) + 1; 667 if (compressed >= SMC_RMBE_SIZES) 668 compressed = SMC_RMBE_SIZES - 1; 669 return compressed; 670 } 671 672 /* convert the RMB size from compressed notation into integer */ 673 int smc_uncompress_bufsize(u8 compressed) 674 { 675 u32 size; 676 677 size = 0x00000001 << (((int)compressed) + 14); 678 return (int)size; 679 } 680 681 /* try to reuse a sndbuf or rmb description slot for a certain 682 * buffer size; if not available, return NULL 683 */ 684 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize, 685 rwlock_t *lock, 686 struct list_head *buf_list) 687 { 688 struct smc_buf_desc *buf_slot; 689 690 read_lock_bh(lock); 691 list_for_each_entry(buf_slot, buf_list, list) { 692 if (cmpxchg(&buf_slot->used, 0, 1) == 0) { 693 read_unlock_bh(lock); 694 return buf_slot; 695 } 696 } 697 read_unlock_bh(lock); 698 return NULL; 699 } 700 701 /* one of the conditions for announcing a receiver's current window size is 702 * that it "results in a minimum increase in the window size of 10% of the 703 * receive buffer space" [RFC7609] 704 */ 705 static inline int smc_rmb_wnd_update_limit(int rmbe_size) 706 { 707 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2); 708 } 709 710 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr, 711 bool is_rmb, int bufsize) 712 { 713 struct smc_buf_desc *buf_desc; 714 struct smc_link *lnk; 715 int rc; 716 717 /* try to alloc a new buffer */ 718 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL); 719 if (!buf_desc) 720 return ERR_PTR(-ENOMEM); 721 722 buf_desc->order = get_order(bufsize); 723 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN | 724 __GFP_NOMEMALLOC | __GFP_COMP | 725 __GFP_NORETRY | __GFP_ZERO, 726 buf_desc->order); 727 if (!buf_desc->pages) { 728 kfree(buf_desc); 729 return ERR_PTR(-EAGAIN); 730 } 731 buf_desc->cpu_addr = (void *)page_address(buf_desc->pages); 732 733 /* build the sg table from the pages */ 734 lnk = &lgr->lnk[SMC_SINGLE_LINK]; 735 rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1, 736 GFP_KERNEL); 737 if (rc) { 738 smc_buf_free(lgr, is_rmb, buf_desc); 739 return ERR_PTR(rc); 740 } 741 sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl, 742 buf_desc->cpu_addr, bufsize); 743 744 /* map sg table to DMA address */ 745 rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc, 746 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 747 /* SMC protocol depends on mapping to one DMA address only */ 748 if (rc != 1) { 749 smc_buf_free(lgr, is_rmb, buf_desc); 750 return ERR_PTR(-EAGAIN); 751 } 752 753 /* create a new memory region for the RMB */ 754 if (is_rmb) { 755 rc = smc_ib_get_memory_region(lnk->roce_pd, 756 IB_ACCESS_REMOTE_WRITE | 757 IB_ACCESS_LOCAL_WRITE, 758 buf_desc); 759 if (rc) { 760 smc_buf_free(lgr, is_rmb, buf_desc); 761 return ERR_PTR(rc); 762 } 763 } 764 765 buf_desc->len = bufsize; 766 return buf_desc; 767 } 768 769 #define SMCD_DMBE_SIZES 7 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */ 770 771 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr, 772 bool is_dmb, int bufsize) 773 { 774 struct smc_buf_desc *buf_desc; 775 int rc; 776 777 if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES) 778 return ERR_PTR(-EAGAIN); 779 780 /* try to alloc a new DMB */ 781 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL); 782 if (!buf_desc) 783 return ERR_PTR(-ENOMEM); 784 if (is_dmb) { 785 rc = smc_ism_register_dmb(lgr, bufsize, buf_desc); 786 if (rc) { 787 kfree(buf_desc); 788 return ERR_PTR(-EAGAIN); 789 } 790 buf_desc->pages = virt_to_page(buf_desc->cpu_addr); 791 /* CDC header stored in buf. So, pretend it was smaller */ 792 buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg); 793 } else { 794 buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL | 795 __GFP_NOWARN | __GFP_NORETRY | 796 __GFP_NOMEMALLOC); 797 if (!buf_desc->cpu_addr) { 798 kfree(buf_desc); 799 return ERR_PTR(-EAGAIN); 800 } 801 buf_desc->len = bufsize; 802 } 803 return buf_desc; 804 } 805 806 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb) 807 { 808 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM); 809 struct smc_connection *conn = &smc->conn; 810 struct smc_link_group *lgr = conn->lgr; 811 struct list_head *buf_list; 812 int bufsize, bufsize_short; 813 int sk_buf_size; 814 rwlock_t *lock; 815 816 if (is_rmb) 817 /* use socket recv buffer size (w/o overhead) as start value */ 818 sk_buf_size = smc->sk.sk_rcvbuf / 2; 819 else 820 /* use socket send buffer size (w/o overhead) as start value */ 821 sk_buf_size = smc->sk.sk_sndbuf / 2; 822 823 for (bufsize_short = smc_compress_bufsize(sk_buf_size); 824 bufsize_short >= 0; bufsize_short--) { 825 826 if (is_rmb) { 827 lock = &lgr->rmbs_lock; 828 buf_list = &lgr->rmbs[bufsize_short]; 829 } else { 830 lock = &lgr->sndbufs_lock; 831 buf_list = &lgr->sndbufs[bufsize_short]; 832 } 833 bufsize = smc_uncompress_bufsize(bufsize_short); 834 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC) 835 continue; 836 837 /* check for reusable slot in the link group */ 838 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list); 839 if (buf_desc) { 840 memset(buf_desc->cpu_addr, 0, bufsize); 841 break; /* found reusable slot */ 842 } 843 844 if (is_smcd) 845 buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize); 846 else 847 buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize); 848 849 if (PTR_ERR(buf_desc) == -ENOMEM) 850 break; 851 if (IS_ERR(buf_desc)) 852 continue; 853 854 buf_desc->used = 1; 855 write_lock_bh(lock); 856 list_add(&buf_desc->list, buf_list); 857 write_unlock_bh(lock); 858 break; /* found */ 859 } 860 861 if (IS_ERR(buf_desc)) 862 return -ENOMEM; 863 864 if (is_rmb) { 865 conn->rmb_desc = buf_desc; 866 conn->rmbe_size_short = bufsize_short; 867 smc->sk.sk_rcvbuf = bufsize * 2; 868 atomic_set(&conn->bytes_to_rcv, 0); 869 conn->rmbe_update_limit = 870 smc_rmb_wnd_update_limit(buf_desc->len); 871 if (is_smcd) 872 smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */ 873 } else { 874 conn->sndbuf_desc = buf_desc; 875 smc->sk.sk_sndbuf = bufsize * 2; 876 atomic_set(&conn->sndbuf_space, bufsize); 877 } 878 return 0; 879 } 880 881 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn) 882 { 883 struct smc_link_group *lgr = conn->lgr; 884 885 if (!conn->lgr || conn->lgr->is_smcd) 886 return; 887 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev, 888 conn->sndbuf_desc, DMA_TO_DEVICE); 889 } 890 891 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn) 892 { 893 struct smc_link_group *lgr = conn->lgr; 894 895 if (!conn->lgr || conn->lgr->is_smcd) 896 return; 897 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev, 898 conn->sndbuf_desc, DMA_TO_DEVICE); 899 } 900 901 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn) 902 { 903 struct smc_link_group *lgr = conn->lgr; 904 905 if (!conn->lgr || conn->lgr->is_smcd) 906 return; 907 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev, 908 conn->rmb_desc, DMA_FROM_DEVICE); 909 } 910 911 void smc_rmb_sync_sg_for_device(struct smc_connection *conn) 912 { 913 struct smc_link_group *lgr = conn->lgr; 914 915 if (!conn->lgr || conn->lgr->is_smcd) 916 return; 917 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev, 918 conn->rmb_desc, DMA_FROM_DEVICE); 919 } 920 921 /* create the send and receive buffer for an SMC socket; 922 * receive buffers are called RMBs; 923 * (even though the SMC protocol allows more than one RMB-element per RMB, 924 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an 925 * extra RMB for every connection in a link group 926 */ 927 int smc_buf_create(struct smc_sock *smc, bool is_smcd) 928 { 929 int rc; 930 931 /* create send buffer */ 932 rc = __smc_buf_create(smc, is_smcd, false); 933 if (rc) 934 return rc; 935 /* create rmb */ 936 rc = __smc_buf_create(smc, is_smcd, true); 937 if (rc) 938 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc); 939 return rc; 940 } 941 942 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr) 943 { 944 int i; 945 946 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) { 947 if (!test_and_set_bit(i, lgr->rtokens_used_mask)) 948 return i; 949 } 950 return -ENOSPC; 951 } 952 953 /* add a new rtoken from peer */ 954 int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey) 955 { 956 u64 dma_addr = be64_to_cpu(nw_vaddr); 957 u32 rkey = ntohl(nw_rkey); 958 int i; 959 960 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { 961 if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) && 962 (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) && 963 test_bit(i, lgr->rtokens_used_mask)) { 964 /* already in list */ 965 return i; 966 } 967 } 968 i = smc_rmb_reserve_rtoken_idx(lgr); 969 if (i < 0) 970 return i; 971 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey; 972 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr; 973 return i; 974 } 975 976 /* delete an rtoken */ 977 int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey) 978 { 979 u32 rkey = ntohl(nw_rkey); 980 int i; 981 982 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { 983 if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey && 984 test_bit(i, lgr->rtokens_used_mask)) { 985 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0; 986 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0; 987 988 clear_bit(i, lgr->rtokens_used_mask); 989 return 0; 990 } 991 } 992 return -ENOENT; 993 } 994 995 /* save rkey and dma_addr received from peer during clc handshake */ 996 int smc_rmb_rtoken_handling(struct smc_connection *conn, 997 struct smc_clc_msg_accept_confirm *clc) 998 { 999 conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr, 1000 clc->rmb_rkey); 1001 if (conn->rtoken_idx < 0) 1002 return conn->rtoken_idx; 1003 return 0; 1004 } 1005 1006 /* Called (from smc_exit) when module is removed */ 1007 void smc_core_exit(void) 1008 { 1009 struct smc_link_group *lgr, *lg; 1010 LIST_HEAD(lgr_freeing_list); 1011 1012 spin_lock_bh(&smc_lgr_list.lock); 1013 if (!list_empty(&smc_lgr_list.list)) 1014 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list); 1015 spin_unlock_bh(&smc_lgr_list.lock); 1016 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) { 1017 list_del_init(&lgr->list); 1018 if (!lgr->is_smcd) { 1019 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; 1020 1021 if (lnk->state == SMC_LNK_ACTIVE) 1022 smc_llc_send_delete_link(lnk, SMC_LLC_REQ, 1023 false); 1024 smc_llc_link_inactive(lnk); 1025 } 1026 cancel_delayed_work_sync(&lgr->free_work); 1027 smc_lgr_free(lgr); /* free link group */ 1028 } 1029 } 1030