Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Linux/drivers/usb/host/   (Open Source Betriebssystem Version 6.17.9©)  Datei vom 24.10.2025 mit Größe 166 kB image not shown  

Quelle  xhci.c   Sprache: C

 
// SPDX-License-Identifier: GPL-2.0
/*
 * xHCI host controller driver
 *
 * Copyright (C) 2008 Intel Corp.
 *
 * Author: Sarah Sharp
 * Some code borrowed from the Linux EHCI driver.
 */


#include <linux/jiffies.h>
#include <linux/pci.h>
#include <linux/iommu.h>
#include <linux/iopoll.h>
#include <linux/irq.h>
#include <linux/log2.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/string_choices.h>
#include <linux/dmi.h>
#include <linux/dma-mapping.h>
#include <linux/usb/xhci-sideband.h>

#include "xhci.h"
#include "xhci-trace.h"
#include "xhci-debugfs.h"
#include "xhci-dbgcap.h"

#define DRIVER_AUTHOR "Sarah Sharp"
#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"

#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)

/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
static int link_quirk;
module_param(link_quirk, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");

static unsigned long long quirks;
module_param(quirks, ullong, S_IRUGO);
MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");

static bool td_on_ring(struct xhci_td *td, struct xhci_ring *ring)
{
 struct xhci_segment *seg;

 if (!td || !td->start_seg)
  return false;

 xhci_for_each_ring_seg(ring->first_seg, seg) {
  if (seg == td->start_seg)
   return true;
 }

 return false;
}

/*
 * xhci_handshake - spin reading hc until handshake completes or fails
 * @ptr: address of hc register to be read
 * @mask: bits to look at in result of read
 * @done: value of those bits when handshake succeeds
 * @usec: timeout in microseconds
 *
 * Returns negative errno, or zero on success
 *
 * Success happens when the "mask" bits have the specified value (hardware
 * handshake done).  There are two failure modes:  "usec" have passed (major
 * hardware flakeout), or the register reads as all-ones (hardware removed).
 */

int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, u64 timeout_us)
{
 u32 result;
 int ret;

 ret = readl_poll_timeout_atomic(ptr, result,
     (result & mask) == done ||
     result == U32_MAX,
     1, timeout_us);
 if (result == U32_MAX)  /* card removed */
  return -ENODEV;

 return ret;
}

/*
 * Disable interrupts and begin the xHCI halting process.
 */

void xhci_quiesce(struct xhci_hcd *xhci)
{
 u32 halted;
 u32 cmd;
 u32 mask;

 mask = ~(XHCI_IRQS);
 halted = readl(&xhci->op_regs->status) & STS_HALT;
 if (!halted)
  mask &= ~CMD_RUN;

 cmd = readl(&xhci->op_regs->command);
 cmd &= mask;
 writel(cmd, &xhci->op_regs->command);
}

/*
 * Force HC into halt state.
 *
 * Disable any IRQs and clear the run/stop bit.
 * HC will complete any current and actively pipelined transactions, and
 * should halt within 16 ms of the run/stop bit being cleared.
 * Read HC Halted bit in the status register to see when the HC is finished.
 */

int xhci_halt(struct xhci_hcd *xhci)
{
 int ret;

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
 xhci_quiesce(xhci);

 ret = xhci_handshake(&xhci->op_regs->status,
   STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
 if (ret) {
  if (!(xhci->xhc_state & XHCI_STATE_DYING))
   xhci_warn(xhci, "Host halt failed, %d\n", ret);
  return ret;
 }

 xhci->xhc_state |= XHCI_STATE_HALTED;
 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;

 return ret;
}

/*
 * Set the run bit and wait for the host to be running.
 */

int xhci_start(struct xhci_hcd *xhci)
{
 u32 temp;
 int ret;

 temp = readl(&xhci->op_regs->command);
 temp |= (CMD_RUN);
 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
   temp);
 writel(temp, &xhci->op_regs->command);

 /*
 * Wait for the HCHalted Status bit to be 0 to indicate the host is
 * running.
 */

 ret = xhci_handshake(&xhci->op_regs->status,
   STS_HALT, 0, XHCI_MAX_HALT_USEC);
 if (ret == -ETIMEDOUT)
  xhci_err(xhci, "Host took too long to start, "
    "waited %u microseconds.\n",
    XHCI_MAX_HALT_USEC);
 if (!ret) {
  /* clear state flags. Including dying, halted or removing */
  xhci->xhc_state = 0;
  xhci->run_graceperiod = jiffies + msecs_to_jiffies(500);
 }

 return ret;
}

/*
 * Reset a halted HC.
 *
 * This resets pipelines, timers, counters, state machines, etc.
 * Transactions will be terminated immediately, and operational registers
 * will be set to their defaults.
 */

int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)
{
 u32 command;
 u32 state;
 int ret;

 state = readl(&xhci->op_regs->status);

 if (state == ~(u32)0) {
  if (!(xhci->xhc_state & XHCI_STATE_DYING))
   xhci_warn(xhci, "Host not accessible, reset failed.\n");
  return -ENODEV;
 }

 if ((state & STS_HALT) == 0) {
  xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
  return 0;
 }

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
 command = readl(&xhci->op_regs->command);
 command |= CMD_RESET;
 writel(command, &xhci->op_regs->command);

 /* Existing Intel xHCI controllers require a delay of 1 mS,
 * after setting the CMD_RESET bit, and before accessing any
 * HC registers. This allows the HC to complete the
 * reset operation and be ready for HC register access.
 * Without this delay, the subsequent HC register access,
 * may result in a system hang very rarely.
 */

 if (xhci->quirks & XHCI_INTEL_HOST)
  udelay(1000);

 ret = xhci_handshake(&xhci->op_regs->command, CMD_RESET, 0, timeout_us);
 if (ret)
  return ret;

 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
  usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));

 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
    "Wait for controller to be ready for doorbell rings");
 /*
 * xHCI cannot write to any doorbells or operational registers other
 * than status until the "Controller Not Ready" flag is cleared.
 */

 ret = xhci_handshake(&xhci->op_regs->status, STS_CNR, 0, timeout_us);

 xhci->usb2_rhub.bus_state.port_c_suspend = 0;
 xhci->usb2_rhub.bus_state.suspended_ports = 0;
 xhci->usb2_rhub.bus_state.resuming_ports = 0;
 xhci->usb3_rhub.bus_state.port_c_suspend = 0;
 xhci->usb3_rhub.bus_state.suspended_ports = 0;
 xhci->usb3_rhub.bus_state.resuming_ports = 0;

 return ret;
}

static void xhci_zero_64b_regs(struct xhci_hcd *xhci)
{
 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
 struct iommu_domain *domain;
 int err, i;
 u64 val;
 u32 intrs;

 /*
 * Some Renesas controllers get into a weird state if they are
 * reset while programmed with 64bit addresses (they will preserve
 * the top half of the address in internal, non visible
 * registers). You end up with half the address coming from the
 * kernel, and the other half coming from the firmware. Also,
 * changing the programming leads to extra accesses even if the
 * controller is supposed to be halted. The controller ends up with
 * a fatal fault, and is then ripe for being properly reset.
 *
 * Special care is taken to only apply this if the device is behind
 * an iommu. Doing anything when there is no iommu is definitely
 * unsafe...
 */

 domain = iommu_get_domain_for_dev(dev);
 if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !domain ||
     domain->type == IOMMU_DOMAIN_IDENTITY)
  return;

 xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n");

 /* Clear HSEIE so that faults do not get signaled */
 val = readl(&xhci->op_regs->command);
 val &= ~CMD_HSEIE;
 writel(val, &xhci->op_regs->command);

 /* Clear HSE (aka FATAL) */
 val = readl(&xhci->op_regs->status);
 val |= STS_FATAL;
 writel(val, &xhci->op_regs->status);

 /* Now zero the registers, and brace for impact */
 val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
 if (upper_32_bits(val))
  xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
 val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
 if (upper_32_bits(val))
  xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);

 intrs = min_t(u32, HCS_MAX_INTRS(xhci->hcs_params1),
        ARRAY_SIZE(xhci->run_regs->ir_set));

 for (i = 0; i < intrs; i++) {
  struct xhci_intr_reg __iomem *ir;

  ir = &xhci->run_regs->ir_set[i];
  val = xhci_read_64(xhci, &ir->erst_base);
  if (upper_32_bits(val))
   xhci_write_64(xhci, 0, &ir->erst_base);
  val= xhci_read_64(xhci, &ir->erst_dequeue);
  if (upper_32_bits(val))
   xhci_write_64(xhci, 0, &ir->erst_dequeue);
 }

 /* Wait for the fault to appear. It will be cleared on reset */
 err = xhci_handshake(&xhci->op_regs->status,
        STS_FATAL, STS_FATAL,
        XHCI_MAX_HALT_USEC);
 if (!err)
  xhci_info(xhci, "Fault detected\n");
}

