PCI page error?

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
metallevel
Posts: 18
Joined: Thu May 17, 2012 12:43 pm
Location: in front of a computer

PCI page error?

Post by metallevel »

In the "Enumerating PCI Buses" section, the code for checking all the functions of a multi-function device starts at function 0 and breaks as soon as a function isn't detected. This requires all the functions of a multi-function device to be numbered consecutively, which I don't think is always true (Bochs for one doesn't appear to).

Code: Select all

 void checkDevice(uint8_t bus, uint8_t device) {
     uint8_t function = 0;
 
     vendorID = getVendorID(bus, device, function);
     if(vendorID = 0xFFFF) return;        // Device doesn't exist
     checkFunction(bus, device, function);
     headerType = getHeaderType(bus, device, function);
     if( (headerType & 0x80) != 0) {
         /* It is a multi-function device, so check remaining functions */
         for(function = 1; function < 8; function++) {
             if(getVendorID(bus, device, function) == 0xFFFF) break;   // I don't think this is right
             checkFunction(bus, device, function);
         }
     }
 }
User avatar
trinopoty
Member
Member
Posts: 87
Joined: Wed Feb 09, 2011 2:21 am
Location: Raipur, India

Re: PCI page error?

Post by trinopoty »

Removing the line

Code: Select all

if(getVendorID(bus, device, function) == 0xFFFF) break;   // I don't think this is right

may work. In-fact, that line has almost no use.
User avatar
trinopoty
Member
Member
Posts: 87
Joined: Wed Feb 09, 2011 2:21 am
Location: Raipur, India

Re: PCI page error?

Post by trinopoty »

So the new code is

Code: Select all

void checkDevice(uint8_t bus, uint8_t device) {
     uint8_t function = 0;

     vendorID = getVendorID(bus, device, function);
     if(vendorID = 0xFFFF) return;        // Device doesn't exist
     checkFunction(bus, device, function);
     headerType = getHeaderType(bus, device, function);
     if( (headerType & 0x80) != 0) {
         /* It is a multi-function device, so check remaining functions */
         for(function = 1; function < 8; function++)
             checkFunction(bus, device, function);
     }
}
metallevel
Posts: 18
Joined: Thu May 17, 2012 12:43 pm
Location: in front of a computer

Re: PCI page error?

Post by metallevel »

That's what I'm thinking. That line is there to cut down on the number of I/O accesses by exiting the function checking loop at the first function that isn't detected. Problem is I don't believe the functions are always consecutive, so bailing out at the first missing function could cause you to miss some of them.

I would discuss this on the articles talk page, but I did some searching on here and this seems to be a common algorithm. Even the venerable Brendan posted some code a while back that quit the multi-function loop in the same fashion :? . Hence why I'm a bit hesitant to call this an error on the PCI page when it could just as easily be Bochs or me.
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Freenode IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: PCI page error?

Post by Griwes »

Wouldn't changing the break into continue be smarter than just dropping the condition in this case?
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: PCI page error?

Post by Brendan »

Hi,

metallevel wrote:I would discuss this on the articles talk page, but I did some searching on here and this seems to be a common algorithm. Even the venerable Brendan posted some code a while back that quit the multi-function loop in the same fashion :? . Hence why I'm a bit hesitant to call this an error on the PCI page when it could just as easily be Bochs or me.


A while ago someone was asking questions about PCI device enumeration. I was just going to tell them "here's the relevant part of the wiki page!" but the information wasn't in the wiki, so I couldn't. It's possible that I posted the pseudo-code as a reply, then slapped myself, then added the same pseudo-code to the wiki.

In general, you should never assume anyone is correct, including me. I also think you're right (and that my pseudo-code was wrong). ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
metallevel
Posts: 18
Joined: Thu May 17, 2012 12:43 pm
Location: in front of a computer

Re: PCI page error?

Post by metallevel »

Ok, thanks. I just don't want to be the guy who 'corrects' people who are already right :D .

@Griwes yeah that would be better, fewer IO accesses.
Post Reply