1774a6620SJohn Baldwin /*
2774a6620SJohn Baldwin * Copyright(c) 2017 Intel Corporation.
3774a6620SJohn Baldwin *
4774a6620SJohn Baldwin * This file is provided under a dual BSD/GPLv2 license. When using or
5774a6620SJohn Baldwin * redistributing this file, you may do so under either license.
6774a6620SJohn Baldwin *
7774a6620SJohn Baldwin * GPL LICENSE SUMMARY
8774a6620SJohn Baldwin *
9774a6620SJohn Baldwin * This program is free software; you can redistribute it and/or modify
10774a6620SJohn Baldwin * it under the terms of version 2 of the GNU General Public License as
11774a6620SJohn Baldwin * published by the Free Software Foundation.
12774a6620SJohn Baldwin *
13774a6620SJohn Baldwin * This program is distributed in the hope that it will be useful, but
14774a6620SJohn Baldwin * WITHOUT ANY WARRANTY; without even the implied warranty of
15774a6620SJohn Baldwin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16774a6620SJohn Baldwin * General Public License for more details.
17774a6620SJohn Baldwin *
18774a6620SJohn Baldwin * BSD LICENSE
19774a6620SJohn Baldwin *
20774a6620SJohn Baldwin * Redistribution and use in source and binary forms, with or without
21774a6620SJohn Baldwin * modification, are permitted provided that the following conditions
22774a6620SJohn Baldwin * are met:
23774a6620SJohn Baldwin *
24774a6620SJohn Baldwin * - Redistributions of source code must retain the above copyright
25774a6620SJohn Baldwin * notice, this list of conditions and the following disclaimer.
26774a6620SJohn Baldwin * - Redistributions in binary form must reproduce the above copyright
27774a6620SJohn Baldwin * notice, this list of conditions and the following disclaimer in
28774a6620SJohn Baldwin * the documentation and/or other materials provided with the
29774a6620SJohn Baldwin * distribution.
30774a6620SJohn Baldwin * - Neither the name of Intel Corporation nor the names of its
31774a6620SJohn Baldwin * contributors may be used to endorse or promote products derived
32774a6620SJohn Baldwin * from this software without specific prior written permission.
33774a6620SJohn Baldwin *
34774a6620SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35774a6620SJohn Baldwin * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36774a6620SJohn Baldwin * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37774a6620SJohn Baldwin * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38774a6620SJohn Baldwin * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39774a6620SJohn Baldwin * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40774a6620SJohn Baldwin * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41774a6620SJohn Baldwin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42774a6620SJohn Baldwin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43774a6620SJohn Baldwin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44774a6620SJohn Baldwin * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45774a6620SJohn Baldwin *
46774a6620SJohn Baldwin */
47774a6620SJohn Baldwin
48774a6620SJohn Baldwin #ifndef OPA_ADDR_H
49774a6620SJohn Baldwin #define OPA_ADDR_H
50774a6620SJohn Baldwin
51687057e7SJohn Baldwin #include <rdma/opa_smi.h>
52687057e7SJohn Baldwin
53774a6620SJohn Baldwin #define OPA_SPECIAL_OUI (0x00066AULL)
54774a6620SJohn Baldwin #define OPA_MAKE_ID(x) (cpu_to_be64(OPA_SPECIAL_OUI << 40 | (x)))
55687057e7SJohn Baldwin #define OPA_TO_IB_UCAST_LID(x) (((x) >= be16_to_cpu(IB_MULTICAST_LID_BASE)) \
56687057e7SJohn Baldwin ? 0 : x)
57687057e7SJohn Baldwin /**
58687057e7SJohn Baldwin * 0xF8 - 4 bits of multicast range and 1 bit for collective range
59687057e7SJohn Baldwin * Example: For 24 bit LID space,
60687057e7SJohn Baldwin * Multicast range: 0xF00000 to 0xF7FFFF
61687057e7SJohn Baldwin * Collective range: 0xF80000 to 0xFFFFFE
62687057e7SJohn Baldwin */
63687057e7SJohn Baldwin #define OPA_MCAST_NR 0x4 /* Number of top bits set */
64687057e7SJohn Baldwin #define OPA_COLLECTIVE_NR 0x1 /* Number of bits after MCAST_NR */
65774a6620SJohn Baldwin
66774a6620SJohn Baldwin /**
67774a6620SJohn Baldwin * ib_is_opa_gid: Returns true if the top 24 bits of the gid
68774a6620SJohn Baldwin * contains the OPA_STL_OUI identifier. This identifies that
69774a6620SJohn Baldwin * the provided gid is a special purpose GID meant to carry
70774a6620SJohn Baldwin * extended LID information.
71774a6620SJohn Baldwin *
72774a6620SJohn Baldwin * @gid: The Global identifier
73774a6620SJohn Baldwin */
ib_is_opa_gid(const union ib_gid * gid)74687057e7SJohn Baldwin static inline bool ib_is_opa_gid(const union ib_gid *gid)
75774a6620SJohn Baldwin {
76774a6620SJohn Baldwin return ((be64_to_cpu(gid->global.interface_id) >> 40) ==
77774a6620SJohn Baldwin OPA_SPECIAL_OUI);
78774a6620SJohn Baldwin }
79774a6620SJohn Baldwin
80774a6620SJohn Baldwin /**
81774a6620SJohn Baldwin * opa_get_lid_from_gid: Returns the last 32 bits of the gid.
82774a6620SJohn Baldwin * OPA devices use one of the gids in the gid table to also
83774a6620SJohn Baldwin * store the lid.
84774a6620SJohn Baldwin *
85774a6620SJohn Baldwin * @gid: The Global identifier
86774a6620SJohn Baldwin */
opa_get_lid_from_gid(const union ib_gid * gid)87687057e7SJohn Baldwin static inline u32 opa_get_lid_from_gid(const union ib_gid *gid)
88774a6620SJohn Baldwin {
89774a6620SJohn Baldwin return be64_to_cpu(gid->global.interface_id) & 0xFFFFFFFF;
90774a6620SJohn Baldwin }
91687057e7SJohn Baldwin
92687057e7SJohn Baldwin /**
93687057e7SJohn Baldwin * opa_is_extended_lid: Returns true if dlid or slid are
94687057e7SJohn Baldwin * extended.
95687057e7SJohn Baldwin *
96687057e7SJohn Baldwin * @dlid: The DLID
97687057e7SJohn Baldwin * @slid: The SLID
98687057e7SJohn Baldwin */
opa_is_extended_lid(u32 dlid,u32 slid)99687057e7SJohn Baldwin static inline bool opa_is_extended_lid(u32 dlid, u32 slid)
100687057e7SJohn Baldwin {
101687057e7SJohn Baldwin if ((be32_to_cpu(dlid) >=
102687057e7SJohn Baldwin be16_to_cpu(IB_MULTICAST_LID_BASE)) ||
103687057e7SJohn Baldwin (be32_to_cpu(slid) >=
104687057e7SJohn Baldwin be16_to_cpu(IB_MULTICAST_LID_BASE)))
105687057e7SJohn Baldwin return true;
106687057e7SJohn Baldwin else
107687057e7SJohn Baldwin return false;
108687057e7SJohn Baldwin }
109687057e7SJohn Baldwin
110687057e7SJohn Baldwin /* Get multicast lid base */
opa_get_mcast_base(u32 nr_top_bits)111687057e7SJohn Baldwin static inline u32 opa_get_mcast_base(u32 nr_top_bits)
112687057e7SJohn Baldwin {
113687057e7SJohn Baldwin return (be32_to_cpu(OPA_LID_PERMISSIVE) << (32 - nr_top_bits));
114687057e7SJohn Baldwin }
115687057e7SJohn Baldwin
1161d99feddSJohn Baldwin /* Check for a valid unicast LID for non-SM traffic types */
rdma_is_valid_unicast_lid(struct rdma_ah_attr * attr)1171d99feddSJohn Baldwin static inline bool rdma_is_valid_unicast_lid(struct rdma_ah_attr *attr)
1181d99feddSJohn Baldwin {
1191d99feddSJohn Baldwin if (attr->type == RDMA_AH_ATTR_TYPE_IB) {
1201d99feddSJohn Baldwin if (!rdma_ah_get_dlid(attr) ||
1211d99feddSJohn Baldwin rdma_ah_get_dlid(attr) >=
122*6a75471dSJohn Baldwin be16_to_cpu(IB_MULTICAST_LID_BASE))
1231d99feddSJohn Baldwin return false;
1241d99feddSJohn Baldwin } else if (attr->type == RDMA_AH_ATTR_TYPE_OPA) {
1251d99feddSJohn Baldwin if (!rdma_ah_get_dlid(attr) ||
1261d99feddSJohn Baldwin rdma_ah_get_dlid(attr) >=
1271d99feddSJohn Baldwin opa_get_mcast_base(OPA_MCAST_NR))
1281d99feddSJohn Baldwin return false;
1291d99feddSJohn Baldwin }
1301d99feddSJohn Baldwin return true;
1311d99feddSJohn Baldwin }
132774a6620SJohn Baldwin #endif /* OPA_ADDR_H */
133