Skip to contentSkip to conversation

pci: add to global list before sysfs?

DISCUSSION Pierre Ossman 3 participants 18 messages Apr 4, 2008 – Apr 15, 2008

lore
Share & tools
lore
#0 lore raw

Hi Greg,

I have a PCI hotplug problem I was hoping you could provide some insight into.

I want the PCI layer to make sure that all subfunctions of a newly added card are visible via pci_get_device() before any of them are binded to drivers. I.e. I want pci_bus_add_devices() to first make a pass and add the devices to the global list, then a second pass for device_add().

The reason I want this is because I have at least two chips where I need to know which other subfunctions are present on the card in order to correctly initalise the subfunction I'm currently probing. Right now that fails miserably as the driver does not see the other devices in its probe function.

Is it possible to do this or will things break left and right if I add them to the global list before I register them with the driver core?

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#1 lore raw
Quoted 10 lines · Pierre Ossman

Hi Greg,

I have a PCI hotplug problem I was hoping you could provide some insight into.

I want the PCI layer to make sure that all subfunctions of a newly added card are visible via pci_get_device() before any of them are binded to drivers. I.e. I want pci_bus_add_devices() to first make a pass and add the devices to the global list, then a second pass for device_add().

What "global list"? You mean the one that I just deleted with my patchset that is in -mm and going to be in 2.6.26? :)

Quoted 5 lines

The reason I want this is because I have at least two chips where I need to know which other subfunctions are present on the card in order to correctly initalise the subfunction I'm currently probing. Right now that fails miserably as the driver does not see the other devices in its probe function.

How will changing the addition of the device to the list affect this behavior? Do you want to walk the list in your driver's probe function, looking for the "subfunction" that is not yet added to the list?

Quoted 2 lines

Is it possible to do this or will things break left and right if I add them to the global list before I register them with the driver core?

As I mention above, the "global list" is now gone, so it's going to be a bit harder to do this. We now only keep one list of all PCI devices, and that is with the driver core mechanisms as the duplicate list was just sitting there doing nothing for the past 4 years.

So is the problem that your driver need to bind to multiple pci devices at the same time in order to work properly?

Or is this just really broken hardware?

confused,

greg k-h

#2 lore raw
Quoted 4 lines · On Fri, 4 Apr 2008 14:01:32 -0700 Greg KH <[email protected]> wrote:

What "global list"? You mean the one that I just deleted with my patchset that is in -mm and going to be in 2.6.26? :)

*gnn*

Quoted 5 lines

How will changing the addition of the device to the list affect this behavior? Do you want to walk the list in your driver's probe function, looking for the "subfunction" that is not yet added to the list?

Yes. Things work fine when the driver is loaded after the entire PCI bus has been scanned, but not when new cards are hotplugged with the driver already present. I'd like the behaviour to be the same in both cases.

Quoted 8 lines
Quoted 2 lines

Is it possible to do this or will things break left and right if I add them to the global list before I register them with the driver core?

As I mention above, the "global list" is now gone, so it's going to be a bit harder to do this. We now only keep one list of all PCI devices, and that is with the driver core mechanisms as the duplicate list was just sitting there doing nothing for the past 4 years.

Talk about bad timing...

Quoted 3 lines

So is the problem that your driver need to bind to multiple pci devices at the same time in order to work properly?

More or less. Primarily it needs to know which other subfunctions are there, but in one of the two cases it also needs to fiddle some PCI config space registers.

Quoted 2 lines

Or is this just really broken hardware?

It is. Deliberately broken in order to work with a certain other broken operating system.

They have two interfaces for the same piece of hardware, and you need to make sure you only use one of them or things go south very quickly. To make things worse, you do not know which of the two will be available for any given card. Hence the need to scan the bus.

In one of the cases we currently try to disable the secondary interface, which is done by PCI config space modifications on subfunction 0.

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#3 lore raw
Quoted 9 lines · Pierre Ossman
Quoted 4 lines · On Fri, 4 Apr 2008 14:01:32 -0700 Greg KH <[email protected]> wrote:

What "global list"? You mean the one that I just deleted with my patchset that is in -mm and going to be in 2.6.26? :)

*gnn*

Well the "global list" was not being used for anything, and it was being created at the same time the driver core list was, so unless you wanted to change the way that all pci devices were probed/created, it would not have worked out.

Quoted 9 lines
Quoted 4 lines

How will changing the addition of the device to the list affect this behavior? Do you want to walk the list in your driver's probe function, looking for the "subfunction" that is not yet added to the list?

Yes. Things work fine when the driver is loaded after the entire PCI bus has been scanned, but not when new cards are hotplugged with the driver already present. I'd like the behaviour to be the same in both cases.

Why not just create a thread in your driver, or timer, and sleep until you see the other devices? If after a certian time you never do, then bail out.

