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