smc_core.h 6.4 KB

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