xref: /openbsd-src/sys/dev/pv/viornd.c (revision 7c0ec4b8992567abb1e1536622dc789a9a39d9f1)
1 /*	$OpenBSD: viornd.c,v 1.11 2024/08/27 18:44:12 sf Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 Stefan Fritsch <sf@sfritsch.de>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/timeout.h>
22 #include <machine/bus.h>
23 #include <sys/device.h>
24 #include <sys/pool.h>
25 #include <dev/pv/virtioreg.h>
26 #include <dev/pv/virtiovar.h>
27 
28 /*
29  * The host may not have an unlimited supply of entropy. Therefore, we must
30  * not blindly get as much entropy as we can. Instead, we just request
31  * VIORND_BUFSIZE bytes at boot and every 15 * (1 << interval_shift) seconds.
32  * XXX There should be an API to check if we actually need more entropy.
33  *
34  * The lowest byte in the flags is used for transport specific settings.
35  * Therefore we use the second byte.
36  */
37 #define	VIORND_INTERVAL_SHIFT(f)	((f >> 8) & 0xf)
38 #define	VIORND_INTERVAL_SHIFT_DEFAULT	5
39 #define	VIORND_ONESHOT			0x1000
40 #define	VIORND_BUFSIZE			16
41 
42 #define VIORND_DEBUG 0
43 
44 struct viornd_softc {
45 	struct device		 sc_dev;
46 	struct virtio_softc	*sc_virtio;
47 
48 	struct virtqueue         sc_vq;
49 	int			*sc_buf;
50 	bus_dmamap_t		 sc_dmamap;
51 
52 	unsigned int		 sc_interval;
53 	struct timeout		 sc_tick;
54 };
55 
56 int	viornd_match(struct device *, void *, void *);
57 void	viornd_attach(struct device *, struct device *, void *);
58 int	viornd_vq_done(struct virtqueue *);
59 void	viornd_tick(void *);
60 
61 const struct cfattach viornd_ca = {
62 	sizeof(struct viornd_softc),
63 	viornd_match,
64 	viornd_attach,
65 	NULL
66 };
67 
68 struct cfdriver viornd_cd = {
69 	NULL, "viornd", DV_DULL
70 };
71 
72 int
73 viornd_match(struct device *parent, void *match, void *aux)
74 {
75 	struct virtio_attach_args *va = aux;
76 	if (va->va_devid == PCI_PRODUCT_VIRTIO_ENTROPY)
77 		return 1;
78 	return 0;
79 }
80 
81 void
82 viornd_attach(struct device *parent, struct device *self, void *aux)
83 {
84 	struct viornd_softc *sc = (struct viornd_softc *)self;
85 	struct virtio_softc *vsc = (struct virtio_softc *)parent;
86 	unsigned int shift;
87 
88 	vsc->sc_vqs = &sc->sc_vq;
89 	vsc->sc_nvqs = 1;
90 	vsc->sc_config_change = NULL;
91 	if (vsc->sc_child != NULL)
92 		panic("already attached to something else");
93 	vsc->sc_child = self;
94 	vsc->sc_ipl = IPL_NET;
95 	sc->sc_virtio = vsc;
96 
97 	virtio_negotiate_features(vsc, NULL);
98 
99 	if (sc->sc_dev.dv_cfdata->cf_flags & VIORND_ONESHOT) {
100 		sc->sc_interval = 0;
101 	} else {
102 		shift = VIORND_INTERVAL_SHIFT(sc->sc_dev.dv_cfdata->cf_flags);
103 		if (shift == 0)
104 			shift = VIORND_INTERVAL_SHIFT_DEFAULT;
105 		sc->sc_interval = 15 * (1 << shift);
106 	}
107 #if VIORND_DEBUG
108 	printf(": request interval: %us\n", sc->sc_interval);
109 #endif
110 
111 	sc->sc_buf = dma_alloc(VIORND_BUFSIZE, PR_NOWAIT|PR_ZERO);
112 	if (sc->sc_buf == NULL) {
113 		printf(": Can't alloc dma buffer\n");
114 		goto err;
115 	}
116 	if (bus_dmamap_create(sc->sc_virtio->sc_dmat, VIORND_BUFSIZE, 1,
117 	    VIORND_BUFSIZE, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
118 	    &sc->sc_dmamap)) {
119 		printf(": Can't alloc dmamap\n");
120 		goto err;
121 	}
122 	if (bus_dmamap_load(sc->sc_virtio->sc_dmat, sc->sc_dmamap,
123 	    sc->sc_buf, VIORND_BUFSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_READ)) {
124 		printf(": Can't load dmamap\n");
125 		goto err2;
126 	}
127 
128 	if (virtio_alloc_vq(vsc, &sc->sc_vq, 0, 1, "Entropy request") != 0) {
129 		printf(": Can't alloc virtqueue\n");
130 		goto err2;
131 	}
132 
133 	sc->sc_vq.vq_done = viornd_vq_done;
134 	virtio_start_vq_intr(vsc, &sc->sc_vq);
135 	timeout_set(&sc->sc_tick, viornd_tick, sc);
136 	timeout_add(&sc->sc_tick, 1);
137 
138 	printf("\n");
139 	virtio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
140 	return;
141 err2:
142 	bus_dmamap_destroy(vsc->sc_dmat, sc->sc_dmamap);
143 err:
144 	vsc->sc_child = VIRTIO_CHILD_ERROR;
145 	if (sc->sc_buf != NULL) {
146 		dma_free(sc->sc_buf, VIORND_BUFSIZE);
147 		sc->sc_buf = NULL;
148 	}
149 	return;
150 }
151 
152 int
153 viornd_vq_done(struct virtqueue *vq)
154 {
155 	struct virtio_softc *vsc = vq->vq_owner;
156 	struct viornd_softc *sc = (struct viornd_softc *)vsc->sc_child;
157 	int slot, len, i;
158 
159 	if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
160 		return 0;
161 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
162 	    BUS_DMASYNC_POSTREAD);
163 	if (len > VIORND_BUFSIZE) {
164 		printf("%s: inconsistent descriptor length %d > %d\n",
165 		    sc->sc_dev.dv_xname, len, VIORND_BUFSIZE);
166 		goto out;
167 	}
168 
169 #if VIORND_DEBUG
170 	printf("%s: got %d bytes of entropy\n", __func__, len);
171 #endif
172 	for (i = 0; (i + 1) * sizeof(int) <= len; i++)
173 		enqueue_randomness(sc->sc_buf[i]);
174 
175 	if (sc->sc_interval)
176 		timeout_add_sec(&sc->sc_tick, sc->sc_interval);
177 
178 out:
179 	virtio_dequeue_commit(vq, slot);
180 	return 1;
181 }
182 
183 void
184 viornd_tick(void *arg)
185 {
186 	struct viornd_softc *sc = arg;
187 	struct virtio_softc *vsc = sc->sc_virtio;
188 	struct virtqueue *vq = &sc->sc_vq;
189 	int slot;
190 
191 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
192 	    BUS_DMASYNC_PREREAD);
193 	if (virtio_enqueue_prep(vq, &slot) != 0 ||
194 	    virtio_enqueue_reserve(vq, slot, 1) != 0) {
195 		panic("%s: virtqueue enqueue failed", sc->sc_dev.dv_xname);
196 	}
197 	virtio_enqueue(vq, slot, sc->sc_dmamap, 0);
198 	virtio_enqueue_commit(vsc, vq, slot, 1);
199 }
200