Skip to contentSkip to conversation

xhci: re-work command queue management

RFC Superseded Mathias Nyman 3 participants 16 messages Jan 13, 2014 – Jan 27, 2014

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover xhci: re-work command queue management

This is an attempt to re-work and solve the issues in xhci command queue management that Sarah has descibed earlier:

Right now, the command management in the xHCI driver is rather ad-hock. Different parts of the driver all submit commands, including interrupt handling routines, functions called from the USB core (with or without the bus bandwidth mutex held).
Some times they need to wait for the command to complete, and sometimes they just issue the command and don't care about the result of the command.

The places that wait on a command all time the command for five seconds, and then attempt to cancel the command.
Unfortunately, that means if several commands are issued at once, and one of them times out, all the commands timeout, even though the host hasn't gotten a chance to service them yet.

This is apparent with some devices that take a long time to respond to the Set Address command during device enumeration (when the device is plugged in). If a driver for a different device attempts to change alternate interface settings at the same time (causing a Configure Endpoint command to be issued), both commands timeout.

Instead of having each command timeout after five seconds, the driver should wait indefinitely in an uninterruptible sleep on the command completion. A global command queue manager should time whatever command is currently running, and cancel that command after five seconds.

If the commands were in a list, like TDs currently are, it may be easier to keep track of where the command ring dequeue pointer is, and avoid racing with events. We may need to have parts of the driver that issue commands without waiting on them still put the commands in the command list.

The Implementation:
-------------------

First step is to create a list of the commands submitted to the command queue. To accomplish this each command is required to be submitted with a properly filled command structure containing completion, status variable and a pointer to the command TRB that will be used.

The first 7 patches are all about creating these command structures and submitting them when we queue commands.
The command structures are allocated on the fly, the commands that are submitted in interrupt context are allocated with GFP_ATOMIC.

Next, the global command queue is introduced. Commands are added to the queue when trb's are queued, and remove when the commad completes.
Also switch to use the status variable and completion in the command struct.

A new timer handles command timeout, the timer is kicked every time when a command finishes and there's a new command waiting in the queue, or when a new command is submitted to an _empty_ command queue.
Timer is deleted when the the last command on the queue finishes (empty queue)

The old cancel_cmd_list is removed.
When the timer expires we simply tag the current command as "ABORTED" and start the ring abortion process. Functions waiting for an aborted command to finish are called after the command abortion is completed.

Mathias Nyman (10):
xhci: Use command structures when calling xhci_configure_endpoint
xhci: use a command structure internally in xhci_address_device()
xhci: use command structures for xhci_queue_slot_control()
xhci: use command structures for xhci_queue_stop_endpoint()
xhci: use command structure for xhci_queue_new_dequeue_state()
xhci: use command structures for xhci_queue_reset_ep()
xhci: Use command structured when queuing commands
xhci: Add a global command queue
xhci: Use completion and status in global command queue
xhci: rework command timeout and cancellation,

 drivers/usb/host/xhci-hub.c  |  42 ++--
 drivers/usb/host/xhci-mem.c  |  22 +-
 drivers/usb/host/xhci-ring.c | 532 ++++++++++++++-----------------------------
 drivers/usb/host/xhci.c      | 264 +++++++++++----------
 drivers/usb/host/xhci.h      |  43 ++--
 5 files changed, 373 insertions(+), 530 deletions(-)
Signature · 1 line
1.8.1.2
#1 lore raw

RFC 1/10 xhci: Use command structures when calling xhci_configure_endpoint

To create a global command queue we need to fill a command structure for each entry on the command ring.

We start by requiring xhci_configure_endpoint() to be called with
a proper command structure. Functions xhci_check_maxpacket and xhci_check_bandwith that called xhci_configure_endpoint without a command strucure are fixed.

A special case where an endpoint needs to be configured after reset, done in interrupt context is also changed to create a command strucure. (this command structure is not used yet, but will be later when we requre all queued commands to have a command strucure)

---
 drivers/usb/host/xhci-ring.c | 10 ++---
 drivers/usb/host/xhci.c      | 95 +++++++++++++++++++++++---------------------
 2 files changed, 54 insertions(+), 51 deletions(-)
drivers/usb/host/xhci-ring.c +4 -6
index 1e2f3f4..da83a844 100644
@@ -1180,12 +1180,15 @@ static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id,
 	 * because the HW can't handle two commands being queued in a row.
 	 */
 	if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
+		struct xhci_command *command;
+		command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
 		xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
 				"Queueing configure endpoint command");
 		xhci_queue_configure_endpoint(xhci,
 				xhci->devs[slot_id]->in_ctx->dma, slot_id,
 				false);
 		xhci_ring_cmd_db(xhci);
+		kfree(command);
 	} else {
 		/* Clear our internal halted state and restart the ring(s) */
 		xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
@@ -1439,7 +1442,7 @@ static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id,
 			add_flags - SLOT_FLAG == drop_flags) {
 		ep_state = virt_dev->eps[ep_index].ep_state;
 		if (!(ep_state & EP_HALTED))
-			goto bandwidth_change;
+			return;
 		xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
 				"Completed config ep cmd - "
 				"last ep index = %d, state = %d",
@@ -1449,11 +1452,6 @@ static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id,
 		ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
 		return;
 	}
-bandwidth_change:
-	xhci_dbg_trace(xhci,  trace_xhci_dbg_context_change,
-			"Completed config ep cmd");
-	virt_dev->cmd_status = cmd_comp_code;
-	complete(&virt_dev->cmd_completion);
 	return;
 }
 
drivers/usb/host/xhci.c +50 -45

216 lines not on this page · raw message

Signature · 1 line
1.8.1.2
#2 lore raw

RFC 2/10 xhci: use a command structure internally in xhci_address_device()

In reply to Mathias Nyman

Preparing for the global command queue by using command strucure in xhci_address_device

---
 drivers/usb/host/xhci.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/usb/host/xhci.c +17 -5
