mac80211: reduce reliance on netdev
[safe/jmp/linux-2.6] / net / mac80211 / mlme.c
index 18dad22..cd5dcc3 100644 (file)
@@ -31,6 +31,7 @@
 #define IEEE80211_AUTH_MAX_TRIES 3
 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
 #define IEEE80211_ASSOC_MAX_TRIES 3
+#define IEEE80211_MAX_PROBE_TRIES 5
 
 /*
  * beacon loss detection timeout
  * Time the connection can be idle before we probe
  * it to see if we can still talk to the AP.
  */
-#define IEEE80211_CONNECTION_IDLE_TIME (2 * HZ)
+#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
 /*
  * Time we wait for a probe response after sending
  * a probe request because of beacon loss or for
  * checking the connection still works.
  */
-#define IEEE80211_PROBE_WAIT           (HZ / 5)
+#define IEEE80211_PROBE_WAIT           (HZ / 2)
 
 #define TMR_RUNNING_TIMER      0
 #define TMR_RUNNING_CHANSW     1
@@ -201,7 +202,7 @@ static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
                ieee80211_hw_config(local, 0);
 
                rcu_read_lock();
-               sta = sta_info_get(local, bssid);
+               sta = sta_info_get(sdata, bssid);
                if (sta)
                        rate_control_rate_update(local, sband, sta,
                                                 IEEE80211_RC_HT_CHANGED);
@@ -247,7 +248,7 @@ static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
                            wk->ssid_len);
        if (!skb) {
                printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
-                      "frame\n", sdata->dev->name);
+                      "frame\n", sdata->name);
                return;
        }
        skb_reserve(skb, local->hw.extra_tx_headroom);
@@ -281,7 +282,7 @@ static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
        mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
        memset(mgmt, 0, 24);
        memcpy(mgmt->da, wk->bss->cbss.bssid, ETH_ALEN);
-       memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
+       memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
        memcpy(mgmt->bssid, wk->bss->cbss.bssid, ETH_ALEN);
 
        if (!is_zero_ether_addr(wk->prev_bssid)) {
@@ -425,7 +426,8 @@ static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
                memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
        }
 
-       ieee80211_tx_skb(sdata, skb, 0);
+       IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 
@@ -441,7 +443,7 @@ static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
        skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
        if (!skb) {
                printk(KERN_DEBUG "%s: failed to allocate buffer for "
-                      "deauth/disassoc frame\n", sdata->dev->name);
+                      "deauth/disassoc frame\n", sdata->name);
                return;
        }
        skb_reserve(skb, local->hw.extra_tx_headroom);
@@ -449,7 +451,7 @@ static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
        mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
        memset(mgmt, 0, 24);
        memcpy(mgmt->da, bssid, ETH_ALEN);
-       memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
+       memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
        memcpy(mgmt->bssid, bssid, ETH_ALEN);
        mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
        skb_put(skb, 2);
@@ -457,10 +459,18 @@ static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
        mgmt->u.deauth.reason_code = cpu_to_le16(reason);
 
        if (stype == IEEE80211_STYPE_DEAUTH)
-               cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len, cookie);
+               if (cookie)
+                       __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
+               else
+                       cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
        else
