ath10k: re-add support for early fw indication
[deliverable/linux.git] / drivers / net / wireless / ath / ath10k / pci.c
index 46c94ce58f97986cf28c6cc9d43cf19e3773a0a2..0a2d1c20e4dfa74574a977305f14315d4e0428a1 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
+#include <linux/bitops.h>
 
 #include "core.h"
 #include "debug.h"
@@ -54,8 +55,14 @@ static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
 static void ath10k_pci_stop_ce(struct ath10k *ar);
 static int ath10k_pci_device_reset(struct ath10k *ar);
 static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
-static int ath10k_pci_start_intr(struct ath10k *ar);
-static void ath10k_pci_stop_intr(struct ath10k *ar);
+static int ath10k_pci_init_irq(struct ath10k *ar);
+static int ath10k_pci_deinit_irq(struct ath10k *ar);
+static int ath10k_pci_request_irq(struct ath10k *ar);
+static void ath10k_pci_free_irq(struct ath10k *ar);
+static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
+                              struct ath10k_ce_pipe *rx_pipe,
+                              struct bmi_xfer *xfer);
+static void ath10k_pci_cleanup_ce(struct ath10k *ar);
 
 static const struct ce_attr host_ce_config_wlan[] = {
        /* CE0: host->target HTC control and raw streams */
@@ -200,6 +207,87 @@ static const struct ce_pipe_config target_ce_config_wlan[] = {
        /* CE7 used only by Host */
 };
 
+static bool ath10k_pci_irq_pending(struct ath10k *ar)
+{
+       u32 cause;
+
+       /* Check if the shared legacy irq is for us */
+       cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
+                                 PCIE_INTR_CAUSE_ADDRESS);
+       if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
+               return true;
+
+       return false;
+}
+
+static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
+{
+       /* IMPORTANT: INTR_CLR register has to be set after
+        * INTR_ENABLE is set to 0, otherwise interrupt can not be
+        * really cleared. */
+       ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
+                          0);
+       ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
+                          PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
+
+       /* IMPORTANT: this extra read transaction is required to
+        * flush the posted write buffer. */
+       (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
+                                PCIE_INTR_ENABLE_ADDRESS);
+}
+
+static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
+{
+       ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
+                          PCIE_INTR_ENABLE_ADDRESS,
+                          PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
+
+       /* IMPORTANT: this extra read transaction is required to
+        * flush the posted write buffer. */
+       (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
+                                PCIE_INTR_ENABLE_ADDRESS);
+}
+
+static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
+{
+       struct ath10k *ar = arg;
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+
+       if (ar_pci->num_msi_intrs == 0) {
+               if (!ath10k_pci_irq_pending(ar))
+                       return IRQ_NONE;
+
+               ath10k_pci_disable_and_clear_legacy_irq(ar);
+       }
+
+       tasklet_schedule(&ar_pci->early_irq_tasklet);
+
+       return IRQ_HANDLED;
+}
+
+static int ath10k_pci_request_early_irq(struct ath10k *ar)
+{
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       int ret;
+
+       /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
+        * interrupt from irq vector is triggered in all cases for FW
+        * indication/errors */
+       ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
+                         IRQF_SHARED, "ath10k_pci (early)", ar);
+       if (ret) {
+               ath10k_warn("failed to request early irq: %d\n", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
+static void ath10k_pci_free_early_irq(struct ath10k *ar)
+{
+       free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
+}
+
 /*
  * Diagnostic read/write access is provided for startup/config/debug usage.
  * Caller must guarantee proper alignment, when applicable, and single user
@@ -805,14 +893,13 @@ static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
               sizeof(ar_pci->msg_callbacks_current));
 }
 
-static int ath10k_pci_start_ce(struct ath10k *ar)
+static int ath10k_pci_alloc_compl(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       struct ath10k_ce_pipe *ce_diag = ar_pci->ce_diag;
        const struct ce_attr *attr;
        struct ath10k_pci_pipe *pipe_info;
        struct ath10k_pci_compl *compl;
-       int i, pipe_num, completions, disable_interrupts;
+       int i, pipe_num, completions;
 
        spin_lock_init(&ar_pci->compl_lock);
        INIT_LIST_HEAD(&ar_pci->compl_process);
@@ -824,34 +911,23 @@ static int ath10k_pci_start_ce(struct ath10k *ar)
                INIT_LIST_HEAD(&pipe_info->compl_free);
 
                /* Handle Diagnostic CE specially */
-               if (pipe_info->ce_hdl == ce_diag)
+               if (pipe_info->ce_hdl == ar_pci->ce_diag)
                        continue;
 
                attr = &host_ce_config_wlan[pipe_num];
                completions = 0;
 
-               if (attr->src_nentries) {
-                       disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
-                       ath10k_ce_send_cb_register(pipe_info->ce_hdl,
-                                                  ath10k_pci_ce_send_done,
-                                                  disable_interrupts);
+               if (attr->src_nentries)
                        completions += attr->src_nentries;
-               }
 
-               if (attr->dest_nentries) {
-                       ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
-                                                  ath10k_pci_ce_recv_data);
+               if (attr->dest_nentries)
                        completions += attr->dest_nentries;
-               }
-
-               if (completions == 0)
-                       continue;
 
                for (i = 0; i < completions; i++) {
                        compl = kmalloc(sizeof(*compl), GFP_KERNEL);
                        if (!compl) {
                                ath10k_warn("No memory for completion state\n");
-                               ath10k_pci_stop_ce(ar);
+                               ath10k_pci_cleanup_ce(ar);
                                return -ENOMEM;
                        }
 
@@ -863,6 +939,37 @@ static int ath10k_pci_start_ce(struct ath10k *ar)
        return 0;
 }
 
+static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
+{
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       const struct ce_attr *attr;
+       struct ath10k_pci_pipe *pipe_info;
+       int pipe_num, disable_interrupts;
+
+       for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
+               pipe_info = &ar_pci->pipe_info[pipe_num];
+
+               /* Handle Diagnostic CE specially */
+               if (pipe_info->ce_hdl == ar_pci->ce_diag)
+                       continue;
+
+               attr = &host_ce_config_wlan[pipe_num];
+
+               if (attr->src_nentries) {
+                       disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
+                       ath10k_ce_send_cb_register(pipe_info->ce_hdl,
+                                                  ath10k_pci_ce_send_done,
+                                                  disable_interrupts);
+               }
+
+               if (attr->dest_nentries)
+                       ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
+                                                  ath10k_pci_ce_recv_data);
+       }
+
+       return 0;
+}
+
 static void ath10k_pci_kill_tasklet(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
@@ -870,6 +977,7 @@ static void ath10k_pci_kill_tasklet(struct ath10k *ar)
 
        tasklet_kill(&ar_pci->intr_tq);
        tasklet_kill(&ar_pci->msi_fw_err);
+       tasklet_kill(&ar_pci->early_irq_tasklet);
 
        for (i = 0; i < CE_COUNT; i++)
                tasklet_kill(&ar_pci->pipe_info[i].intr);
@@ -880,13 +988,6 @@ static void ath10k_pci_stop_ce(struct ath10k *ar)
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
        struct ath10k_pci_compl *compl;
        struct sk_buff *skb;
-       int ret;
-
-       ret = ath10k_ce_disable_interrupts(ar);
-       if (ret)
-               ath10k_warn("failed to disable CE interrupts: %d\n", ret);
-
-       ath10k_pci_kill_tasklet(ar);
 
        /* Mark pending completions as aborted, so that upper layers free up
         * their associated resources */
@@ -1189,12 +1290,28 @@ static int ath10k_pci_post_rx(struct ath10k *ar)
 static int ath10k_pci_hif_start(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       int ret;
+       int ret, ret_early;
 
-       ret = ath10k_pci_start_ce(ar);
+       ath10k_pci_free_early_irq(ar);
+       ath10k_pci_kill_tasklet(ar);
+
+       ret = ath10k_pci_alloc_compl(ar);
        if (ret) {
-               ath10k_warn("failed to start CE: %d\n", ret);
-               return ret;
+               ath10k_warn("failed to allocate CE completions: %d\n", ret);
+               goto err_early_irq;
+       }
+
+       ret = ath10k_pci_request_irq(ar);
+       if (ret) {
+               ath10k_warn("failed to post RX buffers for all pipes: %d\n",
+                           ret);
+               goto err_free_compl;
+       }
+
+       ret = ath10k_pci_setup_ce_irq(ar);
+       if (ret) {
+               ath10k_warn("failed to setup CE interrupts: %d\n", ret);
+               goto err_stop;
        }
 
        /* Post buffers once to start things off. */
@@ -1202,11 +1319,29 @@ static int ath10k_pci_hif_start(struct ath10k *ar)
        if (ret) {
                ath10k_warn("failed to post RX buffers for all pipes: %d\n",
                            ret);
-               return ret;
+               goto err_stop;
        }
 
        ar_pci->started = 1;
        return 0;
+
+err_stop:
+       ath10k_ce_disable_interrupts(ar);
+       ath10k_pci_free_irq(ar);
+       ath10k_pci_kill_tasklet(ar);
+       ath10k_pci_stop_ce(ar);
+       ath10k_pci_process_ce(ar);
+err_free_compl:
+       ath10k_pci_cleanup_ce(ar);
+err_early_irq:
+       /* Though there should be no interrupts (device was reset)
+        * power_down() expects the early IRQ to be installed as per the
+        * driver lifecycle. */
+       ret_early = ath10k_pci_request_early_irq(ar);
+       if (ret_early)
+               ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
+
+       return ret;
 }
 
 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
@@ -1324,27 +1459,25 @@ static void ath10k_pci_ce_deinit(struct ath10k *ar)
        }
 }
 
