Skip to contentSkip to conversation

Extend FW framework for user FW uploads

RFC PATCH In review Russ Weight 4 participants 15 messages Feb 3, 2022 – Feb 11, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Extend FW framework for user FW uploads

Extend the firmware loader subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards automatically load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images that are uploaded by the user.

A device driver may call fw_upload_register() to expose persistent "loading" and "data" sysfs files at /sys/class/firmare/<NAME>/*. These files are used in the same way as the fallback sysfs "loading" and "data" files. However, when 0 is written to "loading" to complete the write of firmware data, the data is also transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.

Additional sysfs nodes are added in the same location as "loading" and "data" to monitor the transfer of the image data to the device using callback functions provided by the lower-level device driver and to allow the data transfer to be cancelled.

Example usage:

$ pwd
/sys/class/firmware/n3000bmc-sec-update.8
$ ls
cancel  device  loading  remaining_size  subsystem
data    error   power    status          uevent
$ echo 1 > loading
$ cat /tmp/firmware.bin > data
$ echo 0 > loading
$ while :; do cat status; cat remaining_size ; sleep 3; done
preparing
44590080
<--snip-->
transferring
44459008
transferring
44311552
<--snip-->
<snip>
transferring
173056
<--snip-->
programming
0
<--snip-->
idle
0
^C

The first two patches in this set make minor changes to enable the fw_priv data structure and the sysfs interfaces to be used multiple times during the existence of the device driver instance. The third patch is mostly a reorganization of existing code in preparation for sharing common code with the firmware-upload support. The final two patches provide the code for user-initiated firmware uploads.

This driver was previously submitted in the context of the FPGA sub- system as the "FPGA Image Load Framework", but the framework is generic enough to support other devices as well. The previous submission of this patch-set can be viewed here:

https://marc.info/?l=linux-kernel&m=163295640216820&w=2

The n3000bmc-sec-update driver is the first driver to use the Firmware Upload API. A recent version of these patches can be viewed here:

https://marc.info/?l=linux-kernel&m=163295697217095&w=2

- Russ

Russ Weight (5):
firmware_loader: Clear data and size in fw_free_paged_buf
firmware_loader: Check fw_state_is_done in loading_store
firmware_loader: Split fw_sysfs support from fallback
firmware_loader: Add firmware-upload support
firmware_loader: Add sysfs nodes to monitor fw_upload

 .../ABI/testing/sysfs-class-firmware          |  77 +++
 .../driver-api/firmware/fw_upload.rst         | 103 ++++
 Documentation/driver-api/firmware/index.rst   |   1 +
 drivers/base/firmware_loader/Kconfig          |  18 +
 drivers/base/firmware_loader/Makefile         |   2 +
 drivers/base/firmware_loader/fallback.c       | 430 ----------------
 drivers/base/firmware_loader/fallback.h       |  46 +-
 drivers/base/firmware_loader/firmware.h       |  11 +
 drivers/base/firmware_loader/fw_sysfs.c       | 468 ++++++++++++++++++
 drivers/base/firmware_loader/fw_sysfs.h       |  98 ++++
 drivers/base/firmware_loader/fw_upload.c      | 348 +++++++++++++
 drivers/base/firmware_loader/fw_upload.h      |  29 ++
 drivers/base/firmware_loader/main.c           |  18 +-
 include/linux/firmware.h                      |  72 +++
 14 files changed, 1236 insertions(+), 485 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-firmware
 create mode 100644 Documentation/driver-api/firmware/fw_upload.rst
 create mode 100644 drivers/base/firmware_loader/fw_sysfs.c
 create mode 100644 drivers/base/firmware_loader/fw_sysfs.h
 create mode 100644 drivers/base/firmware_loader/fw_upload.c
 create mode 100644 drivers/base/firmware_loader/fw_upload.h
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/5 firmware_loader: Clear data and size in fw_free_paged_buf

The fw_free_paged_buf() function resets the paged buffer information in the fw_priv data structure. Additionally, clear the data and size members of fw_priv in order to facilitate the reuse of fw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface.

---
 drivers/base/firmware_loader/main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/firmware_loader/main.c +2 -0
index 94d1789a233e..2cc11d93753a 100644
@@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv)
 	fw_priv->pages = NULL;
 	fw_priv->page_array_size = 0;
 	fw_priv->nr_pages = 0;
+	fw_priv->data = NULL;
+	fw_priv->size = 0;
 }
 
 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed)
Signature · 1 line
2.25.1
#2 lore raw

RFC PATCH 2/5 firmware_loader: Check fw_state_is_done in loading_store

In reply to Russ Weight

Add the fw_state_is_done() function and exit early from firmware_loading_store() if the state is already "done". This is being done in preparation for supporting persistent sysfs nodes to allow userspace to upload firmware to a device, potentially reusing the sysfs loading and data files multiple times.

---
 drivers/base/firmware_loader/fallback.c | 2 +-
 drivers/base/firmware_loader/firmware.h | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
drivers/base/firmware_loader/fallback.c +1 -1
index 4afb0e9312c0..d82e055a4297 100644
@@ -250,7 +250,7 @@ static ssize_t firmware_loading_store(struct device *dev,
 
 	mutex_lock(&fw_lock);
 	fw_priv = fw_sysfs->fw_priv;
-	if (fw_state_is_aborted(fw_priv))
+	if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv))
 		goto out;
 
 	switch (loading) {
drivers/base/firmware_loader/firmware.h +5 -0
index 2889f446ad41..58768d16f8df 100644
@@ -149,6 +149,11 @@ static inline void fw_state_done(struct fw_priv *fw_priv)
 	__fw_state_set(fw_priv, FW_STATUS_DONE);
 }
 
+static inline bool fw_state_is_done(struct fw_priv *fw_priv)
+{
+	return __fw_state_check(fw_priv, FW_STATUS_DONE);
+}
+
 int assign_fw(struct firmware *fw, struct device *device);
 
 #ifdef CONFIG_FW_LOADER
Signature · 1 line
2.25.1
#3 lore raw

RFC PATCH 3/5 firmware_loader: Split fw_sysfs support from fallback

In reply to Russ Weight

In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into fw_sysfs.c and fw_sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node.

CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include fw_sysfs.o in firmware_class-objs.

This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.

---
 drivers/base/firmware_loader/Kconfig    |   4 +
 drivers/base/firmware_loader/Makefile   |   1 +
 drivers/base/firmware_loader/fallback.c | 430 ------------------------
 drivers/base/firmware_loader/fallback.h |  46 +--
 drivers/base/firmware_loader/fw_sysfs.c | 413 +++++++++++++++++++++++
 drivers/base/firmware_loader/fw_sysfs.h |  94 ++++++
 6 files changed, 513 insertions(+), 475 deletions(-)
 create mode 100644 drivers/base/firmware_loader/fw_sysfs.c
 create mode 100644 drivers/base/firmware_loader/fw_sysfs.h
drivers/base/firmware_loader/Kconfig +4 -0
index 5b24f3959255..1bfe18900ed5 100644
@@ -29,6 +29,9 @@ if FW_LOADER
 config FW_LOADER_PAGED_BUF
 	bool
 
+config FW_LOADER_SYSFS
+	bool
+
 config EXTRA_FIRMWARE
 	string "Build named firmware blobs into the kernel binary"
 	help
@@ -70,6 +73,7 @@ config EXTRA_FIRMWARE_DIR
 
 config FW_LOADER_USER_HELPER
 	bool "Enable the firmware sysfs fallback mechanism"
+	select FW_LOADER_SYSFS
 	select FW_LOADER_PAGED_BUF
 	help
 	  This option enables a sysfs loading facility to enable firmware
drivers/base/firmware_loader/Makefile +1 -0
index e87843408fe6..787c833d0c6e 100644
@@ -6,5 +6,6 @@ obj-$(CONFIG_FW_LOADER)	+= firmware_class.o
 firmware_class-objs := main.o
 firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o
 firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o
+firmware_class-$(CONFIG_FW_LOADER_SYSFS) += fw_sysfs.o
 
 obj-y += builtin/
drivers/base/firmware_loader/fallback.c +0 -430

462 lines not on this page · raw message

drivers/base/firmware_loader/fallback.h +1 -45

64 lines not on this page · raw message

drivers/base/firmware_loader/fw_sysfs.c new file +413 -0

413 lines not on this page · raw message

drivers/base/firmware_loader/fw_sysfs.h new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [RFC PATCH 3/5] firmware_loader: Split fw_sysfs support from fallback

Quoted 15 lines · Russ Weight

In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into fw_sysfs.c and fw_sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node.

CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include fw_sysfs.o in firmware_class-objs.

This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.

Quoted 8 lines
---
 drivers/base/firmware_loader/Kconfig    |   4 +
 drivers/base/firmware_loader/Makefile   |   1 +
 drivers/base/firmware_loader/fallback.c | 430 ------------------------
 drivers/base/firmware_loader/fallback.h |  46 +--
 drivers/base/firmware_loader/fw_sysfs.c | 413 +++++++++++++++++++++++
 drivers/base/firmware_loader/fw_sysfs.h |  94 ++++++

Nit: please just use sysfs.[ch]. The directory already implies its for firmware.

Luis

#5 lore raw

Re: [RFC PATCH 3/5] firmware_loader: Split fw_sysfs support from fallback

Quoted 24 lines · Luis Chamberlain
Quoted 15 lines · Russ Weight

In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into fw_sysfs.c and fw_sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node.

CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include fw_sysfs.o in firmware_class-objs.

This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.

Quoted 8 lines
---
 drivers/base/firmware_loader/Kconfig    |   4 +
 drivers/base/firmware_loader/Makefile   |   1 +
 drivers/base/firmware_loader/fallback.c | 430 ------------------------
 drivers/base/firmware_loader/fallback.h |  46 +--
 drivers/base/firmware_loader/fw_sysfs.c | 413 +++++++++++++++++++++++
 drivers/base/firmware_loader/fw_sysfs.h |  94 ++++++
Quoted 2 lines

Nit: please just use sysfs.[ch]. The directory already implies its for firmware.

Sure. I'm assuming the same comment would apply to fw_upload.[ch] int he following patch, so I'll remove fw_ for those as well.

- Russ

Quoted 2 lines

Luis

#6 lore raw

Re: [RFC PATCH 3/5] firmware_loader: Split fw_sysfs support from fallback

In reply to Russ Weight
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Russ,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on driver-core/driver-core-testing] [also build test ERROR on linus/master v5.17-rc2 next-20220203] [cannot apply to linux/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Russ-Weight/Extend-FW-framework-for-user-FW-uploads/20220204-053320
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git 13e906e50a8cf6033f22c03c4d772e36a9e02c6b
config: hexagon-randconfig-r045-20220130 (https://download.01.org/0day-ci/archive/20220204/202202040744.q4EU7xJp-lkp(a)intel.com/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project a73e4ce6a59b01f0e37037761c1e6889d539d233)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/c230afe198b50ce7265dc77e7f63e780b30f8521
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Russ-Weight/Extend-FW-framework-for-user-FW-uploads/20220204-053320 git checkout c230afe198b50ce7265dc77e7f63e780b30f8521
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash drivers/base/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from drivers/base/firmware_loader/main.c:44:
In file included from drivers/base/firmware_loader/fallback.h:9:

>> drivers/base/firmware_loader/fw_sysfs.h:72:27: error: incomplete definition of type 'struct firmware_fallback_config'
           return fw_fallback_config.loading_timeout;
                  ~~~~~~~~~~~~~~~~~~^
   drivers/base/firmware_loader/fw_sysfs.h:7:15: note: forward declaration of 'struct firmware_fallback_config'
   extern struct firmware_fallback_config fw_fallback_config;
                 ^
   drivers/base/firmware_loader/fw_sysfs.h:78:20: error: incomplete definition of type 'struct firmware_fallback_config'
           fw_fallback_config.loading_timeout = timeout;
           ~~~~~~~~~~~~~~~~~~^
   drivers/base/firmware_loader/fw_sysfs.h:7:15: note: forward declaration of 'struct firmware_fallback_config'
   extern struct firmware_fallback_config fw_fallback_config;
                 ^

2 errors generated.

vim +72 drivers/base/firmware_loader/fw_sysfs.h

68
69 /* These getters are vetted to use int properly */
70 static inline int __firmware_loading_timeout(void)
71 {

  > 72		return fw_fallback_config.loading_timeout;
    73	}
    74	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

