Skip to content

Commit 088c5f0

Browse files
Luo bindavem330
authored andcommitted
hinic: add generating mailbox random index support
add support to generate mailbox random id of VF to ensure that mailbox messages PF received are from the correct VF. Signed-off-by: Luo bin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent da79554 commit 088c5f0

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

drivers/net/ethernet/huawei/hinic/hinic_hw_dev.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#define HINIC_MGMT_STATUS_EXIST 0x6
2929
#define HINIC_MGMT_CMD_UNSUPPORTED 0xFF
3030

31+
#define HINIC_CMD_VER_FUNC_ID 2
32+
3133
struct hinic_cap {
3234
u16 max_qps;
3335
u16 num_qps;
@@ -313,6 +315,17 @@ struct hinic_msix_config {
313315
u8 rsvd1[3];
314316
};
315317

318+
struct hinic_set_random_id {
319+
u8 status;
320+
u8 version;
321+
u8 rsvd0[6];
322+
323+
u8 vf_in_pf;
324+
u8 rsvd1;
325+
u16 func_idx;
326+
u32 random_id;
327+
};
328+
316329
struct hinic_board_info {
317330
u32 board_type;
318331
u32 port_num;

drivers/net/ethernet/huawei/hinic/hinic_hw_mbox.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,111 @@ static void recv_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
486486
kfree(rcv_mbox_temp);
487487
}
488488

489+
static int set_vf_mbox_random_id(struct hinic_hwdev *hwdev, u16 func_id)
490+
{
491+
struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
492+
struct hinic_set_random_id rand_info = {0};
493+
u16 out_size = sizeof(rand_info);
494+
struct hinic_pfhwdev *pfhwdev;
495+
int ret;
496+
497+
pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
498+
499+
rand_info.version = HINIC_CMD_VER_FUNC_ID;
500+
rand_info.func_idx = func_id;
501+
rand_info.vf_in_pf = func_id - hinic_glb_pf_vf_offset(hwdev->hwif);
502+
rand_info.random_id = get_random_u32();
503+
504+
func_to_func->vf_mbx_rand_id[func_id] = rand_info.random_id;
505+
506+
ret = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
507+
HINIC_MGMT_CMD_SET_VF_RANDOM_ID,
508+
&rand_info, sizeof(rand_info),
509+
&rand_info, &out_size, HINIC_MGMT_MSG_SYNC);
510+
if ((rand_info.status != HINIC_MGMT_CMD_UNSUPPORTED &&
511+
rand_info.status) || !out_size || ret) {
512+
dev_err(&hwdev->hwif->pdev->dev, "Set VF random id failed, err: %d, status: 0x%x, out size: 0x%x\n",
513+
ret, rand_info.status, out_size);
514+
return -EIO;
515+
}
516+
517+
if (rand_info.status == HINIC_MGMT_CMD_UNSUPPORTED)
518+
return rand_info.status;
519+
520+
func_to_func->vf_mbx_old_rand_id[func_id] =
521+
func_to_func->vf_mbx_rand_id[func_id];
522+
523+
return 0;
524+
}
525+
526+
static void update_random_id_work_handler(struct work_struct *work)
527+
{
528+
struct hinic_mbox_work *mbox_work =
529+
container_of(work, struct hinic_mbox_work, work);
530+
struct hinic_mbox_func_to_func *func_to_func;
531+
u16 src = mbox_work->src_func_idx;
532+
533+
func_to_func = mbox_work->func_to_func;
534+
535+
if (set_vf_mbox_random_id(func_to_func->hwdev, src))
536+
dev_warn(&func_to_func->hwdev->hwif->pdev->dev, "Update VF id: 0x%x random id failed\n",
537+
mbox_work->src_func_idx);
538+
539+
kfree(mbox_work);
540+
}
541+
542+
static bool check_vf_mbox_random_id(struct hinic_mbox_func_to_func *func_to_func,
543+
u8 *header)
544+
{
545+
struct hinic_hwdev *hwdev = func_to_func->hwdev;
546+
struct hinic_mbox_work *mbox_work = NULL;
547+
u64 mbox_header = *((u64 *)header);
548+
u16 offset, src;
549+
u32 random_id;
550+
int vf_in_pf;
551+
552+
src = HINIC_MBOX_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
553+
554+
if (IS_PF_OR_PPF_SRC(src) || !func_to_func->support_vf_random)
555+
return true;
556+
557+
if (!HINIC_IS_PPF(hwdev->hwif)) {
558+
offset = hinic_glb_pf_vf_offset(hwdev->hwif);
559+
vf_in_pf = src - offset;
560+
561+
if (vf_in_pf < 1 || vf_in_pf > hwdev->nic_cap.max_vf) {
562+
dev_warn(&hwdev->hwif->pdev->dev,
563+
"Receive vf id(0x%x) is invalid, vf id should be from 0x%x to 0x%x\n",
564+
src, offset + 1,
565+
hwdev->nic_cap.max_vf + offset);
566+
return false;
567+
}
568+
}
569+
570+
random_id = be32_to_cpu(*(u32 *)(header + MBOX_SEG_LEN +
571+
MBOX_HEADER_SZ));
572+
573+
if (random_id == func_to_func->vf_mbx_rand_id[src] ||
574+
random_id == func_to_func->vf_mbx_old_rand_id[src])
575+
return true;
576+
577+
dev_warn(&hwdev->hwif->pdev->dev,
578+
"The mailbox random id(0x%x) of func_id(0x%x) doesn't match with pf reservation(0x%x)\n",
579+
random_id, src, func_to_func->vf_mbx_rand_id[src]);
580+
581+
mbox_work = kzalloc(sizeof(*mbox_work), GFP_KERNEL);
582+
if (!mbox_work)
583+
return false;
584+
585+
mbox_work->func_to_func = func_to_func;
586+
mbox_work->src_func_idx = src;
587+
588+
INIT_WORK(&mbox_work->work, update_random_id_work_handler);
589+
queue_work(func_to_func->workq, &mbox_work->work);
590+
591+
return false;
592+
}
593+
489594
void hinic_mbox_func_aeqe_handler(void *handle, void *header, u8 size)
490595
{
491596
struct hinic_mbox_func_to_func *func_to_func;
@@ -504,6 +609,9 @@ void hinic_mbox_func_aeqe_handler(void *handle, void *header, u8 size)
504609
return;
505610
}
506611

612+
if (!check_vf_mbox_random_id(func_to_func, header))
613+
return;
614+
507615
recv_mbox = (dir == HINIC_HWIF_DIRECT_SEND) ?
508616
&func_to_func->mbox_send[src] :
509617
&func_to_func->mbox_resp[src];
@@ -1210,3 +1318,32 @@ void hinic_func_to_func_free(struct hinic_hwdev *hwdev)
12101318

12111319
kfree(func_to_func);
12121320
}
1321+
1322+
int hinic_vf_mbox_random_id_init(struct hinic_hwdev *hwdev)
1323+
{
1324+
u16 vf_offset;
1325+
u8 vf_in_pf;
1326+
int err = 0;
1327+
1328+
if (HINIC_IS_VF(hwdev->hwif))
1329+
return 0;
1330+
1331+
vf_offset = hinic_glb_pf_vf_offset(hwdev->hwif);
1332+
1333+
for (vf_in_pf = 1; vf_in_pf <= hwdev->nic_cap.max_vf; vf_in_pf++) {
1334+
err = set_vf_mbox_random_id(hwdev, vf_offset + vf_in_pf);
1335+
if (err)
1336+
break;
1337+
}
1338+
1339+
if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
1340+
hwdev->func_to_func->support_vf_random = false;
1341+
err = 0;
1342+
dev_warn(&hwdev->hwif->pdev->dev, "Mgmt is unsupported to set VF%d random id\n",
1343+
vf_in_pf - 1);
1344+
} else if (!err) {
1345+
hwdev->func_to_func->support_vf_random = true;
1346+
}
1347+
1348+
return err;
1349+
}

drivers/net/ethernet/huawei/hinic/hinic_hw_mbox.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define HINIC_FUNC_CSR_MAILBOX_RESULT_H_OFF 0x0108
2323
#define HINIC_FUNC_CSR_MAILBOX_RESULT_L_OFF 0x010C
2424

25+
#define MAX_FUNCTION_NUM 512
26+
2527
enum hinic_mbox_ack_type {
2628
MBOX_ACK,
2729
MBOX_NO_ACK,
@@ -100,6 +102,10 @@ struct hinic_mbox_func_to_func {
100102

101103
/* lock for mbox event flag */
102104
spinlock_t mbox_lock;
105+
106+
u32 vf_mbx_old_rand_id[MAX_FUNCTION_NUM];
107+
u32 vf_mbx_rand_id[MAX_FUNCTION_NUM];
108+
bool support_vf_random;
103109
};
104110

105111
struct hinic_mbox_work {
@@ -151,4 +157,6 @@ int hinic_mbox_to_vf(struct hinic_hwdev *hwdev,
151157
enum hinic_mod_type mod, u16 vf_id, u8 cmd, void *buf_in,
152158
u16 in_size, void *buf_out, u16 *out_size, u32 timeout);
153159

160+
int hinic_vf_mbox_random_id_init(struct hinic_hwdev *hwdev);
161+
154162
#endif

drivers/net/ethernet/huawei/hinic/hinic_hw_mgmt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ enum hinic_comm_cmd {
9393

9494
HINIC_COMM_CMD_WATCHDOG_INFO = 0x56,
9595

96+
HINIC_MGMT_CMD_SET_VF_RANDOM_ID = 0x61,
97+
9698
HINIC_COMM_CMD_MAX,
9799
};
98100

drivers/net/ethernet/huawei/hinic/hinic_sriov.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,13 @@ int hinic_vf_func_init(struct hinic_hwdev *hwdev)
11081108
int err = 0;
11091109
u32 size, i;
11101110

1111+
err = hinic_vf_mbox_random_id_init(hwdev);
1112+
if (err) {
1113+
dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1114+
err);
1115+
return err;
1116+
}
1117+
11111118
nic_io = &hwdev->func_to_io;
11121119

11131120
if (HINIC_IS_VF(hwdev->hwif)) {

0 commit comments

Comments
 (0)