diff mbox series

[3/3] PCI/ASPM: add sysfs attribute for controlling ASPM

Message ID a0c090cd-e3a4-f667-b99d-f31c48c2e0a3@gmail.com (mailing list archive)
State New, archived
Delegated to: Bjorn Helgaas
Headers show
Series PCI/ASPM: add sysfs attribute for controlling ASPM | expand

Commit Message

Heiner Kallweit May 23, 2019, 8:05 p.m. UTC
Background of this extension is a problem with the r8169 network driver.
Several combinations of board chipsets and network chip versions have
problems if ASPM is enabled, therefore we have to disable ASPM per default.
However especially on notebooks ASPM can provide significant power-saving,
therefore we want to give users the option to enable ASPM. With the new sysfs
attribute users can control which ASPM link-states are enabled/disabled.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 Documentation/ABI/testing/sysfs-bus-pci |  13 ++
 drivers/pci/pci.h                       |   8 +-
 drivers/pci/pcie/aspm.c                 | 180 +++++++++++++++++++++++-
 3 files changed, 193 insertions(+), 8 deletions(-)

Comments

Bjorn Helgaas Aug. 20, 2019, 10:34 a.m. UTC | #1
[+cc Greg, Rajat]

On Thu, May 23, 2019 at 10:05:35PM +0200, Heiner Kallweit wrote:
> Background of this extension is a problem with the r8169 network driver.
> Several combinations of board chipsets and network chip versions have
> problems if ASPM is enabled, therefore we have to disable ASPM per default.
> However especially on notebooks ASPM can provide significant power-saving,
> therefore we want to give users the option to enable ASPM. With the new sysfs
> attribute users can control which ASPM link-states are enabled/disabled.
> 
> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> ---
>  Documentation/ABI/testing/sysfs-bus-pci |  13 ++
>  drivers/pci/pci.h                       |   8 +-
>  drivers/pci/pcie/aspm.c                 | 180 +++++++++++++++++++++++-
>  3 files changed, 193 insertions(+), 8 deletions(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
> index 8bfee557e..38fe358de 100644
> --- a/Documentation/ABI/testing/sysfs-bus-pci
> +++ b/Documentation/ABI/testing/sysfs-bus-pci
> @@ -347,3 +347,16 @@ Description:
>  		If the device has any Peer-to-Peer memory registered, this
>  	        file contains a '1' if the memory has been published for
>  		use outside the driver that owns the device.
> +
> +What:		/sys/bus/pci/devices/.../power/aspm_link_states
> +Date:		May 2019
> +Contact:	Heiner Kallweit <hkallweit1@gmail.com>
> +Description:
> +		If ASPM is supported for an endpoint, then this file can be
> +		used to enable / disable link states. A link state
> +		displayed in brackets is enabled, otherwise it's disabled.
> +		To control link states (case insensitive):
> +		+state : enables a supported state
> +		-state : disables a state
> +		none : disables all link states
> +		all : enables all supported link states

IIUC this "aspm_link_states" file will contain things like this:

  L0S L1 L1.1 L1.2                 # All states supported, all disabled
  [L0S] L1                         # L0s enabled, L1 supported but disabled
  [L0S] [L1]                       # L0s and L1 enabled
  ...

and the control is by writing things like this to it:

  +L1                              # enables L1
  +L1.1                            # enables L1.1
  -L0S                             # disables L0s

I know this file structure is similar to protocol handling in
drivers/media/rc/rc-main.c, but Documentation/filesystems/sysfs.txt
suggests single values in a file, and Greg recently pointed out that
we screwed up some PCI AER stats [1].

So I'm thinking maybe we should split this into several files, e.g.,

  /sys/devices/pci*/.../power/aspm_l0s
  /sys/devices/pci*/.../power/aspm_l1
  /sys/devices/pci*/.../power/aspm_l1.1
  /sys/devices/pci*/.../power/aspm_l1.2

which would contain just 1/0 values, and we'd write 1/0 to
enable/disable things.

Since the L1 PM Substates control register has separate enable bits
for PCI-PM L1.1 and L1.2, we might also want a way to manage those.

Bjorn

[1] https://lore.kernel.org/r/20190621072911.GA21600@kroah.com
Greg Kroah-Hartman Aug. 20, 2019, 5:05 p.m. UTC | #2
On Tue, Aug 20, 2019 at 05:34:00AM -0500, Bjorn Helgaas wrote:
> [+cc Greg, Rajat]
> 
> On Thu, May 23, 2019 at 10:05:35PM +0200, Heiner Kallweit wrote:
> > Background of this extension is a problem with the r8169 network driver.
> > Several combinations of board chipsets and network chip versions have
> > problems if ASPM is enabled, therefore we have to disable ASPM per default.
> > However especially on notebooks ASPM can provide significant power-saving,
> > therefore we want to give users the option to enable ASPM. With the new sysfs
> > attribute users can control which ASPM link-states are enabled/disabled.
> > 
> > Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> > ---
> >  Documentation/ABI/testing/sysfs-bus-pci |  13 ++
> >  drivers/pci/pci.h                       |   8 +-
> >  drivers/pci/pcie/aspm.c                 | 180 +++++++++++++++++++++++-
> >  3 files changed, 193 insertions(+), 8 deletions(-)
> > 
> > diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
> > index 8bfee557e..38fe358de 100644
> > --- a/Documentation/ABI/testing/sysfs-bus-pci
> > +++ b/Documentation/ABI/testing/sysfs-bus-pci
> > @@ -347,3 +347,16 @@ Description:
> >  		If the device has any Peer-to-Peer memory registered, this
> >  	        file contains a '1' if the memory has been published for
> >  		use outside the driver that owns the device.
> > +
> > +What:		/sys/bus/pci/devices/.../power/aspm_link_states
> > +Date:		May 2019
> > +Contact:	Heiner Kallweit <hkallweit1@gmail.com>
> > +Description:
> > +		If ASPM is supported for an endpoint, then this file can be
> > +		used to enable / disable link states. A link state
> > +		displayed in brackets is enabled, otherwise it's disabled.
> > +		To control link states (case insensitive):
> > +		+state : enables a supported state
> > +		-state : disables a state
> > +		none : disables all link states
> > +		all : enables all supported link states
> 
> IIUC this "aspm_link_states" file will contain things like this:
> 
>   L0S L1 L1.1 L1.2                 # All states supported, all disabled
>   [L0S] L1                         # L0s enabled, L1 supported but disabled
>   [L0S] [L1]                       # L0s and L1 enabled
>   ...
> 
> and the control is by writing things like this to it:
> 
>   +L1                              # enables L1
>   +L1.1                            # enables L1.1
>   -L0S                             # disables L0s
> 
> I know this file structure is similar to protocol handling in
> drivers/media/rc/rc-main.c, but Documentation/filesystems/sysfs.txt
> suggests single values in a file, and Greg recently pointed out that
> we screwed up some PCI AER stats [1].
> 
> So I'm thinking maybe we should split this into several files, e.g.,
> 
>   /sys/devices/pci*/.../power/aspm_l0s
>   /sys/devices/pci*/.../power/aspm_l1
>   /sys/devices/pci*/.../power/aspm_l1.1
>   /sys/devices/pci*/.../power/aspm_l1.2
> 
> which would contain just 1/0 values, and we'd write 1/0 to
> enable/disable things.

Yes, that is much simpler for both userspace to handle as well as the
kernel, as no need for parsing anything "special" at all here.

If the file is present, the state is supported, and the kernel code
should be _much_ easier to write and maintain over time.

> Since the L1 PM Substates control register has separate enable bits
> for PCI-PM L1.1 and L1.2, we might also want a way to manage those.

That sounds good as well.

thanks,

greg k-h
Rajat Jain Aug. 20, 2019, 7:05 p.m. UTC | #3
On Tue, Aug 20, 2019 at 3:34 AM Bjorn Helgaas <helgaas@kernel.org> wrote:
>
> [+cc Greg, Rajat]
>
> On Thu, May 23, 2019 at 10:05:35PM +0200, Heiner Kallweit wrote:
> > Background of this extension is a problem with the r8169 network driver.
> > Several combinations of board chipsets and network chip versions have
> > problems if ASPM is enabled, therefore we have to disable ASPM per default.
> > However especially on notebooks ASPM can provide significant power-saving,
> > therefore we want to give users the option to enable ASPM. With the new sysfs
> > attribute users can control which ASPM link-states are enabled/disabled.
> >
> > Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> > ---
> >  Documentation/ABI/testing/sysfs-bus-pci |  13 ++
> >  drivers/pci/pci.h                       |   8 +-
> >  drivers/pci/pcie/aspm.c                 | 180 +++++++++++++++++++++++-
> >  3 files changed, 193 insertions(+), 8 deletions(-)
> >
> > diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
> > index 8bfee557e..38fe358de 100644
> > --- a/Documentation/ABI/testing/sysfs-bus-pci
> > +++ b/Documentation/ABI/testing/sysfs-bus-pci
> > @@ -347,3 +347,16 @@ Description:
> >               If the device has any Peer-to-Peer memory registered, this
> >               file contains a '1' if the memory has been published for
> >               use outside the driver that owns the device.
> > +
> > +What:                /sys/bus/pci/devices/.../power/aspm_link_states
> > +Date:                May 2019
> > +Contact:     Heiner Kallweit <hkallweit1@gmail.com>
> > +Description:
> > +             If ASPM is supported for an endpoint, then this file can be
> > +             used to enable / disable link states. A link state
> > +             displayed in brackets is enabled, otherwise it's disabled.
> > +             To control link states (case insensitive):
> > +             +state : enables a supported state
> > +             -state : disables a state
> > +             none : disables all link states
> > +             all : enables all supported link states
>
> IIUC this "aspm_link_states" file will contain things like this:
>
>   L0S L1 L1.1 L1.2                 # All states supported, all disabled
>   [L0S] L1                         # L0s enabled, L1 supported but disabled
>   [L0S] [L1]                       # L0s and L1 enabled
>   ...
>
> and the control is by writing things like this to it:
>
>   +L1                              # enables L1
>   +L1.1                            # enables L1.1
>   -L0S                             # disables L0s
>
> I know this file structure is similar to protocol handling in
> drivers/media/rc/rc-main.c, but Documentation/filesystems/sysfs.txt
> suggests single values in a file, and Greg recently pointed out that
> we screwed up some PCI AER stats [1].
>
> So I'm thinking maybe we should split this into several files, e.g.,
>
>   /sys/devices/pci*/.../power/aspm_l0s
>   /sys/devices/pci*/.../power/aspm_l1
>   /sys/devices/pci*/.../power/aspm_l1.1
>   /sys/devices/pci*/.../power/aspm_l1.2
>
> which would contain just 1/0 values, and we'd write 1/0 to
> enable/disable things.
>
> Since the L1 PM Substates control register has separate enable bits
> for PCI-PM L1.1 and L1.2, we might also want a way to manage those.
>
> Bjorn
>
> [1] https://lore.kernel.org/r/20190621072911.GA21600@kroah.com


Sorry, this has been sitting on my plate for some time now. I'll work
on it and send out a patch later in this week.

Thanks,

Rajat
Bjorn Helgaas Aug. 20, 2019, 7:32 p.m. UTC | #4
On Tue, Aug 20, 2019 at 12:05:01PM -0700, Rajat Jain wrote:
> On Tue, Aug 20, 2019 at 3:34 AM Bjorn Helgaas <helgaas@kernel.org> wrote:
> > On Thu, May 23, 2019 at 10:05:35PM +0200, Heiner Kallweit wrote:
> > > Background of this extension is a problem with the r8169 network driver.
> > > Several combinations of board chipsets and network chip versions have
> > > problems if ASPM is enabled, therefore we have to disable ASPM per default.
> > > However especially on notebooks ASPM can provide significant power-saving,
> > > therefore we want to give users the option to enable ASPM. With the new sysfs
> > > attribute users can control which ASPM link-states are enabled/disabled.
> > >
> > > Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> > > ---
> > >  Documentation/ABI/testing/sysfs-bus-pci |  13 ++
> > >  drivers/pci/pci.h                       |   8 +-
> > >  drivers/pci/pcie/aspm.c                 | 180 +++++++++++++++++++++++-
> > >  3 files changed, 193 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
> > > index 8bfee557e..38fe358de 100644
> > > --- a/Documentation/ABI/testing/sysfs-bus-pci
> > > +++ b/Documentation/ABI/testing/sysfs-bus-pci
> > > @@ -347,3 +347,16 @@ Description:
> > >               If the device has any Peer-to-Peer memory registered, this
> > >               file contains a '1' if the memory has been published for
> > >               use outside the driver that owns the device.
> > > +
> > > +What:                /sys/bus/pci/devices/.../power/aspm_link_states
> > > +Date:                May 2019
> > > +Contact:     Heiner Kallweit <hkallweit1@gmail.com>
> > > +Description:
> > > +             If ASPM is supported for an endpoint, then this file can be
> > > +             used to enable / disable link states. A link state
> > > +             displayed in brackets is enabled, otherwise it's disabled.
> > > +             To control link states (case insensitive):
> > > +             +state : enables a supported state
> > > +             -state : disables a state
> > > +             none : disables all link states
> > > +             all : enables all supported link states
> >
> > IIUC this "aspm_link_states" file will contain things like this:
> >
> >   L0S L1 L1.1 L1.2                 # All states supported, all disabled
> >   [L0S] L1                         # L0s enabled, L1 supported but disabled
> >   [L0S] [L1]                       # L0s and L1 enabled
> >   ...
> >
> > and the control is by writing things like this to it:
> >
> >   +L1                              # enables L1
> >   +L1.1                            # enables L1.1
> >   -L0S                             # disables L0s
> >
> > I know this file structure is similar to protocol handling in
> > drivers/media/rc/rc-main.c, but Documentation/filesystems/sysfs.txt
> > suggests single values in a file, and Greg recently pointed out that
> > we screwed up some PCI AER stats [1].
> >
> > So I'm thinking maybe we should split this into several files, e.g.,
> >
> >   /sys/devices/pci*/.../power/aspm_l0s
> >   /sys/devices/pci*/.../power/aspm_l1
> >   /sys/devices/pci*/.../power/aspm_l1.1
> >   /sys/devices/pci*/.../power/aspm_l1.2
> >
> > which would contain just 1/0 values, and we'd write 1/0 to
> > enable/disable things.
> >
> > Since the L1 PM Substates control register has separate enable bits
> > for PCI-PM L1.1 and L1.2, we might also want a way to manage those.
> >
> > Bjorn
> >
> > [1] https://lore.kernel.org/r/20190621072911.GA21600@kroah.com
> 
> Sorry, this has been sitting on my plate for some time now. I'll work
> on it and send out a patch later in this week.

Don't worry, I cc'd you mostly as FYI because you're a recent
contributor to aspm.c in this area, though I wouldn't complain about
AER stats sysfs patches either :)

Bjorn
Rajat Jain Aug. 20, 2019, 7:51 p.m. UTC | #5
On Tue, Aug 20, 2019 at 12:32 PM Bjorn Helgaas <helgaas@kernel.org> wrote:
>
> On Tue, Aug 20, 2019 at 12:05:01PM -0700, Rajat Jain wrote:
> > On Tue, Aug 20, 2019 at 3:34 AM Bjorn Helgaas <helgaas@kernel.org> wrote:
> > > On Thu, May 23, 2019 at 10:05:35PM +0200, Heiner Kallweit wrote:
> > > > Background of this extension is a problem with the r8169 network driver.
> > > > Several combinations of board chipsets and network chip versions have
> > > > problems if ASPM is enabled, therefore we have to disable ASPM per default.
> > > > However especially on notebooks ASPM can provide significant power-saving,
> > > > therefore we want to give users the option to enable ASPM. With the new sysfs
> > > > attribute users can control which ASPM link-states are enabled/disabled.
> > > >
> > > > Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> > > > ---
> > > >  Documentation/ABI/testing/sysfs-bus-pci |  13 ++
> > > >  drivers/pci/pci.h                       |   8 +-
> > > >  drivers/pci/pcie/aspm.c                 | 180 +++++++++++++++++++++++-
> > > >  3 files changed, 193 insertions(+), 8 deletions(-)
> > > >
> > > > diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
> > > > index 8bfee557e..38fe358de 100644
> > > > --- a/Documentation/ABI/testing/sysfs-bus-pci
> > > > +++ b/Documentation/ABI/testing/sysfs-bus-pci
> > > > @@ -347,3 +347,16 @@ Description:
> > > >               If the device has any Peer-to-Peer memory registered, this
> > > >               file contains a '1' if the memory has been published for
> > > >               use outside the driver that owns the device.
> > > > +
> > > > +What:                /sys/bus/pci/devices/.../power/aspm_link_states
> > > > +Date:                May 2019
> > > > +Contact:     Heiner Kallweit <hkallweit1@gmail.com>
> > > > +Description:
> > > > +             If ASPM is supported for an endpoint, then this file can be
> > > > +             used to enable / disable link states. A link state
> > > > +             displayed in brackets is enabled, otherwise it's disabled.
> > > > +             To control link states (case insensitive):
> > > > +             +state : enables a supported state
> > > > +             -state : disables a state
> > > > +             none : disables all link states
> > > > +             all : enables all supported link states
> > >
> > > IIUC this "aspm_link_states" file will contain things like this:
> > >
> > >   L0S L1 L1.1 L1.2                 # All states supported, all disabled
> > >   [L0S] L1                         # L0s enabled, L1 supported but disabled
> > >   [L0S] [L1]                       # L0s and L1 enabled
> > >   ...
> > >
> > > and the control is by writing things like this to it:
> > >
> > >   +L1                              # enables L1
> > >   +L1.1                            # enables L1.1
> > >   -L0S                             # disables L0s
> > >
> > > I know this file structure is similar to protocol handling in
> > > drivers/media/rc/rc-main.c, but Documentation/filesystems/sysfs.txt
> > > suggests single values in a file, and Greg recently pointed out that
> > > we screwed up some PCI AER stats [1].
> > >
> > > So I'm thinking maybe we should split this into several files, e.g.,
> > >
> > >   /sys/devices/pci*/.../power/aspm_l0s
> > >   /sys/devices/pci*/.../power/aspm_l1
> > >   /sys/devices/pci*/.../power/aspm_l1.1
> > >   /sys/devices/pci*/.../power/aspm_l1.2
> > >
> > > which would contain just 1/0 values, and we'd write 1/0 to
> > > enable/disable things.
> > >
> > > Since the L1 PM Substates control register has separate enable bits
> > > for PCI-PM L1.1 and L1.2, we might also want a way to manage those.
> > >
> > > Bjorn
> > >
> > > [1] https://lore.kernel.org/r/20190621072911.GA21600@kroah.com
> >
> > Sorry, this has been sitting on my plate for some time now. I'll work
> > on it and send out a patch later in this week.
>
> Don't worry, I cc'd you mostly as FYI because you're a recent
> contributor to aspm.c in this area, though I wouldn't complain about
> AER stats sysfs patches either :)
>

May be we're digressing now, but I'd like to point out that there is
atleast one more file in ASPM that potentially violates the "1 value
per file" rule:

rajatja@rajat2:/sys/module/pcie_aspm/parameters$ cat policy
[default] performance powersave powersupersave
rajatja@rajat2:/sys/module/pcie_aspm/parameters$

... although I would argue in this case that it makes it much clear
what are the allowable values to write, and which is the current
selected one.

Thanks,

Rajat


> Bjorn
Bjorn Helgaas Aug. 20, 2019, 8:48 p.m. UTC | #6
On Tue, Aug 20, 2019 at 12:51:09PM -0700, Rajat Jain wrote:
> 
> May be we're digressing now, but I'd like to point out that there is
> atleast one more file in ASPM that potentially violates the "1 value
> per file" rule:
> 
> rajatja@rajat2:/sys/module/pcie_aspm/parameters$ cat policy
> [default] performance powersave powersupersave
> rajatja@rajat2:/sys/module/pcie_aspm/parameters$
> 
> ... although I would argue in this case that it makes it much clear
> what are the allowable values to write, and which is the current
> selected one.

Huh, that's a good point.  That "policy" file is a little problematic
for several reasons, one being the config options
(CONFIG_PCIEASPM_PERFORMANCE, CONFIG_PCIEASPM_POWERSAVE, etc) that
lock a distro into some default choice.