Re: [RFC PATCH 3/5] firmware_loader: Split fw_sysfs support from fallback

In reply to Russ Weight
Quoted 10 lines · Russ Weight

In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into fw_sysfs.c and fw_sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node.

CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include fw_sysfs.o in firmware_class-objs.

Quoted 6 lines

This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.

Consider how bisectable and reviewable this patch is.

I think this patch should be first since reorganizing should not depend on anything new.

Other users of fallback will want to know if this change is going to break them, so the reorganization should be as mechanical as possible to make the review as easy as possible and easy to revert if something goes wrong.  Ex/ the whitespace changes made as a separate patch to the old file, not embedded in the new file.  The new Kconfig added later if needed.

Also consider if this move is needed at all, generalizing in the existing file is ok.

Tom

#8 lore raw

Re: [RFC PATCH 3/5] firmware_loader: Split fw_sysfs support from fallback

Quoted 17 lines · Tom Rix
Quoted 15 lines · Russ Weight

In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into fw_sysfs.c and fw_sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node.

CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include fw_sysfs.o in firmware_class-objs.

This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch.

Quoted 5 lines
Quoted 1 line

Consider how bisectable and reviewable this patch is.

I think this patch should be first since reorganizing should not depend on anything new.

Hi Tom,

The first two patches are very small and stand on there own. I put them first because they could be taken independent of the rest of the patches.

Quoted 4 lines

Other users of fallback will want to know if this change is going to break them, so the reorganization should be as mechanical as possible to make the review as easy as possible and easy to revert if something goes wrong.  Ex/ the whitespace changes made as a separate patch to the old file, not embedded in the new file.  The new Kconfig added later if needed.

Also consider if this move is needed at all, generalizing in the existing file is ok.

I did give this a lot of consideration. I'm open to reorganizing the code if needed. These are some of the things I considered:

The context of a user-initiated firmware upload is quite different from firmware-fallback. I want to reuse the sysfs support for loading and data, but I don't think firmware upload should share the same config options as firmware-fallback, and incorporating the firmware-upload support into the existing fallback.[ch] files seems pretty ugly. I think the split is the right choice.

The split was largely mechanical, except for the following
(1) Kconfig and Makefile changes - without Makefile changes, the resulting patch wouldn't compile. I suppose I could defer the config change, but it is a small and obvious change.
(2) Data and function scopes: the resulting patch would not compile without these changes.
(3) Whitespace changes: the purpose of these changes was to quiet a handful of errors checkpatch. Moving them to a different patch would defeat the original purpose of make the changes at all. Maybe these changes simply aren't needed?

I could defer the Kconfig change and remove or defer the checkpatch-related white-space changes.

Any other opinions on the code reorganization in this patch?

- Russ

Quoted 3 lines

