ib.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/in.h>
  35. #include <linux/if.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/inetdevice.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/delay.h>
  40. #include "rds.h"
  41. #include "ib.h"
  42. unsigned int fmr_pool_size = RDS_FMR_POOL_SIZE;
  43. unsigned int fmr_message_size = RDS_FMR_SIZE + 1; /* +1 allows for unaligned MRs */
  44. module_param(fmr_pool_size, int, 0444);
  45. MODULE_PARM_DESC(fmr_pool_size, " Max number of fmr per HCA");
  46. module_param(fmr_message_size, int, 0444);
  47. MODULE_PARM_DESC(fmr_message_size, " Max size of a RDMA transfer");
  48. struct list_head rds_ib_devices;
  49. /* NOTE: if also grabbing ibdev lock, grab this first */
  50. DEFINE_SPINLOCK(ib_nodev_conns_lock);
  51. LIST_HEAD(ib_nodev_conns);
  52. void rds_ib_add_one(struct ib_device *device)
  53. {
  54. struct rds_ib_device *rds_ibdev;
  55. struct ib_device_attr *dev_attr;
  56. /* Only handle IB (no iWARP) devices */
  57. if (device->node_type != RDMA_NODE_IB_CA)
  58. return;
  59. dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
  60. if (!dev_attr)
  61. return;
  62. if (ib_query_device(device, dev_attr)) {
  63. rdsdebug("Query device failed for %s\n", device->name);
  64. goto free_attr;
  65. }
  66. rds_ibdev = kmalloc(sizeof *rds_ibdev, GFP_KERNEL);
  67. if (!rds_ibdev)
  68. goto free_attr;
  69. spin_lock_init(&rds_ibdev->spinlock);
  70. rds_ibdev->max_wrs = dev_attr->max_qp_wr;
  71. rds_ibdev->max_sge = min(dev_attr->max_sge, RDS_IB_MAX_SGE);
  72. rds_ibdev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
  73. rds_ibdev->fmr_page_size = 1 << rds_ibdev->fmr_page_shift;
  74. rds_ibdev->fmr_page_mask = ~((u64) rds_ibdev->fmr_page_size - 1);
  75. rds_ibdev->fmr_max_remaps = dev_attr->max_map_per_fmr?: 32;
  76. rds_ibdev->max_fmrs = dev_attr->max_fmr ?
  77. min_t(unsigned int, dev_attr->max_fmr, fmr_pool_size) :
  78. fmr_pool_size;
  79. rds_ibdev->dev = device;
  80. rds_ibdev->pd = ib_alloc_pd(device);
  81. if (IS_ERR(rds_ibdev->pd))
  82. goto free_dev;
  83. rds_ibdev->mr = ib_get_dma_mr(rds_ibdev->pd,
  84. IB_ACCESS_LOCAL_WRITE);
  85. if (IS_ERR(rds_ibdev->mr))
  86. goto err_pd;
  87. rds_ibdev->mr_pool = rds_ib_create_mr_pool(rds_ibdev);
  88. if (IS_ERR(rds_ibdev->mr_pool)) {
  89. rds_ibdev->mr_pool = NULL;
  90. goto err_mr;
  91. }
  92. INIT_LIST_HEAD(&rds_ibdev->ipaddr_list);
  93. INIT_LIST_HEAD(&rds_ibdev->conn_list);
  94. list_add_tail(&rds_ibdev->list, &rds_ib_devices);
  95. ib_set_client_data(device, &rds_ib_client, rds_ibdev);
  96. goto free_attr;
  97. err_mr:
  98. ib_dereg_mr(rds_ibdev->mr);
  99. err_pd:
  100. ib_dealloc_pd(rds_ibdev->pd);
  101. free_dev:
  102. kfree(rds_ibdev);
  103. free_attr:
  104. kfree(dev_attr);
  105. }
  106. void rds_ib_remove_one(struct ib_device *device)
  107. {
  108. struct rds_ib_device *rds_ibdev;
  109. struct rds_ib_ipaddr *i_ipaddr, *i_next;
  110. rds_ibdev = ib_get_client_data(device, &rds_ib_client);
  111. if (!rds_ibdev)
  112. return;
  113. list_for_each_entry_safe(i_ipaddr, i_next, &rds_ibdev->ipaddr_list, list) {
  114. list_del(&i_ipaddr->list);
  115. kfree(i_ipaddr);
  116. }
  117. rds_ib_destroy_conns(rds_ibdev);
  118. if (rds_ibdev->mr_pool)
  119. rds_ib_destroy_mr_pool(rds_ibdev->mr_pool);
  120. ib_dereg_mr(rds_ibdev->mr);
  121. while (ib_dealloc_pd(rds_ibdev->pd)) {
  122. rdsdebug("Failed to dealloc pd %p\n", rds_ibdev->pd);
  123. msleep(1);
  124. }
  125. list_del(&rds_ibdev->list);
  126. kfree(rds_ibdev);
  127. }
  128. struct ib_client rds_ib_client = {
  129. .name = "rds_ib",
  130. .add = rds_ib_add_one,
  131. .remove = rds_ib_remove_one
  132. };
  133. static int rds_ib_conn_info_visitor(struct rds_connection *conn,
  134. void *buffer)
  135. {
  136. struct rds_info_rdma_connection *iinfo = buffer;
  137. struct rds_ib_connection *ic;
  138. /* We will only ever look at IB transports */
  139. if (conn->c_trans != &rds_ib_transport)
  140. return 0;
  141. iinfo->src_addr = conn->c_laddr;
  142. iinfo->dst_addr = conn->c_faddr;
  143. memset(&iinfo->src_gid, 0, sizeof(iinfo->src_gid));
  144. memset(&iinfo->dst_gid, 0, sizeof(iinfo->dst_gid));
  145. if (rds_conn_state(conn) == RDS_CONN_UP) {
  146. struct rds_ib_device *rds_ibdev;
  147. struct rdma_dev_addr *dev_addr;
  148. ic = conn->c_transport_data;
  149. dev_addr = &ic->i_cm_id->route.addr.dev_addr;
  150. ib_addr_get_sgid(dev_addr, (union ib_gid *) &iinfo->src_gid);
  151. ib_addr_get_dgid(dev_addr, (union ib_gid *) &iinfo->dst_gid);
  152. rds_ibdev = ib_get_client_data(ic->i_cm_id->device, &rds_ib_client);
  153. iinfo->max_send_wr = ic->i_send_ring.w_nr;
  154. iinfo->max_recv_wr = ic->i_recv_ring.w_nr;
  155. iinfo->max_send_sge = rds_ibdev->max_sge;
  156. rds_ib_get_mr_info(rds_ibdev, iinfo);
  157. }
  158. return 1;
  159. }
  160. static void rds_ib_ic_info(struct socket *sock, unsigned int len,
  161. struct rds_info_iterator *iter,
  162. struct rds_info_lengths *lens)
  163. {
  164. rds_for_each_conn_info(sock, len, iter, lens,
  165. rds_ib_conn_info_visitor,
  166. sizeof(struct rds_info_rdma_connection));
  167. }
  168. /*
  169. * Early RDS/IB was built to only bind to an address if there is an IPoIB
  170. * device with that address set.
  171. *
  172. * If it were me, I'd advocate for something more flexible. Sending and
  173. * receiving should be device-agnostic. Transports would try and maintain
  174. * connections between peers who have messages queued. Userspace would be
  175. * allowed to influence which paths have priority. We could call userspace
  176. * asserting this policy "routing".
  177. */
  178. static int rds_ib_laddr_check(__be32 addr)
  179. {
  180. int ret;
  181. struct rdma_cm_id *cm_id;
  182. struct sockaddr_in sin;
  183. /* Create a CMA ID and try to bind it. This catches both
  184. * IB and iWARP capable NICs.
  185. */
  186. cm_id = rdma_create_id(NULL, NULL, RDMA_PS_TCP);
  187. if (!cm_id)
  188. return -EADDRNOTAVAIL;
  189. memset(&sin, 0, sizeof(sin));
  190. sin.sin_family = AF_INET;
  191. sin.sin_addr.s_addr = addr;
  192. /* rdma_bind_addr will only succeed for IB & iWARP devices */
  193. ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
  194. /* due to this, we will claim to support iWARP devices unless we
  195. check node_type. */
  196. if (ret || cm_id->device->node_type != RDMA_NODE_IB_CA)
  197. ret = -EADDRNOTAVAIL;
  198. rdsdebug("addr %pI4 ret %d node type %d\n",
  199. &addr, ret,
  200. cm_id->device ? cm_id->device->node_type : -1);
  201. rdma_destroy_id(cm_id);
  202. return ret;
  203. }
  204. void rds_ib_exit(void)
  205. {
  206. rds_info_deregister_func(RDS_INFO_IB_CONNECTIONS, rds_ib_ic_info);
  207. rds_ib_destroy_nodev_conns();
  208. ib_unregister_client(&rds_ib_client);
  209. rds_ib_sysctl_exit();
  210. rds_ib_recv_exit();
  211. rds_trans_unregister(&rds_ib_transport);
  212. }
  213. struct rds_transport rds_ib_transport = {
  214. .laddr_check = rds_ib_laddr_check,
  215. .xmit_complete = rds_ib_xmit_complete,
  216. .xmit = rds_ib_xmit,
  217. .xmit_cong_map = NULL,
  218. .xmit_rdma = rds_ib_xmit_rdma,
  219. .recv = rds_ib_recv,
  220. .conn_alloc = rds_ib_conn_alloc,
  221. .conn_free = rds_ib_conn_free,
  222. .conn_connect = rds_ib_conn_connect,
  223. .conn_shutdown = rds_ib_conn_shutdown,
  224. .inc_copy_to_user = rds_ib_inc_copy_to_user,
  225. .inc_purge = rds_ib_inc_purge,
  226. .inc_free = rds_ib_inc_free,
  227. .cm_initiate_connect = rds_ib_cm_initiate_connect,
  228. .cm_handle_connect = rds_ib_cm_handle_connect,
  229. .cm_connect_complete = rds_ib_cm_connect_complete,
  230. .stats_info_copy = rds_ib_stats_info_copy,
  231. .exit = rds_ib_exit,
  232. .get_mr = rds_ib_get_mr,
  233. .sync_mr = rds_ib_sync_mr,
  234. .free_mr = rds_ib_free_mr,
  235. .flush_mrs = rds_ib_flush_mrs,
  236. .t_owner = THIS_MODULE,
  237. .t_name = "infiniband",
  238. };
  239. int __init rds_ib_init(void)
  240. {
  241. int ret;
  242. INIT_LIST_HEAD(&rds_ib_devices);
  243. ret = ib_register_client(&rds_ib_client);
  244. if (ret)
  245. goto out;
  246. ret = rds_ib_sysctl_init();
  247. if (ret)
  248. goto out_ibreg;
  249. ret = rds_ib_recv_init();
  250. if (ret)
  251. goto out_sysctl;
  252. ret = rds_trans_register(&rds_ib_transport);
  253. if (ret)
  254. goto out_recv;
  255. rds_info_register_func(RDS_INFO_IB_CONNECTIONS, rds_ib_ic_info);
  256. goto out;
  257. out_recv:
  258. rds_ib_recv_exit();
  259. out_sysctl:
  260. rds_ib_sysctl_exit();
  261. out_ibreg:
  262. ib_unregister_client(&rds_ib_client);
  263. out:
  264. return ret;
  265. }
  266. MODULE_LICENSE("GPL");