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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2009-2018 Microsoft Corp.
 * Copyright (c) 2010-2012 Citrix Inc.
 * Copyright (c) 2012 NetApp Inc.
 * All rights reserved.
 */

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>

#include <rte_ethdev_driver.h>
#include <rte_ethdev.h>
#include <rte_string_fns.h>
#include <rte_memzone.h>
#include <rte_malloc.h>
#include <rte_atomic.h>
#include <rte_alarm.h>
#include <rte_branch_prediction.h>
#include <rte_ether.h>
#include <rte_common.h>
#include <rte_errno.h>
#include <rte_cycles.h>
#include <rte_memory.h>
#include <rte_eal.h>
#include <rte_dev.h>
#include <rte_bus_vmbus.h>

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

#define RNDIS_TIMEOUT_SEC 5
#define RNDIS_DELAY_MS    10

#define HN_RNDIS_XFER_SIZE		0x4000

#define HN_NDIS_TXCSUM_CAP_IP4		\
	(NDIS_TXCSUM_CAP_IP4 | NDIS_TXCSUM_CAP_IP4OPT)
#define HN_NDIS_TXCSUM_CAP_TCP4		\
	(NDIS_TXCSUM_CAP_TCP4 | NDIS_TXCSUM_CAP_TCP4OPT)
#define HN_NDIS_TXCSUM_CAP_TCP6		\
	(NDIS_TXCSUM_CAP_TCP6 | NDIS_TXCSUM_CAP_TCP6OPT | \
	 NDIS_TXCSUM_CAP_IP6EXT)
#define HN_NDIS_TXCSUM_CAP_UDP6		\
	(NDIS_TXCSUM_CAP_UDP6 | NDIS_TXCSUM_CAP_IP6EXT)
#define HN_NDIS_LSOV2_CAP_IP6		\
	(NDIS_LSOV2_CAP_IP6EXT | NDIS_LSOV2_CAP_TCP6OPT)

/* Get unique request id */
static inline uint32_t
hn_rndis_rid(struct hn_data *hv)
{
	uint32_t rid;

	do {
		rid = rte_atomic32_add_return(&hv->rndis_req_id, 1);
	} while (rid == 0);

	return rid;
}

static void *hn_rndis_alloc(size_t size)
{
	return rte_zmalloc("RNDIS", size, PAGE_SIZE);
}