-               cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len, cookie);
-       ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
+               if (cookie)
+                       __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
+               else
+                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
+       if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
+               IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 void ieee80211_send_pspoll(struct ieee80211_local *local,
@@ -474,7 +484,7 @@ void ieee80211_send_pspoll(struct ieee80211_local *local,
        skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
        if (!skb) {
                printk(KERN_DEBUG "%s: failed to allocate buffer for "
-                      "pspoll frame\n", sdata->dev->name);
+                      "pspoll frame\n", sdata->name);
                return;
        }
        skb_reserve(skb, local->hw.extra_tx_headroom);
@@ -489,9 +499,10 @@ void ieee80211_send_pspoll(struct ieee80211_local *local,
        pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
 
        memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
-       memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
+       memcpy(pspoll->ta, sdata->vif.addr, ETH_ALEN);
 
-       ieee80211_tx_skb(sdata, skb, 0);
+       IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 void ieee80211_send_nullfunc(struct ieee80211_local *local,
@@ -508,7 +519,7 @@ void ieee80211_send_nullfunc(struct ieee80211_local *local,
        skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
        if (!skb) {
                printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
-                      "frame\n", sdata->dev->name);
+                      "frame\n", sdata->name);
                return;
        }
        skb_reserve(skb, local->hw.extra_tx_headroom);
@@ -521,10 +532,11 @@ void ieee80211_send_nullfunc(struct ieee80211_local *local,
                fc |= cpu_to_le16(IEEE80211_FCTL_PM);
        nullfunc->frame_control = fc;
        memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
-       memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
+       memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
        memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
 
-       ieee80211_tx_skb(sdata, skb, 0);
+       IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 /* spectrum management related things */
@@ -565,7 +577,7 @@ static void ieee80211_chswitch_timer(unsigned long data)
                return;
        }
 
-       queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
+       ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
 }
 
 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
@@ -581,7 +593,7 @@ void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
        if (!ifmgd->associated)
                return;
 
-       if (sdata->local->sw_scanning || sdata->local->hw_scanning)
+       if (sdata->local->scanning)
                return;
 
        /* Disregard subsequent beacons if we are already running a timer
@@ -597,7 +609,7 @@ void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
        sdata->local->csa_channel = new_ch;
 
        if (sw_elem->count <= 1) {
-               queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
+               ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
        } else {
                ieee80211_stop_queues_by_reason(&sdata->local->hw,
                                        IEEE80211_QUEUE_STOP_REASON_CSA);
@@ -639,7 +651,7 @@ static void ieee80211_enable_ps(struct ieee80211_local *local,
         * If we are scanning right now then the parameters will
         * take effect when scan finishes.
         */
-       if (local->hw_scanning || local->sw_scanning)
+       if (local->scanning)
                return;
 
        if (conf->dynamic_ps_timeout > 0 &&
@@ -760,10 +772,10 @@ void ieee80211_dynamic_ps_timer(unsigned long data)
 {
        struct ieee80211_local *local = (void *) data;
 
-       if (local->quiescing)
+       if (local->quiescing || local->suspended)
                return;
 
-       queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
+       ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
 }
 
 /* MLME */
@@ -879,10 +891,11 @@ static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
 }
 
 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
-                                    struct ieee80211_bss *bss,
+                                    struct ieee80211_mgd_work *wk,
                                     u32 bss_info_changed)
 {
        struct ieee80211_local *local = sdata->local;
+       struct ieee80211_bss *bss = wk->bss;
 
        bss_info_changed |= BSS_CHANGED_ASSOC;
        /* set timing information */
@@ -895,6 +908,7 @@ static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
                bss->cbss.capability, bss->has_erp_value, bss->erp_value);
 
        sdata->u.mgd.associated = bss;
+       sdata->u.mgd.old_associate_work = wk;
        memcpy(sdata->u.mgd.bssid, bss->cbss.bssid, ETH_ALEN);
 
        /* just to be sure */
@@ -916,14 +930,11 @@ static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
 
        ieee80211_bss_info_change_notify(sdata, bss_info_changed);
 
-       /* will be same as sdata */
-       if (local->ps_sdata) {
-               mutex_lock(&local->iflist_mtx);
-               ieee80211_recalc_ps(local, -1);
-               mutex_unlock(&local->iflist_mtx);
-       }
+       mutex_lock(&local->iflist_mtx);
+       ieee80211_recalc_ps(local, -1);
+       mutex_unlock(&local->iflist_mtx);
 
-       netif_tx_start_all_queues(sdata->dev);
+       netif_start_queue(sdata->dev);
        netif_carrier_on(sdata->dev);
 }
 
@@ -937,7 +948,7 @@ ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
        wk->tries++;
        if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
                printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
