qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
To: Thomas Huth <thuth@redhat.com>
Cc: qemu-devel@nongnu.org, aik@ozlabs.ru, agraf@suse.de,
	mdroth@linux.vnet.ibm.com, qemu-ppc@nongnu.org,
	david@gibson.dropbear.id.au
Subject: Re: [Qemu-devel] [PATCH v6 5/6] spapr_pci: populate ibm,loc-code
Date: Thu, 04 Jun 2015 10:46:31 +0530	[thread overview]
Message-ID: <87eglsxdcg.fsf@abhimanyu.in.ibm.com> (raw)
In-Reply-To: <20150603194028.1ea14040@thh440s>

Thomas Huth <thuth@redhat.com> writes:

> On Wed,  3 Jun 2015 16:55:56 +0530
> Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:
>
>> Each hardware instance has a platform unique location code.  The OF
>> device tree that describes a part of a hardware entity must include
>> the “ibm,loc-code” property with a value that represents the location
>> code for that hardware entity.
>> 
>> Populate ibm,loc-code.
>> 
>> 1) PCI passthru devices need to identify with its own ibm,loc-code
>>    available on the host. In failure cases use:
>>    vfio_<name>:<phb-index>:<bus>:<slot>.<fn>
>> 
>> 2) Emulated devices encode as following:
>>    qemu_<name>:<phb-index>:<bus>:<slot>.<fn>
>> 
>> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
>> ---
>>  hw/ppc/spapr_pci.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------
>>  1 file changed, 68 insertions(+), 10 deletions(-)
>> 
>> diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c
>> index 4226468..986bb21 100644
>> --- a/hw/ppc/spapr_pci.c
>> +++ b/hw/ppc/spapr_pci.c
>> @@ -746,6 +746,60 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
>>      return &phb->iommu_as;
>>  }
>>  
>> +static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb,  PCIDevice *pdev)
>> +{
>> +    char *path = NULL, *buf = NULL, *host = NULL;
>> +
>> +    /* Get the PCI VFIO host id */
>> +    host = object_property_get_str(OBJECT(pdev), "host", NULL);
>> +    if (!host) {
>> +        goto err_out;
>> +    }
>> +
>> +    /* Construct the path of the file that will give us the DT location */
>> +    path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host);
>> +    g_free(host);
>> +    if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) {
>> +        goto err_out;
>> +    }
>> +    g_free(path);
>> +
>> +    /* Construct and read from host device tree the loc-code */
>> +    path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf);
>> +    g_free(buf);
>> +    if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) {
>> +        goto err_out;
>> +    }
>> +    return buf;
>
> I'd maybe change the above 4 lines into:
>
>     if (path && g_file_get_contents(path, &buf, NULL, NULL)) {
>         return buf;
>     }
>
> so that you can get rid of one goto here.

Wouldnt make much of a difference though ! 

>> +err_out:
>> +    g_free(path);
>> +    return NULL;
>> +}
>> +
>> +static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
>> +{
>> +    char *buf;
>> +    const char *devtype = "qemu";
>> +    uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
>> +
>> +    if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
>> +        buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
>> +        if (buf) {
>> +            return buf;
>> +        }
>> +        devtype = "vfio";
>> +    }
>> +    /*
>> +     * For emulated devices and VFIO-failure case, make up
>> +     * the loc-code.
>> +     */
>> +    buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x",
>> +                          devtype, pdev->name, sphb->index, busnr,
>> +                          PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
>> +    return buf;
>> +}
>> +
>>  /* Macros to operate with address in OF binding to PCI */
>>  #define b_x(x, p, l)    (((x) & ((1<<(l))-1)) << (p))
>>  #define b_n(x)          b_x((x), 31, 1) /* 0 if relocatable */
>> @@ -884,11 +938,12 @@ static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
>>  
>>  static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
>>                                         int phb_index, int drc_index,
>> -                                       const char *drc_name)
>> +                                       sPAPRPHBState *sphb)
>>  {
>>      ResourceProps rp;
>>      bool is_bridge = false;
>>      int pci_status;
>> +    char *buf = NULL;
>
> Is the "= NULL" required here? If not, please remove, newer version
> of gcc tend to complain otherwise.
>
>>      if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
>>          PCI_HEADER_TYPE_BRIDGE) {
>> @@ -949,10 +1004,15 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
>>       * processed by OF beforehand
>>       */
>>      _FDT(fdt_setprop_string(fdt, offset, "name", "pci"));
>> -    if (drc_name) {
>> -        _FDT(fdt_setprop(fdt, offset, "ibm,loc-code", drc_name,
>> -                         strlen(drc_name)));
>> +    buf = spapr_phb_get_loc_code(sphb, dev);
>> +    if (!buf) {
>> +        error_report("Failed setting the ibm,loc-code");
>> +        return -1;
>>      }
>> +
>> +    _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", buf));
>
> I wonder whether this will cause some Coverity warnings later ...

Not sure about that.

> the _FDT macro can return immediately (ugh, return in a macro ... IMHO
> a bad idea...). buf is not freed in that case, and that might trigger a
> warning...

You are right buf may not get freed in case of FDT error. In this case
let me open code this macro here with proper error handling:

    err = fdt_setprop_string(fdt, offset, "ibm,loc-code", buf);
    g_free(buf);
    if (err < 0) {
        return err;
    }

Regards
Nikunj

  reply	other threads:[~2015-06-04  5:17 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-03 11:25 [Qemu-devel] [PATCH v6 0/6] spapr_pci: DT field fixes and PCI DT node creation in QEMU Nikunj A Dadhania
2015-06-03 11:25 ` [Qemu-devel] [PATCH v6 1/6] spapr_pci: encode missing 64-bit memory address space Nikunj A Dadhania
2015-06-03 11:25 ` [Qemu-devel] [PATCH v6 2/6] spapr_pci: encode class code including Prog IF register Nikunj A Dadhania
2015-06-03 11:25 ` [Qemu-devel] [PATCH v6 3/6] spapr_pci: enumerate and add PCI device tree Nikunj A Dadhania
2015-06-03 11:25 ` [Qemu-devel] [PATCH v6 4/6] spapr_pci: set device node unit address as hex Nikunj A Dadhania
2015-06-03 17:23   ` Thomas Huth
2015-06-03 11:25 ` [Qemu-devel] [PATCH v6 5/6] spapr_pci: populate ibm,loc-code Nikunj A Dadhania
2015-06-03 17:40   ` Thomas Huth
2015-06-04  5:16     ` Nikunj A Dadhania [this message]
2015-06-03 11:25 ` [Qemu-devel] [PATCH v6 6/6] spapr_pci: drop redundant args in spapr_populate_pci_child_dt Nikunj A Dadhania

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87eglsxdcg.fsf@abhimanyu.in.ibm.com \
    --to=nikunj@linux.vnet.ibm.com \
    --cc=agraf@suse.de \
    --cc=aik@ozlabs.ru \
    --cc=david@gibson.dropbear.id.au \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).