-static void ath10k_pci_disable_irqs(struct ath10k *ar)
-{
-       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       int i;
-
-       for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
-               disable_irq(ar_pci->pdev->irq + i);
-}
-
 static void ath10k_pci_hif_stop(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       int ret;
 
        ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
 
-       /* Irqs are never explicitly re-enabled. They are implicitly re-enabled
-        * by ath10k_pci_start_intr(). */
-       ath10k_pci_disable_irqs(ar);
+       ret = ath10k_ce_disable_interrupts(ar);
+       if (ret)
+               ath10k_warn("failed to disable CE interrupts: %d\n", ret);
 
+       ath10k_pci_free_irq(ar);
+       ath10k_pci_kill_tasklet(ar);
        ath10k_pci_stop_ce(ar);
 
+       ret = ath10k_pci_request_early_irq(ar);
+       if (ret)
+               ath10k_warn("failed to re-enable early irq: %d\n", ret);
+
        /* At this point, asynchronous threads are stopped, the target should
         * not DMA nor interrupt. We process the leftovers and then free
         * everything else up. */
@@ -1378,6 +1511,8 @@ static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
        void *treq, *tresp = NULL;
        int ret = 0;
 
+       might_sleep();
+
        if (resp && !resp_len)
                return -EINVAL;
 
@@ -1418,14 +1553,12 @@ static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
        if (ret)
                goto err_resp;
 
-       ret = wait_for_completion_timeout(&xfer.done,
-                                         BMI_COMMUNICATION_TIMEOUT_HZ);
-       if (ret <= 0) {
+       ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
+       if (ret) {
                u32 unused_buffer;
                unsigned int unused_nbytes;
                unsigned int unused_id;
 
-               ret = -ETIMEDOUT;
                ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
                                           &unused_nbytes, &unused_id);
        } else {
@@ -1493,6 +1626,25 @@ static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
        complete(&xfer->done);
 }
 
+static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
+                              struct ath10k_ce_pipe *rx_pipe,
+                              struct bmi_xfer *xfer)
+{
+       unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
+
+       while (time_before_eq(jiffies, timeout)) {
+               ath10k_pci_bmi_send_done(tx_pipe);
+               ath10k_pci_bmi_recv_data(rx_pipe);
+
+               if (completion_done(&xfer->done))
+                       return 0;
+
+               schedule();
+       }
+
+       return -ETIMEDOUT;
+}
+
 /*
  * Map from service/endpoint to Copy Engine.
  * This table is derived from the CE_PCI TABLE, above.
@@ -1830,27 +1982,10 @@ static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
        ath10k_pci_sleep(ar);
 }
 
-static void ath10k_pci_start_bmi(struct ath10k *ar)
-{
-       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       struct ath10k_pci_pipe *pipe;
-
-       /*
-        * Initially, establish CE completion handlers for use with BMI.
-        * These are overwritten with generic handlers after we exit BMI phase.
-        */
-       pipe = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
-       ath10k_ce_send_cb_register(pipe->ce_hdl, ath10k_pci_bmi_send_done, 0);
-
-       pipe = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
-       ath10k_ce_recv_cb_register(pipe->ce_hdl, ath10k_pci_bmi_recv_data);
-
-       ath10k_dbg(ATH10K_DBG_BOOT, "boot start bmi\n");
-}
-
 static int ath10k_pci_hif_power_up(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       const char *irq_mode;
        int ret;
 
        /*
@@ -1885,46 +2020,55 @@ static int ath10k_pci_hif_power_up(struct ath10k *ar)
                goto err_ce;
        }
 
-       ret = ath10k_pci_start_intr(ar);
+       ret = ath10k_pci_init_irq(ar);
        if (ret) {
-               ath10k_err("failed to start interrupt handling: %d\n", ret);
+               ath10k_err("failed to init irqs: %d\n", ret);
                goto err_ce;
        }
 
-       ret = ath10k_pci_wait_for_target_init(ar);
+       ret = ath10k_pci_request_early_irq(ar);
        if (ret) {
-               ath10k_err("failed to wait for target to init: %d\n", ret);
-               goto err_irq;
+               ath10k_err("failed to request early irq: %d\n", ret);
+               goto err_deinit_irq;
        }
 
-       ret = ath10k_ce_enable_err_irq(ar);
+       ret = ath10k_pci_wait_for_target_init(ar);
        if (ret) {
-               ath10k_err("failed to enable CE error irq: %d\n", ret);
-               goto err_irq;
+               ath10k_err("failed to wait for target to init: %d\n", ret);
+               goto err_free_early_irq;
        }
 
        ret = ath10k_pci_init_config(ar);
        if (ret) {
                ath10k_err("failed to setup init config: %d\n", ret);
-               goto err_irq;
+               goto err_free_early_irq;
        }
 
        ret = ath10k_pci_wake_target_cpu(ar);
        if (ret) {
                ath10k_err("could not wake up target CPU: %d\n", ret);
-               goto err_irq;
+               goto err_free_early_irq;
        }
 
-       ath10k_pci_start_bmi(ar);
+       if (ar_pci->num_msi_intrs > 1)
+               irq_mode = "MSI-X";
+       else if (ar_pci->num_msi_intrs == 1)
+               irq_mode = "MSI";
+       else
+               irq_mode = "legacy";
+
+       if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
+               ath10k_info("pci irq %s\n", irq_mode);
+
        return 0;
 
-err_irq:
-       ath10k_ce_disable_interrupts(ar);
-       ath10k_pci_stop_intr(ar);
-       ath10k_pci_kill_tasklet(ar);
-       ath10k_pci_device_reset(ar);
+err_free_early_irq:
+       ath10k_pci_free_early_irq(ar);
+err_deinit_irq:
+       ath10k_pci_deinit_irq(ar);
 err_ce:
        ath10k_pci_ce_deinit(ar);
+       ath10k_pci_device_reset(ar);
 err_ps:
        if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
                ath10k_do_pci_sleep(ar);
@@ -1936,7 +2080,9 @@ static void ath10k_pci_hif_power_down(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
 
-       ath10k_pci_stop_intr(ar);
+       ath10k_pci_free_early_irq(ar);
+       ath10k_pci_kill_tasklet(ar);
+       ath10k_pci_deinit_irq(ar);
        ath10k_pci_device_reset(ar);
 
        ath10k_pci_ce_deinit(ar);
@@ -2073,25 +2219,10 @@ static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
 
        if (ar_pci->num_msi_intrs == 0) {
-               /*
-                * IMPORTANT: INTR_CLR regiser has to be set after
-                * INTR_ENABLE is set to 0, otherwise interrupt can not be
-                * really cleared.
-                */
-               iowrite32(0, ar_pci->mem +
-                         (SOC_CORE_BASE_ADDRESS |
-                          PCIE_INTR_ENABLE_ADDRESS));
-               iowrite32(PCIE_INTR_FIRMWARE_MASK |
-                         PCIE_INTR_CE_MASK_ALL,
-                         ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
-                                        PCIE_INTR_CLR_ADDRESS));
-               /*
-                * IMPORTANT: this extra read transaction is required to
-                * flush the posted write buffer.
-                */
-               (void) ioread32(ar_pci->mem +
-                               (SOC_CORE_BASE_ADDRESS |
-                                PCIE_INTR_ENABLE_ADDRESS));
+               if (!ath10k_pci_irq_pending(ar))
+                       return IRQ_NONE;
+
+               ath10k_pci_disable_and_clear_legacy_irq(ar);
        }
 
        tasklet_schedule(&ar_pci->intr_tq);
