blob: 9e08fc601bb4031379ac8fd8b2f1e7c2dc5f92ea [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000013#include <linux/interrupt.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010014#include <linux/module.h>
15#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020016#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010017#include <linux/spinlock.h>
18#include <linux/list.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/completion.h>
22#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010024#include <net/mac80211.h>
25#include <linux/moduleparam.h>
26#include <linux/firmware.h>
27#include <linux/workqueue.h>
28
29#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
30#define MWL8K_NAME KBUILD_MODNAME
Yogesh Ashok Powar00e8e692011-12-20 11:39:29 +053031#define MWL8K_VERSION "0.13"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010032
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080033/* Module parameters */
Rusty Russelleb939922011-12-19 14:08:01 +000034static bool ap_mode_default;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080035module_param(ap_mode_default, bool, 0);
36MODULE_PARM_DESC(ap_mode_default,
37 "Set to 1 to make ap mode the default instead of sta mode");
38
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039/* Register definitions */
40#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020041#define MWL8K_MODE_STA 0x0000005a
42#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010043#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020044#define MWL8K_FWSTA_READY 0xf0f1f2f4
45#define MWL8K_FWAP_READY 0xf1f2f4a5
46#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010047#define MWL8K_HIU_SCRATCH 0x00000c40
48
49/* Host->device communications */
50#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
51#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
52#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
53#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
54#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020055#define MWL8K_H2A_INT_DUMMY (1 << 20)
56#define MWL8K_H2A_INT_RESET (1 << 15)
57#define MWL8K_H2A_INT_DOORBELL (1 << 1)
58#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010059
60/* Device->host communications */
61#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
62#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
63#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
64#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
65#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020066#define MWL8K_A2H_INT_DUMMY (1 << 20)
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070067#define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020068#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
69#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
70#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
71#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
72#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
73#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
74#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
75#define MWL8K_A2H_INT_RX_READY (1 << 1)
76#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010077
Pradeep Nemavat566875d2011-04-21 16:34:57 +053078/* HW micro second timer register
79 * located at offset 0xA600. This
80 * will be used to timestamp tx
81 * packets.
82 */
83
84#define MWL8K_HW_TIMER_REGISTER 0x0000a600
85
Lennert Buytenheka66098d2009-03-10 10:13:33 +010086#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
87 MWL8K_A2H_INT_CHNL_SWITCHED | \
88 MWL8K_A2H_INT_QUEUE_EMPTY | \
89 MWL8K_A2H_INT_RADAR_DETECT | \
90 MWL8K_A2H_INT_RADIO_ON | \
91 MWL8K_A2H_INT_RADIO_OFF | \
92 MWL8K_A2H_INT_MAC_EVENT | \
93 MWL8K_A2H_INT_OPC_DONE | \
94 MWL8K_A2H_INT_RX_READY | \
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070095 MWL8K_A2H_INT_TX_DONE | \
96 MWL8K_A2H_INT_BA_WATCHDOG)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010097
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098#define MWL8K_RX_QUEUES 1
Brian Cavagnoloe6007072011-03-17 11:58:44 -070099#define MWL8K_TX_WMM_QUEUES 4
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700100#define MWL8K_MAX_AMPDU_QUEUES 8
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700101#define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
102#define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +0530104/* txpriorities are mapped with hw queues.
105 * Each hw queue has a txpriority.
106 */
107#define TOTAL_HW_TX_QUEUES 8
108
109/* Each HW queue can have one AMPDU stream.
110 * But, because one of the hw queue is reserved,
111 * maximum AMPDU queues that can be created are
112 * one short of total tx queues.
113 */
114#define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
115
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200116struct rxd_ops {
117 int rxd_size;
118 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
119 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100120 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400121 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200122};
123
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200124struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200125 char *part_name;
126 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800127 char *fw_image_sta;
128 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100129 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800130 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200131};
132
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100133struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200134 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100135
136 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200137 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100138
139 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200140 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200142 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200143 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200144 struct {
145 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900146 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200147 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100148};
149
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100150struct mwl8k_tx_queue {
151 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200152 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
154 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200155 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100156
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200157 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200158 struct mwl8k_tx_desc *txd;
159 dma_addr_t txd_dma;
160 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100161};
162
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700163enum {
164 AMPDU_NO_STREAM,
165 AMPDU_STREAM_NEW,
166 AMPDU_STREAM_IN_PROGRESS,
167 AMPDU_STREAM_ACTIVE,
168};
169
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700170struct mwl8k_ampdu_stream {
171 struct ieee80211_sta *sta;
172 u8 tid;
173 u8 state;
174 u8 idx;
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700175};
176
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100179 struct pci_dev *pdev;
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +0530180 int irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100181
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200182 struct mwl8k_device_info *device_info;
183
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100184 void __iomem *sram;
185 void __iomem *regs;
186
187 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800188 const struct firmware *fw_helper;
189 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100190
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100191 /* hardware/firmware parameters */
192 bool ap_fw;
193 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100194 struct ieee80211_supported_band band_24;
195 struct ieee80211_channel channels_24[14];
196 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100197 struct ieee80211_supported_band band_50;
198 struct ieee80211_channel channels_50[4];
199 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100200 u32 ap_macids_supported;
201 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100202
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700203 /* Ampdu stream information */
204 u8 num_ampdu_queues;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700205 spinlock_t stream_lock;
206 struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700207 struct work_struct watchdog_ba_handle;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700208
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200209 /* firmware access */
210 struct mutex fw_mutex;
211 struct task_struct *fw_mutex_owner;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +0530212 struct task_struct *hw_restart_owner;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200213 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200214 struct completion *hostcmd_wait;
215
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216 /* lock held over TX and TX reap */
217 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200219 /* TX quiesce completion, protected by fw_mutex and tx_lock */
220 struct completion *tx_wait;
221
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100222 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100223 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100224 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100226 /* power management status cookie from firmware */
227 u32 *cookie;
228 dma_addr_t cookie_dma;
229
230 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100231 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200232 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100233
234 /*
235 * Running count of TX packets in flight, to avoid
236 * iterating over the transmit rings each time.
237 */
238 int pending_tx_pkts;
239
240 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700241 struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
242 u32 txq_offset[MWL8K_MAX_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100243
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200244 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200245 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200246 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200247 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100249 /* XXX need to convert this to handle multiple interfaces */
250 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200251 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100252 struct sk_buff *beacon_skb;
253
254 /*
255 * This FJ worker has to be global as it is scheduled from the
256 * RX handler. At this point we don't know which interface it
257 * belongs to until the list of bssids waiting to complete join
258 * is checked.
259 */
260 struct work_struct finalize_join_worker;
261
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100262 /* Tasklet to perform TX reclaim. */
263 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100264
265 /* Tasklet to perform RX. */
266 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400267
268 /* Most recently reported noise in dBm */
269 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800270
271 /*
272 * preserve the queue configurations so they can be restored if/when
273 * the firmware image is swapped.
274 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700275 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800276
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +0530277 /* To perform the task of reloading the firmware */
278 struct work_struct fw_reload;
279 bool hw_restart_in_progress;
280
Brian Cavagnolo99020472010-11-12 17:23:52 -0800281 /* async firmware loading state */
282 unsigned fw_state;
283 char *fw_pref;
284 char *fw_alt;
285 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100286};
287
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800288#define MAX_WEP_KEY_LEN 13
289#define NUM_WEP_KEYS 4
290
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100291/* Per interface specific private data */
292struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100293 struct list_head list;
294 struct ieee80211_vif *vif;
295
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100296 /* Firmware macid for this vif. */
297 int macid;
298
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100299 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100300 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800301
302 /* Saved WEP keys */
303 struct {
304 u8 enabled;
305 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
306 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800307
308 /* BSSID */
309 u8 bssid[ETH_ALEN];
310
311 /* A flag to indicate is HW crypto is enabled for this bssid */
312 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100313};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200314#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800315#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100316
Brian Cavagnolod0805c12011-04-12 11:06:28 -0700317struct tx_traffic_info {
318 u32 start_time;
319 u32 pkts;
320};
321
322#define MWL8K_MAX_TID 8
Lennert Buytenheka6804002010-01-04 21:55:42 +0100323struct mwl8k_sta {
324 /* Index into station database. Returned by UPDATE_STADB. */
325 u8 peer_id;
Nishant Sarmukadam170335432011-03-17 11:58:48 -0700326 u8 is_ampdu_allowed;
Brian Cavagnolod0805c12011-04-12 11:06:28 -0700327 struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
Lennert Buytenheka6804002010-01-04 21:55:42 +0100328};
329#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
330
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100331static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332 { .center_freq = 2412, .hw_value = 1, },
333 { .center_freq = 2417, .hw_value = 2, },
334 { .center_freq = 2422, .hw_value = 3, },
335 { .center_freq = 2427, .hw_value = 4, },
336 { .center_freq = 2432, .hw_value = 5, },
337 { .center_freq = 2437, .hw_value = 6, },
338 { .center_freq = 2442, .hw_value = 7, },
339 { .center_freq = 2447, .hw_value = 8, },
340 { .center_freq = 2452, .hw_value = 9, },
341 { .center_freq = 2457, .hw_value = 10, },
342 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100343 { .center_freq = 2467, .hw_value = 12, },
344 { .center_freq = 2472, .hw_value = 13, },
345 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100346};
347
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100348static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100349 { .bitrate = 10, .hw_value = 2, },
350 { .bitrate = 20, .hw_value = 4, },
351 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200352 { .bitrate = 110, .hw_value = 22, },
353 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100354 { .bitrate = 60, .hw_value = 12, },
355 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356 { .bitrate = 120, .hw_value = 24, },
357 { .bitrate = 180, .hw_value = 36, },
358 { .bitrate = 240, .hw_value = 48, },
359 { .bitrate = 360, .hw_value = 72, },
360 { .bitrate = 480, .hw_value = 96, },
361 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100362 { .bitrate = 720, .hw_value = 144, },
363};
364
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100365static const struct ieee80211_channel mwl8k_channels_50[] = {
366 { .center_freq = 5180, .hw_value = 36, },
367 { .center_freq = 5200, .hw_value = 40, },
368 { .center_freq = 5220, .hw_value = 44, },
369 { .center_freq = 5240, .hw_value = 48, },
370};
371
372static const struct ieee80211_rate mwl8k_rates_50[] = {
373 { .bitrate = 60, .hw_value = 12, },
374 { .bitrate = 90, .hw_value = 18, },
375 { .bitrate = 120, .hw_value = 24, },
376 { .bitrate = 180, .hw_value = 36, },
377 { .bitrate = 240, .hw_value = 48, },
378 { .bitrate = 360, .hw_value = 72, },
379 { .bitrate = 480, .hw_value = 96, },
380 { .bitrate = 540, .hw_value = 108, },
381 { .bitrate = 720, .hw_value = 144, },
382};
383
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100384/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800386#define MWL8K_CMD_SET 0x0001
387#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100388
389/* Firmware command codes */
390#define MWL8K_CMD_CODE_DNLD 0x0001
391#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200392#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100393#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
394#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200395#define MWL8K_CMD_RADIO_CONTROL 0x001c
396#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800397#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200398#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100399#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100400#define MWL8K_CMD_SET_PRE_SCAN 0x0107
401#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200402#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100403#define MWL8K_CMD_SET_AID 0x010d
404#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200405#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100406#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200407#define MWL8K_CMD_SET_SLOT 0x0114
408#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
409#define MWL8K_CMD_SET_WMM_MODE 0x0123
410#define MWL8K_CMD_MIMO_CONFIG 0x0125
411#define MWL8K_CMD_USE_FIXED_RATE 0x0126
412#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100413#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200414#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700415#define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +0530416#define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100417#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
418#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800419#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200420#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700421#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100422
John W. Linvilleb6037422010-07-20 13:55:00 -0400423static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100424{
John W. Linvilleb6037422010-07-20 13:55:00 -0400425 u16 command = le16_to_cpu(cmd);
426
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100427#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
428 snprintf(buf, bufsize, "%s", #x);\
429 return buf;\
430 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400431 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432 MWL8K_CMDNAME(CODE_DNLD);
433 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200434 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100435 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
436 MWL8K_CMDNAME(GET_STAT);
437 MWL8K_CMDNAME(RADIO_CONTROL);
438 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800439 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200440 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100441 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100442 MWL8K_CMDNAME(SET_PRE_SCAN);
443 MWL8K_CMDNAME(SET_POST_SCAN);
444 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100445 MWL8K_CMDNAME(SET_AID);
446 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200447 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100448 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200449 MWL8K_CMDNAME(SET_SLOT);
450 MWL8K_CMDNAME(SET_EDCA_PARAMS);
451 MWL8K_CMDNAME(SET_WMM_MODE);
452 MWL8K_CMDNAME(MIMO_CONFIG);
453 MWL8K_CMDNAME(USE_FIXED_RATE);
454 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200455 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200456 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100457 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100458 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800459 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200460 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700461 MWL8K_CMDNAME(BASTREAM);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700462 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100463 default:
464 snprintf(buf, bufsize, "0x%x", cmd);
465 }
466#undef MWL8K_CMDNAME
467
468 return buf;
469}
470
471/* Hardware and firmware reset */
472static void mwl8k_hw_reset(struct mwl8k_priv *priv)
473{
474 iowrite32(MWL8K_H2A_INT_RESET,
475 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
476 iowrite32(MWL8K_H2A_INT_RESET,
477 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
478 msleep(20);
479}
480
481/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800482static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100483{
484 if (*fw == NULL)
485 return;
486 release_firmware(*fw);
487 *fw = NULL;
488}
489
490static void mwl8k_release_firmware(struct mwl8k_priv *priv)
491{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100492 mwl8k_release_fw(&priv->fw_ucode);
493 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100494}
495
Brian Cavagnolo99020472010-11-12 17:23:52 -0800496/* states for asynchronous f/w loading */
497static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
498enum {
499 FW_STATE_INIT = 0,
500 FW_STATE_LOADING_PREF,
501 FW_STATE_LOADING_ALT,
502 FW_STATE_ERROR,
503};
504
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100505/* Request fw image */
506static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800507 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800508 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100509{
510 /* release current image */
511 if (*fw != NULL)
512 mwl8k_release_fw(fw);
513
Brian Cavagnolo99020472010-11-12 17:23:52 -0800514 if (nowait)
515 return request_firmware_nowait(THIS_MODULE, 1, fname,
516 &priv->pdev->dev, GFP_KERNEL,
517 priv, mwl8k_fw_state_machine);
518 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800519 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100520}
521
Brian Cavagnolo99020472010-11-12 17:23:52 -0800522static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
523 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100524{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200525 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100526 int rc;
527
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200528 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800529 if (nowait)
530 rc = mwl8k_request_fw(priv, di->helper_image,
531 &priv->fw_helper, true);
532 else
533 rc = mwl8k_request_fw(priv, di->helper_image,
534 &priv->fw_helper, false);
535 if (rc)
536 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
537 pci_name(priv->pdev), di->helper_image);
538
539 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200540 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100541 }
542
Brian Cavagnolo99020472010-11-12 17:23:52 -0800543 if (nowait) {
544 /*
545 * if we get here, no helper image is needed. Skip the
546 * FW_STATE_INIT state.
547 */
548 priv->fw_state = FW_STATE_LOADING_PREF;
549 rc = mwl8k_request_fw(priv, fw_image,
550 &priv->fw_ucode,
551 true);
552 } else
553 rc = mwl8k_request_fw(priv, fw_image,
554 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100555 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200556 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800557 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100558 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100559 return rc;
560 }
561
562 return 0;
563}
564
565struct mwl8k_cmd_pkt {
566 __le16 code;
567 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100568 __u8 seq_num;
569 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100570 __le16 result;
571 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000572} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100573
574/*
575 * Firmware loading.
576 */
577static int
578mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
579{
580 void __iomem *regs = priv->regs;
581 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100582 int loops;
583
584 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
585 if (pci_dma_mapping_error(priv->pdev, dma_addr))
586 return -ENOMEM;
587
588 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
589 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
590 iowrite32(MWL8K_H2A_INT_DOORBELL,
591 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
592 iowrite32(MWL8K_H2A_INT_DUMMY,
593 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
594
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100595 loops = 1000;
596 do {
597 u32 int_code;
598
599 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
600 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
601 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100602 break;
603 }
604
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200605 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100606 udelay(1);
607 } while (--loops);
608
609 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
610
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200611 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100612}
613
614static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
615 const u8 *data, size_t length)
616{
617 struct mwl8k_cmd_pkt *cmd;
618 int done;
619 int rc = 0;
620
621 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
622 if (cmd == NULL)
623 return -ENOMEM;
624
625 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
626 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100627 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100628 cmd->result = 0;
629
630 done = 0;
631 while (length) {
632 int block_size = length > 256 ? 256 : length;
633
634 memcpy(cmd->payload, data + done, block_size);
635 cmd->length = cpu_to_le16(block_size);
636
637 rc = mwl8k_send_fw_load_cmd(priv, cmd,
638 sizeof(*cmd) + block_size);
639 if (rc)
640 break;
641
642 done += block_size;
643 length -= block_size;
644 }
645
646 if (!rc) {
647 cmd->length = 0;
648 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
649 }
650
651 kfree(cmd);
652
653 return rc;
654}
655
656static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
657 const u8 *data, size_t length)
658{
659 unsigned char *buffer;
660 int may_continue, rc = 0;
661 u32 done, prev_block_size;
662
663 buffer = kmalloc(1024, GFP_KERNEL);
664 if (buffer == NULL)
665 return -ENOMEM;
666
667 done = 0;
668 prev_block_size = 0;
669 may_continue = 1000;
670 while (may_continue > 0) {
671 u32 block_size;
672
673 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
674 if (block_size & 1) {
675 block_size &= ~1;
676 may_continue--;
677 } else {
678 done += prev_block_size;
679 length -= prev_block_size;
680 }
681
682 if (block_size > 1024 || block_size > length) {
683 rc = -EOVERFLOW;
684 break;
685 }
686
687 if (length == 0) {
688 rc = 0;
689 break;
690 }
691
692 if (block_size == 0) {
693 rc = -EPROTO;
694 may_continue--;
695 udelay(1);
696 continue;
697 }
698
699 prev_block_size = block_size;
700 memcpy(buffer, data + done, block_size);
701
702 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
703 if (rc)
704 break;
705 }
706
707 if (!rc && length != 0)
708 rc = -EREMOTEIO;
709
710 kfree(buffer);
711
712 return rc;
713}
714
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200715static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100716{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200717 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800718 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200719 int rc;
720 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100721
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200722 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800723 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100724
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200725 if (helper == NULL) {
726 printk(KERN_ERR "%s: helper image needed but none "
727 "given\n", pci_name(priv->pdev));
728 return -EINVAL;
729 }
730
731 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100732 if (rc) {
733 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200734 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100735 return rc;
736 }
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +0530737 msleep(20);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100738
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200739 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100740 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200741 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100742 }
743
744 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200745 printk(KERN_ERR "%s: unable to load firmware image\n",
746 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100747 return rc;
748 }
749
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100750 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100751
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100752 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100753 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200754 u32 ready_code;
755
756 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
757 if (ready_code == MWL8K_FWAP_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000758 priv->ap_fw = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100759 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200760 } else if (ready_code == MWL8K_FWSTA_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000761 priv->ap_fw = false;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200762 break;
763 }
764
765 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100766 udelay(1);
767 } while (--loops);
768
769 return loops ? 0 : -ETIMEDOUT;
770}
771
772
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100773/* DMA header used by firmware and hardware. */
774struct mwl8k_dma_data {
775 __le16 fwlen;
776 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100777 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000778} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100779
780/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100781static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100782{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100783 struct mwl8k_dma_data *tr;
784 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100785
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100786 tr = (struct mwl8k_dma_data *)skb->data;
787 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
788
789 if (hdrlen != sizeof(tr->wh)) {
790 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
791 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
792 *((__le16 *)(tr->data - 2)) = qos;
793 } else {
794 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
795 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100796 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100797
798 if (hdrlen != sizeof(*tr))
799 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100800}
801
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530802#define REDUCED_TX_HEADROOM 8
803
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800804static void
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530805mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
806 int head_pad, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100807{
808 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100809 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800810 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100811 struct mwl8k_dma_data *tr;
812
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100813 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100814 * Add a firmware DMA header; the firmware requires that we
815 * present a 2-byte payload length followed by a 4-address
816 * header (without QoS field), followed (optionally) by any
817 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100818 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100819 wh = (struct ieee80211_hdr *)skb->data;
820
821 hdrlen = ieee80211_hdrlen(wh->frame_control);
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530822
823 /*
824 * Check if skb_resize is required because of
825 * tx_headroom adjustment.
826 */
827 if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
828 + REDUCED_TX_HEADROOM))) {
829 if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
830
831 wiphy_err(priv->hw->wiphy,
832 "Failed to reallocate TX buffer\n");
833 return;
834 }
835 skb->truesize += REDUCED_TX_HEADROOM;
836 }
837
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530838 reqd_hdrlen = sizeof(*tr) + head_pad;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800839
840 if (hdrlen != reqd_hdrlen)
841 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100842
843 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800844 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100845
846 tr = (struct mwl8k_dma_data *)skb->data;
847 if (wh != &tr->wh)
848 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100849 if (hdrlen != sizeof(tr->wh))
850 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100851
852 /*
853 * Firmware length is the length of the fully formed "802.11
854 * payload". That is, everything except for the 802.11 header.
855 * This includes all crypto material including the MIC.
856 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800857 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100858}
859
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530860static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
861 struct sk_buff *skb)
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800862{
863 struct ieee80211_hdr *wh;
864 struct ieee80211_tx_info *tx_info;
865 struct ieee80211_key_conf *key_conf;
866 int data_pad;
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530867 int head_pad = 0;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800868
869 wh = (struct ieee80211_hdr *)skb->data;
870
871 tx_info = IEEE80211_SKB_CB(skb);
872
873 key_conf = NULL;
874 if (ieee80211_is_data(wh->frame_control))
875 key_conf = tx_info->control.hw_key;
876
877 /*
878 * Make sure the packet header is in the DMA header format (4-address
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530879 * without QoS), and add head & tail padding when HW crypto is enabled.
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800880 *
881 * We have the following trailer padding requirements:
882 * - WEP: 4 trailer bytes (ICV)
883 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
884 * - CCMP: 8 trailer bytes (MIC)
885 */
886 data_pad = 0;
887 if (key_conf != NULL) {
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530888 head_pad = key_conf->iv_len;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800889 switch (key_conf->cipher) {
890 case WLAN_CIPHER_SUITE_WEP40:
891 case WLAN_CIPHER_SUITE_WEP104:
892 data_pad = 4;
893 break;
894 case WLAN_CIPHER_SUITE_TKIP:
895 data_pad = 12;
896 break;
897 case WLAN_CIPHER_SUITE_CCMP:
898 data_pad = 8;
899 break;
900 }
901 }
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530902 mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800903}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100904
905/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100906 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200907 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100908struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200909 __le16 pkt_len;
910 __u8 sq2;
911 __u8 rate;
912 __le32 pkt_phys_addr;
913 __le32 next_rxd_phys_addr;
914 __le16 qos_control;
915 __le16 htsig2;
916 __le32 hw_rssi_info;
917 __le32 hw_noise_floor_info;
918 __u8 noise_floor;
919 __u8 pad0[3];
920 __u8 rssi;
921 __u8 rx_status;
922 __u8 channel;
923 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000924} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200925
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100926#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
927#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
928#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100929
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100930#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200931
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800932/* 8366 AP rx_status bits */
933#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
934#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
935#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
936#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
937#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
938
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100939static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200940{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100941 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200942
943 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100944 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200945}
946
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100947static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200948{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100949 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200950
951 rxd->pkt_len = cpu_to_le16(len);
952 rxd->pkt_phys_addr = cpu_to_le32(addr);
953 wmb();
954 rxd->rx_ctrl = 0;
955}
956
957static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100958mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400959 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200960{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100961 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200962
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100963 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200964 return -1;
965 rmb();
966
967 memset(status, 0, sizeof(*status));
968
969 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400970 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200971
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100972 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200973 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100974 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100975 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100976 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200977 } else {
978 int i;
979
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100980 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
981 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200982 status->rate_idx = i;
983 break;
984 }
985 }
986 }
987
Lennert Buytenhek85478342010-01-12 13:48:56 +0100988 if (rxd->channel > 14) {
989 status->band = IEEE80211_BAND_5GHZ;
990 if (!(status->flag & RX_FLAG_HT))
991 status->rate_idx -= 5;
992 } else {
993 status->band = IEEE80211_BAND_2GHZ;
994 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900995 status->freq = ieee80211_channel_to_frequency(rxd->channel,
996 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200997
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100998 *qos = rxd->qos_control;
999
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001000 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1001 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1002 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
1003 status->flag |= RX_FLAG_MMIC_ERROR;
1004
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001005 return le16_to_cpu(rxd->pkt_len);
1006}
1007
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001008static struct rxd_ops rxd_8366_ap_ops = {
1009 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
1010 .rxd_init = mwl8k_rxd_8366_ap_init,
1011 .rxd_refill = mwl8k_rxd_8366_ap_refill,
1012 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001013};
1014
1015/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001016 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001017 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001018struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001019 __le16 pkt_len;
1020 __u8 link_quality;
1021 __u8 noise_level;
1022 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001023 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001024 __le16 qos_control;
1025 __le16 rate_info;
1026 __le32 pad0[4];
1027 __u8 rssi;
1028 __u8 channel;
1029 __le16 pad1;
1030 __u8 rx_ctrl;
1031 __u8 rx_status;
1032 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +00001033} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001034
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001035#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
1036#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
1037#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
1038#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
1039#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
1040#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001041
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001042#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001043#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
1044/* ICV=0 or MIC=1 */
1045#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
1046/* Key is uploaded only in failure case */
1047#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001048
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001049static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001050{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001051 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001052
1053 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001054 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001055}
1056
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001057static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001058{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001059 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001060
1061 rxd->pkt_len = cpu_to_le16(len);
1062 rxd->pkt_phys_addr = cpu_to_le32(addr);
1063 wmb();
1064 rxd->rx_ctrl = 0;
1065}
1066
1067static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001068mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -04001069 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001070{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001071 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001072 u16 rate_info;
1073
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001074 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001075 return -1;
1076 rmb();
1077
1078 rate_info = le16_to_cpu(rxd->rate_info);
1079
1080 memset(status, 0, sizeof(*status));
1081
1082 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001083 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001084 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1085 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001086
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001087 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001088 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001089 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001090 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001091 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001092 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001093 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001094 status->flag |= RX_FLAG_HT;
1095
Lennert Buytenhek85478342010-01-12 13:48:56 +01001096 if (rxd->channel > 14) {
1097 status->band = IEEE80211_BAND_5GHZ;
1098 if (!(status->flag & RX_FLAG_HT))
1099 status->rate_idx -= 5;
1100 } else {
1101 status->band = IEEE80211_BAND_2GHZ;
1102 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001103 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1104 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001105
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001106 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001107 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1108 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1109 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001110
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001111 return le16_to_cpu(rxd->pkt_len);
1112}
1113
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001114static struct rxd_ops rxd_sta_ops = {
1115 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1116 .rxd_init = mwl8k_rxd_sta_init,
1117 .rxd_refill = mwl8k_rxd_sta_refill,
1118 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001119};
1120
1121
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001122#define MWL8K_RX_DESCS 256
1123#define MWL8K_RX_MAXSZ 3800
1124
1125static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1126{
1127 struct mwl8k_priv *priv = hw->priv;
1128 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1129 int size;
1130 int i;
1131
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001132 rxq->rxd_count = 0;
1133 rxq->head = 0;
1134 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001135
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001136 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001137
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001138 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1139 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001140 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001141 return -ENOMEM;
1142 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001143 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001144
Shan Weib9ede5f2011-03-08 11:02:03 +08001145 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001146 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001147 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001148 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149 return -ENOMEM;
1150 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151
1152 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001153 int desc_size;
1154 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001156 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001157
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001158 desc_size = priv->rxd_ops->rxd_size;
1159 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001160
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001161 nexti = i + 1;
1162 if (nexti == MWL8K_RX_DESCS)
1163 nexti = 0;
1164 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1165
1166 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 }
1168
1169 return 0;
1170}
1171
1172static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1173{
1174 struct mwl8k_priv *priv = hw->priv;
1175 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1176 int refilled;
1177
1178 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001179 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001180 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001181 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001182 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001183 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001184
1185 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1186 if (skb == NULL)
1187 break;
1188
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001189 addr = pci_map_single(priv->pdev, skb->data,
1190 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001191
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001192 rxq->rxd_count++;
1193 rx = rxq->tail++;
1194 if (rxq->tail == MWL8K_RX_DESCS)
1195 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001196 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001197 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001198
1199 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1200 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201
1202 refilled++;
1203 }
1204
1205 return refilled;
1206}
1207
1208/* Must be called only when the card's reception is completely halted */
1209static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1210{
1211 struct mwl8k_priv *priv = hw->priv;
1212 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1213 int i;
1214
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001215 if (rxq->rxd == NULL)
1216 return;
1217
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001218 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001219 if (rxq->buf[i].skb != NULL) {
1220 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001221 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001222 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001223 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001224
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001225 kfree_skb(rxq->buf[i].skb);
1226 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001227 }
1228 }
1229
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001230 kfree(rxq->buf);
1231 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001232
1233 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001234 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001235 rxq->rxd, rxq->rxd_dma);
1236 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237}
1238
1239
1240/*
1241 * Scan a list of BSSIDs to process for finalize join.
1242 * Allows for extension to process multiple BSSIDs.
1243 */
1244static inline int
1245mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1246{
1247 return priv->capture_beacon &&
1248 ieee80211_is_beacon(wh->frame_control) &&
Joe Perches2e42e472012-05-09 17:17:46 +00001249 ether_addr_equal(wh->addr3, priv->capture_bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250}
1251
Lennert Buytenhek37797522009-10-22 20:19:45 +02001252static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1253 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001254{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001255 struct mwl8k_priv *priv = hw->priv;
1256
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001257 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001258 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001259
1260 /*
1261 * Use GFP_ATOMIC as rxq_process is called from
1262 * the primary interrupt handler, memory allocation call
1263 * must not sleep.
1264 */
1265 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1266 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001267 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001268}
1269
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001270static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1271 u8 *bssid)
1272{
1273 struct mwl8k_vif *mwl8k_vif;
1274
1275 list_for_each_entry(mwl8k_vif,
1276 vif_list, list) {
1277 if (memcmp(bssid, mwl8k_vif->bssid,
1278 ETH_ALEN) == 0)
1279 return mwl8k_vif;
1280 }
1281
1282 return NULL;
1283}
1284
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001285static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1286{
1287 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001288 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001289 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1290 int processed;
1291
1292 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001293 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001294 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001295 void *rxd;
1296 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001297 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001298 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001299 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001301 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001302 if (skb == NULL)
1303 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001304
1305 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1306
John W. Linville0d462bb2010-07-28 14:04:24 -04001307 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1308 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001309 if (pkt_len < 0)
1310 break;
1311
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001312 rxq->buf[rxq->head].skb = NULL;
1313
1314 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001315 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001316 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001317 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001318
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001319 rxq->head++;
1320 if (rxq->head == MWL8K_RX_DESCS)
1321 rxq->head = 0;
1322
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001323 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001324
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001325 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001326
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001327 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001328 * Check for a pending join operation. Save a
1329 * copy of the beacon and schedule a tasklet to
1330 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001331 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001332 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001333 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001335 if (ieee80211_has_protected(wh->frame_control)) {
1336
1337 /* Check if hw crypto has been enabled for
1338 * this bss. If yes, set the status flags
1339 * accordingly
1340 */
1341 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1342 wh->addr1);
1343
1344 if (mwl8k_vif != NULL &&
Joe Perches23677ce2012-02-09 11:17:23 +00001345 mwl8k_vif->is_hw_crypto_enabled) {
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001346 /*
1347 * When MMIC ERROR is encountered
1348 * by the firmware, payload is
1349 * dropped and only 32 bytes of
1350 * mwl8k Firmware header is sent
1351 * to the host.
1352 *
1353 * We need to add four bytes of
1354 * key information. In it
1355 * MAC80211 expects keyidx set to
1356 * 0 for triggering Counter
1357 * Measure of MMIC failure.
1358 */
1359 if (status.flag & RX_FLAG_MMIC_ERROR) {
1360 struct mwl8k_dma_data *tr;
1361 tr = (struct mwl8k_dma_data *)skb->data;
1362 memset((void *)&(tr->data), 0, 4);
1363 pkt_len += 4;
1364 }
1365
1366 if (!ieee80211_is_auth(wh->frame_control))
1367 status.flag |= RX_FLAG_IV_STRIPPED |
1368 RX_FLAG_DECRYPTED |
1369 RX_FLAG_MMIC_STRIPPED;
1370 }
1371 }
1372
1373 skb_put(skb, pkt_len);
1374 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001375 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1376 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001377
1378 processed++;
1379 }
1380
1381 return processed;
1382}
1383
1384
1385/*
1386 * Packet transmission.
1387 */
1388
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001389#define MWL8K_TXD_STATUS_OK 0x00000001
1390#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1391#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1392#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001393#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001394
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001395#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1396#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1397#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1398#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1399#define MWL8K_QOS_EOSP 0x0010
1400
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401struct mwl8k_tx_desc {
1402 __le32 status;
1403 __u8 data_rate;
1404 __u8 tx_priority;
1405 __le16 qos_control;
1406 __le32 pkt_phys_addr;
1407 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001408 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001409 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001410 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001411 __le16 rate_info;
1412 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001413 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001414} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001415
1416#define MWL8K_TX_DESCS 128
1417
1418static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1419{
1420 struct mwl8k_priv *priv = hw->priv;
1421 struct mwl8k_tx_queue *txq = priv->txq + index;
1422 int size;
1423 int i;
1424
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001425 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001426 txq->head = 0;
1427 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001428
1429 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1430
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001431 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1432 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001433 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001434 return -ENOMEM;
1435 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001436 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001437
Shan Weib9ede5f2011-03-08 11:02:03 +08001438 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001439 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001440 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001441 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001442 return -ENOMEM;
1443 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001444
1445 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1446 struct mwl8k_tx_desc *tx_desc;
1447 int nexti;
1448
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001449 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001450 nexti = (i + 1) % MWL8K_TX_DESCS;
1451
1452 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001453 tx_desc->next_txd_phys_addr =
1454 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001455 }
1456
1457 return 0;
1458}
1459
1460static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1461{
1462 iowrite32(MWL8K_H2A_INT_PPA_READY,
1463 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1464 iowrite32(MWL8K_H2A_INT_DUMMY,
1465 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1466 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1467}
1468
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001469static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001470{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001471 struct mwl8k_priv *priv = hw->priv;
1472 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001473
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001474 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001475 struct mwl8k_tx_queue *txq = priv->txq + i;
1476 int fw_owned = 0;
1477 int drv_owned = 0;
1478 int unused = 0;
1479 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001480
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001481 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001482 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1483 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001484
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001485 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001486 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001487 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001488 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001489 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001490
1491 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001492 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001493 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001494
Joe Perchesc96c31e2010-07-26 14:39:58 -07001495 wiphy_err(hw->wiphy,
1496 "txq[%d] len=%d head=%d tail=%d "
1497 "fw_owned=%d drv_owned=%d unused=%d\n",
1498 i,
1499 txq->len, txq->head, txq->tail,
1500 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001501 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001502}
1503
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001504/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001505 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001506 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001507#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001508
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001509static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001510{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001511 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001512 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001513 int retry;
1514 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001515
1516 might_sleep();
1517
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301518 /* Since fw restart is in progress, allow only the firmware
1519 * commands from the restart code and block the other
1520 * commands since they are going to fail in any case since
1521 * the firmware has crashed
1522 */
1523 if (priv->hw_restart_in_progress) {
1524 if (priv->hw_restart_owner == current)
1525 return 0;
1526 else
1527 return -EBUSY;
1528 }
1529
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001530 /*
1531 * The TX queues are stopped at this point, so this test
1532 * doesn't need to take ->tx_lock.
1533 */
1534 if (!priv->pending_tx_pkts)
1535 return 0;
1536
1537 retry = 0;
1538 rc = 0;
1539
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001540 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001541 priv->tx_wait = &tx_wait;
1542 while (!rc) {
1543 int oldcount;
1544 unsigned long timeout;
1545
1546 oldcount = priv->pending_tx_pkts;
1547
1548 spin_unlock_bh(&priv->tx_lock);
1549 timeout = wait_for_completion_timeout(&tx_wait,
1550 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1551 spin_lock_bh(&priv->tx_lock);
1552
1553 if (timeout) {
1554 WARN_ON(priv->pending_tx_pkts);
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301555 if (retry)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001556 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001557 break;
1558 }
1559
1560 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001561 wiphy_notice(hw->wiphy,
1562 "waiting for tx rings to drain (%d -> %d pkts)\n",
1563 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001564 retry = 1;
1565 continue;
1566 }
1567
1568 priv->tx_wait = NULL;
1569
Joe Perchesc96c31e2010-07-26 14:39:58 -07001570 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1571 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001572 mwl8k_dump_tx_rings(hw);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301573 priv->hw_restart_in_progress = true;
1574 ieee80211_queue_work(hw, &priv->fw_reload);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001575
1576 rc = -ETIMEDOUT;
1577 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 spin_unlock_bh(&priv->tx_lock);
1579
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001580 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001581}
1582
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001583#define MWL8K_TXD_SUCCESS(status) \
1584 ((status) & (MWL8K_TXD_STATUS_OK | \
1585 MWL8K_TXD_STATUS_OK_RETRY | \
1586 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001587
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001588static int mwl8k_tid_queue_mapping(u8 tid)
1589{
1590 BUG_ON(tid > 7);
1591
1592 switch (tid) {
1593 case 0:
1594 case 3:
1595 return IEEE80211_AC_BE;
1596 break;
1597 case 1:
1598 case 2:
1599 return IEEE80211_AC_BK;
1600 break;
1601 case 4:
1602 case 5:
1603 return IEEE80211_AC_VI;
1604 break;
1605 case 6:
1606 case 7:
1607 return IEEE80211_AC_VO;
1608 break;
1609 default:
1610 return -1;
1611 break;
1612 }
1613}
1614
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001615/* The firmware will fill in the rate information
1616 * for each packet that gets queued in the hardware
John W. Linville49adc5c2011-04-27 15:04:28 -04001617 * and these macros will interpret that info.
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001618 */
1619
John W. Linville49adc5c2011-04-27 15:04:28 -04001620#define RI_FORMAT(a) (a & 0x0001)
1621#define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001622
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001623static int
1624mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001625{
1626 struct mwl8k_priv *priv = hw->priv;
1627 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001628 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001629
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001630 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001631 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001632 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001633 struct mwl8k_tx_desc *tx_desc;
1634 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001635 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001636 struct sk_buff *skb;
1637 struct ieee80211_tx_info *info;
1638 u32 status;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001639 struct ieee80211_sta *sta;
1640 struct mwl8k_sta *sta_info = NULL;
1641 u16 rate_info;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001642 struct ieee80211_hdr *wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001643
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001644 tx = txq->head;
1645 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001646
1647 status = le32_to_cpu(tx_desc->status);
1648
1649 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1650 if (!force)
1651 break;
1652 tx_desc->status &=
1653 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1654 }
1655
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001656 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001657 BUG_ON(txq->len == 0);
1658 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001659 priv->pending_tx_pkts--;
1660
1661 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001662 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001663 skb = txq->skb[tx];
1664 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001665
1666 BUG_ON(skb == NULL);
1667 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1668
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001669 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001670
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001671 wh = (struct ieee80211_hdr *) skb->data;
1672
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001673 /* Mark descriptor as unused */
1674 tx_desc->pkt_phys_addr = 0;
1675 tx_desc->pkt_len = 0;
1676
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001677 info = IEEE80211_SKB_CB(skb);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001678 if (ieee80211_is_data(wh->frame_control)) {
Thomas Huehn89e11802012-07-11 13:21:41 +02001679 rcu_read_lock();
1680 sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1681 wh->addr2);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001682 if (sta) {
1683 sta_info = MWL8K_STA(sta);
1684 BUG_ON(sta_info == NULL);
1685 rate_info = le16_to_cpu(tx_desc->rate_info);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001686 /* If rate is < 6.5 Mpbs for an ht station
1687 * do not form an ampdu. If the station is a
1688 * legacy station (format = 0), do not form an
1689 * ampdu
1690 */
John W. Linville49adc5c2011-04-27 15:04:28 -04001691 if (RI_RATE_ID_MCS(rate_info) < 1 ||
1692 RI_FORMAT(rate_info) == 0) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001693 sta_info->is_ampdu_allowed = false;
1694 } else {
1695 sta_info->is_ampdu_allowed = true;
1696 }
1697 }
Thomas Huehn89e11802012-07-11 13:21:41 +02001698 rcu_read_unlock();
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001699 }
1700
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001701 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001702
1703 /* Rate control is happening in the firmware.
1704 * Ensure no tx rate is being reported.
1705 */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301706 info->status.rates[0].idx = -1;
1707 info->status.rates[0].count = 1;
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001708
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001709 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001710 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001711
1712 ieee80211_tx_status_irqsafe(hw, skb);
1713
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001714 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001715 }
1716
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001717 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001718}
1719
1720/* must be called only when the card's transmit is completely halted */
1721static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1722{
1723 struct mwl8k_priv *priv = hw->priv;
1724 struct mwl8k_tx_queue *txq = priv->txq + index;
1725
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001726 if (txq->txd == NULL)
1727 return;
1728
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001729 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001730
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001731 kfree(txq->skb);
1732 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001733
1734 pci_free_consistent(priv->pdev,
1735 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001736 txq->txd, txq->txd_dma);
1737 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001738}
1739
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001740/* caller must hold priv->stream_lock when calling the stream functions */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301741static struct mwl8k_ampdu_stream *
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001742mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1743{
1744 struct mwl8k_ampdu_stream *stream;
1745 struct mwl8k_priv *priv = hw->priv;
1746 int i;
1747
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301748 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001749 stream = &priv->ampdu[i];
1750 if (stream->state == AMPDU_NO_STREAM) {
1751 stream->sta = sta;
1752 stream->state = AMPDU_STREAM_NEW;
1753 stream->tid = tid;
1754 stream->idx = i;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001755 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1756 sta->addr, tid);
1757 return stream;
1758 }
1759 }
1760 return NULL;
1761}
1762
1763static int
1764mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1765{
1766 int ret;
1767
1768 /* if the stream has already been started, don't start it again */
1769 if (stream->state != AMPDU_STREAM_NEW)
1770 return 0;
1771 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1772 if (ret)
1773 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1774 "%d\n", stream->sta->addr, stream->tid, ret);
1775 else
1776 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1777 stream->sta->addr, stream->tid);
1778 return ret;
1779}
1780
1781static void
1782mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1783{
1784 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1785 stream->tid);
1786 memset(stream, 0, sizeof(*stream));
1787}
1788
1789static struct mwl8k_ampdu_stream *
1790mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1791{
1792 struct mwl8k_priv *priv = hw->priv;
1793 int i;
1794
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301795 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001796 struct mwl8k_ampdu_stream *stream;
1797 stream = &priv->ampdu[i];
1798 if (stream->state == AMPDU_NO_STREAM)
1799 continue;
1800 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1801 stream->tid == tid)
1802 return stream;
1803 }
1804 return NULL;
1805}
1806
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001807#define MWL8K_AMPDU_PACKET_THRESHOLD 64
1808static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1809{
1810 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1811 struct tx_traffic_info *tx_stats;
1812
1813 BUG_ON(tid >= MWL8K_MAX_TID);
1814 tx_stats = &sta_info->tx_stats[tid];
1815
1816 return sta_info->is_ampdu_allowed &&
1817 tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1818}
1819
1820static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1821{
1822 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1823 struct tx_traffic_info *tx_stats;
1824
1825 BUG_ON(tid >= MWL8K_MAX_TID);
1826 tx_stats = &sta_info->tx_stats[tid];
1827
1828 if (tx_stats->start_time == 0)
1829 tx_stats->start_time = jiffies;
1830
1831 /* reset the packet count after each second elapses. If the number of
1832 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1833 * an ampdu stream to be started.
1834 */
1835 if (jiffies - tx_stats->start_time > HZ) {
1836 tx_stats->pkts = 0;
1837 tx_stats->start_time = 0;
1838 } else
1839 tx_stats->pkts++;
1840}
1841
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301842/* The hardware ampdu queues start from 5.
1843 * txpriorities for ampdu queues are
1844 * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1845 * and queue 3 is lowest (queue 4 is reserved)
1846 */
1847#define BA_QUEUE 5
1848
Johannes Berg7bb45682011-02-24 14:42:06 +01001849static void
Thomas Huehn36323f82012-07-23 21:33:42 +02001850mwl8k_txq_xmit(struct ieee80211_hw *hw,
1851 int index,
1852 struct ieee80211_sta *sta,
1853 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001854{
1855 struct mwl8k_priv *priv = hw->priv;
1856 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001857 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001858 struct ieee80211_hdr *wh;
1859 struct mwl8k_tx_queue *txq;
1860 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001862 u32 txstatus;
1863 u8 txdatarate;
1864 u16 qos;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001865 int txpriority;
1866 u8 tid = 0;
1867 struct mwl8k_ampdu_stream *stream = NULL;
1868 bool start_ba_session = false;
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301869 bool mgmtframe = false;
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001870 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301871 bool eapol_frame = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001872
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001873 wh = (struct ieee80211_hdr *)skb->data;
1874 if (ieee80211_is_data_qos(wh->frame_control))
1875 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1876 else
1877 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001878
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301879 if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1880 eapol_frame = true;
1881
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301882 if (ieee80211_is_mgmt(wh->frame_control))
1883 mgmtframe = true;
1884
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001885 if (priv->ap_fw)
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05301886 mwl8k_encapsulate_tx_frame(priv, skb);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001887 else
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +05301888 mwl8k_add_dma_header(priv, skb, 0, 0);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001889
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001890 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001891
1892 tx_info = IEEE80211_SKB_CB(skb);
1893 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001894
1895 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001896 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001897 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1898 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001899 }
1900
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001901 /* Setup firmware control bit fields for each frame type. */
1902 txstatus = 0;
1903 txdatarate = 0;
1904 if (ieee80211_is_mgmt(wh->frame_control) ||
1905 ieee80211_is_ctl(wh->frame_control)) {
1906 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001907 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001908 } else if (ieee80211_is_data(wh->frame_control)) {
1909 txdatarate = 1;
1910 if (is_multicast_ether_addr(wh->addr1))
1911 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1912
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001913 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001914 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001915 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001916 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001917 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001918 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001920 /* Queue ADDBA request in the respective data queue. While setting up
1921 * the ampdu stream, mac80211 queues further packets for that
1922 * particular ra/tid pair. However, packets piled up in the hardware
1923 * for that ra/tid pair will still go out. ADDBA request and the
1924 * related data packets going out from different queues asynchronously
1925 * will cause a shift in the receiver window which might result in
1926 * ampdu packets getting dropped at the receiver after the stream has
1927 * been setup.
1928 */
1929 if (unlikely(ieee80211_is_action(wh->frame_control) &&
1930 mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1931 mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1932 priv->ap_fw)) {
1933 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1934 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1935 index = mwl8k_tid_queue_mapping(tid);
1936 }
1937
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001938 txpriority = index;
1939
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301940 if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
1941 ieee80211_is_data_qos(wh->frame_control)) {
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001942 tid = qos & 0xf;
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001943 mwl8k_tx_count_packet(sta, tid);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001944 spin_lock(&priv->stream_lock);
1945 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1946 if (stream != NULL) {
1947 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05301948 WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301949 txpriority = (BA_QUEUE + stream->idx) %
1950 TOTAL_HW_TX_QUEUES;
1951 if (stream->idx <= 1)
1952 index = stream->idx +
1953 MWL8K_TX_WMM_QUEUES;
1954
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001955 } else if (stream->state == AMPDU_STREAM_NEW) {
1956 /* We get here if the driver sends us packets
1957 * after we've initiated a stream, but before
1958 * our ampdu_action routine has been called
1959 * with IEEE80211_AMPDU_TX_START to get the SSN
1960 * for the ADDBA request. So this packet can
1961 * go out with no risk of sequence number
1962 * mismatch. No special handling is required.
1963 */
1964 } else {
1965 /* Drop packets that would go out after the
1966 * ADDBA request was sent but before the ADDBA
1967 * response is received. If we don't do this,
1968 * the recipient would probably receive it
1969 * after the ADDBA request with SSN 0. This
1970 * will cause the recipient's BA receive window
1971 * to shift, which would cause the subsequent
1972 * packets in the BA stream to be discarded.
1973 * mac80211 queues our packets for us in this
1974 * case, so this is really just a safety check.
1975 */
1976 wiphy_warn(hw->wiphy,
1977 "Cannot send packet while ADDBA "
1978 "dialog is underway.\n");
1979 spin_unlock(&priv->stream_lock);
1980 dev_kfree_skb(skb);
1981 return;
1982 }
1983 } else {
1984 /* Defer calling mwl8k_start_stream so that the current
1985 * skb can go out before the ADDBA request. This
1986 * prevents sequence number mismatch at the recepient
1987 * as described above.
1988 */
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001989 if (mwl8k_ampdu_allowed(sta, tid)) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001990 stream = mwl8k_add_stream(hw, sta, tid);
1991 if (stream != NULL)
1992 start_ba_session = true;
1993 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001994 }
1995 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05301996 } else {
1997 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
1998 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001999 }
2000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002001 dma = pci_map_single(priv->pdev, skb->data,
2002 skb->len, PCI_DMA_TODEVICE);
2003
2004 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07002005 wiphy_debug(hw->wiphy,
2006 "failed to dma map skb, dropping TX frame.\n");
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002007 if (start_ba_session) {
2008 spin_lock(&priv->stream_lock);
2009 mwl8k_remove_stream(hw, stream);
2010 spin_unlock(&priv->stream_lock);
2011 }
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002012 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01002013 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002014 }
2015
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002016 spin_lock_bh(&priv->tx_lock);
2017
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002018 txq = priv->txq + index;
2019
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302020 /* Mgmt frames that go out frequently are probe
2021 * responses. Other mgmt frames got out relatively
2022 * infrequently. Hence reserve 2 buffers so that
2023 * other mgmt frames do not get dropped due to an
2024 * already queued probe response in one of the
2025 * reserved buffers.
2026 */
2027
2028 if (txq->len >= MWL8K_TX_DESCS - 2) {
Joe Perches23677ce2012-02-09 11:17:23 +00002029 if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302030 if (start_ba_session) {
2031 spin_lock(&priv->stream_lock);
2032 mwl8k_remove_stream(hw, stream);
2033 spin_unlock(&priv->stream_lock);
2034 }
2035 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadamff7aa962012-11-06 19:22:48 +05302036 pci_unmap_single(priv->pdev, dma, skb->len,
2037 PCI_DMA_TODEVICE);
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302038 dev_kfree_skb(skb);
2039 return;
Pradeep Nemavat3a7dbc32011-04-21 16:34:56 +05302040 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002041 }
2042
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002043 BUG_ON(txq->skb[txq->tail] != NULL);
2044 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002045
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002046 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002047 tx->data_rate = txdatarate;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002048 tx->tx_priority = txpriority;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002049 tx->qos_control = cpu_to_le16(qos);
2050 tx->pkt_phys_addr = cpu_to_le32(dma);
2051 tx->pkt_len = cpu_to_le16(skb->len);
2052 tx->rate_info = 0;
Thomas Huehn36323f82012-07-23 21:33:42 +02002053 if (!priv->ap_fw && sta != NULL)
2054 tx->peer_id = MWL8K_STA(sta)->peer_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002055 else
2056 tx->peer_id = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302057
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05302058 if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302059 tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2060 MWL8K_HW_TIMER_REGISTER));
Nishant Sarmukadamb8d9e572012-11-06 19:23:15 +05302061 else
2062 tx->timestamp = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302063
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002064 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002065 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2066
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02002067 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002068 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002069
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002070 txq->tail++;
2071 if (txq->tail == MWL8K_TX_DESCS)
2072 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002073
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002074 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002075
2076 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002077
2078 /* Initiate the ampdu session here */
2079 if (start_ba_session) {
2080 spin_lock(&priv->stream_lock);
2081 if (mwl8k_start_stream(hw, stream))
2082 mwl8k_remove_stream(hw, stream);
2083 spin_unlock(&priv->stream_lock);
2084 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002085}
2086
2087
2088/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002089 * Firmware access.
2090 *
2091 * We have the following requirements for issuing firmware commands:
2092 * - Some commands require that the packet transmit path is idle when
2093 * the command is issued. (For simplicity, we'll just quiesce the
2094 * transmit path for every command.)
2095 * - There are certain sequences of commands that need to be issued to
2096 * the hardware sequentially, with no other intervening commands.
2097 *
2098 * This leads to an implementation of a "firmware lock" as a mutex that
2099 * can be taken recursively, and which is taken by both the low-level
2100 * command submission function (mwl8k_post_cmd) as well as any users of
2101 * that function that require issuing of an atomic sequence of commands,
2102 * and quiesces the transmit path whenever it's taken.
2103 */
2104static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2105{
2106 struct mwl8k_priv *priv = hw->priv;
2107
2108 if (priv->fw_mutex_owner != current) {
2109 int rc;
2110
2111 mutex_lock(&priv->fw_mutex);
2112 ieee80211_stop_queues(hw);
2113
2114 rc = mwl8k_tx_wait_empty(hw);
2115 if (rc) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302116 if (!priv->hw_restart_in_progress)
2117 ieee80211_wake_queues(hw);
2118
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002119 mutex_unlock(&priv->fw_mutex);
2120
2121 return rc;
2122 }
2123
2124 priv->fw_mutex_owner = current;
2125 }
2126
2127 priv->fw_mutex_depth++;
2128
2129 return 0;
2130}
2131
2132static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2133{
2134 struct mwl8k_priv *priv = hw->priv;
2135
2136 if (!--priv->fw_mutex_depth) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302137 if (!priv->hw_restart_in_progress)
2138 ieee80211_wake_queues(hw);
2139
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002140 priv->fw_mutex_owner = NULL;
2141 mutex_unlock(&priv->fw_mutex);
2142 }
2143}
2144
2145
2146/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002147 * Command processing.
2148 */
2149
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002150/* Timeout firmware commands after 10s */
2151#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002152
2153static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
2154{
2155 DECLARE_COMPLETION_ONSTACK(cmd_wait);
2156 struct mwl8k_priv *priv = hw->priv;
2157 void __iomem *regs = priv->regs;
2158 dma_addr_t dma_addr;
2159 unsigned int dma_size;
2160 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002161 unsigned long timeout = 0;
2162 u8 buf[32];
2163
John W. Linvilleb6037422010-07-20 13:55:00 -04002164 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002165 dma_size = le16_to_cpu(cmd->length);
2166 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
2167 PCI_DMA_BIDIRECTIONAL);
2168 if (pci_dma_mapping_error(priv->pdev, dma_addr))
2169 return -ENOMEM;
2170
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002171 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02002172 if (rc) {
2173 pci_unmap_single(priv->pdev, dma_addr, dma_size,
2174 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002175 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02002176 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002177
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002178 priv->hostcmd_wait = &cmd_wait;
2179 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2180 iowrite32(MWL8K_H2A_INT_DOORBELL,
2181 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2182 iowrite32(MWL8K_H2A_INT_DUMMY,
2183 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002184
2185 timeout = wait_for_completion_timeout(&cmd_wait,
2186 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2187
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002188 priv->hostcmd_wait = NULL;
2189
2190 mwl8k_fw_unlock(hw);
2191
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02002192 pci_unmap_single(priv->pdev, dma_addr, dma_size,
2193 PCI_DMA_BIDIRECTIONAL);
2194
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002195 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07002196 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002197 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2198 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002199 rc = -ETIMEDOUT;
2200 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002201 int ms;
2202
2203 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2204
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002205 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002206 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07002207 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002208 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2209 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002210 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07002211 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002212 mwl8k_cmd_name(cmd->code,
2213 buf, sizeof(buf)),
2214 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002215 }
2216
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002217 return rc;
2218}
2219
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01002220static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2221 struct ieee80211_vif *vif,
2222 struct mwl8k_cmd_pkt *cmd)
2223{
2224 if (vif != NULL)
2225 cmd->macid = MWL8K_VIF(vif)->macid;
2226 return mwl8k_post_cmd(hw, cmd);
2227}
2228
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002229/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002230 * Setup code shared between STA and AP firmware images.
2231 */
2232static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2233{
2234 struct mwl8k_priv *priv = hw->priv;
2235
2236 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2237 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2238
2239 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2240 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2241
2242 priv->band_24.band = IEEE80211_BAND_2GHZ;
2243 priv->band_24.channels = priv->channels_24;
2244 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2245 priv->band_24.bitrates = priv->rates_24;
2246 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2247
2248 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2249}
2250
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01002251static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2252{
2253 struct mwl8k_priv *priv = hw->priv;
2254
2255 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2256 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2257
2258 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2259 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2260
2261 priv->band_50.band = IEEE80211_BAND_5GHZ;
2262 priv->band_50.channels = priv->channels_50;
2263 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2264 priv->band_50.bitrates = priv->rates_50;
2265 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2266
2267 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2268}
2269
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002270/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002271 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002272 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002273struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002274 struct mwl8k_cmd_pkt header;
2275 __u8 hw_rev;
2276 __u8 host_interface;
2277 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002278 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002279 __le16 region_code;
2280 __le32 fw_rev;
2281 __le32 ps_cookie;
2282 __le32 caps;
2283 __u8 mcs_bitmap[16];
2284 __le32 rx_queue_ptr;
2285 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002286 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002287 __le32 caps2;
2288 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002289 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002290} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002291
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002292#define MWL8K_CAP_MAX_AMSDU 0x20000000
2293#define MWL8K_CAP_GREENFIELD 0x08000000
2294#define MWL8K_CAP_AMPDU 0x04000000
2295#define MWL8K_CAP_RX_STBC 0x01000000
2296#define MWL8K_CAP_TX_STBC 0x00800000
2297#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2298#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2299#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2300#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2301#define MWL8K_CAP_DELAY_BA 0x00003000
2302#define MWL8K_CAP_MIMO 0x00000200
2303#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01002304#define MWL8K_CAP_BAND_MASK 0x00000007
2305#define MWL8K_CAP_5GHZ 0x00000004
2306#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002307
Lennert Buytenhek06953232010-01-12 13:49:41 +01002308static void
2309mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2310 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002311{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002312 int rx_streams;
2313 int tx_streams;
2314
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002315 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002316
2317 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002318 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002319 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002320 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002321 if (cap & MWL8K_CAP_AMPDU) {
2322 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002323 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2324 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002325 }
2326 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002327 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002328 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002329 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002330 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002331 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002332 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002333 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002334 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002335 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002336 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002337 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002338
2339 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2340 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2341
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002342 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002343 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002344 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002345 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002346 band->ht_cap.mcs.rx_mask[2] = 0xff;
2347 band->ht_cap.mcs.rx_mask[4] = 0x01;
2348 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002349
2350 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002351 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2352 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002353 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2354 }
2355}
2356
Lennert Buytenhek06953232010-01-12 13:49:41 +01002357static void
2358mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2359{
2360 struct mwl8k_priv *priv = hw->priv;
2361
2362 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2363 mwl8k_setup_2ghz_band(hw);
2364 if (caps & MWL8K_CAP_MIMO)
2365 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2366 }
2367
2368 if (caps & MWL8K_CAP_5GHZ) {
2369 mwl8k_setup_5ghz_band(hw);
2370 if (caps & MWL8K_CAP_MIMO)
2371 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2372 }
2373}
2374
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002375static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002376{
2377 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002378 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002379 int rc;
2380 int i;
2381
2382 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2383 if (cmd == NULL)
2384 return -ENOMEM;
2385
2386 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2387 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2388
2389 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2390 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002391 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002392 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2393 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002394 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002395 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002396 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002397
2398 rc = mwl8k_post_cmd(hw, &cmd->header);
2399
2400 if (!rc) {
2401 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2402 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002403 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002404 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002405 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002406 priv->ap_macids_supported = 0x00000000;
2407 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002408 }
2409
2410 kfree(cmd);
2411 return rc;
2412}
2413
2414/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002415 * CMD_GET_HW_SPEC (AP version).
2416 */
2417struct mwl8k_cmd_get_hw_spec_ap {
2418 struct mwl8k_cmd_pkt header;
2419 __u8 hw_rev;
2420 __u8 host_interface;
2421 __le16 num_wcb;
2422 __le16 num_mcaddrs;
2423 __u8 perm_addr[ETH_ALEN];
2424 __le16 region_code;
2425 __le16 num_antenna;
2426 __le32 fw_rev;
2427 __le32 wcbbase0;
2428 __le32 rxwrptr;
2429 __le32 rxrdptr;
2430 __le32 ps_cookie;
2431 __le32 wcbbase1;
2432 __le32 wcbbase2;
2433 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002434 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002435 __le32 caps;
2436 __le32 num_of_ampdu_queues;
2437 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002438} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002439
2440static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2441{
2442 struct mwl8k_priv *priv = hw->priv;
2443 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002444 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002445 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002446
2447 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2448 if (cmd == NULL)
2449 return -ENOMEM;
2450
2451 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2452 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2453
2454 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2455 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2456
2457 rc = mwl8k_post_cmd(hw, &cmd->header);
2458
2459 if (!rc) {
2460 int off;
2461
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002462 api_version = le32_to_cpu(cmd->fw_api_version);
2463 if (priv->device_info->fw_api_ap != api_version) {
2464 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2465 " Expected %d got %d.\n", MWL8K_NAME,
2466 priv->device_info->part_name,
2467 priv->device_info->fw_api_ap,
2468 api_version);
2469 rc = -EINVAL;
2470 goto done;
2471 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002472 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2473 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2474 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2475 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002476 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002477 priv->ap_macids_supported = 0x000000ff;
2478 priv->sta_macids_supported = 0x00000000;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002479 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2480 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2481 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2482 " but we only support %d.\n",
2483 priv->num_ampdu_queues,
2484 MWL8K_MAX_AMPDU_QUEUES);
2485 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2486 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002487 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002488 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002489
2490 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002491 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002492
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002493 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2494 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2495 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2496 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002497
2498 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002499 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002500 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002501 }
2502
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002503done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002504 kfree(cmd);
2505 return rc;
2506}
2507
2508/*
2509 * CMD_SET_HW_SPEC.
2510 */
2511struct mwl8k_cmd_set_hw_spec {
2512 struct mwl8k_cmd_pkt header;
2513 __u8 hw_rev;
2514 __u8 host_interface;
2515 __le16 num_mcaddrs;
2516 __u8 perm_addr[ETH_ALEN];
2517 __le16 region_code;
2518 __le32 fw_rev;
2519 __le32 ps_cookie;
2520 __le32 caps;
2521 __le32 rx_queue_ptr;
2522 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002523 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002524 __le32 flags;
2525 __le32 num_tx_desc_per_queue;
2526 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002527} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002528
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002529/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2530 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2531 * the packets that are queued for more than 500ms, will be dropped in the
2532 * hardware. This helps minimizing the issues caused due to head-of-line
2533 * blocking where a slow client can hog the bandwidth and affect traffic to a
2534 * faster client.
2535 */
2536#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302537#define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002538#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2539#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2540#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002541
2542static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2543{
2544 struct mwl8k_priv *priv = hw->priv;
2545 struct mwl8k_cmd_set_hw_spec *cmd;
2546 int rc;
2547 int i;
2548
2549 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2550 if (cmd == NULL)
2551 return -ENOMEM;
2552
2553 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2554 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2555
2556 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2557 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002558 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002559
2560 /*
2561 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2562 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2563 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2564 * priority is interpreted the right way in firmware.
2565 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002566 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2567 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002568 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2569 }
2570
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002571 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2572 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
Nishant Sarmukadam31d291a2011-04-21 16:34:59 +05302573 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302574 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2575 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002576 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2577 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2578
2579 rc = mwl8k_post_cmd(hw, &cmd->header);
2580 kfree(cmd);
2581
2582 return rc;
2583}
2584
2585/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002586 * CMD_MAC_MULTICAST_ADR.
2587 */
2588struct mwl8k_cmd_mac_multicast_adr {
2589 struct mwl8k_cmd_pkt header;
2590 __le16 action;
2591 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002592 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002593};
2594
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002595#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2596#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2597#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2598#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002599
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002600static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002601__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002602 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002603{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002604 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002605 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002606 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002607 int mc_count = 0;
2608
2609 if (mc_list)
2610 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002611
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002612 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002613 allmulti = 1;
2614 mc_count = 0;
2615 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002616
2617 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2618
2619 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002620 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002621 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002622
2623 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2624 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002625 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2626 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002627
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002628 if (allmulti) {
2629 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2630 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002631 struct netdev_hw_addr *ha;
2632 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002633
2634 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2635 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002636 netdev_hw_addr_list_for_each(ha, mc_list) {
2637 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002638 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002639 }
2640
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002641 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002642}
2643
2644/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002645 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002646 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002647struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002648 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002649 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002650} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002651
2652#define MWL8K_STAT_ACK_FAILURE 9
2653#define MWL8K_STAT_RTS_FAILURE 12
2654#define MWL8K_STAT_FCS_ERROR 24
2655#define MWL8K_STAT_RTS_SUCCESS 11
2656
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002657static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2658 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002659{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002660 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002661 int rc;
2662
2663 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2664 if (cmd == NULL)
2665 return -ENOMEM;
2666
2667 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2668 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002669
2670 rc = mwl8k_post_cmd(hw, &cmd->header);
2671 if (!rc) {
2672 stats->dot11ACKFailureCount =
2673 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2674 stats->dot11RTSFailureCount =
2675 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2676 stats->dot11FCSErrorCount =
2677 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2678 stats->dot11RTSSuccessCount =
2679 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2680 }
2681 kfree(cmd);
2682
2683 return rc;
2684}
2685
2686/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002687 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002688 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002689struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002690 struct mwl8k_cmd_pkt header;
2691 __le16 action;
2692 __le16 control;
2693 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002694} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002695
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002696static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002697mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002698{
2699 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002700 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002701 int rc;
2702
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002703 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002704 return 0;
2705
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002706 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2707 if (cmd == NULL)
2708 return -ENOMEM;
2709
2710 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2711 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2712 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002713 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002714 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2715
2716 rc = mwl8k_post_cmd(hw, &cmd->header);
2717 kfree(cmd);
2718
2719 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002720 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002721
2722 return rc;
2723}
2724
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002725static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002726{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002727 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002728}
2729
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002730static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002731{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002732 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002733}
2734
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002735static int
2736mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2737{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002738 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002739
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002740 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002741
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002742 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002743}
2744
2745/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002746 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002747 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002748#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002749
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002750struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002751 struct mwl8k_cmd_pkt header;
2752 __le16 action;
2753 __le16 support_level;
2754 __le16 current_level;
2755 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002756 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002757} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002758
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002759static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002760{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002761 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002762 int rc;
2763
2764 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2765 if (cmd == NULL)
2766 return -ENOMEM;
2767
2768 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2769 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2770 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2771 cmd->support_level = cpu_to_le16(dBm);
2772
2773 rc = mwl8k_post_cmd(hw, &cmd->header);
2774 kfree(cmd);
2775
2776 return rc;
2777}
2778
2779/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002780 * CMD_TX_POWER.
2781 */
2782#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2783
2784struct mwl8k_cmd_tx_power {
2785 struct mwl8k_cmd_pkt header;
2786 __le16 action;
2787 __le16 band;
2788 __le16 channel;
2789 __le16 bw;
2790 __le16 sub_ch;
2791 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05302792} __packed;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002793
2794static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2795 struct ieee80211_conf *conf,
2796 unsigned short pwr)
2797{
2798 struct ieee80211_channel *channel = conf->channel;
2799 struct mwl8k_cmd_tx_power *cmd;
2800 int rc;
2801 int i;
2802
2803 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2804 if (cmd == NULL)
2805 return -ENOMEM;
2806
2807 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2808 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2809 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2810
2811 if (channel->band == IEEE80211_BAND_2GHZ)
2812 cmd->band = cpu_to_le16(0x1);
2813 else if (channel->band == IEEE80211_BAND_5GHZ)
2814 cmd->band = cpu_to_le16(0x4);
2815
Yogesh Ashok Powar604c4ef2012-01-17 15:45:15 +05302816 cmd->channel = cpu_to_le16(channel->hw_value);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002817
2818 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2819 conf->channel_type == NL80211_CHAN_HT20) {
2820 cmd->bw = cpu_to_le16(0x2);
2821 } else {
2822 cmd->bw = cpu_to_le16(0x4);
2823 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2824 cmd->sub_ch = cpu_to_le16(0x3);
2825 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2826 cmd->sub_ch = cpu_to_le16(0x1);
2827 }
2828
2829 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2830 cmd->power_level_list[i] = cpu_to_le16(pwr);
2831
2832 rc = mwl8k_post_cmd(hw, &cmd->header);
2833 kfree(cmd);
2834
2835 return rc;
2836}
2837
2838/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002839 * CMD_RF_ANTENNA.
2840 */
2841struct mwl8k_cmd_rf_antenna {
2842 struct mwl8k_cmd_pkt header;
2843 __le16 antenna;
2844 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002845} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002846
2847#define MWL8K_RF_ANTENNA_RX 1
2848#define MWL8K_RF_ANTENNA_TX 2
2849
2850static int
2851mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2852{
2853 struct mwl8k_cmd_rf_antenna *cmd;
2854 int rc;
2855
2856 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2857 if (cmd == NULL)
2858 return -ENOMEM;
2859
2860 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2861 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2862 cmd->antenna = cpu_to_le16(antenna);
2863 cmd->mode = cpu_to_le16(mask);
2864
2865 rc = mwl8k_post_cmd(hw, &cmd->header);
2866 kfree(cmd);
2867
2868 return rc;
2869}
2870
2871/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002872 * CMD_SET_BEACON.
2873 */
2874struct mwl8k_cmd_set_beacon {
2875 struct mwl8k_cmd_pkt header;
2876 __le16 beacon_len;
2877 __u8 beacon[0];
2878};
2879
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002880static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2881 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002882{
2883 struct mwl8k_cmd_set_beacon *cmd;
2884 int rc;
2885
2886 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2887 if (cmd == NULL)
2888 return -ENOMEM;
2889
2890 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2891 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2892 cmd->beacon_len = cpu_to_le16(len);
2893 memcpy(cmd->beacon, beacon, len);
2894
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002895 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002896 kfree(cmd);
2897
2898 return rc;
2899}
2900
2901/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002902 * CMD_SET_PRE_SCAN.
2903 */
2904struct mwl8k_cmd_set_pre_scan {
2905 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002906} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002907
2908static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2909{
2910 struct mwl8k_cmd_set_pre_scan *cmd;
2911 int rc;
2912
2913 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2914 if (cmd == NULL)
2915 return -ENOMEM;
2916
2917 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2918 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2919
2920 rc = mwl8k_post_cmd(hw, &cmd->header);
2921 kfree(cmd);
2922
2923 return rc;
2924}
2925
2926/*
2927 * CMD_SET_POST_SCAN.
2928 */
2929struct mwl8k_cmd_set_post_scan {
2930 struct mwl8k_cmd_pkt header;
2931 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002932 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002933} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002934
2935static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002936mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002937{
2938 struct mwl8k_cmd_set_post_scan *cmd;
2939 int rc;
2940
2941 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2942 if (cmd == NULL)
2943 return -ENOMEM;
2944
2945 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2946 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2947 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002948 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949
2950 rc = mwl8k_post_cmd(hw, &cmd->header);
2951 kfree(cmd);
2952
2953 return rc;
2954}
2955
2956/*
2957 * CMD_SET_RF_CHANNEL.
2958 */
2959struct mwl8k_cmd_set_rf_channel {
2960 struct mwl8k_cmd_pkt header;
2961 __le16 action;
2962 __u8 current_channel;
2963 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002964} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002965
2966static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002967 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002968{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002969 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002970 struct mwl8k_cmd_set_rf_channel *cmd;
2971 int rc;
2972
2973 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2974 if (cmd == NULL)
2975 return -ENOMEM;
2976
2977 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2978 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2979 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2980 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002981
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002982 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002983 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002984 else if (channel->band == IEEE80211_BAND_5GHZ)
2985 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002986
2987 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2988 conf->channel_type == NL80211_CHAN_HT20)
2989 cmd->channel_flags |= cpu_to_le32(0x00000080);
2990 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2991 cmd->channel_flags |= cpu_to_le32(0x000001900);
2992 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2993 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002994
2995 rc = mwl8k_post_cmd(hw, &cmd->header);
2996 kfree(cmd);
2997
2998 return rc;
2999}
3000
3001/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003002 * CMD_SET_AID.
3003 */
3004#define MWL8K_FRAME_PROT_DISABLED 0x00
3005#define MWL8K_FRAME_PROT_11G 0x07
3006#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
3007#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
3008
3009struct mwl8k_cmd_update_set_aid {
3010 struct mwl8k_cmd_pkt header;
3011 __le16 aid;
3012
3013 /* AP's MAC address (BSSID) */
3014 __u8 bssid[ETH_ALEN];
3015 __le16 protection_mode;
3016 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003017} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003018
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003019static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3020{
3021 int i;
3022 int j;
3023
3024 /*
3025 * Clear nonstandard rates 4 and 13.
3026 */
3027 mask &= 0x1fef;
3028
3029 for (i = 0, j = 0; i < 14; i++) {
3030 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01003031 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003032 }
3033}
3034
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003035static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003036mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3037 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003038{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003039 struct mwl8k_cmd_update_set_aid *cmd;
3040 u16 prot_mode;
3041 int rc;
3042
3043 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3044 if (cmd == NULL)
3045 return -ENOMEM;
3046
3047 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3048 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003049 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003050 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003051
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003052 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003053 prot_mode = MWL8K_FRAME_PROT_11G;
3054 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003055 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003056 IEEE80211_HT_OP_MODE_PROTECTION) {
3057 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3058 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3059 break;
3060 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3061 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3062 break;
3063 default:
3064 prot_mode = MWL8K_FRAME_PROT_DISABLED;
3065 break;
3066 }
3067 }
3068 cmd->protection_mode = cpu_to_le16(prot_mode);
3069
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003070 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003071
3072 rc = mwl8k_post_cmd(hw, &cmd->header);
3073 kfree(cmd);
3074
3075 return rc;
3076}
3077
3078/*
3079 * CMD_SET_RATE.
3080 */
3081struct mwl8k_cmd_set_rate {
3082 struct mwl8k_cmd_pkt header;
3083 __u8 legacy_rates[14];
3084
3085 /* Bitmap for supported MCS codes. */
3086 __u8 mcs_set[16];
3087 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003088} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003089
3090static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003091mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003092 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003093{
3094 struct mwl8k_cmd_set_rate *cmd;
3095 int rc;
3096
3097 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3098 if (cmd == NULL)
3099 return -ENOMEM;
3100
3101 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3102 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003103 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003104 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003105
3106 rc = mwl8k_post_cmd(hw, &cmd->header);
3107 kfree(cmd);
3108
3109 return rc;
3110}
3111
3112/*
3113 * CMD_FINALIZE_JOIN.
3114 */
3115#define MWL8K_FJ_BEACON_MAXLEN 128
3116
3117struct mwl8k_cmd_finalize_join {
3118 struct mwl8k_cmd_pkt header;
3119 __le32 sleep_interval; /* Number of beacon periods to sleep */
3120 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003121} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003122
3123static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3124 int framelen, int dtim)
3125{
3126 struct mwl8k_cmd_finalize_join *cmd;
3127 struct ieee80211_mgmt *payload = frame;
3128 int payload_len;
3129 int rc;
3130
3131 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3132 if (cmd == NULL)
3133 return -ENOMEM;
3134
3135 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3136 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3137 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3138
3139 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3140 if (payload_len < 0)
3141 payload_len = 0;
3142 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3143 payload_len = MWL8K_FJ_BEACON_MAXLEN;
3144
3145 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3146
3147 rc = mwl8k_post_cmd(hw, &cmd->header);
3148 kfree(cmd);
3149
3150 return rc;
3151}
3152
3153/*
3154 * CMD_SET_RTS_THRESHOLD.
3155 */
3156struct mwl8k_cmd_set_rts_threshold {
3157 struct mwl8k_cmd_pkt header;
3158 __le16 action;
3159 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003160} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003161
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003162static int
3163mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003164{
3165 struct mwl8k_cmd_set_rts_threshold *cmd;
3166 int rc;
3167
3168 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3169 if (cmd == NULL)
3170 return -ENOMEM;
3171
3172 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3173 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003174 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3175 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003176
3177 rc = mwl8k_post_cmd(hw, &cmd->header);
3178 kfree(cmd);
3179
3180 return rc;
3181}
3182
3183/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003184 * CMD_SET_SLOT.
3185 */
3186struct mwl8k_cmd_set_slot {
3187 struct mwl8k_cmd_pkt header;
3188 __le16 action;
3189 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003190} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003191
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003192static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003193{
3194 struct mwl8k_cmd_set_slot *cmd;
3195 int rc;
3196
3197 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3198 if (cmd == NULL)
3199 return -ENOMEM;
3200
3201 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3202 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3203 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003204 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003205
3206 rc = mwl8k_post_cmd(hw, &cmd->header);
3207 kfree(cmd);
3208
3209 return rc;
3210}
3211
3212/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003213 * CMD_SET_EDCA_PARAMS.
3214 */
3215struct mwl8k_cmd_set_edca_params {
3216 struct mwl8k_cmd_pkt header;
3217
3218 /* See MWL8K_SET_EDCA_XXX below */
3219 __le16 action;
3220
3221 /* TX opportunity in units of 32 us */
3222 __le16 txop;
3223
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003224 union {
3225 struct {
3226 /* Log exponent of max contention period: 0...15 */
3227 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003228
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003229 /* Log exponent of min contention period: 0...15 */
3230 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003231
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003232 /* Adaptive interframe spacing in units of 32us */
3233 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003234
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003235 /* TX queue to configure */
3236 __u8 txq;
3237 } ap;
3238 struct {
3239 /* Log exponent of max contention period: 0...15 */
3240 __u8 log_cw_max;
3241
3242 /* Log exponent of min contention period: 0...15 */
3243 __u8 log_cw_min;
3244
3245 /* Adaptive interframe spacing in units of 32us */
3246 __u8 aifs;
3247
3248 /* TX queue to configure */
3249 __u8 txq;
3250 } sta;
3251 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003252} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003253
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003254#define MWL8K_SET_EDCA_CW 0x01
3255#define MWL8K_SET_EDCA_TXOP 0x02
3256#define MWL8K_SET_EDCA_AIFS 0x04
3257
3258#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3259 MWL8K_SET_EDCA_TXOP | \
3260 MWL8K_SET_EDCA_AIFS)
3261
3262static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003263mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3264 __u16 cw_min, __u16 cw_max,
3265 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003266{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003267 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003268 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003269 int rc;
3270
3271 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3272 if (cmd == NULL)
3273 return -ENOMEM;
3274
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003275 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3276 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003277 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3278 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003279 if (priv->ap_fw) {
3280 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3281 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3282 cmd->ap.aifs = aifs;
3283 cmd->ap.txq = qnum;
3284 } else {
3285 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3286 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3287 cmd->sta.aifs = aifs;
3288 cmd->sta.txq = qnum;
3289 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003290
3291 rc = mwl8k_post_cmd(hw, &cmd->header);
3292 kfree(cmd);
3293
3294 return rc;
3295}
3296
3297/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003298 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003299 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003300struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003301 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003302 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003303} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003304
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003305static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003306{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003307 struct mwl8k_priv *priv = hw->priv;
3308 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003309 int rc;
3310
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003311 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3312 if (cmd == NULL)
3313 return -ENOMEM;
3314
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003315 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003316 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003317 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003318
3319 rc = mwl8k_post_cmd(hw, &cmd->header);
3320 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003321
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003322 if (!rc)
3323 priv->wmm_enabled = enable;
3324
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003325 return rc;
3326}
3327
3328/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003329 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003330 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003331struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003332 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003333 __le32 action;
3334 __u8 rx_antenna_map;
3335 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003336} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003337
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003338static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003339{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003340 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003341 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003342
3343 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3344 if (cmd == NULL)
3345 return -ENOMEM;
3346
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003347 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003348 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003349 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3350 cmd->rx_antenna_map = rx;
3351 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003352
3353 rc = mwl8k_post_cmd(hw, &cmd->header);
3354 kfree(cmd);
3355
3356 return rc;
3357}
3358
3359/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003360 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003361 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003362struct mwl8k_cmd_use_fixed_rate_sta {
3363 struct mwl8k_cmd_pkt header;
3364 __le32 action;
3365 __le32 allow_rate_drop;
3366 __le32 num_rates;
3367 struct {
3368 __le32 is_ht_rate;
3369 __le32 enable_retry;
3370 __le32 rate;
3371 __le32 retry_count;
3372 } rate_entry[8];
3373 __le32 rate_type;
3374 __le32 reserved1;
3375 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003376} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003377
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003378#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003379#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003380
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003381static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003382{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003383 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003384 int rc;
3385
3386 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3387 if (cmd == NULL)
3388 return -ENOMEM;
3389
3390 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3391 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003392 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3393 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003394
3395 rc = mwl8k_post_cmd(hw, &cmd->header);
3396 kfree(cmd);
3397
3398 return rc;
3399}
3400
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003401/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003402 * CMD_USE_FIXED_RATE (AP version).
3403 */
3404struct mwl8k_cmd_use_fixed_rate_ap {
3405 struct mwl8k_cmd_pkt header;
3406 __le32 action;
3407 __le32 allow_rate_drop;
3408 __le32 num_rates;
3409 struct mwl8k_rate_entry_ap {
3410 __le32 is_ht_rate;
3411 __le32 enable_retry;
3412 __le32 rate;
3413 __le32 retry_count;
3414 } rate_entry[4];
3415 u8 multicast_rate;
3416 u8 multicast_rate_type;
3417 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003418} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003419
3420static int
3421mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3422{
3423 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3424 int rc;
3425
3426 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3427 if (cmd == NULL)
3428 return -ENOMEM;
3429
3430 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3431 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3432 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3433 cmd->multicast_rate = mcast;
3434 cmd->management_rate = mgmt;
3435
3436 rc = mwl8k_post_cmd(hw, &cmd->header);
3437 kfree(cmd);
3438
3439 return rc;
3440}
3441
3442/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003443 * CMD_ENABLE_SNIFFER.
3444 */
3445struct mwl8k_cmd_enable_sniffer {
3446 struct mwl8k_cmd_pkt header;
3447 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003448} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003449
3450static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3451{
3452 struct mwl8k_cmd_enable_sniffer *cmd;
3453 int rc;
3454
3455 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3456 if (cmd == NULL)
3457 return -ENOMEM;
3458
3459 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3460 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3461 cmd->action = cpu_to_le32(!!enable);
3462
3463 rc = mwl8k_post_cmd(hw, &cmd->header);
3464 kfree(cmd);
3465
3466 return rc;
3467}
3468
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303469struct mwl8k_cmd_update_mac_addr {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003470 struct mwl8k_cmd_pkt header;
3471 union {
3472 struct {
3473 __le16 mac_type;
3474 __u8 mac_addr[ETH_ALEN];
3475 } mbss;
3476 __u8 mac_addr[ETH_ALEN];
3477 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003478} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003479
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003480#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3481#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3482#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3483#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003484
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303485static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3486 struct ieee80211_vif *vif, u8 *mac, bool set)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003487{
3488 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003489 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303490 struct mwl8k_cmd_update_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003491 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003492 int rc;
3493
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003494 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3495 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3496 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3497 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3498 else
3499 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3500 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3501 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3502 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3503 else
3504 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3505 }
3506
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003507 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3508 if (cmd == NULL)
3509 return -ENOMEM;
3510
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303511 if (set)
3512 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3513 else
3514 cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3515
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003516 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3517 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003518 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003519 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3520 } else {
3521 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3522 }
3523
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003524 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003525 kfree(cmd);
3526
3527 return rc;
3528}
3529
3530/*
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303531 * MWL8K_CMD_SET_MAC_ADDR.
3532 */
3533static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3534 struct ieee80211_vif *vif, u8 *mac)
3535{
3536 return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3537}
3538
3539/*
3540 * MWL8K_CMD_DEL_MAC_ADDR.
3541 */
3542static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3543 struct ieee80211_vif *vif, u8 *mac)
3544{
3545 return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3546}
3547
3548/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003549 * CMD_SET_RATEADAPT_MODE.
3550 */
3551struct mwl8k_cmd_set_rate_adapt_mode {
3552 struct mwl8k_cmd_pkt header;
3553 __le16 action;
3554 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003555} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003556
3557static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3558{
3559 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3560 int rc;
3561
3562 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3563 if (cmd == NULL)
3564 return -ENOMEM;
3565
3566 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3567 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3568 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3569 cmd->mode = cpu_to_le16(mode);
3570
3571 rc = mwl8k_post_cmd(hw, &cmd->header);
3572 kfree(cmd);
3573
3574 return rc;
3575}
3576
3577/*
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003578 * CMD_GET_WATCHDOG_BITMAP.
3579 */
3580struct mwl8k_cmd_get_watchdog_bitmap {
3581 struct mwl8k_cmd_pkt header;
3582 u8 bitmap;
3583} __packed;
3584
3585static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3586{
3587 struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3588 int rc;
3589
3590 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3591 if (cmd == NULL)
3592 return -ENOMEM;
3593
3594 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3595 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3596
3597 rc = mwl8k_post_cmd(hw, &cmd->header);
3598 if (!rc)
3599 *bitmap = cmd->bitmap;
3600
3601 kfree(cmd);
3602
3603 return rc;
3604}
3605
3606#define INVALID_BA 0xAA
3607static void mwl8k_watchdog_ba_events(struct work_struct *work)
3608{
3609 int rc;
3610 u8 bitmap = 0, stream_index;
3611 struct mwl8k_ampdu_stream *streams;
3612 struct mwl8k_priv *priv =
3613 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3614
3615 rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3616 if (rc)
3617 return;
3618
3619 if (bitmap == INVALID_BA)
3620 return;
3621
3622 /* the bitmap is the hw queue number. Map it to the ampdu queue. */
3623 stream_index = bitmap - MWL8K_TX_WMM_QUEUES;
3624
3625 BUG_ON(stream_index >= priv->num_ampdu_queues);
3626
3627 streams = &priv->ampdu[stream_index];
3628
3629 if (streams->state == AMPDU_STREAM_ACTIVE)
3630 ieee80211_stop_tx_ba_session(streams->sta, streams->tid);
3631
3632 return;
3633}
3634
3635
3636/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003637 * CMD_BSS_START.
3638 */
3639struct mwl8k_cmd_bss_start {
3640 struct mwl8k_cmd_pkt header;
3641 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003642} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003643
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003644static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3645 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003646{
3647 struct mwl8k_cmd_bss_start *cmd;
3648 int rc;
3649
3650 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3651 if (cmd == NULL)
3652 return -ENOMEM;
3653
3654 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3655 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3656 cmd->enable = cpu_to_le32(enable);
3657
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003658 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003659 kfree(cmd);
3660
3661 return rc;
3662}
3663
3664/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003665 * CMD_BASTREAM.
3666 */
3667
3668/*
3669 * UPSTREAM is tx direction
3670 */
3671#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3672#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3673
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303674enum ba_stream_action_type {
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003675 MWL8K_BA_CREATE,
3676 MWL8K_BA_UPDATE,
3677 MWL8K_BA_DESTROY,
3678 MWL8K_BA_FLUSH,
3679 MWL8K_BA_CHECK,
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303680};
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003681
3682
3683struct mwl8k_create_ba_stream {
3684 __le32 flags;
3685 __le32 idle_thrs;
3686 __le32 bar_thrs;
3687 __le32 window_size;
3688 u8 peer_mac_addr[6];
3689 u8 dialog_token;
3690 u8 tid;
3691 u8 queue_id;
3692 u8 param_info;
3693 __le32 ba_context;
3694 u8 reset_seq_no_flag;
3695 __le16 curr_seq_no;
3696 u8 sta_src_mac_addr[6];
3697} __packed;
3698
3699struct mwl8k_destroy_ba_stream {
3700 __le32 flags;
3701 __le32 ba_context;
3702} __packed;
3703
3704struct mwl8k_cmd_bastream {
3705 struct mwl8k_cmd_pkt header;
3706 __le32 action;
3707 union {
3708 struct mwl8k_create_ba_stream create_params;
3709 struct mwl8k_destroy_ba_stream destroy_params;
3710 };
3711} __packed;
3712
3713static int
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303714mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3715 struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003716{
3717 struct mwl8k_cmd_bastream *cmd;
3718 int rc;
3719
3720 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3721 if (cmd == NULL)
3722 return -ENOMEM;
3723
3724 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3725 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3726
3727 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3728
3729 cmd->create_params.queue_id = stream->idx;
3730 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3731 ETH_ALEN);
3732 cmd->create_params.tid = stream->tid;
3733
3734 cmd->create_params.flags =
3735 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3736 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3737
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303738 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003739
3740 kfree(cmd);
3741
3742 return rc;
3743}
3744
3745static int
3746mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303747 u8 buf_size, struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003748{
3749 struct mwl8k_cmd_bastream *cmd;
3750 int rc;
3751
3752 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3753 if (cmd == NULL)
3754 return -ENOMEM;
3755
3756
3757 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3758 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3759
3760 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3761
3762 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3763 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3764 cmd->create_params.queue_id = stream->idx;
3765
3766 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3767 cmd->create_params.tid = stream->tid;
3768 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3769 cmd->create_params.reset_seq_no_flag = 1;
3770
3771 cmd->create_params.param_info =
3772 (stream->sta->ht_cap.ampdu_factor &
3773 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3774 ((stream->sta->ht_cap.ampdu_density << 2) &
3775 IEEE80211_HT_AMPDU_PARM_DENSITY);
3776
3777 cmd->create_params.flags =
3778 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3779 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3780
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303781 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003782
3783 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3784 stream->sta->addr, stream->tid);
3785 kfree(cmd);
3786
3787 return rc;
3788}
3789
3790static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3791 struct mwl8k_ampdu_stream *stream)
3792{
3793 struct mwl8k_cmd_bastream *cmd;
3794
3795 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3796 if (cmd == NULL)
3797 return;
3798
3799 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3800 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3801 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3802
3803 cmd->destroy_params.ba_context = cpu_to_le32(stream->idx);
3804 mwl8k_post_cmd(hw, &cmd->header);
3805
3806 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", stream->idx);
3807
3808 kfree(cmd);
3809}
3810
3811/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003812 * CMD_SET_NEW_STN.
3813 */
3814struct mwl8k_cmd_set_new_stn {
3815 struct mwl8k_cmd_pkt header;
3816 __le16 aid;
3817 __u8 mac_addr[6];
3818 __le16 stn_id;
3819 __le16 action;
3820 __le16 rsvd;
3821 __le32 legacy_rates;
3822 __u8 ht_rates[4];
3823 __le16 cap_info;
3824 __le16 ht_capabilities_info;
3825 __u8 mac_ht_param_info;
3826 __u8 rev;
3827 __u8 control_channel;
3828 __u8 add_channel;
3829 __le16 op_mode;
3830 __le16 stbc;
3831 __u8 add_qos_info;
3832 __u8 is_qos_sta;
3833 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003834} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003835
3836#define MWL8K_STA_ACTION_ADD 0
3837#define MWL8K_STA_ACTION_REMOVE 2
3838
3839static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3840 struct ieee80211_vif *vif,
3841 struct ieee80211_sta *sta)
3842{
3843 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003844 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003845 int rc;
3846
3847 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3848 if (cmd == NULL)
3849 return -ENOMEM;
3850
3851 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3852 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3853 cmd->aid = cpu_to_le16(sta->aid);
3854 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3855 cmd->stn_id = cpu_to_le16(sta->aid);
3856 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003857 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3858 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3859 else
3860 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3861 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003862 if (sta->ht_cap.ht_supported) {
3863 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3864 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3865 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3866 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3867 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3868 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3869 ((sta->ht_cap.ampdu_density & 7) << 2);
3870 cmd->is_qos_sta = 1;
3871 }
3872
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003873 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003874 kfree(cmd);
3875
3876 return rc;
3877}
3878
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003879static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3880 struct ieee80211_vif *vif)
3881{
3882 struct mwl8k_cmd_set_new_stn *cmd;
3883 int rc;
3884
3885 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3886 if (cmd == NULL)
3887 return -ENOMEM;
3888
3889 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3890 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3891 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3892
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003893 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003894 kfree(cmd);
3895
3896 return rc;
3897}
3898
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003899static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3900 struct ieee80211_vif *vif, u8 *addr)
3901{
3902 struct mwl8k_cmd_set_new_stn *cmd;
3903 int rc;
3904
3905 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3906 if (cmd == NULL)
3907 return -ENOMEM;
3908
3909 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3910 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3911 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3912 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3913
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003914 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003915 kfree(cmd);
3916
3917 return rc;
3918}
3919
3920/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003921 * CMD_UPDATE_ENCRYPTION.
3922 */
3923
3924#define MAX_ENCR_KEY_LENGTH 16
3925#define MIC_KEY_LENGTH 8
3926
3927struct mwl8k_cmd_update_encryption {
3928 struct mwl8k_cmd_pkt header;
3929
3930 __le32 action;
3931 __le32 reserved;
3932 __u8 mac_addr[6];
3933 __u8 encr_type;
3934
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303935} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003936
3937struct mwl8k_cmd_set_key {
3938 struct mwl8k_cmd_pkt header;
3939
3940 __le32 action;
3941 __le32 reserved;
3942 __le16 length;
3943 __le16 key_type_id;
3944 __le32 key_info;
3945 __le32 key_id;
3946 __le16 key_len;
3947 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3948 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3949 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3950 __le16 tkip_rsc_low;
3951 __le32 tkip_rsc_high;
3952 __le16 tkip_tsc_low;
3953 __le32 tkip_tsc_high;
3954 __u8 mac_addr[6];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303955} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003956
3957enum {
3958 MWL8K_ENCR_ENABLE,
3959 MWL8K_ENCR_SET_KEY,
3960 MWL8K_ENCR_REMOVE_KEY,
3961 MWL8K_ENCR_SET_GROUP_KEY,
3962};
3963
3964#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3965#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3966#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3967#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3968#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3969
3970enum {
3971 MWL8K_ALG_WEP,
3972 MWL8K_ALG_TKIP,
3973 MWL8K_ALG_CCMP,
3974};
3975
3976#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3977#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3978#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3979#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3980#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3981
3982static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3983 struct ieee80211_vif *vif,
3984 u8 *addr,
3985 u8 encr_type)
3986{
3987 struct mwl8k_cmd_update_encryption *cmd;
3988 int rc;
3989
3990 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3991 if (cmd == NULL)
3992 return -ENOMEM;
3993
3994 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3995 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3996 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3997 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3998 cmd->encr_type = encr_type;
3999
4000 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4001 kfree(cmd);
4002
4003 return rc;
4004}
4005
4006static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4007 u8 *addr,
4008 struct ieee80211_key_conf *key)
4009{
4010 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4011 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4012 cmd->length = cpu_to_le16(sizeof(*cmd) -
4013 offsetof(struct mwl8k_cmd_set_key, length));
4014 cmd->key_id = cpu_to_le32(key->keyidx);
4015 cmd->key_len = cpu_to_le16(key->keylen);
4016 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4017
4018 switch (key->cipher) {
4019 case WLAN_CIPHER_SUITE_WEP40:
4020 case WLAN_CIPHER_SUITE_WEP104:
4021 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4022 if (key->keyidx == 0)
4023 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4024
4025 break;
4026 case WLAN_CIPHER_SUITE_TKIP:
4027 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4028 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4029 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4030 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4031 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4032 | MWL8K_KEY_FLAG_TSC_VALID);
4033 break;
4034 case WLAN_CIPHER_SUITE_CCMP:
4035 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4036 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4037 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4038 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4039 break;
4040 default:
4041 return -ENOTSUPP;
4042 }
4043
4044 return 0;
4045}
4046
4047static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4048 struct ieee80211_vif *vif,
4049 u8 *addr,
4050 struct ieee80211_key_conf *key)
4051{
4052 struct mwl8k_cmd_set_key *cmd;
4053 int rc;
4054 int keymlen;
4055 u32 action;
4056 u8 idx;
4057 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4058
4059 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4060 if (cmd == NULL)
4061 return -ENOMEM;
4062
4063 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4064 if (rc < 0)
4065 goto done;
4066
4067 idx = key->keyidx;
4068
4069 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4070 action = MWL8K_ENCR_SET_KEY;
4071 else
4072 action = MWL8K_ENCR_SET_GROUP_KEY;
4073
4074 switch (key->cipher) {
4075 case WLAN_CIPHER_SUITE_WEP40:
4076 case WLAN_CIPHER_SUITE_WEP104:
4077 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4078 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4079 sizeof(*key) + key->keylen);
4080 mwl8k_vif->wep_key_conf[idx].enabled = 1;
4081 }
4082
Yogesh Ashok Powar9b571e22011-05-04 17:22:16 +05304083 keymlen = key->keylen;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004084 action = MWL8K_ENCR_SET_KEY;
4085 break;
4086 case WLAN_CIPHER_SUITE_TKIP:
4087 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4088 break;
4089 case WLAN_CIPHER_SUITE_CCMP:
4090 keymlen = key->keylen;
4091 break;
4092 default:
4093 rc = -ENOTSUPP;
4094 goto done;
4095 }
4096
4097 memcpy(cmd->key_material, key->key, keymlen);
4098 cmd->action = cpu_to_le32(action);
4099
4100 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4101done:
4102 kfree(cmd);
4103
4104 return rc;
4105}
4106
4107static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4108 struct ieee80211_vif *vif,
4109 u8 *addr,
4110 struct ieee80211_key_conf *key)
4111{
4112 struct mwl8k_cmd_set_key *cmd;
4113 int rc;
4114 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4115
4116 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4117 if (cmd == NULL)
4118 return -ENOMEM;
4119
4120 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4121 if (rc < 0)
4122 goto done;
4123
4124 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Dan Carpenterd981e052012-01-17 10:33:31 +03004125 key->cipher == WLAN_CIPHER_SUITE_WEP104)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004126 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4127
4128 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4129
4130 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4131done:
4132 kfree(cmd);
4133
4134 return rc;
4135}
4136
4137static int mwl8k_set_key(struct ieee80211_hw *hw,
4138 enum set_key_cmd cmd_param,
4139 struct ieee80211_vif *vif,
4140 struct ieee80211_sta *sta,
4141 struct ieee80211_key_conf *key)
4142{
4143 int rc = 0;
4144 u8 encr_type;
4145 u8 *addr;
4146 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4147
4148 if (vif->type == NL80211_IFTYPE_STATION)
4149 return -EOPNOTSUPP;
4150
4151 if (sta == NULL)
Yogesh Ashok Powarff7e9f92012-01-27 16:08:27 +05304152 addr = vif->addr;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004153 else
4154 addr = sta->addr;
4155
4156 if (cmd_param == SET_KEY) {
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004157 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4158 if (rc)
4159 goto out;
4160
4161 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4162 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4163 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4164 else
4165 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4166
4167 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4168 encr_type);
4169 if (rc)
4170 goto out;
4171
4172 mwl8k_vif->is_hw_crypto_enabled = true;
4173
4174 } else {
4175 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4176
4177 if (rc)
4178 goto out;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004179 }
4180out:
4181 return rc;
4182}
4183
4184/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004185 * CMD_UPDATE_STADB.
4186 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004187struct ewc_ht_info {
4188 __le16 control1;
4189 __le16 control2;
4190 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004191} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004192
4193struct peer_capability_info {
4194 /* Peer type - AP vs. STA. */
4195 __u8 peer_type;
4196
4197 /* Basic 802.11 capabilities from assoc resp. */
4198 __le16 basic_caps;
4199
4200 /* Set if peer supports 802.11n high throughput (HT). */
4201 __u8 ht_support;
4202
4203 /* Valid if HT is supported. */
4204 __le16 ht_caps;
4205 __u8 extended_ht_caps;
4206 struct ewc_ht_info ewc_info;
4207
4208 /* Legacy rate table. Intersection of our rates and peer rates. */
4209 __u8 legacy_rates[12];
4210
4211 /* HT rate table. Intersection of our rates and peer rates. */
4212 __u8 ht_rates[16];
4213 __u8 pad[16];
4214
4215 /* If set, interoperability mode, no proprietary extensions. */
4216 __u8 interop;
4217 __u8 pad2;
4218 __u8 station_id;
4219 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004220} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004221
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004222struct mwl8k_cmd_update_stadb {
4223 struct mwl8k_cmd_pkt header;
4224
4225 /* See STADB_ACTION_TYPE */
4226 __le32 action;
4227
4228 /* Peer MAC address */
4229 __u8 peer_addr[ETH_ALEN];
4230
4231 __le32 reserved;
4232
4233 /* Peer info - valid during add/update. */
4234 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004235} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004236
Lennert Buytenheka6804002010-01-04 21:55:42 +01004237#define MWL8K_STA_DB_MODIFY_ENTRY 1
4238#define MWL8K_STA_DB_DEL_ENTRY 2
4239
4240/* Peer Entry flags - used to define the type of the peer node */
4241#define MWL8K_PEER_TYPE_ACCESSPOINT 2
4242
4243static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004244 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004245 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004246{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004247 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004248 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004249 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004250 int rc;
4251
4252 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4253 if (cmd == NULL)
4254 return -ENOMEM;
4255
4256 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4257 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01004258 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004259 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004260
Lennert Buytenheka6804002010-01-04 21:55:42 +01004261 p = &cmd->peer_info;
4262 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4263 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004264 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04004265 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004266 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4267 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004268 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4269 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4270 else
4271 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4272 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004273 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004274 p->interop = 1;
4275 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004276
Lennert Buytenheka6804002010-01-04 21:55:42 +01004277 rc = mwl8k_post_cmd(hw, &cmd->header);
4278 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004279
Lennert Buytenheka6804002010-01-04 21:55:42 +01004280 return rc ? rc : p->station_id;
4281}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004282
Lennert Buytenheka6804002010-01-04 21:55:42 +01004283static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4284 struct ieee80211_vif *vif, u8 *addr)
4285{
4286 struct mwl8k_cmd_update_stadb *cmd;
4287 int rc;
4288
4289 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4290 if (cmd == NULL)
4291 return -ENOMEM;
4292
4293 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4294 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4295 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4296 memcpy(cmd->peer_addr, addr, ETH_ALEN);
4297
4298 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004299 kfree(cmd);
4300
4301 return rc;
4302}
4303
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004304
4305/*
4306 * Interrupt handling.
4307 */
4308static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4309{
4310 struct ieee80211_hw *hw = dev_id;
4311 struct mwl8k_priv *priv = hw->priv;
4312 u32 status;
4313
4314 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004315 if (!status)
4316 return IRQ_NONE;
4317
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004318 if (status & MWL8K_A2H_INT_TX_DONE) {
4319 status &= ~MWL8K_A2H_INT_TX_DONE;
4320 tasklet_schedule(&priv->poll_tx_task);
4321 }
4322
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004323 if (status & MWL8K_A2H_INT_RX_READY) {
4324 status &= ~MWL8K_A2H_INT_RX_READY;
4325 tasklet_schedule(&priv->poll_rx_task);
4326 }
4327
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004328 if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4329 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4330 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4331 }
4332
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004333 if (status)
4334 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004335
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004336 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004337 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004338 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004339 }
4340
4341 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004342 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004343 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004344 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004345 }
4346
4347 return IRQ_HANDLED;
4348}
4349
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004350static void mwl8k_tx_poll(unsigned long data)
4351{
4352 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4353 struct mwl8k_priv *priv = hw->priv;
4354 int limit;
4355 int i;
4356
4357 limit = 32;
4358
4359 spin_lock_bh(&priv->tx_lock);
4360
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004361 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004362 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4363
4364 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4365 complete(priv->tx_wait);
4366 priv->tx_wait = NULL;
4367 }
4368
4369 spin_unlock_bh(&priv->tx_lock);
4370
4371 if (limit) {
4372 writel(~MWL8K_A2H_INT_TX_DONE,
4373 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4374 } else {
4375 tasklet_schedule(&priv->poll_tx_task);
4376 }
4377}
4378
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004379static void mwl8k_rx_poll(unsigned long data)
4380{
4381 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4382 struct mwl8k_priv *priv = hw->priv;
4383 int limit;
4384
4385 limit = 32;
4386 limit -= rxq_process(hw, 0, limit);
4387 limit -= rxq_refill(hw, 0, limit);
4388
4389 if (limit) {
4390 writel(~MWL8K_A2H_INT_RX_READY,
4391 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4392 } else {
4393 tasklet_schedule(&priv->poll_rx_task);
4394 }
4395}
4396
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004397
4398/*
4399 * Core driver operations.
4400 */
Thomas Huehn36323f82012-07-23 21:33:42 +02004401static void mwl8k_tx(struct ieee80211_hw *hw,
4402 struct ieee80211_tx_control *control,
4403 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004404{
4405 struct mwl8k_priv *priv = hw->priv;
4406 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004407
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004408 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004409 wiphy_debug(hw->wiphy,
4410 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004411 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004412 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004413 }
4414
Thomas Huehn36323f82012-07-23 21:33:42 +02004415 mwl8k_txq_xmit(hw, index, control->sta, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004416}
4417
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004418static int mwl8k_start(struct ieee80211_hw *hw)
4419{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004420 struct mwl8k_priv *priv = hw->priv;
4421 int rc;
4422
Joe Perchesa0607fd2009-11-18 23:29:17 -08004423 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004424 IRQF_SHARED, MWL8K_NAME, hw);
4425 if (rc) {
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304426 priv->irq = -1;
Joe Perches5db55842010-08-11 19:11:19 -07004427 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004428 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004429 }
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304430 priv->irq = priv->pdev->irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004431
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004432 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004433 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004434 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004435
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004436 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004437 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05304438 iowrite32(MWL8K_A2H_EVENTS,
4439 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004440
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004441 rc = mwl8k_fw_lock(hw);
4442 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004443 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004444
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004445 if (!priv->ap_fw) {
4446 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004447 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004448
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004449 if (!rc)
4450 rc = mwl8k_cmd_set_pre_scan(hw);
4451
4452 if (!rc)
4453 rc = mwl8k_cmd_set_post_scan(hw,
4454 "\x00\x00\x00\x00\x00\x00");
4455 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004456
4457 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004458 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004459
4460 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004461 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004462
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004463 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004464 }
4465
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004466 if (rc) {
4467 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4468 free_irq(priv->pdev->irq, hw);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304469 priv->irq = -1;
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004470 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004471 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004472 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004473
4474 return rc;
4475}
4476
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004477static void mwl8k_stop(struct ieee80211_hw *hw)
4478{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004479 struct mwl8k_priv *priv = hw->priv;
4480 int i;
4481
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304482 if (!priv->hw_restart_in_progress)
4483 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004484
4485 ieee80211_stop_queues(hw);
4486
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004487 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004488 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304489 if (priv->irq != -1) {
4490 free_irq(priv->pdev->irq, hw);
4491 priv->irq = -1;
4492 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004493
4494 /* Stop finalize join worker */
4495 cancel_work_sync(&priv->finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004496 cancel_work_sync(&priv->watchdog_ba_handle);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004497 if (priv->beacon_skb != NULL)
4498 dev_kfree_skb(priv->beacon_skb);
4499
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004500 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004501 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004502 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004503
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004504 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004505 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004506 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004507}
4508
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004509static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4510
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004511static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004512 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004513{
4514 struct mwl8k_priv *priv = hw->priv;
4515 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004516 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004517 int macid, rc;
4518 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004519
4520 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004521 * Reject interface creation if sniffer mode is active, as
4522 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004523 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004524 */
4525 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004526 wiphy_info(hw->wiphy,
4527 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004528 return -EINVAL;
4529 }
4530
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004531 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004532 switch (vif->type) {
4533 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004534 if (!priv->ap_fw && di->fw_image_ap) {
4535 /* we must load the ap fw to meet this request */
4536 if (!list_empty(&priv->vif_list))
4537 return -EBUSY;
4538 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4539 if (rc)
4540 return rc;
4541 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004542 macids_supported = priv->ap_macids_supported;
4543 break;
4544 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004545 if (priv->ap_fw && di->fw_image_sta) {
4546 /* we must load the sta fw to meet this request */
4547 if (!list_empty(&priv->vif_list))
4548 return -EBUSY;
4549 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4550 if (rc)
4551 return rc;
4552 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004553 macids_supported = priv->sta_macids_supported;
4554 break;
4555 default:
4556 return -EINVAL;
4557 }
4558
4559 macid = ffs(macids_supported & ~priv->macids_used);
4560 if (!macid--)
4561 return -EBUSY;
4562
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004563 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004564 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004565 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004566 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004567 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004568 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004569 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4570 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004571
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004572 /* Set the mac address. */
4573 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4574
4575 if (priv->ap_fw)
4576 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4577
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004578 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004579 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004580
4581 return 0;
4582}
4583
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304584static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4585{
4586 /* Has ieee80211_restart_hw re-added the removed interfaces? */
4587 if (!priv->macids_used)
4588 return;
4589
4590 priv->macids_used &= ~(1 << vif->macid);
4591 list_del(&vif->list);
4592}
4593
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004594static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004595 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004596{
4597 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004598 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004599
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004600 if (priv->ap_fw)
4601 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4602
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05304603 mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004604
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304605 mwl8k_remove_vif(priv, mwl8k_vif);
4606}
4607
4608static void mwl8k_hw_restart_work(struct work_struct *work)
4609{
4610 struct mwl8k_priv *priv =
4611 container_of(work, struct mwl8k_priv, fw_reload);
4612 struct ieee80211_hw *hw = priv->hw;
4613 struct mwl8k_device_info *di;
4614 int rc;
4615
4616 /* If some command is waiting for a response, clear it */
4617 if (priv->hostcmd_wait != NULL) {
4618 complete(priv->hostcmd_wait);
4619 priv->hostcmd_wait = NULL;
4620 }
4621
4622 priv->hw_restart_owner = current;
4623 di = priv->device_info;
4624 mwl8k_fw_lock(hw);
4625
4626 if (priv->ap_fw)
4627 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4628 else
4629 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4630
4631 if (rc)
4632 goto fail;
4633
4634 priv->hw_restart_owner = NULL;
4635 priv->hw_restart_in_progress = false;
4636
4637 /*
4638 * This unlock will wake up the queues and
4639 * also opens the command path for other
4640 * commands
4641 */
4642 mwl8k_fw_unlock(hw);
4643
4644 ieee80211_restart_hw(hw);
4645
4646 wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4647
4648 return;
4649fail:
4650 mwl8k_fw_unlock(hw);
4651
4652 wiphy_err(hw->wiphy, "Firmware restart failed\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004653}
4654
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004655static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004656{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004657 struct ieee80211_conf *conf = &hw->conf;
4658 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004659 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004660
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004661 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004662 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004663 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004664 }
4665
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004666 rc = mwl8k_fw_lock(hw);
4667 if (rc)
4668 return rc;
4669
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004670 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004671 if (rc)
4672 goto out;
4673
Lennert Buytenhek610677d2010-01-04 21:56:46 +01004674 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004675 if (rc)
4676 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004677
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004678 if (conf->power_level > 18)
4679 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004680
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004681 if (priv->ap_fw) {
Nishant Sarmukadam03217082011-04-05 14:18:09 +05304682
4683 if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4684 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4685 if (rc)
4686 goto out;
4687 }
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004688
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004689 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
4690 if (rc)
4691 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
4692 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
4693 if (rc)
4694 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
4695
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004696 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004697 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4698 if (rc)
4699 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004700 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4701 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004702
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004703out:
4704 mwl8k_fw_unlock(hw);
4705
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004706 return rc;
4707}
4708
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004709static void
4710mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4711 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004712{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004713 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304714 u32 ap_legacy_rates = 0;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004715 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004716 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004717
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004718 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004719 return;
4720
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004721 /*
4722 * No need to capture a beacon if we're no longer associated.
4723 */
4724 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4725 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004726
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004727 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004728 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004729 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004730 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004731 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004732
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004733 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004734
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004735 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004736 if (ap == NULL) {
4737 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004738 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004739 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004740
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004741 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4742 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4743 } else {
4744 ap_legacy_rates =
4745 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4746 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004747 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004748
4749 rcu_read_unlock();
4750 }
4751
4752 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004753 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004754 if (rc)
4755 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004756
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004757 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004758 if (rc)
4759 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004760 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004761
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004762 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004763 rc = mwl8k_set_radio_preamble(hw,
4764 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004765 if (rc)
4766 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004767 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004768
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004769 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004770 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004771 if (rc)
4772 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004773 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004774
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004775 if (vif->bss_conf.assoc &&
4776 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4777 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004778 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004779 if (rc)
4780 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004781 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004782
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004783 if (vif->bss_conf.assoc &&
4784 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004785 /*
4786 * Finalize the join. Tell rx handler to process
4787 * next beacon from our BSSID.
4788 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004789 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004790 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004791 }
4792
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004793out:
4794 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004795}
4796
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004797static void
4798mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4799 struct ieee80211_bss_conf *info, u32 changed)
4800{
4801 int rc;
4802
4803 if (mwl8k_fw_lock(hw))
4804 return;
4805
4806 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4807 rc = mwl8k_set_radio_preamble(hw,
4808 vif->bss_conf.use_short_preamble);
4809 if (rc)
4810 goto out;
4811 }
4812
4813 if (changed & BSS_CHANGED_BASIC_RATES) {
4814 int idx;
4815 int rate;
4816
4817 /*
4818 * Use lowest supported basic rate for multicasts
4819 * and management frames (such as probe responses --
4820 * beacons will always go out at 1 Mb/s).
4821 */
4822 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004823 if (idx)
4824 idx--;
4825
4826 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4827 rate = mwl8k_rates_24[idx].hw_value;
4828 else
4829 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004830
4831 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4832 }
4833
4834 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4835 struct sk_buff *skb;
4836
4837 skb = ieee80211_beacon_get(hw, vif);
4838 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004839 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004840 kfree_skb(skb);
4841 }
4842 }
4843
4844 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004845 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004846
4847out:
4848 mwl8k_fw_unlock(hw);
4849}
4850
4851static void
4852mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4853 struct ieee80211_bss_conf *info, u32 changed)
4854{
4855 struct mwl8k_priv *priv = hw->priv;
4856
4857 if (!priv->ap_fw)
4858 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4859 else
4860 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4861}
4862
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004863static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00004864 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004865{
4866 struct mwl8k_cmd_pkt *cmd;
4867
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004868 /*
4869 * Synthesize and return a command packet that programs the
4870 * hardware multicast address filter. At this point we don't
4871 * know whether FIF_ALLMULTI is being requested, but if it is,
4872 * we'll end up throwing this packet away and creating a new
4873 * one in mwl8k_configure_filter().
4874 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004875 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004876
4877 return (unsigned long)cmd;
4878}
4879
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004880static int
4881mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4882 unsigned int changed_flags,
4883 unsigned int *total_flags)
4884{
4885 struct mwl8k_priv *priv = hw->priv;
4886
4887 /*
4888 * Hardware sniffer mode is mutually exclusive with STA
4889 * operation, so refuse to enable sniffer mode if a STA
4890 * interface is active.
4891 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004892 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004893 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004894 wiphy_info(hw->wiphy,
4895 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004896 return 0;
4897 }
4898
4899 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004900 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004901 return 0;
4902 priv->sniffer_enabled = true;
4903 }
4904
4905 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4906 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4907 FIF_OTHER_BSS;
4908
4909 return 1;
4910}
4911
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004912static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4913{
4914 if (!list_empty(&priv->vif_list))
4915 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4916
4917 return NULL;
4918}
4919
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004920static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4921 unsigned int changed_flags,
4922 unsigned int *total_flags,
4923 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004924{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004925 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004926 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4927
4928 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004929 * AP firmware doesn't allow fine-grained control over
4930 * the receive filter.
4931 */
4932 if (priv->ap_fw) {
4933 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4934 kfree(cmd);
4935 return;
4936 }
4937
4938 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004939 * Enable hardware sniffer mode if FIF_CONTROL or
4940 * FIF_OTHER_BSS is requested.
4941 */
4942 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4943 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4944 kfree(cmd);
4945 return;
4946 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004947
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004948 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004949 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004950
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004951 if (mwl8k_fw_lock(hw)) {
4952 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004953 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004954 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004955
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004956 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004957 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004958 priv->sniffer_enabled = false;
4959 }
4960
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004961 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004962 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4963 /*
4964 * Disable the BSS filter.
4965 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004966 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004967 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004968 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004969 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004970
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004971 /*
4972 * Enable the BSS filter.
4973 *
4974 * If there is an active STA interface, use that
4975 * interface's BSSID, otherwise use a dummy one
4976 * (where the OUI part needs to be nonzero for
4977 * the BSSID to be accepted by POST_SCAN).
4978 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004979 mwl8k_vif = mwl8k_first_vif(priv);
4980 if (mwl8k_vif != NULL)
4981 bssid = mwl8k_vif->vif->bss_conf.bssid;
4982 else
4983 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004984
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004985 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004986 }
4987 }
4988
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004989 /*
4990 * If FIF_ALLMULTI is being requested, throw away the command
4991 * packet that ->prepare_multicast() built and replace it with
4992 * a command packet that enables reception of all multicast
4993 * packets.
4994 */
4995 if (*total_flags & FIF_ALLMULTI) {
4996 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004997 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004998 }
4999
5000 if (cmd != NULL) {
5001 mwl8k_post_cmd(hw, cmd);
5002 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005003 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02005004
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005005 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005006}
5007
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005008static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5009{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01005010 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005011}
5012
Johannes Berg4a6967b2010-02-19 19:18:37 +01005013static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5014 struct ieee80211_vif *vif,
5015 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005016{
5017 struct mwl8k_priv *priv = hw->priv;
5018
Johannes Berg4a6967b2010-02-19 19:18:37 +01005019 if (priv->ap_fw)
5020 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5021 else
5022 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5023}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005024
Johannes Berg4a6967b2010-02-19 19:18:37 +01005025static int mwl8k_sta_add(struct ieee80211_hw *hw,
5026 struct ieee80211_vif *vif,
5027 struct ieee80211_sta *sta)
5028{
5029 struct mwl8k_priv *priv = hw->priv;
5030 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005031 int i;
5032 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5033 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005034
Johannes Berg4a6967b2010-02-19 19:18:37 +01005035 if (!priv->ap_fw) {
5036 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5037 if (ret >= 0) {
5038 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07005039 if (sta->ht_cap.ht_supported)
5040 MWL8K_STA(sta)->is_ampdu_allowed = true;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005041 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005042 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01005043
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005044 } else {
5045 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005046 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005047
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005048 for (i = 0; i < NUM_WEP_KEYS; i++) {
5049 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5050 if (mwl8k_vif->wep_key_conf[i].enabled)
5051 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5052 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005053 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01005054}
5055
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005056static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5057 struct ieee80211_vif *vif, u16 queue,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005058 const struct ieee80211_tx_queue_params *params)
5059{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005060 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005061 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005062
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005063 rc = mwl8k_fw_lock(hw);
5064 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005065 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005066 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5067
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005068 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005069 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005070
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005071 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005072 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005073 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005074 params->cw_min,
5075 params->cw_max,
5076 params->aifs,
5077 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005078 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005079
5080 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005081 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005082
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005083 return rc;
5084}
5085
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005086static int mwl8k_get_stats(struct ieee80211_hw *hw,
5087 struct ieee80211_low_level_stats *stats)
5088{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005089 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005090}
5091
John W. Linville0d462bb2010-07-28 14:04:24 -04005092static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5093 struct survey_info *survey)
5094{
5095 struct mwl8k_priv *priv = hw->priv;
5096 struct ieee80211_conf *conf = &hw->conf;
5097
5098 if (idx != 0)
5099 return -ENOENT;
5100
5101 survey->channel = conf->channel;
5102 survey->filled = SURVEY_INFO_NOISE_DBM;
5103 survey->noise = priv->noise;
5104
5105 return 0;
5106}
5107
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005108#define MAX_AMPDU_ATTEMPTS 5
5109
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005110static int
5111mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5112 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01005113 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5114 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005115{
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005116
5117 int i, rc = 0;
5118 struct mwl8k_priv *priv = hw->priv;
5119 struct mwl8k_ampdu_stream *stream;
5120 u8 *addr = sta->addr;
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305121 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005122
5123 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
5124 return -ENOTSUPP;
5125
5126 spin_lock(&priv->stream_lock);
5127 stream = mwl8k_lookup_stream(hw, addr, tid);
5128
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005129 switch (action) {
5130 case IEEE80211_AMPDU_RX_START:
5131 case IEEE80211_AMPDU_RX_STOP:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005132 break;
5133 case IEEE80211_AMPDU_TX_START:
5134 /* By the time we get here the hw queues may contain outgoing
5135 * packets for this RA/TID that are not part of this BA
5136 * session. The hw will assign sequence numbers to these
5137 * packets as they go out. So if we query the hw for its next
5138 * sequence number and use that for the SSN here, it may end up
5139 * being wrong, which will lead to sequence number mismatch at
5140 * the recipient. To avoid this, we reset the sequence number
5141 * to O for the first MPDU in this BA stream.
5142 */
5143 *ssn = 0;
5144 if (stream == NULL) {
5145 /* This means that somebody outside this driver called
5146 * ieee80211_start_tx_ba_session. This is unexpected
5147 * because we do our own rate control. Just warn and
5148 * move on.
5149 */
5150 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
5151 "Proceeding anyway.\n", __func__);
5152 stream = mwl8k_add_stream(hw, sta, tid);
5153 }
5154 if (stream == NULL) {
5155 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5156 rc = -EBUSY;
5157 break;
5158 }
5159 stream->state = AMPDU_STREAM_IN_PROGRESS;
5160
5161 /* Release the lock before we do the time consuming stuff */
5162 spin_unlock(&priv->stream_lock);
5163 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305164
5165 /* Check if link is still valid */
5166 if (!sta_info->is_ampdu_allowed) {
5167 spin_lock(&priv->stream_lock);
5168 mwl8k_remove_stream(hw, stream);
5169 spin_unlock(&priv->stream_lock);
5170 return -EBUSY;
5171 }
5172
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305173 rc = mwl8k_check_ba(hw, stream, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005174
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305175 /* If HW restart is in progress mwl8k_post_cmd will
5176 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5177 * such cases
5178 */
5179 if (!rc || rc == -EBUSY)
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005180 break;
5181 /*
5182 * HW queues take time to be flushed, give them
5183 * sufficient time
5184 */
5185
5186 msleep(1000);
5187 }
5188 spin_lock(&priv->stream_lock);
5189 if (rc) {
5190 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5191 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5192 mwl8k_remove_stream(hw, stream);
5193 rc = -EBUSY;
5194 break;
5195 }
5196 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
5197 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02005198 case IEEE80211_AMPDU_TX_STOP_CONT:
5199 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5200 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305201 if (stream) {
5202 if (stream->state == AMPDU_STREAM_ACTIVE) {
5203 spin_unlock(&priv->stream_lock);
5204 mwl8k_destroy_ba(hw, stream);
5205 spin_lock(&priv->stream_lock);
5206 }
5207 mwl8k_remove_stream(hw, stream);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005208 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005209 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5210 break;
5211 case IEEE80211_AMPDU_TX_OPERATIONAL:
5212 BUG_ON(stream == NULL);
5213 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5214 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305215 rc = mwl8k_create_ba(hw, stream, buf_size, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005216 spin_lock(&priv->stream_lock);
5217 if (!rc)
5218 stream->state = AMPDU_STREAM_ACTIVE;
5219 else {
5220 spin_unlock(&priv->stream_lock);
5221 mwl8k_destroy_ba(hw, stream);
5222 spin_lock(&priv->stream_lock);
5223 wiphy_debug(hw->wiphy,
5224 "Failed adding stream for sta %pM tid %d\n",
5225 addr, tid);
5226 mwl8k_remove_stream(hw, stream);
5227 }
5228 break;
5229
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005230 default:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005231 rc = -ENOTSUPP;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005232 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005233
5234 spin_unlock(&priv->stream_lock);
5235 return rc;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005236}
5237
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005238static const struct ieee80211_ops mwl8k_ops = {
5239 .tx = mwl8k_tx,
5240 .start = mwl8k_start,
5241 .stop = mwl8k_stop,
5242 .add_interface = mwl8k_add_interface,
5243 .remove_interface = mwl8k_remove_interface,
5244 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005245 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02005246 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005247 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005248 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005249 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01005250 .sta_add = mwl8k_sta_add,
5251 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005252 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005253 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04005254 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005255 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005256};
5257
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005258static void mwl8k_finalize_join_worker(struct work_struct *work)
5259{
5260 struct mwl8k_priv *priv =
5261 container_of(work, struct mwl8k_priv, finalize_join_worker);
5262 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01005263 struct ieee80211_mgmt *mgmt = (void *)skb->data;
5264 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5265 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5266 mgmt->u.beacon.variable, len);
5267 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005268
Johannes Berg56007a02010-01-26 14:19:52 +01005269 if (tim && tim[1] >= 2)
5270 dtim_period = tim[3];
5271
5272 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005273
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005274 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005275 priv->beacon_skb = NULL;
5276}
5277
John W. Linvillebcb628d2009-11-06 16:40:16 -05005278enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005279 MWL8363 = 0,
5280 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005281 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02005282};
5283
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005284#define MWL8K_8366_AP_FW_API 2
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005285#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5286#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5287
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005288static struct mwl8k_device_info mwl8k_info_tbl[] = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005289 [MWL8363] = {
5290 .part_name = "88w8363",
5291 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005292 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005293 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005294 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005295 .part_name = "88w8687",
5296 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005297 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05005298 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005299 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005300 .part_name = "88w8366",
5301 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005302 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005303 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5304 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01005305 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005306 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005307};
5308
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005309MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5310MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5311MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5312MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5313MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5314MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005315MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005316
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005317static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01005318 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005319 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5320 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005321 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5322 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5323 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01005324 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005325 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005326};
5327MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5328
Brian Cavagnolo99020472010-11-12 17:23:52 -08005329static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5330{
5331 int rc;
5332 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5333 "Trying alternative firmware %s\n", pci_name(priv->pdev),
5334 priv->fw_pref, priv->fw_alt);
5335 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5336 if (rc) {
5337 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5338 pci_name(priv->pdev), priv->fw_alt);
5339 return rc;
5340 }
5341 return 0;
5342}
5343
5344static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5345static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5346{
5347 struct mwl8k_priv *priv = context;
5348 struct mwl8k_device_info *di = priv->device_info;
5349 int rc;
5350
5351 switch (priv->fw_state) {
5352 case FW_STATE_INIT:
5353 if (!fw) {
5354 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5355 pci_name(priv->pdev), di->helper_image);
5356 goto fail;
5357 }
5358 priv->fw_helper = fw;
5359 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5360 true);
5361 if (rc && priv->fw_alt) {
5362 rc = mwl8k_request_alt_fw(priv);
5363 if (rc)
5364 goto fail;
5365 priv->fw_state = FW_STATE_LOADING_ALT;
5366 } else if (rc)
5367 goto fail;
5368 else
5369 priv->fw_state = FW_STATE_LOADING_PREF;
5370 break;
5371
5372 case FW_STATE_LOADING_PREF:
5373 if (!fw) {
5374 if (priv->fw_alt) {
5375 rc = mwl8k_request_alt_fw(priv);
5376 if (rc)
5377 goto fail;
5378 priv->fw_state = FW_STATE_LOADING_ALT;
5379 } else
5380 goto fail;
5381 } else {
5382 priv->fw_ucode = fw;
5383 rc = mwl8k_firmware_load_success(priv);
5384 if (rc)
5385 goto fail;
5386 else
5387 complete(&priv->firmware_loading_complete);
5388 }
5389 break;
5390
5391 case FW_STATE_LOADING_ALT:
5392 if (!fw) {
5393 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5394 pci_name(priv->pdev), di->helper_image);
5395 goto fail;
5396 }
5397 priv->fw_ucode = fw;
5398 rc = mwl8k_firmware_load_success(priv);
5399 if (rc)
5400 goto fail;
5401 else
5402 complete(&priv->firmware_loading_complete);
5403 break;
5404
5405 default:
5406 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5407 MWL8K_NAME, priv->fw_state);
5408 BUG_ON(1);
5409 }
5410
5411 return;
5412
5413fail:
5414 priv->fw_state = FW_STATE_ERROR;
5415 complete(&priv->firmware_loading_complete);
5416 device_release_driver(&priv->pdev->dev);
5417 mwl8k_release_firmware(priv);
5418}
5419
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305420#define MAX_RESTART_ATTEMPTS 1
Brian Cavagnolo99020472010-11-12 17:23:52 -08005421static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5422 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005423{
5424 struct mwl8k_priv *priv = hw->priv;
5425 int rc;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305426 int count = MAX_RESTART_ATTEMPTS;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005427
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305428retry:
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005429 /* Reset firmware and hardware */
5430 mwl8k_hw_reset(priv);
5431
5432 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005433 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005434 if (rc) {
5435 wiphy_err(hw->wiphy, "Firmware files not found\n");
5436 return rc;
5437 }
5438
Brian Cavagnolo99020472010-11-12 17:23:52 -08005439 if (nowait)
5440 return rc;
5441
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005442 /* Load firmware into hardware */
5443 rc = mwl8k_load_firmware(hw);
5444 if (rc)
5445 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5446
5447 /* Reclaim memory once firmware is successfully loaded */
5448 mwl8k_release_firmware(priv);
5449
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305450 if (rc && count) {
5451 /* FW did not start successfully;
5452 * lets try one more time
5453 */
5454 count--;
5455 wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5456 msleep(20);
5457 goto retry;
5458 }
5459
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005460 return rc;
5461}
5462
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005463static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5464{
5465 struct mwl8k_priv *priv = hw->priv;
5466 int rc = 0;
5467 int i;
5468
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005469 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005470 rc = mwl8k_txq_init(hw, i);
5471 if (rc)
5472 break;
5473 if (priv->ap_fw)
5474 iowrite32(priv->txq[i].txd_dma,
5475 priv->sram + priv->txq_offset[i]);
5476 }
5477 return rc;
5478}
5479
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005480/* initialize hw after successfully loading a firmware image */
5481static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5482{
5483 struct mwl8k_priv *priv = hw->priv;
5484 int rc = 0;
5485 int i;
5486
5487 if (priv->ap_fw) {
5488 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5489 if (priv->rxd_ops == NULL) {
5490 wiphy_err(hw->wiphy,
5491 "Driver does not have AP firmware image support for this hardware\n");
5492 goto err_stop_firmware;
5493 }
5494 } else {
5495 priv->rxd_ops = &rxd_sta_ops;
5496 }
5497
5498 priv->sniffer_enabled = false;
5499 priv->wmm_enabled = false;
5500 priv->pending_tx_pkts = 0;
5501
5502 rc = mwl8k_rxq_init(hw, 0);
5503 if (rc)
5504 goto err_stop_firmware;
5505 rxq_refill(hw, 0, INT_MAX);
5506
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005507 /* For the sta firmware, we need to know the dma addresses of tx queues
5508 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5509 * prior to issuing this command. But for the AP case, we learn the
5510 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5511 * case we must initialize the tx queues after.
5512 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005513 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005514 if (!priv->ap_fw) {
5515 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005516 if (rc)
5517 goto err_free_queues;
5518 }
5519
5520 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5521 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005522 iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5523 MWL8K_A2H_INT_BA_WATCHDOG,
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005524 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05305525 iowrite32(MWL8K_A2H_INT_OPC_DONE,
5526 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005527
5528 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5529 IRQF_SHARED, MWL8K_NAME, hw);
5530 if (rc) {
5531 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5532 goto err_free_queues;
5533 }
5534
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305535 /*
5536 * When hw restart is requested,
5537 * mac80211 will take care of clearing
5538 * the ampdu streams, so do not clear
5539 * the ampdu state here
5540 */
5541 if (!priv->hw_restart_in_progress)
5542 memset(priv->ampdu, 0, sizeof(priv->ampdu));
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005543
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005544 /*
5545 * Temporarily enable interrupts. Initial firmware host
5546 * commands use interrupts and avoid polling. Disable
5547 * interrupts when done.
5548 */
5549 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5550
5551 /* Get config data, mac addrs etc */
5552 if (priv->ap_fw) {
5553 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5554 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005555 rc = mwl8k_init_txqs(hw);
5556 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005557 rc = mwl8k_cmd_set_hw_spec(hw);
5558 } else {
5559 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5560 }
5561 if (rc) {
5562 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5563 goto err_free_irq;
5564 }
5565
5566 /* Turn radio off */
5567 rc = mwl8k_cmd_radio_disable(hw);
5568 if (rc) {
5569 wiphy_err(hw->wiphy, "Cannot disable\n");
5570 goto err_free_irq;
5571 }
5572
5573 /* Clear MAC address */
5574 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5575 if (rc) {
5576 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5577 goto err_free_irq;
5578 }
5579
5580 /* Disable interrupts */
5581 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5582 free_irq(priv->pdev->irq, hw);
5583
5584 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5585 priv->device_info->part_name,
5586 priv->hw_rev, hw->wiphy->perm_addr,
5587 priv->ap_fw ? "AP" : "STA",
5588 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5589 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5590
5591 return 0;
5592
5593err_free_irq:
5594 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5595 free_irq(priv->pdev->irq, hw);
5596
5597err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005598 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005599 mwl8k_txq_deinit(hw, i);
5600 mwl8k_rxq_deinit(hw, 0);
5601
5602err_stop_firmware:
5603 mwl8k_hw_reset(priv);
5604
5605 return rc;
5606}
5607
5608/*
5609 * invoke mwl8k_reload_firmware to change the firmware image after the device
5610 * has already been registered
5611 */
5612static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5613{
5614 int i, rc = 0;
5615 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305616 struct mwl8k_vif *vif, *tmp_vif;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005617
5618 mwl8k_stop(hw);
5619 mwl8k_rxq_deinit(hw, 0);
5620
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305621 /*
5622 * All the existing interfaces are re-added by the ieee80211_reconfig;
5623 * which means driver should remove existing interfaces before calling
5624 * ieee80211_restart_hw
5625 */
5626 if (priv->hw_restart_in_progress)
5627 list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
5628 mwl8k_remove_vif(priv, vif);
5629
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005630 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005631 mwl8k_txq_deinit(hw, i);
5632
Brian Cavagnolo99020472010-11-12 17:23:52 -08005633 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005634 if (rc)
5635 goto fail;
5636
5637 rc = mwl8k_probe_hw(hw);
5638 if (rc)
5639 goto fail;
5640
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305641 if (priv->hw_restart_in_progress)
5642 return rc;
5643
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005644 rc = mwl8k_start(hw);
5645 if (rc)
5646 goto fail;
5647
5648 rc = mwl8k_config(hw, ~0);
5649 if (rc)
5650 goto fail;
5651
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005652 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005653 rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005654 if (rc)
5655 goto fail;
5656 }
5657
5658 return rc;
5659
5660fail:
5661 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5662 return rc;
5663}
5664
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305665static const struct ieee80211_iface_limit ap_if_limits[] = {
5666 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
5667};
5668
5669static const struct ieee80211_iface_combination ap_if_comb = {
5670 .limits = ap_if_limits,
5671 .n_limits = ARRAY_SIZE(ap_if_limits),
5672 .max_interfaces = 8,
5673 .num_different_channels = 1,
5674};
5675
5676
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005677static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5678{
5679 struct ieee80211_hw *hw = priv->hw;
5680 int i, rc;
5681
Brian Cavagnolo99020472010-11-12 17:23:52 -08005682 rc = mwl8k_load_firmware(hw);
5683 mwl8k_release_firmware(priv);
5684 if (rc) {
5685 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5686 return rc;
5687 }
5688
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005689 /*
5690 * Extra headroom is the size of the required DMA header
5691 * minus the size of the smallest 802.11 frame (CTS frame).
5692 */
5693 hw->extra_tx_headroom =
5694 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5695
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05305696 hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
5697
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005698 hw->channel_change_time = 10;
5699
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005700 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005701
5702 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005703 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Yogesh Ashok Powar2a36a0e2011-08-29 17:12:44 +05305704
5705 /*
5706 * Ask mac80211 to not to trigger PS mode
5707 * based on PM bit of incoming frames.
5708 */
5709 if (priv->ap_fw)
5710 hw->flags |= IEEE80211_HW_AP_LINK_PS;
5711
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005712 hw->vif_data_size = sizeof(struct mwl8k_vif);
5713 hw->sta_data_size = sizeof(struct mwl8k_sta);
5714
5715 priv->macids_used = 0;
5716 INIT_LIST_HEAD(&priv->vif_list);
5717
5718 /* Set default radio state and preamble */
Rusty Russell3db1cd52011-12-19 13:56:45 +00005719 priv->radio_on = false;
5720 priv->radio_short_preamble = false;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005721
5722 /* Finalize join worker */
5723 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005724 /* Handle watchdog ba events */
5725 INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305726 /* To reload the firmware if it crashes */
5727 INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005728
5729 /* TX reclaim and RX tasklets. */
5730 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5731 tasklet_disable(&priv->poll_tx_task);
5732 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5733 tasklet_disable(&priv->poll_rx_task);
5734
5735 /* Power management cookie */
5736 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5737 if (priv->cookie == NULL)
5738 return -ENOMEM;
5739
5740 mutex_init(&priv->fw_mutex);
5741 priv->fw_mutex_owner = NULL;
5742 priv->fw_mutex_depth = 0;
5743 priv->hostcmd_wait = NULL;
5744
5745 spin_lock_init(&priv->tx_lock);
5746
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005747 spin_lock_init(&priv->stream_lock);
5748
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005749 priv->tx_wait = NULL;
5750
5751 rc = mwl8k_probe_hw(hw);
5752 if (rc)
5753 goto err_free_cookie;
5754
5755 hw->wiphy->interface_modes = 0;
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305756
5757 if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005758 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305759 hw->wiphy->iface_combinations = &ap_if_comb;
5760 hw->wiphy->n_iface_combinations = 1;
5761 }
5762
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005763 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5764 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5765
5766 rc = ieee80211_register_hw(hw);
5767 if (rc) {
5768 wiphy_err(hw->wiphy, "Cannot register device\n");
5769 goto err_unprobe_hw;
5770 }
5771
5772 return 0;
5773
5774err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005775 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005776 mwl8k_txq_deinit(hw, i);
5777 mwl8k_rxq_deinit(hw, 0);
5778
5779err_free_cookie:
5780 if (priv->cookie != NULL)
5781 pci_free_consistent(priv->pdev, 4,
5782 priv->cookie, priv->cookie_dma);
5783
5784 return rc;
5785}
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005786static int mwl8k_probe(struct pci_dev *pdev,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005787 const struct pci_device_id *id)
5788{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005789 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005790 struct ieee80211_hw *hw;
5791 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005792 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005793 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005794
5795 if (!printed_version) {
5796 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5797 printed_version = 1;
5798 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005799
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005800
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005801 rc = pci_enable_device(pdev);
5802 if (rc) {
5803 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5804 MWL8K_NAME);
5805 return rc;
5806 }
5807
5808 rc = pci_request_regions(pdev, MWL8K_NAME);
5809 if (rc) {
5810 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5811 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005812 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005813 }
5814
5815 pci_set_master(pdev);
5816
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005817
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005818 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
5819 if (hw == NULL) {
5820 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
5821 rc = -ENOMEM;
5822 goto err_free_reg;
5823 }
5824
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005825 SET_IEEE80211_DEV(hw, &pdev->dev);
5826 pci_set_drvdata(pdev, hw);
5827
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005828 priv = hw->priv;
5829 priv->hw = hw;
5830 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05005831 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005832
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005833
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005834 priv->sram = pci_iomap(pdev, 0, 0x10000);
5835 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005836 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005837 goto err_iounmap;
5838 }
5839
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005840 /*
5841 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
5842 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
5843 */
5844 priv->regs = pci_iomap(pdev, 1, 0x10000);
5845 if (priv->regs == NULL) {
5846 priv->regs = pci_iomap(pdev, 2, 0x10000);
5847 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005848 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005849 goto err_iounmap;
5850 }
5851 }
5852
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005853 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08005854 * Choose the initial fw image depending on user input. If a second
5855 * image is available, make it the alternative image that will be
5856 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005857 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005858 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005859 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005860 if (ap_mode_default && di->fw_image_ap) {
5861 priv->fw_pref = di->fw_image_ap;
5862 priv->fw_alt = di->fw_image_sta;
5863 } else if (!ap_mode_default && di->fw_image_sta) {
5864 priv->fw_pref = di->fw_image_sta;
5865 priv->fw_alt = di->fw_image_ap;
5866 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005867 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005868 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005869 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
5870 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005871 priv->fw_pref = di->fw_image_ap;
5872 }
5873 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005874 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005875 goto err_stop_firmware;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305876
5877 priv->hw_restart_in_progress = false;
5878
Brian Cavagnolo99020472010-11-12 17:23:52 -08005879 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005880
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005881err_stop_firmware:
5882 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005883
5884err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005885 if (priv->regs != NULL)
5886 pci_iounmap(pdev, priv->regs);
5887
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005888 if (priv->sram != NULL)
5889 pci_iounmap(pdev, priv->sram);
5890
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005891 pci_set_drvdata(pdev, NULL);
5892 ieee80211_free_hw(hw);
5893
5894err_free_reg:
5895 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005896
5897err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005898 pci_disable_device(pdev);
5899
5900 return rc;
5901}
5902
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005903static void mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005904{
5905 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
5906 struct mwl8k_priv *priv;
5907 int i;
5908
5909 if (hw == NULL)
5910 return;
5911 priv = hw->priv;
5912
Brian Cavagnolo99020472010-11-12 17:23:52 -08005913 wait_for_completion(&priv->firmware_loading_complete);
5914
5915 if (priv->fw_state == FW_STATE_ERROR) {
5916 mwl8k_hw_reset(priv);
5917 goto unmap;
5918 }
5919
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005920 ieee80211_stop_queues(hw);
5921
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02005922 ieee80211_unregister_hw(hw);
5923
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005924 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01005925 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005926 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005927
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005928 /* Stop hardware */
5929 mwl8k_hw_reset(priv);
5930
5931 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005932 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01005933 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005934
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005935 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005936 mwl8k_txq_deinit(hw, i);
5937
5938 mwl8k_rxq_deinit(hw, 0);
5939
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005940 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005941
Brian Cavagnolo99020472010-11-12 17:23:52 -08005942unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005943 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005944 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005945 pci_set_drvdata(pdev, NULL);
5946 ieee80211_free_hw(hw);
5947 pci_release_regions(pdev);
5948 pci_disable_device(pdev);
5949}
5950
5951static struct pci_driver mwl8k_driver = {
5952 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005953 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005954 .probe = mwl8k_probe,
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005955 .remove = mwl8k_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005956};
5957
Axel Lin5b0a3b72012-04-14 10:38:36 +08005958module_pci_driver(mwl8k_driver);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005959
5960MODULE_DESCRIPTION(MWL8K_DESC);
5961MODULE_VERSION(MWL8K_VERSION);
5962MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5963MODULE_LICENSE("GPL");