index 5e65052..0167a1c 100644
@@ -3732,7 +3732,7 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 	struct xhci_slot_ctx *slot_ctx;
 	struct xhci_input_control_ctx *ctrl_ctx;
 	u64 temp_64;
-	union xhci_trb *cmd_trb;
+	struct xhci_command *command;
 
 	if (!udev->slot_id) {
 		xhci_dbg_trace(xhci, trace_xhci_dbg_address,
@@ -3753,11 +3753,19 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 		return -EINVAL;
 	}
 
+	command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
+	if (!command)
+		return -ENOMEM;
+
+	command->in_ctx = virt_dev->in_ctx;
+	command->completion = &xhci->addr_dev;
+
 	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
 	ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
 	if (!ctrl_ctx) {
 		xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
 				__func__);
+		kfree(command);
 		return -EINVAL;
 	}
 	/*
@@ -3779,20 +3787,22 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 				slot_ctx->dev_info >> 27);
 
 	spin_lock_irqsave(&xhci->lock, flags);
-	cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
+	command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
 	ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
 					udev->slot_id);
 	if (ret) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		xhci_dbg_trace(xhci, trace_xhci_dbg_address,
 				"FIXME: allocate a command ring segment");
+		kfree(command);
 		return ret;
 	}
 	xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
 
 	/* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
-	timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
+	timeleft = wait_for_completion_interruptible_timeout(
+			command->completion,
 			XHCI_CMD_DEFAULT_TIMEOUT);
 	/* FIXME: From section 4.3.4: "Software shall be responsible for timing
 	 * the SetAddress() "recovery interval" required by USB and aborting the
@@ -3802,7 +3812,8 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 		xhci_warn(xhci, "%s while waiting for address device command\n",
 				timeleft == 0 ? "Timeout" : "Signal");
 		/* cancel the address device command */
-		ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
+		ret = xhci_cancel_cmd(xhci, NULL, command->command_trb);
+		kfree(command);
 		if (ret < 0)
 			return ret;
 		return -ETIME;
@@ -3838,6 +3849,7 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 		break;
 	}
 	if (ret) {
+		kfree(command);
 		return ret;
 	}
 	temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
@@ -3872,7 +3884,7 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 	xhci_dbg_trace(xhci, trace_xhci_dbg_address,
 		       "Internal device address = %d",
 		       le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
-
+	kfree(command);
 	return 0;
 }
 
Signature · 1 line
1.8.1.2
#3 lore raw

RFC 3/10 xhci: use command structures for xhci_queue_slot_control()

In reply to Mathias Nyman

Preparing for the global command queue by changing functions calling xhci_queue_slot_control() to internally use command structures

---
 drivers/usb/host/xhci.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)
drivers/usb/host/xhci.c +29 -5
index 0167a1c..a60e432 100644
@@ -3562,6 +3562,11 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
 	unsigned long flags;
 	u32 state;
 	int i, ret;
+	struct xhci_command *command;
+
+	command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
+	if (!command)
+		return;
 
 #ifndef CONFIG_USB_DEFAULT_PERSIST
 	/*
@@ -3577,8 +3582,10 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
 	/* If the host is halted due to driver unload, we still need to free the
 	 * device.
 	 */
-	if (ret <= 0 && ret != -ENODEV)
+	if (ret <= 0 && ret != -ENODEV) {
+		kfree(command);
 		return;
+	}
 
 	virt_dev = xhci->devs[udev->slot_id];
 
@@ -3595,16 +3602,20 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
 			(xhci->xhc_state & XHCI_STATE_HALTED)) {
 		xhci_free_virt_device(xhci, udev->slot_id);
 		spin_unlock_irqrestore(&xhci->lock, flags);
+		kfree(command);
 		return;
 	}
 
 	if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
+		kfree(command);
 		return;
 	}
 	xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
+	kfree(command);
+
 	/*
 	 * Event command completion handler will free any data structures
 	 * associated with the slot.  XXX Can free sleep?
@@ -3644,31 +3655,41 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
 	unsigned long flags;
 	int timeleft;
 	int ret;
-	union xhci_trb *cmd_trb;
+	struct xhci_command *command;
+
+	command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
+	if (!command)
+		return 0;
 
 	spin_lock_irqsave(&xhci->lock, flags);
-	cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
+	command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
+	command->completion = &xhci->addr_dev;
 	ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
 	if (ret) {
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
+		kfree(command);
 		return 0;
 	}
 	xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
 
 	/* XXX: how much time for xHC slot assignment? */
-	timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
+	timeleft = wait_for_completion_interruptible_timeout(
+			command->completion,
 			XHCI_CMD_DEFAULT_TIMEOUT);
 	if (timeleft <= 0) {
 		xhci_warn(xhci, "%s while waiting for a slot\n",
 				timeleft == 0 ? "Timeout" : "Signal");
 		/* cancel the enable slot request */
-		return xhci_cancel_cmd(xhci, NULL, cmd_trb);
+		ret = xhci_cancel_cmd(xhci, NULL, command->command_trb);
+		kfree(command);
+		return ret;
 	}
 
 	if (!xhci->slot_id) {
 		xhci_err(xhci, "Error while assigning device slot ID\n");
+		kfree(command);
 		return 0;
 	}
 
@@ -3703,6 +3724,8 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
 		pm_runtime_get_noresume(hcd->self.controller);
 #endif
 
+
+	kfree(command);
 	/* Is this a LS or FS device under a HS hub? */
 	/* Hub or peripherial? */
 	return 1;
@@ -3713,6 +3736,7 @@ disable_slot:
 	if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
 		xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
+	kfree(command);
 	return 0;
 }
 
Signature · 1 line
1.8.1.2
#4 lore raw

RFC 4/10 xhci: use command structures for xhci_queue_stop_endpoint()

In reply to Mathias Nyman

Prepare for the global command ring by using command structures internally in functions calling xhci_queue_stop_endpoint()

---
 drivers/usb/host/xhci-hub.c | 14 ++++++++++++--
 drivers/usb/host/xhci.c     |  3 +++
 2 files changed, 15 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-hub.c +12 -2
