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
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2019-2021 Broadcom
 * All rights reserved.
 */

#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "tf_msg_common.h"
#include "tf_device.h"
#include "tf_msg.h"
#include "tf_util.h"
#include "tf_common.h"
#include "tf_session.h"
#include "tfp.h"
#include "hwrm_tf.h"
#include "tf_em.h"

/* Logging defines */
#define TF_RM_MSG_DEBUG  0

/* Specific msg size defines as we cannot use defines in tf.yaml. This
 * means we have to manually sync hwrm with these defines if the
 * tf.yaml changes.
 */
#define TF_MSG_SET_GLOBAL_CFG_DATA_SIZE  16
#define TF_MSG_EM_INSERT_KEY_SIZE        64
#define TF_MSG_TBL_TYPE_SET_DATA_SIZE    88

/* Compile check - Catch any msg changes that we depend on, like the
 * defines listed above for array size checking.
 *
 * Checking array size is dangerous in that the type could change and
 * we wouldn't be able to catch it. Thus we check if the complete msg
 * changed instead. Best we can do.
 *
 * If failure is observed then both msg size (defines below) and the
 * array size (define above) should be checked and compared.
 */
#define TF_MSG_SIZE_HWRM_TF_GLOBAL_CFG_SET 56
static_assert(sizeof(struct hwrm_tf_global_cfg_set_input) ==
	      TF_MSG_SIZE_HWRM_TF_GLOBAL_CFG_SET,
	      "HWRM message size changed: hwrm_tf_global_cfg_set_input");

#define TF_MSG_SIZE_HWRM_TF_EM_INSERT      104
static_assert(sizeof(struct hwrm_tf_em_insert_input) ==
	      TF_MSG_SIZE_HWRM_TF_EM_INSERT,
	      "HWRM message size changed: hwrm_tf_em_insert_input");

#define TF_MSG_SIZE_HWRM_TF_TBL_TYPE_SET   128
static_assert(sizeof(struct hwrm_tf_tbl_type_set_input) ==
	      TF_MSG_SIZE_HWRM_TF_TBL_TYPE_SET,
	      "HWRM message size changed: hwrm_tf_tbl_type_set_input");

/**
 * This is the MAX data we can transport across regular HWRM
 */
#define TF_PCI_BUF_SIZE_MAX 88

/**
 * If data bigger than TF_PCI_BUF_SIZE_MAX then use DMA method
 */
struct tf_msg_dma_buf {
	void *va_addr;
	uint64_t pa_addr;
};

/**
 * Allocates a DMA buffer that can be used for message transfer.
 *
 * [in] buf
 *   Pointer to DMA buffer structure
 *
 * [in] size
 *   Requested size of the buffer in bytes
 *
 * Returns:
 *    0      - Success
 *   -ENOMEM - Unable to allocate buffer, no memory
 */
static int
tf_msg_alloc_dma_buf(struct tf_msg_dma_buf *buf, int size)
{
	struct tfp_calloc_parms alloc_parms;
	int rc;

	/* Allocate session */
	alloc_parms.nitems = 1;
	alloc_parms.size = size;
	alloc_parms.alignment = 4096;
	rc = tfp_calloc(&alloc_parms);
	if (rc)
		return -ENOMEM;

	buf->pa_addr = (uintptr_t)alloc_parms.mem_pa;
	buf->va_addr = alloc_parms.mem_va;

	return 0;
}

/**
 * Free's a previous allocated DMA buffer.
 *
 * [in] buf
 *   Pointer to DMA buffer structure
 */
static void
tf_msg_free_dma_buf(struct tf_msg_dma_buf *buf)
{
	tfp_free(buf->va_addr);
}

/* HWRM Direct messages */

int
tf_msg_session_open(struct tf *tfp,
		    char *ctrl_chan_name,
		    uint8_t *fw_session_id,
		    uint8_t *fw_session_client_id)
{
	int rc;
	struct hwrm_tf_session_open_input req = { 0 };
	struct hwrm_tf_session_open_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };

	/* Populate the request */
	tfp_memcpy(&req.session_name, ctrl_chan_name, TF_SESSION_NAME_MAX);

	parms.tf_type = HWRM_TF_SESSION_OPEN;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	*fw_session_id = (uint8_t)tfp_le_to_cpu_32(resp.fw_session_id);
	*fw_session_client_id =
		(uint8_t)tfp_le_to_cpu_32(resp.fw_session_client_id);

	return rc;
}

