All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Williams <dan.j.williams@intel.com>
To: Ben Widawsky <ben.widawsky@intel.com>
Cc: linux-cxl@vger.kernel.org, Linux PCI <linux-pci@vger.kernel.org>,
	Alison Schofield <alison.schofield@intel.com>,
	Ira Weiny <ira.weiny@intel.com>,
	Jonathan Cameron <Jonathan.Cameron@huawei.com>,
	Vishal Verma <vishal.l.verma@intel.com>
Subject: Re: [PATCH 3/7] cxl/pci: Refactor cxl_pci_setup_regs
Date: Tue, 21 Sep 2021 16:39:30 -0700	[thread overview]
Message-ID: <CAPcyv4hK6=mxqwEZEyKH1LHBWaQKUaEoOxoAgZvL5aQ-dOswRg@mail.gmail.com> (raw)
In-Reply-To: <20210921220459.2437386-4-ben.widawsky@intel.com>

On Tue, Sep 21, 2021 at 3:05 PM Ben Widawsky <ben.widawsky@intel.com> wrote:
>
> In preparation for moving parts of register mapping to cxl_core, the
> cxl_pci driver is refactored to utilize a new helper to find register
> blocks by type.
>
> cxl_pci scanned through all register blocks and mapping the ones that
> the driver will use. This logic is inverted so that the driver
> specifically requests the register blocks from a new helper. Under the
> hood, the same implementation of scanning through all register locator
> DVSEC entries exists.
>
> There are 2 behavioral changes (#2 is arguable):
> 1. A dev_err is introduced if cxl_map_regs fails.
> 2. The previous logic would try to map component registers and device
>    registers multiple times if there were present and keep the mapping
>    of the last one found (furthest offset in the register locator).
>    While this is disallowed in the spec, CXL 2.0 8.1.9: "Each register
>    block identifier shall only occur once in the Register Locator DVSEC
>    structure" it was how the driver would respond to the spec violation.
>    The new logic will take the first found register block by type and
>    move on.

Yeah, I think it's silly to try to predict how hardware might violate
the specification. Just wait until there is a known shipping device
with a problem and then add a quirk to the driver.

>
> Signed-off-by: Ben Widawsky <ben.widawsky@intel.com>
> ---
>  drivers/cxl/pci.c | 113 ++++++++++++++++++++++++++--------------------
>  1 file changed, 65 insertions(+), 48 deletions(-)
>
> diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
> index ccc7c2573ddc..6e5c026f5262 100644
> --- a/drivers/cxl/pci.c
> +++ b/drivers/cxl/pci.c
> @@ -428,46 +428,28 @@ static void cxl_decode_register_block(u32 reg_lo, u32 reg_hi,
>         *reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
>  }
>
> -/**
> - * cxl_pci_setup_regs() - Setup necessary MMIO.
> - * @cxlm: The CXL memory device to communicate with.
> - *
> - * Return: 0 if all necessary registers mapped.
> - *
> - * A memory device is required by spec to implement a certain set of MMIO
> - * regions. The purpose of this function is to enumerate and map those
> - * registers.
> - */
> -static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
> +static int find_register_block(struct pci_dev *pdev, enum cxl_regloc_type type,
> +                              struct cxl_register_map *map)
>  {
> -       void __iomem *base;
> +       int regloc, i, rc = -ENODEV;
>         u32 regloc_size, regblocks;
> -       int regloc, i, n_maps, ret = 0;
> -       struct device *dev = cxlm->dev;
> -       struct pci_dev *pdev = to_pci_dev(dev);
> -       struct cxl_register_map *map, maps[CXL_REGLOC_RBI_TYPES];
> +
> +       memset(map, 0, sizeof(*map));

Is this necessary? It seems this fills in all fields on success, why
does it need to zero-init?

>
>         regloc = cxl_pci_dvsec(pdev, PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
> -       if (!regloc) {
> -               dev_err(dev, "register location dvsec not found\n");
> +       if (!regloc)
>                 return -ENXIO;
> -       }
> -
> -       if (pci_request_mem_regions(pdev, pci_name(pdev)))
> -               return -ENODEV;
>
> -       /* Get the size of the Register Locator DVSEC */
>         pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
>         regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
>
>         regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
>         regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
>
> -       for (i = 0, n_maps = 0; i < regblocks; i++, regloc += 8) {
> +       for (i = 0; i < regblocks; i++, regloc += 8) {
>                 u32 reg_lo, reg_hi;
> -               u8 reg_type;
> +               u8 reg_type, bar;
>                 u64 offset;
> -               u8 bar;
>
>                 pci_read_config_dword(pdev, regloc, &reg_lo);
>                 pci_read_config_dword(pdev, regloc + 4, &reg_hi);
> @@ -475,39 +457,74 @@ static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
>                 cxl_decode_register_block(reg_lo, reg_hi, &bar, &offset,
>                                           &reg_type);
>
> -               /* Ignore unknown register block types */
> -               if (reg_type > CXL_REGLOC_RBI_MEMDEV)
> -                       continue;
> +               if (reg_type == type) {
> +                       map->barno = bar;
> +                       map->block_offset = offset;
> +                       map->reg_type = reg_type;
> +                       rc = 0;
> +                       break;

As this patch is already adding helpers, perhaps rather than a loop
break, make the loop a helper so it can just "return 0;" directly:

Something like:

pci_request_mem_regions(...);
rc = __find_register_block(...);
pci_release_mem_regions(...);

...although, now that I see it written that way I think the request +
release regions should probably just be dropped. It's not like any of
the register enumeration would collide with someone else who already
has the registers mapped. The collision only comes when the registers
are mapped for their final usage, and that will have more precision in
the request.

> +               }
> +       }
>
> -               base = cxl_pci_map_regblock(cxlm, bar, offset);
> -               if (!base)
> -                       return -ENOMEM;
> +       pci_release_mem_regions(pdev);
>
> -               map = &maps[n_maps];
> -               map->barno = bar;
> -               map->block_offset = offset;
> -               map->reg_type = reg_type;
> +       return rc;
> +}
>
> -               ret = cxl_probe_regs(cxlm, base + offset, map);
> +/**
> + * cxl_pci_setup_regs() - Setup necessary MMIO.
> + * @cxlm: The CXL memory device to communicate with.
> + *
> + * Return: 0 if all necessary registers mapped.
> + *
> + * A memory device is required by spec to implement a certain set of MMIO
> + * regions. The purpose of this function is to enumerate and map those
> + * registers.
> + */
> +static int cxl_pci_setup_regs(struct cxl_mem *cxlm)
> +{
> +       int rc, i;
> +       struct device *dev = cxlm->dev;
> +       struct pci_dev *pdev = to_pci_dev(dev);
> +       const enum cxl_regloc_type types[] = { CXL_REGLOC_RBI_MEMDEV,
> +                                              CXL_REGLOC_RBI_COMPONENT };
>
> -               /* Always unmap the regblock regardless of probe success */
> -               cxl_pci_unmap_regblock(cxlm, base);
> +       if (pci_request_mem_regions(pdev, pci_name(pdev)))
> +               return -ENODEV;
>
> -               if (ret)
> -                       return ret;
> +       for (i = 0; i < ARRAY_SIZE(types); i++) {
> +               struct cxl_register_map map;
> +               void __iomem *base;
>
> -               n_maps++;
> -       }
> +               rc = find_register_block(pdev, types[i], &map);
> +               if (rc) {
> +                       dev_err(dev, "Couldn't find %s register block\n",
> +                               types[i] == CXL_REGLOC_RBI_MEMDEV ?
> +                                             "device" :
> +                                             "component");
> +                       break;
> +               }
>
> -       pci_release_mem_regions(pdev);
> +               base = cxl_pci_map_regblock(cxlm, map.barno, map.block_offset);
> +               if (!base) {
> +                       rc = -ENOMEM;
> +                       break;
> +               }
>
> -       for (i = 0; i < n_maps; i++) {
> -               ret = cxl_map_regs(cxlm, &maps[i]);
> -               if (ret)
> +               rc = cxl_probe_regs(cxlm, base + map.block_offset, &map);

It strikes me as odd @map has everything except a copy of @base. I
wonder if this patch becomes easier to read if patch4 comes before
this one and all the map_offset usage is dropped because @map can
carry the required information directly. I'm not sure this suggestion
is a win. I'm struggling to make sense of diff in isolation so will
need to circle back when I can apply this and look at the result, for
now it's just these edge comments.

  reply	other threads:[~2021-09-21 23:39 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-21 22:04 [PATCH 0/7] cxl_pci refactor for reusability Ben Widawsky
2021-09-21 22:04 ` [PATCH 1/7] cxl: Convert "RBI" to enum Ben Widawsky
2021-09-21 22:04 ` [PATCH 2/7] cxl/pci: Remove dev_dbg for unknown register blocks Ben Widawsky
2021-09-21 22:04 ` [PATCH 3/7] cxl/pci: Refactor cxl_pci_setup_regs Ben Widawsky
2021-09-21 23:39   ` Dan Williams [this message]
2021-09-22  4:31     ` Ben Widawsky
2021-09-21 22:04 ` [PATCH 4/7] cxl/pci: Make more use of cxl_register_map Ben Widawsky
2021-09-21 22:04 ` [PATCH 5/7] PCI: Add pci_find_dvsec_capability to find designated VSEC Ben Widawsky
2021-09-21 22:04   ` Ben Widawsky
2021-09-22  9:33   ` Frederic Barrat
2021-09-22  9:33     ` Frederic Barrat
2021-09-21 22:04 ` [PATCH 6/7] cxl/pci: Use pci core's DVSEC functionality Ben Widawsky
2021-09-21 22:04 ` [PATCH 7/7] ocxl: " Ben Widawsky
2021-09-21 22:04   ` Ben Widawsky
2021-09-22  0:44   ` Dan Williams
2021-09-22  0:44     ` Dan Williams
2021-09-22  9:38     ` Frederic Barrat
2021-09-22  9:38       ` Frederic Barrat
2021-09-21 22:14 ` [PATCH 0/7] cxl_pci refactor for reusability Ben Widawsky
2021-09-21 22:28 ` Dan Williams
2021-09-21 23:03   ` Ben Widawsky

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='CAPcyv4hK6=mxqwEZEyKH1LHBWaQKUaEoOxoAgZvL5aQ-dOswRg@mail.gmail.com' \
    --to=dan.j.williams@intel.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alison.schofield@intel.com \
    --cc=ben.widawsky@intel.com \
    --cc=ira.weiny@intel.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=vishal.l.verma@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.