-                      sdata->dev->name, wk->bss->cbss.bssid);
+                      sdata->name, wk->bss->cbss.bssid);
 
                /*
                 * Most likely AP is not in the range so remove the
@@ -950,12 +961,12 @@ ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
                 * due to work needing to be done. Hence, queue the STAs work
                 * again for that.
                 */
-               queue_work(local->hw.workqueue, &ifmgd->work);
+               ieee80211_queue_work(&local->hw, &ifmgd->work);
                return RX_MGMT_CFG80211_AUTH_TO;
        }
 
        printk(KERN_DEBUG "%s: direct probe to AP %pM (try %d)\n",
-                       sdata->dev->name, wk->bss->cbss.bssid,
+                       sdata->name, wk->bss->cbss.bssid,
                        wk->tries);
 
        /*
@@ -982,7 +993,7 @@ ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
        if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
                printk(KERN_DEBUG "%s: authentication with AP %pM"
                       " timed out\n",
-                      sdata->dev->name, wk->bss->cbss.bssid);
+                      sdata->name, wk->bss->cbss.bssid);
 
                /*
                 * Most likely AP is not in the range so remove the
@@ -995,12 +1006,12 @@ ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
                 * due to work needing to be done. Hence, queue the STAs work
                 * again for that.
                 */
-               queue_work(local->hw.workqueue, &ifmgd->work);
+               ieee80211_queue_work(&local->hw, &ifmgd->work);
                return RX_MGMT_CFG80211_AUTH_TO;
        }
 
        printk(KERN_DEBUG "%s: authenticate with AP %pM (try %d)\n",
-              sdata->dev->name, wk->bss->cbss.bssid, wk->tries);
+              sdata->name, wk->bss->cbss.bssid, wk->tries);
 
        ieee80211_send_auth(sdata, 1, wk->auth_alg, wk->ie, wk->ie_len,
                            wk->bss->cbss.bssid, NULL, 0, 0);
@@ -1012,7 +1023,8 @@ ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
        return RX_MGMT_NONE;
 }
 
-static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
+static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
+                                  bool deauth)
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        struct ieee80211_local *local = sdata->local;
@@ -1030,6 +1042,16 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
        ifmgd->associated = NULL;
        memset(ifmgd->bssid, 0, ETH_ALEN);
 
+       if (deauth) {
+               kfree(ifmgd->old_associate_work);
+               ifmgd->old_associate_work = NULL;
+       } else {
+               struct ieee80211_mgd_work *wk = ifmgd->old_associate_work;
+
+               wk->state = IEEE80211_MGD_STATE_IDLE;
+               list_add(&wk->list, &ifmgd->work_list);
+       }
+
        /*
         * we need to commit the associated = NULL change because the
         * scan code uses that to determine whether this iface should
@@ -1044,11 +1066,11 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
         * time -- we don't want the scan code to enable queues.
         */
 
-       netif_tx_stop_all_queues(sdata->dev);
+       netif_stop_queue(sdata->dev);
        netif_carrier_off(sdata->dev);
 
        rcu_read_lock();
-       sta = sta_info_get(local, bssid);
+       sta = sta_info_get(sdata, bssid);
        if (sta)
                ieee80211_sta_tear_down_BA_sessions(sta);
        rcu_read_unlock();
@@ -1087,7 +1109,7 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
 
        rcu_read_lock();
 