int xhci_enable_interrupter(struct xhci_interrupter *ir)
{
 u32 iman;

 if (!ir || !ir->ir_set)
  return -EINVAL;

 iman = readl(&ir->ir_set->iman);
 iman &= ~IMAN_IP;
 iman |= IMAN_IE;
 writel(iman, &ir->ir_set->iman);

 /* Read operation to guarantee the write has been flushed from posted buffers */
 readl(&ir->ir_set->iman);
 return 0;
}

int xhci_disable_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
{
 u32 iman;

 if (!ir || !ir->ir_set)
  return -EINVAL;

 iman = readl(&ir->ir_set->iman);
 iman &= ~IMAN_IP;
 iman &= ~IMAN_IE;
 writel(iman, &ir->ir_set->iman);

 iman = readl(&ir->ir_set->iman);
 if (iman & IMAN_IP)
  xhci_dbg(xhci, "%s: Interrupt pending\n", __func__);

 return 0;
}

/* interrupt moderation interval imod_interval in nanoseconds */
int xhci_set_interrupter_moderation(struct xhci_interrupter *ir,
        u32 imod_interval)
{
 u32 imod;

 if (!ir || !ir->ir_set)
  return -EINVAL;

 /* IMODI value in IMOD register is in 250ns increments */
 imod_interval = umin(imod_interval / 250, IMODI_MASK);

 imod = readl(&ir->ir_set->imod);
 imod &= ~IMODI_MASK;
 imod |= imod_interval;
 writel(imod, &ir->ir_set->imod);

 return 0;
}

static void compliance_mode_recovery(struct timer_list *t)
{
 struct xhci_hcd *xhci;
 struct usb_hcd *hcd;
 struct xhci_hub *rhub;
 u32 temp;
 int i;

 xhci = timer_container_of(xhci, t, comp_mode_recovery_timer);
 rhub = &xhci->usb3_rhub;
 hcd = rhub->hcd;

 if (!hcd)
  return;

 for (i = 0; i < rhub->num_ports; i++) {
  temp = readl(rhub->ports[i]->addr);
  if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
   /*
 * Compliance Mode Detected. Letting USB Core
 * handle the Warm Reset
 */

   xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
     "Compliance mode detected->port %d",
     i + 1);
   xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
     "Attempting compliance mode recovery");

   if (hcd->state == HC_STATE_SUSPENDED)
    usb_hcd_resume_root_hub(hcd);

   usb_hcd_poll_rh_status(hcd);
  }
 }

 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1))
  mod_timer(&xhci->comp_mode_recovery_timer,
   jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
}

/*
 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
 * that causes ports behind that hardware to enter compliance mode sometimes.
 * The quirk creates a timer that polls every 2 seconds the link state of
 * each host controller's port and recovers it by issuing a Warm reset
 * if Compliance mode is detected, otherwise the port will become "dead" (no
 * device connections or disconnections will be detected anymore). Becasue no
 * status event is generated when entering compliance mode (per xhci spec),
 * this quirk is needed on systems that have the failing hardware installed.
 */

static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
{
 xhci->port_status_u0 = 0;
 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery,
      0);
 xhci->comp_mode_recovery_timer.expires = jiffies +
   msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);

 add_timer(&xhci->comp_mode_recovery_timer);
 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
   "Compliance mode recovery timer initialized");
}

/*
 * This function identifies the systems that have installed the SN65LVPE502CP
 * USB3.0 re-driver and that need the Compliance Mode Quirk.
 * Systems:
 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
 */

static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
{
 const char *dmi_product_name, *dmi_sys_vendor;

 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
 if (!dmi_product_name || !dmi_sys_vendor)
  return false;

 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
  return false;

 if (strstr(dmi_product_name, "Z420") ||
   strstr(dmi_product_name, "Z620") ||
   strstr(dmi_product_name, "Z820") ||
   strstr(dmi_product_name, "Z1 Workstation"))
  return true;

 return false;
}

static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
{
 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1));
}

static void xhci_hcd_page_size(struct xhci_hcd *xhci)
{
 u32 page_size;

 page_size = readl(&xhci->op_regs->page_size) & XHCI_PAGE_SIZE_MASK;
 if (!is_power_of_2(page_size)) {
  xhci_warn(xhci, "Invalid page size register = 0x%x\n", page_size);
  /* Fallback to 4K page size, since that's common */
  page_size = 1;
 }

 xhci->page_size = page_size << 12;
 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "HCD page size set to %iK",
         xhci->page_size >> 10);
}

static void xhci_enable_max_dev_slots(struct xhci_hcd *xhci)
{
 u32 config_reg;
 u32 max_slots;

 max_slots = HCS_MAX_SLOTS(xhci->hcs_params1);
 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xHC can handle at most %d device slots",
         max_slots);

 config_reg = readl(&xhci->op_regs->config_reg);
 config_reg &= ~HCS_SLOTS_MASK;
 config_reg |= max_slots;

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Setting Max device slots reg = 0x%x",
         config_reg);
 writel(config_reg, &xhci->op_regs->config_reg);
}

static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
{
 dma_addr_t deq_dma;
 u64 crcr;

 deq_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, xhci->cmd_ring->dequeue);
 deq_dma &= CMD_RING_PTR_MASK;

 crcr = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
 crcr &= ~CMD_RING_PTR_MASK;
 crcr |= deq_dma;

 crcr &= ~CMD_RING_CYCLE;
 crcr |= xhci->cmd_ring->cycle_state;

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Setting command ring address to 0x%llx", crcr);
 xhci_write_64(xhci, crcr, &xhci->op_regs->cmd_ring);
}

static void xhci_set_doorbell_ptr(struct xhci_hcd *xhci)
{
 u32 offset;

 offset = readl(&xhci->cap_regs->db_off) & DBOFF_MASK;
 xhci->dba = (void __iomem *)xhci->cap_regs + offset;
 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
         "Doorbell array is located at offset 0x%x from cap regs base addr", offset);
}

/*
 * Enable USB 3.0 device notifications for function remote wake, which is necessary
 * for allowing USB 3.0 devices to do remote wakeup from U3 (device suspend).
 */

static void xhci_set_dev_notifications(struct xhci_hcd *xhci)
{
 u32 dev_notf;

 dev_notf = readl(&xhci->op_regs->dev_notification);
 dev_notf &= ~DEV_NOTE_MASK;
 dev_notf |= DEV_NOTE_FWAKE;
 writel(dev_notf, &xhci->op_regs->dev_notification);
}

/*
 * Initialize memory for HCD and xHC (one-time init).
 *
 * Program the PAGESIZE register, initialize the device context array, create
 * device contexts (?), set up a command ring segment (or two?), create event
 * ring (one for now).
 */

static int xhci_init(struct usb_hcd *hcd)
{
 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 int retval;

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Starting %s", __func__);
 spin_lock_init(&xhci->lock);

 INIT_LIST_HEAD(&xhci->cmd_list);
 INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
 init_completion(&xhci->cmd_ring_stop_completion);
 xhci_hcd_page_size(xhci);
 memset(xhci->devs, 0, MAX_HC_SLOTS * sizeof(*xhci->devs));

 retval = xhci_mem_init(xhci, GFP_KERNEL);
 if (retval)
  return retval;

 /* Set the Number of Device Slots Enabled to the maximum supported value */
 xhci_enable_max_dev_slots(xhci);

 /* Set the address in the Command Ring Control register */
 xhci_set_cmd_ring_deq(xhci);

 /* Set Device Context Base Address Array pointer */
 xhci_write_64(xhci, xhci->dcbaa->dma, &xhci->op_regs->dcbaa_ptr);

 /* Set Doorbell array pointer */
 xhci_set_doorbell_ptr(xhci);

 /* Set USB 3.0 device notifications for function remote wake */
 xhci_set_dev_notifications(xhci);

 /* Initialize the Primary interrupter */
 xhci_add_interrupter(xhci, 0);
 xhci->interrupters[0]->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX;

 /* Initializing Compliance Mode Recovery Data If Needed */
 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
  xhci->quirks |= XHCI_COMP_MODE_QUIRK;
  compliance_mode_recovery_timer_init(xhci);
 }

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished %s", __func__);
 return 0;
}

