mac80211: introduce hw config change flags
[safe/jmp/linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00mac.c
index 1253da8..da7b49a 100644 (file)
@@ -36,21 +36,22 @@ static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
        struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(frag_skb);
        struct ieee80211_tx_info *rts_info;
        struct sk_buff *skb;
-       int size;
+       unsigned int data_length;
+       int retval = 0;
 
        if (tx_info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
-               size = sizeof(struct ieee80211_cts);
+               data_length = sizeof(struct ieee80211_cts);
        else
-               size = sizeof(struct ieee80211_rts);
+               data_length = sizeof(struct ieee80211_rts);
 
-       skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
-       if (!skb) {
+       skb = dev_alloc_skb(data_length + rt2x00dev->hw->extra_tx_headroom);
+       if (unlikely(!skb)) {
                WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
-               return NETDEV_TX_BUSY;
+               return -ENOMEM;
        }
 
        skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
-       skb_put(skb, size);
+       skb_put(skb, data_length);
 
        /*
         * Copy TX information over from original frame to
@@ -63,7 +64,6 @@ static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
         */
        memcpy(skb->cb, frag_skb->cb, sizeof(skb->cb));
        rts_info = IEEE80211_SKB_CB(skb);
-       rts_info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
        rts_info->flags &= ~IEEE80211_TX_CTL_USE_RTS_CTS;
        rts_info->flags &= ~IEEE80211_TX_CTL_USE_CTS_PROTECT;
        rts_info->flags &= ~IEEE80211_TX_CTL_REQ_TX_STATUS;
@@ -73,21 +73,33 @@ static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
        else
                rts_info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
 
+       skb->do_not_encrypt = 1;
+
+       /*
+        * RTS/CTS frame should use the length of the frame plus any
+        * encryption overhead that will be added by the hardware.
+        */
+#ifdef CONFIG_RT2X00_LIB_CRYPTO
+       if (!frag_skb->do_not_encrypt)
+               data_length += rt2x00crypto_tx_overhead(tx_info);
+#endif /* CONFIG_RT2X00_LIB_CRYPTO */
+
        if (tx_info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
                ieee80211_ctstoself_get(rt2x00dev->hw, tx_info->control.vif,
-                                       frag_skb->data, size, tx_info,
+                                       frag_skb->data, data_length, tx_info,
                                        (struct ieee80211_cts *)(skb->data));
        else
                ieee80211_rts_get(rt2x00dev->hw, tx_info->control.vif,
-                                 frag_skb->data, size, tx_info,
+                                 frag_skb->data, data_length, tx_info,
                                  (struct ieee80211_rts *)(skb->data));
 
-       if (rt2x00queue_write_tx_frame(queue, skb)) {
+       retval = rt2x00queue_write_tx_frame(queue, skb);
+       if (retval) {
+               dev_kfree_skb_any(skb);
                WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
-               return NETDEV_TX_BUSY;
        }
 
-       return NETDEV_TX_OK;
+       return retval;
 }
 
 int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
@@ -105,11 +117,8 @@ int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
         * Note that we can only stop the TX queues inside the TX path
         * due to possible race conditions in mac80211.
         */
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
-               ieee80211_stop_queues(hw);
-               dev_kfree_skb_any(skb);
-               return NETDEV_TX_OK;
-       }
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
+               goto exit_fail;
 
        /*
         * Determine which queue to put packet on.
@@ -140,26 +149,25 @@ int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
        if ((tx_info->flags & (IEEE80211_TX_CTL_USE_RTS_CTS |
                               IEEE80211_TX_CTL_USE_CTS_PROTECT)) &&
            !rt2x00dev->ops->hw->set_rts_threshold) {
-               if (rt2x00queue_available(queue) <= 1) {
-                       ieee80211_stop_queue(rt2x00dev->hw, qid);
-                       return NETDEV_TX_BUSY;
-               }
-
-               if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb)) {
-                       ieee80211_stop_queue(rt2x00dev->hw, qid);
-                       return NETDEV_TX_BUSY;
-               }
-       }
+               if (rt2x00queue_available(queue) <= 1)
+                       goto exit_fail;
 
-       if (rt2x00queue_write_tx_frame(queue, skb)) {
-               ieee80211_stop_queue(rt2x00dev->hw, qid);
-               return NETDEV_TX_BUSY;
+               if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb))
+                       goto exit_fail;
        }
 
+       if (rt2x00queue_write_tx_frame(queue, skb))
+               goto exit_fail;
+
        if (rt2x00queue_threshold(queue))
                ieee80211_stop_queue(rt2x00dev->hw, qid);
 
        return NETDEV_TX_OK;
+
+ exit_fail:
+       ieee80211_stop_queue(rt2x00dev->hw, qid);
+       dev_kfree_skb_any(skb);
+       return NETDEV_TX_OK;
 }
 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
 
@@ -167,7 +175,7 @@ int rt2x00mac_start(struct ieee80211_hw *hw)
 {
        struct rt2x00_dev *rt2x00dev = hw->priv;
 
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
                return 0;
 
        return rt2x00lib_start(rt2x00dev);
@@ -178,7 +186,7 @@ void rt2x00mac_stop(struct ieee80211_hw *hw)
 {
        struct rt2x00_dev *rt2x00dev = hw->priv;
 
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
                return;
 
        rt2x00lib_stop(rt2x00dev);
@@ -198,27 +206,47 @@ int rt2x00mac_add_interface(struct ieee80211_hw *hw,
         * Don't allow interfaces to be added
         * the device has disappeared.
         */
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
-           !test_bit(DEVICE_STARTED, &rt2x00dev->flags))
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
+           !test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
                return -ENODEV;
 
-       /*
-        * We don't support mixed combinations of sta and ap virtual
-        * interfaces. We can only add this interface when the rival
-        * interface count is 0.
-        */
-       if ((conf->type == IEEE80211_IF_TYPE_AP && rt2x00dev->intf_sta_count) ||
-           (conf->type != IEEE80211_IF_TYPE_AP && rt2x00dev->intf_ap_count))
-               return -ENOBUFS;
-
-       /*
-        * Check if we exceeded the maximum amount of supported interfaces.
-        */
-       if ((conf->type == IEEE80211_IF_TYPE_AP &&
-            rt2x00dev->intf_ap_count >= rt2x00dev->ops->max_ap_intf) ||
-           (conf->type != IEEE80211_IF_TYPE_AP &&
-            rt2x00dev->intf_sta_count >= rt2x00dev->ops->max_sta_intf))
-               return -ENOBUFS;
+       switch (conf->type) {
+       case NL80211_IFTYPE_AP:
+               /*
+                * We don't support mixed combinations of
+                * sta and ap interfaces.
+                */
+               if (rt2x00dev->intf_sta_count)
+                       return -ENOBUFS;
+
+               /*
+                * Check if we exceeded the maximum amount
+                * of supported interfaces.
+                */
+               if (rt2x00dev->intf_ap_count >= rt2x00dev->ops->max_ap_intf)
+                       return -ENOBUFS;
+
+               break;
+       case NL80211_IFTYPE_STATION:
+       case NL80211_IFTYPE_ADHOC:
+               /*
+                * We don't support mixed combinations of
+                * sta and ap interfaces.
+                */
+               if (rt2x00dev->intf_ap_count)
+                       return -ENOBUFS;
+
+               /*
+                * Check if we exceeded the maximum amount
+                * of supported interfaces.
+                */
+               if (rt2x00dev->intf_sta_count >= rt2x00dev->ops->max_sta_intf)
+                       return -ENOBUFS;
+
+               break;
+       default:
+               return -EINVAL;
+       }
 
        /*
         * Loop through all beacon queues to find a free
@@ -228,7 +256,7 @@ int rt2x00mac_add_interface(struct ieee80211_hw *hw,
         */
        for (i = 0; i < queue->limit; i++) {
                entry = &queue->entries[i];
-               if (!__test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
+               if (!test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
                        break;
        }
 
@@ -240,15 +268,16 @@ int rt2x00mac_add_interface(struct ieee80211_hw *hw,
         * increase interface count and start initialization.
         */
 
-       if (conf->type == IEEE80211_IF_TYPE_AP)
+       if (conf->type == NL80211_IFTYPE_AP)
                rt2x00dev->intf_ap_count++;
        else
                rt2x00dev->intf_sta_count++;
 
        spin_lock_init(&intf->lock);
+       spin_lock_init(&intf->seqlock);
        intf->beacon = entry;
 
-       if (conf->type == IEEE80211_IF_TYPE_AP)
+       if (conf->type == NL80211_IFTYPE_AP)
                memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
        memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
 
@@ -281,12 +310,12 @@ void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
         * either the device has disappeared or when
         * no interface is present.
         */
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
-           (conf->type == IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_ap_count) ||
-           (conf->type != IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_sta_count))
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
+           (conf->type == NL80211_IFTYPE_AP && !rt2x00dev->intf_ap_count) ||
+           (conf->type != NL80211_IFTYPE_AP && !rt2x00dev->intf_sta_count))
                return;
 
-       if (conf->type == IEEE80211_IF_TYPE_AP)
+       if (conf->type == NL80211_IFTYPE_AP)
                rt2x00dev->intf_ap_count--;
        else
                rt2x00dev->intf_sta_count--;
@@ -295,48 +324,60 @@ void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
         * Release beacon entry so it is available for
         * new interfaces again.
         */
-       __clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
+       clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
 
        /*
         * Make sure the bssid and mac address registers
         * are cleared to prevent false ACKing of frames.
         */
        rt2x00lib_config_intf(rt2x00dev, intf,
-                             IEEE80211_IF_TYPE_INVALID, NULL, NULL);
+                             NL80211_IFTYPE_UNSPECIFIED, NULL, NULL);
 }
 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
 
-int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
+int rt2x00mac_config(struct ieee80211_hw *hw, u32 changed)
 {
        struct rt2x00_dev *rt2x00dev = hw->priv;
+       struct ieee80211_conf *conf = &hw->conf;
+       int radio_on;
+       int status;
 
        /*
         * Mac80211 might be calling this function while we are trying
         * to remove the device or perhaps suspending it.
         */
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
                return 0;
 
        /*
-        * Check if we need to disable the radio,
-        * if this is not the case, at least the RX must be disabled.
+        * Only change device state when the radio is enabled. It does not
+        * matter what parameters we have configured when the radio is disabled
+        * because we won't be able to send or receive anyway. Also note that
+        * some configuration parameters (e.g. channel and antenna values) can
+        * only be set when the radio is enabled.
         */
-       if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
-               if (!conf->radio_enabled)
-                       rt2x00lib_disable_radio(rt2x00dev);
-               else
-                       rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
-       }
-
-       rt2x00lib_config(rt2x00dev, conf, 0);
-
-       /*
-        * Reenable RX only if the radio should be on.
-        */
-       if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
+       radio_on = test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
+       if (conf->radio_enabled) {
+               /* For programming the values, we have to turn RX off */
+               rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
+
+               /* Enable the radio */
+               status = rt2x00lib_enable_radio(rt2x00dev);
+               if (unlikely(status))
+                       return status;
+
+               /*
+                * When we've just turned on the radio, we want to reprogram
+                * everything to ensure a consistent state
+                */
+               rt2x00lib_config(rt2x00dev, conf, !radio_on);
+
+               /* Turn RX back on */
                rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
-       else if (conf->radio_enabled)
-               return rt2x00lib_enable_radio(rt2x00dev);
+       } else {
+               /* Disable the radio */
+               rt2x00lib_disable_radio(rt2x00dev);
+       }
 
        return 0;
 }