-       sta = sta_info_get(local, bssid);
+       sta = sta_info_get(sdata, bssid);
        if (!sta) {
                rcu_read_unlock();
                return;
@@ -1111,7 +1133,7 @@ ieee80211_associate(struct ieee80211_sub_if_data *sdata,
        if (wk->tries > IEEE80211_ASSOC_MAX_TRIES) {
                printk(KERN_DEBUG "%s: association with AP %pM"
                       " timed out\n",
-                      sdata->dev->name, wk->bss->cbss.bssid);
+                      sdata->name, wk->bss->cbss.bssid);
 
                /*
                 * Most likely AP is not in the range so remove the
@@ -1124,12 +1146,12 @@ ieee80211_associate(struct ieee80211_sub_if_data *sdata,
                 * due to work needing to be done. Hence, queue the STAs work
                 * again for that.
                 */
-               queue_work(local->hw.workqueue, &ifmgd->work);
+               ieee80211_queue_work(&local->hw, &ifmgd->work);
                return RX_MGMT_CFG80211_ASSOC_TO;
        }
 
        printk(KERN_DEBUG "%s: associate with AP %pM (try %d)\n",
-              sdata->dev->name, wk->bss->cbss.bssid, wk->tries);
+              sdata->name, wk->bss->cbss.bssid, wk->tries);
        ieee80211_send_assoc(sdata, wk);
 
        wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
@@ -1156,13 +1178,32 @@ void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
                  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
 }
 
+static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
+{
+       struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
+       const u8 *ssid;
+
+       ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
+       ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
+                                ssid + 2, ssid[1], NULL, 0);
+
+       ifmgd->probe_send_count++;
+       ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
+       run_again(ifmgd, ifmgd->probe_timeout);
+}
+
 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
                                   bool beacon)
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
-       const u8 *ssid;
        bool already = false;
 
+       if (!netif_running(sdata->dev))
+               return;
+
+       if (sdata->local->scanning)
+               return;
+
        mutex_lock(&ifmgd->mtx);
 
        if (!ifmgd->associated)
@@ -1171,7 +1212,7 @@ static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
        if (beacon && net_ratelimit())
                printk(KERN_DEBUG "%s: detected beacon loss from AP "
-                      "- sending probe request\n", sdata->dev->name);
+                      "- sending probe request\n", sdata->name);
 #endif
 
        /*
@@ -1197,18 +1238,12 @@ static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
        if (already)
                goto out;
 
-       ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
-
        mutex_lock(&sdata->local->iflist_mtx);
        ieee80211_recalc_ps(sdata->local, -1);
        mutex_unlock(&sdata->local->iflist_mtx);
 
-       ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
-       ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
-                                ssid + 2, ssid[1], NULL, 0);
-
-       run_again(ifmgd, ifmgd->probe_timeout);
-
+       ifmgd->probe_send_count = 0;
+       ieee80211_mgd_probe_ap_send(sdata);
  out:
        mutex_unlock(&ifmgd->mtx);
 }
@@ -1226,8 +1261,7 @@ void ieee80211_beacon_loss(struct ieee80211_vif *vif)
 {
        struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 
-       queue_work(sdata->local->hw.workqueue,
-                  &sdata->u.mgd.beacon_loss_work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
 }
 EXPORT_SYMBOL(ieee80211_beacon_loss);
 
@@ -1235,7 +1269,7 @@ static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
                                     struct ieee80211_mgd_work *wk)
 {
        wk->state = IEEE80211_MGD_STATE_IDLE;
-       printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
+       printk(KERN_DEBUG "%s: authenticated\n", sdata->name);
 }
 
 
@@ -1332,10 +1366,10 @@ ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
        reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
 
        printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
-                       sdata->dev->name, bssid, reason_code);
+                       sdata->name, bssid, reason_code);
 
        if (!wk) {
-               ieee80211_set_disassoc(sdata);
+               ieee80211_set_disassoc(sdata, true);
        } else {
                list_del(&wk->list);
                kfree(wk);
@@ -1365,10 +1399,10 @@ ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
 
        reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
 
-       printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
-                       sdata->dev->name, reason_code);
+       printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
+                       sdata->name, mgmt->sa, reason_code);
 
-       ieee80211_set_disassoc(sdata);
+       ieee80211_set_disassoc(sdata, false);
        return RX_MGMT_CFG80211_DISASSOC;
 }
 
@@ -1410,7 +1444,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
 
        printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
               "status=%d aid=%d)\n",
-              sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
+              sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
               capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
 
        pos = mgmt->u.assoc_resp.variable;
@@ -1424,7 +1458,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                ms = tu * 1024 / 1000;
                printk(KERN_DEBUG "%s: AP rejected association temporarily; "
                       "comeback duration %u TU (%u ms)\n",
-                      sdata->dev->name, tu, ms);
+                      sdata->name, tu, ms);
                wk->timeout = jiffies + msecs_to_jiffies(ms);
                if (ms > IEEE80211_ASSOC_TIMEOUT)
                        run_again(ifmgd, jiffies + msecs_to_jiffies(ms));
@@ -1433,30 +1467,29 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
 
        if (status_code != WLAN_STATUS_SUCCESS) {
                printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
-                      sdata->dev->name, status_code);
-               list_del(&wk->list);
-               kfree(wk);
+                      sdata->name, status_code);
+               wk->state = IEEE80211_MGD_STATE_IDLE;
                return RX_MGMT_CFG80211_ASSOC;
        }
 
        if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
                printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
-                      "set\n", sdata->dev->name, aid);
+                      "set\n", sdata->name, aid);
        aid &= ~(BIT(15) | BIT(14));
 
        if (!elems.supp_rates) {
                printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
-                      sdata->dev->name);
+                      sdata->name);
                return RX_MGMT_NONE;
        }
 
-       printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
+       printk(KERN_DEBUG "%s: associated\n", sdata->name);
        ifmgd->aid = aid;
 
        rcu_read_lock();
 
        /* Add STA entry for the AP */
