1 /* $NetBSD: virtio_pci.c,v 1.2 2018/02/15 19:05:10 uwe Exp $ */ 2 3 /* 4 * Copyright (c) 2010 Minoura Makoto. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 34 #include <sys/device.h> 35 36 #include <dev/pci/pcidevs.h> 37 #include <dev/pci/pcireg.h> 38 #include <dev/pci/pcivar.h> 39 40 #define VIRTIO_PRIVATE 41 42 #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */ 43 #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */ 44 45 static int virtio_match(device_t, cfdata_t, void *); 46 static void virtio_attach(device_t, device_t, void *); 47 static int virtio_rescan(device_t, const char *, const int *); 48 static int virtio_detach(device_t, int); 49 50 static const char *virtio_device_name[] = { 51 "Unknown (0)", /* 0 */ 52 "Network", /* 1 */ 53 "Block", /* 2 */ 54 "Console", /* 3 */ 55 "Entropy", /* 4 */ 56 "Memory Balloon", /* 5 */ 57 "I/O Memory", /* 6 */ 58 "Remote Processor Messaging", /* 7 */ 59 "SCSI", /* 8 */ 60 "9P Transport", /* 9 */ 61 "mac80211 wlan", /* 10 */ 62 }; 63 #define NDEVNAMES __arraycount(virtio_device_name) 64 65 CFATTACH_DECL3_NEW(virtio_pci, sizeof(struct virtio_softc), 66 virtio_match, virtio_attach, virtio_detach, NULL, virtio_rescan, NULL, 67 DVF_DETACH_SHUTDOWN); 68 69 static int 70 virtio_match(device_t parent, cfdata_t match, void *aux) 71 { 72 struct pci_attach_args *pa; 73 74 pa = (struct pci_attach_args *)aux; 75 switch (PCI_VENDOR(pa->pa_id)) { 76 case PCI_VENDOR_QUMRANET: 77 if ((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <= 78 PCI_PRODUCT(pa->pa_id)) && 79 (PCI_PRODUCT(pa->pa_id) <= 80 PCI_PRODUCT_QUMRANET_VIRTIO_103F)) 81 return 1; 82 break; 83 } 84 85 return 0; 86 } 87 88 static void 89 virtio_attach(device_t parent, device_t self, void *aux) 90 { 91 struct virtio_softc *sc = device_private(self); 92 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 93 pci_chipset_tag_t pc = pa->pa_pc; 94 pcitag_t tag = pa->pa_tag; 95 int revision; 96 pcireg_t id; 97 pcireg_t csr; 98 99 revision = PCI_REVISION(pa->pa_class); 100 if (revision != 0) { 101 aprint_normal(": unknown revision 0x%02x; giving up\n", 102 revision); 103 return; 104 } 105 aprint_normal("\n"); 106 aprint_naive("\n"); 107 108 /* subsystem ID shows what I am */ 109 id = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG); 110 aprint_normal_dev(self, "Virtio %s Device (rev. 0x%02x)\n", 111 (PCI_SUBSYS_ID(id) < NDEVNAMES? 112 virtio_device_name[PCI_SUBSYS_ID(id)] : "Unknown"), 113 revision); 114 115 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); 116 csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE; 117 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr); 118 119 sc->sc_dev = self; 120 sc->sc_pc = pc; 121 sc->sc_tag = tag; 122 sc->sc_iot = pa->pa_iot; 123 if (pci_dma64_available(pa)) 124 sc->sc_dmat = pa->pa_dmat64; 125 else 126 sc->sc_dmat = pa->pa_dmat; 127 sc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI; 128 129 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0, 130 &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_iosize)) { 131 aprint_error_dev(self, "can't map i/o space\n"); 132 return; 133 } 134 135 virtio_device_reset(sc); 136 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK); 137 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER); 138 139 sc->sc_childdevid = PCI_SUBSYS_ID(id); 140 sc->sc_child = NULL; 141 sc->sc_pa = *pa; 142 virtio_rescan(self, "virtio", 0); 143 return; 144 } 145 146 /* ARGSUSED */ 147 static int 148 virtio_rescan(device_t self, const char *attr, const int *scan_flags) 149 { 150 struct virtio_softc *sc; 151 struct virtio_attach_args va; 152 153 sc = device_private(self); 154 if (sc->sc_child) /* Child already attached? */ 155 return 0; 156 157 memset(&va, 0, sizeof(va)); 158 va.sc_childdevid = sc->sc_childdevid; 159 160 config_found_ia(self, attr, &va, NULL); 161 162 if (sc->sc_child == NULL) { 163 aprint_error_dev(self, 164 "no matching child driver; not configured\n"); 165 return 0; 166 } 167 168 if (sc->sc_child == VIRTIO_CHILD_FAILED) { 169 aprint_error_dev(self, 170 "virtio configuration failed\n"); 171 return 0; 172 } 173 174 /* 175 * Make sure child drivers initialize interrupts via call 176 * to virtio_child_attach_finish(). 177 */ 178 KASSERT(sc->sc_ihs_num != 0); 179 180 return 0; 181 } 182 183 184 static int 185 virtio_detach(device_t self, int flags) 186 { 187 struct virtio_softc *sc = device_private(self); 188 int r; 189 190 if (sc->sc_child != NULL) { 191 r = config_detach(sc->sc_child, flags); 192 if (r) 193 return r; 194 } 195 196 /* Check that child detached properly */ 197 KASSERT(sc->sc_child == NULL); 198 KASSERT(sc->sc_vqs == NULL); 199 KASSERT(sc->sc_ihs_num == 0); 200 201 if (sc->sc_iosize) 202 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize); 203 sc->sc_iosize = 0; 204 205 return 0; 206 } 207