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