#ifdef RTE_LIBRTE_NETVSC_DEBUG_DUMP
void hn_rndis_dump(const void *buf)
{
	const union {
		struct rndis_msghdr hdr;
		struct rndis_packet_msg pkt;
		struct rndis_init_req init_request;
		struct rndis_init_comp init_complete;
		struct rndis_halt_req halt;
		struct rndis_query_req query_request;
		struct rndis_query_comp query_complete;
		struct rndis_set_req set_request;
		struct rndis_set_comp set_complete;
		struct rndis_reset_req reset_request;
		struct rndis_reset_comp reset_complete;
		struct rndis_keepalive_req keepalive_request;
		struct rndis_keepalive_comp keepalive_complete;
		struct rndis_status_msg indicate_status;
	} *rndis_msg = buf;

	switch (rndis_msg->hdr.type) {
	case RNDIS_PACKET_MSG: {
		const struct rndis_pktinfo *ppi;
		unsigned int ppi_len;

		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_MSG_PACKET (len %u, data %u:%u, # oob %u %u:%u, pkt %u:%u)\n",
			    rndis_msg->pkt.len,
			    rndis_msg->pkt.dataoffset,
			    rndis_msg->pkt.datalen,
			    rndis_msg->pkt.oobdataelements,
			    rndis_msg->pkt.oobdataoffset,
			    rndis_msg->pkt.oobdatalen,
			    rndis_msg->pkt.pktinfooffset,
			    rndis_msg->pkt.pktinfolen);

		ppi = (const struct rndis_pktinfo *)
			((const char *)buf
			 + RNDIS_PACKET_MSG_OFFSET_ABS(rndis_msg->pkt.pktinfooffset));

		ppi_len = rndis_msg->pkt.pktinfolen;
		while (ppi_len > 0) {
			const void *ppi_data;

			ppi_data = ppi->data;

			rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
				"    PPI (size %u, type %u, offs %u data %#x)\n",
				ppi->size, ppi->type, ppi->offset,
				*(const uint32_t *)ppi_data);
			if (ppi->size == 0)
				break;
			ppi_len -= ppi->size;
			ppi = (const struct rndis_pktinfo *)
				((const char *)ppi + ppi->size);
		}
		break;
	}
	case RNDIS_INITIALIZE_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_MSG_INIT (len %u id %#x, ver %u.%u max xfer %u)\n",
			    rndis_msg->init_request.len,
			    rndis_msg->init_request.rid,
			    rndis_msg->init_request.ver_major,
			    rndis_msg->init_request.ver_minor,
			    rndis_msg->init_request.max_xfersz);
		break;

	case RNDIS_INITIALIZE_CMPLT:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_MSG_INIT_C (len %u, id %#x, status 0x%x, vers %u.%u, "
			    "flags %d, max xfer %u, max pkts %u, aligned %u)\n",
			    rndis_msg->init_complete.len,
			    rndis_msg->init_complete.rid,
			    rndis_msg->init_complete.status,
			    rndis_msg->init_complete.ver_major,
			    rndis_msg->init_complete.ver_minor,
			    rndis_msg->init_complete.devflags,
			    rndis_msg->init_complete.pktmaxsz,
			    rndis_msg->init_complete.pktmaxcnt,
			    rndis_msg->init_complete.align);
		break;

	case RNDIS_HALT_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_HALT (len %u id %#x)\n",
			    rndis_msg->halt.len, rndis_msg->halt.rid);
		break;

	case RNDIS_QUERY_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_QUERY (len %u, id %#x, oid %#x, info %u:%u)\n",
			    rndis_msg->query_request.len,
			    rndis_msg->query_request.rid,
			    rndis_msg->query_request.oid,
			    rndis_msg->query_request.infobuflen,
			    rndis_msg->query_request.infobufoffset);
		break;

	case RNDIS_QUERY_CMPLT:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_MSG_QUERY_C (len %u, id %#x, status 0x%x, buf %u:%u)\n",
			    rndis_msg->query_complete.len,
			    rndis_msg->query_complete.rid,
			    rndis_msg->query_complete.status,
			    rndis_msg->query_complete.infobuflen,
			    rndis_msg->query_complete.infobufoffset);
		break;

	case RNDIS_SET_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_SET (len %u, id %#x, oid %#x, info %u:%u)\n",
			    rndis_msg->set_request.len,
			    rndis_msg->set_request.rid,
			    rndis_msg->set_request.oid,
			    rndis_msg->set_request.infobuflen,
			    rndis_msg->set_request.infobufoffset);
		break;

	case RNDIS_SET_CMPLT:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
			    rndis_msg->set_complete.len,
			    rndis_msg->set_complete.rid,
			    rndis_msg->set_complete.status);
		break;

	case RNDIS_INDICATE_STATUS_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_MSG_INDICATE (len %u, status %#x, buf len %u, buf offset %u)\n",
			    rndis_msg->indicate_status.len,
			    rndis_msg->indicate_status.status,
			    rndis_msg->indicate_status.stbuflen,
			    rndis_msg->indicate_status.stbufoffset);
		break;

	case RNDIS_RESET_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_RESET (len %u, id %#x)\n",
			    rndis_msg->reset_request.len,
			    rndis_msg->reset_request.rid);
		break;

	case RNDIS_RESET_CMPLT:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_RESET_C (len %u, status %#x address %#x)\n",
			    rndis_msg->reset_complete.len,
			    rndis_msg->reset_complete.status,
			    rndis_msg->reset_complete.adrreset);
		break;

	case RNDIS_KEEPALIVE_MSG:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_KEEPALIVE (len %u, id %#x)\n",
			    rndis_msg->keepalive_request.len,
			    rndis_msg->keepalive_request.rid);
		break;

	case RNDIS_KEEPALIVE_CMPLT:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS_KEEPALIVE_C (len %u, id %#x address %#x)\n",
			    rndis_msg->keepalive_complete.len,
			    rndis_msg->keepalive_complete.rid,
			    rndis_msg->keepalive_complete.status);
		break;

	default:
		rte_log(RTE_LOG_DEBUG, hn_logtype_driver,
			    "RNDIS type %#x len %u\n",
			    rndis_msg->hdr.type,
			    rndis_msg->hdr.len);
		break;
	}
}
#endif

static int hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
				  const void *req, uint32_t reqlen)

{
	struct hn_nvs_rndis nvs_rndis = {
		.type = NVS_TYPE_RNDIS,
		.rndis_mtype = NVS_RNDIS_MTYPE_CTRL,
		.chim_idx = NVS_CHIM_IDX_INVALID,
		.chim_sz = 0
	};
	struct vmbus_gpa sg;
	rte_iova_t addr;

	addr = rte_malloc_virt2iova(req);
	if (unlikely(addr == RTE_BAD_IOVA)) {
		PMD_DRV_LOG(ERR, "RNDIS send request can not get iova");
		return -EINVAL;
	}

	if (unlikely(reqlen > PAGE_SIZE)) {
		PMD_DRV_LOG(ERR, "RNDIS request %u greater than page size",
			    reqlen);
		return -EINVAL;
	}

	sg.page = addr / PAGE_SIZE;
	sg.ofs  = addr & PAGE_MASK;
	sg.len  = reqlen;

	if (sg.ofs + reqlen >  PAGE_SIZE) {
		PMD_DRV_LOG(ERR, "RNDIS request crosses page bounary");
		return -EINVAL;
	}

	hn_rndis_dump(req);

	return hn_nvs_send_sglist(chan, &sg, 1,
				  &nvs_rndis, sizeof(nvs_rndis), 0U, NULL);
}

/*
 * Alarm callback to process link changed notifications.
 * Can not directly since link_status is discovered while reading ring
 */
