1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022, Intel Corporation. */
4 #include "ice_common.h"
8 /* For supporting double VLAN mode, it is necessary to enable or disable certain
9 * boost tcam entries. The metadata labels names that match the following
10 * prefixes will be saved to allow enabling double VLAN mode.
12 #define ICE_DVM_PRE "BOOST_MAC_VLAN_DVM" /* enable these entries */
13 #define ICE_SVM_PRE "BOOST_MAC_VLAN_SVM" /* disable these entries */
15 /* To support tunneling entries by PF, the package will append the PF number to
16 * the label; for example TNL_VXLAN_PF0, TNL_VXLAN_PF1, TNL_VXLAN_PF2, etc.
18 #define ICE_TNL_PRE "TNL_"
19 static const struct ice_tunnel_type_scan tnls[] = {
20 { TNL_VXLAN, "TNL_VXLAN_PF" },
21 { TNL_GENEVE, "TNL_GENEVE_PF" },
26 * ice_verify_pkg - verify package
27 * @pkg: pointer to the package buffer
28 * @len: size of the package buffer
30 * Verifies various attributes of the package file, including length, format
31 * version, and the requirement of at least one segment.
33 enum ice_ddp_state ice_verify_pkg(struct ice_pkg_hdr *pkg, u32 len)
38 if (len < struct_size(pkg, seg_offset, 1))
39 return ICE_DDP_PKG_INVALID_FILE;
41 if (pkg->pkg_format_ver.major != ICE_PKG_FMT_VER_MAJ ||
42 pkg->pkg_format_ver.minor != ICE_PKG_FMT_VER_MNR ||
43 pkg->pkg_format_ver.update != ICE_PKG_FMT_VER_UPD ||
44 pkg->pkg_format_ver.draft != ICE_PKG_FMT_VER_DFT)
45 return ICE_DDP_PKG_INVALID_FILE;
47 /* pkg must have at least one segment */
48 seg_count = le32_to_cpu(pkg->seg_count);
50 return ICE_DDP_PKG_INVALID_FILE;
52 /* make sure segment array fits in package length */
53 if (len < struct_size(pkg, seg_offset, seg_count))
54 return ICE_DDP_PKG_INVALID_FILE;
56 /* all segments must fit within length */
57 for (i = 0; i < seg_count; i++) {
58 u32 off = le32_to_cpu(pkg->seg_offset[i]);
59 struct ice_generic_seg_hdr *seg;
61 /* segment header must fit */
62 if (len < off + sizeof(*seg))
63 return ICE_DDP_PKG_INVALID_FILE;
65 seg = (struct ice_generic_seg_hdr *)((u8 *)pkg + off);
67 /* segment body must fit */
68 if (len < off + le32_to_cpu(seg->seg_size))
69 return ICE_DDP_PKG_INVALID_FILE;
72 return ICE_DDP_PKG_SUCCESS;
76 * ice_free_seg - free package segment pointer
77 * @hw: pointer to the hardware structure
79 * Frees the package segment pointer in the proper manner, depending on if the
80 * segment was allocated or just the passed in pointer was stored.
82 void ice_free_seg(struct ice_hw *hw)
85 devm_kfree(ice_hw_to_dev(hw), hw->pkg_copy);
93 * ice_chk_pkg_version - check package version for compatibility with driver
94 * @pkg_ver: pointer to a version structure to check
96 * Check to make sure that the package about to be downloaded is compatible with
97 * the driver. To be compatible, the major and minor components of the package
98 * version must match our ICE_PKG_SUPP_VER_MAJ and ICE_PKG_SUPP_VER_MNR
101 static enum ice_ddp_state ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
103 if (pkg_ver->major > ICE_PKG_SUPP_VER_MAJ ||
104 (pkg_ver->major == ICE_PKG_SUPP_VER_MAJ &&
105 pkg_ver->minor > ICE_PKG_SUPP_VER_MNR))
106 return ICE_DDP_PKG_FILE_VERSION_TOO_HIGH;
107 else if (pkg_ver->major < ICE_PKG_SUPP_VER_MAJ ||
108 (pkg_ver->major == ICE_PKG_SUPP_VER_MAJ &&
109 pkg_ver->minor < ICE_PKG_SUPP_VER_MNR))
110 return ICE_DDP_PKG_FILE_VERSION_TOO_LOW;
112 return ICE_DDP_PKG_SUCCESS;
117 * @buf: pointer to the ice buffer
119 * This helper function validates a buffer's header.
121 struct ice_buf_hdr *ice_pkg_val_buf(struct ice_buf *buf)
123 struct ice_buf_hdr *hdr;
127 hdr = (struct ice_buf_hdr *)buf->buf;
129 section_count = le16_to_cpu(hdr->section_count);
130 if (section_count < ICE_MIN_S_COUNT || section_count > ICE_MAX_S_COUNT)
133 data_end = le16_to_cpu(hdr->data_end);
134 if (data_end < ICE_MIN_S_DATA_END || data_end > ICE_MAX_S_DATA_END)
142 * @ice_seg: pointer to the ice segment
144 * Returns the address of the buffer table within the ice segment.
146 static struct ice_buf_table *ice_find_buf_table(struct ice_seg *ice_seg)
148 struct ice_nvm_table *nvms = (struct ice_nvm_table *)
149 (ice_seg->device_table + le32_to_cpu(ice_seg->device_table_count));
151 return (__force struct ice_buf_table *)(nvms->vers +
152 le32_to_cpu(nvms->table_count));
157 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
158 * @state: pointer to the enum state
160 * This function will enumerate all the buffers in the ice segment. The first
161 * call is made with the ice_seg parameter non-NULL; on subsequent calls,
162 * ice_seg is set to NULL which continues the enumeration. When the function
163 * returns a NULL pointer, then the end of the buffers has been reached, or an
164 * unexpected value has been detected (for example an invalid section count or
165 * an invalid buffer end value).
167 static struct ice_buf_hdr *ice_pkg_enum_buf(struct ice_seg *ice_seg,
168 struct ice_pkg_enum *state)
171 state->buf_table = ice_find_buf_table(ice_seg);
172 if (!state->buf_table)
176 return ice_pkg_val_buf(state->buf_table->buf_array);
179 if (++state->buf_idx < le32_to_cpu(state->buf_table->buf_count))
180 return ice_pkg_val_buf(state->buf_table->buf_array +
187 * ice_pkg_advance_sect
188 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
189 * @state: pointer to the enum state
191 * This helper function will advance the section within the ice segment,
192 * also advancing the buffer if needed.
194 static bool ice_pkg_advance_sect(struct ice_seg *ice_seg,
195 struct ice_pkg_enum *state)
197 if (!ice_seg && !state->buf)
200 if (!ice_seg && state->buf)
201 if (++state->sect_idx < le16_to_cpu(state->buf->section_count))
204 state->buf = ice_pkg_enum_buf(ice_seg, state);
208 /* start of new buffer, reset section index */
214 * ice_pkg_enum_section
215 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
216 * @state: pointer to the enum state
217 * @sect_type: section type to enumerate
219 * This function will enumerate all the sections of a particular type in the
220 * ice segment. The first call is made with the ice_seg parameter non-NULL;
221 * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
222 * When the function returns a NULL pointer, then the end of the matching
223 * sections has been reached.
225 void *ice_pkg_enum_section(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
231 state->type = sect_type;
233 if (!ice_pkg_advance_sect(ice_seg, state))
236 /* scan for next matching section */
237 while (state->buf->section_entry[state->sect_idx].type !=
238 cpu_to_le32(state->type))
239 if (!ice_pkg_advance_sect(NULL, state))
242 /* validate section */
243 offset = le16_to_cpu(state->buf->section_entry[state->sect_idx].offset);
244 if (offset < ICE_MIN_S_OFF || offset > ICE_MAX_S_OFF)
247 size = le16_to_cpu(state->buf->section_entry[state->sect_idx].size);
248 if (size < ICE_MIN_S_SZ || size > ICE_MAX_S_SZ)
251 /* make sure the section fits in the buffer */
252 if (offset + size > ICE_PKG_BUF_SIZE)
256 le32_to_cpu(state->buf->section_entry[state->sect_idx].type);
258 /* calc pointer to this section */
261 le16_to_cpu(state->buf->section_entry[state->sect_idx].offset);
268 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
269 * @state: pointer to the enum state
270 * @sect_type: section type to enumerate
271 * @offset: pointer to variable that receives the offset in the table (optional)
272 * @handler: function that handles access to the entries into the section type
274 * This function will enumerate all the entries in particular section type in
275 * the ice segment. The first call is made with the ice_seg parameter non-NULL;
276 * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
277 * When the function returns a NULL pointer, then the end of the entries has
280 * Since each section may have a different header and entry size, the handler
281 * function is needed to determine the number and location entries in each
284 * The offset parameter is optional, but should be used for sections that
285 * contain an offset for each section table. For such cases, the section handler
286 * function must return the appropriate offset + index to give the absolution
287 * offset for each entry. For example, if the base for a section's header
288 * indicates a base offset of 10, and the index for the entry is 2, then
289 * section handler function should set the offset to 10 + 2 = 12.
291 static void *ice_pkg_enum_entry(struct ice_seg *ice_seg,
292 struct ice_pkg_enum *state, u32 sect_type,
294 void *(*handler)(u32 sect_type, void *section,
295 u32 index, u32 *offset))
303 if (!ice_pkg_enum_section(ice_seg, state, sect_type))
306 state->entry_idx = 0;
307 state->handler = handler;
316 entry = state->handler(state->sect_type, state->sect, state->entry_idx,
319 /* end of a section, look for another section of this type */
320 if (!ice_pkg_enum_section(NULL, state, 0))
323 state->entry_idx = 0;
324 entry = state->handler(state->sect_type, state->sect,
325 state->entry_idx, offset);
333 * @sect_type: section type
334 * @section: pointer to section
335 * @index: index of the field vector entry to be returned
336 * @offset: ptr to variable that receives the offset in the field vector table
338 * This is a callback function that can be passed to ice_pkg_enum_entry.
339 * This function treats the given section as of type ice_sw_fv_section and
340 * enumerates offset field. "offset" is an index into the field vector table.
342 static void *ice_sw_fv_handler(u32 sect_type, void *section, u32 index,
345 struct ice_sw_fv_section *fv_section = section;
347 if (!section || sect_type != ICE_SID_FLD_VEC_SW)
349 if (index >= le16_to_cpu(fv_section->count))
352 /* "index" passed in to this function is relative to a given
353 * 4k block. To get to the true index into the field vector
354 * table need to add the relative index to the base_offset
355 * field of this section
357 *offset = le16_to_cpu(fv_section->base_offset) + index;
358 return fv_section->fv + index;
362 * ice_get_prof_index_max - get the max profile index for used profile
363 * @hw: pointer to the HW struct
365 * Calling this function will get the max profile index for used profile
366 * and store the index number in struct ice_switch_info *switch_info
367 * in HW for following use.
369 static int ice_get_prof_index_max(struct ice_hw *hw)
371 u16 prof_index = 0, j, max_prof_index = 0;
372 struct ice_pkg_enum state;
373 struct ice_seg *ice_seg;
378 memset(&state, 0, sizeof(state));
386 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
387 &offset, ice_sw_fv_handler);
392 /* in the profile that not be used, the prot_id is set to 0xff
393 * and the off is set to 0x1ff for all the field vectors.
395 for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
396 if (fv->ew[j].prot_id != ICE_PROT_INVALID ||
397 fv->ew[j].off != ICE_FV_OFFSET_INVAL)
399 if (flag && prof_index > max_prof_index)
400 max_prof_index = prof_index;
406 hw->switch_info->max_used_prof_index = max_prof_index;
412 * ice_get_ddp_pkg_state - get DDP pkg state after download
413 * @hw: pointer to the HW struct
414 * @already_loaded: indicates if pkg was already loaded onto the device
416 static enum ice_ddp_state ice_get_ddp_pkg_state(struct ice_hw *hw,
419 if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
420 hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
421 hw->pkg_ver.update == hw->active_pkg_ver.update &&
422 hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
423 !memcmp(hw->pkg_name, hw->active_pkg_name, sizeof(hw->pkg_name))) {
425 return ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED;
427 return ICE_DDP_PKG_SUCCESS;
428 } else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
429 hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
430 return ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED;
431 } else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
432 hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
433 return ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED;
435 return ICE_DDP_PKG_ERR;
440 * ice_init_pkg_regs - initialize additional package registers
441 * @hw: pointer to the hardware structure
443 static void ice_init_pkg_regs(struct ice_hw *hw)
445 #define ICE_SW_BLK_INP_MASK_L 0xFFFFFFFF
446 #define ICE_SW_BLK_INP_MASK_H 0x0000FFFF
447 #define ICE_SW_BLK_IDX 0
449 /* setup Switch block input mask, which is 48-bits in two parts */
450 wr32(hw, GL_PREEXT_L2_PMASK0(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_L);
451 wr32(hw, GL_PREEXT_L2_PMASK1(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_H);
455 * ice_marker_ptype_tcam_handler
456 * @sect_type: section type
457 * @section: pointer to section
458 * @index: index of the Marker PType TCAM entry to be returned
459 * @offset: pointer to receive absolute offset, always 0 for ptype TCAM sections
461 * This is a callback function that can be passed to ice_pkg_enum_entry.
462 * Handles enumeration of individual Marker PType TCAM entries.
464 static void *ice_marker_ptype_tcam_handler(u32 sect_type, void *section,
465 u32 index, u32 *offset)
467 struct ice_marker_ptype_tcam_section *marker_ptype;
469 if (sect_type != ICE_SID_RXPARSER_MARKER_PTYPE)
472 if (index > ICE_MAX_MARKER_PTYPE_TCAMS_IN_BUF)
478 marker_ptype = section;
479 if (index >= le16_to_cpu(marker_ptype->count))
482 return marker_ptype->tcam + index;
487 * @hw: pointer to the HW structure
488 * @val: value of the boost entry
489 * @enable: true if entry needs to be enabled, or false if needs to be disabled
491 static void ice_add_dvm_hint(struct ice_hw *hw, u16 val, bool enable)
493 if (hw->dvm_upd.count < ICE_DVM_MAX_ENTRIES) {
494 hw->dvm_upd.tbl[hw->dvm_upd.count].boost_addr = val;
495 hw->dvm_upd.tbl[hw->dvm_upd.count].enable = enable;
501 * ice_add_tunnel_hint
502 * @hw: pointer to the HW structure
503 * @label_name: label text
504 * @val: value of the tunnel port boost entry
506 static void ice_add_tunnel_hint(struct ice_hw *hw, char *label_name, u16 val)
508 if (hw->tnl.count < ICE_TUNNEL_MAX_ENTRIES) {
511 for (i = 0; tnls[i].type != TNL_LAST; i++) {
512 size_t len = strlen(tnls[i].label_prefix);
514 /* Look for matching label start, before continuing */
515 if (strncmp(label_name, tnls[i].label_prefix, len))
518 /* Make sure this label matches our PF. Note that the PF
519 * character ('0' - '7') will be located where our
520 * prefix string's null terminator is located.
522 if ((label_name[len] - '0') == hw->pf_id) {
523 hw->tnl.tbl[hw->tnl.count].type = tnls[i].type;
524 hw->tnl.tbl[hw->tnl.count].valid = false;
525 hw->tnl.tbl[hw->tnl.count].boost_addr = val;
526 hw->tnl.tbl[hw->tnl.count].port = 0;
535 * ice_label_enum_handler
536 * @sect_type: section type
537 * @section: pointer to section
538 * @index: index of the label entry to be returned
539 * @offset: pointer to receive absolute offset, always zero for label sections
541 * This is a callback function that can be passed to ice_pkg_enum_entry.
542 * Handles enumeration of individual label entries.
544 static void *ice_label_enum_handler(u32 __always_unused sect_type,
545 void *section, u32 index, u32 *offset)
547 struct ice_label_section *labels;
552 if (index > ICE_MAX_LABELS_IN_BUF)
559 if (index >= le16_to_cpu(labels->count))
562 return labels->label + index;
567 * @ice_seg: pointer to the ice segment (NULL on subsequent calls)
568 * @type: the section type that will contain the label (0 on subsequent calls)
569 * @state: ice_pkg_enum structure that will hold the state of the enumeration
570 * @value: pointer to a value that will return the label's value if found
572 * Enumerates a list of labels in the package. The caller will call
573 * ice_enum_labels(ice_seg, type, ...) to start the enumeration, then call
574 * ice_enum_labels(NULL, 0, ...) to continue. When the function returns a NULL
575 * the end of the list has been reached.
577 static char *ice_enum_labels(struct ice_seg *ice_seg, u32 type,
578 struct ice_pkg_enum *state, u16 *value)
580 struct ice_label *label;
582 /* Check for valid label section on first call */
583 if (type && !(type >= ICE_SID_LBL_FIRST && type <= ICE_SID_LBL_LAST))
586 label = ice_pkg_enum_entry(ice_seg, state, type, NULL,
587 ice_label_enum_handler);
591 *value = le16_to_cpu(label->value);
596 * ice_boost_tcam_handler
597 * @sect_type: section type
598 * @section: pointer to section
599 * @index: index of the boost TCAM entry to be returned
600 * @offset: pointer to receive absolute offset, always 0 for boost TCAM sections
602 * This is a callback function that can be passed to ice_pkg_enum_entry.
603 * Handles enumeration of individual boost TCAM entries.
605 static void *ice_boost_tcam_handler(u32 sect_type, void *section, u32 index,
608 struct ice_boost_tcam_section *boost;
613 if (sect_type != ICE_SID_RXPARSER_BOOST_TCAM)
616 if (index > ICE_MAX_BST_TCAMS_IN_BUF)
623 if (index >= le16_to_cpu(boost->count))
626 return boost->tcam + index;
630 * ice_find_boost_entry
631 * @ice_seg: pointer to the ice segment (non-NULL)
632 * @addr: Boost TCAM address of entry to search for
633 * @entry: returns pointer to the entry
635 * Finds a particular Boost TCAM entry and returns a pointer to that entry
636 * if it is found. The ice_seg parameter must not be NULL since the first call
637 * to ice_pkg_enum_entry requires a pointer to an actual ice_segment structure.
639 static int ice_find_boost_entry(struct ice_seg *ice_seg, u16 addr,
640 struct ice_boost_tcam_entry **entry)
642 struct ice_boost_tcam_entry *tcam;
643 struct ice_pkg_enum state;
645 memset(&state, 0, sizeof(state));
651 tcam = ice_pkg_enum_entry(ice_seg, &state,
652 ICE_SID_RXPARSER_BOOST_TCAM, NULL,
653 ice_boost_tcam_handler);
654 if (tcam && le16_to_cpu(tcam->addr) == addr) {
667 * ice_is_init_pkg_successful - check if DDP init was successful
668 * @state: state of the DDP pkg after download
670 bool ice_is_init_pkg_successful(enum ice_ddp_state state)
673 case ICE_DDP_PKG_SUCCESS:
674 case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
675 case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
684 * @hw: pointer to the HW structure
686 * Allocates a package buffer and returns a pointer to the buffer header.
687 * Note: all package contents must be in Little Endian form.
689 struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
691 struct ice_buf_build *bld;
692 struct ice_buf_hdr *buf;
694 bld = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*bld), GFP_KERNEL);
698 buf = (struct ice_buf_hdr *)bld;
700 cpu_to_le16(offsetof(struct ice_buf_hdr, section_entry));
704 static bool ice_is_gtp_u_profile(u16 prof_idx)
706 return (prof_idx >= ICE_PROFID_IPV6_GTPU_TEID &&
707 prof_idx <= ICE_PROFID_IPV6_GTPU_IPV6_TCP_INNER) ||
708 prof_idx == ICE_PROFID_IPV4_GTPU_TEID;
711 static bool ice_is_gtp_c_profile(u16 prof_idx)
714 case ICE_PROFID_IPV4_GTPC_TEID:
715 case ICE_PROFID_IPV4_GTPC_NO_TEID:
716 case ICE_PROFID_IPV6_GTPC_TEID:
717 case ICE_PROFID_IPV6_GTPC_NO_TEID:
725 * ice_get_sw_prof_type - determine switch profile type
726 * @hw: pointer to the HW structure
727 * @fv: pointer to the switch field vector
728 * @prof_idx: profile index to check
730 static enum ice_prof_type ice_get_sw_prof_type(struct ice_hw *hw,
731 struct ice_fv *fv, u32 prof_idx)
735 if (ice_is_gtp_c_profile(prof_idx))
736 return ICE_PROF_TUN_GTPC;
738 if (ice_is_gtp_u_profile(prof_idx))
739 return ICE_PROF_TUN_GTPU;
741 for (i = 0; i < hw->blk[ICE_BLK_SW].es.fvw; i++) {
742 /* UDP tunnel will have UDP_OF protocol ID and VNI offset */
743 if (fv->ew[i].prot_id == (u8)ICE_PROT_UDP_OF &&
744 fv->ew[i].off == ICE_VNI_OFFSET)
745 return ICE_PROF_TUN_UDP;
747 /* GRE tunnel will have GRE protocol */
748 if (fv->ew[i].prot_id == (u8)ICE_PROT_GRE_OF)
749 return ICE_PROF_TUN_GRE;
752 return ICE_PROF_NON_TUN;
756 * ice_get_sw_fv_bitmap - Get switch field vector bitmap based on profile type
757 * @hw: pointer to hardware structure
758 * @req_profs: type of profiles requested
759 * @bm: pointer to memory for returning the bitmap of field vectors
761 void ice_get_sw_fv_bitmap(struct ice_hw *hw, enum ice_prof_type req_profs,
764 struct ice_pkg_enum state;
765 struct ice_seg *ice_seg;
768 if (req_profs == ICE_PROF_ALL) {
769 bitmap_set(bm, 0, ICE_MAX_NUM_PROFILES);
773 memset(&state, 0, sizeof(state));
774 bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
777 enum ice_prof_type prof_type;
780 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
781 &offset, ice_sw_fv_handler);
785 /* Determine field vector type */
786 prof_type = ice_get_sw_prof_type(hw, fv, offset);
788 if (req_profs & prof_type)
789 set_bit((u16)offset, bm);
796 * @hw: pointer to the HW structure
797 * @lkups: list of protocol types
798 * @bm: bitmap of field vectors to consider
799 * @fv_list: Head of a list
801 * Finds all the field vector entries from switch block that contain
802 * a given protocol ID and offset and returns a list of structures of type
803 * "ice_sw_fv_list_entry". Every structure in the list has a field vector
804 * definition and profile ID information
805 * NOTE: The caller of the function is responsible for freeing the memory
806 * allocated for every list entry.
808 int ice_get_sw_fv_list(struct ice_hw *hw, struct ice_prot_lkup_ext *lkups,
809 unsigned long *bm, struct list_head *fv_list)
811 struct ice_sw_fv_list_entry *fvl;
812 struct ice_sw_fv_list_entry *tmp;
813 struct ice_pkg_enum state;
814 struct ice_seg *ice_seg;
818 memset(&state, 0, sizeof(state));
820 if (!lkups->n_val_words || !hw->seg)
827 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
828 &offset, ice_sw_fv_handler);
833 /* If field vector is not in the bitmap list, then skip this
836 if (!test_bit((u16)offset, bm))
839 for (i = 0; i < lkups->n_val_words; i++) {
842 for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
843 if (fv->ew[j].prot_id ==
844 lkups->fv_words[i].prot_id &&
845 fv->ew[j].off == lkups->fv_words[i].off)
847 if (j >= hw->blk[ICE_BLK_SW].es.fvw)
849 if (i + 1 == lkups->n_val_words) {
850 fvl = devm_kzalloc(ice_hw_to_dev(hw),
851 sizeof(*fvl), GFP_KERNEL);
855 fvl->profile_id = offset;
856 list_add(&fvl->list_entry, fv_list);
861 if (list_empty(fv_list)) {
862 dev_warn(ice_hw_to_dev(hw),
863 "Required profiles not found in currently loaded DDP package");
870 list_for_each_entry_safe(fvl, tmp, fv_list, list_entry) {
871 list_del(&fvl->list_entry);
872 devm_kfree(ice_hw_to_dev(hw), fvl);
879 * ice_init_prof_result_bm - Initialize the profile result index bitmap
880 * @hw: pointer to hardware structure
882 void ice_init_prof_result_bm(struct ice_hw *hw)
884 struct ice_pkg_enum state;
885 struct ice_seg *ice_seg;
888 memset(&state, 0, sizeof(state));
898 fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
899 &off, ice_sw_fv_handler);
904 bitmap_zero(hw->switch_info->prof_res_bm[off],
907 /* Determine empty field vector indices, these can be
908 * used for recipe results. Skip index 0, since it is
909 * always used for Switch ID.
911 for (i = 1; i < ICE_MAX_FV_WORDS; i++)
912 if (fv->ew[i].prot_id == ICE_PROT_INVALID &&
913 fv->ew[i].off == ICE_FV_OFFSET_INVAL)
914 set_bit(i, hw->switch_info->prof_res_bm[off]);
920 * @hw: pointer to the HW structure
921 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
923 * Frees a package buffer
925 void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
927 devm_kfree(ice_hw_to_dev(hw), bld);
931 * ice_pkg_buf_reserve_section
932 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
933 * @count: the number of sections to reserve
935 * Reserves one or more section table entries in a package buffer. This routine
936 * can be called multiple times as long as they are made before calling
937 * ice_pkg_buf_alloc_section(). Once ice_pkg_buf_alloc_section()
938 * is called once, the number of sections that can be allocated will not be able
939 * to be increased; not using all reserved sections is fine, but this will
940 * result in some wasted space in the buffer.
941 * Note: all package contents must be in Little Endian form.
943 int ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
945 struct ice_buf_hdr *buf;
952 buf = (struct ice_buf_hdr *)&bld->buf;
954 /* already an active section, can't increase table size */
955 section_count = le16_to_cpu(buf->section_count);
956 if (section_count > 0)
959 if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
961 bld->reserved_section_table_entries += count;
963 data_end = le16_to_cpu(buf->data_end) +
964 flex_array_size(buf, section_entry, count);
965 buf->data_end = cpu_to_le16(data_end);
971 * ice_pkg_buf_alloc_section
972 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
973 * @type: the section type value
974 * @size: the size of the section to reserve (in bytes)
976 * Reserves memory in the buffer for a section's content and updates the
977 * buffers' status accordingly. This routine returns a pointer to the first
978 * byte of the section start within the buffer, which is used to fill in the
980 * Note: all package contents must be in Little Endian form.
982 void *ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
984 struct ice_buf_hdr *buf;
988 if (!bld || !type || !size)
991 buf = (struct ice_buf_hdr *)&bld->buf;
993 /* check for enough space left in buffer */
994 data_end = le16_to_cpu(buf->data_end);
996 /* section start must align on 4 byte boundary */
997 data_end = ALIGN(data_end, 4);
999 if ((data_end + size) > ICE_MAX_S_DATA_END)
1002 /* check for more available section table entries */
1003 sect_count = le16_to_cpu(buf->section_count);
1004 if (sect_count < bld->reserved_section_table_entries) {
1005 void *section_ptr = ((u8 *)buf) + data_end;
1007 buf->section_entry[sect_count].offset = cpu_to_le16(data_end);
1008 buf->section_entry[sect_count].size = cpu_to_le16(size);
1009 buf->section_entry[sect_count].type = cpu_to_le32(type);
1012 buf->data_end = cpu_to_le16(data_end);
1014 buf->section_count = cpu_to_le16(sect_count + 1);
1018 /* no free section table entries */
1023 * ice_pkg_buf_alloc_single_section
1024 * @hw: pointer to the HW structure
1025 * @type: the section type value
1026 * @size: the size of the section to reserve (in bytes)
1027 * @section: returns pointer to the section
1029 * Allocates a package buffer with a single section.
1030 * Note: all package contents must be in Little Endian form.
1032 struct ice_buf_build *ice_pkg_buf_alloc_single_section(struct ice_hw *hw,
1036 struct ice_buf_build *buf;
1041 buf = ice_pkg_buf_alloc(hw);
1045 if (ice_pkg_buf_reserve_section(buf, 1))
1046 goto ice_pkg_buf_alloc_single_section_err;
1048 *section = ice_pkg_buf_alloc_section(buf, type, size);
1050 goto ice_pkg_buf_alloc_single_section_err;
1054 ice_pkg_buf_alloc_single_section_err:
1055 ice_pkg_buf_free(hw, buf);
1060 * ice_pkg_buf_get_active_sections
1061 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1063 * Returns the number of active sections. Before using the package buffer
1064 * in an update package command, the caller should make sure that there is at
1065 * least one active section - otherwise, the buffer is not legal and should
1067 * Note: all package contents must be in Little Endian form.
1069 u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
1071 struct ice_buf_hdr *buf;
1076 buf = (struct ice_buf_hdr *)&bld->buf;
1077 return le16_to_cpu(buf->section_count);
1082 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1084 * Return a pointer to the buffer's header
1086 struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
1094 static enum ice_ddp_state ice_map_aq_err_to_ddp_state(enum ice_aq_err aq_err)
1097 case ICE_AQ_RC_ENOSEC:
1098 case ICE_AQ_RC_EBADSIG:
1099 return ICE_DDP_PKG_FILE_SIGNATURE_INVALID;
1100 case ICE_AQ_RC_ESVN:
1101 return ICE_DDP_PKG_FILE_REVISION_TOO_LOW;
1102 case ICE_AQ_RC_EBADMAN:
1103 case ICE_AQ_RC_EBADBUF:
1104 return ICE_DDP_PKG_LOAD_ERROR;
1106 return ICE_DDP_PKG_ERR;
1111 * ice_acquire_global_cfg_lock
1112 * @hw: pointer to the HW structure
1113 * @access: access type (read or write)
1115 * This function will request ownership of the global config lock for reading
1116 * or writing of the package. When attempting to obtain write access, the
1117 * caller must check for the following two return values:
1119 * 0 - Means the caller has acquired the global config lock
1120 * and can perform writing of the package.
1121 * -EALREADY - Indicates another driver has already written the
1122 * package or has found that no update was necessary; in
1123 * this case, the caller can just skip performing any
1124 * update of the package.
1126 static int ice_acquire_global_cfg_lock(struct ice_hw *hw,
1127 enum ice_aq_res_access_type access)
1131 status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
1132 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
1135 mutex_lock(&ice_global_cfg_lock_sw);
1136 else if (status == -EALREADY)
1137 ice_debug(hw, ICE_DBG_PKG,
1138 "Global config lock: No work to do\n");
1144 * ice_release_global_cfg_lock
1145 * @hw: pointer to the HW structure
1147 * This function will release the global config lock.
1149 static void ice_release_global_cfg_lock(struct ice_hw *hw)
1151 mutex_unlock(&ice_global_cfg_lock_sw);
1152 ice_release_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID);
1156 * ice_dwnld_cfg_bufs
1157 * @hw: pointer to the hardware structure
1158 * @bufs: pointer to an array of buffers
1159 * @count: the number of buffers in the array
1161 * Obtains global config lock and downloads the package configuration buffers
1162 * to the firmware. Metadata buffers are skipped, and the first metadata buffer
1163 * found indicates that the rest of the buffers are all metadata buffers.
1165 static enum ice_ddp_state ice_dwnld_cfg_bufs(struct ice_hw *hw,
1166 struct ice_buf *bufs, u32 count)
1168 enum ice_ddp_state state = ICE_DDP_PKG_SUCCESS;
1169 struct ice_buf_hdr *bh;
1170 enum ice_aq_err err;
1171 u32 offset, info, i;
1174 if (!bufs || !count)
1175 return ICE_DDP_PKG_ERR;
1177 /* If the first buffer's first section has its metadata bit set
1178 * then there are no buffers to be downloaded, and the operation is
1179 * considered a success.
1181 bh = (struct ice_buf_hdr *)bufs;
1182 if (le32_to_cpu(bh->section_entry[0].type) & ICE_METADATA_BUF)
1183 return ICE_DDP_PKG_SUCCESS;
1185 status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1187 if (status == -EALREADY)
1188 return ICE_DDP_PKG_ALREADY_LOADED;
1189 return ice_map_aq_err_to_ddp_state(hw->adminq.sq_last_status);
1192 for (i = 0; i < count; i++) {
1193 bool last = ((i + 1) == count);
1196 /* check next buffer for metadata flag */
1197 bh = (struct ice_buf_hdr *)(bufs + i + 1);
1199 /* A set metadata flag in the next buffer will signal
1200 * that the current buffer will be the last buffer
1203 if (le16_to_cpu(bh->section_count))
1204 if (le32_to_cpu(bh->section_entry[0].type) &
1209 bh = (struct ice_buf_hdr *)(bufs + i);
1211 status = ice_aq_download_pkg(hw, bh, ICE_PKG_BUF_SIZE, last,
1212 &offset, &info, NULL);
1214 /* Save AQ status from download package */
1216 ice_debug(hw, ICE_DBG_PKG,
1217 "Pkg download failed: err %d off %d inf %d\n",
1218 status, offset, info);
1219 err = hw->adminq.sq_last_status;
1220 state = ice_map_aq_err_to_ddp_state(err);
1229 status = ice_set_vlan_mode(hw);
1231 ice_debug(hw, ICE_DBG_PKG,
1232 "Failed to set VLAN mode: err %d\n", status);
1235 ice_release_global_cfg_lock(hw);
1241 * ice_aq_get_pkg_info_list
1242 * @hw: pointer to the hardware structure
1243 * @pkg_info: the buffer which will receive the information list
1244 * @buf_size: the size of the pkg_info information buffer
1245 * @cd: pointer to command details structure or NULL
1247 * Get Package Info List (0x0C43)
1249 static int ice_aq_get_pkg_info_list(struct ice_hw *hw,
1250 struct ice_aqc_get_pkg_info_resp *pkg_info,
1251 u16 buf_size, struct ice_sq_cd *cd)
1253 struct ice_aq_desc desc;
1255 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1257 return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1262 * @hw: pointer to the hardware structure
1263 * @ice_seg: pointer to the segment of the package to be downloaded
1265 * Handles the download of a complete package.
1267 static enum ice_ddp_state ice_download_pkg(struct ice_hw *hw,
1268 struct ice_seg *ice_seg)
1270 struct ice_buf_table *ice_buf_tbl;
1273 ice_debug(hw, ICE_DBG_PKG, "Segment format version: %d.%d.%d.%d\n",
1274 ice_seg->hdr.seg_format_ver.major,
1275 ice_seg->hdr.seg_format_ver.minor,
1276 ice_seg->hdr.seg_format_ver.update,
1277 ice_seg->hdr.seg_format_ver.draft);
1279 ice_debug(hw, ICE_DBG_PKG, "Seg: type 0x%X, size %d, name %s\n",
1280 le32_to_cpu(ice_seg->hdr.seg_type),
1281 le32_to_cpu(ice_seg->hdr.seg_size), ice_seg->hdr.seg_id);
1283 ice_buf_tbl = ice_find_buf_table(ice_seg);
1285 ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1286 le32_to_cpu(ice_buf_tbl->buf_count));
1288 status = ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1289 le32_to_cpu(ice_buf_tbl->buf_count));
1291 ice_post_pkg_dwnld_vlan_mode_cfg(hw);
1297 * ice_aq_download_pkg
1298 * @hw: pointer to the hardware structure
1299 * @pkg_buf: the package buffer to transfer
1300 * @buf_size: the size of the package buffer
1301 * @last_buf: last buffer indicator
1302 * @error_offset: returns error offset
1303 * @error_info: returns error information
1304 * @cd: pointer to command details structure or NULL
1306 * Download Package (0x0C40)
1308 int ice_aq_download_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1309 u16 buf_size, bool last_buf, u32 *error_offset,
1310 u32 *error_info, struct ice_sq_cd *cd)
1312 struct ice_aqc_download_pkg *cmd;
1313 struct ice_aq_desc desc;
1321 cmd = &desc.params.download_pkg;
1322 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_download_pkg);
1323 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1326 cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1328 status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1329 if (status == -EIO) {
1330 /* Read error from buffer only when the FW returned an error */
1331 struct ice_aqc_download_pkg_resp *resp;
1333 resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1335 *error_offset = le32_to_cpu(resp->error_offset);
1337 *error_info = le32_to_cpu(resp->error_info);
1344 * ice_aq_upload_section
1345 * @hw: pointer to the hardware structure
1346 * @pkg_buf: the package buffer which will receive the section
1347 * @buf_size: the size of the package buffer
1348 * @cd: pointer to command details structure or NULL
1350 * Upload Section (0x0C41)
1352 int ice_aq_upload_section(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1353 u16 buf_size, struct ice_sq_cd *cd)
1355 struct ice_aq_desc desc;
1357 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_upload_section);
1358 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1360 return ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1365 * @hw: pointer to the hardware structure
1366 * @pkg_buf: the package cmd buffer
1367 * @buf_size: the size of the package cmd buffer
1368 * @last_buf: last buffer indicator
1369 * @error_offset: returns error offset
1370 * @error_info: returns error information
1371 * @cd: pointer to command details structure or NULL
1373 * Update Package (0x0C42)
1375 static int ice_aq_update_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1376 u16 buf_size, bool last_buf, u32 *error_offset,
1377 u32 *error_info, struct ice_sq_cd *cd)
1379 struct ice_aqc_download_pkg *cmd;
1380 struct ice_aq_desc desc;
1388 cmd = &desc.params.download_pkg;
1389 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_pkg);
1390 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1393 cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1395 status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1396 if (status == -EIO) {
1397 /* Read error from buffer only when the FW returned an error */
1398 struct ice_aqc_download_pkg_resp *resp;
1400 resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1402 *error_offset = le32_to_cpu(resp->error_offset);
1404 *error_info = le32_to_cpu(resp->error_info);
1411 * ice_update_pkg_no_lock
1412 * @hw: pointer to the hardware structure
1413 * @bufs: pointer to an array of buffers
1414 * @count: the number of buffers in the array
1416 int ice_update_pkg_no_lock(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1421 for (i = 0; i < count; i++) {
1422 struct ice_buf_hdr *bh = (struct ice_buf_hdr *)(bufs + i);
1423 bool last = ((i + 1) == count);
1426 status = ice_aq_update_pkg(hw, bh, le16_to_cpu(bh->data_end),
1427 last, &offset, &info, NULL);
1430 ice_debug(hw, ICE_DBG_PKG,
1431 "Update pkg failed: err %d off %d inf %d\n",
1432 status, offset, info);
1442 * @hw: pointer to the hardware structure
1443 * @bufs: pointer to an array of buffers
1444 * @count: the number of buffers in the array
1446 * Obtains change lock and updates package.
1448 int ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1452 status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
1456 status = ice_update_pkg_no_lock(hw, bufs, count);
1458 ice_release_change_lock(hw);
1464 * ice_find_seg_in_pkg
1465 * @hw: pointer to the hardware structure
1466 * @seg_type: the segment type to search for (i.e., SEGMENT_TYPE_CPK)
1467 * @pkg_hdr: pointer to the package header to be searched
1469 * This function searches a package file for a particular segment type. On
1470 * success it returns a pointer to the segment header, otherwise it will
1473 struct ice_generic_seg_hdr *ice_find_seg_in_pkg(struct ice_hw *hw, u32 seg_type,
1474 struct ice_pkg_hdr *pkg_hdr)
1478 ice_debug(hw, ICE_DBG_PKG, "Package format version: %d.%d.%d.%d\n",
1479 pkg_hdr->pkg_format_ver.major, pkg_hdr->pkg_format_ver.minor,
1480 pkg_hdr->pkg_format_ver.update,
1481 pkg_hdr->pkg_format_ver.draft);
1483 /* Search all package segments for the requested segment type */
1484 for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1485 struct ice_generic_seg_hdr *seg;
1487 seg = (struct ice_generic_seg_hdr
1489 le32_to_cpu(pkg_hdr->seg_offset[i]));
1491 if (le32_to_cpu(seg->seg_type) == seg_type)
1500 * @hw: pointer to the hardware structure
1501 * @pkg_hdr: pointer to the driver's package hdr
1503 * Saves off the package details into the HW structure.
1505 static enum ice_ddp_state ice_init_pkg_info(struct ice_hw *hw,
1506 struct ice_pkg_hdr *pkg_hdr)
1508 struct ice_generic_seg_hdr *seg_hdr;
1511 return ICE_DDP_PKG_ERR;
1513 seg_hdr = ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE, pkg_hdr);
1515 struct ice_meta_sect *meta;
1516 struct ice_pkg_enum state;
1518 memset(&state, 0, sizeof(state));
1520 /* Get package information from the Metadata Section */
1521 meta = ice_pkg_enum_section((struct ice_seg *)seg_hdr, &state,
1524 ice_debug(hw, ICE_DBG_INIT,
1525 "Did not find ice metadata section in package\n");
1526 return ICE_DDP_PKG_INVALID_FILE;
1529 hw->pkg_ver = meta->ver;
1530 memcpy(hw->pkg_name, meta->name, sizeof(meta->name));
1532 ice_debug(hw, ICE_DBG_PKG, "Pkg: %d.%d.%d.%d, %s\n",
1533 meta->ver.major, meta->ver.minor, meta->ver.update,
1534 meta->ver.draft, meta->name);
1536 hw->ice_seg_fmt_ver = seg_hdr->seg_format_ver;
1537 memcpy(hw->ice_seg_id, seg_hdr->seg_id, sizeof(hw->ice_seg_id));
1539 ice_debug(hw, ICE_DBG_PKG, "Ice Seg: %d.%d.%d.%d, %s\n",
1540 seg_hdr->seg_format_ver.major,
1541 seg_hdr->seg_format_ver.minor,
1542 seg_hdr->seg_format_ver.update,
1543 seg_hdr->seg_format_ver.draft, seg_hdr->seg_id);
1545 ice_debug(hw, ICE_DBG_INIT,
1546 "Did not find ice segment in driver package\n");
1547 return ICE_DDP_PKG_INVALID_FILE;
1550 return ICE_DDP_PKG_SUCCESS;
1555 * @hw: pointer to the hardware structure
1557 * Store details of the package currently loaded in HW into the HW structure.
1559 static enum ice_ddp_state ice_get_pkg_info(struct ice_hw *hw)
1561 enum ice_ddp_state state = ICE_DDP_PKG_SUCCESS;
1562 struct ice_aqc_get_pkg_info_resp *pkg_info;
1566 size = struct_size(pkg_info, pkg_info, ICE_PKG_CNT);
1567 pkg_info = kzalloc(size, GFP_KERNEL);
1569 return ICE_DDP_PKG_ERR;
1571 if (ice_aq_get_pkg_info_list(hw, pkg_info, size, NULL)) {
1572 state = ICE_DDP_PKG_ERR;
1573 goto init_pkg_free_alloc;
1576 for (i = 0; i < le32_to_cpu(pkg_info->count); i++) {
1577 #define ICE_PKG_FLAG_COUNT 4
1578 char flags[ICE_PKG_FLAG_COUNT + 1] = { 0 };
1581 if (pkg_info->pkg_info[i].is_active) {
1582 flags[place++] = 'A';
1583 hw->active_pkg_ver = pkg_info->pkg_info[i].ver;
1584 hw->active_track_id =
1585 le32_to_cpu(pkg_info->pkg_info[i].track_id);
1586 memcpy(hw->active_pkg_name, pkg_info->pkg_info[i].name,
1587 sizeof(pkg_info->pkg_info[i].name));
1588 hw->active_pkg_in_nvm = pkg_info->pkg_info[i].is_in_nvm;
1590 if (pkg_info->pkg_info[i].is_active_at_boot)
1591 flags[place++] = 'B';
1592 if (pkg_info->pkg_info[i].is_modified)
1593 flags[place++] = 'M';
1594 if (pkg_info->pkg_info[i].is_in_nvm)
1595 flags[place++] = 'N';
1597 ice_debug(hw, ICE_DBG_PKG, "Pkg[%d]: %d.%d.%d.%d,%s,%s\n", i,
1598 pkg_info->pkg_info[i].ver.major,
1599 pkg_info->pkg_info[i].ver.minor,
1600 pkg_info->pkg_info[i].ver.update,
1601 pkg_info->pkg_info[i].ver.draft,
1602 pkg_info->pkg_info[i].name, flags);
1605 init_pkg_free_alloc:
1612 * ice_chk_pkg_compat
1613 * @hw: pointer to the hardware structure
1614 * @ospkg: pointer to the package hdr
1615 * @seg: pointer to the package segment hdr
1617 * This function checks the package version compatibility with driver and NVM
1619 static enum ice_ddp_state ice_chk_pkg_compat(struct ice_hw *hw,
1620 struct ice_pkg_hdr *ospkg,
1621 struct ice_seg **seg)
1623 struct ice_aqc_get_pkg_info_resp *pkg;
1624 enum ice_ddp_state state;
1628 /* Check package version compatibility */
1629 state = ice_chk_pkg_version(&hw->pkg_ver);
1631 ice_debug(hw, ICE_DBG_INIT, "Package version check failed.\n");
1635 /* find ICE segment in given package */
1636 *seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE,
1639 ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
1640 return ICE_DDP_PKG_INVALID_FILE;
1643 /* Check if FW is compatible with the OS package */
1644 size = struct_size(pkg, pkg_info, ICE_PKG_CNT);
1645 pkg = kzalloc(size, GFP_KERNEL);
1647 return ICE_DDP_PKG_ERR;
1649 if (ice_aq_get_pkg_info_list(hw, pkg, size, NULL)) {
1650 state = ICE_DDP_PKG_LOAD_ERROR;
1651 goto fw_ddp_compat_free_alloc;
1654 for (i = 0; i < le32_to_cpu(pkg->count); i++) {
1655 /* loop till we find the NVM package */
1656 if (!pkg->pkg_info[i].is_in_nvm)
1658 if ((*seg)->hdr.seg_format_ver.major !=
1659 pkg->pkg_info[i].ver.major ||
1660 (*seg)->hdr.seg_format_ver.minor >
1661 pkg->pkg_info[i].ver.minor) {
1662 state = ICE_DDP_PKG_FW_MISMATCH;
1663 ice_debug(hw, ICE_DBG_INIT,
1664 "OS package is not compatible with NVM.\n");
1666 /* done processing NVM package so break */
1669 fw_ddp_compat_free_alloc:
1675 * ice_init_pkg_hints
1676 * @hw: pointer to the HW structure
1677 * @ice_seg: pointer to the segment of the package scan (non-NULL)
1679 * This function will scan the package and save off relevant information
1680 * (hints or metadata) for driver use. The ice_seg parameter must not be NULL
1681 * since the first call to ice_enum_labels requires a pointer to an actual
1682 * ice_seg structure.
1684 static void ice_init_pkg_hints(struct ice_hw *hw, struct ice_seg *ice_seg)
1686 struct ice_pkg_enum state;
1691 memset(&hw->tnl, 0, sizeof(hw->tnl));
1692 memset(&state, 0, sizeof(state));
1697 label_name = ice_enum_labels(ice_seg, ICE_SID_LBL_RXPARSER_TMEM, &state,
1700 while (label_name) {
1701 if (!strncmp(label_name, ICE_TNL_PRE, strlen(ICE_TNL_PRE)))
1702 /* check for a tunnel entry */
1703 ice_add_tunnel_hint(hw, label_name, val);
1705 /* check for a dvm mode entry */
1706 else if (!strncmp(label_name, ICE_DVM_PRE, strlen(ICE_DVM_PRE)))
1707 ice_add_dvm_hint(hw, val, true);
1709 /* check for a svm mode entry */
1710 else if (!strncmp(label_name, ICE_SVM_PRE, strlen(ICE_SVM_PRE)))
1711 ice_add_dvm_hint(hw, val, false);
1713 label_name = ice_enum_labels(NULL, 0, &state, &val);
1716 /* Cache the appropriate boost TCAM entry pointers for tunnels */
1717 for (i = 0; i < hw->tnl.count; i++) {
1718 ice_find_boost_entry(ice_seg, hw->tnl.tbl[i].boost_addr,
1719 &hw->tnl.tbl[i].boost_entry);
1720 if (hw->tnl.tbl[i].boost_entry) {
1721 hw->tnl.tbl[i].valid = true;
1722 if (hw->tnl.tbl[i].type < __TNL_TYPE_CNT)
1723 hw->tnl.valid_count[hw->tnl.tbl[i].type]++;
1727 /* Cache the appropriate boost TCAM entry pointers for DVM and SVM */
1728 for (i = 0; i < hw->dvm_upd.count; i++)
1729 ice_find_boost_entry(ice_seg, hw->dvm_upd.tbl[i].boost_addr,
1730 &hw->dvm_upd.tbl[i].boost_entry);
1734 * ice_fill_hw_ptype - fill the enabled PTYPE bit information
1735 * @hw: pointer to the HW structure
1737 static void ice_fill_hw_ptype(struct ice_hw *hw)
1739 struct ice_marker_ptype_tcam_entry *tcam;
1740 struct ice_seg *seg = hw->seg;
1741 struct ice_pkg_enum state;
1743 bitmap_zero(hw->hw_ptype, ICE_FLOW_PTYPE_MAX);
1747 memset(&state, 0, sizeof(state));
1750 tcam = ice_pkg_enum_entry(seg, &state,
1751 ICE_SID_RXPARSER_MARKER_PTYPE, NULL,
1752 ice_marker_ptype_tcam_handler);
1754 le16_to_cpu(tcam->addr) < ICE_MARKER_PTYPE_TCAM_ADDR_MAX &&
1755 le16_to_cpu(tcam->ptype) < ICE_FLOW_PTYPE_MAX)
1756 set_bit(le16_to_cpu(tcam->ptype), hw->hw_ptype);
1763 * ice_init_pkg - initialize/download package
1764 * @hw: pointer to the hardware structure
1765 * @buf: pointer to the package buffer
1766 * @len: size of the package buffer
1768 * This function initializes a package. The package contains HW tables
1769 * required to do packet processing. First, the function extracts package
1770 * information such as version. Then it finds the ice configuration segment
1771 * within the package; this function then saves a copy of the segment pointer
1772 * within the supplied package buffer. Next, the function will cache any hints
1773 * from the package, followed by downloading the package itself. Note, that if
1774 * a previous PF driver has already downloaded the package successfully, then
1775 * the current driver will not have to download the package again.
1777 * The local package contents will be used to query default behavior and to
1778 * update specific sections of the HW's version of the package (e.g. to update
1779 * the parse graph to understand new protocols).
1781 * This function stores a pointer to the package buffer memory, and it is
1782 * expected that the supplied buffer will not be freed immediately. If the
1783 * package buffer needs to be freed, such as when read from a file, use
1784 * ice_copy_and_init_pkg() instead of directly calling ice_init_pkg() in this
1787 enum ice_ddp_state ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
1789 bool already_loaded = false;
1790 enum ice_ddp_state state;
1791 struct ice_pkg_hdr *pkg;
1792 struct ice_seg *seg;
1795 return ICE_DDP_PKG_ERR;
1797 pkg = (struct ice_pkg_hdr *)buf;
1798 state = ice_verify_pkg(pkg, len);
1800 ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
1805 /* initialize package info */
1806 state = ice_init_pkg_info(hw, pkg);
1810 /* before downloading the package, check package version for
1811 * compatibility with driver
1813 state = ice_chk_pkg_compat(hw, pkg, &seg);
1817 /* initialize package hints and then download package */
1818 ice_init_pkg_hints(hw, seg);
1819 state = ice_download_pkg(hw, seg);
1820 if (state == ICE_DDP_PKG_ALREADY_LOADED) {
1821 ice_debug(hw, ICE_DBG_INIT,
1822 "package previously loaded - no work.\n");
1823 already_loaded = true;
1826 /* Get information on the package currently loaded in HW, then make sure
1827 * the driver is compatible with this version.
1829 if (!state || state == ICE_DDP_PKG_ALREADY_LOADED) {
1830 state = ice_get_pkg_info(hw);
1832 state = ice_get_ddp_pkg_state(hw, already_loaded);
1835 if (ice_is_init_pkg_successful(state)) {
1837 /* on successful package download update other required
1838 * registers to support the package and fill HW tables
1839 * with package content.
1841 ice_init_pkg_regs(hw);
1842 ice_fill_blk_tbls(hw);
1843 ice_fill_hw_ptype(hw);
1844 ice_get_prof_index_max(hw);
1846 ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n", state);
1853 * ice_copy_and_init_pkg - initialize/download a copy of the package
1854 * @hw: pointer to the hardware structure
1855 * @buf: pointer to the package buffer
1856 * @len: size of the package buffer
1858 * This function copies the package buffer, and then calls ice_init_pkg() to
1859 * initialize the copied package contents.
1861 * The copying is necessary if the package buffer supplied is constant, or if
1862 * the memory may disappear shortly after calling this function.
1864 * If the package buffer resides in the data segment and can be modified, the
1865 * caller is free to use ice_init_pkg() instead of ice_copy_and_init_pkg().
1867 * However, if the package buffer needs to be copied first, such as when being
1868 * read from a file, the caller should use ice_copy_and_init_pkg().
1870 * This function will first copy the package buffer, before calling
1871 * ice_init_pkg(). The caller is free to immediately destroy the original
1872 * package buffer, as the new copy will be managed by this function and
1875 enum ice_ddp_state ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf,
1878 enum ice_ddp_state state;
1882 return ICE_DDP_PKG_ERR;
1884 buf_copy = devm_kmemdup(ice_hw_to_dev(hw), buf, len, GFP_KERNEL);
1886 state = ice_init_pkg(hw, buf_copy, len);
1887 if (!ice_is_init_pkg_successful(state)) {
1888 /* Free the copy, since we failed to initialize the package */
1889 devm_kfree(ice_hw_to_dev(hw), buf_copy);
1891 /* Track the copied pkg so we can free it later */
1892 hw->pkg_copy = buf_copy;