From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146571; b=3ebbztahN9qn9VYhp9RppFh3b5bW3UDDtljvg36muDPCMjZ7Vm2ABLIZIIXIpykczAuY YVMEKldhD+P+PKuP5yeg8UUxCHW/+lXdnk1H29UY15Bjdtoq0TmW6ypCL7NNpwa6YsQyf 6gjBstFnr3YWinLu9o5zHHhMrV+2QSUNQsKVYaTMTGvVtsFCEravbEi4XXm0S8es+cUW5 sDD9I267GYz4UZEXldoMCHcy6j2d/oWnoE+jWTuu4gf+QgUbQ8VN2YW7t0SCHNVFUyZ0K /iKL0pfBERzs3za62pH3QYORc3V4UGS0MEgrkVwTsqj9e45qzuQx1cNgPSwaRZNwIAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146571; h=DMARC-Filter:Received:DKIM-Signature:Received:Received: DKIM-Signature:X-Google-DKIM-Signature:X-Forwarded-Encrypted: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Authority-Analysis:X-Proofpoint-GUID: X-Proofpoint-ORIG-GUID:X-Proofpoint-Spam-Info: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=qyqVKDjlVePJPHrAm0if5pMs/3BdfpG6T39QTK7RV18=; b=QIWP6U7C/sGXjEmwyQIpkRVUd+A+Fi2z0YlpHJ6D1ZOM4sBtt2BYwn1d5tGwqaTFA7ZX oe3wYSnyET9DVrt6FG5+aZPn+JCoKOhprczI4BSimtTh6HxSqCS+1PbcSviScBgrrvXQe UD3Ad9dA1YJGPBRkEShpkNB1AQq8U8KA6wXoMdAU85P4e33KsJHLe61kLN8ZQ+KzWAa7r qyoBWWYVD1qsWr1JBUmntguuhA5HvlKElccmX0B/SHlZpyUNl0P5MQbfVw/FY/J5eeM53 YUJj7iwcHOjfRbsepmbNSrKUuZUzh5wskbXsz1zMH48kvfNz6870XzSeuohTgWNTIsg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=czN3iJGg; dkim=pass header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=B0IMOWUq; arc=none smtp.remote-ip=205.220.180.131 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8CD6606A31A for ; Tue, 1 Dec 2026 17:36:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C8CD6606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=czN3iJGg; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=B0IMOWUq Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B1F6sOT1575310 for ; Tue, 1 Dec 2026 17:36:09 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=qyqVKDjlVeP JPHrAm0if5pMs/3BdfpG6T39QTK7RV18=; b=czN3iJGghQCEvomaZ3SK71eg3lT +sn9NliTdimZj8TzFZN52cnfD55KB9qRduPaNLbzVtLRtEoADZmO26ojNyev0ZhQ CwzgbV9eVFBsaIrhHT9q/P0uY+rKIXUmv0eQzcZ1Lv0AQZaOQNyHR+HdoGODShCc h5hi7sqXZwI0gLgOFYq9zlLaqic0M7u1ivinnCMFEMvvsLoQ810dVk1HIA/X/V6f lx/U0qSsxpGz3gpNFQhiffL7bpo8+vy1CvInt59AnvJUd3VO1BB2KbXrzOKo90TN UilS1EcED5ZBY3B8nIGTC8ixR6DwEKor/0cYXuHoxQ1Aonwl+xdO9Sd6tZA== Received: from mail-vs1-f71.google.com (mail-vs1-f71.google.com [209.85.217.71]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4j9sa22rq8-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 01 Dec 2026 17:36:09 +0000 (GMT) Received: by mail-vs1-f71.google.com with SMTP id ada2fe7eead31-7d899e4b4fbso2126104137.2 for ; Tue, 01 Dec 2026 09:36:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1796146569; x=1796751369; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qyqVKDjlVePJPHrAm0if5pMs/3BdfpG6T39QTK7RV18=; b=B0IMOWUq6dmqHzP+CRjIoBuwmE8/FvxvxpFcaCz9id+UVZjhekRo3c1khOJHsCoCcb E+JJR4VWF03BBItAhsiCacfqDQXF5P3M9tAinCS/XgnMWATWgIzbPlD5yFL8mK6xn3HC b8eIj6Xq9scaCyuic4GKgV0Nw9vJFmigVz0I0NWFxPtB/+iwv6IskwpcSQbcMdT7YJmu ooQDMIrb3x7XTlaKHH2npFvFLFPt6N0x1RdpZQ0e/a/pgo1vxYOqXTDuHrvSj24A8/AJ LM2AX2FQRE4GX/JwbDxV1HBytTFXY9Q4733SEM8XdlZvvIB6Q2coqeyIFAWErLEmSiJK ZTkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796146569; x=1796751369; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=qyqVKDjlVePJPHrAm0if5pMs/3BdfpG6T39QTK7RV18=; b=wlapNdXTBrQqdDRdE10xIWKPm1WOY4A1deyHq7QxInH4ernv2Vxb3BPpMX2FFbZ97h IJCyLi3WJum0M4CIIUl9YCfWt5eaQ29yVHmjcwWvb6f0a3Km+/AXv3C9Bj9o8xiqahXT ozq97GiW1jMHLSFOCb71loWvWNoFsZBIK1zOJiLNSouph4HhHRUWovUVsPMGfH6gE21Q X5cczRJKC7Fi7zdTBkIXyyTiF7o/DFpi/0N/LAogoYcPprw1TuSLa/LHO122Xqnsvde1 WIEHWGu31gy5yLurk/HYUwDovAL1V701BQqp52sCHS4skbyyFs6tmrXU4+iSlh+F67P6 w4Kw== X-Forwarded-Encrypted: i=1; AL+hErHjtlhPAg6RNOpj0b8/999uIbufr+PE7DBpKgapx4I2upPtEGN+6nyTSUeJS4qLyS9bdy2POdkChYE1Yvg=@vger.kernel.org X-Gm-Message-State: APuU4Z/uRepULo+ByWKRbR2hHs8ixiy8dQBNJ4Iao2g92t02Mp/yE/PA JMuir8ClpOP9FPXgynuxeT9P79cKIdC4jPOJXHAitzBCjk+PYogvZ/93Gs3JB8eALmkcr5dOasc npd1O644nXiU7yf1A9B+Z5nRjIjJIussdBRCfMnRSjZa7223MkNHov2Bmmh1Jjdjuc+w= X-Gm-Gg: AZFdTZe8hiZ7bXU1sqsJz708iru//dDKfIbkjSlOxKilYQnRLiwOcL5j/sRjwmlnWcH 9lWpYdXBBYiR8xkxmz3GacTwgHyZpmc4zYKiRGu2BwErfrwL2UlZaB85Q9RjtRbrLnNaDrxKpc6 gEcJVIg1kb8XebVaetODrib84Abs0QT8k2RCAE1a6LDWOxDwwlUJQYC5bKUDz8LglHBSsZfjeig gnM+NImLo0f1ZecZDC0MLY41MaDj1Y35JUVBNvWoKSPCiPJxOuNT9PTRuyH9btuFOfKijpEnNi4 pkmELfZ7RnqrUfWkvnpccw7GXkqA/r6khbeHv4IUuFrXnuABmLnRLsEUym0lqhJQkNOLIAvic/e lb5uxxoyYwo+y945fdTvSC2ee32dWJtyi0ejQe+zqR4hyc4cPJd6vAfPlr/e4AxnrZ0Vtq9ljJQ JOnjoS X-Received: by 2002:a05:6102:5241:b0:7cc:ccb:ce5a with SMTP id ada2fe7eead31-7d7397d153cmr23968256137.28.1796146568775; Tue, 01 Dec 2026 09:36:08 -0800 (PST) X-Received: by 2002:a05:6102:5241:b0:7cc:ccb:ce5a with SMTP id ada2fe7eead31-7d7397d153cmr23968237137.28.1796146568156; Tue, 01 Dec 2026 09:36:08 -0800 (PST) Received: from QCOM-WFsSr66Fqw.qualcomm.com ([212.136.9.4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbb39dd4e7sm339386455e9.0.2026.12.01.09.36.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 09:36:07 -0800 (PST) From: Igor Skalkin To: "Michael S . Tsirkin" , Jason Wang , Greg Kroah-Hartman Cc: virtualization@lists.linux.dev, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Eugenio Perez , Xuan Zhuo , Vasilii Ianikeev , Aiswarya Cyriac , Anton Yakovlev , Trilok Soni , Matias Ezequiel Vara Larsen , Igor Skalkin Subject: [RFC PATCH v2 5/7] virtio-usb: add per-port role query support Date: Tue, 1 Dec 2026 18:35:34 +0100 Message-ID: <967ac79f8a1bbd88f1e0edeb85d90f5f138e2be8.1796144390.git.igor.skalkin@oss.qualcomm.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: <20260924160907.145405-1-igor.skalkin@oss.qualcomm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Authority-Analysis: v=2.4 cv=A8/5rKWG c=1 sm=1 tr=0 ts=6b0f0589 cx=c_pps a=P2rfLEam3zuxRRdjJWA2cw==:117 a=dNlqnMcrdpbb+gQrTujlOQ==:17 a=pUM-KoujPXoA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=yx91gb_oNiZeI1HMLzn7:22 a=EUspDBNiAAAA:8 a=D_8e-EGeD0ZYVM8zclsA:9 a=ODZdjJIeia2B_SHc_B0f:22 X-Proofpoint-GUID: rgmt5fuWvekta0Dl3E3fvwHl4feK1Vzb X-Proofpoint-ORIG-GUID: rgmt5fuWvekta0Dl3E3fvwHl4feK1Vzb X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAxMDA3MCBTYWx0ZWRfX7+CjMH8g6rAf SS1DcSVpUyRXwauoEh5Kv2WXmLzm2rAhfLBVYrAO8JKRgmtuU77Bj1ir0TB6BrOpcLbKkGOJhRx mesIUWiyzbMEI+Qb2wUFNh10o3x0KJA= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAxMDA3MCBTYWx0ZWRfX99nkNq8/NUhi lGL0Oh6mFG08TrVwx9TBlVExJHrWuMjME2sI0O4IPpHcsWEHUMhsT275BfcdzGoDhcykva2M9uU 0YZhVSbd6k+OdIlcMhg9ZXTPrkUzYDxF8QJ/ZZFjAd0A+ZmIEbF1h/ti4WBEXZGOv/HChr/iBbi oBaiBPUgVg1lvp/IRGaiDnIpyXbz+oakNVUS4alKQ5kfbsLydTQFKOUo+HGh0lztAFa0+1Y9BqR p2Qc4vqXxXIOYmsU4AWaowGYIZXmHzUFmMaYZvJFPz72sO2t94mU8ejAngS8N/svWLf7XomQCdY kfkuFLVNz2c9Eg5F8rJdZ/LWarwJiEIZ8fZxbyPvvQLJ3E224dSt1Ktx/R49JSLBMfDnCUrUPS2 wmRm+ftRwILmTW199ZEvlIKJlBJ7o5R3N5s1QZOD7Z2R3VckncQrKDVk8Y1CnUb9AlX7B0Ex3BY WKBLNhcoxeWO2kOXDtg== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-01_03,2026-12-01_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 phishscore=0 priorityscore=1501 bulkscore=0 spamscore=0 malwarescore=0 clxscore=1011 adultscore=0 lowpriorityscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612010070 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 --- 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 +#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