1178784Skmacy/*
2178784Skmacy * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3178784Skmacy * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4178784Skmacy *
5178784Skmacy * This Software is licensed under one of the following licenses:
6178784Skmacy *
7178784Skmacy * 1) under the terms of the "Common Public License 1.0" a copy of which is
8178784Skmacy *    available from the Open Source Initiative, see
9178784Skmacy *    http://www.opensource.org/licenses/cpl.php.
10178784Skmacy *
11178784Skmacy * 2) under the terms of the "The BSD License" a copy of which is
12178784Skmacy *    available from the Open Source Initiative, see
13178784Skmacy *    http://www.opensource.org/licenses/bsd-license.php.
14178784Skmacy *
15178784Skmacy * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
16178784Skmacy *    copy of which is available from the Open Source Initiative, see
17178784Skmacy *    http://www.opensource.org/licenses/gpl-license.php.
18178784Skmacy *
19178784Skmacy * Licensee has the right to choose one of the above licenses.
20178784Skmacy *
21178784Skmacy * Redistributions of source code must retain the above copyright
22178784Skmacy * notice and one of the license notices.
23178784Skmacy *
24178784Skmacy * Redistributions in binary form must reproduce both the above copyright
25178784Skmacy * notice, one of the license notices in the documentation
26178784Skmacy * and/or other materials provided with the distribution.
27178784Skmacy *
28178784Skmacy * $FreeBSD$
29178784Skmacy */
30178784Skmacy
31178784Skmacy#if !defined(RDMA_CM_H)
32178784Skmacy#define RDMA_CM_H
33178784Skmacy
34178784Skmacy#include <sys/socket.h>
35178784Skmacy#include <netinet/in.h>
36178784Skmacy#include <contrib/rdma/ib_addr.h>
37178784Skmacy#include <contrib/rdma/ib_sa.h>
38178784Skmacy
39178784Skmacy/*
40178784Skmacy * Upon receiving a device removal event, users must destroy the associated
41178784Skmacy * RDMA identifier and release all resources allocated with the device.
42178784Skmacy */
43178784Skmacyenum rdma_cm_event_type {
44178784Skmacy	RDMA_CM_EVENT_ADDR_RESOLVED,
45178784Skmacy	RDMA_CM_EVENT_ADDR_ERROR,
46178784Skmacy	RDMA_CM_EVENT_ROUTE_RESOLVED,
47178784Skmacy	RDMA_CM_EVENT_ROUTE_ERROR,
48178784Skmacy	RDMA_CM_EVENT_CONNECT_REQUEST,
49178784Skmacy	RDMA_CM_EVENT_CONNECT_RESPONSE,
50178784Skmacy	RDMA_CM_EVENT_CONNECT_ERROR,
51178784Skmacy	RDMA_CM_EVENT_UNREACHABLE,
52178784Skmacy	RDMA_CM_EVENT_REJECTED,
53178784Skmacy	RDMA_CM_EVENT_ESTABLISHED,
54178784Skmacy	RDMA_CM_EVENT_DISCONNECTED,
55178784Skmacy	RDMA_CM_EVENT_DEVICE_REMOVAL,
56178784Skmacy	RDMA_CM_EVENT_MULTICAST_JOIN,
57178784Skmacy	RDMA_CM_EVENT_MULTICAST_ERROR
58178784Skmacy};
59178784Skmacy
60178784Skmacyenum rdma_port_space {
61178784Skmacy	RDMA_PS_SDP  = 0x0001,
62178784Skmacy	RDMA_PS_IPOIB= 0x0002,
63178784Skmacy	RDMA_PS_TCP  = 0x0106,
64178784Skmacy	RDMA_PS_UDP  = 0x0111,
65178784Skmacy	RDMA_PS_SCTP = 0x0183
66178784Skmacy};
67178784Skmacy
68178784Skmacystruct rdma_addr {
69178784Skmacy	struct sockaddr src_addr;
70178784Skmacy	u8		src_pad[sizeof(struct sockaddr_in6) -
71178784Skmacy				sizeof(struct sockaddr)];
72178784Skmacy	struct sockaddr dst_addr;
73178784Skmacy	u8		dst_pad[sizeof(struct sockaddr_in6) -
74178784Skmacy				sizeof(struct sockaddr)];
75178784Skmacy	struct rdma_dev_addr dev_addr;
76178784Skmacy};
77178784Skmacy
78178784Skmacystruct rdma_route {
79178784Skmacy	struct rdma_addr addr;
80178784Skmacy	struct ib_sa_path_rec *path_rec;
81178784Skmacy	int num_paths;
82178784Skmacy};
83178784Skmacy
84178784Skmacystruct rdma_conn_param {
85178784Skmacy	const void *private_data;
86178784Skmacy	u8 private_data_len;
87178784Skmacy	u8 responder_resources;
88178784Skmacy	u8 initiator_depth;
89178784Skmacy	u8 flow_control;
90178784Skmacy	u8 retry_count;		/* ignored when accepting */
91178784Skmacy	u8 rnr_retry_count;
92178784Skmacy	/* Fields below ignored if a QP is created on the rdma_cm_id. */
93178784Skmacy	u8 srq;
94178784Skmacy	u32 qp_num;
95178784Skmacy};
96178784Skmacy
97178784Skmacystruct rdma_ud_param {
98178784Skmacy	const void *private_data;
99178784Skmacy	u8 private_data_len;
100178784Skmacy	struct ib_ah_attr ah_attr;
101178784Skmacy	u32 qp_num;
102178784Skmacy	u32 qkey;
103178784Skmacy};
104178784Skmacy
105178784Skmacystruct rdma_cm_event {
106178784Skmacy	enum rdma_cm_event_type	 event;
107178784Skmacy	int			 status;
108178784Skmacy	union {
109178784Skmacy		struct rdma_conn_param	conn;
110178784Skmacy		struct rdma_ud_param	ud;
111178784Skmacy	} param;
112178784Skmacy};
113178784Skmacy
114178784Skmacystruct rdma_cm_id;
115178784Skmacy
116178784Skmacy/**
117178784Skmacy * rdma_cm_event_handler - Callback used to report user events.
118178784Skmacy *
119178784Skmacy * Notes: Users may not call rdma_destroy_id from this callback to destroy
120178784Skmacy *   the passed in id, or a corresponding listen id.  Returning a
121178784Skmacy *   non-zero value from the callback will destroy the passed in id.
122178784Skmacy */
123178784Skmacytypedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id,
124178784Skmacy				     struct rdma_cm_event *event);
125178784Skmacy
126178784Skmacystruct rdma_cm_id {
127178784Skmacy	struct ib_device	*device;
128178784Skmacy	void			*context;
129178784Skmacy	struct ib_qp		*qp;
130178784Skmacy	rdma_cm_event_handler	 event_handler;
131178784Skmacy	struct rdma_route	 route;
132178784Skmacy	enum rdma_port_space	 ps;
133178784Skmacy	u8			 port_num;
134178784Skmacy};
135178784Skmacy
136178784Skmacy/**
137178784Skmacy * rdma_create_id - Create an RDMA identifier.
138178784Skmacy *
139178784Skmacy * @event_handler: User callback invoked to report events associated with the
140178784Skmacy *   returned rdma_id.
141178784Skmacy * @context: User specified context associated with the id.
142178784Skmacy * @ps: RDMA port space.
143178784Skmacy */
144178784Skmacystruct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
145178784Skmacy				  void *context, enum rdma_port_space ps);
146178784Skmacy
147178784Skmacy/**
148178784Skmacy  * rdma_destroy_id - Destroys an RDMA identifier.
149178784Skmacy  *
150178784Skmacy  * @id: RDMA identifier.
151178784Skmacy  *
152178784Skmacy  * Note: calling this function has the effect of canceling in-flight
153178784Skmacy  * asynchronous operations associated with the id.
154178784Skmacy  */
155178784Skmacyvoid rdma_destroy_id(struct rdma_cm_id *id);
156178784Skmacy
157178784Skmacy/**
158178784Skmacy * rdma_bind_addr - Bind an RDMA identifier to a source address and
159178784Skmacy *   associated RDMA device, if needed.
160178784Skmacy *
161178784Skmacy * @id: RDMA identifier.
162178784Skmacy * @addr: Local address information.  Wildcard values are permitted.
163178784Skmacy *
164178784Skmacy * This associates a source address with the RDMA identifier before calling
165178784Skmacy * rdma_listen.  If a specific local address is given, the RDMA identifier will
166178784Skmacy * be bound to a local RDMA device.
167178784Skmacy */
168178784Skmacyint rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr);
169178784Skmacy
170178784Skmacy/**
171178784Skmacy * rdma_resolve_addr - Resolve destination and optional source addresses
172178784Skmacy *   from IP addresses to an RDMA address.  If successful, the specified
173178784Skmacy *   rdma_cm_id will be bound to a local device.
174178784Skmacy *
175178784Skmacy * @id: RDMA identifier.
176178784Skmacy * @src_addr: Source address information.  This parameter may be NULL.
177178784Skmacy * @dst_addr: Destination address information.
178178784Skmacy * @timeout_ms: Time to wait for resolution to complete.
179178784Skmacy */
180178784Skmacyint rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
181178784Skmacy		      struct sockaddr *dst_addr, int timeout_ms);
182178784Skmacy
183178784Skmacy/**
184178784Skmacy * rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier
185178784Skmacy *   into route information needed to establish a connection.
186178784Skmacy *
187178784Skmacy * This is called on the client side of a connection.
188178784Skmacy * Users must have first called rdma_resolve_addr to resolve a dst_addr
189178784Skmacy * into an RDMA address before calling this routine.
190178784Skmacy */
191178784Skmacyint rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms);
192178784Skmacy
193178784Skmacy/**
194178784Skmacy * rdma_create_qp - Allocate a QP and associate it with the specified RDMA
195178784Skmacy * identifier.
196178784Skmacy *
197178784Skmacy * QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA
198178784Skmacy * through their states.
199178784Skmacy */
200178784Skmacyint rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
201178784Skmacy		   struct ib_qp_init_attr *qp_init_attr);
202178784Skmacy
203178784Skmacy/**
204178784Skmacy * rdma_destroy_qp - Deallocate the QP associated with the specified RDMA
205178784Skmacy * identifier.
206178784Skmacy *
207178784Skmacy * Users must destroy any QP associated with an RDMA identifier before
208178784Skmacy * destroying the RDMA ID.
209178784Skmacy */
210178784Skmacyvoid rdma_destroy_qp(struct rdma_cm_id *id);
211178784Skmacy
212178784Skmacy/**
213178784Skmacy * rdma_init_qp_attr - Initializes the QP attributes for use in transitioning
214178784Skmacy *   to a specified QP state.
215178784Skmacy * @id: Communication identifier associated with the QP attributes to
216178784Skmacy *   initialize.
217178784Skmacy * @qp_attr: On input, specifies the desired QP state.  On output, the
218178784Skmacy *   mandatory and desired optional attributes will be set in order to
219178784Skmacy *   modify the QP to the specified state.
220178784Skmacy * @qp_attr_mask: The QP attribute mask that may be used to transition the
221178784Skmacy *   QP to the specified state.
222178784Skmacy *
223178784Skmacy * Users must set the @qp_attr->qp_state to the desired QP state.  This call
224178784Skmacy * will set all required attributes for the given transition, along with
225178784Skmacy * known optional attributes.  Users may override the attributes returned from
226178784Skmacy * this call before calling ib_modify_qp.
227178784Skmacy *
228178784Skmacy * Users that wish to have their QP automatically transitioned through its
229178784Skmacy * states can associate a QP with the rdma_cm_id by calling rdma_create_qp().
230178784Skmacy */
231178784Skmacyint rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
232178784Skmacy		       int *qp_attr_mask);
233178784Skmacy
234178784Skmacy/**
235178784Skmacy * rdma_connect - Initiate an active connection request.
236178784Skmacy * @id: Connection identifier to connect.
237178784Skmacy * @conn_param: Connection information used for connected QPs.
238178784Skmacy *
239178784Skmacy * Users must have resolved a route for the rdma_cm_id to connect with
240178784Skmacy * by having called rdma_resolve_route before calling this routine.
241178784Skmacy *
242178784Skmacy * This call will either connect to a remote QP or obtain remote QP
243178784Skmacy * information for unconnected rdma_cm_id's.  The actual operation is
244178784Skmacy * based on the rdma_cm_id's port space.
245178784Skmacy */
246178784Skmacyint rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
247178784Skmacy
248178784Skmacy/**
249178784Skmacy * rdma_listen - This function is called by the passive side to
250178784Skmacy *   listen for incoming connection requests.
251178784Skmacy *
252178784Skmacy * Users must have bound the rdma_cm_id to a local address by calling
253178784Skmacy * rdma_bind_addr before calling this routine.
254178784Skmacy */
255178784Skmacyint rdma_listen(struct rdma_cm_id *id, int backlog);
256178784Skmacy
257178784Skmacy/**
258178784Skmacy * rdma_accept - Called to accept a connection request or response.
259178784Skmacy * @id: Connection identifier associated with the request.
260178784Skmacy * @conn_param: Information needed to establish the connection.  This must be
261178784Skmacy *   provided if accepting a connection request.  If accepting a connection
262178784Skmacy *   response, this parameter must be NULL.
263178784Skmacy *
264178784Skmacy * Typically, this routine is only called by the listener to accept a connection
265178784Skmacy * request.  It must also be called on the active side of a connection if the
266178784Skmacy * user is performing their own QP transitions.
267178784Skmacy *
268178784Skmacy * In the case of error, a reject message is sent to the remote side and the
269178784Skmacy * state of the qp associated with the id is modified to error, such that any
270178784Skmacy * previously posted receive buffers would be flushed.
271178784Skmacy */
272178784Skmacyint rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
273178784Skmacy
274178784Skmacy/**
275178784Skmacy * rdma_notify - Notifies the RDMA CM of an asynchronous event that has
276178784Skmacy * occurred on the connection.
277178784Skmacy * @id: Connection identifier to transition to established.
278178784Skmacy * @event: Asynchronous event.
279178784Skmacy *
280178784Skmacy * This routine should be invoked by users to notify the CM of relevant
281178784Skmacy * communication events.  Events that should be reported to the CM and
282178784Skmacy * when to report them are:
283178784Skmacy *
284178784Skmacy * IB_EVENT_COMM_EST - Used when a message is received on a connected
285178784Skmacy *    QP before an RTU has been received.
286178784Skmacy */
287178784Skmacyint rdma_notify(struct rdma_cm_id *id, enum ib_event_type event);
288178784Skmacy
289178784Skmacy/**
290178784Skmacy * rdma_reject - Called to reject a connection request or response.
291178784Skmacy */
292178784Skmacyint rdma_reject(struct rdma_cm_id *id, const void *private_data,
293178784Skmacy		u8 private_data_len);
294178784Skmacy
295178784Skmacy/**
296178784Skmacy * rdma_disconnect - This function disconnects the associated QP and
297178784Skmacy *   transitions it into the error state.
298178784Skmacy */
299178784Skmacyint rdma_disconnect(struct rdma_cm_id *id);
300178784Skmacy
301178784Skmacy/**
302178784Skmacy * rdma_join_multicast - Join the multicast group specified by the given
303178784Skmacy *   address.
304178784Skmacy * @id: Communication identifier associated with the request.
305178784Skmacy * @addr: Multicast address identifying the group to join.
306178784Skmacy * @context: User-defined context associated with the join request, returned
307178784Skmacy * to the user through the private_data pointer in multicast events.
308178784Skmacy */
309178784Skmacyint rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
310178784Skmacy			void *context);
311178784Skmacy
312178784Skmacy/**
313178784Skmacy * rdma_leave_multicast - Leave the multicast group specified by the given
314178784Skmacy *   address.
315178784Skmacy */
316178784Skmacyvoid rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);
317178784Skmacy
318178784Skmacy#endif /* RDMA_CM_H */
319