1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
3
4 #include <linux/vmalloc.h>
5
6 #include "ice.h"
7 #include "ice_lib.h"
8 #include "ice_devlink.h"
9 #include "ice_eswitch.h"
10 #include "ice_fw_update.h"
11
12 static int ice_active_port_option = -1;
13
14 /* context for devlink info version reporting */
15 struct ice_info_ctx {
16 char buf[128];
17 struct ice_orom_info pending_orom;
18 struct ice_nvm_info pending_nvm;
19 struct ice_netlist_info pending_netlist;
20 struct ice_hw_dev_caps dev_caps;
21 };
22
23 /* The following functions are used to format specific strings for various
24 * devlink info versions. The ctx parameter is used to provide the storage
25 * buffer, as well as any ancillary information calculated when the info
26 * request was made.
27 *
28 * If a version does not exist, for example when attempting to get the
29 * inactive version of flash when there is no pending update, the function
30 * should leave the buffer in the ctx structure empty.
31 */
32
ice_info_get_dsn(struct ice_pf * pf,struct ice_info_ctx * ctx)33 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
34 {
35 u8 dsn[8];
36
37 /* Copy the DSN into an array in Big Endian format */
38 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
39
40 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
41 }
42
ice_info_pba(struct ice_pf * pf,struct ice_info_ctx * ctx)43 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
44 {
45 struct ice_hw *hw = &pf->hw;
46 int status;
47
48 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
49 if (status)
50 /* We failed to locate the PBA, so just skip this entry */
51 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
52 status);
53 }
54
ice_info_fw_mgmt(struct ice_pf * pf,struct ice_info_ctx * ctx)55 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
56 {
57 struct ice_hw *hw = &pf->hw;
58
59 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
60 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
61 }
62
ice_info_fw_api(struct ice_pf * pf,struct ice_info_ctx * ctx)63 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
64 {
65 struct ice_hw *hw = &pf->hw;
66
67 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
68 hw->api_min_ver, hw->api_patch);
69 }
70
ice_info_fw_build(struct ice_pf * pf,struct ice_info_ctx * ctx)71 static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
72 {
73 struct ice_hw *hw = &pf->hw;
74
75 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
76 }
77
ice_info_orom_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)78 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
79 {
80 struct ice_orom_info *orom = &pf->hw.flash.orom;
81
82 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
83 orom->major, orom->build, orom->patch);
84 }
85
86 static void
ice_info_pending_orom_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)87 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
88 struct ice_info_ctx *ctx)
89 {
90 struct ice_orom_info *orom = &ctx->pending_orom;
91
92 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
93 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
94 orom->major, orom->build, orom->patch);
95 }
96
ice_info_nvm_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)97 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
98 {
99 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
100
101 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
102 }
103
104 static void
ice_info_pending_nvm_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)105 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
106 struct ice_info_ctx *ctx)
107 {
108 struct ice_nvm_info *nvm = &ctx->pending_nvm;
109
110 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
111 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
112 nvm->major, nvm->minor);
113 }
114
ice_info_eetrack(struct ice_pf * pf,struct ice_info_ctx * ctx)115 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
116 {
117 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
118
119 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
120 }
121
122 static void
ice_info_pending_eetrack(struct ice_pf * pf,struct ice_info_ctx * ctx)123 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
124 {
125 struct ice_nvm_info *nvm = &ctx->pending_nvm;
126
127 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
128 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
129 }
130
ice_info_ddp_pkg_name(struct ice_pf * pf,struct ice_info_ctx * ctx)131 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
132 {
133 struct ice_hw *hw = &pf->hw;
134
135 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
136 }
137
138 static void
ice_info_ddp_pkg_version(struct ice_pf * pf,struct ice_info_ctx * ctx)139 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
140 {
141 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
142
143 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
144 pkg->major, pkg->minor, pkg->update, pkg->draft);
145 }
146
147 static void
ice_info_ddp_pkg_bundle_id(struct ice_pf * pf,struct ice_info_ctx * ctx)148 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
149 {
150 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
151 }
152
ice_info_netlist_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)153 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
154 {
155 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
156
157 /* The netlist version fields are BCD formatted */
158 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
159 netlist->major, netlist->minor,
160 netlist->type >> 16, netlist->type & 0xFFFF,
161 netlist->rev, netlist->cust_ver);
162 }
163
ice_info_netlist_build(struct ice_pf * pf,struct ice_info_ctx * ctx)164 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
165 {
166 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
167
168 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
169 }
170
171 static void
ice_info_pending_netlist_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)172 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
173 struct ice_info_ctx *ctx)
174 {
175 struct ice_netlist_info *netlist = &ctx->pending_netlist;
176
177 /* The netlist version fields are BCD formatted */
178 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
179 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
180 netlist->major, netlist->minor,
181 netlist->type >> 16, netlist->type & 0xFFFF,
182 netlist->rev, netlist->cust_ver);
183 }
184
185 static void
ice_info_pending_netlist_build(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)186 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
187 struct ice_info_ctx *ctx)
188 {
189 struct ice_netlist_info *netlist = &ctx->pending_netlist;
190
191 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
192 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
193 }
194
195 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
196 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
197 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
198
199 /* The combined() macro inserts both the running entry as well as a stored
200 * entry. The running entry will always report the version from the active
201 * handler. The stored entry will first try the pending handler, and fallback
202 * to the active handler if the pending function does not report a version.
203 * The pending handler should check the status of a pending update for the
204 * relevant flash component. It should only fill in the buffer in the case
205 * where a valid pending version is available. This ensures that the related
206 * stored and running versions remain in sync, and that stored versions are
207 * correctly reported as expected.
208 */
209 #define combined(key, active, pending) \
210 running(key, active), \
211 stored(key, pending, active)
212
213 enum ice_version_type {
214 ICE_VERSION_FIXED,
215 ICE_VERSION_RUNNING,
216 ICE_VERSION_STORED,
217 };
218
219 static const struct ice_devlink_version {
220 enum ice_version_type type;
221 const char *key;
222 void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
223 void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
224 } ice_devlink_versions[] = {
225 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
226 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
227 running("fw.mgmt.api", ice_info_fw_api),
228 running("fw.mgmt.build", ice_info_fw_build),
229 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
230 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
231 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
232 running("fw.app.name", ice_info_ddp_pkg_name),
233 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
234 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
235 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
236 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
237 };
238
239 /**
240 * ice_devlink_info_get - .info_get devlink handler
241 * @devlink: devlink instance structure
242 * @req: the devlink info request
243 * @extack: extended netdev ack structure
244 *
245 * Callback for the devlink .info_get operation. Reports information about the
246 * device.
247 *
248 * Return: zero on success or an error code on failure.
249 */
ice_devlink_info_get(struct devlink * devlink,struct devlink_info_req * req,struct netlink_ext_ack * extack)250 static int ice_devlink_info_get(struct devlink *devlink,
251 struct devlink_info_req *req,
252 struct netlink_ext_ack *extack)
253 {
254 struct ice_pf *pf = devlink_priv(devlink);
255 struct device *dev = ice_pf_to_dev(pf);
256 struct ice_hw *hw = &pf->hw;
257 struct ice_info_ctx *ctx;
258 size_t i;
259 int err;
260
261 err = ice_wait_for_reset(pf, 10 * HZ);
262 if (err) {
263 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
264 return err;
265 }
266
267 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
268 if (!ctx)
269 return -ENOMEM;
270
271 /* discover capabilities first */
272 err = ice_discover_dev_caps(hw, &ctx->dev_caps);
273 if (err) {
274 dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
275 err, ice_aq_str(hw->adminq.sq_last_status));
276 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
277 goto out_free_ctx;
278 }
279
280 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
281 err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
282 if (err) {
283 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
284 err, ice_aq_str(hw->adminq.sq_last_status));
285
286 /* disable display of pending Option ROM */
287 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
288 }
289 }
290
291 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
292 err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
293 if (err) {
294 dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
295 err, ice_aq_str(hw->adminq.sq_last_status));
296
297 /* disable display of pending Option ROM */
298 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
299 }
300 }
301
302 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
303 err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
304 if (err) {
305 dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
306 err, 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_netlist = false;
310 }
311 }
312
313 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
314 if (err) {
315 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
316 goto out_free_ctx;
317 }
318
319 ice_info_get_dsn(pf, ctx);
320
321 err = devlink_info_serial_number_put(req, ctx->buf);
322 if (err) {
323 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
324 goto out_free_ctx;
325 }
326
327 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
328 enum ice_version_type type = ice_devlink_versions[i].type;
329 const char *key = ice_devlink_versions[i].key;
330
331 memset(ctx->buf, 0, sizeof(ctx->buf));
332
333 ice_devlink_versions[i].getter(pf, ctx);
334
335 /* If the default getter doesn't report a version, use the
336 * fallback function. This is primarily useful in the case of
337 * "stored" versions that want to report the same value as the
338 * running version in the normal case of no pending update.
339 */
340 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
341 ice_devlink_versions[i].fallback(pf, ctx);
342
343 /* Do not report missing versions */
344 if (ctx->buf[0] == '\0')
345 continue;
346
347 switch (type) {
348 case ICE_VERSION_FIXED:
349 err = devlink_info_version_fixed_put(req, key, ctx->buf);
350 if (err) {
351 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
352 goto out_free_ctx;
353 }
354 break;
355 case ICE_VERSION_RUNNING:
356 err = devlink_info_version_running_put(req, key, ctx->buf);
357 if (err) {
358 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
359 goto out_free_ctx;
360 }
361 break;
362 case ICE_VERSION_STORED:
363 err = devlink_info_version_stored_put(req, key, ctx->buf);
364 if (err) {
365 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
366 goto out_free_ctx;
367 }
368 break;
369 }
370 }
371
372 out_free_ctx:
373 kfree(ctx);
374 return err;
375 }
376
377 /**
378 * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
379 * @devlink: pointer to the devlink instance to reload
380 * @netns_change: if true, the network namespace is changing
381 * @action: the action to perform. Must be DEVLINK_RELOAD_ACTION_FW_ACTIVATE
382 * @limit: limits on what reload should do, such as not resetting
383 * @extack: netlink extended ACK structure
384 *
385 * Allow user to activate new Embedded Management Processor firmware by
386 * issuing device specific EMP reset. Called in response to
387 * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
388 *
389 * Note that teardown and rebuild of the driver state happens automatically as
390 * part of an interrupt and watchdog task. This is because all physical
391 * functions on the device must be able to reset when an EMP reset occurs from
392 * any source.
393 */
394 static int
ice_devlink_reload_empr_start(struct devlink * devlink,bool netns_change,enum devlink_reload_action action,enum devlink_reload_limit limit,struct netlink_ext_ack * extack)395 ice_devlink_reload_empr_start(struct devlink *devlink, bool netns_change,
396 enum devlink_reload_action action,
397 enum devlink_reload_limit limit,
398 struct netlink_ext_ack *extack)
399 {
400 struct ice_pf *pf = devlink_priv(devlink);
401 struct device *dev = ice_pf_to_dev(pf);
402 struct ice_hw *hw = &pf->hw;
403 u8 pending;
404 int err;
405
406 err = ice_get_pending_updates(pf, &pending, extack);
407 if (err)
408 return err;
409
410 /* pending is a bitmask of which flash banks have a pending update,
411 * including the main NVM bank, the Option ROM bank, and the netlist
412 * bank. If any of these bits are set, then there is a pending update
413 * waiting to be activated.
414 */
415 if (!pending) {
416 NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
417 return -ECANCELED;
418 }
419
420 if (pf->fw_emp_reset_disabled) {
421 NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
422 return -ECANCELED;
423 }
424
425 dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
426
427 err = ice_aq_nvm_update_empr(hw);
428 if (err) {
429 dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
430 err, ice_aq_str(hw->adminq.sq_last_status));
431 NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
432 return err;
433 }
434
435 return 0;
436 }
437
438 /**
439 * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
440 * @devlink: pointer to the devlink instance reloading
441 * @action: the action requested
442 * @limit: limits imposed by userspace, such as not resetting
443 * @actions_performed: on return, indicate what actions actually performed
444 * @extack: netlink extended ACK structure
445 *
446 * Wait for driver to finish rebuilding after EMP reset is completed. This
447 * includes time to wait for both the actual device reset as well as the time
448 * for the driver's rebuild to complete.
449 */
450 static int
ice_devlink_reload_empr_finish(struct devlink * devlink,enum devlink_reload_action action,enum devlink_reload_limit limit,u32 * actions_performed,struct netlink_ext_ack * extack)451 ice_devlink_reload_empr_finish(struct devlink *devlink,
452 enum devlink_reload_action action,
453 enum devlink_reload_limit limit,
454 u32 *actions_performed,
455 struct netlink_ext_ack *extack)
456 {
457 struct ice_pf *pf = devlink_priv(devlink);
458 int err;
459
460 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
461
462 err = ice_wait_for_reset(pf, 60 * HZ);
463 if (err) {
464 NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
465 return err;
466 }
467
468 return 0;
469 }
470
471 /**
472 * ice_devlink_port_opt_speed_str - convert speed to a string
473 * @speed: speed value
474 */
ice_devlink_port_opt_speed_str(u8 speed)475 static const char *ice_devlink_port_opt_speed_str(u8 speed)
476 {
477 switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) {
478 case ICE_AQC_PORT_OPT_MAX_LANE_100M:
479 return "0.1";
480 case ICE_AQC_PORT_OPT_MAX_LANE_1G:
481 return "1";
482 case ICE_AQC_PORT_OPT_MAX_LANE_2500M:
483 return "2.5";
484 case ICE_AQC_PORT_OPT_MAX_LANE_5G:
485 return "5";
486 case ICE_AQC_PORT_OPT_MAX_LANE_10G:
487 return "10";
488 case ICE_AQC_PORT_OPT_MAX_LANE_25G:
489 return "25";
490 case ICE_AQC_PORT_OPT_MAX_LANE_50G:
491 return "50";
492 case ICE_AQC_PORT_OPT_MAX_LANE_100G:
493 return "100";
494 }
495
496 return "-";
497 }
498
499 #define ICE_PORT_OPT_DESC_LEN 50
500 /**
501 * ice_devlink_port_options_print - Print available port split options
502 * @pf: the PF to print split port options
503 *
504 * Prints a table with available port split options and max port speeds
505 */
ice_devlink_port_options_print(struct ice_pf * pf)506 static void ice_devlink_port_options_print(struct ice_pf *pf)
507 {
508 u8 i, j, options_count, cnt, speed, pending_idx, active_idx;
509 struct ice_aqc_get_port_options_elem *options, *opt;
510 struct device *dev = ice_pf_to_dev(pf);
511 bool active_valid, pending_valid;
512 char desc[ICE_PORT_OPT_DESC_LEN];
513 const char *str;
514 int status;
515
516 options = kcalloc(ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV,
517 sizeof(*options), GFP_KERNEL);
518 if (!options)
519 return;
520
521 for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) {
522 opt = options + i * ICE_AQC_PORT_OPT_MAX;
523 options_count = ICE_AQC_PORT_OPT_MAX;
524 active_valid = 0;
525
526 status = ice_aq_get_port_options(&pf->hw, opt, &options_count,
527 i, true, &active_idx,
528 &active_valid, &pending_idx,
529 &pending_valid);
530 if (status) {
531 dev_dbg(dev, "Couldn't read port option for port %d, err %d\n",
532 i, status);
533 goto err;
534 }
535 }
536
537 dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n");
538 dev_dbg(dev, "Status Split Quad 0 Quad 1\n");
539 dev_dbg(dev, " count L0 L1 L2 L3 L4 L5 L6 L7\n");
540
541 for (i = 0; i < options_count; i++) {
542 cnt = 0;
543
544 if (i == ice_active_port_option)
545 str = "Active";
546 else if ((i == pending_idx) && pending_valid)
547 str = "Pending";
548 else
549 str = "";
550
551 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
552 "%-8s", str);
553
554 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
555 "%-6u", options[i].pmd);
556
557 for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) {
558 speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed;
559 str = ice_devlink_port_opt_speed_str(speed);
560 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
561 "%3s ", str);
562 }
563
564 dev_dbg(dev, "%s\n", desc);
565 }
566
567 err:
568 kfree(options);
569 }
570
571 /**
572 * ice_devlink_aq_set_port_option - Send set port option admin queue command
573 * @pf: the PF to print split port options
574 * @option_idx: selected port option
575 * @extack: extended netdev ack structure
576 *
577 * Sends set port option admin queue command with selected port option and
578 * calls NVM write activate.
579 */
580 static int
ice_devlink_aq_set_port_option(struct ice_pf * pf,u8 option_idx,struct netlink_ext_ack * extack)581 ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx,
582 struct netlink_ext_ack *extack)
583 {
584 struct device *dev = ice_pf_to_dev(pf);
585 int status;
586
587 status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx);
588 if (status) {
589 dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n",
590 status, pf->hw.adminq.sq_last_status);
591 NL_SET_ERR_MSG_MOD(extack, "Port split request failed");
592 return -EIO;
593 }
594
595 status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE);
596 if (status) {
597 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
598 status, pf->hw.adminq.sq_last_status);
599 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
600 return -EIO;
601 }
602
603 status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL);
604 if (status) {
605 dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n",
606 status, pf->hw.adminq.sq_last_status);
607 NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data");
608 ice_release_nvm(&pf->hw);
609 return -EIO;
610 }
611
612 ice_release_nvm(&pf->hw);
613
614 NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split");
615 return 0;
616 }
617
618 /**
619 * ice_devlink_port_split - .port_split devlink handler
620 * @devlink: devlink instance structure
621 * @port: devlink port structure
622 * @count: number of ports to split to
623 * @extack: extended netdev ack structure
624 *
625 * Callback for the devlink .port_split operation.
626 *
627 * Unfortunately, the devlink expression of available options is limited
628 * to just a number, so search for an FW port option which supports
629 * the specified number. As there could be multiple FW port options with
630 * the same port split count, allow switching between them. When the same
631 * port split count request is issued again, switch to the next FW port
632 * option with the same port split count.
633 *
634 * Return: zero on success or an error code on failure.
635 */
636 static int
ice_devlink_port_split(struct devlink * devlink,struct devlink_port * port,unsigned int count,struct netlink_ext_ack * extack)637 ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port,
638 unsigned int count, struct netlink_ext_ack *extack)
639 {
640 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
641 u8 i, j, active_idx, pending_idx, new_option;
642 struct ice_pf *pf = devlink_priv(devlink);
643 u8 option_count = ICE_AQC_PORT_OPT_MAX;
644 struct device *dev = ice_pf_to_dev(pf);
645 bool active_valid, pending_valid;
646 int status;
647
648 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
649 0, true, &active_idx, &active_valid,
650 &pending_idx, &pending_valid);
651 if (status) {
652 dev_dbg(dev, "Couldn't read port split options, err = %d\n",
653 status);
654 NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options");
655 return -EIO;
656 }
657
658 new_option = ICE_AQC_PORT_OPT_MAX;
659 active_idx = pending_valid ? pending_idx : active_idx;
660 for (i = 1; i <= option_count; i++) {
661 /* In order to allow switching between FW port options with
662 * the same port split count, search for a new option starting
663 * from the active/pending option (with array wrap around).
664 */
665 j = (active_idx + i) % option_count;
666
667 if (count == options[j].pmd) {
668 new_option = j;
669 break;
670 }
671 }
672
673 if (new_option == active_idx) {
674 dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n",
675 count);
676 NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set");
677 ice_devlink_port_options_print(pf);
678 return -EINVAL;
679 }
680
681 if (new_option == ICE_AQC_PORT_OPT_MAX) {
682 dev_dbg(dev, "request to split: count: %u not found\n", count);
683 NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config");
684 ice_devlink_port_options_print(pf);
685 return -EINVAL;
686 }
687
688 status = ice_devlink_aq_set_port_option(pf, new_option, extack);
689 if (status)
690 return status;
691
692 ice_devlink_port_options_print(pf);
693
694 return 0;
695 }
696
697 /**
698 * ice_devlink_port_unsplit - .port_unsplit devlink handler
699 * @devlink: devlink instance structure
700 * @port: devlink port structure
701 * @extack: extended netdev ack structure
702 *
703 * Callback for the devlink .port_unsplit operation.
704 * Calls ice_devlink_port_split with split count set to 1.
705 * There could be no FW option available with split count 1.
706 *
707 * Return: zero on success or an error code on failure.
708 */
709 static int
ice_devlink_port_unsplit(struct devlink * devlink,struct devlink_port * port,struct netlink_ext_ack * extack)710 ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port,
711 struct netlink_ext_ack *extack)
712 {
713 return ice_devlink_port_split(devlink, port, 1, extack);
714 }
715
716 static const struct devlink_ops ice_devlink_ops = {
717 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
718 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
719 /* The ice driver currently does not support driver reinit */
720 .reload_down = ice_devlink_reload_empr_start,
721 .reload_up = ice_devlink_reload_empr_finish,
722 .port_split = ice_devlink_port_split,
723 .port_unsplit = ice_devlink_port_unsplit,
724 .eswitch_mode_get = ice_eswitch_mode_get,
725 .eswitch_mode_set = ice_eswitch_mode_set,
726 .info_get = ice_devlink_info_get,
727 .flash_update = ice_devlink_flash_update,
728 };
729
730 static int
ice_devlink_enable_roce_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)731 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
732 struct devlink_param_gset_ctx *ctx)
733 {
734 struct ice_pf *pf = devlink_priv(devlink);
735
736 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
737
738 return 0;
739 }
740
741 static int
ice_devlink_enable_roce_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)742 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
743 struct devlink_param_gset_ctx *ctx)
744 {
745 struct ice_pf *pf = devlink_priv(devlink);
746 bool roce_ena = ctx->val.vbool;
747 int ret;
748
749 if (!roce_ena) {
750 ice_unplug_aux_dev(pf);
751 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
752 return 0;
753 }
754
755 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
756 ret = ice_plug_aux_dev(pf);
757 if (ret)
758 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
759
760 return ret;
761 }
762
763 static int
ice_devlink_enable_roce_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)764 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
765 union devlink_param_value val,
766 struct netlink_ext_ack *extack)
767 {
768 struct ice_pf *pf = devlink_priv(devlink);
769
770 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
771 return -EOPNOTSUPP;
772
773 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
774 NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
775 return -EOPNOTSUPP;
776 }
777
778 return 0;
779 }
780
781 static int
ice_devlink_enable_iw_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)782 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
783 struct devlink_param_gset_ctx *ctx)
784 {
785 struct ice_pf *pf = devlink_priv(devlink);
786
787 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
788
789 return 0;
790 }
791
792 static int
ice_devlink_enable_iw_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)793 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
794 struct devlink_param_gset_ctx *ctx)
795 {
796 struct ice_pf *pf = devlink_priv(devlink);
797 bool iw_ena = ctx->val.vbool;
798 int ret;
799
800 if (!iw_ena) {
801 ice_unplug_aux_dev(pf);
802 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
803 return 0;
804 }
805
806 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
807 ret = ice_plug_aux_dev(pf);
808 if (ret)
809 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
810
811 return ret;
812 }
813
814 static int
ice_devlink_enable_iw_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)815 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
816 union devlink_param_value val,
817 struct netlink_ext_ack *extack)
818 {
819 struct ice_pf *pf = devlink_priv(devlink);
820
821 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
822 return -EOPNOTSUPP;
823
824 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
825 NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
826 return -EOPNOTSUPP;
827 }
828
829 return 0;
830 }
831
832 static const struct devlink_param ice_devlink_params[] = {
833 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
834 ice_devlink_enable_roce_get,
835 ice_devlink_enable_roce_set,
836 ice_devlink_enable_roce_validate),
837 DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
838 ice_devlink_enable_iw_get,
839 ice_devlink_enable_iw_set,
840 ice_devlink_enable_iw_validate),
841
842 };
843
ice_devlink_free(void * devlink_ptr)844 static void ice_devlink_free(void *devlink_ptr)
845 {
846 devlink_free((struct devlink *)devlink_ptr);
847 }
848
849 /**
850 * ice_allocate_pf - Allocate devlink and return PF structure pointer
851 * @dev: the device to allocate for
852 *
853 * Allocate a devlink instance for this device and return the private area as
854 * the PF structure. The devlink memory is kept track of through devres by
855 * adding an action to remove it when unwinding.
856 */
ice_allocate_pf(struct device * dev)857 struct ice_pf *ice_allocate_pf(struct device *dev)
858 {
859 struct devlink *devlink;
860
861 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
862 if (!devlink)
863 return NULL;
864
865 /* Add an action to teardown the devlink when unwinding the driver */
866 if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
867 return NULL;
868
869 return devlink_priv(devlink);
870 }
871
872 /**
873 * ice_devlink_register - Register devlink interface for this PF
874 * @pf: the PF to register the devlink for.
875 *
876 * Register the devlink instance associated with this physical function.
877 *
878 * Return: zero on success or an error code on failure.
879 */
ice_devlink_register(struct ice_pf * pf)880 void ice_devlink_register(struct ice_pf *pf)
881 {
882 struct devlink *devlink = priv_to_devlink(pf);
883
884 devlink_set_features(devlink, DEVLINK_F_RELOAD);
885 devlink_register(devlink);
886 }
887
888 /**
889 * ice_devlink_unregister - Unregister devlink resources for this PF.
890 * @pf: the PF structure to cleanup
891 *
892 * Releases resources used by devlink and cleans up associated memory.
893 */
ice_devlink_unregister(struct ice_pf * pf)894 void ice_devlink_unregister(struct ice_pf *pf)
895 {
896 devlink_unregister(priv_to_devlink(pf));
897 }
898
899 /**
900 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
901 * @pf: the PF to create a devlink port for
902 * @ppid: struct with switch id information
903 */
904 static void
ice_devlink_set_switch_id(struct ice_pf * pf,struct netdev_phys_item_id * ppid)905 ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
906 {
907 struct pci_dev *pdev = pf->pdev;
908 u64 id;
909
910 id = pci_get_dsn(pdev);
911
912 ppid->id_len = sizeof(id);
913 put_unaligned_be64(id, &ppid->id);
914 }
915
ice_devlink_register_params(struct ice_pf * pf)916 int ice_devlink_register_params(struct ice_pf *pf)
917 {
918 struct devlink *devlink = priv_to_devlink(pf);
919 union devlink_param_value value;
920 int err;
921
922 err = devlink_params_register(devlink, ice_devlink_params,
923 ARRAY_SIZE(ice_devlink_params));
924 if (err)
925 return err;
926
927 value.vbool = false;
928 devlink_param_driverinit_value_set(devlink,
929 DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
930 value);
931
932 value.vbool = test_bit(ICE_FLAG_RDMA_ENA, pf->flags) ? true : false;
933 devlink_param_driverinit_value_set(devlink,
934 DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
935 value);
936
937 return 0;
938 }
939
ice_devlink_unregister_params(struct ice_pf * pf)940 void ice_devlink_unregister_params(struct ice_pf *pf)
941 {
942 devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
943 ARRAY_SIZE(ice_devlink_params));
944 }
945
946 /**
947 * ice_devlink_set_port_split_options - Set port split options
948 * @pf: the PF to set port split options
949 * @attrs: devlink attributes
950 *
951 * Sets devlink port split options based on available FW port options
952 */
953 static void
ice_devlink_set_port_split_options(struct ice_pf * pf,struct devlink_port_attrs * attrs)954 ice_devlink_set_port_split_options(struct ice_pf *pf,
955 struct devlink_port_attrs *attrs)
956 {
957 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
958 u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX;
959 bool active_valid, pending_valid;
960 int status;
961
962 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
963 0, true, &active_idx, &active_valid,
964 &pending_idx, &pending_valid);
965 if (status) {
966 dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n",
967 status);
968 return;
969 }
970
971 /* find the biggest available port split count */
972 for (i = 0; i < option_count; i++)
973 attrs->lanes = max_t(int, attrs->lanes, options[i].pmd);
974
975 attrs->splittable = attrs->lanes ? 1 : 0;
976 ice_active_port_option = active_idx;
977 }
978
979 /**
980 * ice_devlink_create_pf_port - Create a devlink port for this PF
981 * @pf: the PF to create a devlink port for
982 *
983 * Create and register a devlink_port for this PF.
984 *
985 * Return: zero on success or an error code on failure.
986 */
ice_devlink_create_pf_port(struct ice_pf * pf)987 int ice_devlink_create_pf_port(struct ice_pf *pf)
988 {
989 struct devlink_port_attrs attrs = {};
990 struct devlink_port *devlink_port;
991 struct devlink *devlink;
992 struct ice_vsi *vsi;
993 struct device *dev;
994 int err;
995
996 dev = ice_pf_to_dev(pf);
997
998 devlink_port = &pf->devlink_port;
999
1000 vsi = ice_get_main_vsi(pf);
1001 if (!vsi)
1002 return -EIO;
1003
1004 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1005 attrs.phys.port_number = pf->hw.bus.func;
1006
1007 /* As FW supports only port split options for whole device,
1008 * set port split options only for first PF.
1009 */
1010 if (pf->hw.pf_id == 0)
1011 ice_devlink_set_port_split_options(pf, &attrs);
1012
1013 ice_devlink_set_switch_id(pf, &attrs.switch_id);
1014
1015 devlink_port_attrs_set(devlink_port, &attrs);
1016 devlink = priv_to_devlink(pf);
1017
1018 err = devlink_port_register(devlink, devlink_port, vsi->idx);
1019 if (err) {
1020 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
1021 pf->hw.pf_id, err);
1022 return err;
1023 }
1024
1025 return 0;
1026 }
1027
1028 /**
1029 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
1030 * @pf: the PF to cleanup
1031 *
1032 * Unregisters the devlink_port structure associated with this PF.
1033 */
ice_devlink_destroy_pf_port(struct ice_pf * pf)1034 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
1035 {
1036 struct devlink_port *devlink_port;
1037
1038 devlink_port = &pf->devlink_port;
1039
1040 devlink_port_type_clear(devlink_port);
1041 devlink_port_unregister(devlink_port);
1042 }
1043
1044 /**
1045 * ice_devlink_create_vf_port - Create a devlink port for this VF
1046 * @vf: the VF to create a port for
1047 *
1048 * Create and register a devlink_port for this VF.
1049 *
1050 * Return: zero on success or an error code on failure.
1051 */
ice_devlink_create_vf_port(struct ice_vf * vf)1052 int ice_devlink_create_vf_port(struct ice_vf *vf)
1053 {
1054 struct devlink_port_attrs attrs = {};
1055 struct devlink_port *devlink_port;
1056 struct devlink *devlink;
1057 struct ice_vsi *vsi;
1058 struct device *dev;
1059 struct ice_pf *pf;
1060 int err;
1061
1062 pf = vf->pf;
1063 dev = ice_pf_to_dev(pf);
1064 devlink_port = &vf->devlink_port;
1065
1066 vsi = ice_get_vf_vsi(vf);
1067 if (!vsi)
1068 return -EINVAL;
1069
1070 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
1071 attrs.pci_vf.pf = pf->hw.bus.func;
1072 attrs.pci_vf.vf = vf->vf_id;
1073
1074 ice_devlink_set_switch_id(pf, &attrs.switch_id);
1075
1076 devlink_port_attrs_set(devlink_port, &attrs);
1077 devlink = priv_to_devlink(pf);
1078
1079 err = devlink_port_register(devlink, devlink_port, vsi->idx);
1080 if (err) {
1081 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
1082 vf->vf_id, err);
1083 return err;
1084 }
1085
1086 return 0;
1087 }
1088
1089 /**
1090 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
1091 * @vf: the VF to cleanup
1092 *
1093 * Unregisters the devlink_port structure associated with this VF.
1094 */
ice_devlink_destroy_vf_port(struct ice_vf * vf)1095 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
1096 {
1097 struct devlink_port *devlink_port;
1098
1099 devlink_port = &vf->devlink_port;
1100
1101 devlink_port_type_clear(devlink_port);
1102 devlink_port_unregister(devlink_port);
1103 }
1104
1105 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1106
1107 /**
1108 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1109 * @devlink: the devlink instance
1110 * @ops: the devlink region being snapshotted
1111 * @extack: extended ACK response structure
1112 * @data: on exit points to snapshot data buffer
1113 *
1114 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1115 * the nvm-flash devlink region. It captures a snapshot of the full NVM flash
1116 * contents, including both banks of flash. This snapshot can later be viewed
1117 * via the devlink-region interface.
1118 *
1119 * It captures the flash using the FLASH_ONLY bit set when reading via
1120 * firmware, so it does not read the current Shadow RAM contents. For that,
1121 * use the shadow-ram region.
1122 *
1123 * @returns zero on success, and updates the data pointer. Returns a non-zero
1124 * error code on failure.
1125 */
ice_devlink_nvm_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)1126 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1127 const struct devlink_region_ops *ops,
1128 struct netlink_ext_ack *extack, u8 **data)
1129 {
1130 struct ice_pf *pf = devlink_priv(devlink);
1131 struct device *dev = ice_pf_to_dev(pf);
1132 struct ice_hw *hw = &pf->hw;
1133 u8 *nvm_data, *tmp, i;
1134 u32 nvm_size, left;
1135 s8 num_blks;
1136 int status;
1137
1138 nvm_size = hw->flash.flash_size;
1139 nvm_data = vzalloc(nvm_size);
1140 if (!nvm_data)
1141 return -ENOMEM;
1142
1143
1144 num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1145 tmp = nvm_data;
1146 left = nvm_size;
1147
1148 /* Some systems take longer to read the NVM than others which causes the
1149 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1150 * this by breaking the reads of the NVM into smaller chunks that will
1151 * probably not take as long. This has some overhead since we are
1152 * increasing the number of AQ commands, but it should always work
1153 */
1154 for (i = 0; i < num_blks; i++) {
1155 u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1156
1157 status = ice_acquire_nvm(hw, ICE_RES_READ);
1158 if (status) {
1159 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1160 status, hw->adminq.sq_last_status);
1161 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1162 vfree(nvm_data);
1163 return -EIO;
1164 }
1165
1166 status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1167 &read_sz, tmp, false);
1168 if (status) {
1169 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1170 read_sz, status, hw->adminq.sq_last_status);
1171 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1172 ice_release_nvm(hw);
1173 vfree(nvm_data);
1174 return -EIO;
1175 }
1176 ice_release_nvm(hw);
1177
1178 tmp += read_sz;
1179 left -= read_sz;
1180 }
1181
1182 *data = nvm_data;
1183
1184 return 0;
1185 }
1186
1187 /**
1188 * ice_devlink_sram_snapshot - Capture a snapshot of the Shadow RAM contents
1189 * @devlink: the devlink instance
1190 * @ops: the devlink region being snapshotted
1191 * @extack: extended ACK response structure
1192 * @data: on exit points to snapshot data buffer
1193 *
1194 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1195 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
1196 * contents. This snapshot can later be viewed via the devlink-region
1197 * interface.
1198 *
1199 * @returns zero on success, and updates the data pointer. Returns a non-zero
1200 * error code on failure.
1201 */
1202 static int
ice_devlink_sram_snapshot(struct devlink * devlink,const struct devlink_region_ops __always_unused * ops,struct netlink_ext_ack * extack,u8 ** data)1203 ice_devlink_sram_snapshot(struct devlink *devlink,
1204 const struct devlink_region_ops __always_unused *ops,
1205 struct netlink_ext_ack *extack, u8 **data)
1206 {
1207 struct ice_pf *pf = devlink_priv(devlink);
1208 struct device *dev = ice_pf_to_dev(pf);
1209 struct ice_hw *hw = &pf->hw;
1210 u8 *sram_data;
1211 u32 sram_size;
1212 int err;
1213
1214 sram_size = hw->flash.sr_words * 2u;
1215 sram_data = vzalloc(sram_size);
1216 if (!sram_data)
1217 return -ENOMEM;
1218
1219 err = ice_acquire_nvm(hw, ICE_RES_READ);
1220 if (err) {
1221 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1222 err, hw->adminq.sq_last_status);
1223 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1224 vfree(sram_data);
1225 return err;
1226 }
1227
1228 /* Read from the Shadow RAM, rather than directly from NVM */
1229 err = ice_read_flat_nvm(hw, 0, &sram_size, sram_data, true);
1230 if (err) {
1231 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1232 sram_size, err, hw->adminq.sq_last_status);
1233 NL_SET_ERR_MSG_MOD(extack,
1234 "Failed to read Shadow RAM contents");
1235 ice_release_nvm(hw);
1236 vfree(sram_data);
1237 return err;
1238 }
1239
1240 ice_release_nvm(hw);
1241
1242 *data = sram_data;
1243
1244 return 0;
1245 }
1246
1247 /**
1248 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1249 * @devlink: the devlink instance
1250 * @ops: the devlink region being snapshotted
1251 * @extack: extended ACK response structure
1252 * @data: on exit points to snapshot data buffer
1253 *
1254 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1255 * the device-caps devlink region. It captures a snapshot of the device
1256 * capabilities reported by firmware.
1257 *
1258 * @returns zero on success, and updates the data pointer. Returns a non-zero
1259 * error code on failure.
1260 */
1261 static int
ice_devlink_devcaps_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)1262 ice_devlink_devcaps_snapshot(struct devlink *devlink,
1263 const struct devlink_region_ops *ops,
1264 struct netlink_ext_ack *extack, u8 **data)
1265 {
1266 struct ice_pf *pf = devlink_priv(devlink);
1267 struct device *dev = ice_pf_to_dev(pf);
1268 struct ice_hw *hw = &pf->hw;
1269 void *devcaps;
1270 int status;
1271
1272 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1273 if (!devcaps)
1274 return -ENOMEM;
1275
1276 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1277 ice_aqc_opc_list_dev_caps, NULL);
1278 if (status) {
1279 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1280 status, hw->adminq.sq_last_status);
1281 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1282 vfree(devcaps);
1283 return status;
1284 }
1285
1286 *data = (u8 *)devcaps;
1287
1288 return 0;
1289 }
1290
1291 static const struct devlink_region_ops ice_nvm_region_ops = {
1292 .name = "nvm-flash",
1293 .destructor = vfree,
1294 .snapshot = ice_devlink_nvm_snapshot,
1295 };
1296
1297 static const struct devlink_region_ops ice_sram_region_ops = {
1298 .name = "shadow-ram",
1299 .destructor = vfree,
1300 .snapshot = ice_devlink_sram_snapshot,
1301 };
1302
1303 static const struct devlink_region_ops ice_devcaps_region_ops = {
1304 .name = "device-caps",
1305 .destructor = vfree,
1306 .snapshot = ice_devlink_devcaps_snapshot,
1307 };
1308
1309 /**
1310 * ice_devlink_init_regions - Initialize devlink regions
1311 * @pf: the PF device structure
1312 *
1313 * Create devlink regions used to enable access to dump the contents of the
1314 * flash memory on the device.
1315 */
ice_devlink_init_regions(struct ice_pf * pf)1316 void ice_devlink_init_regions(struct ice_pf *pf)
1317 {
1318 struct devlink *devlink = priv_to_devlink(pf);
1319 struct device *dev = ice_pf_to_dev(pf);
1320 u64 nvm_size, sram_size;
1321
1322 nvm_size = pf->hw.flash.flash_size;
1323 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
1324 nvm_size);
1325 if (IS_ERR(pf->nvm_region)) {
1326 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1327 PTR_ERR(pf->nvm_region));
1328 pf->nvm_region = NULL;
1329 }
1330
1331 sram_size = pf->hw.flash.sr_words * 2u;
1332 pf->sram_region = devlink_region_create(devlink, &ice_sram_region_ops,
1333 1, sram_size);
1334 if (IS_ERR(pf->sram_region)) {
1335 dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1336 PTR_ERR(pf->sram_region));
1337 pf->sram_region = NULL;
1338 }
1339
1340 pf->devcaps_region = devlink_region_create(devlink,
1341 &ice_devcaps_region_ops, 10,
1342 ICE_AQ_MAX_BUF_LEN);
1343 if (IS_ERR(pf->devcaps_region)) {
1344 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1345 PTR_ERR(pf->devcaps_region));
1346 pf->devcaps_region = NULL;
1347 }
1348 }
1349
1350 /**
1351 * ice_devlink_destroy_regions - Destroy devlink regions
1352 * @pf: the PF device structure
1353 *
1354 * Remove previously created regions for this PF.
1355 */
ice_devlink_destroy_regions(struct ice_pf * pf)1356 void ice_devlink_destroy_regions(struct ice_pf *pf)
1357 {
1358 if (pf->nvm_region)
1359 devlink_region_destroy(pf->nvm_region);
1360
1361 if (pf->sram_region)
1362 devlink_region_destroy(pf->sram_region);
1363
1364 if (pf->devcaps_region)
1365 devlink_region_destroy(pf->devcaps_region);
1366 }
1367