Page 1 of 1

PCI device detection

Posted: Mon Aug 22, 2011 5:32 am
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

Re: PCI device detection

Posted: Mon Aug 22, 2011 6:36 am
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.

Re: PCI device detection

Posted: Wed Aug 24, 2011 5:50 am
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