/*-------------------------------------------------------------------------*/

static int xhci_run_finished(struct xhci_hcd *xhci)
{
 struct xhci_interrupter *ir = xhci->interrupters[0];
 unsigned long flags;
 u32  temp;

 /*
 * Enable interrupts before starting the host (xhci 4.2 and 5.5.2).
 * Protect the short window before host is running with a lock
 */

 spin_lock_irqsave(&xhci->lock, flags);

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable interrupts");
 temp = readl(&xhci->op_regs->command);
 temp |= (CMD_EIE);
 writel(temp, &xhci->op_regs->command);

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable primary interrupter");
 xhci_enable_interrupter(ir);

 if (xhci_start(xhci)) {
  xhci_halt(xhci);
  spin_unlock_irqrestore(&xhci->lock, flags);
  return -ENODEV;
 }

 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;

 if (xhci->quirks & XHCI_NEC_HOST)
  xhci_ring_cmd_db(xhci);

 spin_unlock_irqrestore(&xhci->lock, flags);

 return 0;
}

/*
 * Start the HC after it was halted.
 *
 * This function is called by the USB core when the HC driver is added.
 * Its opposite is xhci_stop().
 *
 * xhci_init() must be called once before this function can be called.
 * Reset the HC, enable device slot contexts, program DCBAAP, and
 * set command ring pointer and event ring pointer.
 *
 * Setup MSI-X vectors and enable interrupts.
 */

int xhci_run(struct usb_hcd *hcd)
{
 u64 temp_64;
 int ret;
 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 struct xhci_interrupter *ir = xhci->interrupters[0];
 /* Start the xHCI host controller running only after the USB 2.0 roothub
 * is setup.
 */


 hcd->uses_new_polling = 1;
 if (hcd->msi_enabled)
  ir->ip_autoclear = true;

 if (!usb_hcd_is_primary_hcd(hcd))
  return xhci_run_finished(xhci);

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");

 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
 temp_64 &= ERST_PTR_MASK;
 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   "ERST deq = 64'h%0lx", (long unsigned int) temp_64);

 xhci_set_interrupter_moderation(ir, xhci->imod_interval);

 if (xhci->quirks & XHCI_NEC_HOST) {
  struct xhci_command *command;

  command = xhci_alloc_command(xhci, false, GFP_KERNEL);
  if (!command)
   return -ENOMEM;

  ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0,
    TRB_TYPE(TRB_NEC_GET_FW));
  if (ret)
   xhci_free_command(xhci, command);
 }
 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   "Finished %s for main hcd", __func__);

 xhci_create_dbc_dev(xhci);

 xhci_debugfs_init(xhci);

 if (xhci_has_one_roothub(xhci))
  return xhci_run_finished(xhci);

 set_bit(HCD_FLAG_DEFER_RH_REGISTER, &hcd->flags);

 return 0;
}
EXPORT_SYMBOL_GPL(xhci_run);

/*
 * Stop xHCI driver.
 *
 * This function is called by the USB core when the HC driver is removed.
 * Its opposite is xhci_run().
 *
 * Disable device contexts, disable IRQs, and quiesce the HC.
 * Reset the HC, finish any completed transactions, and cleanup memory.
 */

void xhci_stop(struct usb_hcd *hcd)
{
 u32 temp;
 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 struct xhci_interrupter *ir = xhci->interrupters[0];

 mutex_lock(&xhci->mutex);

 /* Only halt host and free memory after both hcds are removed */
 if (!usb_hcd_is_primary_hcd(hcd)) {
  mutex_unlock(&xhci->mutex);
  return;
 }

 xhci_remove_dbc_dev(xhci);

 spin_lock_irq(&xhci->lock);
 xhci->xhc_state |= XHCI_STATE_HALTED;
 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
 xhci_halt(xhci);
 xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
 spin_unlock_irq(&xhci->lock);

 /* Deleting Compliance Mode Recovery Timer */
 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
   (!(xhci_all_ports_seen_u0(xhci)))) {
  timer_delete_sync(&xhci->comp_mode_recovery_timer);
  xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
    "%s: compliance mode recovery timer deleted",
    __func__);
 }

 if (xhci->quirks & XHCI_AMD_PLL_FIX)
  usb_amd_dev_put();

 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   "// Disabling event ring interrupts");
 temp = readl(&xhci->op_regs->status);
 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
 xhci_disable_interrupter(xhci, ir);

 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
 xhci_mem_cleanup(xhci);
 xhci_debugfs_exit(xhci);
 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   "xhci_stop completed - status = %x",
   readl(&xhci->op_regs->status));
 mutex_unlock(&xhci->mutex);
}
EXPORT_SYMBOL_GPL(xhci_stop);

/*
 * Shutdown HC (not bus-specific)
 *
 * This is called when the machine is rebooting or halting.  We assume that the
 * machine will be powered off, and the HC's internal state will be reset.
 * Don't bother to free memory.
 *
 * This will only ever be called with the main usb_hcd (the USB3 roothub).
 */

void xhci_shutdown(struct usb_hcd *hcd)
{
 struct xhci_hcd *xhci = hcd_to_xhci(hcd);

 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
  usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));

 /* Don't poll the roothubs after shutdown. */
 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n",
   __func__, hcd->self.busnum);
 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
 timer_delete_sync(&hcd->rh_timer);

 if (xhci->shared_hcd) {
  clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  timer_delete_sync(&xhci->shared_hcd->rh_timer);
 }

 spin_lock_irq(&xhci->lock);
 xhci_halt(xhci);

 /*
 * Workaround for spurious wakeps at shutdown with HSW, and for boot
 * firmware delay in ADL-P PCH if port are left in U3 at shutdown
 */

 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP ||
     xhci->quirks & XHCI_RESET_TO_DEFAULT)
  xhci_reset(xhci, XHCI_RESET_SHORT_USEC);

 spin_unlock_irq(&xhci->lock);

 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   "xhci_shutdown completed - status = %x",
   readl(&xhci->op_regs->status));
}
EXPORT_SYMBOL_GPL(xhci_shutdown);

#ifdef CONFIG_PM
static void xhci_save_registers(struct xhci_hcd *xhci)
{
 struct xhci_interrupter *ir;
 unsigned int i;

 xhci->s3.command = readl(&xhci->op_regs->command);
 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);

 /* save both primary and all secondary interrupters */
 /* fixme, shold we lock  to prevent race with remove secondary interrupter? */
 for (i = 0; i < xhci->max_interrupters; i++) {
  ir = xhci->interrupters[i];
  if (!ir)
   continue;

  ir->s3_erst_size = readl(&ir->ir_set->erst_size);
  ir->s3_erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base);
  ir->s3_erst_dequeue = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
  ir->s3_iman = readl(&ir->ir_set->iman);
  ir->s3_imod = readl(&ir->ir_set->imod);
 }
}

static void xhci_restore_registers(struct xhci_hcd *xhci)
{
 struct xhci_interrupter *ir;
 unsigned int i;

 writel(xhci->s3.command, &xhci->op_regs->command);
 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);

 /* FIXME should we lock to protect against freeing of interrupters */
 for (i = 0; i < xhci->max_interrupters; i++) {
  ir = xhci->interrupters[i];
  if (!ir)
   continue;

  writel(ir->s3_erst_size, &ir->ir_set->erst_size);
  xhci_write_64(xhci, ir->s3_erst_base, &ir->ir_set->erst_base);
  xhci_write_64(xhci, ir->s3_erst_dequeue, &ir->ir_set->erst_dequeue);
  writel(ir->s3_iman, &ir->ir_set->iman);
  writel(ir->s3_imod, &ir->ir_set->imod);
 }
}

