rt2x00: Fix HW crypto key handling
[pandora-kernel.git] / drivers / net / wireless / rt2x00 / rt2x00mac.c
index d065073..2c6cc5c 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->control.hw_key = NULL;
        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,22 +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)
@@ -106,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.
@@ -141,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);
 
@@ -168,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);
@@ -179,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);
@@ -199,12 +206,12 @@ 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;
 
        switch (conf->type) {
-       case IEEE80211_IF_TYPE_AP:
+       case NL80211_IFTYPE_AP:
                /*
                 * We don't support mixed combinations of
                 * sta and ap interfaces.
@@ -220,8 +227,8 @@ int rt2x00mac_add_interface(struct ieee80211_hw *hw,
                        return -ENOBUFS;
 
                break;
-       case IEEE80211_IF_TYPE_STA:
-       case IEEE80211_IF_TYPE_IBSS:
+       case NL80211_IFTYPE_STATION:
+       case NL80211_IFTYPE_ADHOC:
                /*
                 * We don't support mixed combinations of
                 * sta and ap interfaces.
@@ -249,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;
        }
 
@@ -261,7 +268,7 @@ 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++;
@@ -270,7 +277,7 @@ int rt2x00mac_add_interface(struct ieee80211_hw *hw,
        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);
 
@@ -303,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--;
@@ -317,59 +324,59 @@ 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)
 {
        struct rt2x00_dev *rt2x00dev = hw->priv;
-       int force_reconfig;
+       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);
-       }
+       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);
 
-       /*
-        * When the DEVICE_DIRTY_CONFIG flag is set, the device has recently
-        * been started and the configuration must be forced upon the hardware.
-        * Otherwise registers will not be intialized correctly and could
-        * result in non-working hardware because essential registers aren't
-        * initialized.
-        */
-       force_reconfig =
-           __test_and_clear_bit(DEVICE_DIRTY_CONFIG, &rt2x00dev->flags);
+               /* Enable the radio */
+               status = rt2x00lib_enable_radio(rt2x00dev);
+               if (unlikely(status))
+                       return status;
 
-       rt2x00lib_config(rt2x00dev, conf, force_reconfig);
+               /*
+                * When we've just turned on the radio, we want to reprogram
+                * everything to ensure a consistent state
+                */
+               rt2x00lib_config(rt2x00dev, conf, !radio_on);
 
-       /*
-        * Reenable RX only if the radio should be on.
-        */
-       if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
+               /* 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;
 }
@@ -388,7 +395,7 @@ int rt2x00mac_config_interface(struct ieee80211_hw *hw,
         * 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);
@@ -467,6 +474,91 @@ void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
 }
 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)
 {
@@ -575,10 +667,11 @@ int rt2x00mac_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
                queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
 
        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;
 }