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 <linux/wait.h> 17 #include <linux/reboot.h> 18 #include <linux/mutex.h> 19 #include <linux/list.h> 20 #include <linux/smc.h> 21 #include <net/tcp.h> 22 #include <net/sock.h> 23 #include <rdma/ib_verbs.h> 24 #include <rdma/ib_cache.h> 25 26 #include "smc.h" 27 #include "smc_clc.h" 28 #include "smc_core.h" 29 #include "smc_ib.h" 30 #include "smc_wr.h" 31 #include "smc_llc.h" 32 #include "smc_cdc.h" 33 #include "smc_close.h" 34 #include "smc_ism.h" 35 #include "smc_netlink.h" 36 37 #define SMC_LGR_NUM_INCR 256 38 #define SMC_LGR_FREE_DELAY_SERV (600 * HZ) 39 #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10 * HZ) 40 41 struct smc_lgr_list smc_lgr_list = { /* established link groups */ 42 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock), 43 .list = LIST_HEAD_INIT(smc_lgr_list.list), 44 .num = 0, 45 }; 46 47 static atomic_t lgr_cnt = ATOMIC_INIT(0); /* number of existing link groups */ 48 static DECLARE_WAIT_QUEUE_HEAD(lgrs_deleted); 49 50 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb, 51 struct smc_buf_desc *buf_desc); 52 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft); 53 54 static void smc_link_down_work(struct work_struct *work); 55 56 /* return head of link group list and its lock for a given link group */ 57 static inline struct list_head *smc_lgr_list_head(struct smc_link_group *lgr, 58 spinlock_t **lgr_lock) 59 { 60 if (lgr->is_smcd) { 61 *lgr_lock = &lgr->smcd->lgr_lock; 62 return &lgr->smcd->lgr_list; 63 } 64 65 *lgr_lock = &smc_lgr_list.lock; 66 return &smc_lgr_list.list; 67 } 68 69 static void smc_ibdev_cnt_inc(struct smc_link *lnk) 70 { 71 atomic_inc(&lnk->smcibdev->lnk_cnt_by_port[lnk->ibport - 1]); 72 } 73 74 static void smc_ibdev_cnt_dec(struct smc_link *lnk) 75 { 76 atomic_dec(&lnk->smcibdev->lnk_cnt_by_port[lnk->ibport - 1]); 77 } 78 79 static void smc_lgr_schedule_free_work(struct smc_link_group *lgr) 80 { 81 /* client link group creation always follows the server link group 82 * creation. For client use a somewhat higher removal delay time, 83 * otherwise there is a risk of out-of-sync link groups. 84 */ 85 if (!lgr->freeing) { 86 mod_delayed_work(system_wq, &lgr->free_work, 87 (!lgr->is_smcd && lgr->role == SMC_CLNT) ? 88 SMC_LGR_FREE_DELAY_CLNT : 89 SMC_LGR_FREE_DELAY_SERV); 90 } 91 } 92 93 /* Register connection's alert token in our lookup structure. 94 * To use rbtrees we have to implement our own insert core. 95 * Requires @conns_lock 96 * @smc connection to register 97 * Returns 0 on success, != otherwise. 98 */ 99 static void smc_lgr_add_alert_token(struct smc_connection *conn) 100 { 101 struct rb_node **link, *parent = NULL; 102 u32 token = conn->alert_token_local; 103 104 link = &conn->lgr->conns_all.rb_node; 105 while (*link) { 106 struct smc_connection *cur = rb_entry(*link, 107 struct smc_connection, alert_node); 108 109 parent = *link; 110 if (cur->alert_token_local > token) 111 link = &parent->rb_left; 112 else 113 link = &parent->rb_right; 114 } 115 /* Put the new node there */ 116 rb_link_node(&conn->alert_node, parent, link); 117 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all); 118 } 119 120 /* assign an SMC-R link to the connection */ 121 static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first) 122 { 123 enum smc_link_state expected = first ? SMC_LNK_ACTIVATING : 124 SMC_LNK_ACTIVE; 125 int i, j; 126 127 /* do link balancing */ 128 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 129 struct smc_link *lnk = &conn->lgr->lnk[i]; 130 131 if (lnk->state != expected || lnk->link_is_asym) 132 continue; 133 if (conn->lgr->role == SMC_CLNT) { 134 conn->lnk = lnk; /* temporary, SMC server assigns link*/ 135 break; 136 } 137 if (conn->lgr->conns_num % 2) { 138 for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) { 139 struct smc_link *lnk2; 140 141 lnk2 = &conn->lgr->lnk[j]; 142 if (lnk2->state == expected && 143 !lnk2->link_is_asym) { 144 conn->lnk = lnk2; 145 break; 146 } 147 } 148 } 149 if (!conn->lnk) 150 conn->lnk = lnk; 151 break; 152 } 153 if (!conn->lnk) 154 return SMC_CLC_DECL_NOACTLINK; 155 atomic_inc(&conn->lnk->conn_cnt); 156 return 0; 157 } 158 159 /* Register connection in link group by assigning an alert token 160 * registered in a search tree. 161 * Requires @conns_lock 162 * Note that '0' is a reserved value and not assigned. 163 */ 164 static int smc_lgr_register_conn(struct smc_connection *conn, bool first) 165 { 166 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 167 static atomic_t nexttoken = ATOMIC_INIT(0); 168 int rc; 169 170 if (!conn->lgr->is_smcd) { 171 rc = smcr_lgr_conn_assign_link(conn, first); 172 if (rc) 173 return rc; 174 } 175 /* find a new alert_token_local value not yet used by some connection 176 * in this link group 177 */ 178 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */ 179 while (!conn->alert_token_local) { 180 conn->alert_token_local = atomic_inc_return(&nexttoken); 181 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr)) 182 conn->alert_token_local = 0; 183 } 184 smc_lgr_add_alert_token(conn); 185 conn->lgr->conns_num++; 186 return 0; 187 } 188 189 /* Unregister connection and reset the alert token of the given connection< 190 */ 191 static void __smc_lgr_unregister_conn(struct smc_connection *conn) 192 { 193 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 194 struct smc_link_group *lgr = conn->lgr; 195 196 rb_erase(&conn->alert_node, &lgr->conns_all); 197 if (conn->lnk) 198 atomic_dec(&conn->lnk->conn_cnt); 199 lgr->conns_num--; 200 conn->alert_token_local = 0; 201 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */ 202 } 203 204 /* Unregister connection from lgr 205 */ 206 static void smc_lgr_unregister_conn(struct smc_connection *conn) 207 { 208 struct smc_link_group *lgr = conn->lgr; 209 210 if (!lgr) 211 return; 212 write_lock_bh(&lgr->conns_lock); 213 if (conn->alert_token_local) { 214 __smc_lgr_unregister_conn(conn); 215 } 216 write_unlock_bh(&lgr->conns_lock); 217 conn->lgr = NULL; 218 } 219 220 int smc_nl_get_sys_info(struct sk_buff *skb, struct netlink_callback *cb) 221 { 222 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); 223 char hostname[SMC_MAX_HOSTNAME_LEN + 1]; 224 char smc_seid[SMC_MAX_EID_LEN + 1]; 225 struct smcd_dev *smcd_dev; 226 struct nlattr *attrs; 227 u8 *seid = NULL; 228 u8 *host = NULL; 229 void *nlh; 230 231 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 232 &smc_gen_nl_family, NLM_F_MULTI, 233 SMC_NETLINK_GET_SYS_INFO); 234 if (!nlh) 235 goto errmsg; 236 if (cb_ctx->pos[0]) 237 goto errout; 238 attrs = nla_nest_start(skb, SMC_GEN_SYS_INFO); 239 if (!attrs) 240 goto errout; 241 if (nla_put_u8(skb, SMC_NLA_SYS_VER, SMC_V2)) 242 goto errattr; 243 if (nla_put_u8(skb, SMC_NLA_SYS_REL, SMC_RELEASE)) 244 goto errattr; 245 if (nla_put_u8(skb, SMC_NLA_SYS_IS_ISM_V2, smc_ism_is_v2_capable())) 246 goto errattr; 247 smc_clc_get_hostname(&host); 248 if (host) { 249 memcpy(hostname, host, SMC_MAX_HOSTNAME_LEN); 250 hostname[SMC_MAX_HOSTNAME_LEN] = 0; 251 if (nla_put_string(skb, SMC_NLA_SYS_LOCAL_HOST, hostname)) 252 goto errattr; 253 } 254 mutex_lock(&smcd_dev_list.mutex); 255 smcd_dev = list_first_entry_or_null(&smcd_dev_list.list, 256 struct smcd_dev, list); 257 if (smcd_dev) 258 smc_ism_get_system_eid(smcd_dev, &seid); 259 mutex_unlock(&smcd_dev_list.mutex); 260 if (seid && smc_ism_is_v2_capable()) { 261 memcpy(smc_seid, seid, SMC_MAX_EID_LEN); 262 smc_seid[SMC_MAX_EID_LEN] = 0; 263 if (nla_put_string(skb, SMC_NLA_SYS_SEID, smc_seid)) 264 goto errattr; 265 } 266 nla_nest_end(skb, attrs); 267 genlmsg_end(skb, nlh); 268 cb_ctx->pos[0] = 1; 269 return skb->len; 270 271 errattr: 272 nla_nest_cancel(skb, attrs); 273 errout: 274 genlmsg_cancel(skb, nlh); 275 errmsg: 276 return skb->len; 277 } 278 279 static int smc_nl_fill_lgr(struct smc_link_group *lgr, 280 struct sk_buff *skb, 281 struct netlink_callback *cb) 282 { 283 char smc_target[SMC_MAX_PNETID_LEN + 1]; 284 struct nlattr *attrs; 285 286 attrs = nla_nest_start(skb, SMC_GEN_LGR_SMCR); 287 if (!attrs) 288 goto errout; 289 290 if (nla_put_u32(skb, SMC_NLA_LGR_R_ID, *((u32 *)&lgr->id))) 291 goto errattr; 292 if (nla_put_u32(skb, SMC_NLA_LGR_R_CONNS_NUM, lgr->conns_num)) 293 goto errattr; 294 if (nla_put_u8(skb, SMC_NLA_LGR_R_ROLE, lgr->role)) 295 goto errattr; 296 if (nla_put_u8(skb, SMC_NLA_LGR_R_TYPE, lgr->type)) 297 goto errattr; 298 if (nla_put_u8(skb, SMC_NLA_LGR_R_VLAN_ID, lgr->vlan_id)) 299 goto errattr; 300 memcpy(smc_target, lgr->pnet_id, SMC_MAX_PNETID_LEN); 301 smc_target[SMC_MAX_PNETID_LEN] = 0; 302 if (nla_put_string(skb, SMC_NLA_LGR_R_PNETID, smc_target)) 303 goto errattr; 304 305 nla_nest_end(skb, attrs); 306 return 0; 307 errattr: 308 nla_nest_cancel(skb, attrs); 309 errout: 310 return -EMSGSIZE; 311 } 312 313 static int smc_nl_fill_lgr_link(struct smc_link_group *lgr, 314 struct smc_link *link, 315 struct sk_buff *skb, 316 struct netlink_callback *cb) 317 { 318 char smc_ibname[IB_DEVICE_NAME_MAX]; 319 u8 smc_gid_target[41]; 320 struct nlattr *attrs; 321 u32 link_uid = 0; 322 void *nlh; 323 324 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 325 &smc_gen_nl_family, NLM_F_MULTI, 326 SMC_NETLINK_GET_LINK_SMCR); 327 if (!nlh) 328 goto errmsg; 329 330 attrs = nla_nest_start(skb, SMC_GEN_LINK_SMCR); 331 if (!attrs) 332 goto errout; 333 334 if (nla_put_u8(skb, SMC_NLA_LINK_ID, link->link_id)) 335 goto errattr; 336 if (nla_put_u32(skb, SMC_NLA_LINK_STATE, link->state)) 337 goto errattr; 338 if (nla_put_u32(skb, SMC_NLA_LINK_CONN_CNT, 339 atomic_read(&link->conn_cnt))) 340 goto errattr; 341 if (nla_put_u8(skb, SMC_NLA_LINK_IB_PORT, link->ibport)) 342 goto errattr; 343 if (nla_put_u32(skb, SMC_NLA_LINK_NET_DEV, link->ndev_ifidx)) 344 goto errattr; 345 snprintf(smc_ibname, sizeof(smc_ibname), "%s", link->ibname); 346 if (nla_put_string(skb, SMC_NLA_LINK_IB_DEV, smc_ibname)) 347 goto errattr; 348 memcpy(&link_uid, link->link_uid, sizeof(link_uid)); 349 if (nla_put_u32(skb, SMC_NLA_LINK_UID, link_uid)) 350 goto errattr; 351 memcpy(&link_uid, link->peer_link_uid, sizeof(link_uid)); 352 if (nla_put_u32(skb, SMC_NLA_LINK_PEER_UID, link_uid)) 353 goto errattr; 354 memset(smc_gid_target, 0, sizeof(smc_gid_target)); 355 smc_gid_be16_convert(smc_gid_target, link->gid); 356 if (nla_put_string(skb, SMC_NLA_LINK_GID, smc_gid_target)) 357 goto errattr; 358 memset(smc_gid_target, 0, sizeof(smc_gid_target)); 359 smc_gid_be16_convert(smc_gid_target, link->peer_gid); 360 if (nla_put_string(skb, SMC_NLA_LINK_PEER_GID, smc_gid_target)) 361 goto errattr; 362 363 nla_nest_end(skb, attrs); 364 genlmsg_end(skb, nlh); 365 return 0; 366 errattr: 367 nla_nest_cancel(skb, attrs); 368 errout: 369 genlmsg_cancel(skb, nlh); 370 errmsg: 371 return -EMSGSIZE; 372 } 373 374 static int smc_nl_handle_lgr(struct smc_link_group *lgr, 375 struct sk_buff *skb, 376 struct netlink_callback *cb, 377 bool list_links) 378 { 379 void *nlh; 380 int i; 381 382 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 383 &smc_gen_nl_family, NLM_F_MULTI, 384 SMC_NETLINK_GET_LGR_SMCR); 385 if (!nlh) 386 goto errmsg; 387 if (smc_nl_fill_lgr(lgr, skb, cb)) 388 goto errout; 389 390 genlmsg_end(skb, nlh); 391 if (!list_links) 392 goto out; 393 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 394 if (!smc_link_usable(&lgr->lnk[i])) 395 continue; 396 if (smc_nl_fill_lgr_link(lgr, &lgr->lnk[i], skb, cb)) 397 goto errout; 398 } 399 out: 400 return 0; 401 402 errout: 403 genlmsg_cancel(skb, nlh); 404 errmsg: 405 return -EMSGSIZE; 406 } 407 408 static void smc_nl_fill_lgr_list(struct smc_lgr_list *smc_lgr, 409 struct sk_buff *skb, 410 struct netlink_callback *cb, 411 bool list_links) 412 { 413 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); 414 struct smc_link_group *lgr; 415 int snum = cb_ctx->pos[0]; 416 int num = 0; 417 418 spin_lock_bh(&smc_lgr->lock); 419 list_for_each_entry(lgr, &smc_lgr->list, list) { 420 if (num < snum) 421 goto next; 422 if (smc_nl_handle_lgr(lgr, skb, cb, list_links)) 423 goto errout; 424 next: 425 num++; 426 } 427 errout: 428 spin_unlock_bh(&smc_lgr->lock); 429 cb_ctx->pos[0] = num; 430 } 431 432 static int smc_nl_fill_smcd_lgr(struct smc_link_group *lgr, 433 struct sk_buff *skb, 434 struct netlink_callback *cb) 435 { 436 char smc_host[SMC_MAX_HOSTNAME_LEN + 1]; 437 char smc_pnet[SMC_MAX_PNETID_LEN + 1]; 438 char smc_eid[SMC_MAX_EID_LEN + 1]; 439 struct nlattr *v2_attrs; 440 struct nlattr *attrs; 441 void *nlh; 442 443 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 444 &smc_gen_nl_family, NLM_F_MULTI, 445 SMC_NETLINK_GET_LGR_SMCD); 446 if (!nlh) 447 goto errmsg; 448 449 attrs = nla_nest_start(skb, SMC_GEN_LGR_SMCD); 450 if (!attrs) 451 goto errout; 452 453 if (nla_put_u32(skb, SMC_NLA_LGR_D_ID, *((u32 *)&lgr->id))) 454 goto errattr; 455 if (nla_put_u64_64bit(skb, SMC_NLA_LGR_D_GID, lgr->smcd->local_gid, 456 SMC_NLA_LGR_D_PAD)) 457 goto errattr; 458 if (nla_put_u64_64bit(skb, SMC_NLA_LGR_D_PEER_GID, lgr->peer_gid, 459 SMC_NLA_LGR_D_PAD)) 460 goto errattr; 461 if (nla_put_u8(skb, SMC_NLA_LGR_D_VLAN_ID, lgr->vlan_id)) 462 goto errattr; 463 if (nla_put_u32(skb, SMC_NLA_LGR_D_CONNS_NUM, lgr->conns_num)) 464 goto errattr; 465 if (nla_put_u32(skb, SMC_NLA_LGR_D_CHID, smc_ism_get_chid(lgr->smcd))) 466 goto errattr; 467 memcpy(smc_pnet, lgr->smcd->pnetid, SMC_MAX_PNETID_LEN); 468 smc_pnet[SMC_MAX_PNETID_LEN] = 0; 469 if (nla_put_string(skb, SMC_NLA_LGR_D_PNETID, smc_pnet)) 470 goto errattr; 471 472 v2_attrs = nla_nest_start(skb, SMC_NLA_LGR_V2); 473 if (!v2_attrs) 474 goto errattr; 475 if (nla_put_u8(skb, SMC_NLA_LGR_V2_VER, lgr->smc_version)) 476 goto errv2attr; 477 if (nla_put_u8(skb, SMC_NLA_LGR_V2_REL, lgr->peer_smc_release)) 478 goto errv2attr; 479 if (nla_put_u8(skb, SMC_NLA_LGR_V2_OS, lgr->peer_os)) 480 goto errv2attr; 481 memcpy(smc_host, lgr->peer_hostname, SMC_MAX_HOSTNAME_LEN); 482 smc_host[SMC_MAX_HOSTNAME_LEN] = 0; 483 if (nla_put_string(skb, SMC_NLA_LGR_V2_PEER_HOST, smc_host)) 484 goto errv2attr; 485 memcpy(smc_eid, lgr->negotiated_eid, SMC_MAX_EID_LEN); 486 smc_eid[SMC_MAX_EID_LEN] = 0; 487 if (nla_put_string(skb, SMC_NLA_LGR_V2_NEG_EID, smc_eid)) 488 goto errv2attr; 489 490 nla_nest_end(skb, v2_attrs); 491 nla_nest_end(skb, attrs); 492 genlmsg_end(skb, nlh); 493 return 0; 494 495 errv2attr: 496 nla_nest_cancel(skb, v2_attrs); 497 errattr: 498 nla_nest_cancel(skb, attrs); 499 errout: 500 genlmsg_cancel(skb, nlh); 501 errmsg: 502 return -EMSGSIZE; 503 } 504 505 static int smc_nl_handle_smcd_lgr(struct smcd_dev *dev, 506 struct sk_buff *skb, 507 struct netlink_callback *cb) 508 { 509 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); 510 struct smc_link_group *lgr; 511 int snum = cb_ctx->pos[1]; 512 int rc = 0, num = 0; 513 514 spin_lock_bh(&dev->lgr_lock); 515 list_for_each_entry(lgr, &dev->lgr_list, list) { 516 if (!lgr->is_smcd) 517 continue; 518 if (num < snum) 519 goto next; 520 rc = smc_nl_fill_smcd_lgr(lgr, skb, cb); 521 if (rc) 522 goto errout; 523 next: 524 num++; 525 } 526 errout: 527 spin_unlock_bh(&dev->lgr_lock); 528 cb_ctx->pos[1] = num; 529 return rc; 530 } 531 532 static int smc_nl_fill_smcd_dev(struct smcd_dev_list *dev_list, 533 struct sk_buff *skb, 534 struct netlink_callback *cb) 535 { 536 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); 537 struct smcd_dev *smcd_dev; 538 int snum = cb_ctx->pos[0]; 539 int rc = 0, num = 0; 540 541 mutex_lock(&dev_list->mutex); 542 list_for_each_entry(smcd_dev, &dev_list->list, list) { 543 if (list_empty(&smcd_dev->lgr_list)) 544 continue; 545 if (num < snum) 546 goto next; 547 rc = smc_nl_handle_smcd_lgr(smcd_dev, skb, cb); 548 if (rc) 549 goto errout; 550 next: 551 num++; 552 } 553 errout: 554 mutex_unlock(&dev_list->mutex); 555 cb_ctx->pos[0] = num; 556 return rc; 557 } 558 559 int smcr_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb) 560 { 561 bool list_links = false; 562 563 smc_nl_fill_lgr_list(&smc_lgr_list, skb, cb, list_links); 564 return skb->len; 565 } 566 567 int smcr_nl_get_link(struct sk_buff *skb, struct netlink_callback *cb) 568 { 569 bool list_links = true; 570 571 smc_nl_fill_lgr_list(&smc_lgr_list, skb, cb, list_links); 572 return skb->len; 573 } 574 575 int smcd_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb) 576 { 577 smc_nl_fill_smcd_dev(&smcd_dev_list, skb, cb); 578 return skb->len; 579 } 580 581 void smc_lgr_cleanup_early(struct smc_connection *conn) 582 { 583 struct smc_link_group *lgr = conn->lgr; 584 struct list_head *lgr_list; 585 spinlock_t *lgr_lock; 586 587 if (!lgr) 588 return; 589 590 smc_conn_free(conn); 591 lgr_list = smc_lgr_list_head(lgr, &lgr_lock); 592 spin_lock_bh(lgr_lock); 593 /* do not use this link group for new connections */ 594 if (!list_empty(lgr_list)) 595 list_del_init(lgr_list); 596 spin_unlock_bh(lgr_lock); 597 __smc_lgr_terminate(lgr, true); 598 } 599 600 static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr) 601 { 602 int i; 603 604 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 605 struct smc_link *lnk = &lgr->lnk[i]; 606 607 if (smc_link_usable(lnk)) 608 lnk->state = SMC_LNK_INACTIVE; 609 } 610 wake_up_all(&lgr->llc_msg_waiter); 611 wake_up_all(&lgr->llc_flow_waiter); 612 } 613 614 static void smc_lgr_free(struct smc_link_group *lgr); 615 616 static void smc_lgr_free_work(struct work_struct *work) 617 { 618 struct smc_link_group *lgr = container_of(to_delayed_work(work), 619 struct smc_link_group, 620 free_work); 621 spinlock_t *lgr_lock; 622 bool conns; 623 624 smc_lgr_list_head(lgr, &lgr_lock); 625 spin_lock_bh(lgr_lock); 626 if (lgr->freeing) { 627 spin_unlock_bh(lgr_lock); 628 return; 629 } 630 read_lock_bh(&lgr->conns_lock); 631 conns = RB_EMPTY_ROOT(&lgr->conns_all); 632 read_unlock_bh(&lgr->conns_lock); 633 if (!conns) { /* number of lgr connections is no longer zero */ 634 spin_unlock_bh(lgr_lock); 635 return; 636 } 637 list_del_init(&lgr->list); /* remove from smc_lgr_list */ 638 lgr->freeing = 1; /* this instance does the freeing, no new schedule */ 639 spin_unlock_bh(lgr_lock); 640 cancel_delayed_work(&lgr->free_work); 641 642 if (!lgr->is_smcd && !lgr->terminating) 643 smc_llc_send_link_delete_all(lgr, true, 644 SMC_LLC_DEL_PROG_INIT_TERM); 645 if (lgr->is_smcd && !lgr->terminating) 646 smc_ism_signal_shutdown(lgr); 647 if (!lgr->is_smcd) 648 smcr_lgr_link_deactivate_all(lgr); 649 smc_lgr_free(lgr); 650 } 651 652 static void smc_lgr_terminate_work(struct work_struct *work) 653 { 654 struct smc_link_group *lgr = container_of(work, struct smc_link_group, 655 terminate_work); 656 657 __smc_lgr_terminate(lgr, true); 658 } 659 660 /* return next unique link id for the lgr */ 661 static u8 smcr_next_link_id(struct smc_link_group *lgr) 662 { 663 u8 link_id; 664 int i; 665 666 while (1) { 667 link_id = ++lgr->next_link_id; 668 if (!link_id) /* skip zero as link_id */ 669 link_id = ++lgr->next_link_id; 670 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 671 if (smc_link_usable(&lgr->lnk[i]) && 672 lgr->lnk[i].link_id == link_id) 673 continue; 674 } 675 break; 676 } 677 return link_id; 678 } 679 680 static void smcr_copy_dev_info_to_link(struct smc_link *link) 681 { 682 struct smc_ib_device *smcibdev = link->smcibdev; 683 684 snprintf(link->ibname, sizeof(link->ibname), "%s", 685 smcibdev->ibdev->name); 686 link->ndev_ifidx = smcibdev->ndev_ifidx[link->ibport - 1]; 687 } 688 689 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk, 690 u8 link_idx, struct smc_init_info *ini) 691 { 692 u8 rndvec[3]; 693 int rc; 694 695 get_device(&ini->ib_dev->ibdev->dev); 696 atomic_inc(&ini->ib_dev->lnk_cnt); 697 lnk->link_id = smcr_next_link_id(lgr); 698 lnk->lgr = lgr; 699 lnk->link_idx = link_idx; 700 lnk->smcibdev = ini->ib_dev; 701 lnk->ibport = ini->ib_port; 702 smc_ibdev_cnt_inc(lnk); 703 smcr_copy_dev_info_to_link(lnk); 704 lnk->path_mtu = ini->ib_dev->pattr[ini->ib_port - 1].active_mtu; 705 atomic_set(&lnk->conn_cnt, 0); 706 smc_llc_link_set_uid(lnk); 707 INIT_WORK(&lnk->link_down_wrk, smc_link_down_work); 708 if (!ini->ib_dev->initialized) { 709 rc = (int)smc_ib_setup_per_ibdev(ini->ib_dev); 710 if (rc) 711 goto out; 712 } 713 get_random_bytes(rndvec, sizeof(rndvec)); 714 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + 715 (rndvec[2] << 16); 716 rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport, 717 ini->vlan_id, lnk->gid, &lnk->sgid_index); 718 if (rc) 719 goto out; 720 rc = smc_llc_link_init(lnk); 721 if (rc) 722 goto out; 723 rc = smc_wr_alloc_link_mem(lnk); 724 if (rc) 725 goto clear_llc_lnk; 726 rc = smc_ib_create_protection_domain(lnk); 727 if (rc) 728 goto free_link_mem; 729 rc = smc_ib_create_queue_pair(lnk); 730 if (rc) 731 goto dealloc_pd; 732 rc = smc_wr_create_link(lnk); 733 if (rc) 734 goto destroy_qp; 735 lnk->state = SMC_LNK_ACTIVATING; 736 return 0; 737 738 destroy_qp: 739 smc_ib_destroy_queue_pair(lnk); 740 dealloc_pd: 741 smc_ib_dealloc_protection_domain(lnk); 742 free_link_mem: 743 smc_wr_free_link_mem(lnk); 744 clear_llc_lnk: 745 smc_llc_link_clear(lnk, false); 746 out: 747 smc_ibdev_cnt_dec(lnk); 748 put_device(&ini->ib_dev->ibdev->dev); 749 memset(lnk, 0, sizeof(struct smc_link)); 750 lnk->state = SMC_LNK_UNUSED; 751 if (!atomic_dec_return(&ini->ib_dev->lnk_cnt)) 752 wake_up(&ini->ib_dev->lnks_deleted); 753 return rc; 754 } 755 756 /* create a new SMC link group */ 757 static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini) 758 { 759 struct smc_link_group *lgr; 760 struct list_head *lgr_list; 761 struct smc_link *lnk; 762 spinlock_t *lgr_lock; 763 u8 link_idx; 764 int rc = 0; 765 int i; 766 767 if (ini->is_smcd && ini->vlan_id) { 768 if (smc_ism_get_vlan(ini->ism_dev[ini->ism_selected], 769 ini->vlan_id)) { 770 rc = SMC_CLC_DECL_ISMVLANERR; 771 goto out; 772 } 773 } 774 775 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL); 776 if (!lgr) { 777 rc = SMC_CLC_DECL_MEM; 778 goto ism_put_vlan; 779 } 780 lgr->tx_wq = alloc_workqueue("smc_tx_wq-%*phN", 0, 0, 781 SMC_LGR_ID_SIZE, &lgr->id); 782 if (!lgr->tx_wq) { 783 rc = -ENOMEM; 784 goto free_lgr; 785 } 786 lgr->is_smcd = ini->is_smcd; 787 lgr->sync_err = 0; 788 lgr->terminating = 0; 789 lgr->freeing = 0; 790 lgr->vlan_id = ini->vlan_id; 791 mutex_init(&lgr->sndbufs_lock); 792 mutex_init(&lgr->rmbs_lock); 793 rwlock_init(&lgr->conns_lock); 794 for (i = 0; i < SMC_RMBE_SIZES; i++) { 795 INIT_LIST_HEAD(&lgr->sndbufs[i]); 796 INIT_LIST_HEAD(&lgr->rmbs[i]); 797 } 798 lgr->next_link_id = 0; 799 smc_lgr_list.num += SMC_LGR_NUM_INCR; 800 memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE); 801 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work); 802 INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work); 803 lgr->conns_all = RB_ROOT; 804 if (ini->is_smcd) { 805 /* SMC-D specific settings */ 806 get_device(&ini->ism_dev[ini->ism_selected]->dev); 807 lgr->peer_gid = ini->ism_peer_gid[ini->ism_selected]; 808 lgr->smcd = ini->ism_dev[ini->ism_selected]; 809 lgr_list = &ini->ism_dev[ini->ism_selected]->lgr_list; 810 lgr_lock = &lgr->smcd->lgr_lock; 811 lgr->smc_version = ini->smcd_version; 812 lgr->peer_shutdown = 0; 813 atomic_inc(&ini->ism_dev[ini->ism_selected]->lgr_cnt); 814 } else { 815 /* SMC-R specific settings */ 816 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT; 817 memcpy(lgr->peer_systemid, ini->ib_lcl->id_for_peer, 818 SMC_SYSTEMID_LEN); 819 memcpy(lgr->pnet_id, ini->ib_dev->pnetid[ini->ib_port - 1], 820 SMC_MAX_PNETID_LEN); 821 smc_llc_lgr_init(lgr, smc); 822 823 link_idx = SMC_SINGLE_LINK; 824 lnk = &lgr->lnk[link_idx]; 825 rc = smcr_link_init(lgr, lnk, link_idx, ini); 826 if (rc) 827 goto free_wq; 828 lgr_list = &smc_lgr_list.list; 829 lgr_lock = &smc_lgr_list.lock; 830 atomic_inc(&lgr_cnt); 831 } 832 smc->conn.lgr = lgr; 833 spin_lock_bh(lgr_lock); 834 list_add_tail(&lgr->list, lgr_list); 835 spin_unlock_bh(lgr_lock); 836 return 0; 837 838 free_wq: 839 destroy_workqueue(lgr->tx_wq); 840 free_lgr: 841 kfree(lgr); 842 ism_put_vlan: 843 if (ini->is_smcd && ini->vlan_id) 844 smc_ism_put_vlan(ini->ism_dev[ini->ism_selected], ini->vlan_id); 845 out: 846 if (rc < 0) { 847 if (rc == -ENOMEM) 848 rc = SMC_CLC_DECL_MEM; 849 else 850 rc = SMC_CLC_DECL_INTERR; 851 } 852 return rc; 853 } 854 855 static int smc_write_space(struct smc_connection *conn) 856 { 857 int buffer_len = conn->peer_rmbe_size; 858 union smc_host_cursor prod; 859 union smc_host_cursor cons; 860 int space; 861 862 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn); 863 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn); 864 /* determine rx_buf space */ 865 space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod); 866 return space; 867 } 868 869 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, 870 struct smc_wr_buf *wr_buf) 871 { 872 struct smc_connection *conn = &smc->conn; 873 union smc_host_cursor cons, fin; 874 int rc = 0; 875 int diff; 876 877 smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn); 878 smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn); 879 /* set prod cursor to old state, enforce tx_rdma_writes() */ 880 smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn); 881 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn); 882 883 if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) { 884 /* cons cursor advanced more than fin, and prod was set 885 * fin above, so now prod is smaller than cons. Fix that. 886 */ 887 diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons); 888 smc_curs_add(conn->sndbuf_desc->len, 889 &conn->tx_curs_sent, diff); 890 smc_curs_add(conn->sndbuf_desc->len, 891 &conn->tx_curs_fin, diff); 892 893 smp_mb__before_atomic(); 894 atomic_add(diff, &conn->sndbuf_space); 895 smp_mb__after_atomic(); 896 897 smc_curs_add(conn->peer_rmbe_size, 898 &conn->local_tx_ctrl.prod, diff); 899 smc_curs_add(conn->peer_rmbe_size, 900 &conn->local_tx_ctrl_fin, diff); 901 } 902 /* recalculate, value is used by tx_rdma_writes() */ 903 atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn)); 904 905 if (smc->sk.sk_state != SMC_INIT && 906 smc->sk.sk_state != SMC_CLOSED) { 907 rc = smcr_cdc_msg_send_validation(conn, pend, wr_buf); 908 if (!rc) { 909 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, 0); 910 smc->sk.sk_data_ready(&smc->sk); 911 } 912 } else { 913 smc_wr_tx_put_slot(conn->lnk, 914 (struct smc_wr_tx_pend_priv *)pend); 915 } 916 return rc; 917 } 918 919 static void smc_switch_link_and_count(struct smc_connection *conn, 920 struct smc_link *to_lnk) 921 { 922 atomic_dec(&conn->lnk->conn_cnt); 923 conn->lnk = to_lnk; 924 atomic_inc(&conn->lnk->conn_cnt); 925 } 926 927 struct smc_link *smc_switch_conns(struct smc_link_group *lgr, 928 struct smc_link *from_lnk, bool is_dev_err) 929 { 930 struct smc_link *to_lnk = NULL; 931 struct smc_cdc_tx_pend *pend; 932 struct smc_connection *conn; 933 struct smc_wr_buf *wr_buf; 934 struct smc_sock *smc; 935 struct rb_node *node; 936 int i, rc = 0; 937 938 /* link is inactive, wake up tx waiters */ 939 smc_wr_wakeup_tx_wait(from_lnk); 940 941 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 942 if (!smc_link_active(&lgr->lnk[i]) || i == from_lnk->link_idx) 943 continue; 944 if (is_dev_err && from_lnk->smcibdev == lgr->lnk[i].smcibdev && 945 from_lnk->ibport == lgr->lnk[i].ibport) { 946 continue; 947 } 948 to_lnk = &lgr->lnk[i]; 949 break; 950 } 951 if (!to_lnk) { 952 smc_lgr_terminate_sched(lgr); 953 return NULL; 954 } 955 again: 956 read_lock_bh(&lgr->conns_lock); 957 for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) { 958 conn = rb_entry(node, struct smc_connection, alert_node); 959 if (conn->lnk != from_lnk) 960 continue; 961 smc = container_of(conn, struct smc_sock, conn); 962 /* conn->lnk not yet set in SMC_INIT state */ 963 if (smc->sk.sk_state == SMC_INIT) 964 continue; 965 if (smc->sk.sk_state == SMC_CLOSED || 966 smc->sk.sk_state == SMC_PEERCLOSEWAIT1 || 967 smc->sk.sk_state == SMC_PEERCLOSEWAIT2 || 968 smc->sk.sk_state == SMC_APPFINCLOSEWAIT || 969 smc->sk.sk_state == SMC_APPCLOSEWAIT1 || 970 smc->sk.sk_state == SMC_APPCLOSEWAIT2 || 971 smc->sk.sk_state == SMC_PEERFINCLOSEWAIT || 972 smc->sk.sk_state == SMC_PEERABORTWAIT || 973 smc->sk.sk_state == SMC_PROCESSABORT) { 974 spin_lock_bh(&conn->send_lock); 975 smc_switch_link_and_count(conn, to_lnk); 976 spin_unlock_bh(&conn->send_lock); 977 continue; 978 } 979 sock_hold(&smc->sk); 980 read_unlock_bh(&lgr->conns_lock); 981 /* pre-fetch buffer outside of send_lock, might sleep */ 982 rc = smc_cdc_get_free_slot(conn, to_lnk, &wr_buf, NULL, &pend); 983 if (rc) { 984 smcr_link_down_cond_sched(to_lnk); 985 return NULL; 986 } 987 /* avoid race with smcr_tx_sndbuf_nonempty() */ 988 spin_lock_bh(&conn->send_lock); 989 smc_switch_link_and_count(conn, to_lnk); 990 rc = smc_switch_cursor(smc, pend, wr_buf); 991 spin_unlock_bh(&conn->send_lock); 992 sock_put(&smc->sk); 993 if (rc) { 994 smcr_link_down_cond_sched(to_lnk); 995 return NULL; 996 } 997 goto again; 998 } 999 read_unlock_bh(&lgr->conns_lock); 1000 return to_lnk; 1001 } 1002 1003 static void smcr_buf_unuse(struct smc_buf_desc *rmb_desc, 1004 struct smc_link_group *lgr) 1005 { 1006 int rc; 1007 1008 if (rmb_desc->is_conf_rkey && !list_empty(&lgr->list)) { 1009 /* unregister rmb with peer */ 1010 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY); 1011 if (!rc) { 1012 /* protect against smc_llc_cli_rkey_exchange() */ 1013 mutex_lock(&lgr->llc_conf_mutex); 1014 smc_llc_do_delete_rkey(lgr, rmb_desc); 1015 rmb_desc->is_conf_rkey = false; 1016 mutex_unlock(&lgr->llc_conf_mutex); 1017 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl); 1018 } 1019 } 1020 1021 if (rmb_desc->is_reg_err) { 1022 /* buf registration failed, reuse not possible */ 1023 mutex_lock(&lgr->rmbs_lock); 1024 list_del(&rmb_desc->list); 1025 mutex_unlock(&lgr->rmbs_lock); 1026 1027 smc_buf_free(lgr, true, rmb_desc); 1028 } else { 1029 rmb_desc->used = 0; 1030 } 1031 } 1032 1033 static void smc_buf_unuse(struct smc_connection *conn, 1034 struct smc_link_group *lgr) 1035 { 1036 if (conn->sndbuf_desc) 1037 conn->sndbuf_desc->used = 0; 1038 if (conn->rmb_desc && lgr->is_smcd) 1039 conn->rmb_desc->used = 0; 1040 else if (conn->rmb_desc) 1041 smcr_buf_unuse(conn->rmb_desc, lgr); 1042 } 1043 1044 /* remove a finished connection from its link group */ 1045 void smc_conn_free(struct smc_connection *conn) 1046 { 1047 struct smc_link_group *lgr = conn->lgr; 1048 1049 if (!lgr) 1050 return; 1051 if (lgr->is_smcd) { 1052 if (!list_empty(&lgr->list)) 1053 smc_ism_unset_conn(conn); 1054 tasklet_kill(&conn->rx_tsklet); 1055 } else { 1056 smc_cdc_tx_dismiss_slots(conn); 1057 if (current_work() != &conn->abort_work) 1058 cancel_work_sync(&conn->abort_work); 1059 } 1060 if (!list_empty(&lgr->list)) { 1061 smc_lgr_unregister_conn(conn); 1062 smc_buf_unuse(conn, lgr); /* allow buffer reuse */ 1063 } 1064 1065 if (!lgr->conns_num) 1066 smc_lgr_schedule_free_work(lgr); 1067 } 1068 1069 /* unregister a link from a buf_desc */ 1070 static void smcr_buf_unmap_link(struct smc_buf_desc *buf_desc, bool is_rmb, 1071 struct smc_link *lnk) 1072 { 1073 if (is_rmb) 1074 buf_desc->is_reg_mr[lnk->link_idx] = false; 1075 if (!buf_desc->is_map_ib[lnk->link_idx]) 1076 return; 1077 if (is_rmb) { 1078 if (buf_desc->mr_rx[lnk->link_idx]) { 1079 smc_ib_put_memory_region( 1080 buf_desc->mr_rx[lnk->link_idx]); 1081 buf_desc->mr_rx[lnk->link_idx] = NULL; 1082 } 1083 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE); 1084 } else { 1085 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE); 1086 } 1087 sg_free_table(&buf_desc->sgt[lnk->link_idx]); 1088 buf_desc->is_map_ib[lnk->link_idx] = false; 1089 } 1090 1091 /* unmap all buffers of lgr for a deleted link */ 1092 static void smcr_buf_unmap_lgr(struct smc_link *lnk) 1093 { 1094 struct smc_link_group *lgr = lnk->lgr; 1095 struct smc_buf_desc *buf_desc, *bf; 1096 int i; 1097 1098 for (i = 0; i < SMC_RMBE_SIZES; i++) { 1099 mutex_lock(&lgr->rmbs_lock); 1100 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) 1101 smcr_buf_unmap_link(buf_desc, true, lnk); 1102 mutex_unlock(&lgr->rmbs_lock); 1103 mutex_lock(&lgr->sndbufs_lock); 1104 list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i], 1105 list) 1106 smcr_buf_unmap_link(buf_desc, false, lnk); 1107 mutex_unlock(&lgr->sndbufs_lock); 1108 } 1109 } 1110 1111 static void smcr_rtoken_clear_link(struct smc_link *lnk) 1112 { 1113 struct smc_link_group *lgr = lnk->lgr; 1114 int i; 1115 1116 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { 1117 lgr->rtokens[i][lnk->link_idx].rkey = 0; 1118 lgr->rtokens[i][lnk->link_idx].dma_addr = 0; 1119 } 1120 } 1121 1122 /* must be called under lgr->llc_conf_mutex lock */ 1123 void smcr_link_clear(struct smc_link *lnk, bool log) 1124 { 1125 struct smc_ib_device *smcibdev; 1126 1127 if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED) 1128 return; 1129 lnk->peer_qpn = 0; 1130 smc_llc_link_clear(lnk, log); 1131 smcr_buf_unmap_lgr(lnk); 1132 smcr_rtoken_clear_link(lnk); 1133 smc_ib_modify_qp_reset(lnk); 1134 smc_wr_free_link(lnk); 1135 smc_ib_destroy_queue_pair(lnk); 1136 smc_ib_dealloc_protection_domain(lnk); 1137 smc_wr_free_link_mem(lnk); 1138 smc_ibdev_cnt_dec(lnk); 1139 put_device(&lnk->smcibdev->ibdev->dev); 1140 smcibdev = lnk->smcibdev; 1141 memset(lnk, 0, sizeof(struct smc_link)); 1142 lnk->state = SMC_LNK_UNUSED; 1143 if (!atomic_dec_return(&smcibdev->lnk_cnt)) 1144 wake_up(&smcibdev->lnks_deleted); 1145 } 1146 1147 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb, 1148 struct smc_buf_desc *buf_desc) 1149 { 1150 int i; 1151 1152 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) 1153 smcr_buf_unmap_link(buf_desc, is_rmb, &lgr->lnk[i]); 1154 1155 if (buf_desc->pages) 1156 __free_pages(buf_desc->pages, buf_desc->order); 1157 kfree(buf_desc); 1158 } 1159 1160 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb, 1161 struct smc_buf_desc *buf_desc) 1162 { 1163 if (is_dmb) { 1164 /* restore original buf len */ 1165 buf_desc->len += sizeof(struct smcd_cdc_msg); 1166 smc_ism_unregister_dmb(lgr->smcd, buf_desc); 1167 } else { 1168 kfree(buf_desc->cpu_addr); 1169 } 1170 kfree(buf_desc); 1171 } 1172 1173 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb, 1174 struct smc_buf_desc *buf_desc) 1175 { 1176 if (lgr->is_smcd) 1177 smcd_buf_free(lgr, is_rmb, buf_desc); 1178 else 1179 smcr_buf_free(lgr, is_rmb, buf_desc); 1180 } 1181 1182 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb) 1183 { 1184 struct smc_buf_desc *buf_desc, *bf_desc; 1185 struct list_head *buf_list; 1186 int i; 1187 1188 for (i = 0; i < SMC_RMBE_SIZES; i++) { 1189 if (is_rmb) 1190 buf_list = &lgr->rmbs[i]; 1191 else 1192 buf_list = &lgr->sndbufs[i]; 1193 list_for_each_entry_safe(buf_desc, bf_desc, buf_list, 1194 list) { 1195 list_del(&buf_desc->list); 1196 smc_buf_free(lgr, is_rmb, buf_desc); 1197 } 1198 } 1199 } 1200 1201 static void smc_lgr_free_bufs(struct smc_link_group *lgr) 1202 { 1203 /* free send buffers */ 1204 __smc_lgr_free_bufs(lgr, false); 1205 /* free rmbs */ 1206 __smc_lgr_free_bufs(lgr, true); 1207 } 1208 1209 /* remove a link group */ 1210 static void smc_lgr_free(struct smc_link_group *lgr) 1211 { 1212 int i; 1213 1214 if (!lgr->is_smcd) { 1215 mutex_lock(&lgr->llc_conf_mutex); 1216 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 1217 if (lgr->lnk[i].state != SMC_LNK_UNUSED) 1218 smcr_link_clear(&lgr->lnk[i], false); 1219 } 1220 mutex_unlock(&lgr->llc_conf_mutex); 1221 smc_llc_lgr_clear(lgr); 1222 } 1223 1224 smc_lgr_free_bufs(lgr); 1225 destroy_workqueue(lgr->tx_wq); 1226 if (lgr->is_smcd) { 1227 smc_ism_put_vlan(lgr->smcd, lgr->vlan_id); 1228 put_device(&lgr->smcd->dev); 1229 if (!atomic_dec_return(&lgr->smcd->lgr_cnt)) 1230 wake_up(&lgr->smcd->lgrs_deleted); 1231 } else { 1232 if (!atomic_dec_return(&lgr_cnt)) 1233 wake_up(&lgrs_deleted); 1234 } 1235 kfree(lgr); 1236 } 1237 1238 static void smc_sk_wake_ups(struct smc_sock *smc) 1239 { 1240 smc->sk.sk_write_space(&smc->sk); 1241 smc->sk.sk_data_ready(&smc->sk); 1242 smc->sk.sk_state_change(&smc->sk); 1243 } 1244 1245 /* kill a connection */ 1246 static void smc_conn_kill(struct smc_connection *conn, bool soft) 1247 { 1248 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 1249 1250 if (conn->lgr->is_smcd && conn->lgr->peer_shutdown) 1251 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; 1252 else 1253 smc_close_abort(conn); 1254 conn->killed = 1; 1255 smc->sk.sk_err = ECONNABORTED; 1256 smc_sk_wake_ups(smc); 1257 if (conn->lgr->is_smcd) { 1258 smc_ism_unset_conn(conn); 1259 if (soft) 1260 tasklet_kill(&conn->rx_tsklet); 1261 else 1262 tasklet_unlock_wait(&conn->rx_tsklet); 1263 } else { 1264 smc_cdc_tx_dismiss_slots(conn); 1265 } 1266 smc_lgr_unregister_conn(conn); 1267 smc_close_active_abort(smc); 1268 } 1269 1270 static void smc_lgr_cleanup(struct smc_link_group *lgr) 1271 { 1272 if (lgr->is_smcd) { 1273 smc_ism_signal_shutdown(lgr); 1274 } else { 1275 u32 rsn = lgr->llc_termination_rsn; 1276 1277 if (!rsn) 1278 rsn = SMC_LLC_DEL_PROG_INIT_TERM; 1279 smc_llc_send_link_delete_all(lgr, false, rsn); 1280 smcr_lgr_link_deactivate_all(lgr); 1281 } 1282 } 1283 1284 /* terminate link group 1285 * @soft: true if link group shutdown can take its time 1286 * false if immediate link group shutdown is required 1287 */ 1288 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft) 1289 { 1290 struct smc_connection *conn; 1291 struct smc_sock *smc; 1292 struct rb_node *node; 1293 1294 if (lgr->terminating) 1295 return; /* lgr already terminating */ 1296 /* cancel free_work sync, will terminate when lgr->freeing is set */ 1297 cancel_delayed_work_sync(&lgr->free_work); 1298 lgr->terminating = 1; 1299 1300 /* kill remaining link group connections */ 1301 read_lock_bh(&lgr->conns_lock); 1302 node = rb_first(&lgr->conns_all); 1303 while (node) { 1304 read_unlock_bh(&lgr->conns_lock); 1305 conn = rb_entry(node, struct smc_connection, alert_node); 1306 smc = container_of(conn, struct smc_sock, conn); 1307 sock_hold(&smc->sk); /* sock_put below */ 1308 lock_sock(&smc->sk); 1309 smc_conn_kill(conn, soft); 1310 release_sock(&smc->sk); 1311 sock_put(&smc->sk); /* sock_hold above */ 1312 read_lock_bh(&lgr->conns_lock); 1313 node = rb_first(&lgr->conns_all); 1314 } 1315 read_unlock_bh(&lgr->conns_lock); 1316 smc_lgr_cleanup(lgr); 1317 smc_lgr_free(lgr); 1318 } 1319 1320 /* unlink link group and schedule termination */ 1321 void smc_lgr_terminate_sched(struct smc_link_group *lgr) 1322 { 1323 spinlock_t *lgr_lock; 1324 1325 smc_lgr_list_head(lgr, &lgr_lock); 1326 spin_lock_bh(lgr_lock); 1327 if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) { 1328 spin_unlock_bh(lgr_lock); 1329 return; /* lgr already terminating */ 1330 } 1331 list_del_init(&lgr->list); 1332 lgr->freeing = 1; 1333 spin_unlock_bh(lgr_lock); 1334 schedule_work(&lgr->terminate_work); 1335 } 1336 1337 /* Called when peer lgr shutdown (regularly or abnormally) is received */ 1338 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan) 1339 { 1340 struct smc_link_group *lgr, *l; 1341 LIST_HEAD(lgr_free_list); 1342 1343 /* run common cleanup function and build free list */ 1344 spin_lock_bh(&dev->lgr_lock); 1345 list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) { 1346 if ((!peer_gid || lgr->peer_gid == peer_gid) && 1347 (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) { 1348 if (peer_gid) /* peer triggered termination */ 1349 lgr->peer_shutdown = 1; 1350 list_move(&lgr->list, &lgr_free_list); 1351 lgr->freeing = 1; 1352 } 1353 } 1354 spin_unlock_bh(&dev->lgr_lock); 1355 1356 /* cancel the regular free workers and actually free lgrs */ 1357 list_for_each_entry_safe(lgr, l, &lgr_free_list, list) { 1358 list_del_init(&lgr->list); 1359 schedule_work(&lgr->terminate_work); 1360 } 1361 } 1362 1363 /* Called when an SMCD device is removed or the smc module is unloaded */ 1364 void smc_smcd_terminate_all(struct smcd_dev *smcd) 1365 { 1366 struct smc_link_group *lgr, *lg; 1367 LIST_HEAD(lgr_free_list); 1368 1369 spin_lock_bh(&smcd->lgr_lock); 1370 list_splice_init(&smcd->lgr_list, &lgr_free_list); 1371 list_for_each_entry(lgr, &lgr_free_list, list) 1372 lgr->freeing = 1; 1373 spin_unlock_bh(&smcd->lgr_lock); 1374 1375 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) { 1376 list_del_init(&lgr->list); 1377 __smc_lgr_terminate(lgr, false); 1378 } 1379 1380 if (atomic_read(&smcd->lgr_cnt)) 1381 wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt)); 1382 } 1383 1384 /* Called when an SMCR device is removed or the smc module is unloaded. 1385 * If smcibdev is given, all SMCR link groups using this device are terminated. 1386 * If smcibdev is NULL, all SMCR link groups are terminated. 1387 */ 1388 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev) 1389 { 1390 struct smc_link_group *lgr, *lg; 1391 LIST_HEAD(lgr_free_list); 1392 int i; 1393 1394 spin_lock_bh(&smc_lgr_list.lock); 1395 if (!smcibdev) { 1396 list_splice_init(&smc_lgr_list.list, &lgr_free_list); 1397 list_for_each_entry(lgr, &lgr_free_list, list) 1398 lgr->freeing = 1; 1399 } else { 1400 list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) { 1401 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 1402 if (lgr->lnk[i].smcibdev == smcibdev) 1403 smcr_link_down_cond_sched(&lgr->lnk[i]); 1404 } 1405 } 1406 } 1407 spin_unlock_bh(&smc_lgr_list.lock); 1408 1409 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) { 1410 list_del_init(&lgr->list); 1411 smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM); 1412 __smc_lgr_terminate(lgr, false); 1413 } 1414 1415 if (smcibdev) { 1416 if (atomic_read(&smcibdev->lnk_cnt)) 1417 wait_event(smcibdev->lnks_deleted, 1418 !atomic_read(&smcibdev->lnk_cnt)); 1419 } else { 1420 if (atomic_read(&lgr_cnt)) 1421 wait_event(lgrs_deleted, !atomic_read(&lgr_cnt)); 1422 } 1423 } 1424 1425 /* set new lgr type and clear all asymmetric link tagging */ 1426 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type) 1427 { 1428 char *lgr_type = ""; 1429 int i; 1430 1431 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) 1432 if (smc_link_usable(&lgr->lnk[i])) 1433 lgr->lnk[i].link_is_asym = false; 1434 if (lgr->type == new_type) 1435 return; 1436 lgr->type = new_type; 1437 1438 switch (lgr->type) { 1439 case SMC_LGR_NONE: 1440 lgr_type = "NONE"; 1441 break; 1442 case SMC_LGR_SINGLE: 1443 lgr_type = "SINGLE"; 1444 break; 1445 case SMC_LGR_SYMMETRIC: 1446 lgr_type = "SYMMETRIC"; 1447 break; 1448 case SMC_LGR_ASYMMETRIC_PEER: 1449 lgr_type = "ASYMMETRIC_PEER"; 1450 break; 1451 case SMC_LGR_ASYMMETRIC_LOCAL: 1452 lgr_type = "ASYMMETRIC_LOCAL"; 1453 break; 1454 } 1455 pr_warn_ratelimited("smc: SMC-R lg %*phN state changed: " 1456 "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id, 1457 lgr_type, lgr->pnet_id); 1458 } 1459 1460 /* set new lgr type and tag a link as asymmetric */ 1461 void smcr_lgr_set_type_asym(struct smc_link_group *lgr, 1462 enum smc_lgr_type new_type, int asym_lnk_idx) 1463 { 1464 smcr_lgr_set_type(lgr, new_type); 1465 lgr->lnk[asym_lnk_idx].link_is_asym = true; 1466 } 1467 1468 /* abort connection, abort_work scheduled from tasklet context */ 1469 static void smc_conn_abort_work(struct work_struct *work) 1470 { 1471 struct smc_connection *conn = container_of(work, 1472 struct smc_connection, 1473 abort_work); 1474 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 1475 1476 smc_conn_kill(conn, true); 1477 sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */ 1478 } 1479 1480 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport) 1481 { 1482 struct smc_link_group *lgr, *n; 1483 1484 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) { 1485 struct smc_link *link; 1486 1487 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id, 1488 SMC_MAX_PNETID_LEN) || 1489 lgr->type == SMC_LGR_SYMMETRIC || 1490 lgr->type == SMC_LGR_ASYMMETRIC_PEER) 1491 continue; 1492 1493 /* trigger local add link processing */ 1494 link = smc_llc_usable_link(lgr); 1495 if (link) 1496 smc_llc_add_link_local(link); 1497 } 1498 } 1499 1500 /* link is down - switch connections to alternate link, 1501 * must be called under lgr->llc_conf_mutex lock 1502 */ 1503 static void smcr_link_down(struct smc_link *lnk) 1504 { 1505 struct smc_link_group *lgr = lnk->lgr; 1506 struct smc_link *to_lnk; 1507 int del_link_id; 1508 1509 if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list)) 1510 return; 1511 1512 smc_ib_modify_qp_reset(lnk); 1513 to_lnk = smc_switch_conns(lgr, lnk, true); 1514 if (!to_lnk) { /* no backup link available */ 1515 smcr_link_clear(lnk, true); 1516 return; 1517 } 1518 smcr_lgr_set_type(lgr, SMC_LGR_SINGLE); 1519 del_link_id = lnk->link_id; 1520 1521 if (lgr->role == SMC_SERV) { 1522 /* trigger local delete link processing */ 1523 smc_llc_srv_delete_link_local(to_lnk, del_link_id); 1524 } else { 1525 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) { 1526 /* another llc task is ongoing */ 1527 mutex_unlock(&lgr->llc_conf_mutex); 1528 wait_event_timeout(lgr->llc_flow_waiter, 1529 (list_empty(&lgr->list) || 1530 lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE), 1531 SMC_LLC_WAIT_TIME); 1532 mutex_lock(&lgr->llc_conf_mutex); 1533 } 1534 if (!list_empty(&lgr->list)) { 1535 smc_llc_send_delete_link(to_lnk, del_link_id, 1536 SMC_LLC_REQ, true, 1537 SMC_LLC_DEL_LOST_PATH); 1538 smcr_link_clear(lnk, true); 1539 } 1540 wake_up(&lgr->llc_flow_waiter); /* wake up next waiter */ 1541 } 1542 } 1543 1544 /* must be called under lgr->llc_conf_mutex lock */ 1545 void smcr_link_down_cond(struct smc_link *lnk) 1546 { 1547 if (smc_link_downing(&lnk->state)) 1548 smcr_link_down(lnk); 1549 } 1550 1551 /* will get the lgr->llc_conf_mutex lock */ 1552 void smcr_link_down_cond_sched(struct smc_link *lnk) 1553 { 1554 if (smc_link_downing(&lnk->state)) 1555 schedule_work(&lnk->link_down_wrk); 1556 } 1557 1558 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport) 1559 { 1560 struct smc_link_group *lgr, *n; 1561 int i; 1562 1563 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) { 1564 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id, 1565 SMC_MAX_PNETID_LEN)) 1566 continue; /* lgr is not affected */ 1567 if (list_empty(&lgr->list)) 1568 continue; 1569 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 1570 struct smc_link *lnk = &lgr->lnk[i]; 1571 1572 if (smc_link_usable(lnk) && 1573 lnk->smcibdev == smcibdev && lnk->ibport == ibport) 1574 smcr_link_down_cond_sched(lnk); 1575 } 1576 } 1577 } 1578 1579 static void smc_link_down_work(struct work_struct *work) 1580 { 1581 struct smc_link *link = container_of(work, struct smc_link, 1582 link_down_wrk); 1583 struct smc_link_group *lgr = link->lgr; 1584 1585 if (list_empty(&lgr->list)) 1586 return; 1587 wake_up_all(&lgr->llc_msg_waiter); 1588 mutex_lock(&lgr->llc_conf_mutex); 1589 smcr_link_down(link); 1590 mutex_unlock(&lgr->llc_conf_mutex); 1591 } 1592 1593 /* Determine vlan of internal TCP socket. 1594 * @vlan_id: address to store the determined vlan id into 1595 */ 1596 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini) 1597 { 1598 struct dst_entry *dst = sk_dst_get(clcsock->sk); 1599 struct net_device *ndev; 1600 int i, nest_lvl, rc = 0; 1601 1602 ini->vlan_id = 0; 1603 if (!dst) { 1604 rc = -ENOTCONN; 1605 goto out; 1606 } 1607 if (!dst->dev) { 1608 rc = -ENODEV; 1609 goto out_rel; 1610 } 1611 1612 ndev = dst->dev; 1613 if (is_vlan_dev(ndev)) { 1614 ini->vlan_id = vlan_dev_vlan_id(ndev); 1615 goto out_rel; 1616 } 1617 1618 rtnl_lock(); 1619 nest_lvl = ndev->lower_level; 1620 for (i = 0; i < nest_lvl; i++) { 1621 struct list_head *lower = &ndev->adj_list.lower; 1622 1623 if (list_empty(lower)) 1624 break; 1625 lower = lower->next; 1626 ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower); 1627 if (is_vlan_dev(ndev)) { 1628 ini->vlan_id = vlan_dev_vlan_id(ndev); 1629 break; 1630 } 1631 } 1632 rtnl_unlock(); 1633 1634 out_rel: 1635 dst_release(dst); 1636 out: 1637 return rc; 1638 } 1639 1640 static bool smcr_lgr_match(struct smc_link_group *lgr, 1641 struct smc_clc_msg_local *lcl, 1642 enum smc_lgr_role role, u32 clcqpn) 1643 { 1644 int i; 1645 1646 if (memcmp(lgr->peer_systemid, lcl->id_for_peer, SMC_SYSTEMID_LEN) || 1647 lgr->role != role) 1648 return false; 1649 1650 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 1651 if (!smc_link_active(&lgr->lnk[i])) 1652 continue; 1653 if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) && 1654 !memcmp(lgr->lnk[i].peer_gid, &lcl->gid, SMC_GID_SIZE) && 1655 !memcmp(lgr->lnk[i].peer_mac, lcl->mac, sizeof(lcl->mac))) 1656 return true; 1657 } 1658 return false; 1659 } 1660 1661 static bool smcd_lgr_match(struct smc_link_group *lgr, 1662 struct smcd_dev *smcismdev, u64 peer_gid) 1663 { 1664 return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev; 1665 } 1666 1667 /* create a new SMC connection (and a new link group if necessary) */ 1668 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini) 1669 { 1670 struct smc_connection *conn = &smc->conn; 1671 struct list_head *lgr_list; 1672 struct smc_link_group *lgr; 1673 enum smc_lgr_role role; 1674 spinlock_t *lgr_lock; 1675 int rc = 0; 1676 1677 lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list : 1678 &smc_lgr_list.list; 1679 lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock : 1680 &smc_lgr_list.lock; 1681 ini->first_contact_local = 1; 1682 role = smc->listen_smc ? SMC_SERV : SMC_CLNT; 1683 if (role == SMC_CLNT && ini->first_contact_peer) 1684 /* create new link group as well */ 1685 goto create; 1686 1687 /* determine if an existing link group can be reused */ 1688 spin_lock_bh(lgr_lock); 1689 list_for_each_entry(lgr, lgr_list, list) { 1690 write_lock_bh(&lgr->conns_lock); 1691 if ((ini->is_smcd ? 1692 smcd_lgr_match(lgr, ini->ism_dev[ini->ism_selected], 1693 ini->ism_peer_gid[ini->ism_selected]) : 1694 smcr_lgr_match(lgr, ini->ib_lcl, role, ini->ib_clcqpn)) && 1695 !lgr->sync_err && 1696 (ini->smcd_version == SMC_V2 || 1697 lgr->vlan_id == ini->vlan_id) && 1698 (role == SMC_CLNT || ini->is_smcd || 1699 lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) { 1700 /* link group found */ 1701 ini->first_contact_local = 0; 1702 conn->lgr = lgr; 1703 rc = smc_lgr_register_conn(conn, false); 1704 write_unlock_bh(&lgr->conns_lock); 1705 if (!rc && delayed_work_pending(&lgr->free_work)) 1706 cancel_delayed_work(&lgr->free_work); 1707 break; 1708 } 1709 write_unlock_bh(&lgr->conns_lock); 1710 } 1711 spin_unlock_bh(lgr_lock); 1712 if (rc) 1713 return rc; 1714 1715 if (role == SMC_CLNT && !ini->first_contact_peer && 1716 ini->first_contact_local) { 1717 /* Server reuses a link group, but Client wants to start 1718 * a new one 1719 * send out_of_sync decline, reason synchr. error 1720 */ 1721 return SMC_CLC_DECL_SYNCERR; 1722 } 1723 1724 create: 1725 if (ini->first_contact_local) { 1726 rc = smc_lgr_create(smc, ini); 1727 if (rc) 1728 goto out; 1729 lgr = conn->lgr; 1730 write_lock_bh(&lgr->conns_lock); 1731 rc = smc_lgr_register_conn(conn, true); 1732 write_unlock_bh(&lgr->conns_lock); 1733 if (rc) 1734 goto out; 1735 } 1736 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE; 1737 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE; 1738 conn->urg_state = SMC_URG_READ; 1739 INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work); 1740 if (ini->is_smcd) { 1741 conn->rx_off = sizeof(struct smcd_cdc_msg); 1742 smcd_cdc_rx_init(conn); /* init tasklet for this conn */ 1743 } else { 1744 conn->rx_off = 0; 1745 } 1746 #ifndef KERNEL_HAS_ATOMIC64 1747 spin_lock_init(&conn->acurs_lock); 1748 #endif 1749 1750 out: 1751 return rc; 1752 } 1753 1754 /* convert the RMB size into the compressed notation - minimum 16K. 1755 * In contrast to plain ilog2, this rounds towards the next power of 2, 1756 * so the socket application gets at least its desired sndbuf / rcvbuf size. 1757 */ 1758 static u8 smc_compress_bufsize(int size) 1759 { 1760 u8 compressed; 1761 1762 if (size <= SMC_BUF_MIN_SIZE) 1763 return 0; 1764 1765 size = (size - 1) >> 14; 1766 compressed = ilog2(size) + 1; 1767 if (compressed >= SMC_RMBE_SIZES) 1768 compressed = SMC_RMBE_SIZES - 1; 1769 return compressed; 1770 } 1771 1772 /* convert the RMB size from compressed notation into integer */ 1773 int smc_uncompress_bufsize(u8 compressed) 1774 { 1775 u32 size; 1776 1777 size = 0x00000001 << (((int)compressed) + 14); 1778 return (int)size; 1779 } 1780 1781 /* try to reuse a sndbuf or rmb description slot for a certain 1782 * buffer size; if not available, return NULL 1783 */ 1784 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize, 1785 struct mutex *lock, 1786 struct list_head *buf_list) 1787 { 1788 struct smc_buf_desc *buf_slot; 1789 1790 mutex_lock(lock); 1791 list_for_each_entry(buf_slot, buf_list, list) { 1792 if (cmpxchg(&buf_slot->used, 0, 1) == 0) { 1793 mutex_unlock(lock); 1794 return buf_slot; 1795 } 1796 } 1797 mutex_unlock(lock); 1798 return NULL; 1799 } 1800 1801 /* one of the conditions for announcing a receiver's current window size is 1802 * that it "results in a minimum increase in the window size of 10% of the 1803 * receive buffer space" [RFC7609] 1804 */ 1805 static inline int smc_rmb_wnd_update_limit(int rmbe_size) 1806 { 1807 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2); 1808 } 1809 1810 /* map an rmb buf to a link */ 1811 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb, 1812 struct smc_link *lnk) 1813 { 1814 int rc; 1815 1816 if (buf_desc->is_map_ib[lnk->link_idx]) 1817 return 0; 1818 1819 rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL); 1820 if (rc) 1821 return rc; 1822 sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl, 1823 buf_desc->cpu_addr, buf_desc->len); 1824 1825 /* map sg table to DMA address */ 1826 rc = smc_ib_buf_map_sg(lnk, buf_desc, 1827 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1828 /* SMC protocol depends on mapping to one DMA address only */ 1829 if (rc != 1) { 1830 rc = -EAGAIN; 1831 goto free_table; 1832 } 1833 1834 /* create a new memory region for the RMB */ 1835 if (is_rmb) { 1836 rc = smc_ib_get_memory_region(lnk->roce_pd, 1837 IB_ACCESS_REMOTE_WRITE | 1838 IB_ACCESS_LOCAL_WRITE, 1839 buf_desc, lnk->link_idx); 1840 if (rc) 1841 goto buf_unmap; 1842 smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE); 1843 } 1844 buf_desc->is_map_ib[lnk->link_idx] = true; 1845 return 0; 1846 1847 buf_unmap: 1848 smc_ib_buf_unmap_sg(lnk, buf_desc, 1849 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1850 free_table: 1851 sg_free_table(&buf_desc->sgt[lnk->link_idx]); 1852 return rc; 1853 } 1854 1855 /* register a new rmb on IB device, 1856 * must be called under lgr->llc_conf_mutex lock 1857 */ 1858 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc) 1859 { 1860 if (list_empty(&link->lgr->list)) 1861 return -ENOLINK; 1862 if (!rmb_desc->is_reg_mr[link->link_idx]) { 1863 /* register memory region for new rmb */ 1864 if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) { 1865 rmb_desc->is_reg_err = true; 1866 return -EFAULT; 1867 } 1868 rmb_desc->is_reg_mr[link->link_idx] = true; 1869 } 1870 return 0; 1871 } 1872 1873 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct mutex *lock, 1874 struct list_head *lst, bool is_rmb) 1875 { 1876 struct smc_buf_desc *buf_desc, *bf; 1877 int rc = 0; 1878 1879 mutex_lock(lock); 1880 list_for_each_entry_safe(buf_desc, bf, lst, list) { 1881 if (!buf_desc->used) 1882 continue; 1883 rc = smcr_buf_map_link(buf_desc, is_rmb, lnk); 1884 if (rc) 1885 goto out; 1886 } 1887 out: 1888 mutex_unlock(lock); 1889 return rc; 1890 } 1891 1892 /* map all used buffers of lgr for a new link */ 1893 int smcr_buf_map_lgr(struct smc_link *lnk) 1894 { 1895 struct smc_link_group *lgr = lnk->lgr; 1896 int i, rc = 0; 1897 1898 for (i = 0; i < SMC_RMBE_SIZES; i++) { 1899 rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock, 1900 &lgr->rmbs[i], true); 1901 if (rc) 1902 return rc; 1903 rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock, 1904 &lgr->sndbufs[i], false); 1905 if (rc) 1906 return rc; 1907 } 1908 return 0; 1909 } 1910 1911 /* register all used buffers of lgr for a new link, 1912 * must be called under lgr->llc_conf_mutex lock 1913 */ 1914 int smcr_buf_reg_lgr(struct smc_link *lnk) 1915 { 1916 struct smc_link_group *lgr = lnk->lgr; 1917 struct smc_buf_desc *buf_desc, *bf; 1918 int i, rc = 0; 1919 1920 mutex_lock(&lgr->rmbs_lock); 1921 for (i = 0; i < SMC_RMBE_SIZES; i++) { 1922 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) { 1923 if (!buf_desc->used) 1924 continue; 1925 rc = smcr_link_reg_rmb(lnk, buf_desc); 1926 if (rc) 1927 goto out; 1928 } 1929 } 1930 out: 1931 mutex_unlock(&lgr->rmbs_lock); 1932 return rc; 1933 } 1934 1935 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr, 1936 bool is_rmb, int bufsize) 1937 { 1938 struct smc_buf_desc *buf_desc; 1939 1940 /* try to alloc a new buffer */ 1941 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL); 1942 if (!buf_desc) 1943 return ERR_PTR(-ENOMEM); 1944 1945 buf_desc->order = get_order(bufsize); 1946 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN | 1947 __GFP_NOMEMALLOC | __GFP_COMP | 1948 __GFP_NORETRY | __GFP_ZERO, 1949 buf_desc->order); 1950 if (!buf_desc->pages) { 1951 kfree(buf_desc); 1952 return ERR_PTR(-EAGAIN); 1953 } 1954 buf_desc->cpu_addr = (void *)page_address(buf_desc->pages); 1955 buf_desc->len = bufsize; 1956 return buf_desc; 1957 } 1958 1959 /* map buf_desc on all usable links, 1960 * unused buffers stay mapped as long as the link is up 1961 */ 1962 static int smcr_buf_map_usable_links(struct smc_link_group *lgr, 1963 struct smc_buf_desc *buf_desc, bool is_rmb) 1964 { 1965 int i, rc = 0; 1966 1967 /* protect against parallel link reconfiguration */ 1968 mutex_lock(&lgr->llc_conf_mutex); 1969 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 1970 struct smc_link *lnk = &lgr->lnk[i]; 1971 1972 if (!smc_link_usable(lnk)) 1973 continue; 1974 if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) { 1975 rc = -ENOMEM; 1976 goto out; 1977 } 1978 } 1979 out: 1980 mutex_unlock(&lgr->llc_conf_mutex); 1981 return rc; 1982 } 1983 1984 #define SMCD_DMBE_SIZES 6 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */ 1985 1986 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr, 1987 bool is_dmb, int bufsize) 1988 { 1989 struct smc_buf_desc *buf_desc; 1990 int rc; 1991 1992 if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES) 1993 return ERR_PTR(-EAGAIN); 1994 1995 /* try to alloc a new DMB */ 1996 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL); 1997 if (!buf_desc) 1998 return ERR_PTR(-ENOMEM); 1999 if (is_dmb) { 2000 rc = smc_ism_register_dmb(lgr, bufsize, buf_desc); 2001 if (rc) { 2002 kfree(buf_desc); 2003 if (rc == -ENOMEM) 2004 return ERR_PTR(-EAGAIN); 2005 if (rc == -ENOSPC) 2006 return ERR_PTR(-ENOSPC); 2007 return ERR_PTR(-EIO); 2008 } 2009 buf_desc->pages = virt_to_page(buf_desc->cpu_addr); 2010 /* CDC header stored in buf. So, pretend it was smaller */ 2011 buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg); 2012 } else { 2013 buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL | 2014 __GFP_NOWARN | __GFP_NORETRY | 2015 __GFP_NOMEMALLOC); 2016 if (!buf_desc->cpu_addr) { 2017 kfree(buf_desc); 2018 return ERR_PTR(-EAGAIN); 2019 } 2020 buf_desc->len = bufsize; 2021 } 2022 return buf_desc; 2023 } 2024 2025 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb) 2026 { 2027 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM); 2028 struct smc_connection *conn = &smc->conn; 2029 struct smc_link_group *lgr = conn->lgr; 2030 struct list_head *buf_list; 2031 int bufsize, bufsize_short; 2032 struct mutex *lock; /* lock buffer list */ 2033 int sk_buf_size; 2034 2035 if (is_rmb) 2036 /* use socket recv buffer size (w/o overhead) as start value */ 2037 sk_buf_size = smc->sk.sk_rcvbuf / 2; 2038 else 2039 /* use socket send buffer size (w/o overhead) as start value */ 2040 sk_buf_size = smc->sk.sk_sndbuf / 2; 2041 2042 for (bufsize_short = smc_compress_bufsize(sk_buf_size); 2043 bufsize_short >= 0; bufsize_short--) { 2044 2045 if (is_rmb) { 2046 lock = &lgr->rmbs_lock; 2047 buf_list = &lgr->rmbs[bufsize_short]; 2048 } else { 2049 lock = &lgr->sndbufs_lock; 2050 buf_list = &lgr->sndbufs[bufsize_short]; 2051 } 2052 bufsize = smc_uncompress_bufsize(bufsize_short); 2053 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC) 2054 continue; 2055 2056 /* check for reusable slot in the link group */ 2057 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list); 2058 if (buf_desc) { 2059 memset(buf_desc->cpu_addr, 0, bufsize); 2060 break; /* found reusable slot */ 2061 } 2062 2063 if (is_smcd) 2064 buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize); 2065 else 2066 buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize); 2067 2068 if (PTR_ERR(buf_desc) == -ENOMEM) 2069 break; 2070 if (IS_ERR(buf_desc)) 2071 continue; 2072 2073 buf_desc->used = 1; 2074 mutex_lock(lock); 2075 list_add(&buf_desc->list, buf_list); 2076 mutex_unlock(lock); 2077 break; /* found */ 2078 } 2079 2080 if (IS_ERR(buf_desc)) 2081 return PTR_ERR(buf_desc); 2082 2083 if (!is_smcd) { 2084 if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) { 2085 smcr_buf_unuse(buf_desc, lgr); 2086 return -ENOMEM; 2087 } 2088 } 2089 2090 if (is_rmb) { 2091 conn->rmb_desc = buf_desc; 2092 conn->rmbe_size_short = bufsize_short; 2093 smc->sk.sk_rcvbuf = bufsize * 2; 2094 atomic_set(&conn->bytes_to_rcv, 0); 2095 conn->rmbe_update_limit = 2096 smc_rmb_wnd_update_limit(buf_desc->len); 2097 if (is_smcd) 2098 smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */ 2099 } else { 2100 conn->sndbuf_desc = buf_desc; 2101 smc->sk.sk_sndbuf = bufsize * 2; 2102 atomic_set(&conn->sndbuf_space, bufsize); 2103 } 2104 return 0; 2105 } 2106 2107 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn) 2108 { 2109 if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk)) 2110 return; 2111 smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE); 2112 } 2113 2114 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn) 2115 { 2116 if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk)) 2117 return; 2118 smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE); 2119 } 2120 2121 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn) 2122 { 2123 int i; 2124 2125 if (!conn->lgr || conn->lgr->is_smcd) 2126 return; 2127 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 2128 if (!smc_link_active(&conn->lgr->lnk[i])) 2129 continue; 2130 smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc, 2131 DMA_FROM_DEVICE); 2132 } 2133 } 2134 2135 void smc_rmb_sync_sg_for_device(struct smc_connection *conn) 2136 { 2137 int i; 2138 2139 if (!conn->lgr || conn->lgr->is_smcd) 2140 return; 2141 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { 2142 if (!smc_link_active(&conn->lgr->lnk[i])) 2143 continue; 2144 smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc, 2145 DMA_FROM_DEVICE); 2146 } 2147 } 2148 2149 /* create the send and receive buffer for an SMC socket; 2150 * receive buffers are called RMBs; 2151 * (even though the SMC protocol allows more than one RMB-element per RMB, 2152 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an 2153 * extra RMB for every connection in a link group 2154 */ 2155 int smc_buf_create(struct smc_sock *smc, bool is_smcd) 2156 { 2157 int rc; 2158 2159 /* create send buffer */ 2160 rc = __smc_buf_create(smc, is_smcd, false); 2161 if (rc) 2162 return rc; 2163 /* create rmb */ 2164 rc = __smc_buf_create(smc, is_smcd, true); 2165 if (rc) { 2166 mutex_lock(&smc->conn.lgr->sndbufs_lock); 2167 list_del(&smc->conn.sndbuf_desc->list); 2168 mutex_unlock(&smc->conn.lgr->sndbufs_lock); 2169 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc); 2170 smc->conn.sndbuf_desc = NULL; 2171 } 2172 return rc; 2173 } 2174 2175 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr) 2176 { 2177 int i; 2178 2179 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) { 2180 if (!test_and_set_bit(i, lgr->rtokens_used_mask)) 2181 return i; 2182 } 2183 return -ENOSPC; 2184 } 2185 2186 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx, 2187 u32 rkey) 2188 { 2189 int i; 2190 2191 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { 2192 if (test_bit(i, lgr->rtokens_used_mask) && 2193 lgr->rtokens[i][lnk_idx].rkey == rkey) 2194 return i; 2195 } 2196 return -ENOENT; 2197 } 2198 2199 /* set rtoken for a new link to an existing rmb */ 2200 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new, 2201 __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey) 2202 { 2203 int rtok_idx; 2204 2205 rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known)); 2206 if (rtok_idx == -ENOENT) 2207 return; 2208 lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey); 2209 lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr); 2210 } 2211 2212 /* set rtoken for a new link whose link_id is given */ 2213 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id, 2214 __be64 nw_vaddr, __be32 nw_rkey) 2215 { 2216 u64 dma_addr = be64_to_cpu(nw_vaddr); 2217 u32 rkey = ntohl(nw_rkey); 2218 bool found = false; 2219 int link_idx; 2220 2221 for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) { 2222 if (lgr->lnk[link_idx].link_id == link_id) { 2223 found = true; 2224 break; 2225 } 2226 } 2227 if (!found) 2228 return; 2229 lgr->rtokens[rtok_idx][link_idx].rkey = rkey; 2230 lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr; 2231 } 2232 2233 /* add a new rtoken from peer */ 2234 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey) 2235 { 2236 struct smc_link_group *lgr = smc_get_lgr(lnk); 2237 u64 dma_addr = be64_to_cpu(nw_vaddr); 2238 u32 rkey = ntohl(nw_rkey); 2239 int i; 2240 2241 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { 2242 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey && 2243 lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr && 2244 test_bit(i, lgr->rtokens_used_mask)) { 2245 /* already in list */ 2246 return i; 2247 } 2248 } 2249 i = smc_rmb_reserve_rtoken_idx(lgr); 2250 if (i < 0) 2251 return i; 2252 lgr->rtokens[i][lnk->link_idx].rkey = rkey; 2253 lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr; 2254 return i; 2255 } 2256 2257 /* delete an rtoken from all links */ 2258 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey) 2259 { 2260 struct smc_link_group *lgr = smc_get_lgr(lnk); 2261 u32 rkey = ntohl(nw_rkey); 2262 int i, j; 2263 2264 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { 2265 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey && 2266 test_bit(i, lgr->rtokens_used_mask)) { 2267 for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) { 2268 lgr->rtokens[i][j].rkey = 0; 2269 lgr->rtokens[i][j].dma_addr = 0; 2270 } 2271 clear_bit(i, lgr->rtokens_used_mask); 2272 return 0; 2273 } 2274 } 2275 return -ENOENT; 2276 } 2277 2278 /* save rkey and dma_addr received from peer during clc handshake */ 2279 int smc_rmb_rtoken_handling(struct smc_connection *conn, 2280 struct smc_link *lnk, 2281 struct smc_clc_msg_accept_confirm *clc) 2282 { 2283 conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr, 2284 clc->r0.rmb_rkey); 2285 if (conn->rtoken_idx < 0) 2286 return conn->rtoken_idx; 2287 return 0; 2288 } 2289 2290 static void smc_core_going_away(void) 2291 { 2292 struct smc_ib_device *smcibdev; 2293 struct smcd_dev *smcd; 2294 2295 mutex_lock(&smc_ib_devices.mutex); 2296 list_for_each_entry(smcibdev, &smc_ib_devices.list, list) { 2297 int i; 2298 2299 for (i = 0; i < SMC_MAX_PORTS; i++) 2300 set_bit(i, smcibdev->ports_going_away); 2301 } 2302 mutex_unlock(&smc_ib_devices.mutex); 2303 2304 mutex_lock(&smcd_dev_list.mutex); 2305 list_for_each_entry(smcd, &smcd_dev_list.list, list) { 2306 smcd->going_away = 1; 2307 } 2308 mutex_unlock(&smcd_dev_list.mutex); 2309 } 2310 2311 /* Clean up all SMC link groups */ 2312 static void smc_lgrs_shutdown(void) 2313 { 2314 struct smcd_dev *smcd; 2315 2316 smc_core_going_away(); 2317 2318 smc_smcr_terminate_all(NULL); 2319 2320 mutex_lock(&smcd_dev_list.mutex); 2321 list_for_each_entry(smcd, &smcd_dev_list.list, list) 2322 smc_smcd_terminate_all(smcd); 2323 mutex_unlock(&smcd_dev_list.mutex); 2324 } 2325 2326 static int smc_core_reboot_event(struct notifier_block *this, 2327 unsigned long event, void *ptr) 2328 { 2329 smc_lgrs_shutdown(); 2330 smc_ib_unregister_client(); 2331 return 0; 2332 } 2333 2334 static struct notifier_block smc_reboot_notifier = { 2335 .notifier_call = smc_core_reboot_event, 2336 }; 2337 2338 int __init smc_core_init(void) 2339 { 2340 return register_reboot_notifier(&smc_reboot_notifier); 2341 } 2342 2343 /* Called (from smc_exit) when module is removed */ 2344 void smc_core_exit(void) 2345 { 2346 unregister_reboot_notifier(&smc_reboot_notifier); 2347 smc_lgrs_shutdown(); 2348 } 2349