]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/vmx/virtio.c
6451eeef571f28c6fad439bef3ff885b2d2f8af5
[plan9front.git] / sys / src / cmd / vmx / virtio.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "dat.h"
5 #include "fns.h"
6
7 #include <ip.h>         /* parseether() */
8 #include <libsec.h>     /* genrandom() */
9
10 typedef struct VIODev VIODev;
11 typedef struct VIOQueue VIOQueue;
12 typedef struct VIOBuf VIOBuf;
13 typedef struct VIONetDev VIONetDev;
14 typedef struct VIOBlkDev VIOBlkDev;
15
16 enum {
17         BUFCHAIN = 1,
18         BUFWR = 2,
19         
20         USEDNOIRQ = 1,
21         
22         DRIVEROK = 4, /* devstat */
23 };
24
25 struct VIOBuf {
26         u32int flags;
27         VIOQueue *qu;
28         void *p;
29         u64int addr;
30         u32int len;
31         u32int idx;
32         VIOBuf *next, *head;
33         u32int rptr, wptr;
34 };
35
36 struct VIOQueue {
37         QLock;
38         Rendez;
39         VIODev *d;
40         u8int (*desc)[16], *avail, *used;
41         u16int size;
42         u32int addr;
43         u16int availidx, usedidx;
44         void (*notify)(VIOQueue*);
45         int livebuf;
46         Rendez livebufrend;
47 };
48
49 struct VIONetDev {
50         int readfd, writefd;
51         u8int mac[6];
52         enum {
53                 VNETPROMISC = 1,
54                 VNETALLMULTI = 2,
55                 VNETALLUNI = 4,
56                 VNETNOMULTI = 8,
57                 VNETNOUNI = 16,
58                 VNETNOBCAST = 32,
59                 
60                 VNETHEADER = 1<<31,
61         } flags;
62         u64int macbloom, multibloom;
63 };
64
65 struct VIOBlkDev {
66         int fd;
67         uvlong size;
68 };
69
70 struct VIODev {
71         PCIDev *pci;
72         u32int devfeat, guestfeat;
73         u16int qsel;
74         u8int devstat, isrstat;
75         VIOQueue *qu;
76         int nqu, allocqu;
77         u32int (*io)(int, u16int, u32int, int, VIODev *);
78         void (*reset)(VIODev *);
79         union {
80                 VIONetDev net;
81                 VIOBlkDev blk;
82         };
83 };
84
85 static void
86 vioirq_(void *arg)
87 {
88         VIODev *d;
89         int val;
90         
91         d = ((void**)arg)[0];
92         val = (uintptr)((void**)arg)[1];
93         if(val != 0)
94                 d->isrstat |= val;
95         else
96                 d->isrstat = 0;
97         pciirq(d->pci, d->isrstat);
98         free(arg);
99 }
100
101 static void
102 vioirq(VIODev *d, int val)
103 {
104         void **v;
105         
106         assert(d != nil);
107         v = emalloc(sizeof(void*)*2);
108         v[0] = d;
109         v[1] = (void *) val;
110         sendnotif(vioirq_, v);
111 }
112
113 static void *
114 checkdesc(VIOQueue *q, int i)
115 {
116         if(i >= q->size){
117                 vmerror("virtio device %#x: invalid next pointer %d in queue (size %d), ignoring descriptor", q->d->pci->bdf, i, q->size);
118                 return nil;
119         }
120         return q->desc[i];
121 }
122
123 VIOBuf *
124 viogetbuf(VIOQueue *q, int wait)
125 {
126         u16int gidx;
127         VIOBuf *b, *rb, **bp;
128         void *dp;
129         
130         qlock(q);
131 waitloop:
132         while((q->d->devstat & DRIVEROK) == 0 || q->desc == nil || (gidx = GET16(q->avail, 2), gidx == q->availidx)){
133                 if(!wait){
134                         qunlock(q);
135                         return nil;
136                 }
137                 rsleep(q);
138         }
139         dp = checkdesc(q, GET16(q->avail, 4 + 2 * (q->availidx % q->size)));
140         rb = nil;
141         bp = &rb;
142         for(;;){
143                 b = emalloc(sizeof(VIOBuf));
144                 b->qu = q;
145                 b->idx = (u8int(*)[16])dp - q->desc;
146                 b->addr = GET64(dp, 0);
147                 b->len = GET32(dp, 8);
148                 b->flags = GET16(dp, 12);
149                 b->p = gptr(b->addr, b->len);
150                 if(b->p == nil){
151                         vmerror("virtio device %#x: invalid buffer pointer %#p in queue, ignoring descriptor", q->d->pci->bdf, (void*)b->addr);
152                         free(b);
153                         break;
154                 }
155                 *bp = b;
156                 b->head = rb;
157                 bp = &b->next;
158                 if((b->flags & BUFCHAIN) == 0) break;
159                 dp = checkdesc(q, GET16(dp, 14));
160                 if(dp == nil) break;
161         }
162         q->availidx++;
163         if(rb == nil) goto waitloop;
164         q->livebuf++;
165         qunlock(q);
166         return rb;
167 }
168
169 void
170 vioputbuf(VIOBuf *b)
171 {
172         VIOBuf *bn;
173         VIOQueue *q;
174         u8int *p;
175         
176         if(b == nil) return;
177         q = b->qu;
178         qlock(q);
179         if((q->d->devstat & DRIVEROK) == 0){
180                 qunlock(q);
181                 goto end;
182         }
183         if(q->used == nil)
184                 vmerror("virtio device %#x: address was set to an invalid value while holding buffer", q->d->pci->bdf);
185         else{
186                 p = q->used + 4 + 8 * (q->usedidx % q->size);
187                 PUT32(p, 4, b->wptr);
188                 PUT32(p, 0, b->idx);
189                 PUT16(q->used, 2, ++q->usedidx);
190         }
191         if(--q->livebuf <= 0)
192                 rwakeup(&q->livebufrend);
193         qunlock(q);
194         if(q->avail != nil && (GET16(q->avail, 0) & USEDNOIRQ) == 0)
195                 vioirq(q->d, 1);
196 end:
197         while(b != nil){
198                 bn = b->next;
199                 free(b);
200                 b = bn;
201         }
202 }
203
204 ulong
205 vioqread(VIOBuf *b, void *v, ulong n)
206 {
207         VIOBuf *c;
208         u32int p;
209         int rc;
210         ulong m;
211         
212         p = b->rptr;
213         c = b;
214         rc = 0;
215         for(;;){
216                 if(rc >= n) return rc;
217                 for(;;){
218                         if(c == nil) return rc;
219                         if((c->flags & BUFWR) == 0){
220                                 if(p < c->len) break;
221                                 p -= c->len;
222                         }
223                         c = c->next;
224                 }
225                 m = c->len - p;
226                 if(m > n - rc) m = n - rc;
227                 memmove(v, (u8int*)c->p + p, m);
228                 p += m, rc += m;
229                 v = (u8int*)v + m;
230                 b->rptr += m;
231         }
232 }
233
234 ulong
235 vioqwrite(VIOBuf *b, void *v, ulong n)
236 {
237         VIOBuf *c;
238         u32int p;
239         int rc;
240         ulong m;
241         
242         p = b->wptr;
243         c = b;
244         rc = 0;
245         for(;;){
246                 if(rc >= n) return rc;
247                 for(;;){
248                         if(c == nil) return rc;
249                         if((c->flags & BUFWR) != 0){
250                                 if(p < c->len) break;
251                                 p -= c->len;
252                         }
253                         c = c->next;
254                 }
255                 m = c->len - p;
256                 if(m > n - rc) m = n - rc;
257                 memmove((u8int*)c->p + p, v, m);
258                 p += m, rc += m;
259                 v = (u8int*)v + m;
260                 b->wptr += m;
261         }
262 }
263
264 ulong
265 vioqrem(VIOBuf *b, int wr)
266 {
267         VIOBuf *c;
268         u32int p;
269         ulong rc;
270         
271         p = wr ? b->wptr : b->rptr;
272         for(c = b;; c = c->next){
273                 if(c == nil) return 0;
274                 if(((c->flags & BUFWR) != 0) == wr){
275                         if(p < c->len) break;
276                         p -= c->len;
277                 }
278         }
279         rc = c->len - p;
280         for(c = c->next; c != nil; c = c->next)
281                 if(((c->flags & BUFWR) != 0) == wr)
282                         rc += c->len;
283         return rc;
284 }
285
286 static void
287 vioqaddrset(VIOQueue *q, u64int addr)
288 {
289         void *p;
290         int sz1, sz;
291
292         addr <<= 12;
293         sz1 = -(-(18 * q->size + 4) & -4096);
294         sz = sz1 + (-(-(8 * q->size + 6) & -4096));
295         p = gptr(addr, sz);
296         if(p == nil)
297                 vmerror("virtio device %#x: attempt to set queue to invalid address %#p", q->d->pci->bdf, (void *) addr);
298         qlock(q);
299         q->addr = addr;
300         if(p == nil){
301                 q->desc = nil;
302                 q->avail = nil;
303                 q->used = nil;
304         }else{
305                 q->desc = p;
306                 q->avail = (u8int*)p + 16 * q->size;
307                 q->used = (u8int*)p + sz1;
308                 rwakeupall(q);
309         }
310         qunlock(q);
311 }
312
313 static void
314 viodevstatset(VIODev *v, u32int val)
315 {
316         int i;
317
318         v->devstat = val;
319         if(val == 0){
320                 if(v->reset != nil)
321                         v->reset(v);
322                 v->guestfeat = 0;
323                 vioirq(v, 0);
324                 for(i = 0; i < v->nqu; i++){
325                         qlock(&v->qu[i]);
326                         while(v->qu[i].livebuf > 0)
327                                 rsleep(&v->qu[i].livebufrend);
328                         qunlock(&v->qu[i]);
329                 }
330         }else{
331                 for(i = 0; i < v->nqu; i++)
332                         v->qu[i].notify(&v->qu[i]);
333         }
334 }
335
336 u32int
337 vioio(int isin, u16int port, u32int val, int sz, void *vp)
338 {
339         VIODev *v;
340         int rc;
341         static char whinebuf[32];
342         
343         v = vp;
344         switch(isin << 16 | port){
345         case 0x4: v->guestfeat = val; return 0;
346         case 0x8: if(v->qsel < v->nqu) vioqaddrset(&v->qu[v->qsel], val); return 0;
347         case 0xe: v->qsel = val; return 0;
348         case 0x10: if(val < v->nqu) v->qu[val].notify(&v->qu[val]); return 0;
349         case 0x12: viodevstatset(v, val); return 0;
350         case 0x10000: return v->devfeat;
351         case 0x10004: return v->guestfeat;
352         case 0x10008: return v->qsel >= v->nqu ? 0 : v->qu[v->qsel].addr;
353         case 0x1000c: return v->qsel >= v->nqu ? 0 : v->qu[v->qsel].size;
354         case 0x1000e: return v->qsel;
355         case 0x10010: return 0;
356         case 0x10012: return v->devstat;
357         case 0x10013: rc = v->isrstat; vioirq(v, 0); return rc;
358         }
359         if(port >= 20 && v->io != nil)
360                 return v->io(isin, port - 20, val, sz, v);
361         snprint(whinebuf, sizeof(whinebuf), "virtio device %6x", v->pci->bdf);
362         return iowhine(isin, port, val, sz, whinebuf);
363 }
364
365 VIODev *
366 mkviodev(u16int devid, u32int pciclass, u32int subid, int queues)
367 {
368         VIODev *d;
369         
370         d = emalloc(sizeof(VIODev));
371         d->pci = mkpcidev(allocbdf(), devid << 16 | 0x1AF4, pciclass << 8, 1);
372         d->pci->subid = subid << 16;
373         mkpcibar(d->pci, BARIO, 0, 256, vioio, d);
374         d->qu = emalloc(queues * sizeof(VIOQueue));
375         d->allocqu = queues;
376         return d;
377 }
378
379 static void
380 viowakeup(VIOQueue *q)
381 {
382         qlock(q);
383         rwakeupall(q);
384         qunlock(q);
385 }
386
387 VIOQueue *
388 mkvioqueue(VIODev *d, int sz, void (*fn)(VIOQueue*))
389 {
390         VIOQueue *q;
391
392         assert(sz > 0 && sz <= 32768 && (sz & sz - 1) == 0 && fn != nil && d->nqu < d->allocqu);
393         q = d->qu + d->nqu++;
394         q->Rendez.l = q;
395         q->livebufrend.l = q;
396         q->size = sz;
397         q->d = d;
398         q->notify = fn;
399         return q;
400 }
401
402 int
403 bloomhash(u8int *mac)
404 {
405         int x;
406
407         x = mac[0];
408         x ^= mac[0] >> 6 ^ mac[1] << 2;
409         x ^= mac[1] >> 4 ^ mac[2] << 4;
410         x ^= mac[2] >> 2;
411         x ^= mac[3];
412         x ^= mac[3] >> 6 ^ mac[4] << 2;
413         x ^= mac[4] >> 4 ^ mac[5] << 4;
414         x ^= mac[5] >> 2;
415         return x & 63;
416 }
417
418 int
419 viomacok(VIODev *d, u8int *mac)
420 {
421         static u8int bcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
422
423         if((d->net.flags & VNETPROMISC) != 0) return 1;
424         if((mac[0] & 1) == 0){
425                 if((d->net.flags & (VNETNOUNI|VNETALLUNI)) != 0)
426                         return (d->net.flags & VNETNOUNI) == 0;
427                 if(memcmp(mac, d->net.mac, 6) == 0) return 1;
428                 if(d->net.macbloom == 0) return 0;
429                 return d->net.macbloom >> bloomhash(mac) & 1;
430         }else if(memcmp(mac, bcast, 6) == 0)
431                 return (d->net.flags & VNETNOBCAST) == 0;
432         else{
433                 if((d->net.flags & (VNETNOMULTI|VNETALLMULTI)) != 0)
434                         return (d->net.flags & VNETNOMULTI) == 0;
435                 if(d->net.multibloom == 0) return 0;
436                 return d->net.multibloom >> bloomhash(mac) & 1;
437         }
438 }
439
440 void
441 vionetrproc(void *vp)
442 {
443         VIODev *v;
444         VIOQueue *q;
445         VIOBuf *vb;
446         uchar rxhead[10];
447         uchar rxbuf[1600];
448         int rc;
449         
450         threadsetname("vionetrproc");
451         v = vp;
452         q = &v->qu[0];
453         memset(rxhead, 0, sizeof(rxhead));
454         for(;;){
455                 rc = read(v->net.readfd, rxbuf, sizeof(rxbuf));
456                 if(rc == 0){
457                         vmerror("read(vionetrproc): eof");
458                         threadexits("read: eof");
459                 }
460                 if(rc < 0){
461                         vmerror("read(vionetrproc): %r");
462                         threadexits("read: %r");
463                 }
464                 if(rc < 14){
465                         vmerror("vionetrproc: short packet received (len=%d)", rc);
466                         continue;
467                 }
468                 if(!viomacok(v, rxbuf))
469                         continue;
470                 vb = viogetbuf(q, 1);
471                 if(vb == nil){
472                         vmerror("viogetbuf: %r");
473                         continue;
474                 }
475                 vioqwrite(vb, rxhead, sizeof(rxhead));
476                 vioqwrite(vb, rxbuf, rc);
477                 vioputbuf(vb);
478         }
479 }
480
481 void
482 vionetwproc(void *vp)
483 {
484         VIODev *v;
485         VIOQueue *q;
486         VIOBuf *vb;
487         uchar txhead[10];
488         uchar txbuf[1610];
489         int rc, len;
490         uvlong ns;
491         
492         threadsetname("vionetwproc");
493         v = vp;
494         q = &v->qu[1];
495         for(;;){
496                 vb = viogetbuf(q, 1);
497                 if(vb == nil){
498                         vmerror("viogetbuf: %r");
499                         threadexits("viogetbuf: %r");
500                 }
501                 vioqread(vb, txhead, sizeof(txhead));
502                 len = vioqread(vb, txbuf+10, sizeof(txbuf)-10);
503                 if(len == sizeof(txbuf)-10){
504                         vmerror("virtio net: ignoring excessively long packet");
505                         vioputbuf(vb);
506                         continue;
507                 }
508                 if(len < 14){
509                         /* openbsd ends up sending lots of zero length packets sometimes */
510                         if(len != 0)
511                                 vmerror("virtio net: ignoring short packet (length=%d)", len);
512                         vioputbuf(vb);
513                         continue;
514                 }else if(len < 60){ /* openbsd doesn't seem to know about ethernet minimum packet lengths either */
515                         memset(txbuf + 10 + len, 0, 60 - len);
516                         len = 60;
517                 }
518                 if((v->net.flags & VNETHEADER) != 0){
519                         txbuf[0] = len  >> 8;
520                         txbuf[1] = len;
521                         ns = nanosec();
522                         txbuf[2] = ns >> 56;
523                         txbuf[3] = ns >> 48;
524                         txbuf[4] = ns >> 40;
525                         txbuf[5] = ns >> 32;
526                         txbuf[6] = ns >> 24;
527                         txbuf[7] = ns >> 16;
528                         txbuf[8] = ns >> 8;
529                         txbuf[9] = ns;
530                         rc = write(v->net.writefd, txbuf, len + 10);
531                 }else
532                         rc = write(v->net.writefd, txbuf + 10, len);
533                 vioputbuf(vb);
534                 if(rc < 0){
535                         vmerror("write(vionetwproc): %r");
536                         continue;
537                 }
538                 if(rc < len){
539                         vmerror("write(vionetwproc): incomplete write (%d < %d)", rc, len);
540                         continue;
541                 }
542         }
543 }
544
545 u32int
546 vionetio(int isin, u16int port, u32int val, int sz, VIODev *v)
547 {
548         switch(isin << 16 | port){
549         case 0x10000: case 0x10001: case 0x10002: case 0x10003:
550                 return GET32(v->net.mac, 0) >> (port & 3) * 8;
551         case 0x10004: case 0x10005: case 0x10006: case 0x10007:
552                 return (GET16(v->net.mac, 4) | 1 << 16) >> (port & 3) * 8;
553         }
554         return iowhine(isin, port, val, sz, "virtio net");
555 }
556
557 int
558 vionettables(VIODev *d, VIOBuf *b)
559 {
560         u8int buf[4];
561         u8int mac[6];
562         u64int bloom[2];
563         int i, l;
564         
565         bloom[0] = 0;
566         bloom[1] = 0;
567         for(i = 0; i < 2; i++){
568                 if(vioqread(b, buf, 4) < 4)
569                         return 1;
570                 l = GET32(buf, 0);
571                 while(l--){
572                         if(vioqread(b, mac, 6) < 6)
573                                 return 1;
574                         bloom[i] |= 1ULL<<bloomhash(mac);
575                 }
576         }
577         d->net.macbloom = bloom[0];
578         d->net.multibloom = bloom[1];
579         return 0;
580 }
581
582 void
583 vionetcmd(VIOQueue *q)
584 {
585         VIODev *d;
586         VIOBuf *b;
587         u8int cmd[2], buf[6];
588         u8int ack;
589         int fl;
590
591         d = q->d;
592         for(; b = viogetbuf(q, 0), b != nil; vioputbuf(b)){
593                 if(vioqread(b, cmd, 2) < 2){
594                         ack = 1;
595                         vioqwrite(b, &ack, 1);
596                         continue;
597                 }
598                 ack = 0;
599                 switch(cmd[0] << 8 | cmd[1]){
600                 case 0x0000: fl = VNETPROMISC; goto flag;
601                 case 0x0001: fl = VNETALLMULTI; goto flag;
602                 case 0x0002: fl = VNETALLUNI; goto flag;
603                 case 0x0003: fl = VNETNOMULTI; goto flag;
604                 case 0x0004: fl = VNETNOUNI; goto flag;
605                 case 0x0005: fl = VNETNOBCAST; goto flag;
606                 flag:
607                         if(vioqread(b, buf, 1) < 1) ack = 1;
608                         else if(buf[0] == 1) d->net.flags |= fl;
609                         else if(buf[0] == 0) d->net.flags &= ~fl;
610                         else ack = 1;
611                         break;
612                 case 0x0100: /* MAC_TABLE_SET */
613                         ack = vionettables(d, b);
614                         break;
615                 case 0x0101: /* MAC_ADDR_SET */
616                         if(vioqread(b, buf, 6) < 6) ack = 1;
617                         else memmove(d->net.mac, buf, 6);
618                         break;
619                 default:
620                         ack = 1;
621                 }
622                 vioqwrite(b, &ack, 1);
623         }
624 }
625
626 void
627 vionetreset(VIODev *d)
628 {
629         d->net.flags &= VNETHEADER;
630         d->net.macbloom = 0;
631         d->net.multibloom = 0;
632 }
633
634 int
635 mkvionet(char *net)
636 {
637         int fd, cfd;
638         VIODev *d;
639         char *ea;
640         int flags;
641         enum { VNETFILE = 1 };
642
643         ea = nil;
644         flags = 0;
645         for(;;){
646                 if(strncmp(net, "hdr!", 4) == 0){
647                         net += 4;
648                         flags |= VNETHEADER;
649                 }else if(strncmp(net, "file!", 5) == 0){
650                         net += 5;
651                         flags |= VNETFILE;
652                 }else if(strncmp(net, "ea:", 3) == 0){
653                         net = strchr(ea = net+3, '!');
654                         if(net++ == nil){
655                                 werrstr("missing: !");
656                                 return -1;
657                         }
658                 }else
659                         break;
660         }
661         if((flags & VNETFILE) != 0){
662                 flags &= ~VNETFILE;
663                 fd = open(net, ORDWR);
664                 if(fd < 0) return -1;
665         }else{
666                 fd = dial(netmkaddr("-1", net, nil), nil, nil, &cfd);
667                 if(fd < 0) return -1;
668                 if(cfd >= 0) {
669                         write(cfd, "promiscuous", 11);
670                         write(cfd, "bridge", 6);
671                 }
672         }
673         
674         d = mkviodev(0x1000, 0x020000, 1, 3);
675         mkvioqueue(d, 1024, viowakeup);
676         mkvioqueue(d, 1024, viowakeup);
677         mkvioqueue(d, 32, vionetcmd);
678         if(ea == nil || parseether(d->net.mac, ea)){
679                 genrandom(d->net.mac, 6);
680                 d->net.mac[0] = d->net.mac[0] & ~1 | 2;
681         }
682         d->net.flags = flags;
683         d->devfeat = 1<<5|1<<16|1<<17|1<<18|1<<20;
684         d->io = vionetio;
685         d->reset = vionetreset;
686         d->net.readfd = d->net.writefd = fd;
687         proccreate(vionetrproc, d, 8192);
688         proccreate(vionetwproc, d, 8192);
689         return 0;
690 }
691
692 u32int
693 vioblkio(int isin, u16int port, u32int val, int sz, VIODev *v)
694 {
695         switch(isin << 16 | port){
696         case 0x10000: case 0x10001: case 0x10002: case 0x10003:
697                 return (u32int)v->blk.size >> (port & 3) * 8;
698         case 0x10004: case 0x10005: case 0x10006: case 0x10007:
699                 return (u32int)(v->blk.size >> 32) >> (port & 3) * 8;
700         }
701         return iowhine(isin, port, val, sz, "virtio blk");
702 }
703
704 void
705 vioblkproc(void *vp)
706 {
707         VIODev *v;
708         VIOQueue *q;
709         VIOBuf *b;
710         u8int cmd[16];
711         u8int ack;
712         char buf[8192];
713         uvlong addr;
714         int rc, n, m;
715         
716         threadsetname("vioblkproc");
717         v = vp;
718         q = &v->qu[0];
719         for(;;){
720                 b = viogetbuf(q, 1);
721                 if(b == nil){
722                         vmerror("vioblkproc: viogetbuf: %r");
723                         threadexits("vioblkproc: viogetbuf: %r");
724                 }
725                 ack = 0;
726                 if(vioqread(b, cmd, sizeof(cmd)) < sizeof(cmd)) goto nope;
727                 addr = GET64(cmd, 8);
728                 switch(GET32(cmd, 0)){
729                 case 0:
730                         n = vioqrem(b, 1) - 1;
731                         if(n < 0 || addr * 512 + n > v->blk.size * 512){
732                                 ack = 1;
733                                 break;
734                         }
735                         seek(v->blk.fd, addr << 9, 0);
736                         for(; n > 0; n -= rc){
737                                 rc = sizeof(buf);
738                                 if(n < rc) rc = n;
739                                 rc = read(v->blk.fd, buf, rc);
740                                 if(rc < 0) vmerror("read(vioblkproc): %r");
741                                 if(rc <= 0){
742                                         ack = 1;
743                                         break;
744                                 }
745                                 vioqwrite(b, buf, rc);
746                         }
747                         break;
748                 case 1:
749                         n = vioqrem(b, 0);
750                         if(addr * 512 + n > v->blk.size * 512){
751                                 ack = 1;
752                                 break;
753                         }
754                         seek(v->blk.fd, addr << 9, 0);
755                         for(; n > 0; n -= rc){
756                                 m = vioqread(b, buf, sizeof(buf));
757                                 rc = write(v->blk.fd, buf, m);
758                                 if(rc < 0) vmerror("write(vioblkproc): %r");
759                                 if(rc < m){
760                                         ack = 1;
761                                         break;
762                                 }
763                         }
764                         break;
765                 default:
766                 nope:
767                         ack = 2;
768                 }
769                 vioqwrite(b, &ack, 1);
770                 vioputbuf(b);
771         }
772 }
773
774 int
775 mkvioblk(char *fn)
776 {
777         int fd;
778         VIODev *d;
779         
780         fd = open(fn, ORDWR);
781         if(fd < 0) return -1;
782         d = mkviodev(0x1000, 0x018000, 2, 1);
783         mkvioqueue(d, 32, viowakeup);
784         d->io = vioblkio;
785         d->blk.fd = fd;
786         d->blk.size = seek(fd, 0, 2) >> 9;
787         proccreate(vioblkproc, d, 16384);
788         return 0;
789 }