Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev Add VirtIO PCI v1.0 attachments and fix the drivers ...



details:   https://anonhg.NetBSD.org/src/rev/1add65d118b0
branches:  trunk
changeset: 958803:1add65d118b0
user:      reinoud <reinoud%NetBSD.org@localhost>
date:      Wed Jan 20 19:46:48 2021 +0000

description:
Add VirtIO PCI v1.0 attachments and fix the drivers affected.

The vioif, ld, scsi, viornd and viomb devices were adjusted when needed and
tested both in legacy 0.9 and v1.0 attachments trough PCI on amd64, sparc64,
aarch64 and aarch64-eb. ACPI/FDT attachments also tested on
aarch64/aarch64-eb.

Known issues

* viomb on aarch64 works only with ACPI/FDT attachment but not with PCI
  attachment. PCI and ACPI/FDT attachment works on aarch64-eb.

* virtio on sparc64 attaches but is it not functioning though not a
  regression.

diffstat:

 sys/dev/acpi/virtio_acpi.c    |   15 +-
 sys/dev/fdt/virtio_mmio_fdt.c |   17 +-
 sys/dev/pci/if_vioif.c        |  231 +++++----
 sys/dev/pci/ld_virtio.c       |   41 +-
 sys/dev/pci/vio9p.c           |   17 +-
 sys/dev/pci/viomb.c           |  114 ++--
 sys/dev/pci/viornd.c          |    4 +-
 sys/dev/pci/vioscsi.c         |   20 +-
 sys/dev/pci/virtio.c          |  446 ++++++++++++++----
 sys/dev/pci/virtio_pci.c      |  943 +++++++++++++++++++++++++++++++++--------
 sys/dev/pci/virtio_pcireg.h   |  141 ++++++
 sys/dev/pci/virtioreg.h       |   94 ++-
 sys/dev/pci/virtiovar.h       |   58 ++-
 sys/dev/virtio/virtio_mmio.c  |  130 +++--
 14 files changed, 1678 insertions(+), 593 deletions(-)

diffs (truncated from 3860 to 300 lines):

diff -r 7461ab378288 -r 1add65d118b0 sys/dev/acpi/virtio_acpi.c
--- a/sys/dev/acpi/virtio_acpi.c        Wed Jan 20 18:26:11 2021 +0000
+++ b/sys/dev/acpi/virtio_acpi.c        Wed Jan 20 19:46:48 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: virtio_acpi.c,v 1.4 2020/12/07 10:02:51 jmcneill Exp $ */
+/* $NetBSD: virtio_acpi.c,v 1.5 2021/01/20 19:46:48 reinoud Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio_acpi.c,v 1.4 2020/12/07 10:02:51 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_acpi.c,v 1.5 2021/01/20 19:46:48 reinoud Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -163,7 +163,16 @@
        memset(&va, 0, sizeof(va));
        va.sc_childdevid = vsc->sc_childdevid;
 
-       config_found_ia(self, ifattr, &va, virtiobusprint);
+       config_found_ia(self, ifattr, &va, NULL);
+
+       if (virtio_attach_failed(vsc))
+               return 0;
+
+       /*
+        * Make sure child drivers initialize interrupts via call
+        * to virtio_child_attach_finish().
+        */
+       KASSERT(msc->sc_ih != NULL);
 
        return 0;
 }
diff -r 7461ab378288 -r 1add65d118b0 sys/dev/fdt/virtio_mmio_fdt.c
--- a/sys/dev/fdt/virtio_mmio_fdt.c     Wed Jan 20 18:26:11 2021 +0000
+++ b/sys/dev/fdt/virtio_mmio_fdt.c     Wed Jan 20 19:46:48 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: virtio_mmio_fdt.c,v 1.4 2021/01/15 22:35:39 jmcneill Exp $ */
+/* $NetBSD: virtio_mmio_fdt.c,v 1.5 2021/01/20 19:46:48 reinoud Exp $ */
 
 /*
  * Copyright (c) 2018 Jonathan A. Kollasch
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio_mmio_fdt.c,v 1.4 2021/01/15 22:35:39 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_mmio_fdt.c,v 1.5 2021/01/20 19:46:48 reinoud Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -119,19 +119,14 @@
 
        if (vsc->sc_child)      /* Child already attached? */
                return 0;
+
        memset(&va, 0, sizeof(va));
        va.sc_childdevid = vsc->sc_childdevid;
 
-       config_found_ia(self, attr, &va, virtiobusprint);
+       config_found_ia(self, attr, &va, NULL);
 