index 805f234..f93c842 100644
@@ -20,7 +20,8 @@
  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include <linux/gfp.h>
+
+#include <linux/slab.h>
 #include <asm/unaligned.h>
 
 #include "xhci.h"
@@ -284,8 +285,17 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
 
 	spin_lock_irqsave(&xhci->lock, flags);
 	for (i = LAST_EP_INDEX; i > 0; i--) {
-		if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
+		if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
+			struct xhci_command *command;
+			command = xhci_alloc_command(xhci, false, false,
+						     GFP_NOIO);
+			if (!command) {
+				xhci_free_command(xhci, cmd);
+				return -ENOMEM;
+			}
 			xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
+			kfree(command);
+		}
 	}
 	cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
 	list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
drivers/usb/host/xhci.c +3 -0
index a60e432..054132b 100644
@@ -1467,6 +1467,7 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 	unsigned int ep_index;
 	struct xhci_ring *ep_ring;
 	struct xhci_virt_ep *ep;
+	struct xhci_command *command;
 
 	xhci = hcd_to_xhci(hcd);
 	spin_lock_irqsave(&xhci->lock, flags);
@@ -1536,6 +1537,7 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 	 * the first cancellation to be handled.
 	 */
 	if (!(ep->ep_state & EP_HALT_PENDING)) {
+		command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
 		ep->ep_state |= EP_HALT_PENDING;
 		ep->stop_cmds_pending++;
 		ep->stop_cmd_timer.expires = jiffies +
@@ -1543,6 +1545,7 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 		add_timer(&ep->stop_cmd_timer);
 		xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
 		xhci_ring_cmd_db(xhci);
+		kfree(command);
 	}
 done:
 	spin_unlock_irqrestore(&xhci->lock, flags);
Signature · 1 line
1.8.1.2
#5 lore raw

RFC 5/10 xhci: use command structure for xhci_queue_new_dequeue_state()

In reply to Mathias Nyman

Prepare for the global command ring by using command structures internally in functions calling xhci_queue_new_dequeue_state()

---
 drivers/usb/host/xhci-ring.c | 3 +++
 drivers/usb/host/xhci.c      | 6 ++++++
 2 files changed, 9 insertions(+)
drivers/usb/host/xhci-ring.c +3 -0
index da83a844..df5b0f8 100644
@@ -855,11 +855,14 @@ remove_finished_td:
 
 	/* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
 	if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
+		struct xhci_command *command;
+		command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
 		xhci_queue_new_dequeue_state(xhci,
 				slot_id, ep_index,
 				ep->stopped_td->urb->stream_id,
 				&deq_state);
 		xhci_ring_cmd_db(xhci);
+		kfree(command);
 	} else {
 		/* Otherwise ring the doorbell(s) to restart queued transfers */
 		ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
drivers/usb/host/xhci.c +6 -0
index 054132b..fa0ac48 100644
@@ -2867,10 +2867,16 @@ void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
 	 * issue a configure endpoint command later.
 	 */
 	if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
+		struct xhci_command *command;
+		/* Can't sleep if we're called from cleanup_halted_endpoint() */
+		command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
+		if (!command)
+			return;
 		xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
 				"Queueing new dequeue state");
 		xhci_queue_new_dequeue_state(xhci, udev->slot_id,
 				ep_index, ep->stopped_stream, &deq_state);
+		kfree(command);
 	} else {
 		/* Better hope no one uses the input context between now and the
 		 * reset endpoint completion!
Signature · 1 line
1.8.1.2
#6 lore raw

RFC 6/10 xhci: use command structures for xhci_queue_reset_ep()

In reply to Mathias Nyman

Prepare for the global command ring by using command structures internally in functions calling xhci_queue_reset_ep()

---
 drivers/usb/host/xhci-ring.c | 5 +++++
 drivers/usb/host/xhci.c      | 6 ++++++
 2 files changed, 11 insertions(+)
drivers/usb/host/xhci-ring.c +5 -0
index df5b0f8..bf50d28 100644
@@ -1913,6 +1913,10 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
 		struct xhci_td *td, union xhci_trb *event_trb)
 {
 	struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
+	struct xhci_command *command;
+	command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
+	if (!command)
+		return;
 	ep->ep_state |= EP_HALTED;
 	ep->stopped_td = td;
 	ep->stopped_trb = event_trb;
@@ -1926,6 +1930,7 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
 	ep->stopped_stream = 0;
 
 	xhci_ring_cmd_db(xhci);
+	kfree(command);
 }
 
 /* Check if an error has halted the endpoint ring.  The class driver will
drivers/usb/host/xhci.c +6 -0
index fa0ac48..23e057f 100644
@@ -2906,6 +2906,7 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
 	unsigned long flags;
 	int ret;
 	struct xhci_virt_ep *virt_ep;
+	struct xhci_command *command;
 
 	xhci = hcd_to_xhci(hcd);
 	udev = (struct usb_device *) ep->hcpriv;
@@ -2928,6 +2929,10 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
 		return;
 	}
 
+	command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
+	if (!command)
+		return;
+
 	xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
 			"Queueing reset endpoint command");
 	spin_lock_irqsave(&xhci->lock, flags);
@@ -2946,6 +2951,7 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
 	virt_ep->stopped_trb = NULL;
 	virt_ep->stopped_stream = 0;
 	spin_unlock_irqrestore(&xhci->lock, flags);
+	kfree(command);
 
 	if (ret)
 		xhci_warn(xhci, "FIXME allocate a new ring segment\n");
Signature · 1 line
1.8.1.2
#7 lore raw

RFC 7/10 xhci: Use command structured when queuing commands

In reply to Mathias Nyman

Require each queued command to have a command structure. We store the command trb pointer in the structure when queuing it, making the find_next_enqueue() function obsolete.

Don't free the command strucures right away after sending the commands, We will free the commands when we receive a "command handled" event in a later patch.

---
 drivers/usb/host/xhci-hub.c  |  8 ++--
 drivers/usb/host/xhci-ring.c | 94 ++++++++++++++++++++++----------------------
 drivers/usb/host/xhci.c      | 47 +++++++++++-----------
 drivers/usb/host/xhci.h      | 31 ++++++++-------
 4 files changed, 91 insertions(+), 89 deletions(-)
drivers/usb/host/xhci-hub.c +4 -4
index f93c842..3a08e26 100644
@@ -292,14 +292,14 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
 			if (!command) {
 				xhci_free_command(xhci, cmd);
 				return -ENOMEM;
+
 			}
-			xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
-			kfree(command);
+			xhci_queue_stop_endpoint(xhci, command, slot_id, i,
+						 suspend);
 		}
 	}
-	cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
 	list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
-	xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
+	xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
 	xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
 
drivers/usb/host/xhci-ring.c +46 -48

233 lines not on this page · raw message

drivers/usb/host/xhci.c +24 -23

132 lines not on this page · raw message

drivers/usb/host/xhci.h +17 -14
index 03c74b7..d02b73d 100644
@@ -1811,13 +1811,14 @@ struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
 		dma_addr_t suspect_dma);
 int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code);
 void xhci_ring_cmd_db(struct xhci_hcd *xhci);
-int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id);
-int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
-		u32 slot_id);
-int xhci_queue_vendor_command(struct xhci_hcd *xhci,
+int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd,
+			    u32 trb_type, u32 slot_id);
+int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
+			      dma_addr_t in_ctx_ptr, u32 slot_id);
+int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		u32 field1, u32 field2, u32 field3, u32 field4);
-int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
-		unsigned int ep_index, int suspend);
+int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd,
+		int slot_id, unsigned int ep_index, int suspend);
 int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
 		int slot_id, unsigned int ep_index);
 int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
@@ -1826,18 +1827,21 @@ int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
 		int slot_id, unsigned int ep_index);
 int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
 		struct urb *urb, int slot_id, unsigned int ep_index);
-int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
-		u32 slot_id, bool command_must_succeed);
-int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
-		u32 slot_id, bool command_must_succeed);
-int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
-		unsigned int ep_index);
-int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id);
+int xhci_queue_configure_endpoint(struct xhci_hcd *xhci,
+		struct xhci_command *cmd, dma_addr_t in_ctx_ptr, u32 slot_id,
+		bool command_must_succeed);
+int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd,
+		dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed);
+int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd,
+		int slot_id, unsigned int ep_index);
+int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd,
+		u32 slot_id);
 void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
 		unsigned int slot_id, unsigned int ep_index,
 		unsigned int stream_id, struct xhci_td *cur_td,
 		struct xhci_dequeue_state *state);
 void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
+		struct xhci_command *cmd,
 		unsigned int slot_id, unsigned int ep_index,
 		unsigned int stream_id,
 		struct xhci_dequeue_state *deq_state);
@@ -1851,7 +1855,6 @@ int xhci_cancel_cmd(struct xhci_hcd *xhci, struct xhci_command *command,
 		union xhci_trb *cmd_trb);
 void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id,
 		unsigned int ep_index, unsigned int stream_id);
-union xhci_trb *xhci_find_next_enqueue(struct xhci_ring *ring);
 
 /* xHCI roothub code */
 void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
Signature · 1 line
1.8.1.2
#8 lore raw

RFC 8/10 xhci: Add a global command queue

In reply to Mathias Nyman

Create a list to store command structures, add a strucure to it every time a command is submitted, and remove it from the list once we get a command completion event matching the command.

---
 drivers/usb/host/xhci-mem.c  |  8 ++++++++
 drivers/usb/host/xhci-ring.c | 13 ++++++++++++-
 drivers/usb/host/xhci.h      |  1 +
 3 files changed, 21 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-mem.c +8 -0
index 49b8bd0..7f8e4c3 100644
@@ -1694,6 +1694,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 {
 	struct pci_dev	*pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
 	struct xhci_cd  *cur_cd, *next_cd;
+	struct xhci_command *cur_cmd, *next_cmd;
 	int size;
 	int i, j, num_ports;
 
@@ -1722,6 +1723,12 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 		kfree(cur_cd);
 	}
 
+	list_for_each_entry_safe(cur_cmd, next_cmd,
+			&xhci->cmd_list, cmd_list) {
+		list_del(&cur_cmd->cmd_list);
+		kfree(cur_cmd);
+	}
+
 	for (i = 1; i < MAX_HC_SLOTS; ++i)
 		xhci_free_virt_device(xhci, i);
 
@@ -2223,6 +2230,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 	int i;
 
 	INIT_LIST_HEAD(&xhci->cancel_cmd_list);
+	INIT_LIST_HEAD(&xhci->cmd_list);
 
 	page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
drivers/usb/host/xhci-ring.c +12 -1
index 6f6018c..5ccf642 100644
@@ -1502,6 +1502,7 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
 	dma_addr_t cmd_dequeue_dma;
 	u32 cmd_comp_code;
 	union xhci_trb *cmd_trb;
+	struct xhci_command *cmd;
 	u32 cmd_type;
 
 	cmd_dma = le64_to_cpu(event->cmd_trb);
@@ -1519,6 +1520,13 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
 		return;
 	}
 
+	cmd = list_entry(xhci->cmd_list.next, struct xhci_command, cmd_list);
+
+	if (cmd->command_trb != xhci->cmd_ring->dequeue) {
+		xhci_err(xhci,
+			 "Command completion event does not match command\n");
+		return;
+	}
 	trace_xhci_cmd_completion(cmd_trb, (struct xhci_generic_trb *) event);
 
 	cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status));
@@ -1588,6 +1596,9 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
 		xhci->error_bitmask |= 1 << 6;
 		break;
 	}
+
+	list_del(&cmd->cmd_list);
+
 	inc_deq(xhci, xhci->cmd_ring);
 }
 
@@ -3989,7 +4000,7 @@ static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
 		return ret;
 	}
 	cmd->command_trb = xhci->cmd_ring->enqueue;
