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