Maybe there's something we can do there, although there's legacy use
to consider (there are a zillion web pages that document
pcie_aspm/parameters/policy as a way to fix things), and it's
certainly beyond the scope of *this* series.

Bjorn
Rajat Jain Aug. 20, 2019, 8:55 p.m. UTC | #7
On Tue, Aug 20, 2019 at 1:48 PM Bjorn Helgaas <helgaas@kernel.org> wrote:
>
> On Tue, Aug 20, 2019 at 12:51:09PM -0700, Rajat Jain wrote:
> >
> > May be we're digressing now, but I'd like to point out that there is
> > atleast one more file in ASPM that potentially violates the "1 value
> > per file" rule:
> >
> > rajatja@rajat2:/sys/module/pcie_aspm/parameters$ cat policy
> > [default] performance powersave powersupersave
> > rajatja@rajat2:/sys/module/pcie_aspm/parameters$
> >
> > ... although I would argue in this case that it makes it much clear
> > what are the allowable values to write, and which is the current
> > selected one.
>
> Huh, that's a good point.  That "policy" file is a little problematic
> for several reasons, one being the config options
> (CONFIG_PCIEASPM_PERFORMANCE, CONFIG_PCIEASPM_POWERSAVE, etc) that
> lock a distro into some default choice.
>
> Maybe there's something we can do there, although there's legacy use
> to consider (there are a zillion web pages that document
> pcie_aspm/parameters/policy as a way to fix things), and it's
> certainly beyond the scope of *this* series.

Agreed!

>
> Bjorn
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
index 8bfee557e..38fe358de 100644
--- a/Documentation/ABI/testing/sysfs-bus-pci
+++ b/Documentation/ABI/testing/sysfs-bus-pci
@@ -347,3 +347,16 @@  Description:
 		If the device has any Peer-to-Peer memory registered, this
 	        file contains a '1' if the memory has been published for
 		use outside the driver that owns the device.
+
+What:		/sys/bus/pci/devices/.../power/aspm_link_states
+Date:		May 2019
+Contact:	Heiner Kallweit <hkallweit1@gmail.com>
+Description:
+		If ASPM is supported for an endpoint, then this file can be
+		used to enable / disable link states. A link state
+		displayed in brackets is enabled, otherwise it's disabled.
+		To control link states (case insensitive):
+		+state : enables a supported state
+		-state : disables a state
+		none : disables all link states
+		all : enables all supported link states
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 9cb99380c..06642b7de 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -499,17 +499,13 @@  void pcie_aspm_init_link_state(struct pci_dev *pdev);
 void pcie_aspm_exit_link_state(struct pci_dev *pdev);
 void pcie_aspm_pm_state_change(struct pci_dev *pdev);
 void pcie_aspm_powersave_config_link(struct pci_dev *pdev);
