mwl8k: add support for enabling hardware sniffer mode
[safe/jmp/linux-2.6] / drivers / net / wireless / mwl8k.c
index 53447f6..94cbf93 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
+#include <linux/sched.h>
 #include <linux/spinlock.h>
 #include <linux/list.h>
 #include <linux/pci.h>
@@ -83,12 +84,6 @@ MODULE_DEVICE_TABLE(pci, mwl8k_table);
                                 MWL8K_A2H_INT_RX_READY | \
                                 MWL8K_A2H_INT_TX_DONE)
 
-/* WME stream classes */
-#define WME_AC_BE      0               /* best effort */
-#define WME_AC_BK      1               /* background */
-#define WME_AC_VI      2               /* video */
-#define WME_AC_VO      3               /* voice */
-
 #define MWL8K_RX_QUEUES                1
 #define MWL8K_TX_QUEUES                4
 
@@ -174,10 +169,11 @@ struct mwl8k_priv {
        /* PHY parameters */
        struct ieee80211_supported_band band;
        struct ieee80211_channel channels[14];
-       struct ieee80211_rate rates[12];
+       struct ieee80211_rate rates[13];
 
        bool radio_on;
        bool radio_short_preamble;
+       bool sniffer_enabled;
        bool wmm_enabled;
 
        /* XXX need to convert this to handle multiple interfaces */
@@ -213,7 +209,7 @@ struct mwl8k_vif {
         * Subset of supported legacy rates.
         * Intersection of AP and STA supported rates.
         */
-       struct ieee80211_rate legacy_rates[12];
+       struct ieee80211_rate legacy_rates[13];
 
        /* number of supported legacy rates */
        u8      legacy_nrates;
@@ -245,9 +241,10 @@ static const struct ieee80211_rate mwl8k_rates[] = {
        { .bitrate = 10, .hw_value = 2, },
        { .bitrate = 20, .hw_value = 4, },
        { .bitrate = 55, .hw_value = 11, },
+       { .bitrate = 110, .hw_value = 22, },
+       { .bitrate = 220, .hw_value = 44, },
        { .bitrate = 60, .hw_value = 12, },
        { .bitrate = 90, .hw_value = 18, },
-       { .bitrate = 110, .hw_value = 22, },
        { .bitrate = 120, .hw_value = 24, },
        { .bitrate = 180, .hw_value = 36, },
        { .bitrate = 240, .hw_value = 48, },
@@ -280,6 +277,7 @@ static const struct ieee80211_rate mwl8k_rates[] = {
 #define MWL8K_CMD_MIMO_CONFIG          0x0125
 #define MWL8K_CMD_USE_FIXED_RATE       0x0126
 #define MWL8K_CMD_ENABLE_SNIFFER       0x0150
+#define MWL8K_CMD_SET_MAC_ADDR         0x0202
 #define MWL8K_CMD_SET_RATEADAPT_MODE   0x0203
 #define MWL8K_CMD_UPDATE_STADB         0x1123
 
@@ -309,6 +307,7 @@ static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
                MWL8K_CMDNAME(MIMO_CONFIG);
                MWL8K_CMDNAME(USE_FIXED_RATE);
                MWL8K_CMDNAME(ENABLE_SNIFFER);
+               MWL8K_CMDNAME(SET_MAC_ADDR);
                MWL8K_CMDNAME(SET_RATEADAPT_MODE);
                MWL8K_CMDNAME(UPDATE_STADB);
        default:
@@ -426,6 +425,7 @@ mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
                        break;
                }
 
+               cond_resched();
                udelay(1);
        } while (--loops);
 
@@ -603,7 +603,7 @@ struct ewc_ht_info {
 /* Peer Entry flags - used to define the type of the peer node */
 #define MWL8K_PEER_TYPE_ACCESSPOINT    2
 
-#define MWL8K_IEEE_LEGACY_DATA_RATES   12
+#define MWL8K_IEEE_LEGACY_DATA_RATES   13
 #define MWL8K_MCS_BITMAP_SIZE          16
 
 struct peer_capability_info {
@@ -753,6 +753,13 @@ struct mwl8k_rx_desc {
 #define MWL8K_RX_DESCS         256
 #define MWL8K_RX_MAXSZ         3800
 
+#define RATE_INFO_SHORTPRE             0x8000
+#define RATE_INFO_ANTSELECT(x)         (((x) >> 11) & 0x3)
+#define RATE_INFO_RATEID(x)            (((x) >> 3) & 0x3f)
+#define RATE_INFO_40MHZ                        0x0004
+#define RATE_INFO_SHORTGI              0x0002
+#define RATE_INFO_MCS_FORMAT           0x0001
+
 static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
 {
        struct mwl8k_priv *priv = hw->priv;
@@ -909,6 +916,7 @@ static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
                struct ieee80211_rx_status status;
                unsigned long addr;
                struct ieee80211_hdr *wh;
+               u16 rate_info;
 
                rx_desc = rxq->rx_desc_area + rxq->rx_head;
                if (!(rx_desc->rx_ctrl & MWL8K_RX_CTRL_OWNED_BY_HOST))
@@ -940,14 +948,24 @@ static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
                if (mwl8k_capture_bssid(priv, wh))
                        mwl8k_save_beacon(hw, skb);
 
+               rate_info = le16_to_cpu(rx_desc->rate_info);
+
                memset(&status, 0, sizeof(status));
                status.mactime = 0;
                status.signal = -rx_desc->rssi;
                status.noise = -rx_desc->noise_level;
                status.qual = rx_desc->link_quality;
-               status.antenna = 1;
-               status.rate_idx = 1;
+               status.antenna = RATE_INFO_ANTSELECT(rate_info);
+               status.rate_idx = RATE_INFO_RATEID(rate_info);
                status.flag = 0;
+               if (rate_info & RATE_INFO_SHORTPRE)
+                       status.flag |= RX_FLAG_SHORTPRE;
+               if (rate_info & RATE_INFO_40MHZ)
+                       status.flag |= RX_FLAG_40MHZ;
+               if (rate_info & RATE_INFO_SHORTGI)
+                       status.flag |= RX_FLAG_SHORT_GI;
+               if (rate_info & RATE_INFO_MCS_FORMAT)
+                       status.flag |= RX_FLAG_HT;
                status.band = IEEE80211_BAND_2GHZ;
                status.freq = ieee80211_channel_to_frequency(rx_desc->channel);
                memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
@@ -964,24 +982,10 @@ static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
  * Packet transmission.
  */
 
-/* Transmit queue assignment.  */
-enum {
-       MWL8K_WME_AC_BK = 0,            /* background access */
-       MWL8K_WME_AC_BE = 1,            /* best effort access */
-       MWL8K_WME_AC_VI = 2,            /* video access */
-       MWL8K_WME_AC_VO = 3,            /* voice access */
-};
-
 /* Transmit packet ACK policy */
 #define MWL8K_TXD_ACK_POLICY_NORMAL            0
 #define MWL8K_TXD_ACK_POLICY_BLOCKACK          3
 
-#define GET_TXQ(_ac) (\
-               ((_ac) == WME_AC_VO) ? MWL8K_WME_AC_VO : \
-               ((_ac) == WME_AC_VI) ? MWL8K_WME_AC_VI : \
-               ((_ac) == WME_AC_BK) ? MWL8K_WME_AC_BK : \
-               MWL8K_WME_AC_BE)
-
 #define MWL8K_TXD_STATUS_OK                    0x00000001
 #define MWL8K_TXD_STATUS_OK_RETRY              0x00000002
 #define MWL8K_TXD_STATUS_OK_MORE_RETRY         0x00000004
@@ -1903,6 +1907,34 @@ static int mwl8k_enable_sniffer(struct ieee80211_hw *hw, bool enable)
 }
 
 /*
+ * CMD_SET_MAC_ADDR.
+ */
+struct mwl8k_cmd_set_mac_addr {
+       struct mwl8k_cmd_pkt header;
+       __u8 mac_addr[ETH_ALEN];
+} __attribute__((packed));
+
+static int mwl8k_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
+{
+       struct mwl8k_cmd_set_mac_addr *cmd;
+       int rc;
+
+       cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
+       if (cmd == NULL)
+               return -ENOMEM;
+
+       cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
+       cmd->header.length = cpu_to_le16(sizeof(*cmd));
+       memcpy(cmd->mac_addr, mac, ETH_ALEN);
+
+       rc = mwl8k_post_cmd(hw, &cmd->header);
+       kfree(cmd);
+
+       return rc;
+}
+
+
+/*
  * CMD_SET_RATEADAPT_MODE.
  */
 struct mwl8k_cmd_set_rate_adapt_mode {
@@ -2037,6 +2069,12 @@ mwl8k_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
        if (cmd == NULL)
                return -ENOMEM;
 
+       /*
+        * Queues 0 (BE) and 1 (BK) are swapped in hardware for
+        * this call.
+        */
+       qnum ^= !(qnum >> 1);
+
        cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
        cmd->header.length = cpu_to_le16(sizeof(*cmd));
        cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
@@ -2523,11 +2561,24 @@ static int mwl8k_add_interface(struct ieee80211_hw *hw,
        if (conf->type != NL80211_IFTYPE_STATION)
                return -EINVAL;
 
+       /*
+        * Reject interface creation if sniffer mode is active, as
+        * STA operation is mutually exclusive with hardware sniffer
+        * mode.
+        */
+       if (priv->sniffer_enabled) {
+               printk(KERN_INFO "%s: unable to create STA "
+                      "interface due to sniffer mode being enabled\n",
+                      wiphy_name(hw->wiphy));
+               return -EINVAL;
+       }
+
        /* Clean out driver private area */
        mwl8k_vif = MWL8K_VIF(conf->vif);
        memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
 
-       /* Save the mac address */
+       /* Set and save the mac address */
+       mwl8k_set_mac_addr(hw, conf->mac_addr);
        memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
 
        /* Back pointer to parent config block */
@@ -2555,6 +2606,8 @@ static void mwl8k_remove_interface(struct ieee80211_hw *hw,
        if (priv->vif == NULL)
                return;
 
+       mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
+
        priv->vif = NULL;
 }
 
@@ -2690,13 +2743,56 @@ static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
        return (unsigned long)cmd;
 }
 
+static int
+mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
+                              unsigned int changed_flags,
+                              unsigned int *total_flags)
+{
+       struct mwl8k_priv *priv = hw->priv;
+
+       /*
+        * Hardware sniffer mode is mutually exclusive with STA
+        * operation, so refuse to enable sniffer mode if a STA
+        * interface is active.
+        */
+       if (priv->vif != NULL) {
+               if (net_ratelimit())
+                       printk(KERN_INFO "%s: not enabling sniffer "
+                              "mode because STA interface is active\n",
+                              wiphy_name(hw->wiphy));
+               return 0;
+       }
+
+       if (!priv->sniffer_enabled) {
+               if (mwl8k_enable_sniffer(hw, 1))
+                       return 0;
+               priv->sniffer_enabled = true;
+       }
+
+       *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
+                       FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
+                       FIF_OTHER_BSS;
+
+       return 1;
+}
+
 static void mwl8k_configure_filter(struct ieee80211_hw *hw,
                                   unsigned int changed_flags,
                                   unsigned int *total_flags,
                                   u64 multicast)
 {
        struct mwl8k_priv *priv = hw->priv;
-       struct mwl8k_cmd_pkt *cmd;
+       struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
+
+       /*
+        * Enable hardware sniffer mode if FIF_CONTROL or
+        * FIF_OTHER_BSS is requested.
+        */
+       if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
+           mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
+               kfree(cmd);
+               return;
+       }
 
        /* Clear unsupported feature flags */
        *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
@@ -2704,6 +2800,11 @@ static void mwl8k_configure_filter(struct ieee80211_hw *hw,
        if (mwl8k_fw_lock(hw))
                return;
 
+       if (priv->sniffer_enabled) {
+               mwl8k_enable_sniffer(hw, 0);
+               priv->sniffer_enabled = false;
+       }
+
        if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
                if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
                        /*
@@ -2729,8 +2830,6 @@ static void mwl8k_configure_filter(struct ieee80211_hw *hw,
                }
        }
 
-       cmd = (void *)(unsigned long)multicast;
-
        /*
         * If FIF_ALLMULTI is being requested, throw away the command
         * packet that ->prepare_multicast() built and replace it with
@@ -2889,6 +2988,7 @@ static int __devinit mwl8k_probe(struct pci_dev *pdev,
        priv = hw->priv;
        priv->hw = hw;
        priv->pdev = pdev;
+       priv->sniffer_enabled = false;
        priv->wmm_enabled = false;
        priv->pending_tx_pkts = 0;
 
@@ -3025,6 +3125,14 @@ static int __devinit mwl8k_probe(struct pci_dev *pdev,
                goto err_stop_firmware;
        }
 
+       /* Clear MAC address */
+       rc = mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
+       if (rc) {
+               printk(KERN_ERR "%s: Cannot clear MAC address\n",
+                      wiphy_name(hw->wiphy));
+               goto err_stop_firmware;
+       }
+
        /* Disable interrupts */
        iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
        free_irq(priv->pdev->irq, hw);