1. 18 May, 2012 14 commits
    • Sarah Sharp's avatar
      xhci: Add Intel U1/U2 timeout policy. · e3567d2c
      Sarah Sharp authored
      All Intel xHCI host controllers support USB 3.0 Link Power Management.
      The Panther Point xHCI host controller needs the xHCI driver to
      calculate the U1 and U2 timeout values, because it will blindly accept a
      MEL that would cause scheduling issues.
      The Lynx Point xHCI host controller will reject MEL values that are too
      high, but internally it implements the same algorithm that is needed for
      Panther Point xHCI.
      Simplify the code paths by just having the xHCI driver calculate what
      the U1/U2 timeouts should be.  Comments on the policy are in the code.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      xhci: Add infrastructure for host-specific LPM policies. · 3b3db026
      Sarah Sharp authored
      The choice of U1 and U2 timeouts for USB 3.0 Link Power Management (LPM)
      is highly host controller specific.  Here are a few examples of why it's
      host specific:
       1. Setting the U1/U2 timeout too short may cause the link to go into
          U1/U2 in between service intervals, which some hosts may tolerate,
          and some may not.
       2. The host controller has to modify its bus schedule in order to take
          into account the Maximum Exit Latency (MEL) to bring all the links
          from the host to the device into U0.  If the MEL is too big, and it
          takes too long to bring the links into an active state, the host
          controller may not be able to service periodic endpoints in time.
       3. Host controllers may also have scheduling limitations that force
          them to disable U1 or U2 if a USB device is behind too many tiers of
      We could take an educated guess at what U1/U2 timeouts may work for a
      particular host controller.  However, that would result in a binary
      search on every new configuration or alt setting installation, with
      multiple failed Evaluate Context commands.  Worse, the host may blindly
      accept the timeouts and just fail to update its schedule for U1/U2 exit
      latencies, which could result in randomly delayed periodic transfers.
      Since we don't want to cause jitter in periodic transfers, or delay
      config/alt setting changes too much, lay down a framework that xHCI
      vendors can extend in order to add their own U1/U2 timeout policies.
      To extend the framework, they will need to:
       - Modify the PCI init code to add a new xhci->quirk for their host, and
         set the XHCI_LPM_SUPPORT quirk flag.
       - Add their own vendor-specific hooks, like the ones that will be added
         in xhci_call_host_update_timeout_for_endpoint() and
       - Make the LPM enable/disable methods call those functions based on the
         xhci->quirk for their host.
      An example will be provided for the Intel xHCI host controller in the
      next patch.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      USB: Add macros for interrupt endpoint types. · 6538eafc
      Sarah Sharp authored
      The USB 3.0 spec defines a new way of differentiating interrupt
      endpoints.  The idea is that some interrupt endpoints are used for
      notifications, i.e. they continually NAK the transfer until something
      changes on the device.  Other interrupt endpoints are used as a way to
      periodically transfer data.
      The USB 3.0 endpoint descriptor uses bits 5:4 of bmAttributes for
      interrupt endpoints, to define the endpoint as either a Notification
      endpoint, or a Periodic endpoint.  Introduce macros to dig out that
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      xhci: Reserve one command for USB3 LPM disable. · dbc33303
      Sarah Sharp authored
      We want to do everything we can to ensure that USB 3.0 Link Power
      Management (LPM) can be disabled when it is enabled.  If LPM can't be
      disabled, we can't suspend USB 3.0 devices, or reset them.  To make sure
      we can submit the command to disable LPM, allocate a command in the
      xhci_hcd structure, and reserve one TRB on the command ring.
      We only need one command per xHCI driver instance, because LPM is only
      disabled or enabled while the USB core is holding the bandwidth_mutex
      that is shared between the xHCI USB 2.0 and USB 3.0 roothubs.  The
      bandwidth_mutex will be held until the command completes, or times out.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      xhci: Some Evaluate Context commands must succeed. · 4b266541
      Sarah Sharp authored
      The upcoming USB 3.0 Link PM patches will introduce new API to enable
      and disable low-power link states.  We must be able to disable LPM in
      order to reset a device, or place the device into U3 (device suspend).
      Therefore, we need to make sure the Evaluate Context command to disable
      the LPM timeouts can't fail due to there being no room on the command
      Introduce a new flag to the function that queues the Evaluate Context
      command, command_must_succeed.  This tells the ring handler that a TRB
      has already been reserved for the command (by incrementing
      xhci->cmd_ring_reserved_trbs), and basically ensures that prepare_ring()
      won't fail.  A similar flag was already implemented for the Configure
      Endpoint command queuing function.
      All functions that currently call xhci_configure_endpoint() to issue an
      Evaluate Context command pass "false" for the "must_succeed" parameter,
      so this patch should have no effect on current xHCI driver behavior.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      USB: Disable USB 3.0 LPM in critical sections. · 8306095f
      Sarah Sharp authored
      There are several places where the USB core needs to disable USB 3.0
      Link PM:
       - usb_bind_interface
       - usb_unbind_interface
       - usb_driver_claim_interface
       - usb_port_suspend/usb_port_resume
       - usb_reset_and_verify_device
       - usb_set_interface
       - usb_reset_configuration
       - usb_set_configuration
      Use the new LPM disable/enable functions to temporarily disable LPM
      around these critical sections.
      We need to protect the critical section around binding and unbinding USB
      interface drivers.  USB drivers may want to disable hub-initiated USB
      3.0 LPM, which will change the value of the U1/U2 timeouts that the xHCI
      driver will install.  We need to disable LPM completely until the driver
      is bound to the interface, and the driver has a chance to enable
      whatever alternate interface setting it needs in its probe routine.
      Then re-enable USB3 LPM, and recalculate the U1/U2 timeout values.
      We also need to disable LPM in usb_driver_claim_interface,
      because drivers like usbfs can bind to an interface through that
      function.  Note, there is no way currently for userspace drivers to
      disable hub-initiated USB 3.0 LPM.  Revisit this later.
      When a driver is unbound, the U1/U2 timeouts may change because we are
      unbinding the last driver that needed hub-initiated USB 3.0 LPM to be
      USB LPM must be disabled when a USB device is going to be suspended.
      The USB 3.0 spec does not define a state transition from U1 or U2 into
      U3, so we need to bring the device into U0 by disabling LPM before we
      can place it into U3.  Therefore, call usb_unlocked_disable_lpm() in
      usb_port_suspend(), and call usb_unlocked_enable_lpm() in
      usb_port_resume().  If the port suspend fails, make sure to re-enable
      LPM by calling usb_unlocked_enable_lpm(), since usb_port_resume() will
      not be called on a failed port suspend.
      USB 3.0 devices lose their USB 3.0 LPM settings (including whether USB
      device-initiated LPM is enabled) across device suspend.  Therefore,
      disable LPM before the device will be reset in
      usb_reset_and_verify_device(), and re-enable LPM after the reset is
      complete and the configuration/alt settings are re-installed.
      The calculated U1/U2 timeout values are heavily dependent on what USB
      device endpoints are currently enabled.  When any of the enabled
      endpoints on the device might change, due to a new configuration, or new
      alternate interface setting, we need to first disable USB 3.0 LPM, add
      or delete endpoints from the xHCI schedule, install the new interfaces
      and alt settings, and then re-enable LPM.  Do this in usb_set_interface,
      usb_reset_configuration, and usb_set_configuration.
      Basically, there is a call to disable and then enable LPM in all
      functions that lock the bandwidth_mutex.  One exception is
      usb_disable_device, because the device is disconnecting or otherwise
      going away, and we should not care about whether USB 3.0 LPM is enabled.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      USB: Add support to enable/disable USB3 link states. · 1ea7e0e8
      Sarah Sharp authored
      There are various functions within the USB core that will need to
      disable USB 3.0 link power states.  For example, when a USB device
      driver is being bound to an interface, we need to disable USB 3.0 LPM
      until we know if the driver will allow hub-initiated LPM transitions.
      Another example is when the USB core is switching alternate interface
      settings.  The USB 3.0 timeout values are dependent on what endpoints
      are enabled, so we want to ensure that LPM is disabled until the new alt
      setting is fully installed.
      Multiple functions need to disable LPM, and those functions can even be
      nested.  For example, usb_bind_interface() could disable LPM, and then
      call into the driver probe function, which may attempt to switch to a
      different alt setting.  Therefore, we need to keep a count of the number
      of functions that require LPM to be disabled at any point in time.
      Introduce two new USB core API calls, usb_disable_lpm() and
      usb_enable_lpm().  These functions increment and decrement a new
      variable in the usb_device, lpm_disable_count.  If usb_disable_lpm()
      fails, it will call usb_enable_lpm() in order to balance the
      These two new functions must be called with the bandwidth_mutex locked.
      If the bandwidth_mutex is not already held by the caller, it should
      instead call usb_unlocked_disable_lpm() and usb_enable_lpm(), which take
      the bandwidth_mutex before calling usb_disable_lpm() and
      usb_enable_lpm(), respectively.
      Introduce a new variable (timeout) in the usb3_lpm_params structure to
      keep track of the currently enabled U1/U2 timeout values.  When
      usb_disable_lpm() is called, and the USB device has the U1 or U2
      timeouts set to a non-zero value (meaning either device-initiated or
      hub-initiated LPM is enabled), attempt to disable LPM, regardless of the
      state of the lpm_disable_count.  We want to ensure that all callers can
      be guaranteed that LPM is disabled if usb_disable_lpm() returns zero.
      Otherwise the following scenario could occur:
      1. Driver A is being bound to interface 1.  usb_probe_interface()
      disables LPM.  Driver A doesn't care if hub-initiated LPM is enabled, so
      even though usb_disable_lpm() fails, the probe of the driver continues,
      and the bandwidth mutex is dropped.
      2. Meanwhile, Driver B is being bound to interface 2.
      usb_probe_interface() grabs the bandwidth mutex and calls
      usb_disable_lpm().  That call should attempt to disable LPM, even
      though the lpm_disable_count is set to 1 by Driver A.
      For usb_enable_lpm(), we attempt to enable LPM only when the
      lpm_disable_count is zero.  If some step in enabling LPM fails, it will
      only have a minimal impact on power consumption, and all USB device
      drivers should still work properly.  Therefore don't bother to return
      any error codes.
      Don't enable device-initiated LPM if the device is unconfigured.  The
      USB device will only accept the U1/U2_ENABLE control transfers in the
      configured state.  Do enable hub-initiated LPM in that case, since
      devices are allowed to accept the LGO_Ux link commands in any state.
      Don't enable or disable LPM if the device is marked as not being LPM
      capable.  This can happen if:
       - the USB device doesn't have a SS BOS descriptor,
       - the device's parent hub has a zeroed bHeaderDecodeLatency value, or
       - the xHCI host doesn't support LPM.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
      Cc: Andiry Xu <andiry.xu@amd.com>
      Cc: Alan Stern <stern@rowland.harvard.edu>
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      USB: Allow drivers to disable hub-initiated LPM. · 8afa408c
      Sarah Sharp authored
      USB 3.0 Link Power Management (LPM) is designed to allow individual
      links in the bus to go into lower power states.  There are two ways a
      link can enter a lower power state:
      1. Device-initiated LPM.  When a USB device decides it can go into a
      lower power link state, it sends a message to the parent hub, telling it
      to go into either U1 or U2.  Device-initiated LPM is good for devices
      that send data to the host, like communications devices.
      2. Hub-initiated LPM.  After the link has been idle for a specific
      amount of time, the parent hub will request that the child go into a
      lower power state.  The child can refuse that request.  For example, a
      USB modem may want to refuse the LPM request if it is in the middle of
      receiving a text message.  Hub-initiated LPM is good for devices where
      only the host initiates the data transfer, like USB printers or USB mass
      storage devices.
      Links will be automatically placed into higher power states by the USB
      hubs and roothubs whenever the host starts a USB transmission.
      Introduce a new usb_driver flag, disable_hub_initiated_lpm, that allows
      drivers to disable hub-initiated LPM.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
      Cc: Marcel Holtmann <marcel@holtmann.org>
      Cc: Gustavo Padovan <gustavo@padovan.org>
      Cc: Johan Hedberg <johan.hedberg@gmail.com>
      Cc: Hansjoerg Lipp <hjlipp@web.de>
      Cc: Tilman Schmidt <tilman@imap.cc>
      Cc: Karsten Keil <isdn@linux-pingi.de>
      Cc: Oliver Neukum <oliver@neukum.name>
      Cc: Peter Korsgaard <jacmet@sunsite.dk>
      Cc: Jan Dumon <j.dumon@option.com>
      Cc: Petko Manolov <petkan@users.sourceforge.net>
      Cc: Steve Glendinning <steve.glendinning@smsc.com>
      Cc: "John W. Linville" <linville@tuxdriver.com>
      Cc: Kalle Valo <kvalo@qca.qualcomm.com>
      Cc: "Luis R. Rodriguez" <mcgrof@qca.qualcomm.com>
      Cc: Jouni Malinen <jouni@qca.qualcomm.com>
      Cc: Vasanthakumar Thiagarajan <vthiagar@qca.qualcomm.com>
      Cc: Senthil Balasubramanian <senthilb@qca.qualcomm.com>
      Cc: Christian Lamparter <chunkeey@googlemail.com>
      Cc: Brett Rudley <brudley@broadcom.com>
      Cc: Roland Vossen <rvossen@broadcom.com>
      Cc: Arend van Spriel <arend@broadcom.com>
      Cc: "Franky (Zhenhui) Lin" <frankyl@broadcom.com>
      Cc: Kan Yan <kanyan@broadcom.com>
      Cc: Dan Williams <dcbw@redhat.com>
      Cc: Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
      Cc: Ivo van Doorn <IvDoorn@gmail.com>
      Cc: Gertjan van Wingerde <gwingerde@gmail.com>
      Cc: Helmut Schaa <helmut.schaa@googlemail.com>
      Cc: Herton Ronaldo Krzesinski <herton@canonical.com>
      Cc: Hin-Tak Leung <htl10@users.sourceforge.net>
      Cc: Larry Finger <Larry.Finger@lwfinger.net>
      Cc: Chaoming Li <chaoming_li@realsil.com.cn>
      Cc: Daniel Drake <dsd@gentoo.org>
      Cc: Ulrich Kunitz <kune@deine-taler.de>
      Cc: linux-bluetooth@vger.kernel.org
      Cc: gigaset307x-common@lists.sourceforge.net
      Cc: netdev@vger.kernel.org
      Cc: linux-usb@vger.kernel.org
      Cc: linux-wireless@vger.kernel.org
      Cc: ath9k-devel@lists.ath9k.org
      Cc: libertas-dev@lists.infradead.org
      Cc: users@rt2x00.serialmonkey.com
    • Sarah Sharp's avatar
      USB: Calculate USB 3.0 exit latencies for LPM. · 51e0a012
      Sarah Sharp authored
      There are several different exit latencies associated with coming out of
      the U1 or U2 lower power link state.
      Device Exit Latency (DEL) is the maximum time it takes for the USB
      device to bring its upstream link into U0.  That can be found in the
      SuperSpeed Extended Capabilities BOS descriptor for the device.  The
      time it takes for a particular link in the tree to exit to U0 is the
      maximum of either the parent hub's U1/U2 DEL, or the child's U1/U2 DEL.
      Hubs introduce a further delay that effects how long it takes a child
      device to transition to U0.  When a USB 3.0 hub receives a header
      packet, it takes some time to decode that header and figure out which
      downstream port the packet was destined for.  If the port is not in U0,
      this hub header decode latency will cause an additional delay for
      bringing the child device to U0.  This Hub Header Decode Latency is
      found in the USB 3.0 hub descriptor.
      We can use DEL and the header decode latency, along with additional
      latencies imposed by each additional hub tier, to figure out the exit
      latencies for both host-initiated and device-initiated exit to U0.
      The Max Exit Latency (MEL) is the worst-case time it will take for a
      host-initiated exit to U0, based on whether U1 or U2 link states are
      enabled.  The ping or packet must traverse the path to the device, and
      each hub along the way incurs the hub header decode latency in order to
      figure out which device the transfer was bound for.  We say worst-case,
      because some hubs may not be in the lowest link state that is enabled.
      See the examples in section C.2.2.1.
      Note that "HSD" is a "host specific delay" that the power appendix
      architect has not been able to tell me how to calculate.  There's no way
      to get HSD from the xHCI registers either, so I'm simply ignoring it.
      The Path Exit Latency (PEL) is the worst-case time it will take for a
      device-initiate exit to U0 to place all the links from the device to the
      host into U0.
      The System Exit Latency (SEL) is another device-initiated exit latency.
      SEL is useful for USB 3.0 devices that need to send data to the host at
      specific intervals.  The device may send an NRDY to indicate it isn't
      ready to send data, then put its link into a lower power state.  If it
      needs to have that data transmitted at a specific time, it can use SEL
      to back calculate when it will need to bring the link back into U0 to
      meet its deadlines.
      SEL is the worst-case time from the device-initiated exit to U0, to when
      the device will receive a packet from the host controller.  It includes
      PEL, the time it takes for an ERDY to get to the host, a host-specific
      delay for the host to process that ERDY, and the time it takes for the
      packet to traverse the path to the device.  See Figure C-2 in the USB
      3.0 bus specification.
      Note: I have not been able to get good answers about what the
      host-specific delay to process the ERDY should be.  The Intel HW
      developers say it will be specific to the platform the xHCI host is
      integrated into, and they say it's negligible.  Ignore this too.
      Separate from these four exit latencies are the U1/U2 timeout values we
      program into the parent hubs.  These timeouts tell the hub to attempt to
      place the device into a lower power link state after the link has been
      idle for that amount of time.
      Create two arrays (one for U1 and one for U2) to store mel, pel, sel,
      and the timeout values.  Store the exit latency values in nanosecond
      units, since that's the smallest units used (DEL is in us, but the Hub
      Header Decode Latency is in ns).
      If a USB 3.0 device doesn't have a SuperSpeed Extended Capabilities BOS
      descriptor, it's highly unlikely it will be able to handle LPM requests
      properly.  So it's best to disable LPM for devices that don't have this
      descriptor, and any children beneath it, if it's a USB 3.0 hub.  Warn
      users when that happens, since it means they have a non-compliant USB
      3.0 device or hub.
      This patch assumes a simplified design where links deep in the tree will
      not have U1 or U2 enabled unless all their parent links have the
      corresponding LPM state enabled.  Eventually, we might want to allow a
      different policy, and we can revisit this patch when that happens.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
      Cc: Alan Stern <stern@rowland.harvard.edu>
    • Sarah Sharp's avatar
      USB: Refactor code to set LPM support flag. · d9b2099c
      Sarah Sharp authored
      Refactor the code that sets the usb_device flag to indicate the device
      support link power management (lpm_capable).  The current code sets
      lpm_capable unconditionally if the USB devices have a USB 2.0 Extended
      Capabilities Descriptor.  USB 3.0 devices can also have that descriptor,
      but the xHCI driver code that uses lpm_capable will not run the USB 2.0
      LPM test for devices under the USB 3.0 roothub.  Therefore, it's fine
      only set lpm_capable for high speed devices in this refactoring.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      USB: Make sure to fetch the BOS desc for roothubs. · 448b6eb1
      Sarah Sharp authored
      The BOS descriptor is normally fetched and stored in the usb_device->bos
      during enumeration.  USB 3.0 roothubs don't undergo enumeration, but we
      need them to have a BOS descriptor, since each xHCI host has a different
      U1 and U2 exit latency.  Make sure to fetch the BOS descriptor for USB
      3.0 roothubs.  It will be freed when the roothub usb_device is released.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
      Cc: Andiry Xu <andiry.xu@amd.com>
    • Sarah Sharp's avatar
      xhci: Add roothub code to set U1/U2 timeouts. · 797b0ca5
      Sarah Sharp authored
      USB 3.0 hubs can be put into a mode where the hub can automatically
      request that the link go into a deeper link power state after the link
      has been idle for a specified amount of time.  Each of the new USB 3.0
      link states (U1 and U2) have their own timeout that can be programmed
      per port.
      Change the xHCI roothub emulation code to handle the request to set the
      U1 and U2 timeouts.
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
    • Sarah Sharp's avatar
      xhci: Reset reserved command ring TRBs on cleanup. · 33b2831a
      Sarah Sharp authored
      When the xHCI driver needs to clean up memory (perhaps due to a failed
      register restore on resume from S3 or resume from S4), it needs to reset
      the number of reserved TRBs on the command ring to zero.  Otherwise,
      several resume cycles (about 30) with a UAS device attached will
      continually increment the number of reserved TRBs, until all command
      submissions fail because there isn't enough room on the command ring.
      This patch should be backported to kernels as old as 2.6.32,
      that contain the commit 913a8a34
      "USB: xhci: Change how xHCI commands are handled."
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
      Cc: stable@vger.kernel.org
    • Oliver Neukum's avatar
      USB: fix resource leak in xhci power loss path · f8a9e72d
      Oliver Neukum authored
      Some more data structures must be freed and counters
      reset if an XHCI controller has lost power. The failure
      to do so renders some chips inoperative after a certain number
      of S4 cycles.
      This patch should be backported to kernels as old as 3.2,
      that contain the commits c29eea62
      "xhci: Implement HS/FS/LS bandwidth checking." and
      commit 839c817c
      "xhci: Implement HS/FS/LS bandwidth checking."
      Signed-off-by: default avatarOliver Neukum <oneukum@suse.de>
      Signed-off-by: default avatarSarah Sharp <sarah.a.sharp@linux.intel.com>
      Cc: stable@vger.kernel.org
  2. 17 May, 2012 6 commits
  3. 16 May, 2012 12 commits
  4. 15 May, 2012 8 commits