Tom

#9 lore raw

RFC PATCH 4/5 firmware_loader: Add firmware-upload support

In reply to Russ Weight

Extend the firmware subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images provided by the user at his/her convenience.

A device driver may call fw_upload_register() to expose persistent "loading" and "data" sysfs files. These files are used in the same way as the fallback sysfs "loading" and "data" files. When 0 is written to "loading" to complete the write of firmware data, the data is transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.

---
 .../ABI/testing/sysfs-class-firmware          |  32 +++
 .../driver-api/firmware/fw_upload.rst         |  86 +++++++
 Documentation/driver-api/firmware/index.rst   |   1 +
 drivers/base/firmware_loader/Kconfig          |  14 ++
 drivers/base/firmware_loader/Makefile         |   1 +
 drivers/base/firmware_loader/firmware.h       |   6 +
 drivers/base/firmware_loader/fw_sysfs.c       |  50 +++-
 drivers/base/firmware_loader/fw_sysfs.h       |   4 +
 drivers/base/firmware_loader/fw_upload.c      | 229 ++++++++++++++++++
 drivers/base/firmware_loader/fw_upload.h      |  24 ++
 drivers/base/firmware_loader/main.c           |  16 +-
 include/linux/firmware.h                      |  72 ++++++
 12 files changed, 523 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-firmware
 create mode 100644 Documentation/driver-api/firmware/fw_upload.rst
 create mode 100644 drivers/base/firmware_loader/fw_upload.c
 create mode 100644 drivers/base/firmware_loader/fw_upload.h
Documentation/ABI/testing/sysfs-class-firmware new file +32 -0
new file mode 100644
index 000000000000..a2e518f0bf8a
@@ -0,0 +1,32 @@
+What: 		/sys/class/firmware/.../data
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	The data sysfs file is used for firmware-fallback and for
+		firmware uploads. Cat a firmware image to this sysfs file
+		after you echo 1 to the loading sysfs file. When the firmware
+		image write is complete, echo 0 to the loading sysfs file. This
+		sequence will signal the completion of the firmware write and
+		signal the lower-level driver that the firmware data is
+		available.
+
+What: 		/sys/class/firmware/.../loading
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	The loading sysfs file is used for both firmware-fallback and
+		for firmware uploads. Echo 1 onto the loading file to indicate
+		you are writing a firmware file to the data sysfs node. Echo
+		-1 onto this file to abort the data write or echo 0 onto this
+		file to indicate that the write is complete. For firmware
+		uploads, the zero value also triggers the transfer of the
+		firmware data to the lower-level device driver.
+
+What: 		/sys/class/firmware/.../timeout
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	This file supports the timeout mechanism for firmware
+		fallback.  This file has no affect on firmware uploads. For
+		more information on timeouts please see the documentation
+		for firmware fallback.
Documentation/driver-api/firmware/fw_upload.rst new file +86 -0

86 lines not on this page · raw message

Documentation/driver-api/firmware/index.rst +1 -0
index 57415d657173..9d2c19dc8e36 100644
@@ -8,6 +8,7 @@ Linux Firmware API
    core
    efi/index
    request_firmware
+   fw_upload
    other_interfaces
 
 .. only::  subproject and html
drivers/base/firmware_loader/Kconfig +14 -0
index 1bfe18900ed5..cee662f3277b 100644
@@ -185,5 +185,19 @@ config FW_CACHE
 
 	  If unsure, say Y.
 
+config FW_UPLOAD
+	bool "Enable users to initiate firmware updates using sysfs"
+	select FW_LOADER_SYSFS
+	select FW_LOADER_PAGED_BUF
+	help
+	  Enabling this option will allow device drivers to expose a persistent
+	  sysfs interface that allows firmware updates to be initiated from
+	  userspace. For example, FPGA based PCIe cards load firmware and FPGA
+	  images from local FLASH when the card boots. The images in FLASH may
+	  be updated with new images provided by the user. Enable this device
+	  to support cards that rely on user-initiated updates for firmware files.
+
+	  If unsure, say N.
+
 endif # FW_LOADER
 endmenu
drivers/base/firmware_loader/Makefile +1 -0
index 787c833d0c6e..52ef64bd9357 100644
@@ -7,5 +7,6 @@ firmware_class-objs := main.o
 firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o
 firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o
 firmware_class-$(CONFIG_FW_LOADER_SYSFS) += fw_sysfs.o
+firmware_class-$(CONFIG_FW_UPLOAD) += fw_upload.o
 
 obj-y += builtin/
drivers/base/firmware_loader/firmware.h +6 -0
index 58768d16f8df..4019f9423de8 100644
@@ -87,6 +87,7 @@ struct fw_priv {
 };
 
 extern struct mutex fw_lock;
+extern struct firmware_cache fw_cache;
 
 static inline bool __fw_state_check(struct fw_priv *fw_priv,
 				    enum fw_status status)
@@ -154,7 +155,12 @@ static inline bool fw_state_is_done(struct fw_priv *fw_priv)
 	return __fw_state_check(fw_priv, FW_STATUS_DONE);
 }
 
+int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
+			 struct fw_priv **fw_priv, void *dbuf, size_t size,
+			 size_t offset, u32 opt_flags);
 int assign_fw(struct firmware *fw, struct device *device);
+void free_fw_priv(struct fw_priv *fw_priv);
+void fw_state_init(struct fw_priv *fw_priv);
 
 #ifdef CONFIG_FW_LOADER
 bool firmware_is_builtin(const struct firmware *fw);
drivers/base/firmware_loader/fw_sysfs.c +48 -2

87 lines not on this page · raw message

drivers/base/firmware_loader/fw_sysfs.h +4 -0
index 0ca7f72892b0..1aadc12244d7 100644
@@ -4,7 +4,10 @@
 
 #include <linux/device.h>
 
+#include "firmware.h"
+
 extern struct firmware_fallback_config fw_fallback_config;
+extern struct device_attribute dev_attr_loading;
 
 #ifdef CONFIG_FW_LOADER_USER_HELPER
 /**
@@ -59,6 +62,7 @@ struct fw_sysfs {
 	struct device dev;
 	struct fw_priv *fw_priv;
 	struct firmware *fw;
+	void *fw_upload_priv;
 };
 
 static inline struct fw_sysfs *to_fw_sysfs(struct device *dev)
drivers/base/firmware_loader/fw_upload.c new file +229 -0

229 lines not on this page · raw message

drivers/base/firmware_loader/fw_upload.h new file +24 -0
new file mode 100644
index 000000000000..c55ac4047d55
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __FIRMWARE_UPLOAD_H
+#define __FIRMWARE_UPLOAD_H
+
+#include <linux/device.h>
+
+struct fw_upload_priv {
+	struct fw_upload *fw_upload;
+	const char *name;
+	const struct fw_upload_ops *ops;
+	struct mutex lock;		/* protect data structure contents */
+	struct work_struct work;
+	const u8 *data;			/* pointer to update data */
+	u32 remaining_size;		/* size remaining to transfer */
+	u32 progress;
+	u32 err_progress;		/* progress at time of failure */
+	u32 err_code;			/* security manager error code */
+	bool driver_unload;
+};
+
+umode_t
+fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n);
+
+#endif /* __FIRMWARE_UPLOAD_H */
drivers/base/firmware_loader/main.c +6 -10
index 2cc11d93753a..874a5ef31c56 100644
@@ -91,9 +91,9 @@ static inline struct fw_priv *to_fw_priv(struct kref *ref)
  * guarding for corner cases a global lock should be OK */
 DEFINE_MUTEX(fw_lock);
 
