aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/iavf/iavf_vc_common.c
blob: 5f39f0334e83cffa4253e967f9ef0b37c9975f18 (plain) (blame)
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
/* SPDX-License-Identifier: BSD-3-Clause */
/*  Copyright (c) 2021, Intel Corporation
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 *   3. Neither the name of the Intel Corporation nor the names of its
 *      contributors may be used to endorse or promote products derived from
 *      this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file iavf_vc_common.c
 * @brief Common virtchnl interface functions
 *
 * Contains functions implementing the virtchnl interface for connecting to
 * the PF driver. This file contains the functions which are common between
 * the legacy and iflib driver implementations.
 */
#include "iavf_vc_common.h"

/* Static function decls */
static void iavf_handle_link_event(struct iavf_sc *sc,
    struct virtchnl_pf_event *vpe);

/**
 * iavf_send_pf_msg - Send virtchnl message to PF device
 * @sc: device softc
 * @op: the op to send
 * @msg: message contents
 * @len: length of the message
 *
 * Send a message to the PF device over the virtchnl connection. Print
 * a status code if the message reports an error.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_send_pf_msg(struct iavf_sc *sc,
	enum virtchnl_ops op, u8 *msg, u16 len)
{
	struct iavf_hw *hw = &sc->hw;
	device_t dev = sc->dev;
	enum iavf_status status;
	int val_err;

	/* Validating message before sending it to the PF */
	val_err = virtchnl_vc_validate_vf_msg(&sc->version, op, msg, len);
	if (val_err)
		device_printf(dev, "Error validating msg to PF for op %d,"
		    " msglen %d: error %d\n", op, len, val_err);

	if (!iavf_check_asq_alive(hw)) {
		if (op != VIRTCHNL_OP_GET_STATS)
			device_printf(dev, "Unable to send opcode %s to PF, "
			    "ASQ is not alive\n", iavf_vc_opcode_str(op));
		return (IAVF_ERR_ADMIN_QUEUE_ERROR);
	}

	if (op != VIRTCHNL_OP_GET_STATS)
		iavf_dbg_vc(sc,
		    "Sending msg (op=%s[%d]) to PF\n",
		    iavf_vc_opcode_str(op), op);

	status = iavf_aq_send_msg_to_pf(hw, op, IAVF_SUCCESS, msg, len, NULL);
	if (status && op != VIRTCHNL_OP_GET_STATS)
		device_printf(dev, "Unable to send opcode %s to PF, "
		    "status %s, aq error %s\n",
		    iavf_vc_opcode_str(op),
		    iavf_stat_str(hw, status),
		    iavf_aq_str(hw, hw->aq.asq_last_status));

	return (status);
}

/**
 * iavf_send_api_ver - Send the API version we support to the PF
 * @sc: device softc
 *
 * Send API version admin queue message to the PF. The reply is not checked
 * in this function.
 *
 * @returns 0 if the message was successfully sent, or one of the
 * IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
 */
int
iavf_send_api_ver(struct iavf_sc *sc)
{
	struct virtchnl_version_info vvi;

	vvi.major = VIRTCHNL_VERSION_MAJOR;
	vvi.minor = VIRTCHNL_VERSION_MINOR;

	return iavf_send_pf_msg(sc, VIRTCHNL_OP_VERSION,
	    (u8 *)&vvi, sizeof(vvi));
}

/**
 * iavf_verify_api_ver - Verify the PF supports our API version
 * @sc: device softc
 *
 * Compare API versions with the PF. Must be called after admin queue is
 * initialized.
 *
 * @returns 0 if API versions match, ETIMEDOUT if the PF does not reply,
 * or EIO if the versions do not match.
 */
int
iavf_verify_api_ver(struct iavf_sc *sc)
{
	int error;

	error = iavf_verify_api_ver_retries(sc, IAVF_AQ_MAX_ERR);
	if (error == 0)
		device_printf(sc->dev, "PF API %d.%d / VF API %d.%d\n",
		    sc->version.major, sc->version.minor,
		    VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR);
	return (error);
}

/**
 * iavf_verify_api_ver_retries - Verify the PF API version with a retry limit
 * @sc: device softc
 * @max_retries: maximum number of Admin Receive Queue polls
 *
 * @returns 0 if API versions match, ETIMEDOUT if the PF does not reply,
 * or EIO for an invalid response or API version.
 */
int
iavf_verify_api_ver_retries(struct iavf_sc *sc, u32 max_retries)
{
	struct virtchnl_version_info *pf_vvi;
	struct iavf_hw *hw = &sc->hw;
	struct iavf_arq_event_info event;
	enum iavf_status status;
	device_t dev = sc->dev;
	int error = 0;
	u32 retries = 0;

	event.buf_len = IAVF_AQ_BUF_SZ;
	event.msg_buf = (u8 *)malloc(event.buf_len, M_IAVF, M_WAITOK);

	for (;;) {
		if (++retries > max_retries) {
			error = ETIMEDOUT;
			goto out_alloc;
		}

		/* Initial delay here is necessary */
		iavf_msec_pause(100);
		status = iavf_clean_arq_element(hw, &event, NULL);
		if (status == IAVF_ERR_ADMIN_QUEUE_NO_WORK)
			continue;
		else if (status) {
			error = EIO;
			goto out_alloc;
		}

		if ((enum virtchnl_ops)le32toh(event.desc.cookie_high) !=
		    VIRTCHNL_OP_VERSION) {
			iavf_dbg_vc(sc, "%s: Received unexpected op response: %d\n",
			    __func__, le32toh(event.desc.cookie_high));
			/* Don't stop looking for expected response */
			continue;
		}

		status = (enum iavf_status)le32toh(event.desc.cookie_low);
		if (status) {
			error = EIO;
			goto out_alloc;
		} else
			break;
	}

	pf_vvi = (struct virtchnl_version_info *)event.msg_buf;
	if ((pf_vvi->major > VIRTCHNL_VERSION_MAJOR) ||
	    ((pf_vvi->major == VIRTCHNL_VERSION_MAJOR) &&
	    (pf_vvi->minor > VIRTCHNL_VERSION_MINOR))) {
		device_printf(dev, "Critical PF/VF API version mismatch!\n");
		error = EIO;
	} else {
		sc->version.major = pf_vvi->major;
		sc->version.minor = pf_vvi->minor;
	}

out_alloc:
	free(event.msg_buf, M_IAVF);
	return (error);
}

/**
 * iavf_send_vf_config_msg - Send VF configuration request
 * @sc: device softc
 *
 * Send VF configuration request admin queue message to the PF. The reply
 * is not checked in this function.
 *
 * @returns 0 if the message was successfully sent, or one of the
 * IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
 */
int
iavf_send_vf_config_msg(struct iavf_sc *sc)
{
	u32 caps;

	/* Support the base mode functionality, as well as advanced
	 * speed reporting capability.
	 */
	caps = VF_BASE_MODE_OFFLOADS |
	    VIRTCHNL_VF_CAP_ADV_LINK_SPEED;

	iavf_dbg_info(sc, "Sending offload flags: 0x%b\n",
	    caps, IAVF_PRINTF_VF_OFFLOAD_FLAGS);

	if (sc->version.minor == VIRTCHNL_VERSION_MINOR_NO_VF_CAPS)
		return iavf_send_pf_msg(sc, VIRTCHNL_OP_GET_VF_RESOURCES,
				  NULL, 0);
	else
		return iavf_send_pf_msg(sc, VIRTCHNL_OP_GET_VF_RESOURCES,
				  (u8 *)&caps, sizeof(caps));
}

/**
 * iavf_get_vf_config - Get the VF configuration from the PF
 * @sc: device softc
 *
 * Get VF configuration from PF and populate hw structure. Must be called after
 * admin queue is initialized. Busy waits until response is received from PF,
 * with maximum timeout. Response from PF is returned in the buffer for further
 * processing by the caller.
 *
 * @returns zero on success, or an error code on failure
 */
int
iavf_get_vf_config(struct iavf_sc *sc)
{

	return (iavf_get_vf_config_retries(sc, IAVF_AQ_MAX_ERR));
}

/**
 * iavf_get_vf_config_retries - Get VF configuration with a retry limit
 * @sc: device softc
 * @max_retries: maximum number of Admin Receive Queue polls
 *
 * @returns zero on success, ETIMEDOUT if the PF does not reply, or an error
 * for an invalid response.
 */
int
iavf_get_vf_config_retries(struct iavf_sc *sc, u32 max_retries)
{
	struct iavf_hw	*hw = &sc->hw;
	device_t	dev = sc->dev;
	enum iavf_status status = IAVF_SUCCESS;
	struct iavf_arq_event_info event;
	u16 len;
	u32 retries = 0;
	int error = 0;

	/* Note this assumes a single VSI */
	len = sizeof(struct virtchnl_vf_resource) +
	    sizeof(struct virtchnl_vsi_resource);
	event.buf_len = len;
	event.msg_buf = (u8 *)malloc(event.buf_len, M_IAVF, M_WAITOK);

	for (;;) {
		status = iavf_clean_arq_element(hw, &event, NULL);
		if (status == IAVF_ERR_ADMIN_QUEUE_NO_WORK) {
			if (++retries <= max_retries)
				iavf_msec_pause(10);
		} else if ((enum virtchnl_ops)le32toh(event.desc.cookie_high) !=
		    VIRTCHNL_OP_GET_VF_RESOURCES) {
			iavf_dbg_vc(sc, "%s: Received a response from PF,"
			    " opcode %d, error %d",
			    __func__,
			    le32toh(event.desc.cookie_high),
			    le32toh(event.desc.cookie_low));
			retries++;
			continue;
		} else {
			status = (enum iavf_status)le32toh(event.desc.cookie_low);
			if (status) {
				device_printf(dev, "%s: Error returned from PF,"
				    " opcode %d, error %d\n", __func__,
				    le32toh(event.desc.cookie_high),
				    le32toh(event.desc.cookie_low));
				error = EIO;
				goto out_alloc;
			}
			/* We retrieved the config message, with no errors */
			break;
		}

		if (retries > max_retries) {
			iavf_dbg_vc(sc,
			    "%s: Did not receive response after %d tries.",
			    __func__, retries);
			error = ETIMEDOUT;
			goto out_alloc;
		}
	}

	bzero(sc->vf_res, sizeof(struct virtchnl_vf_resource) +
	    IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
	memcpy(sc->vf_res, event.msg_buf, min(event.msg_len, len));
	iavf_vf_parse_hw_config(hw, sc->vf_res);

out_alloc:
	free(event.msg_buf, M_IAVF);
	return (error);
}

/**
 * iavf_enable_queues - Enable queues
 * @sc: device softc
 *
 * Request that the PF enable all of our queues.
 *
 * @remark the reply from the PF is not checked by this function.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_enable_queues(struct iavf_sc *sc)
{
	struct virtchnl_queue_select vqs;
	struct iavf_vsi *vsi = &sc->vsi;

	vqs.vsi_id = sc->vsi_res->vsi_id;
	vqs.tx_queues = (1 << IAVF_NTXQS(vsi)) - 1;
	vqs.rx_queues = vqs.tx_queues;
	return (iavf_send_pf_msg(sc, VIRTCHNL_OP_ENABLE_QUEUES,
	    (u8 *)&vqs, sizeof(vqs)));
}

/**
 * iavf_disable_queues - Disable queues
 * @sc: device softc
 *
 * Request that the PF disable all of our queues.
 *
 * @remark the reply from the PF is not checked by this function.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_disable_queues(struct iavf_sc *sc)
{
	struct virtchnl_queue_select vqs;
	struct iavf_vsi *vsi = &sc->vsi;

	vqs.vsi_id = sc->vsi_res->vsi_id;
	vqs.tx_queues = (1 << IAVF_NTXQS(vsi)) - 1;
	vqs.rx_queues = vqs.tx_queues;
	return (iavf_send_pf_msg(sc, VIRTCHNL_OP_DISABLE_QUEUES,
	    (u8 *)&vqs, sizeof(vqs)));
}

/**
 * iavf_add_vlans - Add VLAN filters
 * @sc: device softc
 *
 * Scan the Filter List looking for vlans that need
 * to be added, then create the data to hand to the AQ
 * for handling.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_add_vlans(struct iavf_sc *sc)
{
	struct virtchnl_vlan_filter_list *v;
	struct iavf_vlan_filter *f, *ftmp;
	device_t dev = sc->dev;
	int i = 0, cnt = 0;
	u32 len;

	/* Get count of VLAN filters to add */
	SLIST_FOREACH(f, sc->vlan_filters, next) {
		if (f->flags & IAVF_FILTER_ADD)
			cnt++;
	}

	if (!cnt) /* no work... */
		return (ENOENT);

	len = sizeof(struct virtchnl_vlan_filter_list) +
	      (cnt * sizeof(u16));

	if (len > IAVF_AQ_BUF_SZ) {
		device_printf(dev, "%s: Exceeded Max AQ Buf size\n",
			__func__);
		return (EFBIG);
	}

	v = (struct virtchnl_vlan_filter_list *)malloc(len, M_IAVF, M_NOWAIT | M_ZERO);
	if (!v) {
		device_printf(dev, "%s: unable to allocate memory\n",
			__func__);
		return (ENOMEM);
	}

	v->vsi_id = sc->vsi_res->vsi_id;
	v->num_elements = cnt;

	/* Scan the filter array */
	SLIST_FOREACH_SAFE(f, sc->vlan_filters, next, ftmp) {
                if (f->flags & IAVF_FILTER_ADD) {
                        bcopy(&f->vlan, &v->vlan_id[i], sizeof(u16));
			f->flags = IAVF_FILTER_USED;
                        i++;
                }
                if (i == cnt)
                        break;
	}

	iavf_send_pf_msg(sc, VIRTCHNL_OP_ADD_VLAN, (u8 *)v, len);
	free(v, M_IAVF);
	/* add stats? */
	return (0);
}

/**
 * iavf_del_vlans - Delete VLAN filters
 * @sc: device softc
 *
 * Scan the Filter Table looking for vlans that need
 * to be removed, then create the data to hand to the AQ
 * for handling.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_del_vlans(struct iavf_sc *sc)
{
	struct virtchnl_vlan_filter_list *v;
	struct iavf_vlan_filter *f, *ftmp;
	device_t dev = sc->dev;
	int i = 0, cnt = 0;
	u32 len;

	/* Get count of VLAN filters to delete */
	SLIST_FOREACH(f, sc->vlan_filters, next) {
		if (f->flags & IAVF_FILTER_DEL)
			cnt++;
	}

	if (!cnt) /* no work... */
		return (ENOENT);

	len = sizeof(struct virtchnl_vlan_filter_list) +
	      (cnt * sizeof(u16));

	if (len > IAVF_AQ_BUF_SZ) {
		device_printf(dev, "%s: Exceeded Max AQ Buf size\n",
			__func__);
		return (EFBIG);
	}

	v = (struct virtchnl_vlan_filter_list *)
	    malloc(len, M_IAVF, M_NOWAIT | M_ZERO);
	if (!v) {
		device_printf(dev, "%s: unable to allocate memory\n",
			__func__);
		return (ENOMEM);
	}

	v->vsi_id = sc->vsi_res->vsi_id;
	v->num_elements = cnt;

	/* Scan the filter array */
	SLIST_FOREACH_SAFE(f, sc->vlan_filters, next, ftmp) {
                if (f->flags & IAVF_FILTER_DEL) {
                        bcopy(&f->vlan, &v->vlan_id[i], sizeof(u16));
                        i++;
                        SLIST_REMOVE(sc->vlan_filters, f, iavf_vlan_filter, next);
                        free(f, M_IAVF);
                }
                if (i == cnt)
                        break;
	}

	iavf_send_pf_msg(sc, VIRTCHNL_OP_DEL_VLAN, (u8 *)v, len);
	free(v, M_IAVF);
	/* add stats? */
	return (0);
}

/**
 * iavf_add_ether_filters - Add MAC filters
 * @sc: device softc
 *
 * This routine takes additions to the vsi filter
 * table and creates an Admin Queue call to create
 * the filters in the hardware.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_add_ether_filters(struct iavf_sc *sc)
{
	struct virtchnl_ether_addr_list *a;
	struct iavf_mac_filter *f;
	device_t dev = sc->dev;
	int len, j = 0, cnt = 0;
	int error;

	/* Get count of MAC addresses to add */
	SLIST_FOREACH(f, sc->mac_filters, next) {
		if (f->flags & IAVF_FILTER_ADD)
			cnt++;
	}
	if (cnt == 0) { /* Should not happen... */
		iavf_dbg_vc(sc, "%s: cnt == 0, exiting...\n", __func__);
		return (ENOENT);
	}

	len = sizeof(struct virtchnl_ether_addr_list) +
	    (cnt * sizeof(struct virtchnl_ether_addr));

	a = (struct virtchnl_ether_addr_list *)
	    malloc(len, M_IAVF, M_NOWAIT | M_ZERO);
	if (a == NULL) {
		device_printf(dev, "%s: Failed to get memory for "
		    "virtchnl_ether_addr_list\n", __func__);
		return (ENOMEM);
	}
	a->vsi_id = sc->vsi.id;
	a->num_elements = cnt;

	/* Scan the filter array */
	SLIST_FOREACH(f, sc->mac_filters, next) {
		if (f->flags & IAVF_FILTER_ADD) {
			bcopy(f->macaddr, a->list[j].addr, ETHER_ADDR_LEN);
			f->flags &= ~IAVF_FILTER_ADD;
			j++;

			iavf_dbg_vc(sc, "%s: ADD: " MAC_FORMAT "\n",
			    __func__, MAC_FORMAT_ARGS(f->macaddr));
		}
		if (j == cnt)
			break;
	}
	iavf_dbg_vc(sc, "%s: len %d, j %d, cnt %d\n", __func__,
	    len, j, cnt);

	error = iavf_send_pf_msg(sc,
	    VIRTCHNL_OP_ADD_ETH_ADDR, (u8 *)a, len);
	/* add stats? */
	free(a, M_IAVF);
	return (error);
}

