--- /dev/null
+From bf0e5013bc2dcac205417e1252205dca39dfc005 Mon Sep 17 00:00:00 2001
+From: Florian Fainelli <f.fainelli@gmail.com>
+Date: Tue, 10 Dec 2019 10:53:47 -0800
+Subject: ata: ahci_brcm: Add missing clock management during recovery
+
+From: Florian Fainelli <f.fainelli@gmail.com>
+
+commit bf0e5013bc2dcac205417e1252205dca39dfc005 upstream.
+
+The downstream implementation of ahci_brcm.c did contain clock
+management recovery, but until recently, did that outside of the
+libahci_platform helpers and this was unintentionally stripped out while
+forward porting the patch upstream.
+
+Add the missing clock management during recovery and sleep for 10
+milliseconds per the design team recommendations to ensure the SATA PHY
+controller and AFE have been fully quiesced.
+
+Fixes: eb73390ae241 ("ata: ahci_brcm: Recover from failures to identify devices")
+Cc: stable@vger.kernel.org
+Reviewed-by: Hans de Goede <hdegoede@redhat.com>
+Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ drivers/ata/ahci_brcm.c | 7 +++++++
+ 1 file changed, 7 insertions(+)
+
+--- a/drivers/ata/ahci_brcm.c
++++ b/drivers/ata/ahci_brcm.c
+@@ -282,6 +282,13 @@ static unsigned int brcm_ahci_read_id(st
+ /* Perform the SATA PHY reset sequence */
+ brcm_sata_phy_disable(priv, ap->port_no);
+
++ /* Reset the SATA clock */
++ ahci_platform_disable_clks(hpriv);
++ msleep(10);
++
++ ahci_platform_enable_clks(hpriv);
++ msleep(10);
++
+ /* Bring the PHY back on */
+ brcm_sata_phy_enable(priv, ap->port_no);
+
--- /dev/null
+From 1a3d78cb6e20779a19388315bd8efefbd8d4a656 Mon Sep 17 00:00:00 2001
+From: Florian Fainelli <f.fainelli@gmail.com>
+Date: Tue, 10 Dec 2019 10:53:46 -0800
+Subject: ata: ahci_brcm: BCM7425 AHCI requires AHCI_HFLAG_DELAY_ENGINE
+
+From: Florian Fainelli <f.fainelli@gmail.com>
+
+commit 1a3d78cb6e20779a19388315bd8efefbd8d4a656 upstream.
+
+Set AHCI_HFLAG_DELAY_ENGINE for the BCM7425 AHCI controller thus making
+it conforming to the 'strict' AHCI implementation which this controller
+is based on.
+
+This solves long link establishment with specific hard drives (e.g.:
+Seagate ST1000VM002-9ZL1 SC12) that would otherwise have to complete the
+error recovery handling before finally establishing a succesful SATA
+link at the desired speed.
+
+We re-order the hpriv->flags assignment to also remove the NONCQ quirk
+since we can set the flag directly.
+
+Fixes: 9586114cf1e9 ("ata: ahci_brcmstb: add support MIPS-based platforms")
+Fixes: 423be77daabe ("ata: ahci_brcmstb: add quirk for broken ncq")
+Cc: stable@vger.kernel.org
+Reviewed-by: Hans de Goede <hdegoede@redhat.com>
+Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ drivers/ata/ahci_brcm.c | 31 ++++++++++++++++---------------
+ 1 file changed, 16 insertions(+), 15 deletions(-)
+
+--- a/drivers/ata/ahci_brcm.c
++++ b/drivers/ata/ahci_brcm.c
+@@ -84,8 +84,7 @@ enum brcm_ahci_version {
+ };
+
+ enum brcm_ahci_quirks {
+- BRCM_AHCI_QUIRK_NO_NCQ = BIT(0),
+- BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(1),
++ BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(0),
+ };
+
+ struct brcm_ahci_priv {
+@@ -440,18 +439,27 @@ static int brcm_ahci_probe(struct platfo
+ if (IS_ERR(priv->top_ctrl))
+ return PTR_ERR(priv->top_ctrl);
+
+- if ((priv->version == BRCM_SATA_BCM7425) ||
+- (priv->version == BRCM_SATA_NSP)) {
+- priv->quirks |= BRCM_AHCI_QUIRK_NO_NCQ;
+- priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
+- }
+-
+ hpriv = ahci_platform_get_resources(pdev, 0);
+ if (IS_ERR(hpriv)) {
+ ret = PTR_ERR(hpriv);
+ goto out_reset;
+ }
+
++ hpriv->plat_data = priv;
++ hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP | AHCI_HFLAG_NO_WRITE_TO_RO;
++
++ switch (priv->version) {
++ case BRCM_SATA_BCM7425:
++ hpriv->flags |= AHCI_HFLAG_DELAY_ENGINE;
++ /* fall through */
++ case BRCM_SATA_NSP:
++ hpriv->flags |= AHCI_HFLAG_NO_NCQ;
++ priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
++ break;
++ default:
++ break;
++ }
++
+ ret = ahci_platform_enable_clks(hpriv);
+ if (ret)
+ goto out_reset;
+@@ -471,15 +479,8 @@ static int brcm_ahci_probe(struct platfo
+ /* Must be done before ahci_platform_enable_phys() */
+ brcm_sata_phys_enable(priv);
+
+- hpriv->plat_data = priv;
+- hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP;
+-
+ brcm_sata_alpm_init(hpriv);
+
+- if (priv->quirks & BRCM_AHCI_QUIRK_NO_NCQ)
+- hpriv->flags |= AHCI_HFLAG_NO_NCQ;
+- hpriv->flags |= AHCI_HFLAG_NO_WRITE_TO_RO;
+-
+ ret = ahci_platform_enable_phys(hpriv);
+ if (ret)
+ goto out_disable_phys;
--- /dev/null
+From c0cdf2ac4b5bf3e5ef2451ea29fb4104278cdabc Mon Sep 17 00:00:00 2001
+From: Florian Fainelli <f.fainelli@gmail.com>
+Date: Tue, 10 Dec 2019 10:53:45 -0800
+Subject: ata: ahci_brcm: Fix AHCI resources management
+
+From: Florian Fainelli <f.fainelli@gmail.com>
+
+commit c0cdf2ac4b5bf3e5ef2451ea29fb4104278cdabc upstream.
+
+The AHCI resources management within ahci_brcm.c is a little
+convoluted, largely because it historically had a dedicated clock that
+was managed within this file in the downstream tree. Once brough
+upstream though, the clock was left to be managed by libahci_platform.c
+which is entirely appropriate.
+
+This patch series ensures that the AHCI resources are fetched and
+enabled before any register access is done, thus avoiding bus errors on
+platforms which clock gate the controller by default.
+
+As a result we need to re-arrange the suspend() and resume() functions
+in order to avoid accessing registers after the clocks have been turned
+off respectively before the clocks have been turned on. Finally, we can
+refactor brcm_ahci_get_portmask() in order to fetch the number of ports
+from hpriv->mmio which is now accessible without jumping through hoops
+like we used to do.
+
+The commit pointed in the Fixes tag is both old and new enough not to
+require major headaches for backporting of this patch.
+
+Fixes: eba68f829794 ("ata: ahci_brcmstb: rename to support across Broadcom SoC's")
+Cc: stable@vger.kernel.org
+Reviewed-by: Hans de Goede <hdegoede@redhat.com>
+Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ drivers/ata/ahci_brcm.c | 105 ++++++++++++++++++++++++++++++++++--------------
+ 1 file changed, 76 insertions(+), 29 deletions(-)
+
+--- a/drivers/ata/ahci_brcm.c
++++ b/drivers/ata/ahci_brcm.c
+@@ -220,19 +220,12 @@ static void brcm_sata_phys_disable(struc
+ brcm_sata_phy_disable(priv, i);
+ }
+
+-static u32 brcm_ahci_get_portmask(struct platform_device *pdev,
++static u32 brcm_ahci_get_portmask(struct ahci_host_priv *hpriv,
+ struct brcm_ahci_priv *priv)
+ {
+- void __iomem *ahci;
+- struct resource *res;
+ u32 impl;
+
+- res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ahci");
+- ahci = devm_ioremap_resource(&pdev->dev, res);
+- if (IS_ERR(ahci))
+- return 0;
+-
+- impl = readl(ahci + HOST_PORTS_IMPL);
++ impl = readl(hpriv->mmio + HOST_PORTS_IMPL);
+
+ if (fls(impl) > SATA_TOP_MAX_PHYS)
+ dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n",
+@@ -240,9 +233,6 @@ static u32 brcm_ahci_get_portmask(struct
+ else if (!impl)
+ dev_info(priv->dev, "no ports found\n");
+
+- devm_iounmap(&pdev->dev, ahci);
+- devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
+-
+ return impl;
+ }
+
+@@ -354,11 +344,10 @@ static int brcm_ahci_suspend(struct devi
+ struct ata_host *host = dev_get_drvdata(dev);
+ struct ahci_host_priv *hpriv = host->private_data;
+ struct brcm_ahci_priv *priv = hpriv->plat_data;
+- int ret;
+
+- ret = ahci_platform_suspend(dev);
+ brcm_sata_phys_disable(priv);
+- return ret;
++
++ return ahci_platform_suspend(dev);
+ }
+
+ static int brcm_ahci_resume(struct device *dev)
+@@ -366,11 +355,44 @@ static int brcm_ahci_resume(struct devic
+ struct ata_host *host = dev_get_drvdata(dev);
+ struct ahci_host_priv *hpriv = host->private_data;
+ struct brcm_ahci_priv *priv = hpriv->plat_data;
++ int ret;
++
++ /* Make sure clocks are turned on before re-configuration */
++ ret = ahci_platform_enable_clks(hpriv);
++ if (ret)
++ return ret;
+
+ brcm_sata_init(priv);
+ brcm_sata_phys_enable(priv);
+ brcm_sata_alpm_init(hpriv);
+- return ahci_platform_resume(dev);
++
++ /* Since we had to enable clocks earlier on, we cannot use
++ * ahci_platform_resume() as-is since a second call to
++ * ahci_platform_enable_resources() would bump up the resources
++ * (regulators, clocks, PHYs) count artificially so we copy the part
++ * after ahci_platform_enable_resources().
++ */
++ ret = ahci_platform_enable_phys(hpriv);
++ if (ret)
++ goto out_disable_phys;
++
++ ret = ahci_platform_resume_host(dev);
++ if (ret)
++ goto out_disable_platform_phys;
++
++ /* We resumed so update PM runtime state */
++ pm_runtime_disable(dev);
++ pm_runtime_set_active(dev);
++ pm_runtime_enable(dev);
++
++ return 0;
++
++out_disable_platform_phys:
++ ahci_platform_disable_phys(hpriv);
++out_disable_phys:
++ brcm_sata_phys_disable(priv);
++ ahci_platform_disable_clks(hpriv);
++ return ret;
+ }
+ #endif
+
+@@ -417,38 +439,63 @@ static int brcm_ahci_probe(struct platfo
+ priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
+ }
+
++ hpriv = ahci_platform_get_resources(pdev, 0);
++ if (IS_ERR(hpriv)) {
++ ret = PTR_ERR(hpriv);
++ goto out_reset;
++ }
++
++ ret = ahci_platform_enable_clks(hpriv);
++ if (ret)
++ goto out_reset;
++
++ /* Must be first so as to configure endianness including that
++ * of the standard AHCI register space.
++ */
+ brcm_sata_init(priv);
+
+- priv->port_mask = brcm_ahci_get_portmask(pdev, priv);
+- if (!priv->port_mask)
+- return -ENODEV;
++ /* Initializes priv->port_mask which is used below */
++ priv->port_mask = brcm_ahci_get_portmask(hpriv, priv);
++ if (!priv->port_mask) {
++ ret = -ENODEV;
++ goto out_disable_clks;
++ }
+
++ /* Must be done before ahci_platform_enable_phys() */
+ brcm_sata_phys_enable(priv);
+
+- hpriv = ahci_platform_get_resources(pdev, 0);
+- if (IS_ERR(hpriv))
+- return PTR_ERR(hpriv);
+ hpriv->plat_data = priv;
+ hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP;
+
+ brcm_sata_alpm_init(hpriv);
+
+- ret = ahci_platform_enable_resources(hpriv);
+- if (ret)
+- return ret;
+-
+ if (priv->quirks & BRCM_AHCI_QUIRK_NO_NCQ)
+ hpriv->flags |= AHCI_HFLAG_NO_NCQ;
+ hpriv->flags |= AHCI_HFLAG_NO_WRITE_TO_RO;
+
++ ret = ahci_platform_enable_phys(hpriv);
++ if (ret)
++ goto out_disable_phys;
++
+ ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info,
+ &ahci_platform_sht);
+ if (ret)
+- return ret;
++ goto out_disable_platform_phys;
+
+ dev_info(dev, "Broadcom AHCI SATA3 registered\n");
+
+ return 0;
++
++out_disable_platform_phys:
++ ahci_platform_disable_phys(hpriv);
++out_disable_phys:
++ brcm_sata_phys_disable(priv);
++out_disable_clks:
++ ahci_platform_disable_clks(hpriv);
++out_reset:
++ if (!IS_ERR_OR_NULL(priv->rcdev))
++ reset_control_assert(priv->rcdev);
++ return ret;
+ }
+
+ static int brcm_ahci_remove(struct platform_device *pdev)
+@@ -458,12 +505,12 @@ static int brcm_ahci_remove(struct platf
+ struct brcm_ahci_priv *priv = hpriv->plat_data;
+ int ret;
+
++ brcm_sata_phys_disable(priv);
++
+ ret = ata_platform_remove_one(pdev);
+ if (ret)
+ return ret;
+
+- brcm_sata_phys_disable(priv);
+-
+ return 0;
+ }
+
--- /dev/null
+From 84b032dbfdf1c139cd2b864e43959510646975f8 Mon Sep 17 00:00:00 2001
+From: Florian Fainelli <f.fainelli@gmail.com>
+Date: Tue, 10 Dec 2019 10:53:44 -0800
+Subject: ata: libahci_platform: Export again ahci_platform_<en/dis>able_phys()
+
+From: Florian Fainelli <f.fainelli@gmail.com>
+
+commit 84b032dbfdf1c139cd2b864e43959510646975f8 upstream.
+
+This reverts commit 6bb86fefa086faba7b60bb452300b76a47cde1a5
+("libahci_platform: Staticize ahci_platform_<en/dis>able_phys()") we are
+going to need ahci_platform_{enable,disable}_phys() in a subsequent
+commit for ahci_brcm.c in order to properly control the PHY
+initialization order.
+
+Also make sure the function prototypes are declared in
+include/linux/ahci_platform.h as a result.
+
+Cc: stable@vger.kernel.org
+Reviewed-by: Hans de Goede <hdegoede@redhat.com>
+Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ drivers/ata/libahci_platform.c | 6 ++++--
+ include/linux/ahci_platform.h | 2 ++
+ 2 files changed, 6 insertions(+), 2 deletions(-)
+
+--- a/drivers/ata/libahci_platform.c
++++ b/drivers/ata/libahci_platform.c
+@@ -47,7 +47,7 @@ EXPORT_SYMBOL_GPL(ahci_platform_ops);
+ * RETURNS:
+ * 0 on success otherwise a negative error code
+ */
+-static int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
++int ahci_platform_enable_phys(struct ahci_host_priv *hpriv)
+ {
+ int rc, i;
+
+@@ -72,6 +72,7 @@ disable_phys:
+ }
+ return rc;
+ }
++EXPORT_SYMBOL_GPL(ahci_platform_enable_phys);
+
+ /**
+ * ahci_platform_disable_phys - Disable PHYs
+@@ -79,7 +80,7 @@ disable_phys:
+ *
+ * This function disables all PHYs found in hpriv->phys.
+ */
+-static void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
++void ahci_platform_disable_phys(struct ahci_host_priv *hpriv)
+ {
+ int i;
+
+@@ -88,6 +89,7 @@ static void ahci_platform_disable_phys(s
+ phy_exit(hpriv->phys[i]);
+ }
+ }
++EXPORT_SYMBOL_GPL(ahci_platform_disable_phys);
+
+ /**
+ * ahci_platform_enable_clks - Enable platform clocks
+--- a/include/linux/ahci_platform.h
++++ b/include/linux/ahci_platform.h
+@@ -23,6 +23,8 @@ struct ahci_host_priv;
+ struct platform_device;
+ struct scsi_host_template;
+
++int ahci_platform_enable_phys(struct ahci_host_priv *hpriv);
++void ahci_platform_disable_phys(struct ahci_host_priv *hpriv);
+ int ahci_platform_enable_clks(struct ahci_host_priv *hpriv);
+ void ahci_platform_disable_clks(struct ahci_host_priv *hpriv);
+ int ahci_platform_enable_regulators(struct ahci_host_priv *hpriv);
--- /dev/null
+From 256efaea1fdc4e38970489197409a26125ee0aaa Mon Sep 17 00:00:00 2001
+From: Russell King <rmk+kernel@armlinux.org.uk>
+Date: Sat, 7 Dec 2019 16:20:18 +0000
+Subject: gpiolib: fix up emulated open drain outputs
+
+From: Russell King <rmk+kernel@armlinux.org.uk>
+
+commit 256efaea1fdc4e38970489197409a26125ee0aaa upstream.
+
+gpiolib has a corner case with open drain outputs that are emulated.
+When such outputs are outputting a logic 1, emulation will set the
+hardware to input mode, which will cause gpiod_get_direction() to
+report that it is in input mode. This is different from the behaviour
+with a true open-drain output.
+
+Unify the semantics here.
+
+Cc: <stable@vger.kernel.org>
+Suggested-by: Linus Walleij <linus.walleij@linaro.org>
+Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
+Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ drivers/gpio/gpiolib.c | 8 ++++++++
+ 1 file changed, 8 insertions(+)
+
+--- a/drivers/gpio/gpiolib.c
++++ b/drivers/gpio/gpiolib.c
+@@ -217,6 +217,14 @@ int gpiod_get_direction(struct gpio_desc
+ chip = gpiod_to_chip(desc);
+ offset = gpio_chip_hwgpio(desc);
+
++ /*
++ * Open drain emulation using input mode may incorrectly report
++ * input here, fix that up.
++ */
++ if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
++ test_bit(FLAG_IS_OUT, &desc->flags))
++ return 0;
++
+ if (!chip->get_direction)
+ return status;
+
--- /dev/null
+From 8385d756e114f2df8568e508902d5f9850817ffb Mon Sep 17 00:00:00 2001
+From: Sascha Hauer <s.hauer@pengutronix.de>
+Date: Fri, 13 Dec 2019 09:04:08 +0100
+Subject: libata: Fix retrieving of active qcs
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+From: Sascha Hauer <s.hauer@pengutronix.de>
+
+commit 8385d756e114f2df8568e508902d5f9850817ffb upstream.
+
+ata_qc_complete_multiple() is called with a mask of the still active
+tags.
+
+mv_sata doesn't have this information directly and instead calculates
+the still active tags from the started tags (ap->qc_active) and the
+finished tags as (ap->qc_active ^ done_mask)
+
+Since 28361c40368 the hw_tag and tag are no longer the same and the
+equation is no longer valid. In ata_exec_internal_sg() ap->qc_active is
+initialized as 1ULL << ATA_TAG_INTERNAL, but in hardware tag 0 is
+started and this will be in done_mask on completion. ap->qc_active ^
+done_mask becomes 0x100000000 ^ 0x1 = 0x100000001 and thus tag 0 used as
+the internal tag will never be reported as completed.
+
+This is fixed by introducing ata_qc_get_active() which returns the
+active hardware tags and calling it where appropriate.
+
+This is tested on mv_sata, but sata_fsl and sata_nv suffer from the same
+problem. There is another case in sata_nv that most likely needs fixing
+as well, but this looks a little different, so I wasn't confident enough
+to change that.
+
+Fixes: 28361c403683 ("libata: add extra internal command")
+Cc: stable@vger.kernel.org
+Tested-by: Pali Rohár <pali.rohar@gmail.com>
+Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+Add missing export of ata_qc_get_active(), as per Pali.
+
+Signed-off-by: Jens Axboe <axboe@kernel.dk>
+
+---
+ drivers/ata/libata-core.c | 24 ++++++++++++++++++++++++
+ drivers/ata/sata_fsl.c | 2 +-
+ drivers/ata/sata_mv.c | 2 +-
+ drivers/ata/sata_nv.c | 2 +-
+ include/linux/libata.h | 1 +
+ 5 files changed, 28 insertions(+), 3 deletions(-)
+
+--- a/drivers/ata/libata-core.c
++++ b/drivers/ata/libata-core.c
+@@ -5344,6 +5344,30 @@ void ata_qc_complete(struct ata_queued_c
+ }
+
+ /**
++ * ata_qc_get_active - get bitmask of active qcs
++ * @ap: port in question
++ *
++ * LOCKING:
++ * spin_lock_irqsave(host lock)
++ *
++ * RETURNS:
++ * Bitmask of active qcs
++ */
++u64 ata_qc_get_active(struct ata_port *ap)
++{
++ u64 qc_active = ap->qc_active;
++
++ /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
++ if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
++ qc_active |= (1 << 0);
++ qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
++ }
++
++ return qc_active;
++}
++EXPORT_SYMBOL_GPL(ata_qc_get_active);
++
++/**
+ * ata_qc_complete_multiple - Complete multiple qcs successfully
+ * @ap: port in question
+ * @qc_active: new qc_active mask
+--- a/drivers/ata/sata_fsl.c
++++ b/drivers/ata/sata_fsl.c
+@@ -1283,7 +1283,7 @@ static void sata_fsl_host_intr(struct at
+ i, ioread32(hcr_base + CC),
+ ioread32(hcr_base + CA));
+ }
+- ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
++ ata_qc_complete_multiple(ap, ata_qc_get_active(ap) ^ done_mask);
+ return;
+
+ } else if ((ap->qc_active & (1ULL << ATA_TAG_INTERNAL))) {
+--- a/drivers/ata/sata_mv.c
++++ b/drivers/ata/sata_mv.c
+@@ -2840,7 +2840,7 @@ static void mv_process_crpb_entries(stru
+ }
+
+ if (work_done) {
+- ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
++ ata_qc_complete_multiple(ap, ata_qc_get_active(ap) ^ done_mask);
+
+ /* Update the software queue position index in hardware */
+ writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
+--- a/drivers/ata/sata_nv.c
++++ b/drivers/ata/sata_nv.c
+@@ -1000,7 +1000,7 @@ static irqreturn_t nv_adma_interrupt(int
+ check_commands = 0;
+ check_commands &= ~(1 << pos);
+ }
+- ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
++ ata_qc_complete_multiple(ap, ata_qc_get_active(ap) ^ done_mask);
+ }
+ }
+
+--- a/include/linux/libata.h
++++ b/include/linux/libata.h
+@@ -1190,6 +1190,7 @@ extern unsigned int ata_do_dev_read_id(s
+ struct ata_taskfile *tf, u16 *id);
+ extern void ata_qc_complete(struct ata_queued_cmd *qc);
+ extern int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active);
++extern u64 ata_qc_get_active(struct ata_port *ap);
+ extern void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd);
+ extern int ata_std_bios_param(struct scsi_device *sdev,
+ struct block_device *bdev,
--- /dev/null
+From 1d8f65798240b6577d8c44d20c8ea8f1d429e495 Mon Sep 17 00:00:00 2001
+From: Zong Li <zong.li@sifive.com>
+Date: Mon, 23 Dec 2019 16:46:13 +0800
+Subject: riscv: ftrace: correct the condition logic in function graph tracer
+
+From: Zong Li <zong.li@sifive.com>
+
+commit 1d8f65798240b6577d8c44d20c8ea8f1d429e495 upstream.
+
+The condition should be logical NOT to assign the hook address to parent
+address. Because the return value 0 of function_graph_enter upon
+success.
+
+Fixes: e949b6db51dc (riscv/function_graph: Simplify with function_graph_enter())
+Signed-off-by: Zong Li <zong.li@sifive.com>
+Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
+Cc: stable@vger.kernel.org
+Signed-off-by: Paul Walmsley <paul.walmsley@sifive.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ arch/riscv/kernel/ftrace.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+--- a/arch/riscv/kernel/ftrace.c
++++ b/arch/riscv/kernel/ftrace.c
+@@ -142,7 +142,7 @@ void prepare_ftrace_return(unsigned long
+ */
+ old = *parent;
+
+- if (function_graph_enter(old, self_addr, frame_pointer, parent))
++ if (!function_graph_enter(old, self_addr, frame_pointer, parent))
+ *parent = return_hooker;
+ }
+
--- /dev/null
+From 8df34c56321479bfa1ec732c675b686c2b4df412 Mon Sep 17 00:00:00 2001
+From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+Date: Wed, 11 Dec 2019 11:17:13 -0500
+Subject: rseq/selftests: Fix: Namespace gettid() for compatibility with glibc 2.30
+
+From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+
+commit 8df34c56321479bfa1ec732c675b686c2b4df412 upstream.
+
+glibc 2.30 introduces gettid() in public headers, which clashes with
+the internal static definition within rseq selftests.
+
+Rename gettid() to rseq_gettid() to eliminate this symbol name clash.
+
+Reported-by: Tommi T. Rantala <tommi.t.rantala@nokia.com>
+Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+Cc: Shuah Khan <skhan@linuxfoundation.org>
+Cc: Tommi T. Rantala <tommi.t.rantala@nokia.com>
+Cc: Thomas Gleixner <tglx@linutronix.de>
+Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
+Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
+Cc: Boqun Feng <boqun.feng@gmail.com>
+Cc: "H . Peter Anvin" <hpa@zytor.com>
+Cc: Paul Turner <pjt@google.com>
+Cc: Dmitry Vyukov <dvyukov@google.com>
+Cc: <stable@vger.kernel.org> # v4.18+
+Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ tools/testing/selftests/rseq/param_test.c | 18 ++++++++++--------
+ 1 file changed, 10 insertions(+), 8 deletions(-)
+
+--- a/tools/testing/selftests/rseq/param_test.c
++++ b/tools/testing/selftests/rseq/param_test.c
+@@ -15,7 +15,7 @@
+ #include <errno.h>
+ #include <stddef.h>
+
+-static inline pid_t gettid(void)
++static inline pid_t rseq_gettid(void)
+ {
+ return syscall(__NR_gettid);
+ }
+@@ -373,11 +373,12 @@ void *test_percpu_spinlock_thread(void *
+ rseq_percpu_unlock(&data->lock, cpu);
+ #ifndef BENCHMARK
+ if (i != 0 && !(i % (reps / 10)))
+- printf_verbose("tid %d: count %lld\n", (int) gettid(), i);
++ printf_verbose("tid %d: count %lld\n",
++ (int) rseq_gettid(), i);
+ #endif
+ }
+ printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
+- (int) gettid(), nr_abort, signals_delivered);
++ (int) rseq_gettid(), nr_abort, signals_delivered);
+ if (!opt_disable_rseq && thread_data->reg &&
+ rseq_unregister_current_thread())
+ abort();
+@@ -454,11 +455,12 @@ void *test_percpu_inc_thread(void *arg)
+ } while (rseq_unlikely(ret));
+ #ifndef BENCHMARK
+ if (i != 0 && !(i % (reps / 10)))
+- printf_verbose("tid %d: count %lld\n", (int) gettid(), i);
++ printf_verbose("tid %d: count %lld\n",
++ (int) rseq_gettid(), i);
+ #endif
+ }
+ printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
+- (int) gettid(), nr_abort, signals_delivered);
++ (int) rseq_gettid(), nr_abort, signals_delivered);
+ if (!opt_disable_rseq && thread_data->reg &&
+ rseq_unregister_current_thread())
+ abort();
+@@ -605,7 +607,7 @@ void *test_percpu_list_thread(void *arg)
+ }
+
+ printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
+- (int) gettid(), nr_abort, signals_delivered);
++ (int) rseq_gettid(), nr_abort, signals_delivered);
+ if (!opt_disable_rseq && rseq_unregister_current_thread())
+ abort();
+
+@@ -796,7 +798,7 @@ void *test_percpu_buffer_thread(void *ar
+ }
+
+ printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
+- (int) gettid(), nr_abort, signals_delivered);
++ (int) rseq_gettid(), nr_abort, signals_delivered);
+ if (!opt_disable_rseq && rseq_unregister_current_thread())
+ abort();
+
+@@ -1011,7 +1013,7 @@ void *test_percpu_memcpy_buffer_thread(v
+ }
+
+ printf_verbose("tid %d: number of rseq abort: %d, signals delivered: %u\n",
+- (int) gettid(), nr_abort, signals_delivered);
++ (int) rseq_gettid(), nr_abort, signals_delivered);
+ if (!opt_disable_rseq && rseq_unregister_current_thread())
+ abort();
+
dmaengine-fix-access-to-uninitialized-dma_slave_caps.patch
compat_ioctl-block-handle-persistent-reservations.patch
compat_ioctl-block-handle-blkreportzone-blkresetzone.patch
+ata-libahci_platform-export-again-ahci_platform_-en-dis-able_phys.patch
+ata-ahci_brcm-fix-ahci-resources-management.patch
+ata-ahci_brcm-add-missing-clock-management-during-recovery.patch
+ata-ahci_brcm-bcm7425-ahci-requires-ahci_hflag_delay_engine.patch
+libata-fix-retrieving-of-active-qcs.patch
+gpiolib-fix-up-emulated-open-drain-outputs.patch
+riscv-ftrace-correct-the-condition-logic-in-function-graph-tracer.patch
+rseq-selftests-fix-namespace-gettid-for-compatibility-with-glibc-2.30.patch
+tracing-fix-lock-inversion-in-trace_event_enable_tgid_record.patch
+tracing-avoid-memory-leak-in-process_system_preds.patch
+tracing-have-the-histogram-compare-functions-convert-to-u64-first.patch
+tracing-fix-endianness-bug-in-histogram-trigger.patch
--- /dev/null
+From 79e65c27f09683fbb50c33acab395d0ddf5302d2 Mon Sep 17 00:00:00 2001
+From: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
+Date: Wed, 11 Dec 2019 09:12:58 +0000
+Subject: tracing: Avoid memory leak in process_system_preds()
+
+From: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
+
+commit 79e65c27f09683fbb50c33acab395d0ddf5302d2 upstream.
+
+When failing in the allocation of filter_item, process_system_preds()
+goes to fail_mem, where the allocated filter is freed.
+
+However, this leads to memory leak of filter->filter_string and
+filter->prog, which is allocated before and in process_preds().
+This bug has been detected by kmemleak as well.
+
+Fix this by changing kfree to __free_fiter.
+
+unreferenced object 0xffff8880658007c0 (size 32):
+ comm "bash", pid 579, jiffies 4295096372 (age 17.752s)
+ hex dump (first 32 bytes):
+ 63 6f 6d 6d 6f 6e 5f 70 69 64 20 20 3e 20 31 30 common_pid > 10
+ 00 00 00 00 00 00 00 00 65 73 00 00 00 00 00 00 ........es......
+ backtrace:
+ [<0000000067441602>] kstrdup+0x2d/0x60
+ [<00000000141cf7b7>] apply_subsystem_event_filter+0x378/0x932
+ [<000000009ca32334>] subsystem_filter_write+0x5a/0x90
+ [<0000000072da2bee>] vfs_write+0xe1/0x240
+ [<000000004f14f473>] ksys_write+0xb4/0x150
+ [<00000000a968b4a0>] do_syscall_64+0x6d/0x1e0
+ [<000000001a189f40>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
+unreferenced object 0xffff888060c22d00 (size 64):
+ comm "bash", pid 579, jiffies 4295096372 (age 17.752s)
+ hex dump (first 32 bytes):
+ 01 00 00 00 00 00 00 00 00 e8 d7 41 80 88 ff ff ...........A....
+ 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+ backtrace:
+ [<00000000b8c1b109>] process_preds+0x243/0x1820
+ [<000000003972c7f0>] apply_subsystem_event_filter+0x3be/0x932
+ [<000000009ca32334>] subsystem_filter_write+0x5a/0x90
+ [<0000000072da2bee>] vfs_write+0xe1/0x240
+ [<000000004f14f473>] ksys_write+0xb4/0x150
+ [<00000000a968b4a0>] do_syscall_64+0x6d/0x1e0
+ [<000000001a189f40>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
+unreferenced object 0xffff888041d7e800 (size 512):
+ comm "bash", pid 579, jiffies 4295096372 (age 17.752s)
+ hex dump (first 32 bytes):
+ 70 bc 85 97 ff ff ff ff 0a 00 00 00 00 00 00 00 p...............
+ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+ backtrace:
+ [<000000001e04af34>] process_preds+0x71a/0x1820
+ [<000000003972c7f0>] apply_subsystem_event_filter+0x3be/0x932
+ [<000000009ca32334>] subsystem_filter_write+0x5a/0x90
+ [<0000000072da2bee>] vfs_write+0xe1/0x240
+ [<000000004f14f473>] ksys_write+0xb4/0x150
+ [<00000000a968b4a0>] do_syscall_64+0x6d/0x1e0
+ [<000000001a189f40>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
+
+Link: http://lkml.kernel.org/r/20191211091258.11310-1-keitasuzuki.park@sslab.ics.keio.ac.jp
+
+Cc: Ingo Molnar <mingo@redhat.com>
+Cc: stable@vger.kernel.org
+Fixes: 404a3add43c9c ("tracing: Only add filter list when needed")
+Signed-off-by: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
+Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ kernel/trace/trace_events_filter.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+--- a/kernel/trace/trace_events_filter.c
++++ b/kernel/trace/trace_events_filter.c
+@@ -1642,7 +1642,7 @@ static int process_system_preds(struct t
+ parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
+ return -EINVAL;
+ fail_mem:
+- kfree(filter);
++ __free_filter(filter);
+ /* If any call succeeded, we still need to sync */
+ if (!fail)
+ tracepoint_synchronize_unregister();
--- /dev/null
+From fe6e096a5bbf73a142f09c72e7aa2835026eb1a3 Mon Sep 17 00:00:00 2001
+From: Sven Schnelle <svens@linux.ibm.com>
+Date: Wed, 18 Dec 2019 08:44:27 +0100
+Subject: tracing: Fix endianness bug in histogram trigger
+
+From: Sven Schnelle <svens@linux.ibm.com>
+
+commit fe6e096a5bbf73a142f09c72e7aa2835026eb1a3 upstream.
+
+At least on PA-RISC and s390 synthetic histogram triggers are failing
+selftests because trace_event_raw_event_synth() always writes a 64 bit
+values, but the reader expects a field->size sized value. On little endian
+machines this doesn't hurt, but on big endian this makes the reader always
+read zero values.
+
+Link: http://lore.kernel.org/linux-trace-devel/20191218074427.96184-4-svens@linux.ibm.com
+
+Cc: stable@vger.kernel.org
+Fixes: 4b147936fa509 ("tracing: Add support for 'synthetic' events")
+Acked-by: Tom Zanussi <tom.zanussi@linux.intel.com>
+Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
+Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ kernel/trace/trace_events_hist.c | 21 ++++++++++++++++++++-
+ 1 file changed, 20 insertions(+), 1 deletion(-)
+
+--- a/kernel/trace/trace_events_hist.c
++++ b/kernel/trace/trace_events_hist.c
+@@ -665,7 +665,26 @@ static notrace void trace_event_raw_even
+ strscpy(str_field, str_val, STR_VAR_LEN_MAX);
+ n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
+ } else {
+- entry->fields[n_u64] = var_ref_vals[var_ref_idx + i];
++ struct synth_field *field = event->fields[i];
++ u64 val = var_ref_vals[var_ref_idx + i];
++
++ switch (field->size) {
++ case 1:
++ *(u8 *)&entry->fields[n_u64] = (u8)val;
++ break;
++
++ case 2:
++ *(u16 *)&entry->fields[n_u64] = (u16)val;
++ break;
++
++ case 4:
++ *(u32 *)&entry->fields[n_u64] = (u32)val;
++ break;
++
++ default:
++ entry->fields[n_u64] = val;
++ break;
++ }
+ n_u64++;
+ }
+ }
--- /dev/null
+From 3a53acf1d9bea11b57c1f6205e3fe73f9d8a3688 Mon Sep 17 00:00:00 2001
+From: Prateek Sood <prsood@codeaurora.org>
+Date: Tue, 10 Dec 2019 09:15:16 +0000
+Subject: tracing: Fix lock inversion in trace_event_enable_tgid_record()
+
+From: Prateek Sood <prsood@codeaurora.org>
+
+commit 3a53acf1d9bea11b57c1f6205e3fe73f9d8a3688 upstream.
+
+ Task T2 Task T3
+trace_options_core_write() subsystem_open()
+
+ mutex_lock(trace_types_lock) mutex_lock(event_mutex)
+
+ set_tracer_flag()
+
+ trace_event_enable_tgid_record() mutex_lock(trace_types_lock)
+
+ mutex_lock(event_mutex)
+
+This gives a circular dependency deadlock between trace_types_lock and
+event_mutex. To fix this invert the usage of trace_types_lock and
+event_mutex in trace_options_core_write(). This keeps the sequence of
+lock usage consistent.
+
+Link: http://lkml.kernel.org/r/0101016eef175e38-8ca71caf-a4eb-480d-a1e6-6f0bbc015495-000000@us-west-2.amazonses.com
+
+Cc: stable@vger.kernel.org
+Fixes: d914ba37d7145 ("tracing: Add support for recording tgid of tasks")
+Signed-off-by: Prateek Sood <prsood@codeaurora.org>
+Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ kernel/trace/trace.c | 8 ++++++++
+ kernel/trace/trace_events.c | 8 ++++----
+ 2 files changed, 12 insertions(+), 4 deletions(-)
+
+--- a/kernel/trace/trace.c
++++ b/kernel/trace/trace.c
+@@ -4370,6 +4370,10 @@ int trace_keep_overwrite(struct tracer *
+
+ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
+ {
++ if ((mask == TRACE_ITER_RECORD_TGID) ||
++ (mask == TRACE_ITER_RECORD_CMD))
++ lockdep_assert_held(&event_mutex);
++
+ /* do nothing if flag is already set */
+ if (!!(tr->trace_flags & mask) == !!enabled)
+ return 0;
+@@ -4435,6 +4439,7 @@ static int trace_set_options(struct trac
+ cmp += 2;
+ }
+
++ mutex_lock(&event_mutex);
+ mutex_lock(&trace_types_lock);
+
+ ret = match_string(trace_options, -1, cmp);
+@@ -4445,6 +4450,7 @@ static int trace_set_options(struct trac
+ ret = set_tracer_flag(tr, 1 << ret, !neg);
+
+ mutex_unlock(&trace_types_lock);
++ mutex_unlock(&event_mutex);
+
+ /*
+ * If the first trailing whitespace is replaced with '\0' by strstrip,
+@@ -7457,9 +7463,11 @@ trace_options_core_write(struct file *fi
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
++ mutex_lock(&event_mutex);
+ mutex_lock(&trace_types_lock);
+ ret = set_tracer_flag(tr, 1 << index, val);
+ mutex_unlock(&trace_types_lock);
++ mutex_unlock(&event_mutex);
+
+ if (ret < 0)
+ return ret;
+--- a/kernel/trace/trace_events.c
++++ b/kernel/trace/trace_events.c
+@@ -327,7 +327,8 @@ void trace_event_enable_cmd_record(bool
+ struct trace_event_file *file;
+ struct trace_array *tr;
+
+- mutex_lock(&event_mutex);
++ lockdep_assert_held(&event_mutex);
++
+ do_for_each_event_file(tr, file) {
+
+ if (!(file->flags & EVENT_FILE_FL_ENABLED))
+@@ -341,7 +342,6 @@ void trace_event_enable_cmd_record(bool
+ clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
+ }
+ } while_for_each_event_file();
+- mutex_unlock(&event_mutex);
+ }
+
+ void trace_event_enable_tgid_record(bool enable)
+@@ -349,7 +349,8 @@ void trace_event_enable_tgid_record(bool
+ struct trace_event_file *file;
+ struct trace_array *tr;
+
+- mutex_lock(&event_mutex);
++ lockdep_assert_held(&event_mutex);
++
+ do_for_each_event_file(tr, file) {
+ if (!(file->flags & EVENT_FILE_FL_ENABLED))
+ continue;
+@@ -363,7 +364,6 @@ void trace_event_enable_tgid_record(bool
+ &file->flags);
+ }
+ } while_for_each_event_file();
+- mutex_unlock(&event_mutex);
+ }
+
+ static int __ftrace_event_enable_disable(struct trace_event_file *file,
--- /dev/null
+From 106f41f5a302cb1f36c7543fae6a05de12e96fa4 Mon Sep 17 00:00:00 2001
+From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
+Date: Wed, 11 Dec 2019 15:44:22 -0500
+Subject: tracing: Have the histogram compare functions convert to u64 first
+
+From: Steven Rostedt (VMware) <rostedt@goodmis.org>
+
+commit 106f41f5a302cb1f36c7543fae6a05de12e96fa4 upstream.
+
+The compare functions of the histogram code would be specific for the size
+of the value being compared (byte, short, int, long long). It would
+reference the value from the array via the type of the compare, but the
+value was stored in a 64 bit number. This is fine for little endian
+machines, but for big endian machines, it would end up comparing zeros or
+all ones (depending on the sign) for anything but 64 bit numbers.
+
+To fix this, first derference the value as a u64 then convert it to the type
+being compared.
+
+Link: http://lkml.kernel.org/r/20191211103557.7bed6928@gandalf.local.home
+
+Cc: stable@vger.kernel.org
+Fixes: 08d43a5fa063e ("tracing: Add lock-free tracing_map")
+Acked-by: Tom Zanussi <zanussi@kernel.org>
+Reported-by: Sven Schnelle <svens@stackframe.org>
+Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ kernel/trace/tracing_map.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+--- a/kernel/trace/tracing_map.c
++++ b/kernel/trace/tracing_map.c
+@@ -148,8 +148,8 @@ static int tracing_map_cmp_atomic64(void
+ #define DEFINE_TRACING_MAP_CMP_FN(type) \
+ static int tracing_map_cmp_##type(void *val_a, void *val_b) \
+ { \
+- type a = *(type *)val_a; \
+- type b = *(type *)val_b; \
++ type a = (type)(*(u64 *)val_a); \
++ type b = (type)(*(u64 *)val_b); \
+ \
+ return (a > b) ? 1 : ((a < b) ? -1 : 0); \
+ }