1/*
2 * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3 * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4 *
5 * This Software is licensed under one of the following licenses:
6 *
7 * 1) under the terms of the "Common Public License 1.0" a copy of which is
8 *    available from the Open Source Initiative, see
9 *    http://www.opensource.org/licenses/cpl.php.
10 *
11 * 2) under the terms of the "The BSD License" a copy of which is
12 *    available from the Open Source Initiative, see
13 *    http://www.opensource.org/licenses/bsd-license.php.
14 *
15 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
16 *    copy of which is available from the Open Source Initiative, see
17 *    http://www.opensource.org/licenses/gpl-license.php.
18 *
19 * Licensee has the right to choose one of the above licenses.
20 *
21 * Redistributions of source code must retain the above copyright
22 * notice and one of the license notices.
23 *
24 * Redistributions in binary form must reproduce both the above copyright
25 * notice, one of the license notices in the documentation
26 * and/or other materials provided with the distribution.
27 *
28 * $FreeBSD$
29 *
30 */
31
32#if !defined(IB_ADDR_H)
33#define IB_ADDR_H
34
35#include <sys/param.h>
36#include <sys/proc.h>
37#include <sys/condvar.h>
38
39#include <net/if.h>
40#include <net/ethernet.h>
41
42#include <contrib/rdma/ib_verbs.h>
43
44
45#define MAX_ADDR_LEN	20
46
47struct rdma_addr_client {
48	int refcount;
49	struct cv comp;
50	struct mtx lock;
51};
52
53/**
54 * rdma_addr_register_client - Register an address client.
55 */
56void rdma_addr_register_client(struct rdma_addr_client *client);
57
58/**
59 * rdma_addr_unregister_client - Deregister an address client.
60 * @client: Client object to deregister.
61 */
62void rdma_addr_unregister_client(struct rdma_addr_client *client);
63
64struct rdma_dev_addr {
65	unsigned char src_dev_addr[MAX_ADDR_LEN];
66	unsigned char dst_dev_addr[MAX_ADDR_LEN];
67	unsigned char broadcast[MAX_ADDR_LEN];
68	enum rdma_node_type dev_type;
69};
70
71/**
72 * rdma_translate_ip - Translate a local IP address to an RDMA hardware
73 *   address.
74 */
75int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr);
76
77/**
78 * rdma_resolve_ip - Resolve source and destination IP addresses to
79 *   RDMA hardware addresses.
80 * @client: Address client associated with request.
81 * @src_addr: An optional source address to use in the resolution.  If a
82 *   source address is not provided, a usable address will be returned via
83 *   the callback.
84 * @dst_addr: The destination address to resolve.
85 * @addr: A reference to a data location that will receive the resolved
86 *   addresses.  The data location must remain valid until the callback has
87 *   been invoked.
88 * @timeout_ms: Amount of time to wait for the address resolution to complete.
89 * @callback: Call invoked once address resolution has completed, timed out,
90 *   or been canceled.  A status of 0 indicates success.
91 * @context: User-specified context associated with the call.
92 */
93int rdma_resolve_ip(struct rdma_addr_client *client,
94		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
95		    struct rdma_dev_addr *addr, int timeout_ms,
96		    void (*callback)(int status, struct sockaddr *src_addr,
97				     struct rdma_dev_addr *addr, void *context),
98		    void *context);
99
100void rdma_addr_cancel(struct rdma_dev_addr *addr);
101
102int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct ifnet *dev,
103	      const unsigned char *dst_dev_addr);
104
105static inline int ip_addr_size(struct sockaddr *addr)
106{
107	return addr->sa_family == AF_INET6 ?
108	       sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
109}
110
111static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
112{
113	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
114}
115
116static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
117{
118	dev_addr->broadcast[8] = pkey >> 8;
119	dev_addr->broadcast[9] = (unsigned char) pkey;
120}
121
122static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
123				    union ib_gid *gid)
124{
125	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
126}
127
128static inline void ib_addr_get_sgid(struct rdma_dev_addr *dev_addr,
129				    union ib_gid *gid)
130{
131	memcpy(gid, dev_addr->src_dev_addr + 4, sizeof *gid);
132}
133
134static inline void ib_addr_set_sgid(struct rdma_dev_addr *dev_addr,
135				    union ib_gid *gid)
136{
137	memcpy(dev_addr->src_dev_addr + 4, gid, sizeof *gid);
138}
139
140static inline void ib_addr_get_dgid(struct rdma_dev_addr *dev_addr,
141				    union ib_gid *gid)
142{
143	memcpy(gid, dev_addr->dst_dev_addr + 4, sizeof *gid);
144}
145
146static inline void ib_addr_set_dgid(struct rdma_dev_addr *dev_addr,
147				    union ib_gid *gid)
148{
149	memcpy(dev_addr->dst_dev_addr + 4, gid, sizeof *gid);
150}
151
152static inline void iw_addr_get_sgid(struct rdma_dev_addr *dev_addr,
153				    union ib_gid *gid)
154{
155	memcpy(gid, dev_addr->src_dev_addr, sizeof *gid);
156}
157
158static inline void iw_addr_get_dgid(struct rdma_dev_addr *dev_addr,
159				    union ib_gid *gid)
160{
161	memcpy(gid, dev_addr->dst_dev_addr, sizeof *gid);
162}
163
164#endif /* IB_ADDR_H */
165