Linux Audio

Check our new training course

Loading...
   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
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2016-2018 Microsoft Corporation
 * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
 * All rights reserved.
 */

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <malloc.h>

#include <rte_ethdev.h>
#include <rte_memcpy.h>
#include <rte_string_fns.h>
#include <rte_memzone.h>
#include <rte_malloc.h>
#include <rte_atomic.h>
#include <rte_bitmap.h>
#include <rte_branch_prediction.h>
#include <rte_ether.h>
#include <rte_common.h>
#include <rte_errno.h>
#include <rte_memory.h>
#include <rte_eal.h>
#include <dev_driver.h>
#include <rte_net.h>
#include <bus_vmbus_driver.h>
#include <rte_spinlock.h>

#include "hn_logs.h"
#include "hn_var.h"
#include "hn_rndis.h"
#include "hn_nvs.h"
#include "ndis.h"

#define HN_NVS_SEND_MSG_SIZE \
	(sizeof(struct vmbus_chanpkt_hdr) + sizeof(struct hn_nvs_rndis))

#define HN_TXD_CACHE_SIZE	32 /* per cpu tx_descriptor pool cache */
#define HN_RXQ_EVENT_DEFAULT	2048

struct hn_rxinfo {
	uint32_t	vlan_info;
	uint32_t	csum_info;
	uint32_t	hash_info;
	uint32_t	hash_value;
};

#define HN_RXINFO_VLAN			0x0001
#define HN_RXINFO_CSUM			0x0002
#define HN_RXINFO_HASHINF		0x0004
#define HN_RXINFO_HASHVAL		0x0008
#define HN_RXINFO_ALL			\
	(HN_RXINFO_VLAN |		\
	 HN_RXINFO_CSUM |		\
	 HN_RXINFO_HASHINF |		\
	 HN_RXINFO_HASHVAL)

#define HN_NDIS_VLAN_INFO_INVALID	0xffffffff
#define HN_NDIS_RXCSUM_INFO_INVALID	0
#define HN_NDIS_HASH_INFO_INVALID	0

/*
 * Per-transmit book keeping.
 * A slot in transmit ring (chim_index) is reserved for each transmit.
 *
 * There are two types of transmit:
 *   - buffered transmit where chimney buffer is used and RNDIS header
 *     is in the buffer. mbuf == NULL for this case.
 *
 *   - direct transmit where RNDIS header is in the in  rndis_pkt
 *     mbuf is freed after transmit.
 *
 * Descriptors come from per-port pool which is used
 * to limit number of outstanding requests per device.
 */
struct hn_txdesc {
	struct rte_mbuf *m;

	uint16_t	queue_id;
	uint32_t	chim_index;
	uint32_t	chim_size;
	uint32_t	data_size;
	uint32_t	packets;

	struct rndis_packet_msg *rndis_pkt;
};

#define HN_RNDIS_PKT_LEN				\
	(sizeof(struct rndis_packet_msg) +		\
	 RNDIS_PKTINFO_SIZE(NDIS_HASH_VALUE_SIZE) +	\
	 RNDIS_PKTINFO_SIZE(NDIS_VLAN_INFO_SIZE) +	\
	 RNDIS_PKTINFO_SIZE(NDIS_LSO2_INFO_SIZE) +	\
	 RNDIS_PKTINFO_SIZE(NDIS_TXCSUM_INFO_SIZE))

#define HN_RNDIS_PKT_ALIGNED	RTE_ALIGN(HN_RNDIS_PKT_LEN, RTE_CACHE_LINE_SIZE)

/* Minimum space required for a packet */
#define HN_PKTSIZE_MIN(align) \
	RTE_ALIGN(RTE_ETHER_MIN_LEN + HN_RNDIS_PKT_LEN, align)

#define DEFAULT_TX_FREE_THRESH 32

static void
hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)
{
	uint32_t s = m->pkt_len;
	const struct rte_ether_addr *ea;

	if (s == 64) {
		stats->size_bins[1]++;
	} else if (s > 64 && s < 1024) {
		uint32_t bin;

		/* count zeros, and offset into correct bin */
		bin = (sizeof(s) * 8) - rte_clz32(s) - 5;
		stats->size_bins[bin]++;
	} else {
		if (s < 64)
			stats->size_bins[0]++;
		else if (s < 1519)
			stats->size_bins[6]++;
		else
			stats->size_bins[7]++;
	}

	ea = rte_pktmbuf_mtod(m, const struct rte_ether_addr *);
	if (rte_is_multicast_ether_addr(ea)) {
		if (rte_is_broadcast_ether_addr(ea))
			stats->broadcast++;
		else
			stats->multicast++;
	}
}

static inline unsigned int hn_rndis_pktlen(const struct rndis_packet_msg *pkt)
{
	return pkt->pktinfooffset + pkt->pktinfolen;
}

static inline uint32_t
hn_rndis_pktmsg_offset(uint32_t ofs)
{
	return ofs - offsetof(struct rndis_packet_msg, dataoffset);
}

static void hn_txd_init(struct rte_mempool *mp __rte_unused,
			void *opaque, void *obj, unsigned int idx)
{
	struct hn_tx_queue *txq = opaque;
	struct hn_txdesc *txd = obj;

	memset(txd, 0, sizeof(*txd));

	txd->queue_id = txq->queue_id;
	txd->chim_index = NVS_CHIM_IDX_INVALID;
	txd->rndis_pkt = (struct rndis_packet_msg *)((char *)txq->tx_rndis
		+ idx * HN_RNDIS_PKT_ALIGNED);
}

int
hn_chim_init(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	uint32_t i, chim_bmp_size;

	rte_spinlock_init(&hv->chim_lock);
	chim_bmp_size = rte_bitmap_get_memory_footprint(hv->chim_cnt);
	hv->chim_bmem = rte_zmalloc("hn_chim_bitmap", chim_bmp_size,
				    RTE_CACHE_LINE_SIZE);
	if (hv->chim_bmem == NULL) {
		PMD_INIT_LOG(ERR, "failed to allocate bitmap size %u",
			     chim_bmp_size);
		return -1;
	}

	hv->chim_bmap = rte_bitmap_init(hv->chim_cnt,
					hv->chim_bmem, chim_bmp_size);
	if (hv->chim_bmap == NULL) {
		PMD_INIT_LOG(ERR, "failed to init chim bitmap");
		return -1;
	}

	for (i = 0; i < hv->chim_cnt; i++)
		rte_bitmap_set(hv->chim_bmap, i);

	return 0;
}

void
hn_chim_uninit(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;

	rte_bitmap_free(hv->chim_bmap);
	rte_free(hv->chim_bmem);
	hv->chim_bmem = NULL;
}

static uint32_t hn_chim_alloc(struct hn_data *hv)
{
	uint32_t index = NVS_CHIM_IDX_INVALID;
	uint64_t slab = 0;

	rte_spinlock_lock(&hv->chim_lock);
	if (rte_bitmap_scan(hv->chim_bmap, &index, &slab)) {
		index += rte_bsf64(slab);
		rte_bitmap_clear(hv->chim_bmap, index);
	}
	rte_spinlock_unlock(&hv->chim_lock);

	return index;
}