-static struct firmware_cache fw_cache;
+struct firmware_cache fw_cache;
 
-static void fw_state_init(struct fw_priv *fw_priv)
+void fw_state_init(struct fw_priv *fw_priv)
 {
 	struct fw_state *fw_st = &fw_priv->fw_st;
 
@@ -163,13 +163,9 @@ static struct fw_priv *__lookup_fw_priv(const char *fw_name)
 }
 
 /* Returns 1 for batching firmware requests with the same name */
-static int alloc_lookup_fw_priv(const char *fw_name,
-				struct firmware_cache *fwc,
-				struct fw_priv **fw_priv,
-				void *dbuf,
-				size_t size,
-				size_t offset,
-				u32 opt_flags)
+int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
+			 struct fw_priv **fw_priv, void *dbuf, size_t size,
+			 size_t offset, u32 opt_flags)
 {
 	struct fw_priv *tmp;
 
@@ -224,7 +220,7 @@ static void __free_fw_priv(struct kref *ref)
 	kfree(fw_priv);
 }
 
-static void free_fw_priv(struct fw_priv *fw_priv)
+void free_fw_priv(struct fw_priv *fw_priv)
 {
 	struct firmware_cache *fwc = fw_priv->fwc;
 	spin_lock(&fwc->lock);
include/linux/firmware.h +72 -0

84 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#10 lore raw

Re: [RFC PATCH 4/5] firmware_loader: Add firmware-upload support

Quoted 300 lines · Russ Weight

Extend the firmware subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images provided by the user at his/her convenience.

A device driver may call fw_upload_register() to expose persistent "loading" and "data" sysfs files. These files are used in the same way as the fallback sysfs "loading" and "data" files. When 0 is written to "loading" to complete the write of firmware data, the data is transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.

---
 .../ABI/testing/sysfs-class-firmware          |  32 +++
 .../driver-api/firmware/fw_upload.rst         |  86 +++++++
 Documentation/driver-api/firmware/index.rst   |   1 +
 drivers/base/firmware_loader/Kconfig          |  14 ++
 drivers/base/firmware_loader/Makefile         |   1 +
 drivers/base/firmware_loader/firmware.h       |   6 +
 drivers/base/firmware_loader/fw_sysfs.c       |  50 +++-
 drivers/base/firmware_loader/fw_sysfs.h       |   4 +
 drivers/base/firmware_loader/fw_upload.c      | 229 ++++++++++++++++++
 drivers/base/firmware_loader/fw_upload.h      |  24 ++
 drivers/base/firmware_loader/main.c           |  16 +-
 include/linux/firmware.h                      |  72 ++++++
 12 files changed, 523 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-firmware
 create mode 100644 Documentation/driver-api/firmware/fw_upload.rst
 create mode 100644 drivers/base/firmware_loader/fw_upload.c
 create mode 100644 drivers/base/firmware_loader/fw_upload.h
Documentation/ABI/testing/sysfs-class-firmware new file +32 -0
new file mode 100644
index 000000000000..a2e518f0bf8a
@@ -0,0 +1,32 @@
+What: 		/sys/class/firmware/.../data
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	The data sysfs file is used for firmware-fallback and for
+		firmware uploads. Cat a firmware image to this sysfs file
+		after you echo 1 to the loading sysfs file. When the firmware
+		image write is complete, echo 0 to the loading sysfs file. This
+		sequence will signal the completion of the firmware write and
+		signal the lower-level driver that the firmware data is
+		available.
+
+What: 		/sys/class/firmware/.../loading
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	The loading sysfs file is used for both firmware-fallback and
+		for firmware uploads. Echo 1 onto the loading file to indicate
+		you are writing a firmware file to the data sysfs node. Echo
+		-1 onto this file to abort the data write or echo 0 onto this
+		file to indicate that the write is complete. For firmware
+		uploads, the zero value also triggers the transfer of the
+		firmware data to the lower-level device driver.
+
+What: 		/sys/class/firmware/.../timeout
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	This file supports the timeout mechanism for firmware
+		fallback.  This file has no affect on firmware uploads. For
+		more information on timeouts please see the documentation
+		for firmware fallback.
Documentation/driver-api/firmware/fw_upload.rst new file +86 -0
new file mode 100644
index 000000000000..bf272f627a1f
@@ -0,0 +1,86 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=============
+fw_upload API
+=============
+
+A device driver that registers with the firmware loader will expose
+persistent sysfs nodes to enable users to initiate firmware updates for
+that device.  It is the responsibility of the device driver and/or the
+device itself to perform any validation on the data received. Firmware
+upload uses the same *loading* and *data* sysfs files described in the
+documentation for firmware fallback.
+
+Register for firmware upload
+============================
+
+A device driver registers for firmware upload by calling fw_upload_register().
+Among the parameter list is a name to identify the device under
+/sys/class/firmware. A user may initiate a firmware upload by echoing
+a 1 to the *loading* sysfs file for the target device. Next, the user writes
+the firmware image to the *data* sysfs file. After writing the firmware
+data, the user echos 0 to the *loading* sysfs file to signal completion.
+Echoing 0 to *loading* also triggers the transfer of the firmware to the
+lower-lever device driver in the context of a kernel worker thread.
+
+To use the fw_upload API, write a driver that implements a set of ops. The
+probe function calls fw_upload_register() and the remove function calls
+fw_upload_unregister() such as::
+
+	static const struct fw_upload_ops m10bmc_ops = {
+		.prepare = m10bmc_sec_prepare,
+		.write = m10bmc_sec_write,
+		.poll_complete = m10bmc_sec_poll_complete,
+		.cancel = m10bmc_sec_cancel,
+		.cleanup = m10bmc_sec_cleanup,
+	};
+
+	static int m10bmc_sec_probe(struct platform_device *pdev)
+	{
+		const char *fw_name, *truncate;
+		struct m10bmc_sec *sec;
+		struct fw_upload *fwl;
+		unsigned int len;
+
+		sec = devm_kzalloc(&pdev->dev, sizeof(*sec), GFP_KERNEL);
+		if (!sec)
+			return -ENOMEM;
+
+		sec->dev = &pdev->dev;
+		sec->m10bmc = dev_get_drvdata(pdev->dev.parent);
+		dev_set_drvdata(&pdev->dev, sec);
+
+		fw_name = dev_name(sec->dev);
+		truncate = strstr(fw_name, ".auto");
+		len = (truncate) ? truncate - fw_name : strlen(fw_name);
+		sec->fw_name = kmemdup_nul(fw_name, len, GFP_KERNEL);
+
+		fwl = fw_upload_register(sec->dev, sec->fw_name, &m10bmc_ops, sec);
+		if (IS_ERR(fwl)) {
+			dev_err(sec->dev, "Firmware Upload driver failed to start\n");
+			kfree(sec->fw_name);
+			return PTR_ERR(fwl);
+		}
+
+		sec->fwl = fwl;
+		return 0;
+	}
+
+	static int m10bmc_sec_remove(struct platform_device *pdev)
+	{
+		struct m10bmc_sec *sec = dev_get_drvdata(&pdev->dev);
+
+		fw_upload_unregister(sec->fwl);
+		kfree(sec->fw_name);
+		return 0;
+	}
+
+fw_upload_register
+------------------
+.. kernel-doc:: drivers/base/firmware_loader/fw_upload.c
+   :functions: fw_upload_register
+
+fw_upload_unregister
+--------------------
+.. kernel-doc:: drivers/base/firmware_loader/fw_upload.c
+   :functions: fw_upload_unregister
Documentation/driver-api/firmware/index.rst +1 -0
index 57415d657173..9d2c19dc8e36 100644
@@ -8,6 +8,7 @@ Linux Firmware API
    core
    efi/index
    request_firmware
+   fw_upload
    other_interfaces
 
 .. only::  subproject and html
drivers/base/firmware_loader/Kconfig +14 -0
index 1bfe18900ed5..cee662f3277b 100644
@@ -185,5 +185,19 @@ config FW_CACHE
 
 	  If unsure, say Y.
 
+config FW_UPLOAD
+	bool "Enable users to initiate firmware updates using sysfs"
+	select FW_LOADER_SYSFS
+	select FW_LOADER_PAGED_BUF
+	help
+	  Enabling this option will allow device drivers to expose a persistent
+	  sysfs interface that allows firmware updates to be initiated from
+	  userspace. For example, FPGA based PCIe cards load firmware and FPGA
+	  images from local FLASH when the card boots. The images in FLASH may
+	  be updated with new images provided by the user. Enable this device
+	  to support cards that rely on user-initiated updates for firmware files.
+
+	  If unsure, say N.
+
 endif # FW_LOADER
 endmenu
drivers/base/firmware_loader/Makefile +1 -0
index 787c833d0c6e..52ef64bd9357 100644
@@ -7,5 +7,6 @@ firmware_class-objs := main.o
 firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o
 firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o
 firmware_class-$(CONFIG_FW_LOADER_SYSFS) += fw_sysfs.o
+firmware_class-$(CONFIG_FW_UPLOAD) += fw_upload.o
 
 obj-y += builtin/
drivers/base/firmware_loader/firmware.h +6 -0
index 58768d16f8df..4019f9423de8 100644
@@ -87,6 +87,7 @@ struct fw_priv {
 };
 
 extern struct mutex fw_lock;
+extern struct firmware_cache fw_cache;
 
 static inline bool __fw_state_check(struct fw_priv *fw_priv,
 				    enum fw_status status)
@@ -154,7 +155,12 @@ static inline bool fw_state_is_done(struct fw_priv *fw_priv)
 	return __fw_state_check(fw_priv, FW_STATUS_DONE);
 }
 
