2018-01-26 20:45:16 +03:00
// SPDX-License-Identifier: GPL-2.0
2008-06-11 01:28:50 +04:00
/*
* Copyright ( C ) 2006 Matthew Wilcox < matthew @ wil . cx >
2009-02-04 21:25:22 +03:00
* Copyright ( C ) 2006 - 2009 Hewlett - Packard Development Company , L . P .
* Alex Chiang < achiang @ hp . com >
2008-06-11 01:28:50 +04:00
*/
# include <linux/kobject.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2011-05-27 17:42:30 +04:00
# include <linux/module.h>
2008-06-11 01:28:50 +04:00
# include <linux/pci.h>
# include <linux/err.h>
# include "pci.h"
struct kset * pci_slots_kset ;
EXPORT_SYMBOL_GPL ( pci_slots_kset ) ;
static ssize_t pci_slot_attr_show ( struct kobject * kobj ,
struct attribute * attr , char * buf )
{
struct pci_slot * slot = to_pci_slot ( kobj ) ;
struct pci_slot_attribute * attribute = to_pci_slot_attr ( attr ) ;
return attribute - > show ? attribute - > show ( slot , buf ) : - EIO ;
}
static ssize_t pci_slot_attr_store ( struct kobject * kobj ,
struct attribute * attr , const char * buf , size_t len )
{
struct pci_slot * slot = to_pci_slot ( kobj ) ;
struct pci_slot_attribute * attribute = to_pci_slot_attr ( attr ) ;
return attribute - > store ? attribute - > store ( slot , buf , len ) : - EIO ;
}
2010-01-19 04:58:23 +03:00
static const struct sysfs_ops pci_slot_sysfs_ops = {
2008-06-11 01:28:50 +04:00
. show = pci_slot_attr_show ,
. store = pci_slot_attr_store ,
} ;
static ssize_t address_read_file ( struct pci_slot * slot , char * buf )
{
if ( slot - > number = = 0xff )
2021-06-03 03:01:07 +03:00
return sysfs_emit ( buf , " %04x:%02x \n " ,
pci_domain_nr ( slot - > bus ) ,
slot - > bus - > number ) ;
return sysfs_emit ( buf , " %04x:%02x:%02x \n " ,
pci_domain_nr ( slot - > bus ) ,
slot - > bus - > number ,
slot - > number ) ;
2008-06-11 01:28:50 +04:00
}
2009-12-13 16:11:32 +03:00
static ssize_t bus_speed_read ( enum pci_bus_speed speed , char * buf )
{
2021-06-03 03:01:07 +03:00
return sysfs_emit ( buf , " %s \n " , pci_speed_string ( speed ) ) ;
2009-12-13 16:11:32 +03:00
}
static ssize_t max_speed_read_file ( struct pci_slot * slot , char * buf )
{
return bus_speed_read ( slot - > bus - > max_bus_speed , buf ) ;
}
static ssize_t cur_speed_read_file ( struct pci_slot * slot , char * buf )
{
return bus_speed_read ( slot - > bus - > cur_bus_speed , buf ) ;
}
2008-06-11 01:28:50 +04:00
static void pci_slot_release ( struct kobject * kobj )
{
2008-09-02 19:40:51 +04:00
struct pci_dev * dev ;
2008-06-11 01:28:50 +04:00
struct pci_slot * slot = to_pci_slot ( kobj ) ;
2009-02-04 21:25:22 +03:00
dev_dbg ( & slot - > bus - > dev , " dev %02x, released physical slot %s \n " ,
slot - > number , pci_slot_name ( slot ) ) ;
2008-06-11 01:28:50 +04:00
2015-07-17 12:16:31 +03:00
down_read ( & pci_bus_sem ) ;
2008-09-02 19:40:51 +04:00
list_for_each_entry ( dev , & slot - > bus - > devices , bus_list )
if ( PCI_SLOT ( dev - > devfn ) = = slot - > number )
dev - > slot = NULL ;
2015-07-17 12:16:31 +03:00
up_read ( & pci_bus_sem ) ;
2008-09-02 19:40:51 +04:00
2008-06-11 01:28:50 +04:00
list_del ( & slot - > list ) ;
kfree ( slot ) ;
}
static struct pci_slot_attribute pci_slot_attr_address =
2014-03-24 05:30:34 +04:00
__ATTR ( address , S_IRUGO , address_read_file , NULL ) ;
2009-12-13 16:11:32 +03:00
static struct pci_slot_attribute pci_slot_attr_max_speed =
2014-03-24 05:30:34 +04:00
__ATTR ( max_bus_speed , S_IRUGO , max_speed_read_file , NULL ) ;
2009-12-13 16:11:32 +03:00
static struct pci_slot_attribute pci_slot_attr_cur_speed =
2014-03-24 05:30:34 +04:00
__ATTR ( cur_bus_speed , S_IRUGO , cur_speed_read_file , NULL ) ;
2008-06-11 01:28:50 +04:00
static struct attribute * pci_slot_default_attrs [ ] = {
& pci_slot_attr_address . attr ,
2009-12-13 16:11:32 +03:00
& pci_slot_attr_max_speed . attr ,
& pci_slot_attr_cur_speed . attr ,
2008-06-11 01:28:50 +04:00
NULL ,
} ;
2021-12-28 16:57:22 +03:00
ATTRIBUTE_GROUPS ( pci_slot_default ) ;
2008-06-11 01:28:50 +04:00
2023-02-16 04:12:25 +03:00
static const struct kobj_type pci_slot_ktype = {
2008-06-11 01:28:50 +04:00
. sysfs_ops = & pci_slot_sysfs_ops ,
. release = & pci_slot_release ,
2021-12-28 16:57:22 +03:00
. default_groups = pci_slot_default_groups ,
2008-06-11 01:28:50 +04:00
} ;
2008-10-21 03:41:02 +04:00
static char * make_slot_name ( const char * name )
{
char * new_name ;
int len , max , dup ;
new_name = kstrdup ( name , GFP_KERNEL ) ;
if ( ! new_name )
return NULL ;
/*
* Make sure we hit the realloc case the first time through the
* loop . ' len ' will be strlen ( name ) + 3 at that point which is
* enough space for " name-X " and the trailing NUL .
*/
len = strlen ( name ) + 2 ;
max = 1 ;
dup = 1 ;
for ( ; ; ) {
struct kobject * dup_slot ;
dup_slot = kset_find_obj ( pci_slots_kset , new_name ) ;
if ( ! dup_slot )
break ;
kobject_put ( dup_slot ) ;
if ( dup = = max ) {
len + + ;
max * = 10 ;
kfree ( new_name ) ;
new_name = kmalloc ( len , GFP_KERNEL ) ;
if ( ! new_name )
break ;
}
sprintf ( new_name , " %s-%d " , name , dup + + ) ;
}
return new_name ;
}
static int rename_slot ( struct pci_slot * slot , const char * name )
{
int result = 0 ;
char * slot_name ;
2008-10-21 03:41:07 +04:00
if ( strcmp ( pci_slot_name ( slot ) , name ) = = 0 )
2008-10-21 03:41:02 +04:00
return result ;
slot_name = make_slot_name ( name ) ;
if ( ! slot_name )
return - ENOMEM ;
result = kobject_rename ( & slot - > kobj , slot_name ) ;
kfree ( slot_name ) ;
return result ;
}
2015-07-17 12:16:32 +03:00
void pci_dev_assign_slot ( struct pci_dev * dev )
{
struct pci_slot * slot ;
mutex_lock ( & pci_slot_mutex ) ;
list_for_each_entry ( slot , & dev - > bus - > slots , list )
if ( PCI_SLOT ( dev - > devfn ) = = slot - > number )
dev - > slot = slot ;
mutex_unlock ( & pci_slot_mutex ) ;
}
2008-10-21 03:41:02 +04:00
static struct pci_slot * get_slot ( struct pci_bus * parent , int slot_nr )
{
struct pci_slot * slot ;
2015-07-17 12:16:31 +03:00
/* We already hold pci_slot_mutex */
2008-10-21 03:41:02 +04:00
list_for_each_entry ( slot , & parent - > slots , list )
if ( slot - > number = = slot_nr ) {
kobject_get ( & slot - > kobj ) ;
return slot ;
}
return NULL ;
}
2008-06-11 01:28:50 +04:00
/**
* pci_create_slot - create or increment refcount for physical PCI slot
* @ parent : struct pci_bus of parent bridge
* @ slot_nr : PCI_SLOT ( pci_dev - > devfn ) or - 1 for placeholder
* @ name : user visible string presented in / sys / bus / pci / slots / < name >
2008-10-21 03:40:52 +04:00
* @ hotplug : set if caller is hotplug driver , NULL otherwise
2008-06-11 01:28:50 +04:00
*
* PCI slots have first class attributes such as address , speed , width ,
* and a & struct pci_slot is used to manage them . This interface will
* either return a new & struct pci_slot to the caller , or if the pci_slot
* already exists , its refcount will be incremented .
*
2008-10-21 03:41:02 +04:00
* Slots are uniquely identified by a @ pci_bus , @ slot_nr tuple .
*
* There are known platforms with broken firmware that assign the same
* name to multiple slots . Workaround these broken platforms by renaming
* the slots on behalf of the caller . If firmware assigns name N to
* multiple slots :
*
* The first slot is assigned N
* The second slot is assigned N - 1
* The third slot is assigned N - 2
* etc .
2008-06-11 01:28:50 +04:00
*
* Placeholder slots :
* In most cases , @ pci_bus , @ slot_nr will be sufficient to uniquely identify
* a slot . There is one notable exception - pSeries ( rpaphp ) , where the
* @ slot_nr cannot be determined until a device is actually inserted into
* the slot . In this scenario , the caller may pass - 1 for @ slot_nr .
*
* The following semantics are imposed when the caller passes @ slot_nr = =
2008-10-21 03:41:02 +04:00
* - 1. First , we no longer check for an existing % struct pci_slot , as there
* may be many slots with @ slot_nr of - 1. The other change in semantics is
2008-06-11 01:28:50 +04:00
* user - visible , which is the ' address ' parameter presented in sysfs will
* consist solely of a dddd : bb tuple , where dddd is the PCI domain of the
* % struct pci_bus and bb is the bus number . In other words , the devfn of
* the ' placeholder ' slot will not be displayed .
*/
struct pci_slot * pci_create_slot ( struct pci_bus * parent , int slot_nr ,
2008-10-21 03:40:52 +04:00
const char * name ,
struct hotplug_slot * hotplug )
2008-06-11 01:28:50 +04:00
{
2008-09-02 19:40:51 +04:00
struct pci_dev * dev ;
2008-06-11 01:28:50 +04:00
struct pci_slot * slot ;
2008-10-21 03:41:02 +04:00
int err = 0 ;
char * slot_name = NULL ;
2008-06-11 01:28:50 +04:00
2015-07-17 12:16:31 +03:00
mutex_lock ( & pci_slot_mutex ) ;
2008-06-11 01:28:50 +04:00
if ( slot_nr = = - 1 )
goto placeholder ;
2008-10-21 03:41:02 +04:00
/*
* Hotplug drivers are allowed to rename an existing slot ,
* but only if not already claimed .
*/
slot = get_slot ( parent , slot_nr ) ;
if ( slot ) {
if ( hotplug ) {
if ( ( err = slot - > hotplug ? - EBUSY : 0 )
| | ( err = rename_slot ( slot , name ) ) ) {
kobject_put ( & slot - > kobj ) ;
slot = NULL ;
goto err ;
}
2008-06-11 01:28:50 +04:00
}
2008-10-21 03:41:02 +04:00
goto out ;
2008-06-11 01:28:50 +04:00
}
placeholder :
slot = kzalloc ( sizeof ( * slot ) , GFP_KERNEL ) ;
if ( ! slot ) {
2008-10-21 03:41:02 +04:00
err = - ENOMEM ;
goto err ;
2008-06-11 01:28:50 +04:00
}
slot - > bus = parent ;
slot - > number = slot_nr ;
slot - > kobj . kset = pci_slots_kset ;
2008-10-21 03:41:02 +04:00
slot_name = make_slot_name ( name ) ;
if ( ! slot_name ) {
err = - ENOMEM ;
2020-05-28 05:13:22 +03:00
kfree ( slot ) ;
2008-06-11 01:28:50 +04:00
goto err ;
}
2020-12-02 05:33:42 +03:00
INIT_LIST_HEAD ( & slot - > list ) ;
list_add ( & slot - > list , & parent - > slots ) ;
2008-10-21 03:41:02 +04:00
err = kobject_init_and_add ( & slot - > kobj , & pci_slot_ktype , NULL ,
" %s " , slot_name ) ;
2020-05-28 05:13:22 +03:00
if ( err ) {
kobject_put ( & slot - > kobj ) ;
2008-10-21 03:41:02 +04:00
goto err ;
2020-05-28 05:13:22 +03:00
}
2008-10-21 03:41:02 +04:00
2015-07-17 12:16:31 +03:00
down_read ( & pci_bus_sem ) ;
2008-09-02 19:40:51 +04:00
list_for_each_entry ( dev , & parent - > devices , bus_list )
if ( PCI_SLOT ( dev - > devfn ) = = slot_nr )
dev - > slot = slot ;
2015-07-17 12:16:31 +03:00
up_read ( & pci_bus_sem ) ;
2008-09-02 19:40:51 +04:00
2009-02-04 21:25:22 +03:00
dev_dbg ( & parent - > dev , " dev %02x, created physical slot %s \n " ,
slot_nr , pci_slot_name ( slot ) ) ;
2008-06-11 01:28:50 +04:00
2008-10-21 03:41:02 +04:00
out :
2008-12-02 04:17:21 +03:00
kfree ( slot_name ) ;
2015-07-17 12:16:31 +03:00
mutex_unlock ( & pci_slot_mutex ) ;
2008-06-11 01:28:50 +04:00
return slot ;
2008-10-21 03:41:02 +04:00
err :
2008-06-11 01:28:50 +04:00
slot = ERR_PTR ( err ) ;
goto out ;
}
EXPORT_SYMBOL_GPL ( pci_create_slot ) ;
/**
* pci_destroy_slot - decrement refcount for physical PCI slot
* @ slot : struct pci_slot to decrement
*
* % struct pci_slot is refcounted , so destroying them is really easy ; we
* just call kobject_put on its kobj and let our release methods do the
* rest .
*/
void pci_destroy_slot ( struct pci_slot * slot )
{
2009-02-04 21:25:22 +03:00
dev_dbg ( & slot - > bus - > dev , " dev %02x, dec refcount to %d \n " ,
2016-11-14 19:29:48 +03:00
slot - > number , kref_read ( & slot - > kobj . kref ) - 1 ) ;
2008-06-11 01:28:50 +04:00
2015-07-17 12:16:31 +03:00
mutex_lock ( & pci_slot_mutex ) ;
2008-06-11 01:28:50 +04:00
kobject_put ( & slot - > kobj ) ;
2015-07-17 12:16:31 +03:00
mutex_unlock ( & pci_slot_mutex ) ;
2008-06-11 01:28:50 +04:00
}
EXPORT_SYMBOL_GPL ( pci_destroy_slot ) ;
2009-06-16 06:01:25 +04:00
# if defined(CONFIG_HOTPLUG_PCI) || defined(CONFIG_HOTPLUG_PCI_MODULE)
# include <linux/pci_hotplug.h>
/**
2020-10-23 19:33:10 +03:00
* pci_hp_create_module_link - create symbolic link to hotplug driver module
2009-06-24 20:18:14 +04:00
* @ pci_slot : struct pci_slot
2009-06-16 06:01:25 +04:00
*
* Helper function for pci_hotplug_core . c to create symbolic link to
* the hotplug driver module .
*/
void pci_hp_create_module_link ( struct pci_slot * pci_slot )
{
struct hotplug_slot * slot = pci_slot - > hotplug ;
struct kobject * kobj = NULL ;
2013-04-12 21:35:40 +04:00
int ret ;
2009-06-16 06:01:25 +04:00
if ( ! slot | | ! slot - > ops )
return ;
2018-09-08 10:59:01 +03:00
kobj = kset_find_obj ( module_kset , slot - > mod_name ) ;
2009-06-16 06:01:25 +04:00
if ( ! kobj )
return ;
2013-04-12 21:35:40 +04:00
ret = sysfs_create_link ( & pci_slot - > kobj , kobj , " module " ) ;
if ( ret )
dev_err ( & pci_slot - > bus - > dev , " Error creating sysfs link (%d) \n " ,
ret ) ;
2009-06-16 06:01:25 +04:00
kobject_put ( kobj ) ;
}
EXPORT_SYMBOL_GPL ( pci_hp_create_module_link ) ;
/**
2020-10-23 19:33:10 +03:00
* pci_hp_remove_module_link - remove symbolic link to the hotplug driver
* module .
2009-06-24 20:18:14 +04:00
* @ pci_slot : struct pci_slot
2009-06-16 06:01:25 +04:00
*
* Helper function for pci_hotplug_core . c to remove symbolic link to
* the hotplug driver module .
*/
void pci_hp_remove_module_link ( struct pci_slot * pci_slot )
{
sysfs_remove_link ( & pci_slot - > kobj , " module " ) ;
}
EXPORT_SYMBOL_GPL ( pci_hp_remove_module_link ) ;
# endif
2008-06-11 01:28:50 +04:00
static int pci_slot_init ( void )
{
struct kset * pci_bus_kset ;
pci_bus_kset = bus_get_kset ( & pci_bus_type ) ;
pci_slots_kset = kset_create_and_add ( " slots " , NULL ,
& pci_bus_kset - > kobj ) ;
if ( ! pci_slots_kset ) {
2019-04-20 07:03:46 +03:00
pr_err ( " PCI: Slot initialization failure \n " ) ;
2008-06-11 01:28:50 +04:00
return - ENOMEM ;
}
return 0 ;
}
subsys_initcall ( pci_slot_init ) ;