soc: qcom: extend interface for big endian support
Subsystems (2)
v4 cover soc: qcom: extend interface for big endian support
Currently, the QMI interface only works on little endian systems due to how it encodes and decodes data. Most QMI related data structures are defined in CPU native order and do not use endian specific types.
Add support for endian conversion of basic element types in the QMI encoding and decoding logic. Fix the handling of QMI_DATA_LEN fields to ensure correct interpretation on big endian systems. These changes are required to allow QMI to operate correctly across architectures with different endianness.
---
Changes in v4:
- Rebase on latest ath master.
- Drop ath12k patch to make a standalone one.
- Remove Fixes: tags as big endian is not yet supported.
Changes in v3:
- Rebase on latest ath master.
- Using a generic encoding/decoding macro causes sparse to complain about type violations. Use separate macros for each basic element type instead.
Changes in v2:
- Handle QMI conversion within the QMI subsystem instead of the driver.
Alexander Wilhelm (3):
soc: qcom: introduce new QMI encode/decode macros
soc: qcom: fix QMI encoding/decoding for basic elements
soc: qcom: preserve CPU endianness for QMI_DATA_LEN
drivers/soc/qcom/qmi_encdec.c | 101 ++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 17 deletions(-)
base-commit: 38cf754c15eeb0d80fbf52c933da10edb33d7906
Signature · 1 line
2.43.0
PATCH RESEND v4 1/3 soc: qcom: introduce new QMI encode/decode macros
Introduce new QMI encode/decode macros for each basic element type. It allows us to further distribute and handle endianness conversion of basic element types of different size.
--- drivers/soc/qcom/qmi_encdec.c | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
drivers/soc/qcom/qmi_encdec.c +56 -0
@@ -30,6 +30,34 @@ do { \ p_src = (u8 *)p_src + size; \ } while (0) +#define QMI_ENCDEC_ENCODE_U8 (p_dst, p_src) \ +do { \ + memcpy(p_dst, p_src, sizeof(u8)); \ + p_dst = (u8 *)p_dst + sizeof(u8); \ + p_src = (u8 *)p_src + sizeof(u8); \ +} while (0) + +#define QMI_ENCDEC_ENCODE_U16 (p_dst, p_src) \ +do { \ + *(__le16 *)p_dst = __cpu_to_le16(*(u16 *)p_src); \ + p_dst = (u8 *)p_dst + sizeof(u16); \ + p_src = (u8 *)p_src + sizeof(u16); \ +} while (0) + +#define QMI_ENCDEC_ENCODE_U32 (p_dst, p_src) \ +do { \ + *(__le32 *)p_dst = __cpu_to_le32(*(u32 *)p_src); \ + p_dst = (u8 *)p_dst + sizeof(u32); \ + p_src = (u8 *)p_src + sizeof(u32); \ +} while (0) + +#define QMI_ENCDEC_ENCODE_U64 (p_dst, p_src) \ +do { \ + *(__le64 *)p_dst = __cpu_to_le64(*(u64 *)p_src); \ + p_dst = (u8 *)p_dst + sizeof(u64); \ + p_src = (u8 *)p_src + sizeof(u64); \ +} while (0) +#define QMI_ENCDEC_DECODE_N_BYTES (p_dst, p_src, size) \ do { \ memcpy(p_dst, p_src, size); \
@@ -37,6 +65,34 @@ do { \ p_src = (u8 *)p_src + size; \ } while (0) +#define QMI_ENCDEC_DECODE_U8 (p_dst, p_src) \ +do { \ + memcpy(p_dst, p_src, sizeof(u8)); \ + p_dst = (u8 *)p_dst + sizeof(u8); \ + p_src = (u8 *)p_src + sizeof(u8); \ +} while (0) + +#define QMI_ENCDEC_DECODE_U16 (p_dst, p_src) \ +do { \ + *(u16 *)p_dst = __le16_to_cpu(*(__le16 *)p_src); \ + p_dst = (u8 *)p_dst + sizeof(u16); \ + p_src = (u8 *)p_src + sizeof(u16); \ +} while (0) + +#define QMI_ENCDEC_DECODE_U32 (p_dst, p_src) \ +do { \ + *(u32 *)p_dst = __le32_to_cpu(*(__le32 *)p_src); \ + p_dst = (u8 *)p_dst + sizeof(u32); \ + p_src = (u8 *)p_src + sizeof(u32); \ +} while (0) + +#define QMI_ENCDEC_DECODE_U64 (p_dst, p_src) \ +do { \ + *(u64 *)p_dst = __le64_to_cpu(*(__le64 *)p_src); \ + p_dst = (u8 *)p_dst + sizeof(u64); \ + p_src = (u8 *)p_src + sizeof(u64); \ +} while (0) +#define UPDATE_ENCODE_VARIABLES (temp_si, buf_dst, \ encoded_bytes, tlv_len, encode_tlv, rc) \ do { \
Signature · 1 line
2.43.0
Re: [PATCH v4 RESEND 1/3] soc: qcom: introduce new QMI encode/decode macros
Quoted 8 lines · Alexander Wilhelm
Introduce new QMI encode/decode macros for each basic element type. It allows us to further distribute and handle endianness conversion of basic element types of different size.
Signed-off-by Alexander Wilhelm <[email protected]>--- drivers/soc/qcom/qmi_encdec.c | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
Nit: I think it might be idiomatic to fold this into the next patch. Nevertheless:
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v4 RESEND 1/3] soc: qcom: introduce new QMI encode/decode macros
Quoted 5 lines · Dmitry Baryshkov
Quoted 4 lines · Alexander Wilhelm
Introduce new QMI encode/decode macros for each basic element type. It allows us to further distribute and handle endianness conversion of basic element types of different size.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Alexander Wilhelm <[email protected]>--- drivers/soc/qcom/qmi_encdec.c | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)Nit: I think it might be idiomatic to fold this into the next patch. Nevertheless:
Sure, I'll do it.
Quoted 7 lines
Reviewed-by Dmitry Baryshkov <[email protected]>Signature · 2 lines
With best wishes Dmitry
Best regards
Alexander Wilhelm
PATCH RESEND v4 2/3 soc: qcom: fix QMI encoding/decoding for basic elements
In reply to Alexander WilhelmExtend the QMI byte encoding and decoding logic to support multiple basic data type sizes (u8, u16, u32, u64) using existing macros. Ensure correct handling of data sizes and proper byte order conversion on big-endian platforms by consistently applying these macros during encoding and decoding of basic elements.
--- drivers/soc/qcom/qmi_encdec.c | 46 +++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-)
drivers/soc/qcom/qmi_encdec.c +30 -16
@@ -23,13 +23,6 @@ *p_length |= ((u8)*p_src) <<8 ; \ } while (0) -#define QMI_ENCDEC_ENCODE_N_BYTES (p_dst, p_src, size) \ -do { \ - memcpy(p_dst, p_src, size); \ - p_dst = (u8 *)p_dst + size; \ - p_src = (u8 *)p_src + size; \ -} while (0) -#define QMI_ENCDEC_ENCODE_U8 (p_dst, p_src) \ do { \ memcpy(p_dst, p_src, sizeof(u8)); \
@@ -58,13 +51,6 @@ do { \ p_src = (u8 *)p_src +sizeof (u64); \ } while (0) -#define QMI_ENCDEC_DECODE_N_BYTES (p_dst, p_src, size) \ -do { \ - memcpy(p_dst, p_src, size); \ - p_dst = (u8 *)p_dst + size; \ - p_src = (u8 *)p_src + size; \ -} while (0) -#define QMI_ENCDEC_DECODE_U8 (p_dst, p_src) \ do { \ memcpy(p_dst, p_src, sizeof(u8)); \
@@ -225,7 +211,21 @@ static int qmi_encode_basic_elem(void *buf_dst, const void *buf_src,u32 i, rc =0 ;for (i =0 ; i < elem_len; i++) { -QMI_ENCDEC_ENCODE_N_BYTES (buf_dst, buf_src, elem_size); +switch (elem_size) { +case sizeof (u8): +QMI_ENCDEC_ENCODE_U8 (buf_dst, buf_src); +break ; +case sizeof (u16): +QMI_ENCDEC_ENCODE_U16 (buf_dst, buf_src); +break ; +case sizeof (u32): +QMI_ENCDEC_ENCODE_U32 (buf_dst, buf_src); +break ; +case sizeof (u64): +QMI_ENCDEC_ENCODE_U64 (buf_dst, buf_src); +break ; + } + rc += elem_size; }
@@ -508,7 +508,21 @@ static int qmi_decode_basic_elem(void *buf_dst, const void *buf_src,u32 i, rc =0 ;for (i =0 ; i < elem_len; i++) { -QMI_ENCDEC_DECODE_N_BYTES (buf_dst, buf_src, elem_size); +switch (elem_size) { +case sizeof (u8): +QMI_ENCDEC_DECODE_U8 (buf_dst, buf_src); +break ; +case sizeof (u16): +QMI_ENCDEC_DECODE_U16 (buf_dst, buf_src); +break ; +case sizeof (u32): +QMI_ENCDEC_DECODE_U32 (buf_dst, buf_src); +break ; +case sizeof (u64): +QMI_ENCDEC_DECODE_U64 (buf_dst, buf_src); +break ; + } + rc += elem_size; }
Signature · 1 line
2.43.0
Re: [PATCH v4 RESEND 2/3] soc: qcom: fix QMI encoding/decoding for basic elements
Quoted 51 lines · Alexander Wilhelm
Extend the QMI byte encoding and decoding logic to support multiple basic data type sizes (u8, u16, u32, u64) using existing macros. Ensure correct handling of data sizes and proper byte order conversion on big-endian platforms by consistently applying these macros during encoding and decoding of basic elements.
Signed-off-by Alexander Wilhelm <[email protected]>--- drivers/soc/qcom/qmi_encdec.c | 46 +++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-)drivers/soc/qcom/qmi_encdec.c +3
-15@@ -23,13 +23,6 @@ *p_length |= ((u8)*p_src) <<8 ; \ } while (0) -#define QMI_ENCDEC_ENCODE_N_BYTES (p_dst, p_src, size) \ -do { \ - memcpy(p_dst, p_src, size); \ - p_dst = (u8 *)p_dst + size; \ - p_src = (u8 *)p_src + size; \ -} while (0) -#define QMI_ENCDEC_ENCODE_U8 (p_dst, p_src) \ do { \ memcpy(p_dst, p_src, sizeof(u8)); \@@ -58,13 +51,6 @@ do { \ p_src = (u8 *)p_src +sizeof (u64); \ } while (0) -#define QMI_ENCDEC_DECODE_N_BYTES (p_dst, p_src, size) \ -do { \ - memcpy(p_dst, p_src, size); \ - p_dst = (u8 *)p_dst + size; \ - p_src = (u8 *)p_src + size; \ -} while (0) -#define QMI_ENCDEC_DECODE_U8 (p_dst, p_src) \ do { \ memcpy(p_dst, p_src, sizeof(u8)); \@@ -225,7 +211,21 @@ static int qmi_encode_basic_elem(void *buf_dst, const void *buf_src,u32 i, rc =0 ;for (i =0 ; i < elem_len; i++) { -QMI_ENCDEC_ENCODE_N_BYTES (buf_dst, buf_src, elem_size); +switch (elem_size) { +case sizeof (u8): +QMI_ENCDEC_ENCODE_U8 (buf_dst, buf_src);
drivers/soc/qcom/qmi_encdec.c static int qmi_encode_basic_elem(void *buf_dst, const void *buf_src, · line 226
Quoted 10 lines
drivers/soc/qcom/qmi_encdec.c +10
-0+break ; +case sizeof (u16): +QMI_ENCDEC_ENCODE_U16 (buf_dst, buf_src); +break ; +case sizeof (u32): +QMI_ENCDEC_ENCODE_U32 (buf_dst, buf_src); +break ; +case sizeof (u64): +QMI_ENCDEC_ENCODE_U64 (buf_dst, buf_src); +break ;
default: scream?
Quoted 13 lines
drivers/soc/qcom/qmi_encdec.c +5
-1+ } + rc += elem_size; }@@ -508,7 +508,21 @@ static int qmi_decode_basic_elem(void *buf_dst, const void *buf_src,u32 i, rc =0 ;for (i =0 ; i < elem_len; i++) { -QMI_ENCDEC_DECODE_N_BYTES (buf_dst, buf_src, elem_size); +switch (elem_size) { +case sizeof (u8): +QMI_ENCDEC_DECODE_U8 (buf_dst, buf_src);
drivers/soc/qcom/qmi_encdec.c static int qmi_decode_basic_elem(void *buf_dst, const void *buf_src, · line 523
Quoted 10 lines
drivers/soc/qcom/qmi_encdec.c +10
-0+break ; +case sizeof (u16): +QMI_ENCDEC_DECODE_U16 (buf_dst, buf_src); +break ; +case sizeof (u32): +QMI_ENCDEC_DECODE_U32 (buf_dst, buf_src); +break ; +case sizeof (u64): +QMI_ENCDEC_DECODE_U64 (buf_dst, buf_src); +break ;
same here
Quoted 8 lines
drivers/soc/qcom/qmi_encdec.c +2
-0+ } + rc += elem_size; }Signature · 1 line
2.43.0
Signature · 2 lines
With best wishes Dmitry
Re: [PATCH v4 RESEND 2/3] soc: qcom: fix QMI encoding/decoding for basic elements
Quoted 56 lines · Dmitry Baryshkov
Quoted 55 lines · Alexander Wilhelm
Extend the QMI byte encoding and decoding logic to support multiple basic data type sizes (u8, u16, u32, u64) using existing macros. Ensure correct handling of data sizes and proper byte order conversion on big-endian platforms by consistently applying these macros during encoding and decoding of basic elements.
Signed-off-by Alexander Wilhelm <[email protected]>--- drivers/soc/qcom/qmi_encdec.c | 46 +++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-)drivers/soc/qcom/qmi_encdec.c +7
-15@@ -23,13 +23,6 @@ *p_length |= ((u8)*p_src) <<8 ; \ } while (0) -#define QMI_ENCDEC_ENCODE_N_BYTES (p_dst, p_src, size) \ -do { \ - memcpy(p_dst, p_src, size); \ - p_dst = (u8 *)p_dst + size; \ - p_src = (u8 *)p_src + size; \ -} while (0) -#define QMI_ENCDEC_ENCODE_U8 (p_dst, p_src) \ do { \ memcpy(p_dst, p_src, sizeof(u8)); \@@ -58,13 +51,6 @@ do { \ p_src = (u8 *)p_src +sizeof (u64); \ } while (0) -#define QMI_ENCDEC_DECODE_N_BYTES (p_dst, p_src, size) \ -do { \ - memcpy(p_dst, p_src, size); \ - p_dst = (u8 *)p_dst + size; \ - p_src = (u8 *)p_src + size; \ -} while (0) -#define QMI_ENCDEC_DECODE_U8 (p_dst, p_src) \ do { \ memcpy(p_dst, p_src, sizeof(u8)); \@@ -225,7 +211,21 @@ static int qmi_encode_basic_elem(void *buf_dst, const void *buf_src,u32 i, rc =0 ;for (i =0 ; i < elem_len; i++) { -QMI_ENCDEC_ENCODE_N_BYTES (buf_dst, buf_src, elem_size); +switch (elem_size) { +case sizeof (u8): +QMI_ENCDEC_ENCODE_U8 (buf_dst, buf_src); +break ; +case sizeof (u16): +QMI_ENCDEC_ENCODE_U16 (buf_dst, buf_src); +break ;
Quoted 8 lines
drivers/soc/qcom/qmi_encdec.c static int qmi_encode_basic_elem(void *buf_dst, const void *buf_src, · line 226
Quoted 6 lines
drivers/soc/qcom/qmi_encdec.c +6
-0+case sizeof (u32): +QMI_ENCDEC_ENCODE_U32 (buf_dst, buf_src); +break ; +case sizeof (u64): +QMI_ENCDEC_ENCODE_U64 (buf_dst, buf_src); +break ;default: scream?
Okay, I'll fix that in the next version. What would be the recommended default approach? The QMI protocol only uses the sizes mentioned above, and previously the data was simple memcpy'ed.
Best regards
Alexander Wilhelm
PATCH RESEND v4 3/3 soc: qcom: preserve CPU endianness for QMI_DATA_LEN
In reply to Alexander WilhelmTo ensure correct handling of endianness in the QMI subsystem, the QMI_DATA_LEN field used in host-side drivers remains in CPU-native byte order. Remove unnecessary endianness conversions, considering that QMI_DATA_LEN is always of type u32 on the host. On the QMI wire interface, however, its representation is variable and may use either 1 or 2 bytes.
--- drivers/soc/qcom/qmi_encdec.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/soc/qcom/qmi_encdec.c +4 -7
@@ -396,6 +396,7 @@ static int qmi_encode(const struct qmi_elem_info *ei_array, void *out_buf,break ;case QMI_DATA_LEN : +memcpy (&data_len_value, buf_src,sizeof (u32)); data_len_sz = temp_ei->elem_size ==sizeof (u8) ?sizeof (u8) :sizeof (u16);/* Check to avoid out of range buffer access */
@@ -406,13 +407,11 @@ static int qmi_encode(const struct qmi_elem_info *ei_array, void *out_buf,return -ETOOSMALL ; }if (data_len_sz ==sizeof (u8)) { - val8 = *(u8 *)buf_src; - data_len_value = (u32 )val8; + val8 = data_len_value; rc =qmi_encode_basic_elem (buf_dst, &val8,1 , data_len_sz); }else { - val16 = *(u16 *)buf_src; - data_len_value = (u32 )le16_to_cpu (val16); + val16 = data_len_value; rc =qmi_encode_basic_elem (buf_dst, &val16,1 , data_len_sz); }
@@ -695,7 +694,6 @@ static int qmi_decode(const struct qmi_elem_info *ei_array, void *out_c_struct,int rc;u8 val8;u16 val16; -u32 val32;while (decoded_bytes < in_buf_len) {if (dec_level >=2 && temp_ei->data_type ==QMI_EOTI )
@@ -743,8 +741,7 @@ static int qmi_decode(const struct qmi_elem_info *ei_array, void *out_c_struct, 1, data_len_sz); data_len_value = (u32 )val16; } - val32 =cpu_to_le32 (data_len_value); -memcpy (buf_dst, &val32,sizeof (u32)); +memcpy (buf_dst, &data_len_value,sizeof (u32)); temp_ei = temp_ei +1 ; buf_dst = out_c_struct + temp_ei->offset; tlv_len -= data_len_sz;
Signature · 1 line
2.43.0
Re: [PATCH v4 RESEND 3/3] soc: qcom: preserve CPU endianness for QMI_DATA_LEN
Quoted 7 lines · Alexander Wilhelm
To ensure correct handling of endianness in the QMI subsystem, the
QMI_DATA_LENfield used in host-side drivers remains in CPU-native byte order. Remove unnecessary endianness conversions, considering thatQMI_DATA_LENis always of typeu32on the host. On the QMI wire interface, however, its representation is variable and may use either 1 or 2 bytes.
Quoted 5 lines
Signed-off-by Alexander Wilhelm <[email protected]>--- drivers/soc/qcom/qmi_encdec.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
Signature · 2 lines
With best wishes Dmitry