/*1* Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved.2* Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved.3* Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.4* Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved.5* Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved.6* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.7*8* This software is available to you under a choice of one of two9* licenses. You may choose to be licensed under the terms of the GNU10* General Public License (GPL) Version 2, available from the file11* COPYING in the main directory of this source tree, or the12* OpenIB.org BSD license below:13*14* Redistribution and use in source and binary forms, with or15* without modification, are permitted provided that the following16* conditions are met:17*18* - Redistributions of source code must retain the above19* copyright notice, this list of conditions and the following20* disclaimer.21*22* - Redistributions in binary form must reproduce the above23* copyright notice, this list of conditions and the following24* disclaimer in the documentation and/or other materials25* provided with the distribution.26*27* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,28* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF29* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND30* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS31* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN32* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN33* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE34* SOFTWARE.35*36*/3738#include <rdma/ib_smi.h>39#include "smi.h"4041/*42* Fixup a directed route SMP for sending43* Return 0 if the SMP should be discarded44*/45enum smi_action smi_handle_dr_smp_send(struct ib_smp *smp,46u8 node_type, int port_num)47{48u8 hop_ptr, hop_cnt;4950hop_ptr = smp->hop_ptr;51hop_cnt = smp->hop_cnt;5253/* See section 14.2.2.2, Vol 1 IB spec */54/* C14-6 -- valid hop_cnt values are from 0 to 63 */55if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)56return IB_SMI_DISCARD;5758if (!ib_get_smp_direction(smp)) {59/* C14-9:1 */60if (hop_cnt && hop_ptr == 0) {61smp->hop_ptr++;62return (smp->initial_path[smp->hop_ptr] ==63port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);64}6566/* C14-9:2 */67if (hop_ptr && hop_ptr < hop_cnt) {68if (node_type != RDMA_NODE_IB_SWITCH)69return IB_SMI_DISCARD;7071/* smp->return_path set when received */72smp->hop_ptr++;73return (smp->initial_path[smp->hop_ptr] ==74port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);75}7677/* C14-9:3 -- We're at the end of the DR segment of path */78if (hop_ptr == hop_cnt) {79/* smp->return_path set when received */80smp->hop_ptr++;81return (node_type == RDMA_NODE_IB_SWITCH ||82smp->dr_dlid == IB_LID_PERMISSIVE ?83IB_SMI_HANDLE : IB_SMI_DISCARD);84}8586/* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */87/* C14-9:5 -- Fail unreasonable hop pointer */88return (hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);8990} else {91/* C14-13:1 */92if (hop_cnt && hop_ptr == hop_cnt + 1) {93smp->hop_ptr--;94return (smp->return_path[smp->hop_ptr] ==95port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);96}9798/* C14-13:2 */99if (2 <= hop_ptr && hop_ptr <= hop_cnt) {100if (node_type != RDMA_NODE_IB_SWITCH)101return IB_SMI_DISCARD;102103smp->hop_ptr--;104return (smp->return_path[smp->hop_ptr] ==105port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);106}107108/* C14-13:3 -- at the end of the DR segment of path */109if (hop_ptr == 1) {110smp->hop_ptr--;111/* C14-13:3 -- SMPs destined for SM shouldn't be here */112return (node_type == RDMA_NODE_IB_SWITCH ||113smp->dr_slid == IB_LID_PERMISSIVE ?114IB_SMI_HANDLE : IB_SMI_DISCARD);115}116117/* C14-13:4 -- hop_ptr = 0 -> should have gone to SM */118if (hop_ptr == 0)119return IB_SMI_HANDLE;120121/* C14-13:5 -- Check for unreasonable hop pointer */122return IB_SMI_DISCARD;123}124}125126/*127* Adjust information for a received SMP128* Return 0 if the SMP should be dropped129*/130enum smi_action smi_handle_dr_smp_recv(struct ib_smp *smp, u8 node_type,131int port_num, int phys_port_cnt)132{133u8 hop_ptr, hop_cnt;134135hop_ptr = smp->hop_ptr;136hop_cnt = smp->hop_cnt;137138/* See section 14.2.2.2, Vol 1 IB spec */139/* C14-6 -- valid hop_cnt values are from 0 to 63 */140if (hop_cnt >= IB_SMP_MAX_PATH_HOPS)141return IB_SMI_DISCARD;142143if (!ib_get_smp_direction(smp)) {144/* C14-9:1 -- sender should have incremented hop_ptr */145if (hop_cnt && hop_ptr == 0)146return IB_SMI_DISCARD;147148/* C14-9:2 -- intermediate hop */149if (hop_ptr && hop_ptr < hop_cnt) {150if (node_type != RDMA_NODE_IB_SWITCH)151return IB_SMI_DISCARD;152153smp->return_path[hop_ptr] = port_num;154/* smp->hop_ptr updated when sending */155return (smp->initial_path[hop_ptr+1] <= phys_port_cnt ?156IB_SMI_HANDLE : IB_SMI_DISCARD);157}158159/* C14-9:3 -- We're at the end of the DR segment of path */160if (hop_ptr == hop_cnt) {161if (hop_cnt)162smp->return_path[hop_ptr] = port_num;163/* smp->hop_ptr updated when sending */164165return (node_type == RDMA_NODE_IB_SWITCH ||166smp->dr_dlid == IB_LID_PERMISSIVE ?167IB_SMI_HANDLE : IB_SMI_DISCARD);168}169170/* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */171/* C14-9:5 -- fail unreasonable hop pointer */172return (hop_ptr == hop_cnt + 1 ? IB_SMI_HANDLE : IB_SMI_DISCARD);173174} else {175176/* C14-13:1 */177if (hop_cnt && hop_ptr == hop_cnt + 1) {178smp->hop_ptr--;179return (smp->return_path[smp->hop_ptr] ==180port_num ? IB_SMI_HANDLE : IB_SMI_DISCARD);181}182183/* C14-13:2 */184if (2 <= hop_ptr && hop_ptr <= hop_cnt) {185if (node_type != RDMA_NODE_IB_SWITCH)186return IB_SMI_DISCARD;187188/* smp->hop_ptr updated when sending */189return (smp->return_path[hop_ptr-1] <= phys_port_cnt ?190IB_SMI_HANDLE : IB_SMI_DISCARD);191}192193/* C14-13:3 -- We're at the end of the DR segment of path */194if (hop_ptr == 1) {195if (smp->dr_slid == IB_LID_PERMISSIVE) {196/* giving SMP to SM - update hop_ptr */197smp->hop_ptr--;198return IB_SMI_HANDLE;199}200/* smp->hop_ptr updated when sending */201return (node_type == RDMA_NODE_IB_SWITCH ?202IB_SMI_HANDLE : IB_SMI_DISCARD);203}204205/* C14-13:4 -- hop_ptr = 0 -> give to SM */206/* C14-13:5 -- Check for unreasonable hop pointer */207return (hop_ptr == 0 ? IB_SMI_HANDLE : IB_SMI_DISCARD);208}209}210211enum smi_forward_action smi_check_forward_dr_smp(struct ib_smp *smp)212{213u8 hop_ptr, hop_cnt;214215hop_ptr = smp->hop_ptr;216hop_cnt = smp->hop_cnt;217218if (!ib_get_smp_direction(smp)) {219/* C14-9:2 -- intermediate hop */220if (hop_ptr && hop_ptr < hop_cnt)221return IB_SMI_FORWARD;222223/* C14-9:3 -- at the end of the DR segment of path */224if (hop_ptr == hop_cnt)225return (smp->dr_dlid == IB_LID_PERMISSIVE ?226IB_SMI_SEND : IB_SMI_LOCAL);227228/* C14-9:4 -- hop_ptr = hop_cnt + 1 -> give to SMA/SM */229if (hop_ptr == hop_cnt + 1)230return IB_SMI_SEND;231} else {232/* C14-13:2 -- intermediate hop */233if (2 <= hop_ptr && hop_ptr <= hop_cnt)234return IB_SMI_FORWARD;235236/* C14-13:3 -- at the end of the DR segment of path */237if (hop_ptr == 1)238return (smp->dr_slid != IB_LID_PERMISSIVE ?239IB_SMI_SEND : IB_SMI_LOCAL);240}241return IB_SMI_LOCAL;242}243244/*245* Return the forwarding port number from initial_path for outgoing SMP and246* from return_path for returning SMP247*/248int smi_get_fwd_port(struct ib_smp *smp)249{250return (!ib_get_smp_direction(smp) ? smp->initial_path[smp->hop_ptr+1] :251smp->return_path[smp->hop_ptr-1]);252}253254255