/**
 * iavf_del_ether_filters - Delete MAC filters
 * @sc: device softc
 *
 * This routine takes filters flagged for deletion in the
 * sc MAC filter list and creates an Admin Queue call
 * to delete those filters in the hardware.
 *
 * @returns zero on success, or an error code on failure.
*/
int
iavf_del_ether_filters(struct iavf_sc *sc)
{
	struct virtchnl_ether_addr_list *d;
	struct iavf_mac_filter *f, *f_temp;
	device_t dev = sc->dev;
	int len, j = 0, cnt = 0;

	/* Get count of MAC addresses to delete */
	SLIST_FOREACH(f, sc->mac_filters, next) {
		if (f->flags & IAVF_FILTER_DEL)
			cnt++;
	}
	if (cnt == 0) {
		iavf_dbg_vc(sc, "%s: cnt == 0, exiting...\n", __func__);
		return (ENOENT);
	}

	len = sizeof(struct virtchnl_ether_addr_list) +
	    (cnt * sizeof(struct virtchnl_ether_addr));

	d = (struct virtchnl_ether_addr_list *)
	    malloc(len, M_IAVF, M_NOWAIT | M_ZERO);
	if (d == NULL) {
		device_printf(dev, "%s: Failed to get memory for "
		    "virtchnl_ether_addr_list\n", __func__);
		return (ENOMEM);
	}
	d->vsi_id = sc->vsi.id;
	d->num_elements = cnt;

	/* Scan the filter array */
	SLIST_FOREACH_SAFE(f, sc->mac_filters, next, f_temp) {
		if (f->flags & IAVF_FILTER_DEL) {
			bcopy(f->macaddr, d->list[j].addr, ETHER_ADDR_LEN);
			iavf_dbg_vc(sc, "DEL: " MAC_FORMAT "\n",
			    MAC_FORMAT_ARGS(f->macaddr));
			j++;
			SLIST_REMOVE(sc->mac_filters, f, iavf_mac_filter, next);
			free(f, M_IAVF);
		}
		if (j == cnt)
			break;
	}
	iavf_send_pf_msg(sc,
	    VIRTCHNL_OP_DEL_ETH_ADDR, (u8 *)d, len);
	/* add stats? */
	free(d, M_IAVF);
	return (0);
}

/**
 * iavf_request_reset - Request a device reset
 * @sc: device softc
 *
 * Request that the PF reset this VF. No response is expected.
 *
 * @returns zero
 */
int
iavf_request_reset(struct iavf_sc *sc)
{
	/*
	** Set the reset status to "in progress" before
	** the request, this avoids any possibility of
	** a mistaken early detection of completion.
	*/
	wr32(&sc->hw, IAVF_VFGEN_RSTAT, VIRTCHNL_VFR_INPROGRESS);
	iavf_send_pf_msg(sc, VIRTCHNL_OP_RESET_VF, NULL, 0);
	return (0);
}

/**
 * iavf_request_stats - Request VF stats
 * @sc: device softc
 *
 * Request the statistics for this VF's VSI from PF.
 *
 * @remark prints an error message on failure to obtain stats, but does not
 * return with an error code.
 *
 * @returns zero
 */
int
iavf_request_stats(struct iavf_sc *sc)
{
	struct virtchnl_queue_select vqs;
	int error = 0;

	vqs.vsi_id = sc->vsi_res->vsi_id;
	/* Low priority, we don't need to error check */
	error = iavf_send_pf_msg(sc, VIRTCHNL_OP_GET_STATS,
	    (u8 *)&vqs, sizeof(vqs));
	if (error)
		device_printf(sc->dev, "Error sending stats request to PF: %d\n", error);

	return (0);
}

/**
 * iavf_update_stats_counters - Update driver statistics
 * @sc: device softc
 * @es: ethernet stats storage
 *
 * Updates driver's stats counters with VSI stats returned from PF.
 */
void
iavf_update_stats_counters(struct iavf_sc *sc, struct iavf_eth_stats *es)
{
	struct iavf_vsi *vsi = &sc->vsi;

	/* Update ifnet stats */
	vsi->ipackets = es->rx_unicast + es->rx_multicast + es->rx_broadcast;
	vsi->opackets = es->tx_unicast + es->tx_multicast + es->tx_broadcast;
	vsi->ibytes = es->rx_bytes;
	vsi->obytes = es->tx_bytes;
	vsi->imcasts = es->rx_multicast;
	vsi->omcasts = es->tx_multicast;

	vsi->oerrors = es->tx_errors;
	vsi->iqdrops = es->rx_discards;
	vsi->oqdrops = es->tx_discards;
	vsi->noproto = es->rx_unknown_protocol;

	vsi->eth_stats = *es;
}

