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>
48 #include <linux/module.h>
49 #include <linux/sysctl.h>
51 #include <rdma/iw_cm.h>
52 #include <rdma/ib_addr.h>
53 #include <rdma/iw_portmap.h>
54 #include <rdma/rdma_netlink.h>
58 MODULE_AUTHOR("Tom Tucker");
59 MODULE_DESCRIPTION("iWARP CM");
60 MODULE_LICENSE("Dual BSD/GPL");
62 static struct ibnl_client_cbs iwcm_nl_cb_table[] = {
63 [RDMA_NL_IWPM_REG_PID] = {.dump = iwpm_register_pid_cb},
64 [RDMA_NL_IWPM_ADD_MAPPING] = {.dump = iwpm_add_mapping_cb},
65 [RDMA_NL_IWPM_QUERY_MAPPING] = {.dump = iwpm_add_and_query_mapping_cb},
66 [RDMA_NL_IWPM_REMOTE_INFO] = {.dump = iwpm_remote_info_cb},
67 [RDMA_NL_IWPM_HANDLE_ERR] = {.dump = iwpm_mapping_error_cb},
68 [RDMA_NL_IWPM_MAPINFO] = {.dump = iwpm_mapping_info_cb},
69 [RDMA_NL_IWPM_MAPINFO_NUM] = {.dump = iwpm_ack_mapping_info_cb}
72 static struct workqueue_struct *iwcm_wq;
74 struct work_struct work;
75 struct iwcm_id_private *cm_id;
76 struct list_head list;
77 struct iw_cm_event event;
78 struct list_head free_list;
81 static unsigned int default_backlog = 256;
83 static struct ctl_table_header *iwcm_ctl_table_hdr;
84 static struct ctl_table iwcm_ctl_table[] = {
86 .procname = "default_backlog",
87 .data = &default_backlog,
88 .maxlen = sizeof(default_backlog),
90 .proc_handler = proc_dointvec,
96 * The following services provide a mechanism for pre-allocating iwcm_work
97 * elements. The design pre-allocates them based on the cm_id type:
98 * LISTENING IDS: Get enough elements preallocated to handle the
100 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
101 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
103 * Allocating them in connect and listen avoids having to deal
104 * with allocation failures on the event upcall from the provider (which
105 * is called in the interrupt context).
107 * One exception is when creating the cm_id for incoming connection requests.
108 * There are two cases:
109 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
110 * the backlog is exceeded, then no more connection request events will
111 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
112 * to the provider to reject the connection request.
113 * 2) in the connection request workqueue handler, cm_conn_req_handler().
114 * If work elements cannot be allocated for the new connect request cm_id,
115 * then IWCM will call the provider reject method. This is ok since
116 * cm_conn_req_handler() runs in the workqueue thread context.
119 static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
121 struct iwcm_work *work;
123 if (list_empty(&cm_id_priv->work_free_list))
125 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
127 list_del_init(&work->free_list);
131 static void put_work(struct iwcm_work *work)
133 list_add(&work->free_list, &work->cm_id->work_free_list);
136 static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
138 struct list_head *e, *tmp;
140 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list) {
142 kfree(list_entry(e, struct iwcm_work, free_list));
146 static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
148 struct iwcm_work *work;
150 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
152 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
154 dealloc_work_entries(cm_id_priv);
157 work->cm_id = cm_id_priv;
158 INIT_LIST_HEAD(&work->list);
165 * Save private data from incoming connection requests to
166 * iw_cm_event, so the low level driver doesn't have to. Adjust
167 * the event ptr to point to the local copy.
169 static int copy_private_data(struct iw_cm_event *event)
173 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
176 event->private_data = p;
180 static void free_cm_id(struct iwcm_id_private *cm_id_priv)
182 dealloc_work_entries(cm_id_priv);
187 * Release a reference on cm_id. If the last reference is being
188 * released, free the cm_id and return 1.
190 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
192 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
193 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
194 BUG_ON(!list_empty(&cm_id_priv->work_list));
195 free_cm_id(cm_id_priv);
202 static void add_ref(struct iw_cm_id *cm_id)
204 struct iwcm_id_private *cm_id_priv;
205 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
206 atomic_inc(&cm_id_priv->refcount);
209 static void rem_ref(struct iw_cm_id *cm_id)
211 struct iwcm_id_private *cm_id_priv;
213 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
215 (void)iwcm_deref_id(cm_id_priv);
218 static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
220 struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
221 iw_cm_handler cm_handler,
224 struct iwcm_id_private *cm_id_priv;
226 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
230 cm_id_priv->state = IW_CM_STATE_IDLE;
231 cm_id_priv->id.device = device;
232 cm_id_priv->id.cm_handler = cm_handler;
233 cm_id_priv->id.context = context;
234 cm_id_priv->id.event_handler = cm_event_handler;
235 cm_id_priv->id.add_ref = add_ref;
236 cm_id_priv->id.rem_ref = rem_ref;
237 spin_lock_init(&cm_id_priv->lock);
238 atomic_set(&cm_id_priv->refcount, 1);
239 init_waitqueue_head(&cm_id_priv->connect_wait);
240 init_completion(&cm_id_priv->destroy_comp);
241 INIT_LIST_HEAD(&cm_id_priv->work_list);
242 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
244 return &cm_id_priv->id;
246 EXPORT_SYMBOL(iw_create_cm_id);
249 static int iwcm_modify_qp_err(struct ib_qp *qp)
251 struct ib_qp_attr qp_attr;
256 qp_attr.qp_state = IB_QPS_ERR;
257 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
261 * This is really the RDMAC CLOSING state. It is most similar to the
264 static int iwcm_modify_qp_sqd(struct ib_qp *qp)
266 struct ib_qp_attr qp_attr;
269 qp_attr.qp_state = IB_QPS_SQD;
270 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
276 * Block if a passive or active connection is currently being processed. Then
277 * process the event as follows:
278 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
279 * based on the abrupt flag
280 * - If the connection is already in the CLOSING or IDLE state, the peer is
281 * disconnecting concurrently with us and we've already seen the
282 * DISCONNECT event -- ignore the request and return 0
283 * - Disconnect on a listening endpoint returns -EINVAL
285 int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
287 struct iwcm_id_private *cm_id_priv;
290 struct ib_qp *qp = NULL;
292 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
293 /* Wait if we're currently in a connect or accept downcall */
294 wait_event(cm_id_priv->connect_wait,
295 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
297 spin_lock_irqsave(&cm_id_priv->lock, flags);
298 switch (cm_id_priv->state) {
299 case IW_CM_STATE_ESTABLISHED:
300 cm_id_priv->state = IW_CM_STATE_CLOSING;
302 /* QP could be <nul> for user-mode client */
308 case IW_CM_STATE_LISTEN:
311 case IW_CM_STATE_CLOSING:
312 /* remote peer closed first */
313 case IW_CM_STATE_IDLE:
314 /* accept or connect returned !0 */
316 case IW_CM_STATE_CONN_RECV:
318 * App called disconnect before/without calling accept after
319 * connect_request event delivered.
322 case IW_CM_STATE_CONN_SENT:
323 /* Can only get here if wait above fails */
327 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
331 ret = iwcm_modify_qp_err(qp);
333 ret = iwcm_modify_qp_sqd(qp);
336 * If both sides are disconnecting the QP could
337 * already be in ERR or SQD states
344 EXPORT_SYMBOL(iw_cm_disconnect);
347 * CM_ID <-- DESTROYING
349 * Clean up all resources associated with the connection and release
350 * the initial reference taken by iw_create_cm_id.
352 static void destroy_cm_id(struct iw_cm_id *cm_id)
354 struct iwcm_id_private *cm_id_priv;
357 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
359 * Wait if we're currently in a connect or accept downcall. A
360 * listening endpoint should never block here.
362 wait_event(cm_id_priv->connect_wait,
363 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
366 * Since we're deleting the cm_id, drop any events that
367 * might arrive before the last dereference.
369 set_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags);
371 spin_lock_irqsave(&cm_id_priv->lock, flags);
372 switch (cm_id_priv->state) {
373 case IW_CM_STATE_LISTEN:
374 cm_id_priv->state = IW_CM_STATE_DESTROYING;
375 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
376 /* destroy the listening endpoint */
377 cm_id->device->iwcm->destroy_listen(cm_id);
378 spin_lock_irqsave(&cm_id_priv->lock, flags);
380 case IW_CM_STATE_ESTABLISHED:
381 cm_id_priv->state = IW_CM_STATE_DESTROYING;
382 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
383 /* Abrupt close of the connection */
384 (void)iwcm_modify_qp_err(cm_id_priv->qp);
385 spin_lock_irqsave(&cm_id_priv->lock, flags);
387 case IW_CM_STATE_IDLE:
388 case IW_CM_STATE_CLOSING:
389 cm_id_priv->state = IW_CM_STATE_DESTROYING;
391 case IW_CM_STATE_CONN_RECV:
393 * App called destroy before/without calling accept after
394 * receiving connection request event notification or
395 * returned non zero from the event callback function.
396 * In either case, must tell the provider to reject.
398 cm_id_priv->state = IW_CM_STATE_DESTROYING;
399 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
400 cm_id->device->iwcm->reject(cm_id, NULL, 0);
401 spin_lock_irqsave(&cm_id_priv->lock, flags);
403 case IW_CM_STATE_CONN_SENT:
404 case IW_CM_STATE_DESTROYING:
409 if (cm_id_priv->qp) {
410 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
411 cm_id_priv->qp = NULL;
413 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
416 iwpm_remove_mapinfo(&cm_id->local_addr, &cm_id->m_local_addr);
417 iwpm_remove_mapping(&cm_id->local_addr, RDMA_NL_IWCM);
420 (void)iwcm_deref_id(cm_id_priv);
424 * This function is only called by the application thread and cannot
425 * be called by the event thread. The function will wait for all
426 * references to be released on the cm_id and then kfree the cm_id
429 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
431 struct iwcm_id_private *cm_id_priv;
433 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
434 destroy_cm_id(cm_id);
436 EXPORT_SYMBOL(iw_destroy_cm_id);
439 * iw_cm_check_wildcard - If IP address is 0 then use original
440 * @pm_addr: sockaddr containing the ip to check for wildcard
441 * @cm_addr: sockaddr containing the actual IP address
442 * @cm_outaddr: sockaddr to set IP addr which leaving port
444 * Checks the pm_addr for wildcard and then sets cm_outaddr's
445 * IP to the actual (cm_addr).
447 static void iw_cm_check_wildcard(struct sockaddr_storage *pm_addr,
448 struct sockaddr_storage *cm_addr,
449 struct sockaddr_storage *cm_outaddr)
451 if (pm_addr->ss_family == AF_INET) {
452 struct sockaddr_in *pm4_addr = (struct sockaddr_in *)pm_addr;
454 if (pm4_addr->sin_addr.s_addr == htonl(INADDR_ANY)) {
455 struct sockaddr_in *cm4_addr =
456 (struct sockaddr_in *)cm_addr;
457 struct sockaddr_in *cm4_outaddr =
458 (struct sockaddr_in *)cm_outaddr;
460 cm4_outaddr->sin_addr = cm4_addr->sin_addr;
463 struct sockaddr_in6 *pm6_addr = (struct sockaddr_in6 *)pm_addr;
465 if (ipv6_addr_type(&pm6_addr->sin6_addr) == IPV6_ADDR_ANY) {
466 struct sockaddr_in6 *cm6_addr =
467 (struct sockaddr_in6 *)cm_addr;
468 struct sockaddr_in6 *cm6_outaddr =
469 (struct sockaddr_in6 *)cm_outaddr;
471 cm6_outaddr->sin6_addr = cm6_addr->sin6_addr;
477 * iw_cm_map - Use portmapper to map the ports
478 * @cm_id: connection manager pointer
479 * @active: Indicates the active side when true
480 * returns nonzero for error only if iwpm_create_mapinfo() fails
482 * Tries to add a mapping for a port using the Portmapper. If
483 * successful in mapping the IP/Port it will check the remote
484 * mapped IP address for a wildcard IP address and replace the
485 * zero IP address with the remote_addr.
487 static int iw_cm_map(struct iw_cm_id *cm_id, bool active)
489 struct iwpm_dev_data pm_reg_msg;
490 struct iwpm_sa_data pm_msg;
493 cm_id->m_local_addr = cm_id->local_addr;
494 cm_id->m_remote_addr = cm_id->remote_addr;
496 memcpy(pm_reg_msg.dev_name, cm_id->device->name,
497 sizeof(pm_reg_msg.dev_name));
498 memcpy(pm_reg_msg.if_name, cm_id->device->iwcm->ifname,
499 sizeof(pm_reg_msg.if_name));
501 if (iwpm_register_pid(&pm_reg_msg, RDMA_NL_IWCM) ||
505 cm_id->mapped = true;
506 pm_msg.loc_addr = cm_id->local_addr;
507 pm_msg.rem_addr = cm_id->remote_addr;
509 status = iwpm_add_and_query_mapping(&pm_msg,
512 status = iwpm_add_mapping(&pm_msg, RDMA_NL_IWCM);
515 cm_id->m_local_addr = pm_msg.mapped_loc_addr;
517 cm_id->m_remote_addr = pm_msg.mapped_rem_addr;
518 iw_cm_check_wildcard(&pm_msg.mapped_rem_addr,
520 &cm_id->m_remote_addr);
524 return iwpm_create_mapinfo(&cm_id->local_addr,
525 &cm_id->m_local_addr,
532 * Start listening for connect requests. Generates one CONNECT_REQUEST
533 * event for each inbound connect request.
535 int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
537 struct iwcm_id_private *cm_id_priv;
541 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
544 backlog = default_backlog;
546 ret = alloc_work_entries(cm_id_priv, backlog);
550 spin_lock_irqsave(&cm_id_priv->lock, flags);
551 switch (cm_id_priv->state) {
552 case IW_CM_STATE_IDLE:
553 cm_id_priv->state = IW_CM_STATE_LISTEN;
554 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
555 ret = iw_cm_map(cm_id, false);
557 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
559 cm_id_priv->state = IW_CM_STATE_IDLE;
560 spin_lock_irqsave(&cm_id_priv->lock, flags);
565 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
569 EXPORT_SYMBOL(iw_cm_listen);
574 * Rejects an inbound connection request. No events are generated.
576 int iw_cm_reject(struct iw_cm_id *cm_id,
577 const void *private_data,
580 struct iwcm_id_private *cm_id_priv;
584 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
585 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
587 spin_lock_irqsave(&cm_id_priv->lock, flags);
588 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
589 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
590 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
591 wake_up_all(&cm_id_priv->connect_wait);
594 cm_id_priv->state = IW_CM_STATE_IDLE;
595 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
597 ret = cm_id->device->iwcm->reject(cm_id, private_data,
600 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
601 wake_up_all(&cm_id_priv->connect_wait);
605 EXPORT_SYMBOL(iw_cm_reject);
608 * CM_ID <-- ESTABLISHED
610 * Accepts an inbound connection request and generates an ESTABLISHED
611 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
612 * until the ESTABLISHED event is received from the provider.
614 int iw_cm_accept(struct iw_cm_id *cm_id,
615 struct iw_cm_conn_param *iw_param)
617 struct iwcm_id_private *cm_id_priv;
622 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
623 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
625 spin_lock_irqsave(&cm_id_priv->lock, flags);
626 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
627 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
628 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
629 wake_up_all(&cm_id_priv->connect_wait);
632 /* Get the ib_qp given the QPN */
633 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
635 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
636 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
637 wake_up_all(&cm_id_priv->connect_wait);
640 cm_id->device->iwcm->add_ref(qp);
642 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
644 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
646 /* An error on accept precludes provider events */
647 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
648 cm_id_priv->state = IW_CM_STATE_IDLE;
649 spin_lock_irqsave(&cm_id_priv->lock, flags);
650 if (cm_id_priv->qp) {
651 cm_id->device->iwcm->rem_ref(qp);
652 cm_id_priv->qp = NULL;
654 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
655 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
656 wake_up_all(&cm_id_priv->connect_wait);
661 EXPORT_SYMBOL(iw_cm_accept);
664 * Active Side: CM_ID <-- CONN_SENT
666 * If successful, results in the generation of a CONNECT_REPLY
667 * event. iw_cm_disconnect and iw_cm_destroy will block until the
668 * CONNECT_REPLY event is received from the provider.
670 int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
672 struct iwcm_id_private *cm_id_priv;
677 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
679 ret = alloc_work_entries(cm_id_priv, 4);
683 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
684 spin_lock_irqsave(&cm_id_priv->lock, flags);
686 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
691 /* Get the ib_qp given the QPN */
692 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
697 cm_id->device->iwcm->add_ref(qp);
699 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
700 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
702 ret = iw_cm_map(cm_id, true);
704 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
706 return 0; /* success */
708 spin_lock_irqsave(&cm_id_priv->lock, flags);
709 if (cm_id_priv->qp) {
710 cm_id->device->iwcm->rem_ref(qp);
711 cm_id_priv->qp = NULL;
713 cm_id_priv->state = IW_CM_STATE_IDLE;
715 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
716 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
717 wake_up_all(&cm_id_priv->connect_wait);
720 EXPORT_SYMBOL(iw_cm_connect);
723 * Passive Side: new CM_ID <-- CONN_RECV
725 * Handles an inbound connect request. The function creates a new
726 * iw_cm_id to represent the new connection and inherits the client
727 * callback function and other attributes from the listening parent.
729 * The work item contains a pointer to the listen_cm_id and the event. The
730 * listen_cm_id contains the client cm_handler, context and
731 * device. These are copied when the device is cloned. The event
732 * contains the new four tuple.
734 * An error on the child should not affect the parent, so this
735 * function does not return a value.
737 static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
738 struct iw_cm_event *iw_event)
741 struct iw_cm_id *cm_id;
742 struct iwcm_id_private *cm_id_priv;
746 * The provider should never generate a connection request
747 * event with a bad status.
749 BUG_ON(iw_event->status);
751 cm_id = iw_create_cm_id(listen_id_priv->id.device,
752 listen_id_priv->id.cm_handler,
753 listen_id_priv->id.context);
754 /* If the cm_id could not be created, ignore the request */
758 cm_id->provider_data = iw_event->provider_data;
759 cm_id->m_local_addr = iw_event->local_addr;
760 cm_id->m_remote_addr = iw_event->remote_addr;
761 cm_id->local_addr = listen_id_priv->id.local_addr;
763 ret = iwpm_get_remote_info(&listen_id_priv->id.m_local_addr,
764 &iw_event->remote_addr,
768 cm_id->remote_addr = iw_event->remote_addr;
770 iw_cm_check_wildcard(&listen_id_priv->id.m_local_addr,
771 &iw_event->local_addr,
773 iw_event->local_addr = cm_id->local_addr;
774 iw_event->remote_addr = cm_id->remote_addr;
777 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
778 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
781 * We could be destroying the listening id. If so, ignore this
784 spin_lock_irqsave(&listen_id_priv->lock, flags);
785 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
786 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
787 iw_cm_reject(cm_id, NULL, 0);
788 iw_destroy_cm_id(cm_id);
791 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
793 ret = alloc_work_entries(cm_id_priv, 3);
795 iw_cm_reject(cm_id, NULL, 0);
796 iw_destroy_cm_id(cm_id);
800 /* Call the client CM handler */
801 ret = cm_id->cm_handler(cm_id, iw_event);
803 iw_cm_reject(cm_id, NULL, 0);
804 iw_destroy_cm_id(cm_id);
808 if (iw_event->private_data_len)
809 kfree(iw_event->private_data);
813 * Passive Side: CM_ID <-- ESTABLISHED
815 * The provider generated an ESTABLISHED event which means that
816 * the MPA negotion has completed successfully and we are now in MPA
819 * This event can only be received in the CONN_RECV state. If the
820 * remote peer closed, the ESTABLISHED event would be received followed
821 * by the CLOSE event. If the app closes, it will block until we wake
822 * it up after processing this event.
824 static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
825 struct iw_cm_event *iw_event)
830 spin_lock_irqsave(&cm_id_priv->lock, flags);
833 * We clear the CONNECT_WAIT bit here to allow the callback
834 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
835 * from a callback handler is not allowed.
837 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
838 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
839 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
840 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
841 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
842 wake_up_all(&cm_id_priv->connect_wait);
848 * Active Side: CM_ID <-- ESTABLISHED
850 * The app has called connect and is waiting for the established event to
851 * post it's requests to the server. This event will wake up anyone
852 * blocked in iw_cm_disconnect or iw_destroy_id.
854 static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
855 struct iw_cm_event *iw_event)
860 spin_lock_irqsave(&cm_id_priv->lock, flags);
862 * Clear the connect wait bit so a callback function calling
863 * iw_cm_disconnect will not wait and deadlock this thread
865 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
866 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
867 if (iw_event->status == 0) {
868 cm_id_priv->id.m_local_addr = iw_event->local_addr;
869 cm_id_priv->id.m_remote_addr = iw_event->remote_addr;
870 iw_event->local_addr = cm_id_priv->id.local_addr;
871 iw_event->remote_addr = cm_id_priv->id.remote_addr;
872 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
874 /* REJECTED or RESET */
875 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
876 cm_id_priv->qp = NULL;
877 cm_id_priv->state = IW_CM_STATE_IDLE;
879 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
880 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
882 if (iw_event->private_data_len)
883 kfree(iw_event->private_data);
885 /* Wake up waiters on connect complete */
886 wake_up_all(&cm_id_priv->connect_wait);
894 * If in the ESTABLISHED state, move to CLOSING.
896 static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
897 struct iw_cm_event *iw_event)
901 spin_lock_irqsave(&cm_id_priv->lock, flags);
902 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
903 cm_id_priv->state = IW_CM_STATE_CLOSING;
904 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
910 * If in the ESTBLISHED or CLOSING states, the QP will have have been
911 * moved by the provider to the ERR state. Disassociate the CM_ID from
912 * the QP, move to IDLE, and remove the 'connected' reference.
914 * If in some other state, the cm_id was destroyed asynchronously.
915 * This is the last reference that will result in waking up
916 * the app thread blocked in iw_destroy_cm_id.
918 static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
919 struct iw_cm_event *iw_event)
923 spin_lock_irqsave(&cm_id_priv->lock, flags);
925 if (cm_id_priv->qp) {
926 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
927 cm_id_priv->qp = NULL;
929 switch (cm_id_priv->state) {
930 case IW_CM_STATE_ESTABLISHED:
931 case IW_CM_STATE_CLOSING:
932 cm_id_priv->state = IW_CM_STATE_IDLE;
933 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
934 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
935 spin_lock_irqsave(&cm_id_priv->lock, flags);
937 case IW_CM_STATE_DESTROYING:
942 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
947 static int process_event(struct iwcm_id_private *cm_id_priv,
948 struct iw_cm_event *iw_event)
952 switch (iw_event->event) {
953 case IW_CM_EVENT_CONNECT_REQUEST:
954 cm_conn_req_handler(cm_id_priv, iw_event);
956 case IW_CM_EVENT_CONNECT_REPLY:
957 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
959 case IW_CM_EVENT_ESTABLISHED:
960 ret = cm_conn_est_handler(cm_id_priv, iw_event);
962 case IW_CM_EVENT_DISCONNECT:
963 cm_disconnect_handler(cm_id_priv, iw_event);
965 case IW_CM_EVENT_CLOSE:
966 ret = cm_close_handler(cm_id_priv, iw_event);
976 * Process events on the work_list for the cm_id. If the callback
977 * function requests that the cm_id be deleted, a flag is set in the
978 * cm_id flags to indicate that when the last reference is
979 * removed, the cm_id is to be destroyed. This is necessary to
980 * distinguish between an object that will be destroyed by the app
981 * thread asleep on the destroy_comp list vs. an object destroyed
982 * here synchronously when the last reference is removed.
984 static void cm_work_handler(struct work_struct *_work)
986 struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
987 struct iw_cm_event levent;
988 struct iwcm_id_private *cm_id_priv = work->cm_id;
993 spin_lock_irqsave(&cm_id_priv->lock, flags);
994 empty = list_empty(&cm_id_priv->work_list);
996 work = list_entry(cm_id_priv->work_list.next,
997 struct iwcm_work, list);
998 list_del_init(&work->list);
999 empty = list_empty(&cm_id_priv->work_list);
1000 levent = work->event;
1002 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1004 if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
1005 ret = process_event(cm_id_priv, &levent);
1007 destroy_cm_id(&cm_id_priv->id);
1009 pr_debug("dropping event %d\n", levent.event);
1010 if (iwcm_deref_id(cm_id_priv))
1014 spin_lock_irqsave(&cm_id_priv->lock, flags);
1016 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1020 * This function is called on interrupt context. Schedule events on
1021 * the iwcm_wq thread to allow callback functions to downcall into
1022 * the CM and/or block. Events are queued to a per-CM_ID
1023 * work_list. If this is the first event on the work_list, the work
1024 * element is also queued on the iwcm_wq thread.
1026 * Each event holds a reference on the cm_id. Until the last posted
1027 * event has been delivered and processed, the cm_id cannot be
1031 * 0 - the event was handled.
1032 * -ENOMEM - the event was not handled due to lack of resources.
1034 static int cm_event_handler(struct iw_cm_id *cm_id,
1035 struct iw_cm_event *iw_event)
1037 struct iwcm_work *work;
1038 struct iwcm_id_private *cm_id_priv;
1039 unsigned long flags;
1042 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1044 spin_lock_irqsave(&cm_id_priv->lock, flags);
1045 work = get_work(cm_id_priv);
1051 INIT_WORK(&work->work, cm_work_handler);
1052 work->cm_id = cm_id_priv;
1053 work->event = *iw_event;
1055 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
1056 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
1057 work->event.private_data_len) {
1058 ret = copy_private_data(&work->event);
1065 atomic_inc(&cm_id_priv->refcount);
1066 if (list_empty(&cm_id_priv->work_list)) {
1067 list_add_tail(&work->list, &cm_id_priv->work_list);
1068 queue_work(iwcm_wq, &work->work);
1070 list_add_tail(&work->list, &cm_id_priv->work_list);
1072 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1076 static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
1077 struct ib_qp_attr *qp_attr,
1080 unsigned long flags;
1083 spin_lock_irqsave(&cm_id_priv->lock, flags);
1084 switch (cm_id_priv->state) {
1085 case IW_CM_STATE_IDLE:
1086 case IW_CM_STATE_CONN_SENT:
1087 case IW_CM_STATE_CONN_RECV:
1088 case IW_CM_STATE_ESTABLISHED:
1089 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1090 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
1091 IB_ACCESS_REMOTE_READ;
1098 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1102 static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
1103 struct ib_qp_attr *qp_attr,
1106 unsigned long flags;
1109 spin_lock_irqsave(&cm_id_priv->lock, flags);
1110 switch (cm_id_priv->state) {
1111 case IW_CM_STATE_IDLE:
1112 case IW_CM_STATE_CONN_SENT:
1113 case IW_CM_STATE_CONN_RECV:
1114 case IW_CM_STATE_ESTABLISHED:
1122 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
1126 int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
1127 struct ib_qp_attr *qp_attr,
1130 struct iwcm_id_private *cm_id_priv;
1133 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1134 switch (qp_attr->qp_state) {
1137 ret = iwcm_init_qp_init_attr(cm_id_priv,
1138 qp_attr, qp_attr_mask);
1141 ret = iwcm_init_qp_rts_attr(cm_id_priv,
1142 qp_attr, qp_attr_mask);
1150 EXPORT_SYMBOL(iw_cm_init_qp_attr);
1152 static int __init iw_cm_init(void)
1156 ret = iwpm_init(RDMA_NL_IWCM);
1158 pr_err("iw_cm: couldn't init iwpm\n");
1160 ret = ibnl_add_client(RDMA_NL_IWCM, ARRAY_SIZE(iwcm_nl_cb_table),
1163 pr_err("iw_cm: couldn't register netlink callbacks\n");
1165 iwcm_wq = alloc_ordered_workqueue("iw_cm_wq", WQ_MEM_RECLAIM);
1169 iwcm_ctl_table_hdr = register_net_sysctl(&init_net, "net/iw_cm",
1171 if (!iwcm_ctl_table_hdr) {
1172 pr_err("iw_cm: couldn't register sysctl paths\n");
1173 destroy_workqueue(iwcm_wq);
1180 static void __exit iw_cm_cleanup(void)
1182 unregister_net_sysctl_table(iwcm_ctl_table_hdr);
1183 destroy_workqueue(iwcm_wq);
1184 ibnl_remove_client(RDMA_NL_IWCM);
1185 iwpm_exit(RDMA_NL_IWCM);
1188 module_init(iw_cm_init);
1189 module_exit(iw_cm_cleanup);