Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev/pci reorganize the attachment process for virtio chi...
details: https://anonhg.NetBSD.org/src/rev/3f8864bb6eee
branches: trunk
changeset: 352320:3f8864bb6eee
user: jdolecek <jdolecek%NetBSD.org@localhost>
date: Sat Mar 25 18:02:06 2017 +0000
description:
reorganize the attachment process for virtio child devices, so that
more common code is shared among the drivers, and it's possible for
the drivers to be correctly dynamically loaded; forbid direct access
to struct virtio_softc from the child driver code
diffstat:
sys/dev/pci/if_vioif.c | 189 ++++++++++++++++++++++----------------------
sys/dev/pci/ld_virtio.c | 93 ++++++++++-----------
sys/dev/pci/viomb.c | 59 ++++++-------
sys/dev/pci/viornd.c | 65 +++++++-------
sys/dev/pci/vioscsi.c | 132 +++++++++++++++++++-----------
sys/dev/pci/virtio.c | 201 ++++++++++++++++++++++++++++++++++++++---------
sys/dev/pci/virtiovar.h | 35 ++++++-
7 files changed, 469 insertions(+), 305 deletions(-)
diffs (truncated from 1949 to 300 lines):
diff -r 73f3a50b90f6 -r 3f8864bb6eee sys/dev/pci/if_vioif.c
--- a/sys/dev/pci/if_vioif.c Sat Mar 25 17:50:51 2017 +0000
+++ b/sys/dev/pci/if_vioif.c Sat Mar 25 18:02:06 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: if_vioif.c,v 1.31 2017/01/17 01:25:21 ozaki-r Exp $ */
+/* $NetBSD: if_vioif.c,v 1.32 2017/03/25 18:02:06 jdolecek Exp $ */
/*
* Copyright (c) 2010 Minoura Makoto.
@@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.31 2017/01/17 01:25:21 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.32 2017/03/25 18:02:06 jdolecek Exp $");
#ifdef _KERNEL_OPT
#include "opt_net_mpsafe.h"
@@ -227,6 +227,8 @@
kmutex_t *sc_tx_lock;
kmutex_t *sc_rx_lock;
bool sc_stopping;
+
+ bool sc_has_ctrl;
};
#define VIRTIO_NET_TX_MAXNSEGS (16) /* XXX */
#define VIRTIO_NET_CTRL_MAC_MAXENTRIES (64) /* XXX */
@@ -281,7 +283,7 @@
static int
vioif_match(device_t parent, cfdata_t match, void *aux)
{
- struct virtio_softc *va = aux;
+ struct virtio_attach_args *va = aux;
if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_NETWORK)
return 1;
@@ -323,12 +325,12 @@
intptr_t p;
int rxqsize, txqsize;
- rxqsize = vsc->sc_vqs[VQ_RX].vq_num;
- txqsize = vsc->sc_vqs[VQ_TX].vq_num;
+ rxqsize = sc->sc_vq[VQ_RX].vq_num;
+ txqsize = sc->sc_vq[VQ_TX].vq_num;
allocsize = sizeof(struct virtio_net_hdr) * rxqsize;
allocsize += sizeof(struct virtio_net_hdr) * txqsize;
- if (vsc->sc_nvqs == 3) {
+ if (sc->sc_has_ctrl) {
allocsize += sizeof(struct virtio_net_ctrl_cmd) * 1;
allocsize += sizeof(struct virtio_net_ctrl_status) * 1;
allocsize += sizeof(struct virtio_net_ctrl_rx) * 1;
@@ -336,7 +338,7 @@
+ sizeof(struct virtio_net_ctrl_mac_tbl)
+ ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
}
- r = bus_dmamem_alloc(vsc->sc_dmat, allocsize, 0, 0,
+ r = bus_dmamem_alloc(virtio_dmat(vsc), allocsize, 0, 0,
&sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
if (r != 0) {
aprint_error_dev(sc->sc_dev,
@@ -344,7 +346,7 @@
"error code %d\n", allocsize, r);
goto err_none;
}
- r = bus_dmamem_map(vsc->sc_dmat,
+ r = bus_dmamem_map(virtio_dmat(vsc),
&sc->sc_hdr_segs[0], 1, allocsize,
&vaddr, BUS_DMA_NOWAIT);
if (r != 0) {
@@ -360,7 +362,7 @@
#define P(name,size) do { sc->sc_ ##name = (void*) p; \
p += size; } while (0)
P(tx_hdrs, sizeof(struct virtio_net_hdr) * txqsize);
- if (vsc->sc_nvqs == 3) {
+ if (sc->sc_has_ctrl) {
P(ctrl_cmd, sizeof(struct virtio_net_ctrl_cmd));
P(ctrl_status, sizeof(struct virtio_net_ctrl_status));
P(ctrl_rx, sizeof(struct virtio_net_ctrl_rx));
@@ -385,7 +387,7 @@
#define C(map, buf, size, nsegs, rw, usage) \
do { \
- r = bus_dmamap_create(vsc->sc_dmat, size, nsegs, size, 0, \
+ r = bus_dmamap_create(virtio_dmat(vsc), size, nsegs, size, 0, \
BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, \
&sc->sc_ ##map); \
if (r != 0) { \
@@ -398,7 +400,7 @@
#define C_L1(map, buf, size, nsegs, rw, usage) \
C(map, buf, size, nsegs, rw, usage); \
do { \
- r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ ##map, \
+ r = bus_dmamap_load(virtio_dmat(vsc), sc->sc_ ##map, \
&sc->sc_ ##buf, size, NULL, \
BUS_DMA_ ##rw | BUS_DMA_NOWAIT); \
if (r != 0) { \
@@ -411,7 +413,7 @@
#define C_L2(map, buf, size, nsegs, rw, usage) \
C(map, buf, size, nsegs, rw, usage); \
do { \
- r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ ##map, \
+ r = bus_dmamap_load(virtio_dmat(vsc), sc->sc_ ##map, \
sc->sc_ ##buf, size, NULL, \
BUS_DMA_ ##rw | BUS_DMA_NOWAIT); \
if (r != 0) { \
@@ -436,7 +438,7 @@
"tx payload");
}
- if (vsc->sc_nvqs == 3) {
+ if (sc->sc_has_ctrl) {
/* control vq class & command */
C_L2(ctrl_cmd_dmamap, ctrl_cmd,
sizeof(struct virtio_net_ctrl_cmd), 1, WRITE,
@@ -474,7 +476,7 @@
#define D(map) \
do { \
if (sc->sc_ ##map) { \
- bus_dmamap_destroy(vsc->sc_dmat, sc->sc_ ##map); \
+ bus_dmamap_destroy(virtio_dmat(vsc), sc->sc_ ##map); \
sc->sc_ ##map = NULL; \
} \
} while (0)
@@ -497,9 +499,9 @@
sc->sc_arrays = 0;
}
err_dmamem_map:
- bus_dmamem_unmap(vsc->sc_dmat, sc->sc_hdrs, allocsize);
+ bus_dmamem_unmap(virtio_dmat(vsc), sc->sc_hdrs, allocsize);
err_dmamem_alloc:
- bus_dmamem_free(vsc->sc_dmat, &sc->sc_hdr_segs[0], 1);
+ bus_dmamem_free(virtio_dmat(vsc), &sc->sc_hdr_segs[0], 1);
err_none:
return -1;
}
@@ -510,12 +512,11 @@
struct vioif_softc *sc = device_private(self);
struct virtio_softc *vsc = device_private(parent);
uint32_t features;
- char buf[256];
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
u_int flags;
- int r;
+ int r, nvqs=0, req_flags;
- if (vsc->sc_child != NULL) {
+ if (virtio_child(vsc) != NULL) {
aprint_normal(": child already attached for %s; "
"something wrong...\n",
device_xname(parent));
@@ -525,27 +526,24 @@
sc->sc_dev = self;
sc->sc_virtio = vsc;
- vsc->sc_child = self;
- vsc->sc_ipl = IPL_NET;
- vsc->sc_vqs = &sc->sc_vq[0];
- vsc->sc_config_change = NULL;
- vsc->sc_intrhand = virtio_vq_intr;
- vsc->sc_flags = 0;
+ req_flags = 0;
#ifdef VIOIF_MPSAFE
- vsc->sc_flags |= VIRTIO_F_PCI_INTR_MPSAFE;
+ req_flags |= VIRTIO_F_PCI_INTR_MPSAFE;
#endif
#ifdef VIOIF_SOFTINT_INTR
- vsc->sc_flags |= VIRTIO_F_PCI_INTR_SOFTINT;
+ req_flags |= VIRTIO_F_PCI_INTR_SOFTINT;
#endif
- vsc->sc_flags |= VIRTIO_F_PCI_INTR_MSIX;
+ req_flags |= VIRTIO_F_PCI_INTR_MSIX;
- features = virtio_negotiate_features(vsc,
- (VIRTIO_NET_F_MAC |
- VIRTIO_NET_F_STATUS |
- VIRTIO_NET_F_CTRL_VQ |
- VIRTIO_NET_F_CTRL_RX |
- VIRTIO_F_NOTIFY_ON_EMPTY));
+ virtio_child_attach_start(vsc, self, IPL_NET, sc->sc_vq,
+ NULL, virtio_vq_intr, req_flags,
+ (VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_NET_F_CTRL_VQ |
+ VIRTIO_NET_F_CTRL_RX | VIRTIO_F_NOTIFY_ON_EMPTY),
+ VIRTIO_NET_FLAG_BITS);
+
+ features = virtio_features(vsc);
+
if (features & VIRTIO_NET_F_MAC) {
sc->sc_mac[0] = virtio_read_device_config_1(vsc,
VIRTIO_NET_CONFIG_MAC+0);
@@ -585,10 +583,8 @@
VIRTIO_NET_CONFIG_MAC+5,
sc->sc_mac[5]);
}
- aprint_normal(": Ethernet address %s\n", ether_sprintf(sc->sc_mac));
- snprintb(buf, sizeof(buf), VIRTIO_NET_FLAG_BITS, features);
- aprint_normal_dev(self, "Features: %s\n", buf);
- aprint_naive("\n");
+
+ aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(sc->sc_mac));
#ifdef VIOIF_MPSAFE
sc->sc_tx_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
@@ -602,22 +598,22 @@
/*
* Allocating a virtqueue for Rx
*/
- r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_RX], 0,
+ r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_RX], VQ_RX,
MCLBYTES+sizeof(struct virtio_net_hdr), 2, "rx");
if (r != 0)
goto err;
- vsc->sc_nvqs = 1;
+ nvqs = 1;
sc->sc_vq[VQ_RX].vq_done = vioif_rx_vq_done;
/*
* Allocating a virtqueue for Tx
*/
- r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_TX], 1,
+ r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_TX], VQ_TX,
(sizeof(struct virtio_net_hdr) + (ETHER_MAX_LEN - ETHER_HDR_LEN)),
VIRTIO_NET_TX_MAXNSEGS + 1, "tx");
if (r != 0)
goto err;
- vsc->sc_nvqs = 2;
+ nvqs = 2;
sc->sc_vq[VQ_TX].vq_done = vioif_tx_vq_done;
virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_RX]);
@@ -628,7 +624,7 @@
/*
* Allocating a virtqueue for control channel
*/
- r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_CTRL], 2,
+ r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_CTRL], VQ_CTRL,
NBPG, 1, "control");
if (r != 0) {
aprint_error_dev(self, "failed to allocate "
@@ -641,7 +637,8 @@
mutex_init(&sc->sc_ctrl_wait_lock, MUTEX_DEFAULT, IPL_NET);
sc->sc_ctrl_inuse = FREE;
virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_CTRL]);
- vsc->sc_nvqs = 3;
+ sc->sc_has_ctrl = true;
+ nvqs = 3;
}
skip:
@@ -659,6 +656,9 @@
if (vioif_alloc_mems(sc) < 0)
goto err;
+ if (virtio_child_attach_finish(vsc) != 0)
+ goto err;
+
strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
@@ -683,15 +683,15 @@
if (sc->sc_rx_lock)
mutex_obj_free(sc->sc_rx_lock);
- if (vsc->sc_nvqs == 3) {
+ if (sc->sc_has_ctrl) {
cv_destroy(&sc->sc_ctrl_wait);
mutex_destroy(&sc->sc_ctrl_wait_lock);
}
- while (vsc->sc_nvqs > 0)
- virtio_free_vq(vsc, &sc->sc_vq[--vsc->sc_nvqs]);
+ while (nvqs > 0)
+ virtio_free_vq(vsc, &sc->sc_vq[--nvqs]);
- vsc->sc_child = (void*)1;
+ virtio_child_attach_failed(vsc);
return;
}
@@ -723,10 +723,8 @@
vioif_stop(ifp, 0);
if (!sc->sc_deferred_init_done) {
- struct virtio_softc *vsc = sc->sc_virtio;
-
sc->sc_deferred_init_done = 1;
- if (vsc->sc_nvqs == 3)
+ if (sc->sc_has_ctrl)
vioif_deferred_init(sc->sc_dev);
}
@@ -766,10 +764,10 @@
vioif_rx_drain(sc);
virtio_reinit_start(vsc);
- virtio_negotiate_features(vsc, vsc->sc_features);
+ virtio_negotiate_features(vsc, virtio_features(vsc));
virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_RX]);
virtio_stop_vq_intr(vsc, &sc->sc_vq[VQ_TX]);
Home |
Main Index |
Thread Index |
Old Index