xhci: free xhci virtual devices with leaf nodes first
Subsystems (5)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/11 xhci: free xhci virtual devices with leaf nodes first
the tt_info provided by a HS hub might be in use to by a child device Make sure we free the devices in the correct order.
This is needed in special cases such as when xhci controller is reset when resuming from hibernate, and all virt_devices are freed.
Also free the virt_devices starting from max slot_id as children more commonly have higher slot_id than parent.
--- drivers/usb/host/xhci-mem.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-mem.c +36 -2
@@ -979,6 +979,40 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id) xhci->devs[slot_id] =NULL ; } +/* +* Free a virt_device structure. +* If the virt_device added a tt_info (a hub) and has children pointing to +* that tt_info, then free the child first. Recursive. +* We can't rely on udev at this point to find child-parent relationships. +*/ +void xhci_free_virt_devices_depth_first (struct xhci_hcd *xhci,int slot_id) +{ +struct xhci_virt_device *vdev; +struct list_head *tt_list_head; +struct xhci_tt_bw_info *tt_info, *next; +int i; + + vdev = xhci->devs[slot_id]; +if (!vdev) +return ; + + tt_list_head = &(xhci->rh_bw[vdev->real_port -1 ].tts); +list_for_each_entry_safe (tt_info, next, tt_list_head, tt_list) { +/* is this a hub device that added a tt_info to the tts list */ +if (tt_info->slot_id == slot_id) { +/* are any devices using this tt_info? */ +for (i =1 ; i <HCS_MAX_SLOTS (xhci->hcs_params1); i++) { + vdev = xhci->devs[i]; +if (vdev && (vdev->tt_info == tt_info)) +xhci_free_virt_devices_depth_first ( + xhci, i); + } + } + } +/* we are now at a leaf device */ +xhci_free_virt_device (xhci, slot_id); +} +int xhci_alloc_virt_device (struct xhci_hcd *xhci,int slot_id,struct usb_device *udev,gfp_t flags) {
@@ -1828,8 +1862,8 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci) } } -for (i =1 ; i <MAX_HC_SLOTS ; ++i) -xhci_free_virt_device (xhci, i); +for (i =HCS_MAX_SLOTS (xhci->hcs_params1); i >0 ; i--) +xhci_free_virt_devices_depth_first (xhci, i);dma_pool_destroy (xhci->segment_pool); xhci->segment_pool =NULL ;
Signature · 1 line
1.9.1
PATCH 2/11 usb: xhci: fix return value of xhci_setup_device()
In reply to a message not in the archiveFrom: Lu Baolu <[email protected]>
xhci_setup_device() should return failure with correct error number when xhci host has died, removed or halted.
During usb device enumeration, if usb host is not accessible (died, removed or halted), the hc_driver->address_device() should return a corresponding error code to usb core. But current xhci driver just returns success. This misleads usb core to continue the enumeration by reading the device descriptor, which will result in failure, and users will get a misleading message like "device descriptor read/8, error -110".
--- drivers/usb/host/xhci.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/host/xhci.c +3 -1
@@ -3787,8 +3787,10 @@ static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,mutex_lock (&xhci->mutex); -if (xhci->xhc_state)/* dying, removing or halted */ +if (xhci->xhc_state) {/* dying, removing or halted */ + ret = -ESHUTDOWN ; goto out; + }if (!udev->slot_id) { xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Signature · 1 line
1.9.1
PATCH 3/11 usb: return error code when platform_get_irq fails
In reply to a message not in the archiveFrom: Pan Bian <[email protected]>
In function xhci_mtk_probe(), variable ret takes the return value. Its value should be negative on failures. However, when the call to function platform_get_irq() fails, it does not set the error code, and 0 will be returned. 0 indicates no error. As a result, the callers of function xhci_mtk_probe() will not be able to detect the error. This patch fixes the bug by assigning the return value of platform_get_irq() to variable ret if it fails.
--- drivers/usb/host/xhci-mtk.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-mtk.c +3 -1
@@ -579,8 +579,10 @@ static int xhci_mtk_probe(struct platform_device *pdev) goto disable_ldos; irq =platform_get_irq (pdev,0 ); -if (irq <0 ) +if (irq <0 ) { + ret = irq; goto disable_clk; + }/* Initialize dma_mask and coherent_dma_mask to 32-bits */ ret =dma_set_coherent_mask (dev,DMA_BIT_MASK (32 ));
Signature · 1 line
1.9.1
PATCH 4/11 usb: xhci: fix possible wild pointer
In reply to a message not in the archiveFrom: Lu Baolu <[email protected]>
handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd.
This might cause problem when xhci->current_cmd is accessed after that.
A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.
--- drivers/usb/host/xhci-ring.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +11 -5
@@ -1267,14 +1267,18 @@ void xhci_handle_command_timeout(unsigned long data)bool second_timeout = false; xhci = (struct xhci_hcd *) data; -/* mark this command to be cancelled */ spin_lock_irqsave (&xhci->lock, flags); -if (xhci->current_cmd) { -if (xhci->current_cmd->status ==COMP_CMD_ABORT ) - second_timeout = true; - xhci->current_cmd->status =COMP_CMD_ABORT ; + +if (!xhci->current_cmd) { +spin_unlock_irqrestore (&xhci->lock, flags); +return ; } +/* mark this command to be cancelled */ +if (xhci->current_cmd->status ==COMP_CMD_ABORT ) + second_timeout = true; + xhci->current_cmd->status =COMP_CMD_ABORT ; +/* Make sure command ring is running before aborting it */ hw_ring_state =xhci_read_64 (xhci, &xhci->op_regs->cmd_ring); if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
@@ -1422,6 +1426,8 @@ static void handle_cmd_completion(struct xhci_hcd *xhci, xhci->current_cmd =list_entry (cmd->cmd_list.next, struct xhci_command, cmd_list);mod_timer (&xhci->cmd_timer, jiffies +XHCI_CMD_DEFAULT_TIMEOUT ); + }else if (xhci->current_cmd == cmd) { + xhci->current_cmd =NULL ; } event_handled:
Signature · 1 line
1.9.1
PATCH 5/11 usb: host: xhci: Fix possible wild pointer when handling abort command
In reply to a message not in the archiveFrom: Baolin Wang <[email protected]>
When current command was supposed to be aborted, host will free the command in handle_cmd_completion() function. But it might be still referenced by xhci->current_cmd, which need to set NULL.
--- drivers/usb/host/xhci-ring.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +4 -1
@@ -1363,8 +1363,11 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,*/ if (cmd_comp_code ==COMP_CMD_ABORT ) { xhci->cmd_ring_state =CMD_RING_STATE_STOPPED ; -if (cmd->status ==COMP_CMD_ABORT ) +if (cmd->status ==COMP_CMD_ABORT ) { +if (xhci->current_cmd == cmd) + xhci->current_cmd =NULL ; goto event_handled; + } } cmd_type =TRB_FIELD_TO_TYPE (le32_to_cpu (cmd_trb->generic.field[3 ]));
Signature · 1 line
1.9.1
PATCH 6/11 xhci: Handle command completion and timeout race
In reply to a message not in the archiveIf we get a command completion event at the same time as the command timeout work starts on another cpu we might end up aborting the wrong command.
If the command completion takes the xhci lock before the timeout work, it will handle the command, pick the next command, mark it as current_cmd, and re-queue the timeout work. When the timeout work finally gets the lock It will start aborting the wrong command.
This case can be resolved by checking if the timeout work is pending inside the timeout function itself. A new timeout work can only be pending if the command completed and a new command was queued.
If there are no more commands pending then command completion will set the current_cmd to NULL, which is already handled in the timeout work.
--- drivers/usb/host/xhci-ring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +5 -1
@@ -1269,7 +1269,11 @@ void xhci_handle_command_timeout(unsigned long data)spin_lock_irqsave (&xhci->lock, flags); -if (!xhci->current_cmd) { +/* +* If timeout work is pending, or current_cmd is NULL, it means we +* raced with command completion. Command is handled so just return. +*/ +if (!xhci->current_cmd ||timer_pending (&xhci->cmd_timer)) {spin_unlock_irqrestore (&xhci->lock, flags);return ; }
Signature · 1 line
1.9.1
PATCH 7/11 usb: xhci: hold lock over xhci_abort_cmd_ring()
In reply to a message not in the archiveFrom: Lu Baolu <[email protected]>
In command timer function, xhci_handle_command_timeout(), xhci->lock is unlocked before call into xhci_abort_cmd_ring(). This might cause race between the timer function and the event handler.
The xhci_abort_cmd_ring() function sets the CMD_RING_ABORT bit in the command register and polling it until the setting takes effect. A stop command ring event might be handled between writing the abort bit and polling for it. The event handler will restart the command ring, which causes the failure of polling, and we ever believed that we failed to stop it.
As a bonus, this also fixes some issues of calling functions without locking in xhci_handle_command_timeout().
--- drivers/usb/host/xhci-ring.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-ring.c +9 -4
@@ -1287,29 +1287,34 @@ void xhci_handle_command_timeout(unsigned long data) hw_ring_state =xhci_read_64 (xhci, &xhci->op_regs->cmd_ring);if ((xhci->cmd_ring_state &CMD_RING_STATE_RUNNING ) && (hw_ring_state &CMD_RING_RUNNING )) { -spin_unlock_irqrestore (&xhci->lock, flags);xhci_dbg (xhci,"Command timeout\n" ); ret =xhci_abort_cmd_ring (xhci);if (unlikely (ret == -ESHUTDOWN )) {xhci_err (xhci,"Abort command ring failed\n" );xhci_cleanup_command_queue (xhci); +spin_unlock_irqrestore (&xhci->lock, flags);usb_hc_died (xhci_to_hcd (xhci)->primary_hcd);xhci_dbg (xhci,"xHCI host controller is dead.\n" ); + +return ; } -return ; + + goto time_out_completed; }/* command ring failed to restart, or host removed. Bail out */ if (second_timeout || xhci->xhc_state &XHCI_STATE_REMOVING ) { -spin_unlock_irqrestore (&xhci->lock, flags);xhci_dbg (xhci,"command timed out twice, ring start fail?\n" );xhci_cleanup_command_queue (xhci); -return ; + + goto time_out_completed; }/* command timeout on stopped ring, ring can't be aborted */ xhci_dbg (xhci,"Command timeout on stopped ring\n" );xhci_handle_stopped_cmd_ring (xhci, xhci->current_cmd); + +time_out_completed:spin_unlock_irqrestore (&xhci->lock, flags);return ; }
Signature · 1 line
1.9.1
PATCH 10/11 usb: xhci: apply XHCI_PME_STUCK_QUIRK to Intel Apollo Lake
In reply to a message not in the archiveFrom: Wan Ahmad Zainie <[email protected]>
Intel Apollo Lake also requires XHCI_PME_STUCK_QUIRK. Adding its PCI ID to quirk.
--- drivers/usb/host/xhci-pci.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-pci.c +2 -1
@@ -165,7 +165,8 @@ static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci) pdev->device ==PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_XHCI || pdev->device ==PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI || pdev->device ==PCI_DEVICE_ID_INTEL_BROXTON_M_XHCI || - pdev->device == PCI_DEVICE_ID_INTEL_BROXTON_B_XHCI)) { + pdev->device ==PCI_DEVICE_ID_INTEL_BROXTON_B_XHCI || + pdev->device == PCI_DEVICE_ID_INTEL_APL_XHCI)) { xhci->quirks |= XHCI_PME_STUCK_QUIRK; } if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
Signature · 1 line
1.9.1
PATCH 11/11 usb: host: xhci: handle COMP_STOP from SETUP phase too
In reply to a message not in the archiveFrom: Felipe Balbi <[email protected]>
Stop Endpoint command can come at any point and we have no control of that. We should make sure to handle COMP_STOP on SETUP phase as well, otherwise urb->actual_length might be set to negative values in some occasions such as below:
urb->length = 4;
build_control_transfer_td_for(urb, ep);
stop_endpoint(ep);
COMP_STOP:
[...] urb->actual_length = urb->length - trb->length;
trb->length is 8 for SETUP stage (8 control request bytes), so actual_length would be set to -4 in this case.
While doing that, also make sure to use TRB_TYPE field of the actual TRB instead of matching pointers to figure out in which stage of the control transfer we got our completion event.
--- drivers/usb/host/xhci-ring.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)
drivers/usb/host/xhci-ring.c +21 -12
@@ -1971,8 +1971,9 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,struct xhci_ep_ctx *ep_ctx;u32 trb_comp_code;u32 remaining, requested; -bool on_data_stage; +u32 trb_type; + trb_type =TRB_FIELD_TO_TYPE (le32_to_cpu (ep_trb->generic.field[3 ])); slot_id =TRB_TO_SLOT_ID (le32_to_cpu (event->flags)); xdev = xhci->devs[slot_id]; ep_index =TRB_TO_EP_ID (le32_to_cpu (event->flags)) -1 ;
@@ -1982,14 +1983,11 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td, requested = td->urb->transfer_buffer_length; remaining =EVENT_TRB_LEN (le32_to_cpu (event->transfer_len)); -/* not setup (dequeue), or status stage means we are at data stage */ - on_data_stage = (ep_trb != ep_ring->dequeue && ep_trb != td->last_trb); -switch (trb_comp_code) {case COMP_SUCCESS : -if (ep_trb != td->last_trb) { +if (trb_type !=TRB_STATUS ) {xhci_warn (xhci,"WARN: Success on ctrl %s TRB without IOC set?\n" , - on_data_stage ?"data" :"setup" ); + (trb_type ==TRB_DATA ) ?"data" :"setup" ); *status = -ESHUTDOWN ;break ; }
@@ -1999,15 +1997,25 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td, *status =0 ;break ;case COMP_STOP_SHORT : -if (on_data_stage) +if (trb_type ==TRB_DATA || trb_type ==TRB_NORMAL ) td->urb->actual_length = remaining;else xhci_warn (xhci,"WARN: Stopped Short Packet on ctrl setup or status TRB\n" ); goto finish_td;case COMP_STOP : -if (on_data_stage) +switch (trb_type) { +case TRB_SETUP : + td->urb->actual_length =0 ; + goto finish_td; +case TRB_DATA : +case TRB_NORMAL : td->urb->actual_length = requested - remaining; - goto finish_td; + goto finish_td; +default : +xhci_warn (xhci,"WARN: unexpected TRB Type %d\n" , + trb_type); + goto finish_td; + }case COMP_STOP_INVAL : goto finish_td;default :
@@ -2019,7 +2027,7 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,/* else fall through */ case COMP_STALL :/* Did we transfer part of the data (middle) phase? */ -if (on_data_stage) +if (trb_type ==TRB_DATA || trb_type ==TRB_NORMAL ) td->urb->actual_length = requested - remaining;else if (!td->urb_length_set) td->urb->actual_length =0 ;
@@ -2027,14 +2035,15 @@ static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td, }/* stopped at setup stage, no data transferred */ -if (ep_trb == ep_ring->dequeue) +if (trb_type ==TRB_SETUP ) goto finish_td;/* * if on data stage then update the actual_length of the URB and flag it * as set, so it won't be overwritten in the event for the last TRB. */ -if (on_data_stage) { +if (trb_type ==TRB_DATA || + trb_type ==TRB_NORMAL ) { td->urb_length_set = true; td->urb->actual_length = requested - remaining;xhci_dbg (xhci,"Waiting for status stage event\n" );
Signature · 1 line
1.9.1