-       if (vsc->sc_child == NULL) {
+       if (virtio_attach_failed(vsc))
                return 0;
-       }
-
-       if (vsc->sc_child == VIRTIO_CHILD_FAILED) {
-               aprint_error_dev(self, "virtio configuration failed\n");
-               return 0;
-       }
 
        /*
         * Make sure child drivers initialize interrupts via call
@@ -164,7 +159,7 @@
                return -1;
        }
 
-       if (vsc->sc_flags & VIRTIO_F_PCI_INTR_MPSAFE)
+       if (vsc->sc_flags & VIRTIO_F_INTR_MPSAFE)
                flags |= FDT_INTR_MPSAFE;
 
        msc->sc_ih = fdtbus_intr_establish_xname(fsc->sc_phandle, 0,
diff -r 7461ab378288 -r 1add65d118b0 sys/dev/pci/if_vioif.c
--- a/sys/dev/pci/if_vioif.c    Wed Jan 20 18:26:11 2021 +0000
+++ b/sys/dev/pci/if_vioif.c    Wed Jan 20 19:46:48 2021 +0000
@@ -1,6 +1,7 @@
-/*     $NetBSD: if_vioif.c,v 1.65 2020/05/28 23:25:17 riastradh Exp $  */
+/*     $NetBSD: if_vioif.c,v 1.66 2021/01/20 19:46:48 reinoud Exp $    */
 
 /*
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
  * Copyright (c) 2010 Minoura Makoto.
  * All rights reserved.
  *
@@ -26,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.65 2020/05/28 23:25:17 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.66 2021/01/20 19:46:48 reinoud Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_net_mpsafe.h"
@@ -70,9 +71,10 @@
  * if_vioifreg.h:
  */
 /* Configuration registers */
-#define VIRTIO_NET_CONFIG_MAC          0 /* 8bit x 6byte */
-#define VIRTIO_NET_CONFIG_STATUS       6 /* 16bit */
-#define VIRTIO_NET_CONFIG_MAX_VQ_PAIRS 8 /* 16bit */
+#define VIRTIO_NET_CONFIG_MAC           0 /* 8bit x 6byte */
+#define VIRTIO_NET_CONFIG_STATUS        6 /* 16bit */
+#define VIRTIO_NET_CONFIG_MAX_VQ_PAIRS  8 /* 16bit */
+#define VIRTIO_NET_CONFIG_MTU          10 /* 16bit */
 
 /* Feature bits */
 #define VIRTIO_NET_F_CSUM              __BIT(0)
@@ -130,9 +132,8 @@
        uint16_t        gso_size;
        uint16_t        csum_start;
        uint16_t        csum_offset;
-#if 0
-       uint16_t        num_buffers; /* if VIRTIO_NET_F_MRG_RXBUF enabled */
-#endif
+
+       uint16_t        num_buffers; /* VIRTIO_NET_F_MRG_RXBUF enabled or v1 */
 } __packed;
 
 #define VIRTIO_NET_HDR_F_NEEDS_CSUM    1 /* flags */
@@ -188,12 +189,6 @@
        uint16_t virtqueue_pairs;
 } __packed;
 
-struct vioif_ctrl_cmdspec {
-       bus_dmamap_t    dmamap;
-       void            *buf;
-       bus_size_t      bufsize;
-};
-
 /*
  * if_vioifvar.h:
  */
@@ -211,6 +206,12 @@
  *      - the lock is held before acquisition of other locks
  */
 
