summaryrefslogtreecommitdiffstats
path: root/drivers/net/sfc/base/efx_vpd.c
blob: 6d783d742bbf58ecd6890ed867be22210a7ae1f4 (plain)
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
/* SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2009-2018 Solarflare Communications Inc.
 * All rights reserved.
 */

#include "efx.h"
#include "efx_impl.h"

#if EFSYS_OPT_VPD

#define	TAG_TYPE_LBN 7
#define	TAG_TYPE_WIDTH 1
#define	TAG_TYPE_LARGE_ITEM_DECODE 1
#define	TAG_TYPE_SMALL_ITEM_DECODE 0

#define	TAG_SMALL_ITEM_NAME_LBN 3
#define	TAG_SMALL_ITEM_NAME_WIDTH 4
#define	TAG_SMALL_ITEM_SIZE_LBN 0
#define	TAG_SMALL_ITEM_SIZE_WIDTH 3

#define	TAG_LARGE_ITEM_NAME_LBN 0
#define	TAG_LARGE_ITEM_NAME_WIDTH 7

#define	TAG_NAME_END_DECODE 0x0f
#define	TAG_NAME_ID_STRING_DECODE 0x02
#define	TAG_NAME_VPD_R_DECODE 0x10
#define	TAG_NAME_VPD_W_DECODE 0x11

#if EFSYS_OPT_SIENA

static const efx_vpd_ops_t	__efx_vpd_siena_ops = {
	siena_vpd_init,		/* evpdo_init */
	siena_vpd_size,		/* evpdo_size */
	siena_vpd_read,		/* evpdo_read */
	siena_vpd_verify,	/* evpdo_verify */
	siena_vpd_reinit,	/* evpdo_reinit */
	siena_vpd_get,		/* evpdo_get */
	siena_vpd_set,		/* evpdo_set */
	siena_vpd_next,		/* evpdo_next */
	siena_vpd_write,	/* evpdo_write */
	siena_vpd_fini,		/* evpdo_fini */
};

#endif	/* EFSYS_OPT_SIENA */

#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2

static const efx_vpd_ops_t	__efx_vpd_ef10_ops = {
	ef10_vpd_init,		/* evpdo_init */
	ef10_vpd_size,		/* evpdo_size */
	ef10_vpd_read,		/* evpdo_read */
	ef10_vpd_verify,	/* evpdo_verify */
	ef10_vpd_reinit,	/* evpdo_reinit */
	ef10_vpd_get,		/* evpdo_get */
	ef10_vpd_set,		/* evpdo_set */
	ef10_vpd_next,		/* evpdo_next */
	ef10_vpd_write,		/* evpdo_write */
	ef10_vpd_fini,		/* evpdo_fini */
};

#endif	/* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */

	__checkReturn		efx_rc_t
efx_vpd_init(
	__in			efx_nic_t *enp)
{
	const efx_vpd_ops_t *evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
	EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_VPD));

	switch (enp->en_family) {
#if EFSYS_OPT_SIENA
	case EFX_FAMILY_SIENA:
		evpdop = &__efx_vpd_siena_ops;
		break;
#endif	/* EFSYS_OPT_SIENA */

#if EFSYS_OPT_HUNTINGTON
	case EFX_FAMILY_HUNTINGTON:
		evpdop = &__efx_vpd_ef10_ops;
		break;
#endif	/* EFSYS_OPT_HUNTINGTON */

#if EFSYS_OPT_MEDFORD
	case EFX_FAMILY_MEDFORD:
		evpdop = &__efx_vpd_ef10_ops;
		break;
#endif	/* EFSYS_OPT_MEDFORD */

#if EFSYS_OPT_MEDFORD2
	case EFX_FAMILY_MEDFORD2:
		evpdop = &__efx_vpd_ef10_ops;
		break;
#endif	/* EFSYS_OPT_MEDFORD2 */

	default:
		EFSYS_ASSERT(0);
		rc = ENOTSUP;
		goto fail1;
	}

	if (evpdop->evpdo_init != NULL) {
		if ((rc = evpdop->evpdo_init(enp)) != 0)
			goto fail2;
	}

	enp->en_evpdop = evpdop;
	enp->en_mod_flags |= EFX_MOD_VPD;

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_size(
	__in			efx_nic_t *enp,
	__out			size_t *sizep)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_size(enp, sizep)) != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_read(
	__in			efx_nic_t *enp,
	__out_bcount(size)	caddr_t data,
	__in			size_t size)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_read(enp, data, size)) != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_verify(
	__in			efx_nic_t *enp,
	__in_bcount(size)	caddr_t data,
	__in			size_t size)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_verify(enp, data, size)) != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_reinit(
	__in			efx_nic_t *enp,
	__in_bcount(size)	caddr_t data,
	__in			size_t size)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if (evpdop->evpdo_reinit == NULL) {
		rc = ENOTSUP;
		goto fail1;
	}

	if ((rc = evpdop->evpdo_reinit(enp, data, size)) != 0)
		goto fail2;

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_get(
	__in			efx_nic_t *enp,
	__in_bcount(size)	caddr_t data,
	__in			size_t size,
	__inout			efx_vpd_value_t *evvp)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_get(enp, data, size, evvp)) != 0) {
		if (rc == ENOENT)
			return (rc);

		goto fail1;
	}

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_set(
	__in			efx_nic_t *enp,
	__inout_bcount(size)	caddr_t data,
	__in			size_t size,
	__in			efx_vpd_value_t *evvp)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_set(enp, data, size, evvp)) != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_next(
	__in			efx_nic_t *enp,
	__inout_bcount(size)	caddr_t data,
	__in			size_t size,
	__out			efx_vpd_value_t *evvp,
	__inout			unsigned int *contp)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_next(enp, data, size, evvp, contp)) != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_write(
	__in			efx_nic_t *enp,
	__in_bcount(size)	caddr_t data,
	__in			size_t size)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;
	efx_rc_t rc;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if ((rc = evpdop->evpdo_write(enp, data, size)) != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	__checkReturn		efx_rc_t
efx_vpd_next_tag(
	__in			caddr_t data,
	__in			size_t size,
	__inout			unsigned int *offsetp,
	__out			efx_vpd_tag_t *tagp,
	__out			uint16_t *lengthp)
{
	efx_byte_t byte;
	efx_word_t word;
	uint8_t name;
	uint16_t length;
	size_t headlen;
	efx_rc_t rc;

	if (*offsetp >= size) {
		rc = EFAULT;
		goto fail1;
	}

	EFX_POPULATE_BYTE_1(byte, EFX_BYTE_0, data[*offsetp]);

	switch (EFX_BYTE_FIELD(byte, TAG_TYPE)) {
	case TAG_TYPE_SMALL_ITEM_DECODE:
		headlen = 1;

		name = EFX_BYTE_FIELD(byte, TAG_SMALL_ITEM_NAME);
		length = (uint16_t)EFX_BYTE_FIELD(byte, TAG_SMALL_ITEM_SIZE);

		break;

	case TAG_TYPE_LARGE_ITEM_DECODE:
		headlen = 3;

		if (*offsetp + headlen > size) {
			rc = EFAULT;
			goto fail2;
		}

		name = EFX_BYTE_FIELD(byte, TAG_LARGE_ITEM_NAME);
		EFX_POPULATE_WORD_2(word,
				    EFX_BYTE_0, data[*offsetp + 1],
				    EFX_BYTE_1, data[*offsetp + 2]);
		length = EFX_WORD_FIELD(word, EFX_WORD_0);

		break;

	default:
		rc = EFAULT;
		goto fail2;
	}

	if (*offsetp + headlen + length > size) {
		rc = EFAULT;
		goto fail3;
	}

	EFX_STATIC_ASSERT(TAG_NAME_END_DECODE == EFX_VPD_END);
	EFX_STATIC_ASSERT(TAG_NAME_ID_STRING_DECODE == EFX_VPD_ID);
	EFX_STATIC_ASSERT(TAG_NAME_VPD_R_DECODE == EFX_VPD_RO);
	EFX_STATIC_ASSERT(TAG_NAME_VPD_W_DECODE == EFX_VPD_RW);
	if (name != EFX_VPD_END && name != EFX_VPD_ID &&
	    name != EFX_VPD_RO) {
		rc = EFAULT;
		goto fail4;
	}

	*tagp = name;
	*lengthp = length;
	*offsetp += headlen;

	return (0);

fail4:
	EFSYS_PROBE(fail4);
fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	__checkReturn		efx_rc_t
efx_vpd_next_keyword(
	__in_bcount(size)	caddr_t tag,
	__in			size_t size,
	__in			unsigned int pos,
	__out			efx_vpd_keyword_t *keywordp,
	__out			uint8_t *lengthp)
{
	efx_vpd_keyword_t keyword;
	uint8_t length;
	efx_rc_t rc;

	if (pos + 3U > size) {
		rc = EFAULT;
		goto fail1;
	}

	keyword = EFX_VPD_KEYWORD(tag[pos], tag[pos + 1]);
	length = tag[pos + 2];

	if (length == 0 || pos + 3U + length > size) {
		rc = EFAULT;
		goto fail2;
	}

	*keywordp = keyword;
	*lengthp = length;

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_hunk_length(
	__in_bcount(size)	caddr_t data,
	__in			size_t size,
	__out			size_t *lengthp)
{
	efx_vpd_tag_t tag;
	unsigned int offset;
	uint16_t taglen;
	efx_rc_t rc;

	offset = 0;
	_NOTE(CONSTANTCONDITION)
	while (1) {
		if ((rc = efx_vpd_next_tag(data, size, &offset,
		    &tag, &taglen)) != 0)
			goto fail1;
		offset += taglen;
		if (tag == EFX_VPD_END)
			break;
	}

	*lengthp = offset;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_hunk_verify(
	__in_bcount(size)	caddr_t data,
	__in			size_t size,
	__out_opt		boolean_t *cksummedp)
{
	efx_vpd_tag_t tag;
	efx_vpd_keyword_t keyword;
	unsigned int offset;
	unsigned int pos;
	unsigned int i;
	uint16_t taglen;
	uint8_t keylen;
	uint8_t cksum;
	boolean_t cksummed = B_FALSE;
	efx_rc_t rc;

	/*
	 * Parse every tag,keyword in the existing VPD. If the csum is present,
	 * the assert it is correct, and is the final keyword in the RO block.
	 */
	offset = 0;
	_NOTE(CONSTANTCONDITION)
	while (1) {
		if ((rc = efx_vpd_next_tag(data, size, &offset,
		    &tag, &taglen)) != 0)
			goto fail1;
		if (tag == EFX_VPD_END)
			break;
		else if (tag == EFX_VPD_ID)
			goto done;

		for (pos = 0; pos != taglen; pos += 3 + keylen) {
			/* RV keyword must be the last in the block */
			if (cksummed) {
				rc = EFAULT;
				goto fail2;
			}

			if ((rc = efx_vpd_next_keyword(data + offset,
			    taglen, pos, &keyword, &keylen)) != 0)
				goto fail3;

			if (keyword == EFX_VPD_KEYWORD('R', 'V')) {
				cksum = 0;
				for (i = 0; i < offset + pos + 4; i++)
					cksum += data[i];

				if (cksum != 0) {
					rc = EFAULT;
					goto fail4;
				}

				cksummed = B_TRUE;
			}
		}

	done:
		offset += taglen;
	}

	if (!cksummed) {
		rc = EFAULT;
		goto fail5;
	}

	if (cksummedp != NULL)
		*cksummedp = cksummed;

	return (0);

fail5:
	EFSYS_PROBE(fail5);
fail4:
	EFSYS_PROBE(fail4);
fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	uint8_t	__efx_vpd_blank_pid[] = {
	/* Large resource type ID length 1 */
	0x82, 0x01, 0x00,
	/* Product name ' ' */
	0x32,
};

static uint8_t __efx_vpd_blank_r[] = {
	/* Large resource type VPD-R length 4 */
	0x90, 0x04, 0x00,
	/* RV keyword length 1 */
	'R', 'V', 0x01,
	/* RV payload checksum */
	0x00,
};

	__checkReturn		efx_rc_t
efx_vpd_hunk_reinit(
	__in_bcount(size)	caddr_t data,
	__in			size_t size,
	__in			boolean_t wantpid)
{
	unsigned int offset = 0;
	unsigned int pos;
	efx_byte_t byte;
	uint8_t cksum;
	efx_rc_t rc;

	if (size < 0x100) {
		rc = ENOSPC;
		goto fail1;
	}

	if (wantpid) {
		memcpy(data + offset, __efx_vpd_blank_pid,
		    sizeof (__efx_vpd_blank_pid));
		offset += sizeof (__efx_vpd_blank_pid);
	}

	memcpy(data + offset, __efx_vpd_blank_r, sizeof (__efx_vpd_blank_r));
	offset += sizeof (__efx_vpd_blank_r);

	/* Update checksum */
	cksum = 0;
	for (pos = 0; pos < offset; pos++)
		cksum += data[pos];
	data[offset - 1] -= cksum;

	/* Append trailing tag */
	EFX_POPULATE_BYTE_3(byte,
			    TAG_TYPE, TAG_TYPE_SMALL_ITEM_DECODE,
			    TAG_SMALL_ITEM_NAME, TAG_NAME_END_DECODE,
			    TAG_SMALL_ITEM_SIZE, 0);
	data[offset] = EFX_BYTE_FIELD(byte, EFX_BYTE_0);
	offset++;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn			efx_rc_t
efx_vpd_hunk_next(
	__in_bcount(size)		caddr_t data,
	__in				size_t size,
	__out				efx_vpd_tag_t *tagp,
	__out				efx_vpd_keyword_t *keywordp,
	__out_opt			unsigned int *payloadp,
	__out_opt			uint8_t *paylenp,
	__inout				unsigned int *contp)
{
	efx_vpd_tag_t tag;
	efx_vpd_keyword_t keyword = 0;
	unsigned int offset;
	unsigned int pos;
	unsigned int index;
	uint16_t taglen;
	uint8_t keylen;
	uint8_t paylen;
	efx_rc_t rc;

	offset = index = 0;
	_NOTE(CONSTANTCONDITION)
	while (1) {
		if ((rc = efx_vpd_next_tag(data, size, &offset,
		    &tag, &taglen)) != 0)
			goto fail1;

		if (tag == EFX_VPD_END) {
			keyword = 0;
			paylen = 0;
			index = 0;
			break;
		}

		if (tag == EFX_VPD_ID) {
			if (index++ == *contp) {
				EFSYS_ASSERT3U(taglen, <, 0x100);
				keyword = 0;
				paylen = (uint8_t)MIN(taglen, 0xff);

				goto done;
			}
		} else {
			for (pos = 0; pos != taglen; pos += 3 + keylen) {
				if ((rc = efx_vpd_next_keyword(data + offset,
				    taglen, pos, &keyword, &keylen)) != 0)
					goto fail2;

				if (index++ == *contp) {
					offset += pos + 3;
					paylen = keylen;

					goto done;
				}
			}
		}

		offset += taglen;
	}

done:
	*tagp = tag;
	*keywordp = keyword;
	if (payloadp != NULL)
		*payloadp = offset;
	if (paylenp != NULL)
		*paylenp = paylen;

	*contp = index;
	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_hunk_get(
	__in_bcount(size)	caddr_t data,
	__in			size_t size,
	__in			efx_vpd_tag_t tag,
	__in			efx_vpd_keyword_t keyword,
	__out			unsigned int *payloadp,
	__out			uint8_t *paylenp)
{
	efx_vpd_tag_t itag;
	efx_vpd_keyword_t ikeyword;
	unsigned int offset;
	unsigned int pos;
	uint16_t taglen;
	uint8_t keylen;
	efx_rc_t rc;

	offset = 0;
	_NOTE(CONSTANTCONDITION)
	while (1) {
		if ((rc = efx_vpd_next_tag(data, size, &offset,
		    &itag, &taglen)) != 0)
			goto fail1;
		if (itag == EFX_VPD_END)
			break;

		if (itag == tag) {
			if (itag == EFX_VPD_ID) {
				EFSYS_ASSERT3U(taglen, <, 0x100);

				*paylenp = (uint8_t)MIN(taglen, 0xff);
				*payloadp = offset;
				return (0);
			}

			for (pos = 0; pos != taglen; pos += 3 + keylen) {
				if ((rc = efx_vpd_next_keyword(data + offset,
				    taglen, pos, &ikeyword, &keylen)) != 0)
					goto fail2;

				if (ikeyword == keyword) {
					*paylenp = keylen;
					*payloadp = offset + pos + 3;
					return (0);
				}
			}
		}

		offset += taglen;
	}

	/* Not an error */
	return (ENOENT);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn		efx_rc_t
efx_vpd_hunk_set(
	__in_bcount(size)	caddr_t data,
	__in			size_t size,
	__in			efx_vpd_value_t *evvp)
{
	efx_word_t word;
	efx_vpd_tag_t tag;
	efx_vpd_keyword_t keyword;
	unsigned int offset;
	unsigned int pos;
	unsigned int taghead;
	unsigned int source;
	unsigned int dest;
	unsigned int i;
	uint16_t taglen;
	uint8_t keylen;
	uint8_t cksum;
	size_t used;
	efx_rc_t rc;

	switch (evvp->evv_tag) {
	case EFX_VPD_ID:
		if (evvp->evv_keyword != 0) {
			rc = EINVAL;
			goto fail1;
		}

		/* Can't delete the ID keyword */
		if (evvp->evv_length == 0) {
			rc = EINVAL;
			goto fail1;
		}
		break;

	case EFX_VPD_RO:
		if (evvp->evv_keyword == EFX_VPD_KEYWORD('R', 'V')) {
			rc = EINVAL;
			goto fail1;
		}
		break;

	default:
		rc = EINVAL;
		goto fail1;
	}

	/* Determine total size of all current tags */
	if ((rc = efx_vpd_hunk_length(data, size, &used)) != 0)
		goto fail2;

	offset = 0;
	_NOTE(CONSTANTCONDITION)
	while (1) {
		taghead = offset;
		if ((rc = efx_vpd_next_tag(data, size, &offset,
		    &tag, &taglen)) != 0)
			goto fail3;
		if (tag == EFX_VPD_END)
			break;
		else if (tag != evvp->evv_tag) {
			offset += taglen;
			continue;
		}

		/* We only support modifying large resource tags */
		if (offset - taghead != 3) {
			rc = EINVAL;
			goto fail4;
		}

		/*
		 * Work out the offset of the byte immediately after the
		 * old (=source) and new (=dest) new keyword/tag
		 */
		pos = 0;
		if (tag == EFX_VPD_ID) {
			source = offset + taglen;
			dest = offset + evvp->evv_length;
			goto check_space;
		}

		EFSYS_ASSERT3U(tag, ==, EFX_VPD_RO);
		source = dest = 0;
		for (pos = 0; pos != taglen; pos += 3 + keylen) {
			if ((rc = efx_vpd_next_keyword(data + offset,
			    taglen, pos, &keyword, &keylen)) != 0)
				goto fail5;

			if (keyword == evvp->evv_keyword &&
			    evvp->evv_length == 0) {
				/* Deleting this keyword */
				source = offset + pos + 3 + keylen;
				dest = offset + pos;
				break;

			} else if (keyword == evvp->evv_keyword) {
				/* Adjusting this keyword */
				source = offset + pos + 3 + keylen;
				dest = offset + pos + 3 + evvp->evv_length;
				break;

			} else if (keyword == EFX_VPD_KEYWORD('R', 'V')) {
				/* The RV keyword must be at the end */
				EFSYS_ASSERT3U(pos + 3 + keylen, ==, taglen);

				/*
				 * The keyword doesn't already exist. If the
				 * user deleting a non-existant keyword then
				 * this is a no-op.
				 */
				if (evvp->evv_length == 0)
					return (0);

				/* Insert this keyword before the RV keyword */
				source = offset + pos;
				dest = offset + pos + 3 + evvp->evv_length;
				break;
			}
		}

	check_space:
		if (used + dest > size + source) {
			rc = ENOSPC;
			goto fail6;
		}

		/* Move trailing data */
		(void) memmove(data + dest, data + source, used - source);

		/* Copy contents */
		memcpy(data + dest - evvp->evv_length, evvp->evv_value,
		    evvp->evv_length);

		/* Insert new keyword header if required */
		if (tag != EFX_VPD_ID && evvp->evv_length > 0) {
			EFX_POPULATE_WORD_1(word, EFX_WORD_0,
					    evvp->evv_keyword);
			data[offset + pos + 0] =
			    EFX_WORD_FIELD(word, EFX_BYTE_0);
			data[offset + pos + 1] =
			    EFX_WORD_FIELD(word, EFX_BYTE_1);
			data[offset + pos + 2] = evvp->evv_length;
		}

		/* Modify tag length (large resource type) */
		taglen += (uint16_t)(dest - source);
		EFX_POPULATE_WORD_1(word, EFX_WORD_0, taglen);
		data[offset - 2] = EFX_WORD_FIELD(word, EFX_BYTE_0);
		data[offset - 1] = EFX_WORD_FIELD(word, EFX_BYTE_1);

		goto checksum;
	}

	/* Unable to find the matching tag */
	rc = ENOENT;
	goto fail7;

checksum:
	/* Find the RV tag, and update the checksum */
	offset = 0;
	_NOTE(CONSTANTCONDITION)
	while (1) {
		if ((rc = efx_vpd_next_tag(data, size, &offset,
		    &tag, &taglen)) != 0)
			goto fail8;
		if (tag == EFX_VPD_END)
			break;
		if (tag == EFX_VPD_RO) {
			for (pos = 0; pos != taglen; pos += 3 + keylen) {
				if ((rc = efx_vpd_next_keyword(data + offset,
				    taglen, pos, &keyword, &keylen)) != 0)
					goto fail9;

				if (keyword == EFX_VPD_KEYWORD('R', 'V')) {
					cksum = 0;
					for (i = 0; i < offset + pos + 3; i++)
						cksum += data[i];
					data[i] = -cksum;
					break;
				}
			}
		}

		offset += taglen;
	}

	/* Zero out the unused portion */
	(void) memset(data + offset + taglen, 0xff, size - offset - taglen);

	return (0);

fail9:
	EFSYS_PROBE(fail9);
fail8:
	EFSYS_PROBE(fail8);
fail7:
	EFSYS_PROBE(fail7);
fail6:
	EFSYS_PROBE(fail6);
fail5:
	EFSYS_PROBE(fail5);
fail4:
	EFSYS_PROBE(fail4);
fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

				void
efx_vpd_fini(
	__in			efx_nic_t *enp)
{
	const efx_vpd_ops_t *evpdop = enp->en_evpdop;

	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_VPD);

	if (evpdop->evpdo_fini != NULL)
		evpdop->evpdo_fini(enp);

	enp->en_evpdop = NULL;
	enp->en_mod_flags &= ~EFX_MOD_VPD;
}

#endif	/* EFSYS_OPT_VPD */