1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Basic Transport Functions exploiting Infiniband API
7 * Copyright IBM Corp. 2016
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
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>
23 #include <rdma/ib_verbs.h>
24 #include <rdma/ib_cache.h>
33 #include "smc_close.h"
35 #include "smc_netlink.h"
36 #include "smc_stats.h"
37 #include "smc_tracepoint.h"
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)
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),
49 static atomic_t lgr_cnt = ATOMIC_INIT(0); /* number of existing link groups */
50 static DECLARE_WAIT_QUEUE_HEAD(lgrs_deleted);
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);
56 static void smc_link_down_work(struct work_struct *work);
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)
63 *lgr_lock = &lgr->smcd->lgr_lock;
64 return &lgr->smcd->lgr_list;
67 *lgr_lock = &smc_lgr_list.lock;
68 return &smc_lgr_list.list;
71 static void smc_ibdev_cnt_inc(struct smc_link *lnk)
73 atomic_inc(&lnk->smcibdev->lnk_cnt_by_port[lnk->ibport - 1]);
76 static void smc_ibdev_cnt_dec(struct smc_link *lnk)
78 atomic_dec(&lnk->smcibdev->lnk_cnt_by_port[lnk->ibport - 1]);
81 static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
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.
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);
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.
101 static void smc_lgr_add_alert_token(struct smc_connection *conn)
103 struct rb_node **link, *parent = NULL;
104 u32 token = conn->alert_token_local;
106 link = &conn->lgr->conns_all.rb_node;
108 struct smc_connection *cur = rb_entry(*link,
109 struct smc_connection, alert_node);
112 if (cur->alert_token_local > token)
113 link = &parent->rb_left;
115 link = &parent->rb_right;
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);
122 /* assign an SMC-R link to the connection */
123 static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first)
125 enum smc_link_state expected = first ? SMC_LNK_ACTIVATING :
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];
134 if (lnk->state != expected || lnk->link_is_asym)
136 if (conn->lgr->role == SMC_CLNT) {
137 conn->lnk = lnk; /* temporary, SMC server assigns link*/
140 if (conn->lgr->conns_num % 2) {
141 for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
142 struct smc_link *lnk2;
144 lnk2 = &conn->lgr->lnk[j];
145 if (lnk2->state == expected &&
146 !lnk2->link_is_asym) {
157 return SMC_CLC_DECL_NOACTLINK;
158 atomic_inc(&conn->lnk->conn_cnt);
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.
167 static int smc_lgr_register_conn(struct smc_connection *conn, bool first)
169 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
170 static atomic_t nexttoken = ATOMIC_INIT(0);
173 if (!conn->lgr->is_smcd) {
174 rc = smcr_lgr_conn_assign_link(conn, first);
180 /* find a new alert_token_local value not yet used by some connection
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;
189 smc_lgr_add_alert_token(conn);
190 conn->lgr->conns_num++;
194 /* Unregister connection and reset the alert token of the given connection<
196 static void __smc_lgr_unregister_conn(struct smc_connection *conn)
198 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
199 struct smc_link_group *lgr = conn->lgr;
201 rb_erase(&conn->alert_node, &lgr->conns_all);
203 atomic_dec(&conn->lnk->conn_cnt);
205 conn->alert_token_local = 0;
206 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
209 /* Unregister connection from lgr
211 static void smc_lgr_unregister_conn(struct smc_connection *conn)
213 struct smc_link_group *lgr = conn->lgr;
215 if (!smc_conn_lgr_valid(conn))
217 write_lock_bh(&lgr->conns_lock);
218 if (conn->alert_token_local) {
219 __smc_lgr_unregister_conn(conn);
221 write_unlock_bh(&lgr->conns_lock);
224 int smc_nl_get_sys_info(struct sk_buff *skb, struct netlink_callback *cb)
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;
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);
241 attrs = nla_nest_start(skb, SMC_GEN_SYS_INFO);
244 if (nla_put_u8(skb, SMC_NLA_SYS_VER, SMC_V2))
246 if (nla_put_u8(skb, SMC_NLA_SYS_REL, SMC_RELEASE))
248 if (nla_put_u8(skb, SMC_NLA_SYS_IS_ISM_V2, smc_ism_is_v2_capable()))
250 if (nla_put_u8(skb, SMC_NLA_SYS_IS_SMCR_V2, true))
252 smc_clc_get_hostname(&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))
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))
266 nla_nest_end(skb, attrs);
267 genlmsg_end(skb, nlh);
272 nla_nest_cancel(skb, attrs);
274 genlmsg_cancel(skb, nlh);
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,
282 struct netlink_callback *cb,
283 struct nlattr *v2_attrs)
285 char smc_host[SMC_MAX_HOSTNAME_LEN + 1];
286 char smc_eid[SMC_MAX_EID_LEN + 1];
288 if (nla_put_u8(skb, SMC_NLA_LGR_V2_VER, lgr->smc_version))
290 if (nla_put_u8(skb, SMC_NLA_LGR_V2_REL, lgr->peer_smc_release))
292 if (nla_put_u8(skb, SMC_NLA_LGR_V2_OS, lgr->peer_os))
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))
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))
303 nla_nest_end(skb, v2_attrs);
307 nla_nest_cancel(skb, v2_attrs);
311 static int smc_nl_fill_smcr_lgr_v2(struct smc_link_group *lgr,
313 struct netlink_callback *cb)
315 struct nlattr *v2_attrs;
317 v2_attrs = nla_nest_start(skb, SMC_NLA_LGR_R_V2);
320 if (nla_put_u8(skb, SMC_NLA_LGR_R_V2_DIRECT, !lgr->uses_gateway))
322 if (nla_put_u8(skb, SMC_NLA_LGR_R_V2_MAX_CONNS, lgr->max_conns))
324 if (nla_put_u8(skb, SMC_NLA_LGR_R_V2_MAX_LINKS, lgr->max_links))
327 nla_nest_end(skb, v2_attrs);
331 nla_nest_cancel(skb, v2_attrs);
336 static int smc_nl_fill_lgr(struct smc_link_group *lgr,
338 struct netlink_callback *cb)
340 char smc_target[SMC_MAX_PNETID_LEN + 1];
341 struct nlattr *attrs, *v2_attrs;
343 attrs = nla_nest_start(skb, SMC_GEN_LGR_SMCR);
347 if (nla_put_u32(skb, SMC_NLA_LGR_R_ID, *((u32 *)&lgr->id)))
349 if (nla_put_u32(skb, SMC_NLA_LGR_R_CONNS_NUM, lgr->conns_num))
351 if (nla_put_u8(skb, SMC_NLA_LGR_R_ROLE, lgr->role))
353 if (nla_put_u8(skb, SMC_NLA_LGR_R_TYPE, lgr->type))
355 if (nla_put_u8(skb, SMC_NLA_LGR_R_BUF_TYPE, lgr->buf_type))
357 if (nla_put_u8(skb, SMC_NLA_LGR_R_VLAN_ID, lgr->vlan_id))
359 if (nla_put_u64_64bit(skb, SMC_NLA_LGR_R_NET_COOKIE,
360 lgr->net->net_cookie, SMC_NLA_LGR_R_PAD))
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))
366 if (lgr->smc_version > SMC_V1) {
367 v2_attrs = nla_nest_start(skb, SMC_NLA_LGR_R_V2_COMMON);
370 if (smc_nl_fill_lgr_v2_common(lgr, skb, cb, v2_attrs))
372 if (smc_nl_fill_smcr_lgr_v2(lgr, skb, cb))
376 nla_nest_end(skb, attrs);
379 nla_nest_cancel(skb, attrs);
384 static int smc_nl_fill_lgr_link(struct smc_link_group *lgr,
385 struct smc_link *link,
387 struct netlink_callback *cb)
389 char smc_ibname[IB_DEVICE_NAME_MAX];
390 u8 smc_gid_target[41];
391 struct nlattr *attrs;
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);
401 attrs = nla_nest_start(skb, SMC_GEN_LINK_SMCR);
405 if (nla_put_u8(skb, SMC_NLA_LINK_ID, link->link_id))
407 if (nla_put_u32(skb, SMC_NLA_LINK_STATE, link->state))
409 if (nla_put_u32(skb, SMC_NLA_LINK_CONN_CNT,
410 atomic_read(&link->conn_cnt)))
412 if (nla_put_u8(skb, SMC_NLA_LINK_IB_PORT, link->ibport))
414 if (nla_put_u32(skb, SMC_NLA_LINK_NET_DEV, link->ndev_ifidx))
416 snprintf(smc_ibname, sizeof(smc_ibname), "%s", link->ibname);
417 if (nla_put_string(skb, SMC_NLA_LINK_IB_DEV, smc_ibname))
419 memcpy(&link_uid, link->link_uid, sizeof(link_uid));
420 if (nla_put_u32(skb, SMC_NLA_LINK_UID, link_uid))
422 memcpy(&link_uid, link->peer_link_uid, sizeof(link_uid));
423 if (nla_put_u32(skb, SMC_NLA_LINK_PEER_UID, link_uid))
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))
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))
434 nla_nest_end(skb, attrs);
435 genlmsg_end(skb, nlh);
438 nla_nest_cancel(skb, attrs);
440 genlmsg_cancel(skb, nlh);
445 static int smc_nl_handle_lgr(struct smc_link_group *lgr,
447 struct netlink_callback *cb,
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);
458 if (smc_nl_fill_lgr(lgr, skb, cb))
461 genlmsg_end(skb, nlh);
464 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
465 if (!smc_link_usable(&lgr->lnk[i]))
467 if (smc_nl_fill_lgr_link(lgr, &lgr->lnk[i], skb, cb))
474 genlmsg_cancel(skb, nlh);
479 static void smc_nl_fill_lgr_list(struct smc_lgr_list *smc_lgr,
481 struct netlink_callback *cb,
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];
489 spin_lock_bh(&smc_lgr->lock);
490 list_for_each_entry(lgr, &smc_lgr->list, list) {
493 if (smc_nl_handle_lgr(lgr, skb, cb, list_links))
499 spin_unlock_bh(&smc_lgr->lock);
500 cb_ctx->pos[0] = num;
503 static int smc_nl_fill_smcd_lgr(struct smc_link_group *lgr,
505 struct netlink_callback *cb)
507 char smc_pnet[SMC_MAX_PNETID_LEN + 1];
508 struct smcd_dev *smcd = lgr->smcd;
509 struct nlattr *attrs;
512 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
513 &smc_gen_nl_family, NLM_F_MULTI,
514 SMC_NETLINK_GET_LGR_SMCD);
518 attrs = nla_nest_start(skb, SMC_GEN_LGR_SMCD);
522 if (nla_put_u32(skb, SMC_NLA_LGR_D_ID, *((u32 *)&lgr->id)))
524 if (nla_put_u64_64bit(skb, SMC_NLA_LGR_D_GID,
525 smcd->ops->get_local_gid(smcd),
528 if (nla_put_u64_64bit(skb, SMC_NLA_LGR_D_PEER_GID, lgr->peer_gid,
531 if (nla_put_u8(skb, SMC_NLA_LGR_D_VLAN_ID, lgr->vlan_id))
533 if (nla_put_u32(skb, SMC_NLA_LGR_D_CONNS_NUM, lgr->conns_num))
535 if (nla_put_u32(skb, SMC_NLA_LGR_D_CHID, smc_ism_get_chid(lgr->smcd)))
537 memcpy(smc_pnet, lgr->smcd->pnetid, SMC_MAX_PNETID_LEN);
538 smc_pnet[SMC_MAX_PNETID_LEN] = 0;
539 if (nla_put_string(skb, SMC_NLA_LGR_D_PNETID, smc_pnet))
541 if (lgr->smc_version > SMC_V1) {
542 struct nlattr *v2_attrs;
544 v2_attrs = nla_nest_start(skb, SMC_NLA_LGR_D_V2_COMMON);
547 if (smc_nl_fill_lgr_v2_common(lgr, skb, cb, v2_attrs))
550 nla_nest_end(skb, attrs);
551 genlmsg_end(skb, nlh);
555 nla_nest_cancel(skb, attrs);
557 genlmsg_cancel(skb, nlh);
562 static int smc_nl_handle_smcd_lgr(struct smcd_dev *dev,
564 struct netlink_callback *cb)
566 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
567 struct smc_link_group *lgr;
568 int snum = cb_ctx->pos[1];
571 spin_lock_bh(&dev->lgr_lock);
572 list_for_each_entry(lgr, &dev->lgr_list, list) {
577 rc = smc_nl_fill_smcd_lgr(lgr, skb, cb);
584 spin_unlock_bh(&dev->lgr_lock);
585 cb_ctx->pos[1] = num;
589 static int smc_nl_fill_smcd_dev(struct smcd_dev_list *dev_list,
591 struct netlink_callback *cb)
593 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
594 struct smcd_dev *smcd_dev;
595 int snum = cb_ctx->pos[0];
598 mutex_lock(&dev_list->mutex);
599 list_for_each_entry(smcd_dev, &dev_list->list, list) {
600 if (list_empty(&smcd_dev->lgr_list))
604 rc = smc_nl_handle_smcd_lgr(smcd_dev, skb, cb);
611 mutex_unlock(&dev_list->mutex);
612 cb_ctx->pos[0] = num;
616 int smcr_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb)
618 bool list_links = false;
620 smc_nl_fill_lgr_list(&smc_lgr_list, skb, cb, list_links);
624 int smcr_nl_get_link(struct sk_buff *skb, struct netlink_callback *cb)
626 bool list_links = true;
628 smc_nl_fill_lgr_list(&smc_lgr_list, skb, cb, list_links);
632 int smcd_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb)
634 smc_nl_fill_smcd_dev(&smcd_dev_list, skb, cb);
638 void smc_lgr_cleanup_early(struct smc_link_group *lgr)
640 spinlock_t *lgr_lock;
645 smc_lgr_list_head(lgr, &lgr_lock);
646 spin_lock_bh(lgr_lock);
647 /* do not use this link group for new connections */
648 if (!list_empty(&lgr->list))
649 list_del_init(&lgr->list);
650 spin_unlock_bh(lgr_lock);
651 __smc_lgr_terminate(lgr, true);
654 static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr)
658 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
659 struct smc_link *lnk = &lgr->lnk[i];
661 if (smc_link_sendable(lnk))
662 lnk->state = SMC_LNK_INACTIVE;
664 wake_up_all(&lgr->llc_msg_waiter);
665 wake_up_all(&lgr->llc_flow_waiter);
668 static void smc_lgr_free(struct smc_link_group *lgr);
670 static void smc_lgr_free_work(struct work_struct *work)
672 struct smc_link_group *lgr = container_of(to_delayed_work(work),
673 struct smc_link_group,
675 spinlock_t *lgr_lock;
678 smc_lgr_list_head(lgr, &lgr_lock);
679 spin_lock_bh(lgr_lock);
681 spin_unlock_bh(lgr_lock);
684 read_lock_bh(&lgr->conns_lock);
685 conns = RB_EMPTY_ROOT(&lgr->conns_all);
686 read_unlock_bh(&lgr->conns_lock);
687 if (!conns) { /* number of lgr connections is no longer zero */
688 spin_unlock_bh(lgr_lock);
691 list_del_init(&lgr->list); /* remove from smc_lgr_list */
692 lgr->freeing = 1; /* this instance does the freeing, no new schedule */
693 spin_unlock_bh(lgr_lock);
694 cancel_delayed_work(&lgr->free_work);
696 if (!lgr->is_smcd && !lgr->terminating)
697 smc_llc_send_link_delete_all(lgr, true,
698 SMC_LLC_DEL_PROG_INIT_TERM);
699 if (lgr->is_smcd && !lgr->terminating)
700 smc_ism_signal_shutdown(lgr);
702 smcr_lgr_link_deactivate_all(lgr);
706 static void smc_lgr_terminate_work(struct work_struct *work)
708 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
711 __smc_lgr_terminate(lgr, true);
714 /* return next unique link id for the lgr */
715 static u8 smcr_next_link_id(struct smc_link_group *lgr)
722 link_id = ++lgr->next_link_id;
723 if (!link_id) /* skip zero as link_id */
724 link_id = ++lgr->next_link_id;
725 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
726 if (smc_link_usable(&lgr->lnk[i]) &&
727 lgr->lnk[i].link_id == link_id)
735 static void smcr_copy_dev_info_to_link(struct smc_link *link)
737 struct smc_ib_device *smcibdev = link->smcibdev;
739 snprintf(link->ibname, sizeof(link->ibname), "%s",
740 smcibdev->ibdev->name);
741 link->ndev_ifidx = smcibdev->ndev_ifidx[link->ibport - 1];
744 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
745 u8 link_idx, struct smc_init_info *ini)
747 struct smc_ib_device *smcibdev;
751 if (lgr->smc_version == SMC_V2) {
752 lnk->smcibdev = ini->smcrv2.ib_dev_v2;
753 lnk->ibport = ini->smcrv2.ib_port_v2;
755 lnk->smcibdev = ini->ib_dev;
756 lnk->ibport = ini->ib_port;
758 get_device(&lnk->smcibdev->ibdev->dev);
759 atomic_inc(&lnk->smcibdev->lnk_cnt);
760 refcount_set(&lnk->refcnt, 1); /* link refcnt is set to 1 */
762 lnk->path_mtu = lnk->smcibdev->pattr[lnk->ibport - 1].active_mtu;
763 lnk->link_id = smcr_next_link_id(lgr);
765 smc_lgr_hold(lgr); /* lgr_put in smcr_link_clear() */
766 lnk->link_idx = link_idx;
767 lnk->wr_rx_id_compl = 0;
768 smc_ibdev_cnt_inc(lnk);
769 smcr_copy_dev_info_to_link(lnk);
770 atomic_set(&lnk->conn_cnt, 0);
771 smc_llc_link_set_uid(lnk);
772 INIT_WORK(&lnk->link_down_wrk, smc_link_down_work);
773 if (!lnk->smcibdev->initialized) {
774 rc = (int)smc_ib_setup_per_ibdev(lnk->smcibdev);
778 get_random_bytes(rndvec, sizeof(rndvec));
779 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
781 rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
782 ini->vlan_id, lnk->gid, &lnk->sgid_index,
783 lgr->smc_version == SMC_V2 ?
784 &ini->smcrv2 : NULL);
787 rc = smc_llc_link_init(lnk);
790 rc = smc_wr_alloc_link_mem(lnk);
793 rc = smc_ib_create_protection_domain(lnk);
796 rc = smc_ib_create_queue_pair(lnk);
799 rc = smc_wr_create_link(lnk);
802 lnk->state = SMC_LNK_ACTIVATING;
806 smc_ib_destroy_queue_pair(lnk);
808 smc_ib_dealloc_protection_domain(lnk);
810 smc_wr_free_link_mem(lnk);
812 smc_llc_link_clear(lnk, false);
814 smc_ibdev_cnt_dec(lnk);
815 put_device(&lnk->smcibdev->ibdev->dev);
816 smcibdev = lnk->smcibdev;
817 memset(lnk, 0, sizeof(struct smc_link));
818 lnk->state = SMC_LNK_UNUSED;
819 if (!atomic_dec_return(&smcibdev->lnk_cnt))
820 wake_up(&smcibdev->lnks_deleted);
821 smc_lgr_put(lgr); /* lgr_hold above */
825 /* create a new SMC link group */
826 static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
828 struct smc_link_group *lgr;
829 struct list_head *lgr_list;
830 struct smcd_dev *smcd;
831 struct smc_link *lnk;
832 spinlock_t *lgr_lock;
837 if (ini->is_smcd && ini->vlan_id) {
838 if (smc_ism_get_vlan(ini->ism_dev[ini->ism_selected],
840 rc = SMC_CLC_DECL_ISMVLANERR;
845 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
847 rc = SMC_CLC_DECL_MEM;
850 lgr->tx_wq = alloc_workqueue("smc_tx_wq-%*phN", 0, 0,
851 SMC_LGR_ID_SIZE, &lgr->id);
856 lgr->is_smcd = ini->is_smcd;
858 lgr->terminating = 0;
860 lgr->vlan_id = ini->vlan_id;
861 refcount_set(&lgr->refcnt, 1); /* set lgr refcnt to 1 */
862 init_rwsem(&lgr->sndbufs_lock);
863 init_rwsem(&lgr->rmbs_lock);
864 rwlock_init(&lgr->conns_lock);
865 for (i = 0; i < SMC_RMBE_SIZES; i++) {
866 INIT_LIST_HEAD(&lgr->sndbufs[i]);
867 INIT_LIST_HEAD(&lgr->rmbs[i]);
869 lgr->next_link_id = 0;
870 smc_lgr_list.num += SMC_LGR_NUM_INCR;
871 memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
872 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
873 INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work);
874 lgr->conns_all = RB_ROOT;
876 /* SMC-D specific settings */
877 smcd = ini->ism_dev[ini->ism_selected];
878 get_device(smcd->ops->get_dev(smcd));
879 lgr->peer_gid = ini->ism_peer_gid[ini->ism_selected];
880 lgr->smcd = ini->ism_dev[ini->ism_selected];
881 lgr_list = &ini->ism_dev[ini->ism_selected]->lgr_list;
882 lgr_lock = &lgr->smcd->lgr_lock;
883 lgr->smc_version = ini->smcd_version;
884 lgr->peer_shutdown = 0;
885 atomic_inc(&ini->ism_dev[ini->ism_selected]->lgr_cnt);
887 /* SMC-R specific settings */
888 struct smc_ib_device *ibdev;
891 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
892 lgr->smc_version = ini->smcr_version;
893 memcpy(lgr->peer_systemid, ini->peer_systemid,
895 if (lgr->smc_version == SMC_V2) {
896 ibdev = ini->smcrv2.ib_dev_v2;
897 ibport = ini->smcrv2.ib_port_v2;
898 lgr->saddr = ini->smcrv2.saddr;
899 lgr->uses_gateway = ini->smcrv2.uses_gateway;
900 memcpy(lgr->nexthop_mac, ini->smcrv2.nexthop_mac,
902 lgr->max_conns = ini->max_conns;
903 lgr->max_links = ini->max_links;
906 ibport = ini->ib_port;
907 lgr->max_conns = SMC_CONN_PER_LGR_MAX;
908 lgr->max_links = SMC_LINKS_ADD_LNK_MAX;
910 memcpy(lgr->pnet_id, ibdev->pnetid[ibport - 1],
912 rc = smc_wr_alloc_lgr_mem(lgr);
915 smc_llc_lgr_init(lgr, smc);
917 link_idx = SMC_SINGLE_LINK;
918 lnk = &lgr->lnk[link_idx];
919 rc = smcr_link_init(lgr, lnk, link_idx, ini);
921 smc_wr_free_lgr_mem(lgr);
924 lgr->net = smc_ib_net(lnk->smcibdev);
925 lgr_list = &smc_lgr_list.list;
926 lgr_lock = &smc_lgr_list.lock;
927 lgr->buf_type = lgr->net->smc.sysctl_smcr_buf_type;
928 atomic_inc(&lgr_cnt);
931 spin_lock_bh(lgr_lock);
932 list_add_tail(&lgr->list, lgr_list);
933 spin_unlock_bh(lgr_lock);
937 destroy_workqueue(lgr->tx_wq);
941 if (ini->is_smcd && ini->vlan_id)
942 smc_ism_put_vlan(ini->ism_dev[ini->ism_selected], ini->vlan_id);
946 rc = SMC_CLC_DECL_MEM;
948 rc = SMC_CLC_DECL_INTERR;
953 static int smc_write_space(struct smc_connection *conn)
955 int buffer_len = conn->peer_rmbe_size;
956 union smc_host_cursor prod;
957 union smc_host_cursor cons;
960 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
961 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
962 /* determine rx_buf space */
963 space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
967 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
968 struct smc_wr_buf *wr_buf)
970 struct smc_connection *conn = &smc->conn;
971 union smc_host_cursor cons, fin;
975 smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
976 smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
977 /* set prod cursor to old state, enforce tx_rdma_writes() */
978 smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
979 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
981 if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
982 /* cons cursor advanced more than fin, and prod was set
983 * fin above, so now prod is smaller than cons. Fix that.
985 diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
986 smc_curs_add(conn->sndbuf_desc->len,
987 &conn->tx_curs_sent, diff);
988 smc_curs_add(conn->sndbuf_desc->len,
989 &conn->tx_curs_fin, diff);
991 smp_mb__before_atomic();
992 atomic_add(diff, &conn->sndbuf_space);
993 smp_mb__after_atomic();
995 smc_curs_add(conn->peer_rmbe_size,
996 &conn->local_tx_ctrl.prod, diff);
997 smc_curs_add(conn->peer_rmbe_size,
998 &conn->local_tx_ctrl_fin, diff);
1000 /* recalculate, value is used by tx_rdma_writes() */
1001 atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));
1003 if (smc->sk.sk_state != SMC_INIT &&
1004 smc->sk.sk_state != SMC_CLOSED) {
1005 rc = smcr_cdc_msg_send_validation(conn, pend, wr_buf);
1007 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, 0);
1008 smc->sk.sk_data_ready(&smc->sk);
1011 smc_wr_tx_put_slot(conn->lnk,
1012 (struct smc_wr_tx_pend_priv *)pend);
1017 void smc_switch_link_and_count(struct smc_connection *conn,
1018 struct smc_link *to_lnk)
1020 atomic_dec(&conn->lnk->conn_cnt);
1021 /* link_hold in smc_conn_create() */
1022 smcr_link_put(conn->lnk);
1024 atomic_inc(&conn->lnk->conn_cnt);
1025 /* link_put in smc_conn_free() */
1026 smcr_link_hold(conn->lnk);
1029 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
1030 struct smc_link *from_lnk, bool is_dev_err)
1032 struct smc_link *to_lnk = NULL;
1033 struct smc_cdc_tx_pend *pend;
1034 struct smc_connection *conn;
1035 struct smc_wr_buf *wr_buf;
1036 struct smc_sock *smc;
1037 struct rb_node *node;
1040 /* link is inactive, wake up tx waiters */
1041 smc_wr_wakeup_tx_wait(from_lnk);
1043 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1044 if (!smc_link_active(&lgr->lnk[i]) || i == from_lnk->link_idx)
1046 if (is_dev_err && from_lnk->smcibdev == lgr->lnk[i].smcibdev &&
1047 from_lnk->ibport == lgr->lnk[i].ibport) {
1050 to_lnk = &lgr->lnk[i];
1053 if (!to_lnk || !smc_wr_tx_link_hold(to_lnk)) {
1054 smc_lgr_terminate_sched(lgr);
1058 read_lock_bh(&lgr->conns_lock);
1059 for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
1060 conn = rb_entry(node, struct smc_connection, alert_node);
1061 if (conn->lnk != from_lnk)
1063 smc = container_of(conn, struct smc_sock, conn);
1064 /* conn->lnk not yet set in SMC_INIT state */
1065 if (smc->sk.sk_state == SMC_INIT)
1067 if (smc->sk.sk_state == SMC_CLOSED ||
1068 smc->sk.sk_state == SMC_PEERCLOSEWAIT1 ||
1069 smc->sk.sk_state == SMC_PEERCLOSEWAIT2 ||
1070 smc->sk.sk_state == SMC_APPFINCLOSEWAIT ||
1071 smc->sk.sk_state == SMC_APPCLOSEWAIT1 ||
1072 smc->sk.sk_state == SMC_APPCLOSEWAIT2 ||
1073 smc->sk.sk_state == SMC_PEERFINCLOSEWAIT ||
1074 smc->sk.sk_state == SMC_PEERABORTWAIT ||
1075 smc->sk.sk_state == SMC_PROCESSABORT) {
1076 spin_lock_bh(&conn->send_lock);
1077 smc_switch_link_and_count(conn, to_lnk);
1078 spin_unlock_bh(&conn->send_lock);
1081 sock_hold(&smc->sk);
1082 read_unlock_bh(&lgr->conns_lock);
1083 /* pre-fetch buffer outside of send_lock, might sleep */
1084 rc = smc_cdc_get_free_slot(conn, to_lnk, &wr_buf, NULL, &pend);
1087 /* avoid race with smcr_tx_sndbuf_nonempty() */
1088 spin_lock_bh(&conn->send_lock);
1089 smc_switch_link_and_count(conn, to_lnk);
1090 rc = smc_switch_cursor(smc, pend, wr_buf);
1091 spin_unlock_bh(&conn->send_lock);
1097 read_unlock_bh(&lgr->conns_lock);
1098 smc_wr_tx_link_put(to_lnk);
1102 smcr_link_down_cond_sched(to_lnk);
1103 smc_wr_tx_link_put(to_lnk);
1107 static void smcr_buf_unuse(struct smc_buf_desc *buf_desc, bool is_rmb,
1108 struct smc_link_group *lgr)
1110 struct rw_semaphore *lock; /* lock buffer list */
1113 if (is_rmb && buf_desc->is_conf_rkey && !list_empty(&lgr->list)) {
1114 /* unregister rmb with peer */
1115 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
1117 /* protect against smc_llc_cli_rkey_exchange() */
1118 down_read(&lgr->llc_conf_mutex);
1119 smc_llc_do_delete_rkey(lgr, buf_desc);
1120 buf_desc->is_conf_rkey = false;
1121 up_read(&lgr->llc_conf_mutex);
1122 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1126 if (buf_desc->is_reg_err) {
1127 /* buf registration failed, reuse not possible */
1128 lock = is_rmb ? &lgr->rmbs_lock :
1131 list_del(&buf_desc->list);
1134 smc_buf_free(lgr, is_rmb, buf_desc);
1136 /* memzero_explicit provides potential memory barrier semantics */
1137 memzero_explicit(buf_desc->cpu_addr, buf_desc->len);
1138 WRITE_ONCE(buf_desc->used, 0);
1142 static void smc_buf_unuse(struct smc_connection *conn,
1143 struct smc_link_group *lgr)
1145 if (conn->sndbuf_desc) {
1146 if (!lgr->is_smcd && conn->sndbuf_desc->is_vm) {
1147 smcr_buf_unuse(conn->sndbuf_desc, false, lgr);
1149 memzero_explicit(conn->sndbuf_desc->cpu_addr, conn->sndbuf_desc->len);
1150 WRITE_ONCE(conn->sndbuf_desc->used, 0);
1153 if (conn->rmb_desc) {
1154 if (!lgr->is_smcd) {
1155 smcr_buf_unuse(conn->rmb_desc, true, lgr);
1157 memzero_explicit(conn->rmb_desc->cpu_addr,
1158 conn->rmb_desc->len + sizeof(struct smcd_cdc_msg));
1159 WRITE_ONCE(conn->rmb_desc->used, 0);
1164 /* remove a finished connection from its link group */
1165 void smc_conn_free(struct smc_connection *conn)
1167 struct smc_link_group *lgr = conn->lgr;
1169 if (!lgr || conn->freed)
1170 /* Connection has never been registered in a
1171 * link group, or has already been freed.
1176 if (!smc_conn_lgr_valid(conn))
1177 /* Connection has already unregistered from
1183 if (!list_empty(&lgr->list))
1184 smc_ism_unset_conn(conn);
1185 tasklet_kill(&conn->rx_tsklet);
1187 smc_cdc_wait_pend_tx_wr(conn);
1188 if (current_work() != &conn->abort_work)
1189 cancel_work_sync(&conn->abort_work);
1191 if (!list_empty(&lgr->list)) {
1192 smc_buf_unuse(conn, lgr); /* allow buffer reuse */
1193 smc_lgr_unregister_conn(conn);
1196 if (!lgr->conns_num)
1197 smc_lgr_schedule_free_work(lgr);
1200 smcr_link_put(conn->lnk); /* link_hold in smc_conn_create() */
1201 smc_lgr_put(lgr); /* lgr_hold in smc_conn_create() */
1204 /* unregister a link from a buf_desc */
1205 static void smcr_buf_unmap_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1206 struct smc_link *lnk)
1208 if (is_rmb || buf_desc->is_vm)
1209 buf_desc->is_reg_mr[lnk->link_idx] = false;
1210 if (!buf_desc->is_map_ib[lnk->link_idx])
1213 if ((is_rmb || buf_desc->is_vm) &&
1214 buf_desc->mr[lnk->link_idx]) {
1215 smc_ib_put_memory_region(buf_desc->mr[lnk->link_idx]);
1216 buf_desc->mr[lnk->link_idx] = NULL;
1219 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE);
1221 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE);
1223 sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1224 buf_desc->is_map_ib[lnk->link_idx] = false;
1227 /* unmap all buffers of lgr for a deleted link */
1228 static void smcr_buf_unmap_lgr(struct smc_link *lnk)
1230 struct smc_link_group *lgr = lnk->lgr;
1231 struct smc_buf_desc *buf_desc, *bf;
1234 for (i = 0; i < SMC_RMBE_SIZES; i++) {
1235 down_write(&lgr->rmbs_lock);
1236 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list)
1237 smcr_buf_unmap_link(buf_desc, true, lnk);
1238 up_write(&lgr->rmbs_lock);
1240 down_write(&lgr->sndbufs_lock);
1241 list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i],
1243 smcr_buf_unmap_link(buf_desc, false, lnk);
1244 up_write(&lgr->sndbufs_lock);
1248 static void smcr_rtoken_clear_link(struct smc_link *lnk)
1250 struct smc_link_group *lgr = lnk->lgr;
1253 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1254 lgr->rtokens[i][lnk->link_idx].rkey = 0;
1255 lgr->rtokens[i][lnk->link_idx].dma_addr = 0;
1259 static void __smcr_link_clear(struct smc_link *lnk)
1261 struct smc_link_group *lgr = lnk->lgr;
1262 struct smc_ib_device *smcibdev;
1264 smc_wr_free_link_mem(lnk);
1265 smc_ibdev_cnt_dec(lnk);
1266 put_device(&lnk->smcibdev->ibdev->dev);
1267 smcibdev = lnk->smcibdev;
1268 memset(lnk, 0, sizeof(struct smc_link));
1269 lnk->state = SMC_LNK_UNUSED;
1270 if (!atomic_dec_return(&smcibdev->lnk_cnt))
1271 wake_up(&smcibdev->lnks_deleted);
1272 smc_lgr_put(lgr); /* lgr_hold in smcr_link_init() */
1275 /* must be called under lgr->llc_conf_mutex lock */
1276 void smcr_link_clear(struct smc_link *lnk, bool log)
1278 if (!lnk->lgr || lnk->clearing ||
1279 lnk->state == SMC_LNK_UNUSED)
1283 smc_llc_link_clear(lnk, log);
1284 smcr_buf_unmap_lgr(lnk);
1285 smcr_rtoken_clear_link(lnk);
1286 smc_ib_modify_qp_error(lnk);
1287 smc_wr_free_link(lnk);
1288 smc_ib_destroy_queue_pair(lnk);
1289 smc_ib_dealloc_protection_domain(lnk);
1290 smcr_link_put(lnk); /* theoretically last link_put */
1293 void smcr_link_hold(struct smc_link *lnk)
1295 refcount_inc(&lnk->refcnt);
1298 void smcr_link_put(struct smc_link *lnk)
1300 if (refcount_dec_and_test(&lnk->refcnt))
1301 __smcr_link_clear(lnk);
1304 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
1305 struct smc_buf_desc *buf_desc)
1309 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1310 smcr_buf_unmap_link(buf_desc, is_rmb, &lgr->lnk[i]);
1312 if (!buf_desc->is_vm && buf_desc->pages)
1313 __free_pages(buf_desc->pages, buf_desc->order);
1314 else if (buf_desc->is_vm && buf_desc->cpu_addr)
1315 vfree(buf_desc->cpu_addr);
1319 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
1320 struct smc_buf_desc *buf_desc)
1323 /* restore original buf len */
1324 buf_desc->len += sizeof(struct smcd_cdc_msg);
1325 smc_ism_unregister_dmb(lgr->smcd, buf_desc);
1327 kfree(buf_desc->cpu_addr);
1332 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
1333 struct smc_buf_desc *buf_desc)
1336 smcd_buf_free(lgr, is_rmb, buf_desc);
1338 smcr_buf_free(lgr, is_rmb, buf_desc);
1341 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
1343 struct smc_buf_desc *buf_desc, *bf_desc;
1344 struct list_head *buf_list;
1347 for (i = 0; i < SMC_RMBE_SIZES; i++) {
1349 buf_list = &lgr->rmbs[i];
1351 buf_list = &lgr->sndbufs[i];
1352 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
1354 list_del(&buf_desc->list);
1355 smc_buf_free(lgr, is_rmb, buf_desc);
1360 static void smc_lgr_free_bufs(struct smc_link_group *lgr)
1362 /* free send buffers */
1363 __smc_lgr_free_bufs(lgr, false);
1365 __smc_lgr_free_bufs(lgr, true);
1368 /* won't be freed until no one accesses to lgr anymore */
1369 static void __smc_lgr_free(struct smc_link_group *lgr)
1371 smc_lgr_free_bufs(lgr);
1373 if (!atomic_dec_return(&lgr->smcd->lgr_cnt))
1374 wake_up(&lgr->smcd->lgrs_deleted);
1376 smc_wr_free_lgr_mem(lgr);
1377 if (!atomic_dec_return(&lgr_cnt))
1378 wake_up(&lgrs_deleted);
1383 /* remove a link group */
1384 static void smc_lgr_free(struct smc_link_group *lgr)
1388 if (!lgr->is_smcd) {
1389 down_write(&lgr->llc_conf_mutex);
1390 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1391 if (lgr->lnk[i].state != SMC_LNK_UNUSED)
1392 smcr_link_clear(&lgr->lnk[i], false);
1394 up_write(&lgr->llc_conf_mutex);
1395 smc_llc_lgr_clear(lgr);
1398 destroy_workqueue(lgr->tx_wq);
1400 smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
1401 put_device(lgr->smcd->ops->get_dev(lgr->smcd));
1403 smc_lgr_put(lgr); /* theoretically last lgr_put */
1406 void smc_lgr_hold(struct smc_link_group *lgr)
1408 refcount_inc(&lgr->refcnt);
1411 void smc_lgr_put(struct smc_link_group *lgr)
1413 if (refcount_dec_and_test(&lgr->refcnt))
1414 __smc_lgr_free(lgr);
1417 static void smc_sk_wake_ups(struct smc_sock *smc)
1419 smc->sk.sk_write_space(&smc->sk);
1420 smc->sk.sk_data_ready(&smc->sk);
1421 smc->sk.sk_state_change(&smc->sk);
1424 /* kill a connection */
1425 static void smc_conn_kill(struct smc_connection *conn, bool soft)
1427 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1429 if (conn->lgr->is_smcd && conn->lgr->peer_shutdown)
1430 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
1432 smc_close_abort(conn);
1434 smc->sk.sk_err = ECONNABORTED;
1435 smc_sk_wake_ups(smc);
1436 if (conn->lgr->is_smcd) {
1437 smc_ism_unset_conn(conn);
1439 tasklet_kill(&conn->rx_tsklet);
1441 tasklet_unlock_wait(&conn->rx_tsklet);
1443 smc_cdc_wait_pend_tx_wr(conn);
1445 smc_lgr_unregister_conn(conn);
1446 smc_close_active_abort(smc);
1449 static void smc_lgr_cleanup(struct smc_link_group *lgr)
1452 smc_ism_signal_shutdown(lgr);
1454 u32 rsn = lgr->llc_termination_rsn;
1457 rsn = SMC_LLC_DEL_PROG_INIT_TERM;
1458 smc_llc_send_link_delete_all(lgr, false, rsn);
1459 smcr_lgr_link_deactivate_all(lgr);
1463 /* terminate link group
1464 * @soft: true if link group shutdown can take its time
1465 * false if immediate link group shutdown is required
1467 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
1469 struct smc_connection *conn;
1470 struct smc_sock *smc;
1471 struct rb_node *node;
1473 if (lgr->terminating)
1474 return; /* lgr already terminating */
1475 /* cancel free_work sync, will terminate when lgr->freeing is set */
1476 cancel_delayed_work(&lgr->free_work);
1477 lgr->terminating = 1;
1479 /* kill remaining link group connections */
1480 read_lock_bh(&lgr->conns_lock);
1481 node = rb_first(&lgr->conns_all);
1483 read_unlock_bh(&lgr->conns_lock);
1484 conn = rb_entry(node, struct smc_connection, alert_node);
1485 smc = container_of(conn, struct smc_sock, conn);
1486 sock_hold(&smc->sk); /* sock_put below */
1487 lock_sock(&smc->sk);
1488 smc_conn_kill(conn, soft);
1489 release_sock(&smc->sk);
1490 sock_put(&smc->sk); /* sock_hold above */
1491 read_lock_bh(&lgr->conns_lock);
1492 node = rb_first(&lgr->conns_all);
1494 read_unlock_bh(&lgr->conns_lock);
1495 smc_lgr_cleanup(lgr);
1499 /* unlink link group and schedule termination */
1500 void smc_lgr_terminate_sched(struct smc_link_group *lgr)
1502 spinlock_t *lgr_lock;
1504 smc_lgr_list_head(lgr, &lgr_lock);
1505 spin_lock_bh(lgr_lock);
1506 if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) {
1507 spin_unlock_bh(lgr_lock);
1508 return; /* lgr already terminating */
1510 list_del_init(&lgr->list);
1512 spin_unlock_bh(lgr_lock);
1513 schedule_work(&lgr->terminate_work);
1516 /* Called when peer lgr shutdown (regularly or abnormally) is received */
1517 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan)
1519 struct smc_link_group *lgr, *l;
1520 LIST_HEAD(lgr_free_list);
1522 /* run common cleanup function and build free list */
1523 spin_lock_bh(&dev->lgr_lock);
1524 list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) {
1525 if ((!peer_gid || lgr->peer_gid == peer_gid) &&
1526 (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) {
1527 if (peer_gid) /* peer triggered termination */
1528 lgr->peer_shutdown = 1;
1529 list_move(&lgr->list, &lgr_free_list);
1533 spin_unlock_bh(&dev->lgr_lock);
1535 /* cancel the regular free workers and actually free lgrs */
1536 list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
1537 list_del_init(&lgr->list);
1538 schedule_work(&lgr->terminate_work);
1542 /* Called when an SMCD device is removed or the smc module is unloaded */
1543 void smc_smcd_terminate_all(struct smcd_dev *smcd)
1545 struct smc_link_group *lgr, *lg;
1546 LIST_HEAD(lgr_free_list);
1548 spin_lock_bh(&smcd->lgr_lock);
1549 list_splice_init(&smcd->lgr_list, &lgr_free_list);
1550 list_for_each_entry(lgr, &lgr_free_list, list)
1552 spin_unlock_bh(&smcd->lgr_lock);
1554 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1555 list_del_init(&lgr->list);
1556 __smc_lgr_terminate(lgr, false);
1559 if (atomic_read(&smcd->lgr_cnt))
1560 wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt));
1563 /* Called when an SMCR device is removed or the smc module is unloaded.
1564 * If smcibdev is given, all SMCR link groups using this device are terminated.
1565 * If smcibdev is NULL, all SMCR link groups are terminated.
1567 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
1569 struct smc_link_group *lgr, *lg;
1570 LIST_HEAD(lgr_free_list);
1573 spin_lock_bh(&smc_lgr_list.lock);
1575 list_splice_init(&smc_lgr_list.list, &lgr_free_list);
1576 list_for_each_entry(lgr, &lgr_free_list, list)
1579 list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
1580 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1581 if (lgr->lnk[i].smcibdev == smcibdev)
1582 smcr_link_down_cond_sched(&lgr->lnk[i]);
1586 spin_unlock_bh(&smc_lgr_list.lock);
1588 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1589 list_del_init(&lgr->list);
1590 smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM);
1591 __smc_lgr_terminate(lgr, false);
1595 if (atomic_read(&smcibdev->lnk_cnt))
1596 wait_event(smcibdev->lnks_deleted,
1597 !atomic_read(&smcibdev->lnk_cnt));
1599 if (atomic_read(&lgr_cnt))
1600 wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
1604 /* set new lgr type and clear all asymmetric link tagging */
1605 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type)
1607 char *lgr_type = "";
1610 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1611 if (smc_link_usable(&lgr->lnk[i]))
1612 lgr->lnk[i].link_is_asym = false;
1613 if (lgr->type == new_type)
1615 lgr->type = new_type;
1617 switch (lgr->type) {
1621 case SMC_LGR_SINGLE:
1622 lgr_type = "SINGLE";
1624 case SMC_LGR_SYMMETRIC:
1625 lgr_type = "SYMMETRIC";
1627 case SMC_LGR_ASYMMETRIC_PEER:
1628 lgr_type = "ASYMMETRIC_PEER";
1630 case SMC_LGR_ASYMMETRIC_LOCAL:
1631 lgr_type = "ASYMMETRIC_LOCAL";
1634 pr_warn_ratelimited("smc: SMC-R lg %*phN net %llu state changed: "
1635 "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id,
1636 lgr->net->net_cookie, lgr_type, lgr->pnet_id);
1639 /* set new lgr type and tag a link as asymmetric */
1640 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
1641 enum smc_lgr_type new_type, int asym_lnk_idx)
1643 smcr_lgr_set_type(lgr, new_type);
1644 lgr->lnk[asym_lnk_idx].link_is_asym = true;
1647 /* abort connection, abort_work scheduled from tasklet context */
1648 static void smc_conn_abort_work(struct work_struct *work)
1650 struct smc_connection *conn = container_of(work,
1651 struct smc_connection,
1653 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1655 lock_sock(&smc->sk);
1656 smc_conn_kill(conn, true);
1657 release_sock(&smc->sk);
1658 sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
1661 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport)
1663 struct smc_link_group *lgr, *n;
1665 spin_lock_bh(&smc_lgr_list.lock);
1666 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1667 struct smc_link *link;
1669 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1670 SMC_MAX_PNETID_LEN) ||
1671 lgr->type == SMC_LGR_SYMMETRIC ||
1672 lgr->type == SMC_LGR_ASYMMETRIC_PEER ||
1673 !rdma_dev_access_netns(smcibdev->ibdev, lgr->net))
1676 if (lgr->type == SMC_LGR_SINGLE && lgr->max_links <= 1)
1679 /* trigger local add link processing */
1680 link = smc_llc_usable_link(lgr);
1682 smc_llc_add_link_local(link);
1684 spin_unlock_bh(&smc_lgr_list.lock);
1687 /* link is down - switch connections to alternate link,
1688 * must be called under lgr->llc_conf_mutex lock
1690 static void smcr_link_down(struct smc_link *lnk)
1692 struct smc_link_group *lgr = lnk->lgr;
1693 struct smc_link *to_lnk;
1696 if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list))
1699 to_lnk = smc_switch_conns(lgr, lnk, true);
1700 if (!to_lnk) { /* no backup link available */
1701 smcr_link_clear(lnk, true);
1704 smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1705 del_link_id = lnk->link_id;
1707 if (lgr->role == SMC_SERV) {
1708 /* trigger local delete link processing */
1709 smc_llc_srv_delete_link_local(to_lnk, del_link_id);
1711 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1712 /* another llc task is ongoing */
1713 up_write(&lgr->llc_conf_mutex);
1714 wait_event_timeout(lgr->llc_flow_waiter,
1715 (list_empty(&lgr->list) ||
1716 lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE),
1718 down_write(&lgr->llc_conf_mutex);
1720 if (!list_empty(&lgr->list)) {
1721 smc_llc_send_delete_link(to_lnk, del_link_id,
1723 SMC_LLC_DEL_LOST_PATH);
1724 smcr_link_clear(lnk, true);
1726 wake_up(&lgr->llc_flow_waiter); /* wake up next waiter */
1730 /* must be called under lgr->llc_conf_mutex lock */
1731 void smcr_link_down_cond(struct smc_link *lnk)
1733 if (smc_link_downing(&lnk->state)) {
1734 trace_smcr_link_down(lnk, __builtin_return_address(0));
1735 smcr_link_down(lnk);
1739 /* will get the lgr->llc_conf_mutex lock */
1740 void smcr_link_down_cond_sched(struct smc_link *lnk)
1742 if (smc_link_downing(&lnk->state)) {
1743 trace_smcr_link_down(lnk, __builtin_return_address(0));
1744 schedule_work(&lnk->link_down_wrk);
1748 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport)
1750 struct smc_link_group *lgr, *n;
1753 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1754 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1755 SMC_MAX_PNETID_LEN))
1756 continue; /* lgr is not affected */
1757 if (list_empty(&lgr->list))
1759 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1760 struct smc_link *lnk = &lgr->lnk[i];
1762 if (smc_link_usable(lnk) &&
1763 lnk->smcibdev == smcibdev && lnk->ibport == ibport)
1764 smcr_link_down_cond_sched(lnk);
1769 static void smc_link_down_work(struct work_struct *work)
1771 struct smc_link *link = container_of(work, struct smc_link,
1773 struct smc_link_group *lgr = link->lgr;
1775 if (list_empty(&lgr->list))
1777 wake_up_all(&lgr->llc_msg_waiter);
1778 down_write(&lgr->llc_conf_mutex);
1779 smcr_link_down(link);
1780 up_write(&lgr->llc_conf_mutex);
1783 static int smc_vlan_by_tcpsk_walk(struct net_device *lower_dev,
1784 struct netdev_nested_priv *priv)
1786 unsigned short *vlan_id = (unsigned short *)priv->data;
1788 if (is_vlan_dev(lower_dev)) {
1789 *vlan_id = vlan_dev_vlan_id(lower_dev);
1796 /* Determine vlan of internal TCP socket. */
1797 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
1799 struct dst_entry *dst = sk_dst_get(clcsock->sk);
1800 struct netdev_nested_priv priv;
1801 struct net_device *ndev;
1815 if (is_vlan_dev(ndev)) {
1816 ini->vlan_id = vlan_dev_vlan_id(ndev);
1820 priv.data = (void *)&ini->vlan_id;
1822 netdev_walk_all_lower_dev(ndev, smc_vlan_by_tcpsk_walk, &priv);
1831 static bool smcr_lgr_match(struct smc_link_group *lgr, u8 smcr_version,
1835 enum smc_lgr_role role, u32 clcqpn,
1838 struct smc_link *lnk;
1841 if (memcmp(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN) ||
1845 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1848 if (!smc_link_active(lnk))
1850 /* use verbs API to check netns, instead of lgr->net */
1851 if (!rdma_dev_access_netns(lnk->smcibdev->ibdev, net))
1853 if ((lgr->role == SMC_SERV || lnk->peer_qpn == clcqpn) &&
1854 !memcmp(lnk->peer_gid, peer_gid, SMC_GID_SIZE) &&
1855 (smcr_version == SMC_V2 ||
1856 !memcmp(lnk->peer_mac, peer_mac_v1, ETH_ALEN)))
1862 static bool smcd_lgr_match(struct smc_link_group *lgr,
1863 struct smcd_dev *smcismdev, u64 peer_gid)
1865 return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
1868 /* create a new SMC connection (and a new link group if necessary) */
1869 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
1871 struct smc_connection *conn = &smc->conn;
1872 struct net *net = sock_net(&smc->sk);
1873 struct list_head *lgr_list;
1874 struct smc_link_group *lgr;
1875 enum smc_lgr_role role;
1876 spinlock_t *lgr_lock;
1879 lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list :
1881 lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock :
1883 ini->first_contact_local = 1;
1884 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
1885 if (role == SMC_CLNT && ini->first_contact_peer)
1886 /* create new link group as well */
1889 /* determine if an existing link group can be reused */
1890 spin_lock_bh(lgr_lock);
1891 list_for_each_entry(lgr, lgr_list, list) {
1892 write_lock_bh(&lgr->conns_lock);
1894 smcd_lgr_match(lgr, ini->ism_dev[ini->ism_selected],
1895 ini->ism_peer_gid[ini->ism_selected]) :
1896 smcr_lgr_match(lgr, ini->smcr_version,
1898 ini->peer_gid, ini->peer_mac, role,
1899 ini->ib_clcqpn, net)) &&
1901 (ini->smcd_version == SMC_V2 ||
1902 lgr->vlan_id == ini->vlan_id) &&
1903 (role == SMC_CLNT || ini->is_smcd ||
1904 (lgr->conns_num < lgr->max_conns &&
1905 !bitmap_full(lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX)))) {
1906 /* link group found */
1907 ini->first_contact_local = 0;
1909 rc = smc_lgr_register_conn(conn, false);
1910 write_unlock_bh(&lgr->conns_lock);
1911 if (!rc && delayed_work_pending(&lgr->free_work))
1912 cancel_delayed_work(&lgr->free_work);
1915 write_unlock_bh(&lgr->conns_lock);
1917 spin_unlock_bh(lgr_lock);
1921 if (role == SMC_CLNT && !ini->first_contact_peer &&
1922 ini->first_contact_local) {
1923 /* Server reuses a link group, but Client wants to start
1925 * send out_of_sync decline, reason synchr. error
1927 return SMC_CLC_DECL_SYNCERR;
1931 if (ini->first_contact_local) {
1932 rc = smc_lgr_create(smc, ini);
1936 write_lock_bh(&lgr->conns_lock);
1937 rc = smc_lgr_register_conn(conn, true);
1938 write_unlock_bh(&lgr->conns_lock);
1940 smc_lgr_cleanup_early(lgr);
1944 smc_lgr_hold(conn->lgr); /* lgr_put in smc_conn_free() */
1945 if (!conn->lgr->is_smcd)
1946 smcr_link_hold(conn->lnk); /* link_put in smc_conn_free() */
1948 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1949 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
1950 conn->urg_state = SMC_URG_READ;
1951 init_waitqueue_head(&conn->cdc_pend_tx_wq);
1952 INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work);
1954 conn->rx_off = sizeof(struct smcd_cdc_msg);
1955 smcd_cdc_rx_init(conn); /* init tasklet for this conn */
1959 #ifndef KERNEL_HAS_ATOMIC64
1960 spin_lock_init(&conn->acurs_lock);
1967 #define SMCD_DMBE_SIZES 6 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1968 #define SMCR_RMBE_SIZES 5 /* 0 -> 16KB, 1 -> 32KB, .. 5 -> 512KB */
1970 /* convert the RMB size into the compressed notation (minimum 16K, see
1971 * SMCD/R_DMBE_SIZES.
1972 * In contrast to plain ilog2, this rounds towards the next power of 2,
1973 * so the socket application gets at least its desired sndbuf / rcvbuf size.
1975 static u8 smc_compress_bufsize(int size, bool is_smcd, bool is_rmb)
1977 const unsigned int max_scat = SG_MAX_SINGLE_ALLOC * PAGE_SIZE;
1980 if (size <= SMC_BUF_MIN_SIZE)
1983 size = (size - 1) >> 14; /* convert to 16K multiple */
1984 compressed = min_t(u8, ilog2(size) + 1,
1985 is_smcd ? SMCD_DMBE_SIZES : SMCR_RMBE_SIZES);
1987 if (!is_smcd && is_rmb)
1988 /* RMBs are backed by & limited to max size of scatterlists */
1989 compressed = min_t(u8, compressed, ilog2(max_scat >> 14));
1994 /* convert the RMB size from compressed notation into integer */
1995 int smc_uncompress_bufsize(u8 compressed)
1999 size = 0x00000001 << (((int)compressed) + 14);
2003 /* try to reuse a sndbuf or rmb description slot for a certain
2004 * buffer size; if not available, return NULL
2006 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
2007 struct rw_semaphore *lock,
2008 struct list_head *buf_list)
2010 struct smc_buf_desc *buf_slot;
2013 list_for_each_entry(buf_slot, buf_list, list) {
2014 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
2023 /* one of the conditions for announcing a receiver's current window size is
2024 * that it "results in a minimum increase in the window size of 10% of the
2025 * receive buffer space" [RFC7609]
2027 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
2029 return max_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
2032 /* map an buf to a link */
2033 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
2034 struct smc_link *lnk)
2036 int rc, i, nents, offset, buf_size, size, access_flags;
2037 struct scatterlist *sg;
2040 if (buf_desc->is_map_ib[lnk->link_idx])
2043 if (buf_desc->is_vm) {
2044 buf = buf_desc->cpu_addr;
2045 buf_size = buf_desc->len;
2046 offset = offset_in_page(buf_desc->cpu_addr);
2047 nents = PAGE_ALIGN(buf_size + offset) / PAGE_SIZE;
2052 rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], nents, GFP_KERNEL);
2056 if (buf_desc->is_vm) {
2057 /* virtually contiguous buffer */
2058 for_each_sg(buf_desc->sgt[lnk->link_idx].sgl, sg, nents, i) {
2059 size = min_t(int, PAGE_SIZE - offset, buf_size);
2060 sg_set_page(sg, vmalloc_to_page(buf), size, offset);
2061 buf += size / sizeof(*buf);
2066 /* physically contiguous buffer */
2067 sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
2068 buf_desc->cpu_addr, buf_desc->len);
2071 /* map sg table to DMA address */
2072 rc = smc_ib_buf_map_sg(lnk, buf_desc,
2073 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2074 /* SMC protocol depends on mapping to one DMA address only */
2080 buf_desc->is_dma_need_sync |=
2081 smc_ib_is_sg_need_sync(lnk, buf_desc) << lnk->link_idx;
2083 if (is_rmb || buf_desc->is_vm) {
2084 /* create a new memory region for the RMB or vzalloced sndbuf */
2085 access_flags = is_rmb ?
2086 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
2087 IB_ACCESS_LOCAL_WRITE;
2089 rc = smc_ib_get_memory_region(lnk->roce_pd, access_flags,
2090 buf_desc, lnk->link_idx);
2093 smc_ib_sync_sg_for_device(lnk, buf_desc,
2094 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2096 buf_desc->is_map_ib[lnk->link_idx] = true;
2100 smc_ib_buf_unmap_sg(lnk, buf_desc,
2101 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2103 sg_free_table(&buf_desc->sgt[lnk->link_idx]);
2107 /* register a new buf on IB device, rmb or vzalloced sndbuf
2108 * must be called under lgr->llc_conf_mutex lock
2110 int smcr_link_reg_buf(struct smc_link *link, struct smc_buf_desc *buf_desc)
2112 if (list_empty(&link->lgr->list))
2114 if (!buf_desc->is_reg_mr[link->link_idx]) {
2115 /* register memory region for new buf */
2116 if (buf_desc->is_vm)
2117 buf_desc->mr[link->link_idx]->iova =
2118 (uintptr_t)buf_desc->cpu_addr;
2119 if (smc_wr_reg_send(link, buf_desc->mr[link->link_idx])) {
2120 buf_desc->is_reg_err = true;
2123 buf_desc->is_reg_mr[link->link_idx] = true;
2128 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct rw_semaphore *lock,
2129 struct list_head *lst, bool is_rmb)
2131 struct smc_buf_desc *buf_desc, *bf;
2135 list_for_each_entry_safe(buf_desc, bf, lst, list) {
2136 if (!buf_desc->used)
2138 rc = smcr_buf_map_link(buf_desc, is_rmb, lnk);
2147 /* map all used buffers of lgr for a new link */
2148 int smcr_buf_map_lgr(struct smc_link *lnk)
2150 struct smc_link_group *lgr = lnk->lgr;
2153 for (i = 0; i < SMC_RMBE_SIZES; i++) {
2154 rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock,
2155 &lgr->rmbs[i], true);
2158 rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock,
2159 &lgr->sndbufs[i], false);
2166 /* register all used buffers of lgr for a new link,
2167 * must be called under lgr->llc_conf_mutex lock
2169 int smcr_buf_reg_lgr(struct smc_link *lnk)
2171 struct smc_link_group *lgr = lnk->lgr;
2172 struct smc_buf_desc *buf_desc, *bf;
2175 /* reg all RMBs for a new link */
2176 down_write(&lgr->rmbs_lock);
2177 for (i = 0; i < SMC_RMBE_SIZES; i++) {
2178 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) {
2179 if (!buf_desc->used)
2181 rc = smcr_link_reg_buf(lnk, buf_desc);
2183 up_write(&lgr->rmbs_lock);
2188 up_write(&lgr->rmbs_lock);
2190 if (lgr->buf_type == SMCR_PHYS_CONT_BUFS)
2193 /* reg all vzalloced sndbufs for a new link */
2194 down_write(&lgr->sndbufs_lock);
2195 for (i = 0; i < SMC_RMBE_SIZES; i++) {
2196 list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i], list) {
2197 if (!buf_desc->used || !buf_desc->is_vm)
2199 rc = smcr_link_reg_buf(lnk, buf_desc);
2201 up_write(&lgr->sndbufs_lock);
2206 up_write(&lgr->sndbufs_lock);
2210 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
2211 bool is_rmb, int bufsize)
2213 struct smc_buf_desc *buf_desc;
2215 /* try to alloc a new buffer */
2216 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
2218 return ERR_PTR(-ENOMEM);
2220 switch (lgr->buf_type) {
2221 case SMCR_PHYS_CONT_BUFS:
2222 case SMCR_MIXED_BUFS:
2223 buf_desc->order = get_order(bufsize);
2224 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
2225 __GFP_NOMEMALLOC | __GFP_COMP |
2226 __GFP_NORETRY | __GFP_ZERO,
2228 if (buf_desc->pages) {
2229 buf_desc->cpu_addr =
2230 (void *)page_address(buf_desc->pages);
2231 buf_desc->len = bufsize;
2232 buf_desc->is_vm = false;
2235 if (lgr->buf_type == SMCR_PHYS_CONT_BUFS)
2237 fallthrough; // try virtually continguous buf
2238 case SMCR_VIRT_CONT_BUFS:
2239 buf_desc->order = get_order(bufsize);
2240 buf_desc->cpu_addr = vzalloc(PAGE_SIZE << buf_desc->order);
2241 if (!buf_desc->cpu_addr)
2243 buf_desc->pages = NULL;
2244 buf_desc->len = bufsize;
2245 buf_desc->is_vm = true;
2252 return ERR_PTR(-EAGAIN);
2255 /* map buf_desc on all usable links,
2256 * unused buffers stay mapped as long as the link is up
2258 static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
2259 struct smc_buf_desc *buf_desc, bool is_rmb)
2261 int i, rc = 0, cnt = 0;
2263 /* protect against parallel link reconfiguration */
2264 down_read(&lgr->llc_conf_mutex);
2265 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2266 struct smc_link *lnk = &lgr->lnk[i];
2268 if (!smc_link_usable(lnk))
2270 if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
2277 up_read(&lgr->llc_conf_mutex);
2283 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
2284 bool is_dmb, int bufsize)
2286 struct smc_buf_desc *buf_desc;
2289 /* try to alloc a new DMB */
2290 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
2292 return ERR_PTR(-ENOMEM);
2294 rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
2298 return ERR_PTR(-EAGAIN);
2300 return ERR_PTR(-ENOSPC);
2301 return ERR_PTR(-EIO);
2303 buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
2304 /* CDC header stored in buf. So, pretend it was smaller */
2305 buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
2307 buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
2308 __GFP_NOWARN | __GFP_NORETRY |
2310 if (!buf_desc->cpu_addr) {
2312 return ERR_PTR(-EAGAIN);
2314 buf_desc->len = bufsize;
2319 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
2321 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
2322 struct smc_connection *conn = &smc->conn;
2323 struct smc_link_group *lgr = conn->lgr;
2324 struct list_head *buf_list;
2325 int bufsize, bufsize_comp;
2326 struct rw_semaphore *lock; /* lock buffer list */
2327 bool is_dgraded = false;
2330 /* use socket recv buffer size (w/o overhead) as start value */
2331 bufsize = smc->sk.sk_rcvbuf / 2;
2333 /* use socket send buffer size (w/o overhead) as start value */
2334 bufsize = smc->sk.sk_sndbuf / 2;
2336 for (bufsize_comp = smc_compress_bufsize(bufsize, is_smcd, is_rmb);
2337 bufsize_comp >= 0; bufsize_comp--) {
2339 lock = &lgr->rmbs_lock;
2340 buf_list = &lgr->rmbs[bufsize_comp];
2342 lock = &lgr->sndbufs_lock;
2343 buf_list = &lgr->sndbufs[bufsize_comp];
2345 bufsize = smc_uncompress_bufsize(bufsize_comp);
2347 /* check for reusable slot in the link group */
2348 buf_desc = smc_buf_get_slot(bufsize_comp, lock, buf_list);
2350 buf_desc->is_dma_need_sync = 0;
2351 SMC_STAT_RMB_SIZE(smc, is_smcd, is_rmb, bufsize);
2352 SMC_STAT_BUF_REUSE(smc, is_smcd, is_rmb);
2353 break; /* found reusable slot */
2357 buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
2359 buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
2361 if (PTR_ERR(buf_desc) == -ENOMEM)
2363 if (IS_ERR(buf_desc)) {
2366 SMC_STAT_RMB_DOWNGRADED(smc, is_smcd, is_rmb);
2371 SMC_STAT_RMB_ALLOC(smc, is_smcd, is_rmb);
2372 SMC_STAT_RMB_SIZE(smc, is_smcd, is_rmb, bufsize);
2375 list_add(&buf_desc->list, buf_list);
2380 if (IS_ERR(buf_desc))
2381 return PTR_ERR(buf_desc);
2384 if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
2385 smcr_buf_unuse(buf_desc, is_rmb, lgr);
2391 conn->rmb_desc = buf_desc;
2392 conn->rmbe_size_comp = bufsize_comp;
2393 smc->sk.sk_rcvbuf = bufsize * 2;
2394 atomic_set(&conn->bytes_to_rcv, 0);
2395 conn->rmbe_update_limit =
2396 smc_rmb_wnd_update_limit(buf_desc->len);
2398 smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
2400 conn->sndbuf_desc = buf_desc;
2401 smc->sk.sk_sndbuf = bufsize * 2;
2402 atomic_set(&conn->sndbuf_space, bufsize);
2407 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
2409 if (!conn->sndbuf_desc->is_dma_need_sync)
2411 if (!smc_conn_lgr_valid(conn) || conn->lgr->is_smcd ||
2412 !smc_link_active(conn->lnk))
2414 smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
2417 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
2421 if (!conn->rmb_desc->is_dma_need_sync)
2423 if (!smc_conn_lgr_valid(conn) || conn->lgr->is_smcd)
2425 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2426 if (!smc_link_active(&conn->lgr->lnk[i]))
2428 smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
2433 /* create the send and receive buffer for an SMC socket;
2434 * receive buffers are called RMBs;
2435 * (even though the SMC protocol allows more than one RMB-element per RMB,
2436 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
2437 * extra RMB for every connection in a link group
2439 int smc_buf_create(struct smc_sock *smc, bool is_smcd)
2443 /* create send buffer */
2444 rc = __smc_buf_create(smc, is_smcd, false);
2448 rc = __smc_buf_create(smc, is_smcd, true);
2450 down_write(&smc->conn.lgr->sndbufs_lock);
2451 list_del(&smc->conn.sndbuf_desc->list);
2452 up_write(&smc->conn.lgr->sndbufs_lock);
2453 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
2454 smc->conn.sndbuf_desc = NULL;
2459 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
2463 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
2464 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
2470 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx,
2475 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2476 if (test_bit(i, lgr->rtokens_used_mask) &&
2477 lgr->rtokens[i][lnk_idx].rkey == rkey)
2483 /* set rtoken for a new link to an existing rmb */
2484 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
2485 __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey)
2489 rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known));
2490 if (rtok_idx == -ENOENT)
2492 lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey);
2493 lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr);
2496 /* set rtoken for a new link whose link_id is given */
2497 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
2498 __be64 nw_vaddr, __be32 nw_rkey)
2500 u64 dma_addr = be64_to_cpu(nw_vaddr);
2501 u32 rkey = ntohl(nw_rkey);
2505 for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) {
2506 if (lgr->lnk[link_idx].link_id == link_id) {
2513 lgr->rtokens[rtok_idx][link_idx].rkey = rkey;
2514 lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr;
2517 /* add a new rtoken from peer */
2518 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
2520 struct smc_link_group *lgr = smc_get_lgr(lnk);
2521 u64 dma_addr = be64_to_cpu(nw_vaddr);
2522 u32 rkey = ntohl(nw_rkey);
2525 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2526 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
2527 lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
2528 test_bit(i, lgr->rtokens_used_mask)) {
2529 /* already in list */
2533 i = smc_rmb_reserve_rtoken_idx(lgr);
2536 lgr->rtokens[i][lnk->link_idx].rkey = rkey;
2537 lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
2541 /* delete an rtoken from all links */
2542 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
2544 struct smc_link_group *lgr = smc_get_lgr(lnk);
2545 u32 rkey = ntohl(nw_rkey);
2548 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2549 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
2550 test_bit(i, lgr->rtokens_used_mask)) {
2551 for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
2552 lgr->rtokens[i][j].rkey = 0;
2553 lgr->rtokens[i][j].dma_addr = 0;
2555 clear_bit(i, lgr->rtokens_used_mask);
2562 /* save rkey and dma_addr received from peer during clc handshake */
2563 int smc_rmb_rtoken_handling(struct smc_connection *conn,
2564 struct smc_link *lnk,
2565 struct smc_clc_msg_accept_confirm *clc)
2567 conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr,
2569 if (conn->rtoken_idx < 0)
2570 return conn->rtoken_idx;
2574 static void smc_core_going_away(void)
2576 struct smc_ib_device *smcibdev;
2577 struct smcd_dev *smcd;
2579 mutex_lock(&smc_ib_devices.mutex);
2580 list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
2583 for (i = 0; i < SMC_MAX_PORTS; i++)
2584 set_bit(i, smcibdev->ports_going_away);
2586 mutex_unlock(&smc_ib_devices.mutex);
2588 mutex_lock(&smcd_dev_list.mutex);
2589 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2590 smcd->going_away = 1;
2592 mutex_unlock(&smcd_dev_list.mutex);
2595 /* Clean up all SMC link groups */
2596 static void smc_lgrs_shutdown(void)
2598 struct smcd_dev *smcd;
2600 smc_core_going_away();
2602 smc_smcr_terminate_all(NULL);
2604 mutex_lock(&smcd_dev_list.mutex);
2605 list_for_each_entry(smcd, &smcd_dev_list.list, list)
2606 smc_smcd_terminate_all(smcd);
2607 mutex_unlock(&smcd_dev_list.mutex);
2610 static int smc_core_reboot_event(struct notifier_block *this,
2611 unsigned long event, void *ptr)
2613 smc_lgrs_shutdown();
2614 smc_ib_unregister_client();
2619 static struct notifier_block smc_reboot_notifier = {
2620 .notifier_call = smc_core_reboot_event,
2623 int __init smc_core_init(void)
2625 return register_reboot_notifier(&smc_reboot_notifier);
2628 /* Called (from smc_exit) when module is removed */
2629 void smc_core_exit(void)
2631 unregister_reboot_notifier(&smc_reboot_notifier);
2632 smc_lgrs_shutdown();