mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Igor Skalkin <igor.skalkin@oss.qualcomm.com>
To: "Michael S . Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowangio@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, Eugenio Perez <eperezma@redhat.com>,
	Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
	Vasilii Ianikeev <vasilii.ianikeev@oss.qualcomm.com>,
	Aiswarya Cyriac <aiswarya.cyriac@oss.qualcomm.com>,
	Anton Yakovlev <anton.yakovlev@oss.qualcomm.com>,
	Trilok Soni <trilok.soni@oss.qualcomm.com>,
	Matias Ezequiel Vara Larsen <mvaralar@redhat.com>,
	Igor Skalkin <igor.skalkin@oss.qualcomm.com>
Subject: [RFC PATCH v2 5/7] virtio-usb: add per-port role query support
Date: Tue,  1 Dec 2026 18:35:34 +0100	[thread overview]
Message-ID: <967ac79f8a1bbd88f1e0edeb85d90f5f138e2be8.1796144390.git.igor.skalkin@oss.qualcomm.com> (raw)
In-Reply-To: <cover.1796144390.git.igor.skalkin@oss.qualcomm.com>

With both host_role and device_role negotiated, a port's own role is
ambiguous (host|device) - determining it requires more than reading
the two feature bits. Introduce a minimal role command/event queue
pair (role_vq_base, role_vqueues[], virtio_usb_rsw_init()/
virtio_usb_rsw_deinit()) and virtio_usb_rsw_get_role(), a synchronous
VIRTIO_USB_CMD_ROLE_GET request/response exchange, and query every
port's role individually during probe instead of the alternative of
overloading the config space ports field with role bits.

This replaces the previous commit's DEVICE-only placeholder for the
both-roles-negotiated case with the real per-port answer.

The code lives in role_switch.c, since the next commit builds runtime
role switching on top of the same queue pair.

Signed-off-by: Igor Skalkin <igor.skalkin@oss.qualcomm.com>
---
 drivers/usb/virtio_usb/Makefile      |   3 +-
 drivers/usb/virtio_usb/controller.c  |  88 +++++++++++---
 drivers/usb/virtio_usb/controller.h  |  12 ++
 drivers/usb/virtio_usb/role_switch.c | 168 +++++++++++++++++++++++++++
 drivers/usb/virtio_usb/role_switch.h |  27 +++++
 5 files changed, 280 insertions(+), 18 deletions(-)
 create mode 100644 drivers/usb/virtio_usb/role_switch.c
 create mode 100644 drivers/usb/virtio_usb/role_switch.h

diff --git a/drivers/usb/virtio_usb/Makefile b/drivers/usb/virtio_usb/Makefile
index ac382d81d8af..081d67323d9d 100644
--- a/drivers/usb/virtio_usb/Makefile
+++ b/drivers/usb/virtio_usb/Makefile
@@ -5,4 +5,5 @@ obj-$(CONFIG_USB_VIRTIO)	+= virtio-usb.o
 virtio-usb-y := controller.o \
 		vq_common.o \
 		host.o \
-		device.o
+		device.o \
+		role_switch.o
diff --git a/drivers/usb/virtio_usb/controller.c b/drivers/usb/virtio_usb/controller.c
index b463513b2da4..9f875a127f67 100644
--- a/drivers/usb/virtio_usb/controller.c
+++ b/drivers/usb/virtio_usb/controller.c
@@ -11,6 +11,7 @@
 #include "controller.h"
 #include "host.h"
 #include "device.h"
+#include "role_switch.h"
 #include "vq_common.h"
 
 /**
@@ -120,12 +121,12 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 
 	/* Only allocate/negotiate the virtqueue triplets this instance
 	 * actually needs: HOST_* only exists when host_role is negotiated,
-	 * DEV_* only when device_role is negotiated. A pure single-role
-	 * instance therefore has exactly VIRTIO_USB_VQ_HOST_MAX (3) or
-	 * VIRTIO_USB_VQ_DEV_MAX (3) virtqueues, not a fixed layout - queues
-	 * that don't exist on the wire must not be created, since a peer
-	 * with no host-role VP has no host command/event/data queues to
-	 * negotiate at all.
+	 * DEV_* only when device_role is negotiated, ROLE_* only when both
+	 * roles are negotiated. A pure single-role instance therefore has
+	 * exactly VIRTIO_USB_VQ_HOST_MAX (3) or VIRTIO_USB_VQ_DEV_MAX (3)
+	 * virtqueues, not a fixed layout - queues that don't exist on the
+	 * wire must not be created, since a peer with no host-role VP has
+	 * no host command/event/data queues to negotiate at all.
 	 */
 	vusb->host_vq_base = -1;
 	vusb->dev_vq_base = -1;
