Module Name: src Committed By: bouyer Date: Thu Sep 1 15:33:23 UTC 2022
Modified Files: src/sys/arch/xen/xen: pciback.c xbdback_xenbus.c xennetback_xenbus.c Log Message: in backend drivers, use xen_shm_(un)map for the rings instead of inline, mostly duplicate code. To generate a diff of this commit: cvs rdiff -u -r1.22 -r1.23 src/sys/arch/xen/xen/pciback.c cvs rdiff -u -r1.100 -r1.101 src/sys/arch/xen/xen/xbdback_xenbus.c cvs rdiff -u -r1.106 -r1.107 src/sys/arch/xen/xen/xennetback_xenbus.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/arch/xen/xen/pciback.c diff -u src/sys/arch/xen/xen/pciback.c:1.22 src/sys/arch/xen/xen/pciback.c:1.23 --- src/sys/arch/xen/xen/pciback.c:1.22 Thu Sep 1 12:29:00 2022 +++ src/sys/arch/xen/xen/pciback.c Thu Sep 1 15:33:23 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: pciback.c,v 1.22 2022/09/01 12:29:00 bouyer Exp $ */ +/* $NetBSD: pciback.c,v 1.23 2022/09/01 15:33:23 bouyer Exp $ */ /* * Copyright (c) 2009 Manuel Bouyer. @@ -26,10 +26,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: pciback.c,v 1.22 2022/09/01 12:29:00 bouyer Exp $"); - -#include "opt_xen.h" - +__KERNEL_RCSID(0, "$NetBSD: pciback.c,v 1.23 2022/09/01 15:33:23 bouyer Exp $"); #include <sys/types.h> #include <sys/param.h> @@ -48,7 +45,7 @@ __KERNEL_RCSID(0, "$NetBSD: pciback.c,v #include <xen/hypervisor.h> #include <xen/evtchn.h> #include <xen/granttables.h> -#include <xen/xenmem.h> +#include <xen/xen_shm.h> #include <xen/include/public/io/pciif.h> #include <xen/xenbus.h> @@ -189,7 +186,6 @@ struct pb_xenbus_instance { /* communication with the domU */ unsigned int pbx_evtchn; /* our even channel */ struct intrhand *pbx_ih; - paddr_t *pbx_sh_info_pa; struct xen_pci_sharedinfo *pbx_sh_info; struct xen_pci_op op; grant_handle_t pbx_shinfo_handle; /* to unmap shared page */ @@ -521,8 +517,6 @@ pciback_xenbus_destroy(void *arg) { struct pb_xenbus_instance *pbxi = arg; struct pciback_pci_dev *pbd; - struct gnttab_unmap_grant_ref op; - int err; hypervisor_mask_event(pbxi->pbx_evtchn); xen_intr_disestablish(pbxi->pbx_ih); @@ -533,30 +527,14 @@ pciback_xenbus_destroy(void *arg) if (pbxi->pbx_sh_info) { -#ifndef XENPV - op.host_addr = pbxi->pbx_sh_info_pa; -#else - op.host_addr = (vaddr_t)pbxi->pbx_sh_info; -#endif - op.handle = pbxi->pbx_shinfo_handle; - op.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &op, 1); - if (err) - aprint_error("pciback: unmap_grant_ref failed: %d\n", - err); - } -#ifndef XENPV - if (pbxi->pbx_sh_info_pa) { - pmap_kremove((vaddr_t)pbxi->pbx_sh_info, PAGE_SIZE); - xenmem_free_pa(pbxi->pbx_sh_info_pa, PAGE_SIZE); + xen_shm_unmap((vaddr_t)pbxi->pbx_sh_info, 1, + &pbxi->pbx_shinfo_handle); + uvm_km_free(kernel_map, (vaddr_t)pbxi->pbx_sh_info, + PAGE_SIZE, UVM_KMF_VAONLY); } -#endif SLIST_FOREACH(pbd, &pbxi->pbx_pb_pci_dev, pb_guest_next) { pbd->pbx_instance = NULL; } - uvm_km_free(kernel_map, (vaddr_t)pbxi->pbx_sh_info, - PAGE_SIZE, UVM_KMF_VAONLY); kmem_free(pbxi, sizeof(*pbxi)); return 0; } @@ -567,9 +545,9 @@ pciback_xenbus_frontend_changed(void *ar struct pb_xenbus_instance *pbxi = arg; struct xenbus_device *xbusd = pbxi->pbx_xbusd; int err; - struct gnttab_map_grant_ref op; evtchn_op_t evop; u_long shared_ref; + grant_ref_t gshared_ref; u_long revtchn; /* do it only once */ @@ -606,30 +584,13 @@ pciback_xenbus_frontend_changed(void *ar xbusd->xbusd_otherend); break; } -#ifndef XENPV - pbxi->pbx_sh_info_pa = - xenmem_alloc_pa(PAGE_SIZE, PAGE_SIZE, false); - if (pbxi->pbx_sh_info_pa == 0) { - xenbus_dev_fatal(xbusd, ENOMEM, - "can't get PA for ring", xbusd->xbusd_otherend); + gshared_ref = shared_ref; + if (xen_shm_map(1, pbxi->pbx_domid, &gshared_ref, + (vaddr_t)pbxi->pbx_sh_info, + &pbxi->pbx_shinfo_handle, 0) != 0) { + aprint_error("pciback: can't map shared grant ref\n"); goto err2; } - pmap_kenter_pa((vaddr_t)pbxi->pbx_sh_info, pbxi->pbx_sh_info_pa, - VM_PROT_READ | VM_PROT_WRITE, 0); - op.host_addr = pbxi->pbx_sh_info_pa; -#else - op.host_addr = (vaddr_t)pbxi->pbx_sh_info; -#endif - op.flags = GNTMAP_host_map; - op.ref = shared_ref; - op.dom = pbxi->pbx_domid; - err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1); - if (err || op.status) { - aprint_error("pciback: can't map shared grant ref: " - "%d/%d\n", err, op.status); - goto err1; - } - pbxi->pbx_shinfo_handle = op.handle; evop.cmd = EVTCHNOP_bind_interdomain; evop.u.bind_interdomain.remote_dom = pbxi->pbx_domid; @@ -666,11 +627,8 @@ pciback_xenbus_frontend_changed(void *ar } return; err1: -#ifndef XENPV - pmap_kremove((vaddr_t)pbxi->pbx_sh_info, PAGE_SIZE); - xenmem_free_pa(pbxi->pbx_sh_info_pa, PAGE_SIZE); + xen_shm_unmap((vaddr_t)pbxi->pbx_sh_info, 1, &pbxi->pbx_shinfo_handle); err2: -#endif uvm_km_free(kernel_map, (vaddr_t)pbxi->pbx_sh_info, PAGE_SIZE, UVM_KMF_VAONLY); } Index: src/sys/arch/xen/xen/xbdback_xenbus.c diff -u src/sys/arch/xen/xen/xbdback_xenbus.c:1.100 src/sys/arch/xen/xen/xbdback_xenbus.c:1.101 --- src/sys/arch/xen/xen/xbdback_xenbus.c:1.100 Thu Sep 1 12:29:00 2022 +++ src/sys/arch/xen/xen/xbdback_xenbus.c Thu Sep 1 15:33:23 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: xbdback_xenbus.c,v 1.100 2022/09/01 12:29:00 bouyer Exp $ */ +/* $NetBSD: xbdback_xenbus.c,v 1.101 2022/09/01 15:33:23 bouyer Exp $ */ /* * Copyright (c) 2006 Manuel Bouyer. @@ -26,9 +26,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: xbdback_xenbus.c,v 1.100 2022/09/01 12:29:00 bouyer Exp $"); - -#include "opt_xen.h" +__KERNEL_RCSID(0, "$NetBSD: xbdback_xenbus.c,v 1.101 2022/09/01 15:33:23 bouyer Exp $"); #include <sys/buf.h> #include <sys/condvar.h> @@ -51,7 +49,6 @@ __KERNEL_RCSID(0, "$NetBSD: xbdback_xenb #include <xen/intr.h> #include <xen/hypervisor.h> #include <xen/xen.h> -#include <xen/xenmem.h> #include <xen/xen_shm.h> #include <xen/evtchn.h> #include <xen/xenbus.h> @@ -211,7 +208,6 @@ struct xbdback_instance { enum xbdi_proto xbdi_proto; grant_handle_t xbdi_ring_handle; /* to unmap the ring */ vaddr_t xbdi_ring_va; /* to unmap the ring */ - paddr_t xbdi_ring_pa; /* to unmap the ring */ /* disconnection must be postponed until all I/O is done */ int xbdi_refcnt; /* @@ -426,9 +422,6 @@ static int xbdback_xenbus_destroy(void *arg) { struct xbdback_instance *xbdi = arg; - struct xenbus_device *xbusd = xbdi->xbdi_xbusd; - struct gnttab_unmap_grant_ref ungrop; - int err; XENPRINTF(("xbdback_xenbus_destroy state %d\n", xbdi->xbdi_status)); @@ -438,27 +431,8 @@ xbdback_xenbus_destroy(void *arg) if (xbdi->xbdi_watch.node) xenbus_unwatch_path(&xbdi->xbdi_watch); /* unmap ring */ -#ifndef XENPV - ungrop.host_addr = xbdi->xbdi_ring_pa; - if (xbdi->xbdi_ring_pa != 0) { - KASSERT(xbdi->xbdi_ring_va != 0); - pmap_kremove(xbdi->xbdi_ring_va, PAGE_SIZE); - } -#else - ungrop.host_addr = xbdi->xbdi_ring_va; -#endif - - if (ungrop.host_addr != 0) { - ungrop.handle = xbdi->xbdi_ring_handle; - ungrop.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &ungrop, 1); - if (err) - printf("xbdback %s: unmap_grant_ref failed: %d\n", - xbusd->xbusd_otherend, err); -#ifndef XENPV - xenmem_free_pa(xbdi->xbdi_ring_pa, PAGE_SIZE); -#endif + if (xbdi->xbdi_ring_handle) { + xen_shm_unmap(xbdi->xbdi_ring_va, 1, &xbdi->xbdi_ring_handle); } if (xbdi->xbdi_ring_va != 0) { @@ -503,9 +477,8 @@ static int xbdback_connect(struct xbdback_instance *xbdi) { int err; - struct gnttab_map_grant_ref grop; - struct gnttab_unmap_grant_ref ungrop; evtchn_op_t evop; + grant_ref_t gring_ref; u_long ring_ref, revtchn; char xsproto[32]; const char *proto; @@ -562,33 +535,17 @@ xbdback_connect(struct xbdback_instance return -1; } XENPRINTF(("xbdback %s: connect va 0x%" PRIxVADDR "\n", xbusd->xbusd_path, xbdi->xbdi_ring_va)); -#ifndef XENPV - xbdi->xbdi_ring_pa = xenmem_alloc_pa(PAGE_SIZE, PAGE_SIZE, false); - if (xbdi->xbdi_ring_pa == 0) { - xenbus_dev_fatal(xbusd, ENOMEM, - "can't get PA for ring", xbusd->xbusd_otherend); - goto err; - } - pmap_kenter_pa(xbdi->xbdi_ring_va, xbdi->xbdi_ring_pa, - VM_PROT_READ | VM_PROT_WRITE, 0); - grop.host_addr = xbdi->xbdi_ring_pa; -#else - grop.host_addr = xbdi->xbdi_ring_va; -#endif - grop.flags = GNTMAP_host_map; - grop.ref = ring_ref; - grop.dom = xbdi->xbdi_domid; - err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, - &grop, 1); - if (err || grop.status) { - aprint_error("xbdback %s: can't map grant ref: %d/%d\n", - xbusd->xbusd_path, err, grop.status); + + gring_ref = ring_ref; + if (xen_shm_map(1, xbdi->xbdi_domid, &gring_ref, xbdi->xbdi_ring_va, + &xbdi->xbdi_ring_handle, 0) != 0) { + aprint_error("xbdback %s: can't map grant ref\n", + xbusd->xbusd_path); xenbus_dev_fatal(xbusd, EINVAL, "can't map ring", xbusd->xbusd_otherend); goto err1; } - xbdi->xbdi_ring_handle = grop.handle; - XENPRINTF(("xbdback %s: connect grhandle %d\n", xbusd->xbusd_path, grop.handle)); + XENPRINTF(("xbdback %s: connect grhandle %d\n", xbusd->xbusd_path, xbdi->xbdi_ring_handle)); switch(xbdi->xbdi_proto) { case XBDIP_NATIVE: @@ -645,25 +602,8 @@ xbdback_connect(struct xbdback_instance err2: /* unmap ring */ -#ifndef XENPV - ungrop.host_addr = xbdi->xbdi_ring_pa; -#else - ungrop.host_addr = xbdi->xbdi_ring_va; -#endif - ungrop.handle = xbdi->xbdi_ring_handle; - ungrop.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &ungrop, 1); - if (err) - aprint_error("xbdback %s: unmap_grant_ref failed: %d\n", - xbusd->xbusd_path, err); - + xen_shm_unmap(xbdi->xbdi_ring_va, 1, &xbdi->xbdi_ring_handle); err1: -#ifndef XENPV - pmap_kremove(xbdi->xbdi_ring_va, PAGE_SIZE); - xenmem_free_pa(xbdi->xbdi_ring_pa, PAGE_SIZE); -err: -#endif /* free ring VA space */ uvm_km_free(kernel_map, xbdi->xbdi_ring_va, PAGE_SIZE, UVM_KMF_VAONLY); return -1; Index: src/sys/arch/xen/xen/xennetback_xenbus.c diff -u src/sys/arch/xen/xen/xennetback_xenbus.c:1.106 src/sys/arch/xen/xen/xennetback_xenbus.c:1.107 --- src/sys/arch/xen/xen/xennetback_xenbus.c:1.106 Thu Sep 1 12:29:00 2022 +++ src/sys/arch/xen/xen/xennetback_xenbus.c Thu Sep 1 15:33:23 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: xennetback_xenbus.c,v 1.106 2022/09/01 12:29:00 bouyer Exp $ */ +/* $NetBSD: xennetback_xenbus.c,v 1.107 2022/09/01 15:33:23 bouyer Exp $ */ /* * Copyright (c) 2006 Manuel Bouyer. @@ -25,9 +25,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: xennetback_xenbus.c,v 1.106 2022/09/01 12:29:00 bouyer Exp $"); - -#include "opt_xen.h" +__KERNEL_RCSID(0, "$NetBSD: xennetback_xenbus.c,v 1.107 2022/09/01 15:33:23 bouyer Exp $"); #include <sys/types.h> #include <sys/param.h> @@ -58,7 +56,6 @@ __KERNEL_RCSID(0, "$NetBSD: xennetback_x #include <xen/evtchn.h> #include <xen/xenbus.h> #include <xen/xennet_checksum.h> -#include <xen/xenmem.h> #include <uvm/uvm.h> @@ -120,9 +117,7 @@ struct xnetback_instance { netif_rx_back_ring_t xni_rxring; grant_handle_t xni_tx_ring_handle; /* to unmap the ring */ grant_handle_t xni_rx_ring_handle; - paddr_t xni_tx_ring_pa; /* to unmap the ring */ vaddr_t xni_tx_ring_va; /* to unmap the ring */ - paddr_t xni_rx_ring_pa; vaddr_t xni_rx_ring_va; /* arrays used in xennetback_ifstart(), used for both Rx and Tx */ @@ -358,8 +353,6 @@ int xennetback_xenbus_destroy(void *arg) { struct xnetback_instance *xneti = arg; - struct gnttab_unmap_grant_ref op; - int err; aprint_verbose_ifnet(&xneti->xni_if, "disconnecting\n"); @@ -390,45 +383,13 @@ xennetback_xenbus_destroy(void *arg) } if (xneti->xni_txring.sring) { -#ifndef XENPV - op.host_addr = xneti->xni_tx_ring_pa; -#else - op.host_addr = xneti->xni_tx_ring_va; -#endif - op.handle = xneti->xni_tx_ring_handle; - op.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &op, 1); - if (err) - aprint_error_ifnet(&xneti->xni_if, - "unmap_grant_ref failed: %d\n", err); + xen_shm_unmap(xneti->xni_tx_ring_va, 1, + &xneti->xni_tx_ring_handle); } if (xneti->xni_rxring.sring) { -#ifndef XENPV - op.host_addr = xneti->xni_rx_ring_pa; -#else - op.host_addr = xneti->xni_rx_ring_va; -#endif - op.handle = xneti->xni_rx_ring_handle; - op.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &op, 1); - if (err) - aprint_error_ifnet(&xneti->xni_if, - "unmap_grant_ref failed: %d\n", err); - } -#ifndef XENPV - if (xneti->xni_rx_ring_pa != 0) { - pmap_kremove(xneti->xni_rx_ring_va, PAGE_SIZE); - xenmem_free_pa(xneti->xni_rx_ring_pa, PAGE_SIZE); - xneti->xni_rx_ring_pa = 0; - } - if (xneti->xni_tx_ring_pa != 0) { - pmap_kremove(xneti->xni_tx_ring_va, PAGE_SIZE); - xenmem_free_pa(xneti->xni_tx_ring_pa, PAGE_SIZE); - xneti->xni_tx_ring_pa = 0; + xen_shm_unmap(xneti->xni_rx_ring_va, 1, + &xneti->xni_rx_ring_handle); } -#endif if (xneti->xni_tx_ring_va != 0) { uvm_km_free(kernel_map, xneti->xni_tx_ring_va, PAGE_SIZE, UVM_KMF_VAONLY); @@ -449,10 +410,9 @@ xennetback_connect(struct xnetback_insta int err; netif_tx_sring_t *tx_ring; netif_rx_sring_t *rx_ring; - struct gnttab_map_grant_ref op; - struct gnttab_unmap_grant_ref uop; evtchn_op_t evop; u_long tx_ring_ref, rx_ring_ref; + grant_ref_t gtx_ring_ref, grx_ring_ref; u_long revtchn, rx_copy; struct xenbus_device *xbusd = xneti->xni_xbusd; @@ -510,56 +470,22 @@ xennetback_connect(struct xnetback_insta } rx_ring = (void *)xneti->xni_rx_ring_va; -#ifndef XENPV - xneti->xni_tx_ring_pa = xenmem_alloc_pa(PAGE_SIZE, PAGE_SIZE, false); - if (xneti->xni_tx_ring_pa == 0) { - xenbus_dev_fatal(xbusd, ENOMEM, - "can't get PA for TX ring", xbusd->xbusd_otherend); - goto err2; - } - op.host_addr = xneti->xni_tx_ring_pa; - pmap_kenter_pa(xneti->xni_tx_ring_va, xneti->xni_tx_ring_pa, - VM_PROT_READ | VM_PROT_WRITE, 0); -#else - op.host_addr = xneti->xni_tx_ring_va; -#endif - op.flags = GNTMAP_host_map; - op.ref = tx_ring_ref; - op.dom = xneti->xni_domid; - err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1); - if (err || op.status) { + gtx_ring_ref = tx_ring_ref; + if (xen_shm_map(1, xneti->xni_domid, >x_ring_ref, + xneti->xni_tx_ring_va, &xneti->xni_tx_ring_handle, 0) != 0) { aprint_error_ifnet(&xneti->xni_if, - "can't map TX grant ref: err %d status %d\n", - err, op.status); + "can't map TX grant ref\n"); goto err2; } - xneti->xni_tx_ring_handle = op.handle; BACK_RING_INIT(&xneti->xni_txring, tx_ring, PAGE_SIZE); -#ifndef XENPV - xneti->xni_rx_ring_pa = xenmem_alloc_pa(PAGE_SIZE, PAGE_SIZE, false); - if (xneti->xni_rx_ring_pa == 0) { - xenbus_dev_fatal(xbusd, ENOMEM, - "can't get PA for RX ring", xbusd->xbusd_otherend); - goto err2; - } - op.host_addr = xneti->xni_rx_ring_pa; - pmap_kenter_pa(xneti->xni_rx_ring_va, xneti->xni_rx_ring_pa, - VM_PROT_READ | VM_PROT_WRITE, 0); -#else - op.host_addr = xneti->xni_rx_ring_va; -#endif - op.flags = GNTMAP_host_map; - op.ref = rx_ring_ref; - op.dom = xneti->xni_domid; - err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1); - if (err || op.status) { + grx_ring_ref = rx_ring_ref; + if (xen_shm_map(1, xneti->xni_domid, &grx_ring_ref, + xneti->xni_rx_ring_va, &xneti->xni_rx_ring_handle, 0) != 0) { aprint_error_ifnet(&xneti->xni_if, - "can't map RX grant ref: err %d status %d\n", - err, op.status); + "can't map RX grant ref\n"); goto err2; } - xneti->xni_rx_ring_handle = op.handle; BACK_RING_INIT(&xneti->xni_rxring, rx_ring, PAGE_SIZE); evop.cmd = EVTCHNOP_bind_interdomain; @@ -588,44 +514,14 @@ xennetback_connect(struct xnetback_insta err2: /* unmap rings */ if (xneti->xni_tx_ring_handle != 0) { -#ifndef XENPV - uop.host_addr = xneti->xni_tx_ring_pa; -#else - uop.host_addr = xneti->xni_tx_ring_va; -#endif - uop.handle = xneti->xni_tx_ring_handle; - uop.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &uop, 1); - if (err) - aprint_error_ifnet(&xneti->xni_if, - "unmap_grant_ref failed: %d\n", err); + xen_shm_unmap(xneti->xni_tx_ring_va, 1, + &xneti->xni_tx_ring_handle); } if (xneti->xni_rx_ring_handle != 0) { -#ifndef XENPV - uop.host_addr = xneti->xni_rx_ring_pa; -#else - uop.host_addr = xneti->xni_rx_ring_va; -#endif - uop.handle = xneti->xni_rx_ring_handle; - uop.dev_bus_addr = 0; - err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, - &uop, 1); - if (err) - aprint_error_ifnet(&xneti->xni_if, - "unmap_grant_ref failed: %d\n", err); - } -#ifndef XENPV - if (xneti->xni_rx_ring_pa != 0) { - pmap_kremove(xneti->xni_rx_ring_va, PAGE_SIZE); - xenmem_free_pa(xneti->xni_rx_ring_pa, PAGE_SIZE); - } - if (xneti->xni_tx_ring_pa != 0) { - pmap_kremove(xneti->xni_tx_ring_va, PAGE_SIZE); - xenmem_free_pa(xneti->xni_tx_ring_pa, PAGE_SIZE); + xen_shm_unmap(xneti->xni_rx_ring_va, 1, + &xneti->xni_rx_ring_handle); } -#endif err1: /* free rings VA space */ if (xneti->xni_rx_ring_va != 0)