1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
6 #include <linux/firmware.h>
9 * i40e_ddp_profiles_eq - checks if DDP profiles are the equivalent
10 * @a: new profile info
11 * @b: old profile info
13 * checks if DDP profiles are the equivalent.
14 * Returns true if profiles are the same.
16 static bool i40e_ddp_profiles_eq(struct i40e_profile_info *a,
17 struct i40e_profile_info *b)
19 return a->track_id == b->track_id &&
20 !memcmp(&a->version, &b->version, sizeof(a->version)) &&
21 !memcmp(&a->name, &b->name, I40E_DDP_NAME_SIZE);
25 * i40e_ddp_does_profile_exist - checks if DDP profile loaded already
26 * @hw: HW data structure
27 * @pinfo: DDP profile information structure
29 * checks if DDP profile loaded already.
30 * Returns >0 if the profile exists.
31 * Returns 0 if the profile is absent.
32 * Returns <0 if error.
34 static int i40e_ddp_does_profile_exist(struct i40e_hw *hw,
35 struct i40e_profile_info *pinfo)
37 struct i40e_ddp_profile_list *profile_list;
38 u8 buff[I40E_PROFILE_LIST_SIZE];
42 status = i40e_aq_get_ddp_list(hw, buff, I40E_PROFILE_LIST_SIZE, 0,
47 profile_list = (struct i40e_ddp_profile_list *)buff;
48 for (i = 0; i < profile_list->p_count; i++) {
49 if (i40e_ddp_profiles_eq(pinfo, &profile_list->p_info[i]))
56 * i40e_ddp_profiles_overlap - checks if DDP profiles overlap.
57 * @new: new profile info
58 * @old: old profile info
60 * checks if DDP profiles overlap.
61 * Returns true if profiles are overlap.
63 static bool i40e_ddp_profiles_overlap(struct i40e_profile_info *new,
64 struct i40e_profile_info *old)
66 unsigned int group_id_old = (u8)((old->track_id & 0x00FF0000) >> 16);
67 unsigned int group_id_new = (u8)((new->track_id & 0x00FF0000) >> 16);
69 /* 0x00 group must be only the first */
70 if (group_id_new == 0)
72 /* 0xFF group is compatible with anything else */
73 if (group_id_new == 0xFF || group_id_old == 0xFF)
75 /* otherwise only profiles from the same group are compatible*/
76 return group_id_old != group_id_new;
80 * i40e_ddp_does_profiles_ - checks if DDP overlaps with existing one.
81 * @hw: HW data structure
82 * @pinfo: DDP profile information structure
84 * checks if DDP profile overlaps with existing one.
85 * Returns >0 if the profile overlaps.
86 * Returns 0 if the profile is ok.
87 * Returns <0 if error.
89 static int i40e_ddp_does_profile_overlap(struct i40e_hw *hw,
90 struct i40e_profile_info *pinfo)
92 struct i40e_ddp_profile_list *profile_list;
93 u8 buff[I40E_PROFILE_LIST_SIZE];
97 status = i40e_aq_get_ddp_list(hw, buff, I40E_PROFILE_LIST_SIZE, 0,
102 profile_list = (struct i40e_ddp_profile_list *)buff;
103 for (i = 0; i < profile_list->p_count; i++) {
104 if (i40e_ddp_profiles_overlap(pinfo,
105 &profile_list->p_info[i]))
113 * @hw: pointer to the hardware structure
114 * @profile: pointer to the profile segment of the package
115 * @profile_info_sec: buffer for information section
116 * @track_id: package tracking id
118 * Register a profile to the list of loaded profiles.
120 static enum i40e_status_code
121 i40e_add_pinfo(struct i40e_hw *hw, struct i40e_profile_segment *profile,
122 u8 *profile_info_sec, u32 track_id)
124 struct i40e_profile_section_header *sec;
125 struct i40e_profile_info *pinfo;
127 u32 offset = 0, info = 0;
129 sec = (struct i40e_profile_section_header *)profile_info_sec;
131 sec->data_end = sizeof(struct i40e_profile_section_header) +
132 sizeof(struct i40e_profile_info);
133 sec->section.type = SECTION_TYPE_INFO;
134 sec->section.offset = sizeof(struct i40e_profile_section_header);
135 sec->section.size = sizeof(struct i40e_profile_info);
136 pinfo = (struct i40e_profile_info *)(profile_info_sec +
137 sec->section.offset);
138 pinfo->track_id = track_id;
139 pinfo->version = profile->version;
140 pinfo->op = I40E_DDP_ADD_TRACKID;
142 /* Clear reserved field */
143 memset(pinfo->reserved, 0, sizeof(pinfo->reserved));
144 memcpy(pinfo->name, profile->name, I40E_DDP_NAME_SIZE);
146 status = i40e_aq_write_ddp(hw, (void *)sec, sec->data_end,
147 track_id, &offset, &info, NULL);
152 * i40e_del_pinfo - delete DDP profile info from NIC
153 * @hw: HW data structure
154 * @profile: DDP profile segment to be deleted
155 * @profile_info_sec: DDP profile section header
156 * @track_id: track ID of the profile for deletion
158 * Removes DDP profile from the NIC.
160 static enum i40e_status_code
161 i40e_del_pinfo(struct i40e_hw *hw, struct i40e_profile_segment *profile,
162 u8 *profile_info_sec, u32 track_id)
164 struct i40e_profile_section_header *sec;
165 struct i40e_profile_info *pinfo;
167 u32 offset = 0, info = 0;
169 sec = (struct i40e_profile_section_header *)profile_info_sec;
171 sec->data_end = sizeof(struct i40e_profile_section_header) +
172 sizeof(struct i40e_profile_info);
173 sec->section.type = SECTION_TYPE_INFO;
174 sec->section.offset = sizeof(struct i40e_profile_section_header);
175 sec->section.size = sizeof(struct i40e_profile_info);
176 pinfo = (struct i40e_profile_info *)(profile_info_sec +
177 sec->section.offset);
178 pinfo->track_id = track_id;
179 pinfo->version = profile->version;
180 pinfo->op = I40E_DDP_REMOVE_TRACKID;
182 /* Clear reserved field */
183 memset(pinfo->reserved, 0, sizeof(pinfo->reserved));
184 memcpy(pinfo->name, profile->name, I40E_DDP_NAME_SIZE);
186 status = i40e_aq_write_ddp(hw, (void *)sec, sec->data_end,
187 track_id, &offset, &info, NULL);
192 * i40e_ddp_is_pkg_hdr_valid - performs basic pkg header integrity checks
193 * @netdev: net device structure (for logging purposes)
194 * @pkg_hdr: pointer to package header
195 * @size_huge: size of the whole DDP profile package in size_t
197 * Checks correctness of pkg header: Version, size too big/small, and
198 * all segment offsets alignment and boundaries. This function lets
199 * reject non DDP profile file to be loaded by administrator mistake.
201 static bool i40e_ddp_is_pkg_hdr_valid(struct net_device *netdev,
202 struct i40e_package_header *pkg_hdr,
205 u32 size = 0xFFFFFFFFU & size_huge;
212 if (pkg_hdr->version.major > 0) {
213 struct i40e_ddp_version ver = pkg_hdr->version;
215 netdev_err(netdev, "Unsupported DDP profile version %u.%u.%u.%u",
216 ver.major, ver.minor, ver.update, ver.draft);
219 if (size_huge > size) {
220 netdev_err(netdev, "Invalid DDP profile - size is bigger than 4G");
223 if (size < (sizeof(struct i40e_package_header) +
224 sizeof(struct i40e_metadata_segment) + sizeof(u32) * 2)) {
225 netdev_err(netdev, "Invalid DDP profile - size is too small.");
229 pkg_hdr_size = sizeof(u32) * (pkg_hdr->segment_count + 2U);
230 if (size < pkg_hdr_size) {
231 netdev_err(netdev, "Invalid DDP profile - too many segments");
234 for (segment = 0; segment < pkg_hdr->segment_count; ++segment) {
235 u32 offset = pkg_hdr->segment_offset[segment];
239 "Invalid DDP profile %u segment alignment",
243 if (pkg_hdr_size > offset || offset >= size) {
245 "Invalid DDP profile %u segment offset",
255 * i40e_ddp_load - performs DDP loading
256 * @netdev: net device structure
257 * @data: buffer containing recipe file
258 * @size: size of the buffer
259 * @is_add: true when loading profile, false when rolling back the previous one
261 * Checks correctness and loads DDP profile to the NIC. The function is
262 * also used for rolling back previously loaded profile.
264 int i40e_ddp_load(struct net_device *netdev, const u8 *data, size_t size,
267 u8 profile_info_sec[sizeof(struct i40e_profile_section_header) +
268 sizeof(struct i40e_profile_info)];
269 struct i40e_metadata_segment *metadata_hdr;
270 struct i40e_profile_segment *profile_hdr;
271 struct i40e_profile_info pinfo;
272 struct i40e_package_header *pkg_hdr;
274 struct i40e_netdev_priv *np = netdev_priv(netdev);
275 struct i40e_vsi *vsi = np->vsi;
276 struct i40e_pf *pf = vsi->back;
280 pkg_hdr = (struct i40e_package_header *)data;
281 if (!i40e_ddp_is_pkg_hdr_valid(netdev, pkg_hdr, size))
284 if (size < (sizeof(struct i40e_package_header) +
285 sizeof(struct i40e_metadata_segment) + sizeof(u32) * 2)) {
286 netdev_err(netdev, "Invalid DDP recipe size.");
290 /* Find beginning of segment data in buffer */
291 metadata_hdr = (struct i40e_metadata_segment *)
292 i40e_find_segment_in_package(SEGMENT_TYPE_METADATA, pkg_hdr);
294 netdev_err(netdev, "Failed to find metadata segment in DDP recipe.");
298 track_id = metadata_hdr->track_id;
299 profile_hdr = (struct i40e_profile_segment *)
300 i40e_find_segment_in_package(SEGMENT_TYPE_I40E, pkg_hdr);
302 netdev_err(netdev, "Failed to find profile segment in DDP recipe.");
306 pinfo.track_id = track_id;
307 pinfo.version = profile_hdr->version;
309 pinfo.op = I40E_DDP_ADD_TRACKID;
311 pinfo.op = I40E_DDP_REMOVE_TRACKID;
313 memcpy(pinfo.name, profile_hdr->name, I40E_DDP_NAME_SIZE);
315 /* Check if profile data already exists*/
316 istatus = i40e_ddp_does_profile_exist(&pf->hw, &pinfo);
318 netdev_err(netdev, "Failed to fetch loaded profiles.");
323 netdev_err(netdev, "DDP profile already loaded.");
326 istatus = i40e_ddp_does_profile_overlap(&pf->hw, &pinfo);
328 netdev_err(netdev, "Failed to fetch loaded profiles.");
332 netdev_err(netdev, "DDP profile overlaps with existing one.");
338 "DDP profile for deletion does not exist.");
343 /* Load profile data */
345 status = i40e_write_profile(&pf->hw, profile_hdr, track_id);
347 if (status == I40E_ERR_DEVICE_NOT_SUPPORTED) {
349 "Profile is not supported by the device.");
352 netdev_err(netdev, "Failed to write DDP profile.");
356 status = i40e_rollback_profile(&pf->hw, profile_hdr, track_id);
358 netdev_err(netdev, "Failed to remove DDP profile.");
363 /* Add/remove profile to/from profile list in FW */
365 status = i40e_add_pinfo(&pf->hw, profile_hdr, profile_info_sec,
368 netdev_err(netdev, "Failed to add DDP profile info.");
372 status = i40e_del_pinfo(&pf->hw, profile_hdr, profile_info_sec,
375 netdev_err(netdev, "Failed to restore DDP profile info.");
384 * i40e_ddp_restore - restore previously loaded profile and remove from list
385 * @pf: PF data struct
387 * Restores previously loaded profile stored on the list in driver memory.
388 * After rolling back removes entry from the list.
390 static int i40e_ddp_restore(struct i40e_pf *pf)
392 struct i40e_ddp_old_profile_list *entry;
393 struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
396 if (!list_empty(&pf->ddp_old_prof)) {
397 entry = list_first_entry(&pf->ddp_old_prof,
398 struct i40e_ddp_old_profile_list,
400 status = i40e_ddp_load(netdev, entry->old_ddp_buf,
401 entry->old_ddp_size, false);
402 list_del(&entry->list);
409 * i40e_ddp_flash - callback function for ethtool flash feature
410 * @netdev: net device structure
411 * @flash: kernel flash structure
413 * Ethtool callback function used for loading and unloading DDP profiles.
415 int i40e_ddp_flash(struct net_device *netdev, struct ethtool_flash *flash)
417 const struct firmware *ddp_config;
418 struct i40e_netdev_priv *np = netdev_priv(netdev);
419 struct i40e_vsi *vsi = np->vsi;
420 struct i40e_pf *pf = vsi->back;
423 /* Check for valid region first */
424 if (flash->region != I40_DDP_FLASH_REGION) {
425 netdev_err(netdev, "Requested firmware region is not recognized by this driver.");
428 if (pf->hw.bus.func != 0) {
429 netdev_err(netdev, "Any DDP operation is allowed only on Phy0 NIC interface");
433 /* If the user supplied "-" instead of file name rollback previously
436 if (strncmp(flash->data, "-", 2) != 0) {
437 struct i40e_ddp_old_profile_list *list_entry;
438 char profile_name[sizeof(I40E_DDP_PROFILE_PATH)
439 + I40E_DDP_PROFILE_NAME_MAX];
441 profile_name[sizeof(profile_name) - 1] = 0;
442 strncpy(profile_name, I40E_DDP_PROFILE_PATH,
443 sizeof(profile_name) - 1);
444 strncat(profile_name, flash->data, I40E_DDP_PROFILE_NAME_MAX);
445 /* Load DDP recipe. */
446 status = request_firmware(&ddp_config, profile_name,
449 netdev_err(netdev, "DDP recipe file request failed.");
453 status = i40e_ddp_load(netdev, ddp_config->data,
454 ddp_config->size, true);
458 kzalloc(sizeof(struct i40e_ddp_old_profile_list) +
459 ddp_config->size, GFP_KERNEL);
461 netdev_info(netdev, "Failed to allocate memory for previous DDP profile data.");
462 netdev_info(netdev, "New profile loaded but roll-back will be impossible.");
464 memcpy(list_entry->old_ddp_buf,
465 ddp_config->data, ddp_config->size);
466 list_entry->old_ddp_size = ddp_config->size;
467 list_add(&list_entry->list, &pf->ddp_old_prof);
471 release_firmware(ddp_config);
473 if (!list_empty(&pf->ddp_old_prof)) {
474 status = i40e_ddp_restore(pf);
476 netdev_warn(netdev, "There is no DDP profile to restore.");