1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
4 #include <linux/firmware.h>
7 #define I40_DDP_FLASH_REGION 100
8 #define I40E_PROFILE_INFO_SIZE 48
9 #define I40E_MAX_PROFILE_NUM 16
10 #define I40E_PROFILE_LIST_SIZE \
11 (I40E_PROFILE_INFO_SIZE * I40E_MAX_PROFILE_NUM + 4)
12 #define I40E_DDP_PROFILE_PATH "intel/i40e/ddp/"
13 #define I40E_DDP_PROFILE_NAME_MAX 64
15 struct i40e_ddp_profile_list
{
17 struct i40e_profile_info p_info
[];
20 struct i40e_ddp_old_profile_list
{
21 struct list_head list
;
27 * i40e_ddp_profiles_eq - checks if DDP profiles are the equivalent
28 * @a: new profile info
29 * @b: old profile info
31 * checks if DDP profiles are the equivalent.
32 * Returns true if profiles are the same.
34 static bool i40e_ddp_profiles_eq(struct i40e_profile_info
*a
,
35 struct i40e_profile_info
*b
)
37 return a
->track_id
== b
->track_id
&&
38 !memcmp(&a
->version
, &b
->version
, sizeof(a
->version
)) &&
39 !memcmp(&a
->name
, &b
->name
, I40E_DDP_NAME_SIZE
);
43 * i40e_ddp_does_profile_exist - checks if DDP profile loaded already
44 * @hw: HW data structure
45 * @pinfo: DDP profile information structure
47 * checks if DDP profile loaded already.
48 * Returns >0 if the profile exists.
49 * Returns 0 if the profile is absent.
50 * Returns <0 if error.
52 static int i40e_ddp_does_profile_exist(struct i40e_hw
*hw
,
53 struct i40e_profile_info
*pinfo
)
55 struct i40e_ddp_profile_list
*profile_list
;
56 u8 buff
[I40E_PROFILE_LIST_SIZE
];
60 status
= i40e_aq_get_ddp_list(hw
, buff
, I40E_PROFILE_LIST_SIZE
, 0,
65 profile_list
= (struct i40e_ddp_profile_list
*)buff
;
66 for (i
= 0; i
< profile_list
->p_count
; i
++) {
67 if (i40e_ddp_profiles_eq(pinfo
, &profile_list
->p_info
[i
]))
74 * i40e_ddp_profiles_overlap - checks if DDP profiles overlap.
75 * @new: new profile info
76 * @old: old profile info
78 * checks if DDP profiles overlap.
79 * Returns true if profiles are overlap.
81 static bool i40e_ddp_profiles_overlap(struct i40e_profile_info
*new,
82 struct i40e_profile_info
*old
)
84 unsigned int group_id_old
= FIELD_GET(0x00FF0000, old
->track_id
);
85 unsigned int group_id_new
= FIELD_GET(0x00FF0000, new->track_id
);
87 /* 0x00 group must be only the first */
88 if (group_id_new
== 0)
90 /* 0xFF group is compatible with anything else */
91 if (group_id_new
== 0xFF || group_id_old
== 0xFF)
93 /* otherwise only profiles from the same group are compatible*/
94 return group_id_old
!= group_id_new
;
98 * i40e_ddp_does_profile_overlap - checks if DDP overlaps with existing one.
99 * @hw: HW data structure
100 * @pinfo: DDP profile information structure
102 * checks if DDP profile overlaps with existing one.
103 * Returns >0 if the profile overlaps.
104 * Returns 0 if the profile is ok.
105 * Returns <0 if error.
107 static int i40e_ddp_does_profile_overlap(struct i40e_hw
*hw
,
108 struct i40e_profile_info
*pinfo
)
110 struct i40e_ddp_profile_list
*profile_list
;
111 u8 buff
[I40E_PROFILE_LIST_SIZE
];
115 status
= i40e_aq_get_ddp_list(hw
, buff
, I40E_PROFILE_LIST_SIZE
, 0,
120 profile_list
= (struct i40e_ddp_profile_list
*)buff
;
121 for (i
= 0; i
< profile_list
->p_count
; i
++) {
122 if (i40e_ddp_profiles_overlap(pinfo
,
123 &profile_list
->p_info
[i
]))
131 * @hw: pointer to the hardware structure
132 * @profile: pointer to the profile segment of the package
133 * @profile_info_sec: buffer for information section
134 * @track_id: package tracking id
136 * Register a profile to the list of loaded profiles.
139 i40e_add_pinfo(struct i40e_hw
*hw
, struct i40e_profile_segment
*profile
,
140 u8
*profile_info_sec
, u32 track_id
)
142 struct i40e_profile_section_header
*sec
;
143 struct i40e_profile_info
*pinfo
;
144 u32 offset
= 0, info
= 0;
147 sec
= (struct i40e_profile_section_header
*)profile_info_sec
;
149 sec
->data_end
= sizeof(struct i40e_profile_section_header
) +
150 sizeof(struct i40e_profile_info
);
151 sec
->section
.type
= SECTION_TYPE_INFO
;
152 sec
->section
.offset
= sizeof(struct i40e_profile_section_header
);
153 sec
->section
.size
= sizeof(struct i40e_profile_info
);
154 pinfo
= (struct i40e_profile_info
*)(profile_info_sec
+
155 sec
->section
.offset
);
156 pinfo
->track_id
= track_id
;
157 pinfo
->version
= profile
->version
;
158 pinfo
->op
= I40E_DDP_ADD_TRACKID
;
160 /* Clear reserved field */
161 memset(pinfo
->reserved
, 0, sizeof(pinfo
->reserved
));
162 memcpy(pinfo
->name
, profile
->name
, I40E_DDP_NAME_SIZE
);
164 status
= i40e_aq_write_ddp(hw
, (void *)sec
, sec
->data_end
,
165 track_id
, &offset
, &info
, NULL
);
170 * i40e_del_pinfo - delete DDP profile info from NIC
171 * @hw: HW data structure
172 * @profile: DDP profile segment to be deleted
173 * @profile_info_sec: DDP profile section header
174 * @track_id: track ID of the profile for deletion
176 * Removes DDP profile from the NIC.
179 i40e_del_pinfo(struct i40e_hw
*hw
, struct i40e_profile_segment
*profile
,
180 u8
*profile_info_sec
, u32 track_id
)
182 struct i40e_profile_section_header
*sec
;
183 struct i40e_profile_info
*pinfo
;
184 u32 offset
= 0, info
= 0;
187 sec
= (struct i40e_profile_section_header
*)profile_info_sec
;
189 sec
->data_end
= sizeof(struct i40e_profile_section_header
) +
190 sizeof(struct i40e_profile_info
);
191 sec
->section
.type
= SECTION_TYPE_INFO
;
192 sec
->section
.offset
= sizeof(struct i40e_profile_section_header
);
193 sec
->section
.size
= sizeof(struct i40e_profile_info
);
194 pinfo
= (struct i40e_profile_info
*)(profile_info_sec
+
195 sec
->section
.offset
);
196 pinfo
->track_id
= track_id
;
197 pinfo
->version
= profile
->version
;
198 pinfo
->op
= I40E_DDP_REMOVE_TRACKID
;
200 /* Clear reserved field */
201 memset(pinfo
->reserved
, 0, sizeof(pinfo
->reserved
));
202 memcpy(pinfo
->name
, profile
->name
, I40E_DDP_NAME_SIZE
);
204 status
= i40e_aq_write_ddp(hw
, (void *)sec
, sec
->data_end
,
205 track_id
, &offset
, &info
, NULL
);
210 * i40e_ddp_is_pkg_hdr_valid - performs basic pkg header integrity checks
211 * @netdev: net device structure (for logging purposes)
212 * @pkg_hdr: pointer to package header
213 * @size_huge: size of the whole DDP profile package in size_t
215 * Checks correctness of pkg header: Version, size too big/small, and
216 * all segment offsets alignment and boundaries. This function lets
217 * reject non DDP profile file to be loaded by administrator mistake.
219 static bool i40e_ddp_is_pkg_hdr_valid(struct net_device
*netdev
,
220 struct i40e_package_header
*pkg_hdr
,
223 u32 size
= 0xFFFFFFFFU
& size_huge
;
230 if (pkg_hdr
->version
.major
> 0) {
231 struct i40e_ddp_version ver
= pkg_hdr
->version
;
233 netdev_err(netdev
, "Unsupported DDP profile version %u.%u.%u.%u",
234 ver
.major
, ver
.minor
, ver
.update
, ver
.draft
);
237 if (size_huge
> size
) {
238 netdev_err(netdev
, "Invalid DDP profile - size is bigger than 4G");
241 if (size
< (sizeof(struct i40e_package_header
) + sizeof(u32
) +
242 sizeof(struct i40e_metadata_segment
) + sizeof(u32
) * 2)) {
243 netdev_err(netdev
, "Invalid DDP profile - size is too small.");
247 pkg_hdr_size
= sizeof(u32
) * (pkg_hdr
->segment_count
+ 2U);
248 if (size
< pkg_hdr_size
) {
249 netdev_err(netdev
, "Invalid DDP profile - too many segments");
252 for (segment
= 0; segment
< pkg_hdr
->segment_count
; ++segment
) {
253 u32 offset
= pkg_hdr
->segment_offset
[segment
];
257 "Invalid DDP profile %u segment alignment",
261 if (pkg_hdr_size
> offset
|| offset
>= size
) {
263 "Invalid DDP profile %u segment offset",
273 * i40e_ddp_load - performs DDP loading
274 * @netdev: net device structure
275 * @data: buffer containing recipe file
276 * @size: size of the buffer
277 * @is_add: true when loading profile, false when rolling back the previous one
279 * Checks correctness and loads DDP profile to the NIC. The function is
280 * also used for rolling back previously loaded profile.
282 static int i40e_ddp_load(struct net_device
*netdev
, const u8
*data
, size_t size
,
285 u8 profile_info_sec
[sizeof(struct i40e_profile_section_header
) +
286 sizeof(struct i40e_profile_info
)];
287 struct i40e_metadata_segment
*metadata_hdr
;
288 struct i40e_profile_segment
*profile_hdr
;
289 struct i40e_profile_info pinfo
;
290 struct i40e_package_header
*pkg_hdr
;
291 struct i40e_netdev_priv
*np
= netdev_priv(netdev
);
292 struct i40e_vsi
*vsi
= np
->vsi
;
293 struct i40e_pf
*pf
= vsi
->back
;
298 pkg_hdr
= (struct i40e_package_header
*)data
;
299 if (!i40e_ddp_is_pkg_hdr_valid(netdev
, pkg_hdr
, size
))
302 if (size
< (sizeof(struct i40e_package_header
) + sizeof(u32
) +
303 sizeof(struct i40e_metadata_segment
) + sizeof(u32
) * 2)) {
304 netdev_err(netdev
, "Invalid DDP recipe size.");
308 /* Find beginning of segment data in buffer */
309 metadata_hdr
= (struct i40e_metadata_segment
*)
310 i40e_find_segment_in_package(SEGMENT_TYPE_METADATA
, pkg_hdr
);
312 netdev_err(netdev
, "Failed to find metadata segment in DDP recipe.");
316 track_id
= metadata_hdr
->track_id
;
317 profile_hdr
= (struct i40e_profile_segment
*)
318 i40e_find_segment_in_package(SEGMENT_TYPE_I40E
, pkg_hdr
);
320 netdev_err(netdev
, "Failed to find profile segment in DDP recipe.");
324 pinfo
.track_id
= track_id
;
325 pinfo
.version
= profile_hdr
->version
;
327 pinfo
.op
= I40E_DDP_ADD_TRACKID
;
329 pinfo
.op
= I40E_DDP_REMOVE_TRACKID
;
331 memcpy(pinfo
.name
, profile_hdr
->name
, I40E_DDP_NAME_SIZE
);
333 /* Check if profile data already exists*/
334 istatus
= i40e_ddp_does_profile_exist(&pf
->hw
, &pinfo
);
336 netdev_err(netdev
, "Failed to fetch loaded profiles.");
341 netdev_err(netdev
, "DDP profile already loaded.");
344 istatus
= i40e_ddp_does_profile_overlap(&pf
->hw
, &pinfo
);
346 netdev_err(netdev
, "Failed to fetch loaded profiles.");
350 netdev_err(netdev
, "DDP profile overlaps with existing one.");
356 "DDP profile for deletion does not exist.");
361 /* Load profile data */
363 status
= i40e_write_profile(&pf
->hw
, profile_hdr
, track_id
);
365 if (status
== -ENODEV
) {
367 "Profile is not supported by the device.");
370 netdev_err(netdev
, "Failed to write DDP profile.");
374 status
= i40e_rollback_profile(&pf
->hw
, profile_hdr
, track_id
);
376 netdev_err(netdev
, "Failed to remove DDP profile.");
381 /* Add/remove profile to/from profile list in FW */
383 status
= i40e_add_pinfo(&pf
->hw
, profile_hdr
, profile_info_sec
,
386 netdev_err(netdev
, "Failed to add DDP profile info.");
390 status
= i40e_del_pinfo(&pf
->hw
, profile_hdr
, profile_info_sec
,
393 netdev_err(netdev
, "Failed to restore DDP profile info.");
402 * i40e_ddp_restore - restore previously loaded profile and remove from list
403 * @pf: PF data struct
405 * Restores previously loaded profile stored on the list in driver memory.
406 * After rolling back removes entry from the list.
408 static int i40e_ddp_restore(struct i40e_pf
*pf
)
410 struct i40e_vsi
*vsi
= i40e_pf_get_main_vsi(pf
);
411 struct net_device
*netdev
= vsi
->netdev
;
412 struct i40e_ddp_old_profile_list
*entry
;
415 if (!list_empty(&pf
->ddp_old_prof
)) {
416 entry
= list_first_entry(&pf
->ddp_old_prof
,
417 struct i40e_ddp_old_profile_list
,
419 status
= i40e_ddp_load(netdev
, entry
->old_ddp_buf
,
420 entry
->old_ddp_size
, false);
421 list_del(&entry
->list
);
428 * i40e_ddp_flash - callback function for ethtool flash feature
429 * @netdev: net device structure
430 * @flash: kernel flash structure
432 * Ethtool callback function used for loading and unloading DDP profiles.
434 int i40e_ddp_flash(struct net_device
*netdev
, struct ethtool_flash
*flash
)
436 const struct firmware
*ddp_config
;
437 struct i40e_netdev_priv
*np
= netdev_priv(netdev
);
438 struct i40e_vsi
*vsi
= np
->vsi
;
439 struct i40e_pf
*pf
= vsi
->back
;
442 /* Check for valid region first */
443 if (flash
->region
!= I40_DDP_FLASH_REGION
) {
444 netdev_err(netdev
, "Requested firmware region is not recognized by this driver.");
447 if (pf
->hw
.bus
.func
!= 0) {
448 netdev_err(netdev
, "Any DDP operation is allowed only on Phy0 NIC interface");
452 /* If the user supplied "-" instead of file name rollback previously
455 if (strncmp(flash
->data
, "-", 2) != 0) {
456 struct i40e_ddp_old_profile_list
*list_entry
;
457 char profile_name
[sizeof(I40E_DDP_PROFILE_PATH
)
458 + I40E_DDP_PROFILE_NAME_MAX
];
460 scnprintf(profile_name
, sizeof(profile_name
), "%s%s",
461 I40E_DDP_PROFILE_PATH
, flash
->data
);
463 /* Load DDP recipe. */
464 status
= request_firmware(&ddp_config
, profile_name
,
467 netdev_err(netdev
, "DDP recipe file request failed.");
471 status
= i40e_ddp_load(netdev
, ddp_config
->data
,
472 ddp_config
->size
, true);
476 kzalloc(sizeof(struct i40e_ddp_old_profile_list
) +
477 ddp_config
->size
, GFP_KERNEL
);
479 netdev_info(netdev
, "Failed to allocate memory for previous DDP profile data.");
480 netdev_info(netdev
, "New profile loaded but roll-back will be impossible.");
482 memcpy(list_entry
->old_ddp_buf
,
483 ddp_config
->data
, ddp_config
->size
);
484 list_entry
->old_ddp_size
= ddp_config
->size
;
485 list_add(&list_entry
->list
, &pf
->ddp_old_prof
);
489 release_firmware(ddp_config
);
491 if (!list_empty(&pf
->ddp_old_prof
)) {
492 status
= i40e_ddp_restore(pf
);
494 netdev_warn(netdev
, "There is no DDP profile to restore.");