New upstream version 18.02
[deb_dpdk.git] / drivers / net / sfc / base / ef10_nvram.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2012-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9
10 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
11
12 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
13
14 #include "ef10_tlv_layout.h"
15
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 */
22 } tlv_cursor_t;
23
24 typedef struct nvram_partition_s {
25         uint16_t type;
26         uint8_t chip_select;
27         uint8_t flags;
28         /*
29          * The full length of the NVRAM partition.
30          * This is different from tlv_partition_header.total_length,
31          *  which can be smaller.
32          */
33         uint32_t length;
34         uint32_t erase_size;
35         uint32_t *data;
36         tlv_cursor_t tlv_cursor;
37 } nvram_partition_t;
38
39
40 static  __checkReturn           efx_rc_t
41 tlv_validate_state(
42         __inout                 tlv_cursor_t *cursor);
43
44
45 static                          void
46 tlv_init_block(
47         __out   uint32_t        *block)
48 {
49         *block = __CPU_TO_LE_32(TLV_TAG_END);
50 }
51
52 static                          uint32_t
53 tlv_tag(
54         __in    tlv_cursor_t    *cursor)
55 {
56         uint32_t dword, tag;
57
58         dword = cursor->current[0];
59         tag = __LE_TO_CPU_32(dword);
60
61         return (tag);
62 }
63
64 static                          size_t
65 tlv_length(
66         __in    tlv_cursor_t    *cursor)
67 {
68         uint32_t dword, length;
69
70         if (tlv_tag(cursor) == TLV_TAG_END)
71                 return (0);
72
73         dword = cursor->current[1];
74         length = __LE_TO_CPU_32(dword);
75
76         return ((size_t)length);
77 }
78
79 static                          uint8_t *
80 tlv_value(
81         __in    tlv_cursor_t    *cursor)
82 {
83         if (tlv_tag(cursor) == TLV_TAG_END)
84                 return (NULL);
85
86         return ((uint8_t *)(&cursor->current[2]));
87 }
88
89 static                          uint8_t *
90 tlv_item(
91         __in    tlv_cursor_t    *cursor)
92 {
93         if (tlv_tag(cursor) == TLV_TAG_END)
94                 return (NULL);
95
96         return ((uint8_t *)cursor->current);
97 }
98
99 /*
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
102  */
103 #define TLV_DWORD_COUNT(length) \
104         (1 + 1 + (((length) + sizeof (uint32_t) - 1) / sizeof (uint32_t)))
105
106
107 static                          uint32_t *
108 tlv_next_item_ptr(
109         __in    tlv_cursor_t    *cursor)
110 {
111         uint32_t length;
112
113         length = tlv_length(cursor);
114
115         return (cursor->current + TLV_DWORD_COUNT(length));
116 }
117
118 static  __checkReturn           efx_rc_t
119 tlv_advance(
120         __inout tlv_cursor_t    *cursor)
121 {
122         efx_rc_t rc;
123
124         if ((rc = tlv_validate_state(cursor)) != 0)
125                 goto fail1;
126
127         if (cursor->current == cursor->end) {
128                 /* No more tags after END tag */
129                 cursor->current = NULL;
130                 rc = ENOENT;
131                 goto fail2;
132         }
133
134         /* Advance to next item and validate */
135         cursor->current = tlv_next_item_ptr(cursor);
136
137         if ((rc = tlv_validate_state(cursor)) != 0)
138                 goto fail3;
139
140         return (0);
141
142 fail3:
143         EFSYS_PROBE(fail3);
144 fail2:
145         EFSYS_PROBE(fail2);
146 fail1:
147         EFSYS_PROBE1(fail1, efx_rc_t, rc);
148
149         return (rc);
150 }
151
152 static                          efx_rc_t
153 tlv_rewind(
154         __in    tlv_cursor_t    *cursor)
155 {
156         efx_rc_t rc;
157
158         cursor->current = cursor->block;
159
160         if ((rc = tlv_validate_state(cursor)) != 0)
161                 goto fail1;
162
163         return (0);
164
165 fail1:
166         EFSYS_PROBE1(fail1, efx_rc_t, rc);
167
168         return (rc);
169 }
170
171 static                          efx_rc_t
172 tlv_find(
173         __inout tlv_cursor_t    *cursor,
174         __in    uint32_t        tag)
175 {
176         efx_rc_t rc;
177
178         rc = tlv_rewind(cursor);
179         while (rc == 0) {
180                 if (tlv_tag(cursor) == tag)
181                         break;
182
183                 rc = tlv_advance(cursor);
184         }
185         return (rc);
186 }
187
188 static  __checkReturn           efx_rc_t
189 tlv_validate_state(
190         __inout tlv_cursor_t    *cursor)
191 {
192         efx_rc_t rc;
193
194         /* Check cursor position */
195         if (cursor->current < cursor->block) {
196                 rc = EINVAL;
197                 goto fail1;
198         }
199         if (cursor->current > cursor->limit) {
200                 rc = EINVAL;
201                 goto fail2;
202         }
203
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;
208                         rc = EFAULT;
209                         goto fail3;
210                 }
211
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;
215                         rc = EFAULT;
216                         goto fail4;
217                 }
218         }
219
220         return (0);
221
222 fail4:
223         EFSYS_PROBE(fail4);
224 fail3:
225         EFSYS_PROBE(fail3);
226 fail2:
227         EFSYS_PROBE(fail2);
228 fail1:
229         EFSYS_PROBE1(fail1, efx_rc_t, rc);
230
231         return (rc);
232 }
233
234 static                          efx_rc_t
235 tlv_init_cursor(
236         __out   tlv_cursor_t    *cursor,
237         __in    uint32_t        *block,
238         __in    uint32_t        *limit,
239         __in    uint32_t        *current)
240 {
241         cursor->block   = block;
242         cursor->limit   = limit;
243
244         cursor->current = current;
245         cursor->end     = NULL;
246
247         return (tlv_validate_state(cursor));
248 }
249
250 static  __checkReturn           efx_rc_t
251 tlv_init_cursor_from_size(
252         __out   tlv_cursor_t    *cursor,
253         __in_bcount(size)
254                 uint8_t         *block,
255         __in    size_t          size)
256 {
257         uint32_t *limit;
258         limit = (uint32_t *)(block + size - sizeof (uint32_t));
259         return (tlv_init_cursor(cursor, (uint32_t *)block,
260                 limit, (uint32_t *)block));
261 }
262
263 static  __checkReturn           efx_rc_t
264 tlv_init_cursor_at_offset(
265         __out   tlv_cursor_t    *cursor,
266         __in_bcount(size)
267                 uint8_t         *block,
268         __in    size_t          size,
269         __in    size_t          offset)
270 {
271         uint32_t *limit;
272         uint32_t *current;
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));
276 }
277
278 static  __checkReturn           efx_rc_t
279 tlv_require_end(
280         __inout tlv_cursor_t    *cursor)
281 {
282         uint32_t *pos;
283         efx_rc_t rc;
284
285         if (cursor->end == NULL) {
286                 pos = cursor->current;
287                 if ((rc = tlv_find(cursor, TLV_TAG_END)) != 0)
288                         goto fail1;
289
290                 cursor->end = cursor->current;
291                 cursor->current = pos;
292         }
293
294         return (0);
295
296 fail1:
297         EFSYS_PROBE1(fail1, efx_rc_t, rc);
298
299         return (rc);
300 }
301
302 static                          size_t
303 tlv_block_length_used(
304         __inout tlv_cursor_t    *cursor)
305 {
306         efx_rc_t rc;
307
308         if ((rc = tlv_validate_state(cursor)) != 0)
309                 goto fail1;
310
311         if ((rc = tlv_require_end(cursor)) != 0)
312                 goto fail2;
313
314         /* Return space used (including the END tag) */
315         return (cursor->end + 1 - cursor->block) * sizeof (uint32_t);
316
317 fail2:
318         EFSYS_PROBE(fail2);
319 fail1:
320         EFSYS_PROBE1(fail1, efx_rc_t, rc);
321
322         return (0);
323 }
324
325 static          uint32_t *
326 tlv_last_segment_end(
327         __in    tlv_cursor_t *cursor)
328 {
329         tlv_cursor_t segment_cursor;
330         uint32_t *last_segment_end = cursor->block;
331         uint32_t *segment_start = cursor->block;
332
333         /*
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.
337          */
338         for (;;) {
339                 if (tlv_init_cursor(&segment_cursor, segment_start,
340                     cursor->limit, segment_start) != 0)
341                         break;
342                 if (tlv_require_end(&segment_cursor) != 0)
343                         break;
344                 last_segment_end = segment_cursor.end;
345                 segment_start = segment_cursor.end + 1;
346         }
347
348         return (last_segment_end);
349 }
350
351
352 static                          uint32_t *
353 tlv_write(
354         __in                    tlv_cursor_t *cursor,
355         __in                    uint32_t tag,
356         __in_bcount(size)       uint8_t *data,
357         __in                    size_t size)
358 {
359         uint32_t len = size;
360         uint32_t *ptr;
361
362         ptr = cursor->current;
363
364         *ptr++ = __CPU_TO_LE_32(tag);
365         *ptr++ = __CPU_TO_LE_32(len);
366
367         if (len > 0) {
368                 ptr[(len - 1) / sizeof (uint32_t)] = 0;
369                 memcpy(ptr, data, len);
370                 ptr += P2ROUNDUP(len, sizeof (uint32_t)) / sizeof (*ptr);
371         }
372
373         return (ptr);
374 }
375
376 static  __checkReturn           efx_rc_t
377 tlv_insert(
378         __inout tlv_cursor_t    *cursor,
379         __in    uint32_t        tag,
380         __in_bcount(size)
381                 uint8_t         *data,
382         __in    size_t          size)
383 {
384         unsigned int delta;
385         uint32_t *last_segment_end;
386         efx_rc_t rc;
387
388         if ((rc = tlv_validate_state(cursor)) != 0)
389                 goto fail1;
390
391         if ((rc = tlv_require_end(cursor)) != 0)
392                 goto fail2;
393
394         if (tag == TLV_TAG_END) {
395                 rc = EINVAL;
396                 goto fail3;
397         }
398
399         last_segment_end = tlv_last_segment_end(cursor);
400
401         delta = TLV_DWORD_COUNT(size);
402         if (last_segment_end + 1 + delta > cursor->limit) {
403                 rc = ENOSPC;
404                 goto fail4;
405         }
406
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));
410
411         /* Adjust the end pointer */
412         cursor->end += delta;
413
414         /* Write new TLV item */
415         tlv_write(cursor, tag, data, size);
416
417         return (0);
418
419 fail4:
420         EFSYS_PROBE(fail4);
421 fail3:
422         EFSYS_PROBE(fail3);
423 fail2:
424         EFSYS_PROBE(fail2);
425 fail1:
426         EFSYS_PROBE1(fail1, efx_rc_t, rc);
427
428         return (rc);
429 }
430
431 static  __checkReturn           efx_rc_t
432 tlv_delete(
433         __inout tlv_cursor_t    *cursor)
434 {
435         unsigned int delta;
436         uint32_t *last_segment_end;
437         efx_rc_t rc;
438
439         if ((rc = tlv_validate_state(cursor)) != 0)
440                 goto fail1;
441
442         if (tlv_tag(cursor) == TLV_TAG_END) {
443                 rc = EINVAL;
444                 goto fail2;
445         }
446
447         delta = TLV_DWORD_COUNT(tlv_length(cursor));
448
449         if ((rc = tlv_require_end(cursor)) != 0)
450                 goto fail3;
451
452         last_segment_end = tlv_last_segment_end(cursor);
453
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;
461
462         return (0);
463
464 fail3:
465         EFSYS_PROBE(fail3);
466 fail2:
467         EFSYS_PROBE(fail2);
468 fail1:
469         EFSYS_PROBE1(fail1, efx_rc_t, rc);
470
471         return (rc);
472 }
473
474 static  __checkReturn           efx_rc_t
475 tlv_modify(
476         __inout tlv_cursor_t    *cursor,
477         __in    uint32_t        tag,
478         __in_bcount(size)
479                 uint8_t         *data,
480         __in    size_t          size)
481 {
482         uint32_t *pos;
483         unsigned int old_ndwords;
484         unsigned int new_ndwords;
485         unsigned int delta;
486         uint32_t *last_segment_end;
487         efx_rc_t rc;
488
489         if ((rc = tlv_validate_state(cursor)) != 0)
490                 goto fail1;
491
492         if (tlv_tag(cursor) == TLV_TAG_END) {
493                 rc = EINVAL;
494                 goto fail2;
495         }
496         if (tlv_tag(cursor) != tag) {
497                 rc = EINVAL;
498                 goto fail3;
499         }
500
501         old_ndwords = TLV_DWORD_COUNT(tlv_length(cursor));
502         new_ndwords = TLV_DWORD_COUNT(size);
503
504         if ((rc = tlv_require_end(cursor)) != 0)
505                 goto fail4;
506
507         last_segment_end = tlv_last_segment_end(cursor);
508
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;
513
514                 if (last_segment_end + 1 + delta > cursor->limit) {
515                         rc = ENOSPC;
516                         goto fail5;
517                 }
518
519                 /* Move up: new space at (cursor->current + old_ndwords) */
520                 memmove(pos + delta, pos,
521                     (last_segment_end + 1 - pos) * sizeof (uint32_t));
522
523                 /* Adjust the end pointer */
524                 cursor->end += delta;
525
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;
530
531                 /* Move down: remove words at (cursor->current + new_ndwords) */
532                 memmove(pos, pos + delta,
533                     (last_segment_end + 1 - pos) * sizeof (uint32_t));
534
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));
538
539                 /* Adjust the end pointer */
540                 cursor->end -= delta;
541         }
542
543         /* Write new data */
544         tlv_write(cursor, tag, data, size);
545
546         return (0);
547
548 fail5:
549         EFSYS_PROBE(fail5);
550 fail4:
551         EFSYS_PROBE(fail4);
552 fail3:
553         EFSYS_PROBE(fail3);
554 fail2:
555         EFSYS_PROBE(fail2);
556 fail1:
557         EFSYS_PROBE1(fail1, efx_rc_t, rc);
558
559         return (rc);
560 }
561
562 static uint32_t checksum_tlv_partition(
563         __in    nvram_partition_t *partition)
564 {
565         tlv_cursor_t *cursor;
566         uint32_t *ptr;
567         uint32_t *end;
568         uint32_t csum;
569         size_t len;
570
571         cursor = &partition->tlv_cursor;
572         len = tlv_block_length_used(cursor);
573         EFSYS_ASSERT3U((len & 3), ==, 0);
574
575         csum = 0;
576         ptr = partition->data;
577         end = &ptr[len >> 2];
578
579         while (ptr < end)
580                 csum += __LE_TO_CPU_32(*ptr++);
581
582         return (csum);
583 }
584
585 static  __checkReturn           efx_rc_t
586 tlv_update_partition_len_and_cks(
587         __in    tlv_cursor_t *cursor)
588 {
589         efx_rc_t rc;
590         nvram_partition_t partition;
591         struct tlv_partition_header *header;
592         struct tlv_partition_trailer *trailer;
593         size_t new_len;
594
595         /*
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.
599          */
600         partition.data = cursor->block;
601         memcpy(&partition.tlv_cursor, cursor, sizeof (*cursor));
602         header = (struct tlv_partition_header *)partition.data;
603         /* Sanity check. */
604         if (__LE_TO_CPU_32(header->tag) != TLV_TAG_PARTITION_HEADER) {
605                 rc = EFAULT;
606                 goto fail1;
607         }
608         new_len =  tlv_block_length_used(&partition.tlv_cursor);
609         if (new_len == 0) {
610                 rc = EFAULT;
611                 goto fail2;
612         }
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);
617
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));
624
625         return (0);
626
627 fail2:
628         EFSYS_PROBE(fail2);
629 fail1:
630         EFSYS_PROBE1(fail1, efx_rc_t, rc);
631
632         return (rc);
633 }
634
635 /* Validate buffer contents (before writing to flash) */
636         __checkReturn           efx_rc_t
637 ef10_nvram_buffer_validate(
638         __in                    efx_nic_t *enp,
639         __in                    uint32_t partn,
640         __in_bcount(partn_size) caddr_t partn_data,
641         __in                    size_t partn_size)
642 {
643         tlv_cursor_t cursor;
644         struct tlv_partition_header *header;
645         struct tlv_partition_trailer *trailer;
646         size_t total_length;
647         uint32_t cksum;
648         int pos;
649         efx_rc_t rc;
650
651         _NOTE(ARGUNUSED(enp, partn))
652         EFX_STATIC_ASSERT(sizeof (*header) <= EF10_NVRAM_CHUNK);
653
654         if ((partn_data == NULL) || (partn_size == 0)) {
655                 rc = EINVAL;
656                 goto fail1;
657         }
658
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,
661                     partn_size)) != 0) {
662                 rc = EFAULT;
663                 goto fail2;
664         }
665         if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
666                 rc = EINVAL;
667                 goto fail3;
668         }
669         header = (struct tlv_partition_header *)tlv_item(&cursor);
670
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) {
674                 rc = EFBIG;
675                 goto fail4;
676         }
677
678         /* Check partition ends with PARTITION_TRAILER and END tags */
679         if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
680                 rc = EINVAL;
681                 goto fail5;
682         }
683         trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
684
685         if ((rc = tlv_advance(&cursor)) != 0) {
686                 rc = EINVAL;
687                 goto fail6;
688         }
689         if (tlv_tag(&cursor) != TLV_TAG_END) {
690                 rc = EINVAL;
691                 goto fail7;
692         }
693
694         /* Check generation counts are consistent */
695         if (trailer->generation != header->generation) {
696                 rc = EINVAL;
697                 goto fail8;
698         }
699
700         /* Verify partition checksum */
701         cksum = 0;
702         for (pos = 0; (size_t)pos < total_length; pos += sizeof (uint32_t)) {
703                 cksum += *((uint32_t *)(partn_data + pos));
704         }
705         if (cksum != 0) {
706                 rc = EINVAL;
707                 goto fail9;
708         }
709
710         return (0);
711
712 fail9:
713         EFSYS_PROBE(fail9);
714 fail8:
715         EFSYS_PROBE(fail8);
716 fail7:
717         EFSYS_PROBE(fail7);
718 fail6:
719         EFSYS_PROBE(fail6);
720 fail5:
721         EFSYS_PROBE(fail5);
722 fail4:
723         EFSYS_PROBE(fail4);
724 fail3:
725         EFSYS_PROBE(fail3);
726 fail2:
727         EFSYS_PROBE(fail2);
728 fail1:
729         EFSYS_PROBE1(fail1, efx_rc_t, rc);
730
731         return (rc);
732 }
733
734
735
736         __checkReturn           efx_rc_t
737 ef10_nvram_buffer_create(
738         __in                    efx_nic_t *enp,
739         __in                    uint16_t partn_type,
740         __in_bcount(partn_size) caddr_t partn_data,
741         __in                    size_t partn_size)
742 {
743         uint32_t *buf = (uint32_t *)partn_data;
744         efx_rc_t rc;
745         tlv_cursor_t cursor;
746         struct tlv_partition_header header;
747         struct tlv_partition_trailer trailer;
748
749         unsigned int min_buf_size = sizeof (struct tlv_partition_header) +
750             sizeof (struct tlv_partition_trailer);
751         if (partn_size < min_buf_size) {
752                 rc = EINVAL;
753                 goto fail1;
754         }
755
756         memset(buf, 0xff, partn_size);
757
758         tlv_init_block(buf);
759         if ((rc = tlv_init_cursor(&cursor, buf,
760             (uint32_t *)((uint8_t *)buf + partn_size),
761             buf)) != 0) {
762                 goto fail2;
763         }
764
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);
768         header.preset = 0;
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)
774                 goto fail3;
775         if ((rc = tlv_advance(&cursor)) != 0)
776                 goto fail4;
777
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)
784                 goto fail5;
785
786         if ((rc = tlv_update_partition_len_and_cks(&cursor)) != 0)
787                 goto fail6;
788
789         /* Check that the partition is valid. */
790         if ((rc = ef10_nvram_buffer_validate(enp, partn_type,
791             partn_data, partn_size)) != 0)
792                 goto fail7;
793
794         return (0);
795
796 fail7:
797         EFSYS_PROBE(fail7);
798 fail6:
799         EFSYS_PROBE(fail6);
800 fail5:
801         EFSYS_PROBE(fail5);
802 fail4:
803         EFSYS_PROBE(fail4);
804 fail3:
805         EFSYS_PROBE(fail3);
806 fail2:
807         EFSYS_PROBE(fail2);
808 fail1:
809         EFSYS_PROBE1(fail1, efx_rc_t, rc);
810
811         return (rc);
812 }
813
814 static                  uint32_t
815 byte_offset(
816         __in            uint32_t *position,
817         __in            uint32_t *base)
818 {
819         return (uint32_t)((uint8_t *)position - (uint8_t *)base);
820 }
821
822         __checkReturn           efx_rc_t
823 ef10_nvram_buffer_find_item_start(
824         __in_bcount(buffer_size)
825                                 caddr_t bufferp,
826         __in                    size_t buffer_size,
827         __out                   uint32_t *startp)
828 {
829         /* Read past partition header to find start address of the first key */
830         tlv_cursor_t cursor;
831         efx_rc_t rc;
832
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) {
836                 rc = EFAULT;
837                 goto fail1;
838         }
839         if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
840                 rc = EINVAL;
841                 goto fail2;
842         }
843
844         if ((rc = tlv_advance(&cursor)) != 0) {
845                 rc = EINVAL;
846                 goto fail3;
847         }
848         *startp = byte_offset(cursor.current, cursor.block);
849
850         if ((rc = tlv_require_end(&cursor)) != 0)
851                 goto fail4;
852
853         return (0);
854
855 fail4:
856         EFSYS_PROBE(fail4);
857 fail3:
858         EFSYS_PROBE(fail3);
859 fail2:
860         EFSYS_PROBE(fail2);
861 fail1:
862         EFSYS_PROBE1(fail1, efx_rc_t, rc);
863
864         return (rc);
865 }
866
867         __checkReturn           efx_rc_t
868 ef10_nvram_buffer_find_end(
869         __in_bcount(buffer_size)
870                                 caddr_t bufferp,
871         __in                    size_t buffer_size,
872         __in                    uint32_t offset,
873         __out                   uint32_t *endp)
874 {
875         /* Read to end of partition */
876         tlv_cursor_t cursor;
877         efx_rc_t rc;
878         uint32_t *segment_used;
879
880         _NOTE(ARGUNUSED(offset))
881
882         if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
883                         buffer_size)) != 0) {
884                 rc = EFAULT;
885                 goto fail1;
886         }
887
888         segment_used = cursor.block;
889
890         /*
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.
894          */
895         while (tlv_tag(&cursor) == TLV_TAG_PARTITION_HEADER) {
896                 if (tlv_require_end(&cursor) != 0) {
897                         if (segment_used == cursor.block) {
898                                 /*
899                                  * First segment is corrupt, so there is
900                                  * no valid data in partition.
901                                  */
902                                 rc = EINVAL;
903                                 goto fail2;
904                         }
905                         break;
906                 }
907                 segment_used = cursor.end + 1;
908
909                 cursor.current = segment_used;
910         }
911         /* Return space used (including the END tag) */
912         *endp = (segment_used - cursor.block) * sizeof (uint32_t);
913
914         return (0);
915
916 fail2:
917         EFSYS_PROBE(fail2);
918 fail1:
919         EFSYS_PROBE1(fail1, efx_rc_t, rc);
920
921         return (rc);
922 }
923
924         __checkReturn   __success(return != B_FALSE)    boolean_t
925 ef10_nvram_buffer_find_item(
926         __in_bcount(buffer_size)
927                                 caddr_t bufferp,
928         __in                    size_t buffer_size,
929         __in                    uint32_t offset,
930         __out                   uint32_t *startp,
931         __out                   uint32_t *lengthp)
932 {
933         /* Find TLV at offset and return key start and length */
934         tlv_cursor_t cursor;
935         uint8_t *key;
936         uint32_t tag;
937
938         if (tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
939                         buffer_size, offset) != 0) {
940                 return (B_FALSE);
941         }
942
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) {
948                                 break;
949                         }
950                         continue;
951                 }
952                 *startp = byte_offset(cursor.current, cursor.block);
953                 *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
954                     cursor.current);
955                 return (B_TRUE);
956         }
957
958         return (B_FALSE);
959 }
960
961         __checkReturn           efx_rc_t
962 ef10_nvram_buffer_get_item(
963         __in_bcount(buffer_size)
964                                 caddr_t bufferp,
965         __in                    size_t buffer_size,
966         __in                    uint32_t offset,
967         __in                    uint32_t length,
968         __out_bcount_part(item_max_size, *lengthp)
969                                 caddr_t itemp,
970         __in                    size_t item_max_size,
971         __out                   uint32_t *lengthp)
972 {
973         efx_rc_t rc;
974         tlv_cursor_t cursor;
975         uint32_t item_length;
976
977         if (item_max_size < length) {
978                 rc = ENOSPC;
979                 goto fail1;
980         }
981
982         if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
983                         buffer_size, offset)) != 0) {
984                 goto fail2;
985         }
986
987         item_length = tlv_length(&cursor);
988         if (length < item_length) {
989                 rc = ENOSPC;
990                 goto fail3;
991         }
992         memcpy(itemp, tlv_value(&cursor), item_length);
993
994         *lengthp = item_length;
995
996         return (0);
997
998 fail3:
999         EFSYS_PROBE(fail3);
1000 fail2:
1001         EFSYS_PROBE(fail2);
1002 fail1:
1003         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1004
1005         return (rc);
1006 }
1007
1008         __checkReturn           efx_rc_t
1009 ef10_nvram_buffer_insert_item(
1010         __in_bcount(buffer_size)
1011                                 caddr_t bufferp,
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)
1017 {
1018         efx_rc_t rc;
1019         tlv_cursor_t cursor;
1020
1021         if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1022                         buffer_size, offset)) != 0) {
1023                 goto fail1;
1024         }
1025
1026         rc = tlv_insert(&cursor, TLV_TAG_LICENSE, (uint8_t *)keyp, length);
1027
1028         if (rc != 0) {
1029                 goto fail2;
1030         }
1031
1032         *lengthp = byte_offset(tlv_next_item_ptr(&cursor),
1033                     cursor.current);
1034
1035         return (0);
1036
1037 fail2:
1038         EFSYS_PROBE(fail2);
1039 fail1:
1040         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1041
1042         return (rc);
1043 }
1044
1045         __checkReturn           efx_rc_t
1046 ef10_nvram_buffer_delete_item(
1047         __in_bcount(buffer_size)
1048                                 caddr_t bufferp,
1049         __in                    size_t buffer_size,
1050         __in                    uint32_t offset,
1051         __in                    uint32_t length,
1052         __in                    uint32_t end)
1053 {
1054         efx_rc_t rc;
1055         tlv_cursor_t cursor;
1056
1057         _NOTE(ARGUNUSED(length, end))
1058
1059         if ((rc = tlv_init_cursor_at_offset(&cursor, (uint8_t *)bufferp,
1060                         buffer_size, offset)) != 0) {
1061                 goto fail1;
1062         }
1063
1064         if ((rc = tlv_delete(&cursor)) != 0)
1065                 goto fail2;
1066
1067         return (0);
1068
1069 fail2:
1070         EFSYS_PROBE(fail2);
1071 fail1:
1072         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1073
1074         return (rc);
1075 }
1076
1077         __checkReturn           efx_rc_t
1078 ef10_nvram_buffer_finish(
1079         __in_bcount(buffer_size)
1080                                 caddr_t bufferp,
1081         __in                    size_t buffer_size)
1082 {
1083         efx_rc_t rc;
1084         tlv_cursor_t cursor;
1085
1086         if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)bufferp,
1087                         buffer_size)) != 0) {
1088                 rc = EFAULT;
1089                 goto fail1;
1090         }
1091
1092         if ((rc = tlv_require_end(&cursor)) != 0)
1093                 goto fail2;
1094
1095         if ((rc = tlv_update_partition_len_and_cks(&cursor)) != 0)
1096                 goto fail3;
1097
1098         return (0);
1099
1100 fail3:
1101         EFSYS_PROBE(fail3);
1102 fail2:
1103         EFSYS_PROBE(fail2);
1104 fail1:
1105         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1106
1107         return (rc);
1108 }
1109
1110
1111
1112 /*
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.
1117  */
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)
1125 {
1126         tlv_cursor_t cursor;
1127         struct tlv_partition_header *header;
1128         struct tlv_partition_trailer *trailer;
1129         size_t total_length;
1130         uint32_t cksum;
1131         int pos;
1132         efx_rc_t rc;
1133
1134         EFX_STATIC_ASSERT(sizeof (*header) <= EF10_NVRAM_CHUNK);
1135
1136         if ((seg_data == NULL) || (max_seg_size == 0)) {
1137                 rc = EINVAL;
1138                 goto fail1;
1139         }
1140
1141         /* Read initial chunk of the segment, starting at offset */
1142         if ((rc = ef10_nvram_partn_read_mode(enp, partn, seg_offset, seg_data,
1143                     EF10_NVRAM_CHUNK,
1144                     MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT)) != 0) {
1145                 goto fail2;
1146         }
1147
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) {
1151                 rc = EFAULT;
1152                 goto fail3;
1153         }
1154         if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1155                 rc = EINVAL;
1156                 goto fail4;
1157         }
1158         header = (struct tlv_partition_header *)tlv_item(&cursor);
1159
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) {
1163                 rc = EFBIG;
1164                 goto fail5;
1165         }
1166
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)
1174                         goto fail6;
1175         }
1176
1177         /* Check segment ends with PARTITION_TRAILER and END tags */
1178         if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1179                 rc = EINVAL;
1180                 goto fail7;
1181         }
1182         trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
1183
1184         if ((rc = tlv_advance(&cursor)) != 0) {
1185                 rc = EINVAL;
1186                 goto fail8;
1187         }
1188         if (tlv_tag(&cursor) != TLV_TAG_END) {
1189                 rc = EINVAL;
1190                 goto fail9;
1191         }
1192
1193         /* Check data read from segment is consistent */
1194         if (trailer->generation != header->generation) {
1195                 /*
1196                  * The partition data may have been modified between successive
1197                  * MCDI NVRAM_READ requests by the MC or another PCI function.
1198                  *
1199                  * The caller must retry to obtain consistent partition data.
1200                  */
1201                 rc = EAGAIN;
1202                 goto fail10;
1203         }
1204
1205         /* Verify segment checksum */
1206         cksum = 0;
1207         for (pos = 0; (size_t)pos < total_length; pos += sizeof (uint32_t)) {
1208                 cksum += *((uint32_t *)(seg_data + pos));
1209         }
1210         if (cksum != 0) {
1211                 rc = EINVAL;
1212                 goto fail11;
1213         }
1214
1215         return (0);
1216
1217 fail11:
1218         EFSYS_PROBE(fail11);
1219 fail10:
1220         EFSYS_PROBE(fail10);
1221 fail9:
1222         EFSYS_PROBE(fail9);
1223 fail8:
1224         EFSYS_PROBE(fail8);
1225 fail7:
1226         EFSYS_PROBE(fail7);
1227 fail6:
1228         EFSYS_PROBE(fail6);
1229 fail5:
1230         EFSYS_PROBE(fail5);
1231 fail4:
1232         EFSYS_PROBE(fail4);
1233 fail3:
1234         EFSYS_PROBE(fail3);
1235 fail2:
1236         EFSYS_PROBE(fail2);
1237 fail1:
1238         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1239
1240         return (rc);
1241 }
1242
1243 /*
1244  * Read a single TLV item from a host memory
1245  * buffer containing a TLV formatted segment.
1246  */
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,
1252         __in                            uint32_t tag,
1253         __deref_out_bcount_opt(*sizep)  caddr_t *datap,
1254         __out                           size_t *sizep)
1255 {
1256         tlv_cursor_t cursor;
1257         caddr_t data;
1258         size_t length;
1259         caddr_t value;
1260         efx_rc_t rc;
1261
1262         _NOTE(ARGUNUSED(enp))
1263
1264         if ((seg_data == NULL) || (max_seg_size == 0)) {
1265                 rc = EINVAL;
1266                 goto fail1;
1267         }
1268
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) {
1272                 rc = EFAULT;
1273                 goto fail2;
1274         }
1275         if ((rc = tlv_find(&cursor, tag)) != 0) {
1276                 rc = ENOENT;
1277                 goto fail3;
1278         }
1279         value = (caddr_t)tlv_value(&cursor);
1280         length = tlv_length(&cursor);
1281
1282         if (length == 0)
1283                 data = NULL;
1284         else {
1285                 /* Copy out data from TLV item */
1286                 EFSYS_KMEM_ALLOC(enp->en_esip, length, data);
1287                 if (data == NULL) {
1288                         rc = ENOMEM;
1289                         goto fail4;
1290                 }
1291                 memcpy(data, value, length);
1292         }
1293
1294         *datap = data;
1295         *sizep = length;
1296
1297         return (0);
1298
1299 fail4:
1300         EFSYS_PROBE(fail4);
1301 fail3:
1302         EFSYS_PROBE(fail3);
1303 fail2:
1304         EFSYS_PROBE(fail2);
1305 fail1:
1306         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1307
1308         return (rc);
1309 }
1310
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,
1316         __in                                    uint32_t tag,
1317         __deref_out_bcount_opt(*seg_sizep)      caddr_t *seg_datap,
1318         __out                                   size_t *seg_sizep)
1319 {
1320         caddr_t seg_data = NULL;
1321         size_t partn_size = 0;
1322         size_t length;
1323         caddr_t data;
1324         int retry;
1325         efx_rc_t rc;
1326
1327         /* Allocate sufficient memory for the entire partition */
1328         if ((rc = ef10_nvram_partn_size(enp, partn, &partn_size)) != 0)
1329                 goto fail1;
1330
1331         if (partn_size == 0) {
1332                 rc = ENOENT;
1333                 goto fail2;
1334         }
1335
1336         EFSYS_KMEM_ALLOC(enp->en_esip, partn_size, seg_data);
1337         if (seg_data == NULL) {
1338                 rc = ENOMEM;
1339                 goto fail3;
1340         }
1341
1342         /*
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.
1349          */
1350         retry = 10;
1351         do {
1352                 rc = ef10_nvram_read_tlv_segment(enp, partn, 0,
1353                     seg_data, partn_size);
1354         } while ((rc == EAGAIN) && (--retry > 0));
1355
1356         if (rc != 0) {
1357                 /* Failed to obtain consistent segment data */
1358                 goto fail4;
1359         }
1360
1361         if ((rc = ef10_nvram_buf_read_tlv(enp, seg_data, partn_size,
1362                     tag, &data, &length)) != 0)
1363                 goto fail5;
1364
1365         EFSYS_KMEM_FREE(enp->en_esip, partn_size, seg_data);
1366
1367         *seg_datap = data;
1368         *seg_sizep = length;
1369
1370         return (0);
1371
1372 fail5:
1373         EFSYS_PROBE(fail5);
1374 fail4:
1375         EFSYS_PROBE(fail4);
1376
1377         EFSYS_KMEM_FREE(enp->en_esip, partn_size, seg_data);
1378 fail3:
1379         EFSYS_PROBE(fail3);
1380 fail2:
1381         EFSYS_PROBE(fail2);
1382 fail1:
1383         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1384
1385         return (rc);
1386 }
1387
1388 /* Compute the size of a segment. */
1389         static  __checkReturn   efx_rc_t
1390 ef10_nvram_buf_segment_size(
1391         __in                    caddr_t seg_data,
1392         __in                    size_t max_seg_size,
1393         __out                   size_t *seg_sizep)
1394 {
1395         efx_rc_t rc;
1396         tlv_cursor_t cursor;
1397         struct tlv_partition_header *header;
1398         uint32_t cksum;
1399         int pos;
1400         uint32_t *end_tag_position;
1401         uint32_t segment_length;
1402
1403         /* A PARTITION_HEADER tag must be the first item at the given offset */
1404         if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1405                     max_seg_size)) != 0) {
1406                 rc = EFAULT;
1407                 goto fail1;
1408         }
1409         if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1410                 rc = EINVAL;
1411                 goto fail2;
1412         }
1413         header = (struct tlv_partition_header *)tlv_item(&cursor);
1414
1415         /* Check TLV segment length (includes the END tag) */
1416         *seg_sizep = __LE_TO_CPU_32(header->total_length);
1417         if (*seg_sizep > max_seg_size) {
1418                 rc = EFBIG;
1419                 goto fail3;
1420         }
1421
1422         /* Check segment ends with PARTITION_TRAILER and END tags */
1423         if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1424                 rc = EINVAL;
1425                 goto fail4;
1426         }
1427
1428         if ((rc = tlv_advance(&cursor)) != 0) {
1429                 rc = EINVAL;
1430                 goto fail5;
1431         }
1432         if (tlv_tag(&cursor) != TLV_TAG_END) {
1433                 rc = EINVAL;
1434                 goto fail6;
1435         }
1436         end_tag_position = cursor.current;
1437
1438         /* Verify segment checksum */
1439         cksum = 0;
1440         for (pos = 0; (size_t)pos < *seg_sizep; pos += sizeof (uint32_t)) {
1441                 cksum += *((uint32_t *)(seg_data + pos));
1442         }
1443         if (cksum != 0) {
1444                 rc = EINVAL;
1445                 goto fail7;
1446         }
1447
1448         /*
1449          * Calculate total length from HEADER to END tags and compare to
1450          * max_seg_size and the total_length field in the HEADER tag.
1451          */
1452         segment_length = tlv_block_length_used(&cursor);
1453
1454         if (segment_length > max_seg_size) {
1455                 rc = EINVAL;
1456                 goto fail8;
1457         }
1458
1459         if (segment_length != *seg_sizep) {
1460                 rc = EINVAL;
1461                 goto fail9;
1462         }
1463
1464         /* Skip over the first HEADER tag. */
1465         rc = tlv_rewind(&cursor);
1466         rc = tlv_advance(&cursor);
1467
1468         while (rc == 0) {
1469                 if (tlv_tag(&cursor) == TLV_TAG_END) {
1470                         /* Check that the END tag is the one found earlier. */
1471                         if (cursor.current != end_tag_position)
1472                                 goto fail10;
1473                         break;
1474                 }
1475                 /* Check for duplicate HEADER tags before the END tag. */
1476                 if (tlv_tag(&cursor) == TLV_TAG_PARTITION_HEADER) {
1477                         rc = EINVAL;
1478                         goto fail11;
1479                 }
1480
1481                 rc = tlv_advance(&cursor);
1482         }
1483         if (rc != 0)
1484                 goto fail12;
1485
1486         return (0);
1487
1488 fail12:
1489         EFSYS_PROBE(fail12);
1490 fail11:
1491         EFSYS_PROBE(fail11);
1492 fail10:
1493         EFSYS_PROBE(fail10);
1494 fail9:
1495         EFSYS_PROBE(fail9);
1496 fail8:
1497         EFSYS_PROBE(fail8);
1498 fail7:
1499         EFSYS_PROBE(fail7);
1500 fail6:
1501         EFSYS_PROBE(fail6);
1502 fail5:
1503         EFSYS_PROBE(fail5);
1504 fail4:
1505         EFSYS_PROBE(fail4);
1506 fail3:
1507         EFSYS_PROBE(fail3);
1508 fail2:
1509         EFSYS_PROBE(fail2);
1510 fail1:
1511         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1512
1513         return (rc);
1514 }
1515
1516 /*
1517  * Add or update a single TLV item in a host memory buffer containing a TLV
1518  * formatted segment. Historically partitions consisted of only one segment.
1519  */
1520         __checkReturn                   efx_rc_t
1521 ef10_nvram_buf_write_tlv(
1522         __inout_bcount(max_seg_size)    caddr_t seg_data,
1523         __in                            size_t max_seg_size,
1524         __in                            uint32_t tag,
1525         __in_bcount(tag_size)           caddr_t tag_data,
1526         __in                            size_t tag_size,
1527         __out                           size_t *total_lengthp)
1528 {
1529         tlv_cursor_t cursor;
1530         struct tlv_partition_header *header;
1531         struct tlv_partition_trailer *trailer;
1532         uint32_t generation;
1533         uint32_t cksum;
1534         int pos;
1535         efx_rc_t rc;
1536
1537         /* A PARTITION_HEADER tag must be the first item (at offset zero) */
1538         if ((rc = tlv_init_cursor_from_size(&cursor, (uint8_t *)seg_data,
1539                         max_seg_size)) != 0) {
1540                 rc = EFAULT;
1541                 goto fail1;
1542         }
1543         if (tlv_tag(&cursor) != TLV_TAG_PARTITION_HEADER) {
1544                 rc = EINVAL;
1545                 goto fail2;
1546         }
1547         header = (struct tlv_partition_header *)tlv_item(&cursor);
1548
1549         /* Update the TLV chain to contain the new data */
1550         if ((rc = tlv_find(&cursor, tag)) == 0) {
1551                 /* Modify existing TLV item */
1552                 if ((rc = tlv_modify(&cursor, tag,
1553                             (uint8_t *)tag_data, tag_size)) != 0)
1554                         goto fail3;
1555         } else {
1556                 /* Insert a new TLV item before the PARTITION_TRAILER */
1557                 rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER);
1558                 if (rc != 0) {
1559                         rc = EINVAL;
1560                         goto fail4;
1561                 }
1562                 if ((rc = tlv_insert(&cursor, tag,
1563                             (uint8_t *)tag_data, tag_size)) != 0) {
1564                         rc = EINVAL;
1565                         goto fail5;
1566                 }
1567         }
1568
1569         /* Find the trailer tag */
1570         if ((rc = tlv_find(&cursor, TLV_TAG_PARTITION_TRAILER)) != 0) {
1571                 rc = EINVAL;
1572                 goto fail6;
1573         }
1574         trailer = (struct tlv_partition_trailer *)tlv_item(&cursor);
1575
1576         /* Update PARTITION_HEADER and PARTITION_TRAILER fields */
1577         *total_lengthp = tlv_block_length_used(&cursor);
1578         if (*total_lengthp > max_seg_size) {
1579                 rc = ENOSPC;
1580                 goto fail7;
1581         }
1582         generation = __LE_TO_CPU_32(header->generation) + 1;
1583
1584         header->total_length    = __CPU_TO_LE_32(*total_lengthp);
1585         header->generation      = __CPU_TO_LE_32(generation);
1586         trailer->generation     = __CPU_TO_LE_32(generation);
1587
1588         /* Recompute PARTITION_TRAILER checksum */
1589         trailer->checksum = 0;
1590         cksum = 0;
1591         for (pos = 0; (size_t)pos < *total_lengthp; pos += sizeof (uint32_t)) {
1592                 cksum += *((uint32_t *)(seg_data + pos));
1593         }
1594         trailer->checksum = ~cksum + 1;
1595
1596         return (0);
1597
1598 fail7:
1599         EFSYS_PROBE(fail7);
1600 fail6:
1601         EFSYS_PROBE(fail6);
1602 fail5:
1603         EFSYS_PROBE(fail5);
1604 fail4:
1605         EFSYS_PROBE(fail4);
1606 fail3:
1607         EFSYS_PROBE(fail3);
1608 fail2:
1609         EFSYS_PROBE(fail2);
1610 fail1:
1611         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1612
1613         return (rc);
1614 }
1615
1616 /*
1617  * Add or update a single TLV item in the first segment of a TLV formatted
1618  * dynamic config partition. The first segment is the current active
1619  * configuration.
1620  */
1621         __checkReturn           efx_rc_t
1622 ef10_nvram_partn_write_tlv(
1623         __in                    efx_nic_t *enp,
1624         __in                    uint32_t partn,
1625         __in                    uint32_t tag,
1626         __in_bcount(size)       caddr_t data,
1627         __in                    size_t size)
1628 {
1629         return ef10_nvram_partn_write_segment_tlv(enp, partn, tag, data,
1630             size, B_FALSE);
1631 }
1632
1633 /*
1634  * Read a segment from nvram at the given offset into a buffer (segment_data)
1635  * and optionally write a new tag to it.
1636  */
1637 static  __checkReturn           efx_rc_t
1638 ef10_nvram_segment_write_tlv(
1639         __in                    efx_nic_t *enp,
1640         __in                    uint32_t partn,
1641         __in                    uint32_t tag,
1642         __in_bcount(size)       caddr_t data,
1643         __in                    size_t size,
1644         __inout                 caddr_t *seg_datap,
1645         __inout                 size_t *partn_offsetp,
1646         __inout                 size_t *src_remain_lenp,
1647         __inout                 size_t *dest_remain_lenp,
1648         __in                    boolean_t write)
1649 {
1650         efx_rc_t rc;
1651         efx_rc_t status;
1652         size_t original_segment_size;
1653         size_t modified_segment_size;
1654
1655         /*
1656          * Read the segment from NVRAM into the segment_data buffer and validate
1657          * it, returning if it does not validate. This is not a failure unless
1658          * this is the first segment in a partition. In this case the caller
1659          * must propagate the error.
1660          */
1661         status = ef10_nvram_read_tlv_segment(enp, partn, *partn_offsetp,
1662             *seg_datap, *src_remain_lenp);
1663         if (status != 0) {
1664                 rc = EINVAL;
1665                 goto fail1;
1666         }
1667
1668         status = ef10_nvram_buf_segment_size(*seg_datap,
1669             *src_remain_lenp, &original_segment_size);
1670         if (status != 0) {
1671                 rc = EINVAL;
1672                 goto fail2;
1673         }
1674
1675         if (write) {
1676                 /* Update the contents of the segment in the buffer */
1677                 if ((rc = ef10_nvram_buf_write_tlv(*seg_datap,
1678                         *dest_remain_lenp, tag, data, size,
1679                         &modified_segment_size)) != 0) {
1680                         goto fail3;
1681                 }
1682                 *dest_remain_lenp -= modified_segment_size;
1683                 *seg_datap += modified_segment_size;
1684         } else {
1685                 /*
1686                  * We won't modify this segment, but still need to update the
1687                  * remaining lengths and pointers.
1688                  */
1689                 *dest_remain_lenp -= original_segment_size;
1690                 *seg_datap += original_segment_size;
1691         }
1692
1693         *partn_offsetp += original_segment_size;
1694         *src_remain_lenp -= original_segment_size;
1695
1696         return (0);
1697
1698 fail3:
1699         EFSYS_PROBE(fail3);
1700 fail2:
1701         EFSYS_PROBE(fail2);
1702 fail1:
1703         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1704
1705         return (rc);
1706 }
1707
1708 /*
1709  * Add or update a single TLV item in either the first segment or in all
1710  * segments in a TLV formatted dynamic config partition. Dynamic config
1711  * partitions on boards that support RFID are divided into a number of segments,
1712  * each formatted like a partition, with header, trailer and end tags. The first
1713  * segment is the current active configuration.
1714  *
1715  * The segments are initialised by manftest and each contain a different
1716  * configuration e.g. firmware variant. The firmware can be instructed
1717  * via RFID to copy a segment to replace the first segment, hence changing the
1718  * active configuration.  This allows ops to change the configuration of a board
1719  * prior to shipment using RFID.
1720  *
1721  * Changes to the dynamic config may need to be written to all segments (e.g.
1722  * firmware versions) or just the first segment (changes to the active
1723  * configuration). See SF-111324-SW "The use of RFID in Solarflare Products".
1724  * If only the first segment is written the code still needs to be aware of the
1725  * possible presence of subsequent segments as writing to a segment may cause
1726  * its size to increase, which would overwrite the subsequent segments and
1727  * invalidate them.
1728  */
1729         __checkReturn           efx_rc_t
1730 ef10_nvram_partn_write_segment_tlv(
1731         __in                    efx_nic_t *enp,
1732         __in                    uint32_t partn,
1733         __in                    uint32_t tag,
1734         __in_bcount(size)       caddr_t data,
1735         __in                    size_t size,
1736         __in                    boolean_t all_segments)
1737 {
1738         size_t partn_size = 0;
1739         caddr_t partn_data;
1740         size_t total_length = 0;
1741         efx_rc_t rc;
1742         size_t current_offset = 0;
1743         size_t remaining_original_length;
1744         size_t remaining_modified_length;
1745         caddr_t segment_data;
1746
1747         EFSYS_ASSERT3U(partn, ==, NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG);
1748
1749         /* Allocate sufficient memory for the entire partition */
1750         if ((rc = ef10_nvram_partn_size(enp, partn, &partn_size)) != 0)
1751                 goto fail1;
1752
1753         EFSYS_KMEM_ALLOC(enp->en_esip, partn_size, partn_data);
1754         if (partn_data == NULL) {
1755                 rc = ENOMEM;
1756                 goto fail2;
1757         }
1758
1759         remaining_original_length = partn_size;
1760         remaining_modified_length = partn_size;
1761         segment_data = partn_data;
1762
1763         /* Lock the partition */
1764         if ((rc = ef10_nvram_partn_lock(enp, partn)) != 0)
1765                 goto fail3;
1766
1767         /* Iterate over each (potential) segment to update it. */
1768         do {
1769                 boolean_t write = all_segments || current_offset == 0;
1770
1771                 rc = ef10_nvram_segment_write_tlv(enp, partn, tag, data, size,
1772                     &segment_data, &current_offset, &remaining_original_length,
1773                     &remaining_modified_length, write);
1774                 if (rc != 0) {
1775                         if (current_offset == 0) {
1776                                 /*
1777                                  * If no data has been read then the first
1778                                  * segment is invalid, which is an error.
1779                                  */
1780                                 goto fail4;
1781                         }
1782                         break;
1783                 }
1784         } while (current_offset < partn_size);
1785
1786         total_length = segment_data - partn_data;
1787
1788         /*
1789          * We've run out of space.  This should actually be dealt with by
1790          * ef10_nvram_buf_write_tlv returning ENOSPC.
1791          */
1792         if (total_length > partn_size) {
1793                 rc = ENOSPC;
1794                 goto fail5;
1795         }
1796
1797         /* Erase the whole partition in NVRAM */
1798         if ((rc = ef10_nvram_partn_erase(enp, partn, 0, partn_size)) != 0)
1799                 goto fail6;
1800
1801         /* Write new partition contents from the buffer to NVRAM */
1802         if ((rc = ef10_nvram_partn_write(enp, partn, 0, partn_data,
1803                     total_length)) != 0)
1804                 goto fail7;
1805
1806         /* Unlock the partition */
1807         ef10_nvram_partn_unlock(enp, partn, NULL);
1808
1809         EFSYS_KMEM_FREE(enp->en_esip, partn_size, partn_data);
1810
1811         return (0);
1812
1813 fail7:
1814         EFSYS_PROBE(fail7);
1815 fail6:
1816         EFSYS_PROBE(fail6);
1817 fail5:
1818         EFSYS_PROBE(fail5);
1819 fail4:
1820         EFSYS_PROBE(fail4);
1821
1822         ef10_nvram_partn_unlock(enp, partn, NULL);
1823 fail3:
1824         EFSYS_PROBE(fail3);
1825
1826         EFSYS_KMEM_FREE(enp->en_esip, partn_size, partn_data);
1827 fail2:
1828         EFSYS_PROBE(fail2);
1829 fail1:
1830         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1831
1832         return (rc);
1833 }
1834
1835 /*
1836  * Get the size of a NVRAM partition. This is the total size allocated in nvram,
1837  * not the data used by the segments in the partition.
1838  */
1839         __checkReturn           efx_rc_t
1840 ef10_nvram_partn_size(
1841         __in                    efx_nic_t *enp,
1842         __in                    uint32_t partn,
1843         __out                   size_t *sizep)
1844 {
1845         efx_rc_t rc;
1846
1847         if ((rc = efx_mcdi_nvram_info(enp, partn, sizep,
1848             NULL, NULL, NULL)) != 0)
1849                 goto fail1;
1850
1851         return (0);
1852
1853 fail1:
1854         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1855
1856         return (rc);
1857 }
1858
1859         __checkReturn           efx_rc_t
1860 ef10_nvram_partn_lock(
1861         __in                    efx_nic_t *enp,
1862         __in                    uint32_t partn)
1863 {
1864         efx_rc_t rc;
1865
1866         if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0)
1867                 goto fail1;
1868
1869         return (0);
1870
1871 fail1:
1872         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1873
1874         return (rc);
1875 }
1876
1877         __checkReturn           efx_rc_t
1878 ef10_nvram_partn_read_mode(
1879         __in                    efx_nic_t *enp,
1880         __in                    uint32_t partn,
1881         __in                    unsigned int offset,
1882         __out_bcount(size)      caddr_t data,
1883         __in                    size_t size,
1884         __in                    uint32_t mode)
1885 {
1886         size_t chunk;
1887         efx_rc_t rc;
1888
1889         while (size > 0) {
1890                 chunk = MIN(size, EF10_NVRAM_CHUNK);
1891
1892                 if ((rc = efx_mcdi_nvram_read(enp, partn, offset,
1893                             data, chunk, mode)) != 0) {
1894                         goto fail1;
1895                 }
1896
1897                 size -= chunk;
1898                 data += chunk;
1899                 offset += chunk;
1900         }
1901
1902         return (0);
1903
1904 fail1:
1905         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1906
1907         return (rc);
1908 }
1909
1910         __checkReturn           efx_rc_t
1911 ef10_nvram_partn_read(
1912         __in                    efx_nic_t *enp,
1913         __in                    uint32_t partn,
1914         __in                    unsigned int offset,
1915         __out_bcount(size)      caddr_t data,
1916         __in                    size_t size)
1917 {
1918         /*
1919          * An A/B partition has two data stores (current and backup).
1920          * Read requests which come in through the EFX API expect to read the
1921          * current, active store of an A/B partition. For non A/B partitions,
1922          * there is only a single store and so the mode param is ignored.
1923          */
1924         return ef10_nvram_partn_read_mode(enp, partn, offset, data, size,
1925                             MC_CMD_NVRAM_READ_IN_V2_TARGET_CURRENT);
1926 }
1927
1928         __checkReturn           efx_rc_t
1929 ef10_nvram_partn_read_backup(
1930         __in                    efx_nic_t *enp,
1931         __in                    uint32_t partn,
1932         __in                    unsigned int offset,
1933         __out_bcount(size)      caddr_t data,
1934         __in                    size_t size)
1935 {
1936         /*
1937          * An A/B partition has two data stores (current and backup).
1938          * Read the backup store of an A/B partition (i.e. the store currently
1939          * being written to if the partition is locked).
1940          *
1941          * This is needed when comparing the existing partition content to avoid
1942          * unnecessary writes, or to read back what has been written to check
1943          * that the writes have succeeded.
1944          */
1945         return ef10_nvram_partn_read_mode(enp, partn, offset, data, size,
1946                             MC_CMD_NVRAM_READ_IN_V2_TARGET_BACKUP);
1947 }
1948
1949         __checkReturn           efx_rc_t
1950 ef10_nvram_partn_erase(
1951         __in                    efx_nic_t *enp,
1952         __in                    uint32_t partn,
1953         __in                    unsigned int offset,
1954         __in                    size_t size)
1955 {
1956         efx_rc_t rc;
1957         uint32_t erase_size;
1958
1959         if ((rc = efx_mcdi_nvram_info(enp, partn, NULL, NULL,
1960             &erase_size, NULL)) != 0)
1961                 goto fail1;
1962
1963         if (erase_size == 0) {
1964                 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset, size)) != 0)
1965                         goto fail2;
1966         } else {
1967                 if (size % erase_size != 0) {
1968                         rc = EINVAL;
1969                         goto fail3;
1970                 }
1971                 while (size > 0) {
1972                         if ((rc = efx_mcdi_nvram_erase(enp, partn, offset,
1973                             erase_size)) != 0)
1974                                 goto fail4;
1975                         offset += erase_size;
1976                         size -= erase_size;
1977                 }
1978         }
1979
1980         return (0);
1981
1982 fail4:
1983         EFSYS_PROBE(fail4);
1984 fail3:
1985         EFSYS_PROBE(fail3);
1986 fail2:
1987         EFSYS_PROBE(fail2);
1988 fail1:
1989         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1990
1991         return (rc);
1992 }
1993
1994         __checkReturn           efx_rc_t
1995 ef10_nvram_partn_write(
1996         __in                    efx_nic_t *enp,
1997         __in                    uint32_t partn,
1998         __in                    unsigned int offset,
1999         __out_bcount(size)      caddr_t data,
2000         __in                    size_t size)
2001 {
2002         size_t chunk;
2003         uint32_t write_size;
2004         efx_rc_t rc;
2005
2006         if ((rc = efx_mcdi_nvram_info(enp, partn, NULL, NULL,
2007             NULL, &write_size)) != 0)
2008                 goto fail1;
2009
2010         if (write_size != 0) {
2011                 /*
2012                  * Check that the size is a multiple of the write chunk size if
2013                  * the write chunk size is available.
2014                  */
2015                 if (size % write_size != 0) {
2016                         rc = EINVAL;
2017                         goto fail2;
2018                 }
2019         } else {
2020                 write_size = EF10_NVRAM_CHUNK;
2021         }
2022
2023         while (size > 0) {
2024                 chunk = MIN(size, write_size);
2025
2026                 if ((rc = efx_mcdi_nvram_write(enp, partn, offset,
2027                             data, chunk)) != 0) {
2028                         goto fail3;
2029                 }
2030
2031                 size -= chunk;
2032                 data += chunk;
2033                 offset += chunk;
2034         }
2035
2036         return (0);
2037
2038 fail3:
2039         EFSYS_PROBE(fail3);
2040 fail2:
2041         EFSYS_PROBE(fail2);
2042 fail1:
2043         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2044
2045         return (rc);
2046 }
2047
2048         __checkReturn           efx_rc_t
2049 ef10_nvram_partn_unlock(
2050         __in                    efx_nic_t *enp,
2051         __in                    uint32_t partn,
2052         __out_opt               uint32_t *verify_resultp)
2053 {
2054         boolean_t reboot = B_FALSE;
2055         efx_rc_t rc;
2056
2057         if (verify_resultp != NULL)
2058                 *verify_resultp = MC_CMD_NVRAM_VERIFY_RC_UNKNOWN;
2059
2060         rc = efx_mcdi_nvram_update_finish(enp, partn, reboot, verify_resultp);
2061         if (rc != 0)
2062                 goto fail1;
2063
2064         return (0);
2065
2066 fail1:
2067         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2068
2069         return (rc);
2070 }
2071
2072         __checkReturn           efx_rc_t
2073 ef10_nvram_partn_set_version(
2074         __in                    efx_nic_t *enp,
2075         __in                    uint32_t partn,
2076         __in_ecount(4)          uint16_t version[4])
2077 {
2078         struct tlv_partition_version partn_version;
2079         size_t size;
2080         efx_rc_t rc;
2081
2082         /* Add or modify partition version TLV item */
2083         partn_version.version_w = __CPU_TO_LE_16(version[0]);
2084         partn_version.version_x = __CPU_TO_LE_16(version[1]);
2085         partn_version.version_y = __CPU_TO_LE_16(version[2]);
2086         partn_version.version_z = __CPU_TO_LE_16(version[3]);
2087
2088         size = sizeof (partn_version) - (2 * sizeof (uint32_t));
2089
2090         /* Write the version number to all segments in the partition */
2091         if ((rc = ef10_nvram_partn_write_segment_tlv(enp,
2092                     NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG,
2093                     TLV_TAG_PARTITION_VERSION(partn),
2094                     (caddr_t)&partn_version.version_w, size, B_TRUE)) != 0)
2095                 goto fail1;
2096
2097         return (0);
2098
2099 fail1:
2100         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2101
2102         return (rc);
2103 }
2104
2105 #endif /* EFSYS_OPT_VPD || EFSYS_OPT_NVRAM */
2106
2107 #if EFSYS_OPT_NVRAM
2108
2109 typedef struct ef10_parttbl_entry_s {
2110         unsigned int            partn;
2111         unsigned int            port_mask;
2112         efx_nvram_type_t        nvtype;
2113 } ef10_parttbl_entry_t;
2114
2115 /* Port mask values */
2116 #define PORT_1          (1u << 1)
2117 #define PORT_2          (1u << 2)
2118 #define PORT_3          (1u << 3)
2119 #define PORT_4          (1u << 4)
2120 #define PORT_ALL        (0xffffffffu)
2121
2122 #define PARTN_MAP_ENTRY(partn, port_mask, nvtype)       \
2123 { (NVRAM_PARTITION_TYPE_##partn), (PORT_##port_mask), (EFX_NVRAM_##nvtype) }
2124
2125 /* Translate EFX NVRAM types to firmware partition types */
2126 static ef10_parttbl_entry_t hunt_parttbl[] = {
2127         /*              partn                   ports   nvtype */
2128         PARTN_MAP_ENTRY(MC_FIRMWARE,            ALL,    MC_FIRMWARE),
2129         PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP,     ALL,    MC_GOLDEN),
2130         PARTN_MAP_ENTRY(EXPANSION_ROM,          ALL,    BOOTROM),
2131         PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT0,    1,      BOOTROM_CFG),
2132         PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT1,    2,      BOOTROM_CFG),
2133         PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT2,    3,      BOOTROM_CFG),
2134         PARTN_MAP_ENTRY(EXPROM_CONFIG_PORT3,    4,      BOOTROM_CFG),
2135         PARTN_MAP_ENTRY(DYNAMIC_CONFIG,         ALL,    DYNAMIC_CFG),
2136         PARTN_MAP_ENTRY(FPGA,                   ALL,    FPGA),
2137         PARTN_MAP_ENTRY(FPGA_BACKUP,            ALL,    FPGA_BACKUP),
2138         PARTN_MAP_ENTRY(LICENSE,                ALL,    LICENSE),
2139 };
2140
2141 static ef10_parttbl_entry_t medford_parttbl[] = {
2142         /*              partn                   ports   nvtype */
2143         PARTN_MAP_ENTRY(MC_FIRMWARE,            ALL,    MC_FIRMWARE),
2144         PARTN_MAP_ENTRY(MC_FIRMWARE_BACKUP,     ALL,    MC_GOLDEN),
2145         PARTN_MAP_ENTRY(EXPANSION_ROM,          ALL,    BOOTROM),
2146         PARTN_MAP_ENTRY(EXPROM_CONFIG,          ALL,    BOOTROM_CFG),
2147         PARTN_MAP_ENTRY(DYNAMIC_CONFIG,         ALL,    DYNAMIC_CFG),
2148         PARTN_MAP_ENTRY(FPGA,                   ALL,    FPGA),
2149         PARTN_MAP_ENTRY(FPGA_BACKUP,            ALL,    FPGA_BACKUP),
2150         PARTN_MAP_ENTRY(LICENSE,                ALL,    LICENSE),
2151         PARTN_MAP_ENTRY(EXPANSION_UEFI,         ALL,    UEFIROM),
2152         PARTN_MAP_ENTRY(MUM_FIRMWARE,           ALL,    MUM_FIRMWARE),
2153 };
2154
2155 static  __checkReturn           efx_rc_t
2156 ef10_parttbl_get(
2157         __in                    efx_nic_t *enp,
2158         __out                   ef10_parttbl_entry_t **parttblp,
2159         __out                   size_t *parttbl_rowsp)
2160 {
2161         switch (enp->en_family) {
2162         case EFX_FAMILY_HUNTINGTON:
2163                 *parttblp = hunt_parttbl;
2164                 *parttbl_rowsp = EFX_ARRAY_SIZE(hunt_parttbl);
2165                 break;
2166
2167         case EFX_FAMILY_MEDFORD:
2168                 *parttblp = medford_parttbl;
2169                 *parttbl_rowsp = EFX_ARRAY_SIZE(medford_parttbl);
2170                 break;
2171
2172         default:
2173                 EFSYS_ASSERT(B_FALSE);
2174                 return (EINVAL);
2175         }
2176         return (0);
2177 }
2178
2179         __checkReturn           efx_rc_t
2180 ef10_nvram_type_to_partn(
2181         __in                    efx_nic_t *enp,
2182         __in                    efx_nvram_type_t type,
2183         __out                   uint32_t *partnp)
2184 {
2185         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
2186         ef10_parttbl_entry_t *parttbl = NULL;
2187         size_t parttbl_rows = 0;
2188         unsigned int i;
2189
2190         EFSYS_ASSERT3U(type, !=, EFX_NVRAM_INVALID);
2191         EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
2192         EFSYS_ASSERT(partnp != NULL);
2193
2194         if (ef10_parttbl_get(enp, &parttbl, &parttbl_rows) == 0) {
2195                 for (i = 0; i < parttbl_rows; i++) {
2196                         ef10_parttbl_entry_t *entry = &parttbl[i];
2197
2198                         if ((entry->nvtype == type) &&
2199                             (entry->port_mask & (1u << emip->emi_port))) {
2200                                 *partnp = entry->partn;
2201                                 return (0);
2202                         }
2203                 }
2204         }
2205
2206         return (ENOTSUP);
2207 }
2208
2209 #if EFSYS_OPT_DIAG
2210
2211 static  __checkReturn           efx_rc_t
2212 ef10_nvram_partn_to_type(
2213         __in                    efx_nic_t *enp,
2214         __in                    uint32_t partn,
2215         __out                   efx_nvram_type_t *typep)
2216 {
2217         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
2218         ef10_parttbl_entry_t *parttbl = NULL;
2219         size_t parttbl_rows = 0;
2220         unsigned int i;
2221
2222         EFSYS_ASSERT(typep != NULL);
2223
2224         if (ef10_parttbl_get(enp, &parttbl, &parttbl_rows) == 0) {
2225                 for (i = 0; i < parttbl_rows; i++) {
2226                         ef10_parttbl_entry_t *entry = &parttbl[i];
2227
2228                         if ((entry->partn == partn) &&
2229                             (entry->port_mask & (1u << emip->emi_port))) {
2230                                 *typep = entry->nvtype;
2231                                 return (0);
2232                         }
2233                 }
2234         }
2235
2236         return (ENOTSUP);
2237 }
2238
2239         __checkReturn           efx_rc_t
2240 ef10_nvram_test(
2241         __in                    efx_nic_t *enp)
2242 {
2243         efx_nvram_type_t type;
2244         unsigned int npartns = 0;
2245         uint32_t *partns = NULL;
2246         size_t size;
2247         unsigned int i;
2248         efx_rc_t rc;
2249
2250         /* Read available partitions from NVRAM partition map */
2251         size = MC_CMD_NVRAM_PARTITIONS_OUT_TYPE_ID_MAXNUM * sizeof (uint32_t);
2252         EFSYS_KMEM_ALLOC(enp->en_esip, size, partns);
2253         if (partns == NULL) {
2254                 rc = ENOMEM;
2255                 goto fail1;
2256         }
2257
2258         if ((rc = efx_mcdi_nvram_partitions(enp, (caddr_t)partns, size,
2259                     &npartns)) != 0) {
2260                 goto fail2;
2261         }
2262
2263         for (i = 0; i < npartns; i++) {
2264                 /* Check if the partition is supported for this port */
2265                 if ((rc = ef10_nvram_partn_to_type(enp, partns[i], &type)) != 0)
2266                         continue;
2267
2268                 if ((rc = efx_mcdi_nvram_test(enp, partns[i])) != 0)
2269                         goto fail3;
2270         }
2271
2272         EFSYS_KMEM_FREE(enp->en_esip, size, partns);
2273         return (0);
2274
2275 fail3:
2276         EFSYS_PROBE(fail3);
2277 fail2:
2278         EFSYS_PROBE(fail2);
2279         EFSYS_KMEM_FREE(enp->en_esip, size, partns);
2280 fail1:
2281         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2282         return (rc);
2283 }
2284
2285 #endif  /* EFSYS_OPT_DIAG */
2286
2287         __checkReturn           efx_rc_t
2288 ef10_nvram_partn_get_version(
2289         __in                    efx_nic_t *enp,
2290         __in                    uint32_t partn,
2291         __out                   uint32_t *subtypep,
2292         __out_ecount(4)         uint16_t version[4])
2293 {
2294         efx_rc_t rc;
2295
2296         /* FIXME: get highest partn version from all ports */
2297         /* FIXME: return partn description if available */
2298
2299         if ((rc = efx_mcdi_nvram_metadata(enp, partn, subtypep,
2300                     version, NULL, 0)) != 0)
2301                 goto fail1;
2302
2303         return (0);
2304
2305 fail1:
2306         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2307
2308         return (rc);
2309 }
2310
2311         __checkReturn           efx_rc_t
2312 ef10_nvram_partn_rw_start(
2313         __in                    efx_nic_t *enp,
2314         __in                    uint32_t partn,
2315         __out                   size_t *chunk_sizep)
2316 {
2317         uint32_t write_size = 0;
2318         efx_rc_t rc;
2319
2320         if ((rc = efx_mcdi_nvram_info(enp, partn, NULL, NULL,
2321             NULL, &write_size)) != 0)
2322                 goto fail1;
2323
2324         if ((rc = ef10_nvram_partn_lock(enp, partn)) != 0)
2325                 goto fail2;
2326
2327         if (chunk_sizep != NULL) {
2328                 if (write_size == 0)
2329                         *chunk_sizep = EF10_NVRAM_CHUNK;
2330                 else
2331                         *chunk_sizep = write_size;
2332         }
2333
2334         return (0);
2335
2336 fail2:
2337         EFSYS_PROBE(fail2);
2338 fail1:
2339         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2340
2341         return (rc);
2342 }
2343
2344         __checkReturn           efx_rc_t
2345 ef10_nvram_partn_rw_finish(
2346         __in                    efx_nic_t *enp,
2347         __in                    uint32_t partn,
2348         __out_opt               uint32_t *verify_resultp)
2349 {
2350         efx_rc_t rc;
2351
2352         if ((rc = ef10_nvram_partn_unlock(enp, partn, verify_resultp)) != 0)
2353                 goto fail1;
2354
2355         return (0);
2356
2357 fail1:
2358         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2359
2360         return (rc);
2361 }
2362
2363 #endif  /* EFSYS_OPT_NVRAM */
2364
2365 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */