smc_core.h 9.3 KB

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