+int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
+			 struct fw_priv **fw_priv, void *dbuf, size_t size,
+			 size_t offset, u32 opt_flags);
 int assign_fw(struct firmware *fw, struct device *device);
+void free_fw_priv(struct fw_priv *fw_priv);
+void fw_state_init(struct fw_priv *fw_priv);
 
 #ifdef CONFIG_FW_LOADER
 bool firmware_is_builtin(const struct firmware *fw);
drivers/base/firmware_loader/fw_sysfs.c +34 -1
index 70cb1d67ffb2..9b0cd37c81df 100644
@@ -6,8 +6,8 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#include "firmware.h"
 #include "fw_sysfs.h"
+#include "fw_upload.h"
 
 /*
  * sysfs support for firmware loader
@@ -80,6 +80,10 @@ static void fw_dev_release(struct device *dev)
 {
 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
 
+	if (fw_sysfs->fw_upload_priv) {
+		free_fw_priv(fw_sysfs->fw_priv);
+		kfree(fw_sysfs->fw_upload_priv);
+	}
 	kfree(fw_sysfs);
 }
 
@@ -165,6 +169,9 @@ static ssize_t firmware_loading_store(struct device *dev,
 				      const char *buf, size_t count)
 {
 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
+#ifdef CONFIG_FW_UPLOAD
+	struct fw_upload_priv *fwlp;
+#endif
 	struct fw_priv *fw_priv;
 	ssize_t written = count;
 	int loading = simple_strtol(buf, NULL, 10);
@@ -211,6 +218,42 @@ static ssize_t firmware_loading_store(struct device *dev,
 				written = rc;
 			} else {
 				fw_state_done(fw_priv);
+
+#ifdef CONFIG_FW_UPLOAD
+				/*
+				 * For fw_uploads, start a worker thread to upload
+				 * data to the parent driver.
+				 */
+				if (!fw_sysfs->fw_upload_priv)
+					break;
+
+				if (!fw_priv->size) {
+					fw_free_paged_buf(fw_priv);
+					fw_state_init(fw_sysfs->fw_priv);
+					break;
+				}
+
+				fwlp = fw_sysfs->fw_upload_priv;
+				mutex_lock(&fwlp->lock);
+
+				/* Do not interfere an on-going fw_upload */
+				if (fwlp->progress != FW_UPLOAD_PROG_IDLE) {
+					mutex_unlock(&fwlp->lock);
+					written = -EBUSY;
+					goto out;
+				}
+
+				fwlp->progress = FW_UPLOAD_PROG_RECEIVING;

drivers/base/firmware_loader/fw_sysfs.c static ssize_t firmware_loading_store(struct device *dev, · line 256

Quoted 10 lines
drivers/base/firmware_loader/fw_sysfs.c +10 -0
+				fwlp->err_code = 0;
+				fwlp->remaining_size = fw_priv->size;
+				fwlp->data = fw_priv->data;
+				pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
+					 __func__, fw_priv->fw_name,
+					 fw_priv, fw_priv->data,
+					 (unsigned int)fw_priv->size);
+				queue_work(system_long_wq, &fwlp->work);
+				mutex_unlock(&fwlp->lock);
+#endif

If you're going to split this work up it'd be nice to avoid to have #ifdef stuff here.

drivers/base/firmware_loader/fw_upload.c

Quoted 1 line
drivers/base/firmware_loader/fw_upload.c +0 -0

No need for fw prefix, sysfs-upload.[ch] would make it clearer.

Quoted 1 line

+EXPORT_SYMBOL_GPL(fw_upload_register);

firmware_upload_register()

Quoted 1 line

+EXPORT_SYMBOL_GPL(fw_upload_unregister);

firmware_upload_unregister()

Quoted 7 lines
include/linux/firmware.h +0 -0
index 3b057dfc8284..9b109f8ff627 100644
@@ -17,6 +17,56 @@ struct firmware {
 	void *priv;
 };

