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
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
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
38 #include <linux/dma-mapping.h>
39 #include <linux/err.h>
40 #include <linux/idr.h>
41 #include <linux/interrupt.h>
42 #include <linux/pci.h>
43 #include <linux/rbtree.h>
44 #include <linux/spinlock.h>
45 #include <linux/workqueue.h>
46 #include <linux/completion.h>
48 #include <rdma/iw_cm.h>
49 #include <rdma/ib_addr.h>
53 MODULE_AUTHOR("Tom Tucker");
54 MODULE_DESCRIPTION("iWARP CM");
55 MODULE_LICENSE("Dual BSD/GPL");
57 static struct workqueue_struct *iwcm_wq;
59 struct work_struct work;
60 struct iwcm_id_private *cm_id;
61 struct list_head list;
62 struct iw_cm_event event;
63 struct list_head free_list;
67 * The following services provide a mechanism for pre-allocating iwcm_work
68 * elements. The design pre-allocates them based on the cm_id type:
69 * LISTENING IDS: Get enough elements preallocated to handle the
71 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
72 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
74 * Allocating them in connect and listen avoids having to deal
75 * with allocation failures on the event upcall from the provider (which
76 * is called in the interrupt context).
78 * One exception is when creating the cm_id for incoming connection requests.
79 * There are two cases:
80 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
81 * the backlog is exceeded, then no more connection request events will
82 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
83 * to the provider to reject the connection request.
84 * 2) in the connection request workqueue handler, cm_conn_req_handler().
85 * If work elements cannot be allocated for the new connect request cm_id,
86 * then IWCM will call the provider reject method. This is ok since
87 * cm_conn_req_handler() runs in the workqueue thread context.
90 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
92 struct iwcm_work *work;
94 if (list_empty(&cm_id_priv->work_free_list))
96 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
98 list_del_init(&work->free_list);
102 static void put_work(struct iwcm_work *work)
104 list_add(&work->free_list, &work->cm_id->work_free_list);
107 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
109 struct list_head *e, *tmp;
111 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
112 kfree(list_entry(e, struct iwcm_work, free_list));
115 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
117 struct iwcm_work *work;
119 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
121 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
123 dealloc_work_entries(cm_id_priv);
126 work->cm_id = cm_id_priv;
127 INIT_LIST_HEAD(&work->list);
134 * Save private data from incoming connection requests to
135 * iw_cm_event, so the low level driver doesn't have to. Adjust
136 * the event ptr to point to the local copy.
138 static int copy_private_data(struct iw_cm_event *event)
142 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
145 event->private_data = p;
150 * Release a reference on cm_id. If the last reference is being removed
151 * and iw_destroy_cm_id is waiting, wake up the waiting thread.
153 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
157 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
158 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
159 BUG_ON(!list_empty(&cm_id_priv->work_list));
160 if (waitqueue_active(&cm_id_priv->destroy_comp.wait)) {
161 BUG_ON(cm_id_priv->state != IW_CM_STATE_DESTROYING);
162 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY,
163 &cm_id_priv->flags));
166 complete(&cm_id_priv->destroy_comp);
172 static void add_ref(struct iw_cm_id *cm_id)
174 struct iwcm_id_private *cm_id_priv;
175 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
176 atomic_inc(&cm_id_priv->refcount);
179 static void rem_ref(struct iw_cm_id *cm_id)
181 struct iwcm_id_private *cm_id_priv;
182 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
183 iwcm_deref_id(cm_id_priv);
186 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
188 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
189 iw_cm_handler cm_handler,
192 struct iwcm_id_private *cm_id_priv;
194 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
196 return ERR_PTR(-ENOMEM);
198 cm_id_priv->state = IW_CM_STATE_IDLE;
199 cm_id_priv->id.device = device;
200 cm_id_priv->id.cm_handler = cm_handler;
201 cm_id_priv->id.context = context;
202 cm_id_priv->id.event_handler = cm_event_handler;
203 cm_id_priv->id.add_ref = add_ref;
204 cm_id_priv->id.rem_ref = rem_ref;
205 spin_lock_init(&cm_id_priv->lock);
206 atomic_set(&cm_id_priv->refcount, 1);
207 init_waitqueue_head(&cm_id_priv->connect_wait);
208 init_completion(&cm_id_priv->destroy_comp);
209 INIT_LIST_HEAD(&cm_id_priv->work_list);
210 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
212 return &cm_id_priv->id;
214 EXPORT_SYMBOL(iw_create_cm_id);
217 static int iwcm_modify_qp_err(struct ib_qp *qp)
219 struct ib_qp_attr qp_attr;
224 qp_attr.qp_state = IB_QPS_ERR;
225 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
229 * This is really the RDMAC CLOSING state. It is most similar to the
232 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
234 struct ib_qp_attr qp_attr;
237 qp_attr.qp_state = IB_QPS_SQD;
238 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
244 * Block if a passive or active connection is currently being processed. Then
245 * process the event as follows:
246 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
247 * based on the abrupt flag
248 * - If the connection is already in the CLOSING or IDLE state, the peer is
249 * disconnecting concurrently with us and we've already seen the
250 * DISCONNECT event -- ignore the request and return 0
251 * - Disconnect on a listening endpoint returns -EINVAL
253 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
255 struct iwcm_id_private *cm_id_priv;
258 struct ib_qp *qp = NULL;
260 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
261 /* Wait if we're currently in a connect or accept downcall */
262 wait_event(cm_id_priv->connect_wait,
263 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
265 spin_lock_irqsave(&cm_id_priv->lock, flags);
266 switch (cm_id_priv->state) {
267 case IW_CM_STATE_ESTABLISHED:
268 cm_id_priv->state = IW_CM_STATE_CLOSING;
270 /* QP could be <nul> for user-mode client */
276 case IW_CM_STATE_LISTEN:
279 case IW_CM_STATE_CLOSING:
280 /* remote peer closed first */
281 case IW_CM_STATE_IDLE:
282 /* accept or connect returned !0 */
284 case IW_CM_STATE_CONN_RECV:
286 * App called disconnect before/without calling accept after
287 * connect_request event delivered.
290 case IW_CM_STATE_CONN_SENT:
291 /* Can only get here if wait above fails */
295 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
299 ret = iwcm_modify_qp_err(qp);
301 ret = iwcm_modify_qp_sqd(qp);
304 * If both sides are disconnecting the QP could
305 * already be in ERR or SQD states
312 EXPORT_SYMBOL(iw_cm_disconnect);
315 * CM_ID <-- DESTROYING
317 * Clean up all resources associated with the connection and release
318 * the initial reference taken by iw_create_cm_id.
320 static void destroy_cm_id(struct iw_cm_id *cm_id)
322 struct iwcm_id_private *cm_id_priv;
326 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
328 * Wait if we're currently in a connect or accept downcall. A
329 * listening endpoint should never block here.
331 wait_event(cm_id_priv->connect_wait,
332 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
334 spin_lock_irqsave(&cm_id_priv->lock, flags);
335 switch (cm_id_priv->state) {
336 case IW_CM_STATE_LISTEN:
337 cm_id_priv->state = IW_CM_STATE_DESTROYING;
338 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
339 /* destroy the listening endpoint */
340 ret = cm_id->device->iwcm->destroy_listen(cm_id);
341 spin_lock_irqsave(&cm_id_priv->lock, flags);
343 case IW_CM_STATE_ESTABLISHED:
344 cm_id_priv->state = IW_CM_STATE_DESTROYING;
345 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
346 /* Abrupt close of the connection */
347 (void)iwcm_modify_qp_err(cm_id_priv->qp);
348 spin_lock_irqsave(&cm_id_priv->lock, flags);
350 case IW_CM_STATE_IDLE:
351 case IW_CM_STATE_CLOSING:
352 cm_id_priv->state = IW_CM_STATE_DESTROYING;
354 case IW_CM_STATE_CONN_RECV:
356 * App called destroy before/without calling accept after
357 * receiving connection request event notification.
359 cm_id_priv->state = IW_CM_STATE_DESTROYING;
361 case IW_CM_STATE_CONN_SENT:
362 case IW_CM_STATE_DESTROYING:
367 if (cm_id_priv->qp) {
368 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
369 cm_id_priv->qp = NULL;
371 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
373 (void)iwcm_deref_id(cm_id_priv);
377 * This function is only called by the application thread and cannot
378 * be called by the event thread. The function will wait for all
379 * references to be released on the cm_id and then kfree the cm_id
382 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
384 struct iwcm_id_private *cm_id_priv;
386 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
387 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
389 destroy_cm_id(cm_id);
391 wait_for_completion(&cm_id_priv->destroy_comp);
393 dealloc_work_entries(cm_id_priv);
397 EXPORT_SYMBOL(iw_destroy_cm_id);
402 * Start listening for connect requests. Generates one CONNECT_REQUEST
403 * event for each inbound connect request.
405 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
407 struct iwcm_id_private *cm_id_priv;
411 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
413 ret = alloc_work_entries(cm_id_priv, backlog);
417 spin_lock_irqsave(&cm_id_priv->lock, flags);
418 switch (cm_id_priv->state) {
419 case IW_CM_STATE_IDLE:
420 cm_id_priv->state = IW_CM_STATE_LISTEN;
421 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
422 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
424 cm_id_priv->state = IW_CM_STATE_IDLE;
425 spin_lock_irqsave(&cm_id_priv->lock, flags);
430 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
434 EXPORT_SYMBOL(iw_cm_listen);
439 * Rejects an inbound connection request. No events are generated.
441 int iw_cm_reject(struct iw_cm_id *cm_id,
442 const void *private_data,
445 struct iwcm_id_private *cm_id_priv;
449 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
450 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
452 spin_lock_irqsave(&cm_id_priv->lock, flags);
453 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
454 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
455 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
456 wake_up_all(&cm_id_priv->connect_wait);
459 cm_id_priv->state = IW_CM_STATE_IDLE;
460 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
462 ret = cm_id->device->iwcm->reject(cm_id, private_data,
465 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
466 wake_up_all(&cm_id_priv->connect_wait);
470 EXPORT_SYMBOL(iw_cm_reject);
473 * CM_ID <-- ESTABLISHED
475 * Accepts an inbound connection request and generates an ESTABLISHED
476 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
477 * until the ESTABLISHED event is received from the provider.
479 int iw_cm_accept(struct iw_cm_id *cm_id,
480 struct iw_cm_conn_param *iw_param)
482 struct iwcm_id_private *cm_id_priv;
487 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
488 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
490 spin_lock_irqsave(&cm_id_priv->lock, flags);
491 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
492 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
493 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
494 wake_up_all(&cm_id_priv->connect_wait);
497 /* Get the ib_qp given the QPN */
498 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
500 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
503 cm_id->device->iwcm->add_ref(qp);
505 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
507 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
509 /* An error on accept precludes provider events */
510 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
511 cm_id_priv->state = IW_CM_STATE_IDLE;
512 spin_lock_irqsave(&cm_id_priv->lock, flags);
513 if (cm_id_priv->qp) {
514 cm_id->device->iwcm->rem_ref(qp);
515 cm_id_priv->qp = NULL;
517 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
518 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
519 wake_up_all(&cm_id_priv->connect_wait);
524 EXPORT_SYMBOL(iw_cm_accept);
527 * Active Side: CM_ID <-- CONN_SENT
529 * If successful, results in the generation of a CONNECT_REPLY
530 * event. iw_cm_disconnect and iw_cm_destroy will block until the
531 * CONNECT_REPLY event is received from the provider.
533 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
535 struct iwcm_id_private *cm_id_priv;
540 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
542 ret = alloc_work_entries(cm_id_priv, 4);
546 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
547 spin_lock_irqsave(&cm_id_priv->lock, flags);
549 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
550 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
551 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
552 wake_up_all(&cm_id_priv->connect_wait);
556 /* Get the ib_qp given the QPN */
557 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
559 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
562 cm_id->device->iwcm->add_ref(qp);
564 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
565 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
567 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
569 spin_lock_irqsave(&cm_id_priv->lock, flags);
570 if (cm_id_priv->qp) {
571 cm_id->device->iwcm->rem_ref(qp);
572 cm_id_priv->qp = NULL;
574 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
575 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
576 cm_id_priv->state = IW_CM_STATE_IDLE;
577 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
578 wake_up_all(&cm_id_priv->connect_wait);
583 EXPORT_SYMBOL(iw_cm_connect);
586 * Passive Side: new CM_ID <-- CONN_RECV
588 * Handles an inbound connect request. The function creates a new
589 * iw_cm_id to represent the new connection and inherits the client
590 * callback function and other attributes from the listening parent.
592 * The work item contains a pointer to the listen_cm_id and the event. The
593 * listen_cm_id contains the client cm_handler, context and
594 * device. These are copied when the device is cloned. The event
595 * contains the new four tuple.
597 * An error on the child should not affect the parent, so this
598 * function does not return a value.
600 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
601 struct iw_cm_event *iw_event)
604 struct iw_cm_id *cm_id;
605 struct iwcm_id_private *cm_id_priv;
609 * The provider should never generate a connection request
610 * event with a bad status.
612 BUG_ON(iw_event->status);
615 * We could be destroying the listening id. If so, ignore this
618 spin_lock_irqsave(&listen_id_priv->lock, flags);
619 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
620 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
623 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
625 cm_id = iw_create_cm_id(listen_id_priv->id.device,
626 listen_id_priv->id.cm_handler,
627 listen_id_priv->id.context);
628 /* If the cm_id could not be created, ignore the request */
632 cm_id->provider_data = iw_event->provider_data;
633 cm_id->local_addr = iw_event->local_addr;
634 cm_id->remote_addr = iw_event->remote_addr;
636 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
637 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
639 ret = alloc_work_entries(cm_id_priv, 3);
641 iw_cm_reject(cm_id, NULL, 0);
642 iw_destroy_cm_id(cm_id);
646 /* Call the client CM handler */
647 ret = cm_id->cm_handler(cm_id, iw_event);
649 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
650 destroy_cm_id(cm_id);
651 if (atomic_read(&cm_id_priv->refcount)==0)
656 if (iw_event->private_data_len)
657 kfree(iw_event->private_data);
661 * Passive Side: CM_ID <-- ESTABLISHED
663 * The provider generated an ESTABLISHED event which means that
664 * the MPA negotion has completed successfully and we are now in MPA
667 * This event can only be received in the CONN_RECV state. If the
668 * remote peer closed, the ESTABLISHED event would be received followed
669 * by the CLOSE event. If the app closes, it will block until we wake
670 * it up after processing this event.
672 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
673 struct iw_cm_event *iw_event)
678 spin_lock_irqsave(&cm_id_priv->lock, flags);
681 * We clear the CONNECT_WAIT bit here to allow the callback
682 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
683 * from a callback handler is not allowed.
685 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
686 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
687 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
688 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
689 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
690 wake_up_all(&cm_id_priv->connect_wait);
696 * Active Side: CM_ID <-- ESTABLISHED
698 * The app has called connect and is waiting for the established event to
699 * post it's requests to the server. This event will wake up anyone
700 * blocked in iw_cm_disconnect or iw_destroy_id.
702 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
703 struct iw_cm_event *iw_event)
708 spin_lock_irqsave(&cm_id_priv->lock, flags);
710 * Clear the connect wait bit so a callback function calling
711 * iw_cm_disconnect will not wait and deadlock this thread
713 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
714 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
715 if (iw_event->status == IW_CM_EVENT_STATUS_ACCEPTED) {
716 cm_id_priv->id.local_addr = iw_event->local_addr;
717 cm_id_priv->id.remote_addr = iw_event->remote_addr;
718 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
720 /* REJECTED or RESET */
721 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
722 cm_id_priv->qp = NULL;
723 cm_id_priv->state = IW_CM_STATE_IDLE;
725 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
726 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
728 if (iw_event->private_data_len)
729 kfree(iw_event->private_data);
731 /* Wake up waiters on connect complete */
732 wake_up_all(&cm_id_priv->connect_wait);
740 * If in the ESTABLISHED state, move to CLOSING.
742 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
743 struct iw_cm_event *iw_event)
747 spin_lock_irqsave(&cm_id_priv->lock, flags);
748 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
749 cm_id_priv->state = IW_CM_STATE_CLOSING;
750 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
756 * If in the ESTBLISHED or CLOSING states, the QP will have have been
757 * moved by the provider to the ERR state. Disassociate the CM_ID from
758 * the QP, move to IDLE, and remove the 'connected' reference.
760 * If in some other state, the cm_id was destroyed asynchronously.
761 * This is the last reference that will result in waking up
762 * the app thread blocked in iw_destroy_cm_id.
764 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
765 struct iw_cm_event *iw_event)
769 spin_lock_irqsave(&cm_id_priv->lock, flags);
771 if (cm_id_priv->qp) {
772 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
773 cm_id_priv->qp = NULL;
775 switch (cm_id_priv->state) {
776 case IW_CM_STATE_ESTABLISHED:
777 case IW_CM_STATE_CLOSING:
778 cm_id_priv->state = IW_CM_STATE_IDLE;
779 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
780 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
781 spin_lock_irqsave(&cm_id_priv->lock, flags);
783 case IW_CM_STATE_DESTROYING:
788 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
793 static int process_event(struct iwcm_id_private *cm_id_priv,
794 struct iw_cm_event *iw_event)
798 switch (iw_event->event) {
799 case IW_CM_EVENT_CONNECT_REQUEST:
800 cm_conn_req_handler(cm_id_priv, iw_event);
802 case IW_CM_EVENT_CONNECT_REPLY:
803 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
805 case IW_CM_EVENT_ESTABLISHED:
806 ret = cm_conn_est_handler(cm_id_priv, iw_event);
808 case IW_CM_EVENT_DISCONNECT:
809 cm_disconnect_handler(cm_id_priv, iw_event);
811 case IW_CM_EVENT_CLOSE:
812 ret = cm_close_handler(cm_id_priv, iw_event);
822 * Process events on the work_list for the cm_id. If the callback
823 * function requests that the cm_id be deleted, a flag is set in the
824 * cm_id flags to indicate that when the last reference is
825 * removed, the cm_id is to be destroyed. This is necessary to
826 * distinguish between an object that will be destroyed by the app
827 * thread asleep on the destroy_comp list vs. an object destroyed
828 * here synchronously when the last reference is removed.
830 static void cm_work_handler(void *arg)
832 struct iwcm_work *work = arg;
833 struct iw_cm_event levent;
834 struct iwcm_id_private *cm_id_priv = work->cm_id;
839 spin_lock_irqsave(&cm_id_priv->lock, flags);
840 empty = list_empty(&cm_id_priv->work_list);
842 work = list_entry(cm_id_priv->work_list.next,
843 struct iwcm_work, list);
844 list_del_init(&work->list);
845 empty = list_empty(&cm_id_priv->work_list);
846 levent = work->event;
848 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
850 ret = process_event(cm_id_priv, &levent);
852 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
853 destroy_cm_id(&cm_id_priv->id);
855 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
856 if (iwcm_deref_id(cm_id_priv))
859 if (atomic_read(&cm_id_priv->refcount)==0 &&
860 test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
861 dealloc_work_entries(cm_id_priv);
865 spin_lock_irqsave(&cm_id_priv->lock, flags);
867 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
871 * This function is called on interrupt context. Schedule events on
872 * the iwcm_wq thread to allow callback functions to downcall into
873 * the CM and/or block. Events are queued to a per-CM_ID
874 * work_list. If this is the first event on the work_list, the work
875 * element is also queued on the iwcm_wq thread.
877 * Each event holds a reference on the cm_id. Until the last posted
878 * event has been delivered and processed, the cm_id cannot be
882 * 0 - the event was handled.
883 * -ENOMEM - the event was not handled due to lack of resources.
885 static int cm_event_handler(struct iw_cm_id *cm_id,
886 struct iw_cm_event *iw_event)
888 struct iwcm_work *work;
889 struct iwcm_id_private *cm_id_priv;
893 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
895 spin_lock_irqsave(&cm_id_priv->lock, flags);
896 work = get_work(cm_id_priv);
902 INIT_WORK(&work->work, cm_work_handler, work);
903 work->cm_id = cm_id_priv;
904 work->event = *iw_event;
906 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
907 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
908 work->event.private_data_len) {
909 ret = copy_private_data(&work->event);
916 atomic_inc(&cm_id_priv->refcount);
917 if (list_empty(&cm_id_priv->work_list)) {
918 list_add_tail(&work->list, &cm_id_priv->work_list);
919 queue_work(iwcm_wq, &work->work);
921 list_add_tail(&work->list, &cm_id_priv->work_list);
923 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
927 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
928 struct ib_qp_attr *qp_attr,
934 spin_lock_irqsave(&cm_id_priv->lock, flags);
935 switch (cm_id_priv->state) {
936 case IW_CM_STATE_IDLE:
937 case IW_CM_STATE_CONN_SENT:
938 case IW_CM_STATE_CONN_RECV:
939 case IW_CM_STATE_ESTABLISHED:
940 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
941 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
942 IB_ACCESS_REMOTE_WRITE|
943 IB_ACCESS_REMOTE_READ;
950 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
954 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
955 struct ib_qp_attr *qp_attr,
961 spin_lock_irqsave(&cm_id_priv->lock, flags);
962 switch (cm_id_priv->state) {
963 case IW_CM_STATE_IDLE:
964 case IW_CM_STATE_CONN_SENT:
965 case IW_CM_STATE_CONN_RECV:
966 case IW_CM_STATE_ESTABLISHED:
974 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
978 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
979 struct ib_qp_attr *qp_attr,
982 struct iwcm_id_private *cm_id_priv;
985 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
986 switch (qp_attr->qp_state) {
989 ret = iwcm_init_qp_init_attr(cm_id_priv,
990 qp_attr, qp_attr_mask);
993 ret = iwcm_init_qp_rts_attr(cm_id_priv,
994 qp_attr, qp_attr_mask);
1002 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1004 static int __init iw_cm_init(void)
1006 iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
1013 static void __exit iw_cm_cleanup(void)
1015 destroy_workqueue(iwcm_wq);
1018 module_init(iw_cm_init);
1019 module_exit(iw_cm_cleanup);