xref: /openbmc/linux/net/smc/smc_core.c (revision dfc66bef)
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 	spinlock_t *lgr_lock;
629 
630 	if (!lgr)
631 		return;
632 
633 	smc_conn_free(conn);
634 	smc_lgr_list_head(lgr, &lgr_lock);
635 	spin_lock_bh(lgr_lock);
636 	/* do not use this link group for new connections */
637 	if (!list_empty(&lgr->list))
638 		list_del_init(&lgr->list);
639 	spin_unlock_bh(lgr_lock);
640 	__smc_lgr_terminate(lgr, true);
641 }
642 
643 static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr)
644 {
645 	int i;
646 
647 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
648 		struct smc_link *lnk = &lgr->lnk[i];
649 
650 		if (smc_link_sendable(lnk))
651 			lnk->state = SMC_LNK_INACTIVE;
652 	}
653 	wake_up_all(&lgr->llc_msg_waiter);
654 	wake_up_all(&lgr->llc_flow_waiter);
655 }
656 
657 static void smc_lgr_free(struct smc_link_group *lgr);
658 
659 static void smc_lgr_free_work(struct work_struct *work)
660 {
661 	struct smc_link_group *lgr = container_of(to_delayed_work(work),
662 						  struct smc_link_group,
663 						  free_work);
664 	spinlock_t *lgr_lock;
665 	bool conns;
666 
667 	smc_lgr_list_head(lgr, &lgr_lock);
668 	spin_lock_bh(lgr_lock);
669 	if (lgr->freeing) {
670 		spin_unlock_bh(lgr_lock);
671 		return;
672 	}
673 	read_lock_bh(&lgr->conns_lock);
674 	conns = RB_EMPTY_ROOT(&lgr->conns_all);
675 	read_unlock_bh(&lgr->conns_lock);
676 	if (!conns) { /* number of lgr connections is no longer zero */
677 		spin_unlock_bh(lgr_lock);
678 		return;
679 	}
680 	list_del_init(&lgr->list); /* remove from smc_lgr_list */
681 	lgr->freeing = 1; /* this instance does the freeing, no new schedule */
682 	spin_unlock_bh(lgr_lock);
683 	cancel_delayed_work(&lgr->free_work);
684 
685 	if (!lgr->is_smcd && !lgr->terminating)
686 		smc_llc_send_link_delete_all(lgr, true,
687 					     SMC_LLC_DEL_PROG_INIT_TERM);
688 	if (lgr->is_smcd && !lgr->terminating)
689 		smc_ism_signal_shutdown(lgr);
690 	if (!lgr->is_smcd)
691 		smcr_lgr_link_deactivate_all(lgr);
692 	smc_lgr_free(lgr);
693 }
694 
695 static void smc_lgr_terminate_work(struct work_struct *work)
696 {
697 	struct smc_link_group *lgr = container_of(work, struct smc_link_group,
698 						  terminate_work);
699 
700 	__smc_lgr_terminate(lgr, true);
701 }
702 
703 /* return next unique link id for the lgr */
704 static u8 smcr_next_link_id(struct smc_link_group *lgr)
705 {
706 	u8 link_id;
707 	int i;
708 
709 	while (1) {
710 again:
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 				goto again;
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_wait_pend_tx_wr(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_error(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_wait_pend_tx_wr(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  * We must wait here for QPs been destroyed before we destroy the CQs,
1464  * or we won't received any CQEs and cdc_pend_tx_wr cannot reach 0 thus
1465  * smc_sock cannot be released.
1466  */
1467 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
1468 {
1469 	struct smc_link_group *lgr, *lg;
1470 	LIST_HEAD(lgr_free_list);
1471 	LIST_HEAD(lgr_linkdown_list);
1472 	int i;
1473 
1474 	spin_lock_bh(&smc_lgr_list.lock);
1475 	if (!smcibdev) {
1476 		list_splice_init(&smc_lgr_list.list, &lgr_free_list);
1477 		list_for_each_entry(lgr, &lgr_free_list, list)
1478 			lgr->freeing = 1;
1479 	} else {
1480 		list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
1481 			for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1482 				if (lgr->lnk[i].smcibdev == smcibdev)
1483 					list_move_tail(&lgr->list, &lgr_linkdown_list);
1484 			}
1485 		}
1486 	}
1487 	spin_unlock_bh(&smc_lgr_list.lock);
1488 
1489 	list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1490 		list_del_init(&lgr->list);
1491 		smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM);
1492 		__smc_lgr_terminate(lgr, false);
1493 	}
1494 
1495 	list_for_each_entry_safe(lgr, lg, &lgr_linkdown_list, list) {
1496 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1497 			if (lgr->lnk[i].smcibdev == smcibdev) {
1498 				mutex_lock(&lgr->llc_conf_mutex);
1499 				smcr_link_down_cond(&lgr->lnk[i]);
1500 				mutex_unlock(&lgr->llc_conf_mutex);
1501 			}
1502 		}
1503 	}
1504 
1505 	if (smcibdev) {
1506 		if (atomic_read(&smcibdev->lnk_cnt))
1507 			wait_event(smcibdev->lnks_deleted,
1508 				   !atomic_read(&smcibdev->lnk_cnt));
1509 	} else {
1510 		if (atomic_read(&lgr_cnt))
1511 			wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
1512 	}
1513 }
1514 
1515 /* set new lgr type and clear all asymmetric link tagging */
1516 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type)
1517 {
1518 	char *lgr_type = "";
1519 	int i;
1520 
1521 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1522 		if (smc_link_usable(&lgr->lnk[i]))
1523 			lgr->lnk[i].link_is_asym = false;
1524 	if (lgr->type == new_type)
1525 		return;
1526 	lgr->type = new_type;
1527 
1528 	switch (lgr->type) {
1529 	case SMC_LGR_NONE:
1530 		lgr_type = "NONE";
1531 		break;
1532 	case SMC_LGR_SINGLE:
1533 		lgr_type = "SINGLE";
1534 		break;
1535 	case SMC_LGR_SYMMETRIC:
1536 		lgr_type = "SYMMETRIC";
1537 		break;
1538 	case SMC_LGR_ASYMMETRIC_PEER:
1539 		lgr_type = "ASYMMETRIC_PEER";
1540 		break;
1541 	case SMC_LGR_ASYMMETRIC_LOCAL:
1542 		lgr_type = "ASYMMETRIC_LOCAL";
1543 		break;
1544 	}
1545 	pr_warn_ratelimited("smc: SMC-R lg %*phN state changed: "
1546 			    "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id,
1547 			    lgr_type, lgr->pnet_id);
1548 }
1549 
1550 /* set new lgr type and tag a link as asymmetric */
1551 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
1552 			    enum smc_lgr_type new_type, int asym_lnk_idx)
1553 {
1554 	smcr_lgr_set_type(lgr, new_type);
1555 	lgr->lnk[asym_lnk_idx].link_is_asym = true;
1556 }
1557 
1558 /* abort connection, abort_work scheduled from tasklet context */
1559 static void smc_conn_abort_work(struct work_struct *work)
1560 {
1561 	struct smc_connection *conn = container_of(work,
1562 						   struct smc_connection,
1563 						   abort_work);
1564 	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1565 
1566 	lock_sock(&smc->sk);
1567 	smc_conn_kill(conn, true);
1568 	release_sock(&smc->sk);
1569 	sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
1570 }
1571 
1572 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport)
1573 {
1574 	struct smc_link_group *lgr, *n;
1575 
1576 	list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1577 		struct smc_link *link;
1578 
1579 		if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1580 			    SMC_MAX_PNETID_LEN) ||
1581 		    lgr->type == SMC_LGR_SYMMETRIC ||
1582 		    lgr->type == SMC_LGR_ASYMMETRIC_PEER)
1583 			continue;
1584 
1585 		/* trigger local add link processing */
1586 		link = smc_llc_usable_link(lgr);
1587 		if (link)
1588 			smc_llc_add_link_local(link);
1589 	}
1590 }
1591 
1592 /* link is down - switch connections to alternate link,
1593  * must be called under lgr->llc_conf_mutex lock
1594  */
1595 static void smcr_link_down(struct smc_link *lnk)
1596 {
1597 	struct smc_link_group *lgr = lnk->lgr;
1598 	struct smc_link *to_lnk;
1599 	int del_link_id;
1600 
1601 	if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list))
1602 		return;
1603 
1604 	to_lnk = smc_switch_conns(lgr, lnk, true);
1605 	if (!to_lnk) { /* no backup link available */
1606 		smcr_link_clear(lnk, true);
1607 		return;
1608 	}
1609 	smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1610 	del_link_id = lnk->link_id;
1611 
1612 	if (lgr->role == SMC_SERV) {
1613 		/* trigger local delete link processing */
1614 		smc_llc_srv_delete_link_local(to_lnk, del_link_id);
1615 	} else {
1616 		if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1617 			/* another llc task is ongoing */
1618 			mutex_unlock(&lgr->llc_conf_mutex);
1619 			wait_event_timeout(lgr->llc_flow_waiter,
1620 				(list_empty(&lgr->list) ||
1621 				 lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE),
1622 				SMC_LLC_WAIT_TIME);
1623 			mutex_lock(&lgr->llc_conf_mutex);
1624 		}
1625 		if (!list_empty(&lgr->list)) {
1626 			smc_llc_send_delete_link(to_lnk, del_link_id,
1627 						 SMC_LLC_REQ, true,
1628 						 SMC_LLC_DEL_LOST_PATH);
1629 			smcr_link_clear(lnk, true);
1630 		}
1631 		wake_up(&lgr->llc_flow_waiter);	/* wake up next waiter */
1632 	}
1633 }
1634 
1635 /* must be called under lgr->llc_conf_mutex lock */
1636 void smcr_link_down_cond(struct smc_link *lnk)
1637 {
1638 	if (smc_link_downing(&lnk->state)) {
1639 		trace_smcr_link_down(lnk, __builtin_return_address(0));
1640 		smcr_link_down(lnk);
1641 	}
1642 }
1643 
1644 /* will get the lgr->llc_conf_mutex lock */
1645 void smcr_link_down_cond_sched(struct smc_link *lnk)
1646 {
1647 	if (smc_link_downing(&lnk->state)) {
1648 		trace_smcr_link_down(lnk, __builtin_return_address(0));
1649 		schedule_work(&lnk->link_down_wrk);
1650 	}
1651 }
1652 
1653 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport)
1654 {
1655 	struct smc_link_group *lgr, *n;
1656 	int i;
1657 
1658 	list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1659 		if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1660 			    SMC_MAX_PNETID_LEN))
1661 			continue; /* lgr is not affected */
1662 		if (list_empty(&lgr->list))
1663 			continue;
1664 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1665 			struct smc_link *lnk = &lgr->lnk[i];
1666 
1667 			if (smc_link_usable(lnk) &&
1668 			    lnk->smcibdev == smcibdev && lnk->ibport == ibport)
1669 				smcr_link_down_cond_sched(lnk);
1670 		}
1671 	}
1672 }
1673 
1674 static void smc_link_down_work(struct work_struct *work)
1675 {
1676 	struct smc_link *link = container_of(work, struct smc_link,
1677 					     link_down_wrk);
1678 	struct smc_link_group *lgr = link->lgr;
1679 
1680 	if (list_empty(&lgr->list))
1681 		return;
1682 	wake_up_all(&lgr->llc_msg_waiter);
1683 	mutex_lock(&lgr->llc_conf_mutex);
1684 	smcr_link_down(link);
1685 	mutex_unlock(&lgr->llc_conf_mutex);
1686 }
1687 
1688 static int smc_vlan_by_tcpsk_walk(struct net_device *lower_dev,
1689 				  struct netdev_nested_priv *priv)
1690 {
1691 	unsigned short *vlan_id = (unsigned short *)priv->data;
1692 
1693 	if (is_vlan_dev(lower_dev)) {
1694 		*vlan_id = vlan_dev_vlan_id(lower_dev);
1695 		return 1;
1696 	}
1697 
1698 	return 0;
1699 }
1700 
1701 /* Determine vlan of internal TCP socket. */
1702 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
1703 {
1704 	struct dst_entry *dst = sk_dst_get(clcsock->sk);
1705 	struct netdev_nested_priv priv;
1706 	struct net_device *ndev;
1707 	int rc = 0;
1708 
1709 	ini->vlan_id = 0;
1710 	if (!dst) {
1711 		rc = -ENOTCONN;
1712 		goto out;
1713 	}
1714 	if (!dst->dev) {
1715 		rc = -ENODEV;
1716 		goto out_rel;
1717 	}
1718 
1719 	ndev = dst->dev;
1720 	if (is_vlan_dev(ndev)) {
1721 		ini->vlan_id = vlan_dev_vlan_id(ndev);
1722 		goto out_rel;
1723 	}
1724 
1725 	priv.data = (void *)&ini->vlan_id;
1726 	rtnl_lock();
1727 	netdev_walk_all_lower_dev(ndev, smc_vlan_by_tcpsk_walk, &priv);
1728 	rtnl_unlock();
1729 
1730 out_rel:
1731 	dst_release(dst);
1732 out:
1733 	return rc;
1734 }
1735 
1736 static bool smcr_lgr_match(struct smc_link_group *lgr, u8 smcr_version,
1737 			   u8 peer_systemid[],
1738 			   u8 peer_gid[],
1739 			   u8 peer_mac_v1[],
1740 			   enum smc_lgr_role role, u32 clcqpn)
1741 {
1742 	int i;
1743 
1744 	if (memcmp(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN) ||
1745 	    lgr->role != role)
1746 		return false;
1747 
1748 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1749 		if (!smc_link_active(&lgr->lnk[i]))
1750 			continue;
1751 		if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) &&
1752 		    !memcmp(lgr->lnk[i].peer_gid, peer_gid, SMC_GID_SIZE) &&
1753 		    (smcr_version == SMC_V2 ||
1754 		     !memcmp(lgr->lnk[i].peer_mac, peer_mac_v1, ETH_ALEN)))
1755 			return true;
1756 	}
1757 	return false;
1758 }
1759 
1760 static bool smcd_lgr_match(struct smc_link_group *lgr,
1761 			   struct smcd_dev *smcismdev, u64 peer_gid)
1762 {
1763 	return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
1764 }
1765 
1766 /* create a new SMC connection (and a new link group if necessary) */
1767 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
1768 {
1769 	struct smc_connection *conn = &smc->conn;
1770 	struct list_head *lgr_list;
1771 	struct smc_link_group *lgr;
1772 	enum smc_lgr_role role;
1773 	spinlock_t *lgr_lock;
1774 	int rc = 0;
1775 
1776 	lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list :
1777 				  &smc_lgr_list.list;
1778 	lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock :
1779 				  &smc_lgr_list.lock;
1780 	ini->first_contact_local = 1;
1781 	role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
1782 	if (role == SMC_CLNT && ini->first_contact_peer)
1783 		/* create new link group as well */
1784 		goto create;
1785 
1786 	/* determine if an existing link group can be reused */
1787 	spin_lock_bh(lgr_lock);
1788 	list_for_each_entry(lgr, lgr_list, list) {
1789 		write_lock_bh(&lgr->conns_lock);
1790 		if ((ini->is_smcd ?
1791 		     smcd_lgr_match(lgr, ini->ism_dev[ini->ism_selected],
1792 				    ini->ism_peer_gid[ini->ism_selected]) :
1793 		     smcr_lgr_match(lgr, ini->smcr_version,
1794 				    ini->peer_systemid,
1795 				    ini->peer_gid, ini->peer_mac, role,
1796 				    ini->ib_clcqpn)) &&
1797 		    !lgr->sync_err &&
1798 		    (ini->smcd_version == SMC_V2 ||
1799 		     lgr->vlan_id == ini->vlan_id) &&
1800 		    (role == SMC_CLNT || ini->is_smcd ||
1801 		     lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
1802 			/* link group found */
1803 			ini->first_contact_local = 0;
1804 			conn->lgr = lgr;
1805 			rc = smc_lgr_register_conn(conn, false);
1806 			write_unlock_bh(&lgr->conns_lock);
1807 			if (!rc && delayed_work_pending(&lgr->free_work))
1808 				cancel_delayed_work(&lgr->free_work);
1809 			break;
1810 		}
1811 		write_unlock_bh(&lgr->conns_lock);
1812 	}
1813 	spin_unlock_bh(lgr_lock);
1814 	if (rc)
1815 		return rc;
1816 
1817 	if (role == SMC_CLNT && !ini->first_contact_peer &&
1818 	    ini->first_contact_local) {
1819 		/* Server reuses a link group, but Client wants to start
1820 		 * a new one
1821 		 * send out_of_sync decline, reason synchr. error
1822 		 */
1823 		return SMC_CLC_DECL_SYNCERR;
1824 	}
1825 
1826 create:
1827 	if (ini->first_contact_local) {
1828 		rc = smc_lgr_create(smc, ini);
1829 		if (rc)
1830 			goto out;
1831 		lgr = conn->lgr;
1832 		write_lock_bh(&lgr->conns_lock);
1833 		rc = smc_lgr_register_conn(conn, true);
1834 		write_unlock_bh(&lgr->conns_lock);
1835 		if (rc)
1836 			goto out;
1837 	}
1838 	conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1839 	conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
1840 	conn->urg_state = SMC_URG_READ;
1841 	init_waitqueue_head(&conn->cdc_pend_tx_wq);
1842 	INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work);
1843 	if (ini->is_smcd) {
1844 		conn->rx_off = sizeof(struct smcd_cdc_msg);
1845 		smcd_cdc_rx_init(conn); /* init tasklet for this conn */
1846 	} else {
1847 		conn->rx_off = 0;
1848 	}
1849 #ifndef KERNEL_HAS_ATOMIC64
1850 	spin_lock_init(&conn->acurs_lock);
1851 #endif
1852 
1853 out:
1854 	return rc;
1855 }
1856 
1857 #define SMCD_DMBE_SIZES		6 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1858 #define SMCR_RMBE_SIZES		5 /* 0 -> 16KB, 1 -> 32KB, .. 5 -> 512KB */
1859 
1860 /* convert the RMB size into the compressed notation (minimum 16K, see
1861  * SMCD/R_DMBE_SIZES.
1862  * In contrast to plain ilog2, this rounds towards the next power of 2,
1863  * so the socket application gets at least its desired sndbuf / rcvbuf size.
1864  */
1865 static u8 smc_compress_bufsize(int size, bool is_smcd, bool is_rmb)
1866 {
1867 	const unsigned int max_scat = SG_MAX_SINGLE_ALLOC * PAGE_SIZE;
1868 	u8 compressed;
1869 
1870 	if (size <= SMC_BUF_MIN_SIZE)
1871 		return 0;
1872 
1873 	size = (size - 1) >> 14;  /* convert to 16K multiple */
1874 	compressed = min_t(u8, ilog2(size) + 1,
1875 			   is_smcd ? SMCD_DMBE_SIZES : SMCR_RMBE_SIZES);
1876 
1877 	if (!is_smcd && is_rmb)
1878 		/* RMBs are backed by & limited to max size of scatterlists */
1879 		compressed = min_t(u8, compressed, ilog2(max_scat >> 14));
1880 
1881 	return compressed;
1882 }
1883 
1884 /* convert the RMB size from compressed notation into integer */
1885 int smc_uncompress_bufsize(u8 compressed)
1886 {
1887 	u32 size;
1888 
1889 	size = 0x00000001 << (((int)compressed) + 14);
1890 	return (int)size;
1891 }
1892 
1893 /* try to reuse a sndbuf or rmb description slot for a certain
1894  * buffer size; if not available, return NULL
1895  */
1896 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
1897 					     struct mutex *lock,
1898 					     struct list_head *buf_list)
1899 {
1900 	struct smc_buf_desc *buf_slot;
1901 
1902 	mutex_lock(lock);
1903 	list_for_each_entry(buf_slot, buf_list, list) {
1904 		if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
1905 			mutex_unlock(lock);
1906 			return buf_slot;
1907 		}
1908 	}
1909 	mutex_unlock(lock);
1910 	return NULL;
1911 }
1912 
1913 /* one of the conditions for announcing a receiver's current window size is
1914  * that it "results in a minimum increase in the window size of 10% of the
1915  * receive buffer space" [RFC7609]
1916  */
1917 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
1918 {
1919 	return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
1920 }
1921 
1922 /* map an rmb buf to a link */
1923 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1924 			     struct smc_link *lnk)
1925 {
1926 	int rc;
1927 
1928 	if (buf_desc->is_map_ib[lnk->link_idx])
1929 		return 0;
1930 
1931 	rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL);
1932 	if (rc)
1933 		return rc;
1934 	sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
1935 		   buf_desc->cpu_addr, buf_desc->len);
1936 
1937 	/* map sg table to DMA address */
1938 	rc = smc_ib_buf_map_sg(lnk, buf_desc,
1939 			       is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1940 	/* SMC protocol depends on mapping to one DMA address only */
1941 	if (rc != 1) {
1942 		rc = -EAGAIN;
1943 		goto free_table;
1944 	}
1945 
1946 	/* create a new memory region for the RMB */
1947 	if (is_rmb) {
1948 		rc = smc_ib_get_memory_region(lnk->roce_pd,
1949 					      IB_ACCESS_REMOTE_WRITE |
1950 					      IB_ACCESS_LOCAL_WRITE,
1951 					      buf_desc, lnk->link_idx);
1952 		if (rc)
1953 			goto buf_unmap;
1954 		smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE);
1955 	}
1956 	buf_desc->is_map_ib[lnk->link_idx] = true;
1957 	return 0;
1958 
1959 buf_unmap:
1960 	smc_ib_buf_unmap_sg(lnk, buf_desc,
1961 			    is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1962 free_table:
1963 	sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1964 	return rc;
1965 }
1966 
1967 /* register a new rmb on IB device,
1968  * must be called under lgr->llc_conf_mutex lock
1969  */
1970 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
1971 {
1972 	if (list_empty(&link->lgr->list))
1973 		return -ENOLINK;
1974 	if (!rmb_desc->is_reg_mr[link->link_idx]) {
1975 		/* register memory region for new rmb */
1976 		if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
1977 			rmb_desc->is_reg_err = true;
1978 			return -EFAULT;
1979 		}
1980 		rmb_desc->is_reg_mr[link->link_idx] = true;
1981 	}
1982 	return 0;
1983 }
1984 
1985 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct mutex *lock,
1986 			     struct list_head *lst, bool is_rmb)
1987 {
1988 	struct smc_buf_desc *buf_desc, *bf;
1989 	int rc = 0;
1990 
1991 	mutex_lock(lock);
1992 	list_for_each_entry_safe(buf_desc, bf, lst, list) {
1993 		if (!buf_desc->used)
1994 			continue;
1995 		rc = smcr_buf_map_link(buf_desc, is_rmb, lnk);
1996 		if (rc)
1997 			goto out;
1998 	}
1999 out:
2000 	mutex_unlock(lock);
2001 	return rc;
2002 }
2003 
2004 /* map all used buffers of lgr for a new link */
2005 int smcr_buf_map_lgr(struct smc_link *lnk)
2006 {
2007 	struct smc_link_group *lgr = lnk->lgr;
2008 	int i, rc = 0;
2009 
2010 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
2011 		rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock,
2012 				       &lgr->rmbs[i], true);
2013 		if (rc)
2014 			return rc;
2015 		rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock,
2016 				       &lgr->sndbufs[i], false);
2017 		if (rc)
2018 			return rc;
2019 	}
2020 	return 0;
2021 }
2022 
2023 /* register all used buffers of lgr for a new link,
2024  * must be called under lgr->llc_conf_mutex lock
2025  */
2026 int smcr_buf_reg_lgr(struct smc_link *lnk)
2027 {
2028 	struct smc_link_group *lgr = lnk->lgr;
2029 	struct smc_buf_desc *buf_desc, *bf;
2030 	int i, rc = 0;
2031 
2032 	mutex_lock(&lgr->rmbs_lock);
2033 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
2034 		list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) {
2035 			if (!buf_desc->used)
2036 				continue;
2037 			rc = smcr_link_reg_rmb(lnk, buf_desc);
2038 			if (rc)
2039 				goto out;
2040 		}
2041 	}
2042 out:
2043 	mutex_unlock(&lgr->rmbs_lock);
2044 	return rc;
2045 }
2046 
2047 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
2048 						bool is_rmb, int bufsize)
2049 {
2050 	struct smc_buf_desc *buf_desc;
2051 
2052 	/* try to alloc a new buffer */
2053 	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
2054 	if (!buf_desc)
2055 		return ERR_PTR(-ENOMEM);
2056 
2057 	buf_desc->order = get_order(bufsize);
2058 	buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
2059 				      __GFP_NOMEMALLOC | __GFP_COMP |
2060 				      __GFP_NORETRY | __GFP_ZERO,
2061 				      buf_desc->order);
2062 	if (!buf_desc->pages) {
2063 		kfree(buf_desc);
2064 		return ERR_PTR(-EAGAIN);
2065 	}
2066 	buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
2067 	buf_desc->len = bufsize;
2068 	return buf_desc;
2069 }
2070 
2071 /* map buf_desc on all usable links,
2072  * unused buffers stay mapped as long as the link is up
2073  */
2074 static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
2075 				     struct smc_buf_desc *buf_desc, bool is_rmb)
2076 {
2077 	int i, rc = 0;
2078 
2079 	/* protect against parallel link reconfiguration */
2080 	mutex_lock(&lgr->llc_conf_mutex);
2081 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2082 		struct smc_link *lnk = &lgr->lnk[i];
2083 
2084 		if (!smc_link_usable(lnk))
2085 			continue;
2086 		if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
2087 			rc = -ENOMEM;
2088 			goto out;
2089 		}
2090 	}
2091 out:
2092 	mutex_unlock(&lgr->llc_conf_mutex);
2093 	return rc;
2094 }
2095 
2096 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
2097 						bool is_dmb, int bufsize)
2098 {
2099 	struct smc_buf_desc *buf_desc;
2100 	int rc;
2101 
2102 	/* try to alloc a new DMB */
2103 	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
2104 	if (!buf_desc)
2105 		return ERR_PTR(-ENOMEM);
2106 	if (is_dmb) {
2107 		rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
2108 		if (rc) {
2109 			kfree(buf_desc);
2110 			if (rc == -ENOMEM)
2111 				return ERR_PTR(-EAGAIN);
2112 			if (rc == -ENOSPC)
2113 				return ERR_PTR(-ENOSPC);
2114 			return ERR_PTR(-EIO);
2115 		}
2116 		buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
2117 		/* CDC header stored in buf. So, pretend it was smaller */
2118 		buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
2119 	} else {
2120 		buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
2121 					     __GFP_NOWARN | __GFP_NORETRY |
2122 					     __GFP_NOMEMALLOC);
2123 		if (!buf_desc->cpu_addr) {
2124 			kfree(buf_desc);
2125 			return ERR_PTR(-EAGAIN);
2126 		}
2127 		buf_desc->len = bufsize;
2128 	}
2129 	return buf_desc;
2130 }
2131 
2132 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
2133 {
2134 	struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
2135 	struct smc_connection *conn = &smc->conn;
2136 	struct smc_link_group *lgr = conn->lgr;
2137 	struct list_head *buf_list;
2138 	int bufsize, bufsize_short;
2139 	bool is_dgraded = false;
2140 	struct mutex *lock;	/* lock buffer list */
2141 	int sk_buf_size;
2142 
2143 	if (is_rmb)
2144 		/* use socket recv buffer size (w/o overhead) as start value */
2145 		sk_buf_size = smc->sk.sk_rcvbuf / 2;
2146 	else
2147 		/* use socket send buffer size (w/o overhead) as start value */
2148 		sk_buf_size = smc->sk.sk_sndbuf / 2;
2149 
2150 	for (bufsize_short = smc_compress_bufsize(sk_buf_size, is_smcd, is_rmb);
2151 	     bufsize_short >= 0; bufsize_short--) {
2152 		if (is_rmb) {
2153 			lock = &lgr->rmbs_lock;
2154 			buf_list = &lgr->rmbs[bufsize_short];
2155 		} else {
2156 			lock = &lgr->sndbufs_lock;
2157 			buf_list = &lgr->sndbufs[bufsize_short];
2158 		}
2159 		bufsize = smc_uncompress_bufsize(bufsize_short);
2160 
2161 		/* check for reusable slot in the link group */
2162 		buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
2163 		if (buf_desc) {
2164 			SMC_STAT_RMB_SIZE(smc, is_smcd, is_rmb, bufsize);
2165 			SMC_STAT_BUF_REUSE(smc, is_smcd, is_rmb);
2166 			memset(buf_desc->cpu_addr, 0, bufsize);
2167 			break; /* found reusable slot */
2168 		}
2169 
2170 		if (is_smcd)
2171 			buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
2172 		else
2173 			buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
2174 
2175 		if (PTR_ERR(buf_desc) == -ENOMEM)
2176 			break;
2177 		if (IS_ERR(buf_desc)) {
2178 			if (!is_dgraded) {
2179 				is_dgraded = true;
2180 				SMC_STAT_RMB_DOWNGRADED(smc, is_smcd, is_rmb);
2181 			}
2182 			continue;
2183 		}
2184 
2185 		SMC_STAT_RMB_ALLOC(smc, is_smcd, is_rmb);
2186 		SMC_STAT_RMB_SIZE(smc, is_smcd, is_rmb, bufsize);
2187 		buf_desc->used = 1;
2188 		mutex_lock(lock);
2189 		list_add(&buf_desc->list, buf_list);
2190 		mutex_unlock(lock);
2191 		break; /* found */
2192 	}
2193 
2194 	if (IS_ERR(buf_desc))
2195 		return PTR_ERR(buf_desc);
2196 
2197 	if (!is_smcd) {
2198 		if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
2199 			smcr_buf_unuse(buf_desc, lgr);
2200 			return -ENOMEM;
2201 		}
2202 	}
2203 
2204 	if (is_rmb) {
2205 		conn->rmb_desc = buf_desc;
2206 		conn->rmbe_size_short = bufsize_short;
2207 		smc->sk.sk_rcvbuf = bufsize * 2;
2208 		atomic_set(&conn->bytes_to_rcv, 0);
2209 		conn->rmbe_update_limit =
2210 			smc_rmb_wnd_update_limit(buf_desc->len);
2211 		if (is_smcd)
2212 			smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
2213 	} else {
2214 		conn->sndbuf_desc = buf_desc;
2215 		smc->sk.sk_sndbuf = bufsize * 2;
2216 		atomic_set(&conn->sndbuf_space, bufsize);
2217 	}
2218 	return 0;
2219 }
2220 
2221 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
2222 {
2223 	if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
2224 		return;
2225 	smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
2226 }
2227 
2228 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
2229 {
2230 	if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
2231 		return;
2232 	smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
2233 }
2234 
2235 void smc_rmb_sync_sg_for_cpu(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_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
2245 				       DMA_FROM_DEVICE);
2246 	}
2247 }
2248 
2249 void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
2250 {
2251 	int i;
2252 
2253 	if (!conn->lgr || conn->lgr->is_smcd)
2254 		return;
2255 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
2256 		if (!smc_link_active(&conn->lgr->lnk[i]))
2257 			continue;
2258 		smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc,
2259 					  DMA_FROM_DEVICE);
2260 	}
2261 }
2262 
2263 /* create the send and receive buffer for an SMC socket;
2264  * receive buffers are called RMBs;
2265  * (even though the SMC protocol allows more than one RMB-element per RMB,
2266  * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
2267  * extra RMB for every connection in a link group
2268  */
2269 int smc_buf_create(struct smc_sock *smc, bool is_smcd)
2270 {
2271 	int rc;
2272 
2273 	/* create send buffer */
2274 	rc = __smc_buf_create(smc, is_smcd, false);
2275 	if (rc)
2276 		return rc;
2277 	/* create rmb */
2278 	rc = __smc_buf_create(smc, is_smcd, true);
2279 	if (rc) {
2280 		mutex_lock(&smc->conn.lgr->sndbufs_lock);
2281 		list_del(&smc->conn.sndbuf_desc->list);
2282 		mutex_unlock(&smc->conn.lgr->sndbufs_lock);
2283 		smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
2284 		smc->conn.sndbuf_desc = NULL;
2285 	}
2286 	return rc;
2287 }
2288 
2289 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
2290 {
2291 	int i;
2292 
2293 	for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
2294 		if (!test_and_set_bit(i, lgr->rtokens_used_mask))
2295 			return i;
2296 	}
2297 	return -ENOSPC;
2298 }
2299 
2300 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx,
2301 				   u32 rkey)
2302 {
2303 	int i;
2304 
2305 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2306 		if (test_bit(i, lgr->rtokens_used_mask) &&
2307 		    lgr->rtokens[i][lnk_idx].rkey == rkey)
2308 			return i;
2309 	}
2310 	return -ENOENT;
2311 }
2312 
2313 /* set rtoken for a new link to an existing rmb */
2314 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
2315 		    __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey)
2316 {
2317 	int rtok_idx;
2318 
2319 	rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known));
2320 	if (rtok_idx == -ENOENT)
2321 		return;
2322 	lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey);
2323 	lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr);
2324 }
2325 
2326 /* set rtoken for a new link whose link_id is given */
2327 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
2328 		     __be64 nw_vaddr, __be32 nw_rkey)
2329 {
2330 	u64 dma_addr = be64_to_cpu(nw_vaddr);
2331 	u32 rkey = ntohl(nw_rkey);
2332 	bool found = false;
2333 	int link_idx;
2334 
2335 	for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) {
2336 		if (lgr->lnk[link_idx].link_id == link_id) {
2337 			found = true;
2338 			break;
2339 		}
2340 	}
2341 	if (!found)
2342 		return;
2343 	lgr->rtokens[rtok_idx][link_idx].rkey = rkey;
2344 	lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr;
2345 }
2346 
2347 /* add a new rtoken from peer */
2348 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
2349 {
2350 	struct smc_link_group *lgr = smc_get_lgr(lnk);
2351 	u64 dma_addr = be64_to_cpu(nw_vaddr);
2352 	u32 rkey = ntohl(nw_rkey);
2353 	int i;
2354 
2355 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2356 		if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
2357 		    lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
2358 		    test_bit(i, lgr->rtokens_used_mask)) {
2359 			/* already in list */
2360 			return i;
2361 		}
2362 	}
2363 	i = smc_rmb_reserve_rtoken_idx(lgr);
2364 	if (i < 0)
2365 		return i;
2366 	lgr->rtokens[i][lnk->link_idx].rkey = rkey;
2367 	lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
2368 	return i;
2369 }
2370 
2371 /* delete an rtoken from all links */
2372 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
2373 {
2374 	struct smc_link_group *lgr = smc_get_lgr(lnk);
2375 	u32 rkey = ntohl(nw_rkey);
2376 	int i, j;
2377 
2378 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
2379 		if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
2380 		    test_bit(i, lgr->rtokens_used_mask)) {
2381 			for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
2382 				lgr->rtokens[i][j].rkey = 0;
2383 				lgr->rtokens[i][j].dma_addr = 0;
2384 			}
2385 			clear_bit(i, lgr->rtokens_used_mask);
2386 			return 0;
2387 		}
2388 	}
2389 	return -ENOENT;
2390 }
2391 
2392 /* save rkey and dma_addr received from peer during clc handshake */
2393 int smc_rmb_rtoken_handling(struct smc_connection *conn,
2394 			    struct smc_link *lnk,
2395 			    struct smc_clc_msg_accept_confirm *clc)
2396 {
2397 	conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr,
2398 					  clc->r0.rmb_rkey);
2399 	if (conn->rtoken_idx < 0)
2400 		return conn->rtoken_idx;
2401 	return 0;
2402 }
2403 
2404 static void smc_core_going_away(void)
2405 {
2406 	struct smc_ib_device *smcibdev;
2407 	struct smcd_dev *smcd;
2408 
2409 	mutex_lock(&smc_ib_devices.mutex);
2410 	list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
2411 		int i;
2412 
2413 		for (i = 0; i < SMC_MAX_PORTS; i++)
2414 			set_bit(i, smcibdev->ports_going_away);
2415 	}
2416 	mutex_unlock(&smc_ib_devices.mutex);
2417 
2418 	mutex_lock(&smcd_dev_list.mutex);
2419 	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2420 		smcd->going_away = 1;
2421 	}
2422 	mutex_unlock(&smcd_dev_list.mutex);
2423 }
2424 
2425 /* Clean up all SMC link groups */
2426 static void smc_lgrs_shutdown(void)
2427 {
2428 	struct smcd_dev *smcd;
2429 
2430 	smc_core_going_away();
2431 
2432 	smc_smcr_terminate_all(NULL);
2433 
2434 	mutex_lock(&smcd_dev_list.mutex);
2435 	list_for_each_entry(smcd, &smcd_dev_list.list, list)
2436 		smc_smcd_terminate_all(smcd);
2437 	mutex_unlock(&smcd_dev_list.mutex);
2438 }
2439 
2440 static int smc_core_reboot_event(struct notifier_block *this,
2441 				 unsigned long event, void *ptr)
2442 {
2443 	smc_lgrs_shutdown();
2444 	smc_ib_unregister_client();
2445 	return 0;
2446 }
2447 
2448 static struct notifier_block smc_reboot_notifier = {
2449 	.notifier_call = smc_core_reboot_event,
2450 };
2451 
2452 int __init smc_core_init(void)
2453 {
2454 	return register_reboot_notifier(&smc_reboot_notifier);
2455 }
2456 
2457 /* Called (from smc_exit) when module is removed */
2458 void smc_core_exit(void)
2459 {
2460 	unregister_reboot_notifier(&smc_reboot_notifier);
2461 	smc_lgrs_shutdown();
2462 }
2463