code: plan9front

Download patch

ref: 3240008dd182a0d88fb2d0d5ffc5e88accdd957f
parent: 87b1d454ed703e6e93e96c40d73388d5220c1a34
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Jan 17 06:53:50 EST 2021

usbuhci: remove resetlk, simplify scanpci()

--- a/sys/src/9/pc/usbuhci.c
+++ b/sys/src/9/pc/usbuhci.c
@@ -144,6 +144,7 @@
 	Lock;			/* for ilock. qh lists and basic ctlr I/O */
 	QLock	portlck;	/* for port resets/enable... */
 	Pcidev*	pcidev;
+	Ctlr	*next;
 	int	active;
 	int	port;		/* I/O address */
 	Qh*	qhs;		/* list of Qhs for this controller */
@@ -289,8 +290,6 @@
 #define dqprint		if(debug || (qh->io && qh->io->debug))print
 #define ddqprint		if(debug>1 || (qh->io && qh->io->debug>1))print
 
-static Ctlr* ctlrs[Nhcis];
-
 static Tdpool tdpool;
 static Qhpool qhpool;
 static int debug;
@@ -533,19 +532,8 @@
 	Qh *qh;
 	int i;
 
-	if(doilock){
-		if(ctlr == ctlrs[0]){
-			lock(&tdpool);
-			print("tds: alloc %d = inuse %d + free %d\n",
-				tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
-			unlock(&tdpool);
-			lock(&qhpool);
-			print("qhs: alloc %d = inuse %d + free %d\n",
-				qhpool.nalloc, qhpool.ninuse, qhpool.nfree);
-			unlock(&qhpool);
-		}
+	if(doilock)
 		ilock(ctlr);
-	}
 	print("uhci port %#x frames %#p nintr %d ntdintr %d",
 		ctlr->port, ctlr->frames, ctlr->nintr, ctlr->ntdintr);
 	print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
@@ -564,8 +552,19 @@
 		}
 	}
 	print("\n");
-	if(doilock)
+	if(doilock){
 		iunlock(ctlr);
+		if(ctlr->next == nil){
+			lock(&tdpool);
+			print("tds: alloc %d = inuse %d + free %d\n",
+				tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
+			unlock(&tdpool);
+			lock(&qhpool);
+			print("qhs: alloc %d = inuse %d + free %d\n",
+				qhpool.nalloc, qhpool.ninuse, qhpool.nfree);
+			unlock(&qhpool);
+		}
+	}
 }
 
 static void
@@ -2109,18 +2108,18 @@
 	return r;
 }
 
-static void
+static Ctlr*
 scanpci(void)
 {
-	static int already = 0;
-	int io;
-	int i;
+	static Ctlr *first, **lastp;
 	Ctlr *ctlr;
 	Pcidev *p;
+	int io;
 
-	if(already)
-		return;
-	already = 1;
+	if(lastp != nil)
+		return first;
+	lastp = &first;
+
 	p = nil;
 	while(p = pcimatch(p, 0, 0)){
 		/*
@@ -2158,14 +2157,11 @@
 		}
 		ctlr->pcidev = p;
 		ctlr->port = io;
-		for(i = 0; i < Nhcis; i++)
-			if(ctlrs[i] == nil){
-				ctlrs[i] = ctlr;
-				break;
-			}
-		if(i == Nhcis)
-			print("usbuhci: bug: no more controllers\n");
+
+		*lastp = ctlr;
+		lastp = &ctlr->next;
 	}
+	return first;
 }
 
 static void
@@ -2293,8 +2289,6 @@
 static int
 reset(Hci *hp)
 {
-	static Lock resetlck;
-	int i;
 	Ctlr *ctlr;
 	Pcidev *p;
 
@@ -2301,16 +2295,11 @@
 	if(getconf("*nousbuhci"))
 		return -1;
 
-	ilock(&resetlck);
-	scanpci();
-
 	/*
 	 * Any adapter matches if no hp->port is supplied,
 	 * otherwise the ports must match.
 	 */
-	ctlr = nil;
-	for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
-		ctlr = ctlrs[i];
+	for(ctlr = scanpci(); ctlr != nil; ctlr = ctlr->next){
 		if(ctlr->active == 0)
 		if(hp->port == 0 || hp->port == ctlr->port){
 			ctlr->active = 1;
@@ -2317,8 +2306,7 @@
 			break;
 		}
 	}
-	iunlock(&resetlck);
-	if(ctlrs[i] == nil || i == Nhcis)
+	if(ctlr == nil)
 		return -1;
 
 	p = ctlr->pcidev;
@@ -2352,6 +2340,7 @@
 	hp->shutdown = shutdown;
 	hp->debug = setdebug;
 	hp->type = "uhci";
+
 	intrenable(hp->irq, hp->interrupt, hp, hp->tbdf, hp->type);
 
 	return 0;