18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2013 - 2018 Intel Corporation. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "i40e.h"
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/firmware.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/**
98c2ecf20Sopenharmony_ci * i40e_ddp_profiles_eq - checks if DDP profiles are the equivalent
108c2ecf20Sopenharmony_ci * @a: new profile info
118c2ecf20Sopenharmony_ci * @b: old profile info
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * checks if DDP profiles are the equivalent.
148c2ecf20Sopenharmony_ci * Returns true if profiles are the same.
158c2ecf20Sopenharmony_ci **/
168c2ecf20Sopenharmony_cistatic bool i40e_ddp_profiles_eq(struct i40e_profile_info *a,
178c2ecf20Sopenharmony_ci				 struct i40e_profile_info *b)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	return a->track_id == b->track_id &&
208c2ecf20Sopenharmony_ci		!memcmp(&a->version, &b->version, sizeof(a->version)) &&
218c2ecf20Sopenharmony_ci		!memcmp(&a->name, &b->name, I40E_DDP_NAME_SIZE);
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/**
258c2ecf20Sopenharmony_ci * i40e_ddp_does_profile_exist - checks if DDP profile loaded already
268c2ecf20Sopenharmony_ci * @hw: HW data structure
278c2ecf20Sopenharmony_ci * @pinfo: DDP profile information structure
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * checks if DDP profile loaded already.
308c2ecf20Sopenharmony_ci * Returns >0 if the profile exists.
318c2ecf20Sopenharmony_ci * Returns  0 if the profile is absent.
328c2ecf20Sopenharmony_ci * Returns <0 if error.
338c2ecf20Sopenharmony_ci **/
348c2ecf20Sopenharmony_cistatic int i40e_ddp_does_profile_exist(struct i40e_hw *hw,
358c2ecf20Sopenharmony_ci				       struct i40e_profile_info *pinfo)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct i40e_ddp_profile_list *profile_list;
388c2ecf20Sopenharmony_ci	u8 buff[I40E_PROFILE_LIST_SIZE];
398c2ecf20Sopenharmony_ci	i40e_status status;
408c2ecf20Sopenharmony_ci	int i;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	status = i40e_aq_get_ddp_list(hw, buff, I40E_PROFILE_LIST_SIZE, 0,
438c2ecf20Sopenharmony_ci				      NULL);
448c2ecf20Sopenharmony_ci	if (status)
458c2ecf20Sopenharmony_ci		return -1;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	profile_list = (struct i40e_ddp_profile_list *)buff;
488c2ecf20Sopenharmony_ci	for (i = 0; i < profile_list->p_count; i++) {
498c2ecf20Sopenharmony_ci		if (i40e_ddp_profiles_eq(pinfo, &profile_list->p_info[i]))
508c2ecf20Sopenharmony_ci			return 1;
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci	return 0;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/**
568c2ecf20Sopenharmony_ci * i40e_ddp_profiles_overlap - checks if DDP profiles overlap.
578c2ecf20Sopenharmony_ci * @new: new profile info
588c2ecf20Sopenharmony_ci * @old: old profile info
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * checks if DDP profiles overlap.
618c2ecf20Sopenharmony_ci * Returns true if profiles are overlap.
628c2ecf20Sopenharmony_ci **/
638c2ecf20Sopenharmony_cistatic bool i40e_ddp_profiles_overlap(struct i40e_profile_info *new,
648c2ecf20Sopenharmony_ci				      struct i40e_profile_info *old)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	unsigned int group_id_old = (u8)((old->track_id & 0x00FF0000) >> 16);
678c2ecf20Sopenharmony_ci	unsigned int group_id_new = (u8)((new->track_id & 0x00FF0000) >> 16);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* 0x00 group must be only the first */
708c2ecf20Sopenharmony_ci	if (group_id_new == 0)
718c2ecf20Sopenharmony_ci		return true;
728c2ecf20Sopenharmony_ci	/* 0xFF group is compatible with anything else */
738c2ecf20Sopenharmony_ci	if (group_id_new == 0xFF || group_id_old == 0xFF)
748c2ecf20Sopenharmony_ci		return false;
758c2ecf20Sopenharmony_ci	/* otherwise only profiles from the same group are compatible*/
768c2ecf20Sopenharmony_ci	return group_id_old != group_id_new;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/**
808c2ecf20Sopenharmony_ci * i40e_ddp_does_profiles_ - checks if DDP overlaps with existing one.
818c2ecf20Sopenharmony_ci * @hw: HW data structure
828c2ecf20Sopenharmony_ci * @pinfo: DDP profile information structure
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * checks if DDP profile overlaps with existing one.
858c2ecf20Sopenharmony_ci * Returns >0 if the profile overlaps.
868c2ecf20Sopenharmony_ci * Returns  0 if the profile is ok.
878c2ecf20Sopenharmony_ci * Returns <0 if error.
888c2ecf20Sopenharmony_ci **/
898c2ecf20Sopenharmony_cistatic int i40e_ddp_does_profile_overlap(struct i40e_hw *hw,
908c2ecf20Sopenharmony_ci					 struct i40e_profile_info *pinfo)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct i40e_ddp_profile_list *profile_list;
938c2ecf20Sopenharmony_ci	u8 buff[I40E_PROFILE_LIST_SIZE];
948c2ecf20Sopenharmony_ci	i40e_status status;
958c2ecf20Sopenharmony_ci	int i;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	status = i40e_aq_get_ddp_list(hw, buff, I40E_PROFILE_LIST_SIZE, 0,
988c2ecf20Sopenharmony_ci				      NULL);
998c2ecf20Sopenharmony_ci	if (status)
1008c2ecf20Sopenharmony_ci		return -EIO;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	profile_list = (struct i40e_ddp_profile_list *)buff;
1038c2ecf20Sopenharmony_ci	for (i = 0; i < profile_list->p_count; i++) {
1048c2ecf20Sopenharmony_ci		if (i40e_ddp_profiles_overlap(pinfo,
1058c2ecf20Sopenharmony_ci					      &profile_list->p_info[i]))
1068c2ecf20Sopenharmony_ci			return 1;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/**
1128c2ecf20Sopenharmony_ci * i40e_add_pinfo
1138c2ecf20Sopenharmony_ci * @hw: pointer to the hardware structure
1148c2ecf20Sopenharmony_ci * @profile: pointer to the profile segment of the package
1158c2ecf20Sopenharmony_ci * @profile_info_sec: buffer for information section
1168c2ecf20Sopenharmony_ci * @track_id: package tracking id
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci * Register a profile to the list of loaded profiles.
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_cistatic enum i40e_status_code
1218c2ecf20Sopenharmony_cii40e_add_pinfo(struct i40e_hw *hw, struct i40e_profile_segment *profile,
1228c2ecf20Sopenharmony_ci	       u8 *profile_info_sec, u32 track_id)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct i40e_profile_section_header *sec;
1258c2ecf20Sopenharmony_ci	struct i40e_profile_info *pinfo;
1268c2ecf20Sopenharmony_ci	i40e_status status;
1278c2ecf20Sopenharmony_ci	u32 offset = 0, info = 0;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	sec = (struct i40e_profile_section_header *)profile_info_sec;
1308c2ecf20Sopenharmony_ci	sec->tbl_size = 1;
1318c2ecf20Sopenharmony_ci	sec->data_end = sizeof(struct i40e_profile_section_header) +
1328c2ecf20Sopenharmony_ci			sizeof(struct i40e_profile_info);
1338c2ecf20Sopenharmony_ci	sec->section.type = SECTION_TYPE_INFO;
1348c2ecf20Sopenharmony_ci	sec->section.offset = sizeof(struct i40e_profile_section_header);
1358c2ecf20Sopenharmony_ci	sec->section.size = sizeof(struct i40e_profile_info);
1368c2ecf20Sopenharmony_ci	pinfo = (struct i40e_profile_info *)(profile_info_sec +
1378c2ecf20Sopenharmony_ci					     sec->section.offset);
1388c2ecf20Sopenharmony_ci	pinfo->track_id = track_id;
1398c2ecf20Sopenharmony_ci	pinfo->version = profile->version;
1408c2ecf20Sopenharmony_ci	pinfo->op = I40E_DDP_ADD_TRACKID;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* Clear reserved field */
1438c2ecf20Sopenharmony_ci	memset(pinfo->reserved, 0, sizeof(pinfo->reserved));
1448c2ecf20Sopenharmony_ci	memcpy(pinfo->name, profile->name, I40E_DDP_NAME_SIZE);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	status = i40e_aq_write_ddp(hw, (void *)sec, sec->data_end,
1478c2ecf20Sopenharmony_ci				   track_id, &offset, &info, NULL);
1488c2ecf20Sopenharmony_ci	return status;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/**
1528c2ecf20Sopenharmony_ci * i40e_del_pinfo - delete DDP profile info from NIC
1538c2ecf20Sopenharmony_ci * @hw: HW data structure
1548c2ecf20Sopenharmony_ci * @profile: DDP profile segment to be deleted
1558c2ecf20Sopenharmony_ci * @profile_info_sec: DDP profile section header
1568c2ecf20Sopenharmony_ci * @track_id: track ID of the profile for deletion
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * Removes DDP profile from the NIC.
1598c2ecf20Sopenharmony_ci **/
1608c2ecf20Sopenharmony_cistatic enum i40e_status_code
1618c2ecf20Sopenharmony_cii40e_del_pinfo(struct i40e_hw *hw, struct i40e_profile_segment *profile,
1628c2ecf20Sopenharmony_ci	       u8 *profile_info_sec, u32 track_id)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct i40e_profile_section_header *sec;
1658c2ecf20Sopenharmony_ci	struct i40e_profile_info *pinfo;
1668c2ecf20Sopenharmony_ci	i40e_status status;
1678c2ecf20Sopenharmony_ci	u32 offset = 0, info = 0;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	sec = (struct i40e_profile_section_header *)profile_info_sec;
1708c2ecf20Sopenharmony_ci	sec->tbl_size = 1;
1718c2ecf20Sopenharmony_ci	sec->data_end = sizeof(struct i40e_profile_section_header) +
1728c2ecf20Sopenharmony_ci			sizeof(struct i40e_profile_info);
1738c2ecf20Sopenharmony_ci	sec->section.type = SECTION_TYPE_INFO;
1748c2ecf20Sopenharmony_ci	sec->section.offset = sizeof(struct i40e_profile_section_header);
1758c2ecf20Sopenharmony_ci	sec->section.size = sizeof(struct i40e_profile_info);
1768c2ecf20Sopenharmony_ci	pinfo = (struct i40e_profile_info *)(profile_info_sec +
1778c2ecf20Sopenharmony_ci					     sec->section.offset);
1788c2ecf20Sopenharmony_ci	pinfo->track_id = track_id;
1798c2ecf20Sopenharmony_ci	pinfo->version = profile->version;
1808c2ecf20Sopenharmony_ci	pinfo->op = I40E_DDP_REMOVE_TRACKID;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* Clear reserved field */
1838c2ecf20Sopenharmony_ci	memset(pinfo->reserved, 0, sizeof(pinfo->reserved));
1848c2ecf20Sopenharmony_ci	memcpy(pinfo->name, profile->name, I40E_DDP_NAME_SIZE);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	status = i40e_aq_write_ddp(hw, (void *)sec, sec->data_end,
1878c2ecf20Sopenharmony_ci				   track_id, &offset, &info, NULL);
1888c2ecf20Sopenharmony_ci	return status;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/**
1928c2ecf20Sopenharmony_ci * i40e_ddp_is_pkg_hdr_valid - performs basic pkg header integrity checks
1938c2ecf20Sopenharmony_ci * @netdev: net device structure (for logging purposes)
1948c2ecf20Sopenharmony_ci * @pkg_hdr: pointer to package header
1958c2ecf20Sopenharmony_ci * @size_huge: size of the whole DDP profile package in size_t
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci * Checks correctness of pkg header: Version, size too big/small, and
1988c2ecf20Sopenharmony_ci * all segment offsets alignment and boundaries. This function lets
1998c2ecf20Sopenharmony_ci * reject non DDP profile file to be loaded by administrator mistake.
2008c2ecf20Sopenharmony_ci **/
2018c2ecf20Sopenharmony_cistatic bool i40e_ddp_is_pkg_hdr_valid(struct net_device *netdev,
2028c2ecf20Sopenharmony_ci				      struct i40e_package_header *pkg_hdr,
2038c2ecf20Sopenharmony_ci				      size_t size_huge)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	u32 size = 0xFFFFFFFFU & size_huge;
2068c2ecf20Sopenharmony_ci	u32 pkg_hdr_size;
2078c2ecf20Sopenharmony_ci	u32 segment;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (!pkg_hdr)
2108c2ecf20Sopenharmony_ci		return false;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	if (pkg_hdr->version.major > 0) {
2138c2ecf20Sopenharmony_ci		struct i40e_ddp_version ver = pkg_hdr->version;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		netdev_err(netdev, "Unsupported DDP profile version %u.%u.%u.%u",
2168c2ecf20Sopenharmony_ci			   ver.major, ver.minor, ver.update, ver.draft);
2178c2ecf20Sopenharmony_ci		return false;
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci	if (size_huge > size) {
2208c2ecf20Sopenharmony_ci		netdev_err(netdev, "Invalid DDP profile - size is bigger than 4G");
2218c2ecf20Sopenharmony_ci		return false;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci	if (size < (sizeof(struct i40e_package_header) +
2248c2ecf20Sopenharmony_ci		sizeof(struct i40e_metadata_segment) + sizeof(u32) * 2)) {
2258c2ecf20Sopenharmony_ci		netdev_err(netdev, "Invalid DDP profile - size is too small.");
2268c2ecf20Sopenharmony_ci		return false;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	pkg_hdr_size = sizeof(u32) * (pkg_hdr->segment_count + 2U);
2308c2ecf20Sopenharmony_ci	if (size < pkg_hdr_size) {
2318c2ecf20Sopenharmony_ci		netdev_err(netdev, "Invalid DDP profile - too many segments");
2328c2ecf20Sopenharmony_ci		return false;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci	for (segment = 0; segment < pkg_hdr->segment_count; ++segment) {
2358c2ecf20Sopenharmony_ci		u32 offset = pkg_hdr->segment_offset[segment];
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci		if (0xFU & offset) {
2388c2ecf20Sopenharmony_ci			netdev_err(netdev,
2398c2ecf20Sopenharmony_ci				   "Invalid DDP profile %u segment alignment",
2408c2ecf20Sopenharmony_ci				   segment);
2418c2ecf20Sopenharmony_ci			return false;
2428c2ecf20Sopenharmony_ci		}
2438c2ecf20Sopenharmony_ci		if (pkg_hdr_size > offset || offset >= size) {
2448c2ecf20Sopenharmony_ci			netdev_err(netdev,
2458c2ecf20Sopenharmony_ci				   "Invalid DDP profile %u segment offset",
2468c2ecf20Sopenharmony_ci				   segment);
2478c2ecf20Sopenharmony_ci			return false;
2488c2ecf20Sopenharmony_ci		}
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return true;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/**
2558c2ecf20Sopenharmony_ci * i40e_ddp_load - performs DDP loading
2568c2ecf20Sopenharmony_ci * @netdev: net device structure
2578c2ecf20Sopenharmony_ci * @data: buffer containing recipe file
2588c2ecf20Sopenharmony_ci * @size: size of the buffer
2598c2ecf20Sopenharmony_ci * @is_add: true when loading profile, false when rolling back the previous one
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci * Checks correctness and loads DDP profile to the NIC. The function is
2628c2ecf20Sopenharmony_ci * also used for rolling back previously loaded profile.
2638c2ecf20Sopenharmony_ci **/
2648c2ecf20Sopenharmony_ciint i40e_ddp_load(struct net_device *netdev, const u8 *data, size_t size,
2658c2ecf20Sopenharmony_ci		  bool is_add)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	u8 profile_info_sec[sizeof(struct i40e_profile_section_header) +
2688c2ecf20Sopenharmony_ci			    sizeof(struct i40e_profile_info)];
2698c2ecf20Sopenharmony_ci	struct i40e_metadata_segment *metadata_hdr;
2708c2ecf20Sopenharmony_ci	struct i40e_profile_segment *profile_hdr;
2718c2ecf20Sopenharmony_ci	struct i40e_profile_info pinfo;
2728c2ecf20Sopenharmony_ci	struct i40e_package_header *pkg_hdr;
2738c2ecf20Sopenharmony_ci	i40e_status status;
2748c2ecf20Sopenharmony_ci	struct i40e_netdev_priv *np = netdev_priv(netdev);
2758c2ecf20Sopenharmony_ci	struct i40e_vsi *vsi = np->vsi;
2768c2ecf20Sopenharmony_ci	struct i40e_pf *pf = vsi->back;
2778c2ecf20Sopenharmony_ci	u32 track_id;
2788c2ecf20Sopenharmony_ci	int istatus;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	pkg_hdr = (struct i40e_package_header *)data;
2818c2ecf20Sopenharmony_ci	if (!i40e_ddp_is_pkg_hdr_valid(netdev, pkg_hdr, size))
2828c2ecf20Sopenharmony_ci		return -EINVAL;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (size < (sizeof(struct i40e_package_header) +
2858c2ecf20Sopenharmony_ci		    sizeof(struct i40e_metadata_segment) + sizeof(u32) * 2)) {
2868c2ecf20Sopenharmony_ci		netdev_err(netdev, "Invalid DDP recipe size.");
2878c2ecf20Sopenharmony_ci		return -EINVAL;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* Find beginning of segment data in buffer */
2918c2ecf20Sopenharmony_ci	metadata_hdr = (struct i40e_metadata_segment *)
2928c2ecf20Sopenharmony_ci		i40e_find_segment_in_package(SEGMENT_TYPE_METADATA, pkg_hdr);
2938c2ecf20Sopenharmony_ci	if (!metadata_hdr) {
2948c2ecf20Sopenharmony_ci		netdev_err(netdev, "Failed to find metadata segment in DDP recipe.");
2958c2ecf20Sopenharmony_ci		return -EINVAL;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	track_id = metadata_hdr->track_id;
2998c2ecf20Sopenharmony_ci	profile_hdr = (struct i40e_profile_segment *)
3008c2ecf20Sopenharmony_ci		i40e_find_segment_in_package(SEGMENT_TYPE_I40E, pkg_hdr);
3018c2ecf20Sopenharmony_ci	if (!profile_hdr) {
3028c2ecf20Sopenharmony_ci		netdev_err(netdev, "Failed to find profile segment in DDP recipe.");
3038c2ecf20Sopenharmony_ci		return -EINVAL;
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	pinfo.track_id = track_id;
3078c2ecf20Sopenharmony_ci	pinfo.version = profile_hdr->version;
3088c2ecf20Sopenharmony_ci	if (is_add)
3098c2ecf20Sopenharmony_ci		pinfo.op = I40E_DDP_ADD_TRACKID;
3108c2ecf20Sopenharmony_ci	else
3118c2ecf20Sopenharmony_ci		pinfo.op = I40E_DDP_REMOVE_TRACKID;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	memcpy(pinfo.name, profile_hdr->name, I40E_DDP_NAME_SIZE);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* Check if profile data already exists*/
3168c2ecf20Sopenharmony_ci	istatus = i40e_ddp_does_profile_exist(&pf->hw, &pinfo);
3178c2ecf20Sopenharmony_ci	if (istatus < 0) {
3188c2ecf20Sopenharmony_ci		netdev_err(netdev, "Failed to fetch loaded profiles.");
3198c2ecf20Sopenharmony_ci		return istatus;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci	if (is_add) {
3228c2ecf20Sopenharmony_ci		if (istatus > 0) {
3238c2ecf20Sopenharmony_ci			netdev_err(netdev, "DDP profile already loaded.");
3248c2ecf20Sopenharmony_ci			return -EINVAL;
3258c2ecf20Sopenharmony_ci		}
3268c2ecf20Sopenharmony_ci		istatus = i40e_ddp_does_profile_overlap(&pf->hw, &pinfo);
3278c2ecf20Sopenharmony_ci		if (istatus < 0) {
3288c2ecf20Sopenharmony_ci			netdev_err(netdev, "Failed to fetch loaded profiles.");
3298c2ecf20Sopenharmony_ci			return istatus;
3308c2ecf20Sopenharmony_ci		}
3318c2ecf20Sopenharmony_ci		if (istatus > 0) {
3328c2ecf20Sopenharmony_ci			netdev_err(netdev, "DDP profile overlaps with existing one.");
3338c2ecf20Sopenharmony_ci			return -EINVAL;
3348c2ecf20Sopenharmony_ci		}
3358c2ecf20Sopenharmony_ci	} else {
3368c2ecf20Sopenharmony_ci		if (istatus == 0) {
3378c2ecf20Sopenharmony_ci			netdev_err(netdev,
3388c2ecf20Sopenharmony_ci				   "DDP profile for deletion does not exist.");
3398c2ecf20Sopenharmony_ci			return -EINVAL;
3408c2ecf20Sopenharmony_ci		}
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/* Load profile data */
3448c2ecf20Sopenharmony_ci	if (is_add) {
3458c2ecf20Sopenharmony_ci		status = i40e_write_profile(&pf->hw, profile_hdr, track_id);
3468c2ecf20Sopenharmony_ci		if (status) {
3478c2ecf20Sopenharmony_ci			if (status == I40E_ERR_DEVICE_NOT_SUPPORTED) {
3488c2ecf20Sopenharmony_ci				netdev_err(netdev,
3498c2ecf20Sopenharmony_ci					   "Profile is not supported by the device.");
3508c2ecf20Sopenharmony_ci				return -EPERM;
3518c2ecf20Sopenharmony_ci			}
3528c2ecf20Sopenharmony_ci			netdev_err(netdev, "Failed to write DDP profile.");
3538c2ecf20Sopenharmony_ci			return -EIO;
3548c2ecf20Sopenharmony_ci		}
3558c2ecf20Sopenharmony_ci	} else {
3568c2ecf20Sopenharmony_ci		status = i40e_rollback_profile(&pf->hw, profile_hdr, track_id);
3578c2ecf20Sopenharmony_ci		if (status) {
3588c2ecf20Sopenharmony_ci			netdev_err(netdev, "Failed to remove DDP profile.");
3598c2ecf20Sopenharmony_ci			return -EIO;
3608c2ecf20Sopenharmony_ci		}
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	/* Add/remove profile to/from profile list in FW */
3648c2ecf20Sopenharmony_ci	if (is_add) {
3658c2ecf20Sopenharmony_ci		status = i40e_add_pinfo(&pf->hw, profile_hdr, profile_info_sec,
3668c2ecf20Sopenharmony_ci					track_id);
3678c2ecf20Sopenharmony_ci		if (status) {
3688c2ecf20Sopenharmony_ci			netdev_err(netdev, "Failed to add DDP profile info.");
3698c2ecf20Sopenharmony_ci			return -EIO;
3708c2ecf20Sopenharmony_ci		}
3718c2ecf20Sopenharmony_ci	} else {
3728c2ecf20Sopenharmony_ci		status = i40e_del_pinfo(&pf->hw, profile_hdr, profile_info_sec,
3738c2ecf20Sopenharmony_ci					track_id);
3748c2ecf20Sopenharmony_ci		if (status) {
3758c2ecf20Sopenharmony_ci			netdev_err(netdev, "Failed to restore DDP profile info.");
3768c2ecf20Sopenharmony_ci			return -EIO;
3778c2ecf20Sopenharmony_ci		}
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return 0;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci/**
3848c2ecf20Sopenharmony_ci * i40e_ddp_restore - restore previously loaded profile and remove from list
3858c2ecf20Sopenharmony_ci * @pf: PF data struct
3868c2ecf20Sopenharmony_ci *
3878c2ecf20Sopenharmony_ci * Restores previously loaded profile stored on the list in driver memory.
3888c2ecf20Sopenharmony_ci * After rolling back removes entry from the list.
3898c2ecf20Sopenharmony_ci **/
3908c2ecf20Sopenharmony_cistatic int i40e_ddp_restore(struct i40e_pf *pf)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	struct i40e_ddp_old_profile_list *entry;
3938c2ecf20Sopenharmony_ci	struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
3948c2ecf20Sopenharmony_ci	int status = 0;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (!list_empty(&pf->ddp_old_prof)) {
3978c2ecf20Sopenharmony_ci		entry = list_first_entry(&pf->ddp_old_prof,
3988c2ecf20Sopenharmony_ci					 struct i40e_ddp_old_profile_list,
3998c2ecf20Sopenharmony_ci					 list);
4008c2ecf20Sopenharmony_ci		status = i40e_ddp_load(netdev, entry->old_ddp_buf,
4018c2ecf20Sopenharmony_ci				       entry->old_ddp_size, false);
4028c2ecf20Sopenharmony_ci		list_del(&entry->list);
4038c2ecf20Sopenharmony_ci		kfree(entry);
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	return status;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci/**
4098c2ecf20Sopenharmony_ci * i40e_ddp_flash - callback function for ethtool flash feature
4108c2ecf20Sopenharmony_ci * @netdev: net device structure
4118c2ecf20Sopenharmony_ci * @flash: kernel flash structure
4128c2ecf20Sopenharmony_ci *
4138c2ecf20Sopenharmony_ci * Ethtool callback function used for loading and unloading DDP profiles.
4148c2ecf20Sopenharmony_ci **/
4158c2ecf20Sopenharmony_ciint i40e_ddp_flash(struct net_device *netdev, struct ethtool_flash *flash)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	const struct firmware *ddp_config;
4188c2ecf20Sopenharmony_ci	struct i40e_netdev_priv *np = netdev_priv(netdev);
4198c2ecf20Sopenharmony_ci	struct i40e_vsi *vsi = np->vsi;
4208c2ecf20Sopenharmony_ci	struct i40e_pf *pf = vsi->back;
4218c2ecf20Sopenharmony_ci	int status = 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	/* Check for valid region first */
4248c2ecf20Sopenharmony_ci	if (flash->region != I40_DDP_FLASH_REGION) {
4258c2ecf20Sopenharmony_ci		netdev_err(netdev, "Requested firmware region is not recognized by this driver.");
4268c2ecf20Sopenharmony_ci		return -EINVAL;
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci	if (pf->hw.bus.func != 0) {
4298c2ecf20Sopenharmony_ci		netdev_err(netdev, "Any DDP operation is allowed only on Phy0 NIC interface");
4308c2ecf20Sopenharmony_ci		return -EINVAL;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* If the user supplied "-" instead of file name rollback previously
4348c2ecf20Sopenharmony_ci	 * stored profile.
4358c2ecf20Sopenharmony_ci	 */
4368c2ecf20Sopenharmony_ci	if (strncmp(flash->data, "-", 2) != 0) {
4378c2ecf20Sopenharmony_ci		struct i40e_ddp_old_profile_list *list_entry;
4388c2ecf20Sopenharmony_ci		char profile_name[sizeof(I40E_DDP_PROFILE_PATH)
4398c2ecf20Sopenharmony_ci				  + I40E_DDP_PROFILE_NAME_MAX];
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		profile_name[sizeof(profile_name) - 1] = 0;
4428c2ecf20Sopenharmony_ci		strncpy(profile_name, I40E_DDP_PROFILE_PATH,
4438c2ecf20Sopenharmony_ci			sizeof(profile_name) - 1);
4448c2ecf20Sopenharmony_ci		strncat(profile_name, flash->data, I40E_DDP_PROFILE_NAME_MAX);
4458c2ecf20Sopenharmony_ci		/* Load DDP recipe. */
4468c2ecf20Sopenharmony_ci		status = request_firmware(&ddp_config, profile_name,
4478c2ecf20Sopenharmony_ci					  &netdev->dev);
4488c2ecf20Sopenharmony_ci		if (status) {
4498c2ecf20Sopenharmony_ci			netdev_err(netdev, "DDP recipe file request failed.");
4508c2ecf20Sopenharmony_ci			return status;
4518c2ecf20Sopenharmony_ci		}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		status = i40e_ddp_load(netdev, ddp_config->data,
4548c2ecf20Sopenharmony_ci				       ddp_config->size, true);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci		if (!status) {
4578c2ecf20Sopenharmony_ci			list_entry =
4588c2ecf20Sopenharmony_ci			  kzalloc(sizeof(struct i40e_ddp_old_profile_list) +
4598c2ecf20Sopenharmony_ci				  ddp_config->size, GFP_KERNEL);
4608c2ecf20Sopenharmony_ci			if (!list_entry) {
4618c2ecf20Sopenharmony_ci				netdev_info(netdev, "Failed to allocate memory for previous DDP profile data.");
4628c2ecf20Sopenharmony_ci				netdev_info(netdev, "New profile loaded but roll-back will be impossible.");
4638c2ecf20Sopenharmony_ci			} else {
4648c2ecf20Sopenharmony_ci				memcpy(list_entry->old_ddp_buf,
4658c2ecf20Sopenharmony_ci				       ddp_config->data, ddp_config->size);
4668c2ecf20Sopenharmony_ci				list_entry->old_ddp_size = ddp_config->size;
4678c2ecf20Sopenharmony_ci				list_add(&list_entry->list, &pf->ddp_old_prof);
4688c2ecf20Sopenharmony_ci			}
4698c2ecf20Sopenharmony_ci		}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci		release_firmware(ddp_config);
4728c2ecf20Sopenharmony_ci	} else {
4738c2ecf20Sopenharmony_ci		if (!list_empty(&pf->ddp_old_prof)) {
4748c2ecf20Sopenharmony_ci			status = i40e_ddp_restore(pf);
4758c2ecf20Sopenharmony_ci		} else {
4768c2ecf20Sopenharmony_ci			netdev_warn(netdev, "There is no DDP profile to restore.");
4778c2ecf20Sopenharmony_ci			status = -ENOENT;
4788c2ecf20Sopenharmony_ci		}
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci	return status;
4818c2ecf20Sopenharmony_ci}
482