1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_devlink.h"
7 #include "ice_fw_update.h"
8
9 /* context for devlink info version reporting */
10 struct ice_info_ctx {
11 char buf[128];
12 struct ice_orom_info pending_orom;
13 struct ice_nvm_info pending_nvm;
14 struct ice_netlist_info pending_netlist;
15 struct ice_hw_dev_caps dev_caps;
16 };
17
18 /* The following functions are used to format specific strings for various
19 * devlink info versions. The ctx parameter is used to provide the storage
20 * buffer, as well as any ancillary information calculated when the info
21 * request was made.
22 *
23 * If a version does not exist, for example when attempting to get the
24 * inactive version of flash when there is no pending update, the function
25 * should leave the buffer in the ctx structure empty and return 0.
26 */
27
ice_info_get_dsn(struct ice_pf * pf,struct ice_info_ctx * ctx)28 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
29 {
30 u8 dsn[8];
31
32 /* Copy the DSN into an array in Big Endian format */
33 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
34
35 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
36 }
37
ice_info_pba(struct ice_pf * pf,struct ice_info_ctx * ctx)38 static int ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
39 {
40 struct ice_hw *hw = &pf->hw;
41 enum ice_status status;
42
43 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
44 if (status)
45 /* We failed to locate the PBA, so just skip this entry */
46 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %s\n",
47 ice_stat_str(status));
48
49 return 0;
50 }
51
ice_info_fw_mgmt(struct ice_pf * pf,struct ice_info_ctx * ctx)52 static int ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
53 {
54 struct ice_hw *hw = &pf->hw;
55
56 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
57 hw->fw_patch);
58
59 return 0;
60 }
61
ice_info_fw_api(struct ice_pf * pf,struct ice_info_ctx * ctx)62 static int ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
63 {
64 struct ice_hw *hw = &pf->hw;
65
66 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
67 hw->api_min_ver, hw->api_patch);
68
69 return 0;
70 }
71
ice_info_fw_build(struct ice_pf * pf,struct ice_info_ctx * ctx)72 static int ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
73 {
74 struct ice_hw *hw = &pf->hw;
75
76 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
77
78 return 0;
79 }
80
ice_info_orom_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)81 static int ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
82 {
83 struct ice_orom_info *orom = &pf->hw.flash.orom;
84
85 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", orom->major, orom->build, orom->patch);
86
87 return 0;
88 }
89
90 static int
ice_info_pending_orom_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)91 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
92 {
93 struct ice_orom_info *orom = &ctx->pending_orom;
94
95 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
96 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
97 orom->major, orom->build, orom->patch);
98
99 return 0;
100 }
101
ice_info_nvm_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)102 static int ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
103 {
104 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
105
106 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
107
108 return 0;
109 }
110
111 static int
ice_info_pending_nvm_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)112 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
113 {
114 struct ice_nvm_info *nvm = &ctx->pending_nvm;
115
116 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
117 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
118
119 return 0;
120 }
121
ice_info_eetrack(struct ice_pf * pf,struct ice_info_ctx * ctx)122 static int ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
123 {
124 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
125
126 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
127
128 return 0;
129 }
130
131 static int
ice_info_pending_eetrack(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)132 ice_info_pending_eetrack(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
133 {
134 struct ice_nvm_info *nvm = &ctx->pending_nvm;
135
136 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
137 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
138
139 return 0;
140 }
141
ice_info_ddp_pkg_name(struct ice_pf * pf,struct ice_info_ctx * ctx)142 static int ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
143 {
144 struct ice_hw *hw = &pf->hw;
145
146 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
147
148 return 0;
149 }
150
ice_info_ddp_pkg_version(struct ice_pf * pf,struct ice_info_ctx * ctx)151 static int ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
152 {
153 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
154
155 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
156 pkg->draft);
157
158 return 0;
159 }
160
ice_info_ddp_pkg_bundle_id(struct ice_pf * pf,struct ice_info_ctx * ctx)161 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
162 {
163 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
164
165 return 0;
166 }
167
ice_info_netlist_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)168 static int ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
169 {
170 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
171
172 /* The netlist version fields are BCD formatted */
173 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
174 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
175 netlist->cust_ver);
176
177 return 0;
178 }
179
ice_info_netlist_build(struct ice_pf * pf,struct ice_info_ctx * ctx)180 static int ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
181 {
182 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
183
184 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
185
186 return 0;
187 }
188
189 static int
ice_info_pending_netlist_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)190 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
191 {
192 struct ice_netlist_info *netlist = &ctx->pending_netlist;
193
194 /* The netlist version fields are BCD formatted */
195 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
196 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
197 netlist->major, netlist->minor,
198 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
199 netlist->cust_ver);
200
201 return 0;
202 }
203
204 static int
ice_info_pending_netlist_build(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)205 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
206 {
207 struct ice_netlist_info *netlist = &ctx->pending_netlist;
208
209 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
210 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
211
212 return 0;
213 }
214
215 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
216 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
217 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
218
219 /* The combined() macro inserts both the running entry as well as a stored
220 * entry. The running entry will always report the version from the active
221 * handler. The stored entry will first try the pending handler, and fallback
222 * to the active handler if the pending function does not report a version.
223 * The pending handler should check the status of a pending update for the
224 * relevant flash component. It should only fill in the buffer in the case
225 * where a valid pending version is available. This ensures that the related
226 * stored and running versions remain in sync, and that stored versions are
227 * correctly reported as expected.
228 */
229 #define combined(key, active, pending) \
230 running(key, active), \
231 stored(key, pending, active)
232
233 enum ice_version_type {
234 ICE_VERSION_FIXED,
235 ICE_VERSION_RUNNING,
236 ICE_VERSION_STORED,
237 };
238
239 static const struct ice_devlink_version {
240 enum ice_version_type type;
241 const char *key;
242 int (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
243 int (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
244 } ice_devlink_versions[] = {
245 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
246 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
247 running("fw.mgmt.api", ice_info_fw_api),
248 running("fw.mgmt.build", ice_info_fw_build),
249 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
250 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
251 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
252 running("fw.app.name", ice_info_ddp_pkg_name),
253 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
254 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
255 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
256 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
257 };
258
259 /**
260 * ice_devlink_info_get - .info_get devlink handler
261 * @devlink: devlink instance structure
262 * @req: the devlink info request
263 * @extack: extended netdev ack structure
264 *
265 * Callback for the devlink .info_get operation. Reports information about the
266 * device.
267 *
268 * Return: zero on success or an error code on failure.
269 */
ice_devlink_info_get(struct devlink * devlink,struct devlink_info_req * req,struct netlink_ext_ack * extack)270 static int ice_devlink_info_get(struct devlink *devlink,
271 struct devlink_info_req *req,
272 struct netlink_ext_ack *extack)
273 {
274 struct ice_pf *pf = devlink_priv(devlink);
275 struct device *dev = ice_pf_to_dev(pf);
276 struct ice_hw *hw = &pf->hw;
277 struct ice_info_ctx *ctx;
278 enum ice_status status;
279 size_t i;
280 int err;
281
282 err = ice_wait_for_reset(pf, 10 * HZ);
283 if (err) {
284 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
285 return err;
286 }
287
288 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
289 if (!ctx)
290 return -ENOMEM;
291
292 /* discover capabilities first */
293 status = ice_discover_dev_caps(hw, &ctx->dev_caps);
294 if (status) {
295 dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
296 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
297 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
298 err = -EIO;
299 goto out_free_ctx;
300 }
301
302 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
303 status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
304 if (status) {
305 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
306 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
307
308 /* disable display of pending Option ROM */
309 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
310 }
311 }
312
313 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
314 status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
315 if (status) {
316 dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
317 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
318
319 /* disable display of pending Option ROM */
320 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
321 }
322 }
323
324 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
325 status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
326 if (status) {
327 dev_dbg(dev, "Unable to read inactive Netlist version data, status %s aq_err %s\n",
328 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
329
330 /* disable display of pending Option ROM */
331 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
332 }
333 }
334
335 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
336 if (err) {
337 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
338 goto out_free_ctx;
339 }
340
341 ice_info_get_dsn(pf, ctx);
342
343 err = devlink_info_serial_number_put(req, ctx->buf);
344 if (err) {
345 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
346 goto out_free_ctx;
347 }
348
349 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
350 enum ice_version_type type = ice_devlink_versions[i].type;
351 const char *key = ice_devlink_versions[i].key;
352
353 memset(ctx->buf, 0, sizeof(ctx->buf));
354
355 err = ice_devlink_versions[i].getter(pf, ctx);
356 if (err) {
357 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
358 goto out_free_ctx;
359 }
360
361 /* If the default getter doesn't report a version, use the
362 * fallback function. This is primarily useful in the case of
363 * "stored" versions that want to report the same value as the
364 * running version in the normal case of no pending update.
365 */
366 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback) {
367 err = ice_devlink_versions[i].fallback(pf, ctx);
368 if (err) {
369 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
370 goto out_free_ctx;
371 }
372 }
373
374 /* Do not report missing versions */
375 if (ctx->buf[0] == '\0')
376 continue;
377
378 switch (type) {
379 case ICE_VERSION_FIXED:
380 err = devlink_info_version_fixed_put(req, key, ctx->buf);
381 if (err) {
382 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
383 goto out_free_ctx;
384 }
385 break;
386 case ICE_VERSION_RUNNING:
387 err = devlink_info_version_running_put(req, key, ctx->buf);
388 if (err) {
389 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
390 goto out_free_ctx;
391 }
392 break;
393 case ICE_VERSION_STORED:
394 err = devlink_info_version_stored_put(req, key, ctx->buf);
395 if (err) {
396 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
397 goto out_free_ctx;
398 }
399 break;
400 }
401 }
402
403 out_free_ctx:
404 kfree(ctx);
405 return err;
406 }
407
408 /**
409 * ice_devlink_flash_update - Update firmware stored in flash on the device
410 * @devlink: pointer to devlink associated with device to update
411 * @params: flash update parameters
412 * @extack: netlink extended ACK structure
413 *
414 * Perform a device flash update. The bulk of the update logic is contained
415 * within the ice_flash_pldm_image function.
416 *
417 * Returns: zero on success, or an error code on failure.
418 */
419 static int
ice_devlink_flash_update(struct devlink * devlink,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)420 ice_devlink_flash_update(struct devlink *devlink,
421 struct devlink_flash_update_params *params,
422 struct netlink_ext_ack *extack)
423 {
424 struct ice_pf *pf = devlink_priv(devlink);
425 struct ice_hw *hw = &pf->hw;
426 u8 preservation;
427 int err;
428
429 if (!params->overwrite_mask) {
430 /* preserve all settings and identifiers */
431 preservation = ICE_AQC_NVM_PRESERVE_ALL;
432 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
433 /* overwrite settings, but preserve the vital device identifiers */
434 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
435 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
436 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
437 /* overwrite both settings and identifiers, preserve nothing */
438 preservation = ICE_AQC_NVM_NO_PRESERVATION;
439 } else {
440 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
441 return -EOPNOTSUPP;
442 }
443
444 if (!hw->dev_caps.common_cap.nvm_unified_update) {
445 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
446 return -EOPNOTSUPP;
447 }
448
449 err = ice_check_for_pending_update(pf, NULL, extack);
450 if (err)
451 return err;
452
453 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
454
455 return ice_flash_pldm_image(pf, params->fw, preservation, extack);
456 }
457
458 static const struct devlink_ops ice_devlink_ops = {
459 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
460 .info_get = ice_devlink_info_get,
461 .flash_update = ice_devlink_flash_update,
462 };
463
ice_devlink_free(void * devlink_ptr)464 static void ice_devlink_free(void *devlink_ptr)
465 {
466 devlink_free((struct devlink *)devlink_ptr);
467 }
468
469 /**
470 * ice_allocate_pf - Allocate devlink and return PF structure pointer
471 * @dev: the device to allocate for
472 *
473 * Allocate a devlink instance for this device and return the private area as
474 * the PF structure. The devlink memory is kept track of through devres by
475 * adding an action to remove it when unwinding.
476 */
ice_allocate_pf(struct device * dev)477 struct ice_pf *ice_allocate_pf(struct device *dev)
478 {
479 struct devlink *devlink;
480
481 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
482 if (!devlink)
483 return NULL;
484
485 /* Add an action to teardown the devlink when unwinding the driver */
486 if (devm_add_action(dev, ice_devlink_free, devlink)) {
487 devlink_free(devlink);
488 return NULL;
489 }
490
491 return devlink_priv(devlink);
492 }
493
494 /**
495 * ice_devlink_register - Register devlink interface for this PF
496 * @pf: the PF to register the devlink for.
497 *
498 * Register the devlink instance associated with this physical function.
499 *
500 * Return: zero on success or an error code on failure.
501 */
ice_devlink_register(struct ice_pf * pf)502 int ice_devlink_register(struct ice_pf *pf)
503 {
504 struct devlink *devlink = priv_to_devlink(pf);
505 struct device *dev = ice_pf_to_dev(pf);
506 int err;
507
508 err = devlink_register(devlink);
509 if (err) {
510 dev_err(dev, "devlink registration failed: %d\n", err);
511 return err;
512 }
513
514 return 0;
515 }
516
517 /**
518 * ice_devlink_unregister - Unregister devlink resources for this PF.
519 * @pf: the PF structure to cleanup
520 *
521 * Releases resources used by devlink and cleans up associated memory.
522 */
ice_devlink_unregister(struct ice_pf * pf)523 void ice_devlink_unregister(struct ice_pf *pf)
524 {
525 devlink_unregister(priv_to_devlink(pf));
526 }
527
528 /**
529 * ice_devlink_create_pf_port - Create a devlink port for this PF
530 * @pf: the PF to create a devlink port for
531 *
532 * Create and register a devlink_port for this PF.
533 *
534 * Return: zero on success or an error code on failure.
535 */
ice_devlink_create_pf_port(struct ice_pf * pf)536 int ice_devlink_create_pf_port(struct ice_pf *pf)
537 {
538 struct devlink_port_attrs attrs = {};
539 struct devlink_port *devlink_port;
540 struct devlink *devlink;
541 struct ice_vsi *vsi;
542 struct device *dev;
543 int err;
544
545 dev = ice_pf_to_dev(pf);
546
547 devlink_port = &pf->devlink_port;
548
549 vsi = ice_get_main_vsi(pf);
550 if (!vsi)
551 return -EIO;
552
553 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
554 attrs.phys.port_number = pf->hw.bus.func;
555 devlink_port_attrs_set(devlink_port, &attrs);
556 devlink = priv_to_devlink(pf);
557
558 err = devlink_port_register(devlink, devlink_port, vsi->idx);
559 if (err) {
560 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
561 pf->hw.pf_id, err);
562 return err;
563 }
564
565 return 0;
566 }
567
568 /**
569 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
570 * @pf: the PF to cleanup
571 *
572 * Unregisters the devlink_port structure associated with this PF.
573 */
ice_devlink_destroy_pf_port(struct ice_pf * pf)574 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
575 {
576 struct devlink_port *devlink_port;
577
578 devlink_port = &pf->devlink_port;
579
580 devlink_port_type_clear(devlink_port);
581 devlink_port_unregister(devlink_port);
582 }
583
584 /**
585 * ice_devlink_create_vf_port - Create a devlink port for this VF
586 * @vf: the VF to create a port for
587 *
588 * Create and register a devlink_port for this VF.
589 *
590 * Return: zero on success or an error code on failure.
591 */
ice_devlink_create_vf_port(struct ice_vf * vf)592 int ice_devlink_create_vf_port(struct ice_vf *vf)
593 {
594 struct devlink_port_attrs attrs = {};
595 struct devlink_port *devlink_port;
596 struct devlink *devlink;
597 struct ice_vsi *vsi;
598 struct device *dev;
599 struct ice_pf *pf;
600 int err;
601
602 pf = vf->pf;
603 dev = ice_pf_to_dev(pf);
604 vsi = ice_get_vf_vsi(vf);
605 devlink_port = &vf->devlink_port;
606
607 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
608 attrs.pci_vf.pf = pf->hw.bus.func;
609 attrs.pci_vf.vf = vf->vf_id;
610
611 devlink_port_attrs_set(devlink_port, &attrs);
612 devlink = priv_to_devlink(pf);
613
614 err = devlink_port_register(devlink, devlink_port, vsi->idx);
615 if (err) {
616 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
617 vf->vf_id, err);
618 return err;
619 }
620
621 return 0;
622 }
623
624 /**
625 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
626 * @vf: the VF to cleanup
627 *
628 * Unregisters the devlink_port structure associated with this VF.
629 */
ice_devlink_destroy_vf_port(struct ice_vf * vf)630 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
631 {
632 struct devlink_port *devlink_port;
633
634 devlink_port = &vf->devlink_port;
635
636 devlink_port_type_clear(devlink_port);
637 devlink_port_unregister(devlink_port);
638 }
639
640 /**
641 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
642 * @devlink: the devlink instance
643 * @ops: the devlink region being snapshotted
644 * @extack: extended ACK response structure
645 * @data: on exit points to snapshot data buffer
646 *
647 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
648 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
649 * contents. This snapshot can later be viewed via the devlink-region
650 * interface.
651 *
652 * @returns zero on success, and updates the data pointer. Returns a non-zero
653 * error code on failure.
654 */
ice_devlink_nvm_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)655 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
656 const struct devlink_region_ops *ops,
657 struct netlink_ext_ack *extack, u8 **data)
658 {
659 struct ice_pf *pf = devlink_priv(devlink);
660 struct device *dev = ice_pf_to_dev(pf);
661 struct ice_hw *hw = &pf->hw;
662 enum ice_status status;
663 void *nvm_data;
664 u32 nvm_size;
665
666 nvm_size = hw->flash.flash_size;
667 nvm_data = vzalloc(nvm_size);
668 if (!nvm_data)
669 return -ENOMEM;
670
671 status = ice_acquire_nvm(hw, ICE_RES_READ);
672 if (status) {
673 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
674 status, hw->adminq.sq_last_status);
675 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
676 vfree(nvm_data);
677 return -EIO;
678 }
679
680 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
681 if (status) {
682 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
683 nvm_size, status, hw->adminq.sq_last_status);
684 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
685 ice_release_nvm(hw);
686 vfree(nvm_data);
687 return -EIO;
688 }
689
690 ice_release_nvm(hw);
691
692 *data = nvm_data;
693
694 return 0;
695 }
696
697 /**
698 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
699 * @devlink: the devlink instance
700 * @ops: the devlink region being snapshotted
701 * @extack: extended ACK response structure
702 * @data: on exit points to snapshot data buffer
703 *
704 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
705 * the device-caps devlink region. It captures a snapshot of the device
706 * capabilities reported by firmware.
707 *
708 * @returns zero on success, and updates the data pointer. Returns a non-zero
709 * error code on failure.
710 */
711 static int
ice_devlink_devcaps_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)712 ice_devlink_devcaps_snapshot(struct devlink *devlink,
713 const struct devlink_region_ops *ops,
714 struct netlink_ext_ack *extack, u8 **data)
715 {
716 struct ice_pf *pf = devlink_priv(devlink);
717 struct device *dev = ice_pf_to_dev(pf);
718 struct ice_hw *hw = &pf->hw;
719 enum ice_status status;
720 void *devcaps;
721
722 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
723 if (!devcaps)
724 return -ENOMEM;
725
726 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
727 ice_aqc_opc_list_dev_caps, NULL);
728 if (status) {
729 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
730 status, hw->adminq.sq_last_status);
731 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
732 vfree(devcaps);
733 return -EIO;
734 }
735
736 *data = (u8 *)devcaps;
737
738 return 0;
739 }
740
741 static const struct devlink_region_ops ice_nvm_region_ops = {
742 .name = "nvm-flash",
743 .destructor = vfree,
744 .snapshot = ice_devlink_nvm_snapshot,
745 };
746
747 static const struct devlink_region_ops ice_devcaps_region_ops = {
748 .name = "device-caps",
749 .destructor = vfree,
750 .snapshot = ice_devlink_devcaps_snapshot,
751 };
752
753 /**
754 * ice_devlink_init_regions - Initialize devlink regions
755 * @pf: the PF device structure
756 *
757 * Create devlink regions used to enable access to dump the contents of the
758 * flash memory on the device.
759 */
ice_devlink_init_regions(struct ice_pf * pf)760 void ice_devlink_init_regions(struct ice_pf *pf)
761 {
762 struct devlink *devlink = priv_to_devlink(pf);
763 struct device *dev = ice_pf_to_dev(pf);
764 u64 nvm_size;
765
766 nvm_size = pf->hw.flash.flash_size;
767 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
768 nvm_size);
769 if (IS_ERR(pf->nvm_region)) {
770 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
771 PTR_ERR(pf->nvm_region));
772 pf->nvm_region = NULL;
773 }
774
775 pf->devcaps_region = devlink_region_create(devlink,
776 &ice_devcaps_region_ops, 10,
777 ICE_AQ_MAX_BUF_LEN);
778 if (IS_ERR(pf->devcaps_region)) {
779 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
780 PTR_ERR(pf->devcaps_region));
781 pf->devcaps_region = NULL;
782 }
783 }
784
785 /**
786 * ice_devlink_destroy_regions - Destroy devlink regions
787 * @pf: the PF device structure
788 *
789 * Remove previously created regions for this PF.
790 */
ice_devlink_destroy_regions(struct ice_pf * pf)791 void ice_devlink_destroy_regions(struct ice_pf *pf)
792 {
793 if (pf->nvm_region)
794 devlink_region_destroy(pf->nvm_region);
795 if (pf->devcaps_region)
796 devlink_region_destroy(pf->devcaps_region);
797 }
798