int
tf_msg_session_attach(struct tf *tfp __rte_unused,
		      char *ctrl_chan_name __rte_unused,
		      uint8_t tf_fw_session_id __rte_unused)
{
	return -1;
}

int
tf_msg_session_client_register(struct tf *tfp,
			       char *ctrl_channel_name,
			       uint8_t *fw_session_client_id)
{
	int rc;
	struct hwrm_tf_session_register_input req = { 0 };
	struct hwrm_tf_session_register_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "Unable to lookup FW id, rc:%s\n",
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	tfp_memcpy(&req.session_client_name,
		   ctrl_channel_name,
		   TF_SESSION_NAME_MAX);

	parms.tf_type = HWRM_TF_SESSION_REGISTER;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	*fw_session_client_id =
		(uint8_t)tfp_le_to_cpu_32(resp.fw_session_client_id);

	return rc;
}

int
tf_msg_session_client_unregister(struct tf *tfp,
				 uint8_t fw_session_client_id)
{
	int rc;
	struct hwrm_tf_session_unregister_input req = { 0 };
	struct hwrm_tf_session_unregister_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "Unable to lookup FW id, rc:%s\n",
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.fw_session_client_id = tfp_cpu_to_le_32(fw_session_client_id);

	parms.tf_type = HWRM_TF_SESSION_UNREGISTER;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);

	return rc;
}

int
tf_msg_session_close(struct tf *tfp)
{
	int rc;
	struct hwrm_tf_session_close_input req = { 0 };
	struct hwrm_tf_session_close_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "Unable to lookup FW id, rc:%s\n",
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);

	parms.tf_type = HWRM_TF_SESSION_CLOSE;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	return rc;
}

int
tf_msg_session_qcfg(struct tf *tfp)
{
	int rc;
	struct hwrm_tf_session_qcfg_input req = { 0 };
	struct hwrm_tf_session_qcfg_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "Unable to lookup FW id, rc:%s\n",
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);

	parms.tf_type = HWRM_TF_SESSION_QCFG,
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	return rc;
}

int
tf_msg_session_resc_qcaps(struct tf *tfp,
			  enum tf_dir dir,
			  uint16_t size,
			  struct tf_rm_resc_req_entry *query,
			  enum tf_rm_resc_resv_strategy *resv_strategy)
{
	int rc;
	int i;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_session_resc_qcaps_input req = { 0 };
	struct hwrm_tf_session_resc_qcaps_output resp = { 0 };
	uint8_t fw_session_id;
	struct tf_msg_dma_buf qcaps_buf = { 0 };
	struct tf_rm_resc_req_entry *data;
	int dma_size;

	TF_CHECK_PARMS3(tfp, query, resv_strategy);

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	/* Prepare DMA buffer */
	dma_size = size * sizeof(struct tf_rm_resc_req_entry);
	rc = tf_msg_alloc_dma_buf(&qcaps_buf, dma_size);
	if (rc)
		return rc;

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_16(dir);
	req.qcaps_size = size;
	req.qcaps_addr = tfp_cpu_to_le_64(qcaps_buf.pa_addr);

	parms.tf_type = HWRM_TF_SESSION_RESC_QCAPS;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);
	if (rc)
		goto cleanup;

	/* Process the response
	 * Should always get expected number of entries
	 */
	if (tfp_le_to_cpu_32(resp.size) != size) {
		TFP_DRV_LOG(ERR,
			    "%s: QCAPS message size error, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(EINVAL));
		rc = -EINVAL;
		goto cleanup;
	}

#if (TF_RM_MSG_DEBUG == 1)
	printf("size: %d\n", tfp_le_to_cpu_32(resp.size));
#endif /* (TF_RM_MSG_DEBUG == 1) */

	/* Post process the response */
	data = (struct tf_rm_resc_req_entry *)qcaps_buf.va_addr;

#if (TF_RM_MSG_DEBUG == 1)
	printf("\nQCAPS\n");
#endif /* (TF_RM_MSG_DEBUG == 1) */
	for (i = 0; i < size; i++) {
		query[i].type = tfp_le_to_cpu_32(data[i].type);
		query[i].min = tfp_le_to_cpu_16(data[i].min);
		query[i].max = tfp_le_to_cpu_16(data[i].max);

#if (TF_RM_MSG_DEBUG == 1)
		printf("type: %d(0x%x) %d %d\n",
		       query[i].type,
		       query[i].type,
		       query[i].min,
		       query[i].max);
#endif /* (TF_RM_MSG_DEBUG == 1) */

	}

	*resv_strategy = resp.flags &
	      HWRM_TF_SESSION_RESC_QCAPS_OUTPUT_FLAGS_SESS_RESV_STRATEGY_MASK;

cleanup:
	tf_msg_free_dma_buf(&qcaps_buf);

	return rc;
}

