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