-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.c
2816 lines (2246 loc) · 69.1 KB
/
provider.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, Microsoft Corporation.
*
* Author:
* K. Y. Srinivasan <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
* NON INFRINGEMENT. See the GNU General Public License for more
* details.
*
* Bug fixes/enhancements: Long Li <[email protected]>
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/ethtool.h>
#include <linux/rtnetlink.h>
#include <linux/inetdevice.h>
#include <linux/io.h>
#include <linux/hyperv.h>
#include <linux/completion.h>
#include <rdma/iw_cm.h>
#include <rdma/ib_verbs.h>
#include <rdma/ib_smi.h>
#include <rdma/ib_umem.h>
#include <rdma/ib_user_verbs.h>
#include <rdma/rdma_cm.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include "vmbus_rdma.h"
/*
* We are emulating mlx4. May have to FIX.
*/
#include "../mlx4/user.h"
int hvnd_log_level = HVND_ERROR;
module_param(hvnd_log_level, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(hvnd_log_level,
"Logging level, "
"0 - Error (default), "
"1 - Warning, "
"2 - Info, "
"3 - Debug.");
static int disable_cq_notify = 1;
module_param(disable_cq_notify, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(disable_cq_notify,
"Disable CQ notification, "
"0 - Enable, "
"1 - Disable (default).");
enum {
MLX4_USER_DEV_CAP_64B_CQE = 1L << 0
};
#define HVND_NODE_DESC "vmbus-RDMA"
#undef MLX4_IB_UVERBS_ABI_VERSION
#define MLX4_IB_UVERBS_ABI_VERSION 4
struct mlx4_wqe_data_seg {
__be32 byte_count;
__be32 lkey;
__be64 addr;
};
/* return value:
true: ep is running
false: ep is stopped
*/
bool ep_add_work_pending(struct hvnd_ep_obj *ep_object)
{
bool ret = true;
atomic_inc(&ep_object->nr_requests_pending);
if (ep_object->stopping) {
if(atomic_dec_and_test(&ep_object->nr_requests_pending))
wake_up(&ep_object->wait_pending);
ret = false;
}
return ret;
}
void ep_del_work_pending(struct hvnd_ep_obj *ep_object)
{
if(atomic_dec_and_test(&ep_object->nr_requests_pending))
wake_up(&ep_object->wait_pending);
if(atomic_read(&ep_object->nr_requests_pending)<0) {
hvnd_error("ep_object->nr_requests_pending=%d type=%d cm_state=%d\n", atomic_read(&ep_object->nr_requests_pending), ep_object->type, ep_object->cm_state);
dump_stack();
}
}
void ep_stop(struct hvnd_ep_obj *ep_object)
{
if (!ep_object->stopping) {
ep_object->stopping = true;
hvnd_cancel_io(ep_object);
}
if(atomic_read(&ep_object->nr_requests_pending)<0) {
hvnd_error("IO canceled, ep_object->nr_requests_pending=%d type=%d cm_state=%d\n", atomic_read(&ep_object->nr_requests_pending), ep_object->type, ep_object->cm_state);
dump_stack();
}
wait_event(ep_object->wait_pending, !atomic_read(&ep_object->nr_requests_pending));
}
static int vmbus_dma_map_sg(struct device *dev, struct scatterlist *sgl,
int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
{
struct scatterlist *sg;
u64 addr;
int i;
int ret = nents;
BUG_ON(!valid_dma_direction(direction));
for_each_sg(sgl, sg, nents, i) {
addr = (u64) page_address(sg_page(sg));
/* TODO: handle highmem pages */
if (!addr) {
ret = 0;
break;
}
sg->dma_address = addr + sg->offset;
sg->dma_length = sg->length;
}
return ret;
}
static void vmbus_dma_unmap_sg(struct device *dev,
struct scatterlist *sg, int nents,
enum dma_data_direction direction, struct dma_attrs *attrs)
{
BUG_ON(!valid_dma_direction(direction));
}
struct dma_map_ops vmbus_dma_ops = {
.map_sg = vmbus_dma_map_sg,
.unmap_sg = vmbus_dma_unmap_sg,
};
static int hvnd_get_incoming_connections(struct hvnd_ep_obj *listener,
struct hvnd_dev *nd_dev,
struct hvnd_ucontext *uctx);
static struct hvnd_ep_obj *hvnd_setup_ep(struct iw_cm_id *cm_id, int ep_type,
struct hvnd_dev *nd_dev,
struct hvnd_ucontext *uctx);
static void hvnd_deinit_ep(struct hvnd_ep_obj *ep)
{
put_irp_handle(ep->nd_dev, ep->local_irp);
}
static void hvnd_destroy_ep(struct hvnd_ep_obj *ep)
{
hvnd_debug("canceling work for ep %p\n", ep);
cancel_work_sync(&ep->wrk.work);
hvnd_deinit_ep(ep);
kfree(ep);
}
#define UC(b) (((int)b)&0xff)
char *debug_inet_ntoa(struct in_addr in, char *b)
{
register char *p;
p = (char *)∈
(void)snprintf(b, 20,
"%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
return (b);
}
void hvnd_process_events(struct work_struct *work);
static int hvnd_init_ep(struct hvnd_ep_obj *ep_object,
struct iw_cm_id *cm_id, int ep_type,
struct hvnd_dev *nd_dev,
struct hvnd_ucontext *uctx)
{
int ret;
ep_object->type = ep_type;
ep_object->cm_id = cm_id;
ep_object->nd_dev = nd_dev;
ep_object->uctx = uctx;
ep_object->parent = NULL;
ep_object->wrk.callback_arg = ep_object;
INIT_WORK(&ep_object->wrk.work, hvnd_process_events);
INIT_LIST_HEAD(&ep_object->incoming_pkt_list);
spin_lock_init(&ep_object->incoming_pkt_list_lock);
ep_object->stopping = false;
atomic_set(&ep_object->nr_requests_pending, 0);
init_waitqueue_head(&ep_object->wait_pending);
ret = get_irp_handle(nd_dev, &ep_object->local_irp, (void *)ep_object);
if (ret) {
hvnd_error("get_irp_handle() failed: err: %d\n", ret);
return ret;
}
return 0;
}
static int set_rq_size(struct hvnd_dev *dev, struct ib_qp_cap *cap,
struct hvnd_qp *qp)
{
/* HW requires >= 1 RQ entry with >= 1 gather entry */
if (!cap->max_recv_wr || !cap->max_recv_sge)
return -EINVAL;
qp->rq_wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
qp->rq_max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
qp->rq_wqe_shift = ilog2(qp->rq_max_gs * sizeof (struct mlx4_wqe_data_seg));
return 0;
}
static int set_user_sq_size(struct hvnd_dev *dev,
struct hvnd_qp *qp,
struct mlx4_ib_create_qp *ucmd)
{
qp->sq_wqe_cnt = 1 << ucmd->log_sq_bb_count;
qp->sq_wqe_shift = ucmd->log_sq_stride;
qp->buf_size = (qp->rq_wqe_cnt << qp->rq_wqe_shift) +
(qp->sq_wqe_cnt << qp->sq_wqe_shift);
return 0;
}
static int hvnd_db_map_user(struct hvnd_ucontext *uctx, unsigned long virt,
struct ib_umem **db_umem)
{
struct mlx4_ib_user_db_page *page;
int err = 0;
mutex_lock(&uctx->db_page_mutex);
list_for_each_entry(page, &uctx->db_page_list, list)
if (page->user_virt == (virt & PAGE_MASK))
goto found;
page = kmalloc(sizeof *page, GFP_KERNEL);
if (!page) {
err = -ENOMEM;
goto out;
}
page->user_virt = (virt & PAGE_MASK);
page->refcnt = 0;
page->umem = ib_umem_get(&uctx->ibucontext, virt & PAGE_MASK,
PAGE_SIZE, 0, 0);
if (IS_ERR(page->umem)) {
hvnd_error("ib_umem_get failure\n");
err = PTR_ERR(page->umem);
kfree(page);
goto out;
}
list_add(&page->list, &uctx->db_page_list);
found:
++page->refcnt;
out:
mutex_unlock(&uctx->db_page_mutex);
if (!err)
*db_umem = page->umem;
return err;
}
static void hvnd_db_unmap_user(struct hvnd_ucontext *uctx, u64 db_addr)
{
struct mlx4_ib_user_db_page *page;
mutex_lock(&uctx->db_page_mutex);
list_for_each_entry(page, &uctx->db_page_list, list)
if (page->user_virt == (db_addr & PAGE_MASK))
goto found;
found:
if (!--page->refcnt) {
list_del(&page->list);
ib_umem_release(page->umem);
kfree(page);
}
mutex_unlock(&uctx->db_page_mutex);
}
static void debug_check(const char *func, int line)
{
hvnd_debug("func is: %s; line is %d\n", func, line);
if (in_interrupt()) {
hvnd_error("In interrupt func is: %s; line is %d\n", func, line);
return;
}
}
static struct ib_ah *hvnd_ah_create(struct ib_pd *pd,
struct ib_ah_attr *ah_attr)
{
debug_check(__func__, __LINE__);
return ERR_PTR(-ENOSYS);
}
static int hvnd_ah_destroy(struct ib_ah *ah)
{
debug_check(__func__, __LINE__);
return -ENOSYS;
}
static int hvnd_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
{
debug_check(__func__, __LINE__);
return -ENOSYS;
}
static int hvnd_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
{
debug_check(__func__, __LINE__);
return -ENOSYS;
}
static int hvnd_process_mad(struct ib_device *ibdev, int mad_flags,
u8 port_num, struct ib_wc *in_wc,
struct ib_grh *in_grh, struct ib_mad *in_mad,
struct ib_mad *out_mad)
{
debug_check(__func__, __LINE__);
return -ENOSYS;
}
void hvnd_acquire_uctx_ref(struct hvnd_ucontext *uctx)
{
atomic_inc(&uctx->refcnt);
}
void hvnd_drop_uctx_ref(struct hvnd_dev *nd_dev,
struct hvnd_ucontext *uctx)
{
if (atomic_dec_and_test(&uctx->refcnt)) {
hvnd_debug("uctx ref cnt dropped it is %d\n", atomic_read(&uctx->refcnt));
hvnd_debug("About to close adaptor\n");
hvnd_close_adaptor(nd_dev, uctx);
}
else
hvnd_debug("uctx ref cnt dropped it is %d\n", atomic_read(&uctx->refcnt));
}
static int hvnd_dealloc_ucontext(struct ib_ucontext *context)
{
struct hvnd_dev *nd_dev;
struct hvnd_ucontext *uctx;
uctx = to_nd_context(context);
nd_dev = to_nd_dev(context->device);
hvnd_debug("calling %s\n", __func__);
hvnd_drop_uctx_ref(nd_dev, uctx);
return 0;
}
static struct ib_ucontext *hvnd_alloc_ucontext(struct ib_device *ibdev,
struct ib_udata *udata)
{
struct hvnd_dev *nd_dev = to_nd_dev(ibdev);
struct hvnd_ucontext *uctx;
struct mlx4_ib_alloc_ucontext_resp resp;
int ret;
if (!nd_dev->ib_active) {
hvnd_error("ib device is not active, try again\n");
return ERR_PTR(-EAGAIN);
}
uctx = get_uctx(nd_dev, current_pid());
if (uctx) {
// it is already opened, just increase its reference count
hvnd_acquire_uctx_ref(uctx);
} else {
/*
* The Windows host expects the following to be done:
* 1. Successfully send struct ndv_pkt_hdr_create_1
* 2. INIT PROVIDER
* 3. Open Adapter
* Before we can complete this call.
*/
uctx = kzalloc(sizeof(struct hvnd_ucontext), GFP_KERNEL);
if (!uctx) {
return ERR_PTR(-ENOMEM);
}
atomic_set(&uctx->refcnt, 1);
INIT_LIST_HEAD(&uctx->db_page_list);
mutex_init(&uctx->db_page_mutex);
/*
* Stash away the context with the calling PID.
*/
ret = insert_handle(nd_dev, &nd_dev->uctxidr, uctx, current_pid());
if (ret) {
hvnd_error("Uctx ID insertion failed; ret is %d\n", ret);
goto err1;
}
hvnd_debug("Opening adaptor pid is %d\n", current_pid());
ret = hvnd_open_adaptor(nd_dev, uctx);
if (ret) {
hvnd_error("hvnd_open_adaptor failed ret=%d\n", ret);
goto err1;
}
}
/*
* Copy the response out.
*/
resp.dev_caps = MLX4_USER_DEV_CAP_64B_CQE;
resp.qp_tab_size = uctx->o_adap_pkt.mappings.ctx_output.qp_tab_size;
resp.bf_reg_size = uctx->o_adap_pkt.mappings.ctx_output.bf_reg_size;
resp.bf_regs_per_page = uctx->o_adap_pkt.mappings.ctx_output.bf_regs_per_page;
resp.cqe_size = uctx->o_adap_pkt.mappings.ctx_output.cqe_size;
ret = ib_copy_to_udata(udata, &resp, sizeof(resp));
if (ret) {
hvnd_error("ib_copy_to_udata failed ret=%d\n", ret);
goto err1;
}
return &uctx->ibucontext;
err1:
kfree(uctx);
return ERR_PTR(ret);
}
static int hvnd_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
{
struct hvnd_ucontext *uctx = to_nd_context(context);
if (vma->vm_end - vma->vm_start != PAGE_SIZE) {
hvnd_error("vma not a page size, actual size=%lu\n", vma->vm_end - vma->vm_start);
return -EINVAL;
}
if (vma->vm_pgoff == 0) {
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
if (io_remap_pfn_range(vma, vma->vm_start,
(uctx->uar_base >> PAGE_SHIFT),
PAGE_SIZE, vma->vm_page_prot)) {
hvnd_error("io_remap_pfn_range failure\n");
return -EAGAIN;
}
} else if (vma->vm_pgoff == 1 && uctx->bf_buf_size != 0) {
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
if (io_remap_pfn_range(vma, vma->vm_start,
(uctx->uar_base >> PAGE_SHIFT) + 1,
PAGE_SIZE, vma->vm_page_prot)) {
hvnd_error("io_remap_pfn_range failure\n");
return -EAGAIN;
}
} else {
hvnd_error("check code\n");
return -EINVAL;
}
return 0;
}
static int hvnd_deallocate_pd(struct ib_pd *pd)
{
struct hvnd_ucontext *uctx;
struct hvnd_dev *nd_dev;
struct hvnd_ib_pd *hvnd_pd;
struct ib_ucontext *ibuctx = pd->uobject->context;
hvnd_pd = to_nd_pd(pd);
nd_dev = to_nd_dev(pd->device);
uctx = to_nd_context(ibuctx);
hvnd_free_handle(nd_dev, uctx, hvnd_pd->handle,
IOCTL_ND_PD_FREE);
hvnd_drop_uctx_ref(nd_dev, uctx);
return 0;
}
static struct ib_pd *hvnd_allocate_pd(struct ib_device *ibdev,
struct ib_ucontext *context,
struct ib_udata *udata)
{
struct hvnd_ucontext *uctx;
struct hvnd_dev *nd_dev;
int ret;
struct hvnd_ib_pd *hvnd_pd;
hvnd_pd = kzalloc(sizeof(struct hvnd_ib_pd), GFP_KERNEL);
if (!hvnd_pd) {
return ERR_PTR(-ENOMEM);
}
uctx = to_nd_context(context);
nd_dev = to_nd_dev(ibdev);
ret = hvnd_create_pd(uctx, nd_dev, hvnd_pd);
if (ret) {
hvnd_error("hvnd_create_pd failure ret=%d\n", ret);
goto error_cr_pd;
}
if (context) {
if (ib_copy_to_udata(udata, &hvnd_pd->pdn, sizeof (__u32))) {
hvnd_error("ib_copy_to_udata failure\n");
ret = -EFAULT;
goto error_fault;
}
}
hvnd_acquire_uctx_ref(uctx);
return &hvnd_pd->ibpd;
error_fault:
hvnd_free_handle(nd_dev, uctx, hvnd_pd->handle,
IOCTL_ND_PD_FREE);
error_cr_pd:
kfree(hvnd_pd);
return ERR_PTR(ret);
}
static int hvnd_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
u16 *pkey)
{
debug_check(__func__, __LINE__);
*pkey = 0;
return 0;
}
static int hvnd_query_gid(struct ib_device *ibdev, u8 port, int index,
union ib_gid *gid)
{
char *ip_addr, *mac_addr;
int ret;
debug_check(__func__, __LINE__);
ret = hvnd_get_ip_addr(&ip_addr, &mac_addr);
if (ret)
return ret;
memset(&(gid->raw[0]), 0, sizeof(gid->raw));
memcpy(&(gid->raw[0]), mac_addr, 6);
return 0;
}
static int hvnd_query_device(struct ib_device *ibdev,
struct ib_device_attr *props)
{
struct hvnd_dev *nd_dev = to_nd_dev(ibdev);
struct adapter_info_v2 *adap_info;
if (!nd_dev->query_pkt_set) {
hvnd_error("query packet not received yet\n");
return -ENODATA;
}
adap_info = &nd_dev->query_pkt.ioctl.ad_info;
memset(props, 0, sizeof *props);
/*
* Copy the relevant properties out.
*/
props->fw_ver = 0;
props->device_cap_flags = 0;
//props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
//props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
//props->device_cap_flags |= IB_DEVICE_XRC;
props->vendor_id = 0x15b3;
props->vendor_part_id = adap_info->device_id;
props->max_mr_size = ~0ull;
props->page_size_cap = PAGE_SIZE;
props->max_qp = 16384;
props->max_qp_wr = min(adap_info->max_recv_q_depth,
adap_info->max_initiator_q_depth);
props->max_sge = min(adap_info->max_initiator_sge,
adap_info->max_recv_sge);
props->max_cq = 0x1FFFF;
props->max_cqe = adap_info->max_completion_q_depth;
props->max_mr = 16384;
props->max_pd = 16384;
props->max_qp_rd_atom = adap_info->max_inbound_read_limit;
props->max_qp_init_rd_atom = adap_info->max_outbound_read_limit;
props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
props->max_srq = 16384;
props->max_srq_wr = adap_info->max_recv_q_depth;
props->max_srq_sge = adap_info->max_recv_sge;
return 0;
}
static int hvnd_query_port(struct ib_device *ibdev, u8 port,
struct ib_port_attr *props)
{
memset(props, 0, sizeof(struct ib_port_attr));
props->max_mtu = IB_MTU_4096;
props->active_mtu = IB_MTU_4096;
/*
* KYS: TBD need to base this on netdev.
*/
props->state = IB_PORT_ACTIVE;
props->port_cap_flags = IB_PORT_CM_SUP;
props->gid_tbl_len = 1;
props->pkey_tbl_len = 1;
props->active_width = 1;
props->active_speed = IB_SPEED_DDR; //KYS: check
props->max_msg_sz = -1;
return 0;
}
static enum rdma_link_layer
hvnd_get_link_layer(struct ib_device *device, u8 port)
{
return IB_LINK_LAYER_ETHERNET;
}
static ssize_t hvnd_show_rev(struct device *dev, struct device_attribute *attr,
char *buf)
{
return 0;
}
static ssize_t hvnd_show_fw_ver(struct device *dev, struct device_attribute *attr,
char *buf)
{
return 0;
}
static ssize_t hvnd_show_hca(struct device *dev, struct device_attribute *attr,
char *buf)
{
return 0;
}
static ssize_t hvnd_show_board(struct device *dev, struct device_attribute *attr,
char *buf)
{
return 0;
}
static int hvnd_get_mib(struct ib_device *ibdev,
union rdma_protocol_stats *stats)
{
return 0;
}
static struct ib_qp *hvnd_ib_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attrs,
struct ib_udata *udata)
{
struct hvnd_ucontext *uctx;
struct hvnd_dev *nd_dev;
struct mlx4_ib_create_qp ucmd;
struct hvnd_qp *qp;
int ret = 0;
struct hvnd_ib_pd *hvnd_pd = to_nd_pd(pd);
struct hvnd_cq *send_cq = to_nd_cq(attrs->send_cq);
struct hvnd_cq *recv_cq = to_nd_cq(attrs->recv_cq);
uctx = get_uctx_from_pd(pd);
nd_dev = to_nd_dev(pd->device);
if (attrs->qp_type != IB_QPT_RC)
{
hvnd_error("attrs->qp_type=%d not IB_QPT_RC\n", attrs->qp_type);
return ERR_PTR(-EINVAL);
}
qp = kzalloc(sizeof *qp, GFP_KERNEL);
if (!qp) {
ret = -ENOMEM;
goto err_done;
}
qp->uctx = uctx;
if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
hvnd_error("ib_copy_from_udata failed\n");
ret = -EFAULT;
goto err_ucpy;
}
qp->qp_buf = (void *)ucmd.buf_addr;
qp->db_addr = (void *)ucmd.db_addr;
qp->log_sq_bb_count = ucmd.log_sq_bb_count;
qp->log_sq_stride = ucmd.log_sq_stride;
qp->sq_no_prefetch = ucmd.sq_no_prefetch;
qp->port = attrs->port_num;
init_waitqueue_head(&qp->wait);
atomic_set(&qp->refcnt, 1);
qp->recv_cq = recv_cq;
qp->send_cq = send_cq;
qp->nd_dev = nd_dev;
qp->receive_cq_handle = recv_cq->cq_handle;
qp->initiator_cq_handle = send_cq->cq_handle;
qp->pd_handle = hvnd_pd->handle;
qp->cq_notify = false;
qp->ibqp.qp_num = attrs->qp_type == IB_QPT_SMI ? 0 : 1;
qp->max_inline_data = attrs->cap.max_inline_data;
qp->initiator_q_depth = attrs->cap.max_send_wr;
qp->initiator_request_sge = attrs->cap.max_send_sge;
qp->receive_q_depth = attrs->cap.max_recv_wr;
qp->receive_request_sge = attrs->cap.max_recv_sge;
set_rq_size(nd_dev, &attrs->cap, qp);
set_user_sq_size(nd_dev, qp, &ucmd);
qp->umem = ib_umem_get(&uctx->ibucontext, ucmd.buf_addr,
qp->buf_size, 0, 0);
if (IS_ERR(qp->umem)) {
ret = PTR_ERR(qp->umem);
hvnd_error("ib_umem_get failed ret=%d\n", ret);
goto err_ucpy;
}
ret = hvnd_db_map_user(uctx, ucmd.db_addr, &qp->db_umem);
if (ret) {
hvnd_error("hvnd_db_map_user failed ret=%d\n", ret);
goto err_db_map;
}
ret = hvnd_create_qp(nd_dev, uctx, qp);
if (ret) {
hvnd_error("hvnd_create_qp failed ret=%d\n", ret);
goto err_qp;
}
hvnd_acquire_uctx_ref(uctx);
qp->ibqp.qp_num = qp->qpn;
qp->ibqp.qp_type = IB_QPT_RC;
return &qp->ibqp;
err_qp:
hvnd_db_unmap_user(uctx, ucmd.db_addr);
err_db_map:
ib_umem_release(qp->umem);
err_ucpy:
kfree(qp);
err_done:
return ERR_PTR(ret);
}
static int hvnd_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
int attr_mask, struct ib_udata *udata)
{
struct hvnd_qp *qp = to_nd_qp(ibqp);
struct hvnd_dev *nd_dev = to_nd_dev(ibqp->device);
enum ib_qp_state cur_state, new_state;
int ret = 0;
cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->qp_state;
new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
if (attr != NULL) {
hvnd_debug("qp->qp_state is %d new state is %d\n", qp->qp_state, new_state);
hvnd_debug("current qp state is %d\n", cur_state);
if (attr_mask & IB_QP_STATE) {
/* Ensure the state is valid */
if (attr->qp_state < 0 || attr->qp_state > IB_QPS_ERR)
{
hvnd_error("incorrect qp state attr->qp_state=%d\n", attr->qp_state);
return EINVAL;
}
if (qp->qp_state != new_state) {
qp->qp_state = new_state;
/*
* The only state transition supported is the transition to
* error state.
*/
switch (new_state) {
case IB_QPS_ERR:
case IB_QPS_SQD:
ret = hvnd_flush_qp(nd_dev, qp->uctx, qp);
if (ret)
hvnd_error("hvnd_flush_qp failed ret=%d\n", ret);
// immediately notify the upper layer on disconnection
if (!ret && qp->connector)
hvnd_process_notify_disconnect(qp->connector, STATUS_SUCCESS);
return ret;
default:
break;
}
}
}
}
return 0;
}
static int hvnd_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
int attr_mask, struct ib_qp_init_attr *init_attr)
{
struct hvnd_qp *qp = to_nd_qp(ibqp);
memset(attr, 0, sizeof *attr);
memset(init_attr, 0, sizeof *init_attr);
attr->qp_state = qp->qp_state;
init_attr->cap.max_send_wr = qp->max_send_wr;
init_attr->cap.max_recv_wr = qp->max_recv_wr;
init_attr->cap.max_send_sge = qp->max_send_sge;
init_attr->cap.max_recv_sge = qp->max_recv_sge;
init_attr->cap.max_inline_data = qp->max_inline_data;
init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
return 0;
}
static void hvnd_refuse_connection(struct hvnd_ep_obj *connector, int status);
static int hvnd_destroy_qp(struct ib_qp *ib_qp)
{
int ret;
struct hvnd_qp *qp = to_nd_qp(ib_qp);
struct hvnd_dev *nd_dev = to_nd_dev(ib_qp->device);
u64 jiffies;
if (!qp->connector) {
hvnd_warn("error: connector is NULL; skip destroying connector\n");
goto free_qp;
}
/* should we flush the qp first on ctrl-C? , no need to disconnect on abrupt shutdown?*/
if(qp->qp_state != IB_QPS_ERR && qp->qp_state != IB_QPS_SQD) {
hvnd_warn("qp_state=%d, doing abrupt disconnect\n", qp->qp_state);
hvnd_flush_qp(nd_dev, qp->uctx, qp);
ep_stop(qp->connector);
// now no pending activity is possible on the connector
switch (qp->connector->cm_state) {
case hvnd_cm_idle:
case hvnd_cm_connect_reply_refused:
case hvnd_cm_connect_request_sent:
case hvnd_cm_close_sent:
hvnd_warn("cm_state = %d not doing anything\n", qp->connector->cm_state);
break;
case hvnd_cm_connect_received:
hvnd_warn("cm_state = %d refusing pending connection request\n", qp->connector->cm_state);
hvnd_refuse_connection(qp->connector, -ECONNREFUSED);
break;
case hvnd_cm_connect_reply_sent:
case hvnd_cm_established_sent:
case hvnd_cm_accept_sent:
hvnd_warn("cm_state = %d notifying disconnect on existing connection\n", qp->connector->cm_state);
hvnd_process_notify_disconnect(qp->connector, STATUS_CANCELLED);
break;
default:
hvnd_error("unknown cm_state = %d\n", qp->connector->cm_state);
}
goto free_connector;
} else {
hvnd_debug("qp_state=%d, doing normal disconnect\n", qp->qp_state);
}
if (!ep_add_work_pending(qp->connector))
goto free_connector;
init_completion(&qp->connector->disconnect_event);
/*
* First issue a disconnect on the connector.
*/
hvnd_debug("calling hvnd_connector_disconnect\n");
ret = hvnd_connector_disconnect(nd_dev, qp->uctx,
qp->connector->ep_handle,
qp->connector);
if (ret) {
ep_del_work_pending(qp->connector);
hvnd_error("disconnect: retval is %d\n", ret);
ep_stop(qp->connector);
goto free_connector;
}
/*
* Now wait for the disconnect.
*/
jiffies = get_jiffies_64();
wait_for_completion(&qp->connector->disconnect_event);
hvnd_debug("Completed disconnect connector=%p jiffies=%llu\n", qp->connector, get_jiffies_64() - jiffies);
/*
* Now free up the connector and drop the reference on uctx.
*/
ep_stop(qp->connector);
free_connector:
hvnd_debug("destroying connector handle: %p\n", (void *) qp->connector->ep_handle);
hvnd_free_handle(nd_dev, qp->uctx,
qp->connector->ep_handle,
IOCTL_ND_CONNECTOR_FREE);
hvnd_drop_uctx_ref(nd_dev, qp->uctx);
hvnd_destroy_ep(qp->connector);
qp->connector = NULL;
free_qp:
atomic_dec(&qp->refcnt);
hvnd_debug("Waiting for the ref cnt to go to 0\n");
wait_event(qp->wait, !atomic_read(&qp->refcnt));
hvnd_debug("About to destroy qp\n");
hvnd_db_unmap_user(qp->uctx, (u64)qp->db_addr);
ib_umem_release(qp->umem);
hvnd_debug("About to free qp\n");
ret = hvnd_free_qp(nd_dev, qp->uctx, qp);