virtio_pci.c revision 1.1 1 /* $NetBSD: virtio_pci.c,v 1.1 2017/08/02 08:39:14 cherry 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
98 revision = PCI_REVISION(pa->pa_class);
99 if (revision != 0) {
100 aprint_normal(": unknown revision 0x%02x; giving up\n",
101 revision);
102 return;
103 }
104 aprint_normal("\n");
105 aprint_naive("\n");
106
107 /* subsystem ID shows what I am */
108 id = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
109 aprint_normal_dev(self, "Virtio %s Device (rev. 0x%02x)\n",
110 (PCI_SUBSYS_ID(id) < NDEVNAMES?
111 virtio_device_name[PCI_SUBSYS_ID(id)] : "Unknown"),
112 revision);
113
114 sc->sc_dev = self;
115 sc->sc_pc = pc;
116 sc->sc_tag = tag;
117 sc->sc_iot = pa->pa_iot;
118 if (pci_dma64_available(pa))
119 sc->sc_dmat = pa->pa_dmat64;
120 else
121 sc->sc_dmat = pa->pa_dmat;
122 sc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
123
124 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
125 &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_iosize)) {
126 aprint_error_dev(self, "can't map i/o space\n");
127 return;
128 }
129
130 virtio_device_reset(sc);
131 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
132 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
133
134 sc->sc_childdevid = PCI_SUBSYS_ID(id);
135 sc->sc_child = NULL;
136 sc->sc_pa = *pa;
137 virtio_rescan(self, "virtio", 0);
138 return;
139 }
140
141 /* ARGSUSED */
142 static int
143 virtio_rescan(device_t self, const char *attr, const int *scan_flags)
144 {
145 struct virtio_softc *sc;
146 struct virtio_attach_args va;
147
148 sc = device_private(self);
149 if (sc->sc_child) /* Child already attached? */
150 return 0;
151
152 memset(&va, 0, sizeof(va));
153 va.sc_childdevid = sc->sc_childdevid;
154
155 config_found_ia(self, attr, &va, NULL);
156
157 if (sc->sc_child == NULL) {
158 aprint_error_dev(self,
159 "no matching child driver; not configured\n");
160 return 0;
161 }
162
163 if (sc->sc_child == VIRTIO_CHILD_FAILED) {
164 aprint_error_dev(self,
165 "virtio configuration failed\n");
166 return 0;
167 }
168
169 /*
170 * Make sure child drivers initialize interrupts via call
171 * to virtio_child_attach_finish().
172 */
173 KASSERT(sc->sc_ihs_num != 0);
174
175 return 0;
176 }
177
178
179 static int
180 virtio_detach(device_t self, int flags)
181 {
182 struct virtio_softc *sc = device_private(self);
183 int r;
184
185 if (sc->sc_child != NULL) {
186 r = config_detach(sc->sc_child, flags);
187 if (r)
188 return r;
189 }
190
191 /* Check that child detached properly */
192 KASSERT(sc->sc_child == NULL);
193 KASSERT(sc->sc_vqs == NULL);
194 KASSERT(sc->sc_ihs_num == 0);
195
196 if (sc->sc_iosize)
197 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
198 sc->sc_iosize = 0;
199
200 return 0;
201 }
202