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 || EFSYS_OPT_MEDFORD2
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 - 1)) {
234 cursor->current = NULL;
239 /* Check we have value data for current item and an END tag */
240 if (tlv_next_item_ptr(cursor) > cursor->limit) {
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(
666 __in_bcount(partn_size) caddr_t partn_data,
667 __in size_t partn_size)
670 struct tlv_partition_header *header;
671 struct tlv_partition_trailer *trailer;
677 EFX_STATIC_ASSERT(sizeof (*header) <= EF10_NVRAM_CHUNK);
679 if ((partn_data == NULL) || (partn_size == 0)) {
684 /* The partition header must be the first item (at offset zero) */
685 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)partn_data,
690 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
694 header = (struct tlv_partition_header *)tlv_item(&cursor);
696 /* Check TLV partition length (includes the END tag) */
697 total_length = __LE_TO_CPU_32(header->total_length);
698 if (total_length > partn_size) {
703 /* Check partition header matches partn */
704 if (__LE_TO_CPU_16(header->type_id) != partn) {
709 /* Check partition ends with PARTITION_TRAILER and END tags */
710 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
714 trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
716 if ((rc = tlv_advance(&cursor)) != 0) {
720 if (tlv_tag(&cursor) != TLV_TAG_END) {
725 /* Check generation counts are consistent */
726 if (trailer->generation != header->generation) {
731 /* Verify partition checksum */
733 for (pos = 0; (size_t)pos < total_length; pos += sizeof (uint32_t)) {
734 cksum += *((uint32_t *)(partn_data + pos));
762 EFSYS_PROBE1(fail1, efx_rc_t, rc);
768 ef10_nvram_buffer_init(
769 __out_bcount(buffer_size)
771 __in size_t buffer_size)
773 uint32_t *buf = (uint32_t *)bufferp;
775 memset(buf, 0xff, buffer_size);
780 __checkReturn efx_rc_t
781 ef10_nvram_buffer_create(
782 __in uint32_t partn_type,
783 __out_bcount(partn_size)
785 __in size_t partn_size)
787 uint32_t *buf = (uint32_t *)partn_data;
790 struct tlv_partition_header header;
791 struct tlv_partition_trailer trailer;
793 unsigned int min_buf_size = sizeof (struct tlv_partition_header) +
794 sizeof (struct tlv_partition_trailer);
795 if (partn_size < min_buf_size) {
800 ef10_nvram_buffer_init(partn_data, partn_size);
802 if ((rc = tlv_init_cursor(&cursor, buf,
803 (uint32_t *)((uint8_t *)buf + partn_size),
808 header.tag = __CPU_TO_LE_32(TLV_TAG_PARTITION_HEADER);
809 header.length = __CPU_TO_LE_32(sizeof (header) - 8);
810 header.type_id = __CPU_TO_LE_16(partn_type);
812 header.generation = __CPU_TO_LE_32(1);
813 header.total_length = 0; /* This will be fixed below. */
814 if ((rc = tlv_insert(
815 &cursor, TLV_TAG_PARTITION_HEADER,
816 (uint8_t *)&header.type_id, sizeof (header) - 8)) != 0)
818 if ((rc = tlv_advance(&cursor)) != 0)
821 trailer.tag = __CPU_TO_LE_32(TLV_TAG_PARTITION_TRAILER);
822 trailer.length = __CPU_TO_LE_32(sizeof (trailer) - 8);
823 trailer.generation = header.generation;
824 trailer.checksum = 0; /* This will be fixed below. */
825 if ((rc = tlv_insert(&cursor, TLV_TAG_PARTITION_TRAILER,
826 (uint8_t *)&trailer.generation, sizeof (trailer) - 8)) != 0)
829 if ((rc = tlv_update_partition_len_and_cks(&cursor)) != 0)
832 /* Check that the partition is valid. */
833 if ((rc = ef10_nvram_buffer_validate(partn_type,
834 partn_data, partn_size)) != 0)
852 EFSYS_PROBE1(fail1, efx_rc_t, rc);
859 __in uint32_t *position,
862 return (uint32_t)((uint8_t *)position - (uint8_t *)base);
865 __checkReturn efx_rc_t
866 ef10_nvram_buffer_find_item_start(
867 __in_bcount(buffer_size)
869 __in size_t buffer_size,
870 __out uint32_t *startp)
872 /* Read past partition header to find start address of the first key */
876 /* A PARTITION_HEADER tag must be the first item (at offset zero) */
877 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
878 buffer_size)) != 0) {
882 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
887 if ((rc = tlv_advance(&cursor)) != 0) {
891 *startp = byte_offset(cursor.current, cursor.block);
893 if ((rc = tlv_require_end(&cursor)) != 0)
905 EFSYS_PROBE1(fail1, efx_rc_t, rc);
910 __checkReturn efx_rc_t
911 ef10_nvram_buffer_find_end(
912 __in_bcount(buffer_size)
914 __in size_t buffer_size,
915 __in uint32_t offset,
916 __out uint32_t *endp)
918 /* Read to end of partition */
921 uint32_t *segment_used;
923 _NOTE(ARGUNUSED(offset))
925 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
926 buffer_size)) != 0) {
931 segment_used = cursor.block;
934 * Go through each segment and check that it has an end tag. If there
935 * is no end tag then the previous segment was the last valid one,
936 * so return the used space including that end tag.
938 while (tlv_tag(&cursor) == TLV_TAG_PARTITION_HEADER) {
939 if (tlv_require_end(&cursor) != 0) {
940 if (segment_used == cursor.block) {
942 * First segment is corrupt, so there is
943 * no valid data in partition.
950 segment_used = cursor.end + 1;
952 cursor.current = segment_used;
954 /* Return space used (including the END tag) */
955 *endp = (segment_used - cursor.block) * sizeof (uint32_t);
962 EFSYS_PROBE1(fail1, efx_rc_t, rc);
967 __checkReturn __success(return != B_FALSE) boolean_t
968 ef10_nvram_buffer_find_item(
969 __in_bcount(buffer_size)
971 __in size_t buffer_size,
972 __in uint32_t offset,
973 __out uint32_t *startp,
974 __out uint32_t *lengthp)
976 /* Find TLV at offset and return key start and length */
981 if (tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
982 buffer_size, offset) != 0) {
986 while ((key = tlv_item(&cursor)) != NULL) {
987 tag = tlv_tag(&cursor);
988 if (tag == TLV_TAG_PARTITION_HEADER ||
989 tag == TLV_TAG_PARTITION_TRAILER) {
990 if (tlv_advance(&cursor) != 0) {
995 *startp = byte_offset(cursor.current, cursor.block);
996 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
1004 __checkReturn efx_rc_t
1005 ef10_nvram_buffer_peek_item(
1006 __in_bcount(buffer_size)
1008 __in size_t buffer_size,
1009 __in uint32_t offset,
1010 __out uint32_t *tagp,
1011 __out uint32_t *lengthp,
1012 __out uint32_t *value_offsetp)
1015 tlv_cursor_t cursor;
1018 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1019 buffer_size, offset)) != 0) {
1023 tag = tlv_tag(&cursor);
1025 if (tag == TLV_TAG_END) {
1027 * To allow stepping over the END tag, report the full tag
1028 * length and a zero length value.
1030 *lengthp = sizeof (tag);
1031 *value_offsetp = sizeof (tag);
1033 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
1035 *value_offsetp = byte_offset((uint32_t *)tlv_value(&cursor),
1041 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1046 __checkReturn efx_rc_t
1047 ef10_nvram_buffer_get_item(
1048 __in_bcount(buffer_size)
1050 __in size_t buffer_size,
1051 __in uint32_t offset,
1052 __in uint32_t length,
1053 __out uint32_t *tagp,
1054 __out_bcount_part(value_max_size, *lengthp)
1056 __in size_t value_max_size,
1057 __out uint32_t *lengthp)
1060 tlv_cursor_t cursor;
1061 uint32_t value_length;
1063 if (buffer_size < (offset + length)) {
1068 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1069 buffer_size, offset)) != 0) {
1073 value_length = tlv_length(&cursor);
1074 if (value_max_size < value_length) {
1078 memcpy(valuep, tlv_value(&cursor), value_length);
1080 *tagp = tlv_tag(&cursor);
1081 *lengthp = value_length;
1090 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1095 __checkReturn efx_rc_t
1096 ef10_nvram_buffer_insert_item(
1097 __in_bcount(buffer_size)
1099 __in size_t buffer_size,
1100 __in uint32_t offset,
1102 __in_bcount(length) caddr_t valuep,
1103 __in uint32_t length,
1104 __out uint32_t *lengthp)
1107 tlv_cursor_t cursor;
1109 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1110 buffer_size, offset)) != 0) {
1114 rc = tlv_insert(&cursor, tag, (uint8_t *)valuep, length);
1119 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
1127 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1132 __checkReturn efx_rc_t
1133 ef10_nvram_buffer_modify_item(
1134 __in_bcount(buffer_size)
1136 __in size_t buffer_size,
1137 __in uint32_t offset,
1139 __in_bcount(length) caddr_t valuep,
1140 __in uint32_t length,
1141 __out uint32_t *lengthp)
1144 tlv_cursor_t cursor;
1146 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1147 buffer_size, offset)) != 0) {
1151 rc = tlv_modify(&cursor, tag, (uint8_t *)valuep, length);
1157 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
1165 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1171 __checkReturn efx_rc_t
1172 ef10_nvram_buffer_delete_item(
1173 __in_bcount(buffer_size)
1175 __in size_t buffer_size,
1176 __in uint32_t offset,
1177 __in uint32_t length,
1181 tlv_cursor_t cursor;
1183 _NOTE(ARGUNUSED(length, end))
1185 if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1186 buffer_size, offset)) != 0) {
1190 if ((rc = tlv_delete(&cursor)) != 0)
1198 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1203 __checkReturn efx_rc_t
1204 ef10_nvram_buffer_finish(
1205 __in_bcount(buffer_size)
1207 __in size_t buffer_size)
1210 tlv_cursor_t cursor;
1212 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
1213 buffer_size)) != 0) {
1218 if ((rc = tlv_require_end(&cursor)) != 0)
1221 if ((rc = tlv_update_partition_len_and_cks(&cursor)) != 0)
1231 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1239 * Read and validate a segment from a partition. A segment is a complete
1240 * tlv chain between PARTITION_HEADER and PARTITION_END tags. There may
1241 * be multiple segments in a partition, so seg_offset allows segments
1242 * beyond the first to be read.
1244 static __checkReturn efx_rc_t
1245 ef10_nvram_read_tlv_segment(
1246 __in efx_nic_t *enp,
1247 __in uint32_t partn,
1248 __in size_t seg_offset,
1249 __in_bcount(max_seg_size) caddr_t seg_data,
1250 __in size_t max_seg_size)
1252 tlv_cursor_t cursor;
1253 struct tlv_partition_header *header;
1254 struct tlv_partition_trailer *trailer;
1255 size_t total_length;
1260 EFX_STATIC_ASSERT(sizeof (*header) <= EF10_NVRAM_CHUNK);
1262 if ((seg_data == NULL) || (max_seg_size == 0)) {
1267 /* Read initial chunk of the segment, starting at offset */
1268 if ((rc = ef10_nvram_partn_read_mode(enp, partn, seg_offset, seg_data,
1270 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT)) != 0) {
1274 /* A PARTITION_HEADER tag must be the first item at the given offset */
1275 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1276 max_seg_size)) != 0) {
1280 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1284 header = (struct tlv_partition_header *)tlv_item(&cursor);
1286 /* Check TLV segment length (includes the END tag) */
1287 total_length = __LE_TO_CPU_32(header->total_length);
1288 if (total_length > max_seg_size) {
1293 /* Read the remaining segment content */
1294 if (total_length > EF10_NVRAM_CHUNK) {
1295 if ((rc = ef10_nvram_partn_read_mode(enp, partn,
1296 seg_offset + EF10_NVRAM_CHUNK,
1297 seg_data + EF10_NVRAM_CHUNK,
1298 total_length - EF10_NVRAM_CHUNK,
1299 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT)) != 0)
1303 /* Check segment ends with PARTITION_TRAILER and END tags */
1304 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1308 trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
1310 if ((rc = tlv_advance(&cursor)) != 0) {
1314 if (tlv_tag(&cursor) != TLV_TAG_END) {
1319 /* Check data read from segment is consistent */
1320 if (trailer->generation != header->generation) {
1322 * The partition data may have been modified between successive
1323 * MCDI NVRAM_READ requests by the MC or another PCI function.
1325 * The caller must retry to obtain consistent partition data.
1331 /* Verify segment checksum */
1333 for (pos = 0; (size_t)pos < total_length; pos += sizeof (uint32_t)) {
1334 cksum += *((uint32_t *)(seg_data + pos));
1344 EFSYS_PROBE(fail11);
1346 EFSYS_PROBE(fail10);
1364 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1370 * Read a single TLV item from a host memory
1371 * buffer containing a TLV formatted segment.
1373 __checkReturn efx_rc_t
1374 ef10_nvram_buf_read_tlv(
1375 __in efx_nic_t *enp,
1376 __in_bcount(max_seg_size) caddr_t seg_data,
1377 __in size_t max_seg_size,
1379 __deref_out_bcount_opt(*sizep) caddr_t *datap,
1380 __out size_t *sizep)
1382 tlv_cursor_t cursor;
1388 _NOTE(ARGUNUSED(enp))
1390 if ((seg_data == NULL) || (max_seg_size == 0)) {
1395 /* Find requested TLV tag in segment data */
1396 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1397 max_seg_size)) != 0) {
1401 if ((rc = tlv_find(&cursor, tag)) != 0) {
1405 value = (caddr_t)tlv_value(&cursor);
1406 length = tlv_length(&cursor);
1411 /* Copy out data from TLV item */
1412 EFSYS_KMEM_ALLOC(enp->en_esip, length, data);
1417 memcpy(data, value, length);
1432 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1437 /* Read a single TLV item from the first segment in a TLV formatted partition */
1438 __checkReturn efx_rc_t
1439 ef10_nvram_partn_read_tlv(
1440 __in efx_nic_t *enp,
1441 __in uint32_t partn,
1443 __deref_out_bcount_opt(*seg_sizep) caddr_t *seg_datap,
1444 __out size_t *seg_sizep)
1446 caddr_t seg_data = NULL;
1447 size_t partn_size = 0;
1453 /* Allocate sufficient memory for the entire partition */
1454 if ((rc = ef10_nvram_partn_size(enp, partn, &partn_size)) != 0)
1457 if (partn_size == 0) {
1462 EFSYS_KMEM_ALLOC(enp->en_esip, partn_size, seg_data);
1463 if (seg_data == NULL) {
1469 * Read the first segment in a TLV partition. Retry until consistent
1470 * segment contents are returned. Inconsistent data may be read if:
1471 * a) the segment contents are invalid
1472 * b) the MC has rebooted while we were reading the partition
1473 * c) the partition has been modified while we were reading it
1474 * Limit retry attempts to ensure forward progress.
1478 if ((rc = ef10_nvram_read_tlv_segment(enp, partn, 0,
1479 seg_data, partn_size)) != 0)
1481 } while ((rc == EAGAIN) && (retry > 0));
1484 /* Failed to obtain consistent segment data */
1491 if ((rc = ef10_nvram_buf_read_tlv(enp, seg_data, partn_size,
1492 tag, &data, &length)) != 0)
1495 EFSYS_KMEM_FREE(enp->en_esip, partn_size, seg_data);
1498 *seg_sizep = length;
1507 EFSYS_KMEM_FREE(enp->en_esip, partn_size, seg_data);
1513 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1518 /* Compute the size of a segment. */
1519 static __checkReturn efx_rc_t
1520 ef10_nvram_buf_segment_size(
1521 __in caddr_t seg_data,
1522 __in size_t max_seg_size,
1523 __out size_t *seg_sizep)
1526 tlv_cursor_t cursor;
1527 struct tlv_partition_header *header;
1530 uint32_t *end_tag_position;
1531 uint32_t segment_length;
1533 /* A PARTITION_HEADER tag must be the first item at the given offset */
1534 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1535 max_seg_size)) != 0) {
1539 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1543 header = (struct tlv_partition_header *)tlv_item(&cursor);
1545 /* Check TLV segment length (includes the END tag) */
1546 *seg_sizep = __LE_TO_CPU_32(header->total_length);
1547 if (*seg_sizep > max_seg_size) {
1552 /* Check segment ends with PARTITION_TRAILER and END tags */
1553 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1558 if ((rc = tlv_advance(&cursor)) != 0) {
1562 if (tlv_tag(&cursor) != TLV_TAG_END) {
1566 end_tag_position = cursor.current;
1568 /* Verify segment checksum */
1570 for (pos = 0; (size_t)pos < *seg_sizep; pos += sizeof (uint32_t)) {
1571 cksum += *((uint32_t *)(seg_data + pos));
1579 * Calculate total length from HEADER to END tags and compare to
1580 * max_seg_size and the total_length field in the HEADER tag.
1582 segment_length = tlv_block_length_used(&cursor);
1584 if (segment_length > max_seg_size) {
1589 if (segment_length != *seg_sizep) {
1594 /* Skip over the first HEADER tag. */
1595 rc = tlv_rewind(&cursor);
1596 rc = tlv_advance(&cursor);
1599 if (tlv_tag(&cursor) == TLV_TAG_END) {
1600 /* Check that the END tag is the one found earlier. */
1601 if (cursor.current != end_tag_position)
1605 /* Check for duplicate HEADER tags before the END tag. */
1606 if (tlv_tag(&cursor) == TLV_TAG_PARTITION_HEADER) {
1611 rc = tlv_advance(&cursor);
1619 EFSYS_PROBE(fail12);
1621 EFSYS_PROBE(fail11);
1623 EFSYS_PROBE(fail10);
1641 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1647 * Add or update a single TLV item in a host memory buffer containing a TLV
1648 * formatted segment. Historically partitions consisted of only one segment.
1650 __checkReturn efx_rc_t
1651 ef10_nvram_buf_write_tlv(
1652 __inout_bcount(max_seg_size) caddr_t seg_data,
1653 __in size_t max_seg_size,
1655 __in_bcount(tag_size) caddr_t tag_data,
1656 __in size_t tag_size,
1657 __out size_t *total_lengthp)
1659 tlv_cursor_t cursor;
1660 struct tlv_partition_header *header;
1661 struct tlv_partition_trailer *trailer;
1662 uint32_t generation;
1667 /* A PARTITION_HEADER tag must be the first item (at offset zero) */
1668 if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1669 max_seg_size)) != 0) {
1673 if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1677 header = (struct tlv_partition_header *)tlv_item(&cursor);
1679 /* Update the TLV chain to contain the new data */
1680 if ((rc = tlv_find(&cursor, tag)) == 0) {
1681 /* Modify existing TLV item */
1682 if ((rc = tlv_modify(&cursor, tag,
1683 (uint8_t *)tag_data, tag_size)) != 0)
1686 /* Insert a new TLV item before the PARTITION_TRAILER */
1687 rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER);
1692 if ((rc = tlv_insert(&cursor, tag,
1693 (uint8_t *)tag_data, tag_size)) != 0) {
1699 /* Find the trailer tag */
1700 if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1704 trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
1706 /* Update PARTITION_HEADER and PARTITION_TRAILER fields */
1707 *total_lengthp = tlv_block_length_used(&cursor);
1708 if (*total_lengthp > max_seg_size) {
1712 generation = __LE_TO_CPU_32(header->generation) + 1;
1714 header->total_length = __CPU_TO_LE_32(*total_lengthp);
1715 header->generation = __CPU_TO_LE_32(generation);
1716 trailer->generation = __CPU_TO_LE_32(generation);
1718 /* Recompute PARTITION_TRAILER checksum */
1719 trailer->checksum = 0;
1721 for (pos = 0; (size_t)pos < *total_lengthp; pos += sizeof (uint32_t)) {
1722 cksum += *((uint32_t *)(seg_data + pos));
1724 trailer->checksum = ~cksum + 1;
1741 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1747 * Add or update a single TLV item in the first segment of a TLV formatted
1748 * dynamic config partition. The first segment is the current active
1751 __checkReturn efx_rc_t
1752 ef10_nvram_partn_write_tlv(
1753 __in efx_nic_t *enp,
1754 __in uint32_t partn,
1756 __in_bcount(size) caddr_t data,
1759 return ef10_nvram_partn_write_segment_tlv(enp, partn, tag, data,
1764 * Read a segment from nvram at the given offset into a buffer (segment_data)
1765 * and optionally write a new tag to it.
1767 static __checkReturn efx_rc_t
1768 ef10_nvram_segment_write_tlv(
1769 __in efx_nic_t *enp,
1770 __in uint32_t partn,
1772 __in_bcount(size) caddr_t data,
1774 __inout caddr_t *seg_datap,
1775 __inout size_t *partn_offsetp,
1776 __inout size_t *src_remain_lenp,
1777 __inout size_t *dest_remain_lenp,
1778 __in boolean_t write)
1782 size_t original_segment_size;
1783 size_t modified_segment_size;
1786 * Read the segment from NVRAM into the segment_data buffer and validate
1787 * it, returning if it does not validate. This is not a failure unless
1788 * this is the first segment in a partition. In this case the caller
1789 * must propagate the error.
1791 status = ef10_nvram_read_tlv_segment(enp, partn, *partn_offsetp,
1792 *seg_datap, *src_remain_lenp);
1798 status = ef10_nvram_buf_segment_size(*seg_datap,
1799 *src_remain_lenp, &original_segment_size);
1806 /* Update the contents of the segment in the buffer */
1807 if ((rc = ef10_nvram_buf_write_tlv(*seg_datap,
1808 *dest_remain_lenp, tag, data, size,
1809 &modified_segment_size)) != 0) {
1812 *dest_remain_lenp -= modified_segment_size;
1813 *seg_datap += modified_segment_size;
1816 * We won't modify this segment, but still need to update the
1817 * remaining lengths and pointers.
1819 *dest_remain_lenp -= original_segment_size;
1820 *seg_datap += original_segment_size;
1823 *partn_offsetp += original_segment_size;
1824 *src_remain_lenp -= original_segment_size;
1833 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1839 * Add or update a single TLV item in either the first segment or in all
1840 * segments in a TLV formatted dynamic config partition. Dynamic config
1841 * partitions on boards that support RFID are divided into a number of segments,
1842 * each formatted like a partition, with header, trailer and end tags. The first
1843 * segment is the current active configuration.
1845 * The segments are initialised by manftest and each contain a different
1846 * configuration e.g. firmware variant. The firmware can be instructed
1847 * via RFID to copy a segment to replace the first segment, hence changing the
1848 * active configuration. This allows ops to change the configuration of a board
1849 * prior to shipment using RFID.
1851 * Changes to the dynamic config may need to be written to all segments (e.g.
1852 * firmware versions) or just the first segment (changes to the active
1853 * configuration). See SF-111324-SW "The use of RFID in Solarflare Products".
1854 * If only the first segment is written the code still needs to be aware of the
1855 * possible presence of subsequent segments as writing to a segment may cause
1856 * its size to increase, which would overwrite the subsequent segments and
1859 __checkReturn efx_rc_t
1860 ef10_nvram_partn_write_segment_tlv(
1861 __in efx_nic_t *enp,
1862 __in uint32_t partn,
1864 __in_bcount(size) caddr_t data,
1866 __in boolean_t all_segments)
1868 size_t partn_size = 0;
1870 size_t total_length = 0;
1872 size_t current_offset = 0;
1873 size_t remaining_original_length;
1874 size_t remaining_modified_length;
1875 caddr_t segment_data;
1877 EFSYS_ASSERT3U(partn, ==, NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG);
1879 /* Allocate sufficient memory for the entire partition */
1880 if ((rc = ef10_nvram_partn_size(enp, partn, &partn_size)) != 0)
1883 EFSYS_KMEM_ALLOC(enp->en_esip, partn_size, partn_data);
1884 if (partn_data == NULL) {
1889 remaining_original_length = partn_size;
1890 remaining_modified_length = partn_size;
1891 segment_data = partn_data;
1893 /* Lock the partition */
1894 if ((rc = ef10_nvram_partn_lock(enp, partn)) != 0)
1897 /* Iterate over each (potential) segment to update it. */
1899 boolean_t write = all_segments || current_offset == 0;
1901 rc = ef10_nvram_segment_write_tlv(enp, partn, tag, data, size,
1902 &segment_data, ¤t_offset, &remaining_original_length,
1903 &remaining_modified_length, write);
1905 if (current_offset == 0) {
1907 * If no data has been read then the first
1908 * segment is invalid, which is an error.
1914 } while (current_offset < partn_size);
1916 total_length = segment_data - partn_data;
1919 * We've run out of space. This should actually be dealt with by
1920 * ef10_nvram_buf_write_tlv returning ENOSPC.
1922 if (total_length > partn_size) {
1927 /* Erase the whole partition in NVRAM */
1928 if ((rc = ef10_nvram_partn_erase(enp, partn, 0, partn_size)) != 0)
1931 /* Write new partition contents from the buffer to NVRAM */
1932 if ((rc = ef10_nvram_partn_write(enp, partn, 0, partn_data,
1933 total_length)) != 0)
1936 /* Unlock the partition */
1937 (void) ef10_nvram_partn_unlock(enp, partn, NULL);
1939 EFSYS_KMEM_FREE(enp->en_esip, partn_size, partn_data);
1952 (void) ef10_nvram_partn_unlock(enp, partn, NULL);
1956 EFSYS_KMEM_FREE(enp->en_esip, partn_size, partn_data);
1960 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1966 * Get the size of a NVRAM partition. This is the total size allocated in nvram,
1967 * not the data used by the segments in the partition.
1969 __checkReturn efx_rc_t
1970 ef10_nvram_partn_size(
1971 __in efx_nic_t *enp,
1972 __in uint32_t partn,
1973 __out size_t *sizep)
1977 if ((rc = efx_mcdi_nvram_info(enp, partn, sizep,
1978 NULL, NULL, NULL)) != 0)
1984 EFSYS_PROBE1(fail1, efx_rc_t, rc);
1989 __checkReturn efx_rc_t
1990 ef10_nvram_partn_lock(
1991 __in efx_nic_t *enp,
1992 __in uint32_t partn)
1996 if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0)
2002 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2007 __checkReturn efx_rc_t
2008 ef10_nvram_partn_read_mode(
2009 __in efx_nic_t *enp,
2010 __in uint32_t partn,
2011 __in unsigned int offset,
2012 __out_bcount(size) caddr_t data,
2020 chunk = MIN(size, EF10_NVRAM_CHUNK);
2022 if ((rc = efx_mcdi_nvram_read(enp, partn, offset,
2023 data, chunk, mode)) != 0) {
2035 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2040 __checkReturn efx_rc_t
2041 ef10_nvram_partn_read(
2042 __in efx_nic_t *enp,
2043 __in uint32_t partn,
2044 __in unsigned int offset,
2045 __out_bcount(size) caddr_t data,
2049 * An A/B partition has two data stores (current and backup).
2050 * Read requests which come in through the EFX API expect to read the
2051 * current, active store of an A/B partition. For non A/B partitions,
2052 * there is only a single store and so the mode param is ignored.
2054 return ef10_nvram_partn_read_mode(enp, partn, offset, data, size,
2055 MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT);
2058 __checkReturn efx_rc_t
2059 ef10_nvram_partn_read_backup(
2060 __in efx_nic_t *enp,
2061 __in uint32_t partn,
2062 __in unsigned int offset,
2063 __out_bcount(size) caddr_t data,
2067 * An A/B partition has two data stores (current and backup).
2068 * Read the backup store of an A/B partition (i.e. the store currently
2069 * being written to if the partition is locked).
2071 * This is needed when comparing the existing partition content to avoid
2072 * unnecessary writes, or to read back what has been written to check
2073 * that the writes have succeeded.
2075 return ef10_nvram_partn_read_mode(enp, partn, offset, data, size,
2076 MC_CMD_NVRAM_READ_IN_V2_TARGET_BACKUP);
2079 __checkReturn efx_rc_t
2080 ef10_nvram_partn_erase(
2081 __in efx_nic_t *enp,
2082 __in uint32_t partn,
2083 __in unsigned int offset,
2087 uint32_t erase_size;
2089 if ((rc = efx_mcdi_nvram_info(enp, partn, NULL, NULL,
2090 &erase_size, NULL)) != 0)
2093 if (erase_size == 0) {
2094 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset, size)) != 0)
2097 if (size % erase_size != 0) {
2102 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset,
2105 offset += erase_size;
2119 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2124 __checkReturn efx_rc_t
2125 ef10_nvram_partn_write(
2126 __in efx_nic_t *enp,
2127 __in uint32_t partn,
2128 __in unsigned int offset,
2129 __in_bcount(size) caddr_t data,
2133 uint32_t write_size;
2136 if ((rc = efx_mcdi_nvram_info(enp, partn, NULL, NULL,
2137 NULL, &write_size)) != 0)
2140 if (write_size != 0) {
2142 * Check that the size is a multiple of the write chunk size if
2143 * the write chunk size is available.
2145 if (size % write_size != 0) {
2150 write_size = EF10_NVRAM_CHUNK;
2154 chunk = MIN(size, write_size);
2156 if ((rc = efx_mcdi_nvram_write(enp, partn, offset,
2157 data, chunk)) != 0) {
2173 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2178 __checkReturn efx_rc_t
2179 ef10_nvram_partn_unlock(
2180 __in efx_nic_t *enp,
2181 __in uint32_t partn,
2182 __out_opt uint32_t *verify_resultp)
2184 boolean_t reboot = B_FALSE;
2187 if (verify_resultp != NULL)
2188 *verify_resultp = MC_CMD_NVRAM_VERIFY_RC_UNKNOWN;
2190 rc = efx_mcdi_nvram_update_finish(enp, partn, reboot, verify_resultp);
2197 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2202 __checkReturn efx_rc_t
2203 ef10_nvram_partn_set_version(
2204 __in efx_nic_t *enp,
2205 __in uint32_t partn,
2206 __in_ecount(4) uint16_t version[4])
2208 struct tlv_partition_version partn_version;
2212 /* Add or modify partition version TLV item */
2213 partn_version.version_w = __CPU_TO_LE_16(version[0]);
2214 partn_version.version_x = __CPU_TO_LE_16(version[1]);
2215 partn_version.version_y = __CPU_TO_LE_16(version[2]);
2216 partn_version.version_z = __CPU_TO_LE_16(version[3]);
2218 size = sizeof (partn_version) - (2 * sizeof (uint32_t));
2220 /* Write the version number to all segments in the partition */
2221 if ((rc = ef10_nvram_partn_write_segment_tlv(enp,
2222 NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG,
2223 TLV_TAG_PARTITION_VERSION(partn),
2224 (caddr_t)&partn_version.version_w, size, B_TRUE)) != 0)
2230 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2235 #endif /* EFSYS_OPT_VPD || EFSYS_OPT_NVRAM */
2239 typedef struct ef10_parttbl_entry_s {
2241 unsigned int port_mask;
2242 efx_nvram_type_t nvtype;
2243 } ef10_parttbl_entry_t;
2245 /* Port mask values */
2246 #define PORT_1 (1u << 1)
2247 #define PORT_2 (1u << 2)
2248 #define PORT_3 (1u << 3)
2249 #define PORT_4 (1u << 4)
2250 #define PORT_ALL (0xffffffffu)
2252 #define PARTN_MAP_ENTRY(partn, port_mask, nvtype) \
2253 { (NVRAM_PARTITION_TYPE_##partn), (PORT_##port_mask), (EFX_NVRAM_##nvtype) }
2255 /* Translate EFX NVRAM types to firmware partition types */
2256 static ef10_parttbl_entry_t hunt_parttbl[] = {
2257 /* partn ports nvtype */
2258 PARTN_MAP_ENTRY(MC_FIRMWARE, ALL, MC_FIRMWARE),
2259 PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP, ALL, MC_GOLDEN),
2260 PARTN_MAP_ENTRY(EXPANSION_ROM, ALL, BOOTROM),
2261 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT0, 1, BOOTROM_CFG),
2262 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT1, 2, BOOTROM_CFG),
2263 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT2, 3, BOOTROM_CFG),
2264 PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT3, 4, BOOTROM_CFG),
2265 PARTN_MAP_ENTRY(DYNAMIC_CONFIG, ALL, DYNAMIC_CFG),
2266 PARTN_MAP_ENTRY(FPGA, ALL, FPGA),
2267 PARTN_MAP_ENTRY(FPGA_BACKUP, ALL, FPGA_BACKUP),
2268 PARTN_MAP_ENTRY(LICENSE, ALL, LICENSE),
2271 static ef10_parttbl_entry_t medford_parttbl[] = {
2272 /* partn ports nvtype */
2273 PARTN_MAP_ENTRY(MC_FIRMWARE, ALL, MC_FIRMWARE),
2274 PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP, ALL, MC_GOLDEN),
2275 PARTN_MAP_ENTRY(EXPANSION_ROM, ALL, BOOTROM),
2276 PARTN_MAP_ENTRY(EXPROM_CONFIG, ALL, BOOTROM_CFG),
2277 PARTN_MAP_ENTRY(DYNAMIC_CONFIG, ALL, DYNAMIC_CFG),
2278 PARTN_MAP_ENTRY(FPGA, ALL, FPGA),
2279 PARTN_MAP_ENTRY(FPGA_BACKUP, ALL, FPGA_BACKUP),
2280 PARTN_MAP_ENTRY(LICENSE, ALL, LICENSE),
2281 PARTN_MAP_ENTRY(EXPANSION_UEFI, ALL, UEFIROM),
2282 PARTN_MAP_ENTRY(MUM_FIRMWARE, ALL, MUM_FIRMWARE),
2285 static ef10_parttbl_entry_t medford2_parttbl[] = {
2286 /* partn ports nvtype */
2287 PARTN_MAP_ENTRY(MC_FIRMWARE, ALL, MC_FIRMWARE),
2288 PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP, ALL, MC_GOLDEN),
2289 PARTN_MAP_ENTRY(EXPANSION_ROM, ALL, BOOTROM),
2290 PARTN_MAP_ENTRY(EXPROM_CONFIG, ALL, BOOTROM_CFG),
2291 PARTN_MAP_ENTRY(DYNAMIC_CONFIG, ALL, DYNAMIC_CFG),
2292 PARTN_MAP_ENTRY(FPGA, ALL, FPGA),
2293 PARTN_MAP_ENTRY(FPGA_BACKUP, ALL, FPGA_BACKUP),
2294 PARTN_MAP_ENTRY(LICENSE, ALL, LICENSE),
2295 PARTN_MAP_ENTRY(EXPANSION_UEFI, ALL, UEFIROM),
2296 PARTN_MAP_ENTRY(MUM_FIRMWARE, ALL, MUM_FIRMWARE),
2297 PARTN_MAP_ENTRY(DYNCONFIG_DEFAULTS, ALL, DYNCONFIG_DEFAULTS),
2298 PARTN_MAP_ENTRY(ROMCONFIG_DEFAULTS, ALL, ROMCONFIG_DEFAULTS),
2301 static __checkReturn efx_rc_t
2303 __in efx_nic_t *enp,
2304 __out ef10_parttbl_entry_t **parttblp,
2305 __out size_t *parttbl_rowsp)
2307 switch (enp->en_family) {
2308 case EFX_FAMILY_HUNTINGTON:
2309 *parttblp = hunt_parttbl;
2310 *parttbl_rowsp = EFX_ARRAY_SIZE(hunt_parttbl);
2313 case EFX_FAMILY_MEDFORD:
2314 *parttblp = medford_parttbl;
2315 *parttbl_rowsp = EFX_ARRAY_SIZE(medford_parttbl);
2318 case EFX_FAMILY_MEDFORD2:
2319 *parttblp = medford2_parttbl;
2320 *parttbl_rowsp = EFX_ARRAY_SIZE(medford2_parttbl);
2324 EFSYS_ASSERT(B_FALSE);
2330 __checkReturn efx_rc_t
2331 ef10_nvram_type_to_partn(
2332 __in efx_nic_t *enp,
2333 __in efx_nvram_type_t type,
2334 __out uint32_t *partnp)
2336 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
2337 ef10_parttbl_entry_t *parttbl = NULL;
2338 size_t parttbl_rows = 0;
2341 EFSYS_ASSERT3U(type, !=, EFX_NVRAM_INVALID);
2342 EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
2343 EFSYS_ASSERT(partnp != NULL);
2345 if (ef10_parttbl_get(enp, &parttbl, &parttbl_rows) == 0) {
2346 for (i = 0; i < parttbl_rows; i++) {
2347 ef10_parttbl_entry_t *entry = &parttbl[i];
2349 if ((entry->nvtype == type) &&
2350 (entry->port_mask & (1u << emip->emi_port))) {
2351 *partnp = entry->partn;
2362 static __checkReturn efx_rc_t
2363 ef10_nvram_partn_to_type(
2364 __in efx_nic_t *enp,
2365 __in uint32_t partn,
2366 __out efx_nvram_type_t *typep)
2368 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
2369 ef10_parttbl_entry_t *parttbl = NULL;
2370 size_t parttbl_rows = 0;
2373 EFSYS_ASSERT(typep != NULL);
2375 if (ef10_parttbl_get(enp, &parttbl, &parttbl_rows) == 0) {
2376 for (i = 0; i < parttbl_rows; i++) {
2377 ef10_parttbl_entry_t *entry = &parttbl[i];
2379 if ((entry->partn == partn) &&
2380 (entry->port_mask & (1u << emip->emi_port))) {
2381 *typep = entry->nvtype;
2390 __checkReturn efx_rc_t
2392 __in efx_nic_t *enp)
2394 efx_nvram_type_t type;
2395 unsigned int npartns = 0;
2396 uint32_t *partns = NULL;
2401 /* Read available partitions from NVRAM partition map */
2402 size = MC_CMD_NVRAM_PARTITIONS_OUT_TYPE_ID_MAXNUM * sizeof (uint32_t);
2403 EFSYS_KMEM_ALLOC(enp->en_esip, size, partns);
2404 if (partns == NULL) {
2409 if ((rc = efx_mcdi_nvram_partitions(enp, (caddr_t)partns, size,
2414 for (i = 0; i < npartns; i++) {
2415 /* Check if the partition is supported for this port */
2416 if ((rc = ef10_nvram_partn_to_type(enp, partns[i], &type)) != 0)
2419 if ((rc = efx_mcdi_nvram_test(enp, partns[i])) != 0)
2423 EFSYS_KMEM_FREE(enp->en_esip, size, partns);
2430 EFSYS_KMEM_FREE(enp->en_esip, size, partns);
2432 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2436 #endif /* EFSYS_OPT_DIAG */
2438 __checkReturn efx_rc_t
2439 ef10_nvram_partn_get_version(
2440 __in efx_nic_t *enp,
2441 __in uint32_t partn,
2442 __out uint32_t *subtypep,
2443 __out_ecount(4) uint16_t version[4])
2447 /* FIXME: get highest partn version from all ports */
2448 /* FIXME: return partn description if available */
2450 if ((rc = efx_mcdi_nvram_metadata(enp, partn, subtypep,
2451 version, NULL, 0)) != 0)
2457 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2462 __checkReturn efx_rc_t
2463 ef10_nvram_partn_rw_start(
2464 __in efx_nic_t *enp,
2465 __in uint32_t partn,
2466 __out size_t *chunk_sizep)
2468 uint32_t write_size = 0;
2471 if ((rc = efx_mcdi_nvram_info(enp, partn, NULL, NULL,
2472 NULL, &write_size)) != 0)
2475 if ((rc = ef10_nvram_partn_lock(enp, partn)) != 0)
2478 if (chunk_sizep != NULL) {
2479 if (write_size == 0)
2480 *chunk_sizep = EF10_NVRAM_CHUNK;
2482 *chunk_sizep = write_size;
2490 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2495 __checkReturn efx_rc_t
2496 ef10_nvram_partn_rw_finish(
2497 __in efx_nic_t *enp,
2498 __in uint32_t partn,
2499 __out_opt uint32_t *verify_resultp)
2503 if ((rc = ef10_nvram_partn_unlock(enp, partn, verify_resultp)) != 0)
2509 EFSYS_PROBE1(fail1, efx_rc_t, rc);
2514 #endif /* EFSYS_OPT_NVRAM */
2516 #endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */