smc_core.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  4. *
  5. * Definitions for SMC Connections, Link Groups and Links
  6. *
  7. * Copyright IBM Corp. 2016
  8. *
  9. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  10. */
  11. #ifndef _SMC_CORE_H
  12. #define _SMC_CORE_H
  13. #include <linux/atomic.h>
  14. #include <rdma/ib_verbs.h>
  15. #include "smc.h"
  16. #include "smc_ib.h"
  17. #define SMC_RMBS_PER_LGR_MAX 255 /* max. # of RMBs per link group */
  18. struct smc_lgr_list { /* list of link group definition */
  19. struct list_head list;
  20. spinlock_t lock; /* protects list of link groups */
  21. };
  22. extern struct smc_lgr_list smc_lgr_list; /* list of link groups */
  23. enum smc_lgr_role { /* possible roles of a link group */
  24. SMC_CLNT, /* client */
  25. SMC_SERV /* server */
  26. };
  27. #define SMC_WR_BUF_SIZE 48 /* size of work request buffer */
  28. struct smc_wr_buf {
  29. u8 raw[SMC_WR_BUF_SIZE];
  30. };
  31. #define SMC_WR_REG_MR_WAIT_TIME (5 * HZ)/* wait time for ib_wr_reg_mr result */
  32. enum smc_wr_reg_state {
  33. POSTED, /* ib_wr_reg_mr request posted */
  34. CONFIRMED, /* ib_wr_reg_mr response: successful */
  35. FAILED /* ib_wr_reg_mr response: failure */
  36. };
  37. struct smc_link {
  38. struct smc_ib_device *smcibdev; /* ib-device */
  39. u8 ibport; /* port - values 1 | 2 */
  40. struct ib_pd *roce_pd; /* IB protection domain,
  41. * unique for every RoCE QP
  42. */
  43. struct ib_qp *roce_qp; /* IB queue pair */
  44. struct ib_qp_attr qp_attr; /* IB queue pair attributes */
  45. struct smc_wr_buf *wr_tx_bufs; /* WR send payload buffers */
  46. struct ib_send_wr *wr_tx_ibs; /* WR send meta data */
  47. struct ib_sge *wr_tx_sges; /* WR send gather meta data */
  48. struct smc_wr_tx_pend *wr_tx_pends; /* WR send waiting for CQE */
  49. /* above four vectors have wr_tx_cnt elements and use the same index */
  50. dma_addr_t wr_tx_dma_addr; /* DMA address of wr_tx_bufs */
  51. atomic_long_t wr_tx_id; /* seq # of last sent WR */
  52. unsigned long *wr_tx_mask; /* bit mask of used indexes */
  53. u32 wr_tx_cnt; /* number of WR send buffers */
  54. wait_queue_head_t wr_tx_wait; /* wait for free WR send buf */
  55. struct smc_wr_buf *wr_rx_bufs; /* WR recv payload buffers */
  56. struct ib_recv_wr *wr_rx_ibs; /* WR recv meta data */
  57. struct ib_sge *wr_rx_sges; /* WR recv scatter meta data */
  58. /* above three vectors have wr_rx_cnt elements and use the same index */
  59. dma_addr_t wr_rx_dma_addr; /* DMA address of wr_rx_bufs */
  60. u64 wr_rx_id; /* seq # of last recv WR */
  61. u32 wr_rx_cnt; /* number of WR recv buffers */
  62. struct ib_reg_wr wr_reg; /* WR register memory region */
  63. wait_queue_head_t wr_reg_wait; /* wait for wr_reg result */
  64. enum smc_wr_reg_state wr_reg_state; /* state of wr_reg request */
  65. union ib_gid gid; /* gid matching used vlan id */
  66. u32 peer_qpn; /* QP number of peer */
  67. enum ib_mtu path_mtu; /* used mtu */
  68. enum ib_mtu peer_mtu; /* mtu size of peer */
  69. u32 psn_initial; /* QP tx initial packet seqno */
  70. u32 peer_psn; /* QP rx initial packet seqno */
  71. u8 peer_mac[ETH_ALEN]; /* = gid[8:10||13:15] */
  72. u8 peer_gid[sizeof(union ib_gid)]; /* gid of peer*/
  73. u8 link_id; /* unique # within link group */
  74. struct completion llc_confirm; /* wait for rx of conf link */
  75. struct completion llc_confirm_resp; /* wait 4 rx of cnf lnk rsp */
  76. };
  77. /* For now we just allow one parallel link per link group. The SMC protocol
  78. * allows more (up to 8).
  79. */
  80. #define SMC_LINKS_PER_LGR_MAX 1
  81. #define SMC_SINGLE_LINK 0
  82. #define SMC_FIRST_CONTACT 1 /* first contact to a peer */
  83. #define SMC_REUSE_CONTACT 0 /* follow-on contact to a peer*/
  84. /* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
  85. struct smc_buf_desc {
  86. struct list_head list;
  87. void *cpu_addr; /* virtual address of buffer */
  88. struct sg_table sgt[SMC_LINKS_PER_LGR_MAX];/* virtual buffer */
  89. struct ib_mr *mr_rx[SMC_LINKS_PER_LGR_MAX];
  90. /* for rmb only: memory region
  91. * incl. rkey provided to peer
  92. */
  93. u32 order; /* allocation order */
  94. u32 used; /* currently used / unused */
  95. bool reused; /* new created / reused */
  96. };
  97. struct smc_rtoken { /* address/key of remote RMB */
  98. u64 dma_addr;
  99. u32 rkey;
  100. };
  101. #define SMC_LGR_ID_SIZE 4
  102. struct smc_link_group {
  103. struct list_head list;
  104. enum smc_lgr_role role; /* client or server */
  105. __be32 daddr; /* destination ip address */
  106. struct smc_link lnk[SMC_LINKS_PER_LGR_MAX]; /* smc link */
  107. char peer_systemid[SMC_SYSTEMID_LEN];
  108. /* unique system_id of peer */
  109. struct rb_root conns_all; /* connection tree */
  110. rwlock_t conns_lock; /* protects conns_all */
  111. unsigned int conns_num; /* current # of connections */
  112. unsigned short vlan_id; /* vlan id of link group */
  113. struct list_head sndbufs[SMC_RMBE_SIZES];/* tx buffers */
  114. rwlock_t sndbufs_lock; /* protects tx buffers */
  115. struct list_head rmbs[SMC_RMBE_SIZES]; /* rx buffers */
  116. rwlock_t rmbs_lock; /* protects rx buffers */
  117. struct smc_rtoken rtokens[SMC_RMBS_PER_LGR_MAX]
  118. [SMC_LINKS_PER_LGR_MAX];
  119. /* remote addr/key pairs */
  120. unsigned long rtokens_used_mask[BITS_TO_LONGS(
  121. SMC_RMBS_PER_LGR_MAX)];
  122. /* used rtoken elements */
  123. u8 id[SMC_LGR_ID_SIZE]; /* unique lgr id */
  124. struct delayed_work free_work; /* delayed freeing of an lgr */
  125. bool sync_err; /* lgr no longer fits to peer */
  126. };
  127. /* Find the connection associated with the given alert token in the link group.
  128. * To use rbtrees we have to implement our own search core.
  129. * Requires @conns_lock
  130. * @token alert token to search for
  131. * @lgr link group to search in
  132. * Returns connection associated with token if found, NULL otherwise.
  133. */
  134. static inline struct smc_connection *smc_lgr_find_conn(
  135. u32 token, struct smc_link_group *lgr)
  136. {
  137. struct smc_connection *res = NULL;
  138. struct rb_node *node;
  139. node = lgr->conns_all.rb_node;
  140. while (node) {
  141. struct smc_connection *cur = rb_entry(node,
  142. struct smc_connection, alert_node);
  143. if (cur->alert_token_local > token) {
  144. node = node->rb_left;
  145. } else {
  146. if (cur->alert_token_local < token) {
  147. node = node->rb_right;
  148. } else {
  149. res = cur;
  150. break;
  151. }
  152. }
  153. }
  154. return res;
  155. }
  156. struct smc_sock;
  157. struct smc_clc_msg_accept_confirm;
  158. void smc_lgr_free(struct smc_link_group *lgr);
  159. void smc_lgr_terminate(struct smc_link_group *lgr);
  160. int smc_buf_create(struct smc_sock *smc);
  161. int smc_rmb_rtoken_handling(struct smc_connection *conn,
  162. struct smc_clc_msg_accept_confirm *clc);
  163. void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
  164. void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
  165. void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
  166. void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
  167. #endif