Skip to content

Commit 83b0baf

Browse files
Luoyouminghaoyue-Xu
authored andcommitted
libhns: Refactor rq inline
The ibv_sge struct is enough, there is no need to customize the hns_roce_rinl_sge struct. Refactored structures and functions for reuse in cqe inline (rq, srq scenarios). Signed-off-by: Luoyouming <luoyouming@huawei.com> Signed-off-by: Haoyue Xu <xuhaoyue1@hisilicon.com>
1 parent 3261cff commit 83b0baf

File tree

3 files changed

+73
-81
lines changed

3 files changed

+73
-81
lines changed

providers/hns/hns_roce_u.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,8 @@ struct hns_roce_sge_ex {
290290
unsigned int sge_shift;
291291
};
292292

293-
struct hns_roce_rinl_sge {
294-
void *addr;
295-
unsigned int len;
296-
};
297-
298293
struct hns_roce_rinl_wqe {
299-
struct hns_roce_rinl_sge *sg_list;
294+
struct ibv_sge *sg_list;
300295
unsigned int sge_cnt;
301296
};
302297

providers/hns/hns_roce_u_hw_v2.c

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <stdio.h>
3535
#include <string.h>
3636
#include <sys/mman.h>
37+
#include <ccan/minmax.h>
3738
#include "hns_roce_u.h"
3839
#include "hns_roce_u_db.h"
3940
#include "hns_roce_u_hw_v2.h"
@@ -416,46 +417,41 @@ static void get_opcode_for_resp(struct hns_roce_v2_cqe *cqe, struct ibv_wc *wc,
416417
wc->opcode = wc_rcv_op_map[opcode];
417418
}
418419

419-
static int handle_recv_inl_wqe(struct hns_roce_v2_cqe *cqe, struct ibv_wc *wc,
420-
struct hns_roce_qp **cur_qp, uint32_t opcode)
420+
static void handle_recv_inl_data(struct hns_roce_v2_cqe *cqe,
421+
struct hns_roce_rinl_buf *rinl_buf,
422+
uint32_t wr_cnt, uint8_t *buf)
421423
{
422-
if (((*cur_qp)->verbs_qp.qp.qp_type == IBV_QPT_RC) &&
423-
(opcode == HNS_ROCE_RECV_OP_SEND ||
424-
opcode == HNS_ROCE_RECV_OP_SEND_WITH_IMM ||
425-
opcode == HNS_ROCE_RECV_OP_SEND_WITH_INV) &&
426-
hr_reg_read(cqe, CQE_RQ_INLINE)) {
427-
struct hns_roce_rinl_sge *sge_list;
428-
uint32_t wr_num, wr_cnt, sge_num, data_len;
429-
uint8_t *wqe_buf;
430-
uint32_t sge_cnt, size;
424+
struct ibv_sge *sge_list;
425+
uint32_t sge_num, data_len;
426+
uint32_t sge_cnt, size;
431427

432-
wr_num = hr_reg_read(cqe, CQE_WQE_IDX);
433-
wr_cnt = wr_num & ((*cur_qp)->rq.wqe_cnt - 1);
428+
sge_list = rinl_buf->wqe_list[wr_cnt].sg_list;
429+
sge_num = rinl_buf->wqe_list[wr_cnt].sge_cnt;
434430

435-
sge_list = (*cur_qp)->rq_rinl_buf.wqe_list[wr_cnt].sg_list;
436-
sge_num = (*cur_qp)->rq_rinl_buf.wqe_list[wr_cnt].sge_cnt;
437-
wqe_buf = (uint8_t *)get_recv_wqe_v2(*cur_qp, wr_cnt);
431+
data_len = le32toh(cqe->byte_cnt);
438432

439-
data_len = wc->byte_len;
433+
for (sge_cnt = 0; (sge_cnt < sge_num) && (data_len); sge_cnt++) {
434+
size = min(sge_list[sge_cnt].length, data_len);
440435

441-
for (sge_cnt = 0; (sge_cnt < sge_num) && (data_len);
442-
sge_cnt++) {
443-
size = sge_list[sge_cnt].len < data_len ?
444-
sge_list[sge_cnt].len : data_len;
436+
memcpy((void *)(uintptr_t)sge_list[sge_cnt].addr, (void *)buf, size);
437+
data_len -= size;
438+
buf += size;
439+
}
445440

446-
memcpy((void *)sge_list[sge_cnt].addr,
447-
(void *)wqe_buf, size);
448-
data_len -= size;
449-
wqe_buf += size;
450-
}
441+
if (data_len)
442+
hr_reg_write(cqe, CQE_STATUS, HNS_ROCE_V2_CQE_LOCAL_LENGTH_ERR);
443+
}
451444

452-
if (data_len) {
453-
wc->status = IBV_WC_LOC_LEN_ERR;
454-
return V2_CQ_POLL_ERR;
455-
}
456-
}
445+
static void handle_recv_rq_inl(struct hns_roce_v2_cqe *cqe,
446+
struct hns_roce_qp *cur_qp)
447+
{
448+
uint8_t *wqe_buf;
449+
uint32_t wr_num;
457450

458-
return V2_CQ_OK;
451+
wr_num = hr_reg_read(cqe, CQE_WQE_IDX) & (cur_qp->rq.wqe_cnt - 1);
452+
453+
wqe_buf = (uint8_t *)get_recv_wqe_v2(cur_qp, wr_num);
454+
handle_recv_inl_data(cqe, &cur_qp->rq_rinl_buf, wr_num, wqe_buf);
459455
}
460456