include/linux/firmware.h struct firmware { · line 28

Quoted 10 lines
include/linux/firmware.h +7 -0
 
 
 
+/* Update progress codes */
+#define FW_UPLOAD_PROG_IDLE		0
+#define FW_UPLOAD_PROG_RECEIVING	1
+#define FW_UPLOAD_PROG_PREPARING	2
+#define FW_UPLOAD_PROG_TRANSFERRING	3
+#define FW_UPLOAD_PROG_PROGRAMMING	4
+#define FW_UPLOAD_PROG_MAX		5

enums with kdoc allows you to nicely document these as well.

include/linux/firmware.h struct firmware { · line 36

Quoted 10 lines
include/linux/firmware.h +10 -0
+
+/* Update error progress codes */
+#define FW_UPLOAD_ERR_HW_ERROR		1
+#define FW_UPLOAD_ERR_TIMEOUT		2
+#define FW_UPLOAD_ERR_CANCELED		3
+#define FW_UPLOAD_ERR_BUSY		4
+#define FW_UPLOAD_ERR_INVALID_SIZE	5
+#define FW_UPLOAD_ERR_RW_ERROR		6
+#define FW_UPLOAD_ERR_WEAROUT		7
+#define FW_UPLOAD_ERR_MAX		8

enums with kdoc allows you to nicely document these as well.

Luis

#11 lore raw

Re: [RFC PATCH 4/5] firmware_loader: Add firmware-upload support

Quoted 305 lines · Luis Chamberlain
Quoted 304 lines · Russ Weight

Extend the firmware subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images provided by the user at his/her convenience.

A device driver may call fw_upload_register() to expose persistent "loading" and "data" sysfs files. These files are used in the same way as the fallback sysfs "loading" and "data" files. When 0 is written to "loading" to complete the write of firmware data, the data is transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.

---
 .../ABI/testing/sysfs-class-firmware          |  32 +++
 .../driver-api/firmware/fw_upload.rst         |  86 +++++++
 Documentation/driver-api/firmware/index.rst   |   1 +
 drivers/base/firmware_loader/Kconfig          |  14 ++
 drivers/base/firmware_loader/Makefile         |   1 +
 drivers/base/firmware_loader/firmware.h       |   6 +
 drivers/base/firmware_loader/fw_sysfs.c       |  50 +++-
 drivers/base/firmware_loader/fw_sysfs.h       |   4 +
 drivers/base/firmware_loader/fw_upload.c      | 229 ++++++++++++++++++
 drivers/base/firmware_loader/fw_upload.h      |  24 ++
 drivers/base/firmware_loader/main.c           |  16 +-
 include/linux/firmware.h                      |  72 ++++++
 12 files changed, 523 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-firmware
 create mode 100644 Documentation/driver-api/firmware/fw_upload.rst
 create mode 100644 drivers/base/firmware_loader/fw_upload.c
 create mode 100644 drivers/base/firmware_loader/fw_upload.h
Documentation/ABI/testing/sysfs-class-firmware new file +32 -0
new file mode 100644
index 000000000000..a2e518f0bf8a
@@ -0,0 +1,32 @@
+What: 		/sys/class/firmware/.../data
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	The data sysfs file is used for firmware-fallback and for
+		firmware uploads. Cat a firmware image to this sysfs file
+		after you echo 1 to the loading sysfs file. When the firmware
+		image write is complete, echo 0 to the loading sysfs file. This
+		sequence will signal the completion of the firmware write and
+		signal the lower-level driver that the firmware data is
+		available.
+
+What: 		/sys/class/firmware/.../loading
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	The loading sysfs file is used for both firmware-fallback and
+		for firmware uploads. Echo 1 onto the loading file to indicate
+		you are writing a firmware file to the data sysfs node. Echo
+		-1 onto this file to abort the data write or echo 0 onto this
+		file to indicate that the write is complete. For firmware
+		uploads, the zero value also triggers the transfer of the
+		firmware data to the lower-level device driver.
+
+What: 		/sys/class/firmware/.../timeout
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	This file supports the timeout mechanism for firmware
+		fallback.  This file has no affect on firmware uploads. For
+		more information on timeouts please see the documentation
+		for firmware fallback.
Documentation/driver-api/firmware/fw_upload.rst new file +86 -0
new file mode 100644
index 000000000000..bf272f627a1f
@@ -0,0 +1,86 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=============
+fw_upload API
+=============
+
+A device driver that registers with the firmware loader will expose
+persistent sysfs nodes to enable users to initiate firmware updates for
+that device.  It is the responsibility of the device driver and/or the
+device itself to perform any validation on the data received. Firmware
+upload uses the same *loading* and *data* sysfs files described in the
+documentation for firmware fallback.
+
+Register for firmware upload
+============================
+
+A device driver registers for firmware upload by calling fw_upload_register().
+Among the parameter list is a name to identify the device under
+/sys/class/firmware. A user may initiate a firmware upload by echoing
+a 1 to the *loading* sysfs file for the target device. Next, the user writes
+the firmware image to the *data* sysfs file. After writing the firmware
+data, the user echos 0 to the *loading* sysfs file to signal completion.
+Echoing 0 to *loading* also triggers the transfer of the firmware to the
+lower-lever device driver in the context of a kernel worker thread.
+
+To use the fw_upload API, write a driver that implements a set of ops. The
+probe function calls fw_upload_register() and the remove function calls
+fw_upload_unregister() such as::
+
+	static const struct fw_upload_ops m10bmc_ops = {
+		.prepare = m10bmc_sec_prepare,
+		.write = m10bmc_sec_write,
+		.poll_complete = m10bmc_sec_poll_complete,
+		.cancel = m10bmc_sec_cancel,
+		.cleanup = m10bmc_sec_cleanup,
+	};
+
+	static int m10bmc_sec_probe(struct platform_device *pdev)
+	{
+		const char *fw_name, *truncate;
+		struct m10bmc_sec *sec;
+		struct fw_upload *fwl;
+		unsigned int len;
+
+		sec = devm_kzalloc(&pdev->dev, sizeof(*sec), GFP_KERNEL);
+		if (!sec)
+			return -ENOMEM;
+
+		sec->dev = &pdev->dev;
+		sec->m10bmc = dev_get_drvdata(pdev->dev.parent);
+		dev_set_drvdata(&pdev->dev, sec);
+
+		fw_name = dev_name(sec->dev);
+		truncate = strstr(fw_name, ".auto");
+		len = (truncate) ? truncate - fw_name : strlen(fw_name);
+		sec->fw_name = kmemdup_nul(fw_name, len, GFP_KERNEL);
+
+		fwl = fw_upload_register(sec->dev, sec->fw_name, &m10bmc_ops, sec);
+		if (IS_ERR(fwl)) {
+			dev_err(sec->dev, "Firmware Upload driver failed to start\n");
+			kfree(sec->fw_name);
+			return PTR_ERR(fwl);
+		}
+
+		sec->fwl = fwl;
+		return 0;
+	}
+
+	static int m10bmc_sec_remove(struct platform_device *pdev)
+	{
+		struct m10bmc_sec *sec = dev_get_drvdata(&pdev->dev);
+
+		fw_upload_unregister(sec->fwl);
+		kfree(sec->fw_name);
+		return 0;
+	}
+
+fw_upload_register
+------------------
+.. kernel-doc:: drivers/base/firmware_loader/fw_upload.c
+   :functions: fw_upload_register
+
+fw_upload_unregister
+--------------------
+.. kernel-doc:: drivers/base/firmware_loader/fw_upload.c
+   :functions: fw_upload_unregister
Documentation/driver-api/firmware/index.rst +1 -0
index 57415d657173..9d2c19dc8e36 100644
@@ -8,6 +8,7 @@ Linux Firmware API
    core
    efi/index
    request_firmware
+   fw_upload
    other_interfaces
 
 .. only::  subproject and html
drivers/base/firmware_loader/Kconfig +14 -0
index 1bfe18900ed5..cee662f3277b 100644
@@ -185,5 +185,19 @@ config FW_CACHE
 
 	  If unsure, say Y.
 
+config FW_UPLOAD
+	bool "Enable users to initiate firmware updates using sysfs"
+	select FW_LOADER_SYSFS
+	select FW_LOADER_PAGED_BUF
+	help
+	  Enabling this option will allow device drivers to expose a persistent
+	  sysfs interface that allows firmware updates to be initiated from
+	  userspace. For example, FPGA based PCIe cards load firmware and FPGA
+	  images from local FLASH when the card boots. The images in FLASH may
+	  be updated with new images provided by the user. Enable this device
+	  to support cards that rely on user-initiated updates for firmware files.
+
+	  If unsure, say N.
+
 endif # FW_LOADER
 endmenu
drivers/base/firmware_loader/Makefile +1 -0
index 787c833d0c6e..52ef64bd9357 100644
@@ -7,5 +7,6 @@ firmware_class-objs := main.o
 firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) += fallback.o
 firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += fallback_platform.o
 firmware_class-$(CONFIG_FW_LOADER_SYSFS) += fw_sysfs.o
+firmware_class-$(CONFIG_FW_UPLOAD) += fw_upload.o
 
 obj-y += builtin/
drivers/base/firmware_loader/firmware.h +6 -0
index 58768d16f8df..4019f9423de8 100644
@@ -87,6 +87,7 @@ struct fw_priv {
 };
 
 extern struct mutex fw_lock;
+extern struct firmware_cache fw_cache;
 
 static inline bool __fw_state_check(struct fw_priv *fw_priv,
 				    enum fw_status status)
@@ -154,7 +155,12 @@ static inline bool fw_state_is_done(struct fw_priv *fw_priv)
 	return __fw_state_check(fw_priv, FW_STATUS_DONE);
 }
 