That should work properly for you for both cases (startup and loading later.)

thanks,

greg k-h

#4 lore raw
Quoted 7 lines · On Sat, 5 Apr 2008 08:53:23 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · Pierre Ossman

Yes. Things work fine when the driver is loaded after the entire PCI bus has been scanned, but not when new cards are hotplugged with the driver already present. I'd like the behaviour to be the same in both cases.

Quoted 7 lines

Why not just create a thread in your driver, or timer, and sleep until you see the other devices? If after a certian time you never do, then bail out.

That should work properly for you for both cases (startup and loading later.)

I'm not sure how that could be done in a clean way. Right now I have the following:

                sd_dev = NULL;
                while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
                        PCI_DEVICE_ID_JMICRON_JMB38X_SD, sd_dev)) != NULL) {
                        if ((PCI_SLOT(chip->pdev->devfn) ==
                                PCI_SLOT(sd_dev->devfn)) &&
                                (chip->pdev->bus == sd_dev->bus))
                                break;
                }

                if (sd_dev) {
                        pci_dev_put(sd_dev);
                        dev_info(&chip->pdev->dev, "Refusing to bind to "
                                "secondary interface.\n");
                        return -ENODEV;
                }

If I do this threaded, I'd have to return success to the driver core and then act like a possum later. Doable, but not very clean. It could be solved if it was possible to sleep in probe functions, but from what I can tell that will stop the probing of subsequent devices.

Also, the timeout would introduce an inherent race. Which means either putting a high timeout to make the race insignificant, or a low timeout so users don't have to wait for their device to actually become usable.

Is it possible to write some routines that can walk the PCI device tree without having any pci_dev structures to hang on to? To solve my basic problem I just need to know how the tree looks like, not actually poke any of the other devices.

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#5 lore raw
Quoted 37 lines · Pierre Ossman
Quoted 7 lines · On Sat, 5 Apr 2008 08:53:23 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · Pierre Ossman

Yes. Things work fine when the driver is loaded after the entire PCI bus has been scanned, but not when new cards are hotplugged with the driver already present. I'd like the behaviour to be the same in both cases.

Quoted 7 lines

Why not just create a thread in your driver, or timer, and sleep until you see the other devices? If after a certian time you never do, then bail out.

That should work properly for you for both cases (startup and loading later.)

I'm not sure how that could be done in a clean way. Right now I have the following:

                sd_dev = NULL;
                while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
                        PCI_DEVICE_ID_JMICRON_JMB38X_SD, sd_dev)) != NULL) {
                        if ((PCI_SLOT(chip->pdev->devfn) ==
                                PCI_SLOT(sd_dev->devfn)) &&
                                (chip->pdev->bus == sd_dev->bus))
                                break;
                }

                if (sd_dev) {
                        pci_dev_put(sd_dev);
                        dev_info(&chip->pdev->dev, "Refusing to bind to "
                                "secondary interface.\n");
                        return -ENODEV;
                }
Quoted 2 lines

If I do this threaded, I'd have to return success to the driver core and then act like a possum later.

Yes.

Quoted 1 line

Doable, but not very clean.

Hey, take it up with the hardware designers compensating for an "unclean" operating system :)

Quoted 2 lines

It could be solved if it was possible to sleep in probe functions, but from what I can tell that will stop the probing of subsequent devices.

Yes, you can sleep, but it will stop the probe of subsequent devices. I did implement multiple threads for pci probing, but it ended up not saving any time and causing more problems than it was worth.

Quoted 8 lines

Also, the timeout would introduce an inherent race. Which means either putting a high timeout to make the race insignificant, or a low timeout so users don't have to wait for their device to actually become usable.

Is it possible to write some routines that can walk the PCI device tree without having any pci_dev structures to hang on to? To solve my basic problem I just need to know how the tree looks like, not actually poke any of the other devices.

No, not in a sane way as all arches walk the pci device tree for discovery in many different ways.

I don't think that a time-out would be a big deal, it shouldn't be very long (a second or two). Also note that all distros would not have this problem as they do pci discovery first, and then start loading all of the modules in the initramfs after the pci tree has been properly scanned. So the very large majority of users would not have a problem.

Or are you doing this on an embedded system?

thanks,

greg k-h

#6 lore raw
Quoted 11 lines · On Sat, 5 Apr 2008 10:12:09 -0700 Greg KH <[email protected]> wrote:
Quoted 2 lines · Pierre Ossman

If I do this threaded, I'd have to return success to the driver core and then act like a possum later.

Yes.

Quoted 1 line

Doable, but not very clean.

Hey, take it up with the hardware designers compensating for an "unclean" operating system :)

I have. And unfortunately that other operating system is just too big of a market to ignore. :/

This is a cooperating vendor though, which is why I'm trying to put a bit more extra effort into actually getting their devices fully supported. Telling them to ditch Windows support is asking too much.

