smc_core.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. u32 num; /* unique link group number */
  22. };
  23. enum smc_lgr_role { /* possible roles of a link group */
  24. SMC_CLNT, /* client */
  25. SMC_SERV /* server */
  26. };
  27. enum smc_link_state { /* possible states of a link */
  28. SMC_LNK_INACTIVE, /* link is inactive */
  29. SMC_LNK_ACTIVATING, /* link is being activated */
  30. SMC_LNK_ACTIVE /* link is active */
  31. };
  32. #define SMC_WR_BUF_SIZE 48 /* size of work request buffer */
  33. struct smc_wr_buf {
  34. u8 raw[SMC_WR_BUF_SIZE];
  35. };
  36. #define SMC_WR_REG_MR_WAIT_TIME (5 * HZ)/* wait time for ib_wr_reg_mr result */
  37. enum smc_wr_reg_state {
  38. POSTED, /* ib_wr_reg_mr request posted */
  39. CONFIRMED, /* ib_wr_reg_mr response: successful */
  40. FAILED /* ib_wr_reg_mr response: failure */
  41. };
  42. struct smc_link {
  43. struct smc_ib_device *smcibdev; /* ib-device */
  44. u8 ibport; /* port - values 1 | 2 */
  45. struct ib_pd *roce_pd; /* IB protection domain,
  46. * unique for every RoCE QP
  47. */
  48. struct ib_qp *roce_qp; /* IB queue pair */
  49. struct ib_qp_attr qp_attr; /* IB queue pair attributes */
  50. struct smc_wr_buf *wr_tx_bufs; /* WR send payload buffers */
  51. struct ib_send_wr *wr_tx_ibs; /* WR send meta data */
  52. struct ib_sge *wr_tx_sges; /* WR send gather meta data */
  53. struct smc_wr_tx_pend *wr_tx_pends; /* WR send waiting for CQE */
  54. /* above four vectors have wr_tx_cnt elements and use the same index */
  55. dma_addr_t wr_tx_dma_addr; /* DMA address of wr_tx_bufs */
  56. atomic_long_t wr_tx_id; /* seq # of last sent WR */
  57. unsigned long *wr_tx_mask; /* bit mask of used indexes */
  58. u32 wr_tx_cnt; /* number of WR send buffers */
  59. wait_queue_head_t wr_tx_wait; /* wait for free WR send buf */
  60. struct smc_wr_buf *wr_rx_bufs; /* WR recv payload buffers */
  61. struct ib_recv_wr *wr_rx_ibs; /* WR recv meta data */
  62. struct ib_sge *wr_rx_sges; /* WR recv scatter meta data */
  63. /* above three vectors have wr_rx_cnt elements and use the same index */
  64. dma_addr_t wr_rx_dma_addr; /* DMA address of wr_rx_bufs */
  65. u64 wr_rx_id; /* seq # of last recv WR */
  66. u32 wr_rx_cnt; /* number of WR recv buffers */
  67. unsigned long wr_rx_tstamp; /* jiffies when last buf rx */
  68. struct ib_reg_wr wr_reg; /* WR register memory region */
  69. wait_queue_head_t wr_reg_wait; /* wait for wr_reg result */
  70. enum smc_wr_reg_state wr_reg_state; /* state of wr_reg request */
  71. union ib_gid gid; /* gid matching used vlan id */
  72. u32 peer_qpn; /* QP number of peer */
  73. enum ib_mtu path_mtu; /* used mtu */
  74. enum ib_mtu peer_mtu; /* mtu size of peer */
  75. u32 psn_initial; /* QP tx initial packet seqno */
  76. u32 peer_psn; /* QP rx initial packet seqno */
  77. u8 peer_mac[ETH_ALEN]; /* = gid[8:10||13:15] */
  78. u8 peer_gid[sizeof(union ib_gid)]; /* gid of peer*/
  79. u8 link_id; /* unique # within link group */
  80. enum smc_link_state state; /* state of link */
  81. struct workqueue_struct *llc_wq; /* single thread work queue */
  82. struct completion llc_confirm; /* wait for rx of conf link */
  83. struct completion llc_confirm_resp; /* wait 4 rx of cnf lnk rsp */
  84. int llc_confirm_rc; /* rc from confirm link msg */
  85. int llc_confirm_resp_rc; /* rc from conf_resp msg */
  86. struct completion llc_add; /* wait for rx of add link */
  87. struct completion llc_add_resp; /* wait for rx of add link rsp*/
  88. struct delayed_work llc_testlink_wrk; /* testlink worker */
  89. struct completion llc_testlink_resp; /* wait for rx of testlink */
  90. int llc_testlink_time; /* testlink interval */
  91. struct completion llc_confirm_rkey; /* wait 4 rx of cnf rkey */
  92. int llc_confirm_rkey_rc; /* rc from cnf rkey msg */
  93. };
  94. /* For now we just allow one parallel link per link group. The SMC protocol
  95. * allows more (up to 8).
  96. */
  97. #define SMC_LINKS_PER_LGR_MAX 1
  98. #define SMC_SINGLE_LINK 0
  99. #define SMC_FIRST_CONTACT 1 /* first contact to a peer */
  100. #define SMC_REUSE_CONTACT 0 /* follow-on contact to a peer*/
  101. /* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
  102. struct smc_buf_desc {
  103. struct list_head list;
  104. void *cpu_addr; /* virtual address of buffer */
  105. struct page *pages;
  106. int len; /* length of buffer */
  107. struct sg_table sgt[SMC_LINKS_PER_LGR_MAX];/* virtual buffer */
  108. struct ib_mr *mr_rx[SMC_LINKS_PER_LGR_MAX];
  109. /* for rmb only: memory region
  110. * incl. rkey provided to peer
  111. */
  112. u32 order; /* allocation order */
  113. u32 used; /* currently used / unused */
  114. u8 reused : 1; /* new created / reused */
  115. u8 regerr : 1; /* err during registration */
  116. };
  117. struct smc_rtoken { /* address/key of remote RMB */
  118. u64 dma_addr;
  119. u32 rkey;
  120. };
  121. #define SMC_LGR_ID_SIZE 4
  122. #define SMC_BUF_MIN_SIZE 16384 /* minimum size of an RMB */
  123. #define SMC_RMBE_SIZES 16 /* number of distinct RMBE sizes */
  124. /* theoretically, the RFC states that largest size would be 512K,
  125. * i.e. compressed 5 and thus 6 sizes (0..5), despite
  126. * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15)
  127. */
  128. struct smc_link_group {
  129. struct list_head list;
  130. enum smc_lgr_role role; /* client or server */
  131. struct smc_link lnk[SMC_LINKS_PER_LGR_MAX]; /* smc link */
  132. char peer_systemid[SMC_SYSTEMID_LEN];
  133. /* unique system_id of peer */
  134. struct rb_root conns_all; /* connection tree */
  135. rwlock_t conns_lock; /* protects conns_all */
  136. unsigned int conns_num; /* current # of connections */
  137. unsigned short vlan_id; /* vlan id of link group */
  138. struct list_head sndbufs[SMC_RMBE_SIZES];/* tx buffers */
  139. rwlock_t sndbufs_lock; /* protects tx buffers */
  140. struct list_head rmbs[SMC_RMBE_SIZES]; /* rx buffers */
  141. rwlock_t rmbs_lock; /* protects rx buffers */
  142. struct smc_rtoken rtokens[SMC_RMBS_PER_LGR_MAX]
  143. [SMC_LINKS_PER_LGR_MAX];
  144. /* remote addr/key pairs */
  145. unsigned long rtokens_used_mask[BITS_TO_LONGS(
  146. SMC_RMBS_PER_LGR_MAX)];
  147. /* used rtoken elements */
  148. u8 id[SMC_LGR_ID_SIZE]; /* unique lgr id */
  149. struct delayed_work free_work; /* delayed freeing of an lgr */
  150. u8 sync_err : 1; /* lgr no longer fits to peer */
  151. u8 terminating : 1;/* lgr is terminating */
  152. };
  153. /* Find the connection associated with the given alert token in the link group.
  154. * To use rbtrees we have to implement our own search core.
  155. * Requires @conns_lock
  156. * @token alert token to search for
  157. * @lgr link group to search in
  158. * Returns connection associated with token if found, NULL otherwise.
  159. */
  160. static inline struct smc_connection *smc_lgr_find_conn(
  161. u32 token, struct smc_link_group *lgr)
  162. {
  163. struct smc_connection *res = NULL;
  164. struct rb_node *node;
  165. node = lgr->conns_all.rb_node;
  166. while (node) {
  167. struct smc_connection *cur = rb_entry(node,
  168. struct smc_connection, alert_node);
  169. if (cur->alert_token_local > token) {
  170. node = node->rb_left;
  171. } else {
  172. if (cur->alert_token_local < token) {
  173. node = node->rb_right;
  174. } else {
  175. res = cur;
  176. break;
  177. }
  178. }
  179. }
  180. return res;
  181. }
  182. struct smc_sock;
  183. struct smc_clc_msg_accept_confirm;
  184. struct smc_clc_msg_local;
  185. void smc_lgr_free(struct smc_link_group *lgr);
  186. void smc_lgr_forget(struct smc_link_group *lgr);
  187. void smc_lgr_terminate(struct smc_link_group *lgr);
  188. void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport);
  189. int smc_buf_create(struct smc_sock *smc);
  190. int smc_uncompress_bufsize(u8 compressed);
  191. int smc_rmb_rtoken_handling(struct smc_connection *conn,
  192. struct smc_clc_msg_accept_confirm *clc);
  193. int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey);
  194. int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey);
  195. void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
  196. void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
  197. void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
  198. void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
  199. void smc_conn_free(struct smc_connection *conn);
  200. int smc_conn_create(struct smc_sock *smc,
  201. struct smc_ib_device *smcibdev, u8 ibport,
  202. struct smc_clc_msg_local *lcl, int srv_first_contact);
  203. void smc_core_exit(void);
  204. #endif