/
verbs.c
1913 lines (1650 loc) · 47.4 KB
/
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
/*
* Broadcom NetXtreme-E User Space RoCE driver
*
* Copyright (c) 2015-2017, Broadcom. All rights reserved. The term
* Broadcom refers to Broadcom Limited and/or its subsidiaries.
*
* 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
* BSD license below:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Description: User IB-Verbs implementation
*/
#include <assert.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 <netinet/in.h>
#include <unistd.h>
#include <util/compiler.h>
#include "main.h"
#include "verbs.h"
int bnxt_re_query_device(struct ibv_context *context,
const struct ibv_query_device_ex_input *input,
struct ibv_device_attr_ex *attr, size_t attr_size)
{
struct ib_uverbs_ex_query_device_resp resp;
size_t resp_size = sizeof(resp);
uint8_t fw_ver[8];
int err;
err = ibv_cmd_query_device_any(context, input, attr, attr_size, &resp,
&resp_size);
if (err)
return err;
memcpy(fw_ver, &resp.base.fw_ver, sizeof(resp.base.fw_ver));
snprintf(attr->orig_attr.fw_ver, 64, "%d.%d.%d.%d", fw_ver[0],
fw_ver[1], fw_ver[2], fw_ver[3]);
return 0;
}
int bnxt_re_query_port(struct ibv_context *ibvctx, uint8_t port,
struct ibv_port_attr *port_attr)
{
struct ibv_query_port cmd;
return ibv_cmd_query_port(ibvctx, port, port_attr, &cmd, sizeof(cmd));
}
struct ibv_pd *bnxt_re_alloc_pd(struct ibv_context *ibvctx)
{
struct ibv_alloc_pd cmd;
struct ubnxt_re_pd_resp resp;
struct bnxt_re_context *cntx = to_bnxt_re_context(ibvctx);
struct bnxt_re_dev *dev = to_bnxt_re_dev(ibvctx->device);
struct bnxt_re_pd *pd;
uint64_t dbr;
pd = calloc(1, sizeof(*pd));
if (!pd)
return NULL;
memset(&resp, 0, sizeof(resp));
if (ibv_cmd_alloc_pd(ibvctx, &pd->ibvpd, &cmd, sizeof(cmd),
&resp.ibv_resp, sizeof(resp)))
goto out;
pd->pdid = resp.pdid;
dbr = resp.dbr;
static_assert(offsetof(struct ubnxt_re_pd_resp, dbr) == 4 * 3,
"Bad dbr placement");
/* Map DB page now. */
if (!cntx->udpi.dbpage) {
cntx->udpi.dpindx = resp.dpi;
cntx->udpi.dbpage = mmap(NULL, dev->pg_size, PROT_WRITE,
MAP_SHARED, ibvctx->cmd_fd, dbr);
if (cntx->udpi.dbpage == MAP_FAILED) {
(void)ibv_cmd_dealloc_pd(&pd->ibvpd);
goto out;
}
}
return &pd->ibvpd;
out:
free(pd);
return NULL;
}
int bnxt_re_free_pd(struct ibv_pd *ibvpd)
{
struct bnxt_re_pd *pd = to_bnxt_re_pd(ibvpd);
int status;
status = ibv_cmd_dealloc_pd(ibvpd);
if (status)
return status;
/* DPI un-mapping will be during uninit_ucontext */
free(pd);
return 0;
}
struct ibv_mr *bnxt_re_reg_mr(struct ibv_pd *ibvpd, void *sva, size_t len,
uint64_t hca_va, int access)
{
struct bnxt_re_mr *mr;
struct ibv_reg_mr cmd;
struct ubnxt_re_mr_resp resp;
mr = calloc(1, sizeof(*mr));
if (!mr)
return NULL;
if (ibv_cmd_reg_mr(ibvpd, sva, len, hca_va, access, &mr->vmr, &cmd,
sizeof(cmd), &resp.ibv_resp, sizeof(resp))) {
free(mr);
return NULL;
}
return &mr->vmr.ibv_mr;
}
int bnxt_re_dereg_mr(struct verbs_mr *vmr)
{
struct bnxt_re_mr *mr = (struct bnxt_re_mr *)vmr;
int status;
status = ibv_cmd_dereg_mr(vmr);
if (status)
return status;
free(mr);
return 0;
}
struct ibv_cq *bnxt_re_create_cq(struct ibv_context *ibvctx, int ncqe,
struct ibv_comp_channel *channel, int vec)
{
struct bnxt_re_cq *cq;
struct ubnxt_re_cq cmd;
struct ubnxt_re_cq_resp resp;
struct bnxt_re_context *cntx = to_bnxt_re_context(ibvctx);
struct bnxt_re_dev *dev = to_bnxt_re_dev(ibvctx->device);
if (!ncqe || ncqe > dev->max_cq_depth) {
errno = EINVAL;
return NULL;
}
cq = calloc(1, sizeof(*cq));
if (!cq)
return NULL;
cq->cqq.depth = roundup_pow_of_two(ncqe + 1);
if (cq->cqq.depth > dev->max_cq_depth + 1)
cq->cqq.depth = dev->max_cq_depth + 1;
cq->cqq.stride = dev->cqe_size;
if (bnxt_re_alloc_aligned(&cq->cqq, dev->pg_size))
goto fail;
pthread_spin_init(&cq->cqq.qlock, PTHREAD_PROCESS_PRIVATE);
cmd.cq_va = (uintptr_t)cq->cqq.va;
cmd.cq_handle = (uintptr_t)cq;
memset(&resp, 0, sizeof(resp));
if (ibv_cmd_create_cq(ibvctx, ncqe, channel, vec,
&cq->ibvcq, &cmd.ibv_cmd, sizeof(cmd),
&resp.ibv_resp, sizeof(resp)))
goto cmdfail;
cq->cqid = resp.cqid;
cq->phase = resp.phase;
cq->cqq.tail = resp.tail;
cq->udpi = &cntx->udpi;
cq->first_arm = true;
list_head_init(&cq->sfhead);
list_head_init(&cq->rfhead);
return &cq->ibvcq;
cmdfail:
bnxt_re_free_aligned(&cq->cqq);
fail:
free(cq);
return NULL;
}
int bnxt_re_destroy_cq(struct ibv_cq *ibvcq)
{
int status;
struct bnxt_re_cq *cq = to_bnxt_re_cq(ibvcq);
status = ibv_cmd_destroy_cq(ibvcq);
if (status)
return status;
bnxt_re_free_aligned(&cq->cqq);
free(cq);
return 0;
}
static uint8_t bnxt_re_poll_err_scqe(struct bnxt_re_qp *qp,
struct ibv_wc *ibvwc,
struct bnxt_re_bcqe *hdr,
struct bnxt_re_req_cqe *scqe, int *cnt)
{
struct bnxt_re_queue *sq = qp->jsqq->hwque;
struct bnxt_re_context *cntx;
struct bnxt_re_wrid *swrid;
struct bnxt_re_psns *spsn;
struct bnxt_re_cq *scq;
uint8_t status;
uint32_t head;
scq = to_bnxt_re_cq(qp->ibvqp.send_cq);
head = qp->jsqq->last_idx;
cntx = to_bnxt_re_context(scq->ibvcq.context);
swrid = &qp->jsqq->swque[head];
spsn = swrid->psns;
*cnt = 1;
status = (le32toh(hdr->flg_st_typ_ph) >> BNXT_RE_BCQE_STATUS_SHIFT) &
BNXT_RE_BCQE_STATUS_MASK;
ibvwc->status = bnxt_re_to_ibv_wc_status(status, true);
ibvwc->wc_flags = 0;
ibvwc->wr_id = swrid->wrid;
ibvwc->qp_num = qp->qpid;
ibvwc->opcode = (le32toh(spsn->opc_spsn) >>
BNXT_RE_PSNS_OPCD_SHIFT) &
BNXT_RE_PSNS_OPCD_MASK;
ibvwc->byte_len = 0;
bnxt_re_incr_head(sq, swrid->slots);
bnxt_re_jqq_mod_last(qp->jsqq, head);
if (qp->qpst != IBV_QPS_ERR)
qp->qpst = IBV_QPS_ERR;
pthread_spin_lock(&cntx->fqlock);
bnxt_re_fque_add_node(&scq->sfhead, &qp->snode);
pthread_spin_unlock(&cntx->fqlock);
return false;
}
static uint8_t bnxt_re_poll_success_scqe(struct bnxt_re_qp *qp,
struct ibv_wc *ibvwc,
struct bnxt_re_bcqe *hdr,
struct bnxt_re_req_cqe *scqe,
int *cnt)
{
struct bnxt_re_queue *sq = qp->jsqq->hwque;
struct bnxt_re_wrid *swrid;
struct bnxt_re_psns *spsn;
uint8_t pcqe = false;
uint32_t cindx;
uint32_t head;
head = qp->jsqq->last_idx;
swrid = &qp->jsqq->swque[head];
spsn = swrid->psns;
cindx = le32toh(scqe->con_indx) & (qp->cap.max_swr - 1);
if (!(swrid->sig & IBV_SEND_SIGNALED)) {
*cnt = 0;
} else {
ibvwc->status = IBV_WC_SUCCESS;
ibvwc->wc_flags = 0;
ibvwc->qp_num = qp->qpid;
ibvwc->wr_id = swrid->wrid;
ibvwc->opcode = (le32toh(spsn->opc_spsn) >>
BNXT_RE_PSNS_OPCD_SHIFT) &
BNXT_RE_PSNS_OPCD_MASK;
if (ibvwc->opcode == IBV_WC_RDMA_READ ||
ibvwc->opcode == IBV_WC_COMP_SWAP ||
ibvwc->opcode == IBV_WC_FETCH_ADD)
ibvwc->byte_len = swrid->bytes;
*cnt = 1;
}
bnxt_re_incr_head(sq, swrid->slots);
bnxt_re_jqq_mod_last(qp->jsqq, head);
if (qp->jsqq->last_idx != cindx)
pcqe = true;
return pcqe;
}
static uint8_t bnxt_re_poll_scqe(struct bnxt_re_qp *qp, struct ibv_wc *ibvwc,
void *cqe, int *cnt)
{
struct bnxt_re_bcqe *hdr;
struct bnxt_re_req_cqe *scqe;
uint8_t status, pcqe = false;
scqe = cqe;
hdr = cqe + sizeof(struct bnxt_re_req_cqe);
status = (le32toh(hdr->flg_st_typ_ph) >> BNXT_RE_BCQE_STATUS_SHIFT) &
BNXT_RE_BCQE_STATUS_MASK;
if (status == BNXT_RE_REQ_ST_OK)
pcqe = bnxt_re_poll_success_scqe(qp, ibvwc, hdr, scqe, cnt);
else
pcqe = bnxt_re_poll_err_scqe(qp, ibvwc, hdr, scqe, cnt);
return pcqe;
}
static void bnxt_re_release_srqe(struct bnxt_re_srq *srq, int tag)
{
pthread_spin_lock(&srq->srqq->qlock);
srq->srwrid[srq->last_idx].next_idx = tag;
srq->last_idx = tag;
srq->srwrid[srq->last_idx].next_idx = -1;
pthread_spin_unlock(&srq->srqq->qlock);
}
static int bnxt_re_poll_err_rcqe(struct bnxt_re_qp *qp, struct ibv_wc *ibvwc,
struct bnxt_re_bcqe *hdr, void *cqe)
{
struct bnxt_re_context *cntx;
struct bnxt_re_wrid *swque;
struct bnxt_re_queue *rq;
uint8_t status, cnt = 0;
struct bnxt_re_cq *rcq;
uint32_t head = 0;
rcq = to_bnxt_re_cq(qp->ibvqp.recv_cq);
cntx = to_bnxt_re_context(rcq->ibvcq.context);
if (!qp->srq) {
rq = qp->jrqq->hwque;
head = qp->jrqq->last_idx;
swque = &qp->jrqq->swque[head];
ibvwc->wr_id = swque->wrid;
cnt = swque->slots;
} else {
struct bnxt_re_srq *srq;
int tag;
srq = qp->srq;
rq = srq->srqq;
cnt = 1;
tag = le32toh(hdr->qphi_rwrid) & BNXT_RE_BCQE_RWRID_MASK;
ibvwc->wr_id = srq->srwrid[tag].wrid;
bnxt_re_release_srqe(srq, tag);
}
status = (le32toh(hdr->flg_st_typ_ph) >> BNXT_RE_BCQE_STATUS_SHIFT) &
BNXT_RE_BCQE_STATUS_MASK;
/* skip h/w flush errors */
if (status == BNXT_RE_RSP_ST_HW_FLUSH)
return 0;
ibvwc->status = bnxt_re_to_ibv_wc_status(status, false);
ibvwc->qp_num = qp->qpid;
ibvwc->opcode = IBV_WC_RECV;
ibvwc->byte_len = 0;
ibvwc->wc_flags = 0;
if (qp->qptyp == IBV_QPT_UD)
ibvwc->src_qp = 0;
if (!qp->srq)
bnxt_re_jqq_mod_last(qp->jrqq, head);
bnxt_re_incr_head(rq, cnt);
if (!qp->srq) {
pthread_spin_lock(&cntx->fqlock);
bnxt_re_fque_add_node(&rcq->rfhead, &qp->rnode);
pthread_spin_unlock(&cntx->fqlock);
}
return 1;
}
static void bnxt_re_fill_ud_cqe(struct ibv_wc *ibvwc,
struct bnxt_re_bcqe *hdr, void *cqe, uint8_t flags)
{
struct bnxt_re_ud_cqe *ucqe = cqe;
uint32_t qpid;
qpid = ((le32toh(hdr->qphi_rwrid) >> BNXT_RE_BCQE_SRCQP_SHIFT) &
BNXT_RE_BCQE_SRCQP_SHIFT) << 0x10; /* higher 8 bits of 24 */
qpid |= (le64toh(ucqe->qplo_mac) >> BNXT_RE_UD_CQE_SRCQPLO_SHIFT) &
BNXT_RE_UD_CQE_SRCQPLO_MASK; /*lower 16 of 24 */
ibvwc->src_qp = qpid;
ibvwc->wc_flags |= IBV_WC_GRH;
ibvwc->sl = (flags & BNXT_RE_UD_FLAGS_IP_VER_MASK) >>
BNXT_RE_UD_FLAGS_IP_VER_SFT;
/*IB-stack ABI in user do not ask for MAC to be reported. */
}
static void bnxt_re_poll_success_rcqe(struct bnxt_re_qp *qp,
struct ibv_wc *ibvwc,
struct bnxt_re_bcqe *hdr, void *cqe)
{
uint8_t flags, is_imm, is_rdma;
struct bnxt_re_rc_cqe *rcqe;
struct bnxt_re_wrid *swque;
struct bnxt_re_queue *rq;
uint32_t rcqe_len;
uint32_t head = 0;
uint8_t cnt = 0;
rcqe = cqe;
if (!qp->srq) {
rq = qp->jrqq->hwque;
head = qp->jrqq->last_idx;
swque = &qp->jrqq->swque[head];
ibvwc->wr_id = swque->wrid;
cnt = swque->slots;
} else {
struct bnxt_re_srq *srq;
int tag;
srq = qp->srq;
rq = srq->srqq;
tag = le32toh(hdr->qphi_rwrid) & BNXT_RE_BCQE_RWRID_MASK;
ibvwc->wr_id = srq->srwrid[tag].wrid;
cnt = 1;
bnxt_re_release_srqe(srq, tag);
}
ibvwc->status = IBV_WC_SUCCESS;
ibvwc->qp_num = qp->qpid;
rcqe_len = le32toh(rcqe->length);
ibvwc->byte_len = (qp->qptyp == IBV_QPT_UD) ?
rcqe_len & BNXT_RE_UD_CQE_LEN_MASK: rcqe_len ;
ibvwc->opcode = IBV_WC_RECV;
flags = (le32toh(hdr->flg_st_typ_ph) >> BNXT_RE_BCQE_FLAGS_SHIFT) &
BNXT_RE_BCQE_FLAGS_MASK;
is_imm = (flags & BNXT_RE_RC_FLAGS_IMM_MASK) >>
BNXT_RE_RC_FLAGS_IMM_SHIFT;
is_rdma = (flags & BNXT_RE_RC_FLAGS_RDMA_MASK) >>
BNXT_RE_RC_FLAGS_RDMA_SHIFT;
ibvwc->wc_flags = 0;
if (is_imm) {
ibvwc->wc_flags |= IBV_WC_WITH_IMM;
/* Completion reports the raw-data in LE format, While
* user expects it in BE format. Thus, swapping on outgoing
* data is needed. On a BE platform le32toh will do the swap
* while on LE platform htobe32 will do the job.
*/
ibvwc->imm_data = htobe32(le32toh(rcqe->imm_key));
if (is_rdma)
ibvwc->opcode = IBV_WC_RECV_RDMA_WITH_IMM;
}
if (qp->qptyp == IBV_QPT_UD)
bnxt_re_fill_ud_cqe(ibvwc, hdr, cqe, flags);
if (!qp->srq)
bnxt_re_jqq_mod_last(qp->jrqq, head);
bnxt_re_incr_head(rq, cnt);
}
static uint8_t bnxt_re_poll_rcqe(struct bnxt_re_qp *qp, struct ibv_wc *ibvwc,
void *cqe, int *cnt)
{
struct bnxt_re_bcqe *hdr;
uint8_t status, pcqe = false;
hdr = cqe + sizeof(struct bnxt_re_rc_cqe);
status = (le32toh(hdr->flg_st_typ_ph) >> BNXT_RE_BCQE_STATUS_SHIFT) &
BNXT_RE_BCQE_STATUS_MASK;
*cnt = 1;
if (status == BNXT_RE_RSP_ST_OK)
bnxt_re_poll_success_rcqe(qp, ibvwc, hdr, cqe);
else
*cnt = bnxt_re_poll_err_rcqe(qp, ibvwc, hdr, cqe);
return pcqe;
}
static uint8_t bnxt_re_poll_term_cqe(struct bnxt_re_qp *qp,
struct ibv_wc *ibvwc, void *cqe, int *cnt)
{
struct bnxt_re_context *cntx;
struct bnxt_re_cq *scq, *rcq;
uint8_t pcqe = false;
scq = to_bnxt_re_cq(qp->ibvqp.send_cq);
rcq = to_bnxt_re_cq(qp->ibvqp.recv_cq);
cntx = to_bnxt_re_context(scq->ibvcq.context);
/* For now just add the QP to flush list without
* considering the index reported in the CQE.
* Continue reporting flush completions until the
* SQ and RQ are empty.
*/
*cnt = 0;
if (qp->qpst != IBV_QPS_ERR)
qp->qpst = IBV_QPS_ERR;
pthread_spin_lock(&cntx->fqlock);
bnxt_re_fque_add_node(&rcq->rfhead, &qp->rnode);
bnxt_re_fque_add_node(&scq->sfhead, &qp->snode);
pthread_spin_unlock(&cntx->fqlock);
return pcqe;
}
static int bnxt_re_poll_one(struct bnxt_re_cq *cq, int nwc, struct ibv_wc *wc)
{
struct bnxt_re_queue *cqq = &cq->cqq;
struct bnxt_re_qp *qp;
struct bnxt_re_bcqe *hdr;
struct bnxt_re_req_cqe *scqe;
struct bnxt_re_ud_cqe *rcqe;
void *cqe;
uint64_t *qp_handle = NULL;
int type, cnt = 0, dqed = 0, hw_polled = 0;
uint8_t pcqe = false;
while (nwc) {
cqe = cqq->va + cqq->head * bnxt_re_get_cqe_sz();
hdr = cqe + sizeof(struct bnxt_re_req_cqe);
if (!bnxt_re_is_cqe_valid(cq, hdr))
break;
type = (le32toh(hdr->flg_st_typ_ph) >>
BNXT_RE_BCQE_TYPE_SHIFT) & BNXT_RE_BCQE_TYPE_MASK;
switch (type) {
case BNXT_RE_WC_TYPE_SEND:
scqe = cqe;
qp_handle = (uint64_t *)&scqe->qp_handle;
qp = (struct bnxt_re_qp *)
(uintptr_t)le64toh(scqe->qp_handle);
if (!qp)
break; /*stale cqe. should be rung.*/
pcqe = bnxt_re_poll_scqe(qp, wc, cqe, &cnt);
break;
case BNXT_RE_WC_TYPE_RECV_RC:
case BNXT_RE_WC_TYPE_RECV_UD:
rcqe = cqe;
qp_handle = (uint64_t *)&rcqe->qp_handle;
qp = (struct bnxt_re_qp *)
(uintptr_t)le64toh(rcqe->qp_handle);
if (!qp)
break; /*stale cqe. should be rung.*/
pcqe = bnxt_re_poll_rcqe(qp, wc, cqe, &cnt);
break;
case BNXT_RE_WC_TYPE_RECV_RAW:
break;
case BNXT_RE_WC_TYPE_TERM:
scqe = cqe;
qp_handle = (uint64_t *)&scqe->qp_handle;
qp = (struct bnxt_re_qp *)
(uintptr_t)le64toh(scqe->qp_handle);
if (!qp)
break;
pcqe = bnxt_re_poll_term_cqe(qp, wc, cqe, &cnt);
break;
case BNXT_RE_WC_TYPE_COFF:
break;
default:
break;
};
if (pcqe)
goto skipp_real;
hw_polled++;
if (qp_handle) {
*qp_handle = 0x0ULL; /* mark cqe as read */
qp_handle = NULL;
}
bnxt_re_incr_head(&cq->cqq, 1);
bnxt_re_change_cq_phase(cq);
skipp_real:
if (cnt) {
cnt = 0;
dqed++;
nwc--;
wc++;
}
}
if (hw_polled)
bnxt_re_ring_cq_db(cq);
return dqed;
}
static int bnxt_re_poll_flush_wcs(struct bnxt_re_joint_queue *jqq,
struct ibv_wc *ibvwc, uint32_t qpid,
int nwc)
{
uint8_t opcode = IBV_WC_RECV;
struct bnxt_re_queue *que;
struct bnxt_re_wrid *wrid;
struct bnxt_re_psns *psns;
uint32_t cnt = 0;
que = jqq->hwque;
while (nwc) {
if (bnxt_re_is_que_empty(que))
break;
wrid = &jqq->swque[jqq->last_idx];
if (wrid->psns) {
psns = wrid->psns;
opcode = (le32toh(psns->opc_spsn) >>
BNXT_RE_PSNS_OPCD_SHIFT) &
BNXT_RE_PSNS_OPCD_MASK;
}
ibvwc->status = IBV_WC_WR_FLUSH_ERR;
ibvwc->opcode = opcode;
ibvwc->wr_id = wrid->wrid;
ibvwc->qp_num = qpid;
ibvwc->byte_len = 0;
ibvwc->wc_flags = 0;
bnxt_re_jqq_mod_last(jqq, jqq->last_idx);
bnxt_re_incr_head(que, wrid->slots);
nwc--;
cnt++;
ibvwc++;
}
return cnt;
}
static int bnxt_re_poll_flush_wqes(struct bnxt_re_cq *cq,
struct list_head *lhead,
struct ibv_wc *ibvwc,
int32_t nwc)
{
struct bnxt_re_fque_node *cur, *tmp;
struct bnxt_re_joint_queue *jqq;
struct bnxt_re_qp *qp;
bool sq_list = false;
uint32_t polled = 0;
sq_list = (lhead == &cq->sfhead) ? true : false;
if (!list_empty(lhead)) {
list_for_each_safe(lhead, cur, tmp, list) {
if (sq_list) {
qp = container_of(cur, struct bnxt_re_qp,
snode);
jqq = qp->jsqq;
} else {
qp = container_of(cur, struct bnxt_re_qp,
rnode);
jqq = qp->jrqq;
}
if (bnxt_re_is_que_empty(jqq->hwque))
continue;
polled += bnxt_re_poll_flush_wcs(jqq, ibvwc + polled,
qp->qpid,
nwc - polled);
if (!(nwc - polled))
break;
}
}
return polled;
}
static int bnxt_re_poll_flush_lists(struct bnxt_re_cq *cq, uint32_t nwc,
struct ibv_wc *ibvwc)
{
int left, polled = 0;
/* Check if flush Qs are empty */
if (list_empty(&cq->sfhead) && list_empty(&cq->rfhead))
return 0;
polled = bnxt_re_poll_flush_wqes(cq, &cq->sfhead, ibvwc, nwc);
left = nwc - polled;
if (!left)
return polled;
polled += bnxt_re_poll_flush_wqes(cq, &cq->rfhead,
ibvwc + polled, left);
return polled;
}
int bnxt_re_poll_cq(struct ibv_cq *ibvcq, int nwc, struct ibv_wc *wc)
{
struct bnxt_re_cq *cq = to_bnxt_re_cq(ibvcq);
struct bnxt_re_context *cntx = to_bnxt_re_context(ibvcq->context);
int dqed, left = 0;
pthread_spin_lock(&cq->cqq.qlock);
dqed = bnxt_re_poll_one(cq, nwc, wc);
if (cq->deferred_arm) {
bnxt_re_ring_cq_arm_db(cq, cq->deferred_arm_flags);
cq->deferred_arm = false;
cq->deferred_arm_flags = 0;
}
pthread_spin_unlock(&cq->cqq.qlock);
left = nwc - dqed;
if (left) {
/* Check if anything is there to flush. */
pthread_spin_lock(&cntx->fqlock);
dqed += bnxt_re_poll_flush_lists(cq, left, (wc + dqed));
pthread_spin_unlock(&cntx->fqlock);
}
return dqed;
}
static void bnxt_re_cleanup_cq(struct bnxt_re_qp *qp, struct bnxt_re_cq *cq)
{
struct bnxt_re_context *cntx;
struct bnxt_re_queue *que = &cq->cqq;
struct bnxt_re_bcqe *hdr;
struct bnxt_re_req_cqe *scqe;
struct bnxt_re_rc_cqe *rcqe;
void *cqe;
int indx, type;
cntx = to_bnxt_re_context(cq->ibvcq.context);
pthread_spin_lock(&que->qlock);
for (indx = 0; indx < que->depth; indx++) {
cqe = que->va + indx * bnxt_re_get_cqe_sz();
hdr = cqe + sizeof(struct bnxt_re_req_cqe);
type = (le32toh(hdr->flg_st_typ_ph) >>
BNXT_RE_BCQE_TYPE_SHIFT) & BNXT_RE_BCQE_TYPE_MASK;
if (type == BNXT_RE_WC_TYPE_COFF)
continue;
if (type == BNXT_RE_WC_TYPE_SEND ||
type == BNXT_RE_WC_TYPE_TERM) {
scqe = cqe;
if (le64toh(scqe->qp_handle) == (uintptr_t)qp)
scqe->qp_handle = 0ULL;
} else {
rcqe = cqe;
if (le64toh(rcqe->qp_handle) == (uintptr_t)qp)
rcqe->qp_handle = 0ULL;
}
}
pthread_spin_unlock(&que->qlock);
pthread_spin_lock(&cntx->fqlock);
bnxt_re_fque_del_node(&qp->snode);
bnxt_re_fque_del_node(&qp->rnode);
pthread_spin_unlock(&cntx->fqlock);
}
int bnxt_re_arm_cq(struct ibv_cq *ibvcq, int flags)
{
struct bnxt_re_cq *cq = to_bnxt_re_cq(ibvcq);
pthread_spin_lock(&cq->cqq.qlock);
flags = !flags ? BNXT_RE_QUE_TYPE_CQ_ARMALL :
BNXT_RE_QUE_TYPE_CQ_ARMSE;
if (cq->first_arm) {
bnxt_re_ring_cq_arm_db(cq, flags);
cq->first_arm = false;
}
cq->deferred_arm = true;
cq->deferred_arm_flags = flags;
pthread_spin_unlock(&cq->cqq.qlock);
return 0;
}
static int bnxt_re_check_qp_limits(struct bnxt_re_context *cntx,
struct ibv_qp_init_attr *attr)
{
struct ibv_device_attr *devattr;
struct bnxt_re_dev *rdev;
rdev = cntx->rdev;
devattr = &rdev->devattr;
if (attr->cap.max_send_sge > devattr->max_sge)
return EINVAL;
if (attr->cap.max_recv_sge > devattr->max_sge)
return EINVAL;
if (attr->cap.max_inline_data > BNXT_RE_MAX_INLINE_SIZE)
return EINVAL;
if (attr->cap.max_send_wr > devattr->max_qp_wr)
attr->cap.max_send_wr = devattr->max_qp_wr;
if (attr->cap.max_recv_wr > devattr->max_qp_wr)
attr->cap.max_recv_wr = devattr->max_qp_wr;
return 0;
}
static void bnxt_re_free_queue_ptr(struct bnxt_re_qp *qp)
{
if (qp->jrqq) {
free(qp->jrqq->hwque);
free(qp->jrqq);
}
if (qp->jsqq) {
free(qp->jsqq->hwque);
free(qp->jsqq);
}
}
static int bnxt_re_alloc_queue_ptr(struct bnxt_re_qp *qp,
struct ibv_qp_init_attr *attr)
{
int rc = -ENOMEM;
qp->jsqq = calloc(1, sizeof(struct bnxt_re_joint_queue));
if (!qp->jsqq)
return rc;
qp->jsqq->hwque = calloc(1, sizeof(struct bnxt_re_queue));
if (!qp->jsqq->hwque)
goto fail;
if (!attr->srq) {
qp->jrqq = calloc(1, sizeof(struct bnxt_re_joint_queue));
if (!qp->jrqq)
goto fail;
qp->jrqq->hwque = calloc(1, sizeof(struct bnxt_re_queue));
if (!qp->jrqq->hwque)
goto fail;
}
return 0;
fail:
bnxt_re_free_queue_ptr(qp);
return rc;
}
static void bnxt_re_free_queues(struct bnxt_re_qp *qp)
{
if (qp->jrqq) {
free(qp->jrqq->swque);
pthread_spin_destroy(&qp->jrqq->hwque->qlock);
bnxt_re_free_aligned(qp->jrqq->hwque);
}
free(qp->jsqq->swque);
pthread_spin_destroy(&qp->jsqq->hwque->qlock);
bnxt_re_free_aligned(qp->jsqq->hwque);
}
static int bnxt_re_alloc_init_swque(struct bnxt_re_joint_queue *jqq, int nwr)
{
int indx;
jqq->swque = calloc(nwr, sizeof(struct bnxt_re_wrid));
if (!jqq->swque)
return -ENOMEM;
jqq->start_idx = 0;
jqq->last_idx = nwr - 1;
for (indx = 0; indx < nwr; indx++)
jqq->swque[indx].next_idx = indx + 1;
jqq->swque[jqq->last_idx].next_idx = 0;
jqq->last_idx = 0;
return 0;
}
static int bnxt_re_calc_wqe_sz(int nsge)
{
/* This is used for both sq and rq. In case hdr size differs
* in future move to individual functions.
*/
return sizeof(struct bnxt_re_sge) * nsge + bnxt_re_get_sqe_hdr_sz();
}
static int bnxt_re_get_rq_slots(struct bnxt_re_dev *rdev,
struct bnxt_re_qp *qp, uint32_t nrwr,
uint32_t nsge)
{
uint32_t max_wqesz;
uint32_t wqe_size;
uint32_t stride;
uint32_t slots;
stride = sizeof(struct bnxt_re_sge);
max_wqesz = bnxt_re_calc_wqe_sz(rdev->devattr.max_sge);
wqe_size = bnxt_re_calc_wqe_sz(nsge);
if (wqe_size > max_wqesz)
return -EINVAL;
if (qp->qpmode == BNXT_RE_WQE_MODE_STATIC)
wqe_size = bnxt_re_calc_wqe_sz(6);
qp->jrqq->hwque->esize = wqe_size;
qp->jrqq->hwque->max_slots = wqe_size / stride;
slots = (nrwr * wqe_size) / stride;
return slots;
}
static int bnxt_re_get_sq_slots(struct bnxt_re_dev *rdev,
struct bnxt_re_qp *qp, uint32_t nswr,
uint32_t nsge, uint32_t *ils)
{
uint32_t max_wqesz;
uint32_t wqe_size;
uint32_t cal_ils;
uint32_t stride;
uint32_t ilsize;
uint32_t hdr_sz;
uint32_t slots;
hdr_sz = bnxt_re_get_sqe_hdr_sz();
stride = sizeof(struct bnxt_re_sge);
max_wqesz = bnxt_re_calc_wqe_sz(rdev->devattr.max_sge);
ilsize = get_aligned(*ils, hdr_sz);
wqe_size = bnxt_re_calc_wqe_sz(nsge);
if (ilsize) {
cal_ils = hdr_sz + ilsize;
wqe_size = MAX(cal_ils, wqe_size);
wqe_size = get_aligned(wqe_size, hdr_sz);
}
if (wqe_size > max_wqesz)
return -EINVAL;
if (qp->qpmode == BNXT_RE_WQE_MODE_STATIC)
wqe_size = bnxt_re_calc_wqe_sz(6);
if (*ils)
*ils = wqe_size - hdr_sz;
qp->jsqq->hwque->esize = wqe_size;
qp->jsqq->hwque->max_slots = (qp->qpmode == BNXT_RE_WQE_MODE_STATIC) ?
wqe_size / stride : 1;
slots = (nswr * wqe_size) / stride;
return slots;
}
static int bnxt_re_alloc_queues(struct bnxt_re_dev *dev,
struct bnxt_re_qp *qp,
struct ibv_qp_init_attr *attr,
uint32_t pg_size) {
struct bnxt_re_psns_ext *psns_ext;
struct bnxt_re_wrid *swque;
struct bnxt_re_queue *que;
struct bnxt_re_psns *psns;
uint32_t nswr, diff;
uint32_t psn_depth;
uint32_t psn_size;
uint32_t nsge;
int ret, indx;
int nslots;
que = qp->jsqq->hwque;
diff = (qp->qpmode == BNXT_RE_WQE_MODE_VARIABLE) ?
0 : BNXT_RE_FULL_FLAG_DELTA;
nswr = roundup_pow_of_two(attr->cap.max_send_wr + 1 + diff);
nsge = attr->cap.max_send_sge;
if (nsge % 2)
nsge++;
nslots = bnxt_re_get_sq_slots(dev, qp, nswr, nsge,
&attr->cap.max_inline_data);
if (nslots < 0)
return nslots;
que->stride = sizeof(struct bnxt_re_sge);
que->depth = nslots;
que->diff = (diff * que->esize) / que->stride;
/* psn_depth extra entries of size que->stride */
psn_size = bnxt_re_is_chip_gen_p5(qp->cctx) ?
sizeof(struct bnxt_re_psns_ext) :