From patchwork Tue Oct 7 14:39:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cornelia Huck X-Patchwork-Id: 5048171 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 4C0089F2F1 for ; Tue, 7 Oct 2014 17:54:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 364D62017D for ; Tue, 7 Oct 2014 17:54:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 13E8D20142 for ; Tue, 7 Oct 2014 17:54:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932099AbaJGRyK (ORCPT ); Tue, 7 Oct 2014 13:54:10 -0400 Received: from e06smtp13.uk.ibm.com ([195.75.94.109]:49507 "EHLO e06smtp13.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932095AbaJGRyI (ORCPT ); Tue, 7 Oct 2014 13:54:08 -0400 Received: from /spool/local by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 7 Oct 2014 18:54:02 +0100 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 7 Oct 2014 18:54:01 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id 12FA939201EF for ; Tue, 7 Oct 2014 15:52:30 +0100 (BST) Received: from d06av09.portsmouth.uk.ibm.com (d06av09.portsmouth.uk.ibm.com [9.149.37.250]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id s97EkJl453281016 for ; Tue, 7 Oct 2014 14:50:14 GMT Received: from d06av09.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av09.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id s97Ee1rB022087 for ; Tue, 7 Oct 2014 08:40:02 -0600 Received: from gondolin.boeblingen.de.ibm.com (dyn-9-152-224-210.boeblingen.de.ibm.com [9.152.224.210]) by d06av09.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id s97Edxuk021862; Tue, 7 Oct 2014 08:40:01 -0600 From: Cornelia Huck To: virtualization@lists.linux-foundation.org, qemu-devel@nongnu.org, kvm@vger.kernel.org Cc: rusty@rustcorp.com.au, thuth@linux.vnet.ibm.com, Cornelia Huck Subject: [PATCH RFC 03/11] virtio: endianess conversion helpers Date: Tue, 7 Oct 2014 16:39:44 +0200 Message-Id: <1412692792-12325-4-git-send-email-cornelia.huck@de.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1412692792-12325-1-git-send-email-cornelia.huck@de.ibm.com> References: <1412692792-12325-1-git-send-email-cornelia.huck@de.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 14100717-2966-0000-0000-000001613E94 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Provide helper functions that convert from/to LE for virtio devices that are not operating in legacy mode. We check for the VERSION_1 feature bit to determine that. Based on original patches by Rusty Russell and Thomas Huth. Reviewed-by: David Hildenbrand Signed-off-by: Cornelia Huck --- drivers/virtio/virtio.c | 4 ++++ include/linux/virtio.h | 40 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/virtio_config.h | 3 +++ 3 files changed, 47 insertions(+) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index cfd5d00..8f74cd6 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -144,6 +144,10 @@ static int virtio_dev_probe(struct device *_d) if (device_features & (1ULL << i)) dev->features |= (1ULL << i); + /* Version 1.0 compliant devices set the VIRTIO_F_VERSION_1 bit */ + if (device_features & (1ULL << VIRTIO_F_VERSION_1)) + dev->features |= (1ULL << VIRTIO_F_VERSION_1); + dev->config->finalize_features(dev); err = drv->probe(dev); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index a24b41f..68cadd4 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -9,6 +9,7 @@ #include #include #include +#include /** * virtqueue - a queue to register buffers for sending or receiving. @@ -102,6 +103,11 @@ static inline struct virtio_device *dev_to_virtio(struct device *_dev) return container_of(_dev, struct virtio_device, dev); } +static inline bool virtio_device_legacy(const struct virtio_device *dev) +{ + return !(dev->features & (1ULL << VIRTIO_F_VERSION_1)); +} + int register_virtio_device(struct virtio_device *dev); void unregister_virtio_device(struct virtio_device *dev); @@ -149,4 +155,38 @@ void unregister_virtio_driver(struct virtio_driver *drv); #define module_virtio_driver(__virtio_driver) \ module_driver(__virtio_driver, register_virtio_driver, \ unregister_virtio_driver) + +/* + * v1.0 specifies LE headers, legacy was native endian. Therefore, we must + * convert from/to LE if and only if vdev is not legacy. + */ +static inline u16 virtio_to_cpu_u16(const struct virtio_device *vdev, u16 v) +{ + return virtio_device_legacy(vdev) ? v : le16_to_cpu(v); +} + +static inline u32 virtio_to_cpu_u32(const struct virtio_device *vdev, u32 v) +{ + return virtio_device_legacy(vdev) ? v : le32_to_cpu(v); +} + +static inline u64 virtio_to_cpu_u64(const struct virtio_device *vdev, u64 v) +{ + return virtio_device_legacy(vdev) ? v : le64_to_cpu(v); +} + +static inline u16 cpu_to_virtio_u16(const struct virtio_device *vdev, u16 v) +{ + return virtio_device_legacy(vdev) ? v : cpu_to_le16(v); +} + +static inline u32 cpu_to_virtio_u32(const struct virtio_device *vdev, u32 v) +{ + return virtio_device_legacy(vdev) ? v : cpu_to_le32(v); +} + +static inline u64 cpu_to_virtio_u64(const struct virtio_device *vdev, u64 v) +{ + return virtio_device_legacy(vdev) ? v : cpu_to_le64(v); +} #endif /* _LINUX_VIRTIO_H */ diff --git a/include/uapi/linux/virtio_config.h b/include/uapi/linux/virtio_config.h index 3ce768c..80e7381 100644 --- a/include/uapi/linux/virtio_config.h +++ b/include/uapi/linux/virtio_config.h @@ -54,4 +54,7 @@ /* Can the device handle any descriptor layout? */ #define VIRTIO_F_ANY_LAYOUT 27 +/* v1.0 compliant. */ +#define VIRTIO_F_VERSION_1 32 + #endif /* _UAPI_LINUX_VIRTIO_CONFIG_H */