@@ -2099,6 +2230,34 @@ static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
        return IRQ_HANDLED;
 }
 
+static void ath10k_pci_early_irq_tasklet(unsigned long data)
+{
+       struct ath10k *ar = (struct ath10k *)data;
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       u32 fw_ind;
+       int ret;
+
+       ret = ath10k_pci_wake(ar);
+       if (ret) {
+               ath10k_warn("failed to wake target in early irq tasklet: %d\n",
+                           ret);
+               return;
+       }
+
+       fw_ind = ath10k_pci_read32(ar, ar_pci->fw_indicator_address);
+       if (fw_ind & FW_IND_EVENT_PENDING) {
+               ath10k_pci_write32(ar, ar_pci->fw_indicator_address,
+                                  fw_ind & ~FW_IND_EVENT_PENDING);
+
+               /* Some structures are unavailable during early boot or at
+                * driver teardown so just print that the device has crashed. */
+               ath10k_warn("device crashed - no diagnostics available\n");
+       }
+
+       ath10k_pci_sleep(ar);
+       ath10k_pci_enable_legacy_irq(ar);
+}
+
 static void ath10k_pci_tasklet(unsigned long data)
 {
        struct ath10k *ar = (struct ath10k *)data;
@@ -2107,40 +2266,22 @@ static void ath10k_pci_tasklet(unsigned long data)
        ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
        ath10k_ce_per_engine_service_any(ar);
 
-       if (ar_pci->num_msi_intrs == 0) {
-               /* Enable Legacy PCI line interrupts */
-               iowrite32(PCIE_INTR_FIRMWARE_MASK |
-                         PCIE_INTR_CE_MASK_ALL,
-                         ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
-                                        PCIE_INTR_ENABLE_ADDRESS));
-               /*
-                * IMPORTANT: this extra read transaction is required to
-                * flush the posted write buffer
-                */
-               (void) ioread32(ar_pci->mem +
-                               (SOC_CORE_BASE_ADDRESS |
-                                PCIE_INTR_ENABLE_ADDRESS));
-       }
+       /* Re-enable legacy irq that was disabled in the irq handler */
+       if (ar_pci->num_msi_intrs == 0)
+               ath10k_pci_enable_legacy_irq(ar);
 }
 
