1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
6 #include "ice_devlink.h"
7 #include "ice_fw_update.h"
9 static void ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len)
13 /* Copy the DSN into an array in Big Endian format */
14 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
16 snprintf(buf, len, "%8phD", dsn);
19 static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
21 struct ice_hw *hw = &pf->hw;
22 enum ice_status status;
24 status = ice_read_pba_string(hw, (u8 *)buf, len);
26 /* We failed to locate the PBA, so just skip this entry */
27 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %s\n",
28 ice_stat_str(status));
33 static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len)
35 struct ice_hw *hw = &pf->hw;
37 snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
43 static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len)
45 struct ice_hw *hw = &pf->hw;
47 snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver);
52 static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len)
54 struct ice_hw *hw = &pf->hw;
56 snprintf(buf, len, "0x%08x", hw->fw_build);
61 static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len)
63 struct ice_orom_info *orom = &pf->hw.nvm.orom;
65 snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch);
70 static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len)
72 struct ice_nvm_info *nvm = &pf->hw.nvm;
74 snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver);
79 static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len)
81 struct ice_nvm_info *nvm = &pf->hw.nvm;
83 snprintf(buf, len, "0x%08x", nvm->eetrack);
88 static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len)
90 struct ice_hw *hw = &pf->hw;
92 snprintf(buf, len, "%s", hw->active_pkg_name);
97 static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len)
99 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
101 snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
107 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, char *buf, size_t len)
109 snprintf(buf, len, "0x%08x", pf->hw.active_track_id);
114 static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
116 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
118 /* The netlist version fields are BCD formatted */
119 snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
120 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
126 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
128 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
130 snprintf(buf, len, "0x%08x", netlist->hash);
135 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
136 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
138 enum ice_version_type {
144 static const struct ice_devlink_version {
145 enum ice_version_type type;
147 int (*getter)(struct ice_pf *pf, char *buf, size_t len);
148 } ice_devlink_versions[] = {
149 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
150 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
151 running("fw.mgmt.api", ice_info_fw_api),
152 running("fw.mgmt.build", ice_info_fw_build),
153 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
154 running("fw.psid.api", ice_info_nvm_ver),
155 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
156 running("fw.app.name", ice_info_ddp_pkg_name),
157 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
158 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
159 running("fw.netlist", ice_info_netlist_ver),
160 running("fw.netlist.build", ice_info_netlist_build),
164 * ice_devlink_info_get - .info_get devlink handler
165 * @devlink: devlink instance structure
166 * @req: the devlink info request
167 * @extack: extended netdev ack structure
169 * Callback for the devlink .info_get operation. Reports information about the
172 * Return: zero on success or an error code on failure.
174 static int ice_devlink_info_get(struct devlink *devlink,
175 struct devlink_info_req *req,
176 struct netlink_ext_ack *extack)
178 struct ice_pf *pf = devlink_priv(devlink);
183 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
185 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
189 ice_info_get_dsn(pf, buf, sizeof(buf));
191 err = devlink_info_serial_number_put(req, buf);
193 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
197 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
198 enum ice_version_type type = ice_devlink_versions[i].type;
199 const char *key = ice_devlink_versions[i].key;
201 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
203 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
208 case ICE_VERSION_FIXED:
209 err = devlink_info_version_fixed_put(req, key, buf);
211 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
215 case ICE_VERSION_RUNNING:
216 err = devlink_info_version_running_put(req, key, buf);
218 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
222 case ICE_VERSION_STORED:
223 err = devlink_info_version_stored_put(req, key, buf);
225 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
236 * ice_devlink_flash_update - Update firmware stored in flash on the device
237 * @devlink: pointer to devlink associated with device to update
238 * @params: flash update parameters
239 * @extack: netlink extended ACK structure
241 * Perform a device flash update. The bulk of the update logic is contained
242 * within the ice_flash_pldm_image function.
244 * Returns: zero on success, or an error code on failure.
247 ice_devlink_flash_update(struct devlink *devlink,
248 struct devlink_flash_update_params *params,
249 struct netlink_ext_ack *extack)
251 struct ice_pf *pf = devlink_priv(devlink);
252 struct device *dev = &pf->pdev->dev;
253 struct ice_hw *hw = &pf->hw;
254 const struct firmware *fw;
258 if (!params->overwrite_mask) {
259 /* preserve all settings and identifiers */
260 preservation = ICE_AQC_NVM_PRESERVE_ALL;
261 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
262 /* overwrite settings, but preserve the vital device identifiers */
263 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
264 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
265 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
266 /* overwrite both settings and identifiers, preserve nothing */
267 preservation = ICE_AQC_NVM_NO_PRESERVATION;
269 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
273 if (!hw->dev_caps.common_cap.nvm_unified_update) {
274 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
278 err = ice_check_for_pending_update(pf, NULL, extack);
282 err = request_firmware(&fw, params->file_name, dev);
284 NL_SET_ERR_MSG_MOD(extack, "Unable to read file from disk");
288 dev_dbg(dev, "Beginning flash update with file '%s'\n", params->file_name);
290 devlink_flash_update_begin_notify(devlink);
291 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
292 err = ice_flash_pldm_image(pf, fw, preservation, extack);
293 devlink_flash_update_end_notify(devlink);
295 release_firmware(fw);
300 static const struct devlink_ops ice_devlink_ops = {
301 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
302 .info_get = ice_devlink_info_get,
303 .flash_update = ice_devlink_flash_update,
306 static void ice_devlink_free(void *devlink_ptr)
308 devlink_free((struct devlink *)devlink_ptr);
312 * ice_allocate_pf - Allocate devlink and return PF structure pointer
313 * @dev: the device to allocate for
315 * Allocate a devlink instance for this device and return the private area as
316 * the PF structure. The devlink memory is kept track of through devres by
317 * adding an action to remove it when unwinding.
319 struct ice_pf *ice_allocate_pf(struct device *dev)
321 struct devlink *devlink;
323 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
327 /* Add an action to teardown the devlink when unwinding the driver */
328 if (devm_add_action(dev, ice_devlink_free, devlink)) {
329 devlink_free(devlink);
333 return devlink_priv(devlink);
337 * ice_devlink_register - Register devlink interface for this PF
338 * @pf: the PF to register the devlink for.
340 * Register the devlink instance associated with this physical function.
342 * Return: zero on success or an error code on failure.
344 int ice_devlink_register(struct ice_pf *pf)
346 struct devlink *devlink = priv_to_devlink(pf);
347 struct device *dev = ice_pf_to_dev(pf);
350 err = devlink_register(devlink, dev);
352 dev_err(dev, "devlink registration failed: %d\n", err);
360 * ice_devlink_unregister - Unregister devlink resources for this PF.
361 * @pf: the PF structure to cleanup
363 * Releases resources used by devlink and cleans up associated memory.
365 void ice_devlink_unregister(struct ice_pf *pf)
367 devlink_unregister(priv_to_devlink(pf));
371 * ice_devlink_create_port - Create a devlink port for this VSI
372 * @vsi: the VSI to create a port for
374 * Create and register a devlink_port for this VSI.
376 * Return: zero on success or an error code on failure.
378 int ice_devlink_create_port(struct ice_vsi *vsi)
380 struct devlink_port_attrs attrs = {};
381 struct ice_port_info *pi;
382 struct devlink *devlink;
387 /* Currently we only create devlink_port instances for PF VSIs */
388 if (vsi->type != ICE_VSI_PF)
392 devlink = priv_to_devlink(pf);
393 dev = ice_pf_to_dev(pf);
394 pi = pf->hw.port_info;
396 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
397 attrs.phys.port_number = pi->lport;
398 devlink_port_attrs_set(&vsi->devlink_port, &attrs);
399 err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
401 dev_err(dev, "devlink_port_register failed: %d\n", err);
405 vsi->devlink_port_registered = true;
411 * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
412 * @vsi: the VSI to cleanup
414 * Unregisters the devlink_port structure associated with this VSI.
416 void ice_devlink_destroy_port(struct ice_vsi *vsi)
418 if (!vsi->devlink_port_registered)
421 devlink_port_type_clear(&vsi->devlink_port);
422 devlink_port_unregister(&vsi->devlink_port);
424 vsi->devlink_port_registered = false;
428 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
429 * @devlink: the devlink instance
430 * @ops: the devlink region being snapshotted
431 * @extack: extended ACK response structure
432 * @data: on exit points to snapshot data buffer
434 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
435 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
436 * contents. This snapshot can later be viewed via the devlink-region
439 * @returns zero on success, and updates the data pointer. Returns a non-zero
440 * error code on failure.
442 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
443 const struct devlink_region_ops *ops,
444 struct netlink_ext_ack *extack, u8 **data)
446 struct ice_pf *pf = devlink_priv(devlink);
447 struct device *dev = ice_pf_to_dev(pf);
448 struct ice_hw *hw = &pf->hw;
449 enum ice_status status;
453 nvm_size = hw->nvm.flash_size;
454 nvm_data = vzalloc(nvm_size);
458 status = ice_acquire_nvm(hw, ICE_RES_READ);
460 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
461 status, hw->adminq.sq_last_status);
462 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
467 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
469 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
470 nvm_size, status, hw->adminq.sq_last_status);
471 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
485 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
486 * @devlink: the devlink instance
487 * @ops: the devlink region being snapshotted
488 * @extack: extended ACK response structure
489 * @data: on exit points to snapshot data buffer
491 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
492 * the device-caps devlink region. It captures a snapshot of the device
493 * capabilities reported by firmware.
495 * @returns zero on success, and updates the data pointer. Returns a non-zero
496 * error code on failure.
499 ice_devlink_devcaps_snapshot(struct devlink *devlink,
500 const struct devlink_region_ops *ops,
501 struct netlink_ext_ack *extack, u8 **data)
503 struct ice_pf *pf = devlink_priv(devlink);
504 struct device *dev = ice_pf_to_dev(pf);
505 struct ice_hw *hw = &pf->hw;
506 enum ice_status status;
509 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
513 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
514 ice_aqc_opc_list_dev_caps, NULL);
516 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
517 status, hw->adminq.sq_last_status);
518 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
523 *data = (u8 *)devcaps;
528 static const struct devlink_region_ops ice_nvm_region_ops = {
531 .snapshot = ice_devlink_nvm_snapshot,
534 static const struct devlink_region_ops ice_devcaps_region_ops = {
535 .name = "device-caps",
537 .snapshot = ice_devlink_devcaps_snapshot,
541 * ice_devlink_init_regions - Initialize devlink regions
542 * @pf: the PF device structure
544 * Create devlink regions used to enable access to dump the contents of the
545 * flash memory on the device.
547 void ice_devlink_init_regions(struct ice_pf *pf)
549 struct devlink *devlink = priv_to_devlink(pf);
550 struct device *dev = ice_pf_to_dev(pf);
553 nvm_size = pf->hw.nvm.flash_size;
554 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
556 if (IS_ERR(pf->nvm_region)) {
557 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
558 PTR_ERR(pf->nvm_region));
559 pf->nvm_region = NULL;
562 pf->devcaps_region = devlink_region_create(devlink,
563 &ice_devcaps_region_ops, 10,
565 if (IS_ERR(pf->devcaps_region)) {
566 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
567 PTR_ERR(pf->devcaps_region));
568 pf->devcaps_region = NULL;
573 * ice_devlink_destroy_regions - Destroy devlink regions
574 * @pf: the PF device structure
576 * Remove previously created regions for this PF.
578 void ice_devlink_destroy_regions(struct ice_pf *pf)
581 devlink_region_destroy(pf->nvm_region);
582 if (pf->devcaps_region)
583 devlink_region_destroy(pf->devcaps_region);