461457
static void parse_for_ud_qp(struct hns_roce_v2_cqe *cqe, struct ibv_wc *wc)
@@ -478,10 +474,9 @@ static void parse_cqe_for_srq(struct hns_roce_v2_cqe *cqe, struct ibv_wc *wc,
478474
}
479475

480476
static int parse_cqe_for_resp(struct hns_roce_v2_cqe *cqe, struct ibv_wc *wc,
481-
struct hns_roce_qp *hr_qp, uint8_t opcode)
477+
struct hns_roce_qp *hr_qp)
482478
{
483479
struct hns_roce_wq *wq;
484-
int ret;
485480

486481
wq = &hr_qp->rq;
487482
wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
@@ -490,12 +485,8 @@ static int parse_cqe_for_resp(struct hns_roce_v2_cqe *cqe, struct ibv_wc *wc,
490485
if (hr_qp->verbs_qp.qp.qp_type == IBV_QPT_UD)
491486
parse_for_ud_qp(cqe, wc);
492487

493-
ret = handle_recv_inl_wqe(cqe, wc, &hr_qp, opcode);
494-
if (ret) {
495-
verbs_err(verbs_get_ctx(hr_qp->verbs_qp.qp.context),
496-
PFX "failed to handle recv inline wqe!\n");
497-
return ret;
498-
}
488+
if (hr_reg_read(cqe, CQE_RQ_INLINE))
489+
handle_recv_rq_inl(cqe, hr_qp);
499490

500491
return 0;
501492
}
@@ -624,7 +615,7 @@ static int parse_cqe_for_cq(struct hns_roce_context *ctx, struct hns_roce_cq *cq
624615
if (srq)
625616
parse_cqe_for_srq(cqe, wc, srq);
626617
else
627-
parse_cqe_for_resp(cqe, wc, cur_qp, opcode);
618+
parse_cqe_for_resp(cqe, wc, cur_qp);
628619
}
629620

630621
return 0;
@@ -1353,26 +1344,31 @@ static void fill_recv_sge_to_wqe(struct ibv_recv_wr *wr, void *wqe,
13531344
}
13541345
}
13551346

1347+
static void fill_recv_inl_buf(struct hns_roce_rinl_buf *rinl_buf,
1348+
unsigned int wqe_idx, struct ibv_recv_wr *wr)
1349+
{
1350+
struct ibv_sge *sge_list;
1351+
unsigned int i;
1352+
1353+
if (!rinl_buf->wqe_cnt)
1354+
return;
1355+
1356+
sge_list = rinl_buf->wqe_list[wqe_idx].sg_list;
1357+
rinl_buf->wqe_list[wqe_idx].sge_cnt = (unsigned int)wr->num_sge;
1358+
for (i = 0; i < wr->num_sge; i++)
1359+
memcpy((void *)&sge_list[i], (void *)&wr->sg_list[i],
1360+
sizeof(struct ibv_sge));
1361+
}
1362+
13561363
static void fill_rq_wqe(struct hns_roce_qp *qp, struct ibv_recv_wr *wr,
13571364
unsigned int wqe_idx, unsigned int max_sge)
13581365
{
1359-
struct hns_roce_rinl_sge *sge_list;
1360-
unsigned int i;
13611366
void *wqe;
13621367

13631368
wqe = get_recv_wqe_v2(qp, wqe_idx);
13641369
fill_recv_sge_to_wqe(wr, wqe, max_sge, qp->rq.rsv_sge);
13651370

1366-
if (!qp->rq_rinl_buf.wqe_cnt)
1367-
return;
1368-
1369-
/* QP support receive inline wqe */
1370-
sge_list = qp->rq_rinl_buf.wqe_list[wqe_idx].sg_list;
1371-
qp->rq_rinl_buf.wqe_list[wqe_idx].sge_cnt = (unsigned int)wr->num_sge;
1372-
for (i = 0; i < wr->num_sge; i++) {
1373-
sge_list[i].addr = (void *)(uintptr_t)wr->sg_list[i].addr;
1374-
sge_list[i].len = wr->sg_list[i].length;
1375-
}
1371+
fill_recv_inl_buf(&qp->rq_rinl_buf, wqe_idx, wr);
13761372
}
13771373

13781374
static int hns_roce_u_v2_post_recv(struct ibv_qp *ibvqp, struct ibv_recv_wr *wr,

providers/hns/hns_roce_u_verbs.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -855,43 +855,44 @@ static int verify_qp_create_attr(struct hns_roce_context *ctx,
855855
return verify_qp_create_cap(ctx, attr);
856856
}
857857

858-
static int qp_alloc_recv_inl_buf(struct ibv_qp_cap *cap,
859-
struct hns_roce_qp *qp)
858+
static int alloc_recv_rinl_buf(uint32_t max_sge,
859+
struct hns_roce_rinl_buf *rinl_buf)
860860
{
861861
unsigned int cnt;
862862
int i;
863863

864-
cnt = qp->rq_rinl_buf.wqe_cnt;
865-
qp->rq_rinl_buf.wqe_list = calloc(cnt,
866-
sizeof(struct hns_roce_rinl_wqe));
867-
if (!qp->rq_rinl_buf.wqe_list)
864+
cnt = rinl_buf->wqe_cnt;
865+
rinl_buf->wqe_list = calloc(cnt, sizeof(struct hns_roce_rinl_wqe));
866+
if (!rinl_buf->wqe_list)
868867
return ENOMEM;
869868

870-
qp->rq_rinl_buf.wqe_list[0].sg_list = calloc(cnt * cap->max_recv_sge,
871-
sizeof(struct hns_roce_rinl_sge));
872-
if (!qp->rq_rinl_buf.wqe_list[0].sg_list)
869+
rinl_buf->wqe_list[0].sg_list = calloc(cnt * max_sge,
870+
sizeof(struct ibv_sge));
871+
if (!rinl_buf->wqe_list[0].sg_list) {
872+
free(rinl_buf->wqe_list);
873873
return ENOMEM;
874+
}
874875

875876
for (i = 0; i < cnt; i++) {
876-
int wqe_size = i * cap->max_recv_sge;
877+
int wqe_size = i * max_sge;
877878

878-
qp->rq_rinl_buf.wqe_list[i].sg_list =
879-
&(qp->rq_rinl_buf.wqe_list[0].sg_list[wqe_size]);
879+
rinl_buf->wqe_list[i].sg_list =
880+
&rinl_buf->wqe_list[0].sg_list[wqe_size];
880881
}
881882

882883
return 0;
883884
}
884885

885-
static void qp_free_recv_inl_buf(struct hns_roce_qp *qp)
886+
static void free_recv_rinl_buf(struct hns_roce_rinl_buf *rinl_buf)
886887
{
887-
if (qp->rq_rinl_buf.wqe_list) {
888-
if (qp->rq_rinl_buf.wqe_list[0].sg_list) {
889-
free(qp->rq_rinl_buf.wqe_list[0].sg_list);
890-
qp->rq_rinl_buf.wqe_list[0].sg_list = NULL;
888+
if (rinl_buf->wqe_list) {
889+
if (rinl_buf->wqe_list[0].sg_list) {
890+
free(rinl_buf->wqe_list[0].sg_list);
891+
rinl_buf->wqe_list[0].sg_list = NULL;
891892
}
892893

893-
free(qp->rq_rinl_buf.wqe_list);
894-
qp->rq_rinl_buf.wqe_list = NULL;
894+
free(rinl_buf->wqe_list);
895+
rinl_buf->wqe_list = NULL;
895896
}
896897
}
897898

@@ -930,7 +931,7 @@ static int calc_qp_buff_size(struct hns_roce_device *hr_dev,
930931

931932
static void qp_free_wqe(struct hns_roce_qp *qp)
932933
{
933-
qp_free_recv_inl_buf(qp);
934+
free_recv_rinl_buf(&qp->rq_rinl_buf);
934935
if (qp->sq.wqe_cnt)
935936
free(qp->sq.wrid);
936937

@@ -958,7 +959,7 @@ static int qp_alloc_wqe(struct ibv_qp_cap *cap, struct hns_roce_qp *qp,
958959
}
959960

960961
if (qp->rq_rinl_buf.wqe_cnt) {
961-
if (qp_alloc_recv_inl_buf(cap, qp))
962+
if (alloc_recv_rinl_buf(cap->max_recv_sge, &qp->rq_rinl_buf))
962963
goto err_alloc;
963964
}
964965

@@ -968,7 +969,7 @@ static int qp_alloc_wqe(struct ibv_qp_cap *cap, struct hns_roce_qp *qp,
968969
return 0;
969970

970971
err_alloc:
971-
qp_free_recv_inl_buf(qp);
972+
free_recv_rinl_buf(&qp->rq_rinl_buf);
972973
if (qp->rq.wrid)
973974
free(qp->rq.wrid);
974975

0 commit comments

Comments
 (0)