/**
 * iavf_config_rss_key - Configure RSS key over virtchnl
 * @sc: device softc
 *
 * Send a message to the PF to configure the RSS key using the virtchnl
 * interface.
 *
 * @remark this does not check the reply from the PF.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_config_rss_key(struct iavf_sc *sc)
{
	struct virtchnl_rss_key *rss_key_msg;
	int msg_len, key_length;
	u32		rss_seed[IAVF_RSS_KEY_SIZE_REG] = {0};

	/* Fetch the configured RSS key */
	rss_getkey((uint8_t *) &rss_seed);

	/* Send the fetched key */
	key_length = sc->vf_res->rss_key_size;
	msg_len = sizeof(struct virtchnl_rss_key) + (sizeof(u8) * key_length) - 1;
	rss_key_msg = (struct virtchnl_rss_key *)
	    malloc(msg_len, M_IAVF, M_NOWAIT | M_ZERO);
	if (rss_key_msg == NULL) {
		device_printf(sc->dev, "Unable to allocate msg memory for RSS key msg.\n");
		return (ENOMEM);
	}

	rss_key_msg->vsi_id = sc->vsi_res->vsi_id;
	rss_key_msg->key_len = key_length;
	bcopy(rss_seed, &rss_key_msg->key[0], key_length);

	iavf_dbg_vc(sc, "%s: vsi_id %d, key_len %d\n", __func__,
	    rss_key_msg->vsi_id, rss_key_msg->key_len);

	iavf_send_pf_msg(sc, VIRTCHNL_OP_CONFIG_RSS_KEY,
			  (u8 *)rss_key_msg, msg_len);

	free(rss_key_msg, M_IAVF);
	return (0);
}

/**
 * iavf_set_rss_hena - Configure the RSS HENA
 * @sc: device softc
 *
 * Configure the RSS HENA values by sending a virtchnl message to the PF
 *
 * @remark the reply from the PF is not checked by this function.
 *
 * @returns zero
 */
int
iavf_set_rss_hena(struct iavf_sc *sc)
{
	struct virtchnl_rss_hena hena;
	struct iavf_hw *hw = &sc->hw;

	if (hw->mac.type == IAVF_MAC_VF)
		hena.hena = IAVF_DEFAULT_RSS_HENA_AVF;
	else if (hw->mac.type == IAVF_MAC_X722_VF)
		hena.hena = IAVF_DEFAULT_RSS_HENA_X722;
	else
		hena.hena = IAVF_DEFAULT_RSS_HENA_BASE;

	iavf_send_pf_msg(sc, VIRTCHNL_OP_SET_RSS_HENA,
	    (u8 *)&hena, sizeof(hena));
	return (0);
}

/**
 * iavf_config_rss_lut - Configure RSS lookup table
 * @sc: device softc
 *
 * Configure the RSS lookup table by sending a virtchnl message to the PF.
 *
 * @remark the reply from the PF is not checked in this function.
 *
 * @returns zero on success, or an error code on failure.
 */
int
iavf_config_rss_lut(struct iavf_sc *sc)
{
	struct virtchnl_rss_lut *rss_lut_msg;
	int msg_len;
	u16 lut_length;
	u32 lut;
	int i, que_id;

	lut_length = sc->vf_res->rss_lut_size;
	msg_len = sizeof(struct virtchnl_rss_lut) + (lut_length * sizeof(u8)) - 1;
	rss_lut_msg = (struct virtchnl_rss_lut *)
	    malloc(msg_len, M_IAVF, M_NOWAIT | M_ZERO);
	if (rss_lut_msg == NULL) {
		device_printf(sc->dev, "Unable to allocate msg memory for RSS lut msg.\n");
		return (ENOMEM);
	}

	rss_lut_msg->vsi_id = sc->vsi_res->vsi_id;
	/* Each LUT entry is a max of 1 byte, so this is easy */
	rss_lut_msg->lut_entries = lut_length;

	/* Populate the LUT with max no. of queues in round robin fashion */
	for (i = 0; i < lut_length; i++) {
#ifdef RSS
		/*
		 * Fetch the RSS bucket id for the given indirection entry.
		 * Cap it at the number of configured buckets (which is
		 * num_queues.)
		 */
		que_id = rss_get_indirection_to_bucket(i);
		que_id = que_id % sc->vsi.num_rx_queues;
#else
		que_id = i % sc->vsi.num_rx_queues;
#endif
		lut = que_id & IAVF_RSS_VSI_LUT_ENTRY_MASK;
		rss_lut_msg->lut[i] = lut;
	}

	iavf_send_pf_msg(sc, VIRTCHNL_OP_CONFIG_RSS_LUT,
			  (u8 *)rss_lut_msg, msg_len);

	free(rss_lut_msg, M_IAVF);
	return (0);
}

/**
 * iavf_config_promisc_mode - Configure promiscuous mode
 * @sc: device softc
 *
 * Configure the device into promiscuous mode by sending a virtchnl message to
 * the PF.
 *
 * @remark the reply from the PF is not checked in this function.
 *
 * @returns zero
 */
int
iavf_config_promisc_mode(struct iavf_sc *sc)
{
	struct virtchnl_promisc_info pinfo;

	pinfo.vsi_id = sc->vsi_res->vsi_id;
	pinfo.flags = sc->promisc_flags;

	iavf_send_pf_msg(sc, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
	    (u8 *)&pinfo, sizeof(pinfo));
	return (0);
}

/**
 * iavf_vc_send_cmd - Convert request into virtchnl calls
 * @sc: device softc
 * @request: the requested command to run
 *
 * Send the proper virtchnl call based on the request value.
 *
 * @returns zero on success, or an error code on failure. Note that unknown
 * requests will return zero.
 */