-
+	list_add_tail(&cmd->cmd_list, &xhci->cmd_list);
 	queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
 			field4 | xhci->cmd_ring->cycle_state);
 	return 0;
drivers/usb/host/xhci.h +1 -0
index d02b73d..71aed35 100644
@@ -1475,6 +1475,7 @@ struct xhci_hcd {
 #define CMD_RING_STATE_ABORTED         (1 << 1)
 #define CMD_RING_STATE_STOPPED         (1 << 2)
 	struct list_head        cancel_cmd_list;
+	struct list_head        cmd_list;
 	unsigned int		cmd_ring_reserved_trbs;
 	struct xhci_ring	*event_ring;
 	struct xhci_erst	erst;
Signature · 1 line
1.8.1.2
#9 lore raw

RFC 9/10 xhci: Use completion and status in global command queue

In reply to Mathias Nyman

Remove the per-device command list and handle_cmd_in_cmd_wait_list() and use the completion and status variables found in the command structure in the global command list.

---
 drivers/usb/host/xhci-hub.c  | 11 ------
 drivers/usb/host/xhci-mem.c  |  1 -
 drivers/usb/host/xhci-ring.c | 79 ++++++++------------------------------------
 drivers/usb/host/xhci.c      | 20 ++---------
 drivers/usb/host/xhci.h      |  3 --
 5 files changed, 17 insertions(+), 97 deletions(-)
drivers/usb/host/xhci-hub.c +0 -11
index 3a08e26..78983d6 100644
@@ -298,7 +298,6 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
 						 suspend);
 		}
 	}
-	list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
 	xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
 	xhci_ring_cmd_db(xhci);
 	spin_unlock_irqrestore(&xhci->lock, flags);
@@ -310,18 +309,8 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
 	if (timeleft <= 0) {
 		xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
 				timeleft == 0 ? "Timeout" : "Signal");
-		spin_lock_irqsave(&xhci->lock, flags);
-		/* The timeout might have raced with the event ring handler, so
-		 * only delete from the list if the item isn't poisoned.
-		 */
-		if (cmd->cmd_list.next != LIST_POISON1)
-			list_del(&cmd->cmd_list);
-		spin_unlock_irqrestore(&xhci->lock, flags);
 		ret = -ETIME;
-		goto command_cleanup;
 	}
-
-command_cleanup:
 	xhci_free_command(xhci, cmd);
 	return ret;
 }
drivers/usb/host/xhci-mem.c +0 -1
index 7f8e4c3..80b9c11 100644
@@ -919,7 +919,6 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
 	dev->num_rings_cached = 0;
 
 	init_completion(&dev->cmd_completion);
-	INIT_LIST_HEAD(&dev->cmd_list);
 	dev->udev = udev;
 
 	/* Point to output device context in dcbaa. */
drivers/usb/host/xhci-ring.c +14 -65

147 lines not on this page · raw message

drivers/usb/host/xhci.c +3 -17
index 6d6e888..6753da3 100644
@@ -2619,8 +2619,6 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 		return -ENOMEM;
 	}
 
-	list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
-
 	if (!ctx_change)
 		ret = xhci_queue_configure_endpoint(xhci, command,
 				command->in_ctx->dma,
@@ -2630,7 +2628,6 @@ static int xhci_configure_endpoint(struct xhci_hcd *xhci,
 				command->in_ctx->dma,
 				udev->slot_id, must_succeed);
 	if (ret < 0) {
-		list_del(&command->cmd_list);
 		if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
 			xhci_free_host_resources(xhci, ctrl_ctx);
 		spin_unlock_irqrestore(&xhci->lock, flags);
@@ -3467,11 +3464,9 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
 	/* Attempt to submit the Reset Device command to the command ring */
 	spin_lock_irqsave(&xhci->lock, flags);
 
-	list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
 	ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
 	if (ret) {
 		xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
-		list_del(&reset_device_cmd->cmd_list);
 		spin_unlock_irqrestore(&xhci->lock, flags);
 		goto command_cleanup;
 	}
@@ -3485,13 +3480,6 @@ int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
 	if (timeleft <= 0) {
 		xhci_warn(xhci, "%s while waiting for reset device command\n",
 				timeleft == 0 ? "Timeout" : "Signal");
-		spin_lock_irqsave(&xhci->lock, flags);
-		/* The timeout might have raced with the event ring handler, so
-		 * only delete from the list if the item isn't poisoned.
-		 */
-		if (reset_device_cmd->cmd_list.next != LIST_POISON1)
-			list_del(&reset_device_cmd->cmd_list);
-		spin_unlock_irqrestore(&xhci->lock, flags);
 		ret = -ETIME;
 		goto command_cleanup;
 	}
@@ -3698,7 +3686,6 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
 				timeleft == 0 ? "Timeout" : "Signal");
 		/* cancel the enable slot request */
 		ret = xhci_cancel_cmd(xhci, NULL, command->command_trb);
-		kfree(command);
 		return ret;
 	}
 
@@ -3853,13 +3840,12 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 				timeleft == 0 ? "Timeout" : "Signal");
 		/* cancel the address device command */
 		ret = xhci_cancel_cmd(xhci, NULL, command->command_trb);
-		kfree(command);
 		if (ret < 0)
 			return ret;
 		return -ETIME;
 	}
 