/*
 * The whole command ring must be cleared to zero when we suspend the host.
 *
 * The host doesn't save the command ring pointer in the suspend well, so we
 * need to re-program it on resume.  Unfortunately, the pointer must be 64-byte
 * aligned, because of the reserved bits in the command ring dequeue pointer
 * register.  Therefore, we can't just set the dequeue pointer back in the
 * middle of the ring (TRBs are 16-byte aligned).
 */

static void xhci_clear_command_ring(struct xhci_hcd *xhci)
{
 struct xhci_ring *ring;
 struct xhci_segment *seg;

 ring = xhci->cmd_ring;
 xhci_for_each_ring_seg(ring->first_seg, seg) {
  /* erase all TRBs before the link */
  memset(seg->trbs, 0, sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
  /* clear link cycle bit */
  seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= cpu_to_le32(~TRB_CYCLE);
 }

 xhci_initialize_ring_info(ring);
 /*
 * Reset the hardware dequeue pointer.
 * Yes, this will need to be re-written after resume, but we're paranoid
 * and want to make sure the hardware doesn't access bogus memory
 * because, say, the BIOS or an SMI started the host without changing
 * the command ring pointers.
 */

 xhci_set_cmd_ring_deq(xhci);
}

/*
 * Disable port wake bits if do_wakeup is not set.
 *
 * Also clear a possible internal port wake state left hanging for ports that
 * detected termination but never successfully enumerated (trained to 0U).
 * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done
 * at enumeration clears this wake, force one here as well for unconnected ports
 */


static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci,
           struct xhci_hub *rhub,
           bool do_wakeup)
{
 unsigned long flags;
 u32 t1, t2, portsc;
 int i;

 spin_lock_irqsave(&xhci->lock, flags);

 for (i = 0; i < rhub->num_ports; i++) {
  portsc = readl(rhub->ports[i]->addr);
  t1 = xhci_port_state_to_neutral(portsc);
  t2 = t1;

  /* clear wake bits if do_wake is not set */
  if (!do_wakeup)
   t2 &= ~PORT_WAKE_BITS;

  /* Don't touch csc bit if connected or connect change is set */
  if (!(portsc & (PORT_CSC | PORT_CONNECT)))
   t2 |= PORT_CSC;

  if (t1 != t2) {
   writel(t2, rhub->ports[i]->addr);
   xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n",
     rhub->hcd->self.busnum, i + 1, portsc, t2);
  }
 }
 spin_unlock_irqrestore(&xhci->lock, flags);
}

static bool xhci_pending_portevent(struct xhci_hcd *xhci)
{
 struct xhci_port **ports;
 int   port_index;
 u32   status;
 u32   portsc;

 status = readl(&xhci->op_regs->status);
 if (status & STS_EINT)
  return true;
 /*
 * Checking STS_EINT is not enough as there is a lag between a change
 * bit being set and the Port Status Change Event that it generated
 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2.
 */


 port_index = xhci->usb2_rhub.num_ports;
 ports = xhci->usb2_rhub.ports;
 while (port_index--) {
  portsc = readl(ports[port_index]->addr);
  if (portsc & PORT_CHANGE_MASK ||
      (portsc & PORT_PLS_MASK) == XDEV_RESUME)
   return true;
 }
 port_index = xhci->usb3_rhub.num_ports;
 ports = xhci->usb3_rhub.ports;
 while (port_index--) {
  portsc = readl(ports[port_index]->addr);
  if (portsc & (PORT_CHANGE_MASK | PORT_CAS) ||
      (portsc & PORT_PLS_MASK) == XDEV_RESUME)
   return true;
 }
 return false;
}

/*
 * Stop HC (not bus-specific)
 *
 * This is called when the machine transition into S3/S4 mode.
 *
 */

int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
{
 int   rc = 0;
 unsigned int  delay = XHCI_MAX_HALT_USEC * 2;
 struct usb_hcd  *hcd = xhci_to_hcd(xhci);
 u32   command;
 u32   res;

 if (!hcd->state)
  return 0;

 if (hcd->state != HC_STATE_SUSPENDED ||
     (xhci->shared_hcd && xhci->shared_hcd->state != HC_STATE_SUSPENDED))
  return -EINVAL;

 /* Clear root port wake on bits if wakeup not allowed. */
 xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup);
 xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup);

 if (!HCD_HW_ACCESSIBLE(hcd))
  return 0;

 xhci_dbc_suspend(xhci);

 /* Don't poll the roothubs on bus suspend. */
 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n",
   __func__, hcd->self.busnum);
 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
 timer_delete_sync(&hcd->rh_timer);
 if (xhci->shared_hcd) {
  clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  timer_delete_sync(&xhci->shared_hcd->rh_timer);
 }

 if (xhci->quirks & XHCI_SUSPEND_DELAY)
  usleep_range(1000, 1500);

 spin_lock_irq(&xhci->lock);
 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
 if (xhci->shared_hcd)
  clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
 /* step 1: stop endpoint */
 /* skipped assuming that port suspend has done */

 /* step 2: clear Run/Stop bit */
 command = readl(&xhci->op_regs->command);
 command &= ~CMD_RUN;
 writel(command, &xhci->op_regs->command);

 /* Some chips from Fresco Logic need an extraordinary delay */
 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;

 if (xhci_handshake(&xhci->op_regs->status,
        STS_HALT, STS_HALT, delay)) {
  xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
  spin_unlock_irq(&xhci->lock);
  return -ETIMEDOUT;
 }
 xhci_clear_command_ring(xhci);

 /* step 3: save registers */
 xhci_save_registers(xhci);

 /* step 4: set CSS flag */
 command = readl(&xhci->op_regs->command);
 command |= CMD_CSS;
 writel(command, &xhci->op_regs->command);
 xhci->broken_suspend = 0;
 if (xhci_handshake(&xhci->op_regs->status,
    STS_SAVE, 0, 20 * 1000)) {
 /*
 * AMD SNPS xHC 3.0 occasionally does not clear the
 * SSS bit of USBSTS and when driver tries to poll
 * to see if the xHC clears BIT(8) which never happens
 * and driver assumes that controller is not responding
 * and times out. To workaround this, its good to check
 * if SRE and HCE bits are not set (as per xhci
 * Section 5.4.2) and bypass the timeout.
 */

  res = readl(&xhci->op_regs->status);
  if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) &&
      (((res & STS_SRE) == 0) &&
    ((res & STS_HCE) == 0))) {
   xhci->broken_suspend = 1;
  } else {
   xhci_warn(xhci, "WARN: xHC save state timeout\n");
   spin_unlock_irq(&xhci->lock);
   return -ETIMEDOUT;
  }
 }
 spin_unlock_irq(&xhci->lock);

 /*
 * Deleting Compliance Mode Recovery Timer because the xHCI Host
 * is about to be suspended.
 */

 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
   (!(xhci_all_ports_seen_u0(xhci)))) {
  timer_delete_sync(&xhci->comp_mode_recovery_timer);
  xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
    "%s: compliance mode recovery timer deleted",
    __func__);
 }

 return rc;
}
EXPORT_SYMBOL_GPL(xhci_suspend);

/*
 * start xHC (not bus-specific)
 *
 * This is called when the machine transition from S3/S4 mode.
 *
 */

