smi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved.
  4. * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
  5. * Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved.
  6. * Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved.
  7. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  8. * Copyright (c) 2014 Intel Corporation. All rights reserved.
  9. *
  10. * This software is available to you under a choice of one of two
  11. * licenses. You may choose to be licensed under the terms of the GNU
  12. * General Public License (GPL) Version 2, available from the file
  13. * COPYING in the main directory of this source tree, or the
  14. * OpenIB.org BSD license below:
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer.
  23. *
  24. * - Redistributions in binary form must reproduce the above
  25. * copyright notice, this list of conditions and the following
  26. * disclaimer in the documentation and/or other materials
  27. * provided with the distribution.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  33. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  35. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  36. * SOFTWARE.
  37. *
  38. */
  39. #include <rdma/ib_smi.h>
  40. #include "smi.h"
  41. #include "opa_smi.h"
  42. static enum smi_action __smi_handle_dr_smp_send(bool is_switch, int port_num,
  43. u8 *hop_ptr, u8 hop_cnt,
  44. const u8 *initial_path,
  45. const u8 *return_path,
  46. u8 direction,
  47. bool dr_dlid_is_permissive,
  48. bool dr_slid_is_permissive)
  49. {
  50. /* See section 14.2.2.2, Vol 1 IB spec */
  51. /* C14-6 -- valid hop_cnt values are from 0 to 63 */
  52. if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)
  53. return IB_SMI_DISCARD;
  54. if (!direction) {
  55. /* C14-9:1 */
  56. if (hop_cnt && *hop_ptr == 0) {
  57. (*hop_ptr)++;
  58. return (initial_path[*hop_ptr] ==
  59. port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  60. }
  61. /* C14-9:2 */
  62. if (*hop_ptr && *hop_ptr < hop_cnt) {
  63. if (!is_switch)
  64. return IB_SMI_DISCARD;
  65. /* return_path set when received */
  66. (*hop_ptr)++;
  67. return (initial_path[*hop_ptr] ==
  68. port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  69. }
  70. /* C14-9:3 -- We're at the end of the DR segment of path */
  71. if (*hop_ptr == hop_cnt) {
  72. /* return_path set when received */
  73. (*hop_ptr)++;
  74. return (is_switch ||
  75. dr_dlid_is_permissive ?
  76. IB_SMI_HANDLE : IB_SMI_DISCARD);
  77. }
  78. /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
  79. /* C14-9:5 -- Fail unreasonable hop pointer */
  80. return (*hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  81. } else {
  82. /* C14-13:1 */
  83. if (hop_cnt && *hop_ptr == hop_cnt + 1) {
  84. (*hop_ptr)--;
  85. return (return_path[*hop_ptr] ==
  86. port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  87. }
  88. /* C14-13:2 */
  89. if (2 <= *hop_ptr && *hop_ptr <= hop_cnt) {
  90. if (!is_switch)
  91. return IB_SMI_DISCARD;
  92. (*hop_ptr)--;
  93. return (return_path[*hop_ptr] ==
  94. port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  95. }
  96. /* C14-13:3 -- at the end of the DR segment of path */
  97. if (*hop_ptr == 1) {
  98. (*hop_ptr)--;
  99. /* C14-13:3 -- SMPs destined for SM shouldn't be here */
  100. return (is_switch ||
  101. dr_slid_is_permissive ?
  102. IB_SMI_HANDLE : IB_SMI_DISCARD);
  103. }
  104. /* C14-13:4 -- hop_ptr = 0 -> should have gone to SM */
  105. if (*hop_ptr == 0)
  106. return IB_SMI_HANDLE;
  107. /* C14-13:5 -- Check for unreasonable hop pointer */
  108. return IB_SMI_DISCARD;
  109. }
  110. }
  111. /*
  112. * Fixup a directed route SMP for sending
  113. * Return IB_SMI_DISCARD if the SMP should be discarded
  114. */
  115. enum smi_action smi_handle_dr_smp_send(struct ib_smp *smp,
  116. bool is_switch, int port_num)
  117. {
  118. return __smi_handle_dr_smp_send(is_switch, port_num,
  119. &smp->hop_ptr, smp->hop_cnt,
  120. smp->initial_path,
  121. smp->return_path,
  122. ib_get_smp_direction(smp),
  123. smp->dr_dlid == IB_LID_PERMISSIVE,
  124. smp->dr_slid == IB_LID_PERMISSIVE);
  125. }
  126. enum smi_action opa_smi_handle_dr_smp_send(struct opa_smp *smp,
  127. bool is_switch, int port_num)
  128. {
  129. return __smi_handle_dr_smp_send(is_switch, port_num,
  130. &smp->hop_ptr, smp->hop_cnt,
  131. smp->route.dr.initial_path,
  132. smp->route.dr.return_path,
  133. opa_get_smp_direction(smp),
  134. smp->route.dr.dr_dlid ==
  135. OPA_LID_PERMISSIVE,
  136. smp->route.dr.dr_slid ==
  137. OPA_LID_PERMISSIVE);
  138. }
  139. static enum smi_action __smi_handle_dr_smp_recv(bool is_switch, int port_num,
  140. int phys_port_cnt,
  141. u8 *hop_ptr, u8 hop_cnt,
  142. const u8 *initial_path,
  143. u8 *return_path,
  144. u8 direction,
  145. bool dr_dlid_is_permissive,
  146. bool dr_slid_is_permissive)
  147. {
  148. /* See section 14.2.2.2, Vol 1 IB spec */
  149. /* C14-6 -- valid hop_cnt values are from 0 to 63 */
  150. if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)
  151. return IB_SMI_DISCARD;
  152. if (!direction) {
  153. /* C14-9:1 -- sender should have incremented hop_ptr */
  154. if (hop_cnt && *hop_ptr == 0)
  155. return IB_SMI_DISCARD;
  156. /* C14-9:2 -- intermediate hop */
  157. if (*hop_ptr && *hop_ptr < hop_cnt) {
  158. if (!is_switch)
  159. return IB_SMI_DISCARD;
  160. return_path[*hop_ptr] = port_num;
  161. /* hop_ptr updated when sending */
  162. return (initial_path[*hop_ptr+1] <= phys_port_cnt ?
  163. IB_SMI_HANDLE : IB_SMI_DISCARD);
  164. }
  165. /* C14-9:3 -- We're at the end of the DR segment of path */
  166. if (*hop_ptr == hop_cnt) {
  167. if (hop_cnt)
  168. return_path[*hop_ptr] = port_num;
  169. /* hop_ptr updated when sending */
  170. return (is_switch ||
  171. dr_dlid_is_permissive ?
  172. IB_SMI_HANDLE : IB_SMI_DISCARD);
  173. }
  174. /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
  175. /* C14-9:5 -- fail unreasonable hop pointer */
  176. return (*hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  177. } else {
  178. /* C14-13:1 */
  179. if (hop_cnt && *hop_ptr == hop_cnt + 1) {
  180. (*hop_ptr)--;
  181. return (return_path[*hop_ptr] ==
  182. port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  183. }
  184. /* C14-13:2 */
  185. if (2 <= *hop_ptr && *hop_ptr <= hop_cnt) {
  186. if (!is_switch)
  187. return IB_SMI_DISCARD;
  188. /* hop_ptr updated when sending */
  189. return (return_path[*hop_ptr-1] <= phys_port_cnt ?
  190. IB_SMI_HANDLE : IB_SMI_DISCARD);
  191. }
  192. /* C14-13:3 -- We're at the end of the DR segment of path */
  193. if (*hop_ptr == 1) {
  194. if (dr_slid_is_permissive) {
  195. /* giving SMP to SM - update hop_ptr */
  196. (*hop_ptr)--;
  197. return IB_SMI_HANDLE;
  198. }
  199. /* hop_ptr updated when sending */
  200. return (is_switch ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  201. }
  202. /* C14-13:4 -- hop_ptr = 0 -> give to SM */
  203. /* C14-13:5 -- Check for unreasonable hop pointer */
  204. return (*hop_ptr == 0 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  205. }
  206. }
  207. /*
  208. * Adjust information for a received SMP
  209. * Return IB_SMI_DISCARD if the SMP should be dropped
  210. */
  211. enum smi_action smi_handle_dr_smp_recv(struct ib_smp *smp, bool is_switch,
  212. int port_num, int phys_port_cnt)
  213. {
  214. return __smi_handle_dr_smp_recv(is_switch, port_num, phys_port_cnt,
  215. &smp->hop_ptr, smp->hop_cnt,
  216. smp->initial_path,
  217. smp->return_path,
  218. ib_get_smp_direction(smp),
  219. smp->dr_dlid == IB_LID_PERMISSIVE,
  220. smp->dr_slid == IB_LID_PERMISSIVE);
  221. }
  222. /*
  223. * Adjust information for a received SMP
  224. * Return IB_SMI_DISCARD if the SMP should be dropped
  225. */
  226. enum smi_action opa_smi_handle_dr_smp_recv(struct opa_smp *smp, bool is_switch,
  227. int port_num, int phys_port_cnt)
  228. {
  229. return __smi_handle_dr_smp_recv(is_switch, port_num, phys_port_cnt,
  230. &smp->hop_ptr, smp->hop_cnt,
  231. smp->route.dr.initial_path,
  232. smp->route.dr.return_path,
  233. opa_get_smp_direction(smp),
  234. smp->route.dr.dr_dlid ==
  235. OPA_LID_PERMISSIVE,
  236. smp->route.dr.dr_slid ==
  237. OPA_LID_PERMISSIVE);
  238. }
  239. static enum smi_forward_action __smi_check_forward_dr_smp(u8 hop_ptr, u8 hop_cnt,
  240. u8 direction,
  241. bool dr_dlid_is_permissive,
  242. bool dr_slid_is_permissive)
  243. {
  244. if (!direction) {
  245. /* C14-9:2 -- intermediate hop */
  246. if (hop_ptr && hop_ptr < hop_cnt)
  247. return IB_SMI_FORWARD;
  248. /* C14-9:3 -- at the end of the DR segment of path */
  249. if (hop_ptr == hop_cnt)
  250. return (dr_dlid_is_permissive ?
  251. IB_SMI_SEND : IB_SMI_LOCAL);
  252. /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
  253. if (hop_ptr == hop_cnt + 1)
  254. return IB_SMI_SEND;
  255. } else {
  256. /* C14-13:2 -- intermediate hop */
  257. if (2 <= hop_ptr && hop_ptr <= hop_cnt)
  258. return IB_SMI_FORWARD;
  259. /* C14-13:3 -- at the end of the DR segment of path */
  260. if (hop_ptr == 1)
  261. return (!dr_slid_is_permissive ?
  262. IB_SMI_SEND : IB_SMI_LOCAL);
  263. }
  264. return IB_SMI_LOCAL;
  265. }
  266. enum smi_forward_action smi_check_forward_dr_smp(struct ib_smp *smp)
  267. {
  268. return __smi_check_forward_dr_smp(smp->hop_ptr, smp->hop_cnt,
  269. ib_get_smp_direction(smp),
  270. smp->dr_dlid == IB_LID_PERMISSIVE,
  271. smp->dr_slid == IB_LID_PERMISSIVE);
  272. }
  273. enum smi_forward_action opa_smi_check_forward_dr_smp(struct opa_smp *smp)
  274. {
  275. return __smi_check_forward_dr_smp(smp->hop_ptr, smp->hop_cnt,
  276. opa_get_smp_direction(smp),
  277. smp->route.dr.dr_dlid ==
  278. OPA_LID_PERMISSIVE,
  279. smp->route.dr.dr_slid ==
  280. OPA_LID_PERMISSIVE);
  281. }
  282. /*
  283. * Return the forwarding port number from initial_path for outgoing SMP and
  284. * from return_path for returning SMP
  285. */
  286. int smi_get_fwd_port(struct ib_smp *smp)
  287. {
  288. return (!ib_get_smp_direction(smp) ? smp->initial_path[smp->hop_ptr+1] :
  289. smp->return_path[smp->hop_ptr-1]);
  290. }
  291. /*
  292. * Return the forwarding port number from initial_path for outgoing SMP and
  293. * from return_path for returning SMP
  294. */
  295. int opa_smi_get_fwd_port(struct opa_smp *smp)
  296. {
  297. return !opa_get_smp_direction(smp) ? smp->route.dr.initial_path[smp->hop_ptr+1] :
  298. smp->route.dr.return_path[smp->hop_ptr-1];
  299. }