@@ -348,24 +389,26 @@ int rt2x00mac_config_interface(struct ieee80211_hw *hw,
 {
        struct rt2x00_dev *rt2x00dev = hw->priv;
        struct rt2x00_intf *intf = vif_to_intf(vif);
-       int status;
+       int update_bssid = 0;
+       int status = 0;
 
        /*
         * Mac80211 might be calling this function while we are trying
         * to remove the device or perhaps suspending it.
         */
-       if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
+       if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
                return 0;
 
        spin_lock(&intf->lock);
 
        /*
-        * If the interface does not work in master mode,
-        * then the bssid value in the interface structure
-        * should now be set.
+        * conf->bssid can be NULL if coming from the internal
+        * beacon update routine.
         */
-       if (conf->type != IEEE80211_IF_TYPE_AP)
+       if (conf->changed & IEEE80211_IFCC_BSSID && conf->bssid) {
+               update_bssid = 1;
                memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
+       }
 
        spin_unlock(&intf->lock);
 
@@ -375,17 +418,14 @@ int rt2x00mac_config_interface(struct ieee80211_hw *hw,
         * values as arguments we make keep access to rt2x00_intf thread safe
         * even without the lock.
         */
-       rt2x00lib_config_intf(rt2x00dev, intf, conf->type, NULL, conf->bssid);
+       rt2x00lib_config_intf(rt2x00dev, intf, vif->type, NULL,
+                             update_bssid ? conf->bssid : NULL);
 
        /*
-        * We only need to initialize the beacon when master mode is enabled.
+        * Update the beacon.
         */
-       if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
-               return 0;
-
-       status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, conf->beacon);
-       if (status)
-               dev_kfree_skb(conf->beacon);
+       if (conf->changed & IEEE80211_IFCC_BEACON)
+               status = rt2x00queue_update_beacon(rt2x00dev, vif);
 
        return status;
 }
@@ -431,10 +471,95 @@ void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
        if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
                rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
        else
-               queue_work(rt2x00dev->workqueue, &rt2x00dev->filter_work);
+               queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->filter_work);
 }
 EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
 
