From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751733AbbCLDGr (ORCPT ); Wed, 11 Mar 2015 23:06:47 -0400 Received: from ozlabs.org ([103.22.144.67]:34579 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751257AbbCLDGq (ORCPT ); Wed, 11 Mar 2015 23:06:46 -0400 From: Rusty Russell To: "Michael S. Tsirkin" , linux-kernel@vger.kernel.org Cc: virtualization@lists.linux-foundation.org, Pawel Moll Subject: Re: [PATCH] virtio_mmio: fix endian-ness for mmio In-Reply-To: <1425592060-18474-1-git-send-email-mst@redhat.com> References: <1425592060-18474-1-git-send-email-mst@redhat.com> User-Agent: Notmuch/0.17 (http://notmuchmail.org) Emacs/24.3.1 (x86_64-pc-linux-gnu) Date: Thu, 12 Mar 2015 12:33:36 +1030 Message-ID: <871tkvx98n.fsf@rustcorp.com.au> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org "Michael S. Tsirkin" writes: > Going over the virtio mmio code, I noticed that it doesn't correctly > return device config values in LE format when using virtio 1.0. > Borrow code from virtio_pci_modern to do this correctly. AFAICT, it doesn't need to. The endian correction is done by the callers. The only reason that virtio_pci_modern() does it is because readl() etc do endian conversion, and we don't want them to. And the PCI part of the spec says to use "natural" accessors, so we don't do byte-at-a-time. Cheers, Rusty. > Signed-off-by: Michael S. Tsirkin > --- > > Note: untested: QEMU doesn't support virtio 1.0 for virtio-mmio. > Pawel, could you please confirm this patch makes sense? > > drivers/virtio/virtio_mmio.c | 79 +++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 71 insertions(+), 8 deletions(-) > > diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c > index cad5698..0375456 100644 > --- a/drivers/virtio/virtio_mmio.c > +++ b/drivers/virtio/virtio_mmio.c > @@ -156,22 +156,85 @@ static void vm_get(struct virtio_device *vdev, unsigned offset, > void *buf, unsigned len) > { > struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); > - u8 *ptr = buf; > - int i; > + void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; > + u8 b; > + __le16 w; > + __le32 l; > > - for (i = 0; i < len; i++) > - ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i); > + if (vm_dev->version == 1) { > + u8 *ptr = buf; > + int i; > + > + for (i = 0; i < len; i++) > + ptr[i] = readb(base + offset + i); > + return; > + } > + > + switch (len) { > + case 1: > + b = readb(base + offset); > + memcpy(buf, &b, sizeof b); > + break; > + case 2: > + w = cpu_to_le16(readw(base + offset)); > + memcpy(buf, &w, sizeof w); > + break; > + case 4: > + l = cpu_to_le32(readl(base + offset)); > + memcpy(buf, &l, sizeof l); > + break; > + case 8: > + l = cpu_to_le32(readl(base + offset)); > + memcpy(buf, &l, sizeof l); > + l = cpu_to_le32(ioread32(base + offset + sizeof l)); > + memcpy(buf + sizeof l, &l, sizeof l); > + break; > + default: > + BUG(); > + } > } > > static void vm_set(struct virtio_device *vdev, unsigned offset, > const void *buf, unsigned len) > { > struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); > - const u8 *ptr = buf; > - int i; > + void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG; > + u8 b; > + __le16 w; > + __le32 l; > > - for (i = 0; i < len; i++) > - writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i); > + if (vm_dev->version == 1) { > + const u8 *ptr = buf; > + int i; > + > + for (i = 0; i < len; i++) > + writeb(ptr[i], base + offset + i); > + > + return; > + } > + > + switch (len) { > + case 1: > + memcpy(&b, buf, sizeof b); > + writeb(b, base + offset); > + break; > + case 2: > + memcpy(&w, buf, sizeof w); > + writew(le16_to_cpu(w), base + offset); > + break; > + case 4: > + memcpy(&l, buf, sizeof l); > + writel(le32_to_cpu(l), base + offset); > + break; > + case 8: > + memcpy(&l, buf, sizeof l); > + writel(le32_to_cpu(l), base + offset); > + memcpy(&l, buf + sizeof l, sizeof l); > + writel(le32_to_cpu(l), base + offset + sizeof l); > + break; > + default: > + BUG(); > + } > } > > static u8 vm_get_status(struct virtio_device *vdev) > -- > MST