blob: ee1778cf270fc194ae6bdc0a4aac1b00b16746c8 [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 Tomonori53b1b3e12010-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
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +0530216 atomic_t watchdog_event_pending;
217
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218 /* lock held over TX and TX reap */
219 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100220
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200221 /* TX quiesce completion, protected by fw_mutex and tx_lock */
222 struct completion *tx_wait;
223
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100224 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100225 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100226 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100227
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100228 /* power management status cookie from firmware */
229 u32 *cookie;
230 dma_addr_t cookie_dma;
231
232 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100233 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200234 u32 fw_rev;
Jonas Gorskic3f251a2013-03-11 17:44:21 +0100235 u32 caps;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100236
237 /*
238 * Running count of TX packets in flight, to avoid
239 * iterating over the transmit rings each time.
240 */
241 int pending_tx_pkts;
242
243 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700244 struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
245 u32 txq_offset[MWL8K_MAX_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100246
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200247 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200248 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200249 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200250 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100251
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100252 /* XXX need to convert this to handle multiple interfaces */
253 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200254 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100255 struct sk_buff *beacon_skb;
256
257 /*
258 * This FJ worker has to be global as it is scheduled from the
259 * RX handler. At this point we don't know which interface it
260 * belongs to until the list of bssids waiting to complete join
261 * is checked.
262 */
263 struct work_struct finalize_join_worker;
264
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100265 /* Tasklet to perform TX reclaim. */
266 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100267
268 /* Tasklet to perform RX. */
269 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400270
271 /* Most recently reported noise in dBm */
272 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800273
274 /*
275 * preserve the queue configurations so they can be restored if/when
276 * the firmware image is swapped.
277 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700278 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800279
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +0530280 /* To perform the task of reloading the firmware */
281 struct work_struct fw_reload;
282 bool hw_restart_in_progress;
283
Brian Cavagnolo99020472010-11-12 17:23:52 -0800284 /* async firmware loading state */
285 unsigned fw_state;
286 char *fw_pref;
287 char *fw_alt;
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530288 bool is_8764;
Brian Cavagnolo99020472010-11-12 17:23:52 -0800289 struct completion firmware_loading_complete;
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +0530290
291 /* bitmap of running BSSes */
292 u32 running_bsses;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100293};
294
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800295#define MAX_WEP_KEY_LEN 13
296#define NUM_WEP_KEYS 4
297
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298/* Per interface specific private data */
299struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100300 struct list_head list;
301 struct ieee80211_vif *vif;
302
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100303 /* Firmware macid for this vif. */
304 int macid;
305
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100306 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100307 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800308
309 /* Saved WEP keys */
310 struct {
311 u8 enabled;
312 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
313 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800314
315 /* BSSID */
316 u8 bssid[ETH_ALEN];
317
318 /* A flag to indicate is HW crypto is enabled for this bssid */
319 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100320};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200321#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800322#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100323
Brian Cavagnolod0805c12011-04-12 11:06:28 -0700324struct tx_traffic_info {
325 u32 start_time;
326 u32 pkts;
327};
328
329#define MWL8K_MAX_TID 8
Lennert Buytenheka6804002010-01-04 21:55:42 +0100330struct mwl8k_sta {
331 /* Index into station database. Returned by UPDATE_STADB. */
332 u8 peer_id;
Nishant Sarmukadam170335432011-03-17 11:58:48 -0700333 u8 is_ampdu_allowed;
Brian Cavagnolod0805c12011-04-12 11:06:28 -0700334 struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
Lennert Buytenheka6804002010-01-04 21:55:42 +0100335};
336#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
337
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100338static const struct ieee80211_channel mwl8k_channels_24[] = {
Jonas Gorskid786f672013-02-08 16:07:25 +0100339 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
340 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
341 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
342 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
343 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
344 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
345 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
346 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
347 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
348 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
349 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
350 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
351 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
352 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100353};
354
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100355static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356 { .bitrate = 10, .hw_value = 2, },
357 { .bitrate = 20, .hw_value = 4, },
358 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200359 { .bitrate = 110, .hw_value = 22, },
360 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100361 { .bitrate = 60, .hw_value = 12, },
362 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100363 { .bitrate = 120, .hw_value = 24, },
364 { .bitrate = 180, .hw_value = 36, },
365 { .bitrate = 240, .hw_value = 48, },
366 { .bitrate = 360, .hw_value = 72, },
367 { .bitrate = 480, .hw_value = 96, },
368 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100369 { .bitrate = 720, .hw_value = 144, },
370};
371
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100372static const struct ieee80211_channel mwl8k_channels_50[] = {
Jonas Gorskid786f672013-02-08 16:07:25 +0100373 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
374 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
375 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
376 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100377};
378
379static const struct ieee80211_rate mwl8k_rates_50[] = {
380 { .bitrate = 60, .hw_value = 12, },
381 { .bitrate = 90, .hw_value = 18, },
382 { .bitrate = 120, .hw_value = 24, },
383 { .bitrate = 180, .hw_value = 36, },
384 { .bitrate = 240, .hw_value = 48, },
385 { .bitrate = 360, .hw_value = 72, },
386 { .bitrate = 480, .hw_value = 96, },
387 { .bitrate = 540, .hw_value = 108, },
388 { .bitrate = 720, .hw_value = 144, },
389};
390
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100391/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100392#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800393#define MWL8K_CMD_SET 0x0001
394#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100395
396/* Firmware command codes */
397#define MWL8K_CMD_CODE_DNLD 0x0001
398#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200399#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100400#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
401#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200402#define MWL8K_CMD_RADIO_CONTROL 0x001c
403#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800404#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200405#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100406#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100407#define MWL8K_CMD_SET_PRE_SCAN 0x0107
408#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200409#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100410#define MWL8K_CMD_SET_AID 0x010d
411#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200412#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100413#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200414#define MWL8K_CMD_SET_SLOT 0x0114
415#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
416#define MWL8K_CMD_SET_WMM_MODE 0x0123
417#define MWL8K_CMD_MIMO_CONFIG 0x0125
418#define MWL8K_CMD_USE_FIXED_RATE 0x0126
419#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100420#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200421#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700422#define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +0530423#define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100424#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
425#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800426#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200427#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700428#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100429
John W. Linvilleb6037422010-07-20 13:55:00 -0400430static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100431{
John W. Linvilleb6037422010-07-20 13:55:00 -0400432 u16 command = le16_to_cpu(cmd);
433
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100434#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
435 snprintf(buf, bufsize, "%s", #x);\
436 return buf;\
437 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400438 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 MWL8K_CMDNAME(CODE_DNLD);
440 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200441 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100442 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
443 MWL8K_CMDNAME(GET_STAT);
444 MWL8K_CMDNAME(RADIO_CONTROL);
445 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800446 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200447 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100448 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100449 MWL8K_CMDNAME(SET_PRE_SCAN);
450 MWL8K_CMDNAME(SET_POST_SCAN);
451 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100452 MWL8K_CMDNAME(SET_AID);
453 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200454 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100455 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200456 MWL8K_CMDNAME(SET_SLOT);
457 MWL8K_CMDNAME(SET_EDCA_PARAMS);
458 MWL8K_CMDNAME(SET_WMM_MODE);
459 MWL8K_CMDNAME(MIMO_CONFIG);
460 MWL8K_CMDNAME(USE_FIXED_RATE);
461 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200462 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200463 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100464 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100465 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800466 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200467 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700468 MWL8K_CMDNAME(BASTREAM);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700469 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100470 default:
471 snprintf(buf, bufsize, "0x%x", cmd);
472 }
473#undef MWL8K_CMDNAME
474
475 return buf;
476}
477
478/* Hardware and firmware reset */
479static void mwl8k_hw_reset(struct mwl8k_priv *priv)
480{
481 iowrite32(MWL8K_H2A_INT_RESET,
482 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
483 iowrite32(MWL8K_H2A_INT_RESET,
484 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
485 msleep(20);
486}
487
488/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800489static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100490{
491 if (*fw == NULL)
492 return;
493 release_firmware(*fw);
494 *fw = NULL;
495}
496
497static void mwl8k_release_firmware(struct mwl8k_priv *priv)
498{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100499 mwl8k_release_fw(&priv->fw_ucode);
500 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100501}
502
Brian Cavagnolo99020472010-11-12 17:23:52 -0800503/* states for asynchronous f/w loading */
504static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
505enum {
506 FW_STATE_INIT = 0,
507 FW_STATE_LOADING_PREF,
508 FW_STATE_LOADING_ALT,
509 FW_STATE_ERROR,
510};
511
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100512/* Request fw image */
513static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800514 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800515 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100516{
517 /* release current image */
518 if (*fw != NULL)
519 mwl8k_release_fw(fw);
520
Brian Cavagnolo99020472010-11-12 17:23:52 -0800521 if (nowait)
522 return request_firmware_nowait(THIS_MODULE, 1, fname,
523 &priv->pdev->dev, GFP_KERNEL,
524 priv, mwl8k_fw_state_machine);
525 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800526 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100527}
528
Brian Cavagnolo99020472010-11-12 17:23:52 -0800529static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
530 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100531{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200532 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100533 int rc;
534
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200535 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800536 if (nowait)
537 rc = mwl8k_request_fw(priv, di->helper_image,
538 &priv->fw_helper, true);
539 else
540 rc = mwl8k_request_fw(priv, di->helper_image,
541 &priv->fw_helper, false);
542 if (rc)
543 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
544 pci_name(priv->pdev), di->helper_image);
545
546 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200547 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100548 }
549
Brian Cavagnolo99020472010-11-12 17:23:52 -0800550 if (nowait) {
551 /*
552 * if we get here, no helper image is needed. Skip the
553 * FW_STATE_INIT state.
554 */
555 priv->fw_state = FW_STATE_LOADING_PREF;
556 rc = mwl8k_request_fw(priv, fw_image,
557 &priv->fw_ucode,
558 true);
559 } else
560 rc = mwl8k_request_fw(priv, fw_image,
561 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100562 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200563 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800564 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100565 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100566 return rc;
567 }
568
569 return 0;
570}
571
572struct mwl8k_cmd_pkt {
573 __le16 code;
574 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100575 __u8 seq_num;
576 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577 __le16 result;
578 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000579} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100580
581/*
582 * Firmware loading.
583 */
584static int
585mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
586{
587 void __iomem *regs = priv->regs;
588 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100589 int loops;
590
591 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
592 if (pci_dma_mapping_error(priv->pdev, dma_addr))
593 return -ENOMEM;
594
595 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
596 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
597 iowrite32(MWL8K_H2A_INT_DOORBELL,
598 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
599 iowrite32(MWL8K_H2A_INT_DUMMY,
600 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
601
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100602 loops = 1000;
603 do {
604 u32 int_code;
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530605 if (priv->is_8764) {
606 int_code = ioread32(regs +
607 MWL8K_HIU_H2A_INTERRUPT_STATUS);
608 if (int_code == 0)
609 break;
610 } else {
611 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
612 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
613 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
614 break;
615 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100616 }
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200617 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100618 udelay(1);
619 } while (--loops);
620
621 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
622
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200623 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100624}
625
626static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
627 const u8 *data, size_t length)
628{
629 struct mwl8k_cmd_pkt *cmd;
630 int done;
631 int rc = 0;
632
633 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
634 if (cmd == NULL)
635 return -ENOMEM;
636
637 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
638 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100639 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100640 cmd->result = 0;
641
642 done = 0;
643 while (length) {
644 int block_size = length > 256 ? 256 : length;
645
646 memcpy(cmd->payload, data + done, block_size);
647 cmd->length = cpu_to_le16(block_size);
648
649 rc = mwl8k_send_fw_load_cmd(priv, cmd,
650 sizeof(*cmd) + block_size);
651 if (rc)
652 break;
653
654 done += block_size;
655 length -= block_size;
656 }
657
658 if (!rc) {
659 cmd->length = 0;
660 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
661 }
662
663 kfree(cmd);
664
665 return rc;
666}
667
668static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
669 const u8 *data, size_t length)
670{
671 unsigned char *buffer;
672 int may_continue, rc = 0;
673 u32 done, prev_block_size;
674
675 buffer = kmalloc(1024, GFP_KERNEL);
676 if (buffer == NULL)
677 return -ENOMEM;
678
679 done = 0;
680 prev_block_size = 0;
681 may_continue = 1000;
682 while (may_continue > 0) {
683 u32 block_size;
684
685 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
686 if (block_size & 1) {
687 block_size &= ~1;
688 may_continue--;
689 } else {
690 done += prev_block_size;
691 length -= prev_block_size;
692 }
693
694 if (block_size > 1024 || block_size > length) {
695 rc = -EOVERFLOW;
696 break;
697 }
698
699 if (length == 0) {
700 rc = 0;
701 break;
702 }
703
704 if (block_size == 0) {
705 rc = -EPROTO;
706 may_continue--;
707 udelay(1);
708 continue;
709 }
710
711 prev_block_size = block_size;
712 memcpy(buffer, data + done, block_size);
713
714 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
715 if (rc)
716 break;
717 }
718
719 if (!rc && length != 0)
720 rc = -EREMOTEIO;
721
722 kfree(buffer);
723
724 return rc;
725}
726
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200727static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100728{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200729 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800730 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200731 int rc;
732 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100733
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530734 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800735 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100736
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200737 if (helper == NULL) {
738 printk(KERN_ERR "%s: helper image needed but none "
739 "given\n", pci_name(priv->pdev));
740 return -EINVAL;
741 }
742
743 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100744 if (rc) {
745 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200746 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100747 return rc;
748 }
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +0530749 msleep(20);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100750
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200751 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100752 } else {
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530753 if (priv->is_8764)
754 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
755 else
756 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100757 }
758
759 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200760 printk(KERN_ERR "%s: unable to load firmware image\n",
761 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100762 return rc;
763 }
764
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100765 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100766
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100767 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100768 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200769 u32 ready_code;
770
771 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
772 if (ready_code == MWL8K_FWAP_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000773 priv->ap_fw = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100774 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200775 } else if (ready_code == MWL8K_FWSTA_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000776 priv->ap_fw = false;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200777 break;
778 }
779
780 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100781 udelay(1);
782 } while (--loops);
783
784 return loops ? 0 : -ETIMEDOUT;
785}
786
787
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100788/* DMA header used by firmware and hardware. */
789struct mwl8k_dma_data {
790 __le16 fwlen;
791 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100792 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000793} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100794
795/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100796static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100797{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100798 struct mwl8k_dma_data *tr;
799 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100800
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100801 tr = (struct mwl8k_dma_data *)skb->data;
802 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
803
804 if (hdrlen != sizeof(tr->wh)) {
805 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
806 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
807 *((__le16 *)(tr->data - 2)) = qos;
808 } else {
809 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
810 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100811 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100812
813 if (hdrlen != sizeof(*tr))
814 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100815}
816
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530817#define REDUCED_TX_HEADROOM 8
818
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800819static void
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530820mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
821 int head_pad, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100822{
823 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100824 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800825 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100826 struct mwl8k_dma_data *tr;
827
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100828 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100829 * Add a firmware DMA header; the firmware requires that we
830 * present a 2-byte payload length followed by a 4-address
831 * header (without QoS field), followed (optionally) by any
832 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100833 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100834 wh = (struct ieee80211_hdr *)skb->data;
835
836 hdrlen = ieee80211_hdrlen(wh->frame_control);
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530837
838 /*
839 * Check if skb_resize is required because of
840 * tx_headroom adjustment.
841 */
842 if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
843 + REDUCED_TX_HEADROOM))) {
844 if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
845
846 wiphy_err(priv->hw->wiphy,
847 "Failed to reallocate TX buffer\n");
848 return;
849 }
850 skb->truesize += REDUCED_TX_HEADROOM;
851 }
852
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530853 reqd_hdrlen = sizeof(*tr) + head_pad;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800854
855 if (hdrlen != reqd_hdrlen)
856 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100857
858 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800859 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100860
861 tr = (struct mwl8k_dma_data *)skb->data;
862 if (wh != &tr->wh)
863 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100864 if (hdrlen != sizeof(tr->wh))
865 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100866
867 /*
868 * Firmware length is the length of the fully formed "802.11
869 * payload". That is, everything except for the 802.11 header.
870 * This includes all crypto material including the MIC.
871 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800872 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100873}
874
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530875static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
876 struct sk_buff *skb)
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800877{
878 struct ieee80211_hdr *wh;
879 struct ieee80211_tx_info *tx_info;
880 struct ieee80211_key_conf *key_conf;
881 int data_pad;
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530882 int head_pad = 0;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800883
884 wh = (struct ieee80211_hdr *)skb->data;
885
886 tx_info = IEEE80211_SKB_CB(skb);
887
888 key_conf = NULL;
889 if (ieee80211_is_data(wh->frame_control))
890 key_conf = tx_info->control.hw_key;
891
892 /*
893 * Make sure the packet header is in the DMA header format (4-address
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530894 * without QoS), and add head & tail padding when HW crypto is enabled.
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800895 *
896 * We have the following trailer padding requirements:
897 * - WEP: 4 trailer bytes (ICV)
898 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
899 * - CCMP: 8 trailer bytes (MIC)
900 */
901 data_pad = 0;
902 if (key_conf != NULL) {
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530903 head_pad = key_conf->iv_len;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800904 switch (key_conf->cipher) {
905 case WLAN_CIPHER_SUITE_WEP40:
906 case WLAN_CIPHER_SUITE_WEP104:
907 data_pad = 4;
908 break;
909 case WLAN_CIPHER_SUITE_TKIP:
910 data_pad = 12;
911 break;
912 case WLAN_CIPHER_SUITE_CCMP:
913 data_pad = 8;
914 break;
915 }
916 }
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530917 mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800918}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919
920/*
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530921 * Packet reception for 88w8366/88w8764 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200922 */
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530923struct mwl8k_rxd_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200924 __le16 pkt_len;
925 __u8 sq2;
926 __u8 rate;
927 __le32 pkt_phys_addr;
928 __le32 next_rxd_phys_addr;
929 __le16 qos_control;
930 __le16 htsig2;
931 __le32 hw_rssi_info;
932 __le32 hw_noise_floor_info;
933 __u8 noise_floor;
934 __u8 pad0[3];
935 __u8 rssi;
936 __u8 rx_status;
937 __u8 channel;
938 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000939} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200940
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530941#define MWL8K_AP_RATE_INFO_MCS_FORMAT 0x80
942#define MWL8K_AP_RATE_INFO_40MHZ 0x40
943#define MWL8K_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100944
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530945#define MWL8K_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200946
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530947/* 8366/8764 AP rx_status bits */
948#define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
949#define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
950#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
951#define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
952#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800953
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530954static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200955{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530956 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200957
958 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530959 rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200960}
961
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530962static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200963{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530964 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200965
966 rxd->pkt_len = cpu_to_le16(len);
967 rxd->pkt_phys_addr = cpu_to_le32(addr);
968 wmb();
969 rxd->rx_ctrl = 0;
970}
971
972static int
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530973mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
974 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200975{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530976 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200977
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530978 if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200979 return -1;
980 rmb();
981
982 memset(status, 0, sizeof(*status));
983
984 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400985 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200986
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530987 if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200988 status->flag |= RX_FLAG_HT;
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530989 if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100990 status->flag |= RX_FLAG_40MHZ;
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530991 status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200992 } else {
993 int i;
994
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100995 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
996 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200997 status->rate_idx = i;
998 break;
999 }
1000 }
1001 }
1002
Lennert Buytenhek85478342010-01-12 13:48:56 +01001003 if (rxd->channel > 14) {
1004 status->band = IEEE80211_BAND_5GHZ;
1005 if (!(status->flag & RX_FLAG_HT))
1006 status->rate_idx -= 5;
1007 } else {
1008 status->band = IEEE80211_BAND_2GHZ;
1009 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001010 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1011 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001012
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001013 *qos = rxd->qos_control;
1014
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05301015 if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1016 (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1017 (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001018 status->flag |= RX_FLAG_MMIC_ERROR;
1019
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001020 return le16_to_cpu(rxd->pkt_len);
1021}
1022
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05301023static struct rxd_ops rxd_ap_ops = {
1024 .rxd_size = sizeof(struct mwl8k_rxd_ap),
1025 .rxd_init = mwl8k_rxd_ap_init,
1026 .rxd_refill = mwl8k_rxd_ap_refill,
1027 .rxd_process = mwl8k_rxd_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001028};
1029
1030/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001031 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001032 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001033struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001034 __le16 pkt_len;
1035 __u8 link_quality;
1036 __u8 noise_level;
1037 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001038 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039 __le16 qos_control;
1040 __le16 rate_info;
1041 __le32 pad0[4];
1042 __u8 rssi;
1043 __u8 channel;
1044 __le16 pad1;
1045 __u8 rx_ctrl;
1046 __u8 rx_status;
1047 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +00001048} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001049
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001050#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
1051#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
1052#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
1053#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
1054#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
1055#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001056
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001057#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001058#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
1059/* ICV=0 or MIC=1 */
1060#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
1061/* Key is uploaded only in failure case */
1062#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001063
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001064static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001065{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001066 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001067
1068 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001069 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001070}
1071
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001072static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001073{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001074 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001075
1076 rxd->pkt_len = cpu_to_le16(len);
1077 rxd->pkt_phys_addr = cpu_to_le32(addr);
1078 wmb();
1079 rxd->rx_ctrl = 0;
1080}
1081
1082static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001083mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -04001084 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001085{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001086 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001087 u16 rate_info;
1088
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001089 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001090 return -1;
1091 rmb();
1092
1093 rate_info = le16_to_cpu(rxd->rate_info);
1094
1095 memset(status, 0, sizeof(*status));
1096
1097 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001098 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001099 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1100 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001101
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001102 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001103 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001104 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001105 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001106 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001107 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001108 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001109 status->flag |= RX_FLAG_HT;
1110
Lennert Buytenhek85478342010-01-12 13:48:56 +01001111 if (rxd->channel > 14) {
1112 status->band = IEEE80211_BAND_5GHZ;
1113 if (!(status->flag & RX_FLAG_HT))
1114 status->rate_idx -= 5;
1115 } else {
1116 status->band = IEEE80211_BAND_2GHZ;
1117 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001118 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1119 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001120
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001121 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001122 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1123 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1124 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001125
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001126 return le16_to_cpu(rxd->pkt_len);
1127}
1128
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001129static struct rxd_ops rxd_sta_ops = {
1130 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1131 .rxd_init = mwl8k_rxd_sta_init,
1132 .rxd_refill = mwl8k_rxd_sta_refill,
1133 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001134};
1135
1136
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001137#define MWL8K_RX_DESCS 256
1138#define MWL8K_RX_MAXSZ 3800
1139
1140static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1141{
1142 struct mwl8k_priv *priv = hw->priv;
1143 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1144 int size;
1145 int i;
1146
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001147 rxq->rxd_count = 0;
1148 rxq->head = 0;
1149 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001150
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001151 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001152
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001153 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1154 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001155 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156 return -ENOMEM;
1157 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001158 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159
Shan Weib9ede5f2011-03-08 11:02:03 +08001160 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001161 if (rxq->buf == NULL) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001162 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163 return -ENOMEM;
1164 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165
1166 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001167 int desc_size;
1168 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001170 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001171
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001172 desc_size = priv->rxd_ops->rxd_size;
1173 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001174
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001175 nexti = i + 1;
1176 if (nexti == MWL8K_RX_DESCS)
1177 nexti = 0;
1178 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1179
1180 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001181 }
1182
1183 return 0;
1184}
1185
1186static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1187{
1188 struct mwl8k_priv *priv = hw->priv;
1189 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1190 int refilled;
1191
1192 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001193 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001194 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001195 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001197 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001198
1199 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1200 if (skb == NULL)
1201 break;
1202
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001203 addr = pci_map_single(priv->pdev, skb->data,
1204 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001206 rxq->rxd_count++;
1207 rx = rxq->tail++;
1208 if (rxq->tail == MWL8K_RX_DESCS)
1209 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001210 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e12010-04-02 13:10:38 +09001211 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001212
1213 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1214 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001215
1216 refilled++;
1217 }
1218
1219 return refilled;
1220}
1221
1222/* Must be called only when the card's reception is completely halted */
1223static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1224{
1225 struct mwl8k_priv *priv = hw->priv;
1226 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1227 int i;
1228
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001229 if (rxq->rxd == NULL)
1230 return;
1231
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001232 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001233 if (rxq->buf[i].skb != NULL) {
1234 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e12010-04-02 13:10:38 +09001235 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001236 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e12010-04-02 13:10:38 +09001237 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001238
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001239 kfree_skb(rxq->buf[i].skb);
1240 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001241 }
1242 }
1243
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001244 kfree(rxq->buf);
1245 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001246
1247 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001248 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001249 rxq->rxd, rxq->rxd_dma);
1250 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001251}
1252
1253
1254/*
1255 * Scan a list of BSSIDs to process for finalize join.
1256 * Allows for extension to process multiple BSSIDs.
1257 */
1258static inline int
1259mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1260{
1261 return priv->capture_beacon &&
1262 ieee80211_is_beacon(wh->frame_control) &&
Joe Perches2e42e472012-05-09 17:17:46 +00001263 ether_addr_equal(wh->addr3, priv->capture_bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001264}
1265
Lennert Buytenhek37797522009-10-22 20:19:45 +02001266static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1267 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001268{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001269 struct mwl8k_priv *priv = hw->priv;
1270
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001271 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001272 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001273
1274 /*
1275 * Use GFP_ATOMIC as rxq_process is called from
1276 * the primary interrupt handler, memory allocation call
1277 * must not sleep.
1278 */
1279 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1280 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001281 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001282}
1283
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001284static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1285 u8 *bssid)
1286{
1287 struct mwl8k_vif *mwl8k_vif;
1288
1289 list_for_each_entry(mwl8k_vif,
1290 vif_list, list) {
1291 if (memcmp(bssid, mwl8k_vif->bssid,
1292 ETH_ALEN) == 0)
1293 return mwl8k_vif;
1294 }
1295
1296 return NULL;
1297}
1298
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1300{
1301 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001302 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1304 int processed;
1305
1306 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001307 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001308 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001309 void *rxd;
1310 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001311 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001312 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001313 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001314
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001315 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001316 if (skb == NULL)
1317 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001318
1319 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1320
John W. Linville0d462bb2010-07-28 14:04:24 -04001321 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1322 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001323 if (pkt_len < 0)
1324 break;
1325
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001326 rxq->buf[rxq->head].skb = NULL;
1327
1328 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e12010-04-02 13:10:38 +09001329 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001330 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e12010-04-02 13:10:38 +09001331 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001332
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001333 rxq->head++;
1334 if (rxq->head == MWL8K_RX_DESCS)
1335 rxq->head = 0;
1336
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001337 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001339 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001342 * Check for a pending join operation. Save a
1343 * copy of the beacon and schedule a tasklet to
1344 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001346 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001347 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001349 if (ieee80211_has_protected(wh->frame_control)) {
1350
1351 /* Check if hw crypto has been enabled for
1352 * this bss. If yes, set the status flags
1353 * accordingly
1354 */
1355 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1356 wh->addr1);
1357
1358 if (mwl8k_vif != NULL &&
Joe Perches23677ce2012-02-09 11:17:23 +00001359 mwl8k_vif->is_hw_crypto_enabled) {
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001360 /*
1361 * When MMIC ERROR is encountered
1362 * by the firmware, payload is
1363 * dropped and only 32 bytes of
1364 * mwl8k Firmware header is sent
1365 * to the host.
1366 *
1367 * We need to add four bytes of
1368 * key information. In it
1369 * MAC80211 expects keyidx set to
1370 * 0 for triggering Counter
1371 * Measure of MMIC failure.
1372 */
1373 if (status.flag & RX_FLAG_MMIC_ERROR) {
1374 struct mwl8k_dma_data *tr;
1375 tr = (struct mwl8k_dma_data *)skb->data;
1376 memset((void *)&(tr->data), 0, 4);
1377 pkt_len += 4;
1378 }
1379
1380 if (!ieee80211_is_auth(wh->frame_control))
1381 status.flag |= RX_FLAG_IV_STRIPPED |
1382 RX_FLAG_DECRYPTED |
1383 RX_FLAG_MMIC_STRIPPED;
1384 }
1385 }
1386
1387 skb_put(skb, pkt_len);
1388 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001389 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1390 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001391
1392 processed++;
1393 }
1394
1395 return processed;
1396}
1397
1398
1399/*
1400 * Packet transmission.
1401 */
1402
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001403#define MWL8K_TXD_STATUS_OK 0x00000001
1404#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1405#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1406#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001408
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001409#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1410#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1411#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1412#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1413#define MWL8K_QOS_EOSP 0x0010
1414
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001415struct mwl8k_tx_desc {
1416 __le32 status;
1417 __u8 data_rate;
1418 __u8 tx_priority;
1419 __le16 qos_control;
1420 __le32 pkt_phys_addr;
1421 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001422 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001423 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001424 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001425 __le16 rate_info;
1426 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001427 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001428} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001429
1430#define MWL8K_TX_DESCS 128
1431
1432static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1433{
1434 struct mwl8k_priv *priv = hw->priv;
1435 struct mwl8k_tx_queue *txq = priv->txq + index;
1436 int size;
1437 int i;
1438
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001439 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001440 txq->head = 0;
1441 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001442
1443 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1444
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001445 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1446 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001447 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001448 return -ENOMEM;
1449 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001450 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001451
Shan Weib9ede5f2011-03-08 11:02:03 +08001452 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001453 if (txq->skb == NULL) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001454 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001455 return -ENOMEM;
1456 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457
1458 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1459 struct mwl8k_tx_desc *tx_desc;
1460 int nexti;
1461
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001462 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001463 nexti = (i + 1) % MWL8K_TX_DESCS;
1464
1465 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001466 tx_desc->next_txd_phys_addr =
1467 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001468 }
1469
1470 return 0;
1471}
1472
1473static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1474{
1475 iowrite32(MWL8K_H2A_INT_PPA_READY,
1476 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1477 iowrite32(MWL8K_H2A_INT_DUMMY,
1478 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1479 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1480}
1481
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001482static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001483{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001484 struct mwl8k_priv *priv = hw->priv;
1485 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001486
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001487 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001488 struct mwl8k_tx_queue *txq = priv->txq + i;
1489 int fw_owned = 0;
1490 int drv_owned = 0;
1491 int unused = 0;
1492 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001493
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001494 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001495 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1496 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001497
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001498 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001499 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001500 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001501 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001502 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001503
1504 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001505 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001506 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001507
Joe Perchesc96c31e2010-07-26 14:39:58 -07001508 wiphy_err(hw->wiphy,
1509 "txq[%d] len=%d head=%d tail=%d "
1510 "fw_owned=%d drv_owned=%d unused=%d\n",
1511 i,
1512 txq->len, txq->head, txq->tail,
1513 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001514 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001515}
1516
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001517/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001518 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001519 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001520#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001521
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001522static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001523{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001524 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001525 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001526 int retry;
1527 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001528
1529 might_sleep();
1530
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301531 /* Since fw restart is in progress, allow only the firmware
1532 * commands from the restart code and block the other
1533 * commands since they are going to fail in any case since
1534 * the firmware has crashed
1535 */
1536 if (priv->hw_restart_in_progress) {
1537 if (priv->hw_restart_owner == current)
1538 return 0;
1539 else
1540 return -EBUSY;
1541 }
1542
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05301543 if (atomic_read(&priv->watchdog_event_pending))
1544 return 0;
1545
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001546 /*
1547 * The TX queues are stopped at this point, so this test
1548 * doesn't need to take ->tx_lock.
1549 */
1550 if (!priv->pending_tx_pkts)
1551 return 0;
1552
1553 retry = 0;
1554 rc = 0;
1555
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001556 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001557 priv->tx_wait = &tx_wait;
1558 while (!rc) {
1559 int oldcount;
1560 unsigned long timeout;
1561
1562 oldcount = priv->pending_tx_pkts;
1563
1564 spin_unlock_bh(&priv->tx_lock);
1565 timeout = wait_for_completion_timeout(&tx_wait,
1566 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05301567
1568 if (atomic_read(&priv->watchdog_event_pending)) {
1569 spin_lock_bh(&priv->tx_lock);
1570 priv->tx_wait = NULL;
1571 spin_unlock_bh(&priv->tx_lock);
1572 return 0;
1573 }
1574
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001575 spin_lock_bh(&priv->tx_lock);
1576
1577 if (timeout) {
1578 WARN_ON(priv->pending_tx_pkts);
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301579 if (retry)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001580 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001581 break;
1582 }
1583
1584 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001585 wiphy_notice(hw->wiphy,
1586 "waiting for tx rings to drain (%d -> %d pkts)\n",
1587 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001588 retry = 1;
1589 continue;
1590 }
1591
1592 priv->tx_wait = NULL;
1593
Joe Perchesc96c31e2010-07-26 14:39:58 -07001594 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1595 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001596 mwl8k_dump_tx_rings(hw);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301597 priv->hw_restart_in_progress = true;
1598 ieee80211_queue_work(hw, &priv->fw_reload);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001599
1600 rc = -ETIMEDOUT;
1601 }
Nishant Sarmukadam9b0b11f2013-01-08 10:16:05 +05301602 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001603 spin_unlock_bh(&priv->tx_lock);
1604
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001605 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001606}
1607
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001608#define MWL8K_TXD_SUCCESS(status) \
1609 ((status) & (MWL8K_TXD_STATUS_OK | \
1610 MWL8K_TXD_STATUS_OK_RETRY | \
1611 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001612
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001613static int mwl8k_tid_queue_mapping(u8 tid)
1614{
1615 BUG_ON(tid > 7);
1616
1617 switch (tid) {
1618 case 0:
1619 case 3:
1620 return IEEE80211_AC_BE;
1621 break;
1622 case 1:
1623 case 2:
1624 return IEEE80211_AC_BK;
1625 break;
1626 case 4:
1627 case 5:
1628 return IEEE80211_AC_VI;
1629 break;
1630 case 6:
1631 case 7:
1632 return IEEE80211_AC_VO;
1633 break;
1634 default:
1635 return -1;
1636 break;
1637 }
1638}
1639
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001640/* The firmware will fill in the rate information
1641 * for each packet that gets queued in the hardware
John W. Linville49adc5c2011-04-27 15:04:28 -04001642 * and these macros will interpret that info.
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001643 */
1644
John W. Linville49adc5c2011-04-27 15:04:28 -04001645#define RI_FORMAT(a) (a & 0x0001)
1646#define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001647
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001648static int
1649mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001650{
1651 struct mwl8k_priv *priv = hw->priv;
1652 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001653 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001654
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001655 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001656 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001658 struct mwl8k_tx_desc *tx_desc;
1659 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001660 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001661 struct sk_buff *skb;
1662 struct ieee80211_tx_info *info;
1663 u32 status;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001664 struct ieee80211_sta *sta;
1665 struct mwl8k_sta *sta_info = NULL;
1666 u16 rate_info;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001667 struct ieee80211_hdr *wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001668
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001669 tx = txq->head;
1670 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001671
1672 status = le32_to_cpu(tx_desc->status);
1673
1674 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1675 if (!force)
1676 break;
1677 tx_desc->status &=
1678 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1679 }
1680
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001681 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001682 BUG_ON(txq->len == 0);
1683 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001684 priv->pending_tx_pkts--;
1685
1686 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001687 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001688 skb = txq->skb[tx];
1689 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001690
1691 BUG_ON(skb == NULL);
1692 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1693
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001694 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001695
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001696 wh = (struct ieee80211_hdr *) skb->data;
1697
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001698 /* Mark descriptor as unused */
1699 tx_desc->pkt_phys_addr = 0;
1700 tx_desc->pkt_len = 0;
1701
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001702 info = IEEE80211_SKB_CB(skb);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001703 if (ieee80211_is_data(wh->frame_control)) {
Thomas Huehn89e11802012-07-11 13:21:41 +02001704 rcu_read_lock();
1705 sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1706 wh->addr2);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001707 if (sta) {
1708 sta_info = MWL8K_STA(sta);
1709 BUG_ON(sta_info == NULL);
1710 rate_info = le16_to_cpu(tx_desc->rate_info);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001711 /* If rate is < 6.5 Mpbs for an ht station
1712 * do not form an ampdu. If the station is a
1713 * legacy station (format = 0), do not form an
1714 * ampdu
1715 */
John W. Linville49adc5c2011-04-27 15:04:28 -04001716 if (RI_RATE_ID_MCS(rate_info) < 1 ||
1717 RI_FORMAT(rate_info) == 0) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001718 sta_info->is_ampdu_allowed = false;
1719 } else {
1720 sta_info->is_ampdu_allowed = true;
1721 }
1722 }
Thomas Huehn89e11802012-07-11 13:21:41 +02001723 rcu_read_unlock();
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001724 }
1725
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001726 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001727
1728 /* Rate control is happening in the firmware.
1729 * Ensure no tx rate is being reported.
1730 */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301731 info->status.rates[0].idx = -1;
1732 info->status.rates[0].count = 1;
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001733
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001734 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001735 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001736
1737 ieee80211_tx_status_irqsafe(hw, skb);
1738
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001739 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001740 }
1741
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001742 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001743}
1744
1745/* must be called only when the card's transmit is completely halted */
1746static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1747{
1748 struct mwl8k_priv *priv = hw->priv;
1749 struct mwl8k_tx_queue *txq = priv->txq + index;
1750
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001751 if (txq->txd == NULL)
1752 return;
1753
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001754 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001755
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001756 kfree(txq->skb);
1757 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001758
1759 pci_free_consistent(priv->pdev,
1760 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001761 txq->txd, txq->txd_dma);
1762 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001763}
1764
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001765/* caller must hold priv->stream_lock when calling the stream functions */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301766static struct mwl8k_ampdu_stream *
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001767mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1768{
1769 struct mwl8k_ampdu_stream *stream;
1770 struct mwl8k_priv *priv = hw->priv;
1771 int i;
1772
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301773 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001774 stream = &priv->ampdu[i];
1775 if (stream->state == AMPDU_NO_STREAM) {
1776 stream->sta = sta;
1777 stream->state = AMPDU_STREAM_NEW;
1778 stream->tid = tid;
1779 stream->idx = i;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001780 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1781 sta->addr, tid);
1782 return stream;
1783 }
1784 }
1785 return NULL;
1786}
1787
1788static int
1789mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1790{
1791 int ret;
1792
1793 /* if the stream has already been started, don't start it again */
1794 if (stream->state != AMPDU_STREAM_NEW)
1795 return 0;
1796 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1797 if (ret)
1798 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1799 "%d\n", stream->sta->addr, stream->tid, ret);
1800 else
1801 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1802 stream->sta->addr, stream->tid);
1803 return ret;
1804}
1805
1806static void
1807mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1808{
1809 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1810 stream->tid);
1811 memset(stream, 0, sizeof(*stream));
1812}
1813
1814static struct mwl8k_ampdu_stream *
1815mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1816{
1817 struct mwl8k_priv *priv = hw->priv;
1818 int i;
1819
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301820 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001821 struct mwl8k_ampdu_stream *stream;
1822 stream = &priv->ampdu[i];
1823 if (stream->state == AMPDU_NO_STREAM)
1824 continue;
1825 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1826 stream->tid == tid)
1827 return stream;
1828 }
1829 return NULL;
1830}
1831
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001832#define MWL8K_AMPDU_PACKET_THRESHOLD 64
1833static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1834{
1835 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1836 struct tx_traffic_info *tx_stats;
1837
1838 BUG_ON(tid >= MWL8K_MAX_TID);
1839 tx_stats = &sta_info->tx_stats[tid];
1840
1841 return sta_info->is_ampdu_allowed &&
1842 tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1843}
1844
1845static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1846{
1847 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1848 struct tx_traffic_info *tx_stats;
1849
1850 BUG_ON(tid >= MWL8K_MAX_TID);
1851 tx_stats = &sta_info->tx_stats[tid];
1852
1853 if (tx_stats->start_time == 0)
1854 tx_stats->start_time = jiffies;
1855
1856 /* reset the packet count after each second elapses. If the number of
1857 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1858 * an ampdu stream to be started.
1859 */
1860 if (jiffies - tx_stats->start_time > HZ) {
1861 tx_stats->pkts = 0;
1862 tx_stats->start_time = 0;
1863 } else
1864 tx_stats->pkts++;
1865}
1866
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301867/* The hardware ampdu queues start from 5.
1868 * txpriorities for ampdu queues are
1869 * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1870 * and queue 3 is lowest (queue 4 is reserved)
1871 */
1872#define BA_QUEUE 5
1873
Johannes Berg7bb45682011-02-24 14:42:06 +01001874static void
Thomas Huehn36323f82012-07-23 21:33:42 +02001875mwl8k_txq_xmit(struct ieee80211_hw *hw,
1876 int index,
1877 struct ieee80211_sta *sta,
1878 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001879{
1880 struct mwl8k_priv *priv = hw->priv;
1881 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001882 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001883 struct ieee80211_hdr *wh;
1884 struct mwl8k_tx_queue *txq;
1885 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001886 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001887 u32 txstatus;
1888 u8 txdatarate;
1889 u16 qos;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001890 int txpriority;
1891 u8 tid = 0;
1892 struct mwl8k_ampdu_stream *stream = NULL;
1893 bool start_ba_session = false;
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301894 bool mgmtframe = false;
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001895 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301896 bool eapol_frame = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001897
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001898 wh = (struct ieee80211_hdr *)skb->data;
1899 if (ieee80211_is_data_qos(wh->frame_control))
1900 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1901 else
1902 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001903
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301904 if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1905 eapol_frame = true;
1906
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301907 if (ieee80211_is_mgmt(wh->frame_control))
1908 mgmtframe = true;
1909
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001910 if (priv->ap_fw)
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05301911 mwl8k_encapsulate_tx_frame(priv, skb);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001912 else
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +05301913 mwl8k_add_dma_header(priv, skb, 0, 0);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001914
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001915 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001916
1917 tx_info = IEEE80211_SKB_CB(skb);
1918 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919
1920 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001921 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001922 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1923 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001924 }
1925
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001926 /* Setup firmware control bit fields for each frame type. */
1927 txstatus = 0;
1928 txdatarate = 0;
1929 if (ieee80211_is_mgmt(wh->frame_control) ||
1930 ieee80211_is_ctl(wh->frame_control)) {
1931 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001932 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001933 } else if (ieee80211_is_data(wh->frame_control)) {
1934 txdatarate = 1;
1935 if (is_multicast_ether_addr(wh->addr1))
1936 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1937
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001938 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001939 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001940 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001941 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001942 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001943 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001944
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001945 /* Queue ADDBA request in the respective data queue. While setting up
1946 * the ampdu stream, mac80211 queues further packets for that
1947 * particular ra/tid pair. However, packets piled up in the hardware
1948 * for that ra/tid pair will still go out. ADDBA request and the
1949 * related data packets going out from different queues asynchronously
1950 * will cause a shift in the receiver window which might result in
1951 * ampdu packets getting dropped at the receiver after the stream has
1952 * been setup.
1953 */
1954 if (unlikely(ieee80211_is_action(wh->frame_control) &&
1955 mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1956 mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1957 priv->ap_fw)) {
1958 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1959 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1960 index = mwl8k_tid_queue_mapping(tid);
1961 }
1962
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001963 txpriority = index;
1964
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301965 if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
1966 ieee80211_is_data_qos(wh->frame_control)) {
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001967 tid = qos & 0xf;
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001968 mwl8k_tx_count_packet(sta, tid);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001969 spin_lock(&priv->stream_lock);
1970 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1971 if (stream != NULL) {
1972 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05301973 WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301974 txpriority = (BA_QUEUE + stream->idx) %
1975 TOTAL_HW_TX_QUEUES;
1976 if (stream->idx <= 1)
1977 index = stream->idx +
1978 MWL8K_TX_WMM_QUEUES;
1979
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001980 } else if (stream->state == AMPDU_STREAM_NEW) {
1981 /* We get here if the driver sends us packets
1982 * after we've initiated a stream, but before
1983 * our ampdu_action routine has been called
1984 * with IEEE80211_AMPDU_TX_START to get the SSN
1985 * for the ADDBA request. So this packet can
1986 * go out with no risk of sequence number
1987 * mismatch. No special handling is required.
1988 */
1989 } else {
1990 /* Drop packets that would go out after the
1991 * ADDBA request was sent but before the ADDBA
1992 * response is received. If we don't do this,
1993 * the recipient would probably receive it
1994 * after the ADDBA request with SSN 0. This
1995 * will cause the recipient's BA receive window
1996 * to shift, which would cause the subsequent
1997 * packets in the BA stream to be discarded.
1998 * mac80211 queues our packets for us in this
1999 * case, so this is really just a safety check.
2000 */
2001 wiphy_warn(hw->wiphy,
2002 "Cannot send packet while ADDBA "
2003 "dialog is underway.\n");
2004 spin_unlock(&priv->stream_lock);
2005 dev_kfree_skb(skb);
2006 return;
2007 }
2008 } else {
2009 /* Defer calling mwl8k_start_stream so that the current
2010 * skb can go out before the ADDBA request. This
2011 * prevents sequence number mismatch at the recepient
2012 * as described above.
2013 */
Brian Cavagnolod0805c12011-04-12 11:06:28 -07002014 if (mwl8k_ampdu_allowed(sta, tid)) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07002015 stream = mwl8k_add_stream(hw, sta, tid);
2016 if (stream != NULL)
2017 start_ba_session = true;
2018 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002019 }
2020 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05302021 } else {
2022 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2023 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002024 }
2025
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002026 dma = pci_map_single(priv->pdev, skb->data,
2027 skb->len, PCI_DMA_TODEVICE);
2028
2029 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07002030 wiphy_debug(hw->wiphy,
2031 "failed to dma map skb, dropping TX frame.\n");
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002032 if (start_ba_session) {
2033 spin_lock(&priv->stream_lock);
2034 mwl8k_remove_stream(hw, stream);
2035 spin_unlock(&priv->stream_lock);
2036 }
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002037 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01002038 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002039 }
2040
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002041 spin_lock_bh(&priv->tx_lock);
2042
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002043 txq = priv->txq + index;
2044
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302045 /* Mgmt frames that go out frequently are probe
2046 * responses. Other mgmt frames got out relatively
2047 * infrequently. Hence reserve 2 buffers so that
2048 * other mgmt frames do not get dropped due to an
2049 * already queued probe response in one of the
2050 * reserved buffers.
2051 */
2052
2053 if (txq->len >= MWL8K_TX_DESCS - 2) {
Joe Perches23677ce2012-02-09 11:17:23 +00002054 if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302055 if (start_ba_session) {
2056 spin_lock(&priv->stream_lock);
2057 mwl8k_remove_stream(hw, stream);
2058 spin_unlock(&priv->stream_lock);
2059 }
2060 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadamff7aa962012-11-06 19:22:48 +05302061 pci_unmap_single(priv->pdev, dma, skb->len,
2062 PCI_DMA_TODEVICE);
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302063 dev_kfree_skb(skb);
2064 return;
Pradeep Nemavat3a7dbc32011-04-21 16:34:56 +05302065 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002066 }
2067
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002068 BUG_ON(txq->skb[txq->tail] != NULL);
2069 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002070
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002071 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002072 tx->data_rate = txdatarate;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002073 tx->tx_priority = txpriority;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002074 tx->qos_control = cpu_to_le16(qos);
2075 tx->pkt_phys_addr = cpu_to_le32(dma);
2076 tx->pkt_len = cpu_to_le16(skb->len);
2077 tx->rate_info = 0;
Thomas Huehn36323f82012-07-23 21:33:42 +02002078 if (!priv->ap_fw && sta != NULL)
2079 tx->peer_id = MWL8K_STA(sta)->peer_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002080 else
2081 tx->peer_id = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302082
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05302083 if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302084 tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2085 MWL8K_HW_TIMER_REGISTER));
Nishant Sarmukadamb8d9e572012-11-06 19:23:15 +05302086 else
2087 tx->timestamp = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302088
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002089 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002090 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2091
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02002092 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002093 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002094
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002095 txq->tail++;
2096 if (txq->tail == MWL8K_TX_DESCS)
2097 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002098
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002099 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002100
2101 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002102
2103 /* Initiate the ampdu session here */
2104 if (start_ba_session) {
2105 spin_lock(&priv->stream_lock);
2106 if (mwl8k_start_stream(hw, stream))
2107 mwl8k_remove_stream(hw, stream);
2108 spin_unlock(&priv->stream_lock);
2109 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002110}
2111
2112
2113/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002114 * Firmware access.
2115 *
2116 * We have the following requirements for issuing firmware commands:
2117 * - Some commands require that the packet transmit path is idle when
2118 * the command is issued. (For simplicity, we'll just quiesce the
2119 * transmit path for every command.)
2120 * - There are certain sequences of commands that need to be issued to
2121 * the hardware sequentially, with no other intervening commands.
2122 *
2123 * This leads to an implementation of a "firmware lock" as a mutex that
2124 * can be taken recursively, and which is taken by both the low-level
2125 * command submission function (mwl8k_post_cmd) as well as any users of
2126 * that function that require issuing of an atomic sequence of commands,
2127 * and quiesces the transmit path whenever it's taken.
2128 */
2129static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2130{
2131 struct mwl8k_priv *priv = hw->priv;
2132
2133 if (priv->fw_mutex_owner != current) {
2134 int rc;
2135
2136 mutex_lock(&priv->fw_mutex);
2137 ieee80211_stop_queues(hw);
2138
2139 rc = mwl8k_tx_wait_empty(hw);
2140 if (rc) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302141 if (!priv->hw_restart_in_progress)
2142 ieee80211_wake_queues(hw);
2143
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002144 mutex_unlock(&priv->fw_mutex);
2145
2146 return rc;
2147 }
2148
2149 priv->fw_mutex_owner = current;
2150 }
2151
2152 priv->fw_mutex_depth++;
2153
2154 return 0;
2155}
2156
2157static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2158{
2159 struct mwl8k_priv *priv = hw->priv;
2160
2161 if (!--priv->fw_mutex_depth) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302162 if (!priv->hw_restart_in_progress)
2163 ieee80211_wake_queues(hw);
2164
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002165 priv->fw_mutex_owner = NULL;
2166 mutex_unlock(&priv->fw_mutex);
2167 }
2168}
2169
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302170static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2171 u32 bitmap);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002172
2173/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002174 * Command processing.
2175 */
2176
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01002177/* Timeout firmware commands after 10s */
2178#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002179
2180static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
2181{
2182 DECLARE_COMPLETION_ONSTACK(cmd_wait);
2183 struct mwl8k_priv *priv = hw->priv;
2184 void __iomem *regs = priv->regs;
2185 dma_addr_t dma_addr;
2186 unsigned int dma_size;
2187 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002188 unsigned long timeout = 0;
2189 u8 buf[32];
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302190 u32 bitmap = 0;
2191
2192 wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2193 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2194
2195 /* Before posting firmware commands that could change the hardware
2196 * characteristics, make sure that all BSSes are stopped temporary.
2197 * Enable these stopped BSSes after completion of the commands
2198 */
2199
2200 rc = mwl8k_fw_lock(hw);
2201 if (rc)
2202 return rc;
2203
2204 if (priv->ap_fw && priv->running_bsses) {
2205 switch (le16_to_cpu(cmd->code)) {
2206 case MWL8K_CMD_SET_RF_CHANNEL:
2207 case MWL8K_CMD_RADIO_CONTROL:
2208 case MWL8K_CMD_RF_TX_POWER:
2209 case MWL8K_CMD_TX_POWER:
2210 case MWL8K_CMD_RF_ANTENNA:
2211 case MWL8K_CMD_RTS_THRESHOLD:
2212 case MWL8K_CMD_MIMO_CONFIG:
2213 bitmap = priv->running_bsses;
2214 mwl8k_enable_bsses(hw, false, bitmap);
2215 break;
2216 }
2217 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002218
John W. Linvilleb6037422010-07-20 13:55:00 -04002219 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002220 dma_size = le16_to_cpu(cmd->length);
2221 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
2222 PCI_DMA_BIDIRECTIONAL);
2223 if (pci_dma_mapping_error(priv->pdev, dma_addr))
2224 return -ENOMEM;
2225
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002226 priv->hostcmd_wait = &cmd_wait;
2227 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2228 iowrite32(MWL8K_H2A_INT_DOORBELL,
2229 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2230 iowrite32(MWL8K_H2A_INT_DUMMY,
2231 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002232
2233 timeout = wait_for_completion_timeout(&cmd_wait,
2234 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2235
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002236 priv->hostcmd_wait = NULL;
2237
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002238
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02002239 pci_unmap_single(priv->pdev, dma_addr, dma_size,
2240 PCI_DMA_BIDIRECTIONAL);
2241
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002242 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07002243 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002244 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2245 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002246 rc = -ETIMEDOUT;
2247 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01002248 int ms;
2249
2250 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2251
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002252 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002253 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07002254 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002255 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2256 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01002257 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07002258 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002259 mwl8k_cmd_name(cmd->code,
2260 buf, sizeof(buf)),
2261 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002262 }
2263
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302264 if (bitmap)
2265 mwl8k_enable_bsses(hw, true, bitmap);
2266
2267 mwl8k_fw_unlock(hw);
2268
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002269 return rc;
2270}
2271
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01002272static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2273 struct ieee80211_vif *vif,
2274 struct mwl8k_cmd_pkt *cmd)
2275{
2276 if (vif != NULL)
2277 cmd->macid = MWL8K_VIF(vif)->macid;
2278 return mwl8k_post_cmd(hw, cmd);
2279}
2280
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002281/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002282 * Setup code shared between STA and AP firmware images.
2283 */
2284static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2285{
2286 struct mwl8k_priv *priv = hw->priv;
2287
2288 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2289 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2290
2291 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2292 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2293
2294 priv->band_24.band = IEEE80211_BAND_2GHZ;
2295 priv->band_24.channels = priv->channels_24;
2296 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2297 priv->band_24.bitrates = priv->rates_24;
2298 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2299
2300 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2301}
2302
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01002303static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2304{
2305 struct mwl8k_priv *priv = hw->priv;
2306
2307 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2308 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2309
2310 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2311 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2312
2313 priv->band_50.band = IEEE80211_BAND_5GHZ;
2314 priv->band_50.channels = priv->channels_50;
2315 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2316 priv->band_50.bitrates = priv->rates_50;
2317 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2318
2319 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2320}
2321
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002322/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002323 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002324 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002325struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002326 struct mwl8k_cmd_pkt header;
2327 __u8 hw_rev;
2328 __u8 host_interface;
2329 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002330 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002331 __le16 region_code;
2332 __le32 fw_rev;
2333 __le32 ps_cookie;
2334 __le32 caps;
2335 __u8 mcs_bitmap[16];
2336 __le32 rx_queue_ptr;
2337 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002338 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002339 __le32 caps2;
2340 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002341 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002342} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002343
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002344#define MWL8K_CAP_MAX_AMSDU 0x20000000
2345#define MWL8K_CAP_GREENFIELD 0x08000000
2346#define MWL8K_CAP_AMPDU 0x04000000
2347#define MWL8K_CAP_RX_STBC 0x01000000
2348#define MWL8K_CAP_TX_STBC 0x00800000
2349#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2350#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2351#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2352#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2353#define MWL8K_CAP_DELAY_BA 0x00003000
2354#define MWL8K_CAP_MIMO 0x00000200
2355#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01002356#define MWL8K_CAP_BAND_MASK 0x00000007
2357#define MWL8K_CAP_5GHZ 0x00000004
2358#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002359
Lennert Buytenhek06953232010-01-12 13:49:41 +01002360static void
2361mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2362 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002363{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002364 int rx_streams;
2365 int tx_streams;
2366
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002367 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002368
2369 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002370 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002371 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002372 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002373 if (cap & MWL8K_CAP_AMPDU) {
2374 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002375 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2376 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002377 }
2378 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002379 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002380 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002381 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002382 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002383 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002384 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002385 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002386 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002387 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002388 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002389 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002390
2391 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2392 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2393
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002394 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002395 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002396 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002397 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002398 band->ht_cap.mcs.rx_mask[2] = 0xff;
2399 band->ht_cap.mcs.rx_mask[4] = 0x01;
2400 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002401
2402 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002403 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2404 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002405 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2406 }
2407}
2408
Lennert Buytenhek06953232010-01-12 13:49:41 +01002409static void
2410mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2411{
2412 struct mwl8k_priv *priv = hw->priv;
2413
Jonas Gorskic3f251a2013-03-11 17:44:21 +01002414 if (priv->caps)
2415 return;
2416
Lennert Buytenhek06953232010-01-12 13:49:41 +01002417 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2418 mwl8k_setup_2ghz_band(hw);
2419 if (caps & MWL8K_CAP_MIMO)
2420 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2421 }
2422
2423 if (caps & MWL8K_CAP_5GHZ) {
2424 mwl8k_setup_5ghz_band(hw);
2425 if (caps & MWL8K_CAP_MIMO)
2426 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2427 }
Jonas Gorskic3f251a2013-03-11 17:44:21 +01002428
2429 priv->caps = caps;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002430}
2431
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002432static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002433{
2434 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002435 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002436 int rc;
2437 int i;
2438
2439 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2440 if (cmd == NULL)
2441 return -ENOMEM;
2442
2443 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2444 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2445
2446 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2447 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002448 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002449 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2450 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002451 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002452 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002453 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002454
2455 rc = mwl8k_post_cmd(hw, &cmd->header);
2456
2457 if (!rc) {
2458 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2459 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002460 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002461 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002462 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002463 priv->ap_macids_supported = 0x00000000;
2464 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002465 }
2466
2467 kfree(cmd);
2468 return rc;
2469}
2470
2471/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002472 * CMD_GET_HW_SPEC (AP version).
2473 */
2474struct mwl8k_cmd_get_hw_spec_ap {
2475 struct mwl8k_cmd_pkt header;
2476 __u8 hw_rev;
2477 __u8 host_interface;
2478 __le16 num_wcb;
2479 __le16 num_mcaddrs;
2480 __u8 perm_addr[ETH_ALEN];
2481 __le16 region_code;
2482 __le16 num_antenna;
2483 __le32 fw_rev;
2484 __le32 wcbbase0;
2485 __le32 rxwrptr;
2486 __le32 rxrdptr;
2487 __le32 ps_cookie;
2488 __le32 wcbbase1;
2489 __le32 wcbbase2;
2490 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002491 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002492 __le32 caps;
2493 __le32 num_of_ampdu_queues;
2494 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002495} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002496
2497static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2498{
2499 struct mwl8k_priv *priv = hw->priv;
2500 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002501 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002502 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002503
2504 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2505 if (cmd == NULL)
2506 return -ENOMEM;
2507
2508 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2509 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2510
2511 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2512 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2513
2514 rc = mwl8k_post_cmd(hw, &cmd->header);
2515
2516 if (!rc) {
2517 int off;
2518
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002519 api_version = le32_to_cpu(cmd->fw_api_version);
2520 if (priv->device_info->fw_api_ap != api_version) {
2521 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2522 " Expected %d got %d.\n", MWL8K_NAME,
2523 priv->device_info->part_name,
2524 priv->device_info->fw_api_ap,
2525 api_version);
2526 rc = -EINVAL;
2527 goto done;
2528 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002529 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2530 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2531 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2532 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002533 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002534 priv->ap_macids_supported = 0x000000ff;
Yogesh Ashok Poward59c1cf2013-01-25 16:20:03 +05302535 priv->sta_macids_supported = 0x00000100;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002536 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2537 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2538 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2539 " but we only support %d.\n",
2540 priv->num_ampdu_queues,
2541 MWL8K_MAX_AMPDU_QUEUES);
2542 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2543 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002544 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002545 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002546
2547 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002548 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002549
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002550 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2551 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2552 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2553 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002554
2555 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002556 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002557 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002558 }
2559
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002560done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002561 kfree(cmd);
2562 return rc;
2563}
2564
2565/*
2566 * CMD_SET_HW_SPEC.
2567 */
2568struct mwl8k_cmd_set_hw_spec {
2569 struct mwl8k_cmd_pkt header;
2570 __u8 hw_rev;
2571 __u8 host_interface;
2572 __le16 num_mcaddrs;
2573 __u8 perm_addr[ETH_ALEN];
2574 __le16 region_code;
2575 __le32 fw_rev;
2576 __le32 ps_cookie;
2577 __le32 caps;
2578 __le32 rx_queue_ptr;
2579 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002580 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002581 __le32 flags;
2582 __le32 num_tx_desc_per_queue;
2583 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002584} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002585
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002586/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2587 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2588 * the packets that are queued for more than 500ms, will be dropped in the
2589 * hardware. This helps minimizing the issues caused due to head-of-line
2590 * blocking where a slow client can hog the bandwidth and affect traffic to a
2591 * faster client.
2592 */
2593#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302594#define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002595#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2596#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2597#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002598
2599static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2600{
2601 struct mwl8k_priv *priv = hw->priv;
2602 struct mwl8k_cmd_set_hw_spec *cmd;
2603 int rc;
2604 int i;
2605
2606 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2607 if (cmd == NULL)
2608 return -ENOMEM;
2609
2610 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2611 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2612
2613 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2614 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002615 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002616
2617 /*
2618 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2619 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2620 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2621 * priority is interpreted the right way in firmware.
2622 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002623 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2624 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002625 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2626 }
2627
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002628 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2629 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
Nishant Sarmukadam31d291a2011-04-21 16:34:59 +05302630 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302631 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2632 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002633 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2634 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2635
2636 rc = mwl8k_post_cmd(hw, &cmd->header);
2637 kfree(cmd);
2638
2639 return rc;
2640}
2641
2642/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002643 * CMD_MAC_MULTICAST_ADR.
2644 */
2645struct mwl8k_cmd_mac_multicast_adr {
2646 struct mwl8k_cmd_pkt header;
2647 __le16 action;
2648 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002649 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002650};
2651
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002652#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2653#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2654#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2655#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002656
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002657static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002658__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002659 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002660{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002661 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002662 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002663 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002664 int mc_count = 0;
2665
2666 if (mc_list)
2667 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002668
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002669 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002670 allmulti = 1;
2671 mc_count = 0;
2672 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002673
2674 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2675
2676 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002677 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002678 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002679
2680 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2681 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002682 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2683 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002684
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002685 if (allmulti) {
2686 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2687 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002688 struct netdev_hw_addr *ha;
2689 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002690
2691 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2692 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00002693 netdev_hw_addr_list_for_each(ha, mc_list) {
2694 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002695 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002696 }
2697
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002698 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002699}
2700
2701/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002702 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002703 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002704struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002705 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002706 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002707} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002708
2709#define MWL8K_STAT_ACK_FAILURE 9
2710#define MWL8K_STAT_RTS_FAILURE 12
2711#define MWL8K_STAT_FCS_ERROR 24
2712#define MWL8K_STAT_RTS_SUCCESS 11
2713
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002714static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2715 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002716{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002717 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002718 int rc;
2719
2720 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2721 if (cmd == NULL)
2722 return -ENOMEM;
2723
2724 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2725 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002726
2727 rc = mwl8k_post_cmd(hw, &cmd->header);
2728 if (!rc) {
2729 stats->dot11ACKFailureCount =
2730 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2731 stats->dot11RTSFailureCount =
2732 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2733 stats->dot11FCSErrorCount =
2734 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2735 stats->dot11RTSSuccessCount =
2736 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2737 }
2738 kfree(cmd);
2739
2740 return rc;
2741}
2742
2743/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002744 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002745 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002746struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002747 struct mwl8k_cmd_pkt header;
2748 __le16 action;
2749 __le16 control;
2750 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002751} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002752
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002753static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002754mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002755{
2756 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002757 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002758 int rc;
2759
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002760 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002761 return 0;
2762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002763 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2764 if (cmd == NULL)
2765 return -ENOMEM;
2766
2767 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2768 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2769 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002770 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002771 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2772
2773 rc = mwl8k_post_cmd(hw, &cmd->header);
2774 kfree(cmd);
2775
2776 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002777 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002778
2779 return rc;
2780}
2781
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002782static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002783{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002784 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002785}
2786
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002787static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002788{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002789 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002790}
2791
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002792static int
2793mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2794{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002795 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002796
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002797 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002798
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002799 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002800}
2801
2802/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002803 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002804 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002805#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002806
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002807struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002808 struct mwl8k_cmd_pkt header;
2809 __le16 action;
2810 __le16 support_level;
2811 __le16 current_level;
2812 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002813 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002814} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002815
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002816static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002817{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002818 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819 int rc;
2820
2821 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2822 if (cmd == NULL)
2823 return -ENOMEM;
2824
2825 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2826 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2827 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2828 cmd->support_level = cpu_to_le16(dBm);
2829
2830 rc = mwl8k_post_cmd(hw, &cmd->header);
2831 kfree(cmd);
2832
2833 return rc;
2834}
2835
2836/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002837 * CMD_TX_POWER.
2838 */
2839#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2840
2841struct mwl8k_cmd_tx_power {
2842 struct mwl8k_cmd_pkt header;
2843 __le16 action;
2844 __le16 band;
2845 __le16 channel;
2846 __le16 bw;
2847 __le16 sub_ch;
2848 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05302849} __packed;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002850
2851static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2852 struct ieee80211_conf *conf,
2853 unsigned short pwr)
2854{
Karl Beldan675a0b02013-03-25 16:26:57 +01002855 struct ieee80211_channel *channel = conf->chandef.chan;
2856 enum nl80211_channel_type channel_type =
2857 cfg80211_get_chandef_type(&conf->chandef);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002858 struct mwl8k_cmd_tx_power *cmd;
2859 int rc;
2860 int i;
2861
2862 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2863 if (cmd == NULL)
2864 return -ENOMEM;
2865
2866 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2867 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2868 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2869
2870 if (channel->band == IEEE80211_BAND_2GHZ)
2871 cmd->band = cpu_to_le16(0x1);
2872 else if (channel->band == IEEE80211_BAND_5GHZ)
2873 cmd->band = cpu_to_le16(0x4);
2874
Yogesh Ashok Powar604c4ef2012-01-17 15:45:15 +05302875 cmd->channel = cpu_to_le16(channel->hw_value);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002876
Karl Beldan675a0b02013-03-25 16:26:57 +01002877 if (channel_type == NL80211_CHAN_NO_HT ||
2878 channel_type == NL80211_CHAN_HT20) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002879 cmd->bw = cpu_to_le16(0x2);
2880 } else {
2881 cmd->bw = cpu_to_le16(0x4);
Karl Beldan675a0b02013-03-25 16:26:57 +01002882 if (channel_type == NL80211_CHAN_HT40MINUS)
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002883 cmd->sub_ch = cpu_to_le16(0x3);
Karl Beldan675a0b02013-03-25 16:26:57 +01002884 else if (channel_type == NL80211_CHAN_HT40PLUS)
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002885 cmd->sub_ch = cpu_to_le16(0x1);
2886 }
2887
2888 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2889 cmd->power_level_list[i] = cpu_to_le16(pwr);
2890
2891 rc = mwl8k_post_cmd(hw, &cmd->header);
2892 kfree(cmd);
2893
2894 return rc;
2895}
2896
2897/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002898 * CMD_RF_ANTENNA.
2899 */
2900struct mwl8k_cmd_rf_antenna {
2901 struct mwl8k_cmd_pkt header;
2902 __le16 antenna;
2903 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002904} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002905
2906#define MWL8K_RF_ANTENNA_RX 1
2907#define MWL8K_RF_ANTENNA_TX 2
2908
2909static int
2910mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2911{
2912 struct mwl8k_cmd_rf_antenna *cmd;
2913 int rc;
2914
2915 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2916 if (cmd == NULL)
2917 return -ENOMEM;
2918
2919 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2920 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2921 cmd->antenna = cpu_to_le16(antenna);
2922 cmd->mode = cpu_to_le16(mask);
2923
2924 rc = mwl8k_post_cmd(hw, &cmd->header);
2925 kfree(cmd);
2926
2927 return rc;
2928}
2929
2930/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002931 * CMD_SET_BEACON.
2932 */
2933struct mwl8k_cmd_set_beacon {
2934 struct mwl8k_cmd_pkt header;
2935 __le16 beacon_len;
2936 __u8 beacon[0];
2937};
2938
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002939static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2940 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002941{
2942 struct mwl8k_cmd_set_beacon *cmd;
2943 int rc;
2944
2945 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2946 if (cmd == NULL)
2947 return -ENOMEM;
2948
2949 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2950 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2951 cmd->beacon_len = cpu_to_le16(len);
2952 memcpy(cmd->beacon, beacon, len);
2953
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002954 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002955 kfree(cmd);
2956
2957 return rc;
2958}
2959
2960/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002961 * CMD_SET_PRE_SCAN.
2962 */
2963struct mwl8k_cmd_set_pre_scan {
2964 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002965} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002966
2967static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2968{
2969 struct mwl8k_cmd_set_pre_scan *cmd;
2970 int rc;
2971
2972 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2973 if (cmd == NULL)
2974 return -ENOMEM;
2975
2976 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2977 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2978
2979 rc = mwl8k_post_cmd(hw, &cmd->header);
2980 kfree(cmd);
2981
2982 return rc;
2983}
2984
2985/*
2986 * CMD_SET_POST_SCAN.
2987 */
2988struct mwl8k_cmd_set_post_scan {
2989 struct mwl8k_cmd_pkt header;
2990 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002991 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002992} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002993
2994static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002995mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002996{
2997 struct mwl8k_cmd_set_post_scan *cmd;
2998 int rc;
2999
3000 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3001 if (cmd == NULL)
3002 return -ENOMEM;
3003
3004 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
3005 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3006 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003007 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003008
3009 rc = mwl8k_post_cmd(hw, &cmd->header);
3010 kfree(cmd);
3011
3012 return rc;
3013}
3014
3015/*
3016 * CMD_SET_RF_CHANNEL.
3017 */
3018struct mwl8k_cmd_set_rf_channel {
3019 struct mwl8k_cmd_pkt header;
3020 __le16 action;
3021 __u8 current_channel;
3022 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003023} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003024
3025static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003026 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003027{
Karl Beldan675a0b02013-03-25 16:26:57 +01003028 struct ieee80211_channel *channel = conf->chandef.chan;
3029 enum nl80211_channel_type channel_type =
3030 cfg80211_get_chandef_type(&conf->chandef);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003031 struct mwl8k_cmd_set_rf_channel *cmd;
3032 int rc;
3033
3034 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3035 if (cmd == NULL)
3036 return -ENOMEM;
3037
3038 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3039 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3040 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3041 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003042
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003043 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003044 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01003045 else if (channel->band == IEEE80211_BAND_5GHZ)
3046 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003047
Karl Beldan675a0b02013-03-25 16:26:57 +01003048 if (channel_type == NL80211_CHAN_NO_HT ||
3049 channel_type == NL80211_CHAN_HT20)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003050 cmd->channel_flags |= cpu_to_le32(0x00000080);
Karl Beldan675a0b02013-03-25 16:26:57 +01003051 else if (channel_type == NL80211_CHAN_HT40MINUS)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003052 cmd->channel_flags |= cpu_to_le32(0x000001900);
Karl Beldan675a0b02013-03-25 16:26:57 +01003053 else if (channel_type == NL80211_CHAN_HT40PLUS)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003054 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003055
3056 rc = mwl8k_post_cmd(hw, &cmd->header);
3057 kfree(cmd);
3058
3059 return rc;
3060}
3061
3062/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003063 * CMD_SET_AID.
3064 */
3065#define MWL8K_FRAME_PROT_DISABLED 0x00
3066#define MWL8K_FRAME_PROT_11G 0x07
3067#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
3068#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
3069
3070struct mwl8k_cmd_update_set_aid {
3071 struct mwl8k_cmd_pkt header;
3072 __le16 aid;
3073
3074 /* AP's MAC address (BSSID) */
3075 __u8 bssid[ETH_ALEN];
3076 __le16 protection_mode;
3077 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003078} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003079
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003080static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3081{
3082 int i;
3083 int j;
3084
3085 /*
3086 * Clear nonstandard rates 4 and 13.
3087 */
3088 mask &= 0x1fef;
3089
3090 for (i = 0, j = 0; i < 14; i++) {
3091 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01003092 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003093 }
3094}
3095
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003096static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003097mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3098 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003099{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003100 struct mwl8k_cmd_update_set_aid *cmd;
3101 u16 prot_mode;
3102 int rc;
3103
3104 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3105 if (cmd == NULL)
3106 return -ENOMEM;
3107
3108 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3109 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003110 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003111 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003112
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003113 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003114 prot_mode = MWL8K_FRAME_PROT_11G;
3115 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003116 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003117 IEEE80211_HT_OP_MODE_PROTECTION) {
3118 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3119 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3120 break;
3121 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3122 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3123 break;
3124 default:
3125 prot_mode = MWL8K_FRAME_PROT_DISABLED;
3126 break;
3127 }
3128 }
3129 cmd->protection_mode = cpu_to_le16(prot_mode);
3130
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003131 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003132
3133 rc = mwl8k_post_cmd(hw, &cmd->header);
3134 kfree(cmd);
3135
3136 return rc;
3137}
3138
3139/*
3140 * CMD_SET_RATE.
3141 */
3142struct mwl8k_cmd_set_rate {
3143 struct mwl8k_cmd_pkt header;
3144 __u8 legacy_rates[14];
3145
3146 /* Bitmap for supported MCS codes. */
3147 __u8 mcs_set[16];
3148 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003149} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003150
3151static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003152mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003153 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003154{
3155 struct mwl8k_cmd_set_rate *cmd;
3156 int rc;
3157
3158 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3159 if (cmd == NULL)
3160 return -ENOMEM;
3161
3162 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3163 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003164 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003165 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003166
3167 rc = mwl8k_post_cmd(hw, &cmd->header);
3168 kfree(cmd);
3169
3170 return rc;
3171}
3172
3173/*
3174 * CMD_FINALIZE_JOIN.
3175 */
3176#define MWL8K_FJ_BEACON_MAXLEN 128
3177
3178struct mwl8k_cmd_finalize_join {
3179 struct mwl8k_cmd_pkt header;
3180 __le32 sleep_interval; /* Number of beacon periods to sleep */
3181 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003182} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003183
3184static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3185 int framelen, int dtim)
3186{
3187 struct mwl8k_cmd_finalize_join *cmd;
3188 struct ieee80211_mgmt *payload = frame;
3189 int payload_len;
3190 int rc;
3191
3192 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3193 if (cmd == NULL)
3194 return -ENOMEM;
3195
3196 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3197 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3198 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3199
3200 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3201 if (payload_len < 0)
3202 payload_len = 0;
3203 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3204 payload_len = MWL8K_FJ_BEACON_MAXLEN;
3205
3206 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3207
3208 rc = mwl8k_post_cmd(hw, &cmd->header);
3209 kfree(cmd);
3210
3211 return rc;
3212}
3213
3214/*
3215 * CMD_SET_RTS_THRESHOLD.
3216 */
3217struct mwl8k_cmd_set_rts_threshold {
3218 struct mwl8k_cmd_pkt header;
3219 __le16 action;
3220 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003221} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003222
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003223static int
3224mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003225{
3226 struct mwl8k_cmd_set_rts_threshold *cmd;
3227 int rc;
3228
3229 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3230 if (cmd == NULL)
3231 return -ENOMEM;
3232
3233 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3234 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003235 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3236 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003237
3238 rc = mwl8k_post_cmd(hw, &cmd->header);
3239 kfree(cmd);
3240
3241 return rc;
3242}
3243
3244/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003245 * CMD_SET_SLOT.
3246 */
3247struct mwl8k_cmd_set_slot {
3248 struct mwl8k_cmd_pkt header;
3249 __le16 action;
3250 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003251} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003252
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003253static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003254{
3255 struct mwl8k_cmd_set_slot *cmd;
3256 int rc;
3257
3258 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3259 if (cmd == NULL)
3260 return -ENOMEM;
3261
3262 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3263 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3264 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003265 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003266
3267 rc = mwl8k_post_cmd(hw, &cmd->header);
3268 kfree(cmd);
3269
3270 return rc;
3271}
3272
3273/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003274 * CMD_SET_EDCA_PARAMS.
3275 */
3276struct mwl8k_cmd_set_edca_params {
3277 struct mwl8k_cmd_pkt header;
3278
3279 /* See MWL8K_SET_EDCA_XXX below */
3280 __le16 action;
3281
3282 /* TX opportunity in units of 32 us */
3283 __le16 txop;
3284
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003285 union {
3286 struct {
3287 /* Log exponent of max contention period: 0...15 */
3288 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003289
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003290 /* Log exponent of min contention period: 0...15 */
3291 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003292
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003293 /* Adaptive interframe spacing in units of 32us */
3294 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003295
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003296 /* TX queue to configure */
3297 __u8 txq;
3298 } ap;
3299 struct {
3300 /* Log exponent of max contention period: 0...15 */
3301 __u8 log_cw_max;
3302
3303 /* Log exponent of min contention period: 0...15 */
3304 __u8 log_cw_min;
3305
3306 /* Adaptive interframe spacing in units of 32us */
3307 __u8 aifs;
3308
3309 /* TX queue to configure */
3310 __u8 txq;
3311 } sta;
3312 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003313} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003314
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003315#define MWL8K_SET_EDCA_CW 0x01
3316#define MWL8K_SET_EDCA_TXOP 0x02
3317#define MWL8K_SET_EDCA_AIFS 0x04
3318
3319#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3320 MWL8K_SET_EDCA_TXOP | \
3321 MWL8K_SET_EDCA_AIFS)
3322
3323static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003324mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3325 __u16 cw_min, __u16 cw_max,
3326 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003327{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003328 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003330 int rc;
3331
3332 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3333 if (cmd == NULL)
3334 return -ENOMEM;
3335
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003336 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3337 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003338 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3339 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003340 if (priv->ap_fw) {
3341 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3342 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3343 cmd->ap.aifs = aifs;
3344 cmd->ap.txq = qnum;
3345 } else {
3346 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3347 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3348 cmd->sta.aifs = aifs;
3349 cmd->sta.txq = qnum;
3350 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003351
3352 rc = mwl8k_post_cmd(hw, &cmd->header);
3353 kfree(cmd);
3354
3355 return rc;
3356}
3357
3358/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003359 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003360 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003361struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003363 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003364} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003365
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003366static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003367{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003368 struct mwl8k_priv *priv = hw->priv;
3369 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003370 int rc;
3371
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003372 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3373 if (cmd == NULL)
3374 return -ENOMEM;
3375
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003376 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003377 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003378 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003379
3380 rc = mwl8k_post_cmd(hw, &cmd->header);
3381 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003382
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003383 if (!rc)
3384 priv->wmm_enabled = enable;
3385
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003386 return rc;
3387}
3388
3389/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003390 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003391 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003392struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003393 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003394 __le32 action;
3395 __u8 rx_antenna_map;
3396 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003397} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003398
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003399static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003400{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003401 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003402 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003403
3404 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3405 if (cmd == NULL)
3406 return -ENOMEM;
3407
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003408 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003409 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003410 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3411 cmd->rx_antenna_map = rx;
3412 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003413
3414 rc = mwl8k_post_cmd(hw, &cmd->header);
3415 kfree(cmd);
3416
3417 return rc;
3418}
3419
3420/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003421 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003422 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003423struct mwl8k_cmd_use_fixed_rate_sta {
3424 struct mwl8k_cmd_pkt header;
3425 __le32 action;
3426 __le32 allow_rate_drop;
3427 __le32 num_rates;
3428 struct {
3429 __le32 is_ht_rate;
3430 __le32 enable_retry;
3431 __le32 rate;
3432 __le32 retry_count;
3433 } rate_entry[8];
3434 __le32 rate_type;
3435 __le32 reserved1;
3436 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003437} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003438
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003439#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003440#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003441
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003442static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003443{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003444 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003445 int rc;
3446
3447 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3448 if (cmd == NULL)
3449 return -ENOMEM;
3450
3451 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3452 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003453 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3454 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003455
3456 rc = mwl8k_post_cmd(hw, &cmd->header);
3457 kfree(cmd);
3458
3459 return rc;
3460}
3461
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003462/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003463 * CMD_USE_FIXED_RATE (AP version).
3464 */
3465struct mwl8k_cmd_use_fixed_rate_ap {
3466 struct mwl8k_cmd_pkt header;
3467 __le32 action;
3468 __le32 allow_rate_drop;
3469 __le32 num_rates;
3470 struct mwl8k_rate_entry_ap {
3471 __le32 is_ht_rate;
3472 __le32 enable_retry;
3473 __le32 rate;
3474 __le32 retry_count;
3475 } rate_entry[4];
3476 u8 multicast_rate;
3477 u8 multicast_rate_type;
3478 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003479} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003480
3481static int
3482mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3483{
3484 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3485 int rc;
3486
3487 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3488 if (cmd == NULL)
3489 return -ENOMEM;
3490
3491 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3492 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3493 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3494 cmd->multicast_rate = mcast;
3495 cmd->management_rate = mgmt;
3496
3497 rc = mwl8k_post_cmd(hw, &cmd->header);
3498 kfree(cmd);
3499
3500 return rc;
3501}
3502
3503/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003504 * CMD_ENABLE_SNIFFER.
3505 */
3506struct mwl8k_cmd_enable_sniffer {
3507 struct mwl8k_cmd_pkt header;
3508 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003509} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003510
3511static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3512{
3513 struct mwl8k_cmd_enable_sniffer *cmd;
3514 int rc;
3515
3516 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3517 if (cmd == NULL)
3518 return -ENOMEM;
3519
3520 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3521 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3522 cmd->action = cpu_to_le32(!!enable);
3523
3524 rc = mwl8k_post_cmd(hw, &cmd->header);
3525 kfree(cmd);
3526
3527 return rc;
3528}
3529
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303530struct mwl8k_cmd_update_mac_addr {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003531 struct mwl8k_cmd_pkt header;
3532 union {
3533 struct {
3534 __le16 mac_type;
3535 __u8 mac_addr[ETH_ALEN];
3536 } mbss;
3537 __u8 mac_addr[ETH_ALEN];
3538 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003539} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003540
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003541#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3542#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3543#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3544#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003545
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303546static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3547 struct ieee80211_vif *vif, u8 *mac, bool set)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003548{
3549 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003550 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303551 struct mwl8k_cmd_update_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003552 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003553 int rc;
3554
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003555 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3556 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3557 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
Yogesh Ashok Powaraf458832013-01-25 16:20:12 +05303558 if (priv->ap_fw)
3559 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3560 else
3561 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003562 else
3563 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3564 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3565 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3566 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3567 else
3568 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3569 }
3570
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003571 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3572 if (cmd == NULL)
3573 return -ENOMEM;
3574
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303575 if (set)
3576 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3577 else
3578 cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3579
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003580 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3581 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003582 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003583 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3584 } else {
3585 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3586 }
3587
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003588 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003589 kfree(cmd);
3590
3591 return rc;
3592}
3593
3594/*
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303595 * MWL8K_CMD_SET_MAC_ADDR.
3596 */
3597static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3598 struct ieee80211_vif *vif, u8 *mac)
3599{
3600 return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3601}
3602
3603/*
3604 * MWL8K_CMD_DEL_MAC_ADDR.
3605 */
3606static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3607 struct ieee80211_vif *vif, u8 *mac)
3608{
3609 return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3610}
3611
3612/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003613 * CMD_SET_RATEADAPT_MODE.
3614 */
3615struct mwl8k_cmd_set_rate_adapt_mode {
3616 struct mwl8k_cmd_pkt header;
3617 __le16 action;
3618 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003619} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003620
3621static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3622{
3623 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3624 int rc;
3625
3626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3627 if (cmd == NULL)
3628 return -ENOMEM;
3629
3630 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3631 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3632 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3633 cmd->mode = cpu_to_le16(mode);
3634
3635 rc = mwl8k_post_cmd(hw, &cmd->header);
3636 kfree(cmd);
3637
3638 return rc;
3639}
3640
3641/*
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003642 * CMD_GET_WATCHDOG_BITMAP.
3643 */
3644struct mwl8k_cmd_get_watchdog_bitmap {
3645 struct mwl8k_cmd_pkt header;
3646 u8 bitmap;
3647} __packed;
3648
3649static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3650{
3651 struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3652 int rc;
3653
3654 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3655 if (cmd == NULL)
3656 return -ENOMEM;
3657
3658 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3659 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3660
3661 rc = mwl8k_post_cmd(hw, &cmd->header);
3662 if (!rc)
3663 *bitmap = cmd->bitmap;
3664
3665 kfree(cmd);
3666
3667 return rc;
3668}
3669
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303670#define MWL8K_WMM_QUEUE_NUMBER 3
3671
3672static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3673 u8 idx);
3674
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003675static void mwl8k_watchdog_ba_events(struct work_struct *work)
3676{
3677 int rc;
3678 u8 bitmap = 0, stream_index;
3679 struct mwl8k_ampdu_stream *streams;
3680 struct mwl8k_priv *priv =
3681 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303682 struct ieee80211_hw *hw = priv->hw;
3683 int i;
3684 u32 status = 0;
3685
3686 mwl8k_fw_lock(hw);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003687
3688 rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3689 if (rc)
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303690 goto done;
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003691
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303692 spin_lock(&priv->stream_lock);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003693
3694 /* the bitmap is the hw queue number. Map it to the ampdu queue. */
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303695 for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3696 if (bitmap & (1 << i)) {
3697 stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3698 TOTAL_HW_TX_QUEUES;
3699 streams = &priv->ampdu[stream_index];
3700 if (streams->state == AMPDU_STREAM_ACTIVE) {
3701 ieee80211_stop_tx_ba_session(streams->sta,
3702 streams->tid);
3703 spin_unlock(&priv->stream_lock);
3704 mwl8k_destroy_ba(hw, stream_index);
3705 spin_lock(&priv->stream_lock);
3706 }
3707 }
3708 }
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003709
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303710 spin_unlock(&priv->stream_lock);
3711done:
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05303712 atomic_dec(&priv->watchdog_event_pending);
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303713 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3714 iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3715 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3716 mwl8k_fw_unlock(hw);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003717 return;
3718}
3719
3720
3721/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003722 * CMD_BSS_START.
3723 */
3724struct mwl8k_cmd_bss_start {
3725 struct mwl8k_cmd_pkt header;
3726 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003727} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003728
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003729static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3730 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003731{
3732 struct mwl8k_cmd_bss_start *cmd;
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303733 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3734 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003735 int rc;
3736
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303737 if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3738 return 0;
3739
3740 if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3741 return 0;
3742
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003743 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3744 if (cmd == NULL)
3745 return -ENOMEM;
3746
3747 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3748 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3749 cmd->enable = cpu_to_le32(enable);
3750
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003751 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003752 kfree(cmd);
3753
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303754 if (!rc) {
3755 if (enable)
3756 priv->running_bsses |= (1 << mwl8k_vif->macid);
3757 else
3758 priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3759 }
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003760 return rc;
3761}
3762
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303763static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3764{
3765 struct mwl8k_priv *priv = hw->priv;
3766 struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3767 struct ieee80211_vif *vif;
3768
3769 list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3770 vif = mwl8k_vif->vif;
3771
3772 if (!(bitmap & (1 << mwl8k_vif->macid)))
3773 continue;
3774
3775 if (vif->type == NL80211_IFTYPE_AP)
3776 mwl8k_cmd_bss_start(hw, vif, enable);
3777 }
3778}
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003779/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003780 * CMD_BASTREAM.
3781 */
3782
3783/*
3784 * UPSTREAM is tx direction
3785 */
3786#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3787#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3788
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303789enum ba_stream_action_type {
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003790 MWL8K_BA_CREATE,
3791 MWL8K_BA_UPDATE,
3792 MWL8K_BA_DESTROY,
3793 MWL8K_BA_FLUSH,
3794 MWL8K_BA_CHECK,
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303795};
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003796
3797
3798struct mwl8k_create_ba_stream {
3799 __le32 flags;
3800 __le32 idle_thrs;
3801 __le32 bar_thrs;
3802 __le32 window_size;
3803 u8 peer_mac_addr[6];
3804 u8 dialog_token;
3805 u8 tid;
3806 u8 queue_id;
3807 u8 param_info;
3808 __le32 ba_context;
3809 u8 reset_seq_no_flag;
3810 __le16 curr_seq_no;
3811 u8 sta_src_mac_addr[6];
3812} __packed;
3813
3814struct mwl8k_destroy_ba_stream {
3815 __le32 flags;
3816 __le32 ba_context;
3817} __packed;
3818
3819struct mwl8k_cmd_bastream {
3820 struct mwl8k_cmd_pkt header;
3821 __le32 action;
3822 union {
3823 struct mwl8k_create_ba_stream create_params;
3824 struct mwl8k_destroy_ba_stream destroy_params;
3825 };
3826} __packed;
3827
3828static int
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303829mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3830 struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003831{
3832 struct mwl8k_cmd_bastream *cmd;
3833 int rc;
3834
3835 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3836 if (cmd == NULL)
3837 return -ENOMEM;
3838
3839 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3840 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3841
3842 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3843
3844 cmd->create_params.queue_id = stream->idx;
3845 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3846 ETH_ALEN);
3847 cmd->create_params.tid = stream->tid;
3848
3849 cmd->create_params.flags =
3850 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3851 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3852
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303853 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003854
3855 kfree(cmd);
3856
3857 return rc;
3858}
3859
3860static int
3861mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303862 u8 buf_size, struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003863{
3864 struct mwl8k_cmd_bastream *cmd;
3865 int rc;
3866
3867 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3868 if (cmd == NULL)
3869 return -ENOMEM;
3870
3871
3872 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3873 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3874
3875 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3876
3877 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3878 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3879 cmd->create_params.queue_id = stream->idx;
3880
3881 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3882 cmd->create_params.tid = stream->tid;
3883 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3884 cmd->create_params.reset_seq_no_flag = 1;
3885
3886 cmd->create_params.param_info =
3887 (stream->sta->ht_cap.ampdu_factor &
3888 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3889 ((stream->sta->ht_cap.ampdu_density << 2) &
3890 IEEE80211_HT_AMPDU_PARM_DENSITY);
3891
3892 cmd->create_params.flags =
3893 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3894 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3895
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303896 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003897
3898 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3899 stream->sta->addr, stream->tid);
3900 kfree(cmd);
3901
3902 return rc;
3903}
3904
3905static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303906 u8 idx)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003907{
3908 struct mwl8k_cmd_bastream *cmd;
3909
3910 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3911 if (cmd == NULL)
3912 return;
3913
3914 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3915 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3916 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3917
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303918 cmd->destroy_params.ba_context = cpu_to_le32(idx);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003919 mwl8k_post_cmd(hw, &cmd->header);
3920
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303921 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003922
3923 kfree(cmd);
3924}
3925
3926/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003927 * CMD_SET_NEW_STN.
3928 */
3929struct mwl8k_cmd_set_new_stn {
3930 struct mwl8k_cmd_pkt header;
3931 __le16 aid;
3932 __u8 mac_addr[6];
3933 __le16 stn_id;
3934 __le16 action;
3935 __le16 rsvd;
3936 __le32 legacy_rates;
3937 __u8 ht_rates[4];
3938 __le16 cap_info;
3939 __le16 ht_capabilities_info;
3940 __u8 mac_ht_param_info;
3941 __u8 rev;
3942 __u8 control_channel;
3943 __u8 add_channel;
3944 __le16 op_mode;
3945 __le16 stbc;
3946 __u8 add_qos_info;
3947 __u8 is_qos_sta;
3948 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003949} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003950
3951#define MWL8K_STA_ACTION_ADD 0
3952#define MWL8K_STA_ACTION_REMOVE 2
3953
3954static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3955 struct ieee80211_vif *vif,
3956 struct ieee80211_sta *sta)
3957{
3958 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003959 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003960 int rc;
3961
3962 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3963 if (cmd == NULL)
3964 return -ENOMEM;
3965
3966 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3967 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3968 cmd->aid = cpu_to_le16(sta->aid);
3969 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3970 cmd->stn_id = cpu_to_le16(sta->aid);
3971 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Karl Beldan675a0b02013-03-25 16:26:57 +01003972 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003973 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3974 else
3975 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3976 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003977 if (sta->ht_cap.ht_supported) {
3978 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3979 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3980 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3981 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3982 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3983 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3984 ((sta->ht_cap.ampdu_density & 7) << 2);
3985 cmd->is_qos_sta = 1;
3986 }
3987
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003988 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003989 kfree(cmd);
3990
3991 return rc;
3992}
3993
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003994static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3995 struct ieee80211_vif *vif)
3996{
3997 struct mwl8k_cmd_set_new_stn *cmd;
3998 int rc;
3999
4000 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4001 if (cmd == NULL)
4002 return -ENOMEM;
4003
4004 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4005 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4006 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
4007
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004008 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004009 kfree(cmd);
4010
4011 return rc;
4012}
4013
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004014static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4015 struct ieee80211_vif *vif, u8 *addr)
4016{
4017 struct mwl8k_cmd_set_new_stn *cmd;
Yogesh Ashok Powar0dd13a42013-01-08 10:16:37 +05304018 struct mwl8k_priv *priv = hw->priv;
4019 int rc, i;
4020 u8 idx;
4021
4022 spin_lock(&priv->stream_lock);
4023 /* Destroy any active ampdu streams for this sta */
4024 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4025 struct mwl8k_ampdu_stream *s;
4026 s = &priv->ampdu[i];
4027 if (s->state != AMPDU_NO_STREAM) {
4028 if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4029 if (s->state == AMPDU_STREAM_ACTIVE) {
4030 idx = s->idx;
4031 spin_unlock(&priv->stream_lock);
4032 mwl8k_destroy_ba(hw, idx);
4033 spin_lock(&priv->stream_lock);
4034 } else if (s->state == AMPDU_STREAM_NEW) {
4035 mwl8k_remove_stream(hw, s);
4036 }
4037 }
4038 }
4039 }
4040
4041 spin_unlock(&priv->stream_lock);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004042
4043 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4044 if (cmd == NULL)
4045 return -ENOMEM;
4046
4047 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4048 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4049 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4050 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4051
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004052 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004053 kfree(cmd);
4054
4055 return rc;
4056}
4057
4058/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004059 * CMD_UPDATE_ENCRYPTION.
4060 */
4061
4062#define MAX_ENCR_KEY_LENGTH 16
4063#define MIC_KEY_LENGTH 8
4064
4065struct mwl8k_cmd_update_encryption {
4066 struct mwl8k_cmd_pkt header;
4067
4068 __le32 action;
4069 __le32 reserved;
4070 __u8 mac_addr[6];
4071 __u8 encr_type;
4072
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304073} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004074
4075struct mwl8k_cmd_set_key {
4076 struct mwl8k_cmd_pkt header;
4077
4078 __le32 action;
4079 __le32 reserved;
4080 __le16 length;
4081 __le16 key_type_id;
4082 __le32 key_info;
4083 __le32 key_id;
4084 __le16 key_len;
4085 __u8 key_material[MAX_ENCR_KEY_LENGTH];
4086 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4087 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4088 __le16 tkip_rsc_low;
4089 __le32 tkip_rsc_high;
4090 __le16 tkip_tsc_low;
4091 __le32 tkip_tsc_high;
4092 __u8 mac_addr[6];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304093} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004094
4095enum {
4096 MWL8K_ENCR_ENABLE,
4097 MWL8K_ENCR_SET_KEY,
4098 MWL8K_ENCR_REMOVE_KEY,
4099 MWL8K_ENCR_SET_GROUP_KEY,
4100};
4101
4102#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
4103#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
4104#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
4105#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
4106#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
4107
4108enum {
4109 MWL8K_ALG_WEP,
4110 MWL8K_ALG_TKIP,
4111 MWL8K_ALG_CCMP,
4112};
4113
4114#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
4115#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
4116#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
4117#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
4118#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
4119
4120static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4121 struct ieee80211_vif *vif,
4122 u8 *addr,
4123 u8 encr_type)
4124{
4125 struct mwl8k_cmd_update_encryption *cmd;
4126 int rc;
4127
4128 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4129 if (cmd == NULL)
4130 return -ENOMEM;
4131
4132 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4133 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4134 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4135 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4136 cmd->encr_type = encr_type;
4137
4138 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4139 kfree(cmd);
4140
4141 return rc;
4142}
4143
4144static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4145 u8 *addr,
4146 struct ieee80211_key_conf *key)
4147{
4148 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4149 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4150 cmd->length = cpu_to_le16(sizeof(*cmd) -
4151 offsetof(struct mwl8k_cmd_set_key, length));
4152 cmd->key_id = cpu_to_le32(key->keyidx);
4153 cmd->key_len = cpu_to_le16(key->keylen);
4154 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4155
4156 switch (key->cipher) {
4157 case WLAN_CIPHER_SUITE_WEP40:
4158 case WLAN_CIPHER_SUITE_WEP104:
4159 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4160 if (key->keyidx == 0)
4161 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4162
4163 break;
4164 case WLAN_CIPHER_SUITE_TKIP:
4165 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4166 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4167 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4168 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4169 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4170 | MWL8K_KEY_FLAG_TSC_VALID);
4171 break;
4172 case WLAN_CIPHER_SUITE_CCMP:
4173 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4174 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4175 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4176 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4177 break;
4178 default:
4179 return -ENOTSUPP;
4180 }
4181
4182 return 0;
4183}
4184
4185static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4186 struct ieee80211_vif *vif,
4187 u8 *addr,
4188 struct ieee80211_key_conf *key)
4189{
4190 struct mwl8k_cmd_set_key *cmd;
4191 int rc;
4192 int keymlen;
4193 u32 action;
4194 u8 idx;
4195 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4196
4197 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4198 if (cmd == NULL)
4199 return -ENOMEM;
4200
4201 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4202 if (rc < 0)
4203 goto done;
4204
4205 idx = key->keyidx;
4206
4207 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4208 action = MWL8K_ENCR_SET_KEY;
4209 else
4210 action = MWL8K_ENCR_SET_GROUP_KEY;
4211
4212 switch (key->cipher) {
4213 case WLAN_CIPHER_SUITE_WEP40:
4214 case WLAN_CIPHER_SUITE_WEP104:
4215 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4216 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4217 sizeof(*key) + key->keylen);
4218 mwl8k_vif->wep_key_conf[idx].enabled = 1;
4219 }
4220
Yogesh Ashok Powar9b571e22011-05-04 17:22:16 +05304221 keymlen = key->keylen;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004222 action = MWL8K_ENCR_SET_KEY;
4223 break;
4224 case WLAN_CIPHER_SUITE_TKIP:
4225 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4226 break;
4227 case WLAN_CIPHER_SUITE_CCMP:
4228 keymlen = key->keylen;
4229 break;
4230 default:
4231 rc = -ENOTSUPP;
4232 goto done;
4233 }
4234
4235 memcpy(cmd->key_material, key->key, keymlen);
4236 cmd->action = cpu_to_le32(action);
4237
4238 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4239done:
4240 kfree(cmd);
4241
4242 return rc;
4243}
4244
4245static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4246 struct ieee80211_vif *vif,
4247 u8 *addr,
4248 struct ieee80211_key_conf *key)
4249{
4250 struct mwl8k_cmd_set_key *cmd;
4251 int rc;
4252 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4253
4254 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4255 if (cmd == NULL)
4256 return -ENOMEM;
4257
4258 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4259 if (rc < 0)
4260 goto done;
4261
4262 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Dan Carpenterd981e052012-01-17 10:33:31 +03004263 key->cipher == WLAN_CIPHER_SUITE_WEP104)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004264 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4265
4266 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4267
4268 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4269done:
4270 kfree(cmd);
4271
4272 return rc;
4273}
4274
4275static int mwl8k_set_key(struct ieee80211_hw *hw,
4276 enum set_key_cmd cmd_param,
4277 struct ieee80211_vif *vif,
4278 struct ieee80211_sta *sta,
4279 struct ieee80211_key_conf *key)
4280{
4281 int rc = 0;
4282 u8 encr_type;
4283 u8 *addr;
4284 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar751930c2013-01-25 16:20:28 +05304285 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004286
Yogesh Ashok Powar751930c2013-01-25 16:20:28 +05304287 if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004288 return -EOPNOTSUPP;
4289
4290 if (sta == NULL)
Yogesh Ashok Powarff7e9f92012-01-27 16:08:27 +05304291 addr = vif->addr;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004292 else
4293 addr = sta->addr;
4294
4295 if (cmd_param == SET_KEY) {
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004296 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4297 if (rc)
4298 goto out;
4299
4300 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4301 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4302 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4303 else
4304 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4305
4306 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4307 encr_type);
4308 if (rc)
4309 goto out;
4310
4311 mwl8k_vif->is_hw_crypto_enabled = true;
4312
4313 } else {
4314 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4315
4316 if (rc)
4317 goto out;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004318 }
4319out:
4320 return rc;
4321}
4322
4323/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004324 * CMD_UPDATE_STADB.
4325 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004326struct ewc_ht_info {
4327 __le16 control1;
4328 __le16 control2;
4329 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004330} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004331
4332struct peer_capability_info {
4333 /* Peer type - AP vs. STA. */
4334 __u8 peer_type;
4335
4336 /* Basic 802.11 capabilities from assoc resp. */
4337 __le16 basic_caps;
4338
4339 /* Set if peer supports 802.11n high throughput (HT). */
4340 __u8 ht_support;
4341
4342 /* Valid if HT is supported. */
4343 __le16 ht_caps;
4344 __u8 extended_ht_caps;
4345 struct ewc_ht_info ewc_info;
4346
4347 /* Legacy rate table. Intersection of our rates and peer rates. */
4348 __u8 legacy_rates[12];
4349
4350 /* HT rate table. Intersection of our rates and peer rates. */
4351 __u8 ht_rates[16];
4352 __u8 pad[16];
4353
4354 /* If set, interoperability mode, no proprietary extensions. */
4355 __u8 interop;
4356 __u8 pad2;
4357 __u8 station_id;
4358 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004359} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004360
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004361struct mwl8k_cmd_update_stadb {
4362 struct mwl8k_cmd_pkt header;
4363
4364 /* See STADB_ACTION_TYPE */
4365 __le32 action;
4366
4367 /* Peer MAC address */
4368 __u8 peer_addr[ETH_ALEN];
4369
4370 __le32 reserved;
4371
4372 /* Peer info - valid during add/update. */
4373 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004374} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004375
Lennert Buytenheka6804002010-01-04 21:55:42 +01004376#define MWL8K_STA_DB_MODIFY_ENTRY 1
4377#define MWL8K_STA_DB_DEL_ENTRY 2
4378
4379/* Peer Entry flags - used to define the type of the peer node */
4380#define MWL8K_PEER_TYPE_ACCESSPOINT 2
4381
4382static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004383 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004384 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004385{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004386 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004387 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004388 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004389 int rc;
4390
4391 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4392 if (cmd == NULL)
4393 return -ENOMEM;
4394
4395 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4396 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01004397 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004398 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004399
Lennert Buytenheka6804002010-01-04 21:55:42 +01004400 p = &cmd->peer_info;
4401 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4402 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004403 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04004404 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004405 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4406 ((sta->ht_cap.ampdu_density & 7) << 2);
Karl Beldan675a0b02013-03-25 16:26:57 +01004407 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004408 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4409 else
4410 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4411 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004412 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004413 p->interop = 1;
4414 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004415
Lennert Buytenheka6804002010-01-04 21:55:42 +01004416 rc = mwl8k_post_cmd(hw, &cmd->header);
Nickolai Zeldovichc4f74d32013-01-06 20:27:22 -05004417 if (!rc)
4418 rc = p->station_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004419 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004420
Nickolai Zeldovichc4f74d32013-01-06 20:27:22 -05004421 return rc;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004422}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004423
Lennert Buytenheka6804002010-01-04 21:55:42 +01004424static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4425 struct ieee80211_vif *vif, u8 *addr)
4426{
4427 struct mwl8k_cmd_update_stadb *cmd;
4428 int rc;
4429
4430 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4431 if (cmd == NULL)
4432 return -ENOMEM;
4433
4434 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4435 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4436 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4437 memcpy(cmd->peer_addr, addr, ETH_ALEN);
4438
4439 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004440 kfree(cmd);
4441
4442 return rc;
4443}
4444
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004445
4446/*
4447 * Interrupt handling.
4448 */
4449static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4450{
4451 struct ieee80211_hw *hw = dev_id;
4452 struct mwl8k_priv *priv = hw->priv;
4453 u32 status;
4454
4455 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004456 if (!status)
4457 return IRQ_NONE;
4458
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004459 if (status & MWL8K_A2H_INT_TX_DONE) {
4460 status &= ~MWL8K_A2H_INT_TX_DONE;
4461 tasklet_schedule(&priv->poll_tx_task);
4462 }
4463
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004464 if (status & MWL8K_A2H_INT_RX_READY) {
4465 status &= ~MWL8K_A2H_INT_RX_READY;
4466 tasklet_schedule(&priv->poll_rx_task);
4467 }
4468
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004469 if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05304470 iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4471 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4472
4473 atomic_inc(&priv->watchdog_event_pending);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004474 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4475 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4476 }
4477
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004478 if (status)
4479 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004480
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004481 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004482 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004483 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004484 }
4485
4486 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004487 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004488 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004489 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004490 }
4491
4492 return IRQ_HANDLED;
4493}
4494
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004495static void mwl8k_tx_poll(unsigned long data)
4496{
4497 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4498 struct mwl8k_priv *priv = hw->priv;
4499 int limit;
4500 int i;
4501
4502 limit = 32;
4503
4504 spin_lock_bh(&priv->tx_lock);
4505
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004506 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004507 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4508
4509 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4510 complete(priv->tx_wait);
4511 priv->tx_wait = NULL;
4512 }
4513
4514 spin_unlock_bh(&priv->tx_lock);
4515
4516 if (limit) {
4517 writel(~MWL8K_A2H_INT_TX_DONE,
4518 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4519 } else {
4520 tasklet_schedule(&priv->poll_tx_task);
4521 }
4522}
4523
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004524static void mwl8k_rx_poll(unsigned long data)
4525{
4526 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4527 struct mwl8k_priv *priv = hw->priv;
4528 int limit;
4529
4530 limit = 32;
4531 limit -= rxq_process(hw, 0, limit);
4532 limit -= rxq_refill(hw, 0, limit);
4533
4534 if (limit) {
4535 writel(~MWL8K_A2H_INT_RX_READY,
4536 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4537 } else {
4538 tasklet_schedule(&priv->poll_rx_task);
4539 }
4540}
4541
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004542
4543/*
4544 * Core driver operations.
4545 */
Thomas Huehn36323f82012-07-23 21:33:42 +02004546static void mwl8k_tx(struct ieee80211_hw *hw,
4547 struct ieee80211_tx_control *control,
4548 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004549{
4550 struct mwl8k_priv *priv = hw->priv;
4551 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004552
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004553 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004554 wiphy_debug(hw->wiphy,
4555 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004556 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004557 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004558 }
4559
Thomas Huehn36323f82012-07-23 21:33:42 +02004560 mwl8k_txq_xmit(hw, index, control->sta, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004561}
4562
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004563static int mwl8k_start(struct ieee80211_hw *hw)
4564{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004565 struct mwl8k_priv *priv = hw->priv;
4566 int rc;
4567
Joe Perchesa0607fd2009-11-18 23:29:17 -08004568 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004569 IRQF_SHARED, MWL8K_NAME, hw);
4570 if (rc) {
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304571 priv->irq = -1;
Joe Perches5db55842010-08-11 19:11:19 -07004572 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004573 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004574 }
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304575 priv->irq = priv->pdev->irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004576
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004577 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004578 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004579 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004580
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004581 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004582 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05304583 iowrite32(MWL8K_A2H_EVENTS,
4584 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004585
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004586 rc = mwl8k_fw_lock(hw);
4587 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004588 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004589
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004590 if (!priv->ap_fw) {
4591 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004592 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004593
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004594 if (!rc)
4595 rc = mwl8k_cmd_set_pre_scan(hw);
4596
4597 if (!rc)
4598 rc = mwl8k_cmd_set_post_scan(hw,
4599 "\x00\x00\x00\x00\x00\x00");
4600 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004601
4602 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004603 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004604
4605 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004606 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004607
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004608 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004609 }
4610
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004611 if (rc) {
4612 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4613 free_irq(priv->pdev->irq, hw);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304614 priv->irq = -1;
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004615 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004616 tasklet_disable(&priv->poll_rx_task);
Nishant Sarmukadam4850b6d2013-01-08 12:10:53 +05304617 } else {
4618 ieee80211_wake_queues(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004619 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004620
4621 return rc;
4622}
4623
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004624static void mwl8k_stop(struct ieee80211_hw *hw)
4625{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004626 struct mwl8k_priv *priv = hw->priv;
4627 int i;
4628
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304629 if (!priv->hw_restart_in_progress)
4630 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004631
4632 ieee80211_stop_queues(hw);
4633
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004634 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004635 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304636 if (priv->irq != -1) {
4637 free_irq(priv->pdev->irq, hw);
4638 priv->irq = -1;
4639 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004640
4641 /* Stop finalize join worker */
4642 cancel_work_sync(&priv->finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004643 cancel_work_sync(&priv->watchdog_ba_handle);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004644 if (priv->beacon_skb != NULL)
4645 dev_kfree_skb(priv->beacon_skb);
4646
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004647 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004648 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004649 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004650
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004651 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004652 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004653 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004654}
4655
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004656static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4657
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004658static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004659 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004660{
4661 struct mwl8k_priv *priv = hw->priv;
4662 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004663 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004664 int macid, rc;
4665 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004666
4667 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004668 * Reject interface creation if sniffer mode is active, as
4669 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004670 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004671 */
4672 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004673 wiphy_info(hw->wiphy,
4674 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004675 return -EINVAL;
4676 }
4677
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004678 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004679 switch (vif->type) {
4680 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004681 if (!priv->ap_fw && di->fw_image_ap) {
4682 /* we must load the ap fw to meet this request */
4683 if (!list_empty(&priv->vif_list))
4684 return -EBUSY;
4685 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4686 if (rc)
4687 return rc;
4688 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004689 macids_supported = priv->ap_macids_supported;
4690 break;
4691 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004692 if (priv->ap_fw && di->fw_image_sta) {
Yogesh Ashok Poward59c1cf2013-01-25 16:20:03 +05304693 if (!list_empty(&priv->vif_list)) {
4694 wiphy_warn(hw->wiphy, "AP interface is running.\n"
4695 "Adding STA interface for WDS");
4696 } else {
4697 /* we must load the sta fw to
4698 * meet this request.
4699 */
4700 rc = mwl8k_reload_firmware(hw,
4701 di->fw_image_sta);
4702 if (rc)
4703 return rc;
4704 }
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004705 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004706 macids_supported = priv->sta_macids_supported;
4707 break;
4708 default:
4709 return -EINVAL;
4710 }
4711
4712 macid = ffs(macids_supported & ~priv->macids_used);
4713 if (!macid--)
4714 return -EBUSY;
4715
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004716 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004717 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004718 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004719 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004720 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004721 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004722 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4723 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004724
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004725 /* Set the mac address. */
4726 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4727
Yogesh Ashok Poward994a1c2013-01-25 16:20:35 +05304728 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004729 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4730
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004731 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004732 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004733
4734 return 0;
4735}
4736
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304737static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4738{
4739 /* Has ieee80211_restart_hw re-added the removed interfaces? */
4740 if (!priv->macids_used)
4741 return;
4742
4743 priv->macids_used &= ~(1 << vif->macid);
4744 list_del(&vif->list);
4745}
4746
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004747static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004748 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004749{
4750 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004751 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004752
Yogesh Ashok Poward994a1c2013-01-25 16:20:35 +05304753 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004754 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4755
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05304756 mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004757
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304758 mwl8k_remove_vif(priv, mwl8k_vif);
4759}
4760
4761static void mwl8k_hw_restart_work(struct work_struct *work)
4762{
4763 struct mwl8k_priv *priv =
4764 container_of(work, struct mwl8k_priv, fw_reload);
4765 struct ieee80211_hw *hw = priv->hw;
4766 struct mwl8k_device_info *di;
4767 int rc;
4768
4769 /* If some command is waiting for a response, clear it */
4770 if (priv->hostcmd_wait != NULL) {
4771 complete(priv->hostcmd_wait);
4772 priv->hostcmd_wait = NULL;
4773 }
4774
4775 priv->hw_restart_owner = current;
4776 di = priv->device_info;
4777 mwl8k_fw_lock(hw);
4778
4779 if (priv->ap_fw)
4780 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4781 else
4782 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4783
4784 if (rc)
4785 goto fail;
4786
4787 priv->hw_restart_owner = NULL;
4788 priv->hw_restart_in_progress = false;
4789
4790 /*
4791 * This unlock will wake up the queues and
4792 * also opens the command path for other
4793 * commands
4794 */
4795 mwl8k_fw_unlock(hw);
4796
4797 ieee80211_restart_hw(hw);
4798
4799 wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4800
4801 return;
4802fail:
4803 mwl8k_fw_unlock(hw);
4804
4805 wiphy_err(hw->wiphy, "Firmware restart failed\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004806}
4807
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004808static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004809{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004810 struct ieee80211_conf *conf = &hw->conf;
4811 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004812 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004813
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004814 rc = mwl8k_fw_lock(hw);
4815 if (rc)
4816 return rc;
4817
Jonas Gorskife21bb02013-03-25 16:39:54 +01004818 if (conf->flags & IEEE80211_CONF_IDLE)
4819 rc = mwl8k_cmd_radio_disable(hw);
4820 else
4821 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004822 if (rc)
4823 goto out;
4824
Yogesh Ashok Powar0f4316b2013-01-25 16:17:45 +05304825 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4826 rc = mwl8k_cmd_set_rf_channel(hw, conf);
4827 if (rc)
4828 goto out;
4829 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004830
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004831 if (conf->power_level > 18)
4832 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004833
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004834 if (priv->ap_fw) {
Nishant Sarmukadam03217082011-04-05 14:18:09 +05304835
4836 if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4837 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4838 if (rc)
4839 goto out;
4840 }
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004841
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004842
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004843 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004844 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4845 if (rc)
4846 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004847 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4848 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004849
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004850out:
4851 mwl8k_fw_unlock(hw);
4852
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004853 return rc;
4854}
4855
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004856static void
4857mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4858 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004859{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004860 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304861 u32 ap_legacy_rates = 0;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004862 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004863 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004864
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004865 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004866 return;
4867
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004868 /*
4869 * No need to capture a beacon if we're no longer associated.
4870 */
4871 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4872 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004873
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004874 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004875 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004876 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004877 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004878 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004879
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004880 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004881
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004882 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004883 if (ap == NULL) {
4884 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004885 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004886 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004887
Karl Beldan675a0b02013-03-25 16:26:57 +01004888 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ) {
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004889 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4890 } else {
4891 ap_legacy_rates =
4892 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4893 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004894 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004895
4896 rcu_read_unlock();
4897 }
4898
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304899 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4900 !priv->ap_fw) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004901 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004902 if (rc)
4903 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004904
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004905 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004906 if (rc)
4907 goto out;
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304908 } else {
4909 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4910 priv->ap_fw) {
4911 int idx;
4912 int rate;
4913
4914 /* Use AP firmware specific rate command.
4915 */
4916 idx = ffs(vif->bss_conf.basic_rates);
4917 if (idx)
4918 idx--;
4919
Karl Beldan675a0b02013-03-25 16:26:57 +01004920 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304921 rate = mwl8k_rates_24[idx].hw_value;
4922 else
4923 rate = mwl8k_rates_50[idx].hw_value;
4924
4925 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4926 }
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004927 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004928
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004929 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004930 rc = mwl8k_set_radio_preamble(hw,
4931 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004932 if (rc)
4933 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004934 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004935
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304936 if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004937 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004938 if (rc)
4939 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004940 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004941
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304942 if (vif->bss_conf.assoc && !priv->ap_fw &&
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004943 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4944 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004945 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004946 if (rc)
4947 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004948 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004949
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004950 if (vif->bss_conf.assoc &&
4951 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004952 /*
4953 * Finalize the join. Tell rx handler to process
4954 * next beacon from our BSSID.
4955 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004956 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004957 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004958 }
4959
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004960out:
4961 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004962}
4963
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004964static void
4965mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4966 struct ieee80211_bss_conf *info, u32 changed)
4967{
4968 int rc;
4969
4970 if (mwl8k_fw_lock(hw))
4971 return;
4972
4973 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4974 rc = mwl8k_set_radio_preamble(hw,
4975 vif->bss_conf.use_short_preamble);
4976 if (rc)
4977 goto out;
4978 }
4979
4980 if (changed & BSS_CHANGED_BASIC_RATES) {
4981 int idx;
4982 int rate;
4983
4984 /*
4985 * Use lowest supported basic rate for multicasts
4986 * and management frames (such as probe responses --
4987 * beacons will always go out at 1 Mb/s).
4988 */
4989 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004990 if (idx)
4991 idx--;
4992
Karl Beldan675a0b02013-03-25 16:26:57 +01004993 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004994 rate = mwl8k_rates_24[idx].hw_value;
4995 else
4996 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004997
4998 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4999 }
5000
5001 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
5002 struct sk_buff *skb;
5003
5004 skb = ieee80211_beacon_get(hw, vif);
5005 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01005006 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005007 kfree_skb(skb);
5008 }
5009 }
5010
5011 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01005012 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005013
5014out:
5015 mwl8k_fw_unlock(hw);
5016}
5017
5018static void
5019mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5020 struct ieee80211_bss_conf *info, u32 changed)
5021{
Yogesh Ashok Powar41bf9112013-01-25 16:20:42 +05305022 if (vif->type == NL80211_IFTYPE_STATION)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005023 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
Yogesh Ashok Powar41bf9112013-01-25 16:20:42 +05305024 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005025 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5026}
5027
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005028static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00005029 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005030{
5031 struct mwl8k_cmd_pkt *cmd;
5032
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005033 /*
5034 * Synthesize and return a command packet that programs the
5035 * hardware multicast address filter. At this point we don't
5036 * know whether FIF_ALLMULTI is being requested, but if it is,
5037 * we'll end up throwing this packet away and creating a new
5038 * one in mwl8k_configure_filter().
5039 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00005040 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005041
5042 return (unsigned long)cmd;
5043}
5044
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005045static int
5046mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5047 unsigned int changed_flags,
5048 unsigned int *total_flags)
5049{
5050 struct mwl8k_priv *priv = hw->priv;
5051
5052 /*
5053 * Hardware sniffer mode is mutually exclusive with STA
5054 * operation, so refuse to enable sniffer mode if a STA
5055 * interface is active.
5056 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005057 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005058 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07005059 wiphy_info(hw->wiphy,
5060 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005061 return 0;
5062 }
5063
5064 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005065 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005066 return 0;
5067 priv->sniffer_enabled = true;
5068 }
5069
5070 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
5071 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5072 FIF_OTHER_BSS;
5073
5074 return 1;
5075}
5076
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005077static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5078{
5079 if (!list_empty(&priv->vif_list))
5080 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5081
5082 return NULL;
5083}
5084
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005085static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5086 unsigned int changed_flags,
5087 unsigned int *total_flags,
5088 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005089{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005090 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005091 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
5092
5093 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02005094 * AP firmware doesn't allow fine-grained control over
5095 * the receive filter.
5096 */
5097 if (priv->ap_fw) {
5098 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5099 kfree(cmd);
5100 return;
5101 }
5102
5103 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005104 * Enable hardware sniffer mode if FIF_CONTROL or
5105 * FIF_OTHER_BSS is requested.
5106 */
5107 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5108 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5109 kfree(cmd);
5110 return;
5111 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005112
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005113 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005114 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005115
Lennert Buytenhek90852f72010-01-02 10:31:42 +01005116 if (mwl8k_fw_lock(hw)) {
5117 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005118 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01005119 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005120
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005121 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005122 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005123 priv->sniffer_enabled = false;
5124 }
5125
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005126 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005127 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5128 /*
5129 * Disable the BSS filter.
5130 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005131 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005132 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005133 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01005134 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02005135
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005136 /*
5137 * Enable the BSS filter.
5138 *
5139 * If there is an active STA interface, use that
5140 * interface's BSSID, otherwise use a dummy one
5141 * (where the OUI part needs to be nonzero for
5142 * the BSSID to be accepted by POST_SCAN).
5143 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005144 mwl8k_vif = mwl8k_first_vif(priv);
5145 if (mwl8k_vif != NULL)
5146 bssid = mwl8k_vif->vif->bss_conf.bssid;
5147 else
5148 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02005149
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005150 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005151 }
5152 }
5153
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005154 /*
5155 * If FIF_ALLMULTI is being requested, throw away the command
5156 * packet that ->prepare_multicast() built and replace it with
5157 * a command packet that enables reception of all multicast
5158 * packets.
5159 */
5160 if (*total_flags & FIF_ALLMULTI) {
5161 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00005162 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005163 }
5164
5165 if (cmd != NULL) {
5166 mwl8k_post_cmd(hw, cmd);
5167 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005168 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02005169
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005170 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005171}
5172
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005173static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5174{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01005175 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005176}
5177
Johannes Berg4a6967b2010-02-19 19:18:37 +01005178static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5179 struct ieee80211_vif *vif,
5180 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005181{
5182 struct mwl8k_priv *priv = hw->priv;
5183
Johannes Berg4a6967b2010-02-19 19:18:37 +01005184 if (priv->ap_fw)
5185 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5186 else
5187 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5188}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005189
Johannes Berg4a6967b2010-02-19 19:18:37 +01005190static int mwl8k_sta_add(struct ieee80211_hw *hw,
5191 struct ieee80211_vif *vif,
5192 struct ieee80211_sta *sta)
5193{
5194 struct mwl8k_priv *priv = hw->priv;
5195 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005196 int i;
5197 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5198 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005199
Johannes Berg4a6967b2010-02-19 19:18:37 +01005200 if (!priv->ap_fw) {
5201 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5202 if (ret >= 0) {
5203 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07005204 if (sta->ht_cap.ht_supported)
5205 MWL8K_STA(sta)->is_ampdu_allowed = true;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005206 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005207 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01005208
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005209 } else {
5210 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005211 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005212
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005213 for (i = 0; i < NUM_WEP_KEYS; i++) {
5214 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5215 if (mwl8k_vif->wep_key_conf[i].enabled)
5216 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5217 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005218 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01005219}
5220
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005221static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5222 struct ieee80211_vif *vif, u16 queue,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005223 const struct ieee80211_tx_queue_params *params)
5224{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005225 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005226 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005227
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005228 rc = mwl8k_fw_lock(hw);
5229 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005230 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005231 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5232
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005233 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005234 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005235
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005236 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005237 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005238 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005239 params->cw_min,
5240 params->cw_max,
5241 params->aifs,
5242 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005243 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005244
5245 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005246 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005247
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005248 return rc;
5249}
5250
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005251static int mwl8k_get_stats(struct ieee80211_hw *hw,
5252 struct ieee80211_low_level_stats *stats)
5253{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005254 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005255}
5256
John W. Linville0d462bb2010-07-28 14:04:24 -04005257static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5258 struct survey_info *survey)
5259{
5260 struct mwl8k_priv *priv = hw->priv;
5261 struct ieee80211_conf *conf = &hw->conf;
5262
5263 if (idx != 0)
5264 return -ENOENT;
5265
Karl Beldan675a0b02013-03-25 16:26:57 +01005266 survey->channel = conf->chandef.chan;
John W. Linville0d462bb2010-07-28 14:04:24 -04005267 survey->filled = SURVEY_INFO_NOISE_DBM;
5268 survey->noise = priv->noise;
5269
5270 return 0;
5271}
5272
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005273#define MAX_AMPDU_ATTEMPTS 5
5274
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005275static int
5276mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5277 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01005278 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5279 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005280{
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005281
5282 int i, rc = 0;
5283 struct mwl8k_priv *priv = hw->priv;
5284 struct mwl8k_ampdu_stream *stream;
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305285 u8 *addr = sta->addr, idx;
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305286 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005287
5288 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
5289 return -ENOTSUPP;
5290
5291 spin_lock(&priv->stream_lock);
5292 stream = mwl8k_lookup_stream(hw, addr, tid);
5293
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005294 switch (action) {
5295 case IEEE80211_AMPDU_RX_START:
5296 case IEEE80211_AMPDU_RX_STOP:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005297 break;
5298 case IEEE80211_AMPDU_TX_START:
5299 /* By the time we get here the hw queues may contain outgoing
5300 * packets for this RA/TID that are not part of this BA
5301 * session. The hw will assign sequence numbers to these
5302 * packets as they go out. So if we query the hw for its next
5303 * sequence number and use that for the SSN here, it may end up
5304 * being wrong, which will lead to sequence number mismatch at
5305 * the recipient. To avoid this, we reset the sequence number
5306 * to O for the first MPDU in this BA stream.
5307 */
5308 *ssn = 0;
5309 if (stream == NULL) {
5310 /* This means that somebody outside this driver called
5311 * ieee80211_start_tx_ba_session. This is unexpected
5312 * because we do our own rate control. Just warn and
5313 * move on.
5314 */
5315 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
5316 "Proceeding anyway.\n", __func__);
5317 stream = mwl8k_add_stream(hw, sta, tid);
5318 }
5319 if (stream == NULL) {
5320 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5321 rc = -EBUSY;
5322 break;
5323 }
5324 stream->state = AMPDU_STREAM_IN_PROGRESS;
5325
5326 /* Release the lock before we do the time consuming stuff */
5327 spin_unlock(&priv->stream_lock);
5328 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305329
5330 /* Check if link is still valid */
5331 if (!sta_info->is_ampdu_allowed) {
5332 spin_lock(&priv->stream_lock);
5333 mwl8k_remove_stream(hw, stream);
5334 spin_unlock(&priv->stream_lock);
5335 return -EBUSY;
5336 }
5337
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305338 rc = mwl8k_check_ba(hw, stream, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005339
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305340 /* If HW restart is in progress mwl8k_post_cmd will
5341 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5342 * such cases
5343 */
5344 if (!rc || rc == -EBUSY)
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005345 break;
5346 /*
5347 * HW queues take time to be flushed, give them
5348 * sufficient time
5349 */
5350
5351 msleep(1000);
5352 }
5353 spin_lock(&priv->stream_lock);
5354 if (rc) {
5355 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5356 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5357 mwl8k_remove_stream(hw, stream);
5358 rc = -EBUSY;
5359 break;
5360 }
5361 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
5362 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02005363 case IEEE80211_AMPDU_TX_STOP_CONT:
5364 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5365 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305366 if (stream) {
5367 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305368 idx = stream->idx;
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305369 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305370 mwl8k_destroy_ba(hw, idx);
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305371 spin_lock(&priv->stream_lock);
5372 }
5373 mwl8k_remove_stream(hw, stream);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005374 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005375 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5376 break;
5377 case IEEE80211_AMPDU_TX_OPERATIONAL:
5378 BUG_ON(stream == NULL);
5379 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5380 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305381 rc = mwl8k_create_ba(hw, stream, buf_size, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005382 spin_lock(&priv->stream_lock);
5383 if (!rc)
5384 stream->state = AMPDU_STREAM_ACTIVE;
5385 else {
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305386 idx = stream->idx;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005387 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305388 mwl8k_destroy_ba(hw, idx);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005389 spin_lock(&priv->stream_lock);
5390 wiphy_debug(hw->wiphy,
5391 "Failed adding stream for sta %pM tid %d\n",
5392 addr, tid);
5393 mwl8k_remove_stream(hw, stream);
5394 }
5395 break;
5396
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005397 default:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005398 rc = -ENOTSUPP;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005399 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005400
5401 spin_unlock(&priv->stream_lock);
5402 return rc;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005403}
5404
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005405static const struct ieee80211_ops mwl8k_ops = {
5406 .tx = mwl8k_tx,
5407 .start = mwl8k_start,
5408 .stop = mwl8k_stop,
5409 .add_interface = mwl8k_add_interface,
5410 .remove_interface = mwl8k_remove_interface,
5411 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005412 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02005413 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005414 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005415 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005416 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01005417 .sta_add = mwl8k_sta_add,
5418 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005419 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005420 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04005421 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005422 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005423};
5424
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005425static void mwl8k_finalize_join_worker(struct work_struct *work)
5426{
5427 struct mwl8k_priv *priv =
5428 container_of(work, struct mwl8k_priv, finalize_join_worker);
5429 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01005430 struct ieee80211_mgmt *mgmt = (void *)skb->data;
5431 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5432 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5433 mgmt->u.beacon.variable, len);
5434 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005435
Johannes Berg56007a02010-01-26 14:19:52 +01005436 if (tim && tim[1] >= 2)
5437 dtim_period = tim[3];
5438
5439 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005440
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005441 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005442 priv->beacon_skb = NULL;
5443}
5444
John W. Linvillebcb628d2009-11-06 16:40:16 -05005445enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005446 MWL8363 = 0,
5447 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005448 MWL8366,
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305449 MWL8764,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02005450};
5451
Yogesh Ashok Powarc2f2e202013-01-03 13:24:44 +05305452#define MWL8K_8366_AP_FW_API 3
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005453#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5454#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5455
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305456#define MWL8K_8764_AP_FW_API 1
5457#define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5458#define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5459
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005460static struct mwl8k_device_info mwl8k_info_tbl[] = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005461 [MWL8363] = {
5462 .part_name = "88w8363",
5463 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005464 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005465 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005466 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005467 .part_name = "88w8687",
5468 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005469 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05005470 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005471 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005472 .part_name = "88w8366",
5473 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005474 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005475 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5476 .fw_api_ap = MWL8K_8366_AP_FW_API,
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305477 .ap_rxd_ops = &rxd_ap_ops,
5478 },
5479 [MWL8764] = {
5480 .part_name = "88w8764",
5481 .fw_image_ap = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5482 .fw_api_ap = MWL8K_8764_AP_FW_API,
5483 .ap_rxd_ops = &rxd_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005484 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005485};
5486
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005487MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5488MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5489MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5490MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5491MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5492MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005493MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005494
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005495static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01005496 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005497 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5498 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005499 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5500 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5501 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Jonas Gorskifc5bc162013-02-05 16:59:17 +01005502 { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5503 { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01005504 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305505 { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005506 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005507};
5508MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5509
Brian Cavagnolo99020472010-11-12 17:23:52 -08005510static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5511{
5512 int rc;
5513 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5514 "Trying alternative firmware %s\n", pci_name(priv->pdev),
5515 priv->fw_pref, priv->fw_alt);
5516 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5517 if (rc) {
5518 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5519 pci_name(priv->pdev), priv->fw_alt);
5520 return rc;
5521 }
5522 return 0;
5523}
5524
5525static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5526static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5527{
5528 struct mwl8k_priv *priv = context;
5529 struct mwl8k_device_info *di = priv->device_info;
5530 int rc;
5531
5532 switch (priv->fw_state) {
5533 case FW_STATE_INIT:
5534 if (!fw) {
5535 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5536 pci_name(priv->pdev), di->helper_image);
5537 goto fail;
5538 }
5539 priv->fw_helper = fw;
5540 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5541 true);
5542 if (rc && priv->fw_alt) {
5543 rc = mwl8k_request_alt_fw(priv);
5544 if (rc)
5545 goto fail;
5546 priv->fw_state = FW_STATE_LOADING_ALT;
5547 } else if (rc)
5548 goto fail;
5549 else
5550 priv->fw_state = FW_STATE_LOADING_PREF;
5551 break;
5552
5553 case FW_STATE_LOADING_PREF:
5554 if (!fw) {
5555 if (priv->fw_alt) {
5556 rc = mwl8k_request_alt_fw(priv);
5557 if (rc)
5558 goto fail;
5559 priv->fw_state = FW_STATE_LOADING_ALT;
5560 } else
5561 goto fail;
5562 } else {
5563 priv->fw_ucode = fw;
5564 rc = mwl8k_firmware_load_success(priv);
5565 if (rc)
5566 goto fail;
5567 else
5568 complete(&priv->firmware_loading_complete);
5569 }
5570 break;
5571
5572 case FW_STATE_LOADING_ALT:
5573 if (!fw) {
5574 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5575 pci_name(priv->pdev), di->helper_image);
5576 goto fail;
5577 }
5578 priv->fw_ucode = fw;
5579 rc = mwl8k_firmware_load_success(priv);
5580 if (rc)
5581 goto fail;
5582 else
5583 complete(&priv->firmware_loading_complete);
5584 break;
5585
5586 default:
5587 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5588 MWL8K_NAME, priv->fw_state);
5589 BUG_ON(1);
5590 }
5591
5592 return;
5593
5594fail:
5595 priv->fw_state = FW_STATE_ERROR;
5596 complete(&priv->firmware_loading_complete);
5597 device_release_driver(&priv->pdev->dev);
5598 mwl8k_release_firmware(priv);
5599}
5600
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305601#define MAX_RESTART_ATTEMPTS 1
Brian Cavagnolo99020472010-11-12 17:23:52 -08005602static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5603 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005604{
5605 struct mwl8k_priv *priv = hw->priv;
5606 int rc;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305607 int count = MAX_RESTART_ATTEMPTS;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005608
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305609retry:
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005610 /* Reset firmware and hardware */
5611 mwl8k_hw_reset(priv);
5612
5613 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005614 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005615 if (rc) {
5616 wiphy_err(hw->wiphy, "Firmware files not found\n");
5617 return rc;
5618 }
5619
Brian Cavagnolo99020472010-11-12 17:23:52 -08005620 if (nowait)
5621 return rc;
5622
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005623 /* Load firmware into hardware */
5624 rc = mwl8k_load_firmware(hw);
5625 if (rc)
5626 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5627
5628 /* Reclaim memory once firmware is successfully loaded */
5629 mwl8k_release_firmware(priv);
5630
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305631 if (rc && count) {
5632 /* FW did not start successfully;
5633 * lets try one more time
5634 */
5635 count--;
5636 wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5637 msleep(20);
5638 goto retry;
5639 }
5640
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005641 return rc;
5642}
5643
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005644static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5645{
5646 struct mwl8k_priv *priv = hw->priv;
5647 int rc = 0;
5648 int i;
5649
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005650 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005651 rc = mwl8k_txq_init(hw, i);
5652 if (rc)
5653 break;
5654 if (priv->ap_fw)
5655 iowrite32(priv->txq[i].txd_dma,
5656 priv->sram + priv->txq_offset[i]);
5657 }
5658 return rc;
5659}
5660
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005661/* initialize hw after successfully loading a firmware image */
5662static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5663{
5664 struct mwl8k_priv *priv = hw->priv;
5665 int rc = 0;
5666 int i;
5667
5668 if (priv->ap_fw) {
5669 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5670 if (priv->rxd_ops == NULL) {
5671 wiphy_err(hw->wiphy,
5672 "Driver does not have AP firmware image support for this hardware\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04005673 rc = -ENOENT;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005674 goto err_stop_firmware;
5675 }
5676 } else {
5677 priv->rxd_ops = &rxd_sta_ops;
5678 }
5679
5680 priv->sniffer_enabled = false;
5681 priv->wmm_enabled = false;
5682 priv->pending_tx_pkts = 0;
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05305683 atomic_set(&priv->watchdog_event_pending, 0);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005684
5685 rc = mwl8k_rxq_init(hw, 0);
5686 if (rc)
5687 goto err_stop_firmware;
5688 rxq_refill(hw, 0, INT_MAX);
5689
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005690 /* For the sta firmware, we need to know the dma addresses of tx queues
5691 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5692 * prior to issuing this command. But for the AP case, we learn the
5693 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5694 * case we must initialize the tx queues after.
5695 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005696 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005697 if (!priv->ap_fw) {
5698 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005699 if (rc)
5700 goto err_free_queues;
5701 }
5702
5703 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5704 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005705 iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5706 MWL8K_A2H_INT_BA_WATCHDOG,
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005707 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05305708 iowrite32(MWL8K_A2H_INT_OPC_DONE,
5709 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005710
5711 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5712 IRQF_SHARED, MWL8K_NAME, hw);
5713 if (rc) {
5714 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5715 goto err_free_queues;
5716 }
5717
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305718 /*
5719 * When hw restart is requested,
5720 * mac80211 will take care of clearing
5721 * the ampdu streams, so do not clear
5722 * the ampdu state here
5723 */
5724 if (!priv->hw_restart_in_progress)
5725 memset(priv->ampdu, 0, sizeof(priv->ampdu));
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005726
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005727 /*
5728 * Temporarily enable interrupts. Initial firmware host
5729 * commands use interrupts and avoid polling. Disable
5730 * interrupts when done.
5731 */
5732 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5733
5734 /* Get config data, mac addrs etc */
5735 if (priv->ap_fw) {
5736 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5737 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005738 rc = mwl8k_init_txqs(hw);
5739 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005740 rc = mwl8k_cmd_set_hw_spec(hw);
5741 } else {
5742 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5743 }
5744 if (rc) {
5745 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5746 goto err_free_irq;
5747 }
5748
5749 /* Turn radio off */
5750 rc = mwl8k_cmd_radio_disable(hw);
5751 if (rc) {
5752 wiphy_err(hw->wiphy, "Cannot disable\n");
5753 goto err_free_irq;
5754 }
5755
5756 /* Clear MAC address */
5757 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5758 if (rc) {
5759 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5760 goto err_free_irq;
5761 }
5762
Yogesh Ashok Powara246ac32013-01-25 16:17:56 +05305763 /* Configure Antennas */
5764 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5765 if (rc)
5766 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5767 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5768 if (rc)
5769 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5770
5771
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005772 /* Disable interrupts */
5773 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5774 free_irq(priv->pdev->irq, hw);
5775
5776 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5777 priv->device_info->part_name,
5778 priv->hw_rev, hw->wiphy->perm_addr,
5779 priv->ap_fw ? "AP" : "STA",
5780 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5781 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5782
5783 return 0;
5784
5785err_free_irq:
5786 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5787 free_irq(priv->pdev->irq, hw);
5788
5789err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005790 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005791 mwl8k_txq_deinit(hw, i);
5792 mwl8k_rxq_deinit(hw, 0);
5793
5794err_stop_firmware:
5795 mwl8k_hw_reset(priv);
5796
5797 return rc;
5798}
5799
5800/*
5801 * invoke mwl8k_reload_firmware to change the firmware image after the device
5802 * has already been registered
5803 */
5804static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5805{
5806 int i, rc = 0;
5807 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305808 struct mwl8k_vif *vif, *tmp_vif;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005809
5810 mwl8k_stop(hw);
5811 mwl8k_rxq_deinit(hw, 0);
5812
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305813 /*
5814 * All the existing interfaces are re-added by the ieee80211_reconfig;
5815 * which means driver should remove existing interfaces before calling
5816 * ieee80211_restart_hw
5817 */
5818 if (priv->hw_restart_in_progress)
5819 list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
5820 mwl8k_remove_vif(priv, vif);
5821
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005822 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005823 mwl8k_txq_deinit(hw, i);
5824
Brian Cavagnolo99020472010-11-12 17:23:52 -08005825 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005826 if (rc)
5827 goto fail;
5828
5829 rc = mwl8k_probe_hw(hw);
5830 if (rc)
5831 goto fail;
5832
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305833 if (priv->hw_restart_in_progress)
5834 return rc;
5835
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005836 rc = mwl8k_start(hw);
5837 if (rc)
5838 goto fail;
5839
5840 rc = mwl8k_config(hw, ~0);
5841 if (rc)
5842 goto fail;
5843
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005844 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005845 rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005846 if (rc)
5847 goto fail;
5848 }
5849
5850 return rc;
5851
5852fail:
5853 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5854 return rc;
5855}
5856
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305857static const struct ieee80211_iface_limit ap_if_limits[] = {
5858 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
Yogesh Ashok Powar2acdaa72013-01-25 16:19:55 +05305859 { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305860};
5861
5862static const struct ieee80211_iface_combination ap_if_comb = {
5863 .limits = ap_if_limits,
5864 .n_limits = ARRAY_SIZE(ap_if_limits),
5865 .max_interfaces = 8,
5866 .num_different_channels = 1,
5867};
5868
5869
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005870static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5871{
5872 struct ieee80211_hw *hw = priv->hw;
5873 int i, rc;
5874
Brian Cavagnolo99020472010-11-12 17:23:52 -08005875 rc = mwl8k_load_firmware(hw);
5876 mwl8k_release_firmware(priv);
5877 if (rc) {
5878 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5879 return rc;
5880 }
5881
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005882 /*
5883 * Extra headroom is the size of the required DMA header
5884 * minus the size of the smallest 802.11 frame (CTS frame).
5885 */
5886 hw->extra_tx_headroom =
5887 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5888
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05305889 hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
5890
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005891 hw->channel_change_time = 10;
5892
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005893 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005894
5895 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005896 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Yogesh Ashok Powar2a36a0e2011-08-29 17:12:44 +05305897
5898 /*
5899 * Ask mac80211 to not to trigger PS mode
5900 * based on PM bit of incoming frames.
5901 */
5902 if (priv->ap_fw)
5903 hw->flags |= IEEE80211_HW_AP_LINK_PS;
5904
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005905 hw->vif_data_size = sizeof(struct mwl8k_vif);
5906 hw->sta_data_size = sizeof(struct mwl8k_sta);
5907
5908 priv->macids_used = 0;
5909 INIT_LIST_HEAD(&priv->vif_list);
5910
5911 /* Set default radio state and preamble */
Rusty Russell3db1cd52011-12-19 13:56:45 +00005912 priv->radio_on = false;
5913 priv->radio_short_preamble = false;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005914
5915 /* Finalize join worker */
5916 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005917 /* Handle watchdog ba events */
5918 INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305919 /* To reload the firmware if it crashes */
5920 INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005921
5922 /* TX reclaim and RX tasklets. */
5923 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5924 tasklet_disable(&priv->poll_tx_task);
5925 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5926 tasklet_disable(&priv->poll_rx_task);
5927
5928 /* Power management cookie */
5929 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5930 if (priv->cookie == NULL)
5931 return -ENOMEM;
5932
5933 mutex_init(&priv->fw_mutex);
5934 priv->fw_mutex_owner = NULL;
5935 priv->fw_mutex_depth = 0;
5936 priv->hostcmd_wait = NULL;
5937
5938 spin_lock_init(&priv->tx_lock);
5939
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005940 spin_lock_init(&priv->stream_lock);
5941
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005942 priv->tx_wait = NULL;
5943
5944 rc = mwl8k_probe_hw(hw);
5945 if (rc)
5946 goto err_free_cookie;
5947
5948 hw->wiphy->interface_modes = 0;
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305949
5950 if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005951 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
Yogesh Ashok Powar2acdaa72013-01-25 16:19:55 +05305952 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305953 hw->wiphy->iface_combinations = &ap_if_comb;
5954 hw->wiphy->n_iface_combinations = 1;
5955 }
5956
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005957 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5958 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5959
5960 rc = ieee80211_register_hw(hw);
5961 if (rc) {
5962 wiphy_err(hw->wiphy, "Cannot register device\n");
5963 goto err_unprobe_hw;
5964 }
5965
5966 return 0;
5967
5968err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005969 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005970 mwl8k_txq_deinit(hw, i);
5971 mwl8k_rxq_deinit(hw, 0);
5972
5973err_free_cookie:
5974 if (priv->cookie != NULL)
5975 pci_free_consistent(priv->pdev, 4,
5976 priv->cookie, priv->cookie_dma);
5977
5978 return rc;
5979}
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005980static int mwl8k_probe(struct pci_dev *pdev,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005981 const struct pci_device_id *id)
5982{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005983 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005984 struct ieee80211_hw *hw;
5985 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005986 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005987 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005988
5989 if (!printed_version) {
5990 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5991 printed_version = 1;
5992 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005993
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005994
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005995 rc = pci_enable_device(pdev);
5996 if (rc) {
5997 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5998 MWL8K_NAME);
5999 return rc;
6000 }
6001
6002 rc = pci_request_regions(pdev, MWL8K_NAME);
6003 if (rc) {
6004 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
6005 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01006006 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006007 }
6008
6009 pci_set_master(pdev);
6010
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006011
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006012 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6013 if (hw == NULL) {
6014 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6015 rc = -ENOMEM;
6016 goto err_free_reg;
6017 }
6018
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006019 SET_IEEE80211_DEV(hw, &pdev->dev);
6020 pci_set_drvdata(pdev, hw);
6021
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006022 priv = hw->priv;
6023 priv->hw = hw;
6024 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05006025 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006026
Nishant Sarmukadam98929822013-03-01 17:13:01 +05306027 if (id->driver_data == MWL8764)
6028 priv->is_8764 = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006029
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006030 priv->sram = pci_iomap(pdev, 0, 0x10000);
6031 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07006032 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04006033 rc = -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006034 goto err_iounmap;
6035 }
6036
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006037 /*
6038 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6039 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6040 */
6041 priv->regs = pci_iomap(pdev, 1, 0x10000);
6042 if (priv->regs == NULL) {
6043 priv->regs = pci_iomap(pdev, 2, 0x10000);
6044 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07006045 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04006046 rc = -EIO;
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006047 goto err_iounmap;
6048 }
6049 }
6050
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006051 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08006052 * Choose the initial fw image depending on user input. If a second
6053 * image is available, make it the alternative image that will be
6054 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006055 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08006056 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006057 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08006058 if (ap_mode_default && di->fw_image_ap) {
6059 priv->fw_pref = di->fw_image_ap;
6060 priv->fw_alt = di->fw_image_sta;
6061 } else if (!ap_mode_default && di->fw_image_sta) {
6062 priv->fw_pref = di->fw_image_sta;
6063 priv->fw_alt = di->fw_image_ap;
6064 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006065 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08006066 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006067 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6068 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08006069 priv->fw_pref = di->fw_image_ap;
6070 }
6071 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006072 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08006073 goto err_stop_firmware;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05306074
6075 priv->hw_restart_in_progress = false;
6076
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05306077 priv->running_bsses = 0;
6078
Brian Cavagnolo99020472010-11-12 17:23:52 -08006079 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006080
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006081err_stop_firmware:
6082 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006083
6084err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006085 if (priv->regs != NULL)
6086 pci_iounmap(pdev, priv->regs);
6087
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006088 if (priv->sram != NULL)
6089 pci_iounmap(pdev, priv->sram);
6090
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006091 pci_set_drvdata(pdev, NULL);
6092 ieee80211_free_hw(hw);
6093
6094err_free_reg:
6095 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01006096
6097err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006098 pci_disable_device(pdev);
6099
6100 return rc;
6101}
6102
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05006103static void mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006104{
6105 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6106 struct mwl8k_priv *priv;
6107 int i;
6108
6109 if (hw == NULL)
6110 return;
6111 priv = hw->priv;
6112
Brian Cavagnolo99020472010-11-12 17:23:52 -08006113 wait_for_completion(&priv->firmware_loading_complete);
6114
6115 if (priv->fw_state == FW_STATE_ERROR) {
6116 mwl8k_hw_reset(priv);
6117 goto unmap;
6118 }
6119
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006120 ieee80211_stop_queues(hw);
6121
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02006122 ieee80211_unregister_hw(hw);
6123
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01006124 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01006125 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01006126 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006127
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006128 /* Stop hardware */
6129 mwl8k_hw_reset(priv);
6130
6131 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07006132 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01006133 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006134
Brian Cavagnoloe6007072011-03-17 11:58:44 -07006135 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006136 mwl8k_txq_deinit(hw, i);
6137
6138 mwl8k_rxq_deinit(hw, 0);
6139
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02006140 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006141
Brian Cavagnolo99020472010-11-12 17:23:52 -08006142unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006143 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006144 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006145 pci_set_drvdata(pdev, NULL);
6146 ieee80211_free_hw(hw);
6147 pci_release_regions(pdev);
6148 pci_disable_device(pdev);
6149}
6150
6151static struct pci_driver mwl8k_driver = {
6152 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02006153 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006154 .probe = mwl8k_probe,
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05006155 .remove = mwl8k_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006156};
6157
Axel Lin5b0a3b72012-04-14 10:38:36 +08006158module_pci_driver(mwl8k_driver);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02006159
6160MODULE_DESCRIPTION(MWL8K_DESC);
6161MODULE_VERSION(MWL8K_VERSION);
6162MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6163MODULE_LICENSE("GPL");