1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
6 #include "ice_devlink.h"
8 static int ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len)
12 /* Copy the DSN into an array in Big Endian format */
13 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
15 snprintf(buf, len, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
16 dsn[0], dsn[1], dsn[2], dsn[3],
17 dsn[4], dsn[5], dsn[6], dsn[7]);
22 static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
24 struct ice_hw *hw = &pf->hw;
25 enum ice_status status;
27 status = ice_read_pba_string(hw, (u8 *)buf, len);
34 static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len)
36 struct ice_hw *hw = &pf->hw;
38 snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
44 static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len)
46 struct ice_hw *hw = &pf->hw;
48 snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver);
53 static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len)
55 struct ice_hw *hw = &pf->hw;
57 snprintf(buf, len, "0x%08x", hw->fw_build);
62 static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len)
64 struct ice_orom_info *orom = &pf->hw.nvm.orom;
66 snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch);
71 static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len)
73 struct ice_nvm_info *nvm = &pf->hw.nvm;
75 snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver);
80 static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len)
82 struct ice_nvm_info *nvm = &pf->hw.nvm;
84 snprintf(buf, len, "0x%08x", nvm->eetrack);
89 static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len)
91 struct ice_hw *hw = &pf->hw;
93 snprintf(buf, len, "%s", hw->active_pkg_name);
98 static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len)
100 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
102 snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
108 static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
110 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
112 /* The netlist version fields are BCD formatted */
113 snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
114 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
120 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
122 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
124 snprintf(buf, len, "0x%08x", netlist->hash);
129 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
130 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
132 enum ice_version_type {
138 static const struct ice_devlink_version {
139 enum ice_version_type type;
141 int (*getter)(struct ice_pf *pf, char *buf, size_t len);
142 } ice_devlink_versions[] = {
143 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
144 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
145 running("fw.mgmt.api", ice_info_fw_api),
146 running("fw.mgmt.build", ice_info_fw_build),
147 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
148 running("fw.psid.api", ice_info_nvm_ver),
149 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
150 running("fw.app.name", ice_info_ddp_pkg_name),
151 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
152 running("fw.netlist", ice_info_netlist_ver),
153 running("fw.netlist.build", ice_info_netlist_build),
157 * ice_devlink_info_get - .info_get devlink handler
158 * @devlink: devlink instance structure
159 * @req: the devlink info request
160 * @extack: extended netdev ack structure
162 * Callback for the devlink .info_get operation. Reports information about the
165 * Return: zero on success or an error code on failure.
167 static int ice_devlink_info_get(struct devlink *devlink,
168 struct devlink_info_req *req,
169 struct netlink_ext_ack *extack)
171 struct ice_pf *pf = devlink_priv(devlink);
176 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
178 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
182 err = ice_info_get_dsn(pf, buf, sizeof(buf));
184 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
188 err = devlink_info_serial_number_put(req, buf);
190 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
194 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
195 enum ice_version_type type = ice_devlink_versions[i].type;
196 const char *key = ice_devlink_versions[i].key;
198 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
200 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
205 case ICE_VERSION_FIXED:
206 err = devlink_info_version_fixed_put(req, key, buf);
208 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
212 case ICE_VERSION_RUNNING:
213 err = devlink_info_version_running_put(req, key, buf);
215 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
219 case ICE_VERSION_STORED:
220 err = devlink_info_version_stored_put(req, key, buf);
222 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
232 static const struct devlink_ops ice_devlink_ops = {
233 .info_get = ice_devlink_info_get,
236 static void ice_devlink_free(void *devlink_ptr)
238 devlink_free((struct devlink *)devlink_ptr);
242 * ice_allocate_pf - Allocate devlink and return PF structure pointer
243 * @dev: the device to allocate for
245 * Allocate a devlink instance for this device and return the private area as
246 * the PF structure. The devlink memory is kept track of through devres by
247 * adding an action to remove it when unwinding.
249 struct ice_pf *ice_allocate_pf(struct device *dev)
251 struct devlink *devlink;
253 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
257 /* Add an action to teardown the devlink when unwinding the driver */
258 if (devm_add_action(dev, ice_devlink_free, devlink)) {
259 devlink_free(devlink);
263 return devlink_priv(devlink);
267 * ice_devlink_register - Register devlink interface for this PF
268 * @pf: the PF to register the devlink for.
270 * Register the devlink instance associated with this physical function.
272 * Return: zero on success or an error code on failure.
274 int ice_devlink_register(struct ice_pf *pf)
276 struct devlink *devlink = priv_to_devlink(pf);
277 struct device *dev = ice_pf_to_dev(pf);
280 err = devlink_register(devlink, dev);
282 dev_err(dev, "devlink registration failed: %d\n", err);
290 * ice_devlink_unregister - Unregister devlink resources for this PF.
291 * @pf: the PF structure to cleanup
293 * Releases resources used by devlink and cleans up associated memory.
295 void ice_devlink_unregister(struct ice_pf *pf)
297 devlink_unregister(priv_to_devlink(pf));
301 * ice_devlink_create_port - Create a devlink port for this PF
302 * @pf: the PF to create a port for
304 * Create and register a devlink_port for this PF. Note that although each
305 * physical function is connected to a separate devlink instance, the port
306 * will still be numbered according to the physical function id.
308 * Return: zero on success or an error code on failure.
310 int ice_devlink_create_port(struct ice_pf *pf)
312 struct devlink *devlink = priv_to_devlink(pf);
313 struct ice_vsi *vsi = ice_get_main_vsi(pf);
314 struct device *dev = ice_pf_to_dev(pf);
318 dev_err(dev, "%s: unable to find main VSI\n", __func__);
322 devlink_port_attrs_set(&pf->devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
323 pf->hw.pf_id, false, 0, NULL, 0);
324 err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
326 dev_err(dev, "devlink_port_register failed: %d\n", err);
334 * ice_devlink_destroy_port - Destroy the devlink_port for this PF
335 * @pf: the PF to cleanup
337 * Unregisters the devlink_port structure associated with this PF.
339 void ice_devlink_destroy_port(struct ice_pf *pf)
341 devlink_port_type_clear(&pf->devlink_port);
342 devlink_port_unregister(&pf->devlink_port);
346 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
347 * @devlink: the devlink instance
348 * @extack: extended ACK response structure
349 * @data: on exit points to snapshot data buffer
351 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
352 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
353 * contents. This snapshot can later be viewed via the devlink-region
356 * @returns zero on success, and updates the data pointer. Returns a non-zero
357 * error code on failure.
359 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
360 struct netlink_ext_ack *extack, u8 **data)
362 struct ice_pf *pf = devlink_priv(devlink);
363 struct device *dev = ice_pf_to_dev(pf);
364 struct ice_hw *hw = &pf->hw;
365 enum ice_status status;
369 nvm_size = hw->nvm.flash_size;
370 nvm_data = vzalloc(nvm_size);
374 status = ice_acquire_nvm(hw, ICE_RES_READ);
376 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
377 status, hw->adminq.sq_last_status);
378 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
383 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
385 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
386 nvm_size, status, hw->adminq.sq_last_status);
387 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
400 static const struct devlink_region_ops ice_nvm_region_ops = {
403 .snapshot = ice_devlink_nvm_snapshot,
407 * ice_devlink_init_regions - Initialize devlink regions
408 * @pf: the PF device structure
410 * Create devlink regions used to enable access to dump the contents of the
411 * flash memory on the device.
413 void ice_devlink_init_regions(struct ice_pf *pf)
415 struct devlink *devlink = priv_to_devlink(pf);
416 struct device *dev = ice_pf_to_dev(pf);
419 nvm_size = pf->hw.nvm.flash_size;
420 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
422 if (IS_ERR(pf->nvm_region)) {
423 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
424 PTR_ERR(pf->nvm_region));
425 pf->nvm_region = NULL;
430 * ice_devlink_destroy_regions - Destroy devlink regions
431 * @pf: the PF device structure
433 * Remove previously created regions for this PF.
435 void ice_devlink_destroy_regions(struct ice_pf *pf)
438 devlink_region_destroy(pf->nvm_region);