static void hn_rndis_link_alarm(void *arg)
{
	_rte_eth_dev_callback_process(arg, RTE_ETH_EVENT_INTR_LSC, NULL);
}

void hn_rndis_link_status(struct rte_eth_dev *dev, const void *msg)
{
	const struct rndis_status_msg *indicate = msg;

	hn_rndis_dump(msg);

	PMD_DRV_LOG(DEBUG, "link status %#x", indicate->status);

	switch (indicate->status) {
	case RNDIS_STATUS_NETWORK_CHANGE:
	case RNDIS_STATUS_TASK_OFFLOAD_CURRENT_CONFIG:
		/* ignore not in DPDK API */
		break;

	case RNDIS_STATUS_LINK_SPEED_CHANGE:
	case RNDIS_STATUS_MEDIA_CONNECT:
	case RNDIS_STATUS_MEDIA_DISCONNECT:
		if (dev->data->dev_conf.intr_conf.lsc)
			rte_eal_alarm_set(10, hn_rndis_link_alarm, dev);
		break;
	default:
		PMD_DRV_LOG(NOTICE, "unknown RNDIS indication: %#x",
			    indicate->status);
	}
}

/* Callback from hn_process_events when response is visible */
void hn_rndis_receive_response(struct hn_data *hv,
			       const void *data, uint32_t len)
{
	const struct rndis_init_comp *hdr = data;

	hn_rndis_dump(data);

	if (len < sizeof(3 * sizeof(uint32_t))) {
		PMD_DRV_LOG(ERR,
			    "missing RNDIS header %u", len);
		return;
	}

	if (len < hdr->len) {
		PMD_DRV_LOG(ERR,
			    "truncated RNDIS response %u", len);
		return;
	}

	if  (len > sizeof(hv->rndis_resp)) {
		PMD_DRV_LOG(NOTICE,
			    "RNDIS response exceeds buffer");
		len = sizeof(hv->rndis_resp);
	}

	if (hdr->rid == 0) {
		PMD_DRV_LOG(NOTICE,
			    "RNDIS response id zero!");
	}

	memcpy(hv->rndis_resp, data, len);

	/* make sure response copied before update */
	rte_smp_wmb();

	if (rte_atomic32_cmpset(&hv->rndis_pending, hdr->rid, 0) == 0) {
		PMD_DRV_LOG(NOTICE,
			    "received id %#x pending id %#x",
			    hdr->rid, (uint32_t)hv->rndis_pending);
	}
}

/* Do request/response transaction */
static int hn_rndis_exec1(struct hn_data *hv,
			  const void *req, uint32_t reqlen,
			  void *comp, uint32_t comp_len)
{
	const struct rndis_halt_req *hdr = req;
	uint32_t rid = hdr->rid;
	struct vmbus_channel *chan = hn_primary_chan(hv);
	int error;

	if (comp_len > sizeof(hv->rndis_resp)) {
		PMD_DRV_LOG(ERR,
			    "Expected completion size %u exceeds buffer %zu",
			    comp_len, sizeof(hv->rndis_resp));
		return -EIO;
	}

	if (rid == 0) {
		PMD_DRV_LOG(ERR, "Invalid request id");
		return -EINVAL;
	}

	if (comp != NULL &&
	    rte_atomic32_cmpset(&hv->rndis_pending, 0, rid) == 0) {
		PMD_DRV_LOG(ERR,
			    "Request already pending");
		return -EBUSY;
	}

	error = hn_nvs_send_rndis_ctrl(chan, req, reqlen);
	if (error) {
		PMD_DRV_LOG(ERR, "RNDIS ctrl send failed: %d", error);
		return error;
	}

	if (comp) {
		time_t start = time(NULL);

		/* Poll primary channel until response received */
		while (hv->rndis_pending == rid) {
			if (hv->closed)
				return -ENETDOWN;

			if (time(NULL) - start > RNDIS_TIMEOUT_SEC) {
				PMD_DRV_LOG(ERR,
					    "RNDIS response timed out");

				rte_atomic32_cmpset(&hv->rndis_pending, rid, 0);
				return -ETIMEDOUT;
			}

			if (rte_vmbus_chan_rx_empty(hv->primary->chan))
				rte_delay_ms(RNDIS_DELAY_MS);

			hn_process_events(hv, 0, 1);
		}

		memcpy(comp, hv->rndis_resp, comp_len);
	}

	return 0;
}

/* Do transaction and validate response */
static int hn_rndis_execute(struct hn_data *hv, uint32_t rid,
			    const void *req, uint32_t reqlen,
			    void *comp, uint32_t comp_len, uint32_t comp_type)
{
	const struct rndis_comp_hdr *hdr = comp;
	int ret;

	memset(comp, 0, comp_len);

	ret = hn_rndis_exec1(hv, req, reqlen, comp, comp_len);
	if (ret < 0)
		return ret;
	/*
	 * Check this RNDIS complete message.
	 */
	if (unlikely(hdr->type != comp_type)) {
		PMD_DRV_LOG(ERR,
			    "unexpected RNDIS response complete %#x expect %#x",
			    hdr->type, comp_type);

		return -ENXIO;
	}
	if (unlikely(hdr->rid != rid)) {
		PMD_DRV_LOG(ERR,
			    "RNDIS comp rid mismatch %#x, expect %#x",
			    hdr->rid, rid);
		return -EINVAL;
	}

	/* All pass! */
	return 0;
}

static int
hn_rndis_query(struct hn_data *hv, uint32_t oid,
	       const void *idata, uint32_t idlen,
	       void *odata, uint32_t odlen)
{
	struct rndis_query_req *req;
	struct rndis_query_comp *comp;
	uint32_t reqlen, comp_len;
	int error = -EIO;
	unsigned int ofs;
	uint32_t rid;

	reqlen = sizeof(*req) + idlen;
	req = hn_rndis_alloc(reqlen);
	if (req == NULL)
		return -ENOMEM;

	comp_len = sizeof(*comp) + odlen;
	comp = rte_zmalloc("QUERY", comp_len, PAGE_SIZE);
	if (!comp) {
		error = -ENOMEM;
		goto done;
	}
	comp->status = RNDIS_STATUS_PENDING;

	rid = hn_rndis_rid(hv);

	req->type = RNDIS_QUERY_MSG;
	req->len = reqlen;
	req->rid = rid;
	req->oid = oid;
	req->infobufoffset = RNDIS_QUERY_REQ_INFOBUFOFFSET;
	req->infobuflen = idlen;

	/* Input data immediately follows RNDIS query. */
	memcpy(req + 1, idata, idlen);

	error = hn_rndis_execute(hv, rid, req, reqlen,
				 comp, comp_len, RNDIS_QUERY_CMPLT);

	if (error)
		goto done;

	if (comp->status != RNDIS_STATUS_SUCCESS) {
		PMD_DRV_LOG(ERR, "RNDIS query 0x%08x failed: status 0x%08x",
			    oid, comp->status);
		error = -EINVAL;
		goto done;
	}

	if (comp->infobuflen == 0 || comp->infobufoffset == 0) {
		/* No output data! */
		PMD_DRV_LOG(ERR, "RNDIS query 0x%08x, no data", oid);
		error = 0;
		goto done;
	}

	/*
	 * Check output data length and offset.
	 */
	/* ofs is the offset from the beginning of comp. */
	ofs = RNDIS_QUERY_COMP_INFOBUFOFFSET_ABS(comp->infobufoffset);
	if (ofs < sizeof(*comp) || ofs + comp->infobuflen > comp_len) {
		PMD_DRV_LOG(ERR, "RNDIS query invalid comp ib off/len, %u/%u",
			    comp->infobufoffset, comp->infobuflen);
		error = -EINVAL;
		goto done;
	}

	/* Save output data. */
	if (comp->infobuflen < odlen)
		odlen = comp->infobuflen;

	/* ofs is the offset from the beginning of comp. */
	memcpy(odata, (const char *)comp + ofs, odlen);

	error = 0;
done:
	rte_free(comp);
	rte_free(req);
	return error;
}

static int
hn_rndis_halt(struct hn_data *hv)
{
	struct rndis_halt_req *halt;

	halt = hn_rndis_alloc(sizeof(*halt));
	if (halt == NULL)
		return -ENOMEM;

	halt->type = RNDIS_HALT_MSG;
	halt->len = sizeof(*halt);
	halt->rid = hn_rndis_rid(hv);

	/* No RNDIS completion; rely on NVS message send completion */
	hn_rndis_exec1(hv, halt, sizeof(*halt), NULL, 0);

	rte_free(halt);

	PMD_INIT_LOG(DEBUG, "RNDIS halt done");
	return 0;
}

static int
hn_rndis_query_hwcaps(struct hn_data *hv, struct ndis_offload *caps)
{
	struct ndis_offload in;
	uint32_t caps_len, size;
	int error;

	memset(caps, 0, sizeof(*caps));
	memset(&in, 0, sizeof(in));
	in.ndis_hdr.ndis_type = NDIS_OBJTYPE_OFFLOAD;

	if (hv->ndis_ver >= NDIS_VERSION_6_30) {
		in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_3;
		size = NDIS_OFFLOAD_SIZE;
	} else if (hv->ndis_ver >= NDIS_VERSION_6_1) {
		in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_2;
		size = NDIS_OFFLOAD_SIZE_6_1;
	} else {
		in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_1;
		size = NDIS_OFFLOAD_SIZE_6_0;
	}
	in.ndis_hdr.ndis_size = size;

	caps_len = NDIS_OFFLOAD_SIZE;
	error = hn_rndis_query(hv, OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
			       &in, size, caps, caps_len);
	if (error)
		return error;

	/* Preliminary verification. */
	if (caps->ndis_hdr.ndis_type != NDIS_OBJTYPE_OFFLOAD) {
		PMD_DRV_LOG(NOTICE, "invalid NDIS objtype 0x%02x",
			    caps->ndis_hdr.ndis_type);
		return -EINVAL;
	}
	if (caps->ndis_hdr.ndis_rev < NDIS_OFFLOAD_REV_1) {
		PMD_DRV_LOG(NOTICE, "invalid NDIS objrev 0x%02x",
			    caps->ndis_hdr.ndis_rev);
		return -EINVAL;
	}
	if (caps->ndis_hdr.ndis_size > caps_len) {
		PMD_DRV_LOG(NOTICE, "invalid NDIS objsize %u, data size %u",
			    caps->ndis_hdr.ndis_size, caps_len);
		return -EINVAL;
	} else if (caps->ndis_hdr.ndis_size < NDIS_OFFLOAD_SIZE_6_0) {
		PMD_DRV_LOG(NOTICE, "invalid NDIS objsize %u",
			    caps->ndis_hdr.ndis_size);
		return -EINVAL;
	}

	return 0;
}

int
hn_rndis_query_rsscaps(struct hn_data *hv,
		       unsigned int *rxr_cnt0)
{
	struct ndis_rss_caps in, caps;
	unsigned int indsz, rxr_cnt;
	uint32_t caps_len;
	int error;

	*rxr_cnt0 = 0;

	if (hv->ndis_ver < NDIS_VERSION_6_20) {
		PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
		return -EOPNOTSUPP;
	}

	memset(&in, 0, sizeof(in));
	in.ndis_hdr.ndis_type = NDIS_OBJTYPE_RSS_CAPS;
	in.ndis_hdr.ndis_rev = NDIS_RSS_CAPS_REV_2;
	in.ndis_hdr.ndis_size = NDIS_RSS_CAPS_SIZE;

	caps_len = NDIS_RSS_CAPS_SIZE;
	error = hn_rndis_query(hv, OID_GEN_RECEIVE_SCALE_CAPABILITIES,
			       &in, NDIS_RSS_CAPS_SIZE,
			       &caps, caps_len);
	if (error)
		return error;

	PMD_INIT_LOG(DEBUG, "RX rings %u indirect %u caps %#x",
		     caps.ndis_nrxr, caps.ndis_nind, caps.ndis_caps);
	/*
	 * Preliminary verification.
	 */
	if (caps.ndis_hdr.ndis_type != NDIS_OBJTYPE_RSS_CAPS) {
		PMD_DRV_LOG(ERR, "invalid NDIS objtype 0x%02x",
			    caps.ndis_hdr.ndis_type);
		return -EINVAL;
	}
	if (caps.ndis_hdr.ndis_rev < NDIS_RSS_CAPS_REV_1) {
		PMD_DRV_LOG(ERR, "invalid NDIS objrev 0x%02x",
			    caps.ndis_hdr.ndis_rev);
		return -EINVAL;
	}
	if (caps.ndis_hdr.ndis_size > caps_len) {
		PMD_DRV_LOG(ERR,
			    "invalid NDIS objsize %u, data size %u",
			    caps.ndis_hdr.ndis_size, caps_len);
		return -EINVAL;
	} else if (caps.ndis_hdr.ndis_size < NDIS_RSS_CAPS_SIZE_6_0) {
		PMD_DRV_LOG(ERR, "invalid NDIS objsize %u",
			    caps.ndis_hdr.ndis_size);
		return -EINVAL;
	}

	/*
	 * Save information for later RSS configuration.
	 */
	if (caps.ndis_nrxr == 0) {
		PMD_DRV_LOG(ERR, "0 RX rings!?");
		return -EINVAL;
	}
	rxr_cnt = caps.ndis_nrxr;

	if (caps.ndis_hdr.ndis_size == NDIS_RSS_CAPS_SIZE &&
	    caps.ndis_hdr.ndis_rev >= NDIS_RSS_CAPS_REV_2) {
		if (caps.ndis_nind > NDIS_HASH_INDCNT) {
			PMD_DRV_LOG(ERR,
				    "too many RSS indirect table entries %u",
				    caps.ndis_nind);
			return -EOPNOTSUPP;
		}
		if (!rte_is_power_of_2(caps.ndis_nind)) {
			PMD_DRV_LOG(ERR,
				    "RSS indirect table size is not power-of-2 %u",
				    caps.ndis_nind);
		}

		indsz = caps.ndis_nind;
	} else {
		indsz = NDIS_HASH_INDCNT;
	}

	if (indsz < rxr_cnt) {
		PMD_DRV_LOG(NOTICE,
			    "# of RX rings (%d) > RSS indirect table size %d",
			    rxr_cnt, indsz);
		rxr_cnt = indsz;
	}

	hv->rss_offloads = 0;
	if (caps.ndis_caps & NDIS_RSS_CAP_IPV4)
		hv->rss_offloads |= ETH_RSS_IPV4
			| ETH_RSS_NONFRAG_IPV4_TCP
			| ETH_RSS_NONFRAG_IPV4_UDP;
	if (caps.ndis_caps & NDIS_RSS_CAP_IPV6)
		hv->rss_offloads |= ETH_RSS_IPV6
			| ETH_RSS_NONFRAG_IPV6_TCP;
	if (caps.ndis_caps & NDIS_RSS_CAP_IPV6_EX)
		hv->rss_offloads |= ETH_RSS_IPV6_EX
			| ETH_RSS_IPV6_TCP_EX;

	/* Commit! */
	*rxr_cnt0 = rxr_cnt;

	return 0;
}

