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 int 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);
21 static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
23 struct ice_hw *hw = &pf->hw;
24 enum ice_status status;
26 status = ice_read_pba_string(hw, (u8 *)buf, len);
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_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
109 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
111 /* The netlist version fields are BCD formatted */
112 snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
113 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
119 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
121 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
123 snprintf(buf, len, "0x%08x", netlist->hash);
128 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
129 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
131 enum ice_version_type {
137 static const struct ice_devlink_version {
138 enum ice_version_type type;
140 int (*getter)(struct ice_pf *pf, char *buf, size_t len);
141 } ice_devlink_versions[] = {
142 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
143 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
144 running("fw.mgmt.api", ice_info_fw_api),
145 running("fw.mgmt.build", ice_info_fw_build),
146 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
147 running("fw.psid.api", ice_info_nvm_ver),
148 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
149 running("fw.app.name", ice_info_ddp_pkg_name),
150 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
151 running("fw.netlist", ice_info_netlist_ver),
152 running("fw.netlist.build", ice_info_netlist_build),
156 * ice_devlink_info_get - .info_get devlink handler
157 * @devlink: devlink instance structure
158 * @req: the devlink info request
159 * @extack: extended netdev ack structure
161 * Callback for the devlink .info_get operation. Reports information about the
164 * Return: zero on success or an error code on failure.
166 static int ice_devlink_info_get(struct devlink *devlink,
167 struct devlink_info_req *req,
168 struct netlink_ext_ack *extack)
170 struct ice_pf *pf = devlink_priv(devlink);
175 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
177 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
181 err = ice_info_get_dsn(pf, buf, sizeof(buf));
183 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
187 err = devlink_info_serial_number_put(req, buf);
189 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
193 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
194 enum ice_version_type type = ice_devlink_versions[i].type;
195 const char *key = ice_devlink_versions[i].key;
197 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
199 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
204 case ICE_VERSION_FIXED:
205 err = devlink_info_version_fixed_put(req, key, buf);
207 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
211 case ICE_VERSION_RUNNING:
212 err = devlink_info_version_running_put(req, key, buf);
214 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
218 case ICE_VERSION_STORED:
219 err = devlink_info_version_stored_put(req, key, buf);
221 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
232 * ice_devlink_flash_update - Update firmware stored in flash on the device
233 * @devlink: pointer to devlink associated with device to update
234 * @params: flash update parameters
235 * @extack: netlink extended ACK structure
237 * Perform a device flash update. The bulk of the update logic is contained
238 * within the ice_flash_pldm_image function.
240 * Returns: zero on success, or an error code on failure.
243 ice_devlink_flash_update(struct devlink *devlink,
244 struct devlink_flash_update_params *params,
245 struct netlink_ext_ack *extack)
247 struct ice_pf *pf = devlink_priv(devlink);
248 struct device *dev = &pf->pdev->dev;
249 struct ice_hw *hw = &pf->hw;
250 const struct firmware *fw;
254 if (!params->overwrite_mask) {
255 /* preserve all settings and identifiers */
256 preservation = ICE_AQC_NVM_PRESERVE_ALL;
257 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
258 /* overwrite settings, but preserve the vital device identifiers */
259 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
260 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
261 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
262 /* overwrite both settings and identifiers, preserve nothing */
263 preservation = ICE_AQC_NVM_NO_PRESERVATION;
265 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
269 if (!hw->dev_caps.common_cap.nvm_unified_update) {
270 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
274 err = ice_check_for_pending_update(pf, NULL, extack);
278 err = request_firmware(&fw, params->file_name, dev);
280 NL_SET_ERR_MSG_MOD(extack, "Unable to read file from disk");
284 devlink_flash_update_begin_notify(devlink);
285 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
286 err = ice_flash_pldm_image(pf, fw, preservation, extack);
287 devlink_flash_update_end_notify(devlink);
289 release_firmware(fw);
294 static const struct devlink_ops ice_devlink_ops = {
295 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
296 .info_get = ice_devlink_info_get,
297 .flash_update = ice_devlink_flash_update,
300 static void ice_devlink_free(void *devlink_ptr)
302 devlink_free((struct devlink *)devlink_ptr);
306 * ice_allocate_pf - Allocate devlink and return PF structure pointer
307 * @dev: the device to allocate for
309 * Allocate a devlink instance for this device and return the private area as
310 * the PF structure. The devlink memory is kept track of through devres by
311 * adding an action to remove it when unwinding.
313 struct ice_pf *ice_allocate_pf(struct device *dev)
315 struct devlink *devlink;
317 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
321 /* Add an action to teardown the devlink when unwinding the driver */
322 if (devm_add_action(dev, ice_devlink_free, devlink)) {
323 devlink_free(devlink);
327 return devlink_priv(devlink);
331 * ice_devlink_register - Register devlink interface for this PF
332 * @pf: the PF to register the devlink for.
334 * Register the devlink instance associated with this physical function.
336 * Return: zero on success or an error code on failure.
338 int ice_devlink_register(struct ice_pf *pf)
340 struct devlink *devlink = priv_to_devlink(pf);
341 struct device *dev = ice_pf_to_dev(pf);
344 err = devlink_register(devlink, dev);
346 dev_err(dev, "devlink registration failed: %d\n", err);
354 * ice_devlink_unregister - Unregister devlink resources for this PF.
355 * @pf: the PF structure to cleanup
357 * Releases resources used by devlink and cleans up associated memory.
359 void ice_devlink_unregister(struct ice_pf *pf)
361 devlink_unregister(priv_to_devlink(pf));
365 * ice_devlink_create_port - Create a devlink port for this PF
366 * @pf: the PF to create a port for
368 * Create and register a devlink_port for this PF. Note that although each
369 * physical function is connected to a separate devlink instance, the port
370 * will still be numbered according to the physical function ID.
372 * Return: zero on success or an error code on failure.
374 int ice_devlink_create_port(struct ice_pf *pf)
376 struct devlink *devlink = priv_to_devlink(pf);
377 struct ice_vsi *vsi = ice_get_main_vsi(pf);
378 struct device *dev = ice_pf_to_dev(pf);
379 struct devlink_port_attrs attrs = {};
383 dev_err(dev, "%s: unable to find main VSI\n", __func__);
387 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
388 attrs.phys.port_number = pf->hw.pf_id;
389 devlink_port_attrs_set(&pf->devlink_port, &attrs);
390 err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
392 dev_err(dev, "devlink_port_register failed: %d\n", err);
400 * ice_devlink_destroy_port - Destroy the devlink_port for this PF
401 * @pf: the PF to cleanup
403 * Unregisters the devlink_port structure associated with this PF.
405 void ice_devlink_destroy_port(struct ice_pf *pf)
407 devlink_port_type_clear(&pf->devlink_port);
408 devlink_port_unregister(&pf->devlink_port);
412 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
413 * @devlink: the devlink instance
414 * @ops: the devlink region being snapshotted
415 * @extack: extended ACK response structure
416 * @data: on exit points to snapshot data buffer
418 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
419 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
420 * contents. This snapshot can later be viewed via the devlink-region
423 * @returns zero on success, and updates the data pointer. Returns a non-zero
424 * error code on failure.
426 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
427 const struct devlink_region_ops *ops,
428 struct netlink_ext_ack *extack, u8 **data)
430 struct ice_pf *pf = devlink_priv(devlink);
431 struct device *dev = ice_pf_to_dev(pf);
432 struct ice_hw *hw = &pf->hw;
433 enum ice_status status;
437 nvm_size = hw->nvm.flash_size;
438 nvm_data = vzalloc(nvm_size);
442 status = ice_acquire_nvm(hw, ICE_RES_READ);
444 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
445 status, hw->adminq.sq_last_status);
446 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
451 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
453 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
454 nvm_size, status, hw->adminq.sq_last_status);
455 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
469 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
470 * @devlink: the devlink instance
471 * @ops: the devlink region being snapshotted
472 * @extack: extended ACK response structure
473 * @data: on exit points to snapshot data buffer
475 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
476 * the device-caps devlink region. It captures a snapshot of the device
477 * capabilities reported by firmware.
479 * @returns zero on success, and updates the data pointer. Returns a non-zero
480 * error code on failure.
483 ice_devlink_devcaps_snapshot(struct devlink *devlink,
484 const struct devlink_region_ops *ops,
485 struct netlink_ext_ack *extack, u8 **data)
487 struct ice_pf *pf = devlink_priv(devlink);
488 struct device *dev = ice_pf_to_dev(pf);
489 struct ice_hw *hw = &pf->hw;
490 enum ice_status status;
493 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
497 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
498 ice_aqc_opc_list_dev_caps, NULL);
500 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
501 status, hw->adminq.sq_last_status);
502 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
507 *data = (u8 *)devcaps;
512 static const struct devlink_region_ops ice_nvm_region_ops = {
515 .snapshot = ice_devlink_nvm_snapshot,
518 static const struct devlink_region_ops ice_devcaps_region_ops = {
519 .name = "device-caps",
521 .snapshot = ice_devlink_devcaps_snapshot,
525 * ice_devlink_init_regions - Initialize devlink regions
526 * @pf: the PF device structure
528 * Create devlink regions used to enable access to dump the contents of the
529 * flash memory on the device.
531 void ice_devlink_init_regions(struct ice_pf *pf)
533 struct devlink *devlink = priv_to_devlink(pf);
534 struct device *dev = ice_pf_to_dev(pf);
537 nvm_size = pf->hw.nvm.flash_size;
538 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
540 if (IS_ERR(pf->nvm_region)) {
541 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
542 PTR_ERR(pf->nvm_region));
543 pf->nvm_region = NULL;
546 pf->devcaps_region = devlink_region_create(devlink,
547 &ice_devcaps_region_ops, 10,
549 if (IS_ERR(pf->devcaps_region)) {
550 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
551 PTR_ERR(pf->devcaps_region));
552 pf->devcaps_region = NULL;
557 * ice_devlink_destroy_regions - Destroy devlink regions
558 * @pf: the PF device structure
560 * Remove previously created regions for this PF.
562 void ice_devlink_destroy_regions(struct ice_pf *pf)
565 devlink_region_destroy(pf->nvm_region);
566 if (pf->devcaps_region)
567 devlink_region_destroy(pf->devcaps_region);