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