DPDK logo

Elixir Cross Referencer

  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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2017 Huawei Technologies Co., Ltd
 */

#include "hinic_compat.h"
#include "hinic_csr.h"
#include "hinic_pmd_hwdev.h"
#include "hinic_pmd_hwif.h"
#include "hinic_pmd_eqs.h"
#include "hinic_pmd_mgmt.h"
#include "hinic_pmd_mbox.h"

#define HINIC_MBOX_INT_DST_FUNC_SHIFT				0
#define HINIC_MBOX_INT_DST_AEQN_SHIFT				10
#define HINIC_MBOX_INT_SRC_RESP_AEQN_SHIFT			12
#define HINIC_MBOX_INT_STAT_DMA_SHIFT				14
/* The size of data to be send (unit of 4 bytes) */
#define HINIC_MBOX_INT_TX_SIZE_SHIFT				20
/* SO_RO(strong order, relax order)  */
#define HINIC_MBOX_INT_STAT_DMA_SO_RO_SHIFT			25
#define HINIC_MBOX_INT_WB_EN_SHIFT				28


#define HINIC_MBOX_INT_DST_FUNC_MASK				0x3FF
#define HINIC_MBOX_INT_DST_AEQN_MASK				0x3
#define HINIC_MBOX_INT_SRC_RESP_AEQN_MASK			0x3
#define HINIC_MBOX_INT_STAT_DMA_MASK				0x3F
#define HINIC_MBOX_INT_TX_SIZE_MASK				0x1F
#define HINIC_MBOX_INT_STAT_DMA_SO_RO_MASK			0x3
#define HINIC_MBOX_INT_WB_EN_MASK				0x1