static void hn_chim_free(struct hn_data *hv, uint32_t chim_idx)
{
	if (chim_idx >= hv->chim_cnt) {
		PMD_DRV_LOG(ERR, "Invalid chimney index %u", chim_idx);
	} else {
		rte_spinlock_lock(&hv->chim_lock);
		rte_bitmap_set(hv->chim_bmap, chim_idx);
		rte_spinlock_unlock(&hv->chim_lock);
	}
}

static void hn_reset_txagg(struct hn_tx_queue *txq)
{
	txq->agg_szleft = txq->agg_szmax;
	txq->agg_pktleft = txq->agg_pktmax;
	txq->agg_txd = NULL;
	txq->agg_prevpkt = NULL;
}

int
hn_dev_tx_queue_setup(struct rte_eth_dev *dev,
		      uint16_t queue_idx, uint16_t nb_desc,
		      unsigned int socket_id,
		      const struct rte_eth_txconf *tx_conf)

{
	struct hn_data *hv = dev->data->dev_private;
	struct hn_tx_queue *txq;
	char name[RTE_MEMPOOL_NAMESIZE];
	uint32_t tx_free_thresh;
	int err = -ENOMEM;

	PMD_INIT_FUNC_TRACE();

	tx_free_thresh = tx_conf->tx_free_thresh;
	if (tx_free_thresh == 0)
		tx_free_thresh = RTE_MIN(nb_desc / 4,
					 DEFAULT_TX_FREE_THRESH);

	if (tx_free_thresh + 3 >= nb_desc) {
		PMD_INIT_LOG(ERR,
			     "tx_free_thresh must be less than the number of TX entries minus 3(%u)."
			     " (tx_free_thresh=%u port=%u queue=%u)\n",
			     nb_desc - 3,
			     tx_free_thresh, dev->data->port_id, queue_idx);
		return -EINVAL;
	}

	txq = rte_zmalloc_socket("HN_TXQ", sizeof(*txq), RTE_CACHE_LINE_SIZE,
				 socket_id);
	if (!txq)
		return -ENOMEM;

	txq->hv = hv;
	txq->chan = hv->channels[queue_idx];
	txq->port_id = dev->data->port_id;
	txq->queue_id = queue_idx;
	txq->free_thresh = tx_free_thresh;

	snprintf(name, sizeof(name),
		 "hn_txd_%u_%u", dev->data->port_id, queue_idx);

	PMD_INIT_LOG(DEBUG, "TX descriptor pool %s n=%u size=%zu",
		     name, nb_desc, sizeof(struct hn_txdesc));

	txq->tx_rndis_mz = rte_memzone_reserve_aligned(name,
			nb_desc * HN_RNDIS_PKT_ALIGNED, rte_socket_id(),
			RTE_MEMZONE_IOVA_CONTIG, HN_RNDIS_PKT_ALIGNED);
	if (!txq->tx_rndis_mz) {
		err = -rte_errno;
		goto error;
	}
	txq->tx_rndis = txq->tx_rndis_mz->addr;
	txq->tx_rndis_iova = txq->tx_rndis_mz->iova;

	txq->txdesc_pool = rte_mempool_create(name, nb_desc,
					      sizeof(struct hn_txdesc),
					      0, 0, NULL, NULL,
					      hn_txd_init, txq,
					      dev->device->numa_node, 0);
	if (txq->txdesc_pool == NULL) {
		PMD_DRV_LOG(ERR,
			    "mempool %s create failed: %d", name, rte_errno);
		goto error;
	}

	txq->agg_szmax  = RTE_MIN(hv->chim_szmax, hv->rndis_agg_size);
	txq->agg_pktmax = hv->rndis_agg_pkts;
	txq->agg_align  = hv->rndis_agg_align;

	hn_reset_txagg(txq);

	err = hn_vf_tx_queue_setup(dev, queue_idx, nb_desc,
				     socket_id, tx_conf);
	if (err == 0) {
		dev->data->tx_queues[queue_idx] = txq;
		return 0;
	}

error:
	rte_mempool_free(txq->txdesc_pool);
	rte_memzone_free(txq->tx_rndis_mz);
	rte_free(txq);
	return err;
}

void
hn_dev_tx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
		     struct rte_eth_txq_info *qinfo)
{
	struct hn_tx_queue *txq = dev->data->tx_queues[queue_id];

	qinfo->nb_desc = txq->txdesc_pool->size;
	qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
}

static struct hn_txdesc *hn_txd_get(struct hn_tx_queue *txq)
{
	struct hn_txdesc *txd;

	if (rte_mempool_get(txq->txdesc_pool, (void **)&txd)) {
		++txq->stats.ring_full;
		PMD_TX_LOG(DEBUG, "tx pool exhausted!");
		return NULL;
	}

	txd->m = NULL;
	txd->packets = 0;
	txd->data_size = 0;
	txd->chim_size = 0;

	return txd;
}

static void hn_txd_put(struct hn_tx_queue *txq, struct hn_txdesc *txd)
{
	rte_mempool_put(txq->txdesc_pool, txd);
}

void
hn_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
{
	struct hn_tx_queue *txq = dev->data->tx_queues[qid];

	PMD_INIT_FUNC_TRACE();

	if (!txq)
		return;

	rte_mempool_free(txq->txdesc_pool);

	rte_memzone_free(txq->tx_rndis_mz);
	rte_free(txq);
}

/*
 * Check the status of a Tx descriptor in the queue.
 *
 * returns:
 *  - -EINVAL              - offset outside of tx_descriptor pool.
 *  - RTE_ETH_TX_DESC_FULL - descriptor is not acknowledged by host.
 *  - RTE_ETH_TX_DESC_DONE - descriptor is available.
 */
int hn_dev_tx_descriptor_status(void *arg, uint16_t offset)
{
	const struct hn_tx_queue *txq = arg;

	hn_process_events(txq->hv, txq->queue_id, 0);

	if (offset >= rte_mempool_avail_count(txq->txdesc_pool))
		return -EINVAL;

	if (offset < rte_mempool_in_use_count(txq->txdesc_pool))
		return RTE_ETH_TX_DESC_FULL;
	else
		return RTE_ETH_TX_DESC_DONE;
}

static void
hn_nvs_send_completed(struct rte_eth_dev *dev, uint16_t queue_id,
		      unsigned long xactid, const struct hn_nvs_rndis_ack *ack)
{
	struct hn_data *hv = dev->data->dev_private;
	struct hn_txdesc *txd = (struct hn_txdesc *)xactid;
	struct hn_tx_queue *txq;

	/* Control packets are sent with xacid == 0 */
	if (!txd)
		return;

	txq = dev->data->tx_queues[queue_id];
	if (likely(ack->status == NVS_STATUS_OK)) {
		PMD_TX_LOG(DEBUG, "port %u:%u complete tx %u packets %u bytes %u",
			   txq->port_id, txq->queue_id, txd->chim_index,
			   txd->packets, txd->data_size);
		txq->stats.bytes += txd->data_size;
		txq->stats.packets += txd->packets;
	} else {
		PMD_DRV_LOG(NOTICE, "port %u:%u complete tx %u failed status %u",
			    txq->port_id, txq->queue_id, txd->chim_index, ack->status);
		++txq->stats.errors;
	}

	if (txd->chim_index != NVS_CHIM_IDX_INVALID) {
		hn_chim_free(hv, txd->chim_index);
		txd->chim_index = NVS_CHIM_IDX_INVALID;
	}

	rte_pktmbuf_free(txd->m);
	hn_txd_put(txq, txd);
}

/* Handle transmit completion events */
static void
hn_nvs_handle_comp(struct rte_eth_dev *dev, uint16_t queue_id,
		   const struct vmbus_chanpkt_hdr *pkt,
		   const void *data)
{
	const struct hn_nvs_hdr *hdr = data;

	switch (hdr->type) {
	case NVS_TYPE_RNDIS_ACK:
		hn_nvs_send_completed(dev, queue_id, pkt->xactid, data);
		break;

	default:
		PMD_DRV_LOG(NOTICE, "unexpected send completion type %u",
			   hdr->type);
	}
}

/* Parse per-packet info (meta data) */
static int
hn_rndis_rxinfo(const void *info_data, unsigned int info_dlen,
		struct hn_rxinfo *info)
{
	const struct rndis_pktinfo *pi = info_data;
	uint32_t mask = 0;

	while (info_dlen != 0) {
		const void *data;
		uint32_t dlen;

		if (unlikely(info_dlen < sizeof(*pi)))
			return -EINVAL;

		if (unlikely(info_dlen < pi->size))
			return -EINVAL;
		info_dlen -= pi->size;

		if (unlikely(pi->size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
			return -EINVAL;
		if (unlikely(pi->size < pi->offset))
			return -EINVAL;

		dlen = pi->size - pi->offset;
		data = pi->data;

		switch (pi->type) {
		case NDIS_PKTINFO_TYPE_VLAN:
			if (unlikely(dlen < NDIS_VLAN_INFO_SIZE))
				return -EINVAL;
			info->vlan_info = *((const uint32_t *)data);
			mask |= HN_RXINFO_VLAN;
			break;

		case NDIS_PKTINFO_TYPE_CSUM:
			if (unlikely(dlen < NDIS_RXCSUM_INFO_SIZE))
				return -EINVAL;
			info->csum_info = *((const uint32_t *)data);
			mask |= HN_RXINFO_CSUM;
			break;

		case NDIS_PKTINFO_TYPE_HASHVAL:
			if (unlikely(dlen < NDIS_HASH_VALUE_SIZE))
				return -EINVAL;
			info->hash_value = *((const uint32_t *)data);
			mask |= HN_RXINFO_HASHVAL;
			break;

		case NDIS_PKTINFO_TYPE_HASHINF:
			if (unlikely(dlen < NDIS_HASH_INFO_SIZE))
				return -EINVAL;
			info->hash_info = *((const uint32_t *)data);
			mask |= HN_RXINFO_HASHINF;
			break;

		default:
			goto next;
		}

		if (mask == HN_RXINFO_ALL)
			break; /* All found; done */
next:
		pi = (const struct rndis_pktinfo *)
		    ((const uint8_t *)pi + pi->size);
	}

	/*
	 * Final fixup.
	 * - If there is no hash value, invalidate the hash info.
	 */
	if (!(mask & HN_RXINFO_HASHVAL))
		info->hash_info = HN_NDIS_HASH_INFO_INVALID;
	return 0;
}

static void hn_rx_buf_free_cb(void *buf __rte_unused, void *opaque)
{
	struct hn_rx_bufinfo *rxb = opaque;
	struct hn_rx_queue *rxq = rxb->rxq;

	rte_atomic32_dec(&rxq->rxbuf_outstanding);
	hn_nvs_ack_rxbuf(rxb->chan, rxb->xactid);
}

static struct hn_rx_bufinfo *hn_rx_buf_init(struct hn_rx_queue *rxq,
					    const struct vmbus_chanpkt_rxbuf *pkt)
{
	struct hn_rx_bufinfo *rxb;

	rxb = rxq->rxbuf_info + pkt->hdr.xactid;
	rxb->chan = rxq->chan;
	rxb->xactid = pkt->hdr.xactid;
	rxb->rxq = rxq;

	rxb->shinfo.free_cb = hn_rx_buf_free_cb;
	rxb->shinfo.fcb_opaque = rxb;
	rte_mbuf_ext_refcnt_set(&rxb->shinfo, 1);
	return rxb;
}

static void hn_rxpkt(struct hn_rx_queue *rxq, struct hn_rx_bufinfo *rxb,
		     uint8_t *data, unsigned int headroom, unsigned int dlen,
		     const struct hn_rxinfo *info)
{
	struct hn_data *hv = rxq->hv;
	struct rte_mbuf *m;
	bool use_extbuf = false;

	m = rte_pktmbuf_alloc(rxq->mb_pool);
	if (unlikely(!m)) {
		struct rte_eth_dev *dev =
			&rte_eth_devices[rxq->port_id];

		dev->data->rx_mbuf_alloc_failed++;
		return;
	}

	/*
	 * For large packets, avoid copy if possible but need to keep
	 * some space available in receive area for later packets.
	 */
	if (hv->rx_extmbuf_enable && dlen > hv->rx_copybreak &&
	    (uint32_t)rte_atomic32_read(&rxq->rxbuf_outstanding) <
			hv->rxbuf_section_cnt / 2) {
		struct rte_mbuf_ext_shared_info *shinfo;
		const void *rxbuf;
		rte_iova_t iova;

		/*
		 * Build an external mbuf that points to receive area.
		 * Use refcount to handle multiple packets in same
		 * receive buffer section.
		 */
		rxbuf = hv->rxbuf_res.addr;
		iova = rte_mem_virt2iova(rxbuf) + RTE_PTR_DIFF(data, rxbuf);
		shinfo = &rxb->shinfo;

		/* shinfo is already set to 1 by the caller */
		if (rte_mbuf_ext_refcnt_update(shinfo, 1) == 2)
			rte_atomic32_inc(&rxq->rxbuf_outstanding);

		rte_pktmbuf_attach_extbuf(m, data, iova,
					  dlen + headroom, shinfo);
		m->data_off = headroom;
		use_extbuf = true;
	} else {
		/* Mbuf's in pool must be large enough to hold small packets */
		if (unlikely(rte_pktmbuf_tailroom(m) < dlen)) {
			rte_pktmbuf_free_seg(m);
			++rxq->stats.errors;
			return;
		}
		rte_memcpy(rte_pktmbuf_mtod(m, void *),
			   data + headroom, dlen);
	}

	m->port = rxq->port_id;
	m->pkt_len = dlen;
	m->data_len = dlen;
	m->packet_type = rte_net_get_ptype(m, NULL,
					   RTE_PTYPE_L2_MASK |
					   RTE_PTYPE_L3_MASK |
					   RTE_PTYPE_L4_MASK);

	if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
		m->vlan_tci = RTE_VLAN_TCI_MAKE(NDIS_VLAN_INFO_ID(info->vlan_info),
						NDIS_VLAN_INFO_PRI(info->vlan_info),
						NDIS_VLAN_INFO_CFI(info->vlan_info));
		m->ol_flags |= RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_RX_VLAN;

		/* NDIS always strips tag, put it back if necessary */
		if (!hv->vlan_strip && rte_vlan_insert(&m)) {
			PMD_DRV_LOG(DEBUG, "vlan insert failed");
			++rxq->stats.errors;
			if (use_extbuf)
				rte_pktmbuf_detach_extbuf(m);
			rte_pktmbuf_free(m);
			return;
		}
	}

	if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
		if (info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK)
			m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;

		if (info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK
				       | NDIS_RXCSUM_INFO_TCPCS_OK))
			m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_GOOD;
		else if (info->csum_info & (NDIS_RXCSUM_INFO_TCPCS_FAILED
					    | NDIS_RXCSUM_INFO_UDPCS_FAILED))
			m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_BAD;
	}

	if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
		m->ol_flags |= RTE_MBUF_F_RX_RSS_HASH;
		m->hash.rss = info->hash_value;
	}

	PMD_RX_LOG(DEBUG,
		   "port %u:%u RX id %"PRIu64" size %u type %#x ol_flags %#"PRIx64,
		   rxq->port_id, rxq->queue_id, rxb->xactid,
		   m->pkt_len, m->packet_type, m->ol_flags);

	++rxq->stats.packets;
	rxq->stats.bytes += m->pkt_len;
	hn_update_packet_stats(&rxq->stats, m);

	if (unlikely(rte_ring_sp_enqueue(rxq->rx_ring, m) != 0)) {
		++rxq->stats.ring_full;
		PMD_RX_LOG(DEBUG, "rx ring full");
		if (use_extbuf)
			rte_pktmbuf_detach_extbuf(m);
		rte_pktmbuf_free(m);
	}
}

