xref: /openbmc/linux/net/smc/smc_llc.c (revision e6e8c82b)
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 
21 #define SMC_LLC_DATA_LEN		40
22 
23 struct smc_llc_hdr {
24 	struct smc_wr_rx_hdr common;
25 	u8 length;	/* 44 */
26 #if defined(__BIG_ENDIAN_BITFIELD)
27 	u8 reserved:4,
28 	   add_link_rej_rsn:4;
29 #elif defined(__LITTLE_ENDIAN_BITFIELD)
30 	u8 add_link_rej_rsn:4,
31 	   reserved:4;
32 #endif
33 	u8 flags;
34 };
35 
36 #define SMC_LLC_FLAG_NO_RMBE_EYEC	0x03
37 
38 struct smc_llc_msg_confirm_link {	/* type 0x01 */
39 	struct smc_llc_hdr hd;
40 	u8 sender_mac[ETH_ALEN];
41 	u8 sender_gid[SMC_GID_SIZE];
42 	u8 sender_qp_num[3];
43 	u8 link_num;
44 	u8 link_uid[SMC_LGR_ID_SIZE];
45 	u8 max_links;
46 	u8 reserved[9];
47 };
48 
49 #define SMC_LLC_FLAG_ADD_LNK_REJ	0x40
50 #define SMC_LLC_REJ_RSN_NO_ALT_PATH	1
51 
52 #define SMC_LLC_ADD_LNK_MAX_LINKS	2
53 
54 struct smc_llc_msg_add_link {		/* type 0x02 */
55 	struct smc_llc_hdr hd;
56 	u8 sender_mac[ETH_ALEN];
57 	u8 reserved2[2];
58 	u8 sender_gid[SMC_GID_SIZE];
59 	u8 sender_qp_num[3];
60 	u8 link_num;
61 	u8 flags2;	/* QP mtu */
62 	u8 initial_psn[3];
63 	u8 reserved[8];
64 };
65 
66 #define SMC_LLC_FLAG_DEL_LINK_ALL	0x40
67 #define SMC_LLC_FLAG_DEL_LINK_ORDERLY	0x20
68 
69 struct smc_llc_msg_del_link {		/* type 0x04 */
70 	struct smc_llc_hdr hd;
71 	u8 link_num;
72 	__be32 reason;
73 	u8 reserved[35];
74 } __packed;			/* format defined in RFC7609 */
75 
76 struct smc_llc_msg_test_link {		/* type 0x07 */
77 	struct smc_llc_hdr hd;
78 	u8 user_data[16];
79 	u8 reserved[24];
80 };
81 
82 struct smc_rmb_rtoken {
83 	union {
84 		u8 num_rkeys;	/* first rtoken byte of CONFIRM LINK msg */
85 				/* is actually the num of rtokens, first */
86 				/* rtoken is always for the current link */
87 		u8 link_id;	/* link id of the rtoken */
88 	};
89 	__be32 rmb_key;
90 	__be64 rmb_vaddr;
91 } __packed;			/* format defined in RFC7609 */
92 
93 #define SMC_LLC_RKEYS_PER_MSG	3
94 
95 struct smc_llc_msg_confirm_rkey {	/* type 0x06 */
96 	struct smc_llc_hdr hd;
97 	struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
98 	u8 reserved;
99 };
100 
101 struct smc_llc_msg_confirm_rkey_cont {	/* type 0x08 */
102 	struct smc_llc_hdr hd;
103 	u8 num_rkeys;
104 	struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
105 };
106 
107 #define SMC_LLC_DEL_RKEY_MAX	8
108 #define SMC_LLC_FLAG_RKEY_NEG	0x20
109 
110 struct smc_llc_msg_delete_rkey {	/* type 0x09 */
111 	struct smc_llc_hdr hd;
112 	u8 num_rkeys;
113 	u8 err_mask;
114 	u8 reserved[2];
115 	__be32 rkey[8];
116 	u8 reserved2[4];
117 };
118 
119 union smc_llc_msg {
120 	struct smc_llc_msg_confirm_link confirm_link;
121 	struct smc_llc_msg_add_link add_link;
122 	struct smc_llc_msg_del_link delete_link;
123 
124 	struct smc_llc_msg_confirm_rkey confirm_rkey;
125 	struct smc_llc_msg_confirm_rkey_cont confirm_rkey_cont;
126 	struct smc_llc_msg_delete_rkey delete_rkey;
127 
128 	struct smc_llc_msg_test_link test_link;
129 	struct {
130 		struct smc_llc_hdr hdr;
131 		u8 data[SMC_LLC_DATA_LEN];
132 	} raw;
133 };
134 
135 #define SMC_LLC_FLAG_RESP		0x80
136 
137 /********************************** send *************************************/
138 
139 struct smc_llc_tx_pend {
140 };
141 
142 /* handler for send/transmission completion of an LLC msg */
143 static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
144 			       struct smc_link *link,
145 			       enum ib_wc_status wc_status)
146 {
147 	/* future work: handle wc_status error for recovery and failover */
148 }
149 
150 /**
151  * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
152  * @link: Pointer to SMC link used for sending LLC control message.
153  * @wr_buf: Out variable returning pointer to work request payload buffer.
154  * @pend: Out variable returning pointer to private pending WR tracking.
155  *	  It's the context the transmit complete handler will get.
156  *
157  * Reserves and pre-fills an entry for a pending work request send/tx.
158  * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
159  * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
160  *
161  * Return: 0 on success, otherwise an error value.
162  */
163 static int smc_llc_add_pending_send(struct smc_link *link,
164 				    struct smc_wr_buf **wr_buf,
165 				    struct smc_wr_tx_pend_priv **pend)
166 {
167 	int rc;
168 
169 	rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, pend);
170 	if (rc < 0)
171 		return rc;
172 	BUILD_BUG_ON_MSG(
173 		sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
174 		"must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
175 	BUILD_BUG_ON_MSG(
176 		sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
177 		"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()");
178 	BUILD_BUG_ON_MSG(
179 		sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
180 		"must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
181 	return 0;
182 }
183 
184 /* high-level API to send LLC confirm link */
185 int smc_llc_send_confirm_link(struct smc_link *link,
186 			      enum smc_llc_reqresp reqresp)
187 {
188 	struct smc_link_group *lgr = smc_get_lgr(link);
189 	struct smc_llc_msg_confirm_link *confllc;
190 	struct smc_wr_tx_pend_priv *pend;
191 	struct smc_wr_buf *wr_buf;
192 	int rc;
193 
194 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
195 	if (rc)
196 		return rc;
197 	confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
198 	memset(confllc, 0, sizeof(*confllc));
199 	confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
200 	confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
201 	confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
202 	if (reqresp == SMC_LLC_RESP)
203 		confllc->hd.flags |= SMC_LLC_FLAG_RESP;
204 	memcpy(confllc->sender_mac, link->smcibdev->mac[link->ibport - 1],
205 	       ETH_ALEN);
206 	memcpy(confllc->sender_gid, link->gid, SMC_GID_SIZE);
207 	hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
208 	confllc->link_num = link->link_id;
209 	memcpy(confllc->link_uid, lgr->id, SMC_LGR_ID_SIZE);
210 	confllc->max_links = SMC_LLC_ADD_LNK_MAX_LINKS; /* enforce peer resp. */
211 	/* send llc message */
212 	rc = smc_wr_tx_send(link, pend);
213 	return rc;
214 }
215 
216 /* send LLC confirm rkey request */
217 static int smc_llc_send_confirm_rkey(struct smc_link *link,
218 				     struct smc_buf_desc *rmb_desc)
219 {
220 	struct smc_llc_msg_confirm_rkey *rkeyllc;
221 	struct smc_wr_tx_pend_priv *pend;
222 	struct smc_wr_buf *wr_buf;
223 	int rc;
224 
225 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
226 	if (rc)
227 		return rc;
228 	rkeyllc = (struct smc_llc_msg_confirm_rkey *)wr_buf;
229 	memset(rkeyllc, 0, sizeof(*rkeyllc));
230 	rkeyllc->hd.common.type = SMC_LLC_CONFIRM_RKEY;
231 	rkeyllc->hd.length = sizeof(struct smc_llc_msg_confirm_rkey);
232 	rkeyllc->rtoken[0].rmb_key =
233 		htonl(rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
234 	rkeyllc->rtoken[0].rmb_vaddr = cpu_to_be64(
235 		(u64)sg_dma_address(rmb_desc->sgt[SMC_SINGLE_LINK].sgl));
236 	/* send llc message */
237 	rc = smc_wr_tx_send(link, pend);
238 	return rc;
239 }
240 
241 /* send LLC delete rkey request */
242 static int smc_llc_send_delete_rkey(struct smc_link *link,
243 				    struct smc_buf_desc *rmb_desc)
244 {
245 	struct smc_llc_msg_delete_rkey *rkeyllc;
246 	struct smc_wr_tx_pend_priv *pend;
247 	struct smc_wr_buf *wr_buf;
248 	int rc;
249 
250 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
251 	if (rc)
252 		return rc;
253 	rkeyllc = (struct smc_llc_msg_delete_rkey *)wr_buf;
254 	memset(rkeyllc, 0, sizeof(*rkeyllc));
255 	rkeyllc->hd.common.type = SMC_LLC_DELETE_RKEY;
256 	rkeyllc->hd.length = sizeof(struct smc_llc_msg_delete_rkey);
257 	rkeyllc->num_rkeys = 1;
258 	rkeyllc->rkey[0] = htonl(rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
259 	/* send llc message */
260 	rc = smc_wr_tx_send(link, pend);
261 	return rc;
262 }
263 
264 /* prepare an add link message */
265 static void smc_llc_prep_add_link(struct smc_llc_msg_add_link *addllc,
266 				  struct smc_link *link, u8 mac[], u8 gid[],
267 				  enum smc_llc_reqresp reqresp)
268 {
269 	memset(addllc, 0, sizeof(*addllc));
270 	addllc->hd.common.type = SMC_LLC_ADD_LINK;
271 	addllc->hd.length = sizeof(struct smc_llc_msg_add_link);
272 	if (reqresp == SMC_LLC_RESP) {
273 		addllc->hd.flags |= SMC_LLC_FLAG_RESP;
274 		/* always reject more links for now */
275 		addllc->hd.flags |= SMC_LLC_FLAG_ADD_LNK_REJ;
276 		addllc->hd.add_link_rej_rsn = SMC_LLC_REJ_RSN_NO_ALT_PATH;
277 	}
278 	memcpy(addllc->sender_mac, mac, ETH_ALEN);
279 	memcpy(addllc->sender_gid, gid, SMC_GID_SIZE);
280 }
281 
282 /* send ADD LINK request or response */
283 int smc_llc_send_add_link(struct smc_link *link, u8 mac[], u8 gid[],
284 			  enum smc_llc_reqresp reqresp)
285 {
286 	struct smc_llc_msg_add_link *addllc;
287 	struct smc_wr_tx_pend_priv *pend;
288 	struct smc_wr_buf *wr_buf;
289 	int rc;
290 
291 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
292 	if (rc)
293 		return rc;
294 	addllc = (struct smc_llc_msg_add_link *)wr_buf;
295 	smc_llc_prep_add_link(addllc, link, mac, gid, reqresp);
296 	/* send llc message */
297 	rc = smc_wr_tx_send(link, pend);
298 	return rc;
299 }
300 
301 /* prepare a delete link message */
302 static void smc_llc_prep_delete_link(struct smc_llc_msg_del_link *delllc,
303 				     struct smc_link *link,
304 				     enum smc_llc_reqresp reqresp, bool orderly)
305 {
306 	memset(delllc, 0, sizeof(*delllc));
307 	delllc->hd.common.type = SMC_LLC_DELETE_LINK;
308 	delllc->hd.length = sizeof(struct smc_llc_msg_add_link);
309 	if (reqresp == SMC_LLC_RESP)
310 		delllc->hd.flags |= SMC_LLC_FLAG_RESP;
311 	/* DEL_LINK_ALL because only 1 link supported */
312 	delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
313 	if (orderly)
314 		delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
315 	delllc->link_num = link->link_id;
316 }
317 
318 /* send DELETE LINK request or response */
319 int smc_llc_send_delete_link(struct smc_link *link,
320 			     enum smc_llc_reqresp reqresp, bool orderly)
321 {
322 	struct smc_llc_msg_del_link *delllc;
323 	struct smc_wr_tx_pend_priv *pend;
324 	struct smc_wr_buf *wr_buf;
325 	int rc;
326 
327 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
328 	if (rc)
329 		return rc;
330 	delllc = (struct smc_llc_msg_del_link *)wr_buf;
331 	smc_llc_prep_delete_link(delllc, link, reqresp, orderly);
332 	/* send llc message */
333 	rc = smc_wr_tx_send(link, pend);
334 	return rc;
335 }
336 
337 /* send LLC test link request */
338 static int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16])
339 {
340 	struct smc_llc_msg_test_link *testllc;
341 	struct smc_wr_tx_pend_priv *pend;
342 	struct smc_wr_buf *wr_buf;
343 	int rc;
344 
345 	rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
346 	if (rc)
347 		return rc;
348 	testllc = (struct smc_llc_msg_test_link *)wr_buf;
349 	memset(testllc, 0, sizeof(*testllc));
350 	testllc->hd.common.type = SMC_LLC_TEST_LINK;
351 	testllc->hd.length = sizeof(struct smc_llc_msg_test_link);
352 	memcpy(testllc->user_data, user_data, sizeof(testllc->user_data));
353 	/* send llc message */
354 	rc = smc_wr_tx_send(link, pend);
355 	return rc;
356 }
357 
358 struct smc_llc_send_work {
359 	struct work_struct work;
360 	struct smc_link *link;
361 	int llclen;
362 	union smc_llc_msg llcbuf;
363 };
364 
365 /* worker that sends a prepared message */
366 static void smc_llc_send_message_work(struct work_struct *work)
367 {
368 	struct smc_llc_send_work *llcwrk = container_of(work,
369 						struct smc_llc_send_work, work);
370 	struct smc_wr_tx_pend_priv *pend;
371 	struct smc_wr_buf *wr_buf;
372 	int rc;
373 
374 	if (llcwrk->link->state == SMC_LNK_INACTIVE)
375 		goto out;
376 	rc = smc_llc_add_pending_send(llcwrk->link, &wr_buf, &pend);
377 	if (rc)
378 		goto out;
379 	memcpy(wr_buf, &llcwrk->llcbuf, llcwrk->llclen);
380 	smc_wr_tx_send(llcwrk->link, pend);
381 out:
382 	kfree(llcwrk);
383 }
384 
385 /* copy llcbuf and schedule an llc send on link */
386 static int smc_llc_send_message(struct smc_link *link, void *llcbuf, int llclen)
387 {
388 	struct smc_llc_send_work *wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
389 
390 	if (!wrk)
391 		return -ENOMEM;
392 	INIT_WORK(&wrk->work, smc_llc_send_message_work);
393 	wrk->link = link;
394 	wrk->llclen = llclen;
395 	memcpy(&wrk->llcbuf, llcbuf, llclen);
396 	queue_work(link->llc_wq, &wrk->work);
397 	return 0;
398 }
399 
400 /********************************* receive ***********************************/
401 
402 static void smc_llc_rx_confirm_link(struct smc_link *link,
403 				    struct smc_llc_msg_confirm_link *llc)
404 {
405 	struct smc_link_group *lgr = smc_get_lgr(link);
406 	int conf_rc;
407 
408 	/* RMBE eyecatchers are not supported */
409 	if (llc->hd.flags & SMC_LLC_FLAG_NO_RMBE_EYEC)
410 		conf_rc = 0;
411 	else
412 		conf_rc = ENOTSUPP;
413 
414 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
415 		if (lgr->role == SMC_SERV &&
416 		    link->state == SMC_LNK_ACTIVATING) {
417 			link->llc_confirm_resp_rc = conf_rc;
418 			complete(&link->llc_confirm_resp);
419 		}
420 	} else {
421 		if (lgr->role == SMC_CLNT &&
422 		    link->state == SMC_LNK_ACTIVATING) {
423 			link->llc_confirm_rc = conf_rc;
424 			link->link_id = llc->link_num;
425 			complete(&link->llc_confirm);
426 		}
427 	}
428 }
429 
430 static void smc_llc_rx_add_link(struct smc_link *link,
431 				struct smc_llc_msg_add_link *llc)
432 {
433 	struct smc_link_group *lgr = smc_get_lgr(link);
434 
435 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
436 		if (link->state == SMC_LNK_ACTIVATING)
437 			complete(&link->llc_add_resp);
438 	} else {
439 		if (link->state == SMC_LNK_ACTIVATING) {
440 			complete(&link->llc_add);
441 			return;
442 		}
443 
444 		if (lgr->role == SMC_SERV) {
445 			smc_llc_prep_add_link(llc, link,
446 					link->smcibdev->mac[link->ibport - 1],
447 					link->gid, SMC_LLC_REQ);
448 
449 		} else {
450 			smc_llc_prep_add_link(llc, link,
451 					link->smcibdev->mac[link->ibport - 1],
452 					link->gid, SMC_LLC_RESP);
453 		}
454 		smc_llc_send_message(link, llc, sizeof(*llc));
455 	}
456 }
457 
458 static void smc_llc_rx_delete_link(struct smc_link *link,
459 				   struct smc_llc_msg_del_link *llc)
460 {
461 	struct smc_link_group *lgr = smc_get_lgr(link);
462 
463 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
464 		if (lgr->role == SMC_SERV)
465 			smc_lgr_schedule_free_work_fast(lgr);
466 	} else {
467 		smc_lgr_forget(lgr);
468 		smc_llc_link_deleting(link);
469 		if (lgr->role == SMC_SERV) {
470 			/* client asks to delete this link, send request */
471 			smc_llc_prep_delete_link(llc, link, SMC_LLC_REQ, true);
472 		} else {
473 			/* server requests to delete this link, send response */
474 			smc_llc_prep_delete_link(llc, link, SMC_LLC_RESP, true);
475 		}
476 		smc_llc_send_message(link, llc, sizeof(*llc));
477 		smc_lgr_schedule_free_work_fast(lgr);
478 	}
479 }
480 
481 static void smc_llc_rx_test_link(struct smc_link *link,
482 				 struct smc_llc_msg_test_link *llc)
483 {
484 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
485 		if (link->state == SMC_LNK_ACTIVE)
486 			complete(&link->llc_testlink_resp);
487 	} else {
488 		llc->hd.flags |= SMC_LLC_FLAG_RESP;
489 		smc_llc_send_message(link, llc, sizeof(*llc));
490 	}
491 }
492 
493 static void smc_llc_rx_confirm_rkey(struct smc_link *link,
494 				    struct smc_llc_msg_confirm_rkey *llc)
495 {
496 	int rc;
497 
498 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
499 		link->llc_confirm_rkey_rc = llc->hd.flags &
500 					    SMC_LLC_FLAG_RKEY_NEG;
501 		complete(&link->llc_confirm_rkey);
502 	} else {
503 		rc = smc_rtoken_add(smc_get_lgr(link),
504 				    llc->rtoken[0].rmb_vaddr,
505 				    llc->rtoken[0].rmb_key);
506 
507 		/* ignore rtokens for other links, we have only one link */
508 
509 		llc->hd.flags |= SMC_LLC_FLAG_RESP;
510 		if (rc < 0)
511 			llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
512 		smc_llc_send_message(link, llc, sizeof(*llc));
513 	}
514 }
515 
516 static void smc_llc_rx_confirm_rkey_cont(struct smc_link *link,
517 				      struct smc_llc_msg_confirm_rkey_cont *llc)
518 {
519 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
520 		/* unused as long as we don't send this type of msg */
521 	} else {
522 		/* ignore rtokens for other links, we have only one link */
523 		llc->hd.flags |= SMC_LLC_FLAG_RESP;
524 		smc_llc_send_message(link, llc, sizeof(*llc));
525 	}
526 }
527 
528 static void smc_llc_rx_delete_rkey(struct smc_link *link,
529 				   struct smc_llc_msg_delete_rkey *llc)
530 {
531 	u8 err_mask = 0;
532 	int i, max;
533 
534 	if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
535 		link->llc_delete_rkey_rc = llc->hd.flags &
536 					    SMC_LLC_FLAG_RKEY_NEG;
537 		complete(&link->llc_delete_rkey);
538 	} else {
539 		max = min_t(u8, llc->num_rkeys, SMC_LLC_DEL_RKEY_MAX);
540 		for (i = 0; i < max; i++) {
541 			if (smc_rtoken_delete(smc_get_lgr(link), llc->rkey[i]))
542 				err_mask |= 1 << (SMC_LLC_DEL_RKEY_MAX - 1 - i);
543 		}
544 
545 		if (err_mask) {
546 			llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
547 			llc->err_mask = err_mask;
548 		}
549 
550 		llc->hd.flags |= SMC_LLC_FLAG_RESP;
551 		smc_llc_send_message(link, llc, sizeof(*llc));
552 	}
553 }
554 
555 static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
556 {
557 	struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
558 	union smc_llc_msg *llc = buf;
559 
560 	if (wc->byte_len < sizeof(*llc))
561 		return; /* short message */
562 	if (llc->raw.hdr.length != sizeof(*llc))
563 		return; /* invalid message */
564 	if (link->state == SMC_LNK_INACTIVE)
565 		return; /* link not active, drop msg */
566 
567 	switch (llc->raw.hdr.common.type) {
568 	case SMC_LLC_TEST_LINK:
569 		smc_llc_rx_test_link(link, &llc->test_link);
570 		break;
571 	case SMC_LLC_CONFIRM_LINK:
572 		smc_llc_rx_confirm_link(link, &llc->confirm_link);
573 		break;
574 	case SMC_LLC_ADD_LINK:
575 		smc_llc_rx_add_link(link, &llc->add_link);
576 		break;
577 	case SMC_LLC_DELETE_LINK:
578 		smc_llc_rx_delete_link(link, &llc->delete_link);
579 		break;
580 	case SMC_LLC_CONFIRM_RKEY:
581 		smc_llc_rx_confirm_rkey(link, &llc->confirm_rkey);
582 		break;
583 	case SMC_LLC_CONFIRM_RKEY_CONT:
584 		smc_llc_rx_confirm_rkey_cont(link, &llc->confirm_rkey_cont);
585 		break;
586 	case SMC_LLC_DELETE_RKEY:
587 		smc_llc_rx_delete_rkey(link, &llc->delete_rkey);
588 		break;
589 	}
590 }
591 
592 /***************************** worker, utils *********************************/
593 
594 static void smc_llc_testlink_work(struct work_struct *work)
595 {
596 	struct smc_link *link = container_of(to_delayed_work(work),
597 					     struct smc_link, llc_testlink_wrk);
598 	unsigned long next_interval;
599 	unsigned long expire_time;
600 	u8 user_data[16] = { 0 };
601 	int rc;
602 
603 	if (link->state != SMC_LNK_ACTIVE)
604 		return;		/* don't reschedule worker */
605 	expire_time = link->wr_rx_tstamp + link->llc_testlink_time;
606 	if (time_is_after_jiffies(expire_time)) {
607 		next_interval = expire_time - jiffies;
608 		goto out;
609 	}
610 	reinit_completion(&link->llc_testlink_resp);
611 	smc_llc_send_test_link(link, user_data);
612 	/* receive TEST LINK response over RoCE fabric */
613 	rc = wait_for_completion_interruptible_timeout(&link->llc_testlink_resp,
614 						       SMC_LLC_WAIT_TIME);
615 	if (rc <= 0) {
616 		smc_lgr_terminate(smc_get_lgr(link));
617 		return;
618 	}
619 	next_interval = link->llc_testlink_time;
620 out:
621 	queue_delayed_work(link->llc_wq, &link->llc_testlink_wrk,
622 			   next_interval);
623 }
624 
625 int smc_llc_link_init(struct smc_link *link)
626 {
627 	struct smc_link_group *lgr = smc_get_lgr(link);
628 	link->llc_wq = alloc_ordered_workqueue("llc_wq-%x:%x)", WQ_MEM_RECLAIM,
629 					       *((u32 *)lgr->id),
630 					       link->link_id);
631 	if (!link->llc_wq)
632 		return -ENOMEM;
633 	init_completion(&link->llc_confirm);
634 	init_completion(&link->llc_confirm_resp);
635 	init_completion(&link->llc_add);
636 	init_completion(&link->llc_add_resp);
637 	init_completion(&link->llc_confirm_rkey);
638 	init_completion(&link->llc_delete_rkey);
639 	mutex_init(&link->llc_delete_rkey_mutex);
640 	init_completion(&link->llc_testlink_resp);
641 	INIT_DELAYED_WORK(&link->llc_testlink_wrk, smc_llc_testlink_work);
642 	return 0;
643 }
644 
645 void smc_llc_link_active(struct smc_link *link, int testlink_time)
646 {
647 	link->state = SMC_LNK_ACTIVE;
648 	if (testlink_time) {
649 		link->llc_testlink_time = testlink_time * HZ;
650 		queue_delayed_work(link->llc_wq, &link->llc_testlink_wrk,
651 				   link->llc_testlink_time);
652 	}
653 }
654 
655 void smc_llc_link_deleting(struct smc_link *link)
656 {
657 	link->state = SMC_LNK_DELETING;
658 }
659 
660 /* called in tasklet context */
661 void smc_llc_link_inactive(struct smc_link *link)
662 {
663 	link->state = SMC_LNK_INACTIVE;
664 	cancel_delayed_work(&link->llc_testlink_wrk);
665 }
666 
667 /* called in worker context */
668 void smc_llc_link_clear(struct smc_link *link)
669 {
670 	flush_workqueue(link->llc_wq);
671 	destroy_workqueue(link->llc_wq);
672 }
673 
674 /* register a new rtoken at the remote peer */
675 int smc_llc_do_confirm_rkey(struct smc_link *link,
676 			    struct smc_buf_desc *rmb_desc)
677 {
678 	int rc;
679 
680 	/* protected by mutex smc_create_lgr_pending */
681 	reinit_completion(&link->llc_confirm_rkey);
682 	rc = smc_llc_send_confirm_rkey(link, rmb_desc);
683 	if (rc)
684 		return rc;
685 	/* receive CONFIRM RKEY response from server over RoCE fabric */
686 	rc = wait_for_completion_interruptible_timeout(&link->llc_confirm_rkey,
687 						       SMC_LLC_WAIT_TIME);
688 	if (rc <= 0 || link->llc_confirm_rkey_rc)
689 		return -EFAULT;
690 	return 0;
691 }
692 
693 /* unregister an rtoken at the remote peer */
694 int smc_llc_do_delete_rkey(struct smc_link *link,
695 			   struct smc_buf_desc *rmb_desc)
696 {
697 	int rc;
698 
699 	mutex_lock(&link->llc_delete_rkey_mutex);
700 	reinit_completion(&link->llc_delete_rkey);
701 	rc = smc_llc_send_delete_rkey(link, rmb_desc);
702 	if (rc)
703 		goto out;
704 	/* receive DELETE RKEY response from server over RoCE fabric */
705 	rc = wait_for_completion_interruptible_timeout(&link->llc_delete_rkey,
706 						       SMC_LLC_WAIT_TIME);
707 	if (rc <= 0 || link->llc_delete_rkey_rc)
708 		rc = -EFAULT;
709 	else
710 		rc = 0;
711 out:
712 	mutex_unlock(&link->llc_delete_rkey_mutex);
713 	return rc;
714 }
715 
716 /***************************** init, exit, misc ******************************/
717 
718 static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
719 	{
720 		.handler	= smc_llc_rx_handler,
721 		.type		= SMC_LLC_CONFIRM_LINK
722 	},
723 	{
724 		.handler	= smc_llc_rx_handler,
725 		.type		= SMC_LLC_TEST_LINK
726 	},
727 	{
728 		.handler	= smc_llc_rx_handler,
729 		.type		= SMC_LLC_ADD_LINK
730 	},
731 	{
732 		.handler	= smc_llc_rx_handler,
733 		.type		= SMC_LLC_DELETE_LINK
734 	},
735 	{
736 		.handler	= smc_llc_rx_handler,
737 		.type		= SMC_LLC_CONFIRM_RKEY
738 	},
739 	{
740 		.handler	= smc_llc_rx_handler,
741 		.type		= SMC_LLC_CONFIRM_RKEY_CONT
742 	},
743 	{
744 		.handler	= smc_llc_rx_handler,
745 		.type		= SMC_LLC_DELETE_RKEY
746 	},
747 	{
748 		.handler	= NULL,
749 	}
750 };
751 
752 int __init smc_llc_init(void)
753 {
754 	struct smc_wr_rx_handler *handler;
755 	int rc = 0;
756 
757 	for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
758 		INIT_HLIST_NODE(&handler->list);
759 		rc = smc_wr_rx_register_handler(handler);
760 		if (rc)
761 			break;
762 	}
763 	return rc;
764 }
765