1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Link Layer Control (LLC)
7 * Copyright IBM Corp. 2016
9 * Author(s): Klaus Wacker <Klaus.Wacker@de.ibm.com>
10 * Ursula Braun <ubraun@linux.vnet.ibm.com>
14 #include <rdma/ib_verbs.h>
22 #define SMC_LLC_DATA_LEN 40
25 struct smc_wr_rx_hdr common;
27 #if defined(__BIG_ENDIAN_BITFIELD)
30 #elif defined(__LITTLE_ENDIAN_BITFIELD)
31 u8 add_link_rej_rsn:4,
37 #define SMC_LLC_FLAG_NO_RMBE_EYEC 0x03
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];
45 u8 link_uid[SMC_LGR_ID_SIZE];
50 #define SMC_LLC_FLAG_ADD_LNK_REJ 0x40
51 #define SMC_LLC_REJ_RSN_NO_ALT_PATH 1
53 #define SMC_LLC_ADD_LNK_MAX_LINKS 2
55 struct smc_llc_msg_add_link { /* type 0x02 */
56 struct smc_llc_hdr hd;
57 u8 sender_mac[ETH_ALEN];
59 u8 sender_gid[SMC_GID_SIZE];
62 #if defined(__BIG_ENDIAN_BITFIELD)
65 #elif defined(__LITTLE_ENDIAN_BITFIELD)
73 struct smc_llc_msg_add_link_cont_rt {
79 #define SMC_LLC_RKEYS_PER_CONT_MSG 2
81 struct smc_llc_msg_add_link_cont { /* type 0x03 */
82 struct smc_llc_hdr hd;
86 struct smc_llc_msg_add_link_cont_rt rt[SMC_LLC_RKEYS_PER_CONT_MSG];
88 } __packed; /* format defined in RFC7609 */
90 #define SMC_LLC_FLAG_DEL_LINK_ALL 0x40
91 #define SMC_LLC_FLAG_DEL_LINK_ORDERLY 0x20
93 struct smc_llc_msg_del_link { /* type 0x04 */
94 struct smc_llc_hdr hd;
98 } __packed; /* format defined in RFC7609 */
100 struct smc_llc_msg_test_link { /* type 0x07 */
101 struct smc_llc_hdr hd;
106 struct smc_rmb_rtoken {
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 */
115 } __packed; /* format defined in RFC7609 */
117 #define SMC_LLC_RKEYS_PER_MSG 3
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];
125 #define SMC_LLC_DEL_RKEY_MAX 8
126 #define SMC_LLC_FLAG_RKEY_RETRY 0x10
127 #define SMC_LLC_FLAG_RKEY_NEG 0x20
129 struct smc_llc_msg_delete_rkey { /* type 0x09 */
130 struct smc_llc_hdr hd;
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;
144 struct smc_llc_msg_confirm_rkey confirm_rkey;
145 struct smc_llc_msg_delete_rkey delete_rkey;
147 struct smc_llc_msg_test_link test_link;
149 struct smc_llc_hdr hdr;
150 u8 data[SMC_LLC_DATA_LEN];
154 #define SMC_LLC_FLAG_RESP 0x80
156 struct smc_llc_qentry {
157 struct list_head list;
158 struct smc_link *link;
159 union smc_llc_msg msg;
162 static void smc_llc_enqueue(struct smc_link *link, union smc_llc_msg *llc);
164 struct smc_llc_qentry *smc_llc_flow_qentry_clr(struct smc_llc_flow *flow)
166 struct smc_llc_qentry *qentry = flow->qentry;
172 void smc_llc_flow_qentry_del(struct smc_llc_flow *flow)
174 struct smc_llc_qentry *qentry;
177 qentry = flow->qentry;
183 static inline void smc_llc_flow_qentry_set(struct smc_llc_flow *flow,
184 struct smc_llc_qentry *qentry)
186 flow->qentry = qentry;
189 /* try to start a new llc flow, initiated by an incoming llc msg */
190 static bool smc_llc_flow_start(struct smc_llc_flow *flow,
191 struct smc_llc_qentry *qentry)
193 struct smc_link_group *lgr = qentry->link->lgr;
195 spin_lock_bh(&lgr->llc_flow_lock);
197 /* a flow is already active */
198 if ((qentry->msg.raw.hdr.common.type == SMC_LLC_ADD_LINK ||
199 qentry->msg.raw.hdr.common.type == SMC_LLC_DELETE_LINK) &&
200 !lgr->delayed_event) {
201 lgr->delayed_event = qentry;
203 /* forget this llc request */
206 spin_unlock_bh(&lgr->llc_flow_lock);
209 switch (qentry->msg.raw.hdr.common.type) {
210 case SMC_LLC_ADD_LINK:
211 flow->type = SMC_LLC_FLOW_ADD_LINK;
213 case SMC_LLC_DELETE_LINK:
214 flow->type = SMC_LLC_FLOW_DEL_LINK;
216 case SMC_LLC_CONFIRM_RKEY:
217 case SMC_LLC_DELETE_RKEY:
218 flow->type = SMC_LLC_FLOW_RKEY;
221 flow->type = SMC_LLC_FLOW_NONE;
223 if (qentry == lgr->delayed_event)
224 lgr->delayed_event = NULL;
225 spin_unlock_bh(&lgr->llc_flow_lock);
226 smc_llc_flow_qentry_set(flow, qentry);
230 /* start a new local llc flow, wait till current flow finished */
231 int smc_llc_flow_initiate(struct smc_link_group *lgr,
232 enum smc_llc_flowtype type)
234 enum smc_llc_flowtype allowed_remote = SMC_LLC_FLOW_NONE;
237 /* all flows except confirm_rkey and delete_rkey are exclusive,
238 * confirm/delete rkey flows can run concurrently (local and remote)
240 if (type == SMC_LLC_FLOW_RKEY)
241 allowed_remote = SMC_LLC_FLOW_RKEY;
243 if (list_empty(&lgr->list))
245 spin_lock_bh(&lgr->llc_flow_lock);
246 if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE &&
247 (lgr->llc_flow_rmt.type == SMC_LLC_FLOW_NONE ||
248 lgr->llc_flow_rmt.type == allowed_remote)) {
249 lgr->llc_flow_lcl.type = type;
250 spin_unlock_bh(&lgr->llc_flow_lock);
253 spin_unlock_bh(&lgr->llc_flow_lock);
254 rc = wait_event_interruptible_timeout(lgr->llc_waiter,
255 (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE &&
256 (lgr->llc_flow_rmt.type == SMC_LLC_FLOW_NONE ||
257 lgr->llc_flow_rmt.type == allowed_remote)),
264 /* finish the current llc flow */
265 void smc_llc_flow_stop(struct smc_link_group *lgr, struct smc_llc_flow *flow)
267 spin_lock_bh(&lgr->llc_flow_lock);
268 memset(flow, 0, sizeof(*flow));
269 flow->type = SMC_LLC_FLOW_NONE;
270 spin_unlock_bh(&lgr->llc_flow_lock);
271 if (!list_empty(&lgr->list) && lgr->delayed_event &&
272 flow == &lgr->llc_flow_lcl)
273 schedule_work(&lgr->llc_event_work);
275 wake_up_interruptible(&lgr->llc_waiter);
278 /* lnk is optional and used for early wakeup when link goes down, useful in
279 * cases where we wait for a response on the link after we sent a request
281 struct smc_llc_qentry *smc_llc_wait(struct smc_link_group *lgr,
282 struct smc_link *lnk,
283 int time_out, u8 exp_msg)
285 struct smc_llc_flow *flow = &lgr->llc_flow_lcl;
287 wait_event_interruptible_timeout(lgr->llc_waiter,
289 (lnk && !smc_link_usable(lnk)) ||
290 list_empty(&lgr->list)),
293 (lnk && !smc_link_usable(lnk)) || list_empty(&lgr->list)) {
294 smc_llc_flow_qentry_del(flow);
297 if (exp_msg && flow->qentry->msg.raw.hdr.common.type != exp_msg) {
298 if (exp_msg == SMC_LLC_ADD_LINK &&
299 flow->qentry->msg.raw.hdr.common.type ==
300 SMC_LLC_DELETE_LINK) {
301 /* flow_start will delay the unexpected msg */
302 smc_llc_flow_start(&lgr->llc_flow_lcl,
303 smc_llc_flow_qentry_clr(flow));
306 smc_llc_flow_qentry_del(flow);
312 /********************************** send *************************************/
314 struct smc_llc_tx_pend {
317 /* handler for send/transmission completion of an LLC msg */
318 static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
319 struct smc_link *link,
320 enum ib_wc_status wc_status)
322 /* future work: handle wc_status error for recovery and failover */
326 * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
327 * @link: Pointer to SMC link used for sending LLC control message.
328 * @wr_buf: Out variable returning pointer to work request payload buffer.
329 * @pend: Out variable returning pointer to private pending WR tracking.
330 * It's the context the transmit complete handler will get.
332 * Reserves and pre-fills an entry for a pending work request send/tx.
333 * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
334 * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
336 * Return: 0 on success, otherwise an error value.
338 static int smc_llc_add_pending_send(struct smc_link *link,
339 struct smc_wr_buf **wr_buf,
340 struct smc_wr_tx_pend_priv **pend)
344 rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, NULL,
349 sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
350 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
352 sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
353 "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()");
355 sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
356 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
360 /* high-level API to send LLC confirm link */
361 int smc_llc_send_confirm_link(struct smc_link *link,
362 enum smc_llc_reqresp reqresp)
364 struct smc_link_group *lgr = smc_get_lgr(link);
365 struct smc_llc_msg_confirm_link *confllc;
366 struct smc_wr_tx_pend_priv *pend;
367 struct smc_wr_buf *wr_buf;
370 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
373 confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
374 memset(confllc, 0, sizeof(*confllc));
375 confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
376 confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
377 confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
378 if (reqresp == SMC_LLC_RESP)
379 confllc->hd.flags |= SMC_LLC_FLAG_RESP;
380 memcpy(confllc->sender_mac, link->smcibdev->mac[link->ibport - 1],
382 memcpy(confllc->sender_gid, link->gid, SMC_GID_SIZE);
383 hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
384 confllc->link_num = link->link_id;
385 memcpy(confllc->link_uid, lgr->id, SMC_LGR_ID_SIZE);
386 confllc->max_links = SMC_LLC_ADD_LNK_MAX_LINKS;
387 /* send llc message */
388 rc = smc_wr_tx_send(link, pend);
392 /* send LLC confirm rkey request */
393 static int smc_llc_send_confirm_rkey(struct smc_link *send_link,
394 struct smc_buf_desc *rmb_desc)
396 struct smc_llc_msg_confirm_rkey *rkeyllc;
397 struct smc_wr_tx_pend_priv *pend;
398 struct smc_wr_buf *wr_buf;
399 struct smc_link *link;
402 rc = smc_llc_add_pending_send(send_link, &wr_buf, &pend);
405 rkeyllc = (struct smc_llc_msg_confirm_rkey *)wr_buf;
406 memset(rkeyllc, 0, sizeof(*rkeyllc));
407 rkeyllc->hd.common.type = SMC_LLC_CONFIRM_RKEY;
408 rkeyllc->hd.length = sizeof(struct smc_llc_msg_confirm_rkey);
411 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
412 link = &send_link->lgr->lnk[i];
413 if (link->state == SMC_LNK_ACTIVE && link != send_link) {
414 rkeyllc->rtoken[rtok_ix].link_id = link->link_id;
415 rkeyllc->rtoken[rtok_ix].rmb_key =
416 htonl(rmb_desc->mr_rx[link->link_idx]->rkey);
417 rkeyllc->rtoken[rtok_ix].rmb_vaddr = cpu_to_be64(
419 rmb_desc->sgt[link->link_idx].sgl));
423 /* rkey of send_link is in rtoken[0] */
424 rkeyllc->rtoken[0].num_rkeys = rtok_ix - 1;
425 rkeyllc->rtoken[0].rmb_key =
426 htonl(rmb_desc->mr_rx[send_link->link_idx]->rkey);
427 rkeyllc->rtoken[0].rmb_vaddr = cpu_to_be64(
428 (u64)sg_dma_address(rmb_desc->sgt[send_link->link_idx].sgl));
429 /* send llc message */
430 rc = smc_wr_tx_send(send_link, pend);
434 /* send LLC delete rkey request */
435 static int smc_llc_send_delete_rkey(struct smc_link *link,
436 struct smc_buf_desc *rmb_desc)
438 struct smc_llc_msg_delete_rkey *rkeyllc;
439 struct smc_wr_tx_pend_priv *pend;
440 struct smc_wr_buf *wr_buf;
443 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
446 rkeyllc = (struct smc_llc_msg_delete_rkey *)wr_buf;
447 memset(rkeyllc, 0, sizeof(*rkeyllc));
448 rkeyllc->hd.common.type = SMC_LLC_DELETE_RKEY;
449 rkeyllc->hd.length = sizeof(struct smc_llc_msg_delete_rkey);
450 rkeyllc->num_rkeys = 1;
451 rkeyllc->rkey[0] = htonl(rmb_desc->mr_rx[link->link_idx]->rkey);
452 /* send llc message */
453 rc = smc_wr_tx_send(link, pend);
457 /* send ADD LINK request or response */
458 int smc_llc_send_add_link(struct smc_link *link, u8 mac[], u8 gid[],
459 struct smc_link *link_new,
460 enum smc_llc_reqresp reqresp)
462 struct smc_llc_msg_add_link *addllc;
463 struct smc_wr_tx_pend_priv *pend;
464 struct smc_wr_buf *wr_buf;
467 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
470 addllc = (struct smc_llc_msg_add_link *)wr_buf;
472 memset(addllc, 0, sizeof(*addllc));
473 addllc->hd.common.type = SMC_LLC_ADD_LINK;
474 addllc->hd.length = sizeof(struct smc_llc_msg_add_link);
475 if (reqresp == SMC_LLC_RESP)
476 addllc->hd.flags |= SMC_LLC_FLAG_RESP;
477 memcpy(addllc->sender_mac, mac, ETH_ALEN);
478 memcpy(addllc->sender_gid, gid, SMC_GID_SIZE);
480 addllc->link_num = link_new->link_id;
481 hton24(addllc->sender_qp_num, link_new->roce_qp->qp_num);
482 hton24(addllc->initial_psn, link_new->psn_initial);
483 if (reqresp == SMC_LLC_REQ)
484 addllc->qp_mtu = link_new->path_mtu;
486 addllc->qp_mtu = min(link_new->path_mtu,
489 /* send llc message */
490 rc = smc_wr_tx_send(link, pend);
494 /* send DELETE LINK request or response */
495 int smc_llc_send_delete_link(struct smc_link *link, u8 link_del_id,
496 enum smc_llc_reqresp reqresp, bool orderly,
499 struct smc_llc_msg_del_link *delllc;
500 struct smc_wr_tx_pend_priv *pend;
501 struct smc_wr_buf *wr_buf;
504 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
507 delllc = (struct smc_llc_msg_del_link *)wr_buf;
509 memset(delllc, 0, sizeof(*delllc));
510 delllc->hd.common.type = SMC_LLC_DELETE_LINK;
511 delllc->hd.length = sizeof(struct smc_llc_msg_del_link);
512 if (reqresp == SMC_LLC_RESP)
513 delllc->hd.flags |= SMC_LLC_FLAG_RESP;
515 delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
517 delllc->link_num = link_del_id;
519 delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
520 delllc->reason = htonl(reason);
521 /* send llc message */
522 rc = smc_wr_tx_send(link, pend);
526 /* send LLC test link request */
527 static int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16])
529 struct smc_llc_msg_test_link *testllc;
530 struct smc_wr_tx_pend_priv *pend;
531 struct smc_wr_buf *wr_buf;
534 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
537 testllc = (struct smc_llc_msg_test_link *)wr_buf;
538 memset(testllc, 0, sizeof(*testllc));
539 testllc->hd.common.type = SMC_LLC_TEST_LINK;
540 testllc->hd.length = sizeof(struct smc_llc_msg_test_link);
541 memcpy(testllc->user_data, user_data, sizeof(testllc->user_data));
542 /* send llc message */
543 rc = smc_wr_tx_send(link, pend);
547 /* schedule an llc send on link, may wait for buffers */
548 static int smc_llc_send_message(struct smc_link *link, void *llcbuf)
550 struct smc_wr_tx_pend_priv *pend;
551 struct smc_wr_buf *wr_buf;
554 if (!smc_link_usable(link))
556 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
559 memcpy(wr_buf, llcbuf, sizeof(union smc_llc_msg));
560 return smc_wr_tx_send(link, pend);
563 /********************************* receive ***********************************/
565 static int smc_llc_alloc_alt_link(struct smc_link_group *lgr,
566 enum smc_lgr_type lgr_new_t)
570 if (lgr->type == SMC_LGR_SYMMETRIC ||
571 (lgr->type != SMC_LGR_SINGLE &&
572 (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
573 lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)))
576 if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
577 lgr_new_t == SMC_LGR_ASYMMETRIC_PEER) {
578 for (i = SMC_LINKS_PER_LGR_MAX - 1; i >= 0; i--)
579 if (lgr->lnk[i].state == SMC_LNK_UNUSED)
582 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
583 if (lgr->lnk[i].state == SMC_LNK_UNUSED)
589 /* return first buffer from any of the next buf lists */
590 static struct smc_buf_desc *_smc_llc_get_next_rmb(struct smc_link_group *lgr,
593 struct smc_buf_desc *buf_pos;
595 while (*buf_lst < SMC_RMBE_SIZES) {
596 buf_pos = list_first_entry_or_null(&lgr->rmbs[*buf_lst],
597 struct smc_buf_desc, list);
605 /* return next rmb from buffer lists */
606 static struct smc_buf_desc *smc_llc_get_next_rmb(struct smc_link_group *lgr,
608 struct smc_buf_desc *buf_pos)
610 struct smc_buf_desc *buf_next;
612 if (!buf_pos || list_is_last(&buf_pos->list, &lgr->rmbs[*buf_lst])) {
614 return _smc_llc_get_next_rmb(lgr, buf_lst);
616 buf_next = list_next_entry(buf_pos, list);
620 static struct smc_buf_desc *smc_llc_get_first_rmb(struct smc_link_group *lgr,
624 return smc_llc_get_next_rmb(lgr, buf_lst, NULL);
627 /* send one add_link_continue msg */
628 static int smc_llc_add_link_cont(struct smc_link *link,
629 struct smc_link *link_new, u8 *num_rkeys_todo,
630 int *buf_lst, struct smc_buf_desc **buf_pos)
632 struct smc_llc_msg_add_link_cont *addc_llc;
633 struct smc_link_group *lgr = link->lgr;
634 int prim_lnk_idx, lnk_idx, i, rc;
635 struct smc_wr_tx_pend_priv *pend;
636 struct smc_wr_buf *wr_buf;
637 struct smc_buf_desc *rmb;
640 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
643 addc_llc = (struct smc_llc_msg_add_link_cont *)wr_buf;
644 memset(addc_llc, 0, sizeof(*addc_llc));
646 prim_lnk_idx = link->link_idx;
647 lnk_idx = link_new->link_idx;
648 addc_llc->link_num = link_new->link_id;
649 addc_llc->num_rkeys = *num_rkeys_todo;
651 for (i = 0; i < min_t(u8, n, SMC_LLC_RKEYS_PER_CONT_MSG); i++) {
653 addc_llc->num_rkeys = addc_llc->num_rkeys -
660 addc_llc->rt[i].rmb_key = htonl(rmb->mr_rx[prim_lnk_idx]->rkey);
661 addc_llc->rt[i].rmb_key_new = htonl(rmb->mr_rx[lnk_idx]->rkey);
662 addc_llc->rt[i].rmb_vaddr_new =
663 cpu_to_be64((u64)sg_dma_address(rmb->sgt[lnk_idx].sgl));
666 *buf_pos = smc_llc_get_next_rmb(lgr, buf_lst, *buf_pos);
667 while (*buf_pos && !(*buf_pos)->used)
668 *buf_pos = smc_llc_get_next_rmb(lgr, buf_lst, *buf_pos);
670 addc_llc->hd.common.type = SMC_LLC_ADD_LINK_CONT;
671 addc_llc->hd.length = sizeof(struct smc_llc_msg_add_link_cont);
672 if (lgr->role == SMC_CLNT)
673 addc_llc->hd.flags |= SMC_LLC_FLAG_RESP;
674 return smc_wr_tx_send(link, pend);
677 static int smc_llc_cli_rkey_exchange(struct smc_link *link,
678 struct smc_link *link_new)
680 struct smc_llc_msg_add_link_cont *addc_llc;
681 struct smc_link_group *lgr = link->lgr;
682 u8 max, num_rkeys_send, num_rkeys_recv;
683 struct smc_llc_qentry *qentry;
684 struct smc_buf_desc *buf_pos;
689 mutex_lock(&lgr->rmbs_lock);
690 num_rkeys_send = lgr->conns_num;
691 buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
693 qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_TIME,
694 SMC_LLC_ADD_LINK_CONT);
699 addc_llc = &qentry->msg.add_link_cont;
700 num_rkeys_recv = addc_llc->num_rkeys;
701 max = min_t(u8, num_rkeys_recv, SMC_LLC_RKEYS_PER_CONT_MSG);
702 for (i = 0; i < max; i++) {
703 smc_rtoken_set(lgr, link->link_idx, link_new->link_idx,
704 addc_llc->rt[i].rmb_key,
705 addc_llc->rt[i].rmb_vaddr_new,
706 addc_llc->rt[i].rmb_key_new);
709 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
710 rc = smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
714 } while (num_rkeys_send || num_rkeys_recv);
716 mutex_unlock(&lgr->rmbs_lock);
720 /* prepare and send an add link reject response */
721 static int smc_llc_cli_add_link_reject(struct smc_llc_qentry *qentry)
723 qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_RESP;
724 qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_ADD_LNK_REJ;
725 qentry->msg.raw.hdr.add_link_rej_rsn = SMC_LLC_REJ_RSN_NO_ALT_PATH;
726 return smc_llc_send_message(qentry->link, &qentry->msg);
729 static int smc_llc_cli_conf_link(struct smc_link *link,
730 struct smc_init_info *ini,
731 struct smc_link *link_new,
732 enum smc_lgr_type lgr_new_t)
734 struct smc_link_group *lgr = link->lgr;
735 struct smc_llc_msg_del_link *del_llc;
736 struct smc_llc_qentry *qentry = NULL;
739 /* receive CONFIRM LINK request over RoCE fabric */
740 qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_FIRST_TIME, 0);
742 rc = smc_llc_send_delete_link(link, link_new->link_id,
744 SMC_LLC_DEL_LOST_PATH);
747 if (qentry->msg.raw.hdr.common.type != SMC_LLC_CONFIRM_LINK) {
748 /* received DELETE_LINK instead */
749 del_llc = &qentry->msg.delete_link;
750 qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_RESP;
751 smc_llc_send_message(link, &qentry->msg);
752 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
755 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
757 rc = smc_ib_modify_qp_rts(link_new);
759 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
760 false, SMC_LLC_DEL_LOST_PATH);
763 smc_wr_remember_qp_attr(link_new);
765 rc = smcr_buf_reg_lgr(link_new);
767 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
768 false, SMC_LLC_DEL_LOST_PATH);
772 /* send CONFIRM LINK response over RoCE fabric */
773 rc = smc_llc_send_confirm_link(link_new, SMC_LLC_RESP);
775 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
776 false, SMC_LLC_DEL_LOST_PATH);
779 smc_llc_link_active(link_new);
780 lgr->type = lgr_new_t;
784 static void smc_llc_save_add_link_info(struct smc_link *link,
785 struct smc_llc_msg_add_link *add_llc)
787 link->peer_qpn = ntoh24(add_llc->sender_qp_num);
788 memcpy(link->peer_gid, add_llc->sender_gid, SMC_GID_SIZE);
789 memcpy(link->peer_mac, add_llc->sender_mac, ETH_ALEN);
790 link->peer_psn = ntoh24(add_llc->initial_psn);
791 link->peer_mtu = add_llc->qp_mtu;
794 /* as an SMC client, process an add link request */
795 int smc_llc_cli_add_link(struct smc_link *link, struct smc_llc_qentry *qentry)
797 struct smc_llc_msg_add_link *llc = &qentry->msg.add_link;
798 enum smc_lgr_type lgr_new_t = SMC_LGR_SYMMETRIC;
799 struct smc_link_group *lgr = smc_get_lgr(link);
800 struct smc_link *lnk_new = NULL;
801 struct smc_init_info ini;
804 ini.vlan_id = lgr->vlan_id;
805 smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
806 if (!memcmp(llc->sender_gid, link->peer_gid, SMC_GID_SIZE) &&
807 !memcmp(llc->sender_mac, link->peer_mac, ETH_ALEN)) {
810 lgr_new_t = SMC_LGR_ASYMMETRIC_PEER;
813 lgr_new_t = SMC_LGR_ASYMMETRIC_LOCAL;
814 ini.ib_dev = link->smcibdev;
815 ini.ib_port = link->ibport;
817 lnk_idx = smc_llc_alloc_alt_link(lgr, lgr_new_t);
820 lnk_new = &lgr->lnk[lnk_idx];
821 rc = smcr_link_init(lgr, lnk_new, lnk_idx, &ini);
824 smc_llc_save_add_link_info(lnk_new, llc);
825 lnk_new->link_id = llc->link_num;
827 rc = smc_ib_ready_link(lnk_new);
831 rc = smcr_buf_map_lgr(lnk_new);
835 rc = smc_llc_send_add_link(link,
836 lnk_new->smcibdev->mac[ini.ib_port - 1],
837 lnk_new->gid, lnk_new, SMC_LLC_RESP);
840 rc = smc_llc_cli_rkey_exchange(link, lnk_new);
845 rc = smc_llc_cli_conf_link(link, &ini, lnk_new, lgr_new_t);
849 smcr_link_clear(lnk_new);
851 smc_llc_cli_add_link_reject(qentry);
857 static void smc_llc_process_cli_add_link(struct smc_link_group *lgr)
859 struct smc_llc_qentry *qentry;
861 qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
863 mutex_lock(&lgr->llc_conf_mutex);
864 smc_llc_cli_add_link(qentry->link, qentry);
865 mutex_unlock(&lgr->llc_conf_mutex);
868 static int smc_llc_active_link_count(struct smc_link_group *lgr)
870 int i, link_count = 0;
872 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
873 if (!smc_link_usable(&lgr->lnk[i]))
880 /* find the asymmetric link when 3 links are established */
881 static struct smc_link *smc_llc_find_asym_link(struct smc_link_group *lgr)
883 int asym_idx = -ENOENT;
887 /* determine asymmetric link */
889 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
890 for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
891 if (!smc_link_usable(&lgr->lnk[i]) ||
892 !smc_link_usable(&lgr->lnk[j]))
894 if (!memcmp(lgr->lnk[i].gid, lgr->lnk[j].gid,
896 found = true; /* asym_lnk is i or j */
904 goto out; /* no asymmetric link */
905 for (k = 0; k < SMC_LINKS_PER_LGR_MAX; k++) {
906 if (!smc_link_usable(&lgr->lnk[k]))
909 !memcmp(lgr->lnk[i].peer_gid, lgr->lnk[k].peer_gid,
915 !memcmp(lgr->lnk[j].peer_gid, lgr->lnk[k].peer_gid,
922 return (asym_idx < 0) ? NULL : &lgr->lnk[asym_idx];
925 static void smc_llc_delete_asym_link(struct smc_link_group *lgr)
927 struct smc_link *lnk_new = NULL, *lnk_asym;
928 struct smc_llc_qentry *qentry;
931 lnk_asym = smc_llc_find_asym_link(lgr);
933 return; /* no asymmetric link */
934 if (!smc_link_downing(&lnk_asym->state))
936 /* tbd: lnk_new = smc_switch_conns(lgr, lnk_asym, false); */
937 smc_wr_tx_wait_no_pending_sends(lnk_asym);
940 /* change flow type from ADD_LINK into DEL_LINK */
941 lgr->llc_flow_lcl.type = SMC_LLC_FLOW_DEL_LINK;
942 rc = smc_llc_send_delete_link(lnk_new, lnk_asym->link_id, SMC_LLC_REQ,
943 true, SMC_LLC_DEL_NO_ASYM_NEEDED);
945 smcr_link_down_cond(lnk_new);
948 qentry = smc_llc_wait(lgr, lnk_new, SMC_LLC_WAIT_TIME,
949 SMC_LLC_DELETE_LINK);
951 smcr_link_down_cond(lnk_new);
954 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
956 smcr_link_clear(lnk_asym);
959 static int smc_llc_srv_rkey_exchange(struct smc_link *link,
960 struct smc_link *link_new)
962 struct smc_llc_msg_add_link_cont *addc_llc;
963 struct smc_link_group *lgr = link->lgr;
964 u8 max, num_rkeys_send, num_rkeys_recv;
965 struct smc_llc_qentry *qentry = NULL;
966 struct smc_buf_desc *buf_pos;
971 mutex_lock(&lgr->rmbs_lock);
972 num_rkeys_send = lgr->conns_num;
973 buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
975 smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
977 qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME,
978 SMC_LLC_ADD_LINK_CONT);
983 addc_llc = &qentry->msg.add_link_cont;
984 num_rkeys_recv = addc_llc->num_rkeys;
985 max = min_t(u8, num_rkeys_recv, SMC_LLC_RKEYS_PER_CONT_MSG);
986 for (i = 0; i < max; i++) {
987 smc_rtoken_set(lgr, link->link_idx, link_new->link_idx,
988 addc_llc->rt[i].rmb_key,
989 addc_llc->rt[i].rmb_vaddr_new,
990 addc_llc->rt[i].rmb_key_new);
993 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
994 } while (num_rkeys_send || num_rkeys_recv);
996 mutex_unlock(&lgr->rmbs_lock);
1000 static int smc_llc_srv_conf_link(struct smc_link *link,
1001 struct smc_link *link_new,
1002 enum smc_lgr_type lgr_new_t)
1004 struct smc_link_group *lgr = link->lgr;
1005 struct smc_llc_qentry *qentry = NULL;
1008 /* send CONFIRM LINK request over the RoCE fabric */
1009 rc = smc_llc_send_confirm_link(link_new, SMC_LLC_REQ);
1012 /* receive CONFIRM LINK response over the RoCE fabric */
1013 qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_FIRST_TIME,
1014 SMC_LLC_CONFIRM_LINK);
1016 /* send DELETE LINK */
1017 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
1018 false, SMC_LLC_DEL_LOST_PATH);
1021 smc_llc_link_active(link_new);
1022 lgr->type = lgr_new_t;
1023 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1027 int smc_llc_srv_add_link(struct smc_link *link)
1029 enum smc_lgr_type lgr_new_t = SMC_LGR_SYMMETRIC;
1030 struct smc_link_group *lgr = link->lgr;
1031 struct smc_llc_msg_add_link *add_llc;
1032 struct smc_llc_qentry *qentry = NULL;
1033 struct smc_link *link_new;
1034 struct smc_init_info ini;
1035 int lnk_idx, rc = 0;
1037 /* ignore client add link recommendation, start new flow */
1038 ini.vlan_id = lgr->vlan_id;
1039 smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
1041 lgr_new_t = SMC_LGR_ASYMMETRIC_LOCAL;
1042 ini.ib_dev = link->smcibdev;
1043 ini.ib_port = link->ibport;
1045 lnk_idx = smc_llc_alloc_alt_link(lgr, lgr_new_t);
1049 rc = smcr_link_init(lgr, &lgr->lnk[lnk_idx], lnk_idx, &ini);
1052 link_new = &lgr->lnk[lnk_idx];
1053 rc = smc_llc_send_add_link(link,
1054 link_new->smcibdev->mac[ini.ib_port - 1],
1055 link_new->gid, link_new, SMC_LLC_REQ);
1058 /* receive ADD LINK response over the RoCE fabric */
1059 qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME, SMC_LLC_ADD_LINK);
1064 add_llc = &qentry->msg.add_link;
1065 if (add_llc->hd.flags & SMC_LLC_FLAG_ADD_LNK_REJ) {
1066 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1070 if (lgr->type == SMC_LGR_SINGLE &&
1071 (!memcmp(add_llc->sender_gid, link->peer_gid, SMC_GID_SIZE) &&
1072 !memcmp(add_llc->sender_mac, link->peer_mac, ETH_ALEN))) {
1073 lgr_new_t = SMC_LGR_ASYMMETRIC_PEER;
1075 smc_llc_save_add_link_info(link_new, add_llc);
1076 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1078 rc = smc_ib_ready_link(link_new);
1081 rc = smcr_buf_map_lgr(link_new);
1084 rc = smcr_buf_reg_lgr(link_new);
1087 rc = smc_llc_srv_rkey_exchange(link, link_new);
1090 rc = smc_llc_srv_conf_link(link, link_new, lgr_new_t);
1095 smcr_link_clear(link_new);
1099 static void smc_llc_process_srv_add_link(struct smc_link_group *lgr)
1101 struct smc_link *link = lgr->llc_flow_lcl.qentry->link;
1104 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1106 mutex_lock(&lgr->llc_conf_mutex);
1107 rc = smc_llc_srv_add_link(link);
1108 if (!rc && lgr->type == SMC_LGR_SYMMETRIC) {
1109 /* delete any asymmetric link */
1110 smc_llc_delete_asym_link(lgr);
1112 mutex_unlock(&lgr->llc_conf_mutex);
1115 /* enqueue a local add_link req to trigger a new add_link flow, only as SERV */
1116 void smc_llc_srv_add_link_local(struct smc_link *link)
1118 struct smc_llc_msg_add_link add_llc = {0};
1120 add_llc.hd.length = sizeof(add_llc);
1121 add_llc.hd.common.type = SMC_LLC_ADD_LINK;
1122 /* no dev and port needed, we as server ignore client data anyway */
1123 smc_llc_enqueue(link, (union smc_llc_msg *)&add_llc);
1126 /* worker to process an add link message */
1127 static void smc_llc_add_link_work(struct work_struct *work)
1129 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1132 if (list_empty(&lgr->list)) {
1133 /* link group is terminating */
1134 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1138 if (lgr->role == SMC_CLNT)
1139 smc_llc_process_cli_add_link(lgr);
1141 smc_llc_process_srv_add_link(lgr);
1143 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1146 /* enqueue a local del_link msg to trigger a new del_link flow,
1147 * called only for role SMC_SERV
1149 void smc_llc_srv_delete_link_local(struct smc_link *link, u8 del_link_id)
1151 struct smc_llc_msg_del_link del_llc = {0};
1153 del_llc.hd.length = sizeof(del_llc);
1154 del_llc.hd.common.type = SMC_LLC_DELETE_LINK;
1155 del_llc.link_num = del_link_id;
1156 del_llc.reason = htonl(SMC_LLC_DEL_LOST_PATH);
1157 del_llc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
1158 smc_llc_enqueue(link, (union smc_llc_msg *)&del_llc);
1161 static void smc_llc_process_cli_delete_link(struct smc_link_group *lgr)
1163 struct smc_link *lnk_del = NULL, *lnk_asym, *lnk;
1164 struct smc_llc_msg_del_link *del_llc;
1165 struct smc_llc_qentry *qentry;
1169 qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
1171 del_llc = &qentry->msg.delete_link;
1173 if (del_llc->hd.flags & SMC_LLC_FLAG_DEL_LINK_ALL) {
1174 smc_lgr_terminate_sched(lgr);
1177 mutex_lock(&lgr->llc_conf_mutex);
1178 /* delete single link */
1179 for (lnk_idx = 0; lnk_idx < SMC_LINKS_PER_LGR_MAX; lnk_idx++) {
1180 if (lgr->lnk[lnk_idx].link_id != del_llc->link_num)
1182 lnk_del = &lgr->lnk[lnk_idx];
1185 del_llc->hd.flags |= SMC_LLC_FLAG_RESP;
1187 /* link was not found */
1188 del_llc->reason = htonl(SMC_LLC_DEL_NOLNK);
1189 smc_llc_send_message(lnk, &qentry->msg);
1192 lnk_asym = smc_llc_find_asym_link(lgr);
1194 del_llc->reason = 0;
1195 smc_llc_send_message(lnk, &qentry->msg); /* response */
1197 if (smc_link_downing(&lnk_del->state)) {
1198 /* tbd: call smc_switch_conns(lgr, lnk_del, false); */
1199 smc_wr_tx_wait_no_pending_sends(lnk_del);
1201 smcr_link_clear(lnk_del);
1203 active_links = smc_llc_active_link_count(lgr);
1204 if (lnk_del == lnk_asym) {
1205 /* expected deletion of asym link, don't change lgr state */
1206 } else if (active_links == 1) {
1207 lgr->type = SMC_LGR_SINGLE;
1208 } else if (!active_links) {
1209 lgr->type = SMC_LGR_NONE;
1210 smc_lgr_terminate_sched(lgr);
1213 mutex_unlock(&lgr->llc_conf_mutex);
1218 static void smc_llc_process_srv_delete_link(struct smc_link_group *lgr)
1220 struct smc_llc_msg_del_link *del_llc;
1221 struct smc_link *lnk, *lnk_del;
1222 struct smc_llc_qentry *qentry;
1226 mutex_lock(&lgr->llc_conf_mutex);
1227 qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
1229 del_llc = &qentry->msg.delete_link;
1231 if (qentry->msg.delete_link.hd.flags & SMC_LLC_FLAG_DEL_LINK_ALL) {
1232 /* delete entire lgr */
1233 smc_lgr_terminate_sched(lgr);
1236 /* delete single link */
1238 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1239 if (lgr->lnk[i].link_id == del_llc->link_num) {
1240 lnk_del = &lgr->lnk[i];
1245 goto out; /* asymmetric link already deleted */
1247 if (smc_link_downing(&lnk_del->state)) {
1248 /* tbd: call smc_switch_conns(lgr, lnk_del, false); */
1249 smc_wr_tx_wait_no_pending_sends(lnk_del);
1251 if (!list_empty(&lgr->list)) {
1252 /* qentry is either a request from peer (send it back to
1253 * initiate the DELETE_LINK processing), or a locally
1254 * enqueued DELETE_LINK request (forward it)
1256 if (!smc_llc_send_message(lnk, &qentry->msg)) {
1257 struct smc_llc_msg_del_link *del_llc_resp;
1258 struct smc_llc_qentry *qentry2;
1260 qentry2 = smc_llc_wait(lgr, lnk, SMC_LLC_WAIT_TIME,
1261 SMC_LLC_DELETE_LINK);
1264 del_llc_resp = &qentry2->msg.delete_link;
1265 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1269 smcr_link_clear(lnk_del);
1271 active_links = smc_llc_active_link_count(lgr);
1272 if (active_links == 1) {
1273 lgr->type = SMC_LGR_SINGLE;
1274 } else if (!active_links) {
1275 lgr->type = SMC_LGR_NONE;
1276 smc_lgr_terminate_sched(lgr);
1279 if (lgr->type == SMC_LGR_SINGLE && !list_empty(&lgr->list)) {
1280 /* trigger setup of asymm alt link */
1281 smc_llc_srv_add_link_local(lnk);
1284 mutex_unlock(&lgr->llc_conf_mutex);
1288 static void smc_llc_delete_link_work(struct work_struct *work)
1290 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1293 if (list_empty(&lgr->list)) {
1294 /* link group is terminating */
1295 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1299 if (lgr->role == SMC_CLNT)
1300 smc_llc_process_cli_delete_link(lgr);
1302 smc_llc_process_srv_delete_link(lgr);
1304 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1307 /* process a confirm_rkey request from peer, remote flow */
1308 static void smc_llc_rmt_conf_rkey(struct smc_link_group *lgr)
1310 struct smc_llc_msg_confirm_rkey *llc;
1311 struct smc_llc_qentry *qentry;
1312 struct smc_link *link;
1317 qentry = lgr->llc_flow_rmt.qentry;
1318 llc = &qentry->msg.confirm_rkey;
1319 link = qentry->link;
1321 num_entries = llc->rtoken[0].num_rkeys;
1322 /* first rkey entry is for receiving link */
1323 rk_idx = smc_rtoken_add(link,
1324 llc->rtoken[0].rmb_vaddr,
1325 llc->rtoken[0].rmb_key);
1329 for (i = 1; i <= min_t(u8, num_entries, SMC_LLC_RKEYS_PER_MSG - 1); i++)
1330 smc_rtoken_set2(lgr, rk_idx, llc->rtoken[i].link_id,
1331 llc->rtoken[i].rmb_vaddr,
1332 llc->rtoken[i].rmb_key);
1333 /* max links is 3 so there is no need to support conf_rkey_cont msgs */
1336 llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
1337 llc->hd.flags |= SMC_LLC_FLAG_RKEY_RETRY;
1339 llc->hd.flags |= SMC_LLC_FLAG_RESP;
1340 smc_llc_send_message(link, &qentry->msg);
1341 smc_llc_flow_qentry_del(&lgr->llc_flow_rmt);
1344 /* process a delete_rkey request from peer, remote flow */
1345 static void smc_llc_rmt_delete_rkey(struct smc_link_group *lgr)
1347 struct smc_llc_msg_delete_rkey *llc;
1348 struct smc_llc_qentry *qentry;
1349 struct smc_link *link;
1353 qentry = lgr->llc_flow_rmt.qentry;
1354 llc = &qentry->msg.delete_rkey;
1355 link = qentry->link;
1357 max = min_t(u8, llc->num_rkeys, SMC_LLC_DEL_RKEY_MAX);
1358 for (i = 0; i < max; i++) {
1359 if (smc_rtoken_delete(link, llc->rkey[i]))
1360 err_mask |= 1 << (SMC_LLC_DEL_RKEY_MAX - 1 - i);
1363 llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
1364 llc->err_mask = err_mask;
1366 llc->hd.flags |= SMC_LLC_FLAG_RESP;
1367 smc_llc_send_message(link, &qentry->msg);
1368 smc_llc_flow_qentry_del(&lgr->llc_flow_rmt);
1371 /* flush the llc event queue */
1372 static void smc_llc_event_flush(struct smc_link_group *lgr)
1374 struct smc_llc_qentry *qentry, *q;
1376 spin_lock_bh(&lgr->llc_event_q_lock);
1377 list_for_each_entry_safe(qentry, q, &lgr->llc_event_q, list) {
1378 list_del_init(&qentry->list);
1381 spin_unlock_bh(&lgr->llc_event_q_lock);
1384 static void smc_llc_event_handler(struct smc_llc_qentry *qentry)
1386 union smc_llc_msg *llc = &qentry->msg;
1387 struct smc_link *link = qentry->link;
1388 struct smc_link_group *lgr = link->lgr;
1390 if (!smc_link_usable(link))
1393 switch (llc->raw.hdr.common.type) {
1394 case SMC_LLC_TEST_LINK:
1395 llc->test_link.hd.flags |= SMC_LLC_FLAG_RESP;
1396 smc_llc_send_message(link, llc);
1398 case SMC_LLC_ADD_LINK:
1399 if (list_empty(&lgr->list))
1400 goto out; /* lgr is terminating */
1401 if (lgr->role == SMC_CLNT) {
1402 if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_ADD_LINK) {
1403 /* a flow is waiting for this message */
1404 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl,
1406 wake_up_interruptible(&lgr->llc_waiter);
1407 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1409 schedule_work(&lgr->llc_add_link_work);
1411 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl, qentry)) {
1412 /* as smc server, handle client suggestion */
1413 schedule_work(&lgr->llc_add_link_work);
1416 case SMC_LLC_CONFIRM_LINK:
1417 case SMC_LLC_ADD_LINK_CONT:
1418 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1419 /* a flow is waiting for this message */
1420 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl, qentry);
1421 wake_up_interruptible(&lgr->llc_waiter);
1425 case SMC_LLC_DELETE_LINK:
1426 if (lgr->role == SMC_CLNT) {
1427 /* server requests to delete this link, send response */
1428 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1429 /* DEL LINK REQ during ADD LINK SEQ */
1430 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl,
1432 wake_up_interruptible(&lgr->llc_waiter);
1433 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1435 schedule_work(&lgr->llc_del_link_work);
1438 if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_ADD_LINK &&
1439 !lgr->llc_flow_lcl.qentry) {
1440 /* DEL LINK REQ during ADD LINK SEQ */
1441 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl,
1443 wake_up_interruptible(&lgr->llc_waiter);
1444 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1446 schedule_work(&lgr->llc_del_link_work);
1450 case SMC_LLC_CONFIRM_RKEY:
1451 /* new request from remote, assign to remote flow */
1452 if (smc_llc_flow_start(&lgr->llc_flow_rmt, qentry)) {
1453 /* process here, does not wait for more llc msgs */
1454 smc_llc_rmt_conf_rkey(lgr);
1455 smc_llc_flow_stop(lgr, &lgr->llc_flow_rmt);
1458 case SMC_LLC_CONFIRM_RKEY_CONT:
1459 /* not used because max links is 3, and 3 rkeys fit into
1460 * one CONFIRM_RKEY message
1463 case SMC_LLC_DELETE_RKEY:
1464 /* new request from remote, assign to remote flow */
1465 if (smc_llc_flow_start(&lgr->llc_flow_rmt, qentry)) {
1466 /* process here, does not wait for more llc msgs */
1467 smc_llc_rmt_delete_rkey(lgr);
1468 smc_llc_flow_stop(lgr, &lgr->llc_flow_rmt);
1476 /* worker to process llc messages on the event queue */
1477 static void smc_llc_event_work(struct work_struct *work)
1479 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1481 struct smc_llc_qentry *qentry;
1483 if (!lgr->llc_flow_lcl.type && lgr->delayed_event) {
1484 if (smc_link_usable(lgr->delayed_event->link)) {
1485 smc_llc_event_handler(lgr->delayed_event);
1487 qentry = lgr->delayed_event;
1488 lgr->delayed_event = NULL;
1494 spin_lock_bh(&lgr->llc_event_q_lock);
1495 if (!list_empty(&lgr->llc_event_q)) {
1496 qentry = list_first_entry(&lgr->llc_event_q,
1497 struct smc_llc_qentry, list);
1498 list_del_init(&qentry->list);
1499 spin_unlock_bh(&lgr->llc_event_q_lock);
1500 smc_llc_event_handler(qentry);
1503 spin_unlock_bh(&lgr->llc_event_q_lock);
1506 /* process llc responses in tasklet context */
1507 static void smc_llc_rx_response(struct smc_link *link,
1508 struct smc_llc_qentry *qentry)
1510 u8 llc_type = qentry->msg.raw.hdr.common.type;
1513 case SMC_LLC_TEST_LINK:
1514 if (link->state == SMC_LNK_ACTIVE)
1515 complete(&link->llc_testlink_resp);
1517 case SMC_LLC_ADD_LINK:
1518 case SMC_LLC_DELETE_LINK:
1519 case SMC_LLC_CONFIRM_LINK:
1520 case SMC_LLC_ADD_LINK_CONT:
1521 case SMC_LLC_CONFIRM_RKEY:
1522 case SMC_LLC_DELETE_RKEY:
1523 /* assign responses to the local flow, we requested them */
1524 smc_llc_flow_qentry_set(&link->lgr->llc_flow_lcl, qentry);
1525 wake_up_interruptible(&link->lgr->llc_waiter);
1527 case SMC_LLC_CONFIRM_RKEY_CONT:
1528 /* not used because max links is 3 */
1534 static void smc_llc_enqueue(struct smc_link *link, union smc_llc_msg *llc)
1536 struct smc_link_group *lgr = link->lgr;
1537 struct smc_llc_qentry *qentry;
1538 unsigned long flags;
1540 qentry = kmalloc(sizeof(*qentry), GFP_ATOMIC);
1543 qentry->link = link;
1544 INIT_LIST_HEAD(&qentry->list);
1545 memcpy(&qentry->msg, llc, sizeof(union smc_llc_msg));
1547 /* process responses immediately */
1548 if (llc->raw.hdr.flags & SMC_LLC_FLAG_RESP) {
1549 smc_llc_rx_response(link, qentry);
1553 /* add requests to event queue */
1554 spin_lock_irqsave(&lgr->llc_event_q_lock, flags);
1555 list_add_tail(&qentry->list, &lgr->llc_event_q);
1556 spin_unlock_irqrestore(&lgr->llc_event_q_lock, flags);
1557 schedule_work(&link->lgr->llc_event_work);
1560 /* copy received msg and add it to the event queue */
1561 static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
1563 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
1564 union smc_llc_msg *llc = buf;
1566 if (wc->byte_len < sizeof(*llc))
1567 return; /* short message */
1568 if (llc->raw.hdr.length != sizeof(*llc))
1569 return; /* invalid message */
1571 smc_llc_enqueue(link, llc);
1574 /***************************** worker, utils *********************************/
1576 static void smc_llc_testlink_work(struct work_struct *work)
1578 struct smc_link *link = container_of(to_delayed_work(work),
1579 struct smc_link, llc_testlink_wrk);
1580 unsigned long next_interval;
1581 unsigned long expire_time;
1582 u8 user_data[16] = { 0 };
1585 if (link->state != SMC_LNK_ACTIVE)
1586 return; /* don't reschedule worker */
1587 expire_time = link->wr_rx_tstamp + link->llc_testlink_time;
1588 if (time_is_after_jiffies(expire_time)) {
1589 next_interval = expire_time - jiffies;
1592 reinit_completion(&link->llc_testlink_resp);
1593 smc_llc_send_test_link(link, user_data);
1594 /* receive TEST LINK response over RoCE fabric */
1595 rc = wait_for_completion_interruptible_timeout(&link->llc_testlink_resp,
1597 if (link->state != SMC_LNK_ACTIVE)
1598 return; /* link state changed */
1600 smcr_link_down_cond_sched(link);
1603 next_interval = link->llc_testlink_time;
1605 schedule_delayed_work(&link->llc_testlink_wrk, next_interval);
1608 void smc_llc_lgr_init(struct smc_link_group *lgr, struct smc_sock *smc)
1610 struct net *net = sock_net(smc->clcsock->sk);
1612 INIT_WORK(&lgr->llc_event_work, smc_llc_event_work);
1613 INIT_WORK(&lgr->llc_add_link_work, smc_llc_add_link_work);
1614 INIT_WORK(&lgr->llc_del_link_work, smc_llc_delete_link_work);
1615 INIT_LIST_HEAD(&lgr->llc_event_q);
1616 spin_lock_init(&lgr->llc_event_q_lock);
1617 spin_lock_init(&lgr->llc_flow_lock);
1618 init_waitqueue_head(&lgr->llc_waiter);
1619 mutex_init(&lgr->llc_conf_mutex);
1620 lgr->llc_testlink_time = net->ipv4.sysctl_tcp_keepalive_time;
1623 /* called after lgr was removed from lgr_list */
1624 void smc_llc_lgr_clear(struct smc_link_group *lgr)
1626 smc_llc_event_flush(lgr);
1627 wake_up_interruptible_all(&lgr->llc_waiter);
1628 cancel_work_sync(&lgr->llc_event_work);
1629 cancel_work_sync(&lgr->llc_add_link_work);
1630 cancel_work_sync(&lgr->llc_del_link_work);
1631 if (lgr->delayed_event) {
1632 kfree(lgr->delayed_event);
1633 lgr->delayed_event = NULL;
1637 int smc_llc_link_init(struct smc_link *link)
1639 init_completion(&link->llc_testlink_resp);
1640 INIT_DELAYED_WORK(&link->llc_testlink_wrk, smc_llc_testlink_work);
1644 void smc_llc_link_active(struct smc_link *link)
1646 link->state = SMC_LNK_ACTIVE;
1647 if (link->lgr->llc_testlink_time) {
1648 link->llc_testlink_time = link->lgr->llc_testlink_time * HZ;
1649 schedule_delayed_work(&link->llc_testlink_wrk,
1650 link->llc_testlink_time);
1654 /* called in worker context */
1655 void smc_llc_link_clear(struct smc_link *link)
1657 complete(&link->llc_testlink_resp);
1658 cancel_delayed_work_sync(&link->llc_testlink_wrk);
1659 smc_wr_wakeup_reg_wait(link);
1660 smc_wr_wakeup_tx_wait(link);
1663 /* register a new rtoken at the remote peer (for all links) */
1664 int smc_llc_do_confirm_rkey(struct smc_link *send_link,
1665 struct smc_buf_desc *rmb_desc)
1667 struct smc_link_group *lgr = send_link->lgr;
1668 struct smc_llc_qentry *qentry = NULL;
1671 rc = smc_llc_send_confirm_rkey(send_link, rmb_desc);
1674 /* receive CONFIRM RKEY response from server over RoCE fabric */
1675 qentry = smc_llc_wait(lgr, send_link, SMC_LLC_WAIT_TIME,
1676 SMC_LLC_CONFIRM_RKEY);
1677 if (!qentry || (qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_RKEY_NEG))
1681 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1685 /* unregister an rtoken at the remote peer */
1686 int smc_llc_do_delete_rkey(struct smc_link_group *lgr,
1687 struct smc_buf_desc *rmb_desc)
1689 struct smc_llc_qentry *qentry = NULL;
1690 struct smc_link *send_link;
1693 send_link = smc_llc_usable_link(lgr);
1697 /* protected by llc_flow control */
1698 rc = smc_llc_send_delete_rkey(send_link, rmb_desc);
1701 /* receive DELETE RKEY response from server over RoCE fabric */
1702 qentry = smc_llc_wait(lgr, send_link, SMC_LLC_WAIT_TIME,
1703 SMC_LLC_DELETE_RKEY);
1704 if (!qentry || (qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_RKEY_NEG))
1708 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1712 /* evaluate confirm link request or response */
1713 int smc_llc_eval_conf_link(struct smc_llc_qentry *qentry,
1714 enum smc_llc_reqresp type)
1716 if (type == SMC_LLC_REQ) /* SMC server assigns link_id */
1717 qentry->link->link_id = qentry->msg.confirm_link.link_num;
1718 if (!(qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_NO_RMBE_EYEC))
1723 /***************************** init, exit, misc ******************************/
1725 static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
1727 .handler = smc_llc_rx_handler,
1728 .type = SMC_LLC_CONFIRM_LINK
1731 .handler = smc_llc_rx_handler,
1732 .type = SMC_LLC_TEST_LINK
1735 .handler = smc_llc_rx_handler,
1736 .type = SMC_LLC_ADD_LINK
1739 .handler = smc_llc_rx_handler,
1740 .type = SMC_LLC_ADD_LINK_CONT
1743 .handler = smc_llc_rx_handler,
1744 .type = SMC_LLC_DELETE_LINK
1747 .handler = smc_llc_rx_handler,
1748 .type = SMC_LLC_CONFIRM_RKEY
1751 .handler = smc_llc_rx_handler,
1752 .type = SMC_LLC_CONFIRM_RKEY_CONT
1755 .handler = smc_llc_rx_handler,
1756 .type = SMC_LLC_DELETE_RKEY
1763 int __init smc_llc_init(void)
1765 struct smc_wr_rx_handler *handler;
1768 for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
1769 INIT_HLIST_NODE(&handler->list);
1770 rc = smc_wr_rx_register_handler(handler);