2 * Copyright (c) 2012-2016 Solarflare Communications Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * The views and conclusions contained in the software and documentation are
27 * those of the authors and should not be interpreted as representing official
28 * policies, either expressed or implied, of the FreeBSD Project.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
39 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
41 #include "ef10_tlv_layout.h"
43 /* Cursor for TLV partition format */
44 typedef struct tlv_cursor_s {
45 uint32_t *block; /* Base of data block */
46 uint32_t *current; /* Cursor position */
47 uint32_t *end; /* End tag position */
48 uint32_t *limit; /* Last dword of data block */
51 typedef struct nvram_partition_s {
56 * The full length of the NVRAM partition.
57 * This is different from tlv_partition_header.total_length,
58 * which can be smaller.
63 tlv_cursor_t tlv_cursor;
67 static __checkReturn efx_rc_t
69 __inout tlv_cursor_t *cursor);
74 __out uint32_t *block)
76 *block = __CPU_TO_LE_32(TLV_TAG_END);
81 __in tlv_cursor_t *cursor)
85 dword = cursor->current[0];
86 tag = __LE_TO_CPU_32(dword);
93 __in tlv_cursor_t *cursor)
95 uint32_t dword, length;
97 if (tlv_tag(cursor) == TLV_TAG_END)
100 dword = cursor->current[1];
101 length = __LE_TO_CPU_32(dword);
103 return ((size_t)length);
108 __in tlv_cursor_t *cursor)
110 if (tlv_tag(cursor) == TLV_TAG_END)
113 return ((uint8_t *)(&cursor->current[2]));
118 __in tlv_cursor_t *cursor)
120 if (tlv_tag(cursor) == TLV_TAG_END)
123 return ((uint8_t *)cursor->current);
127 * TLV item DWORD length is tag + length + value (rounded up to DWORD)
128 * equivalent to tlv_n_words_for_len in mc-comms tlv.c
130 #define TLV_DWORD_COUNT(length) \
131 (1 + 1 + (((length) + sizeof (uint32_t) - 1) / sizeof (uint32_t)))
136 __in tlv_cursor_t *cursor)
140 length = tlv_length(cursor);
142 return (cursor->current + TLV_DWORD_COUNT(length));
145 static __checkReturn efx_rc_t
147 __inout tlv_cursor_t *cursor)
151 if ((rc = tlv_validate_state(cursor)) != 0)
154 if (cursor->current == cursor->end) {
155 /* No more tags after END tag */
156 cursor->current = NULL;
161 /* Advance to next item and validate */
162 cursor->current = tlv_next_item_ptr(cursor);
164 if ((rc = tlv_validate_state(cursor)) != 0)
174 EFSYS_PROBE1(fail1, efx_rc_t, rc);
181 __in tlv_cursor_t *cursor)
185 cursor->current = cursor->block;
187 if ((rc = tlv_validate_state(cursor)) != 0)
193 EFSYS_PROBE1(fail1, efx_rc_t, rc);
200 __inout tlv_cursor_t *cursor,
205 rc = tlv_rewind(cursor);
207 if (tlv_tag(cursor) == tag)
210 rc = tlv_advance(cursor);
215 static __checkReturn efx_rc_t
217 __inout tlv_cursor_t *cursor)
221 /* Check cursor position */
222 if (cursor->current < cursor->block) {
226 if (cursor->current > cursor->limit) {
231 if (tlv_tag(cursor) != TLV_TAG_END) {
232 /* Check current item has space for tag and length */
233 if (cursor->current > (cursor->limit - 2)) {
234 cursor->current = NULL;
239 /* Check we have value data for current item and another tag */
240 if (tlv_next_item_ptr(cursor) > (cursor->limit - 1)) {
241 cursor->current = NULL;
256 EFSYS_PROBE1(fail1, efx_rc_t, rc);
263 __out tlv_cursor_t *cursor,
264 __in uint32_t *block,
265 __in uint32_t *limit,
266 __in uint32_t *current)
268 cursor->block = block;
269 cursor->limit = limit;
271 cursor->current = current;
274 return (tlv_validate_state(cursor));
277 static __checkReturn efx_rc_t
278 tlv_init_cursor_from_size(
279 __out tlv_cursor_t *cursor,
285 limit = (uint32_t *)(block + size - sizeof (uint32_t));
286 return (tlv_init_cursor(cursor, (uint32_t *)block,
287 limit, (uint32_t *)block));
290 static __checkReturn efx_rc_t
291 tlv_init_cursor_at_offset(
292 __out tlv_cursor_t *cursor,
300 limit = (uint32_t *)(block + size - sizeof (uint32_t));
301 current = (uint32_t *)(block + offset);
302 return (tlv_init_cursor(cursor, (uint32_t *)block, limit, current));
305 static __checkReturn efx_rc_t
307 __inout tlv_cursor_t *cursor)
312 if (cursor->end == NULL) {
313 pos = cursor->current;
314 if ((rc = tlv_find(cursor, TLV_TAG_END)) != 0)
317 cursor->end = cursor->current;
318 cursor->current = pos;
324 EFSYS_PROBE1(fail1, efx_rc_t, rc);
330 tlv_block_length_used(
331 __inout tlv_cursor_t *cursor)
335 if ((rc = tlv_validate_state(cursor)) != 0)
338 if ((rc = tlv_require_end(cursor)) != 0)
341 /* Return space used (including the END tag) */
342 return (cursor->end + 1 - cursor->block) * sizeof (uint32_t);
347 EFSYS_PROBE1(fail1, efx_rc_t, rc);
353 tlv_last_segment_end(
354 __in tlv_cursor_t *cursor)
356 tlv_cursor_t segment_cursor;
357 uint32_t *last_segment_end = cursor->block;
358 uint32_t *segment_start = cursor->block;
361 * Go through each segment and check that it has an end tag. If there
362 * is no end tag then the previous segment was the last valid one,
363 * so return the pointer to its end tag.
366 if (tlv_init_cursor(&segment_cursor, segment_start,
367 cursor->limit, segment_start) != 0)
369 if (tlv_require_end(&segment_cursor) != 0)
371 last_segment_end = segment_cursor.end;
372 segment_start = segment_cursor.end + 1;
375 return (last_segment_end);
381 __in tlv_cursor_t *cursor,
383 __in_bcount(size) uint8_t *data,
389 ptr = cursor->current;
391 *ptr++ = __CPU_TO_LE_32(tag);
392 *ptr++ = __CPU_TO_LE_32(len);
395 ptr[(len - 1) / sizeof (uint32_t)] = 0;
396 memcpy(ptr, data, len);
397 ptr += P2ROUNDUP(len, sizeof (uint32_t)) / sizeof (*ptr);
403 static __checkReturn efx_rc_t
405 __inout tlv_cursor_t *cursor,
412 uint32_t *last_segment_end;
415 if ((rc = tlv_validate_state(cursor)) != 0)
418 if ((rc = tlv_require_end(cursor)) != 0)
421 if (tag == TLV_TAG_END) {
426 last_segment_end = tlv_last_segment_end(cursor);
428 delta = TLV_DWORD_COUNT(size);
429 if (last_segment_end + 1 + delta > cursor->limit) {
434 /* Move data up: new space at cursor->current */
435 memmove(cursor->current + delta, cursor->current,
436 (last_segment_end + 1 - cursor->current) * sizeof (uint32_t));
438 /* Adjust the end pointer */
439 cursor->end += delta;
441 /* Write new TLV item */
442 tlv_write(cursor, tag, data, size);
453 EFSYS_PROBE1(fail1, efx_rc_t, rc);
458 static __checkReturn efx_rc_t
460 __inout tlv_cursor_t *cursor)
463 uint32_t *last_segment_end;
466 if ((rc = tlv_validate_state(cursor)) != 0)
469 if (tlv_tag(cursor) == TLV_TAG_END) {
474 delta = TLV_DWORD_COUNT(tlv_length(cursor));
476 if ((rc = tlv_require_end(cursor)) != 0)
479 last_segment_end = tlv_last_segment_end(cursor);
481 /* Shuffle things down, destroying the item at cursor->current */
482 memmove(cursor->current, cursor->current + delta,
483 (last_segment_end + 1 - cursor->current) * sizeof (uint32_t));
484 /* Zero the new space at the end of the TLV chain */
485 memset(last_segment_end + 1 - delta, 0, delta * sizeof (uint32_t));
486 /* Adjust the end pointer */
487 cursor->end -= delta;
496 EFSYS_PROBE1(fail1, efx_rc_t, rc);
501 static __checkReturn efx_rc_t
503 __inout tlv_cursor_t *cursor,
510 unsigned int old_ndwords;
511 unsigned int new_ndwords;
513 uint32_t *last_segment_end;
516 if ((rc = tlv_validate_state(cursor)) != 0)
519 if (tlv_tag(cursor) == TLV_TAG_END) {
523 if (tlv_tag(cursor) != tag) {
528 old_ndwords = TLV_DWORD_COUNT(tlv_length(cursor));
529 new_ndwords = TLV_DWORD_COUNT(size);
531 if ((rc = tlv_require_end(cursor)) != 0)
534 last_segment_end = tlv_last_segment_end(cursor);
536 if (new_ndwords > old_ndwords) {
537 /* Expand space used for TLV item */
538 delta = new_ndwords - old_ndwords;
539 pos = cursor->current + old_ndwords;
541 if (last_segment_end + 1 + delta > cursor->limit) {
546 /* Move up: new space at (cursor->current + old_ndwords) */
547 memmove(pos + delta, pos,
548 (last_segment_end + 1 - pos) * sizeof (uint32_t));
550 /* Adjust the end pointer */
551 cursor->end += delta;
553 } else if (new_ndwords < old_ndwords) {
554 /* Shrink space used for TLV item */
555 delta = old_ndwords - new_ndwords;
556 pos = cursor->current + new_ndwords;
558 /* Move down: remove words at (cursor->current + new_ndwords) */
559 memmove(pos, pos + delta,
560 (last_segment_end + 1 - pos) * sizeof (uint32_t));
562 /* Zero the new space at the end of the TLV chain */
563 memset(last_segment_end + 1 - delta, 0,
564 delta * sizeof (uint32_t));
566 /* Adjust the end pointer */
567 cursor->end -= delta;
571 tlv_write(cursor, tag, data, size);
584 EFSYS_PROBE1(fail1, efx_rc_t, rc);
589 static uint32_t checksum_tlv_partition(
590 __in nvram_partition_t *partition)
592 tlv_cursor_t *cursor;
598 cursor = &partition->tlv_cursor;
599 len = tlv_block_length_used(cursor);
600 EFSYS_ASSERT3U((len & 3), ==, 0);
603 ptr = partition->data;
604 end = &ptr[len >> 2];
607 csum += __LE_TO_CPU_32(*ptr++);
612 static __checkReturn efx_rc_t
613 tlv_update_partition_len_and_cks(
614 __in tlv_cursor_t *cursor)
617 nvram_partition_t partition;
618 struct tlv_partition_header *header;
619 struct tlv_partition_trailer *trailer;
623 * We just modified the partition, so the total length may not be
624 * valid. Don't use tlv_find(), which performs some sanity checks
625 * that may fail here.
627 partition.data = cursor->block;
628 memcpy(&partition.tlv_cursor, cursor, sizeof (*cursor));
629 header = (struct tlv_partition_header *)partition.data;
631 if (__LE_TO_CPU_32(header->tag) != TLV_TAG_PARTITION_HEADER) {
635 new_len = tlv_block_length_used(&partition.tlv_cursor);
640 header->total_length = __CPU_TO_LE_32(new_len);
641 /* Ensure the modified partition always has a new generation count. */
642 header->generation = __CPU_TO_LE_32(
643 __LE_TO_CPU_32(header->generation) + 1);
645 trailer = (struct tlv_partition_trailer *)((uint8_t *)header +
646 new_len - sizeof (*trailer) - sizeof (uint32_t));
647 trailer->generation = header->generation;
648 trailer->checksum = __CPU_TO_LE_32(
649 __LE_TO_CPU_32(trailer->checksum) -
650 checksum_tlv_partition(&partition));
657 EFSYS_PROBE1(fail1, efx_rc_t, rc);
662 /* Validate buffer contents (before writing to flash) */
663 __checkReturn efx_rc_t
664 ef10_nvram_buffer_validate(
667 __in_bcount(partn_size) caddr_t partn_data,
668 __in size_t partn_size)
671 struct tlv_partition_header *header;
672 struct tlv_partition_trailer *trailer;
678 _NOTE(ARGUNUSED(enp, partn))
679 EFX_STATIC_ASSERT(sizeof (*header) <= EF10_NVRAM_CHUNK);
681 if ((partn_data == NULL) || (partn_size == 0)) {
686 /* The partition header must be the first item (at offset zero) */
687 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)partn_data,
692 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
696 header = (struct tlv_partition_header *)tlv_item(&cursor);
698 /* Check TLV partition length (includes the END tag) */
699 total_length = __LE_TO_CPU_32(header->total_length);
700 if (total_length > partn_size) {
705 /* Check partition ends with PARTITION_TRAILER and END tags */
706 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
710 trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
712 if ((rc = tlv_advance(&cursor)) != 0) {
716 if (tlv_tag(&cursor) != TLV_TAG_END) {
721 /* Check generation counts are consistent */
722 if (trailer->generation != header->generation) {
727 /* Verify partition checksum */
729 for (pos = 0; (size_t)pos < total_length; pos += sizeof (uint32_t)) {
730 cksum += *((uint32_t *)(partn_data + pos));
756 EFSYS_PROBE1(fail1, efx_rc_t, rc);
763 __checkReturn efx_rc_t
764 ef10_nvram_buffer_create(
766 __in uint16_t partn_type,
767 __in_bcount(partn_size) caddr_t partn_data,
768 __in size_t partn_size)
770 uint32_t *buf = (uint32_t *)partn_data;
773 struct tlv_partition_header header;
774 struct tlv_partition_trailer trailer;
776 unsigned int min_buf_size = sizeof (struct tlv_partition_header) +
777 sizeof (struct tlv_partition_trailer);
778 if (partn_size < min_buf_size) {
783 memset(buf, 0xff, partn_size);
786 if ((rc = tlv_init_cursor(&cursor, buf,
787 (uint32_t *)((uint8_t *)buf + partn_size),
792 header.tag = __CPU_TO_LE_32(TLV_TAG_PARTITION_HEADER);
793 header.length = __CPU_TO_LE_32(sizeof (header) - 8);
794 header.type_id = __CPU_TO_LE_16(partn_type);
796 header.generation = __CPU_TO_LE_32(1);
797 header.total_length = 0; /* This will be fixed below. */
798 if ((rc = tlv_insert(
799 &cursor, TLV_TAG_PARTITION_HEADER,
800 (uint8_t *)&header.type_id, sizeof (header) - 8)) != 0)
802 if ((rc = tlv_advance(&cursor)) != 0)
805 trailer.tag = __CPU_TO_LE_32(TLV_TAG_PARTITION_TRAILER);
806 trailer.length = __CPU_TO_LE_32(sizeof (trailer) - 8);
807 trailer.generation = header.generation;
808 trailer.checksum = 0; /* This will be fixed below. */
809 if ((rc = tlv_insert(&cursor, TLV_TAG_PARTITION_TRAILER,
810 (uint8_t *)&trailer.generation, sizeof (trailer) - 8)) != 0)
813 if ((rc = tlv_update_partition_len_and_cks(&cursor)) != 0)
816 /* Check that the partition is valid. */
817 if ((rc = ef10_nvram_buffer_validate(enp, partn_type,
818 partn_data, partn_size)) != 0)
836 EFSYS_PROBE1(fail1, efx_rc_t, rc);
843 __in uint32_t *position,
846 return (uint32_t)((uint8_t *)position - (uint8_t *)base);
849 __checkReturn efx_rc_t
850 ef10_nvram_buffer_find_item_start(
851 __in_bcount(buffer_size)
853 __in size_t buffer_size,
854 __out uint32_t *startp)
856 /* Read past partition header to find start address of the first key */
860 /* A PARTITION_HEADER tag must be the first item (at offset zero) */
861 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
862 buffer_size)) != 0) {
866 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
871 if ((rc = tlv_advance(&cursor)) != 0) {
875 *startp = byte_offset(cursor.current, cursor.block);
877 if ((rc = tlv_require_end(&cursor)) != 0)
889 EFSYS_PROBE1(fail1, efx_rc_t, rc);
894 __checkReturn efx_rc_t
895 ef10_nvram_buffer_find_end(
896 __in_bcount(buffer_size)
898 __in size_t buffer_size,
899 __in uint32_t offset,
900 __out uint32_t *endp)
902 /* Read to end of partition */
905 uint32_t *segment_used;
907 _NOTE(ARGUNUSED(offset))
909 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
910 buffer_size)) != 0) {
915 segment_used = cursor.block;
918 * Go through each segment and check that it has an end tag. If there
919 * is no end tag then the previous segment was the last valid one,
920 * so return the used space including that end tag.
922 while (tlv_tag(&cursor) == TLV_TAG_PARTITION_HEADER) {
923 if (tlv_require_end(&cursor) != 0) {
924 if (segment_used == cursor.block) {
926 * First segment is corrupt, so there is
927 * no valid data in partition.
934 segment_used = cursor.end + 1;
936 cursor.current = segment_used;
938 /* Return space used (including the END tag) */
939 *endp = (segment_used - cursor.block) * sizeof (uint32_t);
946 EFSYS_PROBE1(fail1, efx_rc_t, rc);
951 __checkReturn __success(return != B_FALSE) boolean_t
952 ef10_nvram_buffer_find_item(
953 __in_bcount(buffer_size)
955 __in size_t buffer_size,
956 __in uint32_t offset,
957 __out uint32_t *startp,
958 __out uint32_t *lengthp)
960 /* Find TLV at offset and return key start and length */
965 if (tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
966 buffer_size, offset) != 0) {
970 while ((key = tlv_item(&cursor)) != NULL) {
971 tag = tlv_tag(&cursor);
972 if (tag == TLV_TAG_PARTITION_HEADER ||
973 tag == TLV_TAG_PARTITION_TRAILER) {
974 if (tlv_advance(&cursor) != 0) {
979 *startp = byte_offset(cursor.current, cursor.block);
980 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
988 __checkReturn efx_rc_t
989 ef10_nvram_buffer_get_item(
990 __in_bcount(buffer_size)
992 __in size_t buffer_size,
993 __in uint32_t offset,
994 __in uint32_t length,
995 __out_bcount_part(item_max_size, *lengthp)
997 __in size_t item_max_size,
998 __out uint32_t *lengthp)
1001 tlv_cursor_t cursor;
1002 uint32_t item_length;
1004 if (item_max_size < length) {
1009 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1010 buffer_size, offset)) != 0) {
1014 item_length = tlv_length(&cursor);
1015 if (length < item_length) {
1019 memcpy(itemp, tlv_value(&cursor), item_length);
1021 *lengthp = item_length;
1030 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1035 __checkReturn efx_rc_t
1036 ef10_nvram_buffer_insert_item(
1037 __in_bcount(buffer_size)
1039 __in size_t buffer_size,
1040 __in uint32_t offset,
1041 __in_bcount(length) caddr_t keyp,
1042 __in uint32_t length,
1043 __out uint32_t *lengthp)
1046 tlv_cursor_t cursor;
1048 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1049 buffer_size, offset)) != 0) {
1053 rc = tlv_insert(&cursor, TLV_TAG_LICENSE, (uint8_t *)keyp, length);
1059 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
1067 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1072 __checkReturn efx_rc_t
1073 ef10_nvram_buffer_delete_item(
1074 __in_bcount(buffer_size)
1076 __in size_t buffer_size,
1077 __in uint32_t offset,
1078 __in uint32_t length,
1082 tlv_cursor_t cursor;
1084 _NOTE(ARGUNUSED(length, end))
1086 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1087 buffer_size, offset)) != 0) {
1091 if ((rc = tlv_delete(&cursor)) != 0)
1099 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1104 __checkReturn efx_rc_t
1105 ef10_nvram_buffer_finish(
1106 __in_bcount(buffer_size)
1108 __in size_t buffer_size)
1111 tlv_cursor_t cursor;
1113 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
1114 buffer_size)) != 0) {
1119 if ((rc = tlv_require_end(&cursor)) != 0)
1122 if ((rc = tlv_update_partition_len_and_cks(&cursor)) != 0)
1132 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1140 * Read and validate a segment from a partition. A segment is a complete
1141 * tlv chain between PARTITION_HEADER and PARTITION_END tags. There may
1142 * be multiple segments in a partition, so seg_offset allows segments
1143 * beyond the first to be read.
1145 static __checkReturn efx_rc_t
1146 ef10_nvram_read_tlv_segment(
1147 __in efx_nic_t *enp,
1148 __in uint32_t partn,
1149 __in size_t seg_offset,
1150 __in_bcount(max_seg_size) caddr_t seg_data,
1151 __in size_t max_seg_size)
1153 tlv_cursor_t cursor;
1154 struct tlv_partition_header *header;
1155 struct tlv_partition_trailer *trailer;
1156 size_t total_length;
1161 EFX_STATIC_ASSERT(sizeof (*header) <= EF10_NVRAM_CHUNK);
1163 if ((seg_data == NULL) || (max_seg_size == 0)) {
1168 /* Read initial chunk of the segment, starting at offset */
1169 if ((rc = ef10_nvram_partn_read_mode(enp, partn, seg_offset, seg_data,
1171 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT)) != 0) {
1175 /* A PARTITION_HEADER tag must be the first item at the given offset */
1176 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1177 max_seg_size)) != 0) {
1181 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1185 header = (struct tlv_partition_header *)tlv_item(&cursor);
1187 /* Check TLV segment length (includes the END tag) */
1188 total_length = __LE_TO_CPU_32(header->total_length);
1189 if (total_length > max_seg_size) {
1194 /* Read the remaining segment content */
1195 if (total_length > EF10_NVRAM_CHUNK) {
1196 if ((rc = ef10_nvram_partn_read_mode(enp, partn,
1197 seg_offset + EF10_NVRAM_CHUNK,
1198 seg_data + EF10_NVRAM_CHUNK,
1199 total_length - EF10_NVRAM_CHUNK,
1200 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT)) != 0)
1204 /* Check segment ends with PARTITION_TRAILER and END tags */
1205 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1209 trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
1211 if ((rc = tlv_advance(&cursor)) != 0) {
1215 if (tlv_tag(&cursor) != TLV_TAG_END) {
1220 /* Check data read from segment is consistent */
1221 if (trailer->generation != header->generation) {
1223 * The partition data may have been modified between successive
1224 * MCDI NVRAM_READ requests by the MC or another PCI function.
1226 * The caller must retry to obtain consistent partition data.
1232 /* Verify segment checksum */
1234 for (pos = 0; (size_t)pos < total_length; pos += sizeof (uint32_t)) {
1235 cksum += *((uint32_t *)(seg_data + pos));
1245 EFSYS_PROBE(fail11);
1247 EFSYS_PROBE(fail10);
1265 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1271 * Read a single TLV item from a host memory
1272 * buffer containing a TLV formatted segment.
1274 __checkReturn efx_rc_t
1275 ef10_nvram_buf_read_tlv(
1276 __in efx_nic_t *enp,
1277 __in_bcount(max_seg_size) caddr_t seg_data,
1278 __in size_t max_seg_size,
1280 __deref_out_bcount_opt(*sizep) caddr_t *datap,
1281 __out size_t *sizep)
1283 tlv_cursor_t cursor;
1289 _NOTE(ARGUNUSED(enp))
1291 if ((seg_data == NULL) || (max_seg_size == 0)) {
1296 /* Find requested TLV tag in segment data */
1297 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1298 max_seg_size)) != 0) {
1302 if ((rc = tlv_find(&cursor, tag)) != 0) {
1306 value = (caddr_t)tlv_value(&cursor);
1307 length = tlv_length(&cursor);
1312 /* Copy out data from TLV item */
1313 EFSYS_KMEM_ALLOC(enp->en_esip, length, data);
1318 memcpy(data, value, length);
1333 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1338 /* Read a single TLV item from the first segment in a TLV formatted partition */
1339 __checkReturn efx_rc_t
1340 ef10_nvram_partn_read_tlv(
1341 __in efx_nic_t *enp,
1342 __in uint32_t partn,
1344 __deref_out_bcount_opt(*seg_sizep) caddr_t *seg_datap,
1345 __out size_t *seg_sizep)
1347 caddr_t seg_data = NULL;
1348 size_t partn_size = 0;
1354 /* Allocate sufficient memory for the entire partition */
1355 if ((rc = ef10_nvram_partn_size(enp, partn, &partn_size)) != 0)
1358 if (partn_size == 0) {
1363 EFSYS_KMEM_ALLOC(enp->en_esip, partn_size, seg_data);
1364 if (seg_data == NULL) {
1370 * Read the first segment in a TLV partition. Retry until consistent
1371 * segment contents are returned. Inconsistent data may be read if:
1372 * a) the segment contents are invalid
1373 * b) the MC has rebooted while we were reading the partition
1374 * c) the partition has been modified while we were reading it
1375 * Limit retry attempts to ensure forward progress.
1379 rc = ef10_nvram_read_tlv_segment(enp, partn, 0,
1380 seg_data, partn_size);
1381 } while ((rc == EAGAIN) && (--retry > 0));
1384 /* Failed to obtain consistent segment data */
1388 if ((rc = ef10_nvram_buf_read_tlv(enp, seg_data, partn_size,
1389 tag, &data, &length)) != 0)
1392 EFSYS_KMEM_FREE(enp->en_esip, partn_size, seg_data);
1395 *seg_sizep = length;
1404 EFSYS_KMEM_FREE(enp->en_esip, partn_size, seg_data);
1410 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1415 /* Compute the size of a segment. */
1416 static __checkReturn efx_rc_t
1417 ef10_nvram_buf_segment_size(
1418 __in caddr_t seg_data,
1419 __in size_t max_seg_size,
1420 __out size_t *seg_sizep)
1423 tlv_cursor_t cursor;
1424 struct tlv_partition_header *header;
1427 uint32_t *end_tag_position;
1428 uint32_t segment_length;
1430 /* A PARTITION_HEADER tag must be the first item at the given offset */
1431 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1432 max_seg_size)) != 0) {
1436 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1440 header = (struct tlv_partition_header *)tlv_item(&cursor);
1442 /* Check TLV segment length (includes the END tag) */
1443 *seg_sizep = __LE_TO_CPU_32(header->total_length);
1444 if (*seg_sizep > max_seg_size) {
1449 /* Check segment ends with PARTITION_TRAILER and END tags */
1450 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1455 if ((rc = tlv_advance(&cursor)) != 0) {
1459 if (tlv_tag(&cursor) != TLV_TAG_END) {
1463 end_tag_position = cursor.current;
1465 /* Verify segment checksum */
1467 for (pos = 0; (size_t)pos < *seg_sizep; pos += sizeof (uint32_t)) {
1468 cksum += *((uint32_t *)(seg_data + pos));
1476 * Calculate total length from HEADER to END tags and compare to
1477 * max_seg_size and the total_length field in the HEADER tag.
1479 segment_length = tlv_block_length_used(&cursor);
1481 if (segment_length > max_seg_size) {
1486 if (segment_length != *seg_sizep) {
1491 /* Skip over the first HEADER tag. */
1492 rc = tlv_rewind(&cursor);
1493 rc = tlv_advance(&cursor);
1496 if (tlv_tag(&cursor) == TLV_TAG_END) {
1497 /* Check that the END tag is the one found earlier. */
1498 if (cursor.current != end_tag_position)
1502 /* Check for duplicate HEADER tags before the END tag. */
1503 if (tlv_tag(&cursor) == TLV_TAG_PARTITION_HEADER) {
1508 rc = tlv_advance(&cursor);
1516 EFSYS_PROBE(fail12);
1518 EFSYS_PROBE(fail11);
1520 EFSYS_PROBE(fail10);
1538 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1544 * Add or update a single TLV item in a host memory buffer containing a TLV
1545 * formatted segment. Historically partitions consisted of only one segment.
1547 __checkReturn efx_rc_t
1548 ef10_nvram_buf_write_tlv(
1549 __inout_bcount(max_seg_size) caddr_t seg_data,
1550 __in size_t max_seg_size,
1552 __in_bcount(tag_size) caddr_t tag_data,
1553 __in size_t tag_size,
1554 __out size_t *total_lengthp)
1556 tlv_cursor_t cursor;
1557 struct tlv_partition_header *header;
1558 struct tlv_partition_trailer *trailer;
1559 uint32_t generation;
1564 /* A PARTITION_HEADER tag must be the first item (at offset zero) */
1565 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1566 max_seg_size)) != 0) {
1570 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1574 header = (struct tlv_partition_header *)tlv_item(&cursor);
1576 /* Update the TLV chain to contain the new data */
1577 if ((rc = tlv_find(&cursor, tag)) == 0) {
1578 /* Modify existing TLV item */
1579 if ((rc = tlv_modify(&cursor, tag,
1580 (uint8_t *)tag_data, tag_size)) != 0)
1583 /* Insert a new TLV item before the PARTITION_TRAILER */
1584 rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER);
1589 if ((rc = tlv_insert(&cursor, tag,
1590 (uint8_t *)tag_data, tag_size)) != 0) {
1596 /* Find the trailer tag */
1597 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1601 trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
1603 /* Update PARTITION_HEADER and PARTITION_TRAILER fields */
1604 *total_lengthp = tlv_block_length_used(&cursor);
1605 if (*total_lengthp > max_seg_size) {
1609 generation = __LE_TO_CPU_32(header->generation) + 1;
1611 header->total_length = __CPU_TO_LE_32(*total_lengthp);
1612 header->generation = __CPU_TO_LE_32(generation);
1613 trailer->generation = __CPU_TO_LE_32(generation);
1615 /* Recompute PARTITION_TRAILER checksum */
1616 trailer->checksum = 0;
1618 for (pos = 0; (size_t)pos < *total_lengthp; pos += sizeof (uint32_t)) {
1619 cksum += *((uint32_t *)(seg_data + pos));
1621 trailer->checksum = ~cksum + 1;
1638 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1644 * Add or update a single TLV item in the first segment of a TLV formatted
1645 * dynamic config partition. The first segment is the current active
1648 __checkReturn efx_rc_t
1649 ef10_nvram_partn_write_tlv(
1650 __in efx_nic_t *enp,
1651 __in uint32_t partn,
1653 __in_bcount(size) caddr_t data,
1656 return ef10_nvram_partn_write_segment_tlv(enp, partn, tag, data,
1661 * Read a segment from nvram at the given offset into a buffer (segment_data)
1662 * and optionally write a new tag to it.
1664 static __checkReturn efx_rc_t
1665 ef10_nvram_segment_write_tlv(
1666 __in efx_nic_t *enp,
1667 __in uint32_t partn,
1669 __in_bcount(size) caddr_t data,
1671 __inout caddr_t *seg_datap,
1672 __inout size_t *partn_offsetp,
1673 __inout size_t *src_remain_lenp,
1674 __inout size_t *dest_remain_lenp,
1675 __in boolean_t write)
1679 size_t original_segment_size;
1680 size_t modified_segment_size;
1683 * Read the segment from NVRAM into the segment_data buffer and validate
1684 * it, returning if it does not validate. This is not a failure unless
1685 * this is the first segment in a partition. In this case the caller
1686 * must propagate the error.
1688 status = ef10_nvram_read_tlv_segment(enp, partn, *partn_offsetp,
1689 *seg_datap, *src_remain_lenp);
1695 status = ef10_nvram_buf_segment_size(*seg_datap,
1696 *src_remain_lenp, &original_segment_size);
1703 /* Update the contents of the segment in the buffer */
1704 if ((rc = ef10_nvram_buf_write_tlv(*seg_datap,
1705 *dest_remain_lenp, tag, data, size,
1706 &modified_segment_size)) != 0) {
1709 *dest_remain_lenp -= modified_segment_size;
1710 *seg_datap += modified_segment_size;
1713 * We won't modify this segment, but still need to update the
1714 * remaining lengths and pointers.
1716 *dest_remain_lenp -= original_segment_size;
1717 *seg_datap += original_segment_size;
1720 *partn_offsetp += original_segment_size;
1721 *src_remain_lenp -= original_segment_size;
1730 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1736 * Add or update a single TLV item in either the first segment or in all
1737 * segments in a TLV formatted dynamic config partition. Dynamic config
1738 * partitions on boards that support RFID are divided into a number of segments,
1739 * each formatted like a partition, with header, trailer and end tags. The first
1740 * segment is the current active configuration.
1742 * The segments are initialised by manftest and each contain a different
1743 * configuration e.g. firmware variant. The firmware can be instructed
1744 * via RFID to copy a segment to replace the first segment, hence changing the
1745 * active configuration. This allows ops to change the configuration of a board
1746 * prior to shipment using RFID.
1748 * Changes to the dynamic config may need to be written to all segments (e.g.
1749 * firmware versions) or just the first segment (changes to the active
1750 * configuration). See SF-111324-SW "The use of RFID in Solarflare Products".
1751 * If only the first segment is written the code still needs to be aware of the
1752 * possible presence of subsequent segments as writing to a segment may cause
1753 * its size to increase, which would overwrite the subsequent segments and
1756 __checkReturn efx_rc_t
1757 ef10_nvram_partn_write_segment_tlv(
1758 __in efx_nic_t *enp,
1759 __in uint32_t partn,
1761 __in_bcount(size) caddr_t data,
1763 __in boolean_t all_segments)
1765 size_t partn_size = 0;
1767 size_t total_length = 0;
1769 size_t current_offset = 0;
1770 size_t remaining_original_length;
1771 size_t remaining_modified_length;
1772 caddr_t segment_data;
1774 EFSYS_ASSERT3U(partn, ==, NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG);
1776 /* Allocate sufficient memory for the entire partition */
1777 if ((rc = ef10_nvram_partn_size(enp, partn, &partn_size)) != 0)
1780 EFSYS_KMEM_ALLOC(enp->en_esip, partn_size, partn_data);
1781 if (partn_data == NULL) {
1786 remaining_original_length = partn_size;
1787 remaining_modified_length = partn_size;
1788 segment_data = partn_data;
1790 /* Lock the partition */
1791 if ((rc = ef10_nvram_partn_lock(enp, partn)) != 0)
1794 /* Iterate over each (potential) segment to update it. */
1796 boolean_t write = all_segments || current_offset == 0;
1798 rc = ef10_nvram_segment_write_tlv(enp, partn, tag, data, size,
1799 &segment_data, ¤t_offset, &remaining_original_length,
1800 &remaining_modified_length, write);
1802 if (current_offset == 0) {
1804 * If no data has been read then the first
1805 * segment is invalid, which is an error.
1811 } while (current_offset < partn_size);
1813 total_length = segment_data - partn_data;
1816 * We've run out of space. This should actually be dealt with by
1817 * ef10_nvram_buf_write_tlv returning ENOSPC.
1819 if (total_length > partn_size) {
1824 /* Erase the whole partition in NVRAM */
1825 if ((rc = ef10_nvram_partn_erase(enp, partn, 0, partn_size)) != 0)
1828 /* Write new partition contents from the buffer to NVRAM */
1829 if ((rc = ef10_nvram_partn_write(enp, partn, 0, partn_data,
1830 total_length)) != 0)
1833 /* Unlock the partition */
1834 (void) ef10_nvram_partn_unlock(enp, partn, NULL);
1836 EFSYS_KMEM_FREE(enp->en_esip, partn_size, partn_data);
1849 (void) ef10_nvram_partn_unlock(enp, partn, NULL);
1853 EFSYS_KMEM_FREE(enp->en_esip, partn_size, partn_data);
1857 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1863 * Get the size of a NVRAM partition. This is the total size allocated in nvram,
1864 * not the data used by the segments in the partition.
1866 __checkReturn efx_rc_t
1867 ef10_nvram_partn_size(
1868 __in efx_nic_t *enp,
1869 __in uint32_t partn,
1870 __out size_t *sizep)
1874 if ((rc = efx_mcdi_nvram_info(enp, partn, sizep,
1875 NULL, NULL, NULL)) != 0)
1881 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1886 __checkReturn efx_rc_t
1887 ef10_nvram_partn_lock(
1888 __in efx_nic_t *enp,
1889 __in uint32_t partn)
1893 if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0)
1899 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1904 __checkReturn efx_rc_t
1905 ef10_nvram_partn_read_mode(
1906 __in efx_nic_t *enp,
1907 __in uint32_t partn,
1908 __in unsigned int offset,
1909 __out_bcount(size) caddr_t data,
1917 chunk = MIN(size, EF10_NVRAM_CHUNK);
1919 if ((rc = efx_mcdi_nvram_read(enp, partn, offset,
1920 data, chunk, mode)) != 0) {
1932 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1937 __checkReturn efx_rc_t
1938 ef10_nvram_partn_read(
1939 __in efx_nic_t *enp,
1940 __in uint32_t partn,
1941 __in unsigned int offset,
1942 __out_bcount(size) caddr_t data,
1946 * Read requests which come in through the EFX API expect to
1947 * read the current, active partition.
1949 return ef10_nvram_partn_read_mode(enp, partn, offset, data, size,
1950 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT);
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 *resultp)
2058 boolean_t reboot = B_FALSE;
2061 if (resultp != NULL)
2062 *resultp = MC_CMD_NVRAM_VERIFY_RC_UNKNOWN;
2064 rc = efx_mcdi_nvram_update_finish(enp, partn, reboot, 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 {
2116 efx_nvram_type_t nvtype;
2117 } ef10_parttbl_entry_t;
2119 /* Translate EFX NVRAM types to firmware partition types */
2120 static ef10_parttbl_entry_t hunt_parttbl[] = {
2121 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 1, EFX_NVRAM_MC_FIRMWARE},
2122 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 2, EFX_NVRAM_MC_FIRMWARE},
2123 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 3, EFX_NVRAM_MC_FIRMWARE},
2124 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 4, EFX_NVRAM_MC_FIRMWARE},
2125 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 1, EFX_NVRAM_MC_GOLDEN},
2126 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 2, EFX_NVRAM_MC_GOLDEN},
2127 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 3, EFX_NVRAM_MC_GOLDEN},
2128 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 4, EFX_NVRAM_MC_GOLDEN},
2129 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 1, EFX_NVRAM_BOOTROM},
2130 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 2, EFX_NVRAM_BOOTROM},
2131 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 3, EFX_NVRAM_BOOTROM},
2132 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 4, EFX_NVRAM_BOOTROM},
2133 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 1, EFX_NVRAM_BOOTROM_CFG},
2134 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 2, EFX_NVRAM_BOOTROM_CFG},
2135 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 3, EFX_NVRAM_BOOTROM_CFG},
2136 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 4, EFX_NVRAM_BOOTROM_CFG},
2137 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 1, EFX_NVRAM_DYNAMIC_CFG},
2138 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 2, EFX_NVRAM_DYNAMIC_CFG},
2139 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 3, EFX_NVRAM_DYNAMIC_CFG},
2140 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 4, EFX_NVRAM_DYNAMIC_CFG},
2141 {NVRAM_PARTITION_TYPE_FPGA, 1, EFX_NVRAM_FPGA},
2142 {NVRAM_PARTITION_TYPE_FPGA, 2, EFX_NVRAM_FPGA},
2143 {NVRAM_PARTITION_TYPE_FPGA, 3, EFX_NVRAM_FPGA},
2144 {NVRAM_PARTITION_TYPE_FPGA, 4, EFX_NVRAM_FPGA},
2145 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 1, EFX_NVRAM_FPGA_BACKUP},
2146 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 2, EFX_NVRAM_FPGA_BACKUP},
2147 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 3, EFX_NVRAM_FPGA_BACKUP},
2148 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 4, EFX_NVRAM_FPGA_BACKUP},
2149 {NVRAM_PARTITION_TYPE_LICENSE, 1, EFX_NVRAM_LICENSE},
2150 {NVRAM_PARTITION_TYPE_LICENSE, 2, EFX_NVRAM_LICENSE},
2151 {NVRAM_PARTITION_TYPE_LICENSE, 3, EFX_NVRAM_LICENSE},
2152 {NVRAM_PARTITION_TYPE_LICENSE, 4, EFX_NVRAM_LICENSE}
2155 static ef10_parttbl_entry_t medford_parttbl[] = {
2156 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 1, EFX_NVRAM_MC_FIRMWARE},
2157 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 2, EFX_NVRAM_MC_FIRMWARE},
2158 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 3, EFX_NVRAM_MC_FIRMWARE},
2159 {NVRAM_PARTITION_TYPE_MC_FIRMWARE, 4, EFX_NVRAM_MC_FIRMWARE},
2160 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 1, EFX_NVRAM_MC_GOLDEN},
2161 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 2, EFX_NVRAM_MC_GOLDEN},
2162 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 3, EFX_NVRAM_MC_GOLDEN},
2163 {NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 4, EFX_NVRAM_MC_GOLDEN},
2164 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 1, EFX_NVRAM_BOOTROM},
2165 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 2, EFX_NVRAM_BOOTROM},
2166 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 3, EFX_NVRAM_BOOTROM},
2167 {NVRAM_PARTITION_TYPE_EXPANSION_ROM, 4, EFX_NVRAM_BOOTROM},
2168 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 1, EFX_NVRAM_BOOTROM_CFG},
2169 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 2, EFX_NVRAM_BOOTROM_CFG},
2170 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 3, EFX_NVRAM_BOOTROM_CFG},
2171 {NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 4, EFX_NVRAM_BOOTROM_CFG},
2172 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 1, EFX_NVRAM_DYNAMIC_CFG},
2173 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 2, EFX_NVRAM_DYNAMIC_CFG},
2174 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 3, EFX_NVRAM_DYNAMIC_CFG},
2175 {NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 4, EFX_NVRAM_DYNAMIC_CFG},
2176 {NVRAM_PARTITION_TYPE_FPGA, 1, EFX_NVRAM_FPGA},
2177 {NVRAM_PARTITION_TYPE_FPGA, 2, EFX_NVRAM_FPGA},
2178 {NVRAM_PARTITION_TYPE_FPGA, 3, EFX_NVRAM_FPGA},
2179 {NVRAM_PARTITION_TYPE_FPGA, 4, EFX_NVRAM_FPGA},
2180 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 1, EFX_NVRAM_FPGA_BACKUP},
2181 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 2, EFX_NVRAM_FPGA_BACKUP},
2182 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 3, EFX_NVRAM_FPGA_BACKUP},
2183 {NVRAM_PARTITION_TYPE_FPGA_BACKUP, 4, EFX_NVRAM_FPGA_BACKUP},
2184 {NVRAM_PARTITION_TYPE_LICENSE, 1, EFX_NVRAM_LICENSE},
2185 {NVRAM_PARTITION_TYPE_LICENSE, 2, EFX_NVRAM_LICENSE},
2186 {NVRAM_PARTITION_TYPE_LICENSE, 3, EFX_NVRAM_LICENSE},
2187 {NVRAM_PARTITION_TYPE_LICENSE, 4, EFX_NVRAM_LICENSE},
2188 {NVRAM_PARTITION_TYPE_EXPANSION_UEFI, 1, EFX_NVRAM_UEFIROM},
2189 {NVRAM_PARTITION_TYPE_EXPANSION_UEFI, 2, EFX_NVRAM_UEFIROM},
2190 {NVRAM_PARTITION_TYPE_EXPANSION_UEFI, 3, EFX_NVRAM_UEFIROM},
2191 {NVRAM_PARTITION_TYPE_EXPANSION_UEFI, 4, EFX_NVRAM_UEFIROM}
2194 static __checkReturn efx_rc_t
2196 __in efx_nic_t *enp,
2197 __out ef10_parttbl_entry_t **parttblp,
2198 __out size_t *parttbl_rowsp)
2200 switch (enp->en_family) {
2201 case EFX_FAMILY_HUNTINGTON:
2202 *parttblp = hunt_parttbl;
2203 *parttbl_rowsp = EFX_ARRAY_SIZE(hunt_parttbl);
2206 case EFX_FAMILY_MEDFORD:
2207 *parttblp = medford_parttbl;
2208 *parttbl_rowsp = EFX_ARRAY_SIZE(medford_parttbl);
2212 EFSYS_ASSERT(B_FALSE);
2218 __checkReturn efx_rc_t
2219 ef10_nvram_type_to_partn(
2220 __in efx_nic_t *enp,
2221 __in efx_nvram_type_t type,
2222 __out uint32_t *partnp)
2224 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
2225 ef10_parttbl_entry_t *parttbl = NULL;
2226 size_t parttbl_rows = 0;
2229 EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
2230 EFSYS_ASSERT(partnp != NULL);
2232 if (ef10_parttbl_get(enp, &parttbl, &parttbl_rows) == 0) {
2233 for (i = 0; i < parttbl_rows; i++) {
2234 ef10_parttbl_entry_t *entry = &parttbl[i];
2236 if (entry->nvtype == type &&
2237 entry->port == emip->emi_port) {
2238 *partnp = entry->partn;
2249 static __checkReturn efx_rc_t
2250 ef10_nvram_partn_to_type(
2251 __in efx_nic_t *enp,
2252 __in uint32_t partn,
2253 __out efx_nvram_type_t *typep)
2255 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
2256 ef10_parttbl_entry_t *parttbl = NULL;
2257 size_t parttbl_rows = 0;
2260 EFSYS_ASSERT(typep != NULL);
2262 if (ef10_parttbl_get(enp, &parttbl, &parttbl_rows) == 0) {
2263 for (i = 0; i < parttbl_rows; i++) {
2264 ef10_parttbl_entry_t *entry = &parttbl[i];
2266 if (entry->partn == partn &&
2267 entry->port == emip->emi_port) {
2268 *typep = entry->nvtype;
2277 __checkReturn efx_rc_t
2279 __in efx_nic_t *enp)
2281 efx_nvram_type_t type;
2282 unsigned int npartns = 0;
2283 uint32_t *partns = NULL;
2288 /* Read available partitions from NVRAM partition map */
2289 size = MC_CMD_NVRAM_PARTITIONS_OUT_TYPE_ID_MAXNUM * sizeof (uint32_t);
2290 EFSYS_KMEM_ALLOC(enp->en_esip, size, partns);
2291 if (partns == NULL) {
2296 if ((rc = efx_mcdi_nvram_partitions(enp, (caddr_t)partns, size,
2301 for (i = 0; i < npartns; i++) {
2302 /* Check if the partition is supported for this port */
2303 if ((rc = ef10_nvram_partn_to_type(enp, partns[i], &type)) != 0)
2306 if ((rc = efx_mcdi_nvram_test(enp, partns[i])) != 0)
2310 EFSYS_KMEM_FREE(enp->en_esip, size, partns);
2317 EFSYS_KMEM_FREE(enp->en_esip, size, partns);
2319 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2323 #endif /* EFSYS_OPT_DIAG */
2325 __checkReturn efx_rc_t
2326 ef10_nvram_partn_get_version(
2327 __in efx_nic_t *enp,
2328 __in uint32_t partn,
2329 __out uint32_t *subtypep,
2330 __out_ecount(4) uint16_t version[4])
2334 /* FIXME: get highest partn version from all ports */
2335 /* FIXME: return partn description if available */
2337 if ((rc = efx_mcdi_nvram_metadata(enp, partn, subtypep,
2338 version, NULL, 0)) != 0)
2344 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2349 __checkReturn efx_rc_t
2350 ef10_nvram_partn_rw_start(
2351 __in efx_nic_t *enp,
2352 __in uint32_t partn,
2353 __out size_t *chunk_sizep)
2357 if ((rc = ef10_nvram_partn_lock(enp, partn)) != 0)
2360 if (chunk_sizep != NULL)
2361 *chunk_sizep = EF10_NVRAM_CHUNK;
2366 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2371 __checkReturn efx_rc_t
2372 ef10_nvram_partn_rw_finish(
2373 __in efx_nic_t *enp,
2374 __in uint32_t partn)
2378 if ((rc = ef10_nvram_partn_unlock(enp, partn, NULL)) != 0)
2384 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2389 #endif /* EFSYS_OPT_NVRAM */
2391 #endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */