ocrdma_ah.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*******************************************************************
  2. * This file is part of the Emulex RoCE Device Driver for *
  3. * RoCE (RDMA over Converged Ethernet) adapters. *
  4. * Copyright (C) 2008-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *
  20. * Contact Information:
  21. * linux-drivers@emulex.com
  22. *
  23. * Emulex
  24. * 3333 Susan Street
  25. * Costa Mesa, CA 92626
  26. *******************************************************************/
  27. #include <net/neighbour.h>
  28. #include <net/netevent.h>
  29. #include <rdma/ib_addr.h>
  30. #include <rdma/ib_mad.h>
  31. #include "ocrdma.h"
  32. #include "ocrdma_verbs.h"
  33. #include "ocrdma_ah.h"
  34. #include "ocrdma_hw.h"
  35. #include "ocrdma_stats.h"
  36. #define OCRDMA_VID_PCP_SHIFT 0xD
  37. static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah,
  38. struct ib_ah_attr *attr, union ib_gid *sgid,
  39. int pdid, bool *isvlan)
  40. {
  41. int status = 0;
  42. u16 vlan_tag;
  43. struct ocrdma_eth_vlan eth;
  44. struct ocrdma_grh grh;
  45. int eth_sz;
  46. memset(&eth, 0, sizeof(eth));
  47. memset(&grh, 0, sizeof(grh));
  48. /* VLAN */
  49. vlan_tag = attr->vlan_id;
  50. if (!vlan_tag || (vlan_tag > 0xFFF))
  51. vlan_tag = dev->pvid;
  52. if (vlan_tag && (vlan_tag < 0x1000)) {
  53. eth.eth_type = cpu_to_be16(0x8100);
  54. eth.roce_eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE);
  55. vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT;
  56. eth.vlan_tag = cpu_to_be16(vlan_tag);
  57. eth_sz = sizeof(struct ocrdma_eth_vlan);
  58. *isvlan = true;
  59. } else {
  60. eth.eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE);
  61. eth_sz = sizeof(struct ocrdma_eth_basic);
  62. }
  63. /* MAC */
  64. memcpy(&eth.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
  65. status = ocrdma_resolve_dmac(dev, attr, &eth.dmac[0]);
  66. if (status)
  67. return status;
  68. ah->sgid_index = attr->grh.sgid_index;
  69. memcpy(&grh.sgid[0], sgid->raw, sizeof(union ib_gid));
  70. memcpy(&grh.dgid[0], attr->grh.dgid.raw, sizeof(attr->grh.dgid.raw));
  71. grh.tclass_flow = cpu_to_be32((6 << 28) |
  72. (attr->grh.traffic_class << 24) |
  73. attr->grh.flow_label);
  74. /* 0x1b is next header value in GRH */
  75. grh.pdid_hoplimit = cpu_to_be32((pdid << 16) |
  76. (0x1b << 8) | attr->grh.hop_limit);
  77. /* Eth HDR */
  78. memcpy(&ah->av->eth_hdr, &eth, eth_sz);
  79. memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh));
  80. if (*isvlan)
  81. ah->av->valid |= OCRDMA_AV_VLAN_VALID;
  82. ah->av->valid = cpu_to_le32(ah->av->valid);
  83. return status;
  84. }
  85. struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr)
  86. {
  87. u32 *ahid_addr;
  88. bool isvlan = false;
  89. int status;
  90. struct ocrdma_ah *ah;
  91. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  92. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  93. union ib_gid sgid;
  94. if (!(attr->ah_flags & IB_AH_GRH))
  95. return ERR_PTR(-EINVAL);
  96. if (atomic_cmpxchg(&dev->update_sl, 1, 0))
  97. ocrdma_init_service_level(dev);
  98. ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
  99. if (!ah)
  100. return ERR_PTR(-ENOMEM);
  101. status = ocrdma_alloc_av(dev, ah);
  102. if (status)
  103. goto av_err;
  104. status = ocrdma_query_gid(&dev->ibdev, 1, attr->grh.sgid_index, &sgid);
  105. if (status) {
  106. pr_err("%s(): Failed to query sgid, status = %d\n",
  107. __func__, status);
  108. goto av_conf_err;
  109. }
  110. if (pd->uctx) {
  111. status = rdma_addr_find_dmac_by_grh(&sgid, &attr->grh.dgid,
  112. attr->dmac, &attr->vlan_id);
  113. if (status) {
  114. pr_err("%s(): Failed to resolve dmac from gid."
  115. "status = %d\n", __func__, status);
  116. goto av_conf_err;
  117. }
  118. }
  119. status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan);
  120. if (status)
  121. goto av_conf_err;
  122. /* if pd is for the user process, pass the ah_id to user space */
  123. if ((pd->uctx) && (pd->uctx->ah_tbl.va)) {
  124. ahid_addr = pd->uctx->ah_tbl.va + attr->dlid;
  125. *ahid_addr = 0;
  126. *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK;
  127. if (isvlan)
  128. *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK <<
  129. OCRDMA_AH_VLAN_VALID_SHIFT);
  130. }
  131. return &ah->ibah;
  132. av_conf_err:
  133. ocrdma_free_av(dev, ah);
  134. av_err:
  135. kfree(ah);
  136. return ERR_PTR(status);
  137. }
  138. int ocrdma_destroy_ah(struct ib_ah *ibah)
  139. {
  140. struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
  141. struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device);
  142. ocrdma_free_av(dev, ah);
  143. kfree(ah);
  144. return 0;
  145. }
  146. int ocrdma_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
  147. {
  148. struct ocrdma_ah *ah = get_ocrdma_ah(ibah);
  149. struct ocrdma_av *av = ah->av;
  150. struct ocrdma_grh *grh;
  151. attr->ah_flags |= IB_AH_GRH;
  152. if (ah->av->valid & OCRDMA_AV_VALID) {
  153. grh = (struct ocrdma_grh *)((u8 *)ah->av +
  154. sizeof(struct ocrdma_eth_vlan));
  155. attr->sl = be16_to_cpu(av->eth_hdr.vlan_tag) >> 13;
  156. } else {
  157. grh = (struct ocrdma_grh *)((u8 *)ah->av +
  158. sizeof(struct ocrdma_eth_basic));
  159. attr->sl = 0;
  160. }
  161. memcpy(&attr->grh.dgid.raw[0], &grh->dgid[0], sizeof(grh->dgid));
  162. attr->grh.sgid_index = ah->sgid_index;
  163. attr->grh.hop_limit = be32_to_cpu(grh->pdid_hoplimit) & 0xff;
  164. attr->grh.traffic_class = be32_to_cpu(grh->tclass_flow) >> 24;
  165. attr->grh.flow_label = be32_to_cpu(grh->tclass_flow) & 0x00ffffffff;
  166. return 0;
  167. }
  168. int ocrdma_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
  169. {
  170. /* modify_ah is unsupported */
  171. return -ENOSYS;
  172. }
  173. int ocrdma_process_mad(struct ib_device *ibdev,
  174. int process_mad_flags,
  175. u8 port_num,
  176. struct ib_wc *in_wc,
  177. struct ib_grh *in_grh,
  178. struct ib_mad *in_mad, struct ib_mad *out_mad)
  179. {
  180. int status;
  181. struct ocrdma_dev *dev;
  182. switch (in_mad->mad_hdr.mgmt_class) {
  183. case IB_MGMT_CLASS_PERF_MGMT:
  184. dev = get_ocrdma_dev(ibdev);
  185. if (!ocrdma_pma_counters(dev, out_mad))
  186. status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  187. else
  188. status = IB_MAD_RESULT_SUCCESS;
  189. break;
  190. default:
  191. status = IB_MAD_RESULT_SUCCESS;
  192. break;
  193. }
  194. return status;
  195. }