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