int
tf_msg_session_resc_alloc(struct tf *tfp,
			  enum tf_dir dir,
			  uint16_t size,
			  struct tf_rm_resc_req_entry *request,
			  struct tf_rm_resc_entry *resv)
{
	int rc;
	int i;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_session_resc_alloc_input req = { 0 };
	struct hwrm_tf_session_resc_alloc_output resp = { 0 };
	uint8_t fw_session_id;
	struct tf_msg_dma_buf req_buf = { 0 };
	struct tf_msg_dma_buf resv_buf = { 0 };
	struct tf_rm_resc_req_entry *req_data;
	struct tf_rm_resc_entry *resv_data;
	int dma_size;

	TF_CHECK_PARMS3(tfp, request, resv);

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	/* Prepare DMA buffers */
	dma_size = size * sizeof(struct tf_rm_resc_req_entry);
	rc = tf_msg_alloc_dma_buf(&req_buf, dma_size);
	if (rc)
		return rc;

	dma_size = size * sizeof(struct tf_rm_resc_entry);
	rc = tf_msg_alloc_dma_buf(&resv_buf, dma_size);
	if (rc) {
		tf_msg_free_dma_buf(&req_buf);
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_16(dir);
	req.req_size = size;

	req_data = (struct tf_rm_resc_req_entry *)req_buf.va_addr;
	for (i = 0; i < size; i++) {
		req_data[i].type = tfp_cpu_to_le_32(request[i].type);
		req_data[i].min = tfp_cpu_to_le_16(request[i].min);
		req_data[i].max = tfp_cpu_to_le_16(request[i].max);
	}

	req.req_addr = tfp_cpu_to_le_64(req_buf.pa_addr);
	req.resc_addr = tfp_cpu_to_le_64(resv_buf.pa_addr);

	parms.tf_type = HWRM_TF_SESSION_RESC_ALLOC;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);
	if (rc)
		goto cleanup;

	/* Process the response
	 * Should always get expected number of entries
	 */
	if (tfp_le_to_cpu_32(resp.size) != size) {
		TFP_DRV_LOG(ERR,
			    "%s: Alloc message size error, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(EINVAL));
		rc = -EINVAL;
		goto cleanup;
	}

#if (TF_RM_MSG_DEBUG == 1)
	printf("\nRESV\n");
	printf("size: %d\n", tfp_le_to_cpu_32(resp.size));
#endif /* (TF_RM_MSG_DEBUG == 1) */

	/* Post process the response */
	resv_data = (struct tf_rm_resc_entry *)resv_buf.va_addr;
	for (i = 0; i < size; i++) {
		resv[i].type = tfp_le_to_cpu_32(resv_data[i].type);
		resv[i].start = tfp_le_to_cpu_16(resv_data[i].start);
		resv[i].stride = tfp_le_to_cpu_16(resv_data[i].stride);

#if (TF_RM_MSG_DEBUG == 1)
		printf("%d type: %d(0x%x) %d %d\n",
		       i,
		       resv[i].type,
		       resv[i].type,
		       resv[i].start,
		       resv[i].stride);
#endif /* (TF_RM_MSG_DEBUG == 1) */
	}

cleanup:
	tf_msg_free_dma_buf(&req_buf);
	tf_msg_free_dma_buf(&resv_buf);

	return rc;
}

int
tf_msg_session_resc_flush(struct tf *tfp,
			  enum tf_dir dir,
			  uint16_t size,
			  struct tf_rm_resc_entry *resv)
{
	int rc;
	int i;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_session_resc_flush_input req = { 0 };
	struct hwrm_tf_session_resc_flush_output resp = { 0 };
	uint8_t fw_session_id;
	struct tf_msg_dma_buf resv_buf = { 0 };
	struct tf_rm_resc_entry *resv_data;
	int dma_size;

