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
)
141 kfree(list_entry(e
, struct iwcm_work
, free_list
));
144 static int alloc_work_entries(struct iwcm_id_private
*cm_id_priv
, int count
)
146 struct iwcm_work
*work
;
148 BUG_ON(!list_empty(&cm_id_priv
->work_free_list
));
150 work
= kmalloc(sizeof(struct iwcm_work
), GFP_KERNEL
);
152 dealloc_work_entries(cm_id_priv
);
155 work
->cm_id
= cm_id_priv
;
156 INIT_LIST_HEAD(&work
->list
);
163 * Save private data from incoming connection requests to
164 * iw_cm_event, so the low level driver doesn't have to. Adjust
165 * the event ptr to point to the local copy.
167 static int copy_private_data(struct iw_cm_event
*event
)
171 p
= kmemdup(event
->private_data
, event
->private_data_len
, GFP_ATOMIC
);
174 event
->private_data
= p
;
178 static void free_cm_id(struct iwcm_id_private
*cm_id_priv
)
180 dealloc_work_entries(cm_id_priv
);
185 * Release a reference on cm_id. If the last reference is being
186 * released, free the cm_id and return 1.
188 static int iwcm_deref_id(struct iwcm_id_private
*cm_id_priv
)
190 BUG_ON(atomic_read(&cm_id_priv
->refcount
)==0);
191 if (atomic_dec_and_test(&cm_id_priv
->refcount
)) {
192 BUG_ON(!list_empty(&cm_id_priv
->work_list
));
193 free_cm_id(cm_id_priv
);
200 static void add_ref(struct iw_cm_id
*cm_id
)
202 struct iwcm_id_private
*cm_id_priv
;
203 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
204 atomic_inc(&cm_id_priv
->refcount
);
207 static void rem_ref(struct iw_cm_id
*cm_id
)
209 struct iwcm_id_private
*cm_id_priv
;
211 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
213 (void)iwcm_deref_id(cm_id_priv
);
216 static int cm_event_handler(struct iw_cm_id
*cm_id
, struct iw_cm_event
*event
);
218 struct iw_cm_id
*iw_create_cm_id(struct ib_device
*device
,
219 iw_cm_handler cm_handler
,
222 struct iwcm_id_private
*cm_id_priv
;
224 cm_id_priv
= kzalloc(sizeof(*cm_id_priv
), GFP_KERNEL
);
226 return ERR_PTR(-ENOMEM
);
228 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
229 cm_id_priv
->id
.device
= device
;
230 cm_id_priv
->id
.cm_handler
= cm_handler
;
231 cm_id_priv
->id
.context
= context
;
232 cm_id_priv
->id
.event_handler
= cm_event_handler
;
233 cm_id_priv
->id
.add_ref
= add_ref
;
234 cm_id_priv
->id
.rem_ref
= rem_ref
;
235 spin_lock_init(&cm_id_priv
->lock
);
236 atomic_set(&cm_id_priv
->refcount
, 1);
237 init_waitqueue_head(&cm_id_priv
->connect_wait
);
238 init_completion(&cm_id_priv
->destroy_comp
);
239 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
240 INIT_LIST_HEAD(&cm_id_priv
->work_free_list
);
242 return &cm_id_priv
->id
;
244 EXPORT_SYMBOL(iw_create_cm_id
);
247 static int iwcm_modify_qp_err(struct ib_qp
*qp
)
249 struct ib_qp_attr qp_attr
;
254 qp_attr
.qp_state
= IB_QPS_ERR
;
255 return ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
259 * This is really the RDMAC CLOSING state. It is most similar to the
262 static int iwcm_modify_qp_sqd(struct ib_qp
*qp
)
264 struct ib_qp_attr qp_attr
;
267 qp_attr
.qp_state
= IB_QPS_SQD
;
268 return ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
274 * Block if a passive or active connection is currently being processed. Then
275 * process the event as follows:
276 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
277 * based on the abrupt flag
278 * - If the connection is already in the CLOSING or IDLE state, the peer is
279 * disconnecting concurrently with us and we've already seen the
280 * DISCONNECT event -- ignore the request and return 0
281 * - Disconnect on a listening endpoint returns -EINVAL
283 int iw_cm_disconnect(struct iw_cm_id
*cm_id
, int abrupt
)
285 struct iwcm_id_private
*cm_id_priv
;
288 struct ib_qp
*qp
= NULL
;
290 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
291 /* Wait if we're currently in a connect or accept downcall */
292 wait_event(cm_id_priv
->connect_wait
,
293 !test_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
));
295 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
296 switch (cm_id_priv
->state
) {
297 case IW_CM_STATE_ESTABLISHED
:
298 cm_id_priv
->state
= IW_CM_STATE_CLOSING
;
300 /* QP could be <nul> for user-mode client */
306 case IW_CM_STATE_LISTEN
:
309 case IW_CM_STATE_CLOSING
:
310 /* remote peer closed first */
311 case IW_CM_STATE_IDLE
:
312 /* accept or connect returned !0 */
314 case IW_CM_STATE_CONN_RECV
:
316 * App called disconnect before/without calling accept after
317 * connect_request event delivered.
320 case IW_CM_STATE_CONN_SENT
:
321 /* Can only get here if wait above fails */
325 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
329 ret
= iwcm_modify_qp_err(qp
);
331 ret
= iwcm_modify_qp_sqd(qp
);
334 * If both sides are disconnecting the QP could
335 * already be in ERR or SQD states
342 EXPORT_SYMBOL(iw_cm_disconnect
);
345 * CM_ID <-- DESTROYING
347 * Clean up all resources associated with the connection and release
348 * the initial reference taken by iw_create_cm_id.
350 static void destroy_cm_id(struct iw_cm_id
*cm_id
)
352 struct iwcm_id_private
*cm_id_priv
;
355 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
357 * Wait if we're currently in a connect or accept downcall. A
358 * listening endpoint should never block here.
360 wait_event(cm_id_priv
->connect_wait
,
361 !test_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
));
364 * Since we're deleting the cm_id, drop any events that
365 * might arrive before the last dereference.
367 set_bit(IWCM_F_DROP_EVENTS
, &cm_id_priv
->flags
);
369 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
370 switch (cm_id_priv
->state
) {
371 case IW_CM_STATE_LISTEN
:
372 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
373 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
374 /* destroy the listening endpoint */
375 cm_id
->device
->iwcm
->destroy_listen(cm_id
);
376 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
378 case IW_CM_STATE_ESTABLISHED
:
379 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
380 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
381 /* Abrupt close of the connection */
382 (void)iwcm_modify_qp_err(cm_id_priv
->qp
);
383 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
385 case IW_CM_STATE_IDLE
:
386 case IW_CM_STATE_CLOSING
:
387 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
389 case IW_CM_STATE_CONN_RECV
:
391 * App called destroy before/without calling accept after
392 * receiving connection request event notification or
393 * returned non zero from the event callback function.
394 * In either case, must tell the provider to reject.
396 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
397 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
398 cm_id
->device
->iwcm
->reject(cm_id
, NULL
, 0);
399 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
401 case IW_CM_STATE_CONN_SENT
:
402 case IW_CM_STATE_DESTROYING
:
407 if (cm_id_priv
->qp
) {
408 cm_id_priv
->id
.device
->iwcm
->rem_ref(cm_id_priv
->qp
);
409 cm_id_priv
->qp
= NULL
;
411 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
414 iwpm_remove_mapinfo(&cm_id
->local_addr
, &cm_id
->m_local_addr
);
415 iwpm_remove_mapping(&cm_id
->local_addr
, RDMA_NL_IWCM
);
418 (void)iwcm_deref_id(cm_id_priv
);
422 * This function is only called by the application thread and cannot
423 * be called by the event thread. The function will wait for all
424 * references to be released on the cm_id and then kfree the cm_id
427 void iw_destroy_cm_id(struct iw_cm_id
*cm_id
)
429 struct iwcm_id_private
*cm_id_priv
;
431 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
432 destroy_cm_id(cm_id
);
434 EXPORT_SYMBOL(iw_destroy_cm_id
);
437 * iw_cm_check_wildcard - If IP address is 0 then use original
438 * @pm_addr: sockaddr containing the ip to check for wildcard
439 * @cm_addr: sockaddr containing the actual IP address
440 * @cm_outaddr: sockaddr to set IP addr which leaving port
442 * Checks the pm_addr for wildcard and then sets cm_outaddr's
443 * IP to the actual (cm_addr).
445 static void iw_cm_check_wildcard(struct sockaddr_storage
*pm_addr
,
446 struct sockaddr_storage
*cm_addr
,
447 struct sockaddr_storage
*cm_outaddr
)
449 if (pm_addr
->ss_family
== AF_INET
) {
450 struct sockaddr_in
*pm4_addr
= (struct sockaddr_in
*)pm_addr
;
452 if (pm4_addr
->sin_addr
.s_addr
== htonl(INADDR_ANY
)) {
453 struct sockaddr_in
*cm4_addr
=
454 (struct sockaddr_in
*)cm_addr
;
455 struct sockaddr_in
*cm4_outaddr
=
456 (struct sockaddr_in
*)cm_outaddr
;
458 cm4_outaddr
->sin_addr
= cm4_addr
->sin_addr
;
461 struct sockaddr_in6
*pm6_addr
= (struct sockaddr_in6
*)pm_addr
;
463 if (ipv6_addr_type(&pm6_addr
->sin6_addr
) == IPV6_ADDR_ANY
) {
464 struct sockaddr_in6
*cm6_addr
=
465 (struct sockaddr_in6
*)cm_addr
;
466 struct sockaddr_in6
*cm6_outaddr
=
467 (struct sockaddr_in6
*)cm_outaddr
;
469 cm6_outaddr
->sin6_addr
= cm6_addr
->sin6_addr
;
475 * iw_cm_map - Use portmapper to map the ports
476 * @cm_id: connection manager pointer
477 * @active: Indicates the active side when true
478 * returns nonzero for error only if iwpm_create_mapinfo() fails
480 * Tries to add a mapping for a port using the Portmapper. If
481 * successful in mapping the IP/Port it will check the remote
482 * mapped IP address for a wildcard IP address and replace the
483 * zero IP address with the remote_addr.
485 static int iw_cm_map(struct iw_cm_id
*cm_id
, bool active
)
487 struct iwpm_dev_data pm_reg_msg
;
488 struct iwpm_sa_data pm_msg
;
491 cm_id
->m_local_addr
= cm_id
->local_addr
;
492 cm_id
->m_remote_addr
= cm_id
->remote_addr
;
494 memcpy(pm_reg_msg
.dev_name
, cm_id
->device
->name
,
495 sizeof(pm_reg_msg
.dev_name
));
496 memcpy(pm_reg_msg
.if_name
, cm_id
->device
->iwcm
->ifname
,
497 sizeof(pm_reg_msg
.if_name
));
499 if (iwpm_register_pid(&pm_reg_msg
, RDMA_NL_IWCM
) ||
503 cm_id
->mapped
= true;
504 pm_msg
.loc_addr
= cm_id
->local_addr
;
505 pm_msg
.rem_addr
= cm_id
->remote_addr
;
507 status
= iwpm_add_and_query_mapping(&pm_msg
,
510 status
= iwpm_add_mapping(&pm_msg
, RDMA_NL_IWCM
);
513 cm_id
->m_local_addr
= pm_msg
.mapped_loc_addr
;
515 cm_id
->m_remote_addr
= pm_msg
.mapped_rem_addr
;
516 iw_cm_check_wildcard(&pm_msg
.mapped_rem_addr
,
518 &cm_id
->m_remote_addr
);
522 return iwpm_create_mapinfo(&cm_id
->local_addr
,
523 &cm_id
->m_local_addr
,
530 * Start listening for connect requests. Generates one CONNECT_REQUEST
531 * event for each inbound connect request.
533 int iw_cm_listen(struct iw_cm_id
*cm_id
, int backlog
)
535 struct iwcm_id_private
*cm_id_priv
;
539 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
542 backlog
= default_backlog
;
544 ret
= alloc_work_entries(cm_id_priv
, backlog
);
548 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
549 switch (cm_id_priv
->state
) {
550 case IW_CM_STATE_IDLE
:
551 cm_id_priv
->state
= IW_CM_STATE_LISTEN
;
552 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
553 ret
= iw_cm_map(cm_id
, false);
555 ret
= cm_id
->device
->iwcm
->create_listen(cm_id
, backlog
);
557 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
558 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
563 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
567 EXPORT_SYMBOL(iw_cm_listen
);
572 * Rejects an inbound connection request. No events are generated.
574 int iw_cm_reject(struct iw_cm_id
*cm_id
,
575 const void *private_data
,
578 struct iwcm_id_private
*cm_id_priv
;
582 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
583 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
585 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
586 if (cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
) {
587 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
588 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
589 wake_up_all(&cm_id_priv
->connect_wait
);
592 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
593 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
595 ret
= cm_id
->device
->iwcm
->reject(cm_id
, private_data
,
598 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
599 wake_up_all(&cm_id_priv
->connect_wait
);
603 EXPORT_SYMBOL(iw_cm_reject
);
606 * CM_ID <-- ESTABLISHED
608 * Accepts an inbound connection request and generates an ESTABLISHED
609 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
610 * until the ESTABLISHED event is received from the provider.
612 int iw_cm_accept(struct iw_cm_id
*cm_id
,
613 struct iw_cm_conn_param
*iw_param
)
615 struct iwcm_id_private
*cm_id_priv
;
620 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
621 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
623 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
624 if (cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
) {
625 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
626 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
627 wake_up_all(&cm_id_priv
->connect_wait
);
630 /* Get the ib_qp given the QPN */
631 qp
= cm_id
->device
->iwcm
->get_qp(cm_id
->device
, iw_param
->qpn
);
633 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
634 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
635 wake_up_all(&cm_id_priv
->connect_wait
);
638 cm_id
->device
->iwcm
->add_ref(qp
);
640 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
642 ret
= cm_id
->device
->iwcm
->accept(cm_id
, iw_param
);
644 /* An error on accept precludes provider events */
645 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
);
646 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
647 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
648 if (cm_id_priv
->qp
) {
649 cm_id
->device
->iwcm
->rem_ref(qp
);
650 cm_id_priv
->qp
= NULL
;
652 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
653 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
654 wake_up_all(&cm_id_priv
->connect_wait
);
659 EXPORT_SYMBOL(iw_cm_accept
);
662 * Active Side: CM_ID <-- CONN_SENT
664 * If successful, results in the generation of a CONNECT_REPLY
665 * event. iw_cm_disconnect and iw_cm_destroy will block until the
666 * CONNECT_REPLY event is received from the provider.
668 int iw_cm_connect(struct iw_cm_id
*cm_id
, struct iw_cm_conn_param
*iw_param
)
670 struct iwcm_id_private
*cm_id_priv
;
675 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
677 ret
= alloc_work_entries(cm_id_priv
, 4);
681 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
682 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
684 if (cm_id_priv
->state
!= IW_CM_STATE_IDLE
) {
689 /* Get the ib_qp given the QPN */
690 qp
= cm_id
->device
->iwcm
->get_qp(cm_id
->device
, iw_param
->qpn
);
695 cm_id
->device
->iwcm
->add_ref(qp
);
697 cm_id_priv
->state
= IW_CM_STATE_CONN_SENT
;
698 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
700 ret
= iw_cm_map(cm_id
, true);
702 ret
= cm_id
->device
->iwcm
->connect(cm_id
, iw_param
);
704 return 0; /* success */
706 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
707 if (cm_id_priv
->qp
) {
708 cm_id
->device
->iwcm
->rem_ref(qp
);
709 cm_id_priv
->qp
= NULL
;
711 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
713 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
714 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
715 wake_up_all(&cm_id_priv
->connect_wait
);
718 EXPORT_SYMBOL(iw_cm_connect
);
721 * Passive Side: new CM_ID <-- CONN_RECV
723 * Handles an inbound connect request. The function creates a new
724 * iw_cm_id to represent the new connection and inherits the client
725 * callback function and other attributes from the listening parent.
727 * The work item contains a pointer to the listen_cm_id and the event. The
728 * listen_cm_id contains the client cm_handler, context and
729 * device. These are copied when the device is cloned. The event
730 * contains the new four tuple.
732 * An error on the child should not affect the parent, so this
733 * function does not return a value.
735 static void cm_conn_req_handler(struct iwcm_id_private
*listen_id_priv
,
736 struct iw_cm_event
*iw_event
)
739 struct iw_cm_id
*cm_id
;
740 struct iwcm_id_private
*cm_id_priv
;
744 * The provider should never generate a connection request
745 * event with a bad status.
747 BUG_ON(iw_event
->status
);
749 cm_id
= iw_create_cm_id(listen_id_priv
->id
.device
,
750 listen_id_priv
->id
.cm_handler
,
751 listen_id_priv
->id
.context
);
752 /* If the cm_id could not be created, ignore the request */
756 cm_id
->provider_data
= iw_event
->provider_data
;
757 cm_id
->m_local_addr
= iw_event
->local_addr
;
758 cm_id
->m_remote_addr
= iw_event
->remote_addr
;
759 cm_id
->local_addr
= listen_id_priv
->id
.local_addr
;
761 ret
= iwpm_get_remote_info(&listen_id_priv
->id
.m_local_addr
,
762 &iw_event
->remote_addr
,
766 cm_id
->remote_addr
= iw_event
->remote_addr
;
768 iw_cm_check_wildcard(&listen_id_priv
->id
.m_local_addr
,
769 &iw_event
->local_addr
,
771 iw_event
->local_addr
= cm_id
->local_addr
;
772 iw_event
->remote_addr
= cm_id
->remote_addr
;
775 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
776 cm_id_priv
->state
= IW_CM_STATE_CONN_RECV
;
779 * We could be destroying the listening id. If so, ignore this
782 spin_lock_irqsave(&listen_id_priv
->lock
, flags
);
783 if (listen_id_priv
->state
!= IW_CM_STATE_LISTEN
) {
784 spin_unlock_irqrestore(&listen_id_priv
->lock
, flags
);
785 iw_cm_reject(cm_id
, NULL
, 0);
786 iw_destroy_cm_id(cm_id
);
789 spin_unlock_irqrestore(&listen_id_priv
->lock
, flags
);
791 ret
= alloc_work_entries(cm_id_priv
, 3);
793 iw_cm_reject(cm_id
, NULL
, 0);
794 iw_destroy_cm_id(cm_id
);
798 /* Call the client CM handler */
799 ret
= cm_id
->cm_handler(cm_id
, iw_event
);
801 iw_cm_reject(cm_id
, NULL
, 0);
802 iw_destroy_cm_id(cm_id
);
806 if (iw_event
->private_data_len
)
807 kfree(iw_event
->private_data
);
811 * Passive Side: CM_ID <-- ESTABLISHED
813 * The provider generated an ESTABLISHED event which means that
814 * the MPA negotion has completed successfully and we are now in MPA
817 * This event can only be received in the CONN_RECV state. If the
818 * remote peer closed, the ESTABLISHED event would be received followed
819 * by the CLOSE event. If the app closes, it will block until we wake
820 * it up after processing this event.
822 static int cm_conn_est_handler(struct iwcm_id_private
*cm_id_priv
,
823 struct iw_cm_event
*iw_event
)
828 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
831 * We clear the CONNECT_WAIT bit here to allow the callback
832 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
833 * from a callback handler is not allowed.
835 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
836 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
);
837 cm_id_priv
->state
= IW_CM_STATE_ESTABLISHED
;
838 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
839 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
840 wake_up_all(&cm_id_priv
->connect_wait
);
846 * Active Side: CM_ID <-- ESTABLISHED
848 * The app has called connect and is waiting for the established event to
849 * post it's requests to the server. This event will wake up anyone
850 * blocked in iw_cm_disconnect or iw_destroy_id.
852 static int cm_conn_rep_handler(struct iwcm_id_private
*cm_id_priv
,
853 struct iw_cm_event
*iw_event
)
858 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
860 * Clear the connect wait bit so a callback function calling
861 * iw_cm_disconnect will not wait and deadlock this thread
863 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
864 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_SENT
);
865 if (iw_event
->status
== 0) {
866 cm_id_priv
->id
.m_local_addr
= iw_event
->local_addr
;
867 cm_id_priv
->id
.m_remote_addr
= iw_event
->remote_addr
;
868 iw_event
->local_addr
= cm_id_priv
->id
.local_addr
;
869 iw_event
->remote_addr
= cm_id_priv
->id
.remote_addr
;
870 cm_id_priv
->state
= IW_CM_STATE_ESTABLISHED
;
872 /* REJECTED or RESET */
873 cm_id_priv
->id
.device
->iwcm
->rem_ref(cm_id_priv
->qp
);
874 cm_id_priv
->qp
= NULL
;
875 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
877 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
878 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
880 if (iw_event
->private_data_len
)
881 kfree(iw_event
->private_data
);
883 /* Wake up waiters on connect complete */
884 wake_up_all(&cm_id_priv
->connect_wait
);
892 * If in the ESTABLISHED state, move to CLOSING.
894 static void cm_disconnect_handler(struct iwcm_id_private
*cm_id_priv
,
895 struct iw_cm_event
*iw_event
)
899 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
900 if (cm_id_priv
->state
== IW_CM_STATE_ESTABLISHED
)
901 cm_id_priv
->state
= IW_CM_STATE_CLOSING
;
902 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
908 * If in the ESTBLISHED or CLOSING states, the QP will have have been
909 * moved by the provider to the ERR state. Disassociate the CM_ID from
910 * the QP, move to IDLE, and remove the 'connected' reference.
912 * If in some other state, the cm_id was destroyed asynchronously.
913 * This is the last reference that will result in waking up
914 * the app thread blocked in iw_destroy_cm_id.
916 static int cm_close_handler(struct iwcm_id_private
*cm_id_priv
,
917 struct iw_cm_event
*iw_event
)
921 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
923 if (cm_id_priv
->qp
) {
924 cm_id_priv
->id
.device
->iwcm
->rem_ref(cm_id_priv
->qp
);
925 cm_id_priv
->qp
= NULL
;
927 switch (cm_id_priv
->state
) {
928 case IW_CM_STATE_ESTABLISHED
:
929 case IW_CM_STATE_CLOSING
:
930 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
931 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
932 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
933 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
935 case IW_CM_STATE_DESTROYING
:
940 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
945 static int process_event(struct iwcm_id_private
*cm_id_priv
,
946 struct iw_cm_event
*iw_event
)
950 switch (iw_event
->event
) {
951 case IW_CM_EVENT_CONNECT_REQUEST
:
952 cm_conn_req_handler(cm_id_priv
, iw_event
);
954 case IW_CM_EVENT_CONNECT_REPLY
:
955 ret
= cm_conn_rep_handler(cm_id_priv
, iw_event
);
957 case IW_CM_EVENT_ESTABLISHED
:
958 ret
= cm_conn_est_handler(cm_id_priv
, iw_event
);
960 case IW_CM_EVENT_DISCONNECT
:
961 cm_disconnect_handler(cm_id_priv
, iw_event
);
963 case IW_CM_EVENT_CLOSE
:
964 ret
= cm_close_handler(cm_id_priv
, iw_event
);
974 * Process events on the work_list for the cm_id. If the callback
975 * function requests that the cm_id be deleted, a flag is set in the
976 * cm_id flags to indicate that when the last reference is
977 * removed, the cm_id is to be destroyed. This is necessary to
978 * distinguish between an object that will be destroyed by the app
979 * thread asleep on the destroy_comp list vs. an object destroyed
980 * here synchronously when the last reference is removed.
982 static void cm_work_handler(struct work_struct
*_work
)
984 struct iwcm_work
*work
= container_of(_work
, struct iwcm_work
, work
);
985 struct iw_cm_event levent
;
986 struct iwcm_id_private
*cm_id_priv
= work
->cm_id
;
991 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
992 empty
= list_empty(&cm_id_priv
->work_list
);
994 work
= list_entry(cm_id_priv
->work_list
.next
,
995 struct iwcm_work
, list
);
996 list_del_init(&work
->list
);
997 empty
= list_empty(&cm_id_priv
->work_list
);
998 levent
= work
->event
;
1000 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1002 if (!test_bit(IWCM_F_DROP_EVENTS
, &cm_id_priv
->flags
)) {
1003 ret
= process_event(cm_id_priv
, &levent
);
1005 destroy_cm_id(&cm_id_priv
->id
);
1007 pr_debug("dropping event %d\n", levent
.event
);
1008 if (iwcm_deref_id(cm_id_priv
))
1012 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1014 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1018 * This function is called on interrupt context. Schedule events on
1019 * the iwcm_wq thread to allow callback functions to downcall into
1020 * the CM and/or block. Events are queued to a per-CM_ID
1021 * work_list. If this is the first event on the work_list, the work
1022 * element is also queued on the iwcm_wq thread.
1024 * Each event holds a reference on the cm_id. Until the last posted
1025 * event has been delivered and processed, the cm_id cannot be
1029 * 0 - the event was handled.
1030 * -ENOMEM - the event was not handled due to lack of resources.
1032 static int cm_event_handler(struct iw_cm_id
*cm_id
,
1033 struct iw_cm_event
*iw_event
)
1035 struct iwcm_work
*work
;
1036 struct iwcm_id_private
*cm_id_priv
;
1037 unsigned long flags
;
1040 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
1042 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1043 work
= get_work(cm_id_priv
);
1049 INIT_WORK(&work
->work
, cm_work_handler
);
1050 work
->cm_id
= cm_id_priv
;
1051 work
->event
= *iw_event
;
1053 if ((work
->event
.event
== IW_CM_EVENT_CONNECT_REQUEST
||
1054 work
->event
.event
== IW_CM_EVENT_CONNECT_REPLY
) &&
1055 work
->event
.private_data_len
) {
1056 ret
= copy_private_data(&work
->event
);
1063 atomic_inc(&cm_id_priv
->refcount
);
1064 if (list_empty(&cm_id_priv
->work_list
)) {
1065 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1066 queue_work(iwcm_wq
, &work
->work
);
1068 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1070 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1074 static int iwcm_init_qp_init_attr(struct iwcm_id_private
*cm_id_priv
,
1075 struct ib_qp_attr
*qp_attr
,
1078 unsigned long flags
;
1081 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1082 switch (cm_id_priv
->state
) {
1083 case IW_CM_STATE_IDLE
:
1084 case IW_CM_STATE_CONN_SENT
:
1085 case IW_CM_STATE_CONN_RECV
:
1086 case IW_CM_STATE_ESTABLISHED
:
1087 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1088 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
|
1089 IB_ACCESS_REMOTE_READ
;
1096 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1100 static int iwcm_init_qp_rts_attr(struct iwcm_id_private
*cm_id_priv
,
1101 struct ib_qp_attr
*qp_attr
,
1104 unsigned long flags
;
1107 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1108 switch (cm_id_priv
->state
) {
1109 case IW_CM_STATE_IDLE
:
1110 case IW_CM_STATE_CONN_SENT
:
1111 case IW_CM_STATE_CONN_RECV
:
1112 case IW_CM_STATE_ESTABLISHED
:
1120 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1124 int iw_cm_init_qp_attr(struct iw_cm_id
*cm_id
,
1125 struct ib_qp_attr
*qp_attr
,
1128 struct iwcm_id_private
*cm_id_priv
;
1131 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
1132 switch (qp_attr
->qp_state
) {
1135 ret
= iwcm_init_qp_init_attr(cm_id_priv
,
1136 qp_attr
, qp_attr_mask
);
1139 ret
= iwcm_init_qp_rts_attr(cm_id_priv
,
1140 qp_attr
, qp_attr_mask
);
1148 EXPORT_SYMBOL(iw_cm_init_qp_attr
);
1150 static int __init
iw_cm_init(void)
1154 ret
= iwpm_init(RDMA_NL_IWCM
);
1156 pr_err("iw_cm: couldn't init iwpm\n");
1158 ret
= ibnl_add_client(RDMA_NL_IWCM
, ARRAY_SIZE(iwcm_nl_cb_table
),
1161 pr_err("iw_cm: couldn't register netlink callbacks\n");
1163 iwcm_wq
= alloc_ordered_workqueue("iw_cm_wq", WQ_MEM_RECLAIM
);
1167 iwcm_ctl_table_hdr
= register_net_sysctl(&init_net
, "net/iw_cm",
1169 if (!iwcm_ctl_table_hdr
) {
1170 pr_err("iw_cm: couldn't register sysctl paths\n");
1171 destroy_workqueue(iwcm_wq
);
1178 static void __exit
iw_cm_cleanup(void)
1180 unregister_net_sysctl_table(iwcm_ctl_table_hdr
);
1181 destroy_workqueue(iwcm_wq
);
1182 ibnl_remove_client(RDMA_NL_IWCM
);
1183 iwpm_exit(RDMA_NL_IWCM
);
1186 module_init(iw_cm_init
);
1187 module_exit(iw_cm_cleanup
);