@@ -138,17 +139,15 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 		vusb->dev_vq_base = nvqs;
 		nvqs += VIRTIO_USB_VQ_DEV_MAX;
 	}
-
-	/* Resolve every port's role. With only one role negotiated, every
-	 * port unambiguously has that role. With both negotiated, a port's
-	 * own role is ambiguous until a later commit adds a per-port
-	 * role query - default to DEVICE for now as a placeholder.
-	 */
-	for (i = 0; i < vusb->nports; i++) {
-		if (vusb->host_role && !vusb->device_role)
-			vusb->vports[i].role = VIRTIO_USB_ROLE_HOST;
-		else if (vusb->device_role)
-			vusb->vports[i].role = VIRTIO_USB_ROLE_DEVICE;
+	if (vusb->host_role && vusb->device_role) {
+		/* The role command/event queue pair is needed whenever both
+		 * roles are negotiated - it is how the driver asks each
+		 * port for its actual role via virtio_usb_rsw_get_role() below, since
+		 * a port's own role is otherwise ambiguous (host|device)
+		 * until then.
+		 */
+		vusb->role_vq_base = nvqs;
+		nvqs += VIRTIO_USB_VQ_ROLE_MAX;
 	}
 
 	vusb->vqueues = devm_kcalloc(&vdev->dev, nvqs, sizeof(*vusb->vqueues),
@@ -182,6 +181,18 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 				dev_vqueues[i].stop;
 		}
 
+	if (vusb->host_role && vusb->device_role)
+		for (i = 0; i < VIRTIO_USB_VQ_ROLE_MAX; i++) {
+			vusb->vqueues[vusb->role_vq_base + i].name =
+				role_vqueues[i].name;
+			vusb->vqueues[vusb->role_vq_base + i].callback =
+				role_vqueues[i].callback;
+			vusb->vqueues[vusb->role_vq_base + i].process =
+				role_vqueues[i].process;
+			vusb->vqueues[vusb->role_vq_base + i].stop =
+				role_vqueues[i].stop;
+		}
+
 	rc = virtio_usb_find_vqs(vusb);
 	if (rc) {
 		dev_err(&vdev->dev, "%s virtio_usb_find_vqs() error(%d)\n",
@@ -189,6 +200,47 @@ static int virtio_usb_probe(struct virtio_device *vdev)
 		goto on_error;
 	}
 
+	if (vusb->host_role && vusb->device_role) {
+		rc = virtio_usb_rsw_init(vusb);
+		if (rc) {
+			dev_err(&vdev->dev,
+				"%s virtio_usb_rsw_init() error(%d)\n",
+				__func__, rc);
+			goto on_error;
+		}
+	}
+
+	/* Resolve every port's role. With only one role negotiated, every
+	 * port unambiguously has that role. With both negotiated, query
+	 * each port's actual role individually via virtio_usb_rsw_get_role(), since
+	 * it is otherwise ambiguous (host|device).
+	 */
+	for (i = 0; i < vusb->nports; i++) {
+		if (vusb->host_role && !vusb->device_role) {
+			vusb->vports[i].role = VIRTIO_USB_ROLE_HOST;
+		} else if (vusb->device_role && !vusb->host_role) {
+			vusb->vports[i].role = VIRTIO_USB_ROLE_DEVICE;
+		} else {
+			u32 status, role;
+
+			status = virtio_usb_rsw_get_role(vusb, i, &role);
+			if (status != VIRTIO_USB_S_OK) {
+				dev_err(&vdev->dev, "%s status(%d)\n", __func__,
+					status);
+				rc = -EIO;
+				goto on_error;
+			}
+			if (role != VIRTIO_USB_ROLE_HOST &&
+			    role != VIRTIO_USB_ROLE_DEVICE) {
+				dev_err(&vdev->dev, "%s port%d wrong role %d\n",
+					__func__, i, role);
+				rc = -EIO;
+				goto on_error;
+			}
+			vusb->vports[i].role = role;
+		}
+	}
+
 	if (vusb->host_role) {
 		INIT_WORK(&vusb->vq_host_data_rx_work, virtio_usb_hc_rx_work);
 		INIT_WORK(&vusb->vq_host_evt_work, virtio_usb_hc_evt_work);
@@ -276,6 +328,8 @@ static void virtio_usb_remove(struct virtio_device *vdev)
 	virtio_reset_device(vdev);
 
 	vdev->config->del_vqs(vdev);
+
+	virtio_usb_rsw_deinit(vusb);
 }
 
 static const unsigned int virtio_usb_features[] = {
diff --git a/drivers/usb/virtio_usb/controller.h b/drivers/usb/virtio_usb/controller.h
index 446a9274871c..6f39e4c5a4aa 100644
--- a/drivers/usb/virtio_usb/controller.h
+++ b/drivers/usb/virtio_usb/controller.h
@@ -19,6 +19,8 @@
 struct virtio_usb_hc_vp;
 /* Forward declaration - full definition in device.h */
 struct virtio_usb_dc;
+/* Forward declaration - full definition in role_switch.h */
+struct virtio_usb_rsw;
 
 #define VIRTIO_USB_VQ_COMMAND_IDX 0
 #define VIRTIO_USB_VQ_EVENT_IDX 1
@@ -26,6 +28,7 @@ struct virtio_usb_dc;
 
 #define VIRTIO_USB_VQ_HOST_MAX 3
 #define VIRTIO_USB_VQ_DEV_MAX 3
+#define VIRTIO_USB_VQ_ROLE_MAX 2
 
 /**
  * struct virtio_usb_port - Per-virtual-port state.
@@ -55,6 +58,12 @@ struct virtio_usb_port {
  * @dev_vq_base: index into vqueues[] where the DEV_COMMAND/EVENT/DATA
  *               triplet starts, or -1 if this instance has no
  *               device-role VP.
+ * @role_vq_base: index into vqueues[] where the ROLE_COMMAND/EVENT pair
+ *               starts, or -1 if this instance has neither a host-role
+ *               nor a device-role VP. Used to query each port's role via
+ *               virtio_usb_rsw_get_role(), since with both host_role and
+ *               device_role negotiated a port's own role is otherwise
+ *               ambiguous.
  * @vq_host_data_rx_work: Kernel work draining the host data queue, shared
  *                        across every host-role VP.
  * @vq_host_evt_work: Kernel work draining the host event queue, shared
@@ -63,6 +72,7 @@ struct virtio_usb_port {
  *                       across every device-role port.
  * @vq_dev_event_work: Kernel work draining the device event queue, shared
  *                      across every device-role port.
+ * @rsw: Device-wide role-switch state, see role_switch.h.
  * @data_tag_seq: Source of data request tags, see virtio_usb_data_tag_next().
  */
 struct virtio_usb {
@@ -75,10 +85,12 @@ struct virtio_usb {
 	bool device_role;
 	int host_vq_base;
 	int dev_vq_base;
+	int role_vq_base;
 	struct work_struct vq_host_data_rx_work;
 	struct work_struct vq_host_evt_work;
 	struct work_struct vq_dev_data_rx_work;
 	struct work_struct vq_dev_event_work;
+	struct virtio_usb_rsw *rsw;
 	atomic64_t data_tag_seq;
 };
 
diff --git a/drivers/usb/virtio_usb/role_switch.c b/drivers/usb/virtio_usb/role_switch.c
new file mode 100644
index 000000000000..50b5d645abf4
--- /dev/null
+++ b/drivers/usb/virtio_usb/role_switch.c
@@ -0,0 +1,168 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * virtio_usb: VirtIO USB device
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#include <linux/mutex.h>
+#include "controller.h"
+#include "role_switch.h"
+#include "vq_common.h"
+
+int virtio_usb_rsw_init(struct virtio_usb *vusb)
+{
+	struct virtio_usb_rsw *rsw =
+		devm_kzalloc(&vusb->vdev->dev, sizeof(*rsw), GFP_KERNEL);
+	unsigned int i;
+
+	if (!rsw)
+		return -ENOMEM;
+
+	rsw->vusb = vusb;
+	vusb->rsw = rsw;
+	for (i = 0; i < VIRTIO_USB_VQ_ROLE_MAX; i++)
+		rsw->oqs[i] = &vusb->vqueues[vusb->role_vq_base + i];
+
+	mutex_init(&rsw->lock);
+	init_completion(&rsw->completion);
+
+	return 0;
+}
+
+void virtio_usb_rsw_deinit(struct virtio_usb *vusb)
+{
+	struct virtio_usb_rsw *rsw = vusb->rsw;
+
+	if (!rsw)
+		return;
+
+	/* Wake potential role command waiters before releasing role objects. */
+	complete_all(&rsw->completion);
+
+	vusb->rsw = NULL;
+}
+
+/* Send a role command and get a response.
+ *
+ * The function is implemented as synchronous. Design pattern is
+ * virtio_can.c/virtio_can_send_ctrl_msg()
+ */
+u32 virtio_usb_rsw_get_role(struct virtio_usb *vusb, int port_id, u32 *role)
+{
+	struct scatterlist sg_out, sg_in, *sgs[2] = { &sg_out, &sg_in };
+	struct virtqueue *vq =
+		vusb->rsw->oqs[VIRTIO_USB_VQ_COMMAND_IDX]->vqueue;
+	unsigned int len;
+	u32 status = VIRTIO_USB_S_ERR_INTERNAL;
+
+	struct role_get_msg {
+		struct virtio_usb_role_cmd_hdr cmd_hdr;
+		struct virtio_usb_role_info cmd_role;
+	} *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
+
+	if (!msg)
+		return status;
+
+	msg->cmd_hdr.code = cpu_to_le32(VIRTIO_USB_CMD_ROLE_GET);
+	msg->cmd_hdr.port = cpu_to_le32(port_id);
+	sg_init_one(&sg_out, &msg->cmd_hdr, sizeof(msg->cmd_hdr));
+	sg_init_one(&sg_in, &msg->cmd_role, sizeof(msg->cmd_role));
+
+	mutex_lock(&vusb->rsw->lock);
+
+	if (virtqueue_add_sgs(vq, sgs, 1u, 1u, msg, GFP_ATOMIC)) {
+		pr_err("%s virtqueue_add_sgs error\n", __func__);
+		goto exit;
+	}
+
+	if (!virtqueue_kick(vq)) {
+		pr_err("%s virtqueue_kick error\n", __func__);
+		goto exit;
+	}
+
+	while (!virtqueue_get_buf(vq, &len) && !virtqueue_is_broken(vq))
+		wait_for_completion(&vusb->rsw->completion);
+
+	status = le32_to_cpu(msg->cmd_role.status.code);
+	*role = le32_to_cpu(msg->cmd_role.role);
+
+	if (*role != VIRTIO_USB_ROLE_HOST && *role != VIRTIO_USB_ROLE_DEVICE) {
+		pr_err("%s - wrong role (%d)\n", __func__, *role);
+	} else {
+		pr_info("%s role %s\n", __func__,
+			*role == VIRTIO_USB_ROLE_HOST ?
+				"VIRTIO_USB_ROLE_HOST" :
+				"VIRTIO_USB_ROLE_DEVICE");
+	}
+
+exit:
+	kfree(msg);
+	mutex_unlock(&vusb->rsw->lock);
+	return status;
+}
+
+static void virtio_usb_rsw_cmd_notify_cb(struct virtqueue *vqueue)
+{
+	struct virtio_usb *vusb = vqueue->vdev->priv;
+
+	if (!vusb->rsw)
+		return;
+
+	complete(&vusb->rsw->completion);
+}
+
+static void virtio_usb_rsw_cmdq_stop_cb(struct virtio_usb *vusb,
+					struct virtio_usb_queue *vq)
+{
+	unsigned long flags;
+
+	if (!vusb->rsw || !vq->vqueue)
+		return;
+
+	/*
+	 * Wake sleepers in role synchronous command paths so they can
+	 * observe started=false and exit.
+	 */
+	complete_all(&vusb->rsw->completion);
+
+	spin_lock_irqsave(&vq->lock, flags);
+	virtqueue_disable_cb(vq->vqueue);
+	spin_unlock_irqrestore(&vq->lock, flags);
+}
+
+static void virtio_usb_rsw_evtq_stop_cb(struct virtio_usb *vusb,
+					struct virtio_usb_queue *vq)
+{
+	unsigned long flags;
+	u32 length;
+	void *buf;
+
+	if (!vq->vqueue)
+		return;
+
+	/* The role event queue is not populated yet at this stage (no
+	 * VIRTIO_USB_F_SWITCH_ROLE negotiation, no ROLE_CHANGE events),
+	 * so this only has to make sure del_vqs() finds the ring empty.
+	 */
+	spin_lock_irqsave(&vq->lock, flags);
+	virtqueue_disable_cb(vq->vqueue);
+	while ((buf = virtqueue_get_buf(vq->vqueue, &length)))
+		;
+	spin_unlock_irqrestore(&vq->lock, flags);
+}
+
+const struct virtio_usb_vq_desc role_vqueues[VIRTIO_USB_VQ_ROLE_MAX] = {
+	[VIRTIO_USB_VQ_COMMAND_IDX] = {
+		.callback = virtio_usb_rsw_cmd_notify_cb,
+		.name = "virtusb-role-cmd",
+		.process = NULL,
+		.stop = virtio_usb_rsw_cmdq_stop_cb,
+	},
+	[VIRTIO_USB_VQ_EVENT_IDX] = {
+		.callback = NULL,
+		.name = "virtusb-role-evt",
+		.process = NULL,
+		.stop = virtio_usb_rsw_evtq_stop_cb,
+	},
+};
diff --git a/drivers/usb/virtio_usb/role_switch.h b/drivers/usb/virtio_usb/role_switch.h
new file mode 100644
index 000000000000..e3c8031686c3
--- /dev/null
+++ b/drivers/usb/virtio_usb/role_switch.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * virtio_usb: VirtIO USB device
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#ifndef VIRTIO_USB_ROLE_SWITCH_H
+#define VIRTIO_USB_ROLE_SWITCH_H
+
+#include "controller.h"
+
+int virtio_usb_rsw_init(struct virtio_usb *vusb);
+void virtio_usb_rsw_deinit(struct virtio_usb *vusb);
+u32 virtio_usb_rsw_get_role(struct virtio_usb *vusb, int port_id,
+			    u32 *role);
+
+struct virtio_usb_rsw {
+	struct virtio_usb *vusb;
+	struct mutex lock;
+	struct completion completion;
+	struct virtio_usb_queue *oqs[VIRTIO_USB_VQ_ROLE_MAX];
+};
+
+extern const struct virtio_usb_vq_desc role_vqueues[VIRTIO_USB_VQ_ROLE_MAX];
+
+#endif /* VIRTIO_USB_ROLE_SWITCH_H */
-- 
2.49.0


  parent reply	other threads:[~2026-12-01 17:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260924160907.145405-1-igor.skalkin@oss.qualcomm.com>
2026-12-01 17:35 ` [RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 1/7] uapi: virtio_ids: reserve ID for virtio USB device Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 2/7] virtio-usb: add protocol header and skeleton dual-role driver Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 3/7] virtio-usb: add host role (USB Host Controller) support Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 4/7] virtio-usb: add device role (USB Device " Igor Skalkin
2026-12-01 17:35   ` Igor Skalkin [this message]
2026-12-01 17:35   ` [RFC PATCH v2 6/7] virtio-usb: add host/device role switch support Igor Skalkin
2026-12-01 17:35   ` [RFC PATCH v2 7/7] virtio-usb: support a guest UDC name prefix from the bind event Igor Skalkin
2026-12-01 17:49   ` [RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver Michael S. Tsirkin
2026-12-01 20:09   ` Greg Kroah-Hartman

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=967ac79f8a1bbd88f1e0edeb85d90f5f138e2be8.1796144390.git.igor.skalkin@oss.qualcomm.com \
    --to=igor.skalkin@oss.qualcomm.com \
    --cc=aiswarya.cyriac@oss.qualcomm.com \
    --cc=anton.yakovlev@oss.qualcomm.com \
    --cc=eperezma@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jasowangio@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=mvaralar@redhat.com \
    --cc=trilok.soni@oss.qualcomm.com \
    --cc=vasilii.ianikeev@oss.qualcomm.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®