	TF_CHECK_PARMS2(tfp, resv);

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	/* Prepare DMA buffers */
	dma_size = size * sizeof(struct tf_rm_resc_entry);
	rc = tf_msg_alloc_dma_buf(&resv_buf, dma_size);
	if (rc)
		return rc;

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_16(dir);
	req.flush_size = size;

	resv_data = (struct tf_rm_resc_entry *)resv_buf.va_addr;
	for (i = 0; i < size; i++) {
		resv_data[i].type = tfp_cpu_to_le_32(resv[i].type);
		resv_data[i].start = tfp_cpu_to_le_16(resv[i].start);
		resv_data[i].stride = tfp_cpu_to_le_16(resv[i].stride);
	}

	req.flush_addr = tfp_cpu_to_le_64(resv_buf.pa_addr);

	parms.tf_type = HWRM_TF_SESSION_RESC_FLUSH;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);

	tf_msg_free_dma_buf(&resv_buf);

	return rc;
}

int
tf_msg_insert_em_internal_entry(struct tf *tfp,
				struct tf_insert_em_entry_parms *em_parms,
				uint16_t *rptr_index,
				uint8_t *rptr_entry,
				uint8_t *num_of_entries)
{
	int rc;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_em_insert_input req = { 0 };
	struct hwrm_tf_em_insert_output resp = { 0 };
	struct tf_em_64b_entry *em_result =
		(struct tf_em_64b_entry *)em_parms->em_record;
	uint16_t flags;
	uint8_t fw_session_id;
	uint8_t msg_key_size;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(em_parms->dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);

	/* Check for key size conformity */
	msg_key_size = (em_parms->key_sz_in_bits + 7) / 8;
	if (msg_key_size > TF_MSG_EM_INSERT_KEY_SIZE) {
		rc = -EINVAL;
		TFP_DRV_LOG(ERR,
			    "%s: Invalid parameters for msg type, rc:%s\n",
			    tf_dir_2_str(em_parms->dir),
			    strerror(-rc));
		return rc;
	}

	tfp_memcpy(req.em_key,
		   em_parms->key,
		   msg_key_size);

	flags = (em_parms->dir == TF_DIR_TX ?
		 HWRM_TF_EM_INSERT_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_EM_INSERT_INPUT_FLAGS_DIR_RX);
	req.flags = tfp_cpu_to_le_16(flags);
	req.strength = (em_result->hdr.word1 &
			CFA_P4_EEM_ENTRY_STRENGTH_MASK) >>
			CFA_P4_EEM_ENTRY_STRENGTH_SHIFT;
	req.em_key_bitlen = em_parms->key_sz_in_bits;
	req.action_ptr = em_result->hdr.pointer;
	req.em_record_idx = *rptr_index;

	parms.tf_type = HWRM_TF_EM_INSERT;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	*rptr_entry = resp.rptr_entry;
	*rptr_index = resp.rptr_index;
	*num_of_entries = resp.num_of_entries;

	return 0;
}

int
tf_msg_delete_em_entry(struct tf *tfp,
		       struct tf_delete_em_entry_parms *em_parms)
{
	int rc;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_em_delete_input req = { 0 };
	struct hwrm_tf_em_delete_output resp = { 0 };
	uint16_t flags;
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(em_parms->dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);

	flags = (em_parms->dir == TF_DIR_TX ?
		 HWRM_TF_EM_DELETE_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_EM_DELETE_INPUT_FLAGS_DIR_RX);
	req.flags = tfp_cpu_to_le_16(flags);
	req.flow_handle = tfp_cpu_to_le_64(em_parms->flow_handle);

	parms.tf_type = HWRM_TF_EM_DELETE;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	em_parms->index = tfp_le_to_cpu_16(resp.em_index);

	return 0;
}

int
tf_msg_em_mem_rgtr(struct tf *tfp,
		   int page_lvl,
		   int page_size,
		   uint64_t dma_addr,
		   uint16_t *ctx_id)
{
	int rc;
	struct hwrm_tf_ctxt_mem_rgtr_input req = { 0 };
	struct hwrm_tf_ctxt_mem_rgtr_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };

	req.page_level = page_lvl;
	req.page_size = page_size;
	req.page_dir = tfp_cpu_to_le_64(dma_addr);

	parms.tf_type = HWRM_TF_CTXT_MEM_RGTR;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	*ctx_id = tfp_le_to_cpu_16(resp.ctx_id);

	return rc;
}

int
tf_msg_em_mem_unrgtr(struct tf *tfp,
		     uint16_t *ctx_id)
{
	int rc;
	struct hwrm_tf_ctxt_mem_unrgtr_input req = {0};
	struct hwrm_tf_ctxt_mem_unrgtr_output resp = {0};
	struct tfp_send_msg_parms parms = { 0 };

	req.ctx_id = tfp_cpu_to_le_32(*ctx_id);

	parms.tf_type = HWRM_TF_CTXT_MEM_UNRGTR;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	return rc;
}

int
tf_msg_em_qcaps(struct tf *tfp,
		int dir,
		struct tf_em_caps *em_caps)
{
	int rc;
	struct hwrm_tf_ext_em_qcaps_input  req = {0};
	struct hwrm_tf_ext_em_qcaps_output resp = { 0 };
	uint32_t             flags;
	struct tfp_send_msg_parms parms = { 0 };

	flags = (dir == TF_DIR_TX ? HWRM_TF_EXT_EM_QCAPS_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_EXT_EM_QCAPS_INPUT_FLAGS_DIR_RX);
	req.flags = tfp_cpu_to_le_32(flags);

	parms.tf_type = HWRM_TF_EXT_EM_QCAPS;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	em_caps->supported = tfp_le_to_cpu_32(resp.supported);
	em_caps->max_entries_supported =
		tfp_le_to_cpu_32(resp.max_entries_supported);
	em_caps->key_entry_size = tfp_le_to_cpu_16(resp.key_entry_size);
	em_caps->record_entry_size =
		tfp_le_to_cpu_16(resp.record_entry_size);
	em_caps->efc_entry_size = tfp_le_to_cpu_16(resp.efc_entry_size);

	return rc;
}

int
tf_msg_em_cfg(struct tf *tfp,
	      uint32_t num_entries,
	      uint16_t key0_ctx_id,
	      uint16_t key1_ctx_id,
	      uint16_t record_ctx_id,
	      uint16_t efc_ctx_id,
	      uint8_t flush_interval,
	      int dir)
{
	int rc;
	struct hwrm_tf_ext_em_cfg_input  req = {0};
	struct hwrm_tf_ext_em_cfg_output resp = {0};
	uint32_t flags;
	struct tfp_send_msg_parms parms = { 0 };

	flags = (dir == TF_DIR_TX ? HWRM_TF_EXT_EM_CFG_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_EXT_EM_CFG_INPUT_FLAGS_DIR_RX);
	flags |= HWRM_TF_EXT_EM_QCAPS_INPUT_FLAGS_PREFERRED_OFFLOAD;

	req.flags = tfp_cpu_to_le_32(flags);
	req.num_entries = tfp_cpu_to_le_32(num_entries);

	req.flush_interval = flush_interval;

	req.key0_ctx_id = tfp_cpu_to_le_16(key0_ctx_id);
	req.key1_ctx_id = tfp_cpu_to_le_16(key1_ctx_id);
	req.record_ctx_id = tfp_cpu_to_le_16(record_ctx_id);
	req.efc_ctx_id = tfp_cpu_to_le_16(efc_ctx_id);

	parms.tf_type = HWRM_TF_EXT_EM_CFG;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	return rc;
}

int
tf_msg_em_op(struct tf *tfp,
	     int dir,
	     uint16_t op)
{
	int rc;
	struct hwrm_tf_ext_em_op_input req = {0};
	struct hwrm_tf_ext_em_op_output resp = {0};
	uint32_t flags;
	struct tfp_send_msg_parms parms = { 0 };

	flags = (dir == TF_DIR_TX ? HWRM_TF_EXT_EM_CFG_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_EXT_EM_CFG_INPUT_FLAGS_DIR_RX);
	req.flags = tfp_cpu_to_le_32(flags);
	req.op = tfp_cpu_to_le_16(op);

	parms.tf_type = HWRM_TF_EXT_EM_OP;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	return rc;
}

