Source-Changes-HG archive

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

[src/trunk]: src/sys/dev Use enumeration for state of a child driver instead ...



details:   https://anonhg.NetBSD.org/src/rev/425731fc2088
branches:  trunk
changeset: 374330:425731fc2088
user:      yamaguchi <yamaguchi%NetBSD.org@localhost>
date:      Wed Apr 19 00:23:45 2023 +0000

description:
Use enumeration for state of a child driver instead of flags
and check its detaching by using sc->sc_child in virtio_softc

pointed out by riastradh, thanks.
fixes PR/57357

diffstat:

 sys/dev/pci/virtio.c         |  42 ++++++++++++++++++++++++++----------------
 sys/dev/pci/virtio_pci.c     |   9 +++------
 sys/dev/pci/virtiovar.h      |  11 ++++++-----
 sys/dev/virtio/virtio_mmio.c |   6 +++---
 4 files changed, 38 insertions(+), 30 deletions(-)

diffs (187 lines):

diff -r 07269bc2bcae -r 425731fc2088 sys/dev/pci/virtio.c
--- a/sys/dev/pci/virtio.c      Tue Apr 18 23:05:51 2023 +0000
+++ b/sys/dev/pci/virtio.c      Wed Apr 19 00:23:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: virtio.c,v 1.74 2023/03/31 07:34:26 yamaguchi Exp $    */
+/*     $NetBSD: virtio.c,v 1.75 2023/04/19 00:23:45 yamaguchi Exp $    */
 
 /*
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.74 2023/03/31 07:34:26 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.75 2023/04/19 00:23:45 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1328,7 +1328,7 @@ virtio_child_attach_start(struct virtio_
        char buf[1024];
 
        KASSERT(sc->sc_child == NULL);
-       KASSERT(!ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED));
+       KASSERT(sc->sc_child_state == VIRTIO_NO_CHILD);
 
        sc->sc_child = child;
        sc->sc_ipl = ipl;
@@ -1404,7 +1404,7 @@ virtio_child_attach_finish(struct virtio
                }
        }
 
-       SET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FINISHED);
+       sc->sc_child_state = VIRTIO_CHILD_ATTACH_FINISHED;
        virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
        return 0;
 
@@ -1425,10 +1425,9 @@ virtio_child_detach(struct virtio_softc 
 {
 
        /* already detached */
-       if (ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED))
+       if (sc->sc_child == NULL)
                return;
 
-       sc->sc_vqs = NULL;
 
        virtio_device_reset(sc);
 
@@ -1439,7 +1438,8 @@ virtio_child_detach(struct virtio_softc 
                sc->sc_soft_ih = NULL;
        }
 
-       SET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED);
+       sc->sc_vqs = NULL;
+       sc->sc_child = NULL;
 }
 
 void
@@ -1449,7 +1449,7 @@ virtio_child_attach_failed(struct virtio
 
        virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
 
-       SET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FAILED);
+       sc->sc_child_state = VIRTIO_CHILD_ATTACH_FAILED;
 }
 
 bus_dma_tag_t
