ah.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. 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. #include <rdma/ib_addr.h>
  33. #include <rdma/ib_cache.h>
  34. #include <linux/slab.h>
  35. #include <linux/inet.h>
  36. #include <linux/string.h>
  37. #include <linux/mlx4/driver.h>
  38. #include "mlx4_ib.h"
  39. static struct ib_ah *create_ib_ah(struct ib_pd *pd,
  40. struct rdma_ah_attr *ah_attr,
  41. struct mlx4_ib_ah *ah)
  42. {
  43. struct mlx4_dev *dev = to_mdev(pd->device)->dev;
  44. ah->av.ib.port_pd = cpu_to_be32(to_mpd(pd)->pdn |
  45. (rdma_ah_get_port_num(ah_attr) << 24));
  46. ah->av.ib.g_slid = rdma_ah_get_path_bits(ah_attr);
  47. ah->av.ib.sl_tclass_flowlabel =
  48. cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28);
  49. if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
  50. const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
  51. ah->av.ib.g_slid |= 0x80;
  52. ah->av.ib.gid_index = grh->sgid_index;
  53. ah->av.ib.hop_limit = grh->hop_limit;
  54. ah->av.ib.sl_tclass_flowlabel |=
  55. cpu_to_be32((grh->traffic_class << 20) |
  56. grh->flow_label);
  57. memcpy(ah->av.ib.dgid, grh->dgid.raw, 16);
  58. }
  59. ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr));
  60. if (rdma_ah_get_static_rate(ah_attr)) {
  61. u8 static_rate = rdma_ah_get_static_rate(ah_attr) +
  62. MLX4_STAT_RATE_OFFSET;
  63. while (static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  64. !(1 << static_rate & dev->caps.stat_rate_support))
  65. --static_rate;
  66. ah->av.ib.stat_rate = static_rate;
  67. }
  68. return &ah->ibah;
  69. }
  70. static struct ib_ah *create_iboe_ah(struct ib_pd *pd,
  71. struct rdma_ah_attr *ah_attr,
  72. struct mlx4_ib_ah *ah)
  73. {
  74. struct mlx4_ib_dev *ibdev = to_mdev(pd->device);
  75. const struct ib_gid_attr *gid_attr;
  76. struct mlx4_dev *dev = ibdev->dev;
  77. int is_mcast = 0;
  78. struct in6_addr in6;
  79. u16 vlan_tag = 0xffff;
  80. const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
  81. int ret;
  82. memcpy(&in6, grh->dgid.raw, sizeof(in6));
  83. if (rdma_is_multicast_addr(&in6))
  84. is_mcast = 1;
  85. memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN);
  86. eth_zero_addr(ah->av.eth.s_mac);
  87. /*
  88. * If sgid_attr is NULL we are being called by mlx4_ib_create_ah_slave
  89. * and we are directly creating an AV for a slave's gid_index.
  90. */
  91. gid_attr = ah_attr->grh.sgid_attr;
  92. if (gid_attr) {
  93. if (is_vlan_dev(gid_attr->ndev))
  94. vlan_tag = vlan_dev_vlan_id(gid_attr->ndev);
  95. memcpy(ah->av.eth.s_mac, gid_attr->ndev->dev_addr, ETH_ALEN);
  96. ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr);
  97. if (ret < 0)
  98. return ERR_PTR(ret);
  99. ah->av.eth.gid_index = ret;
  100. } else {
  101. /* mlx4_ib_create_ah_slave fills in the s_mac and the vlan */
  102. ah->av.eth.gid_index = ah_attr->grh.sgid_index;
  103. }
  104. if (vlan_tag < 0x1000)
  105. vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
  106. ah->av.eth.port_pd = cpu_to_be32(to_mpd(pd)->pdn |
  107. (rdma_ah_get_port_num(ah_attr) << 24));
  108. ah->av.eth.vlan = cpu_to_be16(vlan_tag);
  109. ah->av.eth.hop_limit = grh->hop_limit;
  110. if (rdma_ah_get_static_rate(ah_attr)) {
  111. ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) +
  112. MLX4_STAT_RATE_OFFSET;
  113. while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  114. !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support))
  115. --ah->av.eth.stat_rate;
  116. }
  117. ah->av.eth.sl_tclass_flowlabel |=
  118. cpu_to_be32((grh->traffic_class << 20) |
  119. grh->flow_label);
  120. /*
  121. * HW requires multicast LID so we just choose one.
  122. */
  123. if (is_mcast)
  124. ah->av.ib.dlid = cpu_to_be16(0xc000);
  125. memcpy(ah->av.eth.dgid, grh->dgid.raw, 16);
  126. ah->av.eth.sl_tclass_flowlabel |= cpu_to_be32(rdma_ah_get_sl(ah_attr)
  127. << 29);
  128. return &ah->ibah;
  129. }
  130. struct ib_ah *mlx4_ib_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
  131. struct ib_udata *udata)
  132. {
  133. struct mlx4_ib_ah *ah;
  134. struct ib_ah *ret;
  135. ah = kzalloc(sizeof *ah, GFP_ATOMIC);
  136. if (!ah)
  137. return ERR_PTR(-ENOMEM);
  138. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
  139. if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) {
  140. ret = ERR_PTR(-EINVAL);
  141. } else {
  142. /*
  143. * TBD: need to handle the case when we get
  144. * called in an atomic context and there we
  145. * might sleep. We don't expect this
  146. * currently since we're working with link
  147. * local addresses which we can translate
  148. * without going to sleep.
  149. */
  150. ret = create_iboe_ah(pd, ah_attr, ah);
  151. }
  152. if (IS_ERR(ret))
  153. kfree(ah);
  154. return ret;
  155. } else
  156. return create_ib_ah(pd, ah_attr, ah); /* never fails */
  157. }
  158. /* AH's created via this call must be free'd by mlx4_ib_destroy_ah. */
  159. struct ib_ah *mlx4_ib_create_ah_slave(struct ib_pd *pd,
  160. struct rdma_ah_attr *ah_attr,
  161. int slave_sgid_index, u8 *s_mac,
  162. u16 vlan_tag)
  163. {
  164. struct rdma_ah_attr slave_attr = *ah_attr;
  165. struct mlx4_ib_ah *mah;
  166. struct ib_ah *ah;
  167. slave_attr.grh.sgid_attr = NULL;
  168. slave_attr.grh.sgid_index = slave_sgid_index;
  169. ah = mlx4_ib_create_ah(pd, &slave_attr, NULL);
  170. if (IS_ERR(ah))
  171. return ah;
  172. ah->device = pd->device;
  173. ah->pd = pd;
  174. ah->type = ah_attr->type;
  175. mah = to_mah(ah);
  176. /* get rid of force-loopback bit */
  177. mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
  178. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
  179. memcpy(mah->av.eth.s_mac, s_mac, 6);
  180. if (vlan_tag < 0x1000)
  181. vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
  182. mah->av.eth.vlan = cpu_to_be16(vlan_tag);
  183. return ah;
  184. }
  185. int mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
  186. {
  187. struct mlx4_ib_ah *ah = to_mah(ibah);
  188. int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24;
  189. memset(ah_attr, 0, sizeof *ah_attr);
  190. ah_attr->type = ibah->type;
  191. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
  192. rdma_ah_set_dlid(ah_attr, 0);
  193. rdma_ah_set_sl(ah_attr,
  194. be32_to_cpu(ah->av.eth.sl_tclass_flowlabel)
  195. >> 29);
  196. } else {
  197. rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid));
  198. rdma_ah_set_sl(ah_attr,
  199. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel)
  200. >> 28);
  201. }
  202. rdma_ah_set_port_num(ah_attr, port_num);
  203. if (ah->av.ib.stat_rate)
  204. rdma_ah_set_static_rate(ah_attr,
  205. ah->av.ib.stat_rate -
  206. MLX4_STAT_RATE_OFFSET);
  207. rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F);
  208. if (mlx4_ib_ah_grh_present(ah)) {
  209. u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel);
  210. rdma_ah_set_grh(ah_attr, NULL,
  211. tc_fl & 0xfffff, ah->av.ib.gid_index,
  212. ah->av.ib.hop_limit,
  213. tc_fl >> 20);
  214. rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid);
  215. }
  216. return 0;
  217. }
  218. int mlx4_ib_destroy_ah(struct ib_ah *ah)
  219. {
  220. kfree(to_mah(ah));
  221. return 0;
  222. }