1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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/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
50#include <rdma/iw_cm.h>
51#include <rdma/ib_addr.h>
52
53#include "iwcm.h"
54
55MODULE_AUTHOR("Tom Tucker");
56MODULE_DESCRIPTION("iWARP CM");
57MODULE_LICENSE("Dual BSD/GPL");
58
59static struct workqueue_struct *iwcm_wq;
60struct iwcm_work {
61 struct work_struct work;
62 struct iwcm_id_private *cm_id;
63 struct list_head list;
64 struct iw_cm_event event;
65 struct list_head free_list;
66};
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
93{
94 struct iwcm_work *work;
95
96 if (list_empty(&cm_id_priv->work_free_list))
97 return NULL;
98 work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
99 free_list);
100 list_del_init(&work->free_list);
101 return work;
102}
103
104static void put_work(struct iwcm_work *work)
105{
106 list_add(&work->free_list, &work->cm_id->work_free_list);
107}
108
109static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv)
110{
111 struct list_head *e, *tmp;
112
113 list_for_each_safe(e, tmp, &cm_id_priv->work_free_list)
114 kfree(list_entry(e, struct iwcm_work, free_list));
115}
116
117static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count)
118{
119 struct iwcm_work *work;
120
121 BUG_ON(!list_empty(&cm_id_priv->work_free_list));
122 while (count--) {
123 work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL);
124 if (!work) {
125 dealloc_work_entries(cm_id_priv);
126 return -ENOMEM;
127 }
128 work->cm_id = cm_id_priv;
129 INIT_LIST_HEAD(&work->list);
130 put_work(work);
131 }
132 return 0;
133}
134
135
136
137
138
139
140static int copy_private_data(struct iw_cm_event *event)
141{
142 void *p;
143
144 p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
145 if (!p)
146 return -ENOMEM;
147 event->private_data = p;
148 return 0;
149}
150
151static void free_cm_id(struct iwcm_id_private *cm_id_priv)
152{
153 dealloc_work_entries(cm_id_priv);
154 kfree(cm_id_priv);
155}
156
157
158
159
160
161
162static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
163{
164 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
165 if (atomic_dec_and_test(&cm_id_priv->refcount)) {
166 BUG_ON(!list_empty(&cm_id_priv->work_list));
167 complete(&cm_id_priv->destroy_comp);
168 return 1;
169 }
170
171 return 0;
172}
173
174static void add_ref(struct iw_cm_id *cm_id)
175{
176 struct iwcm_id_private *cm_id_priv;
177 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
178 atomic_inc(&cm_id_priv->refcount);
179}
180
181static void rem_ref(struct iw_cm_id *cm_id)
182{
183 struct iwcm_id_private *cm_id_priv;
184 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
185 if (iwcm_deref_id(cm_id_priv) &&
186 test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
187 BUG_ON(!list_empty(&cm_id_priv->work_list));
188 free_cm_id(cm_id_priv);
189 }
190}
191
192static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
193
194struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
195 iw_cm_handler cm_handler,
196 void *context)
197{
198 struct iwcm_id_private *cm_id_priv;
199
200 cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
201 if (!cm_id_priv)
202 return ERR_PTR(-ENOMEM);
203
204 cm_id_priv->state = IW_CM_STATE_IDLE;
205 cm_id_priv->id.device = device;
206 cm_id_priv->id.cm_handler = cm_handler;
207 cm_id_priv->id.context = context;
208 cm_id_priv->id.event_handler = cm_event_handler;
209 cm_id_priv->id.add_ref = add_ref;
210 cm_id_priv->id.rem_ref = rem_ref;
211 spin_lock_init(&cm_id_priv->lock);
212 atomic_set(&cm_id_priv->refcount, 1);
213 init_waitqueue_head(&cm_id_priv->connect_wait);
214 init_completion(&cm_id_priv->destroy_comp);
215 INIT_LIST_HEAD(&cm_id_priv->work_list);
216 INIT_LIST_HEAD(&cm_id_priv->work_free_list);
217
218 return &cm_id_priv->id;
219}
220EXPORT_SYMBOL(iw_create_cm_id);
221
222
223static int iwcm_modify_qp_err(struct ib_qp *qp)
224{
225 struct ib_qp_attr qp_attr;
226
227 if (!qp)
228 return -EINVAL;
229
230 qp_attr.qp_state = IB_QPS_ERR;
231 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
232}
233
234
235
236
237
238static int iwcm_modify_qp_sqd(struct ib_qp *qp)
239{
240 struct ib_qp_attr qp_attr;
241
242 BUG_ON(qp == NULL);
243 qp_attr.qp_state = IB_QPS_SQD;
244 return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
245}
246
247
248
249
250
251
252
253
254
255
256
257
258
259int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
260{
261 struct iwcm_id_private *cm_id_priv;
262 unsigned long flags;
263 int ret = 0;
264 struct ib_qp *qp = NULL;
265
266 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
267
268 wait_event(cm_id_priv->connect_wait,
269 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
270
271 spin_lock_irqsave(&cm_id_priv->lock, flags);
272 switch (cm_id_priv->state) {
273 case IW_CM_STATE_ESTABLISHED:
274 cm_id_priv->state = IW_CM_STATE_CLOSING;
275
276
277 if (cm_id_priv->qp)
278 qp = cm_id_priv->qp;
279 else
280 ret = -EINVAL;
281 break;
282 case IW_CM_STATE_LISTEN:
283 ret = -EINVAL;
284 break;
285 case IW_CM_STATE_CLOSING:
286
287 case IW_CM_STATE_IDLE:
288
289 break;
290 case IW_CM_STATE_CONN_RECV:
291
292
293
294
295 break;
296 case IW_CM_STATE_CONN_SENT:
297
298 default:
299 BUG();
300 }
301 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
302
303 if (qp) {
304 if (abrupt)
305 ret = iwcm_modify_qp_err(qp);
306 else
307 ret = iwcm_modify_qp_sqd(qp);
308
309
310
311
312
313 ret = 0;
314 }
315
316 return ret;
317}
318EXPORT_SYMBOL(iw_cm_disconnect);
319
320
321
322
323
324
325
326static void destroy_cm_id(struct iw_cm_id *cm_id)
327{
328 struct iwcm_id_private *cm_id_priv;
329 unsigned long flags;
330 int ret;
331
332 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
333
334
335
336
337 wait_event(cm_id_priv->connect_wait,
338 !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
339
340 spin_lock_irqsave(&cm_id_priv->lock, flags);
341 switch (cm_id_priv->state) {
342 case IW_CM_STATE_LISTEN:
343 cm_id_priv->state = IW_CM_STATE_DESTROYING;
344 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
345
346 ret = cm_id->device->iwcm->destroy_listen(cm_id);
347 spin_lock_irqsave(&cm_id_priv->lock, flags);
348 break;
349 case IW_CM_STATE_ESTABLISHED:
350 cm_id_priv->state = IW_CM_STATE_DESTROYING;
351 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
352
353 (void)iwcm_modify_qp_err(cm_id_priv->qp);
354 spin_lock_irqsave(&cm_id_priv->lock, flags);
355 break;
356 case IW_CM_STATE_IDLE:
357 case IW_CM_STATE_CLOSING:
358 cm_id_priv->state = IW_CM_STATE_DESTROYING;
359 break;
360 case IW_CM_STATE_CONN_RECV:
361
362
363
364
365
366
367 cm_id_priv->state = IW_CM_STATE_DESTROYING;
368 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
369 cm_id->device->iwcm->reject(cm_id, NULL, 0);
370 spin_lock_irqsave(&cm_id_priv->lock, flags);
371 break;
372 case IW_CM_STATE_CONN_SENT:
373 case IW_CM_STATE_DESTROYING:
374 default:
375 BUG();
376 break;
377 }
378 if (cm_id_priv->qp) {
379 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
380 cm_id_priv->qp = NULL;
381 }
382 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
383
384 (void)iwcm_deref_id(cm_id_priv);
385}
386
387
388
389
390
391
392
393void iw_destroy_cm_id(struct iw_cm_id *cm_id)
394{
395 struct iwcm_id_private *cm_id_priv;
396
397 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
398 BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
399
400 destroy_cm_id(cm_id);
401
402 wait_for_completion(&cm_id_priv->destroy_comp);
403
404 free_cm_id(cm_id_priv);
405}
406EXPORT_SYMBOL(iw_destroy_cm_id);
407
408
409
410
411
412
413
414int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
415{
416 struct iwcm_id_private *cm_id_priv;
417 unsigned long flags;
418 int ret;
419
420 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
421
422 ret = alloc_work_entries(cm_id_priv, backlog);
423 if (ret)
424 return ret;
425
426 spin_lock_irqsave(&cm_id_priv->lock, flags);
427 switch (cm_id_priv->state) {
428 case IW_CM_STATE_IDLE:
429 cm_id_priv->state = IW_CM_STATE_LISTEN;
430 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
431 ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
432 if (ret)
433 cm_id_priv->state = IW_CM_STATE_IDLE;
434 spin_lock_irqsave(&cm_id_priv->lock, flags);
435 break;
436 default:
437 ret = -EINVAL;
438 }
439 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
440
441 return ret;
442}
443EXPORT_SYMBOL(iw_cm_listen);
444
445
446
447
448
449
450int iw_cm_reject(struct iw_cm_id *cm_id,
451 const void *private_data,
452 u8 private_data_len)
453{
454 struct iwcm_id_private *cm_id_priv;
455 unsigned long flags;
456 int ret;
457
458 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
459 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
460
461 spin_lock_irqsave(&cm_id_priv->lock, flags);
462 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
463 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
464 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
465 wake_up_all(&cm_id_priv->connect_wait);
466 return -EINVAL;
467 }
468 cm_id_priv->state = IW_CM_STATE_IDLE;
469 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
470
471 ret = cm_id->device->iwcm->reject(cm_id, private_data,
472 private_data_len);
473
474 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
475 wake_up_all(&cm_id_priv->connect_wait);
476
477 return ret;
478}
479EXPORT_SYMBOL(iw_cm_reject);
480
481
482
483
484
485
486
487
488int iw_cm_accept(struct iw_cm_id *cm_id,
489 struct iw_cm_conn_param *iw_param)
490{
491 struct iwcm_id_private *cm_id_priv;
492 struct ib_qp *qp;
493 unsigned long flags;
494 int ret;
495
496 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
497 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
498
499 spin_lock_irqsave(&cm_id_priv->lock, flags);
500 if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
501 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
502 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
503 wake_up_all(&cm_id_priv->connect_wait);
504 return -EINVAL;
505 }
506
507 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
508 if (!qp) {
509 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
510 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
511 wake_up_all(&cm_id_priv->connect_wait);
512 return -EINVAL;
513 }
514 cm_id->device->iwcm->add_ref(qp);
515 cm_id_priv->qp = qp;
516 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
517
518 ret = cm_id->device->iwcm->accept(cm_id, iw_param);
519 if (ret) {
520
521 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
522 cm_id_priv->state = IW_CM_STATE_IDLE;
523 spin_lock_irqsave(&cm_id_priv->lock, flags);
524 if (cm_id_priv->qp) {
525 cm_id->device->iwcm->rem_ref(qp);
526 cm_id_priv->qp = NULL;
527 }
528 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
529 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
530 wake_up_all(&cm_id_priv->connect_wait);
531 }
532
533 return ret;
534}
535EXPORT_SYMBOL(iw_cm_accept);
536
537
538
539
540
541
542
543
544int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
545{
546 struct iwcm_id_private *cm_id_priv;
547 int ret;
548 unsigned long flags;
549 struct ib_qp *qp;
550
551 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
552
553 ret = alloc_work_entries(cm_id_priv, 4);
554 if (ret)
555 return ret;
556
557 set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
558 spin_lock_irqsave(&cm_id_priv->lock, flags);
559
560 if (cm_id_priv->state != IW_CM_STATE_IDLE) {
561 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
562 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
563 wake_up_all(&cm_id_priv->connect_wait);
564 return -EINVAL;
565 }
566
567
568 qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
569 if (!qp) {
570 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
571 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
572 wake_up_all(&cm_id_priv->connect_wait);
573 return -EINVAL;
574 }
575 cm_id->device->iwcm->add_ref(qp);
576 cm_id_priv->qp = qp;
577 cm_id_priv->state = IW_CM_STATE_CONN_SENT;
578 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
579
580 ret = cm_id->device->iwcm->connect(cm_id, iw_param);
581 if (ret) {
582 spin_lock_irqsave(&cm_id_priv->lock, flags);
583 if (cm_id_priv->qp) {
584 cm_id->device->iwcm->rem_ref(qp);
585 cm_id_priv->qp = NULL;
586 }
587 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
588 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
589 cm_id_priv->state = IW_CM_STATE_IDLE;
590 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
591 wake_up_all(&cm_id_priv->connect_wait);
592 }
593
594 return ret;
595}
596EXPORT_SYMBOL(iw_cm_connect);
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
614 struct iw_cm_event *iw_event)
615{
616 unsigned long flags;
617 struct iw_cm_id *cm_id;
618 struct iwcm_id_private *cm_id_priv;
619 int ret;
620
621
622
623
624
625 BUG_ON(iw_event->status);
626
627 cm_id = iw_create_cm_id(listen_id_priv->id.device,
628 listen_id_priv->id.cm_handler,
629 listen_id_priv->id.context);
630
631 if (IS_ERR(cm_id))
632 goto out;
633
634 cm_id->provider_data = iw_event->provider_data;
635 cm_id->local_addr = iw_event->local_addr;
636 cm_id->remote_addr = iw_event->remote_addr;
637
638 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
639 cm_id_priv->state = IW_CM_STATE_CONN_RECV;
640
641
642
643
644
645 spin_lock_irqsave(&listen_id_priv->lock, flags);
646 if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
647 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
648 iw_cm_reject(cm_id, NULL, 0);
649 iw_destroy_cm_id(cm_id);
650 goto out;
651 }
652 spin_unlock_irqrestore(&listen_id_priv->lock, flags);
653
654 ret = alloc_work_entries(cm_id_priv, 3);
655 if (ret) {
656 iw_cm_reject(cm_id, NULL, 0);
657 iw_destroy_cm_id(cm_id);
658 goto out;
659 }
660
661
662 ret = cm_id->cm_handler(cm_id, iw_event);
663 if (ret) {
664 iw_cm_reject(cm_id, NULL, 0);
665 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
666 destroy_cm_id(cm_id);
667 if (atomic_read(&cm_id_priv->refcount)==0)
668 free_cm_id(cm_id_priv);
669 }
670
671out:
672 if (iw_event->private_data_len)
673 kfree(iw_event->private_data);
674}
675
676
677
678
679
680
681
682
683
684
685
686
687
688static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv,
689 struct iw_cm_event *iw_event)
690{
691 unsigned long flags;
692 int ret;
693
694 spin_lock_irqsave(&cm_id_priv->lock, flags);
695
696
697
698
699
700
701 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
702 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
703 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
704 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
705 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
706 wake_up_all(&cm_id_priv->connect_wait);
707
708 return ret;
709}
710
711
712
713
714
715
716
717
718static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv,
719 struct iw_cm_event *iw_event)
720{
721 unsigned long flags;
722 int ret;
723
724 spin_lock_irqsave(&cm_id_priv->lock, flags);
725
726
727
728
729 clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
730 BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
731 if (iw_event->status == 0) {
732 cm_id_priv->id.local_addr = iw_event->local_addr;
733 cm_id_priv->id.remote_addr = iw_event->remote_addr;
734 cm_id_priv->state = IW_CM_STATE_ESTABLISHED;
735 } else {
736
737 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
738 cm_id_priv->qp = NULL;
739 cm_id_priv->state = IW_CM_STATE_IDLE;
740 }
741 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
742 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
743
744 if (iw_event->private_data_len)
745 kfree(iw_event->private_data);
746
747
748 wake_up_all(&cm_id_priv->connect_wait);
749
750 return ret;
751}
752
753
754
755
756
757
758static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv,
759 struct iw_cm_event *iw_event)
760{
761 unsigned long flags;
762
763 spin_lock_irqsave(&cm_id_priv->lock, flags);
764 if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED)
765 cm_id_priv->state = IW_CM_STATE_CLOSING;
766 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
767}
768
769
770
771
772
773
774
775
776
777
778
779
780static int cm_close_handler(struct iwcm_id_private *cm_id_priv,
781 struct iw_cm_event *iw_event)
782{
783 unsigned long flags;
784 int ret = 0;
785 spin_lock_irqsave(&cm_id_priv->lock, flags);
786
787 if (cm_id_priv->qp) {
788 cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
789 cm_id_priv->qp = NULL;
790 }
791 switch (cm_id_priv->state) {
792 case IW_CM_STATE_ESTABLISHED:
793 case IW_CM_STATE_CLOSING:
794 cm_id_priv->state = IW_CM_STATE_IDLE;
795 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
796 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event);
797 spin_lock_irqsave(&cm_id_priv->lock, flags);
798 break;
799 case IW_CM_STATE_DESTROYING:
800 break;
801 default:
802 BUG();
803 }
804 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
805
806 return ret;
807}
808
809static int process_event(struct iwcm_id_private *cm_id_priv,
810 struct iw_cm_event *iw_event)
811{
812 int ret = 0;
813
814 switch (iw_event->event) {
815 case IW_CM_EVENT_CONNECT_REQUEST:
816 cm_conn_req_handler(cm_id_priv, iw_event);
817 break;
818 case IW_CM_EVENT_CONNECT_REPLY:
819 ret = cm_conn_rep_handler(cm_id_priv, iw_event);
820 break;
821 case IW_CM_EVENT_ESTABLISHED:
822 ret = cm_conn_est_handler(cm_id_priv, iw_event);
823 break;
824 case IW_CM_EVENT_DISCONNECT:
825 cm_disconnect_handler(cm_id_priv, iw_event);
826 break;
827 case IW_CM_EVENT_CLOSE:
828 ret = cm_close_handler(cm_id_priv, iw_event);
829 break;
830 default:
831 BUG();
832 }
833
834 return ret;
835}
836
837
838
839
840
841
842
843
844
845
846static void cm_work_handler(struct work_struct *_work)
847{
848 struct iwcm_work *work = container_of(_work, struct iwcm_work, work);
849 struct iw_cm_event levent;
850 struct iwcm_id_private *cm_id_priv = work->cm_id;
851 unsigned long flags;
852 int empty;
853 int ret = 0;
854 int destroy_id;
855
856 spin_lock_irqsave(&cm_id_priv->lock, flags);
857 empty = list_empty(&cm_id_priv->work_list);
858 while (!empty) {
859 work = list_entry(cm_id_priv->work_list.next,
860 struct iwcm_work, list);
861 list_del_init(&work->list);
862 empty = list_empty(&cm_id_priv->work_list);
863 levent = work->event;
864 put_work(work);
865 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
866
867 ret = process_event(cm_id_priv, &levent);
868 if (ret) {
869 set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
870 destroy_cm_id(&cm_id_priv->id);
871 }
872 BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
873 destroy_id = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
874 if (iwcm_deref_id(cm_id_priv)) {
875 if (destroy_id) {
876 BUG_ON(!list_empty(&cm_id_priv->work_list));
877 free_cm_id(cm_id_priv);
878 }
879 return;
880 }
881 if (empty)
882 return;
883 spin_lock_irqsave(&cm_id_priv->lock, flags);
884 }
885 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
886}
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903static int cm_event_handler(struct iw_cm_id *cm_id,
904 struct iw_cm_event *iw_event)
905{
906 struct iwcm_work *work;
907 struct iwcm_id_private *cm_id_priv;
908 unsigned long flags;
909 int ret = 0;
910
911 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
912
913 spin_lock_irqsave(&cm_id_priv->lock, flags);
914 work = get_work(cm_id_priv);
915 if (!work) {
916 ret = -ENOMEM;
917 goto out;
918 }
919
920 INIT_WORK(&work->work, cm_work_handler);
921 work->cm_id = cm_id_priv;
922 work->event = *iw_event;
923
924 if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
925 work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
926 work->event.private_data_len) {
927 ret = copy_private_data(&work->event);
928 if (ret) {
929 put_work(work);
930 goto out;
931 }
932 }
933
934 atomic_inc(&cm_id_priv->refcount);
935 if (list_empty(&cm_id_priv->work_list)) {
936 list_add_tail(&work->list, &cm_id_priv->work_list);
937 queue_work(iwcm_wq, &work->work);
938 } else
939 list_add_tail(&work->list, &cm_id_priv->work_list);
940out:
941 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
942 return ret;
943}
944
945static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
946 struct ib_qp_attr *qp_attr,
947 int *qp_attr_mask)
948{
949 unsigned long flags;
950 int ret;
951
952 spin_lock_irqsave(&cm_id_priv->lock, flags);
953 switch (cm_id_priv->state) {
954 case IW_CM_STATE_IDLE:
955 case IW_CM_STATE_CONN_SENT:
956 case IW_CM_STATE_CONN_RECV:
957 case IW_CM_STATE_ESTABLISHED:
958 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
959 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
960 IB_ACCESS_REMOTE_READ;
961 ret = 0;
962 break;
963 default:
964 ret = -EINVAL;
965 break;
966 }
967 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
968 return ret;
969}
970
971static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
972 struct ib_qp_attr *qp_attr,
973 int *qp_attr_mask)
974{
975 unsigned long flags;
976 int ret;
977
978 spin_lock_irqsave(&cm_id_priv->lock, flags);
979 switch (cm_id_priv->state) {
980 case IW_CM_STATE_IDLE:
981 case IW_CM_STATE_CONN_SENT:
982 case IW_CM_STATE_CONN_RECV:
983 case IW_CM_STATE_ESTABLISHED:
984 *qp_attr_mask = 0;
985 ret = 0;
986 break;
987 default:
988 ret = -EINVAL;
989 break;
990 }
991 spin_unlock_irqrestore(&cm_id_priv->lock, flags);
992 return ret;
993}
994
995int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
996 struct ib_qp_attr *qp_attr,
997 int *qp_attr_mask)
998{
999 struct iwcm_id_private *cm_id_priv;
1000 int ret;
1001
1002 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
1003 switch (qp_attr->qp_state) {
1004 case IB_QPS_INIT:
1005 case IB_QPS_RTR:
1006 ret = iwcm_init_qp_init_attr(cm_id_priv,
1007 qp_attr, qp_attr_mask);
1008 break;
1009 case IB_QPS_RTS:
1010 ret = iwcm_init_qp_rts_attr(cm_id_priv,
1011 qp_attr, qp_attr_mask);
1012 break;
1013 default:
1014 ret = -EINVAL;
1015 break;
1016 }
1017 return ret;
1018}
1019EXPORT_SYMBOL(iw_cm_init_qp_attr);
1020
1021static int __init iw_cm_init(void)
1022{
1023 iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
1024 if (!iwcm_wq)
1025 return -ENOMEM;
1026
1027 return 0;
1028}
1029
1030static void __exit iw_cm_cleanup(void)
1031{
1032 destroy_workqueue(iwcm_wq);
1033}
1034
1035module_init(iw_cm_init);
1036module_exit(iw_cm_cleanup);
1037