Module Name:    src
Committed By:   yamaguchi
Date:           Thu Oct 28 01:36:43 UTC 2021

Modified Files:
        src/sys/dev/pci: if_vioif.c virtio.c virtio_pci.c virtiovar.h

Log Message:
virtio: stop reinit for safety when a device resetting is failed


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/dev/pci/if_vioif.c
cvs rdiff -u -r1.52 -r1.53 src/sys/dev/pci/virtio.c
cvs rdiff -u -r1.32 -r1.33 src/sys/dev/pci/virtio_pci.c
cvs rdiff -u -r1.22 -r1.23 src/sys/dev/pci/virtiovar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/pci/if_vioif.c
diff -u src/sys/dev/pci/if_vioif.c:1.70 src/sys/dev/pci/if_vioif.c:1.71
--- src/sys/dev/pci/if_vioif.c:1.70	Mon Feb  8 06:56:26 2021
+++ src/sys/dev/pci/if_vioif.c	Thu Oct 28 01:36:43 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_vioif.c,v 1.70 2021/02/08 06:56:26 skrll Exp $	*/
+/*	$NetBSD: if_vioif.c,v 1.71 2021/10/28 01:36:43 yamaguchi Exp $	*/
 
 /*
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.70 2021/02/08 06:56:26 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.71 2021/10/28 01:36:43 yamaguchi Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_net_mpsafe.h"
@@ -46,6 +46,7 @@ __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v
 #include <sys/mbuf.h>
 #include <sys/mutex.h>
 #include <sys/sockio.h>
+#include <sys/syslog.h>
 #include <sys/cpu.h>
 #include <sys/module.h>
 #include <sys/pcq.h>
@@ -1155,7 +1156,12 @@ vioif_init(struct ifnet *ifp)
 
 	vioif_stop(ifp, 0);
 
-	virtio_reinit_start(vsc);
+	r = virtio_reinit_start(vsc);
+	if (r != 0) {
+		log(LOG_ERR, "%s: reset failed\n", ifp->if_xname);
+		return EIO;
+	}
+
 	virtio_negotiate_features(vsc, virtio_features(vsc));
 
 	for (i = 0; i < sc->sc_req_nvq_pairs; i++) {

Index: src/sys/dev/pci/virtio.c
diff -u src/sys/dev/pci/virtio.c:1.52 src/sys/dev/pci/virtio.c:1.53
--- src/sys/dev/pci/virtio.c:1.52	Thu Oct 21 07:08:55 2021
+++ src/sys/dev/pci/virtio.c	Thu Oct 28 01:36:43 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: virtio.c,v 1.52 2021/10/21 07:08:55 yamaguchi Exp $	*/
+/*	$NetBSD: virtio.c,v 1.53 2021/10/28 01:36:43 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.52 2021/10/21 07:08:55 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.53 2021/10/28 01:36:43 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -90,7 +90,7 @@ virtio_reset(struct virtio_softc *sc)
 	virtio_device_reset(sc);
 }
 
-void
+int
 virtio_reinit_start(struct virtio_softc *sc)
 {
 	int i, r;
@@ -114,10 +114,15 @@ virtio_reinit_start(struct virtio_softc 
 	}
 
 	r = sc->sc_ops->setup_interrupts(sc, 1);
-	if (r != 0) {
-		printf("%s: failed to setup interrupts\n",
-		    device_xname(sc->sc_dev));
-	}
+	if (r != 0)
+		goto fail;
+
+	return 0;
+
+fail:
+	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
+
+	return 1;
 }
 
 void

Index: src/sys/dev/pci/virtio_pci.c
diff -u src/sys/dev/pci/virtio_pci.c:1.32 src/sys/dev/pci/virtio_pci.c:1.33
--- src/sys/dev/pci/virtio_pci.c:1.32	Thu Oct 21 05:37:43 2021
+++ src/sys/dev/pci/virtio_pci.c	Thu Oct 28 01:36:43 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: virtio_pci.c,v 1.32 2021/10/21 05:37:43 yamaguchi Exp $ */
+/* $NetBSD: virtio_pci.c,v 1.33 2021/10/28 01:36:43 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.32 2021/10/21 05:37:43 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.33 2021/10/28 01:36:43 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -36,6 +36,7 @@ __KERNEL_RCSID(0, "$NetBSD: virtio_pci.c
 #include <sys/module.h>
 #include <sys/endian.h>
 #include <sys/interrupt.h>
+#include <sys/syslog.h>
 
 #include <sys/device.h>
 
@@ -50,6 +51,18 @@ __KERNEL_RCSID(0, "$NetBSD: virtio_pci.c
 #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
 
 
+#define VIRTIO_PCI_LOG(_sc, _use_log, _fmt, _args...)	\
+do {							\
+	if ((_use_log)) {				\
+		log(LOG_DEBUG, "%s: " _fmt,		\
+		    device_xname((_sc)->sc_dev),	\
+		    ##_args);				\
+	} else {					\
+		aprint_error_dev((_sc)->sc_dev,		\
+		    _fmt, ##_args);			\
+	}						\
+} while(0)
+
 static int	virtio_pci_match(device_t, cfdata_t, void *);
 static void	virtio_pci_attach(device_t, device_t, void *);
 static int	virtio_pci_rescan(device_t, const char *, const int *);
@@ -808,7 +821,6 @@ static int
 virtio_pci_setup_interrupts_10(struct virtio_softc *sc, int reinit)
 {
 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
-	device_t self          =  sc->sc_dev;
 	bus_space_tag_t	   iot = psc->sc_iot;
 	bus_space_handle_t ioh = psc->sc_ioh;
 	int vector, ret, qid;
@@ -821,10 +833,8 @@ virtio_pci_setup_interrupts_10(struct vi
 		VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR, vector);
 	ret = bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR);
 	if (ret != vector) {
-		if (reinit == 0) {
-			aprint_error_dev(self,
-			    "can't set config msix vector\n");
-		}
+		VIRTIO_PCI_LOG(sc, reinit,
+		    "can't set config msix vector\n");
 		return -1;
 	}
 
@@ -839,10 +849,8 @@ virtio_pci_setup_interrupts_10(struct vi
 		ret = bus_space_read_2(iot, ioh,
 			VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR);
 		if (ret != vector) {
-			if (reinit == 0) {
-				aprint_error_dev(self, "can't set queue %d "
-				    "msix vector\n", qid);
-			}
+			VIRTIO_PCI_LOG(sc, reinit, "can't set queue %d "
+			    "msix vector\n", qid);
 			return -1;
 		}
 	}
@@ -854,7 +862,6 @@ static int
 virtio_pci_setup_interrupts_09(struct virtio_softc *sc, int reinit)
 {
 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
-	device_t self = sc->sc_dev;
 	int offset, vector, ret, qid;
 
 	if (!virtio_pci_msix_enabled(psc))
@@ -868,10 +875,8 @@ virtio_pci_setup_interrupts_09(struct vi
 	aprint_debug_dev(sc->sc_dev, "expected=%d, actual=%d\n",
 	    vector, ret);
 	if (ret != vector) {
-		if (reinit == 0) {
-			aprint_error_dev(self,
-			    "can't set config msix vector\n");
-		}
+		VIRTIO_PCI_LOG(sc, reinit,
+		    "can't set config msix vector\n");
 		return -1;
 	}
 
@@ -890,10 +895,8 @@ virtio_pci_setup_interrupts_09(struct vi
 		aprint_debug_dev(sc->sc_dev, "expected=%d, actual=%d\n",
 		    vector, ret);
 		if (ret != vector) {
-			if (reinit == 0) {
-				aprint_error_dev(self, "can't set queue %d "
-				    "msix vector\n", qid);
-			}
+			VIRTIO_PCI_LOG(sc, reinit, "can't set queue %d "
+			    "msix vector\n", qid);
 			return -1;
 		}
 	}

Index: src/sys/dev/pci/virtiovar.h
diff -u src/sys/dev/pci/virtiovar.h:1.22 src/sys/dev/pci/virtiovar.h:1.23
--- src/sys/dev/pci/virtiovar.h:1.22	Thu Oct 21 05:37:43 2021
+++ src/sys/dev/pci/virtiovar.h	Thu Oct 28 01:36:43 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: virtiovar.h,v 1.22 2021/10/21 05:37:43 yamaguchi Exp $	*/
+/*	$NetBSD: virtiovar.h,v 1.23 2021/10/28 01:36:43 yamaguchi Exp $	*/
 
 /*
  * Copyright (c) 2010 Minoura Makoto.
@@ -208,7 +208,7 @@ int virtio_alloc_vq(struct virtio_softc*
 		    const char*);
 int virtio_free_vq(struct virtio_softc*, struct virtqueue*);
 void virtio_reset(struct virtio_softc *);
-void virtio_reinit_start(struct virtio_softc *);
+int virtio_reinit_start(struct virtio_softc *);
 void virtio_reinit_end(struct virtio_softc *);
 void virtio_child_attach_start(struct virtio_softc *, device_t, int,
                     struct virtqueue *,

Reply via email to