smc_ib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  3. *
  4. * IB infrastructure:
  5. * Establish SMC-R as an Infiniband Client to be notified about added and
  6. * removed IB devices of type RDMA.
  7. * Determine device and port characteristics for these IB devices.
  8. *
  9. * Copyright IBM Corp. 2016
  10. *
  11. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  12. */
  13. #include <linux/random.h>
  14. #include <rdma/ib_verbs.h>
  15. #include "smc_pnet.h"
  16. #include "smc_ib.h"
  17. #include "smc_core.h"
  18. #include "smc_wr.h"
  19. #include "smc.h"
  20. struct smc_ib_devices smc_ib_devices = { /* smc-registered ib devices */
  21. .lock = __SPIN_LOCK_UNLOCKED(smc_ib_devices.lock),
  22. .list = LIST_HEAD_INIT(smc_ib_devices.list),
  23. };
  24. #define SMC_LOCAL_SYSTEMID_RESET "%%%%%%%"
  25. u8 local_systemid[SMC_SYSTEMID_LEN] = SMC_LOCAL_SYSTEMID_RESET; /* unique system
  26. * identifier
  27. */
  28. void smc_ib_dealloc_protection_domain(struct smc_link *lnk)
  29. {
  30. ib_dealloc_pd(lnk->roce_pd);
  31. lnk->roce_pd = NULL;
  32. }
  33. int smc_ib_create_protection_domain(struct smc_link *lnk)
  34. {
  35. int rc;
  36. lnk->roce_pd = ib_alloc_pd(lnk->smcibdev->ibdev, 0);
  37. rc = PTR_ERR_OR_ZERO(lnk->roce_pd);
  38. if (IS_ERR(lnk->roce_pd))
  39. lnk->roce_pd = NULL;
  40. return rc;
  41. }
  42. static void smc_ib_qp_event_handler(struct ib_event *ibevent, void *priv)
  43. {
  44. switch (ibevent->event) {
  45. case IB_EVENT_DEVICE_FATAL:
  46. case IB_EVENT_GID_CHANGE:
  47. case IB_EVENT_PORT_ERR:
  48. case IB_EVENT_QP_ACCESS_ERR:
  49. /* tbd in follow-on patch:
  50. * abnormal close of corresponding connections
  51. */
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. void smc_ib_destroy_queue_pair(struct smc_link *lnk)
  58. {
  59. ib_destroy_qp(lnk->roce_qp);
  60. lnk->roce_qp = NULL;
  61. }
  62. /* create a queue pair within the protection domain for a link */
  63. int smc_ib_create_queue_pair(struct smc_link *lnk)
  64. {
  65. struct ib_qp_init_attr qp_attr = {
  66. .event_handler = smc_ib_qp_event_handler,
  67. .qp_context = lnk,
  68. .send_cq = lnk->smcibdev->roce_cq_send,
  69. .recv_cq = lnk->smcibdev->roce_cq_recv,
  70. .srq = NULL,
  71. .cap = {
  72. .max_send_wr = SMC_WR_BUF_CNT,
  73. /* include unsolicited rdma_writes as well,
  74. * there are max. 2 RDMA_WRITE per 1 WR_SEND
  75. */
  76. .max_recv_wr = SMC_WR_BUF_CNT * 3,
  77. .max_send_sge = SMC_IB_MAX_SEND_SGE,
  78. .max_recv_sge = 1,
  79. .max_inline_data = SMC_WR_TX_SIZE,
  80. },
  81. .sq_sig_type = IB_SIGNAL_REQ_WR,
  82. .qp_type = IB_QPT_RC,
  83. };
  84. int rc;
  85. lnk->roce_qp = ib_create_qp(lnk->roce_pd, &qp_attr);
  86. rc = PTR_ERR_OR_ZERO(lnk->roce_qp);
  87. if (IS_ERR(lnk->roce_qp))
  88. lnk->roce_qp = NULL;
  89. else
  90. smc_wr_remember_qp_attr(lnk);
  91. return rc;
  92. }
  93. /* map a new TX or RX buffer to DMA */
  94. int smc_ib_buf_map(struct smc_ib_device *smcibdev, int buf_size,
  95. struct smc_buf_desc *buf_slot,
  96. enum dma_data_direction data_direction)
  97. {
  98. int rc = 0;
  99. if (buf_slot->dma_addr[SMC_SINGLE_LINK])
  100. return rc; /* already mapped */
  101. buf_slot->dma_addr[SMC_SINGLE_LINK] =
  102. ib_dma_map_single(smcibdev->ibdev, buf_slot->cpu_addr,
  103. buf_size, data_direction);
  104. if (ib_dma_mapping_error(smcibdev->ibdev,
  105. buf_slot->dma_addr[SMC_SINGLE_LINK]))
  106. rc = -EIO;
  107. return rc;
  108. }
  109. static int smc_ib_fill_gid_and_mac(struct smc_ib_device *smcibdev, u8 ibport)
  110. {
  111. struct net_device *ndev;
  112. int rc;
  113. rc = ib_query_gid(smcibdev->ibdev, ibport, 0,
  114. &smcibdev->gid[ibport - 1], NULL);
  115. /* the SMC protocol requires specification of the roce MAC address;
  116. * if net_device cannot be determined, it can be derived from gid 0
  117. */
  118. ndev = smcibdev->ibdev->get_netdev(smcibdev->ibdev, ibport);
  119. if (ndev) {
  120. memcpy(&smcibdev->mac, ndev->dev_addr, ETH_ALEN);
  121. } else if (!rc) {
  122. memcpy(&smcibdev->mac[ibport - 1][0],
  123. &smcibdev->gid[ibport - 1].raw[8], 3);
  124. memcpy(&smcibdev->mac[ibport - 1][3],
  125. &smcibdev->gid[ibport - 1].raw[13], 3);
  126. smcibdev->mac[ibport - 1][0] &= ~0x02;
  127. }
  128. return rc;
  129. }
  130. /* Create an identifier unique for this instance of SMC-R.
  131. * The MAC-address of the first active registered IB device
  132. * plus a random 2-byte number is used to create this identifier.
  133. * This name is delivered to the peer during connection initialization.
  134. */
  135. static inline void smc_ib_define_local_systemid(struct smc_ib_device *smcibdev,
  136. u8 ibport)
  137. {
  138. memcpy(&local_systemid[2], &smcibdev->mac[ibport - 1],
  139. sizeof(smcibdev->mac[ibport - 1]));
  140. get_random_bytes(&local_systemid[0], 2);
  141. }
  142. bool smc_ib_port_active(struct smc_ib_device *smcibdev, u8 ibport)
  143. {
  144. return smcibdev->pattr[ibport - 1].state == IB_PORT_ACTIVE;
  145. }
  146. int smc_ib_remember_port_attr(struct smc_ib_device *smcibdev, u8 ibport)
  147. {
  148. int rc;
  149. memset(&smcibdev->pattr[ibport - 1], 0,
  150. sizeof(smcibdev->pattr[ibport - 1]));
  151. rc = ib_query_port(smcibdev->ibdev, ibport,
  152. &smcibdev->pattr[ibport - 1]);
  153. if (rc)
  154. goto out;
  155. rc = smc_ib_fill_gid_and_mac(smcibdev, ibport);
  156. if (rc)
  157. goto out;
  158. if (!strncmp(local_systemid, SMC_LOCAL_SYSTEMID_RESET,
  159. sizeof(local_systemid)) &&
  160. smc_ib_port_active(smcibdev, ibport))
  161. /* create unique system identifier */
  162. smc_ib_define_local_systemid(smcibdev, ibport);
  163. out:
  164. return rc;
  165. }
  166. static struct ib_client smc_ib_client;
  167. /* callback function for ib_register_client() */
  168. static void smc_ib_add_dev(struct ib_device *ibdev)
  169. {
  170. struct smc_ib_device *smcibdev;
  171. if (ibdev->node_type != RDMA_NODE_IB_CA)
  172. return;
  173. smcibdev = kzalloc(sizeof(*smcibdev), GFP_KERNEL);
  174. if (!smcibdev)
  175. return;
  176. smcibdev->ibdev = ibdev;
  177. spin_lock(&smc_ib_devices.lock);
  178. list_add_tail(&smcibdev->list, &smc_ib_devices.list);
  179. spin_unlock(&smc_ib_devices.lock);
  180. ib_set_client_data(ibdev, &smc_ib_client, smcibdev);
  181. }
  182. /* callback function for ib_register_client() */
  183. static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
  184. {
  185. struct smc_ib_device *smcibdev;
  186. smcibdev = ib_get_client_data(ibdev, &smc_ib_client);
  187. ib_set_client_data(ibdev, &smc_ib_client, NULL);
  188. spin_lock(&smc_ib_devices.lock);
  189. list_del_init(&smcibdev->list); /* remove from smc_ib_devices */
  190. spin_unlock(&smc_ib_devices.lock);
  191. smc_pnet_remove_by_ibdev(smcibdev);
  192. kfree(smcibdev);
  193. }
  194. static struct ib_client smc_ib_client = {
  195. .name = "smc_ib",
  196. .add = smc_ib_add_dev,
  197. .remove = smc_ib_remove_dev,
  198. };
  199. int __init smc_ib_register_client(void)
  200. {
  201. return ib_register_client(&smc_ib_client);
  202. }
  203. void smc_ib_unregister_client(void)
  204. {
  205. ib_unregister_client(&smc_ib_client);
  206. }