Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2022, Intel Corporation. */
   3
   4#include "ice_common.h"
   5#include "ice.h"
   6#include "ice_ddp.h"
   7#include "ice_sched.h"
   8
   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.
  12 */
  13#define ICE_DVM_PRE "BOOST_MAC_VLAN_DVM" /* enable these entries */
  14#define ICE_SVM_PRE "BOOST_MAC_VLAN_SVM" /* disable these entries */
  15
  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.
  18 */
  19#define ICE_TNL_PRE "TNL_"
  20static const struct ice_tunnel_type_scan tnls[] = {
  21	{ TNL_VXLAN, "TNL_VXLAN_PF" },
  22	{ TNL_GENEVE, "TNL_GENEVE_PF" },
  23	{ TNL_LAST, "" }
  24};
  25
  26/**
  27 * ice_verify_pkg - verify package
  28 * @pkg: pointer to the package buffer
  29 * @len: size of the package buffer
  30 *
  31 * Verifies various attributes of the package file, including length, format
  32 * version, and the requirement of at least one segment.
  33 */
  34static enum ice_ddp_state ice_verify_pkg(const struct ice_pkg_hdr *pkg, u32 len)
  35{
  36	u32 seg_count;
  37	u32 i;
  38
  39	if (len < struct_size(pkg, seg_offset, 1))
  40		return ICE_DDP_PKG_INVALID_FILE;
  41
  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;
  47
  48	/* pkg must have at least one segment */
  49	seg_count = le32_to_cpu(pkg->seg_count);
  50	if (seg_count < 1)
  51		return ICE_DDP_PKG_INVALID_FILE;
  52
  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;
  56
  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;
  61
  62		/* segment header must fit */
  63		if (len < off + sizeof(*seg))
  64			return ICE_DDP_PKG_INVALID_FILE;
  65
  66		seg = (void *)pkg + off;
  67
  68		/* segment body must fit */
  69		if (len < off + le32_to_cpu(seg->seg_size))
  70			return ICE_DDP_PKG_INVALID_FILE;
  71	}
  72
  73	return ICE_DDP_PKG_SUCCESS;
  74}
  75
  76/**
  77 * ice_free_seg - free package segment pointer
  78 * @hw: pointer to the hardware structure
  79 *
  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.
  82 */
  83void ice_free_seg(struct ice_hw *hw)
  84{
  85	if (hw->pkg_copy) {
  86		devm_kfree(ice_hw_to_dev(hw), hw->pkg_copy);
  87		hw->pkg_copy = NULL;
  88		hw->pkg_size = 0;
  89	}
  90	hw->seg = NULL;
  91}
  92
  93/**
  94 * ice_chk_pkg_version - check package version for compatibility with driver
  95 * @pkg_ver: pointer to a version structure to check
  96 *
  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
 100 * definitions.
 101 */
 102static enum ice_ddp_state ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
 103{
 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;
 112
 113	return ICE_DDP_PKG_SUCCESS;
 114}
 115
 116/**
 117 * ice_pkg_val_buf
 118 * @buf: pointer to the ice buffer
 119 *
 120 * This helper function validates a buffer's header.
 121 */
 122static const struct ice_buf_hdr *ice_pkg_val_buf(const struct ice_buf *buf)
 123{
 124	const struct ice_buf_hdr *hdr;
 125	u16 section_count;
 126	u16 data_end;
 127
 128	hdr = (const struct ice_buf_hdr *)buf->buf;
 129	/* verify data */
 130	section_count = le16_to_cpu(hdr->section_count);
 131	if (section_count < ICE_MIN_S_COUNT || section_count > ICE_MAX_S_COUNT)
 132		return NULL;
 133
 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)
 136		return NULL;
 137
 138	return hdr;
 139}
 140
 141/**
 142 * ice_find_buf_table
 143 * @ice_seg: pointer to the ice segment
 144 *
 145 * Returns the address of the buffer table within the ice segment.
 146 */
 147static struct ice_buf_table *ice_find_buf_table(struct ice_seg *ice_seg)
 148{
 149	struct ice_nvm_table *nvms = (struct ice_nvm_table *)
 150		(ice_seg->device_table + le32_to_cpu(ice_seg->device_table_count));
 151
 152	return (__force struct ice_buf_table *)(nvms->vers +
 153						le32_to_cpu(nvms->table_count));
 154}
 155
 156/**
 157 * ice_pkg_enum_buf
 158 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
 159 * @state: pointer to the enum state
 160 *
 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).
 167 */
 168static const struct ice_buf_hdr *ice_pkg_enum_buf(struct ice_seg *ice_seg,
 169						  struct ice_pkg_enum *state)
 170{
 171	if (ice_seg) {
 172		state->buf_table = ice_find_buf_table(ice_seg);
 173		if (!state->buf_table)
 174			return NULL;
 175
 176		state->buf_idx = 0;
 177		return ice_pkg_val_buf(state->buf_table->buf_array);
 178	}
 179
 180	if (++state->buf_idx < le32_to_cpu(state->buf_table->buf_count))
 181		return ice_pkg_val_buf(state->buf_table->buf_array +
 182				       state->buf_idx);
 183	else
 184		return NULL;
 185}
 186
 187/**
 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
 191 *
 192 * This helper function will advance the section within the ice segment,
 193 * also advancing the buffer if needed.
 194 */
 195static bool ice_pkg_advance_sect(struct ice_seg *ice_seg,
 196				 struct ice_pkg_enum *state)
 197{
 198	if (!ice_seg && !state->buf)
 199		return false;
 200
 201	if (!ice_seg && state->buf)
 202		if (++state->sect_idx < le16_to_cpu(state->buf->section_count))
 203			return true;
 204
 205	state->buf = ice_pkg_enum_buf(ice_seg, state);
 206	if (!state->buf)
 207		return false;
 208
 209	/* start of new buffer, reset section index */
 210	state->sect_idx = 0;
 211	return true;
 212}
 213
 214/**
 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
 219 *
 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.
 225 */
 226void *ice_pkg_enum_section(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
 227			   u32 sect_type)
 228{
 229	u16 offset, size;
 230
 231	if (ice_seg)
 232		state->type = sect_type;
 233
 234	if (!ice_pkg_advance_sect(ice_seg, state))
 235		return NULL;
 236
 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))
 241			return NULL;
 242
 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)
 246		return NULL;
 247
 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)
 250		return NULL;
 251
 252	/* make sure the section fits in the buffer */
 253	if (offset + size > ICE_PKG_BUF_SIZE)
 254		return NULL;
 255
 256	state->sect_type =
 257		le32_to_cpu(state->buf->section_entry[state->sect_idx].type);
 258
 259	/* calc pointer to this section */
 260	state->sect =
 261		((u8 *)state->buf) +
 262		le16_to_cpu(state->buf->section_entry[state->sect_idx].offset);
 263
 264	return state->sect;
 265}
 266
 267/**
 268 * ice_pkg_enum_entry
 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
 274 *
 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
 279 * been reached.
 280 *
 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
 283 * section.
 284 *
 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.
 291 */
 292void *ice_pkg_enum_entry(struct ice_seg *ice_seg,
 293			 struct ice_pkg_enum *state, u32 sect_type,
 294			 u32 *offset,
 295			 void *(*handler)(u32 sect_type, void *section,
 296					  u32 index, u32 *offset))
 297{
 298	void *entry;
 299
 300	if (ice_seg) {
 301		if (!handler)
 302			return NULL;
 303
 304		if (!ice_pkg_enum_section(ice_seg, state, sect_type))
 305			return NULL;
 306
 307		state->entry_idx = 0;
 308		state->handler = handler;
 309	} else {
 310		state->entry_idx++;
 311	}
 312
 313	if (!state->handler)
 314		return NULL;
 315
 316	/* get entry */
 317	entry = state->handler(state->sect_type, state->sect, state->entry_idx,
 318			       offset);
 319	if (!entry) {
 320		/* end of a section, look for another section of this type */
 321		if (!ice_pkg_enum_section(NULL, state, 0))
 322			return NULL;
 323
 324		state->entry_idx = 0;
 325		entry = state->handler(state->sect_type, state->sect,
 326				       state->entry_idx, offset);
 327	}
 328
 329	return entry;
 330}
 331
 332/**
 333 * ice_sw_fv_handler
 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
 338 *
 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.
 342 */
 343static void *ice_sw_fv_handler(u32 sect_type, void *section, u32 index,
 344			       u32 *offset)
 345{
 346	struct ice_sw_fv_section *fv_section = section;
 347
 348	if (!section || sect_type != ICE_SID_FLD_VEC_SW)
 349		return NULL;
 350	if (index >= le16_to_cpu(fv_section->count))
 351		return NULL;
 352	if (offset)
 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
 357		 */
 358		*offset = le16_to_cpu(fv_section->base_offset) + index;
 359	return fv_section->fv + index;
 360}
 361
 362/**
 363 * ice_get_prof_index_max - get the max profile index for used profile
 364 * @hw: pointer to the HW struct
 365 *
 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.
 369 */
 370static int ice_get_prof_index_max(struct ice_hw *hw)
 371{
 372	u16 prof_index = 0, j, max_prof_index = 0;
 373	struct ice_pkg_enum state;
 374	struct ice_seg *ice_seg;
 375	bool flag = false;
 376	struct ice_fv *fv;
 377	u32 offset;
 378
 379	memset(&state, 0, sizeof(state));
 380
 381	if (!hw->seg)
 382		return -EINVAL;
 383
 384	ice_seg = hw->seg;
 385
 386	do {
 387		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 388					&offset, ice_sw_fv_handler);
 389		if (!fv)
 390			break;
 391		ice_seg = NULL;
 392
 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.
 395		 */
 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)
 399				flag = true;
 400		if (flag && prof_index > max_prof_index)
 401			max_prof_index = prof_index;
 402
 403		prof_index++;
 404		flag = false;
 405	} while (fv);
 406
 407	hw->switch_info->max_used_prof_index = max_prof_index;
 408
 409	return 0;
 410}
 411
 412/**
 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
 416 */
 417static enum ice_ddp_state ice_get_ddp_pkg_state(struct ice_hw *hw,
 418						bool already_loaded)
 419{
 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))) {
 425		if (already_loaded)
 426			return ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED;
 427		else
 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;
 435	} else {
 436		return ICE_DDP_PKG_ERR;
 437	}
 438}
 439
 440/**
 441 * ice_init_pkg_regs - initialize additional package registers
 442 * @hw: pointer to the hardware structure
 443 */
 444static void ice_init_pkg_regs(struct ice_hw *hw)
 445{
 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
 449
 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);
 453}
 454
 455/**
 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
 461 *
 462 * This is a callback function that can be passed to ice_pkg_enum_entry.
 463 * Handles enumeration of individual Marker PType TCAM entries.
 464 */
 465static void *ice_marker_ptype_tcam_handler(u32 sect_type, void *section,
 466					   u32 index, u32 *offset)
 467{
 468	struct ice_marker_ptype_tcam_section *marker_ptype;
 469
 470	if (sect_type != ICE_SID_RXPARSER_MARKER_PTYPE)
 471		return NULL;
 472
 473	if (index > ICE_MAX_MARKER_PTYPE_TCAMS_IN_BUF)
 474		return NULL;
 475
 476	if (offset)
 477		*offset = 0;
 478
 479	marker_ptype = section;
 480	if (index >= le16_to_cpu(marker_ptype->count))
 481		return NULL;
 482
 483	return marker_ptype->tcam + index;
 484}
 485
 486/**
 487 * ice_add_dvm_hint
 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
 491 */
 492static void ice_add_dvm_hint(struct ice_hw *hw, u16 val, bool enable)
 493{
 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;
 497		hw->dvm_upd.count++;
 498	}
 499}
 500
 501/**
 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
 506 */
 507static void ice_add_tunnel_hint(struct ice_hw *hw, char *label_name, u16 val)
 508{
 509	if (hw->tnl.count < ICE_TUNNEL_MAX_ENTRIES) {
 510		u16 i;
 511
 512		for (i = 0; tnls[i].type != TNL_LAST; i++) {
 513			size_t len = strlen(tnls[i].label_prefix);
 514
 515			/* Look for matching label start, before continuing */
 516			if (strncmp(label_name, tnls[i].label_prefix, len))
 517				continue;
 518
 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.
 522			 */
 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;
 528				hw->tnl.count++;
 529				break;
 530			}
 531		}
 532	}
 533}
 534
 535/**
 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
 541 *
 542 * This is a callback function that can be passed to ice_pkg_enum_entry.
 543 * Handles enumeration of individual label entries.
 544 */
 545static void *ice_label_enum_handler(u32 __always_unused sect_type,
 546				    void *section, u32 index, u32 *offset)
 547{
 548	struct ice_label_section *labels;
 549
 550	if (!section)
 551		return NULL;
 552
 553	if (index > ICE_MAX_LABELS_IN_BUF)
 554		return NULL;
 555
 556	if (offset)
 557		*offset = 0;
 558
 559	labels = section;
 560	if (index >= le16_to_cpu(labels->count))
 561		return NULL;
 562
 563	return labels->label + index;
 564}
 565
 566/**
 567 * ice_enum_labels
 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
 572 *
 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.
 577 */
 578static char *ice_enum_labels(struct ice_seg *ice_seg, u32 type,
 579			     struct ice_pkg_enum *state, u16 *value)
 580{
 581	struct ice_label *label;
 582
 583	/* Check for valid label section on first call */
 584	if (type && !(type >= ICE_SID_LBL_FIRST && type <= ICE_SID_LBL_LAST))
 585		return NULL;
 586
 587	label = ice_pkg_enum_entry(ice_seg, state, type, NULL,
 588				   ice_label_enum_handler);
 589	if (!label)
 590		return NULL;
 591
 592	*value = le16_to_cpu(label->value);
 593	return label->name;
 594}
 595
 596/**
 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
 602 *
 603 * This is a callback function that can be passed to ice_pkg_enum_entry.
 604 * Handles enumeration of individual boost TCAM entries.
 605 */
 606static void *ice_boost_tcam_handler(u32 sect_type, void *section, u32 index,
 607				    u32 *offset)
 608{
 609	struct ice_boost_tcam_section *boost;
 610
 611	if (!section)
 612		return NULL;
 613
 614	if (sect_type != ICE_SID_RXPARSER_BOOST_TCAM)
 615		return NULL;
 616
 617	if (index > ICE_MAX_BST_TCAMS_IN_BUF)
 618		return NULL;
 619
 620	if (offset)
 621		*offset = 0;
 622
 623	boost = section;
 624	if (index >= le16_to_cpu(boost->count))
 625		return NULL;
 626
 627	return boost->tcam + index;
 628}
 629
 630/**
 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
 635 *
 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.
 639 */
 640static int ice_find_boost_entry(struct ice_seg *ice_seg, u16 addr,
 641				struct ice_boost_tcam_entry **entry)
 642{
 643	struct ice_boost_tcam_entry *tcam;
 644	struct ice_pkg_enum state;
 645
 646	memset(&state, 0, sizeof(state));
 647
 648	if (!ice_seg)
 649		return -EINVAL;
 650
 651	do {
 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) {
 656			*entry = tcam;
 657			return 0;
 658		}
 659
 660		ice_seg = NULL;
 661	} while (tcam);
 662
 663	*entry = NULL;
 664	return -EIO;
 665}
 666
 667/**
 668 * ice_is_init_pkg_successful - check if DDP init was successful
 669 * @state: state of the DDP pkg after download
 670 */
 671bool ice_is_init_pkg_successful(enum ice_ddp_state state)
 672{
 673	switch (state) {
 674	case ICE_DDP_PKG_SUCCESS:
 675	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
 676	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
 677		return true;
 678	default:
 679		return false;
 680	}
 681}
 682
 683/**
 684 * ice_pkg_buf_alloc
 685 * @hw: pointer to the HW structure
 686 *
 687 * Allocates a package buffer and returns a pointer to the buffer header.
 688 * Note: all package contents must be in Little Endian form.
 689 */
 690struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
 691{
 692	struct ice_buf_build *bld;
 693	struct ice_buf_hdr *buf;
 694
 695	bld = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*bld), GFP_KERNEL);
 696	if (!bld)
 697		return NULL;
 698
 699	buf = (struct ice_buf_hdr *)bld;
 700	buf->data_end =
 701		cpu_to_le16(offsetof(struct ice_buf_hdr, section_entry));
 702	return bld;
 703}
 704
 705static bool ice_is_gtp_u_profile(u16 prof_idx)
 706{
 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;
 710}
 711
 712static bool ice_is_gtp_c_profile(u16 prof_idx)
 713{
 714	switch (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:
 719		return true;
 720	default:
 721		return false;
 722	}
 723}
 724
 725static bool ice_is_pfcp_profile(u16 prof_idx)
 726{
 727	return prof_idx >= ICE_PROFID_IPV4_PFCP_NODE &&
 728	       prof_idx <= ICE_PROFID_IPV6_PFCP_SESSION;
 729}
 730
 731/**
 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
 736 */
 737static enum ice_prof_type ice_get_sw_prof_type(struct ice_hw *hw,
 738					       struct ice_fv *fv, u32 prof_idx)
 739{
 740	u16 i;
 741
 742	if (ice_is_gtp_c_profile(prof_idx))
 743		return ICE_PROF_TUN_GTPC;
 744
 745	if (ice_is_gtp_u_profile(prof_idx))
 746		return ICE_PROF_TUN_GTPU;
 747
 748	if (ice_is_pfcp_profile(prof_idx))
 749		return ICE_PROF_TUN_PFCP;
 750
 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;
 756
 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;
 760	}
 761
 762	return ICE_PROF_NON_TUN;
 763}
 764
 765/**
 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
 770 */
 771void ice_get_sw_fv_bitmap(struct ice_hw *hw, enum ice_prof_type req_profs,
 772			  unsigned long *bm)
 773{
 774	struct ice_pkg_enum state;
 775	struct ice_seg *ice_seg;
 776	struct ice_fv *fv;
 777
 778	if (req_profs == ICE_PROF_ALL) {
 779		bitmap_set(bm, 0, ICE_MAX_NUM_PROFILES);
 780		return;
 781	}
 782
 783	memset(&state, 0, sizeof(state));
 784	bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
 785	ice_seg = hw->seg;
 786	do {
 787		enum ice_prof_type prof_type;
 788		u32 offset;
 789
 790		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 791					&offset, ice_sw_fv_handler);
 792		ice_seg = NULL;
 793
 794		if (fv) {
 795			/* Determine field vector type */
 796			prof_type = ice_get_sw_prof_type(hw, fv, offset);
 797
 798			if (req_profs & prof_type)
 799				set_bit((u16)offset, bm);
 800		}
 801	} while (fv);
 802}
 803
 804/**
 805 * ice_get_sw_fv_list
 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
 810 *
 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.
 817 */
 818int ice_get_sw_fv_list(struct ice_hw *hw, struct ice_prot_lkup_ext *lkups,
 819		       unsigned long *bm, struct list_head *fv_list)
 820{
 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;
 825	struct ice_fv *fv;
 826	u32 offset;
 827
 828	memset(&state, 0, sizeof(state));
 829
 830	if (!lkups->n_val_words || !hw->seg)
 831		return -EINVAL;
 832
 833	ice_seg = hw->seg;
 834	do {
 835		u16 i;
 836
 837		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 838					&offset, ice_sw_fv_handler);
 839		if (!fv)
 840			break;
 841		ice_seg = NULL;
 842
 843		/* If field vector is not in the bitmap list, then skip this
 844		 * profile.
 845		 */
 846		if (!test_bit((u16)offset, bm))
 847			continue;
 848
 849		for (i = 0; i < lkups->n_val_words; i++) {
 850			int j;
 851
 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)
 856					break;
 857			if (j >= hw->blk[ICE_BLK_SW].es.fvw)
 858				break;
 859			if (i + 1 == lkups->n_val_words) {
 860				fvl = devm_kzalloc(ice_hw_to_dev(hw),
 861						   sizeof(*fvl), GFP_KERNEL);
 862				if (!fvl)
 863					goto err;
 864				fvl->fv_ptr = fv;
 865				fvl->profile_id = offset;
 866				list_add(&fvl->list_entry, fv_list);
 867				break;
 868			}
 869		}
 870	} while (fv);
 871	if (list_empty(fv_list)) {
 872		dev_warn(ice_hw_to_dev(hw),
 873			 "Required profiles not found in currently loaded DDP package");
 874		return -EIO;
 875	}
 876
 877	return 0;
 878
 879err:
 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);
 883	}
 884
 885	return -ENOMEM;
 886}
 887
 888/**
 889 * ice_init_prof_result_bm - Initialize the profile result index bitmap
 890 * @hw: pointer to hardware structure
 891 */
 892void ice_init_prof_result_bm(struct ice_hw *hw)
 893{
 894	struct ice_pkg_enum state;
 895	struct ice_seg *ice_seg;
 896	struct ice_fv *fv;
 897
 898	memset(&state, 0, sizeof(state));
 899
 900	if (!hw->seg)
 901		return;
 902
 903	ice_seg = hw->seg;
 904	do {
 905		u32 off;
 906		u16 i;
 907
 908		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 909					&off, ice_sw_fv_handler);
 910		ice_seg = NULL;
 911		if (!fv)
 912			break;
 913
 914		bitmap_zero(hw->switch_info->prof_res_bm[off],
 915			    ICE_MAX_FV_WORDS);
 916
 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.
 920		 */
 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]);
 925	} while (fv);
 926}
 927
 928/**
 929 * ice_pkg_buf_free
 930 * @hw: pointer to the HW structure
 931 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
 932 *
 933 * Frees a package buffer
 934 */
 935void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
 936{
 937	devm_kfree(ice_hw_to_dev(hw), bld);
 938}
 939
 940/**
 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
 944 *
 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.
 952 */
 953int ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
 954{
 955	struct ice_buf_hdr *buf;
 956	u16 section_count;
 957	u16 data_end;
 958
 959	if (!bld)
 960		return -EINVAL;
 961
 962	buf = (struct ice_buf_hdr *)&bld->buf;
 963
 964	/* already an active section, can't increase table size */
 965	section_count = le16_to_cpu(buf->section_count);
 966	if (section_count > 0)
 967		return -EIO;
 968
 969	if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
 970		return -EIO;
 971	bld->reserved_section_table_entries += count;
 972
 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);
 976
 977	return 0;
 978}
 979
 980/**
 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)
 985 *
 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
 989 * section contents.
 990 * Note: all package contents must be in Little Endian form.
 991 */
 992void *ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
 993{
 994	struct ice_buf_hdr *buf;
 995	u16 sect_count;
 996	u16 data_end;
 997
 998	if (!bld || !type || !size)
 999		return NULL;
1000
1001	buf = (struct ice_buf_hdr *)&bld->buf;
1002
1003	/* check for enough space left in buffer */
1004	data_end = le16_to_cpu(buf->data_end);
1005
1006	/* section start must align on 4 byte boundary */
1007	data_end = ALIGN(data_end, 4);
1008
1009	if ((data_end + size) > ICE_MAX_S_DATA_END)
1010		return NULL;
1011
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;
1016
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);
1020
1021		data_end += size;
1022		buf->data_end = cpu_to_le16(data_end);
1023
1024		buf->section_count = cpu_to_le16(sect_count + 1);
1025		return section_ptr;
1026	}
1027
1028	/* no free section table entries */
1029	return NULL;
1030}
1031
1032/**
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
1038 *
1039 * Allocates a package buffer with a single section.
1040 * Note: all package contents must be in Little Endian form.
1041 */
1042struct ice_buf_build *ice_pkg_buf_alloc_single_section(struct ice_hw *hw,
1043						       u32 type, u16 size,
1044						       void **section)
1045{
1046	struct ice_buf_build *buf;
1047
1048	if (!section)
1049		return NULL;
1050
1051	buf = ice_pkg_buf_alloc(hw);
1052	if (!buf)
1053		return NULL;
1054
1055	if (ice_pkg_buf_reserve_section(buf, 1))
1056		goto ice_pkg_buf_alloc_single_section_err;
1057
1058	*section = ice_pkg_buf_alloc_section(buf, type, size);
1059	if (!*section)
1060		goto ice_pkg_buf_alloc_single_section_err;
1061
1062	return buf;
1063
1064ice_pkg_buf_alloc_single_section_err:
1065	ice_pkg_buf_free(hw, buf);
1066	return NULL;
1067}
1068
1069/**
1070 * ice_pkg_buf_get_active_sections
1071 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1072 *
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
1076 * not be used.
1077 * Note: all package contents must be in Little Endian form.
1078 */
1079u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
1080{
1081	struct ice_buf_hdr *buf;
1082
1083	if (!bld)
1084		return 0;
1085
1086	buf = (struct ice_buf_hdr *)&bld->buf;
1087	return le16_to_cpu(buf->section_count);
1088}
1089
1090/**
1091 * ice_pkg_buf
1092 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1093 *
1094 * Return a pointer to the buffer's header
1095 */
1096struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
1097{
1098	if (!bld)
1099		return NULL;
1100
1101	return &bld->buf;
1102}
1103
1104static enum ice_ddp_state ice_map_aq_err_to_ddp_state(enum ice_aq_err aq_err)
1105{
1106	switch (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;
1115	default:
1116		return ICE_DDP_PKG_ERR;
1117	}
1118}
1119
1120/**
1121 * ice_acquire_global_cfg_lock
1122 * @hw: pointer to the HW structure
1123 * @access: access type (read or write)
1124 *
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:
1128 *
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.
1135 */
1136static int ice_acquire_global_cfg_lock(struct ice_hw *hw,
1137				       enum ice_aq_res_access_type access)
1138{
1139	int status;
1140
1141	status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
1142				 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
1143
1144	if (!status)
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");
1149
1150	return status;
1151}
1152
1153/**
1154 * ice_release_global_cfg_lock
1155 * @hw: pointer to the HW structure
1156 *
1157 * This function will release the global config lock.
1158 */
1159static void ice_release_global_cfg_lock(struct ice_hw *hw)
1160{
1161	mutex_unlock(&ice_global_cfg_lock_sw);
1162	ice_release_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID);
1163}
1164
1165/**
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
1174 *
1175 * Download Package (0x0C40)
1176 */
1177static int
1178ice_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)
1181{
1182	struct ice_aqc_download_pkg *cmd;
1183	struct ice_aq_desc desc;
1184	int status;
1185
1186	if (error_offset)
1187		*error_offset = 0;
1188	if (error_info)
1189		*error_info = 0;
1190
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);
1194
1195	if (last_buf)
1196		cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1197
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;
1202
1203		resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1204		if (error_offset)
1205			*error_offset = le32_to_cpu(resp->error_offset);
1206		if (error_info)
1207			*error_info = le32_to_cpu(resp->error_info);
1208	}
1209
1210	return status;
1211}
1212
1213/**
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.
1217 */
1218static bool ice_is_buffer_metadata(struct ice_buf_hdr *buf)
1219{
1220	return le32_to_cpu(buf->section_entry[0].type) & ICE_METADATA_BUF;
1221}
1222
1223/**
1224 * struct ice_ddp_send_ctx - sending context of current DDP segment
1225 * @hw: pointer to the hardware struct
1226 *
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().
1229 */
1230struct ice_ddp_send_ctx {
1231	struct ice_hw *hw;
1232/* private: only for ice_ddp_send_hunk() */
1233	struct ice_buf_hdr *hdr;
1234	int err;
1235};
1236
1237static void ice_ddp_send_ctx_set_err(struct ice_ddp_send_ctx *ctx, int err)
1238{
1239	ctx->err = err;
1240}
1241
1242/**
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
1246 *
1247 * Send the next hunk of data to FW, retrying if needed.
1248 *
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.
1252 *
1253 * Return: %ICE_DDP_PKG_SUCCESS if there were no problems; a sticky @err
1254 *         otherwise.
1255 */
1256static enum ice_ddp_state ice_ddp_send_hunk(struct ice_ddp_send_ctx *ctx,
1257					    struct ice_buf_hdr *hunk)
1258{
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;
1263	u32 offset, info;
1264	int attempt, err;
1265
1266	if (ctx->err)
1267		return ctx->err;
1268
1269	ctx->hdr = hunk;
1270	if (!prev_hunk)
1271		return ICE_DDP_PKG_SUCCESS; /* no problem so far */
1272
1273	for (attempt = 0; attempt < 5; attempt++) {
1274		if (attempt)
1275			msleep(20);
1276
1277		err = ice_aq_download_pkg(hw, prev_hunk, ICE_PKG_BUF_SIZE,
1278					  prev_was_last, &offset, &info, NULL);
1279
1280		aq_err = hw->adminq.sq_last_status;
1281		if (aq_err != ICE_AQ_RC_ENOSEC && aq_err != ICE_AQ_RC_EBADSIG)
1282			break;
1283	}
1284
1285	if (err) {
1286		ice_debug(hw, ICE_DBG_PKG, "Pkg download failed: err %d off %d inf %d\n",
1287			  err, offset, info);
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);
1292	}
1293
1294	return ctx->err;
1295}
1296
1297/**
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
1303 *
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.
1307 */
1308static enum ice_ddp_state
1309ice_dwnld_cfg_bufs_no_lock(struct ice_ddp_send_ctx *ctx, struct ice_buf *bufs,
1310			   u32 start, u32 count)
1311{
1312	struct ice_buf_hdr *bh;
1313	enum ice_ddp_state err;
1314
1315	if (!bufs || !count) {
1316		ice_ddp_send_ctx_set_err(ctx, ICE_DDP_PKG_ERR);
1317		return ICE_DDP_PKG_ERR;
1318	}
1319
1320	bufs += start;
1321
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".
1326		 */
1327		if (ice_is_buffer_metadata(bh))
1328			break;
1329
1330		err = ice_ddp_send_hunk(ctx, bh);
1331		if (err)
1332			return err;
1333	}
1334
1335	return 0;
1336}
1337
1338/**
1339 * ice_get_pkg_seg_by_idx
1340 * @pkg_hdr: pointer to the package header to be searched
1341 * @idx: index of segment
1342 */
1343static struct ice_generic_seg_hdr *
1344ice_get_pkg_seg_by_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx)
1345{
1346	if (idx < le32_to_cpu(pkg_hdr->seg_count))
1347		return (struct ice_generic_seg_hdr *)
1348			((u8 *)pkg_hdr +
1349			 le32_to_cpu(pkg_hdr->seg_offset[idx]));
1350
1351	return NULL;
1352}
1353
1354/**
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
1358 */
1359static bool ice_is_signing_seg_at_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx)
1360{
1361	struct ice_generic_seg_hdr *seg;
1362
1363	seg = ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1364	if (!seg)
1365		return false;
1366
1367	return le32_to_cpu(seg->seg_type) == SEGMENT_TYPE_SIGNING;
1368}
1369
1370/**
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
1376 *
1377 * Determine if a segment is a signing segment of the correct type
1378 */
1379static bool
1380ice_is_signing_seg_type_at_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx,
1381			       u32 seg_id, u32 sign_type)
1382{
1383	struct ice_sign_seg *seg;
1384
1385	if (!ice_is_signing_seg_at_idx(pkg_hdr, idx))
1386		return false;
1387
1388	seg = (struct ice_sign_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1389
1390	if (seg && le32_to_cpu(seg->seg_id) == seg_id &&
1391	    le32_to_cpu(seg->sign_type) == sign_type)
1392		return true;
1393
1394	return false;
1395}
1396
1397/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1401 */
1402static enum ice_ddp_state
1403ice_download_pkg_sig_seg(struct ice_ddp_send_ctx *ctx, struct ice_sign_seg *seg)
1404{
1405	return ice_dwnld_cfg_bufs_no_lock(ctx, seg->buf_tbl.buf_array, 0,
1406					  le32_to_cpu(seg->buf_tbl.buf_count));
 
1407}
1408
1409/**
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
1416 *
1417 * Note: idx must reference a ICE segment
1418 */
1419static enum ice_ddp_state
1420ice_download_pkg_config_seg(struct ice_ddp_send_ctx *ctx,
1421			    struct ice_pkg_hdr *pkg_hdr, u32 idx, u32 start,
1422			    u32 count)
1423{
1424	struct ice_buf_table *bufs;
1425	struct ice_seg *seg;
1426	u32 buf_count;
1427
1428	seg = (struct ice_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1429	if (!seg)
1430		return ICE_DDP_PKG_ERR;
1431
1432	bufs = ice_find_buf_table(seg);
1433	buf_count = le32_to_cpu(bufs->buf_count);
1434
1435	if (start >= buf_count || start + count > buf_count)
1436		return ICE_DDP_PKG_ERR;
1437
1438	return ice_dwnld_cfg_bufs_no_lock(ctx, bufs->buf_array, start, count);
1439}
1440
1441static bool ice_is_last_sign_seg(u32 flags)
1442{
1443	return !(flags & ICE_SIGN_SEG_FLAGS_VALID) || /* behavior prior to valid */
1444	       (flags & ICE_SIGN_SEG_FLAGS_LAST);
1445}
1446
1447/**
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)
1452 *
1453 * Note: idx must reference a signature segment
1454 */
1455static enum ice_ddp_state
1456ice_dwnld_sign_and_cfg_segs(struct ice_ddp_send_ctx *ctx,
1457			    struct ice_pkg_hdr *pkg_hdr, u32 idx)
1458{
1459	u32 conf_idx, start, count, flags;
1460	enum ice_ddp_state state;
1461	struct ice_sign_seg *seg;
 
 
 
1462
1463	seg = (struct ice_sign_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1464	if (!seg) {
1465		state = ICE_DDP_PKG_ERR;
1466		ice_ddp_send_ctx_set_err(ctx, state);
1467		return state;
1468	}
1469
1470	count = le32_to_cpu(seg->signed_buf_count);
1471	state = ice_download_pkg_sig_seg(ctx, seg);
1472	if (state || !count)
1473		return state;
1474
1475	conf_idx = le32_to_cpu(seg->signed_seg_idx);
1476	start = le32_to_cpu(seg->signed_buf_start);
 
1477
1478	state = ice_download_pkg_config_seg(ctx, pkg_hdr, conf_idx, start,
1479					    count);
 
1480
1481	/* finish up by sending last hunk with "last" flag set if requested by
1482	 * DDP content
1483	 */
1484	flags = le32_to_cpu(seg->flags);
1485	if (ice_is_last_sign_seg(flags))
1486		state = ice_ddp_send_hunk(ctx, NULL);
1487
 
1488	return state;
1489}
1490
1491/**
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
1496 */
1497static bool
1498ice_match_signing_seg(struct ice_pkg_hdr *pkg_hdr, u32 seg_id, u32 sign_type)
1499{
1500	u32 i;
1501
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,
1504						   sign_type))
1505			return true;
1506	}
1507
1508	return false;
1509}
1510
1511/**
1512 * ice_post_dwnld_pkg_actions - perform post download package actions
1513 * @hw: pointer to the hardware structure
1514 */
1515static enum ice_ddp_state
1516ice_post_dwnld_pkg_actions(struct ice_hw *hw)
1517{
1518	int status;
1519
1520	status = ice_set_vlan_mode(hw);
1521	if (status) {
1522		ice_debug(hw, ICE_DBG_PKG, "Failed to set VLAN mode: err %d\n",
1523			  status);
1524		return ICE_DDP_PKG_ERR;
1525	}
1526
1527	return ICE_DDP_PKG_SUCCESS;
1528}
1529
1530/**
1531 * ice_download_pkg_with_sig_seg
1532 * @hw: pointer to the hardware structure
1533 * @pkg_hdr: pointer to package header
1534 *
1535 * Handles the download of a complete package.
1536 */
1537static enum ice_ddp_state
1538ice_download_pkg_with_sig_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1539{
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 };
1543	int status;
1544	u32 i;
1545
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);
1548
1549	status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1550	if (status) {
1551		if (status == -EALREADY)
1552			state = ICE_DDP_PKG_ALREADY_LOADED;
1553		else
1554			state = ice_map_aq_err_to_ddp_state(aq_err);
1555		return state;
1556	}
1557
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,
1560						    hw->pkg_sign_type))
1561			continue;
1562
1563		state = ice_dwnld_sign_and_cfg_segs(&ctx, pkg_hdr, i);
1564		if (state)
1565			break;
1566	}
1567
1568	if (!state)
1569		state = ice_post_dwnld_pkg_actions(hw);
1570
1571	ice_release_global_cfg_lock(hw);
1572
1573	return state;
1574}
1575
1576/**
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
1581 *
1582 * Obtains global config lock and downloads the package configuration buffers
1583 * to the firmware.
1584 */
1585static enum ice_ddp_state
1586ice_dwnld_cfg_bufs(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1587{
1588	struct ice_ddp_send_ctx ctx = { .hw = hw };
1589	enum ice_ddp_state state;
1590	struct ice_buf_hdr *bh;
1591	int status;
1592
1593	if (!bufs || !count)
1594		return ICE_DDP_PKG_ERR;
1595
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.
1599	 */
1600	bh = (struct ice_buf_hdr *)bufs;
1601	if (ice_is_buffer_metadata(bh))
1602		return ICE_DDP_PKG_SUCCESS;
1603
1604	status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1605	if (status) {
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);
1609	}
1610
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);
1614	if (!state)
1615		state = ice_post_dwnld_pkg_actions(hw);
1616
1617	ice_release_global_cfg_lock(hw);
1618
1619	return state;
1620}
1621
1622/**
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
1626 *
1627 * Handles the download of a complete package without signature segment.
1628 */
1629static enum ice_ddp_state
1630ice_download_pkg_without_sig_seg(struct ice_hw *hw, struct ice_seg *ice_seg)
1631{
1632	struct ice_buf_table *ice_buf_tbl;
1633
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);
1639
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);
1643
1644	ice_buf_tbl = ice_find_buf_table(ice_seg);
1645
1646	ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1647		  le32_to_cpu(ice_buf_tbl->buf_count));
1648
1649	return ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1650				  le32_to_cpu(ice_buf_tbl->buf_count));
1651}
1652
1653/**
1654 * ice_download_pkg
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
1658 *
1659 * Handles the download of a complete package.
1660 */
1661static enum ice_ddp_state
1662ice_download_pkg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr,
1663		 struct ice_seg *ice_seg)
1664{
1665	enum ice_ddp_state state;
1666
1667	if (hw->pkg_has_signing_seg)
1668		state = ice_download_pkg_with_sig_seg(hw, pkg_hdr);
1669	else
1670		state = ice_download_pkg_without_sig_seg(hw, ice_seg);
1671
1672	ice_post_pkg_dwnld_vlan_mode_cfg(hw);
1673
1674	return state;
1675}
1676
1677/**
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
1683 *
1684 * Get Package Info List (0x0C43)
1685 */
1686static 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)
1689{
1690	struct ice_aq_desc desc;
1691
1692	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1693
1694	return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1695}
1696
1697/**
1698 * ice_aq_update_pkg
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
1706 *
1707 * Update Package (0x0C42)
1708 */
1709static 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)
1712{
1713	struct ice_aqc_download_pkg *cmd;
1714	struct ice_aq_desc desc;
1715	int status;
1716
1717	if (error_offset)
1718		*error_offset = 0;
1719	if (error_info)
1720		*error_info = 0;
1721
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);
1725
1726	if (last_buf)
1727		cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1728
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;
1733
1734		resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1735		if (error_offset)
1736			*error_offset = le32_to_cpu(resp->error_offset);
1737		if (error_info)
1738			*error_info = le32_to_cpu(resp->error_info);
1739	}
1740
1741	return status;
1742}
1743
1744/**
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
1750 *
1751 * Upload Section (0x0C41)
1752 */
1753int ice_aq_upload_section(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1754			  u16 buf_size, struct ice_sq_cd *cd)
1755{
1756	struct ice_aq_desc desc;
1757
1758	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_upload_section);
1759	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1760
1761	return ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1762}
1763
1764/**
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
1769 */
1770int ice_update_pkg_no_lock(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1771{
1772	int status = 0;
1773	u32 i;
1774
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);
1778		u32 offset, info;
1779
1780		status = ice_aq_update_pkg(hw, bh, le16_to_cpu(bh->data_end),
1781					   last, &offset, &info, NULL);
1782
1783		if (status) {
1784			ice_debug(hw, ICE_DBG_PKG,
1785				  "Update pkg failed: err %d off %d inf %d\n",
1786				  status, offset, info);
1787			break;
1788		}
1789	}
1790
1791	return status;
1792}
1793
1794/**
1795 * ice_update_pkg
1796 * @hw: pointer to the hardware structure
1797 * @bufs: pointer to an array of buffers
1798 * @count: the number of buffers in the array
1799 *
1800 * Obtains change lock and updates package.
1801 */
1802int ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1803{
1804	int status;
1805
1806	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
1807	if (status)
1808		return status;
1809
1810	status = ice_update_pkg_no_lock(hw, bufs, count);
1811
1812	ice_release_change_lock(hw);
1813
1814	return status;
1815}
1816
1817/**
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
1822 *
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
1825 * return NULL.
1826 */
1827static const struct ice_generic_seg_hdr *
1828ice_find_seg_in_pkg(struct ice_hw *hw, u32 seg_type,
1829		    const struct ice_pkg_hdr *pkg_hdr)
1830{
1831	u32 i;
1832
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);
1837
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;
1841
1842		seg = (void *)pkg_hdr + le32_to_cpu(pkg_hdr->seg_offset[i]);
 
 
1843
1844		if (le32_to_cpu(seg->seg_type) == seg_type)
1845			return seg;
1846	}
1847
1848	return NULL;
1849}
1850
1851/**
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
1855 */
1856static bool ice_has_signing_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1857{
1858	struct ice_generic_seg_hdr *seg_hdr;
1859
1860	seg_hdr = (struct ice_generic_seg_hdr *)
1861		ice_find_seg_in_pkg(hw, SEGMENT_TYPE_SIGNING, pkg_hdr);
1862
1863	return seg_hdr ? true : false;
1864}
1865
1866/**
1867 * ice_get_pkg_segment_id - get correct package segment id, based on device
1868 * @mac_type: MAC type of the device
1869 */
1870static u32 ice_get_pkg_segment_id(enum ice_mac_type mac_type)
1871{
1872	u32 seg_id;
1873
1874	switch (mac_type) {
1875	case ICE_MAC_E830:
1876		seg_id = SEGMENT_TYPE_ICE_E830;
1877		break;
1878	case ICE_MAC_GENERIC:
1879	case ICE_MAC_GENERIC_3K_E825:
1880	default:
1881		seg_id = SEGMENT_TYPE_ICE_E810;
1882		break;
1883	}
1884
1885	return seg_id;
1886}
1887
1888/**
1889 * ice_get_pkg_sign_type - get package segment sign type, based on device
1890 * @mac_type: MAC type of the device
1891 */
1892static u32 ice_get_pkg_sign_type(enum ice_mac_type mac_type)
1893{
1894	u32 sign_type;
1895
1896	switch (mac_type) {
1897	case ICE_MAC_E830:
1898		sign_type = SEGMENT_SIGN_TYPE_RSA3K_SBB;
1899		break;
1900	case ICE_MAC_GENERIC_3K_E825:
1901		sign_type = SEGMENT_SIGN_TYPE_RSA3K_E825;
1902		break;
1903	case ICE_MAC_GENERIC:
1904	default:
1905		sign_type = SEGMENT_SIGN_TYPE_RSA2K;
1906		break;
1907	}
1908
1909	return sign_type;
1910}
1911
1912/**
1913 * ice_get_signing_req - get correct package requirements, based on device
1914 * @hw: pointer to the hardware structure
1915 */
1916static void ice_get_signing_req(struct ice_hw *hw)
1917{
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);
1920}
1921
1922/**
1923 * ice_init_pkg_info
1924 * @hw: pointer to the hardware structure
1925 * @pkg_hdr: pointer to the driver's package hdr
1926 *
1927 * Saves off the package details into the HW structure.
1928 */
1929static enum ice_ddp_state ice_init_pkg_info(struct ice_hw *hw,
1930					    struct ice_pkg_hdr *pkg_hdr)
1931{
1932	struct ice_generic_seg_hdr *seg_hdr;
1933
1934	if (!pkg_hdr)
1935		return ICE_DDP_PKG_ERR;
1936
1937	hw->pkg_has_signing_seg = ice_has_signing_seg(hw, pkg_hdr);
1938	ice_get_signing_req(hw);
1939
1940	ice_debug(hw, ICE_DBG_INIT, "Pkg using segment id: 0x%08X\n",
1941		  hw->pkg_seg_id);
1942
1943	seg_hdr = (struct ice_generic_seg_hdr *)
1944		ice_find_seg_in_pkg(hw, hw->pkg_seg_id, pkg_hdr);
1945	if (seg_hdr) {
1946		struct ice_meta_sect *meta;
1947		struct ice_pkg_enum state;
1948
1949		memset(&state, 0, sizeof(state));
1950
1951		/* Get package information from the Metadata Section */
1952		meta = ice_pkg_enum_section((struct ice_seg *)seg_hdr, &state,
1953					    ICE_SID_METADATA);
1954		if (!meta) {
1955			ice_debug(hw, ICE_DBG_INIT,
1956				  "Did not find ice metadata section in package\n");
1957			return ICE_DDP_PKG_INVALID_FILE;
1958		}
1959
1960		hw->pkg_ver = meta->ver;
1961		memcpy(hw->pkg_name, meta->name, sizeof(meta->name));
1962
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);
1966
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));
1969
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);
1975	} else {
1976		ice_debug(hw, ICE_DBG_INIT,
1977			  "Did not find ice segment in driver package\n");
1978		return ICE_DDP_PKG_INVALID_FILE;
1979	}
1980
1981	return ICE_DDP_PKG_SUCCESS;
1982}
1983
1984/**
1985 * ice_get_pkg_info
1986 * @hw: pointer to the hardware structure
1987 *
1988 * Store details of the package currently loaded in HW into the HW structure.
1989 */
1990static enum ice_ddp_state ice_get_pkg_info(struct ice_hw *hw)
1991{
1992	DEFINE_RAW_FLEX(struct ice_aqc_get_pkg_info_resp, pkg_info, pkg_info,
1993			ICE_PKG_CNT);
1994	u16 size = __struct_size(pkg_info);
1995	u32 i;
1996
1997	if (ice_aq_get_pkg_info_list(hw, pkg_info, size, NULL))
1998		return ICE_DDP_PKG_ERR;
1999
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 };
2003		u8 place = 0;
2004
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;
2013		}
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';
2020
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);
2027	}
2028
2029	return ICE_DDP_PKG_SUCCESS;
2030}
2031
2032/**
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
2037 *
2038 * This function checks the package version compatibility with driver and NVM
2039 */
2040static enum ice_ddp_state ice_chk_pkg_compat(struct ice_hw *hw,
2041					     struct ice_pkg_hdr *ospkg,
2042					     struct ice_seg **seg)
2043{
2044	DEFINE_RAW_FLEX(struct ice_aqc_get_pkg_info_resp, pkg, pkg_info,
2045			ICE_PKG_CNT);
2046	u16 size = __struct_size(pkg);
2047	enum ice_ddp_state state;
2048	u32 i;
2049
2050	/* Check package version compatibility */
2051	state = ice_chk_pkg_version(&hw->pkg_ver);
2052	if (state) {
2053		ice_debug(hw, ICE_DBG_INIT, "Package version check failed.\n");
2054		return state;
2055	}
2056
2057	/* find ICE segment in given package */
2058	*seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, hw->pkg_seg_id,
2059						     ospkg);
2060	if (!*seg) {
2061		ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
2062		return ICE_DDP_PKG_INVALID_FILE;
2063	}
2064
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;
2068
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)
2072			continue;
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");
2080		}
2081		/* done processing NVM package so break */
2082		break;
2083	}
2084
2085	return state;
2086}
2087
2088/**
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)
2092 *
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.
2097 */
2098static void ice_init_pkg_hints(struct ice_hw *hw, struct ice_seg *ice_seg)
2099{
2100	struct ice_pkg_enum state;
2101	char *label_name;
2102	u16 val;
2103	int i;
2104
2105	memset(&hw->tnl, 0, sizeof(hw->tnl));
2106	memset(&state, 0, sizeof(state));
2107
2108	if (!ice_seg)
2109		return;
2110
2111	label_name = ice_enum_labels(ice_seg, ICE_SID_LBL_RXPARSER_TMEM, &state,
2112				     &val);
2113
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);
2118
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);
2122
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);
2126
2127		label_name = ice_enum_labels(NULL, 0, &state, &val);
2128	}
2129
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]++;
2138		}
2139	}
2140
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);
2145}
2146
2147/**
2148 * ice_fill_hw_ptype - fill the enabled PTYPE bit information
2149 * @hw: pointer to the HW structure
2150 */
2151static void ice_fill_hw_ptype(struct ice_hw *hw)
2152{
2153	struct ice_marker_ptype_tcam_entry *tcam;
2154	struct ice_seg *seg = hw->seg;
2155	struct ice_pkg_enum state;
2156
2157	bitmap_zero(hw->hw_ptype, ICE_FLOW_PTYPE_MAX);
2158	if (!seg)
2159		return;
2160
2161	memset(&state, 0, sizeof(state));
2162
2163	do {
2164		tcam = ice_pkg_enum_entry(seg, &state,
2165					  ICE_SID_RXPARSER_MARKER_PTYPE, NULL,
2166					  ice_marker_ptype_tcam_handler);
2167		if (tcam &&
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);
2171
2172		seg = NULL;
2173	} while (tcam);
2174}
2175
2176/**
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
2181 *
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.
2190 *
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).
2194 *
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
2199 * case.
2200 */
2201enum ice_ddp_state ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
2202{
2203	bool already_loaded = false;
2204	enum ice_ddp_state state;
2205	struct ice_pkg_hdr *pkg;
2206	struct ice_seg *seg;
2207
2208	if (!buf || !len)
2209		return ICE_DDP_PKG_ERR;
2210
2211	pkg = (struct ice_pkg_hdr *)buf;
2212	state = ice_verify_pkg(pkg, len);
2213	if (state) {
2214		ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
2215			  state);
2216		return state;
2217	}
2218
2219	/* initialize package info */
2220	state = ice_init_pkg_info(hw, pkg);
2221	if (state)
2222		return state;
2223
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;
2228
2229	/* before downloading the package, check package version for
2230	 * compatibility with driver
2231	 */
2232	state = ice_chk_pkg_compat(hw, pkg, &seg);
2233	if (state)
2234		return state;
2235
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;
2243	}
2244
2245	/* Get information on the package currently loaded in HW, then make sure
2246	 * the driver is compatible with this version.
2247	 */
2248	if (!state || state == ICE_DDP_PKG_ALREADY_LOADED) {
2249		state = ice_get_pkg_info(hw);
2250		if (!state)
2251			state = ice_get_ddp_pkg_state(hw, already_loaded);
2252	}
2253
2254	if (ice_is_init_pkg_successful(state)) {
2255		hw->seg = seg;
2256		/* on successful package download update other required
2257		 * registers to support the package and fill HW tables
2258		 * with package content.
2259		 */
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);
2264	} else {
2265		ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n", state);
2266	}
2267
2268	return state;
2269}
2270
2271/**
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
2276 *
2277 * This function copies the package buffer, and then calls ice_init_pkg() to
2278 * initialize the copied package contents.
2279 *
2280 * The copying is necessary if the package buffer supplied is constant, or if
2281 * the memory may disappear shortly after calling this function.
2282 *
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().
2285 *
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().
2288 *
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
2292 * related routines.
2293 */
2294enum ice_ddp_state ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf,
2295					 u32 len)
2296{
2297	enum ice_ddp_state state;
2298	u8 *buf_copy;
2299
2300	if (!buf || !len)
2301		return ICE_DDP_PKG_ERR;
2302
2303	buf_copy = devm_kmemdup(ice_hw_to_dev(hw), buf, len, GFP_KERNEL);
2304
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);
2309	} else {
2310		/* Track the copied pkg so we can free it later */
2311		hw->pkg_copy = buf_copy;
2312		hw->pkg_size = len;
2313	}
2314
2315	return state;
2316}
2317
2318/**
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
2326 *
2327 * The function will get or set Tx topology
2328 *
2329 * Return: zero when set was successful, negative values otherwise.
2330 */
2331static int
2332ice_get_set_tx_topo(struct ice_hw *hw, u8 *buf, u16 buf_size,
2333		    struct ice_sq_cd *cd, u8 *flags, bool set)
2334{
2335	struct ice_aqc_get_set_tx_topo *cmd;
2336	struct ice_aq_desc desc;
2337	int status;
2338
2339	cmd = &desc.params.get_set_tx_topo;
2340	if (set) {
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 */
2344		if (buf)
2345			cmd->set_flags |= ICE_AQC_TX_TOPO_FLAGS_SRC_RAM |
2346					  ICE_AQC_TX_TOPO_FLAGS_LOAD_NEW;
2347
2348		if (ice_is_e825c(hw))
2349			desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2350	} else {
2351		ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_tx_topo);
2352		cmd->get_flags = ICE_AQC_TX_TOPO_GET_RAM;
2353	}
2354
2355	if (!ice_is_e825c(hw))
2356		desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2357
2358	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2359	if (status)
2360		return status;
2361	/* read the return flag values (first byte) for get operation */
2362	if (!set && flags)
2363		*flags = desc.params.get_set_tx_topo.set_flags;
2364
2365	return 0;
2366}
2367
2368/**
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
2372 * @len: buffer size
2373 *
2374 * The function will apply the new Tx topology from the package buffer
2375 * if available.
2376 *
2377 * Return: zero when update was successful, negative values otherwise.
2378 */
2379int ice_cfg_tx_topo(struct ice_hw *hw, const void *buf, u32 len)
2380{
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;
2387	u32 reg = 0;
2388	int status;
2389	u8 flags;
2390
2391	if (!buf || !len)
2392		return -EINVAL;
2393
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");
2397		return -EOPNOTSUPP;
2398	}
2399
2400	topo = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
2401	if (!topo)
2402		return -ENOMEM;
2403
2404	/* Get the current Tx topology flags */
2405	status = ice_get_set_tx_topo(hw, topo, ICE_AQ_MAX_BUF_LEN, NULL, &flags,
2406				     false);
2407
2408	if (status) {
2409		ice_debug(hw, ICE_DBG_INIT, "Get current topology is failed\n");
2410		return status;
2411	}
2412
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");
2417		return -EEXIST;
2418	}
2419
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");
2424		return -EEXIST;
2425	}
2426
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 */
2431		msleep(2000);
2432		return -EEXIST;
2433	}
2434
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");
2439		goto update_topo;
2440	}
2441
2442	pkg_hdr = (const struct ice_pkg_hdr *)buf;
2443	state = ice_verify_pkg(pkg_hdr, len);
2444	if (state) {
2445		ice_debug(hw, ICE_DBG_INIT, "Failed to verify pkg (err: %d)\n",
2446			  state);
2447		return -EIO;
2448	}
2449
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);
2453	if (!seg) {
2454		ice_debug(hw, ICE_DBG_INIT, "5 layer topology segment is missing\n");
2455		return -EIO;
2456	}
2457
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);
2461		return -EIO;
2462	}
2463
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");
2468		return -EIO;
2469	}
2470
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");
2475		return -EIO;
2476	}
2477
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");
2481		return -EIO;
2482	}
2483
2484	/* Get the new topology buffer, reuse current topo copy mem */
2485	static_assert(ICE_PKG_BUF_SIZE == ICE_AQ_MAX_BUF_LEN);
2486	new_topo = topo;
2487	memcpy(new_topo, (u8 *)section + offset, size);
2488
2489update_topo:
2490	/* Acquire global lock to make sure that set topology issued
2491	 * by one PF.
2492	 */
2493	status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, ICE_RES_WRITE,
2494				 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
2495	if (status) {
2496		ice_debug(hw, ICE_DBG_INIT, "Failed to acquire global lock\n");
2497		return status;
2498	}
2499
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);
2506		return 0;
2507	}
2508
2509	/* Set new topology */
2510	status = ice_get_set_tx_topo(hw, new_topo, size, NULL, NULL, true);
2511	if (status) {
2512		ice_debug(hw, ICE_DBG_INIT, "Failed setting Tx topology\n");
2513		return status;
2514	}
2515
2516	/* New topology is updated, delay 1 second before issuing the CORER */
2517	msleep(1000);
2518	ice_reset(hw, ICE_RESET_CORER);
2519	/* CORER will clear the global lock, so no explicit call
2520	 * required for release.
2521	 */
2522
2523	return 0;
2524}
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2022, Intel Corporation. */
   3
   4#include "ice_common.h"
   5#include "ice.h"
   6#include "ice_ddp.h"
 
   7
   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.
  11 */
  12#define ICE_DVM_PRE "BOOST_MAC_VLAN_DVM" /* enable these entries */
  13#define ICE_SVM_PRE "BOOST_MAC_VLAN_SVM" /* disable these entries */
  14
  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.
  17 */
  18#define ICE_TNL_PRE "TNL_"
  19static const struct ice_tunnel_type_scan tnls[] = {
  20	{ TNL_VXLAN, "TNL_VXLAN_PF" },
  21	{ TNL_GENEVE, "TNL_GENEVE_PF" },
  22	{ TNL_LAST, "" }
  23};
  24
  25/**
  26 * ice_verify_pkg - verify package
  27 * @pkg: pointer to the package buffer
  28 * @len: size of the package buffer
  29 *
  30 * Verifies various attributes of the package file, including length, format
  31 * version, and the requirement of at least one segment.
  32 */
  33static enum ice_ddp_state ice_verify_pkg(struct ice_pkg_hdr *pkg, u32 len)
  34{
  35	u32 seg_count;
  36	u32 i;
  37
  38	if (len < struct_size(pkg, seg_offset, 1))
  39		return ICE_DDP_PKG_INVALID_FILE;
  40
  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;
  46
  47	/* pkg must have at least one segment */
  48	seg_count = le32_to_cpu(pkg->seg_count);
  49	if (seg_count < 1)
  50		return ICE_DDP_PKG_INVALID_FILE;
  51
  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;
  55
  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;
  60
  61		/* segment header must fit */
  62		if (len < off + sizeof(*seg))
  63			return ICE_DDP_PKG_INVALID_FILE;
  64
  65		seg = (struct ice_generic_seg_hdr *)((u8 *)pkg + off);
  66
  67		/* segment body must fit */
  68		if (len < off + le32_to_cpu(seg->seg_size))
  69			return ICE_DDP_PKG_INVALID_FILE;
  70	}
  71
  72	return ICE_DDP_PKG_SUCCESS;
  73}
  74
  75/**
  76 * ice_free_seg - free package segment pointer
  77 * @hw: pointer to the hardware structure
  78 *
  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.
  81 */
  82void ice_free_seg(struct ice_hw *hw)
  83{
  84	if (hw->pkg_copy) {
  85		devm_kfree(ice_hw_to_dev(hw), hw->pkg_copy);
  86		hw->pkg_copy = NULL;
  87		hw->pkg_size = 0;
  88	}
  89	hw->seg = NULL;
  90}
  91
  92/**
  93 * ice_chk_pkg_version - check package version for compatibility with driver
  94 * @pkg_ver: pointer to a version structure to check
  95 *
  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
  99 * definitions.
 100 */
 101static enum ice_ddp_state ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
 102{
 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;
 111
 112	return ICE_DDP_PKG_SUCCESS;
 113}
 114
 115/**
 116 * ice_pkg_val_buf
 117 * @buf: pointer to the ice buffer
 118 *
 119 * This helper function validates a buffer's header.
 120 */
 121static struct ice_buf_hdr *ice_pkg_val_buf(struct ice_buf *buf)
 122{
 123	struct ice_buf_hdr *hdr;
 124	u16 section_count;
 125	u16 data_end;
 126
 127	hdr = (struct ice_buf_hdr *)buf->buf;
 128	/* verify data */
 129	section_count = le16_to_cpu(hdr->section_count);
 130	if (section_count < ICE_MIN_S_COUNT || section_count > ICE_MAX_S_COUNT)
 131		return NULL;
 132
 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)
 135		return NULL;
 136
 137	return hdr;
 138}
 139
 140/**
 141 * ice_find_buf_table
 142 * @ice_seg: pointer to the ice segment
 143 *
 144 * Returns the address of the buffer table within the ice segment.
 145 */
 146static struct ice_buf_table *ice_find_buf_table(struct ice_seg *ice_seg)
 147{
 148	struct ice_nvm_table *nvms = (struct ice_nvm_table *)
 149		(ice_seg->device_table + le32_to_cpu(ice_seg->device_table_count));
 150
 151	return (__force struct ice_buf_table *)(nvms->vers +
 152						le32_to_cpu(nvms->table_count));
 153}
 154
 155/**
 156 * ice_pkg_enum_buf
 157 * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
 158 * @state: pointer to the enum state
 159 *
 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).
 166 */
 167static struct ice_buf_hdr *ice_pkg_enum_buf(struct ice_seg *ice_seg,
 168					    struct ice_pkg_enum *state)
 169{
 170	if (ice_seg) {
 171		state->buf_table = ice_find_buf_table(ice_seg);
 172		if (!state->buf_table)
 173			return NULL;
 174
 175		state->buf_idx = 0;
 176		return ice_pkg_val_buf(state->buf_table->buf_array);
 177	}
 178
 179	if (++state->buf_idx < le32_to_cpu(state->buf_table->buf_count))
 180		return ice_pkg_val_buf(state->buf_table->buf_array +
 181				       state->buf_idx);
 182	else
 183		return NULL;
 184}
 185
 186/**
 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
 190 *
 191 * This helper function will advance the section within the ice segment,
 192 * also advancing the buffer if needed.
 193 */
 194static bool ice_pkg_advance_sect(struct ice_seg *ice_seg,
 195				 struct ice_pkg_enum *state)
 196{
 197	if (!ice_seg && !state->buf)
 198		return false;
 199
 200	if (!ice_seg && state->buf)
 201		if (++state->sect_idx < le16_to_cpu(state->buf->section_count))
 202			return true;
 203
 204	state->buf = ice_pkg_enum_buf(ice_seg, state);
 205	if (!state->buf)
 206		return false;
 207
 208	/* start of new buffer, reset section index */
 209	state->sect_idx = 0;
 210	return true;
 211}
 212
 213/**
 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
 218 *
 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.
 224 */
 225void *ice_pkg_enum_section(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
 226			   u32 sect_type)
 227{
 228	u16 offset, size;
 229
 230	if (ice_seg)
 231		state->type = sect_type;
 232
 233	if (!ice_pkg_advance_sect(ice_seg, state))
 234		return NULL;
 235
 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))
 240			return NULL;
 241
 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)
 245		return NULL;
 246
 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)
 249		return NULL;
 250
 251	/* make sure the section fits in the buffer */
 252	if (offset + size > ICE_PKG_BUF_SIZE)
 253		return NULL;
 254
 255	state->sect_type =
 256		le32_to_cpu(state->buf->section_entry[state->sect_idx].type);
 257
 258	/* calc pointer to this section */
 259	state->sect =
 260		((u8 *)state->buf) +
 261		le16_to_cpu(state->buf->section_entry[state->sect_idx].offset);
 262
 263	return state->sect;
 264}
 265
 266/**
 267 * ice_pkg_enum_entry
 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
 273 *
 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
 278 * been reached.
 279 *
 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
 282 * section.
 283 *
 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.
 290 */
 291static void *ice_pkg_enum_entry(struct ice_seg *ice_seg,
 292				struct ice_pkg_enum *state, u32 sect_type,
 293				u32 *offset,
 294				void *(*handler)(u32 sect_type, void *section,
 295						 u32 index, u32 *offset))
 296{
 297	void *entry;
 298
 299	if (ice_seg) {
 300		if (!handler)
 301			return NULL;
 302
 303		if (!ice_pkg_enum_section(ice_seg, state, sect_type))
 304			return NULL;
 305
 306		state->entry_idx = 0;
 307		state->handler = handler;
 308	} else {
 309		state->entry_idx++;
 310	}
 311
 312	if (!state->handler)
 313		return NULL;
 314
 315	/* get entry */
 316	entry = state->handler(state->sect_type, state->sect, state->entry_idx,
 317			       offset);
 318	if (!entry) {
 319		/* end of a section, look for another section of this type */
 320		if (!ice_pkg_enum_section(NULL, state, 0))
 321			return NULL;
 322
 323		state->entry_idx = 0;
 324		entry = state->handler(state->sect_type, state->sect,
 325				       state->entry_idx, offset);
 326	}
 327
 328	return entry;
 329}
 330
 331/**
 332 * ice_sw_fv_handler
 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
 337 *
 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.
 341 */
 342static void *ice_sw_fv_handler(u32 sect_type, void *section, u32 index,
 343			       u32 *offset)
 344{
 345	struct ice_sw_fv_section *fv_section = section;
 346
 347	if (!section || sect_type != ICE_SID_FLD_VEC_SW)
 348		return NULL;
 349	if (index >= le16_to_cpu(fv_section->count))
 350		return NULL;
 351	if (offset)
 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
 356		 */
 357		*offset = le16_to_cpu(fv_section->base_offset) + index;
 358	return fv_section->fv + index;
 359}
 360
 361/**
 362 * ice_get_prof_index_max - get the max profile index for used profile
 363 * @hw: pointer to the HW struct
 364 *
 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.
 368 */
 369static int ice_get_prof_index_max(struct ice_hw *hw)
 370{
 371	u16 prof_index = 0, j, max_prof_index = 0;
 372	struct ice_pkg_enum state;
 373	struct ice_seg *ice_seg;
 374	bool flag = false;
 375	struct ice_fv *fv;
 376	u32 offset;
 377
 378	memset(&state, 0, sizeof(state));
 379
 380	if (!hw->seg)
 381		return -EINVAL;
 382
 383	ice_seg = hw->seg;
 384
 385	do {
 386		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 387					&offset, ice_sw_fv_handler);
 388		if (!fv)
 389			break;
 390		ice_seg = NULL;
 391
 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.
 394		 */
 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)
 398				flag = true;
 399		if (flag && prof_index > max_prof_index)
 400			max_prof_index = prof_index;
 401
 402		prof_index++;
 403		flag = false;
 404	} while (fv);
 405
 406	hw->switch_info->max_used_prof_index = max_prof_index;
 407
 408	return 0;
 409}
 410
 411/**
 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
 415 */
 416static enum ice_ddp_state ice_get_ddp_pkg_state(struct ice_hw *hw,
 417						bool already_loaded)
 418{
 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))) {
 424		if (already_loaded)
 425			return ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED;
 426		else
 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;
 434	} else {
 435		return ICE_DDP_PKG_ERR;
 436	}
 437}
 438
 439/**
 440 * ice_init_pkg_regs - initialize additional package registers
 441 * @hw: pointer to the hardware structure
 442 */
 443static void ice_init_pkg_regs(struct ice_hw *hw)
 444{
 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
 448
 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);
 452}
 453
 454/**
 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
 460 *
 461 * This is a callback function that can be passed to ice_pkg_enum_entry.
 462 * Handles enumeration of individual Marker PType TCAM entries.
 463 */
 464static void *ice_marker_ptype_tcam_handler(u32 sect_type, void *section,
 465					   u32 index, u32 *offset)
 466{
 467	struct ice_marker_ptype_tcam_section *marker_ptype;
 468
 469	if (sect_type != ICE_SID_RXPARSER_MARKER_PTYPE)
 470		return NULL;
 471
 472	if (index > ICE_MAX_MARKER_PTYPE_TCAMS_IN_BUF)
 473		return NULL;
 474
 475	if (offset)
 476		*offset = 0;
 477
 478	marker_ptype = section;
 479	if (index >= le16_to_cpu(marker_ptype->count))
 480		return NULL;
 481
 482	return marker_ptype->tcam + index;
 483}
 484
 485/**
 486 * ice_add_dvm_hint
 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
 490 */
 491static void ice_add_dvm_hint(struct ice_hw *hw, u16 val, bool enable)
 492{
 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;
 496		hw->dvm_upd.count++;
 497	}
 498}
 499
 500/**
 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
 505 */
 506static void ice_add_tunnel_hint(struct ice_hw *hw, char *label_name, u16 val)
 507{
 508	if (hw->tnl.count < ICE_TUNNEL_MAX_ENTRIES) {
 509		u16 i;
 510
 511		for (i = 0; tnls[i].type != TNL_LAST; i++) {
 512			size_t len = strlen(tnls[i].label_prefix);
 513
 514			/* Look for matching label start, before continuing */
 515			if (strncmp(label_name, tnls[i].label_prefix, len))
 516				continue;
 517
 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.
 521			 */
 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;
 527				hw->tnl.count++;
 528				break;
 529			}
 530		}
 531	}
 532}
 533
 534/**
 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
 540 *
 541 * This is a callback function that can be passed to ice_pkg_enum_entry.
 542 * Handles enumeration of individual label entries.
 543 */
 544static void *ice_label_enum_handler(u32 __always_unused sect_type,
 545				    void *section, u32 index, u32 *offset)
 546{
 547	struct ice_label_section *labels;
 548
 549	if (!section)
 550		return NULL;
 551
 552	if (index > ICE_MAX_LABELS_IN_BUF)
 553		return NULL;
 554
 555	if (offset)
 556		*offset = 0;
 557
 558	labels = section;
 559	if (index >= le16_to_cpu(labels->count))
 560		return NULL;
 561
 562	return labels->label + index;
 563}
 564
 565/**
 566 * ice_enum_labels
 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
 571 *
 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.
 576 */
 577static char *ice_enum_labels(struct ice_seg *ice_seg, u32 type,
 578			     struct ice_pkg_enum *state, u16 *value)
 579{
 580	struct ice_label *label;
 581
 582	/* Check for valid label section on first call */
 583	if (type && !(type >= ICE_SID_LBL_FIRST && type <= ICE_SID_LBL_LAST))
 584		return NULL;
 585
 586	label = ice_pkg_enum_entry(ice_seg, state, type, NULL,
 587				   ice_label_enum_handler);
 588	if (!label)
 589		return NULL;
 590
 591	*value = le16_to_cpu(label->value);
 592	return label->name;
 593}
 594
 595/**
 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
 601 *
 602 * This is a callback function that can be passed to ice_pkg_enum_entry.
 603 * Handles enumeration of individual boost TCAM entries.
 604 */
 605static void *ice_boost_tcam_handler(u32 sect_type, void *section, u32 index,
 606				    u32 *offset)
 607{
 608	struct ice_boost_tcam_section *boost;
 609
 610	if (!section)
 611		return NULL;
 612
 613	if (sect_type != ICE_SID_RXPARSER_BOOST_TCAM)
 614		return NULL;
 615
 616	if (index > ICE_MAX_BST_TCAMS_IN_BUF)
 617		return NULL;
 618
 619	if (offset)
 620		*offset = 0;
 621
 622	boost = section;
 623	if (index >= le16_to_cpu(boost->count))
 624		return NULL;
 625
 626	return boost->tcam + index;
 627}
 628
 629/**
 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
 634 *
 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.
 638 */
 639static int ice_find_boost_entry(struct ice_seg *ice_seg, u16 addr,
 640				struct ice_boost_tcam_entry **entry)
 641{
 642	struct ice_boost_tcam_entry *tcam;
 643	struct ice_pkg_enum state;
 644
 645	memset(&state, 0, sizeof(state));
 646
 647	if (!ice_seg)
 648		return -EINVAL;
 649
 650	do {
 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) {
 655			*entry = tcam;
 656			return 0;
 657		}
 658
 659		ice_seg = NULL;
 660	} while (tcam);
 661
 662	*entry = NULL;
 663	return -EIO;
 664}
 665
 666/**
 667 * ice_is_init_pkg_successful - check if DDP init was successful
 668 * @state: state of the DDP pkg after download
 669 */
 670bool ice_is_init_pkg_successful(enum ice_ddp_state state)
 671{
 672	switch (state) {
 673	case ICE_DDP_PKG_SUCCESS:
 674	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
 675	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
 676		return true;
 677	default:
 678		return false;
 679	}
 680}
 681
 682/**
 683 * ice_pkg_buf_alloc
 684 * @hw: pointer to the HW structure
 685 *
 686 * Allocates a package buffer and returns a pointer to the buffer header.
 687 * Note: all package contents must be in Little Endian form.
 688 */
 689struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
 690{
 691	struct ice_buf_build *bld;
 692	struct ice_buf_hdr *buf;
 693
 694	bld = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*bld), GFP_KERNEL);
 695	if (!bld)
 696		return NULL;
 697
 698	buf = (struct ice_buf_hdr *)bld;
 699	buf->data_end =
 700		cpu_to_le16(offsetof(struct ice_buf_hdr, section_entry));
 701	return bld;
 702}
 703
 704static bool ice_is_gtp_u_profile(u16 prof_idx)
 705{
 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;
 709}
 710
 711static bool ice_is_gtp_c_profile(u16 prof_idx)
 712{
 713	switch (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:
 718		return true;
 719	default:
 720		return false;
 721	}
 722}
 723
 
 
 
 
 
 
 724/**
 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
 729 */
 730static enum ice_prof_type ice_get_sw_prof_type(struct ice_hw *hw,
 731					       struct ice_fv *fv, u32 prof_idx)
 732{
 733	u16 i;
 734
 735	if (ice_is_gtp_c_profile(prof_idx))
 736		return ICE_PROF_TUN_GTPC;
 737
 738	if (ice_is_gtp_u_profile(prof_idx))
 739		return ICE_PROF_TUN_GTPU;
 740
 
 
 
 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;
 746
 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;
 750	}
 751
 752	return ICE_PROF_NON_TUN;
 753}
 754
 755/**
 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
 760 */
 761void ice_get_sw_fv_bitmap(struct ice_hw *hw, enum ice_prof_type req_profs,
 762			  unsigned long *bm)
 763{
 764	struct ice_pkg_enum state;
 765	struct ice_seg *ice_seg;
 766	struct ice_fv *fv;
 767
 768	if (req_profs == ICE_PROF_ALL) {
 769		bitmap_set(bm, 0, ICE_MAX_NUM_PROFILES);
 770		return;
 771	}
 772
 773	memset(&state, 0, sizeof(state));
 774	bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
 775	ice_seg = hw->seg;
 776	do {
 777		enum ice_prof_type prof_type;
 778		u32 offset;
 779
 780		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 781					&offset, ice_sw_fv_handler);
 782		ice_seg = NULL;
 783
 784		if (fv) {
 785			/* Determine field vector type */
 786			prof_type = ice_get_sw_prof_type(hw, fv, offset);
 787
 788			if (req_profs & prof_type)
 789				set_bit((u16)offset, bm);
 790		}
 791	} while (fv);
 792}
 793
 794/**
 795 * ice_get_sw_fv_list
 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
 800 *
 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.
 807 */
 808int ice_get_sw_fv_list(struct ice_hw *hw, struct ice_prot_lkup_ext *lkups,
 809		       unsigned long *bm, struct list_head *fv_list)
 810{
 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;
 815	struct ice_fv *fv;
 816	u32 offset;
 817
 818	memset(&state, 0, sizeof(state));
 819
 820	if (!lkups->n_val_words || !hw->seg)
 821		return -EINVAL;
 822
 823	ice_seg = hw->seg;
 824	do {
 825		u16 i;
 826
 827		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 828					&offset, ice_sw_fv_handler);
 829		if (!fv)
 830			break;
 831		ice_seg = NULL;
 832
 833		/* If field vector is not in the bitmap list, then skip this
 834		 * profile.
 835		 */
 836		if (!test_bit((u16)offset, bm))
 837			continue;
 838
 839		for (i = 0; i < lkups->n_val_words; i++) {
 840			int j;
 841
 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)
 846					break;
 847			if (j >= hw->blk[ICE_BLK_SW].es.fvw)
 848				break;
 849			if (i + 1 == lkups->n_val_words) {
 850				fvl = devm_kzalloc(ice_hw_to_dev(hw),
 851						   sizeof(*fvl), GFP_KERNEL);
 852				if (!fvl)
 853					goto err;
 854				fvl->fv_ptr = fv;
 855				fvl->profile_id = offset;
 856				list_add(&fvl->list_entry, fv_list);
 857				break;
 858			}
 859		}
 860	} while (fv);
 861	if (list_empty(fv_list)) {
 862		dev_warn(ice_hw_to_dev(hw),
 863			 "Required profiles not found in currently loaded DDP package");
 864		return -EIO;
 865	}
 866
 867	return 0;
 868
 869err:
 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);
 873	}
 874
 875	return -ENOMEM;
 876}
 877
 878/**
 879 * ice_init_prof_result_bm - Initialize the profile result index bitmap
 880 * @hw: pointer to hardware structure
 881 */
 882void ice_init_prof_result_bm(struct ice_hw *hw)
 883{
 884	struct ice_pkg_enum state;
 885	struct ice_seg *ice_seg;
 886	struct ice_fv *fv;
 887
 888	memset(&state, 0, sizeof(state));
 889
 890	if (!hw->seg)
 891		return;
 892
 893	ice_seg = hw->seg;
 894	do {
 895		u32 off;
 896		u16 i;
 897
 898		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
 899					&off, ice_sw_fv_handler);
 900		ice_seg = NULL;
 901		if (!fv)
 902			break;
 903
 904		bitmap_zero(hw->switch_info->prof_res_bm[off],
 905			    ICE_MAX_FV_WORDS);
 906
 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.
 910		 */
 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]);
 915	} while (fv);
 916}
 917
 918/**
 919 * ice_pkg_buf_free
 920 * @hw: pointer to the HW structure
 921 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
 922 *
 923 * Frees a package buffer
 924 */
 925void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
 926{
 927	devm_kfree(ice_hw_to_dev(hw), bld);
 928}
 929
 930/**
 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
 934 *
 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.
 942 */
 943int ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
 944{
 945	struct ice_buf_hdr *buf;
 946	u16 section_count;
 947	u16 data_end;
 948
 949	if (!bld)
 950		return -EINVAL;
 951
 952	buf = (struct ice_buf_hdr *)&bld->buf;
 953
 954	/* already an active section, can't increase table size */
 955	section_count = le16_to_cpu(buf->section_count);
 956	if (section_count > 0)
 957		return -EIO;
 958
 959	if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
 960		return -EIO;
 961	bld->reserved_section_table_entries += count;
 962
 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);
 966
 967	return 0;
 968}
 969
 970/**
 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)
 975 *
 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
 979 * section contents.
 980 * Note: all package contents must be in Little Endian form.
 981 */
 982void *ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
 983{
 984	struct ice_buf_hdr *buf;
 985	u16 sect_count;
 986	u16 data_end;
 987
 988	if (!bld || !type || !size)
 989		return NULL;
 990
 991	buf = (struct ice_buf_hdr *)&bld->buf;
 992
 993	/* check for enough space left in buffer */
 994	data_end = le16_to_cpu(buf->data_end);
 995
 996	/* section start must align on 4 byte boundary */
 997	data_end = ALIGN(data_end, 4);
 998
 999	if ((data_end + size) > ICE_MAX_S_DATA_END)
1000		return NULL;
1001
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;
1006
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);
1010
1011		data_end += size;
1012		buf->data_end = cpu_to_le16(data_end);
1013
1014		buf->section_count = cpu_to_le16(sect_count + 1);
1015		return section_ptr;
1016	}
1017
1018	/* no free section table entries */
1019	return NULL;
1020}
1021
1022/**
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
1028 *
1029 * Allocates a package buffer with a single section.
1030 * Note: all package contents must be in Little Endian form.
1031 */
1032struct ice_buf_build *ice_pkg_buf_alloc_single_section(struct ice_hw *hw,
1033						       u32 type, u16 size,
1034						       void **section)
1035{
1036	struct ice_buf_build *buf;
1037
1038	if (!section)
1039		return NULL;
1040
1041	buf = ice_pkg_buf_alloc(hw);
1042	if (!buf)
1043		return NULL;
1044
1045	if (ice_pkg_buf_reserve_section(buf, 1))
1046		goto ice_pkg_buf_alloc_single_section_err;
1047
1048	*section = ice_pkg_buf_alloc_section(buf, type, size);
1049	if (!*section)
1050		goto ice_pkg_buf_alloc_single_section_err;
1051
1052	return buf;
1053
1054ice_pkg_buf_alloc_single_section_err:
1055	ice_pkg_buf_free(hw, buf);
1056	return NULL;
1057}
1058
1059/**
1060 * ice_pkg_buf_get_active_sections
1061 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1062 *
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
1066 * not be used.
1067 * Note: all package contents must be in Little Endian form.
1068 */
1069u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
1070{
1071	struct ice_buf_hdr *buf;
1072
1073	if (!bld)
1074		return 0;
1075
1076	buf = (struct ice_buf_hdr *)&bld->buf;
1077	return le16_to_cpu(buf->section_count);
1078}
1079
1080/**
1081 * ice_pkg_buf
1082 * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1083 *
1084 * Return a pointer to the buffer's header
1085 */
1086struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
1087{
1088	if (!bld)
1089		return NULL;
1090
1091	return &bld->buf;
1092}
1093
1094static enum ice_ddp_state ice_map_aq_err_to_ddp_state(enum ice_aq_err aq_err)
1095{
1096	switch (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;
1105	default:
1106		return ICE_DDP_PKG_ERR;
1107	}
1108}
1109
1110/**
1111 * ice_acquire_global_cfg_lock
1112 * @hw: pointer to the HW structure
1113 * @access: access type (read or write)
1114 *
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:
1118 *
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.
1125 */
1126static int ice_acquire_global_cfg_lock(struct ice_hw *hw,
1127				       enum ice_aq_res_access_type access)
1128{
1129	int status;
1130
1131	status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
1132				 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
1133
1134	if (!status)
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");
1139
1140	return status;
1141}
1142
1143/**
1144 * ice_release_global_cfg_lock
1145 * @hw: pointer to the HW structure
1146 *
1147 * This function will release the global config lock.
1148 */
1149static void ice_release_global_cfg_lock(struct ice_hw *hw)
1150{
1151	mutex_unlock(&ice_global_cfg_lock_sw);
1152	ice_release_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID);
1153}
1154
1155/**
1156 * ice_aq_download_pkg
1157 * @hw: pointer to the hardware structure
1158 * @pkg_buf: the package buffer to transfer
1159 * @buf_size: the size of the package buffer
1160 * @last_buf: last buffer indicator
1161 * @error_offset: returns error offset
1162 * @error_info: returns error information
1163 * @cd: pointer to command details structure or NULL
1164 *
1165 * Download Package (0x0C40)
1166 */
1167static int
1168ice_aq_download_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1169		    u16 buf_size, bool last_buf, u32 *error_offset,
1170		    u32 *error_info, struct ice_sq_cd *cd)
1171{
1172	struct ice_aqc_download_pkg *cmd;
1173	struct ice_aq_desc desc;
1174	int status;
1175
1176	if (error_offset)
1177		*error_offset = 0;
1178	if (error_info)
1179		*error_info = 0;
1180
1181	cmd = &desc.params.download_pkg;
1182	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_download_pkg);
1183	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1184
1185	if (last_buf)
1186		cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1187
1188	status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1189	if (status == -EIO) {
1190		/* Read error from buffer only when the FW returned an error */
1191		struct ice_aqc_download_pkg_resp *resp;
1192
1193		resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1194		if (error_offset)
1195			*error_offset = le32_to_cpu(resp->error_offset);
1196		if (error_info)
1197			*error_info = le32_to_cpu(resp->error_info);
1198	}
1199
1200	return status;
1201}
1202
1203/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1204 * ice_get_pkg_seg_by_idx
1205 * @pkg_hdr: pointer to the package header to be searched
1206 * @idx: index of segment
1207 */
1208static struct ice_generic_seg_hdr *
1209ice_get_pkg_seg_by_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx)
1210{
1211	if (idx < le32_to_cpu(pkg_hdr->seg_count))
1212		return (struct ice_generic_seg_hdr *)
1213			((u8 *)pkg_hdr +
1214			 le32_to_cpu(pkg_hdr->seg_offset[idx]));
1215
1216	return NULL;
1217}
1218
1219/**
1220 * ice_is_signing_seg_at_idx - determine if segment is a signing segment
1221 * @pkg_hdr: pointer to package header
1222 * @idx: segment index
1223 */
1224static bool ice_is_signing_seg_at_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx)
1225{
1226	struct ice_generic_seg_hdr *seg;
1227
1228	seg = ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1229	if (!seg)
1230		return false;
1231
1232	return le32_to_cpu(seg->seg_type) == SEGMENT_TYPE_SIGNING;
1233}
1234
1235/**
1236 * ice_is_signing_seg_type_at_idx
1237 * @pkg_hdr: pointer to package header
1238 * @idx: segment index
1239 * @seg_id: segment id that is expected
1240 * @sign_type: signing type
1241 *
1242 * Determine if a segment is a signing segment of the correct type
1243 */
1244static bool
1245ice_is_signing_seg_type_at_idx(struct ice_pkg_hdr *pkg_hdr, u32 idx,
1246			       u32 seg_id, u32 sign_type)
1247{
1248	struct ice_sign_seg *seg;
1249
1250	if (!ice_is_signing_seg_at_idx(pkg_hdr, idx))
1251		return false;
1252
1253	seg = (struct ice_sign_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1254
1255	if (seg && le32_to_cpu(seg->seg_id) == seg_id &&
1256	    le32_to_cpu(seg->sign_type) == sign_type)
1257		return true;
1258
1259	return false;
1260}
1261
1262/**
1263 * ice_is_buffer_metadata - determine if package buffer is a metadata buffer
1264 * @buf: pointer to buffer header
1265 */
1266static bool ice_is_buffer_metadata(struct ice_buf_hdr *buf)
1267{
1268	if (le32_to_cpu(buf->section_entry[0].type) & ICE_METADATA_BUF)
1269		return true;
1270
1271	return false;
1272}
1273
1274/**
1275 * ice_is_last_download_buffer
1276 * @buf: pointer to current buffer header
1277 * @idx: index of the buffer in the current sequence
1278 * @count: the buffer count in the current sequence
1279 *
1280 * Note: this routine should only be called if the buffer is not the last buffer
1281 */
1282static bool
1283ice_is_last_download_buffer(struct ice_buf_hdr *buf, u32 idx, u32 count)
1284{
1285	struct ice_buf *next_buf;
1286
1287	if ((idx + 1) == count)
1288		return true;
1289
1290	/* A set metadata flag in the next buffer will signal that the current
1291	 * buffer will be the last buffer downloaded
1292	 */
1293	next_buf = ((struct ice_buf *)buf) + 1;
1294
1295	return ice_is_buffer_metadata((struct ice_buf_hdr *)next_buf);
1296}
1297
1298/**
1299 * ice_dwnld_cfg_bufs_no_lock
1300 * @hw: pointer to the hardware structure
1301 * @bufs: pointer to an array of buffers
1302 * @start: buffer index of first buffer to download
1303 * @count: the number of buffers to download
1304 * @indicate_last: if true, then set last buffer flag on last buffer download
1305 *
1306 * Downloads package configuration buffers to the firmware. Metadata buffers
1307 * are skipped, and the first metadata buffer found indicates that the rest
1308 * of the buffers are all metadata buffers.
1309 */
1310static enum ice_ddp_state
1311ice_dwnld_cfg_bufs_no_lock(struct ice_hw *hw, struct ice_buf *bufs, u32 start,
1312			   u32 count, bool indicate_last)
1313{
1314	enum ice_ddp_state state = ICE_DDP_PKG_SUCCESS;
1315	struct ice_buf_hdr *bh;
1316	enum ice_aq_err err;
1317	u32 offset, info, i;
1318
1319	if (!bufs || !count)
1320		return ICE_DDP_PKG_ERR;
1321
1322	/* If the first buffer's first section has its metadata bit set
1323	 * then there are no buffers to be downloaded, and the operation is
1324	 * considered a success.
1325	 */
1326	bh = (struct ice_buf_hdr *)(bufs + start);
1327	if (le32_to_cpu(bh->section_entry[0].type) & ICE_METADATA_BUF)
1328		return ICE_DDP_PKG_SUCCESS;
1329
1330	for (i = 0; i < count; i++) {
1331		bool last = false;
1332		int status;
1333
1334		bh = (struct ice_buf_hdr *)(bufs + start + i);
1335
1336		if (indicate_last)
1337			last = ice_is_last_download_buffer(bh, i, count);
1338
1339		status = ice_aq_download_pkg(hw, bh, ICE_PKG_BUF_SIZE, last,
1340					     &offset, &info, NULL);
1341
1342		/* Save AQ status from download package */
1343		if (status) {
1344			ice_debug(hw, ICE_DBG_PKG, "Pkg download failed: err %d off %d inf %d\n",
1345				  status, offset, info);
1346			err = hw->adminq.sq_last_status;
1347			state = ice_map_aq_err_to_ddp_state(err);
1348			break;
1349		}
1350
1351		if (last)
1352			break;
1353	}
1354
1355	return state;
1356}
1357
1358/**
1359 * ice_download_pkg_sig_seg - download a signature segment
1360 * @hw: pointer to the hardware structure
1361 * @seg: pointer to signature segment
1362 */
1363static enum ice_ddp_state
1364ice_download_pkg_sig_seg(struct ice_hw *hw, struct ice_sign_seg *seg)
1365{
1366	return  ice_dwnld_cfg_bufs_no_lock(hw, seg->buf_tbl.buf_array, 0,
1367					   le32_to_cpu(seg->buf_tbl.buf_count),
1368					   false);
1369}
1370
1371/**
1372 * ice_download_pkg_config_seg - download a config segment
1373 * @hw: pointer to the hardware structure
1374 * @pkg_hdr: pointer to package header
1375 * @idx: segment index
1376 * @start: starting buffer
1377 * @count: buffer count
1378 *
1379 * Note: idx must reference a ICE segment
1380 */
1381static enum ice_ddp_state
1382ice_download_pkg_config_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr,
1383			    u32 idx, u32 start, u32 count)
 
