xref: /NextBSD/sys/ofed/drivers/infiniband/core/iwcm.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*
2  * Copyright (c) 2004, 2005 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
4  * Copyright (c) 2004, 2005 Voltaire Corporation.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
7  * Copyright (c) 2005 Network Appliance, Inc. All rights reserved.
8  *
9  * This software is available to you under a choice of one of two
10  * licenses.  You may choose to be licensed under the terms of the GNU
11  * General Public License (GPL) Version 2, available from the file
12  * COPYING in the main directory of this source tree, or the
13  * OpenIB.org BSD license below:
14  *
15  *     Redistribution and use in source and binary forms, with or
16  *     without modification, are permitted provided that the following
17  *     conditions are met:
18  *
19  *      - Redistributions of source code must retain the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer.
22  *
23  *      - Redistributions in binary form must reproduce the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer in the documentation and/or other materials
26  *        provided with the distribution.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35  * SOFTWARE.
36  *
37  */
38 #include <linux/dma-mapping.h>
39 #include <linux/err.h>
40 #include <linux/idr.h>
41 #include <linux/interrupt.h>
42 #include <linux/rbtree.h>
43 #include <linux/sched.h>
44 #include <linux/spinlock.h>
45 #include <linux/workqueue.h>
46 #include <linux/completion.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/string.h>
50 
51 #include <rdma/iw_cm.h>
52 #include <rdma/ib_addr.h>
53 
54 #include "iwcm.h"
55 
56 MODULE_AUTHOR("Tom Tucker");
57 MODULE_DESCRIPTION("iWARP CM");
58 MODULE_LICENSE("Dual BSD/GPL");
59 
60 static struct workqueue_struct *iwcm_wq;
61 struct iwcm_work {
62 	struct work_struct work;
63 	struct iwcm_id_private *cm_id;
64 	struct list_head list;
65 	struct iw_cm_event event;
66 	struct list_head free_list;
67 };
68 
69 /*
70  * The following services provide a mechanism for pre-allocating iwcm_work
71  * elements.  The design pre-allocates them  based on the cm_id type:
72  *	LISTENING IDS: 	Get enough elements preallocated to handle the
73  *			listen backlog.
74  *	ACTIVE IDS:	4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
75  *	PASSIVE IDS:	3: ESTABLISHED, DISCONNECT, CLOSE
76  *
77  * Allocating them in connect and listen avoids having to deal
78  * with allocation failures on the event upcall from the provider (which
79  * is called in the interrupt context).
80  *
81  * One exception is when creating the cm_id for incoming connection requests.
82  * There are two cases:
83  * 1) in the event upcall, cm_event_handler(), for a listening cm_id.  If
84  *    the backlog is exceeded, then no more connection request events will
85  *    be processed.  cm_event_handler() returns -ENOMEM in this case.  Its up
86  *    to the provider to reject the connection request.
87  * 2) in the connection request workqueue handler, cm_conn_req_handler().
88  *    If work elements cannot be allocated for the new connect request cm_id,
89  *    then IWCM will call the provider reject method.  This is ok since
90  *    cm_conn_req_handler() runs in the workqueue thread context.
91  */
92 
get_work(struct iwcm_id_private * cm_id_priv)93 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
94 {
95 	struct iwcm_work *work;
96 
97 	if (list_empty(&cm_id_priv->work_free_list))
98 		return NULL;
99 	work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
100 			  free_list);
101 	list_del_init(&work->free_list);
102 	return work;
103 }
104 
put_work(struct iwcm_work * work)105 static void put_work(struct iwcm_work *work)
106 {
107 	list_add(&work->free_list, &work->cm_id->work_free_list);
108 }
109 
dealloc_work_entries(struct iwcm_id_private * cm_id_priv)110 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
111 {
112 	struct list_head *e, *tmp;
113 
114 	list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
115 		kfree(list_entry(e, struct iwcm_work, free_list));
116 }
117 
alloc_work_entries(struct iwcm_id_private * cm_id_priv,int count)118 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
119 {
120 	struct iwcm_work *work;
121 
122 	BUG_ON(!list_empty(&cm_id_priv->work_free_list));
123 	while (count--) {
124 		work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
125 		if (!work) {
126 			dealloc_work_entries(cm_id_priv);
127 			return -ENOMEM;
128 		}
129 		work->cm_id = cm_id_priv;
130 		INIT_LIST_HEAD(&work->list);
131 		put_work(work);
132 	}
133 	return 0;
134 }
135 
136 /*
137  * Save private data from incoming connection requests to
138  * iw_cm_event, so the low level driver doesn't have to. Adjust
139  * the event ptr to point to the local copy.
140  */
copy_private_data(struct iw_cm_event * event)141 static int copy_private_data(struct iw_cm_event *event)
142 {
143 	void *p;
144 
145 	p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
146 	if (!p)
147 		return -ENOMEM;
148 	event->private_data = p;
149 	return 0;
150 }
151 
free_cm_id(struct iwcm_id_private * cm_id_priv)152 static void free_cm_id(struct iwcm_id_private *cm_id_priv)
153 {
154 	dealloc_work_entries(cm_id_priv);
155 	kfree(cm_id_priv);
156 }
157 
158 /*
159  * Release a reference on cm_id. If the last reference is being
160  * released, enable the waiting thread (in iw_destroy_cm_id) to
161  * get woken up, and return 1 if a thread is already waiting.
162  */
iwcm_deref_id(struct iwcm_id_private * cm_id_priv)163 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
164 {
165 	BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
166 	if (atomic_dec_and_test(&cm_id_priv->refcount)) {
167 		BUG_ON(!list_empty(&cm_id_priv->work_list));
168 		complete(&cm_id_priv->destroy_comp);
169 		return 1;
170 	}
171 
172 	return 0;
173 }
174 
add_ref(struct iw_cm_id * cm_id)175 static void add_ref(struct iw_cm_id *cm_id)
176 {
177 	struct iwcm_id_private *cm_id_priv;
178 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
179 	atomic_inc(&cm_id_priv->refcount);
180 }
181 
rem_ref(struct iw_cm_id * cm_id)182 static void rem_ref(struct iw_cm_id *cm_id)
183 {
184 	struct iwcm_id_private *cm_id_priv;
185 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
186 	if (iwcm_deref_id(cm_id_priv) &&
187 	    test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
188 		BUG_ON(!list_empty(&cm_id_priv->work_list));
189 		free_cm_id(cm_id_priv);
190 	}
191 }
192 
193 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
194 
iw_create_cm_id(struct ib_device * device,struct socket * so,iw_cm_handler cm_handler,void * context)195 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
196 				 struct socket *so,
197 				 iw_cm_handler cm_handler,
198 				 void *context)
199 {
200 	struct iwcm_id_private *cm_id_priv;
201 
202 	cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
203 	if (!cm_id_priv)
204 		return ERR_PTR(-ENOMEM);
205 
206 	cm_id_priv->state = IW_CM_STATE_IDLE;
207 	cm_id_priv->id.device = device;
208 	cm_id_priv->id.cm_handler = cm_handler;
209 	cm_id_priv->id.context = context;
210 	cm_id_priv->id.event_handler = cm_event_handler;
211 	cm_id_priv->id.add_ref = add_ref;
212 	cm_id_priv->id.rem_ref = rem_ref;
213 	cm_id_priv->id.so = so;
214 	spin_lock_init(&cm_id_priv->lock);
215 	atomic_set(&cm_id_priv->refcount, 1);
216 	init_waitqueue_head(&cm_id_priv->connect_wait);
217 	init_completion(&cm_id_priv->destroy_comp);
218 	INIT_LIST_HEAD(&cm_id_priv->work_list);
219 	INIT_LIST_HEAD(&cm_id_priv->work_free_list);
220 
221 	return &cm_id_priv->id;
222 }
223 EXPORT_SYMBOL(iw_create_cm_id);
224 
225 
iwcm_modify_qp_err(struct ib_qp * qp)226 static int iwcm_modify_qp_err(struct ib_qp *qp)
227 {
228 	struct ib_qp_attr qp_attr;
229 
230 	if (!qp)
231 		return -EINVAL;
232 
233 	qp_attr.qp_state = IB_QPS_ERR;
234 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
235 }
236 
237 /*
238  * This is really the RDMAC CLOSING state. It is most similar to the
239  * IB SQD QP state.
240  */
iwcm_modify_qp_sqd(struct ib_qp * qp)241 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
242 {
243 	struct ib_qp_attr qp_attr;
244 
245 	BUG_ON(qp == NULL);
246 	qp_attr.qp_state = IB_QPS_SQD;
247 	return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
248 }
249 
250 /*
251  * CM_ID <-- CLOSING
252  *
253  * Block if a passive or active connection is currently being processed. Then
254  * process the event as follows:
255  * - If we are ESTABLISHED, move to CLOSING and modify the QP state
256  *   based on the abrupt flag
257  * - If the connection is already in the CLOSING or IDLE state, the peer is
258  *   disconnecting concurrently with us and we've already seen the
259  *   DISCONNECT event -- ignore the request and return 0
260  * - Disconnect on a listening endpoint returns -EINVAL
261  */
iw_cm_disconnect(struct iw_cm_id * cm_id,int abrupt)262 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
263 {
264 	struct iwcm_id_private *cm_id_priv;
265 	unsigned long flags;
266 	int ret = 0;
267 	struct ib_qp *qp = NULL;
268 
269 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
270 	/* Wait if we're currently in a connect or accept downcall */
271 	wait_event(cm_id_priv->connect_wait,
272 		   !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
273 
274 	spin_lock_irqsave(&cm_id_priv->lock, flags);
275 	switch (cm_id_priv->state) {
276 	case IW_CM_STATE_ESTABLISHED:
277 		cm_id_priv->state = IW_CM_STATE_CLOSING;
278 
279 		/* QP could be <nul> for user-mode client */
280 		if (cm_id_priv->qp)
281 			qp = cm_id_priv->qp;
282 		else
283 			ret = -EINVAL;
284 		break;
285 	case IW_CM_STATE_LISTEN:
286 		ret = -EINVAL;
287 		break;
288 	case IW_CM_STATE_CLOSING:
289 		/* remote peer closed first */
290 	case IW_CM_STATE_IDLE:
291 		/* accept or connect returned !0 */
292 		break;
293 	case IW_CM_STATE_CONN_RECV:
294 		/*
295 		 * App called disconnect before/without calling accept after
296 		 * connect_request event delivered.
297 		 */
298 		break;
299 	case IW_CM_STATE_CONN_SENT:
300 		/* Can only get here if wait above fails */
301 	default:
302 		BUG();
303 	}
304 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
305 
306 	if (qp) {
307 		if (abrupt)
308 			ret = iwcm_modify_qp_err(qp);
309 		else
310 			ret = iwcm_modify_qp_sqd(qp);
311 
312 		/*
313 		 * If both sides are disconnecting the QP could
314 		 * already be in ERR or SQD states
315 		 */
316 		ret = 0;
317 	}
318 
319 	return ret;
320 }
321 EXPORT_SYMBOL(iw_cm_disconnect);
322 
323 /*
324  * CM_ID <-- DESTROYING
325  *
326  * Clean up all resources associated with the connection and release
327  * the initial reference taken by iw_create_cm_id.
328  */
destroy_cm_id(struct iw_cm_id * cm_id)329 static void destroy_cm_id(struct iw_cm_id *cm_id)
330 {
331 	struct iwcm_id_private *cm_id_priv;
332 	unsigned long flags;
333 	int ret;
334 
335 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
336 	/*
337 	 * Wait if we're currently in a connect or accept downcall. A
338 	 * listening endpoint should never block here.
339 	 */
340 	wait_event(cm_id_priv->connect_wait,
341 		   !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
342 
343 	spin_lock_irqsave(&cm_id_priv->lock, flags);
344 	switch (cm_id_priv->state) {
345 	case IW_CM_STATE_LISTEN:
346 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
347 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
348 		/* destroy the listening endpoint */
349 		ret = cm_id->device->iwcm->destroy_listen(cm_id);
350 		spin_lock_irqsave(&cm_id_priv->lock, flags);
351 		break;
352 	case IW_CM_STATE_ESTABLISHED:
353 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
354 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
355 		/* Abrupt close of the connection */
356 		(void)iwcm_modify_qp_err(cm_id_priv->qp);
357 		spin_lock_irqsave(&cm_id_priv->lock, flags);
358 		break;
359 	case IW_CM_STATE_IDLE:
360 	case IW_CM_STATE_CLOSING:
361 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
362 		break;
363 	case IW_CM_STATE_CONN_RECV:
364 		/*
365 		 * App called destroy before/without calling accept after
366 		 * receiving connection request event notification or
367 		 * returned non zero from the event callback function.
368 		 * In either case, must tell the provider to reject.
369 		 */
370 		cm_id_priv->state = IW_CM_STATE_DESTROYING;
371 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
372 		cm_id->device->iwcm->reject(cm_id, NULL, 0);
373 		spin_lock_irqsave(&cm_id_priv->lock, flags);
374 		break;
375 	case IW_CM_STATE_CONN_SENT:
376 	case IW_CM_STATE_DESTROYING:
377 	default:
378 		BUG();
379 		break;
380 	}
381 	if (cm_id_priv->qp) {
382 		cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
383 		cm_id_priv->qp = NULL;
384 	}
385 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
386 
387 	(void)iwcm_deref_id(cm_id_priv);
388 }
389 
390 /*
391  * This function is only called by the application thread and cannot
392  * be called by the event thread. The function will wait for all
393  * references to be released on the cm_id and then kfree the cm_id
394  * object.
395  */
iw_destroy_cm_id(struct iw_cm_id * cm_id)396 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
397 {
398 	struct iwcm_id_private *cm_id_priv;
399 
400 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
401 	BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
402 
403 	destroy_cm_id(cm_id);
404 
405 	wait_for_completion(&cm_id_priv->destroy_comp);
406 
407 	free_cm_id(cm_id_priv);
408 }
409 EXPORT_SYMBOL(iw_destroy_cm_id);
410 
411 /*
412  * CM_ID <-- LISTEN
413  *
414  * Start listening for connect requests. Generates one CONNECT_REQUEST
415  * event for each inbound connect request.
416  */
iw_cm_listen(struct iw_cm_id * cm_id,int backlog)417 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
418 {
419 	struct iwcm_id_private *cm_id_priv;
420 	unsigned long flags;
421 	int ret;
422 
423 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
424 
425 	ret = alloc_work_entries(cm_id_priv, backlog);
426 	if (ret)
427 		return ret;
428 
429 	spin_lock_irqsave(&cm_id_priv->lock, flags);
430 	switch (cm_id_priv->state) {
431 	case IW_CM_STATE_IDLE:
432 		cm_id_priv->state = IW_CM_STATE_LISTEN;
433 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
434 		ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
435 		if (ret)
436 			cm_id_priv->state = IW_CM_STATE_IDLE;
437 		spin_lock_irqsave(&cm_id_priv->lock, flags);
438 		break;
439 	default:
440 		ret = -EINVAL;
441 	}
442 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
443 
444 	return ret;
445 }
446 EXPORT_SYMBOL(iw_cm_listen);
447 
448 /*
449  * CM_ID <-- IDLE
450  *
451  * Rejects an inbound connection request. No events are generated.
452  */
iw_cm_reject(struct iw_cm_id * cm_id,const void * private_data,u8 private_data_len)453 int iw_cm_reject(struct iw_cm_id *cm_id,
454 		 const void *private_data,
455 		 u8 private_data_len)
456 {
457 	struct iwcm_id_private *cm_id_priv;
458 	unsigned long flags;
459 	int ret;
460 
461 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
462 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
463 
464 	spin_lock_irqsave(&cm_id_priv->lock, flags);
465 	if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
466 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
467 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
468 		wake_up_all(&cm_id_priv->connect_wait);
469 		return -EINVAL;
470 	}
471 	cm_id_priv->state = IW_CM_STATE_IDLE;
472 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
473 
474 	ret = cm_id->device->iwcm->reject(cm_id, private_data,
475 					  private_data_len);
476 
477 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
478 	wake_up_all(&cm_id_priv->connect_wait);
479 
480 	return ret;
481 }
482 EXPORT_SYMBOL(iw_cm_reject);
483 
484 /*
485  * CM_ID <-- ESTABLISHED
486  *
487  * Accepts an inbound connection request and generates an ESTABLISHED
488  * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
489  * until the ESTABLISHED event is received from the provider.
490  */
iw_cm_accept(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)491 int iw_cm_accept(struct iw_cm_id *cm_id,
492 		 struct iw_cm_conn_param *iw_param)
493 {
494 	struct iwcm_id_private *cm_id_priv;
495 	struct ib_qp *qp;
496 	unsigned long flags;
497 	int ret;
498 
499 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
500 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
501 
502 	spin_lock_irqsave(&cm_id_priv->lock, flags);
503 	if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
504 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
505 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
506 		wake_up_all(&cm_id_priv->connect_wait);
507 		return -EINVAL;
508 	}
509 	/* Get the ib_qp given the QPN */
510 	qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
511 	if (!qp) {
512 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
513 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
514 		wake_up_all(&cm_id_priv->connect_wait);
515 		return -EINVAL;
516 	}
517 	cm_id->device->iwcm->add_ref(qp);
518 	cm_id_priv->qp = qp;
519 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
520 
521 	ret = cm_id->device->iwcm->accept(cm_id, iw_param);
522 	if (ret) {
523 		/* An error on accept precludes provider events */
524 		BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
525 		cm_id_priv->state = IW_CM_STATE_IDLE;
526 		spin_lock_irqsave(&cm_id_priv->lock, flags);
527 		if (cm_id_priv->qp) {
528 			cm_id->device->iwcm->rem_ref(qp);
529 			cm_id_priv->qp = NULL;
530 		}
531 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
532 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
533 		wake_up_all(&cm_id_priv->connect_wait);
534 	}
535 
536 	return ret;
537 }
538 EXPORT_SYMBOL(iw_cm_accept);
539 
540 /*
541  * Active Side: CM_ID <-- CONN_SENT
542  *
543  * If successful, results in the generation of a CONNECT_REPLY
544  * event. iw_cm_disconnect and iw_cm_destroy will block until the
545  * CONNECT_REPLY event is received from the provider.
546  */
iw_cm_connect(struct iw_cm_id * cm_id,struct iw_cm_conn_param * iw_param)547 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
548 {
549 	struct iwcm_id_private *cm_id_priv;
550 	int ret;
551 	unsigned long flags;
552 	struct ib_qp *qp;
553 
554 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
555 
556 	ret = alloc_work_entries(cm_id_priv, 4);
557 	if (ret)
558 		return ret;
559 
560 	set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
561 	spin_lock_irqsave(&cm_id_priv->lock, flags);
562 
563 	if (cm_id_priv->state != IW_CM_STATE_IDLE) {
564 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
565 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
566 		wake_up_all(&cm_id_priv->connect_wait);
567 		return -EINVAL;
568 	}
569 
570 	/* Get the ib_qp given the QPN */
571 	qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
572 	if (!qp) {
573 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
574 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
575 		wake_up_all(&cm_id_priv->connect_wait);
576 		return -EINVAL;
577 	}
578 	cm_id->device->iwcm->add_ref(qp);
579 	cm_id_priv->qp = qp;
580 	cm_id_priv->state = IW_CM_STATE_CONN_SENT;
581 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
582 
583 	ret = cm_id->device->iwcm->connect(cm_id, iw_param);
584 	if (ret) {
585 		spin_lock_irqsave(&cm_id_priv->lock, flags);
586 		if (cm_id_priv->qp) {
587 			cm_id->device->iwcm->rem_ref(qp);
588 			cm_id_priv->qp = NULL;
589 		}
590 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
591 		BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
592 		cm_id_priv->state = IW_CM_STATE_IDLE;
593 		clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
594 		wake_up_all(&cm_id_priv->connect_wait);
595 	}
596 
597 	return ret;
598 }
599 EXPORT_SYMBOL(iw_cm_connect);
600 
601 /*
602  * Passive Side: new CM_ID <-- CONN_RECV
603  *
604  * Handles an inbound connect request. The function creates a new
605  * iw_cm_id to represent the new connection and inherits the client
606  * callback function and other attributes from the listening parent.
607  *
608  * The work item contains a pointer to the listen_cm_id and the event. The
609  * listen_cm_id contains the client cm_handler, context and
610  * device. These are copied when the device is cloned. The event
611  * contains the new four tuple.
612  *
613  * An error on the child should not affect the parent, so this
614  * function does not return a value.
615  */
cm_conn_req_handler(struct iwcm_id_private * listen_id_priv,struct iw_cm_event * iw_event)616 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
617 				struct iw_cm_event *iw_event)
618 {
619 	unsigned long flags;
620 	struct iw_cm_id *cm_id;
621 	struct iwcm_id_private *cm_id_priv;
622 	int ret;
623 
624 	/*
625 	 * The provider should never generate a connection request
626 	 * event with a bad status.
627 	 */
628 	BUG_ON(iw_event->status);
629 
630 	cm_id = iw_create_cm_id(listen_id_priv->id.device,
631 				iw_event->so,
632 				listen_id_priv->id.cm_handler,
633 				listen_id_priv->id.context);
634 	/* If the cm_id could not be created, ignore the request */
635 	if (IS_ERR(cm_id))
636 		goto out;
637 
638 	cm_id->provider_data = iw_event->provider_data;
639 	cm_id->local_addr = iw_event->local_addr;
640 	cm_id->remote_addr = iw_event->remote_addr;
641 
642 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
643 	cm_id_priv->state = IW_CM_STATE_CONN_RECV;
644 
645 	/*
646 	 * We could be destroying the listening id. If so, ignore this
647 	 * upcall.
648 	 */
649 	spin_lock_irqsave(&listen_id_priv->lock, flags);
650 	if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
651 		spin_unlock_irqrestore(&listen_id_priv->lock, flags);
652 		iw_cm_reject(cm_id, NULL, 0);
653 		iw_destroy_cm_id(cm_id);
654 		goto out;
655 	}
656 	spin_unlock_irqrestore(&listen_id_priv->lock, flags);
657 
658 	ret = alloc_work_entries(cm_id_priv, 3);
659 	if (ret) {
660 		iw_cm_reject(cm_id, NULL, 0);
661 		iw_destroy_cm_id(cm_id);
662 		goto out;
663 	}
664 
665 	/* Call the client CM handler */
666 	ret = cm_id->cm_handler(cm_id, iw_event);
667 	if (ret) {
668 		iw_cm_reject(cm_id, NULL, 0);
669 		set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
670 		destroy_cm_id(cm_id);
671 		if (atomic_read(&cm_id_priv->refcount)==0)
672 			free_cm_id(cm_id_priv);
673 	}
674 
675 out:
676 	if (iw_event->private_data_len)
677 		kfree(iw_event->private_data);
678 }
679 
680 /*
681  * Passive Side: CM_ID <-- ESTABLISHED
682  *
683  * The provider generated an ESTABLISHED event which means that
684  * the MPA negotion has completed successfully and we are now in MPA
685  * FPDU mode.
686  *
687  * This event can only be received in the CONN_RECV state. If the
688  * remote peer closed, the ESTABLISHED event would be received followed
689  * by the CLOSE event. If the app closes, it will block until we wake
690  * it up after processing this event.
691  */
cm_conn_est_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)692 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
693 			       struct iw_cm_event *iw_event)
694 {
695 	unsigned long flags;
696 	int ret;
697 
698 	spin_lock_irqsave(&cm_id_priv->lock, flags);
699 
700 	/*
701 	 * We clear the CONNECT_WAIT bit here to allow the callback
702 	 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
703 	 * from a callback handler is not allowed.
704 	 */
705 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
706 	BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
707 	cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
708 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
709 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
710 	wake_up_all(&cm_id_priv->connect_wait);
711 
712 	return ret;
713 }
714 
715 /*
716  * Active Side: CM_ID <-- ESTABLISHED
717  *
718  * The app has called connect and is waiting for the established event to
719  * post it's requests to the server. This event will wake up anyone
720  * blocked in iw_cm_disconnect or iw_destroy_id.
721  */
cm_conn_rep_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)722 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
723 			       struct iw_cm_event *iw_event)
724 {
725 	unsigned long flags;
726 	int ret;
727 
728 	spin_lock_irqsave(&cm_id_priv->lock, flags);
729 	/*
730 	 * Clear the connect wait bit so a callback function calling
731 	 * iw_cm_disconnect will not wait and deadlock this thread
732 	 */
733 	clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
734 	BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
735 	if (iw_event->status == 0) {
736 		cm_id_priv->id.local_addr = iw_event->local_addr;
737 		cm_id_priv->id.remote_addr = iw_event->remote_addr;
738 		cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
739 	} else {
740 		/* REJECTED or RESET */
741 		cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
742 		cm_id_priv->qp = NULL;
743 		cm_id_priv->state = IW_CM_STATE_IDLE;
744 	}
745 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
746 	ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
747 
748 	if (iw_event->private_data_len)
749 		kfree(iw_event->private_data);
750 
751 	/* Wake up waiters on connect complete */
752 	wake_up_all(&cm_id_priv->connect_wait);
753 
754 	return ret;
755 }
756 
757 /*
758  * CM_ID <-- CLOSING
759  *
760  * If in the ESTABLISHED state, move to CLOSING.
761  */
cm_disconnect_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)762 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
763 				  struct iw_cm_event *iw_event)
764 {
765 	unsigned long flags;
766 
767 	spin_lock_irqsave(&cm_id_priv->lock, flags);
768 	if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
769 		cm_id_priv->state = IW_CM_STATE_CLOSING;
770 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
771 }
772 
773 /*
774  * CM_ID <-- IDLE
775  *
776  * If in the ESTBLISHED or CLOSING states, the QP will have have been
777  * moved by the provider to the ERR state. Disassociate the CM_ID from
778  * the QP,  move to IDLE, and remove the 'connected' reference.
779  *
780  * If in some other state, the cm_id was destroyed asynchronously.
781  * This is the last reference that will result in waking up
782  * the app thread blocked in iw_destroy_cm_id.
783  */
cm_close_handler(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)784 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
785 				  struct iw_cm_event *iw_event)
786 {
787 	unsigned long flags;
788 	int ret = 0;
789 	spin_lock_irqsave(&cm_id_priv->lock, flags);
790 
791 	if (cm_id_priv->qp) {
792 		cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
793 		cm_id_priv->qp = NULL;
794 	}
795 	switch (cm_id_priv->state) {
796 	case IW_CM_STATE_ESTABLISHED:
797 	case IW_CM_STATE_CLOSING:
798 		cm_id_priv->state = IW_CM_STATE_IDLE;
799 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
800 		ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
801 		spin_lock_irqsave(&cm_id_priv->lock, flags);
802 		break;
803 	case IW_CM_STATE_DESTROYING:
804 		break;
805 	default:
806 		BUG();
807 	}
808 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
809 
810 	return ret;
811 }
812 
process_event(struct iwcm_id_private * cm_id_priv,struct iw_cm_event * iw_event)813 static int process_event(struct iwcm_id_private *cm_id_priv,
814 			 struct iw_cm_event *iw_event)
815 {
816 	int ret = 0;
817 
818 	switch (iw_event->event) {
819 	case IW_CM_EVENT_CONNECT_REQUEST:
820 		cm_conn_req_handler(cm_id_priv, iw_event);
821 		break;
822 	case IW_CM_EVENT_CONNECT_REPLY:
823 		ret = cm_conn_rep_handler(cm_id_priv, iw_event);
824 		break;
825 	case IW_CM_EVENT_ESTABLISHED:
826 		ret = cm_conn_est_handler(cm_id_priv, iw_event);
827 		break;
828 	case IW_CM_EVENT_DISCONNECT:
829 		cm_disconnect_handler(cm_id_priv, iw_event);
830 		break;
831 	case IW_CM_EVENT_CLOSE:
832 		ret = cm_close_handler(cm_id_priv, iw_event);
833 		break;
834 	default:
835 		BUG();
836 	}
837 
838 	return ret;
839 }
840 
841 /*
842  * Process events on the work_list for the cm_id. If the callback
843  * function requests that the cm_id be deleted, a flag is set in the
844  * cm_id flags to indicate that when the last reference is
845  * removed, the cm_id is to be destroyed. This is necessary to
846  * distinguish between an object that will be destroyed by the app
847  * thread asleep on the destroy_comp list vs. an object destroyed
848  * here synchronously when the last reference is removed.
849  */
cm_work_handler(struct work_struct * _work)850 static void cm_work_handler(struct work_struct *_work)
851 {
852 	struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
853 	struct iw_cm_event levent;
854 	struct iwcm_id_private *cm_id_priv = work->cm_id;
855 	unsigned long flags;
856 	int empty;
857 	int ret = 0;
858 	int destroy_id;
859 
860 	spin_lock_irqsave(&cm_id_priv->lock, flags);
861 	empty = list_empty(&cm_id_priv->work_list);
862 	while (!empty) {
863 		work = list_entry(cm_id_priv->work_list.next,
864 				  struct iwcm_work, list);
865 		list_del_init(&work->list);
866 		empty = list_empty(&cm_id_priv->work_list);
867 		levent = work->event;
868 		put_work(work);
869 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
870 
871 		ret = process_event(cm_id_priv, &levent);
872 		if (ret) {
873 			set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
874 			destroy_cm_id(&cm_id_priv->id);
875 		}
876 		BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
877 		destroy_id = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
878 		if (iwcm_deref_id(cm_id_priv)) {
879 			if (destroy_id) {
880 				BUG_ON(!list_empty(&cm_id_priv->work_list));
881 				free_cm_id(cm_id_priv);
882 			}
883 			return;
884 		}
885 		spin_lock_irqsave(&cm_id_priv->lock, flags);
886 	}
887 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
888 }
889 
890 /*
891  * This function is called on interrupt context. Schedule events on
892  * the iwcm_wq thread to allow callback functions to downcall into
893  * the CM and/or block.  Events are queued to a per-CM_ID
894  * work_list. If this is the first event on the work_list, the work
895  * element is also queued on the iwcm_wq thread.
896  *
897  * Each event holds a reference on the cm_id. Until the last posted
898  * event has been delivered and processed, the cm_id cannot be
899  * deleted.
900  *
901  * Returns:
902  * 	      0	- the event was handled.
903  *	-ENOMEM	- the event was not handled due to lack of resources.
904  */
cm_event_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)905 static int cm_event_handler(struct iw_cm_id *cm_id,
906 			     struct iw_cm_event *iw_event)
907 {
908 	struct iwcm_work *work;
909 	struct iwcm_id_private *cm_id_priv;
910 	unsigned long flags;
911 	int ret = 0;
912 
913 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
914 
915 	spin_lock_irqsave(&cm_id_priv->lock, flags);
916 	work = get_work(cm_id_priv);
917 	if (!work) {
918 		ret = -ENOMEM;
919 		goto out;
920 	}
921 
922 	INIT_WORK(&work->work, cm_work_handler);
923 	work->cm_id = cm_id_priv;
924 	work->event = *iw_event;
925 
926 	if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
927 	     work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
928 	    work->event.private_data_len) {
929 		ret = copy_private_data(&work->event);
930 		if (ret) {
931 			put_work(work);
932 			goto out;
933 		}
934 	}
935 
936 	atomic_inc(&cm_id_priv->refcount);
937 	if (list_empty(&cm_id_priv->work_list)) {
938 		list_add_tail(&work->list, &cm_id_priv->work_list);
939 		queue_work(iwcm_wq, &work->work);
940 	} else
941 		list_add_tail(&work->list, &cm_id_priv->work_list);
942 out:
943 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
944 	return ret;
945 }
946 
iwcm_init_qp_init_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)947 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
948 				  struct ib_qp_attr *qp_attr,
949 				  int *qp_attr_mask)
950 {
951 	unsigned long flags;
952 	int ret;
953 
954 	spin_lock_irqsave(&cm_id_priv->lock, flags);
955 	switch (cm_id_priv->state) {
956 	case IW_CM_STATE_IDLE:
957 	case IW_CM_STATE_CONN_SENT:
958 	case IW_CM_STATE_CONN_RECV:
959 	case IW_CM_STATE_ESTABLISHED:
960 		*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
961 		qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
962 					   IB_ACCESS_REMOTE_READ;
963 		ret = 0;
964 		break;
965 	default:
966 		ret = -EINVAL;
967 		break;
968 	}
969 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
970 	return ret;
971 }
972 
iwcm_init_qp_rts_attr(struct iwcm_id_private * cm_id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)973 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
974 				  struct ib_qp_attr *qp_attr,
975 				  int *qp_attr_mask)
976 {
977 	unsigned long flags;
978 	int ret;
979 
980 	spin_lock_irqsave(&cm_id_priv->lock, flags);
981 	switch (cm_id_priv->state) {
982 	case IW_CM_STATE_IDLE:
983 	case IW_CM_STATE_CONN_SENT:
984 	case IW_CM_STATE_CONN_RECV:
985 	case IW_CM_STATE_ESTABLISHED:
986 		*qp_attr_mask = 0;
987 		ret = 0;
988 		break;
989 	default:
990 		ret = -EINVAL;
991 		break;
992 	}
993 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
994 	return ret;
995 }
996 
iw_cm_init_qp_attr(struct iw_cm_id * cm_id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)997 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
998 		       struct ib_qp_attr *qp_attr,
999 		       int *qp_attr_mask)
1000 {
1001 	struct iwcm_id_private *cm_id_priv;
1002 	int ret;
1003 
1004 	cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1005 	switch (qp_attr->qp_state) {
1006 	case IB_QPS_INIT:
1007 	case IB_QPS_RTR:
1008 		ret = iwcm_init_qp_init_attr(cm_id_priv,
1009 					     qp_attr, qp_attr_mask);
1010 		break;
1011 	case IB_QPS_RTS:
1012 		ret = iwcm_init_qp_rts_attr(cm_id_priv,
1013 					    qp_attr, qp_attr_mask);
1014 		break;
1015 	default:
1016 		ret = -EINVAL;
1017 		break;
1018 	}
1019 	return ret;
1020 }
1021 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1022 
iw_cm_init(void)1023 static int __init iw_cm_init(void)
1024 {
1025 	iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
1026 	if (!iwcm_wq)
1027 		return -ENOMEM;
1028 
1029 	return 0;
1030 }
1031 
iw_cm_cleanup(void)1032 static void __exit iw_cm_cleanup(void)
1033 {
1034 	destroy_workqueue(iwcm_wq);
1035 }
1036 
1037 module_init(iw_cm_init);
1038 module_exit(iw_cm_cleanup);
1039