-
Notifications
You must be signed in to change notification settings - Fork 653
/
qelr_verbs.c
2520 lines (2113 loc) · 64.8 KB
/
qelr_verbs.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) 2015-2016 QLogic Corporation
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and /or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <config.h>
#include <assert.h>
#include <endian.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdbool.h>
#include "qelr.h"
#include "qelr_chain.h"
#include "qelr_verbs.h"
#include <util/compiler.h>
#include <util/util.h>
#include <util/mmio.h>
#include <stdio.h>
#include <stdlib.h>
#define QELR_SQE_ELEMENT_SIZE (sizeof(struct rdma_sq_sge))
#define QELR_RQE_ELEMENT_SIZE (sizeof(struct rdma_rq_sge))
#define QELR_CQE_SIZE (sizeof(union rdma_cqe))
static void qelr_inc_sw_cons_u16(struct qelr_qp_hwq_info *info)
{
info->cons = (info->cons + 1) % info->max_wr;
info->wqe_cons++;
}
static void qelr_inc_sw_prod_u16(struct qelr_qp_hwq_info *info)
{
info->prod = (info->prod + 1) % info->max_wr;
}
static inline int qelr_wq_is_full(struct qelr_qp_hwq_info *info)
{
return (((info->prod + 1) % info->max_wr) == info->cons);
}
int qelr_query_device(struct ibv_context *context,
struct ibv_device_attr *attr)
{
struct ibv_query_device cmd;
uint64_t fw_ver;
unsigned int major, minor, revision, eng;
int status;
bzero(attr, sizeof(*attr));
status = ibv_cmd_query_device(context, attr, &fw_ver, &cmd,
sizeof(cmd));
major = (fw_ver >> 24) & 0xff;
minor = (fw_ver >> 16) & 0xff;
revision = (fw_ver >> 8) & 0xff;
eng = fw_ver & 0xff;
snprintf(attr->fw_ver, sizeof(attr->fw_ver),
"%d.%d.%d.%d", major, minor, revision, eng);
return status;
}
int qelr_query_port(struct ibv_context *context, uint8_t port,
struct ibv_port_attr *attr)
{
struct ibv_query_port cmd;
int status;
status = ibv_cmd_query_port(context, port, attr, &cmd, sizeof(cmd));
return status;
}
struct ibv_pd *qelr_alloc_pd(struct ibv_context *context)
{
struct qelr_alloc_pd cmd;
struct qelr_alloc_pd_resp resp;
struct qelr_pd *pd;
struct qelr_devctx *cxt = get_qelr_ctx(context);
pd = malloc(sizeof(*pd));
if (!pd)
return NULL;
bzero(pd, sizeof(*pd));
memset(&cmd, 0, sizeof(cmd));
if (ibv_cmd_alloc_pd(context, &pd->ibv_pd, &cmd.ibv_cmd, sizeof(cmd),
&resp.ibv_resp, sizeof(resp))) {
free(pd);
return NULL;
}
pd->pd_id = resp.pd_id;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_INIT, "Allocated pd: %d\n", pd->pd_id);
return &pd->ibv_pd;
}
int qelr_dealloc_pd(struct ibv_pd *ibpd)
{
int rc = 0;
struct qelr_pd *pd = get_qelr_pd(ibpd);
struct qelr_devctx *cxt = get_qelr_ctx(ibpd->context);
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_INIT, "Deallocated pd: %d\n",
pd->pd_id);
rc = ibv_cmd_dealloc_pd(ibpd);
if (rc)
return rc;
free(pd);
return rc;
}
struct ibv_mr *qelr_reg_mr(struct ibv_pd *ibpd, void *addr, size_t len,
uint64_t hca_va, int access)
{
struct qelr_mr *mr;
struct ibv_reg_mr cmd;
struct qelr_reg_mr_resp resp;
struct qelr_pd *pd = get_qelr_pd(ibpd);
struct qelr_devctx *cxt = get_qelr_ctx(ibpd->context);
mr = malloc(sizeof(*mr));
if (!mr)
return NULL;
bzero(mr, sizeof(*mr));
if (ibv_cmd_reg_mr(ibpd, addr, len, hca_va, access, &mr->vmr, &cmd,
sizeof(cmd), &resp.ibv_resp, sizeof(resp))) {
free(mr);
return NULL;
}
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_MR,
"MR Register %p completed successfully pd_id=%d addr=%p len=%zu access=%d lkey=%x rkey=%x\n",
mr, pd->pd_id, addr, len, access, mr->vmr.ibv_mr.lkey,
mr->vmr.ibv_mr.rkey);
return &mr->vmr.ibv_mr;
}
int qelr_dereg_mr(struct verbs_mr *vmr)
{
struct qelr_devctx *cxt = get_qelr_ctx(vmr->ibv_mr.context);
int rc;
rc = ibv_cmd_dereg_mr(vmr);
if (rc)
return rc;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_MR,
"MR DERegister %p completed successfully\n", vmr);
free(vmr);
return 0;
}
static void consume_cqe(struct qelr_cq *cq)
{
if (cq->latest_cqe == cq->toggle_cqe)
cq->chain_toggle ^= RDMA_CQE_REQUESTER_TOGGLE_BIT_MASK;
cq->latest_cqe = qelr_chain_consume(&cq->chain);
}
static inline int qelr_cq_entries(int entries)
{
/* FW requires an extra entry */
return entries + 1;
}
struct ibv_cq *qelr_create_cq(struct ibv_context *context, int cqe,
struct ibv_comp_channel *channel,
int comp_vector)
{
struct qelr_devctx *cxt = get_qelr_ctx(context);
struct qelr_create_cq_resp resp = {};
struct qelr_create_cq cmd;
struct qelr_cq *cq;
int chain_size;
int rc;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_CQ,
"create cq: context=%p, cqe=%d, channel=%p, comp_vector=%d\n",
context, cqe, channel, comp_vector);
if (!cqe || cqe > cxt->max_cqes) {
DP_ERR(cxt->dbg_fp,
"create cq: failed. attempted to allocate %d cqes but valid range is 1...%d\n",
cqe, cqe > cxt->max_cqes);
return NULL;
}
/* allocate CQ structure */
cq = calloc(1, sizeof(*cq));
if (!cq)
return NULL;
/* allocate CQ buffer */
chain_size = qelr_cq_entries(cqe) * QELR_CQE_SIZE;
rc = qelr_chain_alloc(&cq->chain, chain_size, cxt->kernel_page_size,
QELR_CQE_SIZE);
if (rc)
goto err_0;
cmd.addr = (uintptr_t) cq->chain.first_addr;
cmd.len = cq->chain.size;
rc = ibv_cmd_create_cq(context, cqe, channel, comp_vector,
&cq->ibv_cq, &cmd.ibv_cmd, sizeof(cmd),
&resp.ibv_resp, sizeof(resp));
if (rc) {
DP_ERR(cxt->dbg_fp, "create cq: failed with rc = %d\n", rc);
goto err_1;
}
/* map the doorbell and prepare its data */
cq->db.data.icid = htole16(resp.icid);
cq->db.data.params = DB_AGG_CMD_SET <<
RDMA_PWM_VAL32_DATA_AGG_CMD_SHIFT;
cq->db_addr = cxt->db_addr + resp.db_offset;
if (resp.db_rec_addr) {
cq->db_rec_map = mmap(NULL, cxt->kernel_page_size, PROT_WRITE,
MAP_SHARED, context->cmd_fd,
resp.db_rec_addr);
if (cq->db_rec_map == MAP_FAILED) {
int errsv = errno;
DP_ERR(cxt->dbg_fp,
"alloc context: doorbell rec mapping failed resp.db_rec_addr = %llx size=%d context->cmd_fd=%d errno=%d\n",
resp.db_rec_addr, cxt->kernel_page_size,
context->cmd_fd, errsv);
goto err_1;
}
cq->db_rec_addr = cq->db_rec_map;
} else {
/* Kernel doesn't support doorbell recovery. Point to dummy
* location instead
*/
cq->db_rec_addr = &cxt->db_rec_addr_dummy;
}
/* point to the very last element, passing this we will toggle */
cq->toggle_cqe = qelr_chain_get_last_elem(&cq->chain);
cq->chain_toggle = RDMA_CQE_REQUESTER_TOGGLE_BIT_MASK;
cq->latest_cqe = NULL; /* must be different from chain_toggle */
consume_cqe(cq);
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_CQ,
"create cq: successfully created %p\n", cq);
return &cq->ibv_cq;
err_1:
qelr_chain_free(&cq->chain);
err_0:
free(cq);
return NULL;
}
int qelr_destroy_cq(struct ibv_cq *ibv_cq)
{
struct qelr_devctx *cxt = get_qelr_ctx(ibv_cq->context);
struct qelr_cq *cq = get_qelr_cq(ibv_cq);
int rc;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_CQ, "destroy cq: %p\n", cq);
rc = ibv_cmd_destroy_cq(ibv_cq);
if (rc) {
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_CQ,
"destroy cq: failed to destroy %p, got %d.\n", cq,
rc);
return rc;
}
qelr_chain_free(&cq->chain);
if (cq->db_rec_map)
munmap(cq->db_rec_map, cxt->kernel_page_size);
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_CQ,
"destroy cq: successfully destroyed %p\n", cq);
free(cq);
return 0;
}
int qelr_query_srq(struct ibv_srq *ibv_srq, struct ibv_srq_attr *attr)
{
struct ibv_query_srq cmd;
return ibv_cmd_query_srq(ibv_srq, attr, &cmd, sizeof(cmd));
}
int qelr_modify_srq(struct ibv_srq *srq, struct ibv_srq_attr *attr,
int attr_mask)
{
struct ibv_modify_srq cmd;
return ibv_cmd_modify_srq(srq, attr, attr_mask, &cmd, sizeof(cmd));
}
static void qelr_destroy_srq_buffers(struct ibv_srq *ibv_srq)
{
struct qelr_srq *srq = get_qelr_srq(ibv_srq);
uint32_t *virt_prod_pair_addr;
uint32_t prod_size;
qelr_chain_free(&srq->hw_srq.chain);
virt_prod_pair_addr = srq->hw_srq.virt_prod_pair_addr;
prod_size = sizeof(struct rdma_srq_producers);
ibv_dofork_range(virt_prod_pair_addr, prod_size);
munmap(virt_prod_pair_addr, prod_size);
}
int qelr_destroy_srq(struct ibv_srq *ibv_srq)
{
struct qelr_srq *srq = get_qelr_srq(ibv_srq);
int ret;
ret = ibv_cmd_destroy_srq(ibv_srq);
if (ret)
return ret;
qelr_destroy_srq_buffers(ibv_srq);
free(srq);
return 0;
}
static void qelr_create_srq_configure_req(struct qelr_srq *srq,
struct qelr_create_srq *req)
{
req->srq_addr = (uintptr_t)srq->hw_srq.chain.first_addr;
req->srq_len = srq->hw_srq.chain.size;
req->prod_pair_addr = (uintptr_t)srq->hw_srq.virt_prod_pair_addr;
}
static int qelr_create_srq_buffers(struct qelr_devctx *cxt,
struct qelr_srq *srq,
struct ibv_srq_init_attr *attrs)
{
uint32_t max_wr, max_sges;
int chain_size, prod_size;
void *addr;
int rc;
max_wr = attrs->attr.max_wr;
if (!max_wr)
return -EINVAL;
max_wr = min_t(uint32_t, max_wr, cxt->max_srq_wr);
max_sges = max_wr * (cxt->sges_per_srq_wr + 1); /* +1 for header */
chain_size = max_sges * QELR_RQE_ELEMENT_SIZE;
rc = qelr_chain_alloc(&srq->hw_srq.chain, chain_size,
cxt->kernel_page_size, QELR_RQE_ELEMENT_SIZE);
if (rc) {
DP_ERR(cxt->dbg_fp,
"create srq: failed to map srq, got %d", rc);
return rc;
}
prod_size = sizeof(struct rdma_srq_producers);
addr = mmap(NULL, prod_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1,
0);
if (addr == MAP_FAILED) {
DP_ERR(cxt->dbg_fp,
"create srq: failed to map producer, got %d", errno);
qelr_chain_free(&srq->hw_srq.chain);
return errno;
}
rc = ibv_dontfork_range(addr, prod_size);
if (rc) {
munmap(addr, prod_size);
qelr_chain_free(&srq->hw_srq.chain);
return rc;
}
srq->hw_srq.virt_prod_pair_addr = addr;
srq->hw_srq.max_sges = cxt->sges_per_srq_wr;
srq->hw_srq.max_wr = max_wr;
return 0;
}
struct ibv_srq *qelr_create_srq(struct ibv_pd *pd,
struct ibv_srq_init_attr *init_attr)
{
struct qelr_devctx *cxt = get_qelr_ctx(pd->context);
struct qelr_create_srq req;
struct qelr_create_srq_resp resp;
struct qelr_srq *srq;
int ret;
srq = calloc(1, sizeof(*srq));
if (!srq)
return NULL;
ret = qelr_create_srq_buffers(cxt, srq, init_attr);
if (ret) {
free(srq);
return NULL;
}
pthread_spin_init(&srq->lock, PTHREAD_PROCESS_PRIVATE);
qelr_create_srq_configure_req(srq, &req);
ret = ibv_cmd_create_srq(pd, &srq->ibv_srq, init_attr, &req.ibv_cmd,
sizeof(req), &resp.ibv_resp, sizeof(resp));
if (ret) {
qelr_destroy_srq_buffers(&srq->ibv_srq);
free(srq);
return NULL;
}
return &srq->ibv_srq;
}
static void qelr_free_rq(struct qelr_qp *qp)
{
free(qp->rqe_wr_id);
}
static void qelr_free_sq(struct qelr_qp *qp)
{
free(qp->wqe_wr_id);
}
static void qelr_chain_free_sq(struct qelr_qp *qp)
{
qelr_chain_free(&qp->sq.chain);
}
static void qelr_chain_free_rq(struct qelr_qp *qp)
{
qelr_chain_free(&qp->rq.chain);
}
static inline int qelr_create_qp_buffers_sq(struct qelr_devctx *cxt,
struct qelr_qp *qp,
struct ibv_qp_init_attr *attrs)
{
uint32_t max_send_wr, max_send_sges, max_send_buf;
int chain_size;
int rc;
/* SQ */
max_send_wr = attrs->cap.max_send_wr;
max_send_wr = max_t(uint32_t, max_send_wr, 1);
max_send_wr = min_t(uint32_t, max_send_wr, cxt->max_send_wr);
max_send_sges = max_send_wr * cxt->sges_per_send_wr;
max_send_buf = max_send_sges * QELR_SQE_ELEMENT_SIZE;
chain_size = max_send_buf;
rc = qelr_chain_alloc(&qp->sq.chain, chain_size, cxt->kernel_page_size,
QELR_SQE_ELEMENT_SIZE);
if (rc)
DP_ERR(cxt->dbg_fp, "create qp: failed to map SQ chain, got %d", rc);
qp->sq.max_wr = max_send_wr;
qp->sq.max_sges = cxt->sges_per_send_wr;
return rc;
}
static inline int qelr_create_qp_buffers_rq(struct qelr_devctx *cxt,
struct qelr_qp *qp,
struct ibv_qp_init_attr *attrs)
{
uint32_t max_recv_wr, max_recv_sges, max_recv_buf;
int chain_size;
int rc;
/* RQ */
max_recv_wr = attrs->cap.max_recv_wr;
max_recv_wr = max_t(uint32_t, max_recv_wr, 1);
max_recv_wr = min_t(uint32_t, max_recv_wr, cxt->max_recv_wr);
max_recv_sges = max_recv_wr * cxt->sges_per_recv_wr;
max_recv_buf = max_recv_sges * QELR_RQE_ELEMENT_SIZE;
chain_size = max_recv_buf;
rc = qelr_chain_alloc(&qp->rq.chain, chain_size, cxt->kernel_page_size,
QELR_RQE_ELEMENT_SIZE);
if (rc)
DP_ERR(cxt->dbg_fp, "create qp: failed to map RQ chain, got %d", rc);
qp->rq.max_wr = max_recv_wr;
qp->rq.max_sges = cxt->sges_per_recv_wr;
return rc;
}
static inline int qelr_create_qp_buffers(struct qelr_devctx *cxt,
struct qelr_qp *qp,
struct ibv_qp_init_attr *attrs)
{
int rc;
rc = qelr_create_qp_buffers_sq(cxt, qp, attrs);
if (rc)
return rc;
rc = qelr_create_qp_buffers_rq(cxt, qp, attrs);
if (rc) {
qelr_chain_free_sq(qp);
if (qp->sq.db_rec_map)
munmap(qp->sq.db_rec_map, cxt->kernel_page_size);
return rc;
}
return 0;
}
static inline int qelr_configure_qp_sq(struct qelr_devctx *cxt,
struct qelr_qp *qp,
struct ibv_qp_init_attr *attrs,
struct qelr_create_qp_resp *resp)
{
qp->sq.icid = resp->sq_icid;
qp->sq.db_data.data.icid = htole16(resp->sq_icid);
qp->sq.prod = 0;
qp->sq.db = cxt->db_addr + resp->sq_db_offset;
qp->sq.edpm_db = cxt->db_addr;
if (resp->sq_db_rec_addr) {
qp->sq.db_rec_map = mmap(NULL, cxt->kernel_page_size,
PROT_WRITE, MAP_SHARED,
cxt->ibv_ctx.context.cmd_fd,
resp->sq_db_rec_addr);
if (qp->sq.db_rec_map == MAP_FAILED) {
int errsv = errno;
DP_ERR(cxt->dbg_fp,
"alloc context: doorbell rec mapping failed resp.db_rec_addr = %llx size=%d context->cmd_fd=%d errno=%d\n",
resp->sq_db_rec_addr, cxt->kernel_page_size,
cxt->ibv_ctx.context.cmd_fd, errsv);
return -ENOMEM;
}
qp->sq.db_rec_addr = qp->sq.db_rec_map;
} else {
/* Kernel doesn't support doorbell recovery. Point to dummy
* location instead
*/
qp->sq.db_rec_addr = &cxt->db_rec_addr_dummy;
}
/* shadow SQ */
qp->sq.max_wr++; /* prod/cons method requires N+1 elements */
qp->wqe_wr_id = calloc(qp->sq.max_wr, sizeof(*qp->wqe_wr_id));
if (!qp->wqe_wr_id) {
DP_ERR(cxt->dbg_fp,
"create qp: failed shadow SQ memory allocation\n");
return -ENOMEM;
}
return 0;
}
static inline int qelr_configure_qp_rq(struct qelr_devctx *cxt,
struct qelr_qp *qp,
struct ibv_qp_init_attr *attrs,
struct qelr_create_qp_resp *resp)
{
/* RQ */
qp->rq.icid = resp->rq_icid;
qp->rq.db_data.data.icid = htole16(resp->rq_icid);
qp->rq.db = cxt->db_addr + resp->rq_db_offset;
qp->rq.iwarp_db2 = cxt->db_addr + resp->rq_db2_offset;
qp->rq.iwarp_db2_data.data.icid = htole16(qp->rq.icid);
qp->rq.iwarp_db2_data.data.value = htole16(DQ_TCM_IWARP_POST_RQ_CF_CMD);
qp->rq.prod = 0;
if (resp->rq_db_rec_addr) {
qp->rq.db_rec_map = mmap(NULL, cxt->kernel_page_size,
PROT_WRITE, MAP_SHARED,
cxt->ibv_ctx.context.cmd_fd,
resp->rq_db_rec_addr);
if (qp->rq.db_rec_map == MAP_FAILED) {
int errsv = errno;
DP_ERR(cxt->dbg_fp,
"alloc context: doorbell rec mapping failed resp.db_rec_addr = %llx size=%d context->cmd_fd=%d errno=%d\n",
resp->rq_db_rec_addr, cxt->kernel_page_size,
cxt->ibv_ctx.context.cmd_fd, errsv);
return -ENOMEM;
}
qp->rq.db_rec_addr = qp->rq.db_rec_map;
} else {
/* Kernel doesn't support doorbell recovery. Point to dummy
* location instead
*/
qp->rq.db_rec_addr = &cxt->db_rec_addr_dummy;
}
/* shadow RQ */
qp->rq.max_wr++; /* prod/cons method requires N+1 elements */
qp->rqe_wr_id = calloc(qp->rq.max_wr, sizeof(*qp->rqe_wr_id));
if (!qp->rqe_wr_id) {
DP_ERR(cxt->dbg_fp,
"create qp: failed shadow RQ memory allocation\n");
return -ENOMEM;
}
return 0;
}
static inline int qelr_configure_qp(struct qelr_devctx *cxt, struct qelr_qp *qp,
struct ibv_qp_init_attr *attrs,
struct qelr_create_qp_resp *resp)
{
int rc;
/* general */
pthread_spin_init(&qp->q_lock, PTHREAD_PROCESS_PRIVATE);
qp->qp_id = resp->qp_id;
qp->state = QELR_QPS_RST;
qp->sq_sig_all = attrs->sq_sig_all;
qp->atomic_supported = resp->atomic_supported;
if (cxt->dpm_flags & QELR_DPM_FLAGS_EDPM_MODE)
qp->edpm_mode = 1;
rc = qelr_configure_qp_sq(cxt, qp, attrs, resp);
if (rc)
return rc;
rc = qelr_configure_qp_rq(cxt, qp, attrs, resp);
if (rc)
qelr_free_sq(qp);
return rc;
}
static inline void qelr_print_qp_init_attr(
struct qelr_devctx *cxt,
struct ibv_qp_init_attr *attr)
{
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_QP,
"create qp: send_cq=%p, recv_cq=%p, srq=%p, max_inline_data=%d, max_recv_sge=%d, max_recv_wr=%d, max_send_sge=%d, max_send_wr=%d, qp_type=%d, sq_sig_all=%d\n",
attr->send_cq, attr->recv_cq, attr->srq,
attr->cap.max_inline_data, attr->cap.max_recv_sge,
attr->cap.max_recv_wr, attr->cap.max_send_sge,
attr->cap.max_send_wr, attr->qp_type, attr->sq_sig_all);
}
static inline void
qelr_create_qp_configure_sq_req(struct qelr_qp *qp,
struct qelr_create_qp *req)
{
req->sq_addr = (uintptr_t)qp->sq.chain.first_addr;
req->sq_len = qp->sq.chain.size;
}
static inline void
qelr_create_qp_configure_rq_req(struct qelr_qp *qp,
struct qelr_create_qp *req)
{
req->rq_addr = (uintptr_t)qp->rq.chain.first_addr;
req->rq_len = qp->rq.chain.size;
}
static inline void
qelr_create_qp_configure_req(struct qelr_qp *qp,
struct qelr_create_qp *req)
{
memset(req, 0, sizeof(*req));
req->qp_handle_hi = U64_HI(qp);
req->qp_handle_lo = U64_LO(qp);
qelr_create_qp_configure_sq_req(qp, req);
qelr_create_qp_configure_rq_req(qp, req);
}
struct ibv_qp *qelr_create_qp(struct ibv_pd *pd,
struct ibv_qp_init_attr *attrs)
{
struct qelr_devctx *cxt = get_qelr_ctx(pd->context);
struct qelr_create_qp_resp resp = {};
struct qelr_create_qp req;
struct qelr_qp *qp;
int rc;
qelr_print_qp_init_attr(cxt, attrs);
qp = calloc(1, sizeof(*qp));
if (!qp)
return NULL;
if (attrs->srq)
qp->srq = get_qelr_srq(attrs->srq);
rc = qelr_create_qp_buffers(cxt, qp, attrs);
if (rc)
goto err0;
qelr_create_qp_configure_req(qp, &req);
rc = ibv_cmd_create_qp(pd, &qp->ibv_qp, attrs, &req.ibv_cmd,
sizeof(req), &resp.ibv_resp, sizeof(resp));
if (rc) {
DP_ERR(cxt->dbg_fp,
"create qp: failed on ibv_cmd_create_qp with %d\n", rc);
goto err1;
}
rc = qelr_configure_qp(cxt, qp, attrs, &resp);
if (rc)
goto err2;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_QP,
"create qp: successfully created %p. handle_hi=%x handle_lo=%x\n",
qp, req.qp_handle_hi, req.qp_handle_lo);
return &qp->ibv_qp;
err2:
rc = ibv_cmd_destroy_qp(&qp->ibv_qp);
if (rc)
DP_ERR(cxt->dbg_fp, "create qp: fatal fault. rc=%d\n", rc);
err1:
qelr_chain_free_sq(qp);
qelr_chain_free_rq(qp);
err0:
free(qp);
return NULL;
}
static void qelr_print_ah_attr(struct qelr_devctx *cxt, struct ibv_ah_attr *attr)
{
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_QP,
"grh.dgid=[%#" PRIx64 ":%#" PRIx64 "], grh.flow_label=%d, grh.sgid_index=%d, grh.hop_limit=%d, grh.traffic_class=%d, dlid=%d, sl=%d, src_path_bits=%d, static_rate = %d, port_num=%d\n",
be64toh(attr->grh.dgid.global.interface_id),
be64toh(attr->grh.dgid.global.subnet_prefix),
attr->grh.flow_label, attr->grh.hop_limit,
attr->grh.sgid_index, attr->grh.traffic_class, attr->dlid,
attr->sl, attr->src_path_bits,
attr->static_rate, attr->port_num);
}
static void qelr_print_qp_attr(struct qelr_devctx *cxt, struct ibv_qp_attr *attr)
{
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_QP,
"\tqp_state=%d\tcur_qp_state=%d\tpath_mtu=%d\tpath_mig_state=%d\tqkey=%d\trq_psn=%d\tsq_psn=%d\tdest_qp_num=%d\tqp_access_flags=%d\tmax_inline_data=%d\tmax_recv_sge=%d\tmax_recv_wr=%d\tmax_send_sge=%d\tmax_send_wr=%d\tpkey_index=%d\talt_pkey_index=%d\ten_sqd_async_notify=%d\tsq_draining=%d\tmax_rd_atomic=%d\tmax_dest_rd_atomic=%d\tmin_rnr_timer=%d\tport_num=%d\ttimeout=%d\tretry_cnt=%d\trnr_retry=%d\talt_port_num=%d\talt_timeout=%d\n",
attr->qp_state, attr->cur_qp_state, attr->path_mtu,
attr->path_mig_state, attr->qkey, attr->rq_psn, attr->sq_psn,
attr->dest_qp_num, attr->qp_access_flags,
attr->cap.max_inline_data, attr->cap.max_recv_sge,
attr->cap.max_recv_wr, attr->cap.max_send_sge,
attr->cap.max_send_wr, attr->pkey_index,
attr->alt_pkey_index, attr->en_sqd_async_notify,
attr->sq_draining, attr->max_rd_atomic,
attr->max_dest_rd_atomic, attr->min_rnr_timer,
attr->port_num, attr->timeout, attr->retry_cnt,
attr->rnr_retry, attr->alt_port_num, attr->alt_timeout);
qelr_print_ah_attr(cxt, &attr->ah_attr);
qelr_print_ah_attr(cxt, &attr->alt_ah_attr);
}
int qelr_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
int attr_mask, struct ibv_qp_init_attr *init_attr)
{
struct ibv_query_qp cmd;
struct qelr_devctx *cxt = get_qelr_ctx(qp->context);
int rc;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_QP, "QP Query %p, attr_mask=0x%x\n",
get_qelr_qp(qp), attr_mask);
rc = ibv_cmd_query_qp(qp, attr, attr_mask,
init_attr, &cmd, sizeof(cmd));
qelr_print_qp_attr(cxt, attr);
return rc;
}
static enum qelr_qp_state get_qelr_qp_state(enum ibv_qp_state qps)
{
switch (qps) {
case IBV_QPS_RESET:
return QELR_QPS_RST;
case IBV_QPS_INIT:
return QELR_QPS_INIT;
case IBV_QPS_RTR:
return QELR_QPS_RTR;
case IBV_QPS_RTS:
return QELR_QPS_RTS;
case IBV_QPS_SQD:
return QELR_QPS_SQD;
case IBV_QPS_SQE:
return QELR_QPS_SQE;
case IBV_QPS_ERR:
default:
return QELR_QPS_ERR;
};
}
static void qelr_reset_qp_hwq_info(struct qelr_qp_hwq_info *q)
{
qelr_chain_reset(&q->chain);
q->prod = 0;
q->cons = 0;
q->wqe_cons = 0;
q->db_data.data.value = 0;
}
static int qelr_update_qp_state(struct qelr_qp *qp,
enum ibv_qp_state new_ib_state)
{
int status = 0;
enum qelr_qp_state new_state;
/* iWARP states are updated implicitely by driver and don't have a
* real purpose in user-lib.
*/
if (IS_IWARP(qp->ibv_qp.context->device))
return 0;
new_state = get_qelr_qp_state(new_ib_state);
pthread_spin_lock(&qp->q_lock);
if (new_state == qp->state) {
pthread_spin_unlock(&qp->q_lock);
return 0;
}
switch (qp->state) {
case QELR_QPS_RST:
switch (new_state) {
case QELR_QPS_INIT:
qp->prev_wqe_size = 0;
qelr_reset_qp_hwq_info(&qp->sq);
qelr_reset_qp_hwq_info(&qp->rq);
break;
default:
status = -EINVAL;
break;
};
break;
case QELR_QPS_INIT:
/* INIT->XXX */
switch (new_state) {
case QELR_QPS_RTR:
/* Update doorbell (in case post_recv was done before
* move to RTR)
*/
if (IS_ROCE(qp->ibv_qp.context->device)) {
mmio_wc_start();
writel(qp->rq.db_data.raw, qp->rq.db);
mmio_flush_writes();
}
break;
case QELR_QPS_ERR:
break;
default:
/* invalid state change. */
status = -EINVAL;
break;
};
break;
case QELR_QPS_RTR:
/* RTR->XXX */
switch (new_state) {
case QELR_QPS_RTS:
break;
case QELR_QPS_ERR:
break;
default:
/* invalid state change. */
status = -EINVAL;
break;
};
break;
case QELR_QPS_RTS:
/* RTS->XXX */
switch (new_state) {
case QELR_QPS_SQD:
case QELR_QPS_SQE:
break;
case QELR_QPS_ERR:
break;
default:
/* invalid state change. */
status = -EINVAL;
break;
};
break;
case QELR_QPS_SQD:
/* SQD->XXX */
switch (new_state) {
case QELR_QPS_RTS:
case QELR_QPS_SQE:
case QELR_QPS_ERR:
break;
default:
/* invalid state change. */
status = -EINVAL;
break;
};
break;
case QELR_QPS_SQE:
switch (new_state) {
case QELR_QPS_RTS:
case QELR_QPS_ERR:
break;
default:
/* invalid state change. */
status = -EINVAL;
break;
};
break;
case QELR_QPS_ERR:
/* ERR->XXX */
switch (new_state) {
case QELR_QPS_RST:
break;
default:
status = -EINVAL;
break;
};
break;
default:
status = -EINVAL;
break;
};
if (!status)
qp->state = new_state;
pthread_spin_unlock(&qp->q_lock);
return status;
}
int qelr_modify_qp(struct ibv_qp *ibqp, struct ibv_qp_attr *attr,
int attr_mask)
{
struct ibv_modify_qp cmd = {};
struct qelr_qp *qp = get_qelr_qp(ibqp);
struct qelr_devctx *cxt = get_qelr_ctx(ibqp->context);
union ibv_gid sgid, *p_dgid;
int rc;
DP_VERBOSE(cxt->dbg_fp, QELR_MSG_QP, "QP Modify %p, attr_mask=0x%x\n",
qp, attr_mask);
qelr_print_qp_attr(cxt, attr);
rc = ibv_cmd_modify_qp(ibqp, attr, attr_mask, &cmd, sizeof(cmd));
if (rc) {
DP_ERR(cxt->dbg_fp, "QP Modify: Failed command. rc=%d\n", rc);