int
iavf_vc_send_cmd(struct iavf_sc *sc, uint32_t request)
{
	switch (request) {
	case IAVF_FLAG_AQ_MAP_VECTORS:
		return iavf_map_queues(sc);

	case IAVF_FLAG_AQ_ADD_MAC_FILTER:
		return iavf_add_ether_filters(sc);

	case IAVF_FLAG_AQ_ADD_VLAN_FILTER:
		return iavf_add_vlans(sc);

	case IAVF_FLAG_AQ_DEL_MAC_FILTER:
		return iavf_del_ether_filters(sc);

	case IAVF_FLAG_AQ_DEL_VLAN_FILTER:
		return iavf_del_vlans(sc);

	case IAVF_FLAG_AQ_CONFIGURE_QUEUES:
		return iavf_configure_queues(sc);

	case IAVF_FLAG_AQ_DISABLE_QUEUES:
		return iavf_disable_queues(sc);

	case IAVF_FLAG_AQ_ENABLE_QUEUES:
		return iavf_enable_queues(sc);

	case IAVF_FLAG_AQ_CONFIG_RSS_KEY:
		return iavf_config_rss_key(sc);

	case IAVF_FLAG_AQ_SET_RSS_HENA:
		return iavf_set_rss_hena(sc);

	case IAVF_FLAG_AQ_CONFIG_RSS_LUT:
		return iavf_config_rss_lut(sc);

	case IAVF_FLAG_AQ_CONFIGURE_PROMISC:
		return iavf_config_promisc_mode(sc);
	}

	return (0);
}

/**
 * iavf_vc_get_op_chan - Get op channel for a request
 * @sc: device softc
 * @request: the request type
 *
 * @returns the op channel for the given request, or NULL if no channel is
 * used.
 */
void *
iavf_vc_get_op_chan(struct iavf_sc *sc, uint32_t request)
{
	switch (request) {
	case IAVF_FLAG_AQ_ENABLE_QUEUES:
		return (&sc->enable_queues_chan);
	case IAVF_FLAG_AQ_DISABLE_QUEUES:
		return (&sc->disable_queues_chan);
	default:
		return (NULL);
	}
}

/**
 * iavf_vc_stat_str - convert virtchnl status err code to a string
 * @hw: pointer to the HW structure
 * @stat_err: the status error code to convert
 *
 * @returns the human readable string representing the specified error code.
 **/
const char *
iavf_vc_stat_str(struct iavf_hw *hw, enum virtchnl_status_code stat_err)
{
	switch (stat_err) {
	case VIRTCHNL_STATUS_SUCCESS:
		return "OK";
	case VIRTCHNL_ERR_PARAM:
		return "VIRTCHNL_ERR_PARAM";
	case VIRTCHNL_STATUS_ERR_NO_MEMORY:
		return "VIRTCHNL_STATUS_ERR_NO_MEMORY";
	case VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH:
		return "VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH";
	case VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR:
		return "VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR";
	case VIRTCHNL_STATUS_ERR_INVALID_VF_ID:
		return "VIRTCHNL_STATUS_ERR_INVALID_VF_ID";
	case VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR:
		return "VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR";
	case VIRTCHNL_STATUS_NOT_SUPPORTED:
		return "VIRTCHNL_STATUS_NOT_SUPPORTED";
	}

	snprintf(hw->err_str, sizeof(hw->err_str), "%d", stat_err);
	return hw->err_str;
}

/**
 * iavf_adv_speed_to_ext_speed - Convert numeric speed to iavf speed enum
 * @adv_link_speed: link speed in Mb/s
 *
 * Converts the link speed from the "advanced" link speed virtchnl op into the
 * closest approximation of the internal iavf link speed, rounded down.
 *
 * @returns the link speed as an iavf_ext_link_speed enum value
 */
enum iavf_ext_link_speed
iavf_adv_speed_to_ext_speed(u32 adv_link_speed)
{
	if (adv_link_speed >= 100000)
		return IAVF_EXT_LINK_SPEED_100GB;
	if (adv_link_speed >= 50000)
		return IAVF_EXT_LINK_SPEED_50GB;
	if (adv_link_speed >= 40000)
		return IAVF_EXT_LINK_SPEED_40GB;
	if (adv_link_speed >= 25000)
		return IAVF_EXT_LINK_SPEED_25GB;
	if (adv_link_speed >= 20000)
		return IAVF_EXT_LINK_SPEED_20GB;
	if (adv_link_speed >= 10000)
		return IAVF_EXT_LINK_SPEED_10GB;
	if (adv_link_speed >= 5000)
		return IAVF_EXT_LINK_SPEED_5GB;
	if (adv_link_speed >= 2500)
		return IAVF_EXT_LINK_SPEED_2500MB;
	if (adv_link_speed >= 1000)
		return IAVF_EXT_LINK_SPEED_1000MB;
	if (adv_link_speed >= 100)
		return IAVF_EXT_LINK_SPEED_100MB;
	if (adv_link_speed >= 10)
		return IAVF_EXT_LINK_SPEED_10MB;

	return IAVF_EXT_LINK_SPEED_UNKNOWN;
}

/**
 * iavf_ext_speed_to_ifmedia - Convert internal iavf speed to ifmedia value
 * @link_speed: the link speed
 *
 * @remark this is sort of a hack, because we don't actually know what media
 * type the VF is running on. In an ideal world we might just report the media
 * type as "virtual" and have another mechanism for reporting the link
 * speed.
 *
 * @returns a suitable ifmedia type for the given link speed.
 */
