ice: devlink: use %*phD to print small buffer
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_devlink.c
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 static int ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len)
10 {
11         u8 dsn[8];
12
13         /* Copy the DSN into an array in Big Endian format */
14         put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
15
16         snprintf(buf, len, "%8phD", dsn);
17
18         return 0;
19 }
20
21 static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
22 {
23         struct ice_hw *hw = &pf->hw;
24         enum ice_status status;
25
26         status = ice_read_pba_string(hw, (u8 *)buf, len);
27         if (status)
28                 return -EIO;
29
30         return 0;
31 }
32
33 static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len)
34 {
35         struct ice_hw *hw = &pf->hw;
36
37         snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
38                  hw->fw_patch);
39
40         return 0;
41 }
42
43 static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len)
44 {
45         struct ice_hw *hw = &pf->hw;
46
47         snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver);
48
49         return 0;
50 }
51
52 static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len)
53 {
54         struct ice_hw *hw = &pf->hw;
55
56         snprintf(buf, len, "0x%08x", hw->fw_build);
57
58         return 0;
59 }
60
61 static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len)
62 {
63         struct ice_orom_info *orom = &pf->hw.nvm.orom;
64
65         snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch);
66
67         return 0;
68 }
69
70 static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len)
71 {
72         struct ice_nvm_info *nvm = &pf->hw.nvm;
73
74         snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver);
75
76         return 0;
77 }
78
79 static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len)
80 {
81         struct ice_nvm_info *nvm = &pf->hw.nvm;
82
83         snprintf(buf, len, "0x%08x", nvm->eetrack);
84
85         return 0;
86 }
87
88 static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len)
89 {
90         struct ice_hw *hw = &pf->hw;
91
92         snprintf(buf, len, "%s", hw->active_pkg_name);
93
94         return 0;
95 }
96
97 static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len)
98 {
99         struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
100
101         snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
102                  pkg->draft);
103
104         return 0;
105 }
106
107 static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
108 {
109         struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
110
111         /* The netlist version fields are BCD formatted */
112         snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
113                  netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
114                  netlist->cust_ver);
115
116         return 0;
117 }
118
119 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
120 {
121         struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
122
123         snprintf(buf, len, "0x%08x", netlist->hash);
124
125         return 0;
126 }
127
128 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
129 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
130
131 enum ice_version_type {
132         ICE_VERSION_FIXED,
133         ICE_VERSION_RUNNING,
134         ICE_VERSION_STORED,
135 };
136
137 static const struct ice_devlink_version {
138         enum ice_version_type type;
139         const char *key;
140         int (*getter)(struct ice_pf *pf, char *buf, size_t len);
141 } ice_devlink_versions[] = {
142         fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
143         running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
144         running("fw.mgmt.api", ice_info_fw_api),
145         running("fw.mgmt.build", ice_info_fw_build),
146         running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
147         running("fw.psid.api", ice_info_nvm_ver),
148         running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
149         running("fw.app.name", ice_info_ddp_pkg_name),
150         running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
151         running("fw.netlist", ice_info_netlist_ver),
152         running("fw.netlist.build", ice_info_netlist_build),
153 };
154
155 /**
156  * ice_devlink_info_get - .info_get devlink handler
157  * @devlink: devlink instance structure
158  * @req: the devlink info request
159  * @extack: extended netdev ack structure
160  *
161  * Callback for the devlink .info_get operation. Reports information about the
162  * device.
163  *
164  * Return: zero on success or an error code on failure.
165  */
166 static int ice_devlink_info_get(struct devlink *devlink,
167                                 struct devlink_info_req *req,
168                                 struct netlink_ext_ack *extack)
169 {
170         struct ice_pf *pf = devlink_priv(devlink);
171         char buf[100];
172         size_t i;
173         int err;
174
175         err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
176         if (err) {
177                 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
178                 return err;
179         }
180
181         err = ice_info_get_dsn(pf, buf, sizeof(buf));
182         if (err) {
183                 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
184                 return err;
185         }
186
187         err = devlink_info_serial_number_put(req, buf);
188         if (err) {
189                 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
190                 return err;
191         }
192
193         for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
194                 enum ice_version_type type = ice_devlink_versions[i].type;
195                 const char *key = ice_devlink_versions[i].key;
196
197                 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
198                 if (err) {
199                         NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
200                         return err;
201                 }
202
203                 switch (type) {
204                 case ICE_VERSION_FIXED:
205                         err = devlink_info_version_fixed_put(req, key, buf);
206                         if (err) {
207                                 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
208                                 return err;
209                         }
210                         break;
211                 case ICE_VERSION_RUNNING:
212                         err = devlink_info_version_running_put(req, key, buf);
213                         if (err) {
214                                 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
215                                 return err;
216                         }
217                         break;
218                 case ICE_VERSION_STORED:
219                         err = devlink_info_version_stored_put(req, key, buf);
220                         if (err) {
221                                 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
222                                 return err;
223                         }
224                         break;
225                 }
226         }
227
228         return 0;
229 }
230
231 /**
232  * ice_devlink_flash_update - Update firmware stored in flash on the device
233  * @devlink: pointer to devlink associated with device to update
234  * @params: flash update parameters
235  * @extack: netlink extended ACK structure
236  *
237  * Perform a device flash update. The bulk of the update logic is contained
238  * within the ice_flash_pldm_image function.
239  *
240  * Returns: zero on success, or an error code on failure.
241  */
242 static int
243 ice_devlink_flash_update(struct devlink *devlink,
244                          struct devlink_flash_update_params *params,
245                          struct netlink_ext_ack *extack)
246 {
247         struct ice_pf *pf = devlink_priv(devlink);
248         struct device *dev = &pf->pdev->dev;
249         struct ice_hw *hw = &pf->hw;
250         const struct firmware *fw;
251         u8 preservation;
252         int err;
253
254         if (!params->overwrite_mask) {
255                 /* preserve all settings and identifiers */
256                 preservation = ICE_AQC_NVM_PRESERVE_ALL;
257         } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
258                 /* overwrite settings, but preserve the vital device identifiers */
259                 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
260         } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
261                                               DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
262                 /* overwrite both settings and identifiers, preserve nothing */
263                 preservation = ICE_AQC_NVM_NO_PRESERVATION;
264         } else {
265                 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
266                 return -EOPNOTSUPP;
267         }
268
269         if (!hw->dev_caps.common_cap.nvm_unified_update) {
270                 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
271                 return -EOPNOTSUPP;
272         }
273
274         err = ice_check_for_pending_update(pf, NULL, extack);
275         if (err)
276                 return err;
277
278         err = request_firmware(&fw, params->file_name, dev);
279         if (err) {
280                 NL_SET_ERR_MSG_MOD(extack, "Unable to read file from disk");
281                 return err;
282         }
283
284         devlink_flash_update_begin_notify(devlink);
285         devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
286         err = ice_flash_pldm_image(pf, fw, preservation, extack);
287         devlink_flash_update_end_notify(devlink);
288
289         release_firmware(fw);
290
291         return err;
292 }
293
294 static const struct devlink_ops ice_devlink_ops = {
295         .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
296         .info_get = ice_devlink_info_get,
297         .flash_update = ice_devlink_flash_update,
298 };
299
300 static void ice_devlink_free(void *devlink_ptr)
301 {
302         devlink_free((struct devlink *)devlink_ptr);
303 }
304
305 /**
306  * ice_allocate_pf - Allocate devlink and return PF structure pointer
307  * @dev: the device to allocate for
308  *
309  * Allocate a devlink instance for this device and return the private area as
310  * the PF structure. The devlink memory is kept track of through devres by
311  * adding an action to remove it when unwinding.
312  */
313 struct ice_pf *ice_allocate_pf(struct device *dev)
314 {
315         struct devlink *devlink;
316
317         devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
318         if (!devlink)
319                 return NULL;
320
321         /* Add an action to teardown the devlink when unwinding the driver */
322         if (devm_add_action(dev, ice_devlink_free, devlink)) {
323                 devlink_free(devlink);
324                 return NULL;
325         }
326
327         return devlink_priv(devlink);
328 }
329
330 /**
331  * ice_devlink_register - Register devlink interface for this PF
332  * @pf: the PF to register the devlink for.
333  *
334  * Register the devlink instance associated with this physical function.
335  *
336  * Return: zero on success or an error code on failure.
337  */
338 int ice_devlink_register(struct ice_pf *pf)
339 {
340         struct devlink *devlink = priv_to_devlink(pf);
341         struct device *dev = ice_pf_to_dev(pf);
342         int err;
343
344         err = devlink_register(devlink, dev);
345         if (err) {
346                 dev_err(dev, "devlink registration failed: %d\n", err);
347                 return err;
348         }
349
350         return 0;
351 }
352
353 /**
354  * ice_devlink_unregister - Unregister devlink resources for this PF.
355  * @pf: the PF structure to cleanup
356  *
357  * Releases resources used by devlink and cleans up associated memory.
358  */
359 void ice_devlink_unregister(struct ice_pf *pf)
360 {
361         devlink_unregister(priv_to_devlink(pf));
362 }
363
364 /**
365  * ice_devlink_create_port - Create a devlink port for this PF
366  * @pf: the PF to create a port for
367  *
368  * Create and register a devlink_port for this PF. Note that although each
369  * physical function is connected to a separate devlink instance, the port
370  * will still be numbered according to the physical function ID.
371  *
372  * Return: zero on success or an error code on failure.
373  */
374 int ice_devlink_create_port(struct ice_pf *pf)
375 {
376         struct devlink *devlink = priv_to_devlink(pf);
377         struct ice_vsi *vsi = ice_get_main_vsi(pf);
378         struct device *dev = ice_pf_to_dev(pf);
379         struct devlink_port_attrs attrs = {};
380         int err;
381
382         if (!vsi) {
383                 dev_err(dev, "%s: unable to find main VSI\n", __func__);
384                 return -EIO;
385         }
386
387         attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
388         attrs.phys.port_number = pf->hw.pf_id;
389         devlink_port_attrs_set(&pf->devlink_port, &attrs);
390         err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
391         if (err) {
392                 dev_err(dev, "devlink_port_register failed: %d\n", err);
393                 return err;
394         }
395
396         return 0;
397 }
398
399 /**
400  * ice_devlink_destroy_port - Destroy the devlink_port for this PF
401  * @pf: the PF to cleanup
402  *
403  * Unregisters the devlink_port structure associated with this PF.
404  */
405 void ice_devlink_destroy_port(struct ice_pf *pf)
406 {
407         devlink_port_type_clear(&pf->devlink_port);
408         devlink_port_unregister(&pf->devlink_port);
409 }
410
411 /**
412  * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
413  * @devlink: the devlink instance
414  * @ops: the devlink region being snapshotted
415  * @extack: extended ACK response structure
416  * @data: on exit points to snapshot data buffer
417  *
418  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
419  * the shadow-ram devlink region. It captures a snapshot of the shadow ram
420  * contents. This snapshot can later be viewed via the devlink-region
421  * interface.
422  *
423  * @returns zero on success, and updates the data pointer. Returns a non-zero
424  * error code on failure.
425  */
426 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
427                                     const struct devlink_region_ops *ops,
428                                     struct netlink_ext_ack *extack, u8 **data)
429 {
430         struct ice_pf *pf = devlink_priv(devlink);
431         struct device *dev = ice_pf_to_dev(pf);
432         struct ice_hw *hw = &pf->hw;
433         enum ice_status status;
434         void *nvm_data;
435         u32 nvm_size;
436
437         nvm_size = hw->nvm.flash_size;
438         nvm_data = vzalloc(nvm_size);
439         if (!nvm_data)
440                 return -ENOMEM;
441
442         status = ice_acquire_nvm(hw, ICE_RES_READ);
443         if (status) {
444                 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
445                         status, hw->adminq.sq_last_status);
446                 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
447                 vfree(nvm_data);
448                 return -EIO;
449         }
450
451         status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
452         if (status) {
453                 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
454                         nvm_size, status, hw->adminq.sq_last_status);
455                 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
456                 ice_release_nvm(hw);
457                 vfree(nvm_data);
458                 return -EIO;
459         }
460
461         ice_release_nvm(hw);
462
463         *data = nvm_data;
464
465         return 0;
466 }
467
468 /**
469  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
470  * @devlink: the devlink instance
471  * @ops: the devlink region being snapshotted
472  * @extack: extended ACK response structure
473  * @data: on exit points to snapshot data buffer
474  *
475  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
476  * the device-caps devlink region. It captures a snapshot of the device
477  * capabilities reported by firmware.
478  *
479  * @returns zero on success, and updates the data pointer. Returns a non-zero
480  * error code on failure.
481  */
482 static int
483 ice_devlink_devcaps_snapshot(struct devlink *devlink,
484                              const struct devlink_region_ops *ops,
485                              struct netlink_ext_ack *extack, u8 **data)
486 {
487         struct ice_pf *pf = devlink_priv(devlink);
488         struct device *dev = ice_pf_to_dev(pf);
489         struct ice_hw *hw = &pf->hw;
490         enum ice_status status;
491         void *devcaps;
492
493         devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
494         if (!devcaps)
495                 return -ENOMEM;
496
497         status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
498                                   ice_aqc_opc_list_dev_caps, NULL);
499         if (status) {
500                 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
501                         status, hw->adminq.sq_last_status);
502                 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
503                 vfree(devcaps);
504                 return -EIO;
505         }
506
507         *data = (u8 *)devcaps;
508
509         return 0;
510 }
511
512 static const struct devlink_region_ops ice_nvm_region_ops = {
513         .name = "nvm-flash",
514         .destructor = vfree,
515         .snapshot = ice_devlink_nvm_snapshot,
516 };
517
518 static const struct devlink_region_ops ice_devcaps_region_ops = {
519         .name = "device-caps",
520         .destructor = vfree,
521         .snapshot = ice_devlink_devcaps_snapshot,
522 };
523
524 /**
525  * ice_devlink_init_regions - Initialize devlink regions
526  * @pf: the PF device structure
527  *
528  * Create devlink regions used to enable access to dump the contents of the
529  * flash memory on the device.
530  */
531 void ice_devlink_init_regions(struct ice_pf *pf)
532 {
533         struct devlink *devlink = priv_to_devlink(pf);
534         struct device *dev = ice_pf_to_dev(pf);
535         u64 nvm_size;
536
537         nvm_size = pf->hw.nvm.flash_size;
538         pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
539                                                nvm_size);
540         if (IS_ERR(pf->nvm_region)) {
541                 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
542                         PTR_ERR(pf->nvm_region));
543                 pf->nvm_region = NULL;
544         }
545
546         pf->devcaps_region = devlink_region_create(devlink,
547                                                    &ice_devcaps_region_ops, 10,
548                                                    ICE_AQ_MAX_BUF_LEN);
549         if (IS_ERR(pf->devcaps_region)) {
550                 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
551                         PTR_ERR(pf->devcaps_region));
552                 pf->devcaps_region = NULL;
553         }
554 }
555
556 /**
557  * ice_devlink_destroy_regions - Destroy devlink regions
558  * @pf: the PF device structure
559  *
560  * Remove previously created regions for this PF.
561  */
562 void ice_devlink_destroy_regions(struct ice_pf *pf)
563 {
564         if (pf->nvm_region)
565                 devlink_region_destroy(pf->nvm_region);
566         if (pf->devcaps_region)
567                 devlink_region_destroy(pf->devcaps_region);
568 }