+struct vioif_ctrl_cmdspec {
+       bus_dmamap_t    dmamap;
+       void            *buf;
+       bus_size_t      bufsize;
+};
+
 struct vioif_work {
        struct work      cookie;
        void            (*func)(void *);
@@ -299,6 +300,7 @@
 
        struct virtio_softc     *sc_virtio;
        struct virtqueue        *sc_vqs;
+       u_int                    sc_hdr_size;
 
        int                     sc_max_nvq_pairs;
        int                     sc_req_nvq_pairs;
@@ -357,7 +359,8 @@
 /* rx */
 static int     vioif_add_rx_mbuf(struct vioif_rxqueue *, int);
 static void    vioif_free_rx_mbuf(struct vioif_rxqueue *, int);
-static void    vioif_populate_rx_mbufs_locked(struct vioif_rxqueue *);
+static void    vioif_populate_rx_mbufs_locked(struct vioif_softc *,
+                   struct vioif_rxqueue *);
 static void    vioif_rx_queue_clear(struct vioif_rxqueue *);
 static bool    vioif_rx_deq_locked(struct vioif_softc *, struct virtio_softc *,
                    struct vioif_rxqueue *, u_int);
@@ -412,7 +415,7 @@
 {
        struct virtio_attach_args *va = aux;
 
-       if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_NETWORK)
+       if (va->sc_childdevid == VIRTIO_DEVICE_ID_NETWORK)
                return 1;
 
        return 0;
@@ -571,10 +574,8 @@
                rxq = &sc->sc_rxq[qid];
                txq = &sc->sc_txq[qid];
 
-               allocsize +=
-                   sizeof(struct virtio_net_hdr) * rxq->rxq_vq->vq_num;
-               allocsize +=
-                   sizeof(struct virtio_net_hdr) * txq->txq_vq->vq_num;
+               allocsize += sc->sc_hdr_size * rxq->rxq_vq->vq_num;
+               allocsize += sc->sc_hdr_size * txq->txq_vq->vq_num;
        }
        if (sc->sc_has_ctrl) {
                allocsize += sizeof(struct virtio_net_ctrl_cmd) * 1;
@@ -610,9 +611,9 @@
                txq = &sc->sc_txq[qid];
 
                rxq->rxq_hdrs = vioif_assign_mem(&p,
-                   sizeof(rxq->rxq_hdrs[0]) * rxq->rxq_vq->vq_num);
+                   sc->sc_hdr_size * rxq->rxq_vq->vq_num);
                txq->txq_hdrs = vioif_assign_mem(&p,
-                   sizeof(txq->txq_hdrs[0]) * txq->txq_vq->vq_num);
+                   sc->sc_hdr_size * txq->txq_vq->vq_num);
        }
        if (sc->sc_has_ctrl) {
                ctrlq->ctrlq_cmd = vioif_assign_mem(&p,
@@ -677,7 +678,7 @@
 
                for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
                        r = vioif_dmamap_create_load(sc, &rxq->rxq_hdr_dmamaps[i],
-                           &rxq->rxq_hdrs[i], sizeof(rxq->rxq_hdrs[0]), 1,
+                           &rxq->rxq_hdrs[i], sc->sc_hdr_size, 1,
                            BUS_DMA_READ, "rx header");
                        if (r != 0)
                                goto err_reqs;
@@ -690,7 +691,7 @@
 
                for (i = 0; i < txq->txq_vq->vq_num; i++) {
                        r = vioif_dmamap_create_load(sc, &txq->txq_hdr_dmamaps[i],
-                           &txq->txq_hdrs[i], sizeof(txq->txq_hdrs[0]), 1,
+                           &txq->txq_hdrs[i], sc->sc_hdr_size, 1,
                            BUS_DMA_READ, "tx header");
                        if (r != 0)
                                goto err_reqs;
@@ -785,7 +786,7 @@
        struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
        struct vioif_txqueue *txq;
        struct vioif_rxqueue *rxq;
-       uint32_t features, req_features;
+       uint64_t features, req_features;
        struct ifnet *ifp = &sc->sc_ethercom.ec_if;
        u_int softint_flags;
        int r, i, nvqs=0, req_flags;
@@ -821,13 +822,14 @@
        req_flags = 0;
 
 #ifdef VIOIF_MPSAFE
-       req_flags |= VIRTIO_F_PCI_INTR_MPSAFE;
+       req_flags |= VIRTIO_F_INTR_MPSAFE;
 #endif
-       req_flags |= VIRTIO_F_PCI_INTR_MSIX;
+       req_flags |= VIRTIO_F_INTR_MSIX;
 
        req_features =
            VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_NET_F_CTRL_VQ |
            VIRTIO_NET_F_CTRL_RX | VIRTIO_F_NOTIFY_ON_EMPTY;
+       req_features |= VIRTIO_F_RING_EVENT_IDX;
 #ifdef VIOIF_MULTIQ
        req_features |= VIRTIO_NET_F_MQ;
 #endif
@@ -836,6 +838,8 @@
            req_features, VIRTIO_NET_FLAG_BITS);
 
        features = virtio_features(vsc);
+       if (features == 0)
+               goto err;
 
        if (features & VIRTIO_NET_F_MAC) {
                for (i = 0; i < __arraycount(sc->sc_mac); i++) {
@@ -855,9 +859,16 @@
                }
        }
 
+       /* 'Ethernet' with capital follows other ethernet driver attachment */
        aprint_normal_dev(self, "Ethernet address %s\n",
            ether_sprintf(sc->sc_mac));
 
+       if (features & (VIRTIO_NET_F_MRG_RXBUF | VIRTIO_F_VERSION_1)) {
+               sc->sc_hdr_size = sizeof(struct virtio_net_hdr);
+       } else {
+               sc->sc_hdr_size = offsetof(struct virtio_net_hdr, num_buffers);
+       }
+
        if ((features & VIRTIO_NET_F_CTRL_VQ) &&
            (features & VIRTIO_NET_F_CTRL_RX)) {
                sc->sc_has_ctrl = true;
@@ -908,7 +919,7 @@
 
                snprintf(qname, sizeof(qname), "rx%d", i);
                r = virtio_alloc_vq(vsc, rxq->rxq_vq, nvqs,
-                   MCLBYTES+sizeof(struct virtio_net_hdr), 2, qname);
+                   MCLBYTES + sc->sc_hdr_size, 2, qname);
                if (r != 0)
                        goto err;
                nvqs++;
@@ -934,8 +945,7 @@
 
                snprintf(qname, sizeof(qname), "tx%d", i);
                r = virtio_alloc_vq(vsc, txq->txq_vq, nvqs,
-                   sizeof(struct virtio_net_hdr)
-                   + (ETHER_MAX_LEN - ETHER_HDR_LEN),
+                   sc->sc_hdr_size + (ETHER_MAX_LEN - ETHER_HDR_LEN),
                    VIRTIO_NET_TX_MAXNSEGS + 1, qname);
                if (r != 0)
                        goto err;



Home | Main Index | Thread Index | Old Index