FreeBSD supports lazy allocation of PCI BAR address, that is, when a device
driver's attach method is invoked and the device's BAR addresses haven't been
initialized, the attach method should invoke bus_alloc_resource_any(),
which invokes pci_alloc_resource() -> pci_alloc_multi_resource() ->
pci_reserve_map() -> pci_write_bar() to allocate a proper BAR address for the
bar and write the address into the BAR register.
This model works fine for native FreeBSD drivers, but for device drivers
shared with Linux (e.g. dev/mlx5/mlx5_core/mlx5_main.c and
ofed/drivers/net/mlx4/main.c. Both of them use pci_request_regions()), this
doesn't work, because pci_resource_type() -> _pci_get_rle() always returns
NULL, so pci_request_regions() doesn't have the opportunity to invoke
bus_alloc_resource_any().
Luckily, we can still use the per-device "struct pci_map" to get the BAR
type and hence the patch is made.
The patch should not cause any regression.
BTW, the background of the patch is: we're adding PCIe pass-through support
for FreeBSD VM running on Hyper-V (Windows Server 2016) by adding a front-end
PCI bridge driver and the driver is supposed to initialize the addresses of
the PCI BARs of the passed-through device. And, we need to use lazy
allocation of PCI BARs, otherwise, in "devinfo -r", the BAR resources would
be registered to the pci bridge driver rather than the real device driver of
the device, and we'll be forced to bear the burden of managing the resources,
i.e. alocate/release. With the proposed patch, the lazy allocation model can
work fine with the mlx drivers.
Please review the patch (v2).