2023-10-12 21:53:54 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* fw_tables . c - Parsing support for ACPI and ACPI - like tables provided by
* platform or device firmware
*
* Copyright ( C ) 2001 Paul Diefenbaugh < paul . s . diefenbaugh @ intel . com >
* Copyright ( C ) 2023 Intel Corp .
*/
# include <linux/errno.h>
2023-11-22 18:33:53 +03:00
# include <linux/acpi.h>
2023-10-12 21:53:54 +03:00
# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/string.h>
# include <linux/types.h>
2023-12-22 01:02:31 +03:00
# include <linux/fw_table.h>
2023-10-12 21:53:54 +03:00
enum acpi_subtable_type {
ACPI_SUBTABLE_COMMON ,
ACPI_SUBTABLE_HMAT ,
ACPI_SUBTABLE_PRMT ,
ACPI_SUBTABLE_CEDT ,
2023-12-22 01:02:31 +03:00
CDAT_SUBTABLE ,
2023-10-12 21:53:54 +03:00
} ;
struct acpi_subtable_entry {
union acpi_subtable_headers * hdr ;
enum acpi_subtable_type type ;
} ;
2023-12-22 01:02:31 +03:00
static unsigned long __init_or_fwtbl_lib
2023-10-12 21:53:54 +03:00
acpi_get_entry_type ( struct acpi_subtable_entry * entry )
{
switch ( entry - > type ) {
case ACPI_SUBTABLE_COMMON :
return entry - > hdr - > common . type ;
case ACPI_SUBTABLE_HMAT :
return entry - > hdr - > hmat . type ;
case ACPI_SUBTABLE_PRMT :
return 0 ;
case ACPI_SUBTABLE_CEDT :
return entry - > hdr - > cedt . type ;
2023-12-22 01:02:31 +03:00
case CDAT_SUBTABLE :
return entry - > hdr - > cdat . type ;
2023-10-12 21:53:54 +03:00
}
return 0 ;
}
2023-12-22 01:02:31 +03:00
static unsigned long __init_or_fwtbl_lib
2023-10-12 21:53:54 +03:00
acpi_get_entry_length ( struct acpi_subtable_entry * entry )
{
switch ( entry - > type ) {
case ACPI_SUBTABLE_COMMON :
return entry - > hdr - > common . length ;
case ACPI_SUBTABLE_HMAT :
return entry - > hdr - > hmat . length ;
case ACPI_SUBTABLE_PRMT :
return entry - > hdr - > prmt . length ;
case ACPI_SUBTABLE_CEDT :
return entry - > hdr - > cedt . length ;
2023-12-22 01:02:31 +03:00
case CDAT_SUBTABLE : {
__le16 length = ( __force __le16 ) entry - > hdr - > cdat . length ;
return le16_to_cpu ( length ) ;
}
2023-10-12 21:53:54 +03:00
}
return 0 ;
}
2023-12-22 01:02:31 +03:00
static unsigned long __init_or_fwtbl_lib
2023-10-12 21:53:54 +03:00
acpi_get_subtable_header_length ( struct acpi_subtable_entry * entry )
{
switch ( entry - > type ) {
case ACPI_SUBTABLE_COMMON :
return sizeof ( entry - > hdr - > common ) ;
case ACPI_SUBTABLE_HMAT :
return sizeof ( entry - > hdr - > hmat ) ;
case ACPI_SUBTABLE_PRMT :
return sizeof ( entry - > hdr - > prmt ) ;
case ACPI_SUBTABLE_CEDT :
return sizeof ( entry - > hdr - > cedt ) ;
2023-12-22 01:02:31 +03:00
case CDAT_SUBTABLE :
return sizeof ( entry - > hdr - > cdat ) ;
2023-10-12 21:53:54 +03:00
}
return 0 ;
}
2023-12-22 01:02:31 +03:00
static enum acpi_subtable_type __init_or_fwtbl_lib
2023-10-12 21:53:54 +03:00
acpi_get_subtable_type ( char * id )
{
if ( strncmp ( id , ACPI_SIG_HMAT , 4 ) = = 0 )
return ACPI_SUBTABLE_HMAT ;
if ( strncmp ( id , ACPI_SIG_PRMT , 4 ) = = 0 )
return ACPI_SUBTABLE_PRMT ;
if ( strncmp ( id , ACPI_SIG_CEDT , 4 ) = = 0 )
return ACPI_SUBTABLE_CEDT ;
2023-12-22 01:02:31 +03:00
if ( strncmp ( id , ACPI_SIG_CDAT , 4 ) = = 0 )
return CDAT_SUBTABLE ;
2023-10-12 21:53:54 +03:00
return ACPI_SUBTABLE_COMMON ;
}
2023-12-22 01:02:31 +03:00
static unsigned long __init_or_fwtbl_lib
acpi_table_get_length ( enum acpi_subtable_type type ,
union fw_table_header * header )
{
if ( type = = CDAT_SUBTABLE ) {
__le32 length = ( __force __le32 ) header - > cdat . length ;
return le32_to_cpu ( length ) ;
}
return header - > acpi . length ;
}
static __init_or_fwtbl_lib int call_handler ( struct acpi_subtable_proc * proc ,
union acpi_subtable_headers * hdr ,
unsigned long end )
2023-10-12 21:53:54 +03:00
{
if ( proc - > handler )
return proc - > handler ( hdr , end ) ;
if ( proc - > handler_arg )
return proc - > handler_arg ( hdr , proc - > arg , end ) ;
return - EINVAL ;
}
/**
* acpi_parse_entries_array - for each proc_num find a suitable subtable
*
* @ id : table id ( for debugging purposes )
* @ table_size : size of the root table
2024-02-18 00:39:46 +03:00
* @ max_length : maximum size of the table ( ignore if 0 )
2023-10-12 21:53:54 +03:00
* @ table_header : where does the table start ?
* @ proc : array of acpi_subtable_proc struct containing entry id
* and associated handler with it
* @ proc_num : how big proc is ?
* @ max_entries : how many entries can we process ?
*
* For each proc_num find a subtable with proc - > id and run proc - > handler
* on it . Assumption is that there ' s only single handler for particular
* entry id .
*
* The table_size is not the size of the complete ACPI table ( the length
* field in the header struct ) , but only the size of the root table ; i . e . ,
* the offset from the very first byte of the complete ACPI table , to the
* first byte of the very first subtable .
*
* On success returns sum of all matching entries for all proc handlers .
* Otherwise , - ENODEV or - EINVAL is returned .
*/
2023-12-22 01:02:31 +03:00
int __init_or_fwtbl_lib
2023-10-12 21:53:54 +03:00
acpi_parse_entries_array ( char * id , unsigned long table_size ,
2023-12-22 01:02:31 +03:00
union fw_table_header * table_header ,
2024-02-18 00:39:46 +03:00
unsigned long max_length ,
2023-10-12 21:53:54 +03:00
struct acpi_subtable_proc * proc ,
int proc_num , unsigned int max_entries )
{
2024-02-18 00:39:46 +03:00
unsigned long table_len , table_end , subtable_len , entry_len ;
2023-10-12 21:53:54 +03:00
struct acpi_subtable_entry entry ;
2023-12-22 01:02:31 +03:00
enum acpi_subtable_type type ;
2023-10-12 21:53:54 +03:00
int count = 0 ;
int i ;
2023-12-22 01:02:31 +03:00
type = acpi_get_subtable_type ( id ) ;
2024-02-18 00:39:46 +03:00
table_len = acpi_table_get_length ( type , table_header ) ;
if ( max_length & & max_length < table_len )
table_len = max_length ;
table_end = ( unsigned long ) table_header + table_len ;
2023-10-12 21:53:54 +03:00
/* Parse all entries looking for a match. */
2023-12-22 01:02:31 +03:00
entry . type = type ;
2023-10-12 21:53:54 +03:00
entry . hdr = ( union acpi_subtable_headers * )
( ( unsigned long ) table_header + table_size ) ;
subtable_len = acpi_get_subtable_header_length ( & entry ) ;
ACPI: tables: Correct and clean up the logic of acpi_parse_entries_array()
The original intention of acpi_parse_entries_array() is to return the
number of all matching entries on success. This number may be greater than
the value of the max_entries parameter. When this happens, the function
will output a warning message, indicating that `count - max_entries`
matching entries remain unprocessed and have been ignored.
However, commit 4ceacd02f5a1 ("ACPI / table: Always count matched and
successfully parsed entries") changed this logic to return the number of
entries successfully processed by the handler. In this case, when the
max_entries parameter is not zero, the number of entries successfully
processed can never be greater than the value of max_entries. In other
words, the expression `count > max_entries` will always evaluate to false.
This means that the logic in the final if statement will never be executed.
Commit 99b0efd7c886 ("ACPI / tables: do not report the number of entries
ignored by acpi_parse_entries()") mentioned this issue, but it tried to fix
it by removing part of the warning message. This is meaningless because the
pr_warn statement will never be executed in the first place.
Commit 8726d4f44150 ("ACPI / tables: fix acpi_parse_entries_array() so it
traverses all subtables") introduced an errs variable, which is intended to
make acpi_parse_entries_array() always traverse all of the subtables,
calling as many of the callbacks as possible. However, it seems that the
commit does not achieve this goal. For example, when a handler returns an
error, none of the handlers will be called again in the subsequent
iterations. This result appears to be no different from before the change.
This patch corrects and cleans up the logic of acpi_parse_entries_array(),
making it return the number of all matching entries, rather than the number
of entries successfully processed by handlers. Additionally, if an error
occurs when executing a handler, the function will return -EINVAL immediately.
This patch should not affect existing users of acpi_parse_entries_array().
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2023-11-20 14:41:43 +03:00
while ( ( ( unsigned long ) entry . hdr ) + subtable_len < table_end ) {
2023-10-12 21:53:54 +03:00
for ( i = 0 ; i < proc_num ; i + + ) {
if ( acpi_get_entry_type ( & entry ) ! = proc [ i ] . id )
continue ;
ACPI: tables: Correct and clean up the logic of acpi_parse_entries_array()
The original intention of acpi_parse_entries_array() is to return the
number of all matching entries on success. This number may be greater than
the value of the max_entries parameter. When this happens, the function
will output a warning message, indicating that `count - max_entries`
matching entries remain unprocessed and have been ignored.
However, commit 4ceacd02f5a1 ("ACPI / table: Always count matched and
successfully parsed entries") changed this logic to return the number of
entries successfully processed by the handler. In this case, when the
max_entries parameter is not zero, the number of entries successfully
processed can never be greater than the value of max_entries. In other
words, the expression `count > max_entries` will always evaluate to false.
This means that the logic in the final if statement will never be executed.
Commit 99b0efd7c886 ("ACPI / tables: do not report the number of entries
ignored by acpi_parse_entries()") mentioned this issue, but it tried to fix
it by removing part of the warning message. This is meaningless because the
pr_warn statement will never be executed in the first place.
Commit 8726d4f44150 ("ACPI / tables: fix acpi_parse_entries_array() so it
traverses all subtables") introduced an errs variable, which is intended to
make acpi_parse_entries_array() always traverse all of the subtables,
calling as many of the callbacks as possible. However, it seems that the
commit does not achieve this goal. For example, when a handler returns an
error, none of the handlers will be called again in the subsequent
iterations. This result appears to be no different from before the change.
This patch corrects and cleans up the logic of acpi_parse_entries_array(),
making it return the number of all matching entries, rather than the number
of entries successfully processed by handlers. Additionally, if an error
occurs when executing a handler, the function will return -EINVAL immediately.
This patch should not affect existing users of acpi_parse_entries_array().
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2023-11-20 14:41:43 +03:00
if ( ! max_entries | | count < max_entries )
if ( call_handler ( & proc [ i ] , entry . hdr , table_end ) )
return - EINVAL ;
2023-10-12 21:53:54 +03:00
proc [ i ] . count + + ;
ACPI: tables: Correct and clean up the logic of acpi_parse_entries_array()
The original intention of acpi_parse_entries_array() is to return the
number of all matching entries on success. This number may be greater than
the value of the max_entries parameter. When this happens, the function
will output a warning message, indicating that `count - max_entries`
matching entries remain unprocessed and have been ignored.
However, commit 4ceacd02f5a1 ("ACPI / table: Always count matched and
successfully parsed entries") changed this logic to return the number of
entries successfully processed by the handler. In this case, when the
max_entries parameter is not zero, the number of entries successfully
processed can never be greater than the value of max_entries. In other
words, the expression `count > max_entries` will always evaluate to false.
This means that the logic in the final if statement will never be executed.
Commit 99b0efd7c886 ("ACPI / tables: do not report the number of entries
ignored by acpi_parse_entries()") mentioned this issue, but it tried to fix
it by removing part of the warning message. This is meaningless because the
pr_warn statement will never be executed in the first place.
Commit 8726d4f44150 ("ACPI / tables: fix acpi_parse_entries_array() so it
traverses all subtables") introduced an errs variable, which is intended to
make acpi_parse_entries_array() always traverse all of the subtables,
calling as many of the callbacks as possible. However, it seems that the
commit does not achieve this goal. For example, when a handler returns an
error, none of the handlers will be called again in the subsequent
iterations. This result appears to be no different from before the change.
This patch corrects and cleans up the logic of acpi_parse_entries_array(),
making it return the number of all matching entries, rather than the number
of entries successfully processed by handlers. Additionally, if an error
occurs when executing a handler, the function will return -EINVAL immediately.
This patch should not affect existing users of acpi_parse_entries_array().
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2023-11-20 14:41:43 +03:00
count + + ;
2023-10-12 21:53:54 +03:00
break ;
}
/*
* If entry - > length is 0 , break from this loop to avoid
* infinite loop .
*/
entry_len = acpi_get_entry_length ( & entry ) ;
if ( entry_len = = 0 ) {
pr_err ( " [%4.4s:0x%02x] Invalid zero length \n " , id , proc - > id ) ;
return - EINVAL ;
}
entry . hdr = ( union acpi_subtable_headers * )
( ( unsigned long ) entry . hdr + entry_len ) ;
}
if ( max_entries & & count > max_entries ) {
ACPI: tables: Correct and clean up the logic of acpi_parse_entries_array()
The original intention of acpi_parse_entries_array() is to return the
number of all matching entries on success. This number may be greater than
the value of the max_entries parameter. When this happens, the function
will output a warning message, indicating that `count - max_entries`
matching entries remain unprocessed and have been ignored.
However, commit 4ceacd02f5a1 ("ACPI / table: Always count matched and
successfully parsed entries") changed this logic to return the number of
entries successfully processed by the handler. In this case, when the
max_entries parameter is not zero, the number of entries successfully
processed can never be greater than the value of max_entries. In other
words, the expression `count > max_entries` will always evaluate to false.
This means that the logic in the final if statement will never be executed.
Commit 99b0efd7c886 ("ACPI / tables: do not report the number of entries
ignored by acpi_parse_entries()") mentioned this issue, but it tried to fix
it by removing part of the warning message. This is meaningless because the
pr_warn statement will never be executed in the first place.
Commit 8726d4f44150 ("ACPI / tables: fix acpi_parse_entries_array() so it
traverses all subtables") introduced an errs variable, which is intended to
make acpi_parse_entries_array() always traverse all of the subtables,
calling as many of the callbacks as possible. However, it seems that the
commit does not achieve this goal. For example, when a handler returns an
error, none of the handlers will be called again in the subsequent
iterations. This result appears to be no different from before the change.
This patch corrects and cleans up the logic of acpi_parse_entries_array(),
making it return the number of all matching entries, rather than the number
of entries successfully processed by handlers. Additionally, if an error
occurs when executing a handler, the function will return -EINVAL immediately.
This patch should not affect existing users of acpi_parse_entries_array().
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2023-11-20 14:41:43 +03:00
pr_warn ( " [%4.4s:0x%02x] ignored %i entries of %i found \n " ,
id , proc - > id , count - max_entries , count ) ;
2023-10-12 21:53:54 +03:00
}
ACPI: tables: Correct and clean up the logic of acpi_parse_entries_array()
The original intention of acpi_parse_entries_array() is to return the
number of all matching entries on success. This number may be greater than
the value of the max_entries parameter. When this happens, the function
will output a warning message, indicating that `count - max_entries`
matching entries remain unprocessed and have been ignored.
However, commit 4ceacd02f5a1 ("ACPI / table: Always count matched and
successfully parsed entries") changed this logic to return the number of
entries successfully processed by the handler. In this case, when the
max_entries parameter is not zero, the number of entries successfully
processed can never be greater than the value of max_entries. In other
words, the expression `count > max_entries` will always evaluate to false.
This means that the logic in the final if statement will never be executed.
Commit 99b0efd7c886 ("ACPI / tables: do not report the number of entries
ignored by acpi_parse_entries()") mentioned this issue, but it tried to fix
it by removing part of the warning message. This is meaningless because the
pr_warn statement will never be executed in the first place.
Commit 8726d4f44150 ("ACPI / tables: fix acpi_parse_entries_array() so it
traverses all subtables") introduced an errs variable, which is intended to
make acpi_parse_entries_array() always traverse all of the subtables,
calling as many of the callbacks as possible. However, it seems that the
commit does not achieve this goal. For example, when a handler returns an
error, none of the handlers will be called again in the subsequent
iterations. This result appears to be no different from before the change.
This patch corrects and cleans up the logic of acpi_parse_entries_array(),
making it return the number of all matching entries, rather than the number
of entries successfully processed by handlers. Additionally, if an error
occurs when executing a handler, the function will return -EINVAL immediately.
This patch should not affect existing users of acpi_parse_entries_array().
Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2023-11-20 14:41:43 +03:00
return count ;
2023-10-12 21:53:54 +03:00
}
2023-12-22 01:02:31 +03:00
int __init_or_fwtbl_lib
cdat_table_parse ( enum acpi_cdat_type type ,
acpi_tbl_entry_handler_arg handler_arg ,
void * arg ,
2024-02-18 00:39:46 +03:00
struct acpi_table_cdat * table_header ,
unsigned long length )
2023-12-22 01:02:31 +03:00
{
struct acpi_subtable_proc proc = {
. id = type ,
. handler_arg = handler_arg ,
. arg = arg ,
} ;
if ( ! table_header )
return - EINVAL ;
return acpi_parse_entries_array ( ACPI_SIG_CDAT ,
sizeof ( struct acpi_table_cdat ) ,
( union fw_table_header * ) table_header ,
2024-02-18 00:39:46 +03:00
length , & proc , 1 , 0 ) ;
2023-12-22 01:02:31 +03:00
}
EXPORT_SYMBOL_FWTBL_LIB ( cdat_table_parse ) ;