int xhci_resume(struct xhci_hcd *xhci, bool power_lost, bool is_auto_resume)
{
 u32   command, temp = 0;
 struct usb_hcd  *hcd = xhci_to_hcd(xhci);
 int   retval = 0;
 bool   comp_timer_running = false;
 bool   pending_portevent = false;
 bool   suspended_usb3_devs = false;

 if (!hcd->state)
  return 0;

 /* Wait a bit if either of the roothubs need to settle from the
 * transition into bus suspend.
 */


 if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) ||
     time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange))
  msleep(100);

 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
 if (xhci->shared_hcd)
  set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);

 spin_lock_irq(&xhci->lock);

 if (xhci->quirks & XHCI_RESET_ON_RESUME || xhci->broken_suspend)
  power_lost = true;

 if (!power_lost) {
  /*
 * Some controllers might lose power during suspend, so wait
 * for controller not ready bit to clear, just as in xHC init.
 */

  retval = xhci_handshake(&xhci->op_regs->status,
     STS_CNR, 0, 10 * 1000 * 1000);
  if (retval) {
   xhci_warn(xhci, "Controller not ready at resume %d\n",
      retval);
   spin_unlock_irq(&xhci->lock);
   return retval;
  }
  /* step 1: restore register */
  xhci_restore_registers(xhci);
  /* step 2: initialize command ring buffer */
  xhci_set_cmd_ring_deq(xhci);
  /* step 3: restore state and start state*/
  /* step 3: set CRS flag */
  command = readl(&xhci->op_regs->command);
  command |= CMD_CRS;
  writel(command, &xhci->op_regs->command);
  /*
 * Some controllers take up to 55+ ms to complete the controller
 * restore so setting the timeout to 100ms. Xhci specification
 * doesn't mention any timeout value.
 */

  if (xhci_handshake(&xhci->op_regs->status,
         STS_RESTORE, 0, 100 * 1000)) {
   xhci_warn(xhci, "WARN: xHC restore state timeout\n");
   spin_unlock_irq(&xhci->lock);
   return -ETIMEDOUT;
  }
 }

 temp = readl(&xhci->op_regs->status);

 /* re-initialize the HC on Restore Error, or Host Controller Error */
 if ((temp & (STS_SRE | STS_HCE)) &&
     !(xhci->xhc_state & XHCI_STATE_REMOVING)) {
  if (!power_lost)
   xhci_warn(xhci, "xHC error in resume, USBSTS 0x%x, Reinit\n", temp);
  power_lost = true;
 }

 if (power_lost) {
  if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
    !(xhci_all_ports_seen_u0(xhci))) {
   timer_delete_sync(&xhci->comp_mode_recovery_timer);
   xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
    "Compliance Mode Recovery Timer deleted!");
  }

  /* Let the USB core know _both_ roothubs lost power. */
  usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
  if (xhci->shared_hcd)
   usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);

  xhci_dbg(xhci, "Stop HCD\n");
  xhci_halt(xhci);
  xhci_zero_64b_regs(xhci);
  if (xhci->xhc_state & XHCI_STATE_REMOVING)
   retval = -ENODEV;
  else
   retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC);
  spin_unlock_irq(&xhci->lock);
  if (retval)
   return retval;

  xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  temp = readl(&xhci->op_regs->status);
  writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
  xhci_disable_interrupter(xhci, xhci->interrupters[0]);

  xhci_dbg(xhci, "cleaning up memory\n");
  xhci_mem_cleanup(xhci);
  xhci_debugfs_exit(xhci);
  xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
       readl(&xhci->op_regs->status));

  /* USB core calls the PCI reinit and start functions twice:
 * first with the primary HCD, and then with the secondary HCD.
 * If we don't do the same, the host will never be started.
 */

  xhci_dbg(xhci, "Initialize the xhci_hcd\n");
  retval = xhci_init(hcd);
  if (retval)
   return retval;
  comp_timer_running = true;

  xhci_dbg(xhci, "Start the primary HCD\n");
  retval = xhci_run(hcd);
  if (!retval && xhci->shared_hcd) {
   xhci_dbg(xhci, "Start the secondary HCD\n");
   retval = xhci_run(xhci->shared_hcd);
  }
  if (retval)
   return retval;
  /*
 * Resume roothubs unconditionally as PORTSC change bits are not
 * immediately visible after xHC reset
 */

  hcd->state = HC_STATE_SUSPENDED;

  if (xhci->shared_hcd) {
   xhci->shared_hcd->state = HC_STATE_SUSPENDED;
   usb_hcd_resume_root_hub(xhci->shared_hcd);
  }
  usb_hcd_resume_root_hub(hcd);

  goto done;
 }

 /* step 4: set Run/Stop bit */
 command = readl(&xhci->op_regs->command);
 command |= CMD_RUN;
 writel(command, &xhci->op_regs->command);
 xhci_handshake(&xhci->op_regs->status, STS_HALT,
    0, 250 * 1000);

 /* step 5: walk topology and initialize portsc,
 * portpmsc and portli
 */

 /* this is done in bus_resume */

 /* step 6: restart each of the previously
 * Running endpoints by ringing their doorbells
 */


 spin_unlock_irq(&xhci->lock);

 xhci_dbc_resume(xhci);

 if (retval == 0) {
  /*
 * Resume roothubs only if there are pending events.
 * USB 3 devices resend U3 LFPS wake after a 100ms delay if
 * the first wake signalling failed, give it that chance if
 * there are suspended USB 3 devices.
 */

  if (xhci->usb3_rhub.bus_state.suspended_ports ||
      xhci->usb3_rhub.bus_state.bus_suspended)
   suspended_usb3_devs = true;

  pending_portevent = xhci_pending_portevent(xhci);

  if (suspended_usb3_devs && !pending_portevent && is_auto_resume) {
   msleep(120);
   pending_portevent = xhci_pending_portevent(xhci);
  }

  if (pending_portevent) {
   if (xhci->shared_hcd)
    usb_hcd_resume_root_hub(xhci->shared_hcd);
   usb_hcd_resume_root_hub(hcd);
  }
 }
done:
 /*
 * If system is subject to the Quirk, Compliance Mode Timer needs to
 * be re-initialized Always after a system resume. Ports are subject
 * to suffer the Compliance Mode issue again. It doesn't matter if
 * ports have entered previously to U0 before system's suspension.
 */

 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
  compliance_mode_recovery_timer_init(xhci);

 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
  usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));

 /* Re-enable port polling. */
 xhci_dbg(xhci, "%s: starting usb%d port polling.\n",
   __func__, hcd->self.busnum);
 if (xhci->shared_hcd) {
  set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  usb_hcd_poll_rh_status(xhci->shared_hcd);
 }
 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
 usb_hcd_poll_rh_status(hcd);

 return retval;
}
EXPORT_SYMBOL_GPL(xhci_resume);
#endif /* CONFIG_PM */

/*-------------------------------------------------------------------------*/

static int xhci_map_temp_buffer(struct usb_hcd *hcd, struct urb *urb)
{
 void *temp;
 int ret = 0;
 unsigned int buf_len;
 enum dma_data_direction dir;

 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
 buf_len = urb->transfer_buffer_length;

 temp = kzalloc_node(buf_len, GFP_ATOMIC,
       dev_to_node(hcd->self.sysdev));
 if (!temp)
  return -ENOMEM;

 if (usb_urb_dir_out(urb))
  sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
       temp, buf_len, 0);

 urb->transfer_buffer = temp;
 urb->transfer_dma = dma_map_single(hcd->self.sysdev,
        urb->transfer_buffer,
        urb->transfer_buffer_length,
        dir);

 if (dma_mapping_error(hcd->self.sysdev,
         urb->transfer_dma)) {
  ret = -EAGAIN;
  kfree(temp);
 } else {
  urb->transfer_flags |= URB_DMA_MAP_SINGLE;
 }

 return ret;
}

static bool xhci_urb_temp_buffer_required(struct usb_hcd *hcd,
       struct urb *urb)
{
 bool ret = false;
 unsigned int i;
 unsigned int len = 0;
 unsigned int trb_size;
 unsigned int max_pkt;
 struct scatterlist *sg;
 struct scatterlist *tail_sg;

 tail_sg = urb->sg;
 max_pkt = usb_endpoint_maxp(&urb->ep->desc);

 if (!urb->num_sgs)
  return ret;

 if (urb->dev->speed >= USB_SPEED_SUPER)
  trb_size = TRB_CACHE_SIZE_SS;
 else
  trb_size = TRB_CACHE_SIZE_HS;

 if (urb->transfer_buffer_length != 0 &&
     !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  for_each_sg(urb->sg, sg, urb->num_sgs, i) {
   len = len + sg->length;
   if (i > trb_size - 2) {
    len = len - tail_sg->length;
    if (len < max_pkt) {
     ret = true;
     break;
    }

    tail_sg = sg_next(tail_sg);
   }
  }
 }
 return ret;
}

static void xhci_unmap_temp_buf(struct usb_hcd *hcd, struct urb *urb)
{
 unsigned int len;
 unsigned int buf_len;
 enum dma_data_direction dir;

 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;

 buf_len = urb->transfer_buffer_length;

 if (IS_ENABLED(CONFIG_HAS_DMA) &&
     (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  dma_unmap_single(hcd->self.sysdev,
     urb->transfer_dma,
     urb->transfer_buffer_length,
     dir);

 if (usb_urb_dir_in(urb)) {
  len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs,
        urb->transfer_buffer,
        buf_len,
        0);
  if (len != buf_len) {
   xhci_dbg(hcd_to_xhci(hcd),
     "Copy from tmp buf to urb sg list failed\n");
   urb->actual_length = len;
  }
 }
 urb->transfer_flags &= ~URB_DMA_MAP_SINGLE;
 kfree(urb->transfer_buffer);
 urb->transfer_buffer = NULL;
}

/*
 * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
 * we'll copy the actual data into the TRB address register. This is limited to
 * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize
 * >= 8 bytes. If suitable for IDT only one Transfer TRB per TD is allowed.
 */

static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
    gfp_t mem_flags)
{
 struct xhci_hcd *xhci;

 xhci = hcd_to_xhci(hcd);

 if (xhci_urb_suitable_for_idt(urb))
  return 0;

 if (xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) {
  if (xhci_urb_temp_buffer_required(hcd, urb))
   return xhci_map_temp_buffer(hcd, urb);
 }
 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
}

static void xhci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
{
 struct xhci_hcd *xhci;
 bool unmap_temp_buf = false;

 xhci = hcd_to_xhci(hcd);

 if (urb->num_sgs && (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  unmap_temp_buf = true;

 if ((xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) && unmap_temp_buf)
  xhci_unmap_temp_buf(hcd, urb);
 else
  usb_hcd_unmap_urb_for_dma(hcd, urb);
}

/**
 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
 * HCDs.  Find the index for an endpoint given its descriptor.  Use the return
 * value to right shift 1 for the bitmask.
 * @desc: USB endpoint descriptor to determine index for
 *
 * Index  = (epnum * 2) + direction - 1,
 * where direction = 0 for OUT, 1 for IN.
 * For control endpoints, the IN index is used (OUT index is unused), so
 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
 */

unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
{
 unsigned int index;
 if (usb_endpoint_xfer_control(desc))
  index = (unsigned int) (usb_endpoint_num(desc)*2);
 else
  index = (unsigned int) (usb_endpoint_num(desc)*2) +
   (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
 return index;
}
EXPORT_SYMBOL_GPL(xhci_get_endpoint_index);

/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
 * address from the XHCI endpoint index.
 */

static unsigned int xhci_get_endpoint_address(unsigned int ep_index)
{
 unsigned int number = DIV_ROUND_UP(ep_index, 2);
 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
 return direction | number;
}

/* Find the flag for this endpoint (for use in the control context).  Use the
 * endpoint index to create a bitmask.  The slot context is bit 0, endpoint 0 is
 * bit 1, etc.
 */

static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
{
 return 1 << (xhci_get_endpoint_index(desc) + 1);
}

/* Compute the last valid endpoint context index.  Basically, this is the
 * endpoint index plus one.  For slot contexts with more than valid endpoint,
 * we find the most significant bit set in the added contexts flags.
 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
 */

unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
{
 return fls(added_ctxs) - 1;
}

/* Returns 1 if the arguments are OK;
 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
 */

static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
  const char *func) {
 struct xhci_hcd *xhci;
 struct xhci_virt_device *virt_dev;

 if (!hcd || (check_ep && !ep) || !udev) {
  pr_debug("xHCI %s called with invalid args\n", func);
  return -EINVAL;
 }
 if (!udev->parent) {
  pr_debug("xHCI %s called for root hub\n", func);
  return 0;
 }

 xhci = hcd_to_xhci(hcd);
 if (check_virt_dev) {
  if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
   xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
     func);
   return -EINVAL;
  }

  virt_dev = xhci->devs[udev->slot_id];
  if (virt_dev->udev != udev) {
   xhci_dbg(xhci, "xHCI %s called with udev and "
       "virt_dev does not match\n", func);
   return -EINVAL;
  }
 }

 if (xhci->xhc_state & XHCI_STATE_HALTED)
  return -ENODEV;

 return 1;
}

static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  struct usb_device *udev, struct xhci_command *command,
  bool ctx_change, bool must_succeed);

/*
 * Full speed devices may have a max packet size greater than 8 bytes, but the
 * USB core doesn't know that until it reads the first 8 bytes of the
 * descriptor.  If the usb_device's max packet size changes after that point,
 * we need to issue an evaluate context command and wait on it.
 */

static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_device *vdev)
{
 struct xhci_input_control_ctx *ctrl_ctx;
 struct xhci_ep_ctx *ep_ctx;
 struct xhci_command *command;
 int max_packet_size;
 int hw_max_packet_size;
 int ret = 0;

 ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, 0);
 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
 max_packet_size = usb_endpoint_maxp(&vdev->udev->ep0.desc);

 if (hw_max_packet_size == max_packet_size)
  return 0;

 switch (max_packet_size) {
 case 8: case 16: case 32: case 64: case 9:
  xhci_dbg_trace(xhci,  trace_xhci_dbg_context_change,
    "Max Packet Size for ep 0 changed.");
  xhci_dbg_trace(xhci,  trace_xhci_dbg_context_change,
    "Max packet size in usb_device = %d",
    max_packet_size);
  xhci_dbg_trace(xhci,  trace_xhci_dbg_context_change,
    "Max packet size in xHCI HW = %d",
    hw_max_packet_size);
  xhci_dbg_trace(xhci,  trace_xhci_dbg_context_change,
    "Issuing evaluate context command.");

  command = xhci_alloc_command(xhci, true, GFP_KERNEL);
  if (!command)
   return -ENOMEM;

  command->in_ctx = vdev->in_ctx;
  ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  if (!ctrl_ctx) {
   xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
     __func__);
   ret = -ENOMEM;
   break;
  }
  /* Set up the modified control endpoint 0 */
  xhci_endpoint_copy(xhci, vdev->in_ctx, vdev->out_ctx, 0);

  ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, 0);
  ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */
  ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));

  ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  ctrl_ctx->drop_flags = 0;

  ret = xhci_configure_endpoint(xhci, vdev->udev, command,
           truefalse);
  /* Clean up the input context for later use by bandwidth functions */
  ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  break;
 default:
  dev_dbg(&vdev->udev->dev, "incorrect max packet size %d for ep0\n",
   max_packet_size);
  return -EINVAL;
 }

 kfree(command->completion);
 kfree(command);

 return ret;
}

/*
 * non-error returns are a promise to giveback() the urb later
 * we drop ownership so next owner (or urb unlink) can get it
 */

static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
{
 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
 unsigned long flags;
 int ret = 0;
 unsigned int slot_id, ep_index;
 unsigned int *ep_state;
 struct urb_priv *urb_priv;
 int num_tds;

 ep_index = xhci_get_endpoint_index(&urb->ep->desc);

 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  num_tds = urb->number_of_packets;
 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
     urb->transfer_buffer_length > 0 &&
     urb->transfer_flags & URB_ZERO_PACKET &&
     !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
  num_tds = 2;
 else
  num_tds = 1;

 urb_priv = kzalloc(struct_size(urb_priv, td, num_tds), mem_flags);
 if (!urb_priv)
  return -ENOMEM;

 urb_priv->num_tds = num_tds;
 urb_priv->num_tds_done = 0;
 urb->hcpriv = urb_priv;

 trace_xhci_urb_enqueue(urb);

 spin_lock_irqsave(&xhci->lock, flags);

 ret = xhci_check_args(hcd, urb->dev, urb->ep,
         truetrue, __func__);
 if (ret <= 0) {
  ret = ret ? ret : -EINVAL;
  goto free_priv;
 }

 slot_id = urb->dev->slot_id;

 if (!HCD_HW_ACCESSIBLE(hcd)) {
  ret = -ESHUTDOWN;
  goto free_priv;
 }

 if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) {
  xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n");
  ret = -ENODEV;
  goto free_priv;
 }

 if (xhci->xhc_state & XHCI_STATE_DYING) {
  xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n",
    urb->ep->desc.bEndpointAddress, urb);
  ret = -ESHUTDOWN;
  goto free_priv;
 }

 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state;

 if (*ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) {
  xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n",
     *ep_state);
  ret = -EINVAL;
  goto free_priv;
 }
 if (*ep_state & EP_SOFT_CLEAR_TOGGLE) {
  xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n");
  ret = -EINVAL;
  goto free_priv;
 }

 switch (usb_endpoint_type(&urb->ep->desc)) {

 case USB_ENDPOINT_XFER_CONTROL:
  ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
      slot_id, ep_index);
  break;
 case USB_ENDPOINT_XFER_BULK:
  ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
      slot_id, ep_index);
  break;
 case USB_ENDPOINT_XFER_INT:
  ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
    slot_id, ep_index);
  break;
 case USB_ENDPOINT_XFER_ISOC:
  ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
    slot_id, ep_index);
 }

 if (ret) {
free_priv:
  xhci_urb_free_priv(urb_priv);
  urb->hcpriv = NULL;
 }
 spin_unlock_irqrestore(&xhci->lock, flags);
 return ret;
}