-static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
+static int ath10k_pci_request_irq_msix(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       int ret;
-       int i;
-
-       ret = pci_enable_msi_block(ar_pci->pdev, num);
-       if (ret)
-               return ret;
+       int ret, i;
 
        ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
                          ath10k_pci_msi_fw_handler,
                          IRQF_SHARED, "ath10k_pci", ar);
        if (ret) {
-               ath10k_warn("request_irq(%d) failed %d\n",
+               ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
                            ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
-
-               pci_disable_msi(ar_pci->pdev);
                return ret;
        }
 
@@ -2149,44 +2290,38 @@ static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
                                  ath10k_pci_per_engine_handler,
                                  IRQF_SHARED, "ath10k_pci", ar);
                if (ret) {
-                       ath10k_warn("request_irq(%d) failed %d\n",
+                       ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
                                    ar_pci->pdev->irq + i, ret);
 
                        for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
                                free_irq(ar_pci->pdev->irq + i, ar);
 
                        free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
-                       pci_disable_msi(ar_pci->pdev);
                        return ret;
                }
        }
 
-       ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
        return 0;
 }
 
-static int ath10k_pci_start_intr_msi(struct ath10k *ar)
+static int ath10k_pci_request_irq_msi(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
        int ret;
 
-       ret = pci_enable_msi(ar_pci->pdev);
-       if (ret < 0)
-               return ret;
-
        ret = request_irq(ar_pci->pdev->irq,
                          ath10k_pci_interrupt_handler,
                          IRQF_SHARED, "ath10k_pci", ar);
-       if (ret < 0) {
-               pci_disable_msi(ar_pci->pdev);
+       if (ret) {
+               ath10k_warn("failed to request MSI irq %d: %d\n",
+                           ar_pci->pdev->irq, ret);
                return ret;
        }
 
-       ath10k_info("MSI interrupt handling\n");
        return 0;
 }
 
-static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
+static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
        int ret;
@@ -2194,97 +2329,142 @@ static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
        ret = request_irq(ar_pci->pdev->irq,
                          ath10k_pci_interrupt_handler,
                          IRQF_SHARED, "ath10k_pci", ar);
-       if (ret < 0)
-               return ret;
-
-       ret = ath10k_pci_wake(ar);
        if (ret) {
-               free_irq(ar_pci->pdev->irq, ar);
-               ath10k_err("failed to wake up target: %d\n", ret);
+               ath10k_warn("failed to request legacy irq %d: %d\n",
+                           ar_pci->pdev->irq, ret);
                return ret;
        }
 
-       /*
-        * A potential race occurs here: The CORE_BASE write
-        * depends on target correctly decoding AXI address but
-        * host won't know when target writes BAR to CORE_CTRL.
-        * This write might get lost if target has NOT written BAR.
-        * For now, fix the race by repeating the write in below
-        * synchronization checking.
-        */
-       iowrite32(PCIE_INTR_FIRMWARE_MASK |
-                 PCIE_INTR_CE_MASK_ALL,
-                 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
-                                PCIE_INTR_ENABLE_ADDRESS));
-
-       ath10k_pci_sleep(ar);
-       ath10k_info("legacy interrupt handling\n");
        return 0;
 }
 