+void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev);
+void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev);
 #else
 static inline void pcie_aspm_init_link_state(struct pci_dev *pdev) { }
 static inline void pcie_aspm_exit_link_state(struct pci_dev *pdev) { }
 static inline void pcie_aspm_pm_state_change(struct pci_dev *pdev) { }
 static inline void pcie_aspm_powersave_config_link(struct pci_dev *pdev) { }
-#endif
-
-#ifdef CONFIG_PCIEASPM_DEBUG
-void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev);
-void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev);
-#else
 static inline void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) { }
 static inline void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) { }
 #endif
diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 7847be38e..f3822accb 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -42,6 +42,8 @@ 
 #define ASPM_STATE_ALL		(ASPM_STATE_L0S | ASPM_STATE_L1 |	\
 				 ASPM_STATE_L1SS)
 
+static const char power_group[] = "power";
+
 struct aspm_latency {
 	u32 l0s;			/* L0s latency (nsec) */
 	u32 l1;				/* L1 latency (nsec) */
@@ -1251,38 +1253,212 @@  static ssize_t clk_ctl_store(struct device *dev,
 
 static DEVICE_ATTR_RW(link_state);
 static DEVICE_ATTR_RW(clk_ctl);
+#endif
+
+struct aspm_sysfs_state {
+	const char *name;
+	int mask;
+};
+
+static const struct aspm_sysfs_state aspm_sysfs_states[] = {
+	{ "L0S",	ASPM_STATE_L0S		},
+	{ "L1",		ASPM_STATE_L1		},
+	{ "L1.1",	ASPM_STATE_L1_1_MASK	},
+	{ "L1.2",	ASPM_STATE_L1_2_MASK	},
+};
+
+static struct pcie_link_state *aspm_get_parent_link(struct pci_dev *pdev)
+{
+	struct pci_dev *parent = pdev->bus->self;
+
+	if (pdev->has_secondary_link)
+		parent = pdev;
+
+	return parent ? parent->link_state : NULL;
+}
+
+static bool pcie_check_aspm_endpoint(struct pci_dev *pdev)
+{
+	struct pcie_link_state *link;
+
+	if (pci_pcie_type(pdev) != PCI_EXP_TYPE_ENDPOINT)
+		return false;
+
+	link = aspm_get_parent_link(pdev);
+
+	return link && link->aspm_support;
+}
+
+static ssize_t aspm_link_states_show(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	struct pci_dev *pdev = to_pci_dev(dev);
+	struct pcie_link_state *link;
+	int len = 0, i;
+
+	link = aspm_get_parent_link(pdev);
+	if (!link)
+		return -EOPNOTSUPP;
+
+	mutex_lock(&aspm_lock);
+
+	for (i = 0; i < ARRAY_SIZE(aspm_sysfs_states); i++) {
+		const struct aspm_sysfs_state *st = aspm_sysfs_states + i;
+
+		if (link->aspm_enabled & st->mask)
+			len += scnprintf(buf + len, PAGE_SIZE - len, "[%s] ",
+					 st->name);
+		else
+			len += scnprintf(buf + len, PAGE_SIZE - len, "%s ",
+					 st->name);
+	}
+
+	if (link->clkpm_enabled)
+		len += scnprintf(buf + len, PAGE_SIZE - len, "[CLKPM] ");
+	else
+		len += scnprintf(buf + len, PAGE_SIZE - len, "CLKPM ");
+
+	mutex_unlock(&aspm_lock);
+
+	len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
+
+	return len;
+}
+
+static ssize_t aspm_link_states_store(struct device *dev,
+				      struct device_attribute *attr,
+				      const char *buf, size_t len)
+{
+	struct pci_dev *pdev = to_pci_dev(dev);
+	struct pcie_link_state *link;
+	char *buftmp = (char *)buf, *tok;
+	unsigned int disable_aspm, disable_clkpm;
+	bool first = true, add;
+	int err = 0, i;
+
+	if (aspm_disabled)
+		return -EPERM;
+
+	link = aspm_get_parent_link(pdev);
+	if (!link)
+		return -EOPNOTSUPP;
+
+	down_read(&pci_bus_sem);
+	mutex_lock(&aspm_lock);
+
+	disable_aspm = link->aspm_disable;
+	disable_clkpm = link->clkpm_disable;
+
+	while ((tok = strsep(&buftmp, " \n")) != NULL) {
+		bool found = false;
+
+		if (!*tok)
+			continue;
+
+		if (first) {
+			if (!strcasecmp(tok, "none")) {
+				disable_aspm = ASPM_STATE_ALL;
+				disable_clkpm = 1;
+				break;
+			}
+			if (!strcasecmp(tok, "all")) {
+				disable_aspm = 0;
+				disable_clkpm = 0;
+				break;
+			}
+			first = false;
+		}
+
+		if (*tok != '+' && *tok != '-') {
+			err = -EINVAL;
+			goto out;
+		}
+
+		add = *tok++ == '+';
+
+		for (i = 0; i < ARRAY_SIZE(aspm_sysfs_states); i++) {
+			const struct aspm_sysfs_state *st =
+						aspm_sysfs_states + i;
+
+			if (!strcasecmp(tok, st->name)) {
+				if (add)
+					disable_aspm &= ~st->mask;
+				else
+					disable_aspm |= st->mask;
+				found = true;
+				break;
+			}
+		}
+
+		if (!found && !strcasecmp(tok, "clkpm")) {
+			disable_clkpm = add ? 0 : 1;
+			found = true;
+		}
+
+		if (!found) {
+			err = -EINVAL;
+			goto out;
+		}
+	}
+
+	if (disable_aspm & ASPM_STATE_L1)
+		disable_aspm |= ASPM_STATE_L1SS;
+
+	link->aspm_disable = disable_aspm;
+	link->clkpm_disable = disable_clkpm;
+
+	pcie_config_aspm_link(link, policy_to_aspm_state(link));
+	pcie_set_clkpm(link, policy_to_clkpm_state(link));
+out:
+	mutex_unlock(&aspm_lock);
+	up_read(&pci_bus_sem);
+
+	return err ?: len;
+}
+
+static DEVICE_ATTR_RW(aspm_link_states);
 
-static char power_group[] = "power";
 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
 {
 	struct pcie_link_state *link_state = pdev->link_state;
 
+	if (pcie_check_aspm_endpoint(pdev))
+		sysfs_add_file_to_group(&pdev->dev.kobj,
+			&dev_attr_aspm_link_states.attr, power_group);
+
 	if (!link_state)
 		return;
 
+#ifdef CONFIG_PCIEASPM_DEBUG
 	if (link_state->aspm_support)
 		sysfs_add_file_to_group(&pdev->dev.kobj,
 			&dev_attr_link_state.attr, power_group);
 	if (link_state->clkpm_capable)
 		sysfs_add_file_to_group(&pdev->dev.kobj,
 			&dev_attr_clk_ctl.attr, power_group);
+#endif
 }
 
 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
 {
 	struct pcie_link_state *link_state = pdev->link_state;
 
+	if (pcie_check_aspm_endpoint(pdev))
+		sysfs_remove_file_from_group(&pdev->dev.kobj,
+			&dev_attr_aspm_link_states.attr, power_group);
+
 	if (!link_state)
 		return;
 
+#ifdef CONFIG_PCIEASPM_DEBUG
 	if (link_state->aspm_support)
 		sysfs_remove_file_from_group(&pdev->dev.kobj,
 			&dev_attr_link_state.attr, power_group);
 	if (link_state->clkpm_capable)
 		sysfs_remove_file_from_group(&pdev->dev.kobj,
 			&dev_attr_clk_ctl.attr, power_group);
-}
 #endif
+}
 
 static int __init pcie_aspm_disable(char *str)
 {