iwlwifi: rewrite scan completion
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-scan.c
index 6bd14bc..e65a98d 100644 (file)
 #define IWL_PASSIVE_DWELL_BASE      (100)
 #define IWL_CHANNEL_TUNE_TIME       5
 
+static int iwl_send_scan_abort(struct iwl_priv *priv)
+{
+       int ret;
+       struct iwl_rx_packet *pkt;
+       struct iwl_host_cmd cmd = {
+               .id = REPLY_SCAN_ABORT_CMD,
+               .flags = CMD_WANT_SKB,
+       };
 
+       /* Exit instantly with error when device is not ready
+        * to receive scan abort command or it does not perform
+        * hardware scan currently */
+       if (!test_bit(STATUS_READY, &priv->status) ||
+           !test_bit(STATUS_GEO_CONFIGURED, &priv->status) ||
+           !test_bit(STATUS_SCAN_HW, &priv->status) ||
+           test_bit(STATUS_FW_ERROR, &priv->status) ||
+           test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return -EIO;
 
-/**
- * iwl_scan_cancel - Cancel any currently executing HW scan
- *
- * NOTE: priv->mutex is not required before calling this function
- */
-int iwl_scan_cancel(struct iwl_priv *priv)
+       ret = iwl_send_cmd_sync(priv, &cmd);
+       if (ret)
+               return ret;
+
+       pkt = (struct iwl_rx_packet *)cmd.reply_page;
+       if (pkt->u.status != CAN_ABORT_STATUS) {
+               /* The scan abort will return 1 for success or
+                * 2 for "failure".  A failure condition can be
+                * due to simply not being in an active scan which
+                * can occur if we send the scan abort before we
+                * the microcode has notified us that a scan is
+                * completed. */
+               IWL_DEBUG_INFO(priv, "SCAN_ABORT ret %d.\n", pkt->u.status);
+               ret = -EIO;
+       }
+
+       iwl_free_pages(priv, cmd.reply_page);
+       return ret;
+}
+
+static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
 {
-       if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
-               clear_bit(STATUS_SCANNING, &priv->status);
-               return 0;
+       /* check if scan was requested from mac80211 */
+       if (priv->scan_request) {
+               IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
+               ieee80211_scan_completed(priv->hw, aborted);
        }
 
-       if (test_bit(STATUS_SCANNING, &priv->status)) {
-               if (!test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
-                       IWL_DEBUG_SCAN(priv, "Queuing scan abort.\n");
-                       queue_work(priv->workqueue, &priv->abort_scan);
+       priv->is_internal_short_scan = false;
+       priv->scan_vif = NULL;
+       priv->scan_request = NULL;
+}
 
-               } else
-                       IWL_DEBUG_SCAN(priv, "Scan abort already in progress.\n");
+static void iwl_do_scan_abort(struct iwl_priv *priv)
+{
+       int ret;
 
-               return test_bit(STATUS_SCANNING, &priv->status);
+       lockdep_assert_held(&priv->mutex);
+
+       if (!test_bit(STATUS_SCANNING, &priv->status)) {
+               IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
+               return;
        }
 
+       if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
+               IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
+               return;
+       }
+
+       ret = iwl_send_scan_abort(priv);
+       if (ret) {
+               IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
+               clear_bit(STATUS_SCANNING, &priv->status);
+               clear_bit(STATUS_SCAN_HW, &priv->status);
+               clear_bit(STATUS_SCAN_ABORTING, &priv->status);
+               iwl_complete_scan(priv, true);
+       } else
+               IWL_DEBUG_SCAN(priv, "Sucessfully send scan abort\n");
+}
+
+/**
+ * iwl_scan_cancel - Cancel any currently executing HW scan
+ */
+int iwl_scan_cancel(struct iwl_priv *priv)
+{
+       IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
+       queue_work(priv->workqueue, &priv->abort_scan);
        return 0;
 }
 EXPORT_SYMBOL(iwl_scan_cancel);
+
 /**
  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  * @ms: amount of time to wait (in milliseconds) for scan to abort
  *
- * NOTE: priv->mutex must be held before calling this function
  */
 int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
 {
-       unsigned long now = jiffies;
-       int ret;
-
-       ret = iwl_scan_cancel(priv);
-       if (ret && ms) {
-               mutex_unlock(&priv->mutex);
-               while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
-                               test_bit(STATUS_SCANNING, &priv->status))
-                       msleep(1);
-               mutex_lock(&priv->mutex);
-
-               return test_bit(STATUS_SCANNING, &priv->status);
-       }
+       unsigned long timeout = jiffies + msecs_to_jiffies(ms);
 
-       return ret;
-}
-EXPORT_SYMBOL(iwl_scan_cancel_timeout);
-
-static int iwl_send_scan_abort(struct iwl_priv *priv)
-{
-       int ret = 0;
-       struct iwl_rx_packet *pkt;
-       struct iwl_host_cmd cmd = {
-               .id = REPLY_SCAN_ABORT_CMD,
-               .flags = CMD_WANT_SKB,
-       };
+       lockdep_assert_held(&priv->mutex);
 
-       /* If there isn't a scan actively going on in the hardware
-        * then we are in between scan bands and not actually
-        * actively scanning, so don't send the abort command */
-       if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
-               clear_bit(STATUS_SCAN_ABORTING, &priv->status);
-               return 0;
-       }
+       IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
 
-       ret = iwl_send_cmd_sync(priv, &cmd);
-       if (ret) {
-               clear_bit(STATUS_SCAN_ABORTING, &priv->status);
-               return ret;
-       }
+       iwl_do_scan_abort(priv);
 
-       pkt = (struct iwl_rx_packet *)cmd.reply_page;
-       if (pkt->u.status != CAN_ABORT_STATUS) {
-               /* The scan abort will return 1 for success or
-                * 2 for "failure".  A failure condition can be
-                * due to simply not being in an active scan which
-                * can occur if we send the scan abort before we
-                * the microcode has notified us that a scan is
-                * completed. */
-               IWL_DEBUG_INFO(priv, "SCAN_ABORT returned %d.\n", pkt->u.status);
-               clear_bit(STATUS_SCAN_ABORTING, &priv->status);
-               clear_bit(STATUS_SCAN_HW, &priv->status);
+       while (time_before_eq(jiffies, timeout)) {
+               if (!test_bit(STATUS_SCAN_HW, &priv->status))
+                       break;
+               msleep(20);
        }
 
-       iwl_free_pages(priv, cmd.reply_page);
-
-       return ret;
+       return test_bit(STATUS_SCAN_HW, &priv->status);
 }
+EXPORT_SYMBOL(iwl_scan_cancel_timeout);
 
 /* Service response to REPLY_SCAN_CMD (0x80) */
 static void iwl_rx_reply_scan(struct iwl_priv *priv,
@@ -222,21 +242,11 @@ static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
                       jiffies_to_msecs(elapsed_jiffies
                                        (priv->scan_start, jiffies)));
 
-       /*
-        * If a request to abort was given, or the scan did not succeed
-        * then we reset the scan state machine and terminate,
-        * re-queuing another scan if one has been requested
-        */
-       if (test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status))
-               IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
-
-       IWL_DEBUG_INFO(priv, "Setting scan to off\n");
-
-       clear_bit(STATUS_SCANNING, &priv->status);
+       queue_work(priv->workqueue, &priv->scan_completed);
 
        if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
-           priv->cfg->advanced_bt_coexist && priv->bt_status !=
-           scan_notif->bt_status) {
+           priv->cfg->advanced_bt_coexist &&
+           priv->bt_status != scan_notif->bt_status) {
                if (scan_notif->bt_status) {
                        /* BT on */
                        if (!priv->bt_ch_announce)
@@ -254,7 +264,6 @@ static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
                priv->bt_status = scan_notif->bt_status;
                queue_work(priv->workqueue, &priv->bt_traffic_change_work);
        }
-       queue_work(priv->workqueue, &priv->scan_completed);
 }
 
 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
@@ -538,15 +547,10 @@ static void iwl_bg_abort_scan(struct work_struct *work)
 {
        struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
 
-       if (!test_bit(STATUS_READY, &priv->status) ||
-           !test_bit(STATUS_GEO_CONFIGURED, &priv->status))
-               return;
-
        cancel_delayed_work(&priv->scan_check);
 
        mutex_lock(&priv->mutex);
-       if (test_bit(STATUS_SCAN_ABORTING, &priv->status))
-               iwl_send_scan_abort(priv);
+       iwl_do_scan_abort(priv);
        mutex_unlock(&priv->mutex);
 }
 
@@ -554,41 +558,52 @@ static void iwl_bg_scan_completed(struct work_struct *work)
 {
        struct iwl_priv *priv =
            container_of(work, struct iwl_priv, scan_completed);
-       bool internal = false;
+       bool aborted;
        struct iwl_rxon_context *ctx;
 
-       IWL_DEBUG_SCAN(priv, "SCAN complete scan\n");
+       IWL_DEBUG_INFO(priv, "Completed %sscan.\n",
+                      priv->is_internal_short_scan ? "internal short " : "");
 
        cancel_delayed_work(&priv->scan_check);
 
        mutex_lock(&priv->mutex);
-       if (priv->is_internal_short_scan) {
-               priv->is_internal_short_scan = false;
-               IWL_DEBUG_SCAN(priv, "internal short scan completed\n");
-               internal = true;
-       } else if (priv->scan_request) {
-               priv->scan_request = NULL;
-               priv->scan_vif = NULL;
-               ieee80211_scan_completed(priv->hw, false);
-       }
 
-       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+       aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status);
+       if (aborted)
+               IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
+
+       if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
+               IWL_DEBUG_INFO(priv, "Scan already completed.\n");
                goto out;
+       }
 
-       if (internal && priv->scan_request) {
-               int err = iwl_scan_initiate(priv, priv->scan_vif, false,
-                                       priv->scan_request->channels[0]->band);
+       if (priv->is_internal_short_scan && !aborted) {
+               int err;
+
+               /* Check if mac80211 requested scan during our internal scan */
+               if (priv->scan_request == NULL)
+                       goto out_complete;
 
+               /* If so request a new scan */
+               err = iwl_scan_initiate(priv, priv->scan_vif, false,
+                                       priv->scan_request->channels[0]->band);
                if (err) {
                        IWL_DEBUG_SCAN(priv,
                                "failed to initiate pending scan: %d\n", err);
-                       priv->scan_request = NULL;
-                       priv->scan_vif = NULL;
-                       ieee80211_scan_completed(priv->hw, true);
-               } else
-                       goto out;
+                       aborted = true;
+                       goto out_complete;
+               }
+
+               goto out;
        }
 
+out_complete:
+       iwl_complete_scan(priv, aborted);
+
+       /* Can we still talk to firmware ? */
+       if (!iwl_is_ready_rf(priv))
+               goto out;
+
        /* Since setting the TXPOWER may have been deferred while
         * performing the scan, fire one off */
        iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);