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