1384{
1385	struct ice_buf_table *bufs;
1386	struct ice_seg *seg;
1387	u32 buf_count;
1388
1389	seg = (struct ice_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1390	if (!seg)
1391		return ICE_DDP_PKG_ERR;
1392
1393	bufs = ice_find_buf_table(seg);
1394	buf_count = le32_to_cpu(bufs->buf_count);
1395
1396	if (start >= buf_count || start + count > buf_count)
1397		return ICE_DDP_PKG_ERR;
1398
1399	return  ice_dwnld_cfg_bufs_no_lock(hw, bufs->buf_array, start, count,
1400					   true);
 
 
 
 
 
1401}
1402
1403/**
1404 * ice_dwnld_sign_and_cfg_segs - download a signing segment and config segment
1405 * @hw: pointer to the hardware structure
1406 * @pkg_hdr: pointer to package header
1407 * @idx: segment index (must be a signature segment)
1408 *
1409 * Note: idx must reference a signature segment
1410 */
1411static enum ice_ddp_state
1412ice_dwnld_sign_and_cfg_segs(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr,
1413			    u32 idx)
1414{
 
1415	enum ice_ddp_state state;
1416	struct ice_sign_seg *seg;
1417	u32 conf_idx;
1418	u32 start;
1419	u32 count;
1420
1421	seg = (struct ice_sign_seg *)ice_get_pkg_seg_by_idx(pkg_hdr, idx);
1422	if (!seg) {
1423		state = ICE_DDP_PKG_ERR;
1424		goto exit;
 
1425	}
1426
 
 
 
 
 
1427	conf_idx = le32_to_cpu(seg->signed_seg_idx);
1428	start = le32_to_cpu(seg->signed_buf_start);
1429	count = le32_to_cpu(seg->signed_buf_count);
1430
1431	state = ice_download_pkg_sig_seg(hw, seg);
1432	if (state)
1433		goto exit;
1434
1435	state = ice_download_pkg_config_seg(hw, pkg_hdr, conf_idx, start,
1436					    count);
 
 
 
 
1437
1438exit:
1439	return state;
1440}
1441
1442/**
1443 * ice_match_signing_seg - determine if a matching signing segment exists
1444 * @pkg_hdr: pointer to package header
1445 * @seg_id: segment id that is expected
1446 * @sign_type: signing type
1447 */
1448static bool
1449ice_match_signing_seg(struct ice_pkg_hdr *pkg_hdr, u32 seg_id, u32 sign_type)
1450{
1451	u32 i;
1452
1453	for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1454		if (ice_is_signing_seg_type_at_idx(pkg_hdr, i, seg_id,
1455						   sign_type))
1456			return true;
1457	}
1458
1459	return false;
1460}
1461
1462/**
1463 * ice_post_dwnld_pkg_actions - perform post download package actions
1464 * @hw: pointer to the hardware structure
1465 */
1466static enum ice_ddp_state
1467ice_post_dwnld_pkg_actions(struct ice_hw *hw)
1468{
1469	int status;
1470
1471	status = ice_set_vlan_mode(hw);
1472	if (status) {
1473		ice_debug(hw, ICE_DBG_PKG, "Failed to set VLAN mode: err %d\n",
1474			  status);
1475		return ICE_DDP_PKG_ERR;
1476	}
1477
1478	return ICE_DDP_PKG_SUCCESS;
1479}
1480
1481/**
1482 * ice_download_pkg_with_sig_seg
1483 * @hw: pointer to the hardware structure
1484 * @pkg_hdr: pointer to package header
1485 *
1486 * Handles the download of a complete package.
1487 */
1488static enum ice_ddp_state
1489ice_download_pkg_with_sig_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1490{
1491	enum ice_aq_err aq_err = hw->adminq.sq_last_status;
1492	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
 
1493	int status;
1494	u32 i;
1495
1496	ice_debug(hw, ICE_DBG_INIT, "Segment ID %d\n", hw->pkg_seg_id);
1497	ice_debug(hw, ICE_DBG_INIT, "Signature type %d\n", hw->pkg_sign_type);
1498
1499	status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1500	if (status) {
1501		if (status == -EALREADY)
1502			state = ICE_DDP_PKG_ALREADY_LOADED;
1503		else
1504			state = ice_map_aq_err_to_ddp_state(aq_err);
1505		return state;
1506	}
1507
1508	for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1509		if (!ice_is_signing_seg_type_at_idx(pkg_hdr, i, hw->pkg_seg_id,
1510						    hw->pkg_sign_type))
1511			continue;
1512
1513		state = ice_dwnld_sign_and_cfg_segs(hw, pkg_hdr, i);
1514		if (state)
1515			break;
1516	}
1517
1518	if (!state)
1519		state = ice_post_dwnld_pkg_actions(hw);
1520
1521	ice_release_global_cfg_lock(hw);
1522
1523	return state;
1524}
1525
1526/**
1527 * ice_dwnld_cfg_bufs
1528 * @hw: pointer to the hardware structure
1529 * @bufs: pointer to an array of buffers
1530 * @count: the number of buffers in the array
1531 *
1532 * Obtains global config lock and downloads the package configuration buffers
1533 * to the firmware.
1534 */
1535static enum ice_ddp_state
1536ice_dwnld_cfg_bufs(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1537{
 
1538	enum ice_ddp_state state;
1539	struct ice_buf_hdr *bh;
1540	int status;
1541
1542	if (!bufs || !count)
1543		return ICE_DDP_PKG_ERR;
1544
1545	/* If the first buffer's first section has its metadata bit set
1546	 * then there are no buffers to be downloaded, and the operation is
1547	 * considered a success.
1548	 */
1549	bh = (struct ice_buf_hdr *)bufs;
1550	if (le32_to_cpu(bh->section_entry[0].type) & ICE_METADATA_BUF)
1551		return ICE_DDP_PKG_SUCCESS;
1552
1553	status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1554	if (status) {
1555		if (status == -EALREADY)
1556			return ICE_DDP_PKG_ALREADY_LOADED;
1557		return ice_map_aq_err_to_ddp_state(hw->adminq.sq_last_status);
1558	}
1559
1560	state = ice_dwnld_cfg_bufs_no_lock(hw, bufs, 0, count, true);
 
 
1561	if (!state)
1562		state = ice_post_dwnld_pkg_actions(hw);
1563
1564	ice_release_global_cfg_lock(hw);
1565
1566	return state;
1567}
1568
1569/**
1570 * ice_download_pkg_without_sig_seg
1571 * @hw: pointer to the hardware structure
1572 * @ice_seg: pointer to the segment of the package to be downloaded
1573 *
1574 * Handles the download of a complete package without signature segment.
1575 */
1576static enum ice_ddp_state
1577ice_download_pkg_without_sig_seg(struct ice_hw *hw, struct ice_seg *ice_seg)
1578{
1579	struct ice_buf_table *ice_buf_tbl;
1580
1581	ice_debug(hw, ICE_DBG_PKG, "Segment format version: %d.%d.%d.%d\n",
1582		  ice_seg->hdr.seg_format_ver.major,
1583		  ice_seg->hdr.seg_format_ver.minor,
1584		  ice_seg->hdr.seg_format_ver.update,
1585		  ice_seg->hdr.seg_format_ver.draft);
1586
1587	ice_debug(hw, ICE_DBG_PKG, "Seg: type 0x%X, size %d, name %s\n",
1588		  le32_to_cpu(ice_seg->hdr.seg_type),
1589		  le32_to_cpu(ice_seg->hdr.seg_size), ice_seg->hdr.seg_id);
1590
1591	ice_buf_tbl = ice_find_buf_table(ice_seg);
1592
1593	ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1594		  le32_to_cpu(ice_buf_tbl->buf_count));
1595
1596	return ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1597				  le32_to_cpu(ice_buf_tbl->buf_count));
1598}
1599
1600/**
1601 * ice_download_pkg
1602 * @hw: pointer to the hardware structure
1603 * @pkg_hdr: pointer to package header
1604 * @ice_seg: pointer to the segment of the package to be downloaded
1605 *
1606 * Handles the download of a complete package.
1607 */
1608static enum ice_ddp_state
1609ice_download_pkg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr,
1610		 struct ice_seg *ice_seg)
1611{
1612	enum ice_ddp_state state;
1613
1614	if (hw->pkg_has_signing_seg)
1615		state = ice_download_pkg_with_sig_seg(hw, pkg_hdr);
1616	else
1617		state = ice_download_pkg_without_sig_seg(hw, ice_seg);
1618
1619	ice_post_pkg_dwnld_vlan_mode_cfg(hw);
1620
1621	return state;
1622}
1623
1624/**
1625 * ice_aq_get_pkg_info_list
1626 * @hw: pointer to the hardware structure
1627 * @pkg_info: the buffer which will receive the information list
1628 * @buf_size: the size of the pkg_info information buffer
1629 * @cd: pointer to command details structure or NULL
1630 *
1631 * Get Package Info List (0x0C43)
1632 */
1633static int ice_aq_get_pkg_info_list(struct ice_hw *hw,
1634				    struct ice_aqc_get_pkg_info_resp *pkg_info,
1635				    u16 buf_size, struct ice_sq_cd *cd)
1636{
1637	struct ice_aq_desc desc;
1638
1639	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1640
1641	return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1642}
1643
1644/**
1645 * ice_aq_update_pkg
1646 * @hw: pointer to the hardware structure
1647 * @pkg_buf: the package cmd buffer
1648 * @buf_size: the size of the package cmd buffer
1649 * @last_buf: last buffer indicator
1650 * @error_offset: returns error offset
1651 * @error_info: returns error information
1652 * @cd: pointer to command details structure or NULL
1653 *
1654 * Update Package (0x0C42)
1655 */
1656static int ice_aq_update_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1657			     u16 buf_size, bool last_buf, u32 *error_offset,
1658			     u32 *error_info, struct ice_sq_cd *cd)
1659{
1660	struct ice_aqc_download_pkg *cmd;
1661	struct ice_aq_desc desc;
1662	int status;
1663
1664	if (error_offset)
1665		*error_offset = 0;
1666	if (error_info)
1667		*error_info = 0;
1668
1669	cmd = &desc.params.download_pkg;
1670	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_pkg);
1671	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1672
1673	if (last_buf)
1674		cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
1675
1676	status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1677	if (status == -EIO) {
1678		/* Read error from buffer only when the FW returned an error */
1679		struct ice_aqc_download_pkg_resp *resp;
1680
1681		resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1682		if (error_offset)
1683			*error_offset = le32_to_cpu(resp->error_offset);
1684		if (error_info)
1685			*error_info = le32_to_cpu(resp->error_info);
1686	}
1687
1688	return status;
1689}
1690
1691/**
1692 * ice_aq_upload_section
1693 * @hw: pointer to the hardware structure
1694 * @pkg_buf: the package buffer which will receive the section
1695 * @buf_size: the size of the package buffer
1696 * @cd: pointer to command details structure or NULL
1697 *
1698 * Upload Section (0x0C41)
1699 */
1700int ice_aq_upload_section(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
1701			  u16 buf_size, struct ice_sq_cd *cd)
1702{
1703	struct ice_aq_desc desc;
1704
1705	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_upload_section);
1706	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1707
1708	return ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1709}
1710
1711/**
1712 * ice_update_pkg_no_lock
1713 * @hw: pointer to the hardware structure
1714 * @bufs: pointer to an array of buffers
1715 * @count: the number of buffers in the array
1716 */
1717int ice_update_pkg_no_lock(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1718{
1719	int status = 0;
1720	u32 i;
1721
1722	for (i = 0; i < count; i++) {
1723		struct ice_buf_hdr *bh = (struct ice_buf_hdr *)(bufs + i);
1724		bool last = ((i + 1) == count);
1725		u32 offset, info;
1726
1727		status = ice_aq_update_pkg(hw, bh, le16_to_cpu(bh->data_end),
1728					   last, &offset, &info, NULL);
1729
1730		if (status) {
1731			ice_debug(hw, ICE_DBG_PKG,
1732				  "Update pkg failed: err %d off %d inf %d\n",
1733				  status, offset, info);
1734			break;
1735		}
1736	}
1737
1738	return status;
1739}
1740
1741/**
1742 * ice_update_pkg
1743 * @hw: pointer to the hardware structure
1744 * @bufs: pointer to an array of buffers
1745 * @count: the number of buffers in the array
1746 *
1747 * Obtains change lock and updates package.
1748 */
1749int ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1750{
1751	int status;
1752
1753	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
1754	if (status)
1755		return status;
1756
1757	status = ice_update_pkg_no_lock(hw, bufs, count);
1758
1759	ice_release_change_lock(hw);
1760
1761	return status;
1762}
1763
1764/**
1765 * ice_find_seg_in_pkg
1766 * @hw: pointer to the hardware structure
1767 * @seg_type: the segment type to search for (i.e., SEGMENT_TYPE_CPK)
1768 * @pkg_hdr: pointer to the package header to be searched
1769 *
1770 * This function searches a package file for a particular segment type. On
1771 * success it returns a pointer to the segment header, otherwise it will
1772 * return NULL.
1773 */
1774static struct ice_generic_seg_hdr *
1775ice_find_seg_in_pkg(struct ice_hw *hw, u32 seg_type,
1776		    struct ice_pkg_hdr *pkg_hdr)
1777{
1778	u32 i;
1779
1780	ice_debug(hw, ICE_DBG_PKG, "Package format version: %d.%d.%d.%d\n",
1781		  pkg_hdr->pkg_format_ver.major, pkg_hdr->pkg_format_ver.minor,
1782		  pkg_hdr->pkg_format_ver.update,
1783		  pkg_hdr->pkg_format_ver.draft);
1784
1785	/* Search all package segments for the requested segment type */
1786	for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
1787		struct ice_generic_seg_hdr *seg;
1788
1789		seg = (struct ice_generic_seg_hdr
1790			       *)((u8 *)pkg_hdr +
1791				  le32_to_cpu(pkg_hdr->seg_offset[i]));
1792
1793		if (le32_to_cpu(seg->seg_type) == seg_type)
1794			return seg;
1795	}
1796
1797	return NULL;
1798}
1799
1800/**
1801 * ice_has_signing_seg - determine if package has a signing segment
1802 * @hw: pointer to the hardware structure
1803 * @pkg_hdr: pointer to the driver's package hdr
1804 */
1805static bool ice_has_signing_seg(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1806{
1807	struct ice_generic_seg_hdr *seg_hdr;
1808
1809	seg_hdr = (struct ice_generic_seg_hdr *)
1810		ice_find_seg_in_pkg(hw, SEGMENT_TYPE_SIGNING, pkg_hdr);
1811
1812	return seg_hdr ? true : false;
1813}
1814
1815/**
1816 * ice_get_pkg_segment_id - get correct package segment id, based on device
1817 * @mac_type: MAC type of the device
1818 */
1819static u32 ice_get_pkg_segment_id(enum ice_mac_type mac_type)
1820{
1821	u32 seg_id;
1822
1823	switch (mac_type) {
1824	case ICE_MAC_E830:
1825		seg_id = SEGMENT_TYPE_ICE_E830;
1826		break;
1827	case ICE_MAC_GENERIC:
 
1828	default:
1829		seg_id = SEGMENT_TYPE_ICE_E810;
1830		break;
1831	}
1832
1833	return seg_id;
1834}
1835
1836/**
1837 * ice_get_pkg_sign_type - get package segment sign type, based on device
1838 * @mac_type: MAC type of the device
1839 */
1840static u32 ice_get_pkg_sign_type(enum ice_mac_type mac_type)
1841{
1842	u32 sign_type;
1843
1844	switch (mac_type) {
1845	case ICE_MAC_E830:
1846		sign_type = SEGMENT_SIGN_TYPE_RSA3K_SBB;
1847		break;
 
 
 
1848	case ICE_MAC_GENERIC:
1849	default:
1850		sign_type = SEGMENT_SIGN_TYPE_RSA2K;
1851		break;
1852	}
1853
1854	return sign_type;
1855}
1856
1857/**
1858 * ice_get_signing_req - get correct package requirements, based on device
1859 * @hw: pointer to the hardware structure
1860 */
1861static void ice_get_signing_req(struct ice_hw *hw)
1862{
1863	hw->pkg_seg_id = ice_get_pkg_segment_id(hw->mac_type);
1864	hw->pkg_sign_type = ice_get_pkg_sign_type(hw->mac_type);
1865}
1866
1867/**
1868 * ice_init_pkg_info
1869 * @hw: pointer to the hardware structure
1870 * @pkg_hdr: pointer to the driver's package hdr
1871 *
1872 * Saves off the package details into the HW structure.
1873 */
1874static enum ice_ddp_state ice_init_pkg_info(struct ice_hw *hw,
1875					    struct ice_pkg_hdr *pkg_hdr)
1876{
1877	struct ice_generic_seg_hdr *seg_hdr;
1878
1879	if (!pkg_hdr)
1880		return ICE_DDP_PKG_ERR;
1881
1882	hw->pkg_has_signing_seg = ice_has_signing_seg(hw, pkg_hdr);
1883	ice_get_signing_req(hw);
1884
1885	ice_debug(hw, ICE_DBG_INIT, "Pkg using segment id: 0x%08X\n",
1886		  hw->pkg_seg_id);
1887
1888	seg_hdr = (struct ice_generic_seg_hdr *)
1889		ice_find_seg_in_pkg(hw, hw->pkg_seg_id, pkg_hdr);
1890	if (seg_hdr) {
1891		struct ice_meta_sect *meta;
1892		struct ice_pkg_enum state;
1893
1894		memset(&state, 0, sizeof(state));
1895
1896		/* Get package information from the Metadata Section */
1897		meta = ice_pkg_enum_section((struct ice_seg *)seg_hdr, &state,
1898					    ICE_SID_METADATA);
1899		if (!meta) {
1900			ice_debug(hw, ICE_DBG_INIT,
1901				  "Did not find ice metadata section in package\n");
1902			return ICE_DDP_PKG_INVALID_FILE;
1903		}
1904
1905		hw->pkg_ver = meta->ver;
1906		memcpy(hw->pkg_name, meta->name, sizeof(meta->name));
1907
1908		ice_debug(hw, ICE_DBG_PKG, "Pkg: %d.%d.%d.%d, %s\n",
1909			  meta->ver.major, meta->ver.minor, meta->ver.update,
1910			  meta->ver.draft, meta->name);
1911
1912		hw->ice_seg_fmt_ver = seg_hdr->seg_format_ver;
1913		memcpy(hw->ice_seg_id, seg_hdr->seg_id, sizeof(hw->ice_seg_id));
1914
1915		ice_debug(hw, ICE_DBG_PKG, "Ice Seg: %d.%d.%d.%d, %s\n",
1916			  seg_hdr->seg_format_ver.major,
1917			  seg_hdr->seg_format_ver.minor,
1918			  seg_hdr->seg_format_ver.update,
1919			  seg_hdr->seg_format_ver.draft, seg_hdr->seg_id);
1920	} else {
1921		ice_debug(hw, ICE_DBG_INIT,
1922			  "Did not find ice segment in driver package\n");
1923		return ICE_DDP_PKG_INVALID_FILE;
1924	}
1925
1926	return ICE_DDP_PKG_SUCCESS;
1927}
1928
1929/**
1930 * ice_get_pkg_info
1931 * @hw: pointer to the hardware structure
1932 *
1933 * Store details of the package currently loaded in HW into the HW structure.
1934 */
1935static enum ice_ddp_state ice_get_pkg_info(struct ice_hw *hw)
1936{
1937	DEFINE_FLEX(struct ice_aqc_get_pkg_info_resp, pkg_info, pkg_info,
1938		    ICE_PKG_CNT);
1939	u16 size = __struct_size(pkg_info);
1940	u32 i;
1941
1942	if (ice_aq_get_pkg_info_list(hw, pkg_info, size, NULL))
1943		return ICE_DDP_PKG_ERR;
1944
1945	for (i = 0; i < le32_to_cpu(pkg_info->count); i++) {
1946#define ICE_PKG_FLAG_COUNT 4
1947		char flags[ICE_PKG_FLAG_COUNT + 1] = { 0 };
1948		u8 place = 0;
1949
1950		if (pkg_info->pkg_info[i].is_active) {
1951			flags[place++] = 'A';
1952			hw->active_pkg_ver = pkg_info->pkg_info[i].ver;
1953			hw->active_track_id =
1954				le32_to_cpu(pkg_info->pkg_info[i].track_id);
1955			memcpy(hw->active_pkg_name, pkg_info->pkg_info[i].name,
1956			       sizeof(pkg_info->pkg_info[i].name));
1957			hw->active_pkg_in_nvm = pkg_info->pkg_info[i].is_in_nvm;
1958		}
1959		if (pkg_info->pkg_info[i].is_active_at_boot)
1960			flags[place++] = 'B';
1961		if (pkg_info->pkg_info[i].is_modified)
1962			flags[place++] = 'M';
1963		if (pkg_info->pkg_info[i].is_in_nvm)
1964			flags[place++] = 'N';
1965
1966		ice_debug(hw, ICE_DBG_PKG, "Pkg[%d]: %d.%d.%d.%d,%s,%s\n", i,
1967			  pkg_info->pkg_info[i].ver.major,
1968			  pkg_info->pkg_info[i].ver.minor,
1969			  pkg_info->pkg_info[i].ver.update,
1970			  pkg_info->pkg_info[i].ver.draft,
1971			  pkg_info->pkg_info[i].name, flags);
1972	}
1973
1974	return ICE_DDP_PKG_SUCCESS;
1975}
1976
1977/**
1978 * ice_chk_pkg_compat
1979 * @hw: pointer to the hardware structure
1980 * @ospkg: pointer to the package hdr
1981 * @seg: pointer to the package segment hdr
1982 *
1983 * This function checks the package version compatibility with driver and NVM
1984 */
1985static enum ice_ddp_state ice_chk_pkg_compat(struct ice_hw *hw,
1986					     struct ice_pkg_hdr *ospkg,
1987					     struct ice_seg **seg)
1988{
1989	DEFINE_FLEX(struct ice_aqc_get_pkg_info_resp, pkg, pkg_info,
1990		    ICE_PKG_CNT);
1991	u16 size = __struct_size(pkg);
1992	enum ice_ddp_state state;
1993	u32 i;
1994
1995	/* Check package version compatibility */
1996	state = ice_chk_pkg_version(&hw->pkg_ver);
1997	if (state) {
1998		ice_debug(hw, ICE_DBG_INIT, "Package version check failed.\n");
1999		return state;
2000	}
2001
2002	/* find ICE segment in given package */
2003	*seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, hw->pkg_seg_id,
2004						     ospkg);
2005	if (!*seg) {
2006		ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
2007		return ICE_DDP_PKG_INVALID_FILE;
2008	}
2009
2010	/* Check if FW is compatible with the OS package */
2011	if (ice_aq_get_pkg_info_list(hw, pkg, size, NULL))
2012		return ICE_DDP_PKG_LOAD_ERROR;
2013
2014	for (i = 0; i < le32_to_cpu(pkg->count); i++) {
2015		/* loop till we find the NVM package */
2016		if (!pkg->pkg_info[i].is_in_nvm)
2017			continue;
2018		if ((*seg)->hdr.seg_format_ver.major !=
2019			    pkg->pkg_info[i].ver.major ||
2020		    (*seg)->hdr.seg_format_ver.minor >
2021			    pkg->pkg_info[i].ver.minor) {
2022			state = ICE_DDP_PKG_FW_MISMATCH;
2023			ice_debug(hw, ICE_DBG_INIT,
2024				  "OS package is not compatible with NVM.\n");
2025		}
2026		/* done processing NVM package so break */
2027		break;
2028	}
2029
2030	return state;
2031}
2032
2033/**
2034 * ice_init_pkg_hints
2035 * @hw: pointer to the HW structure
2036 * @ice_seg: pointer to the segment of the package scan (non-NULL)
2037 *
2038 * This function will scan the package and save off relevant information
2039 * (hints or metadata) for driver use. The ice_seg parameter must not be NULL
2040 * since the first call to ice_enum_labels requires a pointer to an actual
2041 * ice_seg structure.
2042 */
2043static void ice_init_pkg_hints(struct ice_hw *hw, struct ice_seg *ice_seg)
2044{
2045	struct ice_pkg_enum state;
2046	char *label_name;
2047	u16 val;
2048	int i;
2049
2050	memset(&hw->tnl, 0, sizeof(hw->tnl));
2051	memset(&state, 0, sizeof(state));
2052
2053	if (!ice_seg)
2054		return;
2055
2056	label_name = ice_enum_labels(ice_seg, ICE_SID_LBL_RXPARSER_TMEM, &state,
2057				     &val);
2058
2059	while (label_name) {
2060		if (!strncmp(label_name, ICE_TNL_PRE, strlen(ICE_TNL_PRE)))
2061			/* check for a tunnel entry */
2062			ice_add_tunnel_hint(hw, label_name, val);
2063
2064		/* check for a dvm mode entry */
2065		else if (!strncmp(label_name, ICE_DVM_PRE, strlen(ICE_DVM_PRE)))
2066			ice_add_dvm_hint(hw, val, true);
2067
2068		/* check for a svm mode entry */
2069		else if (!strncmp(label_name, ICE_SVM_PRE, strlen(ICE_SVM_PRE)))
2070			ice_add_dvm_hint(hw, val, false);
2071
2072		label_name = ice_enum_labels(NULL, 0, &state, &val);
2073	}
2074
2075	/* Cache the appropriate boost TCAM entry pointers for tunnels */
2076	for (i = 0; i < hw->tnl.count; i++) {
2077		ice_find_boost_entry(ice_seg, hw->tnl.tbl[i].boost_addr,
2078				     &hw->tnl.tbl[i].boost_entry);
2079		if (hw->tnl.tbl[i].boost_entry) {
2080			hw->tnl.tbl[i].valid = true;
2081			if (hw->tnl.tbl[i].type < __TNL_TYPE_CNT)
2082				hw->tnl.valid_count[hw->tnl.tbl[i].type]++;
2083		}
2084	}
2085
2086	/* Cache the appropriate boost TCAM entry pointers for DVM and SVM */
2087	for (i = 0; i < hw->dvm_upd.count; i++)
2088		ice_find_boost_entry(ice_seg, hw->dvm_upd.tbl[i].boost_addr,
2089				     &hw->dvm_upd.tbl[i].boost_entry);
2090}
2091
2092/**
2093 * ice_fill_hw_ptype - fill the enabled PTYPE bit information
2094 * @hw: pointer to the HW structure
2095 */
2096static void ice_fill_hw_ptype(struct ice_hw *hw)
2097{
2098	struct ice_marker_ptype_tcam_entry *tcam;
2099	struct ice_seg *seg = hw->seg;
2100	struct ice_pkg_enum state;
2101
2102	bitmap_zero(hw->hw_ptype, ICE_FLOW_PTYPE_MAX);
2103	if (!seg)
2104		return;
2105
2106	memset(&state, 0, sizeof(state));
2107
2108	do {
2109		tcam = ice_pkg_enum_entry(seg, &state,
2110					  ICE_SID_RXPARSER_MARKER_PTYPE, NULL,
2111					  ice_marker_ptype_tcam_handler);
2112		if (tcam &&
2113		    le16_to_cpu(tcam->addr) < ICE_MARKER_PTYPE_TCAM_ADDR_MAX &&
2114		    le16_to_cpu(tcam->ptype) < ICE_FLOW_PTYPE_MAX)
2115			set_bit(le16_to_cpu(tcam->ptype), hw->hw_ptype);
2116
2117		seg = NULL;
2118	} while (tcam);
2119}
2120
2121/**
2122 * ice_init_pkg - initialize/download package
2123 * @hw: pointer to the hardware structure
2124 * @buf: pointer to the package buffer
2125 * @len: size of the package buffer
2126 *
2127 * This function initializes a package. The package contains HW tables
2128 * required to do packet processing. First, the function extracts package
2129 * information such as version. Then it finds the ice configuration segment
2130 * within the package; this function then saves a copy of the segment pointer
2131 * within the supplied package buffer. Next, the function will cache any hints
2132 * from the package, followed by downloading the package itself. Note, that if
2133 * a previous PF driver has already downloaded the package successfully, then
2134 * the current driver will not have to download the package again.
2135 *
2136 * The local package contents will be used to query default behavior and to
2137 * update specific sections of the HW's version of the package (e.g. to update
2138 * the parse graph to understand new protocols).
2139 *
2140 * This function stores a pointer to the package buffer memory, and it is
2141 * expected that the supplied buffer will not be freed immediately. If the
2142 * package buffer needs to be freed, such as when read from a file, use
2143 * ice_copy_and_init_pkg() instead of directly calling ice_init_pkg() in this
2144 * case.
2145 */
2146enum ice_ddp_state ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
2147{
2148	bool already_loaded = false;
2149	enum ice_ddp_state state;
2150	struct ice_pkg_hdr *pkg;
2151	struct ice_seg *seg;
2152
2153	if (!buf || !len)
2154		return ICE_DDP_PKG_ERR;
2155
2156	pkg = (struct ice_pkg_hdr *)buf;
2157	state = ice_verify_pkg(pkg, len);
2158	if (state) {
2159		ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
2160			  state);
2161		return state;
2162	}
2163
2164	/* initialize package info */
2165	state = ice_init_pkg_info(hw, pkg);
2166	if (state)
2167		return state;
2168
2169	/* must be a matching segment */
2170	if (hw->pkg_has_signing_seg &&
2171	    !ice_match_signing_seg(pkg, hw->pkg_seg_id, hw->pkg_sign_type))
2172		return ICE_DDP_PKG_ERR;
2173
2174	/* before downloading the package, check package version for
2175	 * compatibility with driver
2176	 */
2177	state = ice_chk_pkg_compat(hw, pkg, &seg);
2178	if (state)
2179		return state;
2180
2181	/* initialize package hints and then download package */
2182	ice_init_pkg_hints(hw, seg);
2183	state = ice_download_pkg(hw, pkg, seg);
2184	if (state == ICE_DDP_PKG_ALREADY_LOADED) {
2185		ice_debug(hw, ICE_DBG_INIT,
2186			  "package previously loaded - no work.\n");
2187		already_loaded = true;
2188	}
2189
2190	/* Get information on the package currently loaded in HW, then make sure
2191	 * the driver is compatible with this version.
2192	 */
2193	if (!state || state == ICE_DDP_PKG_ALREADY_LOADED) {
2194		state = ice_get_pkg_info(hw);
2195		if (!state)
2196			state = ice_get_ddp_pkg_state(hw, already_loaded);
2197	}
2198
2199	if (ice_is_init_pkg_successful(state)) {
2200		hw->seg = seg;
2201		/* on successful package download update other required
2202		 * registers to support the package and fill HW tables
2203		 * with package content.
2204		 */
2205		ice_init_pkg_regs(hw);
2206		ice_fill_blk_tbls(hw);
2207		ice_fill_hw_ptype(hw);
2208		ice_get_prof_index_max(hw);
2209	} else {
2210		ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n", state);
2211	}
2212
2213	return state;
2214}
2215
2216/**
2217 * ice_copy_and_init_pkg - initialize/download a copy of the package
2218 * @hw: pointer to the hardware structure
2219 * @buf: pointer to the package buffer
2220 * @len: size of the package buffer
2221 *
2222 * This function copies the package buffer, and then calls ice_init_pkg() to
2223 * initialize the copied package contents.
2224 *
2225 * The copying is necessary if the package buffer supplied is constant, or if
2226 * the memory may disappear shortly after calling this function.
2227 *
2228 * If the package buffer resides in the data segment and can be modified, the
2229 * caller is free to use ice_init_pkg() instead of ice_copy_and_init_pkg().
2230 *
2231 * However, if the package buffer needs to be copied first, such as when being
2232 * read from a file, the caller should use ice_copy_and_init_pkg().
2233 *
2234 * This function will first copy the package buffer, before calling
2235 * ice_init_pkg(). The caller is free to immediately destroy the original
2236 * package buffer, as the new copy will be managed by this function and
2237 * related routines.
2238 */
2239enum ice_ddp_state ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf,
2240					 u32 len)
2241{
2242	enum ice_ddp_state state;
2243	u8 *buf_copy;
2244
2245	if (!buf || !len)
2246		return ICE_DDP_PKG_ERR;
2247
2248	buf_copy = devm_kmemdup(ice_hw_to_dev(hw), buf, len, GFP_KERNEL);
2249
2250	state = ice_init_pkg(hw, buf_copy, len);
2251	if (!ice_is_init_pkg_successful(state)) {
2252		/* Free the copy, since we failed to initialize the package */
2253		devm_kfree(ice_hw_to_dev(hw), buf_copy);
2254	} else {
2255		/* Track the copied pkg so we can free it later */
2256		hw->pkg_copy = buf_copy;
2257		hw->pkg_size = len;
2258	}
2259
2260	return state;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2261}