static void hn_rndis_rx_data(struct hn_rx_queue *rxq,
			     struct hn_rx_bufinfo *rxb,
			     void *data, uint32_t dlen)
{
	unsigned int data_off, data_len;
	unsigned int pktinfo_off, pktinfo_len;
	const struct rndis_packet_msg *pkt = data;
	struct hn_rxinfo info = {
		.vlan_info = HN_NDIS_VLAN_INFO_INVALID,
		.csum_info = HN_NDIS_RXCSUM_INFO_INVALID,
		.hash_info = HN_NDIS_HASH_INFO_INVALID,
	};
	int err;

	hn_rndis_dump(pkt);

	if (unlikely(dlen < sizeof(*pkt)))
		goto error;

	if (unlikely(dlen < pkt->len))
		goto error; /* truncated RNDIS from host */

	if (unlikely(pkt->len < pkt->datalen
		     + pkt->oobdatalen + pkt->pktinfolen))
		goto error;

	if (unlikely(pkt->datalen == 0))
		goto error;

	/* Check offsets. */
	if (unlikely(pkt->dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN))
		goto error;

	if (likely(pkt->pktinfooffset > 0) &&
	    unlikely(pkt->pktinfooffset < RNDIS_PACKET_MSG_OFFSET_MIN ||
		     (pkt->pktinfooffset & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK)))
		goto error;

	data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
	data_len = pkt->datalen;
	pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->pktinfooffset);
	pktinfo_len = pkt->pktinfolen;

	if (likely(pktinfo_len > 0)) {
		err = hn_rndis_rxinfo((const uint8_t *)pkt + pktinfo_off,
				      pktinfo_len, &info);
		if (err)
			goto error;
	}

	/* overflow check */
	if (data_len > data_len + data_off || data_len + data_off > pkt->len)
		goto error;

	if (unlikely(data_len < RTE_ETHER_HDR_LEN))
		goto error;

	hn_rxpkt(rxq, rxb, data, data_off, data_len, &info);
	return;
error:
	++rxq->stats.errors;
}

static void
hn_rndis_receive(struct rte_eth_dev *dev, struct hn_rx_queue *rxq,
		 struct hn_rx_bufinfo *rxb, void *buf, uint32_t len)
{
	const struct rndis_msghdr *hdr = buf;

	switch (hdr->type) {
	case RNDIS_PACKET_MSG:
		if (dev->data->dev_started)
			hn_rndis_rx_data(rxq, rxb, buf, len);
		break;

	case RNDIS_INDICATE_STATUS_MSG:
		hn_rndis_link_status(dev, buf);
		break;

	case RNDIS_INITIALIZE_CMPLT:
	case RNDIS_QUERY_CMPLT:
	case RNDIS_SET_CMPLT:
		hn_rndis_receive_response(rxq->hv, buf, len);
		break;

	default:
		PMD_DRV_LOG(NOTICE,
			    "unexpected RNDIS message (type %#x len %u)",
			    hdr->type, len);
		break;
	}
}

static void
hn_nvs_handle_rxbuf(struct rte_eth_dev *dev,
		    struct hn_data *hv,
		    struct hn_rx_queue *rxq,
		    const struct vmbus_chanpkt_hdr *hdr,
		    const void *buf)
{
	const struct vmbus_chanpkt_rxbuf *pkt;
	const struct hn_nvs_hdr *nvs_hdr = buf;
	uint32_t rxbuf_sz = hv->rxbuf_res.len;
	char *rxbuf = hv->rxbuf_res.addr;
	unsigned int i, hlen, count;
	struct hn_rx_bufinfo *rxb;

	/* At minimum we need type header */
	if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*nvs_hdr))) {
		PMD_RX_LOG(ERR, "invalid receive nvs RNDIS");
		return;
	}

	/* Make sure that this is a RNDIS message. */
	if (unlikely(nvs_hdr->type != NVS_TYPE_RNDIS)) {
		PMD_RX_LOG(ERR, "nvs type %u, not RNDIS",
			   nvs_hdr->type);
		return;
	}

	hlen = vmbus_chanpkt_getlen(hdr->hlen);
	if (unlikely(hlen < sizeof(*pkt))) {
		PMD_RX_LOG(ERR, "invalid rxbuf chanpkt");
		return;
	}

	pkt = container_of(hdr, const struct vmbus_chanpkt_rxbuf, hdr);
	if (unlikely(pkt->rxbuf_id != NVS_RXBUF_SIG)) {
		PMD_RX_LOG(ERR, "invalid rxbuf_id 0x%08x",
			   pkt->rxbuf_id);
		return;
	}

	count = pkt->rxbuf_cnt;
	if (unlikely(hlen < offsetof(struct vmbus_chanpkt_rxbuf,
				     rxbuf[count]))) {
		PMD_RX_LOG(ERR, "invalid rxbuf_cnt %u", count);
		return;
	}

	if (pkt->hdr.xactid > hv->rxbuf_section_cnt) {
		PMD_RX_LOG(ERR, "invalid rxbuf section id %" PRIx64,
			   pkt->hdr.xactid);
		return;
	}

	/* Setup receive buffer info to allow for callback */
	rxb = hn_rx_buf_init(rxq, pkt);

	/* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
	for (i = 0; i < count; ++i) {
		unsigned int ofs, len;

		ofs = pkt->rxbuf[i].ofs;
		len = pkt->rxbuf[i].len;

		if (unlikely(ofs + len > rxbuf_sz)) {
			PMD_RX_LOG(ERR,
				   "%uth RNDIS msg overflow ofs %u, len %u",
				   i, ofs, len);
			continue;
		}

		if (unlikely(len == 0)) {
			PMD_RX_LOG(ERR, "%uth RNDIS msg len %u", i, len);
			continue;
		}

		hn_rndis_receive(dev, rxq, rxb,
				 rxbuf + ofs, len);
	}

	/* Send ACK now if external mbuf not used */
	if (rte_mbuf_ext_refcnt_update(&rxb->shinfo, -1) == 0)
		hn_nvs_ack_rxbuf(rxb->chan, rxb->xactid);
}

/*
 * Called when NVS inband events are received.
 * Send up a two part message with port_id and the NVS message
 * to the pipe to the netvsc-vf-event control thread.
 */
static void hn_nvs_handle_notify(struct rte_eth_dev *dev,
				 const struct vmbus_chanpkt_hdr *pkt,
				 const void *data)
{
	const struct hn_nvs_hdr *hdr = data;

	switch (hdr->type) {
	case NVS_TYPE_TXTBL_NOTE:
		/* Transmit indirection table has locking problems
		 * in DPDK and therefore not implemented
		 */
		PMD_DRV_LOG(DEBUG, "host notify of transmit indirection table");
		break;

	case NVS_TYPE_VFASSOC_NOTE:
		hn_nvs_handle_vfassoc(dev, pkt, data);
		break;

	default:
		PMD_DRV_LOG(INFO,
			    "got notify, nvs type %u", hdr->type);
	}
}

struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
				      uint16_t queue_id,
				      unsigned int socket_id)
{
	struct hn_rx_queue *rxq;

	rxq = rte_zmalloc_socket("HN_RXQ", sizeof(*rxq),
				 RTE_CACHE_LINE_SIZE, socket_id);
	if (!rxq)
		return NULL;

	rxq->hv = hv;
	rxq->chan = hv->channels[queue_id];
	rte_spinlock_init(&rxq->ring_lock);
	rxq->port_id = hv->port_id;
	rxq->queue_id = queue_id;
	rxq->event_sz = HN_RXQ_EVENT_DEFAULT;
	rxq->event_buf = rte_malloc_socket("HN_EVENTS", HN_RXQ_EVENT_DEFAULT,
					   RTE_CACHE_LINE_SIZE, socket_id);
	if (!rxq->event_buf) {
		rte_free(rxq);
		return NULL;
	}

	/* setup rxbuf_info for non-primary queue */
	if (queue_id) {
		rxq->rxbuf_info = rte_calloc("HN_RXBUF_INFO",
					hv->rxbuf_section_cnt,
					sizeof(*rxq->rxbuf_info),
					RTE_CACHE_LINE_SIZE);

		if (!rxq->rxbuf_info) {
			PMD_DRV_LOG(ERR,
				"Could not allocate rxbuf info for queue %d\n",
				queue_id);
			rte_free(rxq->event_buf);
			rte_free(rxq);
			return NULL;
		}
	}

	return rxq;
}

void
hn_dev_rx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
		     struct rte_eth_rxq_info *qinfo)
{
	struct hn_rx_queue *rxq = dev->data->rx_queues[queue_id];

	qinfo->mp = rxq->mb_pool;
	qinfo->nb_desc = rxq->rx_ring->size;
	qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
}

int
hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
		      uint16_t queue_idx, uint16_t nb_desc,
		      unsigned int socket_id,
		      const struct rte_eth_rxconf *rx_conf,
		      struct rte_mempool *mp)
{
	struct hn_data *hv = dev->data->dev_private;
	char ring_name[RTE_RING_NAMESIZE];
	struct hn_rx_queue *rxq;
	unsigned int count;
	int error = -ENOMEM;

	PMD_INIT_FUNC_TRACE();

	if (queue_idx == 0) {
		rxq = hv->primary;
	} else {
		rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
		if (!rxq)
			return -ENOMEM;
	}

	rxq->mb_pool = mp;
	count = rte_mempool_avail_count(mp) / dev->data->nb_rx_queues;
	if (nb_desc == 0 || nb_desc > count)
		nb_desc = count;

	/*
	 * Staging ring from receive event logic to rx_pkts.
	 * rx_pkts assumes caller is handling multi-thread issue.
	 * event logic has locking.
	 */
	snprintf(ring_name, sizeof(ring_name),
		 "hn_rx_%u_%u", dev->data->port_id, queue_idx);
	rxq->rx_ring = rte_ring_create(ring_name,
				       rte_align32pow2(nb_desc),
				       socket_id, 0);
	if (!rxq->rx_ring)
		goto fail;

	error = hn_vf_rx_queue_setup(dev, queue_idx, nb_desc,
				     socket_id, rx_conf, mp);
	if (error)
		goto fail;

	dev->data->rx_queues[queue_idx] = rxq;
	return 0;

fail:
	rte_ring_free(rxq->rx_ring);
	rte_free(rxq->rxbuf_info);
	rte_free(rxq->event_buf);
	rte_free(rxq);
	return error;
}

static void
hn_rx_queue_free(struct hn_rx_queue *rxq, bool keep_primary)
{

	if (!rxq)
		return;

	rte_ring_free(rxq->rx_ring);
	rxq->rx_ring = NULL;
	rxq->mb_pool = NULL;

	hn_vf_rx_queue_release(rxq->hv, rxq->queue_id);

	/* Keep primary queue to allow for control operations */
	if (keep_primary && rxq == rxq->hv->primary)
		return;

	rte_free(rxq->rxbuf_info);
	rte_free(rxq->event_buf);
	rte_free(rxq);
}

void
hn_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
{
	struct hn_rx_queue *rxq = dev->data->rx_queues[qid];

	PMD_INIT_FUNC_TRACE();

	hn_rx_queue_free(rxq, true);
}

/*
 * Get the number of used descriptor in a rx queue
 * For this device that means how many packets are pending in the ring.
 */
uint32_t
hn_dev_rx_queue_count(void *rx_queue)
{
	struct hn_rx_queue *rxq = rx_queue;

	return rte_ring_count(rxq->rx_ring);
}

/*
 * Check the status of a Rx descriptor in the queue
 *
 * returns:
 *  - -EINVAL               - offset outside of ring
 *  - RTE_ETH_RX_DESC_AVAIL - no data available yet
 *  - RTE_ETH_RX_DESC_DONE  - data is waiting in staging ring
 */
int hn_dev_rx_queue_status(void *arg, uint16_t offset)
{
	const struct hn_rx_queue *rxq = arg;

	hn_process_events(rxq->hv, rxq->queue_id, 0);
	if (offset >= rxq->rx_ring->capacity)
		return -EINVAL;

	if (offset < rte_ring_count(rxq->rx_ring))
		return RTE_ETH_RX_DESC_DONE;
	else
		return RTE_ETH_RX_DESC_AVAIL;
}

