Received: by 2002:ab2:69cc:0:b0:1fd:c486:4f03 with SMTP id n12csp171452lqp; Tue, 11 Jun 2024 00:13:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXUJfnJVPWEd5y4ADouhmHw341DslF7dBEZCgz3GYdI/0e1y7mhDWkWRJtTArq8rSm9mVlqMJ9UZNvvLTgfASUV72uvIPQrOyHkm70yBA== X-Google-Smtp-Source: AGHT+IGnhvQlDXUZmtkaHiI1NU4JvZdTQ+JjYnt4KoJIMwrvc/jvQ9MCy43OM0ETx4/A3tadZ8UZ X-Received: by 2002:a17:902:a3c8:b0:1f6:3580:65c9 with SMTP id d9443c01a7336-1f728901fdfmr17603135ad.26.1718090018589; Tue, 11 Jun 2024 00:13:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718090018; cv=pass; d=google.com; s=arc-20160816; b=sK4KP0f59jeB1b7dfaaEaIHFydlmI0xD92DNnw0NRUznj5DJ8X9IKxfArfYY9ZGoyE 6bSDfDBikbiAytYLUjIM9MDkAZ8LKhc3+Re6bGKk//SIOWM0GSIdLWDAh0vcKg1GFajg Wy+4mEwnzjfnd4ehJdSbrBxSHvfOfbMFJI7Eu4DmYZctHZsniKKpKCg433gPHf3jm3RB ErGYbi56npQHyQB/HAK+b/CH95nTSY/cukQkFUzN0U2gmedBJpgzIgN5P7YfmPz8+Niw b1imzqEp5HuBO01Nf5rJMhgq+Qm8CRSM6feZ5k63emh6OkMsI/knFRCU5eUKvIxA2Lw3 Ahdg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=eAa8r2NgIqDk/11GMdYmKz23EZYhsJDkyMKNm747VBs=; fh=vRvFeoKyg7HJXosTDvl9sics6kqWEh4mI4ltFPxQxfE=; b=D8ZjwsdKPwjVjLGbEtmf82xjRMwtdbaJlYxW45AKF2WNONadphgt87sdVHwLkrnY9p +Mo9zNFfXFwYOjsCvStJ7HrYrmwnfb3ndxHsamv7o2hBYtNvyRmrDj+TS3MgWtvyfYoh 2rwzvh68Tr/yy0oz75yJUQhdoqmgCKzrNBzMtwuj+1XlUEwK9n84PVEDNZaGOqdlM8rk Ku+qquCWelbUgKBrDITQ09AGIa97Tw6IYZ9wXiLrj5e5gxS2Ud11IVsiM3oGGprBu27X SygOPYoysQLXQKyHX/NubN7CqUJuN/FfX9TlTHKmaC9iDrZyLBjxOvGCQw+95VQd4xIz gHtw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0JPPfuGX; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-209327-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209327-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f7392de620si1829015ad.493.2024.06.11.00.13.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 00:13:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-209327-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0JPPfuGX; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-209327-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209327-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 28024B287B8 for ; Tue, 11 Jun 2024 06:32:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11D9D171E4C; Tue, 11 Jun 2024 06:31:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="0JPPfuGX" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23F5A13F43E; Tue, 11 Jun 2024 06:31:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718087510; cv=none; b=anKaPje5ltc9Afwpp08kMI4mh7rC55nvugidmJxXqreNpgXUMQ9Y/YxzBthpMgP6/x6MF3PNSFV3XDW/orH++5Ws8DsCwS1U8f+6ZAhz+qMsVrzid5SMgrCNJXlfLsndZZ5cqKhcj+lSJT40KW8zopOgGkPnzO+3qRjAL2Qsut0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718087510; c=relaxed/simple; bh=2ZPT3S+8HRYZ+/M0fqX0hFmUPpAO06wEJb3duJQkoAo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PclHaKvNWwoYsdycEaaPUxteTty+JiaYoj7G7sVYL2Y1RnmbeOK21mkUV/wllPqGau1vvgbJR4B4WEoVJ3GvtgvNHvSs1y6L2SIxVHqOmIZHVQy7P1EOPU4oMVKquihc5RUOihD5XUiyZHUkxdvsPyObW3RAV5jZRtDsjwlIhEw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=0JPPfuGX; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F72BC2BD10; Tue, 11 Jun 2024 06:31:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1718087509; bh=2ZPT3S+8HRYZ+/M0fqX0hFmUPpAO06wEJb3duJQkoAo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=0JPPfuGXCrZ6lXFH3MNQIVzydNYC1Ez7pLYaDlhmGhBeO/gQR1uq6ZpiIXlVW1wLP q9KJ+LIe/vdCP1qGQllOpuwHaFTakhpNIBKfjpoGkbscZ04wjb0Updauu86oFlq54B 95DRMaFmFRTTD1b9Il4u9ZK7C1TpnQdnL9brKK1M= Date: Tue, 11 Jun 2024 08:31:46 +0200 From: Greg KH To: Danilo Krummrich Cc: rafael@kernel.org, mcgrof@kernel.org, russell.h.weight@intel.com, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, airlied@gmail.com, fujita.tomonori@gmail.com, pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] rust: add firmware abstractions Message-ID: <2024061128-provolone-coyness-1d3c@gregkh> References: <20240610180318.72152-1-dakr@redhat.com> <20240610180318.72152-3-dakr@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240610180318.72152-3-dakr@redhat.com> On Mon, Jun 10, 2024 at 08:02:28PM +0200, Danilo Krummrich wrote: > Add an abstraction around the kernels firmware API to request firmware > images. The abstraction provides functions to access the firmware's size > and backing buffer. > > The firmware is released once the abstraction instance is dropped. > > Signed-off-by: Danilo Krummrich > --- > rust/bindings/bindings_helper.h | 1 + > rust/kernel/firmware.rs | 107 ++++++++++++++++++++++++++++++++ > rust/kernel/lib.rs | 1 + > 3 files changed, 109 insertions(+) > create mode 100644 rust/kernel/firmware.rs > > diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h > index ddb5644d4fd9..18a3f05115cb 100644 > --- a/rust/bindings/bindings_helper.h > +++ b/rust/bindings/bindings_helper.h > @@ -9,6 +9,7 @@ > #include > #include > #include > +#include > #include > #include > #include > diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs > new file mode 100644 > index 000000000000..7ff4c325f670 > --- /dev/null > +++ b/rust/kernel/firmware.rs > @@ -0,0 +1,107 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Firmware abstraction > +//! > +//! C header: [`include/linux/firmware.h`](srctree/include/linux/firmware.h") > + > +use crate::{bindings, device::Device, error::Error, error::Result, str::CStr}; > +use core::ptr::NonNull; > + > +// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`, > +// `firmware_request_platform`, `bindings::request_firmware_direct` > +type FwFunc = > + unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32; > + > +/// Abstraction around a C `struct firmware`. > +/// > +/// This is a simple abstraction around the C firmware API. Just like with the C API, firmware can > +/// be requested. Once requested the abstraction provides direct access to the firmware buffer as > +/// `&[u8]`. The firmware is released once [`Firmware`] is dropped. > +/// > +/// # Invariants > +/// > +/// The pointer is valid, and has ownership over the instance of `struct firmware`. > +/// > +/// # Examples > +/// > +/// ``` > +/// use kernel::firmware::Firmware; > +/// > +/// let fw = Firmware::request("path/to/firmware.bin", dev.as_ref())?; > +/// driver_load_firmware(fw.data()); > +/// ``` > +pub struct Firmware(NonNull); > + > +impl Firmware { > + fn request_internal(name: &CStr, dev: &Device, func: FwFunc) -> Result { > + let mut fw: *mut bindings::firmware = core::ptr::null_mut(); > + let pfw: *mut *mut bindings::firmware = &mut fw; > + > + // SAFETY: `pfw` is a valid pointer to a NULL initialized `bindings::firmware` pointer. > + // `name` and `dev` are valid as by their type invariants. > + let ret = unsafe { func(pfw as _, name.as_char_ptr(), dev.as_raw()) }; > + if ret != 0 { > + return Err(Error::from_errno(ret)); > + } > + > + // SAFETY: `func` not bailing out with a non-zero error code, guarantees that `fw` is a > + // valid pointer to `bindings::firmware`. > + Ok(Firmware(unsafe { NonNull::new_unchecked(fw) })) > + } > + > + /// Send a firmware request and wait for it. See also `bindings::request_firmware`. > + pub fn request(name: &CStr, dev: &Device) -> Result { > + Self::request_internal(name, dev, bindings::request_firmware) > + } How does this handle when CONFIG_FW_LOADER is not enabled? Why are you building these bindings if that option is not checked? > + > + /// Send a request for an optional firmware module. See also > + /// `bindings::firmware_request_nowarn`. > + pub fn request_nowarn(name: &CStr, dev: &Device) -> Result { > + Self::request_internal(name, dev, bindings::firmware_request_nowarn) > + } > + > + /// Send a request for a firmware with platform-fw fallback. See also > + /// `bindings::firmware_request_platform`. > + pub fn request_platform(name: &CStr, dev: &Device) -> Result { > + Self::request_internal(name, dev, bindings::firmware_request_platform) > + } > + > + /// Send a request for a firmware directly without usermode helper. See also > + /// `bindings::request_firmware_direct`. > + pub fn request_direct(name: &CStr, dev: &Device) -> Result { > + Self::request_internal(name, dev, bindings::request_firmware_direct) > + } Why just these variants? Why not just add the ones that people actually need instead of a random assortment like you choose here :) thanks, greg k-h