From: Ping-Ke Shih <pkshih@realtek.com>
To: "martin.blumenstingl@googlemail.com"
<martin.blumenstingl@googlemail.com>,
"linux-wireless@vger.kernel.org" <linux-wireless@vger.kernel.org>,
"David.Laight@ACULAB.COM" <David.Laight@ACULAB.COM>
Cc: "kvalo@kernel.org" <kvalo@kernel.org>,
"tehuang@realtek.com" <tehuang@realtek.com>,
"s.hauer@pengutronix.de" <s.hauer@pengutronix.de>,
"tony0620emma@gmail.com" <tony0620emma@gmail.com>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/4] rtw88: Add packed attribute to the eFuse structs
Date: Sun, 1 Jan 2023 13:08:48 +0000 [thread overview]
Message-ID: <5c0c77240e7ddfdffbd771ee7e50d36ef3af9c84.camel@realtek.com> (raw)
In-Reply-To: <a86893f11fe64930897473a38226a9a8@AcuMS.aculab.com>
On Sun, 2023-01-01 at 11:54 +0000, David Laight wrote:
> From: Ping-Ke Shih
> > Sent: 01 January 2023 11:42
> >
> > On Sat, 2022-12-31 at 16:57 +0000, David Laight wrote:
> > > From: Ping-Ke Shih
> > > > Sent: 29 December 2022 09:25
> > > >
> > > > > -----Original Message-----
> > > > > From: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > > > > Sent: Wednesday, December 28, 2022 9:36 PM
> > > > > To: linux-wireless@vger.kernel.org
> > > > > Cc: tony0620emma@gmail.com; kvalo@kernel.org; Ping-Ke Shih <pkshih@realtek.com>;
> > > > tehuang@realtek.com;
> > > > > s.hauer@pengutronix.de; netdev@vger.kernel.org; linux-kernel@vger.kernel.org; Martin
> > > > > Blumenstingl
> > > > > <martin.blumenstingl@googlemail.com>
> > > > > Subject: [PATCH 1/4] rtw88: Add packed attribute to the eFuse structs
> > > > >
> > > > > The eFuse definitions in the rtw88 are using structs to describe the
> > > > > eFuse contents. Add the packed attribute to all structs used for the
> > > > > eFuse description so the compiler doesn't add gaps or re-order
> > > > > attributes.
> > > > >
> > > > > Also change the type of the res2..res3 eFuse fields to u16 to avoid the
> > > > > following warning, now that their surrounding struct has the packed
> > > > > attribute:
> > > > > note: offset of packed bit-field 'res2' has changed in GCC 4.4
> > > > >
> > > > > Fixes: e3037485c68e ("rtw88: new Realtek 802.11ac driver")
> > > > > Fixes: ab0a031ecf29 ("rtw88: 8723d: Add read_efuse to recognize efuse info from map")
> > > > > Fixes: 769a29ce2af4 ("rtw88: 8821c: add basic functions")
> > > > > Fixes: 87caeef032fc ("wifi: rtw88: Add rtw8723du chipset support")
> > > > > Fixes: aff5ffd718de ("wifi: rtw88: Add rtw8821cu chipset support")
> > > > > Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > > > > ---
> > > > > drivers/net/wireless/realtek/rtw88/main.h | 6 +++---
> > > > > drivers/net/wireless/realtek/rtw88/rtw8723d.h | 6 +++---
> > > > > drivers/net/wireless/realtek/rtw88/rtw8821c.h | 20 +++++++++----------
> > > > > drivers/net/wireless/realtek/rtw88/rtw8822b.h | 20 +++++++++----------
> > > > > drivers/net/wireless/realtek/rtw88/rtw8822c.h | 20 +++++++++----------
> > > > > 5 files changed, 36 insertions(+), 36 deletions(-)
> > > > >
> > > >
> > > > [...]
> > > >
> > > > > @@ -43,13 +43,13 @@ struct rtw8821ce_efuse {
> > > > > u8 link_cap[4];
> > > > > u8 link_control[2];
> > > > > u8 serial_number[8];
> > > > > - u8 res0:2; /* 0xf4 */
> > > > > - u8 ltr_en:1;
> > > > > - u8 res1:2;
> > > > > - u8 obff:2;
> > > > > - u8 res2:3;
> > > > > - u8 obff_cap:2;
> > > > > - u8 res3:4;
> > > > > + u16 res0:2; /* 0xf4 */
> > > > > + u16 ltr_en:1;
> > > > > + u16 res1:2;
> > > > > + u16 obff:2;
> > > > > + u16 res2:3;
> > > > > + u16 obff_cap:2;
> > > > > + u16 res3:4;
> > > >
> > > > These should be __le16. Though bit fields are suitable to efuse layout,
> > > > we don't access these fields for now. It would be well.
> >
> > Uh. I typo the sentence. Originally, I would like to type
> > "...bit fields are NOT suitable...".
> >
> > In this driver, efuse is read into a u8 array, and cast to this struct
> > pointer to access the field.
>
> Then define it as such.
> The 16bit endianness and bit-order dependant bitfields serve
> no purpose.
>
> > > IIRC the assignment of actual bits to bit-fields is (at best)
> > > architecturally defined - so isn't really suitable for anything
> > > where the bits have to match a portable memory buffer.
> > > The bit allocation isn't tied to the byte endianness.
> >
> > Yes, this kind of struct has endian problem. Fortunately, we don't
> > actually access values via bit fields.
> >
> > > To get an explicit layout you have to do explicit masking.
> >
> > If we actually want to access these values, we will define masks
> > and use {u8,_le16,le32}_get_bits() or bare '&' bit operation to access
> > them.
>
> But you can't take the address of bitfield members.
> Define the data properly.
Yes, it should not use bit filed. Instead, use a __le16 for all fields, such as
struct rtw8821ce_efuse {
...
__le16 caps;
...
}
#define CAPS_RES0 GENMASK(1, 0)
#define CAPS_LTR_EN BIT(2)
#define CAPS_RES1 GENMASK(4, 3)
#define CAPS_OBFF GENMASK(6, 5)
...
Assume the pointer of efuse content is 'const u8 *efuse_raw;'
const struct rtw8821ce_efuse *efuse = (const struct rtw8821ce_efuse *)efuse_raw;
Then, get ltr_en
ltr_en = le16_get_bits(efuse->caps, CAPS_LTR_EN);
>
> > > You also don't need __packed unless the 'natural' alignment
> > > of fields would need gaps or the actual structure itself might
> > > be misaligned in memory.
> > > While C compilers are allowed to add arbitrary padding the Linux kernel
> > > requires that they don't.
> > > I'm also pretty sure that compilers are not allowed to reorder fields.
> > >
> > > Specifying __packed can add considerable run-time (and code size)
> > > overhead on some architectures - it should only be used if actually
> > > needed.
> > >
> >
> > Understood. We only add __packed to the struct which is used to
> > access predefined format, like efuse content defined by vendor.
>
> No - that doesn't mean you need to use __packed.
> It does mean that you shouldn't use bitfields.
> Look at all the hardware drivers, they use structs to map device
> registers and absolutely require the compile not add padding.
>
I think the original struct has two problem -- endian and __packed.
I mentioned endian part above.
Taking below as example to explain why I need __packed,
struct rtw8821ce_efuse {
...
u8 data1; // offset 0x100
__le16 data2; // offset 0x101-0x102
...
} __packed;
Without __packed, compiler could has pad between data1 and data2,
and then get wrong result.
In this patch, struct isn't to map registers. Instead it is used to
access efuse content of a u8 array existing in memory.
Ping-Ke
next prev parent reply other threads:[~2023-01-01 13:11 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-28 13:35 [PATCH 0/4] rtw88: Four fixes found while working on SDIO support Martin Blumenstingl
2022-12-28 13:35 ` [PATCH 1/4] rtw88: Add packed attribute to the eFuse structs Martin Blumenstingl
2022-12-29 9:24 ` Ping-Ke Shih
2022-12-29 10:37 ` Martin Blumenstingl
2022-12-29 11:35 ` Ping-Ke Shih
2022-12-31 16:57 ` David Laight
2023-01-01 11:42 ` Ping-Ke Shih
2023-01-01 11:54 ` David Laight
2023-01-01 13:08 ` Ping-Ke Shih [this message]
2023-01-04 15:30 ` Martin Blumenstingl
2023-01-04 15:53 ` David Laight
2023-01-04 16:07 ` Martin Blumenstingl
2023-01-04 16:31 ` David Laight
2023-01-04 17:49 ` Martin Blumenstingl
2023-01-05 0:56 ` Ping-Ke Shih
2023-01-05 8:34 ` David Laight
2023-01-10 12:02 ` Kalle Valo
2023-01-10 12:34 ` David Laight
2022-12-28 13:35 ` [PATCH 2/4] rtw88: Configure the registers from rtw_bf_assoc() outside the RCU lock Martin Blumenstingl
2022-12-29 9:37 ` Ping-Ke Shih
2022-12-28 13:35 ` [PATCH 3/4] rtw88: Use rtw_iterate_vifs() for rtw_vif_watch_dog_iter() Martin Blumenstingl
2022-12-29 9:39 ` Ping-Ke Shih
2022-12-28 13:35 ` [PATCH 4/4] rtw88: Use non-atomic rtw_iterate_stas() in rtw_ra_mask_info_update() Martin Blumenstingl
2022-12-29 9:39 ` Ping-Ke Shih
2022-12-29 9:26 ` [PATCH 0/4] rtw88: Four fixes found while working on SDIO support Ping-Ke Shih
2022-12-29 10:40 ` Martin Blumenstingl
2022-12-29 11:42 ` Ping-Ke Shih
2023-01-10 12:06 ` Kalle Valo
2022-12-29 12:48 Martin Blumenstingl
2022-12-29 12:48 ` [PATCH 1/4] rtw88: Add packed attribute to the eFuse structs Martin Blumenstingl
2022-12-29 23:47 ` Ping-Ke Shih
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5c0c77240e7ddfdffbd771ee7e50d36ef3af9c84.camel@realtek.com \
--to=pkshih@realtek.com \
--cc=David.Laight@ACULAB.COM \
--cc=kvalo@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=martin.blumenstingl@googlemail.com \
--cc=netdev@vger.kernel.org \
--cc=s.hauer@pengutronix.de \
--cc=tehuang@realtek.com \
--cc=tony0620emma@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®