xref: /netbsd/sys/dev/pci/virtiovar.h (revision c78e0dcf)
1 /*	$NetBSD: virtiovar.h,v 1.29 2023/04/19 00:23:45 yamaguchi 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 /*
29  * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT'
30  * Appendix A.
31  */
32 /* An interface for efficient virtio implementation.
33  *
34  * This header is BSD licensed so anyone can use the definitions
35  * to implement compatible drivers/servers.
36  *
37  * Copyright 2007, 2009, IBM Corporation
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of IBM nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63 
64 
65 #ifndef _DEV_PCI_VIRTIOVAR_H_
66 #define	_DEV_PCI_VIRTIOVAR_H_
67 
68 #include <sys/types.h>
69 #include <sys/queue.h>
70 #include <sys/bus.h>
71 #include <dev/pci/virtioreg.h>
72 
73 struct virtqueue {
74 	struct virtio_softc	*vq_owner;
75         unsigned int		vq_num; /* queue size (# of entries) */
76 	int			vq_index; /* queue number (0, 1, ...) */
77 
78 	/* vring pointers (KVA) */
79         struct vring_desc	*vq_desc;
80         struct vring_avail	*vq_avail;
81         struct vring_used	*vq_used;
82 	struct vring_desc	*vq_indirect;
83 	uint16_t		*vq_used_event;		/* trails avail */
84 	uint16_t		*vq_avail_event;	/* trails used  */
85 
86 	/* virtqueue allocation info */
87 	void			*vq_vaddr;
88 	int			vq_availoffset;
89 	int			vq_usedoffset;
90 	int			vq_indirectoffset;
91 	bus_dma_segment_t	vq_segs[1];
92 	unsigned int		vq_bytesize;
93 	bus_dmamap_t		vq_dmamap;
94 
95 	int			vq_maxsegsize;
96 	int			vq_maxnsegs;
97 
98 	/* enqueue/dequeue status */
99 	uint16_t		vq_avail_idx;
100 	uint16_t		vq_used_idx;
101 	uint16_t		vq_free_idx;
102 	int			vq_queued;
103 	kmutex_t		vq_aring_lock;
104 	kmutex_t		vq_uring_lock;
105 	kmutex_t		vq_freedesc_lock;
106 
107 	/* interrupt handler */
108 	int			(*vq_done)(struct virtqueue*); /* for compatibility */
109 	int			(*vq_intrhand)(void *);
110 	void			*vq_intrhand_arg;
111 
112 	/* for 1.0 */
113 	uint32_t		vq_notify_off;
114 
115 	struct vring_desc_extra {
116 		bool		use_indirect; /* true if using indirect */
117 		struct vring_desc
118 				*desc_base;
119 		uint16_t	 desc_free_idx;
120 	}			*vq_descx;
121 };
122 
123 struct virtio_attach_args {
124 	int			sc_childdevid;
125 };
126 
127 typedef int (*virtio_callback)(struct virtio_softc*);
128 
129 #ifdef VIRTIO_PRIVATE
130 struct virtio_ops {
131 	void		(*kick)(struct virtio_softc *, uint16_t);
132 	uint16_t	(*read_queue_size)(struct virtio_softc *, uint16_t);
133 	void		(*setup_queue)(struct virtio_softc *, uint16_t, uint64_t);
134 	void		(*set_status)(struct virtio_softc *, int);
135 	void		(*neg_features)(struct virtio_softc *, uint64_t);
136 	int		(*alloc_interrupts)(struct virtio_softc *);
137 	void		(*free_interrupts)(struct virtio_softc *);
138 	int		(*setup_interrupts)(struct virtio_softc *, int);
139 };
140 
141 struct virtio_softc {
142 	device_t		sc_dev;
143 	const struct virtio_ops *sc_ops;
144 	bus_dma_tag_t		sc_dmat;
145 
146 	int			sc_bus_endian;
147 	int			sc_struct_endian;
148 
149 	bus_space_tag_t		sc_devcfg_iot;
150 	bus_space_handle_t	sc_devcfg_ioh;
151 	bus_size_t		sc_devcfg_iosize;
152 
153 	int			sc_ipl; /* set by child */
154 	void			*sc_soft_ih;
155 
156 	int			sc_flags; /* set by child */
157 
158 	uint64_t		sc_active_features;
159 	bool			sc_indirect;
160 	bool			sc_version_1;
161 
162 	int			sc_nvqs; /* set by child */
163 	struct virtqueue	*sc_vqs; /* set by child */
164 
165 	int			sc_childdevid;
166 	device_t		sc_child; 		/* set by child */
167 	enum {
168 		VIRTIO_NO_CHILD = 0,
169 		VIRTIO_CHILD_ATTACH_FINISHED,
170 		VIRTIO_CHILD_ATTACH_FAILED
171 	}			sc_child_state;
172 
173 	virtio_callback		sc_config_change; 	/* set by child */
174 	virtio_callback		sc_intrhand;
175 };
176 #else
177 struct virtio_softc;
178 #endif
179 
180 
181 /* interrupt types, stored in virtio_softc->sc_flags */
182 #define VIRTIO_F_INTR_MPSAFE	(1 << 0)
183 #define VIRTIO_F_INTR_SOFTINT	(1 << 1)
184 #define VIRTIO_F_INTR_MSIX	(1 << 2)
185 #define VIRTIO_F_INTR_PERVQ	(1 << 3)
186 
187 /* public interface */
188 void virtio_negotiate_features(struct virtio_softc*, uint64_t);
189 
190 uint8_t virtio_read_device_config_1(struct virtio_softc *, int);
191 uint16_t virtio_read_device_config_2(struct virtio_softc *, int);
192 uint32_t virtio_read_device_config_4(struct virtio_softc *, int);
193 uint64_t virtio_read_device_config_8(struct virtio_softc *, int);
194 uint16_t virtio_read_device_config_le_2(struct virtio_softc *, int);
195 uint32_t virtio_read_device_config_le_4(struct virtio_softc *, int);
196 void virtio_write_device_config_1(struct virtio_softc *, int, uint8_t);
197 void virtio_write_device_config_2(struct virtio_softc *, int, uint16_t);
198 void virtio_write_device_config_4(struct virtio_softc *, int, uint32_t);
199 void virtio_write_device_config_8(struct virtio_softc *, int, uint64_t);
200 void virtio_write_device_config_le_2(struct virtio_softc *, int, uint16_t);
201 void virtio_write_device_config_le_4(struct virtio_softc *, int, uint32_t);
202 
203 void virtio_init_vq(struct virtio_softc *, struct virtqueue *, int,
204 		    int (*)(void *), void *);
205 void virtio_init_vq_vqdone(struct virtio_softc *,struct virtqueue *, int,
206 		    int (*)(struct virtqueue *));
207 int virtio_alloc_vq(struct virtio_softc*, struct virtqueue*, int, int,
208 		    const char*);
209 int virtio_free_vq(struct virtio_softc*, struct virtqueue*);
210 void virtio_reset(struct virtio_softc *);
211 int virtio_reinit_start(struct virtio_softc *);
212 void virtio_reinit_end(struct virtio_softc *);
213 void virtio_child_attach_start(struct virtio_softc *, device_t, int,
214 		    uint64_t, const char *);
215 int virtio_child_attach_finish(struct virtio_softc *,
216 		    struct virtqueue *, size_t,
217 		    virtio_callback, int);
218 void virtio_child_attach_failed(struct virtio_softc *);
219 void virtio_child_detach(struct virtio_softc *);
220 
221 int virtio_enqueue_prep(struct virtio_softc*, struct virtqueue*, int*);
222 int virtio_enqueue_reserve(struct virtio_softc*, struct virtqueue*, int, int);
223 int virtio_enqueue(struct virtio_softc*, struct virtqueue*, int,
224 		   bus_dmamap_t, bool);
225 int virtio_enqueue_p(struct virtio_softc*, struct virtqueue*, int,
226 		     bus_dmamap_t, bus_addr_t, bus_size_t, bool);
227 int virtio_enqueue_commit(struct virtio_softc*, struct virtqueue*, int, bool);
228 int virtio_enqueue_abort(struct virtio_softc*, struct virtqueue*, int);
229 
230 int virtio_dequeue(struct virtio_softc*, struct virtqueue*, int *, int *);
231 int virtio_dequeue_commit(struct virtio_softc*, struct virtqueue*, int);
232 
233 bool virtio_vq_is_enqueued(struct virtio_softc *, struct virtqueue *);
234 int virtio_postpone_intr(struct virtio_softc *sc, struct virtqueue *vq,
235 		uint16_t nslots);
236 int virtio_postpone_intr_smart(struct virtio_softc *sc, struct virtqueue *vq);
237 int virtio_postpone_intr_far(struct virtio_softc *sc, struct virtqueue *vq);
238 void virtio_stop_vq_intr(struct virtio_softc *, struct virtqueue *);
239 int virtio_start_vq_intr(struct virtio_softc *, struct virtqueue *);
240 
241 /* encapsulation */
242 bus_dma_tag_t	virtio_dmat(struct virtio_softc *);
243 device_t	virtio_child(struct virtio_softc *);
244 int		virtio_intrhand(struct virtio_softc *);
245 uint64_t	virtio_features(struct virtio_softc *);
246 
247 /* autoconf(9) common */
248 void virtio_set_status(struct virtio_softc *, int);
249 void virtio_print_device_type(device_t, int, int);
250 int virtio_attach_failed(struct virtio_softc *);
251 
252 #define virtio_device_reset(sc)	virtio_set_status((sc), 0)
253 
254 /* endian conversion */
255 
256 /*
257  * Virtio structures are read/written in host endian for 0.9 but little endian
258  * for 1.0. One notable exception is AArch BE that always uses little endian.
259  */
260 uint16_t virtio_rw16(struct virtio_softc *sc, uint16_t val);
261 uint32_t virtio_rw32(struct virtio_softc *sc, uint32_t val);
262 uint64_t virtio_rw64(struct virtio_softc *sc, uint64_t val);
263 
264 #endif /* _DEV_PCI_VIRTIOVAR_H_ */
265