This source file includes following definitions.
- virtio_disk_init
- alloc_desc
- free_desc
- free_chain
- alloc3_desc
- virtio_disk_rw
- virtio_disk_intr
1
2
3
4
5
6
7
8 #include "types.h"
9 #include "riscv.h"
10 #include "defs.h"
11 #include "param.h"
12 #include "memlayout.h"
13 #include "spinlock.h"
14 #include "sleeplock.h"
15 #include "fs.h"
16 #include "buf.h"
17 #include "virtio.h"
18
19
20 #define R(r) ((volatile uint32 *)(VIRTIO0 + (r)))
21
22 static struct disk {
23
24
25
26
27
28 struct virtq_desc *desc;
29
30
31
32
33
34 struct virtq_avail *avail;
35
36
37
38
39 struct virtq_used *used;
40
41
42 char free[NUM];
43 uint16 used_idx;
44
45
46
47
48 struct {
49 struct buf *b;
50 char status;
51 } info[NUM];
52
53
54
55 struct virtio_blk_req ops[NUM];
56
57 struct spinlock vdisk_lock;
58
59 } disk;
60
61 void
62 virtio_disk_init(void)
63 {
64 uint32 status = 0;
65
66 initlock(&disk.vdisk_lock, "virtio_disk");
67
68 if(*R(VIRTIO_MMIO_MAGIC_VALUE) != 0x74726976 ||
69 *R(VIRTIO_MMIO_VERSION) != 2 ||
70 *R(VIRTIO_MMIO_DEVICE_ID) != 2 ||
71 *R(VIRTIO_MMIO_VENDOR_ID) != 0x554d4551){
72 panic("could not find virtio disk");
73 }
74
75
76 *R(VIRTIO_MMIO_STATUS) = status;
77
78
79 status |= VIRTIO_CONFIG_S_ACKNOWLEDGE;
80 *R(VIRTIO_MMIO_STATUS) = status;
81
82
83 status |= VIRTIO_CONFIG_S_DRIVER;
84 *R(VIRTIO_MMIO_STATUS) = status;
85
86
87 uint64 features = *R(VIRTIO_MMIO_DEVICE_FEATURES);
88 features &= ~(1 << VIRTIO_BLK_F_RO);
89 features &= ~(1 << VIRTIO_BLK_F_SCSI);
90 features &= ~(1 << VIRTIO_BLK_F_CONFIG_WCE);
91 features &= ~(1 << VIRTIO_BLK_F_MQ);
92 features &= ~(1 << VIRTIO_F_ANY_LAYOUT);
93 features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
94 features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
95 *R(VIRTIO_MMIO_DRIVER_FEATURES) = features;
96
97
98 status |= VIRTIO_CONFIG_S_FEATURES_OK;
99 *R(VIRTIO_MMIO_STATUS) = status;
100
101
102 status = *R(VIRTIO_MMIO_STATUS);
103 if(!(status & VIRTIO_CONFIG_S_FEATURES_OK))
104 panic("virtio disk FEATURES_OK unset");
105
106
107 *R(VIRTIO_MMIO_QUEUE_SEL) = 0;
108
109
110 if(*R(VIRTIO_MMIO_QUEUE_READY))
111 panic("virtio disk should not be ready");
112
113
114 uint32 max = *R(VIRTIO_MMIO_QUEUE_NUM_MAX);
115 if(max == 0)
116 panic("virtio disk has no queue 0");
117 if(max < NUM)
118 panic("virtio disk max queue too short");
119
120
121 disk.desc = kalloc();
122 disk.avail = kalloc();
123 disk.used = kalloc();
124 if(!disk.desc || !disk.avail || !disk.used)
125 panic("virtio disk kalloc");
126 memset(disk.desc, 0, PGSIZE);
127 memset(disk.avail, 0, PGSIZE);
128 memset(disk.used, 0, PGSIZE);
129
130
131 *R(VIRTIO_MMIO_QUEUE_NUM) = NUM;
132
133
134 *R(VIRTIO_MMIO_QUEUE_DESC_LOW) = (uint64)disk.desc;
135 *R(VIRTIO_MMIO_QUEUE_DESC_HIGH) = (uint64)disk.desc >> 32;
136 *R(VIRTIO_MMIO_DRIVER_DESC_LOW) = (uint64)disk.avail;
137 *R(VIRTIO_MMIO_DRIVER_DESC_HIGH) = (uint64)disk.avail >> 32;
138 *R(VIRTIO_MMIO_DEVICE_DESC_LOW) = (uint64)disk.used;
139 *R(VIRTIO_MMIO_DEVICE_DESC_HIGH) = (uint64)disk.used >> 32;
140
141
142 *R(VIRTIO_MMIO_QUEUE_READY) = 0x1;
143
144
145 for(int i = 0; i < NUM; i++)
146 disk.free[i] = 1;
147
148
149 status |= VIRTIO_CONFIG_S_DRIVER_OK;
150 *R(VIRTIO_MMIO_STATUS) = status;
151
152
153 }
154
155
156 static int
157 alloc_desc()
158 {
159 for(int i = 0; i < NUM; i++){
160 if(disk.free[i]){
161 disk.free[i] = 0;
162 return i;
163 }
164 }
165 return -1;
166 }
167
168
169 static void
170 free_desc(int i)
171 {
172 if(i >= NUM)
173 panic("free_desc 1");
174 if(disk.free[i])
175 panic("free_desc 2");
176 disk.desc[i].addr = 0;
177 disk.desc[i].len = 0;
178 disk.desc[i].flags = 0;
179 disk.desc[i].next = 0;
180 disk.free[i] = 1;
181 wakeup(&disk.free[0]);
182 }
183
184
185 static void
186 free_chain(int i)
187 {
188 while(1){
189 int flag = disk.desc[i].flags;
190 int nxt = disk.desc[i].next;
191 free_desc(i);
192 if(flag & VRING_DESC_F_NEXT)
193 i = nxt;
194 else
195 break;
196 }
197 }
198
199
200
201 static int
202 alloc3_desc(int *idx)
203 {
204 for(int i = 0; i < 3; i++){
205 idx[i] = alloc_desc();
206 if(idx[i] < 0){
207 for(int j = 0; j < i; j++)
208 free_desc(idx[j]);
209 return -1;
210 }
211 }
212 return 0;
213 }
214
215 void
216 virtio_disk_rw(struct buf *b, int write)
217 {
218 uint64 sector = b->blockno * (BSIZE / 512);
219
220 acquire(&disk.vdisk_lock);
221
222
223
224
225
226
227 int idx[3];
228 while(1){
229 if(alloc3_desc(idx) == 0) {
230 break;
231 }
232 sleep(&disk.free[0], &disk.vdisk_lock);
233 }
234
235
236
237
238 struct virtio_blk_req *buf0 = &disk.ops[idx[0]];
239
240 if(write)
241 buf0->type = VIRTIO_BLK_T_OUT;
242 else
243 buf0->type = VIRTIO_BLK_T_IN;
244 buf0->reserved = 0;
245 buf0->sector = sector;
246
247 disk.desc[idx[0]].addr = (uint64) buf0;
248 disk.desc[idx[0]].len = sizeof(struct virtio_blk_req);
249 disk.desc[idx[0]].flags = VRING_DESC_F_NEXT;
250 disk.desc[idx[0]].next = idx[1];
251
252 disk.desc[idx[1]].addr = (uint64) b->data;
253 disk.desc[idx[1]].len = BSIZE;
254 if(write)
255 disk.desc[idx[1]].flags = 0;
256 else
257 disk.desc[idx[1]].flags = VRING_DESC_F_WRITE;
258 disk.desc[idx[1]].flags |= VRING_DESC_F_NEXT;
259 disk.desc[idx[1]].next = idx[2];
260
261 disk.info[idx[0]].status = 0xff;
262 disk.desc[idx[2]].addr = (uint64) &disk.info[idx[0]].status;
263 disk.desc[idx[2]].len = 1;
264 disk.desc[idx[2]].flags = VRING_DESC_F_WRITE;
265 disk.desc[idx[2]].next = 0;
266
267
268 b->disk = 1;
269 disk.info[idx[0]].b = b;
270
271
272 disk.avail->ring[disk.avail->idx % NUM] = idx[0];
273
274 __sync_synchronize();
275
276
277 disk.avail->idx += 1;
278
279 __sync_synchronize();
280
281 *R(VIRTIO_MMIO_QUEUE_NOTIFY) = 0;
282
283
284 while(b->disk == 1) {
285 sleep(b, &disk.vdisk_lock);
286 }
287
288 disk.info[idx[0]].b = 0;
289 free_chain(idx[0]);
290
291 release(&disk.vdisk_lock);
292 }
293
294 void
295 virtio_disk_intr()
296 {
297 acquire(&disk.vdisk_lock);
298
299
300
301
302
303
304
305 *R(VIRTIO_MMIO_INTERRUPT_ACK) = *R(VIRTIO_MMIO_INTERRUPT_STATUS) & 0x3;
306
307 __sync_synchronize();
308
309
310
311
312 while(disk.used_idx != disk.used->idx){
313 __sync_synchronize();
314 int id = disk.used->ring[disk.used_idx % NUM].id;
315
316 if(disk.info[id].status != 0)
317 panic("virtio_disk_intr status");
318
319 struct buf *b = disk.info[id].b;
320 b->disk = 0;
321 wakeup(b);
322
323 disk.used_idx += 1;
324 }
325
326 release(&disk.vdisk_lock);
327 }