1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2012-2018 Solarflare Communications Inc.
10 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
12 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
14 #include "ef10_tlv_layout.h"
16 /* Cursor for TLV partition format */
17 typedef struct tlv_cursor_s
{
18 uint32_t *block
; /* Base of data block */
19 uint32_t *current
; /* Cursor position */
20 uint32_t *end
; /* End tag position */
21 uint32_t *limit
; /* Last dword of data block */
24 typedef struct nvram_partition_s
{
29 * The full length of the NVRAM partition.
30 * This is different from tlv_partition_header.total_length,
31 * which can be smaller.
36 tlv_cursor_t tlv_cursor
;
40 static __checkReturn efx_rc_t
42 __inout tlv_cursor_t
*cursor
);
47 __out
uint32_t *block
)
49 *block
= __CPU_TO_LE_32(TLV_TAG_END
);
54 __in tlv_cursor_t
*cursor
)
58 dword
= cursor
->current
[0];
59 tag
= __LE_TO_CPU_32(dword
);
66 __in tlv_cursor_t
*cursor
)
68 uint32_t dword
, length
;
70 if (tlv_tag(cursor
) == TLV_TAG_END
)
73 dword
= cursor
->current
[1];
74 length
= __LE_TO_CPU_32(dword
);
76 return ((size_t)length
);
81 __in tlv_cursor_t
*cursor
)
83 if (tlv_tag(cursor
) == TLV_TAG_END
)
86 return ((uint8_t *)(&cursor
->current
[2]));
91 __in tlv_cursor_t
*cursor
)
93 if (tlv_tag(cursor
) == TLV_TAG_END
)
96 return ((uint8_t *)cursor
->current
);
100 * TLV item DWORD length is tag + length + value (rounded up to DWORD)
101 * equivalent to tlv_n_words_for_len in mc-comms tlv.c
103 #define TLV_DWORD_COUNT(length) \
104 (1 + 1 + (((length) + sizeof (uint32_t) - 1) / sizeof (uint32_t)))
109 __in tlv_cursor_t
*cursor
)
113 length
= tlv_length(cursor
);
115 return (cursor
->current
+ TLV_DWORD_COUNT(length
));
118 static __checkReturn efx_rc_t
120 __inout tlv_cursor_t
*cursor
)
124 if ((rc
= tlv_validate_state(cursor
)) != 0)
127 if (cursor
->current
== cursor
->end
) {
128 /* No more tags after END tag */
129 cursor
->current
= NULL
;
134 /* Advance to next item and validate */
135 cursor
->current
= tlv_next_item_ptr(cursor
);
137 if ((rc
= tlv_validate_state(cursor
)) != 0)
147 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
154 __in tlv_cursor_t
*cursor
)
158 cursor
->current
= cursor
->block
;
160 if ((rc
= tlv_validate_state(cursor
)) != 0)
166 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
173 __inout tlv_cursor_t
*cursor
,
178 rc
= tlv_rewind(cursor
);
180 if (tlv_tag(cursor
) == tag
)
183 rc
= tlv_advance(cursor
);
188 static __checkReturn efx_rc_t
190 __inout tlv_cursor_t
*cursor
)
194 /* Check cursor position */
195 if (cursor
->current
< cursor
->block
) {
199 if (cursor
->current
> cursor
->limit
) {
204 if (tlv_tag(cursor
) != TLV_TAG_END
) {
205 /* Check current item has space for tag and length */
206 if (cursor
->current
> (cursor
->limit
- 2)) {
207 cursor
->current
= NULL
;
212 /* Check we have value data for current item and another tag */
213 if (tlv_next_item_ptr(cursor
) > (cursor
->limit
- 1)) {
214 cursor
->current
= NULL
;
229 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
236 __out tlv_cursor_t
*cursor
,
237 __in
uint32_t *block
,
238 __in
uint32_t *limit
,
239 __in
uint32_t *current
)
241 cursor
->block
= block
;
242 cursor
->limit
= limit
;
244 cursor
->current
= current
;
247 return (tlv_validate_state(cursor
));
250 static __checkReturn efx_rc_t
251 tlv_init_cursor_from_size(
252 __out tlv_cursor_t
*cursor
,
258 limit
= (uint32_t *)(block
+ size
- sizeof (uint32_t));
259 return (tlv_init_cursor(cursor
, (uint32_t *)block
,
260 limit
, (uint32_t *)block
));
263 static __checkReturn efx_rc_t
264 tlv_init_cursor_at_offset(
265 __out tlv_cursor_t
*cursor
,
273 limit
= (uint32_t *)(block
+ size
- sizeof (uint32_t));
274 current
= (uint32_t *)(block
+ offset
);
275 return (tlv_init_cursor(cursor
, (uint32_t *)block
, limit
, current
));
278 static __checkReturn efx_rc_t
280 __inout tlv_cursor_t
*cursor
)
285 if (cursor
->end
== NULL
) {
286 pos
= cursor
->current
;
287 if ((rc
= tlv_find(cursor
, TLV_TAG_END
)) != 0)
290 cursor
->end
= cursor
->current
;
291 cursor
->current
= pos
;
297 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
303 tlv_block_length_used(
304 __inout tlv_cursor_t
*cursor
)
308 if ((rc
= tlv_validate_state(cursor
)) != 0)
311 if ((rc
= tlv_require_end(cursor
)) != 0)
314 /* Return space used (including the END tag) */
315 return (cursor
->end
+ 1 - cursor
->block
) * sizeof (uint32_t);
320 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
326 tlv_last_segment_end(
327 __in tlv_cursor_t
*cursor
)
329 tlv_cursor_t segment_cursor
;
330 uint32_t *last_segment_end
= cursor
->block
;
331 uint32_t *segment_start
= cursor
->block
;
334 * Go through each segment and check that it has an end tag. If there
335 * is no end tag then the previous segment was the last valid one,
336 * so return the pointer to its end tag.
339 if (tlv_init_cursor(&segment_cursor
, segment_start
,
340 cursor
->limit
, segment_start
) != 0)
342 if (tlv_require_end(&segment_cursor
) != 0)
344 last_segment_end
= segment_cursor
.end
;
345 segment_start
= segment_cursor
.end
+ 1;
348 return (last_segment_end
);
354 __in tlv_cursor_t
*cursor
,
356 __in_bcount(size
) uint8_t *data
,
362 ptr
= cursor
->current
;
364 *ptr
++ = __CPU_TO_LE_32(tag
);
365 *ptr
++ = __CPU_TO_LE_32(len
);
368 ptr
[(len
- 1) / sizeof (uint32_t)] = 0;
369 memcpy(ptr
, data
, len
);
370 ptr
+= P2ROUNDUP(len
, sizeof (uint32_t)) / sizeof (*ptr
);
376 static __checkReturn efx_rc_t
378 __inout tlv_cursor_t
*cursor
,
385 uint32_t *last_segment_end
;
388 if ((rc
= tlv_validate_state(cursor
)) != 0)
391 if ((rc
= tlv_require_end(cursor
)) != 0)
394 if (tag
== TLV_TAG_END
) {
399 last_segment_end
= tlv_last_segment_end(cursor
);
401 delta
= TLV_DWORD_COUNT(size
);
402 if (last_segment_end
+ 1 + delta
> cursor
->limit
) {
407 /* Move data up: new space at cursor->current */
408 memmove(cursor
->current
+ delta
, cursor
->current
,
409 (last_segment_end
+ 1 - cursor
->current
) * sizeof (uint32_t));
411 /* Adjust the end pointer */
412 cursor
->end
+= delta
;
414 /* Write new TLV item */
415 tlv_write(cursor
, tag
, data
, size
);
426 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
431 static __checkReturn efx_rc_t
433 __inout tlv_cursor_t
*cursor
)
436 uint32_t *last_segment_end
;
439 if ((rc
= tlv_validate_state(cursor
)) != 0)
442 if (tlv_tag(cursor
) == TLV_TAG_END
) {
447 delta
= TLV_DWORD_COUNT(tlv_length(cursor
));
449 if ((rc
= tlv_require_end(cursor
)) != 0)
452 last_segment_end
= tlv_last_segment_end(cursor
);
454 /* Shuffle things down, destroying the item at cursor->current */
455 memmove(cursor
->current
, cursor
->current
+ delta
,
456 (last_segment_end
+ 1 - cursor
->current
) * sizeof (uint32_t));
457 /* Zero the new space at the end of the TLV chain */
458 memset(last_segment_end
+ 1 - delta
, 0, delta
* sizeof (uint32_t));
459 /* Adjust the end pointer */
460 cursor
->end
-= delta
;
469 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
474 static __checkReturn efx_rc_t
476 __inout tlv_cursor_t
*cursor
,
483 unsigned int old_ndwords
;
484 unsigned int new_ndwords
;
486 uint32_t *last_segment_end
;
489 if ((rc
= tlv_validate_state(cursor
)) != 0)
492 if (tlv_tag(cursor
) == TLV_TAG_END
) {
496 if (tlv_tag(cursor
) != tag
) {
501 old_ndwords
= TLV_DWORD_COUNT(tlv_length(cursor
));
502 new_ndwords
= TLV_DWORD_COUNT(size
);
504 if ((rc
= tlv_require_end(cursor
)) != 0)
507 last_segment_end
= tlv_last_segment_end(cursor
);
509 if (new_ndwords
> old_ndwords
) {
510 /* Expand space used for TLV item */
511 delta
= new_ndwords
- old_ndwords
;
512 pos
= cursor
->current
+ old_ndwords
;
514 if (last_segment_end
+ 1 + delta
> cursor
->limit
) {
519 /* Move up: new space at (cursor->current + old_ndwords) */
520 memmove(pos
+ delta
, pos
,
521 (last_segment_end
+ 1 - pos
) * sizeof (uint32_t));
523 /* Adjust the end pointer */
524 cursor
->end
+= delta
;
526 } else if (new_ndwords
< old_ndwords
) {
527 /* Shrink space used for TLV item */
528 delta
= old_ndwords
- new_ndwords
;
529 pos
= cursor
->current
+ new_ndwords
;
531 /* Move down: remove words at (cursor->current + new_ndwords) */
532 memmove(pos
, pos
+ delta
,
533 (last_segment_end
+ 1 - pos
) * sizeof (uint32_t));
535 /* Zero the new space at the end of the TLV chain */
536 memset(last_segment_end
+ 1 - delta
, 0,
537 delta
* sizeof (uint32_t));
539 /* Adjust the end pointer */
540 cursor
->end
-= delta
;
544 tlv_write(cursor
, tag
, data
, size
);
557 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
562 static uint32_t checksum_tlv_partition(
563 __in nvram_partition_t
*partition
)
565 tlv_cursor_t
*cursor
;
571 cursor
= &partition
->tlv_cursor
;
572 len
= tlv_block_length_used(cursor
);
573 EFSYS_ASSERT3U((len
& 3), ==, 0);
576 ptr
= partition
->data
;
577 end
= &ptr
[len
>> 2];
580 csum
+= __LE_TO_CPU_32(*ptr
++);
585 static __checkReturn efx_rc_t
586 tlv_update_partition_len_and_cks(
587 __in tlv_cursor_t
*cursor
)
590 nvram_partition_t partition
;
591 struct tlv_partition_header
*header
;
592 struct tlv_partition_trailer
*trailer
;
596 * We just modified the partition, so the total length may not be
597 * valid. Don't use tlv_find(), which performs some sanity checks
598 * that may fail here.
600 partition
.data
= cursor
->block
;
601 memcpy(&partition
.tlv_cursor
, cursor
, sizeof (*cursor
));
602 header
= (struct tlv_partition_header
*)partition
.data
;
604 if (__LE_TO_CPU_32(header
->tag
) != TLV_TAG_PARTITION_HEADER
) {
608 new_len
= tlv_block_length_used(&partition
.tlv_cursor
);
613 header
->total_length
= __CPU_TO_LE_32(new_len
);
614 /* Ensure the modified partition always has a new generation count. */
615 header
->generation
= __CPU_TO_LE_32(
616 __LE_TO_CPU_32(header
->generation
) + 1);
618 trailer
= (struct tlv_partition_trailer
*)((uint8_t *)header
+
619 new_len
- sizeof (*trailer
) - sizeof (uint32_t));
620 trailer
->generation
= header
->generation
;
621 trailer
->checksum
= __CPU_TO_LE_32(
622 __LE_TO_CPU_32(trailer
->checksum
) -
623 checksum_tlv_partition(&partition
));
630 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
635 /* Validate buffer contents (before writing to flash) */
636 __checkReturn efx_rc_t
637 ef10_nvram_buffer_validate(
640 __in_bcount(partn_size
) caddr_t partn_data
,
641 __in
size_t partn_size
)
644 struct tlv_partition_header
*header
;
645 struct tlv_partition_trailer
*trailer
;
651 _NOTE(ARGUNUSED(enp
, partn
))
652 EFX_STATIC_ASSERT(sizeof (*header
) <= EF10_NVRAM_CHUNK
);
654 if ((partn_data
== NULL
) || (partn_size
== 0)) {
659 /* The partition header must be the first item (at offset zero) */
660 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)partn_data
,
665 if (tlv_tag(&cursor
) != TLV_TAG_PARTITION_HEADER
) {
669 header
= (struct tlv_partition_header
*)tlv_item(&cursor
);
671 /* Check TLV partition length (includes the END tag) */
672 total_length
= __LE_TO_CPU_32(header
->total_length
);
673 if (total_length
> partn_size
) {
678 /* Check partition ends with PARTITION_TRAILER and END tags */
679 if ((rc
= tlv_find(&cursor
, TLV_TAG_PARTITION_TRAILER
)) != 0) {
683 trailer
= (struct tlv_partition_trailer
*)tlv_item(&cursor
);
685 if ((rc
= tlv_advance(&cursor
)) != 0) {
689 if (tlv_tag(&cursor
) != TLV_TAG_END
) {
694 /* Check generation counts are consistent */
695 if (trailer
->generation
!= header
->generation
) {
700 /* Verify partition checksum */
702 for (pos
= 0; (size_t)pos
< total_length
; pos
+= sizeof (uint32_t)) {
703 cksum
+= *((uint32_t *)(partn_data
+ pos
));
729 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
736 __checkReturn efx_rc_t
737 ef10_nvram_buffer_create(
739 __in
uint16_t partn_type
,
740 __in_bcount(partn_size
) caddr_t partn_data
,
741 __in
size_t partn_size
)
743 uint32_t *buf
= (uint32_t *)partn_data
;
746 struct tlv_partition_header header
;
747 struct tlv_partition_trailer trailer
;
749 unsigned int min_buf_size
= sizeof (struct tlv_partition_header
) +
750 sizeof (struct tlv_partition_trailer
);
751 if (partn_size
< min_buf_size
) {
756 memset(buf
, 0xff, partn_size
);
759 if ((rc
= tlv_init_cursor(&cursor
, buf
,
760 (uint32_t *)((uint8_t *)buf
+ partn_size
),
765 header
.tag
= __CPU_TO_LE_32(TLV_TAG_PARTITION_HEADER
);
766 header
.length
= __CPU_TO_LE_32(sizeof (header
) - 8);
767 header
.type_id
= __CPU_TO_LE_16(partn_type
);
769 header
.generation
= __CPU_TO_LE_32(1);
770 header
.total_length
= 0; /* This will be fixed below. */
771 if ((rc
= tlv_insert(
772 &cursor
, TLV_TAG_PARTITION_HEADER
,
773 (uint8_t *)&header
.type_id
, sizeof (header
) - 8)) != 0)
775 if ((rc
= tlv_advance(&cursor
)) != 0)
778 trailer
.tag
= __CPU_TO_LE_32(TLV_TAG_PARTITION_TRAILER
);
779 trailer
.length
= __CPU_TO_LE_32(sizeof (trailer
) - 8);
780 trailer
.generation
= header
.generation
;
781 trailer
.checksum
= 0; /* This will be fixed below. */
782 if ((rc
= tlv_insert(&cursor
, TLV_TAG_PARTITION_TRAILER
,
783 (uint8_t *)&trailer
.generation
, sizeof (trailer
) - 8)) != 0)
786 if ((rc
= tlv_update_partition_len_and_cks(&cursor
)) != 0)
789 /* Check that the partition is valid. */
790 if ((rc
= ef10_nvram_buffer_validate(enp
, partn_type
,
791 partn_data
, partn_size
)) != 0)
809 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
816 __in
uint32_t *position
,
819 return (uint32_t)((uint8_t *)position
- (uint8_t *)base
);
822 __checkReturn efx_rc_t
823 ef10_nvram_buffer_find_item_start(
824 __in_bcount(buffer_size
)
826 __in
size_t buffer_size
,
827 __out
uint32_t *startp
)
829 /* Read past partition header to find start address of the first key */
833 /* A PARTITION_HEADER tag must be the first item (at offset zero) */
834 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)bufferp
,
835 buffer_size
)) != 0) {
839 if (tlv_tag(&cursor
) != TLV_TAG_PARTITION_HEADER
) {
844 if ((rc
= tlv_advance(&cursor
)) != 0) {
848 *startp
= byte_offset(cursor
.current
, cursor
.block
);
850 if ((rc
= tlv_require_end(&cursor
)) != 0)
862 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
867 __checkReturn efx_rc_t
868 ef10_nvram_buffer_find_end(
869 __in_bcount(buffer_size
)
871 __in
size_t buffer_size
,
872 __in
uint32_t offset
,
873 __out
uint32_t *endp
)
875 /* Read to end of partition */
878 uint32_t *segment_used
;
880 _NOTE(ARGUNUSED(offset
))
882 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)bufferp
,
883 buffer_size
)) != 0) {
888 segment_used
= cursor
.block
;
891 * Go through each segment and check that it has an end tag. If there
892 * is no end tag then the previous segment was the last valid one,
893 * so return the used space including that end tag.
895 while (tlv_tag(&cursor
) == TLV_TAG_PARTITION_HEADER
) {
896 if (tlv_require_end(&cursor
) != 0) {
897 if (segment_used
== cursor
.block
) {
899 * First segment is corrupt, so there is
900 * no valid data in partition.
907 segment_used
= cursor
.end
+ 1;
909 cursor
.current
= segment_used
;
911 /* Return space used (including the END tag) */
912 *endp
= (segment_used
- cursor
.block
) * sizeof (uint32_t);
919 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
924 __checkReturn
__success(return != B_FALSE
) boolean_t
925 ef10_nvram_buffer_find_item(
926 __in_bcount(buffer_size
)
928 __in
size_t buffer_size
,
929 __in
uint32_t offset
,
930 __out
uint32_t *startp
,
931 __out
uint32_t *lengthp
)
933 /* Find TLV at offset and return key start and length */
938 if (tlv_init_cursor_at_offset(&cursor
, (uint8_t *)bufferp
,
939 buffer_size
, offset
) != 0) {
943 while ((key
= tlv_item(&cursor
)) != NULL
) {
944 tag
= tlv_tag(&cursor
);
945 if (tag
== TLV_TAG_PARTITION_HEADER
||
946 tag
== TLV_TAG_PARTITION_TRAILER
) {
947 if (tlv_advance(&cursor
) != 0) {
952 *startp
= byte_offset(cursor
.current
, cursor
.block
);
953 *lengthp
= byte_offset(tlv_next_item_ptr(&cursor
),
961 __checkReturn efx_rc_t
962 ef10_nvram_buffer_get_item(
963 __in_bcount(buffer_size
)
965 __in
size_t buffer_size
,
966 __in
uint32_t offset
,
967 __in
uint32_t length
,
968 __out_bcount_part(item_max_size
, *lengthp
)
970 __in
size_t item_max_size
,
971 __out
uint32_t *lengthp
)
975 uint32_t item_length
;
977 if (item_max_size
< length
) {
982 if ((rc
= tlv_init_cursor_at_offset(&cursor
, (uint8_t *)bufferp
,
983 buffer_size
, offset
)) != 0) {
987 item_length
= tlv_length(&cursor
);
988 if (length
< item_length
) {
992 memcpy(itemp
, tlv_value(&cursor
), item_length
);
994 *lengthp
= item_length
;
1003 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1008 __checkReturn efx_rc_t
1009 ef10_nvram_buffer_insert_item(
1010 __in_bcount(buffer_size
)
1012 __in
size_t buffer_size
,
1013 __in
uint32_t offset
,
1014 __in_bcount(length
) caddr_t keyp
,
1015 __in
uint32_t length
,
1016 __out
uint32_t *lengthp
)
1019 tlv_cursor_t cursor
;
1021 if ((rc
= tlv_init_cursor_at_offset(&cursor
, (uint8_t *)bufferp
,
1022 buffer_size
, offset
)) != 0) {
1026 rc
= tlv_insert(&cursor
, TLV_TAG_LICENSE
, (uint8_t *)keyp
, length
);
1032 *lengthp
= byte_offset(tlv_next_item_ptr(&cursor
),
1040 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1045 __checkReturn efx_rc_t
1046 ef10_nvram_buffer_delete_item(
1047 __in_bcount(buffer_size
)
1049 __in
size_t buffer_size
,
1050 __in
uint32_t offset
,
1051 __in
uint32_t length
,
1055 tlv_cursor_t cursor
;
1057 _NOTE(ARGUNUSED(length
, end
))
1059 if ((rc
= tlv_init_cursor_at_offset(&cursor
, (uint8_t *)bufferp
,
1060 buffer_size
, offset
)) != 0) {
1064 if ((rc
= tlv_delete(&cursor
)) != 0)
1072 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1077 __checkReturn efx_rc_t
1078 ef10_nvram_buffer_finish(
1079 __in_bcount(buffer_size
)
1081 __in
size_t buffer_size
)
1084 tlv_cursor_t cursor
;
1086 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)bufferp
,
1087 buffer_size
)) != 0) {
1092 if ((rc
= tlv_require_end(&cursor
)) != 0)
1095 if ((rc
= tlv_update_partition_len_and_cks(&cursor
)) != 0)
1105 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1113 * Read and validate a segment from a partition. A segment is a complete
1114 * tlv chain between PARTITION_HEADER and PARTITION_END tags. There may
1115 * be multiple segments in a partition, so seg_offset allows segments
1116 * beyond the first to be read.
1118 static __checkReturn efx_rc_t
1119 ef10_nvram_read_tlv_segment(
1120 __in efx_nic_t
*enp
,
1121 __in
uint32_t partn
,
1122 __in
size_t seg_offset
,
1123 __in_bcount(max_seg_size
) caddr_t seg_data
,
1124 __in
size_t max_seg_size
)
1126 tlv_cursor_t cursor
;
1127 struct tlv_partition_header
*header
;
1128 struct tlv_partition_trailer
*trailer
;
1129 size_t total_length
;
1134 EFX_STATIC_ASSERT(sizeof (*header
) <= EF10_NVRAM_CHUNK
);
1136 if ((seg_data
== NULL
) || (max_seg_size
== 0)) {
1141 /* Read initial chunk of the segment, starting at offset */
1142 if ((rc
= ef10_nvram_partn_read_mode(enp
, partn
, seg_offset
, seg_data
,
1144 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT
)) != 0) {
1148 /* A PARTITION_HEADER tag must be the first item at the given offset */
1149 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)seg_data
,
1150 max_seg_size
)) != 0) {
1154 if (tlv_tag(&cursor
) != TLV_TAG_PARTITION_HEADER
) {
1158 header
= (struct tlv_partition_header
*)tlv_item(&cursor
);
1160 /* Check TLV segment length (includes the END tag) */
1161 total_length
= __LE_TO_CPU_32(header
->total_length
);
1162 if (total_length
> max_seg_size
) {
1167 /* Read the remaining segment content */
1168 if (total_length
> EF10_NVRAM_CHUNK
) {
1169 if ((rc
= ef10_nvram_partn_read_mode(enp
, partn
,
1170 seg_offset
+ EF10_NVRAM_CHUNK
,
1171 seg_data
+ EF10_NVRAM_CHUNK
,
1172 total_length
- EF10_NVRAM_CHUNK
,
1173 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT
)) != 0)
1177 /* Check segment ends with PARTITION_TRAILER and END tags */
1178 if ((rc
= tlv_find(&cursor
, TLV_TAG_PARTITION_TRAILER
)) != 0) {
1182 trailer
= (struct tlv_partition_trailer
*)tlv_item(&cursor
);
1184 if ((rc
= tlv_advance(&cursor
)) != 0) {
1188 if (tlv_tag(&cursor
) != TLV_TAG_END
) {
1193 /* Check data read from segment is consistent */
1194 if (trailer
->generation
!= header
->generation
) {
1196 * The partition data may have been modified between successive
1197 * MCDI NVRAM_READ requests by the MC or another PCI function.
1199 * The caller must retry to obtain consistent partition data.
1205 /* Verify segment checksum */
1207 for (pos
= 0; (size_t)pos
< total_length
; pos
+= sizeof (uint32_t)) {
1208 cksum
+= *((uint32_t *)(seg_data
+ pos
));
1218 EFSYS_PROBE(fail11
);
1220 EFSYS_PROBE(fail10
);
1238 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1244 * Read a single TLV item from a host memory
1245 * buffer containing a TLV formatted segment.
1247 __checkReturn efx_rc_t
1248 ef10_nvram_buf_read_tlv(
1249 __in efx_nic_t
*enp
,
1250 __in_bcount(max_seg_size
) caddr_t seg_data
,
1251 __in
size_t max_seg_size
,
1253 __deref_out_bcount_opt(*sizep
) caddr_t
*datap
,
1254 __out
size_t *sizep
)
1256 tlv_cursor_t cursor
;
1262 _NOTE(ARGUNUSED(enp
))
1264 if ((seg_data
== NULL
) || (max_seg_size
== 0)) {
1269 /* Find requested TLV tag in segment data */
1270 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)seg_data
,
1271 max_seg_size
)) != 0) {
1275 if ((rc
= tlv_find(&cursor
, tag
)) != 0) {
1279 value
= (caddr_t
)tlv_value(&cursor
);
1280 length
= tlv_length(&cursor
);
1285 /* Copy out data from TLV item */
1286 EFSYS_KMEM_ALLOC(enp
->en_esip
, length
, data
);
1291 memcpy(data
, value
, length
);
1306 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1311 /* Read a single TLV item from the first segment in a TLV formatted partition */
1312 __checkReturn efx_rc_t
1313 ef10_nvram_partn_read_tlv(
1314 __in efx_nic_t
*enp
,
1315 __in
uint32_t partn
,
1317 __deref_out_bcount_opt(*seg_sizep
) caddr_t
*seg_datap
,
1318 __out
size_t *seg_sizep
)
1320 caddr_t seg_data
= NULL
;
1321 size_t partn_size
= 0;
1327 /* Allocate sufficient memory for the entire partition */
1328 if ((rc
= ef10_nvram_partn_size(enp
, partn
, &partn_size
)) != 0)
1331 if (partn_size
== 0) {
1336 EFSYS_KMEM_ALLOC(enp
->en_esip
, partn_size
, seg_data
);
1337 if (seg_data
== NULL
) {
1343 * Read the first segment in a TLV partition. Retry until consistent
1344 * segment contents are returned. Inconsistent data may be read if:
1345 * a) the segment contents are invalid
1346 * b) the MC has rebooted while we were reading the partition
1347 * c) the partition has been modified while we were reading it
1348 * Limit retry attempts to ensure forward progress.
1352 if ((rc
= ef10_nvram_read_tlv_segment(enp
, partn
, 0,
1353 seg_data
, partn_size
)) != 0)
1355 } while ((rc
== EAGAIN
) && (retry
> 0));
1358 /* Failed to obtain consistent segment data */
1365 if ((rc
= ef10_nvram_buf_read_tlv(enp
, seg_data
, partn_size
,
1366 tag
, &data
, &length
)) != 0)
1369 EFSYS_KMEM_FREE(enp
->en_esip
, partn_size
, seg_data
);
1372 *seg_sizep
= length
;
1381 EFSYS_KMEM_FREE(enp
->en_esip
, partn_size
, seg_data
);
1387 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1392 /* Compute the size of a segment. */
1393 static __checkReturn efx_rc_t
1394 ef10_nvram_buf_segment_size(
1395 __in caddr_t seg_data
,
1396 __in
size_t max_seg_size
,
1397 __out
size_t *seg_sizep
)
1400 tlv_cursor_t cursor
;
1401 struct tlv_partition_header
*header
;
1404 uint32_t *end_tag_position
;
1405 uint32_t segment_length
;
1407 /* A PARTITION_HEADER tag must be the first item at the given offset */
1408 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)seg_data
,
1409 max_seg_size
)) != 0) {
1413 if (tlv_tag(&cursor
) != TLV_TAG_PARTITION_HEADER
) {
1417 header
= (struct tlv_partition_header
*)tlv_item(&cursor
);
1419 /* Check TLV segment length (includes the END tag) */
1420 *seg_sizep
= __LE_TO_CPU_32(header
->total_length
);
1421 if (*seg_sizep
> max_seg_size
) {
1426 /* Check segment ends with PARTITION_TRAILER and END tags */
1427 if ((rc
= tlv_find(&cursor
, TLV_TAG_PARTITION_TRAILER
)) != 0) {
1432 if ((rc
= tlv_advance(&cursor
)) != 0) {
1436 if (tlv_tag(&cursor
) != TLV_TAG_END
) {
1440 end_tag_position
= cursor
.current
;
1442 /* Verify segment checksum */
1444 for (pos
= 0; (size_t)pos
< *seg_sizep
; pos
+= sizeof (uint32_t)) {
1445 cksum
+= *((uint32_t *)(seg_data
+ pos
));
1453 * Calculate total length from HEADER to END tags and compare to
1454 * max_seg_size and the total_length field in the HEADER tag.
1456 segment_length
= tlv_block_length_used(&cursor
);
1458 if (segment_length
> max_seg_size
) {
1463 if (segment_length
!= *seg_sizep
) {
1468 /* Skip over the first HEADER tag. */
1469 rc
= tlv_rewind(&cursor
);
1470 rc
= tlv_advance(&cursor
);
1473 if (tlv_tag(&cursor
) == TLV_TAG_END
) {
1474 /* Check that the END tag is the one found earlier. */
1475 if (cursor
.current
!= end_tag_position
)
1479 /* Check for duplicate HEADER tags before the END tag. */
1480 if (tlv_tag(&cursor
) == TLV_TAG_PARTITION_HEADER
) {
1485 rc
= tlv_advance(&cursor
);
1493 EFSYS_PROBE(fail12
);
1495 EFSYS_PROBE(fail11
);
1497 EFSYS_PROBE(fail10
);
1515 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1521 * Add or update a single TLV item in a host memory buffer containing a TLV
1522 * formatted segment. Historically partitions consisted of only one segment.
1524 __checkReturn efx_rc_t
1525 ef10_nvram_buf_write_tlv(
1526 __inout_bcount(max_seg_size
) caddr_t seg_data
,
1527 __in
size_t max_seg_size
,
1529 __in_bcount(tag_size
) caddr_t tag_data
,
1530 __in
size_t tag_size
,
1531 __out
size_t *total_lengthp
)
1533 tlv_cursor_t cursor
;
1534 struct tlv_partition_header
*header
;
1535 struct tlv_partition_trailer
*trailer
;
1536 uint32_t generation
;
1541 /* A PARTITION_HEADER tag must be the first item (at offset zero) */
1542 if ((rc
= tlv_init_cursor_from_size(&cursor
, (uint8_t *)seg_data
,
1543 max_seg_size
)) != 0) {
1547 if (tlv_tag(&cursor
) != TLV_TAG_PARTITION_HEADER
) {
1551 header
= (struct tlv_partition_header
*)tlv_item(&cursor
);
1553 /* Update the TLV chain to contain the new data */
1554 if ((rc
= tlv_find(&cursor
, tag
)) == 0) {
1555 /* Modify existing TLV item */
1556 if ((rc
= tlv_modify(&cursor
, tag
,
1557 (uint8_t *)tag_data
, tag_size
)) != 0)
1560 /* Insert a new TLV item before the PARTITION_TRAILER */
1561 rc
= tlv_find(&cursor
, TLV_TAG_PARTITION_TRAILER
);
1566 if ((rc
= tlv_insert(&cursor
, tag
,
1567 (uint8_t *)tag_data
, tag_size
)) != 0) {
1573 /* Find the trailer tag */
1574 if ((rc
= tlv_find(&cursor
, TLV_TAG_PARTITION_TRAILER
)) != 0) {
1578 trailer
= (struct tlv_partition_trailer
*)tlv_item(&cursor
);
1580 /* Update PARTITION_HEADER and PARTITION_TRAILER fields */
1581 *total_lengthp
= tlv_block_length_used(&cursor
);
1582 if (*total_lengthp
> max_seg_size
) {
1586 generation
= __LE_TO_CPU_32(header
->generation
) + 1;
1588 header
->total_length
= __CPU_TO_LE_32(*total_lengthp
);
1589 header
->generation
= __CPU_TO_LE_32(generation
);
1590 trailer
->generation
= __CPU_TO_LE_32(generation
);
1592 /* Recompute PARTITION_TRAILER checksum */
1593 trailer
->checksum
= 0;
1595 for (pos
= 0; (size_t)pos
< *total_lengthp
; pos
+= sizeof (uint32_t)) {
1596 cksum
+= *((uint32_t *)(seg_data
+ pos
));
1598 trailer
->checksum
= ~cksum
+ 1;
1615 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1621 * Add or update a single TLV item in the first segment of a TLV formatted
1622 * dynamic config partition. The first segment is the current active
1625 __checkReturn efx_rc_t
1626 ef10_nvram_partn_write_tlv(
1627 __in efx_nic_t
*enp
,
1628 __in
uint32_t partn
,
1630 __in_bcount(size
) caddr_t data
,
1633 return ef10_nvram_partn_write_segment_tlv(enp
, partn
, tag
, data
,
1638 * Read a segment from nvram at the given offset into a buffer (segment_data)
1639 * and optionally write a new tag to it.
1641 static __checkReturn efx_rc_t
1642 ef10_nvram_segment_write_tlv(
1643 __in efx_nic_t
*enp
,
1644 __in
uint32_t partn
,
1646 __in_bcount(size
) caddr_t data
,
1648 __inout caddr_t
*seg_datap
,
1649 __inout
size_t *partn_offsetp
,
1650 __inout
size_t *src_remain_lenp
,
1651 __inout
size_t *dest_remain_lenp
,
1652 __in boolean_t write
)
1656 size_t original_segment_size
;
1657 size_t modified_segment_size
;
1660 * Read the segment from NVRAM into the segment_data buffer and validate
1661 * it, returning if it does not validate. This is not a failure unless
1662 * this is the first segment in a partition. In this case the caller
1663 * must propagate the error.
1665 status
= ef10_nvram_read_tlv_segment(enp
, partn
, *partn_offsetp
,
1666 *seg_datap
, *src_remain_lenp
);
1672 status
= ef10_nvram_buf_segment_size(*seg_datap
,
1673 *src_remain_lenp
, &original_segment_size
);
1680 /* Update the contents of the segment in the buffer */
1681 if ((rc
= ef10_nvram_buf_write_tlv(*seg_datap
,
1682 *dest_remain_lenp
, tag
, data
, size
,
1683 &modified_segment_size
)) != 0) {
1686 *dest_remain_lenp
-= modified_segment_size
;
1687 *seg_datap
+= modified_segment_size
;
1690 * We won't modify this segment, but still need to update the
1691 * remaining lengths and pointers.
1693 *dest_remain_lenp
-= original_segment_size
;
1694 *seg_datap
+= original_segment_size
;
1697 *partn_offsetp
+= original_segment_size
;
1698 *src_remain_lenp
-= original_segment_size
;
1707 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1713 * Add or update a single TLV item in either the first segment or in all
1714 * segments in a TLV formatted dynamic config partition. Dynamic config
1715 * partitions on boards that support RFID are divided into a number of segments,
1716 * each formatted like a partition, with header, trailer and end tags. The first
1717 * segment is the current active configuration.
1719 * The segments are initialised by manftest and each contain a different
1720 * configuration e.g. firmware variant. The firmware can be instructed
1721 * via RFID to copy a segment to replace the first segment, hence changing the
1722 * active configuration. This allows ops to change the configuration of a board
1723 * prior to shipment using RFID.
1725 * Changes to the dynamic config may need to be written to all segments (e.g.
1726 * firmware versions) or just the first segment (changes to the active
1727 * configuration). See SF-111324-SW "The use of RFID in Solarflare Products".
1728 * If only the first segment is written the code still needs to be aware of the
1729 * possible presence of subsequent segments as writing to a segment may cause
1730 * its size to increase, which would overwrite the subsequent segments and
1733 __checkReturn efx_rc_t
1734 ef10_nvram_partn_write_segment_tlv(
1735 __in efx_nic_t
*enp
,
1736 __in
uint32_t partn
,
1738 __in_bcount(size
) caddr_t data
,
1740 __in boolean_t all_segments
)
1742 size_t partn_size
= 0;
1744 size_t total_length
= 0;
1746 size_t current_offset
= 0;
1747 size_t remaining_original_length
;
1748 size_t remaining_modified_length
;
1749 caddr_t segment_data
;
1751 EFSYS_ASSERT3U(partn
, ==, NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG
);
1753 /* Allocate sufficient memory for the entire partition */
1754 if ((rc
= ef10_nvram_partn_size(enp
, partn
, &partn_size
)) != 0)
1757 EFSYS_KMEM_ALLOC(enp
->en_esip
, partn_size
, partn_data
);
1758 if (partn_data
== NULL
) {
1763 remaining_original_length
= partn_size
;
1764 remaining_modified_length
= partn_size
;
1765 segment_data
= partn_data
;
1767 /* Lock the partition */
1768 if ((rc
= ef10_nvram_partn_lock(enp
, partn
)) != 0)
1771 /* Iterate over each (potential) segment to update it. */
1773 boolean_t write
= all_segments
|| current_offset
== 0;
1775 rc
= ef10_nvram_segment_write_tlv(enp
, partn
, tag
, data
, size
,
1776 &segment_data
, ¤t_offset
, &remaining_original_length
,
1777 &remaining_modified_length
, write
);
1779 if (current_offset
== 0) {
1781 * If no data has been read then the first
1782 * segment is invalid, which is an error.
1788 } while (current_offset
< partn_size
);
1790 total_length
= segment_data
- partn_data
;
1793 * We've run out of space. This should actually be dealt with by
1794 * ef10_nvram_buf_write_tlv returning ENOSPC.
1796 if (total_length
> partn_size
) {
1801 /* Erase the whole partition in NVRAM */
1802 if ((rc
= ef10_nvram_partn_erase(enp
, partn
, 0, partn_size
)) != 0)
1805 /* Write new partition contents from the buffer to NVRAM */
1806 if ((rc
= ef10_nvram_partn_write(enp
, partn
, 0, partn_data
,
1807 total_length
)) != 0)
1810 /* Unlock the partition */
1811 ef10_nvram_partn_unlock(enp
, partn
, NULL
);
1813 EFSYS_KMEM_FREE(enp
->en_esip
, partn_size
, partn_data
);
1826 ef10_nvram_partn_unlock(enp
, partn
, NULL
);
1830 EFSYS_KMEM_FREE(enp
->en_esip
, partn_size
, partn_data
);
1834 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1840 * Get the size of a NVRAM partition. This is the total size allocated in nvram,
1841 * not the data used by the segments in the partition.
1843 __checkReturn efx_rc_t
1844 ef10_nvram_partn_size(
1845 __in efx_nic_t
*enp
,
1846 __in
uint32_t partn
,
1847 __out
size_t *sizep
)
1851 if ((rc
= efx_mcdi_nvram_info(enp
, partn
, sizep
,
1852 NULL
, NULL
, NULL
)) != 0)
1858 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1863 __checkReturn efx_rc_t
1864 ef10_nvram_partn_lock(
1865 __in efx_nic_t
*enp
,
1866 __in
uint32_t partn
)
1870 if ((rc
= efx_mcdi_nvram_update_start(enp
, partn
)) != 0)
1876 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1881 __checkReturn efx_rc_t
1882 ef10_nvram_partn_read_mode(
1883 __in efx_nic_t
*enp
,
1884 __in
uint32_t partn
,
1885 __in
unsigned int offset
,
1886 __out_bcount(size
) caddr_t data
,
1894 chunk
= MIN(size
, EF10_NVRAM_CHUNK
);
1896 if ((rc
= efx_mcdi_nvram_read(enp
, partn
, offset
,
1897 data
, chunk
, mode
)) != 0) {
1909 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1914 __checkReturn efx_rc_t
1915 ef10_nvram_partn_read(
1916 __in efx_nic_t
*enp
,
1917 __in
uint32_t partn
,
1918 __in
unsigned int offset
,
1919 __out_bcount(size
) caddr_t data
,
1923 * An A/B partition has two data stores (current and backup).
1924 * Read requests which come in through the EFX API expect to read the
1925 * current, active store of an A/B partition. For non A/B partitions,
1926 * there is only a single store and so the mode param is ignored.
1928 return ef10_nvram_partn_read_mode(enp
, partn
, offset
, data
, size
,
1929 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT
);
1932 __checkReturn efx_rc_t
1933 ef10_nvram_partn_read_backup(
1934 __in efx_nic_t
*enp
,
1935 __in
uint32_t partn
,
1936 __in
unsigned int offset
,
1937 __out_bcount(size
) caddr_t data
,
1941 * An A/B partition has two data stores (current and backup).
1942 * Read the backup store of an A/B partition (i.e. the store currently
1943 * being written to if the partition is locked).
1945 * This is needed when comparing the existing partition content to avoid
1946 * unnecessary writes, or to read back what has been written to check
1947 * that the writes have succeeded.
1949 return ef10_nvram_partn_read_mode(enp
, partn
, offset
, data
, size
,
1950 MC_CMD_NVRAM_READ_IN_V2_TARGET_BACKUP
);
1953 __checkReturn efx_rc_t
1954 ef10_nvram_partn_erase(
1955 __in efx_nic_t
*enp
,
1956 __in
uint32_t partn
,
1957 __in
unsigned int offset
,
1961 uint32_t erase_size
;
1963 if ((rc
= efx_mcdi_nvram_info(enp
, partn
, NULL
, NULL
,
1964 &erase_size
, NULL
)) != 0)
1967 if (erase_size
== 0) {
1968 if ((rc
= efx_mcdi_nvram_erase(enp
, partn
, offset
, size
)) != 0)
1971 if (size
% erase_size
!= 0) {
1976 if ((rc
= efx_mcdi_nvram_erase(enp
, partn
, offset
,
1979 offset
+= erase_size
;
1993 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
1998 __checkReturn efx_rc_t
1999 ef10_nvram_partn_write(
2000 __in efx_nic_t
*enp
,
2001 __in
uint32_t partn
,
2002 __in
unsigned int offset
,
2003 __out_bcount(size
) caddr_t data
,
2007 uint32_t write_size
;
2010 if ((rc
= efx_mcdi_nvram_info(enp
, partn
, NULL
, NULL
,
2011 NULL
, &write_size
)) != 0)
2014 if (write_size
!= 0) {
2016 * Check that the size is a multiple of the write chunk size if
2017 * the write chunk size is available.
2019 if (size
% write_size
!= 0) {
2024 write_size
= EF10_NVRAM_CHUNK
;
2028 chunk
= MIN(size
, write_size
);
2030 if ((rc
= efx_mcdi_nvram_write(enp
, partn
, offset
,
2031 data
, chunk
)) != 0) {
2047 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2052 __checkReturn efx_rc_t
2053 ef10_nvram_partn_unlock(
2054 __in efx_nic_t
*enp
,
2055 __in
uint32_t partn
,
2056 __out_opt
uint32_t *verify_resultp
)
2058 boolean_t reboot
= B_FALSE
;
2061 if (verify_resultp
!= NULL
)
2062 *verify_resultp
= MC_CMD_NVRAM_VERIFY_RC_UNKNOWN
;
2064 rc
= efx_mcdi_nvram_update_finish(enp
, partn
, reboot
, verify_resultp
);
2071 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2076 __checkReturn efx_rc_t
2077 ef10_nvram_partn_set_version(
2078 __in efx_nic_t
*enp
,
2079 __in
uint32_t partn
,
2080 __in_ecount(4) uint16_t version
[4])
2082 struct tlv_partition_version partn_version
;
2086 /* Add or modify partition version TLV item */
2087 partn_version
.version_w
= __CPU_TO_LE_16(version
[0]);
2088 partn_version
.version_x
= __CPU_TO_LE_16(version
[1]);
2089 partn_version
.version_y
= __CPU_TO_LE_16(version
[2]);
2090 partn_version
.version_z
= __CPU_TO_LE_16(version
[3]);
2092 size
= sizeof (partn_version
) - (2 * sizeof (uint32_t));
2094 /* Write the version number to all segments in the partition */
2095 if ((rc
= ef10_nvram_partn_write_segment_tlv(enp
,
2096 NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG
,
2097 TLV_TAG_PARTITION_VERSION(partn
),
2098 (caddr_t
)&partn_version
.version_w
, size
, B_TRUE
)) != 0)
2104 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2109 #endif /* EFSYS_OPT_VPD || EFSYS_OPT_NVRAM */
2113 typedef struct ef10_parttbl_entry_s
{
2115 unsigned int port_mask
;
2116 efx_nvram_type_t nvtype
;
2117 } ef10_parttbl_entry_t
;
2119 /* Port mask values */
2120 #define PORT_1 (1u << 1)
2121 #define PORT_2 (1u << 2)
2122 #define PORT_3 (1u << 3)
2123 #define PORT_4 (1u << 4)
2124 #define PORT_ALL (0xffffffffu)
2126 #define PARTN_MAP_ENTRY(partn, port_mask, nvtype) \
2127 { (NVRAM_PARTITION_TYPE_##partn), (PORT_##port_mask), (EFX_NVRAM_##nvtype) }
2129 /* Translate EFX NVRAM types to firmware partition types */
2130 static ef10_parttbl_entry_t hunt_parttbl
[] = {
2131 /* partn ports nvtype */
2132 PARTN_MAP_ENTRY(MC_FIRMWARE
, ALL
, MC_FIRMWARE
),
2133 PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP
, ALL
, MC_GOLDEN
),
2134 PARTN_MAP_ENTRY(EXPANSION_ROM
, ALL
, BOOTROM
),
2135 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT0
, 1, BOOTROM_CFG
),
2136 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT1
, 2, BOOTROM_CFG
),
2137 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT2
, 3, BOOTROM_CFG
),
2138 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT3
, 4, BOOTROM_CFG
),
2139 PARTN_MAP_ENTRY(DYNAMIC_CONFIG
, ALL
, DYNAMIC_CFG
),
2140 PARTN_MAP_ENTRY(FPGA
, ALL
, FPGA
),
2141 PARTN_MAP_ENTRY(FPGA_BACKUP
, ALL
, FPGA_BACKUP
),
2142 PARTN_MAP_ENTRY(LICENSE
, ALL
, LICENSE
),
2145 static ef10_parttbl_entry_t medford_parttbl
[] = {
2146 /* partn ports nvtype */
2147 PARTN_MAP_ENTRY(MC_FIRMWARE
, ALL
, MC_FIRMWARE
),
2148 PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP
, ALL
, MC_GOLDEN
),
2149 PARTN_MAP_ENTRY(EXPANSION_ROM
, ALL
, BOOTROM
),
2150 PARTN_MAP_ENTRY(EXPROM_CONFIG
, ALL
, BOOTROM_CFG
),
2151 PARTN_MAP_ENTRY(DYNAMIC_CONFIG
, ALL
, DYNAMIC_CFG
),
2152 PARTN_MAP_ENTRY(FPGA
, ALL
, FPGA
),
2153 PARTN_MAP_ENTRY(FPGA_BACKUP
, ALL
, FPGA_BACKUP
),
2154 PARTN_MAP_ENTRY(LICENSE
, ALL
, LICENSE
),
2155 PARTN_MAP_ENTRY(EXPANSION_UEFI
, ALL
, UEFIROM
),
2156 PARTN_MAP_ENTRY(MUM_FIRMWARE
, ALL
, MUM_FIRMWARE
),
2159 static ef10_parttbl_entry_t medford2_parttbl
[] = {
2160 /* partn ports nvtype */
2161 PARTN_MAP_ENTRY(MC_FIRMWARE
, ALL
, MC_FIRMWARE
),
2162 PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP
, ALL
, MC_GOLDEN
),
2163 PARTN_MAP_ENTRY(EXPANSION_ROM
, ALL
, BOOTROM
),
2164 PARTN_MAP_ENTRY(EXPROM_CONFIG
, ALL
, BOOTROM_CFG
),
2165 PARTN_MAP_ENTRY(DYNAMIC_CONFIG
, ALL
, DYNAMIC_CFG
),
2166 PARTN_MAP_ENTRY(FPGA
, ALL
, FPGA
),
2167 PARTN_MAP_ENTRY(FPGA_BACKUP
, ALL
, FPGA_BACKUP
),
2168 PARTN_MAP_ENTRY(LICENSE
, ALL
, LICENSE
),
2169 PARTN_MAP_ENTRY(EXPANSION_UEFI
, ALL
, UEFIROM
),
2170 PARTN_MAP_ENTRY(MUM_FIRMWARE
, ALL
, MUM_FIRMWARE
),
2173 static __checkReturn efx_rc_t
2175 __in efx_nic_t
*enp
,
2176 __out ef10_parttbl_entry_t
**parttblp
,
2177 __out
size_t *parttbl_rowsp
)
2179 switch (enp
->en_family
) {
2180 case EFX_FAMILY_HUNTINGTON
:
2181 *parttblp
= hunt_parttbl
;
2182 *parttbl_rowsp
= EFX_ARRAY_SIZE(hunt_parttbl
);
2185 case EFX_FAMILY_MEDFORD
:
2186 *parttblp
= medford_parttbl
;
2187 *parttbl_rowsp
= EFX_ARRAY_SIZE(medford_parttbl
);
2190 case EFX_FAMILY_MEDFORD2
:
2191 *parttblp
= medford2_parttbl
;
2192 *parttbl_rowsp
= EFX_ARRAY_SIZE(medford2_parttbl
);
2196 EFSYS_ASSERT(B_FALSE
);
2202 __checkReturn efx_rc_t
2203 ef10_nvram_type_to_partn(
2204 __in efx_nic_t
*enp
,
2205 __in efx_nvram_type_t type
,
2206 __out
uint32_t *partnp
)
2208 efx_mcdi_iface_t
*emip
= &(enp
->en_mcdi
.em_emip
);
2209 ef10_parttbl_entry_t
*parttbl
= NULL
;
2210 size_t parttbl_rows
= 0;
2213 EFSYS_ASSERT3U(type
, !=, EFX_NVRAM_INVALID
);
2214 EFSYS_ASSERT3U(type
, <, EFX_NVRAM_NTYPES
);
2215 EFSYS_ASSERT(partnp
!= NULL
);
2217 if (ef10_parttbl_get(enp
, &parttbl
, &parttbl_rows
) == 0) {
2218 for (i
= 0; i
< parttbl_rows
; i
++) {
2219 ef10_parttbl_entry_t
*entry
= &parttbl
[i
];
2221 if ((entry
->nvtype
== type
) &&
2222 (entry
->port_mask
& (1u << emip
->emi_port
))) {
2223 *partnp
= entry
->partn
;
2234 static __checkReturn efx_rc_t
2235 ef10_nvram_partn_to_type(
2236 __in efx_nic_t
*enp
,
2237 __in
uint32_t partn
,
2238 __out efx_nvram_type_t
*typep
)
2240 efx_mcdi_iface_t
*emip
= &(enp
->en_mcdi
.em_emip
);
2241 ef10_parttbl_entry_t
*parttbl
= NULL
;
2242 size_t parttbl_rows
= 0;
2245 EFSYS_ASSERT(typep
!= NULL
);
2247 if (ef10_parttbl_get(enp
, &parttbl
, &parttbl_rows
) == 0) {
2248 for (i
= 0; i
< parttbl_rows
; i
++) {
2249 ef10_parttbl_entry_t
*entry
= &parttbl
[i
];
2251 if ((entry
->partn
== partn
) &&
2252 (entry
->port_mask
& (1u << emip
->emi_port
))) {
2253 *typep
= entry
->nvtype
;
2262 __checkReturn efx_rc_t
2264 __in efx_nic_t
*enp
)
2266 efx_nvram_type_t type
;
2267 unsigned int npartns
= 0;
2268 uint32_t *partns
= NULL
;
2273 /* Read available partitions from NVRAM partition map */
2274 size
= MC_CMD_NVRAM_PARTITIONS_OUT_TYPE_ID_MAXNUM
* sizeof (uint32_t);
2275 EFSYS_KMEM_ALLOC(enp
->en_esip
, size
, partns
);
2276 if (partns
== NULL
) {
2281 if ((rc
= efx_mcdi_nvram_partitions(enp
, (caddr_t
)partns
, size
,
2286 for (i
= 0; i
< npartns
; i
++) {
2287 /* Check if the partition is supported for this port */
2288 if ((rc
= ef10_nvram_partn_to_type(enp
, partns
[i
], &type
)) != 0)
2291 if ((rc
= efx_mcdi_nvram_test(enp
, partns
[i
])) != 0)
2295 EFSYS_KMEM_FREE(enp
->en_esip
, size
, partns
);
2302 EFSYS_KMEM_FREE(enp
->en_esip
, size
, partns
);
2304 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2308 #endif /* EFSYS_OPT_DIAG */
2310 __checkReturn efx_rc_t
2311 ef10_nvram_partn_get_version(
2312 __in efx_nic_t
*enp
,
2313 __in
uint32_t partn
,
2314 __out
uint32_t *subtypep
,
2315 __out_ecount(4) uint16_t version
[4])
2319 /* FIXME: get highest partn version from all ports */
2320 /* FIXME: return partn description if available */
2322 if ((rc
= efx_mcdi_nvram_metadata(enp
, partn
, subtypep
,
2323 version
, NULL
, 0)) != 0)
2329 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2334 __checkReturn efx_rc_t
2335 ef10_nvram_partn_rw_start(
2336 __in efx_nic_t
*enp
,
2337 __in
uint32_t partn
,
2338 __out
size_t *chunk_sizep
)
2340 uint32_t write_size
= 0;
2343 if ((rc
= efx_mcdi_nvram_info(enp
, partn
, NULL
, NULL
,
2344 NULL
, &write_size
)) != 0)
2347 if ((rc
= ef10_nvram_partn_lock(enp
, partn
)) != 0)
2350 if (chunk_sizep
!= NULL
) {
2351 if (write_size
== 0)
2352 *chunk_sizep
= EF10_NVRAM_CHUNK
;
2354 *chunk_sizep
= write_size
;
2362 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2367 __checkReturn efx_rc_t
2368 ef10_nvram_partn_rw_finish(
2369 __in efx_nic_t
*enp
,
2370 __in
uint32_t partn
,
2371 __out_opt
uint32_t *verify_resultp
)
2375 if ((rc
= ef10_nvram_partn_unlock(enp
, partn
, verify_resultp
)) != 0)
2381 EFSYS_PROBE1(fail1
, efx_rc_t
, rc
);
2386 #endif /* EFSYS_OPT_NVRAM */
2388 #endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */