95a6c9ab614f4caf080e2af1010d1851bc41aad3
[dpdk.git] / drivers / net / ice / base / ice_nvm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2019
3  */
4
5 #include "ice_common.h"
6
7
8 /**
9  * ice_aq_read_nvm
10  * @hw: pointer to the HW struct
11  * @module_typeid: module pointer location in words from the NVM beginning
12  * @offset: byte offset from the module beginning
13  * @length: length of the section to be read (in bytes from the offset)
14  * @data: command buffer (size [bytes] = length)
15  * @last_command: tells if this is the last command in a series
16  * @read_shadow_ram: tell if this is a shadow RAM read
17  * @cd: pointer to command details structure or NULL
18  *
19  * Read the NVM using the admin queue commands (0x0701)
20  */
21 static enum ice_status
22 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
23                 void *data, bool last_command, bool read_shadow_ram,
24                 struct ice_sq_cd *cd)
25 {
26         struct ice_aq_desc desc;
27         struct ice_aqc_nvm *cmd;
28
29         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
30
31         cmd = &desc.params.nvm;
32
33         /* In offset the highest byte must be zeroed. */
34         if (offset & 0xFF000000)
35                 return ICE_ERR_PARAM;
36
37         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
38
39         if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
40                 cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
41
42         /* If this is the last command in a series, set the proper flag. */
43         if (last_command)
44                 cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
45         cmd->module_typeid = CPU_TO_LE16(module_typeid);
46         cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
47         cmd->offset_high = (offset >> 16) & 0xFF;
48         cmd->length = CPU_TO_LE16(length);
49
50         return ice_aq_send_cmd(hw, &desc, data, length, cd);
51 }
52
53 /**
54  * ice_check_sr_access_params - verify params for Shadow RAM R/W operations.
55  * @hw: pointer to the HW structure
56  * @offset: offset in words from module start
57  * @words: number of words to access
58  */
59 static enum ice_status
60 ice_check_sr_access_params(struct ice_hw *hw, u32 offset, u16 words)
61 {
62         if ((offset + words) > hw->nvm.sr_words) {
63                 ice_debug(hw, ICE_DBG_NVM,
64                           "NVM error: offset beyond SR lmt.\n");
65                 return ICE_ERR_PARAM;
66         }
67
68         if (words > ICE_SR_SECTOR_SIZE_IN_WORDS) {
69                 /* We can access only up to 4KB (one sector), in one AQ write */
70                 ice_debug(hw, ICE_DBG_NVM,
71                           "NVM error: tried to access %d words, limit is %d.\n",
72                           words, ICE_SR_SECTOR_SIZE_IN_WORDS);
73                 return ICE_ERR_PARAM;
74         }
75
76         if (((offset + (words - 1)) / ICE_SR_SECTOR_SIZE_IN_WORDS) !=
77             (offset / ICE_SR_SECTOR_SIZE_IN_WORDS)) {
78                 /* A single access cannot spread over two sectors */
79                 ice_debug(hw, ICE_DBG_NVM,
80                           "NVM error: cannot spread over two sectors.\n");
81                 return ICE_ERR_PARAM;
82         }
83
84         return ICE_SUCCESS;
85 }
86
87 /**
88  * ice_read_sr_aq - Read Shadow RAM.
89  * @hw: pointer to the HW structure
90  * @offset: offset in words from module start
91  * @words: number of words to read
92  * @data: buffer for words reads from Shadow RAM
93  * @last_command: tells the AdminQ that this is the last command
94  *
95  * Reads 16-bit word buffers from the Shadow RAM using the admin command.
96  */
97 static enum ice_status
98 ice_read_sr_aq(struct ice_hw *hw, u32 offset, u16 words, u16 *data,
99                bool last_command)
100 {
101         enum ice_status status;
102
103         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
104
105         status = ice_check_sr_access_params(hw, offset, words);
106
107         /* values in "offset" and "words" parameters are sized as words
108          * (16 bits) but ice_aq_read_nvm expects these values in bytes.
109          * So do this conversion while calling ice_aq_read_nvm.
110          */
111         if (!status)
112                 status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
113                                          2 * offset, 2 * words, data,
114                                          last_command, true, NULL);
115
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 the ice_read_sr_aq method.
126  */
127 static enum ice_status
128 ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
129 {
130         enum ice_status status;
131
132         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
133
134         status = ice_read_sr_aq(hw, offset, 1, data, true);
135         if (!status)
136                 *data = LE16_TO_CPU(*(_FORCE_ __le16 *)data);
137
138         return status;
139 }
140
141
142 /**
143  * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
144  * @hw: pointer to the HW structure
145  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
146  * @words: (in) number of words to read; (out) number of words actually read
147  * @data: words read from the Shadow RAM
148  *
149  * Reads 16 bit words (data buf) from the SR using the ice_read_sr_aq
150  * method. Ownership of the NVM is taken before reading the buffer and later
151  * released.
152  */
153 static enum ice_status
154 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
155 {
156         enum ice_status status;
157         bool last_cmd = false;
158         u16 words_read = 0;
159         u16 i = 0;
160
161         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
162
163         do {
164                 u16 read_size, off_w;
165
166                 /* Calculate number of bytes we should read in this step.
167                  * It's not allowed to read more than one page at a time or
168                  * to cross page boundaries.
169                  */
170                 off_w = offset % ICE_SR_SECTOR_SIZE_IN_WORDS;
171                 read_size = off_w ?
172                         MIN_T(u16, *words,
173                               (ICE_SR_SECTOR_SIZE_IN_WORDS - off_w)) :
174                         MIN_T(u16, (*words - words_read),
175                               ICE_SR_SECTOR_SIZE_IN_WORDS);
176
177                 /* Check if this is last command, if so set proper flag */
178                 if ((words_read + read_size) >= *words)
179                         last_cmd = true;
180
181                 status = ice_read_sr_aq(hw, offset, read_size,
182                                         data + words_read, last_cmd);
183                 if (status)
184                         goto read_nvm_buf_aq_exit;
185
186                 /* Increment counter for words already read and move offset to
187                  * new read location
188                  */
189                 words_read += read_size;
190                 offset += read_size;
191         } while (words_read < *words);
192
193         for (i = 0; i < *words; i++)
194                 data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
195
196 read_nvm_buf_aq_exit:
197         *words = words_read;
198         return status;
199 }
200
201 /**
202  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
203  * @hw: pointer to the HW structure
204  * @access: NVM access type (read or write)
205  *
206  * This function will request NVM ownership.
207  */
208 static enum ice_status
209 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
210 {
211         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
212
213         if (hw->nvm.blank_nvm_mode)
214                 return ICE_SUCCESS;
215
216         return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
217 }
218
219 /**
220  * ice_release_nvm - Generic request for releasing the NVM ownership
221  * @hw: pointer to the HW structure
222  *
223  * This function will release NVM ownership.
224  */
225 static void ice_release_nvm(struct ice_hw *hw)
226 {
227         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
228
229         if (hw->nvm.blank_nvm_mode)
230                 return;
231
232         ice_release_res(hw, ICE_NVM_RES_ID);
233 }
234
235 /**
236  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
237  * @hw: pointer to the HW structure
238  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
239  * @data: word read from the Shadow RAM
240  *
241  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
242  */
243 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
244 {
245         enum ice_status status;
246
247         status = ice_acquire_nvm(hw, ICE_RES_READ);
248         if (!status) {
249                 status = ice_read_sr_word_aq(hw, offset, data);
250                 ice_release_nvm(hw);
251         }
252
253         return status;
254 }
255
256 /**
257  * ice_init_nvm - initializes NVM setting
258  * @hw: pointer to the HW struct
259  *
260  * This function reads and populates NVM settings such as Shadow RAM size,
261  * max_timeout, and blank_nvm_mode
262  */
263 enum ice_status ice_init_nvm(struct ice_hw *hw)
264 {
265         struct ice_nvm_info *nvm = &hw->nvm;
266         u16 oem_hi, oem_lo, boot_cfg_tlv, boot_cfg_tlv_len;
267         u16 eetrack_lo, eetrack_hi;
268         enum ice_status status;
269         u32 fla, gens_stat;
270         u8 sr_size;
271
272         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
273
274         /* The SR size is stored regardless of the NVM programming mode
275          * as the blank mode may be used in the factory line.
276          */
277         gens_stat = rd32(hw, GLNVM_GENS);
278         sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
279
280         /* Switching to words (sr_size contains power of 2) */
281         nvm->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
282
283         /* Check if we are in the normal or blank NVM programming mode */
284         fla = rd32(hw, GLNVM_FLA);
285         if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
286                 nvm->blank_nvm_mode = false;
287         } else {
288                 /* Blank programming mode */
289                 nvm->blank_nvm_mode = true;
290                 ice_debug(hw, ICE_DBG_NVM,
291                           "NVM init error: unsupported blank mode.\n");
292                 return ICE_ERR_NVM_BLANK_MODE;
293         }
294
295         status = ice_read_sr_word(hw, ICE_SR_NVM_DEV_STARTER_VER, &nvm->ver);
296         if (status) {
297                 ice_debug(hw, ICE_DBG_INIT,
298                           "Failed to read DEV starter version.\n");
299                 return status;
300         }
301
302         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
303         if (status) {
304                 ice_debug(hw, ICE_DBG_INIT, "Failed to read EETRACK lo.\n");
305                 return status;
306         }
307         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
308         if (status) {
309                 ice_debug(hw, ICE_DBG_INIT, "Failed to read EETRACK hi.\n");
310                 return status;
311         }
312
313         nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
314
315         /* the following devices do not have boot_cfg_tlv yet */
316         if (hw->device_id == ICE_DEV_ID_C822N_BACKPLANE ||
317             hw->device_id == ICE_DEV_ID_C822N_QSFP ||
318             hw->device_id == ICE_DEV_ID_C822N_SFP)
319                 return status;
320
321         status = ice_get_pfa_module_tlv(hw, &boot_cfg_tlv, &boot_cfg_tlv_len,
322                                         ICE_SR_BOOT_CFG_PTR);
323         if (status) {
324                 ice_debug(hw, ICE_DBG_INIT,
325                           "Failed to read Boot Configuration Block TLV.\n");
326                 return status;
327         }
328
329         /* Boot Configuration Block must have length at least 2 words
330          * (Combo Image Version High and Combo Image Version Low)
331          */
332         if (boot_cfg_tlv_len < 2) {
333                 ice_debug(hw, ICE_DBG_INIT,
334                           "Invalid Boot Configuration Block TLV size.\n");
335                 return ICE_ERR_INVAL_SIZE;
336         }
337
338         status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OEM_VER_OFF),
339                                   &oem_hi);
340         if (status) {
341                 ice_debug(hw, ICE_DBG_INIT, "Failed to read OEM_VER hi.\n");
342                 return status;
343         }
344
345         status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OEM_VER_OFF + 1),
346                                   &oem_lo);
347         if (status) {
348                 ice_debug(hw, ICE_DBG_INIT, "Failed to read OEM_VER lo.\n");
349                 return status;
350         }
351
352         nvm->oem_ver = ((u32)oem_hi << 16) | oem_lo;
353
354         return ICE_SUCCESS;
355 }
356
357
358 /**
359  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
360  * @hw: pointer to the HW structure
361  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
362  * @words: (in) number of words to read; (out) number of words actually read
363  * @data: words read from the Shadow RAM
364  *
365  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
366  * method. The buf read is preceded by the NVM ownership take
367  * and followed by the release.
368  */
369 enum ice_status
370 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
371 {
372         enum ice_status status;
373
374         status = ice_acquire_nvm(hw, ICE_RES_READ);
375         if (!status) {
376                 status = ice_read_sr_buf_aq(hw, offset, words, data);
377                 ice_release_nvm(hw);
378         }
379
380         return status;
381 }
382
383
384 /**
385  * ice_nvm_validate_checksum
386  * @hw: pointer to the HW struct
387  *
388  * Verify NVM PFA checksum validity (0x0706)
389  */
390 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
391 {
392         struct ice_aqc_nvm_checksum *cmd;
393         struct ice_aq_desc desc;
394         enum ice_status status;
395
396         status = ice_acquire_nvm(hw, ICE_RES_READ);
397         if (status)
398                 return status;
399
400         cmd = &desc.params.nvm_checksum;
401
402         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
403         cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
404
405         status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
406         ice_release_nvm(hw);
407
408         if (!status)
409                 if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
410                         status = ICE_ERR_NVM_CHECKSUM;
411
412         return status;
413 }