rxe.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <net/addrconf.h>
  34. #include "rxe.h"
  35. #include "rxe_loc.h"
  36. MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib");
  37. MODULE_DESCRIPTION("Soft RDMA transport");
  38. MODULE_LICENSE("Dual BSD/GPL");
  39. MODULE_VERSION("0.2");
  40. /* free resources for all ports on a device */
  41. static void rxe_cleanup_ports(struct rxe_dev *rxe)
  42. {
  43. kfree(rxe->port.pkey_tbl);
  44. rxe->port.pkey_tbl = NULL;
  45. }
  46. /* free resources for a rxe device all objects created for this device must
  47. * have been destroyed
  48. */
  49. static void rxe_cleanup(struct rxe_dev *rxe)
  50. {
  51. rxe_pool_cleanup(&rxe->uc_pool);
  52. rxe_pool_cleanup(&rxe->pd_pool);
  53. rxe_pool_cleanup(&rxe->ah_pool);
  54. rxe_pool_cleanup(&rxe->srq_pool);
  55. rxe_pool_cleanup(&rxe->qp_pool);
  56. rxe_pool_cleanup(&rxe->cq_pool);
  57. rxe_pool_cleanup(&rxe->mr_pool);
  58. rxe_pool_cleanup(&rxe->mw_pool);
  59. rxe_pool_cleanup(&rxe->mc_grp_pool);
  60. rxe_pool_cleanup(&rxe->mc_elem_pool);
  61. rxe_cleanup_ports(rxe);
  62. crypto_free_shash(rxe->tfm);
  63. }
  64. /* called when all references have been dropped */
  65. void rxe_release(struct kref *kref)
  66. {
  67. struct rxe_dev *rxe = container_of(kref, struct rxe_dev, ref_cnt);
  68. rxe_cleanup(rxe);
  69. ib_dealloc_device(&rxe->ib_dev);
  70. }
  71. void rxe_dev_put(struct rxe_dev *rxe)
  72. {
  73. kref_put(&rxe->ref_cnt, rxe_release);
  74. }
  75. EXPORT_SYMBOL_GPL(rxe_dev_put);
  76. /* initialize rxe device parameters */
  77. static int rxe_init_device_param(struct rxe_dev *rxe)
  78. {
  79. rxe->max_inline_data = RXE_MAX_INLINE_DATA;
  80. rxe->attr.fw_ver = RXE_FW_VER;
  81. rxe->attr.max_mr_size = RXE_MAX_MR_SIZE;
  82. rxe->attr.page_size_cap = RXE_PAGE_SIZE_CAP;
  83. rxe->attr.vendor_id = RXE_VENDOR_ID;
  84. rxe->attr.vendor_part_id = RXE_VENDOR_PART_ID;
  85. rxe->attr.hw_ver = RXE_HW_VER;
  86. rxe->attr.max_qp = RXE_MAX_QP;
  87. rxe->attr.max_qp_wr = RXE_MAX_QP_WR;
  88. rxe->attr.device_cap_flags = RXE_DEVICE_CAP_FLAGS;
  89. rxe->attr.max_sge = RXE_MAX_SGE;
  90. rxe->attr.max_sge_rd = RXE_MAX_SGE_RD;
  91. rxe->attr.max_cq = RXE_MAX_CQ;
  92. rxe->attr.max_cqe = (1 << RXE_MAX_LOG_CQE) - 1;
  93. rxe->attr.max_mr = RXE_MAX_MR;
  94. rxe->attr.max_pd = RXE_MAX_PD;
  95. rxe->attr.max_qp_rd_atom = RXE_MAX_QP_RD_ATOM;
  96. rxe->attr.max_ee_rd_atom = RXE_MAX_EE_RD_ATOM;
  97. rxe->attr.max_res_rd_atom = RXE_MAX_RES_RD_ATOM;
  98. rxe->attr.max_qp_init_rd_atom = RXE_MAX_QP_INIT_RD_ATOM;
  99. rxe->attr.max_ee_init_rd_atom = RXE_MAX_EE_INIT_RD_ATOM;
  100. rxe->attr.atomic_cap = RXE_ATOMIC_CAP;
  101. rxe->attr.max_ee = RXE_MAX_EE;
  102. rxe->attr.max_rdd = RXE_MAX_RDD;
  103. rxe->attr.max_mw = RXE_MAX_MW;
  104. rxe->attr.max_raw_ipv6_qp = RXE_MAX_RAW_IPV6_QP;
  105. rxe->attr.max_raw_ethy_qp = RXE_MAX_RAW_ETHY_QP;
  106. rxe->attr.max_mcast_grp = RXE_MAX_MCAST_GRP;
  107. rxe->attr.max_mcast_qp_attach = RXE_MAX_MCAST_QP_ATTACH;
  108. rxe->attr.max_total_mcast_qp_attach = RXE_MAX_TOT_MCAST_QP_ATTACH;
  109. rxe->attr.max_ah = RXE_MAX_AH;
  110. rxe->attr.max_fmr = RXE_MAX_FMR;
  111. rxe->attr.max_map_per_fmr = RXE_MAX_MAP_PER_FMR;
  112. rxe->attr.max_srq = RXE_MAX_SRQ;
  113. rxe->attr.max_srq_wr = RXE_MAX_SRQ_WR;
  114. rxe->attr.max_srq_sge = RXE_MAX_SRQ_SGE;
  115. rxe->attr.max_fast_reg_page_list_len = RXE_MAX_FMR_PAGE_LIST_LEN;
  116. rxe->attr.max_pkeys = RXE_MAX_PKEYS;
  117. rxe->attr.local_ca_ack_delay = RXE_LOCAL_CA_ACK_DELAY;
  118. rxe->max_ucontext = RXE_MAX_UCONTEXT;
  119. return 0;
  120. }
  121. /* initialize port attributes */
  122. static int rxe_init_port_param(struct rxe_port *port)
  123. {
  124. port->attr.state = RXE_PORT_STATE;
  125. port->attr.max_mtu = RXE_PORT_MAX_MTU;
  126. port->attr.active_mtu = RXE_PORT_ACTIVE_MTU;
  127. port->attr.gid_tbl_len = RXE_PORT_GID_TBL_LEN;
  128. port->attr.port_cap_flags = RXE_PORT_PORT_CAP_FLAGS;
  129. port->attr.max_msg_sz = RXE_PORT_MAX_MSG_SZ;
  130. port->attr.bad_pkey_cntr = RXE_PORT_BAD_PKEY_CNTR;
  131. port->attr.qkey_viol_cntr = RXE_PORT_QKEY_VIOL_CNTR;
  132. port->attr.pkey_tbl_len = RXE_PORT_PKEY_TBL_LEN;
  133. port->attr.lid = RXE_PORT_LID;
  134. port->attr.sm_lid = RXE_PORT_SM_LID;
  135. port->attr.lmc = RXE_PORT_LMC;
  136. port->attr.max_vl_num = RXE_PORT_MAX_VL_NUM;
  137. port->attr.sm_sl = RXE_PORT_SM_SL;
  138. port->attr.subnet_timeout = RXE_PORT_SUBNET_TIMEOUT;
  139. port->attr.init_type_reply = RXE_PORT_INIT_TYPE_REPLY;
  140. port->attr.active_width = RXE_PORT_ACTIVE_WIDTH;
  141. port->attr.active_speed = RXE_PORT_ACTIVE_SPEED;
  142. port->attr.phys_state = RXE_PORT_PHYS_STATE;
  143. port->mtu_cap =
  144. ib_mtu_enum_to_int(RXE_PORT_ACTIVE_MTU);
  145. port->subnet_prefix = cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
  146. return 0;
  147. }
  148. /* initialize port state, note IB convention that HCA ports are always
  149. * numbered from 1
  150. */
  151. static int rxe_init_ports(struct rxe_dev *rxe)
  152. {
  153. struct rxe_port *port = &rxe->port;
  154. rxe_init_port_param(port);
  155. if (!port->attr.pkey_tbl_len || !port->attr.gid_tbl_len)
  156. return -EINVAL;
  157. port->pkey_tbl = kcalloc(port->attr.pkey_tbl_len,
  158. sizeof(*port->pkey_tbl), GFP_KERNEL);
  159. if (!port->pkey_tbl)
  160. return -ENOMEM;
  161. port->pkey_tbl[0] = 0xffff;
  162. addrconf_addr_eui48((unsigned char *)&port->port_guid,
  163. rxe->ndev->dev_addr);
  164. spin_lock_init(&port->port_lock);
  165. return 0;
  166. }
  167. /* init pools of managed objects */
  168. static int rxe_init_pools(struct rxe_dev *rxe)
  169. {
  170. int err;
  171. err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC,
  172. rxe->max_ucontext);
  173. if (err)
  174. goto err1;
  175. err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD,
  176. rxe->attr.max_pd);
  177. if (err)
  178. goto err2;
  179. err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH,
  180. rxe->attr.max_ah);
  181. if (err)
  182. goto err3;
  183. err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ,
  184. rxe->attr.max_srq);
  185. if (err)
  186. goto err4;
  187. err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP,
  188. rxe->attr.max_qp);
  189. if (err)
  190. goto err5;
  191. err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ,
  192. rxe->attr.max_cq);
  193. if (err)
  194. goto err6;
  195. err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR,
  196. rxe->attr.max_mr);
  197. if (err)
  198. goto err7;
  199. err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW,
  200. rxe->attr.max_mw);
  201. if (err)
  202. goto err8;
  203. err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP,
  204. rxe->attr.max_mcast_grp);
  205. if (err)
  206. goto err9;
  207. err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM,
  208. rxe->attr.max_total_mcast_qp_attach);
  209. if (err)
  210. goto err10;
  211. return 0;
  212. err10:
  213. rxe_pool_cleanup(&rxe->mc_grp_pool);
  214. err9:
  215. rxe_pool_cleanup(&rxe->mw_pool);
  216. err8:
  217. rxe_pool_cleanup(&rxe->mr_pool);
  218. err7:
  219. rxe_pool_cleanup(&rxe->cq_pool);
  220. err6:
  221. rxe_pool_cleanup(&rxe->qp_pool);
  222. err5:
  223. rxe_pool_cleanup(&rxe->srq_pool);
  224. err4:
  225. rxe_pool_cleanup(&rxe->ah_pool);
  226. err3:
  227. rxe_pool_cleanup(&rxe->pd_pool);
  228. err2:
  229. rxe_pool_cleanup(&rxe->uc_pool);
  230. err1:
  231. return err;
  232. }
  233. /* initialize rxe device state */
  234. static int rxe_init(struct rxe_dev *rxe)
  235. {
  236. int err;
  237. /* init default device parameters */
  238. rxe_init_device_param(rxe);
  239. err = rxe_init_ports(rxe);
  240. if (err)
  241. goto err1;
  242. err = rxe_init_pools(rxe);
  243. if (err)
  244. goto err2;
  245. /* init pending mmap list */
  246. spin_lock_init(&rxe->mmap_offset_lock);
  247. spin_lock_init(&rxe->pending_lock);
  248. INIT_LIST_HEAD(&rxe->pending_mmaps);
  249. INIT_LIST_HEAD(&rxe->list);
  250. mutex_init(&rxe->usdev_lock);
  251. return 0;
  252. err2:
  253. rxe_cleanup_ports(rxe);
  254. err1:
  255. return err;
  256. }
  257. int rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
  258. {
  259. struct rxe_port *port = &rxe->port;
  260. enum ib_mtu mtu;
  261. mtu = eth_mtu_int_to_enum(ndev_mtu);
  262. /* Make sure that new MTU in range */
  263. mtu = mtu ? min_t(enum ib_mtu, mtu, RXE_PORT_MAX_MTU) : IB_MTU_256;
  264. port->attr.active_mtu = mtu;
  265. port->mtu_cap = ib_mtu_enum_to_int(mtu);
  266. return 0;
  267. }
  268. EXPORT_SYMBOL(rxe_set_mtu);
  269. /* called by ifc layer to create new rxe device.
  270. * The caller should allocate memory for rxe by calling ib_alloc_device.
  271. */
  272. int rxe_add(struct rxe_dev *rxe, unsigned int mtu)
  273. {
  274. int err;
  275. kref_init(&rxe->ref_cnt);
  276. err = rxe_init(rxe);
  277. if (err)
  278. goto err1;
  279. err = rxe_set_mtu(rxe, mtu);
  280. if (err)
  281. goto err1;
  282. err = rxe_register_device(rxe);
  283. if (err)
  284. goto err1;
  285. return 0;
  286. err1:
  287. rxe_dev_put(rxe);
  288. return err;
  289. }
  290. EXPORT_SYMBOL(rxe_add);
  291. /* called by the ifc layer to remove a device */
  292. void rxe_remove(struct rxe_dev *rxe)
  293. {
  294. rxe_unregister_device(rxe);
  295. rxe_dev_put(rxe);
  296. }
  297. EXPORT_SYMBOL(rxe_remove);
  298. static int __init rxe_module_init(void)
  299. {
  300. int err;
  301. /* initialize slab caches for managed objects */
  302. err = rxe_cache_init();
  303. if (err) {
  304. pr_err("unable to init object pools\n");
  305. return err;
  306. }
  307. err = rxe_net_init();
  308. if (err)
  309. return err;
  310. pr_info("loaded\n");
  311. return 0;
  312. }
  313. static void __exit rxe_module_exit(void)
  314. {
  315. rxe_remove_all();
  316. rxe_net_exit();
  317. rxe_cache_exit();
  318. pr_info("unloaded\n");
  319. }
  320. late_initcall(rxe_module_init);
  321. module_exit(rxe_module_exit);