Anyway, a big part of my reluctance to go this route is that this chip is just one of many the driver supports. So I'm not thrilled to do some funky architecture for this device as it'll most certainly cause me pain for the others...

Quoted 8 lines

I don't think that a time-out would be a big deal, it shouldn't be very long (a second or two). Also note that all distros would not have this problem as they do pci discovery first, and then start loading all of the modules in the initramfs after the pci tree has been properly scanned. So the very large majority of users would not have a problem.

Or are you doing this on an embedded system?

No, it's primarily desktop. But these chips are available as ExpressCards, so I'm trying to get them to work when hot-plugged as opposed to when permantently wired into the machine.

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#7 lore raw
Quoted 33 lines · Pierre Ossman
Quoted 11 lines · On Sat, 5 Apr 2008 10:12:09 -0700 Greg KH <[email protected]> wrote:
Quoted 2 lines · Pierre Ossman

If I do this threaded, I'd have to return success to the driver core and then act like a possum later.

Yes.

Quoted 1 line

Doable, but not very clean.

Hey, take it up with the hardware designers compensating for an "unclean" operating system :)

I have. And unfortunately that other operating system is just too big of a market to ignore. :/

This is a cooperating vendor though, which is why I'm trying to put a bit more extra effort into actually getting their devices fully supported. Telling them to ditch Windows support is asking too much.

Anyway, a big part of my reluctance to go this route is that this chip is just one of many the driver supports. So I'm not thrilled to do some funky architecture for this device as it'll most certainly cause me pain for the others...

Quoted 6 lines

I don't think that a time-out would be a big deal, it shouldn't be very long (a second or two). Also note that all distros would not have this problem as they do pci discovery first, and then start loading all of the modules in the initramfs after the pci tree has been properly scanned. So the very large majority of users would not have a problem.

Quoted 6 lines
Quoted 2 lines

Or are you doing this on an embedded system?

No, it's primarily desktop. But these chips are available as ExpressCards, so I'm trying to get them to work when hot-plugged as opposed to when permantently wired into the machine.

Then I suggest a thread/timer, it shouldn't be that difficult.

thanks,

greg k-h

#8 lore raw
Quoted 8 lines · On Mon, 7 Apr 2008 20:41:26 -0700 Greg KH <[email protected]> wrote:
Quoted 4 lines · Pierre Ossman

No, it's primarily desktop. But these chips are available as ExpressCards, so I'm trying to get them to work when hot-plugged as opposed to when permantently wired into the machine.

Then I suggest a thread/timer, it shouldn't be that difficult.

I have another idea. Is it guaranteed that the PCI subsystem will add devices in order? I.e. will 1f.0 be added before 1f.1, 1f.2 and so on? If so, then I can restructure things and get it to work properly without any timers.

Btw, is your @kroah address working? I have a kernel bugzilla entry I was hoping to get your input on.

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#9 lore raw
Quoted 9 lines · Pierre Ossman
Quoted 6 lines · On Mon, 7 Apr 2008 20:41:26 -0700 Greg KH <[email protected]> wrote:
Quoted 4 lines · Pierre Ossman

No, it's primarily desktop. But these chips are available as ExpressCards, so I'm trying to get them to work when hot-plugged as opposed to when permantently wired into the machine.

Quoted 7 lines
Quoted 2 lines

Then I suggest a thread/timer, it shouldn't be that difficult.

I have another idea. Is it guaranteed that the PCI subsystem will add devices in order? I.e. will 1f.0 be added before 1f.1, 1f.2 and so on? If so, then I can restructure things and get it to work properly without any timers.

Yes, that should the be the order in which devices are always scanned.

Quoted 2 lines

Btw, is your @kroah address working? I have a kernel bugzilla entry I was hoping to get your input on.

Yes, that address works very well...

thanks,

greg k-h

#10 lore raw
In reply to Pierre Ossman
Quoted 10 lines · Pierre Ossman
Quoted 1 line

Why not just create a thread in your driver, or timer, and sleep

until

Quoted 1 line

you see the other devices? If after a certian time you never do,

then

Quoted 3 lines

bail out.

That should work properly for you for both cases (startup and

loading

Quoted 2 lines

later.)

You don't need a timer. You can just use the notifier mechanism I added a while ago that should call you on device addition and removal on the bus type you are interested in.

Ben.

#11 lore raw
Quoted 5 lines · On Mon, 14 Apr 2008 07:37:14 +1000 Benjamin Herrenschmidt…

You don't need a timer. You can just use the notifier mechanism I added a while ago that should call you on device addition and removal on the bus type you are interested in.