/*
 * Remove the URB's TD from the endpoint ring.  This may cause the HC to stop
 * USB transfers, potentially stopping in the middle of a TRB buffer.  The HC
 * should pick up where it left off in the TD, unless a Set Transfer Ring
 * Dequeue Pointer is issued.
 *
 * The TRBs that make up the buffers for the canceled URB will be "removed" from
 * the ring.  Since the ring is a contiguous structure, they can't be physically
 * removed.  Instead, there are two options:
 *
 *  1) If the HC is in the middle of processing the URB to be canceled, we
 *     simply move the ring's dequeue pointer past those TRBs using the Set
 *     Transfer Ring Dequeue Pointer command.  This will be the common case,
 *     when drivers timeout on the last submitted URB and attempt to cancel.
 *
 *  2) If the HC is in the middle of a different TD, we turn the TRBs into a
 *     series of 1-TRB transfer no-op TDs.  (No-ops shouldn't be chained.)  The
 *     HC will need to invalidate the any TRBs it has cached after the stop
 *     endpoint command, as noted in the xHCI 0.95 errata.
 *
 *  3) The TD may have completed by the time the Stop Endpoint Command
 *     completes, so software needs to handle that case too.
 *
 * This function should protect against the TD enqueueing code ringing the
 * doorbell while this code is waiting for a Stop Endpoint command to complete.
 * It also needs to account for multiple cancellations on happening at the same
 * time for the same endpoint.
 *
 * Note that this function can be called in any context, or so says
 * usb_hcd_unlink_urb()
 */

static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
{
 unsigned long flags;
 int ret, i;
 u32 temp;
 struct xhci_hcd *xhci;
 struct urb_priv *urb_priv;
 struct xhci_td *td;
 unsigned int ep_index;
 struct xhci_ring *ep_ring;
 struct xhci_virt_ep *ep;
 struct xhci_command *command;
 struct xhci_virt_device *vdev;

 xhci = hcd_to_xhci(hcd);
 spin_lock_irqsave(&xhci->lock, flags);

 trace_xhci_urb_dequeue(urb);

 /* Make sure the URB hasn't completed or been unlinked already */
 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
 if (ret)
  goto done;

 /* give back URB now if we can't queue it for cancel */
 vdev = xhci->devs[urb->dev->slot_id];
 urb_priv = urb->hcpriv;
 if (!vdev || !urb_priv)
  goto err_giveback;

 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
 ep = &vdev->eps[ep_index];
 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
 if (!ep || !ep_ring)
  goto err_giveback;

 /* If xHC is dead take it down and return ALL URBs in xhci_hc_died() */
 temp = readl(&xhci->op_regs->status);
 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) {
  xhci_hc_died(xhci);
  goto done;
 }

 /*
 * check ring is not re-allocated since URB was enqueued. If it is, then
 * make sure none of the ring related pointers in this URB private data
 * are touched, such as td_list, otherwise we overwrite freed data
 */

 if (!td_on_ring(&urb_priv->td[0], ep_ring)) {
  xhci_err(xhci, "Canceled URB td not found on endpoint ring");
  for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) {
   td = &urb_priv->td[i];
   if (!list_empty(&td->cancelled_td_list))
    list_del_init(&td->cancelled_td_list);
  }
  goto err_giveback;
 }

 if (xhci->xhc_state & XHCI_STATE_HALTED) {
  xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
    "HC halted, freeing TD manually.");
  for (i = urb_priv->num_tds_done;
       i < urb_priv->num_tds;
       i++) {
   td = &urb_priv->td[i];
   if (!list_empty(&td->td_list))
    list_del_init(&td->td_list);
   if (!list_empty(&td->cancelled_td_list))
    list_del_init(&td->cancelled_td_list);
  }
  goto err_giveback;
 }

 i = urb_priv->num_tds_done;
 if (i < urb_priv->num_tds)
  xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
    "Cancel URB %p, dev %s, ep 0x%x, "
    "starting at offset 0x%llx",
    urb, urb->dev->devpath,
    urb->ep->desc.bEndpointAddress,
    (unsigned long long) xhci_trb_virt_to_dma(
     urb_priv->td[i].start_seg,
     urb_priv->td[i].start_trb));

 for (; i < urb_priv->num_tds; i++) {
  td = &urb_priv->td[i];
  /* TD can already be on cancelled list if ep halted on it */
  if (list_empty(&td->cancelled_td_list)) {
   td->cancel_status = TD_DIRTY;
   list_add_tail(&td->cancelled_td_list,
          &ep->cancelled_td_list);
  }
 }

 /* These completion handlers will sort out cancelled TDs for us */
 if (ep->ep_state & (EP_STOP_CMD_PENDING | EP_HALTED | SET_DEQ_PENDING)) {
  xhci_dbg(xhci, "Not queuing Stop Endpoint on slot %d ep %d in state 0x%x\n",
    urb->dev->slot_id, ep_index, ep->ep_state);
  goto done;
 }

 /* In this case no commands are pending but the endpoint is stopped */
 if (ep->ep_state & EP_CLEARING_TT) {
  /* and cancelled TDs can be given back right away */
  xhci_dbg(xhci, "Invalidating TDs instantly on slot %d ep %d in state 0x%x\n",
    urb->dev->slot_id, ep_index, ep->ep_state);
  xhci_process_cancelled_tds(ep);
 } else {
  /* Otherwise, queue a new Stop Endpoint command */
  command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
  if (!command) {
   ret = -ENOMEM;
   goto done;
  }
  ep->stop_time = jiffies;
  ep->ep_state |= EP_STOP_CMD_PENDING;
  xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
      ep_index, 0);
  xhci_ring_cmd_db(xhci);
 }
done:
 spin_unlock_irqrestore(&xhci->lock, flags);
 return ret;

err_giveback:
 if (urb_priv)
  xhci_urb_free_priv(urb_priv);
 usb_hcd_unlink_urb_from_ep(hcd, urb);
 spin_unlock_irqrestore(&xhci->lock, flags);
 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
 return ret;
}

/* Drop an endpoint from a new bandwidth configuration for this device.
 * Only one call to this function is allowed per endpoint before
 * check_bandwidth() or reset_bandwidth() must be called.
 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
 * add the endpoint to the schedule with possibly new parameters denoted by a
 * different endpoint descriptor in usb_host_endpoint.
 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
 * not allowed.
 *
 * The USB core will not allow URBs to be queued to an endpoint that is being
 * disabled, so there's no need for mutual exclusion to protect
 * the xhci->devs[slot_id] structure.
 */