-	switch (virt_dev->cmd_status) {
+	switch (command->status) {
 	case COMP_CTX_STATE:
 	case COMP_EBADSLT:
 		xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
@@ -3880,8 +3866,8 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
 				"Successful Address Device command");
 		break;
 	default:
-		xhci_err(xhci, "ERROR: unexpected command completion "
-				"code 0x%x.\n", virt_dev->cmd_status);
+		xhci_err(xhci, "ERROR: unexpected command completion code 0x%x\n.",
+				command->status);
 		xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
 		xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
 		trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
drivers/usb/host/xhci.h +0 -3
index 71aed35..afff1da 100644
@@ -938,9 +938,6 @@ struct xhci_virt_device {
 #define	XHCI_MAX_RINGS_CACHED	31
 	struct xhci_virt_ep		eps[31];
 	struct completion		cmd_completion;
-	/* Status of the last command issued for this device */
-	u32				cmd_status;
-	struct list_head		cmd_list;
 	u8				fake_port;
 	u8				real_port;
 	struct xhci_interval_bw_table	*bw_table;
Signature · 1 line
1.8.1.2
#10 lore raw

RFC 10/10 xhci: rework command timeout and cancellation,

In reply to Mathias Nyman

Use one timer to control command timeout.

start/kick the timer every time a command is completed and a new command is waiting, or a new command is added to a empty list.

If the timer runs out, then tag the current command as "aborted", and start the xhci command abortion process.

Previously each function that submitted a command had its own timer. If that command timed out, a new command structure for the command was created and it was put on a cancel_cmd_list list, then a pci write to abort the command ring was issued.

when the ring was aborted, it checked if the current command was the one to be cancelled, later when the ring was stopped the driver got ownership of the TRBs in the command ring, compared then to the TRBs in the cancel_cmd_list,
and turned them into No-ops.

Now, instead, at timeout we tag the status of the command in the command queue to be aborted, and start the ring abortion.
Ring abortion stops the command ring and gives control of the commands to us. All the aborted commands are now turned into No-ops.

This allows us to remove the entire cancel_cmd_list code.

The functions waiting for a command to finish no longer have their own timeouts. They will wait either until the command completes normally,
or until the whole command abortion is done.

---
 drivers/usb/host/xhci-hub.c  |  11 +-
 drivers/usb/host/xhci-mem.c  |  15 +-
 drivers/usb/host/xhci-ring.c | 336 +++++++++++++------------------------------
 drivers/usb/host/xhci.c      |  79 ++++------
 drivers/usb/host/xhci.h      |   8 +-
 5 files changed, 139 insertions(+), 310 deletions(-)
drivers/usb/host/xhci-hub.c +4 -7
index 78983d6..34daa5a 100644
@@ -271,7 +271,6 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
 	struct xhci_virt_device *virt_dev;
 	struct xhci_command *cmd;
 	unsigned long flags;
-	int timeleft;
 	int ret;
 	int i;
 
@@ -303,12 +302,10 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
 	spin_unlock_irqrestore(&xhci->lock, flags);
 
 	/* Wait for last stop endpoint command to finish */
-	timeleft = wait_for_completion_interruptible_timeout(
-			cmd->completion,
-			XHCI_CMD_DEFAULT_TIMEOUT);
-	if (timeleft <= 0) {
-		xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
-				timeleft == 0 ? "Timeout" : "Signal");
+	wait_for_completion(cmd->completion);
+
+	if (cmd->status == COMP_CMD_ABORT || cmd->status == COMP_CMD_STOP) {
+		xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
 		ret = -ETIME;
 	}
 	xhci_free_command(xhci, cmd);
drivers/usb/host/xhci-mem.c +8 -7
index 80b9c11..d27178b 100644
@@ -1692,7 +1692,6 @@ void xhci_free_command(struct xhci_hcd *xhci,
 void xhci_mem_cleanup(struct xhci_hcd *xhci)
 {
 	struct pci_dev	*pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
-	struct xhci_cd  *cur_cd, *next_cd;
 	struct xhci_command *cur_cmd, *next_cmd;
 	int size;
 	int i, j, num_ports;
@@ -1712,15 +1711,13 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
 	if (xhci->lpm_command)
 		xhci_free_command(xhci, xhci->lpm_command);
 	xhci->cmd_ring_reserved_trbs = 0;
+
+	del_timer_sync(&xhci->cmd_timer);
+
 	if (xhci->cmd_ring)
 		xhci_ring_free(xhci, xhci->cmd_ring);
 	xhci->cmd_ring = NULL;
 	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring");
-	list_for_each_entry_safe(cur_cd, next_cd,
-			&xhci->cancel_cmd_list, cancel_cmd_list) {
-		list_del(&cur_cd->cancel_cmd_list);
-		kfree(cur_cd);
-	}
 
 	list_for_each_entry_safe(cur_cmd, next_cmd,
 			&xhci->cmd_list, cmd_list) {
@@ -2228,7 +2225,6 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 	u32 page_size, temp;
 	int i;
 
-	INIT_LIST_HEAD(&xhci->cancel_cmd_list);
 	INIT_LIST_HEAD(&xhci->cmd_list);
 
 	page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
@@ -2414,6 +2410,11 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 			"Wrote ERST address to ir_set 0.");
 	xhci_print_ir_set(xhci, 0);
 
+	/* init command timeout timer */
+	init_timer(&xhci->cmd_timer);
+	xhci->cmd_timer.data = (unsigned long) xhci;
+	xhci->cmd_timer.function = xhci_handle_command_timeout;
+
 	/*
 	 * XXX: Might need to set the Interrupter Moderation Register to
 	 * something other than the default (~1ms minimum between interrupts).
drivers/usb/host/xhci-ring.c +97 -239

382 lines not on this page · raw message

drivers/usb/host/xhci.c +26 -53

151 lines not on this page · raw message

drivers/usb/host/xhci.h +4 -4
index afff1da..14da9fc 100644
@@ -1287,7 +1287,6 @@ struct xhci_td {
 
 /* command descriptor */
 struct xhci_cd {
-	struct list_head	cancel_cmd_list;
 	struct xhci_command	*command;
 	union xhci_trb		*cmd_trb;
 };
@@ -1471,9 +1470,10 @@ struct xhci_hcd {
 #define CMD_RING_STATE_RUNNING         (1 << 0)
 #define CMD_RING_STATE_ABORTED         (1 << 1)
 #define CMD_RING_STATE_STOPPED         (1 << 2)
-	struct list_head        cancel_cmd_list;
 	struct list_head        cmd_list;
 	unsigned int		cmd_ring_reserved_trbs;
+	struct timer_list	cmd_timer;
+	struct xhci_command	*current_cmd;
 	struct xhci_ring	*event_ring;
 	struct xhci_erst	erst;
 	/* Scratchpad */
@@ -1849,8 +1849,8 @@ void xhci_queue_config_ep_quirk(struct xhci_hcd *xhci,
 		unsigned int slot_id, unsigned int ep_index,
 		struct xhci_dequeue_state *deq_state);
 void xhci_stop_endpoint_command_watchdog(unsigned long arg);
-int xhci_cancel_cmd(struct xhci_hcd *xhci, struct xhci_command *command,
-		union xhci_trb *cmd_trb);
+void xhci_handle_command_timeout(unsigned long data);
+
 void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, unsigned int slot_id,
 		unsigned int ep_index, unsigned int stream_id);
 
Signature · 1 line
1.8.1.2
#11 lore raw
In reply to Mathias Nyman

From: Mathias Nyman

Quoted 10 lines

This is an attempt to re-work and solve the issues in xhci command queue management that Sarah has descibed earlier:

Right now, the command management in the xHCI driver is rather ad-hock. Different parts of the driver all submit commands, including interrupt handling routines, functions called from the USB core (with or without the bus bandwidth mutex held).
Some times they need to wait for the command to complete, and sometimes they just issue the command and don't care about the result of the command.

...

Quoted 9 lines

The Implementation:
-------------------

First step is to create a list of the commands submitted to the command queue. To accomplish this each command is required to be submitted with a properly filled command structure containing completion, status variable and a pointer to the command TRB that will be used.

Quoted 8 lines

The first 7 patches are all about creating these command structures and submitting them when we queue commands.
The command structures are allocated on the fly, the commands that are submitted in interrupt context are allocated with GFP_ATOMIC.

Next, the global command queue is introduced. Commands are added to the queue when trb's are queued, and remove when the commad completes.
Also switch to use the status variable and completion in the command struct.

...

IMHO the xhci driver is already far too complicated, and this probably just makes it even worse.

The fact that you are having to allocate memory ion an ISR ought also to be ringing alarm bells.

Have you considered adding a 'software command ring' (indexed with the same value as the hardware one) and using it to hold additional parameters?

It might even be worth only putting a single command into the hardware ring! That might simplify the timer code.

This still has a fixed constraint on the number of queued commands, but I suspect that is bounded anyway (a few per device?).
If not you can almost certainly arrange to grow the soft-ring before the isr code can run out of entries.

David

#12 lore raw
Quoted 3 lines

IMHO the xhci driver is already far too complicated, and this probably just makes it even worse.

This is an attempt to make it cleaner and less complicated. In the end this series removes far more more code than it adds:

5 files changed, 373 insertions(+), 530 deletions(-)

The timers are also simplified, so now we only have one timeout timer instead of each command running its own timer.

Quoted 3 lines

The fact that you are having to allocate memory ion an ISR ought also to be ringing alarm bells.

It did.
Other options are as you said to use a 'software command ring'. Either just pre-allocate a full command ring (64 trbs * sizeof(struct xhci_command)), roughly 2300 bytes when driver loads, or then a smaller pool of commands just used for ISR submitted commands.
(We then need to either either expand pool, or start allocating in isr if pool is empty)

These solutions require some ring management and possibly expansion code, and increases the complexity again. I selected this simpler approach as I understood that the frequency of commands allocated in ISR is quite low.

This also feels like trying to optimize before we get the main changes working.

Quoted 6 lines

Have you considered adding a 'software command ring' (indexed with the same value as the hardware one) and using it to hold additional parameters?

It might even be worth only putting a single command into the hardware ring! That might simplify the timer code.

This is something I haven't thought about. Could be one possibility, but feels like artificially restricting something the HW is designed to care of. The timer code isn't that complex anyway. In addition to init/exit parts we basically got:

queue_command(...)
{
if (command list is empty)

    /* start timer for new command added to empty list*/
    mod_timer(cmd_timer, timeout);
}

handle_cmd_completion(...) /* in ISR */
{
/* A commandcompleted, remove timeout timer */
del_timer(cmd_timer)

do_other_stuff

if(more commands in list)
mod_timer(cmd_timer, timeout); /* start timer for next command */
}

Quoted 3 lines

This still has a fixed constraint on the number of queued commands, but I suspect that is bounded anyway (a few per device?).

64 commands fit on the command ring segment, I'm not aware of any per-device limits?

Thanks for the comments and ideas

-Mathias

#13 lore raw

From: Mathias Nyman
...

Quoted 10 lines
Quoted 2 lines

The fact that you are having to allocate memory ion an ISR ought also to be ringing alarm bells.

It did.
Other options are as you said to use a 'software command ring'. Either just pre-allocate a full command ring (64 trbs * sizeof(struct xhci_command)), roughly 2300 bytes when driver loads,
or then a smaller pool of commands just used for ISR submitted commands. (We then need to either either expand pool, or start allocating in isr if pool is empty)

If you pre-allocate mapping 1-1 with the command ring entries you don't need many of the fields of xhci_command at all. So the allocated size will be much smaller.

Also not that all the rings have been increased to 256 entries, not that there is any requirement for them to match.

Quoted 4 lines

These solutions require some ring management and possibly expansion code, and increases the complexity again. I selected this simpler approach as I understood that the frequency of commands allocated in ISR is quite low.

The frequency isn't the problem - the fact that it is allowed is a problem.

Quoted 2 lines

This also feels like trying to optimize before we get the main changes working.

I would call it 'not realising the full scope of the problem' and thus leaving the 'difficult bits' for last.
Sorting out how to solve the difficult bits up front can lead to a nicer solution.

Quoted 11 lines
Quoted 7 lines

Have you considered adding a 'software command ring' (indexed with the same value as the hardware one) and using it to hold additional parameters?

It might even be worth only putting a single command into the hardware ring! That might simplify the timer code.

This is something I haven't thought about. Could be one possibility, but feels like artificially restricting something the HW is designed to care of.

It might resolve any issues about the fixed finite size of the hardware command ring.
OTOH you probably don't need that many entries in the hardware command ring for 'normal' processing - provided you have a software ring/queue that can buffer all the requests.

Looking at the number of fields in xci_command, why do you need the caller to pass a structure at all?
Just make the fields parameters to the 'send a command' function along with a parameter that says whether it can sleep (in kmalloc() or if the ring is full depending on the implementation).

...

Quoted 5 lines
Quoted 2 lines

This still has a fixed constraint on the number of queued commands, but I suspect that is bounded anyway (a few per device?).

64 commands fit on the command ring segment, I'm not aware of any per-device limits?

I was wondering about how many commands the software might try to queue, not the number that the hardware allowed to be queued.

David

#14 lore raw
Quoted 13 lines · David Laight

From: Mathias Nyman
...

Quoted 10 lines
Quoted 2 lines

The fact that you are having to allocate memory ion an ISR ought also to be ringing alarm bells.

It did.
Other options are as you said to use a 'software command ring'. Either just pre-allocate a full command ring (64 trbs * sizeof(struct xhci_command)), roughly 2300 bytes when driver loads,
or then a smaller pool of commands just used for ISR submitted commands. (We then need to either either expand pool, or start allocating in isr if pool is empty)

Quoted 3 lines

If you pre-allocate mapping 1-1 with the command ring entries you don't need many of the fields of xhci_command at all. So the allocated size will be much smaller.

True, then the struct list_head could be removed.
Even the trb pointer could be removed if the command ring stays in one segment(just use the trb - base as an index )

...

Quoted 5 lines

Looking at the number of fields in xci_command, why do you need the caller to pass a structure at all?
Just make the fields parameters to the 'send a command' function along with a parameter that says whether it can sleep (in kmalloc() or if the ring is full depending on the implementation).

Command completion events only tell which trb completed and its status. The structure is needed to map the trb to the right completion so that the caller can continue running, check status and move on.

The xhci_command fields forISR allocated commands are really not used that much. Only status is used if the command timeouts. But we want these command structures on the command list to make sure eveything else works (timeout works, commands completion events are in the same order as commands on our command list etc)

So in a way we're allocating memory in ISR which is not really even used.

I'll try to create something to that avoid that

-Mathias

#15 lore raw
In reply to Mathias Nyman

Hi Keith,

You've told me in the past that you've run into an issue where you can hang the xHCI driver when one of your TeleMetrum boards refuses to respond to a Set Address command.

Can you test the following patchset, and see if it fixes your problem? I've applied the patchset against 3.13 here:

https://git.kernel.org/cgit/linux/kernel/git/sarah/xhci.git/log/?h=for-usb-next-command-queue

Thanks,
Sarah Sharp

Quoted 81 lines · Mathias Nyman

This is an attempt to re-work and solve the issues in xhci command queue management that Sarah has descibed earlier:

Right now, the command management in the xHCI driver is rather ad-hock. Different parts of the driver all submit commands, including interrupt handling routines, functions called from the USB core (with or without the bus bandwidth mutex held).
Some times they need to wait for the command to complete, and sometimes they just issue the command and don't care about the result of the command.

The places that wait on a command all time the command for five seconds, and then attempt to cancel the command.
Unfortunately, that means if several commands are issued at once, and one of them times out, all the commands timeout, even though the host hasn't gotten a chance to service them yet.

This is apparent with some devices that take a long time to respond to the Set Address command during device enumeration (when the device is plugged in). If a driver for a different device attempts to change alternate interface settings at the same time (causing a Configure Endpoint command to be issued), both commands timeout.

Instead of having each command timeout after five seconds, the driver should wait indefinitely in an uninterruptible sleep on the command completion. A global command queue manager should time whatever command is currently running, and cancel that command after five seconds.

If the commands were in a list, like TDs currently are, it may be easier to keep track of where the command ring dequeue pointer is, and avoid racing with events. We may need to have parts of the driver that issue commands without waiting on them still put the commands in the command list.

The Implementation:
-------------------

First step is to create a list of the commands submitted to the command queue. To accomplish this each command is required to be submitted with a properly filled command structure containing completion, status variable and a pointer to the command TRB that will be used.

The first 7 patches are all about creating these command structures and submitting them when we queue commands.
The command structures are allocated on the fly, the commands that are submitted in interrupt context are allocated with GFP_ATOMIC.

Next, the global command queue is introduced. Commands are added to the queue when trb's are queued, and remove when the commad completes.
Also switch to use the status variable and completion in the command struct.

A new timer handles command timeout, the timer is kicked every time when a command finishes and there's a new command waiting in the queue, or when a new command is submitted to an _empty_ command queue.
Timer is deleted when the the last command on the queue finishes (empty queue)

The old cancel_cmd_list is removed.
When the timer expires we simply tag the current command as "ABORTED" and start the ring abortion process. Functions waiting for an aborted command to finish are called after the command abortion is completed.

Mathias Nyman (10):
xhci: Use command structures when calling xhci_configure_endpoint
xhci: use a command structure internally in xhci_address_device()
xhci: use command structures for xhci_queue_slot_control()
xhci: use command structures for xhci_queue_stop_endpoint()
xhci: use command structure for xhci_queue_new_dequeue_state()
xhci: use command structures for xhci_queue_reset_ep()
xhci: Use command structured when queuing commands
xhci: Add a global command queue
xhci: Use completion and status in global command queue
xhci: rework command timeout and cancellation,

 drivers/usb/host/xhci-hub.c  |  42 ++--
 drivers/usb/host/xhci-mem.c  |  22 +-
 drivers/usb/host/xhci-ring.c | 532 ++++++++++++++-----------------------------
 drivers/usb/host/xhci.c      | 264 +++++++++++----------
 drivers/usb/host/xhci.h      |  43 ++--
 5 files changed, 373 insertions(+), 530 deletions(-)
Signature · 1 line
1.8.1.2