virtiovar.h revision 1.4 1 /* $NetBSD: virtiovar.h,v 1.4 2014/12/19 06:54:40 ozaki-r 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 vq_entry {
74 SIMPLEQ_ENTRY(vq_entry) qe_list; /* free list */
75 uint16_t qe_index; /* index in vq_desc array */
76 /* followings are used only when it is the `head' entry */
77 int16_t qe_next; /* next enq slot */
78 bool qe_indirect; /* 1 if using indirect */
79 struct vring_desc *qe_desc_base;
80 };
81
82 struct virtqueue {
83 struct virtio_softc *vq_owner;
84 unsigned int vq_num; /* queue size (# of entries) */
85 int vq_index; /* queue number (0, 1, ...) */
86
87 /* vring pointers (KVA) */
88 struct vring_desc *vq_desc;
89 struct vring_avail *vq_avail;
90 struct vring_used *vq_used;
91 void *vq_indirect;
92
93 /* virtqueue allocation info */
94 void *vq_vaddr;
95 int vq_availoffset;
96 int vq_usedoffset;
97 int vq_indirectoffset;
98 bus_dma_segment_t vq_segs[1];
99 unsigned int vq_bytesize;
100 bus_dmamap_t vq_dmamap;
101
102 int vq_maxsegsize;
103 int vq_maxnsegs;
104
105 /* free entry management */
106 struct vq_entry *vq_entries;
107 SIMPLEQ_HEAD(, vq_entry) vq_freelist;
108 kmutex_t vq_freelist_lock;
109
110 /* enqueue/dequeue status */
111 uint16_t vq_avail_idx;
112 uint16_t vq_used_idx;
113 int vq_queued;
114 kmutex_t vq_aring_lock;
115 kmutex_t vq_uring_lock;
116
117 /* interrupt handler */
118 int (*vq_done)(struct virtqueue*);
119 };
120
121 struct virtio_softc {
122 device_t sc_dev;
123 pci_chipset_tag_t sc_pc;
124 pcitag_t sc_tag;
125 bus_dma_tag_t sc_dmat;
126
127 int sc_ipl; /* set by child */
128 void *sc_ih;
129 void *sc_soft_ih;
130
131 int sc_flags; /* set by child */
132
133 bus_space_tag_t sc_iot;
134 bus_space_handle_t sc_ioh;
135 bus_size_t sc_iosize;
136 int sc_config_offset;
137
138 uint32_t sc_features;
139 bool sc_indirect;
140
141 int sc_nvqs; /* set by child */
142 struct virtqueue *sc_vqs; /* set by child */
143
144 int sc_childdevid;
145 device_t sc_child; /* set by child */
146 int (*sc_config_change)(struct virtio_softc*);
147 /* set by child */
148 int (*sc_intrhand)(struct virtio_softc*);
149 /* set by child */
150 };
151
152 #define VIRTIO_F_PCI_INTR_MPSAFE (1 << 0)
153 #define VIRTIO_F_PCI_INTR_SOFTINT (1 << 1)
154
155 /* public interface */
156 uint32_t virtio_negotiate_features(struct virtio_softc*, uint32_t);
157
158 uint8_t virtio_read_device_config_1(struct virtio_softc *, int);
159 uint16_t virtio_read_device_config_2(struct virtio_softc *, int);
160 uint32_t virtio_read_device_config_4(struct virtio_softc *, int);
161 uint64_t virtio_read_device_config_8(struct virtio_softc *, int);
162 void virtio_write_device_config_1(struct virtio_softc *, int, uint8_t);
163 void virtio_write_device_config_2(struct virtio_softc *, int, uint16_t);
164 void virtio_write_device_config_4(struct virtio_softc *, int, uint32_t);
165 void virtio_write_device_config_8(struct virtio_softc *, int, uint64_t);
166
167 int virtio_alloc_vq(struct virtio_softc*, struct virtqueue*, int, int, int,
168 const char*);
169 int virtio_free_vq(struct virtio_softc*, struct virtqueue*);
170 void virtio_reset(struct virtio_softc *);
171 void virtio_reinit_start(struct virtio_softc *);
172 void virtio_reinit_end(struct virtio_softc *);
173
174 int virtio_enqueue_prep(struct virtio_softc*, struct virtqueue*, int*);
175 int virtio_enqueue_reserve(struct virtio_softc*, struct virtqueue*, int, int);
176 int virtio_enqueue(struct virtio_softc*, struct virtqueue*, int,
177 bus_dmamap_t, bool);
178 int virtio_enqueue_p(struct virtio_softc*, struct virtqueue*, int,
179 bus_dmamap_t, bus_addr_t, bus_size_t, bool);
180 int virtio_enqueue_commit(struct virtio_softc*, struct virtqueue*, int, bool);
181 int virtio_enqueue_abort(struct virtio_softc*, struct virtqueue*, int);
182
183 int virtio_dequeue(struct virtio_softc*, struct virtqueue*, int *, int *);
184 int virtio_dequeue_commit(struct virtio_softc*, struct virtqueue*, int);
185
186 int virtio_vq_intr(struct virtio_softc *);
187 void virtio_stop_vq_intr(struct virtio_softc *, struct virtqueue *);
188 void virtio_start_vq_intr(struct virtio_softc *, struct virtqueue *);
189
190 #endif /* _DEV_PCI_VIRTIOVAR_H_ */
191