rxe.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #include <rdma/rdma_netlink.h>
  7. #include <net/addrconf.h>
  8. #include "rxe.h"
  9. #include "rxe_loc.h"
  10. MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib");
  11. MODULE_DESCRIPTION("Soft RDMA transport");
  12. MODULE_LICENSE("Dual BSD/GPL");
  13. /* free resources for a rxe device all objects created for this device must
  14. * have been destroyed
  15. */
  16. void rxe_dealloc(struct ib_device *ib_dev)
  17. {
  18. struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
  19. rxe_pool_cleanup(&rxe->uc_pool);
  20. rxe_pool_cleanup(&rxe->pd_pool);
  21. rxe_pool_cleanup(&rxe->ah_pool);
  22. rxe_pool_cleanup(&rxe->srq_pool);
  23. rxe_pool_cleanup(&rxe->qp_pool);
  24. rxe_pool_cleanup(&rxe->cq_pool);
  25. rxe_pool_cleanup(&rxe->mr_pool);
  26. rxe_pool_cleanup(&rxe->mw_pool);
  27. WARN_ON(!RB_EMPTY_ROOT(&rxe->mcg_tree));
  28. if (rxe->tfm)
  29. crypto_free_shash(rxe->tfm);
  30. }
  31. /* initialize rxe device parameters */
  32. static void rxe_init_device_param(struct rxe_dev *rxe)
  33. {
  34. rxe->max_inline_data = RXE_MAX_INLINE_DATA;
  35. rxe->attr.vendor_id = RXE_VENDOR_ID;
  36. rxe->attr.max_mr_size = RXE_MAX_MR_SIZE;
  37. rxe->attr.page_size_cap = RXE_PAGE_SIZE_CAP;
  38. rxe->attr.max_qp = RXE_MAX_QP;
  39. rxe->attr.max_qp_wr = RXE_MAX_QP_WR;
  40. rxe->attr.device_cap_flags = RXE_DEVICE_CAP_FLAGS;
  41. rxe->attr.kernel_cap_flags = IBK_ALLOW_USER_UNREG;
  42. rxe->attr.max_send_sge = RXE_MAX_SGE;
  43. rxe->attr.max_recv_sge = RXE_MAX_SGE;
  44. rxe->attr.max_sge_rd = RXE_MAX_SGE_RD;
  45. rxe->attr.max_cq = RXE_MAX_CQ;
  46. rxe->attr.max_cqe = (1 << RXE_MAX_LOG_CQE) - 1;
  47. rxe->attr.max_mr = RXE_MAX_MR;
  48. rxe->attr.max_mw = RXE_MAX_MW;
  49. rxe->attr.max_pd = RXE_MAX_PD;
  50. rxe->attr.max_qp_rd_atom = RXE_MAX_QP_RD_ATOM;
  51. rxe->attr.max_res_rd_atom = RXE_MAX_RES_RD_ATOM;
  52. rxe->attr.max_qp_init_rd_atom = RXE_MAX_QP_INIT_RD_ATOM;
  53. rxe->attr.atomic_cap = IB_ATOMIC_HCA;
  54. rxe->attr.max_mcast_grp = RXE_MAX_MCAST_GRP;
  55. rxe->attr.max_mcast_qp_attach = RXE_MAX_MCAST_QP_ATTACH;
  56. rxe->attr.max_total_mcast_qp_attach = RXE_MAX_TOT_MCAST_QP_ATTACH;
  57. rxe->attr.max_ah = RXE_MAX_AH;
  58. rxe->attr.max_srq = RXE_MAX_SRQ;
  59. rxe->attr.max_srq_wr = RXE_MAX_SRQ_WR;
  60. rxe->attr.max_srq_sge = RXE_MAX_SRQ_SGE;
  61. rxe->attr.max_fast_reg_page_list_len = RXE_MAX_FMR_PAGE_LIST_LEN;
  62. rxe->attr.max_pkeys = RXE_MAX_PKEYS;
  63. rxe->attr.local_ca_ack_delay = RXE_LOCAL_CA_ACK_DELAY;
  64. addrconf_addr_eui48((unsigned char *)&rxe->attr.sys_image_guid,
  65. rxe->ndev->dev_addr);
  66. rxe->max_ucontext = RXE_MAX_UCONTEXT;
  67. }
  68. /* initialize port attributes */
  69. static void rxe_init_port_param(struct rxe_port *port)
  70. {
  71. port->attr.state = IB_PORT_DOWN;
  72. port->attr.max_mtu = IB_MTU_4096;
  73. port->attr.active_mtu = IB_MTU_256;
  74. port->attr.gid_tbl_len = RXE_PORT_GID_TBL_LEN;
  75. port->attr.port_cap_flags = RXE_PORT_PORT_CAP_FLAGS;
  76. port->attr.max_msg_sz = RXE_PORT_MAX_MSG_SZ;
  77. port->attr.bad_pkey_cntr = RXE_PORT_BAD_PKEY_CNTR;
  78. port->attr.qkey_viol_cntr = RXE_PORT_QKEY_VIOL_CNTR;
  79. port->attr.pkey_tbl_len = RXE_PORT_PKEY_TBL_LEN;
  80. port->attr.lid = RXE_PORT_LID;
  81. port->attr.sm_lid = RXE_PORT_SM_LID;
  82. port->attr.lmc = RXE_PORT_LMC;
  83. port->attr.max_vl_num = RXE_PORT_MAX_VL_NUM;
  84. port->attr.sm_sl = RXE_PORT_SM_SL;
  85. port->attr.subnet_timeout = RXE_PORT_SUBNET_TIMEOUT;
  86. port->attr.init_type_reply = RXE_PORT_INIT_TYPE_REPLY;
  87. port->attr.active_width = RXE_PORT_ACTIVE_WIDTH;
  88. port->attr.active_speed = RXE_PORT_ACTIVE_SPEED;
  89. port->attr.phys_state = RXE_PORT_PHYS_STATE;
  90. port->mtu_cap = ib_mtu_enum_to_int(IB_MTU_256);
  91. port->subnet_prefix = cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
  92. }
  93. /* initialize port state, note IB convention that HCA ports are always
  94. * numbered from 1
  95. */
  96. static void rxe_init_ports(struct rxe_dev *rxe)
  97. {
  98. struct rxe_port *port = &rxe->port;
  99. rxe_init_port_param(port);
  100. addrconf_addr_eui48((unsigned char *)&port->port_guid,
  101. rxe->ndev->dev_addr);
  102. spin_lock_init(&port->port_lock);
  103. }
  104. /* init pools of managed objects */
  105. static void rxe_init_pools(struct rxe_dev *rxe)
  106. {
  107. rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC);
  108. rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD);
  109. rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH);
  110. rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ);
  111. rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP);
  112. rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ);
  113. rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR);
  114. rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW);
  115. }
  116. /* initialize rxe device state */
  117. static void rxe_init(struct rxe_dev *rxe)
  118. {
  119. /* init default device parameters */
  120. rxe_init_device_param(rxe);
  121. rxe_init_ports(rxe);
  122. rxe_init_pools(rxe);
  123. /* init pending mmap list */
  124. spin_lock_init(&rxe->mmap_offset_lock);
  125. spin_lock_init(&rxe->pending_lock);
  126. INIT_LIST_HEAD(&rxe->pending_mmaps);
  127. /* init multicast support */
  128. spin_lock_init(&rxe->mcg_lock);
  129. rxe->mcg_tree = RB_ROOT;
  130. mutex_init(&rxe->usdev_lock);
  131. }
  132. void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
  133. {
  134. struct rxe_port *port = &rxe->port;
  135. enum ib_mtu mtu;
  136. mtu = eth_mtu_int_to_enum(ndev_mtu);
  137. /* Make sure that new MTU in range */
  138. mtu = mtu ? min_t(enum ib_mtu, mtu, IB_MTU_4096) : IB_MTU_256;
  139. port->attr.active_mtu = mtu;
  140. port->mtu_cap = ib_mtu_enum_to_int(mtu);
  141. }
  142. /* called by ifc layer to create new rxe device.
  143. * The caller should allocate memory for rxe by calling ib_alloc_device.
  144. */
  145. int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name)
  146. {
  147. rxe_init(rxe);
  148. rxe_set_mtu(rxe, mtu);
  149. return rxe_register_device(rxe, ibdev_name);
  150. }
  151. static int rxe_newlink(const char *ibdev_name, struct net_device *ndev)
  152. {
  153. struct rxe_dev *exists;
  154. int err = 0;
  155. if (is_vlan_dev(ndev)) {
  156. pr_err("rxe creation allowed on top of a real device only\n");
  157. err = -EPERM;
  158. goto err;
  159. }
  160. exists = rxe_get_dev_from_net(ndev);
  161. if (exists) {
  162. ib_device_put(&exists->ib_dev);
  163. pr_err("already configured on %s\n", ndev->name);
  164. err = -EEXIST;
  165. goto err;
  166. }
  167. err = rxe_net_add(ibdev_name, ndev);
  168. if (err) {
  169. pr_err("failed to add %s\n", ndev->name);
  170. goto err;
  171. }
  172. err:
  173. return err;
  174. }
  175. static struct rdma_link_ops rxe_link_ops = {
  176. .type = "rxe",
  177. .newlink = rxe_newlink,
  178. };
  179. static int __init rxe_module_init(void)
  180. {
  181. int err;
  182. err = rxe_net_init();
  183. if (err)
  184. return err;
  185. rdma_link_register(&rxe_link_ops);
  186. pr_info("loaded\n");
  187. return 0;
  188. }
  189. static void __exit rxe_module_exit(void)
  190. {
  191. rdma_link_unregister(&rxe_link_ops);
  192. ib_unregister_driver(RDMA_DRIVER_RXE);
  193. rxe_net_exit();
  194. pr_info("unloaded\n");
  195. }
  196. late_initcall(rxe_module_init);
  197. module_exit(rxe_module_exit);
  198. MODULE_ALIAS_RDMA_LINK("rxe");