#define HINIC_MBOX_INT_SET(val, field)	\
			(((val) & HINIC_MBOX_INT_##field##_MASK) << \
			HINIC_MBOX_INT_##field##_SHIFT)

enum hinic_mbox_tx_status {
	TX_DONE = 0,
	TX_IN_PROGRESS,
};

#define HINIC_MBOX_CTRL_TRIGGER_AEQE_SHIFT			0
/* specifies the issue request for the message data.
 * 0 - Tx request is done;
 * 1 - Tx request is in process.
 */
#define HINIC_MBOX_CTRL_TX_STATUS_SHIFT				1

#define HINIC_MBOX_CTRL_TRIGGER_AEQE_MASK			0x1
#define HINIC_MBOX_CTRL_TX_STATUS_MASK				0x1

#define HINIC_MBOX_CTRL_SET(val, field)	\
			(((val) & HINIC_MBOX_CTRL_##field##_MASK) << \
			HINIC_MBOX_CTRL_##field##_SHIFT)

#define HINIC_MBOX_HEADER_MSG_LEN_SHIFT				0
#define HINIC_MBOX_HEADER_MODULE_SHIFT				11
#define HINIC_MBOX_HEADER_SEG_LEN_SHIFT				16
#define HINIC_MBOX_HEADER_NO_ACK_SHIFT				22
#define HINIC_MBOX_HEADER_SEQID_SHIFT				24
#define HINIC_MBOX_HEADER_LAST_SHIFT				30

#define HINIC_MBOX_HEADER_DIRECTION_SHIFT			31
#define HINIC_MBOX_HEADER_CMD_SHIFT				32
#define HINIC_MBOX_HEADER_MSG_ID_SHIFT				40
#define HINIC_MBOX_HEADER_STATUS_SHIFT				48
#define HINIC_MBOX_HEADER_SRC_GLB_FUNC_IDX_SHIFT		54

#define HINIC_MBOX_HEADER_MSG_LEN_MASK				0x7FF
#define HINIC_MBOX_HEADER_MODULE_MASK				0x1F
#define HINIC_MBOX_HEADER_SEG_LEN_MASK				0x3F
#define HINIC_MBOX_HEADER_NO_ACK_MASK				0x1
#define HINIC_MBOX_HEADER_SEQID_MASK				0x3F
#define HINIC_MBOX_HEADER_LAST_MASK				0x1
#define HINIC_MBOX_HEADER_DIRECTION_MASK			0x1
#define HINIC_MBOX_HEADER_CMD_MASK				0xFF
#define HINIC_MBOX_HEADER_MSG_ID_MASK				0xFF
#define HINIC_MBOX_HEADER_STATUS_MASK				0x3F
#define HINIC_MBOX_HEADER_SRC_GLB_FUNC_IDX_MASK			0x3FF

#define HINIC_MBOX_HEADER_GET(val, field)	\
			(((val) >> HINIC_MBOX_HEADER_##field##_SHIFT) & \
			HINIC_MBOX_HEADER_##field##_MASK)
#define HINIC_MBOX_HEADER_SET(val, field)	\
			((u64)((val) & HINIC_MBOX_HEADER_##field##_MASK) << \
			HINIC_MBOX_HEADER_##field##_SHIFT)

#define HINIC_MBOX_COMP_TIME_MS			8000U
#define MBOX_MSG_POLLING_TIMEOUT_MS		5000

/* The size unit is Bytes */
#define HINIC_MBOX_DATA_SIZE			2040
#define MBOX_MAX_BUF_SZ				2048UL
#define MBOX_HEADER_SZ				8

/* MBOX size is 64B, 8B for mbox_header, 4B reserved */
#define MBOX_SEG_LEN				48
#define MBOX_SEG_LEN_ALIGN			4
#define MBOX_WB_STATUS_LEN			16UL
#define MBOX_SIZE				64

/* mbox write back status is 16B, only first 4B is used */
#define MBOX_WB_STATUS_ERRCODE_MASK		0xFFFF
#define MBOX_WB_STATUS_MASK			0xFF
#define MBOX_WB_ERROR_CODE_MASK			0xFF00
#define MBOX_WB_STATUS_FINISHED_SUCCESS		0xFF
#define MBOX_WB_STATUS_FINISHED_WITH_ERR	0xFE
#define MBOX_WB_STATUS_NOT_FINISHED		0x00

#define MBOX_STATUS_FINISHED(wb)	\
	(((wb) & MBOX_WB_STATUS_MASK) != MBOX_WB_STATUS_NOT_FINISHED)
#define MBOX_STATUS_SUCCESS(wb)		\
	(((wb) & MBOX_WB_STATUS_MASK) == MBOX_WB_STATUS_FINISHED_SUCCESS)
#define MBOX_STATUS_ERRCODE(wb)		\
	((wb) & MBOX_WB_ERROR_CODE_MASK)

#define SEQ_ID_START_VAL			0

#define DST_AEQ_IDX_DEFAULT_VAL			0
#define SRC_AEQ_IDX_DEFAULT_VAL			0
#define NO_DMA_ATTRIBUTE_VAL			0

#define MBOX_MSG_NO_DATA_LEN			1

#define FUNC_ID_OFF_SET_8B		8
#define FUNC_ID_OFF_SET_10B		10

#define MBOX_BODY_FROM_HDR(header)	((u8 *)(header) + MBOX_HEADER_SZ)
#define MBOX_AREA(hwif)			\
		((hwif)->cfg_regs_base + HINIC_FUNC_CSR_MAILBOX_DATA_OFF)

#define MBOX_RESPONSE_ERROR		0x1
#define MBOX_MSG_ID_MASK		0xFF
#define MBOX_MSG_ID(func_to_func)	((func_to_func)->send_msg_id)

enum hinic_hwif_direction_type {
	/* driver send msg to up or up send msg to driver*/
	HINIC_HWIF_DIRECT_SEND = 0,
	/* after driver/up send msg to each other, then up/driver ack the msg */
	HINIC_HWIF_RESPONSE,
};

enum mbox_send_mod {
	MBOX_SEND_MSG_POLL = 1
};

enum mbox_seg_type {
	NOT_LAST_SEG,
	LAST_SEG,
};

enum mbox_ordering_type {
	STRONG_ORDER,
	RELAX_ORDER,
};

enum mbox_write_back_type {
	NOT_WRITE_BACK = 0,
	WRITE_BACK,
};

enum mbox_aeq_trig_type {
	NOT_TRIGGER,
	TRIGGER,
};

static int send_mbox_to_func(struct hinic_mbox_func_to_func *func_to_func,
				enum hinic_mod_type mod, u16 cmd, void *msg,
				u16 msg_len, u16 dst_func,
				enum hinic_hwif_direction_type direction,
				enum hinic_mbox_ack_type ack_type,
				struct mbox_msg_info *msg_info);

static int recv_vf_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
				struct hinic_recv_mbox *recv_mbox,
				void *buf_out, u16 *out_size, void *param)
{
	int rc = 0;
	*out_size = 0;

	switch (recv_mbox->mod) {
	case HINIC_MOD_COMM:
		hinic_comm_async_event_handle(func_to_func->hwdev,
						recv_mbox->cmd, recv_mbox->mbox,
						recv_mbox->mbox_len,
						buf_out, out_size);
		break;
	case HINIC_MOD_L2NIC:
		hinic_l2nic_async_event_handle(func_to_func->hwdev, param,
						recv_mbox->cmd, recv_mbox->mbox,
						recv_mbox->mbox_len,
						buf_out, out_size);
		break;
	default:
		PMD_DRV_LOG(ERR, "No handler, mod: %d", recv_mbox->mod);
		rc = HINIC_MBOX_VF_CMD_ERROR;
		break;
	}

	return rc;
}

static void set_mbx_msg_status(struct mbox_msg_info *msg_info, int status)
{
	if (status == HINIC_DEV_BUSY_ACTIVE_FW)
		msg_info->status = HINIC_MBOX_PF_BUSY_ACTIVE_FW;
	else if (status == HINIC_MBOX_VF_CMD_ERROR)
		msg_info->status = HINIC_MBOX_VF_CMD_ERROR;
	else if (status)
		msg_info->status = HINIC_MBOX_PF_SEND_ERR;
}

static void recv_func_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
				struct hinic_recv_mbox *recv_mbox,
				u16 src_func_idx, void *param)
{
	struct hinic_hwdev *dev = func_to_func->hwdev;
	struct mbox_msg_info msg_info = { 0 };
	u16 out_size = MBOX_MAX_BUF_SZ;
	void *buf_out = recv_mbox->buf_out;
	int err = 0;

	if (HINIC_IS_VF(dev)) {
		err = recv_vf_mbox_handler(func_to_func, recv_mbox, buf_out,
						&out_size, param);
	} else {
		err = -EINVAL;
		PMD_DRV_LOG(ERR, "PMD doesn't support non-VF handle mailbox message");
	}

	if (!out_size || err)
		out_size = MBOX_MSG_NO_DATA_LEN;

	if (recv_mbox->ack_type == MBOX_ACK) {
		msg_info.msg_id = recv_mbox->msg_info.msg_id;
		set_mbx_msg_status(&msg_info, err);
		send_mbox_to_func(func_to_func, recv_mbox->mod, recv_mbox->cmd,
				buf_out, out_size, src_func_idx,
				HINIC_HWIF_RESPONSE, MBOX_ACK, &msg_info);
	}
}

static bool check_mbox_seq_id_and_seg_len(struct hinic_recv_mbox *recv_mbox,
					  u8 seq_id, u8 seg_len, u8 msg_id)
{
	if (seq_id > HINIC_SEQ_ID_MAX_VAL || seg_len > HINIC_MSG_SEG_LEN)
		return false;

	if (seq_id == 0) {
		recv_mbox->seq_id = seq_id;
		recv_mbox->msg_info.msg_id = msg_id;
	} else {
		if ((seq_id != recv_mbox->seq_id + 1) ||
			msg_id != recv_mbox->msg_info.msg_id) {
			recv_mbox->seq_id = 0;
			return false;
		}

		recv_mbox->seq_id = seq_id;
	}

	return true;
}

static void clear_mbox_status(struct hinic_send_mbox *mbox)
{
	/* clear mailbox write back status */
	*mbox->wb_status = 0;
	rte_wmb();
}

static void mbox_copy_header(struct hinic_send_mbox *mbox, u64 *header)
{
	u32 *data = (u32 *)header;
	u32 i, idx_max = MBOX_HEADER_SZ / sizeof(u32);

	for (i = 0; i < idx_max; i++)
		__raw_writel(*(data + i), mbox->data + i * sizeof(u32));
}

static void
mbox_copy_send_data(struct hinic_send_mbox *mbox, void *seg, u16 seg_len)
{
	u32 *data = (u32 *)seg;
	u32 data_len, chk_sz = sizeof(u32);
	u32 i, idx_max;
	u8 mbox_max_buf[MBOX_SEG_LEN] = {0};

	/* The mbox message should be aligned in 4 bytes. */
	if (seg_len % chk_sz) {
		memcpy(mbox_max_buf, seg, seg_len);
		data = (u32 *)mbox_max_buf;
	}

	data_len = seg_len;
	idx_max = ALIGN(data_len, chk_sz) / chk_sz;

	for (i = 0; i < idx_max; i++)
		__raw_writel(*(data + i),
				mbox->data + MBOX_HEADER_SZ + i * sizeof(u32));
}

static int mbox_msg_ack_aeqn(struct hinic_hwdev *hwdev)
{
	u16 aeq_num = HINIC_HWIF_NUM_AEQS(hwdev->hwif);
	int msg_ack_aeqn;

	if (aeq_num >= HINIC_MAX_AEQS - 1) {
		msg_ack_aeqn = HINIC_AEQN_2;
	} else if (aeq_num == HINIC_MIN_AEQS) {
		/* This is used for ovs */
		msg_ack_aeqn = HINIC_AEQN_1;
	} else {
		PMD_DRV_LOG(ERR, "Warning: Invalid aeq num: %d\n", aeq_num);
		msg_ack_aeqn = -1;
	}

	return msg_ack_aeqn;
}

static u16 mbox_msg_dst_aeqn(struct hinic_hwdev *hwdev,
			enum hinic_hwif_direction_type seq_dir)
{
	u16 dst_aeqn;

	if (seq_dir == HINIC_HWIF_DIRECT_SEND)
		dst_aeqn = HINIC_AEQN_0;
	else
		dst_aeqn = mbox_msg_ack_aeqn(hwdev);

	return dst_aeqn;
}

static int mbox_seg_ack_aeqn(struct hinic_hwdev *hwdev)
{
	return mbox_msg_ack_aeqn(hwdev);
}

static void write_mbox_msg_attr(struct hinic_mbox_func_to_func *func_to_func,
			u16 dst_func, u16 dst_aeqn, u16 seg_ack_aeqn,
			__rte_unused u16 seg_len, int poll)
{
	u32 mbox_int, mbox_ctrl;

	mbox_int = HINIC_MBOX_INT_SET(dst_func, DST_FUNC) |
		HINIC_MBOX_INT_SET(dst_aeqn, DST_AEQN) |
		/* N/A in polling mode */
		HINIC_MBOX_INT_SET(seg_ack_aeqn, SRC_RESP_AEQN) |
		HINIC_MBOX_INT_SET(NO_DMA_ATTRIBUTE_VAL, STAT_DMA) |
		HINIC_MBOX_INT_SET(ALIGN(MBOX_SIZE, MBOX_SEG_LEN_ALIGN) >> 2,
					TX_SIZE) |
		HINIC_MBOX_INT_SET(STRONG_ORDER, STAT_DMA_SO_RO) |
		HINIC_MBOX_INT_SET(WRITE_BACK, WB_EN);

	hinic_hwif_write_reg(func_to_func->hwdev->hwif,
			HINIC_FUNC_CSR_MAILBOX_INT_OFFSET_OFF, mbox_int);

	rte_wmb();
	mbox_ctrl = HINIC_MBOX_CTRL_SET(TX_IN_PROGRESS, TX_STATUS);

	if (poll)
		mbox_ctrl |= HINIC_MBOX_CTRL_SET(NOT_TRIGGER, TRIGGER_AEQE);
	else
		mbox_ctrl |= HINIC_MBOX_CTRL_SET(TRIGGER, TRIGGER_AEQE);

	hinic_hwif_write_reg(func_to_func->hwdev->hwif,
				HINIC_FUNC_CSR_MAILBOX_CONTROL_OFF, mbox_ctrl);
}

static int init_mbox_info(struct hinic_recv_mbox *mbox_info)
{
	int err;

	mbox_info->mbox = kzalloc(MBOX_MAX_BUF_SZ, GFP_KERNEL);
	if (!mbox_info->mbox) {
		PMD_DRV_LOG(ERR, "Alloc mbox buf_in mem failed\n");
		return -ENOMEM;
	}

	mbox_info->buf_out = kzalloc(MBOX_MAX_BUF_SZ, GFP_KERNEL);
	if (!mbox_info->buf_out) {
		PMD_DRV_LOG(ERR, "Alloc mbox buf_out mem failed\n");
		err = -ENOMEM;
		goto alloc_buf_out_err;
	}

	return 0;

alloc_buf_out_err:
	kfree(mbox_info->mbox);

	return err;
}

static void clean_mbox_info(struct hinic_recv_mbox *mbox_info)
{
	kfree(mbox_info->buf_out);
	kfree(mbox_info->mbox);
}

static int alloc_mbox_info(struct hinic_recv_mbox *mbox_info)
{
	u16 func_idx, i;
	int err;

	for (func_idx = 0; func_idx < HINIC_MAX_FUNCTIONS; func_idx++) {
		err = init_mbox_info(&mbox_info[func_idx]);
		if (err) {
			PMD_DRV_LOG(ERR, "Initialize function[%d] mailbox information failed, err: %d",
				    func_idx, err);
			goto init_mbox_info_err;
		}
	}

	return 0;

init_mbox_info_err:
	for (i = 0; i < func_idx; i++)
		clean_mbox_info(&mbox_info[i]);

	return err;
}

static void free_mbox_info(struct hinic_recv_mbox *mbox_info)
{
	u16 func_idx;

	for (func_idx = 0; func_idx < HINIC_MAX_FUNCTIONS; func_idx++)
		clean_mbox_info(&mbox_info[func_idx]);
}

static void prepare_send_mbox(struct hinic_mbox_func_to_func *func_to_func)
{
	struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;

	send_mbox->data = MBOX_AREA(func_to_func->hwdev->hwif);
}

static int alloc_mbox_wb_status(struct hinic_mbox_func_to_func *func_to_func)
{
	struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
	struct hinic_hwdev *hwdev = func_to_func->hwdev;
	struct hinic_hwif *hwif = hwdev->hwif;
	u32 addr_h, addr_l;

	send_mbox->wb_vaddr = dma_zalloc_coherent(hwdev, MBOX_WB_STATUS_LEN,
					&send_mbox->wb_paddr, SOCKET_ID_ANY);
	if (!send_mbox->wb_vaddr) {
		PMD_DRV_LOG(ERR, "Allocating memory for mailbox wb status failed");
		return -ENOMEM;
	}
	send_mbox->wb_status = (volatile u64 *)send_mbox->wb_vaddr;

	addr_h = upper_32_bits(send_mbox->wb_paddr);
	addr_l = lower_32_bits(send_mbox->wb_paddr);
	hinic_hwif_write_reg(hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_H_OFF, addr_h);
	hinic_hwif_write_reg(hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_L_OFF, addr_l);

	return 0;
}

static void free_mbox_wb_status(struct hinic_mbox_func_to_func *func_to_func)
{
	struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
	struct hinic_hwdev *hwdev = func_to_func->hwdev;
	struct hinic_hwif *hwif = hwdev->hwif;

	hinic_hwif_write_reg(hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_H_OFF, 0);
	hinic_hwif_write_reg(hwif, HINIC_FUNC_CSR_MAILBOX_RESULT_L_OFF, 0);

	dma_free_coherent(hwdev, MBOX_WB_STATUS_LEN,
				send_mbox->wb_vaddr, send_mbox->wb_paddr);
}

static int recv_mbox_handler(struct hinic_mbox_func_to_func *func_to_func,
		void *header, struct hinic_recv_mbox *recv_mbox, void *param)
{
	u64 mbox_header = *((u64 *)header);
	void *mbox_body = MBOX_BODY_FROM_HDR(header);
	u16 src_func_idx;
	enum hinic_hwif_direction_type direction;
	u8 seq_id, seg_len;
	u8 msg_id;
	u8 front_id;

	seq_id = HINIC_MBOX_HEADER_GET(mbox_header, SEQID);
	seg_len = HINIC_MBOX_HEADER_GET(mbox_header, SEG_LEN);
	direction = HINIC_MBOX_HEADER_GET(mbox_header, DIRECTION);
	src_func_idx = HINIC_MBOX_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
	msg_id = HINIC_MBOX_HEADER_GET(mbox_header, MSG_ID);
	front_id = recv_mbox->seq_id;

	if (!check_mbox_seq_id_and_seg_len(recv_mbox, seq_id, seg_len,
		msg_id)) {
		PMD_DRV_LOG(ERR,
			"Mailbox sequence and segment check failed, src func id: 0x%x, "
			"front id: 0x%x, current id: 0x%x, seg len: 0x%x "
			"front msg_id: %d, cur msg_id: %d",
			src_func_idx, front_id, seq_id, seg_len,
			recv_mbox->msg_info.msg_id, msg_id);
		return HINIC_ERROR;
	}

	memcpy((u8 *)recv_mbox->mbox + seq_id * HINIC_MSG_SEG_LEN,
		mbox_body, seg_len);

	if (!HINIC_MBOX_HEADER_GET(mbox_header, LAST))
		return HINIC_ERROR;

	recv_mbox->seq_id = 0;
	recv_mbox->cmd = HINIC_MBOX_HEADER_GET(mbox_header, CMD);
	recv_mbox->mod = HINIC_MBOX_HEADER_GET(mbox_header, MODULE);
	recv_mbox->mbox_len = HINIC_MBOX_HEADER_GET(mbox_header, MSG_LEN);
	recv_mbox->ack_type = HINIC_MBOX_HEADER_GET(mbox_header, NO_ACK);
	recv_mbox->msg_info.msg_id = HINIC_MBOX_HEADER_GET(mbox_header, MSG_ID);
	recv_mbox->msg_info.status = HINIC_MBOX_HEADER_GET(mbox_header, STATUS);

	if (direction == HINIC_HWIF_RESPONSE) {
		if (recv_mbox->msg_info.msg_id == func_to_func->send_msg_id &&
			func_to_func->event_flag == EVENT_START) {
			return HINIC_OK;
		}

		PMD_DRV_LOG(ERR, "Mbox response timeout, current send msg id(0x%x), recv msg id(0x%x), status(0x%x)",
			func_to_func->send_msg_id, recv_mbox->msg_info.msg_id,
			recv_mbox->msg_info.status);
		return HINIC_ERROR;
	}

	recv_func_mbox_handler(func_to_func, recv_mbox, src_func_idx, param);

	return HINIC_ERROR;
}

/**
 * hinic_mbox_func_aeqe_handler - Process mbox info from func which is
 * sent by aeqe.
 *
 * @param handle
 *   Pointer to hradware nic device.
 * @param header
 *   Mbox header info.
 * @param size
 *   The size of aeqe descriptor.
 * @param param
 *   customized parameter.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
int hinic_mbox_func_aeqe_handler(void *handle, u8 *header,
					__rte_unused u8 size, void *param)
{
	struct hinic_mbox_func_to_func *func_to_func =
				((struct hinic_hwdev *)handle)->func_to_func;
	struct hinic_recv_mbox *recv_mbox;
	u64 mbox_header = *((u64 *)header);
	u16 src = HINIC_MBOX_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);

	if (src >= HINIC_MAX_FUNCTIONS) {
		PMD_DRV_LOG(ERR, "Mailbox source function id: %d is invalid",
				src);
		return HINIC_ERROR;
	}

	recv_mbox = (HINIC_MBOX_HEADER_GET(mbox_header, DIRECTION) ==
			HINIC_HWIF_DIRECT_SEND) ?
			&func_to_func->mbox_send[src] :
			&func_to_func->mbox_resp[src];

	return recv_mbox_handler(func_to_func, (u64 *)header, recv_mbox, param);
}

static u16 get_mbox_status(struct hinic_send_mbox *mbox)
{
	/* write back is 16B, but only use first 4B */
	u64 wb_val = be64_to_cpu(*mbox->wb_status);

	rte_rmb(); /* verify reading before check */

	return (u16)(wb_val & MBOX_WB_STATUS_ERRCODE_MASK);
}

static void dump_mox_reg(struct hinic_hwdev *hwdev)
{
	u32 val;

	val = hinic_hwif_read_reg(hwdev->hwif,
					HINIC_FUNC_CSR_MAILBOX_CONTROL_OFF);
	PMD_DRV_LOG(WARNING, "Mailbox control reg: 0x%x", val);
	val = hinic_hwif_read_reg(hwdev->hwif,
					HINIC_FUNC_CSR_MAILBOX_INT_OFFSET_OFF);
	PMD_DRV_LOG(WARNING, "Mailbox interrupt offset: 0x%x", val);
}

static int send_mbox_seg(struct hinic_mbox_func_to_func *func_to_func,
			 u64 header, u16 dst_func, void *seg, u16 seg_len)
{
	struct hinic_send_mbox *send_mbox = &func_to_func->send_mbox;
	struct hinic_hwdev *hwdev = func_to_func->hwdev;
	u16 seq_dir = HINIC_MBOX_HEADER_GET(header, DIRECTION);
	u16 dst_aeqn, seg_ack_aeqn;
	u16 err_code, wb_status = 0;
	u32 cnt = 0;

	dst_aeqn = mbox_msg_dst_aeqn(hwdev, seq_dir);
	seg_ack_aeqn = mbox_seg_ack_aeqn(hwdev);

	clear_mbox_status(send_mbox);

	mbox_copy_header(send_mbox, &header);

	mbox_copy_send_data(send_mbox, seg, seg_len);

	write_mbox_msg_attr(func_to_func, dst_func, dst_aeqn, seg_ack_aeqn,
				seg_len, MBOX_SEND_MSG_POLL);

	rte_wmb();

	while (cnt < MBOX_MSG_POLLING_TIMEOUT_MS) {
		wb_status = get_mbox_status(send_mbox);
		if (MBOX_STATUS_FINISHED(wb_status))
			break;

		rte_delay_ms(1); /* loop every ms */
		cnt++;
	}

	if (cnt == MBOX_MSG_POLLING_TIMEOUT_MS) {
		PMD_DRV_LOG(ERR, "Send mailbox segment timeout, wb status: 0x%x",
				wb_status);
		dump_mox_reg(hwdev);
		return -ETIMEDOUT;
	}

	if (!MBOX_STATUS_SUCCESS(wb_status)) {
		PMD_DRV_LOG(ERR, "Send mailbox segment to function %d error, wb status: 0x%x",
				dst_func, wb_status);
		/*
		 * err_code: 0 responses no errors, other values can
		 * refer to FS doc.
		 */
		err_code = MBOX_STATUS_ERRCODE(wb_status);
		return err_code ? err_code : -EFAULT;
	}

	return 0;
}

static void set_mbox_to_func_event(struct hinic_mbox_func_to_func *func_to_func,
				   enum mbox_event_state event_flag)
{
	spin_lock(&func_to_func->mbox_lock);
	func_to_func->event_flag = event_flag;
	spin_unlock(&func_to_func->mbox_lock);
}

static int send_mbox_to_func(struct hinic_mbox_func_to_func *func_to_func,
				enum hinic_mod_type mod, u16 cmd, void *msg,
				u16 msg_len, u16 dst_func,
				enum hinic_hwif_direction_type direction,
				enum hinic_mbox_ack_type ack_type,
				struct mbox_msg_info *msg_info)
{
	struct hinic_hwdev *hwdev = func_to_func->hwdev;
	int err = 0;
	u32 seq_id = 0;
	u16 seg_len = HINIC_MSG_SEG_LEN;
	u16 left = msg_len;
	u8 *msg_seg = (u8 *)msg;
	u64 header = 0;

	err = hinic_mutex_lock(&func_to_func->msg_send_mutex);
	if (err)
		return err;

	header = HINIC_MBOX_HEADER_SET(msg_len, MSG_LEN) |
		HINIC_MBOX_HEADER_SET(mod, MODULE) |
		HINIC_MBOX_HEADER_SET(seg_len, SEG_LEN) |
		HINIC_MBOX_HEADER_SET(ack_type, NO_ACK) |
		HINIC_MBOX_HEADER_SET(SEQ_ID_START_VAL, SEQID) |
		HINIC_MBOX_HEADER_SET(NOT_LAST_SEG, LAST) |
		HINIC_MBOX_HEADER_SET(direction, DIRECTION) |
		HINIC_MBOX_HEADER_SET(cmd, CMD) |
		HINIC_MBOX_HEADER_SET(msg_info->msg_id, MSG_ID) |
		HINIC_MBOX_HEADER_SET(msg_info->status, STATUS) |
		HINIC_MBOX_HEADER_SET(hinic_global_func_id(hwdev),
					SRC_GLB_FUNC_IDX);

	while (!(HINIC_MBOX_HEADER_GET(header, LAST))) {
		if (left <= HINIC_MSG_SEG_LEN) {
			header &=
			~(HINIC_MBOX_HEADER_SET(HINIC_MBOX_HEADER_SEG_LEN_MASK,
						SEG_LEN));
			header |= HINIC_MBOX_HEADER_SET(left, SEG_LEN);
			header |= HINIC_MBOX_HEADER_SET(LAST_SEG, LAST);

			seg_len = left;
		}

		err = send_mbox_seg(func_to_func, header, dst_func, msg_seg,
				    seg_len);
		if (err) {
			PMD_DRV_LOG(ERR, "Fail to send mbox seg, err: %d", err);
			goto send_err;
		}

		left -= HINIC_MSG_SEG_LEN;
		msg_seg += HINIC_MSG_SEG_LEN;

		seq_id++;
		header &= ~(HINIC_MBOX_HEADER_SET(HINIC_MBOX_HEADER_SEQID_MASK,
							SEQID));
		header |= HINIC_MBOX_HEADER_SET(seq_id, SEQID);
	}

send_err:
	(void)hinic_mutex_unlock(&func_to_func->msg_send_mutex);

	return err;
}

static int hinic_mbox_to_func(struct hinic_mbox_func_to_func *func_to_func,
			enum hinic_mod_type mod, u16 cmd, u16 dst_func,
			void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
			u32 timeout)
{
	struct hinic_recv_mbox *mbox_for_resp =
					&func_to_func->mbox_resp[dst_func];
	struct mbox_msg_info msg_info = {0};
	u32 time;
	int err;

	err = hinic_mutex_lock(&func_to_func->mbox_send_mutex);
	if (err)
		return err;

	msg_info.msg_id = (MBOX_MSG_ID(func_to_func) + 1) & MBOX_MSG_ID_MASK;
	MBOX_MSG_ID(func_to_func) = msg_info.msg_id;

	set_mbox_to_func_event(func_to_func, EVENT_START);

	err = send_mbox_to_func(func_to_func, mod, cmd, buf_in, in_size,
				dst_func, HINIC_HWIF_DIRECT_SEND,
				MBOX_ACK, &msg_info);
	if (err)
		goto send_err;

	time = msecs_to_jiffies(timeout ? timeout : HINIC_MBOX_COMP_TIME_MS);
	err = hinic_aeq_poll_msg(func_to_func->ack_aeq, time, NULL);
	if (err) {
		set_mbox_to_func_event(func_to_func, EVENT_TIMEOUT);
		PMD_DRV_LOG(ERR, "Send mailbox message time out");
		err = -ETIMEDOUT;
		goto send_err;
	}

	set_mbox_to_func_event(func_to_func, EVENT_END);

	if (mbox_for_resp->msg_info.status) {
		err = mbox_for_resp->msg_info.status;
		if (err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
			PMD_DRV_LOG(ERR, "Mailbox response error: 0x%x",
					mbox_for_resp->msg_info.status);
		else
			PMD_DRV_LOG(ERR, "Chip is in active, PF can't process VF message");
		goto send_err;
	}

	rte_rmb();

	if (mbox_for_resp->mbox_len && buf_out && out_size) {
		if (mbox_for_resp->mbox_len <= *out_size) {
			memcpy(buf_out, mbox_for_resp->mbox,
				mbox_for_resp->mbox_len);
			*out_size = mbox_for_resp->mbox_len;
		} else {
			PMD_DRV_LOG(ERR, "Mailbox response message len[%u] overflow",
					mbox_for_resp->mbox_len);
			err = -ERANGE;
		}
	}

send_err:
	if (err && out_size)
		*out_size = 0;
	(void)hinic_mutex_unlock(&func_to_func->mbox_send_mutex);

	return err;
}

static int
mbox_func_params_valid(__rte_unused struct hinic_mbox_func_to_func *mbox_obj,
			void *buf_in, u16 in_size)
{
	if (!buf_in || !in_size)
		return -EINVAL;

	if (in_size > HINIC_MBOX_DATA_SIZE) {
		PMD_DRV_LOG(ERR, "Mailbox message len(%d) exceed limit(%d)",
				in_size, HINIC_MBOX_DATA_SIZE);
		return -EINVAL;
	}

	return 0;
}

static u8 hinic_pf_id_of_vf(void *hwdev)
{
	struct hinic_hwif *hwif = ((struct hinic_hwdev *)hwdev)->hwif;
	return hwif->attr.port_to_port_idx;
}

/**
 * hinic_mbox_to_pf - Send mbox info to pf and need pf to response.
 *
 * @param hwdev
 *   Pointer to hardware nic device.
 * @param mod
 *   Mode type of hardware.
 * @param cmd
 *   The command sent to pf.
 * @param buf_in
 *   Input parameter.
 * @param in_size
 *   Input parameter size.
 * @param buf_out
 *   Output parameter.
 * @param out_size
 *   Output parameter size.
 * @param timeout
 *   Timeout.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
int hinic_mbox_to_pf(struct hinic_hwdev *hwdev,
		      enum hinic_mod_type mod, u8 cmd, void *buf_in,
		      u16 in_size, void *buf_out, u16 *out_size, u32 timeout)
{
	struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;
	int err;

	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
	if (err) {
		PMD_DRV_LOG(ERR, "Mailbox parameters check failed: %d", err);
		return err;
	}

	if (!HINIC_IS_VF(hwdev)) {
		PMD_DRV_LOG(ERR, "Input function type error, func_type: %d",
				hinic_func_type(hwdev));
		return -EINVAL;
	}

	return hinic_mbox_to_func(func_to_func, mod, cmd,
				   hinic_pf_id_of_vf(hwdev), buf_in, in_size,
				   buf_out, out_size, timeout);
}

/**
 * hinic_mbox_to_pf_no_ack - Send mbox info to pf and do not need pf to response
 *
 * @param hwdev
 *   Pointer to hardware nic device.
 * @param mod
 *   Mode type of hardware.
 * @param cmd
 *   The command sent to pf.
 * @param buf_in
 *   Input parameter.
 * @param in_size
 *   Input parameter size.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
int hinic_mbox_to_pf_no_ack(struct hinic_hwdev *hwdev, enum hinic_mod_type mod,
			u8 cmd, void *buf_in, u16 in_size)
{
	int err;
	struct mbox_msg_info msg_info = {0};

	err = hinic_mutex_lock(&hwdev->func_to_func->mbox_send_mutex);
	if (err)
		return err;

	err = send_mbox_to_func(hwdev->func_to_func, mod, cmd, buf_in, in_size,
			hinic_pf_id_of_vf(hwdev), HINIC_HWIF_DIRECT_SEND,
			MBOX_NO_ACK, &msg_info);
	if (err)
		PMD_DRV_LOG(ERR, "Send mailbox no ack failed, err: %d", err);

	(void)hinic_mutex_unlock(&hwdev->func_to_func->mbox_send_mutex);

	return err;
}

static int hinic_func_to_func_init(struct hinic_hwdev *hwdev)
{
	struct hinic_mbox_func_to_func *func_to_func;
	int err;

	func_to_func = kzalloc(sizeof(*func_to_func), GFP_KERNEL);
	if (!func_to_func) {
		PMD_DRV_LOG(ERR, "Allocating memory for func_to_func object failed");
		return -ENOMEM;
	}
	hwdev->func_to_func = func_to_func;
	func_to_func->hwdev = hwdev;
	(void)hinic_mutex_init(&func_to_func->mbox_send_mutex, NULL);
	(void)hinic_mutex_init(&func_to_func->msg_send_mutex, NULL);

	err = alloc_mbox_info(func_to_func->mbox_send);
	if (err) {
		PMD_DRV_LOG(ERR, "Allocating memory for mailbox sending failed");
		goto alloc_mbox_for_send_err;
	}

	err = alloc_mbox_info(func_to_func->mbox_resp);
	if (err) {
		PMD_DRV_LOG(ERR, "Allocating memory for mailbox responding failed");
		goto alloc_mbox_for_resp_err;
	}

	err = alloc_mbox_wb_status(func_to_func);
	if (err)
		goto alloc_wb_status_err;

	prepare_send_mbox(func_to_func);

	return 0;

alloc_wb_status_err:
	free_mbox_info(func_to_func->mbox_resp);

alloc_mbox_for_resp_err:
	free_mbox_info(func_to_func->mbox_send);

alloc_mbox_for_send_err:
	kfree(func_to_func);

	return err;
}

/**
 * hinic_comm_func_to_func_free - Uninitialize func to func resource.
 *
 * @param hwdev
 *   Pointer to hardware nic device.
 */
void hinic_comm_func_to_func_free(struct hinic_hwdev *hwdev)
{
	struct hinic_mbox_func_to_func *func_to_func = hwdev->func_to_func;

	free_mbox_wb_status(func_to_func);
	free_mbox_info(func_to_func->mbox_resp);
	free_mbox_info(func_to_func->mbox_send);
	(void)hinic_mutex_destroy(&func_to_func->mbox_send_mutex);
	(void)hinic_mutex_destroy(&func_to_func->msg_send_mutex);
	kfree(func_to_func);
}

/**
 * hinic_comm_func_to_func_init - Initialize func to func resource.
 *
 * @param hwdev
 *   Pointer to hardware nic device.
 */
int hinic_comm_func_to_func_init(struct hinic_hwdev *hwdev)
{
	int rc;
	u16 msg_ack_aeqn;

	rc = hinic_func_to_func_init(hwdev);
	if (rc)
		return rc;

	msg_ack_aeqn = mbox_msg_ack_aeqn(hwdev);

	hwdev->func_to_func->ack_aeq = &hwdev->aeqs->aeq[msg_ack_aeqn];
	hwdev->func_to_func->recv_aeq = &hwdev->aeqs->aeq[HINIC_AEQN_0];

	return 0;
}