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, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
17 dsn[0], dsn[1], dsn[2], dsn[3],
18 dsn[4], dsn[5], dsn[6], dsn[7]);
23 static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
25 struct ice_hw *hw = &pf->hw;
26 enum ice_status status;
28 status = ice_read_pba_string(hw, (u8 *)buf, len);
35 static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len)
37 struct ice_hw *hw = &pf->hw;
39 snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
45 static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len)
47 struct ice_hw *hw = &pf->hw;
49 snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver);
54 static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len)
56 struct ice_hw *hw = &pf->hw;
58 snprintf(buf, len, "0x%08x", hw->fw_build);
63 static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len)
65 struct ice_orom_info *orom = &pf->hw.nvm.orom;
67 snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch);
72 static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len)
74 struct ice_nvm_info *nvm = &pf->hw.nvm;
76 snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver);
81 static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len)
83 struct ice_nvm_info *nvm = &pf->hw.nvm;
85 snprintf(buf, len, "0x%08x", nvm->eetrack);
90 static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len)
92 struct ice_hw *hw = &pf->hw;
94 snprintf(buf, len, "%s", hw->active_pkg_name);
99 static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len)
101 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
103 snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
109 static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
111 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
113 /* The netlist version fields are BCD formatted */
114 snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
115 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
121 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
123 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
125 snprintf(buf, len, "0x%08x", netlist->hash);
130 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
131 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
133 enum ice_version_type {
139 static const struct ice_devlink_version {
140 enum ice_version_type type;
142 int (*getter)(struct ice_pf *pf, char *buf, size_t len);
143 } ice_devlink_versions[] = {
144 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
145 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
146 running("fw.mgmt.api", ice_info_fw_api),
147 running("fw.mgmt.build", ice_info_fw_build),
148 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
149 running("fw.psid.api", ice_info_nvm_ver),
150 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
151 running("fw.app.name", ice_info_ddp_pkg_name),
152 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
153 running("fw.netlist", ice_info_netlist_ver),
154 running("fw.netlist.build", ice_info_netlist_build),
158 * ice_devlink_info_get - .info_get devlink handler
159 * @devlink: devlink instance structure
160 * @req: the devlink info request
161 * @extack: extended netdev ack structure
163 * Callback for the devlink .info_get operation. Reports information about the
166 * Return: zero on success or an error code on failure.
168 static int ice_devlink_info_get(struct devlink *devlink,
169 struct devlink_info_req *req,
170 struct netlink_ext_ack *extack)
172 struct ice_pf *pf = devlink_priv(devlink);
177 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
179 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
183 err = ice_info_get_dsn(pf, buf, sizeof(buf));
185 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
189 err = devlink_info_serial_number_put(req, buf);
191 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
195 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
196 enum ice_version_type type = ice_devlink_versions[i].type;
197 const char *key = ice_devlink_versions[i].key;
199 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
201 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
206 case ICE_VERSION_FIXED:
207 err = devlink_info_version_fixed_put(req, key, buf);
209 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
213 case ICE_VERSION_RUNNING:
214 err = devlink_info_version_running_put(req, key, buf);
216 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
220 case ICE_VERSION_STORED:
221 err = devlink_info_version_stored_put(req, key, buf);
223 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
234 * ice_devlink_flash_update - Update firmware stored in flash on the device
235 * @devlink: pointer to devlink associated with device to update
236 * @path: the path of the firmware file to use via request_firmware
237 * @component: name of the component to update, or NULL
238 * @extack: netlink extended ACK structure
240 * Perform a device flash update. The bulk of the update logic is contained
241 * within the ice_flash_pldm_image function.
243 * Returns: zero on success, or an error code on failure.
246 ice_devlink_flash_update(struct devlink *devlink, const char *path,
247 const char *component, struct netlink_ext_ack *extack)
249 struct ice_pf *pf = devlink_priv(devlink);
250 struct device *dev = &pf->pdev->dev;
251 struct ice_hw *hw = &pf->hw;
252 const struct firmware *fw;
255 /* individual component update is not yet supported */
259 if (!hw->dev_caps.common_cap.nvm_unified_update) {
260 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
264 err = ice_check_for_pending_update(pf, component, extack);
268 err = request_firmware(&fw, path, dev);
270 NL_SET_ERR_MSG_MOD(extack, "Unable to read file from disk");
274 devlink_flash_update_begin_notify(devlink);
275 devlink_flash_update_status_notify(devlink, "Preparing to flash",
277 err = ice_flash_pldm_image(pf, fw, extack);
278 devlink_flash_update_end_notify(devlink);
280 release_firmware(fw);
285 static const struct devlink_ops ice_devlink_ops = {
286 .info_get = ice_devlink_info_get,
287 .flash_update = ice_devlink_flash_update,
290 static void ice_devlink_free(void *devlink_ptr)
292 devlink_free((struct devlink *)devlink_ptr);
296 * ice_allocate_pf - Allocate devlink and return PF structure pointer
297 * @dev: the device to allocate for
299 * Allocate a devlink instance for this device and return the private area as
300 * the PF structure. The devlink memory is kept track of through devres by
301 * adding an action to remove it when unwinding.
303 struct ice_pf *ice_allocate_pf(struct device *dev)
305 struct devlink *devlink;
307 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
311 /* Add an action to teardown the devlink when unwinding the driver */
312 if (devm_add_action(dev, ice_devlink_free, devlink)) {
313 devlink_free(devlink);
317 return devlink_priv(devlink);
321 * ice_devlink_register - Register devlink interface for this PF
322 * @pf: the PF to register the devlink for.
324 * Register the devlink instance associated with this physical function.
326 * Return: zero on success or an error code on failure.
328 int ice_devlink_register(struct ice_pf *pf)
330 struct devlink *devlink = priv_to_devlink(pf);
331 struct device *dev = ice_pf_to_dev(pf);
334 err = devlink_register(devlink, dev);
336 dev_err(dev, "devlink registration failed: %d\n", err);
344 * ice_devlink_unregister - Unregister devlink resources for this PF.
345 * @pf: the PF structure to cleanup
347 * Releases resources used by devlink and cleans up associated memory.
349 void ice_devlink_unregister(struct ice_pf *pf)
351 devlink_unregister(priv_to_devlink(pf));
355 * ice_devlink_create_port - Create a devlink port for this PF
356 * @pf: the PF to create a port for
358 * Create and register a devlink_port for this PF. Note that although each
359 * physical function is connected to a separate devlink instance, the port
360 * will still be numbered according to the physical function ID.
362 * Return: zero on success or an error code on failure.
364 int ice_devlink_create_port(struct ice_pf *pf)
366 struct devlink *devlink = priv_to_devlink(pf);
367 struct ice_vsi *vsi = ice_get_main_vsi(pf);
368 struct device *dev = ice_pf_to_dev(pf);
369 struct devlink_port_attrs attrs = {};
373 dev_err(dev, "%s: unable to find main VSI\n", __func__);
377 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
378 attrs.phys.port_number = pf->hw.pf_id;
379 devlink_port_attrs_set(&pf->devlink_port, &attrs);
380 err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
382 dev_err(dev, "devlink_port_register failed: %d\n", err);
390 * ice_devlink_destroy_port - Destroy the devlink_port for this PF
391 * @pf: the PF to cleanup
393 * Unregisters the devlink_port structure associated with this PF.
395 void ice_devlink_destroy_port(struct ice_pf *pf)
397 devlink_port_type_clear(&pf->devlink_port);
398 devlink_port_unregister(&pf->devlink_port);
402 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
403 * @devlink: the devlink instance
404 * @extack: extended ACK response structure
405 * @data: on exit points to snapshot data buffer
407 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
408 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
409 * contents. This snapshot can later be viewed via the devlink-region
412 * @returns zero on success, and updates the data pointer. Returns a non-zero
413 * error code on failure.
415 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
416 struct netlink_ext_ack *extack, u8 **data)
418 struct ice_pf *pf = devlink_priv(devlink);
419 struct device *dev = ice_pf_to_dev(pf);
420 struct ice_hw *hw = &pf->hw;
421 enum ice_status status;
425 nvm_size = hw->nvm.flash_size;
426 nvm_data = vzalloc(nvm_size);
430 status = ice_acquire_nvm(hw, ICE_RES_READ);
432 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
433 status, hw->adminq.sq_last_status);
434 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
439 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
441 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
442 nvm_size, status, hw->adminq.sq_last_status);
443 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
457 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
458 * @devlink: the devlink instance
459 * @extack: extended ACK response structure
460 * @data: on exit points to snapshot data buffer
462 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
463 * the device-caps devlink region. It captures a snapshot of the device
464 * capabilities reported by firmware.
466 * @returns zero on success, and updates the data pointer. Returns a non-zero
467 * error code on failure.
470 ice_devlink_devcaps_snapshot(struct devlink *devlink,
471 struct netlink_ext_ack *extack, u8 **data)
473 struct ice_pf *pf = devlink_priv(devlink);
474 struct device *dev = ice_pf_to_dev(pf);
475 struct ice_hw *hw = &pf->hw;
476 enum ice_status status;
479 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
483 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
484 ice_aqc_opc_list_dev_caps, NULL);
486 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
487 status, hw->adminq.sq_last_status);
488 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
493 *data = (u8 *)devcaps;
498 static const struct devlink_region_ops ice_nvm_region_ops = {
501 .snapshot = ice_devlink_nvm_snapshot,
504 static const struct devlink_region_ops ice_devcaps_region_ops = {
505 .name = "device-caps",
507 .snapshot = ice_devlink_devcaps_snapshot,
511 * ice_devlink_init_regions - Initialize devlink regions
512 * @pf: the PF device structure
514 * Create devlink regions used to enable access to dump the contents of the
515 * flash memory on the device.
517 void ice_devlink_init_regions(struct ice_pf *pf)
519 struct devlink *devlink = priv_to_devlink(pf);
520 struct device *dev = ice_pf_to_dev(pf);
523 nvm_size = pf->hw.nvm.flash_size;
524 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
526 if (IS_ERR(pf->nvm_region)) {
527 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
528 PTR_ERR(pf->nvm_region));
529 pf->nvm_region = NULL;
532 pf->devcaps_region = devlink_region_create(devlink,
533 &ice_devcaps_region_ops, 10,
535 if (IS_ERR(pf->devcaps_region)) {
536 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
537 PTR_ERR(pf->devcaps_region));
538 pf->devcaps_region = NULL;
543 * ice_devlink_destroy_regions - Destroy devlink regions
544 * @pf: the PF device structure
546 * Remove previously created regions for this PF.
548 void ice_devlink_destroy_regions(struct ice_pf *pf)
551 devlink_region_destroy(pf->nvm_region);
552 if (pf->devcaps_region)
553 devlink_region_destroy(pf->devcaps_region);