int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
         struct usb_host_endpoint *ep)
{
 struct xhci_hcd *xhci;
 struct xhci_container_ctx *in_ctx, *out_ctx;
 struct xhci_input_control_ctx *ctrl_ctx;
 unsigned int ep_index;
 struct xhci_ep_ctx *ep_ctx;
 u32 drop_flag;
 u32 new_add_flags, new_drop_flags;
 int ret;

 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
 if (ret <= 0)
  return ret;
 xhci = hcd_to_xhci(hcd);
 if (xhci->xhc_state & XHCI_STATE_DYING)
  return -ENODEV;

 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
 drop_flag = xhci_get_endpoint_flag(&ep->desc);
 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
    __func__, drop_flag);
  return 0;
 }

 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
 if (!ctrl_ctx) {
  xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
    __func__);
  return 0;
 }

 ep_index = xhci_get_endpoint_index(&ep->desc);
 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
 /* If the HC already knows the endpoint is disabled,
 * or the HCD has noted it is disabled, ignore this request
 */

 if ((GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) ||
     le32_to_cpu(ctrl_ctx->drop_flags) &
     xhci_get_endpoint_flag(&ep->desc)) {
  /* Do not warn when called after a usb_device_reset */
  if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
   xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
      __func__, ep);
  return 0;
 }

 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);

 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);

 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index);

 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);

 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
   (unsigned int) ep->desc.bEndpointAddress,
   udev->slot_id,
   (unsigned int) new_drop_flags,
   (unsigned int) new_add_flags);
 return 0;
}
EXPORT_SYMBOL_GPL(xhci_drop_endpoint);

/* Add an endpoint to a new possible bandwidth configuration for this device.
 * Only one call to this function is allowed per endpoint before
 * check_bandwidth() or reset_bandwidth() must be called.
 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
 * add the endpoint to the schedule with possibly new parameters denoted by a
 * different endpoint descriptor in usb_host_endpoint.
 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
 * not allowed.
 *
 * The USB core will not allow URBs to be queued to an endpoint until the
 * configuration or alt setting is installed in the device, so there's no need
 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
 */

int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
        struct usb_host_endpoint *ep)
{
 struct xhci_hcd *xhci;
 struct xhci_container_ctx *in_ctx;
 unsigned int ep_index;
 struct xhci_input_control_ctx *ctrl_ctx;
 struct xhci_ep_ctx *ep_ctx;
 u32 added_ctxs;
 u32 new_add_flags, new_drop_flags;
 struct xhci_virt_device *virt_dev;
 int ret = 0;

 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
 if (ret <= 0) {
  /* So we won't queue a reset ep command for a root hub */
  ep->hcpriv = NULL;
  return ret;
 }
 xhci = hcd_to_xhci(hcd);
 if (xhci->xhc_state & XHCI_STATE_DYING)
  return -ENODEV;

 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  /* FIXME when we have to issue an evaluate endpoint command to
 * deal with ep0 max packet size changing once we get the
 * descriptors
 */

  xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
    __func__, added_ctxs);
  return 0;
 }

 virt_dev = xhci->devs[udev->slot_id];
 in_ctx = virt_dev->in_ctx;
 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
 if (!ctrl_ctx) {
  xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
    __func__);
  return 0;
 }

 ep_index = xhci_get_endpoint_index(&ep->desc);
 /* If this endpoint is already in use, and the upper layers are trying
 * to add it again without dropping it, reject the addition.
 */

 if (virt_dev->eps[ep_index].ring &&
   !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
  xhci_warn(xhci, "Trying to add endpoint 0x%x "
    "without dropping it.\n",
    (unsigned int) ep->desc.bEndpointAddress);
  return -EINVAL;
 }

 /* If the HCD has already noted the endpoint is enabled,
 * ignore this request.
 */

 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
  xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
    __func__, ep);
  return 0;
 }

 /*
 * Configuration and alternate setting changes must be done in
 * process context, not interrupt context (or so documenation
 * for usb_set_interface() and usb_set_configuration() claim).
 */

 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
    __func__, ep->desc.bEndpointAddress);
  return -ENOMEM;
 }

 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);

 /* If xhci_endpoint_disable() was called for this endpoint, but the
 * xHC hasn't been notified yet through the check_bandwidth() call,
 * this re-adds a new state for the endpoint from the new endpoint
 * descriptors.  We must drop and re-add this endpoint, so we leave the
 * drop flags alone.
 */

 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);

 /* Store the usb_device pointer for later use */
 ep->hcpriv = udev;

 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
 trace_xhci_add_endpoint(ep_ctx);

 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
   (unsigned int) ep->desc.bEndpointAddress,
   udev->slot_id,
   (unsigned int) new_drop_flags,
   (unsigned int) new_add_flags);
 return 0;
}
EXPORT_SYMBOL_GPL(xhci_add_endpoint);

static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
{
 struct xhci_input_control_ctx *ctrl_ctx;
 struct xhci_ep_ctx *ep_ctx;
 struct xhci_slot_ctx *slot_ctx;
 int i;

 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
 if (!ctrl_ctx) {
  xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
    __func__);
  return;
 }

 /* When a device's add flag and drop flag are zero, any subsequent
 * configure endpoint command will leave that endpoint's state
 * untouched.  Make sure we don't leave any old state in the input
 * endpoint contexts.
 */

 ctrl_ctx->drop_flags = 0;
 ctrl_ctx->add_flags = 0;
 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
 /* Endpoint 0 is always valid */
 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
 for (i = 1; i < 31; i++) {
  ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  ep_ctx->ep_info = 0;
  ep_ctx->ep_info2 = 0;
  ep_ctx->deq = 0;
  ep_ctx->tx_info = 0;
 }
}

static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  struct usb_device *udev, u32 *cmd_status)
{
 int ret;

 switch (*cmd_status) {
 case COMP_COMMAND_ABORTED:
 case COMP_COMMAND_RING_STOPPED:
  xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
  ret = -ETIME;
  break;
 case COMP_RESOURCE_ERROR:
  dev_warn(&udev->dev,
    "Not enough host controller resources for new device state.\n");
  ret = -ENOMEM;
  /* FIXME: can we allocate more resources for the HC? */
  break;
 case COMP_BANDWIDTH_ERROR:
 case COMP_SECONDARY_BANDWIDTH_ERROR:
  dev_warn(&udev->dev,
    "Not enough bandwidth for new device state.\n");
  ret = -ENOSPC;
  /* FIXME: can we go back to the old state? */
  break;
 case COMP_TRB_ERROR:
  /* the HCD set up something wrong */
  dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
    "add flag = 1, "
    "and endpoint is not disabled.\n");
  ret = -EINVAL;
  break;
 case COMP_INCOMPATIBLE_DEVICE_ERROR:
  dev_warn(&udev->dev,
    "ERROR: Incompatible device for endpoint configure command.\n");
  ret = -ENODEV;
  break;
 case COMP_SUCCESS:
  xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
    "Successful Endpoint Configure command");
  ret = 0;
  break;
 default:
  xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
    *cmd_status);
  ret = -EINVAL;
  break;
 }
 return ret;
}

static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  struct usb_device *udev, u32 *cmd_status)
{
 int ret;

 switch (*cmd_status) {
 case COMP_COMMAND_ABORTED:
 case COMP_COMMAND_RING_STOPPED:
  xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
  ret = -ETIME;
  break;
 case COMP_PARAMETER_ERROR:
  dev_warn(&udev->dev,
    "WARN: xHCI driver setup invalid evaluate context command.\n");
  ret = -EINVAL;
  break;
 case COMP_SLOT_NOT_ENABLED_ERROR:
  dev_warn(&udev->dev,
   "WARN: slot not enabled for evaluate context command.\n");
  ret = -EINVAL;
  break;
 case COMP_CONTEXT_STATE_ERROR:
  dev_warn(&udev->dev,
   "WARN: invalid context state for evaluate context command.\n");
  ret = -EINVAL;
  break;
 case COMP_INCOMPATIBLE_DEVICE_ERROR:
  dev_warn(&udev->dev,
   "ERROR: Incompatible device for evaluate context command.\n");
  ret = -ENODEV;
  break;
 case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
  /* Max Exit Latency too large error */
  dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  ret = -EINVAL;
  break;
 case COMP_SUCCESS:
  xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
    "Successful evaluate context command");
  ret = 0;
  break;
 default:
  xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
   *cmd_status);
  ret = -EINVAL;
  break;
 }
 return ret;
}

static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  struct xhci_input_control_ctx *ctrl_ctx)
{
 u32 valid_add_flags;
 u32 valid_drop_flags;

 /* Ignore the slot flag (bit 0), and the default control endpoint flag
 * (bit 1).  The default control endpoint is added during the Address
 * Device command and is never removed until the slot is disabled.
 */

--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=95 H=93 G=93

¤ Dauer der Verarbeitung: 0.22 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.