ocrdma_ah.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* This file is part of the Emulex RoCE Device Driver for
  2. * RoCE (RDMA over Converged Ethernet) adapters.
  3. * Copyright (C) 2012-2015 Emulex. All rights reserved.
  4. * EMULEX and SLI are trademarks of Emulex.
  5. * www.emulex.com
  6. *
  7. * This software is available to you under a choice of one of two licenses.
  8. * You may choose to be licensed under the terms of the GNU General Public
  9. * License (GPL) Version 2, available from the file COPYING in the main
  10. * directory of this source tree, or the BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the distribution.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  33. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Contact Information:
  36. * linux-drivers@emulex.com
  37. *
  38. * Emulex
  39. * 3333 Susan Street
  40. * Costa Mesa, CA 92626
  41. */
  42. #include <net/neighbour.h>
  43. #include <net/netevent.h>
  44. #include <rdma/ib_addr.h>
  45. #include <rdma/ib_mad.h>
  46. #include <rdma/ib_cache.h>
  47. #include "ocrdma.h"
  48. #include "ocrdma_verbs.h"
  49. #include "ocrdma_ah.h"
  50. #include "ocrdma_hw.h"
  51. #include "ocrdma_stats.h"
  52. #define OCRDMA_VID_PCP_SHIFT 0xD
  53. static u16 ocrdma_hdr_type_to_proto_num(int devid, u8 hdr_type)
  54. {
  55. switch (hdr_type) {
  56. case OCRDMA_L3_TYPE_IB_GRH:
  57. return (u16)0x8915;
  58. case OCRDMA_L3_TYPE_IPV4:
  59. return (u16)0x0800;
  60. case OCRDMA_L3_TYPE_IPV6:
  61. return (u16)0x86dd;
  62. default:
  63. pr_err("ocrdma%d: Invalid network header\n", devid);
  64. return 0;
  65. }
  66. }
  67. static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah,
  68. struct ib_ah_attr *attr, union ib_gid *sgid,
  69. int pdid, bool *isvlan, u16 vlan_tag)
  70. {
  71. int status;
  72. struct ocrdma_eth_vlan eth;
  73. struct ocrdma_grh grh;
  74. int eth_sz;
  75. u16 proto_num = 0;
  76. u8 nxthdr = 0x11;
  77. struct iphdr ipv4;
  78. union {
  79. struct sockaddr _sockaddr;
  80. struct sockaddr_in _sockaddr_in;
  81. struct sockaddr_in6 _sockaddr_in6;
  82. } sgid_addr, dgid_addr;
  83. memset(&eth, 0, sizeof(eth));
  84. memset(&grh, 0, sizeof(grh));
  85. /* Protocol Number */
  86. proto_num = ocrdma_hdr_type_to_proto_num(dev->id, ah->hdr_type);
  87. if (!proto_num)
  88. return -EINVAL;
  89. nxthdr = (proto_num == 0x8915) ? 0x1b : 0x11;
  90. /* VLAN */
  91. if (!vlan_tag || (vlan_tag > 0xFFF))
  92. vlan_tag = dev->pvid;
  93. if (vlan_tag || dev->pfc_state) {
  94. if (!vlan_tag) {
  95. pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
  96. dev->id);
  97. pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
  98. dev->id);
  99. }
  100. eth.eth_type = cpu_to_be16(0x8100);
  101. eth.roce_eth_type = cpu_to_be16(proto_num);
  102. vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT;
  103. eth.vlan_tag = cpu_to_be16(vlan_tag);
  104. eth_sz = sizeof(struct ocrdma_eth_vlan);
  105. *isvlan = true;
  106. } else {
  107. eth.eth_type = cpu_to_be16(proto_num);
  108. eth_sz = sizeof(struct ocrdma_eth_basic);
  109. }
  110. /* MAC */
  111. memcpy(&eth.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
  112. status = ocrdma_resolve_dmac(dev, attr, &eth.dmac[0]);
  113. if (status)
  114. return status;
  115. ah->sgid_index = attr->grh.sgid_index;
  116. /* Eth HDR */
  117. memcpy(&ah->av->eth_hdr, &eth, eth_sz);
  118. if (ah->hdr_type == RDMA_NETWORK_IPV4) {
  119. *((__be16 *)&ipv4) = htons((4 << 12) | (5 << 8) |
  120. attr->grh.traffic_class);
  121. ipv4.id = cpu_to_be16(pdid);
  122. ipv4.frag_off = htons(IP_DF);
  123. ipv4.tot_len = htons(0);
  124. ipv4.ttl = attr->grh.hop_limit;
  125. ipv4.protocol = nxthdr;
  126. rdma_gid2ip(&sgid_addr._sockaddr, sgid);
  127. ipv4.saddr = sgid_addr._sockaddr_in.sin_addr.s_addr;
  128. rdma_gid2ip(&dgid_addr._sockaddr, &attr->grh.dgid);
  129. ipv4.daddr = dgid_addr._sockaddr_in.sin_addr.s_addr;
  130. memcpy((u8 *)ah->av + eth_sz, &ipv4, sizeof(struct iphdr));
  131. } else {
  132. memcpy(&grh.sgid[0], sgid->raw, sizeof(union ib_gid));
  133. grh.tclass_flow = cpu_to_be32((6 << 28) |
  134. (attr->grh.traffic_class << 24) |
  135. attr->grh.flow_label);
  136. memcpy(&grh.dgid[0], attr->grh.dgid.raw,
  137. sizeof(attr->grh.dgid.raw));
  138. grh.pdid_hoplimit = cpu_to_be32((pdid << 16) |
  139. (nxthdr << 8) |
  140. attr->grh.hop_limit);
  141. memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh));
  142. }
  143. if (*isvlan)
  144. ah->av->valid |= OCRDMA_AV_VLAN_VALID;
  145. ah->av->valid = cpu_to_le32(ah->av->valid);
  146. return status;
  147. }
  148. struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr,
  149. struct ib_udata *udata)
  150. {
  151. u32 *ahid_addr;
  152. int status;
  153. struct ocrdma_ah *ah;
  154. bool isvlan = false;
  155. u16 vlan_tag = 0xffff;
  156. struct ib_gid_attr sgid_attr;
  157. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  158. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  159. union ib_gid sgid;
  160. if (!(attr->ah_flags & IB_AH_GRH))
  161. return ERR_PTR(-EINVAL);
  162. if (atomic_cmpxchg(&dev->update_sl, 1, 0))
  163. ocrdma_init_service_level(dev);
  164. ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
  165. if (!ah)
  166. return ERR_PTR(-ENOMEM);
  167. status = ocrdma_alloc_av(dev, ah);
  168. if (status)
  169. goto av_err;
  170. status = ib_get_cached_gid(&dev->ibdev, 1, attr->grh.sgid_index, &sgid,
  171. &sgid_attr);
  172. if (status) {
  173. pr_err("%s(): Failed to query sgid, status = %d\n",
  174. __func__, status);
  175. goto av_conf_err;
  176. }
  177. if (sgid_attr.ndev) {
  178. if (is_vlan_dev(sgid_attr.ndev))
  179. vlan_tag = vlan_dev_vlan_id(sgid_attr.ndev);
  180. dev_put(sgid_attr.ndev);
  181. }
  182. /* Get network header type for this GID */
  183. ah->hdr_type = ib_gid_to_network_type(sgid_attr.gid_type, &sgid);
  184. if ((pd->uctx) &&
  185. (!rdma_is_multicast_addr((struct in6_addr *)attr->grh.dgid.raw)) &&
  186. (!rdma_link_local_addr((struct in6_addr *)attr->grh.dgid.raw))) {
  187. status = rdma_addr_find_l2_eth_by_grh(&sgid, &attr->grh.dgid,
  188. attr->dmac, &vlan_tag,
  189. &sgid_attr.ndev->ifindex,
  190. NULL);
  191. if (status) {
  192. pr_err("%s(): Failed to resolve dmac from gid."
  193. "status = %d\n", __func__, status);
  194. goto av_conf_err;
  195. }
  196. }
  197. status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan, vlan_tag);
  198. if (status)
  199. goto av_conf_err;
  200. /* if pd is for the user process, pass the ah_id to user space */
  201. if ((pd->uctx) && (pd->uctx->ah_tbl.va)) {
  202. ahid_addr = pd->uctx->ah_tbl.va + attr->dlid;
  203. *ahid_addr = 0;
  204. *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK;
  205. if (ocrdma_is_udp_encap_supported(dev)) {
  206. *ahid_addr |= ((u32)ah->hdr_type &
  207. OCRDMA_AH_L3_TYPE_MASK) <<
  208. OCRDMA_AH_L3_TYPE_SHIFT;
  209. }
  210. if (isvlan)
  211. *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK <<
  212. OCRDMA_AH_VLAN_VALID_SHIFT);
  213. }
  214. return &ah->ibah;
  215. av_conf_err:
  216. ocrdma_free_av(dev, ah);
  217. av_err:
  218. kfree(ah);
  219. return ERR_PTR(status);
  220. }
  221. int ocrdma_destroy_ah(struct ib_ah *ibah)
  222. {
  223. struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
  224. struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device);
  225. ocrdma_free_av(dev, ah);
  226. kfree(ah);
  227. return 0;
  228. }
  229. int ocrdma_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
  230. {
  231. struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
  232. struct ocrdma_av *av = ah->av;
  233. struct ocrdma_grh *grh;
  234. attr->ah_flags |= IB_AH_GRH;
  235. if (ah->av->valid & OCRDMA_AV_VALID) {
  236. grh = (struct ocrdma_grh *)((u8 *)ah->av +
  237. sizeof(struct ocrdma_eth_vlan));
  238. attr->sl = be16_to_cpu(av->eth_hdr.vlan_tag) >> 13;
  239. } else {
  240. grh = (struct ocrdma_grh *)((u8 *)ah->av +
  241. sizeof(struct ocrdma_eth_basic));
  242. attr->sl = 0;
  243. }
  244. memcpy(&attr->grh.dgid.raw[0], &grh->dgid[0], sizeof(grh->dgid));
  245. attr->grh.sgid_index = ah->sgid_index;
  246. attr->grh.hop_limit = be32_to_cpu(grh->pdid_hoplimit) & 0xff;
  247. attr->grh.traffic_class = be32_to_cpu(grh->tclass_flow) >> 24;
  248. attr->grh.flow_label = be32_to_cpu(grh->tclass_flow) & 0x00ffffffff;
  249. return 0;
  250. }
  251. int ocrdma_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
  252. {
  253. /* modify_ah is unsupported */
  254. return -ENOSYS;
  255. }
  256. int ocrdma_process_mad(struct ib_device *ibdev,
  257. int process_mad_flags,
  258. u8 port_num,
  259. const struct ib_wc *in_wc,
  260. const struct ib_grh *in_grh,
  261. const struct ib_mad_hdr *in, size_t in_mad_size,
  262. struct ib_mad_hdr *out, size_t *out_mad_size,
  263. u16 *out_mad_pkey_index)
  264. {
  265. int status;
  266. struct ocrdma_dev *dev;
  267. const struct ib_mad *in_mad = (const struct ib_mad *)in;
  268. struct ib_mad *out_mad = (struct ib_mad *)out;
  269. if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
  270. *out_mad_size != sizeof(*out_mad)))
  271. return IB_MAD_RESULT_FAILURE;
  272. switch (in_mad->mad_hdr.mgmt_class) {
  273. case IB_MGMT_CLASS_PERF_MGMT:
  274. dev = get_ocrdma_dev(ibdev);
  275. if (!ocrdma_pma_counters(dev, out_mad))
  276. status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  277. else
  278. status = IB_MAD_RESULT_SUCCESS;
  279. break;
  280. default:
  281. status = IB_MAD_RESULT_SUCCESS;
  282. break;
  283. }
  284. return status;
  285. }