smi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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(u8 node_type, 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 (node_type != RDMA_NODE_IB_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 (node_type == RDMA_NODE_IB_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 (node_type != RDMA_NODE_IB_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 (node_type == RDMA_NODE_IB_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. u8 node_type, int port_num)
  117. {
  118. return __smi_handle_dr_smp_send(node_type, 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. u8 node_type, int port_num)
  128. {
  129. return __smi_handle_dr_smp_send(node_type, 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(u8 node_type, 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 (node_type != RDMA_NODE_IB_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 (node_type == RDMA_NODE_IB_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 (node_type != RDMA_NODE_IB_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 (node_type == RDMA_NODE_IB_SWITCH ?
  201. IB_SMI_HANDLE : IB_SMI_DISCARD);
  202. }
  203. /* C14-13:4 -- hop_ptr = 0 -> give to SM */
  204. /* C14-13:5 -- Check for unreasonable hop pointer */
  205. return (*hop_ptr == 0 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  206. }
  207. }
  208. /*
  209. * Adjust information for a received SMP
  210. * Return IB_SMI_DISCARD if the SMP should be dropped
  211. */
  212. enum smi_action smi_handle_dr_smp_recv(struct ib_smp *smp, u8 node_type,
  213. int port_num, int phys_port_cnt)
  214. {
  215. return __smi_handle_dr_smp_recv(node_type, port_num, phys_port_cnt,
  216. &smp->hop_ptr, smp->hop_cnt,
  217. smp->initial_path,
  218. smp->return_path,
  219. ib_get_smp_direction(smp),
  220. smp->dr_dlid == IB_LID_PERMISSIVE,
  221. smp->dr_slid == IB_LID_PERMISSIVE);
  222. }
  223. /*
  224. * Adjust information for a received SMP
  225. * Return IB_SMI_DISCARD if the SMP should be dropped
  226. */
  227. enum smi_action opa_smi_handle_dr_smp_recv(struct opa_smp *smp, u8 node_type,
  228. int port_num, int phys_port_cnt)
  229. {
  230. return __smi_handle_dr_smp_recv(node_type, port_num, phys_port_cnt,
  231. &smp->hop_ptr, smp->hop_cnt,
  232. smp->route.dr.initial_path,
  233. smp->route.dr.return_path,
  234. opa_get_smp_direction(smp),
  235. smp->route.dr.dr_dlid ==
  236. OPA_LID_PERMISSIVE,
  237. smp->route.dr.dr_slid ==
  238. OPA_LID_PERMISSIVE);
  239. }
  240. static enum smi_forward_action __smi_check_forward_dr_smp(u8 hop_ptr, u8 hop_cnt,
  241. u8 direction,
  242. bool dr_dlid_is_permissive,
  243. bool dr_slid_is_permissive)
  244. {
  245. if (!direction) {
  246. /* C14-9:2 -- intermediate hop */
  247. if (hop_ptr && hop_ptr < hop_cnt)
  248. return IB_SMI_FORWARD;
  249. /* C14-9:3 -- at the end of the DR segment of path */
  250. if (hop_ptr == hop_cnt)
  251. return (dr_dlid_is_permissive ?
  252. IB_SMI_SEND : IB_SMI_LOCAL);
  253. /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
  254. if (hop_ptr == hop_cnt + 1)
  255. return IB_SMI_SEND;
  256. } else {
  257. /* C14-13:2 -- intermediate hop */
  258. if (2 <= hop_ptr && hop_ptr <= hop_cnt)
  259. return IB_SMI_FORWARD;
  260. /* C14-13:3 -- at the end of the DR segment of path */
  261. if (hop_ptr == 1)
  262. return (!dr_slid_is_permissive ?
  263. IB_SMI_SEND : IB_SMI_LOCAL);
  264. }
  265. return IB_SMI_LOCAL;
  266. }
  267. enum smi_forward_action smi_check_forward_dr_smp(struct ib_smp *smp)
  268. {
  269. return __smi_check_forward_dr_smp(smp->hop_ptr, smp->hop_cnt,
  270. ib_get_smp_direction(smp),
  271. smp->dr_dlid == IB_LID_PERMISSIVE,
  272. smp->dr_slid == IB_LID_PERMISSIVE);
  273. }
  274. enum smi_forward_action opa_smi_check_forward_dr_smp(struct opa_smp *smp)
  275. {
  276. return __smi_check_forward_dr_smp(smp->hop_ptr, smp->hop_cnt,
  277. opa_get_smp_direction(smp),
  278. smp->route.dr.dr_dlid ==
  279. OPA_LID_PERMISSIVE,
  280. smp->route.dr.dr_slid ==
  281. OPA_LID_PERMISSIVE);
  282. }
  283. /*
  284. * Return the forwarding port number from initial_path for outgoing SMP and
  285. * from return_path for returning SMP
  286. */
  287. int smi_get_fwd_port(struct ib_smp *smp)
  288. {
  289. return (!ib_get_smp_direction(smp) ? smp->initial_path[smp->hop_ptr+1] :
  290. smp->return_path[smp->hop_ptr-1]);
  291. }
  292. /*
  293. * Return the forwarding port number from initial_path for outgoing SMP and
  294. * from return_path for returning SMP
  295. */
  296. int opa_smi_get_fwd_port(struct opa_smp *smp)
  297. {
  298. return !opa_get_smp_direction(smp) ? smp->route.dr.initial_path[smp->hop_ptr+1] :
  299. smp->route.dr.return_path[smp->hop_ptr-1];
  300. }