Another solution presented itself, but do you have some pointers on where I get more information about the notifiers in case I need them in the future?

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#12 lore raw
Quoted 4 lines · Pierre Ossman
Quoted 1 line · On Mon, 14 Apr 2008 07:37:14 +1000 Benjamin Herrenschmidt…
Quoted 8 lines
Quoted 4 lines

You don't need a timer. You can just use the notifier mechanism I added a while ago that should call you on device addition and removal on the bus type you are interested in.

Another solution presented itself, but do you have some pointers on where I get more information about the notifiers in case I need them in the future?

In devices.h :

#13 lore raw
In reply to Pierre Ossman
Quoted 4 lines · Pierre Ossman
Quoted 1 line · On Mon, 14 Apr 2008 07:37:14 +1000 Benjamin Herrenschmidt…
Quoted 8 lines
Quoted 4 lines

You don't need a timer. You can just use the notifier mechanism I added a while ago that should call you on device addition and removal on the bus type you are interested in.

Another solution presented itself, but do you have some pointers on where I get more information about the notifiers in case I need them in the future?

In device.h:

/*
* Bus notifiers: Get notified of addition/removal of devices
* and binding/unbinding of drivers to devices.
* In the long run, it should be a replacement for the platform
* notify hooks.
*/
struct notifier_block;

extern int bus_register_notifier(struct bus_type *bus,

				 struct notifier_block *nb);
extern int bus_unregister_notifier(struct bus_type *bus,
				   struct notifier_block *nb);

/* All 4 notifers below get called with the target struct device *
* as an argument. Note that those functions are likely to be called
* with the device semaphore held in the core, so be careful. */
#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
#define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
#define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
unbound */
Do you need more info ?

Cheers,
Ben.

#14 lore raw
Quoted 9 lines · On Mon, 14 Apr 2008 18:58:36 +1000 Benjamin Herrenschmidt…
Quoted 4 lines · Pierre Ossman

Another solution presented itself, but do you have some pointers on where I get more information about the notifiers in case I need them in the future?

In device.h:

*snip*

Quoted 3 lines

unbound */
Do you need more info ?

That'll do fine. Thanks.

Rgds

Signature · 5 lines
     -- Pierre Ossman

  Linux kernel, MMC maintainer        http://www.kernel.org
  PulseAudio, core developer          http://pulseaudio.org
  rdesktop, core developer          http://www.rdesktop.org
#15 lore raw
In reply to Greg KH
Quoted 11 lines · Greg KH

As I mention above, the "global list" is now gone, so it's going to be a
bit harder to do this. We now only keep one list of all PCI devices, and that is with the driver core mechanisms as the duplicate list was just sitting there doing nothing for the past 4 years.

So is the problem that your driver need to bind to multiple pci devices
at the same time in order to work properly?

Or is this just really broken hardware?

Haven't had a chance to look at your changes yet, but I have memories of being pretty happy with having that 2 step mechanism in PCI where the driver core would only see the devices after fixup.

The reason for that is powerpc, like many other archs, need to do all sorts of fixups before devices get used by drivers. Resources need to be fixed up, interrupts, etc...

A lot of that happens in pcibios_fixup_bus() and we did rely on having no driver bound before that happens. The binding can only happen after the arch calls pci_bus_add_devices().

Is that still the case ? If not, then there is some big potential breakage here and we need to look closely at re-organizing the way we do our fixups.

Cheers,
Ben.

#16 lore raw
Quoted 12 lines · Benjamin Herrenschmidt
Quoted 10 lines · Greg KH

As I mention above, the "global list" is now gone, so it's going to be a
bit harder to do this. We now only keep one list of all PCI devices, and that is with the driver core mechanisms as the duplicate list was just sitting there doing nothing for the past 4 years.

So is the problem that your driver need to bind to multiple pci devices
at the same time in order to work properly?

Quoted 5 lines
Quoted 1 line

Or is this just really broken hardware?

Haven't had a chance to look at your changes yet, but I have memories of being pretty happy with having that 2 step mechanism in PCI where the driver core would only see the devices after fixup.

No, that is left alone, I just dropped the duplicate device list that was linked within the PCI devices themselves. The only user of it was the "reorder the PCI devices" code for some Dell machines, and that was just doing it to be safe (it was also reordering the driver core list of PCI devices, which is what really mattered.)

So everything should still be fine.

thanks,

greg k-h

#17 lore raw
Quoted 11 lines · Greg KH
Quoted 3 lines

Haven't had a chance to look at your changes yet, but I have memories of being pretty happy with having that 2 step mechanism in PCI where the driver core would only see the devices after fixup.

No, that is left alone, I just dropped the duplicate device list that was linked within the PCI devices themselves. The only user of it was the "reorder the PCI devices" code for some Dell machines, and that was just doing it to be safe (it was also reordering the driver core list of PCI devices, which is what really mattered.)

So everything should still be fine.

Ok, thanks.

Cheers,
Ben.