xref: /openbmc/linux/net/smc/smc_core.c (revision 240e6d25)
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 		link_id = ++lgr->next_link_id;
712 		if (!link_id)	/* skip zero as link_id */
713 			link_id = ++lgr->next_link_id;
714 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
715 			if (smc_link_usable(&lgr->lnk[i]) &&
716 			    lgr->lnk[i].link_id == link_id)
717 				continue;
718 		}
719 		break;
720 	}
721 	return link_id;
722 }
723 
724 static void smcr_copy_dev_info_to_link(struct smc_link *link)
725 {
726 	struct smc_ib_device *smcibdev = link->smcibdev;
727 
728 	snprintf(link->ibname, sizeof(link->ibname), "%s",
729 		 smcibdev->ibdev->name);
730 	link->ndev_ifidx = smcibdev->ndev_ifidx[link->ibport - 1];
731 }
732 
733 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
734 		   u8 link_idx, struct smc_init_info *ini)
735 {
736 	struct smc_ib_device *smcibdev;
737 	u8 rndvec[3];
738 	int rc;
739 
740 	if (lgr->smc_version == SMC_V2) {
741 		lnk->smcibdev = ini->smcrv2.ib_dev_v2;
742 		lnk->ibport = ini->smcrv2.ib_port_v2;
743 	} else {
744 		lnk->smcibdev = ini->ib_dev;
745 		lnk->ibport = ini->ib_port;
746 	}
747 	get_device(&lnk->smcibdev->ibdev->dev);
748 	atomic_inc(&lnk->smcibdev->lnk_cnt);
749 	lnk->path_mtu = lnk->smcibdev->pattr[lnk->ibport - 1].active_mtu;
750 	lnk->link_id = smcr_next_link_id(lgr);
751 	lnk->lgr = lgr;
752 	lnk->link_idx = link_idx;
753 	smc_ibdev_cnt_inc(lnk);
754 	smcr_copy_dev_info_to_link(lnk);
755 	atomic_set(&lnk->conn_cnt, 0);
756 	smc_llc_link_set_uid(lnk);
757 	INIT_WORK(&lnk->link_down_wrk, smc_link_down_work);
758 	if (!lnk->smcibdev->initialized) {
759 		rc = (int)smc_ib_setup_per_ibdev(lnk->smcibdev);
760 		if (rc)
761 			goto out;
762 	}
763 	get_random_bytes(rndvec, sizeof(rndvec));
764 	lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
765 		(rndvec[2] << 16);
766 	rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
767 				  ini->vlan_id, lnk->gid, &lnk->sgid_index,
768 				  lgr->smc_version == SMC_V2 ?
769 						  &ini->smcrv2 : NULL);
770 	if (rc)
771 		goto out;
772 	rc = smc_llc_link_init(lnk);
773 	if (rc)
774 		goto out;
775 	rc = smc_wr_alloc_link_mem(lnk);
776 	if (rc)
777 		goto clear_llc_lnk;
778 	rc = smc_ib_create_protection_domain(lnk);
779 	if (rc)
780 		goto free_link_mem;
781 	rc = smc_ib_create_queue_pair(lnk);
782 	if (rc)
783 		goto dealloc_pd;
784 	rc = smc_wr_create_link(lnk);
785 	if (rc)
786 		goto destroy_qp;
787 	lnk->state = SMC_LNK_ACTIVATING;
788 	return 0;
789 
790 destroy_qp:
791 	smc_ib_destroy_queue_pair(lnk);
792 dealloc_pd:
793 	smc_ib_dealloc_protection_domain(lnk);
794 free_link_mem:
795 	smc_wr_free_link_mem(lnk);
796 clear_llc_lnk:
797 	smc_llc_link_clear(lnk, false);
798 out:
799 	smc_ibdev_cnt_dec(lnk);
800 	put_device(&lnk->smcibdev->ibdev->dev);
801 	smcibdev = lnk->smcibdev;
802 	memset(lnk, 0, sizeof(struct smc_link));
803 	lnk->state = SMC_LNK_UNUSED;
804 	if (!atomic_dec_return(&smcibdev->lnk_cnt))
805 		wake_up(&smcibdev->lnks_deleted);
806 	return rc;
807 }
808 
809 /* create a new SMC link group */
810 static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
811 {
812 	struct smc_link_group *lgr;
813 	struct list_head *lgr_list;
814 	struct smc_link *lnk;
815 	spinlock_t *lgr_lock;
816 	u8 link_idx;
817 	int rc = 0;
818 	int i;
819 
820 	if (ini->is_smcd && ini->vlan_id) {
821 		if (smc_ism_get_vlan(ini->ism_dev[ini->ism_selected],
822 				     ini->vlan_id)) {
823 			rc = SMC_CLC_DECL_ISMVLANERR;
824 			goto out;
825 		}
826 	}
827 
828 	lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
829 	if (!lgr) {
830 		rc = SMC_CLC_DECL_MEM;
831 		goto ism_put_vlan;
832 	}
833 	lgr->tx_wq = alloc_workqueue("smc_tx_wq-%*phN", 0, 0,
834 				     SMC_LGR_ID_SIZE, &lgr->id);
835 	if (!lgr->tx_wq) {
836 		rc = -ENOMEM;
837 		goto free_lgr;
838 	}
839 	lgr->is_smcd = ini->is_smcd;
840 	lgr->sync_err = 0;
841 	lgr->terminating = 0;
842 	lgr->freeing = 0;
843 	lgr->vlan_id = ini->vlan_id;
844 	mutex_init(&lgr->sndbufs_lock);
845 	mutex_init(&lgr->rmbs_lock);
846 	rwlock_init(&lgr->conns_lock);
847 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
848 		INIT_LIST_HEAD(&lgr->sndbufs[i]);
849 		INIT_LIST_HEAD(&lgr->rmbs[i]);
850 	}
851 	lgr->next_link_id = 0;
852 	smc_lgr_list.num += SMC_LGR_NUM_INCR;
853 	memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
854 	INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
855 	INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work);
856 	lgr->conns_all = RB_ROOT;
857 	if (ini->is_smcd) {
858 		/* SMC-D specific settings */
859 		get_device(&ini->ism_dev[ini->ism_selected]->dev);
860 		lgr->peer_gid = ini->ism_peer_gid[ini->ism_selected];
861 		lgr->smcd = ini->ism_dev[ini->ism_selected];
862 		lgr_list = &ini->ism_dev[ini->ism_selected]->lgr_list;
863 		lgr_lock = &lgr->smcd->lgr_lock;
864 		lgr->smc_version = ini->smcd_version;
865 		lgr->peer_shutdown = 0;
866 		atomic_inc(&ini->ism_dev[ini->ism_selected]->lgr_cnt);
867 	} else {
868 		/* SMC-R specific settings */
869 		struct smc_ib_device *ibdev;
870 		int ibport;
871 
872 		lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
873 		lgr->smc_version = ini->smcr_version;
874 		memcpy(lgr->peer_systemid, ini->peer_systemid,
875 		       SMC_SYSTEMID_LEN);
876 		if (lgr->smc_version == SMC_V2) {
877 			ibdev = ini->smcrv2.ib_dev_v2;
878 			ibport = ini->smcrv2.ib_port_v2;
879 			lgr->saddr = ini->smcrv2.saddr;
880 			lgr->uses_gateway = ini->smcrv2.uses_gateway;
881 			memcpy(lgr->nexthop_mac, ini->smcrv2.nexthop_mac,
882 			       ETH_ALEN);
883 		} else {
884 			ibdev = ini->ib_dev;
885 			ibport = ini->ib_port;
886 		}
887 		memcpy(lgr->pnet_id, ibdev->pnetid[ibport - 1],
888 		       SMC_MAX_PNETID_LEN);
889 		if (smc_wr_alloc_lgr_mem(lgr))
890 			goto free_wq;
891 		smc_llc_lgr_init(lgr, smc);
892 
893 		link_idx = SMC_SINGLE_LINK;
894 		lnk = &lgr->lnk[link_idx];
895 		rc = smcr_link_init(lgr, lnk, link_idx, ini);
896 		if (rc) {
897 			smc_wr_free_lgr_mem(lgr);
898 			goto free_wq;
899 		}
900 		lgr_list = &smc_lgr_list.list;
901 		lgr_lock = &smc_lgr_list.lock;
902 		atomic_inc(&lgr_cnt);
903 	}
904 	smc->conn.lgr = lgr;
905 	spin_lock_bh(lgr_lock);
906 	list_add_tail(&lgr->list, lgr_list);
907 	spin_unlock_bh(lgr_lock);
908 	return 0;
909 
910 free_wq:
911 	destroy_workqueue(lgr->tx_wq);
912 free_lgr:
913 	kfree(lgr);
914 ism_put_vlan:
915 	if (ini->is_smcd && ini->vlan_id)
916 		smc_ism_put_vlan(ini->ism_dev[ini->ism_selected], ini->vlan_id);
917 out:
918 	if (rc < 0) {
919 		if (rc == -ENOMEM)
920 			rc = SMC_CLC_DECL_MEM;
921 		else
922 			rc = SMC_CLC_DECL_INTERR;
923 	}
924 	return rc;
925 }
926 
927 static int smc_write_space(struct smc_connection *conn)
928 {
929 	int buffer_len = conn->peer_rmbe_size;
930 	union smc_host_cursor prod;
931 	union smc_host_cursor cons;
932 	int space;
933 
934 	smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
935 	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
936 	/* determine rx_buf space */
937 	space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
938 	return space;
939 }
940 
941 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
942 			     struct smc_wr_buf *wr_buf)
943 {
944 	struct smc_connection *conn = &smc->conn;
945 	union smc_host_cursor cons, fin;
946 	int rc = 0;
947 	int diff;
948 
949 	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
950 	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
951 	/* set prod cursor to old state, enforce tx_rdma_writes() */
952 	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
953 	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
954 
955 	if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
956 		/* cons cursor advanced more than fin, and prod was set
957 		 * fin above, so now prod is smaller than cons. Fix that.
958 		 */
959 		diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
960 		smc_curs_add(conn->sndbuf_desc->len,
961 			     &conn->tx_curs_sent, diff);
962 		smc_curs_add(conn->sndbuf_desc->len,
963 			     &conn->tx_curs_fin, diff);
964 
965 		smp_mb__before_atomic();
966 		atomic_add(diff, &conn->sndbuf_space);
967 		smp_mb__after_atomic();
968 
969 		smc_curs_add(conn->peer_rmbe_size,
970 			     &conn->local_tx_ctrl.prod, diff);
971 		smc_curs_add(conn->peer_rmbe_size,
972 			     &conn->local_tx_ctrl_fin, diff);
973 	}
974 	/* recalculate, value is used by tx_rdma_writes() */
975 	atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));
976 
977 	if (smc->sk.sk_state != SMC_INIT &&
978 	    smc->sk.sk_state != SMC_CLOSED) {
979 		rc = smcr_cdc_msg_send_validation(conn, pend, wr_buf);
980 		if (!rc) {
981 			queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, 0);
982 			smc->sk.sk_data_ready(&smc->sk);
983 		}
984 	} else {
985 		smc_wr_tx_put_slot(conn->lnk,
986 				   (struct smc_wr_tx_pend_priv *)pend);
987 	}
988 	return rc;
989 }
990 
991 void smc_switch_link_and_count(struct smc_connection *conn,
992 			       struct smc_link *to_lnk)
993 {
994 	atomic_dec(&conn->lnk->conn_cnt);
995 	conn->lnk = to_lnk;
996 	atomic_inc(&conn->lnk->conn_cnt);
997 }
998 
999 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
1000 				  struct smc_link *from_lnk, bool is_dev_err)
1001 {
1002 	struct smc_link *to_lnk = NULL;
1003 	struct smc_cdc_tx_pend *pend;
1004 	struct smc_connection *conn;
1005 	struct smc_wr_buf *wr_buf;
1006 	struct smc_sock *smc;
1007 	struct rb_node *node;
1008 	int i, rc = 0;
1009 
1010 	/* link is inactive, wake up tx waiters */
1011 	smc_wr_wakeup_tx_wait(from_lnk);
1012 
1013 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1014 		if (!smc_link_active(&lgr->lnk[i]) || i == from_lnk->link_idx)
1015 			continue;
1016 		if (is_dev_err && from_lnk->smcibdev == lgr->lnk[i].smcibdev &&
1017 		    from_lnk->ibport == lgr->lnk[i].ibport) {
1018 			continue;
1019 		}
1020 		to_lnk = &lgr->lnk[i];
1021 		break;
1022 	}
1023 	if (!to_lnk || !smc_wr_tx_link_hold(to_lnk)) {
1024 		smc_lgr_terminate_sched(lgr);
1025 		return NULL;
1026 	}
1027 again:
1028 	read_lock_bh(&lgr->conns_lock);
1029 	for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
1030 		conn = rb_entry(node, struct smc_connection, alert_node);
1031 		if (conn->lnk != from_lnk)
1032 			continue;
1033 		smc = container_of(conn, struct smc_sock, conn);
1034 		/* conn->lnk not yet set in SMC_INIT state */
1035 		if (smc->sk.sk_state == SMC_INIT)
1036 			continue;
1037 		if (smc->sk.sk_state == SMC_CLOSED ||
1038 		    smc->sk.sk_state == SMC_PEERCLOSEWAIT1 ||
1039 		    smc->sk.sk_state == SMC_PEERCLOSEWAIT2 ||
1040 		    smc->sk.sk_state == SMC_APPFINCLOSEWAIT ||
1041 		    smc->sk.sk_state == SMC_APPCLOSEWAIT1 ||
1042 		    smc->sk.sk_state == SMC_APPCLOSEWAIT2 ||
1043 		    smc->sk.sk_state == SMC_PEERFINCLOSEWAIT ||
1044 		    smc->sk.sk_state == SMC_PEERABORTWAIT ||
1045 		    smc->sk.sk_state == SMC_PROCESSABORT) {
1046 			spin_lock_bh(&conn->send_lock);
1047 			smc_switch_link_and_count(conn, to_lnk);
1048 			spin_unlock_bh(&conn->send_lock);
1049 			continue;
1050 		}
1051 		sock_hold(&smc->sk);
1052 		read_unlock_bh(&lgr->conns_lock);
1053 		/* pre-fetch buffer outside of send_lock, might sleep */
1054 		rc = smc_cdc_get_free_slot(conn, to_lnk, &wr_buf, NULL, &pend);
1055 		if (rc)
1056 			goto err_out;
1057 		/* avoid race with smcr_tx_sndbuf_nonempty() */
1058 		spin_lock_bh(&conn->send_lock);
1059 		smc_switch_link_and_count(conn, to_lnk);
1060 		rc = smc_switch_cursor(smc, pend, wr_buf);
1061 		spin_unlock_bh(&conn->send_lock);
1062 		sock_put(&smc->sk);
1063 		if (rc)
1064 			goto err_out;
1065 		goto again;
1066 	}
1067 	read_unlock_bh(&lgr->conns_lock);
1068 	smc_wr_tx_link_put(to_lnk);
1069 	return to_lnk;
1070 
1071 err_out:
1072 	smcr_link_down_cond_sched(to_lnk);
1073 	smc_wr_tx_link_put(to_lnk);
1074 	return NULL;
1075 }
1076 
1077 static void smcr_buf_unuse(struct smc_buf_desc *rmb_desc,
1078 			   struct smc_link_group *lgr)
1079 {
1080 	int rc;
1081 
1082 	if (rmb_desc->is_conf_rkey && !list_empty(&lgr->list)) {
1083 		/* unregister rmb with peer */
1084 		rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
1085 		if (!rc) {
1086 			/* protect against smc_llc_cli_rkey_exchange() */
1087 			mutex_lock(&lgr->llc_conf_mutex);
1088 			smc_llc_do_delete_rkey(lgr, rmb_desc);
1089 			rmb_desc->is_conf_rkey = false;
1090 			mutex_unlock(&lgr->llc_conf_mutex);
1091 			smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1092 		}
1093 	}
1094 
1095 	if (rmb_desc->is_reg_err) {
1096 		/* buf registration failed, reuse not possible */
1097 		mutex_lock(&lgr->rmbs_lock);
1098 		list_del(&rmb_desc->list);
1099 		mutex_unlock(&lgr->rmbs_lock);
1100 
1101 		smc_buf_free(lgr, true, rmb_desc);
1102 	} else {
1103 		rmb_desc->used = 0;
1104 	}
1105 }
1106 
1107 static void smc_buf_unuse(struct smc_connection *conn,
1108 			  struct smc_link_group *lgr)
1109 {
1110 	if (conn->sndbuf_desc)
1111 		conn->sndbuf_desc->used = 0;
1112 	if (conn->rmb_desc && lgr->is_smcd)
1113 		conn->rmb_desc->used = 0;
1114 	else if (conn->rmb_desc)
1115 		smcr_buf_unuse(conn->rmb_desc, lgr);
1116 }
1117 
1118 /* remove a finished connection from its link group */
1119 void smc_conn_free(struct smc_connection *conn)
1120 {
1121 	struct smc_link_group *lgr = conn->lgr;
1122 
1123 	if (!lgr)
1124 		return;
1125 	if (lgr->is_smcd) {
1126 		if (!list_empty(&lgr->list))
1127 			smc_ism_unset_conn(conn);
1128 		tasklet_kill(&conn->rx_tsklet);
1129 	} else {
1130 		smc_cdc_tx_dismiss_slots(conn);
1131 		if (current_work() != &conn->abort_work)
1132 			cancel_work_sync(&conn->abort_work);
1133 	}
1134 	if (!list_empty(&lgr->list)) {
1135 		smc_lgr_unregister_conn(conn);
1136 		smc_buf_unuse(conn, lgr); /* allow buffer reuse */
1137 	}
1138 
1139 	if (!lgr->conns_num)
1140 		smc_lgr_schedule_free_work(lgr);
1141 }
1142 
1143 /* unregister a link from a buf_desc */
1144 static void smcr_buf_unmap_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1145 				struct smc_link *lnk)
1146 {
1147 	if (is_rmb)
1148 		buf_desc->is_reg_mr[lnk->link_idx] = false;
1149 	if (!buf_desc->is_map_ib[lnk->link_idx])
1150 		return;
1151 	if (is_rmb) {
1152 		if (buf_desc->mr_rx[lnk->link_idx]) {
1153 			smc_ib_put_memory_region(
1154 					buf_desc->mr_rx[lnk->link_idx]);
1155 			buf_desc->mr_rx[lnk->link_idx] = NULL;
1156 		}
1157 		smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE);
1158 	} else {
1159 		smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE);
1160 	}
1161 	sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1162 	buf_desc->is_map_ib[lnk->link_idx] = false;
1163 }
1164 
1165 /* unmap all buffers of lgr for a deleted link */
1166 static void smcr_buf_unmap_lgr(struct smc_link *lnk)
1167 {
1168 	struct smc_link_group *lgr = lnk->lgr;
1169 	struct smc_buf_desc *buf_desc, *bf;
1170 	int i;
1171 
1172 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
1173 		mutex_lock(&lgr->rmbs_lock);
1174 		list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list)
1175 			smcr_buf_unmap_link(buf_desc, true, lnk);
1176 		mutex_unlock(&lgr->rmbs_lock);
1177 		mutex_lock(&lgr->sndbufs_lock);
1178 		list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i],
1179 					 list)
1180 			smcr_buf_unmap_link(buf_desc, false, lnk);
1181 		mutex_unlock(&lgr->sndbufs_lock);
1182 	}
1183 }
1184 
1185 static void smcr_rtoken_clear_link(struct smc_link *lnk)
1186 {
1187 	struct smc_link_group *lgr = lnk->lgr;
1188 	int i;
1189 
1190 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1191 		lgr->rtokens[i][lnk->link_idx].rkey = 0;
1192 		lgr->rtokens[i][lnk->link_idx].dma_addr = 0;
1193 	}
1194 }
1195 
1196 /* must be called under lgr->llc_conf_mutex lock */
1197 void smcr_link_clear(struct smc_link *lnk, bool log)
1198 {
1199 	struct smc_ib_device *smcibdev;
1200 
1201 	if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED)
1202 		return;
1203 	lnk->peer_qpn = 0;
1204 	smc_llc_link_clear(lnk, log);
1205 	smcr_buf_unmap_lgr(lnk);
1206 	smcr_rtoken_clear_link(lnk);
1207 	smc_ib_modify_qp_reset(lnk);
1208 	smc_wr_free_link(lnk);
1209 	smc_ib_destroy_queue_pair(lnk);
1210 	smc_ib_dealloc_protection_domain(lnk);
1211 	smc_wr_free_link_mem(lnk);
1212 	smc_ibdev_cnt_dec(lnk);
1213 	put_device(&lnk->smcibdev->ibdev->dev);
1214 	smcibdev = lnk->smcibdev;
1215 	memset(lnk, 0, sizeof(struct smc_link));
1216 	lnk->state = SMC_LNK_UNUSED;
1217 	if (!atomic_dec_return(&smcibdev->lnk_cnt))
1218 		wake_up(&smcibdev->lnks_deleted);
1219 }
1220 
1221 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
1222 			  struct smc_buf_desc *buf_desc)
1223 {
1224 	int i;
1225 
1226 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1227 		smcr_buf_unmap_link(buf_desc, is_rmb, &lgr->lnk[i]);
1228 
1229 	if (buf_desc->pages)
1230 		__free_pages(buf_desc->pages, buf_desc->order);
1231 	kfree(buf_desc);
1232 }
1233 
1234 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
1235 			  struct smc_buf_desc *buf_desc)
1236 {
1237 	if (is_dmb) {
1238 		/* restore original buf len */
1239 		buf_desc->len += sizeof(struct smcd_cdc_msg);
1240 		smc_ism_unregister_dmb(lgr->smcd, buf_desc);
1241 	} else {
1242 		kfree(buf_desc->cpu_addr);
1243 	}
1244 	kfree(buf_desc);
1245 }
1246 
1247 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
1248 			 struct smc_buf_desc *buf_desc)
1249 {
1250 	if (lgr->is_smcd)
1251 		smcd_buf_free(lgr, is_rmb, buf_desc);
1252 	else
1253 		smcr_buf_free(lgr, is_rmb, buf_desc);
1254 }
1255 
1256 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
1257 {
1258 	struct smc_buf_desc *buf_desc, *bf_desc;
1259 	struct list_head *buf_list;
1260 	int i;
1261 
1262 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
1263 		if (is_rmb)
1264 			buf_list = &lgr->rmbs[i];
1265 		else
1266 			buf_list = &lgr->sndbufs[i];
1267 		list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
1268 					 list) {
1269 			list_del(&buf_desc->list);
1270 			smc_buf_free(lgr, is_rmb, buf_desc);
1271 		}
1272 	}
1273 }
1274 
1275 static void smc_lgr_free_bufs(struct smc_link_group *lgr)
1276 {
1277 	/* free send buffers */
1278 	__smc_lgr_free_bufs(lgr, false);
1279 	/* free rmbs */
1280 	__smc_lgr_free_bufs(lgr, true);
1281 }
1282 
1283 /* remove a link group */
1284 static void smc_lgr_free(struct smc_link_group *lgr)
1285 {
1286 	int i;
1287 
1288 	if (!lgr->is_smcd) {
1289 		mutex_lock(&lgr->llc_conf_mutex);
1290 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1291 			if (lgr->lnk[i].state != SMC_LNK_UNUSED)
1292 				smcr_link_clear(&lgr->lnk[i], false);
1293 		}
1294 		mutex_unlock(&lgr->llc_conf_mutex);
1295 		smc_llc_lgr_clear(lgr);
1296 	}
1297 
1298 	smc_lgr_free_bufs(lgr);
1299 	destroy_workqueue(lgr->tx_wq);
1300 	if (lgr->is_smcd) {
1301 		smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
1302 		put_device(&lgr->smcd->dev);
1303 		if (!atomic_dec_return(&lgr->smcd->lgr_cnt))
1304 			wake_up(&lgr->smcd->lgrs_deleted);
1305 	} else {
1306 		smc_wr_free_lgr_mem(lgr);
1307 		if (!atomic_dec_return(&lgr_cnt))
1308 			wake_up(&lgrs_deleted);
1309 	}
1310 	kfree(lgr);
1311 }
1312 
1313 static void smc_sk_wake_ups(struct smc_sock *smc)
1314 {
1315 	smc->sk.sk_write_space(&smc->sk);
1316 	smc->sk.sk_data_ready(&smc->sk);
1317 	smc->sk.sk_state_change(&smc->sk);
1318 }
1319 
1320 /* kill a connection */
1321 static void smc_conn_kill(struct smc_connection *conn, bool soft)
1322 {
1323 	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1324 
1325 	if (conn->lgr->is_smcd && conn->lgr->peer_shutdown)
1326 		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
1327 	else
1328 		smc_close_abort(conn);
1329 	conn->killed = 1;
1330 	smc->sk.sk_err = ECONNABORTED;
1331 	smc_sk_wake_ups(smc);
1332 	if (conn->lgr->is_smcd) {
1333 		smc_ism_unset_conn(conn);
1334 		if (soft)
1335 			tasklet_kill(&conn->rx_tsklet);
1336 		else
1337 			tasklet_unlock_wait(&conn->rx_tsklet);
1338 	} else {
1339 		smc_cdc_tx_dismiss_slots(conn);
1340 	}
1341 	smc_lgr_unregister_conn(conn);
1342 	smc_close_active_abort(smc);
1343 }
1344 
1345 static void smc_lgr_cleanup(struct smc_link_group *lgr)
1346 {
1347 	if (lgr->is_smcd) {
1348 		smc_ism_signal_shutdown(lgr);
1349 	} else {
1350 		u32 rsn = lgr->llc_termination_rsn;
1351 
1352 		if (!rsn)
1353 			rsn = SMC_LLC_DEL_PROG_INIT_TERM;
1354 		smc_llc_send_link_delete_all(lgr, false, rsn);
1355 		smcr_lgr_link_deactivate_all(lgr);
1356 	}
1357 }
1358 
1359 /* terminate link group
1360  * @soft: true if link group shutdown can take its time
1361  *	  false if immediate link group shutdown is required
1362  */
1363 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
1364 {
1365 	struct smc_connection *conn;
1366 	struct smc_sock *smc;
1367 	struct rb_node *node;
1368 
1369 	if (lgr->terminating)
1370 		return;	/* lgr already terminating */
1371 	/* cancel free_work sync, will terminate when lgr->freeing is set */
1372 	cancel_delayed_work_sync(&lgr->free_work);
1373 	lgr->terminating = 1;
1374 
1375 	/* kill remaining link group connections */
1376 	read_lock_bh(&lgr->conns_lock);
1377 	node = rb_first(&lgr->conns_all);
1378 	while (node) {
1379 		read_unlock_bh(&lgr->conns_lock);
1380 		conn = rb_entry(node, struct smc_connection, alert_node);
1381 		smc = container_of(conn, struct smc_sock, conn);
1382 		sock_hold(&smc->sk); /* sock_put below */
1383 		lock_sock(&smc->sk);
1384 		smc_conn_kill(conn, soft);
1385 		release_sock(&smc->sk);
1386 		sock_put(&smc->sk); /* sock_hold above */
1387 		read_lock_bh(&lgr->conns_lock);
1388 		node = rb_first(&lgr->conns_all);
1389 	}
1390 	read_unlock_bh(&lgr->conns_lock);
1391 	smc_lgr_cleanup(lgr);
1392 	smc_lgr_free(lgr);
1393 }
1394 
1395 /* unlink link group and schedule termination */
1396 void smc_lgr_terminate_sched(struct smc_link_group *lgr)
1397 {
1398 	spinlock_t *lgr_lock;
1399 
1400 	smc_lgr_list_head(lgr, &lgr_lock);
1401 	spin_lock_bh(lgr_lock);
1402 	if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) {
1403 		spin_unlock_bh(lgr_lock);
1404 		return;	/* lgr already terminating */
1405 	}
1406 	list_del_init(&lgr->list);
1407 	lgr->freeing = 1;
1408 	spin_unlock_bh(lgr_lock);
1409 	schedule_work(&lgr->terminate_work);
1410 }
1411 
1412 /* Called when peer lgr shutdown (regularly or abnormally) is received */
1413 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan)
1414 {
1415 	struct smc_link_group *lgr, *l;
1416 	LIST_HEAD(lgr_free_list);
1417 
1418 	/* run common cleanup function and build free list */
1419 	spin_lock_bh(&dev->lgr_lock);
1420 	list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) {
1421 		if ((!peer_gid || lgr->peer_gid == peer_gid) &&
1422 		    (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) {
1423 			if (peer_gid) /* peer triggered termination */
1424 				lgr->peer_shutdown = 1;
1425 			list_move(&lgr->list, &lgr_free_list);
1426 			lgr->freeing = 1;
1427 		}
1428 	}
1429 	spin_unlock_bh(&dev->lgr_lock);
1430 
1431 	/* cancel the regular free workers and actually free lgrs */
1432 	list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
1433 		list_del_init(&lgr->list);
1434 		schedule_work(&lgr->terminate_work);
1435 	}
1436 }
1437 
1438 /* Called when an SMCD device is removed or the smc module is unloaded */
1439 void smc_smcd_terminate_all(struct smcd_dev *smcd)
1440 {
1441 	struct smc_link_group *lgr, *lg;
1442 	LIST_HEAD(lgr_free_list);
1443 
1444 	spin_lock_bh(&smcd->lgr_lock);
1445 	list_splice_init(&smcd->lgr_list, &lgr_free_list);
1446 	list_for_each_entry(lgr, &lgr_free_list, list)
1447 		lgr->freeing = 1;
1448 	spin_unlock_bh(&smcd->lgr_lock);
1449 
1450 	list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1451 		list_del_init(&lgr->list);
1452 		__smc_lgr_terminate(lgr, false);
1453 	}
1454 
1455 	if (atomic_read(&smcd->lgr_cnt))
1456 		wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt));
1457 }
1458 
1459 /* Called when an SMCR device is removed or the smc module is unloaded.
1460  * If smcibdev is given, all SMCR link groups using this device are terminated.
1461  * If smcibdev is NULL, all SMCR link groups are terminated.
1462  */
1463 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
1464 {
1465 	struct smc_link_group *lgr, *lg;
1466 	LIST_HEAD(lgr_free_list);
1467 	int i;
1468 
1469 	spin_lock_bh(&smc_lgr_list.lock);
1470 	if (!smcibdev) {
1471 		list_splice_init(&smc_lgr_list.list, &lgr_free_list);
1472 		list_for_each_entry(lgr, &lgr_free_list, list)
1473 			lgr->freeing = 1;
1474 	} else {
1475 		list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
1476 			for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1477 				if (lgr->lnk[i].smcibdev == smcibdev)
1478 					smcr_link_down_cond_sched(&lgr->lnk[i]);
1479 			}
1480 		}
1481 	}
1482 	spin_unlock_bh(&smc_lgr_list.lock);
1483 
1484 	list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1485 		list_del_init(&lgr->list);
1486 		smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM);
1487 		__smc_lgr_terminate(lgr, false);
1488 	}
1489 
1490 	if (smcibdev) {
1491 		if (atomic_read(&smcibdev->lnk_cnt))
1492 			wait_event(smcibdev->lnks_deleted,
1493 				   !atomic_read(&smcibdev->lnk_cnt));
1494 	} else {
1495 		if (atomic_read(&lgr_cnt))
1496 			wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
1497 	}
1498 }
1499 
1500 /* set new lgr type and clear all asymmetric link tagging */
1501 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type)
1502 {
1503 	char *lgr_type = "";
1504 	int i;
1505 
1506 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1507 		if (smc_link_usable(&lgr->lnk[i]))
1508 			lgr->lnk[i].link_is_asym = false;
1509 	if (lgr->type == new_type)
1510 		return;
1511 	lgr->type = new_type;
1512 
1513 	switch (lgr->type) {
1514 	case SMC_LGR_NONE:
1515 		lgr_type = "NONE";
1516 		break;
1517 	case SMC_LGR_SINGLE:
1518 		lgr_type = "SINGLE";
1519 		break;
1520 	case SMC_LGR_SYMMETRIC:
1521 		lgr_type = "SYMMETRIC";
1522 		break;
1523 	case SMC_LGR_ASYMMETRIC_PEER:
1524 		lgr_type = "ASYMMETRIC_PEER";
1525 		break;
1526 	case SMC_LGR_ASYMMETRIC_LOCAL:
1527 		lgr_type = "ASYMMETRIC_LOCAL";
1528 		break;
1529 	}
1530 	pr_warn_ratelimited("smc: SMC-R lg %*phN state changed: "
1531 			    "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id,
1532 			    lgr_type, lgr->pnet_id);
1533 }
1534 
1535 /* set new lgr type and tag a link as asymmetric */
1536 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
1537 			    enum smc_lgr_type new_type, int asym_lnk_idx)
1538 {
1539 	smcr_lgr_set_type(lgr, new_type);
1540 	lgr->lnk[asym_lnk_idx].link_is_asym = true;
1541 }
1542 
1543 /* abort connection, abort_work scheduled from tasklet context */
1544 static void smc_conn_abort_work(struct work_struct *work)
1545 {
1546 	struct smc_connection *conn = container_of(work,
1547 						   struct smc_connection,
1548 						   abort_work);
1549 	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1550 
1551 	lock_sock(&smc->sk);
1552 	smc_conn_kill(conn, true);
1553 	release_sock(&smc->sk);
1554 	sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
1555 }
1556 
1557 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport)
1558 {
1559 	struct smc_link_group *lgr, *n;
1560 
1561 	list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1562 		struct smc_link *link;
1563 
1564 		if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1565 			    SMC_MAX_PNETID_LEN) ||
1566 		    lgr->type == SMC_LGR_SYMMETRIC ||
1567 		    lgr->type == SMC_LGR_ASYMMETRIC_PEER)
1568 			continue;
1569 
1570 		/* trigger local add link processing */
1571 		link = smc_llc_usable_link(lgr);
1572 		if (link)
1573 			smc_llc_add_link_local(link);
1574 	}
1575 }
1576 
1577 /* link is down - switch connections to alternate link,
1578  * must be called under lgr->llc_conf_mutex lock
1579  */
1580 static void smcr_link_down(struct smc_link *lnk)
1581 {
1582 	struct smc_link_group *lgr = lnk->lgr;
1583 	struct smc_link *to_lnk;
1584 	int del_link_id;
1585 
1586 	if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list))
1587 		return;
1588 
1589 	smc_ib_modify_qp_reset(lnk);
1590 	to_lnk = smc_switch_conns(lgr, lnk, true);
1591 	if (!to_lnk) { /* no backup link available */
1592 		smcr_link_clear(lnk, true);
1593 		return;
1594 	}
1595 	smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1596 	del_link_id = lnk->link_id;
1597 
1598 	if (lgr->role == SMC_SERV) {
1599 		/* trigger local delete link processing */
1600 		smc_llc_srv_delete_link_local(to_lnk, del_link_id);
1601 	} else {
1602 		if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1603 			/* another llc task is ongoing */
1604 			mutex_unlock(&lgr->llc_conf_mutex);
1605 			wait_event_timeout(lgr->llc_flow_waiter,
1606 				(list_empty(&lgr->list) ||
1607 				 lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE),
1608 				SMC_LLC_WAIT_TIME);
1609 			mutex_lock(&lgr->llc_conf_mutex);
1610 		}
1611 		if (!list_empty(&lgr->list)) {
1612 			smc_llc_send_delete_link(to_lnk, del_link_id,
1613 						 SMC_LLC_REQ, true,
1614 						 SMC_LLC_DEL_LOST_PATH);
1615 			smcr_link_clear(lnk, true);
1616 		}
1617 		wake_up(&lgr->llc_flow_waiter);	/* wake up next waiter */
1618 	}
1619 }
1620 
1621 /* must be called under lgr->llc_conf_mutex lock */
1622 void smcr_link_down_cond(struct smc_link *lnk)
1623 {
1624 	if (smc_link_downing(&lnk->state)) {
1625 		trace_smcr_link_down(lnk, __builtin_return_address(0));
1626 		smcr_link_down(lnk);
1627 	}
1628 }
1629 
1630 /* will get the lgr->llc_conf_mutex lock */
1631 void smcr_link_down_cond_sched(struct smc_link *lnk)
1632 {
1633 	if (smc_link_downing(&lnk->state)) {
1634 		trace_smcr_link_down(lnk, __builtin_return_address(0));
1635 		schedule_work(&lnk->link_down_wrk);
1636 	}
1637 }
1638 
1639 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport)
1640 {
1641 	struct smc_link_group *lgr, *n;
1642 	int i;
1643 
1644 	list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1645 		if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1646 			    SMC_MAX_PNETID_LEN))
1647 			continue; /* lgr is not affected */
1648 		if (list_empty(&lgr->list))
1649 			continue;
1650 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1651 			struct smc_link *lnk = &lgr->lnk[i];
1652 
1653 			if (smc_link_usable(lnk) &&
1654 			    lnk->smcibdev == smcibdev && lnk->ibport == ibport)
1655 				smcr_link_down_cond_sched(lnk);
1656 		}
1657 	}
1658 }
1659 
1660 static void smc_link_down_work(struct work_struct *work)
1661 {
1662 	struct smc_link *link = container_of(work, struct smc_link,
1663 					     link_down_wrk);
1664 	struct smc_link_group *lgr = link->lgr;
1665 
1666 	if (list_empty(&lgr->list))
1667 		return;
1668 	wake_up_all(&lgr->llc_msg_waiter);
1669 	mutex_lock(&lgr->llc_conf_mutex);
1670 	smcr_link_down(link);
1671 	mutex_unlock(&lgr->llc_conf_mutex);
1672 }
1673 
1674 /* Determine vlan of internal TCP socket.
1675  * @vlan_id: address to store the determined vlan id into
1676  */
1677 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
1678 {
1679 	struct dst_entry *dst = sk_dst_get(clcsock->sk);
1680 	struct net_device *ndev;
1681 	int i, nest_lvl, rc = 0;
1682 
1683 	ini->vlan_id = 0;
1684 	if (!dst) {
1685 		rc = -ENOTCONN;
1686 		goto out;
1687 	}
1688 	if (!dst->dev) {
1689 		rc = -ENODEV;
1690 		goto out_rel;
1691 	}
1692 
1693 	ndev = dst->dev;
1694 	if (is_vlan_dev(ndev)) {
1695 		ini->vlan_id = vlan_dev_vlan_id(ndev);
1696 		goto out_rel;
1697 	}
1698 
1699 	rtnl_lock();
1700 	nest_lvl = ndev->lower_level;
1701 	for (i = 0; i < nest_lvl; i++) {
1702 		struct list_head *lower = &ndev->adj_list.lower;
1703 
1704 		if (list_empty(lower))
1705 			break;
1706 		lower = lower->next;
1707 		ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
1708 		if (is_vlan_dev(ndev)) {
1709 			ini->vlan_id = vlan_dev_vlan_id(ndev);
1710 			break;
1711 		}
1712 	}
1713 	rtnl_unlock();
1714 
1715 out_rel:
1716 	dst_release(dst);
1717 out:
1718 	return rc;
1719 }
1720 
1721 static bool smcr_lgr_match(struct smc_link_group *lgr, u8 smcr_version,
1722 			   u8 peer_systemid[],
1723 			   u8 peer_gid[],
1724 			   u8 peer_mac_v1[],
1725 			   enum smc_lgr_role role, u32 clcqpn)
1726 {
1727 	int i;
1728 
1729 	if (memcmp(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN) ||
1730 	    lgr->role != role)
1731 		return false;
1732 
1733 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1734 		if (!smc_link_active(&lgr->lnk[i]))
1735 			continue;
1736 		if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) &&
1737 		    !memcmp(lgr->lnk[i].peer_gid, peer_gid, SMC_GID_SIZE) &&
1738 		    (smcr_version == SMC_V2 ||
1739 		     !memcmp(lgr->lnk[i].peer_mac, peer_mac_v1, ETH_ALEN)))
1740 			return true;
1741 	}
1742 	return false;
1743 }
1744 
1745 static bool smcd_lgr_match(struct smc_link_group *lgr,
1746 			   struct smcd_dev *smcismdev, u64 peer_gid)
1747 {
1748 	return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
1749 }
1750 
1751 /* create a new SMC connection (and a new link group if necessary) */
1752 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
1753 {
1754 	struct smc_connection *conn = &smc->conn;
1755 	struct list_head *lgr_list;
1756 	struct smc_link_group *lgr;
1757 	enum smc_lgr_role role;
1758 	spinlock_t *lgr_lock;
1759 	int rc = 0;
1760 
1761 	lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list :
1762 				  &smc_lgr_list.list;
1763 	lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock :
1764 				  &smc_lgr_list.lock;
1765 	ini->first_contact_local = 1;
1766 	role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
1767 	if (role == SMC_CLNT && ini->first_contact_peer)
1768 		/* create new link group as well */
1769 		goto create;
1770 
1771 	/* determine if an existing link group can be reused */
1772 	spin_lock_bh(lgr_lock);
1773 	list_for_each_entry(lgr, lgr_list, list) {
1774 		write_lock_bh(&lgr->conns_lock);
1775 		if ((ini->is_smcd ?
1776 		     smcd_lgr_match(lgr, ini->ism_dev[ini->ism_selected],
1777 				    ini->ism_peer_gid[ini->ism_selected]) :
1778 		     smcr_lgr_match(lgr, ini->smcr_version,
1779 				    ini->peer_systemid,
1780 				    ini->peer_gid, ini->peer_mac, role,
1781 				    ini->ib_clcqpn)) &&
1782 		    !lgr->sync_err &&
1783 		    (ini->smcd_version == SMC_V2 ||
1784 		     lgr->vlan_id == ini->vlan_id) &&
1785 		    (role == SMC_CLNT || ini->is_smcd ||
1786 		     lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
1787 			/* link group found */
1788 			ini->first_contact_local = 0;
1789 			conn->lgr = lgr;
1790 			rc = smc_lgr_register_conn(conn, false);
1791 			write_unlock_bh(&lgr->conns_lock);
1792 			if (!rc && delayed_work_pending(&lgr->free_work))
1793 				cancel_delayed_work(&lgr->free_work);
1794 			break;
1795 		}
1796 		write_unlock_bh(&lgr->conns_lock);
1797 	}
1798 	spin_unlock_bh(lgr_lock);
1799 	if (rc)
1800 		return rc;
1801 
1802 	if (role == SMC_CLNT && !ini->first_contact_peer &&
1803 	    ini->first_contact_local) {
1804 		/* Server reuses a link group, but Client wants to start
1805 		 * a new one
1806 		 * send out_of_sync decline, reason synchr. error
1807 		 */
1808 		return SMC_CLC_DECL_SYNCERR;
1809 	}
1810 
1811 create:
1812 	if (ini->first_contact_local) {
1813 		rc = smc_lgr_create(smc, ini);
1814 		if (rc)
1815 			goto out;
1816 		lgr = conn->lgr;
1817 		write_lock_bh(&lgr->conns_lock);
1818 		rc = smc_lgr_register_conn(conn, true);
1819 		write_unlock_bh(&lgr->conns_lock);
1820 		if (rc)
1821 			goto out;
1822 	}
1823 	conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1824 	conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
1825 	conn->urg_state = SMC_URG_READ;
1826 	INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work);
1827 	if (ini->is_smcd) {
1828 		conn->rx_off = sizeof(struct smcd_cdc_msg);
1829 		smcd_cdc_rx_init(conn); /* init tasklet for this conn */
1830 	} else {
1831 		conn->rx_off = 0;
1832 	}
1833 #ifndef KERNEL_HAS_ATOMIC64
1834 	spin_lock_init(&conn->acurs_lock);
1835 #endif
1836 
1837 out:
1838 	return rc;
1839 }
1840 
1841 #define SMCD_DMBE_SIZES		6 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1842 #define SMCR_RMBE_SIZES		5 /* 0 -> 16KB, 1 -> 32KB, .. 5 -> 512KB */
1843 
1844 /* convert the RMB size into the compressed notation (minimum 16K, see
1845  * SMCD/R_DMBE_SIZES.
1846  * In contrast to plain ilog2, this rounds towards the next power of 2,
1847  * so the socket application gets at least its desired sndbuf / rcvbuf size.
1848  */
1849 static u8 smc_compress_bufsize(int size, bool is_smcd, bool is_rmb)
1850 {
1851 	const unsigned int max_scat = SG_MAX_SINGLE_ALLOC * PAGE_SIZE;
1852 	u8 compressed;
1853 
1854 	if (size <= SMC_BUF_MIN_SIZE)
1855 		return 0;
1856 
1857 	size = (size - 1) >> 14;  /* convert to 16K multiple */
1858 	compressed = min_t(u8, ilog2(size) + 1,
1859 			   is_smcd ? SMCD_DMBE_SIZES : SMCR_RMBE_SIZES);
1860 
1861 	if (!is_smcd && is_rmb)
1862 		/* RMBs are backed by & limited to max size of scatterlists */
1863 		compressed = min_t(u8, compressed, ilog2(max_scat >> 14));
1864 
1865 	return compressed;
1866 }
1867 
1868 /* convert the RMB size from compressed notation into integer */
1869 int smc_uncompress_bufsize(u8 compressed)
1870 {
1871 	u32 size;
1872 
1873 	size = 0x00000001 << (((int)compressed) + 14);
1874 	return (int)size;
1875 }
1876 
1877 /* try to reuse a sndbuf or rmb description slot for a certain
1878  * buffer size; if not available, return NULL
1879  */
1880 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
1881 					     struct mutex *lock,
1882 					     struct list_head *buf_list)
1883 {
1884 	struct smc_buf_desc *buf_slot;
1885 
1886 	mutex_lock(lock);
1887 	list_for_each_entry(buf_slot, buf_list, list) {
1888 		if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
1889 			mutex_unlock(lock);
1890 			return buf_slot;
1891 		}
1892 	}
1893 	mutex_unlock(lock);
1894 	return NULL;
1895 }
1896 
1897 /* one of the conditions for announcing a receiver's current window size is
1898  * that it "results in a minimum increase in the window size of 10% of the
1899  * receive buffer space" [RFC7609]
1900  */
1901 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
1902 {
1903 	return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
1904 }
1905 
1906 /* map an rmb buf to a link */
1907 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1908 			     struct smc_link *lnk)
1909 {
1910 	int rc;
1911 
1912 	if (buf_desc->is_map_ib[lnk->link_idx])
1913 		return 0;
1914 
1915 	rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL);
1916 	if (rc)
1917 		return rc;
1918 	sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
1919 		   buf_desc->cpu_addr, buf_desc->len);
1920 
1921 	/* map sg table to DMA address */
1922 	rc = smc_ib_buf_map_sg(lnk, buf_desc,
1923 			       is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1924 	/* SMC protocol depends on mapping to one DMA address only */
1925 	if (rc != 1) {
1926 		rc = -EAGAIN;
1927 		goto free_table;
1928 	}
1929 
1930 	/* create a new memory region for the RMB */
1931 	if (is_rmb) {
1932 		rc = smc_ib_get_memory_region(lnk->roce_pd,
1933 					      IB_ACCESS_REMOTE_WRITE |
1934 					      IB_ACCESS_LOCAL_WRITE,
1935 					      buf_desc, lnk->link_idx);
1936 		if (rc)
1937 			goto buf_unmap;
1938 		smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE);
1939 	}
1940 	buf_desc->is_map_ib[lnk->link_idx] = true;
1941 	return 0;
1942 
1943 buf_unmap:
1944 	smc_ib_buf_unmap_sg(lnk, buf_desc,
1945 			    is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1946 free_table:
1947 	sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1948 	return rc;
1949 }
1950 
1951 /* register a new rmb on IB device,
1952  * must be called under lgr->llc_conf_mutex lock
1953  */
1954 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
1955 {
1956 	if (list_empty(&link->lgr->list))
1957 		return -ENOLINK;
1958 	if (!rmb_desc->is_reg_mr[link->link_idx]) {
1959 		/* register memory region for new rmb */
1960 		if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
1961 			rmb_desc->is_reg_err = true;
1962 			return -EFAULT;
1963 		}
1964 		rmb_desc->is_reg_mr[link->link_idx] = true;
1965 	}
1966 	return 0;
1967 }
1968 
1969 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct mutex *lock,
1970 			     struct list_head *lst, bool is_rmb)
1971 {
1972 	struct smc_buf_desc *buf_desc, *bf;
1973 	int rc = 0;
1974 
1975 	mutex_lock(lock);
1976 	list_for_each_entry_safe(buf_desc, bf, lst, list) {
1977 		if (!buf_desc->used)
1978 			continue;
1979 		rc = smcr_buf_map_link(buf_desc, is_rmb, lnk);
1980 		if (rc)
1981 			goto out;
1982 	}
1983 out:
1984 	mutex_unlock(lock);
1985 	return rc;
1986 }
1987 
1988 /* map all used buffers of lgr for a new link */
1989 int smcr_buf_map_lgr(struct smc_link *lnk)
1990 {
1991 	struct smc_link_group *lgr = lnk->lgr;
1992 	int i, rc = 0;
1993 
1994 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
1995 		rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock,
1996 				       &lgr->rmbs[i], true);
1997 		if (rc)
1998 			return rc;
1999 		rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock,
2000 				       &lgr->sndbufs[i], false);
2001 		if (rc)
2002 			return rc;
2003 	}
2004 	return 0;
2005 }
2006 
2007 /* register all used buffers of lgr for a new link,
2008  * must be called under lgr->llc_conf_mutex lock
2009  */
2010 int smcr_buf_reg_lgr(struct smc_link *lnk)
2011 {
2012 	struct smc_link_group *lgr = lnk->lgr;
2013 	struct smc_buf_desc *buf_desc, *bf;
2014 	int i, rc = 0;
2015 
2016 	mutex_lock(&lgr->rmbs_lock);
2017 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
2018 		list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) {
2019 			if (!buf_desc->used)
2020 				continue;
2021 			rc = smcr_link_reg_rmb(lnk, buf_desc);
2022 			if (rc)
2023 				goto out;
2024 		}
2025 	}
2026 out:
2027 	mutex_unlock(&lgr->rmbs_lock);
2028 	return rc;
2029 }
2030 
2031 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
2032 						bool is_rmb, int bufsize)
2033 {
2034 	struct smc_buf_desc *buf_desc;
2035 
2036 	/* try to alloc a new buffer */
2037 	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
2038 	if (!buf_desc)
2039 		return ERR_PTR(-ENOMEM);
2040 
2041 	buf_desc->order = get_order(bufsize);
2042 	buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
2043 				      __GFP_NOMEMALLOC | __GFP_COMP |
2044 				      __GFP_NORETRY | __GFP_ZERO,
2045 				      buf_desc->order);
2046 	if (!buf_desc->pages) {
2047 		kfree(buf_desc);
2048 		return ERR_PTR(-EAGAIN);
2049 	}
2050 	buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
2051 	buf_desc->len = bufsize;
2052 	return buf_desc;
2053 }
2054 
2055 /* map buf_desc on all usable links,
2056  * unused buffers stay mapped as long as the link is up
2057  */
2058 static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
2059 				     struct smc_buf_desc *buf_desc, bool is_rmb)
2060 {
2061 	int i, rc = 0;
2062 
2063 	/* protect against parallel link reconfiguration */
2064 	mutex_lock(&lgr->llc_conf_mutex);
2065 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2066 		struct smc_link *lnk = &lgr->lnk[i];
2067 
2068 		if (!smc_link_usable(lnk))
2069 			continue;
2070 		if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
2071 			rc = -ENOMEM;
2072 			goto out;
2073 		}
2074 	}
2075 out:
2076 	mutex_unlock(&lgr->llc_conf_mutex);
2077 	return rc;
2078 }
2079 
2080 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
2081 						bool is_dmb, int bufsize)
2082 {
2083 	struct smc_buf_desc *buf_desc;
2084 	int rc;
2085 
2086 	/* try to alloc a new DMB */
2087 	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
2088 	if (!buf_desc)
2089 		return ERR_PTR(-ENOMEM);
2090 	if (is_dmb) {
2091 		rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
2092 		if (rc) {
2093 			kfree(buf_desc);
2094 			if (rc == -ENOMEM)
2095 				return ERR_PTR(-EAGAIN);
2096 			if (rc == -ENOSPC)
2097 				return ERR_PTR(-ENOSPC);
2098 			return ERR_PTR(-EIO);
2099 		}
2100 		buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
2101 		/* CDC header stored in buf. So, pretend it was smaller */
2102 		buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
2103 	} else {
2104 		buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
2105 					     __GFP_NOWARN | __GFP_NORETRY |
2106 					     __GFP_NOMEMALLOC);
2107 		if (!buf_desc->cpu_addr) {
2108 			kfree(buf_desc);
2109 			return ERR_PTR(-EAGAIN);
2110 		}
2111 		buf_desc->len = bufsize;
2112 	}
2113 	return buf_desc;
2114 }
2115 
2116 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
2117 {
2118 	struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
2119 	struct smc_connection *conn = &smc->conn;
2120 	struct smc_link_group *lgr = conn->lgr;
2121 	struct list_head *buf_list;
2122 	int bufsize, bufsize_short;
2123 	bool is_dgraded = false;
2124 	struct mutex *lock;	/* lock buffer list */
2125 	int sk_buf_size;
2126 
2127 	if (is_rmb)
2128 		/* use socket recv buffer size (w/o overhead) as start value */
2129 		sk_buf_size = smc->sk.sk_rcvbuf / 2;
2130 	else
2131 		/* use socket send buffer size (w/o overhead) as start value */
2132 		sk_buf_size = smc->sk.sk_sndbuf / 2;
2133 
2134 	for (bufsize_short = smc_compress_bufsize(sk_buf_size, is_smcd, is_rmb);
2135 	     bufsize_short >= 0; bufsize_short--) {
2136 		if (is_rmb) {
2137 			lock = &lgr->rmbs_lock;
2138 			buf_list = &lgr->rmbs[bufsize_short];
2139 		} else {
2140 			lock = &lgr->sndbufs_lock;
2141 			buf_list = &lgr->sndbufs[bufsize_short];
2142 		}
2143 		bufsize = smc_uncompress_bufsize(bufsize_short);
2144 
2145 		/* check for reusable slot in the link group */
2146 		buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
2147 		if (buf_desc) {
2148 			SMC_STAT_RMB_SIZE(smc, is_smcd, is_rmb, bufsize);
2149 			SMC_STAT_BUF_REUSE(smc, is_smcd, is_rmb);
2150 			memset(buf_desc->cpu_addr, 0, bufsize);
2151 			break; /* found reusable slot */
2152 		}
2153 
2154 		if (is_smcd)
2155 			buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
2156 		else
2157 			buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
2158 
2159 		if (PTR_ERR(buf_desc) == -ENOMEM)
2160 			break;
2161 		if (IS_ERR(buf_desc)) {
2162 			if (!is_dgraded) {
2163 				is_dgraded = true;
2164 				SMC_STAT_RMB_DOWNGRADED(smc, is_smcd, is_rmb);
2165 			}
2166 			continue;
2167 		}
2168 
2169 		SMC_STAT_RMB_ALLOC(smc, is_smcd, is_rmb);
2170 		SMC_STAT_RMB_SIZE(smc, is_smcd, is_rmb, bufsize);
2171 		buf_desc->used = 1;
2172 		mutex_lock(lock);
2173 		list_add(&buf_desc->list, buf_list);
2174 		mutex_unlock(lock);
2175 		break; /* found */
2176 	}
2177 
2178 	if (IS_ERR(buf_desc))
2179 		return PTR_ERR(buf_desc);
2180 
2181 	if (!is_smcd) {
2182 		if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
2183 			smcr_buf_unuse(buf_desc, lgr);
2184 			return -ENOMEM;
2185 		}
2186 	}
2187 
2188 	if (is_rmb) {
2189 		conn->rmb_desc = buf_desc;
2190 		conn->rmbe_size_short = bufsize_short;
2191 		smc->sk.sk_rcvbuf = bufsize * 2;
2192 		atomic_set(&conn->bytes_to_rcv, 0);
2193 		conn->rmbe_update_limit =
2194 			smc_rmb_wnd_update_limit(buf_desc->len);
2195 		if (is_smcd)
2196 			smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
2197 	} else {
2198 		conn->sndbuf_desc = buf_desc;
2199 		smc->sk.sk_sndbuf = bufsize * 2;
2200 		atomic_set(&conn->sndbuf_space, bufsize);
2201 	}
2202 	return 0;
2203 }
2204 
2205 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
2206 {
2207 	if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
2208 		return;
2209 	smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
2210 }
2211 
2212 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
2213 {
2214 	if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
2215 		return;
2216 	smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
2217 }
2218 
2219 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
2220 {
2221 	int i;
2222 
2223 	if (!conn->lgr || conn->lgr->is_smcd)
2224 		return;
2225 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2226 		if (!smc_link_active(&conn->lgr->lnk[i]))
2227 			continue;
2228 		smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
2229 				       DMA_FROM_DEVICE);
2230 	}
2231 }
2232 
2233 void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
2234 {
2235 	int i;
2236 
2237 	if (!conn->lgr || conn->lgr->is_smcd)
2238 		return;
2239 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2240 		if (!smc_link_active(&conn->lgr->lnk[i]))
2241 			continue;
2242 		smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc,
2243 					  DMA_FROM_DEVICE);
2244 	}
2245 }
2246 
2247 /* create the send and receive buffer for an SMC socket;
2248  * receive buffers are called RMBs;
2249  * (even though the SMC protocol allows more than one RMB-element per RMB,
2250  * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
2251  * extra RMB for every connection in a link group
2252  */
2253 int smc_buf_create(struct smc_sock *smc, bool is_smcd)
2254 {
2255 	int rc;
2256 
2257 	/* create send buffer */
2258 	rc = __smc_buf_create(smc, is_smcd, false);
2259 	if (rc)
2260 		return rc;
2261 	/* create rmb */
2262 	rc = __smc_buf_create(smc, is_smcd, true);
2263 	if (rc) {
2264 		mutex_lock(&smc->conn.lgr->sndbufs_lock);
2265 		list_del(&smc->conn.sndbuf_desc->list);
2266 		mutex_unlock(&smc->conn.lgr->sndbufs_lock);
2267 		smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
2268 		smc->conn.sndbuf_desc = NULL;
2269 	}
2270 	return rc;
2271 }
2272 
2273 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
2274 {
2275 	int i;
2276 
2277 	for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
2278 		if (!test_and_set_bit(i, lgr->rtokens_used_mask))
2279 			return i;
2280 	}
2281 	return -ENOSPC;
2282 }
2283 
2284 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx,
2285 				   u32 rkey)
2286 {
2287 	int i;
2288 
2289 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2290 		if (test_bit(i, lgr->rtokens_used_mask) &&
2291 		    lgr->rtokens[i][lnk_idx].rkey == rkey)
2292 			return i;
2293 	}
2294 	return -ENOENT;
2295 }
2296 
2297 /* set rtoken for a new link to an existing rmb */
2298 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
2299 		    __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey)
2300 {
2301 	int rtok_idx;
2302 
2303 	rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known));
2304 	if (rtok_idx == -ENOENT)
2305 		return;
2306 	lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey);
2307 	lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr);
2308 }
2309 
2310 /* set rtoken for a new link whose link_id is given */
2311 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
2312 		     __be64 nw_vaddr, __be32 nw_rkey)
2313 {
2314 	u64 dma_addr = be64_to_cpu(nw_vaddr);
2315 	u32 rkey = ntohl(nw_rkey);
2316 	bool found = false;
2317 	int link_idx;
2318 
2319 	for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) {
2320 		if (lgr->lnk[link_idx].link_id == link_id) {
2321 			found = true;
2322 			break;
2323 		}
2324 	}
2325 	if (!found)
2326 		return;
2327 	lgr->rtokens[rtok_idx][link_idx].rkey = rkey;
2328 	lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr;
2329 }
2330 
2331 /* add a new rtoken from peer */
2332 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
2333 {
2334 	struct smc_link_group *lgr = smc_get_lgr(lnk);
2335 	u64 dma_addr = be64_to_cpu(nw_vaddr);
2336 	u32 rkey = ntohl(nw_rkey);
2337 	int i;
2338 
2339 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2340 		if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
2341 		    lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
2342 		    test_bit(i, lgr->rtokens_used_mask)) {
2343 			/* already in list */
2344 			return i;
2345 		}
2346 	}
2347 	i = smc_rmb_reserve_rtoken_idx(lgr);
2348 	if (i < 0)
2349 		return i;
2350 	lgr->rtokens[i][lnk->link_idx].rkey = rkey;
2351 	lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
2352 	return i;
2353 }
2354 
2355 /* delete an rtoken from all links */
2356 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
2357 {
2358 	struct smc_link_group *lgr = smc_get_lgr(lnk);
2359 	u32 rkey = ntohl(nw_rkey);
2360 	int i, j;
2361 
2362 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2363 		if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
2364 		    test_bit(i, lgr->rtokens_used_mask)) {
2365 			for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
2366 				lgr->rtokens[i][j].rkey = 0;
2367 				lgr->rtokens[i][j].dma_addr = 0;
2368 			}
2369 			clear_bit(i, lgr->rtokens_used_mask);
2370 			return 0;
2371 		}
2372 	}
2373 	return -ENOENT;
2374 }
2375 
2376 /* save rkey and dma_addr received from peer during clc handshake */
2377 int smc_rmb_rtoken_handling(struct smc_connection *conn,
2378 			    struct smc_link *lnk,
2379 			    struct smc_clc_msg_accept_confirm *clc)
2380 {
2381 	conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr,
2382 					  clc->r0.rmb_rkey);
2383 	if (conn->rtoken_idx < 0)
2384 		return conn->rtoken_idx;
2385 	return 0;
2386 }
2387 
2388 static void smc_core_going_away(void)
2389 {
2390 	struct smc_ib_device *smcibdev;
2391 	struct smcd_dev *smcd;
2392 
2393 	mutex_lock(&smc_ib_devices.mutex);
2394 	list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
2395 		int i;
2396 
2397 		for (i = 0; i < SMC_MAX_PORTS; i++)
2398 			set_bit(i, smcibdev->ports_going_away);
2399 	}
2400 	mutex_unlock(&smc_ib_devices.mutex);
2401 
2402 	mutex_lock(&smcd_dev_list.mutex);
2403 	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2404 		smcd->going_away = 1;
2405 	}
2406 	mutex_unlock(&smcd_dev_list.mutex);
2407 }
2408 
2409 /* Clean up all SMC link groups */
2410 static void smc_lgrs_shutdown(void)
2411 {
2412 	struct smcd_dev *smcd;
2413 
2414 	smc_core_going_away();
2415 
2416 	smc_smcr_terminate_all(NULL);
2417 
2418 	mutex_lock(&smcd_dev_list.mutex);
2419 	list_for_each_entry(smcd, &smcd_dev_list.list, list)
2420 		smc_smcd_terminate_all(smcd);
2421 	mutex_unlock(&smcd_dev_list.mutex);
2422 }
2423 
2424 static int smc_core_reboot_event(struct notifier_block *this,
2425 				 unsigned long event, void *ptr)
2426 {
2427 	smc_lgrs_shutdown();
2428 	smc_ib_unregister_client();
2429 	return 0;
2430 }
2431 
2432 static struct notifier_block smc_reboot_notifier = {
2433 	.notifier_call = smc_core_reboot_event,
2434 };
2435 
2436 int __init smc_core_init(void)
2437 {
2438 	return register_reboot_notifier(&smc_reboot_notifier);
2439 }
2440 
2441 /* Called (from smc_exit) when module is removed */
2442 void smc_core_exit(void)
2443 {
2444 	unregister_reboot_notifier(&smc_reboot_notifier);
2445 	smc_lgrs_shutdown();
2446 }
2447