2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved.
5 * Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved.
6 * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
7 * Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved.
8 * Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved.
9 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
10 * Copyright (c) 2014 Intel Corporation. All rights reserved.
12 * This software is available to you under a choice of one of two
13 * licenses. You may choose to be licensed under the terms of the GNU
14 * General Public License (GPL) Version 2, available from the file
15 * COPYING in the main directory of this source tree, or the
16 * OpenIB.org BSD license below:
18 * Redistribution and use in source and binary forms, with or
19 * without modification, are permitted provided that the following
22 * - Redistributions of source code must retain the above
23 * copyright notice, this list of conditions and the following
26 * - Redistributions in binary form must reproduce the above
27 * copyright notice, this list of conditions and the following
28 * disclaimer in the documentation and/or other materials
29 * provided with the distribution.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
35 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
36 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
37 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <rdma/ib_smi.h>
48 static enum smi_action __smi_handle_dr_smp_send(bool is_switch, int port_num,
49 u8 *hop_ptr, u8 hop_cnt,
50 const u8 *initial_path,
51 const u8 *return_path,
53 bool dr_dlid_is_permissive,
54 bool dr_slid_is_permissive)
56 /* See section 14.2.2.2, Vol 1 IB spec */
57 /* C14-6 -- valid hop_cnt values are from 0 to 63 */
58 if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)
59 return IB_SMI_DISCARD;
63 if (hop_cnt && *hop_ptr == 0) {
65 return (initial_path[*hop_ptr] ==
66 port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
70 if (*hop_ptr && *hop_ptr < hop_cnt) {
72 return IB_SMI_DISCARD;
74 /* return_path set when received */
76 return (initial_path[*hop_ptr] ==
77 port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
80 /* C14-9:3 -- We're at the end of the DR segment of path */
81 if (*hop_ptr == hop_cnt) {
82 /* return_path set when received */
85 dr_dlid_is_permissive ?
86 IB_SMI_HANDLE : IB_SMI_DISCARD);
89 /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
90 /* C14-9:5 -- Fail unreasonable hop pointer */
91 return (*hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
95 if (hop_cnt && *hop_ptr == hop_cnt + 1) {
97 return (return_path[*hop_ptr] ==
98 port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
102 if (2 <= *hop_ptr && *hop_ptr <= hop_cnt) {
104 return IB_SMI_DISCARD;
107 return (return_path[*hop_ptr] ==
108 port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
111 /* C14-13:3 -- at the end of the DR segment of path */
114 /* C14-13:3 -- SMPs destined for SM shouldn't be here */
116 dr_slid_is_permissive ?
117 IB_SMI_HANDLE : IB_SMI_DISCARD);
120 /* C14-13:4 -- hop_ptr = 0 -> should have gone to SM */
122 return IB_SMI_HANDLE;
124 /* C14-13:5 -- Check for unreasonable hop pointer */
125 return IB_SMI_DISCARD;
130 * Fixup a directed route SMP for sending
131 * Return IB_SMI_DISCARD if the SMP should be discarded
133 enum smi_action smi_handle_dr_smp_send(struct ib_smp *smp,
134 bool is_switch, int port_num)
136 return __smi_handle_dr_smp_send(is_switch, port_num,
137 &smp->hop_ptr, smp->hop_cnt,
140 ib_get_smp_direction(smp),
141 smp->dr_dlid == IB_LID_PERMISSIVE,
142 smp->dr_slid == IB_LID_PERMISSIVE);
145 enum smi_action opa_smi_handle_dr_smp_send(struct opa_smp *smp,
146 bool is_switch, int port_num)
148 return __smi_handle_dr_smp_send(is_switch, port_num,
149 &smp->hop_ptr, smp->hop_cnt,
150 smp->route.dr.initial_path,
151 smp->route.dr.return_path,
152 opa_get_smp_direction(smp),
153 smp->route.dr.dr_dlid ==
155 smp->route.dr.dr_slid ==
159 static enum smi_action __smi_handle_dr_smp_recv(bool is_switch, int port_num,
161 u8 *hop_ptr, u8 hop_cnt,
162 const u8 *initial_path,
165 bool dr_dlid_is_permissive,
166 bool dr_slid_is_permissive)
168 /* See section 14.2.2.2, Vol 1 IB spec */
169 /* C14-6 -- valid hop_cnt values are from 0 to 63 */
170 if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)
171 return IB_SMI_DISCARD;
174 /* C14-9:1 -- sender should have incremented hop_ptr */
175 if (hop_cnt && *hop_ptr == 0)
176 return IB_SMI_DISCARD;
178 /* C14-9:2 -- intermediate hop */
179 if (*hop_ptr && *hop_ptr < hop_cnt) {
181 return IB_SMI_DISCARD;
183 return_path[*hop_ptr] = port_num;
184 /* hop_ptr updated when sending */
185 return (initial_path[*hop_ptr+1] <= phys_port_cnt ?
186 IB_SMI_HANDLE : IB_SMI_DISCARD);
189 /* C14-9:3 -- We're at the end of the DR segment of path */
190 if (*hop_ptr == hop_cnt) {
192 return_path[*hop_ptr] = port_num;
193 /* hop_ptr updated when sending */
196 dr_dlid_is_permissive ?
197 IB_SMI_HANDLE : IB_SMI_DISCARD);
200 /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
201 /* C14-9:5 -- fail unreasonable hop pointer */
202 return (*hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
207 if (hop_cnt && *hop_ptr == hop_cnt + 1) {
209 return (return_path[*hop_ptr] ==
210 port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);
214 if (2 <= *hop_ptr && *hop_ptr <= hop_cnt) {
216 return IB_SMI_DISCARD;
218 /* hop_ptr updated when sending */
219 return (return_path[*hop_ptr-1] <= phys_port_cnt ?
220 IB_SMI_HANDLE : IB_SMI_DISCARD);
223 /* C14-13:3 -- We're at the end of the DR segment of path */
225 if (dr_slid_is_permissive) {
226 /* giving SMP to SM - update hop_ptr */
228 return IB_SMI_HANDLE;
230 /* hop_ptr updated when sending */
231 return (is_switch ? IB_SMI_HANDLE : IB_SMI_DISCARD);
234 /* C14-13:4 -- hop_ptr = 0 -> give to SM */
235 /* C14-13:5 -- Check for unreasonable hop pointer */
236 return (*hop_ptr == 0 ? IB_SMI_HANDLE : IB_SMI_DISCARD);
241 * Adjust information for a received SMP
242 * Return IB_SMI_DISCARD if the SMP should be dropped
244 enum smi_action smi_handle_dr_smp_recv(struct ib_smp *smp, bool is_switch,
245 int port_num, int phys_port_cnt)
247 return __smi_handle_dr_smp_recv(is_switch, port_num, phys_port_cnt,
248 &smp->hop_ptr, smp->hop_cnt,
251 ib_get_smp_direction(smp),
252 smp->dr_dlid == IB_LID_PERMISSIVE,
253 smp->dr_slid == IB_LID_PERMISSIVE);
257 * Adjust information for a received SMP
258 * Return IB_SMI_DISCARD if the SMP should be dropped
260 enum smi_action opa_smi_handle_dr_smp_recv(struct opa_smp *smp, bool is_switch,
261 int port_num, int phys_port_cnt)
263 return __smi_handle_dr_smp_recv(is_switch, port_num, phys_port_cnt,
264 &smp->hop_ptr, smp->hop_cnt,
265 smp->route.dr.initial_path,
266 smp->route.dr.return_path,
267 opa_get_smp_direction(smp),
268 smp->route.dr.dr_dlid ==
270 smp->route.dr.dr_slid ==
274 static enum smi_forward_action __smi_check_forward_dr_smp(u8 hop_ptr, u8 hop_cnt,
276 bool dr_dlid_is_permissive,
277 bool dr_slid_is_permissive)
280 /* C14-9:2 -- intermediate hop */
281 if (hop_ptr && hop_ptr < hop_cnt)
282 return IB_SMI_FORWARD;
284 /* C14-9:3 -- at the end of the DR segment of path */
285 if (hop_ptr == hop_cnt)
286 return (dr_dlid_is_permissive ?
287 IB_SMI_SEND : IB_SMI_LOCAL);
289 /* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */
290 if (hop_ptr == hop_cnt + 1)
293 /* C14-13:2 -- intermediate hop */
294 if (2 <= hop_ptr && hop_ptr <= hop_cnt)
295 return IB_SMI_FORWARD;
297 /* C14-13:3 -- at the end of the DR segment of path */
299 return (!dr_slid_is_permissive ?
300 IB_SMI_SEND : IB_SMI_LOCAL);
306 enum smi_forward_action smi_check_forward_dr_smp(struct ib_smp *smp)
308 return __smi_check_forward_dr_smp(smp->hop_ptr, smp->hop_cnt,
309 ib_get_smp_direction(smp),
310 smp->dr_dlid == IB_LID_PERMISSIVE,
311 smp->dr_slid == IB_LID_PERMISSIVE);
314 enum smi_forward_action opa_smi_check_forward_dr_smp(struct opa_smp *smp)
316 return __smi_check_forward_dr_smp(smp->hop_ptr, smp->hop_cnt,
317 opa_get_smp_direction(smp),
318 smp->route.dr.dr_dlid ==
320 smp->route.dr.dr_slid ==
325 * Return the forwarding port number from initial_path for outgoing SMP and
326 * from return_path for returning SMP
328 int smi_get_fwd_port(struct ib_smp *smp)
330 return (!ib_get_smp_direction(smp) ? smp->initial_path[smp->hop_ptr+1] :
331 smp->return_path[smp->hop_ptr-1]);
335 * Return the forwarding port number from initial_path for outgoing SMP and
336 * from return_path for returning SMP
338 int opa_smi_get_fwd_port(struct opa_smp *smp)
340 return !opa_get_smp_direction(smp) ? smp->route.dr.initial_path[smp->hop_ptr+1] :
341 smp->route.dr.return_path[smp->hop_ptr-1];