-       sta = sta_info_get(local, wk->bss->cbss.bssid);
+       sta = sta_info_get(sdata, wk->bss->cbss.bssid);
        if (!sta) {
                newsta = true;
 
@@ -1465,7 +1498,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                sta = sta_info_alloc(sdata, wk->bss->cbss.bssid, GFP_KERNEL);
                if (!sta) {
                        printk(KERN_DEBUG "%s: failed to alloc STA entry for"
-                              " the AP\n", sdata->dev->name);
+                              " the AP\n", sdata->name);
                        return RX_MGMT_NONE;
                }
 
@@ -1543,7 +1576,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                int err = sta_info_insert(sta);
                if (err) {
                        printk(KERN_DEBUG "%s: failed to insert STA entry for"
-                              " the AP (error %d)\n", sdata->dev->name, err);
+                              " the AP (error %d)\n", sdata->name, err);
                        rcu_read_unlock();
                        return RX_MGMT_NONE;
                }
@@ -1564,11 +1597,15 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                                               wk->bss->cbss.bssid,
                                               ap_ht_cap_flags);
 
+        /* delete work item -- must be before set_associated for PS */
+       list_del(&wk->list);
+
        /* set AID and assoc capability,
         * ieee80211_set_associated() will tell the driver */
        bss_conf->aid = aid;
        bss_conf->assoc_capability = capab_info;
-       ieee80211_set_associated(sdata, wk->bss, changed);
+       /* this will take ownership of wk */
+       ieee80211_set_associated(sdata, wk, changed);
 
        /*
         * Start timer to probe the connection to the AP now.
@@ -1577,8 +1614,6 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
        ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
        mod_beacon_timer(sdata);
 
-       list_del(&wk->list);
-       kfree(wk);
        return RX_MGMT_CFG80211_ASSOC;
 }
 
@@ -1636,7 +1671,7 @@ static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
 
        ASSERT_MGD_MTX(ifmgd);
 
-       if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
+       if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
                return; /* ignore ProbeResp to foreign address */
 
        baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
@@ -1650,8 +1685,8 @@ static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
 
        /* direct probe may be part of the association flow */
        if (wk && wk->state == IEEE80211_MGD_STATE_PROBE) {
-               printk(KERN_DEBUG "%s direct probe responded\n",
-                      sdata->dev->name);
+               printk(KERN_DEBUG "%s: direct probe responded\n",
+                      sdata->name);
                wk->tries = 0;
                wk->state = IEEE80211_MGD_STATE_AUTH;
                WARN_ON(ieee80211_authenticate(sdata, wk) != RX_MGMT_NONE);
@@ -1744,7 +1779,7 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
                if (net_ratelimit()) {
                        printk(KERN_DEBUG "%s: cancelling probereq poll due "
-                              "to a received beacon\n", sdata->dev->name);
+                              "to a received beacon\n", sdata->name);
                }
 #endif
                ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
