1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022, Intel Corporation. */
4 #include "ice_common.h"
9 /* For supporting double VLAN mode, it is necessary to enable or disable certain
10 * boost tcam entries. The metadata labels names that match the following
11 * prefixes will be saved to allow enabling double VLAN mode.
13 #define ICE_DVM_PRE "BOOST_MAC_VLAN_DVM" /* enable these entries */
14 #define ICE_SVM_PRE "BOOST_MAC_VLAN_SVM" /* disable these entries */
16 /* To support tunneling entries by PF, the package will append the PF number to
17 * the label; for example TNL_VXLAN_PF0, TNL_VXLAN_PF1, TNL_VXLAN_PF2, etc.
19 #define ICE_TNL_PRE "TNL_"
20 static const struct ice_tunnel_type_scan tnls[] = {
21 { TNL_VXLAN, "TNL_VXLAN_PF" },
22 { TNL_GENEVE, "TNL_GENEVE_PF" },
27 * ice_verify_pkg - verify package
28 * @pkg: pointer to the package buffer
29 * @len: size of the package buffer
31 * Verifies various attributes of the package file, including length, format
32 * version, and the requirement of at least one segment.
34 static enum ice_ddp_state ice_verify_pkg(const struct ice_pkg_hdr *pkg, u32 len)
39 if (len < struct_size(pkg, seg_offset, 1))
40 return ICE_DDP_PKG_INVALID_FILE;
42 if (pkg->pkg_format_ver.major != ICE_PKG_FMT_VER_MAJ ||
43 pkg->pkg_format_ver.minor != ICE_PKG_FMT_VER_MNR ||
44 pkg->pkg_format_ver.update != ICE_PKG_FMT_VER_UPD ||
45 pkg->pkg_format_ver.draft != ICE_PKG_FMT_VER_DFT)
46 return ICE_DDP_PKG_INVALID_FILE;
48 /* pkg must have at least one segment */
49 seg_count = le32_to_cpu(pkg->seg_count);
51 return ICE_DDP_PKG_INVALID_FILE;
53 /* make sure segment array fits in package length */
54 if (len < struct_size(pkg, seg_offset, seg_count))
55 return ICE_DDP_PKG_INVALID_FILE;
57 /* all segments must fit within length */
58 for (i = 0; i < seg_count; i++) {
59 u32 off = le32_to_cpu(pkg->seg_offset[i]);
60 const struct ice_generic_seg_hdr *seg;
62 /* segment header must fit */
63 if (len < off + sizeof(*seg))
64 return ICE_DDP_PKG_INVALID_FILE;
66 seg = (void *)pkg + off;
68 /* segment body must fit */
69 if (len < off + le32_to_cpu(seg->seg_size))
70 return ICE_DDP_PKG_INVALID_FILE;
73 return ICE_DDP_PKG_SUCCESS;
77 * ice_free_seg - free package segment pointer
78 * @hw: pointer to the hardware structure
80 * Frees the package segment pointer in the proper manner, depending on if the
81 * segment was allocated or just the passed in pointer was stored.
83 void ice_free_seg(struct ice_hw *hw)
86 devm_kfree(ice_hw_to_dev(hw), hw->pkg_copy);
94 * ice_chk_pkg_version - check package version for compatibility with driver
95 * @pkg_ver: pointer to a version structure to check
97 * Check to make sure that the package about to be downloaded is compatible with
98 * the driver. To be compatible, the major and minor components of the package
99 * version must match our ICE_PKG_SUPP_VER_MAJ and ICE_PKG_SUPP_VER_MNR
102 static enum ice_ddp_state ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
104 if (pkg_ver->major > ICE_PKG_SUPP_VER_MAJ ||
105 (pkg_ver->major == ICE_PKG_SUPP_VER_MAJ &&
106 pkg_ver->minor > ICE_PKG_SUPP_VER_MNR))
107 return ICE_DDP_PKG_FILE_VERSION_TOO_HIGH;
108 else if (pkg_ver->major < ICE_PKG_SUPP_VER_MAJ ||
109 (pkg_ver->major == ICE_PKG_SUPP_VER_MAJ &&
110 pkg_ver->minor < ICE_PKG_SUPP_VER_MNR))
111 return ICE_DDP_PKG_FILE_VERSION_TOO_LOW;
113 return ICE_DDP_PKG_SUCCESS;
118 * @buf: pointer to the ice buffer
120 * This helper function validates a buffer's header.
122 static const struct ice_buf_hdr *ice_pkg_val_buf(const struct ice_buf *buf)
124 const struct ice_buf_hdr *hdr;
128 hdr = (const struct ice_buf_hdr *)buf->buf;
130 section_count = le16_to_cpu(hdr->section_count);
131 if (section_count < ICE_MIN_S_COUNT || section_count > ICE_MAX_S_COUNT)
134 data_end = le16_to_cpu(hdr->data_end);
135 if (data_end < ICE_MIN_S_DATA_END || data_end > ICE_MAX_S_DATA_END)
143 * @ice_seg: pointer to the ice segment
145 * Returns the address of the buffer table within the ice segment.
147 static struct ice_buf_table *ice_find_buf_table(struct ice_seg *ice_seg)
149 struct ice_nvm_table *nvms = (struct ice_nvm_table *)
150 (ice_seg->device_table + le32_to_cpu(ice_seg->device_table_count));
152 return (__force struct ice_buf_table *)(nvms->vers +
153 le32_to_cpu(nvms->table_count));
158 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
159 * @state: pointer to the enum state
161 * This function will enumerate all the buffers in the ice segment. The first
162 * call is made with the ice_seg parameter non-NULL; on subsequent calls,
163 * ice_seg is set to NULL which continues the enumeration. When the function
164 * returns a NULL pointer, then the end of the buffers has been reached, or an
165 * unexpected value has been detected (for example an invalid section count or
166 * an invalid buffer end value).
168 static const struct ice_buf_hdr *ice_pkg_enum_buf(struct ice_seg *ice_seg,
169 struct ice_pkg_enum *state)
172 state->buf_table = ice_find_buf_table(ice_seg);
173 if (!state->buf_table)
177 return ice_pkg_val_buf(state->buf_table->buf_array);
180 if (++state->buf_idx < le32_to_cpu(state->buf_table->buf_count))
181 return ice_pkg_val_buf(state->buf_table->buf_array +
188 * ice_pkg_advance_sect
189 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
190 * @state: pointer to the enum state
192 * This helper function will advance the section within the ice segment,
193 * also advancing the buffer if needed.
195 static bool ice_pkg_advance_sect(struct ice_seg *ice_seg,
196 struct ice_pkg_enum *state)
198 if (!ice_seg && !state->buf)
201 if (!ice_seg && state->buf)
202 if (++state->sect_idx < le16_to_cpu(state->buf->section_count))
205 state->buf = ice_pkg_enum_buf(ice_seg, state);
209 /* start of new buffer, reset section index */
215 * ice_pkg_enum_section
216 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
217 * @state: pointer to the enum state
218 * @sect_type: section type to enumerate
220 * This function will enumerate all the sections of a particular type in the
221 * ice segment. The first call is made with the ice_seg parameter non-NULL;
222 * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
223 * When the function returns a NULL pointer, then the end of the matching
224 * sections has been reached.
226 void *ice_pkg_enum_section(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
232 state->type = sect_type;
234 if (!ice_pkg_advance_sect(ice_seg, state))
237 /* scan for next matching section */
238 while (state->buf->section_entry[state->sect_idx].type !=
239 cpu_to_le32(state->type))
240 if (!ice_pkg_advance_sect(NULL, state))
243 /* validate section */
244 offset = le16_to_cpu(state->buf->section_entry[state->sect_idx].offset);
245 if (offset < ICE_MIN_S_OFF || offset > ICE_MAX_S_OFF)
248 size = le16_to_cpu(state->buf->section_entry[state->sect_idx].size);
249 if (size < ICE_MIN_S_SZ || size > ICE_MAX_S_SZ)
252 /* make sure the section fits in the buffer */
253 if (offset + size > ICE_PKG_BUF_SIZE)
257 le32_to_cpu(state->buf->section_entry[state->sect_idx].type);
259 /* calc pointer to this section */
262 le16_to_cpu(state->buf->section_entry[state->sect_idx].offset);
269 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
270 * @state: pointer to the enum state
271 * @sect_type: section type to enumerate
272 * @offset: pointer to variable that receives the offset in the table (optional)
273 * @handler: function that handles access to the entries into the section type
275 * This function will enumerate all the entries in particular section type in
276 * the ice segment. The first call is made with the ice_seg parameter non-NULL;
277 * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
278 * When the function returns a NULL pointer, then the end of the entries has
281 * Since each section may have a different header and entry size, the handler
282 * function is needed to determine the number and location entries in each
285 * The offset parameter is optional, but should be used for sections that
286 * contain an offset for each section table. For such cases, the section handler
287 * function must return the appropriate offset + index to give the absolution
288 * offset for each entry. For example, if the base for a section's header
289 * indicates a base offset of 10, and the index for the entry is 2, then
290 * section handler function should set the offset to 10 + 2 = 12.
292 void *ice_pkg_enum_entry(struct ice_seg *ice_seg,
293 struct ice_pkg_enum *state, u32 sect_type,
295 void *(*handler)(u32 sect_type, void *section,
296 u32 index, u32 *offset))
304 if (!ice_pkg_enum_section(ice_seg, state, sect_type))
307 state->entry_idx = 0;
308 state->handler = handler;
317 entry = state->handler(state->sect_type, state->sect, state->entry_idx,
320 /* end of a section, look for another section of this type */
321 if (!ice_pkg_enum_section(NULL, state, 0))
324 state->entry_idx = 0;
325 entry = state->handler(state->sect_type, state->sect,
326 state->entry_idx, offset);
334 * @sect_type: section type
335 * @section: pointer to section
336 * @index: index of the field vector entry to be returned
337 * @offset: ptr to variable that receives the offset in the field vector table
339 * This is a callback function that can be passed to ice_pkg_enum_entry.
340 * This function treats the given section as of type ice_sw_fv_section and
341 * enumerates offset field. "offset" is an index into the field vector table.
343 static void *ice_sw_fv_handler(u32 sect_type, void *section, u32 index,
346 struct ice_sw_fv_section *fv_section = section;
348 if (!section || sect_type != ICE_SID_FLD_VEC_SW)
350 if (index >= le16_to_cpu(fv_section->count))
353 /* "index" passed in to this function is relative to a given
354 * 4k block. To get to the true index into the field vector
355 * table need to add the relative index to the base_offset
356 * field of this section
358 *offset = le16_to_cpu(fv_section->base_offset) + index;
359 return fv_section->fv + index;
363 * ice_get_prof_index_max - get the max profile index for used profile
364 * @hw: pointer to the HW struct
366 * Calling this function will get the max profile index for used profile
367 * and store the index number in struct ice_switch_info *switch_info
368 * in HW for following use.
370 static int ice_get_prof_index_max(struct ice_hw *hw)
372 u16 prof_index = 0, j, max_prof_index = 0;
373 struct ice_pkg_enum state;
374 struct ice_seg *ice_seg;
379 memset(&state, 0, sizeof(state));
387 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
388 &offset, ice_sw_fv_handler);
393 /* in the profile that not be used, the prot_id is set to 0xff
394 * and the off is set to 0x1ff for all the field vectors.
396 for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
397 if (fv->ew[j].prot_id != ICE_PROT_INVALID ||
398 fv->ew[j].off != ICE_FV_OFFSET_INVAL)
400 if (flag && prof_index > max_prof_index)
401 max_prof_index = prof_index;
407 hw->switch_info->max_used_prof_index = max_prof_index;
413 * ice_get_ddp_pkg_state - get DDP pkg state after download
414 * @hw: pointer to the HW struct
415 * @already_loaded: indicates if pkg was already loaded onto the device
417 static enum ice_ddp_state ice_get_ddp_pkg_state(struct ice_hw *hw,
420 if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
421 hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
422 hw->pkg_ver.update == hw->active_pkg_ver.update &&
423 hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
424 !memcmp(hw->pkg_name, hw->active_pkg_name, sizeof(hw->pkg_name))) {
426 return ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED;
428 return ICE_DDP_PKG_SUCCESS;
429 } else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
430 hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
431 return ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED;
432 } else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
433 hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
434 return ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED;
436 return ICE_DDP_PKG_ERR;
441 * ice_init_pkg_regs - initialize additional package registers
442 * @hw: pointer to the hardware structure
444 static void ice_init_pkg_regs(struct ice_hw *hw)
446 #define ICE_SW_BLK_INP_MASK_L 0xFFFFFFFF
447 #define ICE_SW_BLK_INP_MASK_H 0x0000FFFF
448 #define ICE_SW_BLK_IDX 0
450 /* setup Switch block input mask, which is 48-bits in two parts */
451 wr32(hw, GL_PREEXT_L2_PMASK0(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_L);
452 wr32(hw, GL_PREEXT_L2_PMASK1(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_H);
456 * ice_marker_ptype_tcam_handler
457 * @sect_type: section type
458 * @section: pointer to section
459 * @index: index of the Marker PType TCAM entry to be returned
460 * @offset: pointer to receive absolute offset, always 0 for ptype TCAM sections
462 * This is a callback function that can be passed to ice_pkg_enum_entry.
463 * Handles enumeration of individual Marker PType TCAM entries.
465 static void *ice_marker_ptype_tcam_handler(u32 sect_type, void *section,
466 u32 index, u32 *offset)
468 struct ice_marker_ptype_tcam_section *marker_ptype;
470 if (sect_type != ICE_SID_RXPARSER_MARKER_PTYPE)
473 if (index > ICE_MAX_MARKER_PTYPE_TCAMS_IN_BUF)
479 marker_ptype = section;
480 if (index >= le16_to_cpu(marker_ptype->count))
483 return marker_ptype->tcam + index;
488 * @hw: pointer to the HW structure
489 * @val: value of the boost entry
490 * @enable: true if entry needs to be enabled, or false if needs to be disabled
492 static void ice_add_dvm_hint(struct ice_hw *hw, u16 val, bool enable)
494 if (hw->dvm_upd.count < ICE_DVM_MAX_ENTRIES) {
495 hw->dvm_upd.tbl[hw->dvm_upd.count].boost_addr = val;
496 hw->dvm_upd.tbl[hw->dvm_upd.count].enable = enable;
502 * ice_add_tunnel_hint
503 * @hw: pointer to the HW structure
504 * @label_name: label text
505 * @val: value of the tunnel port boost entry
507 static void ice_add_tunnel_hint(struct ice_hw *hw, char *label_name, u16 val)
509 if (hw->tnl.count < ICE_TUNNEL_MAX_ENTRIES) {
512 for (i = 0; tnls[i].type != TNL_LAST; i++) {
513 size_t len = strlen(tnls[i].label_prefix);
515 /* Look for matching label start, before continuing */
516 if (strncmp(label_name, tnls[i].label_prefix, len))
519 /* Make sure this label matches our PF. Note that the PF
520 * character ('0' - '7') will be located where our
521 * prefix string's null terminator is located.
523 if ((label_name[len] - '0') == hw->pf_id) {
524 hw->tnl.tbl[hw->tnl.count].type = tnls[i].type;
525 hw->tnl.tbl[hw->tnl.count].valid = false;
526 hw->tnl.tbl[hw->tnl.count].boost_addr = val;
527 hw->tnl.tbl[hw->tnl.count].port = 0;
536 * ice_label_enum_handler
537 * @sect_type: section type
538 * @section: pointer to section
539 * @index: index of the label entry to be returned
540 * @offset: pointer to receive absolute offset, always zero for label sections
542 * This is a callback function that can be passed to ice_pkg_enum_entry.
543 * Handles enumeration of individual label entries.
545 static void *ice_label_enum_handler(u32 __always_unused sect_type,
546 void *section, u32 index, u32 *offset)
548 struct ice_label_section *labels;
553 if (index > ICE_MAX_LABELS_IN_BUF)
560 if (index >= le16_to_cpu(labels->count))
563 return labels->label + index;
568 * @ice_seg: pointer to the ice segment (NULL on subsequent calls)
569 * @type: the section type that will contain the label (0 on subsequent calls)
570 * @state: ice_pkg_enum structure that will hold the state of the enumeration
571 * @value: pointer to a value that will return the label's value if found
573 * Enumerates a list of labels in the package. The caller will call
574 * ice_enum_labels(ice_seg, type, ...) to start the enumeration, then call
575 * ice_enum_labels(NULL, 0, ...) to continue. When the function returns a NULL
576 * the end of the list has been reached.
578 static char *ice_enum_labels(struct ice_seg *ice_seg, u32 type,
579 struct ice_pkg_enum *state, u16 *value)
581 struct ice_label *label;
583 /* Check for valid label section on first call */
584 if (type && !(type >= ICE_SID_LBL_FIRST && type <= ICE_SID_LBL_LAST))
587 label = ice_pkg_enum_entry(ice_seg, state, type, NULL,
588 ice_label_enum_handler);
592 *value = le16_to_cpu(label->value);
597 * ice_boost_tcam_handler
598 * @sect_type: section type
599 * @section: pointer to section
600 * @index: index of the boost TCAM entry to be returned
601 * @offset: pointer to receive absolute offset, always 0 for boost TCAM sections
603 * This is a callback function that can be passed to ice_pkg_enum_entry.
604 * Handles enumeration of individual boost TCAM entries.
606 static void *ice_boost_tcam_handler(u32 sect_type, void *section, u32 index,
609 struct ice_boost_tcam_section *boost;
614 if (sect_type != ICE_SID_RXPARSER_BOOST_TCAM)
617 if (index > ICE_MAX_BST_TCAMS_IN_BUF)
624 if (index >= le16_to_cpu(boost->count))
627 return boost->tcam + index;
631 * ice_find_boost_entry
632 * @ice_seg: pointer to the ice segment (non-NULL)
633 * @addr: Boost TCAM address of entry to search for
634 * @entry: returns pointer to the entry
636 * Finds a particular Boost TCAM entry and returns a pointer to that entry
637 * if it is found. The ice_seg parameter must not be NULL since the first call
638 * to ice_pkg_enum_entry requires a pointer to an actual ice_segment structure.
640 static int ice_find_boost_entry(struct ice_seg *ice_seg, u16 addr,
641 struct ice_boost_tcam_entry **entry)
643 struct ice_boost_tcam_entry *tcam;
644 struct ice_pkg_enum state;
646 memset(&state, 0, sizeof(state));
652 tcam = ice_pkg_enum_entry(ice_seg, &state,
653 ICE_SID_RXPARSER_BOOST_TCAM, NULL,
654 ice_boost_tcam_handler);
655 if (tcam && le16_to_cpu(tcam->addr) == addr) {
668 * ice_is_init_pkg_successful - check if DDP init was successful
669 * @state: state of the DDP pkg after download
671 bool ice_is_init_pkg_successful(enum ice_ddp_state state)
674 case ICE_DDP_PKG_SUCCESS:
675 case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
676 case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
685 * @hw: pointer to the HW structure
687 * Allocates a package buffer and returns a pointer to the buffer header.
688 * Note: all package contents must be in Little Endian form.
690 struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
692 struct ice_buf_build *bld;
693 struct ice_buf_hdr *buf;
695 bld = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*bld), GFP_KERNEL);
699 buf = (struct ice_buf_hdr *)bld;
701 cpu_to_le16(offsetof(struct ice_buf_hdr, section_entry));
705 static bool ice_is_gtp_u_profile(u16 prof_idx)
707 return (prof_idx >= ICE_PROFID_IPV6_GTPU_TEID &&
708 prof_idx <= ICE_PROFID_IPV6_GTPU_IPV6_TCP_INNER) ||
709 prof_idx == ICE_PROFID_IPV4_GTPU_TEID;
712 static bool ice_is_gtp_c_profile(u16 prof_idx)
715 case ICE_PROFID_IPV4_GTPC_TEID:
716 case ICE_PROFID_IPV4_GTPC_NO_TEID:
717 case ICE_PROFID_IPV6_GTPC_TEID:
718 case ICE_PROFID_IPV6_GTPC_NO_TEID:
725 static bool ice_is_pfcp_profile(u16 prof_idx)
727 return prof_idx >= ICE_PROFID_IPV4_PFCP_NODE &&
728 prof_idx <= ICE_PROFID_IPV6_PFCP_SESSION;
732 * ice_get_sw_prof_type - determine switch profile type
733 * @hw: pointer to the HW structure
734 * @fv: pointer to the switch field vector
735 * @prof_idx: profile index to check
737 static enum ice_prof_type ice_get_sw_prof_type(struct ice_hw *hw,
738 struct ice_fv *fv, u32 prof_idx)
742 if (ice_is_gtp_c_profile(prof_idx))
743 return ICE_PROF_TUN_GTPC;
745 if (ice_is_gtp_u_profile(prof_idx))
746 return ICE_PROF_TUN_GTPU;
748 if (ice_is_pfcp_profile(prof_idx))
749 return ICE_PROF_TUN_PFCP;
751 for (i = 0; i < hw->blk[ICE_BLK_SW].es.fvw; i++) {
752 /* UDP tunnel will have UDP_OF protocol ID and VNI offset */
753 if (fv->ew[i].prot_id == (u8)ICE_PROT_UDP_OF &&
754 fv->ew[i].off == ICE_VNI_OFFSET)
755 return ICE_PROF_TUN_UDP;
757 /* GRE tunnel will have GRE protocol */
758 if (fv->ew[i].prot_id == (u8)ICE_PROT_GRE_OF)
759 return ICE_PROF_TUN_GRE;
762 return ICE_PROF_NON_TUN;
766 * ice_get_sw_fv_bitmap - Get switch field vector bitmap based on profile type
767 * @hw: pointer to hardware structure
768 * @req_profs: type of profiles requested
769 * @bm: pointer to memory for returning the bitmap of field vectors
771 void ice_get_sw_fv_bitmap(struct ice_hw *hw, enum ice_prof_type req_profs,
774 struct ice_pkg_enum state;
775 struct ice_seg *ice_seg;
778 if (req_profs == ICE_PROF_ALL) {
779 bitmap_set(bm, 0, ICE_MAX_NUM_PROFILES);
783 memset(&state, 0, sizeof(state));
784 bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
787 enum ice_prof_type prof_type;
790 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
791 &offset, ice_sw_fv_handler);
795 /* Determine field vector type */
796 prof_type = ice_get_sw_prof_type(hw, fv, offset);
798 if (req_profs & prof_type)
799 set_bit((u16)offset, bm);
806 * @hw: pointer to the HW structure
807 * @lkups: list of protocol types
808 * @bm: bitmap of field vectors to consider
809 * @fv_list: Head of a list
811 * Finds all the field vector entries from switch block that contain
812 * a given protocol ID and offset and returns a list of structures of type
813 * "ice_sw_fv_list_entry". Every structure in the list has a field vector
814 * definition and profile ID information
815 * NOTE: The caller of the function is responsible for freeing the memory
816 * allocated for every list entry.
818 int ice_get_sw_fv_list(struct ice_hw *hw, struct ice_prot_lkup_ext *lkups,
819 unsigned long *bm, struct list_head *fv_list)
821 struct ice_sw_fv_list_entry *fvl;
822 struct ice_sw_fv_list_entry *tmp;
823 struct ice_pkg_enum state;
824 struct ice_seg *ice_seg;
828 memset(&state, 0, sizeof(state));
830 if (!lkups->n_val_words || !hw->seg)
837 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
838 &offset, ice_sw_fv_handler);
843 /* If field vector is not in the bitmap list, then skip this
846 if (!test_bit((u16)offset, bm))
849 for (i = 0; i < lkups->n_val_words; i++) {
852 for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
853 if (fv->ew[j].prot_id ==
854 lkups->fv_words[i].prot_id &&
855 fv->ew[j].off == lkups->fv_words[i].off)
857 if (j >= hw->blk[ICE_BLK_SW].es.fvw)
859 if (i + 1 == lkups->n_val_words) {
860 fvl = devm_kzalloc(ice_hw_to_dev(hw),
861 sizeof(*fvl), GFP_KERNEL);
865 fvl->profile_id = offset;
866 list_add(&fvl->list_entry, fv_list);
871 if (list_empty(fv_list)) {
872 dev_warn(ice_hw_to_dev(hw),
873 "Required profiles not found in currently loaded DDP package");
880 list_for_each_entry_safe(fvl, tmp, fv_list, list_entry) {
881 list_del(&fvl->list_entry);
882 devm_kfree(ice_hw_to_dev(hw), fvl);
889 * ice_init_prof_result_bm - Initialize the profile result index bitmap
890 * @hw: pointer to hardware structure
892 void ice_init_prof_result_bm(struct ice_hw *hw)
894 struct ice_pkg_enum state;
895 struct ice_seg *ice_seg;
898 memset(&state, 0, sizeof(state));
908 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
909 &off, ice_sw_fv_handler);
914 bitmap_zero(hw->switch_info->prof_res_bm[off],
917 /* Determine empty field vector indices, these can be
918 * used for recipe results. Skip index 0, since it is
919 * always used for Switch ID.
921 for (i = 1; i < ICE_MAX_FV_WORDS; i++)
922 if (fv->ew[i].prot_id == ICE_PROT_INVALID &&
923 fv->ew[i].off == ICE_FV_OFFSET_INVAL)
924 set_bit(i, hw->switch_info->prof_res_bm[off]);
930 * @hw: pointer to the HW structure
931 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
933 * Frees a package buffer
935 void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
937 devm_kfree(ice_hw_to_dev(hw), bld);
941 * ice_pkg_buf_reserve_section
942 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
943 * @count: the number of sections to reserve
945 * Reserves one or more section table entries in a package buffer. This routine
946 * can be called multiple times as long as they are made before calling
947 * ice_pkg_buf_alloc_section(). Once ice_pkg_buf_alloc_section()
948 * is called once, the number of sections that can be allocated will not be able
949 * to be increased; not using all reserved sections is fine, but this will
950 * result in some wasted space in the buffer.
951 * Note: all package contents must be in Little Endian form.
953 int ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
955 struct ice_buf_hdr *buf;
962 buf = (struct ice_buf_hdr *)&bld->buf;
964 /* already an active section, can't increase table size */
965 section_count = le16_to_cpu(buf->section_count);
966 if (section_count > 0)
969 if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
971 bld->reserved_section_table_entries += count;
973 data_end = le16_to_cpu(buf->data_end) +
974 flex_array_size(buf, section_entry, count);
975 buf->data_end = cpu_to_le16(data_end);
981 * ice_pkg_buf_alloc_section
982 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
983 * @type: the section type value
984 * @size: the size of the section to reserve (in bytes)
986 * Reserves memory in the buffer for a section's content and updates the
987 * buffers' status accordingly. This routine returns a pointer to the first
988 * byte of the section start within the buffer, which is used to fill in the
990 * Note: all package contents must be in Little Endian form.
992 void *ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
994 struct ice_buf_hdr *buf;
998 if (!bld || !type || !size)
1001 buf = (struct ice_buf_hdr *)&bld->buf;
1003 /* check for enough space left in buffer */
1004 data_end = le16_to_cpu(buf->data_end);
1006 /* section start must align on 4 byte boundary */
1007 data_end = ALIGN(data_end, 4);
1009 if ((data_end + size) > ICE_MAX_S_DATA_END)
1012 /* check for more available section table entries */
1013 sect_count = le16_to_cpu(buf->section_count);
1014 if (sect_count < bld->reserved_section_table_entries) {
1015 void *section_ptr = ((u8 *)buf) + data_end;
1017 buf->section_entry[sect_count].offset = cpu_to_le16(data_end);
1018 buf->section_entry[sect_count].size = cpu_to_le16(size);
1019 buf->section_entry[sect_count].type = cpu_to_le32(type);
1022 buf->data_end = cpu_to_le16(data_end);
1024 buf->section_count = cpu_to_le16(sect_count + 1);
1028 /* no free section table entries */
1033 * ice_pkg_buf_alloc_single_section
1034 * @hw: pointer to the HW structure
1035 * @type: the section type value
1036 * @size: the size of the section to reserve (in bytes)
1037 * @section: returns pointer to the section
1039 * Allocates a package buffer with a single section.
1040 * Note: all package contents must be in Little Endian form.
1042 struct ice_buf_build *ice_pkg_buf_alloc_single_section(struct ice_hw *hw,
1046 struct ice_buf_build *buf;
1051 buf = ice_pkg_buf_alloc(hw);
1055 if (ice_pkg_buf_reserve_section(buf, 1))
1056 goto ice_pkg_buf_alloc_single_section_err;
1058 *section = ice_pkg_buf_alloc_section(buf, type, size);
1060 goto ice_pkg_buf_alloc_single_section_err;
1064 ice_pkg_buf_alloc_single_section_err:
1065 ice_pkg_buf_free(hw, buf);
1070 * ice_pkg_buf_get_active_sections
1071 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1073 * Returns the number of active sections. Before using the package buffer
1074 * in an update package command, the caller should make sure that there is at
1075 * least one active section - otherwise, the buffer is not legal and should
1077 * Note: all package contents must be in Little Endian form.
1079 u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
1081 struct ice_buf_hdr *buf;
1086 buf = (struct ice_buf_hdr *)&bld->buf;
1087 return le16_to_cpu(buf->section_count);
1092 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1094 * Return a pointer to the buffer's header
1096 struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
1104 static enum ice_ddp_state ice_map_aq_err_to_ddp_state(enum ice_aq_err aq_err)
1107 case ICE_AQ_RC_ENOSEC:
1108 case ICE_AQ_RC_EBADSIG:
1109 return ICE_DDP_PKG_FILE_SIGNATURE_INVALID;
1110 case ICE_AQ_RC_ESVN:
1111 return ICE_DDP_PKG_FILE_REVISION_TOO_LOW;
1112 case ICE_AQ_RC_EBADMAN:
1113 case ICE_AQ_RC_EBADBUF:
1114 return ICE_DDP_PKG_LOAD_ERROR;
1116 return ICE_DDP_PKG_ERR;
1121 * ice_acquire_global_cfg_lock
1122 * @hw: pointer to the HW structure
1123 * @access: access type (read or write)
1125 * This function will request ownership of the global config lock for reading
1126 * or writing of the package. When attempting to obtain write access, the
1127 * caller must check for the following two return values:
1129 * 0 - Means the caller has acquired the global config lock
1130 * and can perform writing of the package.
1131 * -EALREADY - Indicates another driver has already written the
1132 * package or has found that no update was necessary; in
1133 * this case, the caller can just skip performing any
1134 * update of the package.
1136 static int ice_acquire_global_cfg_lock(struct ice_hw *hw,
1137 enum ice_aq_res_access_type access)
1141 status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
1142 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
1145 mutex_lock(&ice_global_cfg_lock_sw);
1146 else if (status == -EALREADY)
1147 ice_debug(hw, ICE_DBG_PKG,
1148 "Global config lock: No work to do\n");
1154 * ice_release_global_cfg_lock
1155 * @hw: pointer to the HW structure
1157 * This function will release the global config lock.
1159 static void ice_release_global_cfg_lock(struct ice_hw *hw)
1161 mutex_unlock(&ice_global_cfg_lock_sw);
1162 ice_release_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID);
1166 * ice_aq_download_pkg
1167 * @hw: pointer to the hardware structure
1168 * @pkg_buf: the package buffer to transfer
1169 * @buf_size: the size of the package buffer
1170 * @last_buf: last buffer indicator
1171 * @error_offset: returns error offset
1172 * @error_info: returns error information
1173 * @cd: pointer to command details structure or NULL
1175 * Download Package (0x0C40)
1178 ice_aq_download_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1179 u16 buf_size, bool last_buf, u32 *error_offset,
1180 u32 *error_info, struct ice_sq_cd *cd)
1182 struct ice_aqc_download_pkg *cmd;
1183 struct ice_aq_desc desc;
1191 cmd = &desc.params.download_pkg;
1192 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_download_pkg);
1193 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1196 cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1198 status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1199 if (status == -EIO) {
1200 /* Read error from buffer only when the FW returned an error */
1201 struct ice_aqc_download_pkg_resp *resp;
1203 resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1205 *error_offset = le32_to_cpu(resp->error_offset);
1207 *error_info = le32_to_cpu(resp->error_info);
1214 * ice_is_buffer_metadata - determine if package buffer is a metadata buffer
1215 * @buf: pointer to buffer header
1216 * Return: whether given @buf is a metadata one.
1218 static bool ice_is_buffer_metadata(struct ice_buf_hdr *buf)
1220 return le32_to_cpu(buf->section_entry[0].type) & ICE_METADATA_BUF;
1224 * struct ice_ddp_send_ctx - sending context of current DDP segment
1225 * @hw: pointer to the hardware struct
1227 * Keeps current sending state (header, error) for the purpose of proper "last"
1228 * bit setting in ice_aq_download_pkg(). Use via calls to ice_ddp_send_hunk().
1230 struct ice_ddp_send_ctx {
1232 /* private: only for ice_ddp_send_hunk() */
1233 struct ice_buf_hdr *hdr;
1237 static void ice_ddp_send_ctx_set_err(struct ice_ddp_send_ctx *ctx, int err)
1243 * ice_ddp_send_hunk - send one hunk of data to FW
1244 * @ctx: current segment sending context
1245 * @hunk: next hunk to send, size is always ICE_PKG_BUF_SIZE
1247 * Send the next hunk of data to FW, retrying if needed.
1249 * Notice: must be called once more with a NULL @hunk to finish up; such call
1250 * will set up the "last" bit of an AQ request. After such call @ctx.hdr is
1251 * cleared, @hw is still valid.
1253 * Return: %ICE_DDP_PKG_SUCCESS if there were no problems; a sticky @err
1256 static enum ice_ddp_state ice_ddp_send_hunk(struct ice_ddp_send_ctx *ctx,
1257 struct ice_buf_hdr *hunk)
1259 struct ice_buf_hdr *prev_hunk = ctx->hdr;
1260 struct ice_hw *hw = ctx->hw;
1261 bool prev_was_last = !hunk;
1262 enum ice_aq_err aq_err;
1271 return ICE_DDP_PKG_SUCCESS; /* no problem so far */
1273 for (attempt = 0; attempt < 5; attempt++) {
1277 err = ice_aq_download_pkg(hw, prev_hunk, ICE_PKG_BUF_SIZE,
1278 prev_was_last, &offset, &info, NULL);
1280 aq_err = hw->adminq.sq_last_status;
1281 if (aq_err != ICE_AQ_RC_ENOSEC && aq_err != ICE_AQ_RC_EBADSIG)
1286 ice_debug(hw, ICE_DBG_PKG, "Pkg download failed: err %d off %d inf %d\n",
1288 ctx->err = ice_map_aq_err_to_ddp_state(aq_err);
1289 } else if (attempt) {
1290 dev_dbg(ice_hw_to_dev(hw),
1291 "ice_aq_download_pkg number of retries: %d\n", attempt);
1298 * ice_dwnld_cfg_bufs_no_lock
1299 * @ctx: context of the current buffers section to send
1300 * @bufs: pointer to an array of buffers
1301 * @start: buffer index of first buffer to download
1302 * @count: the number of buffers to download
1304 * Downloads package configuration buffers to the firmware. Metadata buffers
1305 * are skipped, and the first metadata buffer found indicates that the rest
1306 * of the buffers are all metadata buffers.
1308 static enum ice_ddp_state
1309 ice_dwnld_cfg_bufs_no_lock(struct ice_ddp_send_ctx *ctx, struct ice_buf *bufs,
1310 u32 start, u32 count)
1312 struct ice_buf_hdr *bh;
1313 enum ice_ddp_state err;
1315 if (!bufs || !count) {
1316 ice_ddp_send_ctx_set_err(ctx, ICE_DDP_PKG_ERR);
1317 return ICE_DDP_PKG_ERR;
1322 for (int i = 0; i < count; i++, bufs++) {
1323 bh = (struct ice_buf_hdr *)bufs;
1324 /* Metadata buffers should not be sent to FW,
1325 * their presence means "we are done here".
1327 if (ice_is_buffer_metadata(bh))
1330 err = ice_ddp_send_hunk(ctx, bh);
1339 * ice_get_pkg_seg_by_idx
1340 * @pkg_hdr: pointer to the package header to be searched
1341 * @idx: index of segment
1343 static struct ice_generic_seg_hdr *
1344 ice_get_pkg_seg_by_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx)
1346 if (idx < le32_to_cpu(pkg_hdr->seg_count))
1347 return (struct ice_generic_seg_hdr *)
1349 le32_to_cpu(pkg_hdr->seg_offset[idx]));
1355 * ice_is_signing_seg_at_idx - determine if segment is a signing segment
1356 * @pkg_hdr: pointer to package header
1357 * @idx: segment index
1359 static bool ice_is_signing_seg_at_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx)
1361 struct ice_generic_seg_hdr *seg;
1363 seg = ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1367 return le32_to_cpu(seg->seg_type) == SEGMENT_TYPE_SIGNING;
1371 * ice_is_signing_seg_type_at_idx
1372 * @pkg_hdr: pointer to package header
1373 * @idx: segment index
1374 * @seg_id: segment id that is expected
1375 * @sign_type: signing type
1377 * Determine if a segment is a signing segment of the correct type
1380 ice_is_signing_seg_type_at_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx,
1381 u32 seg_id, u32 sign_type)
1383 struct ice_sign_seg *seg;
1385 if (!ice_is_signing_seg_at_idx(pkg_hdr, idx))
1388 seg = (struct ice_sign_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1390 if (seg && le32_to_cpu(seg->seg_id) == seg_id &&
1391 le32_to_cpu(seg->sign_type) == sign_type)
1398 * ice_download_pkg_sig_seg - download a signature segment
1399 * @ctx: context of the current buffers section to send
1400 * @seg: pointer to signature segment
1402 static enum ice_ddp_state
1403 ice_download_pkg_sig_seg(struct ice_ddp_send_ctx *ctx, struct ice_sign_seg *seg)
1405 return ice_dwnld_cfg_bufs_no_lock(ctx, seg->buf_tbl.buf_array, 0,
1406 le32_to_cpu(seg->buf_tbl.buf_count));
1410 * ice_download_pkg_config_seg - download a config segment
1411 * @ctx: context of the current buffers section to send
1412 * @pkg_hdr: pointer to package header
1413 * @idx: segment index
1414 * @start: starting buffer
1415 * @count: buffer count
1417 * Note: idx must reference a ICE segment
1419 static enum ice_ddp_state
1420 ice_download_pkg_config_seg(struct ice_ddp_send_ctx *ctx,
1421 struct ice_pkg_hdr *pkg_hdr, u32 idx, u32 start,
1424 struct ice_buf_table *bufs;
1425 struct ice_seg *seg;
1428 seg = (struct ice_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1430 return ICE_DDP_PKG_ERR;
1432 bufs = ice_find_buf_table(seg);
1433 buf_count = le32_to_cpu(bufs->buf_count);
1435 if (start >= buf_count || start + count > buf_count)
1436 return ICE_DDP_PKG_ERR;
1438 return ice_dwnld_cfg_bufs_no_lock(ctx, bufs->buf_array, start, count);
1441 static bool ice_is_last_sign_seg(u32 flags)
1443 return !(flags & ICE_SIGN_SEG_FLAGS_VALID) || /* behavior prior to valid */
1444 (flags & ICE_SIGN_SEG_FLAGS_LAST);
1448 * ice_dwnld_sign_and_cfg_segs - download a signing segment and config segment
1449 * @ctx: context of the current buffers section to send
1450 * @pkg_hdr: pointer to package header
1451 * @idx: segment index (must be a signature segment)
1453 * Note: idx must reference a signature segment
1455 static enum ice_ddp_state
1456 ice_dwnld_sign_and_cfg_segs(struct ice_ddp_send_ctx *ctx,
1457 struct ice_pkg_hdr *pkg_hdr, u32 idx)
1459 u32 conf_idx, start, count, flags;
1460 enum ice_ddp_state state;
1461 struct ice_sign_seg *seg;
1463 seg = (struct ice_sign_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1465 state = ICE_DDP_PKG_ERR;
1466 ice_ddp_send_ctx_set_err(ctx, state);
1470 count = le32_to_cpu(seg->signed_buf_count);
1471 state = ice_download_pkg_sig_seg(ctx, seg);
1472 if (state || !count)
1475 conf_idx = le32_to_cpu(seg->signed_seg_idx);
1476 start = le32_to_cpu(seg->signed_buf_start);
1478 state = ice_download_pkg_config_seg(ctx, pkg_hdr, conf_idx, start,
1481 /* finish up by sending last hunk with "last" flag set if requested by
1484 flags = le32_to_cpu(seg->flags);
1485 if (ice_is_last_sign_seg(flags))
1486 state = ice_ddp_send_hunk(ctx, NULL);
1492 * ice_match_signing_seg - determine if a matching signing segment exists
1493 * @pkg_hdr: pointer to package header
1494 * @seg_id: segment id that is expected
1495 * @sign_type: signing type
1498 ice_match_signing_seg(struct ice_pkg_hdr *pkg_hdr, u32 seg_id, u32 sign_type)
1502 for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1503 if (ice_is_signing_seg_type_at_idx(pkg_hdr, i, seg_id,
1512 * ice_post_dwnld_pkg_actions - perform post download package actions
1513 * @hw: pointer to the hardware structure
1515 static enum ice_ddp_state
1516 ice_post_dwnld_pkg_actions(struct ice_hw *hw)
1520 status = ice_set_vlan_mode(hw);
1522 ice_debug(hw, ICE_DBG_PKG, "Failed to set VLAN mode: err %d\n",
1524 return ICE_DDP_PKG_ERR;
1527 return ICE_DDP_PKG_SUCCESS;
1531 * ice_download_pkg_with_sig_seg
1532 * @hw: pointer to the hardware structure
1533 * @pkg_hdr: pointer to package header
1535 * Handles the download of a complete package.
1537 static enum ice_ddp_state
1538 ice_download_pkg_with_sig_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1540 enum ice_aq_err aq_err = hw->adminq.sq_last_status;
1541 enum ice_ddp_state state = ICE_DDP_PKG_ERR;
1542 struct ice_ddp_send_ctx ctx = { .hw = hw };
1546 ice_debug(hw, ICE_DBG_INIT, "Segment ID %d\n", hw->pkg_seg_id);
1547 ice_debug(hw, ICE_DBG_INIT, "Signature type %d\n", hw->pkg_sign_type);
1549 status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1551 if (status == -EALREADY)
1552 state = ICE_DDP_PKG_ALREADY_LOADED;
1554 state = ice_map_aq_err_to_ddp_state(aq_err);
1558 for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1559 if (!ice_is_signing_seg_type_at_idx(pkg_hdr, i, hw->pkg_seg_id,
1563 state = ice_dwnld_sign_and_cfg_segs(&ctx, pkg_hdr, i);
1569 state = ice_post_dwnld_pkg_actions(hw);
1571 ice_release_global_cfg_lock(hw);
1577 * ice_dwnld_cfg_bufs
1578 * @hw: pointer to the hardware structure
1579 * @bufs: pointer to an array of buffers
1580 * @count: the number of buffers in the array
1582 * Obtains global config lock and downloads the package configuration buffers
1585 static enum ice_ddp_state
1586 ice_dwnld_cfg_bufs(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1588 struct ice_ddp_send_ctx ctx = { .hw = hw };
1589 enum ice_ddp_state state;
1590 struct ice_buf_hdr *bh;
1593 if (!bufs || !count)
1594 return ICE_DDP_PKG_ERR;
1596 /* If the first buffer's first section has its metadata bit set
1597 * then there are no buffers to be downloaded, and the operation is
1598 * considered a success.
1600 bh = (struct ice_buf_hdr *)bufs;
1601 if (ice_is_buffer_metadata(bh))
1602 return ICE_DDP_PKG_SUCCESS;
1604 status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1606 if (status == -EALREADY)
1607 return ICE_DDP_PKG_ALREADY_LOADED;
1608 return ice_map_aq_err_to_ddp_state(hw->adminq.sq_last_status);
1611 ice_dwnld_cfg_bufs_no_lock(&ctx, bufs, 0, count);
1612 /* finish up by sending last hunk with "last" flag set */
1613 state = ice_ddp_send_hunk(&ctx, NULL);
1615 state = ice_post_dwnld_pkg_actions(hw);
1617 ice_release_global_cfg_lock(hw);
1623 * ice_download_pkg_without_sig_seg
1624 * @hw: pointer to the hardware structure
1625 * @ice_seg: pointer to the segment of the package to be downloaded
1627 * Handles the download of a complete package without signature segment.
1629 static enum ice_ddp_state
1630 ice_download_pkg_without_sig_seg(struct ice_hw *hw, struct ice_seg *ice_seg)
1632 struct ice_buf_table *ice_buf_tbl;
1634 ice_debug(hw, ICE_DBG_PKG, "Segment format version: %d.%d.%d.%d\n",
1635 ice_seg->hdr.seg_format_ver.major,
1636 ice_seg->hdr.seg_format_ver.minor,
1637 ice_seg->hdr.seg_format_ver.update,
1638 ice_seg->hdr.seg_format_ver.draft);
1640 ice_debug(hw, ICE_DBG_PKG, "Seg: type 0x%X, size %d, name %s\n",
1641 le32_to_cpu(ice_seg->hdr.seg_type),
1642 le32_to_cpu(ice_seg->hdr.seg_size), ice_seg->hdr.seg_id);
1644 ice_buf_tbl = ice_find_buf_table(ice_seg);
1646 ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1647 le32_to_cpu(ice_buf_tbl->buf_count));
1649 return ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1650 le32_to_cpu(ice_buf_tbl->buf_count));
1655 * @hw: pointer to the hardware structure
1656 * @pkg_hdr: pointer to package header
1657 * @ice_seg: pointer to the segment of the package to be downloaded
1659 * Handles the download of a complete package.
1661 static enum ice_ddp_state
1662 ice_download_pkg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr,
1663 struct ice_seg *ice_seg)
1665 enum ice_ddp_state state;
1667 if (hw->pkg_has_signing_seg)
1668 state = ice_download_pkg_with_sig_seg(hw, pkg_hdr);
1670 state = ice_download_pkg_without_sig_seg(hw, ice_seg);
1672 ice_post_pkg_dwnld_vlan_mode_cfg(hw);
1678 * ice_aq_get_pkg_info_list
1679 * @hw: pointer to the hardware structure
1680 * @pkg_info: the buffer which will receive the information list
1681 * @buf_size: the size of the pkg_info information buffer
1682 * @cd: pointer to command details structure or NULL
1684 * Get Package Info List (0x0C43)
1686 static int ice_aq_get_pkg_info_list(struct ice_hw *hw,
1687 struct ice_aqc_get_pkg_info_resp *pkg_info,
1688 u16 buf_size, struct ice_sq_cd *cd)
1690 struct ice_aq_desc desc;
1692 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1694 return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1699 * @hw: pointer to the hardware structure
1700 * @pkg_buf: the package cmd buffer
1701 * @buf_size: the size of the package cmd buffer
1702 * @last_buf: last buffer indicator
1703 * @error_offset: returns error offset
1704 * @error_info: returns error information
1705 * @cd: pointer to command details structure or NULL
1707 * Update Package (0x0C42)
1709 static int ice_aq_update_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1710 u16 buf_size, bool last_buf, u32 *error_offset,
1711 u32 *error_info, struct ice_sq_cd *cd)
1713 struct ice_aqc_download_pkg *cmd;
1714 struct ice_aq_desc desc;
1722 cmd = &desc.params.download_pkg;
1723 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_pkg);
1724 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1727 cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1729 status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1730 if (status == -EIO) {
1731 /* Read error from buffer only when the FW returned an error */
1732 struct ice_aqc_download_pkg_resp *resp;
1734 resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1736 *error_offset = le32_to_cpu(resp->error_offset);
1738 *error_info = le32_to_cpu(resp->error_info);
1745 * ice_aq_upload_section
1746 * @hw: pointer to the hardware structure
1747 * @pkg_buf: the package buffer which will receive the section
1748 * @buf_size: the size of the package buffer
1749 * @cd: pointer to command details structure or NULL
1751 * Upload Section (0x0C41)
1753 int ice_aq_upload_section(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1754 u16 buf_size, struct ice_sq_cd *cd)
1756 struct ice_aq_desc desc;
1758 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_upload_section);
1759 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1761 return ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1765 * ice_update_pkg_no_lock
1766 * @hw: pointer to the hardware structure
1767 * @bufs: pointer to an array of buffers
1768 * @count: the number of buffers in the array
1770 int ice_update_pkg_no_lock(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1775 for (i = 0; i < count; i++) {
1776 struct ice_buf_hdr *bh = (struct ice_buf_hdr *)(bufs + i);
1777 bool last = ((i + 1) == count);
1780 status = ice_aq_update_pkg(hw, bh, le16_to_cpu(bh->data_end),
1781 last, &offset, &info, NULL);
1784 ice_debug(hw, ICE_DBG_PKG,
1785 "Update pkg failed: err %d off %d inf %d\n",
1786 status, offset, info);
1796 * @hw: pointer to the hardware structure
1797 * @bufs: pointer to an array of buffers
1798 * @count: the number of buffers in the array
1800 * Obtains change lock and updates package.
1802 int ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1806 status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
1810 status = ice_update_pkg_no_lock(hw, bufs, count);
1812 ice_release_change_lock(hw);
1818 * ice_find_seg_in_pkg
1819 * @hw: pointer to the hardware structure
1820 * @seg_type: the segment type to search for (i.e., SEGMENT_TYPE_CPK)
1821 * @pkg_hdr: pointer to the package header to be searched
1823 * This function searches a package file for a particular segment type. On
1824 * success it returns a pointer to the segment header, otherwise it will
1827 static const struct ice_generic_seg_hdr *
1828 ice_find_seg_in_pkg(struct ice_hw *hw, u32 seg_type,
1829 const struct ice_pkg_hdr *pkg_hdr)
1833 ice_debug(hw, ICE_DBG_PKG, "Package format version: %d.%d.%d.%d\n",
1834 pkg_hdr->pkg_format_ver.major, pkg_hdr->pkg_format_ver.minor,
1835 pkg_hdr->pkg_format_ver.update,
1836 pkg_hdr->pkg_format_ver.draft);
1838 /* Search all package segments for the requested segment type */
1839 for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1840 const struct ice_generic_seg_hdr *seg;
1842 seg = (void *)pkg_hdr + le32_to_cpu(pkg_hdr->seg_offset[i]);
1844 if (le32_to_cpu(seg->seg_type) == seg_type)
1852 * ice_has_signing_seg - determine if package has a signing segment
1853 * @hw: pointer to the hardware structure
1854 * @pkg_hdr: pointer to the driver's package hdr
1856 static bool ice_has_signing_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1858 struct ice_generic_seg_hdr *seg_hdr;
1860 seg_hdr = (struct ice_generic_seg_hdr *)
1861 ice_find_seg_in_pkg(hw, SEGMENT_TYPE_SIGNING, pkg_hdr);
1863 return seg_hdr ? true : false;
1867 * ice_get_pkg_segment_id - get correct package segment id, based on device
1868 * @mac_type: MAC type of the device
1870 static u32 ice_get_pkg_segment_id(enum ice_mac_type mac_type)
1876 seg_id = SEGMENT_TYPE_ICE_E830;
1878 case ICE_MAC_GENERIC:
1879 case ICE_MAC_GENERIC_3K_E825:
1881 seg_id = SEGMENT_TYPE_ICE_E810;
1889 * ice_get_pkg_sign_type - get package segment sign type, based on device
1890 * @mac_type: MAC type of the device
1892 static u32 ice_get_pkg_sign_type(enum ice_mac_type mac_type)
1898 sign_type = SEGMENT_SIGN_TYPE_RSA3K_SBB;
1900 case ICE_MAC_GENERIC_3K_E825:
1901 sign_type = SEGMENT_SIGN_TYPE_RSA3K_E825;
1903 case ICE_MAC_GENERIC:
1905 sign_type = SEGMENT_SIGN_TYPE_RSA2K;
1913 * ice_get_signing_req - get correct package requirements, based on device
1914 * @hw: pointer to the hardware structure
1916 static void ice_get_signing_req(struct ice_hw *hw)
1918 hw->pkg_seg_id = ice_get_pkg_segment_id(hw->mac_type);
1919 hw->pkg_sign_type = ice_get_pkg_sign_type(hw->mac_type);
1924 * @hw: pointer to the hardware structure
1925 * @pkg_hdr: pointer to the driver's package hdr
1927 * Saves off the package details into the HW structure.
1929 static enum ice_ddp_state ice_init_pkg_info(struct ice_hw *hw,
1930 struct ice_pkg_hdr *pkg_hdr)
1932 struct ice_generic_seg_hdr *seg_hdr;
1935 return ICE_DDP_PKG_ERR;
1937 hw->pkg_has_signing_seg = ice_has_signing_seg(hw, pkg_hdr);
1938 ice_get_signing_req(hw);
1940 ice_debug(hw, ICE_DBG_INIT, "Pkg using segment id: 0x%08X\n",
1943 seg_hdr = (struct ice_generic_seg_hdr *)
1944 ice_find_seg_in_pkg(hw, hw->pkg_seg_id, pkg_hdr);
1946 struct ice_meta_sect *meta;
1947 struct ice_pkg_enum state;
1949 memset(&state, 0, sizeof(state));
1951 /* Get package information from the Metadata Section */
1952 meta = ice_pkg_enum_section((struct ice_seg *)seg_hdr, &state,
1955 ice_debug(hw, ICE_DBG_INIT,
1956 "Did not find ice metadata section in package\n");
1957 return ICE_DDP_PKG_INVALID_FILE;
1960 hw->pkg_ver = meta->ver;
1961 memcpy(hw->pkg_name, meta->name, sizeof(meta->name));
1963 ice_debug(hw, ICE_DBG_PKG, "Pkg: %d.%d.%d.%d, %s\n",
1964 meta->ver.major, meta->ver.minor, meta->ver.update,
1965 meta->ver.draft, meta->name);
1967 hw->ice_seg_fmt_ver = seg_hdr->seg_format_ver;
1968 memcpy(hw->ice_seg_id, seg_hdr->seg_id, sizeof(hw->ice_seg_id));
1970 ice_debug(hw, ICE_DBG_PKG, "Ice Seg: %d.%d.%d.%d, %s\n",
1971 seg_hdr->seg_format_ver.major,
1972 seg_hdr->seg_format_ver.minor,
1973 seg_hdr->seg_format_ver.update,
1974 seg_hdr->seg_format_ver.draft, seg_hdr->seg_id);
1976 ice_debug(hw, ICE_DBG_INIT,
1977 "Did not find ice segment in driver package\n");
1978 return ICE_DDP_PKG_INVALID_FILE;
1981 return ICE_DDP_PKG_SUCCESS;
1986 * @hw: pointer to the hardware structure
1988 * Store details of the package currently loaded in HW into the HW structure.
1990 static enum ice_ddp_state ice_get_pkg_info(struct ice_hw *hw)
1992 DEFINE_RAW_FLEX(struct ice_aqc_get_pkg_info_resp, pkg_info, pkg_info,
1994 u16 size = __struct_size(pkg_info);
1997 if (ice_aq_get_pkg_info_list(hw, pkg_info, size, NULL))
1998 return ICE_DDP_PKG_ERR;
2000 for (i = 0; i < le32_to_cpu(pkg_info->count); i++) {
2001 #define ICE_PKG_FLAG_COUNT 4
2002 char flags[ICE_PKG_FLAG_COUNT + 1] = { 0 };
2005 if (pkg_info->pkg_info[i].is_active) {
2006 flags[place++] = 'A';
2007 hw->active_pkg_ver = pkg_info->pkg_info[i].ver;
2008 hw->active_track_id =
2009 le32_to_cpu(pkg_info->pkg_info[i].track_id);
2010 memcpy(hw->active_pkg_name, pkg_info->pkg_info[i].name,
2011 sizeof(pkg_info->pkg_info[i].name));
2012 hw->active_pkg_in_nvm = pkg_info->pkg_info[i].is_in_nvm;
2014 if (pkg_info->pkg_info[i].is_active_at_boot)
2015 flags[place++] = 'B';
2016 if (pkg_info->pkg_info[i].is_modified)
2017 flags[place++] = 'M';
2018 if (pkg_info->pkg_info[i].is_in_nvm)
2019 flags[place++] = 'N';
2021 ice_debug(hw, ICE_DBG_PKG, "Pkg[%d]: %d.%d.%d.%d,%s,%s\n", i,
2022 pkg_info->pkg_info[i].ver.major,
2023 pkg_info->pkg_info[i].ver.minor,
2024 pkg_info->pkg_info[i].ver.update,
2025 pkg_info->pkg_info[i].ver.draft,
2026 pkg_info->pkg_info[i].name, flags);
2029 return ICE_DDP_PKG_SUCCESS;
2033 * ice_chk_pkg_compat
2034 * @hw: pointer to the hardware structure
2035 * @ospkg: pointer to the package hdr
2036 * @seg: pointer to the package segment hdr
2038 * This function checks the package version compatibility with driver and NVM
2040 static enum ice_ddp_state ice_chk_pkg_compat(struct ice_hw *hw,
2041 struct ice_pkg_hdr *ospkg,
2042 struct ice_seg **seg)
2044 DEFINE_RAW_FLEX(struct ice_aqc_get_pkg_info_resp, pkg, pkg_info,
2046 u16 size = __struct_size(pkg);
2047 enum ice_ddp_state state;
2050 /* Check package version compatibility */
2051 state = ice_chk_pkg_version(&hw->pkg_ver);
2053 ice_debug(hw, ICE_DBG_INIT, "Package version check failed.\n");
2057 /* find ICE segment in given package */
2058 *seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, hw->pkg_seg_id,
2061 ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
2062 return ICE_DDP_PKG_INVALID_FILE;
2065 /* Check if FW is compatible with the OS package */
2066 if (ice_aq_get_pkg_info_list(hw, pkg, size, NULL))
2067 return ICE_DDP_PKG_LOAD_ERROR;
2069 for (i = 0; i < le32_to_cpu(pkg->count); i++) {
2070 /* loop till we find the NVM package */
2071 if (!pkg->pkg_info[i].is_in_nvm)
2073 if ((*seg)->hdr.seg_format_ver.major !=
2074 pkg->pkg_info[i].ver.major ||
2075 (*seg)->hdr.seg_format_ver.minor >
2076 pkg->pkg_info[i].ver.minor) {
2077 state = ICE_DDP_PKG_FW_MISMATCH;
2078 ice_debug(hw, ICE_DBG_INIT,
2079 "OS package is not compatible with NVM.\n");
2081 /* done processing NVM package so break */
2089 * ice_init_pkg_hints
2090 * @hw: pointer to the HW structure
2091 * @ice_seg: pointer to the segment of the package scan (non-NULL)
2093 * This function will scan the package and save off relevant information
2094 * (hints or metadata) for driver use. The ice_seg parameter must not be NULL
2095 * since the first call to ice_enum_labels requires a pointer to an actual
2096 * ice_seg structure.
2098 static void ice_init_pkg_hints(struct ice_hw *hw, struct ice_seg *ice_seg)
2100 struct ice_pkg_enum state;
2105 memset(&hw->tnl, 0, sizeof(hw->tnl));
2106 memset(&state, 0, sizeof(state));
2111 label_name = ice_enum_labels(ice_seg, ICE_SID_LBL_RXPARSER_TMEM, &state,
2114 while (label_name) {
2115 if (!strncmp(label_name, ICE_TNL_PRE, strlen(ICE_TNL_PRE)))
2116 /* check for a tunnel entry */
2117 ice_add_tunnel_hint(hw, label_name, val);
2119 /* check for a dvm mode entry */
2120 else if (!strncmp(label_name, ICE_DVM_PRE, strlen(ICE_DVM_PRE)))
2121 ice_add_dvm_hint(hw, val, true);
2123 /* check for a svm mode entry */
2124 else if (!strncmp(label_name, ICE_SVM_PRE, strlen(ICE_SVM_PRE)))
2125 ice_add_dvm_hint(hw, val, false);
2127 label_name = ice_enum_labels(NULL, 0, &state, &val);
2130 /* Cache the appropriate boost TCAM entry pointers for tunnels */
2131 for (i = 0; i < hw->tnl.count; i++) {
2132 ice_find_boost_entry(ice_seg, hw->tnl.tbl[i].boost_addr,
2133 &hw->tnl.tbl[i].boost_entry);
2134 if (hw->tnl.tbl[i].boost_entry) {
2135 hw->tnl.tbl[i].valid = true;
2136 if (hw->tnl.tbl[i].type < __TNL_TYPE_CNT)
2137 hw->tnl.valid_count[hw->tnl.tbl[i].type]++;
2141 /* Cache the appropriate boost TCAM entry pointers for DVM and SVM */
2142 for (i = 0; i < hw->dvm_upd.count; i++)
2143 ice_find_boost_entry(ice_seg, hw->dvm_upd.tbl[i].boost_addr,
2144 &hw->dvm_upd.tbl[i].boost_entry);
2148 * ice_fill_hw_ptype - fill the enabled PTYPE bit information
2149 * @hw: pointer to the HW structure
2151 static void ice_fill_hw_ptype(struct ice_hw *hw)
2153 struct ice_marker_ptype_tcam_entry *tcam;
2154 struct ice_seg *seg = hw->seg;
2155 struct ice_pkg_enum state;
2157 bitmap_zero(hw->hw_ptype, ICE_FLOW_PTYPE_MAX);
2161 memset(&state, 0, sizeof(state));
2164 tcam = ice_pkg_enum_entry(seg, &state,
2165 ICE_SID_RXPARSER_MARKER_PTYPE, NULL,
2166 ice_marker_ptype_tcam_handler);
2168 le16_to_cpu(tcam->addr) < ICE_MARKER_PTYPE_TCAM_ADDR_MAX &&
2169 le16_to_cpu(tcam->ptype) < ICE_FLOW_PTYPE_MAX)
2170 set_bit(le16_to_cpu(tcam->ptype), hw->hw_ptype);
2177 * ice_init_pkg - initialize/download package
2178 * @hw: pointer to the hardware structure
2179 * @buf: pointer to the package buffer
2180 * @len: size of the package buffer
2182 * This function initializes a package. The package contains HW tables
2183 * required to do packet processing. First, the function extracts package
2184 * information such as version. Then it finds the ice configuration segment
2185 * within the package; this function then saves a copy of the segment pointer
2186 * within the supplied package buffer. Next, the function will cache any hints
2187 * from the package, followed by downloading the package itself. Note, that if
2188 * a previous PF driver has already downloaded the package successfully, then
2189 * the current driver will not have to download the package again.
2191 * The local package contents will be used to query default behavior and to
2192 * update specific sections of the HW's version of the package (e.g. to update
2193 * the parse graph to understand new protocols).
2195 * This function stores a pointer to the package buffer memory, and it is
2196 * expected that the supplied buffer will not be freed immediately. If the
2197 * package buffer needs to be freed, such as when read from a file, use
2198 * ice_copy_and_init_pkg() instead of directly calling ice_init_pkg() in this
2201 enum ice_ddp_state ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
2203 bool already_loaded = false;
2204 enum ice_ddp_state state;
2205 struct ice_pkg_hdr *pkg;
2206 struct ice_seg *seg;
2209 return ICE_DDP_PKG_ERR;
2211 pkg = (struct ice_pkg_hdr *)buf;
2212 state = ice_verify_pkg(pkg, len);
2214 ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
2219 /* initialize package info */
2220 state = ice_init_pkg_info(hw, pkg);
2224 /* must be a matching segment */
2225 if (hw->pkg_has_signing_seg &&
2226 !ice_match_signing_seg(pkg, hw->pkg_seg_id, hw->pkg_sign_type))
2227 return ICE_DDP_PKG_ERR;
2229 /* before downloading the package, check package version for
2230 * compatibility with driver
2232 state = ice_chk_pkg_compat(hw, pkg, &seg);
2236 /* initialize package hints and then download package */
2237 ice_init_pkg_hints(hw, seg);
2238 state = ice_download_pkg(hw, pkg, seg);
2239 if (state == ICE_DDP_PKG_ALREADY_LOADED) {
2240 ice_debug(hw, ICE_DBG_INIT,
2241 "package previously loaded - no work.\n");
2242 already_loaded = true;
2245 /* Get information on the package currently loaded in HW, then make sure
2246 * the driver is compatible with this version.
2248 if (!state || state == ICE_DDP_PKG_ALREADY_LOADED) {
2249 state = ice_get_pkg_info(hw);
2251 state = ice_get_ddp_pkg_state(hw, already_loaded);
2254 if (ice_is_init_pkg_successful(state)) {
2256 /* on successful package download update other required
2257 * registers to support the package and fill HW tables
2258 * with package content.
2260 ice_init_pkg_regs(hw);
2261 ice_fill_blk_tbls(hw);
2262 ice_fill_hw_ptype(hw);
2263 ice_get_prof_index_max(hw);
2265 ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n", state);
2272 * ice_copy_and_init_pkg - initialize/download a copy of the package
2273 * @hw: pointer to the hardware structure
2274 * @buf: pointer to the package buffer
2275 * @len: size of the package buffer
2277 * This function copies the package buffer, and then calls ice_init_pkg() to
2278 * initialize the copied package contents.
2280 * The copying is necessary if the package buffer supplied is constant, or if
2281 * the memory may disappear shortly after calling this function.
2283 * If the package buffer resides in the data segment and can be modified, the
2284 * caller is free to use ice_init_pkg() instead of ice_copy_and_init_pkg().
2286 * However, if the package buffer needs to be copied first, such as when being
2287 * read from a file, the caller should use ice_copy_and_init_pkg().
2289 * This function will first copy the package buffer, before calling
2290 * ice_init_pkg(). The caller is free to immediately destroy the original
2291 * package buffer, as the new copy will be managed by this function and
2294 enum ice_ddp_state ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf,
2297 enum ice_ddp_state state;
2301 return ICE_DDP_PKG_ERR;
2303 buf_copy = devm_kmemdup(ice_hw_to_dev(hw), buf, len, GFP_KERNEL);
2305 state = ice_init_pkg(hw, buf_copy, len);
2306 if (!ice_is_init_pkg_successful(state)) {
2307 /* Free the copy, since we failed to initialize the package */
2308 devm_kfree(ice_hw_to_dev(hw), buf_copy);
2310 /* Track the copied pkg so we can free it later */
2311 hw->pkg_copy = buf_copy;
2319 * ice_get_set_tx_topo - get or set Tx topology
2320 * @hw: pointer to the HW struct
2321 * @buf: pointer to Tx topology buffer
2322 * @buf_size: buffer size
2323 * @cd: pointer to command details structure or NULL
2324 * @flags: pointer to descriptor flags
2325 * @set: 0-get, 1-set topology
2327 * The function will get or set Tx topology
2329 * Return: zero when set was successful, negative values otherwise.
2332 ice_get_set_tx_topo(struct ice_hw *hw, u8 *buf, u16 buf_size,
2333 struct ice_sq_cd *cd, u8 *flags, bool set)
2335 struct ice_aqc_get_set_tx_topo *cmd;
2336 struct ice_aq_desc desc;
2339 cmd = &desc.params.get_set_tx_topo;
2341 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_tx_topo);
2342 cmd->set_flags = ICE_AQC_TX_TOPO_FLAGS_ISSUED;
2343 /* requested to update a new topology, not a default topology */
2345 cmd->set_flags |= ICE_AQC_TX_TOPO_FLAGS_SRC_RAM |
2346 ICE_AQC_TX_TOPO_FLAGS_LOAD_NEW;
2348 if (ice_is_e825c(hw))
2349 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2351 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_tx_topo);
2352 cmd->get_flags = ICE_AQC_TX_TOPO_GET_RAM;
2355 if (!ice_is_e825c(hw))
2356 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2358 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2361 /* read the return flag values (first byte) for get operation */
2363 *flags = desc.params.get_set_tx_topo.set_flags;
2369 * ice_cfg_tx_topo - Initialize new Tx topology if available
2370 * @hw: pointer to the HW struct
2371 * @buf: pointer to Tx topology buffer
2374 * The function will apply the new Tx topology from the package buffer
2377 * Return: zero when update was successful, negative values otherwise.
2379 int ice_cfg_tx_topo(struct ice_hw *hw, const void *buf, u32 len)
2381 u8 *new_topo = NULL, *topo __free(kfree) = NULL;
2382 const struct ice_run_time_cfg_seg *seg;
2383 const struct ice_buf_hdr *section;
2384 const struct ice_pkg_hdr *pkg_hdr;
2385 enum ice_ddp_state state;
2386 u16 offset, size = 0;
2394 /* Does FW support new Tx topology mode ? */
2395 if (!hw->func_caps.common_cap.tx_sched_topo_comp_mode_en) {
2396 ice_debug(hw, ICE_DBG_INIT, "FW doesn't support compatibility mode\n");
2400 topo = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
2404 /* Get the current Tx topology flags */
2405 status = ice_get_set_tx_topo(hw, topo, ICE_AQ_MAX_BUF_LEN, NULL, &flags,
2409 ice_debug(hw, ICE_DBG_INIT, "Get current topology is failed\n");
2413 /* Is default topology already applied ? */
2414 if (!(flags & ICE_AQC_TX_TOPO_FLAGS_LOAD_NEW) &&
2415 hw->num_tx_sched_layers == ICE_SCHED_9_LAYERS) {
2416 ice_debug(hw, ICE_DBG_INIT, "Default topology already applied\n");
2420 /* Is new topology already applied ? */
2421 if ((flags & ICE_AQC_TX_TOPO_FLAGS_LOAD_NEW) &&
2422 hw->num_tx_sched_layers == ICE_SCHED_5_LAYERS) {
2423 ice_debug(hw, ICE_DBG_INIT, "New topology already applied\n");
2427 /* Setting topology already issued? */
2428 if (flags & ICE_AQC_TX_TOPO_FLAGS_ISSUED) {
2429 ice_debug(hw, ICE_DBG_INIT, "Update Tx topology was done by another PF\n");
2430 /* Add a small delay before exiting */
2435 /* Change the topology from new to default (5 to 9) */
2436 if (!(flags & ICE_AQC_TX_TOPO_FLAGS_LOAD_NEW) &&
2437 hw->num_tx_sched_layers == ICE_SCHED_5_LAYERS) {
2438 ice_debug(hw, ICE_DBG_INIT, "Change topology from 5 to 9 layers\n");
2442 pkg_hdr = (const struct ice_pkg_hdr *)buf;
2443 state = ice_verify_pkg(pkg_hdr, len);
2445 ice_debug(hw, ICE_DBG_INIT, "Failed to verify pkg (err: %d)\n",
2450 /* Find runtime configuration segment */
2451 seg = (const struct ice_run_time_cfg_seg *)
2452 ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE_RUN_TIME_CFG, pkg_hdr);
2454 ice_debug(hw, ICE_DBG_INIT, "5 layer topology segment is missing\n");
2458 if (le32_to_cpu(seg->buf_table.buf_count) < ICE_MIN_S_COUNT) {
2459 ice_debug(hw, ICE_DBG_INIT, "5 layer topology segment count(%d) is wrong\n",
2460 seg->buf_table.buf_count);
2464 section = ice_pkg_val_buf(seg->buf_table.buf_array);
2465 if (!section || le32_to_cpu(section->section_entry[0].type) !=
2466 ICE_SID_TX_5_LAYER_TOPO) {
2467 ice_debug(hw, ICE_DBG_INIT, "5 layer topology section type is wrong\n");
2471 size = le16_to_cpu(section->section_entry[0].size);
2472 offset = le16_to_cpu(section->section_entry[0].offset);
2473 if (size < ICE_MIN_S_SZ || size > ICE_MAX_S_SZ) {
2474 ice_debug(hw, ICE_DBG_INIT, "5 layer topology section size is wrong\n");
2478 /* Make sure the section fits in the buffer */
2479 if (offset + size > ICE_PKG_BUF_SIZE) {
2480 ice_debug(hw, ICE_DBG_INIT, "5 layer topology buffer > 4K\n");
2484 /* Get the new topology buffer, reuse current topo copy mem */
2485 static_assert(ICE_PKG_BUF_SIZE == ICE_AQ_MAX_BUF_LEN);
2487 memcpy(new_topo, (u8 *)section + offset, size);
2490 /* Acquire global lock to make sure that set topology issued
2493 status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, ICE_RES_WRITE,
2494 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
2496 ice_debug(hw, ICE_DBG_INIT, "Failed to acquire global lock\n");
2500 /* Check if reset was triggered already. */
2501 reg = rd32(hw, GLGEN_RSTAT);
2502 if (reg & GLGEN_RSTAT_DEVSTATE_M) {
2503 /* Reset is in progress, re-init the HW again */
2504 ice_debug(hw, ICE_DBG_INIT, "Reset is in progress. Layer topology might be applied already\n");
2505 ice_check_reset(hw);
2509 /* Set new topology */
2510 status = ice_get_set_tx_topo(hw, new_topo, size, NULL, NULL, true);
2512 ice_debug(hw, ICE_DBG_INIT, "Failed setting Tx topology\n");
2516 /* New topology is updated, delay 1 second before issuing the CORER */
2518 ice_reset(hw, ICE_RESET_CORER);
2519 /* CORER will clear the global lock, so no explicit call
2520 * required for release.