static int
hn_rndis_set(struct hn_data *hv, uint32_t oid, const void *data, uint32_t dlen)
{
	struct rndis_set_req *req;
	struct rndis_set_comp comp;
	uint32_t reqlen, comp_len;
	uint32_t rid;
	int error;

	reqlen = sizeof(*req) + dlen;
	req = rte_zmalloc("RNDIS_SET", reqlen, PAGE_SIZE);
	if (!req)
		return -ENOMEM;

	rid = hn_rndis_rid(hv);
	req->type = RNDIS_SET_MSG;
	req->len = reqlen;
	req->rid = rid;
	req->oid = oid;
	req->infobuflen = dlen;
	req->infobufoffset = RNDIS_SET_REQ_INFOBUFOFFSET;

	/* Data immediately follows RNDIS set. */
	memcpy(req + 1, data, dlen);

	comp_len = sizeof(comp);
	error = hn_rndis_execute(hv, rid, req, reqlen,
				 &comp, comp_len,
				 RNDIS_SET_CMPLT);
	if (error) {
		PMD_DRV_LOG(ERR, "exec RNDIS set %#" PRIx32 " failed",
			    oid);
		error = EIO;
		goto done;
	}

	if (comp.status != RNDIS_STATUS_SUCCESS) {
		PMD_DRV_LOG(ERR,
			    "RNDIS set %#" PRIx32 " failed: status %#" PRIx32,
			    oid, comp.status);
		error = EIO;
		goto done;
	}

done:
	rte_free(req);
	return error;
}

int hn_rndis_conf_offload(struct hn_data *hv,
			  uint64_t tx_offloads, uint64_t rx_offloads)
{
	struct ndis_offload_params params;
	struct ndis_offload hwcaps;
	int error;

	error = hn_rndis_query_hwcaps(hv, &hwcaps);
	if (error) {
		PMD_DRV_LOG(ERR, "hwcaps query failed: %d", error);
		return error;
	}

	/* NOTE: 0 means "no change" */
	memset(&params, 0, sizeof(params));

	params.ndis_hdr.ndis_type = NDIS_OBJTYPE_DEFAULT;
	if (hv->ndis_ver < NDIS_VERSION_6_30) {
		params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_2;
		params.ndis_hdr.ndis_size = NDIS_OFFLOAD_PARAMS_SIZE_6_1;
	} else {
		params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_3;
		params.ndis_hdr.ndis_size = NDIS_OFFLOAD_PARAMS_SIZE;
	}

	if (tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM) {
		if (hwcaps.ndis_csum.ndis_ip4_txcsum & NDIS_TXCSUM_CAP_TCP4)
			params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TX;
		else
			goto unsupported;

		if (hwcaps.ndis_csum.ndis_ip6_txcsum & NDIS_TXCSUM_CAP_TCP6)
			params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TX;
		else
			goto unsupported;
	}

	if (rx_offloads & DEV_RX_OFFLOAD_TCP_CKSUM) {
		if ((hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_TCP4)
		    == NDIS_RXCSUM_CAP_TCP4)
			params.ndis_tcp4csum |= NDIS_OFFLOAD_PARAM_RX;
		else
			goto unsupported;

		if ((hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_TCP6)
		    == NDIS_RXCSUM_CAP_TCP6)
			params.ndis_tcp6csum |= NDIS_OFFLOAD_PARAM_RX;
		else
			goto unsupported;
	}

	if (tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM) {
		if (hwcaps.ndis_csum.ndis_ip4_txcsum & NDIS_TXCSUM_CAP_UDP4)
			params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TX;
		else
			goto unsupported;

		if ((hwcaps.ndis_csum.ndis_ip6_txcsum & NDIS_TXCSUM_CAP_UDP6)
		    == NDIS_TXCSUM_CAP_UDP6)
			params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TX;
		else
			goto unsupported;
	}

	if (rx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM) {
		if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_UDP4)
			params.ndis_udp4csum |= NDIS_OFFLOAD_PARAM_RX;
		else
			goto unsupported;

		if (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_UDP6)
			params.ndis_udp6csum |= NDIS_OFFLOAD_PARAM_RX;
		else
			goto unsupported;
	}

	if (tx_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM) {
		if ((hwcaps.ndis_csum.ndis_ip4_txcsum & NDIS_TXCSUM_CAP_IP4)
		    == NDIS_TXCSUM_CAP_IP4)
			params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TX;
		else
			goto unsupported;
	}
	if (rx_offloads & DEV_RX_OFFLOAD_IPV4_CKSUM) {
		if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_IP4)
			params.ndis_ip4csum |= NDIS_OFFLOAD_PARAM_RX;
		else
			goto unsupported;
	}

	if (tx_offloads & DEV_TX_OFFLOAD_TCP_TSO) {
		if (hwcaps.ndis_lsov2.ndis_ip4_encap & NDIS_OFFLOAD_ENCAP_8023)
			params.ndis_lsov2_ip4 = NDIS_OFFLOAD_LSOV2_ON;
		else
			goto unsupported;

		if ((hwcaps.ndis_lsov2.ndis_ip6_opts & HN_NDIS_LSOV2_CAP_IP6)
		    == HN_NDIS_LSOV2_CAP_IP6)
			params.ndis_lsov2_ip6 = NDIS_OFFLOAD_LSOV2_ON;
		else
			goto unsupported;
	}

	error = hn_rndis_set(hv, OID_TCP_OFFLOAD_PARAMETERS, &params,
			     params.ndis_hdr.ndis_size);
	if (error) {
		PMD_DRV_LOG(ERR, "offload config failed");
		return error;
	}

	return 0;
 unsupported:
	PMD_DRV_LOG(NOTICE,
		    "offload tx:%" PRIx64 " rx:%" PRIx64 " not supported by this version",
		    tx_offloads, rx_offloads);
	return -EINVAL;
}