@@ -1822,7 +1857,7 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
 
                rcu_read_lock();
 
-               sta = sta_info_get(local, bssid);
+               sta = sta_info_get(sdata, bssid);
                if (WARN_ON(!sta)) {
                        rcu_read_unlock();
                        return;
@@ -1841,12 +1876,8 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
                                               bssid, ap_ht_cap_flags);
        }
 
+       /* Note: country IE parsing is done for us by cfg80211 */
        if (elems.country_elem) {
-               /* Note we are only reviewing this on beacons
-                * for the BSSID we are associated to */
-               regulatory_hint_11d(local->hw.wiphy,
-                       elems.country_elem, elems.country_elem_len);
-
                /* TODO: IBSS also needs this */
                if (elems.pwr_constr_elem)
                        ieee80211_handle_pwr_constr(sdata,
@@ -1872,7 +1903,6 @@ ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
        fc = le16_to_cpu(mgmt->frame_control);
 
        switch (fc & IEEE80211_FCTL_STYPE) {
-       case IEEE80211_STYPE_PROBE_REQ:
        case IEEE80211_STYPE_PROBE_RESP:
        case IEEE80211_STYPE_BEACON:
        case IEEE80211_STYPE_AUTH:
@@ -1882,7 +1912,7 @@ ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
        case IEEE80211_STYPE_DISASSOC:
        case IEEE80211_STYPE_ACTION:
                skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
-               queue_work(local->hw.workqueue, &sdata->u.mgd.work);
+               ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
                return RX_QUEUED;
        }
 
@@ -1938,12 +1968,10 @@ static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
                        /* no action */
                        break;
                case RX_MGMT_CFG80211_DEAUTH:
-                       cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len,
-                                            NULL);
+                       cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
                        break;
                case RX_MGMT_CFG80211_DISASSOC:
-                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len,
-                                              NULL);
+                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
                        break;
                default:
                        WARN(1, "unexpected: %d", rma);
@@ -1997,6 +2025,9 @@ static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
        case RX_MGMT_CFG80211_ASSOC:
                cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, skb->len);
                break;
+       case RX_MGMT_CFG80211_DEAUTH:
+               cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
+               break;
        default:
                WARN(1, "unexpected: %d", rma);
        }
@@ -2017,7 +2048,7 @@ static void ieee80211_sta_timer(unsigned long data)
                return;
        }
 
-       queue_work(local->hw.workqueue, &ifmgd->work);
+       ieee80211_queue_work(&local->hw, &ifmgd->work);
 }
 
 static void ieee80211_sta_work(struct work_struct *work)
@@ -2035,20 +2066,18 @@ static void ieee80211_sta_work(struct work_struct *work)
        if (!netif_running(sdata->dev))
                return;
 
-       if (local->sw_scanning || local->hw_scanning)
+       if (local->scanning)
                return;
 
        if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
                return;
 
        /*
-        * Nothing should have been stuffed into the workqueue during
-        * the suspend->resume cycle. If this WARN is seen then there
-        * is a bug with either the driver suspend or something in
-        * mac80211 stuffing into the workqueue which we haven't yet
-        * cleared during mac80211's suspend cycle.
+        * ieee80211_queue_work() should have picked up most cases,
+        * here we'll pick the the rest.
         */
-       if (WARN_ON(local->suspended))
+       if (WARN(local->suspended, "STA MLME work scheduled while "
+                "going to suspend\n"))
                return;
 
        ifmgd = &sdata->u.mgd;
