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