ehca_av.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * address vector functions
  5. *
  6. * Authors: Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  7. * Khadija Souissi <souissik@de.ibm.com>
  8. * Reinhard Ernst <rernst@de.ibm.com>
  9. * Christoph Raisch <raisch@de.ibm.com>
  10. *
  11. * Copyright (c) 2005 IBM Corporation
  12. *
  13. * All rights reserved.
  14. *
  15. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  16. * BSD.
  17. *
  18. * OpenIB BSD License
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials
  29. * provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  38. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  39. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. #include <linux/slab.h>
  44. #include "ehca_tools.h"
  45. #include "ehca_iverbs.h"
  46. #include "hcp_if.h"
  47. static struct kmem_cache *av_cache;
  48. int ehca_calc_ipd(struct ehca_shca *shca, int port,
  49. enum ib_rate path_rate, u32 *ipd)
  50. {
  51. int path = ib_rate_to_mult(path_rate);
  52. int link, ret;
  53. struct ib_port_attr pa;
  54. if (path_rate == IB_RATE_PORT_CURRENT) {
  55. *ipd = 0;
  56. return 0;
  57. }
  58. if (unlikely(path < 0)) {
  59. ehca_err(&shca->ib_device, "Invalid static rate! path_rate=%x",
  60. path_rate);
  61. return -EINVAL;
  62. }
  63. ret = ehca_query_port(&shca->ib_device, port, &pa);
  64. if (unlikely(ret < 0)) {
  65. ehca_err(&shca->ib_device, "Failed to query port ret=%i", ret);
  66. return ret;
  67. }
  68. link = ib_width_enum_to_int(pa.active_width) * pa.active_speed;
  69. if (path >= link)
  70. /* no need to throttle if path faster than link */
  71. *ipd = 0;
  72. else
  73. /* IPD = round((link / path) - 1) */
  74. *ipd = ((link + (path >> 1)) / path) - 1;
  75. return 0;
  76. }
  77. struct ib_ah *ehca_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
  78. {
  79. int ret;
  80. struct ehca_av *av;
  81. struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
  82. ib_device);
  83. av = kmem_cache_alloc(av_cache, GFP_KERNEL);
  84. if (!av) {
  85. ehca_err(pd->device, "Out of memory pd=%p ah_attr=%p",
  86. pd, ah_attr);
  87. return ERR_PTR(-ENOMEM);
  88. }
  89. av->av.sl = ah_attr->sl;
  90. av->av.dlid = ah_attr->dlid;
  91. av->av.slid_path_bits = ah_attr->src_path_bits;
  92. if (ehca_static_rate < 0) {
  93. u32 ipd;
  94. if (ehca_calc_ipd(shca, ah_attr->port_num,
  95. ah_attr->static_rate, &ipd)) {
  96. ret = -EINVAL;
  97. goto create_ah_exit1;
  98. }
  99. av->av.ipd = ipd;
  100. } else
  101. av->av.ipd = ehca_static_rate;
  102. av->av.lnh = ah_attr->ah_flags;
  103. av->av.grh.word_0 = EHCA_BMASK_SET(GRH_IPVERSION_MASK, 6);
  104. av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_TCLASS_MASK,
  105. ah_attr->grh.traffic_class);
  106. av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_FLOWLABEL_MASK,
  107. ah_attr->grh.flow_label);
  108. av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_HOPLIMIT_MASK,
  109. ah_attr->grh.hop_limit);
  110. av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_NEXTHEADER_MASK, 0x1B);
  111. /* set sgid in grh.word_1 */
  112. if (ah_attr->ah_flags & IB_AH_GRH) {
  113. int rc;
  114. struct ib_port_attr port_attr;
  115. union ib_gid gid;
  116. memset(&port_attr, 0, sizeof(port_attr));
  117. rc = ehca_query_port(pd->device, ah_attr->port_num,
  118. &port_attr);
  119. if (rc) { /* invalid port number */
  120. ret = -EINVAL;
  121. ehca_err(pd->device, "Invalid port number "
  122. "ehca_query_port() returned %x "
  123. "pd=%p ah_attr=%p", rc, pd, ah_attr);
  124. goto create_ah_exit1;
  125. }
  126. memset(&gid, 0, sizeof(gid));
  127. rc = ehca_query_gid(pd->device,
  128. ah_attr->port_num,
  129. ah_attr->grh.sgid_index, &gid);
  130. if (rc) {
  131. ret = -EINVAL;
  132. ehca_err(pd->device, "Failed to retrieve sgid "
  133. "ehca_query_gid() returned %x "
  134. "pd=%p ah_attr=%p", rc, pd, ah_attr);
  135. goto create_ah_exit1;
  136. }
  137. memcpy(&av->av.grh.word_1, &gid, sizeof(gid));
  138. }
  139. av->av.pmtu = shca->max_mtu;
  140. /* dgid comes in grh.word_3 */
  141. memcpy(&av->av.grh.word_3, &ah_attr->grh.dgid,
  142. sizeof(ah_attr->grh.dgid));
  143. return &av->ib_ah;
  144. create_ah_exit1:
  145. kmem_cache_free(av_cache, av);
  146. return ERR_PTR(ret);
  147. }
  148. int ehca_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
  149. {
  150. struct ehca_av *av;
  151. struct ehca_ud_av new_ehca_av;
  152. struct ehca_shca *shca = container_of(ah->pd->device, struct ehca_shca,
  153. ib_device);
  154. memset(&new_ehca_av, 0, sizeof(new_ehca_av));
  155. new_ehca_av.sl = ah_attr->sl;
  156. new_ehca_av.dlid = ah_attr->dlid;
  157. new_ehca_av.slid_path_bits = ah_attr->src_path_bits;
  158. new_ehca_av.ipd = ah_attr->static_rate;
  159. new_ehca_av.lnh = EHCA_BMASK_SET(GRH_FLAG_MASK,
  160. (ah_attr->ah_flags & IB_AH_GRH) > 0);
  161. new_ehca_av.grh.word_0 = EHCA_BMASK_SET(GRH_TCLASS_MASK,
  162. ah_attr->grh.traffic_class);
  163. new_ehca_av.grh.word_0 |= EHCA_BMASK_SET(GRH_FLOWLABEL_MASK,
  164. ah_attr->grh.flow_label);
  165. new_ehca_av.grh.word_0 |= EHCA_BMASK_SET(GRH_HOPLIMIT_MASK,
  166. ah_attr->grh.hop_limit);
  167. new_ehca_av.grh.word_0 |= EHCA_BMASK_SET(GRH_NEXTHEADER_MASK, 0x1b);
  168. /* set sgid in grh.word_1 */
  169. if (ah_attr->ah_flags & IB_AH_GRH) {
  170. int rc;
  171. struct ib_port_attr port_attr;
  172. union ib_gid gid;
  173. memset(&port_attr, 0, sizeof(port_attr));
  174. rc = ehca_query_port(ah->device, ah_attr->port_num,
  175. &port_attr);
  176. if (rc) { /* invalid port number */
  177. ehca_err(ah->device, "Invalid port number "
  178. "ehca_query_port() returned %x "
  179. "ah=%p ah_attr=%p port_num=%x",
  180. rc, ah, ah_attr, ah_attr->port_num);
  181. return -EINVAL;
  182. }
  183. memset(&gid, 0, sizeof(gid));
  184. rc = ehca_query_gid(ah->device,
  185. ah_attr->port_num,
  186. ah_attr->grh.sgid_index, &gid);
  187. if (rc) {
  188. ehca_err(ah->device, "Failed to retrieve sgid "
  189. "ehca_query_gid() returned %x "
  190. "ah=%p ah_attr=%p port_num=%x "
  191. "sgid_index=%x",
  192. rc, ah, ah_attr, ah_attr->port_num,
  193. ah_attr->grh.sgid_index);
  194. return -EINVAL;
  195. }
  196. memcpy(&new_ehca_av.grh.word_1, &gid, sizeof(gid));
  197. }
  198. new_ehca_av.pmtu = shca->max_mtu;
  199. memcpy(&new_ehca_av.grh.word_3, &ah_attr->grh.dgid,
  200. sizeof(ah_attr->grh.dgid));
  201. av = container_of(ah, struct ehca_av, ib_ah);
  202. av->av = new_ehca_av;
  203. return 0;
  204. }
  205. int ehca_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
  206. {
  207. struct ehca_av *av = container_of(ah, struct ehca_av, ib_ah);
  208. memcpy(&ah_attr->grh.dgid, &av->av.grh.word_3,
  209. sizeof(ah_attr->grh.dgid));
  210. ah_attr->sl = av->av.sl;
  211. ah_attr->dlid = av->av.dlid;
  212. ah_attr->src_path_bits = av->av.slid_path_bits;
  213. ah_attr->static_rate = av->av.ipd;
  214. ah_attr->ah_flags = EHCA_BMASK_GET(GRH_FLAG_MASK, av->av.lnh);
  215. ah_attr->grh.traffic_class = EHCA_BMASK_GET(GRH_TCLASS_MASK,
  216. av->av.grh.word_0);
  217. ah_attr->grh.hop_limit = EHCA_BMASK_GET(GRH_HOPLIMIT_MASK,
  218. av->av.grh.word_0);
  219. ah_attr->grh.flow_label = EHCA_BMASK_GET(GRH_FLOWLABEL_MASK,
  220. av->av.grh.word_0);
  221. return 0;
  222. }
  223. int ehca_destroy_ah(struct ib_ah *ah)
  224. {
  225. kmem_cache_free(av_cache, container_of(ah, struct ehca_av, ib_ah));
  226. return 0;
  227. }
  228. int ehca_init_av_cache(void)
  229. {
  230. av_cache = kmem_cache_create("ehca_cache_av",
  231. sizeof(struct ehca_av), 0,
  232. SLAB_HWCACHE_ALIGN,
  233. NULL);
  234. if (!av_cache)
  235. return -ENOMEM;
  236. return 0;
  237. }
  238. void ehca_cleanup_av_cache(void)
  239. {
  240. if (av_cache)
  241. kmem_cache_destroy(av_cache);
  242. }