All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>
To: xen-devel@lists.xenproject.org
Cc: "Marek Marczykowski-Górecki" <marmarek@invisiblethingslab.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"George Dunlap" <george.dunlap@citrix.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Julien Grall" <julien@xen.org>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Wei Liu" <wl@xen.org>
Subject: [PATCH v2 2/9] xue: reset XHCI ports when initializing dbc
Date: Wed,  6 Jul 2022 17:32:07 +0200	[thread overview]
Message-ID: <4874087c2304034e0d917d649f7cb9d46c149ed6.1657121519.git-series.marmarek@invisiblethingslab.com> (raw)
In-Reply-To: <cover.991b72d99d9e5bd4c2c76791ceb49f1056ce5d1c.1657121519.git-series.marmarek@invisiblethingslab.com>

Reset ports, to force host system to re-enumerate devices. Otheriwse it
will require the cable to be re-plugged, or will wait in the
"configuring" state indefinitely.

Trick and code copied from Linux:
drivers/usb/early/xhci-dbc.c:xdbc_start()->xdbc_reset_debug_port()

Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
---
Changes in v2:
- use uint32_t instead of u32
- code style
---
 xen/drivers/char/xue.c | 70 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 70 insertions(+)

diff --git a/xen/drivers/char/xue.c b/xen/drivers/char/xue.c
index 234b07b563bb..2cbbaea11fa0 100644
--- a/xen/drivers/char/xue.c
+++ b/xen/drivers/char/xue.c
@@ -63,6 +63,10 @@
     ((1UL << XUE_PSC_CSC) | (1UL << XUE_PSC_PRC) | (1UL << XUE_PSC_PLC) |      \
      (1UL << XUE_PSC_CEC))
 
+#define XUE_XHC_EXT_PORT_MAJOR(x)  (((x) >> 24) & 0xff)
+#define PORT_RESET                 (1 << 4)
+#define PORT_CONNECT               (1 << 0)
+
 #define xue_debug(...) printk("xue debug: " __VA_ARGS__)
 #define xue_alert(...) printk("xue alert: " __VA_ARGS__)
 #define xue_error(...) printk("xue error: " __VA_ARGS__)
@@ -590,6 +594,68 @@ static void xue_init_strings(struct xue *xue, uint32_t *info)
     info[8] = (4 << 24) | (30 << 16) | (8 << 8) | 6;
 }
 
