smc_ib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <linux/workqueue.h>
  15. #include <rdma/ib_verbs.h>
  16. #include "smc_pnet.h"
  17. #include "smc_ib.h"
  18. #include "smc_core.h"
  19. #include "smc_wr.h"
  20. #include "smc.h"
  21. #define SMC_QP_MIN_RNR_TIMER 5
  22. #define SMC_QP_TIMEOUT 15 /* 4096 * 2 ** timeout usec */
  23. #define SMC_QP_RETRY_CNT 7 /* 7: infinite */
  24. #define SMC_QP_RNR_RETRY 7 /* 7: infinite */
  25. struct smc_ib_devices smc_ib_devices = { /* smc-registered ib devices */
  26. .lock = __SPIN_LOCK_UNLOCKED(smc_ib_devices.lock),
  27. .list = LIST_HEAD_INIT(smc_ib_devices.list),
  28. };
  29. #define SMC_LOCAL_SYSTEMID_RESET "%%%%%%%"
  30. u8 local_systemid[SMC_SYSTEMID_LEN] = SMC_LOCAL_SYSTEMID_RESET; /* unique system
  31. * identifier
  32. */
  33. static int smc_ib_modify_qp_init(struct smc_link *lnk)
  34. {
  35. struct ib_qp_attr qp_attr;
  36. memset(&qp_attr, 0, sizeof(qp_attr));
  37. qp_attr.qp_state = IB_QPS_INIT;
  38. qp_attr.pkey_index = 0;
  39. qp_attr.port_num = lnk->ibport;
  40. qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE
  41. | IB_ACCESS_REMOTE_WRITE;
  42. return ib_modify_qp(lnk->roce_qp, &qp_attr,
  43. IB_QP_STATE | IB_QP_PKEY_INDEX |
  44. IB_QP_ACCESS_FLAGS | IB_QP_PORT);
  45. }
  46. static int smc_ib_modify_qp_rtr(struct smc_link *lnk)
  47. {
  48. enum ib_qp_attr_mask qp_attr_mask =
  49. IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | IB_QP_DEST_QPN |
  50. IB_QP_RQ_PSN | IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER;
  51. struct ib_qp_attr qp_attr;
  52. memset(&qp_attr, 0, sizeof(qp_attr));
  53. qp_attr.qp_state = IB_QPS_RTR;
  54. qp_attr.path_mtu = min(lnk->path_mtu, lnk->peer_mtu);
  55. qp_attr.ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
  56. rdma_ah_set_port_num(&qp_attr.ah_attr, lnk->ibport);
  57. rdma_ah_set_grh(&qp_attr.ah_attr, NULL, 0, 0, 1, 0);
  58. rdma_ah_set_dgid_raw(&qp_attr.ah_attr, lnk->peer_gid);
  59. memcpy(&qp_attr.ah_attr.roce.dmac, lnk->peer_mac,
  60. sizeof(lnk->peer_mac));
  61. qp_attr.dest_qp_num = lnk->peer_qpn;
  62. qp_attr.rq_psn = lnk->peer_psn; /* starting receive packet seq # */
  63. qp_attr.max_dest_rd_atomic = 1; /* max # of resources for incoming
  64. * requests
  65. */
  66. qp_attr.min_rnr_timer = SMC_QP_MIN_RNR_TIMER;
  67. return ib_modify_qp(lnk->roce_qp, &qp_attr, qp_attr_mask);
  68. }
  69. int smc_ib_modify_qp_rts(struct smc_link *lnk)
  70. {
  71. struct ib_qp_attr qp_attr;
  72. memset(&qp_attr, 0, sizeof(qp_attr));
  73. qp_attr.qp_state = IB_QPS_RTS;
  74. qp_attr.timeout = SMC_QP_TIMEOUT; /* local ack timeout */
  75. qp_attr.retry_cnt = SMC_QP_RETRY_CNT; /* retry count */
  76. qp_attr.rnr_retry = SMC_QP_RNR_RETRY; /* RNR retries, 7=infinite */
  77. qp_attr.sq_psn = lnk->psn_initial; /* starting send packet seq # */
  78. qp_attr.max_rd_atomic = 1; /* # of outstanding RDMA reads and
  79. * atomic ops allowed
  80. */
  81. return ib_modify_qp(lnk->roce_qp, &qp_attr,
  82. IB_QP_STATE | IB_QP_TIMEOUT | IB_QP_RETRY_CNT |
  83. IB_QP_SQ_PSN | IB_QP_RNR_RETRY |
  84. IB_QP_MAX_QP_RD_ATOMIC);
  85. }
  86. int smc_ib_modify_qp_reset(struct smc_link *lnk)
  87. {
  88. struct ib_qp_attr qp_attr;
  89. memset(&qp_attr, 0, sizeof(qp_attr));
  90. qp_attr.qp_state = IB_QPS_RESET;
  91. return ib_modify_qp(lnk->roce_qp, &qp_attr, IB_QP_STATE);
  92. }
  93. int smc_ib_ready_link(struct smc_link *lnk)
  94. {
  95. struct smc_link_group *lgr =
  96. container_of(lnk, struct smc_link_group, lnk[0]);
  97. int rc = 0;
  98. rc = smc_ib_modify_qp_init(lnk);
  99. if (rc)
  100. goto out;
  101. rc = smc_ib_modify_qp_rtr(lnk);
  102. if (rc)
  103. goto out;
  104. smc_wr_remember_qp_attr(lnk);
  105. rc = ib_req_notify_cq(lnk->smcibdev->roce_cq_recv,
  106. IB_CQ_SOLICITED_MASK);
  107. if (rc)
  108. goto out;
  109. rc = smc_wr_rx_post_init(lnk);
  110. if (rc)
  111. goto out;
  112. smc_wr_remember_qp_attr(lnk);
  113. if (lgr->role == SMC_SERV) {
  114. rc = smc_ib_modify_qp_rts(lnk);
  115. if (rc)
  116. goto out;
  117. smc_wr_remember_qp_attr(lnk);
  118. }
  119. out:
  120. return rc;
  121. }
  122. /* process context wrapper for might_sleep smc_ib_remember_port_attr */
  123. static void smc_ib_port_event_work(struct work_struct *work)
  124. {
  125. struct smc_ib_device *smcibdev = container_of(
  126. work, struct smc_ib_device, port_event_work);
  127. u8 port_idx;
  128. for_each_set_bit(port_idx, &smcibdev->port_event_mask, SMC_MAX_PORTS) {
  129. smc_ib_remember_port_attr(smcibdev, port_idx + 1);
  130. clear_bit(port_idx, &smcibdev->port_event_mask);
  131. }
  132. }
  133. /* can be called in IRQ context */
  134. static void smc_ib_global_event_handler(struct ib_event_handler *handler,
  135. struct ib_event *ibevent)
  136. {
  137. struct smc_ib_device *smcibdev;
  138. u8 port_idx;
  139. smcibdev = container_of(handler, struct smc_ib_device, event_handler);
  140. switch (ibevent->event) {
  141. case IB_EVENT_PORT_ERR:
  142. port_idx = ibevent->element.port_num - 1;
  143. set_bit(port_idx, &smcibdev->port_event_mask);
  144. schedule_work(&smcibdev->port_event_work);
  145. /* fall through */
  146. case IB_EVENT_DEVICE_FATAL:
  147. /* tbd in follow-on patch:
  148. * abnormal close of corresponding connections
  149. */
  150. break;
  151. case IB_EVENT_PORT_ACTIVE:
  152. port_idx = ibevent->element.port_num - 1;
  153. set_bit(port_idx, &smcibdev->port_event_mask);
  154. schedule_work(&smcibdev->port_event_work);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. void smc_ib_dealloc_protection_domain(struct smc_link *lnk)
  161. {
  162. ib_dealloc_pd(lnk->roce_pd);
  163. lnk->roce_pd = NULL;
  164. }
  165. int smc_ib_create_protection_domain(struct smc_link *lnk)
  166. {
  167. int rc;
  168. lnk->roce_pd = ib_alloc_pd(lnk->smcibdev->ibdev,
  169. IB_PD_UNSAFE_GLOBAL_RKEY);
  170. rc = PTR_ERR_OR_ZERO(lnk->roce_pd);
  171. if (IS_ERR(lnk->roce_pd))
  172. lnk->roce_pd = NULL;
  173. return rc;
  174. }
  175. static void smc_ib_qp_event_handler(struct ib_event *ibevent, void *priv)
  176. {
  177. switch (ibevent->event) {
  178. case IB_EVENT_DEVICE_FATAL:
  179. case IB_EVENT_GID_CHANGE:
  180. case IB_EVENT_PORT_ERR:
  181. case IB_EVENT_QP_ACCESS_ERR:
  182. /* tbd in follow-on patch:
  183. * abnormal close of corresponding connections
  184. */
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. void smc_ib_destroy_queue_pair(struct smc_link *lnk)
  191. {
  192. ib_destroy_qp(lnk->roce_qp);
  193. lnk->roce_qp = NULL;
  194. }
  195. /* create a queue pair within the protection domain for a link */
  196. int smc_ib_create_queue_pair(struct smc_link *lnk)
  197. {
  198. struct ib_qp_init_attr qp_attr = {
  199. .event_handler = smc_ib_qp_event_handler,
  200. .qp_context = lnk,
  201. .send_cq = lnk->smcibdev->roce_cq_send,
  202. .recv_cq = lnk->smcibdev->roce_cq_recv,
  203. .srq = NULL,
  204. .cap = {
  205. .max_send_wr = SMC_WR_BUF_CNT,
  206. /* include unsolicited rdma_writes as well,
  207. * there are max. 2 RDMA_WRITE per 1 WR_SEND
  208. */
  209. .max_recv_wr = SMC_WR_BUF_CNT * 3,
  210. .max_send_sge = SMC_IB_MAX_SEND_SGE,
  211. .max_recv_sge = 1,
  212. },
  213. .sq_sig_type = IB_SIGNAL_REQ_WR,
  214. .qp_type = IB_QPT_RC,
  215. };
  216. int rc;
  217. lnk->roce_qp = ib_create_qp(lnk->roce_pd, &qp_attr);
  218. rc = PTR_ERR_OR_ZERO(lnk->roce_qp);
  219. if (IS_ERR(lnk->roce_qp))
  220. lnk->roce_qp = NULL;
  221. else
  222. smc_wr_remember_qp_attr(lnk);
  223. return rc;
  224. }
  225. /* map a new TX or RX buffer to DMA */
  226. int smc_ib_buf_map(struct smc_ib_device *smcibdev, int buf_size,
  227. struct smc_buf_desc *buf_slot,
  228. enum dma_data_direction data_direction)
  229. {
  230. int rc = 0;
  231. if (buf_slot->dma_addr[SMC_SINGLE_LINK])
  232. return rc; /* already mapped */
  233. buf_slot->dma_addr[SMC_SINGLE_LINK] =
  234. ib_dma_map_single(smcibdev->ibdev, buf_slot->cpu_addr,
  235. buf_size, data_direction);
  236. if (ib_dma_mapping_error(smcibdev->ibdev,
  237. buf_slot->dma_addr[SMC_SINGLE_LINK]))
  238. rc = -EIO;
  239. return rc;
  240. }
  241. void smc_ib_buf_unmap(struct smc_ib_device *smcibdev, int buf_size,
  242. struct smc_buf_desc *buf_slot,
  243. enum dma_data_direction data_direction)
  244. {
  245. if (!buf_slot->dma_addr[SMC_SINGLE_LINK])
  246. return; /* already unmapped */
  247. ib_dma_unmap_single(smcibdev->ibdev, *buf_slot->dma_addr, buf_size,
  248. data_direction);
  249. buf_slot->dma_addr[SMC_SINGLE_LINK] = 0;
  250. }
  251. static int smc_ib_fill_gid_and_mac(struct smc_ib_device *smcibdev, u8 ibport)
  252. {
  253. struct net_device *ndev;
  254. int rc;
  255. rc = ib_query_gid(smcibdev->ibdev, ibport, 0,
  256. &smcibdev->gid[ibport - 1], NULL);
  257. /* the SMC protocol requires specification of the roce MAC address;
  258. * if net_device cannot be determined, it can be derived from gid 0
  259. */
  260. ndev = smcibdev->ibdev->get_netdev(smcibdev->ibdev, ibport);
  261. if (ndev) {
  262. memcpy(&smcibdev->mac, ndev->dev_addr, ETH_ALEN);
  263. } else if (!rc) {
  264. memcpy(&smcibdev->mac[ibport - 1][0],
  265. &smcibdev->gid[ibport - 1].raw[8], 3);
  266. memcpy(&smcibdev->mac[ibport - 1][3],
  267. &smcibdev->gid[ibport - 1].raw[13], 3);
  268. smcibdev->mac[ibport - 1][0] &= ~0x02;
  269. }
  270. return rc;
  271. }
  272. /* Create an identifier unique for this instance of SMC-R.
  273. * The MAC-address of the first active registered IB device
  274. * plus a random 2-byte number is used to create this identifier.
  275. * This name is delivered to the peer during connection initialization.
  276. */
  277. static inline void smc_ib_define_local_systemid(struct smc_ib_device *smcibdev,
  278. u8 ibport)
  279. {
  280. memcpy(&local_systemid[2], &smcibdev->mac[ibport - 1],
  281. sizeof(smcibdev->mac[ibport - 1]));
  282. get_random_bytes(&local_systemid[0], 2);
  283. }
  284. bool smc_ib_port_active(struct smc_ib_device *smcibdev, u8 ibport)
  285. {
  286. return smcibdev->pattr[ibport - 1].state == IB_PORT_ACTIVE;
  287. }
  288. int smc_ib_remember_port_attr(struct smc_ib_device *smcibdev, u8 ibport)
  289. {
  290. int rc;
  291. memset(&smcibdev->pattr[ibport - 1], 0,
  292. sizeof(smcibdev->pattr[ibport - 1]));
  293. rc = ib_query_port(smcibdev->ibdev, ibport,
  294. &smcibdev->pattr[ibport - 1]);
  295. if (rc)
  296. goto out;
  297. rc = smc_ib_fill_gid_and_mac(smcibdev, ibport);
  298. if (rc)
  299. goto out;
  300. if (!strncmp(local_systemid, SMC_LOCAL_SYSTEMID_RESET,
  301. sizeof(local_systemid)) &&
  302. smc_ib_port_active(smcibdev, ibport))
  303. /* create unique system identifier */
  304. smc_ib_define_local_systemid(smcibdev, ibport);
  305. out:
  306. return rc;
  307. }
  308. long smc_ib_setup_per_ibdev(struct smc_ib_device *smcibdev)
  309. {
  310. struct ib_cq_init_attr cqattr = {
  311. .cqe = SMC_WR_MAX_CQE, .comp_vector = 0 };
  312. long rc;
  313. smcibdev->roce_cq_send = ib_create_cq(smcibdev->ibdev,
  314. smc_wr_tx_cq_handler, NULL,
  315. smcibdev, &cqattr);
  316. rc = PTR_ERR_OR_ZERO(smcibdev->roce_cq_send);
  317. if (IS_ERR(smcibdev->roce_cq_send)) {
  318. smcibdev->roce_cq_send = NULL;
  319. return rc;
  320. }
  321. smcibdev->roce_cq_recv = ib_create_cq(smcibdev->ibdev,
  322. smc_wr_rx_cq_handler, NULL,
  323. smcibdev, &cqattr);
  324. rc = PTR_ERR_OR_ZERO(smcibdev->roce_cq_recv);
  325. if (IS_ERR(smcibdev->roce_cq_recv)) {
  326. smcibdev->roce_cq_recv = NULL;
  327. goto err;
  328. }
  329. INIT_IB_EVENT_HANDLER(&smcibdev->event_handler, smcibdev->ibdev,
  330. smc_ib_global_event_handler);
  331. ib_register_event_handler(&smcibdev->event_handler);
  332. smc_wr_add_dev(smcibdev);
  333. smcibdev->initialized = 1;
  334. return rc;
  335. err:
  336. ib_destroy_cq(smcibdev->roce_cq_send);
  337. return rc;
  338. }
  339. static void smc_ib_cleanup_per_ibdev(struct smc_ib_device *smcibdev)
  340. {
  341. if (!smcibdev->initialized)
  342. return;
  343. smc_wr_remove_dev(smcibdev);
  344. ib_unregister_event_handler(&smcibdev->event_handler);
  345. ib_destroy_cq(smcibdev->roce_cq_recv);
  346. ib_destroy_cq(smcibdev->roce_cq_send);
  347. }
  348. static struct ib_client smc_ib_client;
  349. /* callback function for ib_register_client() */
  350. static void smc_ib_add_dev(struct ib_device *ibdev)
  351. {
  352. struct smc_ib_device *smcibdev;
  353. if (ibdev->node_type != RDMA_NODE_IB_CA)
  354. return;
  355. smcibdev = kzalloc(sizeof(*smcibdev), GFP_KERNEL);
  356. if (!smcibdev)
  357. return;
  358. smcibdev->ibdev = ibdev;
  359. INIT_WORK(&smcibdev->port_event_work, smc_ib_port_event_work);
  360. spin_lock(&smc_ib_devices.lock);
  361. list_add_tail(&smcibdev->list, &smc_ib_devices.list);
  362. spin_unlock(&smc_ib_devices.lock);
  363. ib_set_client_data(ibdev, &smc_ib_client, smcibdev);
  364. }
  365. /* callback function for ib_register_client() */
  366. static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
  367. {
  368. struct smc_ib_device *smcibdev;
  369. smcibdev = ib_get_client_data(ibdev, &smc_ib_client);
  370. ib_set_client_data(ibdev, &smc_ib_client, NULL);
  371. spin_lock(&smc_ib_devices.lock);
  372. list_del_init(&smcibdev->list); /* remove from smc_ib_devices */
  373. spin_unlock(&smc_ib_devices.lock);
  374. smc_pnet_remove_by_ibdev(smcibdev);
  375. smc_ib_cleanup_per_ibdev(smcibdev);
  376. kfree(smcibdev);
  377. }
  378. static struct ib_client smc_ib_client = {
  379. .name = "smc_ib",
  380. .add = smc_ib_add_dev,
  381. .remove = smc_ib_remove_dev,
  382. };
  383. int __init smc_ib_register_client(void)
  384. {
  385. return ib_register_client(&smc_ib_client);
  386. }
  387. void smc_ib_unregister_client(void)
  388. {
  389. ib_unregister_client(&smc_ib_client);
  390. }