int hn_rndis_get_offload(struct hn_data *hv,
			 struct rte_eth_dev_info *dev_info)
{
	struct ndis_offload hwcaps;
	int error;

	memset(&hwcaps, 0, sizeof(hwcaps));

	error = hn_rndis_query_hwcaps(hv, &hwcaps);
	if (error) {
		PMD_DRV_LOG(ERR, "hwcaps query failed: %d", error);
		return error;
	}

	dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MULTI_SEGS |
				    DEV_TX_OFFLOAD_VLAN_INSERT;

	if ((hwcaps.ndis_csum.ndis_ip4_txcsum & HN_NDIS_TXCSUM_CAP_IP4)
	    == HN_NDIS_TXCSUM_CAP_IP4)
		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_IPV4_CKSUM;

	if ((hwcaps.ndis_csum.ndis_ip4_txcsum & HN_NDIS_TXCSUM_CAP_TCP4)
	    == HN_NDIS_TXCSUM_CAP_TCP4 &&
	    (hwcaps.ndis_csum.ndis_ip6_txcsum & HN_NDIS_TXCSUM_CAP_TCP6)
	    == HN_NDIS_TXCSUM_CAP_TCP6)
		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_CKSUM;

	if ((hwcaps.ndis_csum.ndis_ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) &&
	    (hwcaps.ndis_csum.ndis_ip6_txcsum & NDIS_TXCSUM_CAP_UDP6))
		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_UDP_CKSUM;

	if ((hwcaps.ndis_lsov2.ndis_ip4_encap & NDIS_OFFLOAD_ENCAP_8023) &&
	    (hwcaps.ndis_lsov2.ndis_ip6_opts & HN_NDIS_LSOV2_CAP_IP6)
	    == HN_NDIS_LSOV2_CAP_IP6)
		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;

	dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP |
				    DEV_RX_OFFLOAD_RSS_HASH;

	if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_IP4)
		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_IPV4_CKSUM;

	if ((hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_TCP4) &&
	    (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_TCP6))
		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_CKSUM;

	if ((hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_UDP4) &&
	    (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_UDP6))
		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_UDP_CKSUM;

	return 0;
}

uint32_t
hn_rndis_get_ptypes(struct hn_data *hv)
{
	struct ndis_offload hwcaps;
	uint32_t ptypes;
	int error;

	memset(&hwcaps, 0, sizeof(hwcaps));

	error = hn_rndis_query_hwcaps(hv, &hwcaps);
	if (error) {
		PMD_DRV_LOG(ERR, "hwcaps query failed: %d", error);
		return RTE_PTYPE_L2_ETHER;
	}

	ptypes = RTE_PTYPE_L2_ETHER;

	if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_IP4)
		ptypes |= RTE_PTYPE_L3_IPV4;

	if ((hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_TCP4) ||
	    (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_TCP6))
		ptypes |= RTE_PTYPE_L4_TCP;

	if ((hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_UDP4) ||
	    (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_UDP6))
		ptypes |= RTE_PTYPE_L4_UDP;

	return ptypes;
}

int
hn_rndis_set_rxfilter(struct hn_data *hv, uint32_t filter)
{
	int error;

	error = hn_rndis_set(hv, OID_GEN_CURRENT_PACKET_FILTER,
			     &filter, sizeof(filter));
	if (error) {
		PMD_DRV_LOG(ERR, "set RX filter %#" PRIx32 " failed: %d",
			    filter, error);
	} else {
		PMD_DRV_LOG(DEBUG, "set RX filter %#" PRIx32 " done", filter);
	}

	return error;
}

