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