RDMA/iwcm: Remove unnecessary function argument
[deliverable/linux.git] / drivers / infiniband / core / iwcm.c
CommitLineData
922a8e9f
TT
1/*
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.
8 *
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:
14 *
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
18 *
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer.
22 *
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.
27 *
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
35 * SOFTWARE.
36 *
37 */
38#include <linux/dma-mapping.h>
39#include <linux/err.h>
40#include <linux/idr.h>
41#include <linux/interrupt.h>
42#include <linux/pci.h>
43#include <linux/rbtree.h>
44#include <linux/spinlock.h>
45#include <linux/workqueue.h>
46#include <linux/completion.h>
47
48#include <rdma/iw_cm.h>
49#include <rdma/ib_addr.h>
50
51#include "iwcm.h"
52
53MODULE_AUTHOR("Tom Tucker");
54MODULE_DESCRIPTION("iWARP CM");
55MODULE_LICENSE("Dual BSD/GPL");
56
57static struct workqueue_struct *iwcm_wq;
58struct iwcm_work {
59 struct work_struct work;
60 struct iwcm_id_private *cm_id;
61 struct list_head list;
62 struct iw_cm_event event;
63 struct list_head free_list;
64};
65
66/*
67 * The following services provide a mechanism for pre-allocating iwcm_work
68 * elements. The design pre-allocates them based on the cm_id type:
69 * LISTENING IDS: Get enough elements preallocated to handle the
70 * listen backlog.
71 * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE
72 * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE
73 *
74 * Allocating them in connect and listen avoids having to deal
75 * with allocation failures on the event upcall from the provider (which
76 * is called in the interrupt context).
77 *
78 * One exception is when creating the cm_id for incoming connection requests.
79 * There are two cases:
80 * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If
81 * the backlog is exceeded, then no more connection request events will
82 * be processed. cm_event_handler() returns -ENOMEM in this case. Its up
715a588f 83 * to the provider to reject the connection request.
922a8e9f
TT
84 * 2) in the connection request workqueue handler, cm_conn_req_handler().
85 * If work elements cannot be allocated for the new connect request cm_id,
86 * then IWCM will call the provider reject method. This is ok since
87 * cm_conn_req_handler() runs in the workqueue thread context.
88 */
89
90static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
91{
92 struct iwcm_work *work;
93
94 if (list_empty(&cm_id_priv->work_free_list))
95 return NULL;
96 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
97 free_list);
98 list_del_init(&work->free_list);
99 return work;
100}
101
102static void put_work(struct iwcm_work *work)
103{
104 list_add(&work->free_list, &work->cm_id->work_free_list);
105}
106
107static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
108{
109 struct list_head *e, *tmp;
110
111 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
112 kfree(list_entry(e, struct iwcm_work, free_list));
113}
114
115static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
116{
117 struct iwcm_work *work;
118
119 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
120 while (count--) {
121 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
122 if (!work) {
123 dealloc_work_entries(cm_id_priv);
124 return -ENOMEM;
125 }
126 work->cm_id = cm_id_priv;
127 INIT_LIST_HEAD(&work->list);
128 put_work(work);
129 }
130 return 0;
131}
132
133/*
715a588f
KK
134 * Save private data from incoming connection requests to
135 * iw_cm_event, so the low level driver doesn't have to. Adjust
922a8e9f
TT
136 * the event ptr to point to the local copy.
137 */
715a588f 138static int copy_private_data(struct iw_cm_event *event)
922a8e9f
TT
139{
140 void *p;
141
bed8bdfd 142 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
922a8e9f
TT
143 if (!p)
144 return -ENOMEM;
922a8e9f
TT
145 event->private_data = p;
146 return 0;
147}
148
149/*
150 * Release a reference on cm_id. If the last reference is being removed
151 * and iw_destroy_cm_id is waiting, wake up the waiting thread.
152 */
153static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
154{
155 int ret = 0;
156
157 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
158 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
159 BUG_ON(!list_empty(&cm_id_priv->work_list));
160 if (waitqueue_active(&cm_id_priv->destroy_comp.wait)) {
161 BUG_ON(cm_id_priv->state != IW_CM_STATE_DESTROYING);
162 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY,
163 &cm_id_priv->flags));
164 ret = 1;
165 }
166 complete(&cm_id_priv->destroy_comp);
167 }
168
169 return ret;
170}
171
172static void add_ref(struct iw_cm_id *cm_id)
173{
174 struct iwcm_id_private *cm_id_priv;
175 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
176 atomic_inc(&cm_id_priv->refcount);
177}
178
179static void rem_ref(struct iw_cm_id *cm_id)
180{
181 struct iwcm_id_private *cm_id_priv;
182 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
183 iwcm_deref_id(cm_id_priv);
184}
185
186static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
187
188struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
189 iw_cm_handler cm_handler,
190 void *context)
191{
192 struct iwcm_id_private *cm_id_priv;
193
194 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
195 if (!cm_id_priv)
196 return ERR_PTR(-ENOMEM);
197
198 cm_id_priv->state = IW_CM_STATE_IDLE;
199 cm_id_priv->id.device = device;
200 cm_id_priv->id.cm_handler = cm_handler;
201 cm_id_priv->id.context = context;
202 cm_id_priv->id.event_handler = cm_event_handler;
203 cm_id_priv->id.add_ref = add_ref;
204 cm_id_priv->id.rem_ref = rem_ref;
205 spin_lock_init(&cm_id_priv->lock);
206 atomic_set(&cm_id_priv->refcount, 1);
207 init_waitqueue_head(&cm_id_priv->connect_wait);
208 init_completion(&cm_id_priv->destroy_comp);
209 INIT_LIST_HEAD(&cm_id_priv->work_list);
210 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
211
212 return &cm_id_priv->id;
213}
214EXPORT_SYMBOL(iw_create_cm_id);
215
216
217static int iwcm_modify_qp_err(struct ib_qp *qp)
218{
219 struct ib_qp_attr qp_attr;
220
221 if (!qp)
222 return -EINVAL;
223
224 qp_attr.qp_state = IB_QPS_ERR;
225 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
226}
227
228/*
229 * This is really the RDMAC CLOSING state. It is most similar to the
230 * IB SQD QP state.
231 */
232static int iwcm_modify_qp_sqd(struct ib_qp *qp)
233{
234 struct ib_qp_attr qp_attr;
235
236 BUG_ON(qp == NULL);
237 qp_attr.qp_state = IB_QPS_SQD;
238 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
239}
240
241/*
242 * CM_ID <-- CLOSING
243 *
715a588f 244 * Block if a passive or active connection is currently being processed. Then
922a8e9f
TT
245 * process the event as follows:
246 * - If we are ESTABLISHED, move to CLOSING and modify the QP state
247 * based on the abrupt flag
248 * - If the connection is already in the CLOSING or IDLE state, the peer is
249 * disconnecting concurrently with us and we've already seen the
250 * DISCONNECT event -- ignore the request and return 0
251 * - Disconnect on a listening endpoint returns -EINVAL
252 */
253int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
254{
255 struct iwcm_id_private *cm_id_priv;
256 unsigned long flags;
257 int ret = 0;
258 struct ib_qp *qp = NULL;
259
260 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
261 /* Wait if we're currently in a connect or accept downcall */
262 wait_event(cm_id_priv->connect_wait,
263 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
264
265 spin_lock_irqsave(&cm_id_priv->lock, flags);
266 switch (cm_id_priv->state) {
267 case IW_CM_STATE_ESTABLISHED:
268 cm_id_priv->state = IW_CM_STATE_CLOSING;
269
270 /* QP could be <nul> for user-mode client */
271 if (cm_id_priv->qp)
272 qp = cm_id_priv->qp;
273 else
274 ret = -EINVAL;
275 break;
276 case IW_CM_STATE_LISTEN:
277 ret = -EINVAL;
278 break;
279 case IW_CM_STATE_CLOSING:
280 /* remote peer closed first */
281 case IW_CM_STATE_IDLE:
282 /* accept or connect returned !0 */
283 break;
284 case IW_CM_STATE_CONN_RECV:
285 /*
286 * App called disconnect before/without calling accept after
287 * connect_request event delivered.
288 */
289 break;
290 case IW_CM_STATE_CONN_SENT:
291 /* Can only get here if wait above fails */
292 default:
293 BUG();
294 }
295 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
296
297 if (qp) {
298 if (abrupt)
299 ret = iwcm_modify_qp_err(qp);
300 else
301 ret = iwcm_modify_qp_sqd(qp);
302
303 /*
304 * If both sides are disconnecting the QP could
305 * already be in ERR or SQD states
306 */
307 ret = 0;
308 }
309
310 return ret;
311}
312EXPORT_SYMBOL(iw_cm_disconnect);
313
314/*
315 * CM_ID <-- DESTROYING
316 *
317 * Clean up all resources associated with the connection and release
318 * the initial reference taken by iw_create_cm_id.
319 */
320static void destroy_cm_id(struct iw_cm_id *cm_id)
321{
322 struct iwcm_id_private *cm_id_priv;
323 unsigned long flags;
324 int ret;
325
326 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
327 /*
328 * Wait if we're currently in a connect or accept downcall. A
329 * listening endpoint should never block here.
330 */
331 wait_event(cm_id_priv->connect_wait,
332 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
333
334 spin_lock_irqsave(&cm_id_priv->lock, flags);
335 switch (cm_id_priv->state) {
336 case IW_CM_STATE_LISTEN:
337 cm_id_priv->state = IW_CM_STATE_DESTROYING;
338 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
339 /* destroy the listening endpoint */
340 ret = cm_id->device->iwcm->destroy_listen(cm_id);
341 spin_lock_irqsave(&cm_id_priv->lock, flags);
342 break;
343 case IW_CM_STATE_ESTABLISHED:
344 cm_id_priv->state = IW_CM_STATE_DESTROYING;
345 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
346 /* Abrupt close of the connection */
347 (void)iwcm_modify_qp_err(cm_id_priv->qp);
348 spin_lock_irqsave(&cm_id_priv->lock, flags);
349 break;
350 case IW_CM_STATE_IDLE:
351 case IW_CM_STATE_CLOSING:
352 cm_id_priv->state = IW_CM_STATE_DESTROYING;
353 break;
354 case IW_CM_STATE_CONN_RECV:
355 /*
356 * App called destroy before/without calling accept after
357 * receiving connection request event notification.
358 */
359 cm_id_priv->state = IW_CM_STATE_DESTROYING;
360 break;
361 case IW_CM_STATE_CONN_SENT:
362 case IW_CM_STATE_DESTROYING:
363 default:
364 BUG();
365 break;
366 }
367 if (cm_id_priv->qp) {
368 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
369 cm_id_priv->qp = NULL;
370 }
371 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
372
373 (void)iwcm_deref_id(cm_id_priv);
374}
375
376/*
377 * This function is only called by the application thread and cannot
378 * be called by the event thread. The function will wait for all
379 * references to be released on the cm_id and then kfree the cm_id
380 * object.
381 */
382void iw_destroy_cm_id(struct iw_cm_id *cm_id)
383{
384 struct iwcm_id_private *cm_id_priv;
385
386 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
387 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
388
389 destroy_cm_id(cm_id);
390
391 wait_for_completion(&cm_id_priv->destroy_comp);
392
393 dealloc_work_entries(cm_id_priv);
394
395 kfree(cm_id_priv);
396}
397EXPORT_SYMBOL(iw_destroy_cm_id);
398
399/*
400 * CM_ID <-- LISTEN
401 *
402 * Start listening for connect requests. Generates one CONNECT_REQUEST
403 * event for each inbound connect request.
404 */
405int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
406{
407 struct iwcm_id_private *cm_id_priv;
408 unsigned long flags;
13fccdb3 409 int ret;
922a8e9f
TT
410
411 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
412
413 ret = alloc_work_entries(cm_id_priv, backlog);
414 if (ret)
415 return ret;
416
417 spin_lock_irqsave(&cm_id_priv->lock, flags);
418 switch (cm_id_priv->state) {
419 case IW_CM_STATE_IDLE:
420 cm_id_priv->state = IW_CM_STATE_LISTEN;
421 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
422 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
423 if (ret)
424 cm_id_priv->state = IW_CM_STATE_IDLE;
425 spin_lock_irqsave(&cm_id_priv->lock, flags);
426 break;
427 default:
428 ret = -EINVAL;
429 }
430 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
431
432 return ret;
433}
434EXPORT_SYMBOL(iw_cm_listen);
435
436/*
437 * CM_ID <-- IDLE
438 *
439 * Rejects an inbound connection request. No events are generated.
440 */
441int iw_cm_reject(struct iw_cm_id *cm_id,
442 const void *private_data,
443 u8 private_data_len)
444{
445 struct iwcm_id_private *cm_id_priv;
446 unsigned long flags;
447 int ret;
448
449 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
450 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
451
452 spin_lock_irqsave(&cm_id_priv->lock, flags);
453 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
454 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
455 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
456 wake_up_all(&cm_id_priv->connect_wait);
457 return -EINVAL;
458 }
459 cm_id_priv->state = IW_CM_STATE_IDLE;
460 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
461
462 ret = cm_id->device->iwcm->reject(cm_id, private_data,
463 private_data_len);
464
465 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
466 wake_up_all(&cm_id_priv->connect_wait);
467
468 return ret;
469}
470EXPORT_SYMBOL(iw_cm_reject);
471
472/*
473 * CM_ID <-- ESTABLISHED
474 *
475 * Accepts an inbound connection request and generates an ESTABLISHED
476 * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
477 * until the ESTABLISHED event is received from the provider.
478 */
479int iw_cm_accept(struct iw_cm_id *cm_id,
480 struct iw_cm_conn_param *iw_param)
481{
482 struct iwcm_id_private *cm_id_priv;
483 struct ib_qp *qp;
484 unsigned long flags;
485 int ret;
486
487 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
488 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
489
490 spin_lock_irqsave(&cm_id_priv->lock, flags);
491 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
492 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
493 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
494 wake_up_all(&cm_id_priv->connect_wait);
495 return -EINVAL;
496 }
497 /* Get the ib_qp given the QPN */
498 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
499 if (!qp) {
500 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
501 return -EINVAL;
502 }
503 cm_id->device->iwcm->add_ref(qp);
504 cm_id_priv->qp = qp;
505 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
506
507 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
508 if (ret) {
509 /* An error on accept precludes provider events */
510 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
511 cm_id_priv->state = IW_CM_STATE_IDLE;
512 spin_lock_irqsave(&cm_id_priv->lock, flags);
513 if (cm_id_priv->qp) {
514 cm_id->device->iwcm->rem_ref(qp);
515 cm_id_priv->qp = NULL;
516 }
517 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
518 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
519 wake_up_all(&cm_id_priv->connect_wait);
520 }
521
522 return ret;
523}
524EXPORT_SYMBOL(iw_cm_accept);
525
526/*
527 * Active Side: CM_ID <-- CONN_SENT
528 *
529 * If successful, results in the generation of a CONNECT_REPLY
530 * event. iw_cm_disconnect and iw_cm_destroy will block until the
531 * CONNECT_REPLY event is received from the provider.
532 */
533int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
534{
535 struct iwcm_id_private *cm_id_priv;
13fccdb3 536 int ret;
922a8e9f
TT
537 unsigned long flags;
538 struct ib_qp *qp;
539
540 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
541
542 ret = alloc_work_entries(cm_id_priv, 4);
543 if (ret)
544 return ret;
545
546 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
547 spin_lock_irqsave(&cm_id_priv->lock, flags);
548
549 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
550 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
551 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
552 wake_up_all(&cm_id_priv->connect_wait);
553 return -EINVAL;
554 }
555
556 /* Get the ib_qp given the QPN */
557 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
558 if (!qp) {
559 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
560 return -EINVAL;
561 }
562 cm_id->device->iwcm->add_ref(qp);
563 cm_id_priv->qp = qp;
564 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
565 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
566
567 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
568 if (ret) {
569 spin_lock_irqsave(&cm_id_priv->lock, flags);
570 if (cm_id_priv->qp) {
571 cm_id->device->iwcm->rem_ref(qp);
572 cm_id_priv->qp = NULL;
573 }
574 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
575 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
576 cm_id_priv->state = IW_CM_STATE_IDLE;
577 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
578 wake_up_all(&cm_id_priv->connect_wait);
579 }
580
581 return ret;
582}
583EXPORT_SYMBOL(iw_cm_connect);
584
585/*
586 * Passive Side: new CM_ID <-- CONN_RECV
587 *
588 * Handles an inbound connect request. The function creates a new
589 * iw_cm_id to represent the new connection and inherits the client
590 * callback function and other attributes from the listening parent.
591 *
592 * The work item contains a pointer to the listen_cm_id and the event. The
593 * listen_cm_id contains the client cm_handler, context and
594 * device. These are copied when the device is cloned. The event
595 * contains the new four tuple.
596 *
597 * An error on the child should not affect the parent, so this
598 * function does not return a value.
599 */
600static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
601 struct iw_cm_event *iw_event)
602{
603 unsigned long flags;
604 struct iw_cm_id *cm_id;
605 struct iwcm_id_private *cm_id_priv;
606 int ret;
607
608 /*
609 * The provider should never generate a connection request
610 * event with a bad status.
611 */
612 BUG_ON(iw_event->status);
613
614 /*
615 * We could be destroying the listening id. If so, ignore this
616 * upcall.
617 */
618 spin_lock_irqsave(&listen_id_priv->lock, flags);
619 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
620 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
83b96586 621 goto out;
922a8e9f
TT
622 }
623 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
624
625 cm_id = iw_create_cm_id(listen_id_priv->id.device,
626 listen_id_priv->id.cm_handler,
627 listen_id_priv->id.context);
628 /* If the cm_id could not be created, ignore the request */
629 if (IS_ERR(cm_id))
83b96586 630 goto out;
922a8e9f
TT
631
632 cm_id->provider_data = iw_event->provider_data;
633 cm_id->local_addr = iw_event->local_addr;
634 cm_id->remote_addr = iw_event->remote_addr;
635
636 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
637 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
638
639 ret = alloc_work_entries(cm_id_priv, 3);
640 if (ret) {
641 iw_cm_reject(cm_id, NULL, 0);
642 iw_destroy_cm_id(cm_id);
83b96586 643 goto out;
922a8e9f
TT
644 }
645
646 /* Call the client CM handler */
647 ret = cm_id->cm_handler(cm_id, iw_event);
648 if (ret) {
649 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
650 destroy_cm_id(cm_id);
651 if (atomic_read(&cm_id_priv->refcount)==0)
652 kfree(cm_id);
653 }
654
83b96586 655out:
922a8e9f
TT
656 if (iw_event->private_data_len)
657 kfree(iw_event->private_data);
658}
659
660/*
661 * Passive Side: CM_ID <-- ESTABLISHED
662 *
663 * The provider generated an ESTABLISHED event which means that
664 * the MPA negotion has completed successfully and we are now in MPA
665 * FPDU mode.
666 *
667 * This event can only be received in the CONN_RECV state. If the
668 * remote peer closed, the ESTABLISHED event would be received followed
669 * by the CLOSE event. If the app closes, it will block until we wake
670 * it up after processing this event.
671 */
672static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
673 struct iw_cm_event *iw_event)
674{
675 unsigned long flags;
13fccdb3 676 int ret;
922a8e9f
TT
677
678 spin_lock_irqsave(&cm_id_priv->lock, flags);
679
680 /*
681 * We clear the CONNECT_WAIT bit here to allow the callback
682 * function to call iw_cm_disconnect. Calling iw_destroy_cm_id
683 * from a callback handler is not allowed.
684 */
685 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
686 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
687 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
688 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
689 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
690 wake_up_all(&cm_id_priv->connect_wait);
691
692 return ret;
693}
694
695/*
696 * Active Side: CM_ID <-- ESTABLISHED
697 *
698 * The app has called connect and is waiting for the established event to
699 * post it's requests to the server. This event will wake up anyone
700 * blocked in iw_cm_disconnect or iw_destroy_id.
701 */
702static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
703 struct iw_cm_event *iw_event)
704{
705 unsigned long flags;
13fccdb3 706 int ret;
922a8e9f
TT
707
708 spin_lock_irqsave(&cm_id_priv->lock, flags);
709 /*
710 * Clear the connect wait bit so a callback function calling
711 * iw_cm_disconnect will not wait and deadlock this thread
712 */
713 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
714 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
715 if (iw_event->status == IW_CM_EVENT_STATUS_ACCEPTED) {
716 cm_id_priv->id.local_addr = iw_event->local_addr;
717 cm_id_priv->id.remote_addr = iw_event->remote_addr;
718 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
719 } else {
720 /* REJECTED or RESET */
721 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
722 cm_id_priv->qp = NULL;
723 cm_id_priv->state = IW_CM_STATE_IDLE;
724 }
725 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
726 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
727
728 if (iw_event->private_data_len)
729 kfree(iw_event->private_data);
730
731 /* Wake up waiters on connect complete */
732 wake_up_all(&cm_id_priv->connect_wait);
733
734 return ret;
735}
736
737/*
738 * CM_ID <-- CLOSING
739 *
740 * If in the ESTABLISHED state, move to CLOSING.
741 */
742static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
743 struct iw_cm_event *iw_event)
744{
745 unsigned long flags;
746
747 spin_lock_irqsave(&cm_id_priv->lock, flags);
748 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
749 cm_id_priv->state = IW_CM_STATE_CLOSING;
750 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
751}
752
753/*
754 * CM_ID <-- IDLE
755 *
756 * If in the ESTBLISHED or CLOSING states, the QP will have have been
757 * moved by the provider to the ERR state. Disassociate the CM_ID from
758 * the QP, move to IDLE, and remove the 'connected' reference.
759 *
760 * If in some other state, the cm_id was destroyed asynchronously.
761 * This is the last reference that will result in waking up
762 * the app thread blocked in iw_destroy_cm_id.
763 */
764static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
765 struct iw_cm_event *iw_event)
766{
767 unsigned long flags;
768 int ret = 0;
769 spin_lock_irqsave(&cm_id_priv->lock, flags);
770
771 if (cm_id_priv->qp) {
772 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
773 cm_id_priv->qp = NULL;
774 }
775 switch (cm_id_priv->state) {
776 case IW_CM_STATE_ESTABLISHED:
777 case IW_CM_STATE_CLOSING:
778 cm_id_priv->state = IW_CM_STATE_IDLE;
779 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
780 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
781 spin_lock_irqsave(&cm_id_priv->lock, flags);
782 break;
783 case IW_CM_STATE_DESTROYING:
784 break;
785 default:
786 BUG();
787 }
788 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
789
790 return ret;
791}
792
793static int process_event(struct iwcm_id_private *cm_id_priv,
794 struct iw_cm_event *iw_event)
795{
796 int ret = 0;
797
798 switch (iw_event->event) {
799 case IW_CM_EVENT_CONNECT_REQUEST:
800 cm_conn_req_handler(cm_id_priv, iw_event);
801 break;
802 case IW_CM_EVENT_CONNECT_REPLY:
803 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
804 break;
805 case IW_CM_EVENT_ESTABLISHED:
806 ret = cm_conn_est_handler(cm_id_priv, iw_event);
807 break;
808 case IW_CM_EVENT_DISCONNECT:
809 cm_disconnect_handler(cm_id_priv, iw_event);
810 break;
811 case IW_CM_EVENT_CLOSE:
812 ret = cm_close_handler(cm_id_priv, iw_event);
813 break;
814 default:
815 BUG();
816 }
817
818 return ret;
819}
820
821/*
822 * Process events on the work_list for the cm_id. If the callback
823 * function requests that the cm_id be deleted, a flag is set in the
824 * cm_id flags to indicate that when the last reference is
825 * removed, the cm_id is to be destroyed. This is necessary to
826 * distinguish between an object that will be destroyed by the app
827 * thread asleep on the destroy_comp list vs. an object destroyed
828 * here synchronously when the last reference is removed.
829 */
830static void cm_work_handler(void *arg)
831{
33ba0fa9
KK
832 struct iwcm_work *work = arg;
833 struct iw_cm_event levent;
922a8e9f
TT
834 struct iwcm_id_private *cm_id_priv = work->cm_id;
835 unsigned long flags;
836 int empty;
837 int ret = 0;
838
839 spin_lock_irqsave(&cm_id_priv->lock, flags);
840 empty = list_empty(&cm_id_priv->work_list);
841 while (!empty) {
842 work = list_entry(cm_id_priv->work_list.next,
843 struct iwcm_work, list);
844 list_del_init(&work->list);
845 empty = list_empty(&cm_id_priv->work_list);
33ba0fa9 846 levent = work->event;
922a8e9f
TT
847 put_work(work);
848 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
849
33ba0fa9 850 ret = process_event(cm_id_priv, &levent);
922a8e9f
TT
851 if (ret) {
852 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
853 destroy_cm_id(&cm_id_priv->id);
854 }
855 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
856 if (iwcm_deref_id(cm_id_priv))
857 return;
858
859 if (atomic_read(&cm_id_priv->refcount)==0 &&
860 test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
861 dealloc_work_entries(cm_id_priv);
862 kfree(cm_id_priv);
863 return;
864 }
865 spin_lock_irqsave(&cm_id_priv->lock, flags);
866 }
867 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
868}
869
870/*
871 * This function is called on interrupt context. Schedule events on
872 * the iwcm_wq thread to allow callback functions to downcall into
873 * the CM and/or block. Events are queued to a per-CM_ID
874 * work_list. If this is the first event on the work_list, the work
875 * element is also queued on the iwcm_wq thread.
876 *
877 * Each event holds a reference on the cm_id. Until the last posted
878 * event has been delivered and processed, the cm_id cannot be
879 * deleted.
880 *
881 * Returns:
882 * 0 - the event was handled.
883 * -ENOMEM - the event was not handled due to lack of resources.
884 */
885static int cm_event_handler(struct iw_cm_id *cm_id,
886 struct iw_cm_event *iw_event)
887{
888 struct iwcm_work *work;
889 struct iwcm_id_private *cm_id_priv;
890 unsigned long flags;
891 int ret = 0;
892
893 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
894
895 spin_lock_irqsave(&cm_id_priv->lock, flags);
896 work = get_work(cm_id_priv);
897 if (!work) {
898 ret = -ENOMEM;
899 goto out;
900 }
901
902 INIT_WORK(&work->work, cm_work_handler, work);
903 work->cm_id = cm_id_priv;
904 work->event = *iw_event;
905
906 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
907 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
908 work->event.private_data_len) {
715a588f 909 ret = copy_private_data(&work->event);
922a8e9f
TT
910 if (ret) {
911 put_work(work);
912 goto out;
913 }
914 }
915
916 atomic_inc(&cm_id_priv->refcount);
917 if (list_empty(&cm_id_priv->work_list)) {
918 list_add_tail(&work->list, &cm_id_priv->work_list);
919 queue_work(iwcm_wq, &work->work);
920 } else
921 list_add_tail(&work->list, &cm_id_priv->work_list);
922out:
923 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
924 return ret;
925}
926
927static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
928 struct ib_qp_attr *qp_attr,
929 int *qp_attr_mask)
930{
931 unsigned long flags;
932 int ret;
933
934 spin_lock_irqsave(&cm_id_priv->lock, flags);
935 switch (cm_id_priv->state) {
936 case IW_CM_STATE_IDLE:
937 case IW_CM_STATE_CONN_SENT:
938 case IW_CM_STATE_CONN_RECV:
939 case IW_CM_STATE_ESTABLISHED:
940 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
941 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
942 IB_ACCESS_REMOTE_WRITE|
943 IB_ACCESS_REMOTE_READ;
944 ret = 0;
945 break;
946 default:
947 ret = -EINVAL;
948 break;
949 }
950 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
951 return ret;
952}
953
954static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
955 struct ib_qp_attr *qp_attr,
956 int *qp_attr_mask)
957{
958 unsigned long flags;
959 int ret;
960
961 spin_lock_irqsave(&cm_id_priv->lock, flags);
962 switch (cm_id_priv->state) {
963 case IW_CM_STATE_IDLE:
964 case IW_CM_STATE_CONN_SENT:
965 case IW_CM_STATE_CONN_RECV:
966 case IW_CM_STATE_ESTABLISHED:
967 *qp_attr_mask = 0;
968 ret = 0;
969 break;
970 default:
971 ret = -EINVAL;
972 break;
973 }
974 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
975 return ret;
976}
977
978int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
979 struct ib_qp_attr *qp_attr,
980 int *qp_attr_mask)
981{
982 struct iwcm_id_private *cm_id_priv;
983 int ret;
984
985 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
986 switch (qp_attr->qp_state) {
987 case IB_QPS_INIT:
988 case IB_QPS_RTR:
989 ret = iwcm_init_qp_init_attr(cm_id_priv,
990 qp_attr, qp_attr_mask);
991 break;
992 case IB_QPS_RTS:
993 ret = iwcm_init_qp_rts_attr(cm_id_priv,
994 qp_attr, qp_attr_mask);
995 break;
996 default:
997 ret = -EINVAL;
998 break;
999 }
1000 return ret;
1001}
1002EXPORT_SYMBOL(iw_cm_init_qp_attr);
1003
1004static int __init iw_cm_init(void)
1005{
1006 iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
1007 if (!iwcm_wq)
1008 return -ENOMEM;
1009
1010 return 0;
1011}
1012
1013static void __exit iw_cm_cleanup(void)
1014{
1015 destroy_workqueue(iwcm_wq);
1016}
1017
1018module_init(iw_cm_init);
1019module_exit(iw_cm_cleanup);
This page took 0.145833 seconds and 5 git commands to generate.