xref: /openbsd/sys/dev/pv/viornd.c (revision 77d0f823)
1 /*	$OpenBSD: viornd.c,v 1.12 2024/12/20 22:18:27 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
viornd_match(struct device * parent,void * match,void * aux)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
viornd_attach(struct device * parent,struct device * self,void * aux)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 	struct virtio_attach_args *va = aux;
87 	unsigned int shift;
88 
89 	vsc->sc_vqs = &sc->sc_vq;
90 	vsc->sc_nvqs = 1;
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 	if (virtio_negotiate_features(vsc, NULL) != 0)
98 		goto err;
99 
100 	if (sc->sc_dev.dv_cfdata->cf_flags & VIORND_ONESHOT) {
101 		sc->sc_interval = 0;
102 	} else {
103 		shift = VIORND_INTERVAL_SHIFT(sc->sc_dev.dv_cfdata->cf_flags);
104 		if (shift == 0)
105 			shift = VIORND_INTERVAL_SHIFT_DEFAULT;
106 		sc->sc_interval = 15 * (1 << shift);
107 	}
108 #if VIORND_DEBUG
109 	printf(": request interval: %us\n", sc->sc_interval);
110 #endif
111 
112 	sc->sc_buf = dma_alloc(VIORND_BUFSIZE, PR_NOWAIT|PR_ZERO);
113 	if (sc->sc_buf == NULL) {
114 		printf(": Can't alloc dma buffer\n");
115 		goto err;
116 	}
117 	if (bus_dmamap_create(sc->sc_virtio->sc_dmat, VIORND_BUFSIZE, 1,
118 	    VIORND_BUFSIZE, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
119 	    &sc->sc_dmamap)) {
120 		printf(": Can't alloc dmamap\n");
121 		goto err;
122 	}
123 	if (bus_dmamap_load(sc->sc_virtio->sc_dmat, sc->sc_dmamap,
124 	    sc->sc_buf, VIORND_BUFSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_READ)) {
125 		printf(": Can't load dmamap\n");
126 		goto err2;
127 	}
128 
129 	if (virtio_alloc_vq(vsc, &sc->sc_vq, 0, 1, "Entropy request") != 0) {
130 		printf(": Can't alloc virtqueue\n");
131 		goto err2;
132 	}
133 
134 	sc->sc_vq.vq_done = viornd_vq_done;
135 	virtio_start_vq_intr(vsc, &sc->sc_vq);
136 	timeout_set(&sc->sc_tick, viornd_tick, sc);
137 	timeout_add(&sc->sc_tick, 1);
138 
139 	printf("\n");
140 	if (virtio_attach_finish(vsc, va) != 0)
141 		goto err2;
142 	return;
143 err2:
144 	bus_dmamap_destroy(vsc->sc_dmat, sc->sc_dmamap);
145 err:
146 	vsc->sc_child = VIRTIO_CHILD_ERROR;
147 	if (sc->sc_buf != NULL) {
148 		dma_free(sc->sc_buf, VIORND_BUFSIZE);
149 		sc->sc_buf = NULL;
150 	}
151 	return;
152 }
153 
154 int
viornd_vq_done(struct virtqueue * vq)155 viornd_vq_done(struct virtqueue *vq)
156 {
157 	struct virtio_softc *vsc = vq->vq_owner;
158 	struct viornd_softc *sc = (struct viornd_softc *)vsc->sc_child;
159 	int slot, len, i;
160 
161 	if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
162 		return 0;
163 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
164 	    BUS_DMASYNC_POSTREAD);
165 	if (len > VIORND_BUFSIZE) {
166 		printf("%s: inconsistent descriptor length %d > %d\n",
167 		    sc->sc_dev.dv_xname, len, VIORND_BUFSIZE);
168 		goto out;
169 	}
170 
171 #if VIORND_DEBUG
172 	printf("%s: got %d bytes of entropy\n", __func__, len);
173 #endif
174 	for (i = 0; (i + 1) * sizeof(int) <= len; i++)
175 		enqueue_randomness(sc->sc_buf[i]);
176 
177 	if (sc->sc_interval)
178 		timeout_add_sec(&sc->sc_tick, sc->sc_interval);
179 
180 out:
181 	virtio_dequeue_commit(vq, slot);
182 	return 1;
183 }
184 
185 void
viornd_tick(void * arg)186 viornd_tick(void *arg)
187 {
188 	struct viornd_softc *sc = arg;
189 	struct virtio_softc *vsc = sc->sc_virtio;
190 	struct virtqueue *vq = &sc->sc_vq;
191 	int slot;
192 
193 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
194 	    BUS_DMASYNC_PREREAD);
195 	if (virtio_enqueue_prep(vq, &slot) != 0 ||
196 	    virtio_enqueue_reserve(vq, slot, 1) != 0) {
197 		panic("%s: virtqueue enqueue failed", sc->sc_dev.dv_xname);
198 	}
199 	virtio_enqueue(vq, slot, sc->sc_dmamap, 0);
200 	virtio_enqueue_commit(vsc, vq, slot, 1);
201 }
202