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 rdma_nl_cbs iwcm_nl_cb_table
[RDMA_NL_IWPM_NUM_OPS
] = {
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
},
91 [RDMA_NL_IWPM_HELLO
] = {.dump
= iwpm_hello_cb
}
94 static struct workqueue_struct
*iwcm_wq
;
96 struct work_struct work
;
97 struct iwcm_id_private
*cm_id
;
98 struct list_head list
;
99 struct iw_cm_event event
;
100 struct list_head free_list
;
103 static unsigned int default_backlog
= 256;
105 static struct ctl_table_header
*iwcm_ctl_table_hdr
;
106 static struct ctl_table iwcm_ctl_table
[] = {
108 .procname
= "default_backlog",
109 .data
= &default_backlog
,
110 .maxlen
= sizeof(default_backlog
),
112 .proc_handler
= proc_dointvec
,
118 * The following services provide a mechanism for pre-allocating iwcm_work
119 * elements. The design pre-allocates them based on the cm_id type:
120 * LISTENING IDS: Get enough elements preallocated to handle the
122 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
123 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
125 * Allocating them in connect and listen avoids having to deal
126 * with allocation failures on the event upcall from the provider (which
127 * is called in the interrupt context).
129 * One exception is when creating the cm_id for incoming connection requests.
130 * There are two cases:
131 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
132 * the backlog is exceeded, then no more connection request events will
133 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
134 * to the provider to reject the connection request.
135 * 2) in the connection request workqueue handler, cm_conn_req_handler().
136 * If work elements cannot be allocated for the new connect request cm_id,
137 * then IWCM will call the provider reject method. This is ok since
138 * cm_conn_req_handler() runs in the workqueue thread context.
141 static struct iwcm_work
*get_work(struct iwcm_id_private
*cm_id_priv
)
143 struct iwcm_work
*work
;
145 if (list_empty(&cm_id_priv
->work_free_list
))
147 work
= list_entry(cm_id_priv
->work_free_list
.next
, struct iwcm_work
,
149 list_del_init(&work
->free_list
);
153 static void put_work(struct iwcm_work
*work
)
155 list_add(&work
->free_list
, &work
->cm_id
->work_free_list
);
158 static void dealloc_work_entries(struct iwcm_id_private
*cm_id_priv
)
160 struct list_head
*e
, *tmp
;
162 list_for_each_safe(e
, tmp
, &cm_id_priv
->work_free_list
) {
164 kfree(list_entry(e
, struct iwcm_work
, free_list
));
168 static int alloc_work_entries(struct iwcm_id_private
*cm_id_priv
, int count
)
170 struct iwcm_work
*work
;
172 BUG_ON(!list_empty(&cm_id_priv
->work_free_list
));
174 work
= kmalloc(sizeof(struct iwcm_work
), GFP_KERNEL
);
176 dealloc_work_entries(cm_id_priv
);
179 work
->cm_id
= cm_id_priv
;
180 INIT_LIST_HEAD(&work
->list
);
187 * Save private data from incoming connection requests to
188 * iw_cm_event, so the low level driver doesn't have to. Adjust
189 * the event ptr to point to the local copy.
191 static int copy_private_data(struct iw_cm_event
*event
)
195 p
= kmemdup(event
->private_data
, event
->private_data_len
, GFP_ATOMIC
);
198 event
->private_data
= p
;
202 static void free_cm_id(struct iwcm_id_private
*cm_id_priv
)
204 dealloc_work_entries(cm_id_priv
);
209 * Release a reference on cm_id. If the last reference is being
210 * released, free the cm_id and return 1.
212 static int iwcm_deref_id(struct iwcm_id_private
*cm_id_priv
)
214 BUG_ON(atomic_read(&cm_id_priv
->refcount
)==0);
215 if (atomic_dec_and_test(&cm_id_priv
->refcount
)) {
216 BUG_ON(!list_empty(&cm_id_priv
->work_list
));
217 free_cm_id(cm_id_priv
);
224 static void add_ref(struct iw_cm_id
*cm_id
)
226 struct iwcm_id_private
*cm_id_priv
;
227 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
228 atomic_inc(&cm_id_priv
->refcount
);
231 static void rem_ref(struct iw_cm_id
*cm_id
)
233 struct iwcm_id_private
*cm_id_priv
;
235 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
237 (void)iwcm_deref_id(cm_id_priv
);
240 static int cm_event_handler(struct iw_cm_id
*cm_id
, struct iw_cm_event
*event
);
242 struct iw_cm_id
*iw_create_cm_id(struct ib_device
*device
,
243 iw_cm_handler cm_handler
,
246 struct iwcm_id_private
*cm_id_priv
;
248 cm_id_priv
= kzalloc(sizeof(*cm_id_priv
), GFP_KERNEL
);
250 return ERR_PTR(-ENOMEM
);
252 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
253 cm_id_priv
->id
.device
= device
;
254 cm_id_priv
->id
.cm_handler
= cm_handler
;
255 cm_id_priv
->id
.context
= context
;
256 cm_id_priv
->id
.event_handler
= cm_event_handler
;
257 cm_id_priv
->id
.add_ref
= add_ref
;
258 cm_id_priv
->id
.rem_ref
= rem_ref
;
259 spin_lock_init(&cm_id_priv
->lock
);
260 atomic_set(&cm_id_priv
->refcount
, 1);
261 init_waitqueue_head(&cm_id_priv
->connect_wait
);
262 init_completion(&cm_id_priv
->destroy_comp
);
263 INIT_LIST_HEAD(&cm_id_priv
->work_list
);
264 INIT_LIST_HEAD(&cm_id_priv
->work_free_list
);
266 return &cm_id_priv
->id
;
268 EXPORT_SYMBOL(iw_create_cm_id
);
271 static int iwcm_modify_qp_err(struct ib_qp
*qp
)
273 struct ib_qp_attr qp_attr
;
278 qp_attr
.qp_state
= IB_QPS_ERR
;
279 return ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
283 * This is really the RDMAC CLOSING state. It is most similar to the
286 static int iwcm_modify_qp_sqd(struct ib_qp
*qp
)
288 struct ib_qp_attr qp_attr
;
291 qp_attr
.qp_state
= IB_QPS_SQD
;
292 return ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
298 * Block if a passive or active connection is currently being processed. Then
299 * process the event as follows:
300 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
301 * based on the abrupt flag
302 * - If the connection is already in the CLOSING or IDLE state, the peer is
303 * disconnecting concurrently with us and we've already seen the
304 * DISCONNECT event -- ignore the request and return 0
305 * - Disconnect on a listening endpoint returns -EINVAL
307 int iw_cm_disconnect(struct iw_cm_id
*cm_id
, int abrupt
)
309 struct iwcm_id_private
*cm_id_priv
;
312 struct ib_qp
*qp
= NULL
;
314 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
315 /* Wait if we're currently in a connect or accept downcall */
316 wait_event(cm_id_priv
->connect_wait
,
317 !test_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
));
319 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
320 switch (cm_id_priv
->state
) {
321 case IW_CM_STATE_ESTABLISHED
:
322 cm_id_priv
->state
= IW_CM_STATE_CLOSING
;
324 /* QP could be <nul> for user-mode client */
330 case IW_CM_STATE_LISTEN
:
333 case IW_CM_STATE_CLOSING
:
334 /* remote peer closed first */
335 case IW_CM_STATE_IDLE
:
336 /* accept or connect returned !0 */
338 case IW_CM_STATE_CONN_RECV
:
340 * App called disconnect before/without calling accept after
341 * connect_request event delivered.
344 case IW_CM_STATE_CONN_SENT
:
345 /* Can only get here if wait above fails */
349 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
353 ret
= iwcm_modify_qp_err(qp
);
355 ret
= iwcm_modify_qp_sqd(qp
);
358 * If both sides are disconnecting the QP could
359 * already be in ERR or SQD states
366 EXPORT_SYMBOL(iw_cm_disconnect
);
369 * CM_ID <-- DESTROYING
371 * Clean up all resources associated with the connection and release
372 * the initial reference taken by iw_create_cm_id.
374 static void destroy_cm_id(struct iw_cm_id
*cm_id
)
376 struct iwcm_id_private
*cm_id_priv
;
380 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
382 * Wait if we're currently in a connect or accept downcall. A
383 * listening endpoint should never block here.
385 wait_event(cm_id_priv
->connect_wait
,
386 !test_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
));
389 * Since we're deleting the cm_id, drop any events that
390 * might arrive before the last dereference.
392 set_bit(IWCM_F_DROP_EVENTS
, &cm_id_priv
->flags
);
394 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
396 cm_id_priv
->qp
= NULL
;
398 switch (cm_id_priv
->state
) {
399 case IW_CM_STATE_LISTEN
:
400 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
401 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
402 /* destroy the listening endpoint */
403 cm_id
->device
->ops
.iw_destroy_listen(cm_id
);
404 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
406 case IW_CM_STATE_ESTABLISHED
:
407 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
408 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
409 /* Abrupt close of the connection */
410 (void)iwcm_modify_qp_err(qp
);
411 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
413 case IW_CM_STATE_IDLE
:
414 case IW_CM_STATE_CLOSING
:
415 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
417 case IW_CM_STATE_CONN_RECV
:
419 * App called destroy before/without calling accept after
420 * receiving connection request event notification or
421 * returned non zero from the event callback function.
422 * In either case, must tell the provider to reject.
424 cm_id_priv
->state
= IW_CM_STATE_DESTROYING
;
425 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
426 cm_id
->device
->ops
.iw_reject(cm_id
, NULL
, 0);
427 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
429 case IW_CM_STATE_CONN_SENT
:
430 case IW_CM_STATE_DESTROYING
:
435 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
437 cm_id_priv
->id
.device
->ops
.iw_rem_ref(qp
);
440 iwpm_remove_mapinfo(&cm_id
->local_addr
, &cm_id
->m_local_addr
);
441 iwpm_remove_mapping(&cm_id
->local_addr
, RDMA_NL_IWCM
);
444 (void)iwcm_deref_id(cm_id_priv
);
448 * This function is only called by the application thread and cannot
449 * be called by the event thread. The function will wait for all
450 * references to be released on the cm_id and then kfree the cm_id
453 void iw_destroy_cm_id(struct iw_cm_id
*cm_id
)
455 destroy_cm_id(cm_id
);
457 EXPORT_SYMBOL(iw_destroy_cm_id
);
460 * iw_cm_check_wildcard - If IP address is 0 then use original
461 * @pm_addr: sockaddr containing the ip to check for wildcard
462 * @cm_addr: sockaddr containing the actual IP address
463 * @cm_outaddr: sockaddr to set IP addr which leaving port
465 * Checks the pm_addr for wildcard and then sets cm_outaddr's
466 * IP to the actual (cm_addr).
468 static void iw_cm_check_wildcard(struct sockaddr_storage
*pm_addr
,
469 struct sockaddr_storage
*cm_addr
,
470 struct sockaddr_storage
*cm_outaddr
)
472 if (pm_addr
->ss_family
== AF_INET
) {
473 struct sockaddr_in
*pm4_addr
= (struct sockaddr_in
*)pm_addr
;
475 if (pm4_addr
->sin_addr
.s_addr
== htonl(INADDR_ANY
)) {
476 struct sockaddr_in
*cm4_addr
=
477 (struct sockaddr_in
*)cm_addr
;
478 struct sockaddr_in
*cm4_outaddr
=
479 (struct sockaddr_in
*)cm_outaddr
;
481 cm4_outaddr
->sin_addr
= cm4_addr
->sin_addr
;
484 struct sockaddr_in6
*pm6_addr
= (struct sockaddr_in6
*)pm_addr
;
486 if (ipv6_addr_type(&pm6_addr
->sin6_addr
) == IPV6_ADDR_ANY
) {
487 struct sockaddr_in6
*cm6_addr
=
488 (struct sockaddr_in6
*)cm_addr
;
489 struct sockaddr_in6
*cm6_outaddr
=
490 (struct sockaddr_in6
*)cm_outaddr
;
492 cm6_outaddr
->sin6_addr
= cm6_addr
->sin6_addr
;
498 * iw_cm_map - Use portmapper to map the ports
499 * @cm_id: connection manager pointer
500 * @active: Indicates the active side when true
501 * returns nonzero for error only if iwpm_create_mapinfo() fails
503 * Tries to add a mapping for a port using the Portmapper. If
504 * successful in mapping the IP/Port it will check the remote
505 * mapped IP address for a wildcard IP address and replace the
506 * zero IP address with the remote_addr.
508 static int iw_cm_map(struct iw_cm_id
*cm_id
, bool active
)
510 const char *devname
= dev_name(&cm_id
->device
->dev
);
511 const char *ifname
= cm_id
->device
->iw_ifname
;
512 struct iwpm_dev_data pm_reg_msg
= {};
513 struct iwpm_sa_data pm_msg
;
516 if (strlen(devname
) >= sizeof(pm_reg_msg
.dev_name
) ||
517 strlen(ifname
) >= sizeof(pm_reg_msg
.if_name
))
520 cm_id
->m_local_addr
= cm_id
->local_addr
;
521 cm_id
->m_remote_addr
= cm_id
->remote_addr
;
523 strcpy(pm_reg_msg
.dev_name
, devname
);
524 strcpy(pm_reg_msg
.if_name
, ifname
);
526 if (iwpm_register_pid(&pm_reg_msg
, RDMA_NL_IWCM
) ||
530 cm_id
->mapped
= true;
531 pm_msg
.loc_addr
= cm_id
->local_addr
;
532 pm_msg
.rem_addr
= cm_id
->remote_addr
;
533 pm_msg
.flags
= (cm_id
->device
->iw_driver_flags
& IW_F_NO_PORT_MAP
) ?
534 IWPM_FLAGS_NO_PORT_MAP
: 0;
536 status
= iwpm_add_and_query_mapping(&pm_msg
,
539 status
= iwpm_add_mapping(&pm_msg
, RDMA_NL_IWCM
);
542 cm_id
->m_local_addr
= pm_msg
.mapped_loc_addr
;
544 cm_id
->m_remote_addr
= pm_msg
.mapped_rem_addr
;
545 iw_cm_check_wildcard(&pm_msg
.mapped_rem_addr
,
547 &cm_id
->m_remote_addr
);
551 return iwpm_create_mapinfo(&cm_id
->local_addr
,
552 &cm_id
->m_local_addr
,
553 RDMA_NL_IWCM
, pm_msg
.flags
);
559 * Start listening for connect requests. Generates one CONNECT_REQUEST
560 * event for each inbound connect request.
562 int iw_cm_listen(struct iw_cm_id
*cm_id
, int backlog
)
564 struct iwcm_id_private
*cm_id_priv
;
568 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
571 backlog
= default_backlog
;
573 ret
= alloc_work_entries(cm_id_priv
, backlog
);
577 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
578 switch (cm_id_priv
->state
) {
579 case IW_CM_STATE_IDLE
:
580 cm_id_priv
->state
= IW_CM_STATE_LISTEN
;
581 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
582 ret
= iw_cm_map(cm_id
, false);
584 ret
= cm_id
->device
->ops
.iw_create_listen(cm_id
,
587 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
588 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
593 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
597 EXPORT_SYMBOL(iw_cm_listen
);
602 * Rejects an inbound connection request. No events are generated.
604 int iw_cm_reject(struct iw_cm_id
*cm_id
,
605 const void *private_data
,
608 struct iwcm_id_private
*cm_id_priv
;
612 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
613 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
615 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
616 if (cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
) {
617 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
618 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
619 wake_up_all(&cm_id_priv
->connect_wait
);
622 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
623 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
625 ret
= cm_id
->device
->ops
.iw_reject(cm_id
, private_data
,
628 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
629 wake_up_all(&cm_id_priv
->connect_wait
);
633 EXPORT_SYMBOL(iw_cm_reject
);
636 * CM_ID <-- ESTABLISHED
638 * Accepts an inbound connection request and generates an ESTABLISHED
639 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
640 * until the ESTABLISHED event is received from the provider.
642 int iw_cm_accept(struct iw_cm_id
*cm_id
,
643 struct iw_cm_conn_param
*iw_param
)
645 struct iwcm_id_private
*cm_id_priv
;
650 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
651 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
653 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
654 if (cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
) {
655 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
656 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
657 wake_up_all(&cm_id_priv
->connect_wait
);
660 /* Get the ib_qp given the QPN */
661 qp
= cm_id
->device
->ops
.iw_get_qp(cm_id
->device
, iw_param
->qpn
);
663 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
664 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
665 wake_up_all(&cm_id_priv
->connect_wait
);
668 cm_id
->device
->ops
.iw_add_ref(qp
);
670 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
672 ret
= cm_id
->device
->ops
.iw_accept(cm_id
, iw_param
);
674 /* An error on accept precludes provider events */
675 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
);
676 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
677 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
679 cm_id_priv
->qp
= NULL
;
680 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
682 cm_id
->device
->ops
.iw_rem_ref(qp
);
683 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
684 wake_up_all(&cm_id_priv
->connect_wait
);
689 EXPORT_SYMBOL(iw_cm_accept
);
692 * Active Side: CM_ID <-- CONN_SENT
694 * If successful, results in the generation of a CONNECT_REPLY
695 * event. iw_cm_disconnect and iw_cm_destroy will block until the
696 * CONNECT_REPLY event is received from the provider.
698 int iw_cm_connect(struct iw_cm_id
*cm_id
, struct iw_cm_conn_param
*iw_param
)
700 struct iwcm_id_private
*cm_id_priv
;
703 struct ib_qp
*qp
= NULL
;
705 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
707 ret
= alloc_work_entries(cm_id_priv
, 4);
711 set_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
712 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
714 if (cm_id_priv
->state
!= IW_CM_STATE_IDLE
) {
719 /* Get the ib_qp given the QPN */
720 qp
= cm_id
->device
->ops
.iw_get_qp(cm_id
->device
, iw_param
->qpn
);
725 cm_id
->device
->ops
.iw_add_ref(qp
);
727 cm_id_priv
->state
= IW_CM_STATE_CONN_SENT
;
728 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
730 ret
= iw_cm_map(cm_id
, true);
732 ret
= cm_id
->device
->ops
.iw_connect(cm_id
, iw_param
);
734 return 0; /* success */
736 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
738 cm_id_priv
->qp
= NULL
;
739 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
741 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
743 cm_id
->device
->ops
.iw_rem_ref(qp
);
744 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
745 wake_up_all(&cm_id_priv
->connect_wait
);
748 EXPORT_SYMBOL(iw_cm_connect
);
751 * Passive Side: new CM_ID <-- CONN_RECV
753 * Handles an inbound connect request. The function creates a new
754 * iw_cm_id to represent the new connection and inherits the client
755 * callback function and other attributes from the listening parent.
757 * The work item contains a pointer to the listen_cm_id and the event. The
758 * listen_cm_id contains the client cm_handler, context and
759 * device. These are copied when the device is cloned. The event
760 * contains the new four tuple.
762 * An error on the child should not affect the parent, so this
763 * function does not return a value.
765 static void cm_conn_req_handler(struct iwcm_id_private
*listen_id_priv
,
766 struct iw_cm_event
*iw_event
)
769 struct iw_cm_id
*cm_id
;
770 struct iwcm_id_private
*cm_id_priv
;
774 * The provider should never generate a connection request
775 * event with a bad status.
777 BUG_ON(iw_event
->status
);
779 cm_id
= iw_create_cm_id(listen_id_priv
->id
.device
,
780 listen_id_priv
->id
.cm_handler
,
781 listen_id_priv
->id
.context
);
782 /* If the cm_id could not be created, ignore the request */
786 cm_id
->provider_data
= iw_event
->provider_data
;
787 cm_id
->m_local_addr
= iw_event
->local_addr
;
788 cm_id
->m_remote_addr
= iw_event
->remote_addr
;
789 cm_id
->local_addr
= listen_id_priv
->id
.local_addr
;
791 ret
= iwpm_get_remote_info(&listen_id_priv
->id
.m_local_addr
,
792 &iw_event
->remote_addr
,
796 cm_id
->remote_addr
= iw_event
->remote_addr
;
798 iw_cm_check_wildcard(&listen_id_priv
->id
.m_local_addr
,
799 &iw_event
->local_addr
,
801 iw_event
->local_addr
= cm_id
->local_addr
;
802 iw_event
->remote_addr
= cm_id
->remote_addr
;
805 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
806 cm_id_priv
->state
= IW_CM_STATE_CONN_RECV
;
809 * We could be destroying the listening id. If so, ignore this
812 spin_lock_irqsave(&listen_id_priv
->lock
, flags
);
813 if (listen_id_priv
->state
!= IW_CM_STATE_LISTEN
) {
814 spin_unlock_irqrestore(&listen_id_priv
->lock
, flags
);
815 iw_cm_reject(cm_id
, NULL
, 0);
816 iw_destroy_cm_id(cm_id
);
819 spin_unlock_irqrestore(&listen_id_priv
->lock
, flags
);
821 ret
= alloc_work_entries(cm_id_priv
, 3);
823 iw_cm_reject(cm_id
, NULL
, 0);
824 iw_destroy_cm_id(cm_id
);
828 /* Call the client CM handler */
829 ret
= cm_id
->cm_handler(cm_id
, iw_event
);
831 iw_cm_reject(cm_id
, NULL
, 0);
832 iw_destroy_cm_id(cm_id
);
836 if (iw_event
->private_data_len
)
837 kfree(iw_event
->private_data
);
841 * Passive Side: CM_ID <-- ESTABLISHED
843 * The provider generated an ESTABLISHED event which means that
844 * the MPA negotion has completed successfully and we are now in MPA
847 * This event can only be received in the CONN_RECV state. If the
848 * remote peer closed, the ESTABLISHED event would be received followed
849 * by the CLOSE event. If the app closes, it will block until we wake
850 * it up after processing this event.
852 static int cm_conn_est_handler(struct iwcm_id_private
*cm_id_priv
,
853 struct iw_cm_event
*iw_event
)
858 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
861 * We clear the CONNECT_WAIT bit here to allow the callback
862 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
863 * from a callback handler is not allowed.
865 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
866 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_RECV
);
867 cm_id_priv
->state
= IW_CM_STATE_ESTABLISHED
;
868 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
869 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
870 wake_up_all(&cm_id_priv
->connect_wait
);
876 * Active Side: CM_ID <-- ESTABLISHED
878 * The app has called connect and is waiting for the established event to
879 * post it's requests to the server. This event will wake up anyone
880 * blocked in iw_cm_disconnect or iw_destroy_id.
882 static int cm_conn_rep_handler(struct iwcm_id_private
*cm_id_priv
,
883 struct iw_cm_event
*iw_event
)
885 struct ib_qp
*qp
= NULL
;
889 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
891 * Clear the connect wait bit so a callback function calling
892 * iw_cm_disconnect will not wait and deadlock this thread
894 clear_bit(IWCM_F_CONNECT_WAIT
, &cm_id_priv
->flags
);
895 BUG_ON(cm_id_priv
->state
!= IW_CM_STATE_CONN_SENT
);
896 if (iw_event
->status
== 0) {
897 cm_id_priv
->id
.m_local_addr
= iw_event
->local_addr
;
898 cm_id_priv
->id
.m_remote_addr
= iw_event
->remote_addr
;
899 iw_event
->local_addr
= cm_id_priv
->id
.local_addr
;
900 iw_event
->remote_addr
= cm_id_priv
->id
.remote_addr
;
901 cm_id_priv
->state
= IW_CM_STATE_ESTABLISHED
;
903 /* REJECTED or RESET */
905 cm_id_priv
->qp
= NULL
;
906 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
908 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
910 cm_id_priv
->id
.device
->ops
.iw_rem_ref(qp
);
911 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
913 if (iw_event
->private_data_len
)
914 kfree(iw_event
->private_data
);
916 /* Wake up waiters on connect complete */
917 wake_up_all(&cm_id_priv
->connect_wait
);
925 * If in the ESTABLISHED state, move to CLOSING.
927 static void cm_disconnect_handler(struct iwcm_id_private
*cm_id_priv
,
928 struct iw_cm_event
*iw_event
)
932 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
933 if (cm_id_priv
->state
== IW_CM_STATE_ESTABLISHED
)
934 cm_id_priv
->state
= IW_CM_STATE_CLOSING
;
935 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
941 * If in the ESTBLISHED or CLOSING states, the QP will have have been
942 * moved by the provider to the ERR state. Disassociate the CM_ID from
943 * the QP, move to IDLE, and remove the 'connected' reference.
945 * If in some other state, the cm_id was destroyed asynchronously.
946 * This is the last reference that will result in waking up
947 * the app thread blocked in iw_destroy_cm_id.
949 static int cm_close_handler(struct iwcm_id_private
*cm_id_priv
,
950 struct iw_cm_event
*iw_event
)
954 int ret
= 0, notify_event
= 0;
955 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
957 cm_id_priv
->qp
= NULL
;
959 switch (cm_id_priv
->state
) {
960 case IW_CM_STATE_ESTABLISHED
:
961 case IW_CM_STATE_CLOSING
:
962 cm_id_priv
->state
= IW_CM_STATE_IDLE
;
965 case IW_CM_STATE_DESTROYING
:
970 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
973 cm_id_priv
->id
.device
->ops
.iw_rem_ref(qp
);
975 ret
= cm_id_priv
->id
.cm_handler(&cm_id_priv
->id
, iw_event
);
979 static int process_event(struct iwcm_id_private
*cm_id_priv
,
980 struct iw_cm_event
*iw_event
)
984 switch (iw_event
->event
) {
985 case IW_CM_EVENT_CONNECT_REQUEST
:
986 cm_conn_req_handler(cm_id_priv
, iw_event
);
988 case IW_CM_EVENT_CONNECT_REPLY
:
989 ret
= cm_conn_rep_handler(cm_id_priv
, iw_event
);
991 case IW_CM_EVENT_ESTABLISHED
:
992 ret
= cm_conn_est_handler(cm_id_priv
, iw_event
);
994 case IW_CM_EVENT_DISCONNECT
:
995 cm_disconnect_handler(cm_id_priv
, iw_event
);
997 case IW_CM_EVENT_CLOSE
:
998 ret
= cm_close_handler(cm_id_priv
, iw_event
);
1008 * Process events on the work_list for the cm_id. If the callback
1009 * function requests that the cm_id be deleted, a flag is set in the
1010 * cm_id flags to indicate that when the last reference is
1011 * removed, the cm_id is to be destroyed. This is necessary to
1012 * distinguish between an object that will be destroyed by the app
1013 * thread asleep on the destroy_comp list vs. an object destroyed
1014 * here synchronously when the last reference is removed.
1016 static void cm_work_handler(struct work_struct
*_work
)
1018 struct iwcm_work
*work
= container_of(_work
, struct iwcm_work
, work
);
1019 struct iw_cm_event levent
;
1020 struct iwcm_id_private
*cm_id_priv
= work
->cm_id
;
1021 unsigned long flags
;
1025 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1026 empty
= list_empty(&cm_id_priv
->work_list
);
1028 work
= list_entry(cm_id_priv
->work_list
.next
,
1029 struct iwcm_work
, list
);
1030 list_del_init(&work
->list
);
1031 empty
= list_empty(&cm_id_priv
->work_list
);
1032 levent
= work
->event
;
1034 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1036 if (!test_bit(IWCM_F_DROP_EVENTS
, &cm_id_priv
->flags
)) {
1037 ret
= process_event(cm_id_priv
, &levent
);
1039 destroy_cm_id(&cm_id_priv
->id
);
1041 pr_debug("dropping event %d\n", levent
.event
);
1042 if (iwcm_deref_id(cm_id_priv
))
1046 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1048 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1052 * This function is called on interrupt context. Schedule events on
1053 * the iwcm_wq thread to allow callback functions to downcall into
1054 * the CM and/or block. Events are queued to a per-CM_ID
1055 * work_list. If this is the first event on the work_list, the work
1056 * element is also queued on the iwcm_wq thread.
1058 * Each event holds a reference on the cm_id. Until the last posted
1059 * event has been delivered and processed, the cm_id cannot be
1063 * 0 - the event was handled.
1064 * -ENOMEM - the event was not handled due to lack of resources.
1066 static int cm_event_handler(struct iw_cm_id
*cm_id
,
1067 struct iw_cm_event
*iw_event
)
1069 struct iwcm_work
*work
;
1070 struct iwcm_id_private
*cm_id_priv
;
1071 unsigned long flags
;
1074 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
1076 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1077 work
= get_work(cm_id_priv
);
1083 INIT_WORK(&work
->work
, cm_work_handler
);
1084 work
->cm_id
= cm_id_priv
;
1085 work
->event
= *iw_event
;
1087 if ((work
->event
.event
== IW_CM_EVENT_CONNECT_REQUEST
||
1088 work
->event
.event
== IW_CM_EVENT_CONNECT_REPLY
) &&
1089 work
->event
.private_data_len
) {
1090 ret
= copy_private_data(&work
->event
);
1097 atomic_inc(&cm_id_priv
->refcount
);
1098 if (list_empty(&cm_id_priv
->work_list
)) {
1099 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1100 queue_work(iwcm_wq
, &work
->work
);
1102 list_add_tail(&work
->list
, &cm_id_priv
->work_list
);
1104 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1108 static int iwcm_init_qp_init_attr(struct iwcm_id_private
*cm_id_priv
,
1109 struct ib_qp_attr
*qp_attr
,
1112 unsigned long flags
;
1115 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1116 switch (cm_id_priv
->state
) {
1117 case IW_CM_STATE_IDLE
:
1118 case IW_CM_STATE_CONN_SENT
:
1119 case IW_CM_STATE_CONN_RECV
:
1120 case IW_CM_STATE_ESTABLISHED
:
1121 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1122 qp_attr
->qp_access_flags
= IB_ACCESS_REMOTE_WRITE
|
1123 IB_ACCESS_REMOTE_READ
;
1130 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1134 static int iwcm_init_qp_rts_attr(struct iwcm_id_private
*cm_id_priv
,
1135 struct ib_qp_attr
*qp_attr
,
1138 unsigned long flags
;
1141 spin_lock_irqsave(&cm_id_priv
->lock
, flags
);
1142 switch (cm_id_priv
->state
) {
1143 case IW_CM_STATE_IDLE
:
1144 case IW_CM_STATE_CONN_SENT
:
1145 case IW_CM_STATE_CONN_RECV
:
1146 case IW_CM_STATE_ESTABLISHED
:
1154 spin_unlock_irqrestore(&cm_id_priv
->lock
, flags
);
1158 int iw_cm_init_qp_attr(struct iw_cm_id
*cm_id
,
1159 struct ib_qp_attr
*qp_attr
,
1162 struct iwcm_id_private
*cm_id_priv
;
1165 cm_id_priv
= container_of(cm_id
, struct iwcm_id_private
, id
);
1166 switch (qp_attr
->qp_state
) {
1169 ret
= iwcm_init_qp_init_attr(cm_id_priv
,
1170 qp_attr
, qp_attr_mask
);
1173 ret
= iwcm_init_qp_rts_attr(cm_id_priv
,
1174 qp_attr
, qp_attr_mask
);
1182 EXPORT_SYMBOL(iw_cm_init_qp_attr
);
1184 static int __init
iw_cm_init(void)
1188 ret
= iwpm_init(RDMA_NL_IWCM
);
1190 pr_err("iw_cm: couldn't init iwpm\n");
1192 rdma_nl_register(RDMA_NL_IWCM
, iwcm_nl_cb_table
);
1193 iwcm_wq
= alloc_ordered_workqueue("iw_cm_wq", 0);
1197 iwcm_ctl_table_hdr
= register_net_sysctl(&init_net
, "net/iw_cm",
1199 if (!iwcm_ctl_table_hdr
) {
1200 pr_err("iw_cm: couldn't register sysctl paths\n");
1201 destroy_workqueue(iwcm_wq
);
1208 static void __exit
iw_cm_cleanup(void)
1210 unregister_net_sysctl_table(iwcm_ctl_table_hdr
);
1211 destroy_workqueue(iwcm_wq
);
1212 rdma_nl_unregister(RDMA_NL_IWCM
);
1213 iwpm_exit(RDMA_NL_IWCM
);
1216 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_IWCM
, 2);
1218 module_init(iw_cm_init
);
1219 module_exit(iw_cm_cleanup
);