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/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>
49 #include <rdma/iw_cm.h>
50 #include <rdma/ib_addr.h>
54 MODULE_AUTHOR("Tom Tucker");
55 MODULE_DESCRIPTION("iWARP CM");
56 MODULE_LICENSE("Dual BSD/GPL");
58 static struct workqueue_struct *iwcm_wq;
60 struct work_struct work;
61 struct iwcm_id_private *cm_id;
62 struct list_head list;
63 struct iw_cm_event event;
64 struct list_head free_list;
68 * The following services provide a mechanism for pre-allocating iwcm_work
69 * elements. The design pre-allocates them based on the cm_id type:
70 * LISTENING IDS: Get enough elements preallocated to handle the
72 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
73 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
75 * Allocating them in connect and listen avoids having to deal
76 * with allocation failures on the event upcall from the provider (which
77 * is called in the interrupt context).
79 * One exception is when creating the cm_id for incoming connection requests.
80 * There are two cases:
81 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
82 * the backlog is exceeded, then no more connection request events will
83 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
84 * to the provider to reject the connection request.
85 * 2) in the connection request workqueue handler, cm_conn_req_handler().
86 * If work elements cannot be allocated for the new connect request cm_id,
87 * then IWCM will call the provider reject method. This is ok since
88 * cm_conn_req_handler() runs in the workqueue thread context.
91 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
93 struct iwcm_work *work;
95 if (list_empty(&cm_id_priv->work_free_list))
97 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
99 list_del_init(&work->free_list);
103 static void put_work(struct iwcm_work *work)
105 list_add(&work->free_list, &work->cm_id->work_free_list);
108 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
110 struct list_head *e, *tmp;
112 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
113 kfree(list_entry(e, struct iwcm_work, free_list));
116 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
118 struct iwcm_work *work;
120 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
122 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
124 dealloc_work_entries(cm_id_priv);
127 work->cm_id = cm_id_priv;
128 INIT_LIST_HEAD(&work->list);
135 * Save private data from incoming connection requests to
136 * iw_cm_event, so the low level driver doesn't have to. Adjust
137 * the event ptr to point to the local copy.
139 static int copy_private_data(struct iw_cm_event *event)
143 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
146 event->private_data = p;
150 static void free_cm_id(struct iwcm_id_private *cm_id_priv)
152 dealloc_work_entries(cm_id_priv);
157 * Release a reference on cm_id. If the last reference is being
158 * released, enable the waiting thread (in iw_destroy_cm_id) to
159 * get woken up, and return 1 if a thread is already waiting.
161 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
163 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
164 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
165 BUG_ON(!list_empty(&cm_id_priv->work_list));
166 complete(&cm_id_priv->destroy_comp);
173 static void add_ref(struct iw_cm_id *cm_id)
175 struct iwcm_id_private *cm_id_priv;
176 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
177 atomic_inc(&cm_id_priv->refcount);
180 static void rem_ref(struct iw_cm_id *cm_id)
182 struct iwcm_id_private *cm_id_priv;
183 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
184 if (iwcm_deref_id(cm_id_priv) &&
185 test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
186 BUG_ON(!list_empty(&cm_id_priv->work_list));
187 free_cm_id(cm_id_priv);
191 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
193 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
194 iw_cm_handler cm_handler,
197 struct iwcm_id_private *cm_id_priv;
199 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
201 return ERR_PTR(-ENOMEM);
203 cm_id_priv->state = IW_CM_STATE_IDLE;
204 cm_id_priv->id.device = device;
205 cm_id_priv->id.cm_handler = cm_handler;
206 cm_id_priv->id.context = context;
207 cm_id_priv->id.event_handler = cm_event_handler;
208 cm_id_priv->id.add_ref = add_ref;
209 cm_id_priv->id.rem_ref = rem_ref;
210 spin_lock_init(&cm_id_priv->lock);
211 atomic_set(&cm_id_priv->refcount, 1);
212 init_waitqueue_head(&cm_id_priv->connect_wait);
213 init_completion(&cm_id_priv->destroy_comp);
214 INIT_LIST_HEAD(&cm_id_priv->work_list);
215 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
217 return &cm_id_priv->id;
219 EXPORT_SYMBOL(iw_create_cm_id);
222 static int iwcm_modify_qp_err(struct ib_qp *qp)
224 struct ib_qp_attr qp_attr;
229 qp_attr.qp_state = IB_QPS_ERR;
230 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
234 * This is really the RDMAC CLOSING state. It is most similar to the
237 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
239 struct ib_qp_attr qp_attr;
242 qp_attr.qp_state = IB_QPS_SQD;
243 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
249 * Block if a passive or active connection is currently being processed. Then
250 * process the event as follows:
251 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
252 * based on the abrupt flag
253 * - If the connection is already in the CLOSING or IDLE state, the peer is
254 * disconnecting concurrently with us and we've already seen the
255 * DISCONNECT event -- ignore the request and return 0
256 * - Disconnect on a listening endpoint returns -EINVAL
258 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
260 struct iwcm_id_private *cm_id_priv;
263 struct ib_qp *qp = NULL;
265 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
266 /* Wait if we're currently in a connect or accept downcall */
267 wait_event(cm_id_priv->connect_wait,
268 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
270 spin_lock_irqsave(&cm_id_priv->lock, flags);
271 switch (cm_id_priv->state) {
272 case IW_CM_STATE_ESTABLISHED:
273 cm_id_priv->state = IW_CM_STATE_CLOSING;
275 /* QP could be <nul> for user-mode client */
281 case IW_CM_STATE_LISTEN:
284 case IW_CM_STATE_CLOSING:
285 /* remote peer closed first */
286 case IW_CM_STATE_IDLE:
287 /* accept or connect returned !0 */
289 case IW_CM_STATE_CONN_RECV:
291 * App called disconnect before/without calling accept after
292 * connect_request event delivered.
295 case IW_CM_STATE_CONN_SENT:
296 /* Can only get here if wait above fails */
300 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
304 ret = iwcm_modify_qp_err(qp);
306 ret = iwcm_modify_qp_sqd(qp);
309 * If both sides are disconnecting the QP could
310 * already be in ERR or SQD states
317 EXPORT_SYMBOL(iw_cm_disconnect);
320 * CM_ID <-- DESTROYING
322 * Clean up all resources associated with the connection and release
323 * the initial reference taken by iw_create_cm_id.
325 static void destroy_cm_id(struct iw_cm_id *cm_id)
327 struct iwcm_id_private *cm_id_priv;
331 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
333 * Wait if we're currently in a connect or accept downcall. A
334 * listening endpoint should never block here.
336 wait_event(cm_id_priv->connect_wait,
337 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
339 spin_lock_irqsave(&cm_id_priv->lock, flags);
340 switch (cm_id_priv->state) {
341 case IW_CM_STATE_LISTEN:
342 cm_id_priv->state = IW_CM_STATE_DESTROYING;
343 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
344 /* destroy the listening endpoint */
345 ret = cm_id->device->iwcm->destroy_listen(cm_id);
346 spin_lock_irqsave(&cm_id_priv->lock, flags);
348 case IW_CM_STATE_ESTABLISHED:
349 cm_id_priv->state = IW_CM_STATE_DESTROYING;
350 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
351 /* Abrupt close of the connection */
352 (void)iwcm_modify_qp_err(cm_id_priv->qp);
353 spin_lock_irqsave(&cm_id_priv->lock, flags);
355 case IW_CM_STATE_IDLE:
356 case IW_CM_STATE_CLOSING:
357 cm_id_priv->state = IW_CM_STATE_DESTROYING;
359 case IW_CM_STATE_CONN_RECV:
361 * App called destroy before/without calling accept after
362 * receiving connection request event notification or
363 * returned non zero from the event callback function.
364 * In either case, must tell the provider to reject.
366 cm_id_priv->state = IW_CM_STATE_DESTROYING;
367 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
368 cm_id->device->iwcm->reject(cm_id, NULL, 0);
369 spin_lock_irqsave(&cm_id_priv->lock, flags);
371 case IW_CM_STATE_CONN_SENT:
372 case IW_CM_STATE_DESTROYING:
377 if (cm_id_priv->qp) {
378 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
379 cm_id_priv->qp = NULL;
381 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
383 (void)iwcm_deref_id(cm_id_priv);
387 * This function is only called by the application thread and cannot
388 * be called by the event thread. The function will wait for all
389 * references to be released on the cm_id and then kfree the cm_id
392 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
394 struct iwcm_id_private *cm_id_priv;
396 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
397 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
399 destroy_cm_id(cm_id);
401 wait_for_completion(&cm_id_priv->destroy_comp);
403 free_cm_id(cm_id_priv);
405 EXPORT_SYMBOL(iw_destroy_cm_id);
410 * Start listening for connect requests. Generates one CONNECT_REQUEST
411 * event for each inbound connect request.
413 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
415 struct iwcm_id_private *cm_id_priv;
419 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
421 ret = alloc_work_entries(cm_id_priv, backlog);
425 spin_lock_irqsave(&cm_id_priv->lock, flags);
426 switch (cm_id_priv->state) {
427 case IW_CM_STATE_IDLE:
428 cm_id_priv->state = IW_CM_STATE_LISTEN;
429 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
430 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
432 cm_id_priv->state = IW_CM_STATE_IDLE;
433 spin_lock_irqsave(&cm_id_priv->lock, flags);
438 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
442 EXPORT_SYMBOL(iw_cm_listen);
447 * Rejects an inbound connection request. No events are generated.
449 int iw_cm_reject(struct iw_cm_id *cm_id,
450 const void *private_data,
453 struct iwcm_id_private *cm_id_priv;
457 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
458 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
460 spin_lock_irqsave(&cm_id_priv->lock, flags);
461 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
462 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
463 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
464 wake_up_all(&cm_id_priv->connect_wait);
467 cm_id_priv->state = IW_CM_STATE_IDLE;
468 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
470 ret = cm_id->device->iwcm->reject(cm_id, private_data,
473 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
474 wake_up_all(&cm_id_priv->connect_wait);
478 EXPORT_SYMBOL(iw_cm_reject);
481 * CM_ID <-- ESTABLISHED
483 * Accepts an inbound connection request and generates an ESTABLISHED
484 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
485 * until the ESTABLISHED event is received from the provider.
487 int iw_cm_accept(struct iw_cm_id *cm_id,
488 struct iw_cm_conn_param *iw_param)
490 struct iwcm_id_private *cm_id_priv;
495 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
496 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
498 spin_lock_irqsave(&cm_id_priv->lock, flags);
499 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
500 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
501 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
502 wake_up_all(&cm_id_priv->connect_wait);
505 /* Get the ib_qp given the QPN */
506 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
508 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
509 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
510 wake_up_all(&cm_id_priv->connect_wait);
513 cm_id->device->iwcm->add_ref(qp);
515 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
517 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
519 /* An error on accept precludes provider events */
520 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
521 cm_id_priv->state = IW_CM_STATE_IDLE;
522 spin_lock_irqsave(&cm_id_priv->lock, flags);
523 if (cm_id_priv->qp) {
524 cm_id->device->iwcm->rem_ref(qp);
525 cm_id_priv->qp = NULL;
527 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
528 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
529 wake_up_all(&cm_id_priv->connect_wait);
534 EXPORT_SYMBOL(iw_cm_accept);
537 * Active Side: CM_ID <-- CONN_SENT
539 * If successful, results in the generation of a CONNECT_REPLY
540 * event. iw_cm_disconnect and iw_cm_destroy will block until the
541 * CONNECT_REPLY event is received from the provider.
543 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
545 struct iwcm_id_private *cm_id_priv;
550 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
552 ret = alloc_work_entries(cm_id_priv, 4);
556 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
557 spin_lock_irqsave(&cm_id_priv->lock, flags);
559 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
560 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
561 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
562 wake_up_all(&cm_id_priv->connect_wait);
566 /* Get the ib_qp given the QPN */
567 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
569 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
570 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
571 wake_up_all(&cm_id_priv->connect_wait);
574 cm_id->device->iwcm->add_ref(qp);
576 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
577 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
579 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
581 spin_lock_irqsave(&cm_id_priv->lock, flags);
582 if (cm_id_priv->qp) {
583 cm_id->device->iwcm->rem_ref(qp);
584 cm_id_priv->qp = NULL;
586 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
587 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
588 cm_id_priv->state = IW_CM_STATE_IDLE;
589 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
590 wake_up_all(&cm_id_priv->connect_wait);
595 EXPORT_SYMBOL(iw_cm_connect);
598 * Passive Side: new CM_ID <-- CONN_RECV
600 * Handles an inbound connect request. The function creates a new
601 * iw_cm_id to represent the new connection and inherits the client
602 * callback function and other attributes from the listening parent.
604 * The work item contains a pointer to the listen_cm_id and the event. The
605 * listen_cm_id contains the client cm_handler, context and
606 * device. These are copied when the device is cloned. The event
607 * contains the new four tuple.
609 * An error on the child should not affect the parent, so this
610 * function does not return a value.
612 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
613 struct iw_cm_event *iw_event)
616 struct iw_cm_id *cm_id;
617 struct iwcm_id_private *cm_id_priv;
621 * The provider should never generate a connection request
622 * event with a bad status.
624 BUG_ON(iw_event->status);
627 * We could be destroying the listening id. If so, ignore this
630 spin_lock_irqsave(&listen_id_priv->lock, flags);
631 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
632 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
635 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
637 cm_id = iw_create_cm_id(listen_id_priv->id.device,
638 listen_id_priv->id.cm_handler,
639 listen_id_priv->id.context);
640 /* If the cm_id could not be created, ignore the request */
644 cm_id->provider_data = iw_event->provider_data;
645 cm_id->local_addr = iw_event->local_addr;
646 cm_id->remote_addr = iw_event->remote_addr;
648 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
649 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
651 ret = alloc_work_entries(cm_id_priv, 3);
653 iw_cm_reject(cm_id, NULL, 0);
654 iw_destroy_cm_id(cm_id);
658 /* Call the client CM handler */
659 ret = cm_id->cm_handler(cm_id, iw_event);
661 iw_cm_reject(cm_id, NULL, 0);
662 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
663 destroy_cm_id(cm_id);
664 if (atomic_read(&cm_id_priv->refcount)==0)
665 free_cm_id(cm_id_priv);
669 if (iw_event->private_data_len)
670 kfree(iw_event->private_data);
674 * Passive Side: CM_ID <-- ESTABLISHED
676 * The provider generated an ESTABLISHED event which means that
677 * the MPA negotion has completed successfully and we are now in MPA
680 * This event can only be received in the CONN_RECV state. If the
681 * remote peer closed, the ESTABLISHED event would be received followed
682 * by the CLOSE event. If the app closes, it will block until we wake
683 * it up after processing this event.
685 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
686 struct iw_cm_event *iw_event)
691 spin_lock_irqsave(&cm_id_priv->lock, flags);
694 * We clear the CONNECT_WAIT bit here to allow the callback
695 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
696 * from a callback handler is not allowed.
698 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
699 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
700 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
701 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
702 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
703 wake_up_all(&cm_id_priv->connect_wait);
709 * Active Side: CM_ID <-- ESTABLISHED
711 * The app has called connect and is waiting for the established event to
712 * post it's requests to the server. This event will wake up anyone
713 * blocked in iw_cm_disconnect or iw_destroy_id.
715 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
716 struct iw_cm_event *iw_event)
721 spin_lock_irqsave(&cm_id_priv->lock, flags);
723 * Clear the connect wait bit so a callback function calling
724 * iw_cm_disconnect will not wait and deadlock this thread
726 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
727 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
728 if (iw_event->status == IW_CM_EVENT_STATUS_ACCEPTED) {
729 cm_id_priv->id.local_addr = iw_event->local_addr;
730 cm_id_priv->id.remote_addr = iw_event->remote_addr;
731 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
733 /* REJECTED or RESET */
734 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
735 cm_id_priv->qp = NULL;
736 cm_id_priv->state = IW_CM_STATE_IDLE;
738 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
739 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
741 if (iw_event->private_data_len)
742 kfree(iw_event->private_data);
744 /* Wake up waiters on connect complete */
745 wake_up_all(&cm_id_priv->connect_wait);
753 * If in the ESTABLISHED state, move to CLOSING.
755 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
756 struct iw_cm_event *iw_event)
760 spin_lock_irqsave(&cm_id_priv->lock, flags);
761 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
762 cm_id_priv->state = IW_CM_STATE_CLOSING;
763 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
769 * If in the ESTBLISHED or CLOSING states, the QP will have have been
770 * moved by the provider to the ERR state. Disassociate the CM_ID from
771 * the QP, move to IDLE, and remove the 'connected' reference.
773 * If in some other state, the cm_id was destroyed asynchronously.
774 * This is the last reference that will result in waking up
775 * the app thread blocked in iw_destroy_cm_id.
777 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
778 struct iw_cm_event *iw_event)
782 spin_lock_irqsave(&cm_id_priv->lock, flags);
784 if (cm_id_priv->qp) {
785 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
786 cm_id_priv->qp = NULL;
788 switch (cm_id_priv->state) {
789 case IW_CM_STATE_ESTABLISHED:
790 case IW_CM_STATE_CLOSING:
791 cm_id_priv->state = IW_CM_STATE_IDLE;
792 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
793 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
794 spin_lock_irqsave(&cm_id_priv->lock, flags);
796 case IW_CM_STATE_DESTROYING:
801 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
806 static int process_event(struct iwcm_id_private *cm_id_priv,
807 struct iw_cm_event *iw_event)
811 switch (iw_event->event) {
812 case IW_CM_EVENT_CONNECT_REQUEST:
813 cm_conn_req_handler(cm_id_priv, iw_event);
815 case IW_CM_EVENT_CONNECT_REPLY:
816 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
818 case IW_CM_EVENT_ESTABLISHED:
819 ret = cm_conn_est_handler(cm_id_priv, iw_event);
821 case IW_CM_EVENT_DISCONNECT:
822 cm_disconnect_handler(cm_id_priv, iw_event);
824 case IW_CM_EVENT_CLOSE:
825 ret = cm_close_handler(cm_id_priv, iw_event);
835 * Process events on the work_list for the cm_id. If the callback
836 * function requests that the cm_id be deleted, a flag is set in the
837 * cm_id flags to indicate that when the last reference is
838 * removed, the cm_id is to be destroyed. This is necessary to
839 * distinguish between an object that will be destroyed by the app
840 * thread asleep on the destroy_comp list vs. an object destroyed
841 * here synchronously when the last reference is removed.
843 static void cm_work_handler(struct work_struct *_work)
845 struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
846 struct iw_cm_event levent;
847 struct iwcm_id_private *cm_id_priv = work->cm_id;
853 spin_lock_irqsave(&cm_id_priv->lock, flags);
854 empty = list_empty(&cm_id_priv->work_list);
856 work = list_entry(cm_id_priv->work_list.next,
857 struct iwcm_work, list);
858 list_del_init(&work->list);
859 empty = list_empty(&cm_id_priv->work_list);
860 levent = work->event;
862 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
864 ret = process_event(cm_id_priv, &levent);
866 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
867 destroy_cm_id(&cm_id_priv->id);
869 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
870 destroy_id = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
871 if (iwcm_deref_id(cm_id_priv)) {
873 BUG_ON(!list_empty(&cm_id_priv->work_list));
874 free_cm_id(cm_id_priv);
878 spin_lock_irqsave(&cm_id_priv->lock, flags);
880 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
884 * This function is called on interrupt context. Schedule events on
885 * the iwcm_wq thread to allow callback functions to downcall into
886 * the CM and/or block. Events are queued to a per-CM_ID
887 * work_list. If this is the first event on the work_list, the work
888 * element is also queued on the iwcm_wq thread.
890 * Each event holds a reference on the cm_id. Until the last posted
891 * event has been delivered and processed, the cm_id cannot be
895 * 0 - the event was handled.
896 * -ENOMEM - the event was not handled due to lack of resources.
898 static int cm_event_handler(struct iw_cm_id *cm_id,
899 struct iw_cm_event *iw_event)
901 struct iwcm_work *work;
902 struct iwcm_id_private *cm_id_priv;
906 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
908 spin_lock_irqsave(&cm_id_priv->lock, flags);
909 work = get_work(cm_id_priv);
915 INIT_WORK(&work->work, cm_work_handler);
916 work->cm_id = cm_id_priv;
917 work->event = *iw_event;
919 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
920 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
921 work->event.private_data_len) {
922 ret = copy_private_data(&work->event);
929 atomic_inc(&cm_id_priv->refcount);
930 if (list_empty(&cm_id_priv->work_list)) {
931 list_add_tail(&work->list, &cm_id_priv->work_list);
932 queue_work(iwcm_wq, &work->work);
934 list_add_tail(&work->list, &cm_id_priv->work_list);
936 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
940 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
941 struct ib_qp_attr *qp_attr,
947 spin_lock_irqsave(&cm_id_priv->lock, flags);
948 switch (cm_id_priv->state) {
949 case IW_CM_STATE_IDLE:
950 case IW_CM_STATE_CONN_SENT:
951 case IW_CM_STATE_CONN_RECV:
952 case IW_CM_STATE_ESTABLISHED:
953 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
954 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
955 IB_ACCESS_REMOTE_READ;
962 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
966 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
967 struct ib_qp_attr *qp_attr,
973 spin_lock_irqsave(&cm_id_priv->lock, flags);
974 switch (cm_id_priv->state) {
975 case IW_CM_STATE_IDLE:
976 case IW_CM_STATE_CONN_SENT:
977 case IW_CM_STATE_CONN_RECV:
978 case IW_CM_STATE_ESTABLISHED:
986 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
990 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
991 struct ib_qp_attr *qp_attr,
994 struct iwcm_id_private *cm_id_priv;
997 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
998 switch (qp_attr->qp_state) {
1001 ret = iwcm_init_qp_init_attr(cm_id_priv,
1002 qp_attr, qp_attr_mask);
1005 ret = iwcm_init_qp_rts_attr(cm_id_priv,
1006 qp_attr, qp_attr_mask);
1014 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1016 static int __init iw_cm_init(void)
1018 iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
1025 static void __exit iw_cm_cleanup(void)
1027 destroy_workqueue(iwcm_wq);
1030 module_init(iw_cm_init);
1031 module_exit(iw_cm_cleanup);