0

In Linux kernel space, given a struct net_device * how can I get the associated PCI address?

2
  • net_device is 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. Commented Apr 1 at 5:25
  • It's easy, but why do you need this? Commented Apr 2 at 19:46

1 Answer 1

0

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).

Sign up to request clarification or add additional context in comments.

3 Comments

You also need to call dev_is_pci() before doing any to_pci_dev(), otherwise it's potential invalid pointer dereference.
@0andriy now this is what I call an useful comment. Thank you
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.