ethdev: fix VLAN offloads set if no relative capabilities
[dpdk.git] / drivers / net / ice / base / ice_nvm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2020 Intel Corporation
3  */
4
5 #include "ice_common.h"
6
7 /**
8  * ice_aq_read_nvm
9  * @hw: pointer to the HW struct
10  * @module_typeid: module pointer location in words from the NVM beginning
11  * @offset: byte offset from the module beginning
12  * @length: length of the section to be read (in bytes from the offset)
13  * @data: command buffer (size [bytes] = length)
14  * @last_command: tells if this is the last command in a series
15  * @read_shadow_ram: tell if this is a shadow RAM read
16  * @cd: pointer to command details structure or NULL
17  *
18  * Read the NVM using the admin queue commands (0x0701)
19  */
20 enum ice_status
21 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
22                 void *data, bool last_command, bool read_shadow_ram,
23                 struct ice_sq_cd *cd)
24 {
25         struct ice_aq_desc desc;
26         struct ice_aqc_nvm *cmd;
27
28         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
29
30         cmd = &desc.params.nvm;
31
32         if (offset > ICE_AQC_NVM_MAX_OFFSET)
33                 return ICE_ERR_PARAM;
34
35         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
36
37         if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
38                 cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
39
40         /* If this is the last command in a series, set the proper flag. */
41         if (last_command)
42                 cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
43         cmd->module_typeid = CPU_TO_LE16(module_typeid);
44         cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
45         cmd->offset_high = (offset >> 16) & 0xFF;
46         cmd->length = CPU_TO_LE16(length);
47
48         return ice_aq_send_cmd(hw, &desc, data, length, cd);
49 }
50
51 /**
52  * ice_read_flat_nvm - Read portion of NVM by flat offset
53  * @hw: pointer to the HW struct
54  * @offset: offset from beginning of NVM
55  * @length: (in) number of bytes to read; (out) number of bytes actually read
56  * @data: buffer to return data in (sized to fit the specified length)
57  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
58  *
59  * Reads a portion of the NVM, as a flat memory space. This function correctly
60  * breaks read requests across Shadow RAM sectors and ensures that no single
61  * read request exceeds the maximum 4Kb read for a single AdminQ command.
62  *
63  * Returns a status code on failure. Note that the data pointer may be
64  * partially updated if some reads succeed before a failure.
65  */
66 enum ice_status
67 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
68                   bool read_shadow_ram)
69 {
70         enum ice_status status;
71         u32 inlen = *length;
72         u32 bytes_read = 0;
73         bool last_cmd;
74
75         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
76
77         *length = 0;
78
79         /* Verify the length of the read if this is for the Shadow RAM */
80         if (read_shadow_ram && ((offset + inlen) > (hw->nvm.sr_words * 2u))) {
81                 ice_debug(hw, ICE_DBG_NVM,
82                           "NVM error: requested data is beyond Shadow RAM limit\n");
83                 return ICE_ERR_PARAM;
84         }
85
86         do {
87                 u32 read_size, sector_offset;
88
89                 /* ice_aq_read_nvm cannot read more than 4Kb at a time.
90                  * Additionally, a read from the Shadow RAM may not cross over
91                  * a sector boundary. Conveniently, the sector size is also
92                  * 4Kb.
93                  */
94                 sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
95                 read_size = MIN_T(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
96                                   inlen - bytes_read);
97
98                 last_cmd = !(bytes_read + read_size < inlen);
99
100                 /* ice_aq_read_nvm takes the length as a u16. Our read_size is
101                  * calculated using a u32, but the ICE_AQ_MAX_BUF_LEN maximum
102                  * size guarantees that it will fit within the 2 bytes.
103                  */
104                 status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
105                                          offset, (u16)read_size,
106                                          data + bytes_read, last_cmd,
107                                          read_shadow_ram, NULL);
108                 if (status)
109                         break;
110
111                 bytes_read += read_size;
112                 offset += read_size;
113         } while (!last_cmd);
114
115         *length = bytes_read;
116         return status;
117 }
118
119 /**
120  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
121  * @hw: pointer to the HW structure
122  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
123  * @data: word read from the Shadow RAM
124  *
125  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
126  */
127 static enum ice_status
128 ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
129 {
130         u32 bytes = sizeof(u16);
131         enum ice_status status;
132         __le16 data_local;
133
134         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
135
136         /* Note that ice_read_flat_nvm checks if the read is past the Shadow
137          * RAM size, and ensures we don't read across a Shadow RAM sector
138          * boundary
139          */
140         status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
141                                    (u8 *)&data_local, true);
142         if (status)
143                 return status;
144
145         *data = LE16_TO_CPU(data_local);
146         return ICE_SUCCESS;
147 }
148
149 /**
150  * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
151  * @hw: pointer to the HW structure
152  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
153  * @words: (in) number of words to read; (out) number of words actually read
154  * @data: words read from the Shadow RAM
155  *
156  * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
157  * taken before reading the buffer and later released.
158  */
159 static enum ice_status
160 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
161 {
162         u32 bytes = *words * 2, i;
163         enum ice_status status;
164
165         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
166
167         /* ice_read_flat_nvm takes into account the 4Kb AdminQ and Shadow RAM
168          * sector restrictions necessary when reading from the NVM.
169          */
170         status = ice_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
171
172         /* Report the number of words successfully read */
173         *words = bytes / 2;
174
175         /* Byte swap the words up to the amount we actually read */
176         for (i = 0; i < *words; i++)
177                 data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
178
179         return status;
180 }
181
182 /**
183  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
184  * @hw: pointer to the HW structure
185  * @access: NVM access type (read or write)
186  *
187  * This function will request NVM ownership.
188  */
189 enum ice_status
190 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
191 {
192         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
193
194         if (hw->nvm.blank_nvm_mode)
195                 return ICE_SUCCESS;
196
197         return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
198 }
199
200 /**
201  * ice_release_nvm - Generic request for releasing the NVM ownership
202  * @hw: pointer to the HW structure
203  *
204  * This function will release NVM ownership.
205  */
206 void ice_release_nvm(struct ice_hw *hw)
207 {
208         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
209
210         if (hw->nvm.blank_nvm_mode)
211                 return;
212
213         ice_release_res(hw, ICE_NVM_RES_ID);
214 }
215
216 /**
217  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
218  * @hw: pointer to the HW structure
219  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
220  * @data: word read from the Shadow RAM
221  *
222  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
223  */
224 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
225 {
226         enum ice_status status;
227
228         status = ice_acquire_nvm(hw, ICE_RES_READ);
229         if (!status) {
230                 status = ice_read_sr_word_aq(hw, offset, data);
231                 ice_release_nvm(hw);
232         }
233
234         return status;
235 }
236
237 /**
238  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
239  * @hw: pointer to hardware structure
240  * @module_tlv: pointer to module TLV to return
241  * @module_tlv_len: pointer to module TLV length to return
242  * @module_type: module type requested
243  *
244  * Finds the requested sub module TLV type from the Preserved Field
245  * Area (PFA) and returns the TLV pointer and length. The caller can
246  * use these to read the variable length TLV value.
247  */
248 enum ice_status
249 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
250                        u16 module_type)
251 {
252         enum ice_status status;
253         u16 pfa_len, pfa_ptr;
254         u16 next_tlv;
255
256         status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
257         if (status != ICE_SUCCESS) {
258                 ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
259                 return status;
260         }
261         status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
262         if (status != ICE_SUCCESS) {
263                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
264                 return status;
265         }
266         /* Starting with first TLV after PFA length, iterate through the list
267          * of TLVs to find the requested one.
268          */
269         next_tlv = pfa_ptr + 1;
270         while (next_tlv < pfa_ptr + pfa_len) {
271                 u16 tlv_sub_module_type;
272                 u16 tlv_len;
273
274                 /* Read TLV type */
275                 status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
276                 if (status != ICE_SUCCESS) {
277                         ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
278                         break;
279                 }
280                 /* Read TLV length */
281                 status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
282                 if (status != ICE_SUCCESS) {
283                         ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
284                         break;
285                 }
286                 if (tlv_sub_module_type == module_type) {
287                         if (tlv_len) {
288                                 *module_tlv = next_tlv;
289                                 *module_tlv_len = tlv_len;
290                                 return ICE_SUCCESS;
291                         }
292                         return ICE_ERR_INVAL_SIZE;
293                 }
294                 /* Check next TLV, i.e. current TLV pointer + length + 2 words
295                  * (for current TLV's type and length)
296                  */
297                 next_tlv = next_tlv + tlv_len + 2;
298         }
299         /* Module does not exist */
300         return ICE_ERR_DOES_NOT_EXIST;
301 }
302
303 /**
304  * ice_read_pba_string - Reads part number string from NVM
305  * @hw: pointer to hardware structure
306  * @pba_num: stores the part number string from the NVM
307  * @pba_num_size: part number string buffer length
308  *
309  * Reads the part number string from the NVM.
310  */
311 enum ice_status
312 ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
313 {
314         u16 pba_tlv, pba_tlv_len;
315         enum ice_status status;
316         u16 pba_word, pba_size;
317         u16 i;
318
319         status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
320                                         ICE_SR_PBA_BLOCK_PTR);
321         if (status != ICE_SUCCESS) {
322                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
323                 return status;
324         }
325
326         /* pba_size is the next word */
327         status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
328         if (status != ICE_SUCCESS) {
329                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
330                 return status;
331         }
332
333         if (pba_tlv_len < pba_size) {
334                 ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
335                 return ICE_ERR_INVAL_SIZE;
336         }
337
338         /* Subtract one to get PBA word count (PBA Size word is included in
339          * total size)
340          */
341         pba_size--;
342         if (pba_num_size < (((u32)pba_size * 2) + 1)) {
343                 ice_debug(hw, ICE_DBG_INIT,
344                           "Buffer too small for PBA data.\n");
345                 return ICE_ERR_PARAM;
346         }
347
348         for (i = 0; i < pba_size; i++) {
349                 status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
350                 if (status != ICE_SUCCESS) {
351                         ice_debug(hw, ICE_DBG_INIT,
352                                   "Failed to read PBA Block word %d.\n", i);
353                         return status;
354                 }
355
356                 pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
357                 pba_num[(i * 2) + 1] = pba_word & 0xFF;
358         }
359         pba_num[(pba_size * 2)] = '\0';
360
361         return status;
362 }
363
364 /**
365  * ice_get_orom_ver_info - Read Option ROM version information
366  * @hw: pointer to the HW struct
367  *
368  * Read the Combo Image version data from the Boot Configuration TLV and fill
369  * in the option ROM version data.
370  */
371 static enum ice_status ice_get_orom_ver_info(struct ice_hw *hw)
372 {
373         u16 combo_hi, combo_lo, boot_cfg_tlv, boot_cfg_tlv_len;
374         struct ice_orom_info *orom = &hw->nvm.orom;
375         enum ice_status status;
376         u32 combo_ver;
377
378         status = ice_get_pfa_module_tlv(hw, &boot_cfg_tlv, &boot_cfg_tlv_len,
379                                         ICE_SR_BOOT_CFG_PTR);
380         if (status) {
381                 ice_debug(hw, ICE_DBG_INIT,
382                           "Failed to read Boot Configuration Block TLV.\n");
383                 return status;
384         }
385
386         /* Boot Configuration Block must have length at least 2 words
387          * (Combo Image Version High and Combo Image Version Low)
388          */
389         if (boot_cfg_tlv_len < 2) {
390                 ice_debug(hw, ICE_DBG_INIT,
391                           "Invalid Boot Configuration Block TLV size.\n");
392                 return ICE_ERR_INVAL_SIZE;
393         }
394
395         status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF),
396                                   &combo_hi);
397         if (status) {
398                 ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER hi.\n");
399                 return status;
400         }
401
402         status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF + 1),
403                                   &combo_lo);
404         if (status) {
405                 ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER lo.\n");
406                 return status;
407         }
408
409         combo_ver = ((u32)combo_hi << 16) | combo_lo;
410
411         orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >>
412                            ICE_OROM_VER_SHIFT);
413         orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
414         orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >>
415                             ICE_OROM_VER_BUILD_SHIFT);
416
417         return ICE_SUCCESS;
418 }
419
420 /**
421  * ice_discover_flash_size - Discover the available flash size.
422  * @hw: pointer to the HW struct
423  *
424  * The device flash could be up to 16MB in size. However, it is possible that
425  * the actual size is smaller. Use bisection to determine the accessible size
426  * of flash memory.
427  */
428 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
429 {
430         u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
431         enum ice_status status;
432
433         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
434
435         status = ice_acquire_nvm(hw, ICE_RES_READ);
436         if (status)
437                 return status;
438
439         while ((max_size - min_size) > 1) {
440                 u32 offset = (max_size + min_size) / 2;
441                 u32 len = 1;
442                 u8 data;
443
444                 status = ice_read_flat_nvm(hw, offset, &len, &data, false);
445                 if (status == ICE_ERR_AQ_ERROR &&
446                     hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
447                         ice_debug(hw, ICE_DBG_NVM,
448                                   "%s: New upper bound of %u bytes\n",
449                                   __func__, offset);
450                         status = ICE_SUCCESS;
451                         max_size = offset;
452                 } else if (!status) {
453                         ice_debug(hw, ICE_DBG_NVM,
454                                   "%s: New lower bound of %u bytes\n",
455                                   __func__, offset);
456                         min_size = offset;
457                 } else {
458                         /* an unexpected error occurred */
459                         goto err_read_flat_nvm;
460                 }
461         }
462
463         ice_debug(hw, ICE_DBG_NVM,
464                   "Predicted flash size is %u bytes\n", max_size);
465
466         hw->nvm.flash_size = max_size;
467
468 err_read_flat_nvm:
469         ice_release_nvm(hw);
470
471         return status;
472 }
473
474 /**
475  * ice_init_nvm - initializes NVM setting
476  * @hw: pointer to the HW struct
477  *
478  * This function reads and populates NVM settings such as Shadow RAM size,
479  * max_timeout, and blank_nvm_mode
480  */
481 enum ice_status ice_init_nvm(struct ice_hw *hw)
482 {
483         struct ice_nvm_info *nvm = &hw->nvm;
484         u16 eetrack_lo, eetrack_hi, ver;
485         enum ice_status status;
486         u32 fla, gens_stat;
487         u8 sr_size;
488
489         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
490
491         /* The SR size is stored regardless of the NVM programming mode
492          * as the blank mode may be used in the factory line.
493          */
494         gens_stat = rd32(hw, GLNVM_GENS);
495         sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
496
497         /* Switching to words (sr_size contains power of 2) */
498         nvm->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
499
500         /* Check if we are in the normal or blank NVM programming mode */
501         fla = rd32(hw, GLNVM_FLA);
502         if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
503                 nvm->blank_nvm_mode = false;
504         } else {
505                 /* Blank programming mode */
506                 nvm->blank_nvm_mode = true;
507                 ice_debug(hw, ICE_DBG_NVM,
508                           "NVM init error: unsupported blank mode.\n");
509                 return ICE_ERR_NVM_BLANK_MODE;
510         }
511
512         status = ice_read_sr_word(hw, ICE_SR_NVM_DEV_STARTER_VER, &ver);
513         if (status) {
514                 ice_debug(hw, ICE_DBG_INIT,
515                           "Failed to read DEV starter version.\n");
516                 return status;
517         }
518         nvm->major_ver = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
519         nvm->minor_ver = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
520
521         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
522         if (status) {
523                 ice_debug(hw, ICE_DBG_INIT, "Failed to read EETRACK lo.\n");
524                 return status;
525         }
526         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
527         if (status) {
528                 ice_debug(hw, ICE_DBG_INIT, "Failed to read EETRACK hi.\n");
529                 return status;
530         }
531
532         nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
533
534         status = ice_discover_flash_size(hw);
535         if (status) {
536                 ice_debug(hw, ICE_DBG_NVM,
537                           "NVM init error: failed to discover flash size.\n");
538                 return status;
539         }
540
541         switch (hw->device_id) {
542         /* the following devices do not have boot_cfg_tlv yet */
543         case ICE_DEV_ID_E822C_BACKPLANE:
544         case ICE_DEV_ID_E822C_QSFP:
545         case ICE_DEV_ID_E822C_10G_BASE_T:
546         case ICE_DEV_ID_E822C_SGMII:
547         case ICE_DEV_ID_E822C_SFP:
548         case ICE_DEV_ID_E822L_BACKPLANE:
549         case ICE_DEV_ID_E822L_SFP:
550         case ICE_DEV_ID_E822L_10G_BASE_T:
551         case ICE_DEV_ID_E822L_SGMII:
552         case ICE_DEV_ID_E823L_BACKPLANE:
553         case ICE_DEV_ID_E823L_SFP:
554         case ICE_DEV_ID_E823L_10G_BASE_T:
555         case ICE_DEV_ID_E823L_1GBE:
556         case ICE_DEV_ID_E823L_QSFP:
557                 return status;
558         default:
559                 break;
560         }
561
562         status = ice_get_orom_ver_info(hw);
563         if (status) {
564                 ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
565                 return status;
566         }
567
568         return ICE_SUCCESS;
569 }
570
571 /**
572  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
573  * @hw: pointer to the HW structure
574  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
575  * @words: (in) number of words to read; (out) number of words actually read
576  * @data: words read from the Shadow RAM
577  *
578  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
579  * method. The buf read is preceded by the NVM ownership take
580  * and followed by the release.
581  */
582 enum ice_status
583 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
584 {
585         enum ice_status status;
586
587         status = ice_acquire_nvm(hw, ICE_RES_READ);
588         if (!status) {
589                 status = ice_read_sr_buf_aq(hw, offset, words, data);
590                 ice_release_nvm(hw);
591         }
592
593         return status;
594 }
595
596 /**
597  * ice_nvm_validate_checksum
598  * @hw: pointer to the HW struct
599  *
600  * Verify NVM PFA checksum validity (0x0706)
601  */
602 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
603 {
604         struct ice_aqc_nvm_checksum *cmd;
605         struct ice_aq_desc desc;
606         enum ice_status status;
607
608         status = ice_acquire_nvm(hw, ICE_RES_READ);
609         if (status)
610                 return status;
611
612         cmd = &desc.params.nvm_checksum;
613
614         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
615         cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
616
617         status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
618         ice_release_nvm(hw);
619
620         if (!status)
621                 if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
622                         status = ICE_ERR_NVM_CHECKSUM;
623
624         return status;
625 }
626
627 /**
628  * ice_nvm_access_get_features - Return the NVM access features structure
629  * @cmd: NVM access command to process
630  * @data: storage for the driver NVM features
631  *
632  * Fill in the data section of the NVM access request with a copy of the NVM
633  * features structure.
634  */
635 enum ice_status
636 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
637                             union ice_nvm_access_data *data)
638 {
639         /* The provided data_size must be at least as large as our NVM
640          * features structure. A larger size should not be treated as an
641          * error, to allow future extensions to to the features structure to
642          * work on older drivers.
643          */
644         if (cmd->data_size < sizeof(struct ice_nvm_features))
645                 return ICE_ERR_NO_MEMORY;
646
647         /* Initialize the data buffer to zeros */
648         ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
649
650         /* Fill in the features data */
651         data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
652         data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
653         data->drv_features.size = sizeof(struct ice_nvm_features);
654         data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
655
656         return ICE_SUCCESS;
657 }
658
659 /**
660  * ice_nvm_access_get_module - Helper function to read module value
661  * @cmd: NVM access command structure
662  *
663  * Reads the module value out of the NVM access config field.
664  */
665 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
666 {
667         return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
668 }
669
670 /**
671  * ice_nvm_access_get_flags - Helper function to read flags value
672  * @cmd: NVM access command structure
673  *
674  * Reads the flags value out of the NVM access config field.
675  */
676 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
677 {
678         return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
679 }
680
681 /**
682  * ice_nvm_access_get_adapter - Helper function to read adapter info
683  * @cmd: NVM access command structure
684  *
685  * Read the adapter info value out of the NVM access config field.
686  */
687 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
688 {
689         return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
690                 ICE_NVM_CFG_ADAPTER_INFO_S);
691 }
692
693 /**
694  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
695  * @cmd: NVM access command structure
696  *
697  * Validates that an NVM access structure is request to read or write a valid
698  * register offset. First validates that the module and flags are correct, and
699  * then ensures that the register offset is one of the accepted registers.
700  */
701 static enum ice_status
702 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
703 {
704         u32 module, flags, offset;
705         u16 i;
706
707         module = ice_nvm_access_get_module(cmd);
708         flags = ice_nvm_access_get_flags(cmd);
709         offset = cmd->offset;
710
711         /* Make sure the module and flags indicate a read/write request */
712         if (module != ICE_NVM_REG_RW_MODULE ||
713             flags != ICE_NVM_REG_RW_FLAGS ||
714             cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
715                 return ICE_ERR_PARAM;
716
717         switch (offset) {
718         case GL_HICR:
719         case GL_HICR_EN: /* Note, this register is read only */
720         case GL_FWSTS:
721         case GL_MNG_FWSM:
722         case GLGEN_CSR_DEBUG_C:
723         case GLGEN_RSTAT:
724         case GLPCI_LBARCTRL:
725         case GLNVM_GENS:
726         case GLNVM_FLA:
727         case PF_FUNC_RID:
728                 return ICE_SUCCESS;
729         default:
730                 break;
731         }
732
733         for (i = 0; i <= ICE_NVM_ACCESS_GL_HIDA_MAX; i++)
734                 if (offset == (u32)GL_HIDA(i))
735                         return ICE_SUCCESS;
736
737         for (i = 0; i <= ICE_NVM_ACCESS_GL_HIBA_MAX; i++)
738                 if (offset == (u32)GL_HIBA(i))
739                         return ICE_SUCCESS;
740
741         /* All other register offsets are not valid */
742         return ICE_ERR_OUT_OF_RANGE;
743 }
744
745 /**
746  * ice_nvm_access_read - Handle an NVM read request
747  * @hw: pointer to the HW struct
748  * @cmd: NVM access command to process
749  * @data: storage for the register value read
750  *
751  * Process an NVM access request to read a register.
752  */
753 enum ice_status
754 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
755                     union ice_nvm_access_data *data)
756 {
757         enum ice_status status;
758
759         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
760
761         /* Always initialize the output data, even on failure */
762         ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
763
764         /* Make sure this is a valid read/write access request */
765         status = ice_validate_nvm_rw_reg(cmd);
766         if (status)
767                 return status;
768
769         ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
770                   cmd->offset);
771
772         /* Read the register and store the contents in the data field */
773         data->regval = rd32(hw, cmd->offset);
774
775         return ICE_SUCCESS;
776 }
777
778 /**
779  * ice_nvm_access_write - Handle an NVM write request
780  * @hw: pointer to the HW struct
781  * @cmd: NVM access command to process
782  * @data: NVM access data to write
783  *
784  * Process an NVM access request to write a register.
785  */
786 enum ice_status
787 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
788                      union ice_nvm_access_data *data)
789 {
790         enum ice_status status;
791
792         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
793
794         /* Make sure this is a valid read/write access request */
795         status = ice_validate_nvm_rw_reg(cmd);
796         if (status)
797                 return status;
798
799         /* Reject requests to write to read-only registers */
800         switch (cmd->offset) {
801         case GL_HICR_EN:
802         case GLGEN_RSTAT:
803                 return ICE_ERR_OUT_OF_RANGE;
804         default:
805                 break;
806         }
807
808         ice_debug(hw, ICE_DBG_NVM,
809                   "NVM access: writing register %08x with value %08x\n",
810                   cmd->offset, data->regval);
811
812         /* Write the data field to the specified register */
813         wr32(hw, cmd->offset, data->regval);
814
815         return ICE_SUCCESS;
816 }
817
818 /**
819  * ice_handle_nvm_access - Handle an NVM access request
820  * @hw: pointer to the HW struct
821  * @cmd: NVM access command info
822  * @data: pointer to read or return data
823  *
824  * Process an NVM access request. Read the command structure information and
825  * determine if it is valid. If not, report an error indicating the command
826  * was invalid.
827  *
828  * For valid commands, perform the necessary function, copying the data into
829  * the provided data buffer.
830  */
831 enum ice_status
832 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
833                       union ice_nvm_access_data *data)
834 {
835         u32 module, flags, adapter_info;
836
837         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
838
839         /* Extended flags are currently reserved and must be zero */
840         if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
841                 return ICE_ERR_PARAM;
842
843         /* Adapter info must match the HW device ID */
844         adapter_info = ice_nvm_access_get_adapter(cmd);
845         if (adapter_info != hw->device_id)
846                 return ICE_ERR_PARAM;
847
848         switch (cmd->command) {
849         case ICE_NVM_CMD_READ:
850                 module = ice_nvm_access_get_module(cmd);
851                 flags = ice_nvm_access_get_flags(cmd);
852
853                 /* Getting the driver's NVM features structure shares the same
854                  * command type as reading a register. Read the config field
855                  * to determine if this is a request to get features.
856                  */
857                 if (module == ICE_NVM_GET_FEATURES_MODULE &&
858                     flags == ICE_NVM_GET_FEATURES_FLAGS &&
859                     cmd->offset == 0)
860                         return ice_nvm_access_get_features(cmd, data);
861                 else
862                         return ice_nvm_access_read(hw, cmd, data);
863         case ICE_NVM_CMD_WRITE:
864                 return ice_nvm_access_write(hw, cmd, data);
865         default:
866                 return ICE_ERR_PARAM;
867         }
868 }