+int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc,
+			 struct fw_priv **fw_priv, void *dbuf, size_t size,
+			 size_t offset, u32 opt_flags);
 int assign_fw(struct firmware *fw, struct device *device);
+void free_fw_priv(struct fw_priv *fw_priv);
+void fw_state_init(struct fw_priv *fw_priv);
 
 #ifdef CONFIG_FW_LOADER
 bool firmware_is_builtin(const struct firmware *fw);
drivers/base/firmware_loader/fw_sysfs.c +38 -1
index 70cb1d67ffb2..9b0cd37c81df 100644
@@ -6,8 +6,8 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#include "firmware.h"
 #include "fw_sysfs.h"
+#include "fw_upload.h"
 
 /*
  * sysfs support for firmware loader
@@ -80,6 +80,10 @@ static void fw_dev_release(struct device *dev)
 {
 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
 
+	if (fw_sysfs->fw_upload_priv) {
+		free_fw_priv(fw_sysfs->fw_priv);
+		kfree(fw_sysfs->fw_upload_priv);
+	}
 	kfree(fw_sysfs);
 }
 
@@ -165,6 +169,9 @@ static ssize_t firmware_loading_store(struct device *dev,
 				      const char *buf, size_t count)
 {
 	struct fw_sysfs *fw_sysfs = to_fw_sysfs(dev);
+#ifdef CONFIG_FW_UPLOAD
+	struct fw_upload_priv *fwlp;
+#endif
 	struct fw_priv *fw_priv;
 	ssize_t written = count;
 	int loading = simple_strtol(buf, NULL, 10);
@@ -211,6 +218,42 @@ static ssize_t firmware_loading_store(struct device *dev,
 				written = rc;
 			} else {
 				fw_state_done(fw_priv);
+
+#ifdef CONFIG_FW_UPLOAD
+				/*
+				 * For fw_uploads, start a worker thread to upload
+				 * data to the parent driver.
+				 */
+				if (!fw_sysfs->fw_upload_priv)
+					break;
+
+				if (!fw_priv->size) {
+					fw_free_paged_buf(fw_priv);
+					fw_state_init(fw_sysfs->fw_priv);
+					break;
+				}
+
+				fwlp = fw_sysfs->fw_upload_priv;
+				mutex_lock(&fwlp->lock);
+
+				/* Do not interfere an on-going fw_upload */
+				if (fwlp->progress != FW_UPLOAD_PROG_IDLE) {
+					mutex_unlock(&fwlp->lock);
+					written = -EBUSY;
+					goto out;
+				}
+
+				fwlp->progress = FW_UPLOAD_PROG_RECEIVING;
+				fwlp->err_code = 0;
+				fwlp->remaining_size = fw_priv->size;
+				fwlp->data = fw_priv->data;
+				pr_debug("%s: fw-%s fw_priv=%p data=%p size=%u\n",
Quoted 8 lines

drivers/base/firmware_loader/fw_sysfs.c static ssize_t firmware_loading_store(struct device *dev, · line 256

Quoted 6 lines
drivers/base/firmware_loader/fw_sysfs.c +6 -0
+					 __func__, fw_priv->fw_name,
+					 fw_priv, fw_priv->data,
+					 (unsigned int)fw_priv->size);
+				queue_work(system_long_wq, &fwlp->work);
+				mutex_unlock(&fwlp->lock);
+#endif

If you're going to split this work up it'd be nice to avoid to have #ifdef stuff here.

Yes - I'll encapsulate the code into another function and move it to the sysfs-upload.c file.

Quoted 3 lines

drivers/base/firmware_loader/fw_upload.c

Quoted 2 lines
drivers/base/firmware_loader/fw_upload.c +0 -0

No need for fw prefix, sysfs-upload.[ch] would make it clearer.

OK - I'll switch to sysfs-upload.[ch]

Quoted 6 lines
Quoted 2 lines

+EXPORT_SYMBOL_GPL(fw_upload_register);

firmware_upload_register()

Quoted 1 line

+EXPORT_SYMBOL_GPL(fw_upload_unregister);

firmware_upload_unregister()

Sure - I'll change the register/unregister function names.

Quoted 10 lines
Quoted 9 lines
include/linux/firmware.h +0 -0
index 3b057dfc8284..9b109f8ff627 100644
@@ -17,6 +17,56 @@ struct firmware {
 	void *priv;
 };
 
Quoted 8 lines

include/linux/firmware.h struct firmware { · line 26

Quoted 7 lines
include/linux/firmware.h +7 -0
+/* Update progress codes */
+#define FW_UPLOAD_PROG_IDLE		0
+#define FW_UPLOAD_PROG_RECEIVING	1
+#define FW_UPLOAD_PROG_PREPARING	2
+#define FW_UPLOAD_PROG_TRANSFERRING	3
+#define FW_UPLOAD_PROG_PROGRAMMING	4
+#define FW_UPLOAD_PROG_MAX		5

enums with kdoc allows you to nicely document these as well.

OK. I'll switch to enums. I also realised that the FW_UPLOAD_PROG* constants can be defined in a smaller scope. I'll move these to sysfs-upload.h.

Quoted 11 lines

include/linux/firmware.h struct firmware { · line 36

Quoted 10 lines
include/linux/firmware.h +10 -0
+
+/* Update error progress codes */
+#define FW_UPLOAD_ERR_HW_ERROR		1
+#define FW_UPLOAD_ERR_TIMEOUT		2
+#define FW_UPLOAD_ERR_CANCELED		3
+#define FW_UPLOAD_ERR_BUSY		4
+#define FW_UPLOAD_ERR_INVALID_SIZE	5
+#define FW_UPLOAD_ERR_RW_ERROR		6
+#define FW_UPLOAD_ERR_WEAROUT		7
+#define FW_UPLOAD_ERR_MAX		8

enums with kdoc allows you to nicely document these as well.

Thanks,
- Russ

Quoted 2 lines

Luis

#12 lore raw

RFC PATCH 5/5 firmware_loader: Add sysfs nodes to monitor fw_upload

In reply to Russ Weight

Add additional sysfs nodes to monitor the transfer of firmware upload data to the target device:

cancel: Write 1 to cancel the data transfer
error: Display error status for a failed firmware upload
remaining_size: Display the remaining amount of data to be transferred
status: Display the progress of the firmware upload

---
 .../ABI/testing/sysfs-class-firmware          |  45 +++++++
 .../driver-api/firmware/fw_upload.rst         |  19 ++-
 drivers/base/firmware_loader/fw_sysfs.c       |   9 ++
 drivers/base/firmware_loader/fw_upload.c      | 119 ++++++++++++++++++
 drivers/base/firmware_loader/fw_upload.h      |   5 +
 5 files changed, 196 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-class-firmware +45 -0
index a2e518f0bf8a..5653cb2d6e23 100644
@@ -10,6 +10,30 @@ Description:	The data sysfs file is used for firmware-fallback and for
 		signal the lower-level driver that the firmware data is
 		available.
 
+What: 		/sys/class/firmware/.../cancel
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	Write-only. For firmware uploads, write a "1" to this file to
+		request that the transfer of firmware data to the lower-level
+		device be canceled. This request will be rejected (EBUSY) if
+		the update cannot be canceled (e.g. a FLASH write is in
+		progress) or (ENODEV) if there is no firmware update in progress.
+
+What: 		/sys/class/firmware/.../error
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	Read-only. Returns a string describing a failed firmware
+		upload. This string will be in the form of <STATUS>:<ERROR>,
+		where <STATUS> will be one of the status strings described
+		for the status sysfs file and <ERROR> will be one of the
+		following: "hw-error", "timeout", "user-abort", "device-busy",
+		"invalid-file-size", "read-write-error", "flash-wearout". The
+		error sysfs file is only meaningful when the current firmware
+		upload status is "idle". If this file is read while a firmware
+		transfer is in progress, then the read will fail with EBUSY.
+
 What: 		/sys/class/firmware/.../loading
 Date:		Mar 2022
 KernelVersion:	5.18
@@ -22,6 +46,27 @@ Description:	The loading sysfs file is used for both firmware-fallback and
 		uploads, the zero value also triggers the transfer of the
 		firmware data to the lower-level device driver.
 
+What: 		/sys/class/firmware/.../remaining_size
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	Read-only. For firmware upload, this file contains the size
+		of the firmware data that remains to be transferred to the
+		lower-level device driver. The size value is initialized to
+		the full size of the firmware image that was previously
+		written to the data sysfs file. This value is periodically
+		updated during the "transferring" phase of the firmware
+		upload.
+		Format: "%u".
+
+What: 		/sys/class/firmware/.../status
+Date:		Mar 2022
+KernelVersion:	5.18
+Contact:	Russ Weight <[email protected]>
+Description:	Read-only. Returns a string describing the current status of
+		a firmware upload. The string will be one of the following:
+		idle, "receiving", "preparing", "transferring", "programming".
+
 What: 		/sys/class/firmware/.../timeout
 Date:		Mar 2022
 KernelVersion:	5.18
Documentation/driver-api/firmware/fw_upload.rst +18 -1
index bf272f627a1f..28720363ec5c 100644
@@ -9,7 +9,8 @@ persistent sysfs nodes to enable users to initiate firmware updates for
 that device.  It is the responsibility of the device driver and/or the
 device itself to perform any validation on the data received. Firmware
 upload uses the same *loading* and *data* sysfs files described in the
-documentation for firmware fallback.
+documentation for firmware fallback. It also adds additional sysfs files
+to provide status on the transfer of the firmware image to the device.
 
 Register for firmware upload
 ============================
@@ -84,3 +85,19 @@ fw_upload_unregister
 --------------------
 .. kernel-doc:: drivers/base/firmware_loader/fw_upload.c
    :functions: fw_upload_unregister
+
+Sysfs Attributes
+================
+
+In addition to the *loading* and *data* sysfs files, there are additional
+sysfs files to monitor the status of the data transfer to the target
+device and to determine the final pass/fail status of the transfer.
+Depending on the device and the size of the firmware image, a firmware
+update could take milliseconds or minutes.
+
+The additional sysfs files are:
+
+* status - provides an indication of the progress of a firmware update
+* error - provides error information for a failed firmware update
+* remaining_size - tracks the data transfer portion of an update
+* cancel - echo 1 to this file to cancel the update
drivers/base/firmware_loader/fw_sysfs.c +9 -0
index 9b0cd37c81df..2e47b5890470 100644
@@ -414,6 +414,12 @@ static struct bin_attribute firmware_attr_data = {
 
 static struct attribute *fw_dev_attrs[] = {
 	&dev_attr_loading.attr,
+#ifdef CONFIG_FW_UPLOAD
+	&dev_attr_cancel.attr,
+	&dev_attr_status.attr,
+	&dev_attr_error.attr,
+	&dev_attr_remaining_size.attr,
+#endif
 	NULL
 };
 
@@ -425,6 +431,9 @@ static struct bin_attribute *fw_dev_bin_attrs[] = {
 static const struct attribute_group fw_dev_attr_group = {
 	.attrs = fw_dev_attrs,
 	.bin_attrs = fw_dev_bin_attrs,
+#ifdef CONFIG_FW_UPLOAD
+	.is_visible = fw_upload_is_visible,
+#endif
 };
 
 static const struct attribute_group *fw_dev_attr_groups[] = {
drivers/base/firmware_loader/fw_upload.c +119 -0

125 lines not on this page · raw message

drivers/base/firmware_loader/fw_upload.h +5 -0
index c55ac4047d55..e7f13d41c177 100644
@@ -18,6 +18,11 @@ struct fw_upload_priv {
 	bool driver_unload;
 };
 
+extern struct device_attribute dev_attr_status;
+extern struct device_attribute dev_attr_error;
+extern struct device_attribute dev_attr_cancel;
+extern struct device_attribute dev_attr_remaining_size;
+
 umode_t
 fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n);
 
Signature · 1 line
2.25.1
maintainer
#13 lore raw
In reply to Russ Weight
Quoted 40 lines · Russ Weight

Extend the firmware loader subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards automatically load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images that are uploaded by the user.

A device driver may call fw_upload_register() to expose persistent "loading" and "data" sysfs files at /sys/class/firmare/<NAME>/*. These files are used in the same way as the fallback sysfs "loading" and "data" files. However, when 0 is written to "loading" to complete the write of firmware data, the data is also transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.

Additional sysfs nodes are added in the same location as "loading" and "data" to monitor the transfer of the image data to the device using callback functions provided by the lower-level device driver and to allow the data transfer to be cancelled.

Example usage:

$ pwd
/sys/class/firmware/n3000bmc-sec-update.8
$ ls
cancel  device  loading  remaining_size  subsystem
data    error   power    status          uevent
$ echo 1 > loading
$ cat /tmp/firmware.bin > data
$ echo 0 > loading
$ while :; do cat status; cat remaining_size ; sleep 3; done
preparing
44590080
<--snip-->
transferring
44459008
transferring
44311552
<--snip-->
<snip>
transferring
Quoted 8 lines
173056
<--snip-->
programming
0
<--snip-->
idle
0
^C

Nice. Please extend lib/test_firmware.c while at it and try to break and ensure your stuff is not regressed with future changes.

Luis

#14 lore raw
Quoted 49 lines · Luis Chamberlain
Quoted 40 lines · Russ Weight

Extend the firmware loader subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards automatically load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images that are uploaded by the user.

A device driver may call fw_upload_register() to expose persistent "loading" and "data" sysfs files at /sys/class/firmare/<NAME>/*. These files are used in the same way as the fallback sysfs "loading" and "data" files. However, when 0 is written to "loading" to complete the write of firmware data, the data is also transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread.

Additional sysfs nodes are added in the same location as "loading" and "data" to monitor the transfer of the image data to the device using callback functions provided by the lower-level device driver and to allow the data transfer to be cancelled.

Example usage:

$ pwd
/sys/class/firmware/n3000bmc-sec-update.8
$ ls
cancel  device  loading  remaining_size  subsystem
data    error   power    status          uevent
$ echo 1 > loading
$ cat /tmp/firmware.bin > data
$ echo 0 > loading
$ while :; do cat status; cat remaining_size ; sleep 3; done
preparing
44590080
<--snip-->
transferring
44459008
transferring
44311552
<--snip-->
<snip>
transferring
Quoted 8 lines
173056
<--snip-->
programming
0
<--snip-->
idle
0
^C
Quoted 2 lines

Nice. Please extend lib/test_firmware.c while at it and try to break and ensure your stuff is not regressed with future changes.

Yes. I'm looking at the test_firmware module and I'll add to it for the next patch submission.

Thanks,
- Russ

Quoted 2 lines

Luis