int
hn_dev_tx_done_cleanup(void *arg, uint32_t free_cnt)
{
	struct hn_tx_queue *txq = arg;

	return hn_process_events(txq->hv, txq->queue_id, free_cnt);
}

/*
 * Process pending events on the channel.
 * Called from both Rx queue poll and Tx cleanup
 */
uint32_t hn_process_events(struct hn_data *hv, uint16_t queue_id,
			   uint32_t tx_limit)
{
	struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
	struct hn_rx_queue *rxq;
	uint32_t bytes_read = 0;
	uint32_t tx_done = 0;
	int ret = 0;

	rxq = queue_id == 0 ? hv->primary : dev->data->rx_queues[queue_id];

	/*
	 * Since channel is shared between Rx and TX queue need to have a lock
	 * since DPDK does not force same CPU to be used for Rx/Tx.
	 */
	if (unlikely(!rte_spinlock_trylock(&rxq->ring_lock)))
		return 0;

	for (;;) {
		const struct vmbus_chanpkt_hdr *pkt;
		uint32_t len = rxq->event_sz;
		const void *data;

retry:
		ret = rte_vmbus_chan_recv_raw(rxq->chan, rxq->event_buf, &len);
		if (ret == -EAGAIN)
			break;	/* ring is empty */

		if (unlikely(ret == -ENOBUFS)) {
			/* event buffer not large enough to read ring */

			PMD_DRV_LOG(DEBUG,
				    "event buffer expansion (need %u)", len);
			rxq->event_sz = len + len / 4;
			rxq->event_buf = rte_realloc(rxq->event_buf, rxq->event_sz,
						     RTE_CACHE_LINE_SIZE);
			if (rxq->event_buf)
				goto retry;
			/* out of memory, no more events now */
			rxq->event_sz = 0;
			break;
		}

		if (unlikely(ret <= 0)) {
			/* This indicates a failure to communicate (or worse) */
			rte_exit(EXIT_FAILURE,
				 "vmbus ring buffer error: %d", ret);
		}

		bytes_read += ret;
		pkt = (const struct vmbus_chanpkt_hdr *)rxq->event_buf;
		data = (char *)rxq->event_buf + vmbus_chanpkt_getlen(pkt->hlen);

		switch (pkt->type) {
		case VMBUS_CHANPKT_TYPE_COMP:
			++tx_done;
			hn_nvs_handle_comp(dev, queue_id, pkt, data);
			break;

		case VMBUS_CHANPKT_TYPE_RXBUF:
			hn_nvs_handle_rxbuf(dev, hv, rxq, pkt, data);
			break;

		case VMBUS_CHANPKT_TYPE_INBAND:
			hn_nvs_handle_notify(dev, pkt, data);
			break;

		default:
			PMD_DRV_LOG(ERR, "unknown chan pkt %u", pkt->type);
			break;
		}

		if (tx_limit && tx_done >= tx_limit)
			break;
	}

	if (bytes_read > 0)
		rte_vmbus_chan_signal_read(rxq->chan, bytes_read);

	rte_spinlock_unlock(&rxq->ring_lock);

	return tx_done;
}

static void hn_append_to_chim(struct hn_tx_queue *txq,
			      struct rndis_packet_msg *pkt,
			      const struct rte_mbuf *m)
{
	struct hn_txdesc *txd = txq->agg_txd;
	uint8_t *buf = (uint8_t *)pkt;
	unsigned int data_offs;

	hn_rndis_dump(pkt);

	data_offs = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
	txd->chim_size += pkt->len;
	txd->data_size += m->pkt_len;
	++txd->packets;
	hn_update_packet_stats(&txq->stats, m);

	for (; m; m = m->next) {
		uint16_t len = rte_pktmbuf_data_len(m);

		rte_memcpy(buf + data_offs,
			   rte_pktmbuf_mtod(m, const char *), len);
		data_offs += len;
	}
}

/*
 * Send pending aggregated data in chimney buffer (if any).
 * Returns error if send was unsuccessful because channel ring buffer
 * was full.
 */
static int hn_flush_txagg(struct hn_tx_queue *txq, bool *need_sig)

{
	struct hn_txdesc *txd = txq->agg_txd;
	struct hn_nvs_rndis rndis;
	int ret;

	if (!txd)
		return 0;

	rndis = (struct hn_nvs_rndis) {
		.type = NVS_TYPE_RNDIS,
		.rndis_mtype = NVS_RNDIS_MTYPE_DATA,
		.chim_idx = txd->chim_index,
		.chim_sz = txd->chim_size,
	};

	PMD_TX_LOG(DEBUG, "port %u:%u tx %u size %u",
		   txq->port_id, txq->queue_id, txd->chim_index, txd->chim_size);

	ret = hn_nvs_send(txq->chan, VMBUS_CHANPKT_FLAG_RC,
			  &rndis, sizeof(rndis), (uintptr_t)txd, need_sig);

	if (likely(ret == 0))
		hn_reset_txagg(txq);
	else if (ret == -EAGAIN) {
		PMD_TX_LOG(DEBUG, "port %u:%u channel full",
			   txq->port_id, txq->queue_id);
		++txq->stats.channel_full;
	} else {
		++txq->stats.errors;

		PMD_DRV_LOG(NOTICE, "port %u:%u send failed: %d",
			   txq->port_id, txq->queue_id, ret);
	}
	return ret;
}

/*
 * Try and find a place in a send chimney buffer to put
 * the small packet. If space is available, this routine
 * returns a pointer of where to place the data.
 * If no space, caller should try direct transmit.
 */
static void *
hn_try_txagg(struct hn_data *hv, struct hn_tx_queue *txq,
	     struct hn_txdesc *txd, uint32_t pktsize)
{
	struct hn_txdesc *agg_txd = txq->agg_txd;
	struct rndis_packet_msg *pkt;
	void *chim;

	if (agg_txd) {
		unsigned int padding, olen;

		/*
		 * Update the previous RNDIS packet's total length,
		 * it can be increased due to the mandatory alignment
		 * padding for this RNDIS packet.  And update the
		 * aggregating txdesc's chimney sending buffer size
		 * accordingly.
		 *
		 * Zero-out the padding, as required by the RNDIS spec.
		 */
		pkt = txq->agg_prevpkt;
		olen = pkt->len;
		padding = RTE_ALIGN(olen, txq->agg_align) - olen;
		if (padding > 0) {
			agg_txd->chim_size += padding;
			pkt->len += padding;
			memset((uint8_t *)pkt + olen, 0, padding);
		}

		chim = (uint8_t *)pkt + pkt->len;
		txq->agg_prevpkt = chim;
		txq->agg_pktleft--;
		txq->agg_szleft -= pktsize;
		if (txq->agg_szleft < HN_PKTSIZE_MIN(txq->agg_align)) {
			/*
			 * Probably can't aggregate more packets,
			 * flush this aggregating txdesc proactively.
			 */
			txq->agg_pktleft = 0;
		}

		hn_txd_put(txq, txd);
		return chim;
	}

	txd->chim_index = hn_chim_alloc(hv);
	if (txd->chim_index == NVS_CHIM_IDX_INVALID)
		return NULL;

	chim = (uint8_t *)hv->chim_res.addr
			+ txd->chim_index * hv->chim_szmax;

	txq->agg_txd = txd;
	txq->agg_pktleft = txq->agg_pktmax - 1;
	txq->agg_szleft = txq->agg_szmax - pktsize;
	txq->agg_prevpkt = chim;

	return chim;
}

static inline void *
hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt,
			uint32_t pi_dlen, uint32_t pi_type)
{
	const uint32_t pi_size = RNDIS_PKTINFO_SIZE(pi_dlen);
	struct rndis_pktinfo *pi;

	/*
	 * Per-packet-info does not move; it only grows.
	 *
	 * NOTE:
	 * pktinfooffset in this phase counts from the beginning
	 * of rndis_packet_msg.
	 */
	pi = (struct rndis_pktinfo *)((uint8_t *)pkt + hn_rndis_pktlen(pkt));

	pkt->pktinfolen += pi_size;

	pi->size = pi_size;
	pi->type = pi_type;
	pi->offset = RNDIS_PKTINFO_OFFSET;

	return pi->data;
}

/* Put RNDIS header and packet info on packet */
static void hn_encap(struct rndis_packet_msg *pkt,
		     uint16_t queue_id,
		     const struct rte_mbuf *m)
{
	unsigned int hlen = m->l2_len + m->l3_len;
	uint32_t *pi_data;
	uint32_t pkt_hlen;

	pkt->type = RNDIS_PACKET_MSG;
	pkt->len = m->pkt_len;
	pkt->dataoffset = 0;
	pkt->datalen = m->pkt_len;
	pkt->oobdataoffset = 0;
	pkt->oobdatalen = 0;
	pkt->oobdataelements = 0;
	pkt->pktinfooffset = sizeof(*pkt);
	pkt->pktinfolen = 0;
	pkt->vchandle = 0;
	pkt->reserved = 0;

	/*
	 * Set the hash value for this packet, to the queue_id to cause
	 * TX done event for this packet on the right channel.
	 */
	pi_data = hn_rndis_pktinfo_append(pkt, NDIS_HASH_VALUE_SIZE,
					  NDIS_PKTINFO_TYPE_HASHVAL);
	*pi_data = queue_id;

	if (m->ol_flags & RTE_MBUF_F_TX_VLAN) {
		pi_data = hn_rndis_pktinfo_append(pkt, NDIS_VLAN_INFO_SIZE,
						  NDIS_PKTINFO_TYPE_VLAN);
		*pi_data = NDIS_VLAN_INFO_MAKE(RTE_VLAN_TCI_ID(m->vlan_tci),
					       RTE_VLAN_TCI_PRI(m->vlan_tci),
					       RTE_VLAN_TCI_DEI(m->vlan_tci));
	}

	if (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
		pi_data = hn_rndis_pktinfo_append(pkt, NDIS_LSO2_INFO_SIZE,
						  NDIS_PKTINFO_TYPE_LSO);

		if (m->ol_flags & RTE_MBUF_F_TX_IPV6) {
			*pi_data = NDIS_LSO2_INFO_MAKEIPV6(hlen,
							   m->tso_segsz);
		} else {
			*pi_data = NDIS_LSO2_INFO_MAKEIPV4(hlen,
							   m->tso_segsz);
		}
	} else if ((m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
			RTE_MBUF_F_TX_TCP_CKSUM ||
		   (m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
			RTE_MBUF_F_TX_UDP_CKSUM ||
		   (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM)) {
		pi_data = hn_rndis_pktinfo_append(pkt, NDIS_TXCSUM_INFO_SIZE,
						  NDIS_PKTINFO_TYPE_CSUM);
		*pi_data = 0;

		if (m->ol_flags & RTE_MBUF_F_TX_IPV6)
			*pi_data |= NDIS_TXCSUM_INFO_IPV6;
		if (m->ol_flags & RTE_MBUF_F_TX_IPV4) {
			*pi_data |= NDIS_TXCSUM_INFO_IPV4;

			if (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
				*pi_data |= NDIS_TXCSUM_INFO_IPCS;
		}

		if ((m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
				RTE_MBUF_F_TX_TCP_CKSUM)
			*pi_data |= NDIS_TXCSUM_INFO_MKTCPCS(hlen);
		else if ((m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
				RTE_MBUF_F_TX_UDP_CKSUM)
			*pi_data |= NDIS_TXCSUM_INFO_MKUDPCS(hlen);
	}

	pkt_hlen = pkt->pktinfooffset + pkt->pktinfolen;
	/* Fixup RNDIS packet message total length */
	pkt->len += pkt_hlen;

	/* Convert RNDIS packet message offsets */
	pkt->dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
	pkt->pktinfooffset = hn_rndis_pktmsg_offset(pkt->pktinfooffset);
}

/* How many scatter gather list elements ar needed */
static unsigned int hn_get_slots(const struct rte_mbuf *m)
{
	unsigned int slots = 1; /* for RNDIS header */

	while (m) {
		unsigned int size = rte_pktmbuf_data_len(m);
		unsigned int offs = rte_mbuf_data_iova(m) & PAGE_MASK;

		slots += (offs + size + rte_mem_page_size() - 1) /
				rte_mem_page_size();
		m = m->next;
	}

	return slots;
}

/* Build scatter gather list from chained mbuf */
static unsigned int hn_fill_sg(struct vmbus_gpa *sg,
			       const struct rte_mbuf *m)
{
	unsigned int segs = 0;

	while (m) {
		rte_iova_t addr = rte_mbuf_data_iova(m);
		unsigned int page = addr / rte_mem_page_size();
		unsigned int offset = addr & PAGE_MASK;
		unsigned int len = rte_pktmbuf_data_len(m);

		while (len > 0) {
			unsigned int bytes = RTE_MIN(len,
					rte_mem_page_size() - offset);

			sg[segs].page = page;
			sg[segs].ofs = offset;
			sg[segs].len = bytes;
			segs++;

			++page;
			offset = 0;
			len -= bytes;
		}
		m = m->next;
	}

	return segs;
}

/* Transmit directly from mbuf */
static int hn_xmit_sg(struct hn_tx_queue *txq,
		      const struct hn_txdesc *txd, const struct rte_mbuf *m,
		      bool *need_sig)
{
	struct vmbus_gpa sg[hn_get_slots(m)];
	struct hn_nvs_rndis nvs_rndis = {
		.type = NVS_TYPE_RNDIS,
		.rndis_mtype = NVS_RNDIS_MTYPE_DATA,
		.chim_sz = txd->chim_size,
	};
	rte_iova_t addr;
	unsigned int segs;

	/* attach aggregation data if present */
	if (txd->chim_size > 0)
		nvs_rndis.chim_idx = txd->chim_index;
	else
		nvs_rndis.chim_idx = NVS_CHIM_IDX_INVALID;

	hn_rndis_dump(txd->rndis_pkt);

	/* pass IOVA of rndis header in first segment */
	addr = txq->tx_rndis_iova +
		((char *)txd->rndis_pkt - (char *)txq->tx_rndis);

	sg[0].page = addr / rte_mem_page_size();
	sg[0].ofs = addr & PAGE_MASK;
	sg[0].len = RNDIS_PACKET_MSG_OFFSET_ABS(hn_rndis_pktlen(txd->rndis_pkt));
	segs = 1;

	hn_update_packet_stats(&txq->stats, m);

	segs += hn_fill_sg(sg + 1, m);

	PMD_TX_LOG(DEBUG, "port %u:%u tx %u segs %u size %u",
		   txq->port_id, txq->queue_id, txd->chim_index,
		   segs, nvs_rndis.chim_sz);

	return hn_nvs_send_sglist(txq->chan, sg, segs,
				  &nvs_rndis, sizeof(nvs_rndis),
				  (uintptr_t)txd, need_sig);
}

uint16_t
hn_xmit_pkts(void *ptxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
{
	struct hn_tx_queue *txq = ptxq;
	uint16_t queue_id = txq->queue_id;
	struct hn_data *hv = txq->hv;
	struct rte_eth_dev *vf_dev;
	bool need_sig = false;
	uint16_t nb_tx, tx_thresh;
	int ret;

	if (unlikely(hv->closed))
		return 0;

	/*
	 * Always check for events on the primary channel
	 * because that is where hotplug notifications occur.
	 */
	tx_thresh = RTE_MAX(txq->free_thresh, nb_pkts);
	if (txq->queue_id == 0 ||
	    rte_mempool_avail_count(txq->txdesc_pool) < tx_thresh)
		hn_process_events(hv, txq->queue_id, 0);

	/* Transmit over VF if present and up */
	if (hv->vf_ctx.vf_vsc_switched) {
		rte_rwlock_read_lock(&hv->vf_lock);
		vf_dev = hn_get_vf_dev(hv);
		if (hv->vf_ctx.vf_vsc_switched && vf_dev &&
		    vf_dev->data->dev_started) {
			void *sub_q = vf_dev->data->tx_queues[queue_id];

			nb_tx = (*vf_dev->tx_pkt_burst)
					(sub_q, tx_pkts, nb_pkts);
			rte_rwlock_read_unlock(&hv->vf_lock);
			return nb_tx;
		}
		rte_rwlock_read_unlock(&hv->vf_lock);
	}

	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
		struct rte_mbuf *m = tx_pkts[nb_tx];
		uint32_t pkt_size = m->pkt_len + HN_RNDIS_PKT_LEN;
		struct rndis_packet_msg *pkt;
		struct hn_txdesc *txd;

		txd = hn_txd_get(txq);
		if (txd == NULL)
			break;

		/* For small packets aggregate them in chimney buffer */
		if (m->pkt_len <= hv->tx_copybreak &&
		    pkt_size <= txq->agg_szmax) {
			/* If this packet will not fit, then flush  */
			if (txq->agg_pktleft == 0 ||
			    RTE_ALIGN(pkt_size, txq->agg_align) > txq->agg_szleft) {
				if (hn_flush_txagg(txq, &need_sig))
					goto fail;
			}


			pkt = hn_try_txagg(hv, txq, txd, pkt_size);
			if (unlikely(!pkt))
				break;

			hn_encap(pkt, queue_id, m);
			hn_append_to_chim(txq, pkt, m);

			rte_pktmbuf_free(m);

			/* if buffer is full, flush */
			if (txq->agg_pktleft == 0 &&
			    hn_flush_txagg(txq, &need_sig))
				goto fail;
		} else {
			/* Send any outstanding packets in buffer */
			if (txq->agg_txd && hn_flush_txagg(txq, &need_sig))
				goto fail;

			pkt = txd->rndis_pkt;
			txd->m = m;
			txd->data_size = m->pkt_len;
			++txd->packets;

			hn_encap(pkt, queue_id, m);

			ret = hn_xmit_sg(txq, txd, m, &need_sig);
			if (unlikely(ret != 0)) {
				if (ret == -EAGAIN) {
					PMD_TX_LOG(DEBUG, "sg channel full");
					++txq->stats.channel_full;
				} else {
					PMD_DRV_LOG(NOTICE, "sg send failed: %d", ret);
					++txq->stats.errors;
				}
				hn_txd_put(txq, txd);
				goto fail;
			}
		}
	}

	/* If partial buffer left, then try and send it.
	 * if that fails, then reuse it on next send.
	 */
	hn_flush_txagg(txq, &need_sig);

fail:
	if (need_sig)
		rte_vmbus_chan_signal_tx(txq->chan);

	return nb_tx;
}

static uint16_t
hn_recv_vf(uint16_t vf_port, const struct hn_rx_queue *rxq,
	   struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
{
	uint16_t i, n;

	if (unlikely(nb_pkts == 0))
		return 0;

	n = rte_eth_rx_burst(vf_port, rxq->queue_id, rx_pkts, nb_pkts);

	/* relabel the received mbufs */
	for (i = 0; i < n; i++)
		rx_pkts[i]->port = rxq->port_id;

	return n;
}

uint16_t
hn_recv_pkts(void *prxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
{
	struct hn_rx_queue *rxq = prxq;
	struct hn_data *hv = rxq->hv;
	struct rte_eth_dev *vf_dev;
	uint16_t nb_rcv;

	if (unlikely(hv->closed))
		return 0;

	/* Check for new completions (and hotplug) */
	if (likely(rte_ring_count(rxq->rx_ring) < nb_pkts))
		hn_process_events(hv, rxq->queue_id, 0);

	/* Always check the vmbus path for multicast and new flows */
	nb_rcv = rte_ring_sc_dequeue_burst(rxq->rx_ring,
					   (void **)rx_pkts, nb_pkts, NULL);

	/* If VF is available, check that as well */
	if (hv->vf_ctx.vf_vsc_switched) {
		rte_rwlock_read_lock(&hv->vf_lock);
		vf_dev = hn_get_vf_dev(hv);
		if (hv->vf_ctx.vf_vsc_switched && vf_dev &&
		    vf_dev->data->dev_started)
			nb_rcv += hn_recv_vf(vf_dev->data->port_id, rxq,
					     rx_pkts + nb_rcv,
					     nb_pkts - nb_rcv);

		rte_rwlock_read_unlock(&hv->vf_lock);
	}
	return nb_rcv;
}

void
hn_dev_free_queues(struct rte_eth_dev *dev)
{
	unsigned int i;

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		struct hn_rx_queue *rxq = dev->data->rx_queues[i];

		hn_rx_queue_free(rxq, false);
		dev->data->rx_queues[i] = NULL;
	}
	dev->data->nb_rx_queues = 0;

	for (i = 0; i < dev->data->nb_tx_queues; i++) {
		hn_dev_tx_queue_release(dev, i);
		dev->data->tx_queues[i] = NULL;
	}
	dev->data->nb_tx_queues = 0;
}