int
tf_msg_tcam_entry_set(struct tf *tfp,
		      struct tf_tcam_set_parms *parms)
{
	int rc;
	struct tfp_send_msg_parms mparms = { 0 };
	struct hwrm_tf_tcam_set_input req = { 0 };
	struct hwrm_tf_tcam_set_output resp = { 0 };
	struct tf_msg_dma_buf buf = { 0 };
	uint8_t *data = NULL;
	int data_size = 0;
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(parms->dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.type = parms->hcapi_type;
	req.idx = tfp_cpu_to_le_16(parms->idx);
	if (parms->dir == TF_DIR_TX)
		req.flags |= HWRM_TF_TCAM_SET_INPUT_FLAGS_DIR_TX;

	req.key_size = parms->key_size;
	req.mask_offset = parms->key_size;
	/* Result follows after key and mask, thus multiply by 2 */
	req.result_offset = 2 * parms->key_size;
	req.result_size = parms->result_size;
	data_size = 2 * req.key_size + req.result_size;

	if (data_size <= TF_PCI_BUF_SIZE_MAX) {
		/* use pci buffer */
		data = &req.dev_data[0];
	} else {
		/* use dma buffer */
		req.flags |= HWRM_TF_TCAM_SET_INPUT_FLAGS_DMA;
		rc = tf_msg_alloc_dma_buf(&buf, data_size);
		if (rc)
			goto cleanup;
		data = buf.va_addr;
		tfp_memcpy(&req.dev_data[0],
			   &buf.pa_addr,
			   sizeof(buf.pa_addr));
	}

	tfp_memcpy(&data[0], parms->key, parms->key_size);
	tfp_memcpy(&data[parms->key_size], parms->mask, parms->key_size);
	tfp_memcpy(&data[req.result_offset], parms->result, parms->result_size);

	mparms.tf_type = HWRM_TF_TCAM_SET;
	mparms.req_data = (uint32_t *)&req;
	mparms.req_size = sizeof(req);
	mparms.resp_data = (uint32_t *)&resp;
	mparms.resp_size = sizeof(resp);
	mparms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &mparms);

cleanup:
	tf_msg_free_dma_buf(&buf);

	return rc;
}

int
tf_msg_tcam_entry_free(struct tf *tfp,
		       struct tf_tcam_free_parms *in_parms)
{
	int rc;
	struct hwrm_tf_tcam_free_input req =  { 0 };
	struct hwrm_tf_tcam_free_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(in_parms->dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.type = in_parms->hcapi_type;
	req.count = 1;
	req.idx_list[0] = tfp_cpu_to_le_16(in_parms->idx);
	if (in_parms->dir == TF_DIR_TX)
		req.flags |= HWRM_TF_TCAM_FREE_INPUT_FLAGS_DIR_TX;

	parms.tf_type = HWRM_TF_TCAM_FREE;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	return rc;
}

int
tf_msg_set_tbl_entry(struct tf *tfp,
		     enum tf_dir dir,
		     uint16_t hcapi_type,
		     uint16_t size,
		     uint8_t *data,
		     uint32_t index)
{
	int rc;
	struct hwrm_tf_tbl_type_set_input req = { 0 };
	struct hwrm_tf_tbl_type_set_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_16(dir);
	req.type = tfp_cpu_to_le_32(hcapi_type);
	req.size = tfp_cpu_to_le_16(size);
	req.index = tfp_cpu_to_le_32(index);

	/* Check for data size conformity */
	if (size > TF_MSG_TBL_TYPE_SET_DATA_SIZE) {
		rc = -EINVAL;
		TFP_DRV_LOG(ERR,
			    "%s: Invalid parameters for msg type, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	tfp_memcpy(&req.data,
		   data,
		   size);

	parms.tf_type = HWRM_TF_TBL_TYPE_SET;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}

int
tf_msg_get_tbl_entry(struct tf *tfp,
		     enum tf_dir dir,
		     uint16_t hcapi_type,
		     uint16_t size,
		     uint8_t *data,
		     uint32_t index)
{
	int rc;
	struct hwrm_tf_tbl_type_get_input req = { 0 };
	struct hwrm_tf_tbl_type_get_output resp = { 0 };
	struct tfp_send_msg_parms parms = { 0 };
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_16(dir);
	req.type = tfp_cpu_to_le_32(hcapi_type);
	req.index = tfp_cpu_to_le_32(index);

	parms.tf_type = HWRM_TF_TBL_TYPE_GET;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp,
				 &parms);
	if (rc)
		return rc;

	/* Verify that we got enough buffer to return the requested data */
	if (tfp_le_to_cpu_32(resp.size) != size)
		return -EINVAL;

	tfp_memcpy(data,
		   &resp.data,
		   size);

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}

/* HWRM Tunneled messages */

int
tf_msg_get_global_cfg(struct tf *tfp,
		      struct tf_global_cfg_parms *params)
{
	int rc = 0;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_global_cfg_get_input req = { 0 };
	struct hwrm_tf_global_cfg_get_output resp = { 0 };
	uint32_t flags = 0;
	uint8_t fw_session_id;
	uint16_t resp_size = 0;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(params->dir),
			    strerror(-rc));
		return rc;
	}

	flags = (params->dir == TF_DIR_TX ?
		 HWRM_TF_GLOBAL_CFG_GET_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_GLOBAL_CFG_GET_INPUT_FLAGS_DIR_RX);

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_32(flags);
	req.type = tfp_cpu_to_le_32(params->type);
	req.offset = tfp_cpu_to_le_32(params->offset);
	req.size = tfp_cpu_to_le_32(params->config_sz_in_bytes);

	parms.tf_type = HWRM_TF_GLOBAL_CFG_GET;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);
	if (rc != 0)
		return rc;

	/* Verify that we got enough buffer to return the requested data */
	resp_size = tfp_le_to_cpu_16(resp.size);
	if (resp_size < params->config_sz_in_bytes)
		return -EINVAL;

	if (params->config)
		tfp_memcpy(params->config,
			   resp.data,
			   resp_size);
	else
		return -EFAULT;

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}

int
tf_msg_set_global_cfg(struct tf *tfp,
		      struct tf_global_cfg_parms *params)
{
	int rc = 0;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_global_cfg_set_input req = { 0 };
	struct hwrm_tf_global_cfg_set_output resp = { 0 };
	uint32_t flags = 0;
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(params->dir),
			    strerror(-rc));
		return rc;
	}

	flags = (params->dir == TF_DIR_TX ?
		 HWRM_TF_GLOBAL_CFG_SET_INPUT_FLAGS_DIR_TX :
		 HWRM_TF_GLOBAL_CFG_SET_INPUT_FLAGS_DIR_RX);

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_32(flags);
	req.type = tfp_cpu_to_le_32(params->type);
	req.offset = tfp_cpu_to_le_32(params->offset);

	/* Check for data size conformity */
	if (params->config_sz_in_bytes > TF_MSG_SET_GLOBAL_CFG_DATA_SIZE) {
		rc = -EINVAL;
		TFP_DRV_LOG(ERR,
			    "%s: Invalid parameters for msg type, rc:%s\n",
			    tf_dir_2_str(params->dir),
			    strerror(-rc));
		return rc;
	}

	tfp_memcpy(req.data, params->config,
		   params->config_sz_in_bytes);

	/* Only set mask if pointer is provided
	 */
	if (params->config_mask) {
		tfp_memcpy(req.data + params->config_sz_in_bytes,
			   params->config_mask,
			   params->config_sz_in_bytes);
	}

	req.size = tfp_cpu_to_le_32(params->config_sz_in_bytes);

	parms.tf_type = HWRM_TF_GLOBAL_CFG_SET;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);

	if (rc != 0)
		return rc;

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}

