git: 9front

Download patch

ref: 986aa1670625558654fbfd6e78a9c5d78d44fbfd
parent: da72d769a8d56b2fadee5fb72d7a8ebd799b135e
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Tue Apr 24 03:48:59 EDT 2012

sdvirtio: ioalloc, cleanup

--- a/sys/src/9/pc/sdvirtio.c
+++ b/sys/src/9/pc/sdvirtio.c
@@ -15,13 +15,15 @@
 typedef struct Vqueue Vqueue;
 typedef struct Vdev Vdev;
 
+/* status flags */
 enum {
-	Acknowledge	= 1,
-	Driver		= 2,
-	DriverOk	= 4,
-	Failed		= 128,
+	Acknowledge = 1,
+	Driver = 2,
+	DriverOk = 4,
+	Failed = 0x80,
 };
 
+/* virtio ports */
 enum {
 	Devfeat = 0,
 	Drvfeat = 4,
@@ -35,6 +37,7 @@
 	Devspec = 20,
 };
 
+/* descriptor flags */
 enum {
 	Next = 1,
 	Write = 2,
@@ -151,12 +154,11 @@
 static Vdev*
 viopnpdevs(int typ)
 {
-	Vdev *vd, *head, *tail;
+	Vdev *vd, *h, *t;
 	Pcidev *p;
-	u32int a;
 	int n, i;
 
-	head = tail = nil;
+	h = t = nil;
 	for(p = nil; p = pcimatch(p, 0, 0);){
 		if(p->vid != 0x1AF4)
 			continue;
@@ -171,6 +173,11 @@
 			break;
 		}
 		vd->port = p->mem[0].bar & ~0x1;
+		if(ioalloc(vd->port, p->mem[0].size, 0, "virtio") < 0){
+			print("viopnpdevs: port %lux in use\n", vd->port);
+			free(vd);
+			continue;
+		}
 		vd->typ = typ;
 		vd->pci = p;
 
@@ -186,18 +193,18 @@
 			if((vd->queue[i] = mkvqueue(n)) == nil)
 				break;
 			coherence();
-			a = PADDR(vd->queue[i]->desc)/BY2PG;
-			outl(vd->port+Qaddr, a);
+			outl(vd->port+Qaddr, PADDR(vd->queue[i]->desc)/BY2PG);
 		}
 		vd->nqueue = i;
 	
-		if(head == nil)
-			head = vd;
+		if(h == nil)
+			h = vd;
 		else
-			tail->next = vd;
-		tail = vd;
+			t->next = vd;
+		t = vd;
 	}
-	return head;
+
+	return h;
 }
 
 struct Rock {
@@ -404,11 +411,10 @@
 		if(vd->nqueue != 1)
 			continue;
 
-		intrenable(vd->pci->intl, viointerrupt, vd, vd->pci->tbdf, "sdvirtio");
+		intrenable(vd->pci->intl, viointerrupt, vd, vd->pci->tbdf, "virtio");
 		outb(vd->port+Status, inb(vd->port+Status) | DriverOk);
 
-		s = malloc(sizeof(*s));
-		if(s == nil)
+		if((s = malloc(sizeof(*s))) == nil)
 			break;
 		s->ctlr = vd;
 		s->idno = id++;
--