+static void xue_do_reset_debug_port(struct xue *xue,
+                                    unsigned int id, unsigned int count)
+{
+    uint32_t *ops_reg;
+    uint32_t *portsc;
+    uint32_t val, cap_length;
+    int i;
+
+    cap_length = (*(uint32_t*)xue->xhc_mmio) & 0xff;
+    ops_reg = xue->xhc_mmio + cap_length;
+
+    id--;
+    for ( i = id; i < (id + count); i++ )
+    {
+        portsc = ops_reg + 0x100 + i * 0x4;
+        val = *portsc;
+        if ( !(val & PORT_CONNECT) )
+            *portsc = val | PORT_RESET;
+    }
+}
+
+static void xue_reset_debug_port(struct xue *xue)
+{
+    uint32_t val, port_offset, port_count;
+    uint32_t *xcap;
+    uint32_t next;
+    uint32_t id;
+    uint8_t *mmio = (uint8_t *)xue->xhc_mmio;
+    uint32_t *hccp1 = (uint32_t *)(mmio + 0x10);
+    const uint32_t PROTOCOL_ID = 0x2;
+
+    /**
+     * Paranoid check against a zero value. The spec mandates that
+     * at least one "supported protocol" capability must be implemented,
+     * so this should always be false.
+     */
+    if ( (*hccp1 & 0xFFFF0000) == 0 )
+        return;
+
+    xcap = (uint32_t *)(mmio + (((*hccp1 & 0xFFFF0000) >> 16) << 2));
+    next = (*xcap & 0xFF00) >> 8;
+    id = *xcap & 0xFF;
+
+    /* Look for "supported protocol" capability, major revision 3 */
+    for ( ; next; xcap += next, id = *xcap & 0xFF, next = (*xcap & 0xFF00) >> 8)
+    {
+        if ( id != PROTOCOL_ID && next )
+            continue;
+
+        if ( XUE_XHC_EXT_PORT_MAJOR(*xcap) != 0x3 )
+            continue;
+
+        /* extract ports offset and count from the capability structure */
+        val = *(xcap + 2);
+        port_offset = val & 0xff;
+        port_count = (val >> 8) & 0xff;
+
+        /* and reset them all */
+        xue_do_reset_debug_port(xue, port_offset, port_count);
+    }
+}
+
 static void xue_enable_dbc(struct xue *xue)
 {
     struct xue_dbc_reg *reg = xue->dbc_reg;
@@ -601,6 +667,10 @@ static void xue_enable_dbc(struct xue *xue)
     while ( (reg->ctrl & (1UL << XUE_CTRL_DCE)) == 0 )
         xue_sys_pause();
 
+    /* reset ports on initial open, to force re-enumerating by the host */
+    if ( !xue->open )
+        xue_reset_debug_port(xue);
+
     wmb();
     reg->portsc |= (1UL << XUE_PSC_PED);
     wmb();
-- 
git-series 0.9.1


  parent reply	other threads:[~2022-07-06 15:33 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-06 15:32 [PATCH v2 0/9] Add Xue - console over USB 3 Debug Capability Marek Marczykowski-Górecki
2022-07-06 15:32 ` [PATCH v2 1/9] drivers/char: Add support for Xue USB3 debugger Marek Marczykowski-Górecki
2022-07-08  2:11   ` Marek Marczykowski-Górecki
2022-07-12 15:59   ` Jan Beulich
2022-07-18 10:45     ` Marek Marczykowski-Górecki
2022-07-18 10:55       ` Jan Beulich
2022-07-12 16:06   ` Jan Beulich
2022-07-14  6:05   ` Jan Beulich
2022-07-16 22:40     ` Marek Marczykowski-Górecki
2022-07-14 11:58   ` Jan Beulich
2022-07-16 23:32     ` Marek Marczykowski-Górecki
2022-07-20 20:12     ` Marek Marczykowski-Górecki
2022-07-21 10:25       ` Jan Beulich
2022-07-06 15:32 ` Marek Marczykowski-Górecki [this message]
2022-07-13  7:19   ` [PATCH v2 2/9] xue: reset XHCI ports when initializing dbc Jan Beulich
2022-07-06 15:32 ` [PATCH v2 3/9] xue: add support for selecting specific xhci Marek Marczykowski-Górecki
2022-07-13  7:24   ` Jan Beulich
2022-07-06 15:32 ` [PATCH v2 4/9] console: support multiple serial console simultaneously Marek Marczykowski-Górecki
2022-07-13  9:39   ` Jan Beulich
2022-07-18 12:48     ` Marek Marczykowski-Górecki
2022-07-18 14:37       ` Jan Beulich
2022-07-06 15:32 ` [PATCH v2 5/9] IOMMU: add common API for device reserved memory Marek Marczykowski-Górecki
2022-07-14 10:17   ` Jan Beulich
2022-07-18 10:53     ` Marek Marczykowski-Górecki
2022-07-18 11:14       ` Jan Beulich
2022-07-18 11:03     ` Marek Marczykowski-Górecki
2022-07-18 11:15       ` Jan Beulich
2022-07-06 15:32 ` [PATCH v2 6/9] IOMMU/VT-d: wire common device reserved memory API Marek Marczykowski-Górecki
2022-07-06 15:32 ` [PATCH v2 7/9] IOMMU/AMD: " Marek Marczykowski-Górecki
2022-07-14 10:22   ` Jan Beulich
2022-07-18 11:35     ` Marek Marczykowski-Górecki
2022-07-18 11:44       ` Jan Beulich
2022-07-06 15:32 ` [PATCH v2 8/9] xue: mark DMA buffers as reserved for the device Marek Marczykowski-Górecki
2022-07-14 11:51   ` Jan Beulich
2022-07-18 13:15     ` Marek Marczykowski-Górecki
2022-07-20 20:17     ` Marek Marczykowski-Górecki
2022-07-21 10:30       ` Jan Beulich
2022-07-06 15:32 ` [PATCH v2 9/9] xue: allow driving the rest of XHCI by a domain while Xen uses DbC Marek Marczykowski-Górecki
2022-07-14 12:06   ` Jan Beulich
2022-07-18 12:54     ` Marek Marczykowski-Górecki
2022-07-18 15:07       ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4874087c2304034e0d917d649f7cb9d46c149ed6.1657121519.git-series.marmarek@invisiblethingslab.com \
    --to=marmarek@invisiblethingslab.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=george.dunlap@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=sstabellini@kernel.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.