PCI device detection

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

PCI device detection

Post by Bietje »

Hi,

I wrote some code to detect PCI devices. And it seems to work fine, I only have some questions about the way it finds a hard disk.

I'll start with my code:

Code: Select all

static int
ol_pci_iterate()
{
        ol_pci_dev_t dev = kalloc(sizeof(struct ol_pci_dev));
        ol_pci_id_t id;
        
        for(dev->bus = 0; dev->bus < OL_PCI_NUM_BUS; dev->bus++)
        {
                /* iterate through all busses */
                for(dev->device = 0; dev->device < OL_PCI_NUM_DEV; dev->device++)
                {
                        /* Looping trough all devices */
                        for(dev->func = 0; dev->func < OL_PCI_NUM_FUNC; 
                                dev->func++) 
                        {
                                id = ol_pci_read_dword(ol_pci_calculate_address(
                                        dev, OL_PCI_REG_ID));
                                id >>= 16;
       -->                    if(id == 0xffff)
                                {
                                        if(dev->func == 0) break;
                                        printclass(dev);
                                } <---
                                        
                                else
                                {
                                        printclass(dev);
                                }
                        }
                }
        }
}
Only if I add that code (look at the arrows) do I detect devices with class code 1 (the hard disk). That means that the hard disk ID is 0xffff.
http://wiki.osdev.org/PCI wrote:When a configuration access attempts to select a device that does not exist, the host bridge will complete the access without error, dropping all data on writes and returning all ones on reads. The following code segment illustrates the read of a non-existent device.
Because the class code is normal, and the device implements function did i think there was nothing wrong. But to be sure I'm asking.. is it normal? Or should I go through my code again and check for errors? This is something that only appears on a real PC (all PC's I have tested so far).

Greets,
Bietje
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: PCI device detection

Post by Combuster »

There are several potential issues in there:
- PCI does not list harddisks, it only lists controllers.
- It is valid to have a device id of 0xffff, but not a vendor ID of 0xffff or a vendor ID of 0x0000, and you are not checking for that.
- It checks all functions of all devices, which means that half of your devices will show up 8 times.
- It checks all buses, which is mostly unnecessary.
- I don't see any signedness or unsignedness information in your types.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Bietje
Member
Member
Posts: 100
Joined: Wed Apr 20, 2011 6:57 am

Re: PCI device detection

Post by Bietje »

Combuster wrote:There are several potential issues in there:
- PCI does not list harddisks, it only lists controllers.
- It is valid to have a device id of 0xffff, but not a vendor ID of 0xffff or a vendor ID of 0x0000, and you are not checking for that.
- It checks all functions of all devices, which means that half of your devices will show up 8 times.
- It checks all buses, which is mostly unnecessary.
- I don't see any signedness or unsignedness information in your types.
Hi,

Thank you for you reply. I fixed 'problem' 2. I didn't notice it yet. Also, I am looking at more then just the device ID now.

Greets,
Bietje
Post Reply