@@ -1485,19 +1485,29 @@ virtio_attach_failed(struct virtio_softc
        if (sc->sc_childdevid == 0)
                return 1;
 
-       if (ISSET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FAILED)) {
-               aprint_error_dev(self, "virtio configuration failed\n");
-               return 1;
-       }
+       if (sc->sc_child == NULL) {
+               switch (sc->sc_child_state) {
+               case VIRTIO_CHILD_ATTACH_FAILED:
+                       aprint_error_dev(self,
+                           "virtio configuration failed\n");
+                       break;
+               case VIRTIO_NO_CHILD:
+                       aprint_error_dev(self,
+                           "no matching child driver; not configured\n");
+                       break;
+               default:
+                       /* sanity check */
+                       aprint_error_dev(self,
+                           "virtio internal error, "
+                           "child driver is not configured\n");
+                       break;
+               }
 
-       if (sc->sc_child == NULL) {
-               aprint_error_dev(self,
-                   "no matching child driver; not configured\n");
                return 1;
        }
 
        /* sanity check */
-       if (!ISSET(sc->sc_child_flags, VIRTIO_CHILD_ATTACH_FINISHED)) {
+       if (sc->sc_child_state != VIRTIO_CHILD_ATTACH_FINISHED) {
                aprint_error_dev(self, "virtio internal error, child driver "
                    "signaled OK but didn't initialize interrupts\n");
                return 1;
diff -r 07269bc2bcae -r 425731fc2088 sys/dev/pci/virtio_pci.c
--- a/sys/dev/pci/virtio_pci.c  Tue Apr 18 23:05:51 2023 +0000
+++ b/sys/dev/pci/virtio_pci.c  Wed Apr 19 00:23:45 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: virtio_pci.c,v 1.41 2023/04/16 17:57:08 riastradh Exp $ */
+/* $NetBSD: virtio_pci.c,v 1.42 2023/04/19 00:23:45 yamaguchi Exp $ */
 
 /*
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.41 2023/04/16 17:57:08 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.42 2023/04/19 00:23:45 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -334,10 +334,7 @@ virtio_pci_detach(device_t self, int fla
                return r;
 
        /* Check that child never attached, or detached properly */
-       KASSERTMSG(!ISSET(sc->sc_child_flags,
-               (VIRTIO_CHILD_ATTACH_FINISHED|VIRTIO_CHILD_ATTACH_FAILED)) ||
-           ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED),
-           "%s: child flags %x", device_xname(self), sc->sc_child_flags);
+       KASSERT(sc->sc_child == NULL);
        KASSERT(sc->sc_vqs == NULL);
        KASSERT(psc->sc_ihs_num == 0);
 
diff -r 07269bc2bcae -r 425731fc2088 sys/dev/pci/virtiovar.h
--- a/sys/dev/pci/virtiovar.h   Tue Apr 18 23:05:51 2023 +0000
+++ b/sys/dev/pci/virtiovar.h   Wed Apr 19 00:23:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: virtiovar.h,v 1.28 2023/03/31 07:34:26 yamaguchi Exp $ */
+/*     $NetBSD: virtiovar.h,v 1.29 2023/04/19 00:23:45 yamaguchi Exp $ */
 
 /*
  * Copyright (c) 2010 Minoura Makoto.
@@ -164,10 +164,11 @@ struct virtio_softc {
 
        int                     sc_childdevid;
        device_t                sc_child;               /* set by child */
-       uint32_t                sc_child_flags;
-#define VIRTIO_CHILD_ATTACH_FINISHED   __BIT(0)
-#define VIRTIO_CHILD_ATTACH_FAILED     __BIT(1)
-#define VIRTIO_CHILD_DETACHED          __BIT(2)
+       enum {
+               VIRTIO_NO_CHILD = 0,
+               VIRTIO_CHILD_ATTACH_FINISHED,
+               VIRTIO_CHILD_ATTACH_FAILED
+       }                       sc_child_state;
 
        virtio_callback         sc_config_change;       /* set by child */
        virtio_callback         sc_intrhand;
diff -r 07269bc2bcae -r 425731fc2088 sys/dev/virtio/virtio_mmio.c
--- a/sys/dev/virtio/virtio_mmio.c      Tue Apr 18 23:05:51 2023 +0000
+++ b/sys/dev/virtio/virtio_mmio.c      Wed Apr 19 00:23:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: virtio_mmio.c,v 1.9 2023/03/31 23:34:23 yamaguchi Exp $        */
+/*     $NetBSD: virtio_mmio.c,v 1.10 2023/04/19 00:23:45 yamaguchi Exp $       */
 /*     $OpenBSD: virtio_mmio.c,v 1.2 2017/02/24 17:12:31 patrick Exp $ */
 
 /*
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio_mmio.c,v 1.9 2023/03/31 23:34:23 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_mmio.c,v 1.10 2023/04/19 00:23:45 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -217,7 +217,7 @@ virtio_mmio_common_detach(struct virtio_
        if (r != 0)
                return r;
 
-       KASSERT(ISSET(vsc->sc_child_flags, VIRTIO_CHILD_DETACHED));
+       KASSERT(vsc->sc_child == NULL);
        KASSERT(vsc->sc_vqs == NULL);
        KASSERT(sc->sc_ih == NULL);
 



Home | Main Index | Thread Index | Old Index