--- /dev/null
+From a1a3b762163868ad07a4499a73df324f40d5ab0b Mon Sep 17 00:00:00 2001
+From: Hans de Goede <hdegoede@redhat.com>
+Date: Wed, 10 Oct 2018 13:00:58 +0200
+Subject: [PATCH] brcmfmac: Remove firmware-loading code duplication
+
+brcmf_fw_request_next_item and brcmf_fw_request_done both have identical
+code to complete the fw-request depending on the item-type.
+
+This commit adds a new brcmf_fw_complete_request helper removing this code
+duplication.
+
+Signed-off-by: Hans de Goede <hdegoede@redhat.com>
+Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
+---
+ .../broadcom/brcm80211/brcmfmac/firmware.c | 62 +++++++++++-----------
+ 1 file changed, 31 insertions(+), 31 deletions(-)
+
+--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
++++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
+@@ -504,6 +504,34 @@ fail:
+ return -ENOENT;
+ }
+
++static int brcmf_fw_complete_request(const struct firmware *fw,
++ struct brcmf_fw *fwctx)
++{
++ struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos];
++ int ret = 0;
++
++ brcmf_dbg(TRACE, "firmware %s %sfound\n", cur->path, fw ? "" : "not ");
++
++ switch (cur->type) {
++ case BRCMF_FW_TYPE_NVRAM:
++ ret = brcmf_fw_request_nvram_done(fw, fwctx);
++ break;
++ case BRCMF_FW_TYPE_BINARY:
++ if (fw)
++ cur->binary = fw;
++ else
++ ret = -ENOENT;
++ break;
++ default:
++ /* something fishy here so bail out early */
++ brcmf_err("unknown fw type: %d\n", cur->type);
++ release_firmware(fw);
++ ret = -EINVAL;
++ }
++
++ return (cur->flags & BRCMF_FW_REQF_OPTIONAL) ? 0 : ret;
++}
++
+ static int brcmf_fw_request_next_item(struct brcmf_fw *fwctx, bool async)
+ {
+ struct brcmf_fw_item *cur;
+@@ -525,15 +553,7 @@ static int brcmf_fw_request_next_item(st
+ if (ret < 0) {
+ brcmf_fw_request_done(NULL, fwctx);
+ } else if (!async && fw) {
+- brcmf_dbg(TRACE, "firmware %s %sfound\n", cur->path,
+- fw ? "" : "not ");
+- if (cur->type == BRCMF_FW_TYPE_BINARY)
+- cur->binary = fw;
+- else if (cur->type == BRCMF_FW_TYPE_NVRAM)
+- brcmf_fw_request_nvram_done(fw, fwctx);
+- else
+- release_firmware(fw);
+-
++ brcmf_fw_complete_request(fw, fwctx);
+ return -EAGAIN;
+ }
+ return 0;
+@@ -547,28 +567,8 @@ static void brcmf_fw_request_done(const
+
+ cur = &fwctx->req->items[fwctx->curpos];
+
+- brcmf_dbg(TRACE, "enter: firmware %s %sfound\n", cur->path,
+- fw ? "" : "not ");
+-
+- if (!fw)
+- ret = -ENOENT;
+-
+- switch (cur->type) {
+- case BRCMF_FW_TYPE_NVRAM:
+- ret = brcmf_fw_request_nvram_done(fw, fwctx);
+- break;
+- case BRCMF_FW_TYPE_BINARY:
+- cur->binary = fw;
+- break;
+- default:
+- /* something fishy here so bail out early */
+- brcmf_err("unknown fw type: %d\n", cur->type);
+- release_firmware(fw);
+- ret = -EINVAL;
+- goto fail;
+- }
+-
+- if (ret < 0 && !(cur->flags & BRCMF_FW_REQF_OPTIONAL))
++ ret = brcmf_fw_complete_request(fw, fwctx);
++ if (ret < 0)
+ goto fail;
+
+ do {
--- /dev/null
+From 5b587496dc63595b71265d986ce69728c2724370 Mon Sep 17 00:00:00 2001
+From: Hans de Goede <hdegoede@redhat.com>
+Date: Wed, 10 Oct 2018 13:00:59 +0200
+Subject: [PATCH] brcmfmac: Remove recursion from firmware load error handling
+
+Before this commit brcmf_fw_request_done would call
+brcmf_fw_request_next_item to load the next item, which on an error would
+call brcmf_fw_request_done, which if the error is recoverable (*) will
+then continue calling brcmf_fw_request_next_item for the next item again
+which on an error will call brcmf_fw_request_done again...
+
+This does not blow up because we only have a limited number of items so
+we never recurse too deep. But the recursion is still quite ugly and
+frankly is giving me a headache, so lets fix this.
+
+This commit fixes this by removing brcmf_fw_request_next_item and by
+making brcmf_fw_get_firmwares and brcmf_fw_request_done directly call
+firmware_request_nowait resp. firmware_request themselves.
+
+*) brcmf_fw_request_nvram_done fallback path succeeds or
+ BRCMF_FW_REQF_OPTIONAL is set
+
+Signed-off-by: Hans de Goede <hdegoede@redhat.com>
+Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
+---
+ .../broadcom/brcm80211/brcmfmac/firmware.c | 65 +++++++---------------
+ 1 file changed, 19 insertions(+), 46 deletions(-)
+
+--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
++++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
+@@ -532,33 +532,6 @@ static int brcmf_fw_complete_request(con
+ return (cur->flags & BRCMF_FW_REQF_OPTIONAL) ? 0 : ret;
+ }
+
+-static int brcmf_fw_request_next_item(struct brcmf_fw *fwctx, bool async)
+-{
+- struct brcmf_fw_item *cur;
+- const struct firmware *fw = NULL;
+- int ret;
+-
+- cur = &fwctx->req->items[fwctx->curpos];
+-
+- brcmf_dbg(TRACE, "%srequest for %s\n", async ? "async " : "",
+- cur->path);
+-
+- if (async)
+- ret = request_firmware_nowait(THIS_MODULE, true, cur->path,
+- fwctx->dev, GFP_KERNEL, fwctx,
+- brcmf_fw_request_done);
+- else
+- ret = request_firmware(&fw, cur->path, fwctx->dev);
+-
+- if (ret < 0) {
+- brcmf_fw_request_done(NULL, fwctx);
+- } else if (!async && fw) {
+- brcmf_fw_complete_request(fw, fwctx);
+- return -EAGAIN;
+- }
+- return 0;
+-}
+-
+ static void brcmf_fw_request_done(const struct firmware *fw, void *ctx)
+ {
+ struct brcmf_fw *fwctx = ctx;
+@@ -568,26 +541,19 @@ static void brcmf_fw_request_done(const
+ cur = &fwctx->req->items[fwctx->curpos];
+
+ ret = brcmf_fw_complete_request(fw, fwctx);
+- if (ret < 0)
+- goto fail;
+
+- do {
+- if (++fwctx->curpos == fwctx->req->n_items) {
+- ret = 0;
+- goto done;
+- }
+-
+- ret = brcmf_fw_request_next_item(fwctx, false);
+- } while (ret == -EAGAIN);
+-
+- return;
+-
+-fail:
+- brcmf_dbg(TRACE, "failed err=%d: dev=%s, fw=%s\n", ret,
+- dev_name(fwctx->dev), cur->path);
+- brcmf_fw_free_request(fwctx->req);
+- fwctx->req = NULL;
+-done:
++ while (ret == 0 && ++fwctx->curpos < fwctx->req->n_items) {
++ cur = &fwctx->req->items[fwctx->curpos];
++ request_firmware(&fw, cur->path, fwctx->dev);
++ ret = brcmf_fw_complete_request(fw, ctx);
++ }
++
++ if (ret) {
++ brcmf_dbg(TRACE, "failed err=%d: dev=%s, fw=%s\n", ret,
++ dev_name(fwctx->dev), cur->path);
++ brcmf_fw_free_request(fwctx->req);
++ fwctx->req = NULL;
++ }
+ fwctx->done(fwctx->dev, ret, fwctx->req);
+ kfree(fwctx);
+ }
+@@ -611,7 +577,9 @@ int brcmf_fw_get_firmwares(struct device
+ void (*fw_cb)(struct device *dev, int err,
+ struct brcmf_fw_request *req))
+ {
++ struct brcmf_fw_item *first = &req->items[0];
+ struct brcmf_fw *fwctx;
++ int ret;
+
+ brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
+ if (!fw_cb)
+@@ -628,7 +596,12 @@ int brcmf_fw_get_firmwares(struct device
+ fwctx->req = req;
+ fwctx->done = fw_cb;
+
+- brcmf_fw_request_next_item(fwctx, true);
++ ret = request_firmware_nowait(THIS_MODULE, true, first->path,
++ fwctx->dev, GFP_KERNEL, fwctx,
++ brcmf_fw_request_done);
++ if (ret < 0)
++ brcmf_fw_request_done(NULL, fwctx);
++
+ return 0;
+ }
+
};
static void brcmf_fw_request_done(const struct firmware *fw, void *ctx);
-@@ -589,6 +590,8 @@ fail:
- fwctx->req = NULL;
- done:
+@@ -555,6 +556,8 @@ static void brcmf_fw_request_done(const
+ fwctx->req = NULL;
+ }
fwctx->done(fwctx->dev, ret, fwctx->req);
+ if (fwctx->completion)
+ complete(fwctx->completion);
kfree(fwctx);
}
-@@ -612,6 +615,8 @@ int brcmf_fw_get_firmwares(struct device
- struct brcmf_fw_request *req))
+@@ -579,6 +582,7 @@ int brcmf_fw_get_firmwares(struct device
{
+ struct brcmf_fw_item *first = &req->items[0];
struct brcmf_fw *fwctx;
+ struct completion completion;
-+ int err;
+ int ret;
brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
- if (!fw_cb)
-@@ -628,7 +633,14 @@ int brcmf_fw_get_firmwares(struct device
+@@ -595,6 +599,9 @@ int brcmf_fw_get_firmwares(struct device
+ fwctx->dev = dev;
fwctx->req = req;
fwctx->done = fw_cb;
-
++
+ init_completion(&completion);
+ fwctx->completion = &completion;
-+
- brcmf_fw_request_next_item(fwctx, true);
-+
+
+ ret = request_firmware_nowait(THIS_MODULE, true, first->path,
+ fwctx->dev, GFP_KERNEL, fwctx,
+@@ -602,6 +609,9 @@ int brcmf_fw_get_firmwares(struct device
+ if (ret < 0)
+ brcmf_fw_request_done(NULL, fwctx);
+
+ wait_for_completion_timeout(fwctx->completion, msecs_to_jiffies(5000));
+ fwctx->completion = NULL;
+