1/*
2
3 * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
4 * Copyright (c) 2005 Intel Corporation.  All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#if !defined(IB_ADDR_H)
36#define IB_ADDR_H
37/*
38 #include <linux/in.h>
39 #include <linux/in6.h>
40 #include <linux/if_arp.h>
41 #include <linux/netdevice.h>
42 #include <linux/socket.h>
43 #include <rdma/ib_verbs.h>
44 */
45#include <rdma/ib_pack.h>
46/*
47 #include <linux/if_vlan.h>
48
49 struct rdma_addr_client {
50 atomic_t refcount;
51 struct completion comp;
52 };
53
54 *
55 * rdma_addr_register_client - Register an address client.
56
57 void rdma_addr_register_client(struct rdma_addr_client *client);
58
59 *
60 * rdma_addr_unregister_client - Deregister an address client.
61 * @client: Client object to deregister.
62
63 void rdma_addr_unregister_client(struct rdma_addr_client *client);
64
65 struct rdma_dev_addr {
66 unsigned char src_dev_addr[MAX_ADDR_LEN];
67 unsigned char dst_dev_addr[MAX_ADDR_LEN];
68 unsigned char broadcast[MAX_ADDR_LEN];
69 unsigned short dev_type;
70 int bound_dev_if;
71 enum rdma_transport_type transport;
72 };
73
74 *
75 * rdma_translate_ip - Translate a local IP address to an RDMA hardware
76 *   address.
77
78 int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr);
79
80 *
81 * rdma_resolve_ip - Resolve source and destination IP addresses to
82 *   RDMA hardware addresses.
83 * @client: Address client associated with request.
84 * @src_addr: An optional source address to use in the resolution.  If a
85 *   source address is not provided, a usable address will be returned via
86 *   the callback.
87 * @dst_addr: The destination address to resolve.
88 * @addr: A reference to a data location that will receive the resolved
89 *   addresses.  The data location must remain valid until the callback has
90 *   been invoked.
91 * @timeout_ms: Amount of time to wait for the address resolution to complete.
92 * @callback: Call invoked once address resolution has completed, timed out,
93 *   or been canceled.  A status of 0 indicates success.
94 * @context: User-specified context associated with the call.
95
96 int rdma_resolve_ip(struct rdma_addr_client *client,
97 struct sockaddr *src_addr, struct sockaddr *dst_addr,
98 struct rdma_dev_addr *addr, int timeout_ms,
99 void (*callback)(int status, struct sockaddr *src_addr,
100 struct rdma_dev_addr *addr, void *context),
101 void *context);
102
103 void rdma_addr_cancel(struct rdma_dev_addr *addr);
104
105 int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
106 const unsigned char *dst_dev_addr);
107
108 static inline int ip_addr_size(struct sockaddr *addr)
109 {
110 return addr->sa_family == AF_INET6 ?
111 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
112 }
113
114 static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
115 {
116 return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
117 }
118
119 static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
120 {
121 dev_addr->broadcast[8] = pkey >> 8;
122 dev_addr->broadcast[9] = (unsigned char) pkey;
123 }
124
125 static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
126 union ib_gid *gid)
127 {
128 memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
129 }
130
131 static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
132 {
133 return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
134 }
135
136 static inline void iboe_mac_vlan_to_ll(union ib_gid *gid, u8 *mac, u16 vid)
137 {
138 memset(gid->raw, 0, 16);
139 *((u32 *)gid->raw) = cpu_to_be32(0xfe800000);
140 if (vid < 0x1000) {
141 gid->raw[12] = vid & 0xff;
142 gid->raw[11] = vid >> 8;
143 } else {
144 gid->raw[12] = 0xfe;
145 gid->raw[11] = 0xff;
146 }
147
148 memcpy(gid->raw + 13, mac + 3, 3);
149 memcpy(gid->raw + 8, mac, 3);
150 gid->raw[8] ^= 2;
151 }
152
153 static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
154 {
155 #ifdef __linux__
156 return dev->priv_flags & IFF_802_1Q_VLAN ?
157 vlan_dev_vlan_id(dev) : 0xffff;
158 #else
159 uint16_t tag;
160
161 if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0)
162 return 0xffff;
163 return tag;
164 #endif
165 }
166
167 static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
168 union ib_gid *gid)
169 {
170 struct net_device *dev;
171 u16 vid = 0xffff;
172
173 dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
174 if (dev) {
175 vid = rdma_vlan_dev_vlan_id(dev);
176 dev_put(dev);
177 }
178
179 iboe_mac_vlan_to_ll(gid, dev_addr->src_dev_addr, vid);
180 }
181
182 static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
183 {
184 if (dev_addr->transport == RDMA_TRANSPORT_IB &&
185 dev_addr->dev_type != ARPHRD_INFINIBAND)
186 iboe_addr_get_sgid(dev_addr, gid);
187 else
188 memcpy(gid, dev_addr->src_dev_addr +
189 rdma_addr_gid_offset(dev_addr), sizeof *gid);
190 }
191
192 static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
193 {
194 memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
195 }
196
197 static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
198 {
199 memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
200 }
201
202 static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
203 {
204 memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
205 }
206 */
207static inline enum ib_mtu iboe_get_mtu(int mtu) {
208
209	/** reduce IB headers from effective IBoE MTU. 28 stands for
210	 * atomic header which is the biggest possible header after BTH*/
211
212	mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
213
214	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
215		return IB_MTU_4096;
216	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
217		return IB_MTU_2048;
218	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
219		return IB_MTU_1024;
220	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
221		return IB_MTU_512;
222	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
223		return IB_MTU_256;
224	else
225		return 0;
226}
227/*
228 #ifdef __linux__
229 static inline int iboe_get_rate(struct net_device *dev)
230 {
231 struct ethtool_cmd cmd;
232
233 if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings ||
234 dev->ethtool_ops->get_settings(dev, &cmd))
235 return IB_RATE_PORT_CURRENT;
236
237 if (cmd.speed >= 40000)
238 return IB_RATE_40_GBPS;
239 else if (cmd.speed >= 30000)
240 return IB_RATE_30_GBPS;
241 else if (cmd.speed >= 20000)
242 return IB_RATE_20_GBPS;
243 else if (cmd.speed >= 10000)
244 return IB_RATE_10_GBPS;
245 else
246 return IB_RATE_PORT_CURRENT;
247 }
248 #else
249 static inline int iboe_get_rate(struct net_device *dev)
250 {
251 uintmax_t baudrate;
252 int exp;
253
254 baudrate = dev->if_baudrate;
255 for (exp = dev->if_baudrate_pf; exp > 0; exp--)
256 baudrate *= 10;
257 if (baudrate >= IF_Gbps(40))
258 return IB_RATE_40_GBPS;
259 else if (baudrate >= IF_Gbps(30))
260 return IB_RATE_30_GBPS;
261 else if (baudrate >= IF_Gbps(20))
262 return IB_RATE_20_GBPS;
263 else if (baudrate >= IF_Gbps(10))
264 return IB_RATE_10_GBPS;
265 else
266 return IB_RATE_PORT_CURRENT;
267 }
268 #endif
269
270 static inline int rdma_link_local_addr(struct in6_addr *addr)
271 {
272 if (addr->s6_addr32[0] == cpu_to_be32(0xfe800000) &&
273 addr->s6_addr32[1] == 0)
274 return 1;
275
276 return 0;
277 }
278
279 static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
280 {
281 memcpy(mac, &addr->s6_addr[8], 3);
282 memcpy(mac + 3, &addr->s6_addr[13], 3);
283 mac[0] ^= 2;
284 }
285
286 static inline int rdma_is_multicast_addr(struct in6_addr *addr)
287 {
288 return addr->s6_addr[0] == 0xff;
289 }
290
291 static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
292 {
293 int i;
294
295 mac[0] = 0x33;
296 mac[1] = 0x33;
297 for (i = 2; i < 6; ++i)
298 mac[i] = addr->s6_addr[i + 10];
299 }
300
301 static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
302 {
303 u16 vid;
304
305 vid = dgid->raw[11] << 8 | dgid->raw[12];
306 return vid < 0x1000 ? vid  : 0xffff;
307 }
308
309 static inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev)
310 {
311 #ifdef __linux__
312 return dev->priv_flags & IFF_802_1Q_VLAN ?
313 vlan_dev_real_dev(dev) : 0;
314 #else
315 return VLAN_TRUNKDEV(__DECONST(struct ifnet *, dev));
316 #endif
317 }
318 */
319#endif  /*IB_ADDR_H*/
320