+#ifdef CONFIG_RT2X00_LIB_CRYPTO
+int rt2x00mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
+                     const u8 *local_address, const u8 *address,
+                     struct ieee80211_key_conf *key)
+{
+       struct rt2x00_dev *rt2x00dev = hw->priv;
+       int (*set_key) (struct rt2x00_dev *rt2x00dev,
+                       struct rt2x00lib_crypto *crypto,
+                       struct ieee80211_key_conf *key);
+       struct rt2x00lib_crypto crypto;
+
+       if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
+               return -EOPNOTSUPP;
+       else if (key->keylen > 32)
+               return -ENOSPC;
+
+       memset(&crypto, 0, sizeof(crypto));
+
+       /*
+        * When in STA mode, bssidx is always 0 otherwise local_address[5]
+        * contains the bss number, see BSS_ID_MASK comments for details.
+        */
+       if (rt2x00dev->intf_sta_count)
+               crypto.bssidx = 0;
+       else
+               crypto.bssidx =
+                   local_address[5] & (rt2x00dev->ops->max_ap_intf - 1);
+
+       crypto.cipher = rt2x00crypto_key_to_cipher(key);
+       if (crypto.cipher == CIPHER_NONE)
+               return -EOPNOTSUPP;
+
+       crypto.cmd = cmd;
+       crypto.address = address;
+
+       if (crypto.cipher == CIPHER_TKIP) {
+               if (key->keylen > NL80211_TKIP_DATA_OFFSET_ENCR_KEY)
+                       memcpy(&crypto.key,
+                              &key->key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY],
+                              sizeof(crypto.key));
+
+               if (key->keylen > NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
+                       memcpy(&crypto.tx_mic,
+                              &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
+                              sizeof(crypto.tx_mic));
+
+               if (key->keylen > NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY)
+                       memcpy(&crypto.rx_mic,
+                              &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
+                              sizeof(crypto.rx_mic));
+       } else
+               memcpy(&crypto.key, &key->key[0], key->keylen);
+
+       /*
+        * Each BSS has a maximum of 4 shared keys.
+        * Shared key index values:
+        *      0) BSS0 key0
+        *      1) BSS0 key1
+        *      ...
+        *      4) BSS1 key0
+        *      ...
+        *      8) BSS2 key0
+        *      ...
+        * Both pairwise as shared key indeces are determined by
+        * driver. This is required because the hardware requires
+        * keys to be assigned in correct order (When key 1 is
+        * provided but key 0 is not, then the key is not found
+        * by the hardware during RX).
+        */
+       if (cmd == SET_KEY)
+               key->hw_key_idx = 0;
+
+       if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
+               set_key = rt2x00dev->ops->lib->config_pairwise_key;
+       else
+               set_key = rt2x00dev->ops->lib->config_shared_key;
+
+       if (!set_key)
+               return -EOPNOTSUPP;
+
+       return set_key(rt2x00dev, &crypto, key);
+}
+EXPORT_SYMBOL_GPL(rt2x00mac_set_key);
+#endif /* CONFIG_RT2X00_LIB_CRYPTO */
+
 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
                        struct ieee80211_low_level_stats *stats)
 {
@@ -501,7 +626,7 @@ void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
         * When the erp information has changed, we should perform
         * additional configuration steps. For all other changes we are done.
         */
-       if (changes & BSS_CHANGED_ERP_PREAMBLE) {
+       if (changes & (BSS_CHANGED_ERP_PREAMBLE | BSS_CHANGED_ERP_CTS_PROT)) {
                if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
                        rt2x00lib_config_erp(rt2x00dev, intf, bss_conf);
                else
@@ -512,7 +637,7 @@ void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
        memcpy(&intf->conf, bss_conf, sizeof(*bss_conf));
        if (delayed) {
                intf->delayed_flags |= delayed;
-               queue_work(rt2x00dev->workqueue, &rt2x00dev->intf_work);
+               schedule_work(&rt2x00dev->intf_work);
        }
        spin_unlock(&intf->lock);
 }
@@ -542,14 +667,12 @@ int rt2x00mac_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
        else
                queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
 
-       if (params->aifs >= 0)
-               queue->aifs = params->aifs;
-       else
-               queue->aifs = 2;
+       queue->aifs = params->aifs;
+       queue->txop = params->txop;
 
        INFO(rt2x00dev,
-            "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
-            queue_idx, queue->cw_min, queue->cw_max, queue->aifs);
+            "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d, TXop: %d.\n",
+            queue_idx, queue->cw_min, queue->cw_max, queue->aifs, queue->txop);
 
        return 0;
 }