From b644bc75234b19d54b606769895030752fa3008e Mon Sep 17 00:00:00 2001
From: Anders Roxell <anders.roxell@linaro.org>
Date: Wed, 11 Jun 2025 12:31:37 +0200
Subject: [PATCH] wifi: iwlwifi: pcie: ensure RX_QUEUE_CB_SIZE fits bitfield
 for gcc-8|9

GCC-8 and GCC-9 emits a hard error when the value passed to
`u32_encode_bits()` does not fit within the specified bitmask, even in
cases where the value may be masked or valid at runtime. This results in
build failures for older toolchains:

error: call to '__field_overflow' declared with attribute error: value
doesn't fit into mask

The problematic code encodes RX_QUEUE_CB_SIZE() into the
IWL_CTXT_INFO_RB_CB_SIZE bitfield. While newer compilers may tolerate
this, GCC 8 is stricter and fails compilation.

Use FIELD_PREP() to ensure the value fits in the mask and avoid the
overflow error on older compilers.

Fixes: b8eee90f0ba5 ("wifi: iwlwifi: cfg: unify num_rbds config")
Link: https://lore.kernel.org/all/CA+G9fYssasMnOE36xLH5m7ky4fKxbzN7kX5mEE7icnuu+0hGuQ@mail.gmail.com/
Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 drivers/net/wireless/intel/iwlwifi/pcie/ctxt-info.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wireless/intel/iwlwifi/pcie/ctxt-info.c b/drivers/net/wireless/intel/iwlwifi/pcie/ctxt-info.c
index cb36baac14da..7aab52f8c746 100644
--- a/drivers/net/wireless/intel/iwlwifi/pcie/ctxt-info.c
+++ b/drivers/net/wireless/intel/iwlwifi/pcie/ctxt-info.c
@@ -204,9 +204,9 @@ int iwl_pcie_ctxt_info_init(struct iwl_trans *trans,
 
 	WARN_ON(RX_QUEUE_CB_SIZE(iwl_trans_get_num_rbds(trans)) > 12);
 	control_flags = IWL_CTXT_INFO_TFD_FORMAT_LONG;
-	control_flags |=
-		u32_encode_bits(RX_QUEUE_CB_SIZE(iwl_trans_get_num_rbds(trans)),
-				IWL_CTXT_INFO_RB_CB_SIZE);
+	/* This should just be u32_encode_bits() but gcc-8 and gcc-9 fail to build */
+	control_flags |= FIELD_PREP(IWL_CTXT_INFO_RB_CB_SIZE,
+		RX_QUEUE_CB_SIZE(iwl_trans_get_num_rbds(trans)) & FIELD_MAX(IWL_CTXT_INFO_RB_CB_SIZE));
 	control_flags |= u32_encode_bits(rb_size, IWL_CTXT_INFO_RB_SIZE);
 	ctxt_info->control.control_flags = cpu_to_le32(control_flags);
 
-- 
2.47.2

