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