USB: serial: fix whitespace issues
[zen-stable.git] / drivers / infiniband / core / iwcm.c
blob1a696f76b61627b77c2653e7c5275ca19b2bb628
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.
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:
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer.
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.
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.
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>
50 #include <rdma/iw_cm.h>
51 #include <rdma/ib_addr.h>
53 #include "iwcm.h"
55 MODULE_AUTHOR("Tom Tucker");
56 MODULE_DESCRIPTION("iWARP CM");
57 MODULE_LICENSE("Dual BSD/GPL");
59 static struct workqueue_struct *iwcm_wq;
60 struct iwcm_work {
61 struct work_struct work;
62 struct iwcm_id_private *cm_id;
63 struct list_head list;
64 struct iw_cm_event event;
65 struct list_head free_list;
69 * The following services provide a mechanism for pre-allocating iwcm_work
70 * elements. The design pre-allocates them based on the cm_id type:
71 * LISTENING IDS: Get enough elements preallocated to handle the
72 * listen backlog.
73 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
74 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
76 * Allocating them in connect and listen avoids having to deal
77 * with allocation failures on the event upcall from the provider (which
78 * is called in the interrupt context).
80 * One exception is when creating the cm_id for incoming connection requests.
81 * There are two cases:
82 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
83 * the backlog is exceeded, then no more connection request events will
84 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
85 * to the provider to reject the connection request.
86 * 2) in the connection request workqueue handler, cm_conn_req_handler().
87 * If work elements cannot be allocated for the new connect request cm_id,
88 * then IWCM will call the provider reject method. This is ok since
89 * cm_conn_req_handler() runs in the workqueue thread context.
92 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
94 struct iwcm_work *work;
96 if (list_empty(&cm_id_priv->work_free_list))
97 return NULL;
98 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
99 free_list);
100 list_del_init(&work->free_list);
101 return work;
104 static void put_work(struct iwcm_work *work)
106 list_add(&work->free_list, &work->cm_id->work_free_list);
109 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
111 struct list_head *e, *tmp;
113 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
114 kfree(list_entry(e, struct iwcm_work, free_list));
117 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
119 struct iwcm_work *work;
121 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
122 while (count--) {
123 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
124 if (!work) {
125 dealloc_work_entries(cm_id_priv);
126 return -ENOMEM;
128 work->cm_id = cm_id_priv;
129 INIT_LIST_HEAD(&work->list);
130 put_work(work);
132 return 0;
136 * Save private data from incoming connection requests to
137 * iw_cm_event, so the low level driver doesn't have to. Adjust
138 * the event ptr to point to the local copy.
140 static int copy_private_data(struct iw_cm_event *event)
142 void *p;
144 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
145 if (!p)
146 return -ENOMEM;
147 event->private_data = p;
148 return 0;
151 static void free_cm_id(struct iwcm_id_private *cm_id_priv)
153 dealloc_work_entries(cm_id_priv);
154 kfree(cm_id_priv);
158 * Release a reference on cm_id. If the last reference is being
159 * released, enable the waiting thread (in iw_destroy_cm_id) to
160 * get woken up, and return 1 if a thread is already waiting.
162 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
164 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
165 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
166 BUG_ON(!list_empty(&cm_id_priv->work_list));
167 complete(&cm_id_priv->destroy_comp);
168 return 1;
171 return 0;
174 static void add_ref(struct iw_cm_id *cm_id)
176 struct iwcm_id_private *cm_id_priv;
177 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
178 atomic_inc(&cm_id_priv->refcount);
181 static void rem_ref(struct iw_cm_id *cm_id)
183 struct iwcm_id_private *cm_id_priv;
184 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
185 if (iwcm_deref_id(cm_id_priv) &&
186 test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
187 BUG_ON(!list_empty(&cm_id_priv->work_list));
188 free_cm_id(cm_id_priv);
192 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
194 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
195 iw_cm_handler cm_handler,
196 void *context)
198 struct iwcm_id_private *cm_id_priv;
200 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
201 if (!cm_id_priv)
202 return ERR_PTR(-ENOMEM);
204 cm_id_priv->state = IW_CM_STATE_IDLE;
205 cm_id_priv->id.device = device;
206 cm_id_priv->id.cm_handler = cm_handler;
207 cm_id_priv->id.context = context;
208 cm_id_priv->id.event_handler = cm_event_handler;
209 cm_id_priv->id.add_ref = add_ref;
210 cm_id_priv->id.rem_ref = rem_ref;
211 spin_lock_init(&cm_id_priv->lock);
212 atomic_set(&cm_id_priv->refcount, 1);
213 init_waitqueue_head(&cm_id_priv->connect_wait);
214 init_completion(&cm_id_priv->destroy_comp);
215 INIT_LIST_HEAD(&cm_id_priv->work_list);
216 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
218 return &cm_id_priv->id;
220 EXPORT_SYMBOL(iw_create_cm_id);
223 static int iwcm_modify_qp_err(struct ib_qp *qp)
225 struct ib_qp_attr qp_attr;
227 if (!qp)
228 return -EINVAL;
230 qp_attr.qp_state = IB_QPS_ERR;
231 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
235 * This is really the RDMAC CLOSING state. It is most similar to the
236 * IB SQD QP state.
238 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
240 struct ib_qp_attr qp_attr;
242 BUG_ON(qp == NULL);
243 qp_attr.qp_state = IB_QPS_SQD;
244 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
248 * CM_ID <-- CLOSING
250 * Block if a passive or active connection is currently being processed. Then
251 * process the event as follows:
252 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
253 * based on the abrupt flag
254 * - If the connection is already in the CLOSING or IDLE state, the peer is
255 * disconnecting concurrently with us and we've already seen the
256 * DISCONNECT event -- ignore the request and return 0
257 * - Disconnect on a listening endpoint returns -EINVAL
259 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
261 struct iwcm_id_private *cm_id_priv;
262 unsigned long flags;
263 int ret = 0;
264 struct ib_qp *qp = NULL;
266 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
267 /* Wait if we're currently in a connect or accept downcall */
268 wait_event(cm_id_priv->connect_wait,
269 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
271 spin_lock_irqsave(&cm_id_priv->lock, flags);
272 switch (cm_id_priv->state) {
273 case IW_CM_STATE_ESTABLISHED:
274 cm_id_priv->state = IW_CM_STATE_CLOSING;
276 /* QP could be <nul> for user-mode client */
277 if (cm_id_priv->qp)
278 qp = cm_id_priv->qp;
279 else
280 ret = -EINVAL;
281 break;
282 case IW_CM_STATE_LISTEN:
283 ret = -EINVAL;
284 break;
285 case IW_CM_STATE_CLOSING:
286 /* remote peer closed first */
287 case IW_CM_STATE_IDLE:
288 /* accept or connect returned !0 */
289 break;
290 case IW_CM_STATE_CONN_RECV:
292 * App called disconnect before/without calling accept after
293 * connect_request event delivered.
295 break;
296 case IW_CM_STATE_CONN_SENT:
297 /* Can only get here if wait above fails */
298 default:
299 BUG();
301 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
303 if (qp) {
304 if (abrupt)
305 ret = iwcm_modify_qp_err(qp);
306 else
307 ret = iwcm_modify_qp_sqd(qp);
310 * If both sides are disconnecting the QP could
311 * already be in ERR or SQD states
313 ret = 0;
316 return ret;
318 EXPORT_SYMBOL(iw_cm_disconnect);
321 * CM_ID <-- DESTROYING
323 * Clean up all resources associated with the connection and release
324 * the initial reference taken by iw_create_cm_id.
326 static void destroy_cm_id(struct iw_cm_id *cm_id)
328 struct iwcm_id_private *cm_id_priv;
329 unsigned long flags;
330 int ret;
332 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
334 * Wait if we're currently in a connect or accept downcall. A
335 * listening endpoint should never block here.
337 wait_event(cm_id_priv->connect_wait,
338 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
340 spin_lock_irqsave(&cm_id_priv->lock, flags);
341 switch (cm_id_priv->state) {
342 case IW_CM_STATE_LISTEN:
343 cm_id_priv->state = IW_CM_STATE_DESTROYING;
344 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
345 /* destroy the listening endpoint */
346 ret = cm_id->device->iwcm->destroy_listen(cm_id);
347 spin_lock_irqsave(&cm_id_priv->lock, flags);
348 break;
349 case IW_CM_STATE_ESTABLISHED:
350 cm_id_priv->state = IW_CM_STATE_DESTROYING;
351 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
352 /* Abrupt close of the connection */
353 (void)iwcm_modify_qp_err(cm_id_priv->qp);
354 spin_lock_irqsave(&cm_id_priv->lock, flags);
355 break;
356 case IW_CM_STATE_IDLE:
357 case IW_CM_STATE_CLOSING:
358 cm_id_priv->state = IW_CM_STATE_DESTROYING;
359 break;
360 case IW_CM_STATE_CONN_RECV:
362 * App called destroy before/without calling accept after
363 * receiving connection request event notification or
364 * returned non zero from the event callback function.
365 * In either case, must tell the provider to reject.
367 cm_id_priv->state = IW_CM_STATE_DESTROYING;
368 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
369 cm_id->device->iwcm->reject(cm_id, NULL, 0);
370 spin_lock_irqsave(&cm_id_priv->lock, flags);
371 break;
372 case IW_CM_STATE_CONN_SENT:
373 case IW_CM_STATE_DESTROYING:
374 default:
375 BUG();
376 break;
378 if (cm_id_priv->qp) {
379 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
380 cm_id_priv->qp = NULL;
382 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
384 (void)iwcm_deref_id(cm_id_priv);
388 * This function is only called by the application thread and cannot
389 * be called by the event thread. The function will wait for all
390 * references to be released on the cm_id and then kfree the cm_id
391 * object.
393 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
395 struct iwcm_id_private *cm_id_priv;
397 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
398 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
400 destroy_cm_id(cm_id);
402 wait_for_completion(&cm_id_priv->destroy_comp);
404 free_cm_id(cm_id_priv);
406 EXPORT_SYMBOL(iw_destroy_cm_id);
409 * CM_ID <-- LISTEN
411 * Start listening for connect requests. Generates one CONNECT_REQUEST
412 * event for each inbound connect request.
414 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
416 struct iwcm_id_private *cm_id_priv;
417 unsigned long flags;
418 int ret;
420 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
422 ret = alloc_work_entries(cm_id_priv, backlog);
423 if (ret)
424 return ret;
426 spin_lock_irqsave(&cm_id_priv->lock, flags);
427 switch (cm_id_priv->state) {
428 case IW_CM_STATE_IDLE:
429 cm_id_priv->state = IW_CM_STATE_LISTEN;
430 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
431 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
432 if (ret)
433 cm_id_priv->state = IW_CM_STATE_IDLE;
434 spin_lock_irqsave(&cm_id_priv->lock, flags);
435 break;
436 default:
437 ret = -EINVAL;
439 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
441 return ret;
443 EXPORT_SYMBOL(iw_cm_listen);
446 * CM_ID <-- IDLE
448 * Rejects an inbound connection request. No events are generated.
450 int iw_cm_reject(struct iw_cm_id *cm_id,
451 const void *private_data,
452 u8 private_data_len)
454 struct iwcm_id_private *cm_id_priv;
455 unsigned long flags;
456 int ret;
458 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
459 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
461 spin_lock_irqsave(&cm_id_priv->lock, flags);
462 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
463 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
464 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
465 wake_up_all(&cm_id_priv->connect_wait);
466 return -EINVAL;
468 cm_id_priv->state = IW_CM_STATE_IDLE;
469 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
471 ret = cm_id->device->iwcm->reject(cm_id, private_data,
472 private_data_len);
474 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
475 wake_up_all(&cm_id_priv->connect_wait);
477 return ret;
479 EXPORT_SYMBOL(iw_cm_reject);
482 * CM_ID <-- ESTABLISHED
484 * Accepts an inbound connection request and generates an ESTABLISHED
485 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
486 * until the ESTABLISHED event is received from the provider.
488 int iw_cm_accept(struct iw_cm_id *cm_id,
489 struct iw_cm_conn_param *iw_param)
491 struct iwcm_id_private *cm_id_priv;
492 struct ib_qp *qp;
493 unsigned long flags;
494 int ret;
496 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
497 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
499 spin_lock_irqsave(&cm_id_priv->lock, flags);
500 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
501 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
502 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
503 wake_up_all(&cm_id_priv->connect_wait);
504 return -EINVAL;
506 /* Get the ib_qp given the QPN */
507 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
508 if (!qp) {
509 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
510 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
511 wake_up_all(&cm_id_priv->connect_wait);
512 return -EINVAL;
514 cm_id->device->iwcm->add_ref(qp);
515 cm_id_priv->qp = qp;
516 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
518 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
519 if (ret) {
520 /* An error on accept precludes provider events */
521 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
522 cm_id_priv->state = IW_CM_STATE_IDLE;
523 spin_lock_irqsave(&cm_id_priv->lock, flags);
524 if (cm_id_priv->qp) {
525 cm_id->device->iwcm->rem_ref(qp);
526 cm_id_priv->qp = NULL;
528 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
529 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
530 wake_up_all(&cm_id_priv->connect_wait);
533 return ret;
535 EXPORT_SYMBOL(iw_cm_accept);
538 * Active Side: CM_ID <-- CONN_SENT
540 * If successful, results in the generation of a CONNECT_REPLY
541 * event. iw_cm_disconnect and iw_cm_destroy will block until the
542 * CONNECT_REPLY event is received from the provider.
544 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
546 struct iwcm_id_private *cm_id_priv;
547 int ret;
548 unsigned long flags;
549 struct ib_qp *qp;
551 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
553 ret = alloc_work_entries(cm_id_priv, 4);
554 if (ret)
555 return ret;
557 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
558 spin_lock_irqsave(&cm_id_priv->lock, flags);
560 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
561 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
562 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
563 wake_up_all(&cm_id_priv->connect_wait);
564 return -EINVAL;
567 /* Get the ib_qp given the QPN */
568 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
569 if (!qp) {
570 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
571 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
572 wake_up_all(&cm_id_priv->connect_wait);
573 return -EINVAL;
575 cm_id->device->iwcm->add_ref(qp);
576 cm_id_priv->qp = qp;
577 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
578 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
580 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
581 if (ret) {
582 spin_lock_irqsave(&cm_id_priv->lock, flags);
583 if (cm_id_priv->qp) {
584 cm_id->device->iwcm->rem_ref(qp);
585 cm_id_priv->qp = NULL;
587 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
588 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
589 cm_id_priv->state = IW_CM_STATE_IDLE;
590 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
591 wake_up_all(&cm_id_priv->connect_wait);
594 return ret;
596 EXPORT_SYMBOL(iw_cm_connect);
599 * Passive Side: new CM_ID <-- CONN_RECV
601 * Handles an inbound connect request. The function creates a new
602 * iw_cm_id to represent the new connection and inherits the client
603 * callback function and other attributes from the listening parent.
605 * The work item contains a pointer to the listen_cm_id and the event. The
606 * listen_cm_id contains the client cm_handler, context and
607 * device. These are copied when the device is cloned. The event
608 * contains the new four tuple.
610 * An error on the child should not affect the parent, so this
611 * function does not return a value.
613 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
614 struct iw_cm_event *iw_event)
616 unsigned long flags;
617 struct iw_cm_id *cm_id;
618 struct iwcm_id_private *cm_id_priv;
619 int ret;
622 * The provider should never generate a connection request
623 * event with a bad status.
625 BUG_ON(iw_event->status);
628 * We could be destroying the listening id. If so, ignore this
629 * upcall.
631 spin_lock_irqsave(&listen_id_priv->lock, flags);
632 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
633 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
634 goto out;
636 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
638 cm_id = iw_create_cm_id(listen_id_priv->id.device,
639 listen_id_priv->id.cm_handler,
640 listen_id_priv->id.context);
641 /* If the cm_id could not be created, ignore the request */
642 if (IS_ERR(cm_id))
643 goto out;
645 cm_id->provider_data = iw_event->provider_data;
646 cm_id->local_addr = iw_event->local_addr;
647 cm_id->remote_addr = iw_event->remote_addr;
649 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
650 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
652 ret = alloc_work_entries(cm_id_priv, 3);
653 if (ret) {
654 iw_cm_reject(cm_id, NULL, 0);
655 iw_destroy_cm_id(cm_id);
656 goto out;
659 /* Call the client CM handler */
660 ret = cm_id->cm_handler(cm_id, iw_event);
661 if (ret) {
662 iw_cm_reject(cm_id, NULL, 0);
663 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
664 destroy_cm_id(cm_id);
665 if (atomic_read(&cm_id_priv->refcount)==0)
666 free_cm_id(cm_id_priv);
669 out:
670 if (iw_event->private_data_len)
671 kfree(iw_event->private_data);
675 * Passive Side: CM_ID <-- ESTABLISHED
677 * The provider generated an ESTABLISHED event which means that
678 * the MPA negotion has completed successfully and we are now in MPA
679 * FPDU mode.
681 * This event can only be received in the CONN_RECV state. If the
682 * remote peer closed, the ESTABLISHED event would be received followed
683 * by the CLOSE event. If the app closes, it will block until we wake
684 * it up after processing this event.
686 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
687 struct iw_cm_event *iw_event)
689 unsigned long flags;
690 int ret;
692 spin_lock_irqsave(&cm_id_priv->lock, flags);
695 * We clear the CONNECT_WAIT bit here to allow the callback
696 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
697 * from a callback handler is not allowed.
699 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
700 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
701 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
702 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
703 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
704 wake_up_all(&cm_id_priv->connect_wait);
706 return ret;
710 * Active Side: CM_ID <-- ESTABLISHED
712 * The app has called connect and is waiting for the established event to
713 * post it's requests to the server. This event will wake up anyone
714 * blocked in iw_cm_disconnect or iw_destroy_id.
716 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
717 struct iw_cm_event *iw_event)
719 unsigned long flags;
720 int ret;
722 spin_lock_irqsave(&cm_id_priv->lock, flags);
724 * Clear the connect wait bit so a callback function calling
725 * iw_cm_disconnect will not wait and deadlock this thread
727 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
728 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
729 if (iw_event->status == 0) {
730 cm_id_priv->id.local_addr = iw_event->local_addr;
731 cm_id_priv->id.remote_addr = iw_event->remote_addr;
732 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
733 } else {
734 /* REJECTED or RESET */
735 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
736 cm_id_priv->qp = NULL;
737 cm_id_priv->state = IW_CM_STATE_IDLE;
739 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
740 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
742 if (iw_event->private_data_len)
743 kfree(iw_event->private_data);
745 /* Wake up waiters on connect complete */
746 wake_up_all(&cm_id_priv->connect_wait);
748 return ret;
752 * CM_ID <-- CLOSING
754 * If in the ESTABLISHED state, move to CLOSING.
756 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
757 struct iw_cm_event *iw_event)
759 unsigned long flags;
761 spin_lock_irqsave(&cm_id_priv->lock, flags);
762 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
763 cm_id_priv->state = IW_CM_STATE_CLOSING;
764 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
768 * CM_ID <-- IDLE
770 * If in the ESTBLISHED or CLOSING states, the QP will have have been
771 * moved by the provider to the ERR state. Disassociate the CM_ID from
772 * the QP, move to IDLE, and remove the 'connected' reference.
774 * If in some other state, the cm_id was destroyed asynchronously.
775 * This is the last reference that will result in waking up
776 * the app thread blocked in iw_destroy_cm_id.
778 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
779 struct iw_cm_event *iw_event)
781 unsigned long flags;
782 int ret = 0;
783 spin_lock_irqsave(&cm_id_priv->lock, flags);
785 if (cm_id_priv->qp) {
786 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
787 cm_id_priv->qp = NULL;
789 switch (cm_id_priv->state) {
790 case IW_CM_STATE_ESTABLISHED:
791 case IW_CM_STATE_CLOSING:
792 cm_id_priv->state = IW_CM_STATE_IDLE;
793 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
794 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
795 spin_lock_irqsave(&cm_id_priv->lock, flags);
796 break;
797 case IW_CM_STATE_DESTROYING:
798 break;
799 default:
800 BUG();
802 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
804 return ret;
807 static int process_event(struct iwcm_id_private *cm_id_priv,
808 struct iw_cm_event *iw_event)
810 int ret = 0;
812 switch (iw_event->event) {
813 case IW_CM_EVENT_CONNECT_REQUEST:
814 cm_conn_req_handler(cm_id_priv, iw_event);
815 break;
816 case IW_CM_EVENT_CONNECT_REPLY:
817 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
818 break;
819 case IW_CM_EVENT_ESTABLISHED:
820 ret = cm_conn_est_handler(cm_id_priv, iw_event);
821 break;
822 case IW_CM_EVENT_DISCONNECT:
823 cm_disconnect_handler(cm_id_priv, iw_event);
824 break;
825 case IW_CM_EVENT_CLOSE:
826 ret = cm_close_handler(cm_id_priv, iw_event);
827 break;
828 default:
829 BUG();
832 return ret;
836 * Process events on the work_list for the cm_id. If the callback
837 * function requests that the cm_id be deleted, a flag is set in the
838 * cm_id flags to indicate that when the last reference is
839 * removed, the cm_id is to be destroyed. This is necessary to
840 * distinguish between an object that will be destroyed by the app
841 * thread asleep on the destroy_comp list vs. an object destroyed
842 * here synchronously when the last reference is removed.
844 static void cm_work_handler(struct work_struct *_work)
846 struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
847 struct iw_cm_event levent;
848 struct iwcm_id_private *cm_id_priv = work->cm_id;
849 unsigned long flags;
850 int empty;
851 int ret = 0;
852 int destroy_id;
854 spin_lock_irqsave(&cm_id_priv->lock, flags);
855 empty = list_empty(&cm_id_priv->work_list);
856 while (!empty) {
857 work = list_entry(cm_id_priv->work_list.next,
858 struct iwcm_work, list);
859 list_del_init(&work->list);
860 empty = list_empty(&cm_id_priv->work_list);
861 levent = work->event;
862 put_work(work);
863 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
865 ret = process_event(cm_id_priv, &levent);
866 if (ret) {
867 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
868 destroy_cm_id(&cm_id_priv->id);
870 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
871 destroy_id = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
872 if (iwcm_deref_id(cm_id_priv)) {
873 if (destroy_id) {
874 BUG_ON(!list_empty(&cm_id_priv->work_list));
875 free_cm_id(cm_id_priv);
877 return;
879 spin_lock_irqsave(&cm_id_priv->lock, flags);
881 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
885 * This function is called on interrupt context. Schedule events on
886 * the iwcm_wq thread to allow callback functions to downcall into
887 * the CM and/or block. Events are queued to a per-CM_ID
888 * work_list. If this is the first event on the work_list, the work
889 * element is also queued on the iwcm_wq thread.
891 * Each event holds a reference on the cm_id. Until the last posted
892 * event has been delivered and processed, the cm_id cannot be
893 * deleted.
895 * Returns:
896 * 0 - the event was handled.
897 * -ENOMEM - the event was not handled due to lack of resources.
899 static int cm_event_handler(struct iw_cm_id *cm_id,
900 struct iw_cm_event *iw_event)
902 struct iwcm_work *work;
903 struct iwcm_id_private *cm_id_priv;
904 unsigned long flags;
905 int ret = 0;
907 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
909 spin_lock_irqsave(&cm_id_priv->lock, flags);
910 work = get_work(cm_id_priv);
911 if (!work) {
912 ret = -ENOMEM;
913 goto out;
916 INIT_WORK(&work->work, cm_work_handler);
917 work->cm_id = cm_id_priv;
918 work->event = *iw_event;
920 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
921 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
922 work->event.private_data_len) {
923 ret = copy_private_data(&work->event);
924 if (ret) {
925 put_work(work);
926 goto out;
930 atomic_inc(&cm_id_priv->refcount);
931 if (list_empty(&cm_id_priv->work_list)) {
932 list_add_tail(&work->list, &cm_id_priv->work_list);
933 queue_work(iwcm_wq, &work->work);
934 } else
935 list_add_tail(&work->list, &cm_id_priv->work_list);
936 out:
937 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
938 return ret;
941 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
942 struct ib_qp_attr *qp_attr,
943 int *qp_attr_mask)
945 unsigned long flags;
946 int ret;
948 spin_lock_irqsave(&cm_id_priv->lock, flags);
949 switch (cm_id_priv->state) {
950 case IW_CM_STATE_IDLE:
951 case IW_CM_STATE_CONN_SENT:
952 case IW_CM_STATE_CONN_RECV:
953 case IW_CM_STATE_ESTABLISHED:
954 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
955 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
956 IB_ACCESS_REMOTE_READ;
957 ret = 0;
958 break;
959 default:
960 ret = -EINVAL;
961 break;
963 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
964 return ret;
967 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
968 struct ib_qp_attr *qp_attr,
969 int *qp_attr_mask)
971 unsigned long flags;
972 int ret;
974 spin_lock_irqsave(&cm_id_priv->lock, flags);
975 switch (cm_id_priv->state) {
976 case IW_CM_STATE_IDLE:
977 case IW_CM_STATE_CONN_SENT:
978 case IW_CM_STATE_CONN_RECV:
979 case IW_CM_STATE_ESTABLISHED:
980 *qp_attr_mask = 0;
981 ret = 0;
982 break;
983 default:
984 ret = -EINVAL;
985 break;
987 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
988 return ret;
991 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
992 struct ib_qp_attr *qp_attr,
993 int *qp_attr_mask)
995 struct iwcm_id_private *cm_id_priv;
996 int ret;
998 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
999 switch (qp_attr->qp_state) {
1000 case IB_QPS_INIT:
1001 case IB_QPS_RTR:
1002 ret = iwcm_init_qp_init_attr(cm_id_priv,
1003 qp_attr, qp_attr_mask);
1004 break;
1005 case IB_QPS_RTS:
1006 ret = iwcm_init_qp_rts_attr(cm_id_priv,
1007 qp_attr, qp_attr_mask);
1008 break;
1009 default:
1010 ret = -EINVAL;
1011 break;
1013 return ret;
1015 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1017 static int __init iw_cm_init(void)
1019 iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
1020 if (!iwcm_wq)
1021 return -ENOMEM;
1023 return 0;
1026 static void __exit iw_cm_cleanup(void)
1028 destroy_workqueue(iwcm_wq);
1031 module_init(iw_cm_init);
1032 module_exit(iw_cm_cleanup);