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