PCI: Tolerate hierarchies with no Root Port
PCI: Tolerate hierarchies with no Root Port

We should not assume any particular hardware topology.  Commit d0751b98dfa3
("PCI: Add dev->has_secondary_link to track downstream PCIe links") relied
on the assumption that every PCIe hierarchy is rooted at a Root Port.  But
we can't rely on any assumption about what hardware we will find; we just
have to deal with the world as it is.

On some platforms, PCIe devices (endpoints, switch upstream ports, etc.)
appear directly on the root bus, and there is no Root Port in the PCI bus
hierarchy.  For example, Meelis observed these top-level devices on a
Sparc V245:

  0000:02:00.0 PCI bridge to [bus 03-0d]    Switch Upstream Port
  0001:02:00.0 PCI bridge to [bus 03]       PCIe to PCI/PCI-X Bridge

These devices *look* like they have links going upstream, but there really
are no upstream devices.

In set_pcie_port_type(), we used the parent device to figure out which side
of a switch port has a link, so if the parent device did not exist, we
dereferenced a NULL parent pointer.

Check whether the parent device exists before dereferencing it.

Meelis observed this oops on Sparc V245 and T2000.  Ben Herrenschmidt says
this is also possible on IBM PowerVM guests on PowerPC.

[bhelgaas: changelog, comment]
Link: a5379cb061f82e34ea8986826ff5cac4a4320a3d@math.ut.ee
Reported-by: Meelis Roos <c1c0388d3cdd4295e8ad201acc4082e735b9ca96@linux.ee>
Tested-by: Meelis Roos <c1c0388d3cdd4295e8ad201acc4082e735b9ca96@linux.ee>
Signed-off-by: Yijing Wang <ac442ed3e729ed4f09d087e9c319e1c47c72d6f1@huawei.com>
Signed-off-by: Bjorn Helgaas <f10ec01be0c6b54271af5c550cd257810bdf3268@google.com>
Acked-by: David S. Miller <fe08d3c717adf2ae63592e4c9aec6e3e404d8e3e@davemloft.net>
KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs,KristFoundation/Programs