u32
iavf_ext_speed_to_ifmedia(enum iavf_ext_link_speed link_speed)
{
	switch (link_speed) {
	case IAVF_EXT_LINK_SPEED_100GB:
		return IFM_100G_SR4;
	case IAVF_EXT_LINK_SPEED_50GB:
		return IFM_50G_SR2;
	case IAVF_EXT_LINK_SPEED_40GB:
		return IFM_40G_SR4;
	case IAVF_EXT_LINK_SPEED_25GB:
		return IFM_25G_SR;
	case IAVF_EXT_LINK_SPEED_20GB:
		return IFM_20G_KR2;
	case IAVF_EXT_LINK_SPEED_10GB:
		return IFM_10G_SR;
	case IAVF_EXT_LINK_SPEED_5GB:
		return IFM_5000_T;
	case IAVF_EXT_LINK_SPEED_2500MB:
		return IFM_2500_T;
	case IAVF_EXT_LINK_SPEED_1000MB:
		return IFM_1000_T;
	case IAVF_EXT_LINK_SPEED_100MB:
		return IFM_100_TX;
	case IAVF_EXT_LINK_SPEED_10MB:
		return IFM_10_T;
	case IAVF_EXT_LINK_SPEED_UNKNOWN:
	default:
		return IFM_UNKNOWN;
	}
}

/**
 * iavf_vc_speed_to_ext_speed - Convert virtchnl speed enum to native iavf
 * driver speed representation.
 * @link_speed: link speed enum value
 *
 * @returns the link speed in the native iavf format.
 */
enum iavf_ext_link_speed
iavf_vc_speed_to_ext_speed(enum virtchnl_link_speed link_speed)
{
	switch (link_speed) {
	case VIRTCHNL_LINK_SPEED_40GB:
		return IAVF_EXT_LINK_SPEED_40GB;
	case VIRTCHNL_LINK_SPEED_25GB:
		return IAVF_EXT_LINK_SPEED_25GB;
	case VIRTCHNL_LINK_SPEED_20GB:
		return IAVF_EXT_LINK_SPEED_20GB;
	case VIRTCHNL_LINK_SPEED_10GB:
		return IAVF_EXT_LINK_SPEED_10GB;
	case VIRTCHNL_LINK_SPEED_1GB:
		return IAVF_EXT_LINK_SPEED_1000MB;
	case VIRTCHNL_LINK_SPEED_100MB:
		return IAVF_EXT_LINK_SPEED_100MB;
	case VIRTCHNL_LINK_SPEED_UNKNOWN:
	default:
		return IAVF_EXT_LINK_SPEED_UNKNOWN;
	}
}

/**
 * iavf_vc_speed_to_string - Convert virtchnl speed to a string
 * @link_speed: the speed to convert
 *
 * @returns string representing the link speed as reported by the virtchnl
 * interface.
 */
const char *
iavf_vc_speed_to_string(enum virtchnl_link_speed link_speed)
{
	return iavf_ext_speed_to_str(iavf_vc_speed_to_ext_speed(link_speed));
}

/**
 * iavf_ext_speed_to_str - Convert iavf speed enum to string representation
 * @link_speed: link speed enum value
 *
 * XXX: This is an iavf-modified copy of ice_aq_speed_to_str()
 *
 * @returns the string representation of the given link speed.
 */
const char *
iavf_ext_speed_to_str(enum iavf_ext_link_speed link_speed)
{
	switch (link_speed) {
	case IAVF_EXT_LINK_SPEED_100GB:
		return "100 Gbps";
	case IAVF_EXT_LINK_SPEED_50GB:
		return "50 Gbps";
	case IAVF_EXT_LINK_SPEED_40GB:
		return "40 Gbps";
	case IAVF_EXT_LINK_SPEED_25GB:
		return "25 Gbps";
	case IAVF_EXT_LINK_SPEED_20GB:
		return "20 Gbps";
	case IAVF_EXT_LINK_SPEED_10GB:
		return "10 Gbps";
	case IAVF_EXT_LINK_SPEED_5GB:
		return "5 Gbps";
	case IAVF_EXT_LINK_SPEED_2500MB:
		return "2.5 Gbps";
	case IAVF_EXT_LINK_SPEED_1000MB:
		return "1 Gbps";
	case IAVF_EXT_LINK_SPEED_100MB:
		return "100 Mbps";
	case IAVF_EXT_LINK_SPEED_10MB:
		return "10 Mbps";
	case IAVF_EXT_LINK_SPEED_UNKNOWN:
	default:
		return "Unknown";
	}
}

/**
 * iavf_vc_opcode_str - Convert virtchnl opcode to string
 * @op: the virtchnl op code
 *
 * @returns the string representation of the given virtchnl op code
 */
const char *
iavf_vc_opcode_str(uint16_t op)
{
	switch (op) {
	case VIRTCHNL_OP_VERSION:
		return ("VERSION");
	case VIRTCHNL_OP_RESET_VF:
		return ("RESET_VF");
	case VIRTCHNL_OP_GET_VF_RESOURCES:
		return ("GET_VF_RESOURCES");
	case VIRTCHNL_OP_CONFIG_TX_QUEUE:
		return ("CONFIG_TX_QUEUE");
	case VIRTCHNL_OP_CONFIG_RX_QUEUE:
		return ("CONFIG_RX_QUEUE");
	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
		return ("CONFIG_VSI_QUEUES");
	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
		return ("CONFIG_IRQ_MAP");
	case VIRTCHNL_OP_ENABLE_QUEUES:
		return ("ENABLE_QUEUES");
	case VIRTCHNL_OP_DISABLE_QUEUES:
		return ("DISABLE_QUEUES");
	case VIRTCHNL_OP_ADD_ETH_ADDR:
		return ("ADD_ETH_ADDR");
	case VIRTCHNL_OP_DEL_ETH_ADDR:
		return ("DEL_ETH_ADDR");
	case VIRTCHNL_OP_ADD_VLAN:
		return ("ADD_VLAN");
	case VIRTCHNL_OP_DEL_VLAN:
		return ("DEL_VLAN");
	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
		return ("CONFIG_PROMISCUOUS_MODE");
	case VIRTCHNL_OP_GET_STATS:
		return ("GET_STATS");
	case VIRTCHNL_OP_RSVD:
		return ("RSVD");
	case VIRTCHNL_OP_EVENT:
		return ("EVENT");
	case VIRTCHNL_OP_CONFIG_RSS_KEY:
		return ("CONFIG_RSS_KEY");
	case VIRTCHNL_OP_CONFIG_RSS_LUT:
		return ("CONFIG_RSS_LUT");
	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
		return ("GET_RSS_HENA_CAPS");
	case VIRTCHNL_OP_SET_RSS_HENA:
		return ("SET_RSS_HENA");
	default:
		return ("UNKNOWN");
	}
}