int
tf_msg_bulk_get_tbl_entry(struct tf *tfp,
			  enum tf_dir dir,
			  uint16_t hcapi_type,
			  uint32_t starting_idx,
			  uint16_t num_entries,
			  uint16_t entry_sz_in_bytes,
			  uint64_t physical_mem_addr)
{
	int rc;
	struct tfp_send_msg_parms parms = { 0 };
	struct tf_tbl_type_bulk_get_input req = { 0 };
	struct tf_tbl_type_bulk_get_output resp = { 0 };
	int data_size = 0;
	uint8_t fw_session_id;

	rc = tf_session_get_fw_session_id(tfp, &fw_session_id);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Unable to lookup FW id, rc:%s\n",
			    tf_dir_2_str(dir),
			    strerror(-rc));
		return rc;
	}

	/* Populate the request */
	req.fw_session_id = tfp_cpu_to_le_32(fw_session_id);
	req.flags = tfp_cpu_to_le_16(dir);
	req.type = tfp_cpu_to_le_32(hcapi_type);
	req.start_index = tfp_cpu_to_le_32(starting_idx);
	req.num_entries = tfp_cpu_to_le_32(num_entries);

	data_size = num_entries * entry_sz_in_bytes;

	req.host_addr = tfp_cpu_to_le_64(physical_mem_addr);

	MSG_PREP(parms,
		 TF_KONG_MB,
		 HWRM_TF,
		 HWRM_TFT_TBL_TYPE_BULK_GET,
		 req,
		 resp);

	rc = tfp_send_msg_tunneled(tfp, &parms);
	if (rc)
		return rc;

	/* Verify that we got enough buffer to return the requested data */
	if (tfp_le_to_cpu_32(resp.size) != data_size)
		return -EINVAL;

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}

int
tf_msg_get_if_tbl_entry(struct tf *tfp,
			struct tf_if_tbl_get_parms *params)
{
	int rc = 0;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_if_tbl_get_input req = { 0 };
	struct hwrm_tf_if_tbl_get_output resp = { 0 };
	uint32_t flags = 0;
	struct tf_session *tfs;

	/* Retrieve the session information */
	rc = tf_session_get_session(tfp, &tfs);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Failed to lookup session, rc:%s\n",
			    tf_dir_2_str(params->dir),
			    strerror(-rc));
		return rc;
	}

	flags = (params->dir == TF_DIR_TX ?
		HWRM_TF_IF_TBL_GET_INPUT_FLAGS_DIR_TX :
		HWRM_TF_IF_TBL_GET_INPUT_FLAGS_DIR_RX);

	/* Populate the request */
	req.fw_session_id =
		tfp_cpu_to_le_32(tfs->session_id.internal.fw_session_id);
	req.flags = flags;
	req.type = params->hcapi_type;
	req.index = tfp_cpu_to_le_16(params->idx);
	req.size = tfp_cpu_to_le_16(params->data_sz_in_bytes);

	parms.tf_type = HWRM_TF_IF_TBL_GET;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);

	if (rc != 0)
		return rc;

	if (parms.tf_resp_code != 0)
		return tfp_le_to_cpu_32(parms.tf_resp_code);

	tfp_memcpy(&params->data[0], resp.data, req.size);

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}

int
tf_msg_set_if_tbl_entry(struct tf *tfp,
			struct tf_if_tbl_set_parms *params)
{
	int rc = 0;
	struct tfp_send_msg_parms parms = { 0 };
	struct hwrm_tf_if_tbl_set_input req = { 0 };
	struct hwrm_tf_if_tbl_get_output resp = { 0 };
	uint32_t flags = 0;
	struct tf_session *tfs;

	/* Retrieve the session information */
	rc = tf_session_get_session(tfp, &tfs);
	if (rc) {
		TFP_DRV_LOG(ERR,
			    "%s: Failed to lookup session, rc:%s\n",
			    tf_dir_2_str(params->dir),
			    strerror(-rc));
		return rc;
	}


	flags = (params->dir == TF_DIR_TX ?
		HWRM_TF_IF_TBL_SET_INPUT_FLAGS_DIR_TX :
		HWRM_TF_IF_TBL_SET_INPUT_FLAGS_DIR_RX);

	/* Populate the request */
	req.fw_session_id =
		tfp_cpu_to_le_32(tfs->session_id.internal.fw_session_id);
	req.flags = flags;
	req.type = params->hcapi_type;
	req.index = tfp_cpu_to_le_32(params->idx);
	req.size = tfp_cpu_to_le_32(params->data_sz_in_bytes);
	tfp_memcpy(&req.data[0], params->data, params->data_sz_in_bytes);

	parms.tf_type = HWRM_TF_IF_TBL_SET;
	parms.req_data = (uint32_t *)&req;
	parms.req_size = sizeof(req);
	parms.resp_data = (uint32_t *)&resp;
	parms.resp_size = sizeof(resp);
	parms.mailbox = TF_KONG_MB;

	rc = tfp_send_msg_direct(tfp, &parms);

	if (rc != 0)
		return rc;

	return tfp_le_to_cpu_32(parms.tf_resp_code);
}