int hn_rndis_conf_rss(struct hn_data *hv, uint32_t flags)
{
	struct ndis_rssprm_toeplitz rssp;
	struct ndis_rss_params *prm = &rssp.rss_params;
	unsigned int i;
	int error;

	memset(&rssp, 0, sizeof(rssp));

	prm->ndis_hdr.ndis_type = NDIS_OBJTYPE_RSS_PARAMS;
	prm->ndis_hdr.ndis_rev = NDIS_RSS_PARAMS_REV_2;
	prm->ndis_hdr.ndis_size = sizeof(*prm);
	prm->ndis_flags = flags;
	prm->ndis_hash = hv->rss_hash;
	prm->ndis_indsize = sizeof(rssp.rss_ind[0]) * NDIS_HASH_INDCNT;
	prm->ndis_indoffset = offsetof(struct ndis_rssprm_toeplitz, rss_ind[0]);
	prm->ndis_keysize = NDIS_HASH_KEYSIZE_TOEPLITZ;
	prm->ndis_keyoffset = offsetof(struct ndis_rssprm_toeplitz, rss_key[0]);

	for (i = 0; i < NDIS_HASH_INDCNT; i++)
		rssp.rss_ind[i] = hv->rss_ind[i];

	/* Set hask key values */
	memcpy(&rssp.rss_key, hv->rss_key, NDIS_HASH_KEYSIZE_TOEPLITZ);

	error = hn_rndis_set(hv, OID_GEN_RECEIVE_SCALE_PARAMETERS,
			     &rssp, sizeof(rssp));
	if (error != 0) {
		PMD_DRV_LOG(ERR,
			    "RSS config num queues=%u failed: %d",
			    hv->num_queues, error);
	}
	return error;
}

static int hn_rndis_init(struct hn_data *hv)
{
	struct rndis_init_req *req;
	struct rndis_init_comp comp;
	uint32_t comp_len, rid;
	int error;

	req = hn_rndis_alloc(sizeof(*req));
	if (!req) {
		PMD_DRV_LOG(ERR, "no memory for RNDIS init");
		return -ENXIO;
	}

	rid = hn_rndis_rid(hv);
	req->type = RNDIS_INITIALIZE_MSG;
	req->len = sizeof(*req);
	req->rid = rid;
	req->ver_major = RNDIS_VERSION_MAJOR;
	req->ver_minor = RNDIS_VERSION_MINOR;
	req->max_xfersz = HN_RNDIS_XFER_SIZE;

	comp_len = RNDIS_INIT_COMP_SIZE_MIN;
	error = hn_rndis_execute(hv, rid, req, sizeof(*req),
				 &comp, comp_len,
				 RNDIS_INITIALIZE_CMPLT);
	if (error)
		goto done;

	if (comp.status != RNDIS_STATUS_SUCCESS) {
		PMD_DRV_LOG(ERR, "RNDIS init failed: status 0x%08x",
			    comp.status);
		error = -EIO;
		goto done;
	}

	hv->rndis_agg_size = comp.pktmaxsz;
	hv->rndis_agg_pkts = comp.pktmaxcnt;
	hv->rndis_agg_align = 1U << comp.align;

	if (hv->rndis_agg_align < sizeof(uint32_t)) {
		/*
		 * The RNDIS packet message encap assumes that the RNDIS
		 * packet message is at least 4 bytes aligned.  Fix up the
		 * alignment here, if the remote side sets the alignment
		 * too low.
		 */
		PMD_DRV_LOG(NOTICE,
			    "fixup RNDIS aggpkt align: %u -> %zu",
			    hv->rndis_agg_align, sizeof(uint32_t));
		hv->rndis_agg_align = sizeof(uint32_t);
	}

	PMD_INIT_LOG(INFO,
		     "RNDIS ver %u.%u, aggpkt size %u, aggpkt cnt %u, aggpkt align %u",
		     comp.ver_major, comp.ver_minor,
		     hv->rndis_agg_size, hv->rndis_agg_pkts,
		     hv->rndis_agg_align);
	error = 0;
done:
	rte_free(req);
	return error;
}

int
hn_rndis_get_eaddr(struct hn_data *hv, uint8_t *eaddr)
{
	uint32_t eaddr_len;
	int error;

	eaddr_len = RTE_ETHER_ADDR_LEN;
	error = hn_rndis_query(hv, OID_802_3_PERMANENT_ADDRESS, NULL, 0,
			       eaddr, eaddr_len);
	if (error)
		return error;

	PMD_DRV_LOG(INFO, "MAC address %02x:%02x:%02x:%02x:%02x:%02x",
		    eaddr[0], eaddr[1], eaddr[2],
		    eaddr[3], eaddr[4], eaddr[5]);
	return 0;
}

int
hn_rndis_get_linkstatus(struct hn_data *hv)
{
	return hn_rndis_query(hv, OID_GEN_MEDIA_CONNECT_STATUS, NULL, 0,
			      &hv->link_status, sizeof(uint32_t));
}

int
hn_rndis_get_linkspeed(struct hn_data *hv)
{
	return hn_rndis_query(hv, OID_GEN_LINK_SPEED, NULL, 0,
			      &hv->link_speed, sizeof(uint32_t));
}

int
hn_rndis_attach(struct hn_data *hv)
{
	/* Initialize RNDIS. */
	return hn_rndis_init(hv);
}

void
hn_rndis_detach(struct hn_data *hv)
{
	struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];

	rte_eal_alarm_cancel(hn_rndis_link_alarm, dev);

	/* Halt the RNDIS. */
	hn_rndis_halt(hv);
}