-static int ath10k_pci_start_intr(struct ath10k *ar)
+static int ath10k_pci_request_irq(struct ath10k *ar)
+{
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+
+       switch (ar_pci->num_msi_intrs) {
+       case 0:
+               return ath10k_pci_request_irq_legacy(ar);
+       case 1:
+               return ath10k_pci_request_irq_msi(ar);
+       case MSI_NUM_REQUEST:
+               return ath10k_pci_request_irq_msix(ar);
+       }
+
+       ath10k_warn("unknown irq configuration upon request\n");
+       return -EINVAL;
+}
+
+static void ath10k_pci_free_irq(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       int num = MSI_NUM_REQUEST;
-       int ret;
        int i;
 
-       tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
+       /* There's at least one interrupt irregardless whether its legacy INTR
+        * or MSI or MSI-X */
+       for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
+               free_irq(ar_pci->pdev->irq + i, ar);
+}
+
+static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
+{
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       int i;
+
+       tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
        tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
-                    (unsigned long) ar);
+                    (unsigned long)ar);
+       tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
+                    (unsigned long)ar);
 
        for (i = 0; i < CE_COUNT; i++) {
                ar_pci->pipe_info[i].ar_pci = ar_pci;
-               tasklet_init(&ar_pci->pipe_info[i].intr,
-                            ath10k_pci_ce_tasklet,
+               tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
                             (unsigned long)&ar_pci->pipe_info[i]);
        }