@@ -2063,21 +2092,31 @@ static void ieee80211_sta_work(struct work_struct *work)
        if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
                            IEEE80211_STA_CONNECTION_POLL) &&
            ifmgd->associated) {
+               u8 bssid[ETH_ALEN];
+
+               memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
                if (time_is_after_jiffies(ifmgd->probe_timeout))
                        run_again(ifmgd, ifmgd->probe_timeout);
-               else {
-                       u8 bssid[ETH_ALEN];
+
+               else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
+#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
+                       printk(KERN_DEBUG "No probe response from AP %pM"
+                               " after %dms, try %d\n", bssid,
+                               (1000 * IEEE80211_PROBE_WAIT)/HZ,
+                               ifmgd->probe_send_count);
+#endif
+                       ieee80211_mgd_probe_ap_send(sdata);
+               } else {
                        /*
                         * We actually lost the connection ... or did we?
                         * Let's make sure!
                         */
                        ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
                                          IEEE80211_STA_BEACON_POLL);
-                       memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
                        printk(KERN_DEBUG "No probe response from AP %pM"
                                " after %dms, disconnecting.\n",
                                bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
-                       ieee80211_set_disassoc(sdata);
+                       ieee80211_set_disassoc(sdata, true);
                        mutex_unlock(&ifmgd->mtx);
                        /*
                         * must be outside lock due to cfg80211,
@@ -2091,25 +2130,9 @@ static void ieee80211_sta_work(struct work_struct *work)
                }
        }
 
-       list_for_each_entry(wk, &ifmgd->work_list, list) {
-               if (wk->state != IEEE80211_MGD_STATE_IDLE) {
-                       anybusy = true;
-                       break;
-               }
-       }
 
        ieee80211_recalc_idle(local);
 
-       if (!anybusy) {
-               mutex_unlock(&ifmgd->mtx);
-
-               if (test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
-                       queue_delayed_work(local->hw.workqueue,
-                                          &local->scan_work,
-                                          round_jiffies_relative(0));
-               return;
-       }
-
        list_for_each_entry_safe(wk, tmp, &ifmgd->work_list, list) {
                if (time_is_after_jiffies(wk->timeout)) {
                        /*
@@ -2155,6 +2178,18 @@ static void ieee80211_sta_work(struct work_struct *work)
                }
        }
 
+       list_for_each_entry(wk, &ifmgd->work_list, list) {
+               if (wk->state != IEEE80211_MGD_STATE_IDLE) {
+                       anybusy = true;
+                       break;
+               }
+       }
+       if (!anybusy &&
+           test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
+               ieee80211_queue_delayed_work(&local->hw,
+                                            &local->scan_work,
+                                            round_jiffies_relative(0));
+
        mutex_unlock(&ifmgd->mtx);
 
        list_for_each_entry_safe(wk, tmp, &free_work, list) {
@@ -2187,8 +2222,7 @@ static void ieee80211_sta_bcn_mon_timer(unsigned long data)
        if (local->quiescing)
                return;
 
-       queue_work(sdata->local->hw.workqueue,
-                  &sdata->u.mgd.beacon_loss_work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
 }
 
 static void ieee80211_sta_conn_mon_timer(unsigned long data)
@@ -2201,7 +2235,7 @@ static void ieee80211_sta_conn_mon_timer(unsigned long data)
        if (local->quiescing)
                return;
 
-       queue_work(local->hw.workqueue, &ifmgd->monitor_work);
+       ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
 }
 
 static void ieee80211_sta_monitor_work(struct work_struct *work)
@@ -2220,10 +2254,10 @@ static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
                                        IEEE80211_STA_CONNECTION_POLL);
 
                /* let's probe the connection once */
-               queue_work(sdata->local->hw.workqueue,
+               ieee80211_queue_work(&sdata->local->hw,
                           &sdata->u.mgd.monitor_work);
                /* and do all the other regular work too */
-               queue_work(sdata->local->hw.workqueue,
+               ieee80211_queue_work(&sdata->local->hw,
                           &sdata->u.mgd.work);
        }
 }
@@ -2371,6 +2405,7 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
 
        wk->state = IEEE80211_MGD_STATE_PROBE;
        wk->auth_alg = auth_alg;
+       wk->timeout = jiffies; /* run right away */
 
        /*
         * XXX: if still associated need to tell AP that we're going
@@ -2383,7 +2418,7 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
        list_add(&wk->list, &sdata->u.mgd.work_list);
        mutex_unlock(&ifmgd->mtx);
 
-       queue_work(sdata->local->hw.workqueue, &sdata->u.mgd.work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
        return 0;
 }
 
@@ -2442,6 +2477,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 
        wk->state = IEEE80211_MGD_STATE_ASSOC;
        wk->tries = 0;
+       wk->timeout = jiffies; /* run right away */
 
        if (req->use_mfp) {
                ifmgd->mfp = IEEE80211_MFP_REQUIRED;
@@ -2456,7 +2492,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
        else
                ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
 
-       queue_work(sdata->local->hw.workqueue, &sdata->u.mgd.work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
 
        err = 0;
 
@@ -2472,32 +2508,54 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        struct ieee80211_mgd_work *wk;
        const u8 *bssid = NULL;
-
-       printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
-              sdata->dev->name, req->reason_code);
+       bool not_auth_yet = false;
 
        mutex_lock(&ifmgd->mtx);
 
        if (ifmgd->associated && &ifmgd->associated->cbss == req->bss) {
                bssid = req->bss->bssid;
-               ieee80211_set_disassoc(sdata);
+               ieee80211_set_disassoc(sdata, true);
        } else list_for_each_entry(wk, &ifmgd->work_list, list) {
                if (&wk->bss->cbss == req->bss) {
                        bssid = req->bss->bssid;
+                       if (wk->state == IEEE80211_MGD_STATE_PROBE)
+                               not_auth_yet = true;
                        list_del(&wk->list);
                        kfree(wk);
                        break;
                }
        }
 
-       /* cfg80211 should catch this... */
-       if (WARN_ON(!bssid)) {
+       /*
+        * If somebody requests authentication and we haven't
+        * sent out an auth frame yet there's no need to send
+        * out a deauth frame either. If the state was PROBE,
+        * then this is the case. If it's AUTH we have sent a
+        * frame, and if it's IDLE we have completed the auth
+        * process already.
+        */
+       if (not_auth_yet) {
+               mutex_unlock(&ifmgd->mtx);
+               __cfg80211_auth_canceled(sdata->dev, bssid);
+               return 0;
+       }
+
+       /*
+        * cfg80211 should catch this ... but it's racy since
+        * we can receive a deauth frame, process it, hand it
+        * to cfg80211 while that's in a locked section already
+        * trying to tell us that the user wants to disconnect.
+        */
+       if (!bssid) {
                mutex_unlock(&ifmgd->mtx);
                return -ENOLINK;
        }
 
        mutex_unlock(&ifmgd->mtx);
 
+       printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
+              sdata->name, bssid, req->reason_code);
+
        ieee80211_send_deauth_disassoc(sdata, bssid,
                        IEEE80211_STYPE_DEAUTH, req->reason_code,
                        cookie);
@@ -2511,18 +2569,23 @@ int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 
-       printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
-              sdata->dev->name, req->reason_code);
-
        mutex_lock(&ifmgd->mtx);
 
-       /* cfg80211 should catch that */
-       if (WARN_ON(&ifmgd->associated->cbss != req->bss)) {
+       /*
+        * cfg80211 should catch this ... but it's racy since
+        * we can receive a disassoc frame, process it, hand it
+        * to cfg80211 while that's in a locked section already
+        * trying to tell us that the user wants to disconnect.
+        */
+       if (&ifmgd->associated->cbss != req->bss) {
                mutex_unlock(&ifmgd->mtx);
                return -ENOLINK;
        }
 
-       ieee80211_set_disassoc(sdata);
+       printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
+              sdata->name, req->bss->bssid, req->reason_code);
+
+       ieee80211_set_disassoc(sdata, false);
 
        mutex_unlock(&ifmgd->mtx);