In Linux kernel space, given a struct net_device * how can I get the associated PCI address?
1 Answer
Seems like I found a way. Assuming the network driver creating the net_device does SET_NETDEV_DEV(), then it's possible to get the associated struct pci_dev *pdev = to_pci_dev(netdev->dev.parent) and with that pdev->bus->number which is the PCI bus id, and PCI_SLOT(pdev->devfn) which is the PCI device id.
Thanks for all the comments btw, extremely useful (sarcasm btw).
3 Comments
0andriy
You also need to call
dev_is_pci() before doing any to_pci_dev(), otherwise it's potential invalid pointer dereference.pa5h1nh0
@0andriy now this is what I call an useful comment. Thank you
0andriy
Yeah, but my comment was indeed curious. I trully do not understand why you need all this... I can imagine other ways of getting almost everything without this kind of layering violation.
net_deviceis a generic struct. Only a specific device will have hardware address definitions. All cards have different register definitions so there is no single PCI address.