1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3219820Sjeff * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4219820Sjeff *
5219820Sjeff * This software is available to you under a choice of one of two
6219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
7219820Sjeff * General Public License (GPL) Version 2, available from the file
8219820Sjeff * COPYING in the main directory of this source tree, or the
9219820Sjeff * OpenIB.org BSD license below:
10219820Sjeff *
11219820Sjeff *     Redistribution and use in source and binary forms, with or
12219820Sjeff *     without modification, are permitted provided that the following
13219820Sjeff *     conditions are met:
14219820Sjeff *
15219820Sjeff *      - Redistributions of source code must retain the above
16219820Sjeff *        copyright notice, this list of conditions and the following
17219820Sjeff *        disclaimer.
18219820Sjeff *
19219820Sjeff *      - Redistributions in binary form must reproduce the above
20219820Sjeff *        copyright notice, this list of conditions and the following
21219820Sjeff *        disclaimer in the documentation and/or other materials
22219820Sjeff *        provided with the distribution.
23219820Sjeff *
24219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31219820Sjeff * SOFTWARE.
32331772Shselasky *
33331772Shselasky * $FreeBSD: stable/11/sys/ofed/include/rdma/rdma_cm.h 331772 2018-03-30 18:17:33Z hselasky $
34219820Sjeff */
35219820Sjeff
36219820Sjeff#if !defined(RDMA_CM_H)
37219820Sjeff#define RDMA_CM_H
38219820Sjeff
39219820Sjeff#include <linux/socket.h>
40219820Sjeff#include <linux/in6.h>
41219820Sjeff#include <rdma/ib_addr.h>
42219820Sjeff#include <rdma/ib_sa.h>
43219820Sjeff
44219820Sjeff/*
45219820Sjeff * Upon receiving a device removal event, users must destroy the associated
46219820Sjeff * RDMA identifier and release all resources allocated with the device.
47219820Sjeff */
48219820Sjeffenum rdma_cm_event_type {
49219820Sjeff	RDMA_CM_EVENT_ADDR_RESOLVED,
50219820Sjeff	RDMA_CM_EVENT_ADDR_ERROR,
51219820Sjeff	RDMA_CM_EVENT_ROUTE_RESOLVED,
52219820Sjeff	RDMA_CM_EVENT_ROUTE_ERROR,
53219820Sjeff	RDMA_CM_EVENT_CONNECT_REQUEST,
54219820Sjeff	RDMA_CM_EVENT_CONNECT_RESPONSE,
55219820Sjeff	RDMA_CM_EVENT_CONNECT_ERROR,
56219820Sjeff	RDMA_CM_EVENT_UNREACHABLE,
57219820Sjeff	RDMA_CM_EVENT_REJECTED,
58219820Sjeff	RDMA_CM_EVENT_ESTABLISHED,
59219820Sjeff	RDMA_CM_EVENT_DISCONNECTED,
60219820Sjeff	RDMA_CM_EVENT_DEVICE_REMOVAL,
61219820Sjeff	RDMA_CM_EVENT_MULTICAST_JOIN,
62219820Sjeff	RDMA_CM_EVENT_MULTICAST_ERROR,
63219820Sjeff	RDMA_CM_EVENT_ADDR_CHANGE,
64331769Shselasky	RDMA_CM_EVENT_TIMEWAIT_EXIT
65219820Sjeff};
66219820Sjeff
67331769Shselaskyconst char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event);
68331769Shselasky
69219820Sjeffenum rdma_port_space {
70219820Sjeff	RDMA_PS_SDP   = 0x0001,
71219820Sjeff	RDMA_PS_IPOIB = 0x0002,
72278886Shselasky	RDMA_PS_IB    = 0x013F,
73219820Sjeff	RDMA_PS_TCP   = 0x0106,
74219820Sjeff	RDMA_PS_UDP   = 0x0111,
75219820Sjeff};
76219820Sjeff
77331769Shselasky#define RDMA_IB_IP_PS_MASK   0xFFFFFFFFFFFF0000ULL
78331769Shselasky#define RDMA_IB_IP_PS_TCP    0x0000000001060000ULL
79331769Shselasky#define RDMA_IB_IP_PS_UDP    0x0000000001110000ULL
80331769Shselasky#define RDMA_IB_IP_PS_IB     0x00000000013F0000ULL
81278886Shselasky
82219820Sjeffstruct rdma_addr {
83219820Sjeff	struct sockaddr_storage src_addr;
84219820Sjeff	struct sockaddr_storage dst_addr;
85219820Sjeff	struct rdma_dev_addr dev_addr;
86219820Sjeff};
87219820Sjeff
88219820Sjeffstruct rdma_route {
89219820Sjeff	struct rdma_addr addr;
90219820Sjeff	struct ib_sa_path_rec *path_rec;
91219820Sjeff	int num_paths;
92219820Sjeff};
93219820Sjeff
94219820Sjeffstruct rdma_conn_param {
95219820Sjeff	const void *private_data;
96219820Sjeff	u8 private_data_len;
97219820Sjeff	u8 responder_resources;
98219820Sjeff	u8 initiator_depth;
99219820Sjeff	u8 flow_control;
100219820Sjeff	u8 retry_count;		/* ignored when accepting */
101219820Sjeff	u8 rnr_retry_count;
102219820Sjeff	/* Fields below ignored if a QP is created on the rdma_cm_id. */
103219820Sjeff	u8 srq;
104219820Sjeff	u32 qp_num;
105331769Shselasky	u32 qkey;
106219820Sjeff};
107219820Sjeff
108219820Sjeffstruct rdma_ud_param {
109219820Sjeff	const void *private_data;
110219820Sjeff	u8 private_data_len;
111219820Sjeff	struct ib_ah_attr ah_attr;
112219820Sjeff	u32 qp_num;
113219820Sjeff	u32 qkey;
114219820Sjeff};
115219820Sjeff
116219820Sjeffstruct rdma_cm_event {
117219820Sjeff	enum rdma_cm_event_type	 event;
118219820Sjeff	int			 status;
119219820Sjeff	union {
120219820Sjeff		struct rdma_conn_param	conn;
121219820Sjeff		struct rdma_ud_param	ud;
122219820Sjeff	} param;
123219820Sjeff};
124219820Sjeff
125278886Shselaskyenum rdma_cm_state {
126278886Shselasky	RDMA_CM_IDLE,
127278886Shselasky	RDMA_CM_ADDR_QUERY,
128278886Shselasky	RDMA_CM_ADDR_RESOLVED,
129278886Shselasky	RDMA_CM_ROUTE_QUERY,
130278886Shselasky	RDMA_CM_ROUTE_RESOLVED,
131278886Shselasky	RDMA_CM_CONNECT,
132278886Shselasky	RDMA_CM_DISCONNECT,
133278886Shselasky	RDMA_CM_ADDR_BOUND,
134278886Shselasky	RDMA_CM_LISTEN,
135278886Shselasky	RDMA_CM_DEVICE_REMOVAL,
136278886Shselasky	RDMA_CM_DESTROYING
137278886Shselasky};
138278886Shselasky
139219820Sjeffstruct rdma_cm_id;
140219820Sjeff
141219820Sjeff/**
142219820Sjeff * rdma_cm_event_handler - Callback used to report user events.
143219820Sjeff *
144219820Sjeff * Notes: Users may not call rdma_destroy_id from this callback to destroy
145219820Sjeff *   the passed in id, or a corresponding listen id.  Returning a
146219820Sjeff *   non-zero value from the callback will destroy the passed in id.
147219820Sjeff */
148219820Sjefftypedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id,
149219820Sjeff				     struct rdma_cm_event *event);
150219820Sjeff
151219820Sjeffstruct rdma_cm_id {
152219820Sjeff	struct ib_device	*device;
153219820Sjeff	void			*context;
154219820Sjeff	struct ib_qp		*qp;
155219820Sjeff	rdma_cm_event_handler	 event_handler;
156219820Sjeff	struct rdma_route	 route;
157219820Sjeff	enum rdma_port_space	 ps;
158278886Shselasky	enum ib_qp_type		 qp_type;
159219820Sjeff	u8			 port_num;
160219820Sjeff};
161219820Sjeff
162219820Sjeff/**
163219820Sjeff * rdma_create_id - Create an RDMA identifier.
164219820Sjeff *
165331769Shselasky * @net: The network namespace in which to create the new id.
166219820Sjeff * @event_handler: User callback invoked to report events associated with the
167219820Sjeff *   returned rdma_id.
168219820Sjeff * @context: User specified context associated with the id.
169219820Sjeff * @ps: RDMA port space.
170278886Shselasky * @qp_type: type of queue pair associated with the id.
171331769Shselasky *
172331769Shselasky * The id holds a reference on the network namespace until it is destroyed.
173219820Sjeff */
174331769Shselaskystruct rdma_cm_id *rdma_create_id(struct vnet *net,
175331769Shselasky				  rdma_cm_event_handler event_handler,
176278886Shselasky				  void *context, enum rdma_port_space ps,
177278886Shselasky				  enum ib_qp_type qp_type);
178219820Sjeff
179219820Sjeff/**
180219820Sjeff  * rdma_destroy_id - Destroys an RDMA identifier.
181219820Sjeff  *
182219820Sjeff  * @id: RDMA identifier.
183219820Sjeff  *
184219820Sjeff  * Note: calling this function has the effect of canceling in-flight
185219820Sjeff  * asynchronous operations associated with the id.
186219820Sjeff  */
187219820Sjeffvoid rdma_destroy_id(struct rdma_cm_id *id);
188219820Sjeff
189219820Sjeff/**
190219820Sjeff * rdma_bind_addr - Bind an RDMA identifier to a source address and
191219820Sjeff *   associated RDMA device, if needed.
192219820Sjeff *
193219820Sjeff * @id: RDMA identifier.
194219820Sjeff * @addr: Local address information.  Wildcard values are permitted.
195219820Sjeff *
196219820Sjeff * This associates a source address with the RDMA identifier before calling
197219820Sjeff * rdma_listen.  If a specific local address is given, the RDMA identifier will
198219820Sjeff * be bound to a local RDMA device.
199219820Sjeff */
200219820Sjeffint rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr);
201219820Sjeff
202219820Sjeff/**
203219820Sjeff * rdma_resolve_addr - Resolve destination and optional source addresses
204219820Sjeff *   from IP addresses to an RDMA address.  If successful, the specified
205219820Sjeff *   rdma_cm_id will be bound to a local device.
206219820Sjeff *
207219820Sjeff * @id: RDMA identifier.
208219820Sjeff * @src_addr: Source address information.  This parameter may be NULL.
209219820Sjeff * @dst_addr: Destination address information.
210219820Sjeff * @timeout_ms: Time to wait for resolution to complete.
211219820Sjeff */
212219820Sjeffint rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
213219820Sjeff		      struct sockaddr *dst_addr, int timeout_ms);
214219820Sjeff
215219820Sjeff/**
216219820Sjeff * rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier
217219820Sjeff *   into route information needed to establish a connection.
218219820Sjeff *
219219820Sjeff * This is called on the client side of a connection.
220219820Sjeff * Users must have first called rdma_resolve_addr to resolve a dst_addr
221219820Sjeff * into an RDMA address before calling this routine.
222219820Sjeff */
223219820Sjeffint rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms);
224219820Sjeff
225219820Sjeff/**
226219820Sjeff * rdma_create_qp - Allocate a QP and associate it with the specified RDMA
227219820Sjeff * identifier.
228219820Sjeff *
229219820Sjeff * QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA
230219820Sjeff * through their states.
231219820Sjeff */
232219820Sjeffint rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
233219820Sjeff		   struct ib_qp_init_attr *qp_init_attr);
234219820Sjeff
235219820Sjeff/**
236219820Sjeff * rdma_destroy_qp - Deallocate the QP associated with the specified RDMA
237219820Sjeff * identifier.
238219820Sjeff *
239219820Sjeff * Users must destroy any QP associated with an RDMA identifier before
240219820Sjeff * destroying the RDMA ID.
241219820Sjeff */
242219820Sjeffvoid rdma_destroy_qp(struct rdma_cm_id *id);
243219820Sjeff
244219820Sjeff/**
245219820Sjeff * rdma_init_qp_attr - Initializes the QP attributes for use in transitioning
246219820Sjeff *   to a specified QP state.
247219820Sjeff * @id: Communication identifier associated with the QP attributes to
248219820Sjeff *   initialize.
249219820Sjeff * @qp_attr: On input, specifies the desired QP state.  On output, the
250219820Sjeff *   mandatory and desired optional attributes will be set in order to
251219820Sjeff *   modify the QP to the specified state.
252219820Sjeff * @qp_attr_mask: The QP attribute mask that may be used to transition the
253219820Sjeff *   QP to the specified state.
254219820Sjeff *
255219820Sjeff * Users must set the @qp_attr->qp_state to the desired QP state.  This call
256219820Sjeff * will set all required attributes for the given transition, along with
257219820Sjeff * known optional attributes.  Users may override the attributes returned from
258219820Sjeff * this call before calling ib_modify_qp.
259219820Sjeff *
260219820Sjeff * Users that wish to have their QP automatically transitioned through its
261219820Sjeff * states can associate a QP with the rdma_cm_id by calling rdma_create_qp().
262219820Sjeff */
263219820Sjeffint rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
264219820Sjeff		       int *qp_attr_mask);
265219820Sjeff
266219820Sjeff/**
267219820Sjeff * rdma_connect - Initiate an active connection request.
268219820Sjeff * @id: Connection identifier to connect.
269219820Sjeff * @conn_param: Connection information used for connected QPs.
270219820Sjeff *
271219820Sjeff * Users must have resolved a route for the rdma_cm_id to connect with
272219820Sjeff * by having called rdma_resolve_route before calling this routine.
273219820Sjeff *
274219820Sjeff * This call will either connect to a remote QP or obtain remote QP
275219820Sjeff * information for unconnected rdma_cm_id's.  The actual operation is
276219820Sjeff * based on the rdma_cm_id's port space.
277219820Sjeff */
278219820Sjeffint rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
279219820Sjeff
280219820Sjeff/**
281219820Sjeff * rdma_listen - This function is called by the passive side to
282219820Sjeff *   listen for incoming connection requests.
283219820Sjeff *
284219820Sjeff * Users must have bound the rdma_cm_id to a local address by calling
285219820Sjeff * rdma_bind_addr before calling this routine.
286219820Sjeff */
287219820Sjeffint rdma_listen(struct rdma_cm_id *id, int backlog);
288219820Sjeff
289219820Sjeff/**
290219820Sjeff * rdma_accept - Called to accept a connection request or response.
291219820Sjeff * @id: Connection identifier associated with the request.
292219820Sjeff * @conn_param: Information needed to establish the connection.  This must be
293219820Sjeff *   provided if accepting a connection request.  If accepting a connection
294219820Sjeff *   response, this parameter must be NULL.
295219820Sjeff *
296219820Sjeff * Typically, this routine is only called by the listener to accept a connection
297219820Sjeff * request.  It must also be called on the active side of a connection if the
298219820Sjeff * user is performing their own QP transitions.
299219820Sjeff *
300219820Sjeff * In the case of error, a reject message is sent to the remote side and the
301219820Sjeff * state of the qp associated with the id is modified to error, such that any
302219820Sjeff * previously posted receive buffers would be flushed.
303219820Sjeff */
304219820Sjeffint rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
305219820Sjeff
306219820Sjeff/**
307219820Sjeff * rdma_notify - Notifies the RDMA CM of an asynchronous event that has
308219820Sjeff * occurred on the connection.
309219820Sjeff * @id: Connection identifier to transition to established.
310219820Sjeff * @event: Asynchronous event.
311219820Sjeff *
312219820Sjeff * This routine should be invoked by users to notify the CM of relevant
313219820Sjeff * communication events.  Events that should be reported to the CM and
314219820Sjeff * when to report them are:
315219820Sjeff *
316219820Sjeff * IB_EVENT_COMM_EST - Used when a message is received on a connected
317219820Sjeff *    QP before an RTU has been received.
318219820Sjeff */
319219820Sjeffint rdma_notify(struct rdma_cm_id *id, enum ib_event_type event);
320219820Sjeff
321219820Sjeff/**
322219820Sjeff * rdma_reject - Called to reject a connection request or response.
323219820Sjeff */
324219820Sjeffint rdma_reject(struct rdma_cm_id *id, const void *private_data,
325219820Sjeff		u8 private_data_len);
326219820Sjeff
327219820Sjeff/**
328219820Sjeff * rdma_disconnect - This function disconnects the associated QP and
329219820Sjeff *   transitions it into the error state.
330219820Sjeff */
331219820Sjeffint rdma_disconnect(struct rdma_cm_id *id);
332219820Sjeff
333219820Sjeff/**
334219820Sjeff * rdma_join_multicast - Join the multicast group specified by the given
335219820Sjeff *   address.
336219820Sjeff * @id: Communication identifier associated with the request.
337219820Sjeff * @addr: Multicast address identifying the group to join.
338331769Shselasky * @join_state: Multicast JoinState bitmap requested by port.
339331769Shselasky *		Bitmap is based on IB_SA_MCMEMBER_REC_JOIN_STATE bits.
340219820Sjeff * @context: User-defined context associated with the join request, returned
341219820Sjeff * to the user through the private_data pointer in multicast events.
342219820Sjeff */
343219820Sjeffint rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
344331769Shselasky			u8 join_state, void *context);
345219820Sjeff
346219820Sjeff/**
347219820Sjeff * rdma_leave_multicast - Leave the multicast group specified by the given
348219820Sjeff *   address.
349219820Sjeff */
350219820Sjeffvoid rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);
351219820Sjeff
352219820Sjeff/**
353219820Sjeff * rdma_set_service_type - Set the type of service associated with a
354219820Sjeff *   connection identifier.
355219820Sjeff * @id: Communication identifier to associated with service type.
356219820Sjeff * @tos: Type of service.
357219820Sjeff *
358219820Sjeff * The type of service is interpretted as a differentiated service
359219820Sjeff * field (RFC 2474).  The service type should be specified before
360219820Sjeff * performing route resolution, as existing communication on the
361219820Sjeff * connection identifier may be unaffected.  The type of service
362219820Sjeff * requested may not be supported by the network to all destinations.
363219820Sjeff */
364219820Sjeffvoid rdma_set_service_type(struct rdma_cm_id *id, int tos);
365219820Sjeff
366278886Shselasky/**
367278886Shselasky * rdma_set_reuseaddr - Allow the reuse of local addresses when binding
368278886Shselasky *    the rdma_cm_id.
369278886Shselasky * @id: Communication identifier to configure.
370278886Shselasky * @reuse: Value indicating if the bound address is reusable.
371278886Shselasky *
372278886Shselasky * Reuse must be set before an address is bound to the id.
373278886Shselasky */
374278886Shselaskyint rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse);
375278886Shselasky
376278886Shselasky/**
377278886Shselasky * rdma_set_afonly - Specify that listens are restricted to the
378278886Shselasky *    bound address family only.
379278886Shselasky * @id: Communication identifer to configure.
380278886Shselasky * @afonly: Value indicating if listens are restricted.
381278886Shselasky *
382278886Shselasky * Must be set before identifier is in the listening state.
383278886Shselasky */
384278886Shselaskyint rdma_set_afonly(struct rdma_cm_id *id, int afonly);
385278886Shselasky
386331769Shselasky /**
387331769Shselasky * rdma_get_service_id - Return the IB service ID for a specified address.
388331769Shselasky * @id: Communication identifier associated with the address.
389331769Shselasky * @addr: Address for the service ID.
390278886Shselasky */
391331769Shselasky__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr);
392331769Shselasky
393219820Sjeff#endif /* RDMA_CM_H */
394