/**
 * iavf_vc_completion - Handle PF reply messages
 * @sc: device softc
 * @v_opcode: virtchnl op code
 * @v_retval: virtchnl return value
 * @msg: the message to send
 * @msglen: length of the msg buffer
 *
 * Asynchronous completion function for admin queue messages. Rather than busy
 * wait, we fire off our requests and assume that no errors will be returned.
 * This function handles the reply messages.
 */
void
iavf_vc_completion(struct iavf_sc *sc,
    enum virtchnl_ops v_opcode,
    enum virtchnl_status_code v_retval, u8 *msg, u16 msglen __unused)
{
	device_t	dev = sc->dev;

	if (v_opcode != VIRTCHNL_OP_GET_STATS)
		iavf_dbg_vc(sc, "%s: opcode %s\n", __func__,
		    iavf_vc_opcode_str(v_opcode));

	if (v_opcode == VIRTCHNL_OP_EVENT) {
		struct virtchnl_pf_event *vpe =
			(struct virtchnl_pf_event *)msg;

		switch (vpe->event) {
		case VIRTCHNL_EVENT_LINK_CHANGE:
			iavf_handle_link_event(sc, vpe);
			break;
		case VIRTCHNL_EVENT_RESET_IMPENDING:
			device_printf(dev, "PF initiated reset!\n");
			iavf_set_state(&sc->state, IAVF_STATE_RESET_PENDING);
			break;
		default:
			iavf_dbg_vc(sc, "Unknown event %d from AQ\n",
				vpe->event);
			break;
		}

		return;
	}

	/* Catch-all error response */
	if (v_retval) {
		bool print_error = true;

		switch (v_opcode) {
		case VIRTCHNL_OP_ADD_ETH_ADDR:
			device_printf(dev, "WARNING: Error adding VF mac filter!\n");
			device_printf(dev, "WARNING: Device may not receive traffic!\n");
			break;
		case VIRTCHNL_OP_ENABLE_QUEUES:
			sc->enable_queues_chan = 1;
			wakeup_one(&sc->enable_queues_chan);
			break;
		case VIRTCHNL_OP_DISABLE_QUEUES:
			sc->disable_queues_chan = 1;
			wakeup_one(&sc->disable_queues_chan);
			/* This may fail, but it does not necessarily mean that
			 * something is critically wrong.
			 */
			if (!(sc->dbg_mask & IAVF_DBG_VC))
				print_error = false;
			break;
		default:
			break;
		}

		if (print_error)
			device_printf(dev,
			    "%s: AQ returned error %s to our request %s!\n",
			    __func__, iavf_vc_stat_str(&sc->hw, v_retval),
			    iavf_vc_opcode_str(v_opcode));
		return;
	}

	switch (v_opcode) {
	case VIRTCHNL_OP_GET_STATS:
		iavf_update_stats_counters(sc, (struct iavf_eth_stats *)msg);
		break;
	case VIRTCHNL_OP_ADD_ETH_ADDR:
		break;
	case VIRTCHNL_OP_DEL_ETH_ADDR:
		break;
	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
		break;
	case VIRTCHNL_OP_ADD_VLAN:
		break;
	case VIRTCHNL_OP_DEL_VLAN:
		break;
	case VIRTCHNL_OP_ENABLE_QUEUES:
		atomic_store_rel_32(&sc->queues_enabled, 1);
		sc->enable_queues_chan = 1;
		wakeup_one(&sc->enable_queues_chan);
		break;
	case VIRTCHNL_OP_DISABLE_QUEUES:
		atomic_store_rel_32(&sc->queues_enabled, 0);
		sc->disable_queues_chan = 1;
		wakeup_one(&sc->disable_queues_chan);
		break;
	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
		break;
	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
		break;
	case VIRTCHNL_OP_CONFIG_RSS_KEY:
		break;
	case VIRTCHNL_OP_SET_RSS_HENA:
		break;
	case VIRTCHNL_OP_CONFIG_RSS_LUT:
		break;
	default:
		iavf_dbg_vc(sc,
		    "Received unexpected message %s from PF.\n",
		    iavf_vc_opcode_str(v_opcode));
		break;
	}
}

/**
 * iavf_handle_link_event - Handle Link event virtchml message
 * @sc: device softc
 * @vpe: virtchnl PF link event structure
 *
 * Process a virtchnl PF link event and update the driver and stack status of
 * the link event.
 */
static void
iavf_handle_link_event(struct iavf_sc *sc, struct virtchnl_pf_event *vpe)
{
	MPASS(vpe->event == VIRTCHNL_EVENT_LINK_CHANGE);

	if (sc->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
	{
		iavf_dbg_vc(sc, "Link change (adv): status %d, speed %u\n",
		    vpe->event_data.link_event_adv.link_status,
		    vpe->event_data.link_event_adv.link_speed);
		sc->link_up =
			vpe->event_data.link_event_adv.link_status;
		sc->link_speed_adv =
			vpe->event_data.link_event_adv.link_speed;

	} else {
		iavf_dbg_vc(sc, "Link change: status %d, speed %x\n",
		    vpe->event_data.link_event.link_status,
		    vpe->event_data.link_event.link_speed);
		sc->link_up =
			vpe->event_data.link_event.link_status;
		sc->link_speed =
			vpe->event_data.link_event.link_speed;
	}

	iavf_update_link_status(sc);
}