1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005 Network Appliance, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
4309378Sjhb * Copyright (c) 2016 Chelsio Communications.  All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff */
34219820Sjeff#ifndef IW_CM_H
35219820Sjeff#define IW_CM_H
36219820Sjeff
37219820Sjeff#include <linux/in.h>
38219820Sjeff#include <rdma/ib_cm.h>
39219820Sjeff
40219820Sjeffstruct iw_cm_id;
41219820Sjeff
42219820Sjeffenum iw_cm_event_type {
43219820Sjeff	IW_CM_EVENT_CONNECT_REQUEST = 1, /* connect request received */
44219820Sjeff	IW_CM_EVENT_CONNECT_REPLY,	 /* reply from active connect request */
45219820Sjeff	IW_CM_EVENT_ESTABLISHED,	 /* passive side accept successful */
46219820Sjeff	IW_CM_EVENT_DISCONNECT,		 /* orderly shutdown */
47219820Sjeff	IW_CM_EVENT_CLOSE		 /* close complete */
48219820Sjeff};
49219820Sjeff
50219820Sjeffenum iw_cm_event_status {
51219820Sjeff	IW_CM_EVENT_STATUS_OK = 0,	 /* request successful */
52219820Sjeff	IW_CM_EVENT_STATUS_ACCEPTED = 0, /* connect request accepted */
53219820Sjeff	IW_CM_EVENT_STATUS_REJECTED,	 /* connect request rejected */
54219820Sjeff	IW_CM_EVENT_STATUS_TIMEOUT,	 /* the operation timed out */
55219820Sjeff	IW_CM_EVENT_STATUS_RESET,	 /* reset from remote peer */
56219820Sjeff	IW_CM_EVENT_STATUS_EINVAL,	 /* asynchronous failure for bad parm */
57219820Sjeff};
58219820Sjeff
59219820Sjeffstruct iw_cm_event {
60219820Sjeff	enum iw_cm_event_type event;
61219820Sjeff	enum iw_cm_event_status status;
62219820Sjeff	struct sockaddr_in local_addr;
63219820Sjeff	struct sockaddr_in remote_addr;
64219820Sjeff	void *private_data;
65219820Sjeff	u8 private_data_len;
66219820Sjeff	void *provider_data;
67237263Snp	struct socket *so;
68309378Sjhb	u8 ord;
69309378Sjhb	u8 ird;
70219820Sjeff};
71219820Sjeff
72219820Sjeff/**
73219820Sjeff * iw_cm_handler - Function to be called by the IW CM when delivering events
74219820Sjeff * to the client.
75219820Sjeff *
76219820Sjeff * @cm_id: The IW CM identifier associated with the event.
77219820Sjeff * @event: Pointer to the event structure.
78219820Sjeff */
79219820Sjefftypedef int (*iw_cm_handler)(struct iw_cm_id *cm_id,
80219820Sjeff			     struct iw_cm_event *event);
81219820Sjeff
82219820Sjeff/**
83219820Sjeff * iw_event_handler - Function called by the provider when delivering provider
84219820Sjeff * events to the IW CM.  Returns either 0 indicating the event was processed
85219820Sjeff * or -errno if the event could not be processed.
86219820Sjeff *
87219820Sjeff * @cm_id: The IW CM identifier associated with the event.
88219820Sjeff * @event: Pointer to the event structure.
89219820Sjeff */
90219820Sjefftypedef int (*iw_event_handler)(struct iw_cm_id *cm_id,
91219820Sjeff				 struct iw_cm_event *event);
92219820Sjeff
93219820Sjeffstruct iw_cm_id {
94219820Sjeff	iw_cm_handler		cm_handler;      /* client callback function */
95219820Sjeff	void		        *context;	 /* client cb context */
96219820Sjeff	struct ib_device	*device;
97219820Sjeff	struct sockaddr_in      local_addr;
98219820Sjeff	struct sockaddr_in	remote_addr;
99219820Sjeff	void			*provider_data;	 /* provider private data */
100219820Sjeff	iw_event_handler        event_handler;   /* cb for provider
101219820Sjeff						    events */
102219820Sjeff	/* Used by provider to add and remove refs on IW cm_id */
103219820Sjeff	void (*add_ref)(struct iw_cm_id *);
104219820Sjeff	void (*rem_ref)(struct iw_cm_id *);
105237263Snp	struct socket           *so;
106219820Sjeff};
107219820Sjeff
108219820Sjeffstruct iw_cm_conn_param {
109219820Sjeff	const void *private_data;
110219820Sjeff	u16 private_data_len;
111219820Sjeff	u32 ord;
112219820Sjeff	u32 ird;
113219820Sjeff	u32 qpn;
114219820Sjeff};
115219820Sjeff
116219820Sjeffstruct iw_cm_verbs {
117219820Sjeff	void		(*add_ref)(struct ib_qp *qp);
118219820Sjeff
119219820Sjeff	void		(*rem_ref)(struct ib_qp *qp);
120219820Sjeff
121219820Sjeff	struct ib_qp *	(*get_qp)(struct ib_device *device,
122219820Sjeff				  int qpn);
123219820Sjeff
124219820Sjeff	int		(*connect)(struct iw_cm_id *cm_id,
125219820Sjeff				   struct iw_cm_conn_param *conn_param);
126219820Sjeff
127219820Sjeff	int		(*accept)(struct iw_cm_id *cm_id,
128219820Sjeff				  struct iw_cm_conn_param *conn_param);
129219820Sjeff
130219820Sjeff	int		(*reject)(struct iw_cm_id *cm_id,
131219820Sjeff				  const void *pdata, u8 pdata_len);
132219820Sjeff
133309378Sjhb	int		(*create_listen_ep)(struct iw_cm_id *cm_id,
134219820Sjeff					 int backlog);
135219820Sjeff
136309378Sjhb	void		(*destroy_listen_ep)(struct iw_cm_id *cm_id);
137309378Sjhb
138309378Sjhb	void		(*newconn)(struct iw_cm_id *parent_cm_id,
139309378Sjhb						struct socket *so);
140219820Sjeff};
141219820Sjeff
142219820Sjeff/**
143219820Sjeff * iw_create_cm_id - Create an IW CM identifier.
144219820Sjeff *
145219820Sjeff * @device: The IB device on which to create the IW CM identier.
146219820Sjeff * @event_handler: User callback invoked to report events associated with the
147219820Sjeff *   returned IW CM identifier.
148219820Sjeff * @context: User specified context associated with the id.
149219820Sjeff */
150237263Snpstruct iw_cm_id *iw_create_cm_id(struct ib_device *device, struct socket *so,
151219820Sjeff				 iw_cm_handler cm_handler, void *context);
152219820Sjeff
153219820Sjeff/**
154219820Sjeff * iw_destroy_cm_id - Destroy an IW CM identifier.
155219820Sjeff *
156219820Sjeff * @cm_id: The previously created IW CM identifier to destroy.
157219820Sjeff *
158219820Sjeff * The client can assume that no events will be delivered for the CM ID after
159219820Sjeff * this function returns.
160219820Sjeff */
161219820Sjeffvoid iw_destroy_cm_id(struct iw_cm_id *cm_id);
162219820Sjeff
163219820Sjeff/**
164219820Sjeff * iw_cm_bind_qp - Unbind the specified IW CM identifier and QP
165219820Sjeff *
166219820Sjeff * @cm_id: The IW CM idenfier to unbind from the QP.
167219820Sjeff * @qp: The QP
168219820Sjeff *
169219820Sjeff * This is called by the provider when destroying the QP to ensure
170219820Sjeff * that any references held by the IWCM are released. It may also
171219820Sjeff * be called by the IWCM when destroying a CM_ID to that any
172219820Sjeff * references held by the provider are released.
173219820Sjeff */
174219820Sjeffvoid iw_cm_unbind_qp(struct iw_cm_id *cm_id, struct ib_qp *qp);
175219820Sjeff
176219820Sjeff/**
177219820Sjeff * iw_cm_get_qp - Return the ib_qp associated with a QPN
178219820Sjeff *
179219820Sjeff * @ib_device: The IB device
180219820Sjeff * @qpn: The queue pair number
181219820Sjeff */
182219820Sjeffstruct ib_qp *iw_cm_get_qp(struct ib_device *device, int qpn);
183219820Sjeff
184219820Sjeff/**
185219820Sjeff * iw_cm_listen - Listen for incoming connection requests on the
186219820Sjeff * specified IW CM id.
187219820Sjeff *
188219820Sjeff * @cm_id: The IW CM identifier.
189219820Sjeff * @backlog: The maximum number of outstanding un-accepted inbound listen
190219820Sjeff *   requests to queue.
191219820Sjeff *
192219820Sjeff * The source address and port number are specified in the IW CM identifier
193219820Sjeff * structure.
194219820Sjeff */
195219820Sjeffint iw_cm_listen(struct iw_cm_id *cm_id, int backlog);
196219820Sjeff
197219820Sjeff/**
198219820Sjeff * iw_cm_accept - Called to accept an incoming connect request.
199219820Sjeff *
200219820Sjeff * @cm_id: The IW CM identifier associated with the connection request.
201219820Sjeff * @iw_param: Pointer to a structure containing connection establishment
202219820Sjeff *   parameters.
203219820Sjeff *
204219820Sjeff * The specified cm_id will have been provided in the event data for a
205219820Sjeff * CONNECT_REQUEST event. Subsequent events related to this connection will be
206219820Sjeff * delivered to the specified IW CM identifier prior and may occur prior to
207219820Sjeff * the return of this function. If this function returns a non-zero value, the
208219820Sjeff * client can assume that no events will be delivered to the specified IW CM
209219820Sjeff * identifier.
210219820Sjeff */
211219820Sjeffint iw_cm_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param);
212219820Sjeff
213219820Sjeff/**
214219820Sjeff * iw_cm_reject - Reject an incoming connection request.
215219820Sjeff *
216219820Sjeff * @cm_id: Connection identifier associated with the request.
217219820Sjeff * @private_daa: Pointer to data to deliver to the remote peer as part of the
218219820Sjeff *   reject message.
219219820Sjeff * @private_data_len: The number of bytes in the private_data parameter.
220219820Sjeff *
221219820Sjeff * The client can assume that no events will be delivered to the specified IW
222219820Sjeff * CM identifier following the return of this function. The private_data
223219820Sjeff * buffer is available for reuse when this function returns.
224219820Sjeff */
225219820Sjeffint iw_cm_reject(struct iw_cm_id *cm_id, const void *private_data,
226219820Sjeff		 u8 private_data_len);
227219820Sjeff
228219820Sjeff/**
229219820Sjeff * iw_cm_connect - Called to request a connection to a remote peer.
230219820Sjeff *
231219820Sjeff * @cm_id: The IW CM identifier for the connection.
232219820Sjeff * @iw_param: Pointer to a structure containing connection  establishment
233219820Sjeff *   parameters.
234219820Sjeff *
235219820Sjeff * Events may be delivered to the specified IW CM identifier prior to the
236219820Sjeff * return of this function. If this function returns a non-zero value, the
237219820Sjeff * client can assume that no events will be delivered to the specified IW CM
238219820Sjeff * identifier.
239219820Sjeff */
240219820Sjeffint iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param);
241219820Sjeff
242219820Sjeff/**
243219820Sjeff * iw_cm_disconnect - Close the specified connection.
244219820Sjeff *
245219820Sjeff * @cm_id: The IW CM identifier to close.
246219820Sjeff * @abrupt: If 0, the connection will be closed gracefully, otherwise, the
247219820Sjeff *   connection will be reset.
248219820Sjeff *
249219820Sjeff * The IW CM identifier is still active until the IW_CM_EVENT_CLOSE event is
250219820Sjeff * delivered.
251219820Sjeff */
252219820Sjeffint iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt);
253219820Sjeff
254219820Sjeff/**
255219820Sjeff * iw_cm_init_qp_attr - Called to initialize the attributes of the QP
256219820Sjeff * associated with a IW CM identifier.
257219820Sjeff *
258219820Sjeff * @cm_id: The IW CM identifier associated with the QP
259219820Sjeff * @qp_attr: Pointer to the QP attributes structure.
260219820Sjeff * @qp_attr_mask: Pointer to a bit vector specifying which QP attributes are
261219820Sjeff *   valid.
262219820Sjeff */
263219820Sjeffint iw_cm_init_qp_attr(struct iw_cm_id *cm_id, struct ib_qp_attr *qp_attr,
264219820Sjeff		       int *qp_attr_mask);
265219820Sjeff
266219820Sjeff#endif /* IW_CM_H */
267