5544c1b
From ade9c6dfbbf573c22800d7ff6df7f0de933e7c96 Mon Sep 17 00:00:00 2001
c8dfc65
From: Hans de Goede <hdegoede@redhat.com>
5544c1b
Date: Wed, 12 Sep 2012 15:08:32 +0200
5544c1b
Subject: [PATCH] ehci: Don't set seen to 0 when removing unseen queue-heads
c8dfc65
c8dfc65
When removing unseen queue-heads from the async queue list, we should not
c8dfc65
set the seen flag to 0, as this may cause them to be removed by
c8dfc65
ehci_queues_rip_unused() during the next call to ehci_advance_async_state()
c8dfc65
if the timer is late or running at a low frequency.
c8dfc65
c8dfc65
Note:
c8dfc65
1) This *may* have caused the instant unlink / relinks described in commit
c8dfc65
   9bc3a3a216e2689bfcdd36c3e079333bbdbf3ba0
c8dfc65
c8dfc65
2) Rather then putting more if-s inside ehci_queues_rip_unused, this patch
c8dfc65
   instead introduces a new ehci_queues_rip_unseen function.
c8dfc65
c8dfc65
3) This patch also makes it save to call ehci_queues_rip_unseen() multiple
c8dfc65
   times, which gets used in the folluw up patch titled:
c8dfc65
   "ehci: Walk async schedule before and after migration"
c8dfc65
c8dfc65
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
5544c1b
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
5544c1b
(cherry picked from commit 8f5457eb04140714eaf57a99bc08dc661d83fa87)
5544c1b
5544c1b
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
c8dfc65
---
c8dfc65
 hw/usb/hcd-ehci.c | 24 ++++++++++++++++++------
c8dfc65
 1 file changed, 18 insertions(+), 6 deletions(-)
c8dfc65
c8dfc65
diff --git a/hw/usb/hcd-ehci.c b/hw/usb/hcd-ehci.c
5544c1b
index 017a01d..bc86460 100644
c8dfc65
--- a/hw/usb/hcd-ehci.c
c8dfc65
+++ b/hw/usb/hcd-ehci.c
5544c1b
@@ -848,10 +848,10 @@ static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr,
c8dfc65
     return NULL;
c8dfc65
 }
c8dfc65
 
c8dfc65
-static void ehci_queues_rip_unused(EHCIState *ehci, int async, int flush)
c8dfc65
+static void ehci_queues_rip_unused(EHCIState *ehci, int async)
c8dfc65
 {
c8dfc65
     EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
c8dfc65
-    const char *warn = (async && !flush) ? "guest unlinked busy QH" : NULL;
c8dfc65
+    const char *warn = async ? "guest unlinked busy QH" : NULL;
c8dfc65
     uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4;
c8dfc65
     EHCIQueue *q, *tmp;
c8dfc65
 
5544c1b
@@ -861,13 +861,25 @@ static void ehci_queues_rip_unused(EHCIState *ehci, int async, int flush)
c8dfc65
             q->ts = ehci->last_run_ns;
c8dfc65
             continue;
c8dfc65
         }
c8dfc65
-        if (!flush && ehci->last_run_ns < q->ts + maxage) {
c8dfc65
+        if (ehci->last_run_ns < q->ts + maxage) {
c8dfc65
             continue;
c8dfc65
         }
c8dfc65
         ehci_free_queue(q, warn);
c8dfc65
     }
c8dfc65
 }
c8dfc65
 
c8dfc65
+static void ehci_queues_rip_unseen(EHCIState *ehci, int async)
c8dfc65
+{
c8dfc65
+    EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
c8dfc65
+    EHCIQueue *q, *tmp;
c8dfc65
+
c8dfc65
+    QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
c8dfc65
+        if (!q->seen) {
c8dfc65
+            ehci_free_queue(q, NULL);
c8dfc65
+        }
c8dfc65
+    }
c8dfc65
+}
c8dfc65
+
c8dfc65
 static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
c8dfc65
 {
c8dfc65
     EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
5544c1b
@@ -1700,7 +1712,7 @@ static int ehci_state_waitlisthead(EHCIState *ehci,  int async)
c8dfc65
         ehci_set_usbsts(ehci, USBSTS_REC);
c8dfc65
     }
c8dfc65
 
c8dfc65
-    ehci_queues_rip_unused(ehci, async, 0);
c8dfc65
+    ehci_queues_rip_unused(ehci, async);
c8dfc65
 
c8dfc65
     /*  Find the head of the list (4.9.1.1) */
c8dfc65
     for(i = 0; i < MAX_QH; i++) {
5544c1b
@@ -2332,7 +2344,7 @@ static void ehci_advance_async_state(EHCIState *ehci)
c8dfc65
          */
c8dfc65
         if (ehci->usbcmd & USBCMD_IAAD) {
c8dfc65
             /* Remove all unseen qhs from the async qhs queue */
c8dfc65
-            ehci_queues_rip_unused(ehci, async, 1);
c8dfc65
+            ehci_queues_rip_unseen(ehci, async);
c8dfc65
             trace_usb_ehci_doorbell_ack();
c8dfc65
             ehci->usbcmd &= ~USBCMD_IAAD;
c8dfc65
             ehci_raise_irq(ehci, USBSTS_IAA);
5544c1b
@@ -2385,7 +2397,7 @@ static void ehci_advance_periodic_state(EHCIState *ehci)
c8dfc65
         ehci_set_fetch_addr(ehci, async,entry);
c8dfc65
         ehci_set_state(ehci, async, EST_FETCHENTRY);
c8dfc65
         ehci_advance_state(ehci, async);
c8dfc65
-        ehci_queues_rip_unused(ehci, async, 0);
c8dfc65
+        ehci_queues_rip_unused(ehci, async);
c8dfc65
         break;
c8dfc65
 
c8dfc65
     default: