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 const char * const iwcm_rej_reason_strs
[] = {
63 [ECONNRESET
] = "reset by remote host",
64 [ECONNREFUSED
] = "refused by remote application",
65 [ETIMEDOUT
] = "setup timeout",
68 const char *__attribute_const__
iwcm_reject_msg(int reason
)
72 /* iWARP uses negative errnos */
75 if (index
< ARRAY_SIZE(iwcm_rej_reason_strs
) &&
76 iwcm_rej_reason_strs
[index
])
77 return iwcm_rej_reason_strs
[index
];
79 return "unrecognized reason";
81 EXPORT_SYMBOL(iwcm_reject_msg
);
83 static struct ibnl_client_cbs iwcm_nl_cb_table
[] = {
84 [RDMA_NL_IWPM_REG_PID
] = {.dump
= iwpm_register_pid_cb
},
85 [RDMA_NL_IWPM_ADD_MAPPING
] = {.dump
= iwpm_add_mapping_cb
},
86 [RDMA_NL_IWPM_QUERY_MAPPING
] = {.dump
= iwpm_add_and_query_mapping_cb
},
87 [RDMA_NL_IWPM_REMOTE_INFO
] = {.dump
= iwpm_remote_info_cb
},
88 [RDMA_NL_IWPM_HANDLE_ERR
] = {.dump
= iwpm_mapping_error_cb
},
89 [RDMA_NL_IWPM_MAPINFO
] = {.dump
= iwpm_mapping_info_cb
},
90 [RDMA_NL_IWPM_MAPINFO_NUM
] = {.dump
= iwpm_ack_mapping_info_cb
}
93 static struct workqueue_struct
*iwcm_wq
;
95 struct work_struct work
;
96 struct iwcm_id_private
*cm_id
;
97 struct list_head list
;
98 struct iw_cm_event event
;
99 struct list_head free_list
;
102 static unsigned int default_backlog
= 256;
104 static struct ctl_table_header
*iwcm_ctl_table_hdr
;
105 static struct ctl_table iwcm_ctl_table
[] = {
107 .procname
= "default_backlog",
108 .data
= &default_backlog
,
109 .maxlen
= sizeof(default_backlog
),
111 .proc_handler
= proc_dointvec
,
117 * The following services provide a mechanism for pre-allocating iwcm_work
118 * elements. The design pre-allocates them based on the cm_id type:
119 * LISTENING IDS: Get enough elements preallocated to handle the
121 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
122 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
124 * Allocating them in connect and listen avoids having to deal
125 * with allocation failures on the event upcall from the provider (which
126 * is called in the interrupt context).
128 * One exception is when creating the cm_id for incoming connection requests.
129 * There are two cases:
130 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
131 * the backlog is exceeded, then no more connection request events will
132 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
133 * to the provider to reject the connection request.
134 * 2) in the connection request workqueue handler, cm_conn_req_handler().
135 * If work elements cannot be allocated for the new connect request cm_id,
136 * then IWCM will call the provider reject method. This is ok since
137 * cm_conn_req_handler() runs in the workqueue thread context.
140 static struct iwcm_work
*get_work(struct iwcm_id_private
*cm_id_priv
)
142 struct iwcm_work
*work
;
144 if (list_empty(&cm_id_priv
->work_free_list
))
146 work
= list_entry(cm_id_priv
->work_free_list
.next
, struct iwcm_work
,
148 list_del_init(&work
->free_list
);
152 static void put_work(struct iwcm_work
*work
)
154 list_add(&work
->free_list
, &work
->cm_id
->work_free_list
);
157 static void dealloc_work_entries(struct iwcm_id_private
*cm_id_priv
)
159 struct list_head
*e
, *tmp
;
161 list_for_each_safe(e
, tmp
, &cm_id_priv
->work_free_list
)
162 kfree(list_entry(e
, struct iwcm_work
, free_list
));
165 static int alloc_work_entries(struct iwcm_id_private
*cm_id_priv
, int count
)
167 struct iwcm_work
*work
;
169 BUG_ON(!list_empty(&cm_id_priv
->work_free_list
));
171 work
= kmalloc(sizeof(struct iwcm_work
), GFP_KERNEL
);
173 dealloc_work_entries(cm_id_priv
);
176 work
->cm_id
= cm_id_priv
;
177 INIT_LIST_HEAD(&work
->list
);
184 * Save private data from incoming connection requests to
185 * iw_cm_event, so the low level driver doesn't have to. Adjust
186 * the event ptr to point to the local copy.
188 static int copy_private_data(struct iw_cm_event
*event
)
192 p
= kmemdup(event
->private_data
, event
->private_data_len
, GFP_ATOMIC
);
195 event
->private_data
= p
;
199 static void free_cm_id(struct iwcm_id_private
*cm_id_priv
)
201 dealloc_work_entries(cm_id_priv
);
206 * Release a reference on cm_id. If the last reference is being
207 * released, free the cm_id and return 1.
209 static int iwcm_deref_id(struct iwcm_id_private
*cm_id_priv
)
211 BUG_ON(atomic_read(&cm_id_priv
->refcount
)==0);
212 if (atomic_dec_and_test(&cm_id_priv
->refcount
)) {
213 BUG_ON(!list_empty(&cm_id_priv
->work_list
));
214 free_cm_id(cm_id_priv
);
221 static void add_ref(struct iw_cm_id
*cm_id
)
223 struct iwcm_id_private
*cm_id_priv
;
224 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
225 atomic_inc(&cm_id_priv
->refcount
);
228 static void rem_ref(struct iw_cm_id
*cm_id
)
230 struct iwcm_id_private
*cm_id_priv
;
232 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
234 (void)iwcm_deref_id(cm_id_priv
);
237 static int cm_event_handler(struct iw_cm_id
*cm_id
, struct iw_cm_event
*event
);
239 struct iw_cm_id
*iw_create_cm_id(struct ib_device
*device
,
240 iw_cm_handler cm_handler
,
243 struct iwcm_id_private
*cm_id_priv
;
245 cm_id_priv
= kzalloc(sizeof(*cm_id_priv
), GFP_KERNEL
);
247 return ERR_PTR(-ENOMEM
);
249 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
250 cm_id_priv
->id
.device
= device
;
251 cm_id_priv
->id
.cm_handler
= cm_handler
;
252 cm_id_priv
->id
.context
= context
;
253 cm_id_priv
->id
.event_handler
= cm_event_handler
;
254 cm_id_priv
->id
.add_ref
= add_ref
;
255 cm_id_priv
->id
.rem_ref
= rem_ref
;
256 spin_lock_init(&cm_id_priv
->lock
);
257 atomic_set(&cm_id_priv
->refcount
, 1);
258 init_waitqueue_head(&cm_id_priv
->connect_wait
);
259 init_completion(&cm_id_priv
->destroy_comp
);
260 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
261 INIT_LIST_HEAD(&cm_id_priv
->work_free_list
);
263 return &cm_id_priv
->id
;
265 EXPORT_SYMBOL(iw_create_cm_id
);
268 static int iwcm_modify_qp_err(struct ib_qp
*qp
)
270 struct ib_qp_attr qp_attr
;
275 qp_attr
.qp_state
= IB_QPS_ERR
;
276 return ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
280 * This is really the RDMAC CLOSING state. It is most similar to the
283 static int iwcm_modify_qp_sqd(struct ib_qp
*qp
)
285 struct ib_qp_attr qp_attr
;
288 qp_attr
.qp_state
= IB_QPS_SQD
;
289 return ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
295 * Block if a passive or active connection is currently being processed. Then
296 * process the event as follows:
297 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
298 * based on the abrupt flag
299 * - If the connection is already in the CLOSING or IDLE state, the peer is
300 * disconnecting concurrently with us and we've already seen the
301 * DISCONNECT event -- ignore the request and return 0
302 * - Disconnect on a listening endpoint returns -EINVAL
304 int iw_cm_disconnect(struct iw_cm_id
*cm_id
, int abrupt
)
306 struct iwcm_id_private
*cm_id_priv
;
309 struct ib_qp
*qp
= NULL
;
311 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
312 /* Wait if we're currently in a connect or accept downcall */
313 wait_event(cm_id_priv
->connect_wait
,
314 !test_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
));
316 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
317 switch (cm_id_priv
->state
) {
318 case IW_CM_STATE_ESTABLISHED
:
319 cm_id_priv
->state
= IW_CM_STATE_CLOSING
;
321 /* QP could be <nul> for user-mode client */
327 case IW_CM_STATE_LISTEN
:
330 case IW_CM_STATE_CLOSING
:
331 /* remote peer closed first */
332 case IW_CM_STATE_IDLE
:
333 /* accept or connect returned !0 */
335 case IW_CM_STATE_CONN_RECV
:
337 * App called disconnect before/without calling accept after
338 * connect_request event delivered.
341 case IW_CM_STATE_CONN_SENT
:
342 /* Can only get here if wait above fails */
346 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
350 ret
= iwcm_modify_qp_err(qp
);
352 ret
= iwcm_modify_qp_sqd(qp
);
355 * If both sides are disconnecting the QP could
356 * already be in ERR or SQD states
363 EXPORT_SYMBOL(iw_cm_disconnect
);
366 * CM_ID <-- DESTROYING
368 * Clean up all resources associated with the connection and release
369 * the initial reference taken by iw_create_cm_id.
371 static void destroy_cm_id(struct iw_cm_id
*cm_id
)
373 struct iwcm_id_private
*cm_id_priv
;
376 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
378 * Wait if we're currently in a connect or accept downcall. A
379 * listening endpoint should never block here.
381 wait_event(cm_id_priv
->connect_wait
,
382 !test_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
));
385 * Since we're deleting the cm_id, drop any events that
386 * might arrive before the last dereference.
388 set_bit(IWCM_F_DROP_EVENTS
, &cm_id_priv
->flags
);
390 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
391 switch (cm_id_priv
->state
) {
392 case IW_CM_STATE_LISTEN
:
393 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
394 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
395 /* destroy the listening endpoint */
396 cm_id
->device
->iwcm
->destroy_listen(cm_id
);
397 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
399 case IW_CM_STATE_ESTABLISHED
:
400 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
401 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
402 /* Abrupt close of the connection */
403 (void)iwcm_modify_qp_err(cm_id_priv
->qp
);
404 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
406 case IW_CM_STATE_IDLE
:
407 case IW_CM_STATE_CLOSING
:
408 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
410 case IW_CM_STATE_CONN_RECV
:
412 * App called destroy before/without calling accept after
413 * receiving connection request event notification or
414 * returned non zero from the event callback function.
415 * In either case, must tell the provider to reject.
417 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
418 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
419 cm_id
->device
->iwcm
->reject(cm_id
, NULL
, 0);
420 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
422 case IW_CM_STATE_CONN_SENT
:
423 case IW_CM_STATE_DESTROYING
:
428 if (cm_id_priv
->qp
) {
429 cm_id_priv
->id
.device
->iwcm
->rem_ref(cm_id_priv
->qp
);
430 cm_id_priv
->qp
= NULL
;
432 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
435 iwpm_remove_mapinfo(&cm_id
->local_addr
, &cm_id
->m_local_addr
);
436 iwpm_remove_mapping(&cm_id
->local_addr
, RDMA_NL_IWCM
);
439 (void)iwcm_deref_id(cm_id_priv
);
443 * This function is only called by the application thread and cannot
444 * be called by the event thread. The function will wait for all
445 * references to be released on the cm_id and then kfree the cm_id
448 void iw_destroy_cm_id(struct iw_cm_id
*cm_id
)
450 struct iwcm_id_private
*cm_id_priv
;
452 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
453 destroy_cm_id(cm_id
);
455 EXPORT_SYMBOL(iw_destroy_cm_id
);
458 * iw_cm_check_wildcard - If IP address is 0 then use original
459 * @pm_addr: sockaddr containing the ip to check for wildcard
460 * @cm_addr: sockaddr containing the actual IP address
461 * @cm_outaddr: sockaddr to set IP addr which leaving port
463 * Checks the pm_addr for wildcard and then sets cm_outaddr's
464 * IP to the actual (cm_addr).
466 static void iw_cm_check_wildcard(struct sockaddr_storage
*pm_addr
,
467 struct sockaddr_storage
*cm_addr
,
468 struct sockaddr_storage
*cm_outaddr
)
470 if (pm_addr
->ss_family
== AF_INET
) {
471 struct sockaddr_in
*pm4_addr
= (struct sockaddr_in
*)pm_addr
;
473 if (pm4_addr
->sin_addr
.s_addr
== htonl(INADDR_ANY
)) {
474 struct sockaddr_in
*cm4_addr
=
475 (struct sockaddr_in
*)cm_addr
;
476 struct sockaddr_in
*cm4_outaddr
=
477 (struct sockaddr_in
*)cm_outaddr
;
479 cm4_outaddr
->sin_addr
= cm4_addr
->sin_addr
;
482 struct sockaddr_in6
*pm6_addr
= (struct sockaddr_in6
*)pm_addr
;
484 if (ipv6_addr_type(&pm6_addr
->sin6_addr
) == IPV6_ADDR_ANY
) {
485 struct sockaddr_in6
*cm6_addr
=
486 (struct sockaddr_in6
*)cm_addr
;
487 struct sockaddr_in6
*cm6_outaddr
=
488 (struct sockaddr_in6
*)cm_outaddr
;
490 cm6_outaddr
->sin6_addr
= cm6_addr
->sin6_addr
;
496 * iw_cm_map - Use portmapper to map the ports
497 * @cm_id: connection manager pointer
498 * @active: Indicates the active side when true
499 * returns nonzero for error only if iwpm_create_mapinfo() fails
501 * Tries to add a mapping for a port using the Portmapper. If
502 * successful in mapping the IP/Port it will check the remote
503 * mapped IP address for a wildcard IP address and replace the
504 * zero IP address with the remote_addr.
506 static int iw_cm_map(struct iw_cm_id
*cm_id
, bool active
)
508 struct iwpm_dev_data pm_reg_msg
;
509 struct iwpm_sa_data pm_msg
;
512 cm_id
->m_local_addr
= cm_id
->local_addr
;
513 cm_id
->m_remote_addr
= cm_id
->remote_addr
;
515 memcpy(pm_reg_msg
.dev_name
, cm_id
->device
->name
,
516 sizeof(pm_reg_msg
.dev_name
));
517 memcpy(pm_reg_msg
.if_name
, cm_id
->device
->iwcm
->ifname
,
518 sizeof(pm_reg_msg
.if_name
));
520 if (iwpm_register_pid(&pm_reg_msg
, RDMA_NL_IWCM
) ||
524 cm_id
->mapped
= true;
525 pm_msg
.loc_addr
= cm_id
->local_addr
;
526 pm_msg
.rem_addr
= cm_id
->remote_addr
;
528 status
= iwpm_add_and_query_mapping(&pm_msg
,
531 status
= iwpm_add_mapping(&pm_msg
, RDMA_NL_IWCM
);
534 cm_id
->m_local_addr
= pm_msg
.mapped_loc_addr
;
536 cm_id
->m_remote_addr
= pm_msg
.mapped_rem_addr
;
537 iw_cm_check_wildcard(&pm_msg
.mapped_rem_addr
,
539 &cm_id
->m_remote_addr
);
543 return iwpm_create_mapinfo(&cm_id
->local_addr
,
544 &cm_id
->m_local_addr
,
551 * Start listening for connect requests. Generates one CONNECT_REQUEST
552 * event for each inbound connect request.
554 int iw_cm_listen(struct iw_cm_id
*cm_id
, int backlog
)
556 struct iwcm_id_private
*cm_id_priv
;
560 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
563 backlog
= default_backlog
;
565 ret
= alloc_work_entries(cm_id_priv
, backlog
);
569 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
570 switch (cm_id_priv
->state
) {
571 case IW_CM_STATE_IDLE
:
572 cm_id_priv
->state
= IW_CM_STATE_LISTEN
;
573 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
574 ret
= iw_cm_map(cm_id
, false);
576 ret
= cm_id
->device
->iwcm
->create_listen(cm_id
, backlog
);
578 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
579 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
584 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
588 EXPORT_SYMBOL(iw_cm_listen
);
593 * Rejects an inbound connection request. No events are generated.
595 int iw_cm_reject(struct iw_cm_id
*cm_id
,
596 const void *private_data
,
599 struct iwcm_id_private
*cm_id_priv
;
603 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
604 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
606 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
607 if (cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
) {
608 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
609 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
610 wake_up_all(&cm_id_priv
->connect_wait
);
613 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
614 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
616 ret
= cm_id
->device
->iwcm
->reject(cm_id
, private_data
,
619 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
620 wake_up_all(&cm_id_priv
->connect_wait
);
624 EXPORT_SYMBOL(iw_cm_reject
);
627 * CM_ID <-- ESTABLISHED
629 * Accepts an inbound connection request and generates an ESTABLISHED
630 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
631 * until the ESTABLISHED event is received from the provider.
633 int iw_cm_accept(struct iw_cm_id
*cm_id
,
634 struct iw_cm_conn_param
*iw_param
)
636 struct iwcm_id_private
*cm_id_priv
;
641 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
642 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
644 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
645 if (cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
) {
646 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
647 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
648 wake_up_all(&cm_id_priv
->connect_wait
);
651 /* Get the ib_qp given the QPN */
652 qp
= cm_id
->device
->iwcm
->get_qp(cm_id
->device
, iw_param
->qpn
);
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
);
659 cm_id
->device
->iwcm
->add_ref(qp
);
661 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
663 ret
= cm_id
->device
->iwcm
->accept(cm_id
, iw_param
);
665 /* An error on accept precludes provider events */
666 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
);
667 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
668 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
669 if (cm_id_priv
->qp
) {
670 cm_id
->device
->iwcm
->rem_ref(qp
);
671 cm_id_priv
->qp
= NULL
;
673 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
674 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
675 wake_up_all(&cm_id_priv
->connect_wait
);
680 EXPORT_SYMBOL(iw_cm_accept
);
683 * Active Side: CM_ID <-- CONN_SENT
685 * If successful, results in the generation of a CONNECT_REPLY
686 * event. iw_cm_disconnect and iw_cm_destroy will block until the
687 * CONNECT_REPLY event is received from the provider.
689 int iw_cm_connect(struct iw_cm_id
*cm_id
, struct iw_cm_conn_param
*iw_param
)
691 struct iwcm_id_private
*cm_id_priv
;
696 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
698 ret
= alloc_work_entries(cm_id_priv
, 4);
702 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
703 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
705 if (cm_id_priv
->state
!= IW_CM_STATE_IDLE
) {
710 /* Get the ib_qp given the QPN */
711 qp
= cm_id
->device
->iwcm
->get_qp(cm_id
->device
, iw_param
->qpn
);
716 cm_id
->device
->iwcm
->add_ref(qp
);
718 cm_id_priv
->state
= IW_CM_STATE_CONN_SENT
;
719 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
721 ret
= iw_cm_map(cm_id
, true);
723 ret
= cm_id
->device
->iwcm
->connect(cm_id
, iw_param
);
725 return 0; /* success */
727 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
728 if (cm_id_priv
->qp
) {
729 cm_id
->device
->iwcm
->rem_ref(qp
);
730 cm_id_priv
->qp
= NULL
;
732 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
734 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
735 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
736 wake_up_all(&cm_id_priv
->connect_wait
);
739 EXPORT_SYMBOL(iw_cm_connect
);
742 * Passive Side: new CM_ID <-- CONN_RECV
744 * Handles an inbound connect request. The function creates a new
745 * iw_cm_id to represent the new connection and inherits the client
746 * callback function and other attributes from the listening parent.
748 * The work item contains a pointer to the listen_cm_id and the event. The
749 * listen_cm_id contains the client cm_handler, context and
750 * device. These are copied when the device is cloned. The event
751 * contains the new four tuple.
753 * An error on the child should not affect the parent, so this
754 * function does not return a value.
756 static void cm_conn_req_handler(struct iwcm_id_private
*listen_id_priv
,
757 struct iw_cm_event
*iw_event
)
760 struct iw_cm_id
*cm_id
;
761 struct iwcm_id_private
*cm_id_priv
;
765 * The provider should never generate a connection request
766 * event with a bad status.
768 BUG_ON(iw_event
->status
);
770 cm_id
= iw_create_cm_id(listen_id_priv
->id
.device
,
771 listen_id_priv
->id
.cm_handler
,
772 listen_id_priv
->id
.context
);
773 /* If the cm_id could not be created, ignore the request */
777 cm_id
->provider_data
= iw_event
->provider_data
;
778 cm_id
->m_local_addr
= iw_event
->local_addr
;
779 cm_id
->m_remote_addr
= iw_event
->remote_addr
;
780 cm_id
->local_addr
= listen_id_priv
->id
.local_addr
;
782 ret
= iwpm_get_remote_info(&listen_id_priv
->id
.m_local_addr
,
783 &iw_event
->remote_addr
,
787 cm_id
->remote_addr
= iw_event
->remote_addr
;
789 iw_cm_check_wildcard(&listen_id_priv
->id
.m_local_addr
,
790 &iw_event
->local_addr
,
792 iw_event
->local_addr
= cm_id
->local_addr
;
793 iw_event
->remote_addr
= cm_id
->remote_addr
;
796 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
797 cm_id_priv
->state
= IW_CM_STATE_CONN_RECV
;
800 * We could be destroying the listening id. If so, ignore this
803 spin_lock_irqsave(&listen_id_priv
->lock
, flags
);
804 if (listen_id_priv
->state
!= IW_CM_STATE_LISTEN
) {
805 spin_unlock_irqrestore(&listen_id_priv
->lock
, flags
);
806 iw_cm_reject(cm_id
, NULL
, 0);
807 iw_destroy_cm_id(cm_id
);
810 spin_unlock_irqrestore(&listen_id_priv
->lock
, flags
);
812 ret
= alloc_work_entries(cm_id_priv
, 3);
814 iw_cm_reject(cm_id
, NULL
, 0);
815 iw_destroy_cm_id(cm_id
);
819 /* Call the client CM handler */
820 ret
= cm_id
->cm_handler(cm_id
, iw_event
);
822 iw_cm_reject(cm_id
, NULL
, 0);
823 iw_destroy_cm_id(cm_id
);
827 if (iw_event
->private_data_len
)
828 kfree(iw_event
->private_data
);
832 * Passive Side: CM_ID <-- ESTABLISHED
834 * The provider generated an ESTABLISHED event which means that
835 * the MPA negotion has completed successfully and we are now in MPA
838 * This event can only be received in the CONN_RECV state. If the
839 * remote peer closed, the ESTABLISHED event would be received followed
840 * by the CLOSE event. If the app closes, it will block until we wake
841 * it up after processing this event.
843 static int cm_conn_est_handler(struct iwcm_id_private
*cm_id_priv
,
844 struct iw_cm_event
*iw_event
)
849 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
852 * We clear the CONNECT_WAIT bit here to allow the callback
853 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
854 * from a callback handler is not allowed.
856 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
857 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
);
858 cm_id_priv
->state
= IW_CM_STATE_ESTABLISHED
;
859 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
860 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
861 wake_up_all(&cm_id_priv
->connect_wait
);
867 * Active Side: CM_ID <-- ESTABLISHED
869 * The app has called connect and is waiting for the established event to
870 * post it's requests to the server. This event will wake up anyone
871 * blocked in iw_cm_disconnect or iw_destroy_id.
873 static int cm_conn_rep_handler(struct iwcm_id_private
*cm_id_priv
,
874 struct iw_cm_event
*iw_event
)
879 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
881 * Clear the connect wait bit so a callback function calling
882 * iw_cm_disconnect will not wait and deadlock this thread
884 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
885 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_SENT
);
886 if (iw_event
->status
== 0) {
887 cm_id_priv
->id
.m_local_addr
= iw_event
->local_addr
;
888 cm_id_priv
->id
.m_remote_addr
= iw_event
->remote_addr
;
889 iw_event
->local_addr
= cm_id_priv
->id
.local_addr
;
890 iw_event
->remote_addr
= cm_id_priv
->id
.remote_addr
;
891 cm_id_priv
->state
= IW_CM_STATE_ESTABLISHED
;
893 /* REJECTED or RESET */
894 cm_id_priv
->id
.device
->iwcm
->rem_ref(cm_id_priv
->qp
);
895 cm_id_priv
->qp
= NULL
;
896 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
898 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
899 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
901 if (iw_event
->private_data_len
)
902 kfree(iw_event
->private_data
);
904 /* Wake up waiters on connect complete */
905 wake_up_all(&cm_id_priv
->connect_wait
);
913 * If in the ESTABLISHED state, move to CLOSING.
915 static void cm_disconnect_handler(struct iwcm_id_private
*cm_id_priv
,
916 struct iw_cm_event
*iw_event
)
920 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
921 if (cm_id_priv
->state
== IW_CM_STATE_ESTABLISHED
)
922 cm_id_priv
->state
= IW_CM_STATE_CLOSING
;
923 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
929 * If in the ESTBLISHED or CLOSING states, the QP will have have been
930 * moved by the provider to the ERR state. Disassociate the CM_ID from
931 * the QP, move to IDLE, and remove the 'connected' reference.
933 * If in some other state, the cm_id was destroyed asynchronously.
934 * This is the last reference that will result in waking up
935 * the app thread blocked in iw_destroy_cm_id.
937 static int cm_close_handler(struct iwcm_id_private
*cm_id_priv
,
938 struct iw_cm_event
*iw_event
)
942 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
944 if (cm_id_priv
->qp
) {
945 cm_id_priv
->id
.device
->iwcm
->rem_ref(cm_id_priv
->qp
);
946 cm_id_priv
->qp
= NULL
;
948 switch (cm_id_priv
->state
) {
949 case IW_CM_STATE_ESTABLISHED
:
950 case IW_CM_STATE_CLOSING
:
951 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
952 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
953 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
954 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
956 case IW_CM_STATE_DESTROYING
:
961 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
966 static int process_event(struct iwcm_id_private
*cm_id_priv
,
967 struct iw_cm_event
*iw_event
)
971 switch (iw_event
->event
) {
972 case IW_CM_EVENT_CONNECT_REQUEST
:
973 cm_conn_req_handler(cm_id_priv
, iw_event
);
975 case IW_CM_EVENT_CONNECT_REPLY
:
976 ret
= cm_conn_rep_handler(cm_id_priv
, iw_event
);
978 case IW_CM_EVENT_ESTABLISHED
:
979 ret
= cm_conn_est_handler(cm_id_priv
, iw_event
);
981 case IW_CM_EVENT_DISCONNECT
:
982 cm_disconnect_handler(cm_id_priv
, iw_event
);
984 case IW_CM_EVENT_CLOSE
:
985 ret
= cm_close_handler(cm_id_priv
, iw_event
);
995 * Process events on the work_list for the cm_id. If the callback
996 * function requests that the cm_id be deleted, a flag is set in the
997 * cm_id flags to indicate that when the last reference is
998 * removed, the cm_id is to be destroyed. This is necessary to
999 * distinguish between an object that will be destroyed by the app
1000 * thread asleep on the destroy_comp list vs. an object destroyed
1001 * here synchronously when the last reference is removed.
1003 static void cm_work_handler(struct work_struct
*_work
)
1005 struct iwcm_work
*work
= container_of(_work
, struct iwcm_work
, work
);
1006 struct iw_cm_event levent
;
1007 struct iwcm_id_private
*cm_id_priv
= work
->cm_id
;
1008 unsigned long flags
;
1012 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1013 empty
= list_empty(&cm_id_priv
->work_list
);
1015 work
= list_entry(cm_id_priv
->work_list
.next
,
1016 struct iwcm_work
, list
);
1017 list_del_init(&work
->list
);
1018 empty
= list_empty(&cm_id_priv
->work_list
);
1019 levent
= work
->event
;
1021 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1023 if (!test_bit(IWCM_F_DROP_EVENTS
, &cm_id_priv
->flags
)) {
1024 ret
= process_event(cm_id_priv
, &levent
);
1026 destroy_cm_id(&cm_id_priv
->id
);
1028 pr_debug("dropping event %d\n", levent
.event
);
1029 if (iwcm_deref_id(cm_id_priv
))
1033 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1035 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1039 * This function is called on interrupt context. Schedule events on
1040 * the iwcm_wq thread to allow callback functions to downcall into
1041 * the CM and/or block. Events are queued to a per-CM_ID
1042 * work_list. If this is the first event on the work_list, the work
1043 * element is also queued on the iwcm_wq thread.
1045 * Each event holds a reference on the cm_id. Until the last posted
1046 * event has been delivered and processed, the cm_id cannot be
1050 * 0 - the event was handled.
1051 * -ENOMEM - the event was not handled due to lack of resources.
1053 static int cm_event_handler(struct iw_cm_id
*cm_id
,
1054 struct iw_cm_event
*iw_event
)
1056 struct iwcm_work
*work
;
1057 struct iwcm_id_private
*cm_id_priv
;
1058 unsigned long flags
;
1061 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
1063 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1064 work
= get_work(cm_id_priv
);
1070 INIT_WORK(&work
->work
, cm_work_handler
);
1071 work
->cm_id
= cm_id_priv
;
1072 work
->event
= *iw_event
;
1074 if ((work
->event
.event
== IW_CM_EVENT_CONNECT_REQUEST
||
1075 work
->event
.event
== IW_CM_EVENT_CONNECT_REPLY
) &&
1076 work
->event
.private_data_len
) {
1077 ret
= copy_private_data(&work
->event
);
1084 atomic_inc(&cm_id_priv
->refcount
);
1085 if (list_empty(&cm_id_priv
->work_list
)) {
1086 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1087 queue_work(iwcm_wq
, &work
->work
);
1089 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1091 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1095 static int iwcm_init_qp_init_attr(struct iwcm_id_private
*cm_id_priv
,
1096 struct ib_qp_attr
*qp_attr
,
1099 unsigned long flags
;
1102 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1103 switch (cm_id_priv
->state
) {
1104 case IW_CM_STATE_IDLE
:
1105 case IW_CM_STATE_CONN_SENT
:
1106 case IW_CM_STATE_CONN_RECV
:
1107 case IW_CM_STATE_ESTABLISHED
:
1108 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1109 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
|
1110 IB_ACCESS_REMOTE_READ
;
1117 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1121 static int iwcm_init_qp_rts_attr(struct iwcm_id_private
*cm_id_priv
,
1122 struct ib_qp_attr
*qp_attr
,
1125 unsigned long flags
;
1128 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1129 switch (cm_id_priv
->state
) {
1130 case IW_CM_STATE_IDLE
:
1131 case IW_CM_STATE_CONN_SENT
:
1132 case IW_CM_STATE_CONN_RECV
:
1133 case IW_CM_STATE_ESTABLISHED
:
1141 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1145 int iw_cm_init_qp_attr(struct iw_cm_id
*cm_id
,
1146 struct ib_qp_attr
*qp_attr
,
1149 struct iwcm_id_private
*cm_id_priv
;
1152 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
1153 switch (qp_attr
->qp_state
) {
1156 ret
= iwcm_init_qp_init_attr(cm_id_priv
,
1157 qp_attr
, qp_attr_mask
);
1160 ret
= iwcm_init_qp_rts_attr(cm_id_priv
,
1161 qp_attr
, qp_attr_mask
);
1169 EXPORT_SYMBOL(iw_cm_init_qp_attr
);
1171 static int __init
iw_cm_init(void)
1175 ret
= iwpm_init(RDMA_NL_IWCM
);
1177 pr_err("iw_cm: couldn't init iwpm\n");
1179 ret
= ibnl_add_client(RDMA_NL_IWCM
, ARRAY_SIZE(iwcm_nl_cb_table
),
1182 pr_err("iw_cm: couldn't register netlink callbacks\n");
1184 iwcm_wq
= alloc_ordered_workqueue("iw_cm_wq", WQ_MEM_RECLAIM
);
1188 iwcm_ctl_table_hdr
= register_net_sysctl(&init_net
, "net/iw_cm",
1190 if (!iwcm_ctl_table_hdr
) {
1191 pr_err("iw_cm: couldn't register sysctl paths\n");
1192 destroy_workqueue(iwcm_wq
);
1199 static void __exit
iw_cm_cleanup(void)
1201 unregister_net_sysctl_table(iwcm_ctl_table_hdr
);
1202 destroy_workqueue(iwcm_wq
);
1203 ibnl_remove_client(RDMA_NL_IWCM
);
1204 iwpm_exit(RDMA_NL_IWCM
);
1207 module_init(iw_cm_init
);
1208 module_exit(iw_cm_cleanup
);