+}
+
+static int ath10k_pci_init_irq(struct ath10k *ar)
+{
+       struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
+       int ret;
+
+       ath10k_pci_init_irq_tasklets(ar);
 
        if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
-               num = 1;
+               goto msi;
 
-       if (num > 1) {
-               ret = ath10k_pci_start_intr_msix(ar, num);
-               if (ret == 0)
-                       goto exit;
+       /* Try MSI-X */
+       ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
+       ret = pci_enable_msi_block(ar_pci->pdev, ar_pci->num_msi_intrs);
+       if (ret == 0)
+               return 0;
+       if (ret > 0)
+               pci_disable_msi(ar_pci->pdev);
 
-               ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
-               num = 1;
-       }
+msi:
+       /* Try MSI */
+       ar_pci->num_msi_intrs = 1;
+       ret = pci_enable_msi(ar_pci->pdev);
+       if (ret == 0)
+               return 0;
 
-       if (num == 1) {
-               ret = ath10k_pci_start_intr_msi(ar);
-               if (ret == 0)
-                       goto exit;
+       /* Try legacy irq
+        *
+        * A potential race occurs here: The CORE_BASE write
+        * depends on target correctly decoding AXI address but
+        * host won't know when target writes BAR to CORE_CTRL.
+        * This write might get lost if target has NOT written BAR.
+        * For now, fix the race by repeating the write in below
+        * synchronization checking. */
+       ar_pci->num_msi_intrs = 0;
 
-               ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
-                           ret);
-               num = 0;
+       ret = ath10k_pci_wake(ar);
+       if (ret) {
+               ath10k_warn("failed to wake target: %d\n", ret);
+               return ret;
        }
 
-       ret = ath10k_pci_start_intr_legacy(ar);
+       ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
+                          PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
+       ath10k_pci_sleep(ar);
+
+       return 0;
+}
+
+static int ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
+{
+       int ret;
+
+       ret = ath10k_pci_wake(ar);
        if (ret) {
-               ath10k_warn("Failed to start legacy interrupts: %d\n", ret);
+               ath10k_warn("failed to wake target: %d\n", ret);
                return ret;
        }
 
-exit:
-       ar_pci->num_msi_intrs = num;
-       return ret;
+       ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
+                          0);
+       ath10k_pci_sleep(ar);
+
+       return 0;
 }
 
-static void ath10k_pci_stop_intr(struct ath10k *ar)
+static int ath10k_pci_deinit_irq(struct ath10k *ar)
 {
        struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
-       int i;
 
-       /* There's at least one interrupt irregardless whether its legacy INTR
-        * or MSI or MSI-X */
-       for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
-               free_irq(ar_pci->pdev->irq + i, ar);
-
-       if (ar_pci->num_msi_intrs > 0)
+       switch (ar_pci->num_msi_intrs) {
+       case 0:
+               return ath10k_pci_deinit_irq_legacy(ar);
+       case 1:
+               /* fall-through */
+       case MSI_NUM_REQUEST:
                pci_disable_msi(ar_pci->pdev);
+               return 0;
+       }
+
+       ath10k_warn("unknown irq configuration upon deinit\n");
+       return -EINVAL;
 }
 
 static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
This page took 0.042122 seconds and 5 git commands to generate.