xref: /OK3568_Linux_fs/kernel/Documentation/networking/devlink/devlink-flash.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun.. _devlink_flash:
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun=============
6*4882a593SmuzhiyunDevlink Flash
7*4882a593Smuzhiyun=============
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunThe ``devlink-flash`` API allows updating device firmware. It replaces the
10*4882a593Smuzhiyunolder ``ethtool-flash`` mechanism, and doesn't require taking any
11*4882a593Smuzhiyunnetworking locks in the kernel to perform the flash update. Example use::
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun  $ devlink dev flash pci/0000:05:00.0 file flash-boot.bin
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunNote that the file name is a path relative to the firmware loading path
16*4882a593Smuzhiyun(usually ``/lib/firmware/``). Drivers may send status updates to inform
17*4882a593Smuzhiyunuser space about the progress of the update operation.
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunOverwrite Mask
20*4882a593Smuzhiyun==============
21*4882a593Smuzhiyun
22*4882a593SmuzhiyunThe ``devlink-flash`` command allows optionally specifying a mask indicating
23*4882a593Smuzhiyunhow the device should handle subsections of flash components when updating.
24*4882a593SmuzhiyunThis mask indicates the set of sections which are allowed to be overwritten.
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun.. list-table:: List of overwrite mask bits
27*4882a593Smuzhiyun   :widths: 5 95
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun   * - Name
30*4882a593Smuzhiyun     - Description
31*4882a593Smuzhiyun   * - ``DEVLINK_FLASH_OVERWRITE_SETTINGS``
32*4882a593Smuzhiyun     - Indicates that the device should overwrite settings in the components
33*4882a593Smuzhiyun       being updated with the settings found in the provided image.
34*4882a593Smuzhiyun   * - ``DEVLINK_FLASH_OVERWRITE_IDENTIFIERS``
35*4882a593Smuzhiyun     - Indicates that the device should overwrite identifiers in the
36*4882a593Smuzhiyun       components being updated with the identifiers found in the provided
37*4882a593Smuzhiyun       image. This includes MAC addresses, serial IDs, and similar device
38*4882a593Smuzhiyun       identifiers.
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunMultiple overwrite bits may be combined and requested together. If no bits
41*4882a593Smuzhiyunare provided, it is expected that the device only update firmware binaries
42*4882a593Smuzhiyunin the components being updated. Settings and identifiers are expected to be
43*4882a593Smuzhiyunpreserved across the update. A device may not support every combination and
44*4882a593Smuzhiyunthe driver for such a device must reject any combination which cannot be
45*4882a593Smuzhiyunfaithfully implemented.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunFirmware Loading
48*4882a593Smuzhiyun================
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunDevices which require firmware to operate usually store it in non-volatile
51*4882a593Smuzhiyunmemory on the board, e.g. flash. Some devices store only basic firmware on
52*4882a593Smuzhiyunthe board, and the driver loads the rest from disk during probing.
53*4882a593Smuzhiyun``devlink-info`` allows users to query firmware information (loaded
54*4882a593Smuzhiyuncomponents and versions).
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunIn other cases the device can both store the image on the board, load from
57*4882a593Smuzhiyundisk, or automatically flash a new image from disk. The ``fw_load_policy``
58*4882a593Smuzhiyundevlink parameter can be used to control this behavior
59*4882a593Smuzhiyun(:ref:`Documentation/networking/devlink/devlink-params.rst <devlink_params_generic>`).
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunOn-disk firmware files are usually stored in ``/lib/firmware/``.
62*4882a593Smuzhiyun
63*4882a593SmuzhiyunFirmware Version Management
64*4882a593Smuzhiyun===========================
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunDrivers are expected to implement ``devlink-flash`` and ``devlink-info``
67*4882a593Smuzhiyunfunctionality, which together allow for implementing vendor-independent
68*4882a593Smuzhiyunautomated firmware update facilities.
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun``devlink-info`` exposes the ``driver`` name and three version groups
71*4882a593Smuzhiyun(``fixed``, ``running``, ``stored``).
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunThe ``driver`` attribute and ``fixed`` group identify the specific device
74*4882a593Smuzhiyundesign, e.g. for looking up applicable firmware updates. This is why
75*4882a593Smuzhiyun``serial_number`` is not part of the ``fixed`` versions (even though it
76*4882a593Smuzhiyunis fixed) - ``fixed`` versions should identify the design, not a single
77*4882a593Smuzhiyundevice.
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun``running`` and ``stored`` firmware versions identify the firmware running
80*4882a593Smuzhiyunon the device, and firmware which will be activated after reboot or device
81*4882a593Smuzhiyunreset.
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunThe firmware update agent is supposed to be able to follow this simple
84*4882a593Smuzhiyunalgorithm to update firmware contents, regardless of the device vendor:
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun.. code-block:: sh
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun  # Get unique HW design identifier
89*4882a593Smuzhiyun  $hw_id = devlink-dev-info['fixed']
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun  # Find out which FW flash we want to use for this NIC
92*4882a593Smuzhiyun  $want_flash_vers = some-db-backed.lookup($hw_id, 'flash')
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun  # Update flash if necessary
95*4882a593Smuzhiyun  if $want_flash_vers != devlink-dev-info['stored']:
96*4882a593Smuzhiyun      $file = some-db-backed.download($hw_id, 'flash')
97*4882a593Smuzhiyun      devlink-dev-flash($file)
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun  # Find out the expected overall firmware versions
100*4882a593Smuzhiyun  $want_fw_vers = some-db-backed.lookup($hw_id, 'all')
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun  # Update on-disk file if necessary
103*4882a593Smuzhiyun  if $want_fw_vers != devlink-dev-info['running']:
104*4882a593Smuzhiyun      $file = some-db-backed.download($hw_id, 'disk')
105*4882a593Smuzhiyun      write($file, '/lib/firmware/')
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun  # Try device reset, if available
108*4882a593Smuzhiyun  if $want_fw_vers != devlink-dev-info['running']:
109*4882a593Smuzhiyun     devlink-reset()
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun  # Reboot, if reset wasn't enough
112*4882a593Smuzhiyun  if $want_fw_vers != devlink-dev-info['running']:
113*4882a593Smuzhiyun     reboot()
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunNote that each reference to ``devlink-dev-info`` in this pseudo-code
116*4882a593Smuzhiyunis expected to fetch up-to-date information from the kernel.
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunFor the convenience of identifying firmware files some vendors add
119*4882a593Smuzhiyun``bundle_id`` information to the firmware versions. This meta-version covers
120*4882a593Smuzhiyunmultiple per-component versions and can be used e.g. in firmware file names
121*4882a593Smuzhiyun(all component versions could get rather long.)
122