gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / intel / ice / ice_devlink.c
blobc6833944b90a4aabecaefa29535b1f1ca604f17d
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_devlink.h"
8 static int ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len)
10 u8 dsn[8];
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]);
19 return 0;
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);
28 if (status)
29 return -EIO;
31 return 0;
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,
39 hw->fw_patch);
41 return 0;
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);
50 return 0;
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);
59 return 0;
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);
68 return 0;
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);
77 return 0;
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);
86 return 0;
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);
95 return 0;
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,
103 pkg->draft);
105 return 0;
108 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
109 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
111 enum ice_version_type {
112 ICE_VERSION_FIXED,
113 ICE_VERSION_RUNNING,
114 ICE_VERSION_STORED,
117 static const struct ice_devlink_version {
118 enum ice_version_type type;
119 const char *key;
120 int (*getter)(struct ice_pf *pf, char *buf, size_t len);
121 } ice_devlink_versions[] = {
122 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
123 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
124 running("fw.mgmt.api", ice_info_fw_api),
125 running("fw.mgmt.build", ice_info_fw_build),
126 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
127 running("fw.psid.api", ice_info_nvm_ver),
128 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
129 running("fw.app.name", ice_info_ddp_pkg_name),
130 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
134 * ice_devlink_info_get - .info_get devlink handler
135 * @devlink: devlink instance structure
136 * @req: the devlink info request
137 * @extack: extended netdev ack structure
139 * Callback for the devlink .info_get operation. Reports information about the
140 * device.
142 * Return: zero on success or an error code on failure.
144 static int ice_devlink_info_get(struct devlink *devlink,
145 struct devlink_info_req *req,
146 struct netlink_ext_ack *extack)
148 struct ice_pf *pf = devlink_priv(devlink);
149 char buf[100];
150 size_t i;
151 int err;
153 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
154 if (err) {
155 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
156 return err;
159 err = ice_info_get_dsn(pf, buf, sizeof(buf));
160 if (err) {
161 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
162 return err;
165 err = devlink_info_serial_number_put(req, buf);
166 if (err) {
167 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
168 return err;
171 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
172 enum ice_version_type type = ice_devlink_versions[i].type;
173 const char *key = ice_devlink_versions[i].key;
175 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
176 if (err) {
177 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
178 return err;
181 switch (type) {
182 case ICE_VERSION_FIXED:
183 err = devlink_info_version_fixed_put(req, key, buf);
184 if (err) {
185 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
186 return err;
188 break;
189 case ICE_VERSION_RUNNING:
190 err = devlink_info_version_running_put(req, key, buf);
191 if (err) {
192 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
193 return err;
195 break;
196 case ICE_VERSION_STORED:
197 err = devlink_info_version_stored_put(req, key, buf);
198 if (err) {
199 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
200 return err;
202 break;
206 return 0;
209 static const struct devlink_ops ice_devlink_ops = {
210 .info_get = ice_devlink_info_get,
213 static void ice_devlink_free(void *devlink_ptr)
215 devlink_free((struct devlink *)devlink_ptr);
219 * ice_allocate_pf - Allocate devlink and return PF structure pointer
220 * @dev: the device to allocate for
222 * Allocate a devlink instance for this device and return the private area as
223 * the PF structure. The devlink memory is kept track of through devres by
224 * adding an action to remove it when unwinding.
226 struct ice_pf *ice_allocate_pf(struct device *dev)
228 struct devlink *devlink;
230 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
231 if (!devlink)
232 return NULL;
234 /* Add an action to teardown the devlink when unwinding the driver */
235 if (devm_add_action(dev, ice_devlink_free, devlink)) {
236 devlink_free(devlink);
237 return NULL;
240 return devlink_priv(devlink);
244 * ice_devlink_register - Register devlink interface for this PF
245 * @pf: the PF to register the devlink for.
247 * Register the devlink instance associated with this physical function.
249 * Return: zero on success or an error code on failure.
251 int ice_devlink_register(struct ice_pf *pf)
253 struct devlink *devlink = priv_to_devlink(pf);
254 struct device *dev = ice_pf_to_dev(pf);
255 int err;
257 err = devlink_register(devlink, dev);
258 if (err) {
259 dev_err(dev, "devlink registration failed: %d\n", err);
260 return err;
263 return 0;
267 * ice_devlink_unregister - Unregister devlink resources for this PF.
268 * @pf: the PF structure to cleanup
270 * Releases resources used by devlink and cleans up associated memory.
272 void ice_devlink_unregister(struct ice_pf *pf)
274 devlink_unregister(priv_to_devlink(pf));
278 * ice_devlink_create_port - Create a devlink port for this PF
279 * @pf: the PF to create a port for
281 * Create and register a devlink_port for this PF. Note that although each
282 * physical function is connected to a separate devlink instance, the port
283 * will still be numbered according to the physical function id.
285 * Return: zero on success or an error code on failure.
287 int ice_devlink_create_port(struct ice_pf *pf)
289 struct devlink *devlink = priv_to_devlink(pf);
290 struct ice_vsi *vsi = ice_get_main_vsi(pf);
291 struct device *dev = ice_pf_to_dev(pf);
292 int err;
294 if (!vsi) {
295 dev_err(dev, "%s: unable to find main VSI\n", __func__);
296 return -EIO;
299 devlink_port_attrs_set(&pf->devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
300 pf->hw.pf_id, false, 0, NULL, 0);
301 err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
302 if (err) {
303 dev_err(dev, "devlink_port_register failed: %d\n", err);
304 return err;
307 return 0;
311 * ice_devlink_destroy_port - Destroy the devlink_port for this PF
312 * @pf: the PF to cleanup
314 * Unregisters the devlink_port structure associated with this PF.
316 void ice_devlink_destroy_port(struct ice_pf *pf)
318 devlink_port_type_clear(&pf->devlink_port);
319 devlink_port_unregister(&pf->devlink_port);
323 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
324 * @devlink: the devlink instance
325 * @extack: extended ACK response structure
326 * @data: on exit points to snapshot data buffer
328 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
329 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
330 * contents. This snapshot can later be viewed via the devlink-region
331 * interface.
333 * @returns zero on success, and updates the data pointer. Returns a non-zero
334 * error code on failure.
336 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
337 struct netlink_ext_ack *extack, u8 **data)
339 struct ice_pf *pf = devlink_priv(devlink);
340 struct device *dev = ice_pf_to_dev(pf);
341 struct ice_hw *hw = &pf->hw;
342 enum ice_status status;
343 void *nvm_data;
344 u32 nvm_size;
346 nvm_size = hw->nvm.flash_size;
347 nvm_data = vzalloc(nvm_size);
348 if (!nvm_data)
349 return -ENOMEM;
351 status = ice_acquire_nvm(hw, ICE_RES_READ);
352 if (status) {
353 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
354 status, hw->adminq.sq_last_status);
355 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
356 vfree(nvm_data);
357 return -EIO;
360 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
361 if (status) {
362 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
363 nvm_size, status, hw->adminq.sq_last_status);
364 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
365 ice_release_nvm(hw);
366 vfree(nvm_data);
367 return -EIO;
370 ice_release_nvm(hw);
372 *data = nvm_data;
374 return 0;
377 static const struct devlink_region_ops ice_nvm_region_ops = {
378 .name = "nvm-flash",
379 .destructor = vfree,
380 .snapshot = ice_devlink_nvm_snapshot,
384 * ice_devlink_init_regions - Initialize devlink regions
385 * @pf: the PF device structure
387 * Create devlink regions used to enable access to dump the contents of the
388 * flash memory on the device.
390 void ice_devlink_init_regions(struct ice_pf *pf)
392 struct devlink *devlink = priv_to_devlink(pf);
393 struct device *dev = ice_pf_to_dev(pf);
394 u64 nvm_size;
396 nvm_size = pf->hw.nvm.flash_size;
397 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
398 nvm_size);
399 if (IS_ERR(pf->nvm_region)) {
400 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
401 PTR_ERR(pf->nvm_region));
402 pf->nvm_region = NULL;
407 * ice_devlink_destroy_regions - Destroy devlink regions
408 * @pf: the PF device structure
410 * Remove previously created regions for this PF.
412 void ice_devlink_destroy_regions(struct ice_pf *pf)
414 if (pf->nvm_region)
415 devlink_region_destroy(pf->nvm_region);