smc_llc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * For now, we only support the necessary "confirm link" functionality
  8. * which happens for the first RoCE link after successful CLC handshake.
  9. *
  10. * Copyright IBM Corp. 2016
  11. *
  12. * Author(s): Klaus Wacker <Klaus.Wacker@de.ibm.com>
  13. * Ursula Braun <ubraun@linux.vnet.ibm.com>
  14. */
  15. #include <net/tcp.h>
  16. #include <rdma/ib_verbs.h>
  17. #include "smc.h"
  18. #include "smc_core.h"
  19. #include "smc_clc.h"
  20. #include "smc_llc.h"
  21. /********************************** send *************************************/
  22. struct smc_llc_tx_pend {
  23. };
  24. /* handler for send/transmission completion of an LLC msg */
  25. static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
  26. struct smc_link *link,
  27. enum ib_wc_status wc_status)
  28. {
  29. /* future work: handle wc_status error for recovery and failover */
  30. }
  31. /**
  32. * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
  33. * @link: Pointer to SMC link used for sending LLC control message.
  34. * @wr_buf: Out variable returning pointer to work request payload buffer.
  35. * @pend: Out variable returning pointer to private pending WR tracking.
  36. * It's the context the transmit complete handler will get.
  37. *
  38. * Reserves and pre-fills an entry for a pending work request send/tx.
  39. * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
  40. * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
  41. *
  42. * Return: 0 on success, otherwise an error value.
  43. */
  44. static int smc_llc_add_pending_send(struct smc_link *link,
  45. struct smc_wr_buf **wr_buf,
  46. struct smc_wr_tx_pend_priv **pend)
  47. {
  48. int rc;
  49. rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, pend);
  50. if (rc < 0)
  51. return rc;
  52. BUILD_BUG_ON_MSG(
  53. sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
  54. "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
  55. BUILD_BUG_ON_MSG(
  56. sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
  57. "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()");
  58. BUILD_BUG_ON_MSG(
  59. sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
  60. "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
  61. return 0;
  62. }
  63. /* high-level API to send LLC confirm link */
  64. int smc_llc_send_confirm_link(struct smc_link *link, u8 mac[],
  65. union ib_gid *gid,
  66. enum smc_llc_reqresp reqresp)
  67. {
  68. struct smc_link_group *lgr = container_of(link, struct smc_link_group,
  69. lnk[SMC_SINGLE_LINK]);
  70. struct smc_llc_msg_confirm_link *confllc;
  71. struct smc_wr_tx_pend_priv *pend;
  72. struct smc_wr_buf *wr_buf;
  73. int rc;
  74. rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
  75. if (rc)
  76. return rc;
  77. confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
  78. memset(confllc, 0, sizeof(*confllc));
  79. confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
  80. confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
  81. if (reqresp == SMC_LLC_RESP)
  82. confllc->hd.flags |= SMC_LLC_FLAG_RESP;
  83. memcpy(confllc->sender_mac, mac, ETH_ALEN);
  84. memcpy(confllc->sender_gid, gid, SMC_GID_SIZE);
  85. hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
  86. confllc->link_num = link->link_id;
  87. memcpy(confllc->link_uid, lgr->id, SMC_LGR_ID_SIZE);
  88. confllc->max_links = SMC_LINKS_PER_LGR_MAX;
  89. /* send llc message */
  90. rc = smc_wr_tx_send(link, pend);
  91. return rc;
  92. }
  93. /********************************* receive ***********************************/
  94. static void smc_llc_rx_confirm_link(struct smc_link *link,
  95. struct smc_llc_msg_confirm_link *llc)
  96. {
  97. struct smc_link_group *lgr;
  98. lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
  99. if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
  100. if (lgr->role == SMC_SERV)
  101. complete(&link->llc_confirm_resp);
  102. } else {
  103. if (lgr->role == SMC_CLNT) {
  104. link->link_id = llc->link_num;
  105. complete(&link->llc_confirm);
  106. }
  107. }
  108. }
  109. static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
  110. {
  111. struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
  112. union smc_llc_msg *llc = buf;
  113. if (wc->byte_len < sizeof(*llc))
  114. return; /* short message */
  115. if (llc->raw.hdr.length != sizeof(*llc))
  116. return; /* invalid message */
  117. if (llc->raw.hdr.common.type == SMC_LLC_CONFIRM_LINK)
  118. smc_llc_rx_confirm_link(link, &llc->confirm_link);
  119. }
  120. /***************************** init, exit, misc ******************************/
  121. static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
  122. {
  123. .handler = smc_llc_rx_handler,
  124. .type = SMC_LLC_CONFIRM_LINK
  125. },
  126. {
  127. .handler = NULL,
  128. }
  129. };
  130. int __init smc_llc_init(void)
  131. {
  132. struct smc_wr_rx_handler *handler;
  133. int rc = 0;
  134. for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
  135. INIT_HLIST_NODE(&handler->list);
  136. rc = smc_wr_rx_register_handler(handler);
  137. if (rc)
  138. break;
  139. }
  140. return rc;
  141. }