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