From 6a1159c2eb3e933ee562941d26bb98d14af4578b Mon Sep 17 00:00:00 2001
Message-Id: <6a1159c2eb3e933ee562941d26bb98d14af4578b.1343750985.git.minovotn@redhat.com>
In-Reply-To: <3ef4055cdb5048ae1b1c3aa11bf1cae31c337b90.1343750985.git.minovotn@redhat.com>
References: <3ef4055cdb5048ae1b1c3aa11bf1cae31c337b90.1343750985.git.minovotn@redhat.com>
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Mon, 9 Jul 2012 12:56:41 +0200
Subject: [PATCH 5/5] uhci: fix bandwidth management

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1341838601-27682-6-git-send-email-kraxel@redhat.com>
Patchwork-id: 40240
O-Subject: [RHEL-6.4 qemu-kvm PATCH 5/5] uhci: fix bandwidth management
Bugzilla: 808653 831549
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>

uhci_process_frame() can be invoked multiple times per frame, so
accounting usb bandwith in a local variable doesn't fly, use a variable
in UHCIState instead.  Also check the limit more frequently.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
(cherry picked from commit 4aed20e2d70f4353164399a173f20c3ab435b4eb)

Conflicts:

	hw/usb-uhci.c
	trace-events
---
 hw/usb-uhci.c |   22 ++++++++++++----------
 1 files changed, 12 insertions(+), 10 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/usb-uhci.c |   22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/hw/usb-uhci.c b/hw/usb-uhci.c
index 68b375a..001a044 100644
--- a/hw/usb-uhci.c
+++ b/hw/usb-uhci.c
@@ -142,6 +142,7 @@ struct UHCIState {
     uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
     int64_t expire_time;
     QEMUTimer *frame_timer;
+    uint32_t frame_bytes;
     UHCIPort ports[NB_PORTS];
 
     /* Interrupts that should be raised at the end of the current frame.  */
@@ -952,7 +953,7 @@ static int qhdb_insert(QhDb *db, uint32_t addr)
 static void uhci_process_frame(UHCIState *s)
 {
     uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
-    uint32_t curr_qh, td_count = 0, bytes_count = 0;
+    uint32_t curr_qh, td_count = 0;
     int cnt, ret;
     UHCI_TD td;
     UHCI_QH qh;
@@ -971,6 +972,12 @@ static void uhci_process_frame(UHCIState *s)
     qhdb_reset(&qhdb);
 
     for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
+        if (s->frame_bytes >= 1280) {
+            /* We've reached the usb 1.1 bandwidth, which is
+               1280 bytes/frame, stop processing */
+            DPRINTF("uhci: bandwidth limit reached, stop\n");
+            break;
+        }
         if (is_qh(link)) {
             /* QH */
 
@@ -979,19 +986,13 @@ static void uhci_process_frame(UHCIState *s)
                  * We're going in circles. Which is not a bug because
                  * HCD is allowed to do that as part of the BW management.
                  *
-                 * Stop processing here if
-                 *  (a) no transaction has been done since we've been
-                 *      here last time, or
-                 *  (b) we've reached the usb 1.1 bandwidth, which is
-                 *      1280 bytes/frame.
+                 * Stop processing here if no transaction has been done
+                 * since we've been here last time.
                  */
                 DPRINTF("uhci: detected loop. qh 0x%x\n", link);
                 if (td_count == 0) {
                     DPRINTF("uhci: no transaction last round, stop\n");
                     break;
-                } else if (bytes_count >= 1280) {
-                    DPRINTF("uhci: bandwidth limit reached, stop\n");
-                    break;
                 } else {
                     td_count = 0;
                     qhdb_reset(&qhdb);
@@ -1058,7 +1059,7 @@ static void uhci_process_frame(UHCIState *s)
 
         link = td.link;
         td_count++;
-        bytes_count += (td.ctrl & 0x7ff) + 1;
+        s->frame_bytes += (td.ctrl & 0x7ff) + 1;
 
         if (curr_qh) {
 	    /* update QH element link */
@@ -1090,6 +1091,7 @@ static void uhci_frame_timer(void *opaque)
 
     /* prepare the timer for the next frame */
     s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
+    s->frame_bytes = 0;
 
     if (!(s->cmd & UHCI_CMD_RS)) {
         /* Full stop */
-- 
1.7.10.4

