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