smi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. *
  9. * This software is available to you under a choice of one of two
  10. * licenses. You may choose to be licensed under the terms of the GNU
  11. * General Public License (GPL) Version 2, available from the file
  12. * COPYING in the main directory of this source tree, or the
  13. * OpenIB.org BSD license below:
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials
  26. * provided with the distribution.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  32. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  33. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. *
  37. */
  38. #include <rdma/ib_smi.h>
  39. #include "smi.h"
  40. /*
  41. * Fixup a directed route SMP for sending
  42. * Return 0 if the SMP should be discarded
  43. */
  44. enum smi_action smi_handle_dr_smp_send(struct ib_smp *smp,
  45. u8 node_type, int port_num)
  46. {
  47. u8 hop_ptr, hop_cnt;
  48. hop_ptr = smp->hop_ptr;
  49. hop_cnt = smp->hop_cnt;
  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 (!ib_get_smp_direction(smp)) {
  55. /* C14-9:1 */
  56. if (hop_cnt && hop_ptr == 0) {
  57. smp->hop_ptr++;
  58. return (smp->initial_path[smp->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. /* smp->return_path set when received */
  66. smp->hop_ptr++;
  67. return (smp->initial_path[smp->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. /* smp->return_path set when received */
  73. smp->hop_ptr++;
  74. return (node_type == RDMA_NODE_IB_SWITCH ||
  75. smp->dr_dlid == IB_LID_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. smp->hop_ptr--;
  85. return (smp->return_path[smp->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. smp->hop_ptr--;
  93. return (smp->return_path[smp->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. smp->hop_ptr--;
  99. /* C14-13:3 -- SMPs destined for SM shouldn't be here */
  100. return (node_type == RDMA_NODE_IB_SWITCH ||
  101. smp->dr_slid == IB_LID_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. * Adjust information for a received SMP
  113. * Return 0 if the SMP should be dropped
  114. */
  115. enum smi_action smi_handle_dr_smp_recv(struct ib_smp *smp, u8 node_type,
  116. int port_num, int phys_port_cnt)
  117. {
  118. u8 hop_ptr, hop_cnt;
  119. hop_ptr = smp->hop_ptr;
  120. hop_cnt = smp->hop_cnt;
  121. /* See section 14.2.2.2, Vol 1 IB spec */
  122. /* C14-6 -- valid hop_cnt values are from 0 to 63 */
  123. if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)
  124. return IB_SMI_DISCARD;
  125. if (!ib_get_smp_direction(smp)) {
  126. /* C14-9:1 -- sender should have incremented hop_ptr */
  127. if (hop_cnt && hop_ptr == 0)
  128. return IB_SMI_DISCARD;
  129. /* C14-9:2 -- intermediate hop */
  130. if (hop_ptr && hop_ptr < hop_cnt) {
  131. if (node_type != RDMA_NODE_IB_SWITCH)
  132. return IB_SMI_DISCARD;
  133. smp->return_path[hop_ptr] = port_num;
  134. /* smp->hop_ptr updated when sending */
  135. return (smp->initial_path[hop_ptr+1] <= phys_port_cnt ?
  136. IB_SMI_HANDLE : IB_SMI_DISCARD);
  137. }
  138. /* C14-9:3 -- We're at the end of the DR segment of path */
  139. if (hop_ptr == hop_cnt) {
  140. if (hop_cnt)
  141. smp->return_path[hop_ptr] = port_num;
  142. /* smp->hop_ptr updated when sending */
  143. return (node_type == RDMA_NODE_IB_SWITCH ||
  144. smp->dr_dlid == IB_LID_PERMISSIVE ?
  145. IB_SMI_HANDLE : IB_SMI_DISCARD);
  146. }
  147. /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
  148. /* C14-9:5 -- fail unreasonable hop pointer */
  149. return (hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  150. } else {
  151. /* C14-13:1 */
  152. if (hop_cnt && hop_ptr == hop_cnt + 1) {
  153. smp->hop_ptr--;
  154. return (smp->return_path[smp->hop_ptr] ==
  155. port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  156. }
  157. /* C14-13:2 */
  158. if (2 <= hop_ptr && hop_ptr <= hop_cnt) {
  159. if (node_type != RDMA_NODE_IB_SWITCH)
  160. return IB_SMI_DISCARD;
  161. /* smp->hop_ptr updated when sending */
  162. return (smp->return_path[hop_ptr-1] <= phys_port_cnt ?
  163. IB_SMI_HANDLE : IB_SMI_DISCARD);
  164. }
  165. /* C14-13:3 -- We're at the end of the DR segment of path */
  166. if (hop_ptr == 1) {
  167. if (smp->dr_slid == IB_LID_PERMISSIVE) {
  168. /* giving SMP to SM - update hop_ptr */
  169. smp->hop_ptr--;
  170. return IB_SMI_HANDLE;
  171. }
  172. /* smp->hop_ptr updated when sending */
  173. return (node_type == RDMA_NODE_IB_SWITCH ?
  174. IB_SMI_HANDLE : IB_SMI_DISCARD);
  175. }
  176. /* C14-13:4 -- hop_ptr = 0 -> give to SM */
  177. /* C14-13:5 -- Check for unreasonable hop pointer */
  178. return (hop_ptr == 0 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
  179. }
  180. }
  181. enum smi_forward_action smi_check_forward_dr_smp(struct ib_smp *smp)
  182. {
  183. u8 hop_ptr, hop_cnt;
  184. hop_ptr = smp->hop_ptr;
  185. hop_cnt = smp->hop_cnt;
  186. if (!ib_get_smp_direction(smp)) {
  187. /* C14-9:2 -- intermediate hop */
  188. if (hop_ptr && hop_ptr < hop_cnt)
  189. return IB_SMI_FORWARD;
  190. /* C14-9:3 -- at the end of the DR segment of path */
  191. if (hop_ptr == hop_cnt)
  192. return (smp->dr_dlid == IB_LID_PERMISSIVE ?
  193. IB_SMI_SEND : IB_SMI_LOCAL);
  194. /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
  195. if (hop_ptr == hop_cnt + 1)
  196. return IB_SMI_SEND;
  197. } else {
  198. /* C14-13:2 -- intermediate hop */
  199. if (2 <= hop_ptr && hop_ptr <= hop_cnt)
  200. return IB_SMI_FORWARD;
  201. /* C14-13:3 -- at the end of the DR segment of path */
  202. if (hop_ptr == 1)
  203. return (smp->dr_slid != IB_LID_PERMISSIVE ?
  204. IB_SMI_SEND : IB_SMI_LOCAL);
  205. }
  206. return IB_SMI_LOCAL;
  207. }
  208. /*
  209. * Return the forwarding port number from initial_path for outgoing SMP and
  210. * from return_path for returning SMP
  211. */
  212. int smi_get_fwd_port(struct ib_smp *smp)
  213. {
  214. return (!ib_get_smp_direction(smp) ? smp->initial_path[smp->hop_ptr+1] :
  215. smp->return_path[smp->hop_ptr-1]);
  216. }