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