xref: /openbmc/linux/net/smc/smc_llc.c (revision b285d2ae)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  Link Layer Control (LLC)
6  *
7  *  Copyright IBM Corp. 2016
8  *
9  *  Author(s):  Klaus Wacker <Klaus.Wacker@de.ibm.com>
10  *              Ursula Braun <ubraun@linux.vnet.ibm.com>
11  */
12 
13 #include <net/tcp.h>
14 #include <rdma/ib_verbs.h>
15 
16 #include "smc.h"
17 #include "smc_core.h"
18 #include "smc_clc.h"
19 #include "smc_llc.h"
20 #include "smc_pnet.h"
21 
22 #define SMC_LLC_DATA_LEN		40
23 
24 struct smc_llc_hdr {
25 	struct smc_wr_rx_hdr common;
26 	u8 length;	/* 44 */
27 #if defined(__BIG_ENDIAN_BITFIELD)
28 	u8 reserved:4,
29 	   add_link_rej_rsn:4;
30 #elif defined(__LITTLE_ENDIAN_BITFIELD)
31 	u8 add_link_rej_rsn:4,
32 	   reserved:4;
33 #endif
34 	u8 flags;
35 };
36 
37 #define SMC_LLC_FLAG_NO_RMBE_EYEC	0x03
38 
39 struct smc_llc_msg_confirm_link {	/* type 0x01 */
40 	struct smc_llc_hdr hd;
41 	u8 sender_mac[ETH_ALEN];
42 	u8 sender_gid[SMC_GID_SIZE];
43 	u8 sender_qp_num[3];
44 	u8 link_num;
45 	u8 link_uid[SMC_LGR_ID_SIZE];
46 	u8 max_links;
47 	u8 reserved[9];
48 };
49 
50 #define SMC_LLC_FLAG_ADD_LNK_REJ	0x40
51 #define SMC_LLC_REJ_RSN_NO_ALT_PATH	1
52 
53 #define SMC_LLC_ADD_LNK_MAX_LINKS	2
54 
55 struct smc_llc_msg_add_link {		/* type 0x02 */
56 	struct smc_llc_hdr hd;
57 	u8 sender_mac[ETH_ALEN];
58 	u8 reserved2[2];
59 	u8 sender_gid[SMC_GID_SIZE];
60 	u8 sender_qp_num[3];
61 	u8 link_num;
62 #if defined(__BIG_ENDIAN_BITFIELD)
63 	u8 reserved3 : 4,
64 	   qp_mtu   : 4;
65 #elif defined(__LITTLE_ENDIAN_BITFIELD)
66 	u8 qp_mtu   : 4,
67 	   reserved3 : 4;
68 #endif
69 	u8 initial_psn[3];
70 	u8 reserved[8];
71 };
72 
73 struct smc_llc_msg_add_link_cont_rt {
74 	__be32 rmb_key;
75 	__be32 rmb_key_new;
76 	__be64 rmb_vaddr_new;
77 };
78 
79 #define SMC_LLC_RKEYS_PER_CONT_MSG	2
80 
81 struct smc_llc_msg_add_link_cont {	/* type 0x03 */
82 	struct smc_llc_hdr hd;
83 	u8 link_num;
84 	u8 num_rkeys;
85 	u8 reserved2[2];
86 	struct smc_llc_msg_add_link_cont_rt rt[SMC_LLC_RKEYS_PER_CONT_MSG];
87 	u8 reserved[4];
88 } __packed;			/* format defined in RFC7609 */
89 
90 #define SMC_LLC_FLAG_DEL_LINK_ALL	0x40
91 #define SMC_LLC_FLAG_DEL_LINK_ORDERLY	0x20
92 
93 struct smc_llc_msg_del_link {		/* type 0x04 */
94 	struct smc_llc_hdr hd;
95 	u8 link_num;
96 	__be32 reason;
97 	u8 reserved[35];
98 } __packed;			/* format defined in RFC7609 */
99 
100 struct smc_llc_msg_test_link {		/* type 0x07 */
101 	struct smc_llc_hdr hd;
102 	u8 user_data[16];
103 	u8 reserved[24];
104 };
105 
106 struct smc_rmb_rtoken {
107 	union {
108 		u8 num_rkeys;	/* first rtoken byte of CONFIRM LINK msg */
109 				/* is actually the num of rtokens, first */
110 				/* rtoken is always for the current link */
111 		u8 link_id;	/* link id of the rtoken */
112 	};
113 	__be32 rmb_key;
114 	__be64 rmb_vaddr;
115 } __packed;			/* format defined in RFC7609 */
116 
117 #define SMC_LLC_RKEYS_PER_MSG	3
118 
119 struct smc_llc_msg_confirm_rkey {	/* type 0x06 */
120 	struct smc_llc_hdr hd;
121 	struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
122 	u8 reserved;
123 };
124 
125 #define SMC_LLC_DEL_RKEY_MAX	8
126 #define SMC_LLC_FLAG_RKEY_RETRY	0x10
127 #define SMC_LLC_FLAG_RKEY_NEG	0x20
128 
129 struct smc_llc_msg_delete_rkey {	/* type 0x09 */
130 	struct smc_llc_hdr hd;
131 	u8 num_rkeys;
132 	u8 err_mask;
133 	u8 reserved[2];
134 	__be32 rkey[8];
135 	u8 reserved2[4];
136 };
137 
138 union smc_llc_msg {
139 	struct smc_llc_msg_confirm_link confirm_link;
140 	struct smc_llc_msg_add_link add_link;
141 	struct smc_llc_msg_add_link_cont add_link_cont;
142 	struct smc_llc_msg_del_link delete_link;
143 
144 	struct smc_llc_msg_confirm_rkey confirm_rkey;
145 	struct smc_llc_msg_delete_rkey delete_rkey;
146 
147 	struct smc_llc_msg_test_link test_link;
148 	struct {
149 		struct smc_llc_hdr hdr;
150 		u8 data[SMC_LLC_DATA_LEN];
151 	} raw;
152 };
153 
154 #define SMC_LLC_FLAG_RESP		0x80
155 
156 struct smc_llc_qentry {
157 	struct list_head list;
158 	struct smc_link *link;
159 	union smc_llc_msg msg;
160 };
161 
162 static void smc_llc_enqueue(struct smc_link *link, union smc_llc_msg *llc);
163 
164 struct smc_llc_qentry *smc_llc_flow_qentry_clr(struct smc_llc_flow *flow)
165 {
166 	struct smc_llc_qentry *qentry = flow->qentry;
167 
168 	flow->qentry = NULL;
169 	return qentry;
170 }
171 
172 void smc_llc_flow_qentry_del(struct smc_llc_flow *flow)
173 {
174 	struct smc_llc_qentry *qentry;
175 
176 	if (flow->qentry) {
177 		qentry = flow->qentry;
178 		flow->qentry = NULL;
179 		kfree(qentry);
180 	}
181 }
182 
183 static inline void smc_llc_flow_qentry_set(struct smc_llc_flow *flow,
184 					   struct smc_llc_qentry *qentry)
185 {
186 	flow->qentry = qentry;
187 }
188 
189 static void smc_llc_flow_parallel(struct smc_link_group *lgr, u8 flow_type,
190 				  struct smc_llc_qentry *qentry)
191 {
192 	u8 msg_type = qentry->msg.raw.hdr.common.type;
193 
194 	if ((msg_type == SMC_LLC_ADD_LINK || msg_type == SMC_LLC_DELETE_LINK) &&
195 	    flow_type != msg_type && !lgr->delayed_event) {
196 		lgr->delayed_event = qentry;
197 		return;
198 	}
199 	/* drop parallel or already-in-progress llc requests */
200 	if (flow_type != msg_type)
201 		pr_warn_once("smc: SMC-R lg %*phN dropped parallel "
202 			     "LLC msg: msg %d flow %d role %d\n",
203 			     SMC_LGR_ID_SIZE, &lgr->id,
204 			     qentry->msg.raw.hdr.common.type,
205 			     flow_type, lgr->role);
206 	kfree(qentry);
207 }
208 
209 /* try to start a new llc flow, initiated by an incoming llc msg */
210 static bool smc_llc_flow_start(struct smc_llc_flow *flow,
211 			       struct smc_llc_qentry *qentry)
212 {
213 	struct smc_link_group *lgr = qentry->link->lgr;
214 
215 	spin_lock_bh(&lgr->llc_flow_lock);
216 	if (flow->type) {
217 		/* a flow is already active */
218 		smc_llc_flow_parallel(lgr, flow->type, qentry);
219 		spin_unlock_bh(&lgr->llc_flow_lock);
220 		return false;
221 	}
222 	switch (qentry->msg.raw.hdr.common.type) {
223 	case SMC_LLC_ADD_LINK:
224 		flow->type = SMC_LLC_FLOW_ADD_LINK;
225 		break;
226 	case SMC_LLC_DELETE_LINK:
227 		flow->type = SMC_LLC_FLOW_DEL_LINK;
228 		break;
229 	case SMC_LLC_CONFIRM_RKEY:
230 	case SMC_LLC_DELETE_RKEY:
231 		flow->type = SMC_LLC_FLOW_RKEY;
232 		break;
233 	default:
234 		flow->type = SMC_LLC_FLOW_NONE;
235 	}
236 	if (qentry == lgr->delayed_event)
237 		lgr->delayed_event = NULL;
238 	smc_llc_flow_qentry_set(flow, qentry);
239 	spin_unlock_bh(&lgr->llc_flow_lock);
240 	return true;
241 }
242 
243 /* start a new local llc flow, wait till current flow finished */
244 int smc_llc_flow_initiate(struct smc_link_group *lgr,
245 			  enum smc_llc_flowtype type)
246 {
247 	enum smc_llc_flowtype allowed_remote = SMC_LLC_FLOW_NONE;
248 	int rc;
249 
250 	/* all flows except confirm_rkey and delete_rkey are exclusive,
251 	 * confirm/delete rkey flows can run concurrently (local and remote)
252 	 */
253 	if (type == SMC_LLC_FLOW_RKEY)
254 		allowed_remote = SMC_LLC_FLOW_RKEY;
255 again:
256 	if (list_empty(&lgr->list))
257 		return -ENODEV;
258 	spin_lock_bh(&lgr->llc_flow_lock);
259 	if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE &&
260 	    (lgr->llc_flow_rmt.type == SMC_LLC_FLOW_NONE ||
261 	     lgr->llc_flow_rmt.type == allowed_remote)) {
262 		lgr->llc_flow_lcl.type = type;
263 		spin_unlock_bh(&lgr->llc_flow_lock);
264 		return 0;
265 	}
266 	spin_unlock_bh(&lgr->llc_flow_lock);
267 	rc = wait_event_timeout(lgr->llc_flow_waiter, (list_empty(&lgr->list) ||
268 				(lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE &&
269 				 (lgr->llc_flow_rmt.type == SMC_LLC_FLOW_NONE ||
270 				  lgr->llc_flow_rmt.type == allowed_remote))),
271 				SMC_LLC_WAIT_TIME * 10);
272 	if (!rc)
273 		return -ETIMEDOUT;
274 	goto again;
275 }
276 
277 /* finish the current llc flow */
278 void smc_llc_flow_stop(struct smc_link_group *lgr, struct smc_llc_flow *flow)
279 {
280 	spin_lock_bh(&lgr->llc_flow_lock);
281 	memset(flow, 0, sizeof(*flow));
282 	flow->type = SMC_LLC_FLOW_NONE;
283 	spin_unlock_bh(&lgr->llc_flow_lock);
284 	if (!list_empty(&lgr->list) && lgr->delayed_event &&
285 	    flow == &lgr->llc_flow_lcl)
286 		schedule_work(&lgr->llc_event_work);
287 	else
288 		wake_up(&lgr->llc_flow_waiter);
289 }
290 
291 /* lnk is optional and used for early wakeup when link goes down, useful in
292  * cases where we wait for a response on the link after we sent a request
293  */
294 struct smc_llc_qentry *smc_llc_wait(struct smc_link_group *lgr,
295 				    struct smc_link *lnk,
296 				    int time_out, u8 exp_msg)
297 {
298 	struct smc_llc_flow *flow = &lgr->llc_flow_lcl;
299 	u8 rcv_msg;
300 
301 	wait_event_timeout(lgr->llc_msg_waiter,
302 			   (flow->qentry ||
303 			    (lnk && !smc_link_usable(lnk)) ||
304 			    list_empty(&lgr->list)),
305 			   time_out);
306 	if (!flow->qentry ||
307 	    (lnk && !smc_link_usable(lnk)) || list_empty(&lgr->list)) {
308 		smc_llc_flow_qentry_del(flow);
309 		goto out;
310 	}
311 	rcv_msg = flow->qentry->msg.raw.hdr.common.type;
312 	if (exp_msg && rcv_msg != exp_msg) {
313 		if (exp_msg == SMC_LLC_ADD_LINK &&
314 		    rcv_msg == SMC_LLC_DELETE_LINK) {
315 			/* flow_start will delay the unexpected msg */
316 			smc_llc_flow_start(&lgr->llc_flow_lcl,
317 					   smc_llc_flow_qentry_clr(flow));
318 			return NULL;
319 		}
320 		pr_warn_once("smc: SMC-R lg %*phN dropped unexpected LLC msg: "
321 			     "msg %d exp %d flow %d role %d flags %x\n",
322 			     SMC_LGR_ID_SIZE, &lgr->id, rcv_msg, exp_msg,
323 			     flow->type, lgr->role,
324 			     flow->qentry->msg.raw.hdr.flags);
325 		smc_llc_flow_qentry_del(flow);
326 	}
327 out:
328 	return flow->qentry;
329 }
330 
331 /********************************** send *************************************/
332 
333 struct smc_llc_tx_pend {
334 };
335 
336 /* handler for send/transmission completion of an LLC msg */
337 static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
338 			       struct smc_link *link,
339 			       enum ib_wc_status wc_status)
340 {
341 	/* future work: handle wc_status error for recovery and failover */
342 }
343 
344 /**
345  * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
346  * @link: Pointer to SMC link used for sending LLC control message.
347  * @wr_buf: Out variable returning pointer to work request payload buffer.
348  * @pend: Out variable returning pointer to private pending WR tracking.
349  *	  It's the context the transmit complete handler will get.
350  *
351  * Reserves and pre-fills an entry for a pending work request send/tx.
352  * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
353  * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
354  *
355  * Return: 0 on success, otherwise an error value.
356  */
357 static int smc_llc_add_pending_send(struct smc_link *link,
358 				    struct smc_wr_buf **wr_buf,
359 				    struct smc_wr_tx_pend_priv **pend)
360 {
361 	int rc;
362 
363 	rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, NULL,
364 				     pend);
365 	if (rc < 0)
366 		return rc;
367 	BUILD_BUG_ON_MSG(
368 		sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
369 		"must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
370 	BUILD_BUG_ON_MSG(
371 		sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
372 		"must adapt SMC_WR_TX_SIZE to sizeof(struct smc_llc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()");
373 	BUILD_BUG_ON_MSG(
374 		sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
375 		"must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
376 	return 0;
377 }
378 
379 /* high-level API to send LLC confirm link */
380 int smc_llc_send_confirm_link(struct smc_link *link,
381 			      enum smc_llc_reqresp reqresp)
382 {
383 	struct smc_llc_msg_confirm_link *confllc;
384 	struct smc_wr_tx_pend_priv *pend;
385 	struct smc_wr_buf *wr_buf;
386 	int rc;
387 
388 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
389 	if (rc)
390 		return rc;
391 	confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
392 	memset(confllc, 0, sizeof(*confllc));
393 	confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
394 	confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
395 	confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
396 	if (reqresp == SMC_LLC_RESP)
397 		confllc->hd.flags |= SMC_LLC_FLAG_RESP;
398 	memcpy(confllc->sender_mac, link->smcibdev->mac[link->ibport - 1],
399 	       ETH_ALEN);
400 	memcpy(confllc->sender_gid, link->gid, SMC_GID_SIZE);
401 	hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
402 	confllc->link_num = link->link_id;
403 	memcpy(confllc->link_uid, link->link_uid, SMC_LGR_ID_SIZE);
404 	confllc->max_links = SMC_LLC_ADD_LNK_MAX_LINKS;
405 	/* send llc message */
406 	rc = smc_wr_tx_send(link, pend);
407 	return rc;
408 }
409 
410 /* send LLC confirm rkey request */
411 static int smc_llc_send_confirm_rkey(struct smc_link *send_link,
412 				     struct smc_buf_desc *rmb_desc)
413 {
414 	struct smc_llc_msg_confirm_rkey *rkeyllc;
415 	struct smc_wr_tx_pend_priv *pend;
416 	struct smc_wr_buf *wr_buf;
417 	struct smc_link *link;
418 	int i, rc, rtok_ix;
419 
420 	rc = smc_llc_add_pending_send(send_link, &wr_buf, &pend);
421 	if (rc)
422 		return rc;
423 	rkeyllc = (struct smc_llc_msg_confirm_rkey *)wr_buf;
424 	memset(rkeyllc, 0, sizeof(*rkeyllc));
425 	rkeyllc->hd.common.type = SMC_LLC_CONFIRM_RKEY;
426 	rkeyllc->hd.length = sizeof(struct smc_llc_msg_confirm_rkey);
427 
428 	rtok_ix = 1;
429 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
430 		link = &send_link->lgr->lnk[i];
431 		if (smc_link_active(link) && link != send_link) {
432 			rkeyllc->rtoken[rtok_ix].link_id = link->link_id;
433 			rkeyllc->rtoken[rtok_ix].rmb_key =
434 				htonl(rmb_desc->mr_rx[link->link_idx]->rkey);
435 			rkeyllc->rtoken[rtok_ix].rmb_vaddr = cpu_to_be64(
436 				(u64)sg_dma_address(
437 					rmb_desc->sgt[link->link_idx].sgl));
438 			rtok_ix++;
439 		}
440 	}
441 	/* rkey of send_link is in rtoken[0] */
442 	rkeyllc->rtoken[0].num_rkeys = rtok_ix - 1;
443 	rkeyllc->rtoken[0].rmb_key =
444 		htonl(rmb_desc->mr_rx[send_link->link_idx]->rkey);
445 	rkeyllc->rtoken[0].rmb_vaddr = cpu_to_be64(
446 		(u64)sg_dma_address(rmb_desc->sgt[send_link->link_idx].sgl));
447 	/* send llc message */
448 	rc = smc_wr_tx_send(send_link, pend);
449 	return rc;
450 }
451 
452 /* send LLC delete rkey request */
453 static int smc_llc_send_delete_rkey(struct smc_link *link,
454 				    struct smc_buf_desc *rmb_desc)
455 {
456 	struct smc_llc_msg_delete_rkey *rkeyllc;
457 	struct smc_wr_tx_pend_priv *pend;
458 	struct smc_wr_buf *wr_buf;
459 	int rc;
460 
461 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
462 	if (rc)
463 		return rc;
464 	rkeyllc = (struct smc_llc_msg_delete_rkey *)wr_buf;
465 	memset(rkeyllc, 0, sizeof(*rkeyllc));
466 	rkeyllc->hd.common.type = SMC_LLC_DELETE_RKEY;
467 	rkeyllc->hd.length = sizeof(struct smc_llc_msg_delete_rkey);
468 	rkeyllc->num_rkeys = 1;
469 	rkeyllc->rkey[0] = htonl(rmb_desc->mr_rx[link->link_idx]->rkey);
470 	/* send llc message */
471 	rc = smc_wr_tx_send(link, pend);
472 	return rc;
473 }
474 
475 /* send ADD LINK request or response */
476 int smc_llc_send_add_link(struct smc_link *link, u8 mac[], u8 gid[],
477 			  struct smc_link *link_new,
478 			  enum smc_llc_reqresp reqresp)
479 {
480 	struct smc_llc_msg_add_link *addllc;
481 	struct smc_wr_tx_pend_priv *pend;
482 	struct smc_wr_buf *wr_buf;
483 	int rc;
484 
485 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
486 	if (rc)
487 		return rc;
488 	addllc = (struct smc_llc_msg_add_link *)wr_buf;
489 
490 	memset(addllc, 0, sizeof(*addllc));
491 	addllc->hd.common.type = SMC_LLC_ADD_LINK;
492 	addllc->hd.length = sizeof(struct smc_llc_msg_add_link);
493 	if (reqresp == SMC_LLC_RESP)
494 		addllc->hd.flags |= SMC_LLC_FLAG_RESP;
495 	memcpy(addllc->sender_mac, mac, ETH_ALEN);
496 	memcpy(addllc->sender_gid, gid, SMC_GID_SIZE);
497 	if (link_new) {
498 		addllc->link_num = link_new->link_id;
499 		hton24(addllc->sender_qp_num, link_new->roce_qp->qp_num);
500 		hton24(addllc->initial_psn, link_new->psn_initial);
501 		if (reqresp == SMC_LLC_REQ)
502 			addllc->qp_mtu = link_new->path_mtu;
503 		else
504 			addllc->qp_mtu = min(link_new->path_mtu,
505 					     link_new->peer_mtu);
506 	}
507 	/* send llc message */
508 	rc = smc_wr_tx_send(link, pend);
509 	return rc;
510 }
511 
512 /* send DELETE LINK request or response */
513 int smc_llc_send_delete_link(struct smc_link *link, u8 link_del_id,
514 			     enum smc_llc_reqresp reqresp, bool orderly,
515 			     u32 reason)
516 {
517 	struct smc_llc_msg_del_link *delllc;
518 	struct smc_wr_tx_pend_priv *pend;
519 	struct smc_wr_buf *wr_buf;
520 	int rc;
521 
522 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
523 	if (rc)
524 		return rc;
525 	delllc = (struct smc_llc_msg_del_link *)wr_buf;
526 
527 	memset(delllc, 0, sizeof(*delllc));
528 	delllc->hd.common.type = SMC_LLC_DELETE_LINK;
529 	delllc->hd.length = sizeof(struct smc_llc_msg_del_link);
530 	if (reqresp == SMC_LLC_RESP)
531 		delllc->hd.flags |= SMC_LLC_FLAG_RESP;
532 	if (orderly)
533 		delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
534 	if (link_del_id)
535 		delllc->link_num = link_del_id;
536 	else
537 		delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
538 	delllc->reason = htonl(reason);
539 	/* send llc message */
540 	rc = smc_wr_tx_send(link, pend);
541 	return rc;
542 }
543 
544 /* send LLC test link request */
545 static int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16])
546 {
547 	struct smc_llc_msg_test_link *testllc;
548 	struct smc_wr_tx_pend_priv *pend;
549 	struct smc_wr_buf *wr_buf;
550 	int rc;
551 
552 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
553 	if (rc)
554 		return rc;
555 	testllc = (struct smc_llc_msg_test_link *)wr_buf;
556 	memset(testllc, 0, sizeof(*testllc));
557 	testllc->hd.common.type = SMC_LLC_TEST_LINK;
558 	testllc->hd.length = sizeof(struct smc_llc_msg_test_link);
559 	memcpy(testllc->user_data, user_data, sizeof(testllc->user_data));
560 	/* send llc message */
561 	rc = smc_wr_tx_send(link, pend);
562 	return rc;
563 }
564 
565 /* schedule an llc send on link, may wait for buffers */
566 static int smc_llc_send_message(struct smc_link *link, void *llcbuf)
567 {
568 	struct smc_wr_tx_pend_priv *pend;
569 	struct smc_wr_buf *wr_buf;
570 	int rc;
571 
572 	if (!smc_link_usable(link))
573 		return -ENOLINK;
574 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
575 	if (rc)
576 		return rc;
577 	memcpy(wr_buf, llcbuf, sizeof(union smc_llc_msg));
578 	return smc_wr_tx_send(link, pend);
579 }
580 
581 /* schedule an llc send on link, may wait for buffers,
582  * and wait for send completion notification.
583  * @return 0 on success
584  */
585 static int smc_llc_send_message_wait(struct smc_link *link, void *llcbuf)
586 {
587 	struct smc_wr_tx_pend_priv *pend;
588 	struct smc_wr_buf *wr_buf;
589 	int rc;
590 
591 	if (!smc_link_usable(link))
592 		return -ENOLINK;
593 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
594 	if (rc)
595 		return rc;
596 	memcpy(wr_buf, llcbuf, sizeof(union smc_llc_msg));
597 	return smc_wr_tx_send_wait(link, pend, SMC_LLC_WAIT_TIME);
598 }
599 
600 /********************************* receive ***********************************/
601 
602 static int smc_llc_alloc_alt_link(struct smc_link_group *lgr,
603 				  enum smc_lgr_type lgr_new_t)
604 {
605 	int i;
606 
607 	if (lgr->type == SMC_LGR_SYMMETRIC ||
608 	    (lgr->type != SMC_LGR_SINGLE &&
609 	     (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
610 	      lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)))
611 		return -EMLINK;
612 
613 	if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
614 	    lgr_new_t == SMC_LGR_ASYMMETRIC_PEER) {
615 		for (i = SMC_LINKS_PER_LGR_MAX - 1; i >= 0; i--)
616 			if (lgr->lnk[i].state == SMC_LNK_UNUSED)
617 				return i;
618 	} else {
619 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
620 			if (lgr->lnk[i].state == SMC_LNK_UNUSED)
621 				return i;
622 	}
623 	return -EMLINK;
624 }
625 
626 /* return first buffer from any of the next buf lists */
627 static struct smc_buf_desc *_smc_llc_get_next_rmb(struct smc_link_group *lgr,
628 						  int *buf_lst)
629 {
630 	struct smc_buf_desc *buf_pos;
631 
632 	while (*buf_lst < SMC_RMBE_SIZES) {
633 		buf_pos = list_first_entry_or_null(&lgr->rmbs[*buf_lst],
634 						   struct smc_buf_desc, list);
635 		if (buf_pos)
636 			return buf_pos;
637 		(*buf_lst)++;
638 	}
639 	return NULL;
640 }
641 
642 /* return next rmb from buffer lists */
643 static struct smc_buf_desc *smc_llc_get_next_rmb(struct smc_link_group *lgr,
644 						 int *buf_lst,
645 						 struct smc_buf_desc *buf_pos)
646 {
647 	struct smc_buf_desc *buf_next;
648 
649 	if (!buf_pos || list_is_last(&buf_pos->list, &lgr->rmbs[*buf_lst])) {
650 		(*buf_lst)++;
651 		return _smc_llc_get_next_rmb(lgr, buf_lst);
652 	}
653 	buf_next = list_next_entry(buf_pos, list);
654 	return buf_next;
655 }
656 
657 static struct smc_buf_desc *smc_llc_get_first_rmb(struct smc_link_group *lgr,
658 						  int *buf_lst)
659 {
660 	*buf_lst = 0;
661 	return smc_llc_get_next_rmb(lgr, buf_lst, NULL);
662 }
663 
664 /* send one add_link_continue msg */
665 static int smc_llc_add_link_cont(struct smc_link *link,
666 				 struct smc_link *link_new, u8 *num_rkeys_todo,
667 				 int *buf_lst, struct smc_buf_desc **buf_pos)
668 {
669 	struct smc_llc_msg_add_link_cont *addc_llc;
670 	struct smc_link_group *lgr = link->lgr;
671 	int prim_lnk_idx, lnk_idx, i, rc;
672 	struct smc_wr_tx_pend_priv *pend;
673 	struct smc_wr_buf *wr_buf;
674 	struct smc_buf_desc *rmb;
675 	u8 n;
676 
677 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
678 	if (rc)
679 		return rc;
680 	addc_llc = (struct smc_llc_msg_add_link_cont *)wr_buf;
681 	memset(addc_llc, 0, sizeof(*addc_llc));
682 
683 	prim_lnk_idx = link->link_idx;
684 	lnk_idx = link_new->link_idx;
685 	addc_llc->link_num = link_new->link_id;
686 	addc_llc->num_rkeys = *num_rkeys_todo;
687 	n = *num_rkeys_todo;
688 	for (i = 0; i < min_t(u8, n, SMC_LLC_RKEYS_PER_CONT_MSG); i++) {
689 		if (!*buf_pos) {
690 			addc_llc->num_rkeys = addc_llc->num_rkeys -
691 					      *num_rkeys_todo;
692 			*num_rkeys_todo = 0;
693 			break;
694 		}
695 		rmb = *buf_pos;
696 
697 		addc_llc->rt[i].rmb_key = htonl(rmb->mr_rx[prim_lnk_idx]->rkey);
698 		addc_llc->rt[i].rmb_key_new = htonl(rmb->mr_rx[lnk_idx]->rkey);
699 		addc_llc->rt[i].rmb_vaddr_new =
700 			cpu_to_be64((u64)sg_dma_address(rmb->sgt[lnk_idx].sgl));
701 
702 		(*num_rkeys_todo)--;
703 		*buf_pos = smc_llc_get_next_rmb(lgr, buf_lst, *buf_pos);
704 		while (*buf_pos && !(*buf_pos)->used)
705 			*buf_pos = smc_llc_get_next_rmb(lgr, buf_lst, *buf_pos);
706 	}
707 	addc_llc->hd.common.type = SMC_LLC_ADD_LINK_CONT;
708 	addc_llc->hd.length = sizeof(struct smc_llc_msg_add_link_cont);
709 	if (lgr->role == SMC_CLNT)
710 		addc_llc->hd.flags |= SMC_LLC_FLAG_RESP;
711 	return smc_wr_tx_send(link, pend);
712 }
713 
714 static int smc_llc_cli_rkey_exchange(struct smc_link *link,
715 				     struct smc_link *link_new)
716 {
717 	struct smc_llc_msg_add_link_cont *addc_llc;
718 	struct smc_link_group *lgr = link->lgr;
719 	u8 max, num_rkeys_send, num_rkeys_recv;
720 	struct smc_llc_qentry *qentry;
721 	struct smc_buf_desc *buf_pos;
722 	int buf_lst;
723 	int rc = 0;
724 	int i;
725 
726 	mutex_lock(&lgr->rmbs_lock);
727 	num_rkeys_send = lgr->conns_num;
728 	buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
729 	do {
730 		qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_TIME,
731 				      SMC_LLC_ADD_LINK_CONT);
732 		if (!qentry) {
733 			rc = -ETIMEDOUT;
734 			break;
735 		}
736 		addc_llc = &qentry->msg.add_link_cont;
737 		num_rkeys_recv = addc_llc->num_rkeys;
738 		max = min_t(u8, num_rkeys_recv, SMC_LLC_RKEYS_PER_CONT_MSG);
739 		for (i = 0; i < max; i++) {
740 			smc_rtoken_set(lgr, link->link_idx, link_new->link_idx,
741 				       addc_llc->rt[i].rmb_key,
742 				       addc_llc->rt[i].rmb_vaddr_new,
743 				       addc_llc->rt[i].rmb_key_new);
744 			num_rkeys_recv--;
745 		}
746 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
747 		rc = smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
748 					   &buf_lst, &buf_pos);
749 		if (rc)
750 			break;
751 	} while (num_rkeys_send || num_rkeys_recv);
752 
753 	mutex_unlock(&lgr->rmbs_lock);
754 	return rc;
755 }
756 
757 /* prepare and send an add link reject response */
758 static int smc_llc_cli_add_link_reject(struct smc_llc_qentry *qentry)
759 {
760 	qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_RESP;
761 	qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_ADD_LNK_REJ;
762 	qentry->msg.raw.hdr.add_link_rej_rsn = SMC_LLC_REJ_RSN_NO_ALT_PATH;
763 	return smc_llc_send_message(qentry->link, &qentry->msg);
764 }
765 
766 static int smc_llc_cli_conf_link(struct smc_link *link,
767 				 struct smc_init_info *ini,
768 				 struct smc_link *link_new,
769 				 enum smc_lgr_type lgr_new_t)
770 {
771 	struct smc_link_group *lgr = link->lgr;
772 	struct smc_llc_qentry *qentry = NULL;
773 	int rc = 0;
774 
775 	/* receive CONFIRM LINK request over RoCE fabric */
776 	qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_FIRST_TIME, 0);
777 	if (!qentry) {
778 		rc = smc_llc_send_delete_link(link, link_new->link_id,
779 					      SMC_LLC_REQ, false,
780 					      SMC_LLC_DEL_LOST_PATH);
781 		return -ENOLINK;
782 	}
783 	if (qentry->msg.raw.hdr.common.type != SMC_LLC_CONFIRM_LINK) {
784 		/* received DELETE_LINK instead */
785 		qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_RESP;
786 		smc_llc_send_message(link, &qentry->msg);
787 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
788 		return -ENOLINK;
789 	}
790 	smc_llc_save_peer_uid(qentry);
791 	smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
792 
793 	rc = smc_ib_modify_qp_rts(link_new);
794 	if (rc) {
795 		smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
796 					 false, SMC_LLC_DEL_LOST_PATH);
797 		return -ENOLINK;
798 	}
799 	smc_wr_remember_qp_attr(link_new);
800 
801 	rc = smcr_buf_reg_lgr(link_new);
802 	if (rc) {
803 		smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
804 					 false, SMC_LLC_DEL_LOST_PATH);
805 		return -ENOLINK;
806 	}
807 
808 	/* send CONFIRM LINK response over RoCE fabric */
809 	rc = smc_llc_send_confirm_link(link_new, SMC_LLC_RESP);
810 	if (rc) {
811 		smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
812 					 false, SMC_LLC_DEL_LOST_PATH);
813 		return -ENOLINK;
814 	}
815 	smc_llc_link_active(link_new);
816 	if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
817 	    lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)
818 		smcr_lgr_set_type_asym(lgr, lgr_new_t, link_new->link_idx);
819 	else
820 		smcr_lgr_set_type(lgr, lgr_new_t);
821 	return 0;
822 }
823 
824 static void smc_llc_save_add_link_info(struct smc_link *link,
825 				       struct smc_llc_msg_add_link *add_llc)
826 {
827 	link->peer_qpn = ntoh24(add_llc->sender_qp_num);
828 	memcpy(link->peer_gid, add_llc->sender_gid, SMC_GID_SIZE);
829 	memcpy(link->peer_mac, add_llc->sender_mac, ETH_ALEN);
830 	link->peer_psn = ntoh24(add_llc->initial_psn);
831 	link->peer_mtu = add_llc->qp_mtu;
832 }
833 
834 /* as an SMC client, process an add link request */
835 int smc_llc_cli_add_link(struct smc_link *link, struct smc_llc_qentry *qentry)
836 {
837 	struct smc_llc_msg_add_link *llc = &qentry->msg.add_link;
838 	enum smc_lgr_type lgr_new_t = SMC_LGR_SYMMETRIC;
839 	struct smc_link_group *lgr = smc_get_lgr(link);
840 	struct smc_link *lnk_new = NULL;
841 	struct smc_init_info ini;
842 	int lnk_idx, rc = 0;
843 
844 	ini.vlan_id = lgr->vlan_id;
845 	smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
846 	if (!memcmp(llc->sender_gid, link->peer_gid, SMC_GID_SIZE) &&
847 	    !memcmp(llc->sender_mac, link->peer_mac, ETH_ALEN)) {
848 		if (!ini.ib_dev)
849 			goto out_reject;
850 		lgr_new_t = SMC_LGR_ASYMMETRIC_PEER;
851 	}
852 	if (!ini.ib_dev) {
853 		lgr_new_t = SMC_LGR_ASYMMETRIC_LOCAL;
854 		ini.ib_dev = link->smcibdev;
855 		ini.ib_port = link->ibport;
856 	}
857 	lnk_idx = smc_llc_alloc_alt_link(lgr, lgr_new_t);
858 	if (lnk_idx < 0)
859 		goto out_reject;
860 	lnk_new = &lgr->lnk[lnk_idx];
861 	rc = smcr_link_init(lgr, lnk_new, lnk_idx, &ini);
862 	if (rc)
863 		goto out_reject;
864 	smc_llc_save_add_link_info(lnk_new, llc);
865 	lnk_new->link_id = llc->link_num;	/* SMC server assigns link id */
866 	smc_llc_link_set_uid(lnk_new);
867 
868 	rc = smc_ib_ready_link(lnk_new);
869 	if (rc)
870 		goto out_clear_lnk;
871 
872 	rc = smcr_buf_map_lgr(lnk_new);
873 	if (rc)
874 		goto out_clear_lnk;
875 
876 	rc = smc_llc_send_add_link(link,
877 				   lnk_new->smcibdev->mac[ini.ib_port - 1],
878 				   lnk_new->gid, lnk_new, SMC_LLC_RESP);
879 	if (rc)
880 		goto out_clear_lnk;
881 	rc = smc_llc_cli_rkey_exchange(link, lnk_new);
882 	if (rc) {
883 		rc = 0;
884 		goto out_clear_lnk;
885 	}
886 	rc = smc_llc_cli_conf_link(link, &ini, lnk_new, lgr_new_t);
887 	if (!rc)
888 		goto out;
889 out_clear_lnk:
890 	smcr_link_clear(lnk_new, false);
891 out_reject:
892 	smc_llc_cli_add_link_reject(qentry);
893 out:
894 	kfree(qentry);
895 	return rc;
896 }
897 
898 /* as an SMC client, invite server to start the add_link processing */
899 static void smc_llc_cli_add_link_invite(struct smc_link *link,
900 					struct smc_llc_qentry *qentry)
901 {
902 	struct smc_link_group *lgr = smc_get_lgr(link);
903 	struct smc_init_info ini;
904 
905 	if (lgr->type == SMC_LGR_SYMMETRIC ||
906 	    lgr->type == SMC_LGR_ASYMMETRIC_PEER)
907 		goto out;
908 
909 	ini.vlan_id = lgr->vlan_id;
910 	smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
911 	if (!ini.ib_dev)
912 		goto out;
913 
914 	smc_llc_send_add_link(link, ini.ib_dev->mac[ini.ib_port - 1],
915 			      ini.ib_gid, NULL, SMC_LLC_REQ);
916 out:
917 	kfree(qentry);
918 }
919 
920 static bool smc_llc_is_local_add_link(union smc_llc_msg *llc)
921 {
922 	if (llc->raw.hdr.common.type == SMC_LLC_ADD_LINK &&
923 	    !llc->add_link.qp_mtu && !llc->add_link.link_num)
924 		return true;
925 	return false;
926 }
927 
928 static void smc_llc_process_cli_add_link(struct smc_link_group *lgr)
929 {
930 	struct smc_llc_qentry *qentry;
931 
932 	qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
933 
934 	mutex_lock(&lgr->llc_conf_mutex);
935 	if (smc_llc_is_local_add_link(&qentry->msg))
936 		smc_llc_cli_add_link_invite(qentry->link, qentry);
937 	else
938 		smc_llc_cli_add_link(qentry->link, qentry);
939 	mutex_unlock(&lgr->llc_conf_mutex);
940 }
941 
942 static int smc_llc_active_link_count(struct smc_link_group *lgr)
943 {
944 	int i, link_count = 0;
945 
946 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
947 		if (!smc_link_active(&lgr->lnk[i]))
948 			continue;
949 		link_count++;
950 	}
951 	return link_count;
952 }
953 
954 /* find the asymmetric link when 3 links are established  */
955 static struct smc_link *smc_llc_find_asym_link(struct smc_link_group *lgr)
956 {
957 	int asym_idx = -ENOENT;
958 	int i, j, k;
959 	bool found;
960 
961 	/* determine asymmetric link */
962 	found = false;
963 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
964 		for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
965 			if (!smc_link_usable(&lgr->lnk[i]) ||
966 			    !smc_link_usable(&lgr->lnk[j]))
967 				continue;
968 			if (!memcmp(lgr->lnk[i].gid, lgr->lnk[j].gid,
969 				    SMC_GID_SIZE)) {
970 				found = true;	/* asym_lnk is i or j */
971 				break;
972 			}
973 		}
974 		if (found)
975 			break;
976 	}
977 	if (!found)
978 		goto out; /* no asymmetric link */
979 	for (k = 0; k < SMC_LINKS_PER_LGR_MAX; k++) {
980 		if (!smc_link_usable(&lgr->lnk[k]))
981 			continue;
982 		if (k != i &&
983 		    !memcmp(lgr->lnk[i].peer_gid, lgr->lnk[k].peer_gid,
984 			    SMC_GID_SIZE)) {
985 			asym_idx = i;
986 			break;
987 		}
988 		if (k != j &&
989 		    !memcmp(lgr->lnk[j].peer_gid, lgr->lnk[k].peer_gid,
990 			    SMC_GID_SIZE)) {
991 			asym_idx = j;
992 			break;
993 		}
994 	}
995 out:
996 	return (asym_idx < 0) ? NULL : &lgr->lnk[asym_idx];
997 }
998 
999 static void smc_llc_delete_asym_link(struct smc_link_group *lgr)
1000 {
1001 	struct smc_link *lnk_new = NULL, *lnk_asym;
1002 	struct smc_llc_qentry *qentry;
1003 	int rc;
1004 
1005 	lnk_asym = smc_llc_find_asym_link(lgr);
1006 	if (!lnk_asym)
1007 		return; /* no asymmetric link */
1008 	if (!smc_link_downing(&lnk_asym->state))
1009 		return;
1010 	lnk_new = smc_switch_conns(lgr, lnk_asym, false);
1011 	smc_wr_tx_wait_no_pending_sends(lnk_asym);
1012 	if (!lnk_new)
1013 		goto out_free;
1014 	/* change flow type from ADD_LINK into DEL_LINK */
1015 	lgr->llc_flow_lcl.type = SMC_LLC_FLOW_DEL_LINK;
1016 	rc = smc_llc_send_delete_link(lnk_new, lnk_asym->link_id, SMC_LLC_REQ,
1017 				      true, SMC_LLC_DEL_NO_ASYM_NEEDED);
1018 	if (rc) {
1019 		smcr_link_down_cond(lnk_new);
1020 		goto out_free;
1021 	}
1022 	qentry = smc_llc_wait(lgr, lnk_new, SMC_LLC_WAIT_TIME,
1023 			      SMC_LLC_DELETE_LINK);
1024 	if (!qentry) {
1025 		smcr_link_down_cond(lnk_new);
1026 		goto out_free;
1027 	}
1028 	smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1029 out_free:
1030 	smcr_link_clear(lnk_asym, true);
1031 }
1032 
1033 static int smc_llc_srv_rkey_exchange(struct smc_link *link,
1034 				     struct smc_link *link_new)
1035 {
1036 	struct smc_llc_msg_add_link_cont *addc_llc;
1037 	struct smc_link_group *lgr = link->lgr;
1038 	u8 max, num_rkeys_send, num_rkeys_recv;
1039 	struct smc_llc_qentry *qentry = NULL;
1040 	struct smc_buf_desc *buf_pos;
1041 	int buf_lst;
1042 	int rc = 0;
1043 	int i;
1044 
1045 	mutex_lock(&lgr->rmbs_lock);
1046 	num_rkeys_send = lgr->conns_num;
1047 	buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
1048 	do {
1049 		smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
1050 				      &buf_lst, &buf_pos);
1051 		qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME,
1052 				      SMC_LLC_ADD_LINK_CONT);
1053 		if (!qentry) {
1054 			rc = -ETIMEDOUT;
1055 			goto out;
1056 		}
1057 		addc_llc = &qentry->msg.add_link_cont;
1058 		num_rkeys_recv = addc_llc->num_rkeys;
1059 		max = min_t(u8, num_rkeys_recv, SMC_LLC_RKEYS_PER_CONT_MSG);
1060 		for (i = 0; i < max; i++) {
1061 			smc_rtoken_set(lgr, link->link_idx, link_new->link_idx,
1062 				       addc_llc->rt[i].rmb_key,
1063 				       addc_llc->rt[i].rmb_vaddr_new,
1064 				       addc_llc->rt[i].rmb_key_new);
1065 			num_rkeys_recv--;
1066 		}
1067 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1068 	} while (num_rkeys_send || num_rkeys_recv);
1069 out:
1070 	mutex_unlock(&lgr->rmbs_lock);
1071 	return rc;
1072 }
1073 
1074 static int smc_llc_srv_conf_link(struct smc_link *link,
1075 				 struct smc_link *link_new,
1076 				 enum smc_lgr_type lgr_new_t)
1077 {
1078 	struct smc_link_group *lgr = link->lgr;
1079 	struct smc_llc_qentry *qentry = NULL;
1080 	int rc;
1081 
1082 	/* send CONFIRM LINK request over the RoCE fabric */
1083 	rc = smc_llc_send_confirm_link(link_new, SMC_LLC_REQ);
1084 	if (rc)
1085 		return -ENOLINK;
1086 	/* receive CONFIRM LINK response over the RoCE fabric */
1087 	qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_FIRST_TIME, 0);
1088 	if (!qentry ||
1089 	    qentry->msg.raw.hdr.common.type != SMC_LLC_CONFIRM_LINK) {
1090 		/* send DELETE LINK */
1091 		smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
1092 					 false, SMC_LLC_DEL_LOST_PATH);
1093 		if (qentry)
1094 			smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1095 		return -ENOLINK;
1096 	}
1097 	smc_llc_save_peer_uid(qentry);
1098 	smc_llc_link_active(link_new);
1099 	if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
1100 	    lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)
1101 		smcr_lgr_set_type_asym(lgr, lgr_new_t, link_new->link_idx);
1102 	else
1103 		smcr_lgr_set_type(lgr, lgr_new_t);
1104 	smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1105 	return 0;
1106 }
1107 
1108 int smc_llc_srv_add_link(struct smc_link *link)
1109 {
1110 	enum smc_lgr_type lgr_new_t = SMC_LGR_SYMMETRIC;
1111 	struct smc_link_group *lgr = link->lgr;
1112 	struct smc_llc_msg_add_link *add_llc;
1113 	struct smc_llc_qentry *qentry = NULL;
1114 	struct smc_link *link_new;
1115 	struct smc_init_info ini;
1116 	int lnk_idx, rc = 0;
1117 
1118 	/* ignore client add link recommendation, start new flow */
1119 	ini.vlan_id = lgr->vlan_id;
1120 	smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
1121 	if (!ini.ib_dev) {
1122 		lgr_new_t = SMC_LGR_ASYMMETRIC_LOCAL;
1123 		ini.ib_dev = link->smcibdev;
1124 		ini.ib_port = link->ibport;
1125 	}
1126 	lnk_idx = smc_llc_alloc_alt_link(lgr, lgr_new_t);
1127 	if (lnk_idx < 0)
1128 		return 0;
1129 
1130 	rc = smcr_link_init(lgr, &lgr->lnk[lnk_idx], lnk_idx, &ini);
1131 	if (rc)
1132 		return rc;
1133 	link_new = &lgr->lnk[lnk_idx];
1134 	rc = smc_llc_send_add_link(link,
1135 				   link_new->smcibdev->mac[ini.ib_port - 1],
1136 				   link_new->gid, link_new, SMC_LLC_REQ);
1137 	if (rc)
1138 		goto out_err;
1139 	/* receive ADD LINK response over the RoCE fabric */
1140 	qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME, SMC_LLC_ADD_LINK);
1141 	if (!qentry) {
1142 		rc = -ETIMEDOUT;
1143 		goto out_err;
1144 	}
1145 	add_llc = &qentry->msg.add_link;
1146 	if (add_llc->hd.flags & SMC_LLC_FLAG_ADD_LNK_REJ) {
1147 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1148 		rc = -ENOLINK;
1149 		goto out_err;
1150 	}
1151 	if (lgr->type == SMC_LGR_SINGLE &&
1152 	    (!memcmp(add_llc->sender_gid, link->peer_gid, SMC_GID_SIZE) &&
1153 	     !memcmp(add_llc->sender_mac, link->peer_mac, ETH_ALEN))) {
1154 		lgr_new_t = SMC_LGR_ASYMMETRIC_PEER;
1155 	}
1156 	smc_llc_save_add_link_info(link_new, add_llc);
1157 	smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1158 
1159 	rc = smc_ib_ready_link(link_new);
1160 	if (rc)
1161 		goto out_err;
1162 	rc = smcr_buf_map_lgr(link_new);
1163 	if (rc)
1164 		goto out_err;
1165 	rc = smcr_buf_reg_lgr(link_new);
1166 	if (rc)
1167 		goto out_err;
1168 	rc = smc_llc_srv_rkey_exchange(link, link_new);
1169 	if (rc)
1170 		goto out_err;
1171 	rc = smc_llc_srv_conf_link(link, link_new, lgr_new_t);
1172 	if (rc)
1173 		goto out_err;
1174 	return 0;
1175 out_err:
1176 	smcr_link_clear(link_new, false);
1177 	return rc;
1178 }
1179 
1180 static void smc_llc_process_srv_add_link(struct smc_link_group *lgr)
1181 {
1182 	struct smc_link *link = lgr->llc_flow_lcl.qentry->link;
1183 	int rc;
1184 
1185 	smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1186 
1187 	mutex_lock(&lgr->llc_conf_mutex);
1188 	rc = smc_llc_srv_add_link(link);
1189 	if (!rc && lgr->type == SMC_LGR_SYMMETRIC) {
1190 		/* delete any asymmetric link */
1191 		smc_llc_delete_asym_link(lgr);
1192 	}
1193 	mutex_unlock(&lgr->llc_conf_mutex);
1194 }
1195 
1196 /* enqueue a local add_link req to trigger a new add_link flow */
1197 void smc_llc_add_link_local(struct smc_link *link)
1198 {
1199 	struct smc_llc_msg_add_link add_llc = {0};
1200 
1201 	add_llc.hd.length = sizeof(add_llc);
1202 	add_llc.hd.common.type = SMC_LLC_ADD_LINK;
1203 	/* no dev and port needed */
1204 	smc_llc_enqueue(link, (union smc_llc_msg *)&add_llc);
1205 }
1206 
1207 /* worker to process an add link message */
1208 static void smc_llc_add_link_work(struct work_struct *work)
1209 {
1210 	struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1211 						  llc_add_link_work);
1212 
1213 	if (list_empty(&lgr->list)) {
1214 		/* link group is terminating */
1215 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1216 		goto out;
1217 	}
1218 
1219 	if (lgr->role == SMC_CLNT)
1220 		smc_llc_process_cli_add_link(lgr);
1221 	else
1222 		smc_llc_process_srv_add_link(lgr);
1223 out:
1224 	smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1225 }
1226 
1227 /* enqueue a local del_link msg to trigger a new del_link flow,
1228  * called only for role SMC_SERV
1229  */
1230 void smc_llc_srv_delete_link_local(struct smc_link *link, u8 del_link_id)
1231 {
1232 	struct smc_llc_msg_del_link del_llc = {0};
1233 
1234 	del_llc.hd.length = sizeof(del_llc);
1235 	del_llc.hd.common.type = SMC_LLC_DELETE_LINK;
1236 	del_llc.link_num = del_link_id;
1237 	del_llc.reason = htonl(SMC_LLC_DEL_LOST_PATH);
1238 	del_llc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
1239 	smc_llc_enqueue(link, (union smc_llc_msg *)&del_llc);
1240 }
1241 
1242 static void smc_llc_process_cli_delete_link(struct smc_link_group *lgr)
1243 {
1244 	struct smc_link *lnk_del = NULL, *lnk_asym, *lnk;
1245 	struct smc_llc_msg_del_link *del_llc;
1246 	struct smc_llc_qentry *qentry;
1247 	int active_links;
1248 	int lnk_idx;
1249 
1250 	qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
1251 	lnk = qentry->link;
1252 	del_llc = &qentry->msg.delete_link;
1253 
1254 	if (del_llc->hd.flags & SMC_LLC_FLAG_DEL_LINK_ALL) {
1255 		smc_lgr_terminate_sched(lgr);
1256 		goto out;
1257 	}
1258 	mutex_lock(&lgr->llc_conf_mutex);
1259 	/* delete single link */
1260 	for (lnk_idx = 0; lnk_idx < SMC_LINKS_PER_LGR_MAX; lnk_idx++) {
1261 		if (lgr->lnk[lnk_idx].link_id != del_llc->link_num)
1262 			continue;
1263 		lnk_del = &lgr->lnk[lnk_idx];
1264 		break;
1265 	}
1266 	del_llc->hd.flags |= SMC_LLC_FLAG_RESP;
1267 	if (!lnk_del) {
1268 		/* link was not found */
1269 		del_llc->reason = htonl(SMC_LLC_DEL_NOLNK);
1270 		smc_llc_send_message(lnk, &qentry->msg);
1271 		goto out_unlock;
1272 	}
1273 	lnk_asym = smc_llc_find_asym_link(lgr);
1274 
1275 	del_llc->reason = 0;
1276 	smc_llc_send_message(lnk, &qentry->msg); /* response */
1277 
1278 	if (smc_link_downing(&lnk_del->state)) {
1279 		if (smc_switch_conns(lgr, lnk_del, false))
1280 			smc_wr_tx_wait_no_pending_sends(lnk_del);
1281 	}
1282 	smcr_link_clear(lnk_del, true);
1283 
1284 	active_links = smc_llc_active_link_count(lgr);
1285 	if (lnk_del == lnk_asym) {
1286 		/* expected deletion of asym link, don't change lgr state */
1287 	} else if (active_links == 1) {
1288 		smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1289 	} else if (!active_links) {
1290 		smcr_lgr_set_type(lgr, SMC_LGR_NONE);
1291 		smc_lgr_terminate_sched(lgr);
1292 	}
1293 out_unlock:
1294 	mutex_unlock(&lgr->llc_conf_mutex);
1295 out:
1296 	kfree(qentry);
1297 }
1298 
1299 /* try to send a DELETE LINK ALL request on any active link,
1300  * waiting for send completion
1301  */
1302 void smc_llc_send_link_delete_all(struct smc_link_group *lgr, bool ord, u32 rsn)
1303 {
1304 	struct smc_llc_msg_del_link delllc = {0};
1305 	int i;
1306 
1307 	delllc.hd.common.type = SMC_LLC_DELETE_LINK;
1308 	delllc.hd.length = sizeof(delllc);
1309 	if (ord)
1310 		delllc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
1311 	delllc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
1312 	delllc.reason = htonl(rsn);
1313 
1314 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1315 		if (!smc_link_usable(&lgr->lnk[i]))
1316 			continue;
1317 		if (!smc_llc_send_message_wait(&lgr->lnk[i], &delllc))
1318 			break;
1319 	}
1320 }
1321 
1322 static void smc_llc_process_srv_delete_link(struct smc_link_group *lgr)
1323 {
1324 	struct smc_llc_msg_del_link *del_llc;
1325 	struct smc_link *lnk, *lnk_del;
1326 	struct smc_llc_qentry *qentry;
1327 	int active_links;
1328 	int i;
1329 
1330 	mutex_lock(&lgr->llc_conf_mutex);
1331 	qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
1332 	lnk = qentry->link;
1333 	del_llc = &qentry->msg.delete_link;
1334 
1335 	if (qentry->msg.delete_link.hd.flags & SMC_LLC_FLAG_DEL_LINK_ALL) {
1336 		/* delete entire lgr */
1337 		smc_llc_send_link_delete_all(lgr, true, ntohl(
1338 					      qentry->msg.delete_link.reason));
1339 		smc_lgr_terminate_sched(lgr);
1340 		goto out;
1341 	}
1342 	/* delete single link */
1343 	lnk_del = NULL;
1344 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1345 		if (lgr->lnk[i].link_id == del_llc->link_num) {
1346 			lnk_del = &lgr->lnk[i];
1347 			break;
1348 		}
1349 	}
1350 	if (!lnk_del)
1351 		goto out; /* asymmetric link already deleted */
1352 
1353 	if (smc_link_downing(&lnk_del->state)) {
1354 		if (smc_switch_conns(lgr, lnk_del, false))
1355 			smc_wr_tx_wait_no_pending_sends(lnk_del);
1356 	}
1357 	if (!list_empty(&lgr->list)) {
1358 		/* qentry is either a request from peer (send it back to
1359 		 * initiate the DELETE_LINK processing), or a locally
1360 		 * enqueued DELETE_LINK request (forward it)
1361 		 */
1362 		if (!smc_llc_send_message(lnk, &qentry->msg)) {
1363 			struct smc_llc_qentry *qentry2;
1364 
1365 			qentry2 = smc_llc_wait(lgr, lnk, SMC_LLC_WAIT_TIME,
1366 					       SMC_LLC_DELETE_LINK);
1367 			if (qentry2)
1368 				smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1369 		}
1370 	}
1371 	smcr_link_clear(lnk_del, true);
1372 
1373 	active_links = smc_llc_active_link_count(lgr);
1374 	if (active_links == 1) {
1375 		smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1376 	} else if (!active_links) {
1377 		smcr_lgr_set_type(lgr, SMC_LGR_NONE);
1378 		smc_lgr_terminate_sched(lgr);
1379 	}
1380 
1381 	if (lgr->type == SMC_LGR_SINGLE && !list_empty(&lgr->list)) {
1382 		/* trigger setup of asymm alt link */
1383 		smc_llc_add_link_local(lnk);
1384 	}
1385 out:
1386 	mutex_unlock(&lgr->llc_conf_mutex);
1387 	kfree(qentry);
1388 }
1389 
1390 static void smc_llc_delete_link_work(struct work_struct *work)
1391 {
1392 	struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1393 						  llc_del_link_work);
1394 
1395 	if (list_empty(&lgr->list)) {
1396 		/* link group is terminating */
1397 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1398 		goto out;
1399 	}
1400 
1401 	if (lgr->role == SMC_CLNT)
1402 		smc_llc_process_cli_delete_link(lgr);
1403 	else
1404 		smc_llc_process_srv_delete_link(lgr);
1405 out:
1406 	smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1407 }
1408 
1409 /* process a confirm_rkey request from peer, remote flow */
1410 static void smc_llc_rmt_conf_rkey(struct smc_link_group *lgr)
1411 {
1412 	struct smc_llc_msg_confirm_rkey *llc;
1413 	struct smc_llc_qentry *qentry;
1414 	struct smc_link *link;
1415 	int num_entries;
1416 	int rk_idx;
1417 	int i;
1418 
1419 	qentry = lgr->llc_flow_rmt.qentry;
1420 	llc = &qentry->msg.confirm_rkey;
1421 	link = qentry->link;
1422 
1423 	num_entries = llc->rtoken[0].num_rkeys;
1424 	/* first rkey entry is for receiving link */
1425 	rk_idx = smc_rtoken_add(link,
1426 				llc->rtoken[0].rmb_vaddr,
1427 				llc->rtoken[0].rmb_key);
1428 	if (rk_idx < 0)
1429 		goto out_err;
1430 
1431 	for (i = 1; i <= min_t(u8, num_entries, SMC_LLC_RKEYS_PER_MSG - 1); i++)
1432 		smc_rtoken_set2(lgr, rk_idx, llc->rtoken[i].link_id,
1433 				llc->rtoken[i].rmb_vaddr,
1434 				llc->rtoken[i].rmb_key);
1435 	/* max links is 3 so there is no need to support conf_rkey_cont msgs */
1436 	goto out;
1437 out_err:
1438 	llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
1439 	llc->hd.flags |= SMC_LLC_FLAG_RKEY_RETRY;
1440 out:
1441 	llc->hd.flags |= SMC_LLC_FLAG_RESP;
1442 	smc_llc_send_message(link, &qentry->msg);
1443 	smc_llc_flow_qentry_del(&lgr->llc_flow_rmt);
1444 }
1445 
1446 /* process a delete_rkey request from peer, remote flow */
1447 static void smc_llc_rmt_delete_rkey(struct smc_link_group *lgr)
1448 {
1449 	struct smc_llc_msg_delete_rkey *llc;
1450 	struct smc_llc_qentry *qentry;
1451 	struct smc_link *link;
1452 	u8 err_mask = 0;
1453 	int i, max;
1454 
1455 	qentry = lgr->llc_flow_rmt.qentry;
1456 	llc = &qentry->msg.delete_rkey;
1457 	link = qentry->link;
1458 
1459 	max = min_t(u8, llc->num_rkeys, SMC_LLC_DEL_RKEY_MAX);
1460 	for (i = 0; i < max; i++) {
1461 		if (smc_rtoken_delete(link, llc->rkey[i]))
1462 			err_mask |= 1 << (SMC_LLC_DEL_RKEY_MAX - 1 - i);
1463 	}
1464 	if (err_mask) {
1465 		llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
1466 		llc->err_mask = err_mask;
1467 	}
1468 	llc->hd.flags |= SMC_LLC_FLAG_RESP;
1469 	smc_llc_send_message(link, &qentry->msg);
1470 	smc_llc_flow_qentry_del(&lgr->llc_flow_rmt);
1471 }
1472 
1473 static void smc_llc_protocol_violation(struct smc_link_group *lgr, u8 type)
1474 {
1475 	pr_warn_ratelimited("smc: SMC-R lg %*phN LLC protocol violation: "
1476 			    "llc_type %d\n", SMC_LGR_ID_SIZE, &lgr->id, type);
1477 	smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_PROT_VIOL);
1478 	smc_lgr_terminate_sched(lgr);
1479 }
1480 
1481 /* flush the llc event queue */
1482 static void smc_llc_event_flush(struct smc_link_group *lgr)
1483 {
1484 	struct smc_llc_qentry *qentry, *q;
1485 
1486 	spin_lock_bh(&lgr->llc_event_q_lock);
1487 	list_for_each_entry_safe(qentry, q, &lgr->llc_event_q, list) {
1488 		list_del_init(&qentry->list);
1489 		kfree(qentry);
1490 	}
1491 	spin_unlock_bh(&lgr->llc_event_q_lock);
1492 }
1493 
1494 static void smc_llc_event_handler(struct smc_llc_qentry *qentry)
1495 {
1496 	union smc_llc_msg *llc = &qentry->msg;
1497 	struct smc_link *link = qentry->link;
1498 	struct smc_link_group *lgr = link->lgr;
1499 
1500 	if (!smc_link_usable(link))
1501 		goto out;
1502 
1503 	switch (llc->raw.hdr.common.type) {
1504 	case SMC_LLC_TEST_LINK:
1505 		llc->test_link.hd.flags |= SMC_LLC_FLAG_RESP;
1506 		smc_llc_send_message(link, llc);
1507 		break;
1508 	case SMC_LLC_ADD_LINK:
1509 		if (list_empty(&lgr->list))
1510 			goto out;	/* lgr is terminating */
1511 		if (lgr->role == SMC_CLNT) {
1512 			if (smc_llc_is_local_add_link(llc)) {
1513 				if (lgr->llc_flow_lcl.type ==
1514 				    SMC_LLC_FLOW_ADD_LINK)
1515 					break;	/* add_link in progress */
1516 				if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1517 						       qentry)) {
1518 					schedule_work(&lgr->llc_add_link_work);
1519 				}
1520 				return;
1521 			}
1522 			if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_ADD_LINK &&
1523 			    !lgr->llc_flow_lcl.qentry) {
1524 				/* a flow is waiting for this message */
1525 				smc_llc_flow_qentry_set(&lgr->llc_flow_lcl,
1526 							qentry);
1527 				wake_up(&lgr->llc_msg_waiter);
1528 			} else if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1529 						      qentry)) {
1530 				schedule_work(&lgr->llc_add_link_work);
1531 			}
1532 		} else if (smc_llc_flow_start(&lgr->llc_flow_lcl, qentry)) {
1533 			/* as smc server, handle client suggestion */
1534 			schedule_work(&lgr->llc_add_link_work);
1535 		}
1536 		return;
1537 	case SMC_LLC_CONFIRM_LINK:
1538 	case SMC_LLC_ADD_LINK_CONT:
1539 		if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1540 			/* a flow is waiting for this message */
1541 			smc_llc_flow_qentry_set(&lgr->llc_flow_lcl, qentry);
1542 			wake_up(&lgr->llc_msg_waiter);
1543 			return;
1544 		}
1545 		break;
1546 	case SMC_LLC_DELETE_LINK:
1547 		if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_ADD_LINK &&
1548 		    !lgr->llc_flow_lcl.qentry) {
1549 			/* DEL LINK REQ during ADD LINK SEQ */
1550 			smc_llc_flow_qentry_set(&lgr->llc_flow_lcl, qentry);
1551 			wake_up(&lgr->llc_msg_waiter);
1552 		} else if (smc_llc_flow_start(&lgr->llc_flow_lcl, qentry)) {
1553 			schedule_work(&lgr->llc_del_link_work);
1554 		}
1555 		return;
1556 	case SMC_LLC_CONFIRM_RKEY:
1557 		/* new request from remote, assign to remote flow */
1558 		if (smc_llc_flow_start(&lgr->llc_flow_rmt, qentry)) {
1559 			/* process here, does not wait for more llc msgs */
1560 			smc_llc_rmt_conf_rkey(lgr);
1561 			smc_llc_flow_stop(lgr, &lgr->llc_flow_rmt);
1562 		}
1563 		return;
1564 	case SMC_LLC_CONFIRM_RKEY_CONT:
1565 		/* not used because max links is 3, and 3 rkeys fit into
1566 		 * one CONFIRM_RKEY message
1567 		 */
1568 		break;
1569 	case SMC_LLC_DELETE_RKEY:
1570 		/* new request from remote, assign to remote flow */
1571 		if (smc_llc_flow_start(&lgr->llc_flow_rmt, qentry)) {
1572 			/* process here, does not wait for more llc msgs */
1573 			smc_llc_rmt_delete_rkey(lgr);
1574 			smc_llc_flow_stop(lgr, &lgr->llc_flow_rmt);
1575 		}
1576 		return;
1577 	default:
1578 		smc_llc_protocol_violation(lgr, llc->raw.hdr.common.type);
1579 		break;
1580 	}
1581 out:
1582 	kfree(qentry);
1583 }
1584 
1585 /* worker to process llc messages on the event queue */
1586 static void smc_llc_event_work(struct work_struct *work)
1587 {
1588 	struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1589 						  llc_event_work);
1590 	struct smc_llc_qentry *qentry;
1591 
1592 	if (!lgr->llc_flow_lcl.type && lgr->delayed_event) {
1593 		if (smc_link_usable(lgr->delayed_event->link)) {
1594 			smc_llc_event_handler(lgr->delayed_event);
1595 		} else {
1596 			qentry = lgr->delayed_event;
1597 			lgr->delayed_event = NULL;
1598 			kfree(qentry);
1599 		}
1600 	}
1601 
1602 again:
1603 	spin_lock_bh(&lgr->llc_event_q_lock);
1604 	if (!list_empty(&lgr->llc_event_q)) {
1605 		qentry = list_first_entry(&lgr->llc_event_q,
1606 					  struct smc_llc_qentry, list);
1607 		list_del_init(&qentry->list);
1608 		spin_unlock_bh(&lgr->llc_event_q_lock);
1609 		smc_llc_event_handler(qentry);
1610 		goto again;
1611 	}
1612 	spin_unlock_bh(&lgr->llc_event_q_lock);
1613 }
1614 
1615 /* process llc responses in tasklet context */
1616 static void smc_llc_rx_response(struct smc_link *link,
1617 				struct smc_llc_qentry *qentry)
1618 {
1619 	enum smc_llc_flowtype flowtype = link->lgr->llc_flow_lcl.type;
1620 	struct smc_llc_flow *flow = &link->lgr->llc_flow_lcl;
1621 	u8 llc_type = qentry->msg.raw.hdr.common.type;
1622 
1623 	switch (llc_type) {
1624 	case SMC_LLC_TEST_LINK:
1625 		if (smc_link_active(link))
1626 			complete(&link->llc_testlink_resp);
1627 		break;
1628 	case SMC_LLC_ADD_LINK:
1629 	case SMC_LLC_ADD_LINK_CONT:
1630 	case SMC_LLC_CONFIRM_LINK:
1631 		if (flowtype != SMC_LLC_FLOW_ADD_LINK || flow->qentry)
1632 			break;	/* drop out-of-flow response */
1633 		goto assign;
1634 	case SMC_LLC_DELETE_LINK:
1635 		if (flowtype != SMC_LLC_FLOW_DEL_LINK || flow->qentry)
1636 			break;	/* drop out-of-flow response */
1637 		goto assign;
1638 	case SMC_LLC_CONFIRM_RKEY:
1639 	case SMC_LLC_DELETE_RKEY:
1640 		if (flowtype != SMC_LLC_FLOW_RKEY || flow->qentry)
1641 			break;	/* drop out-of-flow response */
1642 		goto assign;
1643 	case SMC_LLC_CONFIRM_RKEY_CONT:
1644 		/* not used because max links is 3 */
1645 		break;
1646 	default:
1647 		smc_llc_protocol_violation(link->lgr, llc_type);
1648 		break;
1649 	}
1650 	kfree(qentry);
1651 	return;
1652 assign:
1653 	/* assign responses to the local flow, we requested them */
1654 	smc_llc_flow_qentry_set(&link->lgr->llc_flow_lcl, qentry);
1655 	wake_up(&link->lgr->llc_msg_waiter);
1656 }
1657 
1658 static void smc_llc_enqueue(struct smc_link *link, union smc_llc_msg *llc)
1659 {
1660 	struct smc_link_group *lgr = link->lgr;
1661 	struct smc_llc_qentry *qentry;
1662 	unsigned long flags;
1663 
1664 	qentry = kmalloc(sizeof(*qentry), GFP_ATOMIC);
1665 	if (!qentry)
1666 		return;
1667 	qentry->link = link;
1668 	INIT_LIST_HEAD(&qentry->list);
1669 	memcpy(&qentry->msg, llc, sizeof(union smc_llc_msg));
1670 
1671 	/* process responses immediately */
1672 	if (llc->raw.hdr.flags & SMC_LLC_FLAG_RESP) {
1673 		smc_llc_rx_response(link, qentry);
1674 		return;
1675 	}
1676 
1677 	/* add requests to event queue */
1678 	spin_lock_irqsave(&lgr->llc_event_q_lock, flags);
1679 	list_add_tail(&qentry->list, &lgr->llc_event_q);
1680 	spin_unlock_irqrestore(&lgr->llc_event_q_lock, flags);
1681 	schedule_work(&lgr->llc_event_work);
1682 }
1683 
1684 /* copy received msg and add it to the event queue */
1685 static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
1686 {
1687 	struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
1688 	union smc_llc_msg *llc = buf;
1689 
1690 	if (wc->byte_len < sizeof(*llc))
1691 		return; /* short message */
1692 	if (llc->raw.hdr.length != sizeof(*llc))
1693 		return; /* invalid message */
1694 
1695 	smc_llc_enqueue(link, llc);
1696 }
1697 
1698 /***************************** worker, utils *********************************/
1699 
1700 static void smc_llc_testlink_work(struct work_struct *work)
1701 {
1702 	struct smc_link *link = container_of(to_delayed_work(work),
1703 					     struct smc_link, llc_testlink_wrk);
1704 	unsigned long next_interval;
1705 	unsigned long expire_time;
1706 	u8 user_data[16] = { 0 };
1707 	int rc;
1708 
1709 	if (!smc_link_active(link))
1710 		return;		/* don't reschedule worker */
1711 	expire_time = link->wr_rx_tstamp + link->llc_testlink_time;
1712 	if (time_is_after_jiffies(expire_time)) {
1713 		next_interval = expire_time - jiffies;
1714 		goto out;
1715 	}
1716 	reinit_completion(&link->llc_testlink_resp);
1717 	smc_llc_send_test_link(link, user_data);
1718 	/* receive TEST LINK response over RoCE fabric */
1719 	rc = wait_for_completion_interruptible_timeout(&link->llc_testlink_resp,
1720 						       SMC_LLC_WAIT_TIME);
1721 	if (!smc_link_active(link))
1722 		return;		/* link state changed */
1723 	if (rc <= 0) {
1724 		smcr_link_down_cond_sched(link);
1725 		return;
1726 	}
1727 	next_interval = link->llc_testlink_time;
1728 out:
1729 	schedule_delayed_work(&link->llc_testlink_wrk, next_interval);
1730 }
1731 
1732 void smc_llc_lgr_init(struct smc_link_group *lgr, struct smc_sock *smc)
1733 {
1734 	struct net *net = sock_net(smc->clcsock->sk);
1735 
1736 	INIT_WORK(&lgr->llc_event_work, smc_llc_event_work);
1737 	INIT_WORK(&lgr->llc_add_link_work, smc_llc_add_link_work);
1738 	INIT_WORK(&lgr->llc_del_link_work, smc_llc_delete_link_work);
1739 	INIT_LIST_HEAD(&lgr->llc_event_q);
1740 	spin_lock_init(&lgr->llc_event_q_lock);
1741 	spin_lock_init(&lgr->llc_flow_lock);
1742 	init_waitqueue_head(&lgr->llc_flow_waiter);
1743 	init_waitqueue_head(&lgr->llc_msg_waiter);
1744 	mutex_init(&lgr->llc_conf_mutex);
1745 	lgr->llc_testlink_time = net->ipv4.sysctl_tcp_keepalive_time;
1746 }
1747 
1748 /* called after lgr was removed from lgr_list */
1749 void smc_llc_lgr_clear(struct smc_link_group *lgr)
1750 {
1751 	smc_llc_event_flush(lgr);
1752 	wake_up_all(&lgr->llc_flow_waiter);
1753 	wake_up_all(&lgr->llc_msg_waiter);
1754 	cancel_work_sync(&lgr->llc_event_work);
1755 	cancel_work_sync(&lgr->llc_add_link_work);
1756 	cancel_work_sync(&lgr->llc_del_link_work);
1757 	if (lgr->delayed_event) {
1758 		kfree(lgr->delayed_event);
1759 		lgr->delayed_event = NULL;
1760 	}
1761 }
1762 
1763 int smc_llc_link_init(struct smc_link *link)
1764 {
1765 	init_completion(&link->llc_testlink_resp);
1766 	INIT_DELAYED_WORK(&link->llc_testlink_wrk, smc_llc_testlink_work);
1767 	return 0;
1768 }
1769 
1770 void smc_llc_link_active(struct smc_link *link)
1771 {
1772 	pr_warn_ratelimited("smc: SMC-R lg %*phN link added: id %*phN, "
1773 			    "peerid %*phN, ibdev %s, ibport %d\n",
1774 			    SMC_LGR_ID_SIZE, &link->lgr->id,
1775 			    SMC_LGR_ID_SIZE, &link->link_uid,
1776 			    SMC_LGR_ID_SIZE, &link->peer_link_uid,
1777 			    link->smcibdev->ibdev->name, link->ibport);
1778 	link->state = SMC_LNK_ACTIVE;
1779 	if (link->lgr->llc_testlink_time) {
1780 		link->llc_testlink_time = link->lgr->llc_testlink_time * HZ;
1781 		schedule_delayed_work(&link->llc_testlink_wrk,
1782 				      link->llc_testlink_time);
1783 	}
1784 }
1785 
1786 /* called in worker context */
1787 void smc_llc_link_clear(struct smc_link *link, bool log)
1788 {
1789 	if (log)
1790 		pr_warn_ratelimited("smc: SMC-R lg %*phN link removed: id %*phN"
1791 				    ", peerid %*phN, ibdev %s, ibport %d\n",
1792 				    SMC_LGR_ID_SIZE, &link->lgr->id,
1793 				    SMC_LGR_ID_SIZE, &link->link_uid,
1794 				    SMC_LGR_ID_SIZE, &link->peer_link_uid,
1795 				    link->smcibdev->ibdev->name, link->ibport);
1796 	complete(&link->llc_testlink_resp);
1797 	cancel_delayed_work_sync(&link->llc_testlink_wrk);
1798 	smc_wr_wakeup_reg_wait(link);
1799 	smc_wr_wakeup_tx_wait(link);
1800 }
1801 
1802 /* register a new rtoken at the remote peer (for all links) */
1803 int smc_llc_do_confirm_rkey(struct smc_link *send_link,
1804 			    struct smc_buf_desc *rmb_desc)
1805 {
1806 	struct smc_link_group *lgr = send_link->lgr;
1807 	struct smc_llc_qentry *qentry = NULL;
1808 	int rc = 0;
1809 
1810 	rc = smc_llc_send_confirm_rkey(send_link, rmb_desc);
1811 	if (rc)
1812 		goto out;
1813 	/* receive CONFIRM RKEY response from server over RoCE fabric */
1814 	qentry = smc_llc_wait(lgr, send_link, SMC_LLC_WAIT_TIME,
1815 			      SMC_LLC_CONFIRM_RKEY);
1816 	if (!qentry || (qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_RKEY_NEG))
1817 		rc = -EFAULT;
1818 out:
1819 	if (qentry)
1820 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1821 	return rc;
1822 }
1823 
1824 /* unregister an rtoken at the remote peer */
1825 int smc_llc_do_delete_rkey(struct smc_link_group *lgr,
1826 			   struct smc_buf_desc *rmb_desc)
1827 {
1828 	struct smc_llc_qentry *qentry = NULL;
1829 	struct smc_link *send_link;
1830 	int rc = 0;
1831 
1832 	send_link = smc_llc_usable_link(lgr);
1833 	if (!send_link)
1834 		return -ENOLINK;
1835 
1836 	/* protected by llc_flow control */
1837 	rc = smc_llc_send_delete_rkey(send_link, rmb_desc);
1838 	if (rc)
1839 		goto out;
1840 	/* receive DELETE RKEY response from server over RoCE fabric */
1841 	qentry = smc_llc_wait(lgr, send_link, SMC_LLC_WAIT_TIME,
1842 			      SMC_LLC_DELETE_RKEY);
1843 	if (!qentry || (qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_RKEY_NEG))
1844 		rc = -EFAULT;
1845 out:
1846 	if (qentry)
1847 		smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1848 	return rc;
1849 }
1850 
1851 void smc_llc_link_set_uid(struct smc_link *link)
1852 {
1853 	__be32 link_uid;
1854 
1855 	link_uid = htonl(*((u32 *)link->lgr->id) + link->link_id);
1856 	memcpy(link->link_uid, &link_uid, SMC_LGR_ID_SIZE);
1857 }
1858 
1859 /* save peers link user id, used for debug purposes */
1860 void smc_llc_save_peer_uid(struct smc_llc_qentry *qentry)
1861 {
1862 	memcpy(qentry->link->peer_link_uid, qentry->msg.confirm_link.link_uid,
1863 	       SMC_LGR_ID_SIZE);
1864 }
1865 
1866 /* evaluate confirm link request or response */
1867 int smc_llc_eval_conf_link(struct smc_llc_qentry *qentry,
1868 			   enum smc_llc_reqresp type)
1869 {
1870 	if (type == SMC_LLC_REQ) {	/* SMC server assigns link_id */
1871 		qentry->link->link_id = qentry->msg.confirm_link.link_num;
1872 		smc_llc_link_set_uid(qentry->link);
1873 	}
1874 	if (!(qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_NO_RMBE_EYEC))
1875 		return -ENOTSUPP;
1876 	return 0;
1877 }
1878 
1879 /***************************** init, exit, misc ******************************/
1880 
1881 static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
1882 	{
1883 		.handler	= smc_llc_rx_handler,
1884 		.type		= SMC_LLC_CONFIRM_LINK
1885 	},
1886 	{
1887 		.handler	= smc_llc_rx_handler,
1888 		.type		= SMC_LLC_TEST_LINK
1889 	},
1890 	{
1891 		.handler	= smc_llc_rx_handler,
1892 		.type		= SMC_LLC_ADD_LINK
1893 	},
1894 	{
1895 		.handler	= smc_llc_rx_handler,
1896 		.type		= SMC_LLC_ADD_LINK_CONT
1897 	},
1898 	{
1899 		.handler	= smc_llc_rx_handler,
1900 		.type		= SMC_LLC_DELETE_LINK
1901 	},
1902 	{
1903 		.handler	= smc_llc_rx_handler,
1904 		.type		= SMC_LLC_CONFIRM_RKEY
1905 	},
1906 	{
1907 		.handler	= smc_llc_rx_handler,
1908 		.type		= SMC_LLC_CONFIRM_RKEY_CONT
1909 	},
1910 	{
1911 		.handler	= smc_llc_rx_handler,
1912 		.type		= SMC_LLC_DELETE_RKEY
1913 	},
1914 	{
1915 		.handler	= NULL,
1916 	}
1917 };
1918 
1919 int __init smc_llc_init(void)
1920 {
1921 	struct smc_wr_rx_handler *handler;
1922 	int rc = 0;
1923 
1924 	for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
1925 		INIT_HLIST_NODE(&handler->list);
1926 		rc = smc_wr_rx_register_handler(handler);
1927 		if (rc)
1928 			break;
1929 	}
1930 	return rc;
1931 }
1932